xref: /linux/include/linux/fs.h (revision 334fbe734e687404f346eba7d5d96ed2b44d35ab) !
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_FS_H
3 #define _LINUX_FS_H
4 
5 #include <linux/fs/super.h>
6 #include <linux/vfsdebug.h>
7 #include <linux/linkage.h>
8 #include <linux/wait_bit.h>
9 #include <linux/kdev_t.h>
10 #include <linux/dcache.h>
11 #include <linux/path.h>
12 #include <linux/stat.h>
13 #include <linux/cache.h>
14 #include <linux/list.h>
15 #include <linux/llist.h>
16 #include <linux/radix-tree.h>
17 #include <linux/xarray.h>
18 #include <linux/rbtree.h>
19 #include <linux/init.h>
20 #include <linux/pid.h>
21 #include <linux/bug.h>
22 #include <linux/mutex.h>
23 #include <linux/rwsem.h>
24 #include <linux/mm_types.h>
25 #include <linux/capability.h>
26 #include <linux/semaphore.h>
27 #include <linux/fcntl.h>
28 #include <linux/rculist_bl.h>
29 #include <linux/atomic.h>
30 #include <linux/shrinker.h>
31 #include <linux/migrate_mode.h>
32 #include <linux/uidgid.h>
33 #include <linux/lockdep.h>
34 #include <linux/percpu-rwsem.h>
35 #include <linux/workqueue.h>
36 #include <linux/delayed_call.h>
37 #include <linux/uuid.h>
38 #include <linux/errseq.h>
39 #include <linux/ioprio.h>
40 #include <linux/build_bug.h>
41 #include <linux/stddef.h>
42 #include <linux/mount.h>
43 #include <linux/cred.h>
44 #include <linux/mnt_idmapping.h>
45 #include <linux/slab.h>
46 #include <linux/maple_tree.h>
47 #include <linux/rw_hint.h>
48 #include <linux/file_ref.h>
49 #include <linux/unicode.h>
50 
51 #include <asm/byteorder.h>
52 #include <uapi/linux/fs.h>
53 
54 struct bdi_writeback;
55 struct bio;
56 struct io_comp_batch;
57 struct fiemap_extent_info;
58 struct kiocb;
59 struct kobject;
60 struct pipe_inode_info;
61 struct poll_table_struct;
62 struct kstatfs;
63 struct vm_area_struct;
64 struct vfsmount;
65 struct cred;
66 struct swap_info_struct;
67 struct seq_file;
68 struct iov_iter;
69 struct fsnotify_mark_connector;
70 struct fs_context;
71 struct fs_parameter_spec;
72 struct file_kattr;
73 struct iomap_ops;
74 struct delegated_inode;
75 
76 extern void __init inode_init(void);
77 extern void __init inode_init_early(void);
78 extern void __init files_init(void);
79 extern void __init files_maxfiles_init(void);
80 
81 extern unsigned long get_max_files(void);
82 extern unsigned int sysctl_nr_open;
83 
84 typedef __kernel_rwf_t rwf_t;
85 
86 struct buffer_head;
87 typedef int (get_block_t)(struct inode *inode, sector_t iblock,
88 			struct buffer_head *bh_result, int create);
89 typedef int (dio_iodone_t)(struct kiocb *iocb, loff_t offset,
90 			ssize_t bytes, void *private);
91 
92 #define MAY_EXEC		0x00000001
93 #define MAY_WRITE		0x00000002
94 #define MAY_READ		0x00000004
95 #define MAY_APPEND		0x00000008
96 #define MAY_ACCESS		0x00000010
97 #define MAY_OPEN		0x00000020
98 #define MAY_CHDIR		0x00000040
99 /* called from RCU mode, don't block */
100 #define MAY_NOT_BLOCK		0x00000080
101 
102 /*
103  * flags in file.f_mode.  Note that FMODE_READ and FMODE_WRITE must correspond
104  * to O_WRONLY and O_RDWR via the strange trick in do_dentry_open()
105  */
106 
107 /* file is open for reading */
108 #define FMODE_READ		((__force fmode_t)(1 << 0))
109 /* file is open for writing */
110 #define FMODE_WRITE		((__force fmode_t)(1 << 1))
111 /* file is seekable */
112 #define FMODE_LSEEK		((__force fmode_t)(1 << 2))
113 /* file can be accessed using pread */
114 #define FMODE_PREAD		((__force fmode_t)(1 << 3))
115 /* file can be accessed using pwrite */
116 #define FMODE_PWRITE		((__force fmode_t)(1 << 4))
117 /* File is opened for execution with sys_execve / sys_uselib */
118 #define FMODE_EXEC		((__force fmode_t)(1 << 5))
119 /* File writes are restricted (block device specific) */
120 #define FMODE_WRITE_RESTRICTED	((__force fmode_t)(1 << 6))
121 /* File supports atomic writes */
122 #define FMODE_CAN_ATOMIC_WRITE	((__force fmode_t)(1 << 7))
123 
124 /* FMODE_* bit 8 */
125 
126 /* 32bit hashes as llseek() offset (for directories) */
127 #define FMODE_32BITHASH         ((__force fmode_t)(1 << 9))
128 /* 64bit hashes as llseek() offset (for directories) */
129 #define FMODE_64BITHASH         ((__force fmode_t)(1 << 10))
130 
131 /*
132  * Don't update ctime and mtime.
133  *
134  * Currently a special hack for the XFS open_by_handle ioctl, but we'll
135  * hopefully graduate it to a proper O_CMTIME flag supported by open(2) soon.
136  */
137 #define FMODE_NOCMTIME		((__force fmode_t)(1 << 11))
138 
139 /* Expect random access pattern */
140 #define FMODE_RANDOM		((__force fmode_t)(1 << 12))
141 
142 /* Supports IOCB_HAS_METADATA */
143 #define FMODE_HAS_METADATA	((__force fmode_t)(1 << 13))
144 
145 /* File is opened with O_PATH; almost nothing can be done with it */
146 #define FMODE_PATH		((__force fmode_t)(1 << 14))
147 
148 /* File needs atomic accesses to f_pos */
149 #define FMODE_ATOMIC_POS	((__force fmode_t)(1 << 15))
150 /* Write access to underlying fs */
151 #define FMODE_WRITER		((__force fmode_t)(1 << 16))
152 /* Has read method(s) */
153 #define FMODE_CAN_READ          ((__force fmode_t)(1 << 17))
154 /* Has write method(s) */
155 #define FMODE_CAN_WRITE         ((__force fmode_t)(1 << 18))
156 
157 #define FMODE_OPENED		((__force fmode_t)(1 << 19))
158 #define FMODE_CREATED		((__force fmode_t)(1 << 20))
159 
160 /* File is stream-like */
161 #define FMODE_STREAM		((__force fmode_t)(1 << 21))
162 
163 /* File supports DIRECT IO */
164 #define	FMODE_CAN_ODIRECT	((__force fmode_t)(1 << 22))
165 
166 #define	FMODE_NOREUSE		((__force fmode_t)(1 << 23))
167 
168 /* File is embedded in backing_file object */
169 #define FMODE_BACKING		((__force fmode_t)(1 << 24))
170 
171 /*
172  * Together with FMODE_NONOTIFY_PERM defines which fsnotify events shouldn't be
173  * generated (see below)
174  */
175 #define FMODE_NONOTIFY		((__force fmode_t)(1 << 25))
176 
177 /*
178  * Together with FMODE_NONOTIFY defines which fsnotify events shouldn't be
179  * generated (see below)
180  */
181 #define FMODE_NONOTIFY_PERM	((__force fmode_t)(1 << 26))
182 
183 /* File is capable of returning -EAGAIN if I/O will block */
184 #define FMODE_NOWAIT		((__force fmode_t)(1 << 27))
185 
186 /* File represents mount that needs unmounting */
187 #define FMODE_NEED_UNMOUNT	((__force fmode_t)(1 << 28))
188 
189 /* File does not contribute to nr_files count */
190 #define FMODE_NOACCOUNT		((__force fmode_t)(1 << 29))
191 
192 /*
193  * The two FMODE_NONOTIFY* define which fsnotify events should not be generated
194  * for an open file. These are the possible values of
195  * (f->f_mode & FMODE_FSNOTIFY_MASK) and their meaning:
196  *
197  * FMODE_NONOTIFY - suppress all (incl. non-permission) events.
198  * FMODE_NONOTIFY_PERM - suppress permission (incl. pre-content) events.
199  * FMODE_NONOTIFY | FMODE_NONOTIFY_PERM - suppress only FAN_ACCESS_PERM.
200  */
201 #define FMODE_FSNOTIFY_MASK \
202 	(FMODE_NONOTIFY | FMODE_NONOTIFY_PERM)
203 
204 #define FMODE_FSNOTIFY_NONE(mode) \
205 	((mode & FMODE_FSNOTIFY_MASK) == FMODE_NONOTIFY)
206 #ifdef CONFIG_FANOTIFY_ACCESS_PERMISSIONS
207 #define FMODE_FSNOTIFY_HSM(mode) \
208 	((mode & FMODE_FSNOTIFY_MASK) == 0 || \
209 	 (mode & FMODE_FSNOTIFY_MASK) == (FMODE_NONOTIFY | FMODE_NONOTIFY_PERM))
210 #define FMODE_FSNOTIFY_ACCESS_PERM(mode) \
211 	((mode & FMODE_FSNOTIFY_MASK) == 0)
212 #else
213 #define FMODE_FSNOTIFY_ACCESS_PERM(mode) 0
214 #define FMODE_FSNOTIFY_HSM(mode)	0
215 #endif
216 
217 /*
218  * Attribute flags.  These should be or-ed together to figure out what
219  * has been changed!
220  */
221 #define ATTR_MODE	(1 << 0)
222 #define ATTR_UID	(1 << 1)
223 #define ATTR_GID	(1 << 2)
224 #define ATTR_SIZE	(1 << 3)
225 #define ATTR_ATIME	(1 << 4)
226 #define ATTR_MTIME	(1 << 5)
227 #define ATTR_CTIME	(1 << 6)
228 #define ATTR_ATIME_SET	(1 << 7)
229 #define ATTR_MTIME_SET	(1 << 8)
230 #define ATTR_FORCE	(1 << 9) /* Not a change, but a change it */
231 #define ATTR_CTIME_SET	(1 << 10)
232 #define ATTR_KILL_SUID	(1 << 11)
233 #define ATTR_KILL_SGID	(1 << 12)
234 #define ATTR_FILE	(1 << 13)
235 #define ATTR_KILL_PRIV	(1 << 14)
236 #define ATTR_OPEN	(1 << 15) /* Truncating from open(O_TRUNC) */
237 #define ATTR_TIMES_SET	(1 << 16)
238 #define ATTR_TOUCH	(1 << 17)
239 #define ATTR_DELEG	(1 << 18) /* Delegated attrs. Don't break write delegations */
240 
241 /*
242  * Whiteout is represented by a char device.  The following constants define the
243  * mode and device number to use.
244  */
245 #define WHITEOUT_MODE 0
246 #define WHITEOUT_DEV 0
247 
248 /*
249  * This is the Inode Attributes structure, used for notify_change().  It
250  * uses the above definitions as flags, to know which values have changed.
251  * Also, in this manner, a Filesystem can look at only the values it cares
252  * about.  Basically, these are the attributes that the VFS layer can
253  * request to change from the FS layer.
254  *
255  * Derek Atkins <warlord@MIT.EDU> 94-10-20
256  */
257 struct iattr {
258 	unsigned int	ia_valid;
259 	umode_t		ia_mode;
260 	/*
261 	 * The two anonymous unions wrap structures with the same member.
262 	 *
263 	 * Filesystems raising FS_ALLOW_IDMAP need to use ia_vfs{g,u}id which
264 	 * are a dedicated type requiring the filesystem to use the dedicated
265 	 * helpers. Other filesystem can continue to use ia_{g,u}id until they
266 	 * have been ported.
267 	 *
268 	 * They always contain the same value. In other words FS_ALLOW_IDMAP
269 	 * pass down the same value on idmapped mounts as they would on regular
270 	 * mounts.
271 	 */
272 	union {
273 		kuid_t		ia_uid;
274 		vfsuid_t	ia_vfsuid;
275 	};
276 	union {
277 		kgid_t		ia_gid;
278 		vfsgid_t	ia_vfsgid;
279 	};
280 	loff_t		ia_size;
281 	struct timespec64 ia_atime;
282 	struct timespec64 ia_mtime;
283 	struct timespec64 ia_ctime;
284 
285 	/*
286 	 * Not an attribute, but an auxiliary info for filesystems wanting to
287 	 * implement an ftruncate() like method.  NOTE: filesystem should
288 	 * check for (ia_valid & ATTR_FILE), and not for (ia_file != NULL).
289 	 */
290 	struct file	*ia_file;
291 };
292 
293 /*
294  * Maximum number of layers of fs stack.  Needs to be limited to
295  * prevent kernel stack overflow
296  */
297 #define FILESYSTEM_MAX_STACK_DEPTH 2
298 
299 /**
300  * enum positive_aop_returns - aop return codes with specific semantics
301  *
302  * @AOP_WRITEPAGE_ACTIVATE: Informs the caller that page writeback has
303  * 			    completed, that the page is still locked, and
304  * 			    should be considered active.  The VM uses this hint
305  * 			    to return the page to the active list -- it won't
306  * 			    be a candidate for writeback again in the near
307  * 			    future.  Other callers must be careful to unlock
308  * 			    the page if they get this return.  Returned by
309  * 			    writepage();
310  *
311  * @AOP_TRUNCATED_PAGE: The AOP method that was handed a locked page has
312  *  			unlocked it and the page might have been truncated.
313  *  			The caller should back up to acquiring a new page and
314  *  			trying again.  The aop will be taking reasonable
315  *  			precautions not to livelock.  If the caller held a page
316  *  			reference, it should drop it before retrying.  Returned
317  *  			by read_folio().
318  *
319  * address_space_operation functions return these large constants to indicate
320  * special semantics to the caller.  These are much larger than the bytes in a
321  * page to allow for functions that return the number of bytes operated on in a
322  * given page.
323  */
324 
325 enum positive_aop_returns {
326 	AOP_WRITEPAGE_ACTIVATE	= 0x80000,
327 	AOP_TRUNCATED_PAGE	= 0x80001,
328 };
329 
330 /*
331  * oh the beauties of C type declarations.
332  */
333 struct page;
334 struct address_space;
335 struct writeback_control;
336 struct readahead_control;
337 
338 /* Match RWF_* bits to IOCB bits */
339 #define IOCB_HIPRI		(__force int) RWF_HIPRI
340 #define IOCB_DSYNC		(__force int) RWF_DSYNC
341 #define IOCB_SYNC		(__force int) RWF_SYNC
342 #define IOCB_NOWAIT		(__force int) RWF_NOWAIT
343 #define IOCB_APPEND		(__force int) RWF_APPEND
344 #define IOCB_ATOMIC		(__force int) RWF_ATOMIC
345 #define IOCB_DONTCACHE		(__force int) RWF_DONTCACHE
346 #define IOCB_NOSIGNAL		(__force int) RWF_NOSIGNAL
347 
348 /* non-RWF related bits - start at 16 */
349 #define IOCB_EVENTFD		(1 << 16)
350 #define IOCB_DIRECT		(1 << 17)
351 #define IOCB_WRITE		(1 << 18)
352 /* iocb->ki_waitq is valid */
353 #define IOCB_WAITQ		(1 << 19)
354 #define IOCB_NOIO		(1 << 20)
355 /* can use bio alloc cache */
356 #define IOCB_ALLOC_CACHE	(1 << 21)
357 /* kiocb is a read or write operation submitted by fs/aio.c. */
358 #define IOCB_AIO_RW		(1 << 22)
359 #define IOCB_HAS_METADATA	(1 << 23)
360 
361 /* for use in trace events */
362 #define TRACE_IOCB_STRINGS \
363 	{ IOCB_HIPRI,		"HIPRI" }, \
364 	{ IOCB_DSYNC,		"DSYNC" }, \
365 	{ IOCB_SYNC,		"SYNC" }, \
366 	{ IOCB_NOWAIT,		"NOWAIT" }, \
367 	{ IOCB_APPEND,		"APPEND" }, \
368 	{ IOCB_ATOMIC,		"ATOMIC" }, \
369 	{ IOCB_DONTCACHE,	"DONTCACHE" }, \
370 	{ IOCB_EVENTFD,		"EVENTFD"}, \
371 	{ IOCB_DIRECT,		"DIRECT" }, \
372 	{ IOCB_WRITE,		"WRITE" }, \
373 	{ IOCB_WAITQ,		"WAITQ" }, \
374 	{ IOCB_NOIO,		"NOIO" }, \
375 	{ IOCB_ALLOC_CACHE,	"ALLOC_CACHE" }, \
376 	{ IOCB_AIO_RW,		"AIO_RW" }, \
377 	{ IOCB_HAS_METADATA,	"AIO_HAS_METADATA" }
378 
379 struct kiocb {
380 	struct file		*ki_filp;
381 	loff_t			ki_pos;
382 	void (*ki_complete)(struct kiocb *iocb, long ret);
383 	void			*private;
384 	int			ki_flags;
385 	u16			ki_ioprio; /* See linux/ioprio.h */
386 	u8			ki_write_stream;
387 
388 	/*
389 	 * Only used for async buffered reads, where it denotes the page
390 	 * waitqueue associated with completing the read.
391 	 * Valid IFF IOCB_WAITQ is set.
392 	 */
393 	struct wait_page_queue	*ki_waitq;
394 };
395 
is_sync_kiocb(struct kiocb * kiocb)396 static inline bool is_sync_kiocb(struct kiocb *kiocb)
397 {
398 	return kiocb->ki_complete == NULL;
399 }
400 
401 struct address_space_operations {
402 	int (*read_folio)(struct file *, struct folio *);
403 
404 	/* Write back some dirty pages from this mapping. */
405 	int (*writepages)(struct address_space *, struct writeback_control *);
406 
407 	/* Mark a folio dirty.  Return true if this dirtied it */
408 	bool (*dirty_folio)(struct address_space *, struct folio *);
409 
410 	void (*readahead)(struct readahead_control *);
411 
412 	int (*write_begin)(const struct kiocb *, struct address_space *mapping,
413 				loff_t pos, unsigned len,
414 				struct folio **foliop, void **fsdata);
415 	int (*write_end)(const struct kiocb *, struct address_space *mapping,
416 				loff_t pos, unsigned len, unsigned copied,
417 				struct folio *folio, void *fsdata);
418 
419 	/* Unfortunately this kludge is needed for FIBMAP. Don't use it */
420 	sector_t (*bmap)(struct address_space *, sector_t);
421 	void (*invalidate_folio) (struct folio *, size_t offset, size_t len);
422 	bool (*release_folio)(struct folio *, gfp_t);
423 	void (*free_folio)(struct folio *folio);
424 	ssize_t (*direct_IO)(struct kiocb *, struct iov_iter *iter);
425 	/*
426 	 * migrate the contents of a folio to the specified target. If
427 	 * migrate_mode is MIGRATE_ASYNC, it must not block.
428 	 */
429 	int (*migrate_folio)(struct address_space *, struct folio *dst,
430 			struct folio *src, enum migrate_mode);
431 	int (*launder_folio)(struct folio *);
432 	bool (*is_partially_uptodate) (struct folio *, size_t from,
433 			size_t count);
434 	void (*is_dirty_writeback) (struct folio *, bool *dirty, bool *wb);
435 	int (*error_remove_folio)(struct address_space *, struct folio *);
436 
437 	/* swapfile support */
438 	int (*swap_activate)(struct swap_info_struct *sis, struct file *file,
439 				sector_t *span);
440 	void (*swap_deactivate)(struct file *file);
441 	int (*swap_rw)(struct kiocb *iocb, struct iov_iter *iter);
442 };
443 
444 extern const struct address_space_operations empty_aops;
445 
446 /* Structure for tracking metadata buffer heads associated with the mapping */
447 struct mapping_metadata_bhs {
448 	struct address_space *mapping;	/* Mapping bhs are associated with */
449 	spinlock_t lock;	/* Lock protecting bh list */
450 	struct list_head list;	/* The list of bhs (b_assoc_buffers) */
451 };
452 
453 /**
454  * struct address_space - Contents of a cacheable, mappable object.
455  * @host: Owner, either the inode or the block_device.
456  * @i_pages: Cached pages.
457  * @invalidate_lock: Guards coherency between page cache contents and
458  *   file offset->disk block mappings in the filesystem during invalidates.
459  *   It is also used to block modification of page cache contents through
460  *   memory mappings.
461  * @gfp_mask: Memory allocation flags to use for allocating pages.
462  * @i_mmap_writable: Number of VM_SHARED, VM_MAYWRITE mappings.
463  * @nr_thps: Number of THPs in the pagecache (non-shmem only).
464  * @i_mmap: Tree of private and shared mappings.
465  * @i_mmap_rwsem: Protects @i_mmap and @i_mmap_writable.
466  * @nrpages: Number of page entries, protected by the i_pages lock.
467  * @writeback_index: Writeback starts here.
468  * @a_ops: Methods.
469  * @flags: Error bits and flags (AS_*).
470  * @wb_err: The most recent error which has occurred.
471  * @i_private_lock: For use by the owner of the address_space.
472  */
473 struct address_space {
474 	struct inode		*host;
475 	struct xarray		i_pages;
476 	struct rw_semaphore	invalidate_lock;
477 	gfp_t			gfp_mask;
478 	atomic_t		i_mmap_writable;
479 #ifdef CONFIG_READ_ONLY_THP_FOR_FS
480 	/* number of thp, only for non-shmem files */
481 	atomic_t		nr_thps;
482 #endif
483 	struct rb_root_cached	i_mmap;
484 	unsigned long		nrpages;
485 	pgoff_t			writeback_index;
486 	const struct address_space_operations *a_ops;
487 	unsigned long		flags;
488 	errseq_t		wb_err;
489 	spinlock_t		i_private_lock;
490 	struct rw_semaphore	i_mmap_rwsem;
491 } __attribute__((aligned(sizeof(long)))) __randomize_layout;
492 	/*
493 	 * On most architectures that alignment is already the case; but
494 	 * must be enforced here for CRIS, to let the least significant bit
495 	 * of struct folio's "mapping" pointer be used for FOLIO_MAPPING_ANON.
496 	 */
497 
498 /* XArray tags, for tagging dirty and writeback pages in the pagecache. */
499 #define PAGECACHE_TAG_DIRTY	XA_MARK_0
500 #define PAGECACHE_TAG_WRITEBACK	XA_MARK_1
501 #define PAGECACHE_TAG_TOWRITE	XA_MARK_2
502 
503 /*
504  * Returns true if any of the pages in the mapping are marked with the tag.
505  */
mapping_tagged(const struct address_space * mapping,xa_mark_t tag)506 static inline bool mapping_tagged(const struct address_space *mapping, xa_mark_t tag)
507 {
508 	return xa_marked(&mapping->i_pages, tag);
509 }
510 
i_mmap_lock_write(struct address_space * mapping)511 static inline void i_mmap_lock_write(struct address_space *mapping)
512 {
513 	down_write(&mapping->i_mmap_rwsem);
514 }
515 
i_mmap_trylock_write(struct address_space * mapping)516 static inline int i_mmap_trylock_write(struct address_space *mapping)
517 {
518 	return down_write_trylock(&mapping->i_mmap_rwsem);
519 }
520 
i_mmap_unlock_write(struct address_space * mapping)521 static inline void i_mmap_unlock_write(struct address_space *mapping)
522 {
523 	up_write(&mapping->i_mmap_rwsem);
524 }
525 
i_mmap_trylock_read(struct address_space * mapping)526 static inline int i_mmap_trylock_read(struct address_space *mapping)
527 {
528 	return down_read_trylock(&mapping->i_mmap_rwsem);
529 }
530 
i_mmap_lock_read(struct address_space * mapping)531 static inline void i_mmap_lock_read(struct address_space *mapping)
532 {
533 	down_read(&mapping->i_mmap_rwsem);
534 }
535 
i_mmap_unlock_read(struct address_space * mapping)536 static inline void i_mmap_unlock_read(struct address_space *mapping)
537 {
538 	up_read(&mapping->i_mmap_rwsem);
539 }
540 
i_mmap_assert_locked(struct address_space * mapping)541 static inline void i_mmap_assert_locked(struct address_space *mapping)
542 {
543 	lockdep_assert_held(&mapping->i_mmap_rwsem);
544 }
545 
i_mmap_assert_write_locked(struct address_space * mapping)546 static inline void i_mmap_assert_write_locked(struct address_space *mapping)
547 {
548 	lockdep_assert_held_write(&mapping->i_mmap_rwsem);
549 }
550 
551 /*
552  * Might pages of this file be mapped into userspace?
553  */
mapping_mapped(const struct address_space * mapping)554 static inline int mapping_mapped(const struct address_space *mapping)
555 {
556 	return	!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root);
557 }
558 
559 /*
560  * Might pages of this file have been modified in userspace?
561  * Note that i_mmap_writable counts all VM_SHARED, VM_MAYWRITE vmas: do_mmap
562  * marks vma as VM_SHARED if it is shared, and the file was opened for
563  * writing i.e. vma may be mprotected writable even if now readonly.
564  *
565  * If i_mmap_writable is negative, no new writable mappings are allowed. You
566  * can only deny writable mappings, if none exists right now.
567  */
mapping_writably_mapped(const struct address_space * mapping)568 static inline int mapping_writably_mapped(const struct address_space *mapping)
569 {
570 	return atomic_read(&mapping->i_mmap_writable) > 0;
571 }
572 
mapping_map_writable(struct address_space * mapping)573 static inline int mapping_map_writable(struct address_space *mapping)
574 {
575 	return atomic_inc_unless_negative(&mapping->i_mmap_writable) ?
576 		0 : -EPERM;
577 }
578 
mapping_unmap_writable(struct address_space * mapping)579 static inline void mapping_unmap_writable(struct address_space *mapping)
580 {
581 	atomic_dec(&mapping->i_mmap_writable);
582 }
583 
mapping_deny_writable(struct address_space * mapping)584 static inline int mapping_deny_writable(struct address_space *mapping)
585 {
586 	return atomic_dec_unless_positive(&mapping->i_mmap_writable) ?
587 		0 : -EBUSY;
588 }
589 
mapping_allow_writable(struct address_space * mapping)590 static inline void mapping_allow_writable(struct address_space *mapping)
591 {
592 	atomic_inc(&mapping->i_mmap_writable);
593 }
594 
595 /*
596  * Use sequence counter to get consistent i_size on 32-bit processors.
597  */
598 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
599 #include <linux/seqlock.h>
600 #define __NEED_I_SIZE_ORDERED
601 #define i_size_ordered_init(inode) seqcount_init(&inode->i_size_seqcount)
602 #else
603 #define i_size_ordered_init(inode) do { } while (0)
604 #endif
605 
606 struct posix_acl;
607 #define ACL_NOT_CACHED ((void *)(-1))
608 /*
609  * ACL_DONT_CACHE is for stacked filesystems, that rely on underlying fs to
610  * cache the ACL.  This also means that ->get_inode_acl() can be called in RCU
611  * mode with the LOOKUP_RCU flag.
612  */
613 #define ACL_DONT_CACHE ((void *)(-3))
614 
615 static inline struct posix_acl *
uncached_acl_sentinel(struct task_struct * task)616 uncached_acl_sentinel(struct task_struct *task)
617 {
618 	return (void *)task + 1;
619 }
620 
621 static inline bool
is_uncached_acl(struct posix_acl * acl)622 is_uncached_acl(struct posix_acl *acl)
623 {
624 	return (long)acl & 1;
625 }
626 
627 #define IOP_FASTPERM		0x0001
628 #define IOP_LOOKUP		0x0002
629 #define IOP_NOFOLLOW		0x0004
630 #define IOP_XATTR		0x0008
631 #define IOP_DEFAULT_READLINK	0x0010
632 #define IOP_MGTIME		0x0020
633 #define IOP_CACHED_LINK		0x0040
634 #define IOP_FASTPERM_MAY_EXEC	0x0080
635 #define IOP_FLCTX		0x0100
636 
637 /*
638  * Inode state bits.  Protected by inode->i_lock
639  *
640  * Four bits determine the dirty state of the inode: I_DIRTY_SYNC,
641  * I_DIRTY_DATASYNC, I_DIRTY_PAGES, and I_DIRTY_TIME.
642  *
643  * Four bits define the lifetime of an inode.  Initially, inodes are I_NEW,
644  * until that flag is cleared.  I_WILL_FREE, I_FREEING and I_CLEAR are set at
645  * various stages of removing an inode.
646  *
647  * Two bits are used for locking and completion notification, I_NEW and I_SYNC.
648  *
649  * I_DIRTY_SYNC		Inode is dirty, but doesn't have to be written on
650  *			fdatasync() (unless I_DIRTY_DATASYNC is also set).
651  *			Timestamp updates are the usual cause.
652  * I_DIRTY_DATASYNC	Data-related inode changes pending.  We keep track of
653  *			these changes separately from I_DIRTY_SYNC so that we
654  *			don't have to write inode on fdatasync() when only
655  *			e.g. the timestamps have changed.
656  * I_DIRTY_PAGES	Inode has dirty pages.  Inode itself may be clean.
657  * I_DIRTY_TIME		The inode itself has dirty timestamps, and the
658  *			lazytime mount option is enabled.  We keep track of this
659  *			separately from I_DIRTY_SYNC in order to implement
660  *			lazytime.  This gets cleared if I_DIRTY_INODE
661  *			(I_DIRTY_SYNC and/or I_DIRTY_DATASYNC) gets set. But
662  *			I_DIRTY_TIME can still be set if I_DIRTY_SYNC is already
663  *			in place because writeback might already be in progress
664  *			and we don't want to lose the time update
665  * I_NEW		Serves as both a mutex and completion notification.
666  *			New inodes set I_NEW.  If two processes both create
667  *			the same inode, one of them will release its inode and
668  *			wait for I_NEW to be released before returning.
669  *			Inodes in I_WILL_FREE, I_FREEING or I_CLEAR state can
670  *			also cause waiting on I_NEW, without I_NEW actually
671  *			being set.  find_inode() uses this to prevent returning
672  *			nearly-dead inodes.
673  * I_WILL_FREE		Must be set when calling write_inode_now() if i_count
674  *			is zero.  I_FREEING must be set when I_WILL_FREE is
675  *			cleared.
676  * I_FREEING		Set when inode is about to be freed but still has dirty
677  *			pages or buffers attached or the inode itself is still
678  *			dirty.
679  * I_CLEAR		Added by clear_inode().  In this state the inode is
680  *			clean and can be destroyed.  Inode keeps I_FREEING.
681  *
682  *			Inodes that are I_WILL_FREE, I_FREEING or I_CLEAR are
683  *			prohibited for many purposes.  iget() must wait for
684  *			the inode to be completely released, then create it
685  *			anew.  Other functions will just ignore such inodes,
686  *			if appropriate.  I_NEW is used for waiting.
687  *
688  * I_SYNC		Writeback of inode is running. The bit is set during
689  *			data writeback, and cleared with a wakeup on the bit
690  *			address once it is done. The bit is also used to pin
691  *			the inode in memory for flusher thread.
692  *
693  * I_REFERENCED		Marks the inode as recently references on the LRU list.
694  *
695  * I_WB_SWITCH		Cgroup bdi_writeback switching in progress.  Used to
696  *			synchronize competing switching instances and to tell
697  *			wb stat updates to grab the i_pages lock.  See
698  *			inode_switch_wbs_work_fn() for details.
699  *
700  * I_OVL_INUSE		Used by overlayfs to get exclusive ownership on upper
701  *			and work dirs among overlayfs mounts.
702  *
703  * I_CREATING		New object's inode in the middle of setting up.
704  *
705  * I_DONTCACHE		Evict inode as soon as it is not used anymore.
706  *
707  * I_SYNC_QUEUED	Inode is queued in b_io or b_more_io writeback lists.
708  *			Used to detect that mark_inode_dirty() should not move
709  *			inode between dirty lists.
710  *
711  * I_PINNING_FSCACHE_WB	Inode is pinning an fscache object for writeback.
712  *
713  * I_LRU_ISOLATING	Inode is pinned being isolated from LRU without holding
714  *			i_count.
715  *
716  * Q: What is the difference between I_WILL_FREE and I_FREEING?
717  *
718  * __I_{SYNC,NEW,LRU_ISOLATING} are used to derive unique addresses to wait
719  * upon. There's one free address left.
720  */
721 
722 enum inode_state_bits {
723 	__I_NEW			= 0U,
724 	__I_SYNC		= 1U,
725 	__I_LRU_ISOLATING	= 2U
726 	/* reserved wait address bit 3 */
727 };
728 
729 enum inode_state_flags_enum {
730 	I_NEW			= (1U << __I_NEW),
731 	I_SYNC			= (1U << __I_SYNC),
732 	I_LRU_ISOLATING         = (1U << __I_LRU_ISOLATING),
733 	/* reserved flag bit 3 */
734 	I_DIRTY_SYNC		= (1U << 4),
735 	I_DIRTY_DATASYNC	= (1U << 5),
736 	I_DIRTY_PAGES		= (1U << 6),
737 	I_WILL_FREE		= (1U << 7),
738 	I_FREEING		= (1U << 8),
739 	I_CLEAR			= (1U << 9),
740 	I_REFERENCED		= (1U << 10),
741 	I_LINKABLE		= (1U << 11),
742 	I_DIRTY_TIME		= (1U << 12),
743 	I_WB_SWITCH		= (1U << 13),
744 	I_OVL_INUSE		= (1U << 14),
745 	I_CREATING		= (1U << 15),
746 	I_DONTCACHE		= (1U << 16),
747 	I_SYNC_QUEUED		= (1U << 17),
748 	I_PINNING_NETFS_WB	= (1U << 18)
749 };
750 
751 #define I_DIRTY_INODE (I_DIRTY_SYNC | I_DIRTY_DATASYNC)
752 #define I_DIRTY (I_DIRTY_INODE | I_DIRTY_PAGES)
753 #define I_DIRTY_ALL (I_DIRTY | I_DIRTY_TIME)
754 
755 /*
756  * Use inode_state_read() & friends to access.
757  */
758 struct inode_state_flags {
759 	enum inode_state_flags_enum __state;
760 };
761 
762 /*
763  * Keep mostly read-only and often accessed (especially for
764  * the RCU path lookup and 'stat' data) fields at the beginning
765  * of the 'struct inode'
766  */
767 struct inode {
768 	umode_t			i_mode;
769 	unsigned short		i_opflags;
770 	unsigned int		i_flags;
771 #ifdef CONFIG_FS_POSIX_ACL
772 	struct posix_acl	*i_acl;
773 	struct posix_acl	*i_default_acl;
774 #endif
775 	kuid_t			i_uid;
776 	kgid_t			i_gid;
777 
778 	const struct inode_operations	*i_op;
779 	struct super_block	*i_sb;
780 	struct address_space	*i_mapping;
781 
782 #ifdef CONFIG_SECURITY
783 	void			*i_security;
784 #endif
785 
786 	/* Stat data, not accessed from path walking */
787 	u64			i_ino;
788 	/*
789 	 * Filesystems may only read i_nlink directly.  They shall use the
790 	 * following functions for modification:
791 	 *
792 	 *    (set|clear|inc|drop)_nlink
793 	 *    inode_(inc|dec)_link_count
794 	 */
795 	union {
796 		const unsigned int i_nlink;
797 		unsigned int __i_nlink;
798 	};
799 	dev_t			i_rdev;
800 	loff_t			i_size;
801 	time64_t		i_atime_sec;
802 	time64_t		i_mtime_sec;
803 	time64_t		i_ctime_sec;
804 	u32			i_atime_nsec;
805 	u32			i_mtime_nsec;
806 	u32			i_ctime_nsec;
807 	u32			i_generation;
808 	spinlock_t		i_lock;	/* i_blocks, i_bytes, maybe i_size */
809 	unsigned short          i_bytes;
810 	u8			i_blkbits;
811 	enum rw_hint		i_write_hint;
812 	blkcnt_t		i_blocks;
813 
814 #ifdef __NEED_I_SIZE_ORDERED
815 	seqcount_t		i_size_seqcount;
816 #endif
817 
818 	/* Misc */
819 	struct inode_state_flags i_state;
820 	/* 32-bit hole */
821 	struct rw_semaphore	i_rwsem;
822 
823 	unsigned long		dirtied_when;	/* jiffies of first dirtying */
824 	unsigned long		dirtied_time_when;
825 
826 	struct hlist_node	i_hash;
827 	struct list_head	i_io_list;	/* backing dev IO list */
828 #ifdef CONFIG_CGROUP_WRITEBACK
829 	struct bdi_writeback	*i_wb;		/* the associated cgroup wb */
830 
831 	/* foreign inode detection, see wbc_detach_inode() */
832 	int			i_wb_frn_winner;
833 	u16			i_wb_frn_avg_time;
834 	u16			i_wb_frn_history;
835 #endif
836 	struct list_head	i_lru;		/* inode LRU list */
837 	struct list_head	i_sb_list;
838 	struct list_head	i_wb_list;	/* backing dev writeback list */
839 	union {
840 		struct hlist_head	i_dentry;
841 		struct rcu_head		i_rcu;
842 	};
843 	atomic64_t		i_version;
844 	atomic64_t		i_sequence; /* see futex */
845 	atomic_t		i_count;
846 	atomic_t		i_dio_count;
847 	atomic_t		i_writecount;
848 #if defined(CONFIG_IMA) || defined(CONFIG_FILE_LOCKING)
849 	atomic_t		i_readcount; /* struct files open RO */
850 #endif
851 	union {
852 		const struct file_operations	*i_fop;	/* former ->i_op->default_file_ops */
853 		void (*free_inode)(struct inode *);
854 	};
855 	struct file_lock_context	*i_flctx;
856 	struct address_space	i_data;
857 	union {
858 		struct list_head	i_devices;
859 		int			i_linklen;
860 	};
861 	union {
862 		struct pipe_inode_info	*i_pipe;
863 		struct cdev		*i_cdev;
864 		char			*i_link;
865 		unsigned		i_dir_seq;
866 	};
867 
868 
869 #ifdef CONFIG_FSNOTIFY
870 	__u32			i_fsnotify_mask; /* all events this inode cares about */
871 	/* 32-bit hole reserved for expanding i_fsnotify_mask */
872 	struct fsnotify_mark_connector __rcu	*i_fsnotify_marks;
873 #endif
874 
875 	void			*i_private; /* fs or device private pointer */
876 } __randomize_layout;
877 
878 /*
879  * i_state handling
880  *
881  * We hide all of it behind helpers so that we can validate consumers.
882  */
inode_state_read_once(struct inode * inode)883 static inline enum inode_state_flags_enum inode_state_read_once(struct inode *inode)
884 {
885 	return READ_ONCE(inode->i_state.__state);
886 }
887 
inode_state_read(struct inode * inode)888 static inline enum inode_state_flags_enum inode_state_read(struct inode *inode)
889 {
890 	lockdep_assert_held(&inode->i_lock);
891 	return inode->i_state.__state;
892 }
893 
inode_state_set_raw(struct inode * inode,enum inode_state_flags_enum flags)894 static inline void inode_state_set_raw(struct inode *inode,
895 				       enum inode_state_flags_enum flags)
896 {
897 	WRITE_ONCE(inode->i_state.__state, inode->i_state.__state | flags);
898 }
899 
inode_state_set(struct inode * inode,enum inode_state_flags_enum flags)900 static inline void inode_state_set(struct inode *inode,
901 				   enum inode_state_flags_enum flags)
902 {
903 	lockdep_assert_held(&inode->i_lock);
904 	inode_state_set_raw(inode, flags);
905 }
906 
inode_state_clear_raw(struct inode * inode,enum inode_state_flags_enum flags)907 static inline void inode_state_clear_raw(struct inode *inode,
908 					 enum inode_state_flags_enum flags)
909 {
910 	WRITE_ONCE(inode->i_state.__state, inode->i_state.__state & ~flags);
911 }
912 
inode_state_clear(struct inode * inode,enum inode_state_flags_enum flags)913 static inline void inode_state_clear(struct inode *inode,
914 				     enum inode_state_flags_enum flags)
915 {
916 	lockdep_assert_held(&inode->i_lock);
917 	inode_state_clear_raw(inode, flags);
918 }
919 
inode_state_assign_raw(struct inode * inode,enum inode_state_flags_enum flags)920 static inline void inode_state_assign_raw(struct inode *inode,
921 					  enum inode_state_flags_enum flags)
922 {
923 	WRITE_ONCE(inode->i_state.__state, flags);
924 }
925 
inode_state_assign(struct inode * inode,enum inode_state_flags_enum flags)926 static inline void inode_state_assign(struct inode *inode,
927 				      enum inode_state_flags_enum flags)
928 {
929 	lockdep_assert_held(&inode->i_lock);
930 	inode_state_assign_raw(inode, flags);
931 }
932 
inode_state_replace_raw(struct inode * inode,enum inode_state_flags_enum clearflags,enum inode_state_flags_enum setflags)933 static inline void inode_state_replace_raw(struct inode *inode,
934 					   enum inode_state_flags_enum clearflags,
935 					   enum inode_state_flags_enum setflags)
936 {
937 	enum inode_state_flags_enum flags;
938 	flags = inode->i_state.__state;
939 	flags &= ~clearflags;
940 	flags |= setflags;
941 	inode_state_assign_raw(inode, flags);
942 }
943 
inode_state_replace(struct inode * inode,enum inode_state_flags_enum clearflags,enum inode_state_flags_enum setflags)944 static inline void inode_state_replace(struct inode *inode,
945 				       enum inode_state_flags_enum clearflags,
946 				       enum inode_state_flags_enum setflags)
947 {
948 	lockdep_assert_held(&inode->i_lock);
949 	inode_state_replace_raw(inode, clearflags, setflags);
950 }
951 
inode_set_cached_link(struct inode * inode,char * link,int linklen)952 static inline void inode_set_cached_link(struct inode *inode, char *link, int linklen)
953 {
954 	VFS_WARN_ON_INODE(strlen(link) != linklen, inode);
955 	VFS_WARN_ON_INODE(inode->i_opflags & IOP_CACHED_LINK, inode);
956 	inode->i_link = link;
957 	inode->i_linklen = linklen;
958 	inode->i_opflags |= IOP_CACHED_LINK;
959 }
960 
961 /*
962  * Get bit address from inode->i_state to use with wait_var_event()
963  * infrastructre.
964  */
965 #define inode_state_wait_address(inode, bit) ((char *)&(inode)->i_state + (bit))
966 
967 struct wait_queue_head *inode_bit_waitqueue(struct wait_bit_queue_entry *wqe,
968 					    struct inode *inode, u32 bit);
969 
inode_wake_up_bit(struct inode * inode,u32 bit)970 static inline void inode_wake_up_bit(struct inode *inode, u32 bit)
971 {
972 	/* Caller is responsible for correct memory barriers. */
973 	wake_up_var(inode_state_wait_address(inode, bit));
974 }
975 
976 struct timespec64 timestamp_truncate(struct timespec64 t, struct inode *inode);
977 
i_blocksize(const struct inode * node)978 static inline unsigned int i_blocksize(const struct inode *node)
979 {
980 	return (1 << node->i_blkbits);
981 }
982 
inode_unhashed(struct inode * inode)983 static inline int inode_unhashed(struct inode *inode)
984 {
985 	return hlist_unhashed(&inode->i_hash);
986 }
987 
988 /*
989  * __mark_inode_dirty expects inodes to be hashed.  Since we don't
990  * want special inodes in the fileset inode space, we make them
991  * appear hashed, but do not put on any lists.  hlist_del()
992  * will work fine and require no locking.
993  */
inode_fake_hash(struct inode * inode)994 static inline void inode_fake_hash(struct inode *inode)
995 {
996 	hlist_add_fake(&inode->i_hash);
997 }
998 
999 void wait_on_new_inode(struct inode *inode);
1000 
1001 /*
1002  * inode->i_rwsem nesting subclasses for the lock validator:
1003  *
1004  * 0: the object of the current VFS operation
1005  * 1: parent
1006  * 2: child/target
1007  * 3: xattr
1008  * 4: second non-directory
1009  * 5: second parent (when locking independent directories in rename)
1010  *
1011  * I_MUTEX_NONDIR2 is for certain operations (such as rename) which lock two
1012  * non-directories at once.
1013  *
1014  * The locking order between these classes is
1015  * parent[2] -> child -> grandchild -> normal -> xattr -> second non-directory
1016  */
1017 enum inode_i_mutex_lock_class
1018 {
1019 	I_MUTEX_NORMAL,
1020 	I_MUTEX_PARENT,
1021 	I_MUTEX_CHILD,
1022 	I_MUTEX_XATTR,
1023 	I_MUTEX_NONDIR2,
1024 	I_MUTEX_PARENT2,
1025 };
1026 
inode_lock(struct inode * inode)1027 static inline void inode_lock(struct inode *inode)
1028 {
1029 	down_write(&inode->i_rwsem);
1030 }
1031 
inode_lock_killable(struct inode * inode)1032 static inline __must_check int inode_lock_killable(struct inode *inode)
1033 {
1034 	return down_write_killable(&inode->i_rwsem);
1035 }
1036 
inode_unlock(struct inode * inode)1037 static inline void inode_unlock(struct inode *inode)
1038 {
1039 	up_write(&inode->i_rwsem);
1040 }
1041 
inode_lock_shared(struct inode * inode)1042 static inline void inode_lock_shared(struct inode *inode)
1043 {
1044 	down_read(&inode->i_rwsem);
1045 }
1046 
inode_lock_shared_killable(struct inode * inode)1047 static inline __must_check int inode_lock_shared_killable(struct inode *inode)
1048 {
1049 	return down_read_killable(&inode->i_rwsem);
1050 }
1051 
inode_unlock_shared(struct inode * inode)1052 static inline void inode_unlock_shared(struct inode *inode)
1053 {
1054 	up_read(&inode->i_rwsem);
1055 }
1056 
inode_trylock(struct inode * inode)1057 static inline int inode_trylock(struct inode *inode)
1058 {
1059 	return down_write_trylock(&inode->i_rwsem);
1060 }
1061 
inode_trylock_shared(struct inode * inode)1062 static inline int inode_trylock_shared(struct inode *inode)
1063 {
1064 	return down_read_trylock(&inode->i_rwsem);
1065 }
1066 
inode_is_locked(struct inode * inode)1067 static inline int inode_is_locked(struct inode *inode)
1068 {
1069 	return rwsem_is_locked(&inode->i_rwsem);
1070 }
1071 
inode_lock_nested(struct inode * inode,unsigned subclass)1072 static inline void inode_lock_nested(struct inode *inode, unsigned subclass)
1073 {
1074 	down_write_nested(&inode->i_rwsem, subclass);
1075 }
1076 
inode_lock_shared_nested(struct inode * inode,unsigned subclass)1077 static inline void inode_lock_shared_nested(struct inode *inode, unsigned subclass)
1078 {
1079 	down_read_nested(&inode->i_rwsem, subclass);
1080 }
1081 
filemap_invalidate_lock(struct address_space * mapping)1082 static inline void filemap_invalidate_lock(struct address_space *mapping)
1083 {
1084 	down_write(&mapping->invalidate_lock);
1085 }
1086 
filemap_invalidate_unlock(struct address_space * mapping)1087 static inline void filemap_invalidate_unlock(struct address_space *mapping)
1088 {
1089 	up_write(&mapping->invalidate_lock);
1090 }
1091 
filemap_invalidate_lock_shared(struct address_space * mapping)1092 static inline void filemap_invalidate_lock_shared(struct address_space *mapping)
1093 {
1094 	down_read(&mapping->invalidate_lock);
1095 }
1096 
filemap_invalidate_trylock_shared(struct address_space * mapping)1097 static inline int filemap_invalidate_trylock_shared(
1098 					struct address_space *mapping)
1099 {
1100 	return down_read_trylock(&mapping->invalidate_lock);
1101 }
1102 
filemap_invalidate_unlock_shared(struct address_space * mapping)1103 static inline void filemap_invalidate_unlock_shared(
1104 					struct address_space *mapping)
1105 {
1106 	up_read(&mapping->invalidate_lock);
1107 }
1108 
1109 void lock_two_nondirectories(struct inode *, struct inode*);
1110 void unlock_two_nondirectories(struct inode *, struct inode*);
1111 
1112 void filemap_invalidate_lock_two(struct address_space *mapping1,
1113 				 struct address_space *mapping2);
1114 void filemap_invalidate_unlock_two(struct address_space *mapping1,
1115 				   struct address_space *mapping2);
1116 
1117 
1118 /*
1119  * NOTE: in a 32bit arch with a preemptable kernel and
1120  * an UP compile the i_size_read/write must be atomic
1121  * with respect to the local cpu (unlike with preempt disabled),
1122  * but they don't need to be atomic with respect to other cpus like in
1123  * true SMP (so they need either to either locally disable irq around
1124  * the read or for example on x86 they can be still implemented as a
1125  * cmpxchg8b without the need of the lock prefix). For SMP compiles
1126  * and 64bit archs it makes no difference if preempt is enabled or not.
1127  */
i_size_read(const struct inode * inode)1128 static inline loff_t i_size_read(const struct inode *inode)
1129 {
1130 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
1131 	loff_t i_size;
1132 	unsigned int seq;
1133 
1134 	do {
1135 		seq = read_seqcount_begin(&inode->i_size_seqcount);
1136 		i_size = inode->i_size;
1137 	} while (read_seqcount_retry(&inode->i_size_seqcount, seq));
1138 	return i_size;
1139 #elif BITS_PER_LONG==32 && defined(CONFIG_PREEMPTION)
1140 	loff_t i_size;
1141 
1142 	preempt_disable();
1143 	i_size = inode->i_size;
1144 	preempt_enable();
1145 	return i_size;
1146 #else
1147 	/* Pairs with smp_store_release() in i_size_write() */
1148 	return smp_load_acquire(&inode->i_size);
1149 #endif
1150 }
1151 
1152 /*
1153  * NOTE: unlike i_size_read(), i_size_write() does need locking around it
1154  * (normally i_rwsem), otherwise on 32bit/SMP an update of i_size_seqcount
1155  * can be lost, resulting in subsequent i_size_read() calls spinning forever.
1156  */
i_size_write(struct inode * inode,loff_t i_size)1157 static inline void i_size_write(struct inode *inode, loff_t i_size)
1158 {
1159 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
1160 	preempt_disable();
1161 	write_seqcount_begin(&inode->i_size_seqcount);
1162 	inode->i_size = i_size;
1163 	write_seqcount_end(&inode->i_size_seqcount);
1164 	preempt_enable();
1165 #elif BITS_PER_LONG==32 && defined(CONFIG_PREEMPTION)
1166 	preempt_disable();
1167 	inode->i_size = i_size;
1168 	preempt_enable();
1169 #else
1170 	/*
1171 	 * Pairs with smp_load_acquire() in i_size_read() to ensure
1172 	 * changes related to inode size (such as page contents) are
1173 	 * visible before we see the changed inode size.
1174 	 */
1175 	smp_store_release(&inode->i_size, i_size);
1176 #endif
1177 }
1178 
iminor(const struct inode * inode)1179 static inline unsigned iminor(const struct inode *inode)
1180 {
1181 	return MINOR(inode->i_rdev);
1182 }
1183 
imajor(const struct inode * inode)1184 static inline unsigned imajor(const struct inode *inode)
1185 {
1186 	return MAJOR(inode->i_rdev);
1187 }
1188 
1189 struct fown_struct {
1190 	struct file *file;	/* backpointer for security modules */
1191 	rwlock_t lock;          /* protects pid, uid, euid fields */
1192 	struct pid *pid;	/* pid or -pgrp where SIGIO should be sent */
1193 	enum pid_type pid_type;	/* Kind of process group SIGIO should be sent to */
1194 	kuid_t uid, euid;	/* uid/euid of process setting the owner */
1195 	int signum;		/* posix.1b rt signal to be delivered on IO */
1196 };
1197 
1198 /**
1199  * struct file_ra_state - Track a file's readahead state.
1200  * @start: Where the most recent readahead started.
1201  * @size: Number of pages read in the most recent readahead.
1202  * @async_size: Numer of pages that were/are not needed immediately
1203  *      and so were/are genuinely "ahead".  Start next readahead when
1204  *      the first of these pages is accessed.
1205  * @ra_pages: Maximum size of a readahead request, copied from the bdi.
1206  * @order: Preferred folio order used for most recent readahead.
1207  * @mmap_miss: How many mmap accesses missed in the page cache.
1208  * @prev_pos: The last byte in the most recent read request.
1209  *
1210  * When this structure is passed to ->readahead(), the "most recent"
1211  * readahead means the current readahead.
1212  */
1213 struct file_ra_state {
1214 	pgoff_t start;
1215 	unsigned int size;
1216 	unsigned int async_size;
1217 	unsigned int ra_pages;
1218 	unsigned short order;
1219 	unsigned short mmap_miss;
1220 	loff_t prev_pos;
1221 };
1222 
1223 /*
1224  * Check if @index falls in the readahead windows.
1225  */
ra_has_index(struct file_ra_state * ra,pgoff_t index)1226 static inline int ra_has_index(struct file_ra_state *ra, pgoff_t index)
1227 {
1228 	return (index >= ra->start &&
1229 		index <  ra->start + ra->size);
1230 }
1231 
1232 /**
1233  * struct file - Represents a file
1234  * @f_lock: Protects f_ep, f_flags. Must not be taken from IRQ context.
1235  * @f_mode: FMODE_* flags often used in hotpaths
1236  * @f_op: file operations
1237  * @f_mapping: Contents of a cacheable, mappable object.
1238  * @private_data: filesystem or driver specific data
1239  * @f_inode: cached inode
1240  * @f_flags: file flags
1241  * @f_iocb_flags: iocb flags
1242  * @f_cred: stashed credentials of creator/opener
1243  * @f_owner: file owner
1244  * @f_path: path of the file
1245  * @__f_path: writable alias for @f_path; *ONLY* for core VFS and only before
1246  *   the file gets open
1247  * @f_pos_lock: lock protecting file position
1248  * @f_pipe: specific to pipes
1249  * @f_pos: file position
1250  * @f_security: LSM security context of this file
1251  * @f_wb_err: writeback error
1252  * @f_sb_err: per sb writeback errors
1253  * @f_ep: link of all epoll hooks for this file
1254  * @f_task_work: task work entry point
1255  * @f_llist: work queue entrypoint
1256  * @f_ra: file's readahead state
1257  * @f_freeptr: Pointer used by SLAB_TYPESAFE_BY_RCU file cache (don't touch.)
1258  * @f_ref: reference count
1259  */
1260 struct file {
1261 	spinlock_t			f_lock;
1262 	fmode_t				f_mode;
1263 	const struct file_operations	*f_op;
1264 	struct address_space		*f_mapping;
1265 	void				*private_data;
1266 	struct inode			*f_inode;
1267 	unsigned int			f_flags;
1268 	unsigned int			f_iocb_flags;
1269 	const struct cred		*f_cred;
1270 	struct fown_struct		*f_owner;
1271 	/* --- cacheline 1 boundary (64 bytes) --- */
1272 	union {
1273 		const struct path	f_path;
1274 		struct path		__f_path;
1275 	};
1276 	union {
1277 		/* regular files (with FMODE_ATOMIC_POS) and directories */
1278 		struct mutex		f_pos_lock;
1279 		/* pipes */
1280 		u64			f_pipe;
1281 	};
1282 	loff_t				f_pos;
1283 #ifdef CONFIG_SECURITY
1284 	void				*f_security;
1285 #endif
1286 	/* --- cacheline 2 boundary (128 bytes) --- */
1287 	errseq_t			f_wb_err;
1288 	errseq_t			f_sb_err;
1289 #ifdef CONFIG_EPOLL
1290 	struct hlist_head		*f_ep;
1291 #endif
1292 	union {
1293 		struct callback_head	f_task_work;
1294 		struct llist_node	f_llist;
1295 		struct file_ra_state	f_ra;
1296 		freeptr_t		f_freeptr;
1297 	};
1298 	file_ref_t			f_ref;
1299 	/* --- cacheline 3 boundary (192 bytes) --- */
1300 } __randomize_layout
1301   __attribute__((aligned(4)));	/* lest something weird decides that 2 is OK */
1302 
1303 struct file_handle {
1304 	__u32 handle_bytes;
1305 	int handle_type;
1306 	/* file identifier */
1307 	unsigned char f_handle[] __counted_by(handle_bytes);
1308 };
1309 
get_file(struct file * f)1310 static inline struct file *get_file(struct file *f)
1311 {
1312 	file_ref_inc(&f->f_ref);
1313 	return f;
1314 }
1315 
1316 struct file *get_file_rcu(struct file __rcu **f);
1317 struct file *get_file_active(struct file **f);
1318 
1319 #define file_count(f)	file_ref_read(&(f)->f_ref)
1320 
1321 #define	MAX_NON_LFS	((1UL<<31) - 1)
1322 
1323 /* Page cache limit. The filesystems should put that into their s_maxbytes
1324    limits, otherwise bad things can happen in VM. */
1325 #if BITS_PER_LONG==32
1326 #define MAX_LFS_FILESIZE	((loff_t)ULONG_MAX << PAGE_SHIFT)
1327 #elif BITS_PER_LONG==64
1328 #define MAX_LFS_FILESIZE 	((loff_t)LLONG_MAX)
1329 #endif
1330 
1331 /* legacy typedef, should eventually be removed */
1332 typedef void *fl_owner_t;
1333 
1334 struct file_lock;
1335 struct file_lease;
1336 
1337 /* The following constant reflects the upper bound of the file/locking space */
1338 #ifndef OFFSET_MAX
1339 #define OFFSET_MAX	type_max(loff_t)
1340 #define OFFT_OFFSET_MAX	type_max(off_t)
1341 #endif
1342 
1343 int file_f_owner_allocate(struct file *file);
file_f_owner(const struct file * file)1344 static inline struct fown_struct *file_f_owner(const struct file *file)
1345 {
1346 	return READ_ONCE(file->f_owner);
1347 }
1348 
1349 extern void send_sigio(struct fown_struct *fown, int fd, int band);
1350 
file_inode(const struct file * f)1351 static inline struct inode *file_inode(const struct file *f)
1352 {
1353 	return f->f_inode;
1354 }
1355 
1356 /*
1357  * file_dentry() is a relic from the days that overlayfs was using files with a
1358  * "fake" path, meaning, f_path on overlayfs and f_inode on underlying fs.
1359  * In those days, file_dentry() was needed to get the underlying fs dentry that
1360  * matches f_inode.
1361  * Files with "fake" path should not exist nowadays, so use an assertion to make
1362  * sure that file_dentry() was not papering over filesystem bugs.
1363  */
file_dentry(const struct file * file)1364 static inline struct dentry *file_dentry(const struct file *file)
1365 {
1366 	struct dentry *dentry = file->f_path.dentry;
1367 
1368 	WARN_ON_ONCE(d_inode(dentry) != file_inode(file));
1369 	return dentry;
1370 }
1371 
1372 struct fasync_struct {
1373 	rwlock_t		fa_lock;
1374 	int			magic;
1375 	int			fa_fd;
1376 	struct fasync_struct	*fa_next; /* singly linked list */
1377 	struct file		*fa_file;
1378 	struct rcu_head		fa_rcu;
1379 };
1380 
1381 #define FASYNC_MAGIC 0x4601
1382 
1383 /* SMP safe fasync helpers: */
1384 extern int fasync_helper(int, struct file *, int, struct fasync_struct **);
1385 extern struct fasync_struct *fasync_insert_entry(int, struct file *, struct fasync_struct **, struct fasync_struct *);
1386 extern int fasync_remove_entry(struct file *, struct fasync_struct **);
1387 extern struct fasync_struct *fasync_alloc(void);
1388 extern void fasync_free(struct fasync_struct *);
1389 
1390 /* can be called from interrupts */
1391 extern void kill_fasync(struct fasync_struct **, int, int);
1392 
1393 extern void __f_setown(struct file *filp, struct pid *, enum pid_type, int force);
1394 extern int f_setown(struct file *filp, int who, int force);
1395 extern void f_delown(struct file *filp);
1396 extern pid_t f_getown(struct file *filp);
1397 extern int send_sigurg(struct file *file);
1398 
1399 /*
1400  *	Umount options
1401  */
1402 
1403 #define MNT_FORCE	0x00000001	/* Attempt to forcibily umount */
1404 #define MNT_DETACH	0x00000002	/* Just detach from the tree */
1405 #define MNT_EXPIRE	0x00000004	/* Mark for expiry */
1406 #define UMOUNT_NOFOLLOW	0x00000008	/* Don't follow symlink on umount */
1407 #define UMOUNT_UNUSED	0x80000000	/* Flag guaranteed to be unused */
1408 
i_user_ns(const struct inode * inode)1409 static inline struct user_namespace *i_user_ns(const struct inode *inode)
1410 {
1411 	return inode->i_sb->s_user_ns;
1412 }
1413 
1414 /* Helper functions so that in most cases filesystems will
1415  * not need to deal directly with kuid_t and kgid_t and can
1416  * instead deal with the raw numeric values that are stored
1417  * in the filesystem.
1418  */
i_uid_read(const struct inode * inode)1419 static inline uid_t i_uid_read(const struct inode *inode)
1420 {
1421 	return from_kuid(i_user_ns(inode), inode->i_uid);
1422 }
1423 
i_gid_read(const struct inode * inode)1424 static inline gid_t i_gid_read(const struct inode *inode)
1425 {
1426 	return from_kgid(i_user_ns(inode), inode->i_gid);
1427 }
1428 
i_uid_write(struct inode * inode,uid_t uid)1429 static inline void i_uid_write(struct inode *inode, uid_t uid)
1430 {
1431 	inode->i_uid = make_kuid(i_user_ns(inode), uid);
1432 }
1433 
i_gid_write(struct inode * inode,gid_t gid)1434 static inline void i_gid_write(struct inode *inode, gid_t gid)
1435 {
1436 	inode->i_gid = make_kgid(i_user_ns(inode), gid);
1437 }
1438 
1439 /**
1440  * i_uid_into_vfsuid - map an inode's i_uid down according to an idmapping
1441  * @idmap: idmap of the mount the inode was found from
1442  * @inode: inode to map
1443  *
1444  * Return: whe inode's i_uid mapped down according to @idmap.
1445  * If the inode's i_uid has no mapping INVALID_VFSUID is returned.
1446  */
i_uid_into_vfsuid(struct mnt_idmap * idmap,const struct inode * inode)1447 static inline vfsuid_t i_uid_into_vfsuid(struct mnt_idmap *idmap,
1448 					 const struct inode *inode)
1449 {
1450 	return make_vfsuid(idmap, i_user_ns(inode), inode->i_uid);
1451 }
1452 
1453 /**
1454  * i_uid_needs_update - check whether inode's i_uid needs to be updated
1455  * @idmap: idmap of the mount the inode was found from
1456  * @attr: the new attributes of @inode
1457  * @inode: the inode to update
1458  *
1459  * Check whether the $inode's i_uid field needs to be updated taking idmapped
1460  * mounts into account if the filesystem supports it.
1461  *
1462  * Return: true if @inode's i_uid field needs to be updated, false if not.
1463  */
i_uid_needs_update(struct mnt_idmap * idmap,const struct iattr * attr,const struct inode * inode)1464 static inline bool i_uid_needs_update(struct mnt_idmap *idmap,
1465 				      const struct iattr *attr,
1466 				      const struct inode *inode)
1467 {
1468 	return ((attr->ia_valid & ATTR_UID) &&
1469 		!vfsuid_eq(attr->ia_vfsuid,
1470 			   i_uid_into_vfsuid(idmap, inode)));
1471 }
1472 
1473 /**
1474  * i_uid_update - update @inode's i_uid field
1475  * @idmap: idmap of the mount the inode was found from
1476  * @attr: the new attributes of @inode
1477  * @inode: the inode to update
1478  *
1479  * Safely update @inode's i_uid field translating the vfsuid of any idmapped
1480  * mount into the filesystem kuid.
1481  */
i_uid_update(struct mnt_idmap * idmap,const struct iattr * attr,struct inode * inode)1482 static inline void i_uid_update(struct mnt_idmap *idmap,
1483 				const struct iattr *attr,
1484 				struct inode *inode)
1485 {
1486 	if (attr->ia_valid & ATTR_UID)
1487 		inode->i_uid = from_vfsuid(idmap, i_user_ns(inode),
1488 					   attr->ia_vfsuid);
1489 }
1490 
1491 /**
1492  * i_gid_into_vfsgid - map an inode's i_gid down according to an idmapping
1493  * @idmap: idmap of the mount the inode was found from
1494  * @inode: inode to map
1495  *
1496  * Return: the inode's i_gid mapped down according to @idmap.
1497  * If the inode's i_gid has no mapping INVALID_VFSGID is returned.
1498  */
i_gid_into_vfsgid(struct mnt_idmap * idmap,const struct inode * inode)1499 static inline vfsgid_t i_gid_into_vfsgid(struct mnt_idmap *idmap,
1500 					 const struct inode *inode)
1501 {
1502 	return make_vfsgid(idmap, i_user_ns(inode), inode->i_gid);
1503 }
1504 
1505 /**
1506  * i_gid_needs_update - check whether inode's i_gid needs to be updated
1507  * @idmap: idmap of the mount the inode was found from
1508  * @attr: the new attributes of @inode
1509  * @inode: the inode to update
1510  *
1511  * Check whether the $inode's i_gid field needs to be updated taking idmapped
1512  * mounts into account if the filesystem supports it.
1513  *
1514  * Return: true if @inode's i_gid field needs to be updated, false if not.
1515  */
i_gid_needs_update(struct mnt_idmap * idmap,const struct iattr * attr,const struct inode * inode)1516 static inline bool i_gid_needs_update(struct mnt_idmap *idmap,
1517 				      const struct iattr *attr,
1518 				      const struct inode *inode)
1519 {
1520 	return ((attr->ia_valid & ATTR_GID) &&
1521 		!vfsgid_eq(attr->ia_vfsgid,
1522 			   i_gid_into_vfsgid(idmap, inode)));
1523 }
1524 
1525 /**
1526  * i_gid_update - update @inode's i_gid field
1527  * @idmap: idmap of the mount the inode was found from
1528  * @attr: the new attributes of @inode
1529  * @inode: the inode to update
1530  *
1531  * Safely update @inode's i_gid field translating the vfsgid of any idmapped
1532  * mount into the filesystem kgid.
1533  */
i_gid_update(struct mnt_idmap * idmap,const struct iattr * attr,struct inode * inode)1534 static inline void i_gid_update(struct mnt_idmap *idmap,
1535 				const struct iattr *attr,
1536 				struct inode *inode)
1537 {
1538 	if (attr->ia_valid & ATTR_GID)
1539 		inode->i_gid = from_vfsgid(idmap, i_user_ns(inode),
1540 					   attr->ia_vfsgid);
1541 }
1542 
1543 /**
1544  * inode_fsuid_set - initialize inode's i_uid field with callers fsuid
1545  * @inode: inode to initialize
1546  * @idmap: idmap of the mount the inode was found from
1547  *
1548  * Initialize the i_uid field of @inode. If the inode was found/created via
1549  * an idmapped mount map the caller's fsuid according to @idmap.
1550  */
inode_fsuid_set(struct inode * inode,struct mnt_idmap * idmap)1551 static inline void inode_fsuid_set(struct inode *inode,
1552 				   struct mnt_idmap *idmap)
1553 {
1554 	inode->i_uid = mapped_fsuid(idmap, i_user_ns(inode));
1555 }
1556 
1557 /**
1558  * inode_fsgid_set - initialize inode's i_gid field with callers fsgid
1559  * @inode: inode to initialize
1560  * @idmap: idmap of the mount the inode was found from
1561  *
1562  * Initialize the i_gid field of @inode. If the inode was found/created via
1563  * an idmapped mount map the caller's fsgid according to @idmap.
1564  */
inode_fsgid_set(struct inode * inode,struct mnt_idmap * idmap)1565 static inline void inode_fsgid_set(struct inode *inode,
1566 				   struct mnt_idmap *idmap)
1567 {
1568 	inode->i_gid = mapped_fsgid(idmap, i_user_ns(inode));
1569 }
1570 
1571 /**
1572  * fsuidgid_has_mapping() - check whether caller's fsuid/fsgid is mapped
1573  * @sb: the superblock we want a mapping in
1574  * @idmap: idmap of the relevant mount
1575  *
1576  * Check whether the caller's fsuid and fsgid have a valid mapping in the
1577  * s_user_ns of the superblock @sb. If the caller is on an idmapped mount map
1578  * the caller's fsuid and fsgid according to the @idmap first.
1579  *
1580  * Return: true if fsuid and fsgid is mapped, false if not.
1581  */
fsuidgid_has_mapping(struct super_block * sb,struct mnt_idmap * idmap)1582 static inline bool fsuidgid_has_mapping(struct super_block *sb,
1583 					struct mnt_idmap *idmap)
1584 {
1585 	struct user_namespace *fs_userns = sb->s_user_ns;
1586 	kuid_t kuid;
1587 	kgid_t kgid;
1588 
1589 	kuid = mapped_fsuid(idmap, fs_userns);
1590 	if (!uid_valid(kuid))
1591 		return false;
1592 	kgid = mapped_fsgid(idmap, fs_userns);
1593 	if (!gid_valid(kgid))
1594 		return false;
1595 	return kuid_has_mapping(fs_userns, kuid) &&
1596 	       kgid_has_mapping(fs_userns, kgid);
1597 }
1598 
1599 struct timespec64 current_time(struct inode *inode);
1600 struct timespec64 inode_set_ctime_current(struct inode *inode);
1601 struct timespec64 inode_set_ctime_deleg(struct inode *inode,
1602 					struct timespec64 update);
1603 
inode_get_atime_sec(const struct inode * inode)1604 static inline time64_t inode_get_atime_sec(const struct inode *inode)
1605 {
1606 	return inode->i_atime_sec;
1607 }
1608 
inode_get_atime_nsec(const struct inode * inode)1609 static inline long inode_get_atime_nsec(const struct inode *inode)
1610 {
1611 	return inode->i_atime_nsec;
1612 }
1613 
inode_get_atime(const struct inode * inode)1614 static inline struct timespec64 inode_get_atime(const struct inode *inode)
1615 {
1616 	struct timespec64 ts = { .tv_sec  = inode_get_atime_sec(inode),
1617 				 .tv_nsec = inode_get_atime_nsec(inode) };
1618 
1619 	return ts;
1620 }
1621 
inode_set_atime_to_ts(struct inode * inode,struct timespec64 ts)1622 static inline struct timespec64 inode_set_atime_to_ts(struct inode *inode,
1623 						      struct timespec64 ts)
1624 {
1625 	inode->i_atime_sec = ts.tv_sec;
1626 	inode->i_atime_nsec = ts.tv_nsec;
1627 	return ts;
1628 }
1629 
inode_set_atime(struct inode * inode,time64_t sec,long nsec)1630 static inline struct timespec64 inode_set_atime(struct inode *inode,
1631 						time64_t sec, long nsec)
1632 {
1633 	struct timespec64 ts = { .tv_sec  = sec,
1634 				 .tv_nsec = nsec };
1635 
1636 	return inode_set_atime_to_ts(inode, ts);
1637 }
1638 
inode_get_mtime_sec(const struct inode * inode)1639 static inline time64_t inode_get_mtime_sec(const struct inode *inode)
1640 {
1641 	return inode->i_mtime_sec;
1642 }
1643 
inode_get_mtime_nsec(const struct inode * inode)1644 static inline long inode_get_mtime_nsec(const struct inode *inode)
1645 {
1646 	return inode->i_mtime_nsec;
1647 }
1648 
inode_get_mtime(const struct inode * inode)1649 static inline struct timespec64 inode_get_mtime(const struct inode *inode)
1650 {
1651 	struct timespec64 ts = { .tv_sec  = inode_get_mtime_sec(inode),
1652 				 .tv_nsec = inode_get_mtime_nsec(inode) };
1653 	return ts;
1654 }
1655 
inode_set_mtime_to_ts(struct inode * inode,struct timespec64 ts)1656 static inline struct timespec64 inode_set_mtime_to_ts(struct inode *inode,
1657 						      struct timespec64 ts)
1658 {
1659 	inode->i_mtime_sec = ts.tv_sec;
1660 	inode->i_mtime_nsec = ts.tv_nsec;
1661 	return ts;
1662 }
1663 
inode_set_mtime(struct inode * inode,time64_t sec,long nsec)1664 static inline struct timespec64 inode_set_mtime(struct inode *inode,
1665 						time64_t sec, long nsec)
1666 {
1667 	struct timespec64 ts = { .tv_sec  = sec,
1668 				 .tv_nsec = nsec };
1669 	return inode_set_mtime_to_ts(inode, ts);
1670 }
1671 
1672 /*
1673  * Multigrain timestamps
1674  *
1675  * Conditionally use fine-grained ctime and mtime timestamps when there
1676  * are users actively observing them via getattr. The primary use-case
1677  * for this is NFS clients that use the ctime to distinguish between
1678  * different states of the file, and that are often fooled by multiple
1679  * operations that occur in the same coarse-grained timer tick.
1680  */
1681 #define I_CTIME_QUERIED		((u32)BIT(31))
1682 
inode_get_ctime_sec(const struct inode * inode)1683 static inline time64_t inode_get_ctime_sec(const struct inode *inode)
1684 {
1685 	return inode->i_ctime_sec;
1686 }
1687 
inode_get_ctime_nsec(const struct inode * inode)1688 static inline long inode_get_ctime_nsec(const struct inode *inode)
1689 {
1690 	return inode->i_ctime_nsec & ~I_CTIME_QUERIED;
1691 }
1692 
inode_get_ctime(const struct inode * inode)1693 static inline struct timespec64 inode_get_ctime(const struct inode *inode)
1694 {
1695 	struct timespec64 ts = { .tv_sec  = inode_get_ctime_sec(inode),
1696 				 .tv_nsec = inode_get_ctime_nsec(inode) };
1697 
1698 	return ts;
1699 }
1700 
1701 struct timespec64 inode_set_ctime_to_ts(struct inode *inode, struct timespec64 ts);
1702 
1703 /**
1704  * inode_set_ctime - set the ctime in the inode
1705  * @inode: inode in which to set the ctime
1706  * @sec: tv_sec value to set
1707  * @nsec: tv_nsec value to set
1708  *
1709  * Set the ctime in @inode to { @sec, @nsec }
1710  */
inode_set_ctime(struct inode * inode,time64_t sec,long nsec)1711 static inline struct timespec64 inode_set_ctime(struct inode *inode,
1712 						time64_t sec, long nsec)
1713 {
1714 	struct timespec64 ts = { .tv_sec  = sec,
1715 				 .tv_nsec = nsec };
1716 
1717 	return inode_set_ctime_to_ts(inode, ts);
1718 }
1719 
1720 struct timespec64 simple_inode_init_ts(struct inode *inode);
1721 
inode_time_dirty_flag(struct inode * inode)1722 static inline int inode_time_dirty_flag(struct inode *inode)
1723 {
1724 	if (inode->i_sb->s_flags & SB_LAZYTIME)
1725 		return I_DIRTY_TIME;
1726 	return I_DIRTY_SYNC;
1727 }
1728 
1729 /*
1730  * Snapshotting support.
1731  */
1732 
1733 /**
1734  * file_write_started - check if SB_FREEZE_WRITE is held
1735  * @file: the file we write to
1736  *
1737  * May be false positive with !CONFIG_LOCKDEP/LOCK_STATE_UNKNOWN.
1738  * May be false positive with !S_ISREG, because file_start_write() has
1739  * no effect on !S_ISREG.
1740  */
file_write_started(const struct file * file)1741 static inline bool file_write_started(const struct file *file)
1742 {
1743 	if (!S_ISREG(file_inode(file)->i_mode))
1744 		return true;
1745 	return sb_write_started(file_inode(file)->i_sb);
1746 }
1747 
1748 /**
1749  * file_write_not_started - check if SB_FREEZE_WRITE is not held
1750  * @file: the file we write to
1751  *
1752  * May be false positive with !CONFIG_LOCKDEP/LOCK_STATE_UNKNOWN.
1753  * May be false positive with !S_ISREG, because file_start_write() has
1754  * no effect on !S_ISREG.
1755  */
file_write_not_started(const struct file * file)1756 static inline bool file_write_not_started(const struct file *file)
1757 {
1758 	if (!S_ISREG(file_inode(file)->i_mode))
1759 		return true;
1760 	return sb_write_not_started(file_inode(file)->i_sb);
1761 }
1762 
1763 bool inode_owner_or_capable(struct mnt_idmap *idmap,
1764 			    const struct inode *inode);
1765 
1766 /*
1767  * VFS helper functions..
1768  */
1769 int vfs_create(struct mnt_idmap *, struct dentry *, umode_t,
1770 	       struct delegated_inode *);
1771 struct dentry *vfs_mkdir(struct mnt_idmap *, struct inode *,
1772 			 struct dentry *, umode_t, struct delegated_inode *);
1773 int vfs_mknod(struct mnt_idmap *, struct inode *, struct dentry *,
1774 	      umode_t, dev_t, struct delegated_inode *);
1775 int vfs_symlink(struct mnt_idmap *, struct inode *,
1776 		struct dentry *, const char *, struct delegated_inode *);
1777 int vfs_link(struct dentry *, struct mnt_idmap *, struct inode *,
1778 	     struct dentry *, struct delegated_inode *);
1779 int vfs_rmdir(struct mnt_idmap *, struct inode *, struct dentry *,
1780 	      struct delegated_inode *);
1781 int vfs_unlink(struct mnt_idmap *, struct inode *, struct dentry *,
1782 	       struct delegated_inode *);
1783 
1784 /**
1785  * struct renamedata - contains all information required for renaming
1786  * @mnt_idmap:     idmap of the mount in which the rename is happening.
1787  * @old_parent:        parent of source
1788  * @old_dentry:                source
1789  * @new_parent:        parent of destination
1790  * @new_dentry:                destination
1791  * @delegated_inode:   returns an inode needing a delegation break
1792  * @flags:             rename flags
1793  */
1794 struct renamedata {
1795 	struct mnt_idmap *mnt_idmap;
1796 	struct dentry *old_parent;
1797 	struct dentry *old_dentry;
1798 	struct dentry *new_parent;
1799 	struct dentry *new_dentry;
1800 	struct delegated_inode *delegated_inode;
1801 	unsigned int flags;
1802 } __randomize_layout;
1803 
1804 int vfs_rename(struct renamedata *);
1805 
vfs_whiteout(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry)1806 static inline int vfs_whiteout(struct mnt_idmap *idmap,
1807 			       struct inode *dir, struct dentry *dentry)
1808 {
1809 	return vfs_mknod(idmap, dir, dentry, S_IFCHR | WHITEOUT_MODE,
1810 			 WHITEOUT_DEV, NULL);
1811 }
1812 
1813 struct file *kernel_tmpfile_open(struct mnt_idmap *idmap,
1814 				 const struct path *parentpath,
1815 				 umode_t mode, int open_flag,
1816 				 const struct cred *cred);
1817 struct file *kernel_file_open(const struct path *path, int flags,
1818 			      const struct cred *cred);
1819 
1820 int vfs_mkobj(struct dentry *, umode_t,
1821 		int (*f)(struct dentry *, umode_t, void *),
1822 		void *);
1823 
1824 int vfs_fchown(struct file *file, uid_t user, gid_t group);
1825 int vfs_fchmod(struct file *file, umode_t mode);
1826 int vfs_utimes(const struct path *path, struct timespec64 *times);
1827 
1828 #ifdef CONFIG_COMPAT
1829 extern long compat_ptr_ioctl(struct file *file, unsigned int cmd,
1830 					unsigned long arg);
1831 #else
1832 #define compat_ptr_ioctl NULL
1833 #endif
1834 
1835 /*
1836  * VFS file helper functions.
1837  */
1838 void inode_init_owner(struct mnt_idmap *idmap, struct inode *inode,
1839 		      const struct inode *dir, umode_t mode);
1840 extern bool may_open_dev(const struct path *path);
1841 umode_t mode_strip_sgid(struct mnt_idmap *idmap,
1842 			const struct inode *dir, umode_t mode);
1843 bool in_group_or_capable(struct mnt_idmap *idmap,
1844 			 const struct inode *inode, vfsgid_t vfsgid);
1845 
1846 /*
1847  * This is the "filldir" function type, used by readdir() to let
1848  * the kernel specify what kind of dirent layout it wants to have.
1849  * This allows the kernel to read directories into kernel space or
1850  * to have different dirent layouts depending on the binary type.
1851  * Return 'true' to keep going and 'false' if there are no more entries.
1852  */
1853 struct dir_context;
1854 typedef bool (*filldir_t)(struct dir_context *, const char *, int, loff_t, u64,
1855 			 unsigned);
1856 
1857 struct dir_context {
1858 	filldir_t actor;
1859 	loff_t pos;
1860 	/*
1861 	 * Filesystems MUST NOT MODIFY count, but may use as a hint:
1862 	 * 0	    unknown
1863 	 * > 0      space in buffer (assume at least one entry)
1864 	 * INT_MAX  unlimited
1865 	 */
1866 	int count;
1867 	/* @actor supports these flags in d_type high bits */
1868 	unsigned int dt_flags_mask;
1869 };
1870 
1871 /* If OR-ed with d_type, pending signals are not checked */
1872 #define FILLDIR_FLAG_NOINTR	0x1000
1873 
1874 /*
1875  * These flags let !MMU mmap() govern direct device mapping vs immediate
1876  * copying more easily for MAP_PRIVATE, especially for ROM filesystems.
1877  *
1878  * NOMMU_MAP_COPY:	Copy can be mapped (MAP_PRIVATE)
1879  * NOMMU_MAP_DIRECT:	Can be mapped directly (MAP_SHARED)
1880  * NOMMU_MAP_READ:	Can be mapped for reading
1881  * NOMMU_MAP_WRITE:	Can be mapped for writing
1882  * NOMMU_MAP_EXEC:	Can be mapped for execution
1883  */
1884 #define NOMMU_MAP_COPY		0x00000001
1885 #define NOMMU_MAP_DIRECT	0x00000008
1886 #define NOMMU_MAP_READ		VM_MAYREAD
1887 #define NOMMU_MAP_WRITE		VM_MAYWRITE
1888 #define NOMMU_MAP_EXEC		VM_MAYEXEC
1889 
1890 #define NOMMU_VMFLAGS \
1891 	(NOMMU_MAP_READ | NOMMU_MAP_WRITE | NOMMU_MAP_EXEC)
1892 
1893 /*
1894  * These flags control the behavior of the remap_file_range function pointer.
1895  * If it is called with len == 0 that means "remap to end of source file".
1896  * See Documentation/filesystems/vfs.rst for more details about this call.
1897  *
1898  * REMAP_FILE_DEDUP: only remap if contents identical (i.e. deduplicate)
1899  * REMAP_FILE_CAN_SHORTEN: caller can handle a shortened request
1900  */
1901 #define REMAP_FILE_DEDUP		(1 << 0)
1902 #define REMAP_FILE_CAN_SHORTEN		(1 << 1)
1903 
1904 /*
1905  * These flags signal that the caller is ok with altering various aspects of
1906  * the behavior of the remap operation.  The changes must be made by the
1907  * implementation; the vfs remap helper functions can take advantage of them.
1908  * Flags in this category exist to preserve the quirky behavior of the hoisted
1909  * btrfs clone/dedupe ioctls.
1910  */
1911 #define REMAP_FILE_ADVISORY		(REMAP_FILE_CAN_SHORTEN)
1912 
1913 /*
1914  * These flags control the behavior of vfs_copy_file_range().
1915  * They are not available to the user via syscall.
1916  *
1917  * COPY_FILE_SPLICE: call splice direct instead of fs clone/copy ops
1918  */
1919 #define COPY_FILE_SPLICE		(1 << 0)
1920 
1921 struct io_uring_cmd;
1922 struct offset_ctx;
1923 
1924 typedef unsigned int __bitwise fop_flags_t;
1925 
1926 struct file_operations {
1927 	struct module *owner;
1928 	fop_flags_t fop_flags;
1929 	loff_t (*llseek) (struct file *, loff_t, int);
1930 	ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
1931 	ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
1932 	ssize_t (*read_iter) (struct kiocb *, struct iov_iter *);
1933 	ssize_t (*write_iter) (struct kiocb *, struct iov_iter *);
1934 	int (*iopoll)(struct kiocb *kiocb, struct io_comp_batch *,
1935 			unsigned int flags);
1936 	int (*iterate_shared) (struct file *, struct dir_context *);
1937 	__poll_t (*poll) (struct file *, struct poll_table_struct *);
1938 	long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
1939 	long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
1940 	int (*mmap) (struct file *, struct vm_area_struct *);
1941 	int (*open) (struct inode *, struct file *);
1942 	int (*flush) (struct file *, fl_owner_t id);
1943 	int (*release) (struct inode *, struct file *);
1944 	int (*fsync) (struct file *, loff_t, loff_t, int datasync);
1945 	int (*fasync) (int, struct file *, int);
1946 	int (*lock) (struct file *, int, struct file_lock *);
1947 	unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
1948 	int (*check_flags)(int);
1949 	int (*flock) (struct file *, int, struct file_lock *);
1950 	ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
1951 	ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
1952 	void (*splice_eof)(struct file *file);
1953 	int (*setlease)(struct file *, int, struct file_lease **, void **);
1954 	long (*fallocate)(struct file *file, int mode, loff_t offset,
1955 			  loff_t len);
1956 	void (*show_fdinfo)(struct seq_file *m, struct file *f);
1957 #ifndef CONFIG_MMU
1958 	unsigned (*mmap_capabilities)(struct file *);
1959 #endif
1960 	ssize_t (*copy_file_range)(struct file *, loff_t, struct file *,
1961 			loff_t, size_t, unsigned int);
1962 	loff_t (*remap_file_range)(struct file *file_in, loff_t pos_in,
1963 				   struct file *file_out, loff_t pos_out,
1964 				   loff_t len, unsigned int remap_flags);
1965 	int (*fadvise)(struct file *, loff_t, loff_t, int);
1966 	int (*uring_cmd)(struct io_uring_cmd *ioucmd, unsigned int issue_flags);
1967 	int (*uring_cmd_iopoll)(struct io_uring_cmd *, struct io_comp_batch *,
1968 				unsigned int poll_flags);
1969 	int (*mmap_prepare)(struct vm_area_desc *);
1970 } __randomize_layout;
1971 
1972 /* Supports async buffered reads */
1973 #define FOP_BUFFER_RASYNC	((__force fop_flags_t)(1 << 0))
1974 /* Supports async buffered writes */
1975 #define FOP_BUFFER_WASYNC	((__force fop_flags_t)(1 << 1))
1976 /* Supports synchronous page faults for mappings */
1977 #define FOP_MMAP_SYNC		((__force fop_flags_t)(1 << 2))
1978 /* Supports non-exclusive O_DIRECT writes from multiple threads */
1979 #define FOP_DIO_PARALLEL_WRITE	((__force fop_flags_t)(1 << 3))
1980 /* Contains huge pages */
1981 #define FOP_HUGE_PAGES		((__force fop_flags_t)(1 << 4))
1982 /* Treat loff_t as unsigned (e.g., /dev/mem) */
1983 #define FOP_UNSIGNED_OFFSET	((__force fop_flags_t)(1 << 5))
1984 /* Supports asynchronous lock callbacks */
1985 #define FOP_ASYNC_LOCK		((__force fop_flags_t)(1 << 6))
1986 /* File system supports uncached read/write buffered IO */
1987 #define FOP_DONTCACHE		((__force fop_flags_t)(1 << 7))
1988 
1989 /* Wrap a directory iterator that needs exclusive inode access */
1990 int wrap_directory_iterator(struct file *, struct dir_context *,
1991 			    int (*) (struct file *, struct dir_context *));
1992 #define WRAP_DIR_ITER(x) \
1993 	static int shared_##x(struct file *file , struct dir_context *ctx) \
1994 	{ return wrap_directory_iterator(file, ctx, x); }
1995 
1996 enum fs_update_time {
1997 	FS_UPD_ATIME,
1998 	FS_UPD_CMTIME,
1999 };
2000 
2001 struct inode_operations {
2002 	struct dentry * (*lookup) (struct inode *,struct dentry *, unsigned int);
2003 	const char * (*get_link) (struct dentry *, struct inode *, struct delayed_call *);
2004 	int (*permission) (struct mnt_idmap *, struct inode *, int);
2005 	struct posix_acl * (*get_inode_acl)(struct inode *, int, bool);
2006 
2007 	int (*readlink) (struct dentry *, char __user *,int);
2008 
2009 	int (*create) (struct mnt_idmap *, struct inode *,struct dentry *,
2010 		       umode_t, bool);
2011 	int (*link) (struct dentry *,struct inode *,struct dentry *);
2012 	int (*unlink) (struct inode *,struct dentry *);
2013 	int (*symlink) (struct mnt_idmap *, struct inode *,struct dentry *,
2014 			const char *);
2015 	struct dentry *(*mkdir) (struct mnt_idmap *, struct inode *,
2016 				 struct dentry *, umode_t);
2017 	int (*rmdir) (struct inode *,struct dentry *);
2018 	int (*mknod) (struct mnt_idmap *, struct inode *,struct dentry *,
2019 		      umode_t,dev_t);
2020 	int (*rename) (struct mnt_idmap *, struct inode *, struct dentry *,
2021 			struct inode *, struct dentry *, unsigned int);
2022 	int (*setattr) (struct mnt_idmap *, struct dentry *, struct iattr *);
2023 	int (*getattr) (struct mnt_idmap *, const struct path *,
2024 			struct kstat *, u32, unsigned int);
2025 	ssize_t (*listxattr) (struct dentry *, char *, size_t);
2026 	int (*fiemap)(struct inode *, struct fiemap_extent_info *, u64 start,
2027 		      u64 len);
2028 	int (*update_time)(struct inode *inode, enum fs_update_time type,
2029 			   unsigned int flags);
2030 	void (*sync_lazytime)(struct inode *inode);
2031 	int (*atomic_open)(struct inode *, struct dentry *,
2032 			   struct file *, unsigned open_flag,
2033 			   umode_t create_mode);
2034 	int (*tmpfile) (struct mnt_idmap *, struct inode *,
2035 			struct file *, umode_t);
2036 	struct posix_acl *(*get_acl)(struct mnt_idmap *, struct dentry *,
2037 				     int);
2038 	int (*set_acl)(struct mnt_idmap *, struct dentry *,
2039 		       struct posix_acl *, int);
2040 	int (*fileattr_set)(struct mnt_idmap *idmap,
2041 			    struct dentry *dentry, struct file_kattr *fa);
2042 	int (*fileattr_get)(struct dentry *dentry, struct file_kattr *fa);
2043 	struct offset_ctx *(*get_offset_ctx)(struct inode *inode);
2044 } ____cacheline_aligned;
2045 
2046 /* Did the driver provide valid mmap hook configuration? */
can_mmap_file(struct file * file)2047 static inline bool can_mmap_file(struct file *file)
2048 {
2049 	bool has_mmap = file->f_op->mmap;
2050 	bool has_mmap_prepare = file->f_op->mmap_prepare;
2051 
2052 	/* Hooks are mutually exclusive. */
2053 	if (WARN_ON_ONCE(has_mmap && has_mmap_prepare))
2054 		return false;
2055 	if (!has_mmap && !has_mmap_prepare)
2056 		return false;
2057 
2058 	return true;
2059 }
2060 
2061 void compat_set_desc_from_vma(struct vm_area_desc *desc, const struct file *file,
2062 			      const struct vm_area_struct *vma);
2063 int __compat_vma_mmap(struct vm_area_desc *desc, struct vm_area_struct *vma);
2064 int compat_vma_mmap(struct file *file, struct vm_area_struct *vma);
2065 int __vma_check_mmap_hook(struct vm_area_struct *vma);
2066 
vfs_mmap(struct file * file,struct vm_area_struct * vma)2067 static inline int vfs_mmap(struct file *file, struct vm_area_struct *vma)
2068 {
2069 	int err;
2070 
2071 	if (file->f_op->mmap_prepare)
2072 		return compat_vma_mmap(file, vma);
2073 
2074 	err = file->f_op->mmap(file, vma);
2075 	if (err)
2076 		return err;
2077 
2078 	return __vma_check_mmap_hook(vma);
2079 }
2080 
vfs_mmap_prepare(struct file * file,struct vm_area_desc * desc)2081 static inline int vfs_mmap_prepare(struct file *file, struct vm_area_desc *desc)
2082 {
2083 	return file->f_op->mmap_prepare(desc);
2084 }
2085 
2086 extern ssize_t vfs_read(struct file *, char __user *, size_t, loff_t *);
2087 extern ssize_t vfs_write(struct file *, const char __user *, size_t, loff_t *);
2088 extern ssize_t vfs_copy_file_range(struct file *, loff_t , struct file *,
2089 				   loff_t, size_t, unsigned int);
2090 int remap_verify_area(struct file *file, loff_t pos, loff_t len, bool write);
2091 int __generic_remap_file_range_prep(struct file *file_in, loff_t pos_in,
2092 				    struct file *file_out, loff_t pos_out,
2093 				    loff_t *len, unsigned int remap_flags,
2094 				    const struct iomap_ops *dax_read_ops);
2095 int generic_remap_file_range_prep(struct file *file_in, loff_t pos_in,
2096 				  struct file *file_out, loff_t pos_out,
2097 				  loff_t *count, unsigned int remap_flags);
2098 extern loff_t vfs_clone_file_range(struct file *file_in, loff_t pos_in,
2099 				   struct file *file_out, loff_t pos_out,
2100 				   loff_t len, unsigned int remap_flags);
2101 extern int vfs_dedupe_file_range(struct file *file,
2102 				 struct file_dedupe_range *same);
2103 extern loff_t vfs_dedupe_file_range_one(struct file *src_file, loff_t src_pos,
2104 					struct file *dst_file, loff_t dst_pos,
2105 					loff_t len, unsigned int remap_flags);
2106 
2107 /*
2108  * Inode flags - they have no relation to superblock flags now
2109  */
2110 #define S_SYNC		(1 << 0)  /* Writes are synced at once */
2111 #define S_NOATIME	(1 << 1)  /* Do not update access times */
2112 #define S_APPEND	(1 << 2)  /* Append-only file */
2113 #define S_IMMUTABLE	(1 << 3)  /* Immutable file */
2114 #define S_DEAD		(1 << 4)  /* removed, but still open directory */
2115 #define S_NOQUOTA	(1 << 5)  /* Inode is not counted to quota */
2116 #define S_DIRSYNC	(1 << 6)  /* Directory modifications are synchronous */
2117 #define S_NOCMTIME	(1 << 7)  /* Do not update file c/mtime */
2118 #define S_SWAPFILE	(1 << 8)  /* Do not truncate: swapon got its bmaps */
2119 #define S_PRIVATE	(1 << 9)  /* Inode is fs-internal */
2120 #define S_IMA		(1 << 10) /* Inode has an associated IMA struct */
2121 #define S_AUTOMOUNT	(1 << 11) /* Automount/referral quasi-directory */
2122 #define S_NOSEC		(1 << 12) /* no suid or xattr security attributes */
2123 #ifdef CONFIG_FS_DAX
2124 #define S_DAX		(1 << 13) /* Direct Access, avoiding the page cache */
2125 #else
2126 #define S_DAX		0	  /* Make all the DAX code disappear */
2127 #endif
2128 #define S_ENCRYPTED	(1 << 14) /* Encrypted file (using fs/crypto/) */
2129 #define S_CASEFOLD	(1 << 15) /* Casefolded file */
2130 #define S_VERITY	(1 << 16) /* Verity file (using fs/verity/) */
2131 #define S_KERNEL_FILE	(1 << 17) /* File is in use by the kernel (eg. fs/cachefiles) */
2132 #define S_ANON_INODE	(1 << 19) /* Inode is an anonymous inode */
2133 
2134 /*
2135  * Note that nosuid etc flags are inode-specific: setting some file-system
2136  * flags just means all the inodes inherit those flags by default. It might be
2137  * possible to override it selectively if you really wanted to with some
2138  * ioctl() that is not currently implemented.
2139  *
2140  * Exception: SB_RDONLY is always applied to the entire file system.
2141  *
2142  * Unfortunately, it is possible to change a filesystems flags with it mounted
2143  * with files in use.  This means that all of the inodes will not have their
2144  * i_flags updated.  Hence, i_flags no longer inherit the superblock mount
2145  * flags, so these have to be checked separately. -- rmk@arm.uk.linux.org
2146  */
2147 #define __IS_FLG(inode, flg)	((inode)->i_sb->s_flags & (flg))
2148 
2149 #define IS_RDONLY(inode)	sb_rdonly((inode)->i_sb)
2150 #define IS_SYNC(inode)		(__IS_FLG(inode, SB_SYNCHRONOUS) || \
2151 					((inode)->i_flags & S_SYNC))
2152 #define IS_DIRSYNC(inode)	(__IS_FLG(inode, SB_SYNCHRONOUS|SB_DIRSYNC) || \
2153 					((inode)->i_flags & (S_SYNC|S_DIRSYNC)))
2154 #define IS_MANDLOCK(inode)	__IS_FLG(inode, SB_MANDLOCK)
2155 #define IS_NOATIME(inode)	__IS_FLG(inode, SB_RDONLY|SB_NOATIME)
2156 #define IS_I_VERSION(inode)	__IS_FLG(inode, SB_I_VERSION)
2157 
2158 #define IS_NOQUOTA(inode)	((inode)->i_flags & S_NOQUOTA)
2159 #define IS_APPEND(inode)	((inode)->i_flags & S_APPEND)
2160 #define IS_IMMUTABLE(inode)	((inode)->i_flags & S_IMMUTABLE)
2161 
2162 #ifdef CONFIG_FS_POSIX_ACL
2163 #define IS_POSIXACL(inode)	__IS_FLG(inode, SB_POSIXACL)
2164 #else
2165 #define IS_POSIXACL(inode)	0
2166 #endif
2167 
2168 #define IS_DEADDIR(inode)	((inode)->i_flags & S_DEAD)
2169 #define IS_NOCMTIME(inode)	((inode)->i_flags & S_NOCMTIME)
2170 
2171 #ifdef CONFIG_SWAP
2172 #define IS_SWAPFILE(inode)	((inode)->i_flags & S_SWAPFILE)
2173 #else
2174 #define IS_SWAPFILE(inode)	((void)(inode), 0U)
2175 #endif
2176 
2177 #define IS_PRIVATE(inode)	((inode)->i_flags & S_PRIVATE)
2178 #define IS_IMA(inode)		((inode)->i_flags & S_IMA)
2179 #define IS_AUTOMOUNT(inode)	((inode)->i_flags & S_AUTOMOUNT)
2180 #define IS_NOSEC(inode)		((inode)->i_flags & S_NOSEC)
2181 #define IS_DAX(inode)		((inode)->i_flags & S_DAX)
2182 #define IS_ENCRYPTED(inode)	((inode)->i_flags & S_ENCRYPTED)
2183 #define IS_CASEFOLDED(inode)	((inode)->i_flags & S_CASEFOLD)
2184 #define IS_VERITY(inode)	((inode)->i_flags & S_VERITY)
2185 
2186 #define IS_WHITEOUT(inode)	(S_ISCHR(inode->i_mode) && \
2187 				 (inode)->i_rdev == WHITEOUT_DEV)
2188 #define IS_ANON_FILE(inode)	((inode)->i_flags & S_ANON_INODE)
2189 
HAS_UNMAPPED_ID(struct mnt_idmap * idmap,struct inode * inode)2190 static inline bool HAS_UNMAPPED_ID(struct mnt_idmap *idmap,
2191 				   struct inode *inode)
2192 {
2193 	return !vfsuid_valid(i_uid_into_vfsuid(idmap, inode)) ||
2194 	       !vfsgid_valid(i_gid_into_vfsgid(idmap, inode));
2195 }
2196 
init_sync_kiocb(struct kiocb * kiocb,struct file * filp)2197 static inline void init_sync_kiocb(struct kiocb *kiocb, struct file *filp)
2198 {
2199 	*kiocb = (struct kiocb) {
2200 		.ki_filp = filp,
2201 		.ki_flags = filp->f_iocb_flags,
2202 		.ki_ioprio = get_current_ioprio(),
2203 	};
2204 }
2205 
kiocb_clone(struct kiocb * kiocb,struct kiocb * kiocb_src,struct file * filp)2206 static inline void kiocb_clone(struct kiocb *kiocb, struct kiocb *kiocb_src,
2207 			       struct file *filp)
2208 {
2209 	*kiocb = (struct kiocb) {
2210 		.ki_filp = filp,
2211 		.ki_flags = kiocb_src->ki_flags,
2212 		.ki_ioprio = kiocb_src->ki_ioprio,
2213 		.ki_pos = kiocb_src->ki_pos,
2214 	};
2215 }
2216 
2217 extern void __mark_inode_dirty(struct inode *, int);
mark_inode_dirty(struct inode * inode)2218 static inline void mark_inode_dirty(struct inode *inode)
2219 {
2220 	__mark_inode_dirty(inode, I_DIRTY);
2221 }
2222 
mark_inode_dirty_sync(struct inode * inode)2223 static inline void mark_inode_dirty_sync(struct inode *inode)
2224 {
2225 	__mark_inode_dirty(inode, I_DIRTY_SYNC);
2226 }
2227 
icount_read(const struct inode * inode)2228 static inline int icount_read(const struct inode *inode)
2229 {
2230 	return atomic_read(&inode->i_count);
2231 }
2232 
2233 /*
2234  * Returns true if the given inode itself only has dirty timestamps (its pages
2235  * may still be dirty) and isn't currently being allocated or freed.
2236  * Filesystems should call this if when writing an inode when lazytime is
2237  * enabled, they want to opportunistically write the timestamps of other inodes
2238  * located very nearby on-disk, e.g. in the same inode block.  This returns true
2239  * if the given inode is in need of such an opportunistic update.  Requires
2240  * i_lock, or at least later re-checking under i_lock.
2241  */
inode_is_dirtytime_only(struct inode * inode)2242 static inline bool inode_is_dirtytime_only(struct inode *inode)
2243 {
2244 	return (inode_state_read_once(inode) &
2245 	       (I_DIRTY_TIME | I_NEW | I_FREEING | I_WILL_FREE)) == I_DIRTY_TIME;
2246 }
2247 
2248 extern void inc_nlink(struct inode *inode);
2249 extern void drop_nlink(struct inode *inode);
2250 extern void clear_nlink(struct inode *inode);
2251 extern void set_nlink(struct inode *inode, unsigned int nlink);
2252 
inode_inc_link_count(struct inode * inode)2253 static inline void inode_inc_link_count(struct inode *inode)
2254 {
2255 	inc_nlink(inode);
2256 	mark_inode_dirty(inode);
2257 }
2258 
inode_dec_link_count(struct inode * inode)2259 static inline void inode_dec_link_count(struct inode *inode)
2260 {
2261 	drop_nlink(inode);
2262 	mark_inode_dirty(inode);
2263 }
2264 
2265 extern bool atime_needs_update(const struct path *, struct inode *);
2266 extern void touch_atime(const struct path *);
2267 
file_accessed(struct file * file)2268 static inline void file_accessed(struct file *file)
2269 {
2270 	if (!(file->f_flags & O_NOATIME))
2271 		touch_atime(&file->f_path);
2272 }
2273 
2274 extern int file_modified(struct file *file);
2275 int kiocb_modified(struct kiocb *iocb);
2276 
2277 int sync_inode_metadata(struct inode *inode, int wait);
2278 
2279 struct file_system_type {
2280 	const char *name;
2281 	int fs_flags;
2282 #define FS_REQUIRES_DEV		1
2283 #define FS_BINARY_MOUNTDATA	2
2284 #define FS_HAS_SUBTYPE		4
2285 #define FS_USERNS_MOUNT		8	/* Can be mounted by userns root */
2286 #define FS_DISALLOW_NOTIFY_PERM	16	/* Disable fanotify permission events */
2287 #define FS_ALLOW_IDMAP         32      /* FS has been updated to handle vfs idmappings. */
2288 #define FS_MGTIME		64	/* FS uses multigrain timestamps */
2289 #define FS_LBS			128	/* FS supports LBS */
2290 #define FS_POWER_FREEZE		256	/* Always freeze on suspend/hibernate */
2291 #define FS_RENAME_DOES_D_MOVE	32768	/* FS will handle d_move() during rename() internally. */
2292 	int (*init_fs_context)(struct fs_context *);
2293 	const struct fs_parameter_spec *parameters;
2294 	void (*kill_sb) (struct super_block *);
2295 	struct module *owner;
2296 	struct file_system_type * next;
2297 	struct hlist_head fs_supers;
2298 
2299 	struct lock_class_key s_lock_key;
2300 	struct lock_class_key s_umount_key;
2301 	struct lock_class_key s_vfs_rename_key;
2302 	struct lock_class_key s_writers_key[SB_FREEZE_LEVELS];
2303 
2304 	struct lock_class_key i_lock_key;
2305 	struct lock_class_key i_mutex_key;
2306 	struct lock_class_key invalidate_lock_key;
2307 	struct lock_class_key i_mutex_dir_key;
2308 };
2309 
2310 #define MODULE_ALIAS_FS(NAME) MODULE_ALIAS("fs-" NAME)
2311 
2312 /**
2313  * is_mgtime: is this inode using multigrain timestamps
2314  * @inode: inode to test for multigrain timestamps
2315  *
2316  * Return true if the inode uses multigrain timestamps, false otherwise.
2317  */
is_mgtime(const struct inode * inode)2318 static inline bool is_mgtime(const struct inode *inode)
2319 {
2320 	return inode->i_opflags & IOP_MGTIME;
2321 }
2322 
2323 extern struct dentry *mount_subtree(struct vfsmount *mnt, const char *path);
2324 void retire_super(struct super_block *sb);
2325 void generic_shutdown_super(struct super_block *sb);
2326 void kill_block_super(struct super_block *sb);
2327 void kill_anon_super(struct super_block *sb);
2328 void deactivate_super(struct super_block *sb);
2329 void deactivate_locked_super(struct super_block *sb);
2330 int set_anon_super(struct super_block *s, void *data);
2331 int set_anon_super_fc(struct super_block *s, struct fs_context *fc);
2332 int get_anon_bdev(dev_t *);
2333 void free_anon_bdev(dev_t);
2334 struct super_block *sget_fc(struct fs_context *fc,
2335 			    int (*test)(struct super_block *, struct fs_context *),
2336 			    int (*set)(struct super_block *, struct fs_context *));
2337 struct super_block *sget(struct file_system_type *type,
2338 			int (*test)(struct super_block *,void *),
2339 			int (*set)(struct super_block *,void *),
2340 			int flags, void *data);
2341 struct super_block *sget_dev(struct fs_context *fc, dev_t dev);
2342 
2343 /* Alas, no aliases. Too much hassle with bringing module.h everywhere */
2344 #define fops_get(fops) ({						\
2345 	const struct file_operations *_fops = (fops);			\
2346 	(((_fops) && try_module_get((_fops)->owner) ? (_fops) : NULL));	\
2347 })
2348 
2349 #define fops_put(fops) ({						\
2350 	const struct file_operations *_fops = (fops);			\
2351 	if (_fops)							\
2352 		module_put((_fops)->owner);				\
2353 })
2354 
2355 /*
2356  * This one is to be used *ONLY* from ->open() instances.
2357  * fops must be non-NULL, pinned down *and* module dependencies
2358  * should be sufficient to pin the caller down as well.
2359  */
2360 #define replace_fops(f, fops) \
2361 	do {	\
2362 		struct file *__file = (f); \
2363 		fops_put(__file->f_op); \
2364 		BUG_ON(!(__file->f_op = (fops))); \
2365 	} while(0)
2366 
2367 extern int register_filesystem(struct file_system_type *);
2368 extern int unregister_filesystem(struct file_system_type *);
2369 extern int vfs_statfs(const struct path *, struct kstatfs *);
2370 extern int user_statfs(const char __user *, struct kstatfs *);
2371 extern int fd_statfs(int, struct kstatfs *);
2372 extern __printf(2, 3)
2373 int super_setup_bdi_name(struct super_block *sb, char *fmt, ...);
2374 extern int super_setup_bdi(struct super_block *sb);
2375 
super_set_uuid(struct super_block * sb,const u8 * uuid,unsigned len)2376 static inline void super_set_uuid(struct super_block *sb, const u8 *uuid, unsigned len)
2377 {
2378 	if (WARN_ON(len > sizeof(sb->s_uuid)))
2379 		len = sizeof(sb->s_uuid);
2380 	sb->s_uuid_len = len;
2381 	memcpy(&sb->s_uuid, uuid, len);
2382 }
2383 
2384 /* set sb sysfs name based on sb->s_bdev */
super_set_sysfs_name_bdev(struct super_block * sb)2385 static inline void super_set_sysfs_name_bdev(struct super_block *sb)
2386 {
2387 	snprintf(sb->s_sysfs_name, sizeof(sb->s_sysfs_name), "%pg", sb->s_bdev);
2388 }
2389 
2390 /* set sb sysfs name based on sb->s_uuid */
super_set_sysfs_name_uuid(struct super_block * sb)2391 static inline void super_set_sysfs_name_uuid(struct super_block *sb)
2392 {
2393 	WARN_ON(sb->s_uuid_len != sizeof(sb->s_uuid));
2394 	snprintf(sb->s_sysfs_name, sizeof(sb->s_sysfs_name), "%pU", sb->s_uuid.b);
2395 }
2396 
2397 /* set sb sysfs name based on sb->s_id */
super_set_sysfs_name_id(struct super_block * sb)2398 static inline void super_set_sysfs_name_id(struct super_block *sb)
2399 {
2400 	strscpy(sb->s_sysfs_name, sb->s_id, sizeof(sb->s_sysfs_name));
2401 }
2402 
2403 /* try to use something standard before you use this */
2404 __printf(2, 3)
super_set_sysfs_name_generic(struct super_block * sb,const char * fmt,...)2405 static inline void super_set_sysfs_name_generic(struct super_block *sb, const char *fmt, ...)
2406 {
2407 	va_list args;
2408 
2409 	va_start(args, fmt);
2410 	vsnprintf(sb->s_sysfs_name, sizeof(sb->s_sysfs_name), fmt, args);
2411 	va_end(args);
2412 }
2413 
2414 extern void ihold(struct inode * inode);
2415 extern void iput(struct inode *);
2416 void iput_not_last(struct inode *);
2417 int inode_update_time(struct inode *inode, enum fs_update_time type,
2418 		unsigned int flags);
2419 int generic_update_time(struct inode *inode, enum fs_update_time type,
2420 		unsigned int flags);
2421 
2422 /* /sys/fs */
2423 extern struct kobject *fs_kobj;
2424 
2425 #define MAX_RW_COUNT (INT_MAX & PAGE_MASK)
2426 
2427 /* fs/open.c */
2428 struct audit_names;
2429 
2430 struct __filename_head {
2431 	const char		*name;	/* pointer to actual string */
2432 	int			refcnt;
2433 	struct audit_names	*aname;
2434 };
2435 #define EMBEDDED_NAME_MAX	(192 - sizeof(struct __filename_head))
2436 struct filename {
2437 	struct __filename_head;
2438 	const char		iname[EMBEDDED_NAME_MAX];
2439 };
2440 static_assert(offsetof(struct filename, iname) % sizeof(long) == 0);
2441 static_assert(sizeof(struct filename) % 64 == 0);
2442 
file_mnt_idmap(const struct file * file)2443 static inline struct mnt_idmap *file_mnt_idmap(const struct file *file)
2444 {
2445 	return mnt_idmap(file->f_path.mnt);
2446 }
2447 
2448 /**
2449  * is_idmapped_mnt - check whether a mount is mapped
2450  * @mnt: the mount to check
2451  *
2452  * If @mnt has an non @nop_mnt_idmap attached to it then @mnt is mapped.
2453  *
2454  * Return: true if mount is mapped, false if not.
2455  */
is_idmapped_mnt(const struct vfsmount * mnt)2456 static inline bool is_idmapped_mnt(const struct vfsmount *mnt)
2457 {
2458 	return mnt_idmap(mnt) != &nop_mnt_idmap;
2459 }
2460 
2461 int vfs_truncate(const struct path *, loff_t);
2462 int do_truncate(struct mnt_idmap *, struct dentry *, loff_t start,
2463 		unsigned int time_attrs, struct file *filp);
2464 extern int vfs_fallocate(struct file *file, int mode, loff_t offset,
2465 			loff_t len);
2466 int do_sys_open(int dfd, const char __user *filename, int flags,
2467 		umode_t mode);
2468 extern struct file *file_open_name(struct filename *, int, umode_t);
2469 extern struct file *filp_open(const char *, int, umode_t);
2470 extern struct file *file_open_root(const struct path *,
2471 				   const char *, int, umode_t);
file_open_root_mnt(struct vfsmount * mnt,const char * name,int flags,umode_t mode)2472 static inline struct file *file_open_root_mnt(struct vfsmount *mnt,
2473 				   const char *name, int flags, umode_t mode)
2474 {
2475 	return file_open_root(&(struct path){.mnt = mnt, .dentry = mnt->mnt_root},
2476 			      name, flags, mode);
2477 }
2478 struct file *dentry_open(const struct path *path, int flags,
2479 			 const struct cred *creds);
2480 struct file *dentry_open_nonotify(const struct path *path, int flags,
2481 				  const struct cred *cred);
2482 struct file *dentry_create(struct path *path, int flags, umode_t mode,
2483 			   const struct cred *cred);
2484 const struct path *backing_file_user_path(const struct file *f);
2485 
2486 #ifdef CONFIG_SECURITY
2487 void *backing_file_security(const struct file *f);
2488 void backing_file_set_security(struct file *f, void *security);
2489 #else
backing_file_security(const struct file * f)2490 static inline void *backing_file_security(const struct file *f)
2491 {
2492 	return NULL;
2493 }
backing_file_set_security(struct file * f,void * security)2494 static inline void backing_file_set_security(struct file *f, void *security)
2495 {
2496 }
2497 #endif /* CONFIG_SECURITY */
2498 
2499 /*
2500  * When mmapping a file on a stackable filesystem (e.g., overlayfs), the file
2501  * stored in ->vm_file is a backing file whose f_inode is on the underlying
2502  * filesystem.  When the mapped file path and inode number are displayed to
2503  * user (e.g. via /proc/<pid>/maps), these helpers should be used to get the
2504  * path and inode number to display to the user, which is the path of the fd
2505  * that user has requested to map and the inode number that would be returned
2506  * by fstat() on that same fd.
2507  */
2508 /* Get the path to display in /proc/<pid>/maps */
file_user_path(const struct file * f)2509 static inline const struct path *file_user_path(const struct file *f)
2510 {
2511 	if (unlikely(f->f_mode & FMODE_BACKING))
2512 		return backing_file_user_path(f);
2513 	return &f->f_path;
2514 }
2515 /* Get the inode whose inode number to display in /proc/<pid>/maps */
file_user_inode(const struct file * f)2516 static inline const struct inode *file_user_inode(const struct file *f)
2517 {
2518 	if (unlikely(f->f_mode & FMODE_BACKING))
2519 		return d_inode(backing_file_user_path(f)->dentry);
2520 	return file_inode(f);
2521 }
2522 
file_clone_open(struct file * file)2523 static inline struct file *file_clone_open(struct file *file)
2524 {
2525 	return dentry_open(&file->f_path, file->f_flags, file->f_cred);
2526 }
2527 extern int filp_close(struct file *, fl_owner_t id);
2528 
2529 extern struct filename *getname_flags(const char __user *, int);
2530 extern struct filename *getname_uflags(const char __user *, int);
getname(const char __user * name)2531 static inline struct filename *getname(const char __user *name)
2532 {
2533 	return getname_flags(name, 0);
2534 }
2535 extern struct filename *getname_kernel(const char *);
2536 extern struct filename *__getname_maybe_null(const char __user *);
getname_maybe_null(const char __user * name,int flags)2537 static inline struct filename *getname_maybe_null(const char __user *name, int flags)
2538 {
2539 	if (!(flags & AT_EMPTY_PATH))
2540 		return getname(name);
2541 
2542 	if (!name)
2543 		return NULL;
2544 	return __getname_maybe_null(name);
2545 }
2546 extern void putname(struct filename *name);
2547 DEFINE_FREE(putname, struct filename *, if (!IS_ERR_OR_NULL(_T)) putname(_T))
2548 
2549 struct delayed_filename {
2550 	struct filename *__incomplete_filename;	// don't touch
2551 };
2552 #define INIT_DELAYED_FILENAME(ptr) \
2553 	((void)(*(ptr) = (struct delayed_filename){}))
2554 int delayed_getname(struct delayed_filename *, const char __user *);
2555 int delayed_getname_uflags(struct delayed_filename *v, const char __user *, int);
2556 void dismiss_delayed_filename(struct delayed_filename *);
2557 int putname_to_delayed(struct delayed_filename *, struct filename *);
2558 struct filename *complete_getname(struct delayed_filename *);
2559 
2560 DEFINE_CLASS(filename, struct filename *, putname(_T), getname(p), const char __user *p)
2561 EXTEND_CLASS(filename, _kernel, getname_kernel(p), const char *p)
2562 EXTEND_CLASS(filename, _flags, getname_flags(p, f), const char __user *p, unsigned int f)
2563 EXTEND_CLASS(filename, _uflags, getname_uflags(p, f), const char __user *p, unsigned int f)
2564 EXTEND_CLASS(filename, _maybe_null, getname_maybe_null(p, f), const char __user *p, unsigned int f)
2565 EXTEND_CLASS(filename, _complete_delayed, complete_getname(p), struct delayed_filename *p)
2566 
2567 extern int finish_open(struct file *file, struct dentry *dentry,
2568 			int (*open)(struct inode *, struct file *));
2569 extern int finish_no_open(struct file *file, struct dentry *dentry);
2570 
2571 /* Helper for the simple case when original dentry is used */
finish_open_simple(struct file * file,int error)2572 static inline int finish_open_simple(struct file *file, int error)
2573 {
2574 	if (error)
2575 		return error;
2576 
2577 	return finish_open(file, file->f_path.dentry, NULL);
2578 }
2579 
2580 /* fs/dcache.c */
2581 extern void __init vfs_caches_init_early(void);
2582 extern void __init vfs_caches_init(void);
2583 
2584 #define __getname()		kmalloc(PATH_MAX, GFP_KERNEL)
2585 #define __putname(name)		kfree(name)
2586 
2587 void emergency_thaw_all(void);
2588 extern int sync_filesystem(struct super_block *);
2589 extern const struct file_operations def_blk_fops;
2590 extern const struct file_operations def_chr_fops;
2591 
2592 /* fs/char_dev.c */
2593 #define CHRDEV_MAJOR_MAX 512
2594 /* Marks the bottom of the first segment of free char majors */
2595 #define CHRDEV_MAJOR_DYN_END 234
2596 /* Marks the top and bottom of the second segment of free char majors */
2597 #define CHRDEV_MAJOR_DYN_EXT_START 511
2598 #define CHRDEV_MAJOR_DYN_EXT_END 384
2599 
2600 extern int alloc_chrdev_region(dev_t *, unsigned, unsigned, const char *);
2601 extern int register_chrdev_region(dev_t, unsigned, const char *);
2602 extern int __register_chrdev(unsigned int major, unsigned int baseminor,
2603 			     unsigned int count, const char *name,
2604 			     const struct file_operations *fops);
2605 extern void __unregister_chrdev(unsigned int major, unsigned int baseminor,
2606 				unsigned int count, const char *name);
2607 extern void unregister_chrdev_region(dev_t, unsigned);
2608 extern void chrdev_show(struct seq_file *,off_t);
2609 
register_chrdev(unsigned int major,const char * name,const struct file_operations * fops)2610 static inline int register_chrdev(unsigned int major, const char *name,
2611 				  const struct file_operations *fops)
2612 {
2613 	return __register_chrdev(major, 0, 256, name, fops);
2614 }
2615 
unregister_chrdev(unsigned int major,const char * name)2616 static inline void unregister_chrdev(unsigned int major, const char *name)
2617 {
2618 	__unregister_chrdev(major, 0, 256, name);
2619 }
2620 
2621 extern void init_special_inode(struct inode *, umode_t, dev_t);
2622 
2623 /* Invalid inode operations -- fs/bad_inode.c */
2624 extern void make_bad_inode(struct inode *);
2625 extern bool is_bad_inode(struct inode *);
2626 
2627 extern int __must_check file_fdatawait_range(struct file *file, loff_t lstart,
2628 						loff_t lend);
2629 extern int __must_check file_check_and_advance_wb_err(struct file *file);
2630 extern int __must_check file_write_and_wait_range(struct file *file,
2631 						loff_t start, loff_t end);
2632 int filemap_flush_range(struct address_space *mapping, loff_t start,
2633 		loff_t end);
2634 
file_write_and_wait(struct file * file)2635 static inline int file_write_and_wait(struct file *file)
2636 {
2637 	return file_write_and_wait_range(file, 0, LLONG_MAX);
2638 }
2639 
2640 extern int vfs_fsync_range(struct file *file, loff_t start, loff_t end,
2641 			   int datasync);
2642 extern int vfs_fsync(struct file *file, int datasync);
2643 
2644 extern int sync_file_range(struct file *file, loff_t offset, loff_t nbytes,
2645 				unsigned int flags);
2646 
iocb_is_dsync(const struct kiocb * iocb)2647 static inline bool iocb_is_dsync(const struct kiocb *iocb)
2648 {
2649 	return (iocb->ki_flags & IOCB_DSYNC) ||
2650 		IS_SYNC(iocb->ki_filp->f_mapping->host);
2651 }
2652 
2653 /*
2654  * Sync the bytes written if this was a synchronous write.  Expect ki_pos
2655  * to already be updated for the write, and will return either the amount
2656  * of bytes passed in, or an error if syncing the file failed.
2657  */
generic_write_sync(struct kiocb * iocb,ssize_t count)2658 static inline ssize_t generic_write_sync(struct kiocb *iocb, ssize_t count)
2659 {
2660 	if (iocb_is_dsync(iocb)) {
2661 		int ret = vfs_fsync_range(iocb->ki_filp,
2662 				iocb->ki_pos - count, iocb->ki_pos - 1,
2663 				(iocb->ki_flags & IOCB_SYNC) ? 0 : 1);
2664 		if (ret)
2665 			return ret;
2666 	} else if (iocb->ki_flags & IOCB_DONTCACHE) {
2667 		struct address_space *mapping = iocb->ki_filp->f_mapping;
2668 
2669 		filemap_flush_range(mapping, iocb->ki_pos - count,
2670 				iocb->ki_pos - 1);
2671 	}
2672 
2673 	return count;
2674 }
2675 
2676 extern void emergency_sync(void);
2677 extern void emergency_remount(void);
2678 
2679 #ifdef CONFIG_BLOCK
2680 extern int bmap(struct inode *inode, sector_t *block);
2681 #else
bmap(struct inode * inode,sector_t * block)2682 static inline int bmap(struct inode *inode,  sector_t *block)
2683 {
2684 	return -EINVAL;
2685 }
2686 #endif
2687 
2688 int notify_change(struct mnt_idmap *, struct dentry *,
2689 		  struct iattr *, struct delegated_inode *);
2690 int inode_permission(struct mnt_idmap *, struct inode *, int);
2691 int generic_permission(struct mnt_idmap *, struct inode *, int);
file_permission(struct file * file,int mask)2692 static inline int file_permission(struct file *file, int mask)
2693 {
2694 	return inode_permission(file_mnt_idmap(file),
2695 				file_inode(file), mask);
2696 }
path_permission(const struct path * path,int mask)2697 static inline int path_permission(const struct path *path, int mask)
2698 {
2699 	return inode_permission(mnt_idmap(path->mnt),
2700 				d_inode(path->dentry), mask);
2701 }
2702 int __check_sticky(struct mnt_idmap *idmap, struct inode *dir,
2703 		   struct inode *inode);
2704 
2705 int may_delete_dentry(struct mnt_idmap *idmap, struct inode *dir,
2706 		      struct dentry *victim, bool isdir);
2707 int may_create_dentry(struct mnt_idmap *idmap,
2708 		      struct inode *dir, struct dentry *child);
2709 
execute_ok(struct inode * inode)2710 static inline bool execute_ok(struct inode *inode)
2711 {
2712 	return (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode);
2713 }
2714 
inode_wrong_type(const struct inode * inode,umode_t mode)2715 static inline bool inode_wrong_type(const struct inode *inode, umode_t mode)
2716 {
2717 	return (inode->i_mode ^ mode) & S_IFMT;
2718 }
2719 
2720 /**
2721  * file_start_write - get write access to a superblock for regular file io
2722  * @file: the file we want to write to
2723  *
2724  * This is a variant of sb_start_write() which is a noop on non-regular file.
2725  * Should be matched with a call to file_end_write().
2726  */
file_start_write(struct file * file)2727 static inline void file_start_write(struct file *file)
2728 {
2729 	if (!S_ISREG(file_inode(file)->i_mode))
2730 		return;
2731 	sb_start_write(file_inode(file)->i_sb);
2732 }
2733 
file_start_write_trylock(struct file * file)2734 static inline bool file_start_write_trylock(struct file *file)
2735 {
2736 	if (!S_ISREG(file_inode(file)->i_mode))
2737 		return true;
2738 	return sb_start_write_trylock(file_inode(file)->i_sb);
2739 }
2740 
2741 /**
2742  * file_end_write - drop write access to a superblock of a regular file
2743  * @file: the file we wrote to
2744  *
2745  * Should be matched with a call to file_start_write().
2746  */
file_end_write(struct file * file)2747 static inline void file_end_write(struct file *file)
2748 {
2749 	if (!S_ISREG(file_inode(file)->i_mode))
2750 		return;
2751 	sb_end_write(file_inode(file)->i_sb);
2752 }
2753 
2754 /**
2755  * kiocb_start_write - get write access to a superblock for async file io
2756  * @iocb: the io context we want to submit the write with
2757  *
2758  * This is a variant of sb_start_write() for async io submission.
2759  * Should be matched with a call to kiocb_end_write().
2760  */
kiocb_start_write(struct kiocb * iocb)2761 static inline void kiocb_start_write(struct kiocb *iocb)
2762 {
2763 	struct inode *inode = file_inode(iocb->ki_filp);
2764 
2765 	sb_start_write(inode->i_sb);
2766 	/*
2767 	 * Fool lockdep by telling it the lock got released so that it
2768 	 * doesn't complain about the held lock when we return to userspace.
2769 	 */
2770 	__sb_writers_release(inode->i_sb, SB_FREEZE_WRITE);
2771 }
2772 
2773 /**
2774  * kiocb_end_write - drop write access to a superblock after async file io
2775  * @iocb: the io context we sumbitted the write with
2776  *
2777  * Should be matched with a call to kiocb_start_write().
2778  */
kiocb_end_write(struct kiocb * iocb)2779 static inline void kiocb_end_write(struct kiocb *iocb)
2780 {
2781 	struct inode *inode = file_inode(iocb->ki_filp);
2782 
2783 	/*
2784 	 * Tell lockdep we inherited freeze protection from submission thread.
2785 	 */
2786 	__sb_writers_acquired(inode->i_sb, SB_FREEZE_WRITE);
2787 	sb_end_write(inode->i_sb);
2788 }
2789 
2790 /*
2791  * This is used for regular files where some users -- especially the
2792  * currently executed binary in a process, previously handled via
2793  * VM_DENYWRITE -- cannot handle concurrent write (and maybe mmap
2794  * read-write shared) accesses.
2795  *
2796  * get_write_access() gets write permission for a file.
2797  * put_write_access() releases this write permission.
2798  * deny_write_access() denies write access to a file.
2799  * allow_write_access() re-enables write access to a file.
2800  *
2801  * The i_writecount field of an inode can have the following values:
2802  * 0: no write access, no denied write access
2803  * < 0: (-i_writecount) users that denied write access to the file.
2804  * > 0: (i_writecount) users that have write access to the file.
2805  *
2806  * Normally we operate on that counter with atomic_{inc,dec} and it's safe
2807  * except for the cases where we don't hold i_writecount yet. Then we need to
2808  * use {get,deny}_write_access() - these functions check the sign and refuse
2809  * to do the change if sign is wrong.
2810  */
get_write_access(struct inode * inode)2811 static inline int get_write_access(struct inode *inode)
2812 {
2813 	return atomic_inc_unless_negative(&inode->i_writecount) ? 0 : -ETXTBSY;
2814 }
deny_write_access(struct file * file)2815 static inline int deny_write_access(struct file *file)
2816 {
2817 	struct inode *inode = file_inode(file);
2818 	return atomic_dec_unless_positive(&inode->i_writecount) ? 0 : -ETXTBSY;
2819 }
put_write_access(struct inode * inode)2820 static inline void put_write_access(struct inode * inode)
2821 {
2822 	atomic_dec(&inode->i_writecount);
2823 }
allow_write_access(struct file * file)2824 static inline void allow_write_access(struct file *file)
2825 {
2826 	if (file)
2827 		atomic_inc(&file_inode(file)->i_writecount);
2828 }
2829 
2830 /*
2831  * Do not prevent write to executable file when watched by pre-content events.
2832  *
2833  * Note that FMODE_FSNOTIFY_HSM mode is set depending on pre-content watches at
2834  * the time of file open and remains constant for entire lifetime of the file,
2835  * so if pre-content watches are added post execution or removed before the end
2836  * of the execution, it will not cause i_writecount reference leak.
2837  */
exe_file_deny_write_access(struct file * exe_file)2838 static inline int exe_file_deny_write_access(struct file *exe_file)
2839 {
2840 	if (unlikely(FMODE_FSNOTIFY_HSM(exe_file->f_mode)))
2841 		return 0;
2842 	return deny_write_access(exe_file);
2843 }
exe_file_allow_write_access(struct file * exe_file)2844 static inline void exe_file_allow_write_access(struct file *exe_file)
2845 {
2846 	if (unlikely(!exe_file || FMODE_FSNOTIFY_HSM(exe_file->f_mode)))
2847 		return;
2848 	allow_write_access(exe_file);
2849 }
2850 
file_set_fsnotify_mode(struct file * file,fmode_t mode)2851 static inline void file_set_fsnotify_mode(struct file *file, fmode_t mode)
2852 {
2853 	file->f_mode &= ~FMODE_FSNOTIFY_MASK;
2854 	file->f_mode |= mode;
2855 }
2856 
inode_is_open_for_write(const struct inode * inode)2857 static inline bool inode_is_open_for_write(const struct inode *inode)
2858 {
2859 	return atomic_read(&inode->i_writecount) > 0;
2860 }
2861 
2862 #if defined(CONFIG_IMA) || defined(CONFIG_FILE_LOCKING)
i_readcount_dec(struct inode * inode)2863 static inline void i_readcount_dec(struct inode *inode)
2864 {
2865 	BUG_ON(atomic_dec_return(&inode->i_readcount) < 0);
2866 }
i_readcount_inc(struct inode * inode)2867 static inline void i_readcount_inc(struct inode *inode)
2868 {
2869 	atomic_inc(&inode->i_readcount);
2870 }
2871 #else
i_readcount_dec(struct inode * inode)2872 static inline void i_readcount_dec(struct inode *inode)
2873 {
2874 	return;
2875 }
i_readcount_inc(struct inode * inode)2876 static inline void i_readcount_inc(struct inode *inode)
2877 {
2878 	return;
2879 }
2880 #endif
2881 extern int do_pipe_flags(int *, int);
2882 
2883 extern ssize_t kernel_read(struct file *, void *, size_t, loff_t *);
2884 ssize_t __kernel_read(struct file *file, void *buf, size_t count, loff_t *pos);
2885 extern ssize_t kernel_write(struct file *, const void *, size_t, loff_t *);
2886 extern ssize_t __kernel_write(struct file *, const void *, size_t, loff_t *);
2887 extern struct file * open_exec(const char *);
2888 
2889 /* fs/dcache.c -- generic fs support functions */
2890 extern bool is_subdir(struct dentry *, struct dentry *);
2891 extern bool path_is_under(const struct path *, const struct path *);
2892 u64 vfsmount_to_propagation_flags(struct vfsmount *mnt);
2893 
2894 extern char *file_path(struct file *, char *, int);
2895 
name_is_dot(const char * name,size_t len)2896 static inline bool name_is_dot(const char *name, size_t len)
2897 {
2898 	return unlikely(len == 1 && name[0] == '.');
2899 }
2900 
name_is_dotdot(const char * name,size_t len)2901 static inline bool name_is_dotdot(const char *name, size_t len)
2902 {
2903 	return unlikely(len == 2 && name[0] == '.' && name[1] == '.');
2904 }
2905 
2906 /**
2907  * name_is_dot_dotdot - returns true only if @name is "." or ".."
2908  * @name: file name to check
2909  * @len: length of file name, in bytes
2910  */
name_is_dot_dotdot(const char * name,size_t len)2911 static inline bool name_is_dot_dotdot(const char *name, size_t len)
2912 {
2913 	return len && unlikely(name[0] == '.') &&
2914 		(len == 1 || (len == 2 && name[1] == '.'));
2915 }
2916 
2917 /**
2918  * name_contains_dotdot - check if a file name contains ".." path components
2919  * @name: File path string to check
2920  * Search for ".." surrounded by either '/' or start/end of string.
2921  */
name_contains_dotdot(const char * name)2922 static inline bool name_contains_dotdot(const char *name)
2923 {
2924 	size_t name_len;
2925 
2926 	name_len = strlen(name);
2927 	return strcmp(name, "..") == 0 ||
2928 	       strncmp(name, "../", 3) == 0 ||
2929 	       strstr(name, "/../") != NULL ||
2930 	       (name_len >= 3 && strcmp(name + name_len - 3, "/..") == 0);
2931 }
2932 
2933 #include <linux/err.h>
2934 
2935 /* needed for stackable file system support */
2936 loff_t default_llseek(struct file *file, loff_t offset, int whence);
2937 
2938 loff_t vfs_llseek(struct file *file, loff_t offset, int whence);
2939 
2940 int inode_init_always_gfp(struct super_block *sb, struct inode *inode, gfp_t gfp);
inode_init_always(struct super_block * sb,struct inode * inode)2941 static inline int inode_init_always(struct super_block *sb, struct inode *inode)
2942 {
2943 	return inode_init_always_gfp(sb, inode, GFP_NOFS);
2944 }
2945 
2946 void inode_init_once(struct inode *inode);
2947 void address_space_init_once(struct address_space *mapping);
2948 struct inode *igrab(struct inode *inode);
2949 ino_t iunique(struct super_block *sb, ino_t max_reserved);
2950 int inode_needs_sync(struct inode *inode);
2951 int inode_just_drop(struct inode *inode);
inode_generic_drop(struct inode * inode)2952 static inline int inode_generic_drop(struct inode *inode)
2953 {
2954 	return !inode->i_nlink || inode_unhashed(inode);
2955 }
2956 void d_mark_dontcache(struct inode *inode);
2957 
2958 struct inode *ilookup5_nowait(struct super_block *sb, u64 hashval,
2959 			      int (*test)(struct inode *, void *), void *data,
2960 			      bool *isnew);
2961 struct inode *ilookup5(struct super_block *sb, u64 hashval,
2962 		       int (*test)(struct inode *, void *), void *data);
2963 struct inode *ilookup(struct super_block *sb, u64 ino);
2964 
2965 struct inode *inode_insert5(struct inode *inode, u64 hashval,
2966 			    int (*test)(struct inode *, void *),
2967 			    int (*set)(struct inode *, void *), void *data);
2968 struct inode *iget5_locked(struct super_block *sb, u64 hashval,
2969 			   int (*test)(struct inode *, void *),
2970 			   int (*set)(struct inode *, void *), void *data);
2971 struct inode *iget5_locked_rcu(struct super_block *sb, u64 hashval,
2972 			       int (*test)(struct inode *, void *),
2973 			       int (*set)(struct inode *, void *), void *data);
2974 struct inode *iget_locked(struct super_block *sb, u64 ino);
2975 struct inode *find_inode_nowait(struct super_block *sb, u64 hashval,
2976 				int (*match)(struct inode *, u64, void *),
2977 				void *data);
2978 struct inode *find_inode_rcu(struct super_block *sb, u64 hashval,
2979 			     int (*test)(struct inode *, void *), void *data);
2980 struct inode *find_inode_by_ino_rcu(struct super_block *sb, u64 ino);
2981 int insert_inode_locked4(struct inode *inode, u64 hashval,
2982 			 int (*test)(struct inode *, void *), void *data);
2983 int insert_inode_locked(struct inode *inode);
2984 #ifdef CONFIG_DEBUG_LOCK_ALLOC
2985 void lockdep_annotate_inode_mutex_key(struct inode *inode);
2986 #else
lockdep_annotate_inode_mutex_key(struct inode * inode)2987 static inline void lockdep_annotate_inode_mutex_key(struct inode *inode) { };
2988 #endif
2989 void unlock_new_inode(struct inode *inode);
2990 void discard_new_inode(struct inode *inode);
2991 unsigned int get_next_ino(void);
2992 void evict_inodes(struct super_block *sb);
2993 void dump_mapping(const struct address_space *);
2994 
2995 /*
2996  * Userspace may rely on the inode number being non-zero. For example, glibc
2997  * simply ignores files with zero i_ino in unlink() and other places.
2998  *
2999  * As an additional complication, if userspace was compiled with
3000  * _FILE_OFFSET_BITS=32 on a 64-bit kernel we'll only end up reading out the
3001  * lower 32 bits, so we need to check that those aren't zero explicitly. With
3002  * _FILE_OFFSET_BITS=64, this may cause some harmless false-negatives, but
3003  * better safe than sorry.
3004  */
is_zero_ino(ino_t ino)3005 static inline bool is_zero_ino(ino_t ino)
3006 {
3007 	return (u32)ino == 0;
3008 }
3009 
__iget(struct inode * inode)3010 static inline void __iget(struct inode *inode)
3011 {
3012 	lockdep_assert_held(&inode->i_lock);
3013 	atomic_inc(&inode->i_count);
3014 }
3015 
3016 extern void iget_failed(struct inode *);
3017 extern void clear_inode(struct inode *);
3018 extern void __destroy_inode(struct inode *);
3019 struct inode *alloc_inode(struct super_block *sb);
new_inode_pseudo(struct super_block * sb)3020 static inline struct inode *new_inode_pseudo(struct super_block *sb)
3021 {
3022 	return alloc_inode(sb);
3023 }
3024 extern struct inode *new_inode(struct super_block *sb);
3025 extern void free_inode_nonrcu(struct inode *inode);
3026 extern int setattr_should_drop_suidgid(struct mnt_idmap *, struct inode *);
3027 extern int file_remove_privs(struct file *);
3028 int setattr_should_drop_sgid(struct mnt_idmap *idmap,
3029 			     const struct inode *inode);
3030 
3031 /*
3032  * This must be used for allocating filesystems specific inodes to set
3033  * up the inode reclaim context correctly.
3034  */
3035 #define alloc_inode_sb(_sb, _cache, _gfp) kmem_cache_alloc_lru(_cache, &_sb->s_inode_lru, _gfp)
3036 
3037 void __insert_inode_hash(struct inode *inode, u64 hashval);
insert_inode_hash(struct inode * inode)3038 static inline void insert_inode_hash(struct inode *inode)
3039 {
3040 	__insert_inode_hash(inode, inode->i_ino);
3041 }
3042 
3043 void __remove_inode_hash(struct inode *inode);
remove_inode_hash(struct inode * inode)3044 static inline void remove_inode_hash(struct inode *inode)
3045 {
3046 	if (!inode_unhashed(inode) && !hlist_fake(&inode->i_hash))
3047 		__remove_inode_hash(inode);
3048 }
3049 
3050 void inode_sb_list_add(struct inode *inode);
3051 void inode_lru_list_add(struct inode *inode);
3052 
3053 int generic_file_mmap(struct file *, struct vm_area_struct *);
3054 int generic_file_mmap_prepare(struct vm_area_desc *desc);
3055 int generic_file_readonly_mmap(struct file *, struct vm_area_struct *);
3056 int generic_file_readonly_mmap_prepare(struct vm_area_desc *desc);
3057 extern ssize_t generic_write_checks(struct kiocb *, struct iov_iter *);
3058 int generic_write_checks_count(struct kiocb *iocb, loff_t *count);
3059 extern int generic_write_check_limits(struct file *file, loff_t pos,
3060 		loff_t *count);
3061 extern int generic_file_rw_checks(struct file *file_in, struct file *file_out);
3062 ssize_t filemap_read(struct kiocb *iocb, struct iov_iter *to,
3063 		ssize_t already_read);
3064 extern ssize_t generic_file_read_iter(struct kiocb *, struct iov_iter *);
3065 extern ssize_t __generic_file_write_iter(struct kiocb *, struct iov_iter *);
3066 extern ssize_t generic_file_write_iter(struct kiocb *, struct iov_iter *);
3067 extern ssize_t generic_file_direct_write(struct kiocb *, struct iov_iter *);
3068 ssize_t generic_perform_write(struct kiocb *, struct iov_iter *);
3069 ssize_t direct_write_fallback(struct kiocb *iocb, struct iov_iter *iter,
3070 		ssize_t direct_written, ssize_t buffered_written);
3071 
3072 ssize_t vfs_iter_read(struct file *file, struct iov_iter *iter, loff_t *ppos,
3073 		rwf_t flags);
3074 ssize_t vfs_iter_write(struct file *file, struct iov_iter *iter, loff_t *ppos,
3075 		rwf_t flags);
3076 ssize_t vfs_iocb_iter_read(struct file *file, struct kiocb *iocb,
3077 			   struct iov_iter *iter);
3078 ssize_t vfs_iocb_iter_write(struct file *file, struct kiocb *iocb,
3079 			    struct iov_iter *iter);
3080 
3081 /* fs/splice.c */
3082 ssize_t filemap_splice_read(struct file *in, loff_t *ppos,
3083 			    struct pipe_inode_info *pipe,
3084 			    size_t len, unsigned int flags);
3085 ssize_t copy_splice_read(struct file *in, loff_t *ppos,
3086 			 struct pipe_inode_info *pipe,
3087 			 size_t len, unsigned int flags);
3088 extern ssize_t iter_file_splice_write(struct pipe_inode_info *,
3089 		struct file *, loff_t *, size_t, unsigned int);
3090 
3091 
3092 extern void
3093 file_ra_state_init(struct file_ra_state *ra, struct address_space *mapping);
3094 extern loff_t noop_llseek(struct file *file, loff_t offset, int whence);
3095 extern loff_t vfs_setpos(struct file *file, loff_t offset, loff_t maxsize);
3096 extern loff_t generic_file_llseek(struct file *file, loff_t offset, int whence);
3097 extern loff_t generic_file_llseek_size(struct file *file, loff_t offset,
3098 		int whence, loff_t maxsize, loff_t eof);
3099 loff_t generic_llseek_cookie(struct file *file, loff_t offset, int whence,
3100 			     u64 *cookie);
3101 extern loff_t fixed_size_llseek(struct file *file, loff_t offset,
3102 		int whence, loff_t size);
3103 extern loff_t no_seek_end_llseek_size(struct file *, loff_t, int, loff_t);
3104 extern loff_t no_seek_end_llseek(struct file *, loff_t, int);
3105 int rw_verify_area(int, struct file *, const loff_t *, size_t);
3106 extern int generic_file_open(struct inode * inode, struct file * filp);
3107 extern int nonseekable_open(struct inode * inode, struct file * filp);
3108 extern int stream_open(struct inode * inode, struct file * filp);
3109 
3110 #ifdef CONFIG_BLOCK
3111 typedef void (dio_submit_t)(struct bio *bio, struct inode *inode,
3112 			    loff_t file_offset);
3113 
3114 enum {
3115 	/* need locking between buffered and direct access */
3116 	DIO_LOCKING	= 0x01,
3117 
3118 	/* filesystem does not support filling holes */
3119 	DIO_SKIP_HOLES	= 0x02,
3120 };
3121 
3122 ssize_t __blockdev_direct_IO(struct kiocb *iocb, struct inode *inode,
3123 			     struct block_device *bdev, struct iov_iter *iter,
3124 			     get_block_t get_block,
3125 			     dio_iodone_t end_io,
3126 			     int flags);
3127 
blockdev_direct_IO(struct kiocb * iocb,struct inode * inode,struct iov_iter * iter,get_block_t get_block)3128 static inline ssize_t blockdev_direct_IO(struct kiocb *iocb,
3129 					 struct inode *inode,
3130 					 struct iov_iter *iter,
3131 					 get_block_t get_block)
3132 {
3133 	return __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev, iter,
3134 			get_block, NULL, DIO_LOCKING | DIO_SKIP_HOLES);
3135 }
3136 #endif
3137 
3138 bool inode_dio_finished(const struct inode *inode);
3139 void inode_dio_wait(struct inode *inode);
3140 void inode_dio_wait_interruptible(struct inode *inode);
3141 
3142 /**
3143  * inode_dio_begin - signal start of a direct I/O requests
3144  * @inode: inode the direct I/O happens on
3145  *
3146  * This is called once we've finished processing a direct I/O request,
3147  * and is used to wake up callers waiting for direct I/O to be quiesced.
3148  */
inode_dio_begin(struct inode * inode)3149 static inline void inode_dio_begin(struct inode *inode)
3150 {
3151 	atomic_inc(&inode->i_dio_count);
3152 }
3153 
3154 /**
3155  * inode_dio_end - signal finish of a direct I/O requests
3156  * @inode: inode the direct I/O happens on
3157  *
3158  * This is called once we've finished processing a direct I/O request,
3159  * and is used to wake up callers waiting for direct I/O to be quiesced.
3160  */
inode_dio_end(struct inode * inode)3161 static inline void inode_dio_end(struct inode *inode)
3162 {
3163 	if (atomic_dec_and_test(&inode->i_dio_count))
3164 		wake_up_var(&inode->i_dio_count);
3165 }
3166 
3167 extern void inode_set_flags(struct inode *inode, unsigned int flags,
3168 			    unsigned int mask);
3169 
3170 extern const struct file_operations generic_ro_fops;
3171 
3172 #define special_file(m) (S_ISCHR(m)||S_ISBLK(m)||S_ISFIFO(m)||S_ISSOCK(m))
3173 
3174 extern int readlink_copy(char __user *, int, const char *, int);
3175 extern int page_readlink(struct dentry *, char __user *, int);
3176 extern const char *page_get_link_raw(struct dentry *, struct inode *,
3177 				     struct delayed_call *);
3178 extern const char *page_get_link(struct dentry *, struct inode *,
3179 				 struct delayed_call *);
3180 extern void page_put_link(void *);
3181 extern int page_symlink(struct inode *inode, const char *symname, int len);
3182 extern const struct inode_operations page_symlink_inode_operations;
3183 extern void kfree_link(void *);
3184 void fill_mg_cmtime(struct kstat *stat, u32 request_mask, struct inode *inode);
3185 void generic_fillattr(struct mnt_idmap *, u32, struct inode *, struct kstat *);
3186 void generic_fill_statx_attr(struct inode *inode, struct kstat *stat);
3187 void generic_fill_statx_atomic_writes(struct kstat *stat,
3188 				      unsigned int unit_min,
3189 				      unsigned int unit_max,
3190 				      unsigned int unit_max_opt);
3191 extern int vfs_getattr_nosec(const struct path *, struct kstat *, u32, unsigned int);
3192 extern int vfs_getattr(const struct path *, struct kstat *, u32, unsigned int);
3193 void __inode_add_bytes(struct inode *inode, loff_t bytes);
3194 void inode_add_bytes(struct inode *inode, loff_t bytes);
3195 void __inode_sub_bytes(struct inode *inode, loff_t bytes);
3196 void inode_sub_bytes(struct inode *inode, loff_t bytes);
__inode_get_bytes(struct inode * inode)3197 static inline loff_t __inode_get_bytes(struct inode *inode)
3198 {
3199 	return (((loff_t)inode->i_blocks) << 9) + inode->i_bytes;
3200 }
3201 loff_t inode_get_bytes(struct inode *inode);
3202 void inode_set_bytes(struct inode *inode, loff_t bytes);
3203 const char *simple_get_link(struct dentry *, struct inode *,
3204 			    struct delayed_call *);
3205 extern const struct inode_operations simple_symlink_inode_operations;
3206 
3207 extern int iterate_dir(struct file *, struct dir_context *);
3208 
3209 int vfs_fstatat(int dfd, const char __user *filename, struct kstat *stat,
3210 		int flags);
3211 int vfs_fstat(int fd, struct kstat *stat);
3212 
vfs_stat(const char __user * filename,struct kstat * stat)3213 static inline int vfs_stat(const char __user *filename, struct kstat *stat)
3214 {
3215 	return vfs_fstatat(AT_FDCWD, filename, stat, 0);
3216 }
vfs_lstat(const char __user * name,struct kstat * stat)3217 static inline int vfs_lstat(const char __user *name, struct kstat *stat)
3218 {
3219 	return vfs_fstatat(AT_FDCWD, name, stat, AT_SYMLINK_NOFOLLOW);
3220 }
3221 
3222 extern const char *vfs_get_link(struct dentry *, struct delayed_call *);
3223 extern int vfs_readlink(struct dentry *, char __user *, int);
3224 
3225 extern struct file_system_type *get_filesystem(struct file_system_type *fs);
3226 extern void put_filesystem(struct file_system_type *fs);
3227 extern struct file_system_type *get_fs_type(const char *name);
3228 extern void drop_super(struct super_block *sb);
3229 extern void drop_super_exclusive(struct super_block *sb);
3230 extern void iterate_supers(void (*f)(struct super_block *, void *), void *arg);
3231 extern void iterate_supers_type(struct file_system_type *,
3232 			        void (*)(struct super_block *, void *), void *);
3233 void filesystems_freeze(bool freeze_all);
3234 void filesystems_thaw(void);
3235 
3236 void end_dirop(struct dentry *de);
3237 
3238 extern int dcache_dir_open(struct inode *, struct file *);
3239 extern int dcache_dir_close(struct inode *, struct file *);
3240 extern loff_t dcache_dir_lseek(struct file *, loff_t, int);
3241 extern int dcache_readdir(struct file *, struct dir_context *);
3242 extern int simple_setattr(struct mnt_idmap *, struct dentry *,
3243 			  struct iattr *);
3244 extern int simple_getattr(struct mnt_idmap *, const struct path *,
3245 			  struct kstat *, u32, unsigned int);
3246 extern int simple_statfs(struct dentry *, struct kstatfs *);
3247 extern int simple_open(struct inode *inode, struct file *file);
3248 extern int simple_link(struct dentry *, struct inode *, struct dentry *);
3249 extern int simple_unlink(struct inode *, struct dentry *);
3250 extern int simple_rmdir(struct inode *, struct dentry *);
3251 extern void __simple_unlink(struct inode *, struct dentry *);
3252 extern void __simple_rmdir(struct inode *, struct dentry *);
3253 void simple_rename_timestamp(struct inode *old_dir, struct dentry *old_dentry,
3254 			     struct inode *new_dir, struct dentry *new_dentry);
3255 extern int simple_rename_exchange(struct inode *old_dir, struct dentry *old_dentry,
3256 				  struct inode *new_dir, struct dentry *new_dentry);
3257 extern int simple_rename(struct mnt_idmap *, struct inode *,
3258 			 struct dentry *, struct inode *, struct dentry *,
3259 			 unsigned int);
3260 extern void simple_recursive_removal(struct dentry *,
3261                               void (*callback)(struct dentry *));
3262 extern void simple_remove_by_name(struct dentry *, const char *,
3263                               void (*callback)(struct dentry *));
3264 extern void locked_recursive_removal(struct dentry *,
3265                               void (*callback)(struct dentry *));
3266 extern int noop_fsync(struct file *, loff_t, loff_t, int);
3267 extern ssize_t noop_direct_IO(struct kiocb *iocb, struct iov_iter *iter);
3268 extern int simple_empty(struct dentry *);
3269 extern int simple_write_begin(const struct kiocb *iocb,
3270 			      struct address_space *mapping,
3271 			      loff_t pos, unsigned len,
3272 			      struct folio **foliop, void **fsdata);
3273 extern const struct address_space_operations ram_aops;
3274 extern int always_delete_dentry(const struct dentry *);
3275 extern struct inode *alloc_anon_inode(struct super_block *);
3276 struct inode *anon_inode_make_secure_inode(struct super_block *sb, const char *name,
3277 					   const struct inode *context_inode);
3278 
3279 extern struct dentry *simple_lookup(struct inode *, struct dentry *, unsigned int flags);
3280 extern ssize_t generic_read_dir(struct file *, char __user *, size_t, loff_t *);
3281 extern const struct file_operations simple_dir_operations;
3282 extern const struct inode_operations simple_dir_inode_operations;
3283 extern void make_empty_dir_inode(struct inode *inode);
3284 extern bool is_empty_dir_inode(struct inode *inode);
3285 struct tree_descr { const char *name; const struct file_operations *ops; int mode; };
3286 struct dentry *d_alloc_name(struct dentry *, const char *);
3287 extern int simple_fill_super(struct super_block *, unsigned long,
3288 			     const struct tree_descr *);
3289 extern int simple_pin_fs(struct file_system_type *, struct vfsmount **mount, int *count);
3290 extern void simple_release_fs(struct vfsmount **mount, int *count);
3291 struct dentry *simple_start_creating(struct dentry *, const char *);
3292 void simple_done_creating(struct dentry *);
3293 
3294 extern ssize_t simple_read_from_buffer(void __user *to, size_t count,
3295 			loff_t *ppos, const void *from, size_t available);
3296 extern ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos,
3297 		const void __user *from, size_t count);
3298 
3299 struct offset_ctx {
3300 	struct maple_tree	mt;
3301 	unsigned long		next_offset;
3302 };
3303 
3304 void simple_offset_init(struct offset_ctx *octx);
3305 int simple_offset_add(struct offset_ctx *octx, struct dentry *dentry);
3306 void simple_offset_remove(struct offset_ctx *octx, struct dentry *dentry);
3307 void simple_offset_rename(struct inode *old_dir, struct dentry *old_dentry,
3308 			 struct inode *new_dir, struct dentry *new_dentry);
3309 int simple_offset_rename_exchange(struct inode *old_dir,
3310 				  struct dentry *old_dentry,
3311 				  struct inode *new_dir,
3312 				  struct dentry *new_dentry);
3313 void simple_offset_destroy(struct offset_ctx *octx);
3314 
3315 extern const struct file_operations simple_offset_dir_operations;
3316 
3317 extern int simple_fsync_noflush(struct file *, loff_t, loff_t, int);
3318 extern int simple_fsync(struct file *, loff_t, loff_t, int);
3319 
3320 extern int generic_check_addressable(unsigned, u64);
3321 
3322 extern void generic_set_sb_d_ops(struct super_block *sb);
3323 extern int generic_ci_match(const struct inode *parent,
3324 			    const struct qstr *name,
3325 			    const struct qstr *folded_name,
3326 			    const u8 *de_name, u32 de_name_len);
3327 
3328 #if IS_ENABLED(CONFIG_UNICODE)
3329 int generic_ci_d_hash(const struct dentry *dentry, struct qstr *str);
3330 int generic_ci_d_compare(const struct dentry *dentry, unsigned int len,
3331 			 const char *str, const struct qstr *name);
3332 
3333 /**
3334  * generic_ci_validate_strict_name - Check if a given name is suitable
3335  * for a directory
3336  *
3337  * This functions checks if the proposed filename is valid for the
3338  * parent directory. That means that only valid UTF-8 filenames will be
3339  * accepted for casefold directories from filesystems created with the
3340  * strict encoding flag.  That also means that any name will be
3341  * accepted for directories that doesn't have casefold enabled, or
3342  * aren't being strict with the encoding.
3343  *
3344  * @dir: inode of the directory where the new file will be created
3345  * @name: name of the new file
3346  *
3347  * Return:
3348  * * True: if the filename is suitable for this directory. It can be
3349  *   true if a given name is not suitable for a strict encoding
3350  *   directory, but the directory being used isn't strict
3351  * * False if the filename isn't suitable for this directory. This only
3352  *   happens when a directory is casefolded and the filesystem is strict
3353  *   about its encoding.
3354  */
generic_ci_validate_strict_name(struct inode * dir,const struct qstr * name)3355 static inline bool generic_ci_validate_strict_name(struct inode *dir,
3356 						   const struct qstr *name)
3357 {
3358 	if (!IS_CASEFOLDED(dir) || !sb_has_strict_encoding(dir->i_sb))
3359 		return true;
3360 
3361 	/*
3362 	 * A casefold dir must have a encoding set, unless the filesystem
3363 	 * is corrupted
3364 	 */
3365 	if (WARN_ON_ONCE(!dir->i_sb->s_encoding))
3366 		return true;
3367 
3368 	return !utf8_validate(dir->i_sb->s_encoding, name);
3369 }
3370 #else
generic_ci_validate_strict_name(struct inode * dir,const struct qstr * name)3371 static inline bool generic_ci_validate_strict_name(struct inode *dir,
3372 						   const struct qstr *name)
3373 {
3374 	return true;
3375 }
3376 #endif
3377 
3378 int may_setattr(struct mnt_idmap *idmap, struct inode *inode,
3379 		unsigned int ia_valid);
3380 int setattr_prepare(struct mnt_idmap *, struct dentry *, struct iattr *);
3381 extern int inode_newsize_ok(const struct inode *, loff_t offset);
3382 void setattr_copy(struct mnt_idmap *, struct inode *inode,
3383 		  const struct iattr *attr);
3384 
3385 extern int file_update_time(struct file *file);
3386 
file_is_dax(const struct file * file)3387 static inline bool file_is_dax(const struct file *file)
3388 {
3389 	return file && IS_DAX(file->f_mapping->host);
3390 }
3391 
vma_is_dax(const struct vm_area_struct * vma)3392 static inline bool vma_is_dax(const struct vm_area_struct *vma)
3393 {
3394 	return file_is_dax(vma->vm_file);
3395 }
3396 
vma_is_fsdax(struct vm_area_struct * vma)3397 static inline bool vma_is_fsdax(struct vm_area_struct *vma)
3398 {
3399 	struct inode *inode;
3400 
3401 	if (!IS_ENABLED(CONFIG_FS_DAX) || !vma->vm_file)
3402 		return false;
3403 	if (!vma_is_dax(vma))
3404 		return false;
3405 	inode = file_inode(vma->vm_file);
3406 	if (S_ISCHR(inode->i_mode))
3407 		return false; /* device-dax */
3408 	return true;
3409 }
3410 
iocb_flags(struct file * file)3411 static inline int iocb_flags(struct file *file)
3412 {
3413 	int res = 0;
3414 	if (file->f_flags & O_APPEND)
3415 		res |= IOCB_APPEND;
3416 	if (file->f_flags & O_DIRECT)
3417 		res |= IOCB_DIRECT;
3418 	if (file->f_flags & O_DSYNC)
3419 		res |= IOCB_DSYNC;
3420 	if (file->f_flags & __O_SYNC)
3421 		res |= IOCB_SYNC;
3422 	return res;
3423 }
3424 
kiocb_set_rw_flags(struct kiocb * ki,rwf_t flags,int rw_type)3425 static inline int kiocb_set_rw_flags(struct kiocb *ki, rwf_t flags,
3426 				     int rw_type)
3427 {
3428 	int kiocb_flags = 0;
3429 
3430 	/* make sure there's no overlap between RWF and private IOCB flags */
3431 	BUILD_BUG_ON((__force int) RWF_SUPPORTED & IOCB_EVENTFD);
3432 
3433 	if (!flags)
3434 		return 0;
3435 	if (unlikely(flags & ~RWF_SUPPORTED))
3436 		return -EOPNOTSUPP;
3437 	if (unlikely((flags & RWF_APPEND) && (flags & RWF_NOAPPEND)))
3438 		return -EINVAL;
3439 
3440 	if (flags & RWF_NOWAIT) {
3441 		if (!(ki->ki_filp->f_mode & FMODE_NOWAIT))
3442 			return -EOPNOTSUPP;
3443 	}
3444 	if (flags & RWF_ATOMIC) {
3445 		if (rw_type != WRITE)
3446 			return -EOPNOTSUPP;
3447 		if (!(ki->ki_filp->f_mode & FMODE_CAN_ATOMIC_WRITE))
3448 			return -EOPNOTSUPP;
3449 	}
3450 	if (flags & RWF_DONTCACHE) {
3451 		/* file system must support it */
3452 		if (!(ki->ki_filp->f_op->fop_flags & FOP_DONTCACHE))
3453 			return -EOPNOTSUPP;
3454 		/* DAX mappings not supported */
3455 		if (IS_DAX(ki->ki_filp->f_mapping->host))
3456 			return -EOPNOTSUPP;
3457 	}
3458 	kiocb_flags |= (__force int) (flags & RWF_SUPPORTED);
3459 	if (flags & RWF_SYNC)
3460 		kiocb_flags |= IOCB_DSYNC;
3461 
3462 	if ((flags & RWF_NOAPPEND) && (ki->ki_flags & IOCB_APPEND)) {
3463 		if (IS_APPEND(file_inode(ki->ki_filp)))
3464 			return -EPERM;
3465 		ki->ki_flags &= ~IOCB_APPEND;
3466 	}
3467 
3468 	ki->ki_flags |= kiocb_flags;
3469 	return 0;
3470 }
3471 
3472 /* Transaction based IO helpers */
3473 
3474 /*
3475  * An argresp is stored in an allocated page and holds the
3476  * size of the argument or response, along with its content
3477  */
3478 struct simple_transaction_argresp {
3479 	ssize_t size;
3480 	char data[];
3481 };
3482 
3483 #define SIMPLE_TRANSACTION_LIMIT (PAGE_SIZE - sizeof(struct simple_transaction_argresp))
3484 
3485 char *simple_transaction_get(struct file *file, const char __user *buf,
3486 				size_t size);
3487 ssize_t simple_transaction_read(struct file *file, char __user *buf,
3488 				size_t size, loff_t *pos);
3489 int simple_transaction_release(struct inode *inode, struct file *file);
3490 
3491 void simple_transaction_set(struct file *file, size_t n);
3492 
3493 /*
3494  * simple attribute files
3495  *
3496  * These attributes behave similar to those in sysfs:
3497  *
3498  * Writing to an attribute immediately sets a value, an open file can be
3499  * written to multiple times.
3500  *
3501  * Reading from an attribute creates a buffer from the value that might get
3502  * read with multiple read calls. When the attribute has been read
3503  * completely, no further read calls are possible until the file is opened
3504  * again.
3505  *
3506  * All attributes contain a text representation of a numeric value
3507  * that are accessed with the get() and set() functions.
3508  */
3509 #define DEFINE_SIMPLE_ATTRIBUTE_XSIGNED(__fops, __get, __set, __fmt, __is_signed)	\
3510 static int __fops ## _open(struct inode *inode, struct file *file)	\
3511 {									\
3512 	__simple_attr_check_format(__fmt, 0ull);			\
3513 	return simple_attr_open(inode, file, __get, __set, __fmt);	\
3514 }									\
3515 static const struct file_operations __fops = {				\
3516 	.owner	 = THIS_MODULE,						\
3517 	.open	 = __fops ## _open,					\
3518 	.release = simple_attr_release,					\
3519 	.read	 = simple_attr_read,					\
3520 	.write	 = (__is_signed) ? simple_attr_write_signed : simple_attr_write,	\
3521 	.llseek	 = generic_file_llseek,					\
3522 }
3523 
3524 #define DEFINE_SIMPLE_ATTRIBUTE(__fops, __get, __set, __fmt)		\
3525 	DEFINE_SIMPLE_ATTRIBUTE_XSIGNED(__fops, __get, __set, __fmt, false)
3526 
3527 #define DEFINE_SIMPLE_ATTRIBUTE_SIGNED(__fops, __get, __set, __fmt)	\
3528 	DEFINE_SIMPLE_ATTRIBUTE_XSIGNED(__fops, __get, __set, __fmt, true)
3529 
3530 static inline __printf(1, 2)
__simple_attr_check_format(const char * fmt,...)3531 void __simple_attr_check_format(const char *fmt, ...)
3532 {
3533 	/* don't do anything, just let the compiler check the arguments; */
3534 }
3535 
3536 int simple_attr_open(struct inode *inode, struct file *file,
3537 		     int (*get)(void *, u64 *), int (*set)(void *, u64),
3538 		     const char *fmt);
3539 int simple_attr_release(struct inode *inode, struct file *file);
3540 ssize_t simple_attr_read(struct file *file, char __user *buf,
3541 			 size_t len, loff_t *ppos);
3542 ssize_t simple_attr_write(struct file *file, const char __user *buf,
3543 			  size_t len, loff_t *ppos);
3544 ssize_t simple_attr_write_signed(struct file *file, const char __user *buf,
3545 				 size_t len, loff_t *ppos);
3546 
3547 int __init list_bdev_fs_names(char *buf, size_t size);
3548 
3549 #define __FMODE_EXEC		((__force int) FMODE_EXEC)
3550 
3551 #define ACC_MODE(x) ("\004\002\006\006"[(x)&O_ACCMODE])
3552 #define OPEN_FMODE(flag) ((__force fmode_t)((flag + 1) & O_ACCMODE))
3553 
is_sxid(umode_t mode)3554 static inline bool is_sxid(umode_t mode)
3555 {
3556 	return mode & (S_ISUID | S_ISGID);
3557 }
3558 
check_sticky(struct mnt_idmap * idmap,struct inode * dir,struct inode * inode)3559 static inline int check_sticky(struct mnt_idmap *idmap,
3560 			       struct inode *dir, struct inode *inode)
3561 {
3562 	if (!(dir->i_mode & S_ISVTX))
3563 		return 0;
3564 
3565 	return __check_sticky(idmap, dir, inode);
3566 }
3567 
inode_has_no_xattr(struct inode * inode)3568 static inline void inode_has_no_xattr(struct inode *inode)
3569 {
3570 	if (!is_sxid(inode->i_mode) && (inode->i_sb->s_flags & SB_NOSEC))
3571 		inode->i_flags |= S_NOSEC;
3572 }
3573 
is_root_inode(struct inode * inode)3574 static inline bool is_root_inode(struct inode *inode)
3575 {
3576 	return inode == inode->i_sb->s_root->d_inode;
3577 }
3578 
dir_emit(struct dir_context * ctx,const char * name,int namelen,u64 ino,unsigned type)3579 static inline bool dir_emit(struct dir_context *ctx,
3580 			    const char *name, int namelen,
3581 			    u64 ino, unsigned type)
3582 {
3583 	unsigned int dt_mask = S_DT_MASK | ctx->dt_flags_mask;
3584 
3585 	return ctx->actor(ctx, name, namelen, ctx->pos, ino, type & dt_mask);
3586 }
dir_emit_dot(struct file * file,struct dir_context * ctx)3587 static inline bool dir_emit_dot(struct file *file, struct dir_context *ctx)
3588 {
3589 	return ctx->actor(ctx, ".", 1, ctx->pos,
3590 			  file->f_path.dentry->d_inode->i_ino, DT_DIR);
3591 }
dir_emit_dotdot(struct file * file,struct dir_context * ctx)3592 static inline bool dir_emit_dotdot(struct file *file, struct dir_context *ctx)
3593 {
3594 	return ctx->actor(ctx, "..", 2, ctx->pos,
3595 			  d_parent_ino(file->f_path.dentry), DT_DIR);
3596 }
dir_emit_dots(struct file * file,struct dir_context * ctx)3597 static inline bool dir_emit_dots(struct file *file, struct dir_context *ctx)
3598 {
3599 	if (ctx->pos == 0) {
3600 		if (!dir_emit_dot(file, ctx))
3601 			return false;
3602 		ctx->pos = 1;
3603 	}
3604 	if (ctx->pos == 1) {
3605 		if (!dir_emit_dotdot(file, ctx))
3606 			return false;
3607 		ctx->pos = 2;
3608 	}
3609 	return true;
3610 }
dir_relax(struct inode * inode)3611 static inline bool dir_relax(struct inode *inode)
3612 {
3613 	inode_unlock(inode);
3614 	inode_lock(inode);
3615 	return !IS_DEADDIR(inode);
3616 }
3617 
dir_relax_shared(struct inode * inode)3618 static inline bool dir_relax_shared(struct inode *inode)
3619 {
3620 	inode_unlock_shared(inode);
3621 	inode_lock_shared(inode);
3622 	return !IS_DEADDIR(inode);
3623 }
3624 
3625 extern bool path_noexec(const struct path *path);
3626 extern void inode_nohighmem(struct inode *inode);
3627 
3628 /* mm/fadvise.c */
3629 extern int vfs_fadvise(struct file *file, loff_t offset, loff_t len,
3630 		       int advice);
3631 extern int generic_fadvise(struct file *file, loff_t offset, loff_t len,
3632 			   int advice);
3633 
vfs_empty_path(int dfd,const char __user * path)3634 static inline bool vfs_empty_path(int dfd, const char __user *path)
3635 {
3636 	char c;
3637 
3638 	if (dfd < 0)
3639 		return false;
3640 
3641 	/* We now allow NULL to be used for empty path. */
3642 	if (!path)
3643 		return true;
3644 
3645 	if (unlikely(get_user(c, path)))
3646 		return false;
3647 
3648 	return !c;
3649 }
3650 
3651 int generic_atomic_write_valid(struct kiocb *iocb, struct iov_iter *iter);
3652 
extensible_ioctl_valid(unsigned int cmd_a,unsigned int cmd_b,size_t min_size)3653 static inline bool extensible_ioctl_valid(unsigned int cmd_a,
3654 					  unsigned int cmd_b, size_t min_size)
3655 {
3656 	if (_IOC_DIR(cmd_a) != _IOC_DIR(cmd_b))
3657 		return false;
3658 	if (_IOC_TYPE(cmd_a) != _IOC_TYPE(cmd_b))
3659 		return false;
3660 	if (_IOC_NR(cmd_a) != _IOC_NR(cmd_b))
3661 		return false;
3662 	if (_IOC_SIZE(cmd_a) < min_size)
3663 		return false;
3664 	return true;
3665 }
3666 
3667 #endif /* _LINUX_FS_H */
3668