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