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