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