f09a701ef1cebee15bdf4a880a88fd8477107072
[lunaix-os.git] / lunaix-os / kernel / fs / vfs.c
1 /**
2  * @file vfs.c
3  * @author Lunaixsky (zelong56@gmail.com)
4  * @brief Lunaix virtual file system - an abstraction layer for all file system.
5  * @version 0.1
6  * @date 2022-07-24
7  *
8  * @copyright Copyright (c) 2022
9  *
10  */
11
12 #include <klibc/string.h>
13 #include <lunaix/dirent.h>
14 #include <lunaix/foptions.h>
15 #include <lunaix/fs.h>
16 #include <lunaix/mm/cake.h>
17 #include <lunaix/mm/page.h>
18 #include <lunaix/mm/valloc.h>
19 #include <lunaix/process.h>
20 #include <lunaix/spike.h>
21 #include <lunaix/syscall.h>
22
23 #define PATH_DELIM '/'
24 #define DNODE_HASHTABLE_BITS 10
25 #define DNODE_HASHTABLE_SIZE (1 << DNODE_HASHTABLE_BITS)
26 #define DNODE_HASH_MASK (DNODE_HASHTABLE_SIZE - 1)
27 #define DNODE_HASHBITS (32 - DNODE_HASHTABLE_BITS)
28
29 static struct cake_pile* dnode_pile;
30 static struct cake_pile* inode_pile;
31 static struct cake_pile* file_pile;
32 static struct cake_pile* superblock_pile;
33 static struct cake_pile* fd_pile;
34
35 static struct v_superblock* root_sb;
36 static struct hbucket* dnode_cache;
37
38 static int fs_id = 0;
39
40 struct hstr vfs_ddot = HSTR("..", 2);
41 struct hstr vfs_dot = HSTR(".", 1);
42 struct hstr vfs_empty = HSTR("", 0);
43
44 struct v_dnode*
45 vfs_d_alloc();
46
47 void
48 vfs_d_free(struct v_dnode* dnode);
49
50 struct v_superblock*
51 vfs_sb_alloc();
52
53 void
54 vfs_sb_free(struct v_superblock* sb);
55
56 void
57 vfs_init()
58 {
59     // 为他们专门创建一个蛋糕堆,而不使用valloc,这样我们可以最小化内碎片的产生
60     dnode_pile = cake_new_pile("dnode_cache", sizeof(struct v_dnode), 1, 0);
61     inode_pile = cake_new_pile("inode_cache", sizeof(struct v_inode), 1, 0);
62     file_pile = cake_new_pile("file_cache", sizeof(struct v_file), 1, 0);
63     fd_pile = cake_new_pile("fd_cache", sizeof(struct v_fd), 1, 0);
64     superblock_pile =
65       cake_new_pile("sb_cache", sizeof(struct v_superblock), 1, 0);
66
67     dnode_cache = vzalloc(DNODE_HASHTABLE_SIZE * sizeof(struct hbucket));
68
69     hstr_rehash(&vfs_ddot, HSTR_FULL_HASH);
70     hstr_rehash(&vfs_dot, HSTR_FULL_HASH);
71
72     // 创建一个根superblock,用来蕴含我们的根目录。
73     root_sb = vfs_sb_alloc();
74     root_sb->root = vfs_d_alloc();
75 }
76
77 inline struct hbucket*
78 __dcache_get_bucket(struct v_dnode* parent, unsigned int hash)
79 {
80     // 与parent的指针值做加法,来减小碰撞的可能性。
81     hash += (uint32_t)parent;
82     // 确保低位更加随机
83     hash = hash ^ (hash >> DNODE_HASHBITS);
84     return &dnode_cache[hash & DNODE_HASH_MASK];
85 }
86
87 struct v_dnode*
88 vfs_dcache_lookup(struct v_dnode* parent, struct hstr* str)
89 {
90     if (!str->len || HSTR_EQ(str, &vfs_dot))
91         return parent;
92
93     if (HSTR_EQ(str, &vfs_ddot)) {
94         return parent->parent ? parent->parent : parent;
95     }
96
97     struct hbucket* slot = __dcache_get_bucket(parent, str->hash);
98
99     struct v_dnode *pos, *n;
100     hashtable_bucket_foreach(slot, pos, n, hash_list)
101     {
102         if (pos->name.hash == str->hash) {
103             return pos;
104         }
105     }
106     return NULL;
107 }
108
109 void
110 vfs_dcache_add(struct v_dnode* parent, struct v_dnode* dnode)
111 {
112     struct hbucket* bucket = __dcache_get_bucket(parent, dnode->name.hash);
113     hlist_add(&bucket->head, &dnode->hash_list);
114 }
115
116 int
117 __vfs_walk(struct v_dnode* start,
118            const char* path,
119            struct v_dnode** dentry,
120            struct hstr* component,
121            int walk_options)
122 {
123     int errno = 0;
124     int i = 0, j = 0;
125
126     if (path[0] == PATH_DELIM || !start) {
127         if ((walk_options & VFS_WALK_FSRELATIVE) && start) {
128             start = start->super_block->root;
129         } else {
130             start = root_sb->root;
131         }
132         i++;
133     }
134
135     struct v_dnode* dnode;
136     struct v_dnode* current_level = start;
137
138     char name_content[VFS_NAME_MAXLEN];
139     struct hstr name = HSTR(name_content, 0);
140
141     char current = path[i++], lookahead;
142     while (current) {
143         lookahead = path[i++];
144         if (current != PATH_DELIM) {
145             if (j >= VFS_NAME_MAXLEN - 1) {
146                 return ENAMETOOLONG;
147             }
148             if (!VFS_VALID_CHAR(current)) {
149                 return EINVAL;
150             }
151             name_content[j++] = current;
152             if (lookahead) {
153                 goto cont;
154             }
155         }
156
157         // handling cases like /^.*(\/+).*$/
158         if (lookahead == PATH_DELIM) {
159             goto cont;
160         }
161
162         name_content[j] = 0;
163         name.len = j;
164         hstr_rehash(&name, HSTR_FULL_HASH);
165
166         if (!lookahead && (walk_options & VFS_WALK_PARENT)) {
167             if (component) {
168                 component->hash = name.hash;
169                 component->len = j;
170                 strcpy(component->value, name_content);
171             }
172             break;
173         }
174
175         dnode = vfs_dcache_lookup(current_level, &name);
176
177         if (!dnode) {
178             dnode = vfs_d_alloc();
179             dnode->name = HHSTR(valloc(VFS_NAME_MAXLEN), j, name.hash);
180
181             strcpy(dnode->name.value, name_content);
182
183             errno =
184               current_level->inode->ops.dir_lookup(current_level->inode, dnode);
185
186             if (errno == ENOENT && (walk_options & VFS_WALK_MKPARENT)) {
187                 if (!current_level->inode->ops.mkdir) {
188                     errno = ENOTSUP;
189                 } else {
190                     errno = current_level->inode->ops.mkdir(
191                       current_level->inode, dnode);
192                 }
193             }
194
195             if (errno) {
196                 goto error;
197             }
198
199             vfs_dcache_add(current_level, dnode);
200
201             dnode->parent = current_level;
202             llist_append(&current_level->children, &dnode->siblings);
203         }
204
205         j = 0;
206         current_level = dnode;
207     cont:
208         current = lookahead;
209     };
210
211     *dentry = current_level;
212     return 0;
213
214 error:
215     vfree(dnode->name.value);
216     vfs_d_free(dnode);
217     *dentry = NULL;
218     return errno;
219 }
220
221 #define VFS_MAX_SYMLINK 16
222
223 int
224 vfs_walk(struct v_dnode* start,
225          const char* path,
226          struct v_dnode** dentry,
227          struct hstr* component,
228          int options)
229 {
230     struct v_dnode* interim;
231     char* pathname = path;
232     int errno = __vfs_walk(start, path, &interim, component, options);
233     int counter = 0;
234
235     while (!errno) {
236         if (counter >= VFS_MAX_SYMLINK) {
237             errno = ELOOP;
238             continue;
239         }
240         if ((interim->inode->itype & VFS_INODE_TYPE_SYMLINK) &&
241             !(options & VFS_WALK_NOFOLLOW) &&
242             interim->inode->ops.read_symlink) {
243             errno = interim->inode->ops.read_symlink(interim->inode, &pathname);
244             if (errno) {
245                 break;
246             }
247         } else {
248             break;
249         }
250         errno = __vfs_walk(start, pathname, &interim, component, options);
251         counter++;
252     }
253
254     *dentry = errno ? 0 : interim;
255
256     return errno;
257 }
258
259 int
260 vfs_mount(const char* target, const char* fs_name, bdev_t device)
261 {
262     int errno;
263     struct v_dnode* mnt;
264
265     if (!(errno = vfs_walk(NULL, target, &mnt, NULL, 0))) {
266         errno = vfs_mount_at(fs_name, device, mnt);
267     }
268
269     return errno;
270 }
271
272 int
273 vfs_unmount(const char* target)
274 {
275     int errno;
276     struct v_dnode* mnt;
277
278     if (!(errno = vfs_walk(NULL, target, &mnt, NULL, 0))) {
279         errno = vfs_unmount_at(mnt);
280     }
281
282     return errno;
283 }
284
285 int
286 vfs_mount_at(const char* fs_name, bdev_t device, struct v_dnode* mnt_point)
287 {
288     struct filesystem* fs = fsm_get(fs_name);
289     if (!fs)
290         return ENODEV;
291     struct v_superblock* sb = vfs_sb_alloc();
292     sb->dev = device;
293     sb->fs_id = fs_id++;
294
295     int errno = 0;
296     if (!(errno = fs->mount(sb, mnt_point))) {
297         sb->fs = fs;
298         sb->root = mnt_point;
299         mnt_point->super_block = sb;
300         llist_append(&root_sb->sb_list, &sb->sb_list);
301     }
302
303     return errno;
304 }
305
306 int
307 vfs_unmount_at(struct v_dnode* mnt_point)
308 {
309     int errno = 0;
310     struct v_superblock* sb = mnt_point->super_block;
311     if (!sb) {
312         return EINVAL;
313     }
314     if (!(errno = sb->fs->unmount(sb))) {
315         struct v_dnode* fs_root = sb->root;
316         llist_delete(&fs_root->siblings);
317         llist_delete(&sb->sb_list);
318         vfs_sb_free(sb);
319     }
320     return errno;
321 }
322
323 int
324 vfs_open(struct v_dnode* dnode, struct v_file** file)
325 {
326     if (!dnode->inode || !dnode->inode->ops.open) {
327         return ENOTSUP;
328     }
329
330     struct v_file* vfile = cake_grab(file_pile);
331     memset(vfile, 0, sizeof(*vfile));
332
333     vfile->dnode = dnode;
334     vfile->inode = dnode->inode;
335     vfile->ref_count = 1;
336     dnode->inode->open_count++;
337
338     if ((dnode->inode->itype & VFS_INODE_TYPE_FILE) &&
339         !dnode->inode->pg_cache) {
340         struct pcache* pcache = vzalloc(sizeof(struct pcache));
341         pcache_init(pcache);
342         dnode->inode->pg_cache = pcache;
343     }
344
345     int errno = dnode->inode->ops.open(dnode->inode, vfile);
346     if (errno) {
347         cake_release(file_pile, vfile);
348     } else {
349         *file = vfile;
350     }
351     return errno;
352 }
353
354 int
355 vfs_link(struct v_dnode* to_link, struct v_dnode* name)
356 {
357     int errno;
358     if (to_link->super_block->root != name->super_block->root) {
359         errno = EXDEV;
360     } else if (!to_link->inode->ops.link) {
361         errno = ENOTSUP;
362     } else if (!(errno = to_link->inode->ops.link(to_link->inode, name))) {
363         name->inode = to_link->inode;
364         to_link->inode->link_count++;
365     }
366
367     return errno;
368 }
369
370 int
371 vfs_close(struct v_file* file)
372 {
373     int errno = 0;
374     if (!file->ops.close || !(errno = file->ops.close(file))) {
375         if (file->inode->open_count) {
376             file->inode->open_count--;
377         }
378         pcache_commit_all(file);
379         cake_release(file_pile, file);
380     }
381     return errno;
382 }
383
384 int
385 vfs_fsync(struct v_file* file)
386 {
387     int errno = ENOTSUP;
388     if (file->ops.sync) {
389         errno = file->ops.sync(file);
390     }
391     if (!errno && file->inode->ops.sync) {
392         return file->inode->ops.sync(file->inode);
393     }
394     return errno;
395 }
396
397 int
398 vfs_alloc_fdslot(int* fd)
399 {
400     for (size_t i = 0; i < VFS_MAX_FD; i++) {
401         if (!__current->fdtable->fds[i]) {
402             *fd = i;
403             return 0;
404         }
405     }
406     return EMFILE;
407 }
408
409 struct v_superblock*
410 vfs_sb_alloc()
411 {
412     struct v_superblock* sb = cake_grab(superblock_pile);
413     memset(sb, 0, sizeof(*sb));
414     llist_init_head(&sb->sb_list);
415     return sb;
416 }
417
418 void
419 vfs_sb_free(struct v_superblock* sb)
420 {
421     cake_release(superblock_pile, sb);
422 }
423
424 struct v_dnode*
425 vfs_d_alloc()
426 {
427     struct v_dnode* dnode = cake_grab(dnode_pile);
428     memset(dnode, 0, sizeof(*dnode));
429     llist_init_head(&dnode->children);
430     dnode->name = vfs_empty;
431     return dnode;
432 }
433
434 void
435 vfs_d_free(struct v_dnode* dnode)
436 {
437     if (dnode->ops.destruct) {
438         dnode->ops.destruct(dnode);
439     }
440     cake_release(dnode_pile, dnode);
441 }
442
443 struct v_inode*
444 vfs_i_alloc()
445 {
446     struct v_inode* inode = cake_grab(inode_pile);
447     memset(inode, 0, sizeof(*inode));
448     return inode;
449 }
450
451 void
452 vfs_i_free(struct v_inode* inode)
453 {
454     cake_release(inode_pile, inode);
455 }
456
457 /* ---- System call definition and support ---- */
458
459 #define FLOCATE_CREATE_EMPTY 1
460
461 #define DO_STATUS(errno) SYSCALL_ESTATUS(__current->k_status = errno)
462
463 int
464 __vfs_try_locate_file(const char* path,
465                       struct v_dnode** fdir,
466                       struct v_dnode** file,
467                       int options)
468 {
469     char name_str[VFS_NAME_MAXLEN];
470     struct hstr name = HSTR(name_str, 0);
471     int errno;
472     if ((errno = vfs_walk(NULL, path, fdir, &name, VFS_WALK_PARENT))) {
473         return errno;
474     }
475
476     errno = vfs_walk(*fdir, name.value, file, NULL, 0);
477     if (errno != ENOENT || !(options & FLOCATE_CREATE_EMPTY)) {
478         return errno;
479     }
480
481     if (!(errno = (*fdir)->inode->ops.create((*fdir)->inode))) {
482         struct v_dnode* file_new;
483         file_new = vfs_d_alloc();
484         file_new->name = HHSTR(valloc(VFS_NAME_MAXLEN), name.len, name.hash);
485         strcpy(file_new->name.value, name_str);
486         *file = file_new;
487
488         llist_append(&(*fdir)->children, &file_new->siblings);
489     }
490
491     return errno;
492 }
493
494 int
495 __vfs_do_open(struct v_file** file_out, const char* path, int options)
496 {
497     int errno;
498     struct v_dnode *dentry, *file;
499     struct v_file* opened_file = 0;
500
501     errno = __vfs_try_locate_file(path, &dentry, &file, 0);
502
503     if (errno != ENOENT && (options & FO_CREATE)) {
504         errno = dentry->inode->ops.create(dentry->inode);
505     }
506
507     if (!errno) {
508         errno = vfs_open(file, &opened_file);
509     }
510
511     *file_out = opened_file;
512     return errno;
513 }
514
515 __DEFINE_LXSYSCALL2(int, open, const char*, path, int, options)
516 {
517     struct v_file* opened_file;
518     int errno = __vfs_do_open(&opened_file, path, options), fd;
519
520     if (!errno && !(errno = vfs_alloc_fdslot(&fd))) {
521         struct v_fd* fd_s = vzalloc(sizeof(*fd_s));
522         opened_file->f_pos =
523           opened_file->inode->fsize & -((options & FO_APPEND) != 0);
524         fd_s->file = opened_file;
525         fd_s->flags = options;
526         __current->fdtable->fds[fd] = fd_s;
527         return fd;
528     }
529
530     return DO_STATUS(errno);
531 }
532 #define TEST_FD(fd) (fd >= 0 && fd < VFS_MAX_FD)
533 #define GET_FD(fd, fd_s) (TEST_FD(fd) && (fd_s = __current->fdtable->fds[fd]))
534
535 __DEFINE_LXSYSCALL1(int, close, int, fd)
536 {
537     struct v_fd* fd_s;
538     int errno = 0;
539     if (!GET_FD(fd, fd_s)) {
540         errno = EBADF;
541         goto done_err;
542     }
543
544     if (fd_s->file->ref_count > 1) {
545         fd_s->file->ref_count--;
546     } else if ((errno = vfs_close(fd_s->file))) {
547         goto done_err;
548     }
549
550     vfree(fd_s);
551     __current->fdtable->fds[fd] = 0;
552
553 done_err:
554     return DO_STATUS(errno);
555 }
556
557 void
558 __vfs_readdir_callback(struct dir_context* dctx,
559                        const char* name,
560                        const int len,
561                        const int dtype)
562 {
563     struct dirent* dent = (struct dirent*)dctx->cb_data;
564     strncpy(dent->d_name, name, DIRENT_NAME_MAX_LEN);
565     dent->d_nlen = len;
566     dent->d_type = dtype;
567 }
568
569 __DEFINE_LXSYSCALL2(int, readdir, int, fd, struct dirent*, dent)
570 {
571     struct v_fd* fd_s;
572     int errno;
573     if (!GET_FD(fd, fd_s)) {
574         errno = EBADF;
575     } else if (!(fd_s->file->inode->itype & VFS_INODE_TYPE_DIR)) {
576         errno = ENOTDIR;
577     } else {
578         struct dir_context dctx =
579           (struct dir_context){ .cb_data = dent,
580                                 .index = dent->d_offset,
581                                 .read_complete_callback =
582                                   __vfs_readdir_callback };
583         if (dent->d_offset == 0) {
584             __vfs_readdir_callback(&dctx, vfs_dot.value, vfs_dot.len, 0);
585         } else if (dent->d_offset == 1) {
586             __vfs_readdir_callback(&dctx, vfs_ddot.value, vfs_ddot.len, 0);
587         } else {
588             dctx.index -= 2;
589             if ((errno = fd_s->file->ops.readdir(fd_s->file, &dctx))) {
590                 goto done;
591             }
592         }
593         errno = 0;
594         dent->d_offset++;
595     }
596
597 done:
598     return DO_STATUS(errno);
599 }
600
601 __DEFINE_LXSYSCALL1(int, mkdir, const char*, path)
602 {
603     struct v_dnode *parent, *dir;
604     struct hstr component = HSTR(valloc(VFS_NAME_MAXLEN), 0);
605     int errno = vfs_walk(NULL, path, &parent, &component, VFS_WALK_PARENT);
606     if (errno) {
607         goto done;
608     }
609
610     if ((parent->super_block->fs->types & FSTYPE_ROFS)) {
611         errno = ENOTSUP;
612     } else if (!parent->inode->ops.mkdir) {
613         errno = ENOTSUP;
614     } else if (!(parent->inode->itype & VFS_INODE_TYPE_DIR)) {
615         errno = ENOTDIR;
616     } else {
617         dir = vfs_d_alloc();
618         dir->name = component;
619         if (!(errno = parent->inode->ops.mkdir(parent->inode, dir))) {
620             llist_append(&parent->children, &dir->siblings);
621         } else {
622             vfs_d_free(dir);
623             vfree(component.value);
624         }
625     }
626
627 done:
628     return DO_STATUS(errno);
629 }
630
631 __DEFINE_LXSYSCALL3(int, read, int, fd, void*, buf, size_t, count)
632 {
633     int errno = 0;
634     struct v_fd* fd_s;
635     if (!GET_FD(fd, fd_s)) {
636         errno = EBADF;
637         goto done;
638     }
639
640     struct v_file* file = fd_s->file;
641     if ((file->inode->itype & VFS_INODE_TYPE_DIR)) {
642         errno = EISDIR;
643         goto done;
644     }
645
646     if ((fd_s->flags & FO_DIRECT)) {
647         errno = file->ops.read(file, buf, count, file->f_pos);
648     } else {
649         errno = pcache_read(file, buf, count);
650     }
651
652     if (errno > 0) {
653         file->f_pos += errno;
654         return errno;
655     }
656
657 done:
658     return DO_STATUS(errno);
659 }
660
661 __DEFINE_LXSYSCALL3(int, write, int, fd, void*, buf, size_t, count)
662 {
663     int errno = 0;
664     struct v_fd* fd_s;
665     if (!GET_FD(fd, fd_s)) {
666         errno = EBADF;
667         goto done;
668     }
669
670     struct v_file* file = fd_s->file;
671     if ((file->inode->itype & VFS_INODE_TYPE_DIR)) {
672         errno = EISDIR;
673         goto done;
674     }
675
676     if ((fd_s->flags & FO_DIRECT)) {
677         errno = file->ops.write(file, buf, count, file->f_pos);
678     } else {
679         errno = pcache_write(file, buf, count);
680     }
681
682     if (errno > 0) {
683         file->f_pos += errno;
684         return errno;
685     }
686
687 done:
688     return DO_STATUS(errno);
689 }
690
691 __DEFINE_LXSYSCALL3(int, lseek, int, fd, int, offset, int, options)
692 {
693     int errno = 0;
694     struct v_fd* fd_s;
695     if (!GET_FD(fd, fd_s)) {
696         errno = EBADF;
697     } else {
698         struct v_file* file = fd_s->file;
699         size_t fpos = file->f_pos;
700         switch (options) {
701             case FSEEK_CUR:
702                 fpos = (size_t)((int)file->f_pos + offset);
703                 break;
704             case FSEEK_END:
705                 fpos = (size_t)((int)file->inode->fsize + offset);
706                 break;
707             case FSEEK_SET:
708                 fpos = offset;
709                 break;
710         }
711         if (!file->ops.seek || !(errno = file->ops.seek(file, fpos))) {
712             file->f_pos = fpos;
713         }
714     }
715
716     return DO_STATUS(errno);
717 }
718
719 int
720 vfs_get_path(struct v_dnode* dnode, char* buf, size_t size, int depth)
721 {
722     if (!dnode) {
723         return 0;
724     }
725
726     if (depth > 64) {
727         return ELOOP;
728     }
729
730     size_t len = vfs_get_path(dnode->parent, buf, size, depth + 1);
731
732     if (len >= size) {
733         return len;
734     }
735
736     size_t cpy_size = MIN(dnode->name.len, size - len);
737     strncpy(buf + len, dnode->name.value, cpy_size);
738     len += cpy_size;
739
740     if (len < size) {
741         buf[len++] = PATH_DELIM;
742     }
743
744     return len;
745 }
746
747 int
748 vfs_readlink(struct v_dnode* dnode, char* buf, size_t size)
749 {
750     char* link;
751     if (dnode->inode->ops.read_symlink) {
752         int errno = dnode->inode->ops.read_symlink(dnode->inode, &link);
753         strncpy(buf, link, size);
754         return errno;
755     }
756     return 0;
757 }
758
759 __DEFINE_LXSYSCALL3(int, realpathat, int, fd, char*, buf, size_t, size)
760 {
761     int errno;
762     struct v_fd* fd_s;
763     if (!GET_FD(fd, fd_s)) {
764         errno = EBADF;
765     } else {
766         struct v_dnode* dnode;
767         errno = vfs_get_path(fd_s->file->dnode, buf, size, 0);
768     }
769
770     if (errno >= 0) {
771         return errno;
772     }
773
774     return DO_STATUS(errno);
775 }
776
777 __DEFINE_LXSYSCALL3(int, readlink, const char*, path, char*, buf, size_t, size)
778 {
779     int errno;
780     struct v_dnode* dnode;
781     if (!(errno = vfs_walk(NULL, path, &dnode, NULL, VFS_WALK_NOFOLLOW))) {
782         errno = vfs_readlink(dnode, buf, size);
783     }
784
785     if (errno >= 0) {
786         return errno;
787     }
788
789     return DO_STATUS(errno);
790 }
791
792 __DEFINE_LXSYSCALL4(int,
793                     readlinkat,
794                     int,
795                     dirfd,
796                     const char*,
797                     pathname,
798                     char*,
799                     buf,
800                     size_t,
801                     size)
802 {
803     int errno;
804     struct v_fd* fd_s;
805     if (!GET_FD(dirfd, fd_s)) {
806         errno = EBADF;
807     } else {
808         struct v_dnode* dnode;
809         if (!(errno = vfs_walk(fd_s->file->dnode,
810                                pathname,
811                                &dnode,
812                                NULL,
813                                VFS_WALK_NOFOLLOW))) {
814             errno = vfs_readlink(fd_s->file->dnode, buf, size);
815         }
816     }
817
818     if (errno >= 0) {
819         return errno;
820     }
821
822     return DO_STATUS(errno);
823 }
824
825 __DEFINE_LXSYSCALL1(int, rmdir, const char*, pathname)
826 {
827     int errno;
828     struct v_dnode* dnode;
829     if ((errno = vfs_walk(NULL, pathname, &dnode, NULL, 0))) {
830         goto done;
831     }
832     if ((dnode->super_block->fs->types & FSTYPE_ROFS)) {
833         errno = EROFS;
834         goto done;
835     }
836
837     if (dnode->inode->open_count) {
838         errno = EBUSY;
839         goto done;
840     }
841
842     if ((dnode->inode->itype & VFS_INODE_TYPE_DIR)) {
843         errno = dnode->inode->ops.rmdir(dnode->inode);
844     } else {
845         errno = ENOTDIR;
846     }
847
848 done:
849     return DO_STATUS(errno);
850 }
851
852 int
853 __vfs_do_unlink(struct v_inode* inode)
854 {
855     int errno;
856     if (inode->open_count) {
857         errno = EBUSY;
858     } else if (!(inode->itype & VFS_INODE_TYPE_DIR)) {
859         // TODO handle symbolic link and type other than regular file
860         errno = inode->ops.unlink(inode);
861         if (!errno) {
862             inode->link_count--;
863         }
864     } else {
865         errno = EISDIR;
866     }
867
868     return errno;
869 }
870
871 __DEFINE_LXSYSCALL1(int, unlink, const char*, pathname)
872 {
873     int errno;
874     struct v_dnode* dnode;
875     if ((errno = vfs_walk(NULL, pathname, &dnode, NULL, 0))) {
876         goto done;
877     }
878     if ((dnode->super_block->fs->types & FSTYPE_ROFS)) {
879         errno = EROFS;
880         goto done;
881     }
882
883     errno = __vfs_do_unlink(dnode->inode);
884
885 done:
886     return DO_STATUS(errno);
887 }
888
889 __DEFINE_LXSYSCALL2(int, unlinkat, int, fd, const char*, pathname)
890 {
891     int errno;
892     struct v_fd* fd_s;
893     if (!GET_FD(fd, fd_s)) {
894         errno = EBADF;
895     } else {
896         struct v_dnode* dnode;
897         if (!(errno = vfs_walk(fd_s->file->dnode, pathname, &dnode, NULL, 0))) {
898             errno = __vfs_do_unlink(dnode->inode);
899         }
900     }
901
902 done:
903     return DO_STATUS(errno);
904 }
905
906 __DEFINE_LXSYSCALL2(int, link, const char*, oldpath, const char*, newpath)
907 {
908     int errno;
909     struct v_dnode *dentry, *to_link, *name_dentry, *name_file;
910
911     errno = __vfs_try_locate_file(oldpath, &dentry, &to_link, 0);
912     if (!errno) {
913         errno = __vfs_try_locate_file(
914           newpath, &name_dentry, &name_file, FLOCATE_CREATE_EMPTY);
915         if (!errno) {
916             errno = EEXIST;
917         } else if (name_file) {
918             errno = vfs_link(to_link, name_file);
919         }
920     }
921     return DO_STATUS(errno);
922 }
923
924 __DEFINE_LXSYSCALL1(int, fsync, int, fildes)
925 {
926     int errno;
927     struct v_fd* fd_s;
928     if (!GET_FD(fildes, fd_s)) {
929         errno = EBADF;
930     } else {
931         errno = vfs_fsync(fd_s->file);
932     }
933
934     return DO_STATUS(errno);
935 }
936
937 int
938 __vfs_dup_fd(struct v_fd* old, struct v_fd** new)
939 {
940     int errno = 0;
941     struct v_fd* copied = cake_grab(fd_pile);
942
943     memcpy(copied, old, sizeof(struct v_fd));
944     old->file->ref_count++;
945
946     return errno;
947 }
948
949 __DEFINE_LXSYSCALL2(int, dup2, int, oldfd, int, newfd)
950 {
951     if (newfd == oldfd) {
952         return newfd;
953     }
954
955     int errno;
956     struct v_fd *oldfd_s, *newfd_s;
957     if (!GET_FD(oldfd, oldfd_s) || TEST_FD(newfd)) {
958         errno = EBADF;
959         goto done;
960     }
961     newfd_s = __current->fdtable->fds[newfd];
962     if (newfd_s && (errno = vfs_close(newfd_s))) {
963         goto done;
964     }
965
966     if (!(errno = __vfs_dup_fd(oldfd_s, &newfd_s))) {
967         __current->fdtable->fds[newfd] = newfd_s;
968         return newfd;
969     }
970
971 done:
972     return DO_STATUS(errno);
973 }
974
975 __DEFINE_LXSYSCALL1(int, dup, int, oldfd)
976 {
977     int errno, newfd;
978     struct v_fd *oldfd_s, *newfd_s;
979     if (!GET_FD(oldfd, oldfd_s)) {
980         errno = EBADF;
981     } else if (!(errno = vfs_alloc_fdslot(&newfd)) &&
982                !(errno = __vfs_dup_fd(oldfd_s, &newfd_s))) {
983         __current->fdtable->fds[newfd] = newfd_s;
984         return newfd;
985     }
986
987     return DO_STATUS(errno);
988 }
989
990 __DEFINE_LXSYSCALL2(int,
991                     symlink,
992                     const char*,
993                     pathname,
994                     const char*,
995                     link_target)
996 {
997     int errno;
998     struct v_dnode* dnode;
999     if ((errno = vfs_walk(NULL, pathname, &dnode, NULL, 0))) {
1000         goto done;
1001     }
1002     if ((dnode->super_block->fs->types & FSTYPE_ROFS)) {
1003         errno = EROFS;
1004         goto done;
1005     }
1006     if (!dnode->inode->ops.symlink) {
1007         errno = ENOTSUP;
1008         goto done;
1009     }
1010
1011     errno = dnode->inode->ops.symlink(dnode->inode, link_target);
1012
1013 done:
1014     return DO_STATUS(errno);
1015 }