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> 145a0e3ad6STejun Heo #include <linux/slab.h> 15dd2cc4dfSMiklos Szeredi #include <linux/seq_file.h> 166966a977SJiri Kosina #include <linux/mount.h> 17d0352d3eSAl Viro #include <linux/namei.h> 181da177e4SLinus Torvalds #include "hostfs.h" 191da177e4SLinus Torvalds #include "init.h" 2084b3db04SJeff Dike #include "kern.h" 211da177e4SLinus Torvalds 221da177e4SLinus Torvalds struct hostfs_inode_info { 231da177e4SLinus Torvalds int fd; 24aeb5d727SAl Viro fmode_t mode; 251da177e4SLinus Torvalds struct inode vfs_inode; 261da177e4SLinus Torvalds }; 271da177e4SLinus Torvalds 281da177e4SLinus Torvalds static inline struct hostfs_inode_info *HOSTFS_I(struct inode *inode) 291da177e4SLinus Torvalds { 30f1adc05eSJeff Dike return list_entry(inode, struct hostfs_inode_info, vfs_inode); 311da177e4SLinus Torvalds } 321da177e4SLinus Torvalds 33680b0da9SJosef Sipek #define FILE_HOSTFS_I(file) HOSTFS_I((file)->f_path.dentry->d_inode) 341da177e4SLinus Torvalds 35fe15ce44SNick Piggin static int hostfs_d_delete(const struct dentry *dentry) 361da177e4SLinus Torvalds { 37f1adc05eSJeff Dike return 1; 381da177e4SLinus Torvalds } 391da177e4SLinus Torvalds 40e16404edSAl Viro static const struct dentry_operations hostfs_dentry_ops = { 411da177e4SLinus Torvalds .d_delete = hostfs_d_delete, 421da177e4SLinus Torvalds }; 431da177e4SLinus Torvalds 441da177e4SLinus Torvalds /* Changed in hostfs_args before the kernel starts running */ 45a6eb0be6SPaolo 'Blaisorblade' Giarrusso static char *root_ino = ""; 461da177e4SLinus Torvalds static int append = 0; 471da177e4SLinus Torvalds 481da177e4SLinus Torvalds #define HOSTFS_SUPER_MAGIC 0x00c0ffee 491da177e4SLinus Torvalds 5092e1d5beSArjan van de Ven static const struct inode_operations hostfs_iops; 5192e1d5beSArjan van de Ven static const struct inode_operations hostfs_dir_iops; 52d0352d3eSAl Viro static const struct inode_operations hostfs_link_iops; 531da177e4SLinus Torvalds 541da177e4SLinus Torvalds #ifndef MODULE 551da177e4SLinus Torvalds static int __init hostfs_args(char *options, int *add) 561da177e4SLinus Torvalds { 571da177e4SLinus Torvalds char *ptr; 581da177e4SLinus Torvalds 591da177e4SLinus Torvalds ptr = strchr(options, ','); 601da177e4SLinus Torvalds if (ptr != NULL) 611da177e4SLinus Torvalds *ptr++ = '\0'; 621da177e4SLinus Torvalds if (*options != '\0') 631da177e4SLinus Torvalds root_ino = options; 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds options = ptr; 661da177e4SLinus Torvalds while (options) { 671da177e4SLinus Torvalds ptr = strchr(options, ','); 681da177e4SLinus Torvalds if (ptr != NULL) 691da177e4SLinus Torvalds *ptr++ = '\0'; 701da177e4SLinus Torvalds if (*options != '\0') { 711da177e4SLinus Torvalds if (!strcmp(options, "append")) 721da177e4SLinus Torvalds append = 1; 731da177e4SLinus Torvalds else printf("hostfs_args - unsupported option - %s\n", 741da177e4SLinus Torvalds options); 751da177e4SLinus Torvalds } 761da177e4SLinus Torvalds options = ptr; 771da177e4SLinus Torvalds } 78f1adc05eSJeff Dike return 0; 791da177e4SLinus Torvalds } 801da177e4SLinus Torvalds 811da177e4SLinus Torvalds __uml_setup("hostfs=", hostfs_args, 821da177e4SLinus Torvalds "hostfs=<root dir>,<flags>,...\n" 831da177e4SLinus Torvalds " This is used to set hostfs parameters. The root directory argument\n" 841da177e4SLinus Torvalds " is used to confine all hostfs mounts to within the specified directory\n" 851da177e4SLinus Torvalds " tree on the host. If this isn't specified, then a user inside UML can\n" 861da177e4SLinus Torvalds " mount anything on the host that's accessible to the user that's running\n" 871da177e4SLinus Torvalds " it.\n" 881da177e4SLinus Torvalds " The only flag currently supported is 'append', which specifies that all\n" 891da177e4SLinus Torvalds " files opened by hostfs will be opened in append mode.\n\n" 901da177e4SLinus Torvalds ); 911da177e4SLinus Torvalds #endif 921da177e4SLinus Torvalds 93e9193059SAl Viro static char *__dentry_name(struct dentry *dentry, char *name) 94e9193059SAl Viro { 95ec2447c2SNick Piggin char *p = dentry_path_raw(dentry, name, PATH_MAX); 96e9193059SAl Viro char *root; 97e9193059SAl Viro size_t len; 98e9193059SAl Viro 99e9193059SAl Viro root = dentry->d_sb->s_fs_info; 100e9193059SAl Viro len = strlen(root); 101e9193059SAl Viro if (IS_ERR(p)) { 102e9193059SAl Viro __putname(name); 103e9193059SAl Viro return NULL; 104e9193059SAl Viro } 105850a496fSAl Viro strlcpy(name, root, PATH_MAX); 106e9193059SAl Viro if (len > p - name) { 107e9193059SAl Viro __putname(name); 108e9193059SAl Viro return NULL; 109e9193059SAl Viro } 110e9193059SAl Viro if (p > name + len) { 111e9193059SAl Viro char *s = name + len; 112e9193059SAl Viro while ((*s++ = *p++) != '\0') 113e9193059SAl Viro ; 114e9193059SAl Viro } 115e9193059SAl Viro return name; 116e9193059SAl Viro } 117e9193059SAl Viro 118c5322220SAl Viro static char *dentry_name(struct dentry *dentry) 1191da177e4SLinus Torvalds { 120e9193059SAl Viro char *name = __getname(); 121e9193059SAl Viro if (!name) 122f1adc05eSJeff Dike return NULL; 1231da177e4SLinus Torvalds 124e9193059SAl Viro return __dentry_name(dentry, name); /* will unlock */ 1251da177e4SLinus Torvalds } 1261da177e4SLinus Torvalds 127c5322220SAl Viro static char *inode_name(struct inode *ino) 1281da177e4SLinus Torvalds { 1291da177e4SLinus Torvalds struct dentry *dentry; 130ec2447c2SNick Piggin char *name; 131ec2447c2SNick Piggin 132ec2447c2SNick Piggin dentry = d_find_alias(ino); 133ec2447c2SNick Piggin if (!dentry) 134e9193059SAl Viro return NULL; 1351da177e4SLinus Torvalds 136ec2447c2SNick Piggin name = dentry_name(dentry); 137ec2447c2SNick Piggin 138ec2447c2SNick Piggin dput(dentry); 139ec2447c2SNick Piggin 140ec2447c2SNick Piggin return name; 1411da177e4SLinus Torvalds } 1421da177e4SLinus Torvalds 1431da177e4SLinus Torvalds static char *follow_link(char *link) 1441da177e4SLinus Torvalds { 1451da177e4SLinus Torvalds int len, n; 1461da177e4SLinus Torvalds char *name, *resolved, *end; 1471da177e4SLinus Torvalds 1481da177e4SLinus Torvalds len = 64; 1491da177e4SLinus Torvalds while (1) { 1501da177e4SLinus Torvalds n = -ENOMEM; 1511da177e4SLinus Torvalds name = kmalloc(len, GFP_KERNEL); 1521da177e4SLinus Torvalds if (name == NULL) 1531da177e4SLinus Torvalds goto out; 1541da177e4SLinus Torvalds 155ea7e743eSWANG Cong n = hostfs_do_readlink(link, name, len); 1561da177e4SLinus Torvalds if (n < len) 1571da177e4SLinus Torvalds break; 1581da177e4SLinus Torvalds len *= 2; 1591da177e4SLinus Torvalds kfree(name); 1601da177e4SLinus Torvalds } 1611da177e4SLinus Torvalds if (n < 0) 1621da177e4SLinus Torvalds goto out_free; 1631da177e4SLinus Torvalds 1641da177e4SLinus Torvalds if (*name == '/') 165f1adc05eSJeff Dike return name; 1661da177e4SLinus Torvalds 1671da177e4SLinus Torvalds end = strrchr(link, '/'); 1681da177e4SLinus Torvalds if (end == NULL) 169f1adc05eSJeff Dike return name; 1701da177e4SLinus Torvalds 1711da177e4SLinus Torvalds *(end + 1) = '\0'; 1721da177e4SLinus Torvalds len = strlen(link) + strlen(name) + 1; 1731da177e4SLinus Torvalds 1741da177e4SLinus Torvalds resolved = kmalloc(len, GFP_KERNEL); 1751da177e4SLinus Torvalds if (resolved == NULL) { 1761da177e4SLinus Torvalds n = -ENOMEM; 1771da177e4SLinus Torvalds goto out_free; 1781da177e4SLinus Torvalds } 1791da177e4SLinus Torvalds 1801da177e4SLinus Torvalds sprintf(resolved, "%s%s", link, name); 1811da177e4SLinus Torvalds kfree(name); 1821da177e4SLinus Torvalds kfree(link); 183f1adc05eSJeff Dike return resolved; 1841da177e4SLinus Torvalds 1851da177e4SLinus Torvalds out_free: 1861da177e4SLinus Torvalds kfree(name); 1871da177e4SLinus Torvalds out: 188f1adc05eSJeff Dike return ERR_PTR(n); 1891da177e4SLinus Torvalds } 1901da177e4SLinus Torvalds 1910a370e5dSDavid Howells static struct inode *hostfs_iget(struct super_block *sb) 1920a370e5dSDavid Howells { 19352b209f7SAl Viro struct inode *inode = new_inode(sb); 1940a370e5dSDavid Howells if (!inode) 1950a370e5dSDavid Howells return ERR_PTR(-ENOMEM); 1960a370e5dSDavid Howells return inode; 1970a370e5dSDavid Howells } 1980a370e5dSDavid Howells 199726c3342SDavid Howells int hostfs_statfs(struct dentry *dentry, struct kstatfs *sf) 2001da177e4SLinus Torvalds { 20184b3db04SJeff Dike /* 20284b3db04SJeff Dike * do_statfs uses struct statfs64 internally, but the linux kernel 2031da177e4SLinus Torvalds * struct statfs still has 32-bit versions for most of these fields, 2041da177e4SLinus Torvalds * so we convert them here 2051da177e4SLinus Torvalds */ 2061da177e4SLinus Torvalds int err; 2071da177e4SLinus Torvalds long long f_blocks; 2081da177e4SLinus Torvalds long long f_bfree; 2091da177e4SLinus Torvalds long long f_bavail; 2101da177e4SLinus Torvalds long long f_files; 2111da177e4SLinus Torvalds long long f_ffree; 2121da177e4SLinus Torvalds 213601d2c38SAl Viro err = do_statfs(dentry->d_sb->s_fs_info, 2141da177e4SLinus Torvalds &sf->f_bsize, &f_blocks, &f_bfree, &f_bavail, &f_files, 2151da177e4SLinus Torvalds &f_ffree, &sf->f_fsid, sizeof(sf->f_fsid), 2161b627d57SRichard Weinberger &sf->f_namelen); 217f1adc05eSJeff Dike if (err) 218f1adc05eSJeff Dike return err; 2191da177e4SLinus Torvalds sf->f_blocks = f_blocks; 2201da177e4SLinus Torvalds sf->f_bfree = f_bfree; 2211da177e4SLinus Torvalds sf->f_bavail = f_bavail; 2221da177e4SLinus Torvalds sf->f_files = f_files; 2231da177e4SLinus Torvalds sf->f_ffree = f_ffree; 2241da177e4SLinus Torvalds sf->f_type = HOSTFS_SUPER_MAGIC; 225f1adc05eSJeff Dike return 0; 2261da177e4SLinus Torvalds } 2271da177e4SLinus Torvalds 2281da177e4SLinus Torvalds static struct inode *hostfs_alloc_inode(struct super_block *sb) 2291da177e4SLinus Torvalds { 2301da177e4SLinus Torvalds struct hostfs_inode_info *hi; 2311da177e4SLinus Torvalds 232601d2c38SAl Viro hi = kzalloc(sizeof(*hi), GFP_KERNEL); 2331da177e4SLinus Torvalds if (hi == NULL) 234f1adc05eSJeff Dike return NULL; 235601d2c38SAl Viro hi->fd = -1; 2361da177e4SLinus Torvalds inode_init_once(&hi->vfs_inode); 237f1adc05eSJeff Dike return &hi->vfs_inode; 2381da177e4SLinus Torvalds } 2391da177e4SLinus Torvalds 240e971a6d7SAl Viro static void hostfs_evict_inode(struct inode *inode) 2411da177e4SLinus Torvalds { 242fef26658SMark Fasheh truncate_inode_pages(&inode->i_data, 0); 243e971a6d7SAl Viro end_writeback(inode); 2441da177e4SLinus Torvalds if (HOSTFS_I(inode)->fd != -1) { 2451da177e4SLinus Torvalds close_file(&HOSTFS_I(inode)->fd); 2461da177e4SLinus Torvalds HOSTFS_I(inode)->fd = -1; 2471da177e4SLinus Torvalds } 2481da177e4SLinus Torvalds } 2491da177e4SLinus Torvalds 250fa0d7e3dSNick Piggin static void hostfs_i_callback(struct rcu_head *head) 251fa0d7e3dSNick Piggin { 252fa0d7e3dSNick Piggin struct inode *inode = container_of(head, struct inode, i_rcu); 253fa0d7e3dSNick Piggin kfree(HOSTFS_I(inode)); 254fa0d7e3dSNick Piggin } 255fa0d7e3dSNick Piggin 2561da177e4SLinus Torvalds static void hostfs_destroy_inode(struct inode *inode) 2571da177e4SLinus Torvalds { 258fa0d7e3dSNick Piggin call_rcu(&inode->i_rcu, hostfs_i_callback); 2591da177e4SLinus Torvalds } 2601da177e4SLinus Torvalds 261dd2cc4dfSMiklos Szeredi static int hostfs_show_options(struct seq_file *seq, struct vfsmount *vfs) 262dd2cc4dfSMiklos Szeredi { 263601d2c38SAl Viro const char *root_path = vfs->mnt_sb->s_fs_info; 264dd2cc4dfSMiklos Szeredi size_t offset = strlen(root_ino) + 1; 265dd2cc4dfSMiklos Szeredi 266dd2cc4dfSMiklos Szeredi if (strlen(root_path) > offset) 267dd2cc4dfSMiklos Szeredi seq_printf(seq, ",%s", root_path + offset); 268dd2cc4dfSMiklos Szeredi 269dd2cc4dfSMiklos Szeredi return 0; 270dd2cc4dfSMiklos Szeredi } 271dd2cc4dfSMiklos Szeredi 272ee9b6d61SJosef 'Jeff' Sipek static const struct super_operations hostfs_sbops = { 2731da177e4SLinus Torvalds .alloc_inode = hostfs_alloc_inode, 2741da177e4SLinus Torvalds .destroy_inode = hostfs_destroy_inode, 275e971a6d7SAl Viro .evict_inode = hostfs_evict_inode, 2761da177e4SLinus Torvalds .statfs = hostfs_statfs, 277dd2cc4dfSMiklos Szeredi .show_options = hostfs_show_options, 2781da177e4SLinus Torvalds }; 2791da177e4SLinus Torvalds 2801da177e4SLinus Torvalds int hostfs_readdir(struct file *file, void *ent, filldir_t filldir) 2811da177e4SLinus Torvalds { 2821da177e4SLinus Torvalds void *dir; 2831da177e4SLinus Torvalds char *name; 2841da177e4SLinus Torvalds unsigned long long next, ino; 2851da177e4SLinus Torvalds int error, len; 2861da177e4SLinus Torvalds 287c5322220SAl Viro name = dentry_name(file->f_path.dentry); 288f1adc05eSJeff Dike if (name == NULL) 289f1adc05eSJeff Dike return -ENOMEM; 2901da177e4SLinus Torvalds dir = open_dir(name, &error); 291e9193059SAl Viro __putname(name); 292f1adc05eSJeff Dike if (dir == NULL) 293f1adc05eSJeff Dike return -error; 2941da177e4SLinus Torvalds next = file->f_pos; 2951da177e4SLinus Torvalds while ((name = read_dir(dir, &next, &ino, &len)) != NULL) { 2961da177e4SLinus Torvalds error = (*filldir)(ent, name, len, file->f_pos, 2971da177e4SLinus Torvalds ino, DT_UNKNOWN); 2981da177e4SLinus Torvalds if (error) break; 2991da177e4SLinus Torvalds file->f_pos = next; 3001da177e4SLinus Torvalds } 3011da177e4SLinus Torvalds close_dir(dir); 302f1adc05eSJeff Dike return 0; 3031da177e4SLinus Torvalds } 3041da177e4SLinus Torvalds 3051da177e4SLinus Torvalds int hostfs_file_open(struct inode *ino, struct file *file) 3061da177e4SLinus Torvalds { 307f8ad850fSAl Viro static DEFINE_MUTEX(open_mutex); 3081da177e4SLinus Torvalds char *name; 309aeb5d727SAl Viro fmode_t mode = 0; 310f8ad850fSAl Viro int err; 311aeb5d727SAl Viro int r = 0, w = 0, fd; 3121da177e4SLinus Torvalds 3131da177e4SLinus Torvalds mode = file->f_mode & (FMODE_READ | FMODE_WRITE); 3141da177e4SLinus Torvalds if ((mode & HOSTFS_I(ino)->mode) == mode) 315f1adc05eSJeff Dike return 0; 3161da177e4SLinus Torvalds 317f8ad850fSAl Viro mode |= HOSTFS_I(ino)->mode; 3181da177e4SLinus Torvalds 319f8ad850fSAl Viro retry: 320f8ad850fSAl Viro if (mode & FMODE_READ) 3211da177e4SLinus Torvalds r = 1; 322f8ad850fSAl Viro if (mode & FMODE_WRITE) 3231da177e4SLinus Torvalds w = 1; 3241da177e4SLinus Torvalds if (w) 3251da177e4SLinus Torvalds r = 1; 3261da177e4SLinus Torvalds 327c5322220SAl Viro name = dentry_name(file->f_path.dentry); 3281da177e4SLinus Torvalds if (name == NULL) 329f1adc05eSJeff Dike return -ENOMEM; 3301da177e4SLinus Torvalds 3311da177e4SLinus Torvalds fd = open_file(name, r, w, append); 332e9193059SAl Viro __putname(name); 333f1adc05eSJeff Dike if (fd < 0) 334f1adc05eSJeff Dike return fd; 335f8ad850fSAl Viro 336f8ad850fSAl Viro mutex_lock(&open_mutex); 337f8ad850fSAl Viro /* somebody else had handled it first? */ 338f8ad850fSAl Viro if ((mode & HOSTFS_I(ino)->mode) == mode) { 339f8ad850fSAl Viro mutex_unlock(&open_mutex); 340f8ad850fSAl Viro return 0; 341f8ad850fSAl Viro } 342f8ad850fSAl Viro if ((mode | HOSTFS_I(ino)->mode) != mode) { 343f8ad850fSAl Viro mode |= HOSTFS_I(ino)->mode; 344f8ad850fSAl Viro mutex_unlock(&open_mutex); 345f8ad850fSAl Viro close_file(&fd); 346f8ad850fSAl Viro goto retry; 347f8ad850fSAl Viro } 348f8ad850fSAl Viro if (HOSTFS_I(ino)->fd == -1) { 349f8ad850fSAl Viro HOSTFS_I(ino)->fd = fd; 350f8ad850fSAl Viro } else { 351f8ad850fSAl Viro err = replace_file(fd, HOSTFS_I(ino)->fd); 352f8ad850fSAl Viro close_file(&fd); 353f8ad850fSAl Viro if (err < 0) { 354f8ad850fSAl Viro mutex_unlock(&open_mutex); 355f8ad850fSAl Viro return err; 356f8ad850fSAl Viro } 357f8ad850fSAl Viro } 358f8ad850fSAl Viro HOSTFS_I(ino)->mode = mode; 359f8ad850fSAl Viro mutex_unlock(&open_mutex); 3601da177e4SLinus Torvalds 361f1adc05eSJeff Dike return 0; 3621da177e4SLinus Torvalds } 3631da177e4SLinus Torvalds 36402c24a82SJosef Bacik int hostfs_fsync(struct file *file, loff_t start, loff_t end, int datasync) 3651da177e4SLinus Torvalds { 36602c24a82SJosef Bacik struct inode *inode = file->f_mapping->host; 36702c24a82SJosef Bacik int ret; 36802c24a82SJosef Bacik 36902c24a82SJosef Bacik ret = filemap_write_and_wait_range(inode->i_mapping, start, end); 37002c24a82SJosef Bacik if (ret) 37102c24a82SJosef Bacik return ret; 37202c24a82SJosef Bacik 37302c24a82SJosef Bacik mutex_lock(&inode->i_mutex); 37402c24a82SJosef Bacik ret = fsync_file(HOSTFS_I(inode)->fd, datasync); 37502c24a82SJosef Bacik mutex_unlock(&inode->i_mutex); 37602c24a82SJosef Bacik 37702c24a82SJosef Bacik return ret; 3781da177e4SLinus Torvalds } 3791da177e4SLinus Torvalds 3804b6f5d20SArjan van de Ven static const struct file_operations hostfs_file_fops = { 3811da177e4SLinus Torvalds .llseek = generic_file_llseek, 382543ade1fSBadari Pulavarty .read = do_sync_read, 3835ffc4ef4SJens Axboe .splice_read = generic_file_splice_read, 3841da177e4SLinus Torvalds .aio_read = generic_file_aio_read, 3851da177e4SLinus Torvalds .aio_write = generic_file_aio_write, 386543ade1fSBadari Pulavarty .write = do_sync_write, 3871da177e4SLinus Torvalds .mmap = generic_file_mmap, 3881da177e4SLinus Torvalds .open = hostfs_file_open, 3891da177e4SLinus Torvalds .release = NULL, 3901da177e4SLinus Torvalds .fsync = hostfs_fsync, 3911da177e4SLinus Torvalds }; 3921da177e4SLinus Torvalds 3934b6f5d20SArjan van de Ven static const struct file_operations hostfs_dir_fops = { 3941da177e4SLinus Torvalds .llseek = generic_file_llseek, 3951da177e4SLinus Torvalds .readdir = hostfs_readdir, 3961da177e4SLinus Torvalds .read = generic_read_dir, 3971da177e4SLinus Torvalds }; 3981da177e4SLinus Torvalds 3991da177e4SLinus Torvalds int hostfs_writepage(struct page *page, struct writeback_control *wbc) 4001da177e4SLinus Torvalds { 4011da177e4SLinus Torvalds struct address_space *mapping = page->mapping; 4021da177e4SLinus Torvalds struct inode *inode = mapping->host; 4031da177e4SLinus Torvalds char *buffer; 4041da177e4SLinus Torvalds unsigned long long base; 4051da177e4SLinus Torvalds int count = PAGE_CACHE_SIZE; 4061da177e4SLinus Torvalds int end_index = inode->i_size >> PAGE_CACHE_SHIFT; 4071da177e4SLinus Torvalds int err; 4081da177e4SLinus Torvalds 4091da177e4SLinus Torvalds if (page->index >= end_index) 4101da177e4SLinus Torvalds count = inode->i_size & (PAGE_CACHE_SIZE-1); 4111da177e4SLinus Torvalds 4121da177e4SLinus Torvalds buffer = kmap(page); 4131da177e4SLinus Torvalds base = ((unsigned long long) page->index) << PAGE_CACHE_SHIFT; 4141da177e4SLinus Torvalds 4151da177e4SLinus Torvalds err = write_file(HOSTFS_I(inode)->fd, &base, buffer, count); 4161da177e4SLinus Torvalds if (err != count) { 4171da177e4SLinus Torvalds ClearPageUptodate(page); 4181da177e4SLinus Torvalds goto out; 4191da177e4SLinus Torvalds } 4201da177e4SLinus Torvalds 4211da177e4SLinus Torvalds if (base > inode->i_size) 4221da177e4SLinus Torvalds inode->i_size = base; 4231da177e4SLinus Torvalds 4241da177e4SLinus Torvalds if (PageError(page)) 4251da177e4SLinus Torvalds ClearPageError(page); 4261da177e4SLinus Torvalds err = 0; 4271da177e4SLinus Torvalds 4281da177e4SLinus Torvalds out: 4291da177e4SLinus Torvalds kunmap(page); 4301da177e4SLinus Torvalds 4311da177e4SLinus Torvalds unlock_page(page); 4321da177e4SLinus Torvalds return err; 4331da177e4SLinus Torvalds } 4341da177e4SLinus Torvalds 4351da177e4SLinus Torvalds int hostfs_readpage(struct file *file, struct page *page) 4361da177e4SLinus Torvalds { 4371da177e4SLinus Torvalds char *buffer; 4381da177e4SLinus Torvalds long long start; 4391da177e4SLinus Torvalds int err = 0; 4401da177e4SLinus Torvalds 4411da177e4SLinus Torvalds start = (long long) page->index << PAGE_CACHE_SHIFT; 4421da177e4SLinus Torvalds buffer = kmap(page); 4431da177e4SLinus Torvalds err = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer, 4441da177e4SLinus Torvalds PAGE_CACHE_SIZE); 44584b3db04SJeff Dike if (err < 0) 44684b3db04SJeff Dike goto out; 4471da177e4SLinus Torvalds 4481da177e4SLinus Torvalds memset(&buffer[err], 0, PAGE_CACHE_SIZE - err); 4491da177e4SLinus Torvalds 4501da177e4SLinus Torvalds flush_dcache_page(page); 4511da177e4SLinus Torvalds SetPageUptodate(page); 4521da177e4SLinus Torvalds if (PageError(page)) ClearPageError(page); 4531da177e4SLinus Torvalds err = 0; 4541da177e4SLinus Torvalds out: 4551da177e4SLinus Torvalds kunmap(page); 4561da177e4SLinus Torvalds unlock_page(page); 457f1adc05eSJeff Dike return err; 4581da177e4SLinus Torvalds } 4591da177e4SLinus Torvalds 460ae361ff4SNick Piggin int hostfs_write_begin(struct file *file, struct address_space *mapping, 461ae361ff4SNick Piggin loff_t pos, unsigned len, unsigned flags, 462ae361ff4SNick Piggin struct page **pagep, void **fsdata) 4631da177e4SLinus Torvalds { 464ae361ff4SNick Piggin pgoff_t index = pos >> PAGE_CACHE_SHIFT; 465ae361ff4SNick Piggin 46654566b2cSNick Piggin *pagep = grab_cache_page_write_begin(mapping, index, flags); 467ae361ff4SNick Piggin if (!*pagep) 468ae361ff4SNick Piggin return -ENOMEM; 469ae361ff4SNick Piggin return 0; 470ae361ff4SNick Piggin } 471ae361ff4SNick Piggin 472ae361ff4SNick Piggin int hostfs_write_end(struct file *file, struct address_space *mapping, 473ae361ff4SNick Piggin loff_t pos, unsigned len, unsigned copied, 474ae361ff4SNick Piggin struct page *page, void *fsdata) 475ae361ff4SNick Piggin { 476ae361ff4SNick Piggin struct inode *inode = mapping->host; 477ae361ff4SNick Piggin void *buffer; 478ae361ff4SNick Piggin unsigned from = pos & (PAGE_CACHE_SIZE - 1); 4791da177e4SLinus Torvalds int err; 4801da177e4SLinus Torvalds 4811da177e4SLinus Torvalds buffer = kmap(page); 482ae361ff4SNick Piggin err = write_file(FILE_HOSTFS_I(file)->fd, &pos, buffer + from, copied); 4831da177e4SLinus Torvalds kunmap(page); 4841da177e4SLinus Torvalds 485ae361ff4SNick Piggin if (!PageUptodate(page) && err == PAGE_CACHE_SIZE) 486ae361ff4SNick Piggin SetPageUptodate(page); 4871da177e4SLinus Torvalds 48884b3db04SJeff Dike /* 48984b3db04SJeff Dike * If err > 0, write_file has added err to pos, so we are comparing 490ae361ff4SNick Piggin * i_size against the last byte written. 491ae361ff4SNick Piggin */ 492ae361ff4SNick Piggin if (err > 0 && (pos > inode->i_size)) 493ae361ff4SNick Piggin inode->i_size = pos; 494ae361ff4SNick Piggin unlock_page(page); 495ae361ff4SNick Piggin page_cache_release(page); 49630f04a4eSPaolo 'Blaisorblade' Giarrusso 497f1adc05eSJeff Dike return err; 4981da177e4SLinus Torvalds } 4991da177e4SLinus Torvalds 500f5e54d6eSChristoph Hellwig static const struct address_space_operations hostfs_aops = { 5011da177e4SLinus Torvalds .writepage = hostfs_writepage, 5021da177e4SLinus Torvalds .readpage = hostfs_readpage, 503ffa0aea6SPaolo 'Blaisorblade' Giarrusso .set_page_dirty = __set_page_dirty_nobuffers, 504ae361ff4SNick Piggin .write_begin = hostfs_write_begin, 505ae361ff4SNick Piggin .write_end = hostfs_write_end, 5061da177e4SLinus Torvalds }; 5071da177e4SLinus Torvalds 5084754b825SAl Viro static int read_name(struct inode *ino, char *name) 5091da177e4SLinus Torvalds { 5104754b825SAl Viro dev_t rdev; 5114754b825SAl Viro struct hostfs_stat st; 5124754b825SAl Viro int err = stat_file(name, &st, -1); 5134754b825SAl Viro if (err) 5144754b825SAl Viro return err; 5151da177e4SLinus Torvalds 5161da177e4SLinus Torvalds /* Reencode maj and min with the kernel encoding.*/ 5174754b825SAl Viro rdev = MKDEV(st.maj, st.min); 5181da177e4SLinus Torvalds 5194754b825SAl Viro switch (st.mode & S_IFMT) { 5204754b825SAl Viro case S_IFLNK: 521d0352d3eSAl Viro ino->i_op = &hostfs_link_iops; 5224754b825SAl Viro break; 5234754b825SAl Viro case S_IFDIR: 5244754b825SAl Viro ino->i_op = &hostfs_dir_iops; 5254754b825SAl Viro ino->i_fop = &hostfs_dir_fops; 5264754b825SAl Viro break; 5274754b825SAl Viro case S_IFCHR: 5284754b825SAl Viro case S_IFBLK: 5294754b825SAl Viro case S_IFIFO: 5304754b825SAl Viro case S_IFSOCK: 5314754b825SAl Viro init_special_inode(ino, st.mode & S_IFMT, rdev); 5324754b825SAl Viro ino->i_op = &hostfs_iops; 5334754b825SAl Viro break; 5341da177e4SLinus Torvalds 5354754b825SAl Viro default: 5364754b825SAl Viro ino->i_op = &hostfs_iops; 5374754b825SAl Viro ino->i_fop = &hostfs_file_fops; 5384754b825SAl Viro ino->i_mapping->a_ops = &hostfs_aops; 5391da177e4SLinus Torvalds } 5404754b825SAl Viro 5414754b825SAl Viro ino->i_ino = st.ino; 5424754b825SAl Viro ino->i_mode = st.mode; 543bfe86848SMiklos Szeredi set_nlink(ino, st.nlink); 5444754b825SAl Viro ino->i_uid = st.uid; 5454754b825SAl Viro ino->i_gid = st.gid; 5464754b825SAl Viro ino->i_atime = st.atime; 5474754b825SAl Viro ino->i_mtime = st.mtime; 5484754b825SAl Viro ino->i_ctime = st.ctime; 5494754b825SAl Viro ino->i_size = st.size; 5504754b825SAl Viro ino->i_blocks = st.blocks; 5514754b825SAl Viro return 0; 5521da177e4SLinus Torvalds } 5531da177e4SLinus Torvalds 5544acdaf27SAl Viro int hostfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, 5551da177e4SLinus Torvalds struct nameidata *nd) 5561da177e4SLinus Torvalds { 5571da177e4SLinus Torvalds struct inode *inode; 5581da177e4SLinus Torvalds char *name; 5591da177e4SLinus Torvalds int error, fd; 5601da177e4SLinus Torvalds 5610a370e5dSDavid Howells inode = hostfs_iget(dir->i_sb); 5620a370e5dSDavid Howells if (IS_ERR(inode)) { 5630a370e5dSDavid Howells error = PTR_ERR(inode); 56484b3db04SJeff Dike goto out; 5650a370e5dSDavid Howells } 5661da177e4SLinus Torvalds 5671da177e4SLinus Torvalds error = -ENOMEM; 568c5322220SAl Viro name = dentry_name(dentry); 5691da177e4SLinus Torvalds if (name == NULL) 5701da177e4SLinus Torvalds goto out_put; 5711da177e4SLinus Torvalds 5721da177e4SLinus Torvalds fd = file_create(name, 5731da177e4SLinus Torvalds mode & S_IRUSR, mode & S_IWUSR, mode & S_IXUSR, 5741da177e4SLinus Torvalds mode & S_IRGRP, mode & S_IWGRP, mode & S_IXGRP, 5751da177e4SLinus Torvalds mode & S_IROTH, mode & S_IWOTH, mode & S_IXOTH); 5764754b825SAl Viro if (fd < 0) 5771da177e4SLinus Torvalds error = fd; 5784754b825SAl Viro else 5795e2df28cSAl Viro error = read_name(inode, name); 5801da177e4SLinus Torvalds 581e9193059SAl Viro __putname(name); 5821da177e4SLinus Torvalds if (error) 5831da177e4SLinus Torvalds goto out_put; 5841da177e4SLinus Torvalds 5851da177e4SLinus Torvalds HOSTFS_I(inode)->fd = fd; 5861da177e4SLinus Torvalds HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE; 5871da177e4SLinus Torvalds d_instantiate(dentry, inode); 588f1adc05eSJeff Dike return 0; 5891da177e4SLinus Torvalds 5901da177e4SLinus Torvalds out_put: 5911da177e4SLinus Torvalds iput(inode); 5921da177e4SLinus Torvalds out: 593f1adc05eSJeff Dike return error; 5941da177e4SLinus Torvalds } 5951da177e4SLinus Torvalds 5961da177e4SLinus Torvalds struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry, 5971da177e4SLinus Torvalds struct nameidata *nd) 5981da177e4SLinus Torvalds { 5991da177e4SLinus Torvalds struct inode *inode; 6001da177e4SLinus Torvalds char *name; 6011da177e4SLinus Torvalds int err; 6021da177e4SLinus Torvalds 6030a370e5dSDavid Howells inode = hostfs_iget(ino->i_sb); 6040a370e5dSDavid Howells if (IS_ERR(inode)) { 6050a370e5dSDavid Howells err = PTR_ERR(inode); 6061da177e4SLinus Torvalds goto out; 6070a370e5dSDavid Howells } 6081da177e4SLinus Torvalds 6091da177e4SLinus Torvalds err = -ENOMEM; 610c5322220SAl Viro name = dentry_name(dentry); 6111da177e4SLinus Torvalds if (name == NULL) 6121da177e4SLinus Torvalds goto out_put; 6131da177e4SLinus Torvalds 6141da177e4SLinus Torvalds err = read_name(inode, name); 6155e2df28cSAl Viro 616e9193059SAl Viro __putname(name); 6171da177e4SLinus Torvalds if (err == -ENOENT) { 6181da177e4SLinus Torvalds iput(inode); 6191da177e4SLinus Torvalds inode = NULL; 6201da177e4SLinus Torvalds } 6211da177e4SLinus Torvalds else if (err) 6221da177e4SLinus Torvalds goto out_put; 6231da177e4SLinus Torvalds 6241da177e4SLinus Torvalds d_add(dentry, inode); 625f1adc05eSJeff Dike return NULL; 6261da177e4SLinus Torvalds 6271da177e4SLinus Torvalds out_put: 6281da177e4SLinus Torvalds iput(inode); 6291da177e4SLinus Torvalds out: 630f1adc05eSJeff Dike return ERR_PTR(err); 6311da177e4SLinus Torvalds } 6321da177e4SLinus Torvalds 6331da177e4SLinus Torvalds int hostfs_link(struct dentry *to, struct inode *ino, struct dentry *from) 6341da177e4SLinus Torvalds { 6351da177e4SLinus Torvalds char *from_name, *to_name; 6361da177e4SLinus Torvalds int err; 6371da177e4SLinus Torvalds 638c5322220SAl Viro if ((from_name = dentry_name(from)) == NULL) 639f1adc05eSJeff Dike return -ENOMEM; 640c5322220SAl Viro to_name = dentry_name(to); 6411da177e4SLinus Torvalds if (to_name == NULL) { 642e9193059SAl Viro __putname(from_name); 643f1adc05eSJeff Dike return -ENOMEM; 6441da177e4SLinus Torvalds } 6451da177e4SLinus Torvalds err = link_file(to_name, from_name); 646e9193059SAl Viro __putname(from_name); 647e9193059SAl Viro __putname(to_name); 648f1adc05eSJeff Dike return err; 6491da177e4SLinus Torvalds } 6501da177e4SLinus Torvalds 6511da177e4SLinus Torvalds int hostfs_unlink(struct inode *ino, struct dentry *dentry) 6521da177e4SLinus Torvalds { 6531da177e4SLinus Torvalds char *file; 6541da177e4SLinus Torvalds int err; 6551da177e4SLinus Torvalds 6561da177e4SLinus Torvalds if (append) 657f1adc05eSJeff Dike return -EPERM; 6581da177e4SLinus Torvalds 659f8d7e187SAl Viro if ((file = dentry_name(dentry)) == NULL) 660f8d7e187SAl Viro return -ENOMEM; 661f8d7e187SAl Viro 6621da177e4SLinus Torvalds err = unlink_file(file); 663e9193059SAl Viro __putname(file); 664f1adc05eSJeff Dike return err; 6651da177e4SLinus Torvalds } 6661da177e4SLinus Torvalds 6671da177e4SLinus Torvalds int hostfs_symlink(struct inode *ino, struct dentry *dentry, const char *to) 6681da177e4SLinus Torvalds { 6691da177e4SLinus Torvalds char *file; 6701da177e4SLinus Torvalds int err; 6711da177e4SLinus Torvalds 672c5322220SAl Viro if ((file = dentry_name(dentry)) == NULL) 673f1adc05eSJeff Dike return -ENOMEM; 6741da177e4SLinus Torvalds err = make_symlink(file, to); 675e9193059SAl Viro __putname(file); 676f1adc05eSJeff Dike return err; 6771da177e4SLinus Torvalds } 6781da177e4SLinus Torvalds 67918bb1db3SAl Viro int hostfs_mkdir(struct inode *ino, struct dentry *dentry, umode_t mode) 6801da177e4SLinus Torvalds { 6811da177e4SLinus Torvalds char *file; 6821da177e4SLinus Torvalds int err; 6831da177e4SLinus Torvalds 684c5322220SAl Viro if ((file = dentry_name(dentry)) == NULL) 685f1adc05eSJeff Dike return -ENOMEM; 6861da177e4SLinus Torvalds err = do_mkdir(file, mode); 687e9193059SAl Viro __putname(file); 688f1adc05eSJeff Dike return err; 6891da177e4SLinus Torvalds } 6901da177e4SLinus Torvalds 6911da177e4SLinus Torvalds int hostfs_rmdir(struct inode *ino, struct dentry *dentry) 6921da177e4SLinus Torvalds { 6931da177e4SLinus Torvalds char *file; 6941da177e4SLinus Torvalds int err; 6951da177e4SLinus Torvalds 696c5322220SAl Viro if ((file = dentry_name(dentry)) == NULL) 697f1adc05eSJeff Dike return -ENOMEM; 6981da177e4SLinus Torvalds err = do_rmdir(file); 699e9193059SAl Viro __putname(file); 700f1adc05eSJeff Dike return err; 7011da177e4SLinus Torvalds } 7021da177e4SLinus Torvalds 703*1a67aafbSAl Viro static int hostfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) 7041da177e4SLinus Torvalds { 7051da177e4SLinus Torvalds struct inode *inode; 7061da177e4SLinus Torvalds char *name; 7070a370e5dSDavid Howells int err; 7081da177e4SLinus Torvalds 7090a370e5dSDavid Howells inode = hostfs_iget(dir->i_sb); 7100a370e5dSDavid Howells if (IS_ERR(inode)) { 7110a370e5dSDavid Howells err = PTR_ERR(inode); 7121da177e4SLinus Torvalds goto out; 7130a370e5dSDavid Howells } 7141da177e4SLinus Torvalds 7151da177e4SLinus Torvalds err = -ENOMEM; 716c5322220SAl Viro name = dentry_name(dentry); 7171da177e4SLinus Torvalds if (name == NULL) 7181da177e4SLinus Torvalds goto out_put; 7191da177e4SLinus Torvalds 7201da177e4SLinus Torvalds init_special_inode(inode, mode, dev); 72188f6cd0cSJohannes Stezenbach err = do_mknod(name, mode, MAJOR(dev), MINOR(dev)); 722e9193059SAl Viro if (!err) 7231da177e4SLinus Torvalds goto out_free; 7241da177e4SLinus Torvalds 7251da177e4SLinus Torvalds err = read_name(inode, name); 726e9193059SAl Viro __putname(name); 7275e2df28cSAl Viro if (err) 7285e2df28cSAl Viro goto out_put; 7291da177e4SLinus Torvalds if (err) 7301da177e4SLinus Torvalds goto out_put; 7311da177e4SLinus Torvalds 7321da177e4SLinus Torvalds d_instantiate(dentry, inode); 733f1adc05eSJeff Dike return 0; 7341da177e4SLinus Torvalds 7351da177e4SLinus Torvalds out_free: 736e9193059SAl Viro __putname(name); 7371da177e4SLinus Torvalds out_put: 7381da177e4SLinus Torvalds iput(inode); 7391da177e4SLinus Torvalds out: 740f1adc05eSJeff Dike return err; 7411da177e4SLinus Torvalds } 7421da177e4SLinus Torvalds 7431da177e4SLinus Torvalds int hostfs_rename(struct inode *from_ino, struct dentry *from, 7441da177e4SLinus Torvalds struct inode *to_ino, struct dentry *to) 7451da177e4SLinus Torvalds { 7461da177e4SLinus Torvalds char *from_name, *to_name; 7471da177e4SLinus Torvalds int err; 7481da177e4SLinus Torvalds 749c5322220SAl Viro if ((from_name = dentry_name(from)) == NULL) 750f1adc05eSJeff Dike return -ENOMEM; 751c5322220SAl Viro if ((to_name = dentry_name(to)) == NULL) { 752e9193059SAl Viro __putname(from_name); 753f1adc05eSJeff Dike return -ENOMEM; 7541da177e4SLinus Torvalds } 7551da177e4SLinus Torvalds err = rename_file(from_name, to_name); 756e9193059SAl Viro __putname(from_name); 757e9193059SAl Viro __putname(to_name); 758f1adc05eSJeff Dike return err; 7591da177e4SLinus Torvalds } 7601da177e4SLinus Torvalds 76110556cb2SAl Viro int hostfs_permission(struct inode *ino, int desired) 7621da177e4SLinus Torvalds { 7631da177e4SLinus Torvalds char *name; 7641da177e4SLinus Torvalds int r = 0, w = 0, x = 0, err; 7651da177e4SLinus Torvalds 76610556cb2SAl Viro if (desired & MAY_NOT_BLOCK) 767b74c79e9SNick Piggin return -ECHILD; 768b74c79e9SNick Piggin 7691da177e4SLinus Torvalds if (desired & MAY_READ) r = 1; 7701da177e4SLinus Torvalds if (desired & MAY_WRITE) w = 1; 7711da177e4SLinus Torvalds if (desired & MAY_EXEC) x = 1; 772c5322220SAl Viro name = inode_name(ino); 773f1adc05eSJeff Dike if (name == NULL) 774f1adc05eSJeff Dike return -ENOMEM; 7751da177e4SLinus Torvalds 7761da177e4SLinus Torvalds if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) || 7771da177e4SLinus Torvalds S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode)) 7781da177e4SLinus Torvalds err = 0; 7791da177e4SLinus Torvalds else 7801da177e4SLinus Torvalds err = access_file(name, r, w, x); 781e9193059SAl Viro __putname(name); 7821da177e4SLinus Torvalds if (!err) 7832830ba7fSAl Viro err = generic_permission(ino, desired); 7841da177e4SLinus Torvalds return err; 7851da177e4SLinus Torvalds } 7861da177e4SLinus Torvalds 7871da177e4SLinus Torvalds int hostfs_setattr(struct dentry *dentry, struct iattr *attr) 7881da177e4SLinus Torvalds { 7891025774cSChristoph Hellwig struct inode *inode = dentry->d_inode; 7901da177e4SLinus Torvalds struct hostfs_iattr attrs; 7911da177e4SLinus Torvalds char *name; 7921da177e4SLinus Torvalds int err; 7931da177e4SLinus Torvalds 7941025774cSChristoph Hellwig int fd = HOSTFS_I(inode)->fd; 7955822b7faSAlberto Bertogli 7961025774cSChristoph Hellwig err = inode_change_ok(inode, attr); 7971da177e4SLinus Torvalds if (err) 7981da177e4SLinus Torvalds return err; 7991da177e4SLinus Torvalds 8001da177e4SLinus Torvalds if (append) 8011da177e4SLinus Torvalds attr->ia_valid &= ~ATTR_SIZE; 8021da177e4SLinus Torvalds 8031da177e4SLinus Torvalds attrs.ia_valid = 0; 8041da177e4SLinus Torvalds if (attr->ia_valid & ATTR_MODE) { 8051da177e4SLinus Torvalds attrs.ia_valid |= HOSTFS_ATTR_MODE; 8061da177e4SLinus Torvalds attrs.ia_mode = attr->ia_mode; 8071da177e4SLinus Torvalds } 8081da177e4SLinus Torvalds if (attr->ia_valid & ATTR_UID) { 8091da177e4SLinus Torvalds attrs.ia_valid |= HOSTFS_ATTR_UID; 8101da177e4SLinus Torvalds attrs.ia_uid = attr->ia_uid; 8111da177e4SLinus Torvalds } 8121da177e4SLinus Torvalds if (attr->ia_valid & ATTR_GID) { 8131da177e4SLinus Torvalds attrs.ia_valid |= HOSTFS_ATTR_GID; 8141da177e4SLinus Torvalds attrs.ia_gid = attr->ia_gid; 8151da177e4SLinus Torvalds } 8161da177e4SLinus Torvalds if (attr->ia_valid & ATTR_SIZE) { 8171da177e4SLinus Torvalds attrs.ia_valid |= HOSTFS_ATTR_SIZE; 8181da177e4SLinus Torvalds attrs.ia_size = attr->ia_size; 8191da177e4SLinus Torvalds } 8201da177e4SLinus Torvalds if (attr->ia_valid & ATTR_ATIME) { 8211da177e4SLinus Torvalds attrs.ia_valid |= HOSTFS_ATTR_ATIME; 8221da177e4SLinus Torvalds attrs.ia_atime = attr->ia_atime; 8231da177e4SLinus Torvalds } 8241da177e4SLinus Torvalds if (attr->ia_valid & ATTR_MTIME) { 8251da177e4SLinus Torvalds attrs.ia_valid |= HOSTFS_ATTR_MTIME; 8261da177e4SLinus Torvalds attrs.ia_mtime = attr->ia_mtime; 8271da177e4SLinus Torvalds } 8281da177e4SLinus Torvalds if (attr->ia_valid & ATTR_CTIME) { 8291da177e4SLinus Torvalds attrs.ia_valid |= HOSTFS_ATTR_CTIME; 8301da177e4SLinus Torvalds attrs.ia_ctime = attr->ia_ctime; 8311da177e4SLinus Torvalds } 8321da177e4SLinus Torvalds if (attr->ia_valid & ATTR_ATIME_SET) { 8331da177e4SLinus Torvalds attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET; 8341da177e4SLinus Torvalds } 8351da177e4SLinus Torvalds if (attr->ia_valid & ATTR_MTIME_SET) { 8361da177e4SLinus Torvalds attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET; 8371da177e4SLinus Torvalds } 838c5322220SAl Viro name = dentry_name(dentry); 839f1adc05eSJeff Dike if (name == NULL) 840f1adc05eSJeff Dike return -ENOMEM; 8415822b7faSAlberto Bertogli err = set_attr(name, &attrs, fd); 842e9193059SAl Viro __putname(name); 8431da177e4SLinus Torvalds if (err) 844f1adc05eSJeff Dike return err; 8451da177e4SLinus Torvalds 8461025774cSChristoph Hellwig if ((attr->ia_valid & ATTR_SIZE) && 8471025774cSChristoph Hellwig attr->ia_size != i_size_read(inode)) { 8481025774cSChristoph Hellwig int error; 8491025774cSChristoph Hellwig 8501025774cSChristoph Hellwig error = vmtruncate(inode, attr->ia_size); 8511025774cSChristoph Hellwig if (err) 8521025774cSChristoph Hellwig return err; 8531025774cSChristoph Hellwig } 8541025774cSChristoph Hellwig 8551025774cSChristoph Hellwig setattr_copy(inode, attr); 8561025774cSChristoph Hellwig mark_inode_dirty(inode); 8571025774cSChristoph Hellwig return 0; 8581da177e4SLinus Torvalds } 8591da177e4SLinus Torvalds 86092e1d5beSArjan van de Ven static const struct inode_operations hostfs_iops = { 8611da177e4SLinus Torvalds .create = hostfs_create, 8621da177e4SLinus Torvalds .link = hostfs_link, 8631da177e4SLinus Torvalds .unlink = hostfs_unlink, 8641da177e4SLinus Torvalds .symlink = hostfs_symlink, 8651da177e4SLinus Torvalds .mkdir = hostfs_mkdir, 8661da177e4SLinus Torvalds .rmdir = hostfs_rmdir, 8671da177e4SLinus Torvalds .mknod = hostfs_mknod, 8681da177e4SLinus Torvalds .rename = hostfs_rename, 8691da177e4SLinus Torvalds .permission = hostfs_permission, 8701da177e4SLinus Torvalds .setattr = hostfs_setattr, 8711da177e4SLinus Torvalds }; 8721da177e4SLinus Torvalds 87392e1d5beSArjan van de Ven static const struct inode_operations hostfs_dir_iops = { 8741da177e4SLinus Torvalds .create = hostfs_create, 8751da177e4SLinus Torvalds .lookup = hostfs_lookup, 8761da177e4SLinus Torvalds .link = hostfs_link, 8771da177e4SLinus Torvalds .unlink = hostfs_unlink, 8781da177e4SLinus Torvalds .symlink = hostfs_symlink, 8791da177e4SLinus Torvalds .mkdir = hostfs_mkdir, 8801da177e4SLinus Torvalds .rmdir = hostfs_rmdir, 8811da177e4SLinus Torvalds .mknod = hostfs_mknod, 8821da177e4SLinus Torvalds .rename = hostfs_rename, 8831da177e4SLinus Torvalds .permission = hostfs_permission, 8841da177e4SLinus Torvalds .setattr = hostfs_setattr, 8851da177e4SLinus Torvalds }; 8861da177e4SLinus Torvalds 887d0352d3eSAl Viro static void *hostfs_follow_link(struct dentry *dentry, struct nameidata *nd) 8881da177e4SLinus Torvalds { 889d0352d3eSAl Viro char *link = __getname(); 890d0352d3eSAl Viro if (link) { 891d0352d3eSAl Viro char *path = dentry_name(dentry); 892d0352d3eSAl Viro int err = -ENOMEM; 893d0352d3eSAl Viro if (path) { 8943b6036d1SAl Viro err = hostfs_do_readlink(path, link, PATH_MAX); 895d0352d3eSAl Viro if (err == PATH_MAX) 8961da177e4SLinus Torvalds err = -E2BIG; 897e9193059SAl Viro __putname(path); 8981da177e4SLinus Torvalds } 899d0352d3eSAl Viro if (err < 0) { 900d0352d3eSAl Viro __putname(link); 901d0352d3eSAl Viro link = ERR_PTR(err); 902d0352d3eSAl Viro } 903d0352d3eSAl Viro } else { 904d0352d3eSAl Viro link = ERR_PTR(-ENOMEM); 9051da177e4SLinus Torvalds } 9061da177e4SLinus Torvalds 907d0352d3eSAl Viro nd_set_link(nd, link); 908d0352d3eSAl Viro return NULL; 909d0352d3eSAl Viro } 910d0352d3eSAl Viro 911d0352d3eSAl Viro static void hostfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) 912d0352d3eSAl Viro { 913d0352d3eSAl Viro char *s = nd_get_link(nd); 914d0352d3eSAl Viro if (!IS_ERR(s)) 915d0352d3eSAl Viro __putname(s); 916d0352d3eSAl Viro } 917d0352d3eSAl Viro 918d0352d3eSAl Viro static const struct inode_operations hostfs_link_iops = { 919d0352d3eSAl Viro .readlink = generic_readlink, 920d0352d3eSAl Viro .follow_link = hostfs_follow_link, 921d0352d3eSAl Viro .put_link = hostfs_put_link, 9221da177e4SLinus Torvalds }; 9231da177e4SLinus Torvalds 9241da177e4SLinus Torvalds static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent) 9251da177e4SLinus Torvalds { 9261da177e4SLinus Torvalds struct inode *root_inode; 92775e8defbSPaolo 'Blaisorblade' Giarrusso char *host_root_path, *req_root = d; 9281da177e4SLinus Torvalds int err; 9291da177e4SLinus Torvalds 9301da177e4SLinus Torvalds sb->s_blocksize = 1024; 9311da177e4SLinus Torvalds sb->s_blocksize_bits = 10; 9321da177e4SLinus Torvalds sb->s_magic = HOSTFS_SUPER_MAGIC; 9331da177e4SLinus Torvalds sb->s_op = &hostfs_sbops; 934f772c4a6SAl Viro sb->s_d_op = &hostfs_dentry_ops; 935752fa51eSWolfgang Illmeyer sb->s_maxbytes = MAX_LFS_FILESIZE; 9361da177e4SLinus Torvalds 937a6eb0be6SPaolo 'Blaisorblade' Giarrusso /* NULL is printed as <NULL> by sprintf: avoid that. */ 93875e8defbSPaolo 'Blaisorblade' Giarrusso if (req_root == NULL) 93975e8defbSPaolo 'Blaisorblade' Giarrusso req_root = ""; 9401da177e4SLinus Torvalds 9411da177e4SLinus Torvalds err = -ENOMEM; 942601d2c38SAl Viro sb->s_fs_info = host_root_path = 943601d2c38SAl Viro kmalloc(strlen(root_ino) + strlen(req_root) + 2, GFP_KERNEL); 94475e8defbSPaolo 'Blaisorblade' Giarrusso if (host_root_path == NULL) 9451da177e4SLinus Torvalds goto out; 9461da177e4SLinus Torvalds 94775e8defbSPaolo 'Blaisorblade' Giarrusso sprintf(host_root_path, "%s/%s", root_ino, req_root); 9481da177e4SLinus Torvalds 94952b209f7SAl Viro root_inode = new_inode(sb); 95052b209f7SAl Viro if (!root_inode) 951601d2c38SAl Viro goto out; 9521da177e4SLinus Torvalds 9534754b825SAl Viro err = read_name(root_inode, host_root_path); 9544754b825SAl Viro if (err) 9554754b825SAl Viro goto out_put; 95652b209f7SAl Viro 9574754b825SAl Viro if (S_ISLNK(root_inode->i_mode)) { 95852b209f7SAl Viro char *name = follow_link(host_root_path); 95952b209f7SAl Viro if (IS_ERR(name)) 96052b209f7SAl Viro err = PTR_ERR(name); 96152b209f7SAl Viro else 96252b209f7SAl Viro err = read_name(root_inode, name); 96352b209f7SAl Viro kfree(name); 9641da177e4SLinus Torvalds if (err) 9651da177e4SLinus Torvalds goto out_put; 9664754b825SAl Viro } 9671da177e4SLinus Torvalds 9681da177e4SLinus Torvalds err = -ENOMEM; 9691da177e4SLinus Torvalds sb->s_root = d_alloc_root(root_inode); 9701da177e4SLinus Torvalds if (sb->s_root == NULL) 9711da177e4SLinus Torvalds goto out_put; 9721da177e4SLinus Torvalds 973f1adc05eSJeff Dike return 0; 9741da177e4SLinus Torvalds 9751da177e4SLinus Torvalds out_put: 9761da177e4SLinus Torvalds iput(root_inode); 9771da177e4SLinus Torvalds out: 978f1adc05eSJeff Dike return err; 9791da177e4SLinus Torvalds } 9801da177e4SLinus Torvalds 9813c26ff6eSAl Viro static struct dentry *hostfs_read_sb(struct file_system_type *type, 9821da177e4SLinus Torvalds int flags, const char *dev_name, 9833c26ff6eSAl Viro void *data) 9841da177e4SLinus Torvalds { 9853c26ff6eSAl Viro return mount_nodev(type, flags, data, hostfs_fill_sb_common); 9861da177e4SLinus Torvalds } 9871da177e4SLinus Torvalds 988601d2c38SAl Viro static void hostfs_kill_sb(struct super_block *s) 989601d2c38SAl Viro { 990601d2c38SAl Viro kill_anon_super(s); 991601d2c38SAl Viro kfree(s->s_fs_info); 992601d2c38SAl Viro } 993601d2c38SAl Viro 9941da177e4SLinus Torvalds static struct file_system_type hostfs_type = { 9951da177e4SLinus Torvalds .owner = THIS_MODULE, 9961da177e4SLinus Torvalds .name = "hostfs", 9973c26ff6eSAl Viro .mount = hostfs_read_sb, 998601d2c38SAl Viro .kill_sb = hostfs_kill_sb, 9991da177e4SLinus Torvalds .fs_flags = 0, 10001da177e4SLinus Torvalds }; 10011da177e4SLinus Torvalds 10021da177e4SLinus Torvalds static int __init init_hostfs(void) 10031da177e4SLinus Torvalds { 1004f1adc05eSJeff Dike return register_filesystem(&hostfs_type); 10051da177e4SLinus Torvalds } 10061da177e4SLinus Torvalds 10071da177e4SLinus Torvalds static void __exit exit_hostfs(void) 10081da177e4SLinus Torvalds { 10091da177e4SLinus Torvalds unregister_filesystem(&hostfs_type); 10101da177e4SLinus Torvalds } 10111da177e4SLinus Torvalds 10121da177e4SLinus Torvalds module_init(init_hostfs) 10131da177e4SLinus Torvalds module_exit(exit_hostfs) 10141da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 1015