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