feat: implement dup(2), dup2(2)
[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 int
222 vfs_mount(const char* target, const char* fs_name, bdev_t device)
223 {
224     int errno;
225     struct v_dnode* mnt;
226
227     if (!(errno = vfs_walk(NULL, target, &mnt, NULL, 0))) {
228         errno = vfs_mount_at(fs_name, device, mnt);
229     }
230
231     return errno;
232 }
233
234 int
235 vfs_unmount(const char* target)
236 {
237     int errno;
238     struct v_dnode* mnt;
239
240     if (!(errno = vfs_walk(NULL, target, &mnt, NULL, 0))) {
241         errno = vfs_unmount_at(mnt);
242     }
243
244     return errno;
245 }
246
247 int
248 vfs_mount_at(const char* fs_name, bdev_t device, struct v_dnode* mnt_point)
249 {
250     struct filesystem* fs = fsm_get(fs_name);
251     if (!fs)
252         return VFS_ENOFS;
253     struct v_superblock* sb = vfs_sb_alloc();
254     sb->dev = device;
255     sb->fs_id = fs_id++;
256
257     int errno = 0;
258     if (!(errno = fs->mount(sb, mnt_point))) {
259         sb->fs = fs;
260         sb->root = mnt_point;
261         mnt_point->super_block = sb;
262         llist_append(&root_sb->sb_list, &sb->sb_list);
263     }
264
265     return errno;
266 }
267
268 int
269 vfs_unmount_at(struct v_dnode* mnt_point)
270 {
271     int errno = 0;
272     struct v_superblock* sb = mnt_point->super_block;
273     if (!sb) {
274         return VFS_EBADMNT;
275     }
276     if (!(errno = sb->fs->unmount(sb))) {
277         struct v_dnode* fs_root = sb->root;
278         llist_delete(&fs_root->siblings);
279         llist_delete(&sb->sb_list);
280         vfs_sb_free(sb);
281     }
282     return errno;
283 }
284
285 int
286 vfs_open(struct v_dnode* dnode, struct v_file** file)
287 {
288     if (!dnode->inode || !dnode->inode->ops.open) {
289         return ENOTSUP;
290     }
291
292     struct v_file* vfile = cake_grab(file_pile);
293     memset(vfile, 0, sizeof(*vfile));
294
295     vfile->dnode = dnode;
296     vfile->inode = dnode->inode;
297     dnode->inode->open_count++;
298
299     int errno = dnode->inode->ops.open(dnode->inode, vfile);
300     if (errno) {
301         cake_release(file_pile, vfile);
302     } else {
303         *file = vfile;
304     }
305     return errno;
306 }
307
308 int
309 vfs_link(struct v_dnode* to_link, struct v_dnode* name)
310 {
311     int errno;
312     if (to_link->super_block->root != name->super_block->root) {
313         errno = EXDEV;
314     } else if (!to_link->inode->ops.link) {
315         errno = ENOTSUP;
316     } else if (!(errno = to_link->inode->ops.link(to_link->inode, name))) {
317         name->inode = to_link->inode;
318         to_link->inode->link_count++;
319     }
320
321     return errno;
322 }
323
324 int
325 vfs_close(struct v_file* file)
326 {
327     if (!file->ops.close) {
328         return ENOTSUP;
329     }
330
331     int errno = file->ops.close(file);
332     if (!errno) {
333         if (file->inode->open_count) {
334             file->inode->open_count--;
335         }
336         cake_release(file_pile, file);
337     }
338     return errno;
339 }
340
341 int
342 vfs_fsync(struct v_file* file)
343 {
344     int errno = ENOTSUP;
345     if (file->ops.sync) {
346         errno = file->ops.sync(file);
347     }
348     if (!errno && file->inode->ops.sync) {
349         return file->inode->ops.sync(file->inode);
350     }
351     return errno;
352 }
353
354 int
355 vfs_alloc_fdslot(int* fd)
356 {
357     for (size_t i = 0; i < VFS_MAX_FD; i++) {
358         if (!__current->fdtable->fds[i]) {
359             *fd = i;
360             return 0;
361         }
362     }
363     return EMFILE;
364 }
365
366 struct v_superblock*
367 vfs_sb_alloc()
368 {
369     struct v_superblock* sb = cake_grab(superblock_pile);
370     memset(sb, 0, sizeof(*sb));
371     llist_init_head(&sb->sb_list);
372     return sb;
373 }
374
375 void
376 vfs_sb_free(struct v_superblock* sb)
377 {
378     cake_release(superblock_pile, sb);
379 }
380
381 struct v_dnode*
382 vfs_d_alloc()
383 {
384     struct v_dnode* dnode = cake_grab(dnode_pile);
385     memset(dnode, 0, sizeof(*dnode));
386     llist_init_head(&dnode->children);
387     dnode->name = vfs_empty;
388     return dnode;
389 }
390
391 void
392 vfs_d_free(struct v_dnode* dnode)
393 {
394     if (dnode->ops.destruct) {
395         dnode->ops.destruct(dnode);
396     }
397     cake_release(dnode_pile, dnode);
398 }
399
400 struct v_inode*
401 vfs_i_alloc()
402 {
403     struct v_inode* inode = cake_grab(inode_pile);
404     memset(inode, 0, sizeof(*inode));
405
406     return inode;
407 }
408
409 void
410 vfs_i_free(struct v_inode* inode)
411 {
412     cake_release(inode_pile, inode);
413 }
414
415 /* ---- System call definition and support ---- */
416
417 #define FLOCATE_CREATE_EMPTY 1
418
419 #define DO_STATUS(errno) SYSCALL_ESTATUS(__current->k_status = errno)
420
421 int
422 __vfs_try_locate_file(const char* path,
423                       struct v_dnode** fdir,
424                       struct v_dnode** file,
425                       int options)
426 {
427     char name_str[VFS_NAME_MAXLEN];
428     struct hstr name = HSTR(name_str, 0);
429     int errno;
430     if (!(errno = vfs_walk(NULL, path, fdir, &name, VFS_WALK_PARENT))) {
431         errno = vfs_walk(*fdir, name.value, file, NULL, 0);
432         if (errno == ENOENT && (options & FLOCATE_CREATE_EMPTY)) {
433             struct v_dnode* file_new;
434             file_new = vfs_d_alloc();
435             file_new->name =
436               HHSTR(valloc(VFS_NAME_MAXLEN), name.len, name.hash);
437             strcpy(file_new->name.value, name_str);
438             *file = file_new;
439
440             llist_append(&(*fdir)->children, &file_new->siblings);
441         }
442     }
443
444     return errno;
445 }
446
447 int
448 __vfs_do_open(struct v_file** file_out, const char* path, int options)
449 {
450     int errno;
451     struct v_dnode *dentry, *file;
452     struct v_file* opened_file = 0;
453
454     errno = __vfs_try_locate_file(path, &dentry, &file, 0);
455
456     if (errno != ENOENT && (options & FO_CREATE)) {
457         errno = dentry->inode->ops.create(dentry->inode, opened_file);
458     } else if (!errno) {
459         errno = vfs_open(file, &opened_file);
460     }
461
462     *file_out = opened_file;
463     return errno;
464 }
465
466 __DEFINE_LXSYSCALL2(int, open, const char*, path, int, options)
467 {
468     struct v_file* opened_file;
469     int errno = __vfs_do_open(&opened_file, path, options), fd;
470
471     if (!errno && !(errno = vfs_alloc_fdslot(&fd))) {
472         struct v_fd* fd_s = vzalloc(sizeof(*fd_s));
473         fd_s->file = opened_file;
474         fd_s->pos = opened_file->inode->fsize & -((options & FO_APPEND) != 0);
475         __current->fdtable->fds[fd] = fd_s;
476         return fd;
477     }
478
479     return DO_STATUS(errno);
480 }
481 #define TEST_FD(fd) (fd >= 0 && fd < VFS_MAX_FD)
482 #define GET_FD(fd, fd_s) (TEST_FD(fd) && (fd_s = __current->fdtable->fds[fd]))
483
484 __DEFINE_LXSYSCALL1(int, close, int, fd)
485 {
486     struct v_fd* fd_s;
487     int errno;
488     if (!GET_FD(fd, fd_s)) {
489         errno = EBADF;
490     } else if (!(errno = vfs_close(fd_s->file))) {
491         vfree(fd_s);
492         __current->fdtable->fds[fd] = 0;
493     }
494
495     return DO_STATUS(errno);
496 }
497
498 void
499 __vfs_readdir_callback(struct dir_context* dctx,
500                        const char* name,
501                        const int len,
502                        const int dtype)
503 {
504     struct dirent* dent = (struct dirent*)dctx->cb_data;
505     strncpy(dent->d_name, name, DIRENT_NAME_MAX_LEN);
506     dent->d_nlen = len;
507     dent->d_type = dtype;
508 }
509
510 __DEFINE_LXSYSCALL2(int, readdir, int, fd, struct dirent*, dent)
511 {
512     struct v_fd* fd_s;
513     int errno;
514     if (!GET_FD(fd, fd_s)) {
515         errno = EBADF;
516     } else if (!(fd_s->file->inode->itype & VFS_INODE_TYPE_DIR)) {
517         errno = ENOTDIR;
518     } else {
519         struct dir_context dctx =
520           (struct dir_context){ .cb_data = dent,
521                                 .index = dent->d_offset,
522                                 .read_complete_callback =
523                                   __vfs_readdir_callback };
524         if (dent->d_offset == 0) {
525             __vfs_readdir_callback(&dctx, vfs_dot.value, vfs_dot.len, 0);
526         } else if (dent->d_offset == 1) {
527             __vfs_readdir_callback(&dctx, vfs_ddot.value, vfs_ddot.len, 0);
528         } else {
529             dctx.index -= 2;
530             if ((errno = fd_s->file->ops.readdir(fd_s->file, &dctx))) {
531                 goto done;
532             }
533         }
534         errno = 0;
535         dent->d_offset++;
536     }
537
538 done:
539     return DO_STATUS(errno);
540 }
541
542 __DEFINE_LXSYSCALL1(int, mkdir, const char*, path)
543 {
544     struct v_dnode *parent, *dir;
545     struct hstr component = HSTR(valloc(VFS_NAME_MAXLEN), 0);
546     int errno = vfs_walk(NULL, path, &parent, &component, VFS_WALK_PARENT);
547     if (errno) {
548         goto done;
549     }
550
551     if ((parent->super_block->fs->types & FSTYPE_ROFS)) {
552         errno = ENOTSUP;
553     } else if (!parent->inode->ops.mkdir) {
554         errno = ENOTSUP;
555     } else if (!(parent->inode->itype & VFS_INODE_TYPE_DIR)) {
556         errno = ENOTDIR;
557     } else {
558         dir = vfs_d_alloc();
559         dir->name = component;
560         if (!(errno = parent->inode->ops.mkdir(parent->inode, dir))) {
561             llist_append(&parent->children, &dir->siblings);
562         } else {
563             vfs_d_free(dir);
564             vfree(component.value);
565         }
566     }
567
568 done:
569     return DO_STATUS(errno);
570 }
571
572 __DEFINE_LXSYSCALL3(int, read, int, fd, void*, buf, size_t, count)
573 {
574     int errno = 0;
575     struct v_fd* fd_s;
576     if (!GET_FD(fd, fd_s)) {
577         errno = EBADF;
578     } else {
579         struct v_file* file = fd_s->file;
580         file->f_pos = fd_s->pos;
581         if ((errno = file->ops.read(file, buf, count)) >= 0) {
582             fd_s->pos += errno;
583         }
584     }
585
586     return DO_STATUS(errno);
587 }
588
589 __DEFINE_LXSYSCALL3(int, write, int, fd, void*, buf, size_t, count)
590 {
591     int errno = 0;
592     struct v_fd* fd_s;
593     if (!GET_FD(fd, fd_s)) {
594         errno = EBADF;
595     } else {
596         struct v_file* file = fd_s->file;
597         file->f_pos = fd_s->pos;
598         if ((errno = file->ops.write(file, buf, count)) >= 0) {
599             fd_s->pos += errno;
600         }
601     }
602
603     return DO_STATUS(errno);
604 }
605
606 __DEFINE_LXSYSCALL3(int, lseek, int, fd, int, offset, int, options)
607 {
608     int errno = 0;
609     struct v_fd* fd_s;
610     if (!GET_FD(fd, fd_s)) {
611         errno = EBADF;
612     } else {
613         size_t fpos = fd_s->file->f_pos;
614         switch (options) {
615             case FSEEK_CUR:
616                 fpos = (size_t)((int)fd_s->file->f_pos + offset);
617                 break;
618             case FSEEK_END:
619                 fpos = (size_t)((int)fd_s->file->inode->fsize + offset);
620                 break;
621             case FSEEK_SET:
622                 fpos = offset;
623                 break;
624
625             default:
626                 break;
627         }
628         fd_s->pos = fpos;
629     }
630
631     return DO_STATUS(errno);
632 }
633
634 int
635 vfs_readlink(struct v_dnode* dnode, char* buf, size_t size, int depth)
636 {
637     if (!dnode) {
638         return 0;
639     }
640
641     if (depth > 64) {
642         return ELOOP;
643     }
644
645     size_t len = vfs_readlink(dnode->parent, buf, size, depth + 1);
646
647     if (len >= size) {
648         return len;
649     }
650
651     size_t cpy_size = MIN(dnode->name.len, size - len);
652     strncpy(buf + len, dnode->name.value, cpy_size);
653     len += cpy_size;
654
655     if (len < size) {
656         buf[len++] = PATH_DELIM;
657     }
658
659     return len;
660 }
661
662 __DEFINE_LXSYSCALL3(int, readlink, const char*, path, char*, buf, size_t, size)
663 {
664     int errno;
665     struct v_dnode* dnode;
666     if (!(errno = vfs_walk(NULL, path, &dnode, NULL, 0))) {
667         errno = vfs_readlink(dnode, buf, size, 0);
668     }
669
670     if (errno >= 0) {
671         return errno;
672     }
673
674     return DO_STATUS(errno);
675 }
676
677 __DEFINE_LXSYSCALL4(int,
678                     readlinkat,
679                     int,
680                     dirfd,
681                     const char*,
682                     pathname,
683                     char*,
684                     buf,
685                     size_t,
686                     size)
687 {
688     int errno;
689     struct v_fd* fd_s;
690     if (!GET_FD(dirfd, fd_s)) {
691         errno = EBADF;
692     } else {
693         struct v_dnode* dnode;
694         if (!(errno = vfs_walk(fd_s->file->dnode, pathname, &dnode, NULL, 0))) {
695             errno = vfs_readlink(fd_s->file->dnode, buf, size, 0);
696         }
697     }
698
699     if (errno >= 0) {
700         return errno;
701     }
702
703     return DO_STATUS(errno);
704 }
705
706 __DEFINE_LXSYSCALL1(int, rmdir, const char*, pathname)
707 {
708     int errno;
709     struct v_dnode* dnode;
710     if ((errno = vfs_walk(NULL, pathname, &dnode, NULL, 0))) {
711         goto done;
712     }
713     if ((dnode->super_block->fs->types & FSTYPE_ROFS)) {
714         errno = EROFS;
715         goto done;
716     }
717
718     if (dnode->inode->open_count) {
719         errno = EBUSY;
720         goto done;
721     }
722
723     if ((dnode->inode->itype & VFS_INODE_TYPE_DIR)) {
724         errno = dnode->inode->ops.rmdir(dnode->inode);
725     } else {
726         errno = ENOTDIR;
727     }
728
729 done:
730     return DO_STATUS(errno);
731 }
732
733 int
734 __vfs_do_unlink(struct v_inode* inode)
735 {
736     int errno;
737     if (inode->open_count) {
738         errno = EBUSY;
739     } else if (!(inode->itype & VFS_INODE_TYPE_DIR)) {
740         // TODO handle symbolic link and type other than regular file
741         errno = inode->ops.unlink(inode);
742         if (!errno) {
743             inode->link_count--;
744         }
745     } else {
746         errno = EISDIR;
747     }
748
749     return errno;
750 }
751
752 __DEFINE_LXSYSCALL1(int, unlink, const char*, pathname)
753 {
754     int errno;
755     struct v_dnode* dnode;
756     if ((errno = vfs_walk(NULL, pathname, &dnode, NULL, 0))) {
757         goto done;
758     }
759     if ((dnode->super_block->fs->types & FSTYPE_ROFS)) {
760         errno = EROFS;
761         goto done;
762     }
763
764     errno = __vfs_do_unlink(dnode->inode);
765
766 done:
767     return DO_STATUS(errno);
768 }
769
770 __DEFINE_LXSYSCALL2(int, unlinkat, int, fd, const char*, pathname)
771 {
772     int errno;
773     struct v_fd* fd_s;
774     if (!GET_FD(fd, fd_s)) {
775         errno = EBADF;
776     } else {
777         struct v_dnode* dnode;
778         if (!(errno = vfs_walk(fd_s->file->dnode, pathname, &dnode, NULL, 0))) {
779             errno = __vfs_do_unlink(dnode->inode);
780         }
781     }
782
783 done:
784     return DO_STATUS(errno);
785 }
786
787 __DEFINE_LXSYSCALL2(int, link, const char*, oldpath, const char*, newpath)
788 {
789     int errno;
790     struct v_dnode *dentry, *to_link, *name_dentry, *name_file;
791
792     errno = __vfs_try_locate_file(oldpath, &dentry, &to_link, 0);
793     if (!errno) {
794         errno = __vfs_try_locate_file(
795           newpath, &name_dentry, &name_file, FLOCATE_CREATE_EMPTY);
796         if (!errno) {
797             errno = EEXIST;
798         } else if (name_file) {
799             errno = vfs_link(to_link, name_file);
800         }
801     }
802     return DO_STATUS(errno);
803 }
804
805 __DEFINE_LXSYSCALL1(int, fsync, int, fildes)
806 {
807     int errno;
808     struct v_fd* fd_s;
809     if (!GET_FD(fildes, fd_s)) {
810         errno = EBADF;
811     } else {
812         errno = vfs_fsync(fd_s->file);
813     }
814
815     return DO_STATUS(errno);
816 }
817
818 int
819 __vfs_dup_fd(struct v_fd* old, struct v_fd** new)
820 {
821     int errno = 0;
822     struct v_file* newopened;
823     if (!(errno = vfs_open(old->file->dnode, &newopened))) {
824         *new = cake_grab(fd_pile);
825         **new = (struct v_fd){ .file = newopened,
826                                .pos = old->pos,
827                                .flags = old->flags };
828     }
829
830     return errno;
831 }
832
833 __DEFINE_LXSYSCALL2(int, dup2, int, oldfd, int, newfd)
834 {
835     if (newfd == oldfd) {
836         return newfd;
837     }
838
839     int errno;
840     struct v_fd *oldfd_s, *newfd_s;
841     if (!GET_FD(oldfd, oldfd_s) || TEST_FD(newfd)) {
842         errno = EBADF;
843         goto done;
844     }
845     newfd_s = __current->fdtable->fds[newfd];
846     if (newfd_s && (errno = vfs_close(newfd_s))) {
847         goto done;
848     }
849
850     if (!(errno = __vfs_dup_fd(oldfd_s, &newfd_s))) {
851         __current->fdtable->fds[newfd] = newfd_s;
852         return newfd;
853     }
854
855 done:
856     return DO_STATUS(errno);
857 }
858
859 __DEFINE_LXSYSCALL1(int, dup, int, oldfd)
860 {
861     int errno, newfd;
862     struct v_fd *oldfd_s, *newfd_s;
863     if (!GET_FD(oldfd, oldfd_s)) {
864         errno = EBADF;
865     } else if (!(errno = vfs_alloc_fdslot(&newfd)) &&
866                !(errno = __vfs_dup_fd(oldfd_s, &newfd_s))) {
867         __current->fdtable->fds[newfd] = newfd_s;
868         return newfd;
869     }
870
871     return DO_STATUS(errno);
872 }