]> xenbits.xensource.com Git - unikraft/unikraft.git/commitdiff
vfscore: Move fd handling into posix-fdtab
authorAndrei Tatar <andrei@unikraft.io>
Tue, 21 Nov 2023 20:11:46 +0000 (21:11 +0100)
committerSimon Kuenzer <simon@unikraft.io>
Wed, 29 Nov 2023 13:23:21 +0000 (14:23 +0100)
This change moves the file descriptor table out of vfscore, along with
the fd-specific syscalls close and dup*.
Implementations of vfscore files are also patched to use posix-fdtab.

Checkpatch-Ignore: USE_NEGATIVE_ERRNO
Checkpatch-Ignore: LINE_SPACING
Signed-off-by: Andrei Tatar <andrei@unikraft.io>
Reviewed-by: Simon Kuenzer <simon@unikraft.io>
Approved-by: Simon Kuenzer <simon@unikraft.io>
GitHub-Closes: #1168

13 files changed:
lib/posix-event/epoll.c
lib/posix-event/eventfd.c
lib/posix-fdtab/Makefile.uk
lib/posix-fdtab/fdtab.c
lib/posix-socket/socket_vnops.c
lib/vfscore/Config.uk
lib/vfscore/Makefile.uk
lib/vfscore/exportsyms.uk
lib/vfscore/fd.c
lib/vfscore/include/vfscore/file.h
lib/vfscore/main.c
lib/vfscore/pipe.c
lib/vfscore/stdio.c

index ce30175e6585d6c6115d690e633111160620fabc..891b317aa6634bf4cb7d2db0e1cc0f8f5fcd5cf5 100644 (file)
@@ -43,6 +43,8 @@
 #include <uk/config.h>
 #include <uk/init.h>
 
+#include <uk/posix-fdtab-legacy.h>
+
 #include <inttypes.h>
 #include <stdlib.h>
 #include <errno.h>
@@ -116,13 +118,6 @@ static int do_epoll_create(struct uk_alloc *a, int flags)
        if (unlikely(flags & ~EPOLL_CLOEXEC))
                return -EINVAL;
 
-       /* Reserve a file descriptor number */
-       vfs_fd = vfscore_alloc_fd();
-       if (unlikely(vfs_fd < 0)) {
-               ret = -ENFILE;
-               goto ERR_EXIT;
-       }
-
        /* Allocate file, vfs_file, and vnode */
        ep = uk_malloc(a, sizeof(struct eventpoll));
        if (unlikely(!ep)) {
@@ -155,7 +150,6 @@ static int do_epoll_create(struct uk_alloc *a, int flags)
        }
 
        /* Initialize data structures */
-       vfs_file->fd = vfs_fd;
        vfs_file->f_flags = 0;
        vfs_file->f_count = 1;
        vfs_file->f_data = ep;
@@ -171,9 +165,11 @@ static int do_epoll_create(struct uk_alloc *a, int flags)
        eventpoll_init(ep, a);
 
        /* Store within the vfs structure */
-       ret = vfscore_install_fd(vfs_fd, vfs_file);
-       if (unlikely(ret))
+       vfs_fd = uk_fdtab_legacy_open(vfs_file);
+       if (unlikely(vfs_fd < 0)) {
+               ret = vfs_fd;
                goto ERR_VFS_INSTALL;
+       }
 
        /* Only the dentry should hold a reference; release ours */
        vput(vfs_vnode);
@@ -189,8 +185,6 @@ ERR_ALLOC_VNODE:
 ERR_MALLOC_VFS_FILE:
        uk_free(a, ep);
 ERR_MALLOC_FILE:
-       vfscore_put_fd(vfs_fd);
-ERR_EXIT:
        UK_ASSERT(ret < 0);
        return ret;
 }
@@ -219,13 +213,13 @@ static int do_epoll_ctl(int epfd, int op, int fd, struct epoll_event *event)
        struct eventpoll *ep;
        int ret;
 
-       epf = vfscore_get_file(epfd);
+       epf = uk_fdtab_legacy_get(epfd);
        if (unlikely(!epf)) {
                ret = -EBADF;
                goto EXIT;
        }
 
-       fp = vfscore_get_file(fd);
+       fp = uk_fdtab_legacy_get(fd);
        if (unlikely(!fp)) {
                ret = -EBADF;
                goto ERR_PUT_EPF;
@@ -290,7 +284,7 @@ static int do_epoll_pwait(int epfd, struct epoll_event *events, int maxevents,
        struct eventpoll *ep;
        int ret;
 
-       epf = vfscore_get_file(epfd);
+       epf = uk_fdtab_legacy_get(epfd);
        if (unlikely(!epf)) {
                ret = -EBADF;
                goto EXIT;
index f260519987179807da10f779a434defc8af46e24..4e399e651b1dda5480528e0352d0cc6020769c38 100644 (file)
@@ -47,6 +47,8 @@
 #include <uk/config.h>
 #include <uk/init.h>
 
+#include <uk/posix-fdtab-legacy.h>
+
 #include <sys/eventfd.h>
 #include <sys/ioctl.h>
 #include <inttypes.h>
@@ -350,13 +352,6 @@ static int do_eventfd(struct uk_alloc *a, unsigned int initval, int flags)
        if (unlikely(flags & ~(EFD_CLOEXEC | EFD_SEMAPHORE | EFD_NONBLOCK)))
                return -EINVAL;
 
-       /* Reserve a file descriptor number */
-       vfs_fd = vfscore_alloc_fd();
-       if (unlikely(vfs_fd < 0)) {
-               ret = -ENFILE;
-               goto ERR_EXIT;
-       }
-
        /* Allocate file, vfs_file, and vnode */
        efd = uk_malloc(a, sizeof(struct eventfd));
        if (unlikely(!efd)) {
@@ -388,7 +383,6 @@ static int do_eventfd(struct uk_alloc *a, unsigned int initval, int flags)
        }
 
        /* Initialize data structures */
-       vfs_file->fd = vfs_fd;
        vfs_file->f_flags = UK_FREAD | UK_FWRITE;
        vfs_file->f_count = 1;
        vfs_file->f_data = efd;
@@ -405,9 +399,11 @@ static int do_eventfd(struct uk_alloc *a, unsigned int initval, int flags)
        eventfd_init(efd, initval, flags);
 
        /* Store within the vfs structure */
-       ret = vfscore_install_fd(vfs_fd, vfs_file);
-       if (unlikely(ret))
+       vfs_fd = uk_fdtab_legacy_open(vfs_file);
+       if (unlikely(vfs_fd < 0)) {
+               ret = vfs_fd;
                goto ERR_VFS_INSTALL;
+       }
 
        /* Only the dentry should hold a reference; release ours */
        vput(vfs_vnode);
@@ -431,8 +427,6 @@ ERR_ALLOC_VNODE:
 ERR_MALLOC_VFS_FILE:
        uk_free(a, efd);
 ERR_MALLOC_FILE:
-       vfscore_put_fd(vfs_fd);
-ERR_EXIT:
        UK_ASSERT(ret < 0);
        return ret;
 }
index 80ef19bc9d6ba3a70ff740aec3d51aa64031b6e4..d3df9db3fe02004fd2232e1b38becaf17edb4a1a 100644 (file)
@@ -4,3 +4,8 @@ CINCLUDES-$(CONFIG_LIBPOSIX_FDTAB) += -I$(LIBPOSIX_FDTAB_BASE)/include
 CXXINCLUDES-$(CONFIG_LIBPOSIX_FDTAB) += -I$(LIBPOSIX_FDTAB_BASE)/include
 
 LIBPOSIX_FDTAB_SRCS-y += $(LIBPOSIX_FDTAB_BASE)/fdtab.c
+
+UK_PROVIDED_SYSCALLS-$(CONFIG_LIBPOSIX_FDTAB) += close-1
+UK_PROVIDED_SYSCALLS-$(CONFIG_LIBPOSIX_FDTAB) += dup-1
+UK_PROVIDED_SYSCALLS-$(CONFIG_LIBPOSIX_FDTAB) += dup3-3
+UK_PROVIDED_SYSCALLS-$(CONFIG_LIBPOSIX_FDTAB) += dup2-2
index 27a3f3fd24842fb126a05d6aa3ef5d4565e4ec62..2510556edc1a5855e0c93405ad0aee799d2fe051 100644 (file)
@@ -11,6 +11,7 @@
 #include <uk/assert.h>
 #include <uk/config.h>
 #include <uk/init.h>
+#include <uk/syscall.h>
 
 #include <uk/posix-fdtab.h>
 
@@ -457,3 +458,25 @@ int uk_sys_dup(int oldfd)
 {
        return uk_sys_dup_min(oldfd, 0, 0);
 }
+
+/* Userspace Syscalls */
+
+UK_SYSCALL_R_DEFINE(int, close, int, fd)
+{
+       return uk_sys_close(fd);
+}
+
+UK_SYSCALL_R_DEFINE(int, dup, int, oldfd)
+{
+       return uk_sys_dup(oldfd);
+}
+
+UK_SYSCALL_R_DEFINE(int, dup2, int, oldfd, int, newfd)
+{
+       return uk_sys_dup2(oldfd, newfd);
+}
+
+UK_SYSCALL_R_DEFINE(int, dup3, int, oldfd, int, newfd, int, flags)
+{
+       return uk_sys_dup3(oldfd, newfd, flags);
+}
index 15c43b2a3e019ba9351937327874477f6d096fc2..b0e713203e09caba682c208f14b95488786adef0 100644 (file)
@@ -49,6 +49,8 @@
 #include <string.h>
 #include <stdlib.h>
 
+#include <uk/posix-fdtab-legacy.h>
+
 static struct mount posix_socket_mount;
 
 static uint64_t s_inode;
@@ -58,7 +60,7 @@ posix_socket_file_get(int sock_fd)
 {
        struct vfscore_file *fos;
 
-       fos = vfscore_get_file(sock_fd);
+       fos = uk_fdtab_legacy_get(sock_fd);
        if (unlikely(!fos))
                return ERR2PTR(-ENOENT);
 
@@ -82,13 +84,6 @@ posix_socket_alloc_fd(struct posix_socket_driver *d, int type, void *sock_data)
        struct dentry *vfs_dentry;
        struct vnode *vfs_vnode;
 
-       /* Reserve file descriptor number */
-       vfs_fd = vfscore_alloc_fd();
-       if (unlikely(vfs_fd < 0)) {
-               ret = -ENFILE;
-               goto ERR_EXIT;
-       }
-
        /* Allocate file, vfs_file, and vnode */
        sock = uk_calloc(d->allocator, 1, sizeof(*sock));
        if (unlikely(!sock)) {
@@ -118,7 +113,6 @@ posix_socket_alloc_fd(struct posix_socket_driver *d, int type, void *sock_data)
        }
 
        /* Put things together, and fill out necessary fields */
-       vfs_file->fd = vfs_fd;
        vfs_file->f_flags = UK_FWRITE | UK_FREAD;
        vfs_file->f_count = 1;
        vfs_file->f_data = sock;
@@ -137,9 +131,11 @@ posix_socket_alloc_fd(struct posix_socket_driver *d, int type, void *sock_data)
        sock->type = type;
 
        /* Store within the vfs structure */
-       ret = vfscore_install_fd(vfs_fd, vfs_file);
-       if (unlikely(ret))
+       vfs_fd = uk_fdtab_legacy_open(vfs_file);
+       if (unlikely(vfs_fd < 0)) {
+               ret = vfs_fd;
                goto ERR_VFS_INSTALL;
+       }
 
        /* Only the dentry should hold a reference; release ours */
        vput(vfs_vnode);
@@ -159,8 +155,6 @@ ERR_ALLOC_VNODE:
 ERR_MALLOC_VFS_FILE:
        uk_free(d->allocator, sock);
 ERR_MALLOC_FILE:
-       vfscore_put_fd(vfs_fd);
-ERR_EXIT:
        UK_ASSERT(ret < 0);
        return ret;
 }
index 659a66ed82134e79561e18dbe44e7a1159849c7b..b703bda6c37f1843d406663cf9f6242c67b4e283 100644 (file)
@@ -5,6 +5,8 @@ menuconfig LIBVFSCORE
        select LIBUKDEBUG
        select LIBUKLOCK
        select LIBPOSIX_TIME
+       select LIBPOSIX_FDTAB
+       select LIBPOSIX_FDTAB_LEGACY_SHIM
 
 if LIBVFSCORE
 config LIBVFSCORE_PIPE_SIZE_ORDER
index 6c9fd54e8ddf2abbf055a86b5b0caa2cf8b62f7e..0d1b2be277d384e9cd1eec7e363942d3a2602a3b 100644 (file)
@@ -31,7 +31,6 @@ UK_PROVIDED_SYSCALLS-$(CONFIG_LIBVFSCORE) += read-3 readv-3 pread64-4
 UK_PROVIDED_SYSCALLS-$(CONFIG_LIBVFSCORE) += fstat-2
 UK_PROVIDED_SYSCALLS-$(CONFIG_LIBVFSCORE) += fcntl-3
 UK_PROVIDED_SYSCALLS-$(CONFIG_LIBVFSCORE) += readlink-3
-UK_PROVIDED_SYSCALLS-$(CONFIG_LIBVFSCORE) += close-1
 UK_PROVIDED_SYSCALLS-$(CONFIG_LIBVFSCORE) += lseek-3
 UK_PROVIDED_SYSCALLS-$(CONFIG_LIBVFSCORE) += link-2
 UK_PROVIDED_SYSCALLS-$(CONFIG_LIBVFSCORE) += ftruncate-2
@@ -43,9 +42,6 @@ UK_PROVIDED_SYSCALLS-$(CONFIG_LIBVFSCORE) += chdir-1
 UK_PROVIDED_SYSCALLS-$(CONFIG_LIBVFSCORE) += fchdir-1
 UK_PROVIDED_SYSCALLS-$(CONFIG_LIBVFSCORE) += chmod-2
 UK_PROVIDED_SYSCALLS-$(CONFIG_LIBVFSCORE) += fchmod-2
-UK_PROVIDED_SYSCALLS-$(CONFIG_LIBVFSCORE) += dup-1
-UK_PROVIDED_SYSCALLS-$(CONFIG_LIBVFSCORE) += dup3-3
-UK_PROVIDED_SYSCALLS-$(CONFIG_LIBVFSCORE) += dup2-2
 UK_PROVIDED_SYSCALLS-$(CONFIG_LIBVFSCORE) += pwritev-4
 UK_PROVIDED_SYSCALLS-$(CONFIG_LIBVFSCORE) += utime-2
 UK_PROVIDED_SYSCALLS-$(CONFIG_LIBVFSCORE) += utimes-2
index 5d0f59763410b7ff0e4f864ada632daf09d6fe18..d25c6cc4828a14a5bad899a857906e6b3eab5b8f 100644 (file)
@@ -26,8 +26,6 @@ write
 uk_syscall_e_write
 uk_syscall_r_write
 close
-uk_syscall_e_close
-uk_syscall_r_close
 read
 uk_syscall_e_read
 uk_syscall_r_read
index d4a4ddd38d3766863059a3041eb7f4187a3da007..a83bd3fda16374a0bfc92c9107e3ff43deeefbdd 100644 (file)
 #include <uk/process.h>
 #endif /* CONFIG_LIBPOSIX_PROCESS_CLONE */
 
-int init_stdio(void);
-
-struct fdtable {
-       unsigned long bitmap[UK_BITS_TO_LONGS(FDTABLE_MAX_FILES)];
-       uint32_t fd_start;
-       struct vfscore_file *files[FDTABLE_MAX_FILES];
-};
-struct fdtable fdtable;
-
-int vfscore_alloc_fd(void)
-{
-       unsigned long flags;
-       int ret;
-
-       flags = ukplat_lcpu_save_irqf();
-       ret = uk_find_next_zero_bit(fdtable.bitmap, FDTABLE_MAX_FILES, 0);
-
-       if (ret == FDTABLE_MAX_FILES) {
-               ret = -ENFILE;
-               goto exit;
-       }
-
-       uk_bitmap_set(fdtable.bitmap, ret, 1);
-
-exit:
-       ukplat_lcpu_restore_irqf(flags);
-       return ret;
-}
-
-int vfscore_reserve_fd(int fd)
-{
-       unsigned long flags;
-       int ret = 0;
-
-       flags = ukplat_lcpu_save_irqf();
-       if (uk_test_bit(fd, fdtable.bitmap)) {
-               ret = -EBUSY;
-               goto exit;
-       }
-
-       uk_bitmap_set(fdtable.bitmap, fd, 1);
-
-exit:
-       ukplat_lcpu_restore_irqf(flags);
-       return ret;
-}
-
-int vfscore_put_fd(int fd)
-{
-       struct vfscore_file *fp;
-       unsigned long flags;
-
-       UK_ASSERT(fd < (int) FDTABLE_MAX_FILES);
-
-       /* FIXME Currently it is not allowed to free std(in|out|err):
-        * if (fd <= 2) return -EBUSY;
-        *
-        * However, returning -EBUSY in this case breaks dup2 with stdin, out,
-        * err. Ignoring this should be fine as long as those are not fdrop-ed
-        * twice, in which case the static fp would be freed, and here be
-        * dragons.
-        */
-
-       flags = ukplat_lcpu_save_irqf();
-       uk_bitmap_clear(fdtable.bitmap, fd, 1);
-       fp = fdtable.files[fd];
-       fdtable.files[fd] = NULL;
-       ukplat_lcpu_restore_irqf(flags);
-
-       /*
-        * Since we can alloc a fd without assigning a
-        * vfsfile we must protect against NULL ptr
-        */
-       if (fp)
-               fdrop(fp);
-
-       return 0;
-}
-
-int vfscore_install_fd(int fd, struct vfscore_file *file)
-{
-       unsigned long flags;
-       struct vfscore_file *orig;
-
-       if ((fd >= (int) FDTABLE_MAX_FILES) || (!file))
-               return -EBADF;
-
-       fhold(file);
-
-       file->fd = fd;
+#include <uk/posix-fdtab-legacy.h>
 
-       flags = ukplat_lcpu_save_irqf();
-       orig = fdtable.files[fd];
-       fdtable.files[fd] = file;
-       ukplat_lcpu_restore_irqf(flags);
-
-       fdrop(file);
-
-       if (orig)
-               fdrop(orig);
+int init_stdio(void);
 
-       return 0;
-}
 
 struct vfscore_file *vfscore_get_file(int fd)
 {
-       unsigned long flags;
-       struct vfscore_file *ret = NULL;
-
-       UK_ASSERT(fd < (int) FDTABLE_MAX_FILES);
-
-       flags = ukplat_lcpu_save_irqf();
-       if (!uk_test_bit(fd, fdtable.bitmap))
-               goto exit;
-       ret = fdtable.files[fd];
-       fhold(ret);
-
-exit:
-       ukplat_lcpu_restore_irqf(flags);
-       return ret;
+       return uk_fdtab_legacy_get(fd);
 }
 
 void vfscore_put_file(struct vfscore_file *file)
@@ -173,48 +61,29 @@ void vfscore_put_file(struct vfscore_file *file)
 
 int fget(int fd, struct vfscore_file **out_fp)
 {
-       int ret = 0;
        struct vfscore_file *fp = vfscore_get_file(fd);
-
        if (!fp)
-               ret = EBADF;
-       else
-               *out_fp = fp;
-
-       return ret;
+               return EBADF;
+       *out_fp = fp;
+       return 0;
 }
 
 int fdalloc(struct vfscore_file *fp, int *newfd)
 {
-       int fd, ret = 0;
-
-       fd = vfscore_alloc_fd();
-       if (fd < 0) {
-               ret = fd;
-               goto exit;
-       }
-
-       fhold(fp);
-
-       ret = vfscore_install_fd(fd, fp);
-       if (ret)
-               fdrop(fp);
-       else
-               *newfd = fd;
-
-exit:
-       return ret;
+       int r = uk_fdtab_legacy_open(fp);
+       if (r < 0)
+               return r;
+       *newfd = r;
+       return 0;
 }
 
 
 static int fdtable_init(void)
 {
-       memset(&fdtable, 0, sizeof(fdtable));
-
        return init_stdio();
 }
 
-uk_early_initcall_prio(fdtable_init, UK_PRIO_EARLIEST);
+uk_early_initcall(fdtable_init);
 
 #if CONFIG_LIBPOSIX_PROCESS_CLONE
 static int uk_posix_clone_files(const struct clone_args *cl_args,
index 910b3732685690f5c6fff1a1e061cf92535d6413..35fa6c69dd4adf8146855317832fe0f51ded7db3 100644 (file)
@@ -66,10 +66,6 @@ struct vfscore_file {
 #define FD_LOCK(fp)       uk_mutex_lock(&(fp->f_lock))
 #define FD_UNLOCK(fp)     uk_mutex_unlock(&(fp->f_lock))
 
-int vfscore_alloc_fd(void);
-int vfscore_reserve_fd(int fd);
-int vfscore_put_fd(int fd);
-int vfscore_install_fd(int fd, struct vfscore_file *file);
 struct vfscore_file *vfscore_get_file(int fd);
 void vfscore_put_file(struct vfscore_file *file);
 
index e0974feeaf80559407d66a0a968e584f2e233747..086ac5097265df0a3b1ff5220b061f8d63b23b46 100644 (file)
@@ -248,42 +248,6 @@ UK_SYSCALL_DEFINE(int, creat, const char*, pathname, mode_t, mode)
 
 LFS64(creat);
 
-UK_TRACEPOINT(trace_vfs_close, "%d", int);
-UK_TRACEPOINT(trace_vfs_close_ret, "");
-UK_TRACEPOINT(trace_vfs_close_err, "%d", int);
-
-int fdclose(int fd)
-{
-       struct vfscore_file *fp;
-       int error;
-
-       fp = vfscore_get_file(fd);
-       if (!fp)
-               return EBADF;
-
-       error = vfscore_put_fd(fd);
-       if (!error)
-               fdrop(fp);
-
-       return error;
-}
-
-UK_SYSCALL_R_DEFINE(int, close, int, fd)
-{
-       int error;
-
-       trace_vfs_close(fd);
-       error = fdclose(fd);
-       if (error)
-               goto out_error;
-
-       trace_vfs_close_ret();
-       return 0;
-
-       out_error:
-       trace_vfs_close_err(error);
-       return -error;
-}
 
 UK_TRACEPOINT(trace_vfs_mknod, "\"%s\" 0%0o %#x", const char*, mode_t, dev_t);
 UK_TRACEPOINT(trace_vfs_mknod_ret, "");
@@ -2066,110 +2030,6 @@ out_error:
        return ERR2PTR(-error);
 }
 
-UK_TRACEPOINT(trace_vfs_dup, "%d", int);
-UK_TRACEPOINT(trace_vfs_dup_ret, "\"%s\"", int);
-UK_TRACEPOINT(trace_vfs_dup_err, "%d", int);
-/*
- * Duplicate a file descriptor
- */
-UK_SYSCALL_R_DEFINE(int, dup, int, oldfd)
-{
-       struct vfscore_file *fp;
-       int newfd;
-       int error;
-
-       trace_vfs_dup(oldfd);
-       error = fget(oldfd, &fp);
-       if (error)
-               goto out_error;
-
-       error = fdalloc(fp, &newfd);
-       if (error)
-               goto out_fdrop;
-
-       fdrop(fp);
-       trace_vfs_dup_ret(newfd);
-       return newfd;
-
-       out_fdrop:
-       fdrop(fp);
-
-       out_error:
-       trace_vfs_dup_err(error);
-       if (error > 0)
-               return -error;
-       return error;
-}
-
-UK_TRACEPOINT(trace_vfs_dup3, "%d %d %#x", int, int, int);
-UK_TRACEPOINT(trace_vfs_dup3_ret, "%d", int);
-UK_TRACEPOINT(trace_vfs_dup3_err, "%d", int);
-/*
- * Duplicate a file descriptor to a particular value.
- */
-UK_SYSCALL_R_DEFINE(int, dup3, int, oldfd, int, newfd, int, flags)
-{
-       struct vfscore_file *fp;
-       int error;
-
-       trace_vfs_dup3(oldfd, newfd, flags);
-       /*
-        * Don't allow any argument but O_CLOEXEC.  But we even ignore
-        * that as we don't support exec() and thus don't care.
-        */
-       if ((flags & ~O_CLOEXEC) != 0) {
-               error = EINVAL;
-               goto out_error;
-       }
-
-       if (oldfd == newfd) {
-               error = EINVAL;
-               goto out_error;
-       }
-
-       error = fget(oldfd, &fp);
-       if (error)
-               goto out_error;
-
-       /* BUG: The close and reserve operation must be atomic */
-       error = uk_syscall_r_close(newfd);
-       if (error && error != -EBADF)
-               goto out_error_drop;
-
-       error = vfscore_reserve_fd(newfd);
-       if (error)
-               goto out_error_drop;
-
-       error = vfscore_install_fd(newfd, fp);
-       if (error)
-               goto out_error_drop;
-
-       trace_vfs_dup3_ret(newfd);
-       return newfd;
-
-out_error_drop:
-       fdrop(fp);
-out_error:
-       trace_vfs_dup3_err(error);
-       return (error > 0) ? -error : error;
-}
-
-UK_SYSCALL_R_DEFINE(int, dup2, int, oldfd, int, newfd)
-{
-       struct vfscore_file *fp;
-       int error;
-
-       if (unlikely(oldfd == newfd)) {
-               error = fget(oldfd, &fp);
-               if (unlikely(error))
-                       return -error;
-
-               fdrop(fp);
-               return newfd;
-       }
-
-       return uk_syscall_r_dup3(oldfd, newfd, 0);
-}
 
 /*
  * The file control system call.
index 3b24cb52efebe82d8d820cab23ca99cd1f135f4f..d0cf93e716a9e81010d9b31399f5083eeead0815 100644 (file)
@@ -49,6 +49,8 @@
 #include <uk/syscall.h>
 #include <uk/init.h>
 
+#include <uk/posix-fdtab-legacy.h>
+
 /* We use the default size in Linux kernel */
 #define PIPE_MAX_SIZE  (1 << CONFIG_LIBVFSCORE_PIPE_SIZE_ORDER)
 
@@ -590,13 +592,6 @@ static int pipe_fd_alloc(struct pipe_file *pipe_file, int flags)
        struct dentry *p_dentry;
        struct vnode *p_vnode;
 
-       /* Reserve file descriptor number */
-       vfs_fd = vfscore_alloc_fd();
-       if (vfs_fd < 0) {
-               ret = -ENFILE;
-               goto ERR_EXIT;
-       }
-
        /* Allocate file, dentry, and vnode */
        vfs_file = calloc(1, sizeof(*vfs_file));
        if (!vfs_file) {
@@ -621,9 +616,8 @@ static int pipe_fd_alloc(struct pipe_file *pipe_file, int flags)
        }
 
        /* Fill out necessary fields. */
-       vfs_file->fd = vfs_fd;
        vfs_file->f_flags = flags;
-       vfs_file->f_count = 1;
+       vfs_file->f_count = 0;
        vfs_file->f_data = pipe_file;
        vfs_file->f_dentry = p_dentry;
        vfs_file->f_vfs_flags = UK_VFSCORE_NOPOS;
@@ -635,9 +629,11 @@ static int pipe_fd_alloc(struct pipe_file *pipe_file, int flags)
        p_vnode->v_type = VFIFO;
 
        /* Assign the file descriptors to the corresponding vfs_file. */
-       ret = vfscore_install_fd(vfs_fd, vfs_file);
-       if (ret)
+       vfs_fd = uk_fdtab_legacy_open(vfs_file);
+       if (vfs_fd < 0) {
+               ret = vfs_fd;
                goto ERR_VFS_INSTALL;
+       }
 
        /* Only the dentry should hold a reference; release ours */
        vrele(p_vnode);
@@ -651,8 +647,6 @@ ERR_ALLOC_DENTRY:
 ERR_ALLOC_VNODE:
        free(vfs_file);
 ERR_MALLOC_VFS_FILE:
-       vfscore_put_fd(vfs_fd);
-ERR_EXIT:
        UK_ASSERT(ret < 0);
        return ret;
 }
@@ -685,7 +679,7 @@ UK_SYSCALL_R_DEFINE(int, pipe, int*, pipefd)
        return ret;
 
 ERR_W_FD:
-       vfscore_put_fd(r_fd);
+       uk_syscall_r_close(r_fd);
 ERR_VFS_INSTALL:
        pipe_file_free(pipe_file);
 ERR_EXIT:
index 431dd4ffdd3f555e8224b3ccd48441e8d90b19d8..ed53f9fb23788e4fdb5849365ba2c3c59cbacfd2 100644 (file)
@@ -46,6 +46,8 @@
 #include <vfscore/mount.h>
 #include <errno.h>
 
+#include <uk/posix-fdtab-legacy.h>
+
 /*
  * When the syscall_shim library is not part of the build, there is warning
  * of implicit declaration of uk_syscall_r_dup2.
@@ -233,12 +235,11 @@ int init_stdio(void)
 {
        int fd;
 
-       fd = vfscore_alloc_fd();
+       fd = uk_fdtab_legacy_open(&stdio_file);
        if (fd != 0) {
-               uk_pr_crit("failed to allocate fd for stdin (fd=0)\n");
+               uk_pr_crit("failed to allocate fd for stdin (fd=%d)\n", fd);
                return (fd < 0) ? fd : -EBADF;
        }
-       vfscore_install_fd(0, &stdio_file);
 
        fd = uk_syscall_r_dup3(0, 1, 0);
        if (fd != 1) {