1
2
3
4
5
6
7#include <linux/config.h>
8#include <linux/fs.h>
9#include <linux/mm.h>
10#include <linux/dcache.h>
11#include <linux/init.h>
12#include <linux/quotaops.h>
13#include <linux/slab.h>
14#include <linux/writeback.h>
15#include <linux/module.h>
16#include <linux/backing-dev.h>
17#include <linux/wait.h>
18#include <linux/hash.h>
19#include <linux/swap.h>
20#include <linux/security.h>
21#include <linux/pagemap.h>
22#include <linux/cdev.h>
23
24
25
26
27
28
29
30
31
32
33#include <linux/buffer_head.h>
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54#define I_HASHBITS i_hash_shift
55#define I_HASHMASK i_hash_mask
56
57static unsigned int i_hash_mask;
58static unsigned int i_hash_shift;
59
60
61
62
63
64
65
66
67
68
69
70
71
72LIST_HEAD(inode_in_use);
73LIST_HEAD(inode_unused);
74static struct hlist_head *inode_hashtable;
75
76
77
78
79
80
81
82spinlock_t inode_lock = SPIN_LOCK_UNLOCKED;
83
84
85
86
87
88
89
90
91
92static DECLARE_MUTEX(iprune_sem);
93
94
95
96
97struct inodes_stat_t inodes_stat;
98
99static kmem_cache_t * inode_cachep;
100
101static struct inode *alloc_inode(struct super_block *sb)
102{
103 static struct address_space_operations empty_aops;
104 static struct inode_operations empty_iops;
105 static struct file_operations empty_fops;
106 struct inode *inode;
107
108 if (sb->s_op->alloc_inode)
109 inode = sb->s_op->alloc_inode(sb);
110 else
111 inode = (struct inode *) kmem_cache_alloc(inode_cachep, SLAB_KERNEL);
112
113 if (inode) {
114 struct address_space * const mapping = &inode->i_data;
115
116 inode->i_sb = sb;
117 inode->i_blkbits = sb->s_blocksize_bits;
118 inode->i_flags = 0;
119 atomic_set(&inode->i_count, 1);
120 inode->i_sock = 0;
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 memset(&inode->i_dquot, 0, sizeof(inode->i_dquot));
130 inode->i_pipe = NULL;
131 inode->i_bdev = NULL;
132 inode->i_cdev = NULL;
133 inode->i_rdev = 0;
134 inode->i_security = NULL;
135 if (security_inode_alloc(inode)) {
136 if (inode->i_sb->s_op->destroy_inode)
137 inode->i_sb->s_op->destroy_inode(inode);
138 else
139 kmem_cache_free(inode_cachep, (inode));
140 return NULL;
141 }
142
143 mapping->a_ops = &empty_aops;
144 mapping->host = inode;
145 mapping->flags = 0;
146 mapping_set_gfp_mask(mapping, GFP_HIGHUSER);
147 mapping->dirtied_when = 0;
148 mapping->assoc_mapping = NULL;
149 mapping->backing_dev_info = &default_backing_dev_info;
150 if (sb->s_bdev)
151 mapping->backing_dev_info = sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
152 memset(&inode->u, 0, sizeof(inode->u));
153 inode->i_mapping = mapping;
154 }
155 return inode;
156}
157
158void destroy_inode(struct inode *inode)
159{
160 if (inode_has_buffers(inode))
161 BUG();
162 security_inode_free(inode);
163 if (inode->i_sb->s_op->destroy_inode)
164 inode->i_sb->s_op->destroy_inode(inode);
165 else
166 kmem_cache_free(inode_cachep, (inode));
167}
168
169
170
171
172
173
174
175void inode_init_once(struct inode *inode)
176{
177 memset(inode, 0, sizeof(*inode));
178 INIT_HLIST_NODE(&inode->i_hash);
179 INIT_LIST_HEAD(&inode->i_data.clean_pages);
180 INIT_LIST_HEAD(&inode->i_data.dirty_pages);
181 INIT_LIST_HEAD(&inode->i_data.locked_pages);
182 INIT_LIST_HEAD(&inode->i_data.io_pages);
183 INIT_LIST_HEAD(&inode->i_dentry);
184 INIT_LIST_HEAD(&inode->i_devices);
185 sema_init(&inode->i_sem, 1);
186 INIT_RADIX_TREE(&inode->i_data.page_tree, GFP_ATOMIC);
187 spin_lock_init(&inode->i_data.page_lock);
188 init_MUTEX(&inode->i_data.i_shared_sem);
189 atomic_set(&inode->i_data.truncate_count, 0);
190 INIT_LIST_HEAD(&inode->i_data.private_list);
191 spin_lock_init(&inode->i_data.private_lock);
192 INIT_LIST_HEAD(&inode->i_data.i_mmap);
193 INIT_LIST_HEAD(&inode->i_data.i_mmap_shared);
194 spin_lock_init(&inode->i_lock);
195 i_size_ordered_init(inode);
196}
197
198EXPORT_SYMBOL(inode_init_once);
199
200static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
201{
202 struct inode * inode = (struct inode *) foo;
203
204 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
205 SLAB_CTOR_CONSTRUCTOR)
206 inode_init_once(inode);
207}
208
209
210
211
212void __iget(struct inode * inode)
213{
214 if (atomic_read(&inode->i_count)) {
215 atomic_inc(&inode->i_count);
216 return;
217 }
218 atomic_inc(&inode->i_count);
219 if (!(inode->i_state & (I_DIRTY|I_LOCK))) {
220 list_del(&inode->i_list);
221 list_add(&inode->i_list, &inode_in_use);
222 }
223 inodes_stat.nr_unused--;
224}
225
226
227
228
229
230
231
232
233
234void clear_inode(struct inode *inode)
235{
236 invalidate_inode_buffers(inode);
237
238 if (inode->i_data.nrpages)
239 BUG();
240 if (!(inode->i_state & I_FREEING))
241 BUG();
242 if (inode->i_state & I_CLEAR)
243 BUG();
244 wait_on_inode(inode);
245 DQUOT_DROP(inode);
246 if (inode->i_sb && inode->i_sb->s_op->clear_inode)
247 inode->i_sb->s_op->clear_inode(inode);
248 if (inode->i_bdev)
249 bd_forget(inode);
250 if (inode->i_cdev)
251 cd_forget(inode);
252 inode->i_state = I_CLEAR;
253}
254
255EXPORT_SYMBOL(clear_inode);
256
257
258
259
260
261
262
263
264static void dispose_list(struct list_head *head)
265{
266 int nr_disposed = 0;
267
268 while (!list_empty(head)) {
269 struct inode *inode;
270
271 inode = list_entry(head->next, struct inode, i_list);
272 list_del(&inode->i_list);
273
274 if (inode->i_data.nrpages)
275 truncate_inode_pages(&inode->i_data, 0);
276 clear_inode(inode);
277 destroy_inode(inode);
278 nr_disposed++;
279 }
280 spin_lock(&inode_lock);
281 inodes_stat.nr_inodes -= nr_disposed;
282 spin_unlock(&inode_lock);
283}
284
285
286
287
288static int invalidate_list(struct list_head *head, struct super_block * sb, struct list_head * dispose)
289{
290 struct list_head *next;
291 int busy = 0, count = 0;
292
293 next = head->next;
294 for (;;) {
295 struct list_head * tmp = next;
296 struct inode * inode;
297
298 next = next->next;
299 if (tmp == head)
300 break;
301 inode = list_entry(tmp, struct inode, i_list);
302 if (inode->i_sb != sb)
303 continue;
304 invalidate_inode_buffers(inode);
305 if (!atomic_read(&inode->i_count)) {
306 hlist_del_init(&inode->i_hash);
307 list_del(&inode->i_list);
308 list_add(&inode->i_list, dispose);
309 inode->i_state |= I_FREEING;
310 count++;
311 continue;
312 }
313 busy = 1;
314 }
315
316 inodes_stat.nr_unused -= count;
317 return busy;
318}
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336int invalidate_inodes(struct super_block * sb)
337{
338 int busy;
339 LIST_HEAD(throw_away);
340
341 down(&iprune_sem);
342 spin_lock(&inode_lock);
343 busy = invalidate_list(&inode_in_use, sb, &throw_away);
344 busy |= invalidate_list(&inode_unused, sb, &throw_away);
345 busy |= invalidate_list(&sb->s_dirty, sb, &throw_away);
346 busy |= invalidate_list(&sb->s_io, sb, &throw_away);
347 spin_unlock(&inode_lock);
348
349 dispose_list(&throw_away);
350 up(&iprune_sem);
351
352 return busy;
353}
354
355EXPORT_SYMBOL(invalidate_inodes);
356
357int __invalidate_device(struct block_device *bdev, int do_sync)
358{
359 struct super_block *sb;
360 int res;
361
362 if (do_sync)
363 fsync_bdev(bdev);
364
365 res = 0;
366 sb = get_super(bdev);
367 if (sb) {
368
369
370
371
372
373
374 shrink_dcache_sb(sb);
375 res = invalidate_inodes(sb);
376 drop_super(sb);
377 }
378 invalidate_bdev(bdev, 0);
379 return res;
380}
381
382EXPORT_SYMBOL(__invalidate_device);
383
384static int can_unuse(struct inode *inode)
385{
386 if (inode->i_state)
387 return 0;
388 if (inode_has_buffers(inode))
389 return 0;
390 if (atomic_read(&inode->i_count))
391 return 0;
392 if (inode->i_data.nrpages)
393 return 0;
394 return 1;
395}
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410static void prune_icache(int nr_to_scan)
411{
412 LIST_HEAD(freeable);
413 int nr_pruned = 0;
414 int nr_scanned;
415 unsigned long reap = 0;
416
417 down(&iprune_sem);
418 spin_lock(&inode_lock);
419 for (nr_scanned = 0; nr_scanned < nr_to_scan; nr_scanned++) {
420 struct inode *inode;
421
422 if (list_empty(&inode_unused))
423 break;
424
425 inode = list_entry(inode_unused.prev, struct inode, i_list);
426
427 if (inode->i_state || atomic_read(&inode->i_count)) {
428 list_move(&inode->i_list, &inode_unused);
429 continue;
430 }
431 if (inode_has_buffers(inode) || inode->i_data.nrpages) {
432 __iget(inode);
433 spin_unlock(&inode_lock);
434 if (remove_inode_buffers(inode))
435 reap += invalidate_inode_pages(&inode->i_data);
436 iput(inode);
437 spin_lock(&inode_lock);
438
439 if (inode != list_entry(inode_unused.next,
440 struct inode, i_list))
441 continue;
442 if (!can_unuse(inode))
443 continue;
444 }
445 hlist_del_init(&inode->i_hash);
446 list_move(&inode->i_list, &freeable);
447 inode->i_state |= I_FREEING;
448 nr_pruned++;
449 }
450 inodes_stat.nr_unused -= nr_pruned;
451 spin_unlock(&inode_lock);
452
453 dispose_list(&freeable);
454 up(&iprune_sem);
455
456 if (current_is_kswapd())
457 mod_page_state(kswapd_inodesteal, reap);
458 else
459 mod_page_state(pginodesteal, reap);
460}
461
462
463
464
465
466
467
468
469
470
471static int shrink_icache_memory(int nr, unsigned int gfp_mask)
472{
473 if (nr) {
474
475
476
477
478
479 if (gfp_mask & __GFP_FS)
480 prune_icache(nr);
481 }
482 return inodes_stat.nr_unused;
483}
484
485static void __wait_on_freeing_inode(struct inode *inode);
486
487
488
489
490
491
492static struct inode * find_inode(struct super_block * sb, struct hlist_head *head, int (*test)(struct inode *, void *), void *data)
493{
494 struct hlist_node *node;
495 struct inode * inode = NULL;
496
497repeat:
498 hlist_for_each (node, head) {
499 inode = hlist_entry(node, struct inode, i_hash);
500 if (inode->i_sb != sb)
501 continue;
502 if (!test(inode, data))
503 continue;
504 if (inode->i_state & (I_FREEING|I_CLEAR)) {
505 __wait_on_freeing_inode(inode);
506 goto repeat;
507 }
508 break;
509 }
510 return node ? inode : NULL;
511}
512
513
514
515
516
517static struct inode * find_inode_fast(struct super_block * sb, struct hlist_head *head, unsigned long ino)
518{
519 struct hlist_node *node;
520 struct inode * inode = NULL;
521
522repeat:
523 hlist_for_each (node, head) {
524 inode = hlist_entry(node, struct inode, i_hash);
525 if (inode->i_ino != ino)
526 continue;
527 if (inode->i_sb != sb)
528 continue;
529 if (inode->i_state & (I_FREEING|I_CLEAR)) {
530 __wait_on_freeing_inode(inode);
531 goto repeat;
532 }
533 break;
534 }
535 return node ? inode : NULL;
536}
537
538
539
540
541
542
543
544struct inode *new_inode(struct super_block *sb)
545{
546 static unsigned long last_ino;
547 struct inode * inode;
548
549 spin_lock_prefetch(&inode_lock);
550
551 inode = alloc_inode(sb);
552 if (inode) {
553 spin_lock(&inode_lock);
554 inodes_stat.nr_inodes++;
555 list_add(&inode->i_list, &inode_in_use);
556 inode->i_ino = ++last_ino;
557 inode->i_state = 0;
558 spin_unlock(&inode_lock);
559 }
560 return inode;
561}
562
563EXPORT_SYMBOL(new_inode);
564
565void unlock_new_inode(struct inode *inode)
566{
567
568
569
570
571
572
573
574
575 inode->i_state &= ~(I_LOCK|I_NEW);
576 wake_up_inode(inode);
577}
578
579EXPORT_SYMBOL(unlock_new_inode);
580
581
582
583
584
585
586
587static 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)
588{
589 struct inode * inode;
590
591 inode = alloc_inode(sb);
592 if (inode) {
593 struct inode * old;
594
595 spin_lock(&inode_lock);
596
597 old = find_inode(sb, head, test, data);
598 if (!old) {
599 if (set(inode, data))
600 goto set_failed;
601
602 inodes_stat.nr_inodes++;
603 list_add(&inode->i_list, &inode_in_use);
604 hlist_add_head(&inode->i_hash, head);
605 inode->i_state = I_LOCK|I_NEW;
606 spin_unlock(&inode_lock);
607
608
609
610
611 return inode;
612 }
613
614
615
616
617
618
619 __iget(old);
620 spin_unlock(&inode_lock);
621 destroy_inode(inode);
622 inode = old;
623 wait_on_inode(inode);
624 }
625 return inode;
626
627set_failed:
628 spin_unlock(&inode_lock);
629 destroy_inode(inode);
630 return NULL;
631}
632
633
634
635
636
637static struct inode * get_new_inode_fast(struct super_block *sb, struct hlist_head *head, unsigned long ino)
638{
639 struct inode * inode;
640
641 inode = alloc_inode(sb);
642 if (inode) {
643 struct inode * old;
644
645 spin_lock(&inode_lock);
646
647 old = find_inode_fast(sb, head, ino);
648 if (!old) {
649 inode->i_ino = ino;
650 inodes_stat.nr_inodes++;
651 list_add(&inode->i_list, &inode_in_use);
652 hlist_add_head(&inode->i_hash, head);
653 inode->i_state = I_LOCK|I_NEW;
654 spin_unlock(&inode_lock);
655
656
657
658
659 return inode;
660 }
661
662
663
664
665
666
667 __iget(old);
668 spin_unlock(&inode_lock);
669 destroy_inode(inode);
670 inode = old;
671 wait_on_inode(inode);
672 }
673 return inode;
674}
675
676static inline unsigned long hash(struct super_block *sb, unsigned long hashval)
677{
678 unsigned long tmp = hashval + ((unsigned long) sb / L1_CACHE_BYTES);
679 tmp = tmp + (tmp >> I_HASHBITS);
680 return tmp & I_HASHMASK;
681}
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699ino_t iunique(struct super_block *sb, ino_t max_reserved)
700{
701 static ino_t counter;
702 struct inode *inode;
703 struct hlist_head * head;
704 ino_t res;
705 spin_lock(&inode_lock);
706retry:
707 if (counter > max_reserved) {
708 head = inode_hashtable + hash(sb,counter);
709 res = counter++;
710 inode = find_inode_fast(sb, head, res);
711 if (!inode) {
712 spin_unlock(&inode_lock);
713 return res;
714 }
715 } else {
716 counter = max_reserved + 1;
717 }
718 goto retry;
719
720}
721
722EXPORT_SYMBOL(iunique);
723
724struct inode *igrab(struct inode *inode)
725{
726 spin_lock(&inode_lock);
727 if (!(inode->i_state & I_FREEING))
728 __iget(inode);
729 else
730
731
732
733
734
735 inode = NULL;
736 spin_unlock(&inode_lock);
737 return inode;
738}
739
740EXPORT_SYMBOL(igrab);
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760static inline struct inode *ifind(struct super_block *sb,
761 struct hlist_head *head, int (*test)(struct inode *, void *),
762 void *data)
763{
764 struct inode *inode;
765
766 spin_lock(&inode_lock);
767 inode = find_inode(sb, head, test, data);
768 if (inode) {
769 __iget(inode);
770 spin_unlock(&inode_lock);
771 wait_on_inode(inode);
772 return inode;
773 }
774 spin_unlock(&inode_lock);
775 return NULL;
776}
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793static inline struct inode *ifind_fast(struct super_block *sb,
794 struct hlist_head *head, unsigned long ino)
795{
796 struct inode *inode;
797
798 spin_lock(&inode_lock);
799 inode = find_inode_fast(sb, head, ino);
800 if (inode) {
801 __iget(inode);
802 spin_unlock(&inode_lock);
803 wait_on_inode(inode);
804 return inode;
805 }
806 spin_unlock(&inode_lock);
807 return NULL;
808}
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
830 int (*test)(struct inode *, void *), void *data)
831{
832 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
833
834 return ifind(sb, head, test, data);
835}
836
837EXPORT_SYMBOL(ilookup5);
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853struct inode *ilookup(struct super_block *sb, unsigned long ino)
854{
855 struct hlist_head *head = inode_hashtable + hash(sb, ino);
856
857 return ifind_fast(sb, head, ino);
858}
859
860EXPORT_SYMBOL(ilookup);
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
885 int (*test)(struct inode *, void *),
886 int (*set)(struct inode *, void *), void *data)
887{
888 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
889 struct inode *inode;
890
891 inode = ifind(sb, head, test, data);
892 if (inode)
893 return inode;
894
895
896
897
898 return get_new_inode(sb, head, test, set, data);
899}
900
901EXPORT_SYMBOL(iget5_locked);
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920struct inode *iget_locked(struct super_block *sb, unsigned long ino)
921{
922 struct hlist_head *head = inode_hashtable + hash(sb, ino);
923 struct inode *inode;
924
925 inode = ifind_fast(sb, head, ino);
926 if (inode)
927 return inode;
928
929
930
931
932 return get_new_inode_fast(sb, head, ino);
933}
934
935EXPORT_SYMBOL(iget_locked);
936
937
938
939
940
941
942
943
944
945void __insert_inode_hash(struct inode *inode, unsigned long hashval)
946{
947 struct hlist_head *head = inode_hashtable + hash(inode->i_sb, hashval);
948 spin_lock(&inode_lock);
949 hlist_add_head(&inode->i_hash, head);
950 spin_unlock(&inode_lock);
951}
952
953EXPORT_SYMBOL(__insert_inode_hash);
954
955
956
957
958
959
960
961void remove_inode_hash(struct inode *inode)
962{
963 spin_lock(&inode_lock);
964 hlist_del_init(&inode->i_hash);
965 spin_unlock(&inode_lock);
966}
967
968EXPORT_SYMBOL(remove_inode_hash);
969
970
971
972
973
974
975
976
977
978
979
980
981
982void generic_delete_inode(struct inode *inode)
983{
984 struct super_operations *op = inode->i_sb->s_op;
985
986 list_del_init(&inode->i_list);
987 inode->i_state|=I_FREEING;
988 inodes_stat.nr_inodes--;
989 spin_unlock(&inode_lock);
990
991 if (inode->i_data.nrpages)
992 truncate_inode_pages(&inode->i_data, 0);
993
994 security_inode_delete(inode);
995
996 if (op->delete_inode) {
997 void (*delete)(struct inode *) = op->delete_inode;
998 if (!is_bad_inode(inode))
999 DQUOT_INIT(inode);
1000
1001 delete(inode);
1002 } else
1003 clear_inode(inode);
1004 spin_lock(&inode_lock);
1005 hlist_del_init(&inode->i_hash);
1006 spin_unlock(&inode_lock);
1007 wake_up_inode(inode);
1008 if (inode->i_state != I_CLEAR)
1009 BUG();
1010 destroy_inode(inode);
1011}
1012
1013EXPORT_SYMBOL(generic_delete_inode);
1014
1015static void generic_forget_inode(struct inode *inode)
1016{
1017 struct super_block *sb = inode->i_sb;
1018
1019 if (!hlist_unhashed(&inode->i_hash)) {
1020 if (!(inode->i_state & (I_DIRTY|I_LOCK))) {
1021 list_del(&inode->i_list);
1022 list_add(&inode->i_list, &inode_unused);
1023 }
1024 inodes_stat.nr_unused++;
1025 spin_unlock(&inode_lock);
1026 if (!sb || (sb->s_flags & MS_ACTIVE))
1027 return;
1028 write_inode_now(inode, 1);
1029 spin_lock(&inode_lock);
1030 inodes_stat.nr_unused--;
1031 hlist_del_init(&inode->i_hash);
1032 }
1033 list_del_init(&inode->i_list);
1034 inode->i_state|=I_FREEING;
1035 inodes_stat.nr_inodes--;
1036 spin_unlock(&inode_lock);
1037 if (inode->i_data.nrpages)
1038 truncate_inode_pages(&inode->i_data, 0);
1039 clear_inode(inode);
1040 destroy_inode(inode);
1041}
1042
1043
1044
1045
1046
1047
1048static void generic_drop_inode(struct inode *inode)
1049{
1050 if (!inode->i_nlink)
1051 generic_delete_inode(inode);
1052 else
1053 generic_forget_inode(inode);
1054}
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067static inline void iput_final(struct inode *inode)
1068{
1069 struct super_operations *op = inode->i_sb->s_op;
1070 void (*drop)(struct inode *) = generic_drop_inode;
1071
1072 if (op && op->drop_inode)
1073 drop = op->drop_inode;
1074 drop(inode);
1075}
1076
1077
1078
1079
1080
1081
1082
1083
1084void iput(struct inode *inode)
1085{
1086 if (inode) {
1087 struct super_operations *op = inode->i_sb->s_op;
1088
1089 if (inode->i_state == I_CLEAR)
1090 BUG();
1091
1092 if (op && op->put_inode)
1093 op->put_inode(inode);
1094
1095 if (atomic_dec_and_lock(&inode->i_count, &inode_lock))
1096 iput_final(inode);
1097 }
1098}
1099
1100EXPORT_SYMBOL(iput);
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113sector_t bmap(struct inode * inode, sector_t block)
1114{
1115 sector_t res = 0;
1116 if (inode->i_mapping->a_ops->bmap)
1117 res = inode->i_mapping->a_ops->bmap(inode->i_mapping, block);
1118 return res;
1119}
1120
1121EXPORT_SYMBOL(bmap);
1122
1123
1124
1125
1126
1127
1128static int inode_times_differ(struct inode *inode,
1129 struct timespec *old, struct timespec *new)
1130{
1131 if (IS_ONE_SECOND(inode))
1132 return old->tv_sec != new->tv_sec;
1133 return !timespec_equal(old, new);
1134}
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144void update_atime(struct inode *inode)
1145{
1146 struct timespec now;
1147
1148 if (IS_NOATIME(inode))
1149 return;
1150 if (IS_NODIRATIME(inode) && S_ISDIR(inode->i_mode))
1151 return;
1152 if (IS_RDONLY(inode))
1153 return;
1154
1155 now = current_kernel_time();
1156 if (inode_times_differ(inode, &inode->i_atime, &now)) {
1157 inode->i_atime = now;
1158 mark_inode_dirty_sync(inode);
1159 } else {
1160 if (!timespec_equal(&inode->i_atime, &now))
1161 inode->i_atime = now;
1162 }
1163}
1164
1165EXPORT_SYMBOL(update_atime);
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176void inode_update_time(struct inode *inode, int ctime_too)
1177{
1178 struct timespec now;
1179 int sync_it = 0;
1180
1181 if (IS_RDONLY(inode))
1182 return;
1183
1184 now = current_kernel_time();
1185
1186 if (inode_times_differ(inode, &inode->i_mtime, &now))
1187 sync_it = 1;
1188 inode->i_mtime = now;
1189
1190 if (ctime_too) {
1191 if (inode_times_differ(inode, &inode->i_ctime, &now))
1192 sync_it = 1;
1193 inode->i_ctime = now;
1194 }
1195 if (sync_it)
1196 mark_inode_dirty_sync(inode);
1197}
1198
1199EXPORT_SYMBOL(inode_update_time);
1200
1201int inode_needs_sync(struct inode *inode)
1202{
1203 if (IS_SYNC(inode))
1204 return 1;
1205 if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
1206 return 1;
1207 return 0;
1208}
1209
1210EXPORT_SYMBOL(inode_needs_sync);
1211
1212
1213
1214
1215#ifdef CONFIG_QUOTA
1216
1217
1218void put_dquot_list(struct list_head *);
1219int remove_inode_dquot_ref(struct inode *, int, struct list_head *);
1220
1221void remove_dquot_ref(struct super_block *sb, int type)
1222{
1223 struct inode *inode;
1224 struct list_head *act_head;
1225 LIST_HEAD(tofree_head);
1226
1227 if (!sb->dq_op)
1228 return;
1229 spin_lock(&inode_lock);
1230
1231
1232 list_for_each(act_head, &inode_in_use) {
1233 inode = list_entry(act_head, struct inode, i_list);
1234 if (inode->i_sb == sb && IS_QUOTAINIT(inode))
1235 remove_inode_dquot_ref(inode, type, &tofree_head);
1236 }
1237 list_for_each(act_head, &inode_unused) {
1238 inode = list_entry(act_head, struct inode, i_list);
1239 if (inode->i_sb == sb && IS_QUOTAINIT(inode))
1240 remove_inode_dquot_ref(inode, type, &tofree_head);
1241 }
1242 list_for_each(act_head, &sb->s_dirty) {
1243 inode = list_entry(act_head, struct inode, i_list);
1244 if (IS_QUOTAINIT(inode))
1245 remove_inode_dquot_ref(inode, type, &tofree_head);
1246 }
1247 list_for_each(act_head, &sb->s_io) {
1248 inode = list_entry(act_head, struct inode, i_list);
1249 if (IS_QUOTAINIT(inode))
1250 remove_inode_dquot_ref(inode, type, &tofree_head);
1251 }
1252 spin_unlock(&inode_lock);
1253
1254 put_dquot_list(&tofree_head);
1255}
1256
1257#endif
1258
1259
1260
1261
1262
1263#define I_WAIT_TABLE_ORDER 3
1264static struct i_wait_queue_head {
1265 wait_queue_head_t wqh;
1266} ____cacheline_aligned_in_smp i_wait_queue_heads[1<<I_WAIT_TABLE_ORDER];
1267
1268
1269
1270
1271static wait_queue_head_t *i_waitq_head(struct inode *inode)
1272{
1273 return &i_wait_queue_heads[hash_ptr(inode, I_WAIT_TABLE_ORDER)].wqh;
1274}
1275
1276void __wait_on_inode(struct inode *inode)
1277{
1278 DECLARE_WAITQUEUE(wait, current);
1279 wait_queue_head_t *wq = i_waitq_head(inode);
1280
1281 add_wait_queue(wq, &wait);
1282repeat:
1283 set_current_state(TASK_UNINTERRUPTIBLE);
1284 if (inode->i_state & I_LOCK) {
1285 schedule();
1286 goto repeat;
1287 }
1288 remove_wait_queue(wq, &wait);
1289 __set_current_state(TASK_RUNNING);
1290}
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305static void __wait_on_freeing_inode(struct inode *inode)
1306{
1307 DECLARE_WAITQUEUE(wait, current);
1308 wait_queue_head_t *wq = i_waitq_head(inode);
1309
1310 add_wait_queue(wq, &wait);
1311 set_current_state(TASK_UNINTERRUPTIBLE);
1312 spin_unlock(&inode_lock);
1313 schedule();
1314 remove_wait_queue(wq, &wait);
1315 spin_lock(&inode_lock);
1316}
1317
1318void wake_up_inode(struct inode *inode)
1319{
1320 wait_queue_head_t *wq = i_waitq_head(inode);
1321
1322
1323
1324
1325 smp_mb();
1326 if (waitqueue_active(wq))
1327 wake_up_all(wq);
1328}
1329
1330
1331
1332
1333void __init inode_init(unsigned long mempages)
1334{
1335 struct hlist_head *head;
1336 unsigned long order;
1337 unsigned int nr_hash;
1338 int i;
1339
1340 for (i = 0; i < ARRAY_SIZE(i_wait_queue_heads); i++)
1341 init_waitqueue_head(&i_wait_queue_heads[i].wqh);
1342
1343 mempages >>= (14 - PAGE_SHIFT);
1344 mempages *= sizeof(struct hlist_head);
1345 for (order = 0; ((1UL << order) << PAGE_SHIFT) < mempages; order++)
1346 ;
1347
1348 do {
1349 unsigned long tmp;
1350
1351 nr_hash = (1UL << order) * PAGE_SIZE /
1352 sizeof(struct hlist_head);
1353 i_hash_mask = (nr_hash - 1);
1354
1355 tmp = nr_hash;
1356 i_hash_shift = 0;
1357 while ((tmp >>= 1UL) != 0UL)
1358 i_hash_shift++;
1359
1360 inode_hashtable = (struct hlist_head *)
1361 __get_free_pages(GFP_ATOMIC, order);
1362 } while (inode_hashtable == NULL && --order >= 0);
1363
1364 printk("Inode-cache hash table entries: %d (order: %ld, %ld bytes)\n",
1365 nr_hash, order, (PAGE_SIZE << order));
1366
1367 if (!inode_hashtable)
1368 panic("Failed to allocate inode hash table\n");
1369
1370 head = inode_hashtable;
1371 i = nr_hash;
1372 do {
1373 INIT_HLIST_HEAD(head);
1374 head++;
1375 i--;
1376 } while (i);
1377
1378
1379 inode_cachep = kmem_cache_create("inode_cache", sizeof(struct inode),
1380 0, SLAB_HWCACHE_ALIGN, init_once,
1381 NULL);
1382 if (!inode_cachep)
1383 panic("cannot create inode slab cache");
1384
1385 set_shrinker(DEFAULT_SEEKS, shrink_icache_memory);
1386}
1387
1388void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
1389{
1390 inode->i_mode = mode;
1391 if (S_ISCHR(mode)) {
1392 inode->i_fop = &def_chr_fops;
1393 inode->i_rdev = rdev;
1394 } else if (S_ISBLK(mode)) {
1395 inode->i_fop = &def_blk_fops;
1396 inode->i_rdev = rdev;
1397 } else if (S_ISFIFO(mode))
1398 inode->i_fop = &def_fifo_fops;
1399 else if (S_ISSOCK(mode))
1400 inode->i_fop = &bad_sock_fops;
1401 else
1402 printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o)\n",
1403 mode);
1404}
1405
1406EXPORT_SYMBOL(init_special_inode);
1407