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