feat: symlink(2) and realpathat syscall
[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 VFS_EINVLD;
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 =
251           __vfs_walk_internal(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 VFS_ENOFS;
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 VFS_EBADMNT;
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_file* vfile = cake_grab(file_pile);
332     memset(vfile, 0, sizeof(*vfile));
333
334     vfile->dnode = dnode;
335     vfile->inode = dnode->inode;
336     dnode->inode->open_count++;
337
338     int errno = dnode->inode->ops.open(dnode->inode, vfile);
339     if (errno) {
340         cake_release(file_pile, vfile);
341     } else {
342         *file = vfile;
343     }
344     return errno;
345 }
346
347 int
348 vfs_link(struct v_dnode* to_link, struct v_dnode* name)
349 {
350     int errno;
351     if (to_link->super_block->root != name->super_block->root) {
352         errno = EXDEV;
353     } else if (!to_link->inode->ops.link) {
354         errno = ENOTSUP;
355     } else if (!(errno = to_link->inode->ops.link(to_link->inode, name))) {
356         name->inode = to_link->inode;
357         to_link->inode->link_count++;
358     }
359
360     return errno;
361 }
362
363 int
364 vfs_close(struct v_file* file)
365 {
366     if (!file->ops.close) {
367         return ENOTSUP;
368     }
369
370     int errno = file->ops.close(file);
371     if (!errno) {
372         if (file->inode->open_count) {
373             file->inode->open_count--;
374         }
375         cake_release(file_pile, file);
376     }
377     return errno;
378 }
379
380 int
381 vfs_fsync(struct v_file* file)
382 {
383     int errno = ENOTSUP;
384     if (file->ops.sync) {
385         errno = file->ops.sync(file);
386     }
387     if (!errno && file->inode->ops.sync) {
388         return file->inode->ops.sync(file->inode);
389     }
390     return errno;
391 }
392
393 int
394 vfs_alloc_fdslot(int* fd)
395 {
396     for (size_t i = 0; i < VFS_MAX_FD; i++) {
397         if (!__current->fdtable->fds[i]) {
398             *fd = i;
399             return 0;
400         }
401     }
402     return EMFILE;
403 }
404
405 struct v_superblock*
406 vfs_sb_alloc()
407 {
408     struct v_superblock* sb = cake_grab(superblock_pile);
409     memset(sb, 0, sizeof(*sb));
410     llist_init_head(&sb->sb_list);
411     return sb;
412 }
413
414 void
415 vfs_sb_free(struct v_superblock* sb)
416 {
417     cake_release(superblock_pile, sb);
418 }
419
420 struct v_dnode*
421 vfs_d_alloc()
422 {
423     struct v_dnode* dnode = cake_grab(dnode_pile);
424     memset(dnode, 0, sizeof(*dnode));
425     llist_init_head(&dnode->children);
426     dnode->name = vfs_empty;
427     return dnode;
428 }
429
430 void
431 vfs_d_free(struct v_dnode* dnode)
432 {
433     if (dnode->ops.destruct) {
434         dnode->ops.destruct(dnode);
435     }
436     cake_release(dnode_pile, dnode);
437 }
438
439 struct v_inode*
440 vfs_i_alloc()
441 {
442     struct v_inode* inode = cake_grab(inode_pile);
443     memset(inode, 0, sizeof(*inode));
444
445     return inode;
446 }
447
448 void
449 vfs_i_free(struct v_inode* inode)
450 {
451     cake_release(inode_pile, inode);
452 }
453
454 /* ---- System call definition and support ---- */
455
456 #define FLOCATE_CREATE_EMPTY 1
457
458 #define DO_STATUS(errno) SYSCALL_ESTATUS(__current->k_status = errno)
459
460 int
461 __vfs_try_locate_file(const char* path,
462                       struct v_dnode** fdir,
463                       struct v_dnode** file,
464                       int options)
465 {
466     char name_str[VFS_NAME_MAXLEN];
467     struct hstr name = HSTR(name_str, 0);
468     int errno;
469     if (!(errno = vfs_walk(NULL, path, fdir, &name, VFS_WALK_PARENT))) {
470         errno = vfs_walk(*fdir, name.value, file, NULL, 0);
471         if (errno == ENOENT && (options & FLOCATE_CREATE_EMPTY)) {
472             struct v_dnode* file_new;
473             file_new = vfs_d_alloc();
474             file_new->name =
475               HHSTR(valloc(VFS_NAME_MAXLEN), name.len, name.hash);
476             strcpy(file_new->name.value, name_str);
477             *file = file_new;
478
479             llist_append(&(*fdir)->children, &file_new->siblings);
480         }
481     }
482
483     return errno;
484 }
485
486 int
487 __vfs_do_open(struct v_file** file_out, const char* path, int options)
488 {
489     int errno;
490     struct v_dnode *dentry, *file;
491     struct v_file* opened_file = 0;
492
493     errno = __vfs_try_locate_file(path, &dentry, &file, 0);
494
495     if (errno != ENOENT && (options & FO_CREATE)) {
496         errno = dentry->inode->ops.create(dentry->inode, opened_file);
497     } else if (!errno) {
498         errno = vfs_open(file, &opened_file);
499     }
500
501     *file_out = opened_file;
502     return errno;
503 }
504
505 __DEFINE_LXSYSCALL2(int, open, const char*, path, int, options)
506 {
507     struct v_file* opened_file;
508     int errno = __vfs_do_open(&opened_file, path, options), fd;
509
510     if (!errno && !(errno = vfs_alloc_fdslot(&fd))) {
511         struct v_fd* fd_s = vzalloc(sizeof(*fd_s));
512         fd_s->file = opened_file;
513         fd_s->pos = opened_file->inode->fsize & -((options & FO_APPEND) != 0);
514         __current->fdtable->fds[fd] = fd_s;
515         return fd;
516     }
517
518     return DO_STATUS(errno);
519 }
520 #define TEST_FD(fd) (fd >= 0 && fd < VFS_MAX_FD)
521 #define GET_FD(fd, fd_s) (TEST_FD(fd) && (fd_s = __current->fdtable->fds[fd]))
522
523 __DEFINE_LXSYSCALL1(int, close, int, fd)
524 {
525     struct v_fd* fd_s;
526     int errno;
527     if (!GET_FD(fd, fd_s)) {
528         errno = EBADF;
529     } else if (!(errno = vfs_close(fd_s->file))) {
530         vfree(fd_s);
531         __current->fdtable->fds[fd] = 0;
532     }
533
534     return DO_STATUS(errno);
535 }
536
537 void
538 __vfs_readdir_callback(struct dir_context* dctx,
539                        const char* name,
540                        const int len,
541                        const int dtype)
542 {
543     struct dirent* dent = (struct dirent*)dctx->cb_data;
544     strncpy(dent->d_name, name, DIRENT_NAME_MAX_LEN);
545     dent->d_nlen = len;
546     dent->d_type = dtype;
547 }
548
549 __DEFINE_LXSYSCALL2(int, readdir, int, fd, struct dirent*, dent)
550 {
551     struct v_fd* fd_s;
552     int errno;
553     if (!GET_FD(fd, fd_s)) {
554         errno = EBADF;
555     } else if (!(fd_s->file->inode->itype & VFS_INODE_TYPE_DIR)) {
556         errno = ENOTDIR;
557     } else {
558         struct dir_context dctx =
559           (struct dir_context){ .cb_data = dent,
560                                 .index = dent->d_offset,
561                                 .read_complete_callback =
562                                   __vfs_readdir_callback };
563         if (dent->d_offset == 0) {
564             __vfs_readdir_callback(&dctx, vfs_dot.value, vfs_dot.len, 0);
565         } else if (dent->d_offset == 1) {
566             __vfs_readdir_callback(&dctx, vfs_ddot.value, vfs_ddot.len, 0);
567         } else {
568             dctx.index -= 2;
569             if ((errno = fd_s->file->ops.readdir(fd_s->file, &dctx))) {
570                 goto done;
571             }
572         }
573         errno = 0;
574         dent->d_offset++;
575     }
576
577 done:
578     return DO_STATUS(errno);
579 }
580
581 __DEFINE_LXSYSCALL1(int, mkdir, const char*, path)
582 {
583     struct v_dnode *parent, *dir;
584     struct hstr component = HSTR(valloc(VFS_NAME_MAXLEN), 0);
585     int errno = vfs_walk(NULL, path, &parent, &component, VFS_WALK_PARENT);
586     if (errno) {
587         goto done;
588     }
589
590     if ((parent->super_block->fs->types & FSTYPE_ROFS)) {
591         errno = ENOTSUP;
592     } else if (!parent->inode->ops.mkdir) {
593         errno = ENOTSUP;
594     } else if (!(parent->inode->itype & VFS_INODE_TYPE_DIR)) {
595         errno = ENOTDIR;
596     } else {
597         dir = vfs_d_alloc();
598         dir->name = component;
599         if (!(errno = parent->inode->ops.mkdir(parent->inode, dir))) {
600             llist_append(&parent->children, &dir->siblings);
601         } else {
602             vfs_d_free(dir);
603             vfree(component.value);
604         }
605     }
606
607 done:
608     return DO_STATUS(errno);
609 }
610
611 __DEFINE_LXSYSCALL3(int, read, int, fd, void*, buf, size_t, count)
612 {
613     int errno = 0;
614     struct v_fd* fd_s;
615     if (!GET_FD(fd, fd_s)) {
616         errno = EBADF;
617     } else {
618         struct v_file* file = fd_s->file;
619         file->f_pos = fd_s->pos;
620         if ((errno = file->ops.read(file, buf, count)) >= 0) {
621             fd_s->pos += errno;
622         }
623     }
624
625     return DO_STATUS(errno);
626 }
627
628 __DEFINE_LXSYSCALL3(int, write, int, fd, void*, buf, size_t, count)
629 {
630     int errno = 0;
631     struct v_fd* fd_s;
632     if (!GET_FD(fd, fd_s)) {
633         errno = EBADF;
634     } else {
635         struct v_file* file = fd_s->file;
636         file->f_pos = fd_s->pos;
637         if ((errno = file->ops.write(file, buf, count)) >= 0) {
638             fd_s->pos += errno;
639         }
640     }
641
642     return DO_STATUS(errno);
643 }
644
645 __DEFINE_LXSYSCALL3(int, lseek, int, fd, int, offset, int, options)
646 {
647     int errno = 0;
648     struct v_fd* fd_s;
649     if (!GET_FD(fd, fd_s)) {
650         errno = EBADF;
651     } else {
652         size_t fpos = fd_s->file->f_pos;
653         switch (options) {
654             case FSEEK_CUR:
655                 fpos = (size_t)((int)fd_s->file->f_pos + offset);
656                 break;
657             case FSEEK_END:
658                 fpos = (size_t)((int)fd_s->file->inode->fsize + offset);
659                 break;
660             case FSEEK_SET:
661                 fpos = offset;
662                 break;
663
664             default:
665                 break;
666         }
667         fd_s->pos = fpos;
668     }
669
670     return DO_STATUS(errno);
671 }
672
673 int
674 vfs_get_path(struct v_dnode* dnode, char* buf, size_t size, int depth)
675 {
676     if (!dnode) {
677         return 0;
678     }
679
680     if (depth > 64) {
681         return ELOOP;
682     }
683
684     size_t len = vfs_get_path(dnode->parent, buf, size, depth + 1);
685
686     if (len >= size) {
687         return len;
688     }
689
690     size_t cpy_size = MIN(dnode->name.len, size - len);
691     strncpy(buf + len, dnode->name.value, cpy_size);
692     len += cpy_size;
693
694     if (len < size) {
695         buf[len++] = PATH_DELIM;
696     }
697
698     return len;
699 }
700
701 int
702 vfs_readlink(struct v_dnode* dnode, char* buf, size_t size)
703 {
704     char* link;
705     if (dnode->inode->ops.read_symlink) {
706         int errno = dnode->inode->ops.read_symlink(dnode->inode, &link);
707         strncpy(buf, link, size);
708         return errno;
709     }
710     return 0;
711 }
712
713 __DEFINE_LXSYSCALL3(int, realpathat, int, fd, char*, buf, size_t, size)
714 {
715     int errno;
716     struct v_fd* fd_s;
717     if (!GET_FD(fd, fd_s)) {
718         errno = EBADF;
719     } else {
720         struct v_dnode* dnode;
721         errno = vfs_get_path(fd_s->file->dnode, buf, size, 0);
722     }
723
724     if (errno >= 0) {
725         return errno;
726     }
727
728     return DO_STATUS(errno);
729 }
730
731 __DEFINE_LXSYSCALL3(int, readlink, const char*, path, char*, buf, size_t, size)
732 {
733     int errno;
734     struct v_dnode* dnode;
735     if (!(errno = vfs_walk(NULL, path, &dnode, NULL, VFS_WALK_NOFOLLOW))) {
736         errno = vfs_readlink(dnode, buf, size);
737     }
738
739     if (errno >= 0) {
740         return errno;
741     }
742
743     return DO_STATUS(errno);
744 }
745
746 __DEFINE_LXSYSCALL4(int,
747                     readlinkat,
748                     int,
749                     dirfd,
750                     const char*,
751                     pathname,
752                     char*,
753                     buf,
754                     size_t,
755                     size)
756 {
757     int errno;
758     struct v_fd* fd_s;
759     if (!GET_FD(dirfd, fd_s)) {
760         errno = EBADF;
761     } else {
762         struct v_dnode* dnode;
763         if (!(errno = vfs_walk(fd_s->file->dnode,
764                                pathname,
765                                &dnode,
766                                NULL,
767                                VFS_WALK_NOFOLLOW))) {
768             errno = vfs_readlink(fd_s->file->dnode, buf, size);
769         }
770     }
771
772     if (errno >= 0) {
773         return errno;
774     }
775
776     return DO_STATUS(errno);
777 }
778
779 __DEFINE_LXSYSCALL1(int, rmdir, const char*, pathname)
780 {
781     int errno;
782     struct v_dnode* dnode;
783     if ((errno = vfs_walk(NULL, pathname, &dnode, NULL, 0))) {
784         goto done;
785     }
786     if ((dnode->super_block->fs->types & FSTYPE_ROFS)) {
787         errno = EROFS;
788         goto done;
789     }
790
791     if (dnode->inode->open_count) {
792         errno = EBUSY;
793         goto done;
794     }
795
796     if ((dnode->inode->itype & VFS_INODE_TYPE_DIR)) {
797         errno = dnode->inode->ops.rmdir(dnode->inode);
798     } else {
799         errno = ENOTDIR;
800     }
801
802 done:
803     return DO_STATUS(errno);
804 }
805
806 int
807 __vfs_do_unlink(struct v_inode* inode)
808 {
809     int errno;
810     if (inode->open_count) {
811         errno = EBUSY;
812     } else if (!(inode->itype & VFS_INODE_TYPE_DIR)) {
813         // TODO handle symbolic link and type other than regular file
814         errno = inode->ops.unlink(inode);
815         if (!errno) {
816             inode->link_count--;
817         }
818     } else {
819         errno = EISDIR;
820     }
821
822     return errno;
823 }
824
825 __DEFINE_LXSYSCALL1(int, unlink, 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     errno = __vfs_do_unlink(dnode->inode);
838
839 done:
840     return DO_STATUS(errno);
841 }
842
843 __DEFINE_LXSYSCALL2(int, unlinkat, int, fd, const char*, pathname)
844 {
845     int errno;
846     struct v_fd* fd_s;
847     if (!GET_FD(fd, fd_s)) {
848         errno = EBADF;
849     } else {
850         struct v_dnode* dnode;
851         if (!(errno = vfs_walk(fd_s->file->dnode, pathname, &dnode, NULL, 0))) {
852             errno = __vfs_do_unlink(dnode->inode);
853         }
854     }
855
856 done:
857     return DO_STATUS(errno);
858 }
859
860 __DEFINE_LXSYSCALL2(int, link, const char*, oldpath, const char*, newpath)
861 {
862     int errno;
863     struct v_dnode *dentry, *to_link, *name_dentry, *name_file;
864
865     errno = __vfs_try_locate_file(oldpath, &dentry, &to_link, 0);
866     if (!errno) {
867         errno = __vfs_try_locate_file(
868           newpath, &name_dentry, &name_file, FLOCATE_CREATE_EMPTY);
869         if (!errno) {
870             errno = EEXIST;
871         } else if (name_file) {
872             errno = vfs_link(to_link, name_file);
873         }
874     }
875     return DO_STATUS(errno);
876 }
877
878 __DEFINE_LXSYSCALL1(int, fsync, int, fildes)
879 {
880     int errno;
881     struct v_fd* fd_s;
882     if (!GET_FD(fildes, fd_s)) {
883         errno = EBADF;
884     } else {
885         errno = vfs_fsync(fd_s->file);
886     }
887
888     return DO_STATUS(errno);
889 }
890
891 int
892 __vfs_dup_fd(struct v_fd* old, struct v_fd** new)
893 {
894     int errno = 0;
895     struct v_file* newopened;
896     if (!(errno = vfs_open(old->file->dnode, &newopened))) {
897         *new = cake_grab(fd_pile);
898         **new = (struct v_fd){ .file = newopened,
899                                .pos = old->pos,
900                                .flags = old->flags };
901     }
902
903     return errno;
904 }
905
906 __DEFINE_LXSYSCALL2(int, dup2, int, oldfd, int, newfd)
907 {
908     if (newfd == oldfd) {
909         return newfd;
910     }
911
912     int errno;
913     struct v_fd *oldfd_s, *newfd_s;
914     if (!GET_FD(oldfd, oldfd_s) || TEST_FD(newfd)) {
915         errno = EBADF;
916         goto done;
917     }
918     newfd_s = __current->fdtable->fds[newfd];
919     if (newfd_s && (errno = vfs_close(newfd_s))) {
920         goto done;
921     }
922
923     if (!(errno = __vfs_dup_fd(oldfd_s, &newfd_s))) {
924         __current->fdtable->fds[newfd] = newfd_s;
925         return newfd;
926     }
927
928 done:
929     return DO_STATUS(errno);
930 }
931
932 __DEFINE_LXSYSCALL1(int, dup, int, oldfd)
933 {
934     int errno, newfd;
935     struct v_fd *oldfd_s, *newfd_s;
936     if (!GET_FD(oldfd, oldfd_s)) {
937         errno = EBADF;
938     } else if (!(errno = vfs_alloc_fdslot(&newfd)) &&
939                !(errno = __vfs_dup_fd(oldfd_s, &newfd_s))) {
940         __current->fdtable->fds[newfd] = newfd_s;
941         return newfd;
942     }
943
944     return DO_STATUS(errno);
945 }
946
947 __DEFINE_LXSYSCALL2(int,
948                     symlink,
949                     const char*,
950                     pathname,
951                     const char*,
952                     link_target)
953 {
954     int errno;
955     struct v_dnode* dnode;
956     if ((errno = vfs_walk(NULL, pathname, &dnode, NULL, 0))) {
957         goto done;
958     }
959     if ((dnode->super_block->fs->types & FSTYPE_ROFS)) {
960         errno = EROFS;
961         goto done;
962     }
963     if (!dnode->inode->ops.symlink) {
964         errno = ENOTSUP;
965         goto done;
966     }
967
968     errno = dnode->inode->ops.symlink(dnode->inode, link_target);
969
970 done:
971     return DO_STATUS(errno);
972 }