Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/fs/inode.c | |
3 | * | |
4 | * (C) 1997 Linus Torvalds | |
5 | */ | |
6 | ||
1da177e4 LT |
7 | #include <linux/fs.h> |
8 | #include <linux/mm.h> | |
9 | #include <linux/dcache.h> | |
10 | #include <linux/init.h> | |
11 | #include <linux/quotaops.h> | |
12 | #include <linux/slab.h> | |
13 | #include <linux/writeback.h> | |
14 | #include <linux/module.h> | |
15 | #include <linux/backing-dev.h> | |
16 | #include <linux/wait.h> | |
17 | #include <linux/hash.h> | |
18 | #include <linux/swap.h> | |
19 | #include <linux/security.h> | |
20 | #include <linux/pagemap.h> | |
21 | #include <linux/cdev.h> | |
22 | #include <linux/bootmem.h> | |
0eeca283 | 23 | #include <linux/inotify.h> |
fc33a7bb | 24 | #include <linux/mount.h> |
1da177e4 LT |
25 | |
26 | /* | |
27 | * This is needed for the following functions: | |
28 | * - inode_has_buffers | |
29 | * - invalidate_inode_buffers | |
1da177e4 LT |
30 | * - invalidate_bdev |
31 | * | |
32 | * FIXME: remove all knowledge of the buffer layer from this file | |
33 | */ | |
34 | #include <linux/buffer_head.h> | |
35 | ||
36 | /* | |
37 | * New inode.c implementation. | |
38 | * | |
39 | * This implementation has the basic premise of trying | |
40 | * to be extremely low-overhead and SMP-safe, yet be | |
41 | * simple enough to be "obviously correct". | |
42 | * | |
43 | * Famous last words. | |
44 | */ | |
45 | ||
46 | /* inode dynamic allocation 1999, Andrea Arcangeli <andrea@suse.de> */ | |
47 | ||
48 | /* #define INODE_PARANOIA 1 */ | |
49 | /* #define INODE_DEBUG 1 */ | |
50 | ||
51 | /* | |
52 | * Inode lookup is no longer as critical as it used to be: | |
53 | * most of the lookups are going to be through the dcache. | |
54 | */ | |
55 | #define I_HASHBITS i_hash_shift | |
56 | #define I_HASHMASK i_hash_mask | |
57 | ||
fa3536cc ED |
58 | static unsigned int i_hash_mask __read_mostly; |
59 | static unsigned int i_hash_shift __read_mostly; | |
1da177e4 LT |
60 | |
61 | /* | |
62 | * Each inode can be on two separate lists. One is | |
63 | * the hash list of the inode, used for lookups. The | |
64 | * other linked list is the "type" list: | |
65 | * "in_use" - valid inode, i_count > 0, i_nlink > 0 | |
66 | * "dirty" - as "in_use" but also dirty | |
67 | * "unused" - valid inode, i_count = 0 | |
68 | * | |
69 | * A "dirty" list is maintained for each super block, | |
70 | * allowing for low-overhead inode sync() operations. | |
71 | */ | |
72 | ||
73 | LIST_HEAD(inode_in_use); | |
74 | LIST_HEAD(inode_unused); | |
fa3536cc | 75 | static struct hlist_head *inode_hashtable __read_mostly; |
1da177e4 LT |
76 | |
77 | /* | |
78 | * A simple spinlock to protect the list manipulations. | |
79 | * | |
80 | * NOTE! You also have to own the lock if you change | |
81 | * the i_state of an inode while it is in use.. | |
82 | */ | |
83 | DEFINE_SPINLOCK(inode_lock); | |
84 | ||
85 | /* | |
f24075bd | 86 | * iprune_mutex provides exclusion between the kswapd or try_to_free_pages |
1da177e4 LT |
87 | * icache shrinking path, and the umount path. Without this exclusion, |
88 | * by the time prune_icache calls iput for the inode whose pages it has | |
89 | * been invalidating, or by the time it calls clear_inode & destroy_inode | |
90 | * from its final dispose_list, the struct super_block they refer to | |
91 | * (for inode->i_sb->s_op) may already have been freed and reused. | |
92 | */ | |
bdfc3266 | 93 | static DEFINE_MUTEX(iprune_mutex); |
1da177e4 LT |
94 | |
95 | /* | |
96 | * Statistics gathering.. | |
97 | */ | |
98 | struct inodes_stat_t inodes_stat; | |
99 | ||
fa3536cc | 100 | static kmem_cache_t * inode_cachep __read_mostly; |
1da177e4 LT |
101 | |
102 | static struct inode *alloc_inode(struct super_block *sb) | |
103 | { | |
f5e54d6e | 104 | static const struct address_space_operations empty_aops; |
1da177e4 | 105 | static struct inode_operations empty_iops; |
99ac48f5 | 106 | static const struct file_operations empty_fops; |
1da177e4 LT |
107 | struct inode *inode; |
108 | ||
109 | if (sb->s_op->alloc_inode) | |
110 | inode = sb->s_op->alloc_inode(sb); | |
111 | else | |
112 | inode = (struct inode *) kmem_cache_alloc(inode_cachep, SLAB_KERNEL); | |
113 | ||
114 | if (inode) { | |
115 | struct address_space * const mapping = &inode->i_data; | |
116 | ||
117 | inode->i_sb = sb; | |
118 | inode->i_blkbits = sb->s_blocksize_bits; | |
119 | inode->i_flags = 0; | |
120 | atomic_set(&inode->i_count, 1); | |
121 | inode->i_op = &empty_iops; | |
122 | inode->i_fop = &empty_fops; | |
123 | inode->i_nlink = 1; | |
124 | atomic_set(&inode->i_writecount, 0); | |
125 | inode->i_size = 0; | |
126 | inode->i_blocks = 0; | |
127 | inode->i_bytes = 0; | |
128 | inode->i_generation = 0; | |
129 | #ifdef CONFIG_QUOTA | |
130 | memset(&inode->i_dquot, 0, sizeof(inode->i_dquot)); | |
131 | #endif | |
132 | inode->i_pipe = NULL; | |
133 | inode->i_bdev = NULL; | |
134 | inode->i_cdev = NULL; | |
135 | inode->i_rdev = 0; | |
1da177e4 LT |
136 | inode->dirtied_when = 0; |
137 | if (security_inode_alloc(inode)) { | |
138 | if (inode->i_sb->s_op->destroy_inode) | |
139 | inode->i_sb->s_op->destroy_inode(inode); | |
140 | else | |
141 | kmem_cache_free(inode_cachep, (inode)); | |
142 | return NULL; | |
143 | } | |
144 | ||
145 | mapping->a_ops = &empty_aops; | |
146 | mapping->host = inode; | |
147 | mapping->flags = 0; | |
148 | mapping_set_gfp_mask(mapping, GFP_HIGHUSER); | |
149 | mapping->assoc_mapping = NULL; | |
150 | mapping->backing_dev_info = &default_backing_dev_info; | |
151 | ||
152 | /* | |
153 | * If the block_device provides a backing_dev_info for client | |
154 | * inodes then use that. Otherwise the inode share the bdev's | |
155 | * backing_dev_info. | |
156 | */ | |
157 | if (sb->s_bdev) { | |
158 | struct backing_dev_info *bdi; | |
159 | ||
160 | bdi = sb->s_bdev->bd_inode_backing_dev_info; | |
161 | if (!bdi) | |
162 | bdi = sb->s_bdev->bd_inode->i_mapping->backing_dev_info; | |
163 | mapping->backing_dev_info = bdi; | |
164 | } | |
8e18e294 | 165 | inode->i_private = 0; |
1da177e4 LT |
166 | inode->i_mapping = mapping; |
167 | } | |
168 | return inode; | |
169 | } | |
170 | ||
171 | void destroy_inode(struct inode *inode) | |
172 | { | |
b7542f8c | 173 | BUG_ON(inode_has_buffers(inode)); |
1da177e4 LT |
174 | security_inode_free(inode); |
175 | if (inode->i_sb->s_op->destroy_inode) | |
176 | inode->i_sb->s_op->destroy_inode(inode); | |
177 | else | |
178 | kmem_cache_free(inode_cachep, (inode)); | |
179 | } | |
180 | ||
181 | ||
182 | /* | |
183 | * These are initializations that only need to be done | |
184 | * once, because the fields are idempotent across use | |
185 | * of the inode, so let the slab aware of that. | |
186 | */ | |
187 | void inode_init_once(struct inode *inode) | |
188 | { | |
189 | memset(inode, 0, sizeof(*inode)); | |
190 | INIT_HLIST_NODE(&inode->i_hash); | |
191 | INIT_LIST_HEAD(&inode->i_dentry); | |
192 | INIT_LIST_HEAD(&inode->i_devices); | |
1b1dcc1b | 193 | mutex_init(&inode->i_mutex); |
1da177e4 LT |
194 | init_rwsem(&inode->i_alloc_sem); |
195 | INIT_RADIX_TREE(&inode->i_data.page_tree, GFP_ATOMIC); | |
196 | rwlock_init(&inode->i_data.tree_lock); | |
197 | spin_lock_init(&inode->i_data.i_mmap_lock); | |
198 | INIT_LIST_HEAD(&inode->i_data.private_list); | |
199 | spin_lock_init(&inode->i_data.private_lock); | |
200 | INIT_RAW_PRIO_TREE_ROOT(&inode->i_data.i_mmap); | |
201 | INIT_LIST_HEAD(&inode->i_data.i_mmap_nonlinear); | |
202 | spin_lock_init(&inode->i_lock); | |
203 | i_size_ordered_init(inode); | |
0eeca283 RL |
204 | #ifdef CONFIG_INOTIFY |
205 | INIT_LIST_HEAD(&inode->inotify_watches); | |
d4f9af9d | 206 | mutex_init(&inode->inotify_mutex); |
0eeca283 | 207 | #endif |
1da177e4 LT |
208 | } |
209 | ||
210 | EXPORT_SYMBOL(inode_init_once); | |
211 | ||
212 | static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags) | |
213 | { | |
214 | struct inode * inode = (struct inode *) foo; | |
215 | ||
216 | if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) == | |
217 | SLAB_CTOR_CONSTRUCTOR) | |
218 | inode_init_once(inode); | |
219 | } | |
220 | ||
221 | /* | |
222 | * inode_lock must be held | |
223 | */ | |
224 | void __iget(struct inode * inode) | |
225 | { | |
226 | if (atomic_read(&inode->i_count)) { | |
227 | atomic_inc(&inode->i_count); | |
228 | return; | |
229 | } | |
230 | atomic_inc(&inode->i_count); | |
231 | if (!(inode->i_state & (I_DIRTY|I_LOCK))) | |
232 | list_move(&inode->i_list, &inode_in_use); | |
233 | inodes_stat.nr_unused--; | |
234 | } | |
235 | ||
236 | /** | |
237 | * clear_inode - clear an inode | |
238 | * @inode: inode to clear | |
239 | * | |
240 | * This is called by the filesystem to tell us | |
241 | * that the inode is no longer useful. We just | |
242 | * terminate it with extreme prejudice. | |
243 | */ | |
244 | void clear_inode(struct inode *inode) | |
245 | { | |
246 | might_sleep(); | |
247 | invalidate_inode_buffers(inode); | |
248 | ||
b7542f8c ES |
249 | BUG_ON(inode->i_data.nrpages); |
250 | BUG_ON(!(inode->i_state & I_FREEING)); | |
251 | BUG_ON(inode->i_state & I_CLEAR); | |
1da177e4 LT |
252 | wait_on_inode(inode); |
253 | DQUOT_DROP(inode); | |
254 | if (inode->i_sb && inode->i_sb->s_op->clear_inode) | |
255 | inode->i_sb->s_op->clear_inode(inode); | |
eaf796e7 | 256 | if (S_ISBLK(inode->i_mode) && inode->i_bdev) |
1da177e4 | 257 | bd_forget(inode); |
577c4eb0 | 258 | if (S_ISCHR(inode->i_mode) && inode->i_cdev) |
1da177e4 LT |
259 | cd_forget(inode); |
260 | inode->i_state = I_CLEAR; | |
261 | } | |
262 | ||
263 | EXPORT_SYMBOL(clear_inode); | |
264 | ||
265 | /* | |
266 | * dispose_list - dispose of the contents of a local list | |
267 | * @head: the head of the list to free | |
268 | * | |
269 | * Dispose-list gets a local list with local inodes in it, so it doesn't | |
270 | * need to worry about list corruption and SMP locks. | |
271 | */ | |
272 | static void dispose_list(struct list_head *head) | |
273 | { | |
274 | int nr_disposed = 0; | |
275 | ||
276 | while (!list_empty(head)) { | |
277 | struct inode *inode; | |
278 | ||
279 | inode = list_entry(head->next, struct inode, i_list); | |
280 | list_del(&inode->i_list); | |
281 | ||
282 | if (inode->i_data.nrpages) | |
283 | truncate_inode_pages(&inode->i_data, 0); | |
284 | clear_inode(inode); | |
4120db47 AB |
285 | |
286 | spin_lock(&inode_lock); | |
287 | hlist_del_init(&inode->i_hash); | |
288 | list_del_init(&inode->i_sb_list); | |
289 | spin_unlock(&inode_lock); | |
290 | ||
291 | wake_up_inode(inode); | |
1da177e4 LT |
292 | destroy_inode(inode); |
293 | nr_disposed++; | |
294 | } | |
295 | spin_lock(&inode_lock); | |
296 | inodes_stat.nr_inodes -= nr_disposed; | |
297 | spin_unlock(&inode_lock); | |
298 | } | |
299 | ||
300 | /* | |
301 | * Invalidate all inodes for a device. | |
302 | */ | |
303 | static int invalidate_list(struct list_head *head, struct list_head *dispose) | |
304 | { | |
305 | struct list_head *next; | |
306 | int busy = 0, count = 0; | |
307 | ||
308 | next = head->next; | |
309 | for (;;) { | |
310 | struct list_head * tmp = next; | |
311 | struct inode * inode; | |
312 | ||
313 | /* | |
314 | * We can reschedule here without worrying about the list's | |
315 | * consistency because the per-sb list of inodes must not | |
f24075bd | 316 | * change during umount anymore, and because iprune_mutex keeps |
1da177e4 LT |
317 | * shrink_icache_memory() away. |
318 | */ | |
319 | cond_resched_lock(&inode_lock); | |
320 | ||
321 | next = next->next; | |
322 | if (tmp == head) | |
323 | break; | |
324 | inode = list_entry(tmp, struct inode, i_sb_list); | |
325 | invalidate_inode_buffers(inode); | |
326 | if (!atomic_read(&inode->i_count)) { | |
1da177e4 LT |
327 | list_move(&inode->i_list, dispose); |
328 | inode->i_state |= I_FREEING; | |
329 | count++; | |
330 | continue; | |
331 | } | |
332 | busy = 1; | |
333 | } | |
334 | /* only unused inodes may be cached with i_count zero */ | |
335 | inodes_stat.nr_unused -= count; | |
336 | return busy; | |
337 | } | |
338 | ||
1da177e4 LT |
339 | /** |
340 | * invalidate_inodes - discard the inodes on a device | |
341 | * @sb: superblock | |
342 | * | |
343 | * Discard all of the inodes for a given superblock. If the discard | |
344 | * fails because there are busy inodes then a non zero value is returned. | |
345 | * If the discard is successful all the inodes have been discarded. | |
346 | */ | |
347 | int invalidate_inodes(struct super_block * sb) | |
348 | { | |
349 | int busy; | |
350 | LIST_HEAD(throw_away); | |
351 | ||
f24075bd | 352 | mutex_lock(&iprune_mutex); |
1da177e4 | 353 | spin_lock(&inode_lock); |
0eeca283 | 354 | inotify_unmount_inodes(&sb->s_inodes); |
1da177e4 LT |
355 | busy = invalidate_list(&sb->s_inodes, &throw_away); |
356 | spin_unlock(&inode_lock); | |
357 | ||
358 | dispose_list(&throw_away); | |
f24075bd | 359 | mutex_unlock(&iprune_mutex); |
1da177e4 LT |
360 | |
361 | return busy; | |
362 | } | |
363 | ||
364 | EXPORT_SYMBOL(invalidate_inodes); | |
365 | ||
2ef41634 | 366 | int __invalidate_device(struct block_device *bdev) |
1da177e4 | 367 | { |
2ef41634 CH |
368 | struct super_block *sb = get_super(bdev); |
369 | int res = 0; | |
1da177e4 | 370 | |
1da177e4 LT |
371 | if (sb) { |
372 | /* | |
373 | * no need to lock the super, get_super holds the | |
f24075bd | 374 | * read mutex so the filesystem cannot go away |
1da177e4 LT |
375 | * under us (->put_super runs with the write lock |
376 | * hold). | |
377 | */ | |
378 | shrink_dcache_sb(sb); | |
379 | res = invalidate_inodes(sb); | |
380 | drop_super(sb); | |
381 | } | |
382 | invalidate_bdev(bdev, 0); | |
383 | return res; | |
384 | } | |
1da177e4 LT |
385 | EXPORT_SYMBOL(__invalidate_device); |
386 | ||
387 | static int can_unuse(struct inode *inode) | |
388 | { | |
389 | if (inode->i_state) | |
390 | return 0; | |
391 | if (inode_has_buffers(inode)) | |
392 | return 0; | |
393 | if (atomic_read(&inode->i_count)) | |
394 | return 0; | |
395 | if (inode->i_data.nrpages) | |
396 | return 0; | |
397 | return 1; | |
398 | } | |
399 | ||
400 | /* | |
401 | * Scan `goal' inodes on the unused list for freeable ones. They are moved to | |
402 | * a temporary list and then are freed outside inode_lock by dispose_list(). | |
403 | * | |
404 | * Any inodes which are pinned purely because of attached pagecache have their | |
405 | * pagecache removed. We expect the final iput() on that inode to add it to | |
406 | * the front of the inode_unused list. So look for it there and if the | |
407 | * inode is still freeable, proceed. The right inode is found 99.9% of the | |
408 | * time in testing on a 4-way. | |
409 | * | |
410 | * If the inode has metadata buffers attached to mapping->private_list then | |
411 | * try to remove them. | |
412 | */ | |
413 | static void prune_icache(int nr_to_scan) | |
414 | { | |
415 | LIST_HEAD(freeable); | |
416 | int nr_pruned = 0; | |
417 | int nr_scanned; | |
418 | unsigned long reap = 0; | |
419 | ||
f24075bd | 420 | mutex_lock(&iprune_mutex); |
1da177e4 LT |
421 | spin_lock(&inode_lock); |
422 | for (nr_scanned = 0; nr_scanned < nr_to_scan; nr_scanned++) { | |
423 | struct inode *inode; | |
424 | ||
425 | if (list_empty(&inode_unused)) | |
426 | break; | |
427 | ||
428 | inode = list_entry(inode_unused.prev, struct inode, i_list); | |
429 | ||
430 | if (inode->i_state || atomic_read(&inode->i_count)) { | |
431 | list_move(&inode->i_list, &inode_unused); | |
432 | continue; | |
433 | } | |
434 | if (inode_has_buffers(inode) || inode->i_data.nrpages) { | |
435 | __iget(inode); | |
436 | spin_unlock(&inode_lock); | |
437 | if (remove_inode_buffers(inode)) | |
438 | reap += invalidate_inode_pages(&inode->i_data); | |
439 | iput(inode); | |
440 | spin_lock(&inode_lock); | |
441 | ||
442 | if (inode != list_entry(inode_unused.next, | |
443 | struct inode, i_list)) | |
444 | continue; /* wrong inode or list_empty */ | |
445 | if (!can_unuse(inode)) | |
446 | continue; | |
447 | } | |
1da177e4 LT |
448 | list_move(&inode->i_list, &freeable); |
449 | inode->i_state |= I_FREEING; | |
450 | nr_pruned++; | |
451 | } | |
452 | inodes_stat.nr_unused -= nr_pruned; | |
f8891e5e CL |
453 | if (current_is_kswapd()) |
454 | __count_vm_events(KSWAPD_INODESTEAL, reap); | |
455 | else | |
456 | __count_vm_events(PGINODESTEAL, reap); | |
1da177e4 LT |
457 | spin_unlock(&inode_lock); |
458 | ||
459 | dispose_list(&freeable); | |
f24075bd | 460 | mutex_unlock(&iprune_mutex); |
1da177e4 LT |
461 | } |
462 | ||
463 | /* | |
464 | * shrink_icache_memory() will attempt to reclaim some unused inodes. Here, | |
465 | * "unused" means that no dentries are referring to the inodes: the files are | |
466 | * not open and the dcache references to those inodes have already been | |
467 | * reclaimed. | |
468 | * | |
469 | * This function is passed the number of inodes to scan, and it returns the | |
470 | * total number of remaining possibly-reclaimable inodes. | |
471 | */ | |
27496a8c | 472 | static int shrink_icache_memory(int nr, gfp_t gfp_mask) |
1da177e4 LT |
473 | { |
474 | if (nr) { | |
475 | /* | |
476 | * Nasty deadlock avoidance. We may hold various FS locks, | |
477 | * and we don't want to recurse into the FS that called us | |
478 | * in clear_inode() and friends.. | |
479 | */ | |
480 | if (!(gfp_mask & __GFP_FS)) | |
481 | return -1; | |
482 | prune_icache(nr); | |
483 | } | |
484 | return (inodes_stat.nr_unused / 100) * sysctl_vfs_cache_pressure; | |
485 | } | |
486 | ||
487 | static void __wait_on_freeing_inode(struct inode *inode); | |
488 | /* | |
489 | * Called with the inode lock held. | |
490 | * NOTE: we are not increasing the inode-refcount, you must call __iget() | |
491 | * by hand after calling find_inode now! This simplifies iunique and won't | |
492 | * add any additional branch in the common code. | |
493 | */ | |
494 | static struct inode * find_inode(struct super_block * sb, struct hlist_head *head, int (*test)(struct inode *, void *), void *data) | |
495 | { | |
496 | struct hlist_node *node; | |
497 | struct inode * inode = NULL; | |
498 | ||
499 | repeat: | |
500 | hlist_for_each (node, head) { | |
501 | inode = hlist_entry(node, struct inode, i_hash); | |
502 | if (inode->i_sb != sb) | |
503 | continue; | |
504 | if (!test(inode, data)) | |
505 | continue; | |
991114c6 | 506 | if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE)) { |
1da177e4 LT |
507 | __wait_on_freeing_inode(inode); |
508 | goto repeat; | |
509 | } | |
510 | break; | |
511 | } | |
512 | return node ? inode : NULL; | |
513 | } | |
514 | ||
515 | /* | |
516 | * find_inode_fast is the fast path version of find_inode, see the comment at | |
517 | * iget_locked for details. | |
518 | */ | |
519 | static struct inode * find_inode_fast(struct super_block * sb, struct hlist_head *head, unsigned long ino) | |
520 | { | |
521 | struct hlist_node *node; | |
522 | struct inode * inode = NULL; | |
523 | ||
524 | repeat: | |
525 | hlist_for_each (node, head) { | |
526 | inode = hlist_entry(node, struct inode, i_hash); | |
527 | if (inode->i_ino != ino) | |
528 | continue; | |
529 | if (inode->i_sb != sb) | |
530 | continue; | |
991114c6 | 531 | if (inode->i_state & (I_FREEING|I_CLEAR|I_WILL_FREE)) { |
1da177e4 LT |
532 | __wait_on_freeing_inode(inode); |
533 | goto repeat; | |
534 | } | |
535 | break; | |
536 | } | |
537 | return node ? inode : NULL; | |
538 | } | |
539 | ||
540 | /** | |
541 | * new_inode - obtain an inode | |
542 | * @sb: superblock | |
543 | * | |
544 | * Allocates a new inode for given superblock. | |
545 | */ | |
546 | struct inode *new_inode(struct super_block *sb) | |
547 | { | |
548 | static unsigned long last_ino; | |
549 | struct inode * inode; | |
550 | ||
551 | spin_lock_prefetch(&inode_lock); | |
552 | ||
553 | inode = alloc_inode(sb); | |
554 | if (inode) { | |
555 | spin_lock(&inode_lock); | |
556 | inodes_stat.nr_inodes++; | |
557 | list_add(&inode->i_list, &inode_in_use); | |
558 | list_add(&inode->i_sb_list, &sb->s_inodes); | |
559 | inode->i_ino = ++last_ino; | |
560 | inode->i_state = 0; | |
561 | spin_unlock(&inode_lock); | |
562 | } | |
563 | return inode; | |
564 | } | |
565 | ||
566 | EXPORT_SYMBOL(new_inode); | |
567 | ||
568 | void unlock_new_inode(struct inode *inode) | |
569 | { | |
570 | /* | |
571 | * This is special! We do not need the spinlock | |
572 | * when clearing I_LOCK, because we're guaranteed | |
573 | * that nobody else tries to do anything about the | |
574 | * state of the inode when it is locked, as we | |
575 | * just created it (so there can be no old holders | |
576 | * that haven't tested I_LOCK). | |
577 | */ | |
578 | inode->i_state &= ~(I_LOCK|I_NEW); | |
579 | wake_up_inode(inode); | |
580 | } | |
581 | ||
582 | EXPORT_SYMBOL(unlock_new_inode); | |
583 | ||
584 | /* | |
585 | * This is called without the inode lock held.. Be careful. | |
586 | * | |
587 | * We no longer cache the sb_flags in i_flags - see fs.h | |
588 | * -- rmk@arm.uk.linux.org | |
589 | */ | |
590 | static struct inode * get_new_inode(struct super_block *sb, struct hlist_head *head, int (*test)(struct inode *, void *), int (*set)(struct inode *, void *), void *data) | |
591 | { | |
592 | struct inode * inode; | |
593 | ||
594 | inode = alloc_inode(sb); | |
595 | if (inode) { | |
596 | struct inode * old; | |
597 | ||
598 | spin_lock(&inode_lock); | |
599 | /* We released the lock, so.. */ | |
600 | old = find_inode(sb, head, test, data); | |
601 | if (!old) { | |
602 | if (set(inode, data)) | |
603 | goto set_failed; | |
604 | ||
605 | inodes_stat.nr_inodes++; | |
606 | list_add(&inode->i_list, &inode_in_use); | |
607 | list_add(&inode->i_sb_list, &sb->s_inodes); | |
608 | hlist_add_head(&inode->i_hash, head); | |
609 | inode->i_state = I_LOCK|I_NEW; | |
610 | spin_unlock(&inode_lock); | |
611 | ||
612 | /* Return the locked inode with I_NEW set, the | |
613 | * caller is responsible for filling in the contents | |
614 | */ | |
615 | return inode; | |
616 | } | |
617 | ||
618 | /* | |
619 | * Uhhuh, somebody else created the same inode under | |
620 | * us. Use the old inode instead of the one we just | |
621 | * allocated. | |
622 | */ | |
623 | __iget(old); | |
624 | spin_unlock(&inode_lock); | |
625 | destroy_inode(inode); | |
626 | inode = old; | |
627 | wait_on_inode(inode); | |
628 | } | |
629 | return inode; | |
630 | ||
631 | set_failed: | |
632 | spin_unlock(&inode_lock); | |
633 | destroy_inode(inode); | |
634 | return NULL; | |
635 | } | |
636 | ||
637 | /* | |
638 | * get_new_inode_fast is the fast path version of get_new_inode, see the | |
639 | * comment at iget_locked for details. | |
640 | */ | |
641 | static struct inode * get_new_inode_fast(struct super_block *sb, struct hlist_head *head, unsigned long ino) | |
642 | { | |
643 | struct inode * inode; | |
644 | ||
645 | inode = alloc_inode(sb); | |
646 | if (inode) { | |
647 | struct inode * old; | |
648 | ||
649 | spin_lock(&inode_lock); | |
650 | /* We released the lock, so.. */ | |
651 | old = find_inode_fast(sb, head, ino); | |
652 | if (!old) { | |
653 | inode->i_ino = ino; | |
654 | inodes_stat.nr_inodes++; | |
655 | list_add(&inode->i_list, &inode_in_use); | |
656 | list_add(&inode->i_sb_list, &sb->s_inodes); | |
657 | hlist_add_head(&inode->i_hash, head); | |
658 | inode->i_state = I_LOCK|I_NEW; | |
659 | spin_unlock(&inode_lock); | |
660 | ||
661 | /* Return the locked inode with I_NEW set, the | |
662 | * caller is responsible for filling in the contents | |
663 | */ | |
664 | return inode; | |
665 | } | |
666 | ||
667 | /* | |
668 | * Uhhuh, somebody else created the same inode under | |
669 | * us. Use the old inode instead of the one we just | |
670 | * allocated. | |
671 | */ | |
672 | __iget(old); | |
673 | spin_unlock(&inode_lock); | |
674 | destroy_inode(inode); | |
675 | inode = old; | |
676 | wait_on_inode(inode); | |
677 | } | |
678 | return inode; | |
679 | } | |
680 | ||
681 | static inline unsigned long hash(struct super_block *sb, unsigned long hashval) | |
682 | { | |
683 | unsigned long tmp; | |
684 | ||
685 | tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) / | |
686 | L1_CACHE_BYTES; | |
687 | tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> I_HASHBITS); | |
688 | return tmp & I_HASHMASK; | |
689 | } | |
690 | ||
691 | /** | |
692 | * iunique - get a unique inode number | |
693 | * @sb: superblock | |
694 | * @max_reserved: highest reserved inode number | |
695 | * | |
696 | * Obtain an inode number that is unique on the system for a given | |
697 | * superblock. This is used by file systems that have no natural | |
698 | * permanent inode numbering system. An inode number is returned that | |
699 | * is higher than the reserved limit but unique. | |
700 | * | |
701 | * BUGS: | |
702 | * With a large number of inodes live on the file system this function | |
703 | * currently becomes quite slow. | |
704 | */ | |
705 | ino_t iunique(struct super_block *sb, ino_t max_reserved) | |
706 | { | |
707 | static ino_t counter; | |
708 | struct inode *inode; | |
709 | struct hlist_head * head; | |
710 | ino_t res; | |
711 | spin_lock(&inode_lock); | |
712 | retry: | |
713 | if (counter > max_reserved) { | |
714 | head = inode_hashtable + hash(sb,counter); | |
715 | res = counter++; | |
716 | inode = find_inode_fast(sb, head, res); | |
717 | if (!inode) { | |
718 | spin_unlock(&inode_lock); | |
719 | return res; | |
720 | } | |
721 | } else { | |
722 | counter = max_reserved + 1; | |
723 | } | |
724 | goto retry; | |
725 | ||
726 | } | |
727 | ||
728 | EXPORT_SYMBOL(iunique); | |
729 | ||
730 | struct inode *igrab(struct inode *inode) | |
731 | { | |
732 | spin_lock(&inode_lock); | |
991114c6 | 733 | if (!(inode->i_state & (I_FREEING|I_WILL_FREE))) |
1da177e4 LT |
734 | __iget(inode); |
735 | else | |
736 | /* | |
737 | * Handle the case where s_op->clear_inode is not been | |
738 | * called yet, and somebody is calling igrab | |
739 | * while the inode is getting freed. | |
740 | */ | |
741 | inode = NULL; | |
742 | spin_unlock(&inode_lock); | |
743 | return inode; | |
744 | } | |
745 | ||
746 | EXPORT_SYMBOL(igrab); | |
747 | ||
748 | /** | |
749 | * ifind - internal function, you want ilookup5() or iget5(). | |
750 | * @sb: super block of file system to search | |
751 | * @head: the head of the list to search | |
752 | * @test: callback used for comparisons between inodes | |
753 | * @data: opaque data pointer to pass to @test | |
88bd5121 | 754 | * @wait: if true wait for the inode to be unlocked, if false do not |
1da177e4 LT |
755 | * |
756 | * ifind() searches for the inode specified by @data in the inode | |
757 | * cache. This is a generalized version of ifind_fast() for file systems where | |
758 | * the inode number is not sufficient for unique identification of an inode. | |
759 | * | |
760 | * If the inode is in the cache, the inode is returned with an incremented | |
761 | * reference count. | |
762 | * | |
763 | * Otherwise NULL is returned. | |
764 | * | |
765 | * Note, @test is called with the inode_lock held, so can't sleep. | |
766 | */ | |
5d2bea45 | 767 | static struct inode *ifind(struct super_block *sb, |
1da177e4 | 768 | struct hlist_head *head, int (*test)(struct inode *, void *), |
88bd5121 | 769 | void *data, const int wait) |
1da177e4 LT |
770 | { |
771 | struct inode *inode; | |
772 | ||
773 | spin_lock(&inode_lock); | |
774 | inode = find_inode(sb, head, test, data); | |
775 | if (inode) { | |
776 | __iget(inode); | |
777 | spin_unlock(&inode_lock); | |
88bd5121 AA |
778 | if (likely(wait)) |
779 | wait_on_inode(inode); | |
1da177e4 LT |
780 | return inode; |
781 | } | |
782 | spin_unlock(&inode_lock); | |
783 | return NULL; | |
784 | } | |
785 | ||
786 | /** | |
787 | * ifind_fast - internal function, you want ilookup() or iget(). | |
788 | * @sb: super block of file system to search | |
789 | * @head: head of the list to search | |
790 | * @ino: inode number to search for | |
791 | * | |
792 | * ifind_fast() searches for the inode @ino in the inode cache. This is for | |
793 | * file systems where the inode number is sufficient for unique identification | |
794 | * of an inode. | |
795 | * | |
796 | * If the inode is in the cache, the inode is returned with an incremented | |
797 | * reference count. | |
798 | * | |
799 | * Otherwise NULL is returned. | |
800 | */ | |
5d2bea45 | 801 | static struct inode *ifind_fast(struct super_block *sb, |
1da177e4 LT |
802 | struct hlist_head *head, unsigned long ino) |
803 | { | |
804 | struct inode *inode; | |
805 | ||
806 | spin_lock(&inode_lock); | |
807 | inode = find_inode_fast(sb, head, ino); | |
808 | if (inode) { | |
809 | __iget(inode); | |
810 | spin_unlock(&inode_lock); | |
811 | wait_on_inode(inode); | |
812 | return inode; | |
813 | } | |
814 | spin_unlock(&inode_lock); | |
815 | return NULL; | |
816 | } | |
817 | ||
818 | /** | |
88bd5121 | 819 | * ilookup5_nowait - search for an inode in the inode cache |
1da177e4 LT |
820 | * @sb: super block of file system to search |
821 | * @hashval: hash value (usually inode number) to search for | |
822 | * @test: callback used for comparisons between inodes | |
823 | * @data: opaque data pointer to pass to @test | |
824 | * | |
825 | * ilookup5() uses ifind() to search for the inode specified by @hashval and | |
826 | * @data in the inode cache. This is a generalized version of ilookup() for | |
827 | * file systems where the inode number is not sufficient for unique | |
828 | * identification of an inode. | |
829 | * | |
830 | * If the inode is in the cache, the inode is returned with an incremented | |
88bd5121 AA |
831 | * reference count. Note, the inode lock is not waited upon so you have to be |
832 | * very careful what you do with the returned inode. You probably should be | |
833 | * using ilookup5() instead. | |
834 | * | |
835 | * Otherwise NULL is returned. | |
836 | * | |
837 | * Note, @test is called with the inode_lock held, so can't sleep. | |
838 | */ | |
839 | struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval, | |
840 | int (*test)(struct inode *, void *), void *data) | |
841 | { | |
842 | struct hlist_head *head = inode_hashtable + hash(sb, hashval); | |
843 | ||
844 | return ifind(sb, head, test, data, 0); | |
845 | } | |
846 | ||
847 | EXPORT_SYMBOL(ilookup5_nowait); | |
848 | ||
849 | /** | |
850 | * ilookup5 - search for an inode in the inode cache | |
851 | * @sb: super block of file system to search | |
852 | * @hashval: hash value (usually inode number) to search for | |
853 | * @test: callback used for comparisons between inodes | |
854 | * @data: opaque data pointer to pass to @test | |
855 | * | |
856 | * ilookup5() uses ifind() to search for the inode specified by @hashval and | |
857 | * @data in the inode cache. This is a generalized version of ilookup() for | |
858 | * file systems where the inode number is not sufficient for unique | |
859 | * identification of an inode. | |
860 | * | |
861 | * If the inode is in the cache, the inode lock is waited upon and the inode is | |
862 | * returned with an incremented reference count. | |
1da177e4 LT |
863 | * |
864 | * Otherwise NULL is returned. | |
865 | * | |
866 | * Note, @test is called with the inode_lock held, so can't sleep. | |
867 | */ | |
868 | struct inode *ilookup5(struct super_block *sb, unsigned long hashval, | |
869 | int (*test)(struct inode *, void *), void *data) | |
870 | { | |
871 | struct hlist_head *head = inode_hashtable + hash(sb, hashval); | |
872 | ||
88bd5121 | 873 | return ifind(sb, head, test, data, 1); |
1da177e4 LT |
874 | } |
875 | ||
876 | EXPORT_SYMBOL(ilookup5); | |
877 | ||
878 | /** | |
879 | * ilookup - search for an inode in the inode cache | |
880 | * @sb: super block of file system to search | |
881 | * @ino: inode number to search for | |
882 | * | |
883 | * ilookup() uses ifind_fast() to search for the inode @ino in the inode cache. | |
884 | * This is for file systems where the inode number is sufficient for unique | |
885 | * identification of an inode. | |
886 | * | |
887 | * If the inode is in the cache, the inode is returned with an incremented | |
888 | * reference count. | |
889 | * | |
890 | * Otherwise NULL is returned. | |
891 | */ | |
892 | struct inode *ilookup(struct super_block *sb, unsigned long ino) | |
893 | { | |
894 | struct hlist_head *head = inode_hashtable + hash(sb, ino); | |
895 | ||
896 | return ifind_fast(sb, head, ino); | |
897 | } | |
898 | ||
899 | EXPORT_SYMBOL(ilookup); | |
900 | ||
901 | /** | |
902 | * iget5_locked - obtain an inode from a mounted file system | |
903 | * @sb: super block of file system | |
904 | * @hashval: hash value (usually inode number) to get | |
905 | * @test: callback used for comparisons between inodes | |
906 | * @set: callback used to initialize a new struct inode | |
907 | * @data: opaque data pointer to pass to @test and @set | |
908 | * | |
909 | * This is iget() without the read_inode() portion of get_new_inode(). | |
910 | * | |
911 | * iget5_locked() uses ifind() to search for the inode specified by @hashval | |
912 | * and @data in the inode cache and if present it is returned with an increased | |
913 | * reference count. This is a generalized version of iget_locked() for file | |
914 | * systems where the inode number is not sufficient for unique identification | |
915 | * of an inode. | |
916 | * | |
917 | * If the inode is not in cache, get_new_inode() is called to allocate a new | |
918 | * inode and this is returned locked, hashed, and with the I_NEW flag set. The | |
919 | * file system gets to fill it in before unlocking it via unlock_new_inode(). | |
920 | * | |
921 | * Note both @test and @set are called with the inode_lock held, so can't sleep. | |
922 | */ | |
923 | struct inode *iget5_locked(struct super_block *sb, unsigned long hashval, | |
924 | int (*test)(struct inode *, void *), | |
925 | int (*set)(struct inode *, void *), void *data) | |
926 | { | |
927 | struct hlist_head *head = inode_hashtable + hash(sb, hashval); | |
928 | struct inode *inode; | |
929 | ||
88bd5121 | 930 | inode = ifind(sb, head, test, data, 1); |
1da177e4 LT |
931 | if (inode) |
932 | return inode; | |
933 | /* | |
934 | * get_new_inode() will do the right thing, re-trying the search | |
935 | * in case it had to block at any point. | |
936 | */ | |
937 | return get_new_inode(sb, head, test, set, data); | |
938 | } | |
939 | ||
940 | EXPORT_SYMBOL(iget5_locked); | |
941 | ||
942 | /** | |
943 | * iget_locked - obtain an inode from a mounted file system | |
944 | * @sb: super block of file system | |
945 | * @ino: inode number to get | |
946 | * | |
947 | * This is iget() without the read_inode() portion of get_new_inode_fast(). | |
948 | * | |
949 | * iget_locked() uses ifind_fast() to search for the inode specified by @ino in | |
950 | * the inode cache and if present it is returned with an increased reference | |
951 | * count. This is for file systems where the inode number is sufficient for | |
952 | * unique identification of an inode. | |
953 | * | |
954 | * If the inode is not in cache, get_new_inode_fast() is called to allocate a | |
955 | * new inode and this is returned locked, hashed, and with the I_NEW flag set. | |
956 | * The file system gets to fill it in before unlocking it via | |
957 | * unlock_new_inode(). | |
958 | */ | |
959 | struct inode *iget_locked(struct super_block *sb, unsigned long ino) | |
960 | { | |
961 | struct hlist_head *head = inode_hashtable + hash(sb, ino); | |
962 | struct inode *inode; | |
963 | ||
964 | inode = ifind_fast(sb, head, ino); | |
965 | if (inode) | |
966 | return inode; | |
967 | /* | |
968 | * get_new_inode_fast() will do the right thing, re-trying the search | |
969 | * in case it had to block at any point. | |
970 | */ | |
971 | return get_new_inode_fast(sb, head, ino); | |
972 | } | |
973 | ||
974 | EXPORT_SYMBOL(iget_locked); | |
975 | ||
976 | /** | |
977 | * __insert_inode_hash - hash an inode | |
978 | * @inode: unhashed inode | |
979 | * @hashval: unsigned long value used to locate this object in the | |
980 | * inode_hashtable. | |
981 | * | |
982 | * Add an inode to the inode hash for this superblock. | |
983 | */ | |
984 | void __insert_inode_hash(struct inode *inode, unsigned long hashval) | |
985 | { | |
986 | struct hlist_head *head = inode_hashtable + hash(inode->i_sb, hashval); | |
987 | spin_lock(&inode_lock); | |
988 | hlist_add_head(&inode->i_hash, head); | |
989 | spin_unlock(&inode_lock); | |
990 | } | |
991 | ||
992 | EXPORT_SYMBOL(__insert_inode_hash); | |
993 | ||
994 | /** | |
995 | * remove_inode_hash - remove an inode from the hash | |
996 | * @inode: inode to unhash | |
997 | * | |
998 | * Remove an inode from the superblock. | |
999 | */ | |
1000 | void remove_inode_hash(struct inode *inode) | |
1001 | { | |
1002 | spin_lock(&inode_lock); | |
1003 | hlist_del_init(&inode->i_hash); | |
1004 | spin_unlock(&inode_lock); | |
1005 | } | |
1006 | ||
1007 | EXPORT_SYMBOL(remove_inode_hash); | |
1008 | ||
1009 | /* | |
1010 | * Tell the filesystem that this inode is no longer of any interest and should | |
1011 | * be completely destroyed. | |
1012 | * | |
1013 | * We leave the inode in the inode hash table until *after* the filesystem's | |
1014 | * ->delete_inode completes. This ensures that an iget (such as nfsd might | |
1015 | * instigate) will always find up-to-date information either in the hash or on | |
1016 | * disk. | |
1017 | * | |
1018 | * I_FREEING is set so that no-one will take a new reference to the inode while | |
1019 | * it is being deleted. | |
1020 | */ | |
1021 | void generic_delete_inode(struct inode *inode) | |
1022 | { | |
1023 | struct super_operations *op = inode->i_sb->s_op; | |
1024 | ||
1025 | list_del_init(&inode->i_list); | |
1026 | list_del_init(&inode->i_sb_list); | |
1027 | inode->i_state|=I_FREEING; | |
1028 | inodes_stat.nr_inodes--; | |
1029 | spin_unlock(&inode_lock); | |
1030 | ||
1da177e4 LT |
1031 | security_inode_delete(inode); |
1032 | ||
1033 | if (op->delete_inode) { | |
1034 | void (*delete)(struct inode *) = op->delete_inode; | |
1035 | if (!is_bad_inode(inode)) | |
1036 | DQUOT_INIT(inode); | |
e85b5652 MF |
1037 | /* Filesystems implementing their own |
1038 | * s_op->delete_inode are required to call | |
1039 | * truncate_inode_pages and clear_inode() | |
1040 | * internally */ | |
1da177e4 | 1041 | delete(inode); |
e85b5652 MF |
1042 | } else { |
1043 | truncate_inode_pages(&inode->i_data, 0); | |
1da177e4 | 1044 | clear_inode(inode); |
e85b5652 | 1045 | } |
1da177e4 LT |
1046 | spin_lock(&inode_lock); |
1047 | hlist_del_init(&inode->i_hash); | |
1048 | spin_unlock(&inode_lock); | |
1049 | wake_up_inode(inode); | |
b7542f8c | 1050 | BUG_ON(inode->i_state != I_CLEAR); |
1da177e4 LT |
1051 | destroy_inode(inode); |
1052 | } | |
1053 | ||
1054 | EXPORT_SYMBOL(generic_delete_inode); | |
1055 | ||
1056 | static void generic_forget_inode(struct inode *inode) | |
1057 | { | |
1058 | struct super_block *sb = inode->i_sb; | |
1059 | ||
1060 | if (!hlist_unhashed(&inode->i_hash)) { | |
1061 | if (!(inode->i_state & (I_DIRTY|I_LOCK))) | |
1062 | list_move(&inode->i_list, &inode_unused); | |
1063 | inodes_stat.nr_unused++; | |
991114c6 AV |
1064 | if (!sb || (sb->s_flags & MS_ACTIVE)) { |
1065 | spin_unlock(&inode_lock); | |
1da177e4 | 1066 | return; |
991114c6 AV |
1067 | } |
1068 | inode->i_state |= I_WILL_FREE; | |
1069 | spin_unlock(&inode_lock); | |
1da177e4 LT |
1070 | write_inode_now(inode, 1); |
1071 | spin_lock(&inode_lock); | |
991114c6 | 1072 | inode->i_state &= ~I_WILL_FREE; |
1da177e4 LT |
1073 | inodes_stat.nr_unused--; |
1074 | hlist_del_init(&inode->i_hash); | |
1075 | } | |
1076 | list_del_init(&inode->i_list); | |
1077 | list_del_init(&inode->i_sb_list); | |
991114c6 | 1078 | inode->i_state |= I_FREEING; |
1da177e4 LT |
1079 | inodes_stat.nr_inodes--; |
1080 | spin_unlock(&inode_lock); | |
1081 | if (inode->i_data.nrpages) | |
1082 | truncate_inode_pages(&inode->i_data, 0); | |
1083 | clear_inode(inode); | |
7f04c26d | 1084 | wake_up_inode(inode); |
1da177e4 LT |
1085 | destroy_inode(inode); |
1086 | } | |
1087 | ||
1088 | /* | |
1089 | * Normal UNIX filesystem behaviour: delete the | |
1090 | * inode when the usage count drops to zero, and | |
1091 | * i_nlink is zero. | |
1092 | */ | |
cb2c0233 | 1093 | void generic_drop_inode(struct inode *inode) |
1da177e4 LT |
1094 | { |
1095 | if (!inode->i_nlink) | |
1096 | generic_delete_inode(inode); | |
1097 | else | |
1098 | generic_forget_inode(inode); | |
1099 | } | |
1100 | ||
cb2c0233 MF |
1101 | EXPORT_SYMBOL_GPL(generic_drop_inode); |
1102 | ||
1da177e4 LT |
1103 | /* |
1104 | * Called when we're dropping the last reference | |
1105 | * to an inode. | |
1106 | * | |
1107 | * Call the FS "drop()" function, defaulting to | |
1108 | * the legacy UNIX filesystem behaviour.. | |
1109 | * | |
1110 | * NOTE! NOTE! NOTE! We're called with the inode lock | |
1111 | * held, and the drop function is supposed to release | |
1112 | * the lock! | |
1113 | */ | |
1114 | static inline void iput_final(struct inode *inode) | |
1115 | { | |
1116 | struct super_operations *op = inode->i_sb->s_op; | |
1117 | void (*drop)(struct inode *) = generic_drop_inode; | |
1118 | ||
1119 | if (op && op->drop_inode) | |
1120 | drop = op->drop_inode; | |
1121 | drop(inode); | |
1122 | } | |
1123 | ||
1124 | /** | |
1125 | * iput - put an inode | |
1126 | * @inode: inode to put | |
1127 | * | |
1128 | * Puts an inode, dropping its usage count. If the inode use count hits | |
1129 | * zero, the inode is then freed and may also be destroyed. | |
1130 | * | |
1131 | * Consequently, iput() can sleep. | |
1132 | */ | |
1133 | void iput(struct inode *inode) | |
1134 | { | |
1135 | if (inode) { | |
1136 | struct super_operations *op = inode->i_sb->s_op; | |
1137 | ||
1138 | BUG_ON(inode->i_state == I_CLEAR); | |
1139 | ||
1140 | if (op && op->put_inode) | |
1141 | op->put_inode(inode); | |
1142 | ||
1143 | if (atomic_dec_and_lock(&inode->i_count, &inode_lock)) | |
1144 | iput_final(inode); | |
1145 | } | |
1146 | } | |
1147 | ||
1148 | EXPORT_SYMBOL(iput); | |
1149 | ||
1150 | /** | |
1151 | * bmap - find a block number in a file | |
1152 | * @inode: inode of file | |
1153 | * @block: block to find | |
1154 | * | |
1155 | * Returns the block number on the device holding the inode that | |
1156 | * is the disk block number for the block of the file requested. | |
1157 | * That is, asked for block 4 of inode 1 the function will return the | |
1158 | * disk block relative to the disk start that holds that block of the | |
1159 | * file. | |
1160 | */ | |
1161 | sector_t bmap(struct inode * inode, sector_t block) | |
1162 | { | |
1163 | sector_t res = 0; | |
1164 | if (inode->i_mapping->a_ops->bmap) | |
1165 | res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block); | |
1166 | return res; | |
1167 | } | |
1168 | ||
1169 | EXPORT_SYMBOL(bmap); | |
1170 | ||
1171 | /** | |
869243a0 CH |
1172 | * touch_atime - update the access time |
1173 | * @mnt: mount the inode is accessed on | |
7045f37b | 1174 | * @dentry: dentry accessed |
1da177e4 LT |
1175 | * |
1176 | * Update the accessed time on an inode and mark it for writeback. | |
1177 | * This function automatically handles read only file systems and media, | |
1178 | * as well as the "noatime" flag and inode specific "noatime" markers. | |
1179 | */ | |
869243a0 | 1180 | void touch_atime(struct vfsmount *mnt, struct dentry *dentry) |
1da177e4 | 1181 | { |
869243a0 | 1182 | struct inode *inode = dentry->d_inode; |
1da177e4 LT |
1183 | struct timespec now; |
1184 | ||
fc33a7bb | 1185 | if (IS_RDONLY(inode)) |
1da177e4 | 1186 | return; |
fc33a7bb CH |
1187 | |
1188 | if ((inode->i_flags & S_NOATIME) || | |
1189 | (inode->i_sb->s_flags & MS_NOATIME) || | |
1190 | ((inode->i_sb->s_flags & MS_NODIRATIME) && S_ISDIR(inode->i_mode))) | |
1da177e4 | 1191 | return; |
fc33a7bb CH |
1192 | |
1193 | /* | |
1194 | * We may have a NULL vfsmount when coming from NFSD | |
1195 | */ | |
1196 | if (mnt && | |
1197 | ((mnt->mnt_flags & MNT_NOATIME) || | |
1198 | ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode)))) | |
1da177e4 LT |
1199 | return; |
1200 | ||
1201 | now = current_fs_time(inode->i_sb); | |
1202 | if (!timespec_equal(&inode->i_atime, &now)) { | |
1203 | inode->i_atime = now; | |
1204 | mark_inode_dirty_sync(inode); | |
1da177e4 LT |
1205 | } |
1206 | } | |
1207 | ||
869243a0 | 1208 | EXPORT_SYMBOL(touch_atime); |
1da177e4 LT |
1209 | |
1210 | /** | |
870f4817 CH |
1211 | * file_update_time - update mtime and ctime time |
1212 | * @file: file accessed | |
1da177e4 | 1213 | * |
870f4817 CH |
1214 | * Update the mtime and ctime members of an inode and mark the inode |
1215 | * for writeback. Note that this function is meant exclusively for | |
1216 | * usage in the file write path of filesystems, and filesystems may | |
1217 | * choose to explicitly ignore update via this function with the | |
1218 | * S_NOCTIME inode flag, e.g. for network filesystem where these | |
1219 | * timestamps are handled by the server. | |
1da177e4 LT |
1220 | */ |
1221 | ||
870f4817 | 1222 | void file_update_time(struct file *file) |
1da177e4 | 1223 | { |
870f4817 | 1224 | struct inode *inode = file->f_dentry->d_inode; |
1da177e4 LT |
1225 | struct timespec now; |
1226 | int sync_it = 0; | |
1227 | ||
1228 | if (IS_NOCMTIME(inode)) | |
1229 | return; | |
1230 | if (IS_RDONLY(inode)) | |
1231 | return; | |
1232 | ||
1233 | now = current_fs_time(inode->i_sb); | |
1234 | if (!timespec_equal(&inode->i_mtime, &now)) | |
1235 | sync_it = 1; | |
1236 | inode->i_mtime = now; | |
1237 | ||
870f4817 CH |
1238 | if (!timespec_equal(&inode->i_ctime, &now)) |
1239 | sync_it = 1; | |
1240 | inode->i_ctime = now; | |
1241 | ||
1da177e4 LT |
1242 | if (sync_it) |
1243 | mark_inode_dirty_sync(inode); | |
1244 | } | |
1245 | ||
870f4817 | 1246 | EXPORT_SYMBOL(file_update_time); |
1da177e4 LT |
1247 | |
1248 | int inode_needs_sync(struct inode *inode) | |
1249 | { | |
1250 | if (IS_SYNC(inode)) | |
1251 | return 1; | |
1252 | if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode)) | |
1253 | return 1; | |
1254 | return 0; | |
1255 | } | |
1256 | ||
1257 | EXPORT_SYMBOL(inode_needs_sync); | |
1258 | ||
1259 | /* | |
1260 | * Quota functions that want to walk the inode lists.. | |
1261 | */ | |
1262 | #ifdef CONFIG_QUOTA | |
1263 | ||
1264 | /* Function back in dquot.c */ | |
1265 | int remove_inode_dquot_ref(struct inode *, int, struct list_head *); | |
1266 | ||
1267 | void remove_dquot_ref(struct super_block *sb, int type, | |
1268 | struct list_head *tofree_head) | |
1269 | { | |
1270 | struct inode *inode; | |
1271 | ||
1272 | if (!sb->dq_op) | |
1273 | return; /* nothing to do */ | |
1274 | spin_lock(&inode_lock); /* This lock is for inodes code */ | |
1275 | ||
1276 | /* | |
1277 | * We don't have to lock against quota code - test IS_QUOTAINIT is | |
1278 | * just for speedup... | |
1279 | */ | |
1280 | list_for_each_entry(inode, &sb->s_inodes, i_sb_list) | |
1281 | if (!IS_NOQUOTA(inode)) | |
1282 | remove_inode_dquot_ref(inode, type, tofree_head); | |
1283 | ||
1284 | spin_unlock(&inode_lock); | |
1285 | } | |
1286 | ||
1287 | #endif | |
1288 | ||
1289 | int inode_wait(void *word) | |
1290 | { | |
1291 | schedule(); | |
1292 | return 0; | |
1293 | } | |
1294 | ||
1295 | /* | |
168a9fd6 MS |
1296 | * If we try to find an inode in the inode hash while it is being |
1297 | * deleted, we have to wait until the filesystem completes its | |
1298 | * deletion before reporting that it isn't found. This function waits | |
1299 | * until the deletion _might_ have completed. Callers are responsible | |
1300 | * to recheck inode state. | |
1301 | * | |
1302 | * It doesn't matter if I_LOCK is not set initially, a call to | |
1303 | * wake_up_inode() after removing from the hash list will DTRT. | |
1304 | * | |
1da177e4 LT |
1305 | * This is called with inode_lock held. |
1306 | */ | |
1307 | static void __wait_on_freeing_inode(struct inode *inode) | |
1308 | { | |
1309 | wait_queue_head_t *wq; | |
1310 | DEFINE_WAIT_BIT(wait, &inode->i_state, __I_LOCK); | |
1da177e4 LT |
1311 | wq = bit_waitqueue(&inode->i_state, __I_LOCK); |
1312 | prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE); | |
1313 | spin_unlock(&inode_lock); | |
1314 | schedule(); | |
1315 | finish_wait(wq, &wait.wait); | |
1316 | spin_lock(&inode_lock); | |
1317 | } | |
1318 | ||
1319 | void wake_up_inode(struct inode *inode) | |
1320 | { | |
1321 | /* | |
1322 | * Prevent speculative execution through spin_unlock(&inode_lock); | |
1323 | */ | |
1324 | smp_mb(); | |
1325 | wake_up_bit(&inode->i_state, __I_LOCK); | |
1326 | } | |
1327 | ||
1328 | static __initdata unsigned long ihash_entries; | |
1329 | static int __init set_ihash_entries(char *str) | |
1330 | { | |
1331 | if (!str) | |
1332 | return 0; | |
1333 | ihash_entries = simple_strtoul(str, &str, 0); | |
1334 | return 1; | |
1335 | } | |
1336 | __setup("ihash_entries=", set_ihash_entries); | |
1337 | ||
1338 | /* | |
1339 | * Initialize the waitqueues and inode hash table. | |
1340 | */ | |
1341 | void __init inode_init_early(void) | |
1342 | { | |
1343 | int loop; | |
1344 | ||
1345 | /* If hashes are distributed across NUMA nodes, defer | |
1346 | * hash allocation until vmalloc space is available. | |
1347 | */ | |
1348 | if (hashdist) | |
1349 | return; | |
1350 | ||
1351 | inode_hashtable = | |
1352 | alloc_large_system_hash("Inode-cache", | |
1353 | sizeof(struct hlist_head), | |
1354 | ihash_entries, | |
1355 | 14, | |
1356 | HASH_EARLY, | |
1357 | &i_hash_shift, | |
1358 | &i_hash_mask, | |
1359 | 0); | |
1360 | ||
1361 | for (loop = 0; loop < (1 << i_hash_shift); loop++) | |
1362 | INIT_HLIST_HEAD(&inode_hashtable[loop]); | |
1363 | } | |
1364 | ||
1365 | void __init inode_init(unsigned long mempages) | |
1366 | { | |
1367 | int loop; | |
1368 | ||
1369 | /* inode slab cache */ | |
b0196009 PJ |
1370 | inode_cachep = kmem_cache_create("inode_cache", |
1371 | sizeof(struct inode), | |
1372 | 0, | |
1373 | (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC| | |
1374 | SLAB_MEM_SPREAD), | |
1375 | init_once, | |
1376 | NULL); | |
1da177e4 LT |
1377 | set_shrinker(DEFAULT_SEEKS, shrink_icache_memory); |
1378 | ||
1379 | /* Hash may have been set up in inode_init_early */ | |
1380 | if (!hashdist) | |
1381 | return; | |
1382 | ||
1383 | inode_hashtable = | |
1384 | alloc_large_system_hash("Inode-cache", | |
1385 | sizeof(struct hlist_head), | |
1386 | ihash_entries, | |
1387 | 14, | |
1388 | 0, | |
1389 | &i_hash_shift, | |
1390 | &i_hash_mask, | |
1391 | 0); | |
1392 | ||
1393 | for (loop = 0; loop < (1 << i_hash_shift); loop++) | |
1394 | INIT_HLIST_HEAD(&inode_hashtable[loop]); | |
1395 | } | |
1396 | ||
1397 | void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev) | |
1398 | { | |
1399 | inode->i_mode = mode; | |
1400 | if (S_ISCHR(mode)) { | |
1401 | inode->i_fop = &def_chr_fops; | |
1402 | inode->i_rdev = rdev; | |
1403 | } else if (S_ISBLK(mode)) { | |
1404 | inode->i_fop = &def_blk_fops; | |
1405 | inode->i_rdev = rdev; | |
1406 | } else if (S_ISFIFO(mode)) | |
1407 | inode->i_fop = &def_fifo_fops; | |
1408 | else if (S_ISSOCK(mode)) | |
1409 | inode->i_fop = &bad_sock_fops; | |
1410 | else | |
1411 | printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o)\n", | |
1412 | mode); | |
1413 | } | |
1414 | EXPORT_SYMBOL(init_special_inode); |