Commit 53dec2ea authored by Jens Axboe's avatar Jens Axboe

fs: provide locked helper variant of close_fd_get_file()

Assumes current->files->file_lock is already held on invocation. Helps
the caller check the file before removing the fd, if it needs to.
Signed-off-by: default avatarJens Axboe <axboe@kernel.dk>
parent e342c807
...@@ -22,6 +22,8 @@ ...@@ -22,6 +22,8 @@
#include <linux/close_range.h> #include <linux/close_range.h>
#include <net/sock.h> #include <net/sock.h>
#include "internal.h"
unsigned int sysctl_nr_open __read_mostly = 1024*1024; unsigned int sysctl_nr_open __read_mostly = 1024*1024;
unsigned int sysctl_nr_open_min = BITS_PER_LONG; unsigned int sysctl_nr_open_min = BITS_PER_LONG;
/* our min() is unusable in constant expressions ;-/ */ /* our min() is unusable in constant expressions ;-/ */
...@@ -732,36 +734,48 @@ int __close_range(unsigned fd, unsigned max_fd, unsigned int flags) ...@@ -732,36 +734,48 @@ 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. * See close_fd_get_file() below, this variant assumes current->files->file_lock
* The caller must ensure that filp_close() called on the file, and then * is held.
* 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 files_struct *files = current->files;
struct file *file; struct file *file;
struct fdtable *fdt; struct fdtable *fdt;
spin_lock(&files->file_lock);
fdt = files_fdtable(files); fdt = files_fdtable(files);
if (fd >= fdt->max_fds) if (fd >= fdt->max_fds)
goto out_unlock; goto out_err;
file = fdt->fd[fd]; file = fdt->fd[fd];
if (!file) if (!file)
goto out_unlock; goto out_err;
rcu_assign_pointer(fdt->fd[fd], NULL); rcu_assign_pointer(fdt->fd[fd], NULL);
__put_unused_fd(files, fd); __put_unused_fd(files, fd);
spin_unlock(&files->file_lock);
get_file(file); get_file(file);
*res = file; *res = file;
return 0; return 0;
out_err:
out_unlock:
spin_unlock(&files->file_lock);
*res = NULL; *res = NULL;
return -ENOENT; return -ENOENT;
} }
/*
* 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)
{
struct files_struct *files = current->files;
int ret;
spin_lock(&files->file_lock);
ret = __close_fd_get_file(fd, res);
spin_unlock(&files->file_lock);
return ret;
}
void do_close_on_exec(struct files_struct *files) void do_close_on_exec(struct files_struct *files)
{ {
unsigned i; unsigned i;
......
...@@ -132,6 +132,7 @@ extern struct file *do_file_open_root(struct dentry *, struct vfsmount *, ...@@ -132,6 +132,7 @@ extern struct file *do_file_open_root(struct dentry *, struct vfsmount *,
const char *, const struct open_flags *); const char *, const struct open_flags *);
extern struct open_how build_open_how(int flags, umode_t mode); extern struct open_how build_open_how(int flags, umode_t mode);
extern int build_open_flags(const struct open_how *how, struct open_flags *op); extern int build_open_flags(const struct open_how *how, struct open_flags *op);
extern int __close_fd_get_file(unsigned int fd, struct file **res);
long do_sys_ftruncate(unsigned int fd, loff_t length, int small); long do_sys_ftruncate(unsigned int fd, loff_t length, int small);
int chmod_common(const struct path *path, umode_t mode); int chmod_common(const struct path *path, umode_t mode);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment