]> Git Repo - linux.git/blob - fs/inode.c
drm/vc4: Run DRM default client setup
[linux.git] / fs / inode.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * (C) 1997 Linus Torvalds
4  * (C) 1999 Andrea Arcangeli <[email protected]> (dynamic inode allocation)
5  */
6 #include <linux/export.h>
7 #include <linux/fs.h>
8 #include <linux/filelock.h>
9 #include <linux/mm.h>
10 #include <linux/backing-dev.h>
11 #include <linux/hash.h>
12 #include <linux/swap.h>
13 #include <linux/security.h>
14 #include <linux/cdev.h>
15 #include <linux/memblock.h>
16 #include <linux/fsnotify.h>
17 #include <linux/mount.h>
18 #include <linux/posix_acl.h>
19 #include <linux/buffer_head.h> /* for inode_has_buffers */
20 #include <linux/ratelimit.h>
21 #include <linux/list_lru.h>
22 #include <linux/iversion.h>
23 #include <linux/rw_hint.h>
24 #include <trace/events/writeback.h>
25 #include "internal.h"
26
27 /*
28  * Inode locking rules:
29  *
30  * inode->i_lock protects:
31  *   inode->i_state, inode->i_hash, __iget(), inode->i_io_list
32  * Inode LRU list locks protect:
33  *   inode->i_sb->s_inode_lru, inode->i_lru
34  * inode->i_sb->s_inode_list_lock protects:
35  *   inode->i_sb->s_inodes, inode->i_sb_list
36  * bdi->wb.list_lock protects:
37  *   bdi->wb.b_{dirty,io,more_io,dirty_time}, inode->i_io_list
38  * inode_hash_lock protects:
39  *   inode_hashtable, inode->i_hash
40  *
41  * Lock ordering:
42  *
43  * inode->i_sb->s_inode_list_lock
44  *   inode->i_lock
45  *     Inode LRU list locks
46  *
47  * bdi->wb.list_lock
48  *   inode->i_lock
49  *
50  * inode_hash_lock
51  *   inode->i_sb->s_inode_list_lock
52  *   inode->i_lock
53  *
54  * iunique_lock
55  *   inode_hash_lock
56  */
57
58 static unsigned int i_hash_mask __ro_after_init;
59 static unsigned int i_hash_shift __ro_after_init;
60 static struct hlist_head *inode_hashtable __ro_after_init;
61 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(inode_hash_lock);
62
63 /*
64  * Empty aops. Can be used for the cases where the user does not
65  * define any of the address_space operations.
66  */
67 const struct address_space_operations empty_aops = {
68 };
69 EXPORT_SYMBOL(empty_aops);
70
71 static DEFINE_PER_CPU(unsigned long, nr_inodes);
72 static DEFINE_PER_CPU(unsigned long, nr_unused);
73
74 static struct kmem_cache *inode_cachep __ro_after_init;
75
76 static long get_nr_inodes(void)
77 {
78         int i;
79         long sum = 0;
80         for_each_possible_cpu(i)
81                 sum += per_cpu(nr_inodes, i);
82         return sum < 0 ? 0 : sum;
83 }
84
85 static inline long get_nr_inodes_unused(void)
86 {
87         int i;
88         long sum = 0;
89         for_each_possible_cpu(i)
90                 sum += per_cpu(nr_unused, i);
91         return sum < 0 ? 0 : sum;
92 }
93
94 long get_nr_dirty_inodes(void)
95 {
96         /* not actually dirty inodes, but a wild approximation */
97         long nr_dirty = get_nr_inodes() - get_nr_inodes_unused();
98         return nr_dirty > 0 ? nr_dirty : 0;
99 }
100
101 /*
102  * Handle nr_inode sysctl
103  */
104 #ifdef CONFIG_SYSCTL
105 /*
106  * Statistics gathering..
107  */
108 static struct inodes_stat_t inodes_stat;
109
110 static int proc_nr_inodes(const struct ctl_table *table, int write, void *buffer,
111                           size_t *lenp, loff_t *ppos)
112 {
113         inodes_stat.nr_inodes = get_nr_inodes();
114         inodes_stat.nr_unused = get_nr_inodes_unused();
115         return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
116 }
117
118 static struct ctl_table inodes_sysctls[] = {
119         {
120                 .procname       = "inode-nr",
121                 .data           = &inodes_stat,
122                 .maxlen         = 2*sizeof(long),
123                 .mode           = 0444,
124                 .proc_handler   = proc_nr_inodes,
125         },
126         {
127                 .procname       = "inode-state",
128                 .data           = &inodes_stat,
129                 .maxlen         = 7*sizeof(long),
130                 .mode           = 0444,
131                 .proc_handler   = proc_nr_inodes,
132         },
133 };
134
135 static int __init init_fs_inode_sysctls(void)
136 {
137         register_sysctl_init("fs", inodes_sysctls);
138         return 0;
139 }
140 early_initcall(init_fs_inode_sysctls);
141 #endif
142
143 static int no_open(struct inode *inode, struct file *file)
144 {
145         return -ENXIO;
146 }
147
148 /**
149  * inode_init_always - perform inode structure initialisation
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 inode_operations empty_iops;
159         static const struct file_operations no_open_fops = {.open = no_open};
160         struct address_space *const mapping = &inode->i_data;
161
162         inode->i_sb = sb;
163         inode->i_blkbits = sb->s_blocksize_bits;
164         inode->i_flags = 0;
165         inode->i_state = 0;
166         atomic64_set(&inode->i_sequence, 0);
167         atomic_set(&inode->i_count, 1);
168         inode->i_op = &empty_iops;
169         inode->i_fop = &no_open_fops;
170         inode->i_ino = 0;
171         inode->__i_nlink = 1;
172         inode->i_opflags = 0;
173         if (sb->s_xattr)
174                 inode->i_opflags |= IOP_XATTR;
175         i_uid_write(inode, 0);
176         i_gid_write(inode, 0);
177         atomic_set(&inode->i_writecount, 0);
178         inode->i_size = 0;
179         inode->i_write_hint = WRITE_LIFE_NOT_SET;
180         inode->i_blocks = 0;
181         inode->i_bytes = 0;
182         inode->i_generation = 0;
183         inode->i_pipe = NULL;
184         inode->i_cdev = NULL;
185         inode->i_link = NULL;
186         inode->i_dir_seq = 0;
187         inode->i_rdev = 0;
188         inode->dirtied_when = 0;
189
190 #ifdef CONFIG_CGROUP_WRITEBACK
191         inode->i_wb_frn_winner = 0;
192         inode->i_wb_frn_avg_time = 0;
193         inode->i_wb_frn_history = 0;
194 #endif
195
196         spin_lock_init(&inode->i_lock);
197         lockdep_set_class(&inode->i_lock, &sb->s_type->i_lock_key);
198
199         init_rwsem(&inode->i_rwsem);
200         lockdep_set_class(&inode->i_rwsem, &sb->s_type->i_mutex_key);
201
202         atomic_set(&inode->i_dio_count, 0);
203
204         mapping->a_ops = &empty_aops;
205         mapping->host = inode;
206         mapping->flags = 0;
207         mapping->wb_err = 0;
208         atomic_set(&mapping->i_mmap_writable, 0);
209 #ifdef CONFIG_READ_ONLY_THP_FOR_FS
210         atomic_set(&mapping->nr_thps, 0);
211 #endif
212         mapping_set_gfp_mask(mapping, GFP_HIGHUSER_MOVABLE);
213         mapping->i_private_data = NULL;
214         mapping->writeback_index = 0;
215         init_rwsem(&mapping->invalidate_lock);
216         lockdep_set_class_and_name(&mapping->invalidate_lock,
217                                    &sb->s_type->invalidate_lock_key,
218                                    "mapping.invalidate_lock");
219         if (sb->s_iflags & SB_I_STABLE_WRITES)
220                 mapping_set_stable_writes(mapping);
221         inode->i_private = NULL;
222         inode->i_mapping = mapping;
223         INIT_HLIST_HEAD(&inode->i_dentry);      /* buggered by rcu freeing */
224 #ifdef CONFIG_FS_POSIX_ACL
225         inode->i_acl = inode->i_default_acl = ACL_NOT_CACHED;
226 #endif
227
228 #ifdef CONFIG_FSNOTIFY
229         inode->i_fsnotify_mask = 0;
230 #endif
231         inode->i_flctx = NULL;
232
233         if (unlikely(security_inode_alloc(inode)))
234                 return -ENOMEM;
235
236         this_cpu_inc(nr_inodes);
237
238         return 0;
239 }
240 EXPORT_SYMBOL(inode_init_always);
241
242 void free_inode_nonrcu(struct inode *inode)
243 {
244         kmem_cache_free(inode_cachep, inode);
245 }
246 EXPORT_SYMBOL(free_inode_nonrcu);
247
248 static void i_callback(struct rcu_head *head)
249 {
250         struct inode *inode = container_of(head, struct inode, i_rcu);
251         if (inode->free_inode)
252                 inode->free_inode(inode);
253         else
254                 free_inode_nonrcu(inode);
255 }
256
257 static struct inode *alloc_inode(struct super_block *sb)
258 {
259         const struct super_operations *ops = sb->s_op;
260         struct inode *inode;
261
262         if (ops->alloc_inode)
263                 inode = ops->alloc_inode(sb);
264         else
265                 inode = alloc_inode_sb(sb, inode_cachep, GFP_KERNEL);
266
267         if (!inode)
268                 return NULL;
269
270         if (unlikely(inode_init_always(sb, inode))) {
271                 if (ops->destroy_inode) {
272                         ops->destroy_inode(inode);
273                         if (!ops->free_inode)
274                                 return NULL;
275                 }
276                 inode->free_inode = ops->free_inode;
277                 i_callback(&inode->i_rcu);
278                 return NULL;
279         }
280
281         return inode;
282 }
283
284 void __destroy_inode(struct inode *inode)
285 {
286         BUG_ON(inode_has_buffers(inode));
287         inode_detach_wb(inode);
288         security_inode_free(inode);
289         fsnotify_inode_delete(inode);
290         locks_free_lock_context(inode);
291         if (!inode->i_nlink) {
292                 WARN_ON(atomic_long_read(&inode->i_sb->s_remove_count) == 0);
293                 atomic_long_dec(&inode->i_sb->s_remove_count);
294         }
295
296 #ifdef CONFIG_FS_POSIX_ACL
297         if (inode->i_acl && !is_uncached_acl(inode->i_acl))
298                 posix_acl_release(inode->i_acl);
299         if (inode->i_default_acl && !is_uncached_acl(inode->i_default_acl))
300                 posix_acl_release(inode->i_default_acl);
301 #endif
302         this_cpu_dec(nr_inodes);
303 }
304 EXPORT_SYMBOL(__destroy_inode);
305
306 static void destroy_inode(struct inode *inode)
307 {
308         const struct super_operations *ops = inode->i_sb->s_op;
309
310         BUG_ON(!list_empty(&inode->i_lru));
311         __destroy_inode(inode);
312         if (ops->destroy_inode) {
313                 ops->destroy_inode(inode);
314                 if (!ops->free_inode)
315                         return;
316         }
317         inode->free_inode = ops->free_inode;
318         call_rcu(&inode->i_rcu, i_callback);
319 }
320
321 /**
322  * drop_nlink - directly drop an inode's link count
323  * @inode: inode
324  *
325  * This is a low-level filesystem helper to replace any
326  * direct filesystem manipulation of i_nlink.  In cases
327  * where we are attempting to track writes to the
328  * filesystem, a decrement to zero means an imminent
329  * write when the file is truncated and actually unlinked
330  * on the filesystem.
331  */
332 void drop_nlink(struct inode *inode)
333 {
334         WARN_ON(inode->i_nlink == 0);
335         inode->__i_nlink--;
336         if (!inode->i_nlink)
337                 atomic_long_inc(&inode->i_sb->s_remove_count);
338 }
339 EXPORT_SYMBOL(drop_nlink);
340
341 /**
342  * clear_nlink - directly zero an inode's link count
343  * @inode: inode
344  *
345  * This is a low-level filesystem helper to replace any
346  * direct filesystem manipulation of i_nlink.  See
347  * drop_nlink() for why we care about i_nlink hitting zero.
348  */
349 void clear_nlink(struct inode *inode)
350 {
351         if (inode->i_nlink) {
352                 inode->__i_nlink = 0;
353                 atomic_long_inc(&inode->i_sb->s_remove_count);
354         }
355 }
356 EXPORT_SYMBOL(clear_nlink);
357
358 /**
359  * set_nlink - directly set an inode's link count
360  * @inode: inode
361  * @nlink: new nlink (should be non-zero)
362  *
363  * This is a low-level filesystem helper to replace any
364  * direct filesystem manipulation of i_nlink.
365  */
366 void set_nlink(struct inode *inode, unsigned int nlink)
367 {
368         if (!nlink) {
369                 clear_nlink(inode);
370         } else {
371                 /* Yes, some filesystems do change nlink from zero to one */
372                 if (inode->i_nlink == 0)
373                         atomic_long_dec(&inode->i_sb->s_remove_count);
374
375                 inode->__i_nlink = nlink;
376         }
377 }
378 EXPORT_SYMBOL(set_nlink);
379
380 /**
381  * inc_nlink - directly increment an inode's link count
382  * @inode: inode
383  *
384  * This is a low-level filesystem helper to replace any
385  * direct filesystem manipulation of i_nlink.  Currently,
386  * it is only here for parity with dec_nlink().
387  */
388 void inc_nlink(struct inode *inode)
389 {
390         if (unlikely(inode->i_nlink == 0)) {
391                 WARN_ON(!(inode->i_state & I_LINKABLE));
392                 atomic_long_dec(&inode->i_sb->s_remove_count);
393         }
394
395         inode->__i_nlink++;
396 }
397 EXPORT_SYMBOL(inc_nlink);
398
399 static void __address_space_init_once(struct address_space *mapping)
400 {
401         xa_init_flags(&mapping->i_pages, XA_FLAGS_LOCK_IRQ | XA_FLAGS_ACCOUNT);
402         init_rwsem(&mapping->i_mmap_rwsem);
403         INIT_LIST_HEAD(&mapping->i_private_list);
404         spin_lock_init(&mapping->i_private_lock);
405         mapping->i_mmap = RB_ROOT_CACHED;
406 }
407
408 void address_space_init_once(struct address_space *mapping)
409 {
410         memset(mapping, 0, sizeof(*mapping));
411         __address_space_init_once(mapping);
412 }
413 EXPORT_SYMBOL(address_space_init_once);
414
415 /*
416  * These are initializations that only need to be done
417  * once, because the fields are idempotent across use
418  * of the inode, so let the slab aware of that.
419  */
420 void inode_init_once(struct inode *inode)
421 {
422         memset(inode, 0, sizeof(*inode));
423         INIT_HLIST_NODE(&inode->i_hash);
424         INIT_LIST_HEAD(&inode->i_devices);
425         INIT_LIST_HEAD(&inode->i_io_list);
426         INIT_LIST_HEAD(&inode->i_wb_list);
427         INIT_LIST_HEAD(&inode->i_lru);
428         INIT_LIST_HEAD(&inode->i_sb_list);
429         __address_space_init_once(&inode->i_data);
430         i_size_ordered_init(inode);
431 }
432 EXPORT_SYMBOL(inode_init_once);
433
434 static void init_once(void *foo)
435 {
436         struct inode *inode = (struct inode *) foo;
437
438         inode_init_once(inode);
439 }
440
441 /*
442  * inode->i_lock must be held
443  */
444 void __iget(struct inode *inode)
445 {
446         atomic_inc(&inode->i_count);
447 }
448
449 /*
450  * get additional reference to inode; caller must already hold one.
451  */
452 void ihold(struct inode *inode)
453 {
454         WARN_ON(atomic_inc_return(&inode->i_count) < 2);
455 }
456 EXPORT_SYMBOL(ihold);
457
458 static void __inode_add_lru(struct inode *inode, bool rotate)
459 {
460         if (inode->i_state & (I_DIRTY_ALL | I_SYNC | I_FREEING | I_WILL_FREE))
461                 return;
462         if (atomic_read(&inode->i_count))
463                 return;
464         if (!(inode->i_sb->s_flags & SB_ACTIVE))
465                 return;
466         if (!mapping_shrinkable(&inode->i_data))
467                 return;
468
469         if (list_lru_add_obj(&inode->i_sb->s_inode_lru, &inode->i_lru))
470                 this_cpu_inc(nr_unused);
471         else if (rotate)
472                 inode->i_state |= I_REFERENCED;
473 }
474
475 /*
476  * Add inode to LRU if needed (inode is unused and clean).
477  *
478  * Needs inode->i_lock held.
479  */
480 void inode_add_lru(struct inode *inode)
481 {
482         __inode_add_lru(inode, false);
483 }
484
485 static void inode_lru_list_del(struct inode *inode)
486 {
487         if (list_lru_del_obj(&inode->i_sb->s_inode_lru, &inode->i_lru))
488                 this_cpu_dec(nr_unused);
489 }
490
491 static void inode_pin_lru_isolating(struct inode *inode)
492 {
493         lockdep_assert_held(&inode->i_lock);
494         WARN_ON(inode->i_state & (I_LRU_ISOLATING | I_FREEING | I_WILL_FREE));
495         inode->i_state |= I_LRU_ISOLATING;
496 }
497
498 static void inode_unpin_lru_isolating(struct inode *inode)
499 {
500         spin_lock(&inode->i_lock);
501         WARN_ON(!(inode->i_state & I_LRU_ISOLATING));
502         inode->i_state &= ~I_LRU_ISOLATING;
503         smp_mb();
504         wake_up_bit(&inode->i_state, __I_LRU_ISOLATING);
505         spin_unlock(&inode->i_lock);
506 }
507
508 static void inode_wait_for_lru_isolating(struct inode *inode)
509 {
510         spin_lock(&inode->i_lock);
511         if (inode->i_state & I_LRU_ISOLATING) {
512                 DEFINE_WAIT_BIT(wq, &inode->i_state, __I_LRU_ISOLATING);
513                 wait_queue_head_t *wqh;
514
515                 wqh = bit_waitqueue(&inode->i_state, __I_LRU_ISOLATING);
516                 spin_unlock(&inode->i_lock);
517                 __wait_on_bit(wqh, &wq, bit_wait, TASK_UNINTERRUPTIBLE);
518                 spin_lock(&inode->i_lock);
519                 WARN_ON(inode->i_state & I_LRU_ISOLATING);
520         }
521         spin_unlock(&inode->i_lock);
522 }
523
524 /**
525  * inode_sb_list_add - add inode to the superblock list of inodes
526  * @inode: inode to add
527  */
528 void inode_sb_list_add(struct inode *inode)
529 {
530         spin_lock(&inode->i_sb->s_inode_list_lock);
531         list_add(&inode->i_sb_list, &inode->i_sb->s_inodes);
532         spin_unlock(&inode->i_sb->s_inode_list_lock);
533 }
534 EXPORT_SYMBOL_GPL(inode_sb_list_add);
535
536 static inline void inode_sb_list_del(struct inode *inode)
537 {
538         if (!list_empty(&inode->i_sb_list)) {
539                 spin_lock(&inode->i_sb->s_inode_list_lock);
540                 list_del_init(&inode->i_sb_list);
541                 spin_unlock(&inode->i_sb->s_inode_list_lock);
542         }
543 }
544
545 static unsigned long hash(struct super_block *sb, unsigned long hashval)
546 {
547         unsigned long tmp;
548
549         tmp = (hashval * (unsigned long)sb) ^ (GOLDEN_RATIO_PRIME + hashval) /
550                         L1_CACHE_BYTES;
551         tmp = tmp ^ ((tmp ^ GOLDEN_RATIO_PRIME) >> i_hash_shift);
552         return tmp & i_hash_mask;
553 }
554
555 /**
556  *      __insert_inode_hash - hash an inode
557  *      @inode: unhashed inode
558  *      @hashval: unsigned long value used to locate this object in the
559  *              inode_hashtable.
560  *
561  *      Add an inode to the inode hash for this superblock.
562  */
563 void __insert_inode_hash(struct inode *inode, unsigned long hashval)
564 {
565         struct hlist_head *b = inode_hashtable + hash(inode->i_sb, hashval);
566
567         spin_lock(&inode_hash_lock);
568         spin_lock(&inode->i_lock);
569         hlist_add_head_rcu(&inode->i_hash, b);
570         spin_unlock(&inode->i_lock);
571         spin_unlock(&inode_hash_lock);
572 }
573 EXPORT_SYMBOL(__insert_inode_hash);
574
575 /**
576  *      __remove_inode_hash - remove an inode from the hash
577  *      @inode: inode to unhash
578  *
579  *      Remove an inode from the superblock.
580  */
581 void __remove_inode_hash(struct inode *inode)
582 {
583         spin_lock(&inode_hash_lock);
584         spin_lock(&inode->i_lock);
585         hlist_del_init_rcu(&inode->i_hash);
586         spin_unlock(&inode->i_lock);
587         spin_unlock(&inode_hash_lock);
588 }
589 EXPORT_SYMBOL(__remove_inode_hash);
590
591 void dump_mapping(const struct address_space *mapping)
592 {
593         struct inode *host;
594         const struct address_space_operations *a_ops;
595         struct hlist_node *dentry_first;
596         struct dentry *dentry_ptr;
597         struct dentry dentry;
598         unsigned long ino;
599
600         /*
601          * If mapping is an invalid pointer, we don't want to crash
602          * accessing it, so probe everything depending on it carefully.
603          */
604         if (get_kernel_nofault(host, &mapping->host) ||
605             get_kernel_nofault(a_ops, &mapping->a_ops)) {
606                 pr_warn("invalid mapping:%px\n", mapping);
607                 return;
608         }
609
610         if (!host) {
611                 pr_warn("aops:%ps\n", a_ops);
612                 return;
613         }
614
615         if (get_kernel_nofault(dentry_first, &host->i_dentry.first) ||
616             get_kernel_nofault(ino, &host->i_ino)) {
617                 pr_warn("aops:%ps invalid inode:%px\n", a_ops, host);
618                 return;
619         }
620
621         if (!dentry_first) {
622                 pr_warn("aops:%ps ino:%lx\n", a_ops, ino);
623                 return;
624         }
625
626         dentry_ptr = container_of(dentry_first, struct dentry, d_u.d_alias);
627         if (get_kernel_nofault(dentry, dentry_ptr) ||
628             !dentry.d_parent || !dentry.d_name.name) {
629                 pr_warn("aops:%ps ino:%lx invalid dentry:%px\n",
630                                 a_ops, ino, dentry_ptr);
631                 return;
632         }
633
634         /*
635          * if dentry is corrupted, the %pd handler may still crash,
636          * but it's unlikely that we reach here with a corrupt mapping
637          */
638         pr_warn("aops:%ps ino:%lx dentry name:\"%pd\"\n", a_ops, ino, &dentry);
639 }
640
641 void clear_inode(struct inode *inode)
642 {
643         /*
644          * We have to cycle the i_pages lock here because reclaim can be in the
645          * process of removing the last page (in __filemap_remove_folio())
646          * and we must not free the mapping under it.
647          */
648         xa_lock_irq(&inode->i_data.i_pages);
649         BUG_ON(inode->i_data.nrpages);
650         /*
651          * Almost always, mapping_empty(&inode->i_data) here; but there are
652          * two known and long-standing ways in which nodes may get left behind
653          * (when deep radix-tree node allocation failed partway; or when THP
654          * collapse_file() failed). Until those two known cases are cleaned up,
655          * or a cleanup function is called here, do not BUG_ON(!mapping_empty),
656          * nor even WARN_ON(!mapping_empty).
657          */
658         xa_unlock_irq(&inode->i_data.i_pages);
659         BUG_ON(!list_empty(&inode->i_data.i_private_list));
660         BUG_ON(!(inode->i_state & I_FREEING));
661         BUG_ON(inode->i_state & I_CLEAR);
662         BUG_ON(!list_empty(&inode->i_wb_list));
663         /* don't need i_lock here, no concurrent mods to i_state */
664         inode->i_state = I_FREEING | I_CLEAR;
665 }
666 EXPORT_SYMBOL(clear_inode);
667
668 /*
669  * Free the inode passed in, removing it from the lists it is still connected
670  * to. We remove any pages still attached to the inode and wait for any IO that
671  * is still in progress before finally destroying the inode.
672  *
673  * An inode must already be marked I_FREEING so that we avoid the inode being
674  * moved back onto lists if we race with other code that manipulates the lists
675  * (e.g. writeback_single_inode). The caller is responsible for setting this.
676  *
677  * An inode must already be removed from the LRU list before being evicted from
678  * the cache. This should occur atomically with setting the I_FREEING state
679  * flag, so no inodes here should ever be on the LRU when being evicted.
680  */
681 static void evict(struct inode *inode)
682 {
683         const struct super_operations *op = inode->i_sb->s_op;
684
685         BUG_ON(!(inode->i_state & I_FREEING));
686         BUG_ON(!list_empty(&inode->i_lru));
687
688         if (!list_empty(&inode->i_io_list))
689                 inode_io_list_del(inode);
690
691         inode_sb_list_del(inode);
692
693         inode_wait_for_lru_isolating(inode);
694
695         /*
696          * Wait for flusher thread to be done with the inode so that filesystem
697          * does not start destroying it while writeback is still running. Since
698          * the inode has I_FREEING set, flusher thread won't start new work on
699          * the inode.  We just have to wait for running writeback to finish.
700          */
701         inode_wait_for_writeback(inode);
702
703         if (op->evict_inode) {
704                 op->evict_inode(inode);
705         } else {
706                 truncate_inode_pages_final(&inode->i_data);
707                 clear_inode(inode);
708         }
709         if (S_ISCHR(inode->i_mode) && inode->i_cdev)
710                 cd_forget(inode);
711
712         remove_inode_hash(inode);
713
714         /*
715          * Wake up waiters in __wait_on_freeing_inode().
716          *
717          * Lockless hash lookup may end up finding the inode before we removed
718          * it above, but only lock it *after* we are done with the wakeup below.
719          * In this case the potential waiter cannot safely block.
720          *
721          * The inode being unhashed after the call to remove_inode_hash() is
722          * used as an indicator whether blocking on it is safe.
723          */
724         spin_lock(&inode->i_lock);
725         wake_up_bit(&inode->i_state, __I_NEW);
726         BUG_ON(inode->i_state != (I_FREEING | I_CLEAR));
727         spin_unlock(&inode->i_lock);
728
729         destroy_inode(inode);
730 }
731
732 /*
733  * dispose_list - dispose of the contents of a local list
734  * @head: the head of the list to free
735  *
736  * Dispose-list gets a local list with local inodes in it, so it doesn't
737  * need to worry about list corruption and SMP locks.
738  */
739 static void dispose_list(struct list_head *head)
740 {
741         while (!list_empty(head)) {
742                 struct inode *inode;
743
744                 inode = list_first_entry(head, struct inode, i_lru);
745                 list_del_init(&inode->i_lru);
746
747                 evict(inode);
748                 cond_resched();
749         }
750 }
751
752 /**
753  * evict_inodes - evict all evictable inodes for a superblock
754  * @sb:         superblock to operate on
755  *
756  * Make sure that no inodes with zero refcount are retained.  This is
757  * called by superblock shutdown after having SB_ACTIVE flag removed,
758  * so any inode reaching zero refcount during or after that call will
759  * be immediately evicted.
760  */
761 void evict_inodes(struct super_block *sb)
762 {
763         struct inode *inode, *next;
764         LIST_HEAD(dispose);
765
766 again:
767         spin_lock(&sb->s_inode_list_lock);
768         list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
769                 if (atomic_read(&inode->i_count))
770                         continue;
771
772                 spin_lock(&inode->i_lock);
773                 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
774                         spin_unlock(&inode->i_lock);
775                         continue;
776                 }
777
778                 inode->i_state |= I_FREEING;
779                 inode_lru_list_del(inode);
780                 spin_unlock(&inode->i_lock);
781                 list_add(&inode->i_lru, &dispose);
782
783                 /*
784                  * We can have a ton of inodes to evict at unmount time given
785                  * enough memory, check to see if we need to go to sleep for a
786                  * bit so we don't livelock.
787                  */
788                 if (need_resched()) {
789                         spin_unlock(&sb->s_inode_list_lock);
790                         cond_resched();
791                         dispose_list(&dispose);
792                         goto again;
793                 }
794         }
795         spin_unlock(&sb->s_inode_list_lock);
796
797         dispose_list(&dispose);
798 }
799 EXPORT_SYMBOL_GPL(evict_inodes);
800
801 /**
802  * invalidate_inodes    - attempt to free all inodes on a superblock
803  * @sb:         superblock to operate on
804  *
805  * Attempts to free all inodes (including dirty inodes) for a given superblock.
806  */
807 void invalidate_inodes(struct super_block *sb)
808 {
809         struct inode *inode, *next;
810         LIST_HEAD(dispose);
811
812 again:
813         spin_lock(&sb->s_inode_list_lock);
814         list_for_each_entry_safe(inode, next, &sb->s_inodes, i_sb_list) {
815                 spin_lock(&inode->i_lock);
816                 if (inode->i_state & (I_NEW | I_FREEING | I_WILL_FREE)) {
817                         spin_unlock(&inode->i_lock);
818                         continue;
819                 }
820                 if (atomic_read(&inode->i_count)) {
821                         spin_unlock(&inode->i_lock);
822                         continue;
823                 }
824
825                 inode->i_state |= I_FREEING;
826                 inode_lru_list_del(inode);
827                 spin_unlock(&inode->i_lock);
828                 list_add(&inode->i_lru, &dispose);
829                 if (need_resched()) {
830                         spin_unlock(&sb->s_inode_list_lock);
831                         cond_resched();
832                         dispose_list(&dispose);
833                         goto again;
834                 }
835         }
836         spin_unlock(&sb->s_inode_list_lock);
837
838         dispose_list(&dispose);
839 }
840
841 /*
842  * Isolate the inode from the LRU in preparation for freeing it.
843  *
844  * If the inode has the I_REFERENCED flag set, then it means that it has been
845  * used recently - the flag is set in iput_final(). When we encounter such an
846  * inode, clear the flag and move it to the back of the LRU so it gets another
847  * pass through the LRU before it gets reclaimed. This is necessary because of
848  * the fact we are doing lazy LRU updates to minimise lock contention so the
849  * LRU does not have strict ordering. Hence we don't want to reclaim inodes
850  * with this flag set because they are the inodes that are out of order.
851  */
852 static enum lru_status inode_lru_isolate(struct list_head *item,
853                 struct list_lru_one *lru, spinlock_t *lru_lock, void *arg)
854 {
855         struct list_head *freeable = arg;
856         struct inode    *inode = container_of(item, struct inode, i_lru);
857
858         /*
859          * We are inverting the lru lock/inode->i_lock here, so use a
860          * trylock. If we fail to get the lock, just skip it.
861          */
862         if (!spin_trylock(&inode->i_lock))
863                 return LRU_SKIP;
864
865         /*
866          * Inodes can get referenced, redirtied, or repopulated while
867          * they're already on the LRU, and this can make them
868          * unreclaimable for a while. Remove them lazily here; iput,
869          * sync, or the last page cache deletion will requeue them.
870          */
871         if (atomic_read(&inode->i_count) ||
872             (inode->i_state & ~I_REFERENCED) ||
873             !mapping_shrinkable(&inode->i_data)) {
874                 list_lru_isolate(lru, &inode->i_lru);
875                 spin_unlock(&inode->i_lock);
876                 this_cpu_dec(nr_unused);
877                 return LRU_REMOVED;
878         }
879
880         /* Recently referenced inodes get one more pass */
881         if (inode->i_state & I_REFERENCED) {
882                 inode->i_state &= ~I_REFERENCED;
883                 spin_unlock(&inode->i_lock);
884                 return LRU_ROTATE;
885         }
886
887         /*
888          * On highmem systems, mapping_shrinkable() permits dropping
889          * page cache in order to free up struct inodes: lowmem might
890          * be under pressure before the cache inside the highmem zone.
891          */
892         if (inode_has_buffers(inode) || !mapping_empty(&inode->i_data)) {
893                 inode_pin_lru_isolating(inode);
894                 spin_unlock(&inode->i_lock);
895                 spin_unlock(lru_lock);
896                 if (remove_inode_buffers(inode)) {
897                         unsigned long reap;
898                         reap = invalidate_mapping_pages(&inode->i_data, 0, -1);
899                         if (current_is_kswapd())
900                                 __count_vm_events(KSWAPD_INODESTEAL, reap);
901                         else
902                                 __count_vm_events(PGINODESTEAL, reap);
903                         mm_account_reclaimed_pages(reap);
904                 }
905                 inode_unpin_lru_isolating(inode);
906                 spin_lock(lru_lock);
907                 return LRU_RETRY;
908         }
909
910         WARN_ON(inode->i_state & I_NEW);
911         inode->i_state |= I_FREEING;
912         list_lru_isolate_move(lru, &inode->i_lru, freeable);
913         spin_unlock(&inode->i_lock);
914
915         this_cpu_dec(nr_unused);
916         return LRU_REMOVED;
917 }
918
919 /*
920  * Walk the superblock inode LRU for freeable inodes and attempt to free them.
921  * This is called from the superblock shrinker function with a number of inodes
922  * to trim from the LRU. Inodes to be freed are moved to a temporary list and
923  * then are freed outside inode_lock by dispose_list().
924  */
925 long prune_icache_sb(struct super_block *sb, struct shrink_control *sc)
926 {
927         LIST_HEAD(freeable);
928         long freed;
929
930         freed = list_lru_shrink_walk(&sb->s_inode_lru, sc,
931                                      inode_lru_isolate, &freeable);
932         dispose_list(&freeable);
933         return freed;
934 }
935
936 static void __wait_on_freeing_inode(struct inode *inode, bool is_inode_hash_locked);
937 /*
938  * Called with the inode lock held.
939  */
940 static struct inode *find_inode(struct super_block *sb,
941                                 struct hlist_head *head,
942                                 int (*test)(struct inode *, void *),
943                                 void *data, bool is_inode_hash_locked)
944 {
945         struct inode *inode = NULL;
946
947         if (is_inode_hash_locked)
948                 lockdep_assert_held(&inode_hash_lock);
949         else
950                 lockdep_assert_not_held(&inode_hash_lock);
951
952         rcu_read_lock();
953 repeat:
954         hlist_for_each_entry_rcu(inode, head, i_hash) {
955                 if (inode->i_sb != sb)
956                         continue;
957                 if (!test(inode, data))
958                         continue;
959                 spin_lock(&inode->i_lock);
960                 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
961                         __wait_on_freeing_inode(inode, is_inode_hash_locked);
962                         goto repeat;
963                 }
964                 if (unlikely(inode->i_state & I_CREATING)) {
965                         spin_unlock(&inode->i_lock);
966                         rcu_read_unlock();
967                         return ERR_PTR(-ESTALE);
968                 }
969                 __iget(inode);
970                 spin_unlock(&inode->i_lock);
971                 rcu_read_unlock();
972                 return inode;
973         }
974         rcu_read_unlock();
975         return NULL;
976 }
977
978 /*
979  * find_inode_fast is the fast path version of find_inode, see the comment at
980  * iget_locked for details.
981  */
982 static struct inode *find_inode_fast(struct super_block *sb,
983                                 struct hlist_head *head, unsigned long ino,
984                                 bool is_inode_hash_locked)
985 {
986         struct inode *inode = NULL;
987
988         if (is_inode_hash_locked)
989                 lockdep_assert_held(&inode_hash_lock);
990         else
991                 lockdep_assert_not_held(&inode_hash_lock);
992
993         rcu_read_lock();
994 repeat:
995         hlist_for_each_entry_rcu(inode, head, i_hash) {
996                 if (inode->i_ino != ino)
997                         continue;
998                 if (inode->i_sb != sb)
999                         continue;
1000                 spin_lock(&inode->i_lock);
1001                 if (inode->i_state & (I_FREEING|I_WILL_FREE)) {
1002                         __wait_on_freeing_inode(inode, is_inode_hash_locked);
1003                         goto repeat;
1004                 }
1005                 if (unlikely(inode->i_state & I_CREATING)) {
1006                         spin_unlock(&inode->i_lock);
1007                         rcu_read_unlock();
1008                         return ERR_PTR(-ESTALE);
1009                 }
1010                 __iget(inode);
1011                 spin_unlock(&inode->i_lock);
1012                 rcu_read_unlock();
1013                 return inode;
1014         }
1015         rcu_read_unlock();
1016         return NULL;
1017 }
1018
1019 /*
1020  * Each cpu owns a range of LAST_INO_BATCH numbers.
1021  * 'shared_last_ino' is dirtied only once out of LAST_INO_BATCH allocations,
1022  * to renew the exhausted range.
1023  *
1024  * This does not significantly increase overflow rate because every CPU can
1025  * consume at most LAST_INO_BATCH-1 unused inode numbers. So there is
1026  * NR_CPUS*(LAST_INO_BATCH-1) wastage. At 4096 and 1024, this is ~0.1% of the
1027  * 2^32 range, and is a worst-case. Even a 50% wastage would only increase
1028  * overflow rate by 2x, which does not seem too significant.
1029  *
1030  * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
1031  * error if st_ino won't fit in target struct field. Use 32bit counter
1032  * here to attempt to avoid that.
1033  */
1034 #define LAST_INO_BATCH 1024
1035 static DEFINE_PER_CPU(unsigned int, last_ino);
1036
1037 unsigned int get_next_ino(void)
1038 {
1039         unsigned int *p = &get_cpu_var(last_ino);
1040         unsigned int res = *p;
1041
1042 #ifdef CONFIG_SMP
1043         if (unlikely((res & (LAST_INO_BATCH-1)) == 0)) {
1044                 static atomic_t shared_last_ino;
1045                 int next = atomic_add_return(LAST_INO_BATCH, &shared_last_ino);
1046
1047                 res = next - LAST_INO_BATCH;
1048         }
1049 #endif
1050
1051         res++;
1052         /* get_next_ino should not provide a 0 inode number */
1053         if (unlikely(!res))
1054                 res++;
1055         *p = res;
1056         put_cpu_var(last_ino);
1057         return res;
1058 }
1059 EXPORT_SYMBOL(get_next_ino);
1060
1061 /**
1062  *      new_inode_pseudo        - obtain an inode
1063  *      @sb: superblock
1064  *
1065  *      Allocates a new inode for given superblock.
1066  *      Inode wont be chained in superblock s_inodes list
1067  *      This means :
1068  *      - fs can't be unmount
1069  *      - quotas, fsnotify, writeback can't work
1070  */
1071 struct inode *new_inode_pseudo(struct super_block *sb)
1072 {
1073         return alloc_inode(sb);
1074 }
1075
1076 /**
1077  *      new_inode       - obtain an inode
1078  *      @sb: superblock
1079  *
1080  *      Allocates a new inode for given superblock. The default gfp_mask
1081  *      for allocations related to inode->i_mapping is GFP_HIGHUSER_MOVABLE.
1082  *      If HIGHMEM pages are unsuitable or it is known that pages allocated
1083  *      for the page cache are not reclaimable or migratable,
1084  *      mapping_set_gfp_mask() must be called with suitable flags on the
1085  *      newly created inode's mapping
1086  *
1087  */
1088 struct inode *new_inode(struct super_block *sb)
1089 {
1090         struct inode *inode;
1091
1092         inode = new_inode_pseudo(sb);
1093         if (inode)
1094                 inode_sb_list_add(inode);
1095         return inode;
1096 }
1097 EXPORT_SYMBOL(new_inode);
1098
1099 #ifdef CONFIG_DEBUG_LOCK_ALLOC
1100 void lockdep_annotate_inode_mutex_key(struct inode *inode)
1101 {
1102         if (S_ISDIR(inode->i_mode)) {
1103                 struct file_system_type *type = inode->i_sb->s_type;
1104
1105                 /* Set new key only if filesystem hasn't already changed it */
1106                 if (lockdep_match_class(&inode->i_rwsem, &type->i_mutex_key)) {
1107                         /*
1108                          * ensure nobody is actually holding i_mutex
1109                          */
1110                         // mutex_destroy(&inode->i_mutex);
1111                         init_rwsem(&inode->i_rwsem);
1112                         lockdep_set_class(&inode->i_rwsem,
1113                                           &type->i_mutex_dir_key);
1114                 }
1115         }
1116 }
1117 EXPORT_SYMBOL(lockdep_annotate_inode_mutex_key);
1118 #endif
1119
1120 /**
1121  * unlock_new_inode - clear the I_NEW state and wake up any waiters
1122  * @inode:      new inode to unlock
1123  *
1124  * Called when the inode is fully initialised to clear the new state of the
1125  * inode and wake up anyone waiting for the inode to finish initialisation.
1126  */
1127 void unlock_new_inode(struct inode *inode)
1128 {
1129         lockdep_annotate_inode_mutex_key(inode);
1130         spin_lock(&inode->i_lock);
1131         WARN_ON(!(inode->i_state & I_NEW));
1132         inode->i_state &= ~I_NEW & ~I_CREATING;
1133         smp_mb();
1134         wake_up_bit(&inode->i_state, __I_NEW);
1135         spin_unlock(&inode->i_lock);
1136 }
1137 EXPORT_SYMBOL(unlock_new_inode);
1138
1139 void discard_new_inode(struct inode *inode)
1140 {
1141         lockdep_annotate_inode_mutex_key(inode);
1142         spin_lock(&inode->i_lock);
1143         WARN_ON(!(inode->i_state & I_NEW));
1144         inode->i_state &= ~I_NEW;
1145         smp_mb();
1146         wake_up_bit(&inode->i_state, __I_NEW);
1147         spin_unlock(&inode->i_lock);
1148         iput(inode);
1149 }
1150 EXPORT_SYMBOL(discard_new_inode);
1151
1152 /**
1153  * lock_two_nondirectories - take two i_mutexes on non-directory objects
1154  *
1155  * Lock any non-NULL argument. Passed objects must not be directories.
1156  * Zero, one or two objects may be locked by this function.
1157  *
1158  * @inode1: first inode to lock
1159  * @inode2: second inode to lock
1160  */
1161 void lock_two_nondirectories(struct inode *inode1, struct inode *inode2)
1162 {
1163         if (inode1)
1164                 WARN_ON_ONCE(S_ISDIR(inode1->i_mode));
1165         if (inode2)
1166                 WARN_ON_ONCE(S_ISDIR(inode2->i_mode));
1167         if (inode1 > inode2)
1168                 swap(inode1, inode2);
1169         if (inode1)
1170                 inode_lock(inode1);
1171         if (inode2 && inode2 != inode1)
1172                 inode_lock_nested(inode2, I_MUTEX_NONDIR2);
1173 }
1174 EXPORT_SYMBOL(lock_two_nondirectories);
1175
1176 /**
1177  * unlock_two_nondirectories - release locks from lock_two_nondirectories()
1178  * @inode1: first inode to unlock
1179  * @inode2: second inode to unlock
1180  */
1181 void unlock_two_nondirectories(struct inode *inode1, struct inode *inode2)
1182 {
1183         if (inode1) {
1184                 WARN_ON_ONCE(S_ISDIR(inode1->i_mode));
1185                 inode_unlock(inode1);
1186         }
1187         if (inode2 && inode2 != inode1) {
1188                 WARN_ON_ONCE(S_ISDIR(inode2->i_mode));
1189                 inode_unlock(inode2);
1190         }
1191 }
1192 EXPORT_SYMBOL(unlock_two_nondirectories);
1193
1194 /**
1195  * inode_insert5 - obtain an inode from a mounted file system
1196  * @inode:      pre-allocated inode to use for insert to cache
1197  * @hashval:    hash value (usually inode number) to get
1198  * @test:       callback used for comparisons between inodes
1199  * @set:        callback used to initialize a new struct inode
1200  * @data:       opaque data pointer to pass to @test and @set
1201  *
1202  * Search for the inode specified by @hashval and @data in the inode cache,
1203  * and if present it is return it with an increased reference count. This is
1204  * a variant of iget5_locked() for callers that don't want to fail on memory
1205  * allocation of inode.
1206  *
1207  * If the inode is not in cache, insert the pre-allocated inode to cache and
1208  * return it locked, hashed, and with the I_NEW flag set. The file system gets
1209  * to fill it in before unlocking it via unlock_new_inode().
1210  *
1211  * Note both @test and @set are called with the inode_hash_lock held, so can't
1212  * sleep.
1213  */
1214 struct inode *inode_insert5(struct inode *inode, unsigned long hashval,
1215                             int (*test)(struct inode *, void *),
1216                             int (*set)(struct inode *, void *), void *data)
1217 {
1218         struct hlist_head *head = inode_hashtable + hash(inode->i_sb, hashval);
1219         struct inode *old;
1220
1221 again:
1222         spin_lock(&inode_hash_lock);
1223         old = find_inode(inode->i_sb, head, test, data, true);
1224         if (unlikely(old)) {
1225                 /*
1226                  * Uhhuh, somebody else created the same inode under us.
1227                  * Use the old inode instead of the preallocated one.
1228                  */
1229                 spin_unlock(&inode_hash_lock);
1230                 if (IS_ERR(old))
1231                         return NULL;
1232                 wait_on_inode(old);
1233                 if (unlikely(inode_unhashed(old))) {
1234                         iput(old);
1235                         goto again;
1236                 }
1237                 return old;
1238         }
1239
1240         if (set && unlikely(set(inode, data))) {
1241                 inode = NULL;
1242                 goto unlock;
1243         }
1244
1245         /*
1246          * Return the locked inode with I_NEW set, the
1247          * caller is responsible for filling in the contents
1248          */
1249         spin_lock(&inode->i_lock);
1250         inode->i_state |= I_NEW;
1251         hlist_add_head_rcu(&inode->i_hash, head);
1252         spin_unlock(&inode->i_lock);
1253
1254         /*
1255          * Add inode to the sb list if it's not already. It has I_NEW at this
1256          * point, so it should be safe to test i_sb_list locklessly.
1257          */
1258         if (list_empty(&inode->i_sb_list))
1259                 inode_sb_list_add(inode);
1260 unlock:
1261         spin_unlock(&inode_hash_lock);
1262
1263         return inode;
1264 }
1265 EXPORT_SYMBOL(inode_insert5);
1266
1267 /**
1268  * iget5_locked - obtain an inode from a mounted file system
1269  * @sb:         super block of file system
1270  * @hashval:    hash value (usually inode number) to get
1271  * @test:       callback used for comparisons between inodes
1272  * @set:        callback used to initialize a new struct inode
1273  * @data:       opaque data pointer to pass to @test and @set
1274  *
1275  * Search for the inode specified by @hashval and @data in the inode cache,
1276  * and if present it is return it with an increased reference count. This is
1277  * a generalized version of iget_locked() for file systems where the inode
1278  * number is not sufficient for unique identification of an inode.
1279  *
1280  * If the inode is not in cache, allocate a new inode and return it locked,
1281  * hashed, and with the I_NEW flag set. The file system gets to fill it in
1282  * before unlocking it via unlock_new_inode().
1283  *
1284  * Note both @test and @set are called with the inode_hash_lock held, so can't
1285  * sleep.
1286  */
1287 struct inode *iget5_locked(struct super_block *sb, unsigned long hashval,
1288                 int (*test)(struct inode *, void *),
1289                 int (*set)(struct inode *, void *), void *data)
1290 {
1291         struct inode *inode = ilookup5(sb, hashval, test, data);
1292
1293         if (!inode) {
1294                 struct inode *new = alloc_inode(sb);
1295
1296                 if (new) {
1297                         inode = inode_insert5(new, hashval, test, set, data);
1298                         if (unlikely(inode != new))
1299                                 destroy_inode(new);
1300                 }
1301         }
1302         return inode;
1303 }
1304 EXPORT_SYMBOL(iget5_locked);
1305
1306 /**
1307  * iget5_locked_rcu - obtain an inode from a mounted file system
1308  * @sb:         super block of file system
1309  * @hashval:    hash value (usually inode number) to get
1310  * @test:       callback used for comparisons between inodes
1311  * @set:        callback used to initialize a new struct inode
1312  * @data:       opaque data pointer to pass to @test and @set
1313  *
1314  * This is equivalent to iget5_locked, except the @test callback must
1315  * tolerate the inode not being stable, including being mid-teardown.
1316  */
1317 struct inode *iget5_locked_rcu(struct super_block *sb, unsigned long hashval,
1318                 int (*test)(struct inode *, void *),
1319                 int (*set)(struct inode *, void *), void *data)
1320 {
1321         struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1322         struct inode *inode, *new;
1323
1324 again:
1325         inode = find_inode(sb, head, test, data, false);
1326         if (inode) {
1327                 if (IS_ERR(inode))
1328                         return NULL;
1329                 wait_on_inode(inode);
1330                 if (unlikely(inode_unhashed(inode))) {
1331                         iput(inode);
1332                         goto again;
1333                 }
1334                 return inode;
1335         }
1336
1337         new = alloc_inode(sb);
1338         if (new) {
1339                 inode = inode_insert5(new, hashval, test, set, data);
1340                 if (unlikely(inode != new))
1341                         destroy_inode(new);
1342         }
1343         return inode;
1344 }
1345 EXPORT_SYMBOL_GPL(iget5_locked_rcu);
1346
1347 /**
1348  * iget_locked - obtain an inode from a mounted file system
1349  * @sb:         super block of file system
1350  * @ino:        inode number to get
1351  *
1352  * Search for the inode specified by @ino in the inode cache and if present
1353  * return it with an increased reference count. This is for file systems
1354  * where the inode number is sufficient for unique identification of an inode.
1355  *
1356  * If the inode is not in cache, allocate a new inode and return it locked,
1357  * hashed, and with the I_NEW flag set.  The file system gets to fill it in
1358  * before unlocking it via unlock_new_inode().
1359  */
1360 struct inode *iget_locked(struct super_block *sb, unsigned long ino)
1361 {
1362         struct hlist_head *head = inode_hashtable + hash(sb, ino);
1363         struct inode *inode;
1364 again:
1365         inode = find_inode_fast(sb, head, ino, false);
1366         if (inode) {
1367                 if (IS_ERR(inode))
1368                         return NULL;
1369                 wait_on_inode(inode);
1370                 if (unlikely(inode_unhashed(inode))) {
1371                         iput(inode);
1372                         goto again;
1373                 }
1374                 return inode;
1375         }
1376
1377         inode = alloc_inode(sb);
1378         if (inode) {
1379                 struct inode *old;
1380
1381                 spin_lock(&inode_hash_lock);
1382                 /* We released the lock, so.. */
1383                 old = find_inode_fast(sb, head, ino, true);
1384                 if (!old) {
1385                         inode->i_ino = ino;
1386                         spin_lock(&inode->i_lock);
1387                         inode->i_state = I_NEW;
1388                         hlist_add_head_rcu(&inode->i_hash, head);
1389                         spin_unlock(&inode->i_lock);
1390                         inode_sb_list_add(inode);
1391                         spin_unlock(&inode_hash_lock);
1392
1393                         /* Return the locked inode with I_NEW set, the
1394                          * caller is responsible for filling in the contents
1395                          */
1396                         return inode;
1397                 }
1398
1399                 /*
1400                  * Uhhuh, somebody else created the same inode under
1401                  * us. Use the old inode instead of the one we just
1402                  * allocated.
1403                  */
1404                 spin_unlock(&inode_hash_lock);
1405                 destroy_inode(inode);
1406                 if (IS_ERR(old))
1407                         return NULL;
1408                 inode = old;
1409                 wait_on_inode(inode);
1410                 if (unlikely(inode_unhashed(inode))) {
1411                         iput(inode);
1412                         goto again;
1413                 }
1414         }
1415         return inode;
1416 }
1417 EXPORT_SYMBOL(iget_locked);
1418
1419 /*
1420  * search the inode cache for a matching inode number.
1421  * If we find one, then the inode number we are trying to
1422  * allocate is not unique and so we should not use it.
1423  *
1424  * Returns 1 if the inode number is unique, 0 if it is not.
1425  */
1426 static int test_inode_iunique(struct super_block *sb, unsigned long ino)
1427 {
1428         struct hlist_head *b = inode_hashtable + hash(sb, ino);
1429         struct inode *inode;
1430
1431         hlist_for_each_entry_rcu(inode, b, i_hash) {
1432                 if (inode->i_ino == ino && inode->i_sb == sb)
1433                         return 0;
1434         }
1435         return 1;
1436 }
1437
1438 /**
1439  *      iunique - get a unique inode number
1440  *      @sb: superblock
1441  *      @max_reserved: highest reserved inode number
1442  *
1443  *      Obtain an inode number that is unique on the system for a given
1444  *      superblock. This is used by file systems that have no natural
1445  *      permanent inode numbering system. An inode number is returned that
1446  *      is higher than the reserved limit but unique.
1447  *
1448  *      BUGS:
1449  *      With a large number of inodes live on the file system this function
1450  *      currently becomes quite slow.
1451  */
1452 ino_t iunique(struct super_block *sb, ino_t max_reserved)
1453 {
1454         /*
1455          * On a 32bit, non LFS stat() call, glibc will generate an EOVERFLOW
1456          * error if st_ino won't fit in target struct field. Use 32bit counter
1457          * here to attempt to avoid that.
1458          */
1459         static DEFINE_SPINLOCK(iunique_lock);
1460         static unsigned int counter;
1461         ino_t res;
1462
1463         rcu_read_lock();
1464         spin_lock(&iunique_lock);
1465         do {
1466                 if (counter <= max_reserved)
1467                         counter = max_reserved + 1;
1468                 res = counter++;
1469         } while (!test_inode_iunique(sb, res));
1470         spin_unlock(&iunique_lock);
1471         rcu_read_unlock();
1472
1473         return res;
1474 }
1475 EXPORT_SYMBOL(iunique);
1476
1477 struct inode *igrab(struct inode *inode)
1478 {
1479         spin_lock(&inode->i_lock);
1480         if (!(inode->i_state & (I_FREEING|I_WILL_FREE))) {
1481                 __iget(inode);
1482                 spin_unlock(&inode->i_lock);
1483         } else {
1484                 spin_unlock(&inode->i_lock);
1485                 /*
1486                  * Handle the case where s_op->clear_inode is not been
1487                  * called yet, and somebody is calling igrab
1488                  * while the inode is getting freed.
1489                  */
1490                 inode = NULL;
1491         }
1492         return inode;
1493 }
1494 EXPORT_SYMBOL(igrab);
1495
1496 /**
1497  * ilookup5_nowait - search for an inode in the inode cache
1498  * @sb:         super block of file system to search
1499  * @hashval:    hash value (usually inode number) to search for
1500  * @test:       callback used for comparisons between inodes
1501  * @data:       opaque data pointer to pass to @test
1502  *
1503  * Search for the inode specified by @hashval and @data in the inode cache.
1504  * If the inode is in the cache, the inode is returned with an incremented
1505  * reference count.
1506  *
1507  * Note: I_NEW is not waited upon so you have to be very careful what you do
1508  * with the returned inode.  You probably should be using ilookup5() instead.
1509  *
1510  * Note2: @test is called with the inode_hash_lock held, so can't sleep.
1511  */
1512 struct inode *ilookup5_nowait(struct super_block *sb, unsigned long hashval,
1513                 int (*test)(struct inode *, void *), void *data)
1514 {
1515         struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1516         struct inode *inode;
1517
1518         spin_lock(&inode_hash_lock);
1519         inode = find_inode(sb, head, test, data, true);
1520         spin_unlock(&inode_hash_lock);
1521
1522         return IS_ERR(inode) ? NULL : inode;
1523 }
1524 EXPORT_SYMBOL(ilookup5_nowait);
1525
1526 /**
1527  * ilookup5 - search for an inode in the inode cache
1528  * @sb:         super block of file system to search
1529  * @hashval:    hash value (usually inode number) to search for
1530  * @test:       callback used for comparisons between inodes
1531  * @data:       opaque data pointer to pass to @test
1532  *
1533  * Search for the inode specified by @hashval and @data in the inode cache,
1534  * and if the inode is in the cache, return the inode with an incremented
1535  * reference count.  Waits on I_NEW before returning the inode.
1536  * returned with an incremented reference count.
1537  *
1538  * This is a generalized version of ilookup() for file systems where the
1539  * inode number is not sufficient for unique identification of an inode.
1540  *
1541  * Note: @test is called with the inode_hash_lock held, so can't sleep.
1542  */
1543 struct inode *ilookup5(struct super_block *sb, unsigned long hashval,
1544                 int (*test)(struct inode *, void *), void *data)
1545 {
1546         struct inode *inode;
1547 again:
1548         inode = ilookup5_nowait(sb, hashval, test, data);
1549         if (inode) {
1550                 wait_on_inode(inode);
1551                 if (unlikely(inode_unhashed(inode))) {
1552                         iput(inode);
1553                         goto again;
1554                 }
1555         }
1556         return inode;
1557 }
1558 EXPORT_SYMBOL(ilookup5);
1559
1560 /**
1561  * ilookup - search for an inode in the inode cache
1562  * @sb:         super block of file system to search
1563  * @ino:        inode number to search for
1564  *
1565  * Search for the inode @ino in the inode cache, and if the inode is in the
1566  * cache, the inode is returned with an incremented reference count.
1567  */
1568 struct inode *ilookup(struct super_block *sb, unsigned long ino)
1569 {
1570         struct hlist_head *head = inode_hashtable + hash(sb, ino);
1571         struct inode *inode;
1572 again:
1573         spin_lock(&inode_hash_lock);
1574         inode = find_inode_fast(sb, head, ino, true);
1575         spin_unlock(&inode_hash_lock);
1576
1577         if (inode) {
1578                 if (IS_ERR(inode))
1579                         return NULL;
1580                 wait_on_inode(inode);
1581                 if (unlikely(inode_unhashed(inode))) {
1582                         iput(inode);
1583                         goto again;
1584                 }
1585         }
1586         return inode;
1587 }
1588 EXPORT_SYMBOL(ilookup);
1589
1590 /**
1591  * find_inode_nowait - find an inode in the inode cache
1592  * @sb:         super block of file system to search
1593  * @hashval:    hash value (usually inode number) to search for
1594  * @match:      callback used for comparisons between inodes
1595  * @data:       opaque data pointer to pass to @match
1596  *
1597  * Search for the inode specified by @hashval and @data in the inode
1598  * cache, where the helper function @match will return 0 if the inode
1599  * does not match, 1 if the inode does match, and -1 if the search
1600  * should be stopped.  The @match function must be responsible for
1601  * taking the i_lock spin_lock and checking i_state for an inode being
1602  * freed or being initialized, and incrementing the reference count
1603  * before returning 1.  It also must not sleep, since it is called with
1604  * the inode_hash_lock spinlock held.
1605  *
1606  * This is a even more generalized version of ilookup5() when the
1607  * function must never block --- find_inode() can block in
1608  * __wait_on_freeing_inode() --- or when the caller can not increment
1609  * the reference count because the resulting iput() might cause an
1610  * inode eviction.  The tradeoff is that the @match funtion must be
1611  * very carefully implemented.
1612  */
1613 struct inode *find_inode_nowait(struct super_block *sb,
1614                                 unsigned long hashval,
1615                                 int (*match)(struct inode *, unsigned long,
1616                                              void *),
1617                                 void *data)
1618 {
1619         struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1620         struct inode *inode, *ret_inode = NULL;
1621         int mval;
1622
1623         spin_lock(&inode_hash_lock);
1624         hlist_for_each_entry(inode, head, i_hash) {
1625                 if (inode->i_sb != sb)
1626                         continue;
1627                 mval = match(inode, hashval, data);
1628                 if (mval == 0)
1629                         continue;
1630                 if (mval == 1)
1631                         ret_inode = inode;
1632                 goto out;
1633         }
1634 out:
1635         spin_unlock(&inode_hash_lock);
1636         return ret_inode;
1637 }
1638 EXPORT_SYMBOL(find_inode_nowait);
1639
1640 /**
1641  * find_inode_rcu - find an inode in the inode cache
1642  * @sb:         Super block of file system to search
1643  * @hashval:    Key to hash
1644  * @test:       Function to test match on an inode
1645  * @data:       Data for test function
1646  *
1647  * Search for the inode specified by @hashval and @data in the inode cache,
1648  * where the helper function @test will return 0 if the inode does not match
1649  * and 1 if it does.  The @test function must be responsible for taking the
1650  * i_lock spin_lock and checking i_state for an inode being freed or being
1651  * initialized.
1652  *
1653  * If successful, this will return the inode for which the @test function
1654  * returned 1 and NULL otherwise.
1655  *
1656  * The @test function is not permitted to take a ref on any inode presented.
1657  * It is also not permitted to sleep.
1658  *
1659  * The caller must hold the RCU read lock.
1660  */
1661 struct inode *find_inode_rcu(struct super_block *sb, unsigned long hashval,
1662                              int (*test)(struct inode *, void *), void *data)
1663 {
1664         struct hlist_head *head = inode_hashtable + hash(sb, hashval);
1665         struct inode *inode;
1666
1667         RCU_LOCKDEP_WARN(!rcu_read_lock_held(),
1668                          "suspicious find_inode_rcu() usage");
1669
1670         hlist_for_each_entry_rcu(inode, head, i_hash) {
1671                 if (inode->i_sb == sb &&
1672                     !(READ_ONCE(inode->i_state) & (I_FREEING | I_WILL_FREE)) &&
1673                     test(inode, data))
1674                         return inode;
1675         }
1676         return NULL;
1677 }
1678 EXPORT_SYMBOL(find_inode_rcu);
1679
1680 /**
1681  * find_inode_by_ino_rcu - Find an inode in the inode cache
1682  * @sb:         Super block of file system to search
1683  * @ino:        The inode number to match
1684  *
1685  * Search for the inode specified by @hashval and @data in the inode cache,
1686  * where the helper function @test will return 0 if the inode does not match
1687  * and 1 if it does.  The @test function must be responsible for taking the
1688  * i_lock spin_lock and checking i_state for an inode being freed or being
1689  * initialized.
1690  *
1691  * If successful, this will return the inode for which the @test function
1692  * returned 1 and NULL otherwise.
1693  *
1694  * The @test function is not permitted to take a ref on any inode presented.
1695  * It is also not permitted to sleep.
1696  *
1697  * The caller must hold the RCU read lock.
1698  */
1699 struct inode *find_inode_by_ino_rcu(struct super_block *sb,
1700                                     unsigned long ino)
1701 {
1702         struct hlist_head *head = inode_hashtable + hash(sb, ino);
1703         struct inode *inode;
1704
1705         RCU_LOCKDEP_WARN(!rcu_read_lock_held(),
1706                          "suspicious find_inode_by_ino_rcu() usage");
1707
1708         hlist_for_each_entry_rcu(inode, head, i_hash) {
1709                 if (inode->i_ino == ino &&
1710                     inode->i_sb == sb &&
1711                     !(READ_ONCE(inode->i_state) & (I_FREEING | I_WILL_FREE)))
1712                     return inode;
1713         }
1714         return NULL;
1715 }
1716 EXPORT_SYMBOL(find_inode_by_ino_rcu);
1717
1718 int insert_inode_locked(struct inode *inode)
1719 {
1720         struct super_block *sb = inode->i_sb;
1721         ino_t ino = inode->i_ino;
1722         struct hlist_head *head = inode_hashtable + hash(sb, ino);
1723
1724         while (1) {
1725                 struct inode *old = NULL;
1726                 spin_lock(&inode_hash_lock);
1727                 hlist_for_each_entry(old, head, i_hash) {
1728                         if (old->i_ino != ino)
1729                                 continue;
1730                         if (old->i_sb != sb)
1731                                 continue;
1732                         spin_lock(&old->i_lock);
1733                         if (old->i_state & (I_FREEING|I_WILL_FREE)) {
1734                                 spin_unlock(&old->i_lock);
1735                                 continue;
1736                         }
1737                         break;
1738                 }
1739                 if (likely(!old)) {
1740                         spin_lock(&inode->i_lock);
1741                         inode->i_state |= I_NEW | I_CREATING;
1742                         hlist_add_head_rcu(&inode->i_hash, head);
1743                         spin_unlock(&inode->i_lock);
1744                         spin_unlock(&inode_hash_lock);
1745                         return 0;
1746                 }
1747                 if (unlikely(old->i_state & I_CREATING)) {
1748                         spin_unlock(&old->i_lock);
1749                         spin_unlock(&inode_hash_lock);
1750                         return -EBUSY;
1751                 }
1752                 __iget(old);
1753                 spin_unlock(&old->i_lock);
1754                 spin_unlock(&inode_hash_lock);
1755                 wait_on_inode(old);
1756                 if (unlikely(!inode_unhashed(old))) {
1757                         iput(old);
1758                         return -EBUSY;
1759                 }
1760                 iput(old);
1761         }
1762 }
1763 EXPORT_SYMBOL(insert_inode_locked);
1764
1765 int insert_inode_locked4(struct inode *inode, unsigned long hashval,
1766                 int (*test)(struct inode *, void *), void *data)
1767 {
1768         struct inode *old;
1769
1770         inode->i_state |= I_CREATING;
1771         old = inode_insert5(inode, hashval, test, NULL, data);
1772
1773         if (old != inode) {
1774                 iput(old);
1775                 return -EBUSY;
1776         }
1777         return 0;
1778 }
1779 EXPORT_SYMBOL(insert_inode_locked4);
1780
1781
1782 int generic_delete_inode(struct inode *inode)
1783 {
1784         return 1;
1785 }
1786 EXPORT_SYMBOL(generic_delete_inode);
1787
1788 /*
1789  * Called when we're dropping the last reference
1790  * to an inode.
1791  *
1792  * Call the FS "drop_inode()" function, defaulting to
1793  * the legacy UNIX filesystem behaviour.  If it tells
1794  * us to evict inode, do so.  Otherwise, retain inode
1795  * in cache if fs is alive, sync and evict if fs is
1796  * shutting down.
1797  */
1798 static void iput_final(struct inode *inode)
1799 {
1800         struct super_block *sb = inode->i_sb;
1801         const struct super_operations *op = inode->i_sb->s_op;
1802         unsigned long state;
1803         int drop;
1804
1805         WARN_ON(inode->i_state & I_NEW);
1806
1807         if (op->drop_inode)
1808                 drop = op->drop_inode(inode);
1809         else
1810                 drop = generic_drop_inode(inode);
1811
1812         if (!drop &&
1813             !(inode->i_state & I_DONTCACHE) &&
1814             (sb->s_flags & SB_ACTIVE)) {
1815                 __inode_add_lru(inode, true);
1816                 spin_unlock(&inode->i_lock);
1817                 return;
1818         }
1819
1820         state = inode->i_state;
1821         if (!drop) {
1822                 WRITE_ONCE(inode->i_state, state | I_WILL_FREE);
1823                 spin_unlock(&inode->i_lock);
1824
1825                 write_inode_now(inode, 1);
1826
1827                 spin_lock(&inode->i_lock);
1828                 state = inode->i_state;
1829                 WARN_ON(state & I_NEW);
1830                 state &= ~I_WILL_FREE;
1831         }
1832
1833         WRITE_ONCE(inode->i_state, state | I_FREEING);
1834         if (!list_empty(&inode->i_lru))
1835                 inode_lru_list_del(inode);
1836         spin_unlock(&inode->i_lock);
1837
1838         evict(inode);
1839 }
1840
1841 /**
1842  *      iput    - put an inode
1843  *      @inode: inode to put
1844  *
1845  *      Puts an inode, dropping its usage count. If the inode use count hits
1846  *      zero, the inode is then freed and may also be destroyed.
1847  *
1848  *      Consequently, iput() can sleep.
1849  */
1850 void iput(struct inode *inode)
1851 {
1852         if (!inode)
1853                 return;
1854         BUG_ON(inode->i_state & I_CLEAR);
1855 retry:
1856         if (atomic_dec_and_lock(&inode->i_count, &inode->i_lock)) {
1857                 if (inode->i_nlink && (inode->i_state & I_DIRTY_TIME)) {
1858                         atomic_inc(&inode->i_count);
1859                         spin_unlock(&inode->i_lock);
1860                         trace_writeback_lazytime_iput(inode);
1861                         mark_inode_dirty_sync(inode);
1862                         goto retry;
1863                 }
1864                 iput_final(inode);
1865         }
1866 }
1867 EXPORT_SYMBOL(iput);
1868
1869 #ifdef CONFIG_BLOCK
1870 /**
1871  *      bmap    - find a block number in a file
1872  *      @inode:  inode owning the block number being requested
1873  *      @block: pointer containing the block to find
1874  *
1875  *      Replaces the value in ``*block`` with the block number on the device holding
1876  *      corresponding to the requested block number in the file.
1877  *      That is, asked for block 4 of inode 1 the function will replace the
1878  *      4 in ``*block``, with disk block relative to the disk start that holds that
1879  *      block of the file.
1880  *
1881  *      Returns -EINVAL in case of error, 0 otherwise. If mapping falls into a
1882  *      hole, returns 0 and ``*block`` is also set to 0.
1883  */
1884 int bmap(struct inode *inode, sector_t *block)
1885 {
1886         if (!inode->i_mapping->a_ops->bmap)
1887                 return -EINVAL;
1888
1889         *block = inode->i_mapping->a_ops->bmap(inode->i_mapping, *block);
1890         return 0;
1891 }
1892 EXPORT_SYMBOL(bmap);
1893 #endif
1894
1895 /*
1896  * With relative atime, only update atime if the previous atime is
1897  * earlier than or equal to either the ctime or mtime,
1898  * or if at least a day has passed since the last atime update.
1899  */
1900 static bool relatime_need_update(struct vfsmount *mnt, struct inode *inode,
1901                              struct timespec64 now)
1902 {
1903         struct timespec64 atime, mtime, ctime;
1904
1905         if (!(mnt->mnt_flags & MNT_RELATIME))
1906                 return true;
1907         /*
1908          * Is mtime younger than or equal to atime? If yes, update atime:
1909          */
1910         atime = inode_get_atime(inode);
1911         mtime = inode_get_mtime(inode);
1912         if (timespec64_compare(&mtime, &atime) >= 0)
1913                 return true;
1914         /*
1915          * Is ctime younger than or equal to atime? If yes, update atime:
1916          */
1917         ctime = inode_get_ctime(inode);
1918         if (timespec64_compare(&ctime, &atime) >= 0)
1919                 return true;
1920
1921         /*
1922          * Is the previous atime value older than a day? If yes,
1923          * update atime:
1924          */
1925         if ((long)(now.tv_sec - atime.tv_sec) >= 24*60*60)
1926                 return true;
1927         /*
1928          * Good, we can skip the atime update:
1929          */
1930         return false;
1931 }
1932
1933 /**
1934  * inode_update_timestamps - update the timestamps on the inode
1935  * @inode: inode to be updated
1936  * @flags: S_* flags that needed to be updated
1937  *
1938  * The update_time function is called when an inode's timestamps need to be
1939  * updated for a read or write operation. This function handles updating the
1940  * actual timestamps. It's up to the caller to ensure that the inode is marked
1941  * dirty appropriately.
1942  *
1943  * In the case where any of S_MTIME, S_CTIME, or S_VERSION need to be updated,
1944  * attempt to update all three of them. S_ATIME updates can be handled
1945  * independently of the rest.
1946  *
1947  * Returns a set of S_* flags indicating which values changed.
1948  */
1949 int inode_update_timestamps(struct inode *inode, int flags)
1950 {
1951         int updated = 0;
1952         struct timespec64 now;
1953
1954         if (flags & (S_MTIME|S_CTIME|S_VERSION)) {
1955                 struct timespec64 ctime = inode_get_ctime(inode);
1956                 struct timespec64 mtime = inode_get_mtime(inode);
1957
1958                 now = inode_set_ctime_current(inode);
1959                 if (!timespec64_equal(&now, &ctime))
1960                         updated |= S_CTIME;
1961                 if (!timespec64_equal(&now, &mtime)) {
1962                         inode_set_mtime_to_ts(inode, now);
1963                         updated |= S_MTIME;
1964                 }
1965                 if (IS_I_VERSION(inode) && inode_maybe_inc_iversion(inode, updated))
1966                         updated |= S_VERSION;
1967         } else {
1968                 now = current_time(inode);
1969         }
1970
1971         if (flags & S_ATIME) {
1972                 struct timespec64 atime = inode_get_atime(inode);
1973
1974                 if (!timespec64_equal(&now, &atime)) {
1975                         inode_set_atime_to_ts(inode, now);
1976                         updated |= S_ATIME;
1977                 }
1978         }
1979         return updated;
1980 }
1981 EXPORT_SYMBOL(inode_update_timestamps);
1982
1983 /**
1984  * generic_update_time - update the timestamps on the inode
1985  * @inode: inode to be updated
1986  * @flags: S_* flags that needed to be updated
1987  *
1988  * The update_time function is called when an inode's timestamps need to be
1989  * updated for a read or write operation. In the case where any of S_MTIME, S_CTIME,
1990  * or S_VERSION need to be updated we attempt to update all three of them. S_ATIME
1991  * updates can be handled done independently of the rest.
1992  *
1993  * Returns a S_* mask indicating which fields were updated.
1994  */
1995 int generic_update_time(struct inode *inode, int flags)
1996 {
1997         int updated = inode_update_timestamps(inode, flags);
1998         int dirty_flags = 0;
1999
2000         if (updated & (S_ATIME|S_MTIME|S_CTIME))
2001                 dirty_flags = inode->i_sb->s_flags & SB_LAZYTIME ? I_DIRTY_TIME : I_DIRTY_SYNC;
2002         if (updated & S_VERSION)
2003                 dirty_flags |= I_DIRTY_SYNC;
2004         __mark_inode_dirty(inode, dirty_flags);
2005         return updated;
2006 }
2007 EXPORT_SYMBOL(generic_update_time);
2008
2009 /*
2010  * This does the actual work of updating an inodes time or version.  Must have
2011  * had called mnt_want_write() before calling this.
2012  */
2013 int inode_update_time(struct inode *inode, int flags)
2014 {
2015         if (inode->i_op->update_time)
2016                 return inode->i_op->update_time(inode, flags);
2017         generic_update_time(inode, flags);
2018         return 0;
2019 }
2020 EXPORT_SYMBOL(inode_update_time);
2021
2022 /**
2023  *      atime_needs_update      -       update the access time
2024  *      @path: the &struct path to update
2025  *      @inode: inode to update
2026  *
2027  *      Update the accessed time on an inode and mark it for writeback.
2028  *      This function automatically handles read only file systems and media,
2029  *      as well as the "noatime" flag and inode specific "noatime" markers.
2030  */
2031 bool atime_needs_update(const struct path *path, struct inode *inode)
2032 {
2033         struct vfsmount *mnt = path->mnt;
2034         struct timespec64 now, atime;
2035
2036         if (inode->i_flags & S_NOATIME)
2037                 return false;
2038
2039         /* Atime updates will likely cause i_uid and i_gid to be written
2040          * back improprely if their true value is unknown to the vfs.
2041          */
2042         if (HAS_UNMAPPED_ID(mnt_idmap(mnt), inode))
2043                 return false;
2044
2045         if (IS_NOATIME(inode))
2046                 return false;
2047         if ((inode->i_sb->s_flags & SB_NODIRATIME) && S_ISDIR(inode->i_mode))
2048                 return false;
2049
2050         if (mnt->mnt_flags & MNT_NOATIME)
2051                 return false;
2052         if ((mnt->mnt_flags & MNT_NODIRATIME) && S_ISDIR(inode->i_mode))
2053                 return false;
2054
2055         now = current_time(inode);
2056
2057         if (!relatime_need_update(mnt, inode, now))
2058                 return false;
2059
2060         atime = inode_get_atime(inode);
2061         if (timespec64_equal(&atime, &now))
2062                 return false;
2063
2064         return true;
2065 }
2066
2067 void touch_atime(const struct path *path)
2068 {
2069         struct vfsmount *mnt = path->mnt;
2070         struct inode *inode = d_inode(path->dentry);
2071
2072         if (!atime_needs_update(path, inode))
2073                 return;
2074
2075         if (!sb_start_write_trylock(inode->i_sb))
2076                 return;
2077
2078         if (mnt_get_write_access(mnt) != 0)
2079                 goto skip_update;
2080         /*
2081          * File systems can error out when updating inodes if they need to
2082          * allocate new space to modify an inode (such is the case for
2083          * Btrfs), but since we touch atime while walking down the path we
2084          * really don't care if we failed to update the atime of the file,
2085          * so just ignore the return value.
2086          * We may also fail on filesystems that have the ability to make parts
2087          * of the fs read only, e.g. subvolumes in Btrfs.
2088          */
2089         inode_update_time(inode, S_ATIME);
2090         mnt_put_write_access(mnt);
2091 skip_update:
2092         sb_end_write(inode->i_sb);
2093 }
2094 EXPORT_SYMBOL(touch_atime);
2095
2096 /*
2097  * Return mask of changes for notify_change() that need to be done as a
2098  * response to write or truncate. Return 0 if nothing has to be changed.
2099  * Negative value on error (change should be denied).
2100  */
2101 int dentry_needs_remove_privs(struct mnt_idmap *idmap,
2102                               struct dentry *dentry)
2103 {
2104         struct inode *inode = d_inode(dentry);
2105         int mask = 0;
2106         int ret;
2107
2108         if (IS_NOSEC(inode))
2109                 return 0;
2110
2111         mask = setattr_should_drop_suidgid(idmap, inode);
2112         ret = security_inode_need_killpriv(dentry);
2113         if (ret < 0)
2114                 return ret;
2115         if (ret)
2116                 mask |= ATTR_KILL_PRIV;
2117         return mask;
2118 }
2119
2120 static int __remove_privs(struct mnt_idmap *idmap,
2121                           struct dentry *dentry, int kill)
2122 {
2123         struct iattr newattrs;
2124
2125         newattrs.ia_valid = ATTR_FORCE | kill;
2126         /*
2127          * Note we call this on write, so notify_change will not
2128          * encounter any conflicting delegations:
2129          */
2130         return notify_change(idmap, dentry, &newattrs, NULL);
2131 }
2132
2133 int file_remove_privs_flags(struct file *file, unsigned int flags)
2134 {
2135         struct dentry *dentry = file_dentry(file);
2136         struct inode *inode = file_inode(file);
2137         int error = 0;
2138         int kill;
2139
2140         if (IS_NOSEC(inode) || !S_ISREG(inode->i_mode))
2141                 return 0;
2142
2143         kill = dentry_needs_remove_privs(file_mnt_idmap(file), dentry);
2144         if (kill < 0)
2145                 return kill;
2146
2147         if (kill) {
2148                 if (flags & IOCB_NOWAIT)
2149                         return -EAGAIN;
2150
2151                 error = __remove_privs(file_mnt_idmap(file), dentry, kill);
2152         }
2153
2154         if (!error)
2155                 inode_has_no_xattr(inode);
2156         return error;
2157 }
2158 EXPORT_SYMBOL_GPL(file_remove_privs_flags);
2159
2160 /**
2161  * file_remove_privs - remove special file privileges (suid, capabilities)
2162  * @file: file to remove privileges from
2163  *
2164  * When file is modified by a write or truncation ensure that special
2165  * file privileges are removed.
2166  *
2167  * Return: 0 on success, negative errno on failure.
2168  */
2169 int file_remove_privs(struct file *file)
2170 {
2171         return file_remove_privs_flags(file, 0);
2172 }
2173 EXPORT_SYMBOL(file_remove_privs);
2174
2175 static int inode_needs_update_time(struct inode *inode)
2176 {
2177         int sync_it = 0;
2178         struct timespec64 now = current_time(inode);
2179         struct timespec64 ts;
2180
2181         /* First try to exhaust all avenues to not sync */
2182         if (IS_NOCMTIME(inode))
2183                 return 0;
2184
2185         ts = inode_get_mtime(inode);
2186         if (!timespec64_equal(&ts, &now))
2187                 sync_it = S_MTIME;
2188
2189         ts = inode_get_ctime(inode);
2190         if (!timespec64_equal(&ts, &now))
2191                 sync_it |= S_CTIME;
2192
2193         if (IS_I_VERSION(inode) && inode_iversion_need_inc(inode))
2194                 sync_it |= S_VERSION;
2195
2196         return sync_it;
2197 }
2198
2199 static int __file_update_time(struct file *file, int sync_mode)
2200 {
2201         int ret = 0;
2202         struct inode *inode = file_inode(file);
2203
2204         /* try to update time settings */
2205         if (!mnt_get_write_access_file(file)) {
2206                 ret = inode_update_time(inode, sync_mode);
2207                 mnt_put_write_access_file(file);
2208         }
2209
2210         return ret;
2211 }
2212
2213 /**
2214  * file_update_time - update mtime and ctime time
2215  * @file: file accessed
2216  *
2217  * Update the mtime and ctime members of an inode and mark the inode for
2218  * writeback. Note that this function is meant exclusively for usage in
2219  * the file write path of filesystems, and filesystems may choose to
2220  * explicitly ignore updates via this function with the _NOCMTIME inode
2221  * flag, e.g. for network filesystem where these imestamps are handled
2222  * by the server. This can return an error for file systems who need to
2223  * allocate space in order to update an inode.
2224  *
2225  * Return: 0 on success, negative errno on failure.
2226  */
2227 int file_update_time(struct file *file)
2228 {
2229         int ret;
2230         struct inode *inode = file_inode(file);
2231
2232         ret = inode_needs_update_time(inode);
2233         if (ret <= 0)
2234                 return ret;
2235
2236         return __file_update_time(file, ret);
2237 }
2238 EXPORT_SYMBOL(file_update_time);
2239
2240 /**
2241  * file_modified_flags - handle mandated vfs changes when modifying a file
2242  * @file: file that was modified
2243  * @flags: kiocb flags
2244  *
2245  * When file has been modified ensure that special
2246  * file privileges are removed and time settings are updated.
2247  *
2248  * If IOCB_NOWAIT is set, special file privileges will not be removed and
2249  * time settings will not be updated. It will return -EAGAIN.
2250  *
2251  * Context: Caller must hold the file's inode lock.
2252  *
2253  * Return: 0 on success, negative errno on failure.
2254  */
2255 static int file_modified_flags(struct file *file, int flags)
2256 {
2257         int ret;
2258         struct inode *inode = file_inode(file);
2259
2260         /*
2261          * Clear the security bits if the process is not being run by root.
2262          * This keeps people from modifying setuid and setgid binaries.
2263          */
2264         ret = file_remove_privs_flags(file, flags);
2265         if (ret)
2266                 return ret;
2267
2268         if (unlikely(file->f_mode & FMODE_NOCMTIME))
2269                 return 0;
2270
2271         ret = inode_needs_update_time(inode);
2272         if (ret <= 0)
2273                 return ret;
2274         if (flags & IOCB_NOWAIT)
2275                 return -EAGAIN;
2276
2277         return __file_update_time(file, ret);
2278 }
2279
2280 /**
2281  * file_modified - handle mandated vfs changes when modifying a file
2282  * @file: file that was modified
2283  *
2284  * When file has been modified ensure that special
2285  * file privileges are removed and time settings are updated.
2286  *
2287  * Context: Caller must hold the file's inode lock.
2288  *
2289  * Return: 0 on success, negative errno on failure.
2290  */
2291 int file_modified(struct file *file)
2292 {
2293         return file_modified_flags(file, 0);
2294 }
2295 EXPORT_SYMBOL(file_modified);
2296
2297 /**
2298  * kiocb_modified - handle mandated vfs changes when modifying a file
2299  * @iocb: iocb that was modified
2300  *
2301  * When file has been modified ensure that special
2302  * file privileges are removed and time settings are updated.
2303  *
2304  * Context: Caller must hold the file's inode lock.
2305  *
2306  * Return: 0 on success, negative errno on failure.
2307  */
2308 int kiocb_modified(struct kiocb *iocb)
2309 {
2310         return file_modified_flags(iocb->ki_filp, iocb->ki_flags);
2311 }
2312 EXPORT_SYMBOL_GPL(kiocb_modified);
2313
2314 int inode_needs_sync(struct inode *inode)
2315 {
2316         if (IS_SYNC(inode))
2317                 return 1;
2318         if (S_ISDIR(inode->i_mode) && IS_DIRSYNC(inode))
2319                 return 1;
2320         return 0;
2321 }
2322 EXPORT_SYMBOL(inode_needs_sync);
2323
2324 /*
2325  * If we try to find an inode in the inode hash while it is being
2326  * deleted, we have to wait until the filesystem completes its
2327  * deletion before reporting that it isn't found.  This function waits
2328  * until the deletion _might_ have completed.  Callers are responsible
2329  * to recheck inode state.
2330  *
2331  * It doesn't matter if I_NEW is not set initially, a call to
2332  * wake_up_bit(&inode->i_state, __I_NEW) after removing from the hash list
2333  * will DTRT.
2334  */
2335 static void __wait_on_freeing_inode(struct inode *inode, bool is_inode_hash_locked)
2336 {
2337         wait_queue_head_t *wq;
2338         DEFINE_WAIT_BIT(wait, &inode->i_state, __I_NEW);
2339
2340         /*
2341          * Handle racing against evict(), see that routine for more details.
2342          */
2343         if (unlikely(inode_unhashed(inode))) {
2344                 WARN_ON(is_inode_hash_locked);
2345                 spin_unlock(&inode->i_lock);
2346                 return;
2347         }
2348
2349         wq = bit_waitqueue(&inode->i_state, __I_NEW);
2350         prepare_to_wait(wq, &wait.wq_entry, TASK_UNINTERRUPTIBLE);
2351         spin_unlock(&inode->i_lock);
2352         rcu_read_unlock();
2353         if (is_inode_hash_locked)
2354                 spin_unlock(&inode_hash_lock);
2355         schedule();
2356         finish_wait(wq, &wait.wq_entry);
2357         if (is_inode_hash_locked)
2358                 spin_lock(&inode_hash_lock);
2359         rcu_read_lock();
2360 }
2361
2362 static __initdata unsigned long ihash_entries;
2363 static int __init set_ihash_entries(char *str)
2364 {
2365         if (!str)
2366                 return 0;
2367         ihash_entries = simple_strtoul(str, &str, 0);
2368         return 1;
2369 }
2370 __setup("ihash_entries=", set_ihash_entries);
2371
2372 /*
2373  * Initialize the waitqueues and inode hash table.
2374  */
2375 void __init inode_init_early(void)
2376 {
2377         /* If hashes are distributed across NUMA nodes, defer
2378          * hash allocation until vmalloc space is available.
2379          */
2380         if (hashdist)
2381                 return;
2382
2383         inode_hashtable =
2384                 alloc_large_system_hash("Inode-cache",
2385                                         sizeof(struct hlist_head),
2386                                         ihash_entries,
2387                                         14,
2388                                         HASH_EARLY | HASH_ZERO,
2389                                         &i_hash_shift,
2390                                         &i_hash_mask,
2391                                         0,
2392                                         0);
2393 }
2394
2395 void __init inode_init(void)
2396 {
2397         /* inode slab cache */
2398         inode_cachep = kmem_cache_create("inode_cache",
2399                                          sizeof(struct inode),
2400                                          0,
2401                                          (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
2402                                          SLAB_ACCOUNT),
2403                                          init_once);
2404
2405         /* Hash may have been set up in inode_init_early */
2406         if (!hashdist)
2407                 return;
2408
2409         inode_hashtable =
2410                 alloc_large_system_hash("Inode-cache",
2411                                         sizeof(struct hlist_head),
2412                                         ihash_entries,
2413                                         14,
2414                                         HASH_ZERO,
2415                                         &i_hash_shift,
2416                                         &i_hash_mask,
2417                                         0,
2418                                         0);
2419 }
2420
2421 void init_special_inode(struct inode *inode, umode_t mode, dev_t rdev)
2422 {
2423         inode->i_mode = mode;
2424         if (S_ISCHR(mode)) {
2425                 inode->i_fop = &def_chr_fops;
2426                 inode->i_rdev = rdev;
2427         } else if (S_ISBLK(mode)) {
2428                 if (IS_ENABLED(CONFIG_BLOCK))
2429                         inode->i_fop = &def_blk_fops;
2430                 inode->i_rdev = rdev;
2431         } else if (S_ISFIFO(mode))
2432                 inode->i_fop = &pipefifo_fops;
2433         else if (S_ISSOCK(mode))
2434                 ;       /* leave it no_open_fops */
2435         else
2436                 printk(KERN_DEBUG "init_special_inode: bogus i_mode (%o) for"
2437                                   " inode %s:%lu\n", mode, inode->i_sb->s_id,
2438                                   inode->i_ino);
2439 }
2440 EXPORT_SYMBOL(init_special_inode);
2441
2442 /**
2443  * inode_init_owner - Init uid,gid,mode for new inode according to posix standards
2444  * @idmap: idmap of the mount the inode was created from
2445  * @inode: New inode
2446  * @dir: Directory inode
2447  * @mode: mode of the new inode
2448  *
2449  * If the inode has been created through an idmapped mount the idmap of
2450  * the vfsmount must be passed through @idmap. This function will then take
2451  * care to map the inode according to @idmap before checking permissions
2452  * and initializing i_uid and i_gid. On non-idmapped mounts or if permission
2453  * checking is to be performed on the raw inode simply pass @nop_mnt_idmap.
2454  */
2455 void inode_init_owner(struct mnt_idmap *idmap, struct inode *inode,
2456                       const struct inode *dir, umode_t mode)
2457 {
2458         inode_fsuid_set(inode, idmap);
2459         if (dir && dir->i_mode & S_ISGID) {
2460                 inode->i_gid = dir->i_gid;
2461
2462                 /* Directories are special, and always inherit S_ISGID */
2463                 if (S_ISDIR(mode))
2464                         mode |= S_ISGID;
2465         } else
2466                 inode_fsgid_set(inode, idmap);
2467         inode->i_mode = mode;
2468 }
2469 EXPORT_SYMBOL(inode_init_owner);
2470
2471 /**
2472  * inode_owner_or_capable - check current task permissions to inode
2473  * @idmap: idmap of the mount the inode was found from
2474  * @inode: inode being checked
2475  *
2476  * Return true if current either has CAP_FOWNER in a namespace with the
2477  * inode owner uid mapped, or owns the file.
2478  *
2479  * If the inode has been found through an idmapped mount the idmap of
2480  * the vfsmount must be passed through @idmap. This function will then take
2481  * care to map the inode according to @idmap before checking permissions.
2482  * On non-idmapped mounts or if permission checking is to be performed on the
2483  * raw inode simply pass @nop_mnt_idmap.
2484  */
2485 bool inode_owner_or_capable(struct mnt_idmap *idmap,
2486                             const struct inode *inode)
2487 {
2488         vfsuid_t vfsuid;
2489         struct user_namespace *ns;
2490
2491         vfsuid = i_uid_into_vfsuid(idmap, inode);
2492         if (vfsuid_eq_kuid(vfsuid, current_fsuid()))
2493                 return true;
2494
2495         ns = current_user_ns();
2496         if (vfsuid_has_mapping(ns, vfsuid) && ns_capable(ns, CAP_FOWNER))
2497                 return true;
2498         return false;
2499 }
2500 EXPORT_SYMBOL(inode_owner_or_capable);
2501
2502 /*
2503  * Direct i/o helper functions
2504  */
2505 static void __inode_dio_wait(struct inode *inode)
2506 {
2507         wait_queue_head_t *wq = bit_waitqueue(&inode->i_state, __I_DIO_WAKEUP);
2508         DEFINE_WAIT_BIT(q, &inode->i_state, __I_DIO_WAKEUP);
2509
2510         do {
2511                 prepare_to_wait(wq, &q.wq_entry, TASK_UNINTERRUPTIBLE);
2512                 if (atomic_read(&inode->i_dio_count))
2513                         schedule();
2514         } while (atomic_read(&inode->i_dio_count));
2515         finish_wait(wq, &q.wq_entry);
2516 }
2517
2518 /**
2519  * inode_dio_wait - wait for outstanding DIO requests to finish
2520  * @inode: inode to wait for
2521  *
2522  * Waits for all pending direct I/O requests to finish so that we can
2523  * proceed with a truncate or equivalent operation.
2524  *
2525  * Must be called under a lock that serializes taking new references
2526  * to i_dio_count, usually by inode->i_mutex.
2527  */
2528 void inode_dio_wait(struct inode *inode)
2529 {
2530         if (atomic_read(&inode->i_dio_count))
2531                 __inode_dio_wait(inode);
2532 }
2533 EXPORT_SYMBOL(inode_dio_wait);
2534
2535 /*
2536  * inode_set_flags - atomically set some inode flags
2537  *
2538  * Note: the caller should be holding i_mutex, or else be sure that
2539  * they have exclusive access to the inode structure (i.e., while the
2540  * inode is being instantiated).  The reason for the cmpxchg() loop
2541  * --- which wouldn't be necessary if all code paths which modify
2542  * i_flags actually followed this rule, is that there is at least one
2543  * code path which doesn't today so we use cmpxchg() out of an abundance
2544  * of caution.
2545  *
2546  * In the long run, i_mutex is overkill, and we should probably look
2547  * at using the i_lock spinlock to protect i_flags, and then make sure
2548  * it is so documented in include/linux/fs.h and that all code follows
2549  * the locking convention!!
2550  */
2551 void inode_set_flags(struct inode *inode, unsigned int flags,
2552                      unsigned int mask)
2553 {
2554         WARN_ON_ONCE(flags & ~mask);
2555         set_mask_bits(&inode->i_flags, mask, flags);
2556 }
2557 EXPORT_SYMBOL(inode_set_flags);
2558
2559 void inode_nohighmem(struct inode *inode)
2560 {
2561         mapping_set_gfp_mask(inode->i_mapping, GFP_USER);
2562 }
2563 EXPORT_SYMBOL(inode_nohighmem);
2564
2565 /**
2566  * timestamp_truncate - Truncate timespec to a granularity
2567  * @t: Timespec
2568  * @inode: inode being updated
2569  *
2570  * Truncate a timespec to the granularity supported by the fs
2571  * containing the inode. Always rounds down. gran must
2572  * not be 0 nor greater than a second (NSEC_PER_SEC, or 10^9 ns).
2573  */
2574 struct timespec64 timestamp_truncate(struct timespec64 t, struct inode *inode)
2575 {
2576         struct super_block *sb = inode->i_sb;
2577         unsigned int gran = sb->s_time_gran;
2578
2579         t.tv_sec = clamp(t.tv_sec, sb->s_time_min, sb->s_time_max);
2580         if (unlikely(t.tv_sec == sb->s_time_max || t.tv_sec == sb->s_time_min))
2581                 t.tv_nsec = 0;
2582
2583         /* Avoid division in the common cases 1 ns and 1 s. */
2584         if (gran == 1)
2585                 ; /* nothing */
2586         else if (gran == NSEC_PER_SEC)
2587                 t.tv_nsec = 0;
2588         else if (gran > 1 && gran < NSEC_PER_SEC)
2589                 t.tv_nsec -= t.tv_nsec % gran;
2590         else
2591                 WARN(1, "invalid file time granularity: %u", gran);
2592         return t;
2593 }
2594 EXPORT_SYMBOL(timestamp_truncate);
2595
2596 /**
2597  * current_time - Return FS time
2598  * @inode: inode.
2599  *
2600  * Return the current time truncated to the time granularity supported by
2601  * the fs.
2602  *
2603  * Note that inode and inode->sb cannot be NULL.
2604  * Otherwise, the function warns and returns time without truncation.
2605  */
2606 struct timespec64 current_time(struct inode *inode)
2607 {
2608         struct timespec64 now;
2609
2610         ktime_get_coarse_real_ts64(&now);
2611         return timestamp_truncate(now, inode);
2612 }
2613 EXPORT_SYMBOL(current_time);
2614
2615 /**
2616  * inode_set_ctime_current - set the ctime to current_time
2617  * @inode: inode
2618  *
2619  * Set the inode->i_ctime to the current value for the inode. Returns
2620  * the current value that was assigned to i_ctime.
2621  */
2622 struct timespec64 inode_set_ctime_current(struct inode *inode)
2623 {
2624         struct timespec64 now = current_time(inode);
2625
2626         inode_set_ctime_to_ts(inode, now);
2627         return now;
2628 }
2629 EXPORT_SYMBOL(inode_set_ctime_current);
2630
2631 /**
2632  * in_group_or_capable - check whether caller is CAP_FSETID privileged
2633  * @idmap:      idmap of the mount @inode was found from
2634  * @inode:      inode to check
2635  * @vfsgid:     the new/current vfsgid of @inode
2636  *
2637  * Check wether @vfsgid is in the caller's group list or if the caller is
2638  * privileged with CAP_FSETID over @inode. This can be used to determine
2639  * whether the setgid bit can be kept or must be dropped.
2640  *
2641  * Return: true if the caller is sufficiently privileged, false if not.
2642  */
2643 bool in_group_or_capable(struct mnt_idmap *idmap,
2644                          const struct inode *inode, vfsgid_t vfsgid)
2645 {
2646         if (vfsgid_in_group_p(vfsgid))
2647                 return true;
2648         if (capable_wrt_inode_uidgid(idmap, inode, CAP_FSETID))
2649                 return true;
2650         return false;
2651 }
2652 EXPORT_SYMBOL(in_group_or_capable);
2653
2654 /**
2655  * mode_strip_sgid - handle the sgid bit for non-directories
2656  * @idmap: idmap of the mount the inode was created from
2657  * @dir: parent directory inode
2658  * @mode: mode of the file to be created in @dir
2659  *
2660  * If the @mode of the new file has both the S_ISGID and S_IXGRP bit
2661  * raised and @dir has the S_ISGID bit raised ensure that the caller is
2662  * either in the group of the parent directory or they have CAP_FSETID
2663  * in their user namespace and are privileged over the parent directory.
2664  * In all other cases, strip the S_ISGID bit from @mode.
2665  *
2666  * Return: the new mode to use for the file
2667  */
2668 umode_t mode_strip_sgid(struct mnt_idmap *idmap,
2669                         const struct inode *dir, umode_t mode)
2670 {
2671         if ((mode & (S_ISGID | S_IXGRP)) != (S_ISGID | S_IXGRP))
2672                 return mode;
2673         if (S_ISDIR(mode) || !dir || !(dir->i_mode & S_ISGID))
2674                 return mode;
2675         if (in_group_or_capable(idmap, dir, i_gid_into_vfsgid(idmap, dir)))
2676                 return mode;
2677         return mode & ~S_ISGID;
2678 }
2679 EXPORT_SYMBOL(mode_strip_sgid);
This page took 0.170976 seconds and 4 git commands to generate.