file.h 2.1 KB
Newer Older
1
/* SPDX-License-Identifier: GPL-2.0 */
L
Linus Torvalds 已提交
2 3 4 5 6 7 8 9
/*
 * Wrapper functions for accessing the file_struct fd array.
 */

#ifndef __LINUX_FILE_H
#define __LINUX_FILE_H

#include <linux/compiler.h>
10
#include <linux/types.h>
A
Al Viro 已提交
11
#include <linux/posix_types.h>
L
Linus Torvalds 已提交
12

A
Al Viro 已提交
13
struct file;
14

15
extern void fput(struct file *);
L
Linus Torvalds 已提交
16

17 18 19
struct file_operations;
struct vfsmount;
struct dentry;
A
Al Viro 已提交
20
struct inode;
21
struct path;
A
Al Viro 已提交
22 23
extern struct file *alloc_file_pseudo(struct inode *, struct vfsmount *,
	const char *, int flags, const struct file_operations *);
A
Al Viro 已提交
24 25
extern struct file *alloc_file_clone(struct file *, int flags,
	const struct file_operations *);
26

L
Linus Torvalds 已提交
27 28
static inline void fput_light(struct file *file, int fput_needed)
{
29
	if (fput_needed)
L
Linus Torvalds 已提交
30 31 32
		fput(file);
}

A
Al Viro 已提交
33 34
struct fd {
	struct file *file;
35
	unsigned int flags;
A
Al Viro 已提交
36
};
37 38
#define FDPUT_FPUT       1
#define FDPUT_POS_UNLOCK 2
A
Al Viro 已提交
39 40 41

static inline void fdput(struct fd fd)
{
42
	if (fd.flags & FDPUT_FPUT)
A
Al Viro 已提交
43 44 45
		fput(fd.file);
}

46
extern struct file *fget(unsigned int fd);
A
Al Viro 已提交
47 48 49 50
extern struct file *fget_raw(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);
51
extern void __f_unlock_pos(struct file *);
A
Al Viro 已提交
52

A
Al Viro 已提交
53
static inline struct fd __to_fd(unsigned long v)
A
Al Viro 已提交
54
{
A
Al Viro 已提交
55
	return (struct fd){(struct file *)(v & ~3),v & 3};
A
Al Viro 已提交
56 57
}

A
Al Viro 已提交
58 59 60 61
static inline struct fd fdget(unsigned int fd)
{
	return __to_fd(__fdget(fd));
}
62

A
Al Viro 已提交
63 64
static inline struct fd fdget_raw(unsigned int fd)
{
A
Al Viro 已提交
65
	return __to_fd(__fdget_raw(fd));
A
Al Viro 已提交
66 67
}

68 69 70 71 72 73 74 75 76 77 78 79
static inline struct fd fdget_pos(int fd)
{
	return __to_fd(__fdget_pos(fd));
}

static inline void fdput_pos(struct fd f)
{
	if (f.flags & FDPUT_POS_UNLOCK)
		__f_unlock_pos(f.file);
	fdput(f);
}

80
extern int f_dupfd(unsigned int from, struct file *file, unsigned flags);
A
Al Viro 已提交
81
extern int replace_fd(unsigned fd, struct file *file, unsigned flags);
82
extern void set_close_on_exec(unsigned int fd, int flag);
83
extern bool get_close_on_exec(unsigned int fd);
A
Al Viro 已提交
84
extern int get_unused_fd_flags(unsigned flags);
85
extern void put_unused_fd(unsigned int fd);
L
Linus Torvalds 已提交
86

87
extern void fd_install(unsigned int fd, struct file *file);
L
Linus Torvalds 已提交
88

A
Al Viro 已提交
89 90 91
extern void flush_delayed_fput(void);
extern void __fput_sync(struct file *);

L
Linus Torvalds 已提交
92
#endif /* __LINUX_FILE_H */