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