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