fbcon: Bugfix soft cursor detection in Tile Blitting
[pandora-kernel.git] / fs / inode.c
1 /*
2  * linux/fs/inode.c
3  *
4  * (C) 1997 Linus Torvalds
5  */
6
7 #include <linux/fs.h>
8 #include <linux/mm.h>
9 #include <linux/dcache.h>
10 #include <linux/init.h>
11 #include <linux/slab.h>
12 #include <linux/writeback.h>
13 #include <linux/module.h>
14 #include <linux/backing-dev.h>
15 #include <linux/wait.h>
16 #include <linux/rwsem.h>
17 #include <linux/hash.h>
18 #include <linux/swap.h>
19 #include <linux/security.h>
20 #include <linux/pagemap.h>
21 #include <linux/cdev.h>
22 #include <linux/bootmem.h>
23 #include <linux/fsnotify.h>
24 #include <linux/mount.h>
25 #include <linux/async.h>
26 #include <linux/posix_acl.h>
27 #include <linux/ima.h>
28
29 /*
30  * This is needed for the following functions:
31  *  - inode_has_buffers
32  *  - invalidate_bdev
33  *
34  * FIXME: remove all knowledge of the buffer layer from this file
35  */
36 #include <linux/buffer_head.h>
37
38 /*
39  * New inode.c implementation.
40  *
41  * This implementation has the basic premise of trying
42  * to be extremely low-overhead and SMP-safe, yet be
43  * simple enough to be "obviously correct".
44  *
45  * Famous last words.
46  */
47
48 /* inode dynamic allocation 1999, Andrea Arcangeli <andrea@suse.de> */
49
50 /* #define INODE_PARANOIA 1 */
51 /* #define INODE_DEBUG 1 */
52
53 /*
54  * Inode lookup is no longer as critical as it used to be:
55  * most of the lookups are going to be through the dcache.
56  */
57 #define I_HASHBITS      i_hash_shift
58 #define I_HASHMASK      i_hash_mask
59
60 static unsigned int i_hash_mask __read_mostly;
61 static unsigned int i_hash_shift __read_mostly;
62
63 /*
64  * Each inode can be on two separate lists. One is
65  * the hash list of the inode, used for lookups. The
66  * other linked list is the "type" list:
67  *  "in_use" - valid inode, i_count > 0, i_nlink > 0
68  *  "dirty"  - as "in_use" but also dirty
69  *  "unused" - valid inode, i_count = 0
70  *
71  * A "dirty" list is maintained for each super block,
72  * allowing for low-overhead inode sync() operations.
73  */
74
75 static LIST_HEAD(inode_lru);
76 static struct hlist_head *inode_hashtable __read_mostly;
77
78 /*
79  * A simple spinlock to protect the list manipulations.
80  *
81  * NOTE! You also have to own the lock if you change
82  * the i_state of an inode while it is in use..
83  */
84 DEFINE_SPINLOCK(inode_lock);
85
86 /*
87  * iprune_sem provides exclusion between the kswapd or try_to_free_pages
88  * icache shrinking path, and the umount path.  Without this exclusion,
89  * by the time prune_icache calls iput for the inode whose pages it has
90  * been invalidating, or by the time it calls clear_inode & destroy_inode
91  * from its final dispose_list, the struct super_block they refer to
92  * (for inode->i_sb->s_op) may already have been freed and reused.
93  *
94  * We make this an rwsem because the fastpath is icache shrinking. In
95  * some cases a filesystem may be doing a significant amount of work in
96  * its inode reclaim code, so this should improve parallelism.
97  */
98 static DECLARE_RWSEM(iprune_sem);
99
100 /*
101  * Statistics gathering..
102  */
103 struct inodes_stat_t inodes_stat;
104
105 static struct percpu_counter nr_inodes __cacheline_aligned_in_smp;
106 static struct percpu_counter nr_inodes_unused __cacheline_aligned_in_smp;
107
108 static struct kmem_cache *inode_cachep __read_mostly;
109
110 static inline int get_nr_inodes(void)
111 {
112         return percpu_counter_sum_positive(&nr_inodes);
113 }
114
115 static inline int get_nr_inodes_unused(void)
116 {
117         return percpu_counter_sum_positive(&nr_inodes_unused);
118 }
119
120 int 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
131 int 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
140 static void wake_up_inode(struct inode *inode)
141 {
142         /*
143          * Prevent speculative execution through spin_unlock(&inode_lock);
144          */
145         smp_mb();
146         wake_up_bit(&inode->i_state, __I_NEW);
147 }
148
149 /**
150  * inode_init_always - perform inode structure intialisation
151  * @sb: superblock inode belongs to
152  * @inode: inode to initialise
153  *
154  * These are initializations that need to be done on every inode
155  * allocation as the fields are not initialised by slab allocation.
156  */
157 int inode_init_always(struct super_block *sb, struct inode *inode)
158 {
159         static const struct address_space_operations empty_aops;
160         static const struct inode_operations empty_iops;
161         static const struct file_operations empty_fops;
162         struct address_space *const mapping = &inode->i_data;
163
164         inode->i_sb = sb;
165         inode->i_blkbits = sb->s_blocksize_bits;
166         inode->i_flags = 0;
167         atomic_set(&inode->i_count, 1);
168         inode->i_op = &empty_iops;
169         inode->i_fop = &empty_fops;
170         inode->i_nlink = 1;
171         inode->i_uid = 0;
172         inode->i_gid = 0;
173         atomic_set(&inode->i_writecount, 0);
174         inode->i_size = 0;
175         inode->i_blocks = 0;
176         inode->i_bytes = 0;
177         inode->i_generation = 0;
178 #ifdef CONFIG_QUOTA
179         memset(&inode->i_dquot, 0, sizeof(inode->i_dquot));
180 #endif
181         inode->i_pipe = NULL;
182         inode->i_bdev = NULL;
183         inode->i_cdev = NULL;
184         inode->i_rdev = 0;
185         inode->dirtied_when = 0;
186
187         if (security_inode_alloc(inode))
188                 goto out;
189         spin_lock_init(&inode->i_lock);
190         lockdep_set_class(&inode->i_lock, &sb->s_type->i_lock_key);
191
192         mutex_init(&inode->i_mutex);
193         lockdep_set_class(&inode->i_mutex, &sb->s_type->i_mutex_key);
194
195         init_rwsem(&inode->i_alloc_sem);
196         lockdep_set_class(&inode->i_alloc_sem, &sb->s_type->i_alloc_sem_key);
197
198         mapping->a_ops = &empty_aops;
199         mapping->host = inode;
200         mapping->flags = 0;
201         mapping_set_gfp_mask(mapping, GFP_HIGHUSER_MOVABLE);
202         mapping->assoc_mapping = NULL;
203         mapping->backing_dev_info = &default_backing_dev_info;
204         mapping->writeback_index = 0;
205
206         /*
207          * If the block_device provides a backing_dev_info for client
208          * inodes then use that.  Otherwise the inode share the bdev's
209          * backing_dev_info.
210          */
211         if (sb->s_bdev) {
212                 struct backing_dev_info *bdi;
213
214                 bdi = sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
215                 mapping->backing_dev_info = bdi;
216         }
217         inode->i_private = NULL;
218         inode->i_mapping = mapping;
219 #ifdef CONFIG_FS_POSIX_ACL
220         inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED;
221 #endif
222
223 #ifdef CONFIG_FSNOTIFY
224         inode->i_fsnotify_mask = 0;
225 #endif
226
227         percpu_counter_inc(&nr_inodes);
228
229         return 0;
230 out:
231         return -ENOMEM;
232 }
233 EXPORT_SYMBOL(inode_init_always);
234
235 static struct inode *alloc_inode(struct super_block *sb)
236 {
237         struct inode *inode;
238
239         if (sb->s_op->alloc_inode)
240                 inode = sb->s_op->alloc_inode(sb);
241         else
242                 inode = kmem_cache_alloc(inode_cachep, GFP_KERNEL);
243
244         if (!inode)
245                 return NULL;
246
247         if (unlikely(inode_init_always(sb, inode))) {
248                 if (inode->i_sb->s_op->destroy_inode)
249                         inode->i_sb->s_op->destroy_inode(inode);
250                 else
251                         kmem_cache_free(inode_cachep, inode);
252                 return NULL;
253         }
254
255         return inode;
256 }
257
258 void __destroy_inode(struct inode *inode)
259 {
260         BUG_ON(inode_has_buffers(inode));
261         security_inode_free(inode);
262         fsnotify_inode_delete(inode);
263 #ifdef CONFIG_FS_POSIX_ACL
264         if (inode->i_acl && inode->i_acl != ACL_NOT_CACHED)
265                 posix_acl_release(inode->i_acl);
266         if (inode->i_default_acl && inode->i_default_acl != ACL_NOT_CACHED)
267                 posix_acl_release(inode->i_default_acl);
268 #endif
269         percpu_counter_dec(&nr_inodes);
270 }
271 EXPORT_SYMBOL(__destroy_inode);
272
273 static void destroy_inode(struct inode *inode)
274 {
275         BUG_ON(!list_empty(&inode->i_lru));
276         __destroy_inode(inode);
277         if (inode->i_sb->s_op->destroy_inode)
278                 inode->i_sb->s_op->destroy_inode(inode);
279         else
280                 kmem_cache_free(inode_cachep, (inode));
281 }
282
283 void address_space_init_once(struct address_space *mapping)
284 {
285         memset(mapping, 0, sizeof(*mapping));
286         INIT_RADIX_TREE(&mapping->page_tree, GFP_ATOMIC);
287         spin_lock_init(&mapping->tree_lock);
288         spin_lock_init(&mapping->i_mmap_lock);
289         INIT_LIST_HEAD(&mapping->private_list);
290         spin_lock_init(&mapping->private_lock);
291         INIT_RAW_PRIO_TREE_ROOT(&mapping->i_mmap);
292         INIT_LIST_HEAD(&mapping->i_mmap_nonlinear);
293         mutex_init(&mapping->unmap_mutex);
294 }
295 EXPORT_SYMBOL(address_space_init_once);
296
297 /*
298  * These are initializations that only need to be done
299  * once, because the fields are idempotent across use
300  * of the inode, so let the slab aware of that.
301  */
302 void inode_init_once(struct inode *inode)
303 {
304         memset(inode, 0, sizeof(*inode));
305         INIT_HLIST_NODE(&inode->i_hash);
306         INIT_LIST_HEAD(&inode->i_dentry);
307         INIT_LIST_HEAD(&inode->i_devices);
308         INIT_LIST_HEAD(&inode->i_wb_list);
309         INIT_LIST_HEAD(&inode->i_lru);
310         address_space_init_once(&inode->i_data);
311         i_size_ordered_init(inode);
312 #ifdef CONFIG_FSNOTIFY
313         INIT_HLIST_HEAD(&inode->i_fsnotify_marks);
314 #endif
315 }
316 EXPORT_SYMBOL(inode_init_once);
317
318 static void init_once(void *foo)
319 {
320         struct inode *inode = (struct inode *) foo;
321
322         inode_init_once(inode);
323 }
324
325 /*
326  * inode_lock must be held
327  */
328 void __iget(struct inode *inode)
329 {
330         atomic_inc(&inode->i_count);
331 }
332
333 /*
334  * get additional reference to inode; caller must already hold one.
335  */
336 void ihold(struct inode *inode)
337 {
338         WARN_ON(atomic_inc_return(&inode->i_count) < 2);
339 }
340 EXPORT_SYMBOL(ihold);
341
342 static void inode_lru_list_add(struct inode *inode)
343 {
344         if (list_empty(&inode->i_lru)) {
345                 list_add(&inode->i_lru, &inode_lru);
346                 percpu_counter_inc(&nr_inodes_unused);
347         }
348 }
349
350 static void inode_lru_list_del(struct inode *inode)
351 {
352         if (!list_empty(&inode->i_lru)) {
353                 list_del_init(&inode->i_lru);
354                 percpu_counter_dec(&nr_inodes_unused);
355         }
356 }
357
358 static inline void __inode_sb_list_add(struct inode *inode)
359 {
360         list_add(&inode->i_sb_list, &inode->i_sb->s_inodes);
361 }
362
363 /**
364  * inode_sb_list_add - add inode to the superblock list of inodes
365  * @inode: inode to add
366  */
367 void inode_sb_list_add(struct inode *inode)
368 {
369         spin_lock(&inode_lock);
370         __inode_sb_list_add(inode);
371         spin_unlock(&inode_lock);
372 }
373 EXPORT_SYMBOL_GPL(inode_sb_list_add);
374
375 static inline void __inode_sb_list_del(struct inode *inode)
376 {
377         list_del_init(&inode->i_sb_list);
378 }
379
380 static unsigned long hash(struct super_block *sb, unsigned long hashval)
381 {
382         unsigned long tmp;
383
384         tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /
385                         L1_CACHE_BYTES;
386         tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> I_HASHBITS);
387         return tmp & I_HASHMASK;
388 }
389
390 /**
391  *      __insert_inode_hash - hash an inode
392  *      @inode: unhashed inode
393  *      @hashval: unsigned long value used to locate this object in the
394  *              inode_hashtable.
395  *
396  *      Add an inode to the inode hash for this superblock.
397  */
398 void __insert_inode_hash(struct inode *inode, unsigned long hashval)
399 {
400         struct hlist_head *b = inode_hashtable + hash(inode->i_sb, hashval);
401
402         spin_lock(&inode_lock);
403         hlist_add_head(&inode->i_hash, b);
404         spin_unlock(&inode_lock);
405 }
406 EXPORT_SYMBOL(__insert_inode_hash);
407
408 /**
409  *      __remove_inode_hash - remove an inode from the hash
410  *      @inode: inode to unhash
411  *
412  *      Remove an inode from the superblock.
413  */
414 static void __remove_inode_hash(struct inode *inode)
415 {
416         hlist_del_init(&inode->i_hash);
417 }
418
419 /**
420  *      remove_inode_hash - remove an inode from the hash
421  *      @inode: inode to unhash
422  *
423  *      Remove an inode from the superblock.
424  */
425 void remove_inode_hash(struct inode *inode)
426 {
427         spin_lock(&inode_lock);
428         hlist_del_init(&inode->i_hash);
429         spin_unlock(&inode_lock);
430 }
431 EXPORT_SYMBOL(remove_inode_hash);
432
433 void end_writeback(struct inode *inode)
434 {
435         might_sleep();
436         BUG_ON(inode->i_data.nrpages);
437         BUG_ON(!list_empty(&inode->i_data.private_list));
438         BUG_ON(!(inode->i_state & I_FREEING));
439         BUG_ON(inode->i_state & I_CLEAR);
440         inode_sync_wait(inode);
441         inode->i_state = I_FREEING | I_CLEAR;
442 }
443 EXPORT_SYMBOL(end_writeback);
444
445 static void evict(struct inode *inode)
446 {
447         const struct super_operations *op = inode->i_sb->s_op;
448
449         if (op->evict_inode) {
450                 op->evict_inode(inode);
451         } else {
452                 if (inode->i_data.nrpages)
453                         truncate_inode_pages(&inode->i_data, 0);
454                 end_writeback(inode);
455         }
456         if (S_ISBLK(inode->i_mode) && inode->i_bdev)
457                 bd_forget(inode);
458         if (S_ISCHR(inode->i_mode) && inode->i_cdev)
459                 cd_forget(inode);
460 }
461
462 /*
463  * dispose_list - dispose of the contents of a local list
464  * @head: the head of the list to free
465  *
466  * Dispose-list gets a local list with local inodes in it, so it doesn't
467  * need to worry about list corruption and SMP locks.
468  */
469 static void dispose_list(struct list_head *head)
470 {
471         while (!list_empty(head)) {
472                 struct inode *inode;
473
474                 inode = list_first_entry(head, struct inode, i_lru);
475                 list_del_init(&inode->i_lru);
476
477                 evict(inode);
478
479                 spin_lock(&inode_lock);
480                 __remove_inode_hash(inode);
481                 __inode_sb_list_del(inode);
482                 spin_unlock(&inode_lock);
483
484                 wake_up_inode(inode);
485                 destroy_inode(inode);
486         }
487 }
488
489 /**
490  * evict_inodes - evict all evictable inodes for a superblock
491  * @sb:         superblock to operate on
492  *
493  * Make sure that no inodes with zero refcount are retained.  This is
494  * called by superblock shutdown after having MS_ACTIVE flag removed,
495  * so any inode reaching zero refcount during or after that call will
496  * be immediately evicted.
497  */
498 void evict_inodes(struct super_block *sb)
499 {
500         struct inode *inode, *next;
501         LIST_HEAD(dispose);
502
503         down_write(&iprune_sem);
504
505         spin_lock(&inode_lock);
506         list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
507                 if (atomic_read(&inode->i_count))
508                         continue;
509
510                 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
511                         WARN_ON(1);
512                         continue;
513                 }
514
515                 inode->i_state |= I_FREEING;
516
517                 /*
518                  * Move the inode off the IO lists and LRU once I_FREEING is
519                  * set so that it won't get moved back on there if it is dirty.
520                  */
521                 list_move(&inode->i_lru, &dispose);
522                 list_del_init(&inode->i_wb_list);
523                 if (!(inode->i_state & (I_DIRTY | I_SYNC)))
524                         percpu_counter_dec(&nr_inodes_unused);
525         }
526         spin_unlock(&inode_lock);
527
528         dispose_list(&dispose);
529         up_write(&iprune_sem);
530 }
531
532 /**
533  * invalidate_inodes    - attempt to free all inodes on a superblock
534  * @sb:         superblock to operate on
535  * @kill_dirty: flag to guide handling of dirty inodes
536  *
537  * Attempts to free all inodes for a given superblock.  If there were any
538  * busy inodes return a non-zero value, else zero.
539  * If @kill_dirty is set, discard dirty inodes too, otherwise treat
540  * them as busy.
541  */
542 int invalidate_inodes(struct super_block *sb, bool kill_dirty)
543 {
544         int busy = 0;
545         struct inode *inode, *next;
546         LIST_HEAD(dispose);
547
548         down_write(&iprune_sem);
549
550         spin_lock(&inode_lock);
551         list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
552                 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE))
553                         continue;
554                 if (inode->i_state & I_DIRTY && !kill_dirty) {
555                         busy = 1;
556                         continue;
557                 }
558                 if (atomic_read(&inode->i_count)) {
559                         busy = 1;
560                         continue;
561                 }
562
563                 inode->i_state |= I_FREEING;
564
565                 /*
566                  * Move the inode off the IO lists and LRU once I_FREEING is
567                  * set so that it won't get moved back on there if it is dirty.
568                  */
569                 list_move(&inode->i_lru, &dispose);
570                 list_del_init(&inode->i_wb_list);
571                 if (!(inode->i_state & (I_DIRTY | I_SYNC)))
572                         percpu_counter_dec(&nr_inodes_unused);
573         }
574         spin_unlock(&inode_lock);
575
576         dispose_list(&dispose);
577         up_write(&iprune_sem);
578
579         return busy;
580 }
581
582 static int can_unuse(struct inode *inode)
583 {
584         if (inode->i_state & ~I_REFERENCED)
585                 return 0;
586         if (inode_has_buffers(inode))
587                 return 0;
588         if (atomic_read(&inode->i_count))
589                 return 0;
590         if (inode->i_data.nrpages)
591                 return 0;
592         return 1;
593 }
594
595 /*
596  * Scan `goal' inodes on the unused list for freeable ones. They are moved to a
597  * temporary list and then are freed outside inode_lock by dispose_list().
598  *
599  * Any inodes which are pinned purely because of attached pagecache have their
600  * pagecache removed.  If the inode has metadata buffers attached to
601  * mapping->private_list then try to remove them.
602  *
603  * If the inode has the I_REFERENCED flag set, then it means that it has been
604  * used recently - the flag is set in iput_final(). When we encounter such an
605  * inode, clear the flag and move it to the back of the LRU so it gets another
606  * pass through the LRU before it gets reclaimed. This is necessary because of
607  * the fact we are doing lazy LRU updates to minimise lock contention so the
608  * LRU does not have strict ordering. Hence we don't want to reclaim inodes
609  * with this flag set because they are the inodes that are out of order.
610  */
611 static void prune_icache(int nr_to_scan)
612 {
613         LIST_HEAD(freeable);
614         int nr_scanned;
615         unsigned long reap = 0;
616
617         down_read(&iprune_sem);
618         spin_lock(&inode_lock);
619         for (nr_scanned = 0; nr_scanned < nr_to_scan; nr_scanned++) {
620                 struct inode *inode;
621
622                 if (list_empty(&inode_lru))
623                         break;
624
625                 inode = list_entry(inode_lru.prev, struct inode, i_lru);
626
627                 /*
628                  * Referenced or dirty inodes are still in use. Give them
629                  * another pass through the LRU as we canot reclaim them now.
630                  */
631                 if (atomic_read(&inode->i_count) ||
632                     (inode->i_state & ~I_REFERENCED)) {
633                         list_del_init(&inode->i_lru);
634                         percpu_counter_dec(&nr_inodes_unused);
635                         continue;
636                 }
637
638                 /* recently referenced inodes get one more pass */
639                 if (inode->i_state & I_REFERENCED) {
640                         list_move(&inode->i_lru, &inode_lru);
641                         inode->i_state &= ~I_REFERENCED;
642                         continue;
643                 }
644                 if (inode_has_buffers(inode) || inode->i_data.nrpages) {
645                         __iget(inode);
646                         spin_unlock(&inode_lock);
647                         if (remove_inode_buffers(inode))
648                                 reap += invalidate_mapping_pages(&inode->i_data,
649                                                                 0, -1);
650                         iput(inode);
651                         spin_lock(&inode_lock);
652
653                         if (inode != list_entry(inode_lru.next,
654                                                 struct inode, i_lru))
655                                 continue;       /* wrong inode or list_empty */
656                         if (!can_unuse(inode))
657                                 continue;
658                 }
659                 WARN_ON(inode->i_state & I_NEW);
660                 inode->i_state |= I_FREEING;
661
662                 /*
663                  * Move the inode off the IO lists and LRU once I_FREEING is
664                  * set so that it won't get moved back on there if it is dirty.
665                  */
666                 list_move(&inode->i_lru, &freeable);
667                 list_del_init(&inode->i_wb_list);
668                 percpu_counter_dec(&nr_inodes_unused);
669         }
670         if (current_is_kswapd())
671                 __count_vm_events(KSWAPD_INODESTEAL, reap);
672         else
673                 __count_vm_events(PGINODESTEAL, reap);
674         spin_unlock(&inode_lock);
675
676         dispose_list(&freeable);
677         up_read(&iprune_sem);
678 }
679
680 /*
681  * shrink_icache_memory() will attempt to reclaim some unused inodes.  Here,
682  * "unused" means that no dentries are referring to the inodes: the files are
683  * not open and the dcache references to those inodes have already been
684  * reclaimed.
685  *
686  * This function is passed the number of inodes to scan, and it returns the
687  * total number of remaining possibly-reclaimable inodes.
688  */
689 static int shrink_icache_memory(struct shrinker *shrink, int nr, gfp_t gfp_mask)
690 {
691         if (nr) {
692                 /*
693                  * Nasty deadlock avoidance.  We may hold various FS locks,
694                  * and we don't want to recurse into the FS that called us
695                  * in clear_inode() and friends..
696                  */
697                 if (!(gfp_mask & __GFP_FS))
698                         return -1;
699                 prune_icache(nr);
700         }
701         return (get_nr_inodes_unused() / 100) * sysctl_vfs_cache_pressure;
702 }
703
704 static struct shrinker icache_shrinker = {
705         .shrink = shrink_icache_memory,
706         .seeks = DEFAULT_SEEKS,
707 };
708
709 static void __wait_on_freeing_inode(struct inode *inode);
710 /*
711  * Called with the inode lock held.
712  */
713 static struct inode *find_inode(struct super_block *sb,
714                                 struct hlist_head *head,
715                                 int (*test)(struct inode *, void *),
716                                 void *data)
717 {
718         struct hlist_node *node;
719         struct inode *inode = NULL;
720
721 repeat:
722         hlist_for_each_entry(inode, node, head, i_hash) {
723                 if (inode->i_sb != sb)
724                         continue;
725                 if (!test(inode, data))
726                         continue;
727                 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
728                         __wait_on_freeing_inode(inode);
729                         goto repeat;
730                 }
731                 __iget(inode);
732                 return inode;
733         }
734         return NULL;
735 }
736
737 /*
738  * find_inode_fast is the fast path version of find_inode, see the comment at
739  * iget_locked for details.
740  */
741 static struct inode *find_inode_fast(struct super_block *sb,
742                                 struct hlist_head *head, unsigned long ino)
743 {
744         struct hlist_node *node;
745         struct inode *inode = NULL;
746
747 repeat:
748         hlist_for_each_entry(inode, node, head, i_hash) {
749                 if (inode->i_ino != ino)
750                         continue;
751                 if (inode->i_sb != sb)
752                         continue;
753                 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
754                         __wait_on_freeing_inode(inode);
755                         goto repeat;
756                 }
757                 __iget(inode);
758                 return inode;
759         }
760         return NULL;
761 }
762
763 /*
764  * Each cpu owns a range of LAST_INO_BATCH numbers.
765  * 'shared_last_ino' is dirtied only once out of LAST_INO_BATCH allocations,
766  * to renew the exhausted range.
767  *
768  * This does not significantly increase overflow rate because every CPU can
769  * consume at most LAST_INO_BATCH-1 unused inode numbers. So there is
770  * NR_CPUS*(LAST_INO_BATCH-1) wastage. At 4096 and 1024, this is ~0.1% of the
771  * 2^32 range, and is a worst-case. Even a 50% wastage would only increase
772  * overflow rate by 2x, which does not seem too significant.
773  *
774  * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
775  * error if st_ino won't fit in target struct field. Use 32bit counter
776  * here to attempt to avoid that.
777  */
778 #define LAST_INO_BATCH 1024
779 static DEFINE_PER_CPU(unsigned int, last_ino);
780
781 unsigned int get_next_ino(void)
782 {
783         unsigned int *p = &get_cpu_var(last_ino);
784         unsigned int res = *p;
785
786 #ifdef CONFIG_SMP
787         if (unlikely((res & (LAST_INO_BATCH-1)) == 0)) {
788                 static atomic_t shared_last_ino;
789                 int next = atomic_add_return(LAST_INO_BATCH, &shared_last_ino);
790
791                 res = next - LAST_INO_BATCH;
792         }
793 #endif
794
795         *p = ++res;
796         put_cpu_var(last_ino);
797         return res;
798 }
799 EXPORT_SYMBOL(get_next_ino);
800
801 /**
802  *      new_inode       - obtain an inode
803  *      @sb: superblock
804  *
805  *      Allocates a new inode for given superblock. The default gfp_mask
806  *      for allocations related to inode->i_mapping is GFP_HIGHUSER_MOVABLE.
807  *      If HIGHMEM pages are unsuitable or it is known that pages allocated
808  *      for the page cache are not reclaimable or migratable,
809  *      mapping_set_gfp_mask() must be called with suitable flags on the
810  *      newly created inode's mapping
811  *
812  */
813 struct inode *new_inode(struct super_block *sb)
814 {
815         struct inode *inode;
816
817         spin_lock_prefetch(&inode_lock);
818
819         inode = alloc_inode(sb);
820         if (inode) {
821                 spin_lock(&inode_lock);
822                 __inode_sb_list_add(inode);
823                 inode->i_state = 0;
824                 spin_unlock(&inode_lock);
825         }
826         return inode;
827 }
828 EXPORT_SYMBOL(new_inode);
829
830 void unlock_new_inode(struct inode *inode)
831 {
832 #ifdef CONFIG_DEBUG_LOCK_ALLOC
833         if (S_ISDIR(inode->i_mode)) {
834                 struct file_system_type *type = inode->i_sb->s_type;
835
836                 /* Set new key only if filesystem hasn't already changed it */
837                 if (!lockdep_match_class(&inode->i_mutex,
838                     &type->i_mutex_key)) {
839                         /*
840                          * ensure nobody is actually holding i_mutex
841                          */
842                         mutex_destroy(&inode->i_mutex);
843                         mutex_init(&inode->i_mutex);
844                         lockdep_set_class(&inode->i_mutex,
845                                           &type->i_mutex_dir_key);
846                 }
847         }
848 #endif
849         /*
850          * This is special!  We do not need the spinlock when clearing I_NEW,
851          * because we're guaranteed that nobody else tries to do anything about
852          * the state of the inode when it is locked, as we just created it (so
853          * there can be no old holders that haven't tested I_NEW).
854          * However we must emit the memory barrier so that other CPUs reliably
855          * see the clearing of I_NEW after the other inode initialisation has
856          * completed.
857          */
858         smp_mb();
859         WARN_ON(!(inode->i_state & I_NEW));
860         inode->i_state &= ~I_NEW;
861         wake_up_inode(inode);
862 }
863 EXPORT_SYMBOL(unlock_new_inode);
864
865 /*
866  * This is called without the inode lock held.. Be careful.
867  *
868  * We no longer cache the sb_flags in i_flags - see fs.h
869  *      -- rmk@arm.uk.linux.org
870  */
871 static struct inode *get_new_inode(struct super_block *sb,
872                                 struct hlist_head *head,
873                                 int (*test)(struct inode *, void *),
874                                 int (*set)(struct inode *, void *),
875                                 void *data)
876 {
877         struct inode *inode;
878
879         inode = alloc_inode(sb);
880         if (inode) {
881                 struct inode *old;
882
883                 spin_lock(&inode_lock);
884                 /* We released the lock, so.. */
885                 old = find_inode(sb, head, test, data);
886                 if (!old) {
887                         if (set(inode, data))
888                                 goto set_failed;
889
890                         hlist_add_head(&inode->i_hash, head);
891                         __inode_sb_list_add(inode);
892                         inode->i_state = I_NEW;
893                         spin_unlock(&inode_lock);
894
895                         /* Return the locked inode with I_NEW set, the
896                          * caller is responsible for filling in the contents
897                          */
898                         return inode;
899                 }
900
901                 /*
902                  * Uhhuh, somebody else created the same inode under
903                  * us. Use the old inode instead of the one we just
904                  * allocated.
905                  */
906                 spin_unlock(&inode_lock);
907                 destroy_inode(inode);
908                 inode = old;
909                 wait_on_inode(inode);
910         }
911         return inode;
912
913 set_failed:
914         spin_unlock(&inode_lock);
915         destroy_inode(inode);
916         return NULL;
917 }
918
919 /*
920  * get_new_inode_fast is the fast path version of get_new_inode, see the
921  * comment at iget_locked for details.
922  */
923 static struct inode *get_new_inode_fast(struct super_block *sb,
924                                 struct hlist_head *head, unsigned long ino)
925 {
926         struct inode *inode;
927
928         inode = alloc_inode(sb);
929         if (inode) {
930                 struct inode *old;
931
932                 spin_lock(&inode_lock);
933                 /* We released the lock, so.. */
934                 old = find_inode_fast(sb, head, ino);
935                 if (!old) {
936                         inode->i_ino = ino;
937                         hlist_add_head(&inode->i_hash, head);
938                         __inode_sb_list_add(inode);
939                         inode->i_state = I_NEW;
940                         spin_unlock(&inode_lock);
941
942                         /* Return the locked inode with I_NEW set, the
943                          * caller is responsible for filling in the contents
944                          */
945                         return inode;
946                 }
947
948                 /*
949                  * Uhhuh, somebody else created the same inode under
950                  * us. Use the old inode instead of the one we just
951                  * allocated.
952                  */
953                 spin_unlock(&inode_lock);
954                 destroy_inode(inode);
955                 inode = old;
956                 wait_on_inode(inode);
957         }
958         return inode;
959 }
960
961 /*
962  * search the inode cache for a matching inode number.
963  * If we find one, then the inode number we are trying to
964  * allocate is not unique and so we should not use it.
965  *
966  * Returns 1 if the inode number is unique, 0 if it is not.
967  */
968 static int test_inode_iunique(struct super_block *sb, unsigned long ino)
969 {
970         struct hlist_head *b = inode_hashtable + hash(sb, ino);
971         struct hlist_node *node;
972         struct inode *inode;
973
974         hlist_for_each_entry(inode, node, b, i_hash) {
975                 if (inode->i_ino == ino && inode->i_sb == sb)
976                         return 0;
977         }
978
979         return 1;
980 }
981
982 /**
983  *      iunique - get a unique inode number
984  *      @sb: superblock
985  *      @max_reserved: highest reserved inode number
986  *
987  *      Obtain an inode number that is unique on the system for a given
988  *      superblock. This is used by file systems that have no natural
989  *      permanent inode numbering system. An inode number is returned that
990  *      is higher than the reserved limit but unique.
991  *
992  *      BUGS:
993  *      With a large number of inodes live on the file system this function
994  *      currently becomes quite slow.
995  */
996 ino_t iunique(struct super_block *sb, ino_t max_reserved)
997 {
998         /*
999          * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
1000          * error if st_ino won't fit in target struct field. Use 32bit counter
1001          * here to attempt to avoid that.
1002          */
1003         static DEFINE_SPINLOCK(iunique_lock);
1004         static unsigned int counter;
1005         ino_t res;
1006
1007         spin_lock(&inode_lock);
1008         spin_lock(&iunique_lock);
1009         do {
1010                 if (counter <= max_reserved)
1011                         counter = max_reserved + 1;
1012                 res = counter++;
1013         } while (!test_inode_iunique(sb, res));
1014         spin_unlock(&iunique_lock);
1015         spin_unlock(&inode_lock);
1016
1017         return res;
1018 }
1019 EXPORT_SYMBOL(iunique);
1020
1021 struct inode *igrab(struct inode *inode)
1022 {
1023         spin_lock(&inode_lock);
1024         if (!(inode->i_state & (I_FREEING|I_WILL_FREE)))
1025                 __iget(inode);
1026         else
1027                 /*
1028                  * Handle the case where s_op->clear_inode is not been
1029                  * called yet, and somebody is calling igrab
1030                  * while the inode is getting freed.
1031                  */
1032                 inode = NULL;
1033         spin_unlock(&inode_lock);
1034         return inode;
1035 }
1036 EXPORT_SYMBOL(igrab);
1037
1038 /**
1039  * ifind - internal function, you want ilookup5() or iget5().
1040  * @sb:         super block of file system to search
1041  * @head:       the head of the list to search
1042  * @test:       callback used for comparisons between inodes
1043  * @data:       opaque data pointer to pass to @test
1044  * @wait:       if true wait for the inode to be unlocked, if false do not
1045  *
1046  * ifind() searches for the inode specified by @data in the inode
1047  * cache. This is a generalized version of ifind_fast() for file systems where
1048  * the inode number is not sufficient for unique identification of an inode.
1049  *
1050  * If the inode is in the cache, the inode is returned with an incremented
1051  * reference count.
1052  *
1053  * Otherwise NULL is returned.
1054  *
1055  * Note, @test is called with the inode_lock held, so can't sleep.
1056  */
1057 static struct inode *ifind(struct super_block *sb,
1058                 struct hlist_head *head, int (*test)(struct inode *, void *),
1059                 void *data, const int wait)
1060 {
1061         struct inode *inode;
1062
1063         spin_lock(&inode_lock);
1064         inode = find_inode(sb, head, test, data);
1065         if (inode) {
1066                 spin_unlock(&inode_lock);
1067                 if (likely(wait))
1068                         wait_on_inode(inode);
1069                 return inode;
1070         }
1071         spin_unlock(&inode_lock);
1072         return NULL;
1073 }
1074
1075 /**
1076  * ifind_fast - internal function, you want ilookup() or iget().
1077  * @sb:         super block of file system to search
1078  * @head:       head of the list to search
1079  * @ino:        inode number to search for
1080  *
1081  * ifind_fast() searches for the inode @ino in the inode cache. This is for
1082  * file systems where the inode number is sufficient for unique identification
1083  * of an inode.
1084  *
1085  * If the inode is in the cache, the inode is returned with an incremented
1086  * reference count.
1087  *
1088  * Otherwise NULL is returned.
1089  */
1090 static struct inode *ifind_fast(struct super_block *sb,
1091                 struct hlist_head *head, unsigned long ino)
1092 {
1093         struct inode *inode;
1094
1095         spin_lock(&inode_lock);
1096         inode = find_inode_fast(sb, head, ino);
1097         if (inode) {
1098                 spin_unlock(&inode_lock);
1099                 wait_on_inode(inode);
1100                 return inode;
1101         }
1102         spin_unlock(&inode_lock);
1103         return NULL;
1104 }
1105
1106 /**
1107  * ilookup5_nowait - search for an inode in the inode cache
1108  * @sb:         super block of file system to search
1109  * @hashval:    hash value (usually inode number) to search for
1110  * @test:       callback used for comparisons between inodes
1111  * @data:       opaque data pointer to pass to @test
1112  *
1113  * ilookup5() uses ifind() to search for the inode specified by @hashval and
1114  * @data in the inode cache. This is a generalized version of ilookup() for
1115  * file systems where the inode number is not sufficient for unique
1116  * identification of an inode.
1117  *
1118  * If the inode is in the cache, the inode is returned with an incremented
1119  * reference count.  Note, the inode lock is not waited upon so you have to be
1120  * very careful what you do with the returned inode.  You probably should be
1121  * using ilookup5() instead.
1122  *
1123  * Otherwise NULL is returned.
1124  *
1125  * Note, @test is called with the inode_lock held, so can't sleep.
1126  */
1127 struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval,
1128                 int (*test)(struct inode *, void *), void *data)
1129 {
1130         struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1131
1132         return ifind(sb, head, test, data, 0);
1133 }
1134 EXPORT_SYMBOL(ilookup5_nowait);
1135
1136 /**
1137  * ilookup5 - search for an inode in the inode cache
1138  * @sb:         super block of file system to search
1139  * @hashval:    hash value (usually inode number) to search for
1140  * @test:       callback used for comparisons between inodes
1141  * @data:       opaque data pointer to pass to @test
1142  *
1143  * ilookup5() uses ifind() to search for the inode specified by @hashval and
1144  * @data in the inode cache. This is a generalized version of ilookup() for
1145  * file systems where the inode number is not sufficient for unique
1146  * identification of an inode.
1147  *
1148  * If the inode is in the cache, the inode lock is waited upon and the inode is
1149  * returned with an incremented reference count.
1150  *
1151  * Otherwise NULL is returned.
1152  *
1153  * Note, @test is called with the inode_lock held, so can't sleep.
1154  */
1155 struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
1156                 int (*test)(struct inode *, void *), void *data)
1157 {
1158         struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1159
1160         return ifind(sb, head, test, data, 1);
1161 }
1162 EXPORT_SYMBOL(ilookup5);
1163
1164 /**
1165  * ilookup - search for an inode in the inode cache
1166  * @sb:         super block of file system to search
1167  * @ino:        inode number to search for
1168  *
1169  * ilookup() uses ifind_fast() to search for the inode @ino in the inode cache.
1170  * This is for file systems where the inode number is sufficient for unique
1171  * identification of an inode.
1172  *
1173  * If the inode is in the cache, the inode is returned with an incremented
1174  * reference count.
1175  *
1176  * Otherwise NULL is returned.
1177  */
1178 struct inode *ilookup(struct super_block *sb, unsigned long ino)
1179 {
1180         struct hlist_head *head = inode_hashtable + hash(sb, ino);
1181
1182         return ifind_fast(sb, head, ino);
1183 }
1184 EXPORT_SYMBOL(ilookup);
1185
1186 /**
1187  * iget5_locked - obtain an inode from a mounted file system
1188  * @sb:         super block of file system
1189  * @hashval:    hash value (usually inode number) to get
1190  * @test:       callback used for comparisons between inodes
1191  * @set:        callback used to initialize a new struct inode
1192  * @data:       opaque data pointer to pass to @test and @set
1193  *
1194  * iget5_locked() uses ifind() to search for the inode specified by @hashval
1195  * and @data in the inode cache and if present it is returned with an increased
1196  * reference count. This is a generalized version of iget_locked() for file
1197  * systems where the inode number is not sufficient for unique identification
1198  * of an inode.
1199  *
1200  * If the inode is not in cache, get_new_inode() is called to allocate a new
1201  * inode and this is returned locked, hashed, and with the I_NEW flag set. The
1202  * file system gets to fill it in before unlocking it via unlock_new_inode().
1203  *
1204  * Note both @test and @set are called with the inode_lock held, so can't sleep.
1205  */
1206 struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
1207                 int (*test)(struct inode *, void *),
1208                 int (*set)(struct inode *, void *), void *data)
1209 {
1210         struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1211         struct inode *inode;
1212
1213         inode = ifind(sb, head, test, data, 1);
1214         if (inode)
1215                 return inode;
1216         /*
1217          * get_new_inode() will do the right thing, re-trying the search
1218          * in case it had to block at any point.
1219          */
1220         return get_new_inode(sb, head, test, set, data);
1221 }
1222 EXPORT_SYMBOL(iget5_locked);
1223
1224 /**
1225  * iget_locked - obtain an inode from a mounted file system
1226  * @sb:         super block of file system
1227  * @ino:        inode number to get
1228  *
1229  * iget_locked() uses ifind_fast() to search for the inode specified by @ino in
1230  * the inode cache and if present it is returned with an increased reference
1231  * count. This is for file systems where the inode number is sufficient for
1232  * unique identification of an inode.
1233  *
1234  * If the inode is not in cache, get_new_inode_fast() is called to allocate a
1235  * new inode and this is returned locked, hashed, and with the I_NEW flag set.
1236  * The file system gets to fill it in before unlocking it via
1237  * unlock_new_inode().
1238  */
1239 struct inode *iget_locked(struct super_block *sb, unsigned long ino)
1240 {
1241         struct hlist_head *head = inode_hashtable + hash(sb, ino);
1242         struct inode *inode;
1243
1244         inode = ifind_fast(sb, head, ino);
1245         if (inode)
1246                 return inode;
1247         /*
1248          * get_new_inode_fast() will do the right thing, re-trying the search
1249          * in case it had to block at any point.
1250          */
1251         return get_new_inode_fast(sb, head, ino);
1252 }
1253 EXPORT_SYMBOL(iget_locked);
1254
1255 int insert_inode_locked(struct inode *inode)
1256 {
1257         struct super_block *sb = inode->i_sb;
1258         ino_t ino = inode->i_ino;
1259         struct hlist_head *head = inode_hashtable + hash(sb, ino);
1260
1261         inode->i_state |= I_NEW;
1262         while (1) {
1263                 struct hlist_node *node;
1264                 struct inode *old = NULL;
1265                 spin_lock(&inode_lock);
1266                 hlist_for_each_entry(old, node, head, i_hash) {
1267                         if (old->i_ino != ino)
1268                                 continue;
1269                         if (old->i_sb != sb)
1270                                 continue;
1271                         if (old->i_state & (I_FREEING|I_WILL_FREE))
1272                                 continue;
1273                         break;
1274                 }
1275                 if (likely(!node)) {
1276                         hlist_add_head(&inode->i_hash, head);
1277                         spin_unlock(&inode_lock);
1278                         return 0;
1279                 }
1280                 __iget(old);
1281                 spin_unlock(&inode_lock);
1282                 wait_on_inode(old);
1283                 if (unlikely(!inode_unhashed(old))) {
1284                         iput(old);
1285                         return -EBUSY;
1286                 }
1287                 iput(old);
1288         }
1289 }
1290 EXPORT_SYMBOL(insert_inode_locked);
1291
1292 int insert_inode_locked4(struct inode *inode, unsigned long hashval,
1293                 int (*test)(struct inode *, void *), void *data)
1294 {
1295         struct super_block *sb = inode->i_sb;
1296         struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1297
1298         inode->i_state |= I_NEW;
1299
1300         while (1) {
1301                 struct hlist_node *node;
1302                 struct inode *old = NULL;
1303
1304                 spin_lock(&inode_lock);
1305                 hlist_for_each_entry(old, node, head, i_hash) {
1306                         if (old->i_sb != sb)
1307                                 continue;
1308                         if (!test(old, data))
1309                                 continue;
1310                         if (old->i_state & (I_FREEING|I_WILL_FREE))
1311                                 continue;
1312                         break;
1313                 }
1314                 if (likely(!node)) {
1315                         hlist_add_head(&inode->i_hash, head);
1316                         spin_unlock(&inode_lock);
1317                         return 0;
1318                 }
1319                 __iget(old);
1320                 spin_unlock(&inode_lock);
1321                 wait_on_inode(old);
1322                 if (unlikely(!inode_unhashed(old))) {
1323                         iput(old);
1324                         return -EBUSY;
1325                 }
1326                 iput(old);
1327         }
1328 }
1329 EXPORT_SYMBOL(insert_inode_locked4);
1330
1331
1332 int generic_delete_inode(struct inode *inode)
1333 {
1334         return 1;
1335 }
1336 EXPORT_SYMBOL(generic_delete_inode);
1337
1338 /*
1339  * Normal UNIX filesystem behaviour: delete the
1340  * inode when the usage count drops to zero, and
1341  * i_nlink is zero.
1342  */
1343 int generic_drop_inode(struct inode *inode)
1344 {
1345         return !inode->i_nlink || inode_unhashed(inode);
1346 }
1347 EXPORT_SYMBOL_GPL(generic_drop_inode);
1348
1349 /*
1350  * Called when we're dropping the last reference
1351  * to an inode.
1352  *
1353  * Call the FS "drop_inode()" function, defaulting to
1354  * the legacy UNIX filesystem behaviour.  If it tells
1355  * us to evict inode, do so.  Otherwise, retain inode
1356  * in cache if fs is alive, sync and evict if fs is
1357  * shutting down.
1358  */
1359 static void iput_final(struct inode *inode)
1360 {
1361         struct super_block *sb = inode->i_sb;
1362         const struct super_operations *op = inode->i_sb->s_op;
1363         int drop;
1364
1365         if (op && op->drop_inode)
1366                 drop = op->drop_inode(inode);
1367         else
1368                 drop = generic_drop_inode(inode);
1369
1370         if (!drop) {
1371                 if (sb->s_flags & MS_ACTIVE) {
1372                         inode->i_state |= I_REFERENCED;
1373                         if (!(inode->i_state & (I_DIRTY|I_SYNC))) {
1374                                 inode_lru_list_add(inode);
1375                         }
1376                         spin_unlock(&inode_lock);
1377                         return;
1378                 }
1379                 WARN_ON(inode->i_state & I_NEW);
1380                 inode->i_state |= I_WILL_FREE;
1381                 spin_unlock(&inode_lock);
1382                 write_inode_now(inode, 1);
1383                 spin_lock(&inode_lock);
1384                 WARN_ON(inode->i_state & I_NEW);
1385                 inode->i_state &= ~I_WILL_FREE;
1386                 __remove_inode_hash(inode);
1387         }
1388
1389         WARN_ON(inode->i_state & I_NEW);
1390         inode->i_state |= I_FREEING;
1391
1392         /*
1393          * Move the inode off the IO lists and LRU once I_FREEING is
1394          * set so that it won't get moved back on there if it is dirty.
1395          */
1396         inode_lru_list_del(inode);
1397         list_del_init(&inode->i_wb_list);
1398
1399         __inode_sb_list_del(inode);
1400         spin_unlock(&inode_lock);
1401         evict(inode);
1402         remove_inode_hash(inode);
1403         wake_up_inode(inode);
1404         BUG_ON(inode->i_state != (I_FREEING | I_CLEAR));
1405         destroy_inode(inode);
1406 }
1407
1408 /**
1409  *      iput    - put an inode
1410  *      @inode: inode to put
1411  *
1412  *      Puts an inode, dropping its usage count. If the inode use count hits
1413  *      zero, the inode is then freed and may also be destroyed.
1414  *
1415  *      Consequently, iput() can sleep.
1416  */
1417 void iput(struct inode *inode)
1418 {
1419         if (inode) {
1420                 BUG_ON(inode->i_state & I_CLEAR);
1421
1422                 if (atomic_dec_and_lock(&inode->i_count, &inode_lock))
1423                         iput_final(inode);
1424         }
1425 }
1426 EXPORT_SYMBOL(iput);
1427
1428 /**
1429  *      bmap    - find a block number in a file
1430  *      @inode: inode of file
1431  *      @block: block to find
1432  *
1433  *      Returns the block number on the device holding the inode that
1434  *      is the disk block number for the block of the file requested.
1435  *      That is, asked for block 4 of inode 1 the function will return the
1436  *      disk block relative to the disk start that holds that block of the
1437  *      file.
1438  */
1439 sector_t bmap(struct inode *inode, sector_t block)
1440 {
1441         sector_t res = 0;
1442         if (inode->i_mapping->a_ops->bmap)
1443                 res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block);
1444         return res;
1445 }
1446 EXPORT_SYMBOL(bmap);
1447
1448 /*
1449  * With relative atime, only update atime if the previous atime is
1450  * earlier than either the ctime or mtime or if at least a day has
1451  * passed since the last atime update.
1452  */
1453 static int relatime_need_update(struct vfsmount *mnt, struct inode *inode,
1454                              struct timespec now)
1455 {
1456
1457         if (!(mnt->mnt_flags & MNT_RELATIME))
1458                 return 1;
1459         /*
1460          * Is mtime younger than atime? If yes, update atime:
1461          */
1462         if (timespec_compare(&inode->i_mtime, &inode->i_atime) >= 0)
1463                 return 1;
1464         /*
1465          * Is ctime younger than atime? If yes, update atime:
1466          */
1467         if (timespec_compare(&inode->i_ctime, &inode->i_atime) >= 0)
1468                 return 1;
1469
1470         /*
1471          * Is the previous atime value older than a day? If yes,
1472          * update atime:
1473          */
1474         if ((long)(now.tv_sec - inode->i_atime.tv_sec) >= 24*60*60)
1475                 return 1;
1476         /*
1477          * Good, we can skip the atime update:
1478          */
1479         return 0;
1480 }
1481
1482 /**
1483  *      touch_atime     -       update the access time
1484  *      @mnt: mount the inode is accessed on
1485  *      @dentry: dentry accessed
1486  *
1487  *      Update the accessed time on an inode and mark it for writeback.
1488  *      This function automatically handles read only file systems and media,
1489  *      as well as the "noatime" flag and inode specific "noatime" markers.
1490  */
1491 void touch_atime(struct vfsmount *mnt, struct dentry *dentry)
1492 {
1493         struct inode *inode = dentry->d_inode;
1494         struct timespec now;
1495
1496         if (inode->i_flags & S_NOATIME)
1497                 return;
1498         if (IS_NOATIME(inode))
1499                 return;
1500         if ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode))
1501                 return;
1502
1503         if (mnt->mnt_flags & MNT_NOATIME)
1504                 return;
1505         if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
1506                 return;
1507
1508         now = current_fs_time(inode->i_sb);
1509
1510         if (!relatime_need_update(mnt, inode, now))
1511                 return;
1512
1513         if (timespec_equal(&inode->i_atime, &now))
1514                 return;
1515
1516         if (mnt_want_write(mnt))
1517                 return;
1518
1519         inode->i_atime = now;
1520         mark_inode_dirty_sync(inode);
1521         mnt_drop_write(mnt);
1522 }
1523 EXPORT_SYMBOL(touch_atime);
1524
1525 /**
1526  *      file_update_time        -       update mtime and ctime time
1527  *      @file: file accessed
1528  *
1529  *      Update the mtime and ctime members of an inode and mark the inode
1530  *      for writeback.  Note that this function is meant exclusively for
1531  *      usage in the file write path of filesystems, and filesystems may
1532  *      choose to explicitly ignore update via this function with the
1533  *      S_NOCMTIME inode flag, e.g. for network filesystem where these
1534  *      timestamps are handled by the server.
1535  */
1536
1537 void file_update_time(struct file *file)
1538 {
1539         struct inode *inode = file->f_path.dentry->d_inode;
1540         struct timespec now;
1541         enum { S_MTIME = 1, S_CTIME = 2, S_VERSION = 4 } sync_it = 0;
1542
1543         /* First try to exhaust all avenues to not sync */
1544         if (IS_NOCMTIME(inode))
1545                 return;
1546
1547         now = current_fs_time(inode->i_sb);
1548         if (!timespec_equal(&inode->i_mtime, &now))
1549                 sync_it = S_MTIME;
1550
1551         if (!timespec_equal(&inode->i_ctime, &now))
1552                 sync_it |= S_CTIME;
1553
1554         if (IS_I_VERSION(inode))
1555                 sync_it |= S_VERSION;
1556
1557         if (!sync_it)
1558                 return;
1559
1560         /* Finally allowed to write? Takes lock. */
1561         if (mnt_want_write_file(file))
1562                 return;
1563
1564         /* Only change inode inside the lock region */
1565         if (sync_it & S_VERSION)
1566                 inode_inc_iversion(inode);
1567         if (sync_it & S_CTIME)
1568                 inode->i_ctime = now;
1569         if (sync_it & S_MTIME)
1570                 inode->i_mtime = now;
1571         mark_inode_dirty_sync(inode);
1572         mnt_drop_write(file->f_path.mnt);
1573 }
1574 EXPORT_SYMBOL(file_update_time);
1575
1576 int inode_needs_sync(struct inode *inode)
1577 {
1578         if (IS_SYNC(inode))
1579                 return 1;
1580         if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
1581                 return 1;
1582         return 0;
1583 }
1584 EXPORT_SYMBOL(inode_needs_sync);
1585
1586 int inode_wait(void *word)
1587 {
1588         schedule();
1589         return 0;
1590 }
1591 EXPORT_SYMBOL(inode_wait);
1592
1593 /*
1594  * If we try to find an inode in the inode hash while it is being
1595  * deleted, we have to wait until the filesystem completes its
1596  * deletion before reporting that it isn't found.  This function waits
1597  * until the deletion _might_ have completed.  Callers are responsible
1598  * to recheck inode state.
1599  *
1600  * It doesn't matter if I_NEW is not set initially, a call to
1601  * wake_up_inode() after removing from the hash list will DTRT.
1602  *
1603  * This is called with inode_lock held.
1604  */
1605 static void __wait_on_freeing_inode(struct inode *inode)
1606 {
1607         wait_queue_head_t *wq;
1608         DEFINE_WAIT_BIT(wait, &inode->i_state, __I_NEW);
1609         wq = bit_waitqueue(&inode->i_state, __I_NEW);
1610         prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
1611         spin_unlock(&inode_lock);
1612         schedule();
1613         finish_wait(wq, &wait.wait);
1614         spin_lock(&inode_lock);
1615 }
1616
1617 static __initdata unsigned long ihash_entries;
1618 static int __init set_ihash_entries(char *str)
1619 {
1620         if (!str)
1621                 return 0;
1622         ihash_entries = simple_strtoul(str, &str, 0);
1623         return 1;
1624 }
1625 __setup("ihash_entries=", set_ihash_entries);
1626
1627 /*
1628  * Initialize the waitqueues and inode hash table.
1629  */
1630 void __init inode_init_early(void)
1631 {
1632         int loop;
1633
1634         /* If hashes are distributed across NUMA nodes, defer
1635          * hash allocation until vmalloc space is available.
1636          */
1637         if (hashdist)
1638                 return;
1639
1640         inode_hashtable =
1641                 alloc_large_system_hash("Inode-cache",
1642                                         sizeof(struct hlist_head),
1643                                         ihash_entries,
1644                                         14,
1645                                         HASH_EARLY,
1646                                         &i_hash_shift,
1647                                         &i_hash_mask,
1648                                         0);
1649
1650         for (loop = 0; loop < (1 << i_hash_shift); loop++)
1651                 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1652 }
1653
1654 void __init inode_init(void)
1655 {
1656         int loop;
1657
1658         /* inode slab cache */
1659         inode_cachep = kmem_cache_create("inode_cache",
1660                                          sizeof(struct inode),
1661                                          0,
1662                                          (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
1663                                          SLAB_MEM_SPREAD),
1664                                          init_once);
1665         register_shrinker(&icache_shrinker);
1666         percpu_counter_init(&nr_inodes, 0);
1667         percpu_counter_init(&nr_inodes_unused, 0);
1668
1669         /* Hash may have been set up in inode_init_early */
1670         if (!hashdist)
1671                 return;
1672
1673         inode_hashtable =
1674                 alloc_large_system_hash("Inode-cache",
1675                                         sizeof(struct hlist_head),
1676                                         ihash_entries,
1677                                         14,
1678                                         0,
1679                                         &i_hash_shift,
1680                                         &i_hash_mask,
1681                                         0);
1682
1683         for (loop = 0; loop < (1 << i_hash_shift); loop++)
1684                 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1685 }
1686
1687 void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
1688 {
1689         inode->i_mode = mode;
1690         if (S_ISCHR(mode)) {
1691                 inode->i_fop = &def_chr_fops;
1692                 inode->i_rdev = rdev;
1693         } else if (S_ISBLK(mode)) {
1694                 inode->i_fop = &def_blk_fops;
1695                 inode->i_rdev = rdev;
1696         } else if (S_ISFIFO(mode))
1697                 inode->i_fop = &def_fifo_fops;
1698         else if (S_ISSOCK(mode))
1699                 inode->i_fop = &bad_sock_fops;
1700         else
1701                 printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o) for"
1702                                   " inode %s:%lu\n", mode, inode->i_sb->s_id,
1703                                   inode->i_ino);
1704 }
1705 EXPORT_SYMBOL(init_special_inode);
1706
1707 /**
1708  * Init uid,gid,mode for new inode according to posix standards
1709  * @inode: New inode
1710  * @dir: Directory inode
1711  * @mode: mode of the new inode
1712  */
1713 void inode_init_owner(struct inode *inode, const struct inode *dir,
1714                         mode_t mode)
1715 {
1716         inode->i_uid = current_fsuid();
1717         if (dir && dir->i_mode & S_ISGID) {
1718                 inode->i_gid = dir->i_gid;
1719                 if (S_ISDIR(mode))
1720                         mode |= S_ISGID;
1721         } else
1722                 inode->i_gid = current_fsgid();
1723         inode->i_mode = mode;
1724 }
1725 EXPORT_SYMBOL(inode_init_owner);