add struct fd constructors, get rid of __to_fd()
Make __fdget() et.al. return struct fd directly. New helpers: BORROWED_FD(file) and CLONED_FD(file), for borrowed and cloned file references resp. NOTE: this might need tuning; in particular, inline on __fget_light() is there to keep the code generation same as before - we probably want to keep it inlined in fdget() et.al. (especially so in fdget_pos()), but that needs profiling. Reviewed-by: Christian Brauner <brauner@kernel.org> Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
This commit is contained in:
parent
88a2f6468d
commit
de12c3391b
26
fs/file.c
26
fs/file.c
@ -1128,7 +1128,7 @@ EXPORT_SYMBOL(task_lookup_next_fdget_rcu);
|
|||||||
* The fput_needed flag returned by fget_light should be passed to the
|
* The fput_needed flag returned by fget_light should be passed to the
|
||||||
* corresponding fput_light.
|
* corresponding fput_light.
|
||||||
*/
|
*/
|
||||||
static unsigned long __fget_light(unsigned int fd, fmode_t mask)
|
static inline struct fd __fget_light(unsigned int fd, fmode_t mask)
|
||||||
{
|
{
|
||||||
struct files_struct *files = current->files;
|
struct files_struct *files = current->files;
|
||||||
struct file *file;
|
struct file *file;
|
||||||
@ -1145,22 +1145,22 @@ static unsigned long __fget_light(unsigned int fd, fmode_t mask)
|
|||||||
if (likely(atomic_read_acquire(&files->count) == 1)) {
|
if (likely(atomic_read_acquire(&files->count) == 1)) {
|
||||||
file = files_lookup_fd_raw(files, fd);
|
file = files_lookup_fd_raw(files, fd);
|
||||||
if (!file || unlikely(file->f_mode & mask))
|
if (!file || unlikely(file->f_mode & mask))
|
||||||
return 0;
|
return EMPTY_FD;
|
||||||
return (unsigned long)file;
|
return BORROWED_FD(file);
|
||||||
} else {
|
} else {
|
||||||
file = __fget_files(files, fd, mask);
|
file = __fget_files(files, fd, mask);
|
||||||
if (!file)
|
if (!file)
|
||||||
return 0;
|
return EMPTY_FD;
|
||||||
return FDPUT_FPUT | (unsigned long)file;
|
return CLONED_FD(file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
unsigned long __fdget(unsigned int fd)
|
struct fd fdget(unsigned int fd)
|
||||||
{
|
{
|
||||||
return __fget_light(fd, FMODE_PATH);
|
return __fget_light(fd, FMODE_PATH);
|
||||||
}
|
}
|
||||||
EXPORT_SYMBOL(__fdget);
|
EXPORT_SYMBOL(fdget);
|
||||||
|
|
||||||
unsigned long __fdget_raw(unsigned int fd)
|
struct fd fdget_raw(unsigned int fd)
|
||||||
{
|
{
|
||||||
return __fget_light(fd, 0);
|
return __fget_light(fd, 0);
|
||||||
}
|
}
|
||||||
@ -1181,16 +1181,16 @@ static inline bool file_needs_f_pos_lock(struct file *file)
|
|||||||
(file_count(file) > 1 || file->f_op->iterate_shared);
|
(file_count(file) > 1 || file->f_op->iterate_shared);
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned long __fdget_pos(unsigned int fd)
|
struct fd fdget_pos(unsigned int fd)
|
||||||
{
|
{
|
||||||
unsigned long v = __fdget(fd);
|
struct fd f = fdget(fd);
|
||||||
struct file *file = (struct file *)(v & ~3);
|
struct file *file = fd_file(f);
|
||||||
|
|
||||||
if (file && file_needs_f_pos_lock(file)) {
|
if (file && file_needs_f_pos_lock(file)) {
|
||||||
v |= FDPUT_POS_UNLOCK;
|
f.word |= FDPUT_POS_UNLOCK;
|
||||||
mutex_lock(&file->f_pos_lock);
|
mutex_lock(&file->f_pos_lock);
|
||||||
}
|
}
|
||||||
return v;
|
return f;
|
||||||
}
|
}
|
||||||
|
|
||||||
void __f_unlock_pos(struct file *f)
|
void __f_unlock_pos(struct file *f)
|
||||||
|
@ -53,6 +53,14 @@ static inline bool fd_empty(struct fd f)
|
|||||||
}
|
}
|
||||||
|
|
||||||
#define EMPTY_FD (struct fd){0}
|
#define EMPTY_FD (struct fd){0}
|
||||||
|
static inline struct fd BORROWED_FD(struct file *f)
|
||||||
|
{
|
||||||
|
return (struct fd){(unsigned long)f};
|
||||||
|
}
|
||||||
|
static inline struct fd CLONED_FD(struct file *f)
|
||||||
|
{
|
||||||
|
return (struct fd){(unsigned long)f | FDPUT_FPUT};
|
||||||
|
}
|
||||||
|
|
||||||
static inline void fdput(struct fd fd)
|
static inline void fdput(struct fd fd)
|
||||||
{
|
{
|
||||||
@ -63,30 +71,11 @@ static inline void fdput(struct fd fd)
|
|||||||
extern struct file *fget(unsigned int fd);
|
extern struct file *fget(unsigned int fd);
|
||||||
extern struct file *fget_raw(unsigned int fd);
|
extern struct file *fget_raw(unsigned int fd);
|
||||||
extern struct file *fget_task(struct task_struct *task, unsigned int fd);
|
extern struct file *fget_task(struct task_struct *task, unsigned int fd);
|
||||||
extern unsigned long __fdget(unsigned int fd);
|
|
||||||
extern unsigned long __fdget_raw(unsigned int fd);
|
|
||||||
extern unsigned long __fdget_pos(unsigned int fd);
|
|
||||||
extern void __f_unlock_pos(struct file *);
|
extern void __f_unlock_pos(struct file *);
|
||||||
|
|
||||||
static inline struct fd __to_fd(unsigned long v)
|
struct fd fdget(unsigned int fd);
|
||||||
{
|
struct fd fdget_raw(unsigned int fd);
|
||||||
return (struct fd){v};
|
struct fd fdget_pos(unsigned int fd);
|
||||||
}
|
|
||||||
|
|
||||||
static inline struct fd fdget(unsigned int fd)
|
|
||||||
{
|
|
||||||
return __to_fd(__fdget(fd));
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline struct fd fdget_raw(unsigned int fd)
|
|
||||||
{
|
|
||||||
return __to_fd(__fdget_raw(fd));
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline struct fd fdget_pos(int fd)
|
|
||||||
{
|
|
||||||
return __to_fd(__fdget_pos(fd));
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline void fdput_pos(struct fd f)
|
static inline void fdput_pos(struct fd f)
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user