xref: /linux/fs/libfs.c (revision 02a336a831f02a18047bacbd49018a237af8a172)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *	fs/libfs.c
4  *	Library for filesystems writers.
5  */
6 
7 #include <linux/blkdev.h>
8 #include <linux/export.h>
9 #include <linux/filelock.h>
10 #include <linux/pagemap.h>
11 #include <linux/slab.h>
12 #include <linux/cred.h>
13 #include <linux/mount.h>
14 #include <linux/vfs.h>
15 #include <linux/quotaops.h>
16 #include <linux/mutex.h>
17 #include <linux/namei.h>
18 #include <linux/exportfs.h>
19 #include <linux/iversion.h>
20 #include <linux/writeback.h>
21 #include <linux/fs_context.h>
22 #include <linux/pseudo_fs.h>
23 #include <linux/fsnotify.h>
24 #include <linux/unicode.h>
25 #include <linux/fscrypt.h>
26 #include <linux/pidfs.h>
27 
28 #include <linux/uaccess.h>
29 
30 #include "internal.h"
31 
32 int simple_getattr(const struct mnt_idmap *idmap, const struct path *path,
33 		   struct kstat *stat, u32 request_mask,
34 		   unsigned int query_flags)
35 {
36 	struct inode *inode = d_inode(path->dentry);
37 	generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
38 	stat->blocks = inode->i_mapping->nrpages << (PAGE_SHIFT - 9);
39 	return 0;
40 }
41 EXPORT_SYMBOL(simple_getattr);
42 
43 int simple_statfs(struct dentry *dentry, struct kstatfs *buf)
44 {
45 	u64 id = huge_encode_dev(dentry->d_sb->s_dev);
46 
47 	buf->f_fsid = u64_to_fsid(id);
48 	buf->f_type = dentry->d_sb->s_magic;
49 	buf->f_bsize = PAGE_SIZE;
50 	buf->f_namelen = NAME_MAX;
51 	return 0;
52 }
53 EXPORT_SYMBOL(simple_statfs);
54 
55 /*
56  * Retaining negative dentries for an in-memory filesystem just wastes
57  * memory and lookup time: arrange for them to be deleted immediately.
58  */
59 int always_delete_dentry(const struct dentry *dentry)
60 {
61 	return 1;
62 }
63 EXPORT_SYMBOL(always_delete_dentry);
64 
65 /*
66  * Lookup the data. This is trivial - if the dentry didn't already
67  * exist, we know it is negative.  Set d_op to delete negative dentries.
68  */
69 struct dentry *simple_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
70 {
71 	if (dentry->d_name.len > NAME_MAX)
72 		return ERR_PTR(-ENAMETOOLONG);
73 	if (!dentry->d_op && !(dentry->d_flags & DCACHE_DONTCACHE)) {
74 		spin_lock(&dentry->d_lock);
75 		dentry->d_flags |= DCACHE_DONTCACHE;
76 		spin_unlock(&dentry->d_lock);
77 	}
78 	if (IS_ENABLED(CONFIG_UNICODE) && IS_CASEFOLDED(dir))
79 		return NULL;
80 
81 	return d_splice_alias(NULL, dentry);
82 }
83 EXPORT_SYMBOL(simple_lookup);
84 
85 int dcache_dir_open(struct inode *inode, struct file *file)
86 {
87 	file->private_data = d_alloc_cursor(file->f_path.dentry);
88 
89 	return file->private_data ? 0 : -ENOMEM;
90 }
91 EXPORT_SYMBOL(dcache_dir_open);
92 
93 int dcache_dir_close(struct inode *inode, struct file *file)
94 {
95 	dput(file->private_data);
96 	return 0;
97 }
98 EXPORT_SYMBOL(dcache_dir_close);
99 
100 /* parent is locked at least shared */
101 /*
102  * Returns an element of siblings' list.
103  * We are looking for <count>th positive after <p>; if
104  * found, dentry is grabbed and returned to caller.
105  * If no such element exists, NULL is returned.
106  */
107 static struct dentry *scan_positives(struct dentry *cursor,
108 					struct hlist_node **p,
109 					loff_t count,
110 					struct dentry *last)
111 {
112 	struct dentry *dentry = cursor->d_parent, *found = NULL;
113 
114 	spin_lock(&dentry->d_lock);
115 	while (*p) {
116 		struct dentry *d = hlist_entry(*p, struct dentry, d_sib);
117 		p = &d->d_sib.next;
118 		// we must at least skip cursors, to avoid livelocks
119 		if (d->d_flags & DCACHE_DENTRY_CURSOR)
120 			continue;
121 		if (simple_positive(d) && !--count) {
122 			spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
123 			if (simple_positive(d))
124 				found = dget_dlock(d);
125 			spin_unlock(&d->d_lock);
126 			if (likely(found))
127 				break;
128 			count = 1;
129 		}
130 		if (need_resched()) {
131 			if (!hlist_unhashed(&cursor->d_sib))
132 				__hlist_del(&cursor->d_sib);
133 			hlist_add_behind(&cursor->d_sib, &d->d_sib);
134 			p = &cursor->d_sib.next;
135 			spin_unlock(&dentry->d_lock);
136 			cond_resched();
137 			spin_lock(&dentry->d_lock);
138 		}
139 	}
140 	spin_unlock(&dentry->d_lock);
141 	dput(last);
142 	return found;
143 }
144 
145 loff_t dcache_dir_lseek(struct file *file, loff_t offset, int whence)
146 {
147 	struct dentry *dentry = file->f_path.dentry;
148 	switch (whence) {
149 		case 1:
150 			offset += file->f_pos;
151 			fallthrough;
152 		case 0:
153 			if (offset >= 0)
154 				break;
155 			fallthrough;
156 		default:
157 			return -EINVAL;
158 	}
159 	if (offset != file->f_pos) {
160 		struct dentry *cursor = file->private_data;
161 		struct dentry *to = NULL;
162 
163 		inode_lock_shared(dentry->d_inode);
164 
165 		if (offset > 2)
166 			to = scan_positives(cursor, &dentry->d_children.first,
167 					    offset - 2, NULL);
168 		spin_lock(&dentry->d_lock);
169 		hlist_del_init(&cursor->d_sib);
170 		if (to)
171 			hlist_add_behind(&cursor->d_sib, &to->d_sib);
172 		spin_unlock(&dentry->d_lock);
173 		dput(to);
174 
175 		file->f_pos = offset;
176 
177 		inode_unlock_shared(dentry->d_inode);
178 	}
179 	return offset;
180 }
181 EXPORT_SYMBOL(dcache_dir_lseek);
182 
183 /*
184  * Directory is locked and all positive dentries in it are safe, since
185  * for ramfs-type trees they can't go away without unlink() or rmdir(),
186  * both impossible due to the lock on directory.
187  */
188 
189 int dcache_readdir(struct file *file, struct dir_context *ctx)
190 {
191 	struct dentry *dentry = file->f_path.dentry;
192 	struct dentry *cursor = file->private_data;
193 	struct dentry *next = NULL;
194 	struct hlist_node **p;
195 
196 	if (!dir_emit_dots(file, ctx))
197 		return 0;
198 
199 	if (ctx->pos == 2)
200 		p = &dentry->d_children.first;
201 	else
202 		p = &cursor->d_sib.next;
203 
204 	while ((next = scan_positives(cursor, p, 1, next)) != NULL) {
205 		if (!dir_emit(ctx, next->d_name.name, next->d_name.len,
206 			      d_inode(next)->i_ino,
207 			      fs_umode_to_dtype(d_inode(next)->i_mode)))
208 			break;
209 		ctx->pos++;
210 		p = &next->d_sib.next;
211 	}
212 	spin_lock(&dentry->d_lock);
213 	hlist_del_init(&cursor->d_sib);
214 	if (next)
215 		hlist_add_before(&cursor->d_sib, &next->d_sib);
216 	spin_unlock(&dentry->d_lock);
217 	dput(next);
218 
219 	return 0;
220 }
221 EXPORT_SYMBOL(dcache_readdir);
222 
223 ssize_t generic_read_dir(struct file *filp, char __user *buf, size_t siz, loff_t *ppos)
224 {
225 	return -EISDIR;
226 }
227 EXPORT_SYMBOL(generic_read_dir);
228 
229 const struct file_operations simple_dir_operations = {
230 	.open		= dcache_dir_open,
231 	.release	= dcache_dir_close,
232 	.llseek		= dcache_dir_lseek,
233 	.read		= generic_read_dir,
234 	.iterate_shared	= dcache_readdir,
235 	.fsync		= noop_fsync,
236 };
237 EXPORT_SYMBOL(simple_dir_operations);
238 
239 const struct inode_operations simple_dir_inode_operations = {
240 	.lookup		= simple_lookup,
241 };
242 EXPORT_SYMBOL(simple_dir_inode_operations);
243 
244 /* simple_offset_add() never assigns these to a dentry */
245 enum {
246 	DIR_OFFSET_FIRST	= 2,		/* Find first real entry */
247 	DIR_OFFSET_EOD		= S32_MAX,
248 };
249 
250 /* simple_offset_add() allocation range */
251 enum {
252 	DIR_OFFSET_MIN		= DIR_OFFSET_FIRST + 1,
253 	DIR_OFFSET_MAX		= DIR_OFFSET_EOD - 1,
254 };
255 
256 static void offset_set(struct dentry *dentry, long offset)
257 {
258 	dentry->d_fsdata = (void *)offset;
259 }
260 
261 static long dentry2offset(struct dentry *dentry)
262 {
263 	return (long)dentry->d_fsdata;
264 }
265 
266 static struct lock_class_key simple_offset_lock_class;
267 
268 /**
269  * simple_offset_init - initialize an offset_ctx
270  * @octx: directory offset map to be initialized
271  *
272  */
273 void simple_offset_init(struct offset_ctx *octx)
274 {
275 	mt_init_flags(&octx->mt, MT_FLAGS_ALLOC_RANGE);
276 	lockdep_set_class(&octx->mt.ma_lock, &simple_offset_lock_class);
277 	octx->next_offset = DIR_OFFSET_MIN;
278 }
279 
280 /**
281  * simple_offset_add - Add an entry to a directory's offset map
282  * @octx: directory offset ctx to be updated
283  * @dentry: new dentry being added
284  *
285  * Returns zero on success. @octx and the dentry's offset are updated.
286  * Otherwise, a negative errno value is returned.
287  */
288 int simple_offset_add(struct offset_ctx *octx, struct dentry *dentry)
289 {
290 	unsigned long offset;
291 	int ret;
292 
293 	if (dentry2offset(dentry) != 0)
294 		return -EBUSY;
295 
296 	ret = mtree_alloc_cyclic(&octx->mt, &offset, dentry, DIR_OFFSET_MIN,
297 				 DIR_OFFSET_MAX, &octx->next_offset,
298 				 GFP_KERNEL);
299 	if (unlikely(ret < 0))
300 		return ret == -EBUSY ? -ENOSPC : ret;
301 
302 	offset_set(dentry, offset);
303 	return 0;
304 }
305 
306 static int simple_offset_replace(struct offset_ctx *octx, struct dentry *dentry,
307 				 long offset)
308 {
309 	int ret;
310 
311 	ret = mtree_store(&octx->mt, offset, dentry, GFP_KERNEL);
312 	if (ret)
313 		return ret;
314 	offset_set(dentry, offset);
315 	return 0;
316 }
317 
318 /**
319  * simple_offset_remove - Remove an entry to a directory's offset map
320  * @octx: directory offset ctx to be updated
321  * @dentry: dentry being removed
322  *
323  */
324 void simple_offset_remove(struct offset_ctx *octx, struct dentry *dentry)
325 {
326 	long offset;
327 
328 	offset = dentry2offset(dentry);
329 	if (offset == 0)
330 		return;
331 
332 	mtree_erase(&octx->mt, offset);
333 	offset_set(dentry, 0);
334 }
335 
336 /**
337  * simple_offset_rename - handle directory offsets for rename
338  * @old_dir: parent directory of source entry
339  * @old_dentry: dentry of source entry
340  * @new_dir: parent_directory of destination entry
341  * @new_dentry: dentry of destination
342  *
343  * Caller provides appropriate serialization.
344  *
345  * User space expects the directory offset value of the replaced
346  * (new) directory entry to be unchanged after a rename.
347  *
348  * Caller must have grabbed a slot for new_dentry in the maple_tree
349  * associated with new_dir, even if dentry is negative.
350  */
351 void simple_offset_rename(struct inode *old_dir, struct dentry *old_dentry,
352 			  struct inode *new_dir, struct dentry *new_dentry)
353 {
354 	struct offset_ctx *old_ctx = old_dir->i_op->get_offset_ctx(old_dir);
355 	struct offset_ctx *new_ctx = new_dir->i_op->get_offset_ctx(new_dir);
356 	long new_offset = dentry2offset(new_dentry);
357 
358 	if (WARN_ON(!new_offset))
359 		return;
360 
361 	simple_offset_remove(old_ctx, old_dentry);
362 	offset_set(new_dentry, 0);
363 	WARN_ON(simple_offset_replace(new_ctx, old_dentry, new_offset));
364 }
365 
366 /**
367  * simple_offset_rename_exchange - exchange rename with directory offsets
368  * @old_dir: parent of dentry being moved
369  * @old_dentry: dentry being moved
370  * @new_dir: destination parent
371  * @new_dentry: destination dentry
372  *
373  * This API preserves the directory offset values. Caller provides
374  * appropriate serialization.
375  *
376  * Returns zero on success. Otherwise a negative errno is returned and the
377  * rename is rolled back.
378  */
379 int simple_offset_rename_exchange(struct inode *old_dir,
380 				  struct dentry *old_dentry,
381 				  struct inode *new_dir,
382 				  struct dentry *new_dentry)
383 {
384 	struct offset_ctx *old_ctx = old_dir->i_op->get_offset_ctx(old_dir);
385 	struct offset_ctx *new_ctx = new_dir->i_op->get_offset_ctx(new_dir);
386 	long old_index = dentry2offset(old_dentry);
387 	long new_index = dentry2offset(new_dentry);
388 	int ret;
389 
390 	if (WARN_ON(!old_index || !new_index))
391 		return -EINVAL;
392 
393 	ret = mtree_store(&new_ctx->mt, new_index, old_dentry, GFP_KERNEL);
394 	if (WARN_ON(ret))
395 		return ret;
396 
397 	ret = mtree_store(&old_ctx->mt, old_index, new_dentry, GFP_KERNEL);
398 	if (WARN_ON(ret)) {
399 		mtree_store(&new_ctx->mt, new_index, new_dentry, GFP_KERNEL);
400 		return ret;
401 	}
402 
403 	offset_set(old_dentry, new_index);
404 	offset_set(new_dentry, old_index);
405 	simple_rename_exchange(old_dir, old_dentry, new_dir, new_dentry);
406 	return 0;
407 }
408 
409 /**
410  * simple_offset_destroy - Release offset map
411  * @octx: directory offset ctx that is about to be destroyed
412  *
413  * During fs teardown (eg. umount), a directory's offset map might still
414  * contain entries. xa_destroy() cleans out anything that remains.
415  */
416 void simple_offset_destroy(struct offset_ctx *octx)
417 {
418 	mtree_destroy(&octx->mt);
419 }
420 
421 /**
422  * offset_dir_llseek - Advance the read position of a directory descriptor
423  * @file: an open directory whose position is to be updated
424  * @offset: a byte offset
425  * @whence: enumerator describing the starting position for this update
426  *
427  * SEEK_END, SEEK_DATA, and SEEK_HOLE are not supported for directories.
428  *
429  * Returns the updated read position if successful; otherwise a
430  * negative errno is returned and the read position remains unchanged.
431  */
432 static loff_t offset_dir_llseek(struct file *file, loff_t offset, int whence)
433 {
434 	switch (whence) {
435 	case SEEK_CUR:
436 		offset += file->f_pos;
437 		fallthrough;
438 	case SEEK_SET:
439 		if (offset >= 0)
440 			break;
441 		fallthrough;
442 	default:
443 		return -EINVAL;
444 	}
445 
446 	return vfs_setpos(file, offset, LONG_MAX);
447 }
448 
449 static struct dentry *find_positive_dentry(struct dentry *parent,
450 					   struct dentry *dentry,
451 					   bool next)
452 {
453 	struct dentry *found = NULL;
454 
455 	spin_lock(&parent->d_lock);
456 	if (next)
457 		dentry = d_next_sibling(dentry);
458 	else if (!dentry)
459 		dentry = d_first_child(parent);
460 	hlist_for_each_entry_from(dentry, d_sib) {
461 		if (!simple_positive(dentry))
462 			continue;
463 		spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
464 		if (simple_positive(dentry))
465 			found = dget_dlock(dentry);
466 		spin_unlock(&dentry->d_lock);
467 		if (likely(found))
468 			break;
469 	}
470 	spin_unlock(&parent->d_lock);
471 	return found;
472 }
473 
474 static noinline_for_stack struct dentry *
475 offset_dir_lookup(struct dentry *parent, loff_t offset)
476 {
477 	struct inode *inode = d_inode(parent);
478 	struct offset_ctx *octx = inode->i_op->get_offset_ctx(inode);
479 	struct dentry *child, *found = NULL;
480 
481 	MA_STATE(mas, &octx->mt, offset, offset);
482 
483 	if (offset == DIR_OFFSET_FIRST)
484 		found = find_positive_dentry(parent, NULL, false);
485 	else {
486 		rcu_read_lock();
487 		child = mas_find_rev(&mas, DIR_OFFSET_MIN);
488 		found = find_positive_dentry(parent, child, false);
489 		rcu_read_unlock();
490 	}
491 	return found;
492 }
493 
494 static bool offset_dir_emit(struct dir_context *ctx, struct dentry *dentry)
495 {
496 	struct inode *inode = d_inode(dentry);
497 
498 	return dir_emit(ctx, dentry->d_name.name, dentry->d_name.len,
499 			inode->i_ino, fs_umode_to_dtype(inode->i_mode));
500 }
501 
502 static void offset_iterate_dir(struct file *file, struct dir_context *ctx)
503 {
504 	struct dentry *dir = file->f_path.dentry;
505 	struct dentry *dentry;
506 
507 	dentry = offset_dir_lookup(dir, ctx->pos);
508 	if (!dentry)
509 		goto out_eod;
510 	while (true) {
511 		struct dentry *next;
512 
513 		ctx->pos = dentry2offset(dentry);
514 		if (!offset_dir_emit(ctx, dentry))
515 			break;
516 
517 		next = find_positive_dentry(dir, dentry, true);
518 		dput(dentry);
519 
520 		if (!next)
521 			goto out_eod;
522 		dentry = next;
523 	}
524 	dput(dentry);
525 	return;
526 
527 out_eod:
528 	ctx->pos = DIR_OFFSET_EOD;
529 }
530 
531 /**
532  * offset_readdir - Emit entries starting at offset @ctx->pos
533  * @file: an open directory to iterate over
534  * @ctx: directory iteration context
535  *
536  * Caller must hold @file's i_rwsem to prevent insertion or removal of
537  * entries during this call.
538  *
539  * On entry, @ctx->pos contains an offset that represents the first entry
540  * to be read from the directory.
541  *
542  * The operation continues until there are no more entries to read, or
543  * until the ctx->actor indicates there is no more space in the caller's
544  * output buffer.
545  *
546  * On return, @ctx->pos contains an offset that will read the next entry
547  * in this directory when offset_readdir() is called again with @ctx.
548  * Caller places this value in the d_off field of the last entry in the
549  * user's buffer.
550  *
551  * Return values:
552  *   %0 - Complete
553  */
554 static int offset_readdir(struct file *file, struct dir_context *ctx)
555 {
556 	struct dentry *dir = file->f_path.dentry;
557 
558 	lockdep_assert_held(&d_inode(dir)->i_rwsem);
559 
560 	if (!dir_emit_dots(file, ctx))
561 		return 0;
562 	if (ctx->pos != DIR_OFFSET_EOD)
563 		offset_iterate_dir(file, ctx);
564 	return 0;
565 }
566 
567 const struct file_operations simple_offset_dir_operations = {
568 	.llseek		= offset_dir_llseek,
569 	.iterate_shared	= offset_readdir,
570 	.read		= generic_read_dir,
571 	.fsync		= noop_fsync,
572 	.setlease	= generic_setlease,
573 };
574 
575 struct dentry *find_next_child(struct dentry *parent, struct dentry *prev)
576 {
577 	struct dentry *child = NULL, *d;
578 
579 	spin_lock(&parent->d_lock);
580 	d = prev ? d_next_sibling(prev) : d_first_child(parent);
581 	hlist_for_each_entry_from(d, d_sib) {
582 		if (simple_positive(d)) {
583 			spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
584 			if (simple_positive(d))
585 				child = dget_dlock(d);
586 			spin_unlock(&d->d_lock);
587 			if (likely(child))
588 				break;
589 		}
590 	}
591 	spin_unlock(&parent->d_lock);
592 	dput(prev);
593 	return child;
594 }
595 EXPORT_SYMBOL(find_next_child);
596 
597 static void __simple_recursive_removal(struct dentry *dentry,
598                               void (*callback)(struct dentry *),
599 			      bool locked)
600 {
601 	struct dentry *this = dget(dentry);
602 	while (true) {
603 		struct dentry *victim = NULL, *child;
604 		struct inode *inode = this->d_inode;
605 
606 		inode_lock_nested(inode, I_MUTEX_CHILD);
607 		if (d_is_dir(this))
608 			inode->i_flags |= S_DEAD;
609 		while ((child = find_next_child(this, victim)) == NULL) {
610 			// kill and ascend
611 			// update metadata while it's still locked
612 			inode_set_ctime_current(inode);
613 			clear_nlink(inode);
614 			inode_unlock(inode);
615 			victim = this;
616 			this = this->d_parent;
617 			inode = this->d_inode;
618 			if (!locked || victim != dentry)
619 				inode_lock_nested(inode, I_MUTEX_CHILD);
620 			if (simple_positive(victim)) {
621 				d_invalidate(victim);	// avoid lost mounts
622 				if (callback)
623 					callback(victim);
624 				fsnotify_delete(inode, d_inode(victim), victim);
625 				d_make_discardable(victim);
626 			}
627 			if (victim == dentry) {
628 				inode_set_mtime_to_ts(inode,
629 						      inode_set_ctime_current(inode));
630 				if (d_is_dir(dentry))
631 					drop_nlink(inode);
632 				if (!locked)
633 					inode_unlock(inode);
634 				dput(dentry);
635 				return;
636 			}
637 		}
638 		inode_unlock(inode);
639 		this = child;
640 	}
641 }
642 
643 void simple_recursive_removal(struct dentry *dentry,
644                               void (*callback)(struct dentry *))
645 {
646 	return __simple_recursive_removal(dentry, callback, false);
647 }
648 EXPORT_SYMBOL(simple_recursive_removal);
649 
650 void simple_remove_by_name(struct dentry *parent, const char *name,
651                            void (*callback)(struct dentry *))
652 {
653 	struct dentry *dentry;
654 
655 	dentry = lookup_noperm_positive_unlocked(&QSTR(name), parent);
656 	if (!IS_ERR(dentry)) {
657 		simple_recursive_removal(dentry, callback);
658 		dput(dentry);	// paired with lookup_noperm_positive_unlocked()
659 	}
660 }
661 EXPORT_SYMBOL(simple_remove_by_name);
662 
663 /* caller holds parent directory with I_MUTEX_PARENT */
664 void locked_recursive_removal(struct dentry *dentry,
665                               void (*callback)(struct dentry *))
666 {
667 	return __simple_recursive_removal(dentry, callback, true);
668 }
669 EXPORT_SYMBOL(locked_recursive_removal);
670 
671 static const struct super_operations simple_super_operations = {
672 	.statfs		= simple_statfs,
673 };
674 
675 static int pseudo_fs_fill_super(struct super_block *s, struct fs_context *fc)
676 {
677 	struct pseudo_fs_context *ctx = fc->fs_private;
678 	struct inode *root;
679 
680 	s->s_maxbytes = MAX_LFS_FILESIZE;
681 	s->s_blocksize = PAGE_SIZE;
682 	s->s_blocksize_bits = PAGE_SHIFT;
683 	s->s_magic = ctx->magic;
684 	s->s_op = ctx->ops ?: &simple_super_operations;
685 	s->s_export_op = ctx->eops;
686 	s->s_xattr = ctx->xattr;
687 	s->s_time_gran = 1;
688 	s->s_d_flags |= ctx->s_d_flags;
689 	root = new_inode(s);
690 	if (!root)
691 		return -ENOMEM;
692 
693 	/*
694 	 * since this is the first inode, make it number 1. New inodes created
695 	 * after this must take care not to collide with it (by passing
696 	 * max_reserved of 1 to iunique).
697 	 */
698 	root->i_ino = 1;
699 	root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR;
700 	simple_inode_init_ts(root);
701 	s->s_root = d_make_root(root);
702 	if (!s->s_root)
703 		return -ENOMEM;
704 	set_default_d_op(s, ctx->dops);
705 	return 0;
706 }
707 
708 static int pseudo_fs_get_tree(struct fs_context *fc)
709 {
710 	return get_tree_nodev(fc, pseudo_fs_fill_super);
711 }
712 
713 static void pseudo_fs_free(struct fs_context *fc)
714 {
715 	kfree(fc->fs_private);
716 }
717 
718 static const struct fs_context_operations pseudo_fs_context_ops = {
719 	.free		= pseudo_fs_free,
720 	.get_tree	= pseudo_fs_get_tree,
721 };
722 
723 /*
724  * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that
725  * will never be mountable)
726  */
727 struct pseudo_fs_context *init_pseudo(struct fs_context *fc,
728 					unsigned long magic)
729 {
730 	struct pseudo_fs_context *ctx;
731 
732 	ctx = kzalloc_obj(struct pseudo_fs_context);
733 	if (likely(ctx)) {
734 		ctx->magic = magic;
735 		fc->fs_private = ctx;
736 		fc->ops = &pseudo_fs_context_ops;
737 		fc->sb_flags |= SB_NOUSER;
738 		fc->s_iflags |= SB_I_NOEXEC | SB_I_NODEV;
739 		fc->global = true;
740 	}
741 	return ctx;
742 }
743 EXPORT_SYMBOL(init_pseudo);
744 
745 int simple_open(struct inode *inode, struct file *file)
746 {
747 	if (inode->i_private)
748 		file->private_data = inode->i_private;
749 	return 0;
750 }
751 EXPORT_SYMBOL(simple_open);
752 
753 int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
754 {
755 	struct inode *inode = d_inode(old_dentry);
756 
757 	inode_set_mtime_to_ts(dir,
758 			      inode_set_ctime_to_ts(dir, inode_set_ctime_current(inode)));
759 	inc_nlink(inode);
760 	ihold(inode);
761 	d_make_persistent(dentry, inode);
762 	return 0;
763 }
764 EXPORT_SYMBOL(simple_link);
765 
766 int simple_empty(struct dentry *dentry)
767 {
768 	struct dentry *child;
769 	int ret = 0;
770 
771 	spin_lock(&dentry->d_lock);
772 	hlist_for_each_entry(child, &dentry->d_children, d_sib) {
773 		spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED);
774 		if (simple_positive(child)) {
775 			spin_unlock(&child->d_lock);
776 			goto out;
777 		}
778 		spin_unlock(&child->d_lock);
779 	}
780 	ret = 1;
781 out:
782 	spin_unlock(&dentry->d_lock);
783 	return ret;
784 }
785 EXPORT_SYMBOL(simple_empty);
786 
787 void __simple_unlink(struct inode *dir, struct dentry *dentry)
788 {
789 	struct inode *inode = d_inode(dentry);
790 
791 	inode_set_mtime_to_ts(dir,
792 			      inode_set_ctime_to_ts(dir, inode_set_ctime_current(inode)));
793 	drop_nlink(inode);
794 }
795 EXPORT_SYMBOL(__simple_unlink);
796 
797 void __simple_rmdir(struct inode *dir, struct dentry *dentry)
798 {
799 	drop_nlink(d_inode(dentry));
800 	__simple_unlink(dir, dentry);
801 	drop_nlink(dir);
802 }
803 EXPORT_SYMBOL(__simple_rmdir);
804 
805 int simple_unlink(struct inode *dir, struct dentry *dentry)
806 {
807 	__simple_unlink(dir, dentry);
808 	d_make_discardable(dentry);
809 	return 0;
810 }
811 EXPORT_SYMBOL(simple_unlink);
812 
813 int simple_rmdir(struct inode *dir, struct dentry *dentry)
814 {
815 	if (!simple_empty(dentry))
816 		return -ENOTEMPTY;
817 
818 	__simple_rmdir(dir, dentry);
819 	d_make_discardable(dentry);
820 	return 0;
821 }
822 EXPORT_SYMBOL(simple_rmdir);
823 
824 /**
825  * simple_rename_timestamp - update the various inode timestamps for rename
826  * @old_dir: old parent directory
827  * @old_dentry: dentry that is being renamed
828  * @new_dir: new parent directory
829  * @new_dentry: target for rename
830  *
831  * POSIX mandates that the old and new parent directories have their ctime and
832  * mtime updated, and that inodes of @old_dentry and @new_dentry (if any), have
833  * their ctime updated.
834  */
835 void simple_rename_timestamp(struct inode *old_dir, struct dentry *old_dentry,
836 			     struct inode *new_dir, struct dentry *new_dentry)
837 {
838 	struct inode *newino = d_inode(new_dentry);
839 
840 	inode_set_mtime_to_ts(old_dir, inode_set_ctime_current(old_dir));
841 	if (new_dir != old_dir)
842 		inode_set_mtime_to_ts(new_dir,
843 				      inode_set_ctime_current(new_dir));
844 	inode_set_ctime_current(d_inode(old_dentry));
845 	if (newino)
846 		inode_set_ctime_current(newino);
847 }
848 EXPORT_SYMBOL_GPL(simple_rename_timestamp);
849 
850 int simple_rename_exchange(struct inode *old_dir, struct dentry *old_dentry,
851 			   struct inode *new_dir, struct dentry *new_dentry)
852 {
853 	bool old_is_dir = d_is_dir(old_dentry);
854 	bool new_is_dir = d_is_dir(new_dentry);
855 
856 	if (old_dir != new_dir && old_is_dir != new_is_dir) {
857 		if (old_is_dir) {
858 			drop_nlink(old_dir);
859 			inc_nlink(new_dir);
860 		} else {
861 			drop_nlink(new_dir);
862 			inc_nlink(old_dir);
863 		}
864 	}
865 	simple_rename_timestamp(old_dir, old_dentry, new_dir, new_dentry);
866 	return 0;
867 }
868 EXPORT_SYMBOL_GPL(simple_rename_exchange);
869 
870 int simple_rename(const struct mnt_idmap *idmap, struct inode *old_dir,
871 		  struct dentry *old_dentry, struct inode *new_dir,
872 		  struct dentry *new_dentry, unsigned int flags)
873 {
874 	int they_are_dirs = d_is_dir(old_dentry);
875 
876 	if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
877 		return -EINVAL;
878 
879 	if (flags & RENAME_EXCHANGE)
880 		return simple_rename_exchange(old_dir, old_dentry, new_dir, new_dentry);
881 
882 	if (!simple_empty(new_dentry))
883 		return -ENOTEMPTY;
884 
885 	if (d_really_is_positive(new_dentry)) {
886 		simple_unlink(new_dir, new_dentry);
887 		if (they_are_dirs) {
888 			drop_nlink(d_inode(new_dentry));
889 			drop_nlink(old_dir);
890 		}
891 	} else if (they_are_dirs) {
892 		drop_nlink(old_dir);
893 		inc_nlink(new_dir);
894 	}
895 
896 	simple_rename_timestamp(old_dir, old_dentry, new_dir, new_dentry);
897 	return 0;
898 }
899 EXPORT_SYMBOL(simple_rename);
900 
901 /**
902  * simple_setattr - setattr for simple filesystem
903  * @idmap: idmap of the target mount
904  * @dentry: dentry
905  * @iattr: iattr structure
906  *
907  * Returns 0 on success, -error on failure.
908  *
909  * simple_setattr is a simple ->setattr implementation without a proper
910  * implementation of size changes.
911  *
912  * It can either be used for in-memory filesystems or special files
913  * on simple regular filesystems.  Anything that needs to change on-disk
914  * or wire state on size changes needs its own setattr method.
915  */
916 int simple_setattr(const struct mnt_idmap *idmap, struct dentry *dentry,
917 		   struct iattr *iattr)
918 {
919 	struct inode *inode = d_inode(dentry);
920 	int error;
921 
922 	error = setattr_prepare(idmap, dentry, iattr);
923 	if (error)
924 		return error;
925 
926 	if (iattr->ia_valid & ATTR_SIZE)
927 		truncate_setsize(inode, iattr->ia_size);
928 	setattr_copy(idmap, inode, iattr);
929 	mark_inode_dirty(inode);
930 	return 0;
931 }
932 EXPORT_SYMBOL(simple_setattr);
933 
934 static int simple_read_folio(struct file *file, struct folio *folio)
935 {
936 	folio_zero_range(folio, 0, folio_size(folio));
937 	flush_dcache_folio(folio);
938 	folio_mark_uptodate(folio);
939 	folio_unlock(folio);
940 	return 0;
941 }
942 
943 int simple_write_begin(const struct kiocb *iocb, struct address_space *mapping,
944 			loff_t pos, unsigned len,
945 			struct folio **foliop, void **fsdata)
946 {
947 	struct folio *folio;
948 
949 	folio = __filemap_get_folio(mapping, pos / PAGE_SIZE, FGP_WRITEBEGIN,
950 			mapping_gfp_mask(mapping));
951 	if (IS_ERR(folio))
952 		return PTR_ERR(folio);
953 
954 	*foliop = folio;
955 
956 	if (!folio_test_uptodate(folio) && (len != folio_size(folio))) {
957 		size_t from = offset_in_folio(folio, pos);
958 
959 		folio_zero_segments(folio, 0, from,
960 				from + len, folio_size(folio));
961 	}
962 	return 0;
963 }
964 EXPORT_SYMBOL(simple_write_begin);
965 
966 /**
967  * simple_write_end - .write_end helper for non-block-device FSes
968  * @iocb: kernel I/O control block
969  * @mapping: 		"
970  * @pos: 		"
971  * @len: 		"
972  * @copied: 		"
973  * @folio: 		"
974  * @fsdata: 		"
975  *
976  * simple_write_end does the minimum needed for updating a folio after
977  * writing is done. It has the same API signature as the .write_end of
978  * address_space_operations vector. So it can just be set onto .write_end for
979  * FSes that don't need any other processing. i_rwsem is assumed to be held
980  * exclusively.
981  * Block based filesystems should use generic_write_end().
982  * NOTE: Even though i_size might get updated by this function, mark_inode_dirty
983  * is not called, so a filesystem that actually does store data in .write_inode
984  * should extend on what's done here with a call to mark_inode_dirty() in the
985  * case that i_size has changed.
986  *
987  * Use *ONLY* with simple_read_folio()
988  */
989 static int simple_write_end(const struct kiocb *iocb,
990 			    struct address_space *mapping,
991 			    loff_t pos, unsigned len, unsigned copied,
992 			    struct folio *folio, void *fsdata)
993 {
994 	struct inode *inode = folio->mapping->host;
995 	loff_t last_pos = pos + copied;
996 
997 	/* zero the stale part of the folio if we did a short copy */
998 	if (!folio_test_uptodate(folio)) {
999 		if (copied < len) {
1000 			size_t from = offset_in_folio(folio, pos);
1001 
1002 			folio_zero_range(folio, from + copied, len - copied);
1003 		}
1004 		folio_mark_uptodate(folio);
1005 	}
1006 	/*
1007 	 * No need to use i_size_read() here, the i_size
1008 	 * cannot change under us because we hold the i_rwsem.
1009 	 */
1010 	if (last_pos > inode->i_size)
1011 		i_size_write(inode, last_pos);
1012 
1013 	folio_mark_dirty(folio);
1014 	folio_unlock(folio);
1015 	folio_put(folio);
1016 
1017 	return copied;
1018 }
1019 
1020 /*
1021  * Provides ramfs-style behavior: data in the pagecache, but no writeback.
1022  */
1023 const struct address_space_operations ram_aops = {
1024 	.read_folio	= simple_read_folio,
1025 	.write_begin	= simple_write_begin,
1026 	.write_end	= simple_write_end,
1027 	.dirty_folio	= noop_dirty_folio,
1028 };
1029 EXPORT_SYMBOL(ram_aops);
1030 
1031 /*
1032  * the inodes created here are not hashed. If you use iunique to generate
1033  * unique inode values later for this filesystem, then you must take care
1034  * to pass it an appropriate max_reserved value to avoid collisions.
1035  */
1036 int simple_fill_super(struct super_block *s, unsigned long magic,
1037 		      const struct tree_descr *files)
1038 {
1039 	struct inode *inode;
1040 	struct dentry *dentry;
1041 	int i;
1042 
1043 	s->s_blocksize = PAGE_SIZE;
1044 	s->s_blocksize_bits = PAGE_SHIFT;
1045 	s->s_magic = magic;
1046 	s->s_op = &simple_super_operations;
1047 	s->s_time_gran = 1;
1048 
1049 	inode = new_inode(s);
1050 	if (!inode)
1051 		return -ENOMEM;
1052 	/*
1053 	 * because the root inode is 1, the files array must not contain an
1054 	 * entry at index 1
1055 	 */
1056 	inode->i_ino = 1;
1057 	inode->i_mode = S_IFDIR | 0755;
1058 	simple_inode_init_ts(inode);
1059 	inode->i_op = &simple_dir_inode_operations;
1060 	inode->i_fop = &simple_dir_operations;
1061 	set_nlink(inode, 2);
1062 	s->s_root = d_make_root(inode);
1063 	if (!s->s_root)
1064 		return -ENOMEM;
1065 	for (i = 0; !files->name || files->name[0]; i++, files++) {
1066 		if (!files->name)
1067 			continue;
1068 
1069 		/* warn if it tries to conflict with the root inode */
1070 		if (unlikely(i == 1))
1071 			printk(KERN_WARNING "%s: %s passed in a files array"
1072 				"with an index of 1!\n", __func__,
1073 				s->s_type->name);
1074 
1075 		dentry = d_alloc_name(s->s_root, files->name);
1076 		if (!dentry)
1077 			return -ENOMEM;
1078 		inode = new_inode(s);
1079 		if (!inode) {
1080 			dput(dentry);
1081 			return -ENOMEM;
1082 		}
1083 		inode->i_mode = S_IFREG | files->mode;
1084 		simple_inode_init_ts(inode);
1085 		inode->i_fop = files->ops;
1086 		inode->i_ino = i;
1087 		d_make_persistent(dentry, inode);
1088 		dput(dentry);
1089 	}
1090 	return 0;
1091 }
1092 EXPORT_SYMBOL(simple_fill_super);
1093 
1094 static DEFINE_SPINLOCK(pin_fs_lock);
1095 
1096 int simple_pin_fs(struct file_system_type *type, struct vfsmount **mount, int *count)
1097 {
1098 	struct vfsmount *mnt = NULL;
1099 	spin_lock(&pin_fs_lock);
1100 	if (unlikely(!*mount)) {
1101 		spin_unlock(&pin_fs_lock);
1102 		mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, NULL);
1103 		if (IS_ERR(mnt))
1104 			return PTR_ERR(mnt);
1105 		spin_lock(&pin_fs_lock);
1106 		if (!*mount) {
1107 			*mount = mnt;
1108 			mnt = NULL;
1109 		}
1110 	}
1111 	++*count;
1112 	spin_unlock(&pin_fs_lock);
1113 	mntput(mnt);
1114 	return 0;
1115 }
1116 EXPORT_SYMBOL(simple_pin_fs);
1117 
1118 void simple_release_fs(struct vfsmount **mount, int *count)
1119 {
1120 	struct vfsmount *mnt = NULL;
1121 	spin_lock(&pin_fs_lock);
1122 	if (!--*count) {
1123 		mnt = *mount;
1124 		*mount = NULL;
1125 	}
1126 	spin_unlock(&pin_fs_lock);
1127 	mntput(mnt);
1128 }
1129 EXPORT_SYMBOL(simple_release_fs);
1130 
1131 /**
1132  * simple_read_from_buffer - copy data from the buffer to user space
1133  * @to: the user space buffer to read to
1134  * @count: the maximum number of bytes to read
1135  * @ppos: the current position in the buffer
1136  * @from: the buffer to read from
1137  * @available: the size of the buffer
1138  *
1139  * The simple_read_from_buffer() function reads up to @count bytes from the
1140  * buffer @from at offset @ppos into the user space address starting at @to.
1141  *
1142  * On success, the number of bytes read is returned and the offset @ppos is
1143  * advanced by this number, or negative value is returned on error.
1144  **/
1145 ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos,
1146 				const void *from, size_t available)
1147 {
1148 	loff_t pos = *ppos;
1149 	size_t ret;
1150 
1151 	if (pos < 0)
1152 		return -EINVAL;
1153 	if (pos >= available || !count)
1154 		return 0;
1155 	if (count > available - pos)
1156 		count = available - pos;
1157 	ret = copy_to_user(to, from + pos, count);
1158 	if (ret == count)
1159 		return -EFAULT;
1160 	count -= ret;
1161 	*ppos = pos + count;
1162 	return count;
1163 }
1164 EXPORT_SYMBOL(simple_read_from_buffer);
1165 
1166 /**
1167  * simple_write_to_buffer - copy data from user space to the buffer
1168  * @to: the buffer to write to
1169  * @available: the size of the buffer
1170  * @ppos: the current position in the buffer
1171  * @from: the user space buffer to read from
1172  * @count: the maximum number of bytes to read
1173  *
1174  * The simple_write_to_buffer() function reads up to @count bytes from the user
1175  * space address starting at @from into the buffer @to at offset @ppos.
1176  *
1177  * On success, the number of bytes written is returned and the offset @ppos is
1178  * advanced by this number, or negative value is returned on error.
1179  **/
1180 ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos,
1181 		const void __user *from, size_t count)
1182 {
1183 	loff_t pos = *ppos;
1184 	size_t res;
1185 
1186 	if (pos < 0)
1187 		return -EINVAL;
1188 	if (pos >= available || !count)
1189 		return 0;
1190 	if (count > available - pos)
1191 		count = available - pos;
1192 	res = copy_from_user(to + pos, from, count);
1193 	if (res == count)
1194 		return -EFAULT;
1195 	count -= res;
1196 	*ppos = pos + count;
1197 	return count;
1198 }
1199 EXPORT_SYMBOL(simple_write_to_buffer);
1200 
1201 /**
1202  * memory_read_from_buffer - copy data from the buffer
1203  * @to: the kernel space buffer to read to
1204  * @count: the maximum number of bytes to read
1205  * @ppos: the current position in the buffer
1206  * @from: the buffer to read from
1207  * @available: the size of the buffer
1208  *
1209  * The memory_read_from_buffer() function reads up to @count bytes from the
1210  * buffer @from at offset @ppos into the kernel space address starting at @to.
1211  *
1212  * On success, the number of bytes read is returned and the offset @ppos is
1213  * advanced by this number, or negative value is returned on error.
1214  **/
1215 ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
1216 				const void *from, size_t available)
1217 {
1218 	loff_t pos = *ppos;
1219 
1220 	if (pos < 0)
1221 		return -EINVAL;
1222 	if (pos >= available)
1223 		return 0;
1224 	if (count > available - pos)
1225 		count = available - pos;
1226 	memcpy(to, from + pos, count);
1227 	*ppos = pos + count;
1228 
1229 	return count;
1230 }
1231 EXPORT_SYMBOL(memory_read_from_buffer);
1232 
1233 /*
1234  * Transaction based IO.
1235  * The file expects a single write which triggers the transaction, and then
1236  * possibly a read which collects the result - which is stored in a
1237  * file-local buffer.
1238  */
1239 
1240 void simple_transaction_set(struct file *file, size_t n)
1241 {
1242 	struct simple_transaction_argresp *ar = file->private_data;
1243 
1244 	BUG_ON(n > SIMPLE_TRANSACTION_LIMIT);
1245 
1246 	/*
1247 	 * The barrier ensures that ar->size will really remain zero until
1248 	 * ar->data is ready for reading.
1249 	 */
1250 	smp_mb();
1251 	ar->size = n;
1252 }
1253 EXPORT_SYMBOL(simple_transaction_set);
1254 
1255 char *simple_transaction_get(struct file *file, const char __user *buf, size_t size)
1256 {
1257 	struct simple_transaction_argresp *ar;
1258 	static DEFINE_SPINLOCK(simple_transaction_lock);
1259 
1260 	if (size > SIMPLE_TRANSACTION_LIMIT - 1)
1261 		return ERR_PTR(-EFBIG);
1262 
1263 	ar = kzalloc(PAGE_SIZE, GFP_KERNEL);
1264 	if (!ar)
1265 		return ERR_PTR(-ENOMEM);
1266 
1267 	spin_lock(&simple_transaction_lock);
1268 
1269 	/* only one write allowed per open */
1270 	if (file->private_data) {
1271 		spin_unlock(&simple_transaction_lock);
1272 		kfree(ar);
1273 		return ERR_PTR(-EBUSY);
1274 	}
1275 
1276 	file->private_data = ar;
1277 
1278 	spin_unlock(&simple_transaction_lock);
1279 
1280 	if (copy_from_user(ar->data, buf, size))
1281 		return ERR_PTR(-EFAULT);
1282 
1283 	return ar->data;
1284 }
1285 EXPORT_SYMBOL(simple_transaction_get);
1286 
1287 ssize_t simple_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
1288 {
1289 	struct simple_transaction_argresp *ar = file->private_data;
1290 
1291 	if (!ar)
1292 		return 0;
1293 	return simple_read_from_buffer(buf, size, pos, ar->data, ar->size);
1294 }
1295 EXPORT_SYMBOL(simple_transaction_read);
1296 
1297 int simple_transaction_release(struct inode *inode, struct file *file)
1298 {
1299 	kfree(file->private_data);
1300 	return 0;
1301 }
1302 EXPORT_SYMBOL(simple_transaction_release);
1303 
1304 /* Simple attribute files */
1305 
1306 struct simple_attr {
1307 	int (*get)(void *, u64 *);
1308 	int (*set)(void *, u64);
1309 	char get_buf[24];	/* enough to store a u64 and "\n\0" */
1310 	char set_buf[24];
1311 	void *data;
1312 	const char *fmt;	/* format for read operation */
1313 	struct mutex mutex;	/* protects access to these buffers */
1314 };
1315 
1316 /* simple_attr_open is called by an actual attribute open file operation
1317  * to set the attribute specific access operations. */
1318 int simple_attr_open(struct inode *inode, struct file *file,
1319 		     int (*get)(void *, u64 *), int (*set)(void *, u64),
1320 		     const char *fmt)
1321 {
1322 	struct simple_attr *attr;
1323 
1324 	attr = kzalloc_obj(*attr);
1325 	if (!attr)
1326 		return -ENOMEM;
1327 
1328 	attr->get = get;
1329 	attr->set = set;
1330 	attr->data = inode->i_private;
1331 	attr->fmt = fmt;
1332 	mutex_init(&attr->mutex);
1333 
1334 	file->private_data = attr;
1335 
1336 	return nonseekable_open(inode, file);
1337 }
1338 EXPORT_SYMBOL_GPL(simple_attr_open);
1339 
1340 int simple_attr_release(struct inode *inode, struct file *file)
1341 {
1342 	kfree(file->private_data);
1343 	return 0;
1344 }
1345 EXPORT_SYMBOL_GPL(simple_attr_release);	/* GPL-only?  This?  Really? */
1346 
1347 /* read from the buffer that is filled with the get function */
1348 ssize_t simple_attr_read(struct file *file, char __user *buf,
1349 			 size_t len, loff_t *ppos)
1350 {
1351 	struct simple_attr *attr;
1352 	size_t size;
1353 	ssize_t ret;
1354 
1355 	attr = file->private_data;
1356 
1357 	if (!attr->get)
1358 		return -EACCES;
1359 
1360 	ret = mutex_lock_interruptible(&attr->mutex);
1361 	if (ret)
1362 		return ret;
1363 
1364 	if (*ppos && attr->get_buf[0]) {
1365 		/* continued read */
1366 		size = strlen(attr->get_buf);
1367 	} else {
1368 		/* first read */
1369 		u64 val;
1370 		ret = attr->get(attr->data, &val);
1371 		if (ret)
1372 			goto out;
1373 
1374 		size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
1375 				 attr->fmt, (unsigned long long)val);
1376 	}
1377 
1378 	ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
1379 out:
1380 	mutex_unlock(&attr->mutex);
1381 	return ret;
1382 }
1383 EXPORT_SYMBOL_GPL(simple_attr_read);
1384 
1385 /* interpret the buffer as a number to call the set function with */
1386 static ssize_t simple_attr_write_xsigned(struct file *file, const char __user *buf,
1387 			  size_t len, loff_t *ppos, bool is_signed)
1388 {
1389 	struct simple_attr *attr;
1390 	unsigned long long val;
1391 	size_t size;
1392 	ssize_t ret;
1393 
1394 	attr = file->private_data;
1395 	if (!attr->set)
1396 		return -EACCES;
1397 
1398 	ret = mutex_lock_interruptible(&attr->mutex);
1399 	if (ret)
1400 		return ret;
1401 
1402 	ret = -EFAULT;
1403 	size = min(sizeof(attr->set_buf) - 1, len);
1404 	if (copy_from_user(attr->set_buf, buf, size))
1405 		goto out;
1406 
1407 	attr->set_buf[size] = '\0';
1408 	if (is_signed)
1409 		ret = kstrtoll(attr->set_buf, 0, &val);
1410 	else
1411 		ret = kstrtoull(attr->set_buf, 0, &val);
1412 	if (ret)
1413 		goto out;
1414 	ret = attr->set(attr->data, val);
1415 	if (ret == 0)
1416 		ret = len; /* on success, claim we got the whole input */
1417 out:
1418 	mutex_unlock(&attr->mutex);
1419 	return ret;
1420 }
1421 
1422 ssize_t simple_attr_write(struct file *file, const char __user *buf,
1423 			  size_t len, loff_t *ppos)
1424 {
1425 	return simple_attr_write_xsigned(file, buf, len, ppos, false);
1426 }
1427 EXPORT_SYMBOL_GPL(simple_attr_write);
1428 
1429 ssize_t simple_attr_write_signed(struct file *file, const char __user *buf,
1430 			  size_t len, loff_t *ppos)
1431 {
1432 	return simple_attr_write_xsigned(file, buf, len, ppos, true);
1433 }
1434 EXPORT_SYMBOL_GPL(simple_attr_write_signed);
1435 
1436 /**
1437  * generic_encode_ino32_fh - generic export_operations->encode_fh function
1438  * @inode:   the object to encode
1439  * @fh:      where to store the file handle fragment
1440  * @max_len: maximum length to store there (in 4 byte units)
1441  * @parent:  parent directory inode, if wanted
1442  *
1443  * This generic encode_fh function assumes that the 32 inode number
1444  * is suitable for locating an inode, and that the generation number
1445  * can be used to check that it is still valid.  It places them in the
1446  * filehandle fragment where export_decode_fh expects to find them.
1447  */
1448 int generic_encode_ino32_fh(struct inode *inode, __u32 *fh, int *max_len,
1449 			    struct inode *parent)
1450 {
1451 	struct fid *fid = (void *)fh;
1452 	int len = *max_len;
1453 	int type = FILEID_INO32_GEN;
1454 
1455 	if (parent && (len < 4)) {
1456 		*max_len = 4;
1457 		return FILEID_INVALID;
1458 	} else if (len < 2) {
1459 		*max_len = 2;
1460 		return FILEID_INVALID;
1461 	}
1462 
1463 	len = 2;
1464 	fid->i32.ino = inode->i_ino;
1465 	fid->i32.gen = inode->i_generation;
1466 	if (parent) {
1467 		fid->i32.parent_ino = parent->i_ino;
1468 		fid->i32.parent_gen = parent->i_generation;
1469 		len = 4;
1470 		type = FILEID_INO32_GEN_PARENT;
1471 	}
1472 	*max_len = len;
1473 	return type;
1474 }
1475 EXPORT_SYMBOL_GPL(generic_encode_ino32_fh);
1476 
1477 /**
1478  * generic_fh_to_dentry - generic helper for the fh_to_dentry export operation
1479  * @sb:		filesystem to do the file handle conversion on
1480  * @fid:	file handle to convert
1481  * @fh_len:	length of the file handle in bytes
1482  * @fh_type:	type of file handle
1483  * @get_inode:	filesystem callback to retrieve inode
1484  *
1485  * This function decodes @fid as long as it has one of the well-known
1486  * Linux filehandle types and calls @get_inode on it to retrieve the
1487  * inode for the object specified in the file handle.
1488  */
1489 struct dentry *generic_fh_to_dentry(struct super_block *sb, struct fid *fid,
1490 		int fh_len, int fh_type, struct inode *(*get_inode)
1491 			(struct super_block *sb, u64 ino, u32 gen))
1492 {
1493 	struct inode *inode = NULL;
1494 
1495 	if (fh_len < 2)
1496 		return NULL;
1497 
1498 	switch (fh_type) {
1499 	case FILEID_INO32_GEN:
1500 	case FILEID_INO32_GEN_PARENT:
1501 		inode = get_inode(sb, fid->i32.ino, fid->i32.gen);
1502 		break;
1503 	}
1504 
1505 	return d_obtain_alias(inode);
1506 }
1507 EXPORT_SYMBOL_GPL(generic_fh_to_dentry);
1508 
1509 /**
1510  * generic_fh_to_parent - generic helper for the fh_to_parent export operation
1511  * @sb:		filesystem to do the file handle conversion on
1512  * @fid:	file handle to convert
1513  * @fh_len:	length of the file handle in bytes
1514  * @fh_type:	type of file handle
1515  * @get_inode:	filesystem callback to retrieve inode
1516  *
1517  * This function decodes @fid as long as it has one of the well-known
1518  * Linux filehandle types and calls @get_inode on it to retrieve the
1519  * inode for the _parent_ object specified in the file handle if it
1520  * is specified in the file handle, or NULL otherwise.
1521  */
1522 struct dentry *generic_fh_to_parent(struct super_block *sb, struct fid *fid,
1523 		int fh_len, int fh_type, struct inode *(*get_inode)
1524 			(struct super_block *sb, u64 ino, u32 gen))
1525 {
1526 	struct inode *inode = NULL;
1527 
1528 	if (fh_len <= 2)
1529 		return NULL;
1530 
1531 	switch (fh_type) {
1532 	case FILEID_INO32_GEN_PARENT:
1533 		inode = get_inode(sb, fid->i32.parent_ino,
1534 				  (fh_len > 3 ? fid->i32.parent_gen : 0));
1535 		break;
1536 	}
1537 
1538 	return d_obtain_alias(inode);
1539 }
1540 EXPORT_SYMBOL_GPL(generic_fh_to_parent);
1541 
1542 /**
1543  * simple_fsync_noflush - generic fsync implementation for simple filesystems
1544  *
1545  * @file:	file to synchronize
1546  * @start:	start offset in bytes
1547  * @end:	end offset in bytes (inclusive)
1548  * @datasync:	only synchronize essential metadata if true
1549  *
1550  * This function is an fsync handler for simple filesystems. It writes out
1551  * dirty data, inode (if dirty), but does not issue a cache flush.
1552  */
1553 int simple_fsync_noflush(struct file *file, loff_t start, loff_t end,
1554 			 int datasync)
1555 {
1556 	struct inode *inode = file->f_mapping->host;
1557 	int err;
1558 	int ret = 0;
1559 
1560 	err = file_write_and_wait_range(file, start, end);
1561 	if (err)
1562 		return err;
1563 
1564 	if (!(inode_state_read_once(inode) &
1565 			(I_DIRTY_ALL | I_SYNC | I_METADATA_WRITEBACK)))
1566 		goto out;
1567 	if (datasync &&
1568 	    !(inode_state_read_once(inode) &
1569 			(I_DIRTY_DATASYNC | I_SYNC | I_METADATA_WRITEBACK)))
1570 		goto out;
1571 
1572 	ret = sync_inode_metadata(inode, 1);
1573 out:
1574 	/* check and advance again to catch errors after syncing out buffers */
1575 	err = file_check_and_advance_wb_err(file);
1576 	if (ret == 0)
1577 		ret = err;
1578 	return ret;
1579 }
1580 EXPORT_SYMBOL(simple_fsync_noflush);
1581 
1582 /**
1583  * simple_fsync - fsync implementation for simple filesystems with flush
1584  * @file:	file to synchronize
1585  * @start:	start offset in bytes
1586  * @end:	end offset in bytes (inclusive)
1587  * @datasync:	only synchronize essential metadata if true
1588  *
1589  * This function is an fsync handler for simple filesystems. It writes out
1590  * dirty data, inode (if dirty), and issues a cache flush.
1591  */
1592 int simple_fsync(struct file *file, loff_t start, loff_t end, int datasync)
1593 {
1594 	struct inode *inode = file->f_mapping->host;
1595 	int err;
1596 
1597 	err = simple_fsync_noflush(file, start, end, datasync);
1598 	if (err)
1599 		return err;
1600 	return blkdev_issue_flush(inode->i_sb->s_bdev);
1601 }
1602 EXPORT_SYMBOL(simple_fsync);
1603 
1604 /**
1605  * generic_check_addressable - Check addressability of file system
1606  * @blocksize_bits:	log of file system block size
1607  * @num_blocks:		number of blocks in file system
1608  *
1609  * Determine whether a file system with @num_blocks blocks (and a
1610  * block size of 2**@blocksize_bits) is addressable by the sector_t
1611  * and page cache of the system.  Return 0 if so and -EFBIG otherwise.
1612  */
1613 int generic_check_addressable(unsigned blocksize_bits, u64 num_blocks)
1614 {
1615 	u64 last_fs_block = num_blocks - 1;
1616 	u64 last_fs_page, max_bytes;
1617 
1618 	if (check_shl_overflow(num_blocks, blocksize_bits, &max_bytes))
1619 		return -EFBIG;
1620 
1621 	last_fs_page = (max_bytes >> PAGE_SHIFT) - 1;
1622 
1623 	if (unlikely(num_blocks == 0))
1624 		return 0;
1625 
1626 	if (blocksize_bits < 9)
1627 		return -EINVAL;
1628 
1629 	if ((last_fs_block > (sector_t)(~0ULL) >> (blocksize_bits - 9)) ||
1630 	    (last_fs_page > (pgoff_t)(~0ULL))) {
1631 		return -EFBIG;
1632 	}
1633 	return 0;
1634 }
1635 EXPORT_SYMBOL(generic_check_addressable);
1636 
1637 /*
1638  * No-op implementation of ->fsync for in-memory filesystems.
1639  */
1640 int noop_fsync(struct file *file, loff_t start, loff_t end, int datasync)
1641 {
1642 	return 0;
1643 }
1644 EXPORT_SYMBOL(noop_fsync);
1645 
1646 ssize_t noop_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
1647 {
1648 	/*
1649 	 * iomap based filesystems support direct I/O without need for
1650 	 * this callback. However, it still needs to be set in
1651 	 * inode->a_ops so that open/fcntl know that direct I/O is
1652 	 * generally supported.
1653 	 */
1654 	return -EINVAL;
1655 }
1656 EXPORT_SYMBOL_GPL(noop_direct_IO);
1657 
1658 /* Because kfree isn't assignment-compatible with void(void*) ;-/ */
1659 void kfree_link(void *p)
1660 {
1661 	kfree(p);
1662 }
1663 EXPORT_SYMBOL(kfree_link);
1664 
1665 struct inode *alloc_anon_inode(struct super_block *s)
1666 {
1667 	static const struct address_space_operations anon_aops = {
1668 		.dirty_folio	= noop_dirty_folio,
1669 	};
1670 	struct inode *inode = new_inode_pseudo(s);
1671 
1672 	if (!inode)
1673 		return ERR_PTR(-ENOMEM);
1674 
1675 	inode->i_ino = get_next_ino();
1676 	inode->i_mapping->a_ops = &anon_aops;
1677 
1678 	/*
1679 	 * Mark the inode dirty from the very beginning,
1680 	 * that way it will never be moved to the dirty
1681 	 * list because mark_inode_dirty() will think
1682 	 * that it already _is_ on the dirty list.
1683 	 */
1684 	inode_state_assign_raw(inode, I_DIRTY);
1685 	/*
1686 	 * Historically anonymous inodes don't have a type at all and
1687 	 * userspace has come to rely on this.
1688 	 */
1689 	inode->i_mode = S_IRUSR | S_IWUSR;
1690 	inode->i_uid = current_fsuid();
1691 	inode->i_gid = current_fsgid();
1692 	inode->i_flags |= S_PRIVATE | S_ANON_INODE;
1693 	simple_inode_init_ts(inode);
1694 	return inode;
1695 }
1696 EXPORT_SYMBOL(alloc_anon_inode);
1697 
1698 /**
1699  * simple_get_link - generic helper to get the target of "fast" symlinks
1700  * @dentry: not used here
1701  * @inode: the symlink inode
1702  * @done: not used here
1703  *
1704  * Generic helper for filesystems to use for symlink inodes where a pointer to
1705  * the symlink target is stored in ->i_link.  NOTE: this isn't normally called,
1706  * since as an optimization the path lookup code uses any non-NULL ->i_link
1707  * directly, without calling ->get_link().  But ->get_link() still must be set,
1708  * to mark the inode_operations as being for a symlink.
1709  *
1710  * Return: the symlink target
1711  */
1712 const char *simple_get_link(struct dentry *dentry, struct inode *inode,
1713 			    struct delayed_call *done)
1714 {
1715 	return inode->i_link;
1716 }
1717 EXPORT_SYMBOL(simple_get_link);
1718 
1719 const struct inode_operations simple_symlink_inode_operations = {
1720 	.get_link = simple_get_link,
1721 };
1722 EXPORT_SYMBOL(simple_symlink_inode_operations);
1723 
1724 /*
1725  * Operations for a permanently empty directory.
1726  */
1727 static struct dentry *empty_dir_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
1728 {
1729 	return ERR_PTR(-ENOENT);
1730 }
1731 
1732 static int empty_dir_setattr(const struct mnt_idmap *idmap,
1733 			     struct dentry *dentry, struct iattr *attr)
1734 {
1735 	return -EPERM;
1736 }
1737 
1738 static ssize_t empty_dir_listxattr(struct dentry *dentry, char *list, size_t size)
1739 {
1740 	return -EOPNOTSUPP;
1741 }
1742 
1743 static const struct inode_operations empty_dir_inode_operations = {
1744 	.lookup		= empty_dir_lookup,
1745 	.setattr	= empty_dir_setattr,
1746 	.listxattr	= empty_dir_listxattr,
1747 };
1748 
1749 static loff_t empty_dir_llseek(struct file *file, loff_t offset, int whence)
1750 {
1751 	/* An empty directory has two entries . and .. at offsets 0 and 1 */
1752 	return generic_file_llseek_size(file, offset, whence, 2, 2);
1753 }
1754 
1755 static int empty_dir_readdir(struct file *file, struct dir_context *ctx)
1756 {
1757 	dir_emit_dots(file, ctx);
1758 	return 0;
1759 }
1760 
1761 static const struct file_operations empty_dir_operations = {
1762 	.llseek		= empty_dir_llseek,
1763 	.read		= generic_read_dir,
1764 	.iterate_shared	= empty_dir_readdir,
1765 	.fsync		= noop_fsync,
1766 };
1767 
1768 
1769 void make_empty_dir_inode(struct inode *inode)
1770 {
1771 	set_nlink(inode, 2);
1772 	inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
1773 	inode->i_uid = GLOBAL_ROOT_UID;
1774 	inode->i_gid = GLOBAL_ROOT_GID;
1775 	inode->i_rdev = 0;
1776 	inode->i_size = 0;
1777 	inode->i_blkbits = PAGE_SHIFT;
1778 	inode->i_blocks = 0;
1779 
1780 	inode->i_op = &empty_dir_inode_operations;
1781 	inode->i_opflags &= ~IOP_XATTR;
1782 	inode->i_fop = &empty_dir_operations;
1783 }
1784 
1785 bool is_empty_dir_inode(struct inode *inode)
1786 {
1787 	return (inode->i_fop == &empty_dir_operations) &&
1788 		(inode->i_op == &empty_dir_inode_operations);
1789 }
1790 
1791 #if IS_ENABLED(CONFIG_UNICODE)
1792 /**
1793  * generic_ci_d_compare - generic d_compare implementation for casefolding filesystems
1794  * @dentry:	dentry whose name we are checking against
1795  * @len:	len of name of dentry
1796  * @str:	str pointer to name of dentry
1797  * @name:	Name to compare against
1798  *
1799  * Return: 0 if names match, 1 if mismatch, or -ERRNO
1800  */
1801 int generic_ci_d_compare(const struct dentry *dentry, unsigned int len,
1802 			 const char *str, const struct qstr *name)
1803 {
1804 	const struct dentry *parent;
1805 	const struct inode *dir;
1806 	union shortname_store strbuf;
1807 	struct qstr qstr;
1808 
1809 	/*
1810 	 * Attempt a case-sensitive match first. It is cheaper and
1811 	 * should cover most lookups, including all the sane
1812 	 * applications that expect a case-sensitive filesystem.
1813 	 *
1814 	 * This comparison is safe under RCU because the caller
1815 	 * guarantees the consistency between str and len. See
1816 	 * __d_lookup_rcu_op_compare() for details.
1817 	 */
1818 	if (len == name->len && !memcmp(str, name->name, len))
1819 		return 0;
1820 
1821 	parent = READ_ONCE(dentry->d_parent);
1822 	dir = READ_ONCE(parent->d_inode);
1823 	if (!dir || !IS_CASEFOLDED(dir))
1824 		return 1;
1825 
1826 	qstr.len = len;
1827 	qstr.name = str;
1828 	/*
1829 	 * If the dentry name is stored in-line, then it may be concurrently
1830 	 * modified by a rename.  If this happens, the VFS will eventually retry
1831 	 * the lookup, so it doesn't matter what ->d_compare() returns.
1832 	 * However, it's unsafe to call utf8_strncasecmp() with an unstable
1833 	 * string.  Therefore, we have to copy the name into a temporary buffer.
1834 	 * As above, len is guaranteed to match str, so the shortname case
1835 	 * is exactly when str points to ->d_shortname.
1836 	 */
1837 	if (qstr.name == dentry->d_shortname.string) {
1838 		strbuf = dentry->d_shortname; // NUL is guaranteed to be in there
1839 		qstr.name = strbuf.string;
1840 		/* prevent compiler from optimizing out the temporary buffer */
1841 		barrier();
1842 	}
1843 
1844 	return utf8_strncasecmp(dentry->d_sb->s_encoding, name, &qstr);
1845 }
1846 EXPORT_SYMBOL(generic_ci_d_compare);
1847 
1848 /**
1849  * generic_ci_d_hash - generic d_hash implementation for casefolding filesystems
1850  * @dentry:	dentry of the parent directory
1851  * @str:	qstr of name whose hash we should fill in
1852  *
1853  * Return: 0 if hash was successful or unchanged, and -EINVAL on error
1854  */
1855 int generic_ci_d_hash(const struct dentry *dentry, struct qstr *str)
1856 {
1857 	const struct inode *dir = READ_ONCE(dentry->d_inode);
1858 	struct super_block *sb = dentry->d_sb;
1859 	const struct unicode_map *um = sb->s_encoding;
1860 	int ret;
1861 
1862 	if (!dir || !IS_CASEFOLDED(dir))
1863 		return 0;
1864 
1865 	ret = utf8_casefold_hash(um, dentry, str);
1866 	if (ret < 0 && sb_has_strict_encoding(sb))
1867 		return -EINVAL;
1868 	return 0;
1869 }
1870 EXPORT_SYMBOL(generic_ci_d_hash);
1871 
1872 static const struct dentry_operations generic_ci_dentry_ops = {
1873 	.d_hash = generic_ci_d_hash,
1874 	.d_compare = generic_ci_d_compare,
1875 #ifdef CONFIG_FS_ENCRYPTION
1876 	.d_revalidate = fscrypt_d_revalidate,
1877 #endif
1878 };
1879 
1880 /**
1881  * generic_ci_match() - Match a name (case-insensitively) with a dirent.
1882  * This is a filesystem helper for comparison with directory entries.
1883  * generic_ci_d_compare should be used in VFS' ->d_compare instead.
1884  *
1885  * @parent: Inode of the parent of the dirent under comparison
1886  * @name: name under lookup.
1887  * @folded_name: Optional pre-folded name under lookup
1888  * @de_name: Dirent name.
1889  * @de_name_len: dirent name length.
1890  *
1891  * Test whether a case-insensitive directory entry matches the filename
1892  * being searched.  If @folded_name is provided, it is used instead of
1893  * recalculating the casefold of @name.
1894  *
1895  * Return: > 0 if the directory entry matches, 0 if it doesn't match, or
1896  * < 0 on error.
1897  */
1898 int generic_ci_match(const struct inode *parent,
1899 		     const struct qstr *name,
1900 		     const struct qstr *folded_name,
1901 		     const u8 *de_name, u32 de_name_len)
1902 {
1903 	const struct super_block *sb = parent->i_sb;
1904 	const struct unicode_map *um = sb->s_encoding;
1905 	struct fscrypt_str decrypted_name = FSTR_INIT(NULL, de_name_len);
1906 	struct qstr dirent = QSTR_INIT(de_name, de_name_len);
1907 	int res = 0;
1908 
1909 	if (IS_ENCRYPTED(parent)) {
1910 		const struct fscrypt_str encrypted_name =
1911 			FSTR_INIT((u8 *) de_name, de_name_len);
1912 
1913 		if (WARN_ON_ONCE(!fscrypt_has_encryption_key(parent)))
1914 			return -EINVAL;
1915 
1916 		decrypted_name.name = kmalloc(de_name_len, GFP_KERNEL);
1917 		if (!decrypted_name.name)
1918 			return -ENOMEM;
1919 		res = fscrypt_fname_disk_to_usr(parent, 0, 0, &encrypted_name,
1920 						&decrypted_name);
1921 		if (res < 0) {
1922 			kfree(decrypted_name.name);
1923 			return res;
1924 		}
1925 		dirent.name = decrypted_name.name;
1926 		dirent.len = decrypted_name.len;
1927 	}
1928 
1929 	/*
1930 	 * Attempt a case-sensitive match first. It is cheaper and
1931 	 * should cover most lookups, including all the sane
1932 	 * applications that expect a case-sensitive filesystem.
1933 	 */
1934 
1935 	if (dirent.len == name->len &&
1936 	    !memcmp(name->name, dirent.name, dirent.len))
1937 		goto out;
1938 
1939 	if (folded_name->name)
1940 		res = utf8_strncasecmp_folded(um, folded_name, &dirent);
1941 	else
1942 		res = utf8_strncasecmp(um, name, &dirent);
1943 
1944 out:
1945 	kfree(decrypted_name.name);
1946 	if (res < 0 && sb_has_strict_encoding(sb)) {
1947 		pr_err_ratelimited("Directory contains filename that is invalid UTF-8");
1948 		return 0;
1949 	}
1950 	return !res;
1951 }
1952 EXPORT_SYMBOL(generic_ci_match);
1953 #endif
1954 
1955 #ifdef CONFIG_FS_ENCRYPTION
1956 static const struct dentry_operations generic_encrypted_dentry_ops = {
1957 	.d_revalidate = fscrypt_d_revalidate,
1958 };
1959 #endif
1960 
1961 /**
1962  * generic_set_sb_d_ops - helper for choosing the set of
1963  * filesystem-wide dentry operations for the enabled features
1964  * @sb: superblock to be configured
1965  *
1966  * Filesystems supporting casefolding and/or fscrypt can call this
1967  * helper at mount-time to configure default dentry_operations to the
1968  * best set of dentry operations required for the enabled features.
1969  * The helper must be called after these have been configured, but
1970  * before the root dentry is created.
1971  */
1972 void generic_set_sb_d_ops(struct super_block *sb)
1973 {
1974 #if IS_ENABLED(CONFIG_UNICODE)
1975 	if (sb->s_encoding) {
1976 		set_default_d_op(sb, &generic_ci_dentry_ops);
1977 		return;
1978 	}
1979 #endif
1980 #ifdef CONFIG_FS_ENCRYPTION
1981 	if (sb->s_cop) {
1982 		set_default_d_op(sb, &generic_encrypted_dentry_ops);
1983 		return;
1984 	}
1985 #endif
1986 }
1987 EXPORT_SYMBOL(generic_set_sb_d_ops);
1988 
1989 /**
1990  * inode_maybe_inc_iversion - increments i_version
1991  * @inode: inode with the i_version that should be updated
1992  * @force: increment the counter even if it's not necessary?
1993  *
1994  * Every time the inode is modified, the i_version field must be seen to have
1995  * changed by any observer.
1996  *
1997  * If "force" is set or the QUERIED flag is set, then ensure that we increment
1998  * the value, and clear the queried flag.
1999  *
2000  * In the common case where neither is set, then we can return "false" without
2001  * updating i_version.
2002  *
2003  * If this function returns false, and no other metadata has changed, then we
2004  * can avoid logging the metadata.
2005  */
2006 bool inode_maybe_inc_iversion(struct inode *inode, bool force)
2007 {
2008 	u64 cur, new;
2009 
2010 	/*
2011 	 * The i_version field is not strictly ordered with any other inode
2012 	 * information, but the legacy inode_inc_iversion code used a spinlock
2013 	 * to serialize increments.
2014 	 *
2015 	 * We add a full memory barrier to ensure that any de facto ordering
2016 	 * with other state is preserved (either implicitly coming from cmpxchg
2017 	 * or explicitly from smp_mb if we don't know upfront if we will execute
2018 	 * the former).
2019 	 *
2020 	 * These barriers pair with inode_query_iversion().
2021 	 */
2022 	cur = inode_peek_iversion_raw(inode);
2023 	if (!force && !(cur & I_VERSION_QUERIED)) {
2024 		smp_mb();
2025 		cur = inode_peek_iversion_raw(inode);
2026 	}
2027 
2028 	do {
2029 		/* If flag is clear then we needn't do anything */
2030 		if (!force && !(cur & I_VERSION_QUERIED))
2031 			return false;
2032 
2033 		/* Since lowest bit is flag, add 2 to avoid it */
2034 		new = (cur & ~I_VERSION_QUERIED) + I_VERSION_INCREMENT;
2035 	} while (!atomic64_try_cmpxchg(&inode->i_version, &cur, new));
2036 	return true;
2037 }
2038 EXPORT_SYMBOL(inode_maybe_inc_iversion);
2039 
2040 /**
2041  * inode_query_iversion - read i_version for later use
2042  * @inode: inode from which i_version should be read
2043  *
2044  * Read the inode i_version counter. This should be used by callers that wish
2045  * to store the returned i_version for later comparison. This will guarantee
2046  * that a later query of the i_version will result in a different value if
2047  * anything has changed.
2048  *
2049  * In this implementation, we fetch the current value, set the QUERIED flag and
2050  * then try to swap it into place with a cmpxchg, if it wasn't already set. If
2051  * that fails, we try again with the newly fetched value from the cmpxchg.
2052  */
2053 u64 inode_query_iversion(struct inode *inode)
2054 {
2055 	u64 cur, new;
2056 	bool fenced = false;
2057 
2058 	/*
2059 	 * Memory barriers (implicit in cmpxchg, explicit in smp_mb) pair with
2060 	 * inode_maybe_inc_iversion(), see that routine for more details.
2061 	 */
2062 	cur = inode_peek_iversion_raw(inode);
2063 	do {
2064 		/* If flag is already set, then no need to swap */
2065 		if (cur & I_VERSION_QUERIED) {
2066 			if (!fenced)
2067 				smp_mb();
2068 			break;
2069 		}
2070 
2071 		fenced = true;
2072 		new = cur | I_VERSION_QUERIED;
2073 	} while (!atomic64_try_cmpxchg(&inode->i_version, &cur, new));
2074 	return cur >> I_VERSION_QUERIED_SHIFT;
2075 }
2076 EXPORT_SYMBOL(inode_query_iversion);
2077 
2078 ssize_t direct_write_fallback(struct kiocb *iocb, struct iov_iter *iter,
2079 		ssize_t direct_written, ssize_t buffered_written)
2080 {
2081 	struct address_space *mapping = iocb->ki_filp->f_mapping;
2082 	loff_t pos = iocb->ki_pos - buffered_written;
2083 	loff_t end = iocb->ki_pos - 1;
2084 	int err;
2085 
2086 	/*
2087 	 * If the buffered write fallback returned an error, we want to return
2088 	 * the number of bytes which were written by direct I/O, or the error
2089 	 * code if that was zero.
2090 	 *
2091 	 * Note that this differs from normal direct-io semantics, which will
2092 	 * return -EFOO even if some bytes were written.
2093 	 */
2094 	if (unlikely(buffered_written < 0)) {
2095 		if (direct_written)
2096 			return direct_written;
2097 		return buffered_written;
2098 	}
2099 
2100 	/*
2101 	 * We need to ensure that the page cache pages are written to disk and
2102 	 * invalidated to preserve the expected O_DIRECT semantics.
2103 	 */
2104 	err = filemap_write_and_wait_range(mapping, pos, end);
2105 	if (err < 0) {
2106 		/*
2107 		 * We don't know how much we wrote, so just return the number of
2108 		 * bytes which were direct-written
2109 		 */
2110 		iocb->ki_pos -= buffered_written;
2111 		if (direct_written)
2112 			return direct_written;
2113 		return err;
2114 	}
2115 	invalidate_mapping_pages(mapping, pos >> PAGE_SHIFT, end >> PAGE_SHIFT);
2116 	return direct_written + buffered_written;
2117 }
2118 EXPORT_SYMBOL_GPL(direct_write_fallback);
2119 
2120 /**
2121  * simple_inode_init_ts - initialize the timestamps for a new inode
2122  * @inode: inode to be initialized
2123  *
2124  * When a new inode is created, most filesystems set the timestamps to the
2125  * current time. Add a helper to do this.
2126  */
2127 struct timespec64 simple_inode_init_ts(struct inode *inode)
2128 {
2129 	struct timespec64 ts = inode_set_ctime_current(inode);
2130 
2131 	inode_set_atime_to_ts(inode, ts);
2132 	inode_set_mtime_to_ts(inode, ts);
2133 	return ts;
2134 }
2135 EXPORT_SYMBOL(simple_inode_init_ts);
2136 
2137 struct dentry *stashed_dentry_get(struct dentry **stashed)
2138 {
2139 	struct dentry *dentry;
2140 
2141 	guard(rcu)();
2142 	dentry = rcu_dereference(*stashed);
2143 	if (!dentry)
2144 		return NULL;
2145 	if (IS_ERR(dentry))
2146 		return dentry;
2147 	if (!lockref_get_not_dead(&dentry->d_lockref))
2148 		return NULL;
2149 	return dentry;
2150 }
2151 
2152 static struct dentry *prepare_anon_dentry(struct dentry **stashed,
2153 					  struct super_block *sb,
2154 					  void *data)
2155 {
2156 	struct dentry *dentry;
2157 	struct inode *inode;
2158 	const struct stashed_operations *sops = sb->s_fs_info;
2159 	int ret;
2160 
2161 	inode = new_inode_pseudo(sb);
2162 	if (!inode) {
2163 		sops->put_data(data);
2164 		return ERR_PTR(-ENOMEM);
2165 	}
2166 
2167 	inode->i_flags |= S_IMMUTABLE;
2168 	inode->i_mode = S_IFREG;
2169 	simple_inode_init_ts(inode);
2170 
2171 	ret = sops->init_inode(inode, data);
2172 	if (ret < 0) {
2173 		iput(inode);
2174 		return ERR_PTR(ret);
2175 	}
2176 
2177 	/* Notice when this is changed. */
2178 	WARN_ON_ONCE(!S_ISREG(inode->i_mode));
2179 
2180 	dentry = d_alloc_anon(sb);
2181 	if (!dentry) {
2182 		iput(inode);
2183 		return ERR_PTR(-ENOMEM);
2184 	}
2185 
2186 	/* Store address of location where dentry's supposed to be stashed. */
2187 	dentry->d_fsdata = stashed;
2188 
2189 	/* @data is now owned by the fs */
2190 	d_instantiate(dentry, inode);
2191 	return dentry;
2192 }
2193 
2194 struct dentry *stash_dentry(struct dentry **stashed, struct dentry *dentry)
2195 {
2196 	guard(rcu)();
2197 	for (;;) {
2198 		struct dentry *old;
2199 
2200 		/* Assume any old dentry was cleared out. */
2201 		old = cmpxchg(stashed, NULL, dentry);
2202 		if (likely(!old))
2203 			return dentry;
2204 
2205 		/* Check if somebody else installed a reusable dentry. */
2206 		if (lockref_get_not_dead(&old->d_lockref))
2207 			return old;
2208 
2209 		/* There's an old dead dentry there, try to take it over. */
2210 		if (likely(try_cmpxchg(stashed, &old, dentry)))
2211 			return dentry;
2212 	}
2213 }
2214 
2215 /**
2216  * path_from_stashed - create path from stashed or new dentry
2217  * @stashed:    where to retrieve or stash dentry
2218  * @mnt:        mnt of the filesystems to use
2219  * @data:       data to store in inode->i_private
2220  * @path:       path to create
2221  *
2222  * The function tries to retrieve a stashed dentry from @stashed. If the dentry
2223  * is still valid then it will be reused. If the dentry isn't able the function
2224  * will allocate a new dentry and inode. It will then check again whether it
2225  * can reuse an existing dentry in case one has been added in the meantime or
2226  * update @stashed with the newly added dentry.
2227  *
2228  * Special-purpose helper for nsfs and pidfs.
2229  *
2230  * Return: On success zero and on failure a negative error is returned.
2231  */
2232 int path_from_stashed(struct dentry **stashed, struct vfsmount *mnt, void *data,
2233 		      struct path *path)
2234 {
2235 	struct dentry *dentry, *res;
2236 	const struct stashed_operations *sops = mnt->mnt_sb->s_fs_info;
2237 
2238 	/* See if dentry can be reused. */
2239 	res = stashed_dentry_get(stashed);
2240 	if (IS_ERR(res))
2241 		return PTR_ERR(res);
2242 	if (res) {
2243 		sops->put_data(data);
2244 		goto make_path;
2245 	}
2246 
2247 	/* Allocate a new dentry. */
2248 	dentry = prepare_anon_dentry(stashed, mnt->mnt_sb, data);
2249 	if (IS_ERR(dentry))
2250 		return PTR_ERR(dentry);
2251 
2252 	/* Added a new dentry. @data is now owned by the filesystem. */
2253 	if (sops->stash_dentry)
2254 		res = sops->stash_dentry(stashed, dentry);
2255 	else
2256 		res = stash_dentry(stashed, dentry);
2257 	if (IS_ERR(res)) {
2258 		dput(dentry);
2259 		return PTR_ERR(res);
2260 	}
2261 	if (res != dentry)
2262 		dput(dentry);
2263 
2264 make_path:
2265 	path->dentry = res;
2266 	path->mnt = mntget(mnt);
2267 	VFS_WARN_ON_ONCE(path->dentry->d_fsdata != stashed);
2268 	VFS_WARN_ON_ONCE(d_inode(path->dentry)->i_private != data);
2269 	return 0;
2270 }
2271 
2272 void stashed_dentry_prune(struct dentry *dentry)
2273 {
2274 	struct dentry **stashed = dentry->d_fsdata;
2275 	struct inode *inode = d_inode(dentry);
2276 
2277 	if (WARN_ON_ONCE(!stashed))
2278 		return;
2279 
2280 	if (!inode)
2281 		return;
2282 
2283 	/*
2284 	 * Only replace our own @dentry as someone else might've
2285 	 * already cleared out @dentry and stashed their own
2286 	 * dentry in there.
2287 	 */
2288 	cmpxchg(stashed, dentry, NULL);
2289 }
2290 
2291 /**
2292  * simple_start_creating - prepare to create a given name
2293  * @parent: directory in which to prepare to create the name
2294  * @name:   the name to be created
2295  *
2296  * Required lock is taken and a lookup in performed prior to creating an
2297  * object in a directory.  No permission checking is performed.
2298  *
2299  * Returns: a negative dentry on which vfs_create() or similar may
2300  *  be attempted, or an error.
2301  */
2302 struct dentry *simple_start_creating(struct dentry *parent, const char *name)
2303 {
2304 	struct qstr qname = QSTR(name);
2305 	int err;
2306 
2307 	err = lookup_noperm_common(&qname, parent);
2308 	if (err)
2309 		return ERR_PTR(err);
2310 	return start_dirop(parent, &qname, LOOKUP_CREATE | LOOKUP_EXCL);
2311 }
2312 EXPORT_SYMBOL(simple_start_creating);
2313 
2314 /* parent must have been held exclusive since simple_start_creating() */
2315 void simple_done_creating(struct dentry *child)
2316 {
2317 	end_creating(child);
2318 }
2319 EXPORT_SYMBOL(simple_done_creating);
2320