xref: /openbmc/linux/fs/libfs.c (revision 3871cb8c)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *	fs/libfs.c
41da177e4SLinus Torvalds  *	Library for filesystems writers.
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
7ac13a829SFabian Frederick #include <linux/blkdev.h>
8630d9c47SPaul Gortmaker #include <linux/export.h>
91da177e4SLinus Torvalds #include <linux/pagemap.h>
105a0e3ad6STejun Heo #include <linux/slab.h>
115b825c3aSIngo Molnar #include <linux/cred.h>
121da177e4SLinus Torvalds #include <linux/mount.h>
131da177e4SLinus Torvalds #include <linux/vfs.h>
147bb46a67Snpiggin@suse.de #include <linux/quotaops.h>
157cf34c76SIngo Molnar #include <linux/mutex.h>
1687dc800bSAl Viro #include <linux/namei.h>
172596110aSChristoph Hellwig #include <linux/exportfs.h>
18d5aacad5SAl Viro #include <linux/writeback.h>
19ff01bb48SAl Viro #include <linux/buffer_head.h> /* sync_mapping_buffers */
2031d6d5ceSDavid Howells #include <linux/fs_context.h>
2131d6d5ceSDavid Howells #include <linux/pseudo_fs.h>
22a3d1e7ebSAl Viro #include <linux/fsnotify.h>
23c843843eSDaniel Rosenberg #include <linux/unicode.h>
24c843843eSDaniel Rosenberg #include <linux/fscrypt.h>
257cf34c76SIngo Molnar 
267c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
271da177e4SLinus Torvalds 
28a4464dbcSAl Viro #include "internal.h"
29a4464dbcSAl Viro 
30549c7297SChristian Brauner int simple_getattr(struct user_namespace *mnt_userns, const struct path *path,
31549c7297SChristian Brauner 		   struct kstat *stat, u32 request_mask,
32549c7297SChristian Brauner 		   unsigned int query_flags)
331da177e4SLinus Torvalds {
34a528d35eSDavid Howells 	struct inode *inode = d_inode(path->dentry);
350d56a451SChristian Brauner 	generic_fillattr(&init_user_ns, inode, stat);
3609cbfeafSKirill A. Shutemov 	stat->blocks = inode->i_mapping->nrpages << (PAGE_SHIFT - 9);
371da177e4SLinus Torvalds 	return 0;
381da177e4SLinus Torvalds }
3912f38872SAl Viro EXPORT_SYMBOL(simple_getattr);
401da177e4SLinus Torvalds 
41726c3342SDavid Howells int simple_statfs(struct dentry *dentry, struct kstatfs *buf)
421da177e4SLinus Torvalds {
43726c3342SDavid Howells 	buf->f_type = dentry->d_sb->s_magic;
4409cbfeafSKirill A. Shutemov 	buf->f_bsize = PAGE_SIZE;
451da177e4SLinus Torvalds 	buf->f_namelen = NAME_MAX;
461da177e4SLinus Torvalds 	return 0;
471da177e4SLinus Torvalds }
4812f38872SAl Viro EXPORT_SYMBOL(simple_statfs);
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds /*
511da177e4SLinus Torvalds  * Retaining negative dentries for an in-memory filesystem just wastes
521da177e4SLinus Torvalds  * memory and lookup time: arrange for them to be deleted immediately.
531da177e4SLinus Torvalds  */
54b26d4cd3SAl Viro int always_delete_dentry(const struct dentry *dentry)
551da177e4SLinus Torvalds {
561da177e4SLinus Torvalds 	return 1;
571da177e4SLinus Torvalds }
58b26d4cd3SAl Viro EXPORT_SYMBOL(always_delete_dentry);
59b26d4cd3SAl Viro 
60b26d4cd3SAl Viro const struct dentry_operations simple_dentry_operations = {
61b26d4cd3SAl Viro 	.d_delete = always_delete_dentry,
62b26d4cd3SAl Viro };
63b26d4cd3SAl Viro EXPORT_SYMBOL(simple_dentry_operations);
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds /*
661da177e4SLinus Torvalds  * Lookup the data. This is trivial - if the dentry didn't already
671da177e4SLinus Torvalds  * exist, we know it is negative.  Set d_op to delete negative dentries.
681da177e4SLinus Torvalds  */
6900cd8dd3SAl Viro struct dentry *simple_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
701da177e4SLinus Torvalds {
711da177e4SLinus Torvalds 	if (dentry->d_name.len > NAME_MAX)
721da177e4SLinus Torvalds 		return ERR_PTR(-ENAMETOOLONG);
7374931da7SAl Viro 	if (!dentry->d_sb->s_d_op)
74fb045adbSNick Piggin 		d_set_d_op(dentry, &simple_dentry_operations);
751da177e4SLinus Torvalds 	d_add(dentry, NULL);
761da177e4SLinus Torvalds 	return NULL;
771da177e4SLinus Torvalds }
7812f38872SAl Viro EXPORT_SYMBOL(simple_lookup);
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds int dcache_dir_open(struct inode *inode, struct file *file)
811da177e4SLinus Torvalds {
82ba65dc5eSAl Viro 	file->private_data = d_alloc_cursor(file->f_path.dentry);
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds 	return file->private_data ? 0 : -ENOMEM;
851da177e4SLinus Torvalds }
8612f38872SAl Viro EXPORT_SYMBOL(dcache_dir_open);
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds int dcache_dir_close(struct inode *inode, struct file *file)
891da177e4SLinus Torvalds {
901da177e4SLinus Torvalds 	dput(file->private_data);
911da177e4SLinus Torvalds 	return 0;
921da177e4SLinus Torvalds }
9312f38872SAl Viro EXPORT_SYMBOL(dcache_dir_close);
941da177e4SLinus Torvalds 
954f42c1b5SAl Viro /* parent is locked at least shared */
96d4f4de5eSAl Viro /*
97d4f4de5eSAl Viro  * Returns an element of siblings' list.
98d4f4de5eSAl Viro  * We are looking for <count>th positive after <p>; if
9926b6c984SAl Viro  * found, dentry is grabbed and returned to caller.
10026b6c984SAl Viro  * If no such element exists, NULL is returned.
101d4f4de5eSAl Viro  */
10226b6c984SAl Viro static struct dentry *scan_positives(struct dentry *cursor,
103d4f4de5eSAl Viro 					struct list_head *p,
104d4f4de5eSAl Viro 					loff_t count,
10526b6c984SAl Viro 					struct dentry *last)
1064f42c1b5SAl Viro {
107d4f4de5eSAl Viro 	struct dentry *dentry = cursor->d_parent, *found = NULL;
1084f42c1b5SAl Viro 
109d4f4de5eSAl Viro 	spin_lock(&dentry->d_lock);
110d4f4de5eSAl Viro 	while ((p = p->next) != &dentry->d_subdirs) {
1114f42c1b5SAl Viro 		struct dentry *d = list_entry(p, struct dentry, d_child);
112d4f4de5eSAl Viro 		// we must at least skip cursors, to avoid livelocks
113d4f4de5eSAl Viro 		if (d->d_flags & DCACHE_DENTRY_CURSOR)
114d4f4de5eSAl Viro 			continue;
115d4f4de5eSAl Viro 		if (simple_positive(d) && !--count) {
116d4f4de5eSAl Viro 			spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
117d4f4de5eSAl Viro 			if (simple_positive(d))
118d4f4de5eSAl Viro 				found = dget_dlock(d);
119d4f4de5eSAl Viro 			spin_unlock(&d->d_lock);
120d4f4de5eSAl Viro 			if (likely(found))
1214f42c1b5SAl Viro 				break;
122d4f4de5eSAl Viro 			count = 1;
123d4f4de5eSAl Viro 		}
124d4f4de5eSAl Viro 		if (need_resched()) {
125d4f4de5eSAl Viro 			list_move(&cursor->d_child, p);
126d4f4de5eSAl Viro 			p = &cursor->d_child;
127d4f4de5eSAl Viro 			spin_unlock(&dentry->d_lock);
128d4f4de5eSAl Viro 			cond_resched();
129d4f4de5eSAl Viro 			spin_lock(&dentry->d_lock);
1304f42c1b5SAl Viro 		}
1314f42c1b5SAl Viro 	}
132d4f4de5eSAl Viro 	spin_unlock(&dentry->d_lock);
13326b6c984SAl Viro 	dput(last);
13426b6c984SAl Viro 	return found;
1354f42c1b5SAl Viro }
1364f42c1b5SAl Viro 
137965c8e59SAndrew Morton loff_t dcache_dir_lseek(struct file *file, loff_t offset, int whence)
1381da177e4SLinus Torvalds {
1392fd6b7f5SNick Piggin 	struct dentry *dentry = file->f_path.dentry;
140965c8e59SAndrew Morton 	switch (whence) {
1411da177e4SLinus Torvalds 		case 1:
1421da177e4SLinus Torvalds 			offset += file->f_pos;
143df561f66SGustavo A. R. Silva 			fallthrough;
1441da177e4SLinus Torvalds 		case 0:
1451da177e4SLinus Torvalds 			if (offset >= 0)
1461da177e4SLinus Torvalds 				break;
147df561f66SGustavo A. R. Silva 			fallthrough;
1481da177e4SLinus Torvalds 		default:
1491da177e4SLinus Torvalds 			return -EINVAL;
1501da177e4SLinus Torvalds 	}
1511da177e4SLinus Torvalds 	if (offset != file->f_pos) {
1521da177e4SLinus Torvalds 		struct dentry *cursor = file->private_data;
153d4f4de5eSAl Viro 		struct dentry *to = NULL;
1541da177e4SLinus Torvalds 
155274f5b04SAl Viro 		inode_lock_shared(dentry->d_inode);
156d4f4de5eSAl Viro 
15726b6c984SAl Viro 		if (offset > 2)
15826b6c984SAl Viro 			to = scan_positives(cursor, &dentry->d_subdirs,
15926b6c984SAl Viro 					    offset - 2, NULL);
160d4f4de5eSAl Viro 		spin_lock(&dentry->d_lock);
16126b6c984SAl Viro 		if (to)
16226b6c984SAl Viro 			list_move(&cursor->d_child, &to->d_child);
16326b6c984SAl Viro 		else
164d4f4de5eSAl Viro 			list_del_init(&cursor->d_child);
165d4f4de5eSAl Viro 		spin_unlock(&dentry->d_lock);
166d4f4de5eSAl Viro 		dput(to);
167d4f4de5eSAl Viro 
16826b6c984SAl Viro 		file->f_pos = offset;
16926b6c984SAl Viro 
170d4f4de5eSAl Viro 		inode_unlock_shared(dentry->d_inode);
1711da177e4SLinus Torvalds 	}
1721da177e4SLinus Torvalds 	return offset;
1731da177e4SLinus Torvalds }
17412f38872SAl Viro EXPORT_SYMBOL(dcache_dir_lseek);
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds /* Relationship between i_mode and the DT_xxx types */
1771da177e4SLinus Torvalds static inline unsigned char dt_type(struct inode *inode)
1781da177e4SLinus Torvalds {
1791da177e4SLinus Torvalds 	return (inode->i_mode >> 12) & 15;
1801da177e4SLinus Torvalds }
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds /*
1831da177e4SLinus Torvalds  * Directory is locked and all positive dentries in it are safe, since
1841da177e4SLinus Torvalds  * for ramfs-type trees they can't go away without unlink() or rmdir(),
1851da177e4SLinus Torvalds  * both impossible due to the lock on directory.
1861da177e4SLinus Torvalds  */
1871da177e4SLinus Torvalds 
1885f99f4e7SAl Viro int dcache_readdir(struct file *file, struct dir_context *ctx)
1891da177e4SLinus Torvalds {
1905f99f4e7SAl Viro 	struct dentry *dentry = file->f_path.dentry;
1915f99f4e7SAl Viro 	struct dentry *cursor = file->private_data;
192d4f4de5eSAl Viro 	struct list_head *anchor = &dentry->d_subdirs;
193d4f4de5eSAl Viro 	struct dentry *next = NULL;
194d4f4de5eSAl Viro 	struct list_head *p;
1951da177e4SLinus Torvalds 
1965f99f4e7SAl Viro 	if (!dir_emit_dots(file, ctx))
1975f99f4e7SAl Viro 		return 0;
1984f42c1b5SAl Viro 
1995f99f4e7SAl Viro 	if (ctx->pos == 2)
200d4f4de5eSAl Viro 		p = anchor;
20126b6c984SAl Viro 	else if (!list_empty(&cursor->d_child))
202d4f4de5eSAl Viro 		p = &cursor->d_child;
20326b6c984SAl Viro 	else
20426b6c984SAl Viro 		return 0;
205d4f4de5eSAl Viro 
20626b6c984SAl Viro 	while ((next = scan_positives(cursor, p, 1, next)) != NULL) {
2075f99f4e7SAl Viro 		if (!dir_emit(ctx, next->d_name.name, next->d_name.len,
208dea655c2SDavid Howells 			      d_inode(next)->i_ino, dt_type(d_inode(next))))
2094f42c1b5SAl Viro 			break;
2105f99f4e7SAl Viro 		ctx->pos++;
21126b6c984SAl Viro 		p = &next->d_child;
2121da177e4SLinus Torvalds 	}
213d4f4de5eSAl Viro 	spin_lock(&dentry->d_lock);
21426b6c984SAl Viro 	if (next)
21526b6c984SAl Viro 		list_move_tail(&cursor->d_child, &next->d_child);
21626b6c984SAl Viro 	else
21726b6c984SAl Viro 		list_del_init(&cursor->d_child);
218d4f4de5eSAl Viro 	spin_unlock(&dentry->d_lock);
219d4f4de5eSAl Viro 	dput(next);
220d4f4de5eSAl Viro 
2211da177e4SLinus Torvalds 	return 0;
2221da177e4SLinus Torvalds }
22312f38872SAl Viro EXPORT_SYMBOL(dcache_readdir);
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds ssize_t generic_read_dir(struct file *filp, char __user *buf, size_t siz, loff_t *ppos)
2261da177e4SLinus Torvalds {
2271da177e4SLinus Torvalds 	return -EISDIR;
2281da177e4SLinus Torvalds }
22912f38872SAl Viro EXPORT_SYMBOL(generic_read_dir);
2301da177e4SLinus Torvalds 
2314b6f5d20SArjan van de Ven const struct file_operations simple_dir_operations = {
2321da177e4SLinus Torvalds 	.open		= dcache_dir_open,
2331da177e4SLinus Torvalds 	.release	= dcache_dir_close,
2341da177e4SLinus Torvalds 	.llseek		= dcache_dir_lseek,
2351da177e4SLinus Torvalds 	.read		= generic_read_dir,
2364e82901cSAl Viro 	.iterate_shared	= dcache_readdir,
2371b061d92SChristoph Hellwig 	.fsync		= noop_fsync,
2381da177e4SLinus Torvalds };
23912f38872SAl Viro EXPORT_SYMBOL(simple_dir_operations);
2401da177e4SLinus Torvalds 
24192e1d5beSArjan van de Ven const struct inode_operations simple_dir_inode_operations = {
2421da177e4SLinus Torvalds 	.lookup		= simple_lookup,
2431da177e4SLinus Torvalds };
24412f38872SAl Viro EXPORT_SYMBOL(simple_dir_inode_operations);
2451da177e4SLinus Torvalds 
246a3d1e7ebSAl Viro static struct dentry *find_next_child(struct dentry *parent, struct dentry *prev)
247a3d1e7ebSAl Viro {
248a3d1e7ebSAl Viro 	struct dentry *child = NULL;
249a3d1e7ebSAl Viro 	struct list_head *p = prev ? &prev->d_child : &parent->d_subdirs;
250a3d1e7ebSAl Viro 
251a3d1e7ebSAl Viro 	spin_lock(&parent->d_lock);
252a3d1e7ebSAl Viro 	while ((p = p->next) != &parent->d_subdirs) {
253a3d1e7ebSAl Viro 		struct dentry *d = container_of(p, struct dentry, d_child);
254a3d1e7ebSAl Viro 		if (simple_positive(d)) {
255a3d1e7ebSAl Viro 			spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
256a3d1e7ebSAl Viro 			if (simple_positive(d))
257a3d1e7ebSAl Viro 				child = dget_dlock(d);
258a3d1e7ebSAl Viro 			spin_unlock(&d->d_lock);
259a3d1e7ebSAl Viro 			if (likely(child))
260a3d1e7ebSAl Viro 				break;
261a3d1e7ebSAl Viro 		}
262a3d1e7ebSAl Viro 	}
263a3d1e7ebSAl Viro 	spin_unlock(&parent->d_lock);
264a3d1e7ebSAl Viro 	dput(prev);
265a3d1e7ebSAl Viro 	return child;
266a3d1e7ebSAl Viro }
267a3d1e7ebSAl Viro 
268a3d1e7ebSAl Viro void simple_recursive_removal(struct dentry *dentry,
269a3d1e7ebSAl Viro                               void (*callback)(struct dentry *))
270a3d1e7ebSAl Viro {
271a3d1e7ebSAl Viro 	struct dentry *this = dget(dentry);
272a3d1e7ebSAl Viro 	while (true) {
273a3d1e7ebSAl Viro 		struct dentry *victim = NULL, *child;
274a3d1e7ebSAl Viro 		struct inode *inode = this->d_inode;
275a3d1e7ebSAl Viro 
276a3d1e7ebSAl Viro 		inode_lock(inode);
277a3d1e7ebSAl Viro 		if (d_is_dir(this))
278a3d1e7ebSAl Viro 			inode->i_flags |= S_DEAD;
279a3d1e7ebSAl Viro 		while ((child = find_next_child(this, victim)) == NULL) {
280a3d1e7ebSAl Viro 			// kill and ascend
281a3d1e7ebSAl Viro 			// update metadata while it's still locked
282a3d1e7ebSAl Viro 			inode->i_ctime = current_time(inode);
283a3d1e7ebSAl Viro 			clear_nlink(inode);
284a3d1e7ebSAl Viro 			inode_unlock(inode);
285a3d1e7ebSAl Viro 			victim = this;
286a3d1e7ebSAl Viro 			this = this->d_parent;
287a3d1e7ebSAl Viro 			inode = this->d_inode;
288a3d1e7ebSAl Viro 			inode_lock(inode);
289a3d1e7ebSAl Viro 			if (simple_positive(victim)) {
290a3d1e7ebSAl Viro 				d_invalidate(victim);	// avoid lost mounts
291a3d1e7ebSAl Viro 				if (d_is_dir(victim))
292a3d1e7ebSAl Viro 					fsnotify_rmdir(inode, victim);
293a3d1e7ebSAl Viro 				else
294a3d1e7ebSAl Viro 					fsnotify_unlink(inode, victim);
295a3d1e7ebSAl Viro 				if (callback)
296a3d1e7ebSAl Viro 					callback(victim);
297a3d1e7ebSAl Viro 				dput(victim);		// unpin it
298a3d1e7ebSAl Viro 			}
299a3d1e7ebSAl Viro 			if (victim == dentry) {
300a3d1e7ebSAl Viro 				inode->i_ctime = inode->i_mtime =
301a3d1e7ebSAl Viro 					current_time(inode);
302a3d1e7ebSAl Viro 				if (d_is_dir(dentry))
303a3d1e7ebSAl Viro 					drop_nlink(inode);
304a3d1e7ebSAl Viro 				inode_unlock(inode);
305a3d1e7ebSAl Viro 				dput(dentry);
306a3d1e7ebSAl Viro 				return;
307a3d1e7ebSAl Viro 			}
308a3d1e7ebSAl Viro 		}
309a3d1e7ebSAl Viro 		inode_unlock(inode);
310a3d1e7ebSAl Viro 		this = child;
311a3d1e7ebSAl Viro 	}
312a3d1e7ebSAl Viro }
313a3d1e7ebSAl Viro EXPORT_SYMBOL(simple_recursive_removal);
314a3d1e7ebSAl Viro 
315759b9775SHugh Dickins static const struct super_operations simple_super_operations = {
316759b9775SHugh Dickins 	.statfs		= simple_statfs,
317759b9775SHugh Dickins };
318759b9775SHugh Dickins 
319db2c246aSDavid Howells static int pseudo_fs_fill_super(struct super_block *s, struct fs_context *fc)
3201da177e4SLinus Torvalds {
32131d6d5ceSDavid Howells 	struct pseudo_fs_context *ctx = fc->fs_private;
3221da177e4SLinus Torvalds 	struct inode *root;
3231da177e4SLinus Torvalds 
32489a4eb4bSJeff Layton 	s->s_maxbytes = MAX_LFS_FILESIZE;
3253971e1a9SAlex Nixon 	s->s_blocksize = PAGE_SIZE;
3263971e1a9SAlex Nixon 	s->s_blocksize_bits = PAGE_SHIFT;
3278d9e46d8SAl Viro 	s->s_magic = ctx->magic;
3288d9e46d8SAl Viro 	s->s_op = ctx->ops ?: &simple_super_operations;
3298d9e46d8SAl Viro 	s->s_xattr = ctx->xattr;
3301da177e4SLinus Torvalds 	s->s_time_gran = 1;
3311da177e4SLinus Torvalds 	root = new_inode(s);
3321da177e4SLinus Torvalds 	if (!root)
333db2c246aSDavid Howells 		return -ENOMEM;
334db2c246aSDavid Howells 
3351a1c9bb4SJeff Layton 	/*
3361a1c9bb4SJeff Layton 	 * since this is the first inode, make it number 1. New inodes created
3371a1c9bb4SJeff Layton 	 * after this must take care not to collide with it (by passing
3381a1c9bb4SJeff Layton 	 * max_reserved of 1 to iunique).
3391a1c9bb4SJeff Layton 	 */
3401a1c9bb4SJeff Layton 	root->i_ino = 1;
3411da177e4SLinus Torvalds 	root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR;
342078cd827SDeepa Dinamani 	root->i_atime = root->i_mtime = root->i_ctime = current_time(root);
3438d9e46d8SAl Viro 	s->s_root = d_make_root(root);
3448d9e46d8SAl Viro 	if (!s->s_root)
3458d9e46d8SAl Viro 		return -ENOMEM;
346db2c246aSDavid Howells 	s->s_d_op = ctx->dops;
347db2c246aSDavid Howells 	return 0;
3481da177e4SLinus Torvalds }
3491da177e4SLinus Torvalds 
350db2c246aSDavid Howells static int pseudo_fs_get_tree(struct fs_context *fc)
351db2c246aSDavid Howells {
3522ac295d4SAl Viro 	return get_tree_nodev(fc, pseudo_fs_fill_super);
3531da177e4SLinus Torvalds }
35431d6d5ceSDavid Howells 
35531d6d5ceSDavid Howells static void pseudo_fs_free(struct fs_context *fc)
35631d6d5ceSDavid Howells {
35731d6d5ceSDavid Howells 	kfree(fc->fs_private);
35831d6d5ceSDavid Howells }
35931d6d5ceSDavid Howells 
36031d6d5ceSDavid Howells static const struct fs_context_operations pseudo_fs_context_ops = {
36131d6d5ceSDavid Howells 	.free		= pseudo_fs_free,
36231d6d5ceSDavid Howells 	.get_tree	= pseudo_fs_get_tree,
36331d6d5ceSDavid Howells };
36431d6d5ceSDavid Howells 
36531d6d5ceSDavid Howells /*
36631d6d5ceSDavid Howells  * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that
36731d6d5ceSDavid Howells  * will never be mountable)
36831d6d5ceSDavid Howells  */
36931d6d5ceSDavid Howells struct pseudo_fs_context *init_pseudo(struct fs_context *fc,
37031d6d5ceSDavid Howells 					unsigned long magic)
37131d6d5ceSDavid Howells {
37231d6d5ceSDavid Howells 	struct pseudo_fs_context *ctx;
37331d6d5ceSDavid Howells 
37431d6d5ceSDavid Howells 	ctx = kzalloc(sizeof(struct pseudo_fs_context), GFP_KERNEL);
37531d6d5ceSDavid Howells 	if (likely(ctx)) {
37631d6d5ceSDavid Howells 		ctx->magic = magic;
37731d6d5ceSDavid Howells 		fc->fs_private = ctx;
37831d6d5ceSDavid Howells 		fc->ops = &pseudo_fs_context_ops;
379db2c246aSDavid Howells 		fc->sb_flags |= SB_NOUSER;
380db2c246aSDavid Howells 		fc->global = true;
38131d6d5ceSDavid Howells 	}
38231d6d5ceSDavid Howells 	return ctx;
38331d6d5ceSDavid Howells }
38431d6d5ceSDavid Howells EXPORT_SYMBOL(init_pseudo);
3851da177e4SLinus Torvalds 
38620955e89SStephen Boyd int simple_open(struct inode *inode, struct file *file)
38720955e89SStephen Boyd {
38820955e89SStephen Boyd 	if (inode->i_private)
38920955e89SStephen Boyd 		file->private_data = inode->i_private;
39020955e89SStephen Boyd 	return 0;
39120955e89SStephen Boyd }
39212f38872SAl Viro EXPORT_SYMBOL(simple_open);
39320955e89SStephen Boyd 
3941da177e4SLinus Torvalds int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
3951da177e4SLinus Torvalds {
396dea655c2SDavid Howells 	struct inode *inode = d_inode(old_dentry);
3971da177e4SLinus Torvalds 
398078cd827SDeepa Dinamani 	inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
399d8c76e6fSDave Hansen 	inc_nlink(inode);
4007de9c6eeSAl Viro 	ihold(inode);
4011da177e4SLinus Torvalds 	dget(dentry);
4021da177e4SLinus Torvalds 	d_instantiate(dentry, inode);
4031da177e4SLinus Torvalds 	return 0;
4041da177e4SLinus Torvalds }
40512f38872SAl Viro EXPORT_SYMBOL(simple_link);
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds int simple_empty(struct dentry *dentry)
4081da177e4SLinus Torvalds {
4091da177e4SLinus Torvalds 	struct dentry *child;
4101da177e4SLinus Torvalds 	int ret = 0;
4111da177e4SLinus Torvalds 
4122fd6b7f5SNick Piggin 	spin_lock(&dentry->d_lock);
413946e51f2SAl Viro 	list_for_each_entry(child, &dentry->d_subdirs, d_child) {
414da502956SNick Piggin 		spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED);
415da502956SNick Piggin 		if (simple_positive(child)) {
416da502956SNick Piggin 			spin_unlock(&child->d_lock);
4171da177e4SLinus Torvalds 			goto out;
418da502956SNick Piggin 		}
419da502956SNick Piggin 		spin_unlock(&child->d_lock);
420da502956SNick Piggin 	}
4211da177e4SLinus Torvalds 	ret = 1;
4221da177e4SLinus Torvalds out:
4232fd6b7f5SNick Piggin 	spin_unlock(&dentry->d_lock);
4241da177e4SLinus Torvalds 	return ret;
4251da177e4SLinus Torvalds }
42612f38872SAl Viro EXPORT_SYMBOL(simple_empty);
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds int simple_unlink(struct inode *dir, struct dentry *dentry)
4291da177e4SLinus Torvalds {
430dea655c2SDavid Howells 	struct inode *inode = d_inode(dentry);
4311da177e4SLinus Torvalds 
432078cd827SDeepa Dinamani 	inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
4339a53c3a7SDave Hansen 	drop_nlink(inode);
4341da177e4SLinus Torvalds 	dput(dentry);
4351da177e4SLinus Torvalds 	return 0;
4361da177e4SLinus Torvalds }
43712f38872SAl Viro EXPORT_SYMBOL(simple_unlink);
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds int simple_rmdir(struct inode *dir, struct dentry *dentry)
4401da177e4SLinus Torvalds {
4411da177e4SLinus Torvalds 	if (!simple_empty(dentry))
4421da177e4SLinus Torvalds 		return -ENOTEMPTY;
4431da177e4SLinus Torvalds 
444dea655c2SDavid Howells 	drop_nlink(d_inode(dentry));
4451da177e4SLinus Torvalds 	simple_unlink(dir, dentry);
4469a53c3a7SDave Hansen 	drop_nlink(dir);
4471da177e4SLinus Torvalds 	return 0;
4481da177e4SLinus Torvalds }
44912f38872SAl Viro EXPORT_SYMBOL(simple_rmdir);
4501da177e4SLinus Torvalds 
4516429e463SLorenz Bauer int simple_rename_exchange(struct inode *old_dir, struct dentry *old_dentry,
4526429e463SLorenz Bauer 			   struct inode *new_dir, struct dentry *new_dentry)
4536429e463SLorenz Bauer {
4546429e463SLorenz Bauer 	bool old_is_dir = d_is_dir(old_dentry);
4556429e463SLorenz Bauer 	bool new_is_dir = d_is_dir(new_dentry);
4566429e463SLorenz Bauer 
4576429e463SLorenz Bauer 	if (old_dir != new_dir && old_is_dir != new_is_dir) {
4586429e463SLorenz Bauer 		if (old_is_dir) {
4596429e463SLorenz Bauer 			drop_nlink(old_dir);
4606429e463SLorenz Bauer 			inc_nlink(new_dir);
4616429e463SLorenz Bauer 		} else {
4626429e463SLorenz Bauer 			drop_nlink(new_dir);
4636429e463SLorenz Bauer 			inc_nlink(old_dir);
4646429e463SLorenz Bauer 		}
4656429e463SLorenz Bauer 	}
4666429e463SLorenz Bauer 	old_dir->i_ctime = old_dir->i_mtime =
4676429e463SLorenz Bauer 	new_dir->i_ctime = new_dir->i_mtime =
4686429e463SLorenz Bauer 	d_inode(old_dentry)->i_ctime =
4696429e463SLorenz Bauer 	d_inode(new_dentry)->i_ctime = current_time(old_dir);
4706429e463SLorenz Bauer 
4716429e463SLorenz Bauer 	return 0;
4726429e463SLorenz Bauer }
4736429e463SLorenz Bauer EXPORT_SYMBOL_GPL(simple_rename_exchange);
4746429e463SLorenz Bauer 
475549c7297SChristian Brauner int simple_rename(struct user_namespace *mnt_userns, struct inode *old_dir,
476549c7297SChristian Brauner 		  struct dentry *old_dentry, struct inode *new_dir,
477549c7297SChristian Brauner 		  struct dentry *new_dentry, unsigned int flags)
4781da177e4SLinus Torvalds {
479dea655c2SDavid Howells 	struct inode *inode = d_inode(old_dentry);
480e36cb0b8SDavid Howells 	int they_are_dirs = d_is_dir(old_dentry);
4811da177e4SLinus Torvalds 
482*3871cb8cSLorenz Bauer 	if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
483e0e0be8aSMiklos Szeredi 		return -EINVAL;
484e0e0be8aSMiklos Szeredi 
485*3871cb8cSLorenz Bauer 	if (flags & RENAME_EXCHANGE)
486*3871cb8cSLorenz Bauer 		return simple_rename_exchange(old_dir, old_dentry, new_dir, new_dentry);
487*3871cb8cSLorenz Bauer 
4881da177e4SLinus Torvalds 	if (!simple_empty(new_dentry))
4891da177e4SLinus Torvalds 		return -ENOTEMPTY;
4901da177e4SLinus Torvalds 
491dea655c2SDavid Howells 	if (d_really_is_positive(new_dentry)) {
4921da177e4SLinus Torvalds 		simple_unlink(new_dir, new_dentry);
493841590ceSAl Viro 		if (they_are_dirs) {
494dea655c2SDavid Howells 			drop_nlink(d_inode(new_dentry));
4959a53c3a7SDave Hansen 			drop_nlink(old_dir);
496841590ceSAl Viro 		}
4971da177e4SLinus Torvalds 	} else if (they_are_dirs) {
4989a53c3a7SDave Hansen 		drop_nlink(old_dir);
499d8c76e6fSDave Hansen 		inc_nlink(new_dir);
5001da177e4SLinus Torvalds 	}
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds 	old_dir->i_ctime = old_dir->i_mtime = new_dir->i_ctime =
503078cd827SDeepa Dinamani 		new_dir->i_mtime = inode->i_ctime = current_time(old_dir);
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds 	return 0;
5061da177e4SLinus Torvalds }
50712f38872SAl Viro EXPORT_SYMBOL(simple_rename);
5081da177e4SLinus Torvalds 
5097bb46a67Snpiggin@suse.de /**
510eef2380cSChristoph Hellwig  * simple_setattr - setattr for simple filesystem
51159347d99SRandy Dunlap  * @mnt_userns: user namespace of the target mount
5127bb46a67Snpiggin@suse.de  * @dentry: dentry
5137bb46a67Snpiggin@suse.de  * @iattr: iattr structure
5147bb46a67Snpiggin@suse.de  *
5157bb46a67Snpiggin@suse.de  * Returns 0 on success, -error on failure.
5167bb46a67Snpiggin@suse.de  *
517eef2380cSChristoph Hellwig  * simple_setattr is a simple ->setattr implementation without a proper
518eef2380cSChristoph Hellwig  * implementation of size changes.
519eef2380cSChristoph Hellwig  *
520eef2380cSChristoph Hellwig  * It can either be used for in-memory filesystems or special files
521eef2380cSChristoph Hellwig  * on simple regular filesystems.  Anything that needs to change on-disk
522eef2380cSChristoph Hellwig  * or wire state on size changes needs its own setattr method.
5237bb46a67Snpiggin@suse.de  */
524549c7297SChristian Brauner int simple_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
525549c7297SChristian Brauner 		   struct iattr *iattr)
5267bb46a67Snpiggin@suse.de {
527dea655c2SDavid Howells 	struct inode *inode = d_inode(dentry);
5287bb46a67Snpiggin@suse.de 	int error;
5297bb46a67Snpiggin@suse.de 
530549c7297SChristian Brauner 	error = setattr_prepare(mnt_userns, dentry, iattr);
5317bb46a67Snpiggin@suse.de 	if (error)
5327bb46a67Snpiggin@suse.de 		return error;
5337bb46a67Snpiggin@suse.de 
5342c27c65eSChristoph Hellwig 	if (iattr->ia_valid & ATTR_SIZE)
5352c27c65eSChristoph Hellwig 		truncate_setsize(inode, iattr->ia_size);
536549c7297SChristian Brauner 	setattr_copy(mnt_userns, inode, iattr);
537eef2380cSChristoph Hellwig 	mark_inode_dirty(inode);
538eef2380cSChristoph Hellwig 	return 0;
5397bb46a67Snpiggin@suse.de }
5407bb46a67Snpiggin@suse.de EXPORT_SYMBOL(simple_setattr);
5417bb46a67Snpiggin@suse.de 
542c1e3dbe9SChristoph Hellwig static int simple_readpage(struct file *file, struct page *page)
5431da177e4SLinus Torvalds {
544c0d92cbcSPekka J Enberg 	clear_highpage(page);
5451da177e4SLinus Torvalds 	flush_dcache_page(page);
5461da177e4SLinus Torvalds 	SetPageUptodate(page);
5471da177e4SLinus Torvalds 	unlock_page(page);
5481da177e4SLinus Torvalds 	return 0;
5491da177e4SLinus Torvalds }
5501da177e4SLinus Torvalds 
551afddba49SNick Piggin int simple_write_begin(struct file *file, struct address_space *mapping,
552afddba49SNick Piggin 			loff_t pos, unsigned len, unsigned flags,
553afddba49SNick Piggin 			struct page **pagep, void **fsdata)
554afddba49SNick Piggin {
555afddba49SNick Piggin 	struct page *page;
556afddba49SNick Piggin 	pgoff_t index;
557afddba49SNick Piggin 
55809cbfeafSKirill A. Shutemov 	index = pos >> PAGE_SHIFT;
559afddba49SNick Piggin 
56054566b2cSNick Piggin 	page = grab_cache_page_write_begin(mapping, index, flags);
561afddba49SNick Piggin 	if (!page)
562afddba49SNick Piggin 		return -ENOMEM;
563afddba49SNick Piggin 
564afddba49SNick Piggin 	*pagep = page;
565afddba49SNick Piggin 
56609cbfeafSKirill A. Shutemov 	if (!PageUptodate(page) && (len != PAGE_SIZE)) {
56709cbfeafSKirill A. Shutemov 		unsigned from = pos & (PAGE_SIZE - 1);
568193cf4b9SBoaz Harrosh 
56909cbfeafSKirill A. Shutemov 		zero_user_segments(page, 0, from, from + len, PAGE_SIZE);
570193cf4b9SBoaz Harrosh 	}
571193cf4b9SBoaz Harrosh 	return 0;
572afddba49SNick Piggin }
57312f38872SAl Viro EXPORT_SYMBOL(simple_write_begin);
574afddba49SNick Piggin 
575ad2a722fSBoaz Harrosh /**
576ad2a722fSBoaz Harrosh  * simple_write_end - .write_end helper for non-block-device FSes
5778e88bfbaSRandy Dunlap  * @file: See .write_end of address_space_operations
578ad2a722fSBoaz Harrosh  * @mapping: 		"
579ad2a722fSBoaz Harrosh  * @pos: 		"
580ad2a722fSBoaz Harrosh  * @len: 		"
581ad2a722fSBoaz Harrosh  * @copied: 		"
582ad2a722fSBoaz Harrosh  * @page: 		"
583ad2a722fSBoaz Harrosh  * @fsdata: 		"
584ad2a722fSBoaz Harrosh  *
585ad2a722fSBoaz Harrosh  * simple_write_end does the minimum needed for updating a page after writing is
586ad2a722fSBoaz Harrosh  * done. It has the same API signature as the .write_end of
587ad2a722fSBoaz Harrosh  * address_space_operations vector. So it can just be set onto .write_end for
588ad2a722fSBoaz Harrosh  * FSes that don't need any other processing. i_mutex is assumed to be held.
589ad2a722fSBoaz Harrosh  * Block based filesystems should use generic_write_end().
590ad2a722fSBoaz Harrosh  * NOTE: Even though i_size might get updated by this function, mark_inode_dirty
591ad2a722fSBoaz Harrosh  * is not called, so a filesystem that actually does store data in .write_inode
592ad2a722fSBoaz Harrosh  * should extend on what's done here with a call to mark_inode_dirty() in the
593ad2a722fSBoaz Harrosh  * case that i_size has changed.
59404fff641SAl Viro  *
59504fff641SAl Viro  * Use *ONLY* with simple_readpage()
596ad2a722fSBoaz Harrosh  */
597c1e3dbe9SChristoph Hellwig static int simple_write_end(struct file *file, struct address_space *mapping,
598ad2a722fSBoaz Harrosh 			loff_t pos, unsigned len, unsigned copied,
599ad2a722fSBoaz Harrosh 			struct page *page, void *fsdata)
6001da177e4SLinus Torvalds {
6011da177e4SLinus Torvalds 	struct inode *inode = page->mapping->host;
602ad2a722fSBoaz Harrosh 	loff_t last_pos = pos + copied;
603ad2a722fSBoaz Harrosh 
604ad2a722fSBoaz Harrosh 	/* zero the stale part of the page if we did a short copy */
60504fff641SAl Viro 	if (!PageUptodate(page)) {
606ad2a722fSBoaz Harrosh 		if (copied < len) {
60709cbfeafSKirill A. Shutemov 			unsigned from = pos & (PAGE_SIZE - 1);
608ad2a722fSBoaz Harrosh 
609ad2a722fSBoaz Harrosh 			zero_user(page, from + copied, len - copied);
610ad2a722fSBoaz Harrosh 		}
611955eff5aSNick Piggin 		SetPageUptodate(page);
61204fff641SAl Viro 	}
6131da177e4SLinus Torvalds 	/*
6141da177e4SLinus Torvalds 	 * No need to use i_size_read() here, the i_size
6151b1dcc1bSJes Sorensen 	 * cannot change under us because we hold the i_mutex.
6161da177e4SLinus Torvalds 	 */
617ad2a722fSBoaz Harrosh 	if (last_pos > inode->i_size)
618ad2a722fSBoaz Harrosh 		i_size_write(inode, last_pos);
619ad2a722fSBoaz Harrosh 
6201da177e4SLinus Torvalds 	set_page_dirty(page);
621afddba49SNick Piggin 	unlock_page(page);
62209cbfeafSKirill A. Shutemov 	put_page(page);
623afddba49SNick Piggin 
624afddba49SNick Piggin 	return copied;
625afddba49SNick Piggin }
626c1e3dbe9SChristoph Hellwig 
627c1e3dbe9SChristoph Hellwig /*
628c1e3dbe9SChristoph Hellwig  * Provides ramfs-style behavior: data in the pagecache, but no writeback.
629c1e3dbe9SChristoph Hellwig  */
630c1e3dbe9SChristoph Hellwig const struct address_space_operations ram_aops = {
631c1e3dbe9SChristoph Hellwig 	.readpage	= simple_readpage,
632c1e3dbe9SChristoph Hellwig 	.write_begin	= simple_write_begin,
633c1e3dbe9SChristoph Hellwig 	.write_end	= simple_write_end,
634c1e3dbe9SChristoph Hellwig 	.set_page_dirty	= __set_page_dirty_no_writeback,
635c1e3dbe9SChristoph Hellwig };
636c1e3dbe9SChristoph Hellwig EXPORT_SYMBOL(ram_aops);
637afddba49SNick Piggin 
6381a1c9bb4SJeff Layton /*
6391a1c9bb4SJeff Layton  * the inodes created here are not hashed. If you use iunique to generate
6401a1c9bb4SJeff Layton  * unique inode values later for this filesystem, then you must take care
6411a1c9bb4SJeff Layton  * to pass it an appropriate max_reserved value to avoid collisions.
6421a1c9bb4SJeff Layton  */
6437d683a09SRoberto Sassu int simple_fill_super(struct super_block *s, unsigned long magic,
644cda37124SEric Biggers 		      const struct tree_descr *files)
6451da177e4SLinus Torvalds {
6461da177e4SLinus Torvalds 	struct inode *inode;
6471da177e4SLinus Torvalds 	struct dentry *root;
6481da177e4SLinus Torvalds 	struct dentry *dentry;
6491da177e4SLinus Torvalds 	int i;
6501da177e4SLinus Torvalds 
65109cbfeafSKirill A. Shutemov 	s->s_blocksize = PAGE_SIZE;
65209cbfeafSKirill A. Shutemov 	s->s_blocksize_bits = PAGE_SHIFT;
6531da177e4SLinus Torvalds 	s->s_magic = magic;
654759b9775SHugh Dickins 	s->s_op = &simple_super_operations;
6551da177e4SLinus Torvalds 	s->s_time_gran = 1;
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds 	inode = new_inode(s);
6581da177e4SLinus Torvalds 	if (!inode)
6591da177e4SLinus Torvalds 		return -ENOMEM;
6601a1c9bb4SJeff Layton 	/*
6611a1c9bb4SJeff Layton 	 * because the root inode is 1, the files array must not contain an
6621a1c9bb4SJeff Layton 	 * entry at index 1
6631a1c9bb4SJeff Layton 	 */
6641a1c9bb4SJeff Layton 	inode->i_ino = 1;
6651da177e4SLinus Torvalds 	inode->i_mode = S_IFDIR | 0755;
666078cd827SDeepa Dinamani 	inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
6671da177e4SLinus Torvalds 	inode->i_op = &simple_dir_inode_operations;
6681da177e4SLinus Torvalds 	inode->i_fop = &simple_dir_operations;
669bfe86848SMiklos Szeredi 	set_nlink(inode, 2);
67048fde701SAl Viro 	root = d_make_root(inode);
67148fde701SAl Viro 	if (!root)
6721da177e4SLinus Torvalds 		return -ENOMEM;
6731da177e4SLinus Torvalds 	for (i = 0; !files->name || files->name[0]; i++, files++) {
6741da177e4SLinus Torvalds 		if (!files->name)
6751da177e4SLinus Torvalds 			continue;
6761a1c9bb4SJeff Layton 
6771a1c9bb4SJeff Layton 		/* warn if it tries to conflict with the root inode */
6781a1c9bb4SJeff Layton 		if (unlikely(i == 1))
6791a1c9bb4SJeff Layton 			printk(KERN_WARNING "%s: %s passed in a files array"
6801a1c9bb4SJeff Layton 				"with an index of 1!\n", __func__,
6811a1c9bb4SJeff Layton 				s->s_type->name);
6821a1c9bb4SJeff Layton 
6831da177e4SLinus Torvalds 		dentry = d_alloc_name(root, files->name);
6841da177e4SLinus Torvalds 		if (!dentry)
6851da177e4SLinus Torvalds 			goto out;
6861da177e4SLinus Torvalds 		inode = new_inode(s);
68732096ea1SKonstantin Khlebnikov 		if (!inode) {
68832096ea1SKonstantin Khlebnikov 			dput(dentry);
6891da177e4SLinus Torvalds 			goto out;
69032096ea1SKonstantin Khlebnikov 		}
6911da177e4SLinus Torvalds 		inode->i_mode = S_IFREG | files->mode;
692078cd827SDeepa Dinamani 		inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
6931da177e4SLinus Torvalds 		inode->i_fop = files->ops;
6941da177e4SLinus Torvalds 		inode->i_ino = i;
6951da177e4SLinus Torvalds 		d_add(dentry, inode);
6961da177e4SLinus Torvalds 	}
6971da177e4SLinus Torvalds 	s->s_root = root;
6981da177e4SLinus Torvalds 	return 0;
6991da177e4SLinus Torvalds out:
7001da177e4SLinus Torvalds 	d_genocide(root);
701640946f2SAl Viro 	shrink_dcache_parent(root);
7021da177e4SLinus Torvalds 	dput(root);
7031da177e4SLinus Torvalds 	return -ENOMEM;
7041da177e4SLinus Torvalds }
70512f38872SAl Viro EXPORT_SYMBOL(simple_fill_super);
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds static DEFINE_SPINLOCK(pin_fs_lock);
7081da177e4SLinus Torvalds 
7091f5ce9e9STrond Myklebust int simple_pin_fs(struct file_system_type *type, struct vfsmount **mount, int *count)
7101da177e4SLinus Torvalds {
7111da177e4SLinus Torvalds 	struct vfsmount *mnt = NULL;
7121da177e4SLinus Torvalds 	spin_lock(&pin_fs_lock);
7131da177e4SLinus Torvalds 	if (unlikely(!*mount)) {
7141da177e4SLinus Torvalds 		spin_unlock(&pin_fs_lock);
7151751e8a6SLinus Torvalds 		mnt = vfs_kern_mount(type, SB_KERNMOUNT, type->name, NULL);
7161da177e4SLinus Torvalds 		if (IS_ERR(mnt))
7171da177e4SLinus Torvalds 			return PTR_ERR(mnt);
7181da177e4SLinus Torvalds 		spin_lock(&pin_fs_lock);
7191da177e4SLinus Torvalds 		if (!*mount)
7201da177e4SLinus Torvalds 			*mount = mnt;
7211da177e4SLinus Torvalds 	}
7221da177e4SLinus Torvalds 	mntget(*mount);
7231da177e4SLinus Torvalds 	++*count;
7241da177e4SLinus Torvalds 	spin_unlock(&pin_fs_lock);
7251da177e4SLinus Torvalds 	mntput(mnt);
7261da177e4SLinus Torvalds 	return 0;
7271da177e4SLinus Torvalds }
72812f38872SAl Viro EXPORT_SYMBOL(simple_pin_fs);
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds void simple_release_fs(struct vfsmount **mount, int *count)
7311da177e4SLinus Torvalds {
7321da177e4SLinus Torvalds 	struct vfsmount *mnt;
7331da177e4SLinus Torvalds 	spin_lock(&pin_fs_lock);
7341da177e4SLinus Torvalds 	mnt = *mount;
7351da177e4SLinus Torvalds 	if (!--*count)
7361da177e4SLinus Torvalds 		*mount = NULL;
7371da177e4SLinus Torvalds 	spin_unlock(&pin_fs_lock);
7381da177e4SLinus Torvalds 	mntput(mnt);
7391da177e4SLinus Torvalds }
74012f38872SAl Viro EXPORT_SYMBOL(simple_release_fs);
7411da177e4SLinus Torvalds 
7426d1029b5SAkinobu Mita /**
7436d1029b5SAkinobu Mita  * simple_read_from_buffer - copy data from the buffer to user space
7446d1029b5SAkinobu Mita  * @to: the user space buffer to read to
7456d1029b5SAkinobu Mita  * @count: the maximum number of bytes to read
7466d1029b5SAkinobu Mita  * @ppos: the current position in the buffer
7476d1029b5SAkinobu Mita  * @from: the buffer to read from
7486d1029b5SAkinobu Mita  * @available: the size of the buffer
7496d1029b5SAkinobu Mita  *
7506d1029b5SAkinobu Mita  * The simple_read_from_buffer() function reads up to @count bytes from the
7516d1029b5SAkinobu Mita  * buffer @from at offset @ppos into the user space address starting at @to.
7526d1029b5SAkinobu Mita  *
7536d1029b5SAkinobu Mita  * On success, the number of bytes read is returned and the offset @ppos is
7546d1029b5SAkinobu Mita  * advanced by this number, or negative value is returned on error.
7556d1029b5SAkinobu Mita  **/
7561da177e4SLinus Torvalds ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos,
7571da177e4SLinus Torvalds 				const void *from, size_t available)
7581da177e4SLinus Torvalds {
7591da177e4SLinus Torvalds 	loff_t pos = *ppos;
76014be2746SSteven Rostedt 	size_t ret;
76114be2746SSteven Rostedt 
7621da177e4SLinus Torvalds 	if (pos < 0)
7631da177e4SLinus Torvalds 		return -EINVAL;
76414be2746SSteven Rostedt 	if (pos >= available || !count)
7651da177e4SLinus Torvalds 		return 0;
7661da177e4SLinus Torvalds 	if (count > available - pos)
7671da177e4SLinus Torvalds 		count = available - pos;
76814be2746SSteven Rostedt 	ret = copy_to_user(to, from + pos, count);
76914be2746SSteven Rostedt 	if (ret == count)
7701da177e4SLinus Torvalds 		return -EFAULT;
77114be2746SSteven Rostedt 	count -= ret;
7721da177e4SLinus Torvalds 	*ppos = pos + count;
7731da177e4SLinus Torvalds 	return count;
7741da177e4SLinus Torvalds }
77512f38872SAl Viro EXPORT_SYMBOL(simple_read_from_buffer);
7761da177e4SLinus Torvalds 
7776d1029b5SAkinobu Mita /**
7786a727b43SJiri Slaby  * simple_write_to_buffer - copy data from user space to the buffer
7796a727b43SJiri Slaby  * @to: the buffer to write to
7806a727b43SJiri Slaby  * @available: the size of the buffer
7816a727b43SJiri Slaby  * @ppos: the current position in the buffer
7826a727b43SJiri Slaby  * @from: the user space buffer to read from
7836a727b43SJiri Slaby  * @count: the maximum number of bytes to read
7846a727b43SJiri Slaby  *
7856a727b43SJiri Slaby  * The simple_write_to_buffer() function reads up to @count bytes from the user
7866a727b43SJiri Slaby  * space address starting at @from into the buffer @to at offset @ppos.
7876a727b43SJiri Slaby  *
7886a727b43SJiri Slaby  * On success, the number of bytes written is returned and the offset @ppos is
7896a727b43SJiri Slaby  * advanced by this number, or negative value is returned on error.
7906a727b43SJiri Slaby  **/
7916a727b43SJiri Slaby ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos,
7926a727b43SJiri Slaby 		const void __user *from, size_t count)
7936a727b43SJiri Slaby {
7946a727b43SJiri Slaby 	loff_t pos = *ppos;
7956a727b43SJiri Slaby 	size_t res;
7966a727b43SJiri Slaby 
7976a727b43SJiri Slaby 	if (pos < 0)
7986a727b43SJiri Slaby 		return -EINVAL;
7996a727b43SJiri Slaby 	if (pos >= available || !count)
8006a727b43SJiri Slaby 		return 0;
8016a727b43SJiri Slaby 	if (count > available - pos)
8026a727b43SJiri Slaby 		count = available - pos;
8036a727b43SJiri Slaby 	res = copy_from_user(to + pos, from, count);
8046a727b43SJiri Slaby 	if (res == count)
8056a727b43SJiri Slaby 		return -EFAULT;
8066a727b43SJiri Slaby 	count -= res;
8076a727b43SJiri Slaby 	*ppos = pos + count;
8086a727b43SJiri Slaby 	return count;
8096a727b43SJiri Slaby }
81012f38872SAl Viro EXPORT_SYMBOL(simple_write_to_buffer);
8116a727b43SJiri Slaby 
8126a727b43SJiri Slaby /**
8136d1029b5SAkinobu Mita  * memory_read_from_buffer - copy data from the buffer
8146d1029b5SAkinobu Mita  * @to: the kernel space buffer to read to
8156d1029b5SAkinobu Mita  * @count: the maximum number of bytes to read
8166d1029b5SAkinobu Mita  * @ppos: the current position in the buffer
8176d1029b5SAkinobu Mita  * @from: the buffer to read from
8186d1029b5SAkinobu Mita  * @available: the size of the buffer
8196d1029b5SAkinobu Mita  *
8206d1029b5SAkinobu Mita  * The memory_read_from_buffer() function reads up to @count bytes from the
8216d1029b5SAkinobu Mita  * buffer @from at offset @ppos into the kernel space address starting at @to.
8226d1029b5SAkinobu Mita  *
8236d1029b5SAkinobu Mita  * On success, the number of bytes read is returned and the offset @ppos is
8246d1029b5SAkinobu Mita  * advanced by this number, or negative value is returned on error.
8256d1029b5SAkinobu Mita  **/
82693b07113SAkinobu Mita ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
82793b07113SAkinobu Mita 				const void *from, size_t available)
82893b07113SAkinobu Mita {
82993b07113SAkinobu Mita 	loff_t pos = *ppos;
83093b07113SAkinobu Mita 
83193b07113SAkinobu Mita 	if (pos < 0)
83293b07113SAkinobu Mita 		return -EINVAL;
83393b07113SAkinobu Mita 	if (pos >= available)
83493b07113SAkinobu Mita 		return 0;
83593b07113SAkinobu Mita 	if (count > available - pos)
83693b07113SAkinobu Mita 		count = available - pos;
83793b07113SAkinobu Mita 	memcpy(to, from + pos, count);
83893b07113SAkinobu Mita 	*ppos = pos + count;
83993b07113SAkinobu Mita 
84093b07113SAkinobu Mita 	return count;
84193b07113SAkinobu Mita }
84212f38872SAl Viro EXPORT_SYMBOL(memory_read_from_buffer);
84393b07113SAkinobu Mita 
8441da177e4SLinus Torvalds /*
8451da177e4SLinus Torvalds  * Transaction based IO.
8461da177e4SLinus Torvalds  * The file expects a single write which triggers the transaction, and then
8471da177e4SLinus Torvalds  * possibly a read which collects the result - which is stored in a
8481da177e4SLinus Torvalds  * file-local buffer.
8491da177e4SLinus Torvalds  */
85076791ab2SIngo Molnar 
85176791ab2SIngo Molnar void simple_transaction_set(struct file *file, size_t n)
85276791ab2SIngo Molnar {
85376791ab2SIngo Molnar 	struct simple_transaction_argresp *ar = file->private_data;
85476791ab2SIngo Molnar 
85576791ab2SIngo Molnar 	BUG_ON(n > SIMPLE_TRANSACTION_LIMIT);
85676791ab2SIngo Molnar 
85776791ab2SIngo Molnar 	/*
85876791ab2SIngo Molnar 	 * The barrier ensures that ar->size will really remain zero until
85976791ab2SIngo Molnar 	 * ar->data is ready for reading.
86076791ab2SIngo Molnar 	 */
86176791ab2SIngo Molnar 	smp_mb();
86276791ab2SIngo Molnar 	ar->size = n;
86376791ab2SIngo Molnar }
86412f38872SAl Viro EXPORT_SYMBOL(simple_transaction_set);
86576791ab2SIngo Molnar 
8661da177e4SLinus Torvalds char *simple_transaction_get(struct file *file, const char __user *buf, size_t size)
8671da177e4SLinus Torvalds {
8681da177e4SLinus Torvalds 	struct simple_transaction_argresp *ar;
8691da177e4SLinus Torvalds 	static DEFINE_SPINLOCK(simple_transaction_lock);
8701da177e4SLinus Torvalds 
8711da177e4SLinus Torvalds 	if (size > SIMPLE_TRANSACTION_LIMIT - 1)
8721da177e4SLinus Torvalds 		return ERR_PTR(-EFBIG);
8731da177e4SLinus Torvalds 
8741da177e4SLinus Torvalds 	ar = (struct simple_transaction_argresp *)get_zeroed_page(GFP_KERNEL);
8751da177e4SLinus Torvalds 	if (!ar)
8761da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
8771da177e4SLinus Torvalds 
8781da177e4SLinus Torvalds 	spin_lock(&simple_transaction_lock);
8791da177e4SLinus Torvalds 
8801da177e4SLinus Torvalds 	/* only one write allowed per open */
8811da177e4SLinus Torvalds 	if (file->private_data) {
8821da177e4SLinus Torvalds 		spin_unlock(&simple_transaction_lock);
8831da177e4SLinus Torvalds 		free_page((unsigned long)ar);
8841da177e4SLinus Torvalds 		return ERR_PTR(-EBUSY);
8851da177e4SLinus Torvalds 	}
8861da177e4SLinus Torvalds 
8871da177e4SLinus Torvalds 	file->private_data = ar;
8881da177e4SLinus Torvalds 
8891da177e4SLinus Torvalds 	spin_unlock(&simple_transaction_lock);
8901da177e4SLinus Torvalds 
8911da177e4SLinus Torvalds 	if (copy_from_user(ar->data, buf, size))
8921da177e4SLinus Torvalds 		return ERR_PTR(-EFAULT);
8931da177e4SLinus Torvalds 
8941da177e4SLinus Torvalds 	return ar->data;
8951da177e4SLinus Torvalds }
89612f38872SAl Viro EXPORT_SYMBOL(simple_transaction_get);
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds ssize_t simple_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
8991da177e4SLinus Torvalds {
9001da177e4SLinus Torvalds 	struct simple_transaction_argresp *ar = file->private_data;
9011da177e4SLinus Torvalds 
9021da177e4SLinus Torvalds 	if (!ar)
9031da177e4SLinus Torvalds 		return 0;
9041da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, size, pos, ar->data, ar->size);
9051da177e4SLinus Torvalds }
90612f38872SAl Viro EXPORT_SYMBOL(simple_transaction_read);
9071da177e4SLinus Torvalds 
9081da177e4SLinus Torvalds int simple_transaction_release(struct inode *inode, struct file *file)
9091da177e4SLinus Torvalds {
9101da177e4SLinus Torvalds 	free_page((unsigned long)file->private_data);
9111da177e4SLinus Torvalds 	return 0;
9121da177e4SLinus Torvalds }
91312f38872SAl Viro EXPORT_SYMBOL(simple_transaction_release);
9141da177e4SLinus Torvalds 
915acaefc25SArnd Bergmann /* Simple attribute files */
916acaefc25SArnd Bergmann 
917acaefc25SArnd Bergmann struct simple_attr {
9188b88b099SChristoph Hellwig 	int (*get)(void *, u64 *);
9198b88b099SChristoph Hellwig 	int (*set)(void *, u64);
920acaefc25SArnd Bergmann 	char get_buf[24];	/* enough to store a u64 and "\n\0" */
921acaefc25SArnd Bergmann 	char set_buf[24];
922acaefc25SArnd Bergmann 	void *data;
923acaefc25SArnd Bergmann 	const char *fmt;	/* format for read operation */
9247cf34c76SIngo Molnar 	struct mutex mutex;	/* protects access to these buffers */
925acaefc25SArnd Bergmann };
926acaefc25SArnd Bergmann 
927acaefc25SArnd Bergmann /* simple_attr_open is called by an actual attribute open file operation
928acaefc25SArnd Bergmann  * to set the attribute specific access operations. */
929acaefc25SArnd Bergmann int simple_attr_open(struct inode *inode, struct file *file,
9308b88b099SChristoph Hellwig 		     int (*get)(void *, u64 *), int (*set)(void *, u64),
931acaefc25SArnd Bergmann 		     const char *fmt)
932acaefc25SArnd Bergmann {
933acaefc25SArnd Bergmann 	struct simple_attr *attr;
934acaefc25SArnd Bergmann 
935a65cab7dSEric Biggers 	attr = kzalloc(sizeof(*attr), GFP_KERNEL);
936acaefc25SArnd Bergmann 	if (!attr)
937acaefc25SArnd Bergmann 		return -ENOMEM;
938acaefc25SArnd Bergmann 
939acaefc25SArnd Bergmann 	attr->get = get;
940acaefc25SArnd Bergmann 	attr->set = set;
9418e18e294STheodore Ts'o 	attr->data = inode->i_private;
942acaefc25SArnd Bergmann 	attr->fmt = fmt;
9437cf34c76SIngo Molnar 	mutex_init(&attr->mutex);
944acaefc25SArnd Bergmann 
945acaefc25SArnd Bergmann 	file->private_data = attr;
946acaefc25SArnd Bergmann 
947acaefc25SArnd Bergmann 	return nonseekable_open(inode, file);
948acaefc25SArnd Bergmann }
94912f38872SAl Viro EXPORT_SYMBOL_GPL(simple_attr_open);
950acaefc25SArnd Bergmann 
95174bedc4dSChristoph Hellwig int simple_attr_release(struct inode *inode, struct file *file)
952acaefc25SArnd Bergmann {
953acaefc25SArnd Bergmann 	kfree(file->private_data);
954acaefc25SArnd Bergmann 	return 0;
955acaefc25SArnd Bergmann }
95612f38872SAl Viro EXPORT_SYMBOL_GPL(simple_attr_release);	/* GPL-only?  This?  Really? */
957acaefc25SArnd Bergmann 
958acaefc25SArnd Bergmann /* read from the buffer that is filled with the get function */
959acaefc25SArnd Bergmann ssize_t simple_attr_read(struct file *file, char __user *buf,
960acaefc25SArnd Bergmann 			 size_t len, loff_t *ppos)
961acaefc25SArnd Bergmann {
962acaefc25SArnd Bergmann 	struct simple_attr *attr;
963acaefc25SArnd Bergmann 	size_t size;
964acaefc25SArnd Bergmann 	ssize_t ret;
965acaefc25SArnd Bergmann 
966acaefc25SArnd Bergmann 	attr = file->private_data;
967acaefc25SArnd Bergmann 
968acaefc25SArnd Bergmann 	if (!attr->get)
969acaefc25SArnd Bergmann 		return -EACCES;
970acaefc25SArnd Bergmann 
9719261303aSChristoph Hellwig 	ret = mutex_lock_interruptible(&attr->mutex);
9729261303aSChristoph Hellwig 	if (ret)
9739261303aSChristoph Hellwig 		return ret;
9749261303aSChristoph Hellwig 
975a65cab7dSEric Biggers 	if (*ppos && attr->get_buf[0]) {
976a65cab7dSEric Biggers 		/* continued read */
977acaefc25SArnd Bergmann 		size = strlen(attr->get_buf);
978a65cab7dSEric Biggers 	} else {
979a65cab7dSEric Biggers 		/* first read */
9808b88b099SChristoph Hellwig 		u64 val;
9818b88b099SChristoph Hellwig 		ret = attr->get(attr->data, &val);
9828b88b099SChristoph Hellwig 		if (ret)
9838b88b099SChristoph Hellwig 			goto out;
9848b88b099SChristoph Hellwig 
985acaefc25SArnd Bergmann 		size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
9868b88b099SChristoph Hellwig 				 attr->fmt, (unsigned long long)val);
9878b88b099SChristoph Hellwig 	}
988acaefc25SArnd Bergmann 
989acaefc25SArnd Bergmann 	ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
9908b88b099SChristoph Hellwig out:
9917cf34c76SIngo Molnar 	mutex_unlock(&attr->mutex);
992acaefc25SArnd Bergmann 	return ret;
993acaefc25SArnd Bergmann }
99412f38872SAl Viro EXPORT_SYMBOL_GPL(simple_attr_read);
995acaefc25SArnd Bergmann 
996acaefc25SArnd Bergmann /* interpret the buffer as a number to call the set function with */
997acaefc25SArnd Bergmann ssize_t simple_attr_write(struct file *file, const char __user *buf,
998acaefc25SArnd Bergmann 			  size_t len, loff_t *ppos)
999acaefc25SArnd Bergmann {
1000acaefc25SArnd Bergmann 	struct simple_attr *attr;
1001488dac0cSYicong Yang 	unsigned long long val;
1002acaefc25SArnd Bergmann 	size_t size;
1003acaefc25SArnd Bergmann 	ssize_t ret;
1004acaefc25SArnd Bergmann 
1005acaefc25SArnd Bergmann 	attr = file->private_data;
1006acaefc25SArnd Bergmann 	if (!attr->set)
1007acaefc25SArnd Bergmann 		return -EACCES;
1008acaefc25SArnd Bergmann 
10099261303aSChristoph Hellwig 	ret = mutex_lock_interruptible(&attr->mutex);
10109261303aSChristoph Hellwig 	if (ret)
10119261303aSChristoph Hellwig 		return ret;
10129261303aSChristoph Hellwig 
1013acaefc25SArnd Bergmann 	ret = -EFAULT;
1014acaefc25SArnd Bergmann 	size = min(sizeof(attr->set_buf) - 1, len);
1015acaefc25SArnd Bergmann 	if (copy_from_user(attr->set_buf, buf, size))
1016acaefc25SArnd Bergmann 		goto out;
1017acaefc25SArnd Bergmann 
1018acaefc25SArnd Bergmann 	attr->set_buf[size] = '\0';
1019488dac0cSYicong Yang 	ret = kstrtoull(attr->set_buf, 0, &val);
1020488dac0cSYicong Yang 	if (ret)
1021488dac0cSYicong Yang 		goto out;
102205cc0ceeSWu Fengguang 	ret = attr->set(attr->data, val);
102305cc0ceeSWu Fengguang 	if (ret == 0)
102405cc0ceeSWu Fengguang 		ret = len; /* on success, claim we got the whole input */
1025acaefc25SArnd Bergmann out:
10267cf34c76SIngo Molnar 	mutex_unlock(&attr->mutex);
1027acaefc25SArnd Bergmann 	return ret;
1028acaefc25SArnd Bergmann }
102912f38872SAl Viro EXPORT_SYMBOL_GPL(simple_attr_write);
1030acaefc25SArnd Bergmann 
10312596110aSChristoph Hellwig /**
10322596110aSChristoph Hellwig  * generic_fh_to_dentry - generic helper for the fh_to_dentry export operation
10332596110aSChristoph Hellwig  * @sb:		filesystem to do the file handle conversion on
10342596110aSChristoph Hellwig  * @fid:	file handle to convert
10352596110aSChristoph Hellwig  * @fh_len:	length of the file handle in bytes
10362596110aSChristoph Hellwig  * @fh_type:	type of file handle
10372596110aSChristoph Hellwig  * @get_inode:	filesystem callback to retrieve inode
10382596110aSChristoph Hellwig  *
10392596110aSChristoph Hellwig  * This function decodes @fid as long as it has one of the well-known
10402596110aSChristoph Hellwig  * Linux filehandle types and calls @get_inode on it to retrieve the
10412596110aSChristoph Hellwig  * inode for the object specified in the file handle.
10422596110aSChristoph Hellwig  */
10432596110aSChristoph Hellwig struct dentry *generic_fh_to_dentry(struct super_block *sb, struct fid *fid,
10442596110aSChristoph Hellwig 		int fh_len, int fh_type, struct inode *(*get_inode)
10452596110aSChristoph Hellwig 			(struct super_block *sb, u64 ino, u32 gen))
10462596110aSChristoph Hellwig {
10472596110aSChristoph Hellwig 	struct inode *inode = NULL;
10482596110aSChristoph Hellwig 
10492596110aSChristoph Hellwig 	if (fh_len < 2)
10502596110aSChristoph Hellwig 		return NULL;
10512596110aSChristoph Hellwig 
10522596110aSChristoph Hellwig 	switch (fh_type) {
10532596110aSChristoph Hellwig 	case FILEID_INO32_GEN:
10542596110aSChristoph Hellwig 	case FILEID_INO32_GEN_PARENT:
10552596110aSChristoph Hellwig 		inode = get_inode(sb, fid->i32.ino, fid->i32.gen);
10562596110aSChristoph Hellwig 		break;
10572596110aSChristoph Hellwig 	}
10582596110aSChristoph Hellwig 
10594ea3ada2SChristoph Hellwig 	return d_obtain_alias(inode);
10602596110aSChristoph Hellwig }
10612596110aSChristoph Hellwig EXPORT_SYMBOL_GPL(generic_fh_to_dentry);
10622596110aSChristoph Hellwig 
10632596110aSChristoph Hellwig /**
1064ca186830SYanchuan Nian  * generic_fh_to_parent - generic helper for the fh_to_parent export operation
10652596110aSChristoph Hellwig  * @sb:		filesystem to do the file handle conversion on
10662596110aSChristoph Hellwig  * @fid:	file handle to convert
10672596110aSChristoph Hellwig  * @fh_len:	length of the file handle in bytes
10682596110aSChristoph Hellwig  * @fh_type:	type of file handle
10692596110aSChristoph Hellwig  * @get_inode:	filesystem callback to retrieve inode
10702596110aSChristoph Hellwig  *
10712596110aSChristoph Hellwig  * This function decodes @fid as long as it has one of the well-known
10722596110aSChristoph Hellwig  * Linux filehandle types and calls @get_inode on it to retrieve the
10732596110aSChristoph Hellwig  * inode for the _parent_ object specified in the file handle if it
10742596110aSChristoph Hellwig  * is specified in the file handle, or NULL otherwise.
10752596110aSChristoph Hellwig  */
10762596110aSChristoph Hellwig struct dentry *generic_fh_to_parent(struct super_block *sb, struct fid *fid,
10772596110aSChristoph Hellwig 		int fh_len, int fh_type, struct inode *(*get_inode)
10782596110aSChristoph Hellwig 			(struct super_block *sb, u64 ino, u32 gen))
10792596110aSChristoph Hellwig {
10802596110aSChristoph Hellwig 	struct inode *inode = NULL;
10812596110aSChristoph Hellwig 
10822596110aSChristoph Hellwig 	if (fh_len <= 2)
10832596110aSChristoph Hellwig 		return NULL;
10842596110aSChristoph Hellwig 
10852596110aSChristoph Hellwig 	switch (fh_type) {
10862596110aSChristoph Hellwig 	case FILEID_INO32_GEN_PARENT:
10872596110aSChristoph Hellwig 		inode = get_inode(sb, fid->i32.parent_ino,
10882596110aSChristoph Hellwig 				  (fh_len > 3 ? fid->i32.parent_gen : 0));
10892596110aSChristoph Hellwig 		break;
10902596110aSChristoph Hellwig 	}
10912596110aSChristoph Hellwig 
10924ea3ada2SChristoph Hellwig 	return d_obtain_alias(inode);
10932596110aSChristoph Hellwig }
10942596110aSChristoph Hellwig EXPORT_SYMBOL_GPL(generic_fh_to_parent);
10952596110aSChristoph Hellwig 
10961b061d92SChristoph Hellwig /**
1097ac13a829SFabian Frederick  * __generic_file_fsync - generic fsync implementation for simple filesystems
1098ac13a829SFabian Frederick  *
10991b061d92SChristoph Hellwig  * @file:	file to synchronize
1100ac13a829SFabian Frederick  * @start:	start offset in bytes
1101ac13a829SFabian Frederick  * @end:	end offset in bytes (inclusive)
11021b061d92SChristoph Hellwig  * @datasync:	only synchronize essential metadata if true
11031b061d92SChristoph Hellwig  *
11041b061d92SChristoph Hellwig  * This is a generic implementation of the fsync method for simple
11051b061d92SChristoph Hellwig  * filesystems which track all non-inode metadata in the buffers list
11061b061d92SChristoph Hellwig  * hanging off the address_space structure.
11071b061d92SChristoph Hellwig  */
1108ac13a829SFabian Frederick int __generic_file_fsync(struct file *file, loff_t start, loff_t end,
110902c24a82SJosef Bacik 				 int datasync)
1110d5aacad5SAl Viro {
11117ea80859SChristoph Hellwig 	struct inode *inode = file->f_mapping->host;
1112d5aacad5SAl Viro 	int err;
1113d5aacad5SAl Viro 	int ret;
1114d5aacad5SAl Viro 
1115383aa543SJeff Layton 	err = file_write_and_wait_range(file, start, end);
111602c24a82SJosef Bacik 	if (err)
111702c24a82SJosef Bacik 		return err;
111802c24a82SJosef Bacik 
11195955102cSAl Viro 	inode_lock(inode);
1120d5aacad5SAl Viro 	ret = sync_mapping_buffers(inode->i_mapping);
11210ae45f63STheodore Ts'o 	if (!(inode->i_state & I_DIRTY_ALL))
112202c24a82SJosef Bacik 		goto out;
1123d5aacad5SAl Viro 	if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
112402c24a82SJosef Bacik 		goto out;
1125d5aacad5SAl Viro 
1126c3765016SChristoph Hellwig 	err = sync_inode_metadata(inode, 1);
1127d5aacad5SAl Viro 	if (ret == 0)
1128d5aacad5SAl Viro 		ret = err;
1129ac13a829SFabian Frederick 
113002c24a82SJosef Bacik out:
11315955102cSAl Viro 	inode_unlock(inode);
1132383aa543SJeff Layton 	/* check and advance again to catch errors after syncing out buffers */
1133383aa543SJeff Layton 	err = file_check_and_advance_wb_err(file);
1134383aa543SJeff Layton 	if (ret == 0)
1135383aa543SJeff Layton 		ret = err;
1136383aa543SJeff Layton 	return ret;
1137d5aacad5SAl Viro }
1138ac13a829SFabian Frederick EXPORT_SYMBOL(__generic_file_fsync);
1139ac13a829SFabian Frederick 
1140ac13a829SFabian Frederick /**
1141ac13a829SFabian Frederick  * generic_file_fsync - generic fsync implementation for simple filesystems
1142ac13a829SFabian Frederick  *			with flush
1143ac13a829SFabian Frederick  * @file:	file to synchronize
1144ac13a829SFabian Frederick  * @start:	start offset in bytes
1145ac13a829SFabian Frederick  * @end:	end offset in bytes (inclusive)
1146ac13a829SFabian Frederick  * @datasync:	only synchronize essential metadata if true
1147ac13a829SFabian Frederick  *
1148ac13a829SFabian Frederick  */
1149ac13a829SFabian Frederick 
1150ac13a829SFabian Frederick int generic_file_fsync(struct file *file, loff_t start, loff_t end,
1151ac13a829SFabian Frederick 		       int datasync)
1152ac13a829SFabian Frederick {
1153ac13a829SFabian Frederick 	struct inode *inode = file->f_mapping->host;
1154ac13a829SFabian Frederick 	int err;
1155ac13a829SFabian Frederick 
1156ac13a829SFabian Frederick 	err = __generic_file_fsync(file, start, end, datasync);
1157ac13a829SFabian Frederick 	if (err)
1158ac13a829SFabian Frederick 		return err;
1159c6bf3f0eSChristoph Hellwig 	return blkdev_issue_flush(inode->i_sb->s_bdev);
1160ac13a829SFabian Frederick }
11611b061d92SChristoph Hellwig EXPORT_SYMBOL(generic_file_fsync);
11621b061d92SChristoph Hellwig 
116330ca22c7SPatrick J. LoPresti /**
116430ca22c7SPatrick J. LoPresti  * generic_check_addressable - Check addressability of file system
116530ca22c7SPatrick J. LoPresti  * @blocksize_bits:	log of file system block size
116630ca22c7SPatrick J. LoPresti  * @num_blocks:		number of blocks in file system
116730ca22c7SPatrick J. LoPresti  *
116830ca22c7SPatrick J. LoPresti  * Determine whether a file system with @num_blocks blocks (and a
116930ca22c7SPatrick J. LoPresti  * block size of 2**@blocksize_bits) is addressable by the sector_t
117030ca22c7SPatrick J. LoPresti  * and page cache of the system.  Return 0 if so and -EFBIG otherwise.
117130ca22c7SPatrick J. LoPresti  */
117230ca22c7SPatrick J. LoPresti int generic_check_addressable(unsigned blocksize_bits, u64 num_blocks)
117330ca22c7SPatrick J. LoPresti {
117430ca22c7SPatrick J. LoPresti 	u64 last_fs_block = num_blocks - 1;
1175a33f13efSJoel Becker 	u64 last_fs_page =
117609cbfeafSKirill A. Shutemov 		last_fs_block >> (PAGE_SHIFT - blocksize_bits);
117730ca22c7SPatrick J. LoPresti 
117830ca22c7SPatrick J. LoPresti 	if (unlikely(num_blocks == 0))
117930ca22c7SPatrick J. LoPresti 		return 0;
118030ca22c7SPatrick J. LoPresti 
118109cbfeafSKirill A. Shutemov 	if ((blocksize_bits < 9) || (blocksize_bits > PAGE_SHIFT))
118230ca22c7SPatrick J. LoPresti 		return -EINVAL;
118330ca22c7SPatrick J. LoPresti 
1184a33f13efSJoel Becker 	if ((last_fs_block > (sector_t)(~0ULL) >> (blocksize_bits - 9)) ||
1185a33f13efSJoel Becker 	    (last_fs_page > (pgoff_t)(~0ULL))) {
118630ca22c7SPatrick J. LoPresti 		return -EFBIG;
118730ca22c7SPatrick J. LoPresti 	}
118830ca22c7SPatrick J. LoPresti 	return 0;
118930ca22c7SPatrick J. LoPresti }
119030ca22c7SPatrick J. LoPresti EXPORT_SYMBOL(generic_check_addressable);
119130ca22c7SPatrick J. LoPresti 
11921b061d92SChristoph Hellwig /*
11931b061d92SChristoph Hellwig  * No-op implementation of ->fsync for in-memory filesystems.
11941b061d92SChristoph Hellwig  */
119502c24a82SJosef Bacik int noop_fsync(struct file *file, loff_t start, loff_t end, int datasync)
11961b061d92SChristoph Hellwig {
11971b061d92SChristoph Hellwig 	return 0;
11981b061d92SChristoph Hellwig }
11991b061d92SChristoph Hellwig EXPORT_SYMBOL(noop_fsync);
120087dc800bSAl Viro 
1201f44c7763SDan Williams void noop_invalidatepage(struct page *page, unsigned int offset,
1202f44c7763SDan Williams 		unsigned int length)
1203f44c7763SDan Williams {
1204f44c7763SDan Williams 	/*
1205f44c7763SDan Williams 	 * There is no page cache to invalidate in the dax case, however
1206f44c7763SDan Williams 	 * we need this callback defined to prevent falling back to
1207f44c7763SDan Williams 	 * block_invalidatepage() in do_invalidatepage().
1208f44c7763SDan Williams 	 */
1209f44c7763SDan Williams }
1210f44c7763SDan Williams EXPORT_SYMBOL_GPL(noop_invalidatepage);
1211f44c7763SDan Williams 
1212f44c7763SDan Williams ssize_t noop_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
1213f44c7763SDan Williams {
1214f44c7763SDan Williams 	/*
1215f44c7763SDan Williams 	 * iomap based filesystems support direct I/O without need for
1216f44c7763SDan Williams 	 * this callback. However, it still needs to be set in
1217f44c7763SDan Williams 	 * inode->a_ops so that open/fcntl know that direct I/O is
1218f44c7763SDan Williams 	 * generally supported.
1219f44c7763SDan Williams 	 */
1220f44c7763SDan Williams 	return -EINVAL;
1221f44c7763SDan Williams }
1222f44c7763SDan Williams EXPORT_SYMBOL_GPL(noop_direct_IO);
1223f44c7763SDan Williams 
1224fceef393SAl Viro /* Because kfree isn't assignment-compatible with void(void*) ;-/ */
1225fceef393SAl Viro void kfree_link(void *p)
122687dc800bSAl Viro {
1227fceef393SAl Viro 	kfree(p);
122887dc800bSAl Viro }
1229fceef393SAl Viro EXPORT_SYMBOL(kfree_link);
12306987843fSAl Viro 
12316987843fSAl Viro struct inode *alloc_anon_inode(struct super_block *s)
12326987843fSAl Viro {
12336987843fSAl Viro 	static const struct address_space_operations anon_aops = {
1234fc50eee3SMatthew Wilcox (Oracle) 		.set_page_dirty = __set_page_dirty_no_writeback,
12356987843fSAl Viro 	};
12366987843fSAl Viro 	struct inode *inode = new_inode_pseudo(s);
12376987843fSAl Viro 
12386987843fSAl Viro 	if (!inode)
12396987843fSAl Viro 		return ERR_PTR(-ENOMEM);
12406987843fSAl Viro 
12416987843fSAl Viro 	inode->i_ino = get_next_ino();
12426987843fSAl Viro 	inode->i_mapping->a_ops = &anon_aops;
12436987843fSAl Viro 
12446987843fSAl Viro 	/*
12456987843fSAl Viro 	 * Mark the inode dirty from the very beginning,
12466987843fSAl Viro 	 * that way it will never be moved to the dirty
12476987843fSAl Viro 	 * list because mark_inode_dirty() will think
12486987843fSAl Viro 	 * that it already _is_ on the dirty list.
12496987843fSAl Viro 	 */
12506987843fSAl Viro 	inode->i_state = I_DIRTY;
12516987843fSAl Viro 	inode->i_mode = S_IRUSR | S_IWUSR;
12526987843fSAl Viro 	inode->i_uid = current_fsuid();
12536987843fSAl Viro 	inode->i_gid = current_fsgid();
12546987843fSAl Viro 	inode->i_flags |= S_PRIVATE;
1255078cd827SDeepa Dinamani 	inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
12566987843fSAl Viro 	return inode;
12576987843fSAl Viro }
12586987843fSAl Viro EXPORT_SYMBOL(alloc_anon_inode);
12591c994a09SJeff Layton 
12601c994a09SJeff Layton /**
12611c994a09SJeff Layton  * simple_nosetlease - generic helper for prohibiting leases
12621c994a09SJeff Layton  * @filp: file pointer
12631c994a09SJeff Layton  * @arg: type of lease to obtain
12641c994a09SJeff Layton  * @flp: new lease supplied for insertion
1265e6f5c789SJeff Layton  * @priv: private data for lm_setup operation
12661c994a09SJeff Layton  *
12671c994a09SJeff Layton  * Generic helper for filesystems that do not wish to allow leases to be set.
12681c994a09SJeff Layton  * All arguments are ignored and it just returns -EINVAL.
12691c994a09SJeff Layton  */
12701c994a09SJeff Layton int
1271e6f5c789SJeff Layton simple_nosetlease(struct file *filp, long arg, struct file_lock **flp,
1272e6f5c789SJeff Layton 		  void **priv)
12731c994a09SJeff Layton {
12741c994a09SJeff Layton 	return -EINVAL;
12751c994a09SJeff Layton }
12761c994a09SJeff Layton EXPORT_SYMBOL(simple_nosetlease);
127761ba64fcSAl Viro 
12786ee9706aSEric Biggers /**
12796ee9706aSEric Biggers  * simple_get_link - generic helper to get the target of "fast" symlinks
12806ee9706aSEric Biggers  * @dentry: not used here
12816ee9706aSEric Biggers  * @inode: the symlink inode
12826ee9706aSEric Biggers  * @done: not used here
12836ee9706aSEric Biggers  *
12846ee9706aSEric Biggers  * Generic helper for filesystems to use for symlink inodes where a pointer to
12856ee9706aSEric Biggers  * the symlink target is stored in ->i_link.  NOTE: this isn't normally called,
12866ee9706aSEric Biggers  * since as an optimization the path lookup code uses any non-NULL ->i_link
12876ee9706aSEric Biggers  * directly, without calling ->get_link().  But ->get_link() still must be set,
12886ee9706aSEric Biggers  * to mark the inode_operations as being for a symlink.
12896ee9706aSEric Biggers  *
12906ee9706aSEric Biggers  * Return: the symlink target
12916ee9706aSEric Biggers  */
12926b255391SAl Viro const char *simple_get_link(struct dentry *dentry, struct inode *inode,
1293fceef393SAl Viro 			    struct delayed_call *done)
129461ba64fcSAl Viro {
12956b255391SAl Viro 	return inode->i_link;
129661ba64fcSAl Viro }
12976b255391SAl Viro EXPORT_SYMBOL(simple_get_link);
129861ba64fcSAl Viro 
129961ba64fcSAl Viro const struct inode_operations simple_symlink_inode_operations = {
13006b255391SAl Viro 	.get_link = simple_get_link,
130161ba64fcSAl Viro };
130261ba64fcSAl Viro EXPORT_SYMBOL(simple_symlink_inode_operations);
1303fbabfd0fSEric W. Biederman 
1304fbabfd0fSEric W. Biederman /*
1305fbabfd0fSEric W. Biederman  * Operations for a permanently empty directory.
1306fbabfd0fSEric W. Biederman  */
1307fbabfd0fSEric W. Biederman static struct dentry *empty_dir_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
1308fbabfd0fSEric W. Biederman {
1309fbabfd0fSEric W. Biederman 	return ERR_PTR(-ENOENT);
1310fbabfd0fSEric W. Biederman }
1311fbabfd0fSEric W. Biederman 
1312549c7297SChristian Brauner static int empty_dir_getattr(struct user_namespace *mnt_userns,
1313549c7297SChristian Brauner 			     const struct path *path, struct kstat *stat,
1314a528d35eSDavid Howells 			     u32 request_mask, unsigned int query_flags)
1315fbabfd0fSEric W. Biederman {
1316a528d35eSDavid Howells 	struct inode *inode = d_inode(path->dentry);
13170d56a451SChristian Brauner 	generic_fillattr(&init_user_ns, inode, stat);
1318fbabfd0fSEric W. Biederman 	return 0;
1319fbabfd0fSEric W. Biederman }
1320fbabfd0fSEric W. Biederman 
1321549c7297SChristian Brauner static int empty_dir_setattr(struct user_namespace *mnt_userns,
1322549c7297SChristian Brauner 			     struct dentry *dentry, struct iattr *attr)
1323fbabfd0fSEric W. Biederman {
1324fbabfd0fSEric W. Biederman 	return -EPERM;
1325fbabfd0fSEric W. Biederman }
1326fbabfd0fSEric W. Biederman 
1327fbabfd0fSEric W. Biederman static ssize_t empty_dir_listxattr(struct dentry *dentry, char *list, size_t size)
1328fbabfd0fSEric W. Biederman {
1329fbabfd0fSEric W. Biederman 	return -EOPNOTSUPP;
1330fbabfd0fSEric W. Biederman }
1331fbabfd0fSEric W. Biederman 
1332fbabfd0fSEric W. Biederman static const struct inode_operations empty_dir_inode_operations = {
1333fbabfd0fSEric W. Biederman 	.lookup		= empty_dir_lookup,
1334fbabfd0fSEric W. Biederman 	.permission	= generic_permission,
1335fbabfd0fSEric W. Biederman 	.setattr	= empty_dir_setattr,
1336fbabfd0fSEric W. Biederman 	.getattr	= empty_dir_getattr,
1337fbabfd0fSEric W. Biederman 	.listxattr	= empty_dir_listxattr,
1338fbabfd0fSEric W. Biederman };
1339fbabfd0fSEric W. Biederman 
1340fbabfd0fSEric W. Biederman static loff_t empty_dir_llseek(struct file *file, loff_t offset, int whence)
1341fbabfd0fSEric W. Biederman {
1342fbabfd0fSEric W. Biederman 	/* An empty directory has two entries . and .. at offsets 0 and 1 */
1343fbabfd0fSEric W. Biederman 	return generic_file_llseek_size(file, offset, whence, 2, 2);
1344fbabfd0fSEric W. Biederman }
1345fbabfd0fSEric W. Biederman 
1346fbabfd0fSEric W. Biederman static int empty_dir_readdir(struct file *file, struct dir_context *ctx)
1347fbabfd0fSEric W. Biederman {
1348fbabfd0fSEric W. Biederman 	dir_emit_dots(file, ctx);
1349fbabfd0fSEric W. Biederman 	return 0;
1350fbabfd0fSEric W. Biederman }
1351fbabfd0fSEric W. Biederman 
1352fbabfd0fSEric W. Biederman static const struct file_operations empty_dir_operations = {
1353fbabfd0fSEric W. Biederman 	.llseek		= empty_dir_llseek,
1354fbabfd0fSEric W. Biederman 	.read		= generic_read_dir,
1355c51da20cSAl Viro 	.iterate_shared	= empty_dir_readdir,
1356fbabfd0fSEric W. Biederman 	.fsync		= noop_fsync,
1357fbabfd0fSEric W. Biederman };
1358fbabfd0fSEric W. Biederman 
1359fbabfd0fSEric W. Biederman 
1360fbabfd0fSEric W. Biederman void make_empty_dir_inode(struct inode *inode)
1361fbabfd0fSEric W. Biederman {
1362fbabfd0fSEric W. Biederman 	set_nlink(inode, 2);
1363fbabfd0fSEric W. Biederman 	inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
1364fbabfd0fSEric W. Biederman 	inode->i_uid = GLOBAL_ROOT_UID;
1365fbabfd0fSEric W. Biederman 	inode->i_gid = GLOBAL_ROOT_GID;
1366fbabfd0fSEric W. Biederman 	inode->i_rdev = 0;
13674b75de86SEric W. Biederman 	inode->i_size = 0;
1368fbabfd0fSEric W. Biederman 	inode->i_blkbits = PAGE_SHIFT;
1369fbabfd0fSEric W. Biederman 	inode->i_blocks = 0;
1370fbabfd0fSEric W. Biederman 
1371fbabfd0fSEric W. Biederman 	inode->i_op = &empty_dir_inode_operations;
1372f5c24438SAndreas Gruenbacher 	inode->i_opflags &= ~IOP_XATTR;
1373fbabfd0fSEric W. Biederman 	inode->i_fop = &empty_dir_operations;
1374fbabfd0fSEric W. Biederman }
1375fbabfd0fSEric W. Biederman 
1376fbabfd0fSEric W. Biederman bool is_empty_dir_inode(struct inode *inode)
1377fbabfd0fSEric W. Biederman {
1378fbabfd0fSEric W. Biederman 	return (inode->i_fop == &empty_dir_operations) &&
1379fbabfd0fSEric W. Biederman 		(inode->i_op == &empty_dir_inode_operations);
1380fbabfd0fSEric W. Biederman }
1381c843843eSDaniel Rosenberg 
1382c843843eSDaniel Rosenberg #ifdef CONFIG_UNICODE
1383c843843eSDaniel Rosenberg /*
1384c843843eSDaniel Rosenberg  * Determine if the name of a dentry should be casefolded.
1385c843843eSDaniel Rosenberg  *
1386c843843eSDaniel Rosenberg  * Return: if names will need casefolding
1387c843843eSDaniel Rosenberg  */
1388c843843eSDaniel Rosenberg static bool needs_casefold(const struct inode *dir)
1389c843843eSDaniel Rosenberg {
1390c843843eSDaniel Rosenberg 	return IS_CASEFOLDED(dir) && dir->i_sb->s_encoding;
1391c843843eSDaniel Rosenberg }
1392c843843eSDaniel Rosenberg 
1393c843843eSDaniel Rosenberg /**
1394c843843eSDaniel Rosenberg  * generic_ci_d_compare - generic d_compare implementation for casefolding filesystems
1395c843843eSDaniel Rosenberg  * @dentry:	dentry whose name we are checking against
1396c843843eSDaniel Rosenberg  * @len:	len of name of dentry
1397c843843eSDaniel Rosenberg  * @str:	str pointer to name of dentry
1398c843843eSDaniel Rosenberg  * @name:	Name to compare against
1399c843843eSDaniel Rosenberg  *
1400c843843eSDaniel Rosenberg  * Return: 0 if names match, 1 if mismatch, or -ERRNO
1401c843843eSDaniel Rosenberg  */
1402794c43f7SEric Biggers static int generic_ci_d_compare(const struct dentry *dentry, unsigned int len,
1403c843843eSDaniel Rosenberg 				const char *str, const struct qstr *name)
1404c843843eSDaniel Rosenberg {
1405c843843eSDaniel Rosenberg 	const struct dentry *parent = READ_ONCE(dentry->d_parent);
1406c843843eSDaniel Rosenberg 	const struct inode *dir = READ_ONCE(parent->d_inode);
1407c843843eSDaniel Rosenberg 	const struct super_block *sb = dentry->d_sb;
1408c843843eSDaniel Rosenberg 	const struct unicode_map *um = sb->s_encoding;
1409c843843eSDaniel Rosenberg 	struct qstr qstr = QSTR_INIT(str, len);
1410c843843eSDaniel Rosenberg 	char strbuf[DNAME_INLINE_LEN];
1411c843843eSDaniel Rosenberg 	int ret;
1412c843843eSDaniel Rosenberg 
1413c843843eSDaniel Rosenberg 	if (!dir || !needs_casefold(dir))
1414c843843eSDaniel Rosenberg 		goto fallback;
1415c843843eSDaniel Rosenberg 	/*
1416c843843eSDaniel Rosenberg 	 * If the dentry name is stored in-line, then it may be concurrently
1417c843843eSDaniel Rosenberg 	 * modified by a rename.  If this happens, the VFS will eventually retry
1418c843843eSDaniel Rosenberg 	 * the lookup, so it doesn't matter what ->d_compare() returns.
1419c843843eSDaniel Rosenberg 	 * However, it's unsafe to call utf8_strncasecmp() with an unstable
1420c843843eSDaniel Rosenberg 	 * string.  Therefore, we have to copy the name into a temporary buffer.
1421c843843eSDaniel Rosenberg 	 */
1422c843843eSDaniel Rosenberg 	if (len <= DNAME_INLINE_LEN - 1) {
1423c843843eSDaniel Rosenberg 		memcpy(strbuf, str, len);
1424c843843eSDaniel Rosenberg 		strbuf[len] = 0;
1425c843843eSDaniel Rosenberg 		qstr.name = strbuf;
1426c843843eSDaniel Rosenberg 		/* prevent compiler from optimizing out the temporary buffer */
1427c843843eSDaniel Rosenberg 		barrier();
1428c843843eSDaniel Rosenberg 	}
1429c843843eSDaniel Rosenberg 	ret = utf8_strncasecmp(um, name, &qstr);
1430c843843eSDaniel Rosenberg 	if (ret >= 0)
1431c843843eSDaniel Rosenberg 		return ret;
1432c843843eSDaniel Rosenberg 
1433c843843eSDaniel Rosenberg 	if (sb_has_strict_encoding(sb))
1434c843843eSDaniel Rosenberg 		return -EINVAL;
1435c843843eSDaniel Rosenberg fallback:
1436c843843eSDaniel Rosenberg 	if (len != name->len)
1437c843843eSDaniel Rosenberg 		return 1;
1438c843843eSDaniel Rosenberg 	return !!memcmp(str, name->name, len);
1439c843843eSDaniel Rosenberg }
1440c843843eSDaniel Rosenberg 
1441c843843eSDaniel Rosenberg /**
1442c843843eSDaniel Rosenberg  * generic_ci_d_hash - generic d_hash implementation for casefolding filesystems
1443c843843eSDaniel Rosenberg  * @dentry:	dentry of the parent directory
1444c843843eSDaniel Rosenberg  * @str:	qstr of name whose hash we should fill in
1445c843843eSDaniel Rosenberg  *
1446c843843eSDaniel Rosenberg  * Return: 0 if hash was successful or unchanged, and -EINVAL on error
1447c843843eSDaniel Rosenberg  */
1448794c43f7SEric Biggers static int generic_ci_d_hash(const struct dentry *dentry, struct qstr *str)
1449c843843eSDaniel Rosenberg {
1450c843843eSDaniel Rosenberg 	const struct inode *dir = READ_ONCE(dentry->d_inode);
1451c843843eSDaniel Rosenberg 	struct super_block *sb = dentry->d_sb;
1452c843843eSDaniel Rosenberg 	const struct unicode_map *um = sb->s_encoding;
1453c843843eSDaniel Rosenberg 	int ret = 0;
1454c843843eSDaniel Rosenberg 
1455c843843eSDaniel Rosenberg 	if (!dir || !needs_casefold(dir))
1456c843843eSDaniel Rosenberg 		return 0;
1457c843843eSDaniel Rosenberg 
1458c843843eSDaniel Rosenberg 	ret = utf8_casefold_hash(um, dentry, str);
1459c843843eSDaniel Rosenberg 	if (ret < 0 && sb_has_strict_encoding(sb))
1460c843843eSDaniel Rosenberg 		return -EINVAL;
1461c843843eSDaniel Rosenberg 	return 0;
1462c843843eSDaniel Rosenberg }
1463608af703SDaniel Rosenberg 
1464608af703SDaniel Rosenberg static const struct dentry_operations generic_ci_dentry_ops = {
1465608af703SDaniel Rosenberg 	.d_hash = generic_ci_d_hash,
1466608af703SDaniel Rosenberg 	.d_compare = generic_ci_d_compare,
1467608af703SDaniel Rosenberg };
1468c843843eSDaniel Rosenberg #endif
1469608af703SDaniel Rosenberg 
1470608af703SDaniel Rosenberg #ifdef CONFIG_FS_ENCRYPTION
1471608af703SDaniel Rosenberg static const struct dentry_operations generic_encrypted_dentry_ops = {
1472608af703SDaniel Rosenberg 	.d_revalidate = fscrypt_d_revalidate,
1473608af703SDaniel Rosenberg };
1474608af703SDaniel Rosenberg #endif
1475608af703SDaniel Rosenberg 
1476608af703SDaniel Rosenberg #if defined(CONFIG_FS_ENCRYPTION) && defined(CONFIG_UNICODE)
1477608af703SDaniel Rosenberg static const struct dentry_operations generic_encrypted_ci_dentry_ops = {
1478608af703SDaniel Rosenberg 	.d_hash = generic_ci_d_hash,
1479608af703SDaniel Rosenberg 	.d_compare = generic_ci_d_compare,
1480608af703SDaniel Rosenberg 	.d_revalidate = fscrypt_d_revalidate,
1481608af703SDaniel Rosenberg };
1482608af703SDaniel Rosenberg #endif
1483608af703SDaniel Rosenberg 
1484608af703SDaniel Rosenberg /**
1485608af703SDaniel Rosenberg  * generic_set_encrypted_ci_d_ops - helper for setting d_ops for given dentry
1486608af703SDaniel Rosenberg  * @dentry:	dentry to set ops on
1487608af703SDaniel Rosenberg  *
1488608af703SDaniel Rosenberg  * Casefolded directories need d_hash and d_compare set, so that the dentries
1489608af703SDaniel Rosenberg  * contained in them are handled case-insensitively.  Note that these operations
1490608af703SDaniel Rosenberg  * are needed on the parent directory rather than on the dentries in it, and
1491608af703SDaniel Rosenberg  * while the casefolding flag can be toggled on and off on an empty directory,
1492608af703SDaniel Rosenberg  * dentry_operations can't be changed later.  As a result, if the filesystem has
1493608af703SDaniel Rosenberg  * casefolding support enabled at all, we have to give all dentries the
1494608af703SDaniel Rosenberg  * casefolding operations even if their inode doesn't have the casefolding flag
1495608af703SDaniel Rosenberg  * currently (and thus the casefolding ops would be no-ops for now).
1496608af703SDaniel Rosenberg  *
1497608af703SDaniel Rosenberg  * Encryption works differently in that the only dentry operation it needs is
1498608af703SDaniel Rosenberg  * d_revalidate, which it only needs on dentries that have the no-key name flag.
1499608af703SDaniel Rosenberg  * The no-key flag can't be set "later", so we don't have to worry about that.
1500608af703SDaniel Rosenberg  *
1501608af703SDaniel Rosenberg  * Finally, to maximize compatibility with overlayfs (which isn't compatible
1502608af703SDaniel Rosenberg  * with certain dentry operations) and to avoid taking an unnecessary
1503608af703SDaniel Rosenberg  * performance hit, we use custom dentry_operations for each possible
1504608af703SDaniel Rosenberg  * combination rather than always installing all operations.
1505608af703SDaniel Rosenberg  */
1506608af703SDaniel Rosenberg void generic_set_encrypted_ci_d_ops(struct dentry *dentry)
1507608af703SDaniel Rosenberg {
1508608af703SDaniel Rosenberg #ifdef CONFIG_FS_ENCRYPTION
1509608af703SDaniel Rosenberg 	bool needs_encrypt_ops = dentry->d_flags & DCACHE_NOKEY_NAME;
1510608af703SDaniel Rosenberg #endif
1511608af703SDaniel Rosenberg #ifdef CONFIG_UNICODE
1512608af703SDaniel Rosenberg 	bool needs_ci_ops = dentry->d_sb->s_encoding;
1513608af703SDaniel Rosenberg #endif
1514608af703SDaniel Rosenberg #if defined(CONFIG_FS_ENCRYPTION) && defined(CONFIG_UNICODE)
1515608af703SDaniel Rosenberg 	if (needs_encrypt_ops && needs_ci_ops) {
1516608af703SDaniel Rosenberg 		d_set_d_op(dentry, &generic_encrypted_ci_dentry_ops);
1517608af703SDaniel Rosenberg 		return;
1518608af703SDaniel Rosenberg 	}
1519608af703SDaniel Rosenberg #endif
1520608af703SDaniel Rosenberg #ifdef CONFIG_FS_ENCRYPTION
1521608af703SDaniel Rosenberg 	if (needs_encrypt_ops) {
1522608af703SDaniel Rosenberg 		d_set_d_op(dentry, &generic_encrypted_dentry_ops);
1523608af703SDaniel Rosenberg 		return;
1524608af703SDaniel Rosenberg 	}
1525608af703SDaniel Rosenberg #endif
1526608af703SDaniel Rosenberg #ifdef CONFIG_UNICODE
1527608af703SDaniel Rosenberg 	if (needs_ci_ops) {
1528608af703SDaniel Rosenberg 		d_set_d_op(dentry, &generic_ci_dentry_ops);
1529608af703SDaniel Rosenberg 		return;
1530608af703SDaniel Rosenberg 	}
1531608af703SDaniel Rosenberg #endif
1532608af703SDaniel Rosenberg }
1533608af703SDaniel Rosenberg EXPORT_SYMBOL(generic_set_encrypted_ci_d_ops);
1534