]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/inode.c
fs: simplify handling of zero sized reads in __blockdev_direct_IO
[mirror_ubuntu-artful-kernel.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
a66979ab
DC
40 * inode_wb_list_lock protects:
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
DC
50 *
51 * inode_wb_list_lock
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 67__cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_sb_list_lock);
a66979ab 68__cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_wb_list_lock);
55fa6091 69
7dcda1c9
JA
70/*
71 * Empty aops. Can be used for the cases where the user does not
72 * define any of the address_space operations.
73 */
74const struct address_space_operations empty_aops = {
75};
76EXPORT_SYMBOL(empty_aops);
77
1da177e4
LT
78/*
79 * Statistics gathering..
80 */
81struct inodes_stat_t inodes_stat;
82
3e880fb5 83static DEFINE_PER_CPU(unsigned int, nr_inodes);
fcb94f72 84static DEFINE_PER_CPU(unsigned int, nr_unused);
cffbc8aa 85
6b3304b5 86static struct kmem_cache *inode_cachep __read_mostly;
1da177e4 87
3e880fb5 88static int get_nr_inodes(void)
cffbc8aa 89{
3e880fb5
NP
90 int i;
91 int sum = 0;
92 for_each_possible_cpu(i)
93 sum += per_cpu(nr_inodes, i);
94 return sum < 0 ? 0 : sum;
cffbc8aa
DC
95}
96
97static inline int get_nr_inodes_unused(void)
98{
fcb94f72
DC
99 int i;
100 int sum = 0;
101 for_each_possible_cpu(i)
102 sum += per_cpu(nr_unused, i);
103 return sum < 0 ? 0 : sum;
cffbc8aa
DC
104}
105
106int get_nr_dirty_inodes(void)
107{
3e880fb5 108 /* not actually dirty inodes, but a wild approximation */
cffbc8aa
DC
109 int nr_dirty = get_nr_inodes() - get_nr_inodes_unused();
110 return nr_dirty > 0 ? nr_dirty : 0;
cffbc8aa
DC
111}
112
113/*
114 * Handle nr_inode sysctl
115 */
116#ifdef CONFIG_SYSCTL
117int proc_nr_inodes(ctl_table *table, int write,
118 void __user *buffer, size_t *lenp, loff_t *ppos)
119{
120 inodes_stat.nr_inodes = get_nr_inodes();
fcb94f72 121 inodes_stat.nr_unused = get_nr_inodes_unused();
cffbc8aa
DC
122 return proc_dointvec(table, write, buffer, lenp, ppos);
123}
124#endif
125
2cb1599f
DC
126/**
127 * inode_init_always - perform inode structure intialisation
0bc02f3f
RD
128 * @sb: superblock inode belongs to
129 * @inode: inode to initialise
2cb1599f
DC
130 *
131 * These are initializations that need to be done on every inode
132 * allocation as the fields are not initialised by slab allocation.
133 */
54e34621 134int inode_init_always(struct super_block *sb, struct inode *inode)
1da177e4 135{
6e1d5dcc 136 static const struct inode_operations empty_iops;
99ac48f5 137 static const struct file_operations empty_fops;
6b3304b5 138 struct address_space *const mapping = &inode->i_data;
2cb1599f
DC
139
140 inode->i_sb = sb;
141 inode->i_blkbits = sb->s_blocksize_bits;
142 inode->i_flags = 0;
143 atomic_set(&inode->i_count, 1);
144 inode->i_op = &empty_iops;
145 inode->i_fop = &empty_fops;
146 inode->i_nlink = 1;
56ff5efa
AV
147 inode->i_uid = 0;
148 inode->i_gid = 0;
2cb1599f
DC
149 atomic_set(&inode->i_writecount, 0);
150 inode->i_size = 0;
151 inode->i_blocks = 0;
152 inode->i_bytes = 0;
153 inode->i_generation = 0;
1da177e4 154#ifdef CONFIG_QUOTA
2cb1599f 155 memset(&inode->i_dquot, 0, sizeof(inode->i_dquot));
1da177e4 156#endif
2cb1599f
DC
157 inode->i_pipe = NULL;
158 inode->i_bdev = NULL;
159 inode->i_cdev = NULL;
160 inode->i_rdev = 0;
161 inode->dirtied_when = 0;
6146f0d5
MZ
162
163 if (security_inode_alloc(inode))
54e34621 164 goto out;
2cb1599f
DC
165 spin_lock_init(&inode->i_lock);
166 lockdep_set_class(&inode->i_lock, &sb->s_type->i_lock_key);
167
168 mutex_init(&inode->i_mutex);
169 lockdep_set_class(&inode->i_mutex, &sb->s_type->i_mutex_key);
170
171 init_rwsem(&inode->i_alloc_sem);
172 lockdep_set_class(&inode->i_alloc_sem, &sb->s_type->i_alloc_sem_key);
173
174 mapping->a_ops = &empty_aops;
175 mapping->host = inode;
176 mapping->flags = 0;
3c1d4378 177 mapping_set_gfp_mask(mapping, GFP_HIGHUSER_MOVABLE);
2cb1599f
DC
178 mapping->assoc_mapping = NULL;
179 mapping->backing_dev_info = &default_backing_dev_info;
180 mapping->writeback_index = 0;
181
182 /*
183 * If the block_device provides a backing_dev_info for client
184 * inodes then use that. Otherwise the inode share the bdev's
185 * backing_dev_info.
186 */
187 if (sb->s_bdev) {
188 struct backing_dev_info *bdi;
189
2c96ce9f 190 bdi = sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
2cb1599f
DC
191 mapping->backing_dev_info = bdi;
192 }
193 inode->i_private = NULL;
194 inode->i_mapping = mapping;
f19d4a8f
AV
195#ifdef CONFIG_FS_POSIX_ACL
196 inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED;
197#endif
2cb1599f 198
3be25f49
EP
199#ifdef CONFIG_FSNOTIFY
200 inode->i_fsnotify_mask = 0;
201#endif
202
3e880fb5 203 this_cpu_inc(nr_inodes);
cffbc8aa 204
54e34621 205 return 0;
54e34621
CH
206out:
207 return -ENOMEM;
1da177e4 208}
2cb1599f
DC
209EXPORT_SYMBOL(inode_init_always);
210
211static struct inode *alloc_inode(struct super_block *sb)
212{
213 struct inode *inode;
214
215 if (sb->s_op->alloc_inode)
216 inode = sb->s_op->alloc_inode(sb);
217 else
218 inode = kmem_cache_alloc(inode_cachep, GFP_KERNEL);
219
54e34621
CH
220 if (!inode)
221 return NULL;
222
223 if (unlikely(inode_init_always(sb, inode))) {
224 if (inode->i_sb->s_op->destroy_inode)
225 inode->i_sb->s_op->destroy_inode(inode);
226 else
227 kmem_cache_free(inode_cachep, inode);
228 return NULL;
229 }
230
231 return inode;
2cb1599f 232}
1da177e4 233
ff0c7d15
NP
234void free_inode_nonrcu(struct inode *inode)
235{
236 kmem_cache_free(inode_cachep, inode);
237}
238EXPORT_SYMBOL(free_inode_nonrcu);
239
2e00c97e 240void __destroy_inode(struct inode *inode)
1da177e4 241{
b7542f8c 242 BUG_ON(inode_has_buffers(inode));
1da177e4 243 security_inode_free(inode);
3be25f49 244 fsnotify_inode_delete(inode);
f19d4a8f
AV
245#ifdef CONFIG_FS_POSIX_ACL
246 if (inode->i_acl && inode->i_acl != ACL_NOT_CACHED)
247 posix_acl_release(inode->i_acl);
248 if (inode->i_default_acl && inode->i_default_acl != ACL_NOT_CACHED)
249 posix_acl_release(inode->i_default_acl);
250#endif
3e880fb5 251 this_cpu_dec(nr_inodes);
2e00c97e
CH
252}
253EXPORT_SYMBOL(__destroy_inode);
254
fa0d7e3d
NP
255static void i_callback(struct rcu_head *head)
256{
257 struct inode *inode = container_of(head, struct inode, i_rcu);
258 INIT_LIST_HEAD(&inode->i_dentry);
259 kmem_cache_free(inode_cachep, inode);
260}
261
56b0dacf 262static void destroy_inode(struct inode *inode)
2e00c97e 263{
7ccf19a8 264 BUG_ON(!list_empty(&inode->i_lru));
2e00c97e 265 __destroy_inode(inode);
1da177e4
LT
266 if (inode->i_sb->s_op->destroy_inode)
267 inode->i_sb->s_op->destroy_inode(inode);
268 else
fa0d7e3d 269 call_rcu(&inode->i_rcu, i_callback);
1da177e4 270}
1da177e4 271
2aa15890
MS
272void address_space_init_once(struct address_space *mapping)
273{
274 memset(mapping, 0, sizeof(*mapping));
275 INIT_RADIX_TREE(&mapping->page_tree, GFP_ATOMIC);
276 spin_lock_init(&mapping->tree_lock);
3d48ae45 277 mutex_init(&mapping->i_mmap_mutex);
2aa15890
MS
278 INIT_LIST_HEAD(&mapping->private_list);
279 spin_lock_init(&mapping->private_lock);
280 INIT_RAW_PRIO_TREE_ROOT(&mapping->i_mmap);
281 INIT_LIST_HEAD(&mapping->i_mmap_nonlinear);
2aa15890
MS
282}
283EXPORT_SYMBOL(address_space_init_once);
284
1da177e4
LT
285/*
286 * These are initializations that only need to be done
287 * once, because the fields are idempotent across use
288 * of the inode, so let the slab aware of that.
289 */
290void inode_init_once(struct inode *inode)
291{
292 memset(inode, 0, sizeof(*inode));
293 INIT_HLIST_NODE(&inode->i_hash);
294 INIT_LIST_HEAD(&inode->i_dentry);
295 INIT_LIST_HEAD(&inode->i_devices);
7ccf19a8
NP
296 INIT_LIST_HEAD(&inode->i_wb_list);
297 INIT_LIST_HEAD(&inode->i_lru);
2aa15890 298 address_space_init_once(&inode->i_data);
1da177e4 299 i_size_ordered_init(inode);
3be25f49 300#ifdef CONFIG_FSNOTIFY
e61ce867 301 INIT_HLIST_HEAD(&inode->i_fsnotify_marks);
3be25f49 302#endif
1da177e4 303}
1da177e4
LT
304EXPORT_SYMBOL(inode_init_once);
305
51cc5068 306static void init_once(void *foo)
1da177e4 307{
6b3304b5 308 struct inode *inode = (struct inode *) foo;
1da177e4 309
a35afb83 310 inode_init_once(inode);
1da177e4
LT
311}
312
313/*
250df6ed 314 * inode->i_lock must be held
1da177e4 315 */
6b3304b5 316void __iget(struct inode *inode)
1da177e4 317{
9e38d86f
NP
318 atomic_inc(&inode->i_count);
319}
2e147f1e 320
7de9c6ee
AV
321/*
322 * get additional reference to inode; caller must already hold one.
323 */
324void ihold(struct inode *inode)
325{
326 WARN_ON(atomic_inc_return(&inode->i_count) < 2);
327}
328EXPORT_SYMBOL(ihold);
329
9e38d86f
NP
330static void inode_lru_list_add(struct inode *inode)
331{
09cc9fc7 332 spin_lock(&inode->i_sb->s_inode_lru_lock);
7ccf19a8 333 if (list_empty(&inode->i_lru)) {
98b745c6
DC
334 list_add(&inode->i_lru, &inode->i_sb->s_inode_lru);
335 inode->i_sb->s_nr_inodes_unused++;
fcb94f72 336 this_cpu_inc(nr_unused);
9e38d86f 337 }
09cc9fc7 338 spin_unlock(&inode->i_sb->s_inode_lru_lock);
9e38d86f 339}
2e147f1e 340
9e38d86f
NP
341static void inode_lru_list_del(struct inode *inode)
342{
09cc9fc7 343 spin_lock(&inode->i_sb->s_inode_lru_lock);
7ccf19a8
NP
344 if (!list_empty(&inode->i_lru)) {
345 list_del_init(&inode->i_lru);
98b745c6 346 inode->i_sb->s_nr_inodes_unused--;
fcb94f72 347 this_cpu_dec(nr_unused);
9e38d86f 348 }
09cc9fc7 349 spin_unlock(&inode->i_sb->s_inode_lru_lock);
1da177e4
LT
350}
351
646ec461
CH
352/**
353 * inode_sb_list_add - add inode to the superblock list of inodes
354 * @inode: inode to add
355 */
356void inode_sb_list_add(struct inode *inode)
357{
55fa6091
DC
358 spin_lock(&inode_sb_list_lock);
359 list_add(&inode->i_sb_list, &inode->i_sb->s_inodes);
360 spin_unlock(&inode_sb_list_lock);
646ec461
CH
361}
362EXPORT_SYMBOL_GPL(inode_sb_list_add);
363
55fa6091 364static inline void inode_sb_list_del(struct inode *inode)
646ec461 365{
55fa6091 366 spin_lock(&inode_sb_list_lock);
646ec461 367 list_del_init(&inode->i_sb_list);
55fa6091 368 spin_unlock(&inode_sb_list_lock);
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
1da177e4
LT
800/**
801 * new_inode - obtain an inode
802 * @sb: superblock
803 *
769848c0 804 * Allocates a new inode for given superblock. The default gfp_mask
3c1d4378 805 * for allocations related to inode->i_mapping is GFP_HIGHUSER_MOVABLE.
769848c0
MG
806 * If HIGHMEM pages are unsuitable or it is known that pages allocated
807 * for the page cache are not reclaimable or migratable,
808 * mapping_set_gfp_mask() must be called with suitable flags on the
809 * newly created inode's mapping
810 *
1da177e4
LT
811 */
812struct inode *new_inode(struct super_block *sb)
813{
6b3304b5 814 struct inode *inode;
1da177e4 815
55fa6091 816 spin_lock_prefetch(&inode_sb_list_lock);
6b3304b5 817
1da177e4
LT
818 inode = alloc_inode(sb);
819 if (inode) {
250df6ed 820 spin_lock(&inode->i_lock);
1da177e4 821 inode->i_state = 0;
250df6ed 822 spin_unlock(&inode->i_lock);
55fa6091 823 inode_sb_list_add(inode);
1da177e4
LT
824 }
825 return inode;
826}
1da177e4
LT
827EXPORT_SYMBOL(new_inode);
828
250df6ed
DC
829/**
830 * unlock_new_inode - clear the I_NEW state and wake up any waiters
831 * @inode: new inode to unlock
832 *
833 * Called when the inode is fully initialised to clear the new state of the
834 * inode and wake up anyone waiting for the inode to finish initialisation.
835 */
1da177e4
LT
836void unlock_new_inode(struct inode *inode)
837{
14358e6d 838#ifdef CONFIG_DEBUG_LOCK_ALLOC
a3314a0e 839 if (S_ISDIR(inode->i_mode)) {
1e89a5e1
PZ
840 struct file_system_type *type = inode->i_sb->s_type;
841
9a7aa12f
JK
842 /* Set new key only if filesystem hasn't already changed it */
843 if (!lockdep_match_class(&inode->i_mutex,
844 &type->i_mutex_key)) {
845 /*
846 * ensure nobody is actually holding i_mutex
847 */
848 mutex_destroy(&inode->i_mutex);
849 mutex_init(&inode->i_mutex);
850 lockdep_set_class(&inode->i_mutex,
851 &type->i_mutex_dir_key);
852 }
1e89a5e1 853 }
14358e6d 854#endif
250df6ed 855 spin_lock(&inode->i_lock);
eaff8079
CH
856 WARN_ON(!(inode->i_state & I_NEW));
857 inode->i_state &= ~I_NEW;
250df6ed
DC
858 wake_up_bit(&inode->i_state, __I_NEW);
859 spin_unlock(&inode->i_lock);
1da177e4 860}
1da177e4
LT
861EXPORT_SYMBOL(unlock_new_inode);
862
0b2d0724
CH
863/**
864 * iget5_locked - obtain an inode from a mounted file system
865 * @sb: super block of file system
866 * @hashval: hash value (usually inode number) to get
867 * @test: callback used for comparisons between inodes
868 * @set: callback used to initialize a new struct inode
869 * @data: opaque data pointer to pass to @test and @set
870 *
871 * Search for the inode specified by @hashval and @data in the inode cache,
872 * and if present it is return it with an increased reference count. This is
873 * a generalized version of iget_locked() for file systems where the inode
874 * number is not sufficient for unique identification of an inode.
875 *
876 * If the inode is not in cache, allocate a new inode and return it locked,
877 * hashed, and with the I_NEW flag set. The file system gets to fill it in
878 * before unlocking it via unlock_new_inode().
1da177e4 879 *
0b2d0724
CH
880 * Note both @test and @set are called with the inode_hash_lock held, so can't
881 * sleep.
1da177e4 882 */
0b2d0724
CH
883struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
884 int (*test)(struct inode *, void *),
885 int (*set)(struct inode *, void *), void *data)
1da177e4 886{
0b2d0724 887 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
6b3304b5 888 struct inode *inode;
1da177e4 889
0b2d0724
CH
890 spin_lock(&inode_hash_lock);
891 inode = find_inode(sb, head, test, data);
892 spin_unlock(&inode_hash_lock);
893
894 if (inode) {
895 wait_on_inode(inode);
896 return inode;
897 }
898
1da177e4
LT
899 inode = alloc_inode(sb);
900 if (inode) {
6b3304b5 901 struct inode *old;
1da177e4 902
67a23c49 903 spin_lock(&inode_hash_lock);
1da177e4
LT
904 /* We released the lock, so.. */
905 old = find_inode(sb, head, test, data);
906 if (!old) {
907 if (set(inode, data))
908 goto set_failed;
909
250df6ed
DC
910 spin_lock(&inode->i_lock);
911 inode->i_state = I_NEW;
646ec461 912 hlist_add_head(&inode->i_hash, head);
250df6ed 913 spin_unlock(&inode->i_lock);
55fa6091 914 inode_sb_list_add(inode);
67a23c49 915 spin_unlock(&inode_hash_lock);
1da177e4
LT
916
917 /* Return the locked inode with I_NEW set, the
918 * caller is responsible for filling in the contents
919 */
920 return inode;
921 }
922
923 /*
924 * Uhhuh, somebody else created the same inode under
925 * us. Use the old inode instead of the one we just
926 * allocated.
927 */
67a23c49 928 spin_unlock(&inode_hash_lock);
1da177e4
LT
929 destroy_inode(inode);
930 inode = old;
931 wait_on_inode(inode);
932 }
933 return inode;
934
935set_failed:
67a23c49 936 spin_unlock(&inode_hash_lock);
1da177e4
LT
937 destroy_inode(inode);
938 return NULL;
939}
0b2d0724 940EXPORT_SYMBOL(iget5_locked);
1da177e4 941
0b2d0724
CH
942/**
943 * iget_locked - obtain an inode from a mounted file system
944 * @sb: super block of file system
945 * @ino: inode number to get
946 *
947 * Search for the inode specified by @ino in the inode cache and if present
948 * return it with an increased reference count. This is for file systems
949 * where the inode number is sufficient for unique identification of an inode.
950 *
951 * If the inode is not in cache, allocate a new inode and return it locked,
952 * hashed, and with the I_NEW flag set. The file system gets to fill it in
953 * before unlocking it via unlock_new_inode().
1da177e4 954 */
0b2d0724 955struct inode *iget_locked(struct super_block *sb, unsigned long ino)
1da177e4 956{
0b2d0724 957 struct hlist_head *head = inode_hashtable + hash(sb, ino);
6b3304b5 958 struct inode *inode;
1da177e4 959
0b2d0724
CH
960 spin_lock(&inode_hash_lock);
961 inode = find_inode_fast(sb, head, ino);
962 spin_unlock(&inode_hash_lock);
963 if (inode) {
964 wait_on_inode(inode);
965 return inode;
966 }
967
1da177e4
LT
968 inode = alloc_inode(sb);
969 if (inode) {
6b3304b5 970 struct inode *old;
1da177e4 971
67a23c49 972 spin_lock(&inode_hash_lock);
1da177e4
LT
973 /* We released the lock, so.. */
974 old = find_inode_fast(sb, head, ino);
975 if (!old) {
976 inode->i_ino = ino;
250df6ed
DC
977 spin_lock(&inode->i_lock);
978 inode->i_state = I_NEW;
646ec461 979 hlist_add_head(&inode->i_hash, head);
250df6ed 980 spin_unlock(&inode->i_lock);
55fa6091 981 inode_sb_list_add(inode);
67a23c49 982 spin_unlock(&inode_hash_lock);
1da177e4
LT
983
984 /* Return the locked inode with I_NEW set, the
985 * caller is responsible for filling in the contents
986 */
987 return inode;
988 }
989
990 /*
991 * Uhhuh, somebody else created the same inode under
992 * us. Use the old inode instead of the one we just
993 * allocated.
994 */
67a23c49 995 spin_unlock(&inode_hash_lock);
1da177e4
LT
996 destroy_inode(inode);
997 inode = old;
998 wait_on_inode(inode);
999 }
1000 return inode;
1001}
0b2d0724 1002EXPORT_SYMBOL(iget_locked);
1da177e4 1003
ad5e195a
CH
1004/*
1005 * search the inode cache for a matching inode number.
1006 * If we find one, then the inode number we are trying to
1007 * allocate is not unique and so we should not use it.
1008 *
1009 * Returns 1 if the inode number is unique, 0 if it is not.
1010 */
1011static int test_inode_iunique(struct super_block *sb, unsigned long ino)
1012{
1013 struct hlist_head *b = inode_hashtable + hash(sb, ino);
1014 struct hlist_node *node;
1015 struct inode *inode;
1016
67a23c49 1017 spin_lock(&inode_hash_lock);
ad5e195a 1018 hlist_for_each_entry(inode, node, b, i_hash) {
67a23c49
DC
1019 if (inode->i_ino == ino && inode->i_sb == sb) {
1020 spin_unlock(&inode_hash_lock);
ad5e195a 1021 return 0;
67a23c49 1022 }
ad5e195a 1023 }
67a23c49 1024 spin_unlock(&inode_hash_lock);
ad5e195a
CH
1025
1026 return 1;
1027}
1028
1da177e4
LT
1029/**
1030 * iunique - get a unique inode number
1031 * @sb: superblock
1032 * @max_reserved: highest reserved inode number
1033 *
1034 * Obtain an inode number that is unique on the system for a given
1035 * superblock. This is used by file systems that have no natural
1036 * permanent inode numbering system. An inode number is returned that
1037 * is higher than the reserved limit but unique.
1038 *
1039 * BUGS:
1040 * With a large number of inodes live on the file system this function
1041 * currently becomes quite slow.
1042 */
1043ino_t iunique(struct super_block *sb, ino_t max_reserved)
1044{
866b04fc
JL
1045 /*
1046 * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
1047 * error if st_ino won't fit in target struct field. Use 32bit counter
1048 * here to attempt to avoid that.
1049 */
ad5e195a 1050 static DEFINE_SPINLOCK(iunique_lock);
866b04fc 1051 static unsigned int counter;
1da177e4 1052 ino_t res;
3361c7be 1053
ad5e195a 1054 spin_lock(&iunique_lock);
3361c7be
JL
1055 do {
1056 if (counter <= max_reserved)
1057 counter = max_reserved + 1;
1da177e4 1058 res = counter++;
ad5e195a
CH
1059 } while (!test_inode_iunique(sb, res));
1060 spin_unlock(&iunique_lock);
1da177e4 1061
3361c7be
JL
1062 return res;
1063}
1da177e4
LT
1064EXPORT_SYMBOL(iunique);
1065
1066struct inode *igrab(struct inode *inode)
1067{
250df6ed
DC
1068 spin_lock(&inode->i_lock);
1069 if (!(inode->i_state & (I_FREEING|I_WILL_FREE))) {
1da177e4 1070 __iget(inode);
250df6ed
DC
1071 spin_unlock(&inode->i_lock);
1072 } else {
1073 spin_unlock(&inode->i_lock);
1da177e4
LT
1074 /*
1075 * Handle the case where s_op->clear_inode is not been
1076 * called yet, and somebody is calling igrab
1077 * while the inode is getting freed.
1078 */
1079 inode = NULL;
250df6ed 1080 }
1da177e4
LT
1081 return inode;
1082}
1da177e4
LT
1083EXPORT_SYMBOL(igrab);
1084
1085/**
0b2d0724 1086 * ilookup5_nowait - search for an inode in the inode cache
1da177e4 1087 * @sb: super block of file system to search
0b2d0724 1088 * @hashval: hash value (usually inode number) to search for
1da177e4
LT
1089 * @test: callback used for comparisons between inodes
1090 * @data: opaque data pointer to pass to @test
1da177e4 1091 *
0b2d0724 1092 * Search for the inode specified by @hashval and @data in the inode cache.
1da177e4
LT
1093 * If the inode is in the cache, the inode is returned with an incremented
1094 * reference count.
1095 *
0b2d0724
CH
1096 * Note: I_NEW is not waited upon so you have to be very careful what you do
1097 * with the returned inode. You probably should be using ilookup5() instead.
1da177e4 1098 *
b6d0ad68 1099 * Note2: @test is called with the inode_hash_lock held, so can't sleep.
1da177e4 1100 */
0b2d0724
CH
1101struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval,
1102 int (*test)(struct inode *, void *), void *data)
1da177e4 1103{
0b2d0724 1104 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1da177e4
LT
1105 struct inode *inode;
1106
67a23c49 1107 spin_lock(&inode_hash_lock);
1da177e4 1108 inode = find_inode(sb, head, test, data);
67a23c49 1109 spin_unlock(&inode_hash_lock);
88bd5121 1110
0b2d0724 1111 return inode;
88bd5121 1112}
88bd5121
AA
1113EXPORT_SYMBOL(ilookup5_nowait);
1114
1115/**
1116 * ilookup5 - search for an inode in the inode cache
1117 * @sb: super block of file system to search
1118 * @hashval: hash value (usually inode number) to search for
1119 * @test: callback used for comparisons between inodes
1120 * @data: opaque data pointer to pass to @test
1121 *
0b2d0724
CH
1122 * Search for the inode specified by @hashval and @data in the inode cache,
1123 * and if the inode is in the cache, return the inode with an incremented
1124 * reference count. Waits on I_NEW before returning the inode.
88bd5121 1125 * returned with an incremented reference count.
1da177e4 1126 *
0b2d0724
CH
1127 * This is a generalized version of ilookup() for file systems where the
1128 * inode number is not sufficient for unique identification of an inode.
1da177e4 1129 *
0b2d0724 1130 * Note: @test is called with the inode_hash_lock held, so can't sleep.
1da177e4
LT
1131 */
1132struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
1133 int (*test)(struct inode *, void *), void *data)
1134{
0b2d0724 1135 struct inode *inode = ilookup5_nowait(sb, hashval, test, data);
1da177e4 1136
0b2d0724
CH
1137 if (inode)
1138 wait_on_inode(inode);
1139 return inode;
1da177e4 1140}
1da177e4
LT
1141EXPORT_SYMBOL(ilookup5);
1142
1143/**
1144 * ilookup - search for an inode in the inode cache
1145 * @sb: super block of file system to search
1146 * @ino: inode number to search for
1147 *
0b2d0724
CH
1148 * Search for the inode @ino in the inode cache, and if the inode is in the
1149 * cache, the inode is returned with an incremented reference count.
1da177e4
LT
1150 */
1151struct inode *ilookup(struct super_block *sb, unsigned long ino)
1152{
1153 struct hlist_head *head = inode_hashtable + hash(sb, ino);
1da177e4
LT
1154 struct inode *inode;
1155
0b2d0724
CH
1156 spin_lock(&inode_hash_lock);
1157 inode = find_inode_fast(sb, head, ino);
1158 spin_unlock(&inode_hash_lock);
1da177e4 1159
1da177e4 1160 if (inode)
0b2d0724
CH
1161 wait_on_inode(inode);
1162 return inode;
1da177e4 1163}
0b2d0724 1164EXPORT_SYMBOL(ilookup);
1da177e4 1165
261bca86
AV
1166int insert_inode_locked(struct inode *inode)
1167{
1168 struct super_block *sb = inode->i_sb;
1169 ino_t ino = inode->i_ino;
1170 struct hlist_head *head = inode_hashtable + hash(sb, ino);
261bca86 1171
261bca86 1172 while (1) {
72a43d63
AV
1173 struct hlist_node *node;
1174 struct inode *old = NULL;
67a23c49 1175 spin_lock(&inode_hash_lock);
72a43d63
AV
1176 hlist_for_each_entry(old, node, head, i_hash) {
1177 if (old->i_ino != ino)
1178 continue;
1179 if (old->i_sb != sb)
1180 continue;
250df6ed
DC
1181 spin_lock(&old->i_lock);
1182 if (old->i_state & (I_FREEING|I_WILL_FREE)) {
1183 spin_unlock(&old->i_lock);
72a43d63 1184 continue;
250df6ed 1185 }
72a43d63
AV
1186 break;
1187 }
1188 if (likely(!node)) {
250df6ed
DC
1189 spin_lock(&inode->i_lock);
1190 inode->i_state |= I_NEW;
261bca86 1191 hlist_add_head(&inode->i_hash, head);
250df6ed 1192 spin_unlock(&inode->i_lock);
67a23c49 1193 spin_unlock(&inode_hash_lock);
261bca86
AV
1194 return 0;
1195 }
1196 __iget(old);
250df6ed 1197 spin_unlock(&old->i_lock);
67a23c49 1198 spin_unlock(&inode_hash_lock);
261bca86 1199 wait_on_inode(old);
1d3382cb 1200 if (unlikely(!inode_unhashed(old))) {
261bca86
AV
1201 iput(old);
1202 return -EBUSY;
1203 }
1204 iput(old);
1205 }
1206}
261bca86
AV
1207EXPORT_SYMBOL(insert_inode_locked);
1208
1209int insert_inode_locked4(struct inode *inode, unsigned long hashval,
1210 int (*test)(struct inode *, void *), void *data)
1211{
1212 struct super_block *sb = inode->i_sb;
1213 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
261bca86 1214
261bca86 1215 while (1) {
72a43d63
AV
1216 struct hlist_node *node;
1217 struct inode *old = NULL;
1218
67a23c49 1219 spin_lock(&inode_hash_lock);
72a43d63
AV
1220 hlist_for_each_entry(old, node, head, i_hash) {
1221 if (old->i_sb != sb)
1222 continue;
1223 if (!test(old, data))
1224 continue;
250df6ed
DC
1225 spin_lock(&old->i_lock);
1226 if (old->i_state & (I_FREEING|I_WILL_FREE)) {
1227 spin_unlock(&old->i_lock);
72a43d63 1228 continue;
250df6ed 1229 }
72a43d63
AV
1230 break;
1231 }
1232 if (likely(!node)) {
250df6ed
DC
1233 spin_lock(&inode->i_lock);
1234 inode->i_state |= I_NEW;
261bca86 1235 hlist_add_head(&inode->i_hash, head);
250df6ed 1236 spin_unlock(&inode->i_lock);
67a23c49 1237 spin_unlock(&inode_hash_lock);
261bca86
AV
1238 return 0;
1239 }
1240 __iget(old);
250df6ed 1241 spin_unlock(&old->i_lock);
67a23c49 1242 spin_unlock(&inode_hash_lock);
261bca86 1243 wait_on_inode(old);
1d3382cb 1244 if (unlikely(!inode_unhashed(old))) {
261bca86
AV
1245 iput(old);
1246 return -EBUSY;
1247 }
1248 iput(old);
1249 }
1250}
261bca86
AV
1251EXPORT_SYMBOL(insert_inode_locked4);
1252
1da177e4 1253
45321ac5
AV
1254int generic_delete_inode(struct inode *inode)
1255{
1256 return 1;
1257}
1258EXPORT_SYMBOL(generic_delete_inode);
1259
1da177e4 1260/*
45321ac5
AV
1261 * Normal UNIX filesystem behaviour: delete the
1262 * inode when the usage count drops to zero, and
1263 * i_nlink is zero.
1da177e4 1264 */
45321ac5 1265int generic_drop_inode(struct inode *inode)
1da177e4 1266{
1d3382cb 1267 return !inode->i_nlink || inode_unhashed(inode);
1da177e4 1268}
45321ac5 1269EXPORT_SYMBOL_GPL(generic_drop_inode);
1da177e4 1270
45321ac5
AV
1271/*
1272 * Called when we're dropping the last reference
1273 * to an inode.
22fe4042 1274 *
45321ac5
AV
1275 * Call the FS "drop_inode()" function, defaulting to
1276 * the legacy UNIX filesystem behaviour. If it tells
1277 * us to evict inode, do so. Otherwise, retain inode
1278 * in cache if fs is alive, sync and evict if fs is
1279 * shutting down.
22fe4042 1280 */
45321ac5 1281static void iput_final(struct inode *inode)
1da177e4
LT
1282{
1283 struct super_block *sb = inode->i_sb;
45321ac5
AV
1284 const struct super_operations *op = inode->i_sb->s_op;
1285 int drop;
1286
250df6ed
DC
1287 WARN_ON(inode->i_state & I_NEW);
1288
e7f59097 1289 if (op->drop_inode)
45321ac5
AV
1290 drop = op->drop_inode(inode);
1291 else
1292 drop = generic_drop_inode(inode);
1da177e4 1293
b2b2af8e
DC
1294 if (!drop && (sb->s_flags & MS_ACTIVE)) {
1295 inode->i_state |= I_REFERENCED;
1296 if (!(inode->i_state & (I_DIRTY|I_SYNC)))
1297 inode_lru_list_add(inode);
1298 spin_unlock(&inode->i_lock);
b2b2af8e
DC
1299 return;
1300 }
1301
45321ac5 1302 if (!drop) {
991114c6 1303 inode->i_state |= I_WILL_FREE;
250df6ed 1304 spin_unlock(&inode->i_lock);
1da177e4 1305 write_inode_now(inode, 1);
250df6ed 1306 spin_lock(&inode->i_lock);
7ef0d737 1307 WARN_ON(inode->i_state & I_NEW);
991114c6 1308 inode->i_state &= ~I_WILL_FREE;
1da177e4 1309 }
7ccf19a8 1310
991114c6 1311 inode->i_state |= I_FREEING;
9e38d86f 1312 inode_lru_list_del(inode);
b2b2af8e 1313 spin_unlock(&inode->i_lock);
b2b2af8e 1314
644da596 1315 evict(inode);
1da177e4
LT
1316}
1317
1da177e4 1318/**
6b3304b5 1319 * iput - put an inode
1da177e4
LT
1320 * @inode: inode to put
1321 *
1322 * Puts an inode, dropping its usage count. If the inode use count hits
1323 * zero, the inode is then freed and may also be destroyed.
1324 *
1325 * Consequently, iput() can sleep.
1326 */
1327void iput(struct inode *inode)
1328{
1329 if (inode) {
a4ffdde6 1330 BUG_ON(inode->i_state & I_CLEAR);
1da177e4 1331
f283c86a 1332 if (atomic_dec_and_lock(&inode->i_count, &inode->i_lock))
1da177e4
LT
1333 iput_final(inode);
1334 }
1335}
1da177e4
LT
1336EXPORT_SYMBOL(iput);
1337
1338/**
1339 * bmap - find a block number in a file
1340 * @inode: inode of file
1341 * @block: block to find
1342 *
1343 * Returns the block number on the device holding the inode that
1344 * is the disk block number for the block of the file requested.
1345 * That is, asked for block 4 of inode 1 the function will return the
6b3304b5 1346 * disk block relative to the disk start that holds that block of the
1da177e4
LT
1347 * file.
1348 */
6b3304b5 1349sector_t bmap(struct inode *inode, sector_t block)
1da177e4
LT
1350{
1351 sector_t res = 0;
1352 if (inode->i_mapping->a_ops->bmap)
1353 res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block);
1354 return res;
1355}
1da177e4
LT
1356EXPORT_SYMBOL(bmap);
1357
11ff6f05
MG
1358/*
1359 * With relative atime, only update atime if the previous atime is
1360 * earlier than either the ctime or mtime or if at least a day has
1361 * passed since the last atime update.
1362 */
1363static int relatime_need_update(struct vfsmount *mnt, struct inode *inode,
1364 struct timespec now)
1365{
1366
1367 if (!(mnt->mnt_flags & MNT_RELATIME))
1368 return 1;
1369 /*
1370 * Is mtime younger than atime? If yes, update atime:
1371 */
1372 if (timespec_compare(&inode->i_mtime, &inode->i_atime) >= 0)
1373 return 1;
1374 /*
1375 * Is ctime younger than atime? If yes, update atime:
1376 */
1377 if (timespec_compare(&inode->i_ctime, &inode->i_atime) >= 0)
1378 return 1;
1379
1380 /*
1381 * Is the previous atime value older than a day? If yes,
1382 * update atime:
1383 */
1384 if ((long)(now.tv_sec - inode->i_atime.tv_sec) >= 24*60*60)
1385 return 1;
1386 /*
1387 * Good, we can skip the atime update:
1388 */
1389 return 0;
1390}
1391
1da177e4 1392/**
869243a0
CH
1393 * touch_atime - update the access time
1394 * @mnt: mount the inode is accessed on
7045f37b 1395 * @dentry: dentry accessed
1da177e4
LT
1396 *
1397 * Update the accessed time on an inode and mark it for writeback.
1398 * This function automatically handles read only file systems and media,
1399 * as well as the "noatime" flag and inode specific "noatime" markers.
1400 */
869243a0 1401void touch_atime(struct vfsmount *mnt, struct dentry *dentry)
1da177e4 1402{
869243a0 1403 struct inode *inode = dentry->d_inode;
1da177e4
LT
1404 struct timespec now;
1405
cdb70f3f 1406 if (inode->i_flags & S_NOATIME)
b12536c2 1407 return;
37756ced 1408 if (IS_NOATIME(inode))
b12536c2 1409 return;
b2276138 1410 if ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode))
b12536c2 1411 return;
47ae32d6 1412
cdb70f3f 1413 if (mnt->mnt_flags & MNT_NOATIME)
b12536c2 1414 return;
cdb70f3f 1415 if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
b12536c2 1416 return;
1da177e4
LT
1417
1418 now = current_fs_time(inode->i_sb);
11ff6f05
MG
1419
1420 if (!relatime_need_update(mnt, inode, now))
b12536c2 1421 return;
11ff6f05 1422
47ae32d6 1423 if (timespec_equal(&inode->i_atime, &now))
b12536c2
AK
1424 return;
1425
1426 if (mnt_want_write(mnt))
1427 return;
47ae32d6
VH
1428
1429 inode->i_atime = now;
1430 mark_inode_dirty_sync(inode);
cdb70f3f 1431 mnt_drop_write(mnt);
1da177e4 1432}
869243a0 1433EXPORT_SYMBOL(touch_atime);
1da177e4
LT
1434
1435/**
870f4817
CH
1436 * file_update_time - update mtime and ctime time
1437 * @file: file accessed
1da177e4 1438 *
870f4817
CH
1439 * Update the mtime and ctime members of an inode and mark the inode
1440 * for writeback. Note that this function is meant exclusively for
1441 * usage in the file write path of filesystems, and filesystems may
1442 * choose to explicitly ignore update via this function with the
2eadfc0e 1443 * S_NOCMTIME inode flag, e.g. for network filesystem where these
870f4817 1444 * timestamps are handled by the server.
1da177e4
LT
1445 */
1446
870f4817 1447void file_update_time(struct file *file)
1da177e4 1448{
0f7fc9e4 1449 struct inode *inode = file->f_path.dentry->d_inode;
1da177e4 1450 struct timespec now;
ce06e0b2 1451 enum { S_MTIME = 1, S_CTIME = 2, S_VERSION = 4 } sync_it = 0;
1da177e4 1452
ce06e0b2 1453 /* First try to exhaust all avenues to not sync */
1da177e4
LT
1454 if (IS_NOCMTIME(inode))
1455 return;
20ddee2c 1456
1da177e4 1457 now = current_fs_time(inode->i_sb);
ce06e0b2
AK
1458 if (!timespec_equal(&inode->i_mtime, &now))
1459 sync_it = S_MTIME;
1da177e4 1460
ce06e0b2
AK
1461 if (!timespec_equal(&inode->i_ctime, &now))
1462 sync_it |= S_CTIME;
870f4817 1463
ce06e0b2
AK
1464 if (IS_I_VERSION(inode))
1465 sync_it |= S_VERSION;
7a224228 1466
ce06e0b2
AK
1467 if (!sync_it)
1468 return;
1469
1470 /* Finally allowed to write? Takes lock. */
1471 if (mnt_want_write_file(file))
1472 return;
1473
1474 /* Only change inode inside the lock region */
1475 if (sync_it & S_VERSION)
1476 inode_inc_iversion(inode);
1477 if (sync_it & S_CTIME)
1478 inode->i_ctime = now;
1479 if (sync_it & S_MTIME)
1480 inode->i_mtime = now;
1481 mark_inode_dirty_sync(inode);
20ddee2c 1482 mnt_drop_write(file->f_path.mnt);
1da177e4 1483}
870f4817 1484EXPORT_SYMBOL(file_update_time);
1da177e4
LT
1485
1486int inode_needs_sync(struct inode *inode)
1487{
1488 if (IS_SYNC(inode))
1489 return 1;
1490 if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
1491 return 1;
1492 return 0;
1493}
1da177e4
LT
1494EXPORT_SYMBOL(inode_needs_sync);
1495
1da177e4
LT
1496int inode_wait(void *word)
1497{
1498 schedule();
1499 return 0;
1500}
d44dab8d 1501EXPORT_SYMBOL(inode_wait);
1da177e4
LT
1502
1503/*
168a9fd6
MS
1504 * If we try to find an inode in the inode hash while it is being
1505 * deleted, we have to wait until the filesystem completes its
1506 * deletion before reporting that it isn't found. This function waits
1507 * until the deletion _might_ have completed. Callers are responsible
1508 * to recheck inode state.
1509 *
eaff8079 1510 * It doesn't matter if I_NEW is not set initially, a call to
250df6ed
DC
1511 * wake_up_bit(&inode->i_state, __I_NEW) after removing from the hash list
1512 * will DTRT.
1da177e4
LT
1513 */
1514static void __wait_on_freeing_inode(struct inode *inode)
1515{
1516 wait_queue_head_t *wq;
eaff8079
CH
1517 DEFINE_WAIT_BIT(wait, &inode->i_state, __I_NEW);
1518 wq = bit_waitqueue(&inode->i_state, __I_NEW);
1da177e4 1519 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
250df6ed 1520 spin_unlock(&inode->i_lock);
67a23c49 1521 spin_unlock(&inode_hash_lock);
1da177e4
LT
1522 schedule();
1523 finish_wait(wq, &wait.wait);
67a23c49 1524 spin_lock(&inode_hash_lock);
1da177e4
LT
1525}
1526
1da177e4
LT
1527static __initdata unsigned long ihash_entries;
1528static int __init set_ihash_entries(char *str)
1529{
1530 if (!str)
1531 return 0;
1532 ihash_entries = simple_strtoul(str, &str, 0);
1533 return 1;
1534}
1535__setup("ihash_entries=", set_ihash_entries);
1536
1537/*
1538 * Initialize the waitqueues and inode hash table.
1539 */
1540void __init inode_init_early(void)
1541{
1542 int loop;
1543
1544 /* If hashes are distributed across NUMA nodes, defer
1545 * hash allocation until vmalloc space is available.
1546 */
1547 if (hashdist)
1548 return;
1549
1550 inode_hashtable =
1551 alloc_large_system_hash("Inode-cache",
1552 sizeof(struct hlist_head),
1553 ihash_entries,
1554 14,
1555 HASH_EARLY,
1556 &i_hash_shift,
1557 &i_hash_mask,
1558 0);
1559
1560 for (loop = 0; loop < (1 << i_hash_shift); loop++)
1561 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1562}
1563
74bf17cf 1564void __init inode_init(void)
1da177e4
LT
1565{
1566 int loop;
1567
1568 /* inode slab cache */
b0196009
PJ
1569 inode_cachep = kmem_cache_create("inode_cache",
1570 sizeof(struct inode),
1571 0,
1572 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
1573 SLAB_MEM_SPREAD),
20c2df83 1574 init_once);
1da177e4
LT
1575
1576 /* Hash may have been set up in inode_init_early */
1577 if (!hashdist)
1578 return;
1579
1580 inode_hashtable =
1581 alloc_large_system_hash("Inode-cache",
1582 sizeof(struct hlist_head),
1583 ihash_entries,
1584 14,
1585 0,
1586 &i_hash_shift,
1587 &i_hash_mask,
1588 0);
1589
1590 for (loop = 0; loop < (1 << i_hash_shift); loop++)
1591 INIT_HLIST_HEAD(&inode_hashtable[loop]);
1592}
1593
1594void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
1595{
1596 inode->i_mode = mode;
1597 if (S_ISCHR(mode)) {
1598 inode->i_fop = &def_chr_fops;
1599 inode->i_rdev = rdev;
1600 } else if (S_ISBLK(mode)) {
1601 inode->i_fop = &def_blk_fops;
1602 inode->i_rdev = rdev;
1603 } else if (S_ISFIFO(mode))
1604 inode->i_fop = &def_fifo_fops;
1605 else if (S_ISSOCK(mode))
1606 inode->i_fop = &bad_sock_fops;
1607 else
af0d9ae8
MK
1608 printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o) for"
1609 " inode %s:%lu\n", mode, inode->i_sb->s_id,
1610 inode->i_ino);
1da177e4
LT
1611}
1612EXPORT_SYMBOL(init_special_inode);
a1bd120d
DM
1613
1614/**
eaae668d 1615 * inode_init_owner - Init uid,gid,mode for new inode according to posix standards
a1bd120d
DM
1616 * @inode: New inode
1617 * @dir: Directory inode
1618 * @mode: mode of the new inode
1619 */
1620void inode_init_owner(struct inode *inode, const struct inode *dir,
1621 mode_t mode)
1622{
1623 inode->i_uid = current_fsuid();
1624 if (dir && dir->i_mode & S_ISGID) {
1625 inode->i_gid = dir->i_gid;
1626 if (S_ISDIR(mode))
1627 mode |= S_ISGID;
1628 } else
1629 inode->i_gid = current_fsgid();
1630 inode->i_mode = mode;
1631}
1632EXPORT_SYMBOL(inode_init_owner);
e795b717 1633
2e149670
SH
1634/**
1635 * inode_owner_or_capable - check current task permissions to inode
1636 * @inode: inode being checked
1637 *
1638 * Return true if current either has CAP_FOWNER to the inode, or
1639 * owns the file.
e795b717 1640 */
2e149670 1641bool inode_owner_or_capable(const struct inode *inode)
e795b717
SH
1642{
1643 struct user_namespace *ns = inode_userns(inode);
1644
1645 if (current_user_ns() == ns && current_fsuid() == inode->i_uid)
1646 return true;
1647 if (ns_capable(ns, CAP_FOWNER))
1648 return true;
1649 return false;
1650}
2e149670 1651EXPORT_SYMBOL(inode_owner_or_capable);