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