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