xref: /openbmc/linux/fs/libfs.c (revision 02c24a82)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *	fs/libfs.c
31da177e4SLinus Torvalds  *	Library for filesystems writers.
41da177e4SLinus Torvalds  */
51da177e4SLinus Torvalds 
61da177e4SLinus Torvalds #include <linux/module.h>
71da177e4SLinus Torvalds #include <linux/pagemap.h>
85a0e3ad6STejun Heo #include <linux/slab.h>
91da177e4SLinus Torvalds #include <linux/mount.h>
101da177e4SLinus Torvalds #include <linux/vfs.h>
117bb46a67Snpiggin@suse.de #include <linux/quotaops.h>
127cf34c76SIngo Molnar #include <linux/mutex.h>
132596110aSChristoph Hellwig #include <linux/exportfs.h>
14d5aacad5SAl Viro #include <linux/writeback.h>
15d5aacad5SAl Viro #include <linux/buffer_head.h>
167cf34c76SIngo Molnar 
171da177e4SLinus Torvalds #include <asm/uaccess.h>
181da177e4SLinus Torvalds 
19a4464dbcSAl Viro #include "internal.h"
20a4464dbcSAl Viro 
21da502956SNick Piggin static inline int simple_positive(struct dentry *dentry)
22da502956SNick Piggin {
23da502956SNick Piggin 	return dentry->d_inode && !d_unhashed(dentry);
24da502956SNick Piggin }
25da502956SNick Piggin 
261da177e4SLinus Torvalds int simple_getattr(struct vfsmount *mnt, struct dentry *dentry,
271da177e4SLinus Torvalds 		   struct kstat *stat)
281da177e4SLinus Torvalds {
291da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
301da177e4SLinus Torvalds 	generic_fillattr(inode, stat);
311da177e4SLinus Torvalds 	stat->blocks = inode->i_mapping->nrpages << (PAGE_CACHE_SHIFT - 9);
321da177e4SLinus Torvalds 	return 0;
331da177e4SLinus Torvalds }
341da177e4SLinus Torvalds 
35726c3342SDavid Howells int simple_statfs(struct dentry *dentry, struct kstatfs *buf)
361da177e4SLinus Torvalds {
37726c3342SDavid Howells 	buf->f_type = dentry->d_sb->s_magic;
381da177e4SLinus Torvalds 	buf->f_bsize = PAGE_CACHE_SIZE;
391da177e4SLinus Torvalds 	buf->f_namelen = NAME_MAX;
401da177e4SLinus Torvalds 	return 0;
411da177e4SLinus Torvalds }
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds /*
441da177e4SLinus Torvalds  * Retaining negative dentries for an in-memory filesystem just wastes
451da177e4SLinus Torvalds  * memory and lookup time: arrange for them to be deleted immediately.
461da177e4SLinus Torvalds  */
47fe15ce44SNick Piggin static int simple_delete_dentry(const struct dentry *dentry)
481da177e4SLinus Torvalds {
491da177e4SLinus Torvalds 	return 1;
501da177e4SLinus Torvalds }
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds /*
531da177e4SLinus Torvalds  * Lookup the data. This is trivial - if the dentry didn't already
541da177e4SLinus Torvalds  * exist, we know it is negative.  Set d_op to delete negative dentries.
551da177e4SLinus Torvalds  */
561da177e4SLinus Torvalds struct dentry *simple_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
571da177e4SLinus Torvalds {
583ba13d17SAl Viro 	static const struct dentry_operations simple_dentry_operations = {
591da177e4SLinus Torvalds 		.d_delete = simple_delete_dentry,
601da177e4SLinus Torvalds 	};
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds 	if (dentry->d_name.len > NAME_MAX)
631da177e4SLinus Torvalds 		return ERR_PTR(-ENAMETOOLONG);
64fb045adbSNick Piggin 	d_set_d_op(dentry, &simple_dentry_operations);
651da177e4SLinus Torvalds 	d_add(dentry, NULL);
661da177e4SLinus Torvalds 	return NULL;
671da177e4SLinus Torvalds }
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds int dcache_dir_open(struct inode *inode, struct file *file)
701da177e4SLinus Torvalds {
711da177e4SLinus Torvalds 	static struct qstr cursor_name = {.len = 1, .name = "."};
721da177e4SLinus Torvalds 
730f7fc9e4SJosef "Jeff" Sipek 	file->private_data = d_alloc(file->f_path.dentry, &cursor_name);
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds 	return file->private_data ? 0 : -ENOMEM;
761da177e4SLinus Torvalds }
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds int dcache_dir_close(struct inode *inode, struct file *file)
791da177e4SLinus Torvalds {
801da177e4SLinus Torvalds 	dput(file->private_data);
811da177e4SLinus Torvalds 	return 0;
821da177e4SLinus Torvalds }
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds loff_t dcache_dir_lseek(struct file *file, loff_t offset, int origin)
851da177e4SLinus Torvalds {
862fd6b7f5SNick Piggin 	struct dentry *dentry = file->f_path.dentry;
872fd6b7f5SNick Piggin 	mutex_lock(&dentry->d_inode->i_mutex);
881da177e4SLinus Torvalds 	switch (origin) {
891da177e4SLinus Torvalds 		case 1:
901da177e4SLinus Torvalds 			offset += file->f_pos;
911da177e4SLinus Torvalds 		case 0:
921da177e4SLinus Torvalds 			if (offset >= 0)
931da177e4SLinus Torvalds 				break;
941da177e4SLinus Torvalds 		default:
952fd6b7f5SNick Piggin 			mutex_unlock(&dentry->d_inode->i_mutex);
961da177e4SLinus Torvalds 			return -EINVAL;
971da177e4SLinus Torvalds 	}
981da177e4SLinus Torvalds 	if (offset != file->f_pos) {
991da177e4SLinus Torvalds 		file->f_pos = offset;
1001da177e4SLinus Torvalds 		if (file->f_pos >= 2) {
1011da177e4SLinus Torvalds 			struct list_head *p;
1021da177e4SLinus Torvalds 			struct dentry *cursor = file->private_data;
1031da177e4SLinus Torvalds 			loff_t n = file->f_pos - 2;
1041da177e4SLinus Torvalds 
1052fd6b7f5SNick Piggin 			spin_lock(&dentry->d_lock);
1062fd6b7f5SNick Piggin 			/* d_lock not required for cursor */
1075160ee6fSEric Dumazet 			list_del(&cursor->d_u.d_child);
1082fd6b7f5SNick Piggin 			p = dentry->d_subdirs.next;
1092fd6b7f5SNick Piggin 			while (n && p != &dentry->d_subdirs) {
1101da177e4SLinus Torvalds 				struct dentry *next;
1115160ee6fSEric Dumazet 				next = list_entry(p, struct dentry, d_u.d_child);
1122fd6b7f5SNick Piggin 				spin_lock_nested(&next->d_lock, DENTRY_D_LOCK_NESTED);
113da502956SNick Piggin 				if (simple_positive(next))
1141da177e4SLinus Torvalds 					n--;
115da502956SNick Piggin 				spin_unlock(&next->d_lock);
1161da177e4SLinus Torvalds 				p = p->next;
1171da177e4SLinus Torvalds 			}
1185160ee6fSEric Dumazet 			list_add_tail(&cursor->d_u.d_child, p);
1192fd6b7f5SNick Piggin 			spin_unlock(&dentry->d_lock);
1201da177e4SLinus Torvalds 		}
1211da177e4SLinus Torvalds 	}
1222fd6b7f5SNick Piggin 	mutex_unlock(&dentry->d_inode->i_mutex);
1231da177e4SLinus Torvalds 	return offset;
1241da177e4SLinus Torvalds }
1251da177e4SLinus Torvalds 
1261da177e4SLinus Torvalds /* Relationship between i_mode and the DT_xxx types */
1271da177e4SLinus Torvalds static inline unsigned char dt_type(struct inode *inode)
1281da177e4SLinus Torvalds {
1291da177e4SLinus Torvalds 	return (inode->i_mode >> 12) & 15;
1301da177e4SLinus Torvalds }
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds /*
1331da177e4SLinus Torvalds  * Directory is locked and all positive dentries in it are safe, since
1341da177e4SLinus Torvalds  * for ramfs-type trees they can't go away without unlink() or rmdir(),
1351da177e4SLinus Torvalds  * both impossible due to the lock on directory.
1361da177e4SLinus Torvalds  */
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds int dcache_readdir(struct file * filp, void * dirent, filldir_t filldir)
1391da177e4SLinus Torvalds {
1400f7fc9e4SJosef "Jeff" Sipek 	struct dentry *dentry = filp->f_path.dentry;
1411da177e4SLinus Torvalds 	struct dentry *cursor = filp->private_data;
1425160ee6fSEric Dumazet 	struct list_head *p, *q = &cursor->d_u.d_child;
1431da177e4SLinus Torvalds 	ino_t ino;
1441da177e4SLinus Torvalds 	int i = filp->f_pos;
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds 	switch (i) {
1471da177e4SLinus Torvalds 		case 0:
1481da177e4SLinus Torvalds 			ino = dentry->d_inode->i_ino;
1491da177e4SLinus Torvalds 			if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
1501da177e4SLinus Torvalds 				break;
1511da177e4SLinus Torvalds 			filp->f_pos++;
1521da177e4SLinus Torvalds 			i++;
1531da177e4SLinus Torvalds 			/* fallthrough */
1541da177e4SLinus Torvalds 		case 1:
1551da177e4SLinus Torvalds 			ino = parent_ino(dentry);
1561da177e4SLinus Torvalds 			if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
1571da177e4SLinus Torvalds 				break;
1581da177e4SLinus Torvalds 			filp->f_pos++;
1591da177e4SLinus Torvalds 			i++;
1601da177e4SLinus Torvalds 			/* fallthrough */
1611da177e4SLinus Torvalds 		default:
1622fd6b7f5SNick Piggin 			spin_lock(&dentry->d_lock);
1631bfba4e8SAkinobu Mita 			if (filp->f_pos == 2)
1641bfba4e8SAkinobu Mita 				list_move(q, &dentry->d_subdirs);
1651bfba4e8SAkinobu Mita 
1661da177e4SLinus Torvalds 			for (p=q->next; p != &dentry->d_subdirs; p=p->next) {
1671da177e4SLinus Torvalds 				struct dentry *next;
1685160ee6fSEric Dumazet 				next = list_entry(p, struct dentry, d_u.d_child);
169da502956SNick Piggin 				spin_lock_nested(&next->d_lock, DENTRY_D_LOCK_NESTED);
170da502956SNick Piggin 				if (!simple_positive(next)) {
171da502956SNick Piggin 					spin_unlock(&next->d_lock);
1721da177e4SLinus Torvalds 					continue;
173da502956SNick Piggin 				}
1741da177e4SLinus Torvalds 
175da502956SNick Piggin 				spin_unlock(&next->d_lock);
1762fd6b7f5SNick Piggin 				spin_unlock(&dentry->d_lock);
1770f8952c2SRonni Nielsen 				if (filldir(dirent, next->d_name.name,
1780f8952c2SRonni Nielsen 					    next->d_name.len, filp->f_pos,
1790f8952c2SRonni Nielsen 					    next->d_inode->i_ino,
1800f8952c2SRonni Nielsen 					    dt_type(next->d_inode)) < 0)
1811da177e4SLinus Torvalds 					return 0;
1822fd6b7f5SNick Piggin 				spin_lock(&dentry->d_lock);
1832fd6b7f5SNick Piggin 				spin_lock_nested(&next->d_lock, DENTRY_D_LOCK_NESTED);
1841da177e4SLinus Torvalds 				/* next is still alive */
1851bfba4e8SAkinobu Mita 				list_move(q, p);
1862fd6b7f5SNick Piggin 				spin_unlock(&next->d_lock);
1871da177e4SLinus Torvalds 				p = q;
1881da177e4SLinus Torvalds 				filp->f_pos++;
1891da177e4SLinus Torvalds 			}
1902fd6b7f5SNick Piggin 			spin_unlock(&dentry->d_lock);
1911da177e4SLinus Torvalds 	}
1921da177e4SLinus Torvalds 	return 0;
1931da177e4SLinus Torvalds }
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds ssize_t generic_read_dir(struct file *filp, char __user *buf, size_t siz, loff_t *ppos)
1961da177e4SLinus Torvalds {
1971da177e4SLinus Torvalds 	return -EISDIR;
1981da177e4SLinus Torvalds }
1991da177e4SLinus Torvalds 
2004b6f5d20SArjan van de Ven const struct file_operations simple_dir_operations = {
2011da177e4SLinus Torvalds 	.open		= dcache_dir_open,
2021da177e4SLinus Torvalds 	.release	= dcache_dir_close,
2031da177e4SLinus Torvalds 	.llseek		= dcache_dir_lseek,
2041da177e4SLinus Torvalds 	.read		= generic_read_dir,
2051da177e4SLinus Torvalds 	.readdir	= dcache_readdir,
2061b061d92SChristoph Hellwig 	.fsync		= noop_fsync,
2071da177e4SLinus Torvalds };
2081da177e4SLinus Torvalds 
20992e1d5beSArjan van de Ven const struct inode_operations simple_dir_inode_operations = {
2101da177e4SLinus Torvalds 	.lookup		= simple_lookup,
2111da177e4SLinus Torvalds };
2121da177e4SLinus Torvalds 
213759b9775SHugh Dickins static const struct super_operations simple_super_operations = {
214759b9775SHugh Dickins 	.statfs		= simple_statfs,
215759b9775SHugh Dickins };
216759b9775SHugh Dickins 
2171da177e4SLinus Torvalds /*
2181da177e4SLinus Torvalds  * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that
2191da177e4SLinus Torvalds  * will never be mountable)
2201da177e4SLinus Torvalds  */
22151139adaSAl Viro struct dentry *mount_pseudo(struct file_system_type *fs_type, char *name,
222c74a1cbbSAl Viro 	const struct super_operations *ops,
223c74a1cbbSAl Viro 	const struct dentry_operations *dops, unsigned long magic)
2241da177e4SLinus Torvalds {
2251da177e4SLinus Torvalds 	struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
2261da177e4SLinus Torvalds 	struct dentry *dentry;
2271da177e4SLinus Torvalds 	struct inode *root;
2281da177e4SLinus Torvalds 	struct qstr d_name = {.name = name, .len = strlen(name)};
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds 	if (IS_ERR(s))
23151139adaSAl Viro 		return ERR_CAST(s);
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds 	s->s_flags = MS_NOUSER;
23489a4eb4bSJeff Layton 	s->s_maxbytes = MAX_LFS_FILESIZE;
2353971e1a9SAlex Nixon 	s->s_blocksize = PAGE_SIZE;
2363971e1a9SAlex Nixon 	s->s_blocksize_bits = PAGE_SHIFT;
2371da177e4SLinus Torvalds 	s->s_magic = magic;
238759b9775SHugh Dickins 	s->s_op = ops ? ops : &simple_super_operations;
2391da177e4SLinus Torvalds 	s->s_time_gran = 1;
2401da177e4SLinus Torvalds 	root = new_inode(s);
2411da177e4SLinus Torvalds 	if (!root)
2421da177e4SLinus Torvalds 		goto Enomem;
2431a1c9bb4SJeff Layton 	/*
2441a1c9bb4SJeff Layton 	 * since this is the first inode, make it number 1. New inodes created
2451a1c9bb4SJeff Layton 	 * after this must take care not to collide with it (by passing
2461a1c9bb4SJeff Layton 	 * max_reserved of 1 to iunique).
2471a1c9bb4SJeff Layton 	 */
2481a1c9bb4SJeff Layton 	root->i_ino = 1;
2491da177e4SLinus Torvalds 	root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR;
2501da177e4SLinus Torvalds 	root->i_atime = root->i_mtime = root->i_ctime = CURRENT_TIME;
251a4464dbcSAl Viro 	dentry = __d_alloc(s, &d_name);
2521da177e4SLinus Torvalds 	if (!dentry) {
2531da177e4SLinus Torvalds 		iput(root);
2541da177e4SLinus Torvalds 		goto Enomem;
2551da177e4SLinus Torvalds 	}
2561da177e4SLinus Torvalds 	d_instantiate(dentry, root);
2571da177e4SLinus Torvalds 	s->s_root = dentry;
258c74a1cbbSAl Viro 	s->s_d_op = dops;
2591da177e4SLinus Torvalds 	s->s_flags |= MS_ACTIVE;
26051139adaSAl Viro 	return dget(s->s_root);
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds Enomem:
2636f5bbff9SAl Viro 	deactivate_locked_super(s);
26451139adaSAl Viro 	return ERR_PTR(-ENOMEM);
2651da177e4SLinus Torvalds }
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
2681da177e4SLinus Torvalds {
2691da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds 	inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
272d8c76e6fSDave Hansen 	inc_nlink(inode);
2737de9c6eeSAl Viro 	ihold(inode);
2741da177e4SLinus Torvalds 	dget(dentry);
2751da177e4SLinus Torvalds 	d_instantiate(dentry, inode);
2761da177e4SLinus Torvalds 	return 0;
2771da177e4SLinus Torvalds }
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds int simple_empty(struct dentry *dentry)
2801da177e4SLinus Torvalds {
2811da177e4SLinus Torvalds 	struct dentry *child;
2821da177e4SLinus Torvalds 	int ret = 0;
2831da177e4SLinus Torvalds 
2842fd6b7f5SNick Piggin 	spin_lock(&dentry->d_lock);
285da502956SNick Piggin 	list_for_each_entry(child, &dentry->d_subdirs, d_u.d_child) {
286da502956SNick Piggin 		spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED);
287da502956SNick Piggin 		if (simple_positive(child)) {
288da502956SNick Piggin 			spin_unlock(&child->d_lock);
2891da177e4SLinus Torvalds 			goto out;
290da502956SNick Piggin 		}
291da502956SNick Piggin 		spin_unlock(&child->d_lock);
292da502956SNick Piggin 	}
2931da177e4SLinus Torvalds 	ret = 1;
2941da177e4SLinus Torvalds out:
2952fd6b7f5SNick Piggin 	spin_unlock(&dentry->d_lock);
2961da177e4SLinus Torvalds 	return ret;
2971da177e4SLinus Torvalds }
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds int simple_unlink(struct inode *dir, struct dentry *dentry)
3001da177e4SLinus Torvalds {
3011da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds 	inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
3049a53c3a7SDave Hansen 	drop_nlink(inode);
3051da177e4SLinus Torvalds 	dput(dentry);
3061da177e4SLinus Torvalds 	return 0;
3071da177e4SLinus Torvalds }
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds int simple_rmdir(struct inode *dir, struct dentry *dentry)
3101da177e4SLinus Torvalds {
3111da177e4SLinus Torvalds 	if (!simple_empty(dentry))
3121da177e4SLinus Torvalds 		return -ENOTEMPTY;
3131da177e4SLinus Torvalds 
3149a53c3a7SDave Hansen 	drop_nlink(dentry->d_inode);
3151da177e4SLinus Torvalds 	simple_unlink(dir, dentry);
3169a53c3a7SDave Hansen 	drop_nlink(dir);
3171da177e4SLinus Torvalds 	return 0;
3181da177e4SLinus Torvalds }
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds int simple_rename(struct inode *old_dir, struct dentry *old_dentry,
3211da177e4SLinus Torvalds 		struct inode *new_dir, struct dentry *new_dentry)
3221da177e4SLinus Torvalds {
3231da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
3241da177e4SLinus Torvalds 	int they_are_dirs = S_ISDIR(old_dentry->d_inode->i_mode);
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds 	if (!simple_empty(new_dentry))
3271da177e4SLinus Torvalds 		return -ENOTEMPTY;
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds 	if (new_dentry->d_inode) {
3301da177e4SLinus Torvalds 		simple_unlink(new_dir, new_dentry);
3311da177e4SLinus Torvalds 		if (they_are_dirs)
3329a53c3a7SDave Hansen 			drop_nlink(old_dir);
3331da177e4SLinus Torvalds 	} else if (they_are_dirs) {
3349a53c3a7SDave Hansen 		drop_nlink(old_dir);
335d8c76e6fSDave Hansen 		inc_nlink(new_dir);
3361da177e4SLinus Torvalds 	}
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds 	old_dir->i_ctime = old_dir->i_mtime = new_dir->i_ctime =
3391da177e4SLinus Torvalds 		new_dir->i_mtime = inode->i_ctime = CURRENT_TIME;
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds 	return 0;
3421da177e4SLinus Torvalds }
3431da177e4SLinus Torvalds 
3447bb46a67Snpiggin@suse.de /**
345eef2380cSChristoph Hellwig  * simple_setattr - setattr for simple filesystem
3467bb46a67Snpiggin@suse.de  * @dentry: dentry
3477bb46a67Snpiggin@suse.de  * @iattr: iattr structure
3487bb46a67Snpiggin@suse.de  *
3497bb46a67Snpiggin@suse.de  * Returns 0 on success, -error on failure.
3507bb46a67Snpiggin@suse.de  *
351eef2380cSChristoph Hellwig  * simple_setattr is a simple ->setattr implementation without a proper
352eef2380cSChristoph Hellwig  * implementation of size changes.
353eef2380cSChristoph Hellwig  *
354eef2380cSChristoph Hellwig  * It can either be used for in-memory filesystems or special files
355eef2380cSChristoph Hellwig  * on simple regular filesystems.  Anything that needs to change on-disk
356eef2380cSChristoph Hellwig  * or wire state on size changes needs its own setattr method.
3577bb46a67Snpiggin@suse.de  */
3587bb46a67Snpiggin@suse.de int simple_setattr(struct dentry *dentry, struct iattr *iattr)
3597bb46a67Snpiggin@suse.de {
3607bb46a67Snpiggin@suse.de 	struct inode *inode = dentry->d_inode;
3617bb46a67Snpiggin@suse.de 	int error;
3627bb46a67Snpiggin@suse.de 
363eef2380cSChristoph Hellwig 	WARN_ON_ONCE(inode->i_op->truncate);
364eef2380cSChristoph Hellwig 
3657bb46a67Snpiggin@suse.de 	error = inode_change_ok(inode, iattr);
3667bb46a67Snpiggin@suse.de 	if (error)
3677bb46a67Snpiggin@suse.de 		return error;
3687bb46a67Snpiggin@suse.de 
3692c27c65eSChristoph Hellwig 	if (iattr->ia_valid & ATTR_SIZE)
3702c27c65eSChristoph Hellwig 		truncate_setsize(inode, iattr->ia_size);
3716a1a90adSChristoph Hellwig 	setattr_copy(inode, iattr);
372eef2380cSChristoph Hellwig 	mark_inode_dirty(inode);
373eef2380cSChristoph Hellwig 	return 0;
3747bb46a67Snpiggin@suse.de }
3757bb46a67Snpiggin@suse.de EXPORT_SYMBOL(simple_setattr);
3767bb46a67Snpiggin@suse.de 
3771da177e4SLinus Torvalds int simple_readpage(struct file *file, struct page *page)
3781da177e4SLinus Torvalds {
379c0d92cbcSPekka J Enberg 	clear_highpage(page);
3801da177e4SLinus Torvalds 	flush_dcache_page(page);
3811da177e4SLinus Torvalds 	SetPageUptodate(page);
3821da177e4SLinus Torvalds 	unlock_page(page);
3831da177e4SLinus Torvalds 	return 0;
3841da177e4SLinus Torvalds }
3851da177e4SLinus Torvalds 
386afddba49SNick Piggin int simple_write_begin(struct file *file, struct address_space *mapping,
387afddba49SNick Piggin 			loff_t pos, unsigned len, unsigned flags,
388afddba49SNick Piggin 			struct page **pagep, void **fsdata)
389afddba49SNick Piggin {
390afddba49SNick Piggin 	struct page *page;
391afddba49SNick Piggin 	pgoff_t index;
392afddba49SNick Piggin 
393afddba49SNick Piggin 	index = pos >> PAGE_CACHE_SHIFT;
394afddba49SNick Piggin 
39554566b2cSNick Piggin 	page = grab_cache_page_write_begin(mapping, index, flags);
396afddba49SNick Piggin 	if (!page)
397afddba49SNick Piggin 		return -ENOMEM;
398afddba49SNick Piggin 
399afddba49SNick Piggin 	*pagep = page;
400afddba49SNick Piggin 
401193cf4b9SBoaz Harrosh 	if (!PageUptodate(page) && (len != PAGE_CACHE_SIZE)) {
402193cf4b9SBoaz Harrosh 		unsigned from = pos & (PAGE_CACHE_SIZE - 1);
403193cf4b9SBoaz Harrosh 
404193cf4b9SBoaz Harrosh 		zero_user_segments(page, 0, from, from + len, PAGE_CACHE_SIZE);
405193cf4b9SBoaz Harrosh 	}
406193cf4b9SBoaz Harrosh 	return 0;
407afddba49SNick Piggin }
408afddba49SNick Piggin 
409ad2a722fSBoaz Harrosh /**
410ad2a722fSBoaz Harrosh  * simple_write_end - .write_end helper for non-block-device FSes
411ad2a722fSBoaz Harrosh  * @available: See .write_end of address_space_operations
412ad2a722fSBoaz Harrosh  * @file: 		"
413ad2a722fSBoaz Harrosh  * @mapping: 		"
414ad2a722fSBoaz Harrosh  * @pos: 		"
415ad2a722fSBoaz Harrosh  * @len: 		"
416ad2a722fSBoaz Harrosh  * @copied: 		"
417ad2a722fSBoaz Harrosh  * @page: 		"
418ad2a722fSBoaz Harrosh  * @fsdata: 		"
419ad2a722fSBoaz Harrosh  *
420ad2a722fSBoaz Harrosh  * simple_write_end does the minimum needed for updating a page after writing is
421ad2a722fSBoaz Harrosh  * done. It has the same API signature as the .write_end of
422ad2a722fSBoaz Harrosh  * address_space_operations vector. So it can just be set onto .write_end for
423ad2a722fSBoaz Harrosh  * FSes that don't need any other processing. i_mutex is assumed to be held.
424ad2a722fSBoaz Harrosh  * Block based filesystems should use generic_write_end().
425ad2a722fSBoaz Harrosh  * NOTE: Even though i_size might get updated by this function, mark_inode_dirty
426ad2a722fSBoaz Harrosh  * is not called, so a filesystem that actually does store data in .write_inode
427ad2a722fSBoaz Harrosh  * should extend on what's done here with a call to mark_inode_dirty() in the
428ad2a722fSBoaz Harrosh  * case that i_size has changed.
429ad2a722fSBoaz Harrosh  */
430ad2a722fSBoaz Harrosh int simple_write_end(struct file *file, struct address_space *mapping,
431ad2a722fSBoaz Harrosh 			loff_t pos, unsigned len, unsigned copied,
432ad2a722fSBoaz Harrosh 			struct page *page, void *fsdata)
4331da177e4SLinus Torvalds {
4341da177e4SLinus Torvalds 	struct inode *inode = page->mapping->host;
435ad2a722fSBoaz Harrosh 	loff_t last_pos = pos + copied;
436ad2a722fSBoaz Harrosh 
437ad2a722fSBoaz Harrosh 	/* zero the stale part of the page if we did a short copy */
438ad2a722fSBoaz Harrosh 	if (copied < len) {
439ad2a722fSBoaz Harrosh 		unsigned from = pos & (PAGE_CACHE_SIZE - 1);
440ad2a722fSBoaz Harrosh 
441ad2a722fSBoaz Harrosh 		zero_user(page, from + copied, len - copied);
442ad2a722fSBoaz Harrosh 	}
4431da177e4SLinus Torvalds 
444955eff5aSNick Piggin 	if (!PageUptodate(page))
445955eff5aSNick Piggin 		SetPageUptodate(page);
4461da177e4SLinus Torvalds 	/*
4471da177e4SLinus Torvalds 	 * No need to use i_size_read() here, the i_size
4481b1dcc1bSJes Sorensen 	 * cannot change under us because we hold the i_mutex.
4491da177e4SLinus Torvalds 	 */
450ad2a722fSBoaz Harrosh 	if (last_pos > inode->i_size)
451ad2a722fSBoaz Harrosh 		i_size_write(inode, last_pos);
452ad2a722fSBoaz Harrosh 
4531da177e4SLinus Torvalds 	set_page_dirty(page);
454afddba49SNick Piggin 	unlock_page(page);
455afddba49SNick Piggin 	page_cache_release(page);
456afddba49SNick Piggin 
457afddba49SNick Piggin 	return copied;
458afddba49SNick Piggin }
459afddba49SNick Piggin 
4601a1c9bb4SJeff Layton /*
4611a1c9bb4SJeff Layton  * the inodes created here are not hashed. If you use iunique to generate
4621a1c9bb4SJeff Layton  * unique inode values later for this filesystem, then you must take care
4631a1c9bb4SJeff Layton  * to pass it an appropriate max_reserved value to avoid collisions.
4641a1c9bb4SJeff Layton  */
4657d683a09SRoberto Sassu int simple_fill_super(struct super_block *s, unsigned long magic,
4667d683a09SRoberto Sassu 		      struct tree_descr *files)
4671da177e4SLinus Torvalds {
4681da177e4SLinus Torvalds 	struct inode *inode;
4691da177e4SLinus Torvalds 	struct dentry *root;
4701da177e4SLinus Torvalds 	struct dentry *dentry;
4711da177e4SLinus Torvalds 	int i;
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds 	s->s_blocksize = PAGE_CACHE_SIZE;
4741da177e4SLinus Torvalds 	s->s_blocksize_bits = PAGE_CACHE_SHIFT;
4751da177e4SLinus Torvalds 	s->s_magic = magic;
476759b9775SHugh Dickins 	s->s_op = &simple_super_operations;
4771da177e4SLinus Torvalds 	s->s_time_gran = 1;
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds 	inode = new_inode(s);
4801da177e4SLinus Torvalds 	if (!inode)
4811da177e4SLinus Torvalds 		return -ENOMEM;
4821a1c9bb4SJeff Layton 	/*
4831a1c9bb4SJeff Layton 	 * because the root inode is 1, the files array must not contain an
4841a1c9bb4SJeff Layton 	 * entry at index 1
4851a1c9bb4SJeff Layton 	 */
4861a1c9bb4SJeff Layton 	inode->i_ino = 1;
4871da177e4SLinus Torvalds 	inode->i_mode = S_IFDIR | 0755;
4881da177e4SLinus Torvalds 	inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
4891da177e4SLinus Torvalds 	inode->i_op = &simple_dir_inode_operations;
4901da177e4SLinus Torvalds 	inode->i_fop = &simple_dir_operations;
4917656f328SVincent Hanquez 	inode->i_nlink = 2;
4921da177e4SLinus Torvalds 	root = d_alloc_root(inode);
4931da177e4SLinus Torvalds 	if (!root) {
4941da177e4SLinus Torvalds 		iput(inode);
4951da177e4SLinus Torvalds 		return -ENOMEM;
4961da177e4SLinus Torvalds 	}
4971da177e4SLinus Torvalds 	for (i = 0; !files->name || files->name[0]; i++, files++) {
4981da177e4SLinus Torvalds 		if (!files->name)
4991da177e4SLinus Torvalds 			continue;
5001a1c9bb4SJeff Layton 
5011a1c9bb4SJeff Layton 		/* warn if it tries to conflict with the root inode */
5021a1c9bb4SJeff Layton 		if (unlikely(i == 1))
5031a1c9bb4SJeff Layton 			printk(KERN_WARNING "%s: %s passed in a files array"
5041a1c9bb4SJeff Layton 				"with an index of 1!\n", __func__,
5051a1c9bb4SJeff Layton 				s->s_type->name);
5061a1c9bb4SJeff Layton 
5071da177e4SLinus Torvalds 		dentry = d_alloc_name(root, files->name);
5081da177e4SLinus Torvalds 		if (!dentry)
5091da177e4SLinus Torvalds 			goto out;
5101da177e4SLinus Torvalds 		inode = new_inode(s);
5111da177e4SLinus Torvalds 		if (!inode)
5121da177e4SLinus Torvalds 			goto out;
5131da177e4SLinus Torvalds 		inode->i_mode = S_IFREG | files->mode;
5141da177e4SLinus Torvalds 		inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
5151da177e4SLinus Torvalds 		inode->i_fop = files->ops;
5161da177e4SLinus Torvalds 		inode->i_ino = i;
5171da177e4SLinus Torvalds 		d_add(dentry, inode);
5181da177e4SLinus Torvalds 	}
5191da177e4SLinus Torvalds 	s->s_root = root;
5201da177e4SLinus Torvalds 	return 0;
5211da177e4SLinus Torvalds out:
5221da177e4SLinus Torvalds 	d_genocide(root);
5231da177e4SLinus Torvalds 	dput(root);
5241da177e4SLinus Torvalds 	return -ENOMEM;
5251da177e4SLinus Torvalds }
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds static DEFINE_SPINLOCK(pin_fs_lock);
5281da177e4SLinus Torvalds 
5291f5ce9e9STrond Myklebust int simple_pin_fs(struct file_system_type *type, struct vfsmount **mount, int *count)
5301da177e4SLinus Torvalds {
5311da177e4SLinus Torvalds 	struct vfsmount *mnt = NULL;
5321da177e4SLinus Torvalds 	spin_lock(&pin_fs_lock);
5331da177e4SLinus Torvalds 	if (unlikely(!*mount)) {
5341da177e4SLinus Torvalds 		spin_unlock(&pin_fs_lock);
5351f5ce9e9STrond Myklebust 		mnt = vfs_kern_mount(type, 0, type->name, NULL);
5361da177e4SLinus Torvalds 		if (IS_ERR(mnt))
5371da177e4SLinus Torvalds 			return PTR_ERR(mnt);
5381da177e4SLinus Torvalds 		spin_lock(&pin_fs_lock);
5391da177e4SLinus Torvalds 		if (!*mount)
5401da177e4SLinus Torvalds 			*mount = mnt;
5411da177e4SLinus Torvalds 	}
5421da177e4SLinus Torvalds 	mntget(*mount);
5431da177e4SLinus Torvalds 	++*count;
5441da177e4SLinus Torvalds 	spin_unlock(&pin_fs_lock);
5451da177e4SLinus Torvalds 	mntput(mnt);
5461da177e4SLinus Torvalds 	return 0;
5471da177e4SLinus Torvalds }
5481da177e4SLinus Torvalds 
5491da177e4SLinus Torvalds void simple_release_fs(struct vfsmount **mount, int *count)
5501da177e4SLinus Torvalds {
5511da177e4SLinus Torvalds 	struct vfsmount *mnt;
5521da177e4SLinus Torvalds 	spin_lock(&pin_fs_lock);
5531da177e4SLinus Torvalds 	mnt = *mount;
5541da177e4SLinus Torvalds 	if (!--*count)
5551da177e4SLinus Torvalds 		*mount = NULL;
5561da177e4SLinus Torvalds 	spin_unlock(&pin_fs_lock);
5571da177e4SLinus Torvalds 	mntput(mnt);
5581da177e4SLinus Torvalds }
5591da177e4SLinus Torvalds 
5606d1029b5SAkinobu Mita /**
5616d1029b5SAkinobu Mita  * simple_read_from_buffer - copy data from the buffer to user space
5626d1029b5SAkinobu Mita  * @to: the user space buffer to read to
5636d1029b5SAkinobu Mita  * @count: the maximum number of bytes to read
5646d1029b5SAkinobu Mita  * @ppos: the current position in the buffer
5656d1029b5SAkinobu Mita  * @from: the buffer to read from
5666d1029b5SAkinobu Mita  * @available: the size of the buffer
5676d1029b5SAkinobu Mita  *
5686d1029b5SAkinobu Mita  * The simple_read_from_buffer() function reads up to @count bytes from the
5696d1029b5SAkinobu Mita  * buffer @from at offset @ppos into the user space address starting at @to.
5706d1029b5SAkinobu Mita  *
5716d1029b5SAkinobu Mita  * On success, the number of bytes read is returned and the offset @ppos is
5726d1029b5SAkinobu Mita  * advanced by this number, or negative value is returned on error.
5736d1029b5SAkinobu Mita  **/
5741da177e4SLinus Torvalds ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos,
5751da177e4SLinus Torvalds 				const void *from, size_t available)
5761da177e4SLinus Torvalds {
5771da177e4SLinus Torvalds 	loff_t pos = *ppos;
57814be2746SSteven Rostedt 	size_t ret;
57914be2746SSteven Rostedt 
5801da177e4SLinus Torvalds 	if (pos < 0)
5811da177e4SLinus Torvalds 		return -EINVAL;
58214be2746SSteven Rostedt 	if (pos >= available || !count)
5831da177e4SLinus Torvalds 		return 0;
5841da177e4SLinus Torvalds 	if (count > available - pos)
5851da177e4SLinus Torvalds 		count = available - pos;
58614be2746SSteven Rostedt 	ret = copy_to_user(to, from + pos, count);
58714be2746SSteven Rostedt 	if (ret == count)
5881da177e4SLinus Torvalds 		return -EFAULT;
58914be2746SSteven Rostedt 	count -= ret;
5901da177e4SLinus Torvalds 	*ppos = pos + count;
5911da177e4SLinus Torvalds 	return count;
5921da177e4SLinus Torvalds }
5931da177e4SLinus Torvalds 
5946d1029b5SAkinobu Mita /**
5956a727b43SJiri Slaby  * simple_write_to_buffer - copy data from user space to the buffer
5966a727b43SJiri Slaby  * @to: the buffer to write to
5976a727b43SJiri Slaby  * @available: the size of the buffer
5986a727b43SJiri Slaby  * @ppos: the current position in the buffer
5996a727b43SJiri Slaby  * @from: the user space buffer to read from
6006a727b43SJiri Slaby  * @count: the maximum number of bytes to read
6016a727b43SJiri Slaby  *
6026a727b43SJiri Slaby  * The simple_write_to_buffer() function reads up to @count bytes from the user
6036a727b43SJiri Slaby  * space address starting at @from into the buffer @to at offset @ppos.
6046a727b43SJiri Slaby  *
6056a727b43SJiri Slaby  * On success, the number of bytes written is returned and the offset @ppos is
6066a727b43SJiri Slaby  * advanced by this number, or negative value is returned on error.
6076a727b43SJiri Slaby  **/
6086a727b43SJiri Slaby ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos,
6096a727b43SJiri Slaby 		const void __user *from, size_t count)
6106a727b43SJiri Slaby {
6116a727b43SJiri Slaby 	loff_t pos = *ppos;
6126a727b43SJiri Slaby 	size_t res;
6136a727b43SJiri Slaby 
6146a727b43SJiri Slaby 	if (pos < 0)
6156a727b43SJiri Slaby 		return -EINVAL;
6166a727b43SJiri Slaby 	if (pos >= available || !count)
6176a727b43SJiri Slaby 		return 0;
6186a727b43SJiri Slaby 	if (count > available - pos)
6196a727b43SJiri Slaby 		count = available - pos;
6206a727b43SJiri Slaby 	res = copy_from_user(to + pos, from, count);
6216a727b43SJiri Slaby 	if (res == count)
6226a727b43SJiri Slaby 		return -EFAULT;
6236a727b43SJiri Slaby 	count -= res;
6246a727b43SJiri Slaby 	*ppos = pos + count;
6256a727b43SJiri Slaby 	return count;
6266a727b43SJiri Slaby }
6276a727b43SJiri Slaby 
6286a727b43SJiri Slaby /**
6296d1029b5SAkinobu Mita  * memory_read_from_buffer - copy data from the buffer
6306d1029b5SAkinobu Mita  * @to: the kernel space buffer to read to
6316d1029b5SAkinobu Mita  * @count: the maximum number of bytes to read
6326d1029b5SAkinobu Mita  * @ppos: the current position in the buffer
6336d1029b5SAkinobu Mita  * @from: the buffer to read from
6346d1029b5SAkinobu Mita  * @available: the size of the buffer
6356d1029b5SAkinobu Mita  *
6366d1029b5SAkinobu Mita  * The memory_read_from_buffer() function reads up to @count bytes from the
6376d1029b5SAkinobu Mita  * buffer @from at offset @ppos into the kernel space address starting at @to.
6386d1029b5SAkinobu Mita  *
6396d1029b5SAkinobu Mita  * On success, the number of bytes read is returned and the offset @ppos is
6406d1029b5SAkinobu Mita  * advanced by this number, or negative value is returned on error.
6416d1029b5SAkinobu Mita  **/
64293b07113SAkinobu Mita ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
64393b07113SAkinobu Mita 				const void *from, size_t available)
64493b07113SAkinobu Mita {
64593b07113SAkinobu Mita 	loff_t pos = *ppos;
64693b07113SAkinobu Mita 
64793b07113SAkinobu Mita 	if (pos < 0)
64893b07113SAkinobu Mita 		return -EINVAL;
64993b07113SAkinobu Mita 	if (pos >= available)
65093b07113SAkinobu Mita 		return 0;
65193b07113SAkinobu Mita 	if (count > available - pos)
65293b07113SAkinobu Mita 		count = available - pos;
65393b07113SAkinobu Mita 	memcpy(to, from + pos, count);
65493b07113SAkinobu Mita 	*ppos = pos + count;
65593b07113SAkinobu Mita 
65693b07113SAkinobu Mita 	return count;
65793b07113SAkinobu Mita }
65893b07113SAkinobu Mita 
6591da177e4SLinus Torvalds /*
6601da177e4SLinus Torvalds  * Transaction based IO.
6611da177e4SLinus Torvalds  * The file expects a single write which triggers the transaction, and then
6621da177e4SLinus Torvalds  * possibly a read which collects the result - which is stored in a
6631da177e4SLinus Torvalds  * file-local buffer.
6641da177e4SLinus Torvalds  */
66576791ab2SIngo Molnar 
66676791ab2SIngo Molnar void simple_transaction_set(struct file *file, size_t n)
66776791ab2SIngo Molnar {
66876791ab2SIngo Molnar 	struct simple_transaction_argresp *ar = file->private_data;
66976791ab2SIngo Molnar 
67076791ab2SIngo Molnar 	BUG_ON(n > SIMPLE_TRANSACTION_LIMIT);
67176791ab2SIngo Molnar 
67276791ab2SIngo Molnar 	/*
67376791ab2SIngo Molnar 	 * The barrier ensures that ar->size will really remain zero until
67476791ab2SIngo Molnar 	 * ar->data is ready for reading.
67576791ab2SIngo Molnar 	 */
67676791ab2SIngo Molnar 	smp_mb();
67776791ab2SIngo Molnar 	ar->size = n;
67876791ab2SIngo Molnar }
67976791ab2SIngo Molnar 
6801da177e4SLinus Torvalds char *simple_transaction_get(struct file *file, const char __user *buf, size_t size)
6811da177e4SLinus Torvalds {
6821da177e4SLinus Torvalds 	struct simple_transaction_argresp *ar;
6831da177e4SLinus Torvalds 	static DEFINE_SPINLOCK(simple_transaction_lock);
6841da177e4SLinus Torvalds 
6851da177e4SLinus Torvalds 	if (size > SIMPLE_TRANSACTION_LIMIT - 1)
6861da177e4SLinus Torvalds 		return ERR_PTR(-EFBIG);
6871da177e4SLinus Torvalds 
6881da177e4SLinus Torvalds 	ar = (struct simple_transaction_argresp *)get_zeroed_page(GFP_KERNEL);
6891da177e4SLinus Torvalds 	if (!ar)
6901da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
6911da177e4SLinus Torvalds 
6921da177e4SLinus Torvalds 	spin_lock(&simple_transaction_lock);
6931da177e4SLinus Torvalds 
6941da177e4SLinus Torvalds 	/* only one write allowed per open */
6951da177e4SLinus Torvalds 	if (file->private_data) {
6961da177e4SLinus Torvalds 		spin_unlock(&simple_transaction_lock);
6971da177e4SLinus Torvalds 		free_page((unsigned long)ar);
6981da177e4SLinus Torvalds 		return ERR_PTR(-EBUSY);
6991da177e4SLinus Torvalds 	}
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds 	file->private_data = ar;
7021da177e4SLinus Torvalds 
7031da177e4SLinus Torvalds 	spin_unlock(&simple_transaction_lock);
7041da177e4SLinus Torvalds 
7051da177e4SLinus Torvalds 	if (copy_from_user(ar->data, buf, size))
7061da177e4SLinus Torvalds 		return ERR_PTR(-EFAULT);
7071da177e4SLinus Torvalds 
7081da177e4SLinus Torvalds 	return ar->data;
7091da177e4SLinus Torvalds }
7101da177e4SLinus Torvalds 
7111da177e4SLinus Torvalds ssize_t simple_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
7121da177e4SLinus Torvalds {
7131da177e4SLinus Torvalds 	struct simple_transaction_argresp *ar = file->private_data;
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds 	if (!ar)
7161da177e4SLinus Torvalds 		return 0;
7171da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, size, pos, ar->data, ar->size);
7181da177e4SLinus Torvalds }
7191da177e4SLinus Torvalds 
7201da177e4SLinus Torvalds int simple_transaction_release(struct inode *inode, struct file *file)
7211da177e4SLinus Torvalds {
7221da177e4SLinus Torvalds 	free_page((unsigned long)file->private_data);
7231da177e4SLinus Torvalds 	return 0;
7241da177e4SLinus Torvalds }
7251da177e4SLinus Torvalds 
726acaefc25SArnd Bergmann /* Simple attribute files */
727acaefc25SArnd Bergmann 
728acaefc25SArnd Bergmann struct simple_attr {
7298b88b099SChristoph Hellwig 	int (*get)(void *, u64 *);
7308b88b099SChristoph Hellwig 	int (*set)(void *, u64);
731acaefc25SArnd Bergmann 	char get_buf[24];	/* enough to store a u64 and "\n\0" */
732acaefc25SArnd Bergmann 	char set_buf[24];
733acaefc25SArnd Bergmann 	void *data;
734acaefc25SArnd Bergmann 	const char *fmt;	/* format for read operation */
7357cf34c76SIngo Molnar 	struct mutex mutex;	/* protects access to these buffers */
736acaefc25SArnd Bergmann };
737acaefc25SArnd Bergmann 
738acaefc25SArnd Bergmann /* simple_attr_open is called by an actual attribute open file operation
739acaefc25SArnd Bergmann  * to set the attribute specific access operations. */
740acaefc25SArnd Bergmann int simple_attr_open(struct inode *inode, struct file *file,
7418b88b099SChristoph Hellwig 		     int (*get)(void *, u64 *), int (*set)(void *, u64),
742acaefc25SArnd Bergmann 		     const char *fmt)
743acaefc25SArnd Bergmann {
744acaefc25SArnd Bergmann 	struct simple_attr *attr;
745acaefc25SArnd Bergmann 
746acaefc25SArnd Bergmann 	attr = kmalloc(sizeof(*attr), GFP_KERNEL);
747acaefc25SArnd Bergmann 	if (!attr)
748acaefc25SArnd Bergmann 		return -ENOMEM;
749acaefc25SArnd Bergmann 
750acaefc25SArnd Bergmann 	attr->get = get;
751acaefc25SArnd Bergmann 	attr->set = set;
7528e18e294STheodore Ts'o 	attr->data = inode->i_private;
753acaefc25SArnd Bergmann 	attr->fmt = fmt;
7547cf34c76SIngo Molnar 	mutex_init(&attr->mutex);
755acaefc25SArnd Bergmann 
756acaefc25SArnd Bergmann 	file->private_data = attr;
757acaefc25SArnd Bergmann 
758acaefc25SArnd Bergmann 	return nonseekable_open(inode, file);
759acaefc25SArnd Bergmann }
760acaefc25SArnd Bergmann 
76174bedc4dSChristoph Hellwig int simple_attr_release(struct inode *inode, struct file *file)
762acaefc25SArnd Bergmann {
763acaefc25SArnd Bergmann 	kfree(file->private_data);
764acaefc25SArnd Bergmann 	return 0;
765acaefc25SArnd Bergmann }
766acaefc25SArnd Bergmann 
767acaefc25SArnd Bergmann /* read from the buffer that is filled with the get function */
768acaefc25SArnd Bergmann ssize_t simple_attr_read(struct file *file, char __user *buf,
769acaefc25SArnd Bergmann 			 size_t len, loff_t *ppos)
770acaefc25SArnd Bergmann {
771acaefc25SArnd Bergmann 	struct simple_attr *attr;
772acaefc25SArnd Bergmann 	size_t size;
773acaefc25SArnd Bergmann 	ssize_t ret;
774acaefc25SArnd Bergmann 
775acaefc25SArnd Bergmann 	attr = file->private_data;
776acaefc25SArnd Bergmann 
777acaefc25SArnd Bergmann 	if (!attr->get)
778acaefc25SArnd Bergmann 		return -EACCES;
779acaefc25SArnd Bergmann 
7809261303aSChristoph Hellwig 	ret = mutex_lock_interruptible(&attr->mutex);
7819261303aSChristoph Hellwig 	if (ret)
7829261303aSChristoph Hellwig 		return ret;
7839261303aSChristoph Hellwig 
7848b88b099SChristoph Hellwig 	if (*ppos) {		/* continued read */
785acaefc25SArnd Bergmann 		size = strlen(attr->get_buf);
7868b88b099SChristoph Hellwig 	} else {		/* first read */
7878b88b099SChristoph Hellwig 		u64 val;
7888b88b099SChristoph Hellwig 		ret = attr->get(attr->data, &val);
7898b88b099SChristoph Hellwig 		if (ret)
7908b88b099SChristoph Hellwig 			goto out;
7918b88b099SChristoph Hellwig 
792acaefc25SArnd Bergmann 		size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
7938b88b099SChristoph Hellwig 				 attr->fmt, (unsigned long long)val);
7948b88b099SChristoph Hellwig 	}
795acaefc25SArnd Bergmann 
796acaefc25SArnd Bergmann 	ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
7978b88b099SChristoph Hellwig out:
7987cf34c76SIngo Molnar 	mutex_unlock(&attr->mutex);
799acaefc25SArnd Bergmann 	return ret;
800acaefc25SArnd Bergmann }
801acaefc25SArnd Bergmann 
802acaefc25SArnd Bergmann /* interpret the buffer as a number to call the set function with */
803acaefc25SArnd Bergmann ssize_t simple_attr_write(struct file *file, const char __user *buf,
804acaefc25SArnd Bergmann 			  size_t len, loff_t *ppos)
805acaefc25SArnd Bergmann {
806acaefc25SArnd Bergmann 	struct simple_attr *attr;
807acaefc25SArnd Bergmann 	u64 val;
808acaefc25SArnd Bergmann 	size_t size;
809acaefc25SArnd Bergmann 	ssize_t ret;
810acaefc25SArnd Bergmann 
811acaefc25SArnd Bergmann 	attr = file->private_data;
812acaefc25SArnd Bergmann 	if (!attr->set)
813acaefc25SArnd Bergmann 		return -EACCES;
814acaefc25SArnd Bergmann 
8159261303aSChristoph Hellwig 	ret = mutex_lock_interruptible(&attr->mutex);
8169261303aSChristoph Hellwig 	if (ret)
8179261303aSChristoph Hellwig 		return ret;
8189261303aSChristoph Hellwig 
819acaefc25SArnd Bergmann 	ret = -EFAULT;
820acaefc25SArnd Bergmann 	size = min(sizeof(attr->set_buf) - 1, len);
821acaefc25SArnd Bergmann 	if (copy_from_user(attr->set_buf, buf, size))
822acaefc25SArnd Bergmann 		goto out;
823acaefc25SArnd Bergmann 
824acaefc25SArnd Bergmann 	attr->set_buf[size] = '\0';
825f7b88631SAkinobu Mita 	val = simple_strtoll(attr->set_buf, NULL, 0);
82605cc0ceeSWu Fengguang 	ret = attr->set(attr->data, val);
82705cc0ceeSWu Fengguang 	if (ret == 0)
82805cc0ceeSWu Fengguang 		ret = len; /* on success, claim we got the whole input */
829acaefc25SArnd Bergmann out:
8307cf34c76SIngo Molnar 	mutex_unlock(&attr->mutex);
831acaefc25SArnd Bergmann 	return ret;
832acaefc25SArnd Bergmann }
833acaefc25SArnd Bergmann 
8342596110aSChristoph Hellwig /**
8352596110aSChristoph Hellwig  * generic_fh_to_dentry - generic helper for the fh_to_dentry export operation
8362596110aSChristoph Hellwig  * @sb:		filesystem to do the file handle conversion on
8372596110aSChristoph Hellwig  * @fid:	file handle to convert
8382596110aSChristoph Hellwig  * @fh_len:	length of the file handle in bytes
8392596110aSChristoph Hellwig  * @fh_type:	type of file handle
8402596110aSChristoph Hellwig  * @get_inode:	filesystem callback to retrieve inode
8412596110aSChristoph Hellwig  *
8422596110aSChristoph Hellwig  * This function decodes @fid as long as it has one of the well-known
8432596110aSChristoph Hellwig  * Linux filehandle types and calls @get_inode on it to retrieve the
8442596110aSChristoph Hellwig  * inode for the object specified in the file handle.
8452596110aSChristoph Hellwig  */
8462596110aSChristoph Hellwig struct dentry *generic_fh_to_dentry(struct super_block *sb, struct fid *fid,
8472596110aSChristoph Hellwig 		int fh_len, int fh_type, struct inode *(*get_inode)
8482596110aSChristoph Hellwig 			(struct super_block *sb, u64 ino, u32 gen))
8492596110aSChristoph Hellwig {
8502596110aSChristoph Hellwig 	struct inode *inode = NULL;
8512596110aSChristoph Hellwig 
8522596110aSChristoph Hellwig 	if (fh_len < 2)
8532596110aSChristoph Hellwig 		return NULL;
8542596110aSChristoph Hellwig 
8552596110aSChristoph Hellwig 	switch (fh_type) {
8562596110aSChristoph Hellwig 	case FILEID_INO32_GEN:
8572596110aSChristoph Hellwig 	case FILEID_INO32_GEN_PARENT:
8582596110aSChristoph Hellwig 		inode = get_inode(sb, fid->i32.ino, fid->i32.gen);
8592596110aSChristoph Hellwig 		break;
8602596110aSChristoph Hellwig 	}
8612596110aSChristoph Hellwig 
8624ea3ada2SChristoph Hellwig 	return d_obtain_alias(inode);
8632596110aSChristoph Hellwig }
8642596110aSChristoph Hellwig EXPORT_SYMBOL_GPL(generic_fh_to_dentry);
8652596110aSChristoph Hellwig 
8662596110aSChristoph Hellwig /**
8672596110aSChristoph Hellwig  * generic_fh_to_dentry - generic helper for the fh_to_parent export operation
8682596110aSChristoph Hellwig  * @sb:		filesystem to do the file handle conversion on
8692596110aSChristoph Hellwig  * @fid:	file handle to convert
8702596110aSChristoph Hellwig  * @fh_len:	length of the file handle in bytes
8712596110aSChristoph Hellwig  * @fh_type:	type of file handle
8722596110aSChristoph Hellwig  * @get_inode:	filesystem callback to retrieve inode
8732596110aSChristoph Hellwig  *
8742596110aSChristoph Hellwig  * This function decodes @fid as long as it has one of the well-known
8752596110aSChristoph Hellwig  * Linux filehandle types and calls @get_inode on it to retrieve the
8762596110aSChristoph Hellwig  * inode for the _parent_ object specified in the file handle if it
8772596110aSChristoph Hellwig  * is specified in the file handle, or NULL otherwise.
8782596110aSChristoph Hellwig  */
8792596110aSChristoph Hellwig struct dentry *generic_fh_to_parent(struct super_block *sb, struct fid *fid,
8802596110aSChristoph Hellwig 		int fh_len, int fh_type, struct inode *(*get_inode)
8812596110aSChristoph Hellwig 			(struct super_block *sb, u64 ino, u32 gen))
8822596110aSChristoph Hellwig {
8832596110aSChristoph Hellwig 	struct inode *inode = NULL;
8842596110aSChristoph Hellwig 
8852596110aSChristoph Hellwig 	if (fh_len <= 2)
8862596110aSChristoph Hellwig 		return NULL;
8872596110aSChristoph Hellwig 
8882596110aSChristoph Hellwig 	switch (fh_type) {
8892596110aSChristoph Hellwig 	case FILEID_INO32_GEN_PARENT:
8902596110aSChristoph Hellwig 		inode = get_inode(sb, fid->i32.parent_ino,
8912596110aSChristoph Hellwig 				  (fh_len > 3 ? fid->i32.parent_gen : 0));
8922596110aSChristoph Hellwig 		break;
8932596110aSChristoph Hellwig 	}
8942596110aSChristoph Hellwig 
8954ea3ada2SChristoph Hellwig 	return d_obtain_alias(inode);
8962596110aSChristoph Hellwig }
8972596110aSChristoph Hellwig EXPORT_SYMBOL_GPL(generic_fh_to_parent);
8982596110aSChristoph Hellwig 
8991b061d92SChristoph Hellwig /**
9001b061d92SChristoph Hellwig  * generic_file_fsync - generic fsync implementation for simple filesystems
9011b061d92SChristoph Hellwig  * @file:	file to synchronize
9021b061d92SChristoph Hellwig  * @datasync:	only synchronize essential metadata if true
9031b061d92SChristoph Hellwig  *
9041b061d92SChristoph Hellwig  * This is a generic implementation of the fsync method for simple
9051b061d92SChristoph Hellwig  * filesystems which track all non-inode metadata in the buffers list
9061b061d92SChristoph Hellwig  * hanging off the address_space structure.
9071b061d92SChristoph Hellwig  */
90802c24a82SJosef Bacik int generic_file_fsync(struct file *file, loff_t start, loff_t end,
90902c24a82SJosef Bacik 		       int datasync)
910d5aacad5SAl Viro {
9117ea80859SChristoph Hellwig 	struct inode *inode = file->f_mapping->host;
912d5aacad5SAl Viro 	int err;
913d5aacad5SAl Viro 	int ret;
914d5aacad5SAl Viro 
91502c24a82SJosef Bacik 	err = filemap_write_and_wait_range(inode->i_mapping, start, end);
91602c24a82SJosef Bacik 	if (err)
91702c24a82SJosef Bacik 		return err;
91802c24a82SJosef Bacik 
91902c24a82SJosef Bacik 	mutex_lock(&inode->i_mutex);
920d5aacad5SAl Viro 	ret = sync_mapping_buffers(inode->i_mapping);
921d5aacad5SAl Viro 	if (!(inode->i_state & I_DIRTY))
92202c24a82SJosef Bacik 		goto out;
923d5aacad5SAl Viro 	if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
92402c24a82SJosef Bacik 		goto out;
925d5aacad5SAl Viro 
926c3765016SChristoph Hellwig 	err = sync_inode_metadata(inode, 1);
927d5aacad5SAl Viro 	if (ret == 0)
928d5aacad5SAl Viro 		ret = err;
92902c24a82SJosef Bacik out:
93002c24a82SJosef Bacik 	mutex_unlock(&inode->i_mutex);
931d5aacad5SAl Viro 	return ret;
932d5aacad5SAl Viro }
9331b061d92SChristoph Hellwig EXPORT_SYMBOL(generic_file_fsync);
9341b061d92SChristoph Hellwig 
93530ca22c7SPatrick J. LoPresti /**
93630ca22c7SPatrick J. LoPresti  * generic_check_addressable - Check addressability of file system
93730ca22c7SPatrick J. LoPresti  * @blocksize_bits:	log of file system block size
93830ca22c7SPatrick J. LoPresti  * @num_blocks:		number of blocks in file system
93930ca22c7SPatrick J. LoPresti  *
94030ca22c7SPatrick J. LoPresti  * Determine whether a file system with @num_blocks blocks (and a
94130ca22c7SPatrick J. LoPresti  * block size of 2**@blocksize_bits) is addressable by the sector_t
94230ca22c7SPatrick J. LoPresti  * and page cache of the system.  Return 0 if so and -EFBIG otherwise.
94330ca22c7SPatrick J. LoPresti  */
94430ca22c7SPatrick J. LoPresti int generic_check_addressable(unsigned blocksize_bits, u64 num_blocks)
94530ca22c7SPatrick J. LoPresti {
94630ca22c7SPatrick J. LoPresti 	u64 last_fs_block = num_blocks - 1;
947a33f13efSJoel Becker 	u64 last_fs_page =
948a33f13efSJoel Becker 		last_fs_block >> (PAGE_CACHE_SHIFT - blocksize_bits);
94930ca22c7SPatrick J. LoPresti 
95030ca22c7SPatrick J. LoPresti 	if (unlikely(num_blocks == 0))
95130ca22c7SPatrick J. LoPresti 		return 0;
95230ca22c7SPatrick J. LoPresti 
95330ca22c7SPatrick J. LoPresti 	if ((blocksize_bits < 9) || (blocksize_bits > PAGE_CACHE_SHIFT))
95430ca22c7SPatrick J. LoPresti 		return -EINVAL;
95530ca22c7SPatrick J. LoPresti 
956a33f13efSJoel Becker 	if ((last_fs_block > (sector_t)(~0ULL) >> (blocksize_bits - 9)) ||
957a33f13efSJoel Becker 	    (last_fs_page > (pgoff_t)(~0ULL))) {
95830ca22c7SPatrick J. LoPresti 		return -EFBIG;
95930ca22c7SPatrick J. LoPresti 	}
96030ca22c7SPatrick J. LoPresti 	return 0;
96130ca22c7SPatrick J. LoPresti }
96230ca22c7SPatrick J. LoPresti EXPORT_SYMBOL(generic_check_addressable);
96330ca22c7SPatrick J. LoPresti 
9641b061d92SChristoph Hellwig /*
9651b061d92SChristoph Hellwig  * No-op implementation of ->fsync for in-memory filesystems.
9661b061d92SChristoph Hellwig  */
96702c24a82SJosef Bacik int noop_fsync(struct file *file, loff_t start, loff_t end, int datasync)
9681b061d92SChristoph Hellwig {
9691b061d92SChristoph Hellwig 	return 0;
9701b061d92SChristoph Hellwig }
971d5aacad5SAl Viro 
9721da177e4SLinus Torvalds EXPORT_SYMBOL(dcache_dir_close);
9731da177e4SLinus Torvalds EXPORT_SYMBOL(dcache_dir_lseek);
9741da177e4SLinus Torvalds EXPORT_SYMBOL(dcache_dir_open);
9751da177e4SLinus Torvalds EXPORT_SYMBOL(dcache_readdir);
9761da177e4SLinus Torvalds EXPORT_SYMBOL(generic_read_dir);
97751139adaSAl Viro EXPORT_SYMBOL(mount_pseudo);
978afddba49SNick Piggin EXPORT_SYMBOL(simple_write_begin);
979afddba49SNick Piggin EXPORT_SYMBOL(simple_write_end);
9801da177e4SLinus Torvalds EXPORT_SYMBOL(simple_dir_inode_operations);
9811da177e4SLinus Torvalds EXPORT_SYMBOL(simple_dir_operations);
9821da177e4SLinus Torvalds EXPORT_SYMBOL(simple_empty);
9831da177e4SLinus Torvalds EXPORT_SYMBOL(simple_fill_super);
9841da177e4SLinus Torvalds EXPORT_SYMBOL(simple_getattr);
9851da177e4SLinus Torvalds EXPORT_SYMBOL(simple_link);
9861da177e4SLinus Torvalds EXPORT_SYMBOL(simple_lookup);
9871da177e4SLinus Torvalds EXPORT_SYMBOL(simple_pin_fs);
9881da177e4SLinus Torvalds EXPORT_SYMBOL(simple_readpage);
9891da177e4SLinus Torvalds EXPORT_SYMBOL(simple_release_fs);
9901da177e4SLinus Torvalds EXPORT_SYMBOL(simple_rename);
9911da177e4SLinus Torvalds EXPORT_SYMBOL(simple_rmdir);
9921da177e4SLinus Torvalds EXPORT_SYMBOL(simple_statfs);
9931b061d92SChristoph Hellwig EXPORT_SYMBOL(noop_fsync);
9941da177e4SLinus Torvalds EXPORT_SYMBOL(simple_unlink);
9951da177e4SLinus Torvalds EXPORT_SYMBOL(simple_read_from_buffer);
9966a727b43SJiri Slaby EXPORT_SYMBOL(simple_write_to_buffer);
99793b07113SAkinobu Mita EXPORT_SYMBOL(memory_read_from_buffer);
99876791ab2SIngo Molnar EXPORT_SYMBOL(simple_transaction_set);
9991da177e4SLinus Torvalds EXPORT_SYMBOL(simple_transaction_get);
10001da177e4SLinus Torvalds EXPORT_SYMBOL(simple_transaction_read);
10011da177e4SLinus Torvalds EXPORT_SYMBOL(simple_transaction_release);
1002acaefc25SArnd Bergmann EXPORT_SYMBOL_GPL(simple_attr_open);
100374bedc4dSChristoph Hellwig EXPORT_SYMBOL_GPL(simple_attr_release);
1004acaefc25SArnd Bergmann EXPORT_SYMBOL_GPL(simple_attr_read);
1005acaefc25SArnd Bergmann EXPORT_SYMBOL_GPL(simple_attr_write);
1006