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