1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * (C) 1997 Linus Torvalds 4 * (C) 1999 Andrea Arcangeli <andrea@suse.de> (dynamic inode allocation) 5 */ 6 #include <linux/export.h> 7 #include <linux/fs.h> 8 #include <linux/filelock.h> 9 #include <linux/mm.h> 10 #include <linux/backing-dev.h> 11 #include <linux/hash.h> 12 #include <linux/swap.h> 13 #include <linux/security.h> 14 #include <linux/cdev.h> 15 #include <linux/memblock.h> 16 #include <linux/fsnotify.h> 17 #include <linux/fsverity.h> 18 #include <linux/mount.h> 19 #include <linux/posix_acl.h> 20 #include <linux/ratelimit.h> 21 #include <linux/list_lru.h> 22 #include <linux/iversion.h> 23 #include <linux/rw_hint.h> 24 #include <linux/seq_file.h> 25 #include <linux/debugfs.h> 26 #include <trace/events/writeback.h> 27 #define CREATE_TRACE_POINTS 28 #include <trace/events/timestamp.h> 29 30 #include "internal.h" 31 32 /* 33 * Inode locking rules: 34 * 35 * inode->i_lock protects: 36 * inode->i_state, inode->i_hash, __iget(), inode->i_io_list 37 * Inode LRU list locks protect: 38 * inode->i_sb->s_inode_lru, inode->i_lru 39 * inode->i_sb->s_inode_list_lock protects: 40 * inode->i_sb->s_inodes, inode->i_sb_list 41 * bdi->wb.list_lock protects: 42 * bdi->wb.b_{dirty,io,more_io,dirty_time}, inode->i_io_list 43 * inode_hash_lock protects: 44 * inode_hashtable, inode->i_hash 45 * 46 * Lock ordering: 47 * 48 * inode->i_sb->s_inode_list_lock 49 * inode->i_lock 50 * Inode LRU list locks 51 * 52 * bdi->wb.list_lock 53 * inode->i_lock 54 * 55 * inode_hash_lock 56 * inode->i_lock 57 */ 58 59 static unsigned int i_hash_mask __ro_after_init; 60 static unsigned int i_hash_shift __ro_after_init; 61 static struct hlist_head *inode_hashtable __ro_after_init; 62 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_hash_lock); 63 64 /* 65 * Empty aops. Can be used for the cases where the user does not 66 * define any of the address_space operations. 67 */ 68 const struct address_space_operations empty_aops = { 69 }; 70 EXPORT_SYMBOL(empty_aops); 71 72 static DEFINE_PER_CPU(unsigned long, nr_inodes); 73 static DEFINE_PER_CPU(unsigned long, nr_unused); 74 75 static struct kmem_cache *inode_cachep __ro_after_init; 76 77 static long get_nr_inodes(void) 78 { 79 int i; 80 long sum = 0; 81 for_each_possible_cpu(i) 82 sum += per_cpu(nr_inodes, i); 83 return sum < 0 ? 0 : sum; 84 } 85 86 static inline long get_nr_inodes_unused(void) 87 { 88 int i; 89 long sum = 0; 90 for_each_possible_cpu(i) 91 sum += per_cpu(nr_unused, i); 92 return sum < 0 ? 0 : sum; 93 } 94 95 long get_nr_dirty_inodes(void) 96 { 97 /* not actually dirty inodes, but a wild approximation */ 98 long nr_dirty = get_nr_inodes() - get_nr_inodes_unused(); 99 return nr_dirty > 0 ? nr_dirty : 0; 100 } 101 102 #ifdef CONFIG_DEBUG_FS 103 static DEFINE_PER_CPU(long, mg_ctime_updates); 104 static DEFINE_PER_CPU(long, mg_fine_stamps); 105 static DEFINE_PER_CPU(long, mg_ctime_swaps); 106 107 static unsigned long get_mg_ctime_updates(void) 108 { 109 unsigned long sum = 0; 110 int i; 111 112 for_each_possible_cpu(i) 113 sum += data_race(per_cpu(mg_ctime_updates, i)); 114 return sum; 115 } 116 117 static unsigned long get_mg_fine_stamps(void) 118 { 119 unsigned long sum = 0; 120 int i; 121 122 for_each_possible_cpu(i) 123 sum += data_race(per_cpu(mg_fine_stamps, i)); 124 return sum; 125 } 126 127 static unsigned long get_mg_ctime_swaps(void) 128 { 129 unsigned long sum = 0; 130 int i; 131 132 for_each_possible_cpu(i) 133 sum += data_race(per_cpu(mg_ctime_swaps, i)); 134 return sum; 135 } 136 137 #define mgtime_counter_inc(__var) this_cpu_inc(__var) 138 139 static int mgts_show(struct seq_file *s, void *p) 140 { 141 unsigned long ctime_updates = get_mg_ctime_updates(); 142 unsigned long ctime_swaps = get_mg_ctime_swaps(); 143 unsigned long fine_stamps = get_mg_fine_stamps(); 144 unsigned long floor_swaps = timekeeping_get_mg_floor_swaps(); 145 146 seq_printf(s, "%lu %lu %lu %lu\n", 147 ctime_updates, ctime_swaps, fine_stamps, floor_swaps); 148 return 0; 149 } 150 151 DEFINE_SHOW_ATTRIBUTE(mgts); 152 153 static int __init mg_debugfs_init(void) 154 { 155 debugfs_create_file("multigrain_timestamps", S_IFREG | S_IRUGO, NULL, NULL, &mgts_fops); 156 return 0; 157 } 158 late_initcall(mg_debugfs_init); 159 160 #else /* ! CONFIG_DEBUG_FS */ 161 162 #define mgtime_counter_inc(__var) do { } while (0) 163 164 #endif /* CONFIG_DEBUG_FS */ 165 166 /* 167 * Handle nr_inode sysctl 168 */ 169 #ifdef CONFIG_SYSCTL 170 /* 171 * Statistics gathering.. 172 */ 173 static struct inodes_stat_t inodes_stat; 174 175 static int proc_nr_inodes(const struct ctl_table *table, int write, void *buffer, 176 size_t *lenp, loff_t *ppos) 177 { 178 inodes_stat.nr_inodes = get_nr_inodes(); 179 inodes_stat.nr_unused = get_nr_inodes_unused(); 180 return proc_doulongvec_minmax(table, write, buffer, lenp, ppos); 181 } 182 183 static const struct ctl_table inodes_sysctls[] = { 184 { 185 .procname = "inode-nr", 186 .data = &inodes_stat, 187 .maxlen = 2*sizeof(long), 188 .mode = 0444, 189 .proc_handler = proc_nr_inodes, 190 }, 191 { 192 .procname = "inode-state", 193 .data = &inodes_stat, 194 .maxlen = 7*sizeof(long), 195 .mode = 0444, 196 .proc_handler = proc_nr_inodes, 197 }, 198 }; 199 200 static int __init init_fs_inode_sysctls(void) 201 { 202 register_sysctl_init("fs", inodes_sysctls); 203 return 0; 204 } 205 early_initcall(init_fs_inode_sysctls); 206 #endif 207 208 static int no_open(struct inode *inode, struct file *file) 209 { 210 return -ENXIO; 211 } 212 213 /** 214 * inode_init_always_gfp - perform inode structure initialisation 215 * @sb: superblock inode belongs to 216 * @inode: inode to initialise 217 * @gfp: allocation flags 218 * 219 * These are initializations that need to be done on every inode 220 * allocation as the fields are not initialised by slab allocation. 221 * If there are additional allocations required @gfp is used. 222 */ 223 int inode_init_always_gfp(struct super_block *sb, struct inode *inode, gfp_t gfp) 224 { 225 static const struct inode_operations empty_iops; 226 static const struct file_operations no_open_fops = {.open = no_open}; 227 struct address_space *const mapping = &inode->i_data; 228 229 inode->i_sb = sb; 230 inode->i_blkbits = sb->s_blocksize_bits; 231 inode->i_flags = 0; 232 inode_state_assign_raw(inode, 0); 233 atomic64_set(&inode->i_sequence, 0); 234 atomic_set(&inode->i_count, 1); 235 inode->i_op = &empty_iops; 236 inode->i_fop = &no_open_fops; 237 inode->i_ino = 0; 238 inode->__i_nlink = 1; 239 inode->i_opflags = 0; 240 if (sb->s_xattr) 241 inode->i_opflags |= IOP_XATTR; 242 if (sb->s_type->fs_flags & FS_MGTIME) 243 inode->i_opflags |= IOP_MGTIME; 244 i_uid_write(inode, 0); 245 i_gid_write(inode, 0); 246 atomic_set(&inode->i_writecount, 0); 247 inode->i_size = 0; 248 inode->i_write_hint = WRITE_LIFE_NOT_SET; 249 inode->i_blocks = 0; 250 inode->i_bytes = 0; 251 inode->i_generation = 0; 252 inode->i_pipe = NULL; 253 inode->i_cdev = NULL; 254 inode->i_link = NULL; 255 inode->i_dir_seq = 0; 256 inode->i_rdev = 0; 257 inode->dirtied_when = 0; 258 259 #ifdef CONFIG_CGROUP_WRITEBACK 260 inode->i_wb_frn_winner = 0; 261 inode->i_wb_frn_avg_time = 0; 262 inode->i_wb_frn_history = 0; 263 #endif 264 265 spin_lock_init(&inode->i_lock); 266 lockdep_set_class(&inode->i_lock, &sb->s_type->i_lock_key); 267 268 init_rwsem(&inode->i_rwsem); 269 lockdep_set_class(&inode->i_rwsem, &sb->s_type->i_mutex_key); 270 271 atomic_set(&inode->i_dio_count, 0); 272 273 mapping->a_ops = &empty_aops; 274 mapping->host = inode; 275 mapping->flags = 0; 276 mapping->wb_err = 0; 277 atomic_set(&mapping->i_mmap_writable, 0); 278 mapping_set_gfp_mask(mapping, GFP_HIGHUSER_MOVABLE); 279 mapping->writeback_index = 0; 280 init_rwsem(&mapping->invalidate_lock); 281 lockdep_set_class_and_name(&mapping->invalidate_lock, 282 &sb->s_type->invalidate_lock_key, 283 "mapping.invalidate_lock"); 284 if (sb->s_iflags & SB_I_STABLE_WRITES) 285 mapping_set_stable_writes(mapping); 286 inode->i_private = NULL; 287 inode->i_mapping = mapping; 288 INIT_HLIST_HEAD(&inode->i_dentry); /* buggered by rcu freeing */ 289 #ifdef CONFIG_FS_POSIX_ACL 290 inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED; 291 #endif 292 293 #ifdef CONFIG_FSNOTIFY 294 inode->i_fsnotify_mask = 0; 295 #endif 296 inode->i_flctx = NULL; 297 298 if (unlikely(security_inode_alloc(inode, gfp))) 299 return -ENOMEM; 300 301 this_cpu_inc(nr_inodes); 302 303 return 0; 304 } 305 EXPORT_SYMBOL(inode_init_always_gfp); 306 307 void free_inode_nonrcu(struct inode *inode) 308 { 309 kmem_cache_free(inode_cachep, inode); 310 } 311 EXPORT_SYMBOL(free_inode_nonrcu); 312 313 static void i_callback(struct rcu_head *head) 314 { 315 struct inode *inode = container_of(head, struct inode, i_rcu); 316 if (inode->free_inode) 317 inode->free_inode(inode); 318 else 319 free_inode_nonrcu(inode); 320 } 321 322 /** 323 * alloc_inode - obtain an inode 324 * @sb: superblock 325 * 326 * Allocates a new inode for given superblock. 327 * Inode wont be chained in superblock s_inodes list 328 * This means : 329 * - fs can't be unmount 330 * - quotas, fsnotify, writeback can't work 331 */ 332 struct inode *alloc_inode(struct super_block *sb) 333 { 334 const struct super_operations *ops = sb->s_op; 335 struct inode *inode; 336 337 if (ops->alloc_inode) 338 inode = ops->alloc_inode(sb); 339 else 340 inode = alloc_inode_sb(sb, inode_cachep, GFP_KERNEL); 341 342 if (!inode) 343 return NULL; 344 345 if (unlikely(inode_init_always(sb, inode))) { 346 if (ops->destroy_inode) { 347 ops->destroy_inode(inode); 348 if (!ops->free_inode) 349 return NULL; 350 } 351 inode->free_inode = ops->free_inode; 352 i_callback(&inode->i_rcu); 353 return NULL; 354 } 355 356 return inode; 357 } 358 359 void __destroy_inode(struct inode *inode) 360 { 361 inode_detach_wb(inode); 362 security_inode_free(inode); 363 fsnotify_inode_delete(inode); 364 locks_free_lock_context(inode); 365 if (!inode->i_nlink) { 366 WARN_ON(atomic_long_read(&inode->i_sb->s_remove_count) == 0); 367 atomic_long_dec(&inode->i_sb->s_remove_count); 368 } 369 370 #ifdef CONFIG_FS_POSIX_ACL 371 if (inode->i_acl && !is_uncached_acl(inode->i_acl)) 372 posix_acl_release(inode->i_acl); 373 if (inode->i_default_acl && !is_uncached_acl(inode->i_default_acl)) 374 posix_acl_release(inode->i_default_acl); 375 #endif 376 this_cpu_dec(nr_inodes); 377 } 378 EXPORT_SYMBOL(__destroy_inode); 379 380 static void destroy_inode(struct inode *inode) 381 { 382 const struct super_operations *ops = inode->i_sb->s_op; 383 384 BUG_ON(!list_empty(&inode->i_lru)); 385 __destroy_inode(inode); 386 if (ops->destroy_inode) { 387 ops->destroy_inode(inode); 388 if (!ops->free_inode) 389 return; 390 } 391 inode->free_inode = ops->free_inode; 392 call_rcu(&inode->i_rcu, i_callback); 393 } 394 395 /** 396 * drop_nlink - directly drop an inode's link count 397 * @inode: inode 398 * 399 * This is a low-level filesystem helper to replace any 400 * direct filesystem manipulation of i_nlink. In cases 401 * where we are attempting to track writes to the 402 * filesystem, a decrement to zero means an imminent 403 * write when the file is truncated and actually unlinked 404 * on the filesystem. 405 */ 406 void drop_nlink(struct inode *inode) 407 { 408 WARN_ON(inode->i_nlink == 0); 409 inode->__i_nlink--; 410 if (!inode->i_nlink) 411 atomic_long_inc(&inode->i_sb->s_remove_count); 412 } 413 EXPORT_SYMBOL(drop_nlink); 414 415 /** 416 * clear_nlink - directly zero an inode's link count 417 * @inode: inode 418 * 419 * This is a low-level filesystem helper to replace any 420 * direct filesystem manipulation of i_nlink. See 421 * drop_nlink() for why we care about i_nlink hitting zero. 422 */ 423 void clear_nlink(struct inode *inode) 424 { 425 if (inode->i_nlink) { 426 inode->__i_nlink = 0; 427 atomic_long_inc(&inode->i_sb->s_remove_count); 428 } 429 } 430 EXPORT_SYMBOL(clear_nlink); 431 432 /** 433 * set_nlink - directly set an inode's link count 434 * @inode: inode 435 * @nlink: new nlink (should be non-zero) 436 * 437 * This is a low-level filesystem helper to replace any 438 * direct filesystem manipulation of i_nlink. 439 */ 440 void set_nlink(struct inode *inode, unsigned int nlink) 441 { 442 if (!nlink) { 443 clear_nlink(inode); 444 } else { 445 /* Yes, some filesystems do change nlink from zero to one */ 446 if (inode->i_nlink == 0) 447 atomic_long_dec(&inode->i_sb->s_remove_count); 448 449 inode->__i_nlink = nlink; 450 } 451 } 452 EXPORT_SYMBOL(set_nlink); 453 454 /** 455 * inc_nlink - directly increment an inode's link count 456 * @inode: inode 457 * 458 * This is a low-level filesystem helper to replace any 459 * direct filesystem manipulation of i_nlink. Currently, 460 * it is only here for parity with dec_nlink(). 461 */ 462 void inc_nlink(struct inode *inode) 463 { 464 if (unlikely(inode->i_nlink == 0)) { 465 WARN_ON(!(inode_state_read_once(inode) & I_LINKABLE)); 466 atomic_long_dec(&inode->i_sb->s_remove_count); 467 } 468 469 inode->__i_nlink++; 470 } 471 EXPORT_SYMBOL(inc_nlink); 472 473 static void __address_space_init_once(struct address_space *mapping) 474 { 475 xa_init_flags(&mapping->i_pages, XA_FLAGS_LOCK_IRQ | XA_FLAGS_ACCOUNT); 476 init_rwsem(&mapping->i_mmap_rwsem); 477 spin_lock_init(&mapping->i_private_lock); 478 mapping->i_mmap = RB_ROOT_CACHED; 479 } 480 481 void address_space_init_once(struct address_space *mapping) 482 { 483 memset(mapping, 0, sizeof(*mapping)); 484 __address_space_init_once(mapping); 485 } 486 EXPORT_SYMBOL(address_space_init_once); 487 488 /* 489 * These are initializations that only need to be done 490 * once, because the fields are idempotent across use 491 * of the inode, so let the slab aware of that. 492 */ 493 void inode_init_once(struct inode *inode) 494 { 495 memset(inode, 0, sizeof(*inode)); 496 INIT_HLIST_NODE(&inode->i_hash); 497 INIT_LIST_HEAD(&inode->i_devices); 498 INIT_LIST_HEAD(&inode->i_io_list); 499 INIT_LIST_HEAD(&inode->i_wb_list); 500 INIT_LIST_HEAD(&inode->i_lru); 501 INIT_LIST_HEAD(&inode->i_sb_list); 502 __address_space_init_once(&inode->i_data); 503 i_size_ordered_init(inode); 504 } 505 EXPORT_SYMBOL(inode_init_once); 506 507 static void init_once(void *foo) 508 { 509 struct inode *inode = (struct inode *) foo; 510 511 inode_init_once(inode); 512 } 513 514 struct wait_queue_head *inode_bit_waitqueue(struct wait_bit_queue_entry *wqe, 515 struct inode *inode, u32 bit) 516 { 517 void *bit_address; 518 519 bit_address = inode_state_wait_address(inode, bit); 520 init_wait_var_entry(wqe, bit_address, 0); 521 return __var_waitqueue(bit_address); 522 } 523 EXPORT_SYMBOL(inode_bit_waitqueue); 524 525 void wait_on_new_inode(struct inode *inode) 526 { 527 struct wait_bit_queue_entry wqe; 528 struct wait_queue_head *wq_head; 529 530 spin_lock(&inode->i_lock); 531 if (!(inode_state_read(inode) & I_NEW)) { 532 spin_unlock(&inode->i_lock); 533 return; 534 } 535 536 wq_head = inode_bit_waitqueue(&wqe, inode, __I_NEW); 537 for (;;) { 538 prepare_to_wait_event(wq_head, &wqe.wq_entry, TASK_UNINTERRUPTIBLE); 539 if (!(inode_state_read(inode) & I_NEW)) 540 break; 541 spin_unlock(&inode->i_lock); 542 schedule(); 543 spin_lock(&inode->i_lock); 544 } 545 finish_wait(wq_head, &wqe.wq_entry); 546 WARN_ON(inode_state_read(inode) & I_NEW); 547 spin_unlock(&inode->i_lock); 548 } 549 EXPORT_SYMBOL(wait_on_new_inode); 550 551 static void __inode_lru_list_add(struct inode *inode, bool rotate) 552 { 553 lockdep_assert_held(&inode->i_lock); 554 555 if (inode_state_read(inode) & (I_DIRTY_ALL | I_SYNC | I_FREEING | I_WILL_FREE)) 556 return; 557 if (icount_read(inode)) 558 return; 559 if (!(inode->i_sb->s_flags & SB_ACTIVE)) 560 return; 561 if (!mapping_shrinkable(&inode->i_data)) 562 return; 563 564 if (list_lru_add_obj(&inode->i_sb->s_inode_lru, &inode->i_lru)) 565 this_cpu_inc(nr_unused); 566 else if (rotate) 567 inode_state_set(inode, I_REFERENCED); 568 } 569 570 /* 571 * Add inode to LRU if needed (inode is unused and clean). 572 */ 573 void inode_lru_list_add(struct inode *inode) 574 { 575 __inode_lru_list_add(inode, false); 576 } 577 578 static void inode_lru_list_del(struct inode *inode) 579 { 580 if (list_empty(&inode->i_lru)) 581 return; 582 583 if (list_lru_del_obj(&inode->i_sb->s_inode_lru, &inode->i_lru)) 584 this_cpu_dec(nr_unused); 585 } 586 587 static void inode_pin_lru_isolating(struct inode *inode) 588 { 589 lockdep_assert_held(&inode->i_lock); 590 WARN_ON(inode_state_read(inode) & (I_LRU_ISOLATING | I_FREEING | I_WILL_FREE)); 591 inode_state_set(inode, I_LRU_ISOLATING); 592 } 593 594 static void inode_unpin_lru_isolating(struct inode *inode) 595 { 596 spin_lock(&inode->i_lock); 597 WARN_ON(!(inode_state_read(inode) & I_LRU_ISOLATING)); 598 inode_state_clear(inode, I_LRU_ISOLATING); 599 /* Called with inode->i_lock which ensures memory ordering. */ 600 inode_wake_up_bit(inode, __I_LRU_ISOLATING); 601 spin_unlock(&inode->i_lock); 602 } 603 604 static void inode_wait_for_lru_isolating(struct inode *inode) 605 { 606 struct wait_bit_queue_entry wqe; 607 struct wait_queue_head *wq_head; 608 609 lockdep_assert_held(&inode->i_lock); 610 if (!(inode_state_read(inode) & I_LRU_ISOLATING)) 611 return; 612 613 wq_head = inode_bit_waitqueue(&wqe, inode, __I_LRU_ISOLATING); 614 for (;;) { 615 prepare_to_wait_event(wq_head, &wqe.wq_entry, TASK_UNINTERRUPTIBLE); 616 /* 617 * Checking I_LRU_ISOLATING with inode->i_lock guarantees 618 * memory ordering. 619 */ 620 if (!(inode_state_read(inode) & I_LRU_ISOLATING)) 621 break; 622 spin_unlock(&inode->i_lock); 623 schedule(); 624 spin_lock(&inode->i_lock); 625 } 626 finish_wait(wq_head, &wqe.wq_entry); 627 WARN_ON(inode_state_read(inode) & I_LRU_ISOLATING); 628 } 629 630 /** 631 * inode_sb_list_add - add inode to the superblock list of inodes 632 * @inode: inode to add 633 */ 634 void inode_sb_list_add(struct inode *inode) 635 { 636 struct super_block *sb = inode->i_sb; 637 638 spin_lock(&sb->s_inode_list_lock); 639 list_add(&inode->i_sb_list, &sb->s_inodes); 640 spin_unlock(&sb->s_inode_list_lock); 641 } 642 EXPORT_SYMBOL_GPL(inode_sb_list_add); 643 644 static inline void inode_sb_list_del(struct inode *inode) 645 { 646 struct super_block *sb = inode->i_sb; 647 648 if (!list_empty(&inode->i_sb_list)) { 649 spin_lock(&sb->s_inode_list_lock); 650 list_del_init(&inode->i_sb_list); 651 spin_unlock(&sb->s_inode_list_lock); 652 } 653 } 654 655 static unsigned long hash(struct super_block *sb, u64 hashval) 656 { 657 unsigned long tmp; 658 659 tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) / 660 L1_CACHE_BYTES; 661 tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> i_hash_shift); 662 return tmp & i_hash_mask; 663 } 664 665 /** 666 * __insert_inode_hash - hash an inode 667 * @inode: unhashed inode 668 * @hashval: u64 value used to locate this object in the 669 * inode_hashtable. 670 * 671 * Add an inode to the inode hash for this superblock. 672 */ 673 void __insert_inode_hash(struct inode *inode, u64 hashval) 674 { 675 struct hlist_head *b = inode_hashtable + hash(inode->i_sb, hashval); 676 677 spin_lock(&inode_hash_lock); 678 spin_lock(&inode->i_lock); 679 hlist_add_head_rcu(&inode->i_hash, b); 680 spin_unlock(&inode->i_lock); 681 spin_unlock(&inode_hash_lock); 682 } 683 EXPORT_SYMBOL(__insert_inode_hash); 684 685 /** 686 * __remove_inode_hash - remove an inode from the hash 687 * @inode: inode to unhash 688 * 689 * Remove an inode from the superblock. 690 */ 691 void __remove_inode_hash(struct inode *inode) 692 { 693 spin_lock(&inode_hash_lock); 694 spin_lock(&inode->i_lock); 695 hlist_del_init_rcu(&inode->i_hash); 696 spin_unlock(&inode->i_lock); 697 spin_unlock(&inode_hash_lock); 698 } 699 EXPORT_SYMBOL(__remove_inode_hash); 700 701 void dump_mapping(const struct address_space *mapping) 702 { 703 struct inode *host; 704 const struct address_space_operations *a_ops; 705 struct hlist_node *dentry_first; 706 struct dentry *dentry_ptr; 707 struct dentry dentry; 708 char fname[64] = {}; 709 u64 ino; 710 711 /* 712 * If mapping is an invalid pointer, we don't want to crash 713 * accessing it, so probe everything depending on it carefully. 714 */ 715 if (get_kernel_nofault(host, &mapping->host) || 716 get_kernel_nofault(a_ops, &mapping->a_ops)) { 717 pr_warn("invalid mapping:%px\n", mapping); 718 return; 719 } 720 721 if (!host) { 722 pr_warn("aops:%ps\n", a_ops); 723 return; 724 } 725 726 if (get_kernel_nofault(dentry_first, &host->i_dentry.first) || 727 get_kernel_nofault(ino, &host->i_ino)) { 728 pr_warn("aops:%ps invalid inode:%px\n", a_ops, host); 729 return; 730 } 731 732 if (!dentry_first) { 733 pr_warn("aops:%ps ino:%llx\n", a_ops, ino); 734 return; 735 } 736 737 dentry_ptr = container_of(dentry_first, struct dentry, d_alias); 738 if (get_kernel_nofault(dentry, dentry_ptr) || 739 !dentry.d_parent || !dentry.d_name.name) { 740 pr_warn("aops:%ps ino:%llx invalid dentry:%px\n", 741 a_ops, ino, dentry_ptr); 742 return; 743 } 744 745 if (strncpy_from_kernel_nofault(fname, dentry.d_name.name, 63) < 0) 746 strscpy(fname, "<invalid>"); 747 /* 748 * Even if strncpy_from_kernel_nofault() succeeded, 749 * the fname could be unreliable 750 */ 751 pr_warn("aops:%ps ino:%llx dentry name(?):\"%s\"\n", 752 a_ops, ino, fname); 753 } 754 755 void clear_inode(struct inode *inode) 756 { 757 /* 758 * Only IS_VERITY() inodes can have verity info, so start by checking 759 * for IS_VERITY() (which is faster than retrieving the pointer to the 760 * verity info). This minimizes overhead for non-verity inodes. 761 */ 762 if (IS_ENABLED(CONFIG_FS_VERITY) && IS_VERITY(inode)) 763 fsverity_cleanup_inode(inode); 764 765 /* 766 * We have to cycle the i_pages lock here because reclaim 767 * can be in the process of removing the last page (in 768 * __filemap_remove_folio()) and we must not free the mapping 769 * under it. We also remove nodes which are empty; these 770 * can occur in two different ways. The first is that radix 771 * tree expansion can fail partway and the second is that THP 772 * collapse_file() can allocate some temporary nodes and not 773 * clean them up. 774 */ 775 xa_destroy(&inode->i_data.i_pages); 776 777 BUG_ON(inode->i_data.nrpages); 778 BUG_ON(!(inode_state_read_once(inode) & I_FREEING)); 779 BUG_ON(inode_state_read_once(inode) & I_CLEAR); 780 BUG_ON(!list_empty(&inode->i_wb_list)); 781 /* don't need i_lock here, no concurrent mods to i_state */ 782 inode_state_assign_raw(inode, I_FREEING | I_CLEAR); 783 } 784 EXPORT_SYMBOL(clear_inode); 785 786 /* 787 * Free the inode passed in, removing it from the lists it is still connected 788 * to. We remove any pages still attached to the inode and wait for any IO that 789 * is still in progress before finally destroying the inode. 790 * 791 * An inode must already be marked I_FREEING so that we avoid the inode being 792 * moved back onto lists if we race with other code that manipulates the lists 793 * (e.g. writeback_single_inode). The caller is responsible for setting this. 794 * 795 * An inode must already be removed from the LRU list before being evicted from 796 * the cache. This should occur atomically with setting the I_FREEING state 797 * flag, so no inodes here should ever be on the LRU when being evicted. 798 */ 799 static void evict(struct inode *inode) 800 { 801 const struct super_operations *op = inode->i_sb->s_op; 802 803 BUG_ON(!(inode_state_read_once(inode) & I_FREEING)); 804 BUG_ON(!list_empty(&inode->i_lru)); 805 806 inode_io_list_del(inode); 807 inode_sb_list_del(inode); 808 809 spin_lock(&inode->i_lock); 810 inode_wait_for_lru_isolating(inode); 811 812 /* 813 * Wait for flusher thread to be done with the inode so that filesystem 814 * does not start destroying it while writeback is still running. Since 815 * the inode has I_FREEING set, flusher thread won't start new work on 816 * the inode. We just have to wait for running writeback to finish. 817 */ 818 inode_wait_for_writeback(inode); 819 spin_unlock(&inode->i_lock); 820 821 if (op->evict_inode) { 822 op->evict_inode(inode); 823 } else { 824 truncate_inode_pages_final(&inode->i_data); 825 clear_inode(inode); 826 } 827 if (S_ISCHR(inode->i_mode) && inode->i_cdev) 828 cd_forget(inode); 829 830 remove_inode_hash(inode); 831 832 /* 833 * Wake up waiters in __wait_on_freeing_inode(). 834 * 835 * It is an invariant that any thread we need to wake up is already 836 * accounted for before remove_inode_hash() acquires ->i_lock -- both 837 * sides take the lock and sleep is aborted if the inode is found 838 * unhashed. Thus either the sleeper wins and goes off CPU, or removal 839 * wins and the sleeper aborts after testing with the lock. 840 * 841 * This also means we don't need any fences for the call below. 842 */ 843 inode_wake_up_bit(inode, __I_NEW); 844 BUG_ON(inode_state_read_once(inode) != (I_FREEING | I_CLEAR)); 845 846 destroy_inode(inode); 847 } 848 849 /* 850 * dispose_list - dispose of the contents of a local list 851 * @head: the head of the list to free 852 * 853 * Dispose-list gets a local list with local inodes in it, so it doesn't 854 * need to worry about list corruption and SMP locks. 855 */ 856 static void dispose_list(struct list_head *head) 857 { 858 while (!list_empty(head)) { 859 struct inode *inode; 860 861 inode = list_first_entry(head, struct inode, i_lru); 862 list_del_init(&inode->i_lru); 863 864 evict(inode); 865 cond_resched(); 866 } 867 } 868 869 /** 870 * evict_inodes - evict all evictable inodes for a superblock 871 * @sb: superblock to operate on 872 * 873 * Make sure that no inodes with zero refcount are retained. This is 874 * called by superblock shutdown after having SB_ACTIVE flag removed, 875 * so any inode reaching zero refcount during or after that call will 876 * be immediately evicted. 877 */ 878 void evict_inodes(struct super_block *sb) 879 { 880 struct inode *inode; 881 LIST_HEAD(dispose); 882 883 again: 884 spin_lock(&sb->s_inode_list_lock); 885 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) { 886 if (icount_read_once(inode)) 887 continue; 888 889 spin_lock(&inode->i_lock); 890 if (icount_read(inode)) { 891 spin_unlock(&inode->i_lock); 892 continue; 893 } 894 if (inode_state_read(inode) & (I_NEW | I_FREEING | I_WILL_FREE)) { 895 spin_unlock(&inode->i_lock); 896 continue; 897 } 898 899 inode_state_set(inode, I_FREEING); 900 inode_lru_list_del(inode); 901 spin_unlock(&inode->i_lock); 902 list_add(&inode->i_lru, &dispose); 903 904 /* 905 * We can have a ton of inodes to evict at unmount time given 906 * enough memory, check to see if we need to go to sleep for a 907 * bit so we don't livelock. 908 */ 909 if (need_resched()) { 910 spin_unlock(&sb->s_inode_list_lock); 911 cond_resched(); 912 dispose_list(&dispose); 913 goto again; 914 } 915 } 916 spin_unlock(&sb->s_inode_list_lock); 917 918 dispose_list(&dispose); 919 } 920 EXPORT_SYMBOL_GPL(evict_inodes); 921 922 /* 923 * Isolate the inode from the LRU in preparation for freeing it. 924 * 925 * If the inode has the I_REFERENCED flag set, then it means that it has been 926 * used recently - the flag is set in iput_final(). When we encounter such an 927 * inode, clear the flag and move it to the back of the LRU so it gets another 928 * pass through the LRU before it gets reclaimed. This is necessary because of 929 * the fact we are doing lazy LRU updates to minimise lock contention so the 930 * LRU does not have strict ordering. Hence we don't want to reclaim inodes 931 * with this flag set because they are the inodes that are out of order. 932 */ 933 static enum lru_status inode_lru_isolate(struct list_head *item, 934 struct list_lru_one *lru, void *arg) 935 { 936 struct list_head *freeable = arg; 937 struct inode *inode = container_of(item, struct inode, i_lru); 938 939 /* 940 * We are inverting the lru lock/inode->i_lock here, so use a 941 * trylock. If we fail to get the lock, just skip it. 942 */ 943 if (!spin_trylock(&inode->i_lock)) 944 return LRU_SKIP; 945 946 /* 947 * Inodes can get referenced, redirtied, or repopulated while 948 * they're already on the LRU, and this can make them 949 * unreclaimable for a while. Remove them lazily here; iput, 950 * sync, or the last page cache deletion will requeue them. 951 */ 952 if (icount_read(inode) || 953 (inode_state_read(inode) & ~I_REFERENCED) || 954 !mapping_shrinkable(&inode->i_data)) { 955 list_lru_isolate(lru, &inode->i_lru); 956 spin_unlock(&inode->i_lock); 957 this_cpu_dec(nr_unused); 958 return LRU_REMOVED; 959 } 960 961 /* Recently referenced inodes get one more pass */ 962 if (inode_state_read(inode) & I_REFERENCED) { 963 inode_state_clear(inode, I_REFERENCED); 964 spin_unlock(&inode->i_lock); 965 return LRU_ROTATE; 966 } 967 968 /* 969 * On highmem systems, mapping_shrinkable() permits dropping 970 * page cache in order to free up struct inodes: lowmem might 971 * be under pressure before the cache inside the highmem zone. 972 */ 973 if (!mapping_empty(&inode->i_data)) { 974 unsigned long reap; 975 976 inode_pin_lru_isolating(inode); 977 spin_unlock(&inode->i_lock); 978 spin_unlock(&lru->lock); 979 reap = invalidate_mapping_pages(&inode->i_data, 0, -1); 980 if (current_is_kswapd()) 981 __count_vm_events(KSWAPD_INODESTEAL, reap); 982 else 983 __count_vm_events(PGINODESTEAL, reap); 984 mm_account_reclaimed_pages(reap); 985 inode_unpin_lru_isolating(inode); 986 return LRU_RETRY; 987 } 988 989 WARN_ON(inode_state_read(inode) & I_NEW); 990 inode_state_set(inode, I_FREEING); 991 list_lru_isolate_move(lru, &inode->i_lru, freeable); 992 spin_unlock(&inode->i_lock); 993 994 this_cpu_dec(nr_unused); 995 return LRU_REMOVED; 996 } 997 998 /* 999 * Walk the superblock inode LRU for freeable inodes and attempt to free them. 1000 * This is called from the superblock shrinker function with a number of inodes 1001 * to trim from the LRU. Inodes to be freed are moved to a temporary list and 1002 * then are freed outside inode_lock by dispose_list(). 1003 */ 1004 long prune_icache_sb(struct super_block *sb, struct shrink_control *sc) 1005 { 1006 LIST_HEAD(freeable); 1007 long freed; 1008 1009 freed = list_lru_shrink_walk(&sb->s_inode_lru, sc, 1010 inode_lru_isolate, &freeable); 1011 dispose_list(&freeable); 1012 return freed; 1013 } 1014 1015 static void __wait_on_freeing_inode(struct inode *inode, bool hash_locked, bool rcu_locked); 1016 static bool igrab_from_hash(struct inode *inode); 1017 1018 /* 1019 * Called with the inode lock held. 1020 */ 1021 static struct inode *find_inode(struct super_block *sb, 1022 struct hlist_head *head, 1023 int (*test)(struct inode *, void *), 1024 void *data, bool hash_locked, 1025 bool *isnew) 1026 { 1027 struct inode *inode = NULL; 1028 1029 if (hash_locked) 1030 lockdep_assert_held(&inode_hash_lock); 1031 else 1032 lockdep_assert_not_held(&inode_hash_lock); 1033 1034 rcu_read_lock(); 1035 repeat: 1036 hlist_for_each_entry_rcu(inode, head, i_hash) { 1037 if (inode->i_sb != sb) 1038 continue; 1039 if (!test(inode, data)) 1040 continue; 1041 if (igrab_from_hash(inode)) { 1042 rcu_read_unlock(); 1043 *isnew = false; 1044 return inode; 1045 } 1046 spin_lock(&inode->i_lock); 1047 if (inode_state_read(inode) & (I_FREEING | I_WILL_FREE)) { 1048 __wait_on_freeing_inode(inode, hash_locked, true); 1049 goto repeat; 1050 } 1051 if (unlikely(inode_state_read(inode) & I_CREATING)) { 1052 spin_unlock(&inode->i_lock); 1053 rcu_read_unlock(); 1054 return ERR_PTR(-ESTALE); 1055 } 1056 __iget(inode); 1057 *isnew = !!(inode_state_read(inode) & I_NEW); 1058 spin_unlock(&inode->i_lock); 1059 rcu_read_unlock(); 1060 return inode; 1061 } 1062 rcu_read_unlock(); 1063 return NULL; 1064 } 1065 1066 /* 1067 * find_inode_fast is the fast path version of find_inode, see the comment at 1068 * iget_locked for details. 1069 */ 1070 static struct inode *find_inode_fast(struct super_block *sb, 1071 struct hlist_head *head, u64 ino, 1072 bool hash_locked, bool *isnew) 1073 { 1074 struct inode *inode = NULL; 1075 1076 if (hash_locked) 1077 lockdep_assert_held(&inode_hash_lock); 1078 else 1079 lockdep_assert_not_held(&inode_hash_lock); 1080 1081 rcu_read_lock(); 1082 repeat: 1083 hlist_for_each_entry_rcu(inode, head, i_hash) { 1084 if (inode->i_ino != ino) 1085 continue; 1086 if (inode->i_sb != sb) 1087 continue; 1088 if (igrab_from_hash(inode)) { 1089 rcu_read_unlock(); 1090 *isnew = false; 1091 return inode; 1092 } 1093 spin_lock(&inode->i_lock); 1094 if (inode_state_read(inode) & (I_FREEING | I_WILL_FREE)) { 1095 __wait_on_freeing_inode(inode, hash_locked, true); 1096 goto repeat; 1097 } 1098 if (unlikely(inode_state_read(inode) & I_CREATING)) { 1099 spin_unlock(&inode->i_lock); 1100 rcu_read_unlock(); 1101 return ERR_PTR(-ESTALE); 1102 } 1103 __iget(inode); 1104 *isnew = !!(inode_state_read(inode) & I_NEW); 1105 spin_unlock(&inode->i_lock); 1106 rcu_read_unlock(); 1107 return inode; 1108 } 1109 rcu_read_unlock(); 1110 return NULL; 1111 } 1112 1113 /* 1114 * Each cpu owns a range of LAST_INO_BATCH numbers. 1115 * 'shared_last_ino' is dirtied only once out of LAST_INO_BATCH allocations, 1116 * to renew the exhausted range. 1117 * 1118 * This does not significantly increase overflow rate because every CPU can 1119 * consume at most LAST_INO_BATCH-1 unused inode numbers. So there is 1120 * NR_CPUS*(LAST_INO_BATCH-1) wastage. At 4096 and 1024, this is ~0.1% of the 1121 * 2^32 range, and is a worst-case. Even a 50% wastage would only increase 1122 * overflow rate by 2x, which does not seem too significant. 1123 * 1124 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW 1125 * error if st_ino won't fit in target struct field. Use 32bit counter 1126 * here to attempt to avoid that. 1127 */ 1128 #define LAST_INO_BATCH 1024 1129 static DEFINE_PER_CPU(unsigned int, last_ino); 1130 1131 unsigned int get_next_ino(void) 1132 { 1133 unsigned int *p = &get_cpu_var(last_ino); 1134 unsigned int res = *p; 1135 1136 #ifdef CONFIG_SMP 1137 if (unlikely((res & (LAST_INO_BATCH-1)) == 0)) { 1138 static atomic_t shared_last_ino; 1139 int next = atomic_add_return(LAST_INO_BATCH, &shared_last_ino); 1140 1141 res = next - LAST_INO_BATCH; 1142 } 1143 #endif 1144 1145 res++; 1146 /* get_next_ino should not provide a 0 inode number */ 1147 if (unlikely(!res)) 1148 res++; 1149 *p = res; 1150 put_cpu_var(last_ino); 1151 return res; 1152 } 1153 EXPORT_SYMBOL(get_next_ino); 1154 1155 /** 1156 * new_inode - obtain an inode 1157 * @sb: superblock 1158 * 1159 * Allocates a new inode for given superblock. The default gfp_mask 1160 * for allocations related to inode->i_mapping is GFP_HIGHUSER_MOVABLE. 1161 * If HIGHMEM pages are unsuitable or it is known that pages allocated 1162 * for the page cache are not reclaimable or migratable, 1163 * mapping_set_gfp_mask() must be called with suitable flags on the 1164 * newly created inode's mapping 1165 * 1166 */ 1167 struct inode *new_inode(struct super_block *sb) 1168 { 1169 struct inode *inode; 1170 1171 inode = alloc_inode(sb); 1172 if (inode) 1173 inode_sb_list_add(inode); 1174 return inode; 1175 } 1176 EXPORT_SYMBOL(new_inode); 1177 1178 #ifdef CONFIG_DEBUG_LOCK_ALLOC 1179 void lockdep_annotate_inode_mutex_key(struct inode *inode) 1180 { 1181 if (S_ISDIR(inode->i_mode)) { 1182 struct file_system_type *type = inode->i_sb->s_type; 1183 1184 /* Set new key only if filesystem hasn't already changed it */ 1185 if (lockdep_match_class(&inode->i_rwsem, &type->i_mutex_key)) { 1186 /* 1187 * ensure nobody is actually holding i_rwsem 1188 */ 1189 init_rwsem(&inode->i_rwsem); 1190 lockdep_set_class(&inode->i_rwsem, 1191 &type->i_mutex_dir_key); 1192 } 1193 } 1194 } 1195 EXPORT_SYMBOL(lockdep_annotate_inode_mutex_key); 1196 #endif 1197 1198 /** 1199 * unlock_new_inode - clear the I_NEW state and wake up any waiters 1200 * @inode: new inode to unlock 1201 * 1202 * Called when the inode is fully initialised to clear the new state of the 1203 * inode and wake up anyone waiting for the inode to finish initialisation. 1204 */ 1205 void unlock_new_inode(struct inode *inode) 1206 { 1207 lockdep_annotate_inode_mutex_key(inode); 1208 spin_lock(&inode->i_lock); 1209 WARN_ON(!(inode_state_read(inode) & I_NEW)); 1210 /* 1211 * Paired with igrab_from_hash() 1212 */ 1213 smp_wmb(); 1214 inode_state_clear(inode, I_NEW | I_CREATING); 1215 inode_wake_up_bit(inode, __I_NEW); 1216 spin_unlock(&inode->i_lock); 1217 } 1218 EXPORT_SYMBOL(unlock_new_inode); 1219 1220 void discard_new_inode(struct inode *inode) 1221 { 1222 lockdep_annotate_inode_mutex_key(inode); 1223 spin_lock(&inode->i_lock); 1224 WARN_ON(!(inode_state_read(inode) & I_NEW)); 1225 /* 1226 * Paired with igrab_from_hash() 1227 */ 1228 smp_wmb(); 1229 inode_state_clear(inode, I_NEW); 1230 inode_wake_up_bit(inode, __I_NEW); 1231 spin_unlock(&inode->i_lock); 1232 iput(inode); 1233 } 1234 EXPORT_SYMBOL(discard_new_inode); 1235 1236 /** 1237 * lock_two_nondirectories - take two i_mutexes on non-directory objects 1238 * 1239 * Lock any non-NULL argument. Passed objects must not be directories. 1240 * Zero, one or two objects may be locked by this function. 1241 * 1242 * @inode1: first inode to lock 1243 * @inode2: second inode to lock 1244 */ 1245 void lock_two_nondirectories(struct inode *inode1, struct inode *inode2) 1246 { 1247 if (inode1) 1248 WARN_ON_ONCE(S_ISDIR(inode1->i_mode)); 1249 if (inode2) 1250 WARN_ON_ONCE(S_ISDIR(inode2->i_mode)); 1251 if (inode1 > inode2) 1252 swap(inode1, inode2); 1253 if (inode1) 1254 inode_lock(inode1); 1255 if (inode2 && inode2 != inode1) 1256 inode_lock_nested(inode2, I_MUTEX_NONDIR2); 1257 } 1258 EXPORT_SYMBOL(lock_two_nondirectories); 1259 1260 /** 1261 * unlock_two_nondirectories - release locks from lock_two_nondirectories() 1262 * @inode1: first inode to unlock 1263 * @inode2: second inode to unlock 1264 */ 1265 void unlock_two_nondirectories(struct inode *inode1, struct inode *inode2) 1266 { 1267 if (inode1) { 1268 WARN_ON_ONCE(S_ISDIR(inode1->i_mode)); 1269 inode_unlock(inode1); 1270 } 1271 if (inode2 && inode2 != inode1) { 1272 WARN_ON_ONCE(S_ISDIR(inode2->i_mode)); 1273 inode_unlock(inode2); 1274 } 1275 } 1276 EXPORT_SYMBOL(unlock_two_nondirectories); 1277 1278 /** 1279 * inode_insert5 - obtain an inode from a mounted file system 1280 * @inode: pre-allocated inode to use for insert to cache 1281 * @hashval: hash value (usually inode number) to get 1282 * @test: callback used for comparisons between inodes 1283 * @set: callback used to initialize a new struct inode 1284 * @data: opaque data pointer to pass to @test and @set 1285 * 1286 * Search for the inode specified by @hashval and @data in the inode cache, 1287 * and if present return it with an increased reference count. This is a 1288 * variant of iget5_locked() that doesn't allocate an inode. 1289 * 1290 * If the inode is not present in the cache, insert the pre-allocated inode and 1291 * return it locked, hashed, and with the I_NEW flag set. The file system gets 1292 * to fill it in before unlocking it via unlock_new_inode(). 1293 * 1294 * Note that both @test and @set are called with the inode_hash_lock held, so 1295 * they can't sleep. 1296 */ 1297 struct inode *inode_insert5(struct inode *inode, u64 hashval, 1298 int (*test)(struct inode *, void *), 1299 int (*set)(struct inode *, void *), void *data) 1300 { 1301 struct hlist_head *head = inode_hashtable + hash(inode->i_sb, hashval); 1302 struct inode *old; 1303 bool isnew; 1304 1305 might_sleep(); 1306 1307 again: 1308 spin_lock(&inode_hash_lock); 1309 old = find_inode(inode->i_sb, head, test, data, true, &isnew); 1310 if (unlikely(old)) { 1311 /* 1312 * Uhhuh, somebody else created the same inode under us. 1313 * Use the old inode instead of the preallocated one. 1314 */ 1315 spin_unlock(&inode_hash_lock); 1316 if (IS_ERR(old)) 1317 return NULL; 1318 if (unlikely(isnew)) 1319 wait_on_new_inode(old); 1320 if (unlikely(inode_unhashed(old))) { 1321 iput(old); 1322 goto again; 1323 } 1324 return old; 1325 } 1326 1327 if (set && unlikely(set(inode, data))) { 1328 spin_unlock(&inode_hash_lock); 1329 return NULL; 1330 } 1331 1332 /* 1333 * Return the locked inode with I_NEW set, the 1334 * caller is responsible for filling in the contents 1335 */ 1336 spin_lock(&inode->i_lock); 1337 inode_state_set(inode, I_NEW); 1338 hlist_add_head_rcu(&inode->i_hash, head); 1339 spin_unlock(&inode->i_lock); 1340 1341 spin_unlock(&inode_hash_lock); 1342 1343 /* 1344 * Add inode to the sb list if it's not already. It has I_NEW at this 1345 * point, so it should be safe to test i_sb_list locklessly. 1346 */ 1347 if (list_empty(&inode->i_sb_list)) 1348 inode_sb_list_add(inode); 1349 1350 return inode; 1351 } 1352 EXPORT_SYMBOL(inode_insert5); 1353 1354 /** 1355 * iget5_locked - obtain an inode from a mounted file system 1356 * @sb: super block of file system 1357 * @hashval: hash value (usually inode number) to get 1358 * @test: callback used for comparisons between inodes 1359 * @set: callback used to initialize a new struct inode 1360 * @data: opaque data pointer to pass to @test and @set 1361 * 1362 * Search for the inode specified by @hashval and @data in the inode cache, 1363 * and if present return it with an increased reference count. This is a 1364 * generalized version of iget_locked() for file systems where the inode 1365 * number is not sufficient for unique identification of an inode. 1366 * 1367 * If the inode is not present in the cache, allocate and insert a new inode 1368 * and return it locked, hashed, and with the I_NEW flag set. The file system 1369 * gets to fill it in before unlocking it via unlock_new_inode(). 1370 * 1371 * Note that both @test and @set are called with the inode_hash_lock held, so 1372 * they can't sleep. 1373 */ 1374 struct inode *iget5_locked(struct super_block *sb, u64 hashval, 1375 int (*test)(struct inode *, void *), 1376 int (*set)(struct inode *, void *), void *data) 1377 { 1378 struct inode *inode = ilookup5(sb, hashval, test, data); 1379 1380 if (!inode) { 1381 struct inode *new = alloc_inode(sb); 1382 1383 if (new) { 1384 inode = inode_insert5(new, hashval, test, set, data); 1385 if (unlikely(inode != new)) 1386 destroy_inode(new); 1387 } 1388 } 1389 return inode; 1390 } 1391 EXPORT_SYMBOL(iget5_locked); 1392 1393 /** 1394 * iget5_locked_rcu - obtain an inode from a mounted file system 1395 * @sb: super block of file system 1396 * @hashval: hash value (usually inode number) to get 1397 * @test: callback used for comparisons between inodes 1398 * @set: callback used to initialize a new struct inode 1399 * @data: opaque data pointer to pass to @test and @set 1400 * 1401 * This is equivalent to iget5_locked, except the @test callback must 1402 * tolerate the inode not being stable, including being mid-teardown. 1403 */ 1404 struct inode *iget5_locked_rcu(struct super_block *sb, u64 hashval, 1405 int (*test)(struct inode *, void *), 1406 int (*set)(struct inode *, void *), void *data) 1407 { 1408 struct hlist_head *head = inode_hashtable + hash(sb, hashval); 1409 struct inode *inode, *new; 1410 bool isnew; 1411 1412 might_sleep(); 1413 1414 again: 1415 inode = find_inode(sb, head, test, data, false, &isnew); 1416 if (inode) { 1417 if (IS_ERR(inode)) 1418 return NULL; 1419 if (unlikely(isnew)) 1420 wait_on_new_inode(inode); 1421 if (unlikely(inode_unhashed(inode))) { 1422 iput(inode); 1423 goto again; 1424 } 1425 return inode; 1426 } 1427 1428 new = alloc_inode(sb); 1429 if (new) { 1430 inode = inode_insert5(new, hashval, test, set, data); 1431 if (unlikely(inode != new)) 1432 destroy_inode(new); 1433 } 1434 return inode; 1435 } 1436 EXPORT_SYMBOL_GPL(iget5_locked_rcu); 1437 1438 /** 1439 * iget_locked - obtain an inode from a mounted file system 1440 * @sb: super block of file system 1441 * @ino: inode number to get 1442 * 1443 * Search for the inode specified by @ino in the inode cache and if present 1444 * return it with an increased reference count. This is for file systems 1445 * where the inode number is sufficient for unique identification of an inode. 1446 * 1447 * If the inode is not in cache, allocate a new inode and return it locked, 1448 * hashed, and with the I_NEW flag set. The file system gets to fill it in 1449 * before unlocking it via unlock_new_inode(). 1450 */ 1451 struct inode *iget_locked(struct super_block *sb, u64 ino) 1452 { 1453 struct hlist_head *head = inode_hashtable + hash(sb, ino); 1454 struct inode *inode; 1455 bool isnew; 1456 1457 might_sleep(); 1458 1459 again: 1460 inode = find_inode_fast(sb, head, ino, false, &isnew); 1461 if (inode) { 1462 if (IS_ERR(inode)) 1463 return NULL; 1464 if (unlikely(isnew)) 1465 wait_on_new_inode(inode); 1466 if (unlikely(inode_unhashed(inode))) { 1467 iput(inode); 1468 goto again; 1469 } 1470 return inode; 1471 } 1472 1473 inode = alloc_inode(sb); 1474 if (inode) { 1475 struct inode *old; 1476 1477 spin_lock(&inode_hash_lock); 1478 /* We released the lock, so.. */ 1479 old = find_inode_fast(sb, head, ino, true, &isnew); 1480 if (!old) { 1481 inode->i_ino = ino; 1482 spin_lock(&inode->i_lock); 1483 inode_state_assign(inode, I_NEW); 1484 hlist_add_head_rcu(&inode->i_hash, head); 1485 spin_unlock(&inode->i_lock); 1486 spin_unlock(&inode_hash_lock); 1487 inode_sb_list_add(inode); 1488 1489 /* Return the locked inode with I_NEW set, the 1490 * caller is responsible for filling in the contents 1491 */ 1492 return inode; 1493 } 1494 1495 /* 1496 * Uhhuh, somebody else created the same inode under 1497 * us. Use the old inode instead of the one we just 1498 * allocated. 1499 */ 1500 spin_unlock(&inode_hash_lock); 1501 destroy_inode(inode); 1502 if (IS_ERR(old)) 1503 return NULL; 1504 inode = old; 1505 if (unlikely(isnew)) 1506 wait_on_new_inode(inode); 1507 if (unlikely(inode_unhashed(inode))) { 1508 iput(inode); 1509 goto again; 1510 } 1511 } 1512 return inode; 1513 } 1514 EXPORT_SYMBOL(iget_locked); 1515 1516 /* 1517 * search the inode cache for a matching inode number. 1518 * If we find one, then the inode number we are trying to 1519 * allocate is not unique and so we should not use it. 1520 * 1521 * Returns 1 if the inode number is unique, 0 if it is not. 1522 */ 1523 static int test_inode_iunique(struct super_block *sb, u64 ino) 1524 { 1525 struct hlist_head *b = inode_hashtable + hash(sb, ino); 1526 struct inode *inode; 1527 1528 hlist_for_each_entry_rcu(inode, b, i_hash) { 1529 if (inode->i_ino == ino && inode->i_sb == sb) 1530 return 0; 1531 } 1532 return 1; 1533 } 1534 1535 /** 1536 * iunique - get a unique inode number 1537 * @sb: superblock 1538 * @max_reserved: highest reserved inode number 1539 * 1540 * Obtain an inode number that is unique on the system for a given 1541 * superblock. This is used by file systems that have no natural 1542 * permanent inode numbering system. An inode number is returned that 1543 * is higher than the reserved limit but unique. 1544 * 1545 * BUGS: 1546 * With a large number of inodes live on the file system this function 1547 * currently becomes quite slow. 1548 */ 1549 ino_t iunique(struct super_block *sb, ino_t max_reserved) 1550 { 1551 /* 1552 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW 1553 * error if st_ino won't fit in target struct field. Use 32bit counter 1554 * here to attempt to avoid that. 1555 */ 1556 static DEFINE_SPINLOCK(iunique_lock); 1557 static unsigned int counter; 1558 ino_t res; 1559 1560 rcu_read_lock(); 1561 spin_lock(&iunique_lock); 1562 do { 1563 if (counter <= max_reserved) 1564 counter = max_reserved + 1; 1565 res = counter++; 1566 } while (!test_inode_iunique(sb, res)); 1567 spin_unlock(&iunique_lock); 1568 rcu_read_unlock(); 1569 1570 return res; 1571 } 1572 EXPORT_SYMBOL(iunique); 1573 1574 /** 1575 * ihold - get a reference on the inode, provided you already have one 1576 * @inode: inode to operate on 1577 */ 1578 void ihold(struct inode *inode) 1579 { 1580 VFS_BUG_ON_INODE(icount_read_once(inode) < 1, inode); 1581 WARN_ON(atomic_inc_return(&inode->i_count) < 2); 1582 } 1583 EXPORT_SYMBOL(ihold); 1584 1585 struct inode *igrab(struct inode *inode) 1586 { 1587 /* 1588 * Read commentary above igrab_from_hash() for an explanation why this works. 1589 */ 1590 if (atomic_add_unless(&inode->i_count, 1, 0)) { 1591 VFS_BUG_ON_INODE(inode_state_read_once(inode) & (I_FREEING | I_WILL_FREE), inode); 1592 return inode; 1593 } 1594 1595 spin_lock(&inode->i_lock); 1596 if (!(inode_state_read(inode) & (I_FREEING | I_WILL_FREE))) { 1597 __iget(inode); 1598 spin_unlock(&inode->i_lock); 1599 } else { 1600 spin_unlock(&inode->i_lock); 1601 /* 1602 * Handle the case where s_op->clear_inode is not been 1603 * called yet, and somebody is calling igrab 1604 * while the inode is getting freed. 1605 */ 1606 inode = NULL; 1607 } 1608 return inode; 1609 } 1610 EXPORT_SYMBOL(igrab); 1611 1612 /* 1613 * igrab_from_hash - special inode refcount acquire primitive for the inode hash 1614 * 1615 * It provides lockless refcount acquire in the common case of no problematic 1616 * flags being set and the count being > 0. 1617 * 1618 * There are 4 state flags to worry about and the routine makes sure to not bump the 1619 * ref if any of them is present. 1620 * 1621 * I_NEW and I_CREATING can only legally get set *before* the inode becomes visible 1622 * during lookup. Thus if the flags are not spotted, they are guaranteed to not be 1623 * a factor. However, we need an acquire fence before returning the inode just 1624 * in case we raced against clearing the state to make sure our consumer picks up 1625 * any other changes made prior. atomic_add_unless provides a full fence, which 1626 * takes care of it. 1627 * 1628 * I_FREEING and I_WILL_FREE can only legally get set if ->i_count == 0 and it is 1629 * illegal to bump the ref if either is present. Consequently if atomic_add_unless 1630 * managed to replace a non-0 value with a bigger one, we have a guarantee neither 1631 * of these flags is set. Note this means explicitly checking of these flags below 1632 * is not necessary, it is only done because it does not cost anything on top of the 1633 * load which already needs to be done to handle the other flags. 1634 */ 1635 static bool igrab_from_hash(struct inode *inode) 1636 { 1637 if (inode_state_read_once(inode) & (I_NEW | I_CREATING | I_FREEING | I_WILL_FREE)) 1638 return false; 1639 /* 1640 * Paired with routines clearing I_NEW 1641 */ 1642 if (atomic_add_unless(&inode->i_count, 1, 0)) { 1643 VFS_BUG_ON_INODE(inode_state_read_once(inode) & (I_FREEING | I_WILL_FREE), inode); 1644 return true; 1645 } 1646 return false; 1647 } 1648 1649 /** 1650 * ilookup5_nowait - search for an inode in the inode cache 1651 * @sb: super block of file system to search 1652 * @hashval: hash value (usually inode number) to search for 1653 * @test: callback used for comparisons between inodes 1654 * @data: opaque data pointer to pass to @test 1655 * @isnew: return argument telling whether I_NEW was set when 1656 * the inode was found in hash (the caller needs to 1657 * wait for I_NEW to clear) 1658 * 1659 * Search for the inode specified by @hashval and @data in the inode cache. 1660 * If the inode is in the cache, the inode is returned with an incremented 1661 * reference count. 1662 * 1663 * Note: I_NEW is not waited upon so you have to be very careful what you do 1664 * with the returned inode. You probably should be using ilookup5() instead. 1665 * 1666 * Note2: @test is called with the inode_hash_lock held, so can't sleep. 1667 */ 1668 struct inode *ilookup5_nowait(struct super_block *sb, u64 hashval, 1669 int (*test)(struct inode *, void *), void *data, bool *isnew) 1670 { 1671 struct hlist_head *head = inode_hashtable + hash(sb, hashval); 1672 struct inode *inode; 1673 1674 spin_lock(&inode_hash_lock); 1675 inode = find_inode(sb, head, test, data, true, isnew); 1676 spin_unlock(&inode_hash_lock); 1677 1678 return IS_ERR(inode) ? NULL : inode; 1679 } 1680 EXPORT_SYMBOL(ilookup5_nowait); 1681 1682 /** 1683 * ilookup5 - search for an inode in the inode cache 1684 * @sb: super block of file system to search 1685 * @hashval: hash value (usually inode number) to search for 1686 * @test: callback used for comparisons between inodes 1687 * @data: opaque data pointer to pass to @test 1688 * 1689 * Search for the inode specified by @hashval and @data in the inode cache, 1690 * and if the inode is in the cache, return the inode with an incremented 1691 * reference count. Waits on I_NEW before returning the inode. 1692 * returned with an incremented reference count. 1693 * 1694 * This is a generalized version of ilookup() for file systems where the 1695 * inode number is not sufficient for unique identification of an inode. 1696 * 1697 * Note: @test is called with the inode_hash_lock held, so can't sleep. 1698 */ 1699 struct inode *ilookup5(struct super_block *sb, u64 hashval, 1700 int (*test)(struct inode *, void *), void *data) 1701 { 1702 struct inode *inode; 1703 bool isnew; 1704 1705 might_sleep(); 1706 1707 again: 1708 inode = ilookup5_nowait(sb, hashval, test, data, &isnew); 1709 if (inode) { 1710 if (unlikely(isnew)) 1711 wait_on_new_inode(inode); 1712 if (unlikely(inode_unhashed(inode))) { 1713 iput(inode); 1714 goto again; 1715 } 1716 } 1717 return inode; 1718 } 1719 EXPORT_SYMBOL(ilookup5); 1720 1721 /** 1722 * ilookup - search for an inode in the inode cache 1723 * @sb: super block of file system to search 1724 * @ino: inode number to search for 1725 * 1726 * Search for the inode @ino in the inode cache, and if the inode is in the 1727 * cache, the inode is returned with an incremented reference count. 1728 */ 1729 struct inode *ilookup(struct super_block *sb, u64 ino) 1730 { 1731 struct hlist_head *head = inode_hashtable + hash(sb, ino); 1732 struct inode *inode; 1733 bool isnew; 1734 1735 might_sleep(); 1736 1737 again: 1738 inode = find_inode_fast(sb, head, ino, false, &isnew); 1739 1740 if (inode) { 1741 if (IS_ERR(inode)) 1742 return NULL; 1743 if (unlikely(isnew)) 1744 wait_on_new_inode(inode); 1745 if (unlikely(inode_unhashed(inode))) { 1746 iput(inode); 1747 goto again; 1748 } 1749 } 1750 return inode; 1751 } 1752 EXPORT_SYMBOL(ilookup); 1753 1754 /** 1755 * find_inode_nowait - find an inode in the inode cache 1756 * @sb: super block of file system to search 1757 * @hashval: hash value (usually inode number) to search for 1758 * @match: callback used for comparisons between inodes 1759 * @data: opaque data pointer to pass to @match 1760 * 1761 * Search for the inode specified by @hashval and @data in the inode 1762 * cache, where the helper function @match will return 0 if the inode 1763 * does not match, 1 if the inode does match, and -1 if the search 1764 * should be stopped. The @match function must be responsible for 1765 * taking the i_lock spin_lock and checking i_state for an inode being 1766 * freed or being initialized, and incrementing the reference count 1767 * before returning 1. It also must not sleep, since it is called with 1768 * the inode_hash_lock spinlock held. 1769 * 1770 * This is a even more generalized version of ilookup5() when the 1771 * function must never block --- find_inode() can block in 1772 * __wait_on_freeing_inode() --- or when the caller can not increment 1773 * the reference count because the resulting iput() might cause an 1774 * inode eviction. The tradeoff is that the @match funtion must be 1775 * very carefully implemented. 1776 */ 1777 struct inode *find_inode_nowait(struct super_block *sb, 1778 u64 hashval, 1779 int (*match)(struct inode *, u64, 1780 void *), 1781 void *data) 1782 { 1783 struct hlist_head *head = inode_hashtable + hash(sb, hashval); 1784 struct inode *inode, *ret_inode = NULL; 1785 int mval; 1786 1787 spin_lock(&inode_hash_lock); 1788 hlist_for_each_entry(inode, head, i_hash) { 1789 if (inode->i_sb != sb) 1790 continue; 1791 mval = match(inode, hashval, data); 1792 if (mval == 0) 1793 continue; 1794 if (mval == 1) 1795 ret_inode = inode; 1796 goto out; 1797 } 1798 out: 1799 spin_unlock(&inode_hash_lock); 1800 return ret_inode; 1801 } 1802 EXPORT_SYMBOL(find_inode_nowait); 1803 1804 /** 1805 * find_inode_rcu - find an inode in the inode cache 1806 * @sb: Super block of file system to search 1807 * @hashval: Key to hash 1808 * @test: Function to test match on an inode 1809 * @data: Data for test function 1810 * 1811 * Search for the inode specified by @hashval and @data in the inode cache, 1812 * where the helper function @test will return 0 if the inode does not match 1813 * and 1 if it does. The @test function must be responsible for taking the 1814 * i_lock spin_lock and checking i_state for an inode being freed or being 1815 * initialized. 1816 * 1817 * If successful, this will return the inode for which the @test function 1818 * returned 1 and NULL otherwise. 1819 * 1820 * The @test function is not permitted to take a ref on any inode presented. 1821 * It is also not permitted to sleep. 1822 * 1823 * The caller must hold the RCU read lock. 1824 */ 1825 struct inode *find_inode_rcu(struct super_block *sb, u64 hashval, 1826 int (*test)(struct inode *, void *), void *data) 1827 { 1828 struct hlist_head *head = inode_hashtable + hash(sb, hashval); 1829 struct inode *inode; 1830 1831 RCU_LOCKDEP_WARN(!rcu_read_lock_held(), 1832 "suspicious find_inode_rcu() usage"); 1833 1834 hlist_for_each_entry_rcu(inode, head, i_hash) { 1835 if (inode->i_sb == sb && 1836 !(inode_state_read_once(inode) & (I_FREEING | I_WILL_FREE)) && 1837 test(inode, data)) 1838 return inode; 1839 } 1840 return NULL; 1841 } 1842 EXPORT_SYMBOL(find_inode_rcu); 1843 1844 /** 1845 * find_inode_by_ino_rcu - Find an inode in the inode cache 1846 * @sb: Super block of file system to search 1847 * @ino: The inode number to match 1848 * 1849 * Search for the inode specified by @hashval and @data in the inode cache, 1850 * where the helper function @test will return 0 if the inode does not match 1851 * and 1 if it does. The @test function must be responsible for taking the 1852 * i_lock spin_lock and checking i_state for an inode being freed or being 1853 * initialized. 1854 * 1855 * If successful, this will return the inode for which the @test function 1856 * returned 1 and NULL otherwise. 1857 * 1858 * The @test function is not permitted to take a ref on any inode presented. 1859 * It is also not permitted to sleep. 1860 * 1861 * The caller must hold the RCU read lock. 1862 */ 1863 struct inode *find_inode_by_ino_rcu(struct super_block *sb, 1864 u64 ino) 1865 { 1866 struct hlist_head *head = inode_hashtable + hash(sb, ino); 1867 struct inode *inode; 1868 1869 RCU_LOCKDEP_WARN(!rcu_read_lock_held(), 1870 "suspicious find_inode_by_ino_rcu() usage"); 1871 1872 hlist_for_each_entry_rcu(inode, head, i_hash) { 1873 if (inode->i_ino == ino && 1874 inode->i_sb == sb && 1875 !(inode_state_read_once(inode) & (I_FREEING | I_WILL_FREE))) 1876 return inode; 1877 } 1878 return NULL; 1879 } 1880 EXPORT_SYMBOL(find_inode_by_ino_rcu); 1881 1882 int insert_inode_locked(struct inode *inode) 1883 { 1884 struct super_block *sb = inode->i_sb; 1885 u64 ino = inode->i_ino; 1886 struct hlist_head *head = inode_hashtable + hash(sb, ino); 1887 bool isnew; 1888 1889 might_sleep(); 1890 1891 while (1) { 1892 struct inode *old = NULL; 1893 spin_lock(&inode_hash_lock); 1894 repeat: 1895 hlist_for_each_entry(old, head, i_hash) { 1896 if (old->i_ino != ino) 1897 continue; 1898 if (old->i_sb != sb) 1899 continue; 1900 spin_lock(&old->i_lock); 1901 break; 1902 } 1903 if (likely(!old)) { 1904 spin_lock(&inode->i_lock); 1905 inode_state_set(inode, I_NEW | I_CREATING); 1906 hlist_add_head_rcu(&inode->i_hash, head); 1907 spin_unlock(&inode->i_lock); 1908 spin_unlock(&inode_hash_lock); 1909 return 0; 1910 } 1911 if (inode_state_read(old) & (I_FREEING | I_WILL_FREE)) { 1912 __wait_on_freeing_inode(old, true, false); 1913 old = NULL; 1914 goto repeat; 1915 } 1916 if (unlikely(inode_state_read(old) & I_CREATING)) { 1917 spin_unlock(&old->i_lock); 1918 spin_unlock(&inode_hash_lock); 1919 return -EBUSY; 1920 } 1921 __iget(old); 1922 isnew = !!(inode_state_read(old) & I_NEW); 1923 spin_unlock(&old->i_lock); 1924 spin_unlock(&inode_hash_lock); 1925 if (isnew) 1926 wait_on_new_inode(old); 1927 if (unlikely(!inode_unhashed(old))) { 1928 iput(old); 1929 return -EBUSY; 1930 } 1931 iput(old); 1932 } 1933 } 1934 EXPORT_SYMBOL(insert_inode_locked); 1935 1936 int insert_inode_locked4(struct inode *inode, u64 hashval, 1937 int (*test)(struct inode *, void *), void *data) 1938 { 1939 struct inode *old; 1940 1941 might_sleep(); 1942 1943 inode_state_set_raw(inode, I_CREATING); 1944 old = inode_insert5(inode, hashval, test, NULL, data); 1945 1946 if (old != inode) { 1947 iput(old); 1948 return -EBUSY; 1949 } 1950 return 0; 1951 } 1952 EXPORT_SYMBOL(insert_inode_locked4); 1953 1954 1955 int inode_just_drop(struct inode *inode) 1956 { 1957 return 1; 1958 } 1959 EXPORT_SYMBOL(inode_just_drop); 1960 1961 /* 1962 * Called when we're dropping the last reference 1963 * to an inode. 1964 * 1965 * Call the FS "drop_inode()" function, defaulting to 1966 * the legacy UNIX filesystem behaviour. If it tells 1967 * us to evict inode, do so. Otherwise, retain inode 1968 * in cache if fs is alive, sync and evict if fs is 1969 * shutting down. 1970 */ 1971 static void iput_final(struct inode *inode) 1972 { 1973 struct super_block *sb = inode->i_sb; 1974 const struct super_operations *op = inode->i_sb->s_op; 1975 int drop; 1976 1977 WARN_ON(inode_state_read(inode) & I_NEW); 1978 VFS_BUG_ON_INODE(icount_read(inode) != 0, inode); 1979 1980 if (op->drop_inode) 1981 drop = op->drop_inode(inode); 1982 else 1983 drop = inode_generic_drop(inode); 1984 1985 if (!drop && 1986 !(inode_state_read(inode) & I_DONTCACHE) && 1987 (sb->s_flags & SB_ACTIVE)) { 1988 __inode_lru_list_add(inode, true); 1989 spin_unlock(&inode->i_lock); 1990 return; 1991 } 1992 1993 /* 1994 * Re-check ->i_count in case the ->drop_inode() hooks played games. 1995 * Note we only execute this if the verdict was to drop the inode. 1996 */ 1997 VFS_BUG_ON_INODE(icount_read(inode) != 0, inode); 1998 1999 if (drop) { 2000 inode_state_set(inode, I_FREEING); 2001 } else { 2002 inode_state_set(inode, I_WILL_FREE); 2003 spin_unlock(&inode->i_lock); 2004 2005 write_inode_now(inode, 1); 2006 2007 spin_lock(&inode->i_lock); 2008 WARN_ON(inode_state_read(inode) & I_NEW); 2009 inode_state_replace(inode, I_WILL_FREE, I_FREEING); 2010 } 2011 2012 inode_lru_list_del(inode); 2013 spin_unlock(&inode->i_lock); 2014 2015 evict(inode); 2016 } 2017 2018 /** 2019 * iput - put an inode 2020 * @inode: inode to put 2021 * 2022 * Puts an inode, dropping its usage count. If the inode use count hits 2023 * zero, the inode is then freed and may also be destroyed. 2024 * 2025 * Consequently, iput() can sleep. 2026 */ 2027 void iput(struct inode *inode) 2028 { 2029 might_sleep(); 2030 if (unlikely(!inode)) 2031 return; 2032 2033 retry: 2034 lockdep_assert_not_held(&inode->i_lock); 2035 VFS_BUG_ON_INODE(inode_state_read_once(inode) & (I_FREEING | I_CLEAR), inode); 2036 /* 2037 * Note this assert is technically racy as if the count is bogusly 2038 * equal to one, then two CPUs racing to further drop it can both 2039 * conclude it's fine. 2040 */ 2041 VFS_BUG_ON_INODE(icount_read_once(inode) < 1, inode); 2042 2043 if (atomic_add_unless(&inode->i_count, -1, 1)) 2044 return; 2045 2046 if (inode->i_nlink && sync_lazytime(inode)) 2047 goto retry; 2048 2049 spin_lock(&inode->i_lock); 2050 if (unlikely((inode_state_read(inode) & I_DIRTY_TIME) && inode->i_nlink)) { 2051 spin_unlock(&inode->i_lock); 2052 goto retry; 2053 } 2054 2055 if (!atomic_dec_and_test(&inode->i_count)) { 2056 spin_unlock(&inode->i_lock); 2057 return; 2058 } 2059 2060 /* 2061 * iput_final() drops ->i_lock, we can't assert on it as the inode may 2062 * be deallocated by the time the call returns. 2063 */ 2064 iput_final(inode); 2065 } 2066 EXPORT_SYMBOL(iput); 2067 2068 /** 2069 * iput_not_last - put an inode assuming this is not the last reference 2070 * @inode: inode to put 2071 */ 2072 void iput_not_last(struct inode *inode) 2073 { 2074 VFS_BUG_ON_INODE(inode_state_read_once(inode) & (I_FREEING | I_CLEAR), inode); 2075 VFS_BUG_ON_INODE(icount_read_once(inode) < 2, inode); 2076 2077 WARN_ON(atomic_sub_return(1, &inode->i_count) == 0); 2078 } 2079 EXPORT_SYMBOL(iput_not_last); 2080 2081 #ifdef CONFIG_BLOCK 2082 /** 2083 * bmap - find a block number in a file 2084 * @inode: inode owning the block number being requested 2085 * @block: pointer containing the block to find 2086 * 2087 * Replaces the value in ``*block`` with the block number on the device holding 2088 * corresponding to the requested block number in the file. 2089 * That is, asked for block 4 of inode 1 the function will replace the 2090 * 4 in ``*block``, with disk block relative to the disk start that holds that 2091 * block of the file. 2092 * 2093 * Returns -EINVAL in case of error, 0 otherwise. If mapping falls into a 2094 * hole, returns 0 and ``*block`` is also set to 0. 2095 */ 2096 int bmap(struct inode *inode, sector_t *block) 2097 { 2098 if (!inode->i_mapping->a_ops->bmap) 2099 return -EINVAL; 2100 2101 *block = inode->i_mapping->a_ops->bmap(inode->i_mapping, *block); 2102 return 0; 2103 } 2104 EXPORT_SYMBOL(bmap); 2105 #endif 2106 2107 /* 2108 * With relative atime, only update atime if the previous atime is 2109 * earlier than or equal to either the ctime or mtime, 2110 * or if at least a day has passed since the last atime update. 2111 */ 2112 static bool relatime_need_update(struct vfsmount *mnt, struct inode *inode, 2113 struct timespec64 now) 2114 { 2115 struct timespec64 atime, mtime, ctime; 2116 2117 if (!(mnt->mnt_flags & MNT_RELATIME)) 2118 return true; 2119 /* 2120 * Is mtime younger than or equal to atime? If yes, update atime: 2121 */ 2122 atime = inode_get_atime(inode); 2123 mtime = inode_get_mtime(inode); 2124 if (timespec64_compare(&mtime, &atime) >= 0) 2125 return true; 2126 /* 2127 * Is ctime younger than or equal to atime? If yes, update atime: 2128 */ 2129 ctime = inode_get_ctime(inode); 2130 if (timespec64_compare(&ctime, &atime) >= 0) 2131 return true; 2132 2133 /* 2134 * Is the previous atime value older than a day? If yes, 2135 * update atime: 2136 */ 2137 if ((long)(now.tv_sec - atime.tv_sec) >= 24*60*60) 2138 return true; 2139 /* 2140 * Good, we can skip the atime update: 2141 */ 2142 return false; 2143 } 2144 2145 static int inode_update_atime(struct inode *inode) 2146 { 2147 struct timespec64 atime = inode_get_atime(inode); 2148 struct timespec64 now = current_time(inode); 2149 2150 if (timespec64_equal(&now, &atime)) 2151 return 0; 2152 2153 inode_set_atime_to_ts(inode, now); 2154 return inode_time_dirty_flag(inode); 2155 } 2156 2157 static int inode_update_cmtime(struct inode *inode, unsigned int flags) 2158 { 2159 struct timespec64 ctime = inode_get_ctime(inode); 2160 struct timespec64 mtime = inode_get_mtime(inode); 2161 struct timespec64 now = inode_set_ctime_current(inode); 2162 unsigned int dirty = 0; 2163 bool mtime_changed; 2164 2165 mtime_changed = !timespec64_equal(&now, &mtime); 2166 if (mtime_changed || !timespec64_equal(&now, &ctime)) 2167 dirty = inode_time_dirty_flag(inode); 2168 2169 /* 2170 * Pure timestamp updates can be recorded in the inode without blocking 2171 * by not dirtying the inode. But when the file system requires 2172 * i_version updates, the update of i_version can still block. 2173 * Error out if we'd actually have to update i_version or don't support 2174 * lazytime. 2175 */ 2176 if (IS_I_VERSION(inode)) { 2177 if (flags & IOCB_NOWAIT) { 2178 if (!(inode->i_sb->s_flags & SB_LAZYTIME) || 2179 inode_iversion_need_inc(inode)) 2180 return -EAGAIN; 2181 } else { 2182 /* 2183 * Don't force iversion increment for pure lazytime 2184 * updates (I_DIRTY_TIME only), let I_VERSION_QUERIED 2185 * dictate whether the increment is needed. 2186 */ 2187 if (inode_maybe_inc_iversion(inode, 2188 dirty != I_DIRTY_TIME)) 2189 dirty |= I_DIRTY_SYNC; 2190 } 2191 } 2192 2193 if (mtime_changed) 2194 inode_set_mtime_to_ts(inode, now); 2195 return dirty; 2196 } 2197 2198 /** 2199 * inode_update_time - update either atime or c/mtime and i_version on the inode 2200 * @inode: inode to be updated 2201 * @type: timestamp to be updated 2202 * @flags: flags for the update 2203 * 2204 * Update either atime or c/mtime and version in a inode if needed for a file 2205 * access or modification. It is up to the caller to mark the inode dirty 2206 * appropriately. 2207 * 2208 * Returns the positive I_DIRTY_* flags for __mark_inode_dirty() if the inode 2209 * needs to be marked dirty, 0 if it did not, or a negative errno if an error 2210 * happened. 2211 */ 2212 int inode_update_time(struct inode *inode, enum fs_update_time type, 2213 unsigned int flags) 2214 { 2215 switch (type) { 2216 case FS_UPD_ATIME: 2217 return inode_update_atime(inode); 2218 case FS_UPD_CMTIME: 2219 return inode_update_cmtime(inode, flags); 2220 default: 2221 WARN_ON_ONCE(1); 2222 return -EIO; 2223 } 2224 } 2225 EXPORT_SYMBOL(inode_update_time); 2226 2227 /** 2228 * generic_update_time - update the timestamps on the inode 2229 * @inode: inode to be updated 2230 * @type: timestamp to be updated 2231 * @flags: flags for the update 2232 * 2233 * Returns a negative error value on error, else 0. 2234 */ 2235 int generic_update_time(struct inode *inode, enum fs_update_time type, 2236 unsigned int flags) 2237 { 2238 int dirty; 2239 2240 /* 2241 * ->dirty_inode is what could make generic timestamp updates block. 2242 * Don't support non-blocking timestamp updates here if it is set. 2243 * File systems that implement ->dirty_inode but want to support 2244 * non-blocking timestamp updates should call inode_update_time 2245 * directly. 2246 */ 2247 if ((flags & IOCB_NOWAIT) && inode->i_sb->s_op->dirty_inode) 2248 return -EAGAIN; 2249 2250 dirty = inode_update_time(inode, type, flags); 2251 if (dirty <= 0) 2252 return dirty; 2253 __mark_inode_dirty(inode, dirty); 2254 return 0; 2255 } 2256 EXPORT_SYMBOL(generic_update_time); 2257 2258 /** 2259 * atime_needs_update - update the access time 2260 * @path: the &struct path to update 2261 * @inode: inode to update 2262 * 2263 * Update the accessed time on an inode and mark it for writeback. 2264 * This function automatically handles read only file systems and media, 2265 * as well as the "noatime" flag and inode specific "noatime" markers. 2266 */ 2267 bool atime_needs_update(const struct path *path, struct inode *inode) 2268 { 2269 struct vfsmount *mnt = path->mnt; 2270 struct timespec64 now, atime; 2271 2272 if (inode->i_flags & S_NOATIME) 2273 return false; 2274 2275 /* Atime updates will likely cause i_uid and i_gid to be written 2276 * back improprely if their true value is unknown to the vfs. 2277 */ 2278 if (HAS_UNMAPPED_ID(mnt_idmap(mnt), inode)) 2279 return false; 2280 2281 if (IS_NOATIME(inode)) 2282 return false; 2283 if ((inode->i_sb->s_flags & SB_NODIRATIME) && S_ISDIR(inode->i_mode)) 2284 return false; 2285 2286 if (mnt->mnt_flags & MNT_NOATIME) 2287 return false; 2288 if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)) 2289 return false; 2290 2291 now = current_time(inode); 2292 2293 if (!relatime_need_update(mnt, inode, now)) 2294 return false; 2295 2296 atime = inode_get_atime(inode); 2297 if (timespec64_equal(&atime, &now)) 2298 return false; 2299 2300 return true; 2301 } 2302 2303 void touch_atime(const struct path *path) 2304 { 2305 struct vfsmount *mnt = path->mnt; 2306 struct inode *inode = d_inode(path->dentry); 2307 2308 if (!atime_needs_update(path, inode)) 2309 return; 2310 2311 if (!sb_start_write_trylock(inode->i_sb)) 2312 return; 2313 2314 if (mnt_get_write_access(mnt) != 0) 2315 goto skip_update; 2316 /* 2317 * File systems can error out when updating inodes if they need to 2318 * allocate new space to modify an inode (such is the case for 2319 * Btrfs), but since we touch atime while walking down the path we 2320 * really don't care if we failed to update the atime of the file, 2321 * so just ignore the return value. 2322 * We may also fail on filesystems that have the ability to make parts 2323 * of the fs read only, e.g. subvolumes in Btrfs. 2324 */ 2325 if (inode->i_op->update_time) 2326 inode->i_op->update_time(inode, FS_UPD_ATIME, 0); 2327 else 2328 generic_update_time(inode, FS_UPD_ATIME, 0); 2329 mnt_put_write_access(mnt); 2330 skip_update: 2331 sb_end_write(inode->i_sb); 2332 } 2333 EXPORT_SYMBOL(touch_atime); 2334 2335 /* 2336 * Return mask of changes for notify_change() that need to be done as a 2337 * response to write or truncate. Return 0 if nothing has to be changed. 2338 * Negative value on error (change should be denied). 2339 */ 2340 int dentry_needs_remove_privs(struct mnt_idmap *idmap, 2341 struct dentry *dentry) 2342 { 2343 struct inode *inode = d_inode(dentry); 2344 int mask = 0; 2345 int ret; 2346 2347 if (IS_NOSEC(inode)) 2348 return 0; 2349 2350 mask = setattr_should_drop_suidgid(idmap, inode); 2351 ret = security_inode_need_killpriv(dentry); 2352 if (ret < 0) 2353 return ret; 2354 if (ret) 2355 mask |= ATTR_KILL_PRIV; 2356 return mask; 2357 } 2358 2359 static int __remove_privs(struct mnt_idmap *idmap, 2360 struct dentry *dentry, int kill) 2361 { 2362 struct iattr newattrs; 2363 2364 newattrs.ia_valid = ATTR_FORCE | kill; 2365 /* 2366 * Note we call this on write, so notify_change will not 2367 * encounter any conflicting delegations: 2368 */ 2369 return notify_change(idmap, dentry, &newattrs, NULL); 2370 } 2371 2372 static int file_remove_privs_flags(struct file *file, unsigned int flags) 2373 { 2374 struct dentry *dentry = file_dentry(file); 2375 struct inode *inode = file_inode(file); 2376 int error = 0; 2377 int kill; 2378 2379 if (IS_NOSEC(inode) || !S_ISREG(inode->i_mode)) 2380 return 0; 2381 2382 kill = dentry_needs_remove_privs(file_mnt_idmap(file), dentry); 2383 if (kill < 0) 2384 return kill; 2385 2386 if (kill) { 2387 if (flags & IOCB_NOWAIT) 2388 return -EAGAIN; 2389 2390 error = __remove_privs(file_mnt_idmap(file), dentry, kill); 2391 } 2392 2393 if (!error) 2394 inode_has_no_xattr(inode); 2395 return error; 2396 } 2397 2398 /** 2399 * file_remove_privs - remove special file privileges (suid, capabilities) 2400 * @file: file to remove privileges from 2401 * 2402 * When file is modified by a write or truncation ensure that special 2403 * file privileges are removed. 2404 * 2405 * Return: 0 on success, negative errno on failure. 2406 */ 2407 int file_remove_privs(struct file *file) 2408 { 2409 return file_remove_privs_flags(file, 0); 2410 } 2411 EXPORT_SYMBOL(file_remove_privs); 2412 2413 /** 2414 * current_time - Return FS time (possibly fine-grained) 2415 * @inode: inode. 2416 * 2417 * Return the current time truncated to the time granularity supported by 2418 * the fs, as suitable for a ctime/mtime change. If the ctime is flagged 2419 * as having been QUERIED, get a fine-grained timestamp, but don't update 2420 * the floor. 2421 * 2422 * For a multigrain inode, this is effectively an estimate of the timestamp 2423 * that a file would receive. An actual update must go through 2424 * inode_set_ctime_current(). 2425 */ 2426 struct timespec64 current_time(struct inode *inode) 2427 { 2428 struct timespec64 now; 2429 u32 cns; 2430 2431 ktime_get_coarse_real_ts64_mg(&now); 2432 2433 if (!is_mgtime(inode)) 2434 goto out; 2435 2436 /* If nothing has queried it, then coarse time is fine */ 2437 cns = smp_load_acquire(&inode->i_ctime_nsec); 2438 if (cns & I_CTIME_QUERIED) { 2439 /* 2440 * If there is no apparent change, then get a fine-grained 2441 * timestamp. 2442 */ 2443 if (now.tv_nsec == (cns & ~I_CTIME_QUERIED)) 2444 ktime_get_real_ts64(&now); 2445 } 2446 out: 2447 return timestamp_truncate(now, inode); 2448 } 2449 EXPORT_SYMBOL(current_time); 2450 2451 static inline bool need_cmtime_update(struct inode *inode) 2452 { 2453 struct timespec64 now = current_time(inode), ts; 2454 2455 ts = inode_get_mtime(inode); 2456 if (!timespec64_equal(&ts, &now)) 2457 return true; 2458 ts = inode_get_ctime(inode); 2459 if (!timespec64_equal(&ts, &now)) 2460 return true; 2461 return IS_I_VERSION(inode) && inode_iversion_need_inc(inode); 2462 } 2463 2464 static int file_update_time_flags(struct file *file, unsigned int flags) 2465 { 2466 struct inode *inode = file_inode(file); 2467 int ret; 2468 2469 /* First try to exhaust all avenues to not sync */ 2470 if (IS_NOCMTIME(inode)) 2471 return 0; 2472 if (unlikely(file->f_mode & FMODE_NOCMTIME)) 2473 return 0; 2474 if (!need_cmtime_update(inode)) 2475 return 0; 2476 2477 flags &= IOCB_NOWAIT; 2478 if (mnt_get_write_access_file(file)) 2479 return 0; 2480 if (inode->i_op->update_time) 2481 ret = inode->i_op->update_time(inode, FS_UPD_CMTIME, flags); 2482 else 2483 ret = generic_update_time(inode, FS_UPD_CMTIME, flags); 2484 mnt_put_write_access_file(file); 2485 return ret; 2486 } 2487 2488 /** 2489 * file_update_time - update mtime and ctime time 2490 * @file: file accessed 2491 * 2492 * Update the mtime and ctime members of an inode and mark the inode for 2493 * writeback. Note that this function is meant exclusively for usage in 2494 * the file write path of filesystems, and filesystems may choose to 2495 * explicitly ignore updates via this function with the _NOCMTIME inode 2496 * flag, e.g. for network filesystem where these imestamps are handled 2497 * by the server. This can return an error for file systems who need to 2498 * allocate space in order to update an inode. 2499 * 2500 * Return: 0 on success, negative errno on failure. 2501 */ 2502 int file_update_time(struct file *file) 2503 { 2504 return file_update_time_flags(file, 0); 2505 } 2506 EXPORT_SYMBOL(file_update_time); 2507 2508 /** 2509 * file_modified_flags - handle mandated vfs changes when modifying a file 2510 * @file: file that was modified 2511 * @flags: kiocb flags 2512 * 2513 * When file has been modified ensure that special 2514 * file privileges are removed and time settings are updated. 2515 * 2516 * If IOCB_NOWAIT is set, special file privileges will not be removed and 2517 * time settings will not be updated. It will return -EAGAIN. 2518 * 2519 * Context: Caller must hold the file's inode lock. 2520 * 2521 * Return: 0 on success, negative errno on failure. 2522 */ 2523 static int file_modified_flags(struct file *file, int flags) 2524 { 2525 int ret; 2526 2527 /* 2528 * Clear the security bits if the process is not being run by root. 2529 * This keeps people from modifying setuid and setgid binaries. 2530 */ 2531 ret = file_remove_privs_flags(file, flags); 2532 if (ret) 2533 return ret; 2534 return file_update_time_flags(file, flags); 2535 } 2536 2537 /** 2538 * file_modified - handle mandated vfs changes when modifying a file 2539 * @file: file that was modified 2540 * 2541 * When file has been modified ensure that special 2542 * file privileges are removed and time settings are updated. 2543 * 2544 * Context: Caller must hold the file's inode lock. 2545 * 2546 * Return: 0 on success, negative errno on failure. 2547 */ 2548 int file_modified(struct file *file) 2549 { 2550 return file_modified_flags(file, 0); 2551 } 2552 EXPORT_SYMBOL(file_modified); 2553 2554 /** 2555 * kiocb_modified - handle mandated vfs changes when modifying a file 2556 * @iocb: iocb that was modified 2557 * 2558 * When file has been modified ensure that special 2559 * file privileges are removed and time settings are updated. 2560 * 2561 * Context: Caller must hold the file's inode lock. 2562 * 2563 * Return: 0 on success, negative errno on failure. 2564 */ 2565 int kiocb_modified(struct kiocb *iocb) 2566 { 2567 return file_modified_flags(iocb->ki_filp, iocb->ki_flags); 2568 } 2569 EXPORT_SYMBOL_GPL(kiocb_modified); 2570 2571 int inode_needs_sync(struct inode *inode) 2572 { 2573 if (IS_SYNC(inode)) 2574 return 1; 2575 if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode)) 2576 return 1; 2577 return 0; 2578 } 2579 EXPORT_SYMBOL(inode_needs_sync); 2580 2581 /* 2582 * If we try to find an inode in the inode hash while it is being 2583 * deleted, we have to wait until the filesystem completes its 2584 * deletion before reporting that it isn't found. This function waits 2585 * until the deletion _might_ have completed. Callers are responsible 2586 * to recheck inode state. 2587 * 2588 * It doesn't matter if I_NEW is not set initially, a call to 2589 * wake_up_bit(&inode->i_state, __I_NEW) after removing from the hash list 2590 * will DTRT. 2591 */ 2592 static void __wait_on_freeing_inode(struct inode *inode, bool hash_locked, bool rcu_locked) 2593 { 2594 struct wait_bit_queue_entry wqe; 2595 struct wait_queue_head *wq_head; 2596 2597 VFS_BUG_ON(!hash_locked && !rcu_locked); 2598 2599 /* 2600 * Handle racing against evict(), see that routine for more details. 2601 */ 2602 if (unlikely(inode_unhashed(inode))) { 2603 WARN_ON(hash_locked); 2604 spin_unlock(&inode->i_lock); 2605 return; 2606 } 2607 2608 wq_head = inode_bit_waitqueue(&wqe, inode, __I_NEW); 2609 prepare_to_wait_event(wq_head, &wqe.wq_entry, TASK_UNINTERRUPTIBLE); 2610 spin_unlock(&inode->i_lock); 2611 if (rcu_locked) 2612 rcu_read_unlock(); 2613 if (hash_locked) 2614 spin_unlock(&inode_hash_lock); 2615 schedule(); 2616 finish_wait(wq_head, &wqe.wq_entry); 2617 if (hash_locked) 2618 spin_lock(&inode_hash_lock); 2619 if (rcu_locked) 2620 rcu_read_lock(); 2621 } 2622 2623 static __initdata unsigned long ihash_entries; 2624 static int __init set_ihash_entries(char *str) 2625 { 2626 return kstrtoul(str, 0, &ihash_entries) == 0; 2627 } 2628 __setup("ihash_entries=", set_ihash_entries); 2629 2630 /* 2631 * Initialize the waitqueues and inode hash table. 2632 */ 2633 void __init inode_init_early(void) 2634 { 2635 /* If hashes are distributed across NUMA nodes, defer 2636 * hash allocation until vmalloc space is available. 2637 */ 2638 if (hashdist) 2639 return; 2640 2641 inode_hashtable = 2642 alloc_large_system_hash("Inode-cache", 2643 sizeof(struct hlist_head), 2644 ihash_entries, 2645 14, 2646 HASH_EARLY | HASH_ZERO, 2647 &i_hash_shift, 2648 &i_hash_mask, 2649 0, 2650 0); 2651 } 2652 2653 void __init inode_init(void) 2654 { 2655 /* inode slab cache */ 2656 inode_cachep = kmem_cache_create("inode_cache", 2657 sizeof(struct inode), 2658 0, 2659 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC| 2660 SLAB_ACCOUNT), 2661 init_once); 2662 2663 /* Hash may have been set up in inode_init_early */ 2664 if (!hashdist) 2665 return; 2666 2667 inode_hashtable = 2668 alloc_large_system_hash("Inode-cache", 2669 sizeof(struct hlist_head), 2670 ihash_entries, 2671 14, 2672 HASH_ZERO, 2673 &i_hash_shift, 2674 &i_hash_mask, 2675 0, 2676 0); 2677 } 2678 2679 void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev) 2680 { 2681 inode->i_mode = mode; 2682 switch (inode->i_mode & S_IFMT) { 2683 case S_IFCHR: 2684 inode->i_fop = &def_chr_fops; 2685 inode->i_rdev = rdev; 2686 break; 2687 case S_IFBLK: 2688 if (IS_ENABLED(CONFIG_BLOCK)) 2689 inode->i_fop = &def_blk_fops; 2690 inode->i_rdev = rdev; 2691 break; 2692 case S_IFIFO: 2693 inode->i_fop = &pipefifo_fops; 2694 break; 2695 case S_IFSOCK: 2696 /* leave it no_open_fops */ 2697 break; 2698 default: 2699 pr_debug("init_special_inode: bogus i_mode (%o) for inode %s:%llu\n", 2700 mode, inode->i_sb->s_id, inode->i_ino); 2701 break; 2702 } 2703 } 2704 EXPORT_SYMBOL(init_special_inode); 2705 2706 /** 2707 * inode_init_owner - Init uid,gid,mode for new inode according to posix standards 2708 * @idmap: idmap of the mount the inode was created from 2709 * @inode: New inode 2710 * @dir: Directory inode 2711 * @mode: mode of the new inode 2712 * 2713 * If the inode has been created through an idmapped mount the idmap of 2714 * the vfsmount must be passed through @idmap. This function will then take 2715 * care to map the inode according to @idmap before checking permissions 2716 * and initializing i_uid and i_gid. On non-idmapped mounts or if permission 2717 * checking is to be performed on the raw inode simply pass @nop_mnt_idmap. 2718 */ 2719 void inode_init_owner(struct mnt_idmap *idmap, struct inode *inode, 2720 const struct inode *dir, umode_t mode) 2721 { 2722 inode_fsuid_set(inode, idmap); 2723 if (dir && dir->i_mode & S_ISGID) { 2724 inode->i_gid = dir->i_gid; 2725 2726 /* Directories are special, and always inherit S_ISGID */ 2727 if (S_ISDIR(mode)) 2728 mode |= S_ISGID; 2729 } else 2730 inode_fsgid_set(inode, idmap); 2731 inode->i_mode = mode; 2732 } 2733 EXPORT_SYMBOL(inode_init_owner); 2734 2735 /** 2736 * inode_owner_or_capable - check current task permissions to inode 2737 * @idmap: idmap of the mount the inode was found from 2738 * @inode: inode being checked 2739 * 2740 * Return true if current either has CAP_FOWNER in a namespace with the 2741 * inode owner uid mapped, or owns the file. 2742 * 2743 * If the inode has been found through an idmapped mount the idmap of 2744 * the vfsmount must be passed through @idmap. This function will then take 2745 * care to map the inode according to @idmap before checking permissions. 2746 * On non-idmapped mounts or if permission checking is to be performed on the 2747 * raw inode simply pass @nop_mnt_idmap. 2748 */ 2749 bool inode_owner_or_capable(struct mnt_idmap *idmap, 2750 const struct inode *inode) 2751 { 2752 vfsuid_t vfsuid; 2753 struct user_namespace *ns; 2754 2755 vfsuid = i_uid_into_vfsuid(idmap, inode); 2756 if (vfsuid_eq_kuid(vfsuid, current_fsuid())) 2757 return true; 2758 2759 ns = current_user_ns(); 2760 if (vfsuid_has_mapping(ns, vfsuid) && ns_capable(ns, CAP_FOWNER)) 2761 return true; 2762 return false; 2763 } 2764 EXPORT_SYMBOL(inode_owner_or_capable); 2765 2766 /* 2767 * Direct i/o helper functions 2768 */ 2769 bool inode_dio_finished(const struct inode *inode) 2770 { 2771 return atomic_read(&inode->i_dio_count) == 0; 2772 } 2773 EXPORT_SYMBOL(inode_dio_finished); 2774 2775 /** 2776 * inode_dio_wait - wait for outstanding DIO requests to finish 2777 * @inode: inode to wait for 2778 * 2779 * Waits for all pending direct I/O requests to finish so that we can 2780 * proceed with a truncate or equivalent operation. 2781 * 2782 * Must be called under a lock that serializes taking new references 2783 * to i_dio_count, usually by inode->i_rwsem. 2784 */ 2785 void inode_dio_wait(struct inode *inode) 2786 { 2787 wait_var_event(&inode->i_dio_count, inode_dio_finished(inode)); 2788 } 2789 EXPORT_SYMBOL(inode_dio_wait); 2790 2791 void inode_dio_wait_interruptible(struct inode *inode) 2792 { 2793 wait_var_event_interruptible(&inode->i_dio_count, 2794 inode_dio_finished(inode)); 2795 } 2796 EXPORT_SYMBOL(inode_dio_wait_interruptible); 2797 2798 /* 2799 * inode_set_flags - atomically set some inode flags 2800 * 2801 * Note: the caller should be holding i_rwsem exclusively, or else be sure that 2802 * they have exclusive access to the inode structure (i.e., while the 2803 * inode is being instantiated). The reason for the cmpxchg() loop 2804 * --- which wouldn't be necessary if all code paths which modify 2805 * i_flags actually followed this rule, is that there is at least one 2806 * code path which doesn't today so we use cmpxchg() out of an abundance 2807 * of caution. 2808 * 2809 * In the long run, i_rwsem is overkill, and we should probably look 2810 * at using the i_lock spinlock to protect i_flags, and then make sure 2811 * it is so documented in include/linux/fs.h and that all code follows 2812 * the locking convention!! 2813 */ 2814 void inode_set_flags(struct inode *inode, unsigned int flags, 2815 unsigned int mask) 2816 { 2817 WARN_ON_ONCE(flags & ~mask); 2818 set_mask_bits(&inode->i_flags, mask, flags); 2819 } 2820 EXPORT_SYMBOL(inode_set_flags); 2821 2822 void inode_nohighmem(struct inode *inode) 2823 { 2824 mapping_set_gfp_mask(inode->i_mapping, GFP_USER); 2825 } 2826 EXPORT_SYMBOL(inode_nohighmem); 2827 2828 struct timespec64 inode_set_ctime_to_ts(struct inode *inode, struct timespec64 ts) 2829 { 2830 trace_inode_set_ctime_to_ts(inode, &ts); 2831 set_normalized_timespec64(&ts, ts.tv_sec, ts.tv_nsec); 2832 WRITE_ONCE(inode->i_ctime_sec, ts.tv_sec); 2833 WRITE_ONCE(inode->i_ctime_nsec, ts.tv_nsec); 2834 return ts; 2835 } 2836 EXPORT_SYMBOL(inode_set_ctime_to_ts); 2837 2838 /** 2839 * timestamp_truncate - Truncate timespec to a granularity 2840 * @t: Timespec 2841 * @inode: inode being updated 2842 * 2843 * Truncate a timespec to the granularity supported by the fs 2844 * containing the inode. Always rounds down. gran must 2845 * not be 0 nor greater than a second (NSEC_PER_SEC, or 10^9 ns). 2846 */ 2847 struct timespec64 timestamp_truncate(struct timespec64 t, struct inode *inode) 2848 { 2849 struct super_block *sb = inode->i_sb; 2850 unsigned int gran = sb->s_time_gran; 2851 2852 t.tv_sec = clamp(t.tv_sec, sb->s_time_min, sb->s_time_max); 2853 if (unlikely(t.tv_sec == sb->s_time_max || t.tv_sec == sb->s_time_min)) 2854 t.tv_nsec = 0; 2855 2856 /* Avoid division in the common cases 1 ns and 1 s. */ 2857 if (gran == 1) 2858 ; /* nothing */ 2859 else if (gran == NSEC_PER_SEC) 2860 t.tv_nsec = 0; 2861 else if (gran > 1 && gran < NSEC_PER_SEC) 2862 t.tv_nsec -= t.tv_nsec % gran; 2863 else 2864 WARN(1, "invalid file time granularity: %u", gran); 2865 return t; 2866 } 2867 EXPORT_SYMBOL(timestamp_truncate); 2868 2869 /** 2870 * inode_set_ctime_current - set the ctime to current_time 2871 * @inode: inode 2872 * 2873 * Set the inode's ctime to the current value for the inode. Returns the 2874 * current value that was assigned. If this is not a multigrain inode, then we 2875 * set it to the later of the coarse time and floor value. 2876 * 2877 * If it is multigrain, then we first see if the coarse-grained timestamp is 2878 * distinct from what is already there. If so, then use that. Otherwise, get a 2879 * fine-grained timestamp. 2880 * 2881 * After that, try to swap the new value into i_ctime_nsec. Accept the 2882 * resulting ctime, regardless of the outcome of the swap. If it has 2883 * already been replaced, then that timestamp is later than the earlier 2884 * unacceptable one, and is thus acceptable. 2885 */ 2886 struct timespec64 inode_set_ctime_current(struct inode *inode) 2887 { 2888 struct timespec64 now; 2889 u32 cns, cur; 2890 2891 ktime_get_coarse_real_ts64_mg(&now); 2892 now = timestamp_truncate(now, inode); 2893 2894 /* Just return that if this is not a multigrain fs */ 2895 if (!is_mgtime(inode)) { 2896 inode_set_ctime_to_ts(inode, now); 2897 goto out; 2898 } 2899 2900 /* 2901 * A fine-grained time is only needed if someone has queried 2902 * for timestamps, and the current coarse grained time isn't 2903 * later than what's already there. 2904 */ 2905 cns = smp_load_acquire(&inode->i_ctime_nsec); 2906 if (cns & I_CTIME_QUERIED) { 2907 struct timespec64 ctime = { .tv_sec = inode_get_ctime_sec(inode), 2908 .tv_nsec = cns & ~I_CTIME_QUERIED }; 2909 2910 if (timespec64_compare(&now, &ctime) <= 0) { 2911 ktime_get_real_ts64_mg(&now); 2912 now = timestamp_truncate(now, inode); 2913 mgtime_counter_inc(mg_fine_stamps); 2914 } 2915 } 2916 mgtime_counter_inc(mg_ctime_updates); 2917 2918 /* No need to cmpxchg if it's exactly the same */ 2919 if (cns == now.tv_nsec && inode_get_ctime_sec(inode) == now.tv_sec) { 2920 trace_ctime_xchg_skip(inode, &now); 2921 goto out; 2922 } 2923 cur = cns; 2924 retry: 2925 /* Try to swap the nsec value into place. */ 2926 if (try_cmpxchg(&inode->i_ctime_nsec, &cur, now.tv_nsec)) { 2927 /* If swap occurred, then we're (mostly) done */ 2928 WRITE_ONCE(inode->i_ctime_sec, now.tv_sec); 2929 trace_ctime_ns_xchg(inode, cns, now.tv_nsec, cur); 2930 mgtime_counter_inc(mg_ctime_swaps); 2931 } else { 2932 /* 2933 * Was the change due to someone marking the old ctime QUERIED? 2934 * If so then retry the swap. This can only happen once since 2935 * the only way to clear I_CTIME_QUERIED is to stamp the inode 2936 * with a new ctime. 2937 */ 2938 if (!(cns & I_CTIME_QUERIED) && (cns | I_CTIME_QUERIED) == cur) { 2939 cns = cur; 2940 goto retry; 2941 } 2942 /* Otherwise, keep the existing ctime */ 2943 now.tv_sec = inode_get_ctime_sec(inode); 2944 now.tv_nsec = cur & ~I_CTIME_QUERIED; 2945 } 2946 out: 2947 return now; 2948 } 2949 EXPORT_SYMBOL(inode_set_ctime_current); 2950 2951 /** 2952 * inode_set_ctime_deleg - try to update the ctime on a delegated inode 2953 * @inode: inode to update 2954 * @update: timespec64 to set the ctime 2955 * 2956 * Attempt to atomically update the ctime on behalf of a delegation holder. 2957 * 2958 * The nfs server can call back the holder of a delegation to get updated 2959 * inode attributes, including the mtime. When updating the mtime, update 2960 * the ctime to a value at least equal to that. 2961 * 2962 * This can race with concurrent updates to the inode, in which 2963 * case the update is skipped. 2964 * 2965 * Note that this works even when multigrain timestamps are not enabled, 2966 * so it is used in either case. 2967 */ 2968 struct timespec64 inode_set_ctime_deleg(struct inode *inode, struct timespec64 update) 2969 { 2970 struct timespec64 now, cur_ts; 2971 u32 cur, old; 2972 2973 /* pairs with try_cmpxchg below */ 2974 cur = smp_load_acquire(&inode->i_ctime_nsec); 2975 cur_ts.tv_nsec = cur & ~I_CTIME_QUERIED; 2976 cur_ts.tv_sec = inode_get_ctime_sec(inode); 2977 2978 /* If the update is older than the existing value, skip it. */ 2979 if (timespec64_compare(&update, &cur_ts) <= 0) 2980 return cur_ts; 2981 2982 ktime_get_coarse_real_ts64_mg(&now); 2983 2984 /* Clamp the update to "now" if it's in the future */ 2985 if (timespec64_compare(&update, &now) > 0) 2986 update = now; 2987 2988 update = timestamp_truncate(update, inode); 2989 2990 /* No need to update if the values are already the same */ 2991 if (timespec64_equal(&update, &cur_ts)) 2992 return cur_ts; 2993 2994 /* 2995 * Try to swap the nsec value into place. If it fails, that means 2996 * it raced with an update due to a write or similar activity. That 2997 * stamp takes precedence, so just skip the update. 2998 */ 2999 retry: 3000 old = cur; 3001 if (try_cmpxchg(&inode->i_ctime_nsec, &cur, update.tv_nsec)) { 3002 WRITE_ONCE(inode->i_ctime_sec, update.tv_sec); 3003 mgtime_counter_inc(mg_ctime_swaps); 3004 return update; 3005 } 3006 3007 /* 3008 * Was the change due to another task marking the old ctime QUERIED? 3009 * 3010 * If so, then retry the swap. This can only happen once since 3011 * the only way to clear I_CTIME_QUERIED is to stamp the inode 3012 * with a new ctime. 3013 */ 3014 if (!(old & I_CTIME_QUERIED) && (cur == (old | I_CTIME_QUERIED))) 3015 goto retry; 3016 3017 /* Otherwise, it was a new timestamp. */ 3018 cur_ts.tv_sec = inode_get_ctime_sec(inode); 3019 cur_ts.tv_nsec = cur & ~I_CTIME_QUERIED; 3020 return cur_ts; 3021 } 3022 EXPORT_SYMBOL(inode_set_ctime_deleg); 3023 3024 /** 3025 * in_group_or_capable - check whether caller is CAP_FSETID privileged 3026 * @idmap: idmap of the mount @inode was found from 3027 * @inode: inode to check 3028 * @vfsgid: the new/current vfsgid of @inode 3029 * 3030 * Check whether @vfsgid is in the caller's group list or if the caller is 3031 * privileged with CAP_FSETID over @inode. This can be used to determine 3032 * whether the setgid bit can be kept or must be dropped. 3033 * 3034 * Return: true if the caller is sufficiently privileged, false if not. 3035 */ 3036 bool in_group_or_capable(struct mnt_idmap *idmap, 3037 const struct inode *inode, vfsgid_t vfsgid) 3038 { 3039 if (vfsgid_in_group_p(vfsgid)) 3040 return true; 3041 if (capable_wrt_inode_uidgid(idmap, inode, CAP_FSETID)) 3042 return true; 3043 return false; 3044 } 3045 EXPORT_SYMBOL(in_group_or_capable); 3046 3047 /** 3048 * mode_strip_sgid - handle the sgid bit for non-directories 3049 * @idmap: idmap of the mount the inode was created from 3050 * @dir: parent directory inode 3051 * @mode: mode of the file to be created in @dir 3052 * 3053 * If the @mode of the new file has both the S_ISGID and S_IXGRP bit 3054 * raised and @dir has the S_ISGID bit raised ensure that the caller is 3055 * either in the group of the parent directory or they have CAP_FSETID 3056 * in their user namespace and are privileged over the parent directory. 3057 * In all other cases, strip the S_ISGID bit from @mode. 3058 * 3059 * Return: the new mode to use for the file 3060 */ 3061 umode_t mode_strip_sgid(struct mnt_idmap *idmap, 3062 const struct inode *dir, umode_t mode) 3063 { 3064 if ((mode & (S_ISGID | S_IXGRP)) != (S_ISGID | S_IXGRP)) 3065 return mode; 3066 if (S_ISDIR(mode) || !dir || !(dir->i_mode & S_ISGID)) 3067 return mode; 3068 if (in_group_or_capable(idmap, dir, i_gid_into_vfsgid(idmap, dir))) 3069 return mode; 3070 return mode & ~S_ISGID; 3071 } 3072 EXPORT_SYMBOL(mode_strip_sgid); 3073 3074 #ifdef CONFIG_DEBUG_VFS 3075 /** 3076 * dump_inode - dump an inode. 3077 * @inode: inode to dump 3078 * @reason: reason for dumping 3079 * 3080 * If inode is an invalid pointer, we don't want to crash accessing it, 3081 * so probe everything depending on it carefully with get_kernel_nofault(). 3082 */ 3083 void dump_inode(struct inode *inode, const char *reason) 3084 { 3085 struct super_block *sb; 3086 struct file_system_type *s_type; 3087 const char *fs_name_ptr; 3088 char fs_name[32] = {}; 3089 umode_t mode; 3090 unsigned short opflags; 3091 unsigned int flags; 3092 unsigned int state; 3093 int count; 3094 3095 if (get_kernel_nofault(sb, &inode->i_sb) || 3096 get_kernel_nofault(mode, &inode->i_mode) || 3097 get_kernel_nofault(opflags, &inode->i_opflags) || 3098 get_kernel_nofault(flags, &inode->i_flags)) { 3099 pr_warn("%s: unreadable inode:%px\n", reason, inode); 3100 return; 3101 } 3102 3103 state = inode_state_read_once(inode); 3104 count = icount_read_once(inode); 3105 3106 if (!sb || 3107 get_kernel_nofault(s_type, &sb->s_type) || !s_type || 3108 get_kernel_nofault(fs_name_ptr, &s_type->name) || !fs_name_ptr || 3109 strncpy_from_kernel_nofault(fs_name, fs_name_ptr, sizeof(fs_name) - 1) < 0) 3110 strscpy(fs_name, "<unknown, sb unreadable>"); 3111 3112 pr_warn("%s: inode:%px fs:%s mode:%ho opflags:%#x flags:%#x state:%#x count:%d\n", 3113 reason, inode, fs_name, mode, opflags, flags, state, count); 3114 } 3115 EXPORT_SYMBOL(dump_inode); 3116 #endif 3117