feat: lru eviction policy on page caches
[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     const 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(__current->cwd, 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(__current->cwd, 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
340     if ((inode->itype & VFS_IFFILE) && !inode->pg_cache) {
341         struct pcache* pcache = vzalloc(sizeof(struct pcache));
342         pcache_init(pcache);
343         pcache->master = inode;
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         dnode->ref_count++;
352         inode->open_count++;
353         *file = vfile;
354     }
355
356     return errno;
357 }
358
359 int
360 vfs_link(struct v_dnode* to_link, struct v_dnode* name)
361 {
362     int errno;
363     if (to_link->super_block->root != name->super_block->root) {
364         errno = EXDEV;
365     } else if (!to_link->inode->ops.link) {
366         errno = ENOTSUP;
367     } else if (!(errno = to_link->inode->ops.link(to_link->inode, name))) {
368         name->inode = to_link->inode;
369         to_link->inode->link_count++;
370     }
371
372     return errno;
373 }
374
375 int
376 vfs_close(struct v_file* file)
377 {
378     int errno = 0;
379     if (!file->ops.close || !(errno = file->ops.close(file))) {
380         file->dnode->ref_count--;
381         file->inode->open_count--;
382         pcache_commit_all(file->inode);
383         cake_release(file_pile, file);
384     }
385     return errno;
386 }
387
388 int
389 vfs_fsync(struct v_file* file)
390 {
391     int errno = ENOTSUP;
392     pcache_commit_all(file->inode);
393     if (file->ops.sync) {
394         errno = file->ops.sync(file->inode);
395     }
396     return errno;
397 }
398
399 int
400 vfs_alloc_fdslot(int* fd)
401 {
402     for (size_t i = 0; i < VFS_MAX_FD; i++) {
403         if (!__current->fdtable->fds[i]) {
404             *fd = i;
405             return 0;
406         }
407     }
408     return EMFILE;
409 }
410
411 struct v_superblock*
412 vfs_sb_alloc()
413 {
414     struct v_superblock* sb = cake_grab(superblock_pile);
415     memset(sb, 0, sizeof(*sb));
416     llist_init_head(&sb->sb_list);
417     return sb;
418 }
419
420 void
421 vfs_sb_free(struct v_superblock* sb)
422 {
423     cake_release(superblock_pile, sb);
424 }
425
426 struct v_dnode*
427 vfs_d_alloc()
428 {
429     struct v_dnode* dnode = cake_grab(dnode_pile);
430     memset(dnode, 0, sizeof(*dnode));
431     llist_init_head(&dnode->children);
432     dnode->name = vfs_empty;
433     return dnode;
434 }
435
436 void
437 vfs_d_free(struct v_dnode* dnode)
438 {
439     if (dnode->ops.destruct) {
440         dnode->ops.destruct(dnode);
441     }
442     cake_release(dnode_pile, dnode);
443 }
444
445 struct v_inode*
446 vfs_i_alloc()
447 {
448     struct v_inode* inode = cake_grab(inode_pile);
449     memset(inode, 0, sizeof(*inode));
450     return inode;
451 }
452
453 void
454 vfs_i_free(struct v_inode* inode)
455 {
456     cake_release(inode_pile, inode);
457 }
458
459 /* ---- System call definition and support ---- */
460
461 #define FLOCATE_CREATE_EMPTY 1
462
463 #define DO_STATUS(errno) SYSCALL_ESTATUS(__current->k_status = errno)
464 #define DO_STATUS_OR_RETURN(errno) ({ errno < 0 ? DO_STATUS(errno) : errno; })
465
466 #define TEST_FD(fd) (fd >= 0 && fd < VFS_MAX_FD)
467
468 int
469 __vfs_getfd(int fd, struct v_fd** fd_s)
470 {
471     if (TEST_FD(fd) && (*fd_s = __current->fdtable->fds[fd])) {
472         return 0;
473     }
474     return EBADF;
475 }
476
477 int
478 __vfs_try_locate_file(const char* path,
479                       struct v_dnode** fdir,
480                       struct v_dnode** file,
481                       int options)
482 {
483     char name_str[VFS_NAME_MAXLEN];
484     struct hstr name = HSTR(name_str, 0);
485     int errno;
486     if ((errno =
487            vfs_walk(__current->cwd, path, fdir, &name, VFS_WALK_PARENT))) {
488         return errno;
489     }
490
491     errno = vfs_walk(*fdir, name.value, file, NULL, 0);
492     if (errno != ENOENT || !(options & FLOCATE_CREATE_EMPTY)) {
493         return errno;
494     }
495
496     if (!(errno = (*fdir)->inode->ops.create((*fdir)->inode))) {
497         struct v_dnode* file_new;
498         file_new = vfs_d_alloc();
499         file_new->name = HHSTR(valloc(VFS_NAME_MAXLEN), name.len, name.hash);
500         strcpy(file_new->name.value, name_str);
501         *file = file_new;
502
503         llist_append(&(*fdir)->children, &file_new->siblings);
504     }
505
506     return errno;
507 }
508
509 int
510 vfs_do_open(const char* path, int options)
511 {
512     int errno, fd;
513     struct v_dnode *dentry, *file;
514     struct v_file* ofile = 0;
515
516     errno = __vfs_try_locate_file(path, &dentry, &file, 0);
517
518     if (errno != ENOENT && (options & FO_CREATE)) {
519         errno = dentry->inode->ops.create(dentry->inode);
520     }
521
522     if (!errno && (errno = vfs_open(file, &ofile))) {
523         return errno;
524     }
525
526     struct v_inode* o_inode = ofile->inode;
527     if (!(o_inode->itype & VFS_IFSEQDEV) && !(options & FO_DIRECT)) {
528         // XXX Change here accordingly when signature of pcache_r/w changed.
529         ofile->ops.read = pcache_read;
530         ofile->ops.write = pcache_write;
531     }
532
533     if (!errno && !(errno = vfs_alloc_fdslot(&fd))) {
534         struct v_fd* fd_s = vzalloc(sizeof(*fd_s));
535         ofile->f_pos = ofile->inode->fsize & -((options & FO_APPEND) != 0);
536         fd_s->file = ofile;
537         fd_s->flags = options;
538         __current->fdtable->fds[fd] = fd_s;
539         return fd;
540     }
541
542     return errno;
543 }
544
545 __DEFINE_LXSYSCALL2(int, open, const char*, path, int, options)
546 {
547     int errno = vfs_do_open(path, options);
548     return DO_STATUS_OR_RETURN(errno);
549 }
550
551 __DEFINE_LXSYSCALL1(int, close, int, fd)
552 {
553     struct v_fd* fd_s;
554     int errno = 0;
555     if ((errno = __vfs_getfd(fd, &fd_s))) {
556         goto done_err;
557     }
558
559     if (fd_s->file->ref_count > 1) {
560         fd_s->file->ref_count--;
561     } else if ((errno = vfs_close(fd_s->file))) {
562         goto done_err;
563     }
564
565     vfree(fd_s);
566     __current->fdtable->fds[fd] = 0;
567
568 done_err:
569     return DO_STATUS(errno);
570 }
571
572 void
573 __vfs_readdir_callback(struct dir_context* dctx,
574                        const char* name,
575                        const int len,
576                        const int dtype)
577 {
578     struct dirent* dent = (struct dirent*)dctx->cb_data;
579     strncpy(dent->d_name, name, DIRENT_NAME_MAX_LEN);
580     dent->d_nlen = len;
581     dent->d_type = dtype;
582 }
583
584 __DEFINE_LXSYSCALL2(int, readdir, int, fd, struct dirent*, dent)
585 {
586     struct v_fd* fd_s;
587     int errno;
588
589     if ((errno = __vfs_getfd(fd, &fd_s))) {
590         goto done;
591     }
592
593     if (!(fd_s->file->inode->itype & VFS_IFDIR)) {
594         errno = ENOTDIR;
595     } else {
596         struct dir_context dctx =
597           (struct dir_context){ .cb_data = dent,
598                                 .index = dent->d_offset,
599                                 .read_complete_callback =
600                                   __vfs_readdir_callback };
601         if (dent->d_offset == 0) {
602             __vfs_readdir_callback(&dctx, vfs_dot.value, vfs_dot.len, 0);
603         } else if (dent->d_offset == 1) {
604             __vfs_readdir_callback(&dctx, vfs_ddot.value, vfs_ddot.len, 0);
605         } else {
606             dctx.index -= 2;
607             if ((errno = fd_s->file->ops.readdir(fd_s->file->inode, &dctx))) {
608                 goto done;
609             }
610         }
611         errno = 0;
612         dent->d_offset++;
613     }
614
615 done:
616     return DO_STATUS(errno);
617 }
618
619 __DEFINE_LXSYSCALL1(int, mkdir, const char*, path)
620 {
621     struct v_dnode *parent, *dir;
622     struct hstr component = HSTR(valloc(VFS_NAME_MAXLEN), 0);
623     int errno =
624       vfs_walk(__current->cwd, path, &parent, &component, VFS_WALK_PARENT);
625     if (errno) {
626         goto done;
627     }
628
629     if ((parent->super_block->fs->types & FSTYPE_ROFS)) {
630         errno = ENOTSUP;
631     } else if (!parent->inode->ops.mkdir) {
632         errno = ENOTSUP;
633     } else if (!(parent->inode->itype & VFS_IFDIR)) {
634         errno = ENOTDIR;
635     } else {
636         dir = vfs_d_alloc();
637         dir->name = component;
638         if (!(errno = parent->inode->ops.mkdir(parent->inode, dir))) {
639             llist_append(&parent->children, &dir->siblings);
640         } else {
641             vfs_d_free(dir);
642             vfree(component.value);
643         }
644     }
645
646 done:
647     return DO_STATUS(errno);
648 }
649
650 __DEFINE_LXSYSCALL3(int, read, int, fd, void*, buf, size_t, count)
651 {
652     int errno = 0;
653     struct v_fd* fd_s;
654     if ((errno = __vfs_getfd(fd, &fd_s))) {
655         goto done;
656     }
657
658     struct v_file* file = fd_s->file;
659     if ((file->inode->itype & VFS_IFDIR)) {
660         errno = EISDIR;
661         goto done;
662     }
663
664     __SYSCALL_INTERRUPTIBLE(
665       { errno = file->ops.read(file->inode, buf, count, file->f_pos); })
666
667     if (errno > 0) {
668         file->f_pos += errno;
669         return errno;
670     }
671
672 done:
673     return DO_STATUS(errno);
674 }
675
676 __DEFINE_LXSYSCALL3(int, write, int, fd, void*, buf, size_t, count)
677 {
678     int errno = 0;
679     struct v_fd* fd_s;
680     if ((errno = __vfs_getfd(fd, &fd_s))) {
681         goto done;
682     }
683
684     struct v_file* file = fd_s->file;
685     if ((file->inode->itype & VFS_IFDIR)) {
686         errno = EISDIR;
687         goto done;
688     }
689
690     __SYSCALL_INTERRUPTIBLE(
691       { errno = file->ops.write(file->inode, buf, count, file->f_pos); })
692
693     if (errno > 0) {
694         file->f_pos += errno;
695         return errno;
696     }
697
698 done:
699     return DO_STATUS(errno);
700 }
701
702 __DEFINE_LXSYSCALL3(int, lseek, int, fd, int, offset, int, options)
703 {
704     int errno = 0;
705     struct v_fd* fd_s;
706     if ((errno = __vfs_getfd(fd, &fd_s))) {
707         goto done;
708     }
709
710     struct v_file* file = fd_s->file;
711     size_t fpos = file->f_pos;
712     switch (options) {
713         case FSEEK_CUR:
714             fpos = (size_t)((int)file->f_pos + offset);
715             break;
716         case FSEEK_END:
717             fpos = (size_t)((int)file->inode->fsize + offset);
718             break;
719         case FSEEK_SET:
720             fpos = offset;
721             break;
722     }
723     if (!file->ops.seek || !(errno = file->ops.seek(file->inode, fpos))) {
724         file->f_pos = fpos;
725     }
726
727 done:
728     return DO_STATUS(errno);
729 }
730
731 int
732 vfs_get_path(struct v_dnode* dnode, char* buf, size_t size, int depth)
733 {
734     if (!dnode) {
735         return 0;
736     }
737
738     if (depth > 64) {
739         return ELOOP;
740     }
741
742     size_t len = vfs_get_path(dnode->parent, buf, size, depth + 1);
743
744     if (len >= size) {
745         return len;
746     }
747
748     size_t cpy_size = MIN(dnode->name.len, size - len);
749     strncpy(buf + len, dnode->name.value, cpy_size);
750     len += cpy_size;
751
752     if (len < size) {
753         buf[len++] = PATH_DELIM;
754     }
755
756     return len;
757 }
758
759 int
760 vfs_readlink(struct v_dnode* dnode, char* buf, size_t size)
761 {
762     const char* link;
763     if (dnode->inode->ops.read_symlink) {
764         int errno = dnode->inode->ops.read_symlink(dnode->inode, &link);
765         strncpy(buf, link, size);
766         return errno;
767     }
768     return 0;
769 }
770
771 __DEFINE_LXSYSCALL3(int, realpathat, int, fd, char*, buf, size_t, size)
772 {
773     int errno;
774     struct v_fd* fd_s;
775     if ((errno = __vfs_getfd(fd, &fd_s))) {
776         goto done;
777     }
778
779     struct v_dnode* dnode;
780     errno = vfs_get_path(fd_s->file->dnode, buf, size, 0);
781
782     if (errno >= 0) {
783         return errno;
784     }
785
786 done:
787     return DO_STATUS(errno);
788 }
789
790 __DEFINE_LXSYSCALL3(int, readlink, const char*, path, char*, buf, size_t, size)
791 {
792     int errno;
793     struct v_dnode* dnode;
794     if (!(errno =
795             vfs_walk(__current->cwd, path, &dnode, NULL, VFS_WALK_NOFOLLOW))) {
796         errno = vfs_readlink(dnode, buf, size);
797     }
798
799     if (errno >= 0) {
800         return errno;
801     }
802
803     return DO_STATUS(errno);
804 }
805
806 __DEFINE_LXSYSCALL4(int,
807                     readlinkat,
808                     int,
809                     dirfd,
810                     const char*,
811                     pathname,
812                     char*,
813                     buf,
814                     size_t,
815                     size)
816 {
817     int errno;
818     struct v_fd* fd_s;
819     if ((errno = __vfs_getfd(dirfd, &fd_s))) {
820         goto done;
821     }
822
823     struct v_dnode* dnode;
824     if (!(errno = vfs_walk(
825             fd_s->file->dnode, pathname, &dnode, NULL, VFS_WALK_NOFOLLOW))) {
826         errno = vfs_readlink(fd_s->file->dnode, buf, size);
827     }
828
829     if (errno >= 0) {
830         return errno;
831     }
832
833 done:
834     return DO_STATUS(errno);
835 }
836
837 __DEFINE_LXSYSCALL1(int, rmdir, const char*, pathname)
838 {
839     int errno;
840     struct v_dnode* dnode;
841     if ((errno = vfs_walk(__current->cwd, pathname, &dnode, NULL, 0))) {
842         goto done;
843     }
844     if ((dnode->super_block->fs->types & FSTYPE_ROFS)) {
845         errno = EROFS;
846         goto done;
847     }
848
849     if (dnode->inode->open_count) {
850         errno = EBUSY;
851         goto done;
852     }
853
854     if ((dnode->inode->itype & VFS_IFDIR)) {
855         errno = dnode->inode->ops.rmdir(dnode->inode);
856     } else {
857         errno = ENOTDIR;
858     }
859
860 done:
861     return DO_STATUS(errno);
862 }
863
864 int
865 __vfs_do_unlink(struct v_inode* inode)
866 {
867     int errno;
868     if (inode->open_count) {
869         errno = EBUSY;
870     } else if (!(inode->itype & VFS_IFDIR)) {
871         // TODO handle symbolic link and type other than regular file
872         errno = inode->ops.unlink(inode);
873         if (!errno) {
874             inode->link_count--;
875         }
876     } else {
877         errno = EISDIR;
878     }
879
880     return errno;
881 }
882
883 __DEFINE_LXSYSCALL1(int, unlink, const char*, pathname)
884 {
885     int errno;
886     struct v_dnode* dnode;
887     if ((errno = vfs_walk(__current->cwd, pathname, &dnode, NULL, 0))) {
888         goto done;
889     }
890     if ((dnode->super_block->fs->types & FSTYPE_ROFS)) {
891         errno = EROFS;
892         goto done;
893     }
894
895     errno = __vfs_do_unlink(dnode->inode);
896
897 done:
898     return DO_STATUS(errno);
899 }
900
901 __DEFINE_LXSYSCALL2(int, unlinkat, int, fd, const char*, pathname)
902 {
903     int errno;
904     struct v_fd* fd_s;
905     if ((errno = __vfs_getfd(fd, &fd_s))) {
906         goto done;
907     }
908
909     struct v_dnode* dnode;
910     if (!(errno = vfs_walk(fd_s->file->dnode, pathname, &dnode, NULL, 0))) {
911         errno = __vfs_do_unlink(dnode->inode);
912     }
913
914 done:
915     return DO_STATUS(errno);
916 }
917
918 __DEFINE_LXSYSCALL2(int, link, const char*, oldpath, const char*, newpath)
919 {
920     int errno;
921     struct v_dnode *dentry, *to_link, *name_dentry, *name_file;
922
923     errno = __vfs_try_locate_file(oldpath, &dentry, &to_link, 0);
924     if (!errno) {
925         errno = __vfs_try_locate_file(
926           newpath, &name_dentry, &name_file, FLOCATE_CREATE_EMPTY);
927         if (!errno) {
928             errno = EEXIST;
929         } else if (name_file) {
930             errno = vfs_link(to_link, name_file);
931         }
932     }
933     return DO_STATUS(errno);
934 }
935
936 __DEFINE_LXSYSCALL1(int, fsync, int, fildes)
937 {
938     int errno;
939     struct v_fd* fd_s;
940     if (!(errno = __vfs_getfd(fildes, &fd_s))) {
941         errno = vfs_fsync(fd_s->file);
942     }
943
944     return DO_STATUS(errno);
945 }
946
947 int
948 vfs_dup_fd(struct v_fd* old, struct v_fd** new)
949 {
950     int errno = 0;
951     struct v_fd* copied = cake_grab(fd_pile);
952
953     memcpy(copied, old, sizeof(struct v_fd));
954     old->file->ref_count++;
955
956     *new = copied;
957
958     return errno;
959 }
960
961 int
962 vfs_dup2(int oldfd, int newfd)
963 {
964     if (newfd == oldfd) {
965         return newfd;
966     }
967
968     int errno;
969     struct v_fd *oldfd_s, *newfd_s;
970     if ((errno = __vfs_getfd(oldfd, &oldfd_s))) {
971         goto done;
972     }
973
974     if (!TEST_FD(newfd)) {
975         errno = EBADF;
976         goto done;
977     }
978
979     newfd_s = __current->fdtable->fds[newfd];
980     if (newfd_s && (errno = vfs_close(newfd_s->file))) {
981         goto done;
982     }
983
984     if (!(errno = vfs_dup_fd(oldfd_s, &newfd_s))) {
985         __current->fdtable->fds[newfd] = newfd_s;
986         return newfd;
987     }
988
989 done:
990     return DO_STATUS(errno);
991 }
992
993 __DEFINE_LXSYSCALL2(int, dup2, int, oldfd, int, newfd)
994 {
995     return vfs_dup2(oldfd, newfd);
996 }
997
998 __DEFINE_LXSYSCALL1(int, dup, int, oldfd)
999 {
1000     int errno, newfd;
1001     struct v_fd *oldfd_s, *newfd_s;
1002     if ((errno = __vfs_getfd(oldfd, &oldfd_s))) {
1003         goto done;
1004     }
1005
1006     if (!(errno = vfs_alloc_fdslot(&newfd)) &&
1007         !(errno = vfs_dup_fd(oldfd_s, &newfd_s))) {
1008         __current->fdtable->fds[newfd] = newfd_s;
1009         return newfd;
1010     }
1011
1012 done:
1013     return DO_STATUS(errno);
1014 }
1015
1016 __DEFINE_LXSYSCALL2(int,
1017                     symlink,
1018                     const char*,
1019                     pathname,
1020                     const char*,
1021                     link_target)
1022 {
1023     int errno;
1024     struct v_dnode* dnode;
1025     if ((errno = vfs_walk(__current->cwd, pathname, &dnode, NULL, 0))) {
1026         goto done;
1027     }
1028     if ((dnode->super_block->fs->types & FSTYPE_ROFS)) {
1029         errno = EROFS;
1030         goto done;
1031     }
1032     if (!dnode->inode->ops.symlink) {
1033         errno = ENOTSUP;
1034         goto done;
1035     }
1036
1037     errno = dnode->inode->ops.symlink(dnode->inode, link_target);
1038
1039 done:
1040     return DO_STATUS(errno);
1041 }
1042
1043 int
1044 __vfs_do_chdir(struct v_dnode* dnode)
1045 {
1046     int errno = 0;
1047     if (!(dnode->inode->itype & VFS_IFDIR)) {
1048         errno = ENOTDIR;
1049         goto done;
1050     }
1051
1052     if (__current->cwd) {
1053         __current->cwd->ref_count--;
1054     }
1055
1056     dnode->ref_count++;
1057     __current->cwd = dnode;
1058
1059 done:
1060     return errno;
1061 }
1062
1063 __DEFINE_LXSYSCALL1(int, chdir, const char*, path)
1064 {
1065     struct v_dnode* dnode;
1066     int errno = 0;
1067
1068     if ((errno = vfs_walk(__current->cwd, path, &dnode, NULL, 0))) {
1069         goto done;
1070     }
1071
1072     errno = __vfs_do_chdir(dnode);
1073
1074 done:
1075     return DO_STATUS(errno);
1076 }
1077
1078 __DEFINE_LXSYSCALL1(int, fchdir, int, fd)
1079 {
1080     struct v_fd* fd_s;
1081     int errno = 0;
1082
1083     if ((errno = __vfs_getfd(fd, &fd_s))) {
1084         goto done;
1085     }
1086
1087     errno = __vfs_do_chdir(fd_s->file->dnode);
1088
1089 done:
1090     return DO_STATUS(errno);
1091 }
1092
1093 __DEFINE_LXSYSCALL2(char*, getcwd, char*, buf, size_t, size)
1094 {
1095     int errno = 0;
1096     char* ret_ptr = 0;
1097     if (size < 2) {
1098         errno = ERANGE;
1099         goto done;
1100     }
1101
1102     size_t len = 0;
1103
1104     if (!__current->cwd) {
1105         *buf = PATH_DELIM;
1106         len = 1;
1107     } else {
1108         len = vfs_get_path(__current->cwd, buf, size, 0);
1109         if (len == size) {
1110             errno = ERANGE;
1111             goto done;
1112         }
1113     }
1114
1115     buf[len + 1] = '\0';
1116
1117     ret_ptr = buf;
1118
1119 done:
1120     __current->k_status = errno;
1121     return ret_ptr;
1122 }