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