1 // SPDX-License-Identifier: GPL-2.0-only 1 2 /* 3 * (C) 1997 Linus Torvalds 4 * (C) 1999 Andrea Arcangeli <andrea@suse.de> 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/mount.h> 18 #include <linux/posix_acl.h> 19 #include <linux/buffer_head.h> /* for inode_ha 20 #include <linux/ratelimit.h> 21 #include <linux/list_lru.h> 22 #include <linux/iversion.h> 23 #include <linux/rw_hint.h> 24 #include <trace/events/writeback.h> 25 #include "internal.h" 26 27 /* 28 * Inode locking rules: 29 * 30 * inode->i_lock protects: 31 * inode->i_state, inode->i_hash, __iget(), 32 * Inode LRU list locks protect: 33 * inode->i_sb->s_inode_lru, inode->i_lru 34 * inode->i_sb->s_inode_list_lock protects: 35 * inode->i_sb->s_inodes, inode->i_sb_list 36 * bdi->wb.list_lock protects: 37 * bdi->wb.b_{dirty,io,more_io,dirty_time}, 38 * inode_hash_lock protects: 39 * inode_hashtable, inode->i_hash 40 * 41 * Lock ordering: 42 * 43 * inode->i_sb->s_inode_list_lock 44 * inode->i_lock 45 * Inode LRU list locks 46 * 47 * bdi->wb.list_lock 48 * inode->i_lock 49 * 50 * inode_hash_lock 51 * inode->i_sb->s_inode_list_lock 52 * inode->i_lock 53 * 54 * iunique_lock 55 * inode_hash_lock 56 */ 57 58 static unsigned int i_hash_mask __ro_after_ini 59 static unsigned int i_hash_shift __ro_after_in 60 static struct hlist_head *inode_hashtable __ro 61 static __cacheline_aligned_in_smp DEFINE_SPINL 62 63 /* 64 * Empty aops. Can be used for the cases where 65 * define any of the address_space operations. 66 */ 67 const struct address_space_operations empty_ao 68 }; 69 EXPORT_SYMBOL(empty_aops); 70 71 static DEFINE_PER_CPU(unsigned long, nr_inodes 72 static DEFINE_PER_CPU(unsigned long, nr_unused 73 74 static struct kmem_cache *inode_cachep __ro_af 75 76 static long get_nr_inodes(void) 77 { 78 int i; 79 long sum = 0; 80 for_each_possible_cpu(i) 81 sum += per_cpu(nr_inodes, i); 82 return sum < 0 ? 0 : sum; 83 } 84 85 static inline long get_nr_inodes_unused(void) 86 { 87 int i; 88 long sum = 0; 89 for_each_possible_cpu(i) 90 sum += per_cpu(nr_unused, i); 91 return sum < 0 ? 0 : sum; 92 } 93 94 long get_nr_dirty_inodes(void) 95 { 96 /* not actually dirty inodes, but a wi 97 long nr_dirty = get_nr_inodes() - get_ 98 return nr_dirty > 0 ? nr_dirty : 0; 99 } 100 101 /* 102 * Handle nr_inode sysctl 103 */ 104 #ifdef CONFIG_SYSCTL 105 /* 106 * Statistics gathering.. 107 */ 108 static struct inodes_stat_t inodes_stat; 109 110 static int proc_nr_inodes(const struct ctl_tab 111 size_t *lenp, loff_t 112 { 113 inodes_stat.nr_inodes = get_nr_inodes( 114 inodes_stat.nr_unused = get_nr_inodes_ 115 return proc_doulongvec_minmax(table, w 116 } 117 118 static struct ctl_table inodes_sysctls[] = { 119 { 120 .procname = "inode-nr", 121 .data = &inodes_stat 122 .maxlen = 2*sizeof(lon 123 .mode = 0444, 124 .proc_handler = proc_nr_inod 125 }, 126 { 127 .procname = "inode-state 128 .data = &inodes_stat 129 .maxlen = 7*sizeof(lon 130 .mode = 0444, 131 .proc_handler = proc_nr_inod 132 }, 133 }; 134 135 static int __init init_fs_inode_sysctls(void) 136 { 137 register_sysctl_init("fs", inodes_sysc 138 return 0; 139 } 140 early_initcall(init_fs_inode_sysctls); 141 #endif 142 143 static int no_open(struct inode *inode, struct 144 { 145 return -ENXIO; 146 } 147 148 /** 149 * inode_init_always_gfp - perform inode struc 150 * @sb: superblock inode belongs to 151 * @inode: inode to initialise 152 * @gfp: allocation flags 153 * 154 * These are initializations that need to be d 155 * allocation as the fields are not initialise 156 * If there are additional allocations require 157 */ 158 int inode_init_always_gfp(struct super_block * 159 { 160 static const struct inode_operations e 161 static const struct file_operations no 162 struct address_space *const mapping = 163 164 inode->i_sb = sb; 165 inode->i_blkbits = sb->s_blocksize_bit 166 inode->i_flags = 0; 167 inode->i_state = 0; 168 atomic64_set(&inode->i_sequence, 0); 169 atomic_set(&inode->i_count, 1); 170 inode->i_op = &empty_iops; 171 inode->i_fop = &no_open_fops; 172 inode->i_ino = 0; 173 inode->__i_nlink = 1; 174 inode->i_opflags = 0; 175 if (sb->s_xattr) 176 inode->i_opflags |= IOP_XATTR; 177 i_uid_write(inode, 0); 178 i_gid_write(inode, 0); 179 atomic_set(&inode->i_writecount, 0); 180 inode->i_size = 0; 181 inode->i_write_hint = WRITE_LIFE_NOT_S 182 inode->i_blocks = 0; 183 inode->i_bytes = 0; 184 inode->i_generation = 0; 185 inode->i_pipe = NULL; 186 inode->i_cdev = NULL; 187 inode->i_link = NULL; 188 inode->i_dir_seq = 0; 189 inode->i_rdev = 0; 190 inode->dirtied_when = 0; 191 192 #ifdef CONFIG_CGROUP_WRITEBACK 193 inode->i_wb_frn_winner = 0; 194 inode->i_wb_frn_avg_time = 0; 195 inode->i_wb_frn_history = 0; 196 #endif 197 198 spin_lock_init(&inode->i_lock); 199 lockdep_set_class(&inode->i_lock, &sb- 200 201 init_rwsem(&inode->i_rwsem); 202 lockdep_set_class(&inode->i_rwsem, &sb 203 204 atomic_set(&inode->i_dio_count, 0); 205 206 mapping->a_ops = &empty_aops; 207 mapping->host = inode; 208 mapping->flags = 0; 209 mapping->wb_err = 0; 210 atomic_set(&mapping->i_mmap_writable, 211 #ifdef CONFIG_READ_ONLY_THP_FOR_FS 212 atomic_set(&mapping->nr_thps, 0); 213 #endif 214 mapping_set_gfp_mask(mapping, GFP_HIGH 215 mapping->i_private_data = NULL; 216 mapping->writeback_index = 0; 217 init_rwsem(&mapping->invalidate_lock); 218 lockdep_set_class_and_name(&mapping->i 219 &sb->s_type 220 "mapping.in 221 if (sb->s_iflags & SB_I_STABLE_WRITES) 222 mapping_set_stable_writes(mapp 223 inode->i_private = NULL; 224 inode->i_mapping = mapping; 225 INIT_HLIST_HEAD(&inode->i_dentry); 226 #ifdef CONFIG_FS_POSIX_ACL 227 inode->i_acl = inode->i_default_acl = 228 #endif 229 230 #ifdef CONFIG_FSNOTIFY 231 inode->i_fsnotify_mask = 0; 232 #endif 233 inode->i_flctx = NULL; 234 235 if (unlikely(security_inode_alloc(inod 236 return -ENOMEM; 237 238 this_cpu_inc(nr_inodes); 239 240 return 0; 241 } 242 EXPORT_SYMBOL(inode_init_always_gfp); 243 244 void free_inode_nonrcu(struct inode *inode) 245 { 246 kmem_cache_free(inode_cachep, inode); 247 } 248 EXPORT_SYMBOL(free_inode_nonrcu); 249 250 static void i_callback(struct rcu_head *head) 251 { 252 struct inode *inode = container_of(hea 253 if (inode->free_inode) 254 inode->free_inode(inode); 255 else 256 free_inode_nonrcu(inode); 257 } 258 259 static struct inode *alloc_inode(struct super_ 260 { 261 const struct super_operations *ops = s 262 struct inode *inode; 263 264 if (ops->alloc_inode) 265 inode = ops->alloc_inode(sb); 266 else 267 inode = alloc_inode_sb(sb, ino 268 269 if (!inode) 270 return NULL; 271 272 if (unlikely(inode_init_always(sb, ino 273 if (ops->destroy_inode) { 274 ops->destroy_inode(ino 275 if (!ops->free_inode) 276 return NULL; 277 } 278 inode->free_inode = ops->free_ 279 i_callback(&inode->i_rcu); 280 return NULL; 281 } 282 283 return inode; 284 } 285 286 void __destroy_inode(struct inode *inode) 287 { 288 BUG_ON(inode_has_buffers(inode)); 289 inode_detach_wb(inode); 290 security_inode_free(inode); 291 fsnotify_inode_delete(inode); 292 locks_free_lock_context(inode); 293 if (!inode->i_nlink) { 294 WARN_ON(atomic_long_read(&inod 295 atomic_long_dec(&inode->i_sb-> 296 } 297 298 #ifdef CONFIG_FS_POSIX_ACL 299 if (inode->i_acl && !is_uncached_acl(i 300 posix_acl_release(inode->i_acl 301 if (inode->i_default_acl && !is_uncach 302 posix_acl_release(inode->i_def 303 #endif 304 this_cpu_dec(nr_inodes); 305 } 306 EXPORT_SYMBOL(__destroy_inode); 307 308 static void destroy_inode(struct inode *inode) 309 { 310 const struct super_operations *ops = i 311 312 BUG_ON(!list_empty(&inode->i_lru)); 313 __destroy_inode(inode); 314 if (ops->destroy_inode) { 315 ops->destroy_inode(inode); 316 if (!ops->free_inode) 317 return; 318 } 319 inode->free_inode = ops->free_inode; 320 call_rcu(&inode->i_rcu, i_callback); 321 } 322 323 /** 324 * drop_nlink - directly drop an inode's link 325 * @inode: inode 326 * 327 * This is a low-level filesystem helper to re 328 * direct filesystem manipulation of i_nlink. 329 * where we are attempting to track writes to 330 * filesystem, a decrement to zero means an im 331 * write when the file is truncated and actual 332 * on the filesystem. 333 */ 334 void drop_nlink(struct inode *inode) 335 { 336 WARN_ON(inode->i_nlink == 0); 337 inode->__i_nlink--; 338 if (!inode->i_nlink) 339 atomic_long_inc(&inode->i_sb-> 340 } 341 EXPORT_SYMBOL(drop_nlink); 342 343 /** 344 * clear_nlink - directly zero an inode's link 345 * @inode: inode 346 * 347 * This is a low-level filesystem helper to re 348 * direct filesystem manipulation of i_nlink. 349 * drop_nlink() for why we care about i_nlink 350 */ 351 void clear_nlink(struct inode *inode) 352 { 353 if (inode->i_nlink) { 354 inode->__i_nlink = 0; 355 atomic_long_inc(&inode->i_sb-> 356 } 357 } 358 EXPORT_SYMBOL(clear_nlink); 359 360 /** 361 * set_nlink - directly set an inode's link co 362 * @inode: inode 363 * @nlink: new nlink (should be non-zero) 364 * 365 * This is a low-level filesystem helper to re 366 * direct filesystem manipulation of i_nlink. 367 */ 368 void set_nlink(struct inode *inode, unsigned i 369 { 370 if (!nlink) { 371 clear_nlink(inode); 372 } else { 373 /* Yes, some filesystems do ch 374 if (inode->i_nlink == 0) 375 atomic_long_dec(&inode 376 377 inode->__i_nlink = nlink; 378 } 379 } 380 EXPORT_SYMBOL(set_nlink); 381 382 /** 383 * inc_nlink - directly increment an inode's l 384 * @inode: inode 385 * 386 * This is a low-level filesystem helper to re 387 * direct filesystem manipulation of i_nlink. 388 * it is only here for parity with dec_nlink() 389 */ 390 void inc_nlink(struct inode *inode) 391 { 392 if (unlikely(inode->i_nlink == 0)) { 393 WARN_ON(!(inode->i_state & I_L 394 atomic_long_dec(&inode->i_sb-> 395 } 396 397 inode->__i_nlink++; 398 } 399 EXPORT_SYMBOL(inc_nlink); 400 401 static void __address_space_init_once(struct a 402 { 403 xa_init_flags(&mapping->i_pages, XA_FL 404 init_rwsem(&mapping->i_mmap_rwsem); 405 INIT_LIST_HEAD(&mapping->i_private_lis 406 spin_lock_init(&mapping->i_private_loc 407 mapping->i_mmap = RB_ROOT_CACHED; 408 } 409 410 void address_space_init_once(struct address_sp 411 { 412 memset(mapping, 0, sizeof(*mapping)); 413 __address_space_init_once(mapping); 414 } 415 EXPORT_SYMBOL(address_space_init_once); 416 417 /* 418 * These are initializations that only need to 419 * once, because the fields are idempotent acr 420 * of the inode, so let the slab aware of that 421 */ 422 void inode_init_once(struct inode *inode) 423 { 424 memset(inode, 0, sizeof(*inode)); 425 INIT_HLIST_NODE(&inode->i_hash); 426 INIT_LIST_HEAD(&inode->i_devices); 427 INIT_LIST_HEAD(&inode->i_io_list); 428 INIT_LIST_HEAD(&inode->i_wb_list); 429 INIT_LIST_HEAD(&inode->i_lru); 430 INIT_LIST_HEAD(&inode->i_sb_list); 431 __address_space_init_once(&inode->i_da 432 i_size_ordered_init(inode); 433 } 434 EXPORT_SYMBOL(inode_init_once); 435 436 static void init_once(void *foo) 437 { 438 struct inode *inode = (struct inode *) 439 440 inode_init_once(inode); 441 } 442 443 /* 444 * get additional reference to inode; caller m 445 */ 446 void ihold(struct inode *inode) 447 { 448 WARN_ON(atomic_inc_return(&inode->i_co 449 } 450 EXPORT_SYMBOL(ihold); 451 452 static void __inode_add_lru(struct inode *inod 453 { 454 if (inode->i_state & (I_DIRTY_ALL | I_ 455 return; 456 if (atomic_read(&inode->i_count)) 457 return; 458 if (!(inode->i_sb->s_flags & SB_ACTIVE 459 return; 460 if (!mapping_shrinkable(&inode->i_data 461 return; 462 463 if (list_lru_add_obj(&inode->i_sb->s_i 464 this_cpu_inc(nr_unused); 465 else if (rotate) 466 inode->i_state |= I_REFERENCED 467 } 468 469 struct wait_queue_head *inode_bit_waitqueue(st 470 st 471 { 472 void *bit_address; 473 474 bit_address = inode_state_wait_address 475 init_wait_var_entry(wqe, bit_address, 476 return __var_waitqueue(bit_address); 477 } 478 EXPORT_SYMBOL(inode_bit_waitqueue); 479 480 /* 481 * Add inode to LRU if needed (inode is unused 482 * 483 * Needs inode->i_lock held. 484 */ 485 void inode_add_lru(struct inode *inode) 486 { 487 __inode_add_lru(inode, false); 488 } 489 490 static void inode_lru_list_del(struct inode *i 491 { 492 if (list_lru_del_obj(&inode->i_sb->s_i 493 this_cpu_dec(nr_unused); 494 } 495 496 static void inode_pin_lru_isolating(struct ino 497 { 498 lockdep_assert_held(&inode->i_lock); 499 WARN_ON(inode->i_state & (I_LRU_ISOLAT 500 inode->i_state |= I_LRU_ISOLATING; 501 } 502 503 static void inode_unpin_lru_isolating(struct i 504 { 505 spin_lock(&inode->i_lock); 506 WARN_ON(!(inode->i_state & I_LRU_ISOLA 507 inode->i_state &= ~I_LRU_ISOLATING; 508 /* Called with inode->i_lock which ens 509 inode_wake_up_bit(inode, __I_LRU_ISOLA 510 spin_unlock(&inode->i_lock); 511 } 512 513 static void inode_wait_for_lru_isolating(struc 514 { 515 struct wait_bit_queue_entry wqe; 516 struct wait_queue_head *wq_head; 517 518 lockdep_assert_held(&inode->i_lock); 519 if (!(inode->i_state & I_LRU_ISOLATING 520 return; 521 522 wq_head = inode_bit_waitqueue(&wqe, in 523 for (;;) { 524 prepare_to_wait_event(wq_head, 525 /* 526 * Checking I_LRU_ISOLATING wi 527 * memory ordering. 528 */ 529 if (!(inode->i_state & I_LRU_I 530 break; 531 spin_unlock(&inode->i_lock); 532 schedule(); 533 spin_lock(&inode->i_lock); 534 } 535 finish_wait(wq_head, &wqe.wq_entry); 536 WARN_ON(inode->i_state & I_LRU_ISOLATI 537 } 538 539 /** 540 * inode_sb_list_add - add inode to the superb 541 * @inode: inode to add 542 */ 543 void inode_sb_list_add(struct inode *inode) 544 { 545 spin_lock(&inode->i_sb->s_inode_list_l 546 list_add(&inode->i_sb_list, &inode->i_ 547 spin_unlock(&inode->i_sb->s_inode_list 548 } 549 EXPORT_SYMBOL_GPL(inode_sb_list_add); 550 551 static inline void inode_sb_list_del(struct in 552 { 553 if (!list_empty(&inode->i_sb_list)) { 554 spin_lock(&inode->i_sb->s_inod 555 list_del_init(&inode->i_sb_lis 556 spin_unlock(&inode->i_sb->s_in 557 } 558 } 559 560 static unsigned long hash(struct super_block * 561 { 562 unsigned long tmp; 563 564 tmp = (hashval * (unsigned long)sb) ^ 565 L1_CACHE_BYTES; 566 tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME 567 return tmp & i_hash_mask; 568 } 569 570 /** 571 * __insert_inode_hash - hash an inode 572 * @inode: unhashed inode 573 * @hashval: unsigned long value used to 574 * inode_hashtable. 575 * 576 * Add an inode to the inode hash for thi 577 */ 578 void __insert_inode_hash(struct inode *inode, 579 { 580 struct hlist_head *b = inode_hashtable 581 582 spin_lock(&inode_hash_lock); 583 spin_lock(&inode->i_lock); 584 hlist_add_head_rcu(&inode->i_hash, b); 585 spin_unlock(&inode->i_lock); 586 spin_unlock(&inode_hash_lock); 587 } 588 EXPORT_SYMBOL(__insert_inode_hash); 589 590 /** 591 * __remove_inode_hash - remove an inode 592 * @inode: inode to unhash 593 * 594 * Remove an inode from the superblock. 595 */ 596 void __remove_inode_hash(struct inode *inode) 597 { 598 spin_lock(&inode_hash_lock); 599 spin_lock(&inode->i_lock); 600 hlist_del_init_rcu(&inode->i_hash); 601 spin_unlock(&inode->i_lock); 602 spin_unlock(&inode_hash_lock); 603 } 604 EXPORT_SYMBOL(__remove_inode_hash); 605 606 void dump_mapping(const struct address_space * 607 { 608 struct inode *host; 609 const struct address_space_operations 610 struct hlist_node *dentry_first; 611 struct dentry *dentry_ptr; 612 struct dentry dentry; 613 char fname[64] = {}; 614 unsigned long ino; 615 616 /* 617 * If mapping is an invalid pointer, w 618 * accessing it, so probe everything d 619 */ 620 if (get_kernel_nofault(host, &mapping- 621 get_kernel_nofault(a_ops, &mapping 622 pr_warn("invalid mapping:%px\n 623 return; 624 } 625 626 if (!host) { 627 pr_warn("aops:%ps\n", a_ops); 628 return; 629 } 630 631 if (get_kernel_nofault(dentry_first, & 632 get_kernel_nofault(ino, &host->i_i 633 pr_warn("aops:%ps invalid inod 634 return; 635 } 636 637 if (!dentry_first) { 638 pr_warn("aops:%ps ino:%lx\n", 639 return; 640 } 641 642 dentry_ptr = container_of(dentry_first 643 if (get_kernel_nofault(dentry, dentry_ 644 !dentry.d_parent || !dentry.d_name 645 pr_warn("aops:%ps ino:%lx inva 646 a_ops, ino, de 647 return; 648 } 649 650 if (strncpy_from_kernel_nofault(fname, 651 strscpy(fname, "<invalid>"); 652 /* 653 * Even if strncpy_from_kernel_nofault 654 * the fname could be unreliable 655 */ 656 pr_warn("aops:%ps ino:%lx dentry name( 657 a_ops, ino, fname); 658 } 659 660 void clear_inode(struct inode *inode) 661 { 662 /* 663 * We have to cycle the i_pages lock h 664 * process of removing the last page ( 665 * and we must not free the mapping un 666 */ 667 xa_lock_irq(&inode->i_data.i_pages); 668 BUG_ON(inode->i_data.nrpages); 669 /* 670 * Almost always, mapping_empty(&inode 671 * two known and long-standing ways in 672 * (when deep radix-tree node allocati 673 * collapse_file() failed). Until thos 674 * or a cleanup function is called her 675 * nor even WARN_ON(!mapping_empty). 676 */ 677 xa_unlock_irq(&inode->i_data.i_pages); 678 BUG_ON(!list_empty(&inode->i_data.i_pr 679 BUG_ON(!(inode->i_state & I_FREEING)); 680 BUG_ON(inode->i_state & I_CLEAR); 681 BUG_ON(!list_empty(&inode->i_wb_list)) 682 /* don't need i_lock here, no concurre 683 inode->i_state = I_FREEING | I_CLEAR; 684 } 685 EXPORT_SYMBOL(clear_inode); 686 687 /* 688 * Free the inode passed in, removing it from 689 * to. We remove any pages still attached to t 690 * is still in progress before finally destroy 691 * 692 * An inode must already be marked I_FREEING s 693 * moved back onto lists if we race with other 694 * (e.g. writeback_single_inode). The caller i 695 * 696 * An inode must already be removed from the L 697 * the cache. This should occur atomically wit 698 * flag, so no inodes here should ever be on t 699 */ 700 static void evict(struct inode *inode) 701 { 702 const struct super_operations *op = in 703 704 BUG_ON(!(inode->i_state & I_FREEING)); 705 BUG_ON(!list_empty(&inode->i_lru)); 706 707 if (!list_empty(&inode->i_io_list)) 708 inode_io_list_del(inode); 709 710 inode_sb_list_del(inode); 711 712 spin_lock(&inode->i_lock); 713 inode_wait_for_lru_isolating(inode); 714 715 /* 716 * Wait for flusher thread to be done 717 * does not start destroying it while 718 * the inode has I_FREEING set, flushe 719 * the inode. We just have to wait fo 720 */ 721 inode_wait_for_writeback(inode); 722 spin_unlock(&inode->i_lock); 723 724 if (op->evict_inode) { 725 op->evict_inode(inode); 726 } else { 727 truncate_inode_pages_final(&in 728 clear_inode(inode); 729 } 730 if (S_ISCHR(inode->i_mode) && inode->i 731 cd_forget(inode); 732 733 remove_inode_hash(inode); 734 735 /* 736 * Wake up waiters in __wait_on_freein 737 * 738 * Lockless hash lookup may end up fin 739 * it above, but only lock it *after* 740 * In this case the potential waiter c 741 * 742 * The inode being unhashed after the 743 * used as an indicator whether blocki 744 */ 745 spin_lock(&inode->i_lock); 746 /* 747 * Pairs with the barrier in prepare_t 748 * ___wait_var_event() either sees the 749 * waitqueue_active() check in wake_up 750 */ 751 smp_mb(); 752 inode_wake_up_bit(inode, __I_NEW); 753 BUG_ON(inode->i_state != (I_FREEING | 754 spin_unlock(&inode->i_lock); 755 756 destroy_inode(inode); 757 } 758 759 /* 760 * dispose_list - dispose of the contents of a 761 * @head: the head of the list to free 762 * 763 * Dispose-list gets a local list with local i 764 * need to worry about list corruption and SMP 765 */ 766 static void dispose_list(struct list_head *hea 767 { 768 while (!list_empty(head)) { 769 struct inode *inode; 770 771 inode = list_first_entry(head, 772 list_del_init(&inode->i_lru); 773 774 evict(inode); 775 cond_resched(); 776 } 777 } 778 779 /** 780 * evict_inodes - evict all evictable inodes f 781 * @sb: superblock to operate on 782 * 783 * Make sure that no inodes with zero refcount 784 * called by superblock shutdown after having 785 * so any inode reaching zero refcount during 786 * be immediately evicted. 787 */ 788 void evict_inodes(struct super_block *sb) 789 { 790 struct inode *inode, *next; 791 LIST_HEAD(dispose); 792 793 again: 794 spin_lock(&sb->s_inode_list_lock); 795 list_for_each_entry_safe(inode, next, 796 if (atomic_read(&inode->i_coun 797 continue; 798 799 spin_lock(&inode->i_lock); 800 if (atomic_read(&inode->i_coun 801 spin_unlock(&inode->i_ 802 continue; 803 } 804 if (inode->i_state & (I_NEW | 805 spin_unlock(&inode->i_ 806 continue; 807 } 808 809 inode->i_state |= I_FREEING; 810 inode_lru_list_del(inode); 811 spin_unlock(&inode->i_lock); 812 list_add(&inode->i_lru, &dispo 813 814 /* 815 * We can have a ton of inodes 816 * enough memory, check to see 817 * bit so we don't livelock. 818 */ 819 if (need_resched()) { 820 spin_unlock(&sb->s_ino 821 cond_resched(); 822 dispose_list(&dispose) 823 goto again; 824 } 825 } 826 spin_unlock(&sb->s_inode_list_lock); 827 828 dispose_list(&dispose); 829 } 830 EXPORT_SYMBOL_GPL(evict_inodes); 831 832 /** 833 * invalidate_inodes - attempt to free all 834 * @sb: superblock to operate on 835 * 836 * Attempts to free all inodes (including dirt 837 */ 838 void invalidate_inodes(struct super_block *sb) 839 { 840 struct inode *inode, *next; 841 LIST_HEAD(dispose); 842 843 again: 844 spin_lock(&sb->s_inode_list_lock); 845 list_for_each_entry_safe(inode, next, 846 spin_lock(&inode->i_lock); 847 if (inode->i_state & (I_NEW | 848 spin_unlock(&inode->i_ 849 continue; 850 } 851 if (atomic_read(&inode->i_coun 852 spin_unlock(&inode->i_ 853 continue; 854 } 855 856 inode->i_state |= I_FREEING; 857 inode_lru_list_del(inode); 858 spin_unlock(&inode->i_lock); 859 list_add(&inode->i_lru, &dispo 860 if (need_resched()) { 861 spin_unlock(&sb->s_ino 862 cond_resched(); 863 dispose_list(&dispose) 864 goto again; 865 } 866 } 867 spin_unlock(&sb->s_inode_list_lock); 868 869 dispose_list(&dispose); 870 } 871 872 /* 873 * Isolate the inode from the LRU in preparati 874 * 875 * If the inode has the I_REFERENCED flag set, 876 * used recently - the flag is set in iput_fin 877 * inode, clear the flag and move it to the ba 878 * pass through the LRU before it gets reclaim 879 * the fact we are doing lazy LRU updates to m 880 * LRU does not have strict ordering. Hence we 881 * with this flag set because they are the ino 882 */ 883 static enum lru_status inode_lru_isolate(struc 884 struct list_lru_one *lru, spin 885 { 886 struct list_head *freeable = arg; 887 struct inode *inode = container_of( 888 889 /* 890 * We are inverting the lru lock/inode 891 * trylock. If we fail to get the lock 892 */ 893 if (!spin_trylock(&inode->i_lock)) 894 return LRU_SKIP; 895 896 /* 897 * Inodes can get referenced, redirtie 898 * they're already on the LRU, and thi 899 * unreclaimable for a while. Remove t 900 * sync, or the last page cache deleti 901 */ 902 if (atomic_read(&inode->i_count) || 903 (inode->i_state & ~I_REFERENCED) | 904 !mapping_shrinkable(&inode->i_data 905 list_lru_isolate(lru, &inode-> 906 spin_unlock(&inode->i_lock); 907 this_cpu_dec(nr_unused); 908 return LRU_REMOVED; 909 } 910 911 /* Recently referenced inodes get one 912 if (inode->i_state & I_REFERENCED) { 913 inode->i_state &= ~I_REFERENCE 914 spin_unlock(&inode->i_lock); 915 return LRU_ROTATE; 916 } 917 918 /* 919 * On highmem systems, mapping_shrinka 920 * page cache in order to free up stru 921 * be under pressure before the cache 922 */ 923 if (inode_has_buffers(inode) || !mappi 924 inode_pin_lru_isolating(inode) 925 spin_unlock(&inode->i_lock); 926 spin_unlock(lru_lock); 927 if (remove_inode_buffers(inode 928 unsigned long reap; 929 reap = invalidate_mapp 930 if (current_is_kswapd( 931 __count_vm_eve 932 else 933 __count_vm_eve 934 mm_account_reclaimed_p 935 } 936 inode_unpin_lru_isolating(inod 937 spin_lock(lru_lock); 938 return LRU_RETRY; 939 } 940 941 WARN_ON(inode->i_state & I_NEW); 942 inode->i_state |= I_FREEING; 943 list_lru_isolate_move(lru, &inode->i_l 944 spin_unlock(&inode->i_lock); 945 946 this_cpu_dec(nr_unused); 947 return LRU_REMOVED; 948 } 949 950 /* 951 * Walk the superblock inode LRU for freeable 952 * This is called from the superblock shrinker 953 * to trim from the LRU. Inodes to be freed ar 954 * then are freed outside inode_lock by dispos 955 */ 956 long prune_icache_sb(struct super_block *sb, s 957 { 958 LIST_HEAD(freeable); 959 long freed; 960 961 freed = list_lru_shrink_walk(&sb->s_in 962 inode_lru 963 dispose_list(&freeable); 964 return freed; 965 } 966 967 static void __wait_on_freeing_inode(struct ino 968 /* 969 * Called with the inode lock held. 970 */ 971 static struct inode *find_inode(struct super_b 972 struct hlist_h 973 int (*test)(st 974 void *data, bo 975 { 976 struct inode *inode = NULL; 977 978 if (is_inode_hash_locked) 979 lockdep_assert_held(&inode_has 980 else 981 lockdep_assert_not_held(&inode 982 983 rcu_read_lock(); 984 repeat: 985 hlist_for_each_entry_rcu(inode, head, 986 if (inode->i_sb != sb) 987 continue; 988 if (!test(inode, data)) 989 continue; 990 spin_lock(&inode->i_lock); 991 if (inode->i_state & (I_FREEIN 992 __wait_on_freeing_inod 993 goto repeat; 994 } 995 if (unlikely(inode->i_state & 996 spin_unlock(&inode->i_ 997 rcu_read_unlock(); 998 return ERR_PTR(-ESTALE 999 } 1000 __iget(inode); 1001 spin_unlock(&inode->i_lock); 1002 rcu_read_unlock(); 1003 return inode; 1004 } 1005 rcu_read_unlock(); 1006 return NULL; 1007 } 1008 1009 /* 1010 * find_inode_fast is the fast path version o 1011 * iget_locked for details. 1012 */ 1013 static struct inode *find_inode_fast(struct s 1014 struct hlist_ 1015 bool is_inode 1016 { 1017 struct inode *inode = NULL; 1018 1019 if (is_inode_hash_locked) 1020 lockdep_assert_held(&inode_ha 1021 else 1022 lockdep_assert_not_held(&inod 1023 1024 rcu_read_lock(); 1025 repeat: 1026 hlist_for_each_entry_rcu(inode, head, 1027 if (inode->i_ino != ino) 1028 continue; 1029 if (inode->i_sb != sb) 1030 continue; 1031 spin_lock(&inode->i_lock); 1032 if (inode->i_state & (I_FREEI 1033 __wait_on_freeing_ino 1034 goto repeat; 1035 } 1036 if (unlikely(inode->i_state & 1037 spin_unlock(&inode->i 1038 rcu_read_unlock(); 1039 return ERR_PTR(-ESTAL 1040 } 1041 __iget(inode); 1042 spin_unlock(&inode->i_lock); 1043 rcu_read_unlock(); 1044 return inode; 1045 } 1046 rcu_read_unlock(); 1047 return NULL; 1048 } 1049 1050 /* 1051 * Each cpu owns a range of LAST_INO_BATCH nu 1052 * 'shared_last_ino' is dirtied only once out 1053 * to renew the exhausted range. 1054 * 1055 * This does not significantly increase overf 1056 * consume at most LAST_INO_BATCH-1 unused in 1057 * NR_CPUS*(LAST_INO_BATCH-1) wastage. At 409 1058 * 2^32 range, and is a worst-case. Even a 50 1059 * overflow rate by 2x, which does not seem t 1060 * 1061 * On a 32bit, non LFS stat() call, glibc wil 1062 * error if st_ino won't fit in target struct 1063 * here to attempt to avoid that. 1064 */ 1065 #define LAST_INO_BATCH 1024 1066 static DEFINE_PER_CPU(unsigned int, last_ino) 1067 1068 unsigned int get_next_ino(void) 1069 { 1070 unsigned int *p = &get_cpu_var(last_i 1071 unsigned int res = *p; 1072 1073 #ifdef CONFIG_SMP 1074 if (unlikely((res & (LAST_INO_BATCH-1 1075 static atomic_t shared_last_i 1076 int next = atomic_add_return( 1077 1078 res = next - LAST_INO_BATCH; 1079 } 1080 #endif 1081 1082 res++; 1083 /* get_next_ino should not provide a 1084 if (unlikely(!res)) 1085 res++; 1086 *p = res; 1087 put_cpu_var(last_ino); 1088 return res; 1089 } 1090 EXPORT_SYMBOL(get_next_ino); 1091 1092 /** 1093 * new_inode_pseudo - obtain an i 1094 * @sb: superblock 1095 * 1096 * Allocates a new inode for given super 1097 * Inode wont be chained in superblock s 1098 * This means : 1099 * - fs can't be unmount 1100 * - quotas, fsnotify, writeback can't w 1101 */ 1102 struct inode *new_inode_pseudo(struct super_b 1103 { 1104 return alloc_inode(sb); 1105 } 1106 1107 /** 1108 * new_inode - obtain an inode 1109 * @sb: superblock 1110 * 1111 * Allocates a new inode for given super 1112 * for allocations related to inode->i_m 1113 * If HIGHMEM pages are unsuitable or it 1114 * for the page cache are not reclaimabl 1115 * mapping_set_gfp_mask() must be called 1116 * newly created inode's mapping 1117 * 1118 */ 1119 struct inode *new_inode(struct super_block *s 1120 { 1121 struct inode *inode; 1122 1123 inode = new_inode_pseudo(sb); 1124 if (inode) 1125 inode_sb_list_add(inode); 1126 return inode; 1127 } 1128 EXPORT_SYMBOL(new_inode); 1129 1130 #ifdef CONFIG_DEBUG_LOCK_ALLOC 1131 void lockdep_annotate_inode_mutex_key(struct 1132 { 1133 if (S_ISDIR(inode->i_mode)) { 1134 struct file_system_type *type 1135 1136 /* Set new key only if filesy 1137 if (lockdep_match_class(&inod 1138 /* 1139 * ensure nobody is a 1140 */ 1141 // mutex_destroy(&ino 1142 init_rwsem(&inode->i_ 1143 lockdep_set_class(&in 1144 &ty 1145 } 1146 } 1147 } 1148 EXPORT_SYMBOL(lockdep_annotate_inode_mutex_ke 1149 #endif 1150 1151 /** 1152 * unlock_new_inode - clear the I_NEW state a 1153 * @inode: new inode to unlock 1154 * 1155 * Called when the inode is fully initialised 1156 * inode and wake up anyone waiting for the i 1157 */ 1158 void unlock_new_inode(struct inode *inode) 1159 { 1160 lockdep_annotate_inode_mutex_key(inod 1161 spin_lock(&inode->i_lock); 1162 WARN_ON(!(inode->i_state & I_NEW)); 1163 inode->i_state &= ~I_NEW & ~I_CREATIN 1164 /* 1165 * Pairs with the barrier in prepare_ 1166 * ___wait_var_event() either sees th 1167 * waitqueue_active() check in wake_u 1168 */ 1169 smp_mb(); 1170 inode_wake_up_bit(inode, __I_NEW); 1171 spin_unlock(&inode->i_lock); 1172 } 1173 EXPORT_SYMBOL(unlock_new_inode); 1174 1175 void discard_new_inode(struct inode *inode) 1176 { 1177 lockdep_annotate_inode_mutex_key(inod 1178 spin_lock(&inode->i_lock); 1179 WARN_ON(!(inode->i_state & I_NEW)); 1180 inode->i_state &= ~I_NEW; 1181 /* 1182 * Pairs with the barrier in prepare_ 1183 * ___wait_var_event() either sees th 1184 * waitqueue_active() check in wake_u 1185 */ 1186 smp_mb(); 1187 inode_wake_up_bit(inode, __I_NEW); 1188 spin_unlock(&inode->i_lock); 1189 iput(inode); 1190 } 1191 EXPORT_SYMBOL(discard_new_inode); 1192 1193 /** 1194 * lock_two_nondirectories - take two i_mutex 1195 * 1196 * Lock any non-NULL argument. Passed objects 1197 * Zero, one or two objects may be locked by 1198 * 1199 * @inode1: first inode to lock 1200 * @inode2: second inode to lock 1201 */ 1202 void lock_two_nondirectories(struct inode *in 1203 { 1204 if (inode1) 1205 WARN_ON_ONCE(S_ISDIR(inode1-> 1206 if (inode2) 1207 WARN_ON_ONCE(S_ISDIR(inode2-> 1208 if (inode1 > inode2) 1209 swap(inode1, inode2); 1210 if (inode1) 1211 inode_lock(inode1); 1212 if (inode2 && inode2 != inode1) 1213 inode_lock_nested(inode2, I_M 1214 } 1215 EXPORT_SYMBOL(lock_two_nondirectories); 1216 1217 /** 1218 * unlock_two_nondirectories - release locks 1219 * @inode1: first inode to unlock 1220 * @inode2: second inode to unlock 1221 */ 1222 void unlock_two_nondirectories(struct inode * 1223 { 1224 if (inode1) { 1225 WARN_ON_ONCE(S_ISDIR(inode1-> 1226 inode_unlock(inode1); 1227 } 1228 if (inode2 && inode2 != inode1) { 1229 WARN_ON_ONCE(S_ISDIR(inode2-> 1230 inode_unlock(inode2); 1231 } 1232 } 1233 EXPORT_SYMBOL(unlock_two_nondirectories); 1234 1235 /** 1236 * inode_insert5 - obtain an inode from a mou 1237 * @inode: pre-allocated inode to use fo 1238 * @hashval: hash value (usually inode num 1239 * @test: callback used for comparisons 1240 * @set: callback used to initialize a 1241 * @data: opaque data pointer to pass t 1242 * 1243 * Search for the inode specified by @hashval 1244 * and if present it is return it with an inc 1245 * a variant of iget5_locked() for callers th 1246 * allocation of inode. 1247 * 1248 * If the inode is not in cache, insert the p 1249 * return it locked, hashed, and with the I_N 1250 * to fill it in before unlocking it via unlo 1251 * 1252 * Note both @test and @set are called with t 1253 * sleep. 1254 */ 1255 struct inode *inode_insert5(struct inode *ino 1256 int (*test)(struc 1257 int (*set)(struct 1258 { 1259 struct hlist_head *head = inode_hasht 1260 struct inode *old; 1261 1262 again: 1263 spin_lock(&inode_hash_lock); 1264 old = find_inode(inode->i_sb, head, t 1265 if (unlikely(old)) { 1266 /* 1267 * Uhhuh, somebody else creat 1268 * Use the old inode instead 1269 */ 1270 spin_unlock(&inode_hash_lock) 1271 if (IS_ERR(old)) 1272 return NULL; 1273 wait_on_inode(old); 1274 if (unlikely(inode_unhashed(o 1275 iput(old); 1276 goto again; 1277 } 1278 return old; 1279 } 1280 1281 if (set && unlikely(set(inode, data)) 1282 inode = NULL; 1283 goto unlock; 1284 } 1285 1286 /* 1287 * Return the locked inode with I_NEW 1288 * caller is responsible for filling 1289 */ 1290 spin_lock(&inode->i_lock); 1291 inode->i_state |= I_NEW; 1292 hlist_add_head_rcu(&inode->i_hash, he 1293 spin_unlock(&inode->i_lock); 1294 1295 /* 1296 * Add inode to the sb list if it's n 1297 * point, so it should be safe to tes 1298 */ 1299 if (list_empty(&inode->i_sb_list)) 1300 inode_sb_list_add(inode); 1301 unlock: 1302 spin_unlock(&inode_hash_lock); 1303 1304 return inode; 1305 } 1306 EXPORT_SYMBOL(inode_insert5); 1307 1308 /** 1309 * iget5_locked - obtain an inode from a moun 1310 * @sb: super block of file system 1311 * @hashval: hash value (usually inode num 1312 * @test: callback used for comparisons 1313 * @set: callback used to initialize a 1314 * @data: opaque data pointer to pass t 1315 * 1316 * Search for the inode specified by @hashval 1317 * and if present it is return it with an inc 1318 * a generalized version of iget_locked() for 1319 * number is not sufficient for unique identi 1320 * 1321 * If the inode is not in cache, allocate a n 1322 * hashed, and with the I_NEW flag set. The f 1323 * before unlocking it via unlock_new_inode() 1324 * 1325 * Note both @test and @set are called with t 1326 * sleep. 1327 */ 1328 struct inode *iget5_locked(struct super_block 1329 int (*test)(struct inode *, v 1330 int (*set)(struct inode *, vo 1331 { 1332 struct inode *inode = ilookup5(sb, ha 1333 1334 if (!inode) { 1335 struct inode *new = alloc_ino 1336 1337 if (new) { 1338 inode = inode_insert5 1339 if (unlikely(inode != 1340 destroy_inode 1341 } 1342 } 1343 return inode; 1344 } 1345 EXPORT_SYMBOL(iget5_locked); 1346 1347 /** 1348 * iget5_locked_rcu - obtain an inode from a 1349 * @sb: super block of file system 1350 * @hashval: hash value (usually inode num 1351 * @test: callback used for comparisons 1352 * @set: callback used to initialize a 1353 * @data: opaque data pointer to pass t 1354 * 1355 * This is equivalent to iget5_locked, except 1356 * tolerate the inode not being stable, inclu 1357 */ 1358 struct inode *iget5_locked_rcu(struct super_b 1359 int (*test)(struct inode *, v 1360 int (*set)(struct inode *, vo 1361 { 1362 struct hlist_head *head = inode_hasht 1363 struct inode *inode, *new; 1364 1365 again: 1366 inode = find_inode(sb, head, test, da 1367 if (inode) { 1368 if (IS_ERR(inode)) 1369 return NULL; 1370 wait_on_inode(inode); 1371 if (unlikely(inode_unhashed(i 1372 iput(inode); 1373 goto again; 1374 } 1375 return inode; 1376 } 1377 1378 new = alloc_inode(sb); 1379 if (new) { 1380 inode = inode_insert5(new, ha 1381 if (unlikely(inode != new)) 1382 destroy_inode(new); 1383 } 1384 return inode; 1385 } 1386 EXPORT_SYMBOL_GPL(iget5_locked_rcu); 1387 1388 /** 1389 * iget_locked - obtain an inode from a mount 1390 * @sb: super block of file system 1391 * @ino: inode number to get 1392 * 1393 * Search for the inode specified by @ino in 1394 * return it with an increased reference coun 1395 * where the inode number is sufficient for u 1396 * 1397 * If the inode is not in cache, allocate a n 1398 * hashed, and with the I_NEW flag set. The 1399 * before unlocking it via unlock_new_inode() 1400 */ 1401 struct inode *iget_locked(struct super_block 1402 { 1403 struct hlist_head *head = inode_hasht 1404 struct inode *inode; 1405 again: 1406 inode = find_inode_fast(sb, head, ino 1407 if (inode) { 1408 if (IS_ERR(inode)) 1409 return NULL; 1410 wait_on_inode(inode); 1411 if (unlikely(inode_unhashed(i 1412 iput(inode); 1413 goto again; 1414 } 1415 return inode; 1416 } 1417 1418 inode = alloc_inode(sb); 1419 if (inode) { 1420 struct inode *old; 1421 1422 spin_lock(&inode_hash_lock); 1423 /* We released the lock, so.. 1424 old = find_inode_fast(sb, hea 1425 if (!old) { 1426 inode->i_ino = ino; 1427 spin_lock(&inode->i_l 1428 inode->i_state = I_NE 1429 hlist_add_head_rcu(&i 1430 spin_unlock(&inode->i 1431 inode_sb_list_add(ino 1432 spin_unlock(&inode_ha 1433 1434 /* Return the locked 1435 * caller is responsi 1436 */ 1437 return inode; 1438 } 1439 1440 /* 1441 * Uhhuh, somebody else creat 1442 * us. Use the old inode inst 1443 * allocated. 1444 */ 1445 spin_unlock(&inode_hash_lock) 1446 destroy_inode(inode); 1447 if (IS_ERR(old)) 1448 return NULL; 1449 inode = old; 1450 wait_on_inode(inode); 1451 if (unlikely(inode_unhashed(i 1452 iput(inode); 1453 goto again; 1454 } 1455 } 1456 return inode; 1457 } 1458 EXPORT_SYMBOL(iget_locked); 1459 1460 /* 1461 * search the inode cache for a matching inod 1462 * If we find one, then the inode number we a 1463 * allocate is not unique and so we should no 1464 * 1465 * Returns 1 if the inode number is unique, 0 1466 */ 1467 static int test_inode_iunique(struct super_bl 1468 { 1469 struct hlist_head *b = inode_hashtabl 1470 struct inode *inode; 1471 1472 hlist_for_each_entry_rcu(inode, b, i_ 1473 if (inode->i_ino == ino && in 1474 return 0; 1475 } 1476 return 1; 1477 } 1478 1479 /** 1480 * iunique - get a unique inode number 1481 * @sb: superblock 1482 * @max_reserved: highest reserved inode 1483 * 1484 * Obtain an inode number that is unique 1485 * superblock. This is used by file syst 1486 * permanent inode numbering system. An 1487 * is higher than the reserved limit but 1488 * 1489 * BUGS: 1490 * With a large number of inodes live on 1491 * currently becomes quite slow. 1492 */ 1493 ino_t iunique(struct super_block *sb, ino_t m 1494 { 1495 /* 1496 * On a 32bit, non LFS stat() call, g 1497 * error if st_ino won't fit in targe 1498 * here to attempt to avoid that. 1499 */ 1500 static DEFINE_SPINLOCK(iunique_lock); 1501 static unsigned int counter; 1502 ino_t res; 1503 1504 rcu_read_lock(); 1505 spin_lock(&iunique_lock); 1506 do { 1507 if (counter <= max_reserved) 1508 counter = max_reserve 1509 res = counter++; 1510 } while (!test_inode_iunique(sb, res) 1511 spin_unlock(&iunique_lock); 1512 rcu_read_unlock(); 1513 1514 return res; 1515 } 1516 EXPORT_SYMBOL(iunique); 1517 1518 struct inode *igrab(struct inode *inode) 1519 { 1520 spin_lock(&inode->i_lock); 1521 if (!(inode->i_state & (I_FREEING|I_W 1522 __iget(inode); 1523 spin_unlock(&inode->i_lock); 1524 } else { 1525 spin_unlock(&inode->i_lock); 1526 /* 1527 * Handle the case where s_op 1528 * called yet, and somebody i 1529 * while the inode is getting 1530 */ 1531 inode = NULL; 1532 } 1533 return inode; 1534 } 1535 EXPORT_SYMBOL(igrab); 1536 1537 /** 1538 * ilookup5_nowait - search for an inode in t 1539 * @sb: super block of file system to 1540 * @hashval: hash value (usually inode num 1541 * @test: callback used for comparisons 1542 * @data: opaque data pointer to pass t 1543 * 1544 * Search for the inode specified by @hashval 1545 * If the inode is in the cache, the inode is 1546 * reference count. 1547 * 1548 * Note: I_NEW is not waited upon so you have 1549 * with the returned inode. You probably sho 1550 * 1551 * Note2: @test is called with the inode_hash 1552 */ 1553 struct inode *ilookup5_nowait(struct super_bl 1554 int (*test)(struct inode *, v 1555 { 1556 struct hlist_head *head = inode_hasht 1557 struct inode *inode; 1558 1559 spin_lock(&inode_hash_lock); 1560 inode = find_inode(sb, head, test, da 1561 spin_unlock(&inode_hash_lock); 1562 1563 return IS_ERR(inode) ? NULL : inode; 1564 } 1565 EXPORT_SYMBOL(ilookup5_nowait); 1566 1567 /** 1568 * ilookup5 - search for an inode in the inod 1569 * @sb: super block of file system to 1570 * @hashval: hash value (usually inode num 1571 * @test: callback used for comparisons 1572 * @data: opaque data pointer to pass t 1573 * 1574 * Search for the inode specified by @hashval 1575 * and if the inode is in the cache, return t 1576 * reference count. Waits on I_NEW before re 1577 * returned with an incremented reference cou 1578 * 1579 * This is a generalized version of ilookup() 1580 * inode number is not sufficient for unique 1581 * 1582 * Note: @test is called with the inode_hash_ 1583 */ 1584 struct inode *ilookup5(struct super_block *sb 1585 int (*test)(struct inode *, v 1586 { 1587 struct inode *inode; 1588 again: 1589 inode = ilookup5_nowait(sb, hashval, 1590 if (inode) { 1591 wait_on_inode(inode); 1592 if (unlikely(inode_unhashed(i 1593 iput(inode); 1594 goto again; 1595 } 1596 } 1597 return inode; 1598 } 1599 EXPORT_SYMBOL(ilookup5); 1600 1601 /** 1602 * ilookup - search for an inode in the inode 1603 * @sb: super block of file system to 1604 * @ino: inode number to search for 1605 * 1606 * Search for the inode @ino in the inode cac 1607 * cache, the inode is returned with an incre 1608 */ 1609 struct inode *ilookup(struct super_block *sb, 1610 { 1611 struct hlist_head *head = inode_hasht 1612 struct inode *inode; 1613 again: 1614 inode = find_inode_fast(sb, head, ino 1615 1616 if (inode) { 1617 if (IS_ERR(inode)) 1618 return NULL; 1619 wait_on_inode(inode); 1620 if (unlikely(inode_unhashed(i 1621 iput(inode); 1622 goto again; 1623 } 1624 } 1625 return inode; 1626 } 1627 EXPORT_SYMBOL(ilookup); 1628 1629 /** 1630 * find_inode_nowait - find an inode in the i 1631 * @sb: super block of file system to 1632 * @hashval: hash value (usually inode num 1633 * @match: callback used for comparisons 1634 * @data: opaque data pointer to pass t 1635 * 1636 * Search for the inode specified by @hashval 1637 * cache, where the helper function @match wi 1638 * does not match, 1 if the inode does match, 1639 * should be stopped. The @match function mu 1640 * taking the i_lock spin_lock and checking i 1641 * freed or being initialized, and incrementi 1642 * before returning 1. It also must not slee 1643 * the inode_hash_lock spinlock held. 1644 * 1645 * This is a even more generalized version of 1646 * function must never block --- find_inode() 1647 * __wait_on_freeing_inode() --- or when the 1648 * the reference count because the resulting 1649 * inode eviction. The tradeoff is that the 1650 * very carefully implemented. 1651 */ 1652 struct inode *find_inode_nowait(struct super_ 1653 unsigned long 1654 int (*match)( 1655 1656 void *data) 1657 { 1658 struct hlist_head *head = inode_hasht 1659 struct inode *inode, *ret_inode = NUL 1660 int mval; 1661 1662 spin_lock(&inode_hash_lock); 1663 hlist_for_each_entry(inode, head, i_h 1664 if (inode->i_sb != sb) 1665 continue; 1666 mval = match(inode, hashval, 1667 if (mval == 0) 1668 continue; 1669 if (mval == 1) 1670 ret_inode = inode; 1671 goto out; 1672 } 1673 out: 1674 spin_unlock(&inode_hash_lock); 1675 return ret_inode; 1676 } 1677 EXPORT_SYMBOL(find_inode_nowait); 1678 1679 /** 1680 * find_inode_rcu - find an inode in the inod 1681 * @sb: Super block of file system to 1682 * @hashval: Key to hash 1683 * @test: Function to test match on an 1684 * @data: Data for test function 1685 * 1686 * Search for the inode specified by @hashval 1687 * where the helper function @test will retur 1688 * and 1 if it does. The @test function must 1689 * i_lock spin_lock and checking i_state for 1690 * initialized. 1691 * 1692 * If successful, this will return the inode 1693 * returned 1 and NULL otherwise. 1694 * 1695 * The @test function is not permitted to tak 1696 * It is also not permitted to sleep. 1697 * 1698 * The caller must hold the RCU read lock. 1699 */ 1700 struct inode *find_inode_rcu(struct super_blo 1701 int (*test)(stru 1702 { 1703 struct hlist_head *head = inode_hasht 1704 struct inode *inode; 1705 1706 RCU_LOCKDEP_WARN(!rcu_read_lock_held( 1707 "suspicious find_ino 1708 1709 hlist_for_each_entry_rcu(inode, head, 1710 if (inode->i_sb == sb && 1711 !(READ_ONCE(inode->i_stat 1712 test(inode, data)) 1713 return inode; 1714 } 1715 return NULL; 1716 } 1717 EXPORT_SYMBOL(find_inode_rcu); 1718 1719 /** 1720 * find_inode_by_ino_rcu - Find an inode in t 1721 * @sb: Super block of file system to 1722 * @ino: The inode number to match 1723 * 1724 * Search for the inode specified by @hashval 1725 * where the helper function @test will retur 1726 * and 1 if it does. The @test function must 1727 * i_lock spin_lock and checking i_state for 1728 * initialized. 1729 * 1730 * If successful, this will return the inode 1731 * returned 1 and NULL otherwise. 1732 * 1733 * The @test function is not permitted to tak 1734 * It is also not permitted to sleep. 1735 * 1736 * The caller must hold the RCU read lock. 1737 */ 1738 struct inode *find_inode_by_ino_rcu(struct su 1739 unsigned 1740 { 1741 struct hlist_head *head = inode_hasht 1742 struct inode *inode; 1743 1744 RCU_LOCKDEP_WARN(!rcu_read_lock_held( 1745 "suspicious find_ino 1746 1747 hlist_for_each_entry_rcu(inode, head, 1748 if (inode->i_ino == ino && 1749 inode->i_sb == sb && 1750 !(READ_ONCE(inode->i_stat 1751 return inode; 1752 } 1753 return NULL; 1754 } 1755 EXPORT_SYMBOL(find_inode_by_ino_rcu); 1756 1757 int insert_inode_locked(struct inode *inode) 1758 { 1759 struct super_block *sb = inode->i_sb; 1760 ino_t ino = inode->i_ino; 1761 struct hlist_head *head = inode_hasht 1762 1763 while (1) { 1764 struct inode *old = NULL; 1765 spin_lock(&inode_hash_lock); 1766 hlist_for_each_entry(old, hea 1767 if (old->i_ino != ino 1768 continue; 1769 if (old->i_sb != sb) 1770 continue; 1771 spin_lock(&old->i_loc 1772 if (old->i_state & (I 1773 spin_unlock(& 1774 continue; 1775 } 1776 break; 1777 } 1778 if (likely(!old)) { 1779 spin_lock(&inode->i_l 1780 inode->i_state |= I_N 1781 hlist_add_head_rcu(&i 1782 spin_unlock(&inode->i 1783 spin_unlock(&inode_ha 1784 return 0; 1785 } 1786 if (unlikely(old->i_state & I 1787 spin_unlock(&old->i_l 1788 spin_unlock(&inode_ha 1789 return -EBUSY; 1790 } 1791 __iget(old); 1792 spin_unlock(&old->i_lock); 1793 spin_unlock(&inode_hash_lock) 1794 wait_on_inode(old); 1795 if (unlikely(!inode_unhashed( 1796 iput(old); 1797 return -EBUSY; 1798 } 1799 iput(old); 1800 } 1801 } 1802 EXPORT_SYMBOL(insert_inode_locked); 1803 1804 int insert_inode_locked4(struct inode *inode, 1805 int (*test)(struct inode *, v 1806 { 1807 struct inode *old; 1808 1809 inode->i_state |= I_CREATING; 1810 old = inode_insert5(inode, hashval, t 1811 1812 if (old != inode) { 1813 iput(old); 1814 return -EBUSY; 1815 } 1816 return 0; 1817 } 1818 EXPORT_SYMBOL(insert_inode_locked4); 1819 1820 1821 int generic_delete_inode(struct inode *inode) 1822 { 1823 return 1; 1824 } 1825 EXPORT_SYMBOL(generic_delete_inode); 1826 1827 /* 1828 * Called when we're dropping the last refere 1829 * to an inode. 1830 * 1831 * Call the FS "drop_inode()" function, defau 1832 * the legacy UNIX filesystem behaviour. If 1833 * us to evict inode, do so. Otherwise, reta 1834 * in cache if fs is alive, sync and evict if 1835 * shutting down. 1836 */ 1837 static void iput_final(struct inode *inode) 1838 { 1839 struct super_block *sb = inode->i_sb; 1840 const struct super_operations *op = i 1841 unsigned long state; 1842 int drop; 1843 1844 WARN_ON(inode->i_state & I_NEW); 1845 1846 if (op->drop_inode) 1847 drop = op->drop_inode(inode); 1848 else 1849 drop = generic_drop_inode(ino 1850 1851 if (!drop && 1852 !(inode->i_state & I_DONTCACHE) & 1853 (sb->s_flags & SB_ACTIVE)) { 1854 __inode_add_lru(inode, true); 1855 spin_unlock(&inode->i_lock); 1856 return; 1857 } 1858 1859 state = inode->i_state; 1860 if (!drop) { 1861 WRITE_ONCE(inode->i_state, st 1862 spin_unlock(&inode->i_lock); 1863 1864 write_inode_now(inode, 1); 1865 1866 spin_lock(&inode->i_lock); 1867 state = inode->i_state; 1868 WARN_ON(state & I_NEW); 1869 state &= ~I_WILL_FREE; 1870 } 1871 1872 WRITE_ONCE(inode->i_state, state | I_ 1873 if (!list_empty(&inode->i_lru)) 1874 inode_lru_list_del(inode); 1875 spin_unlock(&inode->i_lock); 1876 1877 evict(inode); 1878 } 1879 1880 /** 1881 * iput - put an inode 1882 * @inode: inode to put 1883 * 1884 * Puts an inode, dropping its usage cou 1885 * zero, the inode is then freed and may 1886 * 1887 * Consequently, iput() can sleep. 1888 */ 1889 void iput(struct inode *inode) 1890 { 1891 if (!inode) 1892 return; 1893 BUG_ON(inode->i_state & I_CLEAR); 1894 retry: 1895 if (atomic_dec_and_lock(&inode->i_cou 1896 if (inode->i_nlink && (inode- 1897 atomic_inc(&inode->i_ 1898 spin_unlock(&inode->i 1899 trace_writeback_lazyt 1900 mark_inode_dirty_sync 1901 goto retry; 1902 } 1903 iput_final(inode); 1904 } 1905 } 1906 EXPORT_SYMBOL(iput); 1907 1908 #ifdef CONFIG_BLOCK 1909 /** 1910 * bmap - find a block number in a fi 1911 * @inode: inode owning the block numbe 1912 * @block: pointer containing the block 1913 * 1914 * Replaces the value in ``*block`` with 1915 * corresponding to the requested block 1916 * That is, asked for block 4 of inode 1 1917 * 4 in ``*block``, with disk block rela 1918 * block of the file. 1919 * 1920 * Returns -EINVAL in case of error, 0 o 1921 * hole, returns 0 and ``*block`` is als 1922 */ 1923 int bmap(struct inode *inode, sector_t *block 1924 { 1925 if (!inode->i_mapping->a_ops->bmap) 1926 return -EINVAL; 1927 1928 *block = inode->i_mapping->a_ops->bma 1929 return 0; 1930 } 1931 EXPORT_SYMBOL(bmap); 1932 #endif 1933 1934 /* 1935 * With relative atime, only update atime if 1936 * earlier than or equal to either the ctime 1937 * or if at least a day has passed since the 1938 */ 1939 static bool relatime_need_update(struct vfsmo 1940 struct timespec6 1941 { 1942 struct timespec64 atime, mtime, ctime 1943 1944 if (!(mnt->mnt_flags & MNT_RELATIME)) 1945 return true; 1946 /* 1947 * Is mtime younger than or equal to 1948 */ 1949 atime = inode_get_atime(inode); 1950 mtime = inode_get_mtime(inode); 1951 if (timespec64_compare(&mtime, &atime 1952 return true; 1953 /* 1954 * Is ctime younger than or equal to 1955 */ 1956 ctime = inode_get_ctime(inode); 1957 if (timespec64_compare(&ctime, &atime 1958 return true; 1959 1960 /* 1961 * Is the previous atime value older 1962 * update atime: 1963 */ 1964 if ((long)(now.tv_sec - atime.tv_sec) 1965 return true; 1966 /* 1967 * Good, we can skip the atime update 1968 */ 1969 return false; 1970 } 1971 1972 /** 1973 * inode_update_timestamps - update the times 1974 * @inode: inode to be updated 1975 * @flags: S_* flags that needed to be update 1976 * 1977 * The update_time function is called when an 1978 * updated for a read or write operation. Thi 1979 * actual timestamps. It's up to the caller t 1980 * dirty appropriately. 1981 * 1982 * In the case where any of S_MTIME, S_CTIME, 1983 * attempt to update all three of them. S_ATI 1984 * independently of the rest. 1985 * 1986 * Returns a set of S_* flags indicating whic 1987 */ 1988 int inode_update_timestamps(struct inode *ino 1989 { 1990 int updated = 0; 1991 struct timespec64 now; 1992 1993 if (flags & (S_MTIME|S_CTIME|S_VERSIO 1994 struct timespec64 ctime = ino 1995 struct timespec64 mtime = ino 1996 1997 now = inode_set_ctime_current 1998 if (!timespec64_equal(&now, & 1999 updated |= S_CTIME; 2000 if (!timespec64_equal(&now, & 2001 inode_set_mtime_to_ts 2002 updated |= S_MTIME; 2003 } 2004 if (IS_I_VERSION(inode) && in 2005 updated |= S_VERSION; 2006 } else { 2007 now = current_time(inode); 2008 } 2009 2010 if (flags & S_ATIME) { 2011 struct timespec64 atime = ino 2012 2013 if (!timespec64_equal(&now, & 2014 inode_set_atime_to_ts 2015 updated |= S_ATIME; 2016 } 2017 } 2018 return updated; 2019 } 2020 EXPORT_SYMBOL(inode_update_timestamps); 2021 2022 /** 2023 * generic_update_time - update the timestamp 2024 * @inode: inode to be updated 2025 * @flags: S_* flags that needed to be update 2026 * 2027 * The update_time function is called when an 2028 * updated for a read or write operation. In 2029 * or S_VERSION need to be updated we attempt 2030 * updates can be handled done independently 2031 * 2032 * Returns a S_* mask indicating which fields 2033 */ 2034 int generic_update_time(struct inode *inode, 2035 { 2036 int updated = inode_update_timestamps 2037 int dirty_flags = 0; 2038 2039 if (updated & (S_ATIME|S_MTIME|S_CTIM 2040 dirty_flags = inode->i_sb->s_ 2041 if (updated & S_VERSION) 2042 dirty_flags |= I_DIRTY_SYNC; 2043 __mark_inode_dirty(inode, dirty_flags 2044 return updated; 2045 } 2046 EXPORT_SYMBOL(generic_update_time); 2047 2048 /* 2049 * This does the actual work of updating an i 2050 * had called mnt_want_write() before calling 2051 */ 2052 int inode_update_time(struct inode *inode, in 2053 { 2054 if (inode->i_op->update_time) 2055 return inode->i_op->update_ti 2056 generic_update_time(inode, flags); 2057 return 0; 2058 } 2059 EXPORT_SYMBOL(inode_update_time); 2060 2061 /** 2062 * atime_needs_update - updat 2063 * @path: the &struct path to update 2064 * @inode: inode to update 2065 * 2066 * Update the accessed time on an inode 2067 * This function automatically handles r 2068 * as well as the "noatime" flag and ino 2069 */ 2070 bool atime_needs_update(const struct path *pa 2071 { 2072 struct vfsmount *mnt = path->mnt; 2073 struct timespec64 now, atime; 2074 2075 if (inode->i_flags & S_NOATIME) 2076 return false; 2077 2078 /* Atime updates will likely cause i_ 2079 * back improprely if their true valu 2080 */ 2081 if (HAS_UNMAPPED_ID(mnt_idmap(mnt), i 2082 return false; 2083 2084 if (IS_NOATIME(inode)) 2085 return false; 2086 if ((inode->i_sb->s_flags & SB_NODIRA 2087 return false; 2088 2089 if (mnt->mnt_flags & MNT_NOATIME) 2090 return false; 2091 if ((mnt->mnt_flags & MNT_NODIRATIME) 2092 return false; 2093 2094 now = current_time(inode); 2095 2096 if (!relatime_need_update(mnt, inode, 2097 return false; 2098 2099 atime = inode_get_atime(inode); 2100 if (timespec64_equal(&atime, &now)) 2101 return false; 2102 2103 return true; 2104 } 2105 2106 void touch_atime(const struct path *path) 2107 { 2108 struct vfsmount *mnt = path->mnt; 2109 struct inode *inode = d_inode(path->d 2110 2111 if (!atime_needs_update(path, inode)) 2112 return; 2113 2114 if (!sb_start_write_trylock(inode->i_ 2115 return; 2116 2117 if (mnt_get_write_access(mnt) != 0) 2118 goto skip_update; 2119 /* 2120 * File systems can error out when up 2121 * allocate new space to modify an in 2122 * Btrfs), but since we touch atime w 2123 * really don't care if we failed to 2124 * so just ignore the return value. 2125 * We may also fail on filesystems th 2126 * of the fs read only, e.g. subvolum 2127 */ 2128 inode_update_time(inode, S_ATIME); 2129 mnt_put_write_access(mnt); 2130 skip_update: 2131 sb_end_write(inode->i_sb); 2132 } 2133 EXPORT_SYMBOL(touch_atime); 2134 2135 /* 2136 * Return mask of changes for notify_change() 2137 * response to write or truncate. Return 0 if 2138 * Negative value on error (change should be 2139 */ 2140 int dentry_needs_remove_privs(struct mnt_idma 2141 struct dentry * 2142 { 2143 struct inode *inode = d_inode(dentry) 2144 int mask = 0; 2145 int ret; 2146 2147 if (IS_NOSEC(inode)) 2148 return 0; 2149 2150 mask = setattr_should_drop_suidgid(id 2151 ret = security_inode_need_killpriv(de 2152 if (ret < 0) 2153 return ret; 2154 if (ret) 2155 mask |= ATTR_KILL_PRIV; 2156 return mask; 2157 } 2158 2159 static int __remove_privs(struct mnt_idmap *i 2160 struct dentry *dent 2161 { 2162 struct iattr newattrs; 2163 2164 newattrs.ia_valid = ATTR_FORCE | kill 2165 /* 2166 * Note we call this on write, so not 2167 * encounter any conflicting delegati 2168 */ 2169 return notify_change(idmap, dentry, & 2170 } 2171 2172 int file_remove_privs_flags(struct file *file 2173 { 2174 struct dentry *dentry = file_dentry(f 2175 struct inode *inode = file_inode(file 2176 int error = 0; 2177 int kill; 2178 2179 if (IS_NOSEC(inode) || !S_ISREG(inode 2180 return 0; 2181 2182 kill = dentry_needs_remove_privs(file 2183 if (kill < 0) 2184 return kill; 2185 2186 if (kill) { 2187 if (flags & IOCB_NOWAIT) 2188 return -EAGAIN; 2189 2190 error = __remove_privs(file_m 2191 } 2192 2193 if (!error) 2194 inode_has_no_xattr(inode); 2195 return error; 2196 } 2197 EXPORT_SYMBOL_GPL(file_remove_privs_flags); 2198 2199 /** 2200 * file_remove_privs - remove special file pr 2201 * @file: file to remove privileges from 2202 * 2203 * When file is modified by a write or trunca 2204 * file privileges are removed. 2205 * 2206 * Return: 0 on success, negative errno on fa 2207 */ 2208 int file_remove_privs(struct file *file) 2209 { 2210 return file_remove_privs_flags(file, 2211 } 2212 EXPORT_SYMBOL(file_remove_privs); 2213 2214 static int inode_needs_update_time(struct ino 2215 { 2216 int sync_it = 0; 2217 struct timespec64 now = current_time( 2218 struct timespec64 ts; 2219 2220 /* First try to exhaust all avenues t 2221 if (IS_NOCMTIME(inode)) 2222 return 0; 2223 2224 ts = inode_get_mtime(inode); 2225 if (!timespec64_equal(&ts, &now)) 2226 sync_it = S_MTIME; 2227 2228 ts = inode_get_ctime(inode); 2229 if (!timespec64_equal(&ts, &now)) 2230 sync_it |= S_CTIME; 2231 2232 if (IS_I_VERSION(inode) && inode_iver 2233 sync_it |= S_VERSION; 2234 2235 return sync_it; 2236 } 2237 2238 static int __file_update_time(struct file *fi 2239 { 2240 int ret = 0; 2241 struct inode *inode = file_inode(file 2242 2243 /* try to update time settings */ 2244 if (!mnt_get_write_access_file(file)) 2245 ret = inode_update_time(inode 2246 mnt_put_write_access_file(fil 2247 } 2248 2249 return ret; 2250 } 2251 2252 /** 2253 * file_update_time - update mtime and ctime 2254 * @file: file accessed 2255 * 2256 * Update the mtime and ctime members of an i 2257 * writeback. Note that this function is mean 2258 * the file write path of filesystems, and fi 2259 * explicitly ignore updates via this functio 2260 * flag, e.g. for network filesystem where th 2261 * by the server. This can return an error fo 2262 * allocate space in order to update an inode 2263 * 2264 * Return: 0 on success, negative errno on fa 2265 */ 2266 int file_update_time(struct file *file) 2267 { 2268 int ret; 2269 struct inode *inode = file_inode(file 2270 2271 ret = inode_needs_update_time(inode); 2272 if (ret <= 0) 2273 return ret; 2274 2275 return __file_update_time(file, ret); 2276 } 2277 EXPORT_SYMBOL(file_update_time); 2278 2279 /** 2280 * file_modified_flags - handle mandated vfs 2281 * @file: file that was modified 2282 * @flags: kiocb flags 2283 * 2284 * When file has been modified ensure that sp 2285 * file privileges are removed and time setti 2286 * 2287 * If IOCB_NOWAIT is set, special file privil 2288 * time settings will not be updated. It will 2289 * 2290 * Context: Caller must hold the file's inode 2291 * 2292 * Return: 0 on success, negative errno on fa 2293 */ 2294 static int file_modified_flags(struct file *f 2295 { 2296 int ret; 2297 struct inode *inode = file_inode(file 2298 2299 /* 2300 * Clear the security bits if the pro 2301 * This keeps people from modifying s 2302 */ 2303 ret = file_remove_privs_flags(file, f 2304 if (ret) 2305 return ret; 2306 2307 if (unlikely(file->f_mode & FMODE_NOC 2308 return 0; 2309 2310 ret = inode_needs_update_time(inode); 2311 if (ret <= 0) 2312 return ret; 2313 if (flags & IOCB_NOWAIT) 2314 return -EAGAIN; 2315 2316 return __file_update_time(file, ret); 2317 } 2318 2319 /** 2320 * file_modified - handle mandated vfs change 2321 * @file: file that was modified 2322 * 2323 * When file has been modified ensure that sp 2324 * file privileges are removed and time setti 2325 * 2326 * Context: Caller must hold the file's inode 2327 * 2328 * Return: 0 on success, negative errno on fa 2329 */ 2330 int file_modified(struct file *file) 2331 { 2332 return file_modified_flags(file, 0); 2333 } 2334 EXPORT_SYMBOL(file_modified); 2335 2336 /** 2337 * kiocb_modified - handle mandated vfs chang 2338 * @iocb: iocb that was modified 2339 * 2340 * When file has been modified ensure that sp 2341 * file privileges are removed and time setti 2342 * 2343 * Context: Caller must hold the file's inode 2344 * 2345 * Return: 0 on success, negative errno on fa 2346 */ 2347 int kiocb_modified(struct kiocb *iocb) 2348 { 2349 return file_modified_flags(iocb->ki_f 2350 } 2351 EXPORT_SYMBOL_GPL(kiocb_modified); 2352 2353 int inode_needs_sync(struct inode *inode) 2354 { 2355 if (IS_SYNC(inode)) 2356 return 1; 2357 if (S_ISDIR(inode->i_mode) && IS_DIRS 2358 return 1; 2359 return 0; 2360 } 2361 EXPORT_SYMBOL(inode_needs_sync); 2362 2363 /* 2364 * If we try to find an inode in the inode ha 2365 * deleted, we have to wait until the filesys 2366 * deletion before reporting that it isn't fo 2367 * until the deletion _might_ have completed. 2368 * to recheck inode state. 2369 * 2370 * It doesn't matter if I_NEW is not set init 2371 * wake_up_bit(&inode->i_state, __I_NEW) afte 2372 * will DTRT. 2373 */ 2374 static void __wait_on_freeing_inode(struct in 2375 { 2376 struct wait_bit_queue_entry wqe; 2377 struct wait_queue_head *wq_head; 2378 2379 /* 2380 * Handle racing against evict(), see 2381 */ 2382 if (unlikely(inode_unhashed(inode))) 2383 WARN_ON(is_inode_hash_locked) 2384 spin_unlock(&inode->i_lock); 2385 return; 2386 } 2387 2388 wq_head = inode_bit_waitqueue(&wqe, i 2389 prepare_to_wait_event(wq_head, &wqe.w 2390 spin_unlock(&inode->i_lock); 2391 rcu_read_unlock(); 2392 if (is_inode_hash_locked) 2393 spin_unlock(&inode_hash_lock) 2394 schedule(); 2395 finish_wait(wq_head, &wqe.wq_entry); 2396 if (is_inode_hash_locked) 2397 spin_lock(&inode_hash_lock); 2398 rcu_read_lock(); 2399 } 2400 2401 static __initdata unsigned long ihash_entries 2402 static int __init set_ihash_entries(char *str 2403 { 2404 if (!str) 2405 return 0; 2406 ihash_entries = simple_strtoul(str, & 2407 return 1; 2408 } 2409 __setup("ihash_entries=", set_ihash_entries); 2410 2411 /* 2412 * Initialize the waitqueues and inode hash t 2413 */ 2414 void __init inode_init_early(void) 2415 { 2416 /* If hashes are distributed across N 2417 * hash allocation until vmalloc spac 2418 */ 2419 if (hashdist) 2420 return; 2421 2422 inode_hashtable = 2423 alloc_large_system_hash("Inod 2424 sizeo 2425 ihash 2426 14, 2427 HASH_ 2428 &i_ha 2429 &i_ha 2430 0, 2431 0); 2432 } 2433 2434 void __init inode_init(void) 2435 { 2436 /* inode slab cache */ 2437 inode_cachep = kmem_cache_create("ino 2438 size 2439 0, 2440 (SLA 2441 SLAB 2442 init 2443 2444 /* Hash may have been set up in inode 2445 if (!hashdist) 2446 return; 2447 2448 inode_hashtable = 2449 alloc_large_system_hash("Inod 2450 sizeo 2451 ihash 2452 14, 2453 HASH_ 2454 &i_ha 2455 &i_ha 2456 0, 2457 0); 2458 } 2459 2460 void init_special_inode(struct inode *inode, 2461 { 2462 inode->i_mode = mode; 2463 if (S_ISCHR(mode)) { 2464 inode->i_fop = &def_chr_fops; 2465 inode->i_rdev = rdev; 2466 } else if (S_ISBLK(mode)) { 2467 if (IS_ENABLED(CONFIG_BLOCK)) 2468 inode->i_fop = &def_b 2469 inode->i_rdev = rdev; 2470 } else if (S_ISFIFO(mode)) 2471 inode->i_fop = &pipefifo_fops 2472 else if (S_ISSOCK(mode)) 2473 ; /* leave it no_open_f 2474 else 2475 printk(KERN_DEBUG "init_speci 2476 " inode %s: 2477 inode->i_in 2478 } 2479 EXPORT_SYMBOL(init_special_inode); 2480 2481 /** 2482 * inode_init_owner - Init uid,gid,mode for n 2483 * @idmap: idmap of the mount the inode was c 2484 * @inode: New inode 2485 * @dir: Directory inode 2486 * @mode: mode of the new inode 2487 * 2488 * If the inode has been created through an i 2489 * the vfsmount must be passed through @idmap 2490 * care to map the inode according to @idmap 2491 * and initializing i_uid and i_gid. On non-i 2492 * checking is to be performed on the raw ino 2493 */ 2494 void inode_init_owner(struct mnt_idmap *idmap 2495 const struct inode *dir 2496 { 2497 inode_fsuid_set(inode, idmap); 2498 if (dir && dir->i_mode & S_ISGID) { 2499 inode->i_gid = dir->i_gid; 2500 2501 /* Directories are special, a 2502 if (S_ISDIR(mode)) 2503 mode |= S_ISGID; 2504 } else 2505 inode_fsgid_set(inode, idmap) 2506 inode->i_mode = mode; 2507 } 2508 EXPORT_SYMBOL(inode_init_owner); 2509 2510 /** 2511 * inode_owner_or_capable - check current tas 2512 * @idmap: idmap of the mount the inode was f 2513 * @inode: inode being checked 2514 * 2515 * Return true if current either has CAP_FOWN 2516 * inode owner uid mapped, or owns the file. 2517 * 2518 * If the inode has been found through an idm 2519 * the vfsmount must be passed through @idmap 2520 * care to map the inode according to @idmap 2521 * On non-idmapped mounts or if permission ch 2522 * raw inode simply pass @nop_mnt_idmap. 2523 */ 2524 bool inode_owner_or_capable(struct mnt_idmap 2525 const struct inod 2526 { 2527 vfsuid_t vfsuid; 2528 struct user_namespace *ns; 2529 2530 vfsuid = i_uid_into_vfsuid(idmap, ino 2531 if (vfsuid_eq_kuid(vfsuid, current_fs 2532 return true; 2533 2534 ns = current_user_ns(); 2535 if (vfsuid_has_mapping(ns, vfsuid) && 2536 return true; 2537 return false; 2538 } 2539 EXPORT_SYMBOL(inode_owner_or_capable); 2540 2541 /* 2542 * Direct i/o helper functions 2543 */ 2544 bool inode_dio_finished(const struct inode *i 2545 { 2546 return atomic_read(&inode->i_dio_coun 2547 } 2548 EXPORT_SYMBOL(inode_dio_finished); 2549 2550 /** 2551 * inode_dio_wait - wait for outstanding DIO 2552 * @inode: inode to wait for 2553 * 2554 * Waits for all pending direct I/O requests 2555 * proceed with a truncate or equivalent oper 2556 * 2557 * Must be called under a lock that serialize 2558 * to i_dio_count, usually by inode->i_mutex. 2559 */ 2560 void inode_dio_wait(struct inode *inode) 2561 { 2562 wait_var_event(&inode->i_dio_count, i 2563 } 2564 EXPORT_SYMBOL(inode_dio_wait); 2565 2566 void inode_dio_wait_interruptible(struct inod 2567 { 2568 wait_var_event_interruptible(&inode-> 2569 inode_di 2570 } 2571 EXPORT_SYMBOL(inode_dio_wait_interruptible); 2572 2573 /* 2574 * inode_set_flags - atomically set some inod 2575 * 2576 * Note: the caller should be holding i_mutex 2577 * they have exclusive access to the inode st 2578 * inode is being instantiated). The reason 2579 * --- which wouldn't be necessary if all cod 2580 * i_flags actually followed this rule, is th 2581 * code path which doesn't today so we use cm 2582 * of caution. 2583 * 2584 * In the long run, i_mutex is overkill, and 2585 * at using the i_lock spinlock to protect i_ 2586 * it is so documented in include/linux/fs.h 2587 * the locking convention!! 2588 */ 2589 void inode_set_flags(struct inode *inode, uns 2590 unsigned int mask) 2591 { 2592 WARN_ON_ONCE(flags & ~mask); 2593 set_mask_bits(&inode->i_flags, mask, 2594 } 2595 EXPORT_SYMBOL(inode_set_flags); 2596 2597 void inode_nohighmem(struct inode *inode) 2598 { 2599 mapping_set_gfp_mask(inode->i_mapping 2600 } 2601 EXPORT_SYMBOL(inode_nohighmem); 2602 2603 /** 2604 * timestamp_truncate - Truncate timespec to 2605 * @t: Timespec 2606 * @inode: inode being updated 2607 * 2608 * Truncate a timespec to the granularity sup 2609 * containing the inode. Always rounds down. 2610 * not be 0 nor greater than a second (NSEC_P 2611 */ 2612 struct timespec64 timestamp_truncate(struct t 2613 { 2614 struct super_block *sb = inode->i_sb; 2615 unsigned int gran = sb->s_time_gran; 2616 2617 t.tv_sec = clamp(t.tv_sec, sb->s_time 2618 if (unlikely(t.tv_sec == sb->s_time_m 2619 t.tv_nsec = 0; 2620 2621 /* Avoid division in the common cases 2622 if (gran == 1) 2623 ; /* nothing */ 2624 else if (gran == NSEC_PER_SEC) 2625 t.tv_nsec = 0; 2626 else if (gran > 1 && gran < NSEC_PER_ 2627 t.tv_nsec -= t.tv_nsec % gran 2628 else 2629 WARN(1, "invalid file time gr 2630 return t; 2631 } 2632 EXPORT_SYMBOL(timestamp_truncate); 2633 2634 /** 2635 * current_time - Return FS time 2636 * @inode: inode. 2637 * 2638 * Return the current time truncated to the t 2639 * the fs. 2640 * 2641 * Note that inode and inode->sb cannot be NU 2642 * Otherwise, the function warns and returns 2643 */ 2644 struct timespec64 current_time(struct inode * 2645 { 2646 struct timespec64 now; 2647 2648 ktime_get_coarse_real_ts64(&now); 2649 return timestamp_truncate(now, inode) 2650 } 2651 EXPORT_SYMBOL(current_time); 2652 2653 /** 2654 * inode_set_ctime_current - set the ctime to 2655 * @inode: inode 2656 * 2657 * Set the inode->i_ctime to the current valu 2658 * the current value that was assigned to i_c 2659 */ 2660 struct timespec64 inode_set_ctime_current(str 2661 { 2662 struct timespec64 now = current_time( 2663 2664 inode_set_ctime_to_ts(inode, now); 2665 return now; 2666 } 2667 EXPORT_SYMBOL(inode_set_ctime_current); 2668 2669 /** 2670 * in_group_or_capable - check whether caller 2671 * @idmap: idmap of the mount @inode was 2672 * @inode: inode to check 2673 * @vfsgid: the new/current vfsgid of @in 2674 * 2675 * Check wether @vfsgid is in the caller's gr 2676 * privileged with CAP_FSETID over @inode. Th 2677 * whether the setgid bit can be kept or must 2678 * 2679 * Return: true if the caller is sufficiently 2680 */ 2681 bool in_group_or_capable(struct mnt_idmap *id 2682 const struct inode * 2683 { 2684 if (vfsgid_in_group_p(vfsgid)) 2685 return true; 2686 if (capable_wrt_inode_uidgid(idmap, i 2687 return true; 2688 return false; 2689 } 2690 EXPORT_SYMBOL(in_group_or_capable); 2691 2692 /** 2693 * mode_strip_sgid - handle the sgid bit for 2694 * @idmap: idmap of the mount the inode was c 2695 * @dir: parent directory inode 2696 * @mode: mode of the file to be created in @ 2697 * 2698 * If the @mode of the new file has both the 2699 * raised and @dir has the S_ISGID bit raised 2700 * either in the group of the parent director 2701 * in their user namespace and are privileged 2702 * In all other cases, strip the S_ISGID bit 2703 * 2704 * Return: the new mode to use for the file 2705 */ 2706 umode_t mode_strip_sgid(struct mnt_idmap *idm 2707 const struct inode *d 2708 { 2709 if ((mode & (S_ISGID | S_IXGRP)) != ( 2710 return mode; 2711 if (S_ISDIR(mode) || !dir || !(dir->i 2712 return mode; 2713 if (in_group_or_capable(idmap, dir, i 2714 return mode; 2715 return mode & ~S_ISGID; 2716 } 2717 EXPORT_SYMBOL(mode_strip_sgid); 2718
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.