11da177e4SLinus Torvalds /* 2f1adc05eSJeff Dike * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) 31da177e4SLinus Torvalds * Licensed under the GPL 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Ported the filesystem routines to 2.5. 61da177e4SLinus Torvalds * 2003-02-10 Petr Baudis <pasky@ucw.cz> 71da177e4SLinus Torvalds */ 81da177e4SLinus Torvalds 91da177e4SLinus Torvalds #include <linux/fs.h> 101da177e4SLinus Torvalds #include <linux/module.h> 1184b3db04SJeff Dike #include <linux/mm.h> 121da177e4SLinus Torvalds #include <linux/pagemap.h> 131da177e4SLinus Torvalds #include <linux/statfs.h> 14dd2cc4dfSMiklos Szeredi #include <linux/seq_file.h> 156966a977SJiri Kosina #include <linux/mount.h> 161da177e4SLinus Torvalds #include "hostfs.h" 171da177e4SLinus Torvalds #include "init.h" 1884b3db04SJeff Dike #include "kern.h" 191da177e4SLinus Torvalds 201da177e4SLinus Torvalds struct hostfs_inode_info { 211da177e4SLinus Torvalds char *host_filename; 221da177e4SLinus Torvalds int fd; 23aeb5d727SAl Viro fmode_t mode; 241da177e4SLinus Torvalds struct inode vfs_inode; 251da177e4SLinus Torvalds }; 261da177e4SLinus Torvalds 271da177e4SLinus Torvalds static inline struct hostfs_inode_info *HOSTFS_I(struct inode *inode) 281da177e4SLinus Torvalds { 29f1adc05eSJeff Dike return list_entry(inode, struct hostfs_inode_info, vfs_inode); 301da177e4SLinus Torvalds } 311da177e4SLinus Torvalds 32680b0da9SJosef Sipek #define FILE_HOSTFS_I(file) HOSTFS_I((file)->f_path.dentry->d_inode) 331da177e4SLinus Torvalds 34*e16404edSAl Viro static int hostfs_d_delete(struct dentry *dentry) 351da177e4SLinus Torvalds { 36f1adc05eSJeff Dike return 1; 371da177e4SLinus Torvalds } 381da177e4SLinus Torvalds 39*e16404edSAl Viro static const struct dentry_operations hostfs_dentry_ops = { 401da177e4SLinus Torvalds .d_delete = hostfs_d_delete, 411da177e4SLinus Torvalds }; 421da177e4SLinus Torvalds 431da177e4SLinus Torvalds /* Changed in hostfs_args before the kernel starts running */ 44a6eb0be6SPaolo 'Blaisorblade' Giarrusso static char *root_ino = ""; 451da177e4SLinus Torvalds static int append = 0; 461da177e4SLinus Torvalds 471da177e4SLinus Torvalds #define HOSTFS_SUPER_MAGIC 0x00c0ffee 481da177e4SLinus Torvalds 4992e1d5beSArjan van de Ven static const struct inode_operations hostfs_iops; 5092e1d5beSArjan van de Ven static const struct inode_operations hostfs_dir_iops; 51f5e54d6eSChristoph Hellwig static const struct address_space_operations hostfs_link_aops; 521da177e4SLinus Torvalds 531da177e4SLinus Torvalds #ifndef MODULE 541da177e4SLinus Torvalds static int __init hostfs_args(char *options, int *add) 551da177e4SLinus Torvalds { 561da177e4SLinus Torvalds char *ptr; 571da177e4SLinus Torvalds 581da177e4SLinus Torvalds ptr = strchr(options, ','); 591da177e4SLinus Torvalds if (ptr != NULL) 601da177e4SLinus Torvalds *ptr++ = '\0'; 611da177e4SLinus Torvalds if (*options != '\0') 621da177e4SLinus Torvalds root_ino = options; 631da177e4SLinus Torvalds 641da177e4SLinus Torvalds options = ptr; 651da177e4SLinus Torvalds while (options) { 661da177e4SLinus Torvalds ptr = strchr(options, ','); 671da177e4SLinus Torvalds if (ptr != NULL) 681da177e4SLinus Torvalds *ptr++ = '\0'; 691da177e4SLinus Torvalds if (*options != '\0') { 701da177e4SLinus Torvalds if (!strcmp(options, "append")) 711da177e4SLinus Torvalds append = 1; 721da177e4SLinus Torvalds else printf("hostfs_args - unsupported option - %s\n", 731da177e4SLinus Torvalds options); 741da177e4SLinus Torvalds } 751da177e4SLinus Torvalds options = ptr; 761da177e4SLinus Torvalds } 77f1adc05eSJeff Dike return 0; 781da177e4SLinus Torvalds } 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds __uml_setup("hostfs=", hostfs_args, 811da177e4SLinus Torvalds "hostfs=<root dir>,<flags>,...\n" 821da177e4SLinus Torvalds " This is used to set hostfs parameters. The root directory argument\n" 831da177e4SLinus Torvalds " is used to confine all hostfs mounts to within the specified directory\n" 841da177e4SLinus Torvalds " tree on the host. If this isn't specified, then a user inside UML can\n" 851da177e4SLinus Torvalds " mount anything on the host that's accessible to the user that's running\n" 861da177e4SLinus Torvalds " it.\n" 871da177e4SLinus Torvalds " The only flag currently supported is 'append', which specifies that all\n" 881da177e4SLinus Torvalds " files opened by hostfs will be opened in append mode.\n\n" 891da177e4SLinus Torvalds ); 901da177e4SLinus Torvalds #endif 911da177e4SLinus Torvalds 921da177e4SLinus Torvalds static char *dentry_name(struct dentry *dentry, int extra) 931da177e4SLinus Torvalds { 941da177e4SLinus Torvalds struct dentry *parent; 951da177e4SLinus Torvalds char *root, *name; 961da177e4SLinus Torvalds int len; 971da177e4SLinus Torvalds 981da177e4SLinus Torvalds len = 0; 991da177e4SLinus Torvalds parent = dentry; 1001da177e4SLinus Torvalds while (parent->d_parent != parent) { 1011da177e4SLinus Torvalds len += parent->d_name.len + 1; 1021da177e4SLinus Torvalds parent = parent->d_parent; 1031da177e4SLinus Torvalds } 1041da177e4SLinus Torvalds 1051da177e4SLinus Torvalds root = HOSTFS_I(parent->d_inode)->host_filename; 1061da177e4SLinus Torvalds len += strlen(root); 1071da177e4SLinus Torvalds name = kmalloc(len + extra + 1, GFP_KERNEL); 108f1adc05eSJeff Dike if (name == NULL) 109f1adc05eSJeff Dike return NULL; 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds name[len] = '\0'; 1121da177e4SLinus Torvalds parent = dentry; 1131da177e4SLinus Torvalds while (parent->d_parent != parent) { 1141da177e4SLinus Torvalds len -= parent->d_name.len + 1; 1151da177e4SLinus Torvalds name[len] = '/'; 1161da177e4SLinus Torvalds strncpy(&name[len + 1], parent->d_name.name, 1171da177e4SLinus Torvalds parent->d_name.len); 1181da177e4SLinus Torvalds parent = parent->d_parent; 1191da177e4SLinus Torvalds } 1201da177e4SLinus Torvalds strncpy(name, root, strlen(root)); 121f1adc05eSJeff Dike return name; 1221da177e4SLinus Torvalds } 1231da177e4SLinus Torvalds 1241da177e4SLinus Torvalds static char *inode_name(struct inode *ino, int extra) 1251da177e4SLinus Torvalds { 1261da177e4SLinus Torvalds struct dentry *dentry; 1271da177e4SLinus Torvalds 1281da177e4SLinus Torvalds dentry = list_entry(ino->i_dentry.next, struct dentry, d_alias); 129f1adc05eSJeff Dike return dentry_name(dentry, extra); 1301da177e4SLinus Torvalds } 1311da177e4SLinus Torvalds 1321da177e4SLinus Torvalds static int read_name(struct inode *ino, char *name) 1331da177e4SLinus Torvalds { 13484b3db04SJeff Dike /* 13584b3db04SJeff Dike * The non-int inode fields are copied into ints by stat_file and 1361da177e4SLinus Torvalds * then copied into the inode because passing the actual pointers 1371da177e4SLinus Torvalds * in and having them treated as int * breaks on big-endian machines 1381da177e4SLinus Torvalds */ 1391da177e4SLinus Torvalds int err; 1401da177e4SLinus Torvalds int i_mode, i_nlink, i_blksize; 1411da177e4SLinus Torvalds unsigned long long i_size; 1421da177e4SLinus Torvalds unsigned long long i_ino; 1431da177e4SLinus Torvalds unsigned long long i_blocks; 1441da177e4SLinus Torvalds 1451da177e4SLinus Torvalds err = stat_file(name, &i_ino, &i_mode, &i_nlink, &ino->i_uid, 1461da177e4SLinus Torvalds &ino->i_gid, &i_size, &ino->i_atime, &ino->i_mtime, 1475822b7faSAlberto Bertogli &ino->i_ctime, &i_blksize, &i_blocks, -1); 1481da177e4SLinus Torvalds if (err) 149f1adc05eSJeff Dike return err; 1501da177e4SLinus Torvalds 1511da177e4SLinus Torvalds ino->i_ino = i_ino; 1521da177e4SLinus Torvalds ino->i_mode = i_mode; 1531da177e4SLinus Torvalds ino->i_nlink = i_nlink; 1541da177e4SLinus Torvalds ino->i_size = i_size; 1551da177e4SLinus Torvalds ino->i_blocks = i_blocks; 156f1adc05eSJeff Dike return 0; 1571da177e4SLinus Torvalds } 1581da177e4SLinus Torvalds 1591da177e4SLinus Torvalds static char *follow_link(char *link) 1601da177e4SLinus Torvalds { 1611da177e4SLinus Torvalds int len, n; 1621da177e4SLinus Torvalds char *name, *resolved, *end; 1631da177e4SLinus Torvalds 1641da177e4SLinus Torvalds len = 64; 1651da177e4SLinus Torvalds while (1) { 1661da177e4SLinus Torvalds n = -ENOMEM; 1671da177e4SLinus Torvalds name = kmalloc(len, GFP_KERNEL); 1681da177e4SLinus Torvalds if (name == NULL) 1691da177e4SLinus Torvalds goto out; 1701da177e4SLinus Torvalds 171ea7e743eSWANG Cong n = hostfs_do_readlink(link, name, len); 1721da177e4SLinus Torvalds if (n < len) 1731da177e4SLinus Torvalds break; 1741da177e4SLinus Torvalds len *= 2; 1751da177e4SLinus Torvalds kfree(name); 1761da177e4SLinus Torvalds } 1771da177e4SLinus Torvalds if (n < 0) 1781da177e4SLinus Torvalds goto out_free; 1791da177e4SLinus Torvalds 1801da177e4SLinus Torvalds if (*name == '/') 181f1adc05eSJeff Dike return name; 1821da177e4SLinus Torvalds 1831da177e4SLinus Torvalds end = strrchr(link, '/'); 1841da177e4SLinus Torvalds if (end == NULL) 185f1adc05eSJeff Dike return name; 1861da177e4SLinus Torvalds 1871da177e4SLinus Torvalds *(end + 1) = '\0'; 1881da177e4SLinus Torvalds len = strlen(link) + strlen(name) + 1; 1891da177e4SLinus Torvalds 1901da177e4SLinus Torvalds resolved = kmalloc(len, GFP_KERNEL); 1911da177e4SLinus Torvalds if (resolved == NULL) { 1921da177e4SLinus Torvalds n = -ENOMEM; 1931da177e4SLinus Torvalds goto out_free; 1941da177e4SLinus Torvalds } 1951da177e4SLinus Torvalds 1961da177e4SLinus Torvalds sprintf(resolved, "%s%s", link, name); 1971da177e4SLinus Torvalds kfree(name); 1981da177e4SLinus Torvalds kfree(link); 199f1adc05eSJeff Dike return resolved; 2001da177e4SLinus Torvalds 2011da177e4SLinus Torvalds out_free: 2021da177e4SLinus Torvalds kfree(name); 2031da177e4SLinus Torvalds out: 204f1adc05eSJeff Dike return ERR_PTR(n); 2051da177e4SLinus Torvalds } 2061da177e4SLinus Torvalds 2070a370e5dSDavid Howells static int hostfs_read_inode(struct inode *ino) 2081da177e4SLinus Torvalds { 2091da177e4SLinus Torvalds char *name; 2101da177e4SLinus Torvalds int err = 0; 2111da177e4SLinus Torvalds 21284b3db04SJeff Dike /* 21384b3db04SJeff Dike * Unfortunately, we are called from iget() when we don't have a dentry 2141da177e4SLinus Torvalds * allocated yet. 2151da177e4SLinus Torvalds */ 2161da177e4SLinus Torvalds if (list_empty(&ino->i_dentry)) 2171da177e4SLinus Torvalds goto out; 2181da177e4SLinus Torvalds 2191da177e4SLinus Torvalds err = -ENOMEM; 2201da177e4SLinus Torvalds name = inode_name(ino, 0); 2211da177e4SLinus Torvalds if (name == NULL) 2221da177e4SLinus Torvalds goto out; 2231da177e4SLinus Torvalds 2241da177e4SLinus Torvalds if (file_type(name, NULL, NULL) == OS_TYPE_SYMLINK) { 2251da177e4SLinus Torvalds name = follow_link(name); 2261da177e4SLinus Torvalds if (IS_ERR(name)) { 2271da177e4SLinus Torvalds err = PTR_ERR(name); 2281da177e4SLinus Torvalds goto out; 2291da177e4SLinus Torvalds } 2301da177e4SLinus Torvalds } 2311da177e4SLinus Torvalds 2321da177e4SLinus Torvalds err = read_name(ino, name); 2331da177e4SLinus Torvalds kfree(name); 2341da177e4SLinus Torvalds out: 235f1adc05eSJeff Dike return err; 2361da177e4SLinus Torvalds } 2371da177e4SLinus Torvalds 2380a370e5dSDavid Howells static struct inode *hostfs_iget(struct super_block *sb) 2390a370e5dSDavid Howells { 2400a370e5dSDavid Howells struct inode *inode; 2410a370e5dSDavid Howells long ret; 2420a370e5dSDavid Howells 2430a370e5dSDavid Howells inode = iget_locked(sb, 0); 2440a370e5dSDavid Howells if (!inode) 2450a370e5dSDavid Howells return ERR_PTR(-ENOMEM); 2460a370e5dSDavid Howells if (inode->i_state & I_NEW) { 2470a370e5dSDavid Howells ret = hostfs_read_inode(inode); 2480a370e5dSDavid Howells if (ret < 0) { 2490a370e5dSDavid Howells iget_failed(inode); 2500a370e5dSDavid Howells return ERR_PTR(ret); 2510a370e5dSDavid Howells } 2520a370e5dSDavid Howells unlock_new_inode(inode); 2530a370e5dSDavid Howells } 2540a370e5dSDavid Howells return inode; 2550a370e5dSDavid Howells } 2560a370e5dSDavid Howells 257726c3342SDavid Howells int hostfs_statfs(struct dentry *dentry, struct kstatfs *sf) 2581da177e4SLinus Torvalds { 25984b3db04SJeff Dike /* 26084b3db04SJeff Dike * do_statfs uses struct statfs64 internally, but the linux kernel 2611da177e4SLinus Torvalds * struct statfs still has 32-bit versions for most of these fields, 2621da177e4SLinus Torvalds * so we convert them here 2631da177e4SLinus Torvalds */ 2641da177e4SLinus Torvalds int err; 2651da177e4SLinus Torvalds long long f_blocks; 2661da177e4SLinus Torvalds long long f_bfree; 2671da177e4SLinus Torvalds long long f_bavail; 2681da177e4SLinus Torvalds long long f_files; 2691da177e4SLinus Torvalds long long f_ffree; 2701da177e4SLinus Torvalds 271726c3342SDavid Howells err = do_statfs(HOSTFS_I(dentry->d_sb->s_root->d_inode)->host_filename, 2721da177e4SLinus Torvalds &sf->f_bsize, &f_blocks, &f_bfree, &f_bavail, &f_files, 2731da177e4SLinus Torvalds &f_ffree, &sf->f_fsid, sizeof(sf->f_fsid), 2741da177e4SLinus Torvalds &sf->f_namelen, sf->f_spare); 275f1adc05eSJeff Dike if (err) 276f1adc05eSJeff Dike return err; 2771da177e4SLinus Torvalds sf->f_blocks = f_blocks; 2781da177e4SLinus Torvalds sf->f_bfree = f_bfree; 2791da177e4SLinus Torvalds sf->f_bavail = f_bavail; 2801da177e4SLinus Torvalds sf->f_files = f_files; 2811da177e4SLinus Torvalds sf->f_ffree = f_ffree; 2821da177e4SLinus Torvalds sf->f_type = HOSTFS_SUPER_MAGIC; 283f1adc05eSJeff Dike return 0; 2841da177e4SLinus Torvalds } 2851da177e4SLinus Torvalds 2861da177e4SLinus Torvalds static struct inode *hostfs_alloc_inode(struct super_block *sb) 2871da177e4SLinus Torvalds { 2881da177e4SLinus Torvalds struct hostfs_inode_info *hi; 2891da177e4SLinus Torvalds 2901da177e4SLinus Torvalds hi = kmalloc(sizeof(*hi), GFP_KERNEL); 2911da177e4SLinus Torvalds if (hi == NULL) 292f1adc05eSJeff Dike return NULL; 2931da177e4SLinus Torvalds 2941da177e4SLinus Torvalds *hi = ((struct hostfs_inode_info) { .host_filename = NULL, 2951da177e4SLinus Torvalds .fd = -1, 2961da177e4SLinus Torvalds .mode = 0 }); 2971da177e4SLinus Torvalds inode_init_once(&hi->vfs_inode); 298f1adc05eSJeff Dike return &hi->vfs_inode; 2991da177e4SLinus Torvalds } 3001da177e4SLinus Torvalds 3011da177e4SLinus Torvalds static void hostfs_delete_inode(struct inode *inode) 3021da177e4SLinus Torvalds { 303fef26658SMark Fasheh truncate_inode_pages(&inode->i_data, 0); 3041da177e4SLinus Torvalds if (HOSTFS_I(inode)->fd != -1) { 3051da177e4SLinus Torvalds close_file(&HOSTFS_I(inode)->fd); 3061da177e4SLinus Torvalds HOSTFS_I(inode)->fd = -1; 3071da177e4SLinus Torvalds } 3081da177e4SLinus Torvalds clear_inode(inode); 3091da177e4SLinus Torvalds } 3101da177e4SLinus Torvalds 3111da177e4SLinus Torvalds static void hostfs_destroy_inode(struct inode *inode) 3121da177e4SLinus Torvalds { 3131da177e4SLinus Torvalds kfree(HOSTFS_I(inode)->host_filename); 3141da177e4SLinus Torvalds 31584b3db04SJeff Dike /* 31684b3db04SJeff Dike * XXX: This should not happen, probably. The check is here for 31784b3db04SJeff Dike * additional safety. 31884b3db04SJeff Dike */ 3191da177e4SLinus Torvalds if (HOSTFS_I(inode)->fd != -1) { 3201da177e4SLinus Torvalds close_file(&HOSTFS_I(inode)->fd); 3211da177e4SLinus Torvalds printk(KERN_DEBUG "Closing host fd in .destroy_inode\n"); 3221da177e4SLinus Torvalds } 3231da177e4SLinus Torvalds 3241da177e4SLinus Torvalds kfree(HOSTFS_I(inode)); 3251da177e4SLinus Torvalds } 3261da177e4SLinus Torvalds 327dd2cc4dfSMiklos Szeredi static int hostfs_show_options(struct seq_file *seq, struct vfsmount *vfs) 328dd2cc4dfSMiklos Szeredi { 329dd2cc4dfSMiklos Szeredi struct inode *root = vfs->mnt_sb->s_root->d_inode; 330dd2cc4dfSMiklos Szeredi const char *root_path = HOSTFS_I(root)->host_filename; 331dd2cc4dfSMiklos Szeredi size_t offset = strlen(root_ino) + 1; 332dd2cc4dfSMiklos Szeredi 333dd2cc4dfSMiklos Szeredi if (strlen(root_path) > offset) 334dd2cc4dfSMiklos Szeredi seq_printf(seq, ",%s", root_path + offset); 335dd2cc4dfSMiklos Szeredi 336dd2cc4dfSMiklos Szeredi return 0; 337dd2cc4dfSMiklos Szeredi } 338dd2cc4dfSMiklos Szeredi 339ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations hostfs_sbops = { 3401da177e4SLinus Torvalds .alloc_inode = hostfs_alloc_inode, 3411da177e4SLinus Torvalds .drop_inode = generic_delete_inode, 3421da177e4SLinus Torvalds .delete_inode = hostfs_delete_inode, 3431da177e4SLinus Torvalds .destroy_inode = hostfs_destroy_inode, 3441da177e4SLinus Torvalds .statfs = hostfs_statfs, 345dd2cc4dfSMiklos Szeredi .show_options = hostfs_show_options, 3461da177e4SLinus Torvalds }; 3471da177e4SLinus Torvalds 3481da177e4SLinus Torvalds int hostfs_readdir(struct file *file, void *ent, filldir_t filldir) 3491da177e4SLinus Torvalds { 3501da177e4SLinus Torvalds void *dir; 3511da177e4SLinus Torvalds char *name; 3521da177e4SLinus Torvalds unsigned long long next, ino; 3531da177e4SLinus Torvalds int error, len; 3541da177e4SLinus Torvalds 355680b0da9SJosef Sipek name = dentry_name(file->f_path.dentry, 0); 356f1adc05eSJeff Dike if (name == NULL) 357f1adc05eSJeff Dike return -ENOMEM; 3581da177e4SLinus Torvalds dir = open_dir(name, &error); 3591da177e4SLinus Torvalds kfree(name); 360f1adc05eSJeff Dike if (dir == NULL) 361f1adc05eSJeff Dike return -error; 3621da177e4SLinus Torvalds next = file->f_pos; 3631da177e4SLinus Torvalds while ((name = read_dir(dir, &next, &ino, &len)) != NULL) { 3641da177e4SLinus Torvalds error = (*filldir)(ent, name, len, file->f_pos, 3651da177e4SLinus Torvalds ino, DT_UNKNOWN); 3661da177e4SLinus Torvalds if (error) break; 3671da177e4SLinus Torvalds file->f_pos = next; 3681da177e4SLinus Torvalds } 3691da177e4SLinus Torvalds close_dir(dir); 370f1adc05eSJeff Dike return 0; 3711da177e4SLinus Torvalds } 3721da177e4SLinus Torvalds 3731da177e4SLinus Torvalds int hostfs_file_open(struct inode *ino, struct file *file) 3741da177e4SLinus Torvalds { 3751da177e4SLinus Torvalds char *name; 376aeb5d727SAl Viro fmode_t mode = 0; 377aeb5d727SAl Viro int r = 0, w = 0, fd; 3781da177e4SLinus Torvalds 3791da177e4SLinus Torvalds mode = file->f_mode & (FMODE_READ | FMODE_WRITE); 3801da177e4SLinus Torvalds if ((mode & HOSTFS_I(ino)->mode) == mode) 381f1adc05eSJeff Dike return 0; 3821da177e4SLinus Torvalds 38384b3db04SJeff Dike /* 38484b3db04SJeff Dike * The file may already have been opened, but with the wrong access, 3851da177e4SLinus Torvalds * so this resets things and reopens the file with the new access. 3861da177e4SLinus Torvalds */ 3871da177e4SLinus Torvalds if (HOSTFS_I(ino)->fd != -1) { 3881da177e4SLinus Torvalds close_file(&HOSTFS_I(ino)->fd); 3891da177e4SLinus Torvalds HOSTFS_I(ino)->fd = -1; 3901da177e4SLinus Torvalds } 3911da177e4SLinus Torvalds 3921da177e4SLinus Torvalds HOSTFS_I(ino)->mode |= mode; 3931da177e4SLinus Torvalds if (HOSTFS_I(ino)->mode & FMODE_READ) 3941da177e4SLinus Torvalds r = 1; 3951da177e4SLinus Torvalds if (HOSTFS_I(ino)->mode & FMODE_WRITE) 3961da177e4SLinus Torvalds w = 1; 3971da177e4SLinus Torvalds if (w) 3981da177e4SLinus Torvalds r = 1; 3991da177e4SLinus Torvalds 400680b0da9SJosef Sipek name = dentry_name(file->f_path.dentry, 0); 4011da177e4SLinus Torvalds if (name == NULL) 402f1adc05eSJeff Dike return -ENOMEM; 4031da177e4SLinus Torvalds 4041da177e4SLinus Torvalds fd = open_file(name, r, w, append); 4051da177e4SLinus Torvalds kfree(name); 406f1adc05eSJeff Dike if (fd < 0) 407f1adc05eSJeff Dike return fd; 4081da177e4SLinus Torvalds FILE_HOSTFS_I(file)->fd = fd; 4091da177e4SLinus Torvalds 410f1adc05eSJeff Dike return 0; 4111da177e4SLinus Torvalds } 4121da177e4SLinus Torvalds 4131da177e4SLinus Torvalds int hostfs_fsync(struct file *file, struct dentry *dentry, int datasync) 4141da177e4SLinus Torvalds { 415a2d76bd8SPaolo 'Blaisorblade' Giarrusso return fsync_file(HOSTFS_I(dentry->d_inode)->fd, datasync); 4161da177e4SLinus Torvalds } 4171da177e4SLinus Torvalds 4184b6f5d20SArjan van de Ven static const struct file_operations hostfs_file_fops = { 4191da177e4SLinus Torvalds .llseek = generic_file_llseek, 420543ade1fSBadari Pulavarty .read = do_sync_read, 4215ffc4ef4SJens Axboe .splice_read = generic_file_splice_read, 4221da177e4SLinus Torvalds .aio_read = generic_file_aio_read, 4231da177e4SLinus Torvalds .aio_write = generic_file_aio_write, 424543ade1fSBadari Pulavarty .write = do_sync_write, 4251da177e4SLinus Torvalds .mmap = generic_file_mmap, 4261da177e4SLinus Torvalds .open = hostfs_file_open, 4271da177e4SLinus Torvalds .release = NULL, 4281da177e4SLinus Torvalds .fsync = hostfs_fsync, 4291da177e4SLinus Torvalds }; 4301da177e4SLinus Torvalds 4314b6f5d20SArjan van de Ven static const struct file_operations hostfs_dir_fops = { 4321da177e4SLinus Torvalds .llseek = generic_file_llseek, 4331da177e4SLinus Torvalds .readdir = hostfs_readdir, 4341da177e4SLinus Torvalds .read = generic_read_dir, 4351da177e4SLinus Torvalds }; 4361da177e4SLinus Torvalds 4371da177e4SLinus Torvalds int hostfs_writepage(struct page *page, struct writeback_control *wbc) 4381da177e4SLinus Torvalds { 4391da177e4SLinus Torvalds struct address_space *mapping = page->mapping; 4401da177e4SLinus Torvalds struct inode *inode = mapping->host; 4411da177e4SLinus Torvalds char *buffer; 4421da177e4SLinus Torvalds unsigned long long base; 4431da177e4SLinus Torvalds int count = PAGE_CACHE_SIZE; 4441da177e4SLinus Torvalds int end_index = inode->i_size >> PAGE_CACHE_SHIFT; 4451da177e4SLinus Torvalds int err; 4461da177e4SLinus Torvalds 4471da177e4SLinus Torvalds if (page->index >= end_index) 4481da177e4SLinus Torvalds count = inode->i_size & (PAGE_CACHE_SIZE-1); 4491da177e4SLinus Torvalds 4501da177e4SLinus Torvalds buffer = kmap(page); 4511da177e4SLinus Torvalds base = ((unsigned long long) page->index) << PAGE_CACHE_SHIFT; 4521da177e4SLinus Torvalds 4531da177e4SLinus Torvalds err = write_file(HOSTFS_I(inode)->fd, &base, buffer, count); 4541da177e4SLinus Torvalds if (err != count) { 4551da177e4SLinus Torvalds ClearPageUptodate(page); 4561da177e4SLinus Torvalds goto out; 4571da177e4SLinus Torvalds } 4581da177e4SLinus Torvalds 4591da177e4SLinus Torvalds if (base > inode->i_size) 4601da177e4SLinus Torvalds inode->i_size = base; 4611da177e4SLinus Torvalds 4621da177e4SLinus Torvalds if (PageError(page)) 4631da177e4SLinus Torvalds ClearPageError(page); 4641da177e4SLinus Torvalds err = 0; 4651da177e4SLinus Torvalds 4661da177e4SLinus Torvalds out: 4671da177e4SLinus Torvalds kunmap(page); 4681da177e4SLinus Torvalds 4691da177e4SLinus Torvalds unlock_page(page); 4701da177e4SLinus Torvalds return err; 4711da177e4SLinus Torvalds } 4721da177e4SLinus Torvalds 4731da177e4SLinus Torvalds int hostfs_readpage(struct file *file, struct page *page) 4741da177e4SLinus Torvalds { 4751da177e4SLinus Torvalds char *buffer; 4761da177e4SLinus Torvalds long long start; 4771da177e4SLinus Torvalds int err = 0; 4781da177e4SLinus Torvalds 4791da177e4SLinus Torvalds start = (long long) page->index << PAGE_CACHE_SHIFT; 4801da177e4SLinus Torvalds buffer = kmap(page); 4811da177e4SLinus Torvalds err = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer, 4821da177e4SLinus Torvalds PAGE_CACHE_SIZE); 48384b3db04SJeff Dike if (err < 0) 48484b3db04SJeff Dike goto out; 4851da177e4SLinus Torvalds 4861da177e4SLinus Torvalds memset(&buffer[err], 0, PAGE_CACHE_SIZE - err); 4871da177e4SLinus Torvalds 4881da177e4SLinus Torvalds flush_dcache_page(page); 4891da177e4SLinus Torvalds SetPageUptodate(page); 4901da177e4SLinus Torvalds if (PageError(page)) ClearPageError(page); 4911da177e4SLinus Torvalds err = 0; 4921da177e4SLinus Torvalds out: 4931da177e4SLinus Torvalds kunmap(page); 4941da177e4SLinus Torvalds unlock_page(page); 495f1adc05eSJeff Dike return err; 4961da177e4SLinus Torvalds } 4971da177e4SLinus Torvalds 498ae361ff4SNick Piggin int hostfs_write_begin(struct file *file, struct address_space *mapping, 499ae361ff4SNick Piggin loff_t pos, unsigned len, unsigned flags, 500ae361ff4SNick Piggin struct page **pagep, void **fsdata) 5011da177e4SLinus Torvalds { 502ae361ff4SNick Piggin pgoff_t index = pos >> PAGE_CACHE_SHIFT; 503ae361ff4SNick Piggin 50454566b2cSNick Piggin *pagep = grab_cache_page_write_begin(mapping, index, flags); 505ae361ff4SNick Piggin if (!*pagep) 506ae361ff4SNick Piggin return -ENOMEM; 507ae361ff4SNick Piggin return 0; 508ae361ff4SNick Piggin } 509ae361ff4SNick Piggin 510ae361ff4SNick Piggin int hostfs_write_end(struct file *file, struct address_space *mapping, 511ae361ff4SNick Piggin loff_t pos, unsigned len, unsigned copied, 512ae361ff4SNick Piggin struct page *page, void *fsdata) 513ae361ff4SNick Piggin { 514ae361ff4SNick Piggin struct inode *inode = mapping->host; 515ae361ff4SNick Piggin void *buffer; 516ae361ff4SNick Piggin unsigned from = pos & (PAGE_CACHE_SIZE - 1); 5171da177e4SLinus Torvalds int err; 5181da177e4SLinus Torvalds 5191da177e4SLinus Torvalds buffer = kmap(page); 520ae361ff4SNick Piggin err = write_file(FILE_HOSTFS_I(file)->fd, &pos, buffer + from, copied); 5211da177e4SLinus Torvalds kunmap(page); 5221da177e4SLinus Torvalds 523ae361ff4SNick Piggin if (!PageUptodate(page) && err == PAGE_CACHE_SIZE) 524ae361ff4SNick Piggin SetPageUptodate(page); 5251da177e4SLinus Torvalds 52684b3db04SJeff Dike /* 52784b3db04SJeff Dike * If err > 0, write_file has added err to pos, so we are comparing 528ae361ff4SNick Piggin * i_size against the last byte written. 529ae361ff4SNick Piggin */ 530ae361ff4SNick Piggin if (err > 0 && (pos > inode->i_size)) 531ae361ff4SNick Piggin inode->i_size = pos; 532ae361ff4SNick Piggin unlock_page(page); 533ae361ff4SNick Piggin page_cache_release(page); 53430f04a4eSPaolo 'Blaisorblade' Giarrusso 535f1adc05eSJeff Dike return err; 5361da177e4SLinus Torvalds } 5371da177e4SLinus Torvalds 538f5e54d6eSChristoph Hellwig static const struct address_space_operations hostfs_aops = { 5391da177e4SLinus Torvalds .writepage = hostfs_writepage, 5401da177e4SLinus Torvalds .readpage = hostfs_readpage, 541ffa0aea6SPaolo 'Blaisorblade' Giarrusso .set_page_dirty = __set_page_dirty_nobuffers, 542ae361ff4SNick Piggin .write_begin = hostfs_write_begin, 543ae361ff4SNick Piggin .write_end = hostfs_write_end, 5441da177e4SLinus Torvalds }; 5451da177e4SLinus Torvalds 5461da177e4SLinus Torvalds static int init_inode(struct inode *inode, struct dentry *dentry) 5471da177e4SLinus Torvalds { 5481da177e4SLinus Torvalds char *name; 5491da177e4SLinus Torvalds int type, err = -ENOMEM; 5501da177e4SLinus Torvalds int maj, min; 5511da177e4SLinus Torvalds dev_t rdev = 0; 5521da177e4SLinus Torvalds 5531da177e4SLinus Torvalds if (dentry) { 5541da177e4SLinus Torvalds name = dentry_name(dentry, 0); 5551da177e4SLinus Torvalds if (name == NULL) 5561da177e4SLinus Torvalds goto out; 5571da177e4SLinus Torvalds type = file_type(name, &maj, &min); 5581da177e4SLinus Torvalds /* Reencode maj and min with the kernel encoding.*/ 5591da177e4SLinus Torvalds rdev = MKDEV(maj, min); 5601da177e4SLinus Torvalds kfree(name); 5611da177e4SLinus Torvalds } 5621da177e4SLinus Torvalds else type = OS_TYPE_DIR; 5631da177e4SLinus Torvalds 5641da177e4SLinus Torvalds err = 0; 5651da177e4SLinus Torvalds if (type == OS_TYPE_SYMLINK) 5661da177e4SLinus Torvalds inode->i_op = &page_symlink_inode_operations; 5671da177e4SLinus Torvalds else if (type == OS_TYPE_DIR) 5681da177e4SLinus Torvalds inode->i_op = &hostfs_dir_iops; 5691da177e4SLinus Torvalds else inode->i_op = &hostfs_iops; 5701da177e4SLinus Torvalds 5711da177e4SLinus Torvalds if (type == OS_TYPE_DIR) inode->i_fop = &hostfs_dir_fops; 5721da177e4SLinus Torvalds else inode->i_fop = &hostfs_file_fops; 5731da177e4SLinus Torvalds 5741da177e4SLinus Torvalds if (type == OS_TYPE_SYMLINK) 5751da177e4SLinus Torvalds inode->i_mapping->a_ops = &hostfs_link_aops; 5761da177e4SLinus Torvalds else inode->i_mapping->a_ops = &hostfs_aops; 5771da177e4SLinus Torvalds 5781da177e4SLinus Torvalds switch (type) { 5791da177e4SLinus Torvalds case OS_TYPE_CHARDEV: 5801da177e4SLinus Torvalds init_special_inode(inode, S_IFCHR, rdev); 5811da177e4SLinus Torvalds break; 5821da177e4SLinus Torvalds case OS_TYPE_BLOCKDEV: 5831da177e4SLinus Torvalds init_special_inode(inode, S_IFBLK, rdev); 5841da177e4SLinus Torvalds break; 5851da177e4SLinus Torvalds case OS_TYPE_FIFO: 5861da177e4SLinus Torvalds init_special_inode(inode, S_IFIFO, 0); 5871da177e4SLinus Torvalds break; 5881da177e4SLinus Torvalds case OS_TYPE_SOCK: 5891da177e4SLinus Torvalds init_special_inode(inode, S_IFSOCK, 0); 5901da177e4SLinus Torvalds break; 5911da177e4SLinus Torvalds } 5921da177e4SLinus Torvalds out: 593f1adc05eSJeff Dike return err; 5941da177e4SLinus Torvalds } 5951da177e4SLinus Torvalds 5961da177e4SLinus Torvalds int hostfs_create(struct inode *dir, struct dentry *dentry, int mode, 5971da177e4SLinus Torvalds struct nameidata *nd) 5981da177e4SLinus Torvalds { 5991da177e4SLinus Torvalds struct inode *inode; 6001da177e4SLinus Torvalds char *name; 6011da177e4SLinus Torvalds int error, fd; 6021da177e4SLinus Torvalds 6030a370e5dSDavid Howells inode = hostfs_iget(dir->i_sb); 6040a370e5dSDavid Howells if (IS_ERR(inode)) { 6050a370e5dSDavid Howells error = PTR_ERR(inode); 60684b3db04SJeff Dike goto out; 6070a370e5dSDavid Howells } 6081da177e4SLinus Torvalds 6091da177e4SLinus Torvalds error = init_inode(inode, dentry); 6101da177e4SLinus Torvalds if (error) 6111da177e4SLinus Torvalds goto out_put; 6121da177e4SLinus Torvalds 6131da177e4SLinus Torvalds error = -ENOMEM; 6141da177e4SLinus Torvalds name = dentry_name(dentry, 0); 6151da177e4SLinus Torvalds if (name == NULL) 6161da177e4SLinus Torvalds goto out_put; 6171da177e4SLinus Torvalds 6181da177e4SLinus Torvalds fd = file_create(name, 6191da177e4SLinus Torvalds mode & S_IRUSR, mode & S_IWUSR, mode & S_IXUSR, 6201da177e4SLinus Torvalds mode & S_IRGRP, mode & S_IWGRP, mode & S_IXGRP, 6211da177e4SLinus Torvalds mode & S_IROTH, mode & S_IWOTH, mode & S_IXOTH); 6221da177e4SLinus Torvalds if (fd < 0) 6231da177e4SLinus Torvalds error = fd; 6241da177e4SLinus Torvalds else error = read_name(inode, name); 6251da177e4SLinus Torvalds 6261da177e4SLinus Torvalds kfree(name); 6271da177e4SLinus Torvalds if (error) 6281da177e4SLinus Torvalds goto out_put; 6291da177e4SLinus Torvalds 6301da177e4SLinus Torvalds HOSTFS_I(inode)->fd = fd; 6311da177e4SLinus Torvalds HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE; 6321da177e4SLinus Torvalds d_instantiate(dentry, inode); 633f1adc05eSJeff Dike return 0; 6341da177e4SLinus Torvalds 6351da177e4SLinus Torvalds out_put: 6361da177e4SLinus Torvalds iput(inode); 6371da177e4SLinus Torvalds out: 638f1adc05eSJeff Dike return error; 6391da177e4SLinus Torvalds } 6401da177e4SLinus Torvalds 6411da177e4SLinus Torvalds struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry, 6421da177e4SLinus Torvalds struct nameidata *nd) 6431da177e4SLinus Torvalds { 6441da177e4SLinus Torvalds struct inode *inode; 6451da177e4SLinus Torvalds char *name; 6461da177e4SLinus Torvalds int err; 6471da177e4SLinus Torvalds 6480a370e5dSDavid Howells inode = hostfs_iget(ino->i_sb); 6490a370e5dSDavid Howells if (IS_ERR(inode)) { 6500a370e5dSDavid Howells err = PTR_ERR(inode); 6511da177e4SLinus Torvalds goto out; 6520a370e5dSDavid Howells } 6531da177e4SLinus Torvalds 6541da177e4SLinus Torvalds err = init_inode(inode, dentry); 6551da177e4SLinus Torvalds if (err) 6561da177e4SLinus Torvalds goto out_put; 6571da177e4SLinus Torvalds 6581da177e4SLinus Torvalds err = -ENOMEM; 6591da177e4SLinus Torvalds name = dentry_name(dentry, 0); 6601da177e4SLinus Torvalds if (name == NULL) 6611da177e4SLinus Torvalds goto out_put; 6621da177e4SLinus Torvalds 6631da177e4SLinus Torvalds err = read_name(inode, name); 6641da177e4SLinus Torvalds kfree(name); 6651da177e4SLinus Torvalds if (err == -ENOENT) { 6661da177e4SLinus Torvalds iput(inode); 6671da177e4SLinus Torvalds inode = NULL; 6681da177e4SLinus Torvalds } 6691da177e4SLinus Torvalds else if (err) 6701da177e4SLinus Torvalds goto out_put; 6711da177e4SLinus Torvalds 6721da177e4SLinus Torvalds d_add(dentry, inode); 6731da177e4SLinus Torvalds dentry->d_op = &hostfs_dentry_ops; 674f1adc05eSJeff Dike return NULL; 6751da177e4SLinus Torvalds 6761da177e4SLinus Torvalds out_put: 6771da177e4SLinus Torvalds iput(inode); 6781da177e4SLinus Torvalds out: 679f1adc05eSJeff Dike return ERR_PTR(err); 6801da177e4SLinus Torvalds } 6811da177e4SLinus Torvalds 6821da177e4SLinus Torvalds static char *inode_dentry_name(struct inode *ino, struct dentry *dentry) 6831da177e4SLinus Torvalds { 6841da177e4SLinus Torvalds char *file; 6851da177e4SLinus Torvalds int len; 6861da177e4SLinus Torvalds 6871da177e4SLinus Torvalds file = inode_name(ino, dentry->d_name.len + 1); 688f1adc05eSJeff Dike if (file == NULL) 689f1adc05eSJeff Dike return NULL; 6901da177e4SLinus Torvalds strcat(file, "/"); 6911da177e4SLinus Torvalds len = strlen(file); 6921da177e4SLinus Torvalds strncat(file, dentry->d_name.name, dentry->d_name.len); 6931da177e4SLinus Torvalds file[len + dentry->d_name.len] = '\0'; 694f1adc05eSJeff Dike return file; 6951da177e4SLinus Torvalds } 6961da177e4SLinus Torvalds 6971da177e4SLinus Torvalds int hostfs_link(struct dentry *to, struct inode *ino, struct dentry *from) 6981da177e4SLinus Torvalds { 6991da177e4SLinus Torvalds char *from_name, *to_name; 7001da177e4SLinus Torvalds int err; 7011da177e4SLinus Torvalds 7021da177e4SLinus Torvalds if ((from_name = inode_dentry_name(ino, from)) == NULL) 703f1adc05eSJeff Dike return -ENOMEM; 7041da177e4SLinus Torvalds to_name = dentry_name(to, 0); 7051da177e4SLinus Torvalds if (to_name == NULL) { 7061da177e4SLinus Torvalds kfree(from_name); 707f1adc05eSJeff Dike return -ENOMEM; 7081da177e4SLinus Torvalds } 7091da177e4SLinus Torvalds err = link_file(to_name, from_name); 7101da177e4SLinus Torvalds kfree(from_name); 7111da177e4SLinus Torvalds kfree(to_name); 712f1adc05eSJeff Dike return err; 7131da177e4SLinus Torvalds } 7141da177e4SLinus Torvalds 7151da177e4SLinus Torvalds int hostfs_unlink(struct inode *ino, struct dentry *dentry) 7161da177e4SLinus Torvalds { 7171da177e4SLinus Torvalds char *file; 7181da177e4SLinus Torvalds int err; 7191da177e4SLinus Torvalds 720f1adc05eSJeff Dike if ((file = inode_dentry_name(ino, dentry)) == NULL) 721f1adc05eSJeff Dike return -ENOMEM; 7221da177e4SLinus Torvalds if (append) 723f1adc05eSJeff Dike return -EPERM; 7241da177e4SLinus Torvalds 7251da177e4SLinus Torvalds err = unlink_file(file); 7261da177e4SLinus Torvalds kfree(file); 727f1adc05eSJeff Dike return err; 7281da177e4SLinus Torvalds } 7291da177e4SLinus Torvalds 7301da177e4SLinus Torvalds int hostfs_symlink(struct inode *ino, struct dentry *dentry, const char *to) 7311da177e4SLinus Torvalds { 7321da177e4SLinus Torvalds char *file; 7331da177e4SLinus Torvalds int err; 7341da177e4SLinus Torvalds 735f1adc05eSJeff Dike if ((file = inode_dentry_name(ino, dentry)) == NULL) 736f1adc05eSJeff Dike return -ENOMEM; 7371da177e4SLinus Torvalds err = make_symlink(file, to); 7381da177e4SLinus Torvalds kfree(file); 739f1adc05eSJeff Dike return err; 7401da177e4SLinus Torvalds } 7411da177e4SLinus Torvalds 7421da177e4SLinus Torvalds int hostfs_mkdir(struct inode *ino, struct dentry *dentry, int mode) 7431da177e4SLinus Torvalds { 7441da177e4SLinus Torvalds char *file; 7451da177e4SLinus Torvalds int err; 7461da177e4SLinus Torvalds 747f1adc05eSJeff Dike if ((file = inode_dentry_name(ino, dentry)) == NULL) 748f1adc05eSJeff Dike return -ENOMEM; 7491da177e4SLinus Torvalds err = do_mkdir(file, mode); 7501da177e4SLinus Torvalds kfree(file); 751f1adc05eSJeff Dike return err; 7521da177e4SLinus Torvalds } 7531da177e4SLinus Torvalds 7541da177e4SLinus Torvalds int hostfs_rmdir(struct inode *ino, struct dentry *dentry) 7551da177e4SLinus Torvalds { 7561da177e4SLinus Torvalds char *file; 7571da177e4SLinus Torvalds int err; 7581da177e4SLinus Torvalds 759f1adc05eSJeff Dike if ((file = inode_dentry_name(ino, dentry)) == NULL) 760f1adc05eSJeff Dike return -ENOMEM; 7611da177e4SLinus Torvalds err = do_rmdir(file); 7621da177e4SLinus Torvalds kfree(file); 763f1adc05eSJeff Dike return err; 7641da177e4SLinus Torvalds } 7651da177e4SLinus Torvalds 7661da177e4SLinus Torvalds int hostfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) 7671da177e4SLinus Torvalds { 7681da177e4SLinus Torvalds struct inode *inode; 7691da177e4SLinus Torvalds char *name; 7700a370e5dSDavid Howells int err; 7711da177e4SLinus Torvalds 7720a370e5dSDavid Howells inode = hostfs_iget(dir->i_sb); 7730a370e5dSDavid Howells if (IS_ERR(inode)) { 7740a370e5dSDavid Howells err = PTR_ERR(inode); 7751da177e4SLinus Torvalds goto out; 7760a370e5dSDavid Howells } 7771da177e4SLinus Torvalds 7781da177e4SLinus Torvalds err = init_inode(inode, dentry); 7791da177e4SLinus Torvalds if (err) 7801da177e4SLinus Torvalds goto out_put; 7811da177e4SLinus Torvalds 7821da177e4SLinus Torvalds err = -ENOMEM; 7831da177e4SLinus Torvalds name = dentry_name(dentry, 0); 7841da177e4SLinus Torvalds if (name == NULL) 7851da177e4SLinus Torvalds goto out_put; 7861da177e4SLinus Torvalds 7871da177e4SLinus Torvalds init_special_inode(inode, mode, dev); 78888f6cd0cSJohannes Stezenbach err = do_mknod(name, mode, MAJOR(dev), MINOR(dev)); 7891da177e4SLinus Torvalds if (err) 7901da177e4SLinus Torvalds goto out_free; 7911da177e4SLinus Torvalds 7921da177e4SLinus Torvalds err = read_name(inode, name); 7931da177e4SLinus Torvalds kfree(name); 7941da177e4SLinus Torvalds if (err) 7951da177e4SLinus Torvalds goto out_put; 7961da177e4SLinus Torvalds 7971da177e4SLinus Torvalds d_instantiate(dentry, inode); 798f1adc05eSJeff Dike return 0; 7991da177e4SLinus Torvalds 8001da177e4SLinus Torvalds out_free: 8011da177e4SLinus Torvalds kfree(name); 8021da177e4SLinus Torvalds out_put: 8031da177e4SLinus Torvalds iput(inode); 8041da177e4SLinus Torvalds out: 805f1adc05eSJeff Dike return err; 8061da177e4SLinus Torvalds } 8071da177e4SLinus Torvalds 8081da177e4SLinus Torvalds int hostfs_rename(struct inode *from_ino, struct dentry *from, 8091da177e4SLinus Torvalds struct inode *to_ino, struct dentry *to) 8101da177e4SLinus Torvalds { 8111da177e4SLinus Torvalds char *from_name, *to_name; 8121da177e4SLinus Torvalds int err; 8131da177e4SLinus Torvalds 8141da177e4SLinus Torvalds if ((from_name = inode_dentry_name(from_ino, from)) == NULL) 815f1adc05eSJeff Dike return -ENOMEM; 8161da177e4SLinus Torvalds if ((to_name = inode_dentry_name(to_ino, to)) == NULL) { 8171da177e4SLinus Torvalds kfree(from_name); 818f1adc05eSJeff Dike return -ENOMEM; 8191da177e4SLinus Torvalds } 8201da177e4SLinus Torvalds err = rename_file(from_name, to_name); 8211da177e4SLinus Torvalds kfree(from_name); 8221da177e4SLinus Torvalds kfree(to_name); 823f1adc05eSJeff Dike return err; 8241da177e4SLinus Torvalds } 8251da177e4SLinus Torvalds 826e6305c43SAl Viro int hostfs_permission(struct inode *ino, int desired) 8271da177e4SLinus Torvalds { 8281da177e4SLinus Torvalds char *name; 8291da177e4SLinus Torvalds int r = 0, w = 0, x = 0, err; 8301da177e4SLinus Torvalds 8311da177e4SLinus Torvalds if (desired & MAY_READ) r = 1; 8321da177e4SLinus Torvalds if (desired & MAY_WRITE) w = 1; 8331da177e4SLinus Torvalds if (desired & MAY_EXEC) x = 1; 8341da177e4SLinus Torvalds name = inode_name(ino, 0); 835f1adc05eSJeff Dike if (name == NULL) 836f1adc05eSJeff Dike return -ENOMEM; 8371da177e4SLinus Torvalds 8381da177e4SLinus Torvalds if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) || 8391da177e4SLinus Torvalds S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode)) 8401da177e4SLinus Torvalds err = 0; 8411da177e4SLinus Torvalds else 8421da177e4SLinus Torvalds err = access_file(name, r, w, x); 8431da177e4SLinus Torvalds kfree(name); 8441da177e4SLinus Torvalds if (!err) 8451da177e4SLinus Torvalds err = generic_permission(ino, desired, NULL); 8461da177e4SLinus Torvalds return err; 8471da177e4SLinus Torvalds } 8481da177e4SLinus Torvalds 8491da177e4SLinus Torvalds int hostfs_setattr(struct dentry *dentry, struct iattr *attr) 8501da177e4SLinus Torvalds { 8511da177e4SLinus Torvalds struct hostfs_iattr attrs; 8521da177e4SLinus Torvalds char *name; 8531da177e4SLinus Torvalds int err; 8541da177e4SLinus Torvalds 8555822b7faSAlberto Bertogli int fd = HOSTFS_I(dentry->d_inode)->fd; 8565822b7faSAlberto Bertogli 8571da177e4SLinus Torvalds err = inode_change_ok(dentry->d_inode, attr); 8581da177e4SLinus Torvalds if (err) 8591da177e4SLinus Torvalds return err; 8601da177e4SLinus Torvalds 8611da177e4SLinus Torvalds if (append) 8621da177e4SLinus Torvalds attr->ia_valid &= ~ATTR_SIZE; 8631da177e4SLinus Torvalds 8641da177e4SLinus Torvalds attrs.ia_valid = 0; 8651da177e4SLinus Torvalds if (attr->ia_valid & ATTR_MODE) { 8661da177e4SLinus Torvalds attrs.ia_valid |= HOSTFS_ATTR_MODE; 8671da177e4SLinus Torvalds attrs.ia_mode = attr->ia_mode; 8681da177e4SLinus Torvalds } 8691da177e4SLinus Torvalds if (attr->ia_valid & ATTR_UID) { 8701da177e4SLinus Torvalds attrs.ia_valid |= HOSTFS_ATTR_UID; 8711da177e4SLinus Torvalds attrs.ia_uid = attr->ia_uid; 8721da177e4SLinus Torvalds } 8731da177e4SLinus Torvalds if (attr->ia_valid & ATTR_GID) { 8741da177e4SLinus Torvalds attrs.ia_valid |= HOSTFS_ATTR_GID; 8751da177e4SLinus Torvalds attrs.ia_gid = attr->ia_gid; 8761da177e4SLinus Torvalds } 8771da177e4SLinus Torvalds if (attr->ia_valid & ATTR_SIZE) { 8781da177e4SLinus Torvalds attrs.ia_valid |= HOSTFS_ATTR_SIZE; 8791da177e4SLinus Torvalds attrs.ia_size = attr->ia_size; 8801da177e4SLinus Torvalds } 8811da177e4SLinus Torvalds if (attr->ia_valid & ATTR_ATIME) { 8821da177e4SLinus Torvalds attrs.ia_valid |= HOSTFS_ATTR_ATIME; 8831da177e4SLinus Torvalds attrs.ia_atime = attr->ia_atime; 8841da177e4SLinus Torvalds } 8851da177e4SLinus Torvalds if (attr->ia_valid & ATTR_MTIME) { 8861da177e4SLinus Torvalds attrs.ia_valid |= HOSTFS_ATTR_MTIME; 8871da177e4SLinus Torvalds attrs.ia_mtime = attr->ia_mtime; 8881da177e4SLinus Torvalds } 8891da177e4SLinus Torvalds if (attr->ia_valid & ATTR_CTIME) { 8901da177e4SLinus Torvalds attrs.ia_valid |= HOSTFS_ATTR_CTIME; 8911da177e4SLinus Torvalds attrs.ia_ctime = attr->ia_ctime; 8921da177e4SLinus Torvalds } 8931da177e4SLinus Torvalds if (attr->ia_valid & ATTR_ATIME_SET) { 8941da177e4SLinus Torvalds attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET; 8951da177e4SLinus Torvalds } 8961da177e4SLinus Torvalds if (attr->ia_valid & ATTR_MTIME_SET) { 8971da177e4SLinus Torvalds attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET; 8981da177e4SLinus Torvalds } 8991da177e4SLinus Torvalds name = dentry_name(dentry, 0); 900f1adc05eSJeff Dike if (name == NULL) 901f1adc05eSJeff Dike return -ENOMEM; 9025822b7faSAlberto Bertogli err = set_attr(name, &attrs, fd); 9031da177e4SLinus Torvalds kfree(name); 9041da177e4SLinus Torvalds if (err) 905f1adc05eSJeff Dike return err; 9061da177e4SLinus Torvalds 907f1adc05eSJeff Dike return inode_setattr(dentry->d_inode, attr); 9081da177e4SLinus Torvalds } 9091da177e4SLinus Torvalds 91092e1d5beSArjan van de Ven static const struct inode_operations hostfs_iops = { 9111da177e4SLinus Torvalds .create = hostfs_create, 9121da177e4SLinus Torvalds .link = hostfs_link, 9131da177e4SLinus Torvalds .unlink = hostfs_unlink, 9141da177e4SLinus Torvalds .symlink = hostfs_symlink, 9151da177e4SLinus Torvalds .mkdir = hostfs_mkdir, 9161da177e4SLinus Torvalds .rmdir = hostfs_rmdir, 9171da177e4SLinus Torvalds .mknod = hostfs_mknod, 9181da177e4SLinus Torvalds .rename = hostfs_rename, 9191da177e4SLinus Torvalds .permission = hostfs_permission, 9201da177e4SLinus Torvalds .setattr = hostfs_setattr, 9211da177e4SLinus Torvalds }; 9221da177e4SLinus Torvalds 92392e1d5beSArjan van de Ven static const struct inode_operations hostfs_dir_iops = { 9241da177e4SLinus Torvalds .create = hostfs_create, 9251da177e4SLinus Torvalds .lookup = hostfs_lookup, 9261da177e4SLinus Torvalds .link = hostfs_link, 9271da177e4SLinus Torvalds .unlink = hostfs_unlink, 9281da177e4SLinus Torvalds .symlink = hostfs_symlink, 9291da177e4SLinus Torvalds .mkdir = hostfs_mkdir, 9301da177e4SLinus Torvalds .rmdir = hostfs_rmdir, 9311da177e4SLinus Torvalds .mknod = hostfs_mknod, 9321da177e4SLinus Torvalds .rename = hostfs_rename, 9331da177e4SLinus Torvalds .permission = hostfs_permission, 9341da177e4SLinus Torvalds .setattr = hostfs_setattr, 9351da177e4SLinus Torvalds }; 9361da177e4SLinus Torvalds 9371da177e4SLinus Torvalds int hostfs_link_readpage(struct file *file, struct page *page) 9381da177e4SLinus Torvalds { 9391da177e4SLinus Torvalds char *buffer, *name; 9401da177e4SLinus Torvalds int err; 9411da177e4SLinus Torvalds 9421da177e4SLinus Torvalds buffer = kmap(page); 9431da177e4SLinus Torvalds name = inode_name(page->mapping->host, 0); 944f1adc05eSJeff Dike if (name == NULL) 945f1adc05eSJeff Dike return -ENOMEM; 946ea7e743eSWANG Cong err = hostfs_do_readlink(name, buffer, PAGE_CACHE_SIZE); 9471da177e4SLinus Torvalds kfree(name); 9481da177e4SLinus Torvalds if (err == PAGE_CACHE_SIZE) 9491da177e4SLinus Torvalds err = -E2BIG; 9501da177e4SLinus Torvalds else if (err > 0) { 9511da177e4SLinus Torvalds flush_dcache_page(page); 9521da177e4SLinus Torvalds SetPageUptodate(page); 9531da177e4SLinus Torvalds if (PageError(page)) ClearPageError(page); 9541da177e4SLinus Torvalds err = 0; 9551da177e4SLinus Torvalds } 9561da177e4SLinus Torvalds kunmap(page); 9571da177e4SLinus Torvalds unlock_page(page); 958f1adc05eSJeff Dike return err; 9591da177e4SLinus Torvalds } 9601da177e4SLinus Torvalds 961f5e54d6eSChristoph Hellwig static const struct address_space_operations hostfs_link_aops = { 9621da177e4SLinus Torvalds .readpage = hostfs_link_readpage, 9631da177e4SLinus Torvalds }; 9641da177e4SLinus Torvalds 9651da177e4SLinus Torvalds static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent) 9661da177e4SLinus Torvalds { 9671da177e4SLinus Torvalds struct inode *root_inode; 96875e8defbSPaolo 'Blaisorblade' Giarrusso char *host_root_path, *req_root = d; 9691da177e4SLinus Torvalds int err; 9701da177e4SLinus Torvalds 9711da177e4SLinus Torvalds sb->s_blocksize = 1024; 9721da177e4SLinus Torvalds sb->s_blocksize_bits = 10; 9731da177e4SLinus Torvalds sb->s_magic = HOSTFS_SUPER_MAGIC; 9741da177e4SLinus Torvalds sb->s_op = &hostfs_sbops; 9751da177e4SLinus Torvalds 976a6eb0be6SPaolo 'Blaisorblade' Giarrusso /* NULL is printed as <NULL> by sprintf: avoid that. */ 97775e8defbSPaolo 'Blaisorblade' Giarrusso if (req_root == NULL) 97875e8defbSPaolo 'Blaisorblade' Giarrusso req_root = ""; 9791da177e4SLinus Torvalds 9801da177e4SLinus Torvalds err = -ENOMEM; 98175e8defbSPaolo 'Blaisorblade' Giarrusso host_root_path = kmalloc(strlen(root_ino) + 1 98275e8defbSPaolo 'Blaisorblade' Giarrusso + strlen(req_root) + 1, GFP_KERNEL); 98375e8defbSPaolo 'Blaisorblade' Giarrusso if (host_root_path == NULL) 9841da177e4SLinus Torvalds goto out; 9851da177e4SLinus Torvalds 98675e8defbSPaolo 'Blaisorblade' Giarrusso sprintf(host_root_path, "%s/%s", root_ino, req_root); 9871da177e4SLinus Torvalds 9880a370e5dSDavid Howells root_inode = hostfs_iget(sb); 9890a370e5dSDavid Howells if (IS_ERR(root_inode)) { 9900a370e5dSDavid Howells err = PTR_ERR(root_inode); 9911da177e4SLinus Torvalds goto out_free; 9920a370e5dSDavid Howells } 9931da177e4SLinus Torvalds 9941da177e4SLinus Torvalds err = init_inode(root_inode, NULL); 9951da177e4SLinus Torvalds if (err) 9961da177e4SLinus Torvalds goto out_put; 9971da177e4SLinus Torvalds 99875e8defbSPaolo 'Blaisorblade' Giarrusso HOSTFS_I(root_inode)->host_filename = host_root_path; 99984b3db04SJeff Dike /* 100084b3db04SJeff Dike * Avoid that in the error path, iput(root_inode) frees again 100184b3db04SJeff Dike * host_root_path through hostfs_destroy_inode! 100284b3db04SJeff Dike */ 100375e8defbSPaolo 'Blaisorblade' Giarrusso host_root_path = NULL; 10041da177e4SLinus Torvalds 10051da177e4SLinus Torvalds err = -ENOMEM; 10061da177e4SLinus Torvalds sb->s_root = d_alloc_root(root_inode); 10071da177e4SLinus Torvalds if (sb->s_root == NULL) 10081da177e4SLinus Torvalds goto out_put; 10091da177e4SLinus Torvalds 10100a370e5dSDavid Howells err = hostfs_read_inode(root_inode); 101151a14110SJeff Dike if (err) { 101251a14110SJeff Dike /* No iput in this case because the dput does that for us */ 101351a14110SJeff Dike dput(sb->s_root); 101451a14110SJeff Dike sb->s_root = NULL; 1015bca27113SPaolo 'Blaisorblade' Giarrusso goto out; 101651a14110SJeff Dike } 10171da177e4SLinus Torvalds 1018f1adc05eSJeff Dike return 0; 10191da177e4SLinus Torvalds 10201da177e4SLinus Torvalds out_put: 10211da177e4SLinus Torvalds iput(root_inode); 10221da177e4SLinus Torvalds out_free: 102375e8defbSPaolo 'Blaisorblade' Giarrusso kfree(host_root_path); 10241da177e4SLinus Torvalds out: 1025f1adc05eSJeff Dike return err; 10261da177e4SLinus Torvalds } 10271da177e4SLinus Torvalds 1028454e2398SDavid Howells static int hostfs_read_sb(struct file_system_type *type, 10291da177e4SLinus Torvalds int flags, const char *dev_name, 1030454e2398SDavid Howells void *data, struct vfsmount *mnt) 10311da177e4SLinus Torvalds { 1032454e2398SDavid Howells return get_sb_nodev(type, flags, data, hostfs_fill_sb_common, mnt); 10331da177e4SLinus Torvalds } 10341da177e4SLinus Torvalds 10351da177e4SLinus Torvalds static struct file_system_type hostfs_type = { 10361da177e4SLinus Torvalds .owner = THIS_MODULE, 10371da177e4SLinus Torvalds .name = "hostfs", 10381da177e4SLinus Torvalds .get_sb = hostfs_read_sb, 10391da177e4SLinus Torvalds .kill_sb = kill_anon_super, 10401da177e4SLinus Torvalds .fs_flags = 0, 10411da177e4SLinus Torvalds }; 10421da177e4SLinus Torvalds 10431da177e4SLinus Torvalds static int __init init_hostfs(void) 10441da177e4SLinus Torvalds { 1045f1adc05eSJeff Dike return register_filesystem(&hostfs_type); 10461da177e4SLinus Torvalds } 10471da177e4SLinus Torvalds 10481da177e4SLinus Torvalds static void __exit exit_hostfs(void) 10491da177e4SLinus Torvalds { 10501da177e4SLinus Torvalds unregister_filesystem(&hostfs_type); 10511da177e4SLinus Torvalds } 10521da177e4SLinus Torvalds 10531da177e4SLinus Torvalds module_init(init_hostfs) 10541da177e4SLinus Torvalds module_exit(exit_hostfs) 10551da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 1056