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