xref: /linux/fs/hostfs/hostfs_kern.c (revision bca271136f06514253aa28c24c04fc23b88e971e)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.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/stddef.h>
101da177e4SLinus Torvalds #include <linux/fs.h>
111da177e4SLinus Torvalds #include <linux/module.h>
121da177e4SLinus Torvalds #include <linux/init.h>
131da177e4SLinus Torvalds #include <linux/slab.h>
141da177e4SLinus Torvalds #include <linux/pagemap.h>
151da177e4SLinus Torvalds #include <linux/blkdev.h>
161da177e4SLinus Torvalds #include <linux/list.h>
171da177e4SLinus Torvalds #include <linux/statfs.h>
181da177e4SLinus Torvalds #include <linux/kdev_t.h>
191da177e4SLinus Torvalds #include <asm/uaccess.h>
201da177e4SLinus Torvalds #include "hostfs.h"
211da177e4SLinus Torvalds #include "kern_util.h"
221da177e4SLinus Torvalds #include "kern.h"
231da177e4SLinus Torvalds #include "user_util.h"
241da177e4SLinus Torvalds #include "init.h"
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds struct hostfs_inode_info {
271da177e4SLinus Torvalds 	char *host_filename;
281da177e4SLinus Torvalds 	int fd;
291da177e4SLinus Torvalds 	int mode;
301da177e4SLinus Torvalds 	struct inode vfs_inode;
311da177e4SLinus Torvalds };
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds static inline struct hostfs_inode_info *HOSTFS_I(struct inode *inode)
341da177e4SLinus Torvalds {
351da177e4SLinus Torvalds 	return(list_entry(inode, struct hostfs_inode_info, vfs_inode));
361da177e4SLinus Torvalds }
371da177e4SLinus Torvalds 
38680b0da9SJosef Sipek #define FILE_HOSTFS_I(file) HOSTFS_I((file)->f_path.dentry->d_inode)
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds int hostfs_d_delete(struct dentry *dentry)
411da177e4SLinus Torvalds {
421da177e4SLinus Torvalds 	return(1);
431da177e4SLinus Torvalds }
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds struct dentry_operations hostfs_dentry_ops = {
461da177e4SLinus Torvalds 	.d_delete		= hostfs_d_delete,
471da177e4SLinus Torvalds };
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds /* Changed in hostfs_args before the kernel starts running */
501da177e4SLinus Torvalds static char *root_ino = "/";
511da177e4SLinus Torvalds static int append = 0;
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds #define HOSTFS_SUPER_MAGIC 0x00c0ffee
541da177e4SLinus Torvalds 
5592e1d5beSArjan van de Ven static const struct inode_operations hostfs_iops;
5692e1d5beSArjan van de Ven static const struct inode_operations hostfs_dir_iops;
57f5e54d6eSChristoph Hellwig static const struct address_space_operations hostfs_link_aops;
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds #ifndef MODULE
601da177e4SLinus Torvalds static int __init hostfs_args(char *options, int *add)
611da177e4SLinus Torvalds {
621da177e4SLinus Torvalds 	char *ptr;
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds 	ptr = strchr(options, ',');
651da177e4SLinus Torvalds 	if(ptr != NULL)
661da177e4SLinus Torvalds 		*ptr++ = '\0';
671da177e4SLinus Torvalds 	if(*options != '\0')
681da177e4SLinus Torvalds 		root_ino = options;
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds 	options = ptr;
711da177e4SLinus Torvalds 	while(options){
721da177e4SLinus Torvalds 		ptr = strchr(options, ',');
731da177e4SLinus Torvalds 		if(ptr != NULL)
741da177e4SLinus Torvalds 			*ptr++ = '\0';
751da177e4SLinus Torvalds 		if(*options != '\0'){
761da177e4SLinus Torvalds 			if(!strcmp(options, "append"))
771da177e4SLinus Torvalds 				append = 1;
781da177e4SLinus Torvalds 			else printf("hostfs_args - unsupported option - %s\n",
791da177e4SLinus Torvalds 				    options);
801da177e4SLinus Torvalds 		}
811da177e4SLinus Torvalds 		options = ptr;
821da177e4SLinus Torvalds 	}
831da177e4SLinus Torvalds 	return(0);
841da177e4SLinus Torvalds }
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds __uml_setup("hostfs=", hostfs_args,
871da177e4SLinus Torvalds "hostfs=<root dir>,<flags>,...\n"
881da177e4SLinus Torvalds "    This is used to set hostfs parameters.  The root directory argument\n"
891da177e4SLinus Torvalds "    is used to confine all hostfs mounts to within the specified directory\n"
901da177e4SLinus Torvalds "    tree on the host.  If this isn't specified, then a user inside UML can\n"
911da177e4SLinus Torvalds "    mount anything on the host that's accessible to the user that's running\n"
921da177e4SLinus Torvalds "    it.\n"
931da177e4SLinus Torvalds "    The only flag currently supported is 'append', which specifies that all\n"
941da177e4SLinus Torvalds "    files opened by hostfs will be opened in append mode.\n\n"
951da177e4SLinus Torvalds );
961da177e4SLinus Torvalds #endif
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds static char *dentry_name(struct dentry *dentry, int extra)
991da177e4SLinus Torvalds {
1001da177e4SLinus Torvalds 	struct dentry *parent;
1011da177e4SLinus Torvalds 	char *root, *name;
1021da177e4SLinus Torvalds 	int len;
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds 	len = 0;
1051da177e4SLinus Torvalds 	parent = dentry;
1061da177e4SLinus Torvalds 	while(parent->d_parent != parent){
1071da177e4SLinus Torvalds 		len += parent->d_name.len + 1;
1081da177e4SLinus Torvalds 		parent = parent->d_parent;
1091da177e4SLinus Torvalds 	}
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds 	root = HOSTFS_I(parent->d_inode)->host_filename;
1121da177e4SLinus Torvalds 	len += strlen(root);
1131da177e4SLinus Torvalds 	name = kmalloc(len + extra + 1, GFP_KERNEL);
1141da177e4SLinus Torvalds 	if(name == NULL) return(NULL);
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds 	name[len] = '\0';
1171da177e4SLinus Torvalds 	parent = dentry;
1181da177e4SLinus Torvalds 	while(parent->d_parent != parent){
1191da177e4SLinus Torvalds 		len -= parent->d_name.len + 1;
1201da177e4SLinus Torvalds 		name[len] = '/';
1211da177e4SLinus Torvalds 		strncpy(&name[len + 1], parent->d_name.name,
1221da177e4SLinus Torvalds 			parent->d_name.len);
1231da177e4SLinus Torvalds 		parent = parent->d_parent;
1241da177e4SLinus Torvalds 	}
1251da177e4SLinus Torvalds 	strncpy(name, root, strlen(root));
1261da177e4SLinus Torvalds 	return(name);
1271da177e4SLinus Torvalds }
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds static char *inode_name(struct inode *ino, int extra)
1301da177e4SLinus Torvalds {
1311da177e4SLinus Torvalds 	struct dentry *dentry;
1321da177e4SLinus Torvalds 
1331da177e4SLinus Torvalds 	dentry = list_entry(ino->i_dentry.next, struct dentry, d_alias);
1341da177e4SLinus Torvalds 	return(dentry_name(dentry, extra));
1351da177e4SLinus Torvalds }
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds static int read_name(struct inode *ino, char *name)
1381da177e4SLinus Torvalds {
1391da177e4SLinus Torvalds 	/* The non-int inode fields are copied into ints by stat_file and
1401da177e4SLinus Torvalds 	 * then copied into the inode because passing the actual pointers
1411da177e4SLinus Torvalds 	 * in and having them treated as int * breaks on big-endian machines
1421da177e4SLinus Torvalds 	 */
1431da177e4SLinus Torvalds 	int err;
1441da177e4SLinus Torvalds 	int i_mode, i_nlink, i_blksize;
1451da177e4SLinus Torvalds 	unsigned long long i_size;
1461da177e4SLinus Torvalds 	unsigned long long i_ino;
1471da177e4SLinus Torvalds 	unsigned long long i_blocks;
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds 	err = stat_file(name, &i_ino, &i_mode, &i_nlink, &ino->i_uid,
1501da177e4SLinus Torvalds 			&ino->i_gid, &i_size, &ino->i_atime, &ino->i_mtime,
1511da177e4SLinus Torvalds 			&ino->i_ctime, &i_blksize, &i_blocks);
1521da177e4SLinus Torvalds 	if(err)
1531da177e4SLinus Torvalds 		return(err);
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds 	ino->i_ino = i_ino;
1561da177e4SLinus Torvalds 	ino->i_mode = i_mode;
1571da177e4SLinus Torvalds 	ino->i_nlink = i_nlink;
1581da177e4SLinus Torvalds 	ino->i_size = i_size;
1591da177e4SLinus Torvalds 	ino->i_blocks = i_blocks;
1601da177e4SLinus Torvalds 	return(0);
1611da177e4SLinus Torvalds }
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds static char *follow_link(char *link)
1641da177e4SLinus Torvalds {
1651da177e4SLinus Torvalds 	int len, n;
1661da177e4SLinus Torvalds 	char *name, *resolved, *end;
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds 	len = 64;
1691da177e4SLinus Torvalds 	while(1){
1701da177e4SLinus Torvalds 		n = -ENOMEM;
1711da177e4SLinus Torvalds 		name = kmalloc(len, GFP_KERNEL);
1721da177e4SLinus Torvalds 		if(name == NULL)
1731da177e4SLinus Torvalds 			goto out;
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds 		n = do_readlink(link, name, len);
1761da177e4SLinus Torvalds 		if(n < len)
1771da177e4SLinus Torvalds 			break;
1781da177e4SLinus Torvalds 		len *= 2;
1791da177e4SLinus Torvalds 		kfree(name);
1801da177e4SLinus Torvalds 	}
1811da177e4SLinus Torvalds 	if(n < 0)
1821da177e4SLinus Torvalds 		goto out_free;
1831da177e4SLinus Torvalds 
1841da177e4SLinus Torvalds 	if(*name == '/')
1851da177e4SLinus Torvalds 		return(name);
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds 	end = strrchr(link, '/');
1881da177e4SLinus Torvalds 	if(end == NULL)
1891da177e4SLinus Torvalds 		return(name);
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds 	*(end + 1) = '\0';
1921da177e4SLinus Torvalds 	len = strlen(link) + strlen(name) + 1;
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds 	resolved = kmalloc(len, GFP_KERNEL);
1951da177e4SLinus Torvalds 	if(resolved == NULL){
1961da177e4SLinus Torvalds 		n = -ENOMEM;
1971da177e4SLinus Torvalds 		goto out_free;
1981da177e4SLinus Torvalds 	}
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds 	sprintf(resolved, "%s%s", link, name);
2011da177e4SLinus Torvalds 	kfree(name);
2021da177e4SLinus Torvalds 	kfree(link);
2031da177e4SLinus Torvalds 	return(resolved);
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds  out_free:
2061da177e4SLinus Torvalds 	kfree(name);
2071da177e4SLinus Torvalds  out:
2081da177e4SLinus Torvalds 	return(ERR_PTR(n));
2091da177e4SLinus Torvalds }
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds static int read_inode(struct inode *ino)
2121da177e4SLinus Torvalds {
2131da177e4SLinus Torvalds 	char *name;
2141da177e4SLinus Torvalds 	int err = 0;
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds 	/* Unfortunately, we are called from iget() when we don't have a dentry
2171da177e4SLinus Torvalds 	 * allocated yet.
2181da177e4SLinus Torvalds 	 */
2191da177e4SLinus Torvalds 	if(list_empty(&ino->i_dentry))
2201da177e4SLinus Torvalds 		goto out;
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds 	err = -ENOMEM;
2231da177e4SLinus Torvalds 	name = inode_name(ino, 0);
2241da177e4SLinus Torvalds 	if(name == NULL)
2251da177e4SLinus Torvalds 		goto out;
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds 	if(file_type(name, NULL, NULL) == OS_TYPE_SYMLINK){
2281da177e4SLinus Torvalds 		name = follow_link(name);
2291da177e4SLinus Torvalds 		if(IS_ERR(name)){
2301da177e4SLinus Torvalds 			err = PTR_ERR(name);
2311da177e4SLinus Torvalds 			goto out;
2321da177e4SLinus Torvalds 		}
2331da177e4SLinus Torvalds 	}
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds 	err = read_name(ino, name);
2361da177e4SLinus Torvalds 	kfree(name);
2371da177e4SLinus Torvalds  out:
2381da177e4SLinus Torvalds 	return(err);
2391da177e4SLinus Torvalds }
2401da177e4SLinus Torvalds 
241726c3342SDavid Howells int hostfs_statfs(struct dentry *dentry, struct kstatfs *sf)
2421da177e4SLinus Torvalds {
2431da177e4SLinus Torvalds 	/* do_statfs uses struct statfs64 internally, but the linux kernel
2441da177e4SLinus Torvalds 	 * struct statfs still has 32-bit versions for most of these fields,
2451da177e4SLinus Torvalds 	 * so we convert them here
2461da177e4SLinus Torvalds 	 */
2471da177e4SLinus Torvalds 	int err;
2481da177e4SLinus Torvalds 	long long f_blocks;
2491da177e4SLinus Torvalds 	long long f_bfree;
2501da177e4SLinus Torvalds 	long long f_bavail;
2511da177e4SLinus Torvalds 	long long f_files;
2521da177e4SLinus Torvalds 	long long f_ffree;
2531da177e4SLinus Torvalds 
254726c3342SDavid Howells 	err = do_statfs(HOSTFS_I(dentry->d_sb->s_root->d_inode)->host_filename,
2551da177e4SLinus Torvalds 			&sf->f_bsize, &f_blocks, &f_bfree, &f_bavail, &f_files,
2561da177e4SLinus Torvalds 			&f_ffree, &sf->f_fsid, sizeof(sf->f_fsid),
2571da177e4SLinus Torvalds 			&sf->f_namelen, sf->f_spare);
2581da177e4SLinus Torvalds 	if(err) return(err);
2591da177e4SLinus Torvalds 	sf->f_blocks = f_blocks;
2601da177e4SLinus Torvalds 	sf->f_bfree = f_bfree;
2611da177e4SLinus Torvalds 	sf->f_bavail = f_bavail;
2621da177e4SLinus Torvalds 	sf->f_files = f_files;
2631da177e4SLinus Torvalds 	sf->f_ffree = f_ffree;
2641da177e4SLinus Torvalds 	sf->f_type = HOSTFS_SUPER_MAGIC;
2651da177e4SLinus Torvalds 	return(0);
2661da177e4SLinus Torvalds }
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds static struct inode *hostfs_alloc_inode(struct super_block *sb)
2691da177e4SLinus Torvalds {
2701da177e4SLinus Torvalds 	struct hostfs_inode_info *hi;
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds 	hi = kmalloc(sizeof(*hi), GFP_KERNEL);
2731da177e4SLinus Torvalds 	if(hi == NULL)
2741da177e4SLinus Torvalds 		return(NULL);
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds 	*hi = ((struct hostfs_inode_info) { .host_filename	= NULL,
2771da177e4SLinus Torvalds 					    .fd			= -1,
2781da177e4SLinus Torvalds 					    .mode		= 0 });
2791da177e4SLinus Torvalds 	inode_init_once(&hi->vfs_inode);
2801da177e4SLinus Torvalds 	return(&hi->vfs_inode);
2811da177e4SLinus Torvalds }
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds static void hostfs_delete_inode(struct inode *inode)
2841da177e4SLinus Torvalds {
285fef26658SMark Fasheh 	truncate_inode_pages(&inode->i_data, 0);
2861da177e4SLinus Torvalds 	if(HOSTFS_I(inode)->fd != -1) {
2871da177e4SLinus Torvalds 		close_file(&HOSTFS_I(inode)->fd);
2881da177e4SLinus Torvalds 		HOSTFS_I(inode)->fd = -1;
2891da177e4SLinus Torvalds 	}
2901da177e4SLinus Torvalds 	clear_inode(inode);
2911da177e4SLinus Torvalds }
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds static void hostfs_destroy_inode(struct inode *inode)
2941da177e4SLinus Torvalds {
2951da177e4SLinus Torvalds 	kfree(HOSTFS_I(inode)->host_filename);
2961da177e4SLinus Torvalds 
2971da177e4SLinus Torvalds 	/*XXX: This should not happen, probably. The check is here for
2981da177e4SLinus Torvalds 	 * additional safety.*/
2991da177e4SLinus Torvalds 	if(HOSTFS_I(inode)->fd != -1) {
3001da177e4SLinus Torvalds 		close_file(&HOSTFS_I(inode)->fd);
3011da177e4SLinus Torvalds 		printk(KERN_DEBUG "Closing host fd in .destroy_inode\n");
3021da177e4SLinus Torvalds 	}
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds 	kfree(HOSTFS_I(inode));
3051da177e4SLinus Torvalds }
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds static void hostfs_read_inode(struct inode *inode)
3081da177e4SLinus Torvalds {
3091da177e4SLinus Torvalds 	read_inode(inode);
3101da177e4SLinus Torvalds }
3111da177e4SLinus Torvalds 
312ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations hostfs_sbops = {
3131da177e4SLinus Torvalds 	.alloc_inode	= hostfs_alloc_inode,
3141da177e4SLinus Torvalds 	.drop_inode	= generic_delete_inode,
3151da177e4SLinus Torvalds 	.delete_inode   = hostfs_delete_inode,
3161da177e4SLinus Torvalds 	.destroy_inode	= hostfs_destroy_inode,
3171da177e4SLinus Torvalds 	.read_inode	= hostfs_read_inode,
3181da177e4SLinus Torvalds 	.statfs		= hostfs_statfs,
3191da177e4SLinus Torvalds };
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds int hostfs_readdir(struct file *file, void *ent, filldir_t filldir)
3221da177e4SLinus Torvalds {
3231da177e4SLinus Torvalds 	void *dir;
3241da177e4SLinus Torvalds 	char *name;
3251da177e4SLinus Torvalds 	unsigned long long next, ino;
3261da177e4SLinus Torvalds 	int error, len;
3271da177e4SLinus Torvalds 
328680b0da9SJosef Sipek 	name = dentry_name(file->f_path.dentry, 0);
3291da177e4SLinus Torvalds 	if(name == NULL) return(-ENOMEM);
3301da177e4SLinus Torvalds 	dir = open_dir(name, &error);
3311da177e4SLinus Torvalds 	kfree(name);
3321da177e4SLinus Torvalds 	if(dir == NULL) return(-error);
3331da177e4SLinus Torvalds 	next = file->f_pos;
3341da177e4SLinus Torvalds 	while((name = read_dir(dir, &next, &ino, &len)) != NULL){
3351da177e4SLinus Torvalds 		error = (*filldir)(ent, name, len, file->f_pos,
3361da177e4SLinus Torvalds 				   ino, DT_UNKNOWN);
3371da177e4SLinus Torvalds 		if(error) break;
3381da177e4SLinus Torvalds 		file->f_pos = next;
3391da177e4SLinus Torvalds 	}
3401da177e4SLinus Torvalds 	close_dir(dir);
3411da177e4SLinus Torvalds 	return(0);
3421da177e4SLinus Torvalds }
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds int hostfs_file_open(struct inode *ino, struct file *file)
3451da177e4SLinus Torvalds {
3461da177e4SLinus Torvalds 	char *name;
3471da177e4SLinus Torvalds 	int mode = 0, r = 0, w = 0, fd;
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds 	mode = file->f_mode & (FMODE_READ | FMODE_WRITE);
3501da177e4SLinus Torvalds 	if((mode & HOSTFS_I(ino)->mode) == mode)
3511da177e4SLinus Torvalds 		return(0);
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds 	/* The file may already have been opened, but with the wrong access,
3541da177e4SLinus Torvalds 	 * so this resets things and reopens the file with the new access.
3551da177e4SLinus Torvalds 	 */
3561da177e4SLinus Torvalds 	if(HOSTFS_I(ino)->fd != -1){
3571da177e4SLinus Torvalds 		close_file(&HOSTFS_I(ino)->fd);
3581da177e4SLinus Torvalds 		HOSTFS_I(ino)->fd = -1;
3591da177e4SLinus Torvalds 	}
3601da177e4SLinus Torvalds 
3611da177e4SLinus Torvalds 	HOSTFS_I(ino)->mode |= mode;
3621da177e4SLinus Torvalds 	if(HOSTFS_I(ino)->mode & FMODE_READ)
3631da177e4SLinus Torvalds 		r = 1;
3641da177e4SLinus Torvalds 	if(HOSTFS_I(ino)->mode & FMODE_WRITE)
3651da177e4SLinus Torvalds 		w = 1;
3661da177e4SLinus Torvalds 	if(w)
3671da177e4SLinus Torvalds 		r = 1;
3681da177e4SLinus Torvalds 
369680b0da9SJosef Sipek 	name = dentry_name(file->f_path.dentry, 0);
3701da177e4SLinus Torvalds 	if(name == NULL)
3711da177e4SLinus Torvalds 		return(-ENOMEM);
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds 	fd = open_file(name, r, w, append);
3741da177e4SLinus Torvalds 	kfree(name);
3751da177e4SLinus Torvalds 	if(fd < 0) return(fd);
3761da177e4SLinus Torvalds 	FILE_HOSTFS_I(file)->fd = fd;
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds 	return(0);
3791da177e4SLinus Torvalds }
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds int hostfs_fsync(struct file *file, struct dentry *dentry, int datasync)
3821da177e4SLinus Torvalds {
383a2d76bd8SPaolo 'Blaisorblade' Giarrusso 	return fsync_file(HOSTFS_I(dentry->d_inode)->fd, datasync);
3841da177e4SLinus Torvalds }
3851da177e4SLinus Torvalds 
3864b6f5d20SArjan van de Ven static const struct file_operations hostfs_file_fops = {
3871da177e4SLinus Torvalds 	.llseek		= generic_file_llseek,
388543ade1fSBadari Pulavarty 	.read		= do_sync_read,
3891da177e4SLinus Torvalds 	.sendfile	= generic_file_sendfile,
3901da177e4SLinus Torvalds 	.aio_read	= generic_file_aio_read,
3911da177e4SLinus Torvalds 	.aio_write	= generic_file_aio_write,
392543ade1fSBadari Pulavarty 	.write		= do_sync_write,
3931da177e4SLinus Torvalds 	.mmap		= generic_file_mmap,
3941da177e4SLinus Torvalds 	.open		= hostfs_file_open,
3951da177e4SLinus Torvalds 	.release	= NULL,
3961da177e4SLinus Torvalds 	.fsync		= hostfs_fsync,
3971da177e4SLinus Torvalds };
3981da177e4SLinus Torvalds 
3994b6f5d20SArjan van de Ven static const struct file_operations hostfs_dir_fops = {
4001da177e4SLinus Torvalds 	.llseek		= generic_file_llseek,
4011da177e4SLinus Torvalds 	.readdir	= hostfs_readdir,
4021da177e4SLinus Torvalds 	.read		= generic_read_dir,
4031da177e4SLinus Torvalds };
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds int hostfs_writepage(struct page *page, struct writeback_control *wbc)
4061da177e4SLinus Torvalds {
4071da177e4SLinus Torvalds 	struct address_space *mapping = page->mapping;
4081da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
4091da177e4SLinus Torvalds 	char *buffer;
4101da177e4SLinus Torvalds 	unsigned long long base;
4111da177e4SLinus Torvalds 	int count = PAGE_CACHE_SIZE;
4121da177e4SLinus Torvalds 	int end_index = inode->i_size >> PAGE_CACHE_SHIFT;
4131da177e4SLinus Torvalds 	int err;
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds 	if (page->index >= end_index)
4161da177e4SLinus Torvalds 		count = inode->i_size & (PAGE_CACHE_SIZE-1);
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds 	buffer = kmap(page);
4191da177e4SLinus Torvalds 	base = ((unsigned long long) page->index) << PAGE_CACHE_SHIFT;
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds 	err = write_file(HOSTFS_I(inode)->fd, &base, buffer, count);
4221da177e4SLinus Torvalds 	if(err != count){
4231da177e4SLinus Torvalds 		ClearPageUptodate(page);
4241da177e4SLinus Torvalds 		goto out;
4251da177e4SLinus Torvalds 	}
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds 	if (base > inode->i_size)
4281da177e4SLinus Torvalds 		inode->i_size = base;
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 	if (PageError(page))
4311da177e4SLinus Torvalds 		ClearPageError(page);
4321da177e4SLinus Torvalds 	err = 0;
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds  out:
4351da177e4SLinus Torvalds 	kunmap(page);
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds 	unlock_page(page);
4381da177e4SLinus Torvalds 	return err;
4391da177e4SLinus Torvalds }
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds int hostfs_readpage(struct file *file, struct page *page)
4421da177e4SLinus Torvalds {
4431da177e4SLinus Torvalds 	char *buffer;
4441da177e4SLinus Torvalds 	long long start;
4451da177e4SLinus Torvalds 	int err = 0;
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds 	start = (long long) page->index << PAGE_CACHE_SHIFT;
4481da177e4SLinus Torvalds 	buffer = kmap(page);
4491da177e4SLinus Torvalds 	err = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer,
4501da177e4SLinus Torvalds 			PAGE_CACHE_SIZE);
4511da177e4SLinus Torvalds 	if(err < 0) goto out;
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds 	memset(&buffer[err], 0, PAGE_CACHE_SIZE - err);
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds 	flush_dcache_page(page);
4561da177e4SLinus Torvalds 	SetPageUptodate(page);
4571da177e4SLinus Torvalds 	if (PageError(page)) ClearPageError(page);
4581da177e4SLinus Torvalds 	err = 0;
4591da177e4SLinus Torvalds  out:
4601da177e4SLinus Torvalds 	kunmap(page);
4611da177e4SLinus Torvalds 	unlock_page(page);
4621da177e4SLinus Torvalds 	return(err);
4631da177e4SLinus Torvalds }
4641da177e4SLinus Torvalds 
4651da177e4SLinus Torvalds int hostfs_prepare_write(struct file *file, struct page *page,
4661da177e4SLinus Torvalds 			 unsigned int from, unsigned int to)
4671da177e4SLinus Torvalds {
4681da177e4SLinus Torvalds 	char *buffer;
4691da177e4SLinus Torvalds 	long long start, tmp;
4701da177e4SLinus Torvalds 	int err;
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds 	start = (long long) page->index << PAGE_CACHE_SHIFT;
4731da177e4SLinus Torvalds 	buffer = kmap(page);
4741da177e4SLinus Torvalds 	if(from != 0){
4751da177e4SLinus Torvalds 		tmp = start;
4761da177e4SLinus Torvalds 		err = read_file(FILE_HOSTFS_I(file)->fd, &tmp, buffer,
4771da177e4SLinus Torvalds 				from);
4781da177e4SLinus Torvalds 		if(err < 0) goto out;
4791da177e4SLinus Torvalds 	}
4801da177e4SLinus Torvalds 	if(to != PAGE_CACHE_SIZE){
4811da177e4SLinus Torvalds 		start += to;
4821da177e4SLinus Torvalds 		err = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer + to,
4831da177e4SLinus Torvalds 				PAGE_CACHE_SIZE - to);
4841da177e4SLinus Torvalds 		if(err < 0) goto out;
4851da177e4SLinus Torvalds 	}
4861da177e4SLinus Torvalds 	err = 0;
4871da177e4SLinus Torvalds  out:
4881da177e4SLinus Torvalds 	kunmap(page);
4891da177e4SLinus Torvalds 	return(err);
4901da177e4SLinus Torvalds }
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds int hostfs_commit_write(struct file *file, struct page *page, unsigned from,
4931da177e4SLinus Torvalds 		 unsigned to)
4941da177e4SLinus Torvalds {
4951da177e4SLinus Torvalds 	struct address_space *mapping = page->mapping;
4961da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
4971da177e4SLinus Torvalds 	char *buffer;
4981da177e4SLinus Torvalds 	long long start;
4991da177e4SLinus Torvalds 	int err = 0;
5001da177e4SLinus Torvalds 
50130f04a4eSPaolo 'Blaisorblade' Giarrusso 	start = (((long long) page->index) << PAGE_CACHE_SHIFT) + from;
5021da177e4SLinus Torvalds 	buffer = kmap(page);
5031da177e4SLinus Torvalds 	err = write_file(FILE_HOSTFS_I(file)->fd, &start, buffer + from,
5041da177e4SLinus Torvalds 			 to - from);
5051da177e4SLinus Torvalds 	if(err > 0) err = 0;
50630f04a4eSPaolo 'Blaisorblade' Giarrusso 
50730f04a4eSPaolo 'Blaisorblade' Giarrusso 	/* Actually, if !err, write_file has added to-from to start, so, despite
50830f04a4eSPaolo 'Blaisorblade' Giarrusso 	 * the appearance, we are comparing i_size against the _last_ written
50930f04a4eSPaolo 'Blaisorblade' Giarrusso 	 * location, as we should. */
51030f04a4eSPaolo 'Blaisorblade' Giarrusso 
5111da177e4SLinus Torvalds 	if(!err && (start > inode->i_size))
5121da177e4SLinus Torvalds 		inode->i_size = start;
5131da177e4SLinus Torvalds 
5141da177e4SLinus Torvalds 	kunmap(page);
5151da177e4SLinus Torvalds 	return(err);
5161da177e4SLinus Torvalds }
5171da177e4SLinus Torvalds 
518f5e54d6eSChristoph Hellwig static const struct address_space_operations hostfs_aops = {
5191da177e4SLinus Torvalds 	.writepage 	= hostfs_writepage,
5201da177e4SLinus Torvalds 	.readpage	= hostfs_readpage,
521ffa0aea6SPaolo 'Blaisorblade' Giarrusso 	.set_page_dirty = __set_page_dirty_nobuffers,
5221da177e4SLinus Torvalds 	.prepare_write	= hostfs_prepare_write,
5231da177e4SLinus Torvalds 	.commit_write	= hostfs_commit_write
5241da177e4SLinus Torvalds };
5251da177e4SLinus Torvalds 
5261da177e4SLinus Torvalds static int init_inode(struct inode *inode, struct dentry *dentry)
5271da177e4SLinus Torvalds {
5281da177e4SLinus Torvalds 	char *name;
5291da177e4SLinus Torvalds 	int type, err = -ENOMEM;
5301da177e4SLinus Torvalds 	int maj, min;
5311da177e4SLinus Torvalds 	dev_t rdev = 0;
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds 	if(dentry){
5341da177e4SLinus Torvalds 		name = dentry_name(dentry, 0);
5351da177e4SLinus Torvalds 		if(name == NULL)
5361da177e4SLinus Torvalds 			goto out;
5371da177e4SLinus Torvalds 		type = file_type(name, &maj, &min);
5381da177e4SLinus Torvalds 		/*Reencode maj and min with the kernel encoding.*/
5391da177e4SLinus Torvalds 		rdev = MKDEV(maj, min);
5401da177e4SLinus Torvalds 		kfree(name);
5411da177e4SLinus Torvalds 	}
5421da177e4SLinus Torvalds 	else type = OS_TYPE_DIR;
5431da177e4SLinus Torvalds 
5441da177e4SLinus Torvalds 	err = 0;
5451da177e4SLinus Torvalds 	if(type == OS_TYPE_SYMLINK)
5461da177e4SLinus Torvalds 		inode->i_op = &page_symlink_inode_operations;
5471da177e4SLinus Torvalds 	else if(type == OS_TYPE_DIR)
5481da177e4SLinus Torvalds 		inode->i_op = &hostfs_dir_iops;
5491da177e4SLinus Torvalds 	else inode->i_op = &hostfs_iops;
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds 	if(type == OS_TYPE_DIR) inode->i_fop = &hostfs_dir_fops;
5521da177e4SLinus Torvalds 	else inode->i_fop = &hostfs_file_fops;
5531da177e4SLinus Torvalds 
5541da177e4SLinus Torvalds 	if(type == OS_TYPE_SYMLINK)
5551da177e4SLinus Torvalds 		inode->i_mapping->a_ops = &hostfs_link_aops;
5561da177e4SLinus Torvalds 	else inode->i_mapping->a_ops = &hostfs_aops;
5571da177e4SLinus Torvalds 
5581da177e4SLinus Torvalds 	switch (type) {
5591da177e4SLinus Torvalds 	case OS_TYPE_CHARDEV:
5601da177e4SLinus Torvalds 		init_special_inode(inode, S_IFCHR, rdev);
5611da177e4SLinus Torvalds 		break;
5621da177e4SLinus Torvalds 	case OS_TYPE_BLOCKDEV:
5631da177e4SLinus Torvalds 		init_special_inode(inode, S_IFBLK, rdev);
5641da177e4SLinus Torvalds 		break;
5651da177e4SLinus Torvalds 	case OS_TYPE_FIFO:
5661da177e4SLinus Torvalds 		init_special_inode(inode, S_IFIFO, 0);
5671da177e4SLinus Torvalds 		break;
5681da177e4SLinus Torvalds 	case OS_TYPE_SOCK:
5691da177e4SLinus Torvalds 		init_special_inode(inode, S_IFSOCK, 0);
5701da177e4SLinus Torvalds 		break;
5711da177e4SLinus Torvalds 	}
5721da177e4SLinus Torvalds  out:
5731da177e4SLinus Torvalds 	return(err);
5741da177e4SLinus Torvalds }
5751da177e4SLinus Torvalds 
5761da177e4SLinus Torvalds int hostfs_create(struct inode *dir, struct dentry *dentry, int mode,
5771da177e4SLinus Torvalds                  struct nameidata *nd)
5781da177e4SLinus Torvalds {
5791da177e4SLinus Torvalds 	struct inode *inode;
5801da177e4SLinus Torvalds 	char *name;
5811da177e4SLinus Torvalds 	int error, fd;
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds 	error = -ENOMEM;
5841da177e4SLinus Torvalds 	inode = iget(dir->i_sb, 0);
5851da177e4SLinus Torvalds 	if(inode == NULL) goto out;
5861da177e4SLinus Torvalds 
5871da177e4SLinus Torvalds 	error = init_inode(inode, dentry);
5881da177e4SLinus Torvalds 	if(error)
5891da177e4SLinus Torvalds 		goto out_put;
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds 	error = -ENOMEM;
5921da177e4SLinus Torvalds 	name = dentry_name(dentry, 0);
5931da177e4SLinus Torvalds 	if(name == NULL)
5941da177e4SLinus Torvalds 		goto out_put;
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds 	fd = file_create(name,
5971da177e4SLinus Torvalds 			 mode & S_IRUSR, mode & S_IWUSR, mode & S_IXUSR,
5981da177e4SLinus Torvalds 			 mode & S_IRGRP, mode & S_IWGRP, mode & S_IXGRP,
5991da177e4SLinus Torvalds 			 mode & S_IROTH, mode & S_IWOTH, mode & S_IXOTH);
6001da177e4SLinus Torvalds 	if(fd < 0)
6011da177e4SLinus Torvalds 		error = fd;
6021da177e4SLinus Torvalds 	else error = read_name(inode, name);
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds 	kfree(name);
6051da177e4SLinus Torvalds 	if(error)
6061da177e4SLinus Torvalds 		goto out_put;
6071da177e4SLinus Torvalds 
6081da177e4SLinus Torvalds 	HOSTFS_I(inode)->fd = fd;
6091da177e4SLinus Torvalds 	HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE;
6101da177e4SLinus Torvalds 	d_instantiate(dentry, inode);
6111da177e4SLinus Torvalds 	return(0);
6121da177e4SLinus Torvalds 
6131da177e4SLinus Torvalds  out_put:
6141da177e4SLinus Torvalds 	iput(inode);
6151da177e4SLinus Torvalds  out:
6161da177e4SLinus Torvalds 	return(error);
6171da177e4SLinus Torvalds }
6181da177e4SLinus Torvalds 
6191da177e4SLinus Torvalds struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry,
6201da177e4SLinus Torvalds                             struct nameidata *nd)
6211da177e4SLinus Torvalds {
6221da177e4SLinus Torvalds 	struct inode *inode;
6231da177e4SLinus Torvalds 	char *name;
6241da177e4SLinus Torvalds 	int err;
6251da177e4SLinus Torvalds 
6261da177e4SLinus Torvalds 	err = -ENOMEM;
6271da177e4SLinus Torvalds 	inode = iget(ino->i_sb, 0);
6281da177e4SLinus Torvalds 	if(inode == NULL)
6291da177e4SLinus Torvalds 		goto out;
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds 	err = init_inode(inode, dentry);
6321da177e4SLinus Torvalds 	if(err)
6331da177e4SLinus Torvalds 		goto out_put;
6341da177e4SLinus Torvalds 
6351da177e4SLinus Torvalds 	err = -ENOMEM;
6361da177e4SLinus Torvalds 	name = dentry_name(dentry, 0);
6371da177e4SLinus Torvalds 	if(name == NULL)
6381da177e4SLinus Torvalds 		goto out_put;
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds 	err = read_name(inode, name);
6411da177e4SLinus Torvalds 	kfree(name);
6421da177e4SLinus Torvalds 	if(err == -ENOENT){
6431da177e4SLinus Torvalds 		iput(inode);
6441da177e4SLinus Torvalds 		inode = NULL;
6451da177e4SLinus Torvalds 	}
6461da177e4SLinus Torvalds 	else if(err)
6471da177e4SLinus Torvalds 		goto out_put;
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds 	d_add(dentry, inode);
6501da177e4SLinus Torvalds 	dentry->d_op = &hostfs_dentry_ops;
6511da177e4SLinus Torvalds 	return(NULL);
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds  out_put:
6541da177e4SLinus Torvalds 	iput(inode);
6551da177e4SLinus Torvalds  out:
6561da177e4SLinus Torvalds 	return(ERR_PTR(err));
6571da177e4SLinus Torvalds }
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds static char *inode_dentry_name(struct inode *ino, struct dentry *dentry)
6601da177e4SLinus Torvalds {
6611da177e4SLinus Torvalds         char *file;
6621da177e4SLinus Torvalds 	int len;
6631da177e4SLinus Torvalds 
6641da177e4SLinus Torvalds 	file = inode_name(ino, dentry->d_name.len + 1);
6651da177e4SLinus Torvalds 	if(file == NULL) return(NULL);
6661da177e4SLinus Torvalds         strcat(file, "/");
6671da177e4SLinus Torvalds 	len = strlen(file);
6681da177e4SLinus Torvalds         strncat(file, dentry->d_name.name, dentry->d_name.len);
6691da177e4SLinus Torvalds 	file[len + dentry->d_name.len] = '\0';
6701da177e4SLinus Torvalds         return(file);
6711da177e4SLinus Torvalds }
6721da177e4SLinus Torvalds 
6731da177e4SLinus Torvalds int hostfs_link(struct dentry *to, struct inode *ino, struct dentry *from)
6741da177e4SLinus Torvalds {
6751da177e4SLinus Torvalds         char *from_name, *to_name;
6761da177e4SLinus Torvalds         int err;
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds         if((from_name = inode_dentry_name(ino, from)) == NULL)
6791da177e4SLinus Torvalds                 return(-ENOMEM);
6801da177e4SLinus Torvalds         to_name = dentry_name(to, 0);
6811da177e4SLinus Torvalds 	if(to_name == NULL){
6821da177e4SLinus Torvalds 		kfree(from_name);
6831da177e4SLinus Torvalds 		return(-ENOMEM);
6841da177e4SLinus Torvalds 	}
6851da177e4SLinus Torvalds         err = link_file(to_name, from_name);
6861da177e4SLinus Torvalds         kfree(from_name);
6871da177e4SLinus Torvalds         kfree(to_name);
6881da177e4SLinus Torvalds         return(err);
6891da177e4SLinus Torvalds }
6901da177e4SLinus Torvalds 
6911da177e4SLinus Torvalds int hostfs_unlink(struct inode *ino, struct dentry *dentry)
6921da177e4SLinus Torvalds {
6931da177e4SLinus Torvalds 	char *file;
6941da177e4SLinus Torvalds 	int err;
6951da177e4SLinus Torvalds 
6961da177e4SLinus Torvalds 	if((file = inode_dentry_name(ino, dentry)) == NULL) return(-ENOMEM);
6971da177e4SLinus Torvalds 	if(append)
6981da177e4SLinus Torvalds 		return(-EPERM);
6991da177e4SLinus Torvalds 
7001da177e4SLinus Torvalds 	err = unlink_file(file);
7011da177e4SLinus Torvalds 	kfree(file);
7021da177e4SLinus Torvalds 	return(err);
7031da177e4SLinus Torvalds }
7041da177e4SLinus Torvalds 
7051da177e4SLinus Torvalds int hostfs_symlink(struct inode *ino, struct dentry *dentry, const char *to)
7061da177e4SLinus Torvalds {
7071da177e4SLinus Torvalds 	char *file;
7081da177e4SLinus Torvalds 	int err;
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds 	if((file = inode_dentry_name(ino, dentry)) == NULL) return(-ENOMEM);
7111da177e4SLinus Torvalds 	err = make_symlink(file, to);
7121da177e4SLinus Torvalds 	kfree(file);
7131da177e4SLinus Torvalds 	return(err);
7141da177e4SLinus Torvalds }
7151da177e4SLinus Torvalds 
7161da177e4SLinus Torvalds int hostfs_mkdir(struct inode *ino, struct dentry *dentry, int mode)
7171da177e4SLinus Torvalds {
7181da177e4SLinus Torvalds 	char *file;
7191da177e4SLinus Torvalds 	int err;
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds 	if((file = inode_dentry_name(ino, dentry)) == NULL) return(-ENOMEM);
7221da177e4SLinus Torvalds 	err = do_mkdir(file, mode);
7231da177e4SLinus Torvalds 	kfree(file);
7241da177e4SLinus Torvalds 	return(err);
7251da177e4SLinus Torvalds }
7261da177e4SLinus Torvalds 
7271da177e4SLinus Torvalds int hostfs_rmdir(struct inode *ino, struct dentry *dentry)
7281da177e4SLinus Torvalds {
7291da177e4SLinus Torvalds 	char *file;
7301da177e4SLinus Torvalds 	int err;
7311da177e4SLinus Torvalds 
7321da177e4SLinus Torvalds 	if((file = inode_dentry_name(ino, dentry)) == NULL) return(-ENOMEM);
7331da177e4SLinus Torvalds 	err = do_rmdir(file);
7341da177e4SLinus Torvalds 	kfree(file);
7351da177e4SLinus Torvalds 	return(err);
7361da177e4SLinus Torvalds }
7371da177e4SLinus Torvalds 
7381da177e4SLinus Torvalds int hostfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
7391da177e4SLinus Torvalds {
7401da177e4SLinus Torvalds 	struct inode *inode;
7411da177e4SLinus Torvalds 	char *name;
7421da177e4SLinus Torvalds 	int err = -ENOMEM;
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds 	inode = iget(dir->i_sb, 0);
7451da177e4SLinus Torvalds 	if(inode == NULL)
7461da177e4SLinus Torvalds 		goto out;
7471da177e4SLinus Torvalds 
7481da177e4SLinus Torvalds 	err = init_inode(inode, dentry);
7491da177e4SLinus Torvalds 	if(err)
7501da177e4SLinus Torvalds 		goto out_put;
7511da177e4SLinus Torvalds 
7521da177e4SLinus Torvalds 	err = -ENOMEM;
7531da177e4SLinus Torvalds 	name = dentry_name(dentry, 0);
7541da177e4SLinus Torvalds 	if(name == NULL)
7551da177e4SLinus Torvalds 		goto out_put;
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds 	init_special_inode(inode, mode, dev);
75888f6cd0cSJohannes Stezenbach 	err = do_mknod(name, mode, MAJOR(dev), MINOR(dev));
7591da177e4SLinus Torvalds 	if(err)
7601da177e4SLinus Torvalds 		goto out_free;
7611da177e4SLinus Torvalds 
7621da177e4SLinus Torvalds 	err = read_name(inode, name);
7631da177e4SLinus Torvalds 	kfree(name);
7641da177e4SLinus Torvalds 	if(err)
7651da177e4SLinus Torvalds 		goto out_put;
7661da177e4SLinus Torvalds 
7671da177e4SLinus Torvalds 	d_instantiate(dentry, inode);
7681da177e4SLinus Torvalds 	return(0);
7691da177e4SLinus Torvalds 
7701da177e4SLinus Torvalds  out_free:
7711da177e4SLinus Torvalds 	kfree(name);
7721da177e4SLinus Torvalds  out_put:
7731da177e4SLinus Torvalds 	iput(inode);
7741da177e4SLinus Torvalds  out:
7751da177e4SLinus Torvalds 	return(err);
7761da177e4SLinus Torvalds }
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds int hostfs_rename(struct inode *from_ino, struct dentry *from,
7791da177e4SLinus Torvalds 		  struct inode *to_ino, struct dentry *to)
7801da177e4SLinus Torvalds {
7811da177e4SLinus Torvalds 	char *from_name, *to_name;
7821da177e4SLinus Torvalds 	int err;
7831da177e4SLinus Torvalds 
7841da177e4SLinus Torvalds 	if((from_name = inode_dentry_name(from_ino, from)) == NULL)
7851da177e4SLinus Torvalds 		return(-ENOMEM);
7861da177e4SLinus Torvalds 	if((to_name = inode_dentry_name(to_ino, to)) == NULL){
7871da177e4SLinus Torvalds 		kfree(from_name);
7881da177e4SLinus Torvalds 		return(-ENOMEM);
7891da177e4SLinus Torvalds 	}
7901da177e4SLinus Torvalds 	err = rename_file(from_name, to_name);
7911da177e4SLinus Torvalds 	kfree(from_name);
7921da177e4SLinus Torvalds 	kfree(to_name);
7931da177e4SLinus Torvalds 	return(err);
7941da177e4SLinus Torvalds }
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds int hostfs_permission(struct inode *ino, int desired, struct nameidata *nd)
7971da177e4SLinus Torvalds {
7981da177e4SLinus Torvalds 	char *name;
7991da177e4SLinus Torvalds 	int r = 0, w = 0, x = 0, err;
8001da177e4SLinus Torvalds 
8011da177e4SLinus Torvalds 	if (desired & MAY_READ) r = 1;
8021da177e4SLinus Torvalds 	if (desired & MAY_WRITE) w = 1;
8031da177e4SLinus Torvalds 	if (desired & MAY_EXEC) x = 1;
8041da177e4SLinus Torvalds 	name = inode_name(ino, 0);
8051da177e4SLinus Torvalds 	if (name == NULL) return(-ENOMEM);
8061da177e4SLinus Torvalds 
8071da177e4SLinus Torvalds 	if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) ||
8081da177e4SLinus Torvalds 			S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode))
8091da177e4SLinus Torvalds 		err = 0;
8101da177e4SLinus Torvalds 	else
8111da177e4SLinus Torvalds 		err = access_file(name, r, w, x);
8121da177e4SLinus Torvalds 	kfree(name);
8131da177e4SLinus Torvalds 	if(!err)
8141da177e4SLinus Torvalds 		err = generic_permission(ino, desired, NULL);
8151da177e4SLinus Torvalds 	return err;
8161da177e4SLinus Torvalds }
8171da177e4SLinus Torvalds 
8181da177e4SLinus Torvalds int hostfs_setattr(struct dentry *dentry, struct iattr *attr)
8191da177e4SLinus Torvalds {
8201da177e4SLinus Torvalds 	struct hostfs_iattr attrs;
8211da177e4SLinus Torvalds 	char *name;
8221da177e4SLinus Torvalds 	int err;
8231da177e4SLinus Torvalds 
8241da177e4SLinus Torvalds 	err = inode_change_ok(dentry->d_inode, attr);
8251da177e4SLinus Torvalds 	if (err)
8261da177e4SLinus Torvalds 		return err;
8271da177e4SLinus Torvalds 
8281da177e4SLinus Torvalds 	if(append)
8291da177e4SLinus Torvalds 		attr->ia_valid &= ~ATTR_SIZE;
8301da177e4SLinus Torvalds 
8311da177e4SLinus Torvalds 	attrs.ia_valid = 0;
8321da177e4SLinus Torvalds 	if(attr->ia_valid & ATTR_MODE){
8331da177e4SLinus Torvalds 		attrs.ia_valid |= HOSTFS_ATTR_MODE;
8341da177e4SLinus Torvalds 		attrs.ia_mode = attr->ia_mode;
8351da177e4SLinus Torvalds 	}
8361da177e4SLinus Torvalds 	if(attr->ia_valid & ATTR_UID){
8371da177e4SLinus Torvalds 		attrs.ia_valid |= HOSTFS_ATTR_UID;
8381da177e4SLinus Torvalds 		attrs.ia_uid = attr->ia_uid;
8391da177e4SLinus Torvalds 	}
8401da177e4SLinus Torvalds 	if(attr->ia_valid & ATTR_GID){
8411da177e4SLinus Torvalds 		attrs.ia_valid |= HOSTFS_ATTR_GID;
8421da177e4SLinus Torvalds 		attrs.ia_gid = attr->ia_gid;
8431da177e4SLinus Torvalds 	}
8441da177e4SLinus Torvalds 	if(attr->ia_valid & ATTR_SIZE){
8451da177e4SLinus Torvalds 		attrs.ia_valid |= HOSTFS_ATTR_SIZE;
8461da177e4SLinus Torvalds 		attrs.ia_size = attr->ia_size;
8471da177e4SLinus Torvalds 	}
8481da177e4SLinus Torvalds 	if(attr->ia_valid & ATTR_ATIME){
8491da177e4SLinus Torvalds 		attrs.ia_valid |= HOSTFS_ATTR_ATIME;
8501da177e4SLinus Torvalds 		attrs.ia_atime = attr->ia_atime;
8511da177e4SLinus Torvalds 	}
8521da177e4SLinus Torvalds 	if(attr->ia_valid & ATTR_MTIME){
8531da177e4SLinus Torvalds 		attrs.ia_valid |= HOSTFS_ATTR_MTIME;
8541da177e4SLinus Torvalds 		attrs.ia_mtime = attr->ia_mtime;
8551da177e4SLinus Torvalds 	}
8561da177e4SLinus Torvalds 	if(attr->ia_valid & ATTR_CTIME){
8571da177e4SLinus Torvalds 		attrs.ia_valid |= HOSTFS_ATTR_CTIME;
8581da177e4SLinus Torvalds 		attrs.ia_ctime = attr->ia_ctime;
8591da177e4SLinus Torvalds 	}
8601da177e4SLinus Torvalds 	if(attr->ia_valid & ATTR_ATIME_SET){
8611da177e4SLinus Torvalds 		attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET;
8621da177e4SLinus Torvalds 	}
8631da177e4SLinus Torvalds 	if(attr->ia_valid & ATTR_MTIME_SET){
8641da177e4SLinus Torvalds 		attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET;
8651da177e4SLinus Torvalds 	}
8661da177e4SLinus Torvalds 	name = dentry_name(dentry, 0);
8671da177e4SLinus Torvalds 	if(name == NULL) return(-ENOMEM);
8681da177e4SLinus Torvalds 	err = set_attr(name, &attrs);
8691da177e4SLinus Torvalds 	kfree(name);
8701da177e4SLinus Torvalds 	if(err)
8711da177e4SLinus Torvalds 		return(err);
8721da177e4SLinus Torvalds 
8731da177e4SLinus Torvalds 	return(inode_setattr(dentry->d_inode, attr));
8741da177e4SLinus Torvalds }
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds int hostfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
8771da177e4SLinus Torvalds 	   struct kstat *stat)
8781da177e4SLinus Torvalds {
8791da177e4SLinus Torvalds 	generic_fillattr(dentry->d_inode, stat);
8801da177e4SLinus Torvalds 	return(0);
8811da177e4SLinus Torvalds }
8821da177e4SLinus Torvalds 
88392e1d5beSArjan van de Ven static const struct inode_operations hostfs_iops = {
8841da177e4SLinus Torvalds 	.create		= hostfs_create,
8851da177e4SLinus Torvalds 	.link		= hostfs_link,
8861da177e4SLinus Torvalds 	.unlink		= hostfs_unlink,
8871da177e4SLinus Torvalds 	.symlink	= hostfs_symlink,
8881da177e4SLinus Torvalds 	.mkdir		= hostfs_mkdir,
8891da177e4SLinus Torvalds 	.rmdir		= hostfs_rmdir,
8901da177e4SLinus Torvalds 	.mknod		= hostfs_mknod,
8911da177e4SLinus Torvalds 	.rename		= hostfs_rename,
8921da177e4SLinus Torvalds 	.permission	= hostfs_permission,
8931da177e4SLinus Torvalds 	.setattr	= hostfs_setattr,
8941da177e4SLinus Torvalds 	.getattr	= hostfs_getattr,
8951da177e4SLinus Torvalds };
8961da177e4SLinus Torvalds 
89792e1d5beSArjan van de Ven static const struct inode_operations hostfs_dir_iops = {
8981da177e4SLinus Torvalds 	.create		= hostfs_create,
8991da177e4SLinus Torvalds 	.lookup		= hostfs_lookup,
9001da177e4SLinus Torvalds 	.link		= hostfs_link,
9011da177e4SLinus Torvalds 	.unlink		= hostfs_unlink,
9021da177e4SLinus Torvalds 	.symlink	= hostfs_symlink,
9031da177e4SLinus Torvalds 	.mkdir		= hostfs_mkdir,
9041da177e4SLinus Torvalds 	.rmdir		= hostfs_rmdir,
9051da177e4SLinus Torvalds 	.mknod		= hostfs_mknod,
9061da177e4SLinus Torvalds 	.rename		= hostfs_rename,
9071da177e4SLinus Torvalds 	.permission	= hostfs_permission,
9081da177e4SLinus Torvalds 	.setattr	= hostfs_setattr,
9091da177e4SLinus Torvalds 	.getattr	= hostfs_getattr,
9101da177e4SLinus Torvalds };
9111da177e4SLinus Torvalds 
9121da177e4SLinus Torvalds int hostfs_link_readpage(struct file *file, struct page *page)
9131da177e4SLinus Torvalds {
9141da177e4SLinus Torvalds 	char *buffer, *name;
9151da177e4SLinus Torvalds 	int err;
9161da177e4SLinus Torvalds 
9171da177e4SLinus Torvalds 	buffer = kmap(page);
9181da177e4SLinus Torvalds 	name = inode_name(page->mapping->host, 0);
9191da177e4SLinus Torvalds 	if(name == NULL) return(-ENOMEM);
9201da177e4SLinus Torvalds 	err = do_readlink(name, buffer, PAGE_CACHE_SIZE);
9211da177e4SLinus Torvalds 	kfree(name);
9221da177e4SLinus Torvalds 	if(err == PAGE_CACHE_SIZE)
9231da177e4SLinus Torvalds 		err = -E2BIG;
9241da177e4SLinus Torvalds 	else if(err > 0){
9251da177e4SLinus Torvalds 		flush_dcache_page(page);
9261da177e4SLinus Torvalds 		SetPageUptodate(page);
9271da177e4SLinus Torvalds 		if (PageError(page)) ClearPageError(page);
9281da177e4SLinus Torvalds 		err = 0;
9291da177e4SLinus Torvalds 	}
9301da177e4SLinus Torvalds 	kunmap(page);
9311da177e4SLinus Torvalds 	unlock_page(page);
9321da177e4SLinus Torvalds 	return(err);
9331da177e4SLinus Torvalds }
9341da177e4SLinus Torvalds 
935f5e54d6eSChristoph Hellwig static const struct address_space_operations hostfs_link_aops = {
9361da177e4SLinus Torvalds 	.readpage	= hostfs_link_readpage,
9371da177e4SLinus Torvalds };
9381da177e4SLinus Torvalds 
9391da177e4SLinus Torvalds static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent)
9401da177e4SLinus Torvalds {
9411da177e4SLinus Torvalds 	struct inode *root_inode;
9421da177e4SLinus Torvalds 	char *name, *data = d;
9431da177e4SLinus Torvalds 	int err;
9441da177e4SLinus Torvalds 
9451da177e4SLinus Torvalds 	sb->s_blocksize = 1024;
9461da177e4SLinus Torvalds 	sb->s_blocksize_bits = 10;
9471da177e4SLinus Torvalds 	sb->s_magic = HOSTFS_SUPER_MAGIC;
9481da177e4SLinus Torvalds 	sb->s_op = &hostfs_sbops;
9491da177e4SLinus Torvalds 
9501da177e4SLinus Torvalds 	if((data == NULL) || (*data == '\0'))
9511da177e4SLinus Torvalds 		data = root_ino;
9521da177e4SLinus Torvalds 
9531da177e4SLinus Torvalds 	err = -ENOMEM;
9541da177e4SLinus Torvalds 	name = kmalloc(strlen(data) + 1, GFP_KERNEL);
9551da177e4SLinus Torvalds 	if(name == NULL)
9561da177e4SLinus Torvalds 		goto out;
9571da177e4SLinus Torvalds 
9581da177e4SLinus Torvalds 	strcpy(name, data);
9591da177e4SLinus Torvalds 
9601da177e4SLinus Torvalds 	root_inode = iget(sb, 0);
9611da177e4SLinus Torvalds 	if(root_inode == NULL)
9621da177e4SLinus Torvalds 		goto out_free;
9631da177e4SLinus Torvalds 
9641da177e4SLinus Torvalds 	err = init_inode(root_inode, NULL);
9651da177e4SLinus Torvalds 	if(err)
9661da177e4SLinus Torvalds 		goto out_put;
9671da177e4SLinus Torvalds 
9681da177e4SLinus Torvalds 	HOSTFS_I(root_inode)->host_filename = name;
969*bca27113SPaolo 'Blaisorblade' Giarrusso 	/* Avoid that in the error path, iput(root_inode) frees again name through
970*bca27113SPaolo 'Blaisorblade' Giarrusso 	 * hostfs_destroy_inode! */
971*bca27113SPaolo 'Blaisorblade' Giarrusso 	name = NULL;
9721da177e4SLinus Torvalds 
9731da177e4SLinus Torvalds 	err = -ENOMEM;
9741da177e4SLinus Torvalds 	sb->s_root = d_alloc_root(root_inode);
9751da177e4SLinus Torvalds 	if(sb->s_root == NULL)
9761da177e4SLinus Torvalds 		goto out_put;
9771da177e4SLinus Torvalds 
9781da177e4SLinus Torvalds 	err = read_inode(root_inode);
97951a14110SJeff Dike 	if(err){
98051a14110SJeff Dike                 /* No iput in this case because the dput does that for us */
98151a14110SJeff Dike                 dput(sb->s_root);
98251a14110SJeff Dike                 sb->s_root = NULL;
983*bca27113SPaolo 'Blaisorblade' Giarrusso 		goto out;
98451a14110SJeff Dike         }
9851da177e4SLinus Torvalds 
9861da177e4SLinus Torvalds 	return(0);
9871da177e4SLinus Torvalds 
9881da177e4SLinus Torvalds  out_put:
9891da177e4SLinus Torvalds         iput(root_inode);
9901da177e4SLinus Torvalds  out_free:
9911da177e4SLinus Torvalds 	kfree(name);
9921da177e4SLinus Torvalds  out:
9931da177e4SLinus Torvalds 	return(err);
9941da177e4SLinus Torvalds }
9951da177e4SLinus Torvalds 
996454e2398SDavid Howells static int hostfs_read_sb(struct file_system_type *type,
9971da177e4SLinus Torvalds 			  int flags, const char *dev_name,
998454e2398SDavid Howells 			  void *data, struct vfsmount *mnt)
9991da177e4SLinus Torvalds {
1000454e2398SDavid Howells 	return get_sb_nodev(type, flags, data, hostfs_fill_sb_common, mnt);
10011da177e4SLinus Torvalds }
10021da177e4SLinus Torvalds 
10031da177e4SLinus Torvalds static struct file_system_type hostfs_type = {
10041da177e4SLinus Torvalds 	.owner 		= THIS_MODULE,
10051da177e4SLinus Torvalds 	.name 		= "hostfs",
10061da177e4SLinus Torvalds 	.get_sb 	= hostfs_read_sb,
10071da177e4SLinus Torvalds 	.kill_sb	= kill_anon_super,
10081da177e4SLinus Torvalds 	.fs_flags 	= 0,
10091da177e4SLinus Torvalds };
10101da177e4SLinus Torvalds 
10111da177e4SLinus Torvalds static int __init init_hostfs(void)
10121da177e4SLinus Torvalds {
10131da177e4SLinus Torvalds 	return(register_filesystem(&hostfs_type));
10141da177e4SLinus Torvalds }
10151da177e4SLinus Torvalds 
10161da177e4SLinus Torvalds static void __exit exit_hostfs(void)
10171da177e4SLinus Torvalds {
10181da177e4SLinus Torvalds 	unregister_filesystem(&hostfs_type);
10191da177e4SLinus Torvalds }
10201da177e4SLinus Torvalds 
10211da177e4SLinus Torvalds module_init(init_hostfs)
10221da177e4SLinus Torvalds module_exit(exit_hostfs)
10231da177e4SLinus Torvalds MODULE_LICENSE("GPL");
10241da177e4SLinus Torvalds 
10251da177e4SLinus Torvalds /*
10261da177e4SLinus Torvalds  * Overrides for Emacs so that we follow Linus's tabbing style.
10271da177e4SLinus Torvalds  * Emacs will notice this stuff at the end of the file and automatically
10281da177e4SLinus Torvalds  * adjust the settings for this buffer only.  This must remain at the end
10291da177e4SLinus Torvalds  * of the file.
10301da177e4SLinus Torvalds  * ---------------------------------------------------------------------------
10311da177e4SLinus Torvalds  * Local variables:
10321da177e4SLinus Torvalds  * c-file-style: "linux"
10331da177e4SLinus Torvalds  * End:
10341da177e4SLinus Torvalds  */
1035