xref: /linux/fs/hostfs/hostfs_kern.c (revision 4b6f5d20b04dcbc3d888555522b90ba6d36c4106)
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 
381da177e4SLinus Torvalds #define FILE_HOSTFS_I(file) HOSTFS_I((file)->f_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 
551da177e4SLinus Torvalds static struct inode_operations hostfs_iops;
561da177e4SLinus Torvalds static struct inode_operations hostfs_dir_iops;
571da177e4SLinus Torvalds static 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_blksize = i_blksize;
1601da177e4SLinus Torvalds 	ino->i_blocks = i_blocks;
1611da177e4SLinus Torvalds 	return(0);
1621da177e4SLinus Torvalds }
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds static char *follow_link(char *link)
1651da177e4SLinus Torvalds {
1661da177e4SLinus Torvalds 	int len, n;
1671da177e4SLinus Torvalds 	char *name, *resolved, *end;
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 	len = 64;
1701da177e4SLinus Torvalds 	while(1){
1711da177e4SLinus Torvalds 		n = -ENOMEM;
1721da177e4SLinus Torvalds 		name = kmalloc(len, GFP_KERNEL);
1731da177e4SLinus Torvalds 		if(name == NULL)
1741da177e4SLinus Torvalds 			goto out;
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds 		n = do_readlink(link, name, len);
1771da177e4SLinus Torvalds 		if(n < len)
1781da177e4SLinus Torvalds 			break;
1791da177e4SLinus Torvalds 		len *= 2;
1801da177e4SLinus Torvalds 		kfree(name);
1811da177e4SLinus Torvalds 	}
1821da177e4SLinus Torvalds 	if(n < 0)
1831da177e4SLinus Torvalds 		goto out_free;
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds 	if(*name == '/')
1861da177e4SLinus Torvalds 		return(name);
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds 	end = strrchr(link, '/');
1891da177e4SLinus Torvalds 	if(end == NULL)
1901da177e4SLinus Torvalds 		return(name);
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 	*(end + 1) = '\0';
1931da177e4SLinus Torvalds 	len = strlen(link) + strlen(name) + 1;
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds 	resolved = kmalloc(len, GFP_KERNEL);
1961da177e4SLinus Torvalds 	if(resolved == NULL){
1971da177e4SLinus Torvalds 		n = -ENOMEM;
1981da177e4SLinus Torvalds 		goto out_free;
1991da177e4SLinus Torvalds 	}
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds 	sprintf(resolved, "%s%s", link, name);
2021da177e4SLinus Torvalds 	kfree(name);
2031da177e4SLinus Torvalds 	kfree(link);
2041da177e4SLinus Torvalds 	return(resolved);
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds  out_free:
2071da177e4SLinus Torvalds 	kfree(name);
2081da177e4SLinus Torvalds  out:
2091da177e4SLinus Torvalds 	return(ERR_PTR(n));
2101da177e4SLinus Torvalds }
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds static int read_inode(struct inode *ino)
2131da177e4SLinus Torvalds {
2141da177e4SLinus Torvalds 	char *name;
2151da177e4SLinus Torvalds 	int err = 0;
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 	/* Unfortunately, we are called from iget() when we don't have a dentry
2181da177e4SLinus Torvalds 	 * allocated yet.
2191da177e4SLinus Torvalds 	 */
2201da177e4SLinus Torvalds 	if(list_empty(&ino->i_dentry))
2211da177e4SLinus Torvalds 		goto out;
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds 	err = -ENOMEM;
2241da177e4SLinus Torvalds 	name = inode_name(ino, 0);
2251da177e4SLinus Torvalds 	if(name == NULL)
2261da177e4SLinus Torvalds 		goto out;
2271da177e4SLinus Torvalds 
2281da177e4SLinus Torvalds 	if(file_type(name, NULL, NULL) == OS_TYPE_SYMLINK){
2291da177e4SLinus Torvalds 		name = follow_link(name);
2301da177e4SLinus Torvalds 		if(IS_ERR(name)){
2311da177e4SLinus Torvalds 			err = PTR_ERR(name);
2321da177e4SLinus Torvalds 			goto out;
2331da177e4SLinus Torvalds 		}
2341da177e4SLinus Torvalds 	}
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds 	err = read_name(ino, name);
2371da177e4SLinus Torvalds 	kfree(name);
2381da177e4SLinus Torvalds  out:
2391da177e4SLinus Torvalds 	return(err);
2401da177e4SLinus Torvalds }
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds int hostfs_statfs(struct super_block *sb, struct kstatfs *sf)
2431da177e4SLinus Torvalds {
2441da177e4SLinus Torvalds 	/* do_statfs uses struct statfs64 internally, but the linux kernel
2451da177e4SLinus Torvalds 	 * struct statfs still has 32-bit versions for most of these fields,
2461da177e4SLinus Torvalds 	 * so we convert them here
2471da177e4SLinus Torvalds 	 */
2481da177e4SLinus Torvalds 	int err;
2491da177e4SLinus Torvalds 	long long f_blocks;
2501da177e4SLinus Torvalds 	long long f_bfree;
2511da177e4SLinus Torvalds 	long long f_bavail;
2521da177e4SLinus Torvalds 	long long f_files;
2531da177e4SLinus Torvalds 	long long f_ffree;
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 	err = do_statfs(HOSTFS_I(sb->s_root->d_inode)->host_filename,
2561da177e4SLinus Torvalds 			&sf->f_bsize, &f_blocks, &f_bfree, &f_bavail, &f_files,
2571da177e4SLinus Torvalds 			&f_ffree, &sf->f_fsid, sizeof(sf->f_fsid),
2581da177e4SLinus Torvalds 			&sf->f_namelen, sf->f_spare);
2591da177e4SLinus Torvalds 	if(err) return(err);
2601da177e4SLinus Torvalds 	sf->f_blocks = f_blocks;
2611da177e4SLinus Torvalds 	sf->f_bfree = f_bfree;
2621da177e4SLinus Torvalds 	sf->f_bavail = f_bavail;
2631da177e4SLinus Torvalds 	sf->f_files = f_files;
2641da177e4SLinus Torvalds 	sf->f_ffree = f_ffree;
2651da177e4SLinus Torvalds 	sf->f_type = HOSTFS_SUPER_MAGIC;
2661da177e4SLinus Torvalds 	return(0);
2671da177e4SLinus Torvalds }
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds static struct inode *hostfs_alloc_inode(struct super_block *sb)
2701da177e4SLinus Torvalds {
2711da177e4SLinus Torvalds 	struct hostfs_inode_info *hi;
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds 	hi = kmalloc(sizeof(*hi), GFP_KERNEL);
2741da177e4SLinus Torvalds 	if(hi == NULL)
2751da177e4SLinus Torvalds 		return(NULL);
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 	*hi = ((struct hostfs_inode_info) { .host_filename	= NULL,
2781da177e4SLinus Torvalds 					    .fd			= -1,
2791da177e4SLinus Torvalds 					    .mode		= 0 });
2801da177e4SLinus Torvalds 	inode_init_once(&hi->vfs_inode);
2811da177e4SLinus Torvalds 	return(&hi->vfs_inode);
2821da177e4SLinus Torvalds }
2831da177e4SLinus Torvalds 
2841da177e4SLinus Torvalds static void hostfs_delete_inode(struct inode *inode)
2851da177e4SLinus Torvalds {
286fef26658SMark Fasheh 	truncate_inode_pages(&inode->i_data, 0);
2871da177e4SLinus Torvalds 	if(HOSTFS_I(inode)->fd != -1) {
2881da177e4SLinus Torvalds 		close_file(&HOSTFS_I(inode)->fd);
2891da177e4SLinus Torvalds 		HOSTFS_I(inode)->fd = -1;
2901da177e4SLinus Torvalds 	}
2911da177e4SLinus Torvalds 	clear_inode(inode);
2921da177e4SLinus Torvalds }
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds static void hostfs_destroy_inode(struct inode *inode)
2951da177e4SLinus Torvalds {
2961da177e4SLinus Torvalds 	kfree(HOSTFS_I(inode)->host_filename);
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds 	/*XXX: This should not happen, probably. The check is here for
2991da177e4SLinus Torvalds 	 * additional safety.*/
3001da177e4SLinus Torvalds 	if(HOSTFS_I(inode)->fd != -1) {
3011da177e4SLinus Torvalds 		close_file(&HOSTFS_I(inode)->fd);
3021da177e4SLinus Torvalds 		printk(KERN_DEBUG "Closing host fd in .destroy_inode\n");
3031da177e4SLinus Torvalds 	}
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds 	kfree(HOSTFS_I(inode));
3061da177e4SLinus Torvalds }
3071da177e4SLinus Torvalds 
3081da177e4SLinus Torvalds static void hostfs_read_inode(struct inode *inode)
3091da177e4SLinus Torvalds {
3101da177e4SLinus Torvalds 	read_inode(inode);
3111da177e4SLinus Torvalds }
3121da177e4SLinus Torvalds 
3131da177e4SLinus Torvalds static struct super_operations hostfs_sbops = {
3141da177e4SLinus Torvalds 	.alloc_inode	= hostfs_alloc_inode,
3151da177e4SLinus Torvalds 	.drop_inode	= generic_delete_inode,
3161da177e4SLinus Torvalds 	.delete_inode   = hostfs_delete_inode,
3171da177e4SLinus Torvalds 	.destroy_inode	= hostfs_destroy_inode,
3181da177e4SLinus Torvalds 	.read_inode	= hostfs_read_inode,
3191da177e4SLinus Torvalds 	.statfs		= hostfs_statfs,
3201da177e4SLinus Torvalds };
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds int hostfs_readdir(struct file *file, void *ent, filldir_t filldir)
3231da177e4SLinus Torvalds {
3241da177e4SLinus Torvalds 	void *dir;
3251da177e4SLinus Torvalds 	char *name;
3261da177e4SLinus Torvalds 	unsigned long long next, ino;
3271da177e4SLinus Torvalds 	int error, len;
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds 	name = dentry_name(file->f_dentry, 0);
3301da177e4SLinus Torvalds 	if(name == NULL) return(-ENOMEM);
3311da177e4SLinus Torvalds 	dir = open_dir(name, &error);
3321da177e4SLinus Torvalds 	kfree(name);
3331da177e4SLinus Torvalds 	if(dir == NULL) return(-error);
3341da177e4SLinus Torvalds 	next = file->f_pos;
3351da177e4SLinus Torvalds 	while((name = read_dir(dir, &next, &ino, &len)) != NULL){
3361da177e4SLinus Torvalds 		error = (*filldir)(ent, name, len, file->f_pos,
3371da177e4SLinus Torvalds 				   ino, DT_UNKNOWN);
3381da177e4SLinus Torvalds 		if(error) break;
3391da177e4SLinus Torvalds 		file->f_pos = next;
3401da177e4SLinus Torvalds 	}
3411da177e4SLinus Torvalds 	close_dir(dir);
3421da177e4SLinus Torvalds 	return(0);
3431da177e4SLinus Torvalds }
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds int hostfs_file_open(struct inode *ino, struct file *file)
3461da177e4SLinus Torvalds {
3471da177e4SLinus Torvalds 	char *name;
3481da177e4SLinus Torvalds 	int mode = 0, r = 0, w = 0, fd;
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds 	mode = file->f_mode & (FMODE_READ | FMODE_WRITE);
3511da177e4SLinus Torvalds 	if((mode & HOSTFS_I(ino)->mode) == mode)
3521da177e4SLinus Torvalds 		return(0);
3531da177e4SLinus Torvalds 
3541da177e4SLinus Torvalds 	/* The file may already have been opened, but with the wrong access,
3551da177e4SLinus Torvalds 	 * so this resets things and reopens the file with the new access.
3561da177e4SLinus Torvalds 	 */
3571da177e4SLinus Torvalds 	if(HOSTFS_I(ino)->fd != -1){
3581da177e4SLinus Torvalds 		close_file(&HOSTFS_I(ino)->fd);
3591da177e4SLinus Torvalds 		HOSTFS_I(ino)->fd = -1;
3601da177e4SLinus Torvalds 	}
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds 	HOSTFS_I(ino)->mode |= mode;
3631da177e4SLinus Torvalds 	if(HOSTFS_I(ino)->mode & FMODE_READ)
3641da177e4SLinus Torvalds 		r = 1;
3651da177e4SLinus Torvalds 	if(HOSTFS_I(ino)->mode & FMODE_WRITE)
3661da177e4SLinus Torvalds 		w = 1;
3671da177e4SLinus Torvalds 	if(w)
3681da177e4SLinus Torvalds 		r = 1;
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 	name = dentry_name(file->f_dentry, 0);
3711da177e4SLinus Torvalds 	if(name == NULL)
3721da177e4SLinus Torvalds 		return(-ENOMEM);
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds 	fd = open_file(name, r, w, append);
3751da177e4SLinus Torvalds 	kfree(name);
3761da177e4SLinus Torvalds 	if(fd < 0) return(fd);
3771da177e4SLinus Torvalds 	FILE_HOSTFS_I(file)->fd = fd;
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds 	return(0);
3801da177e4SLinus Torvalds }
3811da177e4SLinus Torvalds 
3821da177e4SLinus Torvalds int hostfs_fsync(struct file *file, struct dentry *dentry, int datasync)
3831da177e4SLinus Torvalds {
384a2d76bd8SPaolo 'Blaisorblade' Giarrusso 	return fsync_file(HOSTFS_I(dentry->d_inode)->fd, datasync);
3851da177e4SLinus Torvalds }
3861da177e4SLinus Torvalds 
387*4b6f5d20SArjan van de Ven static const struct file_operations hostfs_file_fops = {
3881da177e4SLinus Torvalds 	.llseek		= generic_file_llseek,
3891da177e4SLinus Torvalds 	.read		= generic_file_read,
3901da177e4SLinus Torvalds 	.sendfile	= generic_file_sendfile,
3911da177e4SLinus Torvalds 	.aio_read	= generic_file_aio_read,
3921da177e4SLinus Torvalds 	.aio_write	= generic_file_aio_write,
3931da177e4SLinus Torvalds 	.readv		= generic_file_readv,
3941da177e4SLinus Torvalds 	.writev		= generic_file_writev,
3951da177e4SLinus Torvalds 	.write		= generic_file_write,
3961da177e4SLinus Torvalds 	.mmap		= generic_file_mmap,
3971da177e4SLinus Torvalds 	.open		= hostfs_file_open,
3981da177e4SLinus Torvalds 	.release	= NULL,
3991da177e4SLinus Torvalds 	.fsync		= hostfs_fsync,
4001da177e4SLinus Torvalds };
4011da177e4SLinus Torvalds 
402*4b6f5d20SArjan van de Ven static const struct file_operations hostfs_dir_fops = {
4031da177e4SLinus Torvalds 	.llseek		= generic_file_llseek,
4041da177e4SLinus Torvalds 	.readdir	= hostfs_readdir,
4051da177e4SLinus Torvalds 	.read		= generic_read_dir,
4061da177e4SLinus Torvalds };
4071da177e4SLinus Torvalds 
4081da177e4SLinus Torvalds 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 
4441da177e4SLinus Torvalds int hostfs_readpage(struct file *file, struct page *page)
4451da177e4SLinus Torvalds {
4461da177e4SLinus Torvalds 	char *buffer;
4471da177e4SLinus Torvalds 	long long start;
4481da177e4SLinus Torvalds 	int err = 0;
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds 	start = (long long) page->index << PAGE_CACHE_SHIFT;
4511da177e4SLinus Torvalds 	buffer = kmap(page);
4521da177e4SLinus Torvalds 	err = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer,
4531da177e4SLinus Torvalds 			PAGE_CACHE_SIZE);
4541da177e4SLinus Torvalds 	if(err < 0) goto out;
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds 	memset(&buffer[err], 0, PAGE_CACHE_SIZE - err);
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds 	flush_dcache_page(page);
4591da177e4SLinus Torvalds 	SetPageUptodate(page);
4601da177e4SLinus Torvalds 	if (PageError(page)) ClearPageError(page);
4611da177e4SLinus Torvalds 	err = 0;
4621da177e4SLinus Torvalds  out:
4631da177e4SLinus Torvalds 	kunmap(page);
4641da177e4SLinus Torvalds 	unlock_page(page);
4651da177e4SLinus Torvalds 	return(err);
4661da177e4SLinus Torvalds }
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds int hostfs_prepare_write(struct file *file, struct page *page,
4691da177e4SLinus Torvalds 			 unsigned int from, unsigned int to)
4701da177e4SLinus Torvalds {
4711da177e4SLinus Torvalds 	char *buffer;
4721da177e4SLinus Torvalds 	long long start, tmp;
4731da177e4SLinus Torvalds 	int err;
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds 	start = (long long) page->index << PAGE_CACHE_SHIFT;
4761da177e4SLinus Torvalds 	buffer = kmap(page);
4771da177e4SLinus Torvalds 	if(from != 0){
4781da177e4SLinus Torvalds 		tmp = start;
4791da177e4SLinus Torvalds 		err = read_file(FILE_HOSTFS_I(file)->fd, &tmp, buffer,
4801da177e4SLinus Torvalds 				from);
4811da177e4SLinus Torvalds 		if(err < 0) goto out;
4821da177e4SLinus Torvalds 	}
4831da177e4SLinus Torvalds 	if(to != PAGE_CACHE_SIZE){
4841da177e4SLinus Torvalds 		start += to;
4851da177e4SLinus Torvalds 		err = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer + to,
4861da177e4SLinus Torvalds 				PAGE_CACHE_SIZE - to);
4871da177e4SLinus Torvalds 		if(err < 0) goto out;
4881da177e4SLinus Torvalds 	}
4891da177e4SLinus Torvalds 	err = 0;
4901da177e4SLinus Torvalds  out:
4911da177e4SLinus Torvalds 	kunmap(page);
4921da177e4SLinus Torvalds 	return(err);
4931da177e4SLinus Torvalds }
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds int hostfs_commit_write(struct file *file, struct page *page, unsigned from,
4961da177e4SLinus Torvalds 		 unsigned to)
4971da177e4SLinus Torvalds {
4981da177e4SLinus Torvalds 	struct address_space *mapping = page->mapping;
4991da177e4SLinus Torvalds 	struct inode *inode = mapping->host;
5001da177e4SLinus Torvalds 	char *buffer;
5011da177e4SLinus Torvalds 	long long start;
5021da177e4SLinus Torvalds 	int err = 0;
5031da177e4SLinus Torvalds 
50430f04a4eSPaolo 'Blaisorblade' Giarrusso 	start = (((long long) page->index) << PAGE_CACHE_SHIFT) + from;
5051da177e4SLinus Torvalds 	buffer = kmap(page);
5061da177e4SLinus Torvalds 	err = write_file(FILE_HOSTFS_I(file)->fd, &start, buffer + from,
5071da177e4SLinus Torvalds 			 to - from);
5081da177e4SLinus Torvalds 	if(err > 0) err = 0;
50930f04a4eSPaolo 'Blaisorblade' Giarrusso 
51030f04a4eSPaolo 'Blaisorblade' Giarrusso 	/* Actually, if !err, write_file has added to-from to start, so, despite
51130f04a4eSPaolo 'Blaisorblade' Giarrusso 	 * the appearance, we are comparing i_size against the _last_ written
51230f04a4eSPaolo 'Blaisorblade' Giarrusso 	 * location, as we should. */
51330f04a4eSPaolo 'Blaisorblade' Giarrusso 
5141da177e4SLinus Torvalds 	if(!err && (start > inode->i_size))
5151da177e4SLinus Torvalds 		inode->i_size = start;
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds 	kunmap(page);
5181da177e4SLinus Torvalds 	return(err);
5191da177e4SLinus Torvalds }
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds static struct address_space_operations hostfs_aops = {
5221da177e4SLinus Torvalds 	.writepage 	= hostfs_writepage,
5231da177e4SLinus Torvalds 	.readpage	= hostfs_readpage,
524ffa0aea6SPaolo 'Blaisorblade' Giarrusso 	.set_page_dirty = __set_page_dirty_nobuffers,
5251da177e4SLinus Torvalds 	.prepare_write	= hostfs_prepare_write,
5261da177e4SLinus Torvalds 	.commit_write	= hostfs_commit_write
5271da177e4SLinus Torvalds };
5281da177e4SLinus Torvalds 
5291da177e4SLinus Torvalds static int init_inode(struct inode *inode, struct dentry *dentry)
5301da177e4SLinus Torvalds {
5311da177e4SLinus Torvalds 	char *name;
5321da177e4SLinus Torvalds 	int type, err = -ENOMEM;
5331da177e4SLinus Torvalds 	int maj, min;
5341da177e4SLinus Torvalds 	dev_t rdev = 0;
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds 	if(dentry){
5371da177e4SLinus Torvalds 		name = dentry_name(dentry, 0);
5381da177e4SLinus Torvalds 		if(name == NULL)
5391da177e4SLinus Torvalds 			goto out;
5401da177e4SLinus Torvalds 		type = file_type(name, &maj, &min);
5411da177e4SLinus Torvalds 		/*Reencode maj and min with the kernel encoding.*/
5421da177e4SLinus Torvalds 		rdev = MKDEV(maj, min);
5431da177e4SLinus Torvalds 		kfree(name);
5441da177e4SLinus Torvalds 	}
5451da177e4SLinus Torvalds 	else type = OS_TYPE_DIR;
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds 	err = 0;
5481da177e4SLinus Torvalds 	if(type == OS_TYPE_SYMLINK)
5491da177e4SLinus Torvalds 		inode->i_op = &page_symlink_inode_operations;
5501da177e4SLinus Torvalds 	else if(type == OS_TYPE_DIR)
5511da177e4SLinus Torvalds 		inode->i_op = &hostfs_dir_iops;
5521da177e4SLinus Torvalds 	else inode->i_op = &hostfs_iops;
5531da177e4SLinus Torvalds 
5541da177e4SLinus Torvalds 	if(type == OS_TYPE_DIR) inode->i_fop = &hostfs_dir_fops;
5551da177e4SLinus Torvalds 	else inode->i_fop = &hostfs_file_fops;
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds 	if(type == OS_TYPE_SYMLINK)
5581da177e4SLinus Torvalds 		inode->i_mapping->a_ops = &hostfs_link_aops;
5591da177e4SLinus Torvalds 	else inode->i_mapping->a_ops = &hostfs_aops;
5601da177e4SLinus Torvalds 
5611da177e4SLinus Torvalds 	switch (type) {
5621da177e4SLinus Torvalds 	case OS_TYPE_CHARDEV:
5631da177e4SLinus Torvalds 		init_special_inode(inode, S_IFCHR, rdev);
5641da177e4SLinus Torvalds 		break;
5651da177e4SLinus Torvalds 	case OS_TYPE_BLOCKDEV:
5661da177e4SLinus Torvalds 		init_special_inode(inode, S_IFBLK, rdev);
5671da177e4SLinus Torvalds 		break;
5681da177e4SLinus Torvalds 	case OS_TYPE_FIFO:
5691da177e4SLinus Torvalds 		init_special_inode(inode, S_IFIFO, 0);
5701da177e4SLinus Torvalds 		break;
5711da177e4SLinus Torvalds 	case OS_TYPE_SOCK:
5721da177e4SLinus Torvalds 		init_special_inode(inode, S_IFSOCK, 0);
5731da177e4SLinus Torvalds 		break;
5741da177e4SLinus Torvalds 	}
5751da177e4SLinus Torvalds  out:
5761da177e4SLinus Torvalds 	return(err);
5771da177e4SLinus Torvalds }
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds int hostfs_create(struct inode *dir, struct dentry *dentry, int mode,
5801da177e4SLinus Torvalds                  struct nameidata *nd)
5811da177e4SLinus Torvalds {
5821da177e4SLinus Torvalds 	struct inode *inode;
5831da177e4SLinus Torvalds 	char *name;
5841da177e4SLinus Torvalds 	int error, fd;
5851da177e4SLinus Torvalds 
5861da177e4SLinus Torvalds 	error = -ENOMEM;
5871da177e4SLinus Torvalds 	inode = iget(dir->i_sb, 0);
5881da177e4SLinus Torvalds 	if(inode == NULL) goto out;
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds 	error = init_inode(inode, dentry);
5911da177e4SLinus Torvalds 	if(error)
5921da177e4SLinus Torvalds 		goto out_put;
5931da177e4SLinus Torvalds 
5941da177e4SLinus Torvalds 	error = -ENOMEM;
5951da177e4SLinus Torvalds 	name = dentry_name(dentry, 0);
5961da177e4SLinus Torvalds 	if(name == NULL)
5971da177e4SLinus Torvalds 		goto out_put;
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds 	fd = file_create(name,
6001da177e4SLinus Torvalds 			 mode & S_IRUSR, mode & S_IWUSR, mode & S_IXUSR,
6011da177e4SLinus Torvalds 			 mode & S_IRGRP, mode & S_IWGRP, mode & S_IXGRP,
6021da177e4SLinus Torvalds 			 mode & S_IROTH, mode & S_IWOTH, mode & S_IXOTH);
6031da177e4SLinus Torvalds 	if(fd < 0)
6041da177e4SLinus Torvalds 		error = fd;
6051da177e4SLinus Torvalds 	else error = read_name(inode, name);
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds 	kfree(name);
6081da177e4SLinus Torvalds 	if(error)
6091da177e4SLinus Torvalds 		goto out_put;
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds 	HOSTFS_I(inode)->fd = fd;
6121da177e4SLinus Torvalds 	HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE;
6131da177e4SLinus Torvalds 	d_instantiate(dentry, inode);
6141da177e4SLinus Torvalds 	return(0);
6151da177e4SLinus Torvalds 
6161da177e4SLinus Torvalds  out_put:
6171da177e4SLinus Torvalds 	iput(inode);
6181da177e4SLinus Torvalds  out:
6191da177e4SLinus Torvalds 	return(error);
6201da177e4SLinus Torvalds }
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry,
6231da177e4SLinus Torvalds                             struct nameidata *nd)
6241da177e4SLinus Torvalds {
6251da177e4SLinus Torvalds 	struct inode *inode;
6261da177e4SLinus Torvalds 	char *name;
6271da177e4SLinus Torvalds 	int err;
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds 	err = -ENOMEM;
6301da177e4SLinus Torvalds 	inode = iget(ino->i_sb, 0);
6311da177e4SLinus Torvalds 	if(inode == NULL)
6321da177e4SLinus Torvalds 		goto out;
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds 	err = init_inode(inode, dentry);
6351da177e4SLinus Torvalds 	if(err)
6361da177e4SLinus Torvalds 		goto out_put;
6371da177e4SLinus Torvalds 
6381da177e4SLinus Torvalds 	err = -ENOMEM;
6391da177e4SLinus Torvalds 	name = dentry_name(dentry, 0);
6401da177e4SLinus Torvalds 	if(name == NULL)
6411da177e4SLinus Torvalds 		goto out_put;
6421da177e4SLinus Torvalds 
6431da177e4SLinus Torvalds 	err = read_name(inode, name);
6441da177e4SLinus Torvalds 	kfree(name);
6451da177e4SLinus Torvalds 	if(err == -ENOENT){
6461da177e4SLinus Torvalds 		iput(inode);
6471da177e4SLinus Torvalds 		inode = NULL;
6481da177e4SLinus Torvalds 	}
6491da177e4SLinus Torvalds 	else if(err)
6501da177e4SLinus Torvalds 		goto out_put;
6511da177e4SLinus Torvalds 
6521da177e4SLinus Torvalds 	d_add(dentry, inode);
6531da177e4SLinus Torvalds 	dentry->d_op = &hostfs_dentry_ops;
6541da177e4SLinus Torvalds 	return(NULL);
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds  out_put:
6571da177e4SLinus Torvalds 	iput(inode);
6581da177e4SLinus Torvalds  out:
6591da177e4SLinus Torvalds 	return(ERR_PTR(err));
6601da177e4SLinus Torvalds }
6611da177e4SLinus Torvalds 
6621da177e4SLinus Torvalds static char *inode_dentry_name(struct inode *ino, struct dentry *dentry)
6631da177e4SLinus Torvalds {
6641da177e4SLinus Torvalds         char *file;
6651da177e4SLinus Torvalds 	int len;
6661da177e4SLinus Torvalds 
6671da177e4SLinus Torvalds 	file = inode_name(ino, dentry->d_name.len + 1);
6681da177e4SLinus Torvalds 	if(file == NULL) return(NULL);
6691da177e4SLinus Torvalds         strcat(file, "/");
6701da177e4SLinus Torvalds 	len = strlen(file);
6711da177e4SLinus Torvalds         strncat(file, dentry->d_name.name, dentry->d_name.len);
6721da177e4SLinus Torvalds 	file[len + dentry->d_name.len] = '\0';
6731da177e4SLinus Torvalds         return(file);
6741da177e4SLinus Torvalds }
6751da177e4SLinus Torvalds 
6761da177e4SLinus Torvalds int hostfs_link(struct dentry *to, struct inode *ino, struct dentry *from)
6771da177e4SLinus Torvalds {
6781da177e4SLinus Torvalds         char *from_name, *to_name;
6791da177e4SLinus Torvalds         int err;
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds         if((from_name = inode_dentry_name(ino, from)) == NULL)
6821da177e4SLinus Torvalds                 return(-ENOMEM);
6831da177e4SLinus Torvalds         to_name = dentry_name(to, 0);
6841da177e4SLinus Torvalds 	if(to_name == NULL){
6851da177e4SLinus Torvalds 		kfree(from_name);
6861da177e4SLinus Torvalds 		return(-ENOMEM);
6871da177e4SLinus Torvalds 	}
6881da177e4SLinus Torvalds         err = link_file(to_name, from_name);
6891da177e4SLinus Torvalds         kfree(from_name);
6901da177e4SLinus Torvalds         kfree(to_name);
6911da177e4SLinus Torvalds         return(err);
6921da177e4SLinus Torvalds }
6931da177e4SLinus Torvalds 
6941da177e4SLinus Torvalds int hostfs_unlink(struct inode *ino, struct dentry *dentry)
6951da177e4SLinus Torvalds {
6961da177e4SLinus Torvalds 	char *file;
6971da177e4SLinus Torvalds 	int err;
6981da177e4SLinus Torvalds 
6991da177e4SLinus Torvalds 	if((file = inode_dentry_name(ino, dentry)) == NULL) return(-ENOMEM);
7001da177e4SLinus Torvalds 	if(append)
7011da177e4SLinus Torvalds 		return(-EPERM);
7021da177e4SLinus Torvalds 
7031da177e4SLinus Torvalds 	err = unlink_file(file);
7041da177e4SLinus Torvalds 	kfree(file);
7051da177e4SLinus Torvalds 	return(err);
7061da177e4SLinus Torvalds }
7071da177e4SLinus Torvalds 
7081da177e4SLinus Torvalds int hostfs_symlink(struct inode *ino, struct dentry *dentry, const char *to)
7091da177e4SLinus Torvalds {
7101da177e4SLinus Torvalds 	char *file;
7111da177e4SLinus Torvalds 	int err;
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds 	if((file = inode_dentry_name(ino, dentry)) == NULL) return(-ENOMEM);
7141da177e4SLinus Torvalds 	err = make_symlink(file, to);
7151da177e4SLinus Torvalds 	kfree(file);
7161da177e4SLinus Torvalds 	return(err);
7171da177e4SLinus Torvalds }
7181da177e4SLinus Torvalds 
7191da177e4SLinus Torvalds int hostfs_mkdir(struct inode *ino, struct dentry *dentry, int mode)
7201da177e4SLinus Torvalds {
7211da177e4SLinus Torvalds 	char *file;
7221da177e4SLinus Torvalds 	int err;
7231da177e4SLinus Torvalds 
7241da177e4SLinus Torvalds 	if((file = inode_dentry_name(ino, dentry)) == NULL) return(-ENOMEM);
7251da177e4SLinus Torvalds 	err = do_mkdir(file, mode);
7261da177e4SLinus Torvalds 	kfree(file);
7271da177e4SLinus Torvalds 	return(err);
7281da177e4SLinus Torvalds }
7291da177e4SLinus Torvalds 
7301da177e4SLinus Torvalds int hostfs_rmdir(struct inode *ino, struct dentry *dentry)
7311da177e4SLinus Torvalds {
7321da177e4SLinus Torvalds 	char *file;
7331da177e4SLinus Torvalds 	int err;
7341da177e4SLinus Torvalds 
7351da177e4SLinus Torvalds 	if((file = inode_dentry_name(ino, dentry)) == NULL) return(-ENOMEM);
7361da177e4SLinus Torvalds 	err = do_rmdir(file);
7371da177e4SLinus Torvalds 	kfree(file);
7381da177e4SLinus Torvalds 	return(err);
7391da177e4SLinus Torvalds }
7401da177e4SLinus Torvalds 
7411da177e4SLinus Torvalds int hostfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
7421da177e4SLinus Torvalds {
7431da177e4SLinus Torvalds 	struct inode *inode;
7441da177e4SLinus Torvalds 	char *name;
7451da177e4SLinus Torvalds 	int err = -ENOMEM;
7461da177e4SLinus Torvalds 
7471da177e4SLinus Torvalds 	inode = iget(dir->i_sb, 0);
7481da177e4SLinus Torvalds 	if(inode == NULL)
7491da177e4SLinus Torvalds 		goto out;
7501da177e4SLinus Torvalds 
7511da177e4SLinus Torvalds 	err = init_inode(inode, dentry);
7521da177e4SLinus Torvalds 	if(err)
7531da177e4SLinus Torvalds 		goto out_put;
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds 	err = -ENOMEM;
7561da177e4SLinus Torvalds 	name = dentry_name(dentry, 0);
7571da177e4SLinus Torvalds 	if(name == NULL)
7581da177e4SLinus Torvalds 		goto out_put;
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds 	init_special_inode(inode, mode, dev);
7611da177e4SLinus Torvalds 	err = do_mknod(name, mode, dev);
7621da177e4SLinus Torvalds 	if(err)
7631da177e4SLinus Torvalds 		goto out_free;
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds 	err = read_name(inode, name);
7661da177e4SLinus Torvalds 	kfree(name);
7671da177e4SLinus Torvalds 	if(err)
7681da177e4SLinus Torvalds 		goto out_put;
7691da177e4SLinus Torvalds 
7701da177e4SLinus Torvalds 	d_instantiate(dentry, inode);
7711da177e4SLinus Torvalds 	return(0);
7721da177e4SLinus Torvalds 
7731da177e4SLinus Torvalds  out_free:
7741da177e4SLinus Torvalds 	kfree(name);
7751da177e4SLinus Torvalds  out_put:
7761da177e4SLinus Torvalds 	iput(inode);
7771da177e4SLinus Torvalds  out:
7781da177e4SLinus Torvalds 	return(err);
7791da177e4SLinus Torvalds }
7801da177e4SLinus Torvalds 
7811da177e4SLinus Torvalds int hostfs_rename(struct inode *from_ino, struct dentry *from,
7821da177e4SLinus Torvalds 		  struct inode *to_ino, struct dentry *to)
7831da177e4SLinus Torvalds {
7841da177e4SLinus Torvalds 	char *from_name, *to_name;
7851da177e4SLinus Torvalds 	int err;
7861da177e4SLinus Torvalds 
7871da177e4SLinus Torvalds 	if((from_name = inode_dentry_name(from_ino, from)) == NULL)
7881da177e4SLinus Torvalds 		return(-ENOMEM);
7891da177e4SLinus Torvalds 	if((to_name = inode_dentry_name(to_ino, to)) == NULL){
7901da177e4SLinus Torvalds 		kfree(from_name);
7911da177e4SLinus Torvalds 		return(-ENOMEM);
7921da177e4SLinus Torvalds 	}
7931da177e4SLinus Torvalds 	err = rename_file(from_name, to_name);
7941da177e4SLinus Torvalds 	kfree(from_name);
7951da177e4SLinus Torvalds 	kfree(to_name);
7961da177e4SLinus Torvalds 	return(err);
7971da177e4SLinus Torvalds }
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds int hostfs_permission(struct inode *ino, int desired, struct nameidata *nd)
8001da177e4SLinus Torvalds {
8011da177e4SLinus Torvalds 	char *name;
8021da177e4SLinus Torvalds 	int r = 0, w = 0, x = 0, err;
8031da177e4SLinus Torvalds 
8041da177e4SLinus Torvalds 	if (desired & MAY_READ) r = 1;
8051da177e4SLinus Torvalds 	if (desired & MAY_WRITE) w = 1;
8061da177e4SLinus Torvalds 	if (desired & MAY_EXEC) x = 1;
8071da177e4SLinus Torvalds 	name = inode_name(ino, 0);
8081da177e4SLinus Torvalds 	if (name == NULL) return(-ENOMEM);
8091da177e4SLinus Torvalds 
8101da177e4SLinus Torvalds 	if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) ||
8111da177e4SLinus Torvalds 			S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode))
8121da177e4SLinus Torvalds 		err = 0;
8131da177e4SLinus Torvalds 	else
8141da177e4SLinus Torvalds 		err = access_file(name, r, w, x);
8151da177e4SLinus Torvalds 	kfree(name);
8161da177e4SLinus Torvalds 	if(!err)
8171da177e4SLinus Torvalds 		err = generic_permission(ino, desired, NULL);
8181da177e4SLinus Torvalds 	return err;
8191da177e4SLinus Torvalds }
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds int hostfs_setattr(struct dentry *dentry, struct iattr *attr)
8221da177e4SLinus Torvalds {
8231da177e4SLinus Torvalds 	struct hostfs_iattr attrs;
8241da177e4SLinus Torvalds 	char *name;
8251da177e4SLinus Torvalds 	int err;
8261da177e4SLinus Torvalds 
8271da177e4SLinus Torvalds 	err = inode_change_ok(dentry->d_inode, attr);
8281da177e4SLinus Torvalds 	if (err)
8291da177e4SLinus Torvalds 		return err;
8301da177e4SLinus Torvalds 
8311da177e4SLinus Torvalds 	if(append)
8321da177e4SLinus Torvalds 		attr->ia_valid &= ~ATTR_SIZE;
8331da177e4SLinus Torvalds 
8341da177e4SLinus Torvalds 	attrs.ia_valid = 0;
8351da177e4SLinus Torvalds 	if(attr->ia_valid & ATTR_MODE){
8361da177e4SLinus Torvalds 		attrs.ia_valid |= HOSTFS_ATTR_MODE;
8371da177e4SLinus Torvalds 		attrs.ia_mode = attr->ia_mode;
8381da177e4SLinus Torvalds 	}
8391da177e4SLinus Torvalds 	if(attr->ia_valid & ATTR_UID){
8401da177e4SLinus Torvalds 		attrs.ia_valid |= HOSTFS_ATTR_UID;
8411da177e4SLinus Torvalds 		attrs.ia_uid = attr->ia_uid;
8421da177e4SLinus Torvalds 	}
8431da177e4SLinus Torvalds 	if(attr->ia_valid & ATTR_GID){
8441da177e4SLinus Torvalds 		attrs.ia_valid |= HOSTFS_ATTR_GID;
8451da177e4SLinus Torvalds 		attrs.ia_gid = attr->ia_gid;
8461da177e4SLinus Torvalds 	}
8471da177e4SLinus Torvalds 	if(attr->ia_valid & ATTR_SIZE){
8481da177e4SLinus Torvalds 		attrs.ia_valid |= HOSTFS_ATTR_SIZE;
8491da177e4SLinus Torvalds 		attrs.ia_size = attr->ia_size;
8501da177e4SLinus Torvalds 	}
8511da177e4SLinus Torvalds 	if(attr->ia_valid & ATTR_ATIME){
8521da177e4SLinus Torvalds 		attrs.ia_valid |= HOSTFS_ATTR_ATIME;
8531da177e4SLinus Torvalds 		attrs.ia_atime = attr->ia_atime;
8541da177e4SLinus Torvalds 	}
8551da177e4SLinus Torvalds 	if(attr->ia_valid & ATTR_MTIME){
8561da177e4SLinus Torvalds 		attrs.ia_valid |= HOSTFS_ATTR_MTIME;
8571da177e4SLinus Torvalds 		attrs.ia_mtime = attr->ia_mtime;
8581da177e4SLinus Torvalds 	}
8591da177e4SLinus Torvalds 	if(attr->ia_valid & ATTR_CTIME){
8601da177e4SLinus Torvalds 		attrs.ia_valid |= HOSTFS_ATTR_CTIME;
8611da177e4SLinus Torvalds 		attrs.ia_ctime = attr->ia_ctime;
8621da177e4SLinus Torvalds 	}
8631da177e4SLinus Torvalds 	if(attr->ia_valid & ATTR_ATIME_SET){
8641da177e4SLinus Torvalds 		attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET;
8651da177e4SLinus Torvalds 	}
8661da177e4SLinus Torvalds 	if(attr->ia_valid & ATTR_MTIME_SET){
8671da177e4SLinus Torvalds 		attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET;
8681da177e4SLinus Torvalds 	}
8691da177e4SLinus Torvalds 	name = dentry_name(dentry, 0);
8701da177e4SLinus Torvalds 	if(name == NULL) return(-ENOMEM);
8711da177e4SLinus Torvalds 	err = set_attr(name, &attrs);
8721da177e4SLinus Torvalds 	kfree(name);
8731da177e4SLinus Torvalds 	if(err)
8741da177e4SLinus Torvalds 		return(err);
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds 	return(inode_setattr(dentry->d_inode, attr));
8771da177e4SLinus Torvalds }
8781da177e4SLinus Torvalds 
8791da177e4SLinus Torvalds int hostfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
8801da177e4SLinus Torvalds 	   struct kstat *stat)
8811da177e4SLinus Torvalds {
8821da177e4SLinus Torvalds 	generic_fillattr(dentry->d_inode, stat);
8831da177e4SLinus Torvalds 	return(0);
8841da177e4SLinus Torvalds }
8851da177e4SLinus Torvalds 
8861da177e4SLinus Torvalds static struct inode_operations hostfs_iops = {
8871da177e4SLinus Torvalds 	.create		= hostfs_create,
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,
8941da177e4SLinus Torvalds 	.rename		= hostfs_rename,
8951da177e4SLinus Torvalds 	.permission	= hostfs_permission,
8961da177e4SLinus Torvalds 	.setattr	= hostfs_setattr,
8971da177e4SLinus Torvalds 	.getattr	= hostfs_getattr,
8981da177e4SLinus Torvalds };
8991da177e4SLinus Torvalds 
9001da177e4SLinus Torvalds static struct inode_operations hostfs_dir_iops = {
9011da177e4SLinus Torvalds 	.create		= hostfs_create,
9021da177e4SLinus Torvalds 	.lookup		= hostfs_lookup,
9031da177e4SLinus Torvalds 	.link		= hostfs_link,
9041da177e4SLinus Torvalds 	.unlink		= hostfs_unlink,
9051da177e4SLinus Torvalds 	.symlink	= hostfs_symlink,
9061da177e4SLinus Torvalds 	.mkdir		= hostfs_mkdir,
9071da177e4SLinus Torvalds 	.rmdir		= hostfs_rmdir,
9081da177e4SLinus Torvalds 	.mknod		= hostfs_mknod,
9091da177e4SLinus Torvalds 	.rename		= hostfs_rename,
9101da177e4SLinus Torvalds 	.permission	= hostfs_permission,
9111da177e4SLinus Torvalds 	.setattr	= hostfs_setattr,
9121da177e4SLinus Torvalds 	.getattr	= hostfs_getattr,
9131da177e4SLinus Torvalds };
9141da177e4SLinus Torvalds 
9151da177e4SLinus Torvalds int hostfs_link_readpage(struct file *file, struct page *page)
9161da177e4SLinus Torvalds {
9171da177e4SLinus Torvalds 	char *buffer, *name;
9181da177e4SLinus Torvalds 	int err;
9191da177e4SLinus Torvalds 
9201da177e4SLinus Torvalds 	buffer = kmap(page);
9211da177e4SLinus Torvalds 	name = inode_name(page->mapping->host, 0);
9221da177e4SLinus Torvalds 	if(name == NULL) return(-ENOMEM);
9231da177e4SLinus Torvalds 	err = do_readlink(name, buffer, PAGE_CACHE_SIZE);
9241da177e4SLinus Torvalds 	kfree(name);
9251da177e4SLinus Torvalds 	if(err == PAGE_CACHE_SIZE)
9261da177e4SLinus Torvalds 		err = -E2BIG;
9271da177e4SLinus Torvalds 	else if(err > 0){
9281da177e4SLinus Torvalds 		flush_dcache_page(page);
9291da177e4SLinus Torvalds 		SetPageUptodate(page);
9301da177e4SLinus Torvalds 		if (PageError(page)) ClearPageError(page);
9311da177e4SLinus Torvalds 		err = 0;
9321da177e4SLinus Torvalds 	}
9331da177e4SLinus Torvalds 	kunmap(page);
9341da177e4SLinus Torvalds 	unlock_page(page);
9351da177e4SLinus Torvalds 	return(err);
9361da177e4SLinus Torvalds }
9371da177e4SLinus Torvalds 
9381da177e4SLinus Torvalds static struct address_space_operations hostfs_link_aops = {
9391da177e4SLinus Torvalds 	.readpage	= hostfs_link_readpage,
9401da177e4SLinus Torvalds };
9411da177e4SLinus Torvalds 
9421da177e4SLinus Torvalds static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent)
9431da177e4SLinus Torvalds {
9441da177e4SLinus Torvalds 	struct inode *root_inode;
9451da177e4SLinus Torvalds 	char *name, *data = d;
9461da177e4SLinus Torvalds 	int err;
9471da177e4SLinus Torvalds 
9481da177e4SLinus Torvalds 	sb->s_blocksize = 1024;
9491da177e4SLinus Torvalds 	sb->s_blocksize_bits = 10;
9501da177e4SLinus Torvalds 	sb->s_magic = HOSTFS_SUPER_MAGIC;
9511da177e4SLinus Torvalds 	sb->s_op = &hostfs_sbops;
9521da177e4SLinus Torvalds 
9531da177e4SLinus Torvalds 	if((data == NULL) || (*data == '\0'))
9541da177e4SLinus Torvalds 		data = root_ino;
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds 	err = -ENOMEM;
9571da177e4SLinus Torvalds 	name = kmalloc(strlen(data) + 1, GFP_KERNEL);
9581da177e4SLinus Torvalds 	if(name == NULL)
9591da177e4SLinus Torvalds 		goto out;
9601da177e4SLinus Torvalds 
9611da177e4SLinus Torvalds 	strcpy(name, data);
9621da177e4SLinus Torvalds 
9631da177e4SLinus Torvalds 	root_inode = iget(sb, 0);
9641da177e4SLinus Torvalds 	if(root_inode == NULL)
9651da177e4SLinus Torvalds 		goto out_free;
9661da177e4SLinus Torvalds 
9671da177e4SLinus Torvalds 	err = init_inode(root_inode, NULL);
9681da177e4SLinus Torvalds 	if(err)
9691da177e4SLinus Torvalds 		goto out_put;
9701da177e4SLinus Torvalds 
9711da177e4SLinus Torvalds 	HOSTFS_I(root_inode)->host_filename = name;
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;
98351a14110SJeff Dike 		goto out_free;
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 
9961da177e4SLinus Torvalds static struct super_block *hostfs_read_sb(struct file_system_type *type,
9971da177e4SLinus Torvalds 					     int flags, const char *dev_name,
9981da177e4SLinus Torvalds 					     void *data)
9991da177e4SLinus Torvalds {
10001da177e4SLinus Torvalds 	return(get_sb_nodev(type, flags, data, hostfs_fill_sb_common));
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