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