xref: /OK3568_Linux_fs/kernel/include/linux/fs.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef _LINUX_FS_H
3*4882a593Smuzhiyun #define _LINUX_FS_H
4*4882a593Smuzhiyun 
5*4882a593Smuzhiyun #include <linux/linkage.h>
6*4882a593Smuzhiyun #include <linux/wait_bit.h>
7*4882a593Smuzhiyun #include <linux/kdev_t.h>
8*4882a593Smuzhiyun #include <linux/dcache.h>
9*4882a593Smuzhiyun #include <linux/path.h>
10*4882a593Smuzhiyun #include <linux/stat.h>
11*4882a593Smuzhiyun #include <linux/cache.h>
12*4882a593Smuzhiyun #include <linux/list.h>
13*4882a593Smuzhiyun #include <linux/list_lru.h>
14*4882a593Smuzhiyun #include <linux/llist.h>
15*4882a593Smuzhiyun #include <linux/radix-tree.h>
16*4882a593Smuzhiyun #include <linux/xarray.h>
17*4882a593Smuzhiyun #include <linux/rbtree.h>
18*4882a593Smuzhiyun #include <linux/init.h>
19*4882a593Smuzhiyun #include <linux/pid.h>
20*4882a593Smuzhiyun #include <linux/bug.h>
21*4882a593Smuzhiyun #include <linux/mutex.h>
22*4882a593Smuzhiyun #include <linux/rwsem.h>
23*4882a593Smuzhiyun #include <linux/mm_types.h>
24*4882a593Smuzhiyun #include <linux/capability.h>
25*4882a593Smuzhiyun #include <linux/semaphore.h>
26*4882a593Smuzhiyun #include <linux/fcntl.h>
27*4882a593Smuzhiyun #include <linux/rculist_bl.h>
28*4882a593Smuzhiyun #include <linux/atomic.h>
29*4882a593Smuzhiyun #include <linux/shrinker.h>
30*4882a593Smuzhiyun #include <linux/migrate_mode.h>
31*4882a593Smuzhiyun #include <linux/uidgid.h>
32*4882a593Smuzhiyun #include <linux/lockdep.h>
33*4882a593Smuzhiyun #include <linux/percpu-rwsem.h>
34*4882a593Smuzhiyun #include <linux/workqueue.h>
35*4882a593Smuzhiyun #include <linux/delayed_call.h>
36*4882a593Smuzhiyun #include <linux/uuid.h>
37*4882a593Smuzhiyun #include <linux/errseq.h>
38*4882a593Smuzhiyun #include <linux/ioprio.h>
39*4882a593Smuzhiyun #include <linux/fs_types.h>
40*4882a593Smuzhiyun #include <linux/build_bug.h>
41*4882a593Smuzhiyun #include <linux/stddef.h>
42*4882a593Smuzhiyun #include <linux/android_kabi.h>
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun #include <asm/byteorder.h>
45*4882a593Smuzhiyun #include <uapi/linux/fs.h>
46*4882a593Smuzhiyun #include <linux/android_vendor.h>
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun struct backing_dev_info;
49*4882a593Smuzhiyun struct bdi_writeback;
50*4882a593Smuzhiyun struct bio;
51*4882a593Smuzhiyun struct export_operations;
52*4882a593Smuzhiyun struct fiemap_extent_info;
53*4882a593Smuzhiyun struct hd_geometry;
54*4882a593Smuzhiyun struct iovec;
55*4882a593Smuzhiyun struct kiocb;
56*4882a593Smuzhiyun struct kobject;
57*4882a593Smuzhiyun struct pipe_inode_info;
58*4882a593Smuzhiyun struct poll_table_struct;
59*4882a593Smuzhiyun struct kstatfs;
60*4882a593Smuzhiyun struct vm_area_struct;
61*4882a593Smuzhiyun struct vfsmount;
62*4882a593Smuzhiyun struct cred;
63*4882a593Smuzhiyun struct swap_info_struct;
64*4882a593Smuzhiyun struct seq_file;
65*4882a593Smuzhiyun struct workqueue_struct;
66*4882a593Smuzhiyun struct iov_iter;
67*4882a593Smuzhiyun struct fscrypt_info;
68*4882a593Smuzhiyun struct fscrypt_operations;
69*4882a593Smuzhiyun struct fsverity_info;
70*4882a593Smuzhiyun struct fsverity_operations;
71*4882a593Smuzhiyun struct fs_context;
72*4882a593Smuzhiyun struct fs_parameter_spec;
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun extern void __init inode_init(void);
75*4882a593Smuzhiyun extern void __init inode_init_early(void);
76*4882a593Smuzhiyun extern void __init files_init(void);
77*4882a593Smuzhiyun extern void __init files_maxfiles_init(void);
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun extern struct files_stat_struct files_stat;
80*4882a593Smuzhiyun extern unsigned long get_max_files(void);
81*4882a593Smuzhiyun extern unsigned int sysctl_nr_open;
82*4882a593Smuzhiyun extern struct inodes_stat_t inodes_stat;
83*4882a593Smuzhiyun extern int leases_enable, lease_break_time;
84*4882a593Smuzhiyun extern int sysctl_protected_symlinks;
85*4882a593Smuzhiyun extern int sysctl_protected_hardlinks;
86*4882a593Smuzhiyun extern int sysctl_protected_fifos;
87*4882a593Smuzhiyun extern int sysctl_protected_regular;
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun typedef __kernel_rwf_t rwf_t;
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun struct buffer_head;
92*4882a593Smuzhiyun typedef int (get_block_t)(struct inode *inode, sector_t iblock,
93*4882a593Smuzhiyun 			struct buffer_head *bh_result, int create);
94*4882a593Smuzhiyun typedef int (dio_iodone_t)(struct kiocb *iocb, loff_t offset,
95*4882a593Smuzhiyun 			ssize_t bytes, void *private);
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun #define MAY_EXEC		0x00000001
98*4882a593Smuzhiyun #define MAY_WRITE		0x00000002
99*4882a593Smuzhiyun #define MAY_READ		0x00000004
100*4882a593Smuzhiyun #define MAY_APPEND		0x00000008
101*4882a593Smuzhiyun #define MAY_ACCESS		0x00000010
102*4882a593Smuzhiyun #define MAY_OPEN		0x00000020
103*4882a593Smuzhiyun #define MAY_CHDIR		0x00000040
104*4882a593Smuzhiyun /* called from RCU mode, don't block */
105*4882a593Smuzhiyun #define MAY_NOT_BLOCK		0x00000080
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun /*
108*4882a593Smuzhiyun  * flags in file.f_mode.  Note that FMODE_READ and FMODE_WRITE must correspond
109*4882a593Smuzhiyun  * to O_WRONLY and O_RDWR via the strange trick in do_dentry_open()
110*4882a593Smuzhiyun  */
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun /* file is open for reading */
113*4882a593Smuzhiyun #define FMODE_READ		((__force fmode_t)0x1)
114*4882a593Smuzhiyun /* file is open for writing */
115*4882a593Smuzhiyun #define FMODE_WRITE		((__force fmode_t)0x2)
116*4882a593Smuzhiyun /* file is seekable */
117*4882a593Smuzhiyun #define FMODE_LSEEK		((__force fmode_t)0x4)
118*4882a593Smuzhiyun /* file can be accessed using pread */
119*4882a593Smuzhiyun #define FMODE_PREAD		((__force fmode_t)0x8)
120*4882a593Smuzhiyun /* file can be accessed using pwrite */
121*4882a593Smuzhiyun #define FMODE_PWRITE		((__force fmode_t)0x10)
122*4882a593Smuzhiyun /* File is opened for execution with sys_execve / sys_uselib */
123*4882a593Smuzhiyun #define FMODE_EXEC		((__force fmode_t)0x20)
124*4882a593Smuzhiyun /* File is opened with O_NDELAY (only set for block devices) */
125*4882a593Smuzhiyun #define FMODE_NDELAY		((__force fmode_t)0x40)
126*4882a593Smuzhiyun /* File is opened with O_EXCL (only set for block devices) */
127*4882a593Smuzhiyun #define FMODE_EXCL		((__force fmode_t)0x80)
128*4882a593Smuzhiyun /* File is opened using open(.., 3, ..) and is writeable only for ioctls
129*4882a593Smuzhiyun    (specialy hack for floppy.c) */
130*4882a593Smuzhiyun #define FMODE_WRITE_IOCTL	((__force fmode_t)0x100)
131*4882a593Smuzhiyun /* 32bit hashes as llseek() offset (for directories) */
132*4882a593Smuzhiyun #define FMODE_32BITHASH         ((__force fmode_t)0x200)
133*4882a593Smuzhiyun /* 64bit hashes as llseek() offset (for directories) */
134*4882a593Smuzhiyun #define FMODE_64BITHASH         ((__force fmode_t)0x400)
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun /*
137*4882a593Smuzhiyun  * Don't update ctime and mtime.
138*4882a593Smuzhiyun  *
139*4882a593Smuzhiyun  * Currently a special hack for the XFS open_by_handle ioctl, but we'll
140*4882a593Smuzhiyun  * hopefully graduate it to a proper O_CMTIME flag supported by open(2) soon.
141*4882a593Smuzhiyun  */
142*4882a593Smuzhiyun #define FMODE_NOCMTIME		((__force fmode_t)0x800)
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun /* Expect random access pattern */
145*4882a593Smuzhiyun #define FMODE_RANDOM		((__force fmode_t)0x1000)
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun /* File is huge (eg. /dev/kmem): treat loff_t as unsigned */
148*4882a593Smuzhiyun #define FMODE_UNSIGNED_OFFSET	((__force fmode_t)0x2000)
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun /* File is opened with O_PATH; almost nothing can be done with it */
151*4882a593Smuzhiyun #define FMODE_PATH		((__force fmode_t)0x4000)
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun /* File needs atomic accesses to f_pos */
154*4882a593Smuzhiyun #define FMODE_ATOMIC_POS	((__force fmode_t)0x8000)
155*4882a593Smuzhiyun /* Write access to underlying fs */
156*4882a593Smuzhiyun #define FMODE_WRITER		((__force fmode_t)0x10000)
157*4882a593Smuzhiyun /* Has read method(s) */
158*4882a593Smuzhiyun #define FMODE_CAN_READ          ((__force fmode_t)0x20000)
159*4882a593Smuzhiyun /* Has write method(s) */
160*4882a593Smuzhiyun #define FMODE_CAN_WRITE         ((__force fmode_t)0x40000)
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun #define FMODE_OPENED		((__force fmode_t)0x80000)
163*4882a593Smuzhiyun #define FMODE_CREATED		((__force fmode_t)0x100000)
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun /* File is stream-like */
166*4882a593Smuzhiyun #define FMODE_STREAM		((__force fmode_t)0x200000)
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun /* File was opened by fanotify and shouldn't generate fanotify events */
169*4882a593Smuzhiyun #define FMODE_NONOTIFY		((__force fmode_t)0x4000000)
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun /* File is capable of returning -EAGAIN if I/O will block */
172*4882a593Smuzhiyun #define FMODE_NOWAIT		((__force fmode_t)0x8000000)
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun /* File represents mount that needs unmounting */
175*4882a593Smuzhiyun #define FMODE_NEED_UNMOUNT	((__force fmode_t)0x10000000)
176*4882a593Smuzhiyun 
177*4882a593Smuzhiyun /* File does not contribute to nr_files count */
178*4882a593Smuzhiyun #define FMODE_NOACCOUNT		((__force fmode_t)0x20000000)
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun /* File supports async buffered reads */
181*4882a593Smuzhiyun #define FMODE_BUF_RASYNC	((__force fmode_t)0x40000000)
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun /*
184*4882a593Smuzhiyun  * Attribute flags.  These should be or-ed together to figure out what
185*4882a593Smuzhiyun  * has been changed!
186*4882a593Smuzhiyun  */
187*4882a593Smuzhiyun #define ATTR_MODE	(1 << 0)
188*4882a593Smuzhiyun #define ATTR_UID	(1 << 1)
189*4882a593Smuzhiyun #define ATTR_GID	(1 << 2)
190*4882a593Smuzhiyun #define ATTR_SIZE	(1 << 3)
191*4882a593Smuzhiyun #define ATTR_ATIME	(1 << 4)
192*4882a593Smuzhiyun #define ATTR_MTIME	(1 << 5)
193*4882a593Smuzhiyun #define ATTR_CTIME	(1 << 6)
194*4882a593Smuzhiyun #define ATTR_ATIME_SET	(1 << 7)
195*4882a593Smuzhiyun #define ATTR_MTIME_SET	(1 << 8)
196*4882a593Smuzhiyun #define ATTR_FORCE	(1 << 9) /* Not a change, but a change it */
197*4882a593Smuzhiyun #define ATTR_KILL_SUID	(1 << 11)
198*4882a593Smuzhiyun #define ATTR_KILL_SGID	(1 << 12)
199*4882a593Smuzhiyun #define ATTR_FILE	(1 << 13)
200*4882a593Smuzhiyun #define ATTR_KILL_PRIV	(1 << 14)
201*4882a593Smuzhiyun #define ATTR_OPEN	(1 << 15) /* Truncating from open(O_TRUNC) */
202*4882a593Smuzhiyun #define ATTR_TIMES_SET	(1 << 16)
203*4882a593Smuzhiyun #define ATTR_TOUCH	(1 << 17)
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun /*
206*4882a593Smuzhiyun  * Whiteout is represented by a char device.  The following constants define the
207*4882a593Smuzhiyun  * mode and device number to use.
208*4882a593Smuzhiyun  */
209*4882a593Smuzhiyun #define WHITEOUT_MODE 0
210*4882a593Smuzhiyun #define WHITEOUT_DEV 0
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun /*
213*4882a593Smuzhiyun  * This is the Inode Attributes structure, used for notify_change().  It
214*4882a593Smuzhiyun  * uses the above definitions as flags, to know which values have changed.
215*4882a593Smuzhiyun  * Also, in this manner, a Filesystem can look at only the values it cares
216*4882a593Smuzhiyun  * about.  Basically, these are the attributes that the VFS layer can
217*4882a593Smuzhiyun  * request to change from the FS layer.
218*4882a593Smuzhiyun  *
219*4882a593Smuzhiyun  * Derek Atkins <warlord@MIT.EDU> 94-10-20
220*4882a593Smuzhiyun  */
221*4882a593Smuzhiyun struct iattr {
222*4882a593Smuzhiyun 	unsigned int	ia_valid;
223*4882a593Smuzhiyun 	umode_t		ia_mode;
224*4882a593Smuzhiyun 	kuid_t		ia_uid;
225*4882a593Smuzhiyun 	kgid_t		ia_gid;
226*4882a593Smuzhiyun 	loff_t		ia_size;
227*4882a593Smuzhiyun 	struct timespec64 ia_atime;
228*4882a593Smuzhiyun 	struct timespec64 ia_mtime;
229*4882a593Smuzhiyun 	struct timespec64 ia_ctime;
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 	/*
232*4882a593Smuzhiyun 	 * Not an attribute, but an auxiliary info for filesystems wanting to
233*4882a593Smuzhiyun 	 * implement an ftruncate() like method.  NOTE: filesystem should
234*4882a593Smuzhiyun 	 * check for (ia_valid & ATTR_FILE), and not for (ia_file != NULL).
235*4882a593Smuzhiyun 	 */
236*4882a593Smuzhiyun 	struct file	*ia_file;
237*4882a593Smuzhiyun };
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun /*
240*4882a593Smuzhiyun  * Includes for diskquotas.
241*4882a593Smuzhiyun  */
242*4882a593Smuzhiyun #include <linux/quota.h>
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun /*
245*4882a593Smuzhiyun  * Maximum number of layers of fs stack.  Needs to be limited to
246*4882a593Smuzhiyun  * prevent kernel stack overflow
247*4882a593Smuzhiyun  */
248*4882a593Smuzhiyun #define FILESYSTEM_MAX_STACK_DEPTH 2
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun /**
251*4882a593Smuzhiyun  * enum positive_aop_returns - aop return codes with specific semantics
252*4882a593Smuzhiyun  *
253*4882a593Smuzhiyun  * @AOP_WRITEPAGE_ACTIVATE: Informs the caller that page writeback has
254*4882a593Smuzhiyun  * 			    completed, that the page is still locked, and
255*4882a593Smuzhiyun  * 			    should be considered active.  The VM uses this hint
256*4882a593Smuzhiyun  * 			    to return the page to the active list -- it won't
257*4882a593Smuzhiyun  * 			    be a candidate for writeback again in the near
258*4882a593Smuzhiyun  * 			    future.  Other callers must be careful to unlock
259*4882a593Smuzhiyun  * 			    the page if they get this return.  Returned by
260*4882a593Smuzhiyun  * 			    writepage();
261*4882a593Smuzhiyun  *
262*4882a593Smuzhiyun  * @AOP_TRUNCATED_PAGE: The AOP method that was handed a locked page has
263*4882a593Smuzhiyun  *  			unlocked it and the page might have been truncated.
264*4882a593Smuzhiyun  *  			The caller should back up to acquiring a new page and
265*4882a593Smuzhiyun  *  			trying again.  The aop will be taking reasonable
266*4882a593Smuzhiyun  *  			precautions not to livelock.  If the caller held a page
267*4882a593Smuzhiyun  *  			reference, it should drop it before retrying.  Returned
268*4882a593Smuzhiyun  *  			by readpage().
269*4882a593Smuzhiyun  *
270*4882a593Smuzhiyun  * address_space_operation functions return these large constants to indicate
271*4882a593Smuzhiyun  * special semantics to the caller.  These are much larger than the bytes in a
272*4882a593Smuzhiyun  * page to allow for functions that return the number of bytes operated on in a
273*4882a593Smuzhiyun  * given page.
274*4882a593Smuzhiyun  */
275*4882a593Smuzhiyun 
276*4882a593Smuzhiyun enum positive_aop_returns {
277*4882a593Smuzhiyun 	AOP_WRITEPAGE_ACTIVATE	= 0x80000,
278*4882a593Smuzhiyun 	AOP_TRUNCATED_PAGE	= 0x80001,
279*4882a593Smuzhiyun };
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun #define AOP_FLAG_CONT_EXPAND		0x0001 /* called from cont_expand */
282*4882a593Smuzhiyun #define AOP_FLAG_NOFS			0x0002 /* used by filesystem to direct
283*4882a593Smuzhiyun 						* helper code (eg buffer layer)
284*4882a593Smuzhiyun 						* to clear GFP_FS from alloc */
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun /*
287*4882a593Smuzhiyun  * oh the beauties of C type declarations.
288*4882a593Smuzhiyun  */
289*4882a593Smuzhiyun struct page;
290*4882a593Smuzhiyun struct address_space;
291*4882a593Smuzhiyun struct writeback_control;
292*4882a593Smuzhiyun struct readahead_control;
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun /*
295*4882a593Smuzhiyun  * Write life time hint values.
296*4882a593Smuzhiyun  * Stored in struct inode as u8.
297*4882a593Smuzhiyun  */
298*4882a593Smuzhiyun enum rw_hint {
299*4882a593Smuzhiyun 	WRITE_LIFE_NOT_SET	= 0,
300*4882a593Smuzhiyun 	WRITE_LIFE_NONE		= RWH_WRITE_LIFE_NONE,
301*4882a593Smuzhiyun 	WRITE_LIFE_SHORT	= RWH_WRITE_LIFE_SHORT,
302*4882a593Smuzhiyun 	WRITE_LIFE_MEDIUM	= RWH_WRITE_LIFE_MEDIUM,
303*4882a593Smuzhiyun 	WRITE_LIFE_LONG		= RWH_WRITE_LIFE_LONG,
304*4882a593Smuzhiyun 	WRITE_LIFE_EXTREME	= RWH_WRITE_LIFE_EXTREME,
305*4882a593Smuzhiyun };
306*4882a593Smuzhiyun 
307*4882a593Smuzhiyun /* Match RWF_* bits to IOCB bits */
308*4882a593Smuzhiyun #define IOCB_HIPRI		(__force int) RWF_HIPRI
309*4882a593Smuzhiyun #define IOCB_DSYNC		(__force int) RWF_DSYNC
310*4882a593Smuzhiyun #define IOCB_SYNC		(__force int) RWF_SYNC
311*4882a593Smuzhiyun #define IOCB_NOWAIT		(__force int) RWF_NOWAIT
312*4882a593Smuzhiyun #define IOCB_APPEND		(__force int) RWF_APPEND
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun /* non-RWF related bits - start at 16 */
315*4882a593Smuzhiyun #define IOCB_EVENTFD		(1 << 16)
316*4882a593Smuzhiyun #define IOCB_DIRECT		(1 << 17)
317*4882a593Smuzhiyun #define IOCB_WRITE		(1 << 18)
318*4882a593Smuzhiyun /* iocb->ki_waitq is valid */
319*4882a593Smuzhiyun #define IOCB_WAITQ		(1 << 19)
320*4882a593Smuzhiyun #define IOCB_NOIO		(1 << 20)
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun struct kiocb {
323*4882a593Smuzhiyun 	struct file		*ki_filp;
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 	/* The 'ki_filp' pointer is shared in a union for aio */
326*4882a593Smuzhiyun 	randomized_struct_fields_start
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun 	loff_t			ki_pos;
329*4882a593Smuzhiyun 	void (*ki_complete)(struct kiocb *iocb, long ret, long ret2);
330*4882a593Smuzhiyun 	void			*private;
331*4882a593Smuzhiyun 	int			ki_flags;
332*4882a593Smuzhiyun 	u16			ki_hint;
333*4882a593Smuzhiyun 	u16			ki_ioprio; /* See linux/ioprio.h */
334*4882a593Smuzhiyun 	union {
335*4882a593Smuzhiyun 		unsigned int		ki_cookie; /* for ->iopoll */
336*4882a593Smuzhiyun 		struct wait_page_queue	*ki_waitq; /* for async buffered IO */
337*4882a593Smuzhiyun 	};
338*4882a593Smuzhiyun 
339*4882a593Smuzhiyun 	randomized_struct_fields_end
340*4882a593Smuzhiyun };
341*4882a593Smuzhiyun 
is_sync_kiocb(struct kiocb * kiocb)342*4882a593Smuzhiyun static inline bool is_sync_kiocb(struct kiocb *kiocb)
343*4882a593Smuzhiyun {
344*4882a593Smuzhiyun 	return kiocb->ki_complete == NULL;
345*4882a593Smuzhiyun }
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun /*
348*4882a593Smuzhiyun  * "descriptor" for what we're up to with a read.
349*4882a593Smuzhiyun  * This allows us to use the same read code yet
350*4882a593Smuzhiyun  * have multiple different users of the data that
351*4882a593Smuzhiyun  * we read from a file.
352*4882a593Smuzhiyun  *
353*4882a593Smuzhiyun  * The simplest case just copies the data to user
354*4882a593Smuzhiyun  * mode.
355*4882a593Smuzhiyun  */
356*4882a593Smuzhiyun typedef struct {
357*4882a593Smuzhiyun 	size_t written;
358*4882a593Smuzhiyun 	size_t count;
359*4882a593Smuzhiyun 	union {
360*4882a593Smuzhiyun 		char __user *buf;
361*4882a593Smuzhiyun 		void *data;
362*4882a593Smuzhiyun 	} arg;
363*4882a593Smuzhiyun 	int error;
364*4882a593Smuzhiyun } read_descriptor_t;
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun typedef int (*read_actor_t)(read_descriptor_t *, struct page *,
367*4882a593Smuzhiyun 		unsigned long, unsigned long);
368*4882a593Smuzhiyun 
369*4882a593Smuzhiyun struct address_space_operations {
370*4882a593Smuzhiyun 	int (*writepage)(struct page *page, struct writeback_control *wbc);
371*4882a593Smuzhiyun 	int (*readpage)(struct file *, struct page *);
372*4882a593Smuzhiyun 
373*4882a593Smuzhiyun 	/* Write back some dirty pages from this mapping. */
374*4882a593Smuzhiyun 	int (*writepages)(struct address_space *, struct writeback_control *);
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun 	/* Set a page dirty.  Return true if this dirtied it */
377*4882a593Smuzhiyun 	int (*set_page_dirty)(struct page *page);
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun 	/*
380*4882a593Smuzhiyun 	 * Reads in the requested pages. Unlike ->readpage(), this is
381*4882a593Smuzhiyun 	 * PURELY used for read-ahead!.
382*4882a593Smuzhiyun 	 */
383*4882a593Smuzhiyun 	int (*readpages)(struct file *filp, struct address_space *mapping,
384*4882a593Smuzhiyun 			struct list_head *pages, unsigned nr_pages);
385*4882a593Smuzhiyun 	void (*readahead)(struct readahead_control *);
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun 	int (*write_begin)(struct file *, struct address_space *mapping,
388*4882a593Smuzhiyun 				loff_t pos, unsigned len, unsigned flags,
389*4882a593Smuzhiyun 				struct page **pagep, void **fsdata);
390*4882a593Smuzhiyun 	int (*write_end)(struct file *, struct address_space *mapping,
391*4882a593Smuzhiyun 				loff_t pos, unsigned len, unsigned copied,
392*4882a593Smuzhiyun 				struct page *page, void *fsdata);
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun 	/* Unfortunately this kludge is needed for FIBMAP. Don't use it */
395*4882a593Smuzhiyun 	sector_t (*bmap)(struct address_space *, sector_t);
396*4882a593Smuzhiyun 	void (*invalidatepage) (struct page *, unsigned int, unsigned int);
397*4882a593Smuzhiyun 	int (*releasepage) (struct page *, gfp_t);
398*4882a593Smuzhiyun 	void (*freepage)(struct page *);
399*4882a593Smuzhiyun 	ssize_t (*direct_IO)(struct kiocb *, struct iov_iter *iter);
400*4882a593Smuzhiyun 	/*
401*4882a593Smuzhiyun 	 * migrate the contents of a page to the specified target. If
402*4882a593Smuzhiyun 	 * migrate_mode is MIGRATE_ASYNC, it must not block.
403*4882a593Smuzhiyun 	 */
404*4882a593Smuzhiyun 	int (*migratepage) (struct address_space *,
405*4882a593Smuzhiyun 			struct page *, struct page *, enum migrate_mode);
406*4882a593Smuzhiyun 	bool (*isolate_page)(struct page *, isolate_mode_t);
407*4882a593Smuzhiyun 	void (*putback_page)(struct page *);
408*4882a593Smuzhiyun 	int (*launder_page) (struct page *);
409*4882a593Smuzhiyun 	int (*is_partially_uptodate) (struct page *, unsigned long,
410*4882a593Smuzhiyun 					unsigned long);
411*4882a593Smuzhiyun 	void (*is_dirty_writeback) (struct page *, bool *, bool *);
412*4882a593Smuzhiyun 	int (*error_remove_page)(struct address_space *, struct page *);
413*4882a593Smuzhiyun 
414*4882a593Smuzhiyun 	/* swapfile support */
415*4882a593Smuzhiyun 	int (*swap_activate)(struct swap_info_struct *sis, struct file *file,
416*4882a593Smuzhiyun 				sector_t *span);
417*4882a593Smuzhiyun 	void (*swap_deactivate)(struct file *file);
418*4882a593Smuzhiyun 
419*4882a593Smuzhiyun 	ANDROID_KABI_RESERVE(1);
420*4882a593Smuzhiyun 	ANDROID_KABI_RESERVE(2);
421*4882a593Smuzhiyun 	ANDROID_KABI_RESERVE(3);
422*4882a593Smuzhiyun 	ANDROID_KABI_RESERVE(4);
423*4882a593Smuzhiyun };
424*4882a593Smuzhiyun 
425*4882a593Smuzhiyun extern const struct address_space_operations empty_aops;
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun /*
428*4882a593Smuzhiyun  * pagecache_write_begin/pagecache_write_end must be used by general code
429*4882a593Smuzhiyun  * to write into the pagecache.
430*4882a593Smuzhiyun  */
431*4882a593Smuzhiyun int pagecache_write_begin(struct file *, struct address_space *mapping,
432*4882a593Smuzhiyun 				loff_t pos, unsigned len, unsigned flags,
433*4882a593Smuzhiyun 				struct page **pagep, void **fsdata);
434*4882a593Smuzhiyun 
435*4882a593Smuzhiyun int pagecache_write_end(struct file *, struct address_space *mapping,
436*4882a593Smuzhiyun 				loff_t pos, unsigned len, unsigned copied,
437*4882a593Smuzhiyun 				struct page *page, void *fsdata);
438*4882a593Smuzhiyun 
439*4882a593Smuzhiyun /**
440*4882a593Smuzhiyun  * struct address_space - Contents of a cacheable, mappable object.
441*4882a593Smuzhiyun  * @host: Owner, either the inode or the block_device.
442*4882a593Smuzhiyun  * @i_pages: Cached pages.
443*4882a593Smuzhiyun  * @gfp_mask: Memory allocation flags to use for allocating pages.
444*4882a593Smuzhiyun  * @i_mmap_writable: Number of VM_SHARED mappings.
445*4882a593Smuzhiyun  * @nr_thps: Number of THPs in the pagecache (non-shmem only).
446*4882a593Smuzhiyun  * @i_mmap: Tree of private and shared mappings.
447*4882a593Smuzhiyun  * @i_mmap_rwsem: Protects @i_mmap and @i_mmap_writable.
448*4882a593Smuzhiyun  * @nrpages: Number of page entries, protected by the i_pages lock.
449*4882a593Smuzhiyun  * @nrexceptional: Shadow or DAX entries, protected by the i_pages lock.
450*4882a593Smuzhiyun  * @writeback_index: Writeback starts here.
451*4882a593Smuzhiyun  * @a_ops: Methods.
452*4882a593Smuzhiyun  * @flags: Error bits and flags (AS_*).
453*4882a593Smuzhiyun  * @wb_err: The most recent error which has occurred.
454*4882a593Smuzhiyun  * @private_lock: For use by the owner of the address_space.
455*4882a593Smuzhiyun  * @private_list: For use by the owner of the address_space.
456*4882a593Smuzhiyun  * @private_data: For use by the owner of the address_space.
457*4882a593Smuzhiyun  */
458*4882a593Smuzhiyun struct address_space {
459*4882a593Smuzhiyun 	struct inode		*host;
460*4882a593Smuzhiyun 	struct xarray		i_pages;
461*4882a593Smuzhiyun 	gfp_t			gfp_mask;
462*4882a593Smuzhiyun 	atomic_t		i_mmap_writable;
463*4882a593Smuzhiyun #ifdef CONFIG_READ_ONLY_THP_FOR_FS
464*4882a593Smuzhiyun 	/* number of thp, only for non-shmem files */
465*4882a593Smuzhiyun 	atomic_t		nr_thps;
466*4882a593Smuzhiyun #endif
467*4882a593Smuzhiyun 	struct rb_root_cached	i_mmap;
468*4882a593Smuzhiyun 	struct rw_semaphore	i_mmap_rwsem;
469*4882a593Smuzhiyun 	unsigned long		nrpages;
470*4882a593Smuzhiyun 	unsigned long		nrexceptional;
471*4882a593Smuzhiyun 	pgoff_t			writeback_index;
472*4882a593Smuzhiyun 	const struct address_space_operations *a_ops;
473*4882a593Smuzhiyun 	unsigned long		flags;
474*4882a593Smuzhiyun 	errseq_t		wb_err;
475*4882a593Smuzhiyun 	spinlock_t		private_lock;
476*4882a593Smuzhiyun 	struct list_head	private_list;
477*4882a593Smuzhiyun 	void			*private_data;
478*4882a593Smuzhiyun 
479*4882a593Smuzhiyun 	ANDROID_KABI_RESERVE(1);
480*4882a593Smuzhiyun 	ANDROID_KABI_RESERVE(2);
481*4882a593Smuzhiyun 	ANDROID_KABI_RESERVE(3);
482*4882a593Smuzhiyun 	ANDROID_KABI_RESERVE(4);
483*4882a593Smuzhiyun } __attribute__((aligned(sizeof(long)))) __randomize_layout;
484*4882a593Smuzhiyun 	/*
485*4882a593Smuzhiyun 	 * On most architectures that alignment is already the case; but
486*4882a593Smuzhiyun 	 * must be enforced here for CRIS, to let the least significant bit
487*4882a593Smuzhiyun 	 * of struct page's "mapping" pointer be used for PAGE_MAPPING_ANON.
488*4882a593Smuzhiyun 	 */
489*4882a593Smuzhiyun 
490*4882a593Smuzhiyun /* XArray tags, for tagging dirty and writeback pages in the pagecache. */
491*4882a593Smuzhiyun #define PAGECACHE_TAG_DIRTY	XA_MARK_0
492*4882a593Smuzhiyun #define PAGECACHE_TAG_WRITEBACK	XA_MARK_1
493*4882a593Smuzhiyun #define PAGECACHE_TAG_TOWRITE	XA_MARK_2
494*4882a593Smuzhiyun 
495*4882a593Smuzhiyun /*
496*4882a593Smuzhiyun  * Returns true if any of the pages in the mapping are marked with the tag.
497*4882a593Smuzhiyun  */
mapping_tagged(struct address_space * mapping,xa_mark_t tag)498*4882a593Smuzhiyun static inline bool mapping_tagged(struct address_space *mapping, xa_mark_t tag)
499*4882a593Smuzhiyun {
500*4882a593Smuzhiyun 	return xa_marked(&mapping->i_pages, tag);
501*4882a593Smuzhiyun }
502*4882a593Smuzhiyun 
i_mmap_lock_write(struct address_space * mapping)503*4882a593Smuzhiyun static inline void i_mmap_lock_write(struct address_space *mapping)
504*4882a593Smuzhiyun {
505*4882a593Smuzhiyun 	down_write(&mapping->i_mmap_rwsem);
506*4882a593Smuzhiyun }
507*4882a593Smuzhiyun 
i_mmap_trylock_write(struct address_space * mapping)508*4882a593Smuzhiyun static inline int i_mmap_trylock_write(struct address_space *mapping)
509*4882a593Smuzhiyun {
510*4882a593Smuzhiyun 	return down_write_trylock(&mapping->i_mmap_rwsem);
511*4882a593Smuzhiyun }
512*4882a593Smuzhiyun 
i_mmap_unlock_write(struct address_space * mapping)513*4882a593Smuzhiyun static inline void i_mmap_unlock_write(struct address_space *mapping)
514*4882a593Smuzhiyun {
515*4882a593Smuzhiyun 	up_write(&mapping->i_mmap_rwsem);
516*4882a593Smuzhiyun }
517*4882a593Smuzhiyun 
i_mmap_trylock_read(struct address_space * mapping)518*4882a593Smuzhiyun static inline int i_mmap_trylock_read(struct address_space *mapping)
519*4882a593Smuzhiyun {
520*4882a593Smuzhiyun 	return down_read_trylock(&mapping->i_mmap_rwsem);
521*4882a593Smuzhiyun }
522*4882a593Smuzhiyun 
i_mmap_lock_read(struct address_space * mapping)523*4882a593Smuzhiyun static inline void i_mmap_lock_read(struct address_space *mapping)
524*4882a593Smuzhiyun {
525*4882a593Smuzhiyun 	down_read(&mapping->i_mmap_rwsem);
526*4882a593Smuzhiyun }
527*4882a593Smuzhiyun 
i_mmap_unlock_read(struct address_space * mapping)528*4882a593Smuzhiyun static inline void i_mmap_unlock_read(struct address_space *mapping)
529*4882a593Smuzhiyun {
530*4882a593Smuzhiyun 	up_read(&mapping->i_mmap_rwsem);
531*4882a593Smuzhiyun }
532*4882a593Smuzhiyun 
i_mmap_assert_locked(struct address_space * mapping)533*4882a593Smuzhiyun static inline void i_mmap_assert_locked(struct address_space *mapping)
534*4882a593Smuzhiyun {
535*4882a593Smuzhiyun 	lockdep_assert_held(&mapping->i_mmap_rwsem);
536*4882a593Smuzhiyun }
537*4882a593Smuzhiyun 
i_mmap_assert_write_locked(struct address_space * mapping)538*4882a593Smuzhiyun static inline void i_mmap_assert_write_locked(struct address_space *mapping)
539*4882a593Smuzhiyun {
540*4882a593Smuzhiyun 	lockdep_assert_held_write(&mapping->i_mmap_rwsem);
541*4882a593Smuzhiyun }
542*4882a593Smuzhiyun 
543*4882a593Smuzhiyun /*
544*4882a593Smuzhiyun  * Might pages of this file be mapped into userspace?
545*4882a593Smuzhiyun  */
mapping_mapped(struct address_space * mapping)546*4882a593Smuzhiyun static inline int mapping_mapped(struct address_space *mapping)
547*4882a593Smuzhiyun {
548*4882a593Smuzhiyun 	return	!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root);
549*4882a593Smuzhiyun }
550*4882a593Smuzhiyun 
551*4882a593Smuzhiyun /*
552*4882a593Smuzhiyun  * Might pages of this file have been modified in userspace?
553*4882a593Smuzhiyun  * Note that i_mmap_writable counts all VM_SHARED vmas: do_mmap
554*4882a593Smuzhiyun  * marks vma as VM_SHARED if it is shared, and the file was opened for
555*4882a593Smuzhiyun  * writing i.e. vma may be mprotected writable even if now readonly.
556*4882a593Smuzhiyun  *
557*4882a593Smuzhiyun  * If i_mmap_writable is negative, no new writable mappings are allowed. You
558*4882a593Smuzhiyun  * can only deny writable mappings, if none exists right now.
559*4882a593Smuzhiyun  */
mapping_writably_mapped(struct address_space * mapping)560*4882a593Smuzhiyun static inline int mapping_writably_mapped(struct address_space *mapping)
561*4882a593Smuzhiyun {
562*4882a593Smuzhiyun 	return atomic_read(&mapping->i_mmap_writable) > 0;
563*4882a593Smuzhiyun }
564*4882a593Smuzhiyun 
mapping_map_writable(struct address_space * mapping)565*4882a593Smuzhiyun static inline int mapping_map_writable(struct address_space *mapping)
566*4882a593Smuzhiyun {
567*4882a593Smuzhiyun 	return atomic_inc_unless_negative(&mapping->i_mmap_writable) ?
568*4882a593Smuzhiyun 		0 : -EPERM;
569*4882a593Smuzhiyun }
570*4882a593Smuzhiyun 
mapping_unmap_writable(struct address_space * mapping)571*4882a593Smuzhiyun static inline void mapping_unmap_writable(struct address_space *mapping)
572*4882a593Smuzhiyun {
573*4882a593Smuzhiyun 	atomic_dec(&mapping->i_mmap_writable);
574*4882a593Smuzhiyun }
575*4882a593Smuzhiyun 
mapping_deny_writable(struct address_space * mapping)576*4882a593Smuzhiyun static inline int mapping_deny_writable(struct address_space *mapping)
577*4882a593Smuzhiyun {
578*4882a593Smuzhiyun 	return atomic_dec_unless_positive(&mapping->i_mmap_writable) ?
579*4882a593Smuzhiyun 		0 : -EBUSY;
580*4882a593Smuzhiyun }
581*4882a593Smuzhiyun 
mapping_allow_writable(struct address_space * mapping)582*4882a593Smuzhiyun static inline void mapping_allow_writable(struct address_space *mapping)
583*4882a593Smuzhiyun {
584*4882a593Smuzhiyun 	atomic_inc(&mapping->i_mmap_writable);
585*4882a593Smuzhiyun }
586*4882a593Smuzhiyun 
587*4882a593Smuzhiyun /*
588*4882a593Smuzhiyun  * Use sequence counter to get consistent i_size on 32-bit processors.
589*4882a593Smuzhiyun  */
590*4882a593Smuzhiyun #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
591*4882a593Smuzhiyun #include <linux/seqlock.h>
592*4882a593Smuzhiyun #define __NEED_I_SIZE_ORDERED
593*4882a593Smuzhiyun #define i_size_ordered_init(inode) seqcount_init(&inode->i_size_seqcount)
594*4882a593Smuzhiyun #else
595*4882a593Smuzhiyun #define i_size_ordered_init(inode) do { } while (0)
596*4882a593Smuzhiyun #endif
597*4882a593Smuzhiyun 
598*4882a593Smuzhiyun struct posix_acl;
599*4882a593Smuzhiyun #define ACL_NOT_CACHED ((void *)(-1))
600*4882a593Smuzhiyun #define ACL_DONT_CACHE ((void *)(-3))
601*4882a593Smuzhiyun 
602*4882a593Smuzhiyun static inline struct posix_acl *
uncached_acl_sentinel(struct task_struct * task)603*4882a593Smuzhiyun uncached_acl_sentinel(struct task_struct *task)
604*4882a593Smuzhiyun {
605*4882a593Smuzhiyun 	return (void *)task + 1;
606*4882a593Smuzhiyun }
607*4882a593Smuzhiyun 
608*4882a593Smuzhiyun static inline bool
is_uncached_acl(struct posix_acl * acl)609*4882a593Smuzhiyun is_uncached_acl(struct posix_acl *acl)
610*4882a593Smuzhiyun {
611*4882a593Smuzhiyun 	return (long)acl & 1;
612*4882a593Smuzhiyun }
613*4882a593Smuzhiyun 
614*4882a593Smuzhiyun #define IOP_FASTPERM	0x0001
615*4882a593Smuzhiyun #define IOP_LOOKUP	0x0002
616*4882a593Smuzhiyun #define IOP_NOFOLLOW	0x0004
617*4882a593Smuzhiyun #define IOP_XATTR	0x0008
618*4882a593Smuzhiyun #define IOP_DEFAULT_READLINK	0x0010
619*4882a593Smuzhiyun 
620*4882a593Smuzhiyun struct fsnotify_mark_connector;
621*4882a593Smuzhiyun 
622*4882a593Smuzhiyun /*
623*4882a593Smuzhiyun  * Keep mostly read-only and often accessed (especially for
624*4882a593Smuzhiyun  * the RCU path lookup and 'stat' data) fields at the beginning
625*4882a593Smuzhiyun  * of the 'struct inode'
626*4882a593Smuzhiyun  */
627*4882a593Smuzhiyun struct inode {
628*4882a593Smuzhiyun 	umode_t			i_mode;
629*4882a593Smuzhiyun 	unsigned short		i_opflags;
630*4882a593Smuzhiyun 	kuid_t			i_uid;
631*4882a593Smuzhiyun 	kgid_t			i_gid;
632*4882a593Smuzhiyun 	unsigned int		i_flags;
633*4882a593Smuzhiyun 
634*4882a593Smuzhiyun #ifdef CONFIG_FS_POSIX_ACL
635*4882a593Smuzhiyun 	struct posix_acl	*i_acl;
636*4882a593Smuzhiyun 	struct posix_acl	*i_default_acl;
637*4882a593Smuzhiyun #endif
638*4882a593Smuzhiyun 
639*4882a593Smuzhiyun 	const struct inode_operations	*i_op;
640*4882a593Smuzhiyun 	struct super_block	*i_sb;
641*4882a593Smuzhiyun 	struct address_space	*i_mapping;
642*4882a593Smuzhiyun 
643*4882a593Smuzhiyun #ifdef CONFIG_SECURITY
644*4882a593Smuzhiyun 	void			*i_security;
645*4882a593Smuzhiyun #endif
646*4882a593Smuzhiyun 
647*4882a593Smuzhiyun 	/* Stat data, not accessed from path walking */
648*4882a593Smuzhiyun 	unsigned long		i_ino;
649*4882a593Smuzhiyun 	/*
650*4882a593Smuzhiyun 	 * Filesystems may only read i_nlink directly.  They shall use the
651*4882a593Smuzhiyun 	 * following functions for modification:
652*4882a593Smuzhiyun 	 *
653*4882a593Smuzhiyun 	 *    (set|clear|inc|drop)_nlink
654*4882a593Smuzhiyun 	 *    inode_(inc|dec)_link_count
655*4882a593Smuzhiyun 	 */
656*4882a593Smuzhiyun 	union {
657*4882a593Smuzhiyun 		const unsigned int i_nlink;
658*4882a593Smuzhiyun 		unsigned int __i_nlink;
659*4882a593Smuzhiyun 	};
660*4882a593Smuzhiyun 	dev_t			i_rdev;
661*4882a593Smuzhiyun 	loff_t			i_size;
662*4882a593Smuzhiyun 	struct timespec64	i_atime;
663*4882a593Smuzhiyun 	struct timespec64	i_mtime;
664*4882a593Smuzhiyun 	struct timespec64	i_ctime;
665*4882a593Smuzhiyun 	spinlock_t		i_lock;	/* i_blocks, i_bytes, maybe i_size */
666*4882a593Smuzhiyun 	unsigned short          i_bytes;
667*4882a593Smuzhiyun 	u8			i_blkbits;
668*4882a593Smuzhiyun 	u8			i_write_hint;
669*4882a593Smuzhiyun 	blkcnt_t		i_blocks;
670*4882a593Smuzhiyun 
671*4882a593Smuzhiyun #ifdef __NEED_I_SIZE_ORDERED
672*4882a593Smuzhiyun 	seqcount_t		i_size_seqcount;
673*4882a593Smuzhiyun #endif
674*4882a593Smuzhiyun 
675*4882a593Smuzhiyun 	/* Misc */
676*4882a593Smuzhiyun 	unsigned long		i_state;
677*4882a593Smuzhiyun 	struct rw_semaphore	i_rwsem;
678*4882a593Smuzhiyun 
679*4882a593Smuzhiyun 	unsigned long		dirtied_when;	/* jiffies of first dirtying */
680*4882a593Smuzhiyun 	unsigned long		dirtied_time_when;
681*4882a593Smuzhiyun 
682*4882a593Smuzhiyun 	struct hlist_node	i_hash;
683*4882a593Smuzhiyun 	struct list_head	i_io_list;	/* backing dev IO list */
684*4882a593Smuzhiyun #ifdef CONFIG_CGROUP_WRITEBACK
685*4882a593Smuzhiyun 	struct bdi_writeback	*i_wb;		/* the associated cgroup wb */
686*4882a593Smuzhiyun 
687*4882a593Smuzhiyun 	/* foreign inode detection, see wbc_detach_inode() */
688*4882a593Smuzhiyun 	int			i_wb_frn_winner;
689*4882a593Smuzhiyun 	u16			i_wb_frn_avg_time;
690*4882a593Smuzhiyun 	u16			i_wb_frn_history;
691*4882a593Smuzhiyun #endif
692*4882a593Smuzhiyun 	struct list_head	i_lru;		/* inode LRU list */
693*4882a593Smuzhiyun 	struct list_head	i_sb_list;
694*4882a593Smuzhiyun 	struct list_head	i_wb_list;	/* backing dev writeback list */
695*4882a593Smuzhiyun 	union {
696*4882a593Smuzhiyun 		struct hlist_head	i_dentry;
697*4882a593Smuzhiyun 		struct rcu_head		i_rcu;
698*4882a593Smuzhiyun 	};
699*4882a593Smuzhiyun 	atomic64_t		i_version;
700*4882a593Smuzhiyun 	atomic64_t		i_sequence; /* see futex */
701*4882a593Smuzhiyun 	atomic_t		i_count;
702*4882a593Smuzhiyun 	atomic_t		i_dio_count;
703*4882a593Smuzhiyun 	atomic_t		i_writecount;
704*4882a593Smuzhiyun #if defined(CONFIG_IMA) || defined(CONFIG_FILE_LOCKING)
705*4882a593Smuzhiyun 	atomic_t		i_readcount; /* struct files open RO */
706*4882a593Smuzhiyun #endif
707*4882a593Smuzhiyun 	union {
708*4882a593Smuzhiyun 		const struct file_operations	*i_fop;	/* former ->i_op->default_file_ops */
709*4882a593Smuzhiyun 		void (*free_inode)(struct inode *);
710*4882a593Smuzhiyun 	};
711*4882a593Smuzhiyun 	struct file_lock_context	*i_flctx;
712*4882a593Smuzhiyun 	struct address_space	i_data;
713*4882a593Smuzhiyun 	struct list_head	i_devices;
714*4882a593Smuzhiyun 	union {
715*4882a593Smuzhiyun 		struct pipe_inode_info	*i_pipe;
716*4882a593Smuzhiyun 		struct block_device	*i_bdev;
717*4882a593Smuzhiyun 		struct cdev		*i_cdev;
718*4882a593Smuzhiyun 		char			*i_link;
719*4882a593Smuzhiyun 		unsigned		i_dir_seq;
720*4882a593Smuzhiyun 	};
721*4882a593Smuzhiyun 
722*4882a593Smuzhiyun 	__u32			i_generation;
723*4882a593Smuzhiyun 
724*4882a593Smuzhiyun #ifdef CONFIG_FSNOTIFY
725*4882a593Smuzhiyun 	__u32			i_fsnotify_mask; /* all events this inode cares about */
726*4882a593Smuzhiyun 	struct fsnotify_mark_connector __rcu	*i_fsnotify_marks;
727*4882a593Smuzhiyun #endif
728*4882a593Smuzhiyun 
729*4882a593Smuzhiyun #ifdef CONFIG_FS_ENCRYPTION
730*4882a593Smuzhiyun 	struct fscrypt_info	*i_crypt_info;
731*4882a593Smuzhiyun #endif
732*4882a593Smuzhiyun 
733*4882a593Smuzhiyun #ifdef CONFIG_FS_VERITY
734*4882a593Smuzhiyun 	struct fsverity_info	*i_verity_info;
735*4882a593Smuzhiyun #endif
736*4882a593Smuzhiyun 
737*4882a593Smuzhiyun 	void			*i_private; /* fs or device private pointer */
738*4882a593Smuzhiyun 
739*4882a593Smuzhiyun 	ANDROID_KABI_RESERVE(1);
740*4882a593Smuzhiyun 	ANDROID_KABI_RESERVE(2);
741*4882a593Smuzhiyun } __randomize_layout;
742*4882a593Smuzhiyun 
743*4882a593Smuzhiyun struct timespec64 timestamp_truncate(struct timespec64 t, struct inode *inode);
744*4882a593Smuzhiyun 
i_blocksize(const struct inode * node)745*4882a593Smuzhiyun static inline unsigned int i_blocksize(const struct inode *node)
746*4882a593Smuzhiyun {
747*4882a593Smuzhiyun 	return (1 << node->i_blkbits);
748*4882a593Smuzhiyun }
749*4882a593Smuzhiyun 
inode_unhashed(struct inode * inode)750*4882a593Smuzhiyun static inline int inode_unhashed(struct inode *inode)
751*4882a593Smuzhiyun {
752*4882a593Smuzhiyun 	return hlist_unhashed(&inode->i_hash);
753*4882a593Smuzhiyun }
754*4882a593Smuzhiyun 
755*4882a593Smuzhiyun /*
756*4882a593Smuzhiyun  * __mark_inode_dirty expects inodes to be hashed.  Since we don't
757*4882a593Smuzhiyun  * want special inodes in the fileset inode space, we make them
758*4882a593Smuzhiyun  * appear hashed, but do not put on any lists.  hlist_del()
759*4882a593Smuzhiyun  * will work fine and require no locking.
760*4882a593Smuzhiyun  */
inode_fake_hash(struct inode * inode)761*4882a593Smuzhiyun static inline void inode_fake_hash(struct inode *inode)
762*4882a593Smuzhiyun {
763*4882a593Smuzhiyun 	hlist_add_fake(&inode->i_hash);
764*4882a593Smuzhiyun }
765*4882a593Smuzhiyun 
766*4882a593Smuzhiyun /*
767*4882a593Smuzhiyun  * inode->i_mutex nesting subclasses for the lock validator:
768*4882a593Smuzhiyun  *
769*4882a593Smuzhiyun  * 0: the object of the current VFS operation
770*4882a593Smuzhiyun  * 1: parent
771*4882a593Smuzhiyun  * 2: child/target
772*4882a593Smuzhiyun  * 3: xattr
773*4882a593Smuzhiyun  * 4: second non-directory
774*4882a593Smuzhiyun  * 5: second parent (when locking independent directories in rename)
775*4882a593Smuzhiyun  *
776*4882a593Smuzhiyun  * I_MUTEX_NONDIR2 is for certain operations (such as rename) which lock two
777*4882a593Smuzhiyun  * non-directories at once.
778*4882a593Smuzhiyun  *
779*4882a593Smuzhiyun  * The locking order between these classes is
780*4882a593Smuzhiyun  * parent[2] -> child -> grandchild -> normal -> xattr -> second non-directory
781*4882a593Smuzhiyun  */
782*4882a593Smuzhiyun enum inode_i_mutex_lock_class
783*4882a593Smuzhiyun {
784*4882a593Smuzhiyun 	I_MUTEX_NORMAL,
785*4882a593Smuzhiyun 	I_MUTEX_PARENT,
786*4882a593Smuzhiyun 	I_MUTEX_CHILD,
787*4882a593Smuzhiyun 	I_MUTEX_XATTR,
788*4882a593Smuzhiyun 	I_MUTEX_NONDIR2,
789*4882a593Smuzhiyun 	I_MUTEX_PARENT2,
790*4882a593Smuzhiyun };
791*4882a593Smuzhiyun 
inode_lock(struct inode * inode)792*4882a593Smuzhiyun static inline void inode_lock(struct inode *inode)
793*4882a593Smuzhiyun {
794*4882a593Smuzhiyun 	down_write(&inode->i_rwsem);
795*4882a593Smuzhiyun }
796*4882a593Smuzhiyun 
inode_unlock(struct inode * inode)797*4882a593Smuzhiyun static inline void inode_unlock(struct inode *inode)
798*4882a593Smuzhiyun {
799*4882a593Smuzhiyun 	up_write(&inode->i_rwsem);
800*4882a593Smuzhiyun }
801*4882a593Smuzhiyun 
inode_lock_shared(struct inode * inode)802*4882a593Smuzhiyun static inline void inode_lock_shared(struct inode *inode)
803*4882a593Smuzhiyun {
804*4882a593Smuzhiyun 	down_read(&inode->i_rwsem);
805*4882a593Smuzhiyun }
806*4882a593Smuzhiyun 
inode_unlock_shared(struct inode * inode)807*4882a593Smuzhiyun static inline void inode_unlock_shared(struct inode *inode)
808*4882a593Smuzhiyun {
809*4882a593Smuzhiyun 	up_read(&inode->i_rwsem);
810*4882a593Smuzhiyun }
811*4882a593Smuzhiyun 
inode_trylock(struct inode * inode)812*4882a593Smuzhiyun static inline int inode_trylock(struct inode *inode)
813*4882a593Smuzhiyun {
814*4882a593Smuzhiyun 	return down_write_trylock(&inode->i_rwsem);
815*4882a593Smuzhiyun }
816*4882a593Smuzhiyun 
inode_trylock_shared(struct inode * inode)817*4882a593Smuzhiyun static inline int inode_trylock_shared(struct inode *inode)
818*4882a593Smuzhiyun {
819*4882a593Smuzhiyun 	return down_read_trylock(&inode->i_rwsem);
820*4882a593Smuzhiyun }
821*4882a593Smuzhiyun 
inode_is_locked(struct inode * inode)822*4882a593Smuzhiyun static inline int inode_is_locked(struct inode *inode)
823*4882a593Smuzhiyun {
824*4882a593Smuzhiyun 	return rwsem_is_locked(&inode->i_rwsem);
825*4882a593Smuzhiyun }
826*4882a593Smuzhiyun 
inode_lock_nested(struct inode * inode,unsigned subclass)827*4882a593Smuzhiyun static inline void inode_lock_nested(struct inode *inode, unsigned subclass)
828*4882a593Smuzhiyun {
829*4882a593Smuzhiyun 	down_write_nested(&inode->i_rwsem, subclass);
830*4882a593Smuzhiyun }
831*4882a593Smuzhiyun 
inode_lock_shared_nested(struct inode * inode,unsigned subclass)832*4882a593Smuzhiyun static inline void inode_lock_shared_nested(struct inode *inode, unsigned subclass)
833*4882a593Smuzhiyun {
834*4882a593Smuzhiyun 	down_read_nested(&inode->i_rwsem, subclass);
835*4882a593Smuzhiyun }
836*4882a593Smuzhiyun 
837*4882a593Smuzhiyun void lock_two_nondirectories(struct inode *, struct inode*);
838*4882a593Smuzhiyun void unlock_two_nondirectories(struct inode *, struct inode*);
839*4882a593Smuzhiyun 
840*4882a593Smuzhiyun /*
841*4882a593Smuzhiyun  * NOTE: in a 32bit arch with a preemptable kernel and
842*4882a593Smuzhiyun  * an UP compile the i_size_read/write must be atomic
843*4882a593Smuzhiyun  * with respect to the local cpu (unlike with preempt disabled),
844*4882a593Smuzhiyun  * but they don't need to be atomic with respect to other cpus like in
845*4882a593Smuzhiyun  * true SMP (so they need either to either locally disable irq around
846*4882a593Smuzhiyun  * the read or for example on x86 they can be still implemented as a
847*4882a593Smuzhiyun  * cmpxchg8b without the need of the lock prefix). For SMP compiles
848*4882a593Smuzhiyun  * and 64bit archs it makes no difference if preempt is enabled or not.
849*4882a593Smuzhiyun  */
i_size_read(const struct inode * inode)850*4882a593Smuzhiyun static inline loff_t i_size_read(const struct inode *inode)
851*4882a593Smuzhiyun {
852*4882a593Smuzhiyun #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
853*4882a593Smuzhiyun 	loff_t i_size;
854*4882a593Smuzhiyun 	unsigned int seq;
855*4882a593Smuzhiyun 
856*4882a593Smuzhiyun 	do {
857*4882a593Smuzhiyun 		seq = read_seqcount_begin(&inode->i_size_seqcount);
858*4882a593Smuzhiyun 		i_size = inode->i_size;
859*4882a593Smuzhiyun 	} while (read_seqcount_retry(&inode->i_size_seqcount, seq));
860*4882a593Smuzhiyun 	return i_size;
861*4882a593Smuzhiyun #elif BITS_PER_LONG==32 && defined(CONFIG_PREEMPTION)
862*4882a593Smuzhiyun 	loff_t i_size;
863*4882a593Smuzhiyun 
864*4882a593Smuzhiyun 	preempt_disable();
865*4882a593Smuzhiyun 	i_size = inode->i_size;
866*4882a593Smuzhiyun 	preempt_enable();
867*4882a593Smuzhiyun 	return i_size;
868*4882a593Smuzhiyun #else
869*4882a593Smuzhiyun 	return inode->i_size;
870*4882a593Smuzhiyun #endif
871*4882a593Smuzhiyun }
872*4882a593Smuzhiyun 
873*4882a593Smuzhiyun /*
874*4882a593Smuzhiyun  * NOTE: unlike i_size_read(), i_size_write() does need locking around it
875*4882a593Smuzhiyun  * (normally i_mutex), otherwise on 32bit/SMP an update of i_size_seqcount
876*4882a593Smuzhiyun  * can be lost, resulting in subsequent i_size_read() calls spinning forever.
877*4882a593Smuzhiyun  */
i_size_write(struct inode * inode,loff_t i_size)878*4882a593Smuzhiyun static inline void i_size_write(struct inode *inode, loff_t i_size)
879*4882a593Smuzhiyun {
880*4882a593Smuzhiyun #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
881*4882a593Smuzhiyun 	preempt_disable();
882*4882a593Smuzhiyun 	write_seqcount_begin(&inode->i_size_seqcount);
883*4882a593Smuzhiyun 	inode->i_size = i_size;
884*4882a593Smuzhiyun 	write_seqcount_end(&inode->i_size_seqcount);
885*4882a593Smuzhiyun 	preempt_enable();
886*4882a593Smuzhiyun #elif BITS_PER_LONG==32 && defined(CONFIG_PREEMPTION)
887*4882a593Smuzhiyun 	preempt_disable();
888*4882a593Smuzhiyun 	inode->i_size = i_size;
889*4882a593Smuzhiyun 	preempt_enable();
890*4882a593Smuzhiyun #else
891*4882a593Smuzhiyun 	inode->i_size = i_size;
892*4882a593Smuzhiyun #endif
893*4882a593Smuzhiyun }
894*4882a593Smuzhiyun 
iminor(const struct inode * inode)895*4882a593Smuzhiyun static inline unsigned iminor(const struct inode *inode)
896*4882a593Smuzhiyun {
897*4882a593Smuzhiyun 	return MINOR(inode->i_rdev);
898*4882a593Smuzhiyun }
899*4882a593Smuzhiyun 
imajor(const struct inode * inode)900*4882a593Smuzhiyun static inline unsigned imajor(const struct inode *inode)
901*4882a593Smuzhiyun {
902*4882a593Smuzhiyun 	return MAJOR(inode->i_rdev);
903*4882a593Smuzhiyun }
904*4882a593Smuzhiyun 
905*4882a593Smuzhiyun struct fown_struct {
906*4882a593Smuzhiyun 	rwlock_t lock;          /* protects pid, uid, euid fields */
907*4882a593Smuzhiyun 	struct pid *pid;	/* pid or -pgrp where SIGIO should be sent */
908*4882a593Smuzhiyun 	enum pid_type pid_type;	/* Kind of process group SIGIO should be sent to */
909*4882a593Smuzhiyun 	kuid_t uid, euid;	/* uid/euid of process setting the owner */
910*4882a593Smuzhiyun 	int signum;		/* posix.1b rt signal to be delivered on IO */
911*4882a593Smuzhiyun };
912*4882a593Smuzhiyun 
913*4882a593Smuzhiyun /*
914*4882a593Smuzhiyun  * Track a single file's readahead state
915*4882a593Smuzhiyun  */
916*4882a593Smuzhiyun struct file_ra_state {
917*4882a593Smuzhiyun 	pgoff_t start;			/* where readahead started */
918*4882a593Smuzhiyun 	unsigned int size;		/* # of readahead pages */
919*4882a593Smuzhiyun 	unsigned int async_size;	/* do asynchronous readahead when
920*4882a593Smuzhiyun 					   there are only # of pages ahead */
921*4882a593Smuzhiyun 
922*4882a593Smuzhiyun 	unsigned int ra_pages;		/* Maximum readahead window */
923*4882a593Smuzhiyun 	unsigned int mmap_miss;		/* Cache miss stat for mmap accesses */
924*4882a593Smuzhiyun 	loff_t prev_pos;		/* Cache last read() position */
925*4882a593Smuzhiyun };
926*4882a593Smuzhiyun 
927*4882a593Smuzhiyun /*
928*4882a593Smuzhiyun  * Check if @index falls in the readahead windows.
929*4882a593Smuzhiyun  */
ra_has_index(struct file_ra_state * ra,pgoff_t index)930*4882a593Smuzhiyun static inline int ra_has_index(struct file_ra_state *ra, pgoff_t index)
931*4882a593Smuzhiyun {
932*4882a593Smuzhiyun 	return (index >= ra->start &&
933*4882a593Smuzhiyun 		index <  ra->start + ra->size);
934*4882a593Smuzhiyun }
935*4882a593Smuzhiyun 
936*4882a593Smuzhiyun struct file {
937*4882a593Smuzhiyun 	union {
938*4882a593Smuzhiyun 		struct llist_node	fu_llist;
939*4882a593Smuzhiyun 		struct rcu_head 	fu_rcuhead;
940*4882a593Smuzhiyun 	} f_u;
941*4882a593Smuzhiyun 	struct path		f_path;
942*4882a593Smuzhiyun 	struct inode		*f_inode;	/* cached value */
943*4882a593Smuzhiyun 	const struct file_operations	*f_op;
944*4882a593Smuzhiyun 
945*4882a593Smuzhiyun 	/*
946*4882a593Smuzhiyun 	 * Protects f_ep_links, f_flags.
947*4882a593Smuzhiyun 	 * Must not be taken from IRQ context.
948*4882a593Smuzhiyun 	 */
949*4882a593Smuzhiyun 	spinlock_t		f_lock;
950*4882a593Smuzhiyun 	enum rw_hint		f_write_hint;
951*4882a593Smuzhiyun 	atomic_long_t		f_count;
952*4882a593Smuzhiyun 	unsigned int 		f_flags;
953*4882a593Smuzhiyun 	fmode_t			f_mode;
954*4882a593Smuzhiyun 	struct mutex		f_pos_lock;
955*4882a593Smuzhiyun 	loff_t			f_pos;
956*4882a593Smuzhiyun 	struct fown_struct	f_owner;
957*4882a593Smuzhiyun 	const struct cred	*f_cred;
958*4882a593Smuzhiyun 	struct file_ra_state	f_ra;
959*4882a593Smuzhiyun 
960*4882a593Smuzhiyun 	u64			f_version;
961*4882a593Smuzhiyun #ifdef CONFIG_SECURITY
962*4882a593Smuzhiyun 	void			*f_security;
963*4882a593Smuzhiyun #endif
964*4882a593Smuzhiyun 	/* needed for tty driver, and maybe others */
965*4882a593Smuzhiyun 	void			*private_data;
966*4882a593Smuzhiyun 
967*4882a593Smuzhiyun #ifdef CONFIG_EPOLL
968*4882a593Smuzhiyun 	/* Used by fs/eventpoll.c to link all the hooks to this file */
969*4882a593Smuzhiyun 	struct list_head	f_ep_links;
970*4882a593Smuzhiyun 	struct list_head	f_tfile_llink;
971*4882a593Smuzhiyun #endif /* #ifdef CONFIG_EPOLL */
972*4882a593Smuzhiyun 	struct address_space	*f_mapping;
973*4882a593Smuzhiyun 	errseq_t		f_wb_err;
974*4882a593Smuzhiyun 	errseq_t		f_sb_err; /* for syncfs */
975*4882a593Smuzhiyun 
976*4882a593Smuzhiyun 	ANDROID_KABI_RESERVE(1);
977*4882a593Smuzhiyun 	ANDROID_KABI_RESERVE(2);
978*4882a593Smuzhiyun 	ANDROID_OEM_DATA(1);
979*4882a593Smuzhiyun } __randomize_layout
980*4882a593Smuzhiyun   __attribute__((aligned(4)));	/* lest something weird decides that 2 is OK */
981*4882a593Smuzhiyun 
982*4882a593Smuzhiyun struct file_handle {
983*4882a593Smuzhiyun 	__u32 handle_bytes;
984*4882a593Smuzhiyun 	int handle_type;
985*4882a593Smuzhiyun 	/* file identifier */
986*4882a593Smuzhiyun 	unsigned char f_handle[];
987*4882a593Smuzhiyun };
988*4882a593Smuzhiyun 
get_file(struct file * f)989*4882a593Smuzhiyun static inline struct file *get_file(struct file *f)
990*4882a593Smuzhiyun {
991*4882a593Smuzhiyun 	atomic_long_inc(&f->f_count);
992*4882a593Smuzhiyun 	return f;
993*4882a593Smuzhiyun }
994*4882a593Smuzhiyun #define get_file_rcu_many(x, cnt)	\
995*4882a593Smuzhiyun 	atomic_long_add_unless(&(x)->f_count, (cnt), 0)
996*4882a593Smuzhiyun #define get_file_rcu(x) get_file_rcu_many((x), 1)
997*4882a593Smuzhiyun #define file_count(x)	atomic_long_read(&(x)->f_count)
998*4882a593Smuzhiyun 
999*4882a593Smuzhiyun #define	MAX_NON_LFS	((1UL<<31) - 1)
1000*4882a593Smuzhiyun 
1001*4882a593Smuzhiyun /* Page cache limit. The filesystems should put that into their s_maxbytes
1002*4882a593Smuzhiyun    limits, otherwise bad things can happen in VM. */
1003*4882a593Smuzhiyun #if BITS_PER_LONG==32
1004*4882a593Smuzhiyun #define MAX_LFS_FILESIZE	((loff_t)ULONG_MAX << PAGE_SHIFT)
1005*4882a593Smuzhiyun #elif BITS_PER_LONG==64
1006*4882a593Smuzhiyun #define MAX_LFS_FILESIZE 	((loff_t)LLONG_MAX)
1007*4882a593Smuzhiyun #endif
1008*4882a593Smuzhiyun 
1009*4882a593Smuzhiyun #define FL_POSIX	1
1010*4882a593Smuzhiyun #define FL_FLOCK	2
1011*4882a593Smuzhiyun #define FL_DELEG	4	/* NFSv4 delegation */
1012*4882a593Smuzhiyun #define FL_ACCESS	8	/* not trying to lock, just looking */
1013*4882a593Smuzhiyun #define FL_EXISTS	16	/* when unlocking, test for existence */
1014*4882a593Smuzhiyun #define FL_LEASE	32	/* lease held on this file */
1015*4882a593Smuzhiyun #define FL_CLOSE	64	/* unlock on close */
1016*4882a593Smuzhiyun #define FL_SLEEP	128	/* A blocking lock */
1017*4882a593Smuzhiyun #define FL_DOWNGRADE_PENDING	256 /* Lease is being downgraded */
1018*4882a593Smuzhiyun #define FL_UNLOCK_PENDING	512 /* Lease is being broken */
1019*4882a593Smuzhiyun #define FL_OFDLCK	1024	/* lock is "owned" by struct file */
1020*4882a593Smuzhiyun #define FL_LAYOUT	2048	/* outstanding pNFS layout */
1021*4882a593Smuzhiyun 
1022*4882a593Smuzhiyun #define FL_CLOSE_POSIX (FL_POSIX | FL_CLOSE)
1023*4882a593Smuzhiyun 
1024*4882a593Smuzhiyun /*
1025*4882a593Smuzhiyun  * Special return value from posix_lock_file() and vfs_lock_file() for
1026*4882a593Smuzhiyun  * asynchronous locking.
1027*4882a593Smuzhiyun  */
1028*4882a593Smuzhiyun #define FILE_LOCK_DEFERRED 1
1029*4882a593Smuzhiyun 
1030*4882a593Smuzhiyun /* legacy typedef, should eventually be removed */
1031*4882a593Smuzhiyun typedef void *fl_owner_t;
1032*4882a593Smuzhiyun 
1033*4882a593Smuzhiyun struct file_lock;
1034*4882a593Smuzhiyun 
1035*4882a593Smuzhiyun struct file_lock_operations {
1036*4882a593Smuzhiyun 	void (*fl_copy_lock)(struct file_lock *, struct file_lock *);
1037*4882a593Smuzhiyun 	void (*fl_release_private)(struct file_lock *);
1038*4882a593Smuzhiyun 
1039*4882a593Smuzhiyun 	ANDROID_KABI_RESERVE(1);
1040*4882a593Smuzhiyun 	ANDROID_KABI_RESERVE(2);
1041*4882a593Smuzhiyun };
1042*4882a593Smuzhiyun 
1043*4882a593Smuzhiyun struct lock_manager_operations {
1044*4882a593Smuzhiyun 	fl_owner_t (*lm_get_owner)(fl_owner_t);
1045*4882a593Smuzhiyun 	void (*lm_put_owner)(fl_owner_t);
1046*4882a593Smuzhiyun 	void (*lm_notify)(struct file_lock *);	/* unblock callback */
1047*4882a593Smuzhiyun 	int (*lm_grant)(struct file_lock *, int);
1048*4882a593Smuzhiyun 	bool (*lm_break)(struct file_lock *);
1049*4882a593Smuzhiyun 	int (*lm_change)(struct file_lock *, int, struct list_head *);
1050*4882a593Smuzhiyun 	void (*lm_setup)(struct file_lock *, void **);
1051*4882a593Smuzhiyun 	bool (*lm_breaker_owns_lease)(struct file_lock *);
1052*4882a593Smuzhiyun 
1053*4882a593Smuzhiyun 	ANDROID_KABI_RESERVE(1);
1054*4882a593Smuzhiyun 	ANDROID_KABI_RESERVE(2);
1055*4882a593Smuzhiyun };
1056*4882a593Smuzhiyun 
1057*4882a593Smuzhiyun struct lock_manager {
1058*4882a593Smuzhiyun 	struct list_head list;
1059*4882a593Smuzhiyun 	/*
1060*4882a593Smuzhiyun 	 * NFSv4 and up also want opens blocked during the grace period;
1061*4882a593Smuzhiyun 	 * NLM doesn't care:
1062*4882a593Smuzhiyun 	 */
1063*4882a593Smuzhiyun 	bool block_opens;
1064*4882a593Smuzhiyun };
1065*4882a593Smuzhiyun 
1066*4882a593Smuzhiyun struct net;
1067*4882a593Smuzhiyun void locks_start_grace(struct net *, struct lock_manager *);
1068*4882a593Smuzhiyun void locks_end_grace(struct lock_manager *);
1069*4882a593Smuzhiyun bool locks_in_grace(struct net *);
1070*4882a593Smuzhiyun bool opens_in_grace(struct net *);
1071*4882a593Smuzhiyun 
1072*4882a593Smuzhiyun /* that will die - we need it for nfs_lock_info */
1073*4882a593Smuzhiyun #include <linux/nfs_fs_i.h>
1074*4882a593Smuzhiyun 
1075*4882a593Smuzhiyun /*
1076*4882a593Smuzhiyun  * struct file_lock represents a generic "file lock". It's used to represent
1077*4882a593Smuzhiyun  * POSIX byte range locks, BSD (flock) locks, and leases. It's important to
1078*4882a593Smuzhiyun  * note that the same struct is used to represent both a request for a lock and
1079*4882a593Smuzhiyun  * the lock itself, but the same object is never used for both.
1080*4882a593Smuzhiyun  *
1081*4882a593Smuzhiyun  * FIXME: should we create a separate "struct lock_request" to help distinguish
1082*4882a593Smuzhiyun  * these two uses?
1083*4882a593Smuzhiyun  *
1084*4882a593Smuzhiyun  * The varous i_flctx lists are ordered by:
1085*4882a593Smuzhiyun  *
1086*4882a593Smuzhiyun  * 1) lock owner
1087*4882a593Smuzhiyun  * 2) lock range start
1088*4882a593Smuzhiyun  * 3) lock range end
1089*4882a593Smuzhiyun  *
1090*4882a593Smuzhiyun  * Obviously, the last two criteria only matter for POSIX locks.
1091*4882a593Smuzhiyun  */
1092*4882a593Smuzhiyun struct file_lock {
1093*4882a593Smuzhiyun 	struct file_lock *fl_blocker;	/* The lock, that is blocking us */
1094*4882a593Smuzhiyun 	struct list_head fl_list;	/* link into file_lock_context */
1095*4882a593Smuzhiyun 	struct hlist_node fl_link;	/* node in global lists */
1096*4882a593Smuzhiyun 	struct list_head fl_blocked_requests;	/* list of requests with
1097*4882a593Smuzhiyun 						 * ->fl_blocker pointing here
1098*4882a593Smuzhiyun 						 */
1099*4882a593Smuzhiyun 	struct list_head fl_blocked_member;	/* node in
1100*4882a593Smuzhiyun 						 * ->fl_blocker->fl_blocked_requests
1101*4882a593Smuzhiyun 						 */
1102*4882a593Smuzhiyun 	fl_owner_t fl_owner;
1103*4882a593Smuzhiyun 	unsigned int fl_flags;
1104*4882a593Smuzhiyun 	unsigned char fl_type;
1105*4882a593Smuzhiyun 	unsigned int fl_pid;
1106*4882a593Smuzhiyun 	int fl_link_cpu;		/* what cpu's list is this on? */
1107*4882a593Smuzhiyun 	wait_queue_head_t fl_wait;
1108*4882a593Smuzhiyun 	struct file *fl_file;
1109*4882a593Smuzhiyun 	loff_t fl_start;
1110*4882a593Smuzhiyun 	loff_t fl_end;
1111*4882a593Smuzhiyun 
1112*4882a593Smuzhiyun 	struct fasync_struct *	fl_fasync; /* for lease break notifications */
1113*4882a593Smuzhiyun 	/* for lease breaks: */
1114*4882a593Smuzhiyun 	unsigned long fl_break_time;
1115*4882a593Smuzhiyun 	unsigned long fl_downgrade_time;
1116*4882a593Smuzhiyun 
1117*4882a593Smuzhiyun 	const struct file_lock_operations *fl_ops;	/* Callbacks for filesystems */
1118*4882a593Smuzhiyun 	const struct lock_manager_operations *fl_lmops;	/* Callbacks for lockmanagers */
1119*4882a593Smuzhiyun 	union {
1120*4882a593Smuzhiyun 		struct nfs_lock_info	nfs_fl;
1121*4882a593Smuzhiyun 		struct nfs4_lock_info	nfs4_fl;
1122*4882a593Smuzhiyun 		struct {
1123*4882a593Smuzhiyun 			struct list_head link;	/* link in AFS vnode's pending_locks list */
1124*4882a593Smuzhiyun 			int state;		/* state of grant or error if -ve */
1125*4882a593Smuzhiyun 			unsigned int	debug_id;
1126*4882a593Smuzhiyun 		} afs;
1127*4882a593Smuzhiyun 	} fl_u;
1128*4882a593Smuzhiyun 
1129*4882a593Smuzhiyun 	struct list_head android_reserved1;	/* not a macro as we might just need it as-is */
1130*4882a593Smuzhiyun 	ANDROID_KABI_RESERVE(1);
1131*4882a593Smuzhiyun 	ANDROID_KABI_RESERVE(2);
1132*4882a593Smuzhiyun } __randomize_layout;
1133*4882a593Smuzhiyun 
1134*4882a593Smuzhiyun struct file_lock_context {
1135*4882a593Smuzhiyun 	spinlock_t		flc_lock;
1136*4882a593Smuzhiyun 	struct list_head	flc_flock;
1137*4882a593Smuzhiyun 	struct list_head	flc_posix;
1138*4882a593Smuzhiyun 	struct list_head	flc_lease;
1139*4882a593Smuzhiyun };
1140*4882a593Smuzhiyun 
1141*4882a593Smuzhiyun /* The following constant reflects the upper bound of the file/locking space */
1142*4882a593Smuzhiyun #ifndef OFFSET_MAX
1143*4882a593Smuzhiyun #define INT_LIMIT(x)	(~((x)1 << (sizeof(x)*8 - 1)))
1144*4882a593Smuzhiyun #define OFFSET_MAX	INT_LIMIT(loff_t)
1145*4882a593Smuzhiyun #define OFFT_OFFSET_MAX	INT_LIMIT(off_t)
1146*4882a593Smuzhiyun #endif
1147*4882a593Smuzhiyun 
1148*4882a593Smuzhiyun extern void send_sigio(struct fown_struct *fown, int fd, int band);
1149*4882a593Smuzhiyun 
1150*4882a593Smuzhiyun #define locks_inode(f) file_inode(f)
1151*4882a593Smuzhiyun 
1152*4882a593Smuzhiyun #ifdef CONFIG_FILE_LOCKING
1153*4882a593Smuzhiyun extern int fcntl_getlk(struct file *, unsigned int, struct flock *);
1154*4882a593Smuzhiyun extern int fcntl_setlk(unsigned int, struct file *, unsigned int,
1155*4882a593Smuzhiyun 			struct flock *);
1156*4882a593Smuzhiyun 
1157*4882a593Smuzhiyun #if BITS_PER_LONG == 32
1158*4882a593Smuzhiyun extern int fcntl_getlk64(struct file *, unsigned int, struct flock64 *);
1159*4882a593Smuzhiyun extern int fcntl_setlk64(unsigned int, struct file *, unsigned int,
1160*4882a593Smuzhiyun 			struct flock64 *);
1161*4882a593Smuzhiyun #endif
1162*4882a593Smuzhiyun 
1163*4882a593Smuzhiyun extern int fcntl_setlease(unsigned int fd, struct file *filp, long arg);
1164*4882a593Smuzhiyun extern int fcntl_getlease(struct file *filp);
1165*4882a593Smuzhiyun 
1166*4882a593Smuzhiyun /* fs/locks.c */
1167*4882a593Smuzhiyun void locks_free_lock_context(struct inode *inode);
1168*4882a593Smuzhiyun void locks_free_lock(struct file_lock *fl);
1169*4882a593Smuzhiyun extern void locks_init_lock(struct file_lock *);
1170*4882a593Smuzhiyun extern struct file_lock * locks_alloc_lock(void);
1171*4882a593Smuzhiyun extern void locks_copy_lock(struct file_lock *, struct file_lock *);
1172*4882a593Smuzhiyun extern void locks_copy_conflock(struct file_lock *, struct file_lock *);
1173*4882a593Smuzhiyun extern void locks_remove_posix(struct file *, fl_owner_t);
1174*4882a593Smuzhiyun extern void locks_remove_file(struct file *);
1175*4882a593Smuzhiyun extern void locks_release_private(struct file_lock *);
1176*4882a593Smuzhiyun extern void posix_test_lock(struct file *, struct file_lock *);
1177*4882a593Smuzhiyun extern int posix_lock_file(struct file *, struct file_lock *, struct file_lock *);
1178*4882a593Smuzhiyun extern int locks_delete_block(struct file_lock *);
1179*4882a593Smuzhiyun extern int vfs_test_lock(struct file *, struct file_lock *);
1180*4882a593Smuzhiyun extern int vfs_lock_file(struct file *, unsigned int, struct file_lock *, struct file_lock *);
1181*4882a593Smuzhiyun extern int vfs_cancel_lock(struct file *filp, struct file_lock *fl);
1182*4882a593Smuzhiyun extern int locks_lock_inode_wait(struct inode *inode, struct file_lock *fl);
1183*4882a593Smuzhiyun extern int __break_lease(struct inode *inode, unsigned int flags, unsigned int type);
1184*4882a593Smuzhiyun extern void lease_get_mtime(struct inode *, struct timespec64 *time);
1185*4882a593Smuzhiyun extern int generic_setlease(struct file *, long, struct file_lock **, void **priv);
1186*4882a593Smuzhiyun extern int vfs_setlease(struct file *, long, struct file_lock **, void **);
1187*4882a593Smuzhiyun extern int lease_modify(struct file_lock *, int, struct list_head *);
1188*4882a593Smuzhiyun 
1189*4882a593Smuzhiyun struct notifier_block;
1190*4882a593Smuzhiyun extern int lease_register_notifier(struct notifier_block *);
1191*4882a593Smuzhiyun extern void lease_unregister_notifier(struct notifier_block *);
1192*4882a593Smuzhiyun 
1193*4882a593Smuzhiyun struct files_struct;
1194*4882a593Smuzhiyun extern void show_fd_locks(struct seq_file *f,
1195*4882a593Smuzhiyun 			 struct file *filp, struct files_struct *files);
1196*4882a593Smuzhiyun #else /* !CONFIG_FILE_LOCKING */
fcntl_getlk(struct file * file,unsigned int cmd,struct flock __user * user)1197*4882a593Smuzhiyun static inline int fcntl_getlk(struct file *file, unsigned int cmd,
1198*4882a593Smuzhiyun 			      struct flock __user *user)
1199*4882a593Smuzhiyun {
1200*4882a593Smuzhiyun 	return -EINVAL;
1201*4882a593Smuzhiyun }
1202*4882a593Smuzhiyun 
fcntl_setlk(unsigned int fd,struct file * file,unsigned int cmd,struct flock __user * user)1203*4882a593Smuzhiyun static inline int fcntl_setlk(unsigned int fd, struct file *file,
1204*4882a593Smuzhiyun 			      unsigned int cmd, struct flock __user *user)
1205*4882a593Smuzhiyun {
1206*4882a593Smuzhiyun 	return -EACCES;
1207*4882a593Smuzhiyun }
1208*4882a593Smuzhiyun 
1209*4882a593Smuzhiyun #if BITS_PER_LONG == 32
fcntl_getlk64(struct file * file,unsigned int cmd,struct flock64 __user * user)1210*4882a593Smuzhiyun static inline int fcntl_getlk64(struct file *file, unsigned int cmd,
1211*4882a593Smuzhiyun 				struct flock64 __user *user)
1212*4882a593Smuzhiyun {
1213*4882a593Smuzhiyun 	return -EINVAL;
1214*4882a593Smuzhiyun }
1215*4882a593Smuzhiyun 
fcntl_setlk64(unsigned int fd,struct file * file,unsigned int cmd,struct flock64 __user * user)1216*4882a593Smuzhiyun static inline int fcntl_setlk64(unsigned int fd, struct file *file,
1217*4882a593Smuzhiyun 				unsigned int cmd, struct flock64 __user *user)
1218*4882a593Smuzhiyun {
1219*4882a593Smuzhiyun 	return -EACCES;
1220*4882a593Smuzhiyun }
1221*4882a593Smuzhiyun #endif
fcntl_setlease(unsigned int fd,struct file * filp,long arg)1222*4882a593Smuzhiyun static inline int fcntl_setlease(unsigned int fd, struct file *filp, long arg)
1223*4882a593Smuzhiyun {
1224*4882a593Smuzhiyun 	return -EINVAL;
1225*4882a593Smuzhiyun }
1226*4882a593Smuzhiyun 
fcntl_getlease(struct file * filp)1227*4882a593Smuzhiyun static inline int fcntl_getlease(struct file *filp)
1228*4882a593Smuzhiyun {
1229*4882a593Smuzhiyun 	return F_UNLCK;
1230*4882a593Smuzhiyun }
1231*4882a593Smuzhiyun 
1232*4882a593Smuzhiyun static inline void
locks_free_lock_context(struct inode * inode)1233*4882a593Smuzhiyun locks_free_lock_context(struct inode *inode)
1234*4882a593Smuzhiyun {
1235*4882a593Smuzhiyun }
1236*4882a593Smuzhiyun 
locks_init_lock(struct file_lock * fl)1237*4882a593Smuzhiyun static inline void locks_init_lock(struct file_lock *fl)
1238*4882a593Smuzhiyun {
1239*4882a593Smuzhiyun 	return;
1240*4882a593Smuzhiyun }
1241*4882a593Smuzhiyun 
locks_copy_conflock(struct file_lock * new,struct file_lock * fl)1242*4882a593Smuzhiyun static inline void locks_copy_conflock(struct file_lock *new, struct file_lock *fl)
1243*4882a593Smuzhiyun {
1244*4882a593Smuzhiyun 	return;
1245*4882a593Smuzhiyun }
1246*4882a593Smuzhiyun 
locks_copy_lock(struct file_lock * new,struct file_lock * fl)1247*4882a593Smuzhiyun static inline void locks_copy_lock(struct file_lock *new, struct file_lock *fl)
1248*4882a593Smuzhiyun {
1249*4882a593Smuzhiyun 	return;
1250*4882a593Smuzhiyun }
1251*4882a593Smuzhiyun 
locks_remove_posix(struct file * filp,fl_owner_t owner)1252*4882a593Smuzhiyun static inline void locks_remove_posix(struct file *filp, fl_owner_t owner)
1253*4882a593Smuzhiyun {
1254*4882a593Smuzhiyun 	return;
1255*4882a593Smuzhiyun }
1256*4882a593Smuzhiyun 
locks_remove_file(struct file * filp)1257*4882a593Smuzhiyun static inline void locks_remove_file(struct file *filp)
1258*4882a593Smuzhiyun {
1259*4882a593Smuzhiyun 	return;
1260*4882a593Smuzhiyun }
1261*4882a593Smuzhiyun 
posix_test_lock(struct file * filp,struct file_lock * fl)1262*4882a593Smuzhiyun static inline void posix_test_lock(struct file *filp, struct file_lock *fl)
1263*4882a593Smuzhiyun {
1264*4882a593Smuzhiyun 	return;
1265*4882a593Smuzhiyun }
1266*4882a593Smuzhiyun 
posix_lock_file(struct file * filp,struct file_lock * fl,struct file_lock * conflock)1267*4882a593Smuzhiyun static inline int posix_lock_file(struct file *filp, struct file_lock *fl,
1268*4882a593Smuzhiyun 				  struct file_lock *conflock)
1269*4882a593Smuzhiyun {
1270*4882a593Smuzhiyun 	return -ENOLCK;
1271*4882a593Smuzhiyun }
1272*4882a593Smuzhiyun 
locks_delete_block(struct file_lock * waiter)1273*4882a593Smuzhiyun static inline int locks_delete_block(struct file_lock *waiter)
1274*4882a593Smuzhiyun {
1275*4882a593Smuzhiyun 	return -ENOENT;
1276*4882a593Smuzhiyun }
1277*4882a593Smuzhiyun 
vfs_test_lock(struct file * filp,struct file_lock * fl)1278*4882a593Smuzhiyun static inline int vfs_test_lock(struct file *filp, struct file_lock *fl)
1279*4882a593Smuzhiyun {
1280*4882a593Smuzhiyun 	return 0;
1281*4882a593Smuzhiyun }
1282*4882a593Smuzhiyun 
vfs_lock_file(struct file * filp,unsigned int cmd,struct file_lock * fl,struct file_lock * conf)1283*4882a593Smuzhiyun static inline int vfs_lock_file(struct file *filp, unsigned int cmd,
1284*4882a593Smuzhiyun 				struct file_lock *fl, struct file_lock *conf)
1285*4882a593Smuzhiyun {
1286*4882a593Smuzhiyun 	return -ENOLCK;
1287*4882a593Smuzhiyun }
1288*4882a593Smuzhiyun 
vfs_cancel_lock(struct file * filp,struct file_lock * fl)1289*4882a593Smuzhiyun static inline int vfs_cancel_lock(struct file *filp, struct file_lock *fl)
1290*4882a593Smuzhiyun {
1291*4882a593Smuzhiyun 	return 0;
1292*4882a593Smuzhiyun }
1293*4882a593Smuzhiyun 
locks_lock_inode_wait(struct inode * inode,struct file_lock * fl)1294*4882a593Smuzhiyun static inline int locks_lock_inode_wait(struct inode *inode, struct file_lock *fl)
1295*4882a593Smuzhiyun {
1296*4882a593Smuzhiyun 	return -ENOLCK;
1297*4882a593Smuzhiyun }
1298*4882a593Smuzhiyun 
__break_lease(struct inode * inode,unsigned int mode,unsigned int type)1299*4882a593Smuzhiyun static inline int __break_lease(struct inode *inode, unsigned int mode, unsigned int type)
1300*4882a593Smuzhiyun {
1301*4882a593Smuzhiyun 	return 0;
1302*4882a593Smuzhiyun }
1303*4882a593Smuzhiyun 
lease_get_mtime(struct inode * inode,struct timespec64 * time)1304*4882a593Smuzhiyun static inline void lease_get_mtime(struct inode *inode,
1305*4882a593Smuzhiyun 				   struct timespec64 *time)
1306*4882a593Smuzhiyun {
1307*4882a593Smuzhiyun 	return;
1308*4882a593Smuzhiyun }
1309*4882a593Smuzhiyun 
generic_setlease(struct file * filp,long arg,struct file_lock ** flp,void ** priv)1310*4882a593Smuzhiyun static inline int generic_setlease(struct file *filp, long arg,
1311*4882a593Smuzhiyun 				    struct file_lock **flp, void **priv)
1312*4882a593Smuzhiyun {
1313*4882a593Smuzhiyun 	return -EINVAL;
1314*4882a593Smuzhiyun }
1315*4882a593Smuzhiyun 
vfs_setlease(struct file * filp,long arg,struct file_lock ** lease,void ** priv)1316*4882a593Smuzhiyun static inline int vfs_setlease(struct file *filp, long arg,
1317*4882a593Smuzhiyun 			       struct file_lock **lease, void **priv)
1318*4882a593Smuzhiyun {
1319*4882a593Smuzhiyun 	return -EINVAL;
1320*4882a593Smuzhiyun }
1321*4882a593Smuzhiyun 
lease_modify(struct file_lock * fl,int arg,struct list_head * dispose)1322*4882a593Smuzhiyun static inline int lease_modify(struct file_lock *fl, int arg,
1323*4882a593Smuzhiyun 			       struct list_head *dispose)
1324*4882a593Smuzhiyun {
1325*4882a593Smuzhiyun 	return -EINVAL;
1326*4882a593Smuzhiyun }
1327*4882a593Smuzhiyun 
1328*4882a593Smuzhiyun struct files_struct;
show_fd_locks(struct seq_file * f,struct file * filp,struct files_struct * files)1329*4882a593Smuzhiyun static inline void show_fd_locks(struct seq_file *f,
1330*4882a593Smuzhiyun 			struct file *filp, struct files_struct *files) {}
1331*4882a593Smuzhiyun #endif /* !CONFIG_FILE_LOCKING */
1332*4882a593Smuzhiyun 
file_inode(const struct file * f)1333*4882a593Smuzhiyun static inline struct inode *file_inode(const struct file *f)
1334*4882a593Smuzhiyun {
1335*4882a593Smuzhiyun 	return f->f_inode;
1336*4882a593Smuzhiyun }
1337*4882a593Smuzhiyun 
file_dentry(const struct file * file)1338*4882a593Smuzhiyun static inline struct dentry *file_dentry(const struct file *file)
1339*4882a593Smuzhiyun {
1340*4882a593Smuzhiyun 	return d_real(file->f_path.dentry, file_inode(file));
1341*4882a593Smuzhiyun }
1342*4882a593Smuzhiyun 
locks_lock_file_wait(struct file * filp,struct file_lock * fl)1343*4882a593Smuzhiyun static inline int locks_lock_file_wait(struct file *filp, struct file_lock *fl)
1344*4882a593Smuzhiyun {
1345*4882a593Smuzhiyun 	return locks_lock_inode_wait(locks_inode(filp), fl);
1346*4882a593Smuzhiyun }
1347*4882a593Smuzhiyun 
1348*4882a593Smuzhiyun struct fasync_struct {
1349*4882a593Smuzhiyun 	rwlock_t		fa_lock;
1350*4882a593Smuzhiyun 	int			magic;
1351*4882a593Smuzhiyun 	int			fa_fd;
1352*4882a593Smuzhiyun 	struct fasync_struct	*fa_next; /* singly linked list */
1353*4882a593Smuzhiyun 	struct file		*fa_file;
1354*4882a593Smuzhiyun 	struct rcu_head		fa_rcu;
1355*4882a593Smuzhiyun };
1356*4882a593Smuzhiyun 
1357*4882a593Smuzhiyun #define FASYNC_MAGIC 0x4601
1358*4882a593Smuzhiyun 
1359*4882a593Smuzhiyun /* SMP safe fasync helpers: */
1360*4882a593Smuzhiyun extern int fasync_helper(int, struct file *, int, struct fasync_struct **);
1361*4882a593Smuzhiyun extern struct fasync_struct *fasync_insert_entry(int, struct file *, struct fasync_struct **, struct fasync_struct *);
1362*4882a593Smuzhiyun extern int fasync_remove_entry(struct file *, struct fasync_struct **);
1363*4882a593Smuzhiyun extern struct fasync_struct *fasync_alloc(void);
1364*4882a593Smuzhiyun extern void fasync_free(struct fasync_struct *);
1365*4882a593Smuzhiyun 
1366*4882a593Smuzhiyun /* can be called from interrupts */
1367*4882a593Smuzhiyun extern void kill_fasync(struct fasync_struct **, int, int);
1368*4882a593Smuzhiyun 
1369*4882a593Smuzhiyun extern void __f_setown(struct file *filp, struct pid *, enum pid_type, int force);
1370*4882a593Smuzhiyun extern int f_setown(struct file *filp, unsigned long arg, int force);
1371*4882a593Smuzhiyun extern void f_delown(struct file *filp);
1372*4882a593Smuzhiyun extern pid_t f_getown(struct file *filp);
1373*4882a593Smuzhiyun extern int send_sigurg(struct fown_struct *fown);
1374*4882a593Smuzhiyun 
1375*4882a593Smuzhiyun /*
1376*4882a593Smuzhiyun  * sb->s_flags.  Note that these mirror the equivalent MS_* flags where
1377*4882a593Smuzhiyun  * represented in both.
1378*4882a593Smuzhiyun  */
1379*4882a593Smuzhiyun #define SB_RDONLY	 1	/* Mount read-only */
1380*4882a593Smuzhiyun #define SB_NOSUID	 2	/* Ignore suid and sgid bits */
1381*4882a593Smuzhiyun #define SB_NODEV	 4	/* Disallow access to device special files */
1382*4882a593Smuzhiyun #define SB_NOEXEC	 8	/* Disallow program execution */
1383*4882a593Smuzhiyun #define SB_SYNCHRONOUS	16	/* Writes are synced at once */
1384*4882a593Smuzhiyun #define SB_MANDLOCK	64	/* Allow mandatory locks on an FS */
1385*4882a593Smuzhiyun #define SB_DIRSYNC	128	/* Directory modifications are synchronous */
1386*4882a593Smuzhiyun #define SB_NOATIME	1024	/* Do not update access times. */
1387*4882a593Smuzhiyun #define SB_NODIRATIME	2048	/* Do not update directory access times */
1388*4882a593Smuzhiyun #define SB_SILENT	32768
1389*4882a593Smuzhiyun #define SB_POSIXACL	(1<<16)	/* VFS does not apply the umask */
1390*4882a593Smuzhiyun #define SB_INLINECRYPT	(1<<17)	/* Use blk-crypto for encrypted files */
1391*4882a593Smuzhiyun #define SB_KERNMOUNT	(1<<22) /* this is a kern_mount call */
1392*4882a593Smuzhiyun #define SB_I_VERSION	(1<<23) /* Update inode I_version field */
1393*4882a593Smuzhiyun #define SB_LAZYTIME	(1<<25) /* Update the on-disk [acm]times lazily */
1394*4882a593Smuzhiyun 
1395*4882a593Smuzhiyun /* These sb flags are internal to the kernel */
1396*4882a593Smuzhiyun #define SB_SUBMOUNT     (1<<26)
1397*4882a593Smuzhiyun #define SB_FORCE    	(1<<27)
1398*4882a593Smuzhiyun #define SB_NOSEC	(1<<28)
1399*4882a593Smuzhiyun #define SB_BORN		(1<<29)
1400*4882a593Smuzhiyun #define SB_ACTIVE	(1<<30)
1401*4882a593Smuzhiyun #define SB_NOUSER	(1<<31)
1402*4882a593Smuzhiyun 
1403*4882a593Smuzhiyun /* These flags relate to encoding and casefolding */
1404*4882a593Smuzhiyun #define SB_ENC_STRICT_MODE_FL	(1 << 0)
1405*4882a593Smuzhiyun 
1406*4882a593Smuzhiyun #define sb_has_strict_encoding(sb) \
1407*4882a593Smuzhiyun 	(sb->s_encoding_flags & SB_ENC_STRICT_MODE_FL)
1408*4882a593Smuzhiyun 
1409*4882a593Smuzhiyun /*
1410*4882a593Smuzhiyun  *	Umount options
1411*4882a593Smuzhiyun  */
1412*4882a593Smuzhiyun 
1413*4882a593Smuzhiyun #define MNT_FORCE	0x00000001	/* Attempt to forcibily umount */
1414*4882a593Smuzhiyun #define MNT_DETACH	0x00000002	/* Just detach from the tree */
1415*4882a593Smuzhiyun #define MNT_EXPIRE	0x00000004	/* Mark for expiry */
1416*4882a593Smuzhiyun #define UMOUNT_NOFOLLOW	0x00000008	/* Don't follow symlink on umount */
1417*4882a593Smuzhiyun #define UMOUNT_UNUSED	0x80000000	/* Flag guaranteed to be unused */
1418*4882a593Smuzhiyun 
1419*4882a593Smuzhiyun /* sb->s_iflags */
1420*4882a593Smuzhiyun #define SB_I_CGROUPWB	0x00000001	/* cgroup-aware writeback enabled */
1421*4882a593Smuzhiyun #define SB_I_NOEXEC	0x00000002	/* Ignore executables on this fs */
1422*4882a593Smuzhiyun #define SB_I_NODEV	0x00000004	/* Ignore devices on this fs */
1423*4882a593Smuzhiyun #define SB_I_STABLE_WRITES 0x00000008	/* don't modify blks until WB is done */
1424*4882a593Smuzhiyun 
1425*4882a593Smuzhiyun /* sb->s_iflags to limit user namespace mounts */
1426*4882a593Smuzhiyun #define SB_I_USERNS_VISIBLE		0x00000010 /* fstype already mounted */
1427*4882a593Smuzhiyun #define SB_I_IMA_UNVERIFIABLE_SIGNATURE	0x00000020
1428*4882a593Smuzhiyun #define SB_I_UNTRUSTED_MOUNTER		0x00000040
1429*4882a593Smuzhiyun 
1430*4882a593Smuzhiyun #define SB_I_SKIP_SYNC	0x00000100	/* Skip superblock at global sync */
1431*4882a593Smuzhiyun 
1432*4882a593Smuzhiyun /* Possible states of 'frozen' field */
1433*4882a593Smuzhiyun enum {
1434*4882a593Smuzhiyun 	SB_UNFROZEN = 0,		/* FS is unfrozen */
1435*4882a593Smuzhiyun 	SB_FREEZE_WRITE	= 1,		/* Writes, dir ops, ioctls frozen */
1436*4882a593Smuzhiyun 	SB_FREEZE_PAGEFAULT = 2,	/* Page faults stopped as well */
1437*4882a593Smuzhiyun 	SB_FREEZE_FS = 3,		/* For internal FS use (e.g. to stop
1438*4882a593Smuzhiyun 					 * internal threads if needed) */
1439*4882a593Smuzhiyun 	SB_FREEZE_COMPLETE = 4,		/* ->freeze_fs finished successfully */
1440*4882a593Smuzhiyun };
1441*4882a593Smuzhiyun 
1442*4882a593Smuzhiyun #define SB_FREEZE_LEVELS (SB_FREEZE_COMPLETE - 1)
1443*4882a593Smuzhiyun 
1444*4882a593Smuzhiyun struct sb_writers {
1445*4882a593Smuzhiyun 	int				frozen;		/* Is sb frozen? */
1446*4882a593Smuzhiyun 	wait_queue_head_t		wait_unfrozen;	/* for get_super_thawed() */
1447*4882a593Smuzhiyun 	struct percpu_rw_semaphore	rw_sem[SB_FREEZE_LEVELS];
1448*4882a593Smuzhiyun };
1449*4882a593Smuzhiyun 
1450*4882a593Smuzhiyun struct super_block {
1451*4882a593Smuzhiyun 	struct list_head	s_list;		/* Keep this first */
1452*4882a593Smuzhiyun 	dev_t			s_dev;		/* search index; _not_ kdev_t */
1453*4882a593Smuzhiyun 	unsigned char		s_blocksize_bits;
1454*4882a593Smuzhiyun 	unsigned long		s_blocksize;
1455*4882a593Smuzhiyun 	loff_t			s_maxbytes;	/* Max file size */
1456*4882a593Smuzhiyun 	struct file_system_type	*s_type;
1457*4882a593Smuzhiyun 	const struct super_operations	*s_op;
1458*4882a593Smuzhiyun 	const struct dquot_operations	*dq_op;
1459*4882a593Smuzhiyun 	const struct quotactl_ops	*s_qcop;
1460*4882a593Smuzhiyun 	const struct export_operations *s_export_op;
1461*4882a593Smuzhiyun 	unsigned long		s_flags;
1462*4882a593Smuzhiyun 	unsigned long		s_iflags;	/* internal SB_I_* flags */
1463*4882a593Smuzhiyun 	unsigned long		s_magic;
1464*4882a593Smuzhiyun 	struct dentry		*s_root;
1465*4882a593Smuzhiyun 	struct rw_semaphore	s_umount;
1466*4882a593Smuzhiyun 	int			s_count;
1467*4882a593Smuzhiyun 	atomic_t		s_active;
1468*4882a593Smuzhiyun #ifdef CONFIG_SECURITY
1469*4882a593Smuzhiyun 	void                    *s_security;
1470*4882a593Smuzhiyun #endif
1471*4882a593Smuzhiyun 	const struct xattr_handler **s_xattr;
1472*4882a593Smuzhiyun #ifdef CONFIG_FS_ENCRYPTION
1473*4882a593Smuzhiyun 	const struct fscrypt_operations	*s_cop;
1474*4882a593Smuzhiyun #ifdef __GENKSYMS__
1475*4882a593Smuzhiyun 	/*
1476*4882a593Smuzhiyun 	 * Android ABI CRC preservation due to commit 391cceee6d43 ("fscrypt:
1477*4882a593Smuzhiyun 	 * stop using keyrings subsystem for fscrypt_master_key") changing this
1478*4882a593Smuzhiyun 	 * type.  Size is the same, this is a private field.
1479*4882a593Smuzhiyun 	 */
1480*4882a593Smuzhiyun 	struct key		*s_master_keys; /* master crypto keys in use */
1481*4882a593Smuzhiyun #else
1482*4882a593Smuzhiyun 	struct fscrypt_keyring	*s_master_keys; /* master crypto keys in use */
1483*4882a593Smuzhiyun #endif
1484*4882a593Smuzhiyun #endif
1485*4882a593Smuzhiyun #ifdef CONFIG_FS_VERITY
1486*4882a593Smuzhiyun 	const struct fsverity_operations *s_vop;
1487*4882a593Smuzhiyun #endif
1488*4882a593Smuzhiyun #ifdef CONFIG_UNICODE
1489*4882a593Smuzhiyun 	struct unicode_map *s_encoding;
1490*4882a593Smuzhiyun 	__u16 s_encoding_flags;
1491*4882a593Smuzhiyun #endif
1492*4882a593Smuzhiyun 	struct hlist_bl_head	s_roots;	/* alternate root dentries for NFS */
1493*4882a593Smuzhiyun 	struct list_head	s_mounts;	/* list of mounts; _not_ for fs use */
1494*4882a593Smuzhiyun 	struct block_device	*s_bdev;
1495*4882a593Smuzhiyun 	struct backing_dev_info *s_bdi;
1496*4882a593Smuzhiyun 	struct mtd_info		*s_mtd;
1497*4882a593Smuzhiyun 	struct hlist_node	s_instances;
1498*4882a593Smuzhiyun 	unsigned int		s_quota_types;	/* Bitmask of supported quota types */
1499*4882a593Smuzhiyun 	struct quota_info	s_dquot;	/* Diskquota specific options */
1500*4882a593Smuzhiyun 
1501*4882a593Smuzhiyun 	struct sb_writers	s_writers;
1502*4882a593Smuzhiyun 
1503*4882a593Smuzhiyun 	/*
1504*4882a593Smuzhiyun 	 * Keep s_fs_info, s_time_gran, s_fsnotify_mask, and
1505*4882a593Smuzhiyun 	 * s_fsnotify_marks together for cache efficiency. They are frequently
1506*4882a593Smuzhiyun 	 * accessed and rarely modified.
1507*4882a593Smuzhiyun 	 */
1508*4882a593Smuzhiyun 	void			*s_fs_info;	/* Filesystem private info */
1509*4882a593Smuzhiyun 
1510*4882a593Smuzhiyun 	/* Granularity of c/m/atime in ns (cannot be worse than a second) */
1511*4882a593Smuzhiyun 	u32			s_time_gran;
1512*4882a593Smuzhiyun 	/* Time limits for c/m/atime in seconds */
1513*4882a593Smuzhiyun 	time64_t		   s_time_min;
1514*4882a593Smuzhiyun 	time64_t		   s_time_max;
1515*4882a593Smuzhiyun #ifdef CONFIG_FSNOTIFY
1516*4882a593Smuzhiyun 	__u32			s_fsnotify_mask;
1517*4882a593Smuzhiyun 	struct fsnotify_mark_connector __rcu	*s_fsnotify_marks;
1518*4882a593Smuzhiyun #endif
1519*4882a593Smuzhiyun 
1520*4882a593Smuzhiyun 	char			s_id[32];	/* Informational name */
1521*4882a593Smuzhiyun 	uuid_t			s_uuid;		/* UUID */
1522*4882a593Smuzhiyun 
1523*4882a593Smuzhiyun 	unsigned int		s_max_links;
1524*4882a593Smuzhiyun 	fmode_t			s_mode;
1525*4882a593Smuzhiyun 
1526*4882a593Smuzhiyun 	/*
1527*4882a593Smuzhiyun 	 * The next field is for VFS *only*. No filesystems have any business
1528*4882a593Smuzhiyun 	 * even looking at it. You had been warned.
1529*4882a593Smuzhiyun 	 */
1530*4882a593Smuzhiyun 	struct mutex s_vfs_rename_mutex;	/* Kludge */
1531*4882a593Smuzhiyun 
1532*4882a593Smuzhiyun 	/*
1533*4882a593Smuzhiyun 	 * Filesystem subtype.  If non-empty the filesystem type field
1534*4882a593Smuzhiyun 	 * in /proc/mounts will be "type.subtype"
1535*4882a593Smuzhiyun 	 */
1536*4882a593Smuzhiyun 	const char *s_subtype;
1537*4882a593Smuzhiyun 
1538*4882a593Smuzhiyun 	const struct dentry_operations *s_d_op; /* default d_op for dentries */
1539*4882a593Smuzhiyun 
1540*4882a593Smuzhiyun 	/*
1541*4882a593Smuzhiyun 	 * Saved pool identifier for cleancache (-1 means none)
1542*4882a593Smuzhiyun 	 */
1543*4882a593Smuzhiyun 	int cleancache_poolid;
1544*4882a593Smuzhiyun 
1545*4882a593Smuzhiyun 	struct shrinker s_shrink;	/* per-sb shrinker handle */
1546*4882a593Smuzhiyun 
1547*4882a593Smuzhiyun 	/* Number of inodes with nlink == 0 but still referenced */
1548*4882a593Smuzhiyun 	atomic_long_t s_remove_count;
1549*4882a593Smuzhiyun 
1550*4882a593Smuzhiyun 	/* Pending fsnotify inode refs */
1551*4882a593Smuzhiyun 	atomic_long_t s_fsnotify_inode_refs;
1552*4882a593Smuzhiyun 
1553*4882a593Smuzhiyun 	/* Being remounted read-only */
1554*4882a593Smuzhiyun 	int s_readonly_remount;
1555*4882a593Smuzhiyun 
1556*4882a593Smuzhiyun 	/* per-sb errseq_t for reporting writeback errors via syncfs */
1557*4882a593Smuzhiyun 	errseq_t s_wb_err;
1558*4882a593Smuzhiyun 
1559*4882a593Smuzhiyun 	/* AIO completions deferred from interrupt context */
1560*4882a593Smuzhiyun 	struct workqueue_struct *s_dio_done_wq;
1561*4882a593Smuzhiyun 	struct hlist_head s_pins;
1562*4882a593Smuzhiyun 
1563*4882a593Smuzhiyun 	/*
1564*4882a593Smuzhiyun 	 * Owning user namespace and default context in which to
1565*4882a593Smuzhiyun 	 * interpret filesystem uids, gids, quotas, device nodes,
1566*4882a593Smuzhiyun 	 * xattrs and security labels.
1567*4882a593Smuzhiyun 	 */
1568*4882a593Smuzhiyun 	struct user_namespace *s_user_ns;
1569*4882a593Smuzhiyun 
1570*4882a593Smuzhiyun 	/*
1571*4882a593Smuzhiyun 	 * The list_lru structure is essentially just a pointer to a table
1572*4882a593Smuzhiyun 	 * of per-node lru lists, each of which has its own spinlock.
1573*4882a593Smuzhiyun 	 * There is no need to put them into separate cachelines.
1574*4882a593Smuzhiyun 	 */
1575*4882a593Smuzhiyun 	struct list_lru		s_dentry_lru;
1576*4882a593Smuzhiyun 	struct list_lru		s_inode_lru;
1577*4882a593Smuzhiyun 	struct rcu_head		rcu;
1578*4882a593Smuzhiyun 	struct work_struct	destroy_work;
1579*4882a593Smuzhiyun 
1580*4882a593Smuzhiyun 	struct mutex		s_sync_lock;	/* sync serialisation lock */
1581*4882a593Smuzhiyun 
1582*4882a593Smuzhiyun 	/*
1583*4882a593Smuzhiyun 	 * Indicates how deep in a filesystem stack this SB is
1584*4882a593Smuzhiyun 	 */
1585*4882a593Smuzhiyun 	int s_stack_depth;
1586*4882a593Smuzhiyun 
1587*4882a593Smuzhiyun 	/* s_inode_list_lock protects s_inodes */
1588*4882a593Smuzhiyun 	spinlock_t		s_inode_list_lock ____cacheline_aligned_in_smp;
1589*4882a593Smuzhiyun 	struct list_head	s_inodes;	/* all inodes */
1590*4882a593Smuzhiyun 
1591*4882a593Smuzhiyun 	spinlock_t		s_inode_wblist_lock;
1592*4882a593Smuzhiyun 	struct list_head	s_inodes_wb;	/* writeback inodes */
1593*4882a593Smuzhiyun 
1594*4882a593Smuzhiyun 	ANDROID_KABI_RESERVE(1);
1595*4882a593Smuzhiyun 	ANDROID_KABI_RESERVE(2);
1596*4882a593Smuzhiyun 	ANDROID_KABI_RESERVE(3);
1597*4882a593Smuzhiyun 	ANDROID_KABI_RESERVE(4);
1598*4882a593Smuzhiyun } __randomize_layout;
1599*4882a593Smuzhiyun 
1600*4882a593Smuzhiyun /* Helper functions so that in most cases filesystems will
1601*4882a593Smuzhiyun  * not need to deal directly with kuid_t and kgid_t and can
1602*4882a593Smuzhiyun  * instead deal with the raw numeric values that are stored
1603*4882a593Smuzhiyun  * in the filesystem.
1604*4882a593Smuzhiyun  */
i_uid_read(const struct inode * inode)1605*4882a593Smuzhiyun static inline uid_t i_uid_read(const struct inode *inode)
1606*4882a593Smuzhiyun {
1607*4882a593Smuzhiyun 	return from_kuid(inode->i_sb->s_user_ns, inode->i_uid);
1608*4882a593Smuzhiyun }
1609*4882a593Smuzhiyun 
i_gid_read(const struct inode * inode)1610*4882a593Smuzhiyun static inline gid_t i_gid_read(const struct inode *inode)
1611*4882a593Smuzhiyun {
1612*4882a593Smuzhiyun 	return from_kgid(inode->i_sb->s_user_ns, inode->i_gid);
1613*4882a593Smuzhiyun }
1614*4882a593Smuzhiyun 
i_uid_write(struct inode * inode,uid_t uid)1615*4882a593Smuzhiyun static inline void i_uid_write(struct inode *inode, uid_t uid)
1616*4882a593Smuzhiyun {
1617*4882a593Smuzhiyun 	inode->i_uid = make_kuid(inode->i_sb->s_user_ns, uid);
1618*4882a593Smuzhiyun }
1619*4882a593Smuzhiyun 
i_gid_write(struct inode * inode,gid_t gid)1620*4882a593Smuzhiyun static inline void i_gid_write(struct inode *inode, gid_t gid)
1621*4882a593Smuzhiyun {
1622*4882a593Smuzhiyun 	inode->i_gid = make_kgid(inode->i_sb->s_user_ns, gid);
1623*4882a593Smuzhiyun }
1624*4882a593Smuzhiyun 
1625*4882a593Smuzhiyun extern struct timespec64 current_time(struct inode *inode);
1626*4882a593Smuzhiyun 
1627*4882a593Smuzhiyun /*
1628*4882a593Smuzhiyun  * Snapshotting support.
1629*4882a593Smuzhiyun  */
1630*4882a593Smuzhiyun 
1631*4882a593Smuzhiyun /*
1632*4882a593Smuzhiyun  * These are internal functions, please use sb_start_{write,pagefault,intwrite}
1633*4882a593Smuzhiyun  * instead.
1634*4882a593Smuzhiyun  */
__sb_end_write(struct super_block * sb,int level)1635*4882a593Smuzhiyun static inline void __sb_end_write(struct super_block *sb, int level)
1636*4882a593Smuzhiyun {
1637*4882a593Smuzhiyun 	percpu_up_read(sb->s_writers.rw_sem + level-1);
1638*4882a593Smuzhiyun }
1639*4882a593Smuzhiyun 
__sb_start_write(struct super_block * sb,int level)1640*4882a593Smuzhiyun static inline void __sb_start_write(struct super_block *sb, int level)
1641*4882a593Smuzhiyun {
1642*4882a593Smuzhiyun 	percpu_down_read(sb->s_writers.rw_sem + level - 1);
1643*4882a593Smuzhiyun }
1644*4882a593Smuzhiyun 
__sb_start_write_trylock(struct super_block * sb,int level)1645*4882a593Smuzhiyun static inline bool __sb_start_write_trylock(struct super_block *sb, int level)
1646*4882a593Smuzhiyun {
1647*4882a593Smuzhiyun 	return percpu_down_read_trylock(sb->s_writers.rw_sem + level - 1);
1648*4882a593Smuzhiyun }
1649*4882a593Smuzhiyun 
1650*4882a593Smuzhiyun #define __sb_writers_acquired(sb, lev)	\
1651*4882a593Smuzhiyun 	percpu_rwsem_acquire(&(sb)->s_writers.rw_sem[(lev)-1], 1, _THIS_IP_)
1652*4882a593Smuzhiyun #define __sb_writers_release(sb, lev)	\
1653*4882a593Smuzhiyun 	percpu_rwsem_release(&(sb)->s_writers.rw_sem[(lev)-1], 1, _THIS_IP_)
1654*4882a593Smuzhiyun 
1655*4882a593Smuzhiyun /**
1656*4882a593Smuzhiyun  * sb_end_write - drop write access to a superblock
1657*4882a593Smuzhiyun  * @sb: the super we wrote to
1658*4882a593Smuzhiyun  *
1659*4882a593Smuzhiyun  * Decrement number of writers to the filesystem. Wake up possible waiters
1660*4882a593Smuzhiyun  * wanting to freeze the filesystem.
1661*4882a593Smuzhiyun  */
sb_end_write(struct super_block * sb)1662*4882a593Smuzhiyun static inline void sb_end_write(struct super_block *sb)
1663*4882a593Smuzhiyun {
1664*4882a593Smuzhiyun 	__sb_end_write(sb, SB_FREEZE_WRITE);
1665*4882a593Smuzhiyun }
1666*4882a593Smuzhiyun 
1667*4882a593Smuzhiyun /**
1668*4882a593Smuzhiyun  * sb_end_pagefault - drop write access to a superblock from a page fault
1669*4882a593Smuzhiyun  * @sb: the super we wrote to
1670*4882a593Smuzhiyun  *
1671*4882a593Smuzhiyun  * Decrement number of processes handling write page fault to the filesystem.
1672*4882a593Smuzhiyun  * Wake up possible waiters wanting to freeze the filesystem.
1673*4882a593Smuzhiyun  */
sb_end_pagefault(struct super_block * sb)1674*4882a593Smuzhiyun static inline void sb_end_pagefault(struct super_block *sb)
1675*4882a593Smuzhiyun {
1676*4882a593Smuzhiyun 	__sb_end_write(sb, SB_FREEZE_PAGEFAULT);
1677*4882a593Smuzhiyun }
1678*4882a593Smuzhiyun 
1679*4882a593Smuzhiyun /**
1680*4882a593Smuzhiyun  * sb_end_intwrite - drop write access to a superblock for internal fs purposes
1681*4882a593Smuzhiyun  * @sb: the super we wrote to
1682*4882a593Smuzhiyun  *
1683*4882a593Smuzhiyun  * Decrement fs-internal number of writers to the filesystem.  Wake up possible
1684*4882a593Smuzhiyun  * waiters wanting to freeze the filesystem.
1685*4882a593Smuzhiyun  */
sb_end_intwrite(struct super_block * sb)1686*4882a593Smuzhiyun static inline void sb_end_intwrite(struct super_block *sb)
1687*4882a593Smuzhiyun {
1688*4882a593Smuzhiyun 	__sb_end_write(sb, SB_FREEZE_FS);
1689*4882a593Smuzhiyun }
1690*4882a593Smuzhiyun 
1691*4882a593Smuzhiyun /**
1692*4882a593Smuzhiyun  * sb_start_write - get write access to a superblock
1693*4882a593Smuzhiyun  * @sb: the super we write to
1694*4882a593Smuzhiyun  *
1695*4882a593Smuzhiyun  * When a process wants to write data or metadata to a file system (i.e. dirty
1696*4882a593Smuzhiyun  * a page or an inode), it should embed the operation in a sb_start_write() -
1697*4882a593Smuzhiyun  * sb_end_write() pair to get exclusion against file system freezing. This
1698*4882a593Smuzhiyun  * function increments number of writers preventing freezing. If the file
1699*4882a593Smuzhiyun  * system is already frozen, the function waits until the file system is
1700*4882a593Smuzhiyun  * thawed.
1701*4882a593Smuzhiyun  *
1702*4882a593Smuzhiyun  * Since freeze protection behaves as a lock, users have to preserve
1703*4882a593Smuzhiyun  * ordering of freeze protection and other filesystem locks. Generally,
1704*4882a593Smuzhiyun  * freeze protection should be the outermost lock. In particular, we have:
1705*4882a593Smuzhiyun  *
1706*4882a593Smuzhiyun  * sb_start_write
1707*4882a593Smuzhiyun  *   -> i_mutex			(write path, truncate, directory ops, ...)
1708*4882a593Smuzhiyun  *   -> s_umount		(freeze_super, thaw_super)
1709*4882a593Smuzhiyun  */
sb_start_write(struct super_block * sb)1710*4882a593Smuzhiyun static inline void sb_start_write(struct super_block *sb)
1711*4882a593Smuzhiyun {
1712*4882a593Smuzhiyun 	__sb_start_write(sb, SB_FREEZE_WRITE);
1713*4882a593Smuzhiyun }
1714*4882a593Smuzhiyun 
sb_start_write_trylock(struct super_block * sb)1715*4882a593Smuzhiyun static inline bool sb_start_write_trylock(struct super_block *sb)
1716*4882a593Smuzhiyun {
1717*4882a593Smuzhiyun 	return __sb_start_write_trylock(sb, SB_FREEZE_WRITE);
1718*4882a593Smuzhiyun }
1719*4882a593Smuzhiyun 
1720*4882a593Smuzhiyun /**
1721*4882a593Smuzhiyun  * sb_start_pagefault - get write access to a superblock from a page fault
1722*4882a593Smuzhiyun  * @sb: the super we write to
1723*4882a593Smuzhiyun  *
1724*4882a593Smuzhiyun  * When a process starts handling write page fault, it should embed the
1725*4882a593Smuzhiyun  * operation into sb_start_pagefault() - sb_end_pagefault() pair to get
1726*4882a593Smuzhiyun  * exclusion against file system freezing. This is needed since the page fault
1727*4882a593Smuzhiyun  * is going to dirty a page. This function increments number of running page
1728*4882a593Smuzhiyun  * faults preventing freezing. If the file system is already frozen, the
1729*4882a593Smuzhiyun  * function waits until the file system is thawed.
1730*4882a593Smuzhiyun  *
1731*4882a593Smuzhiyun  * Since page fault freeze protection behaves as a lock, users have to preserve
1732*4882a593Smuzhiyun  * ordering of freeze protection and other filesystem locks. It is advised to
1733*4882a593Smuzhiyun  * put sb_start_pagefault() close to mmap_lock in lock ordering. Page fault
1734*4882a593Smuzhiyun  * handling code implies lock dependency:
1735*4882a593Smuzhiyun  *
1736*4882a593Smuzhiyun  * mmap_lock
1737*4882a593Smuzhiyun  *   -> sb_start_pagefault
1738*4882a593Smuzhiyun  */
sb_start_pagefault(struct super_block * sb)1739*4882a593Smuzhiyun static inline void sb_start_pagefault(struct super_block *sb)
1740*4882a593Smuzhiyun {
1741*4882a593Smuzhiyun 	__sb_start_write(sb, SB_FREEZE_PAGEFAULT);
1742*4882a593Smuzhiyun }
1743*4882a593Smuzhiyun 
1744*4882a593Smuzhiyun /*
1745*4882a593Smuzhiyun  * sb_start_intwrite - get write access to a superblock for internal fs purposes
1746*4882a593Smuzhiyun  * @sb: the super we write to
1747*4882a593Smuzhiyun  *
1748*4882a593Smuzhiyun  * This is the third level of protection against filesystem freezing. It is
1749*4882a593Smuzhiyun  * free for use by a filesystem. The only requirement is that it must rank
1750*4882a593Smuzhiyun  * below sb_start_pagefault.
1751*4882a593Smuzhiyun  *
1752*4882a593Smuzhiyun  * For example filesystem can call sb_start_intwrite() when starting a
1753*4882a593Smuzhiyun  * transaction which somewhat eases handling of freezing for internal sources
1754*4882a593Smuzhiyun  * of filesystem changes (internal fs threads, discarding preallocation on file
1755*4882a593Smuzhiyun  * close, etc.).
1756*4882a593Smuzhiyun  */
sb_start_intwrite(struct super_block * sb)1757*4882a593Smuzhiyun static inline void sb_start_intwrite(struct super_block *sb)
1758*4882a593Smuzhiyun {
1759*4882a593Smuzhiyun 	__sb_start_write(sb, SB_FREEZE_FS);
1760*4882a593Smuzhiyun }
1761*4882a593Smuzhiyun 
sb_start_intwrite_trylock(struct super_block * sb)1762*4882a593Smuzhiyun static inline bool sb_start_intwrite_trylock(struct super_block *sb)
1763*4882a593Smuzhiyun {
1764*4882a593Smuzhiyun 	return __sb_start_write_trylock(sb, SB_FREEZE_FS);
1765*4882a593Smuzhiyun }
1766*4882a593Smuzhiyun 
1767*4882a593Smuzhiyun 
1768*4882a593Smuzhiyun extern bool inode_owner_or_capable(const struct inode *inode);
1769*4882a593Smuzhiyun 
1770*4882a593Smuzhiyun /*
1771*4882a593Smuzhiyun  * VFS helper functions..
1772*4882a593Smuzhiyun  */
1773*4882a593Smuzhiyun extern int vfs_create(struct inode *, struct dentry *, umode_t, bool);
1774*4882a593Smuzhiyun extern int vfs_mkdir(struct inode *, struct dentry *, umode_t);
1775*4882a593Smuzhiyun extern int vfs_mknod(struct inode *, struct dentry *, umode_t, dev_t);
1776*4882a593Smuzhiyun extern int vfs_symlink(struct inode *, struct dentry *, const char *);
1777*4882a593Smuzhiyun extern int vfs_link(struct dentry *, struct inode *, struct dentry *, struct inode **);
1778*4882a593Smuzhiyun extern int vfs_rmdir(struct inode *, struct dentry *);
1779*4882a593Smuzhiyun extern int vfs_unlink(struct inode *, struct dentry *, struct inode **);
1780*4882a593Smuzhiyun extern int vfs_rename(struct inode *, struct dentry *, struct inode *, struct dentry *, struct inode **, unsigned int);
1781*4882a593Smuzhiyun 
vfs_whiteout(struct inode * dir,struct dentry * dentry)1782*4882a593Smuzhiyun static inline int vfs_whiteout(struct inode *dir, struct dentry *dentry)
1783*4882a593Smuzhiyun {
1784*4882a593Smuzhiyun 	return vfs_mknod(dir, dentry, S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
1785*4882a593Smuzhiyun }
1786*4882a593Smuzhiyun 
1787*4882a593Smuzhiyun extern struct dentry *vfs_tmpfile(struct dentry *dentry, umode_t mode,
1788*4882a593Smuzhiyun 				  int open_flag);
1789*4882a593Smuzhiyun 
1790*4882a593Smuzhiyun int vfs_mkobj(struct dentry *, umode_t,
1791*4882a593Smuzhiyun 		int (*f)(struct dentry *, umode_t, void *),
1792*4882a593Smuzhiyun 		void *);
1793*4882a593Smuzhiyun 
1794*4882a593Smuzhiyun int vfs_fchown(struct file *file, uid_t user, gid_t group);
1795*4882a593Smuzhiyun int vfs_fchmod(struct file *file, umode_t mode);
1796*4882a593Smuzhiyun int vfs_utimes(const struct path *path, struct timespec64 *times);
1797*4882a593Smuzhiyun 
1798*4882a593Smuzhiyun extern long vfs_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
1799*4882a593Smuzhiyun 
1800*4882a593Smuzhiyun #ifdef CONFIG_COMPAT
1801*4882a593Smuzhiyun extern long compat_ptr_ioctl(struct file *file, unsigned int cmd,
1802*4882a593Smuzhiyun 					unsigned long arg);
1803*4882a593Smuzhiyun #else
1804*4882a593Smuzhiyun #define compat_ptr_ioctl NULL
1805*4882a593Smuzhiyun #endif
1806*4882a593Smuzhiyun 
1807*4882a593Smuzhiyun /*
1808*4882a593Smuzhiyun  * VFS file helper functions.
1809*4882a593Smuzhiyun  */
1810*4882a593Smuzhiyun extern void inode_init_owner(struct inode *inode, const struct inode *dir,
1811*4882a593Smuzhiyun 			umode_t mode);
1812*4882a593Smuzhiyun extern bool may_open_dev(const struct path *path);
1813*4882a593Smuzhiyun 
1814*4882a593Smuzhiyun /*
1815*4882a593Smuzhiyun  * This is the "filldir" function type, used by readdir() to let
1816*4882a593Smuzhiyun  * the kernel specify what kind of dirent layout it wants to have.
1817*4882a593Smuzhiyun  * This allows the kernel to read directories into kernel space or
1818*4882a593Smuzhiyun  * to have different dirent layouts depending on the binary type.
1819*4882a593Smuzhiyun  */
1820*4882a593Smuzhiyun struct dir_context;
1821*4882a593Smuzhiyun typedef int (*filldir_t)(struct dir_context *, const char *, int, loff_t, u64,
1822*4882a593Smuzhiyun 			 unsigned);
1823*4882a593Smuzhiyun 
1824*4882a593Smuzhiyun struct dir_context {
1825*4882a593Smuzhiyun 	filldir_t actor;
1826*4882a593Smuzhiyun 	loff_t pos;
1827*4882a593Smuzhiyun };
1828*4882a593Smuzhiyun 
1829*4882a593Smuzhiyun /*
1830*4882a593Smuzhiyun  * These flags let !MMU mmap() govern direct device mapping vs immediate
1831*4882a593Smuzhiyun  * copying more easily for MAP_PRIVATE, especially for ROM filesystems.
1832*4882a593Smuzhiyun  *
1833*4882a593Smuzhiyun  * NOMMU_MAP_COPY:	Copy can be mapped (MAP_PRIVATE)
1834*4882a593Smuzhiyun  * NOMMU_MAP_DIRECT:	Can be mapped directly (MAP_SHARED)
1835*4882a593Smuzhiyun  * NOMMU_MAP_READ:	Can be mapped for reading
1836*4882a593Smuzhiyun  * NOMMU_MAP_WRITE:	Can be mapped for writing
1837*4882a593Smuzhiyun  * NOMMU_MAP_EXEC:	Can be mapped for execution
1838*4882a593Smuzhiyun  */
1839*4882a593Smuzhiyun #define NOMMU_MAP_COPY		0x00000001
1840*4882a593Smuzhiyun #define NOMMU_MAP_DIRECT	0x00000008
1841*4882a593Smuzhiyun #define NOMMU_MAP_READ		VM_MAYREAD
1842*4882a593Smuzhiyun #define NOMMU_MAP_WRITE		VM_MAYWRITE
1843*4882a593Smuzhiyun #define NOMMU_MAP_EXEC		VM_MAYEXEC
1844*4882a593Smuzhiyun 
1845*4882a593Smuzhiyun #define NOMMU_VMFLAGS \
1846*4882a593Smuzhiyun 	(NOMMU_MAP_READ | NOMMU_MAP_WRITE | NOMMU_MAP_EXEC)
1847*4882a593Smuzhiyun 
1848*4882a593Smuzhiyun /*
1849*4882a593Smuzhiyun  * These flags control the behavior of the remap_file_range function pointer.
1850*4882a593Smuzhiyun  * If it is called with len == 0 that means "remap to end of source file".
1851*4882a593Smuzhiyun  * See Documentation/filesystems/vfs.rst for more details about this call.
1852*4882a593Smuzhiyun  *
1853*4882a593Smuzhiyun  * REMAP_FILE_DEDUP: only remap if contents identical (i.e. deduplicate)
1854*4882a593Smuzhiyun  * REMAP_FILE_CAN_SHORTEN: caller can handle a shortened request
1855*4882a593Smuzhiyun  */
1856*4882a593Smuzhiyun #define REMAP_FILE_DEDUP		(1 << 0)
1857*4882a593Smuzhiyun #define REMAP_FILE_CAN_SHORTEN		(1 << 1)
1858*4882a593Smuzhiyun 
1859*4882a593Smuzhiyun /*
1860*4882a593Smuzhiyun  * These flags signal that the caller is ok with altering various aspects of
1861*4882a593Smuzhiyun  * the behavior of the remap operation.  The changes must be made by the
1862*4882a593Smuzhiyun  * implementation; the vfs remap helper functions can take advantage of them.
1863*4882a593Smuzhiyun  * Flags in this category exist to preserve the quirky behavior of the hoisted
1864*4882a593Smuzhiyun  * btrfs clone/dedupe ioctls.
1865*4882a593Smuzhiyun  */
1866*4882a593Smuzhiyun #define REMAP_FILE_ADVISORY		(REMAP_FILE_CAN_SHORTEN)
1867*4882a593Smuzhiyun 
1868*4882a593Smuzhiyun /*
1869*4882a593Smuzhiyun  * These flags control the behavior of vfs_copy_file_range().
1870*4882a593Smuzhiyun  * They are not available to the user via syscall.
1871*4882a593Smuzhiyun  *
1872*4882a593Smuzhiyun  * COPY_FILE_SPLICE: call splice direct instead of fs clone/copy ops
1873*4882a593Smuzhiyun  */
1874*4882a593Smuzhiyun #define COPY_FILE_SPLICE		(1 << 0)
1875*4882a593Smuzhiyun 
1876*4882a593Smuzhiyun struct iov_iter;
1877*4882a593Smuzhiyun 
1878*4882a593Smuzhiyun struct file_operations {
1879*4882a593Smuzhiyun 	struct module *owner;
1880*4882a593Smuzhiyun 	loff_t (*llseek) (struct file *, loff_t, int);
1881*4882a593Smuzhiyun 	ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
1882*4882a593Smuzhiyun 	ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
1883*4882a593Smuzhiyun 	ssize_t (*read_iter) (struct kiocb *, struct iov_iter *);
1884*4882a593Smuzhiyun 	ssize_t (*write_iter) (struct kiocb *, struct iov_iter *);
1885*4882a593Smuzhiyun 	int (*iopoll)(struct kiocb *kiocb, bool spin);
1886*4882a593Smuzhiyun 	int (*iterate) (struct file *, struct dir_context *);
1887*4882a593Smuzhiyun 	int (*iterate_shared) (struct file *, struct dir_context *);
1888*4882a593Smuzhiyun 	__poll_t (*poll) (struct file *, struct poll_table_struct *);
1889*4882a593Smuzhiyun 	long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
1890*4882a593Smuzhiyun 	long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
1891*4882a593Smuzhiyun 	int (*mmap) (struct file *, struct vm_area_struct *);
1892*4882a593Smuzhiyun 	unsigned long mmap_supported_flags;
1893*4882a593Smuzhiyun 	int (*open) (struct inode *, struct file *);
1894*4882a593Smuzhiyun 	int (*flush) (struct file *, fl_owner_t id);
1895*4882a593Smuzhiyun 	int (*release) (struct inode *, struct file *);
1896*4882a593Smuzhiyun 	int (*fsync) (struct file *, loff_t, loff_t, int datasync);
1897*4882a593Smuzhiyun 	int (*fasync) (int, struct file *, int);
1898*4882a593Smuzhiyun 	int (*lock) (struct file *, int, struct file_lock *);
1899*4882a593Smuzhiyun 	ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
1900*4882a593Smuzhiyun 	unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
1901*4882a593Smuzhiyun 	int (*check_flags)(int);
1902*4882a593Smuzhiyun 	int (*flock) (struct file *, int, struct file_lock *);
1903*4882a593Smuzhiyun 	ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
1904*4882a593Smuzhiyun 	ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
1905*4882a593Smuzhiyun 	int (*setlease)(struct file *, long, struct file_lock **, void **);
1906*4882a593Smuzhiyun 	long (*fallocate)(struct file *file, int mode, loff_t offset,
1907*4882a593Smuzhiyun 			  loff_t len);
1908*4882a593Smuzhiyun 	void (*show_fdinfo)(struct seq_file *m, struct file *f);
1909*4882a593Smuzhiyun #ifndef CONFIG_MMU
1910*4882a593Smuzhiyun 	unsigned (*mmap_capabilities)(struct file *);
1911*4882a593Smuzhiyun #endif
1912*4882a593Smuzhiyun 	ssize_t (*copy_file_range)(struct file *, loff_t, struct file *,
1913*4882a593Smuzhiyun 			loff_t, size_t, unsigned int);
1914*4882a593Smuzhiyun 	loff_t (*remap_file_range)(struct file *file_in, loff_t pos_in,
1915*4882a593Smuzhiyun 				   struct file *file_out, loff_t pos_out,
1916*4882a593Smuzhiyun 				   loff_t len, unsigned int remap_flags);
1917*4882a593Smuzhiyun 	int (*fadvise)(struct file *, loff_t, loff_t, int);
1918*4882a593Smuzhiyun 
1919*4882a593Smuzhiyun 	ANDROID_KABI_RESERVE(1);
1920*4882a593Smuzhiyun 	ANDROID_KABI_RESERVE(2);
1921*4882a593Smuzhiyun 	ANDROID_KABI_RESERVE(3);
1922*4882a593Smuzhiyun 	ANDROID_KABI_RESERVE(4);
1923*4882a593Smuzhiyun } __randomize_layout;
1924*4882a593Smuzhiyun 
1925*4882a593Smuzhiyun struct inode_operations {
1926*4882a593Smuzhiyun 	struct dentry * (*lookup) (struct inode *,struct dentry *, unsigned int);
1927*4882a593Smuzhiyun 	const char * (*get_link) (struct dentry *, struct inode *, struct delayed_call *);
1928*4882a593Smuzhiyun 	int (*permission) (struct inode *, int);
1929*4882a593Smuzhiyun 	struct posix_acl * (*get_acl)(struct inode *, int);
1930*4882a593Smuzhiyun 
1931*4882a593Smuzhiyun 	int (*readlink) (struct dentry *, char __user *,int);
1932*4882a593Smuzhiyun 
1933*4882a593Smuzhiyun 	int (*create) (struct inode *,struct dentry *, umode_t, bool);
1934*4882a593Smuzhiyun 	int (*link) (struct dentry *,struct inode *,struct dentry *);
1935*4882a593Smuzhiyun 	int (*unlink) (struct inode *,struct dentry *);
1936*4882a593Smuzhiyun 	int (*symlink) (struct inode *,struct dentry *,const char *);
1937*4882a593Smuzhiyun 	int (*mkdir) (struct inode *,struct dentry *,umode_t);
1938*4882a593Smuzhiyun 	int (*rmdir) (struct inode *,struct dentry *);
1939*4882a593Smuzhiyun 	int (*mknod) (struct inode *,struct dentry *,umode_t,dev_t);
1940*4882a593Smuzhiyun 	int (*rename) (struct inode *, struct dentry *,
1941*4882a593Smuzhiyun 			struct inode *, struct dentry *, unsigned int);
1942*4882a593Smuzhiyun 	int (*setattr) (struct dentry *, struct iattr *);
1943*4882a593Smuzhiyun 	int (*getattr) (const struct path *, struct kstat *, u32, unsigned int);
1944*4882a593Smuzhiyun 	ssize_t (*listxattr) (struct dentry *, char *, size_t);
1945*4882a593Smuzhiyun 	int (*fiemap)(struct inode *, struct fiemap_extent_info *, u64 start,
1946*4882a593Smuzhiyun 		      u64 len);
1947*4882a593Smuzhiyun 	int (*update_time)(struct inode *, struct timespec64 *, int);
1948*4882a593Smuzhiyun 	int (*atomic_open)(struct inode *, struct dentry *,
1949*4882a593Smuzhiyun 			   struct file *, unsigned open_flag,
1950*4882a593Smuzhiyun 			   umode_t create_mode);
1951*4882a593Smuzhiyun 	int (*tmpfile) (struct inode *, struct dentry *, umode_t);
1952*4882a593Smuzhiyun 	int (*set_acl)(struct inode *, struct posix_acl *, int);
1953*4882a593Smuzhiyun 
1954*4882a593Smuzhiyun 	ANDROID_KABI_RESERVE(1);
1955*4882a593Smuzhiyun 	ANDROID_KABI_RESERVE(2);
1956*4882a593Smuzhiyun 	ANDROID_KABI_RESERVE(3);
1957*4882a593Smuzhiyun 	ANDROID_KABI_RESERVE(4);
1958*4882a593Smuzhiyun } ____cacheline_aligned;
1959*4882a593Smuzhiyun 
call_read_iter(struct file * file,struct kiocb * kio,struct iov_iter * iter)1960*4882a593Smuzhiyun static inline ssize_t call_read_iter(struct file *file, struct kiocb *kio,
1961*4882a593Smuzhiyun 				     struct iov_iter *iter)
1962*4882a593Smuzhiyun {
1963*4882a593Smuzhiyun 	return file->f_op->read_iter(kio, iter);
1964*4882a593Smuzhiyun }
1965*4882a593Smuzhiyun 
call_write_iter(struct file * file,struct kiocb * kio,struct iov_iter * iter)1966*4882a593Smuzhiyun static inline ssize_t call_write_iter(struct file *file, struct kiocb *kio,
1967*4882a593Smuzhiyun 				      struct iov_iter *iter)
1968*4882a593Smuzhiyun {
1969*4882a593Smuzhiyun 	return file->f_op->write_iter(kio, iter);
1970*4882a593Smuzhiyun }
1971*4882a593Smuzhiyun 
call_mmap(struct file * file,struct vm_area_struct * vma)1972*4882a593Smuzhiyun static inline int call_mmap(struct file *file, struct vm_area_struct *vma)
1973*4882a593Smuzhiyun {
1974*4882a593Smuzhiyun 	return file->f_op->mmap(file, vma);
1975*4882a593Smuzhiyun }
1976*4882a593Smuzhiyun 
1977*4882a593Smuzhiyun extern ssize_t vfs_read(struct file *, char __user *, size_t, loff_t *);
1978*4882a593Smuzhiyun extern ssize_t vfs_write(struct file *, const char __user *, size_t, loff_t *);
1979*4882a593Smuzhiyun extern ssize_t vfs_copy_file_range(struct file *, loff_t , struct file *,
1980*4882a593Smuzhiyun 				   loff_t, size_t, unsigned int);
1981*4882a593Smuzhiyun extern ssize_t generic_copy_file_range(struct file *file_in, loff_t pos_in,
1982*4882a593Smuzhiyun 				       struct file *file_out, loff_t pos_out,
1983*4882a593Smuzhiyun 				       size_t len, unsigned int flags);
1984*4882a593Smuzhiyun extern int generic_remap_file_range_prep(struct file *file_in, loff_t pos_in,
1985*4882a593Smuzhiyun 					 struct file *file_out, loff_t pos_out,
1986*4882a593Smuzhiyun 					 loff_t *count,
1987*4882a593Smuzhiyun 					 unsigned int remap_flags);
1988*4882a593Smuzhiyun extern loff_t do_clone_file_range(struct file *file_in, loff_t pos_in,
1989*4882a593Smuzhiyun 				  struct file *file_out, loff_t pos_out,
1990*4882a593Smuzhiyun 				  loff_t len, unsigned int remap_flags);
1991*4882a593Smuzhiyun extern loff_t vfs_clone_file_range(struct file *file_in, loff_t pos_in,
1992*4882a593Smuzhiyun 				   struct file *file_out, loff_t pos_out,
1993*4882a593Smuzhiyun 				   loff_t len, unsigned int remap_flags);
1994*4882a593Smuzhiyun extern int vfs_dedupe_file_range(struct file *file,
1995*4882a593Smuzhiyun 				 struct file_dedupe_range *same);
1996*4882a593Smuzhiyun extern loff_t vfs_dedupe_file_range_one(struct file *src_file, loff_t src_pos,
1997*4882a593Smuzhiyun 					struct file *dst_file, loff_t dst_pos,
1998*4882a593Smuzhiyun 					loff_t len, unsigned int remap_flags);
1999*4882a593Smuzhiyun 
2000*4882a593Smuzhiyun 
2001*4882a593Smuzhiyun struct super_operations {
2002*4882a593Smuzhiyun    	struct inode *(*alloc_inode)(struct super_block *sb);
2003*4882a593Smuzhiyun 	void (*destroy_inode)(struct inode *);
2004*4882a593Smuzhiyun 	void (*free_inode)(struct inode *);
2005*4882a593Smuzhiyun 
2006*4882a593Smuzhiyun    	void (*dirty_inode) (struct inode *, int flags);
2007*4882a593Smuzhiyun 	int (*write_inode) (struct inode *, struct writeback_control *wbc);
2008*4882a593Smuzhiyun 	int (*drop_inode) (struct inode *);
2009*4882a593Smuzhiyun 	void (*evict_inode) (struct inode *);
2010*4882a593Smuzhiyun 	void (*put_super) (struct super_block *);
2011*4882a593Smuzhiyun 	int (*sync_fs)(struct super_block *sb, int wait);
2012*4882a593Smuzhiyun 	int (*freeze_super) (struct super_block *);
2013*4882a593Smuzhiyun 	int (*freeze_fs) (struct super_block *);
2014*4882a593Smuzhiyun 	int (*thaw_super) (struct super_block *);
2015*4882a593Smuzhiyun 	int (*unfreeze_fs) (struct super_block *);
2016*4882a593Smuzhiyun 	int (*statfs) (struct dentry *, struct kstatfs *);
2017*4882a593Smuzhiyun 	int (*remount_fs) (struct super_block *, int *, char *);
2018*4882a593Smuzhiyun 	void (*umount_begin) (struct super_block *);
2019*4882a593Smuzhiyun 
2020*4882a593Smuzhiyun 	int (*show_options)(struct seq_file *, struct dentry *);
2021*4882a593Smuzhiyun 	int (*show_devname)(struct seq_file *, struct dentry *);
2022*4882a593Smuzhiyun 	int (*show_path)(struct seq_file *, struct dentry *);
2023*4882a593Smuzhiyun 	int (*show_stats)(struct seq_file *, struct dentry *);
2024*4882a593Smuzhiyun #ifdef CONFIG_QUOTA
2025*4882a593Smuzhiyun 	ssize_t (*quota_read)(struct super_block *, int, char *, size_t, loff_t);
2026*4882a593Smuzhiyun 	ssize_t (*quota_write)(struct super_block *, int, const char *, size_t, loff_t);
2027*4882a593Smuzhiyun 	struct dquot **(*get_dquots)(struct inode *);
2028*4882a593Smuzhiyun #endif
2029*4882a593Smuzhiyun 	int (*bdev_try_to_free_page)(struct super_block*, struct page*, gfp_t);
2030*4882a593Smuzhiyun 	long (*nr_cached_objects)(struct super_block *,
2031*4882a593Smuzhiyun 				  struct shrink_control *);
2032*4882a593Smuzhiyun 	long (*free_cached_objects)(struct super_block *,
2033*4882a593Smuzhiyun 				    struct shrink_control *);
2034*4882a593Smuzhiyun 
2035*4882a593Smuzhiyun 	ANDROID_KABI_RESERVE(1);
2036*4882a593Smuzhiyun 	ANDROID_KABI_RESERVE(2);
2037*4882a593Smuzhiyun 	ANDROID_KABI_RESERVE(3);
2038*4882a593Smuzhiyun 	ANDROID_KABI_RESERVE(4);
2039*4882a593Smuzhiyun };
2040*4882a593Smuzhiyun 
2041*4882a593Smuzhiyun /*
2042*4882a593Smuzhiyun  * Inode flags - they have no relation to superblock flags now
2043*4882a593Smuzhiyun  */
2044*4882a593Smuzhiyun #define S_SYNC		(1 << 0)  /* Writes are synced at once */
2045*4882a593Smuzhiyun #define S_NOATIME	(1 << 1)  /* Do not update access times */
2046*4882a593Smuzhiyun #define S_APPEND	(1 << 2)  /* Append-only file */
2047*4882a593Smuzhiyun #define S_IMMUTABLE	(1 << 3)  /* Immutable file */
2048*4882a593Smuzhiyun #define S_DEAD		(1 << 4)  /* removed, but still open directory */
2049*4882a593Smuzhiyun #define S_NOQUOTA	(1 << 5)  /* Inode is not counted to quota */
2050*4882a593Smuzhiyun #define S_DIRSYNC	(1 << 6)  /* Directory modifications are synchronous */
2051*4882a593Smuzhiyun #define S_NOCMTIME	(1 << 7)  /* Do not update file c/mtime */
2052*4882a593Smuzhiyun #define S_SWAPFILE	(1 << 8)  /* Do not truncate: swapon got its bmaps */
2053*4882a593Smuzhiyun #define S_PRIVATE	(1 << 9)  /* Inode is fs-internal */
2054*4882a593Smuzhiyun #define S_IMA		(1 << 10) /* Inode has an associated IMA struct */
2055*4882a593Smuzhiyun #define S_AUTOMOUNT	(1 << 11) /* Automount/referral quasi-directory */
2056*4882a593Smuzhiyun #define S_NOSEC		(1 << 12) /* no suid or xattr security attributes */
2057*4882a593Smuzhiyun #ifdef CONFIG_FS_DAX
2058*4882a593Smuzhiyun #define S_DAX		(1 << 13) /* Direct Access, avoiding the page cache */
2059*4882a593Smuzhiyun #else
2060*4882a593Smuzhiyun #define S_DAX		0	  /* Make all the DAX code disappear */
2061*4882a593Smuzhiyun #endif
2062*4882a593Smuzhiyun #define S_ENCRYPTED	(1 << 14) /* Encrypted file (using fs/crypto/) */
2063*4882a593Smuzhiyun #define S_CASEFOLD	(1 << 15) /* Casefolded file */
2064*4882a593Smuzhiyun #define S_VERITY	(1 << 16) /* Verity file (using fs/verity/) */
2065*4882a593Smuzhiyun 
2066*4882a593Smuzhiyun /*
2067*4882a593Smuzhiyun  * Note that nosuid etc flags are inode-specific: setting some file-system
2068*4882a593Smuzhiyun  * flags just means all the inodes inherit those flags by default. It might be
2069*4882a593Smuzhiyun  * possible to override it selectively if you really wanted to with some
2070*4882a593Smuzhiyun  * ioctl() that is not currently implemented.
2071*4882a593Smuzhiyun  *
2072*4882a593Smuzhiyun  * Exception: SB_RDONLY is always applied to the entire file system.
2073*4882a593Smuzhiyun  *
2074*4882a593Smuzhiyun  * Unfortunately, it is possible to change a filesystems flags with it mounted
2075*4882a593Smuzhiyun  * with files in use.  This means that all of the inodes will not have their
2076*4882a593Smuzhiyun  * i_flags updated.  Hence, i_flags no longer inherit the superblock mount
2077*4882a593Smuzhiyun  * flags, so these have to be checked separately. -- rmk@arm.uk.linux.org
2078*4882a593Smuzhiyun  */
2079*4882a593Smuzhiyun #define __IS_FLG(inode, flg)	((inode)->i_sb->s_flags & (flg))
2080*4882a593Smuzhiyun 
sb_rdonly(const struct super_block * sb)2081*4882a593Smuzhiyun static inline bool sb_rdonly(const struct super_block *sb) { return sb->s_flags & SB_RDONLY; }
2082*4882a593Smuzhiyun #define IS_RDONLY(inode)	sb_rdonly((inode)->i_sb)
2083*4882a593Smuzhiyun #define IS_SYNC(inode)		(__IS_FLG(inode, SB_SYNCHRONOUS) || \
2084*4882a593Smuzhiyun 					((inode)->i_flags & S_SYNC))
2085*4882a593Smuzhiyun #define IS_DIRSYNC(inode)	(__IS_FLG(inode, SB_SYNCHRONOUS|SB_DIRSYNC) || \
2086*4882a593Smuzhiyun 					((inode)->i_flags & (S_SYNC|S_DIRSYNC)))
2087*4882a593Smuzhiyun #define IS_MANDLOCK(inode)	__IS_FLG(inode, SB_MANDLOCK)
2088*4882a593Smuzhiyun #define IS_NOATIME(inode)	__IS_FLG(inode, SB_RDONLY|SB_NOATIME)
2089*4882a593Smuzhiyun #define IS_I_VERSION(inode)	__IS_FLG(inode, SB_I_VERSION)
2090*4882a593Smuzhiyun 
2091*4882a593Smuzhiyun #define IS_NOQUOTA(inode)	((inode)->i_flags & S_NOQUOTA)
2092*4882a593Smuzhiyun #define IS_APPEND(inode)	((inode)->i_flags & S_APPEND)
2093*4882a593Smuzhiyun #define IS_IMMUTABLE(inode)	((inode)->i_flags & S_IMMUTABLE)
2094*4882a593Smuzhiyun #define IS_POSIXACL(inode)	__IS_FLG(inode, SB_POSIXACL)
2095*4882a593Smuzhiyun 
2096*4882a593Smuzhiyun #define IS_DEADDIR(inode)	((inode)->i_flags & S_DEAD)
2097*4882a593Smuzhiyun #define IS_NOCMTIME(inode)	((inode)->i_flags & S_NOCMTIME)
2098*4882a593Smuzhiyun #define IS_SWAPFILE(inode)	((inode)->i_flags & S_SWAPFILE)
2099*4882a593Smuzhiyun #define IS_PRIVATE(inode)	((inode)->i_flags & S_PRIVATE)
2100*4882a593Smuzhiyun #define IS_IMA(inode)		((inode)->i_flags & S_IMA)
2101*4882a593Smuzhiyun #define IS_AUTOMOUNT(inode)	((inode)->i_flags & S_AUTOMOUNT)
2102*4882a593Smuzhiyun #define IS_NOSEC(inode)		((inode)->i_flags & S_NOSEC)
2103*4882a593Smuzhiyun #define IS_DAX(inode)		((inode)->i_flags & S_DAX)
2104*4882a593Smuzhiyun #define IS_ENCRYPTED(inode)	((inode)->i_flags & S_ENCRYPTED)
2105*4882a593Smuzhiyun #define IS_CASEFOLDED(inode)	((inode)->i_flags & S_CASEFOLD)
2106*4882a593Smuzhiyun #define IS_VERITY(inode)	((inode)->i_flags & S_VERITY)
2107*4882a593Smuzhiyun 
2108*4882a593Smuzhiyun #define IS_WHITEOUT(inode)	(S_ISCHR(inode->i_mode) && \
2109*4882a593Smuzhiyun 				 (inode)->i_rdev == WHITEOUT_DEV)
2110*4882a593Smuzhiyun 
HAS_UNMAPPED_ID(struct inode * inode)2111*4882a593Smuzhiyun static inline bool HAS_UNMAPPED_ID(struct inode *inode)
2112*4882a593Smuzhiyun {
2113*4882a593Smuzhiyun 	return !uid_valid(inode->i_uid) || !gid_valid(inode->i_gid);
2114*4882a593Smuzhiyun }
2115*4882a593Smuzhiyun 
file_write_hint(struct file * file)2116*4882a593Smuzhiyun static inline enum rw_hint file_write_hint(struct file *file)
2117*4882a593Smuzhiyun {
2118*4882a593Smuzhiyun 	if (file->f_write_hint != WRITE_LIFE_NOT_SET)
2119*4882a593Smuzhiyun 		return file->f_write_hint;
2120*4882a593Smuzhiyun 
2121*4882a593Smuzhiyun 	return file_inode(file)->i_write_hint;
2122*4882a593Smuzhiyun }
2123*4882a593Smuzhiyun 
2124*4882a593Smuzhiyun static inline int iocb_flags(struct file *file);
2125*4882a593Smuzhiyun 
ki_hint_validate(enum rw_hint hint)2126*4882a593Smuzhiyun static inline u16 ki_hint_validate(enum rw_hint hint)
2127*4882a593Smuzhiyun {
2128*4882a593Smuzhiyun 	typeof(((struct kiocb *)0)->ki_hint) max_hint = -1;
2129*4882a593Smuzhiyun 
2130*4882a593Smuzhiyun 	if (hint <= max_hint)
2131*4882a593Smuzhiyun 		return hint;
2132*4882a593Smuzhiyun 	return 0;
2133*4882a593Smuzhiyun }
2134*4882a593Smuzhiyun 
init_sync_kiocb(struct kiocb * kiocb,struct file * filp)2135*4882a593Smuzhiyun static inline void init_sync_kiocb(struct kiocb *kiocb, struct file *filp)
2136*4882a593Smuzhiyun {
2137*4882a593Smuzhiyun 	*kiocb = (struct kiocb) {
2138*4882a593Smuzhiyun 		.ki_filp = filp,
2139*4882a593Smuzhiyun 		.ki_flags = iocb_flags(filp),
2140*4882a593Smuzhiyun 		.ki_hint = ki_hint_validate(file_write_hint(filp)),
2141*4882a593Smuzhiyun 		.ki_ioprio = get_current_ioprio(),
2142*4882a593Smuzhiyun 	};
2143*4882a593Smuzhiyun }
2144*4882a593Smuzhiyun 
kiocb_clone(struct kiocb * kiocb,struct kiocb * kiocb_src,struct file * filp)2145*4882a593Smuzhiyun static inline void kiocb_clone(struct kiocb *kiocb, struct kiocb *kiocb_src,
2146*4882a593Smuzhiyun 			       struct file *filp)
2147*4882a593Smuzhiyun {
2148*4882a593Smuzhiyun 	*kiocb = (struct kiocb) {
2149*4882a593Smuzhiyun 		.ki_filp = filp,
2150*4882a593Smuzhiyun 		.ki_flags = kiocb_src->ki_flags,
2151*4882a593Smuzhiyun 		.ki_hint = kiocb_src->ki_hint,
2152*4882a593Smuzhiyun 		.ki_ioprio = kiocb_src->ki_ioprio,
2153*4882a593Smuzhiyun 		.ki_pos = kiocb_src->ki_pos,
2154*4882a593Smuzhiyun 	};
2155*4882a593Smuzhiyun }
2156*4882a593Smuzhiyun 
2157*4882a593Smuzhiyun /*
2158*4882a593Smuzhiyun  * Inode state bits.  Protected by inode->i_lock
2159*4882a593Smuzhiyun  *
2160*4882a593Smuzhiyun  * Three bits determine the dirty state of the inode, I_DIRTY_SYNC,
2161*4882a593Smuzhiyun  * I_DIRTY_DATASYNC and I_DIRTY_PAGES.
2162*4882a593Smuzhiyun  *
2163*4882a593Smuzhiyun  * Four bits define the lifetime of an inode.  Initially, inodes are I_NEW,
2164*4882a593Smuzhiyun  * until that flag is cleared.  I_WILL_FREE, I_FREEING and I_CLEAR are set at
2165*4882a593Smuzhiyun  * various stages of removing an inode.
2166*4882a593Smuzhiyun  *
2167*4882a593Smuzhiyun  * Two bits are used for locking and completion notification, I_NEW and I_SYNC.
2168*4882a593Smuzhiyun  *
2169*4882a593Smuzhiyun  * I_DIRTY_SYNC		Inode is dirty, but doesn't have to be written on
2170*4882a593Smuzhiyun  *			fdatasync().  i_atime is the usual cause.
2171*4882a593Smuzhiyun  * I_DIRTY_DATASYNC	Data-related inode changes pending. We keep track of
2172*4882a593Smuzhiyun  *			these changes separately from I_DIRTY_SYNC so that we
2173*4882a593Smuzhiyun  *			don't have to write inode on fdatasync() when only
2174*4882a593Smuzhiyun  *			mtime has changed in it.
2175*4882a593Smuzhiyun  * I_DIRTY_PAGES	Inode has dirty pages.  Inode itself may be clean.
2176*4882a593Smuzhiyun  * I_NEW		Serves as both a mutex and completion notification.
2177*4882a593Smuzhiyun  *			New inodes set I_NEW.  If two processes both create
2178*4882a593Smuzhiyun  *			the same inode, one of them will release its inode and
2179*4882a593Smuzhiyun  *			wait for I_NEW to be released before returning.
2180*4882a593Smuzhiyun  *			Inodes in I_WILL_FREE, I_FREEING or I_CLEAR state can
2181*4882a593Smuzhiyun  *			also cause waiting on I_NEW, without I_NEW actually
2182*4882a593Smuzhiyun  *			being set.  find_inode() uses this to prevent returning
2183*4882a593Smuzhiyun  *			nearly-dead inodes.
2184*4882a593Smuzhiyun  * I_WILL_FREE		Must be set when calling write_inode_now() if i_count
2185*4882a593Smuzhiyun  *			is zero.  I_FREEING must be set when I_WILL_FREE is
2186*4882a593Smuzhiyun  *			cleared.
2187*4882a593Smuzhiyun  * I_FREEING		Set when inode is about to be freed but still has dirty
2188*4882a593Smuzhiyun  *			pages or buffers attached or the inode itself is still
2189*4882a593Smuzhiyun  *			dirty.
2190*4882a593Smuzhiyun  * I_CLEAR		Added by clear_inode().  In this state the inode is
2191*4882a593Smuzhiyun  *			clean and can be destroyed.  Inode keeps I_FREEING.
2192*4882a593Smuzhiyun  *
2193*4882a593Smuzhiyun  *			Inodes that are I_WILL_FREE, I_FREEING or I_CLEAR are
2194*4882a593Smuzhiyun  *			prohibited for many purposes.  iget() must wait for
2195*4882a593Smuzhiyun  *			the inode to be completely released, then create it
2196*4882a593Smuzhiyun  *			anew.  Other functions will just ignore such inodes,
2197*4882a593Smuzhiyun  *			if appropriate.  I_NEW is used for waiting.
2198*4882a593Smuzhiyun  *
2199*4882a593Smuzhiyun  * I_SYNC		Writeback of inode is running. The bit is set during
2200*4882a593Smuzhiyun  *			data writeback, and cleared with a wakeup on the bit
2201*4882a593Smuzhiyun  *			address once it is done. The bit is also used to pin
2202*4882a593Smuzhiyun  *			the inode in memory for flusher thread.
2203*4882a593Smuzhiyun  *
2204*4882a593Smuzhiyun  * I_REFERENCED		Marks the inode as recently references on the LRU list.
2205*4882a593Smuzhiyun  *
2206*4882a593Smuzhiyun  * I_DIO_WAKEUP		Never set.  Only used as a key for wait_on_bit().
2207*4882a593Smuzhiyun  *
2208*4882a593Smuzhiyun  * I_WB_SWITCH		Cgroup bdi_writeback switching in progress.  Used to
2209*4882a593Smuzhiyun  *			synchronize competing switching instances and to tell
2210*4882a593Smuzhiyun  *			wb stat updates to grab the i_pages lock.  See
2211*4882a593Smuzhiyun  *			inode_switch_wbs_work_fn() for details.
2212*4882a593Smuzhiyun  *
2213*4882a593Smuzhiyun  * I_OVL_INUSE		Used by overlayfs to get exclusive ownership on upper
2214*4882a593Smuzhiyun  *			and work dirs among overlayfs mounts.
2215*4882a593Smuzhiyun  *
2216*4882a593Smuzhiyun  * I_CREATING		New object's inode in the middle of setting up.
2217*4882a593Smuzhiyun  *
2218*4882a593Smuzhiyun  * I_DONTCACHE		Evict inode as soon as it is not used anymore.
2219*4882a593Smuzhiyun  *
2220*4882a593Smuzhiyun  * I_SYNC_QUEUED	Inode is queued in b_io or b_more_io writeback lists.
2221*4882a593Smuzhiyun  *			Used to detect that mark_inode_dirty() should not move
2222*4882a593Smuzhiyun  * 			inode between dirty lists.
2223*4882a593Smuzhiyun  *
2224*4882a593Smuzhiyun  * Q: What is the difference between I_WILL_FREE and I_FREEING?
2225*4882a593Smuzhiyun  */
2226*4882a593Smuzhiyun #define I_DIRTY_SYNC		(1 << 0)
2227*4882a593Smuzhiyun #define I_DIRTY_DATASYNC	(1 << 1)
2228*4882a593Smuzhiyun #define I_DIRTY_PAGES		(1 << 2)
2229*4882a593Smuzhiyun #define __I_NEW			3
2230*4882a593Smuzhiyun #define I_NEW			(1 << __I_NEW)
2231*4882a593Smuzhiyun #define I_WILL_FREE		(1 << 4)
2232*4882a593Smuzhiyun #define I_FREEING		(1 << 5)
2233*4882a593Smuzhiyun #define I_CLEAR			(1 << 6)
2234*4882a593Smuzhiyun #define __I_SYNC		7
2235*4882a593Smuzhiyun #define I_SYNC			(1 << __I_SYNC)
2236*4882a593Smuzhiyun #define I_REFERENCED		(1 << 8)
2237*4882a593Smuzhiyun #define __I_DIO_WAKEUP		9
2238*4882a593Smuzhiyun #define I_DIO_WAKEUP		(1 << __I_DIO_WAKEUP)
2239*4882a593Smuzhiyun #define I_LINKABLE		(1 << 10)
2240*4882a593Smuzhiyun #define I_DIRTY_TIME		(1 << 11)
2241*4882a593Smuzhiyun #define I_WB_SWITCH		(1 << 13)
2242*4882a593Smuzhiyun #define I_OVL_INUSE		(1 << 14)
2243*4882a593Smuzhiyun #define I_CREATING		(1 << 15)
2244*4882a593Smuzhiyun #define I_DONTCACHE		(1 << 16)
2245*4882a593Smuzhiyun #define I_SYNC_QUEUED		(1 << 17)
2246*4882a593Smuzhiyun 
2247*4882a593Smuzhiyun #define I_DIRTY_INODE (I_DIRTY_SYNC | I_DIRTY_DATASYNC)
2248*4882a593Smuzhiyun #define I_DIRTY (I_DIRTY_INODE | I_DIRTY_PAGES)
2249*4882a593Smuzhiyun #define I_DIRTY_ALL (I_DIRTY | I_DIRTY_TIME)
2250*4882a593Smuzhiyun 
2251*4882a593Smuzhiyun extern void __mark_inode_dirty(struct inode *, int);
mark_inode_dirty(struct inode * inode)2252*4882a593Smuzhiyun static inline void mark_inode_dirty(struct inode *inode)
2253*4882a593Smuzhiyun {
2254*4882a593Smuzhiyun 	__mark_inode_dirty(inode, I_DIRTY);
2255*4882a593Smuzhiyun }
2256*4882a593Smuzhiyun 
mark_inode_dirty_sync(struct inode * inode)2257*4882a593Smuzhiyun static inline void mark_inode_dirty_sync(struct inode *inode)
2258*4882a593Smuzhiyun {
2259*4882a593Smuzhiyun 	__mark_inode_dirty(inode, I_DIRTY_SYNC);
2260*4882a593Smuzhiyun }
2261*4882a593Smuzhiyun 
2262*4882a593Smuzhiyun extern void inc_nlink(struct inode *inode);
2263*4882a593Smuzhiyun extern void drop_nlink(struct inode *inode);
2264*4882a593Smuzhiyun extern void clear_nlink(struct inode *inode);
2265*4882a593Smuzhiyun extern void set_nlink(struct inode *inode, unsigned int nlink);
2266*4882a593Smuzhiyun 
inode_inc_link_count(struct inode * inode)2267*4882a593Smuzhiyun static inline void inode_inc_link_count(struct inode *inode)
2268*4882a593Smuzhiyun {
2269*4882a593Smuzhiyun 	inc_nlink(inode);
2270*4882a593Smuzhiyun 	mark_inode_dirty(inode);
2271*4882a593Smuzhiyun }
2272*4882a593Smuzhiyun 
inode_dec_link_count(struct inode * inode)2273*4882a593Smuzhiyun static inline void inode_dec_link_count(struct inode *inode)
2274*4882a593Smuzhiyun {
2275*4882a593Smuzhiyun 	drop_nlink(inode);
2276*4882a593Smuzhiyun 	mark_inode_dirty(inode);
2277*4882a593Smuzhiyun }
2278*4882a593Smuzhiyun 
2279*4882a593Smuzhiyun enum file_time_flags {
2280*4882a593Smuzhiyun 	S_ATIME = 1,
2281*4882a593Smuzhiyun 	S_MTIME = 2,
2282*4882a593Smuzhiyun 	S_CTIME = 4,
2283*4882a593Smuzhiyun 	S_VERSION = 8,
2284*4882a593Smuzhiyun };
2285*4882a593Smuzhiyun 
2286*4882a593Smuzhiyun extern bool atime_needs_update(const struct path *, struct inode *);
2287*4882a593Smuzhiyun extern void touch_atime(const struct path *);
2288*4882a593Smuzhiyun int inode_update_time(struct inode *inode, struct timespec64 *time, int flags);
2289*4882a593Smuzhiyun 
file_accessed(struct file * file)2290*4882a593Smuzhiyun static inline void file_accessed(struct file *file)
2291*4882a593Smuzhiyun {
2292*4882a593Smuzhiyun 	if (!(file->f_flags & O_NOATIME))
2293*4882a593Smuzhiyun 		touch_atime(&file->f_path);
2294*4882a593Smuzhiyun }
2295*4882a593Smuzhiyun 
2296*4882a593Smuzhiyun extern int file_modified(struct file *file);
2297*4882a593Smuzhiyun 
2298*4882a593Smuzhiyun int sync_inode(struct inode *inode, struct writeback_control *wbc);
2299*4882a593Smuzhiyun int sync_inode_metadata(struct inode *inode, int wait);
2300*4882a593Smuzhiyun 
2301*4882a593Smuzhiyun struct file_system_type {
2302*4882a593Smuzhiyun 	const char *name;
2303*4882a593Smuzhiyun 	int fs_flags;
2304*4882a593Smuzhiyun #define FS_REQUIRES_DEV		1
2305*4882a593Smuzhiyun #define FS_BINARY_MOUNTDATA	2
2306*4882a593Smuzhiyun #define FS_HAS_SUBTYPE		4
2307*4882a593Smuzhiyun #define FS_USERNS_MOUNT		8	/* Can be mounted by userns root */
2308*4882a593Smuzhiyun #define FS_DISALLOW_NOTIFY_PERM	16	/* Disable fanotify permission events */
2309*4882a593Smuzhiyun #define FS_THP_SUPPORT		8192	/* Remove once all fs converted */
2310*4882a593Smuzhiyun #define FS_RENAME_DOES_D_MOVE	32768	/* FS will handle d_move() during rename() internally. */
2311*4882a593Smuzhiyun 	int (*init_fs_context)(struct fs_context *);
2312*4882a593Smuzhiyun 	const struct fs_parameter_spec *parameters;
2313*4882a593Smuzhiyun 	struct dentry *(*mount) (struct file_system_type *, int,
2314*4882a593Smuzhiyun 		       const char *, void *);
2315*4882a593Smuzhiyun 	void (*kill_sb) (struct super_block *);
2316*4882a593Smuzhiyun 	struct module *owner;
2317*4882a593Smuzhiyun 	struct file_system_type * next;
2318*4882a593Smuzhiyun 	struct hlist_head fs_supers;
2319*4882a593Smuzhiyun 
2320*4882a593Smuzhiyun 	struct lock_class_key s_lock_key;
2321*4882a593Smuzhiyun 	struct lock_class_key s_umount_key;
2322*4882a593Smuzhiyun 	struct lock_class_key s_vfs_rename_key;
2323*4882a593Smuzhiyun 	struct lock_class_key s_writers_key[SB_FREEZE_LEVELS];
2324*4882a593Smuzhiyun 
2325*4882a593Smuzhiyun 	struct lock_class_key i_lock_key;
2326*4882a593Smuzhiyun 	struct lock_class_key i_mutex_key;
2327*4882a593Smuzhiyun 	struct lock_class_key i_mutex_dir_key;
2328*4882a593Smuzhiyun 
2329*4882a593Smuzhiyun 	ANDROID_KABI_RESERVE(1);
2330*4882a593Smuzhiyun 	ANDROID_KABI_RESERVE(2);
2331*4882a593Smuzhiyun 	ANDROID_KABI_RESERVE(3);
2332*4882a593Smuzhiyun 	ANDROID_KABI_RESERVE(4);
2333*4882a593Smuzhiyun };
2334*4882a593Smuzhiyun 
2335*4882a593Smuzhiyun #define MODULE_ALIAS_FS(NAME) MODULE_ALIAS("fs-" NAME)
2336*4882a593Smuzhiyun 
2337*4882a593Smuzhiyun extern struct dentry *mount_bdev(struct file_system_type *fs_type,
2338*4882a593Smuzhiyun 	int flags, const char *dev_name, void *data,
2339*4882a593Smuzhiyun 	int (*fill_super)(struct super_block *, void *, int));
2340*4882a593Smuzhiyun extern struct dentry *mount_single(struct file_system_type *fs_type,
2341*4882a593Smuzhiyun 	int flags, void *data,
2342*4882a593Smuzhiyun 	int (*fill_super)(struct super_block *, void *, int));
2343*4882a593Smuzhiyun extern struct dentry *mount_nodev(struct file_system_type *fs_type,
2344*4882a593Smuzhiyun 	int flags, void *data,
2345*4882a593Smuzhiyun 	int (*fill_super)(struct super_block *, void *, int));
2346*4882a593Smuzhiyun extern struct dentry *mount_subtree(struct vfsmount *mnt, const char *path);
2347*4882a593Smuzhiyun void generic_shutdown_super(struct super_block *sb);
2348*4882a593Smuzhiyun void kill_block_super(struct super_block *sb);
2349*4882a593Smuzhiyun void kill_anon_super(struct super_block *sb);
2350*4882a593Smuzhiyun void kill_litter_super(struct super_block *sb);
2351*4882a593Smuzhiyun void deactivate_super(struct super_block *sb);
2352*4882a593Smuzhiyun void deactivate_locked_super(struct super_block *sb);
2353*4882a593Smuzhiyun int set_anon_super(struct super_block *s, void *data);
2354*4882a593Smuzhiyun int set_anon_super_fc(struct super_block *s, struct fs_context *fc);
2355*4882a593Smuzhiyun int get_anon_bdev(dev_t *);
2356*4882a593Smuzhiyun void free_anon_bdev(dev_t);
2357*4882a593Smuzhiyun struct super_block *sget_fc(struct fs_context *fc,
2358*4882a593Smuzhiyun 			    int (*test)(struct super_block *, struct fs_context *),
2359*4882a593Smuzhiyun 			    int (*set)(struct super_block *, struct fs_context *));
2360*4882a593Smuzhiyun struct super_block *sget(struct file_system_type *type,
2361*4882a593Smuzhiyun 			int (*test)(struct super_block *,void *),
2362*4882a593Smuzhiyun 			int (*set)(struct super_block *,void *),
2363*4882a593Smuzhiyun 			int flags, void *data);
2364*4882a593Smuzhiyun 
2365*4882a593Smuzhiyun /* Alas, no aliases. Too much hassle with bringing module.h everywhere */
2366*4882a593Smuzhiyun #define fops_get(fops) \
2367*4882a593Smuzhiyun 	(((fops) && try_module_get((fops)->owner) ? (fops) : NULL))
2368*4882a593Smuzhiyun #define fops_put(fops) \
2369*4882a593Smuzhiyun 	do { if (fops) module_put((fops)->owner); } while(0)
2370*4882a593Smuzhiyun /*
2371*4882a593Smuzhiyun  * This one is to be used *ONLY* from ->open() instances.
2372*4882a593Smuzhiyun  * fops must be non-NULL, pinned down *and* module dependencies
2373*4882a593Smuzhiyun  * should be sufficient to pin the caller down as well.
2374*4882a593Smuzhiyun  */
2375*4882a593Smuzhiyun #define replace_fops(f, fops) \
2376*4882a593Smuzhiyun 	do {	\
2377*4882a593Smuzhiyun 		struct file *__file = (f); \
2378*4882a593Smuzhiyun 		fops_put(__file->f_op); \
2379*4882a593Smuzhiyun 		BUG_ON(!(__file->f_op = (fops))); \
2380*4882a593Smuzhiyun 	} while(0)
2381*4882a593Smuzhiyun 
2382*4882a593Smuzhiyun extern int register_filesystem(struct file_system_type *);
2383*4882a593Smuzhiyun extern int unregister_filesystem(struct file_system_type *);
2384*4882a593Smuzhiyun extern struct vfsmount *kern_mount(struct file_system_type *);
2385*4882a593Smuzhiyun extern void kern_unmount(struct vfsmount *mnt);
2386*4882a593Smuzhiyun extern int may_umount_tree(struct vfsmount *);
2387*4882a593Smuzhiyun extern int may_umount(struct vfsmount *);
2388*4882a593Smuzhiyun extern long do_mount(const char *, const char __user *,
2389*4882a593Smuzhiyun 		     const char *, unsigned long, void *);
2390*4882a593Smuzhiyun extern struct vfsmount *collect_mounts(const struct path *);
2391*4882a593Smuzhiyun extern void drop_collected_mounts(struct vfsmount *);
2392*4882a593Smuzhiyun extern int iterate_mounts(int (*)(struct vfsmount *, void *), void *,
2393*4882a593Smuzhiyun 			  struct vfsmount *);
2394*4882a593Smuzhiyun extern int vfs_statfs(const struct path *, struct kstatfs *);
2395*4882a593Smuzhiyun extern int user_statfs(const char __user *, struct kstatfs *);
2396*4882a593Smuzhiyun extern int fd_statfs(int, struct kstatfs *);
2397*4882a593Smuzhiyun extern int freeze_super(struct super_block *super);
2398*4882a593Smuzhiyun extern int thaw_super(struct super_block *super);
2399*4882a593Smuzhiyun extern bool our_mnt(struct vfsmount *mnt);
2400*4882a593Smuzhiyun extern __printf(2, 3)
2401*4882a593Smuzhiyun int super_setup_bdi_name(struct super_block *sb, char *fmt, ...);
2402*4882a593Smuzhiyun extern int super_setup_bdi(struct super_block *sb);
2403*4882a593Smuzhiyun 
2404*4882a593Smuzhiyun extern int current_umask(void);
2405*4882a593Smuzhiyun 
2406*4882a593Smuzhiyun extern void ihold(struct inode * inode);
2407*4882a593Smuzhiyun extern void iput(struct inode *);
2408*4882a593Smuzhiyun extern int generic_update_time(struct inode *, struct timespec64 *, int);
2409*4882a593Smuzhiyun 
2410*4882a593Smuzhiyun /* /sys/fs */
2411*4882a593Smuzhiyun extern struct kobject *fs_kobj;
2412*4882a593Smuzhiyun 
2413*4882a593Smuzhiyun #define MAX_RW_COUNT (INT_MAX & PAGE_MASK)
2414*4882a593Smuzhiyun 
2415*4882a593Smuzhiyun #ifdef CONFIG_MANDATORY_FILE_LOCKING
2416*4882a593Smuzhiyun extern int locks_mandatory_locked(struct file *);
2417*4882a593Smuzhiyun extern int locks_mandatory_area(struct inode *, struct file *, loff_t, loff_t, unsigned char);
2418*4882a593Smuzhiyun 
2419*4882a593Smuzhiyun /*
2420*4882a593Smuzhiyun  * Candidates for mandatory locking have the setgid bit set
2421*4882a593Smuzhiyun  * but no group execute bit -  an otherwise meaningless combination.
2422*4882a593Smuzhiyun  */
2423*4882a593Smuzhiyun 
__mandatory_lock(struct inode * ino)2424*4882a593Smuzhiyun static inline int __mandatory_lock(struct inode *ino)
2425*4882a593Smuzhiyun {
2426*4882a593Smuzhiyun 	return (ino->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID;
2427*4882a593Smuzhiyun }
2428*4882a593Smuzhiyun 
2429*4882a593Smuzhiyun /*
2430*4882a593Smuzhiyun  * ... and these candidates should be on SB_MANDLOCK mounted fs,
2431*4882a593Smuzhiyun  * otherwise these will be advisory locks
2432*4882a593Smuzhiyun  */
2433*4882a593Smuzhiyun 
mandatory_lock(struct inode * ino)2434*4882a593Smuzhiyun static inline int mandatory_lock(struct inode *ino)
2435*4882a593Smuzhiyun {
2436*4882a593Smuzhiyun 	return IS_MANDLOCK(ino) && __mandatory_lock(ino);
2437*4882a593Smuzhiyun }
2438*4882a593Smuzhiyun 
locks_verify_locked(struct file * file)2439*4882a593Smuzhiyun static inline int locks_verify_locked(struct file *file)
2440*4882a593Smuzhiyun {
2441*4882a593Smuzhiyun 	if (mandatory_lock(locks_inode(file)))
2442*4882a593Smuzhiyun 		return locks_mandatory_locked(file);
2443*4882a593Smuzhiyun 	return 0;
2444*4882a593Smuzhiyun }
2445*4882a593Smuzhiyun 
locks_verify_truncate(struct inode * inode,struct file * f,loff_t size)2446*4882a593Smuzhiyun static inline int locks_verify_truncate(struct inode *inode,
2447*4882a593Smuzhiyun 				    struct file *f,
2448*4882a593Smuzhiyun 				    loff_t size)
2449*4882a593Smuzhiyun {
2450*4882a593Smuzhiyun 	if (!inode->i_flctx || !mandatory_lock(inode))
2451*4882a593Smuzhiyun 		return 0;
2452*4882a593Smuzhiyun 
2453*4882a593Smuzhiyun 	if (size < inode->i_size) {
2454*4882a593Smuzhiyun 		return locks_mandatory_area(inode, f, size, inode->i_size - 1,
2455*4882a593Smuzhiyun 				F_WRLCK);
2456*4882a593Smuzhiyun 	} else {
2457*4882a593Smuzhiyun 		return locks_mandatory_area(inode, f, inode->i_size, size - 1,
2458*4882a593Smuzhiyun 				F_WRLCK);
2459*4882a593Smuzhiyun 	}
2460*4882a593Smuzhiyun }
2461*4882a593Smuzhiyun 
2462*4882a593Smuzhiyun #else /* !CONFIG_MANDATORY_FILE_LOCKING */
2463*4882a593Smuzhiyun 
locks_mandatory_locked(struct file * file)2464*4882a593Smuzhiyun static inline int locks_mandatory_locked(struct file *file)
2465*4882a593Smuzhiyun {
2466*4882a593Smuzhiyun 	return 0;
2467*4882a593Smuzhiyun }
2468*4882a593Smuzhiyun 
locks_mandatory_area(struct inode * inode,struct file * filp,loff_t start,loff_t end,unsigned char type)2469*4882a593Smuzhiyun static inline int locks_mandatory_area(struct inode *inode, struct file *filp,
2470*4882a593Smuzhiyun                                        loff_t start, loff_t end, unsigned char type)
2471*4882a593Smuzhiyun {
2472*4882a593Smuzhiyun 	return 0;
2473*4882a593Smuzhiyun }
2474*4882a593Smuzhiyun 
__mandatory_lock(struct inode * inode)2475*4882a593Smuzhiyun static inline int __mandatory_lock(struct inode *inode)
2476*4882a593Smuzhiyun {
2477*4882a593Smuzhiyun 	return 0;
2478*4882a593Smuzhiyun }
2479*4882a593Smuzhiyun 
mandatory_lock(struct inode * inode)2480*4882a593Smuzhiyun static inline int mandatory_lock(struct inode *inode)
2481*4882a593Smuzhiyun {
2482*4882a593Smuzhiyun 	return 0;
2483*4882a593Smuzhiyun }
2484*4882a593Smuzhiyun 
locks_verify_locked(struct file * file)2485*4882a593Smuzhiyun static inline int locks_verify_locked(struct file *file)
2486*4882a593Smuzhiyun {
2487*4882a593Smuzhiyun 	return 0;
2488*4882a593Smuzhiyun }
2489*4882a593Smuzhiyun 
locks_verify_truncate(struct inode * inode,struct file * filp,size_t size)2490*4882a593Smuzhiyun static inline int locks_verify_truncate(struct inode *inode, struct file *filp,
2491*4882a593Smuzhiyun 					size_t size)
2492*4882a593Smuzhiyun {
2493*4882a593Smuzhiyun 	return 0;
2494*4882a593Smuzhiyun }
2495*4882a593Smuzhiyun 
2496*4882a593Smuzhiyun #endif /* CONFIG_MANDATORY_FILE_LOCKING */
2497*4882a593Smuzhiyun 
2498*4882a593Smuzhiyun 
2499*4882a593Smuzhiyun #ifdef CONFIG_FILE_LOCKING
break_lease(struct inode * inode,unsigned int mode)2500*4882a593Smuzhiyun static inline int break_lease(struct inode *inode, unsigned int mode)
2501*4882a593Smuzhiyun {
2502*4882a593Smuzhiyun 	/*
2503*4882a593Smuzhiyun 	 * Since this check is lockless, we must ensure that any refcounts
2504*4882a593Smuzhiyun 	 * taken are done before checking i_flctx->flc_lease. Otherwise, we
2505*4882a593Smuzhiyun 	 * could end up racing with tasks trying to set a new lease on this
2506*4882a593Smuzhiyun 	 * file.
2507*4882a593Smuzhiyun 	 */
2508*4882a593Smuzhiyun 	smp_mb();
2509*4882a593Smuzhiyun 	if (inode->i_flctx && !list_empty_careful(&inode->i_flctx->flc_lease))
2510*4882a593Smuzhiyun 		return __break_lease(inode, mode, FL_LEASE);
2511*4882a593Smuzhiyun 	return 0;
2512*4882a593Smuzhiyun }
2513*4882a593Smuzhiyun 
break_deleg(struct inode * inode,unsigned int mode)2514*4882a593Smuzhiyun static inline int break_deleg(struct inode *inode, unsigned int mode)
2515*4882a593Smuzhiyun {
2516*4882a593Smuzhiyun 	/*
2517*4882a593Smuzhiyun 	 * Since this check is lockless, we must ensure that any refcounts
2518*4882a593Smuzhiyun 	 * taken are done before checking i_flctx->flc_lease. Otherwise, we
2519*4882a593Smuzhiyun 	 * could end up racing with tasks trying to set a new lease on this
2520*4882a593Smuzhiyun 	 * file.
2521*4882a593Smuzhiyun 	 */
2522*4882a593Smuzhiyun 	smp_mb();
2523*4882a593Smuzhiyun 	if (inode->i_flctx && !list_empty_careful(&inode->i_flctx->flc_lease))
2524*4882a593Smuzhiyun 		return __break_lease(inode, mode, FL_DELEG);
2525*4882a593Smuzhiyun 	return 0;
2526*4882a593Smuzhiyun }
2527*4882a593Smuzhiyun 
try_break_deleg(struct inode * inode,struct inode ** delegated_inode)2528*4882a593Smuzhiyun static inline int try_break_deleg(struct inode *inode, struct inode **delegated_inode)
2529*4882a593Smuzhiyun {
2530*4882a593Smuzhiyun 	int ret;
2531*4882a593Smuzhiyun 
2532*4882a593Smuzhiyun 	ret = break_deleg(inode, O_WRONLY|O_NONBLOCK);
2533*4882a593Smuzhiyun 	if (ret == -EWOULDBLOCK && delegated_inode) {
2534*4882a593Smuzhiyun 		*delegated_inode = inode;
2535*4882a593Smuzhiyun 		ihold(inode);
2536*4882a593Smuzhiyun 	}
2537*4882a593Smuzhiyun 	return ret;
2538*4882a593Smuzhiyun }
2539*4882a593Smuzhiyun 
break_deleg_wait(struct inode ** delegated_inode)2540*4882a593Smuzhiyun static inline int break_deleg_wait(struct inode **delegated_inode)
2541*4882a593Smuzhiyun {
2542*4882a593Smuzhiyun 	int ret;
2543*4882a593Smuzhiyun 
2544*4882a593Smuzhiyun 	ret = break_deleg(*delegated_inode, O_WRONLY);
2545*4882a593Smuzhiyun 	iput(*delegated_inode);
2546*4882a593Smuzhiyun 	*delegated_inode = NULL;
2547*4882a593Smuzhiyun 	return ret;
2548*4882a593Smuzhiyun }
2549*4882a593Smuzhiyun 
break_layout(struct inode * inode,bool wait)2550*4882a593Smuzhiyun static inline int break_layout(struct inode *inode, bool wait)
2551*4882a593Smuzhiyun {
2552*4882a593Smuzhiyun 	smp_mb();
2553*4882a593Smuzhiyun 	if (inode->i_flctx && !list_empty_careful(&inode->i_flctx->flc_lease))
2554*4882a593Smuzhiyun 		return __break_lease(inode,
2555*4882a593Smuzhiyun 				wait ? O_WRONLY : O_WRONLY | O_NONBLOCK,
2556*4882a593Smuzhiyun 				FL_LAYOUT);
2557*4882a593Smuzhiyun 	return 0;
2558*4882a593Smuzhiyun }
2559*4882a593Smuzhiyun 
2560*4882a593Smuzhiyun #else /* !CONFIG_FILE_LOCKING */
break_lease(struct inode * inode,unsigned int mode)2561*4882a593Smuzhiyun static inline int break_lease(struct inode *inode, unsigned int mode)
2562*4882a593Smuzhiyun {
2563*4882a593Smuzhiyun 	return 0;
2564*4882a593Smuzhiyun }
2565*4882a593Smuzhiyun 
break_deleg(struct inode * inode,unsigned int mode)2566*4882a593Smuzhiyun static inline int break_deleg(struct inode *inode, unsigned int mode)
2567*4882a593Smuzhiyun {
2568*4882a593Smuzhiyun 	return 0;
2569*4882a593Smuzhiyun }
2570*4882a593Smuzhiyun 
try_break_deleg(struct inode * inode,struct inode ** delegated_inode)2571*4882a593Smuzhiyun static inline int try_break_deleg(struct inode *inode, struct inode **delegated_inode)
2572*4882a593Smuzhiyun {
2573*4882a593Smuzhiyun 	return 0;
2574*4882a593Smuzhiyun }
2575*4882a593Smuzhiyun 
break_deleg_wait(struct inode ** delegated_inode)2576*4882a593Smuzhiyun static inline int break_deleg_wait(struct inode **delegated_inode)
2577*4882a593Smuzhiyun {
2578*4882a593Smuzhiyun 	BUG();
2579*4882a593Smuzhiyun 	return 0;
2580*4882a593Smuzhiyun }
2581*4882a593Smuzhiyun 
break_layout(struct inode * inode,bool wait)2582*4882a593Smuzhiyun static inline int break_layout(struct inode *inode, bool wait)
2583*4882a593Smuzhiyun {
2584*4882a593Smuzhiyun 	return 0;
2585*4882a593Smuzhiyun }
2586*4882a593Smuzhiyun 
2587*4882a593Smuzhiyun #endif /* CONFIG_FILE_LOCKING */
2588*4882a593Smuzhiyun 
2589*4882a593Smuzhiyun /* fs/open.c */
2590*4882a593Smuzhiyun struct audit_names;
2591*4882a593Smuzhiyun struct filename {
2592*4882a593Smuzhiyun 	const char		*name;	/* pointer to actual string */
2593*4882a593Smuzhiyun 	const __user char	*uptr;	/* original userland pointer */
2594*4882a593Smuzhiyun 	int			refcnt;
2595*4882a593Smuzhiyun 	struct audit_names	*aname;
2596*4882a593Smuzhiyun 	const char		iname[];
2597*4882a593Smuzhiyun };
2598*4882a593Smuzhiyun static_assert(offsetof(struct filename, iname) % sizeof(long) == 0);
2599*4882a593Smuzhiyun 
2600*4882a593Smuzhiyun extern long vfs_truncate(const struct path *, loff_t);
2601*4882a593Smuzhiyun extern int do_truncate(struct dentry *, loff_t start, unsigned int time_attrs,
2602*4882a593Smuzhiyun 		       struct file *filp);
2603*4882a593Smuzhiyun extern int vfs_fallocate(struct file *file, int mode, loff_t offset,
2604*4882a593Smuzhiyun 			loff_t len);
2605*4882a593Smuzhiyun extern long do_sys_open(int dfd, const char __user *filename, int flags,
2606*4882a593Smuzhiyun 			umode_t mode);
2607*4882a593Smuzhiyun extern struct file *file_open_name(struct filename *, int, umode_t);
2608*4882a593Smuzhiyun extern struct file *filp_open(const char *, int, umode_t);
2609*4882a593Smuzhiyun extern struct file *filp_open_block(const char *, int, umode_t);
2610*4882a593Smuzhiyun extern struct file *file_open_root(struct dentry *, struct vfsmount *,
2611*4882a593Smuzhiyun 				   const char *, int, umode_t);
2612*4882a593Smuzhiyun extern struct file * dentry_open(const struct path *, int, const struct cred *);
2613*4882a593Smuzhiyun extern struct file * open_with_fake_path(const struct path *, int,
2614*4882a593Smuzhiyun 					 struct inode*, const struct cred *);
file_clone_open(struct file * file)2615*4882a593Smuzhiyun static inline struct file *file_clone_open(struct file *file)
2616*4882a593Smuzhiyun {
2617*4882a593Smuzhiyun 	return dentry_open(&file->f_path, file->f_flags, file->f_cred);
2618*4882a593Smuzhiyun }
2619*4882a593Smuzhiyun extern int filp_close(struct file *, fl_owner_t id);
2620*4882a593Smuzhiyun 
2621*4882a593Smuzhiyun extern struct filename *getname_flags(const char __user *, int, int *);
2622*4882a593Smuzhiyun extern struct filename *getname(const char __user *);
2623*4882a593Smuzhiyun extern struct filename *getname_kernel(const char *);
2624*4882a593Smuzhiyun extern void putname(struct filename *name);
2625*4882a593Smuzhiyun 
2626*4882a593Smuzhiyun extern int finish_open(struct file *file, struct dentry *dentry,
2627*4882a593Smuzhiyun 			int (*open)(struct inode *, struct file *));
2628*4882a593Smuzhiyun extern int finish_no_open(struct file *file, struct dentry *dentry);
2629*4882a593Smuzhiyun 
2630*4882a593Smuzhiyun /* fs/dcache.c */
2631*4882a593Smuzhiyun extern void __init vfs_caches_init_early(void);
2632*4882a593Smuzhiyun extern void __init vfs_caches_init(void);
2633*4882a593Smuzhiyun 
2634*4882a593Smuzhiyun extern struct kmem_cache *names_cachep;
2635*4882a593Smuzhiyun 
2636*4882a593Smuzhiyun #define __getname()		kmem_cache_alloc(names_cachep, GFP_KERNEL)
2637*4882a593Smuzhiyun #define __putname(name)		kmem_cache_free(names_cachep, (void *)(name))
2638*4882a593Smuzhiyun 
2639*4882a593Smuzhiyun extern struct super_block *blockdev_superblock;
sb_is_blkdev_sb(struct super_block * sb)2640*4882a593Smuzhiyun static inline bool sb_is_blkdev_sb(struct super_block *sb)
2641*4882a593Smuzhiyun {
2642*4882a593Smuzhiyun 	return IS_ENABLED(CONFIG_BLOCK) && sb == blockdev_superblock;
2643*4882a593Smuzhiyun }
2644*4882a593Smuzhiyun 
2645*4882a593Smuzhiyun void emergency_thaw_all(void);
2646*4882a593Smuzhiyun extern int sync_filesystem(struct super_block *);
2647*4882a593Smuzhiyun extern const struct file_operations def_blk_fops;
2648*4882a593Smuzhiyun extern const struct file_operations def_chr_fops;
2649*4882a593Smuzhiyun 
2650*4882a593Smuzhiyun /* fs/char_dev.c */
2651*4882a593Smuzhiyun #define CHRDEV_MAJOR_MAX 512
2652*4882a593Smuzhiyun /* Marks the bottom of the first segment of free char majors */
2653*4882a593Smuzhiyun #define CHRDEV_MAJOR_DYN_END 234
2654*4882a593Smuzhiyun /* Marks the top and bottom of the second segment of free char majors */
2655*4882a593Smuzhiyun #define CHRDEV_MAJOR_DYN_EXT_START 511
2656*4882a593Smuzhiyun #define CHRDEV_MAJOR_DYN_EXT_END 384
2657*4882a593Smuzhiyun 
2658*4882a593Smuzhiyun extern int alloc_chrdev_region(dev_t *, unsigned, unsigned, const char *);
2659*4882a593Smuzhiyun extern int register_chrdev_region(dev_t, unsigned, const char *);
2660*4882a593Smuzhiyun extern int __register_chrdev(unsigned int major, unsigned int baseminor,
2661*4882a593Smuzhiyun 			     unsigned int count, const char *name,
2662*4882a593Smuzhiyun 			     const struct file_operations *fops);
2663*4882a593Smuzhiyun extern void __unregister_chrdev(unsigned int major, unsigned int baseminor,
2664*4882a593Smuzhiyun 				unsigned int count, const char *name);
2665*4882a593Smuzhiyun extern void unregister_chrdev_region(dev_t, unsigned);
2666*4882a593Smuzhiyun extern void chrdev_show(struct seq_file *,off_t);
2667*4882a593Smuzhiyun 
register_chrdev(unsigned int major,const char * name,const struct file_operations * fops)2668*4882a593Smuzhiyun static inline int register_chrdev(unsigned int major, const char *name,
2669*4882a593Smuzhiyun 				  const struct file_operations *fops)
2670*4882a593Smuzhiyun {
2671*4882a593Smuzhiyun 	return __register_chrdev(major, 0, 256, name, fops);
2672*4882a593Smuzhiyun }
2673*4882a593Smuzhiyun 
unregister_chrdev(unsigned int major,const char * name)2674*4882a593Smuzhiyun static inline void unregister_chrdev(unsigned int major, const char *name)
2675*4882a593Smuzhiyun {
2676*4882a593Smuzhiyun 	__unregister_chrdev(major, 0, 256, name);
2677*4882a593Smuzhiyun }
2678*4882a593Smuzhiyun 
2679*4882a593Smuzhiyun extern void init_special_inode(struct inode *, umode_t, dev_t);
2680*4882a593Smuzhiyun 
2681*4882a593Smuzhiyun /* Invalid inode operations -- fs/bad_inode.c */
2682*4882a593Smuzhiyun extern void make_bad_inode(struct inode *);
2683*4882a593Smuzhiyun extern bool is_bad_inode(struct inode *);
2684*4882a593Smuzhiyun 
2685*4882a593Smuzhiyun unsigned long invalidate_mapping_pages(struct address_space *mapping,
2686*4882a593Smuzhiyun 					pgoff_t start, pgoff_t end);
2687*4882a593Smuzhiyun 
2688*4882a593Smuzhiyun void invalidate_mapping_pagevec(struct address_space *mapping,
2689*4882a593Smuzhiyun 				pgoff_t start, pgoff_t end,
2690*4882a593Smuzhiyun 				unsigned long *nr_pagevec);
2691*4882a593Smuzhiyun 
invalidate_remote_inode(struct inode * inode)2692*4882a593Smuzhiyun static inline void invalidate_remote_inode(struct inode *inode)
2693*4882a593Smuzhiyun {
2694*4882a593Smuzhiyun 	if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
2695*4882a593Smuzhiyun 	    S_ISLNK(inode->i_mode))
2696*4882a593Smuzhiyun 		invalidate_mapping_pages(inode->i_mapping, 0, -1);
2697*4882a593Smuzhiyun }
2698*4882a593Smuzhiyun extern int invalidate_inode_pages2(struct address_space *mapping);
2699*4882a593Smuzhiyun extern int invalidate_inode_pages2_range(struct address_space *mapping,
2700*4882a593Smuzhiyun 					 pgoff_t start, pgoff_t end);
2701*4882a593Smuzhiyun extern int write_inode_now(struct inode *, int);
2702*4882a593Smuzhiyun extern int filemap_fdatawrite(struct address_space *);
2703*4882a593Smuzhiyun extern int filemap_flush(struct address_space *);
2704*4882a593Smuzhiyun extern int filemap_fdatawait_keep_errors(struct address_space *mapping);
2705*4882a593Smuzhiyun extern int filemap_fdatawait_range(struct address_space *, loff_t lstart,
2706*4882a593Smuzhiyun 				   loff_t lend);
2707*4882a593Smuzhiyun extern int filemap_fdatawait_range_keep_errors(struct address_space *mapping,
2708*4882a593Smuzhiyun 		loff_t start_byte, loff_t end_byte);
2709*4882a593Smuzhiyun 
filemap_fdatawait(struct address_space * mapping)2710*4882a593Smuzhiyun static inline int filemap_fdatawait(struct address_space *mapping)
2711*4882a593Smuzhiyun {
2712*4882a593Smuzhiyun 	return filemap_fdatawait_range(mapping, 0, LLONG_MAX);
2713*4882a593Smuzhiyun }
2714*4882a593Smuzhiyun 
2715*4882a593Smuzhiyun extern bool filemap_range_has_page(struct address_space *, loff_t lstart,
2716*4882a593Smuzhiyun 				  loff_t lend);
2717*4882a593Smuzhiyun extern int filemap_write_and_wait_range(struct address_space *mapping,
2718*4882a593Smuzhiyun 				        loff_t lstart, loff_t lend);
2719*4882a593Smuzhiyun extern int __filemap_fdatawrite_range(struct address_space *mapping,
2720*4882a593Smuzhiyun 				loff_t start, loff_t end, int sync_mode);
2721*4882a593Smuzhiyun extern int filemap_fdatawrite_range(struct address_space *mapping,
2722*4882a593Smuzhiyun 				loff_t start, loff_t end);
2723*4882a593Smuzhiyun extern int filemap_check_errors(struct address_space *mapping);
2724*4882a593Smuzhiyun extern void __filemap_set_wb_err(struct address_space *mapping, int err);
2725*4882a593Smuzhiyun 
filemap_write_and_wait(struct address_space * mapping)2726*4882a593Smuzhiyun static inline int filemap_write_and_wait(struct address_space *mapping)
2727*4882a593Smuzhiyun {
2728*4882a593Smuzhiyun 	return filemap_write_and_wait_range(mapping, 0, LLONG_MAX);
2729*4882a593Smuzhiyun }
2730*4882a593Smuzhiyun 
2731*4882a593Smuzhiyun extern int __must_check file_fdatawait_range(struct file *file, loff_t lstart,
2732*4882a593Smuzhiyun 						loff_t lend);
2733*4882a593Smuzhiyun extern int __must_check file_check_and_advance_wb_err(struct file *file);
2734*4882a593Smuzhiyun extern int __must_check file_write_and_wait_range(struct file *file,
2735*4882a593Smuzhiyun 						loff_t start, loff_t end);
2736*4882a593Smuzhiyun 
file_write_and_wait(struct file * file)2737*4882a593Smuzhiyun static inline int file_write_and_wait(struct file *file)
2738*4882a593Smuzhiyun {
2739*4882a593Smuzhiyun 	return file_write_and_wait_range(file, 0, LLONG_MAX);
2740*4882a593Smuzhiyun }
2741*4882a593Smuzhiyun 
2742*4882a593Smuzhiyun /**
2743*4882a593Smuzhiyun  * filemap_set_wb_err - set a writeback error on an address_space
2744*4882a593Smuzhiyun  * @mapping: mapping in which to set writeback error
2745*4882a593Smuzhiyun  * @err: error to be set in mapping
2746*4882a593Smuzhiyun  *
2747*4882a593Smuzhiyun  * When writeback fails in some way, we must record that error so that
2748*4882a593Smuzhiyun  * userspace can be informed when fsync and the like are called.  We endeavor
2749*4882a593Smuzhiyun  * to report errors on any file that was open at the time of the error.  Some
2750*4882a593Smuzhiyun  * internal callers also need to know when writeback errors have occurred.
2751*4882a593Smuzhiyun  *
2752*4882a593Smuzhiyun  * When a writeback error occurs, most filesystems will want to call
2753*4882a593Smuzhiyun  * filemap_set_wb_err to record the error in the mapping so that it will be
2754*4882a593Smuzhiyun  * automatically reported whenever fsync is called on the file.
2755*4882a593Smuzhiyun  */
filemap_set_wb_err(struct address_space * mapping,int err)2756*4882a593Smuzhiyun static inline void filemap_set_wb_err(struct address_space *mapping, int err)
2757*4882a593Smuzhiyun {
2758*4882a593Smuzhiyun 	/* Fastpath for common case of no error */
2759*4882a593Smuzhiyun 	if (unlikely(err))
2760*4882a593Smuzhiyun 		__filemap_set_wb_err(mapping, err);
2761*4882a593Smuzhiyun }
2762*4882a593Smuzhiyun 
2763*4882a593Smuzhiyun /**
2764*4882a593Smuzhiyun  * filemap_check_wb_err - has an error occurred since the mark was sampled?
2765*4882a593Smuzhiyun  * @mapping: mapping to check for writeback errors
2766*4882a593Smuzhiyun  * @since: previously-sampled errseq_t
2767*4882a593Smuzhiyun  *
2768*4882a593Smuzhiyun  * Grab the errseq_t value from the mapping, and see if it has changed "since"
2769*4882a593Smuzhiyun  * the given value was sampled.
2770*4882a593Smuzhiyun  *
2771*4882a593Smuzhiyun  * If it has then report the latest error set, otherwise return 0.
2772*4882a593Smuzhiyun  */
filemap_check_wb_err(struct address_space * mapping,errseq_t since)2773*4882a593Smuzhiyun static inline int filemap_check_wb_err(struct address_space *mapping,
2774*4882a593Smuzhiyun 					errseq_t since)
2775*4882a593Smuzhiyun {
2776*4882a593Smuzhiyun 	return errseq_check(&mapping->wb_err, since);
2777*4882a593Smuzhiyun }
2778*4882a593Smuzhiyun 
2779*4882a593Smuzhiyun /**
2780*4882a593Smuzhiyun  * filemap_sample_wb_err - sample the current errseq_t to test for later errors
2781*4882a593Smuzhiyun  * @mapping: mapping to be sampled
2782*4882a593Smuzhiyun  *
2783*4882a593Smuzhiyun  * Writeback errors are always reported relative to a particular sample point
2784*4882a593Smuzhiyun  * in the past. This function provides those sample points.
2785*4882a593Smuzhiyun  */
filemap_sample_wb_err(struct address_space * mapping)2786*4882a593Smuzhiyun static inline errseq_t filemap_sample_wb_err(struct address_space *mapping)
2787*4882a593Smuzhiyun {
2788*4882a593Smuzhiyun 	return errseq_sample(&mapping->wb_err);
2789*4882a593Smuzhiyun }
2790*4882a593Smuzhiyun 
2791*4882a593Smuzhiyun /**
2792*4882a593Smuzhiyun  * file_sample_sb_err - sample the current errseq_t to test for later errors
2793*4882a593Smuzhiyun  * @file: file pointer to be sampled
2794*4882a593Smuzhiyun  *
2795*4882a593Smuzhiyun  * Grab the most current superblock-level errseq_t value for the given
2796*4882a593Smuzhiyun  * struct file.
2797*4882a593Smuzhiyun  */
file_sample_sb_err(struct file * file)2798*4882a593Smuzhiyun static inline errseq_t file_sample_sb_err(struct file *file)
2799*4882a593Smuzhiyun {
2800*4882a593Smuzhiyun 	return errseq_sample(&file->f_path.dentry->d_sb->s_wb_err);
2801*4882a593Smuzhiyun }
2802*4882a593Smuzhiyun 
2803*4882a593Smuzhiyun extern int vfs_fsync_range(struct file *file, loff_t start, loff_t end,
2804*4882a593Smuzhiyun 			   int datasync);
2805*4882a593Smuzhiyun extern int vfs_fsync(struct file *file, int datasync);
2806*4882a593Smuzhiyun 
2807*4882a593Smuzhiyun extern int sync_file_range(struct file *file, loff_t offset, loff_t nbytes,
2808*4882a593Smuzhiyun 				unsigned int flags);
2809*4882a593Smuzhiyun 
2810*4882a593Smuzhiyun /*
2811*4882a593Smuzhiyun  * Sync the bytes written if this was a synchronous write.  Expect ki_pos
2812*4882a593Smuzhiyun  * to already be updated for the write, and will return either the amount
2813*4882a593Smuzhiyun  * of bytes passed in, or an error if syncing the file failed.
2814*4882a593Smuzhiyun  */
generic_write_sync(struct kiocb * iocb,ssize_t count)2815*4882a593Smuzhiyun static inline ssize_t generic_write_sync(struct kiocb *iocb, ssize_t count)
2816*4882a593Smuzhiyun {
2817*4882a593Smuzhiyun 	if (iocb->ki_flags & IOCB_DSYNC) {
2818*4882a593Smuzhiyun 		int ret = vfs_fsync_range(iocb->ki_filp,
2819*4882a593Smuzhiyun 				iocb->ki_pos - count, iocb->ki_pos - 1,
2820*4882a593Smuzhiyun 				(iocb->ki_flags & IOCB_SYNC) ? 0 : 1);
2821*4882a593Smuzhiyun 		if (ret)
2822*4882a593Smuzhiyun 			return ret;
2823*4882a593Smuzhiyun 	}
2824*4882a593Smuzhiyun 
2825*4882a593Smuzhiyun 	return count;
2826*4882a593Smuzhiyun }
2827*4882a593Smuzhiyun 
2828*4882a593Smuzhiyun extern void emergency_sync(void);
2829*4882a593Smuzhiyun extern void emergency_remount(void);
2830*4882a593Smuzhiyun 
2831*4882a593Smuzhiyun #ifdef CONFIG_BLOCK
2832*4882a593Smuzhiyun extern int bmap(struct inode *inode, sector_t *block);
2833*4882a593Smuzhiyun #else
bmap(struct inode * inode,sector_t * block)2834*4882a593Smuzhiyun static inline int bmap(struct inode *inode,  sector_t *block)
2835*4882a593Smuzhiyun {
2836*4882a593Smuzhiyun 	return -EINVAL;
2837*4882a593Smuzhiyun }
2838*4882a593Smuzhiyun #endif
2839*4882a593Smuzhiyun 
2840*4882a593Smuzhiyun extern int notify_change(struct dentry *, struct iattr *, struct inode **);
2841*4882a593Smuzhiyun extern int inode_permission(struct inode *, int);
2842*4882a593Smuzhiyun extern int generic_permission(struct inode *, int);
2843*4882a593Smuzhiyun extern int __check_sticky(struct inode *dir, struct inode *inode);
2844*4882a593Smuzhiyun 
execute_ok(struct inode * inode)2845*4882a593Smuzhiyun static inline bool execute_ok(struct inode *inode)
2846*4882a593Smuzhiyun {
2847*4882a593Smuzhiyun 	return (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode);
2848*4882a593Smuzhiyun }
2849*4882a593Smuzhiyun 
inode_wrong_type(const struct inode * inode,umode_t mode)2850*4882a593Smuzhiyun static inline bool inode_wrong_type(const struct inode *inode, umode_t mode)
2851*4882a593Smuzhiyun {
2852*4882a593Smuzhiyun 	return (inode->i_mode ^ mode) & S_IFMT;
2853*4882a593Smuzhiyun }
2854*4882a593Smuzhiyun 
file_start_write(struct file * file)2855*4882a593Smuzhiyun static inline void file_start_write(struct file *file)
2856*4882a593Smuzhiyun {
2857*4882a593Smuzhiyun 	if (!S_ISREG(file_inode(file)->i_mode))
2858*4882a593Smuzhiyun 		return;
2859*4882a593Smuzhiyun 	sb_start_write(file_inode(file)->i_sb);
2860*4882a593Smuzhiyun }
2861*4882a593Smuzhiyun 
file_start_write_trylock(struct file * file)2862*4882a593Smuzhiyun static inline bool file_start_write_trylock(struct file *file)
2863*4882a593Smuzhiyun {
2864*4882a593Smuzhiyun 	if (!S_ISREG(file_inode(file)->i_mode))
2865*4882a593Smuzhiyun 		return true;
2866*4882a593Smuzhiyun 	return sb_start_write_trylock(file_inode(file)->i_sb);
2867*4882a593Smuzhiyun }
2868*4882a593Smuzhiyun 
file_end_write(struct file * file)2869*4882a593Smuzhiyun static inline void file_end_write(struct file *file)
2870*4882a593Smuzhiyun {
2871*4882a593Smuzhiyun 	if (!S_ISREG(file_inode(file)->i_mode))
2872*4882a593Smuzhiyun 		return;
2873*4882a593Smuzhiyun 	__sb_end_write(file_inode(file)->i_sb, SB_FREEZE_WRITE);
2874*4882a593Smuzhiyun }
2875*4882a593Smuzhiyun 
2876*4882a593Smuzhiyun /*
2877*4882a593Smuzhiyun  * get_write_access() gets write permission for a file.
2878*4882a593Smuzhiyun  * put_write_access() releases this write permission.
2879*4882a593Smuzhiyun  * This is used for regular files.
2880*4882a593Smuzhiyun  * We cannot support write (and maybe mmap read-write shared) accesses and
2881*4882a593Smuzhiyun  * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
2882*4882a593Smuzhiyun  * can have the following values:
2883*4882a593Smuzhiyun  * 0: no writers, no VM_DENYWRITE mappings
2884*4882a593Smuzhiyun  * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
2885*4882a593Smuzhiyun  * > 0: (i_writecount) users are writing to the file.
2886*4882a593Smuzhiyun  *
2887*4882a593Smuzhiyun  * Normally we operate on that counter with atomic_{inc,dec} and it's safe
2888*4882a593Smuzhiyun  * except for the cases where we don't hold i_writecount yet. Then we need to
2889*4882a593Smuzhiyun  * use {get,deny}_write_access() - these functions check the sign and refuse
2890*4882a593Smuzhiyun  * to do the change if sign is wrong.
2891*4882a593Smuzhiyun  */
get_write_access(struct inode * inode)2892*4882a593Smuzhiyun static inline int get_write_access(struct inode *inode)
2893*4882a593Smuzhiyun {
2894*4882a593Smuzhiyun 	return atomic_inc_unless_negative(&inode->i_writecount) ? 0 : -ETXTBSY;
2895*4882a593Smuzhiyun }
deny_write_access(struct file * file)2896*4882a593Smuzhiyun static inline int deny_write_access(struct file *file)
2897*4882a593Smuzhiyun {
2898*4882a593Smuzhiyun 	struct inode *inode = file_inode(file);
2899*4882a593Smuzhiyun 	return atomic_dec_unless_positive(&inode->i_writecount) ? 0 : -ETXTBSY;
2900*4882a593Smuzhiyun }
put_write_access(struct inode * inode)2901*4882a593Smuzhiyun static inline void put_write_access(struct inode * inode)
2902*4882a593Smuzhiyun {
2903*4882a593Smuzhiyun 	atomic_dec(&inode->i_writecount);
2904*4882a593Smuzhiyun }
allow_write_access(struct file * file)2905*4882a593Smuzhiyun static inline void allow_write_access(struct file *file)
2906*4882a593Smuzhiyun {
2907*4882a593Smuzhiyun 	if (file)
2908*4882a593Smuzhiyun 		atomic_inc(&file_inode(file)->i_writecount);
2909*4882a593Smuzhiyun }
inode_is_open_for_write(const struct inode * inode)2910*4882a593Smuzhiyun static inline bool inode_is_open_for_write(const struct inode *inode)
2911*4882a593Smuzhiyun {
2912*4882a593Smuzhiyun 	return atomic_read(&inode->i_writecount) > 0;
2913*4882a593Smuzhiyun }
2914*4882a593Smuzhiyun 
2915*4882a593Smuzhiyun #if defined(CONFIG_IMA) || defined(CONFIG_FILE_LOCKING)
i_readcount_dec(struct inode * inode)2916*4882a593Smuzhiyun static inline void i_readcount_dec(struct inode *inode)
2917*4882a593Smuzhiyun {
2918*4882a593Smuzhiyun 	BUG_ON(!atomic_read(&inode->i_readcount));
2919*4882a593Smuzhiyun 	atomic_dec(&inode->i_readcount);
2920*4882a593Smuzhiyun }
i_readcount_inc(struct inode * inode)2921*4882a593Smuzhiyun static inline void i_readcount_inc(struct inode *inode)
2922*4882a593Smuzhiyun {
2923*4882a593Smuzhiyun 	atomic_inc(&inode->i_readcount);
2924*4882a593Smuzhiyun }
2925*4882a593Smuzhiyun #else
i_readcount_dec(struct inode * inode)2926*4882a593Smuzhiyun static inline void i_readcount_dec(struct inode *inode)
2927*4882a593Smuzhiyun {
2928*4882a593Smuzhiyun 	return;
2929*4882a593Smuzhiyun }
i_readcount_inc(struct inode * inode)2930*4882a593Smuzhiyun static inline void i_readcount_inc(struct inode *inode)
2931*4882a593Smuzhiyun {
2932*4882a593Smuzhiyun 	return;
2933*4882a593Smuzhiyun }
2934*4882a593Smuzhiyun #endif
2935*4882a593Smuzhiyun extern int do_pipe_flags(int *, int);
2936*4882a593Smuzhiyun 
2937*4882a593Smuzhiyun extern ssize_t kernel_read(struct file *, void *, size_t, loff_t *);
2938*4882a593Smuzhiyun ssize_t __kernel_read(struct file *file, void *buf, size_t count, loff_t *pos);
2939*4882a593Smuzhiyun extern ssize_t kernel_write(struct file *, const void *, size_t, loff_t *);
2940*4882a593Smuzhiyun extern ssize_t __kernel_write(struct file *, const void *, size_t, loff_t *);
2941*4882a593Smuzhiyun extern struct file * open_exec(const char *);
2942*4882a593Smuzhiyun 
2943*4882a593Smuzhiyun /* fs/dcache.c -- generic fs support functions */
2944*4882a593Smuzhiyun extern bool is_subdir(struct dentry *, struct dentry *);
2945*4882a593Smuzhiyun extern bool path_is_under(const struct path *, const struct path *);
2946*4882a593Smuzhiyun 
2947*4882a593Smuzhiyun extern char *file_path(struct file *, char *, int);
2948*4882a593Smuzhiyun 
2949*4882a593Smuzhiyun #include <linux/err.h>
2950*4882a593Smuzhiyun 
2951*4882a593Smuzhiyun /* needed for stackable file system support */
2952*4882a593Smuzhiyun extern loff_t default_llseek(struct file *file, loff_t offset, int whence);
2953*4882a593Smuzhiyun 
2954*4882a593Smuzhiyun extern loff_t vfs_llseek(struct file *file, loff_t offset, int whence);
2955*4882a593Smuzhiyun 
2956*4882a593Smuzhiyun extern int inode_init_always(struct super_block *, struct inode *);
2957*4882a593Smuzhiyun extern void inode_init_once(struct inode *);
2958*4882a593Smuzhiyun extern void address_space_init_once(struct address_space *mapping);
2959*4882a593Smuzhiyun extern struct inode * igrab(struct inode *);
2960*4882a593Smuzhiyun extern ino_t iunique(struct super_block *, ino_t);
2961*4882a593Smuzhiyun extern int inode_needs_sync(struct inode *inode);
2962*4882a593Smuzhiyun extern int generic_delete_inode(struct inode *inode);
generic_drop_inode(struct inode * inode)2963*4882a593Smuzhiyun static inline int generic_drop_inode(struct inode *inode)
2964*4882a593Smuzhiyun {
2965*4882a593Smuzhiyun 	return !inode->i_nlink || inode_unhashed(inode);
2966*4882a593Smuzhiyun }
2967*4882a593Smuzhiyun extern void d_mark_dontcache(struct inode *inode);
2968*4882a593Smuzhiyun 
2969*4882a593Smuzhiyun extern struct inode *ilookup5_nowait(struct super_block *sb,
2970*4882a593Smuzhiyun 		unsigned long hashval, int (*test)(struct inode *, void *),
2971*4882a593Smuzhiyun 		void *data);
2972*4882a593Smuzhiyun extern struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
2973*4882a593Smuzhiyun 		int (*test)(struct inode *, void *), void *data);
2974*4882a593Smuzhiyun extern struct inode *ilookup(struct super_block *sb, unsigned long ino);
2975*4882a593Smuzhiyun 
2976*4882a593Smuzhiyun extern struct inode *inode_insert5(struct inode *inode, unsigned long hashval,
2977*4882a593Smuzhiyun 		int (*test)(struct inode *, void *),
2978*4882a593Smuzhiyun 		int (*set)(struct inode *, void *),
2979*4882a593Smuzhiyun 		void *data);
2980*4882a593Smuzhiyun extern struct inode * iget5_locked(struct super_block *, unsigned long, int (*test)(struct inode *, void *), int (*set)(struct inode *, void *), void *);
2981*4882a593Smuzhiyun extern struct inode * iget_locked(struct super_block *, unsigned long);
2982*4882a593Smuzhiyun extern struct inode *find_inode_nowait(struct super_block *,
2983*4882a593Smuzhiyun 				       unsigned long,
2984*4882a593Smuzhiyun 				       int (*match)(struct inode *,
2985*4882a593Smuzhiyun 						    unsigned long, void *),
2986*4882a593Smuzhiyun 				       void *data);
2987*4882a593Smuzhiyun extern struct inode *find_inode_rcu(struct super_block *, unsigned long,
2988*4882a593Smuzhiyun 				    int (*)(struct inode *, void *), void *);
2989*4882a593Smuzhiyun extern struct inode *find_inode_by_ino_rcu(struct super_block *, unsigned long);
2990*4882a593Smuzhiyun extern int insert_inode_locked4(struct inode *, unsigned long, int (*test)(struct inode *, void *), void *);
2991*4882a593Smuzhiyun extern int insert_inode_locked(struct inode *);
2992*4882a593Smuzhiyun #ifdef CONFIG_DEBUG_LOCK_ALLOC
2993*4882a593Smuzhiyun extern void lockdep_annotate_inode_mutex_key(struct inode *inode);
2994*4882a593Smuzhiyun #else
lockdep_annotate_inode_mutex_key(struct inode * inode)2995*4882a593Smuzhiyun static inline void lockdep_annotate_inode_mutex_key(struct inode *inode) { };
2996*4882a593Smuzhiyun #endif
2997*4882a593Smuzhiyun extern void unlock_new_inode(struct inode *);
2998*4882a593Smuzhiyun extern void discard_new_inode(struct inode *);
2999*4882a593Smuzhiyun extern unsigned int get_next_ino(void);
3000*4882a593Smuzhiyun extern void evict_inodes(struct super_block *sb);
3001*4882a593Smuzhiyun 
3002*4882a593Smuzhiyun /*
3003*4882a593Smuzhiyun  * Userspace may rely on the the inode number being non-zero. For example, glibc
3004*4882a593Smuzhiyun  * simply ignores files with zero i_ino in unlink() and other places.
3005*4882a593Smuzhiyun  *
3006*4882a593Smuzhiyun  * As an additional complication, if userspace was compiled with
3007*4882a593Smuzhiyun  * _FILE_OFFSET_BITS=32 on a 64-bit kernel we'll only end up reading out the
3008*4882a593Smuzhiyun  * lower 32 bits, so we need to check that those aren't zero explicitly. With
3009*4882a593Smuzhiyun  * _FILE_OFFSET_BITS=64, this may cause some harmless false-negatives, but
3010*4882a593Smuzhiyun  * better safe than sorry.
3011*4882a593Smuzhiyun  */
is_zero_ino(ino_t ino)3012*4882a593Smuzhiyun static inline bool is_zero_ino(ino_t ino)
3013*4882a593Smuzhiyun {
3014*4882a593Smuzhiyun 	return (u32)ino == 0;
3015*4882a593Smuzhiyun }
3016*4882a593Smuzhiyun 
3017*4882a593Smuzhiyun extern void __iget(struct inode * inode);
3018*4882a593Smuzhiyun extern void iget_failed(struct inode *);
3019*4882a593Smuzhiyun extern void clear_inode(struct inode *);
3020*4882a593Smuzhiyun extern void __destroy_inode(struct inode *);
3021*4882a593Smuzhiyun extern struct inode *new_inode_pseudo(struct super_block *sb);
3022*4882a593Smuzhiyun extern struct inode *new_inode(struct super_block *sb);
3023*4882a593Smuzhiyun extern void free_inode_nonrcu(struct inode *inode);
3024*4882a593Smuzhiyun extern int should_remove_suid(struct dentry *);
3025*4882a593Smuzhiyun extern int file_remove_privs(struct file *);
3026*4882a593Smuzhiyun 
3027*4882a593Smuzhiyun extern void __insert_inode_hash(struct inode *, unsigned long hashval);
insert_inode_hash(struct inode * inode)3028*4882a593Smuzhiyun static inline void insert_inode_hash(struct inode *inode)
3029*4882a593Smuzhiyun {
3030*4882a593Smuzhiyun 	__insert_inode_hash(inode, inode->i_ino);
3031*4882a593Smuzhiyun }
3032*4882a593Smuzhiyun 
3033*4882a593Smuzhiyun extern void __remove_inode_hash(struct inode *);
remove_inode_hash(struct inode * inode)3034*4882a593Smuzhiyun static inline void remove_inode_hash(struct inode *inode)
3035*4882a593Smuzhiyun {
3036*4882a593Smuzhiyun 	if (!inode_unhashed(inode) && !hlist_fake(&inode->i_hash))
3037*4882a593Smuzhiyun 		__remove_inode_hash(inode);
3038*4882a593Smuzhiyun }
3039*4882a593Smuzhiyun 
3040*4882a593Smuzhiyun extern void inode_sb_list_add(struct inode *inode);
3041*4882a593Smuzhiyun 
3042*4882a593Smuzhiyun extern int sb_set_blocksize(struct super_block *, int);
3043*4882a593Smuzhiyun extern int sb_min_blocksize(struct super_block *, int);
3044*4882a593Smuzhiyun 
3045*4882a593Smuzhiyun extern int generic_file_mmap(struct file *, struct vm_area_struct *);
3046*4882a593Smuzhiyun extern int generic_file_readonly_mmap(struct file *, struct vm_area_struct *);
3047*4882a593Smuzhiyun extern ssize_t generic_write_checks(struct kiocb *, struct iov_iter *);
3048*4882a593Smuzhiyun extern int generic_write_check_limits(struct file *file, loff_t pos,
3049*4882a593Smuzhiyun 		loff_t *count);
3050*4882a593Smuzhiyun extern int generic_file_rw_checks(struct file *file_in, struct file *file_out);
3051*4882a593Smuzhiyun extern ssize_t generic_file_buffered_read(struct kiocb *iocb,
3052*4882a593Smuzhiyun 		struct iov_iter *to, ssize_t already_read);
3053*4882a593Smuzhiyun extern ssize_t generic_file_read_iter(struct kiocb *, struct iov_iter *);
3054*4882a593Smuzhiyun extern ssize_t __generic_file_write_iter(struct kiocb *, struct iov_iter *);
3055*4882a593Smuzhiyun extern ssize_t generic_file_write_iter(struct kiocb *, struct iov_iter *);
3056*4882a593Smuzhiyun extern ssize_t generic_file_direct_write(struct kiocb *, struct iov_iter *);
3057*4882a593Smuzhiyun extern ssize_t generic_perform_write(struct file *, struct iov_iter *, loff_t);
3058*4882a593Smuzhiyun 
3059*4882a593Smuzhiyun ssize_t vfs_iter_read(struct file *file, struct iov_iter *iter, loff_t *ppos,
3060*4882a593Smuzhiyun 		rwf_t flags);
3061*4882a593Smuzhiyun ssize_t vfs_iter_write(struct file *file, struct iov_iter *iter, loff_t *ppos,
3062*4882a593Smuzhiyun 		rwf_t flags);
3063*4882a593Smuzhiyun ssize_t vfs_iocb_iter_read(struct file *file, struct kiocb *iocb,
3064*4882a593Smuzhiyun 			   struct iov_iter *iter);
3065*4882a593Smuzhiyun ssize_t vfs_iocb_iter_write(struct file *file, struct kiocb *iocb,
3066*4882a593Smuzhiyun 			    struct iov_iter *iter);
3067*4882a593Smuzhiyun 
3068*4882a593Smuzhiyun /* fs/block_dev.c */
3069*4882a593Smuzhiyun extern ssize_t blkdev_read_iter(struct kiocb *iocb, struct iov_iter *to);
3070*4882a593Smuzhiyun extern ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from);
3071*4882a593Smuzhiyun extern int blkdev_fsync(struct file *filp, loff_t start, loff_t end,
3072*4882a593Smuzhiyun 			int datasync);
3073*4882a593Smuzhiyun extern void block_sync_page(struct page *page);
3074*4882a593Smuzhiyun 
3075*4882a593Smuzhiyun /* fs/splice.c */
3076*4882a593Smuzhiyun extern ssize_t generic_file_splice_read(struct file *, loff_t *,
3077*4882a593Smuzhiyun 		struct pipe_inode_info *, size_t, unsigned int);
3078*4882a593Smuzhiyun extern ssize_t iter_file_splice_write(struct pipe_inode_info *,
3079*4882a593Smuzhiyun 		struct file *, loff_t *, size_t, unsigned int);
3080*4882a593Smuzhiyun extern ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe,
3081*4882a593Smuzhiyun 		struct file *out, loff_t *, size_t len, unsigned int flags);
3082*4882a593Smuzhiyun extern long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
3083*4882a593Smuzhiyun 		loff_t *opos, size_t len, unsigned int flags);
3084*4882a593Smuzhiyun 
3085*4882a593Smuzhiyun 
3086*4882a593Smuzhiyun extern void
3087*4882a593Smuzhiyun file_ra_state_init(struct file_ra_state *ra, struct address_space *mapping);
3088*4882a593Smuzhiyun extern loff_t noop_llseek(struct file *file, loff_t offset, int whence);
3089*4882a593Smuzhiyun extern loff_t no_llseek(struct file *file, loff_t offset, int whence);
3090*4882a593Smuzhiyun extern loff_t vfs_setpos(struct file *file, loff_t offset, loff_t maxsize);
3091*4882a593Smuzhiyun extern loff_t generic_file_llseek(struct file *file, loff_t offset, int whence);
3092*4882a593Smuzhiyun extern loff_t generic_file_llseek_size(struct file *file, loff_t offset,
3093*4882a593Smuzhiyun 		int whence, loff_t maxsize, loff_t eof);
3094*4882a593Smuzhiyun extern loff_t fixed_size_llseek(struct file *file, loff_t offset,
3095*4882a593Smuzhiyun 		int whence, loff_t size);
3096*4882a593Smuzhiyun extern loff_t no_seek_end_llseek_size(struct file *, loff_t, int, loff_t);
3097*4882a593Smuzhiyun extern loff_t no_seek_end_llseek(struct file *, loff_t, int);
3098*4882a593Smuzhiyun extern int generic_file_open(struct inode * inode, struct file * filp);
3099*4882a593Smuzhiyun extern int nonseekable_open(struct inode * inode, struct file * filp);
3100*4882a593Smuzhiyun extern int stream_open(struct inode * inode, struct file * filp);
3101*4882a593Smuzhiyun 
3102*4882a593Smuzhiyun #ifdef CONFIG_BLOCK
3103*4882a593Smuzhiyun typedef void (dio_submit_t)(struct bio *bio, struct inode *inode,
3104*4882a593Smuzhiyun 			    loff_t file_offset);
3105*4882a593Smuzhiyun 
3106*4882a593Smuzhiyun enum {
3107*4882a593Smuzhiyun 	/* need locking between buffered and direct access */
3108*4882a593Smuzhiyun 	DIO_LOCKING	= 0x01,
3109*4882a593Smuzhiyun 
3110*4882a593Smuzhiyun 	/* filesystem does not support filling holes */
3111*4882a593Smuzhiyun 	DIO_SKIP_HOLES	= 0x02,
3112*4882a593Smuzhiyun };
3113*4882a593Smuzhiyun 
3114*4882a593Smuzhiyun ssize_t __blockdev_direct_IO(struct kiocb *iocb, struct inode *inode,
3115*4882a593Smuzhiyun 			     struct block_device *bdev, struct iov_iter *iter,
3116*4882a593Smuzhiyun 			     get_block_t get_block,
3117*4882a593Smuzhiyun 			     dio_iodone_t end_io, dio_submit_t submit_io,
3118*4882a593Smuzhiyun 			     int flags);
3119*4882a593Smuzhiyun 
blockdev_direct_IO(struct kiocb * iocb,struct inode * inode,struct iov_iter * iter,get_block_t get_block)3120*4882a593Smuzhiyun static inline ssize_t blockdev_direct_IO(struct kiocb *iocb,
3121*4882a593Smuzhiyun 					 struct inode *inode,
3122*4882a593Smuzhiyun 					 struct iov_iter *iter,
3123*4882a593Smuzhiyun 					 get_block_t get_block)
3124*4882a593Smuzhiyun {
3125*4882a593Smuzhiyun 	return __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev, iter,
3126*4882a593Smuzhiyun 			get_block, NULL, NULL, DIO_LOCKING | DIO_SKIP_HOLES);
3127*4882a593Smuzhiyun }
3128*4882a593Smuzhiyun #endif
3129*4882a593Smuzhiyun 
3130*4882a593Smuzhiyun void inode_dio_wait(struct inode *inode);
3131*4882a593Smuzhiyun 
3132*4882a593Smuzhiyun /*
3133*4882a593Smuzhiyun  * inode_dio_begin - signal start of a direct I/O requests
3134*4882a593Smuzhiyun  * @inode: inode the direct I/O happens on
3135*4882a593Smuzhiyun  *
3136*4882a593Smuzhiyun  * This is called once we've finished processing a direct I/O request,
3137*4882a593Smuzhiyun  * and is used to wake up callers waiting for direct I/O to be quiesced.
3138*4882a593Smuzhiyun  */
inode_dio_begin(struct inode * inode)3139*4882a593Smuzhiyun static inline void inode_dio_begin(struct inode *inode)
3140*4882a593Smuzhiyun {
3141*4882a593Smuzhiyun 	atomic_inc(&inode->i_dio_count);
3142*4882a593Smuzhiyun }
3143*4882a593Smuzhiyun 
3144*4882a593Smuzhiyun /*
3145*4882a593Smuzhiyun  * inode_dio_end - signal finish of a direct I/O requests
3146*4882a593Smuzhiyun  * @inode: inode the direct I/O happens on
3147*4882a593Smuzhiyun  *
3148*4882a593Smuzhiyun  * This is called once we've finished processing a direct I/O request,
3149*4882a593Smuzhiyun  * and is used to wake up callers waiting for direct I/O to be quiesced.
3150*4882a593Smuzhiyun  */
inode_dio_end(struct inode * inode)3151*4882a593Smuzhiyun static inline void inode_dio_end(struct inode *inode)
3152*4882a593Smuzhiyun {
3153*4882a593Smuzhiyun 	if (atomic_dec_and_test(&inode->i_dio_count))
3154*4882a593Smuzhiyun 		wake_up_bit(&inode->i_state, __I_DIO_WAKEUP);
3155*4882a593Smuzhiyun }
3156*4882a593Smuzhiyun 
3157*4882a593Smuzhiyun /*
3158*4882a593Smuzhiyun  * Warn about a page cache invalidation failure diring a direct I/O write.
3159*4882a593Smuzhiyun  */
3160*4882a593Smuzhiyun void dio_warn_stale_pagecache(struct file *filp);
3161*4882a593Smuzhiyun 
3162*4882a593Smuzhiyun extern void inode_set_flags(struct inode *inode, unsigned int flags,
3163*4882a593Smuzhiyun 			    unsigned int mask);
3164*4882a593Smuzhiyun 
3165*4882a593Smuzhiyun extern const struct file_operations generic_ro_fops;
3166*4882a593Smuzhiyun 
3167*4882a593Smuzhiyun #define special_file(m) (S_ISCHR(m)||S_ISBLK(m)||S_ISFIFO(m)||S_ISSOCK(m))
3168*4882a593Smuzhiyun 
3169*4882a593Smuzhiyun extern int readlink_copy(char __user *, int, const char *);
3170*4882a593Smuzhiyun extern int page_readlink(struct dentry *, char __user *, int);
3171*4882a593Smuzhiyun extern const char *page_get_link(struct dentry *, struct inode *,
3172*4882a593Smuzhiyun 				 struct delayed_call *);
3173*4882a593Smuzhiyun extern void page_put_link(void *);
3174*4882a593Smuzhiyun extern int __page_symlink(struct inode *inode, const char *symname, int len,
3175*4882a593Smuzhiyun 		int nofs);
3176*4882a593Smuzhiyun extern int page_symlink(struct inode *inode, const char *symname, int len);
3177*4882a593Smuzhiyun extern const struct inode_operations page_symlink_inode_operations;
3178*4882a593Smuzhiyun extern void kfree_link(void *);
3179*4882a593Smuzhiyun extern void generic_fillattr(struct inode *, struct kstat *);
3180*4882a593Smuzhiyun extern int vfs_getattr_nosec(const struct path *, struct kstat *, u32, unsigned int);
3181*4882a593Smuzhiyun extern int vfs_getattr(const struct path *, struct kstat *, u32, unsigned int);
3182*4882a593Smuzhiyun void __inode_add_bytes(struct inode *inode, loff_t bytes);
3183*4882a593Smuzhiyun void inode_add_bytes(struct inode *inode, loff_t bytes);
3184*4882a593Smuzhiyun void __inode_sub_bytes(struct inode *inode, loff_t bytes);
3185*4882a593Smuzhiyun void inode_sub_bytes(struct inode *inode, loff_t bytes);
__inode_get_bytes(struct inode * inode)3186*4882a593Smuzhiyun static inline loff_t __inode_get_bytes(struct inode *inode)
3187*4882a593Smuzhiyun {
3188*4882a593Smuzhiyun 	return (((loff_t)inode->i_blocks) << 9) + inode->i_bytes;
3189*4882a593Smuzhiyun }
3190*4882a593Smuzhiyun loff_t inode_get_bytes(struct inode *inode);
3191*4882a593Smuzhiyun void inode_set_bytes(struct inode *inode, loff_t bytes);
3192*4882a593Smuzhiyun const char *simple_get_link(struct dentry *, struct inode *,
3193*4882a593Smuzhiyun 			    struct delayed_call *);
3194*4882a593Smuzhiyun extern const struct inode_operations simple_symlink_inode_operations;
3195*4882a593Smuzhiyun 
3196*4882a593Smuzhiyun extern int iterate_dir(struct file *, struct dir_context *);
3197*4882a593Smuzhiyun 
3198*4882a593Smuzhiyun int vfs_fstatat(int dfd, const char __user *filename, struct kstat *stat,
3199*4882a593Smuzhiyun 		int flags);
3200*4882a593Smuzhiyun int vfs_fstat(int fd, struct kstat *stat);
3201*4882a593Smuzhiyun 
vfs_stat(const char __user * filename,struct kstat * stat)3202*4882a593Smuzhiyun static inline int vfs_stat(const char __user *filename, struct kstat *stat)
3203*4882a593Smuzhiyun {
3204*4882a593Smuzhiyun 	return vfs_fstatat(AT_FDCWD, filename, stat, 0);
3205*4882a593Smuzhiyun }
vfs_lstat(const char __user * name,struct kstat * stat)3206*4882a593Smuzhiyun static inline int vfs_lstat(const char __user *name, struct kstat *stat)
3207*4882a593Smuzhiyun {
3208*4882a593Smuzhiyun 	return vfs_fstatat(AT_FDCWD, name, stat, AT_SYMLINK_NOFOLLOW);
3209*4882a593Smuzhiyun }
3210*4882a593Smuzhiyun 
3211*4882a593Smuzhiyun extern const char *vfs_get_link(struct dentry *, struct delayed_call *);
3212*4882a593Smuzhiyun extern int vfs_readlink(struct dentry *, char __user *, int);
3213*4882a593Smuzhiyun 
3214*4882a593Smuzhiyun extern struct file_system_type *get_filesystem(struct file_system_type *fs);
3215*4882a593Smuzhiyun extern void put_filesystem(struct file_system_type *fs);
3216*4882a593Smuzhiyun extern struct file_system_type *get_fs_type(const char *name);
3217*4882a593Smuzhiyun extern struct super_block *get_super(struct block_device *);
3218*4882a593Smuzhiyun extern struct super_block *get_super_thawed(struct block_device *);
3219*4882a593Smuzhiyun extern struct super_block *get_super_exclusive_thawed(struct block_device *bdev);
3220*4882a593Smuzhiyun extern struct super_block *get_active_super(struct block_device *bdev);
3221*4882a593Smuzhiyun extern void drop_super(struct super_block *sb);
3222*4882a593Smuzhiyun extern void drop_super_exclusive(struct super_block *sb);
3223*4882a593Smuzhiyun extern void iterate_supers(void (*)(struct super_block *, void *), void *);
3224*4882a593Smuzhiyun extern void iterate_supers_type(struct file_system_type *,
3225*4882a593Smuzhiyun 			        void (*)(struct super_block *, void *), void *);
3226*4882a593Smuzhiyun 
3227*4882a593Smuzhiyun extern int dcache_dir_open(struct inode *, struct file *);
3228*4882a593Smuzhiyun extern int dcache_dir_close(struct inode *, struct file *);
3229*4882a593Smuzhiyun extern loff_t dcache_dir_lseek(struct file *, loff_t, int);
3230*4882a593Smuzhiyun extern int dcache_readdir(struct file *, struct dir_context *);
3231*4882a593Smuzhiyun extern int simple_setattr(struct dentry *, struct iattr *);
3232*4882a593Smuzhiyun extern int simple_getattr(const struct path *, struct kstat *, u32, unsigned int);
3233*4882a593Smuzhiyun extern int simple_statfs(struct dentry *, struct kstatfs *);
3234*4882a593Smuzhiyun extern int simple_open(struct inode *inode, struct file *file);
3235*4882a593Smuzhiyun extern int simple_link(struct dentry *, struct inode *, struct dentry *);
3236*4882a593Smuzhiyun extern int simple_unlink(struct inode *, struct dentry *);
3237*4882a593Smuzhiyun extern int simple_rmdir(struct inode *, struct dentry *);
3238*4882a593Smuzhiyun extern int simple_rename(struct inode *, struct dentry *,
3239*4882a593Smuzhiyun 			 struct inode *, struct dentry *, unsigned int);
3240*4882a593Smuzhiyun extern void simple_recursive_removal(struct dentry *,
3241*4882a593Smuzhiyun                               void (*callback)(struct dentry *));
3242*4882a593Smuzhiyun extern int noop_fsync(struct file *, loff_t, loff_t, int);
3243*4882a593Smuzhiyun extern int noop_set_page_dirty(struct page *page);
3244*4882a593Smuzhiyun extern void noop_invalidatepage(struct page *page, unsigned int offset,
3245*4882a593Smuzhiyun 		unsigned int length);
3246*4882a593Smuzhiyun extern ssize_t noop_direct_IO(struct kiocb *iocb, struct iov_iter *iter);
3247*4882a593Smuzhiyun extern int simple_empty(struct dentry *);
3248*4882a593Smuzhiyun extern int simple_readpage(struct file *file, struct page *page);
3249*4882a593Smuzhiyun extern int simple_write_begin(struct file *file, struct address_space *mapping,
3250*4882a593Smuzhiyun 			loff_t pos, unsigned len, unsigned flags,
3251*4882a593Smuzhiyun 			struct page **pagep, void **fsdata);
3252*4882a593Smuzhiyun extern int simple_write_end(struct file *file, struct address_space *mapping,
3253*4882a593Smuzhiyun 			loff_t pos, unsigned len, unsigned copied,
3254*4882a593Smuzhiyun 			struct page *page, void *fsdata);
3255*4882a593Smuzhiyun extern int always_delete_dentry(const struct dentry *);
3256*4882a593Smuzhiyun extern struct inode *alloc_anon_inode(struct super_block *);
3257*4882a593Smuzhiyun extern int simple_nosetlease(struct file *, long, struct file_lock **, void **);
3258*4882a593Smuzhiyun extern const struct dentry_operations simple_dentry_operations;
3259*4882a593Smuzhiyun 
3260*4882a593Smuzhiyun extern struct dentry *simple_lookup(struct inode *, struct dentry *, unsigned int flags);
3261*4882a593Smuzhiyun extern ssize_t generic_read_dir(struct file *, char __user *, size_t, loff_t *);
3262*4882a593Smuzhiyun extern const struct file_operations simple_dir_operations;
3263*4882a593Smuzhiyun extern const struct inode_operations simple_dir_inode_operations;
3264*4882a593Smuzhiyun extern void make_empty_dir_inode(struct inode *inode);
3265*4882a593Smuzhiyun extern bool is_empty_dir_inode(struct inode *inode);
3266*4882a593Smuzhiyun struct tree_descr { const char *name; const struct file_operations *ops; int mode; };
3267*4882a593Smuzhiyun struct dentry *d_alloc_name(struct dentry *, const char *);
3268*4882a593Smuzhiyun extern int simple_fill_super(struct super_block *, unsigned long,
3269*4882a593Smuzhiyun 			     const struct tree_descr *);
3270*4882a593Smuzhiyun extern int simple_pin_fs(struct file_system_type *, struct vfsmount **mount, int *count);
3271*4882a593Smuzhiyun extern void simple_release_fs(struct vfsmount **mount, int *count);
3272*4882a593Smuzhiyun 
3273*4882a593Smuzhiyun extern ssize_t simple_read_from_buffer(void __user *to, size_t count,
3274*4882a593Smuzhiyun 			loff_t *ppos, const void *from, size_t available);
3275*4882a593Smuzhiyun extern ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos,
3276*4882a593Smuzhiyun 		const void __user *from, size_t count);
3277*4882a593Smuzhiyun 
3278*4882a593Smuzhiyun extern int __generic_file_fsync(struct file *, loff_t, loff_t, int);
3279*4882a593Smuzhiyun extern int generic_file_fsync(struct file *, loff_t, loff_t, int);
3280*4882a593Smuzhiyun 
3281*4882a593Smuzhiyun extern int generic_check_addressable(unsigned, u64);
3282*4882a593Smuzhiyun 
3283*4882a593Smuzhiyun extern void generic_set_encrypted_ci_d_ops(struct dentry *dentry);
3284*4882a593Smuzhiyun 
3285*4882a593Smuzhiyun #ifdef CONFIG_MIGRATION
3286*4882a593Smuzhiyun extern int buffer_migrate_page(struct address_space *,
3287*4882a593Smuzhiyun 				struct page *, struct page *,
3288*4882a593Smuzhiyun 				enum migrate_mode);
3289*4882a593Smuzhiyun extern int buffer_migrate_page_norefs(struct address_space *,
3290*4882a593Smuzhiyun 				struct page *, struct page *,
3291*4882a593Smuzhiyun 				enum migrate_mode);
3292*4882a593Smuzhiyun #else
3293*4882a593Smuzhiyun #define buffer_migrate_page NULL
3294*4882a593Smuzhiyun #define buffer_migrate_page_norefs NULL
3295*4882a593Smuzhiyun #endif
3296*4882a593Smuzhiyun 
3297*4882a593Smuzhiyun extern int setattr_prepare(struct dentry *, struct iattr *);
3298*4882a593Smuzhiyun extern int inode_newsize_ok(const struct inode *, loff_t offset);
3299*4882a593Smuzhiyun extern void setattr_copy(struct inode *inode, const struct iattr *attr);
3300*4882a593Smuzhiyun 
3301*4882a593Smuzhiyun extern int file_update_time(struct file *file);
3302*4882a593Smuzhiyun 
vma_is_dax(const struct vm_area_struct * vma)3303*4882a593Smuzhiyun static inline bool vma_is_dax(const struct vm_area_struct *vma)
3304*4882a593Smuzhiyun {
3305*4882a593Smuzhiyun 	return vma->vm_file && IS_DAX(vma->vm_file->f_mapping->host);
3306*4882a593Smuzhiyun }
3307*4882a593Smuzhiyun 
vma_is_fsdax(struct vm_area_struct * vma)3308*4882a593Smuzhiyun static inline bool vma_is_fsdax(struct vm_area_struct *vma)
3309*4882a593Smuzhiyun {
3310*4882a593Smuzhiyun 	struct inode *inode;
3311*4882a593Smuzhiyun 
3312*4882a593Smuzhiyun 	if (!IS_ENABLED(CONFIG_FS_DAX) || !vma->vm_file)
3313*4882a593Smuzhiyun 		return false;
3314*4882a593Smuzhiyun 	if (!vma_is_dax(vma))
3315*4882a593Smuzhiyun 		return false;
3316*4882a593Smuzhiyun 	inode = file_inode(vma->vm_file);
3317*4882a593Smuzhiyun 	if (S_ISCHR(inode->i_mode))
3318*4882a593Smuzhiyun 		return false; /* device-dax */
3319*4882a593Smuzhiyun 	return true;
3320*4882a593Smuzhiyun }
3321*4882a593Smuzhiyun 
iocb_flags(struct file * file)3322*4882a593Smuzhiyun static inline int iocb_flags(struct file *file)
3323*4882a593Smuzhiyun {
3324*4882a593Smuzhiyun 	int res = 0;
3325*4882a593Smuzhiyun 	if (file->f_flags & O_APPEND)
3326*4882a593Smuzhiyun 		res |= IOCB_APPEND;
3327*4882a593Smuzhiyun 	if (file->f_flags & O_DIRECT)
3328*4882a593Smuzhiyun 		res |= IOCB_DIRECT;
3329*4882a593Smuzhiyun 	if ((file->f_flags & O_DSYNC) || IS_SYNC(file->f_mapping->host))
3330*4882a593Smuzhiyun 		res |= IOCB_DSYNC;
3331*4882a593Smuzhiyun 	if (file->f_flags & __O_SYNC)
3332*4882a593Smuzhiyun 		res |= IOCB_SYNC;
3333*4882a593Smuzhiyun 	return res;
3334*4882a593Smuzhiyun }
3335*4882a593Smuzhiyun 
kiocb_set_rw_flags(struct kiocb * ki,rwf_t flags)3336*4882a593Smuzhiyun static inline int kiocb_set_rw_flags(struct kiocb *ki, rwf_t flags)
3337*4882a593Smuzhiyun {
3338*4882a593Smuzhiyun 	int kiocb_flags = 0;
3339*4882a593Smuzhiyun 
3340*4882a593Smuzhiyun 	/* make sure there's no overlap between RWF and private IOCB flags */
3341*4882a593Smuzhiyun 	BUILD_BUG_ON((__force int) RWF_SUPPORTED & IOCB_EVENTFD);
3342*4882a593Smuzhiyun 
3343*4882a593Smuzhiyun 	if (!flags)
3344*4882a593Smuzhiyun 		return 0;
3345*4882a593Smuzhiyun 	if (unlikely(flags & ~RWF_SUPPORTED))
3346*4882a593Smuzhiyun 		return -EOPNOTSUPP;
3347*4882a593Smuzhiyun 
3348*4882a593Smuzhiyun 	if (flags & RWF_NOWAIT) {
3349*4882a593Smuzhiyun 		if (!(ki->ki_filp->f_mode & FMODE_NOWAIT))
3350*4882a593Smuzhiyun 			return -EOPNOTSUPP;
3351*4882a593Smuzhiyun 		kiocb_flags |= IOCB_NOIO;
3352*4882a593Smuzhiyun 	}
3353*4882a593Smuzhiyun 	kiocb_flags |= (__force int) (flags & RWF_SUPPORTED);
3354*4882a593Smuzhiyun 	if (flags & RWF_SYNC)
3355*4882a593Smuzhiyun 		kiocb_flags |= IOCB_DSYNC;
3356*4882a593Smuzhiyun 
3357*4882a593Smuzhiyun 	ki->ki_flags |= kiocb_flags;
3358*4882a593Smuzhiyun 	return 0;
3359*4882a593Smuzhiyun }
3360*4882a593Smuzhiyun 
iocb_to_rw_flags(int ifl,int iocb_mask)3361*4882a593Smuzhiyun static inline rwf_t iocb_to_rw_flags(int ifl, int iocb_mask)
3362*4882a593Smuzhiyun {
3363*4882a593Smuzhiyun 	return ifl & iocb_mask;
3364*4882a593Smuzhiyun }
3365*4882a593Smuzhiyun 
parent_ino(struct dentry * dentry)3366*4882a593Smuzhiyun static inline ino_t parent_ino(struct dentry *dentry)
3367*4882a593Smuzhiyun {
3368*4882a593Smuzhiyun 	ino_t res;
3369*4882a593Smuzhiyun 
3370*4882a593Smuzhiyun 	/*
3371*4882a593Smuzhiyun 	 * Don't strictly need d_lock here? If the parent ino could change
3372*4882a593Smuzhiyun 	 * then surely we'd have a deeper race in the caller?
3373*4882a593Smuzhiyun 	 */
3374*4882a593Smuzhiyun 	spin_lock(&dentry->d_lock);
3375*4882a593Smuzhiyun 	res = dentry->d_parent->d_inode->i_ino;
3376*4882a593Smuzhiyun 	spin_unlock(&dentry->d_lock);
3377*4882a593Smuzhiyun 	return res;
3378*4882a593Smuzhiyun }
3379*4882a593Smuzhiyun 
3380*4882a593Smuzhiyun /* Transaction based IO helpers */
3381*4882a593Smuzhiyun 
3382*4882a593Smuzhiyun /*
3383*4882a593Smuzhiyun  * An argresp is stored in an allocated page and holds the
3384*4882a593Smuzhiyun  * size of the argument or response, along with its content
3385*4882a593Smuzhiyun  */
3386*4882a593Smuzhiyun struct simple_transaction_argresp {
3387*4882a593Smuzhiyun 	ssize_t size;
3388*4882a593Smuzhiyun 	char data[];
3389*4882a593Smuzhiyun };
3390*4882a593Smuzhiyun 
3391*4882a593Smuzhiyun #define SIMPLE_TRANSACTION_LIMIT (PAGE_SIZE - sizeof(struct simple_transaction_argresp))
3392*4882a593Smuzhiyun 
3393*4882a593Smuzhiyun char *simple_transaction_get(struct file *file, const char __user *buf,
3394*4882a593Smuzhiyun 				size_t size);
3395*4882a593Smuzhiyun ssize_t simple_transaction_read(struct file *file, char __user *buf,
3396*4882a593Smuzhiyun 				size_t size, loff_t *pos);
3397*4882a593Smuzhiyun int simple_transaction_release(struct inode *inode, struct file *file);
3398*4882a593Smuzhiyun 
3399*4882a593Smuzhiyun void simple_transaction_set(struct file *file, size_t n);
3400*4882a593Smuzhiyun 
3401*4882a593Smuzhiyun /*
3402*4882a593Smuzhiyun  * simple attribute files
3403*4882a593Smuzhiyun  *
3404*4882a593Smuzhiyun  * These attributes behave similar to those in sysfs:
3405*4882a593Smuzhiyun  *
3406*4882a593Smuzhiyun  * Writing to an attribute immediately sets a value, an open file can be
3407*4882a593Smuzhiyun  * written to multiple times.
3408*4882a593Smuzhiyun  *
3409*4882a593Smuzhiyun  * Reading from an attribute creates a buffer from the value that might get
3410*4882a593Smuzhiyun  * read with multiple read calls. When the attribute has been read
3411*4882a593Smuzhiyun  * completely, no further read calls are possible until the file is opened
3412*4882a593Smuzhiyun  * again.
3413*4882a593Smuzhiyun  *
3414*4882a593Smuzhiyun  * All attributes contain a text representation of a numeric value
3415*4882a593Smuzhiyun  * that are accessed with the get() and set() functions.
3416*4882a593Smuzhiyun  */
3417*4882a593Smuzhiyun #define DEFINE_SIMPLE_ATTRIBUTE(__fops, __get, __set, __fmt)		\
3418*4882a593Smuzhiyun static int __fops ## _open(struct inode *inode, struct file *file)	\
3419*4882a593Smuzhiyun {									\
3420*4882a593Smuzhiyun 	__simple_attr_check_format(__fmt, 0ull);			\
3421*4882a593Smuzhiyun 	return simple_attr_open(inode, file, __get, __set, __fmt);	\
3422*4882a593Smuzhiyun }									\
3423*4882a593Smuzhiyun static const struct file_operations __fops = {				\
3424*4882a593Smuzhiyun 	.owner	 = THIS_MODULE,						\
3425*4882a593Smuzhiyun 	.open	 = __fops ## _open,					\
3426*4882a593Smuzhiyun 	.release = simple_attr_release,					\
3427*4882a593Smuzhiyun 	.read	 = simple_attr_read,					\
3428*4882a593Smuzhiyun 	.write	 = simple_attr_write,					\
3429*4882a593Smuzhiyun 	.llseek	 = generic_file_llseek,					\
3430*4882a593Smuzhiyun }
3431*4882a593Smuzhiyun 
3432*4882a593Smuzhiyun static inline __printf(1, 2)
__simple_attr_check_format(const char * fmt,...)3433*4882a593Smuzhiyun void __simple_attr_check_format(const char *fmt, ...)
3434*4882a593Smuzhiyun {
3435*4882a593Smuzhiyun 	/* don't do anything, just let the compiler check the arguments; */
3436*4882a593Smuzhiyun }
3437*4882a593Smuzhiyun 
3438*4882a593Smuzhiyun int simple_attr_open(struct inode *inode, struct file *file,
3439*4882a593Smuzhiyun 		     int (*get)(void *, u64 *), int (*set)(void *, u64),
3440*4882a593Smuzhiyun 		     const char *fmt);
3441*4882a593Smuzhiyun int simple_attr_release(struct inode *inode, struct file *file);
3442*4882a593Smuzhiyun ssize_t simple_attr_read(struct file *file, char __user *buf,
3443*4882a593Smuzhiyun 			 size_t len, loff_t *ppos);
3444*4882a593Smuzhiyun ssize_t simple_attr_write(struct file *file, const char __user *buf,
3445*4882a593Smuzhiyun 			  size_t len, loff_t *ppos);
3446*4882a593Smuzhiyun 
3447*4882a593Smuzhiyun struct ctl_table;
3448*4882a593Smuzhiyun int proc_nr_files(struct ctl_table *table, int write,
3449*4882a593Smuzhiyun 		  void *buffer, size_t *lenp, loff_t *ppos);
3450*4882a593Smuzhiyun int proc_nr_dentry(struct ctl_table *table, int write,
3451*4882a593Smuzhiyun 		  void *buffer, size_t *lenp, loff_t *ppos);
3452*4882a593Smuzhiyun int proc_nr_inodes(struct ctl_table *table, int write,
3453*4882a593Smuzhiyun 		   void *buffer, size_t *lenp, loff_t *ppos);
3454*4882a593Smuzhiyun int __init get_filesystem_list(char *buf);
3455*4882a593Smuzhiyun 
3456*4882a593Smuzhiyun #define __FMODE_EXEC		((__force int) FMODE_EXEC)
3457*4882a593Smuzhiyun #define __FMODE_NONOTIFY	((__force int) FMODE_NONOTIFY)
3458*4882a593Smuzhiyun 
3459*4882a593Smuzhiyun #define ACC_MODE(x) ("\004\002\006\006"[(x)&O_ACCMODE])
3460*4882a593Smuzhiyun #define OPEN_FMODE(flag) ((__force fmode_t)(((flag + 1) & O_ACCMODE) | \
3461*4882a593Smuzhiyun 					    (flag & __FMODE_NONOTIFY)))
3462*4882a593Smuzhiyun 
is_sxid(umode_t mode)3463*4882a593Smuzhiyun static inline bool is_sxid(umode_t mode)
3464*4882a593Smuzhiyun {
3465*4882a593Smuzhiyun 	return (mode & S_ISUID) || ((mode & S_ISGID) && (mode & S_IXGRP));
3466*4882a593Smuzhiyun }
3467*4882a593Smuzhiyun 
check_sticky(struct inode * dir,struct inode * inode)3468*4882a593Smuzhiyun static inline int check_sticky(struct inode *dir, struct inode *inode)
3469*4882a593Smuzhiyun {
3470*4882a593Smuzhiyun 	if (!(dir->i_mode & S_ISVTX))
3471*4882a593Smuzhiyun 		return 0;
3472*4882a593Smuzhiyun 
3473*4882a593Smuzhiyun 	return __check_sticky(dir, inode);
3474*4882a593Smuzhiyun }
3475*4882a593Smuzhiyun 
inode_has_no_xattr(struct inode * inode)3476*4882a593Smuzhiyun static inline void inode_has_no_xattr(struct inode *inode)
3477*4882a593Smuzhiyun {
3478*4882a593Smuzhiyun 	if (!is_sxid(inode->i_mode) && (inode->i_sb->s_flags & SB_NOSEC))
3479*4882a593Smuzhiyun 		inode->i_flags |= S_NOSEC;
3480*4882a593Smuzhiyun }
3481*4882a593Smuzhiyun 
is_root_inode(struct inode * inode)3482*4882a593Smuzhiyun static inline bool is_root_inode(struct inode *inode)
3483*4882a593Smuzhiyun {
3484*4882a593Smuzhiyun 	return inode == inode->i_sb->s_root->d_inode;
3485*4882a593Smuzhiyun }
3486*4882a593Smuzhiyun 
dir_emit(struct dir_context * ctx,const char * name,int namelen,u64 ino,unsigned type)3487*4882a593Smuzhiyun static inline bool dir_emit(struct dir_context *ctx,
3488*4882a593Smuzhiyun 			    const char *name, int namelen,
3489*4882a593Smuzhiyun 			    u64 ino, unsigned type)
3490*4882a593Smuzhiyun {
3491*4882a593Smuzhiyun 	return ctx->actor(ctx, name, namelen, ctx->pos, ino, type) == 0;
3492*4882a593Smuzhiyun }
dir_emit_dot(struct file * file,struct dir_context * ctx)3493*4882a593Smuzhiyun static inline bool dir_emit_dot(struct file *file, struct dir_context *ctx)
3494*4882a593Smuzhiyun {
3495*4882a593Smuzhiyun 	return ctx->actor(ctx, ".", 1, ctx->pos,
3496*4882a593Smuzhiyun 			  file->f_path.dentry->d_inode->i_ino, DT_DIR) == 0;
3497*4882a593Smuzhiyun }
dir_emit_dotdot(struct file * file,struct dir_context * ctx)3498*4882a593Smuzhiyun static inline bool dir_emit_dotdot(struct file *file, struct dir_context *ctx)
3499*4882a593Smuzhiyun {
3500*4882a593Smuzhiyun 	return ctx->actor(ctx, "..", 2, ctx->pos,
3501*4882a593Smuzhiyun 			  parent_ino(file->f_path.dentry), DT_DIR) == 0;
3502*4882a593Smuzhiyun }
dir_emit_dots(struct file * file,struct dir_context * ctx)3503*4882a593Smuzhiyun static inline bool dir_emit_dots(struct file *file, struct dir_context *ctx)
3504*4882a593Smuzhiyun {
3505*4882a593Smuzhiyun 	if (ctx->pos == 0) {
3506*4882a593Smuzhiyun 		if (!dir_emit_dot(file, ctx))
3507*4882a593Smuzhiyun 			return false;
3508*4882a593Smuzhiyun 		ctx->pos = 1;
3509*4882a593Smuzhiyun 	}
3510*4882a593Smuzhiyun 	if (ctx->pos == 1) {
3511*4882a593Smuzhiyun 		if (!dir_emit_dotdot(file, ctx))
3512*4882a593Smuzhiyun 			return false;
3513*4882a593Smuzhiyun 		ctx->pos = 2;
3514*4882a593Smuzhiyun 	}
3515*4882a593Smuzhiyun 	return true;
3516*4882a593Smuzhiyun }
dir_relax(struct inode * inode)3517*4882a593Smuzhiyun static inline bool dir_relax(struct inode *inode)
3518*4882a593Smuzhiyun {
3519*4882a593Smuzhiyun 	inode_unlock(inode);
3520*4882a593Smuzhiyun 	inode_lock(inode);
3521*4882a593Smuzhiyun 	return !IS_DEADDIR(inode);
3522*4882a593Smuzhiyun }
3523*4882a593Smuzhiyun 
dir_relax_shared(struct inode * inode)3524*4882a593Smuzhiyun static inline bool dir_relax_shared(struct inode *inode)
3525*4882a593Smuzhiyun {
3526*4882a593Smuzhiyun 	inode_unlock_shared(inode);
3527*4882a593Smuzhiyun 	inode_lock_shared(inode);
3528*4882a593Smuzhiyun 	return !IS_DEADDIR(inode);
3529*4882a593Smuzhiyun }
3530*4882a593Smuzhiyun 
3531*4882a593Smuzhiyun extern bool path_noexec(const struct path *path);
3532*4882a593Smuzhiyun extern void inode_nohighmem(struct inode *inode);
3533*4882a593Smuzhiyun 
3534*4882a593Smuzhiyun /* mm/fadvise.c */
3535*4882a593Smuzhiyun extern int vfs_fadvise(struct file *file, loff_t offset, loff_t len,
3536*4882a593Smuzhiyun 		       int advice);
3537*4882a593Smuzhiyun extern int generic_fadvise(struct file *file, loff_t offset, loff_t len,
3538*4882a593Smuzhiyun 			   int advice);
3539*4882a593Smuzhiyun 
3540*4882a593Smuzhiyun int vfs_ioc_setflags_prepare(struct inode *inode, unsigned int oldflags,
3541*4882a593Smuzhiyun 			     unsigned int flags);
3542*4882a593Smuzhiyun 
3543*4882a593Smuzhiyun int vfs_ioc_fssetxattr_check(struct inode *inode, const struct fsxattr *old_fa,
3544*4882a593Smuzhiyun 			     struct fsxattr *fa);
3545*4882a593Smuzhiyun 
simple_fill_fsxattr(struct fsxattr * fa,__u32 xflags)3546*4882a593Smuzhiyun static inline void simple_fill_fsxattr(struct fsxattr *fa, __u32 xflags)
3547*4882a593Smuzhiyun {
3548*4882a593Smuzhiyun 	memset(fa, 0, sizeof(*fa));
3549*4882a593Smuzhiyun 	fa->fsx_xflags = xflags;
3550*4882a593Smuzhiyun }
3551*4882a593Smuzhiyun 
3552*4882a593Smuzhiyun /*
3553*4882a593Smuzhiyun  * Flush file data before changing attributes.  Caller must hold any locks
3554*4882a593Smuzhiyun  * required to prevent further writes to this file until we're done setting
3555*4882a593Smuzhiyun  * flags.
3556*4882a593Smuzhiyun  */
inode_drain_writes(struct inode * inode)3557*4882a593Smuzhiyun static inline int inode_drain_writes(struct inode *inode)
3558*4882a593Smuzhiyun {
3559*4882a593Smuzhiyun 	inode_dio_wait(inode);
3560*4882a593Smuzhiyun 	return filemap_write_and_wait(inode->i_mapping);
3561*4882a593Smuzhiyun }
3562*4882a593Smuzhiyun 
3563*4882a593Smuzhiyun #endif /* _LINUX_FS_H */
3564