Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
0003  * Licensed under the GPL
0004  *
0005  * Ported the filesystem routines to 2.5.
0006  * 2003-02-10 Petr Baudis <pasky@ucw.cz>
0007  */
0008 
0009 #include <linux/fs.h>
0010 #include <linux/magic.h>
0011 #include <linux/module.h>
0012 #include <linux/mm.h>
0013 #include <linux/pagemap.h>
0014 #include <linux/statfs.h>
0015 #include <linux/slab.h>
0016 #include <linux/seq_file.h>
0017 #include <linux/writeback.h>
0018 #include <linux/mount.h>
0019 #include <linux/namei.h>
0020 #include "hostfs.h"
0021 #include <init.h>
0022 #include <kern.h>
0023 
0024 struct hostfs_inode_info {
0025     int fd;
0026     fmode_t mode;
0027     struct inode vfs_inode;
0028     struct mutex open_mutex;
0029 };
0030 
0031 static inline struct hostfs_inode_info *HOSTFS_I(struct inode *inode)
0032 {
0033     return list_entry(inode, struct hostfs_inode_info, vfs_inode);
0034 }
0035 
0036 #define FILE_HOSTFS_I(file) HOSTFS_I(file_inode(file))
0037 
0038 static struct kmem_cache *hostfs_inode_cache;
0039 
0040 /* Changed in hostfs_args before the kernel starts running */
0041 static char *root_ino = "";
0042 static int append = 0;
0043 
0044 static const struct inode_operations hostfs_iops;
0045 static const struct inode_operations hostfs_dir_iops;
0046 static const struct inode_operations hostfs_link_iops;
0047 
0048 #ifndef MODULE
0049 static int __init hostfs_args(char *options, int *add)
0050 {
0051     char *ptr;
0052 
0053     ptr = strchr(options, ',');
0054     if (ptr != NULL)
0055         *ptr++ = '\0';
0056     if (*options != '\0')
0057         root_ino = options;
0058 
0059     options = ptr;
0060     while (options) {
0061         ptr = strchr(options, ',');
0062         if (ptr != NULL)
0063             *ptr++ = '\0';
0064         if (*options != '\0') {
0065             if (!strcmp(options, "append"))
0066                 append = 1;
0067             else printf("hostfs_args - unsupported option - %s\n",
0068                     options);
0069         }
0070         options = ptr;
0071     }
0072     return 0;
0073 }
0074 
0075 __uml_setup("hostfs=", hostfs_args,
0076 "hostfs=<root dir>,<flags>,...\n"
0077 "    This is used to set hostfs parameters.  The root directory argument\n"
0078 "    is used to confine all hostfs mounts to within the specified directory\n"
0079 "    tree on the host.  If this isn't specified, then a user inside UML can\n"
0080 "    mount anything on the host that's accessible to the user that's running\n"
0081 "    it.\n"
0082 "    The only flag currently supported is 'append', which specifies that all\n"
0083 "    files opened by hostfs will be opened in append mode.\n\n"
0084 );
0085 #endif
0086 
0087 static char *__dentry_name(struct dentry *dentry, char *name)
0088 {
0089     char *p = dentry_path_raw(dentry, name, PATH_MAX);
0090     char *root;
0091     size_t len;
0092 
0093     root = dentry->d_sb->s_fs_info;
0094     len = strlen(root);
0095     if (IS_ERR(p)) {
0096         __putname(name);
0097         return NULL;
0098     }
0099 
0100     /*
0101      * This function relies on the fact that dentry_path_raw() will place
0102      * the path name at the end of the provided buffer.
0103      */
0104     BUG_ON(p + strlen(p) + 1 != name + PATH_MAX);
0105 
0106     strlcpy(name, root, PATH_MAX);
0107     if (len > p - name) {
0108         __putname(name);
0109         return NULL;
0110     }
0111 
0112     if (p > name + len)
0113         strcpy(name + len, p);
0114 
0115     return name;
0116 }
0117 
0118 static char *dentry_name(struct dentry *dentry)
0119 {
0120     char *name = __getname();
0121     if (!name)
0122         return NULL;
0123 
0124     return __dentry_name(dentry, name);
0125 }
0126 
0127 static char *inode_name(struct inode *ino)
0128 {
0129     struct dentry *dentry;
0130     char *name;
0131 
0132     dentry = d_find_alias(ino);
0133     if (!dentry)
0134         return NULL;
0135 
0136     name = dentry_name(dentry);
0137 
0138     dput(dentry);
0139 
0140     return name;
0141 }
0142 
0143 static char *follow_link(char *link)
0144 {
0145     char *name, *resolved, *end;
0146     int n;
0147 
0148     name = kmalloc(PATH_MAX, GFP_KERNEL);
0149     if (!name) {
0150         n = -ENOMEM;
0151         goto out_free;
0152     }
0153 
0154     n = hostfs_do_readlink(link, name, PATH_MAX);
0155     if (n < 0)
0156         goto out_free;
0157     else if (n == PATH_MAX) {
0158         n = -E2BIG;
0159         goto out_free;
0160     }
0161 
0162     if (*name == '/')
0163         return name;
0164 
0165     end = strrchr(link, '/');
0166     if (end == NULL)
0167         return name;
0168 
0169     *(end + 1) = '\0';
0170 
0171     resolved = kasprintf(GFP_KERNEL, "%s%s", link, name);
0172     if (resolved == NULL) {
0173         n = -ENOMEM;
0174         goto out_free;
0175     }
0176 
0177     kfree(name);
0178     return resolved;
0179 
0180  out_free:
0181     kfree(name);
0182     return ERR_PTR(n);
0183 }
0184 
0185 static struct inode *hostfs_iget(struct super_block *sb)
0186 {
0187     struct inode *inode = new_inode(sb);
0188     if (!inode)
0189         return ERR_PTR(-ENOMEM);
0190     return inode;
0191 }
0192 
0193 static int hostfs_statfs(struct dentry *dentry, struct kstatfs *sf)
0194 {
0195     /*
0196      * do_statfs uses struct statfs64 internally, but the linux kernel
0197      * struct statfs still has 32-bit versions for most of these fields,
0198      * so we convert them here
0199      */
0200     int err;
0201     long long f_blocks;
0202     long long f_bfree;
0203     long long f_bavail;
0204     long long f_files;
0205     long long f_ffree;
0206 
0207     err = do_statfs(dentry->d_sb->s_fs_info,
0208             &sf->f_bsize, &f_blocks, &f_bfree, &f_bavail, &f_files,
0209             &f_ffree, &sf->f_fsid, sizeof(sf->f_fsid),
0210             &sf->f_namelen);
0211     if (err)
0212         return err;
0213     sf->f_blocks = f_blocks;
0214     sf->f_bfree = f_bfree;
0215     sf->f_bavail = f_bavail;
0216     sf->f_files = f_files;
0217     sf->f_ffree = f_ffree;
0218     sf->f_type = HOSTFS_SUPER_MAGIC;
0219     return 0;
0220 }
0221 
0222 static struct inode *hostfs_alloc_inode(struct super_block *sb)
0223 {
0224     struct hostfs_inode_info *hi;
0225 
0226     hi = alloc_inode_sb(sb, hostfs_inode_cache, GFP_KERNEL_ACCOUNT);
0227     if (hi == NULL)
0228         return NULL;
0229     hi->fd = -1;
0230     hi->mode = 0;
0231     inode_init_once(&hi->vfs_inode);
0232     mutex_init(&hi->open_mutex);
0233     return &hi->vfs_inode;
0234 }
0235 
0236 static void hostfs_evict_inode(struct inode *inode)
0237 {
0238     truncate_inode_pages_final(&inode->i_data);
0239     clear_inode(inode);
0240     if (HOSTFS_I(inode)->fd != -1) {
0241         close_file(&HOSTFS_I(inode)->fd);
0242         HOSTFS_I(inode)->fd = -1;
0243     }
0244 }
0245 
0246 static void hostfs_free_inode(struct inode *inode)
0247 {
0248     kmem_cache_free(hostfs_inode_cache, HOSTFS_I(inode));
0249 }
0250 
0251 static int hostfs_show_options(struct seq_file *seq, struct dentry *root)
0252 {
0253     const char *root_path = root->d_sb->s_fs_info;
0254     size_t offset = strlen(root_ino) + 1;
0255 
0256     if (strlen(root_path) > offset)
0257         seq_show_option(seq, root_path + offset, NULL);
0258 
0259     if (append)
0260         seq_puts(seq, ",append");
0261 
0262     return 0;
0263 }
0264 
0265 static const struct super_operations hostfs_sbops = {
0266     .alloc_inode    = hostfs_alloc_inode,
0267     .free_inode = hostfs_free_inode,
0268     .evict_inode    = hostfs_evict_inode,
0269     .statfs     = hostfs_statfs,
0270     .show_options   = hostfs_show_options,
0271 };
0272 
0273 static int hostfs_readdir(struct file *file, struct dir_context *ctx)
0274 {
0275     void *dir;
0276     char *name;
0277     unsigned long long next, ino;
0278     int error, len;
0279     unsigned int type;
0280 
0281     name = dentry_name(file->f_path.dentry);
0282     if (name == NULL)
0283         return -ENOMEM;
0284     dir = open_dir(name, &error);
0285     __putname(name);
0286     if (dir == NULL)
0287         return -error;
0288     next = ctx->pos;
0289     seek_dir(dir, next);
0290     while ((name = read_dir(dir, &next, &ino, &len, &type)) != NULL) {
0291         if (!dir_emit(ctx, name, len, ino, type))
0292             break;
0293         ctx->pos = next;
0294     }
0295     close_dir(dir);
0296     return 0;
0297 }
0298 
0299 static int hostfs_open(struct inode *ino, struct file *file)
0300 {
0301     char *name;
0302     fmode_t mode;
0303     int err;
0304     int r, w, fd;
0305 
0306     mode = file->f_mode & (FMODE_READ | FMODE_WRITE);
0307     if ((mode & HOSTFS_I(ino)->mode) == mode)
0308         return 0;
0309 
0310     mode |= HOSTFS_I(ino)->mode;
0311 
0312 retry:
0313     r = w = 0;
0314 
0315     if (mode & FMODE_READ)
0316         r = 1;
0317     if (mode & FMODE_WRITE)
0318         r = w = 1;
0319 
0320     name = dentry_name(file_dentry(file));
0321     if (name == NULL)
0322         return -ENOMEM;
0323 
0324     fd = open_file(name, r, w, append);
0325     __putname(name);
0326     if (fd < 0)
0327         return fd;
0328 
0329     mutex_lock(&HOSTFS_I(ino)->open_mutex);
0330     /* somebody else had handled it first? */
0331     if ((mode & HOSTFS_I(ino)->mode) == mode) {
0332         mutex_unlock(&HOSTFS_I(ino)->open_mutex);
0333         close_file(&fd);
0334         return 0;
0335     }
0336     if ((mode | HOSTFS_I(ino)->mode) != mode) {
0337         mode |= HOSTFS_I(ino)->mode;
0338         mutex_unlock(&HOSTFS_I(ino)->open_mutex);
0339         close_file(&fd);
0340         goto retry;
0341     }
0342     if (HOSTFS_I(ino)->fd == -1) {
0343         HOSTFS_I(ino)->fd = fd;
0344     } else {
0345         err = replace_file(fd, HOSTFS_I(ino)->fd);
0346         close_file(&fd);
0347         if (err < 0) {
0348             mutex_unlock(&HOSTFS_I(ino)->open_mutex);
0349             return err;
0350         }
0351     }
0352     HOSTFS_I(ino)->mode = mode;
0353     mutex_unlock(&HOSTFS_I(ino)->open_mutex);
0354 
0355     return 0;
0356 }
0357 
0358 static int hostfs_file_release(struct inode *inode, struct file *file)
0359 {
0360     filemap_write_and_wait(inode->i_mapping);
0361 
0362     return 0;
0363 }
0364 
0365 static int hostfs_fsync(struct file *file, loff_t start, loff_t end,
0366             int datasync)
0367 {
0368     struct inode *inode = file->f_mapping->host;
0369     int ret;
0370 
0371     ret = file_write_and_wait_range(file, start, end);
0372     if (ret)
0373         return ret;
0374 
0375     inode_lock(inode);
0376     ret = fsync_file(HOSTFS_I(inode)->fd, datasync);
0377     inode_unlock(inode);
0378 
0379     return ret;
0380 }
0381 
0382 static const struct file_operations hostfs_file_fops = {
0383     .llseek     = generic_file_llseek,
0384     .splice_read    = generic_file_splice_read,
0385     .splice_write   = iter_file_splice_write,
0386     .read_iter  = generic_file_read_iter,
0387     .write_iter = generic_file_write_iter,
0388     .mmap       = generic_file_mmap,
0389     .open       = hostfs_open,
0390     .release    = hostfs_file_release,
0391     .fsync      = hostfs_fsync,
0392 };
0393 
0394 static const struct file_operations hostfs_dir_fops = {
0395     .llseek     = generic_file_llseek,
0396     .iterate_shared = hostfs_readdir,
0397     .read       = generic_read_dir,
0398     .open       = hostfs_open,
0399     .fsync      = hostfs_fsync,
0400 };
0401 
0402 static int hostfs_writepage(struct page *page, struct writeback_control *wbc)
0403 {
0404     struct address_space *mapping = page->mapping;
0405     struct inode *inode = mapping->host;
0406     char *buffer;
0407     loff_t base = page_offset(page);
0408     int count = PAGE_SIZE;
0409     int end_index = inode->i_size >> PAGE_SHIFT;
0410     int err;
0411 
0412     if (page->index >= end_index)
0413         count = inode->i_size & (PAGE_SIZE-1);
0414 
0415     buffer = kmap(page);
0416 
0417     err = write_file(HOSTFS_I(inode)->fd, &base, buffer, count);
0418     if (err != count) {
0419         if (err >= 0)
0420             err = -EIO;
0421         mapping_set_error(mapping, err);
0422         goto out;
0423     }
0424 
0425     if (base > inode->i_size)
0426         inode->i_size = base;
0427 
0428     err = 0;
0429 
0430  out:
0431     kunmap(page);
0432 
0433     unlock_page(page);
0434     return err;
0435 }
0436 
0437 static int hostfs_read_folio(struct file *file, struct folio *folio)
0438 {
0439     struct page *page = &folio->page;
0440     char *buffer;
0441     loff_t start = page_offset(page);
0442     int bytes_read, ret = 0;
0443 
0444     buffer = kmap(page);
0445     bytes_read = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer,
0446             PAGE_SIZE);
0447     if (bytes_read < 0) {
0448         ClearPageUptodate(page);
0449         SetPageError(page);
0450         ret = bytes_read;
0451         goto out;
0452     }
0453 
0454     memset(buffer + bytes_read, 0, PAGE_SIZE - bytes_read);
0455 
0456     ClearPageError(page);
0457     SetPageUptodate(page);
0458 
0459  out:
0460     flush_dcache_page(page);
0461     kunmap(page);
0462     unlock_page(page);
0463     return ret;
0464 }
0465 
0466 static int hostfs_write_begin(struct file *file, struct address_space *mapping,
0467                   loff_t pos, unsigned len,
0468                   struct page **pagep, void **fsdata)
0469 {
0470     pgoff_t index = pos >> PAGE_SHIFT;
0471 
0472     *pagep = grab_cache_page_write_begin(mapping, index);
0473     if (!*pagep)
0474         return -ENOMEM;
0475     return 0;
0476 }
0477 
0478 static int hostfs_write_end(struct file *file, struct address_space *mapping,
0479                 loff_t pos, unsigned len, unsigned copied,
0480                 struct page *page, void *fsdata)
0481 {
0482     struct inode *inode = mapping->host;
0483     void *buffer;
0484     unsigned from = pos & (PAGE_SIZE - 1);
0485     int err;
0486 
0487     buffer = kmap(page);
0488     err = write_file(FILE_HOSTFS_I(file)->fd, &pos, buffer + from, copied);
0489     kunmap(page);
0490 
0491     if (!PageUptodate(page) && err == PAGE_SIZE)
0492         SetPageUptodate(page);
0493 
0494     /*
0495      * If err > 0, write_file has added err to pos, so we are comparing
0496      * i_size against the last byte written.
0497      */
0498     if (err > 0 && (pos > inode->i_size))
0499         inode->i_size = pos;
0500     unlock_page(page);
0501     put_page(page);
0502 
0503     return err;
0504 }
0505 
0506 static const struct address_space_operations hostfs_aops = {
0507     .writepage  = hostfs_writepage,
0508     .read_folio = hostfs_read_folio,
0509     .dirty_folio    = filemap_dirty_folio,
0510     .write_begin    = hostfs_write_begin,
0511     .write_end  = hostfs_write_end,
0512 };
0513 
0514 static int read_name(struct inode *ino, char *name)
0515 {
0516     dev_t rdev;
0517     struct hostfs_stat st;
0518     int err = stat_file(name, &st, -1);
0519     if (err)
0520         return err;
0521 
0522     /* Reencode maj and min with the kernel encoding.*/
0523     rdev = MKDEV(st.maj, st.min);
0524 
0525     switch (st.mode & S_IFMT) {
0526     case S_IFLNK:
0527         ino->i_op = &hostfs_link_iops;
0528         break;
0529     case S_IFDIR:
0530         ino->i_op = &hostfs_dir_iops;
0531         ino->i_fop = &hostfs_dir_fops;
0532         break;
0533     case S_IFCHR:
0534     case S_IFBLK:
0535     case S_IFIFO:
0536     case S_IFSOCK:
0537         init_special_inode(ino, st.mode & S_IFMT, rdev);
0538         ino->i_op = &hostfs_iops;
0539         break;
0540     case S_IFREG:
0541         ino->i_op = &hostfs_iops;
0542         ino->i_fop = &hostfs_file_fops;
0543         ino->i_mapping->a_ops = &hostfs_aops;
0544         break;
0545     default:
0546         return -EIO;
0547     }
0548 
0549     ino->i_ino = st.ino;
0550     ino->i_mode = st.mode;
0551     set_nlink(ino, st.nlink);
0552     i_uid_write(ino, st.uid);
0553     i_gid_write(ino, st.gid);
0554     ino->i_atime = (struct timespec64){ st.atime.tv_sec, st.atime.tv_nsec };
0555     ino->i_mtime = (struct timespec64){ st.mtime.tv_sec, st.mtime.tv_nsec };
0556     ino->i_ctime = (struct timespec64){ st.ctime.tv_sec, st.ctime.tv_nsec };
0557     ino->i_size = st.size;
0558     ino->i_blocks = st.blocks;
0559     return 0;
0560 }
0561 
0562 static int hostfs_create(struct user_namespace *mnt_userns, struct inode *dir,
0563              struct dentry *dentry, umode_t mode, bool excl)
0564 {
0565     struct inode *inode;
0566     char *name;
0567     int error, fd;
0568 
0569     inode = hostfs_iget(dir->i_sb);
0570     if (IS_ERR(inode)) {
0571         error = PTR_ERR(inode);
0572         goto out;
0573     }
0574 
0575     error = -ENOMEM;
0576     name = dentry_name(dentry);
0577     if (name == NULL)
0578         goto out_put;
0579 
0580     fd = file_create(name, mode & 0777);
0581     if (fd < 0)
0582         error = fd;
0583     else
0584         error = read_name(inode, name);
0585 
0586     __putname(name);
0587     if (error)
0588         goto out_put;
0589 
0590     HOSTFS_I(inode)->fd = fd;
0591     HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE;
0592     d_instantiate(dentry, inode);
0593     return 0;
0594 
0595  out_put:
0596     iput(inode);
0597  out:
0598     return error;
0599 }
0600 
0601 static struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry,
0602                     unsigned int flags)
0603 {
0604     struct inode *inode;
0605     char *name;
0606     int err;
0607 
0608     inode = hostfs_iget(ino->i_sb);
0609     if (IS_ERR(inode))
0610         goto out;
0611 
0612     err = -ENOMEM;
0613     name = dentry_name(dentry);
0614     if (name) {
0615         err = read_name(inode, name);
0616         __putname(name);
0617     }
0618     if (err) {
0619         iput(inode);
0620         inode = (err == -ENOENT) ? NULL : ERR_PTR(err);
0621     }
0622  out:
0623     return d_splice_alias(inode, dentry);
0624 }
0625 
0626 static int hostfs_link(struct dentry *to, struct inode *ino,
0627                struct dentry *from)
0628 {
0629     char *from_name, *to_name;
0630     int err;
0631 
0632     if ((from_name = dentry_name(from)) == NULL)
0633         return -ENOMEM;
0634     to_name = dentry_name(to);
0635     if (to_name == NULL) {
0636         __putname(from_name);
0637         return -ENOMEM;
0638     }
0639     err = link_file(to_name, from_name);
0640     __putname(from_name);
0641     __putname(to_name);
0642     return err;
0643 }
0644 
0645 static int hostfs_unlink(struct inode *ino, struct dentry *dentry)
0646 {
0647     char *file;
0648     int err;
0649 
0650     if (append)
0651         return -EPERM;
0652 
0653     if ((file = dentry_name(dentry)) == NULL)
0654         return -ENOMEM;
0655 
0656     err = unlink_file(file);
0657     __putname(file);
0658     return err;
0659 }
0660 
0661 static int hostfs_symlink(struct user_namespace *mnt_userns, struct inode *ino,
0662               struct dentry *dentry, const char *to)
0663 {
0664     char *file;
0665     int err;
0666 
0667     if ((file = dentry_name(dentry)) == NULL)
0668         return -ENOMEM;
0669     err = make_symlink(file, to);
0670     __putname(file);
0671     return err;
0672 }
0673 
0674 static int hostfs_mkdir(struct user_namespace *mnt_userns, struct inode *ino,
0675             struct dentry *dentry, umode_t mode)
0676 {
0677     char *file;
0678     int err;
0679 
0680     if ((file = dentry_name(dentry)) == NULL)
0681         return -ENOMEM;
0682     err = do_mkdir(file, mode);
0683     __putname(file);
0684     return err;
0685 }
0686 
0687 static int hostfs_rmdir(struct inode *ino, struct dentry *dentry)
0688 {
0689     char *file;
0690     int err;
0691 
0692     if ((file = dentry_name(dentry)) == NULL)
0693         return -ENOMEM;
0694     err = hostfs_do_rmdir(file);
0695     __putname(file);
0696     return err;
0697 }
0698 
0699 static int hostfs_mknod(struct user_namespace *mnt_userns, struct inode *dir,
0700             struct dentry *dentry, umode_t mode, dev_t dev)
0701 {
0702     struct inode *inode;
0703     char *name;
0704     int err;
0705 
0706     inode = hostfs_iget(dir->i_sb);
0707     if (IS_ERR(inode)) {
0708         err = PTR_ERR(inode);
0709         goto out;
0710     }
0711 
0712     err = -ENOMEM;
0713     name = dentry_name(dentry);
0714     if (name == NULL)
0715         goto out_put;
0716 
0717     err = do_mknod(name, mode, MAJOR(dev), MINOR(dev));
0718     if (err)
0719         goto out_free;
0720 
0721     err = read_name(inode, name);
0722     __putname(name);
0723     if (err)
0724         goto out_put;
0725 
0726     d_instantiate(dentry, inode);
0727     return 0;
0728 
0729  out_free:
0730     __putname(name);
0731  out_put:
0732     iput(inode);
0733  out:
0734     return err;
0735 }
0736 
0737 static int hostfs_rename2(struct user_namespace *mnt_userns,
0738               struct inode *old_dir, struct dentry *old_dentry,
0739               struct inode *new_dir, struct dentry *new_dentry,
0740               unsigned int flags)
0741 {
0742     char *old_name, *new_name;
0743     int err;
0744 
0745     if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
0746         return -EINVAL;
0747 
0748     old_name = dentry_name(old_dentry);
0749     if (old_name == NULL)
0750         return -ENOMEM;
0751     new_name = dentry_name(new_dentry);
0752     if (new_name == NULL) {
0753         __putname(old_name);
0754         return -ENOMEM;
0755     }
0756     if (!flags)
0757         err = rename_file(old_name, new_name);
0758     else
0759         err = rename2_file(old_name, new_name, flags);
0760 
0761     __putname(old_name);
0762     __putname(new_name);
0763     return err;
0764 }
0765 
0766 static int hostfs_permission(struct user_namespace *mnt_userns,
0767                  struct inode *ino, int desired)
0768 {
0769     char *name;
0770     int r = 0, w = 0, x = 0, err;
0771 
0772     if (desired & MAY_NOT_BLOCK)
0773         return -ECHILD;
0774 
0775     if (desired & MAY_READ) r = 1;
0776     if (desired & MAY_WRITE) w = 1;
0777     if (desired & MAY_EXEC) x = 1;
0778     name = inode_name(ino);
0779     if (name == NULL)
0780         return -ENOMEM;
0781 
0782     if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) ||
0783         S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode))
0784         err = 0;
0785     else
0786         err = access_file(name, r, w, x);
0787     __putname(name);
0788     if (!err)
0789         err = generic_permission(&init_user_ns, ino, desired);
0790     return err;
0791 }
0792 
0793 static int hostfs_setattr(struct user_namespace *mnt_userns,
0794               struct dentry *dentry, struct iattr *attr)
0795 {
0796     struct inode *inode = d_inode(dentry);
0797     struct hostfs_iattr attrs;
0798     char *name;
0799     int err;
0800 
0801     int fd = HOSTFS_I(inode)->fd;
0802 
0803     err = setattr_prepare(&init_user_ns, dentry, attr);
0804     if (err)
0805         return err;
0806 
0807     if (append)
0808         attr->ia_valid &= ~ATTR_SIZE;
0809 
0810     attrs.ia_valid = 0;
0811     if (attr->ia_valid & ATTR_MODE) {
0812         attrs.ia_valid |= HOSTFS_ATTR_MODE;
0813         attrs.ia_mode = attr->ia_mode;
0814     }
0815     if (attr->ia_valid & ATTR_UID) {
0816         attrs.ia_valid |= HOSTFS_ATTR_UID;
0817         attrs.ia_uid = from_kuid(&init_user_ns, attr->ia_uid);
0818     }
0819     if (attr->ia_valid & ATTR_GID) {
0820         attrs.ia_valid |= HOSTFS_ATTR_GID;
0821         attrs.ia_gid = from_kgid(&init_user_ns, attr->ia_gid);
0822     }
0823     if (attr->ia_valid & ATTR_SIZE) {
0824         attrs.ia_valid |= HOSTFS_ATTR_SIZE;
0825         attrs.ia_size = attr->ia_size;
0826     }
0827     if (attr->ia_valid & ATTR_ATIME) {
0828         attrs.ia_valid |= HOSTFS_ATTR_ATIME;
0829         attrs.ia_atime = (struct hostfs_timespec)
0830             { attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec };
0831     }
0832     if (attr->ia_valid & ATTR_MTIME) {
0833         attrs.ia_valid |= HOSTFS_ATTR_MTIME;
0834         attrs.ia_mtime = (struct hostfs_timespec)
0835             { attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec };
0836     }
0837     if (attr->ia_valid & ATTR_CTIME) {
0838         attrs.ia_valid |= HOSTFS_ATTR_CTIME;
0839         attrs.ia_ctime = (struct hostfs_timespec)
0840             { attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec };
0841     }
0842     if (attr->ia_valid & ATTR_ATIME_SET) {
0843         attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET;
0844     }
0845     if (attr->ia_valid & ATTR_MTIME_SET) {
0846         attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET;
0847     }
0848     name = dentry_name(dentry);
0849     if (name == NULL)
0850         return -ENOMEM;
0851     err = set_attr(name, &attrs, fd);
0852     __putname(name);
0853     if (err)
0854         return err;
0855 
0856     if ((attr->ia_valid & ATTR_SIZE) &&
0857         attr->ia_size != i_size_read(inode))
0858         truncate_setsize(inode, attr->ia_size);
0859 
0860     setattr_copy(&init_user_ns, inode, attr);
0861     mark_inode_dirty(inode);
0862     return 0;
0863 }
0864 
0865 static const struct inode_operations hostfs_iops = {
0866     .permission = hostfs_permission,
0867     .setattr    = hostfs_setattr,
0868 };
0869 
0870 static const struct inode_operations hostfs_dir_iops = {
0871     .create     = hostfs_create,
0872     .lookup     = hostfs_lookup,
0873     .link       = hostfs_link,
0874     .unlink     = hostfs_unlink,
0875     .symlink    = hostfs_symlink,
0876     .mkdir      = hostfs_mkdir,
0877     .rmdir      = hostfs_rmdir,
0878     .mknod      = hostfs_mknod,
0879     .rename     = hostfs_rename2,
0880     .permission = hostfs_permission,
0881     .setattr    = hostfs_setattr,
0882 };
0883 
0884 static const char *hostfs_get_link(struct dentry *dentry,
0885                    struct inode *inode,
0886                    struct delayed_call *done)
0887 {
0888     char *link;
0889     if (!dentry)
0890         return ERR_PTR(-ECHILD);
0891     link = kmalloc(PATH_MAX, GFP_KERNEL);
0892     if (link) {
0893         char *path = dentry_name(dentry);
0894         int err = -ENOMEM;
0895         if (path) {
0896             err = hostfs_do_readlink(path, link, PATH_MAX);
0897             if (err == PATH_MAX)
0898                 err = -E2BIG;
0899             __putname(path);
0900         }
0901         if (err < 0) {
0902             kfree(link);
0903             return ERR_PTR(err);
0904         }
0905     } else {
0906         return ERR_PTR(-ENOMEM);
0907     }
0908 
0909     set_delayed_call(done, kfree_link, link);
0910     return link;
0911 }
0912 
0913 static const struct inode_operations hostfs_link_iops = {
0914     .get_link   = hostfs_get_link,
0915 };
0916 
0917 static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent)
0918 {
0919     struct inode *root_inode;
0920     char *host_root_path, *req_root = d;
0921     int err;
0922 
0923     sb->s_blocksize = 1024;
0924     sb->s_blocksize_bits = 10;
0925     sb->s_magic = HOSTFS_SUPER_MAGIC;
0926     sb->s_op = &hostfs_sbops;
0927     sb->s_d_op = &simple_dentry_operations;
0928     sb->s_maxbytes = MAX_LFS_FILESIZE;
0929     err = super_setup_bdi(sb);
0930     if (err)
0931         goto out;
0932 
0933     /* NULL is printed as '(null)' by printf(): avoid that. */
0934     if (req_root == NULL)
0935         req_root = "";
0936 
0937     err = -ENOMEM;
0938     sb->s_fs_info = host_root_path =
0939         kasprintf(GFP_KERNEL, "%s/%s", root_ino, req_root);
0940     if (host_root_path == NULL)
0941         goto out;
0942 
0943     root_inode = new_inode(sb);
0944     if (!root_inode)
0945         goto out;
0946 
0947     err = read_name(root_inode, host_root_path);
0948     if (err)
0949         goto out_put;
0950 
0951     if (S_ISLNK(root_inode->i_mode)) {
0952         char *name = follow_link(host_root_path);
0953         if (IS_ERR(name)) {
0954             err = PTR_ERR(name);
0955             goto out_put;
0956         }
0957         err = read_name(root_inode, name);
0958         kfree(name);
0959         if (err)
0960             goto out_put;
0961     }
0962 
0963     err = -ENOMEM;
0964     sb->s_root = d_make_root(root_inode);
0965     if (sb->s_root == NULL)
0966         goto out;
0967 
0968     return 0;
0969 
0970 out_put:
0971     iput(root_inode);
0972 out:
0973     return err;
0974 }
0975 
0976 static struct dentry *hostfs_read_sb(struct file_system_type *type,
0977               int flags, const char *dev_name,
0978               void *data)
0979 {
0980     return mount_nodev(type, flags, data, hostfs_fill_sb_common);
0981 }
0982 
0983 static void hostfs_kill_sb(struct super_block *s)
0984 {
0985     kill_anon_super(s);
0986     kfree(s->s_fs_info);
0987 }
0988 
0989 static struct file_system_type hostfs_type = {
0990     .owner      = THIS_MODULE,
0991     .name       = "hostfs",
0992     .mount      = hostfs_read_sb,
0993     .kill_sb    = hostfs_kill_sb,
0994     .fs_flags   = 0,
0995 };
0996 MODULE_ALIAS_FS("hostfs");
0997 
0998 static int __init init_hostfs(void)
0999 {
1000     hostfs_inode_cache = KMEM_CACHE(hostfs_inode_info, 0);
1001     if (!hostfs_inode_cache)
1002         return -ENOMEM;
1003     return register_filesystem(&hostfs_type);
1004 }
1005 
1006 static void __exit exit_hostfs(void)
1007 {
1008     unregister_filesystem(&hostfs_type);
1009     kmem_cache_destroy(hostfs_inode_cache);
1010 }
1011 
1012 module_init(init_hostfs)
1013 module_exit(exit_hostfs)
1014 MODULE_LICENSE("GPL");