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