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