Merge tag 'scsi-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[linux-2.6-microblaze.git] / fs / file.c
index 6215637..dab120b 100644 (file)
--- a/fs/file.c
+++ b/fs/file.c
@@ -21,7 +21,6 @@
 #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;
@@ -411,19 +410,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)) {
@@ -441,7 +427,6 @@ 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);
@@ -629,11 +614,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);
@@ -642,7 +625,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.
@@ -659,7 +671,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)
@@ -680,8 +692,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);
@@ -697,16 +711,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) {
                /*
@@ -723,11 +732,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;
@@ -1027,7 +1036,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;