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