aboutsummaryrefslogtreecommitdiffstats
path: root/fs/inode.c
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2010-10-26 20:58:44 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2010-10-26 20:58:44 -0400
commit426e1f5cec4821945642230218876b0e89aafab1 (patch)
tree2728ace018d0698886989da586210ef1543a7098 /fs/inode.c
parent9e5fca251f44832cb996961048ea977f80faf6ea (diff)
parent63997e98a3be68d7cec806d22bf9b02b2e1daabb (diff)
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs-2.6
* 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs-2.6: (52 commits) split invalidate_inodes() fs: skip I_FREEING inodes in writeback_sb_inodes fs: fold invalidate_list into invalidate_inodes fs: do not drop inode_lock in dispose_list fs: inode split IO and LRU lists fs: switch bdev inode bdi's correctly fs: fix buffer invalidation in invalidate_list fsnotify: use dget_parent smbfs: use dget_parent exportfs: use dget_parent fs: use RCU read side protection in d_validate fs: clean up dentry lru modification fs: split __shrink_dcache_sb fs: improve DCACHE_REFERENCED usage fs: use percpu counter for nr_dentry and nr_dentry_unused fs: simplify __d_free fs: take dcache_lock inside __d_path fs: do not assign default i_ino in new_inode fs: introduce a per-cpu last_ino allocator new helper: ihold() ...
Diffstat (limited to 'fs/inode.c')
-rw-r--r--fs/inode.c526
1 files changed, 336 insertions, 190 deletions
diff --git a/fs/inode.c b/fs/inode.c
index 56d909d69bc8..ae2727ab0c3a 100644
--- a/fs/inode.c
+++ b/fs/inode.c
@@ -29,7 +29,6 @@
29/* 29/*
30 * This is needed for the following functions: 30 * This is needed for the following functions:
31 * - inode_has_buffers 31 * - inode_has_buffers
32 * - invalidate_inode_buffers
33 * - invalidate_bdev 32 * - invalidate_bdev
34 * 33 *
35 * FIXME: remove all knowledge of the buffer layer from this file 34 * FIXME: remove all knowledge of the buffer layer from this file
@@ -73,8 +72,7 @@ static unsigned int i_hash_shift __read_mostly;
73 * allowing for low-overhead inode sync() operations. 72 * allowing for low-overhead inode sync() operations.
74 */ 73 */
75 74
76LIST_HEAD(inode_in_use); 75static LIST_HEAD(inode_lru);
77LIST_HEAD(inode_unused);
78static struct hlist_head *inode_hashtable __read_mostly; 76static struct hlist_head *inode_hashtable __read_mostly;
79 77
80/* 78/*
@@ -104,8 +102,41 @@ static DECLARE_RWSEM(iprune_sem);
104 */ 102 */
105struct inodes_stat_t inodes_stat; 103struct inodes_stat_t inodes_stat;
106 104
105static struct percpu_counter nr_inodes __cacheline_aligned_in_smp;
106static struct percpu_counter nr_inodes_unused __cacheline_aligned_in_smp;
107
107static struct kmem_cache *inode_cachep __read_mostly; 108static struct kmem_cache *inode_cachep __read_mostly;
108 109
110static inline int get_nr_inodes(void)
111{
112 return percpu_counter_sum_positive(&nr_inodes);
113}
114
115static inline int get_nr_inodes_unused(void)
116{
117 return percpu_counter_sum_positive(&nr_inodes_unused);
118}
119
120int get_nr_dirty_inodes(void)
121{
122 int nr_dirty = get_nr_inodes() - get_nr_inodes_unused();
123 return nr_dirty > 0 ? nr_dirty : 0;
124
125}
126
127/*
128 * Handle nr_inode sysctl
129 */
130#ifdef CONFIG_SYSCTL
131int proc_nr_inodes(ctl_table *table, int write,
132 void __user *buffer, size_t *lenp, loff_t *ppos)
133{
134 inodes_stat.nr_inodes = get_nr_inodes();
135 inodes_stat.nr_unused = get_nr_inodes_unused();
136 return proc_dointvec(table, write, buffer, lenp, ppos);
137}
138#endif
139
109static void wake_up_inode(struct inode *inode) 140static void wake_up_inode(struct inode *inode)
110{ 141{
111 /* 142 /*
@@ -193,6 +224,8 @@ int inode_init_always(struct super_block *sb, struct inode *inode)
193 inode->i_fsnotify_mask = 0; 224 inode->i_fsnotify_mask = 0;
194#endif 225#endif
195 226
227 percpu_counter_inc(&nr_inodes);
228
196 return 0; 229 return 0;
197out: 230out:
198 return -ENOMEM; 231 return -ENOMEM;
@@ -233,11 +266,13 @@ void __destroy_inode(struct inode *inode)
233 if (inode->i_default_acl && inode->i_default_acl != ACL_NOT_CACHED) 266 if (inode->i_default_acl && inode->i_default_acl != ACL_NOT_CACHED)
234 posix_acl_release(inode->i_default_acl); 267 posix_acl_release(inode->i_default_acl);
235#endif 268#endif
269 percpu_counter_dec(&nr_inodes);
236} 270}
237EXPORT_SYMBOL(__destroy_inode); 271EXPORT_SYMBOL(__destroy_inode);
238 272
239void destroy_inode(struct inode *inode) 273static void destroy_inode(struct inode *inode)
240{ 274{
275 BUG_ON(!list_empty(&inode->i_lru));
241 __destroy_inode(inode); 276 __destroy_inode(inode);
242 if (inode->i_sb->s_op->destroy_inode) 277 if (inode->i_sb->s_op->destroy_inode)
243 inode->i_sb->s_op->destroy_inode(inode); 278 inode->i_sb->s_op->destroy_inode(inode);
@@ -256,6 +291,8 @@ void inode_init_once(struct inode *inode)
256 INIT_HLIST_NODE(&inode->i_hash); 291 INIT_HLIST_NODE(&inode->i_hash);
257 INIT_LIST_HEAD(&inode->i_dentry); 292 INIT_LIST_HEAD(&inode->i_dentry);
258 INIT_LIST_HEAD(&inode->i_devices); 293 INIT_LIST_HEAD(&inode->i_devices);
294 INIT_LIST_HEAD(&inode->i_wb_list);
295 INIT_LIST_HEAD(&inode->i_lru);
259 INIT_RADIX_TREE(&inode->i_data.page_tree, GFP_ATOMIC); 296 INIT_RADIX_TREE(&inode->i_data.page_tree, GFP_ATOMIC);
260 spin_lock_init(&inode->i_data.tree_lock); 297 spin_lock_init(&inode->i_data.tree_lock);
261 spin_lock_init(&inode->i_data.i_mmap_lock); 298 spin_lock_init(&inode->i_data.i_mmap_lock);
@@ -282,14 +319,109 @@ static void init_once(void *foo)
282 */ 319 */
283void __iget(struct inode *inode) 320void __iget(struct inode *inode)
284{ 321{
285 if (atomic_inc_return(&inode->i_count) != 1) 322 atomic_inc(&inode->i_count);
286 return; 323}
324
325/*
326 * get additional reference to inode; caller must already hold one.
327 */
328void ihold(struct inode *inode)
329{
330 WARN_ON(atomic_inc_return(&inode->i_count) < 2);
331}
332EXPORT_SYMBOL(ihold);
333
334static void inode_lru_list_add(struct inode *inode)
335{
336 if (list_empty(&inode->i_lru)) {
337 list_add(&inode->i_lru, &inode_lru);
338 percpu_counter_inc(&nr_inodes_unused);
339 }
340}
287 341
288 if (!(inode->i_state & (I_DIRTY|I_SYNC))) 342static void inode_lru_list_del(struct inode *inode)
289 list_move(&inode->i_list, &inode_in_use); 343{
290 inodes_stat.nr_unused--; 344 if (!list_empty(&inode->i_lru)) {
345 list_del_init(&inode->i_lru);
346 percpu_counter_dec(&nr_inodes_unused);
347 }
348}
349
350static inline void __inode_sb_list_add(struct inode *inode)
351{
352 list_add(&inode->i_sb_list, &inode->i_sb->s_inodes);
291} 353}
292 354
355/**
356 * inode_sb_list_add - add inode to the superblock list of inodes
357 * @inode: inode to add
358 */
359void inode_sb_list_add(struct inode *inode)
360{
361 spin_lock(&inode_lock);
362 __inode_sb_list_add(inode);
363 spin_unlock(&inode_lock);
364}
365EXPORT_SYMBOL_GPL(inode_sb_list_add);
366
367static inline void __inode_sb_list_del(struct inode *inode)
368{
369 list_del_init(&inode->i_sb_list);
370}
371
372static unsigned long hash(struct super_block *sb, unsigned long hashval)
373{
374 unsigned long tmp;
375
376 tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /
377 L1_CACHE_BYTES;
378 tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> I_HASHBITS);
379 return tmp & I_HASHMASK;
380}
381
382/**
383 * __insert_inode_hash - hash an inode
384 * @inode: unhashed inode
385 * @hashval: unsigned long value used to locate this object in the
386 * inode_hashtable.
387 *
388 * Add an inode to the inode hash for this superblock.
389 */
390void __insert_inode_hash(struct inode *inode, unsigned long hashval)
391{
392 struct hlist_head *b = inode_hashtable + hash(inode->i_sb, hashval);
393
394 spin_lock(&inode_lock);
395 hlist_add_head(&inode->i_hash, b);
396 spin_unlock(&inode_lock);
397}
398EXPORT_SYMBOL(__insert_inode_hash);
399
400/**
401 * __remove_inode_hash - remove an inode from the hash
402 * @inode: inode to unhash
403 *
404 * Remove an inode from the superblock.
405 */
406static void __remove_inode_hash(struct inode *inode)
407{
408 hlist_del_init(&inode->i_hash);
409}
410
411/**
412 * remove_inode_hash - remove an inode from the hash
413 * @inode: inode to unhash
414 *
415 * Remove an inode from the superblock.
416 */
417void remove_inode_hash(struct inode *inode)
418{
419 spin_lock(&inode_lock);
420 hlist_del_init(&inode->i_hash);
421 spin_unlock(&inode_lock);
422}
423EXPORT_SYMBOL(remove_inode_hash);
424
293void end_writeback(struct inode *inode) 425void end_writeback(struct inode *inode)
294{ 426{
295 might_sleep(); 427 might_sleep();
@@ -328,101 +460,113 @@ static void evict(struct inode *inode)
328 */ 460 */
329static void dispose_list(struct list_head *head) 461static void dispose_list(struct list_head *head)
330{ 462{
331 int nr_disposed = 0;
332
333 while (!list_empty(head)) { 463 while (!list_empty(head)) {
334 struct inode *inode; 464 struct inode *inode;
335 465
336 inode = list_first_entry(head, struct inode, i_list); 466 inode = list_first_entry(head, struct inode, i_lru);
337 list_del(&inode->i_list); 467 list_del_init(&inode->i_lru);
338 468
339 evict(inode); 469 evict(inode);
340 470
341 spin_lock(&inode_lock); 471 spin_lock(&inode_lock);
342 hlist_del_init(&inode->i_hash); 472 __remove_inode_hash(inode);
343 list_del_init(&inode->i_sb_list); 473 __inode_sb_list_del(inode);
344 spin_unlock(&inode_lock); 474 spin_unlock(&inode_lock);
345 475
346 wake_up_inode(inode); 476 wake_up_inode(inode);
347 destroy_inode(inode); 477 destroy_inode(inode);
348 nr_disposed++;
349 } 478 }
350 spin_lock(&inode_lock);
351 inodes_stat.nr_inodes -= nr_disposed;
352 spin_unlock(&inode_lock);
353} 479}
354 480
355/* 481/**
356 * Invalidate all inodes for a device. 482 * evict_inodes - evict all evictable inodes for a superblock
483 * @sb: superblock to operate on
484 *
485 * Make sure that no inodes with zero refcount are retained. This is
486 * called by superblock shutdown after having MS_ACTIVE flag removed,
487 * so any inode reaching zero refcount during or after that call will
488 * be immediately evicted.
357 */ 489 */
358static int invalidate_list(struct list_head *head, struct list_head *dispose) 490void evict_inodes(struct super_block *sb)
359{ 491{
360 struct list_head *next; 492 struct inode *inode, *next;
361 int busy = 0, count = 0; 493 LIST_HEAD(dispose);
362
363 next = head->next;
364 for (;;) {
365 struct list_head *tmp = next;
366 struct inode *inode;
367 494
368 /* 495 down_write(&iprune_sem);
369 * We can reschedule here without worrying about the list's
370 * consistency because the per-sb list of inodes must not
371 * change during umount anymore, and because iprune_sem keeps
372 * shrink_icache_memory() away.
373 */
374 cond_resched_lock(&inode_lock);
375 496
376 next = next->next; 497 spin_lock(&inode_lock);
377 if (tmp == head) 498 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
378 break; 499 if (atomic_read(&inode->i_count))
379 inode = list_entry(tmp, struct inode, i_sb_list);
380 if (inode->i_state & I_NEW)
381 continue; 500 continue;
382 invalidate_inode_buffers(inode); 501
383 if (!atomic_read(&inode->i_count)) { 502 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
384 list_move(&inode->i_list, dispose); 503 WARN_ON(1);
385 WARN_ON(inode->i_state & I_NEW);
386 inode->i_state |= I_FREEING;
387 count++;
388 continue; 504 continue;
389 } 505 }
390 busy = 1; 506
507 inode->i_state |= I_FREEING;
508
509 /*
510 * Move the inode off the IO lists and LRU once I_FREEING is
511 * set so that it won't get moved back on there if it is dirty.
512 */
513 list_move(&inode->i_lru, &dispose);
514 list_del_init(&inode->i_wb_list);
515 if (!(inode->i_state & (I_DIRTY | I_SYNC)))
516 percpu_counter_dec(&nr_inodes_unused);
391 } 517 }
392 /* only unused inodes may be cached with i_count zero */ 518 spin_unlock(&inode_lock);
393 inodes_stat.nr_unused -= count; 519
394 return busy; 520 dispose_list(&dispose);
521 up_write(&iprune_sem);
395} 522}
396 523
397/** 524/**
398 * invalidate_inodes - discard the inodes on a device 525 * invalidate_inodes - attempt to free all inodes on a superblock
399 * @sb: superblock 526 * @sb: superblock to operate on
400 * 527 *
401 * Discard all of the inodes for a given superblock. If the discard 528 * Attempts to free all inodes for a given superblock. If there were any
402 * fails because there are busy inodes then a non zero value is returned. 529 * busy inodes return a non-zero value, else zero.
403 * If the discard is successful all the inodes have been discarded.
404 */ 530 */
405int invalidate_inodes(struct super_block *sb) 531int invalidate_inodes(struct super_block *sb)
406{ 532{
407 int busy; 533 int busy = 0;
408 LIST_HEAD(throw_away); 534 struct inode *inode, *next;
535 LIST_HEAD(dispose);
409 536
410 down_write(&iprune_sem); 537 down_write(&iprune_sem);
538
411 spin_lock(&inode_lock); 539 spin_lock(&inode_lock);
412 fsnotify_unmount_inodes(&sb->s_inodes); 540 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
413 busy = invalidate_list(&sb->s_inodes, &throw_away); 541 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE))
542 continue;
543 if (atomic_read(&inode->i_count)) {
544 busy = 1;
545 continue;
546 }
547
548 inode->i_state |= I_FREEING;
549
550 /*
551 * Move the inode off the IO lists and LRU once I_FREEING is
552 * set so that it won't get moved back on there if it is dirty.
553 */
554 list_move(&inode->i_lru, &dispose);
555 list_del_init(&inode->i_wb_list);
556 if (!(inode->i_state & (I_DIRTY | I_SYNC)))
557 percpu_counter_dec(&nr_inodes_unused);
558 }
414 spin_unlock(&inode_lock); 559 spin_unlock(&inode_lock);
415 560
416 dispose_list(&throw_away); 561 dispose_list(&dispose);
417 up_write(&iprune_sem); 562 up_write(&iprune_sem);
418 563
419 return busy; 564 return busy;
420} 565}
421EXPORT_SYMBOL(invalidate_inodes);
422 566
423static int can_unuse(struct inode *inode) 567static int can_unuse(struct inode *inode)
424{ 568{
425 if (inode->i_state) 569 if (inode->i_state & ~I_REFERENCED)
426 return 0; 570 return 0;
427 if (inode_has_buffers(inode)) 571 if (inode_has_buffers(inode))
428 return 0; 572 return 0;
@@ -434,22 +578,24 @@ static int can_unuse(struct inode *inode)
434} 578}
435 579
436/* 580/*
437 * Scan `goal' inodes on the unused list for freeable ones. They are moved to 581 * Scan `goal' inodes on the unused list for freeable ones. They are moved to a
438 * a temporary list and then are freed outside inode_lock by dispose_list(). 582 * temporary list and then are freed outside inode_lock by dispose_list().
439 * 583 *
440 * Any inodes which are pinned purely because of attached pagecache have their 584 * Any inodes which are pinned purely because of attached pagecache have their
441 * pagecache removed. We expect the final iput() on that inode to add it to 585 * pagecache removed. If the inode has metadata buffers attached to
442 * the front of the inode_unused list. So look for it there and if the 586 * mapping->private_list then try to remove them.
443 * inode is still freeable, proceed. The right inode is found 99.9% of the
444 * time in testing on a 4-way.
445 * 587 *
446 * If the inode has metadata buffers attached to mapping->private_list then 588 * If the inode has the I_REFERENCED flag set, then it means that it has been
447 * try to remove them. 589 * used recently - the flag is set in iput_final(). When we encounter such an
590 * inode, clear the flag and move it to the back of the LRU so it gets another
591 * pass through the LRU before it gets reclaimed. This is necessary because of
592 * the fact we are doing lazy LRU updates to minimise lock contention so the
593 * LRU does not have strict ordering. Hence we don't want to reclaim inodes
594 * with this flag set because they are the inodes that are out of order.
448 */ 595 */
449static void prune_icache(int nr_to_scan) 596static void prune_icache(int nr_to_scan)
450{ 597{
451 LIST_HEAD(freeable); 598 LIST_HEAD(freeable);
452 int nr_pruned = 0;
453 int nr_scanned; 599 int nr_scanned;
454 unsigned long reap = 0; 600 unsigned long reap = 0;
455 601
@@ -458,13 +604,26 @@ static void prune_icache(int nr_to_scan)
458 for (nr_scanned = 0; nr_scanned < nr_to_scan; nr_scanned++) { 604 for (nr_scanned = 0; nr_scanned < nr_to_scan; nr_scanned++) {
459 struct inode *inode; 605 struct inode *inode;
460 606
461 if (list_empty(&inode_unused)) 607 if (list_empty(&inode_lru))
462 break; 608 break;
463 609
464 inode = list_entry(inode_unused.prev, struct inode, i_list); 610 inode = list_entry(inode_lru.prev, struct inode, i_lru);
465 611
466 if (inode->i_state || atomic_read(&inode->i_count)) { 612 /*
467 list_move(&inode->i_list, &inode_unused); 613 * Referenced or dirty inodes are still in use. Give them
614 * another pass through the LRU as we canot reclaim them now.
615 */
616 if (atomic_read(&inode->i_count) ||
617 (inode->i_state & ~I_REFERENCED)) {
618 list_del_init(&inode->i_lru);
619 percpu_counter_dec(&nr_inodes_unused);
620 continue;
621 }
622
623 /* recently referenced inodes get one more pass */
624 if (inode->i_state & I_REFERENCED) {
625 list_move(&inode->i_lru, &inode_lru);
626 inode->i_state &= ~I_REFERENCED;
468 continue; 627 continue;
469 } 628 }
470 if (inode_has_buffers(inode) || inode->i_data.nrpages) { 629 if (inode_has_buffers(inode) || inode->i_data.nrpages) {
@@ -476,18 +635,23 @@ static void prune_icache(int nr_to_scan)
476 iput(inode); 635 iput(inode);
477 spin_lock(&inode_lock); 636 spin_lock(&inode_lock);
478 637
479 if (inode != list_entry(inode_unused.next, 638 if (inode != list_entry(inode_lru.next,
480 struct inode, i_list)) 639 struct inode, i_lru))
481 continue; /* wrong inode or list_empty */ 640 continue; /* wrong inode or list_empty */
482 if (!can_unuse(inode)) 641 if (!can_unuse(inode))
483 continue; 642 continue;
484 } 643 }
485 list_move(&inode->i_list, &freeable);
486 WARN_ON(inode->i_state & I_NEW); 644 WARN_ON(inode->i_state & I_NEW);
487 inode->i_state |= I_FREEING; 645 inode->i_state |= I_FREEING;
488 nr_pruned++; 646
647 /*
648 * Move the inode off the IO lists and LRU once I_FREEING is
649 * set so that it won't get moved back on there if it is dirty.
650 */
651 list_move(&inode->i_lru, &freeable);
652 list_del_init(&inode->i_wb_list);
653 percpu_counter_dec(&nr_inodes_unused);
489 } 654 }
490 inodes_stat.nr_unused -= nr_pruned;
491 if (current_is_kswapd()) 655 if (current_is_kswapd())
492 __count_vm_events(KSWAPD_INODESTEAL, reap); 656 __count_vm_events(KSWAPD_INODESTEAL, reap);
493 else 657 else
@@ -519,7 +683,7 @@ static int shrink_icache_memory(struct shrinker *shrink, int nr, gfp_t gfp_mask)
519 return -1; 683 return -1;
520 prune_icache(nr); 684 prune_icache(nr);
521 } 685 }
522 return (inodes_stat.nr_unused / 100) * sysctl_vfs_cache_pressure; 686 return (get_nr_inodes_unused() / 100) * sysctl_vfs_cache_pressure;
523} 687}
524 688
525static struct shrinker icache_shrinker = { 689static struct shrinker icache_shrinker = {
@@ -530,9 +694,6 @@ static struct shrinker icache_shrinker = {
530static void __wait_on_freeing_inode(struct inode *inode); 694static void __wait_on_freeing_inode(struct inode *inode);
531/* 695/*
532 * Called with the inode lock held. 696 * Called with the inode lock held.
533 * NOTE: we are not increasing the inode-refcount, you must call __iget()
534 * by hand after calling find_inode now! This simplifies iunique and won't
535 * add any additional branch in the common code.
536 */ 697 */
537static struct inode *find_inode(struct super_block *sb, 698static struct inode *find_inode(struct super_block *sb,
538 struct hlist_head *head, 699 struct hlist_head *head,
@@ -552,9 +713,10 @@ repeat:
552 __wait_on_freeing_inode(inode); 713 __wait_on_freeing_inode(inode);
553 goto repeat; 714 goto repeat;
554 } 715 }
555 break; 716 __iget(inode);
717 return inode;
556 } 718 }
557 return node ? inode : NULL; 719 return NULL;
558} 720}
559 721
560/* 722/*
@@ -577,53 +739,49 @@ repeat:
577 __wait_on_freeing_inode(inode); 739 __wait_on_freeing_inode(inode);
578 goto repeat; 740 goto repeat;
579 } 741 }
580 break; 742 __iget(inode);
743 return inode;
581 } 744 }
582 return node ? inode : NULL; 745 return NULL;
583}
584
585static unsigned long hash(struct super_block *sb, unsigned long hashval)
586{
587 unsigned long tmp;
588
589 tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /
590 L1_CACHE_BYTES;
591 tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> I_HASHBITS);
592 return tmp & I_HASHMASK;
593}
594
595static inline void
596__inode_add_to_lists(struct super_block *sb, struct hlist_head *head,
597 struct inode *inode)
598{
599 inodes_stat.nr_inodes++;
600 list_add(&inode->i_list, &inode_in_use);
601 list_add(&inode->i_sb_list, &sb->s_inodes);
602 if (head)
603 hlist_add_head(&inode->i_hash, head);
604} 746}
605 747
606/** 748/*
607 * inode_add_to_lists - add a new inode to relevant lists 749 * Each cpu owns a range of LAST_INO_BATCH numbers.
608 * @sb: superblock inode belongs to 750 * 'shared_last_ino' is dirtied only once out of LAST_INO_BATCH allocations,
609 * @inode: inode to mark in use 751 * to renew the exhausted range.
610 * 752 *
611 * When an inode is allocated it needs to be accounted for, added to the in use 753 * This does not significantly increase overflow rate because every CPU can
612 * list, the owning superblock and the inode hash. This needs to be done under 754 * consume at most LAST_INO_BATCH-1 unused inode numbers. So there is
613 * the inode_lock, so export a function to do this rather than the inode lock 755 * NR_CPUS*(LAST_INO_BATCH-1) wastage. At 4096 and 1024, this is ~0.1% of the
614 * itself. We calculate the hash list to add to here so it is all internal 756 * 2^32 range, and is a worst-case. Even a 50% wastage would only increase
615 * which requires the caller to have already set up the inode number in the 757 * overflow rate by 2x, which does not seem too significant.
616 * inode to add. 758 *
759 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
760 * error if st_ino won't fit in target struct field. Use 32bit counter
761 * here to attempt to avoid that.
617 */ 762 */
618void inode_add_to_lists(struct super_block *sb, struct inode *inode) 763#define LAST_INO_BATCH 1024
764static DEFINE_PER_CPU(unsigned int, last_ino);
765
766unsigned int get_next_ino(void)
619{ 767{
620 struct hlist_head *head = inode_hashtable + hash(sb, inode->i_ino); 768 unsigned int *p = &get_cpu_var(last_ino);
769 unsigned int res = *p;
621 770
622 spin_lock(&inode_lock); 771#ifdef CONFIG_SMP
623 __inode_add_to_lists(sb, head, inode); 772 if (unlikely((res & (LAST_INO_BATCH-1)) == 0)) {
624 spin_unlock(&inode_lock); 773 static atomic_t shared_last_ino;
774 int next = atomic_add_return(LAST_INO_BATCH, &shared_last_ino);
775
776 res = next - LAST_INO_BATCH;
777 }
778#endif
779
780 *p = ++res;
781 put_cpu_var(last_ino);
782 return res;
625} 783}
626EXPORT_SYMBOL_GPL(inode_add_to_lists); 784EXPORT_SYMBOL(get_next_ino);
627 785
628/** 786/**
629 * new_inode - obtain an inode 787 * new_inode - obtain an inode
@@ -639,12 +797,6 @@ EXPORT_SYMBOL_GPL(inode_add_to_lists);
639 */ 797 */
640struct inode *new_inode(struct super_block *sb) 798struct inode *new_inode(struct super_block *sb)
641{ 799{
642 /*
643 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
644 * error if st_ino won't fit in target struct field. Use 32bit counter
645 * here to attempt to avoid that.
646 */
647 static unsigned int last_ino;
648 struct inode *inode; 800 struct inode *inode;
649 801
650 spin_lock_prefetch(&inode_lock); 802 spin_lock_prefetch(&inode_lock);
@@ -652,8 +804,7 @@ struct inode *new_inode(struct super_block *sb)
652 inode = alloc_inode(sb); 804 inode = alloc_inode(sb);
653 if (inode) { 805 if (inode) {
654 spin_lock(&inode_lock); 806 spin_lock(&inode_lock);
655 __inode_add_to_lists(sb, NULL, inode); 807 __inode_sb_list_add(inode);
656 inode->i_ino = ++last_ino;
657 inode->i_state = 0; 808 inode->i_state = 0;
658 spin_unlock(&inode_lock); 809 spin_unlock(&inode_lock);
659 } 810 }
@@ -664,7 +815,7 @@ EXPORT_SYMBOL(new_inode);
664void unlock_new_inode(struct inode *inode) 815void unlock_new_inode(struct inode *inode)
665{ 816{
666#ifdef CONFIG_DEBUG_LOCK_ALLOC 817#ifdef CONFIG_DEBUG_LOCK_ALLOC
667 if (inode->i_mode & S_IFDIR) { 818 if (S_ISDIR(inode->i_mode)) {
668 struct file_system_type *type = inode->i_sb->s_type; 819 struct file_system_type *type = inode->i_sb->s_type;
669 820
670 /* Set new key only if filesystem hasn't already changed it */ 821 /* Set new key only if filesystem hasn't already changed it */
@@ -721,7 +872,8 @@ static struct inode *get_new_inode(struct super_block *sb,
721 if (set(inode, data)) 872 if (set(inode, data))
722 goto set_failed; 873 goto set_failed;
723 874
724 __inode_add_to_lists(sb, head, inode); 875 hlist_add_head(&inode->i_hash, head);
876 __inode_sb_list_add(inode);
725 inode->i_state = I_NEW; 877 inode->i_state = I_NEW;
726 spin_unlock(&inode_lock); 878 spin_unlock(&inode_lock);
727 879
@@ -736,7 +888,6 @@ static struct inode *get_new_inode(struct super_block *sb,
736 * us. Use the old inode instead of the one we just 888 * us. Use the old inode instead of the one we just
737 * allocated. 889 * allocated.
738 */ 890 */
739 __iget(old);
740 spin_unlock(&inode_lock); 891 spin_unlock(&inode_lock);
741 destroy_inode(inode); 892 destroy_inode(inode);
742 inode = old; 893 inode = old;
@@ -768,7 +919,8 @@ static struct inode *get_new_inode_fast(struct super_block *sb,
768 old = find_inode_fast(sb, head, ino); 919 old = find_inode_fast(sb, head, ino);
769 if (!old) { 920 if (!old) {
770 inode->i_ino = ino; 921 inode->i_ino = ino;
771 __inode_add_to_lists(sb, head, inode); 922 hlist_add_head(&inode->i_hash, head);
923 __inode_sb_list_add(inode);
772 inode->i_state = I_NEW; 924 inode->i_state = I_NEW;
773 spin_unlock(&inode_lock); 925 spin_unlock(&inode_lock);
774 926
@@ -783,7 +935,6 @@ static struct inode *get_new_inode_fast(struct super_block *sb,
783 * us. Use the old inode instead of the one we just 935 * us. Use the old inode instead of the one we just
784 * allocated. 936 * allocated.
785 */ 937 */
786 __iget(old);
787 spin_unlock(&inode_lock); 938 spin_unlock(&inode_lock);
788 destroy_inode(inode); 939 destroy_inode(inode);
789 inode = old; 940 inode = old;
@@ -792,6 +943,27 @@ static struct inode *get_new_inode_fast(struct super_block *sb,
792 return inode; 943 return inode;
793} 944}
794 945
946/*
947 * search the inode cache for a matching inode number.
948 * If we find one, then the inode number we are trying to
949 * allocate is not unique and so we should not use it.
950 *
951 * Returns 1 if the inode number is unique, 0 if it is not.
952 */
953static int test_inode_iunique(struct super_block *sb, unsigned long ino)
954{
955 struct hlist_head *b = inode_hashtable + hash(sb, ino);
956 struct hlist_node *node;
957 struct inode *inode;
958
959 hlist_for_each_entry(inode, node, b, i_hash) {
960 if (inode->i_ino == ino && inode->i_sb == sb)
961 return 0;
962 }
963
964 return 1;
965}
966
795/** 967/**
796 * iunique - get a unique inode number 968 * iunique - get a unique inode number
797 * @sb: superblock 969 * @sb: superblock
@@ -813,19 +985,18 @@ ino_t iunique(struct super_block *sb, ino_t max_reserved)
813 * error if st_ino won't fit in target struct field. Use 32bit counter 985 * error if st_ino won't fit in target struct field. Use 32bit counter
814 * here to attempt to avoid that. 986 * here to attempt to avoid that.
815 */ 987 */
988 static DEFINE_SPINLOCK(iunique_lock);
816 static unsigned int counter; 989 static unsigned int counter;
817 struct inode *inode;
818 struct hlist_head *head;
819 ino_t res; 990 ino_t res;
820 991
821 spin_lock(&inode_lock); 992 spin_lock(&inode_lock);
993 spin_lock(&iunique_lock);
822 do { 994 do {
823 if (counter <= max_reserved) 995 if (counter <= max_reserved)
824 counter = max_reserved + 1; 996 counter = max_reserved + 1;
825 res = counter++; 997 res = counter++;
826 head = inode_hashtable + hash(sb, res); 998 } while (!test_inode_iunique(sb, res));
827 inode = find_inode_fast(sb, head, res); 999 spin_unlock(&iunique_lock);
828 } while (inode != NULL);
829 spin_unlock(&inode_lock); 1000 spin_unlock(&inode_lock);
830 1001
831 return res; 1002 return res;
@@ -877,7 +1048,6 @@ static struct inode *ifind(struct super_block *sb,
877 spin_lock(&inode_lock); 1048 spin_lock(&inode_lock);
878 inode = find_inode(sb, head, test, data); 1049 inode = find_inode(sb, head, test, data);
879 if (inode) { 1050 if (inode) {
880 __iget(inode);
881 spin_unlock(&inode_lock); 1051 spin_unlock(&inode_lock);
882 if (likely(wait)) 1052 if (likely(wait))
883 wait_on_inode(inode); 1053 wait_on_inode(inode);
@@ -910,7 +1080,6 @@ static struct inode *ifind_fast(struct super_block *sb,
910 spin_lock(&inode_lock); 1080 spin_lock(&inode_lock);
911 inode = find_inode_fast(sb, head, ino); 1081 inode = find_inode_fast(sb, head, ino);
912 if (inode) { 1082 if (inode) {
913 __iget(inode);
914 spin_unlock(&inode_lock); 1083 spin_unlock(&inode_lock);
915 wait_on_inode(inode); 1084 wait_on_inode(inode);
916 return inode; 1085 return inode;
@@ -1096,7 +1265,7 @@ int insert_inode_locked(struct inode *inode)
1096 __iget(old); 1265 __iget(old);
1097 spin_unlock(&inode_lock); 1266 spin_unlock(&inode_lock);
1098 wait_on_inode(old); 1267 wait_on_inode(old);
1099 if (unlikely(!hlist_unhashed(&old->i_hash))) { 1268 if (unlikely(!inode_unhashed(old))) {
1100 iput(old); 1269 iput(old);
1101 return -EBUSY; 1270 return -EBUSY;
1102 } 1271 }
@@ -1135,7 +1304,7 @@ int insert_inode_locked4(struct inode *inode, unsigned long hashval,
1135 __iget(old); 1304 __iget(old);
1136 spin_unlock(&inode_lock); 1305 spin_unlock(&inode_lock);
1137 wait_on_inode(old); 1306 wait_on_inode(old);
1138 if (unlikely(!hlist_unhashed(&old->i_hash))) { 1307 if (unlikely(!inode_unhashed(old))) {
1139 iput(old); 1308 iput(old);
1140 return -EBUSY; 1309 return -EBUSY;
1141 } 1310 }
@@ -1144,36 +1313,6 @@ int insert_inode_locked4(struct inode *inode, unsigned long hashval,
1144} 1313}
1145EXPORT_SYMBOL(insert_inode_locked4); 1314EXPORT_SYMBOL(insert_inode_locked4);
1146 1315
1147/**
1148 * __insert_inode_hash - hash an inode
1149 * @inode: unhashed inode
1150 * @hashval: unsigned long value used to locate this object in the
1151 * inode_hashtable.
1152 *
1153 * Add an inode to the inode hash for this superblock.
1154 */
1155void __insert_inode_hash(struct inode *inode, unsigned long hashval)
1156{
1157 struct hlist_head *head = inode_hashtable + hash(inode->i_sb, hashval);
1158 spin_lock(&inode_lock);
1159 hlist_add_head(&inode->i_hash, head);
1160 spin_unlock(&inode_lock);
1161}
1162EXPORT_SYMBOL(__insert_inode_hash);
1163
1164/**
1165 * remove_inode_hash - remove an inode from the hash
1166 * @inode: inode to unhash
1167 *
1168 * Remove an inode from the superblock.
1169 */
1170void remove_inode_hash(struct inode *inode)
1171{
1172 spin_lock(&inode_lock);
1173 hlist_del_init(&inode->i_hash);
1174 spin_unlock(&inode_lock);
1175}
1176EXPORT_SYMBOL(remove_inode_hash);
1177 1316
1178int generic_delete_inode(struct inode *inode) 1317int generic_delete_inode(struct inode *inode)
1179{ 1318{
@@ -1188,7 +1327,7 @@ EXPORT_SYMBOL(generic_delete_inode);
1188 */ 1327 */
1189int generic_drop_inode(struct inode *inode) 1328int generic_drop_inode(struct inode *inode)
1190{ 1329{
1191 return !inode->i_nlink || hlist_unhashed(&inode->i_hash); 1330 return !inode->i_nlink || inode_unhashed(inode);
1192} 1331}
1193EXPORT_SYMBOL_GPL(generic_drop_inode); 1332EXPORT_SYMBOL_GPL(generic_drop_inode);
1194 1333
@@ -1214,10 +1353,11 @@ static void iput_final(struct inode *inode)
1214 drop = generic_drop_inode(inode); 1353 drop = generic_drop_inode(inode);
1215 1354
1216 if (!drop) { 1355 if (!drop) {
1217 if (!(inode->i_state & (I_DIRTY|I_SYNC)))
1218 list_move(&inode->i_list, &inode_unused);
1219 inodes_stat.nr_unused++;
1220 if (sb->s_flags & MS_ACTIVE) { 1356 if (sb->s_flags & MS_ACTIVE) {
1357 inode->i_state |= I_REFERENCED;
1358 if (!(inode->i_state & (I_DIRTY|I_SYNC))) {
1359 inode_lru_list_add(inode);
1360 }
1221 spin_unlock(&inode_lock); 1361 spin_unlock(&inode_lock);
1222 return; 1362 return;
1223 } 1363 }
@@ -1228,19 +1368,23 @@ static void iput_final(struct inode *inode)
1228 spin_lock(&inode_lock); 1368 spin_lock(&inode_lock);
1229 WARN_ON(inode->i_state & I_NEW); 1369 WARN_ON(inode->i_state & I_NEW);
1230 inode->i_state &= ~I_WILL_FREE; 1370 inode->i_state &= ~I_WILL_FREE;
1231 inodes_stat.nr_unused--; 1371 __remove_inode_hash(inode);
1232 hlist_del_init(&inode->i_hash);
1233 } 1372 }
1234 list_del_init(&inode->i_list); 1373
1235 list_del_init(&inode->i_sb_list);
1236 WARN_ON(inode->i_state & I_NEW); 1374 WARN_ON(inode->i_state & I_NEW);
1237 inode->i_state |= I_FREEING; 1375 inode->i_state |= I_FREEING;
1238 inodes_stat.nr_inodes--; 1376
1377 /*
1378 * Move the inode off the IO lists and LRU once I_FREEING is
1379 * set so that it won't get moved back on there if it is dirty.
1380 */
1381 inode_lru_list_del(inode);
1382 list_del_init(&inode->i_wb_list);
1383
1384 __inode_sb_list_del(inode);
1239 spin_unlock(&inode_lock); 1385 spin_unlock(&inode_lock);
1240 evict(inode); 1386 evict(inode);
1241 spin_lock(&inode_lock); 1387 remove_inode_hash(inode);
1242 hlist_del_init(&inode->i_hash);
1243 spin_unlock(&inode_lock);
1244 wake_up_inode(inode); 1388 wake_up_inode(inode);
1245 BUG_ON(inode->i_state != (I_FREEING | I_CLEAR)); 1389 BUG_ON(inode->i_state != (I_FREEING | I_CLEAR));
1246 destroy_inode(inode); 1390 destroy_inode(inode);
@@ -1504,6 +1648,8 @@ void __init inode_init(void)
1504 SLAB_MEM_SPREAD), 1648 SLAB_MEM_SPREAD),
1505 init_once); 1649 init_once);
1506 register_shrinker(&icache_shrinker); 1650 register_shrinker(&icache_shrinker);
1651 percpu_counter_init(&nr_inodes, 0);
1652 percpu_counter_init(&nr_inodes_unused, 0);
1507 1653
1508 /* Hash may have been set up in inode_init_early */ 1654 /* Hash may have been set up in inode_init_early */
1509 if (!hashdist) 1655 if (!hashdist)