2 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
5 * Ported the filesystem routines to 2.5.
10 #include <linux/magic.h>
11 #include <linux/module.h>
13 #include <linux/pagemap.h>
14 #include <linux/statfs.h>
15 #include <linux/slab.h>
16 #include <linux/seq_file.h>
17 #include <linux/writeback.h>
18 #include <linux/mount.h>
19 #include <linux/fs_context.h>
20 #include <linux/fs_parser.h>
21 #include <linux/namei.h>
26 struct hostfs_fs_info {
30 struct hostfs_inode_info {
33 struct inode vfs_inode;
34 struct mutex open_mutex;
38 static inline struct hostfs_inode_info *HOSTFS_I(struct inode *inode)
40 return list_entry(inode, struct hostfs_inode_info, vfs_inode);
43 #define FILE_HOSTFS_I(file) HOSTFS_I(file_inode(file))
45 static struct kmem_cache *hostfs_inode_cache;
47 /* Changed in hostfs_args before the kernel starts running */
48 static char *root_ino = "";
49 static int append = 0;
51 static const struct inode_operations hostfs_iops;
52 static const struct inode_operations hostfs_dir_iops;
53 static const struct inode_operations hostfs_link_iops;
56 static int __init hostfs_args(char *options, int *add)
61 ptr = strchr(options, ',');
69 ptr = strchr(options, ',');
72 if (*options != '\0') {
73 if (!strcmp(options, "append"))
75 else printf("hostfs_args - unsupported option - %s\n",
83 __uml_setup("hostfs=", hostfs_args,
84 "hostfs=<root dir>,<flags>,...\n"
85 " This is used to set hostfs parameters. The root directory argument\n"
86 " is used to confine all hostfs mounts to within the specified directory\n"
87 " tree on the host. If this isn't specified, then a user inside UML can\n"
88 " mount anything on the host that's accessible to the user that's running\n"
90 " The only flag currently supported is 'append', which specifies that all\n"
91 " files opened by hostfs will be opened in append mode.\n\n"
95 static char *__dentry_name(struct dentry *dentry, char *name)
97 char *p = dentry_path_raw(dentry, name, PATH_MAX);
100 struct hostfs_fs_info *fsi;
102 fsi = dentry->d_sb->s_fs_info;
103 root = fsi->host_root_path;
111 * This function relies on the fact that dentry_path_raw() will place
112 * the path name at the end of the provided buffer.
114 BUG_ON(p + strlen(p) + 1 != name + PATH_MAX);
116 strscpy(name, root, PATH_MAX);
117 if (len > p - name) {
123 strcpy(name + len, p);
128 static char *dentry_name(struct dentry *dentry)
130 char *name = __getname();
134 return __dentry_name(dentry, name);
137 static char *inode_name(struct inode *ino)
139 struct dentry *dentry;
142 dentry = d_find_alias(ino);
146 name = dentry_name(dentry);
153 static char *follow_link(char *link)
155 char *name, *resolved, *end;
158 name = kmalloc(PATH_MAX, GFP_KERNEL);
164 n = hostfs_do_readlink(link, name, PATH_MAX);
167 else if (n == PATH_MAX) {
175 end = strrchr(link, '/');
181 resolved = kasprintf(GFP_KERNEL, "%s%s", link, name);
182 if (resolved == NULL) {
195 static int hostfs_statfs(struct dentry *dentry, struct kstatfs *sf)
198 * do_statfs uses struct statfs64 internally, but the linux kernel
199 * struct statfs still has 32-bit versions for most of these fields,
200 * so we convert them here
208 struct hostfs_fs_info *fsi;
210 fsi = dentry->d_sb->s_fs_info;
211 err = do_statfs(fsi->host_root_path,
212 &sf->f_bsize, &f_blocks, &f_bfree, &f_bavail, &f_files,
213 &f_ffree, &sf->f_fsid, sizeof(sf->f_fsid),
217 sf->f_blocks = f_blocks;
218 sf->f_bfree = f_bfree;
219 sf->f_bavail = f_bavail;
220 sf->f_files = f_files;
221 sf->f_ffree = f_ffree;
222 sf->f_type = HOSTFS_SUPER_MAGIC;
226 static struct inode *hostfs_alloc_inode(struct super_block *sb)
228 struct hostfs_inode_info *hi;
230 hi = alloc_inode_sb(sb, hostfs_inode_cache, GFP_KERNEL_ACCOUNT);
236 inode_init_once(&hi->vfs_inode);
237 mutex_init(&hi->open_mutex);
238 return &hi->vfs_inode;
241 static void hostfs_evict_inode(struct inode *inode)
243 truncate_inode_pages_final(&inode->i_data);
245 if (HOSTFS_I(inode)->fd != -1) {
246 close_file(&HOSTFS_I(inode)->fd);
247 HOSTFS_I(inode)->fd = -1;
248 HOSTFS_I(inode)->dev = 0;
252 static void hostfs_free_inode(struct inode *inode)
254 kmem_cache_free(hostfs_inode_cache, HOSTFS_I(inode));
257 static int hostfs_show_options(struct seq_file *seq, struct dentry *root)
259 struct hostfs_fs_info *fsi;
260 const char *root_path;
262 fsi = root->d_sb->s_fs_info;
263 root_path = fsi->host_root_path;
264 size_t offset = strlen(root_ino) + 1;
266 if (strlen(root_path) > offset)
267 seq_show_option(seq, root_path + offset, NULL);
270 seq_puts(seq, ",append");
275 static const struct super_operations hostfs_sbops = {
276 .alloc_inode = hostfs_alloc_inode,
277 .free_inode = hostfs_free_inode,
278 .drop_inode = generic_delete_inode,
279 .evict_inode = hostfs_evict_inode,
280 .statfs = hostfs_statfs,
281 .show_options = hostfs_show_options,
284 static int hostfs_readdir(struct file *file, struct dir_context *ctx)
288 unsigned long long next, ino;
292 name = dentry_name(file->f_path.dentry);
295 dir = open_dir(name, &error);
301 while ((name = read_dir(dir, &next, &ino, &len, &type)) != NULL) {
302 if (!dir_emit(ctx, name, len, ino, type))
310 static int hostfs_open(struct inode *ino, struct file *file)
317 mode = file->f_mode & (FMODE_READ | FMODE_WRITE);
318 if ((mode & HOSTFS_I(ino)->mode) == mode)
321 mode |= HOSTFS_I(ino)->mode;
326 if (mode & FMODE_READ)
328 if (mode & FMODE_WRITE)
331 name = dentry_name(file_dentry(file));
335 fd = open_file(name, r, w, append);
340 mutex_lock(&HOSTFS_I(ino)->open_mutex);
341 /* somebody else had handled it first? */
342 if ((mode & HOSTFS_I(ino)->mode) == mode) {
343 mutex_unlock(&HOSTFS_I(ino)->open_mutex);
347 if ((mode | HOSTFS_I(ino)->mode) != mode) {
348 mode |= HOSTFS_I(ino)->mode;
349 mutex_unlock(&HOSTFS_I(ino)->open_mutex);
353 if (HOSTFS_I(ino)->fd == -1) {
354 HOSTFS_I(ino)->fd = fd;
356 err = replace_file(fd, HOSTFS_I(ino)->fd);
359 mutex_unlock(&HOSTFS_I(ino)->open_mutex);
363 HOSTFS_I(ino)->mode = mode;
364 mutex_unlock(&HOSTFS_I(ino)->open_mutex);
369 static int hostfs_file_release(struct inode *inode, struct file *file)
371 filemap_write_and_wait(inode->i_mapping);
376 static int hostfs_fsync(struct file *file, loff_t start, loff_t end,
379 struct inode *inode = file->f_mapping->host;
382 ret = file_write_and_wait_range(file, start, end);
387 ret = fsync_file(HOSTFS_I(inode)->fd, datasync);
393 static const struct file_operations hostfs_file_fops = {
394 .llseek = generic_file_llseek,
395 .splice_read = filemap_splice_read,
396 .splice_write = iter_file_splice_write,
397 .read_iter = generic_file_read_iter,
398 .write_iter = generic_file_write_iter,
399 .mmap = generic_file_mmap,
401 .release = hostfs_file_release,
402 .fsync = hostfs_fsync,
405 static const struct file_operations hostfs_dir_fops = {
406 .llseek = generic_file_llseek,
407 .iterate_shared = hostfs_readdir,
408 .read = generic_read_dir,
410 .fsync = hostfs_fsync,
413 static int hostfs_writepage(struct page *page, struct writeback_control *wbc)
415 struct address_space *mapping = page->mapping;
416 struct inode *inode = mapping->host;
418 loff_t base = page_offset(page);
419 int count = PAGE_SIZE;
420 int end_index = inode->i_size >> PAGE_SHIFT;
423 if (page->index >= end_index)
424 count = inode->i_size & (PAGE_SIZE-1);
426 buffer = kmap_local_page(page);
428 err = write_file(HOSTFS_I(inode)->fd, &base, buffer, count);
432 mapping_set_error(mapping, err);
436 if (base > inode->i_size)
437 inode->i_size = base;
442 kunmap_local(buffer);
448 static int hostfs_read_folio(struct file *file, struct folio *folio)
451 loff_t start = folio_pos(folio);
452 int bytes_read, ret = 0;
454 buffer = kmap_local_folio(folio, 0);
455 bytes_read = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer,
460 buffer = folio_zero_tail(folio, bytes_read, buffer + bytes_read);
461 kunmap_local(buffer);
463 folio_end_read(folio, ret == 0);
467 static int hostfs_write_begin(struct file *file, struct address_space *mapping,
468 loff_t pos, unsigned len,
469 struct folio **foliop, void **fsdata)
471 pgoff_t index = pos >> PAGE_SHIFT;
473 *foliop = __filemap_get_folio(mapping, index, FGP_WRITEBEGIN,
474 mapping_gfp_mask(mapping));
476 return PTR_ERR(*foliop);
480 static int hostfs_write_end(struct file *file, struct address_space *mapping,
481 loff_t pos, unsigned len, unsigned copied,
482 struct folio *folio, void *fsdata)
484 struct inode *inode = mapping->host;
486 size_t from = offset_in_folio(folio, pos);
489 buffer = kmap_local_folio(folio, from);
490 err = write_file(FILE_HOSTFS_I(file)->fd, &pos, buffer, copied);
491 kunmap_local(buffer);
493 if (!folio_test_uptodate(folio) && err == folio_size(folio))
494 folio_mark_uptodate(folio);
497 * If err > 0, write_file has added err to pos, so we are comparing
498 * i_size against the last byte written.
500 if (err > 0 && (pos > inode->i_size))
508 static const struct address_space_operations hostfs_aops = {
509 .writepage = hostfs_writepage,
510 .read_folio = hostfs_read_folio,
511 .dirty_folio = filemap_dirty_folio,
512 .write_begin = hostfs_write_begin,
513 .write_end = hostfs_write_end,
516 static int hostfs_inode_update(struct inode *ino, const struct hostfs_stat *st)
518 set_nlink(ino, st->nlink);
519 i_uid_write(ino, st->uid);
520 i_gid_write(ino, st->gid);
521 inode_set_atime_to_ts(ino, (struct timespec64){
525 inode_set_mtime_to_ts(ino, (struct timespec64){
529 inode_set_ctime(ino, st->ctime.tv_sec, st->ctime.tv_nsec);
530 ino->i_size = st->size;
531 ino->i_blocks = st->blocks;
535 static int hostfs_inode_set(struct inode *ino, void *data)
537 struct hostfs_stat *st = data;
540 /* Reencode maj and min with the kernel encoding.*/
541 rdev = MKDEV(st->rdev.maj, st->rdev.min);
542 dev = MKDEV(st->dev.maj, st->dev.min);
544 switch (st->mode & S_IFMT) {
546 ino->i_op = &hostfs_link_iops;
549 ino->i_op = &hostfs_dir_iops;
550 ino->i_fop = &hostfs_dir_fops;
556 init_special_inode(ino, st->mode & S_IFMT, rdev);
557 ino->i_op = &hostfs_iops;
560 ino->i_op = &hostfs_iops;
561 ino->i_fop = &hostfs_file_fops;
562 ino->i_mapping->a_ops = &hostfs_aops;
568 HOSTFS_I(ino)->dev = dev;
569 ino->i_ino = st->ino;
570 ino->i_mode = st->mode;
571 return hostfs_inode_update(ino, st);
574 static int hostfs_inode_test(struct inode *inode, void *data)
576 const struct hostfs_stat *st = data;
577 dev_t dev = MKDEV(st->dev.maj, st->dev.min);
579 return inode->i_ino == st->ino && HOSTFS_I(inode)->dev == dev;
582 static struct inode *hostfs_iget(struct super_block *sb, char *name)
585 struct hostfs_stat st;
586 int err = stat_file(name, &st, -1);
591 inode = iget5_locked(sb, st.ino, hostfs_inode_test, hostfs_inode_set,
594 return ERR_PTR(-ENOMEM);
596 if (inode->i_state & I_NEW) {
597 unlock_new_inode(inode);
599 spin_lock(&inode->i_lock);
600 hostfs_inode_update(inode, &st);
601 spin_unlock(&inode->i_lock);
607 static int hostfs_create(struct mnt_idmap *idmap, struct inode *dir,
608 struct dentry *dentry, umode_t mode, bool excl)
614 name = dentry_name(dentry);
618 fd = file_create(name, mode & 0777);
624 inode = hostfs_iget(dir->i_sb, name);
627 return PTR_ERR(inode);
629 HOSTFS_I(inode)->fd = fd;
630 HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE;
631 d_instantiate(dentry, inode);
635 static struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry,
638 struct inode *inode = NULL;
641 name = dentry_name(dentry);
643 return ERR_PTR(-ENOMEM);
645 inode = hostfs_iget(ino->i_sb, name);
647 if (inode == ERR_PTR(-ENOENT))
650 return d_splice_alias(inode, dentry);
653 static int hostfs_link(struct dentry *to, struct inode *ino,
656 char *from_name, *to_name;
659 if ((from_name = dentry_name(from)) == NULL)
661 to_name = dentry_name(to);
662 if (to_name == NULL) {
663 __putname(from_name);
666 err = link_file(to_name, from_name);
667 __putname(from_name);
672 static int hostfs_unlink(struct inode *ino, struct dentry *dentry)
680 if ((file = dentry_name(dentry)) == NULL)
683 err = unlink_file(file);
688 static int hostfs_symlink(struct mnt_idmap *idmap, struct inode *ino,
689 struct dentry *dentry, const char *to)
694 if ((file = dentry_name(dentry)) == NULL)
696 err = make_symlink(file, to);
701 static int hostfs_mkdir(struct mnt_idmap *idmap, struct inode *ino,
702 struct dentry *dentry, umode_t mode)
707 if ((file = dentry_name(dentry)) == NULL)
709 err = do_mkdir(file, mode);
714 static int hostfs_rmdir(struct inode *ino, struct dentry *dentry)
719 if ((file = dentry_name(dentry)) == NULL)
721 err = hostfs_do_rmdir(file);
726 static int hostfs_mknod(struct mnt_idmap *idmap, struct inode *dir,
727 struct dentry *dentry, umode_t mode, dev_t dev)
733 name = dentry_name(dentry);
737 err = do_mknod(name, mode, MAJOR(dev), MINOR(dev));
743 inode = hostfs_iget(dir->i_sb, name);
746 return PTR_ERR(inode);
748 d_instantiate(dentry, inode);
752 static int hostfs_rename2(struct mnt_idmap *idmap,
753 struct inode *old_dir, struct dentry *old_dentry,
754 struct inode *new_dir, struct dentry *new_dentry,
757 char *old_name, *new_name;
760 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
763 old_name = dentry_name(old_dentry);
764 if (old_name == NULL)
766 new_name = dentry_name(new_dentry);
767 if (new_name == NULL) {
772 err = rename_file(old_name, new_name);
774 err = rename2_file(old_name, new_name, flags);
781 static int hostfs_permission(struct mnt_idmap *idmap,
782 struct inode *ino, int desired)
785 int r = 0, w = 0, x = 0, err;
787 if (desired & MAY_NOT_BLOCK)
790 if (desired & MAY_READ) r = 1;
791 if (desired & MAY_WRITE) w = 1;
792 if (desired & MAY_EXEC) x = 1;
793 name = inode_name(ino);
797 if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) ||
798 S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode))
801 err = access_file(name, r, w, x);
804 err = generic_permission(&nop_mnt_idmap, ino, desired);
808 static int hostfs_setattr(struct mnt_idmap *idmap,
809 struct dentry *dentry, struct iattr *attr)
811 struct inode *inode = d_inode(dentry);
812 struct hostfs_iattr attrs;
816 int fd = HOSTFS_I(inode)->fd;
818 err = setattr_prepare(&nop_mnt_idmap, dentry, attr);
823 attr->ia_valid &= ~ATTR_SIZE;
826 if (attr->ia_valid & ATTR_MODE) {
827 attrs.ia_valid |= HOSTFS_ATTR_MODE;
828 attrs.ia_mode = attr->ia_mode;
830 if (attr->ia_valid & ATTR_UID) {
831 attrs.ia_valid |= HOSTFS_ATTR_UID;
832 attrs.ia_uid = from_kuid(&init_user_ns, attr->ia_uid);
834 if (attr->ia_valid & ATTR_GID) {
835 attrs.ia_valid |= HOSTFS_ATTR_GID;
836 attrs.ia_gid = from_kgid(&init_user_ns, attr->ia_gid);
838 if (attr->ia_valid & ATTR_SIZE) {
839 attrs.ia_valid |= HOSTFS_ATTR_SIZE;
840 attrs.ia_size = attr->ia_size;
842 if (attr->ia_valid & ATTR_ATIME) {
843 attrs.ia_valid |= HOSTFS_ATTR_ATIME;
844 attrs.ia_atime = (struct hostfs_timespec)
845 { attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec };
847 if (attr->ia_valid & ATTR_MTIME) {
848 attrs.ia_valid |= HOSTFS_ATTR_MTIME;
849 attrs.ia_mtime = (struct hostfs_timespec)
850 { attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec };
852 if (attr->ia_valid & ATTR_CTIME) {
853 attrs.ia_valid |= HOSTFS_ATTR_CTIME;
854 attrs.ia_ctime = (struct hostfs_timespec)
855 { attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec };
857 if (attr->ia_valid & ATTR_ATIME_SET) {
858 attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET;
860 if (attr->ia_valid & ATTR_MTIME_SET) {
861 attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET;
863 name = dentry_name(dentry);
866 err = set_attr(name, &attrs, fd);
871 if ((attr->ia_valid & ATTR_SIZE) &&
872 attr->ia_size != i_size_read(inode))
873 truncate_setsize(inode, attr->ia_size);
875 setattr_copy(&nop_mnt_idmap, inode, attr);
876 mark_inode_dirty(inode);
880 static const struct inode_operations hostfs_iops = {
881 .permission = hostfs_permission,
882 .setattr = hostfs_setattr,
885 static const struct inode_operations hostfs_dir_iops = {
886 .create = hostfs_create,
887 .lookup = hostfs_lookup,
889 .unlink = hostfs_unlink,
890 .symlink = hostfs_symlink,
891 .mkdir = hostfs_mkdir,
892 .rmdir = hostfs_rmdir,
893 .mknod = hostfs_mknod,
894 .rename = hostfs_rename2,
895 .permission = hostfs_permission,
896 .setattr = hostfs_setattr,
899 static const char *hostfs_get_link(struct dentry *dentry,
901 struct delayed_call *done)
905 return ERR_PTR(-ECHILD);
906 link = kmalloc(PATH_MAX, GFP_KERNEL);
908 char *path = dentry_name(dentry);
911 err = hostfs_do_readlink(path, link, PATH_MAX);
921 return ERR_PTR(-ENOMEM);
924 set_delayed_call(done, kfree_link, link);
928 static const struct inode_operations hostfs_link_iops = {
929 .get_link = hostfs_get_link,
932 static int hostfs_fill_super(struct super_block *sb, struct fs_context *fc)
934 struct hostfs_fs_info *fsi = sb->s_fs_info;
935 struct inode *root_inode;
938 sb->s_blocksize = 1024;
939 sb->s_blocksize_bits = 10;
940 sb->s_magic = HOSTFS_SUPER_MAGIC;
941 sb->s_op = &hostfs_sbops;
942 sb->s_d_op = &simple_dentry_operations;
943 sb->s_maxbytes = MAX_LFS_FILESIZE;
944 err = super_setup_bdi(sb);
948 root_inode = hostfs_iget(sb, fsi->host_root_path);
949 if (IS_ERR(root_inode))
950 return PTR_ERR(root_inode);
952 if (S_ISLNK(root_inode->i_mode)) {
956 name = follow_link(fsi->host_root_path);
958 return PTR_ERR(name);
960 root_inode = hostfs_iget(sb, name);
962 if (IS_ERR(root_inode))
963 return PTR_ERR(root_inode);
966 sb->s_root = d_make_root(root_inode);
967 if (sb->s_root == NULL)
977 static const struct fs_parameter_spec hostfs_param_specs[] = {
978 fsparam_string_empty("hostfs", Opt_hostfs),
982 static int hostfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
984 struct hostfs_fs_info *fsi = fc->s_fs_info;
985 struct fs_parse_result result;
989 opt = fs_parse(fc, hostfs_param_specs, param, &result);
995 host_root = param->string;
998 fsi->host_root_path =
999 kasprintf(GFP_KERNEL, "%s/%s", root_ino, host_root);
1000 if (fsi->host_root_path == NULL)
1008 static int hostfs_parse_monolithic(struct fs_context *fc, void *data)
1010 struct hostfs_fs_info *fsi = fc->s_fs_info;
1011 char *host_root = (char *)data;
1013 /* NULL is printed as '(null)' by printf(): avoid that. */
1014 if (host_root == NULL)
1017 fsi->host_root_path =
1018 kasprintf(GFP_KERNEL, "%s/%s", root_ino, host_root);
1019 if (fsi->host_root_path == NULL)
1025 static int hostfs_fc_get_tree(struct fs_context *fc)
1027 return get_tree_nodev(fc, hostfs_fill_super);
1030 static void hostfs_fc_free(struct fs_context *fc)
1032 struct hostfs_fs_info *fsi = fc->s_fs_info;
1037 kfree(fsi->host_root_path);
1041 static const struct fs_context_operations hostfs_context_ops = {
1042 .parse_monolithic = hostfs_parse_monolithic,
1043 .parse_param = hostfs_parse_param,
1044 .get_tree = hostfs_fc_get_tree,
1045 .free = hostfs_fc_free,
1048 static int hostfs_init_fs_context(struct fs_context *fc)
1050 struct hostfs_fs_info *fsi;
1052 fsi = kzalloc(sizeof(*fsi), GFP_KERNEL);
1056 fc->s_fs_info = fsi;
1057 fc->ops = &hostfs_context_ops;
1061 static void hostfs_kill_sb(struct super_block *s)
1064 kfree(s->s_fs_info);
1067 static struct file_system_type hostfs_type = {
1068 .owner = THIS_MODULE,
1070 .init_fs_context = hostfs_init_fs_context,
1071 .kill_sb = hostfs_kill_sb,
1074 MODULE_ALIAS_FS("hostfs");
1076 static int __init init_hostfs(void)
1078 hostfs_inode_cache = KMEM_CACHE(hostfs_inode_info, 0);
1079 if (!hostfs_inode_cache)
1081 return register_filesystem(&hostfs_type);
1084 static void __exit exit_hostfs(void)
1086 unregister_filesystem(&hostfs_type);
1087 kmem_cache_destroy(hostfs_inode_cache);
1090 module_init(init_hostfs)
1091 module_exit(exit_hostfs)
1092 MODULE_DESCRIPTION("User-Mode Linux Host filesystem");
1093 MODULE_LICENSE("GPL");