Merge branch 'akpm' (patches from Andrew)
[linux-2.6-microblaze.git] / fs / file.c
index 21c0893..c0b6096 100644 (file)
--- a/fs/file.c
+++ b/fs/file.c
@@ -21,6 +21,7 @@
 #include <linux/rcupdate.h>
 #include <linux/close_range.h>
 #include <net/sock.h>
+#include <linux/io_uring.h>
 
 unsigned int sysctl_nr_open __read_mostly = 1024*1024;
 unsigned int sysctl_nr_open_min = BITS_PER_LONG;
@@ -157,7 +158,7 @@ static int expand_fdtable(struct files_struct *files, unsigned int nr)
        spin_unlock(&files->file_lock);
        new_fdt = alloc_fdtable(nr);
 
-       /* make sure all __fd_install() have seen resize_in_progress
+       /* make sure all fd_install() have seen resize_in_progress
         * or have finished their rcu_read_lock_sched() section.
         */
        if (atomic_read(&files->count) > 1)
@@ -180,7 +181,7 @@ static int expand_fdtable(struct files_struct *files, unsigned int nr)
        rcu_assign_pointer(files->fdt, new_fdt);
        if (cur_fdt != &files->fdtab)
                call_rcu(&cur_fdt->rcu, free_fdtable_rcu);
-       /* coupled with smp_rmb() in __fd_install() */
+       /* coupled with smp_rmb() in fd_install() */
        smp_wmb();
        return 1;
 }
@@ -410,19 +411,6 @@ static struct fdtable *close_files(struct files_struct * files)
        return fdt;
 }
 
-struct files_struct *get_files_struct(struct task_struct *task)
-{
-       struct files_struct *files;
-
-       task_lock(task);
-       files = task->files;
-       if (files)
-               atomic_inc(&files->count);
-       task_unlock(task);
-
-       return files;
-}
-
 void put_files_struct(struct files_struct *files)
 {
        if (atomic_dec_and_test(&files->count)) {
@@ -435,23 +423,12 @@ void put_files_struct(struct files_struct *files)
        }
 }
 
-void reset_files_struct(struct files_struct *files)
-{
-       struct task_struct *tsk = current;
-       struct files_struct *old;
-
-       old = tsk->files;
-       task_lock(tsk);
-       tsk->files = files;
-       task_unlock(tsk);
-       put_files_struct(old);
-}
-
 void exit_files(struct task_struct *tsk)
 {
        struct files_struct * files = tsk->files;
 
        if (files) {
+               io_uring_files_cancel(files);
                task_lock(tsk);
                tsk->files = NULL;
                task_unlock(tsk);
@@ -490,9 +467,9 @@ static unsigned int find_next_fd(struct fdtable *fdt, unsigned int start)
 /*
  * allocate a file descriptor, mark it busy.
  */
-int __alloc_fd(struct files_struct *files,
-              unsigned start, unsigned end, unsigned flags)
+static int alloc_fd(unsigned start, unsigned end, unsigned flags)
 {
+       struct files_struct *files = current->files;
        unsigned int fd;
        int error;
        struct fdtable *fdt;
@@ -548,14 +525,9 @@ out:
        return error;
 }
 
-static int alloc_fd(unsigned start, unsigned flags)
-{
-       return __alloc_fd(current->files, start, rlimit(RLIMIT_NOFILE), flags);
-}
-
 int __get_unused_fd_flags(unsigned flags, unsigned long nofile)
 {
-       return __alloc_fd(current->files, 0, nofile, flags);
+       return alloc_fd(0, nofile, flags);
 }
 
 int get_unused_fd_flags(unsigned flags)
@@ -594,17 +566,13 @@ EXPORT_SYMBOL(put_unused_fd);
  * It should never happen - if we allow dup2() do it, _really_ bad things
  * will follow.
  *
- * NOTE: __fd_install() variant is really, really low-level; don't
- * use it unless you are forced to by truly lousy API shoved down
- * your throat.  'files' *MUST* be either current->files or obtained
- * by get_files_struct(current) done by whoever had given it to you,
- * or really bad things will happen.  Normally you want to use
- * fd_install() instead.
+ * This consumes the "file" refcount, so callers should treat it
+ * as if they had called fput(file).
  */
 
-void __fd_install(struct files_struct *files, unsigned int fd,
-               struct file *file)
+void fd_install(unsigned int fd, struct file *file)
 {
+       struct files_struct *files = current->files;
        struct fdtable *fdt;
 
        rcu_read_lock_sched();
@@ -626,15 +594,6 @@ void __fd_install(struct files_struct *files, unsigned int fd,
        rcu_read_unlock_sched();
 }
 
-/*
- * This consumes the "file" refcount, so callers should treat it
- * as if they had called fput(file).
- */
-void fd_install(unsigned int fd, struct file *file)
-{
-       __fd_install(current->files, fd, file);
-}
-
 EXPORT_SYMBOL(fd_install);
 
 static struct file *pick_file(struct files_struct *files, unsigned fd)
@@ -657,11 +616,9 @@ out_unlock:
        return file;
 }
 
-/*
- * The same warnings as for __alloc_fd()/__fd_install() apply here...
- */
-int __close_fd(struct files_struct *files, unsigned fd)
+int close_fd(unsigned fd)
 {
+       struct files_struct *files = current->files;
        struct file *file;
 
        file = pick_file(files, fd);
@@ -670,7 +627,36 @@ int __close_fd(struct files_struct *files, unsigned fd)
 
        return filp_close(file, files);
 }
-EXPORT_SYMBOL(__close_fd); /* for ksys_close() */
+EXPORT_SYMBOL(close_fd); /* for ksys_close() */
+
+static inline void __range_cloexec(struct files_struct *cur_fds,
+                                  unsigned int fd, unsigned int max_fd)
+{
+       struct fdtable *fdt;
+
+       if (fd > max_fd)
+               return;
+
+       spin_lock(&cur_fds->file_lock);
+       fdt = files_fdtable(cur_fds);
+       bitmap_set(fdt->close_on_exec, fd, max_fd - fd + 1);
+       spin_unlock(&cur_fds->file_lock);
+}
+
+static inline void __range_close(struct files_struct *cur_fds, unsigned int fd,
+                                unsigned int max_fd)
+{
+       while (fd <= max_fd) {
+               struct file *file;
+
+               file = pick_file(cur_fds, fd++);
+               if (!file)
+                       continue;
+
+               filp_close(file, cur_fds);
+               cond_resched();
+       }
+}
 
 /**
  * __close_range() - Close all file descriptors in a given range.
@@ -687,7 +673,7 @@ int __close_range(unsigned fd, unsigned max_fd, unsigned int flags)
        struct task_struct *me = current;
        struct files_struct *cur_fds = me->files, *fds = NULL;
 
-       if (flags & ~CLOSE_RANGE_UNSHARE)
+       if (flags & ~(CLOSE_RANGE_UNSHARE | CLOSE_RANGE_CLOEXEC))
                return -EINVAL;
 
        if (fd > max_fd)
@@ -708,8 +694,10 @@ int __close_range(unsigned fd, unsigned max_fd, unsigned int flags)
                 * If the requested range is greater than the current maximum,
                 * we're closing everything so only copy all file descriptors
                 * beneath the lowest file descriptor.
+                * If the caller requested all fds to be made cloexec copy all
+                * of the file descriptors since they still want to use them.
                 */
-               if (max_fd >= cur_max)
+               if (!(flags & CLOSE_RANGE_CLOEXEC) && (max_fd >= cur_max))
                        max_unshare_fds = fd;
 
                ret = unshare_fd(CLONE_FILES, max_unshare_fds, &fds);
@@ -725,16 +713,11 @@ int __close_range(unsigned fd, unsigned max_fd, unsigned int flags)
        }
 
        max_fd = min(max_fd, cur_max);
-       while (fd <= max_fd) {
-               struct file *file;
 
-               file = pick_file(cur_fds, fd++);
-               if (!file)
-                       continue;
-
-               filp_close(file, cur_fds);
-               cond_resched();
-       }
+       if (flags & CLOSE_RANGE_CLOEXEC)
+               __range_cloexec(cur_fds, fd, max_fd);
+       else
+               __range_close(cur_fds, fd, max_fd);
 
        if (fds) {
                /*
@@ -751,11 +734,11 @@ int __close_range(unsigned fd, unsigned max_fd, unsigned int flags)
 }
 
 /*
- * variant of __close_fd that gets a ref on the file for later fput.
+ * variant of close_fd that gets a ref on the file for later fput.
  * The caller must ensure that filp_close() called on the file, and then
  * an fput().
  */
-int __close_fd_get_file(unsigned int fd, struct file **res)
+int close_fd_get_file(unsigned int fd, struct file **res)
 {
        struct files_struct *files = current->files;
        struct file *file;
@@ -824,7 +807,7 @@ static struct file *__fget_files(struct files_struct *files, unsigned int fd,
 
        rcu_read_lock();
 loop:
-       file = fcheck_files(files, fd);
+       file = files_lookup_fd_rcu(files, fd);
        if (file) {
                /* File object ref couldn't be taken.
                 * dup2() atomicity guarantee is the reason
@@ -875,6 +858,42 @@ struct file *fget_task(struct task_struct *task, unsigned int fd)
        return file;
 }
 
+struct file *task_lookup_fd_rcu(struct task_struct *task, unsigned int fd)
+{
+       /* Must be called with rcu_read_lock held */
+       struct files_struct *files;
+       struct file *file = NULL;
+
+       task_lock(task);
+       files = task->files;
+       if (files)
+               file = files_lookup_fd_rcu(files, fd);
+       task_unlock(task);
+
+       return file;
+}
+
+struct file *task_lookup_next_fd_rcu(struct task_struct *task, unsigned int *ret_fd)
+{
+       /* Must be called with rcu_read_lock held */
+       struct files_struct *files;
+       unsigned int fd = *ret_fd;
+       struct file *file = NULL;
+
+       task_lock(task);
+       files = task->files;
+       if (files) {
+               for (; fd < files_fdtable(files)->max_fds; fd++) {
+                       file = files_lookup_fd_rcu(files, fd);
+                       if (file)
+                               break;
+               }
+       }
+       task_unlock(task);
+       *ret_fd = fd;
+       return file;
+}
+
 /*
  * Lightweight file lookup - no refcnt increment if fd table isn't shared.
  *
@@ -897,7 +916,7 @@ static unsigned long __fget_light(unsigned int fd, fmode_t mask)
        struct file *file;
 
        if (atomic_read(&files->count) == 1) {
-               file = __fcheck_files(files, fd);
+               file = files_lookup_fd_raw(files, fd);
                if (!file || unlikely(file->f_mode & mask))
                        return 0;
                return (unsigned long)file;
@@ -1019,7 +1038,7 @@ int replace_fd(unsigned fd, struct file *file, unsigned flags)
        struct files_struct *files = current->files;
 
        if (!file)
-               return __close_fd(files, fd);
+               return close_fd(fd);
 
        if (fd >= rlimit(RLIMIT_NOFILE))
                return -EBADF;
@@ -1108,7 +1127,7 @@ static int ksys_dup3(unsigned int oldfd, unsigned int newfd, int flags)
 
        spin_lock(&files->file_lock);
        err = expand_files(files, newfd);
-       file = fcheck(oldfd);
+       file = files_lookup_fd_locked(files, oldfd);
        if (unlikely(!file))
                goto Ebadf;
        if (unlikely(err < 0)) {
@@ -1137,7 +1156,7 @@ SYSCALL_DEFINE2(dup2, unsigned int, oldfd, unsigned int, newfd)
                int retval = oldfd;
 
                rcu_read_lock();
-               if (!fcheck_files(files, oldfd))
+               if (!files_lookup_fd_rcu(files, oldfd))
                        retval = -EBADF;
                rcu_read_unlock();
                return retval;
@@ -1162,10 +1181,11 @@ SYSCALL_DEFINE1(dup, unsigned int, fildes)
 
 int f_dupfd(unsigned int from, struct file *file, unsigned flags)
 {
+       unsigned long nofile = rlimit(RLIMIT_NOFILE);
        int err;
-       if (from >= rlimit(RLIMIT_NOFILE))
+       if (from >= nofile)
                return -EINVAL;
-       err = alloc_fd(from, flags);
+       err = alloc_fd(from, nofile, flags);
        if (err >= 0) {
                get_file(file);
                fd_install(err, file);