1
2
3
4
5
6#include <linux/export.h>
7#include <linux/fs.h>
8#include <linux/mm.h>
9#include <linux/backing-dev.h>
10#include <linux/hash.h>
11#include <linux/swap.h>
12#include <linux/security.h>
13#include <linux/cdev.h>
14#include <linux/memblock.h>
15#include <linux/fsnotify.h>
16#include <linux/mount.h>
17#include <linux/posix_acl.h>
18#include <linux/prefetch.h>
19#include <linux/buffer_head.h>
20#include <linux/ratelimit.h>
21#include <linux/list_lru.h>
22#include <linux/iversion.h>
23#include <trace/events/writeback.h>
24#include "internal.h"
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57static unsigned int i_hash_mask __read_mostly;
58static unsigned int i_hash_shift __read_mostly;
59static struct hlist_head *inode_hashtable __read_mostly;
60static __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_hash_lock);
61
62
63
64
65
66const struct address_space_operations empty_aops = {
67};
68EXPORT_SYMBOL(empty_aops);
69
70
71
72
73struct inodes_stat_t inodes_stat;
74
75static DEFINE_PER_CPU(unsigned long, nr_inodes);
76static DEFINE_PER_CPU(unsigned long, nr_unused);
77
78static struct kmem_cache *inode_cachep __read_mostly;
79
80static long get_nr_inodes(void)
81{
82 int i;
83 long sum = 0;
84 for_each_possible_cpu(i)
85 sum += per_cpu(nr_inodes, i);
86 return sum < 0 ? 0 : sum;
87}
88
89static inline long get_nr_inodes_unused(void)
90{
91 int i;
92 long sum = 0;
93 for_each_possible_cpu(i)
94 sum += per_cpu(nr_unused, i);
95 return sum < 0 ? 0 : sum;
96}
97
98long get_nr_dirty_inodes(void)
99{
100
101 long nr_dirty = get_nr_inodes() - get_nr_inodes_unused();
102 return nr_dirty > 0 ? nr_dirty : 0;
103}
104
105
106
107
108#ifdef CONFIG_SYSCTL
109int proc_nr_inodes(struct ctl_table *table, int write,
110 void *buffer, size_t *lenp, loff_t *ppos)
111{
112 inodes_stat.nr_inodes = get_nr_inodes();
113 inodes_stat.nr_unused = get_nr_inodes_unused();
114 return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
115}
116#endif
117
118static int no_open(struct inode *inode, struct file *file)
119{
120 return -ENXIO;
121}
122
123
124
125
126
127
128
129
130
131int inode_init_always(struct super_block *sb, struct inode *inode)
132{
133 static const struct inode_operations empty_iops;
134 static const struct file_operations no_open_fops = {.open = no_open};
135 struct address_space *const mapping = &inode->i_data;
136
137 inode->i_sb = sb;
138 inode->i_blkbits = sb->s_blocksize_bits;
139 inode->i_flags = 0;
140 atomic64_set(&inode->i_sequence, 0);
141 atomic_set(&inode->i_count, 1);
142 inode->i_op = &empty_iops;
143 inode->i_fop = &no_open_fops;
144 inode->i_ino = 0;
145 inode->__i_nlink = 1;
146 inode->i_opflags = 0;
147 if (sb->s_xattr)
148 inode->i_opflags |= IOP_XATTR;
149 i_uid_write(inode, 0);
150 i_gid_write(inode, 0);
151 atomic_set(&inode->i_writecount, 0);
152 inode->i_size = 0;
153 inode->i_write_hint = WRITE_LIFE_NOT_SET;
154 inode->i_blocks = 0;
155 inode->i_bytes = 0;
156 inode->i_generation = 0;
157 inode->i_pipe = NULL;
158 inode->i_cdev = NULL;
159 inode->i_link = NULL;
160 inode->i_dir_seq = 0;
161 inode->i_rdev = 0;
162 inode->dirtied_when = 0;
163
164#ifdef CONFIG_CGROUP_WRITEBACK
165 inode->i_wb_frn_winner = 0;
166 inode->i_wb_frn_avg_time = 0;
167 inode->i_wb_frn_history = 0;
168#endif
169
170 if (security_inode_alloc(inode))
171 goto out;
172 spin_lock_init(&inode->i_lock);
173 lockdep_set_class(&inode->i_lock, &sb->s_type->i_lock_key);
174
175 init_rwsem(&inode->i_rwsem);
176 lockdep_set_class(&inode->i_rwsem, &sb->s_type->i_mutex_key);
177
178 atomic_set(&inode->i_dio_count, 0);
179
180 mapping->a_ops = &empty_aops;
181 mapping->host = inode;
182 mapping->flags = 0;
183 mapping->wb_err = 0;
184 atomic_set(&mapping->i_mmap_writable, 0);
185#ifdef CONFIG_READ_ONLY_THP_FOR_FS
186 atomic_set(&mapping->nr_thps, 0);
187#endif
188 mapping_set_gfp_mask(mapping, GFP_HIGHUSER_MOVABLE);
189 mapping->private_data = NULL;
190 mapping->writeback_index = 0;
191 init_rwsem(&mapping->invalidate_lock);
192 lockdep_set_class_and_name(&mapping->invalidate_lock,
193 &sb->s_type->invalidate_lock_key,
194 "mapping.invalidate_lock");
195 inode->i_private = NULL;
196 inode->i_mapping = mapping;
197 INIT_HLIST_HEAD(&inode->i_dentry);
198#ifdef CONFIG_FS_POSIX_ACL
199 inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED;
200#endif
201
202#ifdef CONFIG_FSNOTIFY
203 inode->i_fsnotify_mask = 0;
204#endif
205 inode->i_flctx = NULL;
206 this_cpu_inc(nr_inodes);
207
208 return 0;
209out:
210 return -ENOMEM;
211}
212EXPORT_SYMBOL(inode_init_always);
213
214void free_inode_nonrcu(struct inode *inode)
215{
216 kmem_cache_free(inode_cachep, inode);
217}
218EXPORT_SYMBOL(free_inode_nonrcu);
219
220static void i_callback(struct rcu_head *head)
221{
222 struct inode *inode = container_of(head, struct inode, i_rcu);
223 if (inode->free_inode)
224 inode->free_inode(inode);
225 else
226 free_inode_nonrcu(inode);
227}
228
229static struct inode *alloc_inode(struct super_block *sb)
230{
231 const struct super_operations *ops = sb->s_op;
232 struct inode *inode;
233
234 if (ops->alloc_inode)
235 inode = ops->alloc_inode(sb);
236 else
237 inode = kmem_cache_alloc(inode_cachep, GFP_KERNEL);
238
239 if (!inode)
240 return NULL;
241
242 if (unlikely(inode_init_always(sb, inode))) {
243 if (ops->destroy_inode) {
244 ops->destroy_inode(inode);
245 if (!ops->free_inode)
246 return NULL;
247 }
248 inode->free_inode = ops->free_inode;
249 i_callback(&inode->i_rcu);
250 return NULL;
251 }
252
253 return inode;
254}
255
256void __destroy_inode(struct inode *inode)
257{
258 BUG_ON(inode_has_buffers(inode));
259 inode_detach_wb(inode);
260 security_inode_free(inode);
261 fsnotify_inode_delete(inode);
262 locks_free_lock_context(inode);
263 if (!inode->i_nlink) {
264 WARN_ON(atomic_long_read(&inode->i_sb->s_remove_count) == 0);
265 atomic_long_dec(&inode->i_sb->s_remove_count);
266 }
267
268#ifdef CONFIG_FS_POSIX_ACL
269 if (inode->i_acl && !is_uncached_acl(inode->i_acl))
270 posix_acl_release(inode->i_acl);
271 if (inode->i_default_acl && !is_uncached_acl(inode->i_default_acl))
272 posix_acl_release(inode->i_default_acl);
273#endif
274 this_cpu_dec(nr_inodes);
275}
276EXPORT_SYMBOL(__destroy_inode);
277
278static void destroy_inode(struct inode *inode)
279{
280 const struct super_operations *ops = inode->i_sb->s_op;
281
282 BUG_ON(!list_empty(&inode->i_lru));
283 __destroy_inode(inode);
284 if (ops->destroy_inode) {
285 ops->destroy_inode(inode);
286 if (!ops->free_inode)
287 return;
288 }
289 inode->free_inode = ops->free_inode;
290 call_rcu(&inode->i_rcu, i_callback);
291}
292
293
294
295
296
297
298
299
300
301
302
303
304void drop_nlink(struct inode *inode)
305{
306 WARN_ON(inode->i_nlink == 0);
307 inode->__i_nlink--;
308 if (!inode->i_nlink)
309 atomic_long_inc(&inode->i_sb->s_remove_count);
310}
311EXPORT_SYMBOL(drop_nlink);
312
313
314
315
316
317
318
319
320
321void clear_nlink(struct inode *inode)
322{
323 if (inode->i_nlink) {
324 inode->__i_nlink = 0;
325 atomic_long_inc(&inode->i_sb->s_remove_count);
326 }
327}
328EXPORT_SYMBOL(clear_nlink);
329
330
331
332
333
334
335
336
337
338void set_nlink(struct inode *inode, unsigned int nlink)
339{
340 if (!nlink) {
341 clear_nlink(inode);
342 } else {
343
344 if (inode->i_nlink == 0)
345 atomic_long_dec(&inode->i_sb->s_remove_count);
346
347 inode->__i_nlink = nlink;
348 }
349}
350EXPORT_SYMBOL(set_nlink);
351
352
353
354
355
356
357
358
359
360void inc_nlink(struct inode *inode)
361{
362 if (unlikely(inode->i_nlink == 0)) {
363 WARN_ON(!(inode->i_state & I_LINKABLE));
364 atomic_long_dec(&inode->i_sb->s_remove_count);
365 }
366
367 inode->__i_nlink++;
368}
369EXPORT_SYMBOL(inc_nlink);
370
371static void __address_space_init_once(struct address_space *mapping)
372{
373 xa_init_flags(&mapping->i_pages, XA_FLAGS_LOCK_IRQ | XA_FLAGS_ACCOUNT);
374 init_rwsem(&mapping->i_mmap_rwsem);
375 INIT_LIST_HEAD(&mapping->private_list);
376 spin_lock_init(&mapping->private_lock);
377 mapping->i_mmap = RB_ROOT_CACHED;
378}
379
380void address_space_init_once(struct address_space *mapping)
381{
382 memset(mapping, 0, sizeof(*mapping));
383 __address_space_init_once(mapping);
384}
385EXPORT_SYMBOL(address_space_init_once);
386
387
388
389
390
391
392void inode_init_once(struct inode *inode)
393{
394 memset(inode, 0, sizeof(*inode));
395 INIT_HLIST_NODE(&inode->i_hash);
396 INIT_LIST_HEAD(&inode->i_devices);
397 INIT_LIST_HEAD(&inode->i_io_list);
398 INIT_LIST_HEAD(&inode->i_wb_list);
399 INIT_LIST_HEAD(&inode->i_lru);
400 __address_space_init_once(&inode->i_data);
401 i_size_ordered_init(inode);
402}
403EXPORT_SYMBOL(inode_init_once);
404
405static void init_once(void *foo)
406{
407 struct inode *inode = (struct inode *) foo;
408
409 inode_init_once(inode);
410}
411
412
413
414
415void __iget(struct inode *inode)
416{
417 atomic_inc(&inode->i_count);
418}
419
420
421
422
423void ihold(struct inode *inode)
424{
425 WARN_ON(atomic_inc_return(&inode->i_count) < 2);
426}
427EXPORT_SYMBOL(ihold);
428
429static void __inode_add_lru(struct inode *inode, bool rotate)
430{
431 if (inode->i_state & (I_DIRTY_ALL | I_SYNC | I_FREEING | I_WILL_FREE))
432 return;
433 if (atomic_read(&inode->i_count))
434 return;
435 if (!(inode->i_sb->s_flags & SB_ACTIVE))
436 return;
437 if (!mapping_shrinkable(&inode->i_data))
438 return;
439
440 if (list_lru_add(&inode->i_sb->s_inode_lru, &inode->i_lru))
441 this_cpu_inc(nr_unused);
442 else if (rotate)
443 inode->i_state |= I_REFERENCED;
444}
445
446
447
448
449
450
451void inode_add_lru(struct inode *inode)
452{
453 __inode_add_lru(inode, false);
454}
455
456static void inode_lru_list_del(struct inode *inode)
457{
458 if (list_lru_del(&inode->i_sb->s_inode_lru, &inode->i_lru))
459 this_cpu_dec(nr_unused);
460}
461
462
463
464
465
466void inode_sb_list_add(struct inode *inode)
467{
468 spin_lock(&inode->i_sb->s_inode_list_lock);
469 list_add(&inode->i_sb_list, &inode->i_sb->s_inodes);
470 spin_unlock(&inode->i_sb->s_inode_list_lock);
471}
472EXPORT_SYMBOL_GPL(inode_sb_list_add);
473
474static inline void inode_sb_list_del(struct inode *inode)
475{
476 if (!list_empty(&inode->i_sb_list)) {
477 spin_lock(&inode->i_sb->s_inode_list_lock);
478 list_del_init(&inode->i_sb_list);
479 spin_unlock(&inode->i_sb->s_inode_list_lock);
480 }
481}
482
483static unsigned long hash(struct super_block *sb, unsigned long hashval)
484{
485 unsigned long tmp;
486
487 tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /
488 L1_CACHE_BYTES;
489 tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> i_hash_shift);
490 return tmp & i_hash_mask;
491}
492
493
494
495
496
497
498
499
500
501void __insert_inode_hash(struct inode *inode, unsigned long hashval)
502{
503 struct hlist_head *b = inode_hashtable + hash(inode->i_sb, hashval);
504
505 spin_lock(&inode_hash_lock);
506 spin_lock(&inode->i_lock);
507 hlist_add_head_rcu(&inode->i_hash, b);
508 spin_unlock(&inode->i_lock);
509 spin_unlock(&inode_hash_lock);
510}
511EXPORT_SYMBOL(__insert_inode_hash);
512
513
514
515
516
517
518
519void __remove_inode_hash(struct inode *inode)
520{
521 spin_lock(&inode_hash_lock);
522 spin_lock(&inode->i_lock);
523 hlist_del_init_rcu(&inode->i_hash);
524 spin_unlock(&inode->i_lock);
525 spin_unlock(&inode_hash_lock);
526}
527EXPORT_SYMBOL(__remove_inode_hash);
528
529void clear_inode(struct inode *inode)
530{
531
532
533
534
535
536 xa_lock_irq(&inode->i_data.i_pages);
537 BUG_ON(inode->i_data.nrpages);
538
539
540
541
542
543
544
545
546 xa_unlock_irq(&inode->i_data.i_pages);
547 BUG_ON(!list_empty(&inode->i_data.private_list));
548 BUG_ON(!(inode->i_state & I_FREEING));
549 BUG_ON(inode->i_state & I_CLEAR);
550 BUG_ON(!list_empty(&inode->i_wb_list));
551
552 inode->i_state = I_FREEING | I_CLEAR;
553}
554EXPORT_SYMBOL(clear_inode);
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569static void evict(struct inode *inode)
570{
571 const struct super_operations *op = inode->i_sb->s_op;
572
573 BUG_ON(!(inode->i_state & I_FREEING));
574 BUG_ON(!list_empty(&inode->i_lru));
575
576 if (!list_empty(&inode->i_io_list))
577 inode_io_list_del(inode);
578
579 inode_sb_list_del(inode);
580
581
582
583
584
585
586
587 inode_wait_for_writeback(inode);
588
589 if (op->evict_inode) {
590 op->evict_inode(inode);
591 } else {
592 truncate_inode_pages_final(&inode->i_data);
593 clear_inode(inode);
594 }
595 if (S_ISCHR(inode->i_mode) && inode->i_cdev)
596 cd_forget(inode);
597
598 remove_inode_hash(inode);
599
600 spin_lock(&inode->i_lock);
601 wake_up_bit(&inode->i_state, __I_NEW);
602 BUG_ON(inode->i_state != (I_FREEING | I_CLEAR));
603 spin_unlock(&inode->i_lock);
604
605 destroy_inode(inode);
606}
607
608
609
610
611
612
613
614
615static void dispose_list(struct list_head *head)
616{
617 while (!list_empty(head)) {
618 struct inode *inode;
619
620 inode = list_first_entry(head, struct inode, i_lru);
621 list_del_init(&inode->i_lru);
622
623 evict(inode);
624 cond_resched();
625 }
626}
627
628
629
630
631
632
633
634
635
636
637void evict_inodes(struct super_block *sb)
638{
639 struct inode *inode, *next;
640 LIST_HEAD(dispose);
641
642again:
643 spin_lock(&sb->s_inode_list_lock);
644 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
645 if (atomic_read(&inode->i_count))
646 continue;
647
648 spin_lock(&inode->i_lock);
649 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
650 spin_unlock(&inode->i_lock);
651 continue;
652 }
653
654 inode->i_state |= I_FREEING;
655 inode_lru_list_del(inode);
656 spin_unlock(&inode->i_lock);
657 list_add(&inode->i_lru, &dispose);
658
659
660
661
662
663
664 if (need_resched()) {
665 spin_unlock(&sb->s_inode_list_lock);
666 cond_resched();
667 dispose_list(&dispose);
668 goto again;
669 }
670 }
671 spin_unlock(&sb->s_inode_list_lock);
672
673 dispose_list(&dispose);
674}
675EXPORT_SYMBOL_GPL(evict_inodes);
676
677
678
679
680
681
682
683
684
685
686
687int invalidate_inodes(struct super_block *sb, bool kill_dirty)
688{
689 int busy = 0;
690 struct inode *inode, *next;
691 LIST_HEAD(dispose);
692
693again:
694 spin_lock(&sb->s_inode_list_lock);
695 list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
696 spin_lock(&inode->i_lock);
697 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
698 spin_unlock(&inode->i_lock);
699 continue;
700 }
701 if (inode->i_state & I_DIRTY_ALL && !kill_dirty) {
702 spin_unlock(&inode->i_lock);
703 busy = 1;
704 continue;
705 }
706 if (atomic_read(&inode->i_count)) {
707 spin_unlock(&inode->i_lock);
708 busy = 1;
709 continue;
710 }
711
712 inode->i_state |= I_FREEING;
713 inode_lru_list_del(inode);
714 spin_unlock(&inode->i_lock);
715 list_add(&inode->i_lru, &dispose);
716 if (need_resched()) {
717 spin_unlock(&sb->s_inode_list_lock);
718 cond_resched();
719 dispose_list(&dispose);
720 goto again;
721 }
722 }
723 spin_unlock(&sb->s_inode_list_lock);
724
725 dispose_list(&dispose);
726
727 return busy;
728}
729
730
731
732
733
734
735
736
737
738
739
740
741static enum lru_status inode_lru_isolate(struct list_head *item,
742 struct list_lru_one *lru, spinlock_t *lru_lock, void *arg)
743{
744 struct list_head *freeable = arg;
745 struct inode *inode = container_of(item, struct inode, i_lru);
746
747
748
749
750
751 if (!spin_trylock(&inode->i_lock))
752 return LRU_SKIP;
753
754
755
756
757
758
759
760 if (atomic_read(&inode->i_count) ||
761 (inode->i_state & ~I_REFERENCED) ||
762 !mapping_shrinkable(&inode->i_data)) {
763 list_lru_isolate(lru, &inode->i_lru);
764 spin_unlock(&inode->i_lock);
765 this_cpu_dec(nr_unused);
766 return LRU_REMOVED;
767 }
768
769
770 if (inode->i_state & I_REFERENCED) {
771 inode->i_state &= ~I_REFERENCED;
772 spin_unlock(&inode->i_lock);
773 return LRU_ROTATE;
774 }
775
776
777
778
779
780
781 if (inode_has_buffers(inode) || !mapping_empty(&inode->i_data)) {
782 __iget(inode);
783 spin_unlock(&inode->i_lock);
784 spin_unlock(lru_lock);
785 if (remove_inode_buffers(inode)) {
786 unsigned long reap;
787 reap = invalidate_mapping_pages(&inode->i_data, 0, -1);
788 if (current_is_kswapd())
789 __count_vm_events(KSWAPD_INODESTEAL, reap);
790 else
791 __count_vm_events(PGINODESTEAL, reap);
792 if (current->reclaim_state)
793 current->reclaim_state->reclaimed_slab += reap;
794 }
795 iput(inode);
796 spin_lock(lru_lock);
797 return LRU_RETRY;
798 }
799
800 WARN_ON(inode->i_state & I_NEW);
801 inode->i_state |= I_FREEING;
802 list_lru_isolate_move(lru, &inode->i_lru, freeable);
803 spin_unlock(&inode->i_lock);
804
805 this_cpu_dec(nr_unused);
806 return LRU_REMOVED;
807}
808
809
810
811
812
813
814
815long prune_icache_sb(struct super_block *sb, struct shrink_control *sc)
816{
817 LIST_HEAD(freeable);
818 long freed;
819
820 freed = list_lru_shrink_walk(&sb->s_inode_lru, sc,
821 inode_lru_isolate, &freeable);
822 dispose_list(&freeable);
823 return freed;
824}
825
826static void __wait_on_freeing_inode(struct inode *inode);
827
828
829
830static struct inode *find_inode(struct super_block *sb,
831 struct hlist_head *head,
832 int (*test)(struct inode *, void *),
833 void *data)
834{
835 struct inode *inode = NULL;
836
837repeat:
838 hlist_for_each_entry(inode, head, i_hash) {
839 if (inode->i_sb != sb)
840 continue;
841 if (!test(inode, data))
842 continue;
843 spin_lock(&inode->i_lock);
844 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
845 __wait_on_freeing_inode(inode);
846 goto repeat;
847 }
848 if (unlikely(inode->i_state & I_CREATING)) {
849 spin_unlock(&inode->i_lock);
850 return ERR_PTR(-ESTALE);
851 }
852 __iget(inode);
853 spin_unlock(&inode->i_lock);
854 return inode;
855 }
856 return NULL;
857}
858
859
860
861
862
863static struct inode *find_inode_fast(struct super_block *sb,
864 struct hlist_head *head, unsigned long ino)
865{
866 struct inode *inode = NULL;
867
868repeat:
869 hlist_for_each_entry(inode, head, i_hash) {
870 if (inode->i_ino != ino)
871 continue;
872 if (inode->i_sb != sb)
873 continue;
874 spin_lock(&inode->i_lock);
875 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
876 __wait_on_freeing_inode(inode);
877 goto repeat;
878 }
879 if (unlikely(inode->i_state & I_CREATING)) {
880 spin_unlock(&inode->i_lock);
881 return ERR_PTR(-ESTALE);
882 }
883 __iget(inode);
884 spin_unlock(&inode->i_lock);
885 return inode;
886 }
887 return NULL;
888}
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905#define LAST_INO_BATCH 1024
906static DEFINE_PER_CPU(unsigned int, last_ino);
907
908unsigned int get_next_ino(void)
909{
910 unsigned int *p = &get_cpu_var(last_ino);
911 unsigned int res = *p;
912
913#ifdef CONFIG_SMP
914 if (unlikely((res & (LAST_INO_BATCH-1)) == 0)) {
915 static atomic_t shared_last_ino;
916 int next = atomic_add_return(LAST_INO_BATCH, &shared_last_ino);
917
918 res = next - LAST_INO_BATCH;
919 }
920#endif
921
922 res++;
923
924 if (unlikely(!res))
925 res++;
926 *p = res;
927 put_cpu_var(last_ino);
928 return res;
929}
930EXPORT_SYMBOL(get_next_ino);
931
932
933
934
935
936
937
938
939
940
941
942struct inode *new_inode_pseudo(struct super_block *sb)
943{
944 struct inode *inode = alloc_inode(sb);
945
946 if (inode) {
947 spin_lock(&inode->i_lock);
948 inode->i_state = 0;
949 spin_unlock(&inode->i_lock);
950 INIT_LIST_HEAD(&inode->i_sb_list);
951 }
952 return inode;
953}
954
955
956
957
958
959
960
961
962
963
964
965
966
967struct inode *new_inode(struct super_block *sb)
968{
969 struct inode *inode;
970
971 spin_lock_prefetch(&sb->s_inode_list_lock);
972
973 inode = new_inode_pseudo(sb);
974 if (inode)
975 inode_sb_list_add(inode);
976 return inode;
977}
978EXPORT_SYMBOL(new_inode);
979
980#ifdef CONFIG_DEBUG_LOCK_ALLOC
981void lockdep_annotate_inode_mutex_key(struct inode *inode)
982{
983 if (S_ISDIR(inode->i_mode)) {
984 struct file_system_type *type = inode->i_sb->s_type;
985
986
987 if (lockdep_match_class(&inode->i_rwsem, &type->i_mutex_key)) {
988
989
990
991
992 init_rwsem(&inode->i_rwsem);
993 lockdep_set_class(&inode->i_rwsem,
994 &type->i_mutex_dir_key);
995 }
996 }
997}
998EXPORT_SYMBOL(lockdep_annotate_inode_mutex_key);
999#endif
1000
1001
1002
1003
1004
1005
1006
1007
1008void unlock_new_inode(struct inode *inode)
1009{
1010 lockdep_annotate_inode_mutex_key(inode);
1011 spin_lock(&inode->i_lock);
1012 WARN_ON(!(inode->i_state & I_NEW));
1013 inode->i_state &= ~I_NEW & ~I_CREATING;
1014 smp_mb();
1015 wake_up_bit(&inode->i_state, __I_NEW);
1016 spin_unlock(&inode->i_lock);
1017}
1018EXPORT_SYMBOL(unlock_new_inode);
1019
1020void discard_new_inode(struct inode *inode)
1021{
1022 lockdep_annotate_inode_mutex_key(inode);
1023 spin_lock(&inode->i_lock);
1024 WARN_ON(!(inode->i_state & I_NEW));
1025 inode->i_state &= ~I_NEW;
1026 smp_mb();
1027 wake_up_bit(&inode->i_state, __I_NEW);
1028 spin_unlock(&inode->i_lock);
1029 iput(inode);
1030}
1031EXPORT_SYMBOL(discard_new_inode);
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042void lock_two_nondirectories(struct inode *inode1, struct inode *inode2)
1043{
1044 if (inode1 > inode2)
1045 swap(inode1, inode2);
1046
1047 if (inode1 && !S_ISDIR(inode1->i_mode))
1048 inode_lock(inode1);
1049 if (inode2 && !S_ISDIR(inode2->i_mode) && inode2 != inode1)
1050 inode_lock_nested(inode2, I_MUTEX_NONDIR2);
1051}
1052EXPORT_SYMBOL(lock_two_nondirectories);
1053
1054
1055
1056
1057
1058
1059void unlock_two_nondirectories(struct inode *inode1, struct inode *inode2)
1060{
1061 if (inode1 && !S_ISDIR(inode1->i_mode))
1062 inode_unlock(inode1);
1063 if (inode2 && !S_ISDIR(inode2->i_mode) && inode2 != inode1)
1064 inode_unlock(inode2);
1065}
1066EXPORT_SYMBOL(unlock_two_nondirectories);
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088struct inode *inode_insert5(struct inode *inode, unsigned long hashval,
1089 int (*test)(struct inode *, void *),
1090 int (*set)(struct inode *, void *), void *data)
1091{
1092 struct hlist_head *head = inode_hashtable + hash(inode->i_sb, hashval);
1093 struct inode *old;
1094 bool creating = inode->i_state & I_CREATING;
1095
1096again:
1097 spin_lock(&inode_hash_lock);
1098 old = find_inode(inode->i_sb, head, test, data);
1099 if (unlikely(old)) {
1100
1101
1102
1103
1104 spin_unlock(&inode_hash_lock);
1105 if (IS_ERR(old))
1106 return NULL;
1107 wait_on_inode(old);
1108 if (unlikely(inode_unhashed(old))) {
1109 iput(old);
1110 goto again;
1111 }
1112 return old;
1113 }
1114
1115 if (set && unlikely(set(inode, data))) {
1116 inode = NULL;
1117 goto unlock;
1118 }
1119
1120
1121
1122
1123
1124 spin_lock(&inode->i_lock);
1125 inode->i_state |= I_NEW;
1126 hlist_add_head_rcu(&inode->i_hash, head);
1127 spin_unlock(&inode->i_lock);
1128 if (!creating)
1129 inode_sb_list_add(inode);
1130unlock:
1131 spin_unlock(&inode_hash_lock);
1132
1133 return inode;
1134}
1135EXPORT_SYMBOL(inode_insert5);
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
1158 int (*test)(struct inode *, void *),
1159 int (*set)(struct inode *, void *), void *data)
1160{
1161 struct inode *inode = ilookup5(sb, hashval, test, data);
1162
1163 if (!inode) {
1164 struct inode *new = alloc_inode(sb);
1165
1166 if (new) {
1167 new->i_state = 0;
1168 inode = inode_insert5(new, hashval, test, set, data);
1169 if (unlikely(inode != new))
1170 destroy_inode(new);
1171 }
1172 }
1173 return inode;
1174}
1175EXPORT_SYMBOL(iget5_locked);
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190struct inode *iget_locked(struct super_block *sb, unsigned long ino)
1191{
1192 struct hlist_head *head = inode_hashtable + hash(sb, ino);
1193 struct inode *inode;
1194again:
1195 spin_lock(&inode_hash_lock);
1196 inode = find_inode_fast(sb, head, ino);
1197 spin_unlock(&inode_hash_lock);
1198 if (inode) {
1199 if (IS_ERR(inode))
1200 return NULL;
1201 wait_on_inode(inode);
1202 if (unlikely(inode_unhashed(inode))) {
1203 iput(inode);
1204 goto again;
1205 }
1206 return inode;
1207 }
1208
1209 inode = alloc_inode(sb);
1210 if (inode) {
1211 struct inode *old;
1212
1213 spin_lock(&inode_hash_lock);
1214
1215 old = find_inode_fast(sb, head, ino);
1216 if (!old) {
1217 inode->i_ino = ino;
1218 spin_lock(&inode->i_lock);
1219 inode->i_state = I_NEW;
1220 hlist_add_head_rcu(&inode->i_hash, head);
1221 spin_unlock(&inode->i_lock);
1222 inode_sb_list_add(inode);
1223 spin_unlock(&inode_hash_lock);
1224
1225
1226
1227
1228 return inode;
1229 }
1230
1231
1232
1233
1234
1235
1236 spin_unlock(&inode_hash_lock);
1237 destroy_inode(inode);
1238 if (IS_ERR(old))
1239 return NULL;
1240 inode = old;
1241 wait_on_inode(inode);
1242 if (unlikely(inode_unhashed(inode))) {
1243 iput(inode);
1244 goto again;
1245 }
1246 }
1247 return inode;
1248}
1249EXPORT_SYMBOL(iget_locked);
1250
1251
1252
1253
1254
1255
1256
1257
1258static int test_inode_iunique(struct super_block *sb, unsigned long ino)
1259{
1260 struct hlist_head *b = inode_hashtable + hash(sb, ino);
1261 struct inode *inode;
1262
1263 hlist_for_each_entry_rcu(inode, b, i_hash) {
1264 if (inode->i_ino == ino && inode->i_sb == sb)
1265 return 0;
1266 }
1267 return 1;
1268}
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284ino_t iunique(struct super_block *sb, ino_t max_reserved)
1285{
1286
1287
1288
1289
1290
1291 static DEFINE_SPINLOCK(iunique_lock);
1292 static unsigned int counter;
1293 ino_t res;
1294
1295 rcu_read_lock();
1296 spin_lock(&iunique_lock);
1297 do {
1298 if (counter <= max_reserved)
1299 counter = max_reserved + 1;
1300 res = counter++;
1301 } while (!test_inode_iunique(sb, res));
1302 spin_unlock(&iunique_lock);
1303 rcu_read_unlock();
1304
1305 return res;
1306}
1307EXPORT_SYMBOL(iunique);
1308
1309struct inode *igrab(struct inode *inode)
1310{
1311 spin_lock(&inode->i_lock);
1312 if (!(inode->i_state & (I_FREEING|I_WILL_FREE))) {
1313 __iget(inode);
1314 spin_unlock(&inode->i_lock);
1315 } else {
1316 spin_unlock(&inode->i_lock);
1317
1318
1319
1320
1321
1322 inode = NULL;
1323 }
1324 return inode;
1325}
1326EXPORT_SYMBOL(igrab);
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval,
1345 int (*test)(struct inode *, void *), void *data)
1346{
1347 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1348 struct inode *inode;
1349
1350 spin_lock(&inode_hash_lock);
1351 inode = find_inode(sb, head, test, data);
1352 spin_unlock(&inode_hash_lock);
1353
1354 return IS_ERR(inode) ? NULL : inode;
1355}
1356EXPORT_SYMBOL(ilookup5_nowait);
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
1376 int (*test)(struct inode *, void *), void *data)
1377{
1378 struct inode *inode;
1379again:
1380 inode = ilookup5_nowait(sb, hashval, test, data);
1381 if (inode) {
1382 wait_on_inode(inode);
1383 if (unlikely(inode_unhashed(inode))) {
1384 iput(inode);
1385 goto again;
1386 }
1387 }
1388 return inode;
1389}
1390EXPORT_SYMBOL(ilookup5);
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400struct inode *ilookup(struct super_block *sb, unsigned long ino)
1401{
1402 struct hlist_head *head = inode_hashtable + hash(sb, ino);
1403 struct inode *inode;
1404again:
1405 spin_lock(&inode_hash_lock);
1406 inode = find_inode_fast(sb, head, ino);
1407 spin_unlock(&inode_hash_lock);
1408
1409 if (inode) {
1410 if (IS_ERR(inode))
1411 return NULL;
1412 wait_on_inode(inode);
1413 if (unlikely(inode_unhashed(inode))) {
1414 iput(inode);
1415 goto again;
1416 }
1417 }
1418 return inode;
1419}
1420EXPORT_SYMBOL(ilookup);
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445struct inode *find_inode_nowait(struct super_block *sb,
1446 unsigned long hashval,
1447 int (*match)(struct inode *, unsigned long,
1448 void *),
1449 void *data)
1450{
1451 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1452 struct inode *inode, *ret_inode = NULL;
1453 int mval;
1454
1455 spin_lock(&inode_hash_lock);
1456 hlist_for_each_entry(inode, head, i_hash) {
1457 if (inode->i_sb != sb)
1458 continue;
1459 mval = match(inode, hashval, data);
1460 if (mval == 0)
1461 continue;
1462 if (mval == 1)
1463 ret_inode = inode;
1464 goto out;
1465 }
1466out:
1467 spin_unlock(&inode_hash_lock);
1468 return ret_inode;
1469}
1470EXPORT_SYMBOL(find_inode_nowait);
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493struct inode *find_inode_rcu(struct super_block *sb, unsigned long hashval,
1494 int (*test)(struct inode *, void *), void *data)
1495{
1496 struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1497 struct inode *inode;
1498
1499 RCU_LOCKDEP_WARN(!rcu_read_lock_held(),
1500 "suspicious find_inode_rcu() usage");
1501
1502 hlist_for_each_entry_rcu(inode, head, i_hash) {
1503 if (inode->i_sb == sb &&
1504 !(READ_ONCE(inode->i_state) & (I_FREEING | I_WILL_FREE)) &&
1505 test(inode, data))
1506 return inode;
1507 }
1508 return NULL;
1509}
1510EXPORT_SYMBOL(find_inode_rcu);
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531struct inode *find_inode_by_ino_rcu(struct super_block *sb,
1532 unsigned long ino)
1533{
1534 struct hlist_head *head = inode_hashtable + hash(sb, ino);
1535 struct inode *inode;
1536
1537 RCU_LOCKDEP_WARN(!rcu_read_lock_held(),
1538 "suspicious find_inode_by_ino_rcu() usage");
1539
1540 hlist_for_each_entry_rcu(inode, head, i_hash) {
1541 if (inode->i_ino == ino &&
1542 inode->i_sb == sb &&
1543 !(READ_ONCE(inode->i_state) & (I_FREEING | I_WILL_FREE)))
1544 return inode;
1545 }
1546 return NULL;
1547}
1548EXPORT_SYMBOL(find_inode_by_ino_rcu);
1549
1550int insert_inode_locked(struct inode *inode)
1551{
1552 struct super_block *sb = inode->i_sb;
1553 ino_t ino = inode->i_ino;
1554 struct hlist_head *head = inode_hashtable + hash(sb, ino);
1555
1556 while (1) {
1557 struct inode *old = NULL;
1558 spin_lock(&inode_hash_lock);
1559 hlist_for_each_entry(old, head, i_hash) {
1560 if (old->i_ino != ino)
1561 continue;
1562 if (old->i_sb != sb)
1563 continue;
1564 spin_lock(&old->i_lock);
1565 if (old->i_state & (I_FREEING|I_WILL_FREE)) {
1566 spin_unlock(&old->i_lock);
1567 continue;
1568 }
1569 break;
1570 }
1571 if (likely(!old)) {
1572 spin_lock(&inode->i_lock);
1573 inode->i_state |= I_NEW | I_CREATING;
1574 hlist_add_head_rcu(&inode->i_hash, head);
1575 spin_unlock(&inode->i_lock);
1576 spin_unlock(&inode_hash_lock);
1577 return 0;
1578 }
1579 if (unlikely(old->i_state & I_CREATING)) {
1580 spin_unlock(&old->i_lock);
1581 spin_unlock(&inode_hash_lock);
1582 return -EBUSY;
1583 }
1584 __iget(old);
1585 spin_unlock(&old->i_lock);
1586 spin_unlock(&inode_hash_lock);
1587 wait_on_inode(old);
1588 if (unlikely(!inode_unhashed(old))) {
1589 iput(old);
1590 return -EBUSY;
1591 }
1592 iput(old);
1593 }
1594}
1595EXPORT_SYMBOL(insert_inode_locked);
1596
1597int insert_inode_locked4(struct inode *inode, unsigned long hashval,
1598 int (*test)(struct inode *, void *), void *data)
1599{
1600 struct inode *old;
1601
1602 inode->i_state |= I_CREATING;
1603 old = inode_insert5(inode, hashval, test, NULL, data);
1604
1605 if (old != inode) {
1606 iput(old);
1607 return -EBUSY;
1608 }
1609 return 0;
1610}
1611EXPORT_SYMBOL(insert_inode_locked4);
1612
1613
1614int generic_delete_inode(struct inode *inode)
1615{
1616 return 1;
1617}
1618EXPORT_SYMBOL(generic_delete_inode);
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630static void iput_final(struct inode *inode)
1631{
1632 struct super_block *sb = inode->i_sb;
1633 const struct super_operations *op = inode->i_sb->s_op;
1634 unsigned long state;
1635 int drop;
1636
1637 WARN_ON(inode->i_state & I_NEW);
1638
1639 if (op->drop_inode)
1640 drop = op->drop_inode(inode);
1641 else
1642 drop = generic_drop_inode(inode);
1643
1644 if (!drop &&
1645 !(inode->i_state & I_DONTCACHE) &&
1646 (sb->s_flags & SB_ACTIVE)) {
1647 __inode_add_lru(inode, true);
1648 spin_unlock(&inode->i_lock);
1649 return;
1650 }
1651
1652 state = inode->i_state;
1653 if (!drop) {
1654 WRITE_ONCE(inode->i_state, state | I_WILL_FREE);
1655 spin_unlock(&inode->i_lock);
1656
1657 write_inode_now(inode, 1);
1658
1659 spin_lock(&inode->i_lock);
1660 state = inode->i_state;
1661 WARN_ON(state & I_NEW);
1662 state &= ~I_WILL_FREE;
1663 }
1664
1665 WRITE_ONCE(inode->i_state, state | I_FREEING);
1666 if (!list_empty(&inode->i_lru))
1667 inode_lru_list_del(inode);
1668 spin_unlock(&inode->i_lock);
1669
1670 evict(inode);
1671}
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682void iput(struct inode *inode)
1683{
1684 if (!inode)
1685 return;
1686 BUG_ON(inode->i_state & I_CLEAR);
1687retry:
1688 if (atomic_dec_and_lock(&inode->i_count, &inode->i_lock)) {
1689 if (inode->i_nlink && (inode->i_state & I_DIRTY_TIME)) {
1690 atomic_inc(&inode->i_count);
1691 spin_unlock(&inode->i_lock);
1692 trace_writeback_lazytime_iput(inode);
1693 mark_inode_dirty_sync(inode);
1694 goto retry;
1695 }
1696 iput_final(inode);
1697 }
1698}
1699EXPORT_SYMBOL(iput);
1700
1701#ifdef CONFIG_BLOCK
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716int bmap(struct inode *inode, sector_t *block)
1717{
1718 if (!inode->i_mapping->a_ops->bmap)
1719 return -EINVAL;
1720
1721 *block = inode->i_mapping->a_ops->bmap(inode->i_mapping, *block);
1722 return 0;
1723}
1724EXPORT_SYMBOL(bmap);
1725#endif
1726
1727
1728
1729
1730
1731
1732static int relatime_need_update(struct vfsmount *mnt, struct inode *inode,
1733 struct timespec64 now)
1734{
1735
1736 if (!(mnt->mnt_flags & MNT_RELATIME))
1737 return 1;
1738
1739
1740
1741 if (timespec64_compare(&inode->i_mtime, &inode->i_atime) >= 0)
1742 return 1;
1743
1744
1745
1746 if (timespec64_compare(&inode->i_ctime, &inode->i_atime) >= 0)
1747 return 1;
1748
1749
1750
1751
1752
1753 if ((long)(now.tv_sec - inode->i_atime.tv_sec) >= 24*60*60)
1754 return 1;
1755
1756
1757
1758 return 0;
1759}
1760
1761int generic_update_time(struct inode *inode, struct timespec64 *time, int flags)
1762{
1763 int dirty_flags = 0;
1764
1765 if (flags & (S_ATIME | S_CTIME | S_MTIME)) {
1766 if (flags & S_ATIME)
1767 inode->i_atime = *time;
1768 if (flags & S_CTIME)
1769 inode->i_ctime = *time;
1770 if (flags & S_MTIME)
1771 inode->i_mtime = *time;
1772
1773 if (inode->i_sb->s_flags & SB_LAZYTIME)
1774 dirty_flags |= I_DIRTY_TIME;
1775 else
1776 dirty_flags |= I_DIRTY_SYNC;
1777 }
1778
1779 if ((flags & S_VERSION) && inode_maybe_inc_iversion(inode, false))
1780 dirty_flags |= I_DIRTY_SYNC;
1781
1782 __mark_inode_dirty(inode, dirty_flags);
1783 return 0;
1784}
1785EXPORT_SYMBOL(generic_update_time);
1786
1787
1788
1789
1790
1791int inode_update_time(struct inode *inode, struct timespec64 *time, int flags)
1792{
1793 if (inode->i_op->update_time)
1794 return inode->i_op->update_time(inode, time, flags);
1795 return generic_update_time(inode, time, flags);
1796}
1797EXPORT_SYMBOL(inode_update_time);
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808bool atime_needs_update(const struct path *path, struct inode *inode)
1809{
1810 struct vfsmount *mnt = path->mnt;
1811 struct timespec64 now;
1812
1813 if (inode->i_flags & S_NOATIME)
1814 return false;
1815
1816
1817
1818
1819 if (HAS_UNMAPPED_ID(mnt_user_ns(mnt), inode))
1820 return false;
1821
1822 if (IS_NOATIME(inode))
1823 return false;
1824 if ((inode->i_sb->s_flags & SB_NODIRATIME) && S_ISDIR(inode->i_mode))
1825 return false;
1826
1827 if (mnt->mnt_flags & MNT_NOATIME)
1828 return false;
1829 if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
1830 return false;
1831
1832 now = current_time(inode);
1833
1834 if (!relatime_need_update(mnt, inode, now))
1835 return false;
1836
1837 if (timespec64_equal(&inode->i_atime, &now))
1838 return false;
1839
1840 return true;
1841}
1842
1843void touch_atime(const struct path *path)
1844{
1845 struct vfsmount *mnt = path->mnt;
1846 struct inode *inode = d_inode(path->dentry);
1847 struct timespec64 now;
1848
1849 if (!atime_needs_update(path, inode))
1850 return;
1851
1852 if (!sb_start_write_trylock(inode->i_sb))
1853 return;
1854
1855 if (__mnt_want_write(mnt) != 0)
1856 goto skip_update;
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866 now = current_time(inode);
1867 inode_update_time(inode, &now, S_ATIME);
1868 __mnt_drop_write(mnt);
1869skip_update:
1870 sb_end_write(inode->i_sb);
1871}
1872EXPORT_SYMBOL(touch_atime);
1873
1874
1875
1876
1877
1878
1879
1880int should_remove_suid(struct dentry *dentry)
1881{
1882 umode_t mode = d_inode(dentry)->i_mode;
1883 int kill = 0;
1884
1885
1886 if (unlikely(mode & S_ISUID))
1887 kill = ATTR_KILL_SUID;
1888
1889
1890
1891
1892
1893 if (unlikely((mode & S_ISGID) && (mode & S_IXGRP)))
1894 kill |= ATTR_KILL_SGID;
1895
1896 if (unlikely(kill && !capable(CAP_FSETID) && S_ISREG(mode)))
1897 return kill;
1898
1899 return 0;
1900}
1901EXPORT_SYMBOL(should_remove_suid);
1902
1903
1904
1905
1906
1907
1908int dentry_needs_remove_privs(struct dentry *dentry)
1909{
1910 struct inode *inode = d_inode(dentry);
1911 int mask = 0;
1912 int ret;
1913
1914 if (IS_NOSEC(inode))
1915 return 0;
1916
1917 mask = should_remove_suid(dentry);
1918 ret = security_inode_need_killpriv(dentry);
1919 if (ret < 0)
1920 return ret;
1921 if (ret)
1922 mask |= ATTR_KILL_PRIV;
1923 return mask;
1924}
1925
1926static int __remove_privs(struct user_namespace *mnt_userns,
1927 struct dentry *dentry, int kill)
1928{
1929 struct iattr newattrs;
1930
1931 newattrs.ia_valid = ATTR_FORCE | kill;
1932
1933
1934
1935
1936 return notify_change(mnt_userns, dentry, &newattrs, NULL);
1937}
1938
1939
1940
1941
1942
1943int file_remove_privs(struct file *file)
1944{
1945 struct dentry *dentry = file_dentry(file);
1946 struct inode *inode = file_inode(file);
1947 int kill;
1948 int error = 0;
1949
1950
1951
1952
1953
1954
1955
1956 if (IS_NOSEC(inode) || !S_ISREG(inode->i_mode))
1957 return 0;
1958
1959 kill = dentry_needs_remove_privs(dentry);
1960 if (kill < 0)
1961 return kill;
1962 if (kill)
1963 error = __remove_privs(file_mnt_user_ns(file), dentry, kill);
1964 if (!error)
1965 inode_has_no_xattr(inode);
1966
1967 return error;
1968}
1969EXPORT_SYMBOL(file_remove_privs);
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984int file_update_time(struct file *file)
1985{
1986 struct inode *inode = file_inode(file);
1987 struct timespec64 now;
1988 int sync_it = 0;
1989 int ret;
1990
1991
1992 if (IS_NOCMTIME(inode))
1993 return 0;
1994
1995 now = current_time(inode);
1996 if (!timespec64_equal(&inode->i_mtime, &now))
1997 sync_it = S_MTIME;
1998
1999 if (!timespec64_equal(&inode->i_ctime, &now))
2000 sync_it |= S_CTIME;
2001
2002 if (IS_I_VERSION(inode) && inode_iversion_need_inc(inode))
2003 sync_it |= S_VERSION;
2004
2005 if (!sync_it)
2006 return 0;
2007
2008
2009 if (__mnt_want_write_file(file))
2010 return 0;
2011
2012 ret = inode_update_time(inode, &now, sync_it);
2013 __mnt_drop_write_file(file);
2014
2015 return ret;
2016}
2017EXPORT_SYMBOL(file_update_time);
2018
2019
2020int file_modified(struct file *file)
2021{
2022 int err;
2023
2024
2025
2026
2027
2028 err = file_remove_privs(file);
2029 if (err)
2030 return err;
2031
2032 if (unlikely(file->f_mode & FMODE_NOCMTIME))
2033 return 0;
2034
2035 return file_update_time(file);
2036}
2037EXPORT_SYMBOL(file_modified);
2038
2039int inode_needs_sync(struct inode *inode)
2040{
2041 if (IS_SYNC(inode))
2042 return 1;
2043 if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
2044 return 1;
2045 return 0;
2046}
2047EXPORT_SYMBOL(inode_needs_sync);
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060static void __wait_on_freeing_inode(struct inode *inode)
2061{
2062 wait_queue_head_t *wq;
2063 DEFINE_WAIT_BIT(wait, &inode->i_state, __I_NEW);
2064 wq = bit_waitqueue(&inode->i_state, __I_NEW);
2065 prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
2066 spin_unlock(&inode->i_lock);
2067 spin_unlock(&inode_hash_lock);
2068 schedule();
2069 finish_wait(wq, &wait.wq_entry);
2070 spin_lock(&inode_hash_lock);
2071}
2072
2073static __initdata unsigned long ihash_entries;
2074static int __init set_ihash_entries(char *str)
2075{
2076 if (!str)
2077 return 0;
2078 ihash_entries = simple_strtoul(str, &str, 0);
2079 return 1;
2080}
2081__setup("ihash_entries=", set_ihash_entries);
2082
2083
2084
2085
2086void __init inode_init_early(void)
2087{
2088
2089
2090
2091 if (hashdist)
2092 return;
2093
2094 inode_hashtable =
2095 alloc_large_system_hash("Inode-cache",
2096 sizeof(struct hlist_head),
2097 ihash_entries,
2098 14,
2099 HASH_EARLY | HASH_ZERO,
2100 &i_hash_shift,
2101 &i_hash_mask,
2102 0,
2103 0);
2104}
2105
2106void __init inode_init(void)
2107{
2108
2109 inode_cachep = kmem_cache_create("inode_cache",
2110 sizeof(struct inode),
2111 0,
2112 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
2113 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
2114 init_once);
2115
2116
2117 if (!hashdist)
2118 return;
2119
2120 inode_hashtable =
2121 alloc_large_system_hash("Inode-cache",
2122 sizeof(struct hlist_head),
2123 ihash_entries,
2124 14,
2125 HASH_ZERO,
2126 &i_hash_shift,
2127 &i_hash_mask,
2128 0,
2129 0);
2130}
2131
2132void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
2133{
2134 inode->i_mode = mode;
2135 if (S_ISCHR(mode)) {
2136 inode->i_fop = &def_chr_fops;
2137 inode->i_rdev = rdev;
2138 } else if (S_ISBLK(mode)) {
2139 inode->i_fop = &def_blk_fops;
2140 inode->i_rdev = rdev;
2141 } else if (S_ISFIFO(mode))
2142 inode->i_fop = &pipefifo_fops;
2143 else if (S_ISSOCK(mode))
2144 ;
2145 else
2146 printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o) for"
2147 " inode %s:%lu\n", mode, inode->i_sb->s_id,
2148 inode->i_ino);
2149}
2150EXPORT_SYMBOL(init_special_inode);
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165void inode_init_owner(struct user_namespace *mnt_userns, struct inode *inode,
2166 const struct inode *dir, umode_t mode)
2167{
2168 inode_fsuid_set(inode, mnt_userns);
2169 if (dir && dir->i_mode & S_ISGID) {
2170 inode->i_gid = dir->i_gid;
2171
2172
2173 if (S_ISDIR(mode))
2174 mode |= S_ISGID;
2175 else if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP) &&
2176 !in_group_p(i_gid_into_mnt(mnt_userns, dir)) &&
2177 !capable_wrt_inode_uidgid(mnt_userns, dir, CAP_FSETID))
2178 mode &= ~S_ISGID;
2179 } else
2180 inode_fsgid_set(inode, mnt_userns);
2181 inode->i_mode = mode;
2182}
2183EXPORT_SYMBOL(inode_init_owner);
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199bool inode_owner_or_capable(struct user_namespace *mnt_userns,
2200 const struct inode *inode)
2201{
2202 kuid_t i_uid;
2203 struct user_namespace *ns;
2204
2205 i_uid = i_uid_into_mnt(mnt_userns, inode);
2206 if (uid_eq(current_fsuid(), i_uid))
2207 return true;
2208
2209 ns = current_user_ns();
2210 if (kuid_has_mapping(ns, i_uid) && ns_capable(ns, CAP_FOWNER))
2211 return true;
2212 return false;
2213}
2214EXPORT_SYMBOL(inode_owner_or_capable);
2215
2216
2217
2218
2219static void __inode_dio_wait(struct inode *inode)
2220{
2221 wait_queue_head_t *wq = bit_waitqueue(&inode->i_state, __I_DIO_WAKEUP);
2222 DEFINE_WAIT_BIT(q, &inode->i_state, __I_DIO_WAKEUP);
2223
2224 do {
2225 prepare_to_wait(wq, &q.wq_entry, TASK_UNINTERRUPTIBLE);
2226 if (atomic_read(&inode->i_dio_count))
2227 schedule();
2228 } while (atomic_read(&inode->i_dio_count));
2229 finish_wait(wq, &q.wq_entry);
2230}
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242void inode_dio_wait(struct inode *inode)
2243{
2244 if (atomic_read(&inode->i_dio_count))
2245 __inode_dio_wait(inode);
2246}
2247EXPORT_SYMBOL(inode_dio_wait);
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265void inode_set_flags(struct inode *inode, unsigned int flags,
2266 unsigned int mask)
2267{
2268 WARN_ON_ONCE(flags & ~mask);
2269 set_mask_bits(&inode->i_flags, mask, flags);
2270}
2271EXPORT_SYMBOL(inode_set_flags);
2272
2273void inode_nohighmem(struct inode *inode)
2274{
2275 mapping_set_gfp_mask(inode->i_mapping, GFP_USER);
2276}
2277EXPORT_SYMBOL(inode_nohighmem);
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288struct timespec64 timestamp_truncate(struct timespec64 t, struct inode *inode)
2289{
2290 struct super_block *sb = inode->i_sb;
2291 unsigned int gran = sb->s_time_gran;
2292
2293 t.tv_sec = clamp(t.tv_sec, sb->s_time_min, sb->s_time_max);
2294 if (unlikely(t.tv_sec == sb->s_time_max || t.tv_sec == sb->s_time_min))
2295 t.tv_nsec = 0;
2296
2297
2298 if (gran == 1)
2299 ;
2300 else if (gran == NSEC_PER_SEC)
2301 t.tv_nsec = 0;
2302 else if (gran > 1 && gran < NSEC_PER_SEC)
2303 t.tv_nsec -= t.tv_nsec % gran;
2304 else
2305 WARN(1, "invalid file time granularity: %u", gran);
2306 return t;
2307}
2308EXPORT_SYMBOL(timestamp_truncate);
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320struct timespec64 current_time(struct inode *inode)
2321{
2322 struct timespec64 now;
2323
2324 ktime_get_coarse_real_ts64(&now);
2325
2326 if (unlikely(!inode->i_sb)) {
2327 WARN(1, "current_time() called with uninitialized super_block in the inode");
2328 return now;
2329 }
2330
2331 return timestamp_truncate(now, inode);
2332}
2333EXPORT_SYMBOL(current_time);
2334