18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *	fs/libfs.c
48c2ecf20Sopenharmony_ci *	Library for filesystems writers.
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/blkdev.h>
88c2ecf20Sopenharmony_ci#include <linux/export.h>
98c2ecf20Sopenharmony_ci#include <linux/pagemap.h>
108c2ecf20Sopenharmony_ci#include <linux/slab.h>
118c2ecf20Sopenharmony_ci#include <linux/cred.h>
128c2ecf20Sopenharmony_ci#include <linux/mount.h>
138c2ecf20Sopenharmony_ci#include <linux/vfs.h>
148c2ecf20Sopenharmony_ci#include <linux/quotaops.h>
158c2ecf20Sopenharmony_ci#include <linux/mutex.h>
168c2ecf20Sopenharmony_ci#include <linux/namei.h>
178c2ecf20Sopenharmony_ci#include <linux/exportfs.h>
188c2ecf20Sopenharmony_ci#include <linux/writeback.h>
198c2ecf20Sopenharmony_ci#include <linux/buffer_head.h> /* sync_mapping_buffers */
208c2ecf20Sopenharmony_ci#include <linux/fs_context.h>
218c2ecf20Sopenharmony_ci#include <linux/pseudo_fs.h>
228c2ecf20Sopenharmony_ci#include <linux/fsnotify.h>
238c2ecf20Sopenharmony_ci#include <linux/unicode.h>
248c2ecf20Sopenharmony_ci#include <linux/fscrypt.h>
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#include "internal.h"
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ciint simple_getattr(const struct path *path, struct kstat *stat,
318c2ecf20Sopenharmony_ci		   u32 request_mask, unsigned int query_flags)
328c2ecf20Sopenharmony_ci{
338c2ecf20Sopenharmony_ci	struct inode *inode = d_inode(path->dentry);
348c2ecf20Sopenharmony_ci	generic_fillattr(inode, stat);
358c2ecf20Sopenharmony_ci	stat->blocks = inode->i_mapping->nrpages << (PAGE_SHIFT - 9);
368c2ecf20Sopenharmony_ci	return 0;
378c2ecf20Sopenharmony_ci}
388c2ecf20Sopenharmony_ciEXPORT_SYMBOL(simple_getattr);
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ciint simple_statfs(struct dentry *dentry, struct kstatfs *buf)
418c2ecf20Sopenharmony_ci{
428c2ecf20Sopenharmony_ci	buf->f_type = dentry->d_sb->s_magic;
438c2ecf20Sopenharmony_ci	buf->f_bsize = PAGE_SIZE;
448c2ecf20Sopenharmony_ci	buf->f_namelen = NAME_MAX;
458c2ecf20Sopenharmony_ci	return 0;
468c2ecf20Sopenharmony_ci}
478c2ecf20Sopenharmony_ciEXPORT_SYMBOL(simple_statfs);
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci/*
508c2ecf20Sopenharmony_ci * Retaining negative dentries for an in-memory filesystem just wastes
518c2ecf20Sopenharmony_ci * memory and lookup time: arrange for them to be deleted immediately.
528c2ecf20Sopenharmony_ci */
538c2ecf20Sopenharmony_ciint always_delete_dentry(const struct dentry *dentry)
548c2ecf20Sopenharmony_ci{
558c2ecf20Sopenharmony_ci	return 1;
568c2ecf20Sopenharmony_ci}
578c2ecf20Sopenharmony_ciEXPORT_SYMBOL(always_delete_dentry);
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ciconst struct dentry_operations simple_dentry_operations = {
608c2ecf20Sopenharmony_ci	.d_delete = always_delete_dentry,
618c2ecf20Sopenharmony_ci};
628c2ecf20Sopenharmony_ciEXPORT_SYMBOL(simple_dentry_operations);
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci/*
658c2ecf20Sopenharmony_ci * Lookup the data. This is trivial - if the dentry didn't already
668c2ecf20Sopenharmony_ci * exist, we know it is negative.  Set d_op to delete negative dentries.
678c2ecf20Sopenharmony_ci */
688c2ecf20Sopenharmony_cistruct dentry *simple_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	if (dentry->d_name.len > NAME_MAX)
718c2ecf20Sopenharmony_ci		return ERR_PTR(-ENAMETOOLONG);
728c2ecf20Sopenharmony_ci	if (!dentry->d_sb->s_d_op)
738c2ecf20Sopenharmony_ci		d_set_d_op(dentry, &simple_dentry_operations);
748c2ecf20Sopenharmony_ci	d_add(dentry, NULL);
758c2ecf20Sopenharmony_ci	return NULL;
768c2ecf20Sopenharmony_ci}
778c2ecf20Sopenharmony_ciEXPORT_SYMBOL(simple_lookup);
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ciint dcache_dir_open(struct inode *inode, struct file *file)
808c2ecf20Sopenharmony_ci{
818c2ecf20Sopenharmony_ci	file->private_data = d_alloc_cursor(file->f_path.dentry);
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	return file->private_data ? 0 : -ENOMEM;
848c2ecf20Sopenharmony_ci}
858c2ecf20Sopenharmony_ciEXPORT_SYMBOL(dcache_dir_open);
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ciint dcache_dir_close(struct inode *inode, struct file *file)
888c2ecf20Sopenharmony_ci{
898c2ecf20Sopenharmony_ci	dput(file->private_data);
908c2ecf20Sopenharmony_ci	return 0;
918c2ecf20Sopenharmony_ci}
928c2ecf20Sopenharmony_ciEXPORT_SYMBOL(dcache_dir_close);
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci/* parent is locked at least shared */
958c2ecf20Sopenharmony_ci/*
968c2ecf20Sopenharmony_ci * Returns an element of siblings' list.
978c2ecf20Sopenharmony_ci * We are looking for <count>th positive after <p>; if
988c2ecf20Sopenharmony_ci * found, dentry is grabbed and returned to caller.
998c2ecf20Sopenharmony_ci * If no such element exists, NULL is returned.
1008c2ecf20Sopenharmony_ci */
1018c2ecf20Sopenharmony_cistatic struct dentry *scan_positives(struct dentry *cursor,
1028c2ecf20Sopenharmony_ci					struct list_head *p,
1038c2ecf20Sopenharmony_ci					loff_t count,
1048c2ecf20Sopenharmony_ci					struct dentry *last)
1058c2ecf20Sopenharmony_ci{
1068c2ecf20Sopenharmony_ci	struct dentry *dentry = cursor->d_parent, *found = NULL;
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	spin_lock(&dentry->d_lock);
1098c2ecf20Sopenharmony_ci	while ((p = p->next) != &dentry->d_subdirs) {
1108c2ecf20Sopenharmony_ci		struct dentry *d = list_entry(p, struct dentry, d_child);
1118c2ecf20Sopenharmony_ci		// we must at least skip cursors, to avoid livelocks
1128c2ecf20Sopenharmony_ci		if (d->d_flags & DCACHE_DENTRY_CURSOR)
1138c2ecf20Sopenharmony_ci			continue;
1148c2ecf20Sopenharmony_ci		if (simple_positive(d) && !--count) {
1158c2ecf20Sopenharmony_ci			spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
1168c2ecf20Sopenharmony_ci			if (simple_positive(d))
1178c2ecf20Sopenharmony_ci				found = dget_dlock(d);
1188c2ecf20Sopenharmony_ci			spin_unlock(&d->d_lock);
1198c2ecf20Sopenharmony_ci			if (likely(found))
1208c2ecf20Sopenharmony_ci				break;
1218c2ecf20Sopenharmony_ci			count = 1;
1228c2ecf20Sopenharmony_ci		}
1238c2ecf20Sopenharmony_ci		if (need_resched()) {
1248c2ecf20Sopenharmony_ci			list_move(&cursor->d_child, p);
1258c2ecf20Sopenharmony_ci			p = &cursor->d_child;
1268c2ecf20Sopenharmony_ci			spin_unlock(&dentry->d_lock);
1278c2ecf20Sopenharmony_ci			cond_resched();
1288c2ecf20Sopenharmony_ci			spin_lock(&dentry->d_lock);
1298c2ecf20Sopenharmony_ci		}
1308c2ecf20Sopenharmony_ci	}
1318c2ecf20Sopenharmony_ci	spin_unlock(&dentry->d_lock);
1328c2ecf20Sopenharmony_ci	dput(last);
1338c2ecf20Sopenharmony_ci	return found;
1348c2ecf20Sopenharmony_ci}
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ciloff_t dcache_dir_lseek(struct file *file, loff_t offset, int whence)
1378c2ecf20Sopenharmony_ci{
1388c2ecf20Sopenharmony_ci	struct dentry *dentry = file->f_path.dentry;
1398c2ecf20Sopenharmony_ci	switch (whence) {
1408c2ecf20Sopenharmony_ci		case 1:
1418c2ecf20Sopenharmony_ci			offset += file->f_pos;
1428c2ecf20Sopenharmony_ci			fallthrough;
1438c2ecf20Sopenharmony_ci		case 0:
1448c2ecf20Sopenharmony_ci			if (offset >= 0)
1458c2ecf20Sopenharmony_ci				break;
1468c2ecf20Sopenharmony_ci			fallthrough;
1478c2ecf20Sopenharmony_ci		default:
1488c2ecf20Sopenharmony_ci			return -EINVAL;
1498c2ecf20Sopenharmony_ci	}
1508c2ecf20Sopenharmony_ci	if (offset != file->f_pos) {
1518c2ecf20Sopenharmony_ci		struct dentry *cursor = file->private_data;
1528c2ecf20Sopenharmony_ci		struct dentry *to = NULL;
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci		inode_lock_shared(dentry->d_inode);
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci		if (offset > 2)
1578c2ecf20Sopenharmony_ci			to = scan_positives(cursor, &dentry->d_subdirs,
1588c2ecf20Sopenharmony_ci					    offset - 2, NULL);
1598c2ecf20Sopenharmony_ci		spin_lock(&dentry->d_lock);
1608c2ecf20Sopenharmony_ci		if (to)
1618c2ecf20Sopenharmony_ci			list_move(&cursor->d_child, &to->d_child);
1628c2ecf20Sopenharmony_ci		else
1638c2ecf20Sopenharmony_ci			list_del_init(&cursor->d_child);
1648c2ecf20Sopenharmony_ci		spin_unlock(&dentry->d_lock);
1658c2ecf20Sopenharmony_ci		dput(to);
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci		file->f_pos = offset;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci		inode_unlock_shared(dentry->d_inode);
1708c2ecf20Sopenharmony_ci	}
1718c2ecf20Sopenharmony_ci	return offset;
1728c2ecf20Sopenharmony_ci}
1738c2ecf20Sopenharmony_ciEXPORT_SYMBOL(dcache_dir_lseek);
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci/* Relationship between i_mode and the DT_xxx types */
1768c2ecf20Sopenharmony_cistatic inline unsigned char dt_type(struct inode *inode)
1778c2ecf20Sopenharmony_ci{
1788c2ecf20Sopenharmony_ci	return (inode->i_mode >> 12) & 15;
1798c2ecf20Sopenharmony_ci}
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci/*
1828c2ecf20Sopenharmony_ci * Directory is locked and all positive dentries in it are safe, since
1838c2ecf20Sopenharmony_ci * for ramfs-type trees they can't go away without unlink() or rmdir(),
1848c2ecf20Sopenharmony_ci * both impossible due to the lock on directory.
1858c2ecf20Sopenharmony_ci */
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ciint dcache_readdir(struct file *file, struct dir_context *ctx)
1888c2ecf20Sopenharmony_ci{
1898c2ecf20Sopenharmony_ci	struct dentry *dentry = file->f_path.dentry;
1908c2ecf20Sopenharmony_ci	struct dentry *cursor = file->private_data;
1918c2ecf20Sopenharmony_ci	struct list_head *anchor = &dentry->d_subdirs;
1928c2ecf20Sopenharmony_ci	struct dentry *next = NULL;
1938c2ecf20Sopenharmony_ci	struct list_head *p;
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	if (!dir_emit_dots(file, ctx))
1968c2ecf20Sopenharmony_ci		return 0;
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	if (ctx->pos == 2)
1998c2ecf20Sopenharmony_ci		p = anchor;
2008c2ecf20Sopenharmony_ci	else if (!list_empty(&cursor->d_child))
2018c2ecf20Sopenharmony_ci		p = &cursor->d_child;
2028c2ecf20Sopenharmony_ci	else
2038c2ecf20Sopenharmony_ci		return 0;
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	while ((next = scan_positives(cursor, p, 1, next)) != NULL) {
2068c2ecf20Sopenharmony_ci		if (!dir_emit(ctx, next->d_name.name, next->d_name.len,
2078c2ecf20Sopenharmony_ci			      d_inode(next)->i_ino, dt_type(d_inode(next))))
2088c2ecf20Sopenharmony_ci			break;
2098c2ecf20Sopenharmony_ci		ctx->pos++;
2108c2ecf20Sopenharmony_ci		p = &next->d_child;
2118c2ecf20Sopenharmony_ci	}
2128c2ecf20Sopenharmony_ci	spin_lock(&dentry->d_lock);
2138c2ecf20Sopenharmony_ci	if (next)
2148c2ecf20Sopenharmony_ci		list_move_tail(&cursor->d_child, &next->d_child);
2158c2ecf20Sopenharmony_ci	else
2168c2ecf20Sopenharmony_ci		list_del_init(&cursor->d_child);
2178c2ecf20Sopenharmony_ci	spin_unlock(&dentry->d_lock);
2188c2ecf20Sopenharmony_ci	dput(next);
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	return 0;
2218c2ecf20Sopenharmony_ci}
2228c2ecf20Sopenharmony_ciEXPORT_SYMBOL(dcache_readdir);
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_cissize_t generic_read_dir(struct file *filp, char __user *buf, size_t siz, loff_t *ppos)
2258c2ecf20Sopenharmony_ci{
2268c2ecf20Sopenharmony_ci	return -EISDIR;
2278c2ecf20Sopenharmony_ci}
2288c2ecf20Sopenharmony_ciEXPORT_SYMBOL(generic_read_dir);
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ciconst struct file_operations simple_dir_operations = {
2318c2ecf20Sopenharmony_ci	.open		= dcache_dir_open,
2328c2ecf20Sopenharmony_ci	.release	= dcache_dir_close,
2338c2ecf20Sopenharmony_ci	.llseek		= dcache_dir_lseek,
2348c2ecf20Sopenharmony_ci	.read		= generic_read_dir,
2358c2ecf20Sopenharmony_ci	.iterate_shared	= dcache_readdir,
2368c2ecf20Sopenharmony_ci	.fsync		= noop_fsync,
2378c2ecf20Sopenharmony_ci};
2388c2ecf20Sopenharmony_ciEXPORT_SYMBOL(simple_dir_operations);
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ciconst struct inode_operations simple_dir_inode_operations = {
2418c2ecf20Sopenharmony_ci	.lookup		= simple_lookup,
2428c2ecf20Sopenharmony_ci};
2438c2ecf20Sopenharmony_ciEXPORT_SYMBOL(simple_dir_inode_operations);
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_cistatic struct dentry *find_next_child(struct dentry *parent, struct dentry *prev)
2468c2ecf20Sopenharmony_ci{
2478c2ecf20Sopenharmony_ci	struct dentry *child = NULL;
2488c2ecf20Sopenharmony_ci	struct list_head *p = prev ? &prev->d_child : &parent->d_subdirs;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	spin_lock(&parent->d_lock);
2518c2ecf20Sopenharmony_ci	while ((p = p->next) != &parent->d_subdirs) {
2528c2ecf20Sopenharmony_ci		struct dentry *d = container_of(p, struct dentry, d_child);
2538c2ecf20Sopenharmony_ci		if (simple_positive(d)) {
2548c2ecf20Sopenharmony_ci			spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
2558c2ecf20Sopenharmony_ci			if (simple_positive(d))
2568c2ecf20Sopenharmony_ci				child = dget_dlock(d);
2578c2ecf20Sopenharmony_ci			spin_unlock(&d->d_lock);
2588c2ecf20Sopenharmony_ci			if (likely(child))
2598c2ecf20Sopenharmony_ci				break;
2608c2ecf20Sopenharmony_ci		}
2618c2ecf20Sopenharmony_ci	}
2628c2ecf20Sopenharmony_ci	spin_unlock(&parent->d_lock);
2638c2ecf20Sopenharmony_ci	dput(prev);
2648c2ecf20Sopenharmony_ci	return child;
2658c2ecf20Sopenharmony_ci}
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_civoid simple_recursive_removal(struct dentry *dentry,
2688c2ecf20Sopenharmony_ci                              void (*callback)(struct dentry *))
2698c2ecf20Sopenharmony_ci{
2708c2ecf20Sopenharmony_ci	struct dentry *this = dget(dentry);
2718c2ecf20Sopenharmony_ci	while (true) {
2728c2ecf20Sopenharmony_ci		struct dentry *victim = NULL, *child;
2738c2ecf20Sopenharmony_ci		struct inode *inode = this->d_inode;
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci		inode_lock(inode);
2768c2ecf20Sopenharmony_ci		if (d_is_dir(this))
2778c2ecf20Sopenharmony_ci			inode->i_flags |= S_DEAD;
2788c2ecf20Sopenharmony_ci		while ((child = find_next_child(this, victim)) == NULL) {
2798c2ecf20Sopenharmony_ci			// kill and ascend
2808c2ecf20Sopenharmony_ci			// update metadata while it's still locked
2818c2ecf20Sopenharmony_ci			inode->i_ctime = current_time(inode);
2828c2ecf20Sopenharmony_ci			clear_nlink(inode);
2838c2ecf20Sopenharmony_ci			inode_unlock(inode);
2848c2ecf20Sopenharmony_ci			victim = this;
2858c2ecf20Sopenharmony_ci			this = this->d_parent;
2868c2ecf20Sopenharmony_ci			inode = this->d_inode;
2878c2ecf20Sopenharmony_ci			inode_lock(inode);
2888c2ecf20Sopenharmony_ci			if (simple_positive(victim)) {
2898c2ecf20Sopenharmony_ci				d_invalidate(victim);	// avoid lost mounts
2908c2ecf20Sopenharmony_ci				if (d_is_dir(victim))
2918c2ecf20Sopenharmony_ci					fsnotify_rmdir(inode, victim);
2928c2ecf20Sopenharmony_ci				else
2938c2ecf20Sopenharmony_ci					fsnotify_unlink(inode, victim);
2948c2ecf20Sopenharmony_ci				if (callback)
2958c2ecf20Sopenharmony_ci					callback(victim);
2968c2ecf20Sopenharmony_ci				dput(victim);		// unpin it
2978c2ecf20Sopenharmony_ci			}
2988c2ecf20Sopenharmony_ci			if (victim == dentry) {
2998c2ecf20Sopenharmony_ci				inode->i_ctime = inode->i_mtime =
3008c2ecf20Sopenharmony_ci					current_time(inode);
3018c2ecf20Sopenharmony_ci				if (d_is_dir(dentry))
3028c2ecf20Sopenharmony_ci					drop_nlink(inode);
3038c2ecf20Sopenharmony_ci				inode_unlock(inode);
3048c2ecf20Sopenharmony_ci				dput(dentry);
3058c2ecf20Sopenharmony_ci				return;
3068c2ecf20Sopenharmony_ci			}
3078c2ecf20Sopenharmony_ci		}
3088c2ecf20Sopenharmony_ci		inode_unlock(inode);
3098c2ecf20Sopenharmony_ci		this = child;
3108c2ecf20Sopenharmony_ci	}
3118c2ecf20Sopenharmony_ci}
3128c2ecf20Sopenharmony_ciEXPORT_SYMBOL(simple_recursive_removal);
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_cistatic const struct super_operations simple_super_operations = {
3158c2ecf20Sopenharmony_ci	.statfs		= simple_statfs,
3168c2ecf20Sopenharmony_ci};
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_cistatic int pseudo_fs_fill_super(struct super_block *s, struct fs_context *fc)
3198c2ecf20Sopenharmony_ci{
3208c2ecf20Sopenharmony_ci	struct pseudo_fs_context *ctx = fc->fs_private;
3218c2ecf20Sopenharmony_ci	struct inode *root;
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci	s->s_maxbytes = MAX_LFS_FILESIZE;
3248c2ecf20Sopenharmony_ci	s->s_blocksize = PAGE_SIZE;
3258c2ecf20Sopenharmony_ci	s->s_blocksize_bits = PAGE_SHIFT;
3268c2ecf20Sopenharmony_ci	s->s_magic = ctx->magic;
3278c2ecf20Sopenharmony_ci	s->s_op = ctx->ops ?: &simple_super_operations;
3288c2ecf20Sopenharmony_ci	s->s_xattr = ctx->xattr;
3298c2ecf20Sopenharmony_ci	s->s_time_gran = 1;
3308c2ecf20Sopenharmony_ci	root = new_inode(s);
3318c2ecf20Sopenharmony_ci	if (!root)
3328c2ecf20Sopenharmony_ci		return -ENOMEM;
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	/*
3358c2ecf20Sopenharmony_ci	 * since this is the first inode, make it number 1. New inodes created
3368c2ecf20Sopenharmony_ci	 * after this must take care not to collide with it (by passing
3378c2ecf20Sopenharmony_ci	 * max_reserved of 1 to iunique).
3388c2ecf20Sopenharmony_ci	 */
3398c2ecf20Sopenharmony_ci	root->i_ino = 1;
3408c2ecf20Sopenharmony_ci	root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR;
3418c2ecf20Sopenharmony_ci	root->i_atime = root->i_mtime = root->i_ctime = current_time(root);
3428c2ecf20Sopenharmony_ci	s->s_root = d_make_root(root);
3438c2ecf20Sopenharmony_ci	if (!s->s_root)
3448c2ecf20Sopenharmony_ci		return -ENOMEM;
3458c2ecf20Sopenharmony_ci	s->s_d_op = ctx->dops;
3468c2ecf20Sopenharmony_ci	return 0;
3478c2ecf20Sopenharmony_ci}
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_cistatic int pseudo_fs_get_tree(struct fs_context *fc)
3508c2ecf20Sopenharmony_ci{
3518c2ecf20Sopenharmony_ci	return get_tree_nodev(fc, pseudo_fs_fill_super);
3528c2ecf20Sopenharmony_ci}
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_cistatic void pseudo_fs_free(struct fs_context *fc)
3558c2ecf20Sopenharmony_ci{
3568c2ecf20Sopenharmony_ci	kfree(fc->fs_private);
3578c2ecf20Sopenharmony_ci}
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_cistatic const struct fs_context_operations pseudo_fs_context_ops = {
3608c2ecf20Sopenharmony_ci	.free		= pseudo_fs_free,
3618c2ecf20Sopenharmony_ci	.get_tree	= pseudo_fs_get_tree,
3628c2ecf20Sopenharmony_ci};
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci/*
3658c2ecf20Sopenharmony_ci * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that
3668c2ecf20Sopenharmony_ci * will never be mountable)
3678c2ecf20Sopenharmony_ci */
3688c2ecf20Sopenharmony_cistruct pseudo_fs_context *init_pseudo(struct fs_context *fc,
3698c2ecf20Sopenharmony_ci					unsigned long magic)
3708c2ecf20Sopenharmony_ci{
3718c2ecf20Sopenharmony_ci	struct pseudo_fs_context *ctx;
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci	ctx = kzalloc(sizeof(struct pseudo_fs_context), GFP_KERNEL);
3748c2ecf20Sopenharmony_ci	if (likely(ctx)) {
3758c2ecf20Sopenharmony_ci		ctx->magic = magic;
3768c2ecf20Sopenharmony_ci		fc->fs_private = ctx;
3778c2ecf20Sopenharmony_ci		fc->ops = &pseudo_fs_context_ops;
3788c2ecf20Sopenharmony_ci		fc->sb_flags |= SB_NOUSER;
3798c2ecf20Sopenharmony_ci		fc->global = true;
3808c2ecf20Sopenharmony_ci	}
3818c2ecf20Sopenharmony_ci	return ctx;
3828c2ecf20Sopenharmony_ci}
3838c2ecf20Sopenharmony_ciEXPORT_SYMBOL(init_pseudo);
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ciint simple_open(struct inode *inode, struct file *file)
3868c2ecf20Sopenharmony_ci{
3878c2ecf20Sopenharmony_ci	if (inode->i_private)
3888c2ecf20Sopenharmony_ci		file->private_data = inode->i_private;
3898c2ecf20Sopenharmony_ci	return 0;
3908c2ecf20Sopenharmony_ci}
3918c2ecf20Sopenharmony_ciEXPORT_SYMBOL(simple_open);
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ciint simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
3948c2ecf20Sopenharmony_ci{
3958c2ecf20Sopenharmony_ci	struct inode *inode = d_inode(old_dentry);
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_ci	inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
3988c2ecf20Sopenharmony_ci	inc_nlink(inode);
3998c2ecf20Sopenharmony_ci	ihold(inode);
4008c2ecf20Sopenharmony_ci	dget(dentry);
4018c2ecf20Sopenharmony_ci	d_instantiate(dentry, inode);
4028c2ecf20Sopenharmony_ci	return 0;
4038c2ecf20Sopenharmony_ci}
4048c2ecf20Sopenharmony_ciEXPORT_SYMBOL(simple_link);
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ciint simple_empty(struct dentry *dentry)
4078c2ecf20Sopenharmony_ci{
4088c2ecf20Sopenharmony_ci	struct dentry *child;
4098c2ecf20Sopenharmony_ci	int ret = 0;
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci	spin_lock(&dentry->d_lock);
4128c2ecf20Sopenharmony_ci	list_for_each_entry(child, &dentry->d_subdirs, d_child) {
4138c2ecf20Sopenharmony_ci		spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED);
4148c2ecf20Sopenharmony_ci		if (simple_positive(child)) {
4158c2ecf20Sopenharmony_ci			spin_unlock(&child->d_lock);
4168c2ecf20Sopenharmony_ci			goto out;
4178c2ecf20Sopenharmony_ci		}
4188c2ecf20Sopenharmony_ci		spin_unlock(&child->d_lock);
4198c2ecf20Sopenharmony_ci	}
4208c2ecf20Sopenharmony_ci	ret = 1;
4218c2ecf20Sopenharmony_ciout:
4228c2ecf20Sopenharmony_ci	spin_unlock(&dentry->d_lock);
4238c2ecf20Sopenharmony_ci	return ret;
4248c2ecf20Sopenharmony_ci}
4258c2ecf20Sopenharmony_ciEXPORT_SYMBOL(simple_empty);
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ciint simple_unlink(struct inode *dir, struct dentry *dentry)
4288c2ecf20Sopenharmony_ci{
4298c2ecf20Sopenharmony_ci	struct inode *inode = d_inode(dentry);
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci	inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
4328c2ecf20Sopenharmony_ci	drop_nlink(inode);
4338c2ecf20Sopenharmony_ci	dput(dentry);
4348c2ecf20Sopenharmony_ci	return 0;
4358c2ecf20Sopenharmony_ci}
4368c2ecf20Sopenharmony_ciEXPORT_SYMBOL(simple_unlink);
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_ciint simple_rmdir(struct inode *dir, struct dentry *dentry)
4398c2ecf20Sopenharmony_ci{
4408c2ecf20Sopenharmony_ci	if (!simple_empty(dentry))
4418c2ecf20Sopenharmony_ci		return -ENOTEMPTY;
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci	drop_nlink(d_inode(dentry));
4448c2ecf20Sopenharmony_ci	simple_unlink(dir, dentry);
4458c2ecf20Sopenharmony_ci	drop_nlink(dir);
4468c2ecf20Sopenharmony_ci	return 0;
4478c2ecf20Sopenharmony_ci}
4488c2ecf20Sopenharmony_ciEXPORT_SYMBOL(simple_rmdir);
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ciint simple_rename(struct inode *old_dir, struct dentry *old_dentry,
4518c2ecf20Sopenharmony_ci		  struct inode *new_dir, struct dentry *new_dentry,
4528c2ecf20Sopenharmony_ci		  unsigned int flags)
4538c2ecf20Sopenharmony_ci{
4548c2ecf20Sopenharmony_ci	struct inode *inode = d_inode(old_dentry);
4558c2ecf20Sopenharmony_ci	int they_are_dirs = d_is_dir(old_dentry);
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_ci	if (flags & ~RENAME_NOREPLACE)
4588c2ecf20Sopenharmony_ci		return -EINVAL;
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci	if (!simple_empty(new_dentry))
4618c2ecf20Sopenharmony_ci		return -ENOTEMPTY;
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ci	if (d_really_is_positive(new_dentry)) {
4648c2ecf20Sopenharmony_ci		simple_unlink(new_dir, new_dentry);
4658c2ecf20Sopenharmony_ci		if (they_are_dirs) {
4668c2ecf20Sopenharmony_ci			drop_nlink(d_inode(new_dentry));
4678c2ecf20Sopenharmony_ci			drop_nlink(old_dir);
4688c2ecf20Sopenharmony_ci		}
4698c2ecf20Sopenharmony_ci	} else if (they_are_dirs) {
4708c2ecf20Sopenharmony_ci		drop_nlink(old_dir);
4718c2ecf20Sopenharmony_ci		inc_nlink(new_dir);
4728c2ecf20Sopenharmony_ci	}
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci	old_dir->i_ctime = old_dir->i_mtime = new_dir->i_ctime =
4758c2ecf20Sopenharmony_ci		new_dir->i_mtime = inode->i_ctime = current_time(old_dir);
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci	return 0;
4788c2ecf20Sopenharmony_ci}
4798c2ecf20Sopenharmony_ciEXPORT_SYMBOL(simple_rename);
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci/**
4828c2ecf20Sopenharmony_ci * simple_setattr - setattr for simple filesystem
4838c2ecf20Sopenharmony_ci * @dentry: dentry
4848c2ecf20Sopenharmony_ci * @iattr: iattr structure
4858c2ecf20Sopenharmony_ci *
4868c2ecf20Sopenharmony_ci * Returns 0 on success, -error on failure.
4878c2ecf20Sopenharmony_ci *
4888c2ecf20Sopenharmony_ci * simple_setattr is a simple ->setattr implementation without a proper
4898c2ecf20Sopenharmony_ci * implementation of size changes.
4908c2ecf20Sopenharmony_ci *
4918c2ecf20Sopenharmony_ci * It can either be used for in-memory filesystems or special files
4928c2ecf20Sopenharmony_ci * on simple regular filesystems.  Anything that needs to change on-disk
4938c2ecf20Sopenharmony_ci * or wire state on size changes needs its own setattr method.
4948c2ecf20Sopenharmony_ci */
4958c2ecf20Sopenharmony_ciint simple_setattr(struct dentry *dentry, struct iattr *iattr)
4968c2ecf20Sopenharmony_ci{
4978c2ecf20Sopenharmony_ci	struct inode *inode = d_inode(dentry);
4988c2ecf20Sopenharmony_ci	int error;
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci	error = setattr_prepare(dentry, iattr);
5018c2ecf20Sopenharmony_ci	if (error)
5028c2ecf20Sopenharmony_ci		return error;
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci	if (iattr->ia_valid & ATTR_SIZE)
5058c2ecf20Sopenharmony_ci		truncate_setsize(inode, iattr->ia_size);
5068c2ecf20Sopenharmony_ci	setattr_copy(inode, iattr);
5078c2ecf20Sopenharmony_ci	mark_inode_dirty(inode);
5088c2ecf20Sopenharmony_ci	return 0;
5098c2ecf20Sopenharmony_ci}
5108c2ecf20Sopenharmony_ciEXPORT_SYMBOL(simple_setattr);
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_ciint simple_readpage(struct file *file, struct page *page)
5138c2ecf20Sopenharmony_ci{
5148c2ecf20Sopenharmony_ci	clear_highpage(page);
5158c2ecf20Sopenharmony_ci	flush_dcache_page(page);
5168c2ecf20Sopenharmony_ci	SetPageUptodate(page);
5178c2ecf20Sopenharmony_ci	unlock_page(page);
5188c2ecf20Sopenharmony_ci	return 0;
5198c2ecf20Sopenharmony_ci}
5208c2ecf20Sopenharmony_ciEXPORT_SYMBOL(simple_readpage);
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_ciint simple_write_begin(struct file *file, struct address_space *mapping,
5238c2ecf20Sopenharmony_ci			loff_t pos, unsigned len, unsigned flags,
5248c2ecf20Sopenharmony_ci			struct page **pagep, void **fsdata)
5258c2ecf20Sopenharmony_ci{
5268c2ecf20Sopenharmony_ci	struct page *page;
5278c2ecf20Sopenharmony_ci	pgoff_t index;
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci	index = pos >> PAGE_SHIFT;
5308c2ecf20Sopenharmony_ci
5318c2ecf20Sopenharmony_ci	page = grab_cache_page_write_begin(mapping, index, flags);
5328c2ecf20Sopenharmony_ci	if (!page)
5338c2ecf20Sopenharmony_ci		return -ENOMEM;
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci	*pagep = page;
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_ci	if (!PageUptodate(page) && (len != PAGE_SIZE)) {
5388c2ecf20Sopenharmony_ci		unsigned from = pos & (PAGE_SIZE - 1);
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_ci		zero_user_segments(page, 0, from, from + len, PAGE_SIZE);
5418c2ecf20Sopenharmony_ci	}
5428c2ecf20Sopenharmony_ci	return 0;
5438c2ecf20Sopenharmony_ci}
5448c2ecf20Sopenharmony_ciEXPORT_SYMBOL(simple_write_begin);
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_ci/**
5478c2ecf20Sopenharmony_ci * simple_write_end - .write_end helper for non-block-device FSes
5488c2ecf20Sopenharmony_ci * @file: See .write_end of address_space_operations
5498c2ecf20Sopenharmony_ci * @mapping: 		"
5508c2ecf20Sopenharmony_ci * @pos: 		"
5518c2ecf20Sopenharmony_ci * @len: 		"
5528c2ecf20Sopenharmony_ci * @copied: 		"
5538c2ecf20Sopenharmony_ci * @page: 		"
5548c2ecf20Sopenharmony_ci * @fsdata: 		"
5558c2ecf20Sopenharmony_ci *
5568c2ecf20Sopenharmony_ci * simple_write_end does the minimum needed for updating a page after writing is
5578c2ecf20Sopenharmony_ci * done. It has the same API signature as the .write_end of
5588c2ecf20Sopenharmony_ci * address_space_operations vector. So it can just be set onto .write_end for
5598c2ecf20Sopenharmony_ci * FSes that don't need any other processing. i_mutex is assumed to be held.
5608c2ecf20Sopenharmony_ci * Block based filesystems should use generic_write_end().
5618c2ecf20Sopenharmony_ci * NOTE: Even though i_size might get updated by this function, mark_inode_dirty
5628c2ecf20Sopenharmony_ci * is not called, so a filesystem that actually does store data in .write_inode
5638c2ecf20Sopenharmony_ci * should extend on what's done here with a call to mark_inode_dirty() in the
5648c2ecf20Sopenharmony_ci * case that i_size has changed.
5658c2ecf20Sopenharmony_ci *
5668c2ecf20Sopenharmony_ci * Use *ONLY* with simple_readpage()
5678c2ecf20Sopenharmony_ci */
5688c2ecf20Sopenharmony_ciint simple_write_end(struct file *file, struct address_space *mapping,
5698c2ecf20Sopenharmony_ci			loff_t pos, unsigned len, unsigned copied,
5708c2ecf20Sopenharmony_ci			struct page *page, void *fsdata)
5718c2ecf20Sopenharmony_ci{
5728c2ecf20Sopenharmony_ci	struct inode *inode = page->mapping->host;
5738c2ecf20Sopenharmony_ci	loff_t last_pos = pos + copied;
5748c2ecf20Sopenharmony_ci
5758c2ecf20Sopenharmony_ci	/* zero the stale part of the page if we did a short copy */
5768c2ecf20Sopenharmony_ci	if (!PageUptodate(page)) {
5778c2ecf20Sopenharmony_ci		if (copied < len) {
5788c2ecf20Sopenharmony_ci			unsigned from = pos & (PAGE_SIZE - 1);
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_ci			zero_user(page, from + copied, len - copied);
5818c2ecf20Sopenharmony_ci		}
5828c2ecf20Sopenharmony_ci		SetPageUptodate(page);
5838c2ecf20Sopenharmony_ci	}
5848c2ecf20Sopenharmony_ci	/*
5858c2ecf20Sopenharmony_ci	 * No need to use i_size_read() here, the i_size
5868c2ecf20Sopenharmony_ci	 * cannot change under us because we hold the i_mutex.
5878c2ecf20Sopenharmony_ci	 */
5888c2ecf20Sopenharmony_ci	if (last_pos > inode->i_size)
5898c2ecf20Sopenharmony_ci		i_size_write(inode, last_pos);
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci	set_page_dirty(page);
5928c2ecf20Sopenharmony_ci	unlock_page(page);
5938c2ecf20Sopenharmony_ci	put_page(page);
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_ci	return copied;
5968c2ecf20Sopenharmony_ci}
5978c2ecf20Sopenharmony_ciEXPORT_SYMBOL(simple_write_end);
5988c2ecf20Sopenharmony_ci
5998c2ecf20Sopenharmony_ci/*
6008c2ecf20Sopenharmony_ci * the inodes created here are not hashed. If you use iunique to generate
6018c2ecf20Sopenharmony_ci * unique inode values later for this filesystem, then you must take care
6028c2ecf20Sopenharmony_ci * to pass it an appropriate max_reserved value to avoid collisions.
6038c2ecf20Sopenharmony_ci */
6048c2ecf20Sopenharmony_ciint simple_fill_super(struct super_block *s, unsigned long magic,
6058c2ecf20Sopenharmony_ci		      const struct tree_descr *files)
6068c2ecf20Sopenharmony_ci{
6078c2ecf20Sopenharmony_ci	struct inode *inode;
6088c2ecf20Sopenharmony_ci	struct dentry *root;
6098c2ecf20Sopenharmony_ci	struct dentry *dentry;
6108c2ecf20Sopenharmony_ci	int i;
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_ci	s->s_blocksize = PAGE_SIZE;
6138c2ecf20Sopenharmony_ci	s->s_blocksize_bits = PAGE_SHIFT;
6148c2ecf20Sopenharmony_ci	s->s_magic = magic;
6158c2ecf20Sopenharmony_ci	s->s_op = &simple_super_operations;
6168c2ecf20Sopenharmony_ci	s->s_time_gran = 1;
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ci	inode = new_inode(s);
6198c2ecf20Sopenharmony_ci	if (!inode)
6208c2ecf20Sopenharmony_ci		return -ENOMEM;
6218c2ecf20Sopenharmony_ci	/*
6228c2ecf20Sopenharmony_ci	 * because the root inode is 1, the files array must not contain an
6238c2ecf20Sopenharmony_ci	 * entry at index 1
6248c2ecf20Sopenharmony_ci	 */
6258c2ecf20Sopenharmony_ci	inode->i_ino = 1;
6268c2ecf20Sopenharmony_ci	inode->i_mode = S_IFDIR | 0755;
6278c2ecf20Sopenharmony_ci	inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
6288c2ecf20Sopenharmony_ci	inode->i_op = &simple_dir_inode_operations;
6298c2ecf20Sopenharmony_ci	inode->i_fop = &simple_dir_operations;
6308c2ecf20Sopenharmony_ci	set_nlink(inode, 2);
6318c2ecf20Sopenharmony_ci	root = d_make_root(inode);
6328c2ecf20Sopenharmony_ci	if (!root)
6338c2ecf20Sopenharmony_ci		return -ENOMEM;
6348c2ecf20Sopenharmony_ci	for (i = 0; !files->name || files->name[0]; i++, files++) {
6358c2ecf20Sopenharmony_ci		if (!files->name)
6368c2ecf20Sopenharmony_ci			continue;
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci		/* warn if it tries to conflict with the root inode */
6398c2ecf20Sopenharmony_ci		if (unlikely(i == 1))
6408c2ecf20Sopenharmony_ci			printk(KERN_WARNING "%s: %s passed in a files array"
6418c2ecf20Sopenharmony_ci				"with an index of 1!\n", __func__,
6428c2ecf20Sopenharmony_ci				s->s_type->name);
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_ci		dentry = d_alloc_name(root, files->name);
6458c2ecf20Sopenharmony_ci		if (!dentry)
6468c2ecf20Sopenharmony_ci			goto out;
6478c2ecf20Sopenharmony_ci		inode = new_inode(s);
6488c2ecf20Sopenharmony_ci		if (!inode) {
6498c2ecf20Sopenharmony_ci			dput(dentry);
6508c2ecf20Sopenharmony_ci			goto out;
6518c2ecf20Sopenharmony_ci		}
6528c2ecf20Sopenharmony_ci		inode->i_mode = S_IFREG | files->mode;
6538c2ecf20Sopenharmony_ci		inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
6548c2ecf20Sopenharmony_ci		inode->i_fop = files->ops;
6558c2ecf20Sopenharmony_ci		inode->i_ino = i;
6568c2ecf20Sopenharmony_ci		d_add(dentry, inode);
6578c2ecf20Sopenharmony_ci	}
6588c2ecf20Sopenharmony_ci	s->s_root = root;
6598c2ecf20Sopenharmony_ci	return 0;
6608c2ecf20Sopenharmony_ciout:
6618c2ecf20Sopenharmony_ci	d_genocide(root);
6628c2ecf20Sopenharmony_ci	shrink_dcache_parent(root);
6638c2ecf20Sopenharmony_ci	dput(root);
6648c2ecf20Sopenharmony_ci	return -ENOMEM;
6658c2ecf20Sopenharmony_ci}
6668c2ecf20Sopenharmony_ciEXPORT_SYMBOL(simple_fill_super);
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_cistatic DEFINE_SPINLOCK(pin_fs_lock);
6698c2ecf20Sopenharmony_ci
6708c2ecf20Sopenharmony_ciint simple_pin_fs(struct file_system_type *type, struct vfsmount **mount, int *count)
6718c2ecf20Sopenharmony_ci{
6728c2ecf20Sopenharmony_ci	struct vfsmount *mnt = NULL;
6738c2ecf20Sopenharmony_ci	spin_lock(&pin_fs_lock);
6748c2ecf20Sopenharmony_ci	if (unlikely(!*mount)) {
6758c2ecf20Sopenharmony_ci		spin_unlock(&pin_fs_lock);
6768c2ecf20Sopenharmony_ci		mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, NULL);
6778c2ecf20Sopenharmony_ci		if (IS_ERR(mnt))
6788c2ecf20Sopenharmony_ci			return PTR_ERR(mnt);
6798c2ecf20Sopenharmony_ci		spin_lock(&pin_fs_lock);
6808c2ecf20Sopenharmony_ci		if (!*mount)
6818c2ecf20Sopenharmony_ci			*mount = mnt;
6828c2ecf20Sopenharmony_ci	}
6838c2ecf20Sopenharmony_ci	mntget(*mount);
6848c2ecf20Sopenharmony_ci	++*count;
6858c2ecf20Sopenharmony_ci	spin_unlock(&pin_fs_lock);
6868c2ecf20Sopenharmony_ci	mntput(mnt);
6878c2ecf20Sopenharmony_ci	return 0;
6888c2ecf20Sopenharmony_ci}
6898c2ecf20Sopenharmony_ciEXPORT_SYMBOL(simple_pin_fs);
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_civoid simple_release_fs(struct vfsmount **mount, int *count)
6928c2ecf20Sopenharmony_ci{
6938c2ecf20Sopenharmony_ci	struct vfsmount *mnt;
6948c2ecf20Sopenharmony_ci	spin_lock(&pin_fs_lock);
6958c2ecf20Sopenharmony_ci	mnt = *mount;
6968c2ecf20Sopenharmony_ci	if (!--*count)
6978c2ecf20Sopenharmony_ci		*mount = NULL;
6988c2ecf20Sopenharmony_ci	spin_unlock(&pin_fs_lock);
6998c2ecf20Sopenharmony_ci	mntput(mnt);
7008c2ecf20Sopenharmony_ci}
7018c2ecf20Sopenharmony_ciEXPORT_SYMBOL(simple_release_fs);
7028c2ecf20Sopenharmony_ci
7038c2ecf20Sopenharmony_ci/**
7048c2ecf20Sopenharmony_ci * simple_read_from_buffer - copy data from the buffer to user space
7058c2ecf20Sopenharmony_ci * @to: the user space buffer to read to
7068c2ecf20Sopenharmony_ci * @count: the maximum number of bytes to read
7078c2ecf20Sopenharmony_ci * @ppos: the current position in the buffer
7088c2ecf20Sopenharmony_ci * @from: the buffer to read from
7098c2ecf20Sopenharmony_ci * @available: the size of the buffer
7108c2ecf20Sopenharmony_ci *
7118c2ecf20Sopenharmony_ci * The simple_read_from_buffer() function reads up to @count bytes from the
7128c2ecf20Sopenharmony_ci * buffer @from at offset @ppos into the user space address starting at @to.
7138c2ecf20Sopenharmony_ci *
7148c2ecf20Sopenharmony_ci * On success, the number of bytes read is returned and the offset @ppos is
7158c2ecf20Sopenharmony_ci * advanced by this number, or negative value is returned on error.
7168c2ecf20Sopenharmony_ci **/
7178c2ecf20Sopenharmony_cissize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos,
7188c2ecf20Sopenharmony_ci				const void *from, size_t available)
7198c2ecf20Sopenharmony_ci{
7208c2ecf20Sopenharmony_ci	loff_t pos = *ppos;
7218c2ecf20Sopenharmony_ci	size_t ret;
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_ci	if (pos < 0)
7248c2ecf20Sopenharmony_ci		return -EINVAL;
7258c2ecf20Sopenharmony_ci	if (pos >= available || !count)
7268c2ecf20Sopenharmony_ci		return 0;
7278c2ecf20Sopenharmony_ci	if (count > available - pos)
7288c2ecf20Sopenharmony_ci		count = available - pos;
7298c2ecf20Sopenharmony_ci	ret = copy_to_user(to, from + pos, count);
7308c2ecf20Sopenharmony_ci	if (ret == count)
7318c2ecf20Sopenharmony_ci		return -EFAULT;
7328c2ecf20Sopenharmony_ci	count -= ret;
7338c2ecf20Sopenharmony_ci	*ppos = pos + count;
7348c2ecf20Sopenharmony_ci	return count;
7358c2ecf20Sopenharmony_ci}
7368c2ecf20Sopenharmony_ciEXPORT_SYMBOL(simple_read_from_buffer);
7378c2ecf20Sopenharmony_ci
7388c2ecf20Sopenharmony_ci/**
7398c2ecf20Sopenharmony_ci * simple_write_to_buffer - copy data from user space to the buffer
7408c2ecf20Sopenharmony_ci * @to: the buffer to write to
7418c2ecf20Sopenharmony_ci * @available: the size of the buffer
7428c2ecf20Sopenharmony_ci * @ppos: the current position in the buffer
7438c2ecf20Sopenharmony_ci * @from: the user space buffer to read from
7448c2ecf20Sopenharmony_ci * @count: the maximum number of bytes to read
7458c2ecf20Sopenharmony_ci *
7468c2ecf20Sopenharmony_ci * The simple_write_to_buffer() function reads up to @count bytes from the user
7478c2ecf20Sopenharmony_ci * space address starting at @from into the buffer @to at offset @ppos.
7488c2ecf20Sopenharmony_ci *
7498c2ecf20Sopenharmony_ci * On success, the number of bytes written is returned and the offset @ppos is
7508c2ecf20Sopenharmony_ci * advanced by this number, or negative value is returned on error.
7518c2ecf20Sopenharmony_ci **/
7528c2ecf20Sopenharmony_cissize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos,
7538c2ecf20Sopenharmony_ci		const void __user *from, size_t count)
7548c2ecf20Sopenharmony_ci{
7558c2ecf20Sopenharmony_ci	loff_t pos = *ppos;
7568c2ecf20Sopenharmony_ci	size_t res;
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_ci	if (pos < 0)
7598c2ecf20Sopenharmony_ci		return -EINVAL;
7608c2ecf20Sopenharmony_ci	if (pos >= available || !count)
7618c2ecf20Sopenharmony_ci		return 0;
7628c2ecf20Sopenharmony_ci	if (count > available - pos)
7638c2ecf20Sopenharmony_ci		count = available - pos;
7648c2ecf20Sopenharmony_ci	res = copy_from_user(to + pos, from, count);
7658c2ecf20Sopenharmony_ci	if (res == count)
7668c2ecf20Sopenharmony_ci		return -EFAULT;
7678c2ecf20Sopenharmony_ci	count -= res;
7688c2ecf20Sopenharmony_ci	*ppos = pos + count;
7698c2ecf20Sopenharmony_ci	return count;
7708c2ecf20Sopenharmony_ci}
7718c2ecf20Sopenharmony_ciEXPORT_SYMBOL(simple_write_to_buffer);
7728c2ecf20Sopenharmony_ci
7738c2ecf20Sopenharmony_ci/**
7748c2ecf20Sopenharmony_ci * memory_read_from_buffer - copy data from the buffer
7758c2ecf20Sopenharmony_ci * @to: the kernel space buffer to read to
7768c2ecf20Sopenharmony_ci * @count: the maximum number of bytes to read
7778c2ecf20Sopenharmony_ci * @ppos: the current position in the buffer
7788c2ecf20Sopenharmony_ci * @from: the buffer to read from
7798c2ecf20Sopenharmony_ci * @available: the size of the buffer
7808c2ecf20Sopenharmony_ci *
7818c2ecf20Sopenharmony_ci * The memory_read_from_buffer() function reads up to @count bytes from the
7828c2ecf20Sopenharmony_ci * buffer @from at offset @ppos into the kernel space address starting at @to.
7838c2ecf20Sopenharmony_ci *
7848c2ecf20Sopenharmony_ci * On success, the number of bytes read is returned and the offset @ppos is
7858c2ecf20Sopenharmony_ci * advanced by this number, or negative value is returned on error.
7868c2ecf20Sopenharmony_ci **/
7878c2ecf20Sopenharmony_cissize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
7888c2ecf20Sopenharmony_ci				const void *from, size_t available)
7898c2ecf20Sopenharmony_ci{
7908c2ecf20Sopenharmony_ci	loff_t pos = *ppos;
7918c2ecf20Sopenharmony_ci
7928c2ecf20Sopenharmony_ci	if (pos < 0)
7938c2ecf20Sopenharmony_ci		return -EINVAL;
7948c2ecf20Sopenharmony_ci	if (pos >= available)
7958c2ecf20Sopenharmony_ci		return 0;
7968c2ecf20Sopenharmony_ci	if (count > available - pos)
7978c2ecf20Sopenharmony_ci		count = available - pos;
7988c2ecf20Sopenharmony_ci	memcpy(to, from + pos, count);
7998c2ecf20Sopenharmony_ci	*ppos = pos + count;
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_ci	return count;
8028c2ecf20Sopenharmony_ci}
8038c2ecf20Sopenharmony_ciEXPORT_SYMBOL(memory_read_from_buffer);
8048c2ecf20Sopenharmony_ci
8058c2ecf20Sopenharmony_ci/*
8068c2ecf20Sopenharmony_ci * Transaction based IO.
8078c2ecf20Sopenharmony_ci * The file expects a single write which triggers the transaction, and then
8088c2ecf20Sopenharmony_ci * possibly a read which collects the result - which is stored in a
8098c2ecf20Sopenharmony_ci * file-local buffer.
8108c2ecf20Sopenharmony_ci */
8118c2ecf20Sopenharmony_ci
8128c2ecf20Sopenharmony_civoid simple_transaction_set(struct file *file, size_t n)
8138c2ecf20Sopenharmony_ci{
8148c2ecf20Sopenharmony_ci	struct simple_transaction_argresp *ar = file->private_data;
8158c2ecf20Sopenharmony_ci
8168c2ecf20Sopenharmony_ci	BUG_ON(n > SIMPLE_TRANSACTION_LIMIT);
8178c2ecf20Sopenharmony_ci
8188c2ecf20Sopenharmony_ci	/*
8198c2ecf20Sopenharmony_ci	 * The barrier ensures that ar->size will really remain zero until
8208c2ecf20Sopenharmony_ci	 * ar->data is ready for reading.
8218c2ecf20Sopenharmony_ci	 */
8228c2ecf20Sopenharmony_ci	smp_mb();
8238c2ecf20Sopenharmony_ci	ar->size = n;
8248c2ecf20Sopenharmony_ci}
8258c2ecf20Sopenharmony_ciEXPORT_SYMBOL(simple_transaction_set);
8268c2ecf20Sopenharmony_ci
8278c2ecf20Sopenharmony_cichar *simple_transaction_get(struct file *file, const char __user *buf, size_t size)
8288c2ecf20Sopenharmony_ci{
8298c2ecf20Sopenharmony_ci	struct simple_transaction_argresp *ar;
8308c2ecf20Sopenharmony_ci	static DEFINE_SPINLOCK(simple_transaction_lock);
8318c2ecf20Sopenharmony_ci
8328c2ecf20Sopenharmony_ci	if (size > SIMPLE_TRANSACTION_LIMIT - 1)
8338c2ecf20Sopenharmony_ci		return ERR_PTR(-EFBIG);
8348c2ecf20Sopenharmony_ci
8358c2ecf20Sopenharmony_ci	ar = (struct simple_transaction_argresp *)get_zeroed_page(GFP_KERNEL);
8368c2ecf20Sopenharmony_ci	if (!ar)
8378c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
8388c2ecf20Sopenharmony_ci
8398c2ecf20Sopenharmony_ci	spin_lock(&simple_transaction_lock);
8408c2ecf20Sopenharmony_ci
8418c2ecf20Sopenharmony_ci	/* only one write allowed per open */
8428c2ecf20Sopenharmony_ci	if (file->private_data) {
8438c2ecf20Sopenharmony_ci		spin_unlock(&simple_transaction_lock);
8448c2ecf20Sopenharmony_ci		free_page((unsigned long)ar);
8458c2ecf20Sopenharmony_ci		return ERR_PTR(-EBUSY);
8468c2ecf20Sopenharmony_ci	}
8478c2ecf20Sopenharmony_ci
8488c2ecf20Sopenharmony_ci	file->private_data = ar;
8498c2ecf20Sopenharmony_ci
8508c2ecf20Sopenharmony_ci	spin_unlock(&simple_transaction_lock);
8518c2ecf20Sopenharmony_ci
8528c2ecf20Sopenharmony_ci	if (copy_from_user(ar->data, buf, size))
8538c2ecf20Sopenharmony_ci		return ERR_PTR(-EFAULT);
8548c2ecf20Sopenharmony_ci
8558c2ecf20Sopenharmony_ci	return ar->data;
8568c2ecf20Sopenharmony_ci}
8578c2ecf20Sopenharmony_ciEXPORT_SYMBOL(simple_transaction_get);
8588c2ecf20Sopenharmony_ci
8598c2ecf20Sopenharmony_cissize_t simple_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
8608c2ecf20Sopenharmony_ci{
8618c2ecf20Sopenharmony_ci	struct simple_transaction_argresp *ar = file->private_data;
8628c2ecf20Sopenharmony_ci
8638c2ecf20Sopenharmony_ci	if (!ar)
8648c2ecf20Sopenharmony_ci		return 0;
8658c2ecf20Sopenharmony_ci	return simple_read_from_buffer(buf, size, pos, ar->data, ar->size);
8668c2ecf20Sopenharmony_ci}
8678c2ecf20Sopenharmony_ciEXPORT_SYMBOL(simple_transaction_read);
8688c2ecf20Sopenharmony_ci
8698c2ecf20Sopenharmony_ciint simple_transaction_release(struct inode *inode, struct file *file)
8708c2ecf20Sopenharmony_ci{
8718c2ecf20Sopenharmony_ci	free_page((unsigned long)file->private_data);
8728c2ecf20Sopenharmony_ci	return 0;
8738c2ecf20Sopenharmony_ci}
8748c2ecf20Sopenharmony_ciEXPORT_SYMBOL(simple_transaction_release);
8758c2ecf20Sopenharmony_ci
8768c2ecf20Sopenharmony_ci/* Simple attribute files */
8778c2ecf20Sopenharmony_ci
8788c2ecf20Sopenharmony_cistruct simple_attr {
8798c2ecf20Sopenharmony_ci	int (*get)(void *, u64 *);
8808c2ecf20Sopenharmony_ci	int (*set)(void *, u64);
8818c2ecf20Sopenharmony_ci	char get_buf[24];	/* enough to store a u64 and "\n\0" */
8828c2ecf20Sopenharmony_ci	char set_buf[24];
8838c2ecf20Sopenharmony_ci	void *data;
8848c2ecf20Sopenharmony_ci	const char *fmt;	/* format for read operation */
8858c2ecf20Sopenharmony_ci	struct mutex mutex;	/* protects access to these buffers */
8868c2ecf20Sopenharmony_ci};
8878c2ecf20Sopenharmony_ci
8888c2ecf20Sopenharmony_ci/* simple_attr_open is called by an actual attribute open file operation
8898c2ecf20Sopenharmony_ci * to set the attribute specific access operations. */
8908c2ecf20Sopenharmony_ciint simple_attr_open(struct inode *inode, struct file *file,
8918c2ecf20Sopenharmony_ci		     int (*get)(void *, u64 *), int (*set)(void *, u64),
8928c2ecf20Sopenharmony_ci		     const char *fmt)
8938c2ecf20Sopenharmony_ci{
8948c2ecf20Sopenharmony_ci	struct simple_attr *attr;
8958c2ecf20Sopenharmony_ci
8968c2ecf20Sopenharmony_ci	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
8978c2ecf20Sopenharmony_ci	if (!attr)
8988c2ecf20Sopenharmony_ci		return -ENOMEM;
8998c2ecf20Sopenharmony_ci
9008c2ecf20Sopenharmony_ci	attr->get = get;
9018c2ecf20Sopenharmony_ci	attr->set = set;
9028c2ecf20Sopenharmony_ci	attr->data = inode->i_private;
9038c2ecf20Sopenharmony_ci	attr->fmt = fmt;
9048c2ecf20Sopenharmony_ci	mutex_init(&attr->mutex);
9058c2ecf20Sopenharmony_ci
9068c2ecf20Sopenharmony_ci	file->private_data = attr;
9078c2ecf20Sopenharmony_ci
9088c2ecf20Sopenharmony_ci	return nonseekable_open(inode, file);
9098c2ecf20Sopenharmony_ci}
9108c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(simple_attr_open);
9118c2ecf20Sopenharmony_ci
9128c2ecf20Sopenharmony_ciint simple_attr_release(struct inode *inode, struct file *file)
9138c2ecf20Sopenharmony_ci{
9148c2ecf20Sopenharmony_ci	kfree(file->private_data);
9158c2ecf20Sopenharmony_ci	return 0;
9168c2ecf20Sopenharmony_ci}
9178c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(simple_attr_release);	/* GPL-only?  This?  Really? */
9188c2ecf20Sopenharmony_ci
9198c2ecf20Sopenharmony_ci/* read from the buffer that is filled with the get function */
9208c2ecf20Sopenharmony_cissize_t simple_attr_read(struct file *file, char __user *buf,
9218c2ecf20Sopenharmony_ci			 size_t len, loff_t *ppos)
9228c2ecf20Sopenharmony_ci{
9238c2ecf20Sopenharmony_ci	struct simple_attr *attr;
9248c2ecf20Sopenharmony_ci	size_t size;
9258c2ecf20Sopenharmony_ci	ssize_t ret;
9268c2ecf20Sopenharmony_ci
9278c2ecf20Sopenharmony_ci	attr = file->private_data;
9288c2ecf20Sopenharmony_ci
9298c2ecf20Sopenharmony_ci	if (!attr->get)
9308c2ecf20Sopenharmony_ci		return -EACCES;
9318c2ecf20Sopenharmony_ci
9328c2ecf20Sopenharmony_ci	ret = mutex_lock_interruptible(&attr->mutex);
9338c2ecf20Sopenharmony_ci	if (ret)
9348c2ecf20Sopenharmony_ci		return ret;
9358c2ecf20Sopenharmony_ci
9368c2ecf20Sopenharmony_ci	if (*ppos && attr->get_buf[0]) {
9378c2ecf20Sopenharmony_ci		/* continued read */
9388c2ecf20Sopenharmony_ci		size = strlen(attr->get_buf);
9398c2ecf20Sopenharmony_ci	} else {
9408c2ecf20Sopenharmony_ci		/* first read */
9418c2ecf20Sopenharmony_ci		u64 val;
9428c2ecf20Sopenharmony_ci		ret = attr->get(attr->data, &val);
9438c2ecf20Sopenharmony_ci		if (ret)
9448c2ecf20Sopenharmony_ci			goto out;
9458c2ecf20Sopenharmony_ci
9468c2ecf20Sopenharmony_ci		size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
9478c2ecf20Sopenharmony_ci				 attr->fmt, (unsigned long long)val);
9488c2ecf20Sopenharmony_ci	}
9498c2ecf20Sopenharmony_ci
9508c2ecf20Sopenharmony_ci	ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
9518c2ecf20Sopenharmony_ciout:
9528c2ecf20Sopenharmony_ci	mutex_unlock(&attr->mutex);
9538c2ecf20Sopenharmony_ci	return ret;
9548c2ecf20Sopenharmony_ci}
9558c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(simple_attr_read);
9568c2ecf20Sopenharmony_ci
9578c2ecf20Sopenharmony_ci/* interpret the buffer as a number to call the set function with */
9588c2ecf20Sopenharmony_cistatic ssize_t simple_attr_write_xsigned(struct file *file, const char __user *buf,
9598c2ecf20Sopenharmony_ci			  size_t len, loff_t *ppos, bool is_signed)
9608c2ecf20Sopenharmony_ci{
9618c2ecf20Sopenharmony_ci	struct simple_attr *attr;
9628c2ecf20Sopenharmony_ci	unsigned long long val;
9638c2ecf20Sopenharmony_ci	size_t size;
9648c2ecf20Sopenharmony_ci	ssize_t ret;
9658c2ecf20Sopenharmony_ci
9668c2ecf20Sopenharmony_ci	attr = file->private_data;
9678c2ecf20Sopenharmony_ci	if (!attr->set)
9688c2ecf20Sopenharmony_ci		return -EACCES;
9698c2ecf20Sopenharmony_ci
9708c2ecf20Sopenharmony_ci	ret = mutex_lock_interruptible(&attr->mutex);
9718c2ecf20Sopenharmony_ci	if (ret)
9728c2ecf20Sopenharmony_ci		return ret;
9738c2ecf20Sopenharmony_ci
9748c2ecf20Sopenharmony_ci	ret = -EFAULT;
9758c2ecf20Sopenharmony_ci	size = min(sizeof(attr->set_buf) - 1, len);
9768c2ecf20Sopenharmony_ci	if (copy_from_user(attr->set_buf, buf, size))
9778c2ecf20Sopenharmony_ci		goto out;
9788c2ecf20Sopenharmony_ci
9798c2ecf20Sopenharmony_ci	attr->set_buf[size] = '\0';
9808c2ecf20Sopenharmony_ci	if (is_signed)
9818c2ecf20Sopenharmony_ci		ret = kstrtoll(attr->set_buf, 0, &val);
9828c2ecf20Sopenharmony_ci	else
9838c2ecf20Sopenharmony_ci		ret = kstrtoull(attr->set_buf, 0, &val);
9848c2ecf20Sopenharmony_ci	if (ret)
9858c2ecf20Sopenharmony_ci		goto out;
9868c2ecf20Sopenharmony_ci	ret = attr->set(attr->data, val);
9878c2ecf20Sopenharmony_ci	if (ret == 0)
9888c2ecf20Sopenharmony_ci		ret = len; /* on success, claim we got the whole input */
9898c2ecf20Sopenharmony_ciout:
9908c2ecf20Sopenharmony_ci	mutex_unlock(&attr->mutex);
9918c2ecf20Sopenharmony_ci	return ret;
9928c2ecf20Sopenharmony_ci}
9938c2ecf20Sopenharmony_ci
9948c2ecf20Sopenharmony_cissize_t simple_attr_write(struct file *file, const char __user *buf,
9958c2ecf20Sopenharmony_ci			  size_t len, loff_t *ppos)
9968c2ecf20Sopenharmony_ci{
9978c2ecf20Sopenharmony_ci	return simple_attr_write_xsigned(file, buf, len, ppos, false);
9988c2ecf20Sopenharmony_ci}
9998c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(simple_attr_write);
10008c2ecf20Sopenharmony_ci
10018c2ecf20Sopenharmony_cissize_t simple_attr_write_signed(struct file *file, const char __user *buf,
10028c2ecf20Sopenharmony_ci			  size_t len, loff_t *ppos)
10038c2ecf20Sopenharmony_ci{
10048c2ecf20Sopenharmony_ci	return simple_attr_write_xsigned(file, buf, len, ppos, true);
10058c2ecf20Sopenharmony_ci}
10068c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(simple_attr_write_signed);
10078c2ecf20Sopenharmony_ci
10088c2ecf20Sopenharmony_ci/**
10098c2ecf20Sopenharmony_ci * generic_fh_to_dentry - generic helper for the fh_to_dentry export operation
10108c2ecf20Sopenharmony_ci * @sb:		filesystem to do the file handle conversion on
10118c2ecf20Sopenharmony_ci * @fid:	file handle to convert
10128c2ecf20Sopenharmony_ci * @fh_len:	length of the file handle in bytes
10138c2ecf20Sopenharmony_ci * @fh_type:	type of file handle
10148c2ecf20Sopenharmony_ci * @get_inode:	filesystem callback to retrieve inode
10158c2ecf20Sopenharmony_ci *
10168c2ecf20Sopenharmony_ci * This function decodes @fid as long as it has one of the well-known
10178c2ecf20Sopenharmony_ci * Linux filehandle types and calls @get_inode on it to retrieve the
10188c2ecf20Sopenharmony_ci * inode for the object specified in the file handle.
10198c2ecf20Sopenharmony_ci */
10208c2ecf20Sopenharmony_cistruct dentry *generic_fh_to_dentry(struct super_block *sb, struct fid *fid,
10218c2ecf20Sopenharmony_ci		int fh_len, int fh_type, struct inode *(*get_inode)
10228c2ecf20Sopenharmony_ci			(struct super_block *sb, u64 ino, u32 gen))
10238c2ecf20Sopenharmony_ci{
10248c2ecf20Sopenharmony_ci	struct inode *inode = NULL;
10258c2ecf20Sopenharmony_ci
10268c2ecf20Sopenharmony_ci	if (fh_len < 2)
10278c2ecf20Sopenharmony_ci		return NULL;
10288c2ecf20Sopenharmony_ci
10298c2ecf20Sopenharmony_ci	switch (fh_type) {
10308c2ecf20Sopenharmony_ci	case FILEID_INO32_GEN:
10318c2ecf20Sopenharmony_ci	case FILEID_INO32_GEN_PARENT:
10328c2ecf20Sopenharmony_ci		inode = get_inode(sb, fid->i32.ino, fid->i32.gen);
10338c2ecf20Sopenharmony_ci		break;
10348c2ecf20Sopenharmony_ci	}
10358c2ecf20Sopenharmony_ci
10368c2ecf20Sopenharmony_ci	return d_obtain_alias(inode);
10378c2ecf20Sopenharmony_ci}
10388c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(generic_fh_to_dentry);
10398c2ecf20Sopenharmony_ci
10408c2ecf20Sopenharmony_ci/**
10418c2ecf20Sopenharmony_ci * generic_fh_to_parent - generic helper for the fh_to_parent export operation
10428c2ecf20Sopenharmony_ci * @sb:		filesystem to do the file handle conversion on
10438c2ecf20Sopenharmony_ci * @fid:	file handle to convert
10448c2ecf20Sopenharmony_ci * @fh_len:	length of the file handle in bytes
10458c2ecf20Sopenharmony_ci * @fh_type:	type of file handle
10468c2ecf20Sopenharmony_ci * @get_inode:	filesystem callback to retrieve inode
10478c2ecf20Sopenharmony_ci *
10488c2ecf20Sopenharmony_ci * This function decodes @fid as long as it has one of the well-known
10498c2ecf20Sopenharmony_ci * Linux filehandle types and calls @get_inode on it to retrieve the
10508c2ecf20Sopenharmony_ci * inode for the _parent_ object specified in the file handle if it
10518c2ecf20Sopenharmony_ci * is specified in the file handle, or NULL otherwise.
10528c2ecf20Sopenharmony_ci */
10538c2ecf20Sopenharmony_cistruct dentry *generic_fh_to_parent(struct super_block *sb, struct fid *fid,
10548c2ecf20Sopenharmony_ci		int fh_len, int fh_type, struct inode *(*get_inode)
10558c2ecf20Sopenharmony_ci			(struct super_block *sb, u64 ino, u32 gen))
10568c2ecf20Sopenharmony_ci{
10578c2ecf20Sopenharmony_ci	struct inode *inode = NULL;
10588c2ecf20Sopenharmony_ci
10598c2ecf20Sopenharmony_ci	if (fh_len <= 2)
10608c2ecf20Sopenharmony_ci		return NULL;
10618c2ecf20Sopenharmony_ci
10628c2ecf20Sopenharmony_ci	switch (fh_type) {
10638c2ecf20Sopenharmony_ci	case FILEID_INO32_GEN_PARENT:
10648c2ecf20Sopenharmony_ci		inode = get_inode(sb, fid->i32.parent_ino,
10658c2ecf20Sopenharmony_ci				  (fh_len > 3 ? fid->i32.parent_gen : 0));
10668c2ecf20Sopenharmony_ci		break;
10678c2ecf20Sopenharmony_ci	}
10688c2ecf20Sopenharmony_ci
10698c2ecf20Sopenharmony_ci	return d_obtain_alias(inode);
10708c2ecf20Sopenharmony_ci}
10718c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(generic_fh_to_parent);
10728c2ecf20Sopenharmony_ci
10738c2ecf20Sopenharmony_ci/**
10748c2ecf20Sopenharmony_ci * __generic_file_fsync - generic fsync implementation for simple filesystems
10758c2ecf20Sopenharmony_ci *
10768c2ecf20Sopenharmony_ci * @file:	file to synchronize
10778c2ecf20Sopenharmony_ci * @start:	start offset in bytes
10788c2ecf20Sopenharmony_ci * @end:	end offset in bytes (inclusive)
10798c2ecf20Sopenharmony_ci * @datasync:	only synchronize essential metadata if true
10808c2ecf20Sopenharmony_ci *
10818c2ecf20Sopenharmony_ci * This is a generic implementation of the fsync method for simple
10828c2ecf20Sopenharmony_ci * filesystems which track all non-inode metadata in the buffers list
10838c2ecf20Sopenharmony_ci * hanging off the address_space structure.
10848c2ecf20Sopenharmony_ci */
10858c2ecf20Sopenharmony_ciint __generic_file_fsync(struct file *file, loff_t start, loff_t end,
10868c2ecf20Sopenharmony_ci				 int datasync)
10878c2ecf20Sopenharmony_ci{
10888c2ecf20Sopenharmony_ci	struct inode *inode = file->f_mapping->host;
10898c2ecf20Sopenharmony_ci	int err;
10908c2ecf20Sopenharmony_ci	int ret;
10918c2ecf20Sopenharmony_ci
10928c2ecf20Sopenharmony_ci	err = file_write_and_wait_range(file, start, end);
10938c2ecf20Sopenharmony_ci	if (err)
10948c2ecf20Sopenharmony_ci		return err;
10958c2ecf20Sopenharmony_ci
10968c2ecf20Sopenharmony_ci	inode_lock(inode);
10978c2ecf20Sopenharmony_ci	ret = sync_mapping_buffers(inode->i_mapping);
10988c2ecf20Sopenharmony_ci	if (!(inode->i_state & I_DIRTY_ALL))
10998c2ecf20Sopenharmony_ci		goto out;
11008c2ecf20Sopenharmony_ci	if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
11018c2ecf20Sopenharmony_ci		goto out;
11028c2ecf20Sopenharmony_ci
11038c2ecf20Sopenharmony_ci	err = sync_inode_metadata(inode, 1);
11048c2ecf20Sopenharmony_ci	if (ret == 0)
11058c2ecf20Sopenharmony_ci		ret = err;
11068c2ecf20Sopenharmony_ci
11078c2ecf20Sopenharmony_ciout:
11088c2ecf20Sopenharmony_ci	inode_unlock(inode);
11098c2ecf20Sopenharmony_ci	/* check and advance again to catch errors after syncing out buffers */
11108c2ecf20Sopenharmony_ci	err = file_check_and_advance_wb_err(file);
11118c2ecf20Sopenharmony_ci	if (ret == 0)
11128c2ecf20Sopenharmony_ci		ret = err;
11138c2ecf20Sopenharmony_ci	return ret;
11148c2ecf20Sopenharmony_ci}
11158c2ecf20Sopenharmony_ciEXPORT_SYMBOL(__generic_file_fsync);
11168c2ecf20Sopenharmony_ci
11178c2ecf20Sopenharmony_ci/**
11188c2ecf20Sopenharmony_ci * generic_file_fsync - generic fsync implementation for simple filesystems
11198c2ecf20Sopenharmony_ci *			with flush
11208c2ecf20Sopenharmony_ci * @file:	file to synchronize
11218c2ecf20Sopenharmony_ci * @start:	start offset in bytes
11228c2ecf20Sopenharmony_ci * @end:	end offset in bytes (inclusive)
11238c2ecf20Sopenharmony_ci * @datasync:	only synchronize essential metadata if true
11248c2ecf20Sopenharmony_ci *
11258c2ecf20Sopenharmony_ci */
11268c2ecf20Sopenharmony_ci
11278c2ecf20Sopenharmony_ciint generic_file_fsync(struct file *file, loff_t start, loff_t end,
11288c2ecf20Sopenharmony_ci		       int datasync)
11298c2ecf20Sopenharmony_ci{
11308c2ecf20Sopenharmony_ci	struct inode *inode = file->f_mapping->host;
11318c2ecf20Sopenharmony_ci	int err;
11328c2ecf20Sopenharmony_ci
11338c2ecf20Sopenharmony_ci	err = __generic_file_fsync(file, start, end, datasync);
11348c2ecf20Sopenharmony_ci	if (err)
11358c2ecf20Sopenharmony_ci		return err;
11368c2ecf20Sopenharmony_ci	return blkdev_issue_flush(inode->i_sb->s_bdev, GFP_KERNEL);
11378c2ecf20Sopenharmony_ci}
11388c2ecf20Sopenharmony_ciEXPORT_SYMBOL(generic_file_fsync);
11398c2ecf20Sopenharmony_ci
11408c2ecf20Sopenharmony_ci/**
11418c2ecf20Sopenharmony_ci * generic_check_addressable - Check addressability of file system
11428c2ecf20Sopenharmony_ci * @blocksize_bits:	log of file system block size
11438c2ecf20Sopenharmony_ci * @num_blocks:		number of blocks in file system
11448c2ecf20Sopenharmony_ci *
11458c2ecf20Sopenharmony_ci * Determine whether a file system with @num_blocks blocks (and a
11468c2ecf20Sopenharmony_ci * block size of 2**@blocksize_bits) is addressable by the sector_t
11478c2ecf20Sopenharmony_ci * and page cache of the system.  Return 0 if so and -EFBIG otherwise.
11488c2ecf20Sopenharmony_ci */
11498c2ecf20Sopenharmony_ciint generic_check_addressable(unsigned blocksize_bits, u64 num_blocks)
11508c2ecf20Sopenharmony_ci{
11518c2ecf20Sopenharmony_ci	u64 last_fs_block = num_blocks - 1;
11528c2ecf20Sopenharmony_ci	u64 last_fs_page =
11538c2ecf20Sopenharmony_ci		last_fs_block >> (PAGE_SHIFT - blocksize_bits);
11548c2ecf20Sopenharmony_ci
11558c2ecf20Sopenharmony_ci	if (unlikely(num_blocks == 0))
11568c2ecf20Sopenharmony_ci		return 0;
11578c2ecf20Sopenharmony_ci
11588c2ecf20Sopenharmony_ci	if ((blocksize_bits < 9) || (blocksize_bits > PAGE_SHIFT))
11598c2ecf20Sopenharmony_ci		return -EINVAL;
11608c2ecf20Sopenharmony_ci
11618c2ecf20Sopenharmony_ci	if ((last_fs_block > (sector_t)(~0ULL) >> (blocksize_bits - 9)) ||
11628c2ecf20Sopenharmony_ci	    (last_fs_page > (pgoff_t)(~0ULL))) {
11638c2ecf20Sopenharmony_ci		return -EFBIG;
11648c2ecf20Sopenharmony_ci	}
11658c2ecf20Sopenharmony_ci	return 0;
11668c2ecf20Sopenharmony_ci}
11678c2ecf20Sopenharmony_ciEXPORT_SYMBOL(generic_check_addressable);
11688c2ecf20Sopenharmony_ci
11698c2ecf20Sopenharmony_ci/*
11708c2ecf20Sopenharmony_ci * No-op implementation of ->fsync for in-memory filesystems.
11718c2ecf20Sopenharmony_ci */
11728c2ecf20Sopenharmony_ciint noop_fsync(struct file *file, loff_t start, loff_t end, int datasync)
11738c2ecf20Sopenharmony_ci{
11748c2ecf20Sopenharmony_ci	return 0;
11758c2ecf20Sopenharmony_ci}
11768c2ecf20Sopenharmony_ciEXPORT_SYMBOL(noop_fsync);
11778c2ecf20Sopenharmony_ci
11788c2ecf20Sopenharmony_ciint noop_set_page_dirty(struct page *page)
11798c2ecf20Sopenharmony_ci{
11808c2ecf20Sopenharmony_ci	/*
11818c2ecf20Sopenharmony_ci	 * Unlike __set_page_dirty_no_writeback that handles dirty page
11828c2ecf20Sopenharmony_ci	 * tracking in the page object, dax does all dirty tracking in
11838c2ecf20Sopenharmony_ci	 * the inode address_space in response to mkwrite faults. In the
11848c2ecf20Sopenharmony_ci	 * dax case we only need to worry about potentially dirty CPU
11858c2ecf20Sopenharmony_ci	 * caches, not dirty page cache pages to write back.
11868c2ecf20Sopenharmony_ci	 *
11878c2ecf20Sopenharmony_ci	 * This callback is defined to prevent fallback to
11888c2ecf20Sopenharmony_ci	 * __set_page_dirty_buffers() in set_page_dirty().
11898c2ecf20Sopenharmony_ci	 */
11908c2ecf20Sopenharmony_ci	return 0;
11918c2ecf20Sopenharmony_ci}
11928c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(noop_set_page_dirty);
11938c2ecf20Sopenharmony_ci
11948c2ecf20Sopenharmony_civoid noop_invalidatepage(struct page *page, unsigned int offset,
11958c2ecf20Sopenharmony_ci		unsigned int length)
11968c2ecf20Sopenharmony_ci{
11978c2ecf20Sopenharmony_ci	/*
11988c2ecf20Sopenharmony_ci	 * There is no page cache to invalidate in the dax case, however
11998c2ecf20Sopenharmony_ci	 * we need this callback defined to prevent falling back to
12008c2ecf20Sopenharmony_ci	 * block_invalidatepage() in do_invalidatepage().
12018c2ecf20Sopenharmony_ci	 */
12028c2ecf20Sopenharmony_ci}
12038c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(noop_invalidatepage);
12048c2ecf20Sopenharmony_ci
12058c2ecf20Sopenharmony_cissize_t noop_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
12068c2ecf20Sopenharmony_ci{
12078c2ecf20Sopenharmony_ci	/*
12088c2ecf20Sopenharmony_ci	 * iomap based filesystems support direct I/O without need for
12098c2ecf20Sopenharmony_ci	 * this callback. However, it still needs to be set in
12108c2ecf20Sopenharmony_ci	 * inode->a_ops so that open/fcntl know that direct I/O is
12118c2ecf20Sopenharmony_ci	 * generally supported.
12128c2ecf20Sopenharmony_ci	 */
12138c2ecf20Sopenharmony_ci	return -EINVAL;
12148c2ecf20Sopenharmony_ci}
12158c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(noop_direct_IO);
12168c2ecf20Sopenharmony_ci
12178c2ecf20Sopenharmony_ci/* Because kfree isn't assignment-compatible with void(void*) ;-/ */
12188c2ecf20Sopenharmony_civoid kfree_link(void *p)
12198c2ecf20Sopenharmony_ci{
12208c2ecf20Sopenharmony_ci	kfree(p);
12218c2ecf20Sopenharmony_ci}
12228c2ecf20Sopenharmony_ciEXPORT_SYMBOL(kfree_link);
12238c2ecf20Sopenharmony_ci
12248c2ecf20Sopenharmony_ci/*
12258c2ecf20Sopenharmony_ci * nop .set_page_dirty method so that people can use .page_mkwrite on
12268c2ecf20Sopenharmony_ci * anon inodes.
12278c2ecf20Sopenharmony_ci */
12288c2ecf20Sopenharmony_cistatic int anon_set_page_dirty(struct page *page)
12298c2ecf20Sopenharmony_ci{
12308c2ecf20Sopenharmony_ci	return 0;
12318c2ecf20Sopenharmony_ci};
12328c2ecf20Sopenharmony_ci
12338c2ecf20Sopenharmony_ci/*
12348c2ecf20Sopenharmony_ci * A single inode exists for all anon_inode files. Contrary to pipes,
12358c2ecf20Sopenharmony_ci * anon_inode inodes have no associated per-instance data, so we need
12368c2ecf20Sopenharmony_ci * only allocate one of them.
12378c2ecf20Sopenharmony_ci */
12388c2ecf20Sopenharmony_cistruct inode *alloc_anon_inode(struct super_block *s)
12398c2ecf20Sopenharmony_ci{
12408c2ecf20Sopenharmony_ci	static const struct address_space_operations anon_aops = {
12418c2ecf20Sopenharmony_ci		.set_page_dirty = anon_set_page_dirty,
12428c2ecf20Sopenharmony_ci	};
12438c2ecf20Sopenharmony_ci	struct inode *inode = new_inode_pseudo(s);
12448c2ecf20Sopenharmony_ci
12458c2ecf20Sopenharmony_ci	if (!inode)
12468c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
12478c2ecf20Sopenharmony_ci
12488c2ecf20Sopenharmony_ci	inode->i_ino = get_next_ino();
12498c2ecf20Sopenharmony_ci	inode->i_mapping->a_ops = &anon_aops;
12508c2ecf20Sopenharmony_ci
12518c2ecf20Sopenharmony_ci	/*
12528c2ecf20Sopenharmony_ci	 * Mark the inode dirty from the very beginning,
12538c2ecf20Sopenharmony_ci	 * that way it will never be moved to the dirty
12548c2ecf20Sopenharmony_ci	 * list because mark_inode_dirty() will think
12558c2ecf20Sopenharmony_ci	 * that it already _is_ on the dirty list.
12568c2ecf20Sopenharmony_ci	 */
12578c2ecf20Sopenharmony_ci	inode->i_state = I_DIRTY;
12588c2ecf20Sopenharmony_ci	inode->i_mode = S_IRUSR | S_IWUSR;
12598c2ecf20Sopenharmony_ci	inode->i_uid = current_fsuid();
12608c2ecf20Sopenharmony_ci	inode->i_gid = current_fsgid();
12618c2ecf20Sopenharmony_ci	inode->i_flags |= S_PRIVATE;
12628c2ecf20Sopenharmony_ci	inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
12638c2ecf20Sopenharmony_ci	return inode;
12648c2ecf20Sopenharmony_ci}
12658c2ecf20Sopenharmony_ciEXPORT_SYMBOL(alloc_anon_inode);
12668c2ecf20Sopenharmony_ci
12678c2ecf20Sopenharmony_ci/**
12688c2ecf20Sopenharmony_ci * simple_nosetlease - generic helper for prohibiting leases
12698c2ecf20Sopenharmony_ci * @filp: file pointer
12708c2ecf20Sopenharmony_ci * @arg: type of lease to obtain
12718c2ecf20Sopenharmony_ci * @flp: new lease supplied for insertion
12728c2ecf20Sopenharmony_ci * @priv: private data for lm_setup operation
12738c2ecf20Sopenharmony_ci *
12748c2ecf20Sopenharmony_ci * Generic helper for filesystems that do not wish to allow leases to be set.
12758c2ecf20Sopenharmony_ci * All arguments are ignored and it just returns -EINVAL.
12768c2ecf20Sopenharmony_ci */
12778c2ecf20Sopenharmony_ciint
12788c2ecf20Sopenharmony_cisimple_nosetlease(struct file *filp, long arg, struct file_lock **flp,
12798c2ecf20Sopenharmony_ci		  void **priv)
12808c2ecf20Sopenharmony_ci{
12818c2ecf20Sopenharmony_ci	return -EINVAL;
12828c2ecf20Sopenharmony_ci}
12838c2ecf20Sopenharmony_ciEXPORT_SYMBOL(simple_nosetlease);
12848c2ecf20Sopenharmony_ci
12858c2ecf20Sopenharmony_ci/**
12868c2ecf20Sopenharmony_ci * simple_get_link - generic helper to get the target of "fast" symlinks
12878c2ecf20Sopenharmony_ci * @dentry: not used here
12888c2ecf20Sopenharmony_ci * @inode: the symlink inode
12898c2ecf20Sopenharmony_ci * @done: not used here
12908c2ecf20Sopenharmony_ci *
12918c2ecf20Sopenharmony_ci * Generic helper for filesystems to use for symlink inodes where a pointer to
12928c2ecf20Sopenharmony_ci * the symlink target is stored in ->i_link.  NOTE: this isn't normally called,
12938c2ecf20Sopenharmony_ci * since as an optimization the path lookup code uses any non-NULL ->i_link
12948c2ecf20Sopenharmony_ci * directly, without calling ->get_link().  But ->get_link() still must be set,
12958c2ecf20Sopenharmony_ci * to mark the inode_operations as being for a symlink.
12968c2ecf20Sopenharmony_ci *
12978c2ecf20Sopenharmony_ci * Return: the symlink target
12988c2ecf20Sopenharmony_ci */
12998c2ecf20Sopenharmony_ciconst char *simple_get_link(struct dentry *dentry, struct inode *inode,
13008c2ecf20Sopenharmony_ci			    struct delayed_call *done)
13018c2ecf20Sopenharmony_ci{
13028c2ecf20Sopenharmony_ci	return inode->i_link;
13038c2ecf20Sopenharmony_ci}
13048c2ecf20Sopenharmony_ciEXPORT_SYMBOL(simple_get_link);
13058c2ecf20Sopenharmony_ci
13068c2ecf20Sopenharmony_ciconst struct inode_operations simple_symlink_inode_operations = {
13078c2ecf20Sopenharmony_ci	.get_link = simple_get_link,
13088c2ecf20Sopenharmony_ci};
13098c2ecf20Sopenharmony_ciEXPORT_SYMBOL(simple_symlink_inode_operations);
13108c2ecf20Sopenharmony_ci
13118c2ecf20Sopenharmony_ci/*
13128c2ecf20Sopenharmony_ci * Operations for a permanently empty directory.
13138c2ecf20Sopenharmony_ci */
13148c2ecf20Sopenharmony_cistatic struct dentry *empty_dir_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
13158c2ecf20Sopenharmony_ci{
13168c2ecf20Sopenharmony_ci	return ERR_PTR(-ENOENT);
13178c2ecf20Sopenharmony_ci}
13188c2ecf20Sopenharmony_ci
13198c2ecf20Sopenharmony_cistatic int empty_dir_getattr(const struct path *path, struct kstat *stat,
13208c2ecf20Sopenharmony_ci			     u32 request_mask, unsigned int query_flags)
13218c2ecf20Sopenharmony_ci{
13228c2ecf20Sopenharmony_ci	struct inode *inode = d_inode(path->dentry);
13238c2ecf20Sopenharmony_ci	generic_fillattr(inode, stat);
13248c2ecf20Sopenharmony_ci	return 0;
13258c2ecf20Sopenharmony_ci}
13268c2ecf20Sopenharmony_ci
13278c2ecf20Sopenharmony_cistatic int empty_dir_setattr(struct dentry *dentry, struct iattr *attr)
13288c2ecf20Sopenharmony_ci{
13298c2ecf20Sopenharmony_ci	return -EPERM;
13308c2ecf20Sopenharmony_ci}
13318c2ecf20Sopenharmony_ci
13328c2ecf20Sopenharmony_cistatic ssize_t empty_dir_listxattr(struct dentry *dentry, char *list, size_t size)
13338c2ecf20Sopenharmony_ci{
13348c2ecf20Sopenharmony_ci	return -EOPNOTSUPP;
13358c2ecf20Sopenharmony_ci}
13368c2ecf20Sopenharmony_ci
13378c2ecf20Sopenharmony_cistatic const struct inode_operations empty_dir_inode_operations = {
13388c2ecf20Sopenharmony_ci	.lookup		= empty_dir_lookup,
13398c2ecf20Sopenharmony_ci	.permission	= generic_permission,
13408c2ecf20Sopenharmony_ci	.setattr	= empty_dir_setattr,
13418c2ecf20Sopenharmony_ci	.getattr	= empty_dir_getattr,
13428c2ecf20Sopenharmony_ci	.listxattr	= empty_dir_listxattr,
13438c2ecf20Sopenharmony_ci};
13448c2ecf20Sopenharmony_ci
13458c2ecf20Sopenharmony_cistatic loff_t empty_dir_llseek(struct file *file, loff_t offset, int whence)
13468c2ecf20Sopenharmony_ci{
13478c2ecf20Sopenharmony_ci	/* An empty directory has two entries . and .. at offsets 0 and 1 */
13488c2ecf20Sopenharmony_ci	return generic_file_llseek_size(file, offset, whence, 2, 2);
13498c2ecf20Sopenharmony_ci}
13508c2ecf20Sopenharmony_ci
13518c2ecf20Sopenharmony_cistatic int empty_dir_readdir(struct file *file, struct dir_context *ctx)
13528c2ecf20Sopenharmony_ci{
13538c2ecf20Sopenharmony_ci	dir_emit_dots(file, ctx);
13548c2ecf20Sopenharmony_ci	return 0;
13558c2ecf20Sopenharmony_ci}
13568c2ecf20Sopenharmony_ci
13578c2ecf20Sopenharmony_cistatic const struct file_operations empty_dir_operations = {
13588c2ecf20Sopenharmony_ci	.llseek		= empty_dir_llseek,
13598c2ecf20Sopenharmony_ci	.read		= generic_read_dir,
13608c2ecf20Sopenharmony_ci	.iterate_shared	= empty_dir_readdir,
13618c2ecf20Sopenharmony_ci	.fsync		= noop_fsync,
13628c2ecf20Sopenharmony_ci};
13638c2ecf20Sopenharmony_ci
13648c2ecf20Sopenharmony_ci
13658c2ecf20Sopenharmony_civoid make_empty_dir_inode(struct inode *inode)
13668c2ecf20Sopenharmony_ci{
13678c2ecf20Sopenharmony_ci	set_nlink(inode, 2);
13688c2ecf20Sopenharmony_ci	inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
13698c2ecf20Sopenharmony_ci	inode->i_uid = GLOBAL_ROOT_UID;
13708c2ecf20Sopenharmony_ci	inode->i_gid = GLOBAL_ROOT_GID;
13718c2ecf20Sopenharmony_ci	inode->i_rdev = 0;
13728c2ecf20Sopenharmony_ci	inode->i_size = 0;
13738c2ecf20Sopenharmony_ci	inode->i_blkbits = PAGE_SHIFT;
13748c2ecf20Sopenharmony_ci	inode->i_blocks = 0;
13758c2ecf20Sopenharmony_ci
13768c2ecf20Sopenharmony_ci	inode->i_op = &empty_dir_inode_operations;
13778c2ecf20Sopenharmony_ci	inode->i_opflags &= ~IOP_XATTR;
13788c2ecf20Sopenharmony_ci	inode->i_fop = &empty_dir_operations;
13798c2ecf20Sopenharmony_ci}
13808c2ecf20Sopenharmony_ci
13818c2ecf20Sopenharmony_cibool is_empty_dir_inode(struct inode *inode)
13828c2ecf20Sopenharmony_ci{
13838c2ecf20Sopenharmony_ci	return (inode->i_fop == &empty_dir_operations) &&
13848c2ecf20Sopenharmony_ci		(inode->i_op == &empty_dir_inode_operations);
13858c2ecf20Sopenharmony_ci}
13868c2ecf20Sopenharmony_ci
13878c2ecf20Sopenharmony_ci#ifdef CONFIG_UNICODE
13888c2ecf20Sopenharmony_ci/*
13898c2ecf20Sopenharmony_ci * Determine if the name of a dentry should be casefolded.
13908c2ecf20Sopenharmony_ci *
13918c2ecf20Sopenharmony_ci * Return: if names will need casefolding
13928c2ecf20Sopenharmony_ci */
13938c2ecf20Sopenharmony_cistatic bool needs_casefold(const struct inode *dir)
13948c2ecf20Sopenharmony_ci{
13958c2ecf20Sopenharmony_ci	return IS_CASEFOLDED(dir) && dir->i_sb->s_encoding;
13968c2ecf20Sopenharmony_ci}
13978c2ecf20Sopenharmony_ci
13988c2ecf20Sopenharmony_ci/**
13998c2ecf20Sopenharmony_ci * generic_ci_d_compare - generic d_compare implementation for casefolding filesystems
14008c2ecf20Sopenharmony_ci * @dentry:	dentry whose name we are checking against
14018c2ecf20Sopenharmony_ci * @len:	len of name of dentry
14028c2ecf20Sopenharmony_ci * @str:	str pointer to name of dentry
14038c2ecf20Sopenharmony_ci * @name:	Name to compare against
14048c2ecf20Sopenharmony_ci *
14058c2ecf20Sopenharmony_ci * Return: 0 if names match, 1 if mismatch, or -ERRNO
14068c2ecf20Sopenharmony_ci */
14078c2ecf20Sopenharmony_ciint generic_ci_d_compare(const struct dentry *dentry, unsigned int len,
14088c2ecf20Sopenharmony_ci			  const char *str, const struct qstr *name)
14098c2ecf20Sopenharmony_ci{
14108c2ecf20Sopenharmony_ci	const struct dentry *parent = READ_ONCE(dentry->d_parent);
14118c2ecf20Sopenharmony_ci	const struct inode *dir = READ_ONCE(parent->d_inode);
14128c2ecf20Sopenharmony_ci	const struct super_block *sb = dentry->d_sb;
14138c2ecf20Sopenharmony_ci	const struct unicode_map *um = sb->s_encoding;
14148c2ecf20Sopenharmony_ci	struct qstr qstr = QSTR_INIT(str, len);
14158c2ecf20Sopenharmony_ci	char strbuf[DNAME_INLINE_LEN];
14168c2ecf20Sopenharmony_ci	int ret;
14178c2ecf20Sopenharmony_ci
14188c2ecf20Sopenharmony_ci	if (!dir || !needs_casefold(dir))
14198c2ecf20Sopenharmony_ci		goto fallback;
14208c2ecf20Sopenharmony_ci	/*
14218c2ecf20Sopenharmony_ci	 * If the dentry name is stored in-line, then it may be concurrently
14228c2ecf20Sopenharmony_ci	 * modified by a rename.  If this happens, the VFS will eventually retry
14238c2ecf20Sopenharmony_ci	 * the lookup, so it doesn't matter what ->d_compare() returns.
14248c2ecf20Sopenharmony_ci	 * However, it's unsafe to call utf8_strncasecmp() with an unstable
14258c2ecf20Sopenharmony_ci	 * string.  Therefore, we have to copy the name into a temporary buffer.
14268c2ecf20Sopenharmony_ci	 */
14278c2ecf20Sopenharmony_ci	if (len <= DNAME_INLINE_LEN - 1) {
14288c2ecf20Sopenharmony_ci		memcpy(strbuf, str, len);
14298c2ecf20Sopenharmony_ci		strbuf[len] = 0;
14308c2ecf20Sopenharmony_ci		qstr.name = strbuf;
14318c2ecf20Sopenharmony_ci		/* prevent compiler from optimizing out the temporary buffer */
14328c2ecf20Sopenharmony_ci		barrier();
14338c2ecf20Sopenharmony_ci	}
14348c2ecf20Sopenharmony_ci	ret = utf8_strncasecmp(um, name, &qstr);
14358c2ecf20Sopenharmony_ci	if (ret >= 0)
14368c2ecf20Sopenharmony_ci		return ret;
14378c2ecf20Sopenharmony_ci
14388c2ecf20Sopenharmony_ci	if (sb_has_strict_encoding(sb))
14398c2ecf20Sopenharmony_ci		return -EINVAL;
14408c2ecf20Sopenharmony_cifallback:
14418c2ecf20Sopenharmony_ci	if (len != name->len)
14428c2ecf20Sopenharmony_ci		return 1;
14438c2ecf20Sopenharmony_ci	return !!memcmp(str, name->name, len);
14448c2ecf20Sopenharmony_ci}
14458c2ecf20Sopenharmony_ciEXPORT_SYMBOL(generic_ci_d_compare);
14468c2ecf20Sopenharmony_ci
14478c2ecf20Sopenharmony_ci/**
14488c2ecf20Sopenharmony_ci * generic_ci_d_hash - generic d_hash implementation for casefolding filesystems
14498c2ecf20Sopenharmony_ci * @dentry:	dentry of the parent directory
14508c2ecf20Sopenharmony_ci * @str:	qstr of name whose hash we should fill in
14518c2ecf20Sopenharmony_ci *
14528c2ecf20Sopenharmony_ci * Return: 0 if hash was successful or unchanged, and -EINVAL on error
14538c2ecf20Sopenharmony_ci */
14548c2ecf20Sopenharmony_ciint generic_ci_d_hash(const struct dentry *dentry, struct qstr *str)
14558c2ecf20Sopenharmony_ci{
14568c2ecf20Sopenharmony_ci	const struct inode *dir = READ_ONCE(dentry->d_inode);
14578c2ecf20Sopenharmony_ci	struct super_block *sb = dentry->d_sb;
14588c2ecf20Sopenharmony_ci	const struct unicode_map *um = sb->s_encoding;
14598c2ecf20Sopenharmony_ci	int ret = 0;
14608c2ecf20Sopenharmony_ci
14618c2ecf20Sopenharmony_ci	if (!dir || !needs_casefold(dir))
14628c2ecf20Sopenharmony_ci		return 0;
14638c2ecf20Sopenharmony_ci
14648c2ecf20Sopenharmony_ci	ret = utf8_casefold_hash(um, dentry, str);
14658c2ecf20Sopenharmony_ci	if (ret < 0 && sb_has_strict_encoding(sb))
14668c2ecf20Sopenharmony_ci		return -EINVAL;
14678c2ecf20Sopenharmony_ci	return 0;
14688c2ecf20Sopenharmony_ci}
14698c2ecf20Sopenharmony_ciEXPORT_SYMBOL(generic_ci_d_hash);
14708c2ecf20Sopenharmony_ci#endif
1471