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