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