aboutsummaryrefslogtreecommitdiffstats
path: root/fs/inode.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/inode.c')
-rw-r--r--fs/inode.c527
1 files changed, 337 insertions, 190 deletions
diff --git a/fs/inode.c b/fs/inode.c
index 86464332e590..ae2727ab0c3a 100644
--- a/fs/inode.c
+++ b/fs/inode.c
@@ -24,11 +24,11 @@
24#include <linux/mount.h> 24#include <linux/mount.h>
25#include <linux/async.h> 25#include <linux/async.h>
26#include <linux/posix_acl.h> 26#include <linux/posix_acl.h>
27#include <linux/ima.h>
27 28
28/* 29/*
29 * This is needed for the following functions: 30 * This is needed for the following functions:
30 * - inode_has_buffers 31 * - inode_has_buffers
31 * - invalidate_inode_buffers
32 * - invalidate_bdev 32 * - invalidate_bdev
33 * 33 *
34 * FIXME: remove all knowledge of the buffer layer from this file 34 * FIXME: remove all knowledge of the buffer layer from this file
@@ -72,8 +72,7 @@ static unsigned int i_hash_shift __read_mostly;
72 * allowing for low-overhead inode sync() operations. 72 * allowing for low-overhead inode sync() operations.
73 */ 73 */
74 74
75LIST_HEAD(inode_in_use); 75static LIST_HEAD(inode_lru);
76LIST_HEAD(inode_unused);
77static struct hlist_head *inode_hashtable __read_mostly; 76static struct hlist_head *inode_hashtable __read_mostly;
78 77
79/* 78/*
@@ -103,8 +102,41 @@ static DECLARE_RWSEM(iprune_sem);
103 */ 102 */
104struct inodes_stat_t inodes_stat; 103struct inodes_stat_t inodes_stat;
105 104
105static struct percpu_counter nr_inodes __cacheline_aligned_in_smp;
106static struct percpu_counter nr_inodes_unused __cacheline_aligned_in_smp;
107
106static struct kmem_cache *inode_cachep __read_mostly; 108static struct kmem_cache *inode_cachep __read_mostly;
107 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
108static void wake_up_inode(struct inode *inode) 140static void wake_up_inode(struct inode *inode)
109{ 141{
110 /* 142 /*
@@ -192,6 +224,8 @@ int inode_init_always(struct super_block *sb, struct inode *inode)
192 inode->i_fsnotify_mask = 0; 224 inode->i_fsnotify_mask = 0;
193#endif 225#endif
194 226
227 percpu_counter_inc(&nr_inodes);
228
195 return 0; 229 return 0;
196out: 230out:
197 return -ENOMEM; 231 return -ENOMEM;
@@ -232,11 +266,13 @@ void __destroy_inode(struct inode *inode)
232 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)
233 posix_acl_release(inode->i_default_acl); 267 posix_acl_release(inode->i_default_acl);
234#endif 268#endif
269 percpu_counter_dec(&nr_inodes);
235} 270}
236EXPORT_SYMBOL(__destroy_inode); 271EXPORT_SYMBOL(__destroy_inode);
237 272
238void destroy_inode(struct inode *inode) 273static void destroy_inode(struct inode *inode)
239{ 274{
275 BUG_ON(!list_empty(&inode->i_lru));
240 __destroy_inode(inode); 276 __destroy_inode(inode);
241 if (inode->i_sb->s_op->destroy_inode) 277 if (inode->i_sb->s_op->destroy_inode)
242 inode->i_sb->s_op->destroy_inode(inode); 278 inode->i_sb->s_op->destroy_inode(inode);
@@ -255,6 +291,8 @@ void inode_init_once(struct inode *inode)
255 INIT_HLIST_NODE(&inode->i_hash); 291 INIT_HLIST_NODE(&inode->i_hash);
256 INIT_LIST_HEAD(&inode->i_dentry); 292 INIT_LIST_HEAD(&inode->i_dentry);
257 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);
258 INIT_RADIX_TREE(&inode->i_data.page_tree, GFP_ATOMIC); 296 INIT_RADIX_TREE(&inode->i_data.page_tree, GFP_ATOMIC);
259 spin_lock_init(&inode->i_data.tree_lock); 297 spin_lock_init(&inode->i_data.tree_lock);
260 spin_lock_init(&inode->i_data.i_mmap_lock); 298 spin_lock_init(&inode->i_data.i_mmap_lock);
@@ -281,14 +319,109 @@ static void init_once(void *foo)
281 */ 319 */
282void __iget(struct inode *inode) 320void __iget(struct inode *inode)
283{ 321{
284 if (atomic_inc_return(&inode->i_count) != 1) 322 atomic_inc(&inode->i_count);
285 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}
286 341
287 if (!(inode->i_state & (I_DIRTY|I_SYNC))) 342static void inode_lru_list_del(struct inode *inode)
288 list_move(&inode->i_list, &inode_in_use); 343{
289 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);
290} 353}
291 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
292void end_writeback(struct inode *inode) 425void end_writeback(struct inode *inode)
293{ 426{
294 might_sleep(); 427 might_sleep();
@@ -327,101 +460,113 @@ static void evict(struct inode *inode)
327 */ 460 */
328static void dispose_list(struct list_head *head) 461static void dispose_list(struct list_head *head)
329{ 462{
330 int nr_disposed = 0;
331
332 while (!list_empty(head)) { 463 while (!list_empty(head)) {
333 struct inode *inode; 464 struct inode *inode;
334 465
335 inode = list_first_entry(head, struct inode, i_list); 466 inode = list_first_entry(head, struct inode, i_lru);
336 list_del(&inode->i_list); 467 list_del_init(&inode->i_lru);
337 468
338 evict(inode); 469 evict(inode);
339 470
340 spin_lock(&inode_lock); 471 spin_lock(&inode_lock);
341 hlist_del_init(&inode->i_hash); 472 __remove_inode_hash(inode);
342 list_del_init(&inode->i_sb_list); 473 __inode_sb_list_del(inode);
343 spin_unlock(&inode_lock); 474 spin_unlock(&inode_lock);
344 475
345 wake_up_inode(inode); 476 wake_up_inode(inode);
346 destroy_inode(inode); 477 destroy_inode(inode);
347 nr_disposed++;
348 } 478 }
349 spin_lock(&inode_lock);
350 inodes_stat.nr_inodes -= nr_disposed;
351 spin_unlock(&inode_lock);
352} 479}
353 480
354/* 481/**
355 * 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.
356 */ 489 */
357static int invalidate_list(struct list_head *head, struct list_head *dispose) 490void evict_inodes(struct super_block *sb)
358{ 491{
359 struct list_head *next; 492 struct inode *inode, *next;
360 int busy = 0, count = 0; 493 LIST_HEAD(dispose);
361
362 next = head->next;
363 for (;;) {
364 struct list_head *tmp = next;
365 struct inode *inode;
366 494
367 /* 495 down_write(&iprune_sem);
368 * We can reschedule here without worrying about the list's
369 * consistency because the per-sb list of inodes must not
370 * change during umount anymore, and because iprune_sem keeps
371 * shrink_icache_memory() away.
372 */
373 cond_resched_lock(&inode_lock);
374 496
375 next = next->next; 497 spin_lock(&inode_lock);
376 if (tmp == head) 498 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
377 break; 499 if (atomic_read(&inode->i_count))
378 inode = list_entry(tmp, struct inode, i_sb_list);
379 if (inode->i_state & I_NEW)
380 continue; 500 continue;
381 invalidate_inode_buffers(inode); 501
382 if (!atomic_read(&inode->i_count)) { 502 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
383 list_move(&inode->i_list, dispose); 503 WARN_ON(1);
384 WARN_ON(inode->i_state & I_NEW);
385 inode->i_state |= I_FREEING;
386 count++;
387 continue; 504 continue;
388 } 505 }
389 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);
390 } 517 }
391 /* only unused inodes may be cached with i_count zero */ 518 spin_unlock(&inode_lock);
392 inodes_stat.nr_unused -= count; 519
393 return busy; 520 dispose_list(&dispose);
521 up_write(&iprune_sem);
394} 522}
395 523
396/** 524/**
397 * invalidate_inodes - discard the inodes on a device 525 * invalidate_inodes - attempt to free all inodes on a superblock
398 * @sb: superblock 526 * @sb: superblock to operate on
399 * 527 *
400 * 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
401 * fails because there are busy inodes then a non zero value is returned. 529 * busy inodes return a non-zero value, else zero.
402 * If the discard is successful all the inodes have been discarded.
403 */ 530 */
404int invalidate_inodes(struct super_block *sb) 531int invalidate_inodes(struct super_block *sb)
405{ 532{
406 int busy; 533 int busy = 0;
407 LIST_HEAD(throw_away); 534 struct inode *inode, *next;
535 LIST_HEAD(dispose);
408 536
409 down_write(&iprune_sem); 537 down_write(&iprune_sem);
538
410 spin_lock(&inode_lock); 539 spin_lock(&inode_lock);
411 fsnotify_unmount_inodes(&sb->s_inodes); 540 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
412 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 }
413 spin_unlock(&inode_lock); 559 spin_unlock(&inode_lock);
414 560
415 dispose_list(&throw_away); 561 dispose_list(&dispose);
416 up_write(&iprune_sem); 562 up_write(&iprune_sem);
417 563
418 return busy; 564 return busy;
419} 565}
420EXPORT_SYMBOL(invalidate_inodes);
421 566
422static int can_unuse(struct inode *inode) 567static int can_unuse(struct inode *inode)
423{ 568{
424 if (inode->i_state) 569 if (inode->i_state & ~I_REFERENCED)
425 return 0; 570 return 0;
426 if (inode_has_buffers(inode)) 571 if (inode_has_buffers(inode))
427 return 0; 572 return 0;
@@ -433,22 +578,24 @@ static int can_unuse(struct inode *inode)
433} 578}
434 579
435/* 580/*
436 * 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
437 * 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().
438 * 583 *
439 * 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
440 * 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
441 * the front of the inode_unused list. So look for it there and if the 586 * mapping->private_list then try to remove them.
442 * inode is still freeable, proceed. The right inode is found 99.9% of the
443 * time in testing on a 4-way.
444 * 587 *
445 * 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
446 * 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.
447 */ 595 */
448static void prune_icache(int nr_to_scan) 596static void prune_icache(int nr_to_scan)
449{ 597{
450 LIST_HEAD(freeable); 598 LIST_HEAD(freeable);
451 int nr_pruned = 0;
452 int nr_scanned; 599 int nr_scanned;
453 unsigned long reap = 0; 600 unsigned long reap = 0;
454 601
@@ -457,13 +604,26 @@ static void prune_icache(int nr_to_scan)
457 for (nr_scanned = 0; nr_scanned < nr_to_scan; nr_scanned++) { 604 for (nr_scanned = 0; nr_scanned < nr_to_scan; nr_scanned++) {
458 struct inode *inode; 605 struct inode *inode;
459 606
460 if (list_empty(&inode_unused)) 607 if (list_empty(&inode_lru))
461 break; 608 break;
462 609
463 inode = list_entry(inode_unused.prev, struct inode, i_list); 610 inode = list_entry(inode_lru.prev, struct inode, i_lru);
464 611
465 if (inode->i_state || atomic_read(&inode->i_count)) { 612 /*
466 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;
467 continue; 627 continue;
468 } 628 }
469 if (inode_has_buffers(inode) || inode->i_data.nrpages) { 629 if (inode_has_buffers(inode) || inode->i_data.nrpages) {
@@ -475,18 +635,23 @@ static void prune_icache(int nr_to_scan)
475 iput(inode); 635 iput(inode);
476 spin_lock(&inode_lock); 636 spin_lock(&inode_lock);
477 637
478 if (inode != list_entry(inode_unused.next, 638 if (inode != list_entry(inode_lru.next,
479 struct inode, i_list)) 639 struct inode, i_lru))
480 continue; /* wrong inode or list_empty */ 640 continue; /* wrong inode or list_empty */
481 if (!can_unuse(inode)) 641 if (!can_unuse(inode))
482 continue; 642 continue;
483 } 643 }
484 list_move(&inode->i_list, &freeable);
485 WARN_ON(inode->i_state & I_NEW); 644 WARN_ON(inode->i_state & I_NEW);
486 inode->i_state |= I_FREEING; 645 inode->i_state |= I_FREEING;
487 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);
488 } 654 }
489 inodes_stat.nr_unused -= nr_pruned;
490 if (current_is_kswapd()) 655 if (current_is_kswapd())
491 __count_vm_events(KSWAPD_INODESTEAL, reap); 656 __count_vm_events(KSWAPD_INODESTEAL, reap);
492 else 657 else
@@ -518,7 +683,7 @@ static int shrink_icache_memory(struct shrinker *shrink, int nr, gfp_t gfp_mask)
518 return -1; 683 return -1;
519 prune_icache(nr); 684 prune_icache(nr);
520 } 685 }
521 return (inodes_stat.nr_unused / 100) * sysctl_vfs_cache_pressure; 686 return (get_nr_inodes_unused() / 100) * sysctl_vfs_cache_pressure;
522} 687}
523 688
524static struct shrinker icache_shrinker = { 689static struct shrinker icache_shrinker = {
@@ -529,9 +694,6 @@ static struct shrinker icache_shrinker = {
529static void __wait_on_freeing_inode(struct inode *inode); 694static void __wait_on_freeing_inode(struct inode *inode);
530/* 695/*
531 * Called with the inode lock held. 696 * Called with the inode lock held.
532 * NOTE: we are not increasing the inode-refcount, you must call __iget()
533 * by hand after calling find_inode now! This simplifies iunique and won't
534 * add any additional branch in the common code.
535 */ 697 */
536static struct inode *find_inode(struct super_block *sb, 698static struct inode *find_inode(struct super_block *sb,
537 struct hlist_head *head, 699 struct hlist_head *head,
@@ -551,9 +713,10 @@ repeat:
551 __wait_on_freeing_inode(inode); 713 __wait_on_freeing_inode(inode);
552 goto repeat; 714 goto repeat;
553 } 715 }
554 break; 716 __iget(inode);
717 return inode;
555 } 718 }
556 return node ? inode : NULL; 719 return NULL;
557} 720}
558 721
559/* 722/*
@@ -576,53 +739,49 @@ repeat:
576 __wait_on_freeing_inode(inode); 739 __wait_on_freeing_inode(inode);
577 goto repeat; 740 goto repeat;
578 } 741 }
579 break; 742 __iget(inode);
743 return inode;
580 } 744 }
581 return node ? inode : NULL; 745 return NULL;
582}
583
584static unsigned long hash(struct super_block *sb, unsigned long hashval)
585{
586 unsigned long tmp;
587
588 tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /
589 L1_CACHE_BYTES;
590 tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> I_HASHBITS);
591 return tmp & I_HASHMASK;
592}
593
594static inline void
595__inode_add_to_lists(struct super_block *sb, struct hlist_head *head,
596 struct inode *inode)
597{
598 inodes_stat.nr_inodes++;
599 list_add(&inode->i_list, &inode_in_use);
600 list_add(&inode->i_sb_list, &sb->s_inodes);
601 if (head)
602 hlist_add_head(&inode->i_hash, head);
603} 746}
604 747
605/** 748/*
606 * inode_add_to_lists - add a new inode to relevant lists 749 * Each cpu owns a range of LAST_INO_BATCH numbers.
607 * @sb: superblock inode belongs to 750 * 'shared_last_ino' is dirtied only once out of LAST_INO_BATCH allocations,
608 * @inode: inode to mark in use 751 * to renew the exhausted range.
609 * 752 *
610 * 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
611 * 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
612 * 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
613 * 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
614 * 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.
615 * 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.
616 */ 762 */
617void 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)
618{ 767{
619 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;
620 770
621 spin_lock(&inode_lock); 771#ifdef CONFIG_SMP
622 __inode_add_to_lists(sb, head, inode); 772 if (unlikely((res & (LAST_INO_BATCH-1)) == 0)) {
623 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;
624} 783}
625EXPORT_SYMBOL_GPL(inode_add_to_lists); 784EXPORT_SYMBOL(get_next_ino);
626 785
627/** 786/**
628 * new_inode - obtain an inode 787 * new_inode - obtain an inode
@@ -638,12 +797,6 @@ EXPORT_SYMBOL_GPL(inode_add_to_lists);
638 */ 797 */
639struct inode *new_inode(struct super_block *sb) 798struct inode *new_inode(struct super_block *sb)
640{ 799{
641 /*
642 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
643 * error if st_ino won't fit in target struct field. Use 32bit counter
644 * here to attempt to avoid that.
645 */
646 static unsigned int last_ino;
647 struct inode *inode; 800 struct inode *inode;
648 801
649 spin_lock_prefetch(&inode_lock); 802 spin_lock_prefetch(&inode_lock);
@@ -651,8 +804,7 @@ struct inode *new_inode(struct super_block *sb)
651 inode = alloc_inode(sb); 804 inode = alloc_inode(sb);
652 if (inode) { 805 if (inode) {
653 spin_lock(&inode_lock); 806 spin_lock(&inode_lock);
654 __inode_add_to_lists(sb, NULL, inode); 807 __inode_sb_list_add(inode);
655 inode->i_ino = ++last_ino;
656 inode->i_state = 0; 808 inode->i_state = 0;
657 spin_unlock(&inode_lock); 809 spin_unlock(&inode_lock);
658 } 810 }
@@ -663,7 +815,7 @@ EXPORT_SYMBOL(new_inode);
663void unlock_new_inode(struct inode *inode) 815void unlock_new_inode(struct inode *inode)
664{ 816{
665#ifdef CONFIG_DEBUG_LOCK_ALLOC 817#ifdef CONFIG_DEBUG_LOCK_ALLOC
666 if (inode->i_mode & S_IFDIR) { 818 if (S_ISDIR(inode->i_mode)) {
667 struct file_system_type *type = inode->i_sb->s_type; 819 struct file_system_type *type = inode->i_sb->s_type;
668 820
669 /* Set new key only if filesystem hasn't already changed it */ 821 /* Set new key only if filesystem hasn't already changed it */
@@ -720,7 +872,8 @@ static struct inode *get_new_inode(struct super_block *sb,
720 if (set(inode, data)) 872 if (set(inode, data))
721 goto set_failed; 873 goto set_failed;
722 874
723 __inode_add_to_lists(sb, head, inode); 875 hlist_add_head(&inode->i_hash, head);
876 __inode_sb_list_add(inode);
724 inode->i_state = I_NEW; 877 inode->i_state = I_NEW;
725 spin_unlock(&inode_lock); 878 spin_unlock(&inode_lock);
726 879
@@ -735,7 +888,6 @@ static struct inode *get_new_inode(struct super_block *sb,
735 * us. Use the old inode instead of the one we just 888 * us. Use the old inode instead of the one we just
736 * allocated. 889 * allocated.
737 */ 890 */
738 __iget(old);
739 spin_unlock(&inode_lock); 891 spin_unlock(&inode_lock);
740 destroy_inode(inode); 892 destroy_inode(inode);
741 inode = old; 893 inode = old;
@@ -767,7 +919,8 @@ static struct inode *get_new_inode_fast(struct super_block *sb,
767 old = find_inode_fast(sb, head, ino); 919 old = find_inode_fast(sb, head, ino);
768 if (!old) { 920 if (!old) {
769 inode->i_ino = ino; 921 inode->i_ino = ino;
770 __inode_add_to_lists(sb, head, inode); 922 hlist_add_head(&inode->i_hash, head);
923 __inode_sb_list_add(inode);
771 inode->i_state = I_NEW; 924 inode->i_state = I_NEW;
772 spin_unlock(&inode_lock); 925 spin_unlock(&inode_lock);
773 926
@@ -782,7 +935,6 @@ static struct inode *get_new_inode_fast(struct super_block *sb,
782 * us. Use the old inode instead of the one we just 935 * us. Use the old inode instead of the one we just
783 * allocated. 936 * allocated.
784 */ 937 */
785 __iget(old);
786 spin_unlock(&inode_lock); 938 spin_unlock(&inode_lock);
787 destroy_inode(inode); 939 destroy_inode(inode);
788 inode = old; 940 inode = old;
@@ -791,6 +943,27 @@ static struct inode *get_new_inode_fast(struct super_block *sb,
791 return inode; 943 return inode;
792} 944}
793 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
794/** 967/**
795 * iunique - get a unique inode number 968 * iunique - get a unique inode number
796 * @sb: superblock 969 * @sb: superblock
@@ -812,19 +985,18 @@ ino_t iunique(struct super_block *sb, ino_t max_reserved)
812 * 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
813 * here to attempt to avoid that. 986 * here to attempt to avoid that.
814 */ 987 */
988 static DEFINE_SPINLOCK(iunique_lock);
815 static unsigned int counter; 989 static unsigned int counter;
816 struct inode *inode;
817 struct hlist_head *head;
818 ino_t res; 990 ino_t res;
819 991
820 spin_lock(&inode_lock); 992 spin_lock(&inode_lock);
993 spin_lock(&iunique_lock);
821 do { 994 do {
822 if (counter <= max_reserved) 995 if (counter <= max_reserved)
823 counter = max_reserved + 1; 996 counter = max_reserved + 1;
824 res = counter++; 997 res = counter++;
825 head = inode_hashtable + hash(sb, res); 998 } while (!test_inode_iunique(sb, res));
826 inode = find_inode_fast(sb, head, res); 999 spin_unlock(&iunique_lock);
827 } while (inode != NULL);
828 spin_unlock(&inode_lock); 1000 spin_unlock(&inode_lock);
829 1001
830 return res; 1002 return res;
@@ -876,7 +1048,6 @@ static struct inode *ifind(struct super_block *sb,
876 spin_lock(&inode_lock); 1048 spin_lock(&inode_lock);
877 inode = find_inode(sb, head, test, data); 1049 inode = find_inode(sb, head, test, data);
878 if (inode) { 1050 if (inode) {
879 __iget(inode);
880 spin_unlock(&inode_lock); 1051 spin_unlock(&inode_lock);
881 if (likely(wait)) 1052 if (likely(wait))
882 wait_on_inode(inode); 1053 wait_on_inode(inode);
@@ -909,7 +1080,6 @@ static struct inode *ifind_fast(struct super_block *sb,
909 spin_lock(&inode_lock); 1080 spin_lock(&inode_lock);
910 inode = find_inode_fast(sb, head, ino); 1081 inode = find_inode_fast(sb, head, ino);
911 if (inode) { 1082 if (inode) {
912 __iget(inode);
913 spin_unlock(&inode_lock); 1083 spin_unlock(&inode_lock);
914 wait_on_inode(inode); 1084 wait_on_inode(inode);
915 return inode; 1085 return inode;
@@ -1095,7 +1265,7 @@ int insert_inode_locked(struct inode *inode)
1095 __iget(old); 1265 __iget(old);
1096 spin_unlock(&inode_lock); 1266 spin_unlock(&inode_lock);
1097 wait_on_inode(old); 1267 wait_on_inode(old);
1098 if (unlikely(!hlist_unhashed(&old->i_hash))) { 1268 if (unlikely(!inode_unhashed(old))) {
1099 iput(old); 1269 iput(old);
1100 return -EBUSY; 1270 return -EBUSY;
1101 } 1271 }
@@ -1134,7 +1304,7 @@ int insert_inode_locked4(struct inode *inode, unsigned long hashval,
1134 __iget(old); 1304 __iget(old);
1135 spin_unlock(&inode_lock); 1305 spin_unlock(&inode_lock);
1136 wait_on_inode(old); 1306 wait_on_inode(old);
1137 if (unlikely(!hlist_unhashed(&old->i_hash))) { 1307 if (unlikely(!inode_unhashed(old))) {
1138 iput(old); 1308 iput(old);
1139 return -EBUSY; 1309 return -EBUSY;
1140 } 1310 }
@@ -1143,36 +1313,6 @@ int insert_inode_locked4(struct inode *inode, unsigned long hashval,
1143} 1313}
1144EXPORT_SYMBOL(insert_inode_locked4); 1314EXPORT_SYMBOL(insert_inode_locked4);
1145 1315
1146/**
1147 * __insert_inode_hash - hash an inode
1148 * @inode: unhashed inode
1149 * @hashval: unsigned long value used to locate this object in the
1150 * inode_hashtable.
1151 *
1152 * Add an inode to the inode hash for this superblock.
1153 */
1154void __insert_inode_hash(struct inode *inode, unsigned long hashval)
1155{
1156 struct hlist_head *head = inode_hashtable + hash(inode->i_sb, hashval);
1157 spin_lock(&inode_lock);
1158 hlist_add_head(&inode->i_hash, head);
1159 spin_unlock(&inode_lock);
1160}
1161EXPORT_SYMBOL(__insert_inode_hash);
1162
1163/**
1164 * remove_inode_hash - remove an inode from the hash
1165 * @inode: inode to unhash
1166 *
1167 * Remove an inode from the superblock.
1168 */
1169void remove_inode_hash(struct inode *inode)
1170{
1171 spin_lock(&inode_lock);
1172 hlist_del_init(&inode->i_hash);
1173 spin_unlock(&inode_lock);
1174}
1175EXPORT_SYMBOL(remove_inode_hash);
1176 1316
1177int generic_delete_inode(struct inode *inode) 1317int generic_delete_inode(struct inode *inode)
1178{ 1318{
@@ -1187,7 +1327,7 @@ EXPORT_SYMBOL(generic_delete_inode);
1187 */ 1327 */
1188int generic_drop_inode(struct inode *inode) 1328int generic_drop_inode(struct inode *inode)
1189{ 1329{
1190 return !inode->i_nlink || hlist_unhashed(&inode->i_hash); 1330 return !inode->i_nlink || inode_unhashed(inode);
1191} 1331}
1192EXPORT_SYMBOL_GPL(generic_drop_inode); 1332EXPORT_SYMBOL_GPL(generic_drop_inode);
1193 1333
@@ -1213,10 +1353,11 @@ static void iput_final(struct inode *inode)
1213 drop = generic_drop_inode(inode); 1353 drop = generic_drop_inode(inode);
1214 1354
1215 if (!drop) { 1355 if (!drop) {
1216 if (!(inode->i_state & (I_DIRTY|I_SYNC)))
1217 list_move(&inode->i_list, &inode_unused);
1218 inodes_stat.nr_unused++;
1219 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 }
1220 spin_unlock(&inode_lock); 1361 spin_unlock(&inode_lock);
1221 return; 1362 return;
1222 } 1363 }
@@ -1227,19 +1368,23 @@ static void iput_final(struct inode *inode)
1227 spin_lock(&inode_lock); 1368 spin_lock(&inode_lock);
1228 WARN_ON(inode->i_state & I_NEW); 1369 WARN_ON(inode->i_state & I_NEW);
1229 inode->i_state &= ~I_WILL_FREE; 1370 inode->i_state &= ~I_WILL_FREE;
1230 inodes_stat.nr_unused--; 1371 __remove_inode_hash(inode);
1231 hlist_del_init(&inode->i_hash);
1232 } 1372 }
1233 list_del_init(&inode->i_list); 1373
1234 list_del_init(&inode->i_sb_list);
1235 WARN_ON(inode->i_state & I_NEW); 1374 WARN_ON(inode->i_state & I_NEW);
1236 inode->i_state |= I_FREEING; 1375 inode->i_state |= I_FREEING;
1237 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);
1238 spin_unlock(&inode_lock); 1385 spin_unlock(&inode_lock);
1239 evict(inode); 1386 evict(inode);
1240 spin_lock(&inode_lock); 1387 remove_inode_hash(inode);
1241 hlist_del_init(&inode->i_hash);
1242 spin_unlock(&inode_lock);
1243 wake_up_inode(inode); 1388 wake_up_inode(inode);
1244 BUG_ON(inode->i_state != (I_FREEING | I_CLEAR)); 1389 BUG_ON(inode->i_state != (I_FREEING | I_CLEAR));
1245 destroy_inode(inode); 1390 destroy_inode(inode);
@@ -1503,6 +1648,8 @@ void __init inode_init(void)
1503 SLAB_MEM_SPREAD), 1648 SLAB_MEM_SPREAD),
1504 init_once); 1649 init_once);
1505 register_shrinker(&icache_shrinker); 1650 register_shrinker(&icache_shrinker);
1651 percpu_counter_init(&nr_inodes, 0);
1652 percpu_counter_init(&nr_inodes_unused, 0);
1506 1653
1507 /* Hash may have been set up in inode_init_early */ 1654 /* Hash may have been set up in inode_init_early */
1508 if (!hashdist) 1655 if (!hashdist)