xref: /linux/fs/hostfs/hostfs_kern.c (revision 91b0abe36a7b2b3b02d7500925a5f8455334f0e5)
11da177e4SLinus Torvalds /*
2f1adc05eSJeff Dike  * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
31da177e4SLinus Torvalds  * Licensed under the GPL
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Ported the filesystem routines to 2.5.
61da177e4SLinus Torvalds  * 2003-02-10 Petr Baudis <pasky@ucw.cz>
71da177e4SLinus Torvalds  */
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds #include <linux/fs.h>
102b3b9bb0SJames Hogan #include <linux/magic.h>
111da177e4SLinus Torvalds #include <linux/module.h>
1284b3db04SJeff Dike #include <linux/mm.h>
131da177e4SLinus Torvalds #include <linux/pagemap.h>
141da177e4SLinus Torvalds #include <linux/statfs.h>
155a0e3ad6STejun Heo #include <linux/slab.h>
16dd2cc4dfSMiklos Szeredi #include <linux/seq_file.h>
176966a977SJiri Kosina #include <linux/mount.h>
18d0352d3eSAl Viro #include <linux/namei.h>
191da177e4SLinus Torvalds #include "hostfs.h"
2037185b33SAl Viro #include <init.h>
2137185b33SAl Viro #include <kern.h>
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds struct hostfs_inode_info {
241da177e4SLinus Torvalds 	int fd;
25aeb5d727SAl Viro 	fmode_t mode;
261da177e4SLinus Torvalds 	struct inode vfs_inode;
271da177e4SLinus Torvalds };
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds static inline struct hostfs_inode_info *HOSTFS_I(struct inode *inode)
301da177e4SLinus Torvalds {
31f1adc05eSJeff Dike 	return list_entry(inode, struct hostfs_inode_info, vfs_inode);
321da177e4SLinus Torvalds }
331da177e4SLinus Torvalds 
34496ad9aaSAl Viro #define FILE_HOSTFS_I(file) HOSTFS_I(file_inode(file))
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds /* Changed in hostfs_args before the kernel starts running */
37a6eb0be6SPaolo 'Blaisorblade' Giarrusso static char *root_ino = "";
381da177e4SLinus Torvalds static int append = 0;
391da177e4SLinus Torvalds 
4092e1d5beSArjan van de Ven static const struct inode_operations hostfs_iops;
4192e1d5beSArjan van de Ven static const struct inode_operations hostfs_dir_iops;
42d0352d3eSAl Viro static const struct inode_operations hostfs_link_iops;
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds #ifndef MODULE
451da177e4SLinus Torvalds static int __init hostfs_args(char *options, int *add)
461da177e4SLinus Torvalds {
471da177e4SLinus Torvalds 	char *ptr;
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds 	ptr = strchr(options, ',');
501da177e4SLinus Torvalds 	if (ptr != NULL)
511da177e4SLinus Torvalds 		*ptr++ = '\0';
521da177e4SLinus Torvalds 	if (*options != '\0')
531da177e4SLinus Torvalds 		root_ino = options;
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds 	options = ptr;
561da177e4SLinus Torvalds 	while (options) {
571da177e4SLinus Torvalds 		ptr = strchr(options, ',');
581da177e4SLinus Torvalds 		if (ptr != NULL)
591da177e4SLinus Torvalds 			*ptr++ = '\0';
601da177e4SLinus Torvalds 		if (*options != '\0') {
611da177e4SLinus Torvalds 			if (!strcmp(options, "append"))
621da177e4SLinus Torvalds 				append = 1;
631da177e4SLinus Torvalds 			else printf("hostfs_args - unsupported option - %s\n",
641da177e4SLinus Torvalds 				    options);
651da177e4SLinus Torvalds 		}
661da177e4SLinus Torvalds 		options = ptr;
671da177e4SLinus Torvalds 	}
68f1adc05eSJeff Dike 	return 0;
691da177e4SLinus Torvalds }
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds __uml_setup("hostfs=", hostfs_args,
721da177e4SLinus Torvalds "hostfs=<root dir>,<flags>,...\n"
731da177e4SLinus Torvalds "    This is used to set hostfs parameters.  The root directory argument\n"
741da177e4SLinus Torvalds "    is used to confine all hostfs mounts to within the specified directory\n"
751da177e4SLinus Torvalds "    tree on the host.  If this isn't specified, then a user inside UML can\n"
761da177e4SLinus Torvalds "    mount anything on the host that's accessible to the user that's running\n"
771da177e4SLinus Torvalds "    it.\n"
781da177e4SLinus Torvalds "    The only flag currently supported is 'append', which specifies that all\n"
791da177e4SLinus Torvalds "    files opened by hostfs will be opened in append mode.\n\n"
801da177e4SLinus Torvalds );
811da177e4SLinus Torvalds #endif
821da177e4SLinus Torvalds 
83e9193059SAl Viro static char *__dentry_name(struct dentry *dentry, char *name)
84e9193059SAl Viro {
85ec2447c2SNick Piggin 	char *p = dentry_path_raw(dentry, name, PATH_MAX);
86e9193059SAl Viro 	char *root;
87e9193059SAl Viro 	size_t len;
88e9193059SAl Viro 
89e9193059SAl Viro 	root = dentry->d_sb->s_fs_info;
90e9193059SAl Viro 	len = strlen(root);
91e9193059SAl Viro 	if (IS_ERR(p)) {
92e9193059SAl Viro 		__putname(name);
93e9193059SAl Viro 		return NULL;
94e9193059SAl Viro 	}
95850a496fSAl Viro 	strlcpy(name, root, PATH_MAX);
96e9193059SAl Viro 	if (len > p - name) {
97e9193059SAl Viro 		__putname(name);
98e9193059SAl Viro 		return NULL;
99e9193059SAl Viro 	}
100e9193059SAl Viro 	if (p > name + len) {
101e9193059SAl Viro 		char *s = name + len;
102e9193059SAl Viro 		while ((*s++ = *p++) != '\0')
103e9193059SAl Viro 			;
104e9193059SAl Viro 	}
105e9193059SAl Viro 	return name;
106e9193059SAl Viro }
107e9193059SAl Viro 
108c5322220SAl Viro static char *dentry_name(struct dentry *dentry)
1091da177e4SLinus Torvalds {
110e9193059SAl Viro 	char *name = __getname();
111e9193059SAl Viro 	if (!name)
112f1adc05eSJeff Dike 		return NULL;
1131da177e4SLinus Torvalds 
1149dcc5e8aSJames Hogan 	return __dentry_name(dentry, name);
1151da177e4SLinus Torvalds }
1161da177e4SLinus Torvalds 
117c5322220SAl Viro static char *inode_name(struct inode *ino)
1181da177e4SLinus Torvalds {
1191da177e4SLinus Torvalds 	struct dentry *dentry;
120ec2447c2SNick Piggin 	char *name;
121ec2447c2SNick Piggin 
122ec2447c2SNick Piggin 	dentry = d_find_alias(ino);
123ec2447c2SNick Piggin 	if (!dentry)
124e9193059SAl Viro 		return NULL;
1251da177e4SLinus Torvalds 
126ec2447c2SNick Piggin 	name = dentry_name(dentry);
127ec2447c2SNick Piggin 
128ec2447c2SNick Piggin 	dput(dentry);
129ec2447c2SNick Piggin 
130ec2447c2SNick Piggin 	return name;
1311da177e4SLinus Torvalds }
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds static char *follow_link(char *link)
1341da177e4SLinus Torvalds {
1351da177e4SLinus Torvalds 	int len, n;
1361da177e4SLinus Torvalds 	char *name, *resolved, *end;
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds 	len = 64;
1391da177e4SLinus Torvalds 	while (1) {
1401da177e4SLinus Torvalds 		n = -ENOMEM;
1411da177e4SLinus Torvalds 		name = kmalloc(len, GFP_KERNEL);
1421da177e4SLinus Torvalds 		if (name == NULL)
1431da177e4SLinus Torvalds 			goto out;
1441da177e4SLinus Torvalds 
145ea7e743eSWANG Cong 		n = hostfs_do_readlink(link, name, len);
1461da177e4SLinus Torvalds 		if (n < len)
1471da177e4SLinus Torvalds 			break;
1481da177e4SLinus Torvalds 		len *= 2;
1491da177e4SLinus Torvalds 		kfree(name);
1501da177e4SLinus Torvalds 	}
1511da177e4SLinus Torvalds 	if (n < 0)
1521da177e4SLinus Torvalds 		goto out_free;
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds 	if (*name == '/')
155f1adc05eSJeff Dike 		return name;
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds 	end = strrchr(link, '/');
1581da177e4SLinus Torvalds 	if (end == NULL)
159f1adc05eSJeff Dike 		return name;
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds 	*(end + 1) = '\0';
1621da177e4SLinus Torvalds 	len = strlen(link) + strlen(name) + 1;
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	resolved = kmalloc(len, GFP_KERNEL);
1651da177e4SLinus Torvalds 	if (resolved == NULL) {
1661da177e4SLinus Torvalds 		n = -ENOMEM;
1671da177e4SLinus Torvalds 		goto out_free;
1681da177e4SLinus Torvalds 	}
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds 	sprintf(resolved, "%s%s", link, name);
1711da177e4SLinus Torvalds 	kfree(name);
1721da177e4SLinus Torvalds 	kfree(link);
173f1adc05eSJeff Dike 	return resolved;
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds  out_free:
1761da177e4SLinus Torvalds 	kfree(name);
1771da177e4SLinus Torvalds  out:
178f1adc05eSJeff Dike 	return ERR_PTR(n);
1791da177e4SLinus Torvalds }
1801da177e4SLinus Torvalds 
1810a370e5dSDavid Howells static struct inode *hostfs_iget(struct super_block *sb)
1820a370e5dSDavid Howells {
18352b209f7SAl Viro 	struct inode *inode = new_inode(sb);
1840a370e5dSDavid Howells 	if (!inode)
1850a370e5dSDavid Howells 		return ERR_PTR(-ENOMEM);
1860a370e5dSDavid Howells 	return inode;
1870a370e5dSDavid Howells }
1880a370e5dSDavid Howells 
1899e443bc3SJames Hogan static int hostfs_statfs(struct dentry *dentry, struct kstatfs *sf)
1901da177e4SLinus Torvalds {
19184b3db04SJeff Dike 	/*
19284b3db04SJeff Dike 	 * do_statfs uses struct statfs64 internally, but the linux kernel
1931da177e4SLinus Torvalds 	 * struct statfs still has 32-bit versions for most of these fields,
1941da177e4SLinus Torvalds 	 * so we convert them here
1951da177e4SLinus Torvalds 	 */
1961da177e4SLinus Torvalds 	int err;
1971da177e4SLinus Torvalds 	long long f_blocks;
1981da177e4SLinus Torvalds 	long long f_bfree;
1991da177e4SLinus Torvalds 	long long f_bavail;
2001da177e4SLinus Torvalds 	long long f_files;
2011da177e4SLinus Torvalds 	long long f_ffree;
2021da177e4SLinus Torvalds 
203601d2c38SAl Viro 	err = do_statfs(dentry->d_sb->s_fs_info,
2041da177e4SLinus Torvalds 			&sf->f_bsize, &f_blocks, &f_bfree, &f_bavail, &f_files,
2051da177e4SLinus Torvalds 			&f_ffree, &sf->f_fsid, sizeof(sf->f_fsid),
2061b627d57SRichard Weinberger 			&sf->f_namelen);
207f1adc05eSJeff Dike 	if (err)
208f1adc05eSJeff Dike 		return err;
2091da177e4SLinus Torvalds 	sf->f_blocks = f_blocks;
2101da177e4SLinus Torvalds 	sf->f_bfree = f_bfree;
2111da177e4SLinus Torvalds 	sf->f_bavail = f_bavail;
2121da177e4SLinus Torvalds 	sf->f_files = f_files;
2131da177e4SLinus Torvalds 	sf->f_ffree = f_ffree;
2141da177e4SLinus Torvalds 	sf->f_type = HOSTFS_SUPER_MAGIC;
215f1adc05eSJeff Dike 	return 0;
2161da177e4SLinus Torvalds }
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds static struct inode *hostfs_alloc_inode(struct super_block *sb)
2191da177e4SLinus Torvalds {
2201da177e4SLinus Torvalds 	struct hostfs_inode_info *hi;
2211da177e4SLinus Torvalds 
222371fdab1SJames Hogan 	hi = kmalloc(sizeof(*hi), GFP_KERNEL);
2231da177e4SLinus Torvalds 	if (hi == NULL)
224f1adc05eSJeff Dike 		return NULL;
225601d2c38SAl Viro 	hi->fd = -1;
226371fdab1SJames Hogan 	hi->mode = 0;
2271da177e4SLinus Torvalds 	inode_init_once(&hi->vfs_inode);
228f1adc05eSJeff Dike 	return &hi->vfs_inode;
2291da177e4SLinus Torvalds }
2301da177e4SLinus Torvalds 
231e971a6d7SAl Viro static void hostfs_evict_inode(struct inode *inode)
2321da177e4SLinus Torvalds {
233*91b0abe3SJohannes Weiner 	truncate_inode_pages_final(&inode->i_data);
234dbd5768fSJan Kara 	clear_inode(inode);
2351da177e4SLinus Torvalds 	if (HOSTFS_I(inode)->fd != -1) {
2361da177e4SLinus Torvalds 		close_file(&HOSTFS_I(inode)->fd);
2371da177e4SLinus Torvalds 		HOSTFS_I(inode)->fd = -1;
2381da177e4SLinus Torvalds 	}
2391da177e4SLinus Torvalds }
2401da177e4SLinus Torvalds 
241fa0d7e3dSNick Piggin static void hostfs_i_callback(struct rcu_head *head)
242fa0d7e3dSNick Piggin {
243fa0d7e3dSNick Piggin 	struct inode *inode = container_of(head, struct inode, i_rcu);
244fa0d7e3dSNick Piggin 	kfree(HOSTFS_I(inode));
245fa0d7e3dSNick Piggin }
246fa0d7e3dSNick Piggin 
2471da177e4SLinus Torvalds static void hostfs_destroy_inode(struct inode *inode)
2481da177e4SLinus Torvalds {
249fa0d7e3dSNick Piggin 	call_rcu(&inode->i_rcu, hostfs_i_callback);
2501da177e4SLinus Torvalds }
2511da177e4SLinus Torvalds 
25234c80b1dSAl Viro static int hostfs_show_options(struct seq_file *seq, struct dentry *root)
253dd2cc4dfSMiklos Szeredi {
25434c80b1dSAl Viro 	const char *root_path = root->d_sb->s_fs_info;
255dd2cc4dfSMiklos Szeredi 	size_t offset = strlen(root_ino) + 1;
256dd2cc4dfSMiklos Szeredi 
257dd2cc4dfSMiklos Szeredi 	if (strlen(root_path) > offset)
258dd2cc4dfSMiklos Szeredi 		seq_printf(seq, ",%s", root_path + offset);
259dd2cc4dfSMiklos Szeredi 
260dd2cc4dfSMiklos Szeredi 	return 0;
261dd2cc4dfSMiklos Szeredi }
262dd2cc4dfSMiklos Szeredi 
263ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations hostfs_sbops = {
2641da177e4SLinus Torvalds 	.alloc_inode	= hostfs_alloc_inode,
2651da177e4SLinus Torvalds 	.destroy_inode	= hostfs_destroy_inode,
266e971a6d7SAl Viro 	.evict_inode	= hostfs_evict_inode,
2671da177e4SLinus Torvalds 	.statfs		= hostfs_statfs,
268dd2cc4dfSMiklos Szeredi 	.show_options	= hostfs_show_options,
2691da177e4SLinus Torvalds };
2701da177e4SLinus Torvalds 
2719e443bc3SJames Hogan static int hostfs_readdir(struct file *file, struct dir_context *ctx)
2721da177e4SLinus Torvalds {
2731da177e4SLinus Torvalds 	void *dir;
2741da177e4SLinus Torvalds 	char *name;
2751da177e4SLinus Torvalds 	unsigned long long next, ino;
2761da177e4SLinus Torvalds 	int error, len;
2773ee6bd8eSGeert Uytterhoeven 	unsigned int type;
2781da177e4SLinus Torvalds 
279c5322220SAl Viro 	name = dentry_name(file->f_path.dentry);
280f1adc05eSJeff Dike 	if (name == NULL)
281f1adc05eSJeff Dike 		return -ENOMEM;
2821da177e4SLinus Torvalds 	dir = open_dir(name, &error);
283e9193059SAl Viro 	__putname(name);
284f1adc05eSJeff Dike 	if (dir == NULL)
285f1adc05eSJeff Dike 		return -error;
2868e28bc7eSAl Viro 	next = ctx->pos;
2873ee6bd8eSGeert Uytterhoeven 	while ((name = read_dir(dir, &next, &ino, &len, &type)) != NULL) {
2888e28bc7eSAl Viro 		if (!dir_emit(ctx, name, len, ino, type))
2898e28bc7eSAl Viro 			break;
2908e28bc7eSAl Viro 		ctx->pos = next;
2911da177e4SLinus Torvalds 	}
2921da177e4SLinus Torvalds 	close_dir(dir);
293f1adc05eSJeff Dike 	return 0;
2941da177e4SLinus Torvalds }
2951da177e4SLinus Torvalds 
2969e443bc3SJames Hogan static int hostfs_file_open(struct inode *ino, struct file *file)
2971da177e4SLinus Torvalds {
298f8ad850fSAl Viro 	static DEFINE_MUTEX(open_mutex);
2991da177e4SLinus Torvalds 	char *name;
300aeb5d727SAl Viro 	fmode_t mode = 0;
301f8ad850fSAl Viro 	int err;
302aeb5d727SAl Viro 	int r = 0, w = 0, fd;
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds 	mode = file->f_mode & (FMODE_READ | FMODE_WRITE);
3051da177e4SLinus Torvalds 	if ((mode & HOSTFS_I(ino)->mode) == mode)
306f1adc05eSJeff Dike 		return 0;
3071da177e4SLinus Torvalds 
308f8ad850fSAl Viro 	mode |= HOSTFS_I(ino)->mode;
3091da177e4SLinus Torvalds 
310f8ad850fSAl Viro retry:
311f8ad850fSAl Viro 	if (mode & FMODE_READ)
3121da177e4SLinus Torvalds 		r = 1;
313f8ad850fSAl Viro 	if (mode & FMODE_WRITE)
3141da177e4SLinus Torvalds 		w = 1;
3151da177e4SLinus Torvalds 	if (w)
3161da177e4SLinus Torvalds 		r = 1;
3171da177e4SLinus Torvalds 
318c5322220SAl Viro 	name = dentry_name(file->f_path.dentry);
3191da177e4SLinus Torvalds 	if (name == NULL)
320f1adc05eSJeff Dike 		return -ENOMEM;
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds 	fd = open_file(name, r, w, append);
323e9193059SAl Viro 	__putname(name);
324f1adc05eSJeff Dike 	if (fd < 0)
325f1adc05eSJeff Dike 		return fd;
326f8ad850fSAl Viro 
327f8ad850fSAl Viro 	mutex_lock(&open_mutex);
328f8ad850fSAl Viro 	/* somebody else had handled it first? */
329f8ad850fSAl Viro 	if ((mode & HOSTFS_I(ino)->mode) == mode) {
330f8ad850fSAl Viro 		mutex_unlock(&open_mutex);
331f8ad850fSAl Viro 		return 0;
332f8ad850fSAl Viro 	}
333f8ad850fSAl Viro 	if ((mode | HOSTFS_I(ino)->mode) != mode) {
334f8ad850fSAl Viro 		mode |= HOSTFS_I(ino)->mode;
335f8ad850fSAl Viro 		mutex_unlock(&open_mutex);
336f8ad850fSAl Viro 		close_file(&fd);
337f8ad850fSAl Viro 		goto retry;
338f8ad850fSAl Viro 	}
339f8ad850fSAl Viro 	if (HOSTFS_I(ino)->fd == -1) {
340f8ad850fSAl Viro 		HOSTFS_I(ino)->fd = fd;
341f8ad850fSAl Viro 	} else {
342f8ad850fSAl Viro 		err = replace_file(fd, HOSTFS_I(ino)->fd);
343f8ad850fSAl Viro 		close_file(&fd);
344f8ad850fSAl Viro 		if (err < 0) {
345f8ad850fSAl Viro 			mutex_unlock(&open_mutex);
346f8ad850fSAl Viro 			return err;
347f8ad850fSAl Viro 		}
348f8ad850fSAl Viro 	}
349f8ad850fSAl Viro 	HOSTFS_I(ino)->mode = mode;
350f8ad850fSAl Viro 	mutex_unlock(&open_mutex);
3511da177e4SLinus Torvalds 
352f1adc05eSJeff Dike 	return 0;
3531da177e4SLinus Torvalds }
3541da177e4SLinus Torvalds 
35565984ff9SRichard Weinberger static int hostfs_file_release(struct inode *inode, struct file *file)
35665984ff9SRichard Weinberger {
35765984ff9SRichard Weinberger 	filemap_write_and_wait(inode->i_mapping);
35865984ff9SRichard Weinberger 
35965984ff9SRichard Weinberger 	return 0;
36065984ff9SRichard Weinberger }
36165984ff9SRichard Weinberger 
3629e443bc3SJames Hogan static int hostfs_fsync(struct file *file, loff_t start, loff_t end,
3639e443bc3SJames Hogan 			int datasync)
3641da177e4SLinus Torvalds {
36502c24a82SJosef Bacik 	struct inode *inode = file->f_mapping->host;
36602c24a82SJosef Bacik 	int ret;
36702c24a82SJosef Bacik 
36802c24a82SJosef Bacik 	ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
36902c24a82SJosef Bacik 	if (ret)
37002c24a82SJosef Bacik 		return ret;
37102c24a82SJosef Bacik 
37202c24a82SJosef Bacik 	mutex_lock(&inode->i_mutex);
37302c24a82SJosef Bacik 	ret = fsync_file(HOSTFS_I(inode)->fd, datasync);
37402c24a82SJosef Bacik 	mutex_unlock(&inode->i_mutex);
37502c24a82SJosef Bacik 
37602c24a82SJosef Bacik 	return ret;
3771da177e4SLinus Torvalds }
3781da177e4SLinus Torvalds 
3794b6f5d20SArjan van de Ven static const struct file_operations hostfs_file_fops = {
3801da177e4SLinus Torvalds 	.llseek		= generic_file_llseek,
381543ade1fSBadari Pulavarty 	.read		= do_sync_read,
3825ffc4ef4SJens Axboe 	.splice_read	= generic_file_splice_read,
3831da177e4SLinus Torvalds 	.aio_read	= generic_file_aio_read,
3841da177e4SLinus Torvalds 	.aio_write	= generic_file_aio_write,
385543ade1fSBadari Pulavarty 	.write		= do_sync_write,
3861da177e4SLinus Torvalds 	.mmap		= generic_file_mmap,
3871da177e4SLinus Torvalds 	.open		= hostfs_file_open,
38865984ff9SRichard Weinberger 	.release	= hostfs_file_release,
3891da177e4SLinus Torvalds 	.fsync		= hostfs_fsync,
3901da177e4SLinus Torvalds };
3911da177e4SLinus Torvalds 
3924b6f5d20SArjan van de Ven static const struct file_operations hostfs_dir_fops = {
3931da177e4SLinus Torvalds 	.llseek		= generic_file_llseek,
3948e28bc7eSAl Viro 	.iterate	= hostfs_readdir,
3951da177e4SLinus Torvalds 	.read		= generic_read_dir,
3961da177e4SLinus Torvalds };
3971da177e4SLinus Torvalds 
3989e443bc3SJames Hogan static int hostfs_writepage(struct page *page, struct writeback_control *wbc)
3991da177e4SLinus Torvalds {
4001da177e4SLinus Torvalds 	struct address_space *mapping = page->mapping;
4011da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
4021da177e4SLinus Torvalds 	char *buffer;
4031da177e4SLinus Torvalds 	unsigned long long base;
4041da177e4SLinus Torvalds 	int count = PAGE_CACHE_SIZE;
4051da177e4SLinus Torvalds 	int end_index = inode->i_size >> PAGE_CACHE_SHIFT;
4061da177e4SLinus Torvalds 	int err;
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds 	if (page->index >= end_index)
4091da177e4SLinus Torvalds 		count = inode->i_size & (PAGE_CACHE_SIZE-1);
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds 	buffer = kmap(page);
4121da177e4SLinus Torvalds 	base = ((unsigned long long) page->index) << PAGE_CACHE_SHIFT;
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds 	err = write_file(HOSTFS_I(inode)->fd, &base, buffer, count);
4151da177e4SLinus Torvalds 	if (err != count) {
4161da177e4SLinus Torvalds 		ClearPageUptodate(page);
4171da177e4SLinus Torvalds 		goto out;
4181da177e4SLinus Torvalds 	}
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds 	if (base > inode->i_size)
4211da177e4SLinus Torvalds 		inode->i_size = base;
4221da177e4SLinus Torvalds 
4231da177e4SLinus Torvalds 	if (PageError(page))
4241da177e4SLinus Torvalds 		ClearPageError(page);
4251da177e4SLinus Torvalds 	err = 0;
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds  out:
4281da177e4SLinus Torvalds 	kunmap(page);
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 	unlock_page(page);
4311da177e4SLinus Torvalds 	return err;
4321da177e4SLinus Torvalds }
4331da177e4SLinus Torvalds 
4349e443bc3SJames Hogan static int hostfs_readpage(struct file *file, struct page *page)
4351da177e4SLinus Torvalds {
4361da177e4SLinus Torvalds 	char *buffer;
4371da177e4SLinus Torvalds 	long long start;
4381da177e4SLinus Torvalds 	int err = 0;
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds 	start = (long long) page->index << PAGE_CACHE_SHIFT;
4411da177e4SLinus Torvalds 	buffer = kmap(page);
4421da177e4SLinus Torvalds 	err = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer,
4431da177e4SLinus Torvalds 			PAGE_CACHE_SIZE);
44484b3db04SJeff Dike 	if (err < 0)
44584b3db04SJeff Dike 		goto out;
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds 	memset(&buffer[err], 0, PAGE_CACHE_SIZE - err);
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds 	flush_dcache_page(page);
4501da177e4SLinus Torvalds 	SetPageUptodate(page);
4511da177e4SLinus Torvalds 	if (PageError(page)) ClearPageError(page);
4521da177e4SLinus Torvalds 	err = 0;
4531da177e4SLinus Torvalds  out:
4541da177e4SLinus Torvalds 	kunmap(page);
4551da177e4SLinus Torvalds 	unlock_page(page);
456f1adc05eSJeff Dike 	return err;
4571da177e4SLinus Torvalds }
4581da177e4SLinus Torvalds 
4599e443bc3SJames Hogan static int hostfs_write_begin(struct file *file, struct address_space *mapping,
460ae361ff4SNick Piggin 			      loff_t pos, unsigned len, unsigned flags,
461ae361ff4SNick Piggin 			      struct page **pagep, void **fsdata)
4621da177e4SLinus Torvalds {
463ae361ff4SNick Piggin 	pgoff_t index = pos >> PAGE_CACHE_SHIFT;
464ae361ff4SNick Piggin 
46554566b2cSNick Piggin 	*pagep = grab_cache_page_write_begin(mapping, index, flags);
466ae361ff4SNick Piggin 	if (!*pagep)
467ae361ff4SNick Piggin 		return -ENOMEM;
468ae361ff4SNick Piggin 	return 0;
469ae361ff4SNick Piggin }
470ae361ff4SNick Piggin 
4719e443bc3SJames Hogan static int hostfs_write_end(struct file *file, struct address_space *mapping,
472ae361ff4SNick Piggin 			    loff_t pos, unsigned len, unsigned copied,
473ae361ff4SNick Piggin 			    struct page *page, void *fsdata)
474ae361ff4SNick Piggin {
475ae361ff4SNick Piggin 	struct inode *inode = mapping->host;
476ae361ff4SNick Piggin 	void *buffer;
477ae361ff4SNick Piggin 	unsigned from = pos & (PAGE_CACHE_SIZE - 1);
4781da177e4SLinus Torvalds 	int err;
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds 	buffer = kmap(page);
481ae361ff4SNick Piggin 	err = write_file(FILE_HOSTFS_I(file)->fd, &pos, buffer + from, copied);
4821da177e4SLinus Torvalds 	kunmap(page);
4831da177e4SLinus Torvalds 
484ae361ff4SNick Piggin 	if (!PageUptodate(page) && err == PAGE_CACHE_SIZE)
485ae361ff4SNick Piggin 		SetPageUptodate(page);
4861da177e4SLinus Torvalds 
48784b3db04SJeff Dike 	/*
48884b3db04SJeff Dike 	 * If err > 0, write_file has added err to pos, so we are comparing
489ae361ff4SNick Piggin 	 * i_size against the last byte written.
490ae361ff4SNick Piggin 	 */
491ae361ff4SNick Piggin 	if (err > 0 && (pos > inode->i_size))
492ae361ff4SNick Piggin 		inode->i_size = pos;
493ae361ff4SNick Piggin 	unlock_page(page);
494ae361ff4SNick Piggin 	page_cache_release(page);
49530f04a4eSPaolo 'Blaisorblade' Giarrusso 
496f1adc05eSJeff Dike 	return err;
4971da177e4SLinus Torvalds }
4981da177e4SLinus Torvalds 
499f5e54d6eSChristoph Hellwig static const struct address_space_operations hostfs_aops = {
5001da177e4SLinus Torvalds 	.writepage 	= hostfs_writepage,
5011da177e4SLinus Torvalds 	.readpage	= hostfs_readpage,
502ffa0aea6SPaolo 'Blaisorblade' Giarrusso 	.set_page_dirty = __set_page_dirty_nobuffers,
503ae361ff4SNick Piggin 	.write_begin	= hostfs_write_begin,
504ae361ff4SNick Piggin 	.write_end	= hostfs_write_end,
5051da177e4SLinus Torvalds };
5061da177e4SLinus Torvalds 
5074754b825SAl Viro static int read_name(struct inode *ino, char *name)
5081da177e4SLinus Torvalds {
5094754b825SAl Viro 	dev_t rdev;
5104754b825SAl Viro 	struct hostfs_stat st;
5114754b825SAl Viro 	int err = stat_file(name, &st, -1);
5124754b825SAl Viro 	if (err)
5134754b825SAl Viro 		return err;
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds 	/* Reencode maj and min with the kernel encoding.*/
5164754b825SAl Viro 	rdev = MKDEV(st.maj, st.min);
5171da177e4SLinus Torvalds 
5184754b825SAl Viro 	switch (st.mode & S_IFMT) {
5194754b825SAl Viro 	case S_IFLNK:
520d0352d3eSAl Viro 		ino->i_op = &hostfs_link_iops;
5214754b825SAl Viro 		break;
5224754b825SAl Viro 	case S_IFDIR:
5234754b825SAl Viro 		ino->i_op = &hostfs_dir_iops;
5244754b825SAl Viro 		ino->i_fop = &hostfs_dir_fops;
5254754b825SAl Viro 		break;
5264754b825SAl Viro 	case S_IFCHR:
5274754b825SAl Viro 	case S_IFBLK:
5284754b825SAl Viro 	case S_IFIFO:
5294754b825SAl Viro 	case S_IFSOCK:
5304754b825SAl Viro 		init_special_inode(ino, st.mode & S_IFMT, rdev);
5314754b825SAl Viro 		ino->i_op = &hostfs_iops;
5324754b825SAl Viro 		break;
5331da177e4SLinus Torvalds 
5344754b825SAl Viro 	default:
5354754b825SAl Viro 		ino->i_op = &hostfs_iops;
5364754b825SAl Viro 		ino->i_fop = &hostfs_file_fops;
5374754b825SAl Viro 		ino->i_mapping->a_ops = &hostfs_aops;
5381da177e4SLinus Torvalds 	}
5394754b825SAl Viro 
5404754b825SAl Viro 	ino->i_ino = st.ino;
5414754b825SAl Viro 	ino->i_mode = st.mode;
542bfe86848SMiklos Szeredi 	set_nlink(ino, st.nlink);
54329f82ae5SEric W. Biederman 	i_uid_write(ino, st.uid);
54429f82ae5SEric W. Biederman 	i_gid_write(ino, st.gid);
5454754b825SAl Viro 	ino->i_atime = st.atime;
5464754b825SAl Viro 	ino->i_mtime = st.mtime;
5474754b825SAl Viro 	ino->i_ctime = st.ctime;
5484754b825SAl Viro 	ino->i_size = st.size;
5494754b825SAl Viro 	ino->i_blocks = st.blocks;
5504754b825SAl Viro 	return 0;
5511da177e4SLinus Torvalds }
5521da177e4SLinus Torvalds 
5539e443bc3SJames Hogan static int hostfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
554ebfc3b49SAl Viro 			 bool excl)
5551da177e4SLinus Torvalds {
5561da177e4SLinus Torvalds 	struct inode *inode;
5571da177e4SLinus Torvalds 	char *name;
5581da177e4SLinus Torvalds 	int error, fd;
5591da177e4SLinus Torvalds 
5600a370e5dSDavid Howells 	inode = hostfs_iget(dir->i_sb);
5610a370e5dSDavid Howells 	if (IS_ERR(inode)) {
5620a370e5dSDavid Howells 		error = PTR_ERR(inode);
56384b3db04SJeff Dike 		goto out;
5640a370e5dSDavid Howells 	}
5651da177e4SLinus Torvalds 
5661da177e4SLinus Torvalds 	error = -ENOMEM;
567c5322220SAl Viro 	name = dentry_name(dentry);
5681da177e4SLinus Torvalds 	if (name == NULL)
5691da177e4SLinus Torvalds 		goto out_put;
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds 	fd = file_create(name,
5721da177e4SLinus Torvalds 			 mode & S_IRUSR, mode & S_IWUSR, mode & S_IXUSR,
5731da177e4SLinus Torvalds 			 mode & S_IRGRP, mode & S_IWGRP, mode & S_IXGRP,
5741da177e4SLinus Torvalds 			 mode & S_IROTH, mode & S_IWOTH, mode & S_IXOTH);
5754754b825SAl Viro 	if (fd < 0)
5761da177e4SLinus Torvalds 		error = fd;
5774754b825SAl Viro 	else
5785e2df28cSAl Viro 		error = read_name(inode, name);
5791da177e4SLinus Torvalds 
580e9193059SAl Viro 	__putname(name);
5811da177e4SLinus Torvalds 	if (error)
5821da177e4SLinus Torvalds 		goto out_put;
5831da177e4SLinus Torvalds 
5841da177e4SLinus Torvalds 	HOSTFS_I(inode)->fd = fd;
5851da177e4SLinus Torvalds 	HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE;
5861da177e4SLinus Torvalds 	d_instantiate(dentry, inode);
587f1adc05eSJeff Dike 	return 0;
5881da177e4SLinus Torvalds 
5891da177e4SLinus Torvalds  out_put:
5901da177e4SLinus Torvalds 	iput(inode);
5911da177e4SLinus Torvalds  out:
592f1adc05eSJeff Dike 	return error;
5931da177e4SLinus Torvalds }
5941da177e4SLinus Torvalds 
5959e443bc3SJames Hogan static struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry,
59600cd8dd3SAl Viro 				    unsigned int flags)
5971da177e4SLinus Torvalds {
5981da177e4SLinus Torvalds 	struct inode *inode;
5991da177e4SLinus Torvalds 	char *name;
6001da177e4SLinus Torvalds 	int err;
6011da177e4SLinus Torvalds 
6020a370e5dSDavid Howells 	inode = hostfs_iget(ino->i_sb);
6030a370e5dSDavid Howells 	if (IS_ERR(inode)) {
6040a370e5dSDavid Howells 		err = PTR_ERR(inode);
6051da177e4SLinus Torvalds 		goto out;
6060a370e5dSDavid Howells 	}
6071da177e4SLinus Torvalds 
6081da177e4SLinus Torvalds 	err = -ENOMEM;
609c5322220SAl Viro 	name = dentry_name(dentry);
6101da177e4SLinus Torvalds 	if (name == NULL)
6111da177e4SLinus Torvalds 		goto out_put;
6121da177e4SLinus Torvalds 
6131da177e4SLinus Torvalds 	err = read_name(inode, name);
6145e2df28cSAl Viro 
615e9193059SAl Viro 	__putname(name);
6161da177e4SLinus Torvalds 	if (err == -ENOENT) {
6171da177e4SLinus Torvalds 		iput(inode);
6181da177e4SLinus Torvalds 		inode = NULL;
6191da177e4SLinus Torvalds 	}
6201da177e4SLinus Torvalds 	else if (err)
6211da177e4SLinus Torvalds 		goto out_put;
6221da177e4SLinus Torvalds 
6231da177e4SLinus Torvalds 	d_add(dentry, inode);
624f1adc05eSJeff Dike 	return NULL;
6251da177e4SLinus Torvalds 
6261da177e4SLinus Torvalds  out_put:
6271da177e4SLinus Torvalds 	iput(inode);
6281da177e4SLinus Torvalds  out:
629f1adc05eSJeff Dike 	return ERR_PTR(err);
6301da177e4SLinus Torvalds }
6311da177e4SLinus Torvalds 
6329e443bc3SJames Hogan static int hostfs_link(struct dentry *to, struct inode *ino,
6339e443bc3SJames Hogan 		       struct dentry *from)
6341da177e4SLinus Torvalds {
6351da177e4SLinus Torvalds 	char *from_name, *to_name;
6361da177e4SLinus Torvalds 	int err;
6371da177e4SLinus Torvalds 
638c5322220SAl Viro 	if ((from_name = dentry_name(from)) == NULL)
639f1adc05eSJeff Dike 		return -ENOMEM;
640c5322220SAl Viro 	to_name = dentry_name(to);
6411da177e4SLinus Torvalds 	if (to_name == NULL) {
642e9193059SAl Viro 		__putname(from_name);
643f1adc05eSJeff Dike 		return -ENOMEM;
6441da177e4SLinus Torvalds 	}
6451da177e4SLinus Torvalds 	err = link_file(to_name, from_name);
646e9193059SAl Viro 	__putname(from_name);
647e9193059SAl Viro 	__putname(to_name);
648f1adc05eSJeff Dike 	return err;
6491da177e4SLinus Torvalds }
6501da177e4SLinus Torvalds 
6519e443bc3SJames Hogan static int hostfs_unlink(struct inode *ino, struct dentry *dentry)
6521da177e4SLinus Torvalds {
6531da177e4SLinus Torvalds 	char *file;
6541da177e4SLinus Torvalds 	int err;
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds 	if (append)
657f1adc05eSJeff Dike 		return -EPERM;
6581da177e4SLinus Torvalds 
659f8d7e187SAl Viro 	if ((file = dentry_name(dentry)) == NULL)
660f8d7e187SAl Viro 		return -ENOMEM;
661f8d7e187SAl Viro 
6621da177e4SLinus Torvalds 	err = unlink_file(file);
663e9193059SAl Viro 	__putname(file);
664f1adc05eSJeff Dike 	return err;
6651da177e4SLinus Torvalds }
6661da177e4SLinus Torvalds 
6679e443bc3SJames Hogan static int hostfs_symlink(struct inode *ino, struct dentry *dentry,
6689e443bc3SJames Hogan 			  const char *to)
6691da177e4SLinus Torvalds {
6701da177e4SLinus Torvalds 	char *file;
6711da177e4SLinus Torvalds 	int err;
6721da177e4SLinus Torvalds 
673c5322220SAl Viro 	if ((file = dentry_name(dentry)) == NULL)
674f1adc05eSJeff Dike 		return -ENOMEM;
6751da177e4SLinus Torvalds 	err = make_symlink(file, to);
676e9193059SAl Viro 	__putname(file);
677f1adc05eSJeff Dike 	return err;
6781da177e4SLinus Torvalds }
6791da177e4SLinus Torvalds 
6809e443bc3SJames Hogan static int hostfs_mkdir(struct inode *ino, struct dentry *dentry, umode_t mode)
6811da177e4SLinus Torvalds {
6821da177e4SLinus Torvalds 	char *file;
6831da177e4SLinus Torvalds 	int err;
6841da177e4SLinus Torvalds 
685c5322220SAl Viro 	if ((file = dentry_name(dentry)) == NULL)
686f1adc05eSJeff Dike 		return -ENOMEM;
6871da177e4SLinus Torvalds 	err = do_mkdir(file, mode);
688e9193059SAl Viro 	__putname(file);
689f1adc05eSJeff Dike 	return err;
6901da177e4SLinus Torvalds }
6911da177e4SLinus Torvalds 
6929e443bc3SJames Hogan static int hostfs_rmdir(struct inode *ino, struct dentry *dentry)
6931da177e4SLinus Torvalds {
6941da177e4SLinus Torvalds 	char *file;
6951da177e4SLinus Torvalds 	int err;
6961da177e4SLinus Torvalds 
697c5322220SAl Viro 	if ((file = dentry_name(dentry)) == NULL)
698f1adc05eSJeff Dike 		return -ENOMEM;
6991da177e4SLinus Torvalds 	err = do_rmdir(file);
700e9193059SAl Viro 	__putname(file);
701f1adc05eSJeff Dike 	return err;
7021da177e4SLinus Torvalds }
7031da177e4SLinus Torvalds 
7041a67aafbSAl Viro static int hostfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
7051da177e4SLinus Torvalds {
7061da177e4SLinus Torvalds 	struct inode *inode;
7071da177e4SLinus Torvalds 	char *name;
7080a370e5dSDavid Howells 	int err;
7091da177e4SLinus Torvalds 
7100a370e5dSDavid Howells 	inode = hostfs_iget(dir->i_sb);
7110a370e5dSDavid Howells 	if (IS_ERR(inode)) {
7120a370e5dSDavid Howells 		err = PTR_ERR(inode);
7131da177e4SLinus Torvalds 		goto out;
7140a370e5dSDavid Howells 	}
7151da177e4SLinus Torvalds 
7161da177e4SLinus Torvalds 	err = -ENOMEM;
717c5322220SAl Viro 	name = dentry_name(dentry);
7181da177e4SLinus Torvalds 	if (name == NULL)
7191da177e4SLinus Torvalds 		goto out_put;
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds 	init_special_inode(inode, mode, dev);
72288f6cd0cSJohannes Stezenbach 	err = do_mknod(name, mode, MAJOR(dev), MINOR(dev));
723e9193059SAl Viro 	if (!err)
7241da177e4SLinus Torvalds 		goto out_free;
7251da177e4SLinus Torvalds 
7261da177e4SLinus Torvalds 	err = read_name(inode, name);
727e9193059SAl Viro 	__putname(name);
7285e2df28cSAl Viro 	if (err)
7295e2df28cSAl Viro 		goto out_put;
7301da177e4SLinus Torvalds 	if (err)
7311da177e4SLinus Torvalds 		goto out_put;
7321da177e4SLinus Torvalds 
7331da177e4SLinus Torvalds 	d_instantiate(dentry, inode);
734f1adc05eSJeff Dike 	return 0;
7351da177e4SLinus Torvalds 
7361da177e4SLinus Torvalds  out_free:
737e9193059SAl Viro 	__putname(name);
7381da177e4SLinus Torvalds  out_put:
7391da177e4SLinus Torvalds 	iput(inode);
7401da177e4SLinus Torvalds  out:
741f1adc05eSJeff Dike 	return err;
7421da177e4SLinus Torvalds }
7431da177e4SLinus Torvalds 
7449e443bc3SJames Hogan static int hostfs_rename(struct inode *from_ino, struct dentry *from,
7451da177e4SLinus Torvalds 			 struct inode *to_ino, struct dentry *to)
7461da177e4SLinus Torvalds {
7471da177e4SLinus Torvalds 	char *from_name, *to_name;
7481da177e4SLinus Torvalds 	int err;
7491da177e4SLinus Torvalds 
750c5322220SAl Viro 	if ((from_name = dentry_name(from)) == NULL)
751f1adc05eSJeff Dike 		return -ENOMEM;
752c5322220SAl Viro 	if ((to_name = dentry_name(to)) == NULL) {
753e9193059SAl Viro 		__putname(from_name);
754f1adc05eSJeff Dike 		return -ENOMEM;
7551da177e4SLinus Torvalds 	}
7561da177e4SLinus Torvalds 	err = rename_file(from_name, to_name);
757e9193059SAl Viro 	__putname(from_name);
758e9193059SAl Viro 	__putname(to_name);
759f1adc05eSJeff Dike 	return err;
7601da177e4SLinus Torvalds }
7611da177e4SLinus Torvalds 
7629e443bc3SJames Hogan static int hostfs_permission(struct inode *ino, int desired)
7631da177e4SLinus Torvalds {
7641da177e4SLinus Torvalds 	char *name;
7651da177e4SLinus Torvalds 	int r = 0, w = 0, x = 0, err;
7661da177e4SLinus Torvalds 
76710556cb2SAl Viro 	if (desired & MAY_NOT_BLOCK)
768b74c79e9SNick Piggin 		return -ECHILD;
769b74c79e9SNick Piggin 
7701da177e4SLinus Torvalds 	if (desired & MAY_READ) r = 1;
7711da177e4SLinus Torvalds 	if (desired & MAY_WRITE) w = 1;
7721da177e4SLinus Torvalds 	if (desired & MAY_EXEC) x = 1;
773c5322220SAl Viro 	name = inode_name(ino);
774f1adc05eSJeff Dike 	if (name == NULL)
775f1adc05eSJeff Dike 		return -ENOMEM;
7761da177e4SLinus Torvalds 
7771da177e4SLinus Torvalds 	if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) ||
7781da177e4SLinus Torvalds 	    S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode))
7791da177e4SLinus Torvalds 		err = 0;
7801da177e4SLinus Torvalds 	else
7811da177e4SLinus Torvalds 		err = access_file(name, r, w, x);
782e9193059SAl Viro 	__putname(name);
7831da177e4SLinus Torvalds 	if (!err)
7842830ba7fSAl Viro 		err = generic_permission(ino, desired);
7851da177e4SLinus Torvalds 	return err;
7861da177e4SLinus Torvalds }
7871da177e4SLinus Torvalds 
7889e443bc3SJames Hogan static int hostfs_setattr(struct dentry *dentry, struct iattr *attr)
7891da177e4SLinus Torvalds {
7901025774cSChristoph Hellwig 	struct inode *inode = dentry->d_inode;
7911da177e4SLinus Torvalds 	struct hostfs_iattr attrs;
7921da177e4SLinus Torvalds 	char *name;
7931da177e4SLinus Torvalds 	int err;
7941da177e4SLinus Torvalds 
7951025774cSChristoph Hellwig 	int fd = HOSTFS_I(inode)->fd;
7965822b7faSAlberto Bertogli 
7971025774cSChristoph Hellwig 	err = inode_change_ok(inode, attr);
7981da177e4SLinus Torvalds 	if (err)
7991da177e4SLinus Torvalds 		return err;
8001da177e4SLinus Torvalds 
8011da177e4SLinus Torvalds 	if (append)
8021da177e4SLinus Torvalds 		attr->ia_valid &= ~ATTR_SIZE;
8031da177e4SLinus Torvalds 
8041da177e4SLinus Torvalds 	attrs.ia_valid = 0;
8051da177e4SLinus Torvalds 	if (attr->ia_valid & ATTR_MODE) {
8061da177e4SLinus Torvalds 		attrs.ia_valid |= HOSTFS_ATTR_MODE;
8071da177e4SLinus Torvalds 		attrs.ia_mode = attr->ia_mode;
8081da177e4SLinus Torvalds 	}
8091da177e4SLinus Torvalds 	if (attr->ia_valid & ATTR_UID) {
8101da177e4SLinus Torvalds 		attrs.ia_valid |= HOSTFS_ATTR_UID;
81129f82ae5SEric W. Biederman 		attrs.ia_uid = from_kuid(&init_user_ns, attr->ia_uid);
8121da177e4SLinus Torvalds 	}
8131da177e4SLinus Torvalds 	if (attr->ia_valid & ATTR_GID) {
8141da177e4SLinus Torvalds 		attrs.ia_valid |= HOSTFS_ATTR_GID;
81529f82ae5SEric W. Biederman 		attrs.ia_gid = from_kgid(&init_user_ns, attr->ia_gid);
8161da177e4SLinus Torvalds 	}
8171da177e4SLinus Torvalds 	if (attr->ia_valid & ATTR_SIZE) {
8181da177e4SLinus Torvalds 		attrs.ia_valid |= HOSTFS_ATTR_SIZE;
8191da177e4SLinus Torvalds 		attrs.ia_size = attr->ia_size;
8201da177e4SLinus Torvalds 	}
8211da177e4SLinus Torvalds 	if (attr->ia_valid & ATTR_ATIME) {
8221da177e4SLinus Torvalds 		attrs.ia_valid |= HOSTFS_ATTR_ATIME;
8231da177e4SLinus Torvalds 		attrs.ia_atime = attr->ia_atime;
8241da177e4SLinus Torvalds 	}
8251da177e4SLinus Torvalds 	if (attr->ia_valid & ATTR_MTIME) {
8261da177e4SLinus Torvalds 		attrs.ia_valid |= HOSTFS_ATTR_MTIME;
8271da177e4SLinus Torvalds 		attrs.ia_mtime = attr->ia_mtime;
8281da177e4SLinus Torvalds 	}
8291da177e4SLinus Torvalds 	if (attr->ia_valid & ATTR_CTIME) {
8301da177e4SLinus Torvalds 		attrs.ia_valid |= HOSTFS_ATTR_CTIME;
8311da177e4SLinus Torvalds 		attrs.ia_ctime = attr->ia_ctime;
8321da177e4SLinus Torvalds 	}
8331da177e4SLinus Torvalds 	if (attr->ia_valid & ATTR_ATIME_SET) {
8341da177e4SLinus Torvalds 		attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET;
8351da177e4SLinus Torvalds 	}
8361da177e4SLinus Torvalds 	if (attr->ia_valid & ATTR_MTIME_SET) {
8371da177e4SLinus Torvalds 		attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET;
8381da177e4SLinus Torvalds 	}
839c5322220SAl Viro 	name = dentry_name(dentry);
840f1adc05eSJeff Dike 	if (name == NULL)
841f1adc05eSJeff Dike 		return -ENOMEM;
8425822b7faSAlberto Bertogli 	err = set_attr(name, &attrs, fd);
843e9193059SAl Viro 	__putname(name);
8441da177e4SLinus Torvalds 	if (err)
845f1adc05eSJeff Dike 		return err;
8461da177e4SLinus Torvalds 
8471025774cSChristoph Hellwig 	if ((attr->ia_valid & ATTR_SIZE) &&
848bc077320SMarco Stornelli 	    attr->ia_size != i_size_read(inode))
8493be2be0aSMarco Stornelli 		truncate_setsize(inode, attr->ia_size);
8501025774cSChristoph Hellwig 
8511025774cSChristoph Hellwig 	setattr_copy(inode, attr);
8521025774cSChristoph Hellwig 	mark_inode_dirty(inode);
8531025774cSChristoph Hellwig 	return 0;
8541da177e4SLinus Torvalds }
8551da177e4SLinus Torvalds 
85692e1d5beSArjan van de Ven static const struct inode_operations hostfs_iops = {
8571da177e4SLinus Torvalds 	.permission	= hostfs_permission,
8581da177e4SLinus Torvalds 	.setattr	= hostfs_setattr,
8591da177e4SLinus Torvalds };
8601da177e4SLinus Torvalds 
86192e1d5beSArjan van de Ven static const struct inode_operations hostfs_dir_iops = {
8621da177e4SLinus Torvalds 	.create		= hostfs_create,
8631da177e4SLinus Torvalds 	.lookup		= hostfs_lookup,
8641da177e4SLinus Torvalds 	.link		= hostfs_link,
8651da177e4SLinus Torvalds 	.unlink		= hostfs_unlink,
8661da177e4SLinus Torvalds 	.symlink	= hostfs_symlink,
8671da177e4SLinus Torvalds 	.mkdir		= hostfs_mkdir,
8681da177e4SLinus Torvalds 	.rmdir		= hostfs_rmdir,
8691da177e4SLinus Torvalds 	.mknod		= hostfs_mknod,
8701da177e4SLinus Torvalds 	.rename		= hostfs_rename,
8711da177e4SLinus Torvalds 	.permission	= hostfs_permission,
8721da177e4SLinus Torvalds 	.setattr	= hostfs_setattr,
8731da177e4SLinus Torvalds };
8741da177e4SLinus Torvalds 
875d0352d3eSAl Viro static void *hostfs_follow_link(struct dentry *dentry, struct nameidata *nd)
8761da177e4SLinus Torvalds {
877d0352d3eSAl Viro 	char *link = __getname();
878d0352d3eSAl Viro 	if (link) {
879d0352d3eSAl Viro 		char *path = dentry_name(dentry);
880d0352d3eSAl Viro 		int err = -ENOMEM;
881d0352d3eSAl Viro 		if (path) {
8823b6036d1SAl Viro 			err = hostfs_do_readlink(path, link, PATH_MAX);
883d0352d3eSAl Viro 			if (err == PATH_MAX)
8841da177e4SLinus Torvalds 				err = -E2BIG;
885e9193059SAl Viro 			__putname(path);
8861da177e4SLinus Torvalds 		}
887d0352d3eSAl Viro 		if (err < 0) {
888d0352d3eSAl Viro 			__putname(link);
889d0352d3eSAl Viro 			link = ERR_PTR(err);
890d0352d3eSAl Viro 		}
891d0352d3eSAl Viro 	} else {
892d0352d3eSAl Viro 		link = ERR_PTR(-ENOMEM);
8931da177e4SLinus Torvalds 	}
8941da177e4SLinus Torvalds 
895d0352d3eSAl Viro 	nd_set_link(nd, link);
896d0352d3eSAl Viro 	return NULL;
897d0352d3eSAl Viro }
898d0352d3eSAl Viro 
899d0352d3eSAl Viro static void hostfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
900d0352d3eSAl Viro {
901d0352d3eSAl Viro 	char *s = nd_get_link(nd);
902d0352d3eSAl Viro 	if (!IS_ERR(s))
903d0352d3eSAl Viro 		__putname(s);
904d0352d3eSAl Viro }
905d0352d3eSAl Viro 
906d0352d3eSAl Viro static const struct inode_operations hostfs_link_iops = {
907d0352d3eSAl Viro 	.readlink	= generic_readlink,
908d0352d3eSAl Viro 	.follow_link	= hostfs_follow_link,
909d0352d3eSAl Viro 	.put_link	= hostfs_put_link,
9101da177e4SLinus Torvalds };
9111da177e4SLinus Torvalds 
9121da177e4SLinus Torvalds static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent)
9131da177e4SLinus Torvalds {
9141da177e4SLinus Torvalds 	struct inode *root_inode;
91575e8defbSPaolo 'Blaisorblade' Giarrusso 	char *host_root_path, *req_root = d;
9161da177e4SLinus Torvalds 	int err;
9171da177e4SLinus Torvalds 
9181da177e4SLinus Torvalds 	sb->s_blocksize = 1024;
9191da177e4SLinus Torvalds 	sb->s_blocksize_bits = 10;
9201da177e4SLinus Torvalds 	sb->s_magic = HOSTFS_SUPER_MAGIC;
9211da177e4SLinus Torvalds 	sb->s_op = &hostfs_sbops;
922b26d4cd3SAl Viro 	sb->s_d_op = &simple_dentry_operations;
923752fa51eSWolfgang Illmeyer 	sb->s_maxbytes = MAX_LFS_FILESIZE;
9241da177e4SLinus Torvalds 
925a6eb0be6SPaolo 'Blaisorblade' Giarrusso 	/* NULL is printed as <NULL> by sprintf: avoid that. */
92675e8defbSPaolo 'Blaisorblade' Giarrusso 	if (req_root == NULL)
92775e8defbSPaolo 'Blaisorblade' Giarrusso 		req_root = "";
9281da177e4SLinus Torvalds 
9291da177e4SLinus Torvalds 	err = -ENOMEM;
930601d2c38SAl Viro 	sb->s_fs_info = host_root_path =
931601d2c38SAl Viro 		kmalloc(strlen(root_ino) + strlen(req_root) + 2, GFP_KERNEL);
93275e8defbSPaolo 'Blaisorblade' Giarrusso 	if (host_root_path == NULL)
9331da177e4SLinus Torvalds 		goto out;
9341da177e4SLinus Torvalds 
93575e8defbSPaolo 'Blaisorblade' Giarrusso 	sprintf(host_root_path, "%s/%s", root_ino, req_root);
9361da177e4SLinus Torvalds 
93752b209f7SAl Viro 	root_inode = new_inode(sb);
93852b209f7SAl Viro 	if (!root_inode)
939601d2c38SAl Viro 		goto out;
9401da177e4SLinus Torvalds 
9414754b825SAl Viro 	err = read_name(root_inode, host_root_path);
9424754b825SAl Viro 	if (err)
9434754b825SAl Viro 		goto out_put;
94452b209f7SAl Viro 
9454754b825SAl Viro 	if (S_ISLNK(root_inode->i_mode)) {
94652b209f7SAl Viro 		char *name = follow_link(host_root_path);
94752b209f7SAl Viro 		if (IS_ERR(name))
94852b209f7SAl Viro 			err = PTR_ERR(name);
94952b209f7SAl Viro 		else
95052b209f7SAl Viro 			err = read_name(root_inode, name);
95152b209f7SAl Viro 		kfree(name);
9521da177e4SLinus Torvalds 		if (err)
9531da177e4SLinus Torvalds 			goto out_put;
9544754b825SAl Viro 	}
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds 	err = -ENOMEM;
95748fde701SAl Viro 	sb->s_root = d_make_root(root_inode);
9581da177e4SLinus Torvalds 	if (sb->s_root == NULL)
95948fde701SAl Viro 		goto out;
9601da177e4SLinus Torvalds 
961f1adc05eSJeff Dike 	return 0;
9621da177e4SLinus Torvalds 
9631da177e4SLinus Torvalds out_put:
9641da177e4SLinus Torvalds 	iput(root_inode);
9651da177e4SLinus Torvalds out:
966f1adc05eSJeff Dike 	return err;
9671da177e4SLinus Torvalds }
9681da177e4SLinus Torvalds 
9693c26ff6eSAl Viro static struct dentry *hostfs_read_sb(struct file_system_type *type,
9701da177e4SLinus Torvalds 			  int flags, const char *dev_name,
9713c26ff6eSAl Viro 			  void *data)
9721da177e4SLinus Torvalds {
9733c26ff6eSAl Viro 	return mount_nodev(type, flags, data, hostfs_fill_sb_common);
9741da177e4SLinus Torvalds }
9751da177e4SLinus Torvalds 
976601d2c38SAl Viro static void hostfs_kill_sb(struct super_block *s)
977601d2c38SAl Viro {
978601d2c38SAl Viro 	kill_anon_super(s);
979601d2c38SAl Viro 	kfree(s->s_fs_info);
980601d2c38SAl Viro }
981601d2c38SAl Viro 
9821da177e4SLinus Torvalds static struct file_system_type hostfs_type = {
9831da177e4SLinus Torvalds 	.owner 		= THIS_MODULE,
9841da177e4SLinus Torvalds 	.name 		= "hostfs",
9853c26ff6eSAl Viro 	.mount	 	= hostfs_read_sb,
986601d2c38SAl Viro 	.kill_sb	= hostfs_kill_sb,
9871da177e4SLinus Torvalds 	.fs_flags 	= 0,
9881da177e4SLinus Torvalds };
9893e64fe5bSEric W. Biederman MODULE_ALIAS_FS("hostfs");
9901da177e4SLinus Torvalds 
9911da177e4SLinus Torvalds static int __init init_hostfs(void)
9921da177e4SLinus Torvalds {
993f1adc05eSJeff Dike 	return register_filesystem(&hostfs_type);
9941da177e4SLinus Torvalds }
9951da177e4SLinus Torvalds 
9961da177e4SLinus Torvalds static void __exit exit_hostfs(void)
9971da177e4SLinus Torvalds {
9981da177e4SLinus Torvalds 	unregister_filesystem(&hostfs_type);
9991da177e4SLinus Torvalds }
10001da177e4SLinus Torvalds 
10011da177e4SLinus Torvalds module_init(init_hostfs)
10021da177e4SLinus Torvalds module_exit(exit_hostfs)
10031da177e4SLinus Torvalds MODULE_LICENSE("GPL");
1004