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