xref: /linux/fs/hostfs/hostfs_kern.c (revision a9d1958b4b9a688becceda44938baac99a62fbd7)
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;
2769886e67SRichard Weinberger 	struct mutex open_mutex;
281da177e4SLinus Torvalds };
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds static inline struct hostfs_inode_info *HOSTFS_I(struct inode *inode)
311da177e4SLinus Torvalds {
32f1adc05eSJeff Dike 	return list_entry(inode, struct hostfs_inode_info, vfs_inode);
331da177e4SLinus Torvalds }
341da177e4SLinus Torvalds 
35496ad9aaSAl Viro #define FILE_HOSTFS_I(file) HOSTFS_I(file_inode(file))
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds /* Changed in hostfs_args before the kernel starts running */
38a6eb0be6SPaolo 'Blaisorblade' Giarrusso static char *root_ino = "";
391da177e4SLinus Torvalds static int append = 0;
401da177e4SLinus Torvalds 
4192e1d5beSArjan van de Ven static const struct inode_operations hostfs_iops;
4292e1d5beSArjan van de Ven static const struct inode_operations hostfs_dir_iops;
43d0352d3eSAl Viro static const struct inode_operations hostfs_link_iops;
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds #ifndef MODULE
461da177e4SLinus Torvalds static int __init hostfs_args(char *options, int *add)
471da177e4SLinus Torvalds {
481da177e4SLinus Torvalds 	char *ptr;
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds 	ptr = strchr(options, ',');
511da177e4SLinus Torvalds 	if (ptr != NULL)
521da177e4SLinus Torvalds 		*ptr++ = '\0';
531da177e4SLinus Torvalds 	if (*options != '\0')
541da177e4SLinus Torvalds 		root_ino = options;
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds 	options = ptr;
571da177e4SLinus Torvalds 	while (options) {
581da177e4SLinus Torvalds 		ptr = strchr(options, ',');
591da177e4SLinus Torvalds 		if (ptr != NULL)
601da177e4SLinus Torvalds 			*ptr++ = '\0';
611da177e4SLinus Torvalds 		if (*options != '\0') {
621da177e4SLinus Torvalds 			if (!strcmp(options, "append"))
631da177e4SLinus Torvalds 				append = 1;
641da177e4SLinus Torvalds 			else printf("hostfs_args - unsupported option - %s\n",
651da177e4SLinus Torvalds 				    options);
661da177e4SLinus Torvalds 		}
671da177e4SLinus Torvalds 		options = ptr;
681da177e4SLinus Torvalds 	}
69f1adc05eSJeff Dike 	return 0;
701da177e4SLinus Torvalds }
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds __uml_setup("hostfs=", hostfs_args,
731da177e4SLinus Torvalds "hostfs=<root dir>,<flags>,...\n"
741da177e4SLinus Torvalds "    This is used to set hostfs parameters.  The root directory argument\n"
751da177e4SLinus Torvalds "    is used to confine all hostfs mounts to within the specified directory\n"
761da177e4SLinus Torvalds "    tree on the host.  If this isn't specified, then a user inside UML can\n"
771da177e4SLinus Torvalds "    mount anything on the host that's accessible to the user that's running\n"
781da177e4SLinus Torvalds "    it.\n"
791da177e4SLinus Torvalds "    The only flag currently supported is 'append', which specifies that all\n"
801da177e4SLinus Torvalds "    files opened by hostfs will be opened in append mode.\n\n"
811da177e4SLinus Torvalds );
821da177e4SLinus Torvalds #endif
831da177e4SLinus Torvalds 
84e9193059SAl Viro static char *__dentry_name(struct dentry *dentry, char *name)
85e9193059SAl Viro {
86ec2447c2SNick Piggin 	char *p = dentry_path_raw(dentry, name, PATH_MAX);
87e9193059SAl Viro 	char *root;
88e9193059SAl Viro 	size_t len;
89e9193059SAl Viro 
90e9193059SAl Viro 	root = dentry->d_sb->s_fs_info;
91e9193059SAl Viro 	len = strlen(root);
92e9193059SAl Viro 	if (IS_ERR(p)) {
93e9193059SAl Viro 		__putname(name);
94e9193059SAl Viro 		return NULL;
95e9193059SAl Viro 	}
96aad50b1eSRichard Weinberger 
97aad50b1eSRichard Weinberger 	/*
98aad50b1eSRichard Weinberger 	 * This function relies on the fact that dentry_path_raw() will place
99aad50b1eSRichard Weinberger 	 * the path name at the end of the provided buffer.
100aad50b1eSRichard Weinberger 	 */
101aad50b1eSRichard Weinberger 	BUG_ON(p + strlen(p) + 1 != name + PATH_MAX);
102aad50b1eSRichard Weinberger 
103850a496fSAl Viro 	strlcpy(name, root, PATH_MAX);
104e9193059SAl Viro 	if (len > p - name) {
105e9193059SAl Viro 		__putname(name);
106e9193059SAl Viro 		return NULL;
107e9193059SAl Viro 	}
108c278e81bSRichard Weinberger 
109c278e81bSRichard Weinberger 	if (p > name + len)
110c278e81bSRichard Weinberger 		strcpy(name + len, p);
111c278e81bSRichard Weinberger 
112e9193059SAl Viro 	return name;
113e9193059SAl Viro }
114e9193059SAl Viro 
115c5322220SAl Viro static char *dentry_name(struct dentry *dentry)
1161da177e4SLinus Torvalds {
117e9193059SAl Viro 	char *name = __getname();
118e9193059SAl Viro 	if (!name)
119f1adc05eSJeff Dike 		return NULL;
1201da177e4SLinus Torvalds 
1219dcc5e8aSJames Hogan 	return __dentry_name(dentry, name);
1221da177e4SLinus Torvalds }
1231da177e4SLinus Torvalds 
124c5322220SAl Viro static char *inode_name(struct inode *ino)
1251da177e4SLinus Torvalds {
1261da177e4SLinus Torvalds 	struct dentry *dentry;
127ec2447c2SNick Piggin 	char *name;
128ec2447c2SNick Piggin 
129ec2447c2SNick Piggin 	dentry = d_find_alias(ino);
130ec2447c2SNick Piggin 	if (!dentry)
131e9193059SAl Viro 		return NULL;
1321da177e4SLinus Torvalds 
133ec2447c2SNick Piggin 	name = dentry_name(dentry);
134ec2447c2SNick Piggin 
135ec2447c2SNick Piggin 	dput(dentry);
136ec2447c2SNick Piggin 
137ec2447c2SNick Piggin 	return name;
1381da177e4SLinus Torvalds }
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds static char *follow_link(char *link)
1411da177e4SLinus Torvalds {
1421da177e4SLinus Torvalds 	int len, n;
1431da177e4SLinus Torvalds 	char *name, *resolved, *end;
1441da177e4SLinus Torvalds 
1457c950992SRichard Weinberger 	name = __getname();
1467c950992SRichard Weinberger 	if (!name) {
1471da177e4SLinus Torvalds 		n = -ENOMEM;
1487c950992SRichard Weinberger 		goto out_free;
1491da177e4SLinus Torvalds 	}
1507c950992SRichard Weinberger 
1517c950992SRichard Weinberger 	n = hostfs_do_readlink(link, name, PATH_MAX);
1521da177e4SLinus Torvalds 	if (n < 0)
1531da177e4SLinus Torvalds 		goto out_free;
1547c950992SRichard Weinberger 	else if (n == PATH_MAX) {
1557c950992SRichard Weinberger 		n = -E2BIG;
1567c950992SRichard Weinberger 		goto out_free;
1577c950992SRichard Weinberger 	}
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds 	if (*name == '/')
160f1adc05eSJeff Dike 		return name;
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds 	end = strrchr(link, '/');
1631da177e4SLinus Torvalds 	if (end == NULL)
164f1adc05eSJeff Dike 		return name;
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 	*(end + 1) = '\0';
1671da177e4SLinus Torvalds 	len = strlen(link) + strlen(name) + 1;
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 	resolved = kmalloc(len, GFP_KERNEL);
1701da177e4SLinus Torvalds 	if (resolved == NULL) {
1711da177e4SLinus Torvalds 		n = -ENOMEM;
1721da177e4SLinus Torvalds 		goto out_free;
1731da177e4SLinus Torvalds 	}
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds 	sprintf(resolved, "%s%s", link, name);
1767c950992SRichard Weinberger 	__putname(name);
1771da177e4SLinus Torvalds 	kfree(link);
178f1adc05eSJeff Dike 	return resolved;
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds  out_free:
1817c950992SRichard Weinberger 	__putname(name);
182f1adc05eSJeff Dike 	return ERR_PTR(n);
1831da177e4SLinus Torvalds }
1841da177e4SLinus Torvalds 
1850a370e5dSDavid Howells static struct inode *hostfs_iget(struct super_block *sb)
1860a370e5dSDavid Howells {
18752b209f7SAl Viro 	struct inode *inode = new_inode(sb);
1880a370e5dSDavid Howells 	if (!inode)
1890a370e5dSDavid Howells 		return ERR_PTR(-ENOMEM);
1900a370e5dSDavid Howells 	return inode;
1910a370e5dSDavid Howells }
1920a370e5dSDavid Howells 
1939e443bc3SJames Hogan static int hostfs_statfs(struct dentry *dentry, struct kstatfs *sf)
1941da177e4SLinus Torvalds {
19584b3db04SJeff Dike 	/*
19684b3db04SJeff Dike 	 * do_statfs uses struct statfs64 internally, but the linux kernel
1971da177e4SLinus Torvalds 	 * struct statfs still has 32-bit versions for most of these fields,
1981da177e4SLinus Torvalds 	 * so we convert them here
1991da177e4SLinus Torvalds 	 */
2001da177e4SLinus Torvalds 	int err;
2011da177e4SLinus Torvalds 	long long f_blocks;
2021da177e4SLinus Torvalds 	long long f_bfree;
2031da177e4SLinus Torvalds 	long long f_bavail;
2041da177e4SLinus Torvalds 	long long f_files;
2051da177e4SLinus Torvalds 	long long f_ffree;
2061da177e4SLinus Torvalds 
207601d2c38SAl Viro 	err = do_statfs(dentry->d_sb->s_fs_info,
2081da177e4SLinus Torvalds 			&sf->f_bsize, &f_blocks, &f_bfree, &f_bavail, &f_files,
2091da177e4SLinus Torvalds 			&f_ffree, &sf->f_fsid, sizeof(sf->f_fsid),
2101b627d57SRichard Weinberger 			&sf->f_namelen);
211f1adc05eSJeff Dike 	if (err)
212f1adc05eSJeff Dike 		return err;
2131da177e4SLinus Torvalds 	sf->f_blocks = f_blocks;
2141da177e4SLinus Torvalds 	sf->f_bfree = f_bfree;
2151da177e4SLinus Torvalds 	sf->f_bavail = f_bavail;
2161da177e4SLinus Torvalds 	sf->f_files = f_files;
2171da177e4SLinus Torvalds 	sf->f_ffree = f_ffree;
2181da177e4SLinus Torvalds 	sf->f_type = HOSTFS_SUPER_MAGIC;
219f1adc05eSJeff Dike 	return 0;
2201da177e4SLinus Torvalds }
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds static struct inode *hostfs_alloc_inode(struct super_block *sb)
2231da177e4SLinus Torvalds {
2241da177e4SLinus Torvalds 	struct hostfs_inode_info *hi;
2251da177e4SLinus Torvalds 
226371fdab1SJames Hogan 	hi = kmalloc(sizeof(*hi), GFP_KERNEL);
2271da177e4SLinus Torvalds 	if (hi == NULL)
228f1adc05eSJeff Dike 		return NULL;
229601d2c38SAl Viro 	hi->fd = -1;
230371fdab1SJames Hogan 	hi->mode = 0;
2311da177e4SLinus Torvalds 	inode_init_once(&hi->vfs_inode);
23269886e67SRichard Weinberger 	mutex_init(&hi->open_mutex);
233f1adc05eSJeff Dike 	return &hi->vfs_inode;
2341da177e4SLinus Torvalds }
2351da177e4SLinus Torvalds 
236e971a6d7SAl Viro static void hostfs_evict_inode(struct inode *inode)
2371da177e4SLinus Torvalds {
23891b0abe3SJohannes Weiner 	truncate_inode_pages_final(&inode->i_data);
239dbd5768fSJan Kara 	clear_inode(inode);
2401da177e4SLinus Torvalds 	if (HOSTFS_I(inode)->fd != -1) {
2411da177e4SLinus Torvalds 		close_file(&HOSTFS_I(inode)->fd);
2421da177e4SLinus Torvalds 		HOSTFS_I(inode)->fd = -1;
2431da177e4SLinus Torvalds 	}
2441da177e4SLinus Torvalds }
2451da177e4SLinus Torvalds 
246fa0d7e3dSNick Piggin static void hostfs_i_callback(struct rcu_head *head)
247fa0d7e3dSNick Piggin {
248fa0d7e3dSNick Piggin 	struct inode *inode = container_of(head, struct inode, i_rcu);
249fa0d7e3dSNick Piggin 	kfree(HOSTFS_I(inode));
250fa0d7e3dSNick Piggin }
251fa0d7e3dSNick Piggin 
2521da177e4SLinus Torvalds static void hostfs_destroy_inode(struct inode *inode)
2531da177e4SLinus Torvalds {
254fa0d7e3dSNick Piggin 	call_rcu(&inode->i_rcu, hostfs_i_callback);
2551da177e4SLinus Torvalds }
2561da177e4SLinus Torvalds 
25734c80b1dSAl Viro static int hostfs_show_options(struct seq_file *seq, struct dentry *root)
258dd2cc4dfSMiklos Szeredi {
25934c80b1dSAl Viro 	const char *root_path = root->d_sb->s_fs_info;
260dd2cc4dfSMiklos Szeredi 	size_t offset = strlen(root_ino) + 1;
261dd2cc4dfSMiklos Szeredi 
262dd2cc4dfSMiklos Szeredi 	if (strlen(root_path) > offset)
263dd2cc4dfSMiklos Szeredi 		seq_printf(seq, ",%s", root_path + offset);
264dd2cc4dfSMiklos Szeredi 
2657f74a668SRichard Weinberger 	if (append)
2667f74a668SRichard Weinberger 		seq_puts(seq, ",append");
2677f74a668SRichard Weinberger 
268dd2cc4dfSMiklos Szeredi 	return 0;
269dd2cc4dfSMiklos Szeredi }
270dd2cc4dfSMiklos Szeredi 
271ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations hostfs_sbops = {
2721da177e4SLinus Torvalds 	.alloc_inode	= hostfs_alloc_inode,
2731da177e4SLinus Torvalds 	.destroy_inode	= hostfs_destroy_inode,
274e971a6d7SAl Viro 	.evict_inode	= hostfs_evict_inode,
2751da177e4SLinus Torvalds 	.statfs		= hostfs_statfs,
276dd2cc4dfSMiklos Szeredi 	.show_options	= hostfs_show_options,
2771da177e4SLinus Torvalds };
2781da177e4SLinus Torvalds 
2799e443bc3SJames Hogan static int hostfs_readdir(struct file *file, struct dir_context *ctx)
2801da177e4SLinus Torvalds {
2811da177e4SLinus Torvalds 	void *dir;
2821da177e4SLinus Torvalds 	char *name;
2831da177e4SLinus Torvalds 	unsigned long long next, ino;
2841da177e4SLinus Torvalds 	int error, len;
2853ee6bd8eSGeert Uytterhoeven 	unsigned int type;
2861da177e4SLinus Torvalds 
287c5322220SAl Viro 	name = dentry_name(file->f_path.dentry);
288f1adc05eSJeff Dike 	if (name == NULL)
289f1adc05eSJeff Dike 		return -ENOMEM;
2901da177e4SLinus Torvalds 	dir = open_dir(name, &error);
291e9193059SAl Viro 	__putname(name);
292f1adc05eSJeff Dike 	if (dir == NULL)
293f1adc05eSJeff Dike 		return -error;
2948e28bc7eSAl Viro 	next = ctx->pos;
2953ee6bd8eSGeert Uytterhoeven 	while ((name = read_dir(dir, &next, &ino, &len, &type)) != NULL) {
2968e28bc7eSAl Viro 		if (!dir_emit(ctx, name, len, ino, type))
2978e28bc7eSAl Viro 			break;
2988e28bc7eSAl Viro 		ctx->pos = next;
2991da177e4SLinus Torvalds 	}
3001da177e4SLinus Torvalds 	close_dir(dir);
301f1adc05eSJeff Dike 	return 0;
3021da177e4SLinus Torvalds }
3031da177e4SLinus Torvalds 
3044c6dcafcSRichard Weinberger static int hostfs_open(struct inode *ino, struct file *file)
3051da177e4SLinus Torvalds {
3061da177e4SLinus Torvalds 	char *name;
307aeb5d727SAl Viro 	fmode_t mode = 0;
308f8ad850fSAl Viro 	int err;
309aeb5d727SAl Viro 	int r = 0, w = 0, fd;
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds 	mode = file->f_mode & (FMODE_READ | FMODE_WRITE);
3121da177e4SLinus Torvalds 	if ((mode & HOSTFS_I(ino)->mode) == mode)
313f1adc05eSJeff Dike 		return 0;
3141da177e4SLinus Torvalds 
315f8ad850fSAl Viro 	mode |= HOSTFS_I(ino)->mode;
3161da177e4SLinus Torvalds 
317f8ad850fSAl Viro retry:
318*a9d1958bSRichard Weinberger 	r = w = 0;
319*a9d1958bSRichard Weinberger 
320f8ad850fSAl Viro 	if (mode & FMODE_READ)
3211da177e4SLinus Torvalds 		r = 1;
322f8ad850fSAl Viro 	if (mode & FMODE_WRITE)
323112a5da7SRichard Weinberger 		r = w = 1;
3241da177e4SLinus Torvalds 
325c5322220SAl Viro 	name = dentry_name(file->f_path.dentry);
3261da177e4SLinus Torvalds 	if (name == NULL)
327f1adc05eSJeff Dike 		return -ENOMEM;
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds 	fd = open_file(name, r, w, append);
330e9193059SAl Viro 	__putname(name);
331f1adc05eSJeff Dike 	if (fd < 0)
332f1adc05eSJeff Dike 		return fd;
333f8ad850fSAl Viro 
33469886e67SRichard Weinberger 	mutex_lock(&HOSTFS_I(ino)->open_mutex);
335f8ad850fSAl Viro 	/* somebody else had handled it first? */
336f8ad850fSAl Viro 	if ((mode & HOSTFS_I(ino)->mode) == mode) {
33769886e67SRichard Weinberger 		mutex_unlock(&HOSTFS_I(ino)->open_mutex);
338af955658SRichard Weinberger 		close_file(&fd);
339f8ad850fSAl Viro 		return 0;
340f8ad850fSAl Viro 	}
341f8ad850fSAl Viro 	if ((mode | HOSTFS_I(ino)->mode) != mode) {
342f8ad850fSAl Viro 		mode |= HOSTFS_I(ino)->mode;
34369886e67SRichard Weinberger 		mutex_unlock(&HOSTFS_I(ino)->open_mutex);
344f8ad850fSAl Viro 		close_file(&fd);
345f8ad850fSAl Viro 		goto retry;
346f8ad850fSAl Viro 	}
347f8ad850fSAl Viro 	if (HOSTFS_I(ino)->fd == -1) {
348f8ad850fSAl Viro 		HOSTFS_I(ino)->fd = fd;
349f8ad850fSAl Viro 	} else {
350f8ad850fSAl Viro 		err = replace_file(fd, HOSTFS_I(ino)->fd);
351f8ad850fSAl Viro 		close_file(&fd);
352f8ad850fSAl Viro 		if (err < 0) {
35369886e67SRichard Weinberger 			mutex_unlock(&HOSTFS_I(ino)->open_mutex);
354f8ad850fSAl Viro 			return err;
355f8ad850fSAl Viro 		}
356f8ad850fSAl Viro 	}
357f8ad850fSAl Viro 	HOSTFS_I(ino)->mode = mode;
35869886e67SRichard Weinberger 	mutex_unlock(&HOSTFS_I(ino)->open_mutex);
3591da177e4SLinus Torvalds 
360f1adc05eSJeff Dike 	return 0;
3611da177e4SLinus Torvalds }
3621da177e4SLinus Torvalds 
36365984ff9SRichard Weinberger static int hostfs_file_release(struct inode *inode, struct file *file)
36465984ff9SRichard Weinberger {
36565984ff9SRichard Weinberger 	filemap_write_and_wait(inode->i_mapping);
36665984ff9SRichard Weinberger 
36765984ff9SRichard Weinberger 	return 0;
36865984ff9SRichard Weinberger }
36965984ff9SRichard Weinberger 
3709e443bc3SJames Hogan static int hostfs_fsync(struct file *file, loff_t start, loff_t end,
3719e443bc3SJames Hogan 			int datasync)
3721da177e4SLinus Torvalds {
37302c24a82SJosef Bacik 	struct inode *inode = file->f_mapping->host;
37402c24a82SJosef Bacik 	int ret;
37502c24a82SJosef Bacik 
37602c24a82SJosef Bacik 	ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
37702c24a82SJosef Bacik 	if (ret)
37802c24a82SJosef Bacik 		return ret;
37902c24a82SJosef Bacik 
38002c24a82SJosef Bacik 	mutex_lock(&inode->i_mutex);
38102c24a82SJosef Bacik 	ret = fsync_file(HOSTFS_I(inode)->fd, datasync);
38202c24a82SJosef Bacik 	mutex_unlock(&inode->i_mutex);
38302c24a82SJosef Bacik 
38402c24a82SJosef Bacik 	return ret;
3851da177e4SLinus Torvalds }
3861da177e4SLinus Torvalds 
3874b6f5d20SArjan van de Ven static const struct file_operations hostfs_file_fops = {
3881da177e4SLinus Torvalds 	.llseek		= generic_file_llseek,
389aad4f8bbSAl Viro 	.read		= new_sync_read,
3905ffc4ef4SJens Axboe 	.splice_read	= generic_file_splice_read,
391aad4f8bbSAl Viro 	.read_iter	= generic_file_read_iter,
3928174202bSAl Viro 	.write_iter	= generic_file_write_iter,
3938174202bSAl Viro 	.write		= new_sync_write,
3941da177e4SLinus Torvalds 	.mmap		= generic_file_mmap,
3954c6dcafcSRichard Weinberger 	.open		= hostfs_open,
39665984ff9SRichard Weinberger 	.release	= hostfs_file_release,
3971da177e4SLinus Torvalds 	.fsync		= hostfs_fsync,
3981da177e4SLinus Torvalds };
3991da177e4SLinus Torvalds 
4004b6f5d20SArjan van de Ven static const struct file_operations hostfs_dir_fops = {
4011da177e4SLinus Torvalds 	.llseek		= generic_file_llseek,
4028e28bc7eSAl Viro 	.iterate	= hostfs_readdir,
4031da177e4SLinus Torvalds 	.read		= generic_read_dir,
4044c6dcafcSRichard Weinberger 	.open		= hostfs_open,
4054c6dcafcSRichard Weinberger 	.fsync		= hostfs_fsync,
4061da177e4SLinus Torvalds };
4071da177e4SLinus Torvalds 
4089e443bc3SJames Hogan static int hostfs_writepage(struct page *page, struct writeback_control *wbc)
4091da177e4SLinus Torvalds {
4101da177e4SLinus Torvalds 	struct address_space *mapping = page->mapping;
4111da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
4121da177e4SLinus Torvalds 	char *buffer;
4131da177e4SLinus Torvalds 	unsigned long long base;
4141da177e4SLinus Torvalds 	int count = PAGE_CACHE_SIZE;
4151da177e4SLinus Torvalds 	int end_index = inode->i_size >> PAGE_CACHE_SHIFT;
4161da177e4SLinus Torvalds 	int err;
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds 	if (page->index >= end_index)
4191da177e4SLinus Torvalds 		count = inode->i_size & (PAGE_CACHE_SIZE-1);
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 	buffer = kmap(page);
4221da177e4SLinus Torvalds 	base = ((unsigned long long) page->index) << PAGE_CACHE_SHIFT;
4231da177e4SLinus Torvalds 
4241da177e4SLinus Torvalds 	err = write_file(HOSTFS_I(inode)->fd, &base, buffer, count);
4251da177e4SLinus Torvalds 	if (err != count) {
4261da177e4SLinus Torvalds 		ClearPageUptodate(page);
4271da177e4SLinus Torvalds 		goto out;
4281da177e4SLinus Torvalds 	}
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 	if (base > inode->i_size)
4311da177e4SLinus Torvalds 		inode->i_size = base;
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds 	if (PageError(page))
4341da177e4SLinus Torvalds 		ClearPageError(page);
4351da177e4SLinus Torvalds 	err = 0;
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds  out:
4381da177e4SLinus Torvalds 	kunmap(page);
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds 	unlock_page(page);
4411da177e4SLinus Torvalds 	return err;
4421da177e4SLinus Torvalds }
4431da177e4SLinus Torvalds 
4449e443bc3SJames Hogan static int hostfs_readpage(struct file *file, struct page *page)
4451da177e4SLinus Torvalds {
4461da177e4SLinus Torvalds 	char *buffer;
4471da177e4SLinus Torvalds 	long long start;
44841761ddfSRichard Weinberger 	int bytes_read, ret;
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds 	start = (long long) page->index << PAGE_CACHE_SHIFT;
4511da177e4SLinus Torvalds 	buffer = kmap(page);
45241761ddfSRichard Weinberger 	bytes_read = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer,
4531da177e4SLinus Torvalds 			PAGE_CACHE_SIZE);
45441761ddfSRichard Weinberger 	if (bytes_read < 0) {
45541761ddfSRichard Weinberger 		ret = bytes_read;
45684b3db04SJeff Dike 		goto out;
45741761ddfSRichard Weinberger 	}
4581da177e4SLinus Torvalds 
45941761ddfSRichard Weinberger 	memset(buffer + bytes_read, 0, PAGE_CACHE_SIZE - bytes_read);
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds 	flush_dcache_page(page);
4621da177e4SLinus Torvalds 	SetPageUptodate(page);
4631da177e4SLinus Torvalds 	if (PageError(page)) ClearPageError(page);
46441761ddfSRichard Weinberger 	ret = 0;
4651da177e4SLinus Torvalds  out:
4661da177e4SLinus Torvalds 	kunmap(page);
4671da177e4SLinus Torvalds 	unlock_page(page);
46841761ddfSRichard Weinberger 	return ret;
4691da177e4SLinus Torvalds }
4701da177e4SLinus Torvalds 
4719e443bc3SJames Hogan static int hostfs_write_begin(struct file *file, struct address_space *mapping,
472ae361ff4SNick Piggin 			      loff_t pos, unsigned len, unsigned flags,
473ae361ff4SNick Piggin 			      struct page **pagep, void **fsdata)
4741da177e4SLinus Torvalds {
475ae361ff4SNick Piggin 	pgoff_t index = pos >> PAGE_CACHE_SHIFT;
476ae361ff4SNick Piggin 
47754566b2cSNick Piggin 	*pagep = grab_cache_page_write_begin(mapping, index, flags);
478ae361ff4SNick Piggin 	if (!*pagep)
479ae361ff4SNick Piggin 		return -ENOMEM;
480ae361ff4SNick Piggin 	return 0;
481ae361ff4SNick Piggin }
482ae361ff4SNick Piggin 
4839e443bc3SJames Hogan static int hostfs_write_end(struct file *file, struct address_space *mapping,
484ae361ff4SNick Piggin 			    loff_t pos, unsigned len, unsigned copied,
485ae361ff4SNick Piggin 			    struct page *page, void *fsdata)
486ae361ff4SNick Piggin {
487ae361ff4SNick Piggin 	struct inode *inode = mapping->host;
488ae361ff4SNick Piggin 	void *buffer;
489ae361ff4SNick Piggin 	unsigned from = pos & (PAGE_CACHE_SIZE - 1);
4901da177e4SLinus Torvalds 	int err;
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds 	buffer = kmap(page);
493ae361ff4SNick Piggin 	err = write_file(FILE_HOSTFS_I(file)->fd, &pos, buffer + from, copied);
4941da177e4SLinus Torvalds 	kunmap(page);
4951da177e4SLinus Torvalds 
496ae361ff4SNick Piggin 	if (!PageUptodate(page) && err == PAGE_CACHE_SIZE)
497ae361ff4SNick Piggin 		SetPageUptodate(page);
4981da177e4SLinus Torvalds 
49984b3db04SJeff Dike 	/*
50084b3db04SJeff Dike 	 * If err > 0, write_file has added err to pos, so we are comparing
501ae361ff4SNick Piggin 	 * i_size against the last byte written.
502ae361ff4SNick Piggin 	 */
503ae361ff4SNick Piggin 	if (err > 0 && (pos > inode->i_size))
504ae361ff4SNick Piggin 		inode->i_size = pos;
505ae361ff4SNick Piggin 	unlock_page(page);
506ae361ff4SNick Piggin 	page_cache_release(page);
50730f04a4eSPaolo 'Blaisorblade' Giarrusso 
508f1adc05eSJeff Dike 	return err;
5091da177e4SLinus Torvalds }
5101da177e4SLinus Torvalds 
511f5e54d6eSChristoph Hellwig static const struct address_space_operations hostfs_aops = {
5121da177e4SLinus Torvalds 	.writepage 	= hostfs_writepage,
5131da177e4SLinus Torvalds 	.readpage	= hostfs_readpage,
514ffa0aea6SPaolo 'Blaisorblade' Giarrusso 	.set_page_dirty = __set_page_dirty_nobuffers,
515ae361ff4SNick Piggin 	.write_begin	= hostfs_write_begin,
516ae361ff4SNick Piggin 	.write_end	= hostfs_write_end,
5171da177e4SLinus Torvalds };
5181da177e4SLinus Torvalds 
5194754b825SAl Viro static int read_name(struct inode *ino, char *name)
5201da177e4SLinus Torvalds {
5214754b825SAl Viro 	dev_t rdev;
5224754b825SAl Viro 	struct hostfs_stat st;
5234754b825SAl Viro 	int err = stat_file(name, &st, -1);
5244754b825SAl Viro 	if (err)
5254754b825SAl Viro 		return err;
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds 	/* Reencode maj and min with the kernel encoding.*/
5284754b825SAl Viro 	rdev = MKDEV(st.maj, st.min);
5291da177e4SLinus Torvalds 
5304754b825SAl Viro 	switch (st.mode & S_IFMT) {
5314754b825SAl Viro 	case S_IFLNK:
532d0352d3eSAl Viro 		ino->i_op = &hostfs_link_iops;
5334754b825SAl Viro 		break;
5344754b825SAl Viro 	case S_IFDIR:
5354754b825SAl Viro 		ino->i_op = &hostfs_dir_iops;
5364754b825SAl Viro 		ino->i_fop = &hostfs_dir_fops;
5374754b825SAl Viro 		break;
5384754b825SAl Viro 	case S_IFCHR:
5394754b825SAl Viro 	case S_IFBLK:
5404754b825SAl Viro 	case S_IFIFO:
5414754b825SAl Viro 	case S_IFSOCK:
5424754b825SAl Viro 		init_special_inode(ino, st.mode & S_IFMT, rdev);
5434754b825SAl Viro 		ino->i_op = &hostfs_iops;
5444754b825SAl Viro 		break;
5452ad2dca6SRichard Weinberger 	case S_IFREG:
5464754b825SAl Viro 		ino->i_op = &hostfs_iops;
5474754b825SAl Viro 		ino->i_fop = &hostfs_file_fops;
5484754b825SAl Viro 		ino->i_mapping->a_ops = &hostfs_aops;
5492ad2dca6SRichard Weinberger 		break;
5502ad2dca6SRichard Weinberger 	default:
5512ad2dca6SRichard Weinberger 		return -EIO;
5521da177e4SLinus Torvalds 	}
5534754b825SAl Viro 
5544754b825SAl Viro 	ino->i_ino = st.ino;
5554754b825SAl Viro 	ino->i_mode = st.mode;
556bfe86848SMiklos Szeredi 	set_nlink(ino, st.nlink);
55729f82ae5SEric W. Biederman 	i_uid_write(ino, st.uid);
55829f82ae5SEric W. Biederman 	i_gid_write(ino, st.gid);
5594754b825SAl Viro 	ino->i_atime = st.atime;
5604754b825SAl Viro 	ino->i_mtime = st.mtime;
5614754b825SAl Viro 	ino->i_ctime = st.ctime;
5624754b825SAl Viro 	ino->i_size = st.size;
5634754b825SAl Viro 	ino->i_blocks = st.blocks;
5644754b825SAl Viro 	return 0;
5651da177e4SLinus Torvalds }
5661da177e4SLinus Torvalds 
5679e443bc3SJames Hogan static int hostfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
568ebfc3b49SAl Viro 			 bool excl)
5691da177e4SLinus Torvalds {
5701da177e4SLinus Torvalds 	struct inode *inode;
5711da177e4SLinus Torvalds 	char *name;
5721da177e4SLinus Torvalds 	int error, fd;
5731da177e4SLinus Torvalds 
5740a370e5dSDavid Howells 	inode = hostfs_iget(dir->i_sb);
5750a370e5dSDavid Howells 	if (IS_ERR(inode)) {
5760a370e5dSDavid Howells 		error = PTR_ERR(inode);
57784b3db04SJeff Dike 		goto out;
5780a370e5dSDavid Howells 	}
5791da177e4SLinus Torvalds 
5801da177e4SLinus Torvalds 	error = -ENOMEM;
581c5322220SAl Viro 	name = dentry_name(dentry);
5821da177e4SLinus Torvalds 	if (name == NULL)
5831da177e4SLinus Torvalds 		goto out_put;
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds 	fd = file_create(name,
5861da177e4SLinus Torvalds 			 mode & S_IRUSR, mode & S_IWUSR, mode & S_IXUSR,
5871da177e4SLinus Torvalds 			 mode & S_IRGRP, mode & S_IWGRP, mode & S_IXGRP,
5881da177e4SLinus Torvalds 			 mode & S_IROTH, mode & S_IWOTH, mode & S_IXOTH);
5894754b825SAl Viro 	if (fd < 0)
5901da177e4SLinus Torvalds 		error = fd;
5914754b825SAl Viro 	else
5925e2df28cSAl Viro 		error = read_name(inode, name);
5931da177e4SLinus Torvalds 
594e9193059SAl Viro 	__putname(name);
5951da177e4SLinus Torvalds 	if (error)
5961da177e4SLinus Torvalds 		goto out_put;
5971da177e4SLinus Torvalds 
5981da177e4SLinus Torvalds 	HOSTFS_I(inode)->fd = fd;
5991da177e4SLinus Torvalds 	HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE;
6001da177e4SLinus Torvalds 	d_instantiate(dentry, inode);
601f1adc05eSJeff Dike 	return 0;
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds  out_put:
6041da177e4SLinus Torvalds 	iput(inode);
6051da177e4SLinus Torvalds  out:
606f1adc05eSJeff Dike 	return error;
6071da177e4SLinus Torvalds }
6081da177e4SLinus Torvalds 
6099e443bc3SJames Hogan static struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry,
61000cd8dd3SAl Viro 				    unsigned int flags)
6111da177e4SLinus Torvalds {
6121da177e4SLinus Torvalds 	struct inode *inode;
6131da177e4SLinus Torvalds 	char *name;
6141da177e4SLinus Torvalds 	int err;
6151da177e4SLinus Torvalds 
6160a370e5dSDavid Howells 	inode = hostfs_iget(ino->i_sb);
6170a370e5dSDavid Howells 	if (IS_ERR(inode)) {
6180a370e5dSDavid Howells 		err = PTR_ERR(inode);
6191da177e4SLinus Torvalds 		goto out;
6200a370e5dSDavid Howells 	}
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds 	err = -ENOMEM;
623c5322220SAl Viro 	name = dentry_name(dentry);
6241da177e4SLinus Torvalds 	if (name == NULL)
6251da177e4SLinus Torvalds 		goto out_put;
6261da177e4SLinus Torvalds 
6271da177e4SLinus Torvalds 	err = read_name(inode, name);
6285e2df28cSAl Viro 
629e9193059SAl Viro 	__putname(name);
6301da177e4SLinus Torvalds 	if (err == -ENOENT) {
6311da177e4SLinus Torvalds 		iput(inode);
6321da177e4SLinus Torvalds 		inode = NULL;
6331da177e4SLinus Torvalds 	}
6341da177e4SLinus Torvalds 	else if (err)
6351da177e4SLinus Torvalds 		goto out_put;
6361da177e4SLinus Torvalds 
6371da177e4SLinus Torvalds 	d_add(dentry, inode);
638f1adc05eSJeff Dike 	return NULL;
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds  out_put:
6411da177e4SLinus Torvalds 	iput(inode);
6421da177e4SLinus Torvalds  out:
643f1adc05eSJeff Dike 	return ERR_PTR(err);
6441da177e4SLinus Torvalds }
6451da177e4SLinus Torvalds 
6469e443bc3SJames Hogan static int hostfs_link(struct dentry *to, struct inode *ino,
6479e443bc3SJames Hogan 		       struct dentry *from)
6481da177e4SLinus Torvalds {
6491da177e4SLinus Torvalds 	char *from_name, *to_name;
6501da177e4SLinus Torvalds 	int err;
6511da177e4SLinus Torvalds 
652c5322220SAl Viro 	if ((from_name = dentry_name(from)) == NULL)
653f1adc05eSJeff Dike 		return -ENOMEM;
654c5322220SAl Viro 	to_name = dentry_name(to);
6551da177e4SLinus Torvalds 	if (to_name == NULL) {
656e9193059SAl Viro 		__putname(from_name);
657f1adc05eSJeff Dike 		return -ENOMEM;
6581da177e4SLinus Torvalds 	}
6591da177e4SLinus Torvalds 	err = link_file(to_name, from_name);
660e9193059SAl Viro 	__putname(from_name);
661e9193059SAl Viro 	__putname(to_name);
662f1adc05eSJeff Dike 	return err;
6631da177e4SLinus Torvalds }
6641da177e4SLinus Torvalds 
6659e443bc3SJames Hogan static int hostfs_unlink(struct inode *ino, struct dentry *dentry)
6661da177e4SLinus Torvalds {
6671da177e4SLinus Torvalds 	char *file;
6681da177e4SLinus Torvalds 	int err;
6691da177e4SLinus Torvalds 
6701da177e4SLinus Torvalds 	if (append)
671f1adc05eSJeff Dike 		return -EPERM;
6721da177e4SLinus Torvalds 
673f8d7e187SAl Viro 	if ((file = dentry_name(dentry)) == NULL)
674f8d7e187SAl Viro 		return -ENOMEM;
675f8d7e187SAl Viro 
6761da177e4SLinus Torvalds 	err = unlink_file(file);
677e9193059SAl Viro 	__putname(file);
678f1adc05eSJeff Dike 	return err;
6791da177e4SLinus Torvalds }
6801da177e4SLinus Torvalds 
6819e443bc3SJames Hogan static int hostfs_symlink(struct inode *ino, struct dentry *dentry,
6829e443bc3SJames Hogan 			  const char *to)
6831da177e4SLinus Torvalds {
6841da177e4SLinus Torvalds 	char *file;
6851da177e4SLinus Torvalds 	int err;
6861da177e4SLinus Torvalds 
687c5322220SAl Viro 	if ((file = dentry_name(dentry)) == NULL)
688f1adc05eSJeff Dike 		return -ENOMEM;
6891da177e4SLinus Torvalds 	err = make_symlink(file, to);
690e9193059SAl Viro 	__putname(file);
691f1adc05eSJeff Dike 	return err;
6921da177e4SLinus Torvalds }
6931da177e4SLinus Torvalds 
6949e443bc3SJames Hogan static int hostfs_mkdir(struct inode *ino, struct dentry *dentry, umode_t mode)
6951da177e4SLinus Torvalds {
6961da177e4SLinus Torvalds 	char *file;
6971da177e4SLinus Torvalds 	int err;
6981da177e4SLinus Torvalds 
699c5322220SAl Viro 	if ((file = dentry_name(dentry)) == NULL)
700f1adc05eSJeff Dike 		return -ENOMEM;
7011da177e4SLinus Torvalds 	err = do_mkdir(file, mode);
702e9193059SAl Viro 	__putname(file);
703f1adc05eSJeff Dike 	return err;
7041da177e4SLinus Torvalds }
7051da177e4SLinus Torvalds 
7069e443bc3SJames Hogan static int hostfs_rmdir(struct inode *ino, struct dentry *dentry)
7071da177e4SLinus Torvalds {
7081da177e4SLinus Torvalds 	char *file;
7091da177e4SLinus Torvalds 	int err;
7101da177e4SLinus Torvalds 
711c5322220SAl Viro 	if ((file = dentry_name(dentry)) == NULL)
712f1adc05eSJeff Dike 		return -ENOMEM;
7131da177e4SLinus Torvalds 	err = do_rmdir(file);
714e9193059SAl Viro 	__putname(file);
715f1adc05eSJeff Dike 	return err;
7161da177e4SLinus Torvalds }
7171da177e4SLinus Torvalds 
7181a67aafbSAl Viro static int hostfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
7191da177e4SLinus Torvalds {
7201da177e4SLinus Torvalds 	struct inode *inode;
7211da177e4SLinus Torvalds 	char *name;
7220a370e5dSDavid Howells 	int err;
7231da177e4SLinus Torvalds 
7240a370e5dSDavid Howells 	inode = hostfs_iget(dir->i_sb);
7250a370e5dSDavid Howells 	if (IS_ERR(inode)) {
7260a370e5dSDavid Howells 		err = PTR_ERR(inode);
7271da177e4SLinus Torvalds 		goto out;
7280a370e5dSDavid Howells 	}
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds 	err = -ENOMEM;
731c5322220SAl Viro 	name = dentry_name(dentry);
7321da177e4SLinus Torvalds 	if (name == NULL)
7331da177e4SLinus Torvalds 		goto out_put;
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds 	init_special_inode(inode, mode, dev);
73688f6cd0cSJohannes Stezenbach 	err = do_mknod(name, mode, MAJOR(dev), MINOR(dev));
737e9193059SAl Viro 	if (!err)
7381da177e4SLinus Torvalds 		goto out_free;
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds 	err = read_name(inode, name);
741e9193059SAl Viro 	__putname(name);
7425e2df28cSAl Viro 	if (err)
7435e2df28cSAl Viro 		goto out_put;
7441da177e4SLinus Torvalds 	if (err)
7451da177e4SLinus Torvalds 		goto out_put;
7461da177e4SLinus Torvalds 
7471da177e4SLinus Torvalds 	d_instantiate(dentry, inode);
748f1adc05eSJeff Dike 	return 0;
7491da177e4SLinus Torvalds 
7501da177e4SLinus Torvalds  out_free:
751e9193059SAl Viro 	__putname(name);
7521da177e4SLinus Torvalds  out_put:
7531da177e4SLinus Torvalds 	iput(inode);
7541da177e4SLinus Torvalds  out:
755f1adc05eSJeff Dike 	return err;
7561da177e4SLinus Torvalds }
7571da177e4SLinus Torvalds 
7589a423bb6SMiklos Szeredi static int hostfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
7599a423bb6SMiklos Szeredi 			  struct inode *new_dir, struct dentry *new_dentry,
7609a423bb6SMiklos Szeredi 			  unsigned int flags)
7611da177e4SLinus Torvalds {
7629a423bb6SMiklos Szeredi 	char *old_name, *new_name;
7631da177e4SLinus Torvalds 	int err;
7641da177e4SLinus Torvalds 
7659a423bb6SMiklos Szeredi 	if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
7669a423bb6SMiklos Szeredi 		return -EINVAL;
7679a423bb6SMiklos Szeredi 
7689a423bb6SMiklos Szeredi 	old_name = dentry_name(old_dentry);
7699a423bb6SMiklos Szeredi 	if (old_name == NULL)
770f1adc05eSJeff Dike 		return -ENOMEM;
7719a423bb6SMiklos Szeredi 	new_name = dentry_name(new_dentry);
7729a423bb6SMiklos Szeredi 	if (new_name == NULL) {
7739a423bb6SMiklos Szeredi 		__putname(old_name);
774f1adc05eSJeff Dike 		return -ENOMEM;
7751da177e4SLinus Torvalds 	}
7769a423bb6SMiklos Szeredi 	if (!flags)
7779a423bb6SMiklos Szeredi 		err = rename_file(old_name, new_name);
7789a423bb6SMiklos Szeredi 	else
7799a423bb6SMiklos Szeredi 		err = rename2_file(old_name, new_name, flags);
7809a423bb6SMiklos Szeredi 
7819a423bb6SMiklos Szeredi 	__putname(old_name);
7829a423bb6SMiklos Szeredi 	__putname(new_name);
783f1adc05eSJeff Dike 	return err;
7841da177e4SLinus Torvalds }
7851da177e4SLinus Torvalds 
7869e443bc3SJames Hogan static int hostfs_permission(struct inode *ino, int desired)
7871da177e4SLinus Torvalds {
7881da177e4SLinus Torvalds 	char *name;
7891da177e4SLinus Torvalds 	int r = 0, w = 0, x = 0, err;
7901da177e4SLinus Torvalds 
79110556cb2SAl Viro 	if (desired & MAY_NOT_BLOCK)
792b74c79e9SNick Piggin 		return -ECHILD;
793b74c79e9SNick Piggin 
7941da177e4SLinus Torvalds 	if (desired & MAY_READ) r = 1;
7951da177e4SLinus Torvalds 	if (desired & MAY_WRITE) w = 1;
7961da177e4SLinus Torvalds 	if (desired & MAY_EXEC) x = 1;
797c5322220SAl Viro 	name = inode_name(ino);
798f1adc05eSJeff Dike 	if (name == NULL)
799f1adc05eSJeff Dike 		return -ENOMEM;
8001da177e4SLinus Torvalds 
8011da177e4SLinus Torvalds 	if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) ||
8021da177e4SLinus Torvalds 	    S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode))
8031da177e4SLinus Torvalds 		err = 0;
8041da177e4SLinus Torvalds 	else
8051da177e4SLinus Torvalds 		err = access_file(name, r, w, x);
806e9193059SAl Viro 	__putname(name);
8071da177e4SLinus Torvalds 	if (!err)
8082830ba7fSAl Viro 		err = generic_permission(ino, desired);
8091da177e4SLinus Torvalds 	return err;
8101da177e4SLinus Torvalds }
8111da177e4SLinus Torvalds 
8129e443bc3SJames Hogan static int hostfs_setattr(struct dentry *dentry, struct iattr *attr)
8131da177e4SLinus Torvalds {
8141025774cSChristoph Hellwig 	struct inode *inode = dentry->d_inode;
8151da177e4SLinus Torvalds 	struct hostfs_iattr attrs;
8161da177e4SLinus Torvalds 	char *name;
8171da177e4SLinus Torvalds 	int err;
8181da177e4SLinus Torvalds 
8191025774cSChristoph Hellwig 	int fd = HOSTFS_I(inode)->fd;
8205822b7faSAlberto Bertogli 
8211025774cSChristoph Hellwig 	err = inode_change_ok(inode, attr);
8221da177e4SLinus Torvalds 	if (err)
8231da177e4SLinus Torvalds 		return err;
8241da177e4SLinus Torvalds 
8251da177e4SLinus Torvalds 	if (append)
8261da177e4SLinus Torvalds 		attr->ia_valid &= ~ATTR_SIZE;
8271da177e4SLinus Torvalds 
8281da177e4SLinus Torvalds 	attrs.ia_valid = 0;
8291da177e4SLinus Torvalds 	if (attr->ia_valid & ATTR_MODE) {
8301da177e4SLinus Torvalds 		attrs.ia_valid |= HOSTFS_ATTR_MODE;
8311da177e4SLinus Torvalds 		attrs.ia_mode = attr->ia_mode;
8321da177e4SLinus Torvalds 	}
8331da177e4SLinus Torvalds 	if (attr->ia_valid & ATTR_UID) {
8341da177e4SLinus Torvalds 		attrs.ia_valid |= HOSTFS_ATTR_UID;
83529f82ae5SEric W. Biederman 		attrs.ia_uid = from_kuid(&init_user_ns, attr->ia_uid);
8361da177e4SLinus Torvalds 	}
8371da177e4SLinus Torvalds 	if (attr->ia_valid & ATTR_GID) {
8381da177e4SLinus Torvalds 		attrs.ia_valid |= HOSTFS_ATTR_GID;
83929f82ae5SEric W. Biederman 		attrs.ia_gid = from_kgid(&init_user_ns, attr->ia_gid);
8401da177e4SLinus Torvalds 	}
8411da177e4SLinus Torvalds 	if (attr->ia_valid & ATTR_SIZE) {
8421da177e4SLinus Torvalds 		attrs.ia_valid |= HOSTFS_ATTR_SIZE;
8431da177e4SLinus Torvalds 		attrs.ia_size = attr->ia_size;
8441da177e4SLinus Torvalds 	}
8451da177e4SLinus Torvalds 	if (attr->ia_valid & ATTR_ATIME) {
8461da177e4SLinus Torvalds 		attrs.ia_valid |= HOSTFS_ATTR_ATIME;
8471da177e4SLinus Torvalds 		attrs.ia_atime = attr->ia_atime;
8481da177e4SLinus Torvalds 	}
8491da177e4SLinus Torvalds 	if (attr->ia_valid & ATTR_MTIME) {
8501da177e4SLinus Torvalds 		attrs.ia_valid |= HOSTFS_ATTR_MTIME;
8511da177e4SLinus Torvalds 		attrs.ia_mtime = attr->ia_mtime;
8521da177e4SLinus Torvalds 	}
8531da177e4SLinus Torvalds 	if (attr->ia_valid & ATTR_CTIME) {
8541da177e4SLinus Torvalds 		attrs.ia_valid |= HOSTFS_ATTR_CTIME;
8551da177e4SLinus Torvalds 		attrs.ia_ctime = attr->ia_ctime;
8561da177e4SLinus Torvalds 	}
8571da177e4SLinus Torvalds 	if (attr->ia_valid & ATTR_ATIME_SET) {
8581da177e4SLinus Torvalds 		attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET;
8591da177e4SLinus Torvalds 	}
8601da177e4SLinus Torvalds 	if (attr->ia_valid & ATTR_MTIME_SET) {
8611da177e4SLinus Torvalds 		attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET;
8621da177e4SLinus Torvalds 	}
863c5322220SAl Viro 	name = dentry_name(dentry);
864f1adc05eSJeff Dike 	if (name == NULL)
865f1adc05eSJeff Dike 		return -ENOMEM;
8665822b7faSAlberto Bertogli 	err = set_attr(name, &attrs, fd);
867e9193059SAl Viro 	__putname(name);
8681da177e4SLinus Torvalds 	if (err)
869f1adc05eSJeff Dike 		return err;
8701da177e4SLinus Torvalds 
8711025774cSChristoph Hellwig 	if ((attr->ia_valid & ATTR_SIZE) &&
872bc077320SMarco Stornelli 	    attr->ia_size != i_size_read(inode))
8733be2be0aSMarco Stornelli 		truncate_setsize(inode, attr->ia_size);
8741025774cSChristoph Hellwig 
8751025774cSChristoph Hellwig 	setattr_copy(inode, attr);
8761025774cSChristoph Hellwig 	mark_inode_dirty(inode);
8771025774cSChristoph Hellwig 	return 0;
8781da177e4SLinus Torvalds }
8791da177e4SLinus Torvalds 
88092e1d5beSArjan van de Ven static const struct inode_operations hostfs_iops = {
8811da177e4SLinus Torvalds 	.permission	= hostfs_permission,
8821da177e4SLinus Torvalds 	.setattr	= hostfs_setattr,
8831da177e4SLinus Torvalds };
8841da177e4SLinus Torvalds 
88592e1d5beSArjan van de Ven static const struct inode_operations hostfs_dir_iops = {
8861da177e4SLinus Torvalds 	.create		= hostfs_create,
8871da177e4SLinus Torvalds 	.lookup		= hostfs_lookup,
8881da177e4SLinus Torvalds 	.link		= hostfs_link,
8891da177e4SLinus Torvalds 	.unlink		= hostfs_unlink,
8901da177e4SLinus Torvalds 	.symlink	= hostfs_symlink,
8911da177e4SLinus Torvalds 	.mkdir		= hostfs_mkdir,
8921da177e4SLinus Torvalds 	.rmdir		= hostfs_rmdir,
8931da177e4SLinus Torvalds 	.mknod		= hostfs_mknod,
8949a423bb6SMiklos Szeredi 	.rename2	= hostfs_rename2,
8951da177e4SLinus Torvalds 	.permission	= hostfs_permission,
8961da177e4SLinus Torvalds 	.setattr	= hostfs_setattr,
8971da177e4SLinus Torvalds };
8981da177e4SLinus Torvalds 
899d0352d3eSAl Viro static void *hostfs_follow_link(struct dentry *dentry, struct nameidata *nd)
9001da177e4SLinus Torvalds {
901d0352d3eSAl Viro 	char *link = __getname();
902d0352d3eSAl Viro 	if (link) {
903d0352d3eSAl Viro 		char *path = dentry_name(dentry);
904d0352d3eSAl Viro 		int err = -ENOMEM;
905d0352d3eSAl Viro 		if (path) {
9063b6036d1SAl Viro 			err = hostfs_do_readlink(path, link, PATH_MAX);
907d0352d3eSAl Viro 			if (err == PATH_MAX)
9081da177e4SLinus Torvalds 				err = -E2BIG;
909e9193059SAl Viro 			__putname(path);
9101da177e4SLinus Torvalds 		}
911d0352d3eSAl Viro 		if (err < 0) {
912d0352d3eSAl Viro 			__putname(link);
913d0352d3eSAl Viro 			link = ERR_PTR(err);
914d0352d3eSAl Viro 		}
915d0352d3eSAl Viro 	} else {
916d0352d3eSAl Viro 		link = ERR_PTR(-ENOMEM);
9171da177e4SLinus Torvalds 	}
9181da177e4SLinus Torvalds 
919d0352d3eSAl Viro 	nd_set_link(nd, link);
920d0352d3eSAl Viro 	return NULL;
921d0352d3eSAl Viro }
922d0352d3eSAl Viro 
923d0352d3eSAl Viro static void hostfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
924d0352d3eSAl Viro {
925d0352d3eSAl Viro 	char *s = nd_get_link(nd);
926d0352d3eSAl Viro 	if (!IS_ERR(s))
927d0352d3eSAl Viro 		__putname(s);
928d0352d3eSAl Viro }
929d0352d3eSAl Viro 
930d0352d3eSAl Viro static const struct inode_operations hostfs_link_iops = {
931d0352d3eSAl Viro 	.readlink	= generic_readlink,
932d0352d3eSAl Viro 	.follow_link	= hostfs_follow_link,
933d0352d3eSAl Viro 	.put_link	= hostfs_put_link,
9341da177e4SLinus Torvalds };
9351da177e4SLinus Torvalds 
9361da177e4SLinus Torvalds static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent)
9371da177e4SLinus Torvalds {
9381da177e4SLinus Torvalds 	struct inode *root_inode;
93975e8defbSPaolo 'Blaisorblade' Giarrusso 	char *host_root_path, *req_root = d;
9401da177e4SLinus Torvalds 	int err;
9411da177e4SLinus Torvalds 
9421da177e4SLinus Torvalds 	sb->s_blocksize = 1024;
9431da177e4SLinus Torvalds 	sb->s_blocksize_bits = 10;
9441da177e4SLinus Torvalds 	sb->s_magic = HOSTFS_SUPER_MAGIC;
9451da177e4SLinus Torvalds 	sb->s_op = &hostfs_sbops;
946b26d4cd3SAl Viro 	sb->s_d_op = &simple_dentry_operations;
947752fa51eSWolfgang Illmeyer 	sb->s_maxbytes = MAX_LFS_FILESIZE;
9481da177e4SLinus Torvalds 
949a6eb0be6SPaolo 'Blaisorblade' Giarrusso 	/* NULL is printed as <NULL> by sprintf: avoid that. */
95075e8defbSPaolo 'Blaisorblade' Giarrusso 	if (req_root == NULL)
95175e8defbSPaolo 'Blaisorblade' Giarrusso 		req_root = "";
9521da177e4SLinus Torvalds 
9531da177e4SLinus Torvalds 	err = -ENOMEM;
954601d2c38SAl Viro 	sb->s_fs_info = host_root_path =
955601d2c38SAl Viro 		kmalloc(strlen(root_ino) + strlen(req_root) + 2, GFP_KERNEL);
95675e8defbSPaolo 'Blaisorblade' Giarrusso 	if (host_root_path == NULL)
9571da177e4SLinus Torvalds 		goto out;
9581da177e4SLinus Torvalds 
95975e8defbSPaolo 'Blaisorblade' Giarrusso 	sprintf(host_root_path, "%s/%s", root_ino, req_root);
9601da177e4SLinus Torvalds 
96152b209f7SAl Viro 	root_inode = new_inode(sb);
96252b209f7SAl Viro 	if (!root_inode)
963601d2c38SAl Viro 		goto out;
9641da177e4SLinus Torvalds 
9654754b825SAl Viro 	err = read_name(root_inode, host_root_path);
9664754b825SAl Viro 	if (err)
9674754b825SAl Viro 		goto out_put;
96852b209f7SAl Viro 
9694754b825SAl Viro 	if (S_ISLNK(root_inode->i_mode)) {
97052b209f7SAl Viro 		char *name = follow_link(host_root_path);
97152b209f7SAl Viro 		if (IS_ERR(name))
97252b209f7SAl Viro 			err = PTR_ERR(name);
97352b209f7SAl Viro 		else
97452b209f7SAl Viro 			err = read_name(root_inode, name);
97552b209f7SAl Viro 		kfree(name);
9761da177e4SLinus Torvalds 		if (err)
9771da177e4SLinus Torvalds 			goto out_put;
9784754b825SAl Viro 	}
9791da177e4SLinus Torvalds 
9801da177e4SLinus Torvalds 	err = -ENOMEM;
98148fde701SAl Viro 	sb->s_root = d_make_root(root_inode);
9821da177e4SLinus Torvalds 	if (sb->s_root == NULL)
98348fde701SAl Viro 		goto out;
9841da177e4SLinus Torvalds 
985f1adc05eSJeff Dike 	return 0;
9861da177e4SLinus Torvalds 
9871da177e4SLinus Torvalds out_put:
9881da177e4SLinus Torvalds 	iput(root_inode);
9891da177e4SLinus Torvalds out:
990f1adc05eSJeff Dike 	return err;
9911da177e4SLinus Torvalds }
9921da177e4SLinus Torvalds 
9933c26ff6eSAl Viro static struct dentry *hostfs_read_sb(struct file_system_type *type,
9941da177e4SLinus Torvalds 			  int flags, const char *dev_name,
9953c26ff6eSAl Viro 			  void *data)
9961da177e4SLinus Torvalds {
9973c26ff6eSAl Viro 	return mount_nodev(type, flags, data, hostfs_fill_sb_common);
9981da177e4SLinus Torvalds }
9991da177e4SLinus Torvalds 
1000601d2c38SAl Viro static void hostfs_kill_sb(struct super_block *s)
1001601d2c38SAl Viro {
1002601d2c38SAl Viro 	kill_anon_super(s);
1003601d2c38SAl Viro 	kfree(s->s_fs_info);
1004601d2c38SAl Viro }
1005601d2c38SAl Viro 
10061da177e4SLinus Torvalds static struct file_system_type hostfs_type = {
10071da177e4SLinus Torvalds 	.owner 		= THIS_MODULE,
10081da177e4SLinus Torvalds 	.name 		= "hostfs",
10093c26ff6eSAl Viro 	.mount	 	= hostfs_read_sb,
1010601d2c38SAl Viro 	.kill_sb	= hostfs_kill_sb,
10111da177e4SLinus Torvalds 	.fs_flags 	= 0,
10121da177e4SLinus Torvalds };
10133e64fe5bSEric W. Biederman MODULE_ALIAS_FS("hostfs");
10141da177e4SLinus Torvalds 
10151da177e4SLinus Torvalds static int __init init_hostfs(void)
10161da177e4SLinus Torvalds {
1017f1adc05eSJeff Dike 	return register_filesystem(&hostfs_type);
10181da177e4SLinus Torvalds }
10191da177e4SLinus Torvalds 
10201da177e4SLinus Torvalds static void __exit exit_hostfs(void)
10211da177e4SLinus Torvalds {
10221da177e4SLinus Torvalds 	unregister_filesystem(&hostfs_type);
10231da177e4SLinus Torvalds }
10241da177e4SLinus Torvalds 
10251da177e4SLinus Torvalds module_init(init_hostfs)
10261da177e4SLinus Torvalds module_exit(exit_hostfs)
10271da177e4SLinus Torvalds MODULE_LICENSE("GPL");
1028