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