xref: /openbmc/linux/fs/libfs.c (revision 2fd6b7f5)
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 
19da502956SNick Piggin static inline int simple_positive(struct dentry *dentry)
20da502956SNick Piggin {
21da502956SNick Piggin 	return dentry->d_inode && !d_unhashed(dentry);
22da502956SNick Piggin }
23da502956SNick Piggin 
241da177e4SLinus Torvalds int simple_getattr(struct vfsmount *mnt, struct dentry *dentry,
251da177e4SLinus Torvalds 		   struct kstat *stat)
261da177e4SLinus Torvalds {
271da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
281da177e4SLinus Torvalds 	generic_fillattr(inode, stat);
291da177e4SLinus Torvalds 	stat->blocks = inode->i_mapping->nrpages << (PAGE_CACHE_SHIFT - 9);
301da177e4SLinus Torvalds 	return 0;
311da177e4SLinus Torvalds }
321da177e4SLinus Torvalds 
33726c3342SDavid Howells int simple_statfs(struct dentry *dentry, struct kstatfs *buf)
341da177e4SLinus Torvalds {
35726c3342SDavid Howells 	buf->f_type = dentry->d_sb->s_magic;
361da177e4SLinus Torvalds 	buf->f_bsize = PAGE_CACHE_SIZE;
371da177e4SLinus Torvalds 	buf->f_namelen = NAME_MAX;
381da177e4SLinus Torvalds 	return 0;
391da177e4SLinus Torvalds }
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds /*
421da177e4SLinus Torvalds  * Retaining negative dentries for an in-memory filesystem just wastes
431da177e4SLinus Torvalds  * memory and lookup time: arrange for them to be deleted immediately.
441da177e4SLinus Torvalds  */
45fe15ce44SNick Piggin static int simple_delete_dentry(const struct dentry *dentry)
461da177e4SLinus Torvalds {
471da177e4SLinus Torvalds 	return 1;
481da177e4SLinus Torvalds }
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds /*
511da177e4SLinus Torvalds  * Lookup the data. This is trivial - if the dentry didn't already
521da177e4SLinus Torvalds  * exist, we know it is negative.  Set d_op to delete negative dentries.
531da177e4SLinus Torvalds  */
541da177e4SLinus Torvalds struct dentry *simple_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
551da177e4SLinus Torvalds {
563ba13d17SAl Viro 	static const struct dentry_operations simple_dentry_operations = {
571da177e4SLinus Torvalds 		.d_delete = simple_delete_dentry,
581da177e4SLinus Torvalds 	};
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds 	if (dentry->d_name.len > NAME_MAX)
611da177e4SLinus Torvalds 		return ERR_PTR(-ENAMETOOLONG);
621da177e4SLinus Torvalds 	dentry->d_op = &simple_dentry_operations;
631da177e4SLinus Torvalds 	d_add(dentry, NULL);
641da177e4SLinus Torvalds 	return NULL;
651da177e4SLinus Torvalds }
661da177e4SLinus Torvalds 
671da177e4SLinus Torvalds int dcache_dir_open(struct inode *inode, struct file *file)
681da177e4SLinus Torvalds {
691da177e4SLinus Torvalds 	static struct qstr cursor_name = {.len = 1, .name = "."};
701da177e4SLinus Torvalds 
710f7fc9e4SJosef "Jeff" Sipek 	file->private_data = d_alloc(file->f_path.dentry, &cursor_name);
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds 	return file->private_data ? 0 : -ENOMEM;
741da177e4SLinus Torvalds }
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds int dcache_dir_close(struct inode *inode, struct file *file)
771da177e4SLinus Torvalds {
781da177e4SLinus Torvalds 	dput(file->private_data);
791da177e4SLinus Torvalds 	return 0;
801da177e4SLinus Torvalds }
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds loff_t dcache_dir_lseek(struct file *file, loff_t offset, int origin)
831da177e4SLinus Torvalds {
842fd6b7f5SNick Piggin 	struct dentry *dentry = file->f_path.dentry;
852fd6b7f5SNick Piggin 	mutex_lock(&dentry->d_inode->i_mutex);
861da177e4SLinus Torvalds 	switch (origin) {
871da177e4SLinus Torvalds 		case 1:
881da177e4SLinus Torvalds 			offset += file->f_pos;
891da177e4SLinus Torvalds 		case 0:
901da177e4SLinus Torvalds 			if (offset >= 0)
911da177e4SLinus Torvalds 				break;
921da177e4SLinus Torvalds 		default:
932fd6b7f5SNick Piggin 			mutex_unlock(&dentry->d_inode->i_mutex);
941da177e4SLinus Torvalds 			return -EINVAL;
951da177e4SLinus Torvalds 	}
961da177e4SLinus Torvalds 	if (offset != file->f_pos) {
971da177e4SLinus Torvalds 		file->f_pos = offset;
981da177e4SLinus Torvalds 		if (file->f_pos >= 2) {
991da177e4SLinus Torvalds 			struct list_head *p;
1001da177e4SLinus Torvalds 			struct dentry *cursor = file->private_data;
1011da177e4SLinus Torvalds 			loff_t n = file->f_pos - 2;
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds 			spin_lock(&dcache_lock);
1042fd6b7f5SNick Piggin 			spin_lock(&dentry->d_lock);
1052fd6b7f5SNick Piggin 			/* d_lock not required for cursor */
1065160ee6fSEric Dumazet 			list_del(&cursor->d_u.d_child);
1072fd6b7f5SNick Piggin 			p = dentry->d_subdirs.next;
1082fd6b7f5SNick Piggin 			while (n && p != &dentry->d_subdirs) {
1091da177e4SLinus Torvalds 				struct dentry *next;
1105160ee6fSEric Dumazet 				next = list_entry(p, struct dentry, d_u.d_child);
1112fd6b7f5SNick Piggin 				spin_lock_nested(&next->d_lock, DENTRY_D_LOCK_NESTED);
112da502956SNick Piggin 				if (simple_positive(next))
1131da177e4SLinus Torvalds 					n--;
114da502956SNick Piggin 				spin_unlock(&next->d_lock);
1151da177e4SLinus Torvalds 				p = p->next;
1161da177e4SLinus Torvalds 			}
1175160ee6fSEric Dumazet 			list_add_tail(&cursor->d_u.d_child, p);
1182fd6b7f5SNick Piggin 			spin_unlock(&dentry->d_lock);
1191da177e4SLinus Torvalds 			spin_unlock(&dcache_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:
1621da177e4SLinus Torvalds 			spin_lock(&dcache_lock);
1632fd6b7f5SNick Piggin 			spin_lock(&dentry->d_lock);
1641bfba4e8SAkinobu Mita 			if (filp->f_pos == 2)
1651bfba4e8SAkinobu Mita 				list_move(q, &dentry->d_subdirs);
1661bfba4e8SAkinobu Mita 
1671da177e4SLinus Torvalds 			for (p=q->next; p != &dentry->d_subdirs; p=p->next) {
1681da177e4SLinus Torvalds 				struct dentry *next;
1695160ee6fSEric Dumazet 				next = list_entry(p, struct dentry, d_u.d_child);
170da502956SNick Piggin 				spin_lock_nested(&next->d_lock, DENTRY_D_LOCK_NESTED);
171da502956SNick Piggin 				if (!simple_positive(next)) {
172da502956SNick Piggin 					spin_unlock(&next->d_lock);
1731da177e4SLinus Torvalds 					continue;
174da502956SNick Piggin 				}
1751da177e4SLinus Torvalds 
176da502956SNick Piggin 				spin_unlock(&next->d_lock);
1772fd6b7f5SNick Piggin 				spin_unlock(&dentry->d_lock);
1781da177e4SLinus Torvalds 				spin_unlock(&dcache_lock);
1790f8952c2SRonni Nielsen 				if (filldir(dirent, next->d_name.name,
1800f8952c2SRonni Nielsen 					    next->d_name.len, filp->f_pos,
1810f8952c2SRonni Nielsen 					    next->d_inode->i_ino,
1820f8952c2SRonni Nielsen 					    dt_type(next->d_inode)) < 0)
1831da177e4SLinus Torvalds 					return 0;
1841da177e4SLinus Torvalds 				spin_lock(&dcache_lock);
1852fd6b7f5SNick Piggin 				spin_lock(&dentry->d_lock);
1862fd6b7f5SNick Piggin 				spin_lock_nested(&next->d_lock, DENTRY_D_LOCK_NESTED);
1871da177e4SLinus Torvalds 				/* next is still alive */
1881bfba4e8SAkinobu Mita 				list_move(q, p);
1892fd6b7f5SNick Piggin 				spin_unlock(&next->d_lock);
1901da177e4SLinus Torvalds 				p = q;
1911da177e4SLinus Torvalds 				filp->f_pos++;
1921da177e4SLinus Torvalds 			}
1932fd6b7f5SNick Piggin 			spin_unlock(&dentry->d_lock);
1941da177e4SLinus Torvalds 			spin_unlock(&dcache_lock);
1951da177e4SLinus Torvalds 	}
1961da177e4SLinus Torvalds 	return 0;
1971da177e4SLinus Torvalds }
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds ssize_t generic_read_dir(struct file *filp, char __user *buf, size_t siz, loff_t *ppos)
2001da177e4SLinus Torvalds {
2011da177e4SLinus Torvalds 	return -EISDIR;
2021da177e4SLinus Torvalds }
2031da177e4SLinus Torvalds 
2044b6f5d20SArjan van de Ven const struct file_operations simple_dir_operations = {
2051da177e4SLinus Torvalds 	.open		= dcache_dir_open,
2061da177e4SLinus Torvalds 	.release	= dcache_dir_close,
2071da177e4SLinus Torvalds 	.llseek		= dcache_dir_lseek,
2081da177e4SLinus Torvalds 	.read		= generic_read_dir,
2091da177e4SLinus Torvalds 	.readdir	= dcache_readdir,
2101b061d92SChristoph Hellwig 	.fsync		= noop_fsync,
2111da177e4SLinus Torvalds };
2121da177e4SLinus Torvalds 
21392e1d5beSArjan van de Ven const struct inode_operations simple_dir_inode_operations = {
2141da177e4SLinus Torvalds 	.lookup		= simple_lookup,
2151da177e4SLinus Torvalds };
2161da177e4SLinus Torvalds 
217759b9775SHugh Dickins static const struct super_operations simple_super_operations = {
218759b9775SHugh Dickins 	.statfs		= simple_statfs,
219759b9775SHugh Dickins };
220759b9775SHugh Dickins 
2211da177e4SLinus Torvalds /*
2221da177e4SLinus Torvalds  * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that
2231da177e4SLinus Torvalds  * will never be mountable)
2241da177e4SLinus Torvalds  */
22551139adaSAl Viro struct dentry *mount_pseudo(struct file_system_type *fs_type, char *name,
22651139adaSAl Viro 	const struct super_operations *ops, unsigned long magic)
2271da177e4SLinus Torvalds {
2281da177e4SLinus Torvalds 	struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
2291da177e4SLinus Torvalds 	struct dentry *dentry;
2301da177e4SLinus Torvalds 	struct inode *root;
2311da177e4SLinus Torvalds 	struct qstr d_name = {.name = name, .len = strlen(name)};
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds 	if (IS_ERR(s))
23451139adaSAl Viro 		return ERR_CAST(s);
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds 	s->s_flags = MS_NOUSER;
23789a4eb4bSJeff Layton 	s->s_maxbytes = MAX_LFS_FILESIZE;
2383971e1a9SAlex Nixon 	s->s_blocksize = PAGE_SIZE;
2393971e1a9SAlex Nixon 	s->s_blocksize_bits = PAGE_SHIFT;
2401da177e4SLinus Torvalds 	s->s_magic = magic;
241759b9775SHugh Dickins 	s->s_op = ops ? ops : &simple_super_operations;
2421da177e4SLinus Torvalds 	s->s_time_gran = 1;
2431da177e4SLinus Torvalds 	root = new_inode(s);
2441da177e4SLinus Torvalds 	if (!root)
2451da177e4SLinus Torvalds 		goto Enomem;
2461a1c9bb4SJeff Layton 	/*
2471a1c9bb4SJeff Layton 	 * since this is the first inode, make it number 1. New inodes created
2481a1c9bb4SJeff Layton 	 * after this must take care not to collide with it (by passing
2491a1c9bb4SJeff Layton 	 * max_reserved of 1 to iunique).
2501a1c9bb4SJeff Layton 	 */
2511a1c9bb4SJeff Layton 	root->i_ino = 1;
2521da177e4SLinus Torvalds 	root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR;
2531da177e4SLinus Torvalds 	root->i_atime = root->i_mtime = root->i_ctime = CURRENT_TIME;
2541da177e4SLinus Torvalds 	dentry = d_alloc(NULL, &d_name);
2551da177e4SLinus Torvalds 	if (!dentry) {
2561da177e4SLinus Torvalds 		iput(root);
2571da177e4SLinus Torvalds 		goto Enomem;
2581da177e4SLinus Torvalds 	}
2591da177e4SLinus Torvalds 	dentry->d_sb = s;
2601da177e4SLinus Torvalds 	dentry->d_parent = dentry;
2611da177e4SLinus Torvalds 	d_instantiate(dentry, root);
2621da177e4SLinus Torvalds 	s->s_root = dentry;
2631da177e4SLinus Torvalds 	s->s_flags |= MS_ACTIVE;
26451139adaSAl Viro 	return dget(s->s_root);
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds Enomem:
2676f5bbff9SAl Viro 	deactivate_locked_super(s);
26851139adaSAl Viro 	return ERR_PTR(-ENOMEM);
2691da177e4SLinus Torvalds }
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
2721da177e4SLinus Torvalds {
2731da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 	inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
276d8c76e6fSDave Hansen 	inc_nlink(inode);
2777de9c6eeSAl Viro 	ihold(inode);
2781da177e4SLinus Torvalds 	dget(dentry);
2791da177e4SLinus Torvalds 	d_instantiate(dentry, inode);
2801da177e4SLinus Torvalds 	return 0;
2811da177e4SLinus Torvalds }
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds int simple_empty(struct dentry *dentry)
2841da177e4SLinus Torvalds {
2851da177e4SLinus Torvalds 	struct dentry *child;
2861da177e4SLinus Torvalds 	int ret = 0;
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds 	spin_lock(&dcache_lock);
2892fd6b7f5SNick Piggin 	spin_lock(&dentry->d_lock);
290da502956SNick Piggin 	list_for_each_entry(child, &dentry->d_subdirs, d_u.d_child) {
291da502956SNick Piggin 		spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED);
292da502956SNick Piggin 		if (simple_positive(child)) {
293da502956SNick Piggin 			spin_unlock(&child->d_lock);
2941da177e4SLinus Torvalds 			goto out;
295da502956SNick Piggin 		}
296da502956SNick Piggin 		spin_unlock(&child->d_lock);
297da502956SNick Piggin 	}
2981da177e4SLinus Torvalds 	ret = 1;
2991da177e4SLinus Torvalds out:
3002fd6b7f5SNick Piggin 	spin_unlock(&dentry->d_lock);
3011da177e4SLinus Torvalds 	spin_unlock(&dcache_lock);
3021da177e4SLinus Torvalds 	return ret;
3031da177e4SLinus Torvalds }
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds int simple_unlink(struct inode *dir, struct dentry *dentry)
3061da177e4SLinus Torvalds {
3071da177e4SLinus Torvalds 	struct inode *inode = dentry->d_inode;
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds 	inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
3109a53c3a7SDave Hansen 	drop_nlink(inode);
3111da177e4SLinus Torvalds 	dput(dentry);
3121da177e4SLinus Torvalds 	return 0;
3131da177e4SLinus Torvalds }
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds int simple_rmdir(struct inode *dir, struct dentry *dentry)
3161da177e4SLinus Torvalds {
3171da177e4SLinus Torvalds 	if (!simple_empty(dentry))
3181da177e4SLinus Torvalds 		return -ENOTEMPTY;
3191da177e4SLinus Torvalds 
3209a53c3a7SDave Hansen 	drop_nlink(dentry->d_inode);
3211da177e4SLinus Torvalds 	simple_unlink(dir, dentry);
3229a53c3a7SDave Hansen 	drop_nlink(dir);
3231da177e4SLinus Torvalds 	return 0;
3241da177e4SLinus Torvalds }
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds int simple_rename(struct inode *old_dir, struct dentry *old_dentry,
3271da177e4SLinus Torvalds 		struct inode *new_dir, struct dentry *new_dentry)
3281da177e4SLinus Torvalds {
3291da177e4SLinus Torvalds 	struct inode *inode = old_dentry->d_inode;
3301da177e4SLinus Torvalds 	int they_are_dirs = S_ISDIR(old_dentry->d_inode->i_mode);
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds 	if (!simple_empty(new_dentry))
3331da177e4SLinus Torvalds 		return -ENOTEMPTY;
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 	if (new_dentry->d_inode) {
3361da177e4SLinus Torvalds 		simple_unlink(new_dir, new_dentry);
3371da177e4SLinus Torvalds 		if (they_are_dirs)
3389a53c3a7SDave Hansen 			drop_nlink(old_dir);
3391da177e4SLinus Torvalds 	} else if (they_are_dirs) {
3409a53c3a7SDave Hansen 		drop_nlink(old_dir);
341d8c76e6fSDave Hansen 		inc_nlink(new_dir);
3421da177e4SLinus Torvalds 	}
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds 	old_dir->i_ctime = old_dir->i_mtime = new_dir->i_ctime =
3451da177e4SLinus Torvalds 		new_dir->i_mtime = inode->i_ctime = CURRENT_TIME;
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds 	return 0;
3481da177e4SLinus Torvalds }
3491da177e4SLinus Torvalds 
3507bb46a67Snpiggin@suse.de /**
351eef2380cSChristoph Hellwig  * simple_setattr - setattr for simple filesystem
3527bb46a67Snpiggin@suse.de  * @dentry: dentry
3537bb46a67Snpiggin@suse.de  * @iattr: iattr structure
3547bb46a67Snpiggin@suse.de  *
3557bb46a67Snpiggin@suse.de  * Returns 0 on success, -error on failure.
3567bb46a67Snpiggin@suse.de  *
357eef2380cSChristoph Hellwig  * simple_setattr is a simple ->setattr implementation without a proper
358eef2380cSChristoph Hellwig  * implementation of size changes.
359eef2380cSChristoph Hellwig  *
360eef2380cSChristoph Hellwig  * It can either be used for in-memory filesystems or special files
361eef2380cSChristoph Hellwig  * on simple regular filesystems.  Anything that needs to change on-disk
362eef2380cSChristoph Hellwig  * or wire state on size changes needs its own setattr method.
3637bb46a67Snpiggin@suse.de  */
3647bb46a67Snpiggin@suse.de int simple_setattr(struct dentry *dentry, struct iattr *iattr)
3657bb46a67Snpiggin@suse.de {
3667bb46a67Snpiggin@suse.de 	struct inode *inode = dentry->d_inode;
3677bb46a67Snpiggin@suse.de 	int error;
3687bb46a67Snpiggin@suse.de 
369eef2380cSChristoph Hellwig 	WARN_ON_ONCE(inode->i_op->truncate);
370eef2380cSChristoph Hellwig 
3717bb46a67Snpiggin@suse.de 	error = inode_change_ok(inode, iattr);
3727bb46a67Snpiggin@suse.de 	if (error)
3737bb46a67Snpiggin@suse.de 		return error;
3747bb46a67Snpiggin@suse.de 
3752c27c65eSChristoph Hellwig 	if (iattr->ia_valid & ATTR_SIZE)
3762c27c65eSChristoph Hellwig 		truncate_setsize(inode, iattr->ia_size);
3776a1a90adSChristoph Hellwig 	setattr_copy(inode, iattr);
378eef2380cSChristoph Hellwig 	mark_inode_dirty(inode);
379eef2380cSChristoph Hellwig 	return 0;
3807bb46a67Snpiggin@suse.de }
3817bb46a67Snpiggin@suse.de EXPORT_SYMBOL(simple_setattr);
3827bb46a67Snpiggin@suse.de 
3831da177e4SLinus Torvalds int simple_readpage(struct file *file, struct page *page)
3841da177e4SLinus Torvalds {
385c0d92cbcSPekka J Enberg 	clear_highpage(page);
3861da177e4SLinus Torvalds 	flush_dcache_page(page);
3871da177e4SLinus Torvalds 	SetPageUptodate(page);
3881da177e4SLinus Torvalds 	unlock_page(page);
3891da177e4SLinus Torvalds 	return 0;
3901da177e4SLinus Torvalds }
3911da177e4SLinus Torvalds 
392afddba49SNick Piggin int simple_write_begin(struct file *file, struct address_space *mapping,
393afddba49SNick Piggin 			loff_t pos, unsigned len, unsigned flags,
394afddba49SNick Piggin 			struct page **pagep, void **fsdata)
395afddba49SNick Piggin {
396afddba49SNick Piggin 	struct page *page;
397afddba49SNick Piggin 	pgoff_t index;
398afddba49SNick Piggin 
399afddba49SNick Piggin 	index = pos >> PAGE_CACHE_SHIFT;
400afddba49SNick Piggin 
40154566b2cSNick Piggin 	page = grab_cache_page_write_begin(mapping, index, flags);
402afddba49SNick Piggin 	if (!page)
403afddba49SNick Piggin 		return -ENOMEM;
404afddba49SNick Piggin 
405afddba49SNick Piggin 	*pagep = page;
406afddba49SNick Piggin 
407193cf4b9SBoaz Harrosh 	if (!PageUptodate(page) && (len != PAGE_CACHE_SIZE)) {
408193cf4b9SBoaz Harrosh 		unsigned from = pos & (PAGE_CACHE_SIZE - 1);
409193cf4b9SBoaz Harrosh 
410193cf4b9SBoaz Harrosh 		zero_user_segments(page, 0, from, from + len, PAGE_CACHE_SIZE);
411193cf4b9SBoaz Harrosh 	}
412193cf4b9SBoaz Harrosh 	return 0;
413afddba49SNick Piggin }
414afddba49SNick Piggin 
415ad2a722fSBoaz Harrosh /**
416ad2a722fSBoaz Harrosh  * simple_write_end - .write_end helper for non-block-device FSes
417ad2a722fSBoaz Harrosh  * @available: See .write_end of address_space_operations
418ad2a722fSBoaz Harrosh  * @file: 		"
419ad2a722fSBoaz Harrosh  * @mapping: 		"
420ad2a722fSBoaz Harrosh  * @pos: 		"
421ad2a722fSBoaz Harrosh  * @len: 		"
422ad2a722fSBoaz Harrosh  * @copied: 		"
423ad2a722fSBoaz Harrosh  * @page: 		"
424ad2a722fSBoaz Harrosh  * @fsdata: 		"
425ad2a722fSBoaz Harrosh  *
426ad2a722fSBoaz Harrosh  * simple_write_end does the minimum needed for updating a page after writing is
427ad2a722fSBoaz Harrosh  * done. It has the same API signature as the .write_end of
428ad2a722fSBoaz Harrosh  * address_space_operations vector. So it can just be set onto .write_end for
429ad2a722fSBoaz Harrosh  * FSes that don't need any other processing. i_mutex is assumed to be held.
430ad2a722fSBoaz Harrosh  * Block based filesystems should use generic_write_end().
431ad2a722fSBoaz Harrosh  * NOTE: Even though i_size might get updated by this function, mark_inode_dirty
432ad2a722fSBoaz Harrosh  * is not called, so a filesystem that actually does store data in .write_inode
433ad2a722fSBoaz Harrosh  * should extend on what's done here with a call to mark_inode_dirty() in the
434ad2a722fSBoaz Harrosh  * case that i_size has changed.
435ad2a722fSBoaz Harrosh  */
436ad2a722fSBoaz Harrosh int simple_write_end(struct file *file, struct address_space *mapping,
437ad2a722fSBoaz Harrosh 			loff_t pos, unsigned len, unsigned copied,
438ad2a722fSBoaz Harrosh 			struct page *page, void *fsdata)
4391da177e4SLinus Torvalds {
4401da177e4SLinus Torvalds 	struct inode *inode = page->mapping->host;
441ad2a722fSBoaz Harrosh 	loff_t last_pos = pos + copied;
442ad2a722fSBoaz Harrosh 
443ad2a722fSBoaz Harrosh 	/* zero the stale part of the page if we did a short copy */
444ad2a722fSBoaz Harrosh 	if (copied < len) {
445ad2a722fSBoaz Harrosh 		unsigned from = pos & (PAGE_CACHE_SIZE - 1);
446ad2a722fSBoaz Harrosh 
447ad2a722fSBoaz Harrosh 		zero_user(page, from + copied, len - copied);
448ad2a722fSBoaz Harrosh 	}
4491da177e4SLinus Torvalds 
450955eff5aSNick Piggin 	if (!PageUptodate(page))
451955eff5aSNick Piggin 		SetPageUptodate(page);
4521da177e4SLinus Torvalds 	/*
4531da177e4SLinus Torvalds 	 * No need to use i_size_read() here, the i_size
4541b1dcc1bSJes Sorensen 	 * cannot change under us because we hold the i_mutex.
4551da177e4SLinus Torvalds 	 */
456ad2a722fSBoaz Harrosh 	if (last_pos > inode->i_size)
457ad2a722fSBoaz Harrosh 		i_size_write(inode, last_pos);
458ad2a722fSBoaz Harrosh 
4591da177e4SLinus Torvalds 	set_page_dirty(page);
460afddba49SNick Piggin 	unlock_page(page);
461afddba49SNick Piggin 	page_cache_release(page);
462afddba49SNick Piggin 
463afddba49SNick Piggin 	return copied;
464afddba49SNick Piggin }
465afddba49SNick Piggin 
4661a1c9bb4SJeff Layton /*
4671a1c9bb4SJeff Layton  * the inodes created here are not hashed. If you use iunique to generate
4681a1c9bb4SJeff Layton  * unique inode values later for this filesystem, then you must take care
4691a1c9bb4SJeff Layton  * to pass it an appropriate max_reserved value to avoid collisions.
4701a1c9bb4SJeff Layton  */
4717d683a09SRoberto Sassu int simple_fill_super(struct super_block *s, unsigned long magic,
4727d683a09SRoberto Sassu 		      struct tree_descr *files)
4731da177e4SLinus Torvalds {
4741da177e4SLinus Torvalds 	struct inode *inode;
4751da177e4SLinus Torvalds 	struct dentry *root;
4761da177e4SLinus Torvalds 	struct dentry *dentry;
4771da177e4SLinus Torvalds 	int i;
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds 	s->s_blocksize = PAGE_CACHE_SIZE;
4801da177e4SLinus Torvalds 	s->s_blocksize_bits = PAGE_CACHE_SHIFT;
4811da177e4SLinus Torvalds 	s->s_magic = magic;
482759b9775SHugh Dickins 	s->s_op = &simple_super_operations;
4831da177e4SLinus Torvalds 	s->s_time_gran = 1;
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds 	inode = new_inode(s);
4861da177e4SLinus Torvalds 	if (!inode)
4871da177e4SLinus Torvalds 		return -ENOMEM;
4881a1c9bb4SJeff Layton 	/*
4891a1c9bb4SJeff Layton 	 * because the root inode is 1, the files array must not contain an
4901a1c9bb4SJeff Layton 	 * entry at index 1
4911a1c9bb4SJeff Layton 	 */
4921a1c9bb4SJeff Layton 	inode->i_ino = 1;
4931da177e4SLinus Torvalds 	inode->i_mode = S_IFDIR | 0755;
4941da177e4SLinus Torvalds 	inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
4951da177e4SLinus Torvalds 	inode->i_op = &simple_dir_inode_operations;
4961da177e4SLinus Torvalds 	inode->i_fop = &simple_dir_operations;
4977656f328SVincent Hanquez 	inode->i_nlink = 2;
4981da177e4SLinus Torvalds 	root = d_alloc_root(inode);
4991da177e4SLinus Torvalds 	if (!root) {
5001da177e4SLinus Torvalds 		iput(inode);
5011da177e4SLinus Torvalds 		return -ENOMEM;
5021da177e4SLinus Torvalds 	}
5031da177e4SLinus Torvalds 	for (i = 0; !files->name || files->name[0]; i++, files++) {
5041da177e4SLinus Torvalds 		if (!files->name)
5051da177e4SLinus Torvalds 			continue;
5061a1c9bb4SJeff Layton 
5071a1c9bb4SJeff Layton 		/* warn if it tries to conflict with the root inode */
5081a1c9bb4SJeff Layton 		if (unlikely(i == 1))
5091a1c9bb4SJeff Layton 			printk(KERN_WARNING "%s: %s passed in a files array"
5101a1c9bb4SJeff Layton 				"with an index of 1!\n", __func__,
5111a1c9bb4SJeff Layton 				s->s_type->name);
5121a1c9bb4SJeff Layton 
5131da177e4SLinus Torvalds 		dentry = d_alloc_name(root, files->name);
5141da177e4SLinus Torvalds 		if (!dentry)
5151da177e4SLinus Torvalds 			goto out;
5161da177e4SLinus Torvalds 		inode = new_inode(s);
5171da177e4SLinus Torvalds 		if (!inode)
5181da177e4SLinus Torvalds 			goto out;
5191da177e4SLinus Torvalds 		inode->i_mode = S_IFREG | files->mode;
5201da177e4SLinus Torvalds 		inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
5211da177e4SLinus Torvalds 		inode->i_fop = files->ops;
5221da177e4SLinus Torvalds 		inode->i_ino = i;
5231da177e4SLinus Torvalds 		d_add(dentry, inode);
5241da177e4SLinus Torvalds 	}
5251da177e4SLinus Torvalds 	s->s_root = root;
5261da177e4SLinus Torvalds 	return 0;
5271da177e4SLinus Torvalds out:
5281da177e4SLinus Torvalds 	d_genocide(root);
5291da177e4SLinus Torvalds 	dput(root);
5301da177e4SLinus Torvalds 	return -ENOMEM;
5311da177e4SLinus Torvalds }
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds static DEFINE_SPINLOCK(pin_fs_lock);
5341da177e4SLinus Torvalds 
5351f5ce9e9STrond Myklebust int simple_pin_fs(struct file_system_type *type, struct vfsmount **mount, int *count)
5361da177e4SLinus Torvalds {
5371da177e4SLinus Torvalds 	struct vfsmount *mnt = NULL;
5381da177e4SLinus Torvalds 	spin_lock(&pin_fs_lock);
5391da177e4SLinus Torvalds 	if (unlikely(!*mount)) {
5401da177e4SLinus Torvalds 		spin_unlock(&pin_fs_lock);
5411f5ce9e9STrond Myklebust 		mnt = vfs_kern_mount(type, 0, type->name, NULL);
5421da177e4SLinus Torvalds 		if (IS_ERR(mnt))
5431da177e4SLinus Torvalds 			return PTR_ERR(mnt);
5441da177e4SLinus Torvalds 		spin_lock(&pin_fs_lock);
5451da177e4SLinus Torvalds 		if (!*mount)
5461da177e4SLinus Torvalds 			*mount = mnt;
5471da177e4SLinus Torvalds 	}
5481da177e4SLinus Torvalds 	mntget(*mount);
5491da177e4SLinus Torvalds 	++*count;
5501da177e4SLinus Torvalds 	spin_unlock(&pin_fs_lock);
5511da177e4SLinus Torvalds 	mntput(mnt);
5521da177e4SLinus Torvalds 	return 0;
5531da177e4SLinus Torvalds }
5541da177e4SLinus Torvalds 
5551da177e4SLinus Torvalds void simple_release_fs(struct vfsmount **mount, int *count)
5561da177e4SLinus Torvalds {
5571da177e4SLinus Torvalds 	struct vfsmount *mnt;
5581da177e4SLinus Torvalds 	spin_lock(&pin_fs_lock);
5591da177e4SLinus Torvalds 	mnt = *mount;
5601da177e4SLinus Torvalds 	if (!--*count)
5611da177e4SLinus Torvalds 		*mount = NULL;
5621da177e4SLinus Torvalds 	spin_unlock(&pin_fs_lock);
5631da177e4SLinus Torvalds 	mntput(mnt);
5641da177e4SLinus Torvalds }
5651da177e4SLinus Torvalds 
5666d1029b5SAkinobu Mita /**
5676d1029b5SAkinobu Mita  * simple_read_from_buffer - copy data from the buffer to user space
5686d1029b5SAkinobu Mita  * @to: the user space buffer to read to
5696d1029b5SAkinobu Mita  * @count: the maximum number of bytes to read
5706d1029b5SAkinobu Mita  * @ppos: the current position in the buffer
5716d1029b5SAkinobu Mita  * @from: the buffer to read from
5726d1029b5SAkinobu Mita  * @available: the size of the buffer
5736d1029b5SAkinobu Mita  *
5746d1029b5SAkinobu Mita  * The simple_read_from_buffer() function reads up to @count bytes from the
5756d1029b5SAkinobu Mita  * buffer @from at offset @ppos into the user space address starting at @to.
5766d1029b5SAkinobu Mita  *
5776d1029b5SAkinobu Mita  * On success, the number of bytes read is returned and the offset @ppos is
5786d1029b5SAkinobu Mita  * advanced by this number, or negative value is returned on error.
5796d1029b5SAkinobu Mita  **/
5801da177e4SLinus Torvalds ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos,
5811da177e4SLinus Torvalds 				const void *from, size_t available)
5821da177e4SLinus Torvalds {
5831da177e4SLinus Torvalds 	loff_t pos = *ppos;
58414be2746SSteven Rostedt 	size_t ret;
58514be2746SSteven Rostedt 
5861da177e4SLinus Torvalds 	if (pos < 0)
5871da177e4SLinus Torvalds 		return -EINVAL;
58814be2746SSteven Rostedt 	if (pos >= available || !count)
5891da177e4SLinus Torvalds 		return 0;
5901da177e4SLinus Torvalds 	if (count > available - pos)
5911da177e4SLinus Torvalds 		count = available - pos;
59214be2746SSteven Rostedt 	ret = copy_to_user(to, from + pos, count);
59314be2746SSteven Rostedt 	if (ret == count)
5941da177e4SLinus Torvalds 		return -EFAULT;
59514be2746SSteven Rostedt 	count -= ret;
5961da177e4SLinus Torvalds 	*ppos = pos + count;
5971da177e4SLinus Torvalds 	return count;
5981da177e4SLinus Torvalds }
5991da177e4SLinus Torvalds 
6006d1029b5SAkinobu Mita /**
6016a727b43SJiri Slaby  * simple_write_to_buffer - copy data from user space to the buffer
6026a727b43SJiri Slaby  * @to: the buffer to write to
6036a727b43SJiri Slaby  * @available: the size of the buffer
6046a727b43SJiri Slaby  * @ppos: the current position in the buffer
6056a727b43SJiri Slaby  * @from: the user space buffer to read from
6066a727b43SJiri Slaby  * @count: the maximum number of bytes to read
6076a727b43SJiri Slaby  *
6086a727b43SJiri Slaby  * The simple_write_to_buffer() function reads up to @count bytes from the user
6096a727b43SJiri Slaby  * space address starting at @from into the buffer @to at offset @ppos.
6106a727b43SJiri Slaby  *
6116a727b43SJiri Slaby  * On success, the number of bytes written is returned and the offset @ppos is
6126a727b43SJiri Slaby  * advanced by this number, or negative value is returned on error.
6136a727b43SJiri Slaby  **/
6146a727b43SJiri Slaby ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos,
6156a727b43SJiri Slaby 		const void __user *from, size_t count)
6166a727b43SJiri Slaby {
6176a727b43SJiri Slaby 	loff_t pos = *ppos;
6186a727b43SJiri Slaby 	size_t res;
6196a727b43SJiri Slaby 
6206a727b43SJiri Slaby 	if (pos < 0)
6216a727b43SJiri Slaby 		return -EINVAL;
6226a727b43SJiri Slaby 	if (pos >= available || !count)
6236a727b43SJiri Slaby 		return 0;
6246a727b43SJiri Slaby 	if (count > available - pos)
6256a727b43SJiri Slaby 		count = available - pos;
6266a727b43SJiri Slaby 	res = copy_from_user(to + pos, from, count);
6276a727b43SJiri Slaby 	if (res == count)
6286a727b43SJiri Slaby 		return -EFAULT;
6296a727b43SJiri Slaby 	count -= res;
6306a727b43SJiri Slaby 	*ppos = pos + count;
6316a727b43SJiri Slaby 	return count;
6326a727b43SJiri Slaby }
6336a727b43SJiri Slaby 
6346a727b43SJiri Slaby /**
6356d1029b5SAkinobu Mita  * memory_read_from_buffer - copy data from the buffer
6366d1029b5SAkinobu Mita  * @to: the kernel space buffer to read to
6376d1029b5SAkinobu Mita  * @count: the maximum number of bytes to read
6386d1029b5SAkinobu Mita  * @ppos: the current position in the buffer
6396d1029b5SAkinobu Mita  * @from: the buffer to read from
6406d1029b5SAkinobu Mita  * @available: the size of the buffer
6416d1029b5SAkinobu Mita  *
6426d1029b5SAkinobu Mita  * The memory_read_from_buffer() function reads up to @count bytes from the
6436d1029b5SAkinobu Mita  * buffer @from at offset @ppos into the kernel space address starting at @to.
6446d1029b5SAkinobu Mita  *
6456d1029b5SAkinobu Mita  * On success, the number of bytes read is returned and the offset @ppos is
6466d1029b5SAkinobu Mita  * advanced by this number, or negative value is returned on error.
6476d1029b5SAkinobu Mita  **/
64893b07113SAkinobu Mita ssize_t memory_read_from_buffer(void *to, size_t count, loff_t *ppos,
64993b07113SAkinobu Mita 				const void *from, size_t available)
65093b07113SAkinobu Mita {
65193b07113SAkinobu Mita 	loff_t pos = *ppos;
65293b07113SAkinobu Mita 
65393b07113SAkinobu Mita 	if (pos < 0)
65493b07113SAkinobu Mita 		return -EINVAL;
65593b07113SAkinobu Mita 	if (pos >= available)
65693b07113SAkinobu Mita 		return 0;
65793b07113SAkinobu Mita 	if (count > available - pos)
65893b07113SAkinobu Mita 		count = available - pos;
65993b07113SAkinobu Mita 	memcpy(to, from + pos, count);
66093b07113SAkinobu Mita 	*ppos = pos + count;
66193b07113SAkinobu Mita 
66293b07113SAkinobu Mita 	return count;
66393b07113SAkinobu Mita }
66493b07113SAkinobu Mita 
6651da177e4SLinus Torvalds /*
6661da177e4SLinus Torvalds  * Transaction based IO.
6671da177e4SLinus Torvalds  * The file expects a single write which triggers the transaction, and then
6681da177e4SLinus Torvalds  * possibly a read which collects the result - which is stored in a
6691da177e4SLinus Torvalds  * file-local buffer.
6701da177e4SLinus Torvalds  */
67176791ab2SIngo Molnar 
67276791ab2SIngo Molnar void simple_transaction_set(struct file *file, size_t n)
67376791ab2SIngo Molnar {
67476791ab2SIngo Molnar 	struct simple_transaction_argresp *ar = file->private_data;
67576791ab2SIngo Molnar 
67676791ab2SIngo Molnar 	BUG_ON(n > SIMPLE_TRANSACTION_LIMIT);
67776791ab2SIngo Molnar 
67876791ab2SIngo Molnar 	/*
67976791ab2SIngo Molnar 	 * The barrier ensures that ar->size will really remain zero until
68076791ab2SIngo Molnar 	 * ar->data is ready for reading.
68176791ab2SIngo Molnar 	 */
68276791ab2SIngo Molnar 	smp_mb();
68376791ab2SIngo Molnar 	ar->size = n;
68476791ab2SIngo Molnar }
68576791ab2SIngo Molnar 
6861da177e4SLinus Torvalds char *simple_transaction_get(struct file *file, const char __user *buf, size_t size)
6871da177e4SLinus Torvalds {
6881da177e4SLinus Torvalds 	struct simple_transaction_argresp *ar;
6891da177e4SLinus Torvalds 	static DEFINE_SPINLOCK(simple_transaction_lock);
6901da177e4SLinus Torvalds 
6911da177e4SLinus Torvalds 	if (size > SIMPLE_TRANSACTION_LIMIT - 1)
6921da177e4SLinus Torvalds 		return ERR_PTR(-EFBIG);
6931da177e4SLinus Torvalds 
6941da177e4SLinus Torvalds 	ar = (struct simple_transaction_argresp *)get_zeroed_page(GFP_KERNEL);
6951da177e4SLinus Torvalds 	if (!ar)
6961da177e4SLinus Torvalds 		return ERR_PTR(-ENOMEM);
6971da177e4SLinus Torvalds 
6981da177e4SLinus Torvalds 	spin_lock(&simple_transaction_lock);
6991da177e4SLinus Torvalds 
7001da177e4SLinus Torvalds 	/* only one write allowed per open */
7011da177e4SLinus Torvalds 	if (file->private_data) {
7021da177e4SLinus Torvalds 		spin_unlock(&simple_transaction_lock);
7031da177e4SLinus Torvalds 		free_page((unsigned long)ar);
7041da177e4SLinus Torvalds 		return ERR_PTR(-EBUSY);
7051da177e4SLinus Torvalds 	}
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds 	file->private_data = ar;
7081da177e4SLinus Torvalds 
7091da177e4SLinus Torvalds 	spin_unlock(&simple_transaction_lock);
7101da177e4SLinus Torvalds 
7111da177e4SLinus Torvalds 	if (copy_from_user(ar->data, buf, size))
7121da177e4SLinus Torvalds 		return ERR_PTR(-EFAULT);
7131da177e4SLinus Torvalds 
7141da177e4SLinus Torvalds 	return ar->data;
7151da177e4SLinus Torvalds }
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds ssize_t simple_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
7181da177e4SLinus Torvalds {
7191da177e4SLinus Torvalds 	struct simple_transaction_argresp *ar = file->private_data;
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds 	if (!ar)
7221da177e4SLinus Torvalds 		return 0;
7231da177e4SLinus Torvalds 	return simple_read_from_buffer(buf, size, pos, ar->data, ar->size);
7241da177e4SLinus Torvalds }
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds int simple_transaction_release(struct inode *inode, struct file *file)
7271da177e4SLinus Torvalds {
7281da177e4SLinus Torvalds 	free_page((unsigned long)file->private_data);
7291da177e4SLinus Torvalds 	return 0;
7301da177e4SLinus Torvalds }
7311da177e4SLinus Torvalds 
732acaefc25SArnd Bergmann /* Simple attribute files */
733acaefc25SArnd Bergmann 
734acaefc25SArnd Bergmann struct simple_attr {
7358b88b099SChristoph Hellwig 	int (*get)(void *, u64 *);
7368b88b099SChristoph Hellwig 	int (*set)(void *, u64);
737acaefc25SArnd Bergmann 	char get_buf[24];	/* enough to store a u64 and "\n\0" */
738acaefc25SArnd Bergmann 	char set_buf[24];
739acaefc25SArnd Bergmann 	void *data;
740acaefc25SArnd Bergmann 	const char *fmt;	/* format for read operation */
7417cf34c76SIngo Molnar 	struct mutex mutex;	/* protects access to these buffers */
742acaefc25SArnd Bergmann };
743acaefc25SArnd Bergmann 
744acaefc25SArnd Bergmann /* simple_attr_open is called by an actual attribute open file operation
745acaefc25SArnd Bergmann  * to set the attribute specific access operations. */
746acaefc25SArnd Bergmann int simple_attr_open(struct inode *inode, struct file *file,
7478b88b099SChristoph Hellwig 		     int (*get)(void *, u64 *), int (*set)(void *, u64),
748acaefc25SArnd Bergmann 		     const char *fmt)
749acaefc25SArnd Bergmann {
750acaefc25SArnd Bergmann 	struct simple_attr *attr;
751acaefc25SArnd Bergmann 
752acaefc25SArnd Bergmann 	attr = kmalloc(sizeof(*attr), GFP_KERNEL);
753acaefc25SArnd Bergmann 	if (!attr)
754acaefc25SArnd Bergmann 		return -ENOMEM;
755acaefc25SArnd Bergmann 
756acaefc25SArnd Bergmann 	attr->get = get;
757acaefc25SArnd Bergmann 	attr->set = set;
7588e18e294STheodore Ts'o 	attr->data = inode->i_private;
759acaefc25SArnd Bergmann 	attr->fmt = fmt;
7607cf34c76SIngo Molnar 	mutex_init(&attr->mutex);
761acaefc25SArnd Bergmann 
762acaefc25SArnd Bergmann 	file->private_data = attr;
763acaefc25SArnd Bergmann 
764acaefc25SArnd Bergmann 	return nonseekable_open(inode, file);
765acaefc25SArnd Bergmann }
766acaefc25SArnd Bergmann 
76774bedc4dSChristoph Hellwig int simple_attr_release(struct inode *inode, struct file *file)
768acaefc25SArnd Bergmann {
769acaefc25SArnd Bergmann 	kfree(file->private_data);
770acaefc25SArnd Bergmann 	return 0;
771acaefc25SArnd Bergmann }
772acaefc25SArnd Bergmann 
773acaefc25SArnd Bergmann /* read from the buffer that is filled with the get function */
774acaefc25SArnd Bergmann ssize_t simple_attr_read(struct file *file, char __user *buf,
775acaefc25SArnd Bergmann 			 size_t len, loff_t *ppos)
776acaefc25SArnd Bergmann {
777acaefc25SArnd Bergmann 	struct simple_attr *attr;
778acaefc25SArnd Bergmann 	size_t size;
779acaefc25SArnd Bergmann 	ssize_t ret;
780acaefc25SArnd Bergmann 
781acaefc25SArnd Bergmann 	attr = file->private_data;
782acaefc25SArnd Bergmann 
783acaefc25SArnd Bergmann 	if (!attr->get)
784acaefc25SArnd Bergmann 		return -EACCES;
785acaefc25SArnd Bergmann 
7869261303aSChristoph Hellwig 	ret = mutex_lock_interruptible(&attr->mutex);
7879261303aSChristoph Hellwig 	if (ret)
7889261303aSChristoph Hellwig 		return ret;
7899261303aSChristoph Hellwig 
7908b88b099SChristoph Hellwig 	if (*ppos) {		/* continued read */
791acaefc25SArnd Bergmann 		size = strlen(attr->get_buf);
7928b88b099SChristoph Hellwig 	} else {		/* first read */
7938b88b099SChristoph Hellwig 		u64 val;
7948b88b099SChristoph Hellwig 		ret = attr->get(attr->data, &val);
7958b88b099SChristoph Hellwig 		if (ret)
7968b88b099SChristoph Hellwig 			goto out;
7978b88b099SChristoph Hellwig 
798acaefc25SArnd Bergmann 		size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
7998b88b099SChristoph Hellwig 				 attr->fmt, (unsigned long long)val);
8008b88b099SChristoph Hellwig 	}
801acaefc25SArnd Bergmann 
802acaefc25SArnd Bergmann 	ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
8038b88b099SChristoph Hellwig out:
8047cf34c76SIngo Molnar 	mutex_unlock(&attr->mutex);
805acaefc25SArnd Bergmann 	return ret;
806acaefc25SArnd Bergmann }
807acaefc25SArnd Bergmann 
808acaefc25SArnd Bergmann /* interpret the buffer as a number to call the set function with */
809acaefc25SArnd Bergmann ssize_t simple_attr_write(struct file *file, const char __user *buf,
810acaefc25SArnd Bergmann 			  size_t len, loff_t *ppos)
811acaefc25SArnd Bergmann {
812acaefc25SArnd Bergmann 	struct simple_attr *attr;
813acaefc25SArnd Bergmann 	u64 val;
814acaefc25SArnd Bergmann 	size_t size;
815acaefc25SArnd Bergmann 	ssize_t ret;
816acaefc25SArnd Bergmann 
817acaefc25SArnd Bergmann 	attr = file->private_data;
818acaefc25SArnd Bergmann 	if (!attr->set)
819acaefc25SArnd Bergmann 		return -EACCES;
820acaefc25SArnd Bergmann 
8219261303aSChristoph Hellwig 	ret = mutex_lock_interruptible(&attr->mutex);
8229261303aSChristoph Hellwig 	if (ret)
8239261303aSChristoph Hellwig 		return ret;
8249261303aSChristoph Hellwig 
825acaefc25SArnd Bergmann 	ret = -EFAULT;
826acaefc25SArnd Bergmann 	size = min(sizeof(attr->set_buf) - 1, len);
827acaefc25SArnd Bergmann 	if (copy_from_user(attr->set_buf, buf, size))
828acaefc25SArnd Bergmann 		goto out;
829acaefc25SArnd Bergmann 
830acaefc25SArnd Bergmann 	attr->set_buf[size] = '\0';
831acaefc25SArnd Bergmann 	val = simple_strtol(attr->set_buf, NULL, 0);
83205cc0ceeSWu Fengguang 	ret = attr->set(attr->data, val);
83305cc0ceeSWu Fengguang 	if (ret == 0)
83405cc0ceeSWu Fengguang 		ret = len; /* on success, claim we got the whole input */
835acaefc25SArnd Bergmann out:
8367cf34c76SIngo Molnar 	mutex_unlock(&attr->mutex);
837acaefc25SArnd Bergmann 	return ret;
838acaefc25SArnd Bergmann }
839acaefc25SArnd Bergmann 
8402596110aSChristoph Hellwig /**
8412596110aSChristoph Hellwig  * generic_fh_to_dentry - generic helper for the fh_to_dentry export operation
8422596110aSChristoph Hellwig  * @sb:		filesystem to do the file handle conversion on
8432596110aSChristoph Hellwig  * @fid:	file handle to convert
8442596110aSChristoph Hellwig  * @fh_len:	length of the file handle in bytes
8452596110aSChristoph Hellwig  * @fh_type:	type of file handle
8462596110aSChristoph Hellwig  * @get_inode:	filesystem callback to retrieve inode
8472596110aSChristoph Hellwig  *
8482596110aSChristoph Hellwig  * This function decodes @fid as long as it has one of the well-known
8492596110aSChristoph Hellwig  * Linux filehandle types and calls @get_inode on it to retrieve the
8502596110aSChristoph Hellwig  * inode for the object specified in the file handle.
8512596110aSChristoph Hellwig  */
8522596110aSChristoph Hellwig struct dentry *generic_fh_to_dentry(struct super_block *sb, struct fid *fid,
8532596110aSChristoph Hellwig 		int fh_len, int fh_type, struct inode *(*get_inode)
8542596110aSChristoph Hellwig 			(struct super_block *sb, u64 ino, u32 gen))
8552596110aSChristoph Hellwig {
8562596110aSChristoph Hellwig 	struct inode *inode = NULL;
8572596110aSChristoph Hellwig 
8582596110aSChristoph Hellwig 	if (fh_len < 2)
8592596110aSChristoph Hellwig 		return NULL;
8602596110aSChristoph Hellwig 
8612596110aSChristoph Hellwig 	switch (fh_type) {
8622596110aSChristoph Hellwig 	case FILEID_INO32_GEN:
8632596110aSChristoph Hellwig 	case FILEID_INO32_GEN_PARENT:
8642596110aSChristoph Hellwig 		inode = get_inode(sb, fid->i32.ino, fid->i32.gen);
8652596110aSChristoph Hellwig 		break;
8662596110aSChristoph Hellwig 	}
8672596110aSChristoph Hellwig 
8684ea3ada2SChristoph Hellwig 	return d_obtain_alias(inode);
8692596110aSChristoph Hellwig }
8702596110aSChristoph Hellwig EXPORT_SYMBOL_GPL(generic_fh_to_dentry);
8712596110aSChristoph Hellwig 
8722596110aSChristoph Hellwig /**
8732596110aSChristoph Hellwig  * generic_fh_to_dentry - generic helper for the fh_to_parent export operation
8742596110aSChristoph Hellwig  * @sb:		filesystem to do the file handle conversion on
8752596110aSChristoph Hellwig  * @fid:	file handle to convert
8762596110aSChristoph Hellwig  * @fh_len:	length of the file handle in bytes
8772596110aSChristoph Hellwig  * @fh_type:	type of file handle
8782596110aSChristoph Hellwig  * @get_inode:	filesystem callback to retrieve inode
8792596110aSChristoph Hellwig  *
8802596110aSChristoph Hellwig  * This function decodes @fid as long as it has one of the well-known
8812596110aSChristoph Hellwig  * Linux filehandle types and calls @get_inode on it to retrieve the
8822596110aSChristoph Hellwig  * inode for the _parent_ object specified in the file handle if it
8832596110aSChristoph Hellwig  * is specified in the file handle, or NULL otherwise.
8842596110aSChristoph Hellwig  */
8852596110aSChristoph Hellwig struct dentry *generic_fh_to_parent(struct super_block *sb, struct fid *fid,
8862596110aSChristoph Hellwig 		int fh_len, int fh_type, struct inode *(*get_inode)
8872596110aSChristoph Hellwig 			(struct super_block *sb, u64 ino, u32 gen))
8882596110aSChristoph Hellwig {
8892596110aSChristoph Hellwig 	struct inode *inode = NULL;
8902596110aSChristoph Hellwig 
8912596110aSChristoph Hellwig 	if (fh_len <= 2)
8922596110aSChristoph Hellwig 		return NULL;
8932596110aSChristoph Hellwig 
8942596110aSChristoph Hellwig 	switch (fh_type) {
8952596110aSChristoph Hellwig 	case FILEID_INO32_GEN_PARENT:
8962596110aSChristoph Hellwig 		inode = get_inode(sb, fid->i32.parent_ino,
8972596110aSChristoph Hellwig 				  (fh_len > 3 ? fid->i32.parent_gen : 0));
8982596110aSChristoph Hellwig 		break;
8992596110aSChristoph Hellwig 	}
9002596110aSChristoph Hellwig 
9014ea3ada2SChristoph Hellwig 	return d_obtain_alias(inode);
9022596110aSChristoph Hellwig }
9032596110aSChristoph Hellwig EXPORT_SYMBOL_GPL(generic_fh_to_parent);
9042596110aSChristoph Hellwig 
9051b061d92SChristoph Hellwig /**
9061b061d92SChristoph Hellwig  * generic_file_fsync - generic fsync implementation for simple filesystems
9071b061d92SChristoph Hellwig  * @file:	file to synchronize
9081b061d92SChristoph Hellwig  * @datasync:	only synchronize essential metadata if true
9091b061d92SChristoph Hellwig  *
9101b061d92SChristoph Hellwig  * This is a generic implementation of the fsync method for simple
9111b061d92SChristoph Hellwig  * filesystems which track all non-inode metadata in the buffers list
9121b061d92SChristoph Hellwig  * hanging off the address_space structure.
9131b061d92SChristoph Hellwig  */
9141b061d92SChristoph Hellwig int generic_file_fsync(struct file *file, int datasync)
915d5aacad5SAl Viro {
9167ea80859SChristoph Hellwig 	struct inode *inode = file->f_mapping->host;
917d5aacad5SAl Viro 	int err;
918d5aacad5SAl Viro 	int ret;
919d5aacad5SAl Viro 
920d5aacad5SAl Viro 	ret = sync_mapping_buffers(inode->i_mapping);
921d5aacad5SAl Viro 	if (!(inode->i_state & I_DIRTY))
922d5aacad5SAl Viro 		return ret;
923d5aacad5SAl Viro 	if (datasync && !(inode->i_state & I_DIRTY_DATASYNC))
924d5aacad5SAl Viro 		return ret;
925d5aacad5SAl Viro 
926c3765016SChristoph Hellwig 	err = sync_inode_metadata(inode, 1);
927d5aacad5SAl Viro 	if (ret == 0)
928d5aacad5SAl Viro 		ret = err;
929d5aacad5SAl Viro 	return ret;
930d5aacad5SAl Viro }
9311b061d92SChristoph Hellwig EXPORT_SYMBOL(generic_file_fsync);
9321b061d92SChristoph Hellwig 
93330ca22c7SPatrick J. LoPresti /**
93430ca22c7SPatrick J. LoPresti  * generic_check_addressable - Check addressability of file system
93530ca22c7SPatrick J. LoPresti  * @blocksize_bits:	log of file system block size
93630ca22c7SPatrick J. LoPresti  * @num_blocks:		number of blocks in file system
93730ca22c7SPatrick J. LoPresti  *
93830ca22c7SPatrick J. LoPresti  * Determine whether a file system with @num_blocks blocks (and a
93930ca22c7SPatrick J. LoPresti  * block size of 2**@blocksize_bits) is addressable by the sector_t
94030ca22c7SPatrick J. LoPresti  * and page cache of the system.  Return 0 if so and -EFBIG otherwise.
94130ca22c7SPatrick J. LoPresti  */
94230ca22c7SPatrick J. LoPresti int generic_check_addressable(unsigned blocksize_bits, u64 num_blocks)
94330ca22c7SPatrick J. LoPresti {
94430ca22c7SPatrick J. LoPresti 	u64 last_fs_block = num_blocks - 1;
945a33f13efSJoel Becker 	u64 last_fs_page =
946a33f13efSJoel Becker 		last_fs_block >> (PAGE_CACHE_SHIFT - blocksize_bits);
94730ca22c7SPatrick J. LoPresti 
94830ca22c7SPatrick J. LoPresti 	if (unlikely(num_blocks == 0))
94930ca22c7SPatrick J. LoPresti 		return 0;
95030ca22c7SPatrick J. LoPresti 
95130ca22c7SPatrick J. LoPresti 	if ((blocksize_bits < 9) || (blocksize_bits > PAGE_CACHE_SHIFT))
95230ca22c7SPatrick J. LoPresti 		return -EINVAL;
95330ca22c7SPatrick J. LoPresti 
954a33f13efSJoel Becker 	if ((last_fs_block > (sector_t)(~0ULL) >> (blocksize_bits - 9)) ||
955a33f13efSJoel Becker 	    (last_fs_page > (pgoff_t)(~0ULL))) {
95630ca22c7SPatrick J. LoPresti 		return -EFBIG;
95730ca22c7SPatrick J. LoPresti 	}
95830ca22c7SPatrick J. LoPresti 	return 0;
95930ca22c7SPatrick J. LoPresti }
96030ca22c7SPatrick J. LoPresti EXPORT_SYMBOL(generic_check_addressable);
96130ca22c7SPatrick J. LoPresti 
9621b061d92SChristoph Hellwig /*
9631b061d92SChristoph Hellwig  * No-op implementation of ->fsync for in-memory filesystems.
9641b061d92SChristoph Hellwig  */
9651b061d92SChristoph Hellwig int noop_fsync(struct file *file, int datasync)
9661b061d92SChristoph Hellwig {
9671b061d92SChristoph Hellwig 	return 0;
9681b061d92SChristoph Hellwig }
969d5aacad5SAl Viro 
9701da177e4SLinus Torvalds EXPORT_SYMBOL(dcache_dir_close);
9711da177e4SLinus Torvalds EXPORT_SYMBOL(dcache_dir_lseek);
9721da177e4SLinus Torvalds EXPORT_SYMBOL(dcache_dir_open);
9731da177e4SLinus Torvalds EXPORT_SYMBOL(dcache_readdir);
9741da177e4SLinus Torvalds EXPORT_SYMBOL(generic_read_dir);
97551139adaSAl Viro EXPORT_SYMBOL(mount_pseudo);
976afddba49SNick Piggin EXPORT_SYMBOL(simple_write_begin);
977afddba49SNick Piggin EXPORT_SYMBOL(simple_write_end);
9781da177e4SLinus Torvalds EXPORT_SYMBOL(simple_dir_inode_operations);
9791da177e4SLinus Torvalds EXPORT_SYMBOL(simple_dir_operations);
9801da177e4SLinus Torvalds EXPORT_SYMBOL(simple_empty);
9811da177e4SLinus Torvalds EXPORT_SYMBOL(simple_fill_super);
9821da177e4SLinus Torvalds EXPORT_SYMBOL(simple_getattr);
9831da177e4SLinus Torvalds EXPORT_SYMBOL(simple_link);
9841da177e4SLinus Torvalds EXPORT_SYMBOL(simple_lookup);
9851da177e4SLinus Torvalds EXPORT_SYMBOL(simple_pin_fs);
9861da177e4SLinus Torvalds EXPORT_SYMBOL(simple_readpage);
9871da177e4SLinus Torvalds EXPORT_SYMBOL(simple_release_fs);
9881da177e4SLinus Torvalds EXPORT_SYMBOL(simple_rename);
9891da177e4SLinus Torvalds EXPORT_SYMBOL(simple_rmdir);
9901da177e4SLinus Torvalds EXPORT_SYMBOL(simple_statfs);
9911b061d92SChristoph Hellwig EXPORT_SYMBOL(noop_fsync);
9921da177e4SLinus Torvalds EXPORT_SYMBOL(simple_unlink);
9931da177e4SLinus Torvalds EXPORT_SYMBOL(simple_read_from_buffer);
9946a727b43SJiri Slaby EXPORT_SYMBOL(simple_write_to_buffer);
99593b07113SAkinobu Mita EXPORT_SYMBOL(memory_read_from_buffer);
99676791ab2SIngo Molnar EXPORT_SYMBOL(simple_transaction_set);
9971da177e4SLinus Torvalds EXPORT_SYMBOL(simple_transaction_get);
9981da177e4SLinus Torvalds EXPORT_SYMBOL(simple_transaction_read);
9991da177e4SLinus Torvalds EXPORT_SYMBOL(simple_transaction_release);
1000acaefc25SArnd Bergmann EXPORT_SYMBOL_GPL(simple_attr_open);
100174bedc4dSChristoph Hellwig EXPORT_SYMBOL_GPL(simple_attr_release);
1002acaefc25SArnd Bergmann EXPORT_SYMBOL_GPL(simple_attr_read);
1003acaefc25SArnd Bergmann EXPORT_SYMBOL_GPL(simple_attr_write);
1004