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.
6 * 2003-02-10 Petr Baudis <pasky@ucw.cz>
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/namei.h>
24 struct hostfs_inode_info {
27 struct inode vfs_inode;
28 struct mutex open_mutex;
32 static inline struct hostfs_inode_info *HOSTFS_I(struct inode *inode)
34 return list_entry(inode, struct hostfs_inode_info, vfs_inode);
37 #define FILE_HOSTFS_I(file) HOSTFS_I(file_inode(file))
39 static struct kmem_cache *hostfs_inode_cache;
41 /* Changed in hostfs_args before the kernel starts running */
42 static char *root_ino = "";
43 static int append = 0;
45 static const struct inode_operations hostfs_iops;
46 static const struct inode_operations hostfs_dir_iops;
47 static const struct inode_operations hostfs_link_iops;
50 static int __init hostfs_args(char *options, int *add)
54 ptr = strchr(options, ',');
62 ptr = strchr(options, ',');
65 if (*options != '\0') {
66 if (!strcmp(options, "append"))
68 else printf("hostfs_args - unsupported option - %s\n",
76 __uml_setup("hostfs=", hostfs_args,
77 "hostfs=<root dir>,<flags>,...\n"
78 " This is used to set hostfs parameters. The root directory argument\n"
79 " is used to confine all hostfs mounts to within the specified directory\n"
80 " tree on the host. If this isn't specified, then a user inside UML can\n"
81 " mount anything on the host that's accessible to the user that's running\n"
83 " The only flag currently supported is 'append', which specifies that all\n"
84 " files opened by hostfs will be opened in append mode.\n\n"
88 static char *__dentry_name(struct dentry *dentry, char *name)
90 char *p = dentry_path_raw(dentry, name, PATH_MAX);
94 root = dentry->d_sb->s_fs_info;
102 * This function relies on the fact that dentry_path_raw() will place
103 * the path name at the end of the provided buffer.
105 BUG_ON(p + strlen(p) + 1 != name + PATH_MAX);
107 strscpy(name, root, PATH_MAX);
108 if (len > p - name) {
114 strcpy(name + len, p);
119 static char *dentry_name(struct dentry *dentry)
121 char *name = __getname();
125 return __dentry_name(dentry, name);
128 static char *inode_name(struct inode *ino)
130 struct dentry *dentry;
133 dentry = d_find_alias(ino);
137 name = dentry_name(dentry);
144 static char *follow_link(char *link)
146 char *name, *resolved, *end;
149 name = kmalloc(PATH_MAX, GFP_KERNEL);
155 n = hostfs_do_readlink(link, name, PATH_MAX);
158 else if (n == PATH_MAX) {
166 end = strrchr(link, '/');
172 resolved = kasprintf(GFP_KERNEL, "%s%s", link, name);
173 if (resolved == NULL) {
186 static int hostfs_statfs(struct dentry *dentry, struct kstatfs *sf)
189 * do_statfs uses struct statfs64 internally, but the linux kernel
190 * struct statfs still has 32-bit versions for most of these fields,
191 * so we convert them here
200 err = do_statfs(dentry->d_sb->s_fs_info,
201 &sf->f_bsize, &f_blocks, &f_bfree, &f_bavail, &f_files,
202 &f_ffree, &sf->f_fsid, sizeof(sf->f_fsid),
206 sf->f_blocks = f_blocks;
207 sf->f_bfree = f_bfree;
208 sf->f_bavail = f_bavail;
209 sf->f_files = f_files;
210 sf->f_ffree = f_ffree;
211 sf->f_type = HOSTFS_SUPER_MAGIC;
215 static struct inode *hostfs_alloc_inode(struct super_block *sb)
217 struct hostfs_inode_info *hi;
219 hi = alloc_inode_sb(sb, hostfs_inode_cache, GFP_KERNEL_ACCOUNT);
225 inode_init_once(&hi->vfs_inode);
226 mutex_init(&hi->open_mutex);
227 return &hi->vfs_inode;
230 static void hostfs_evict_inode(struct inode *inode)
232 truncate_inode_pages_final(&inode->i_data);
234 if (HOSTFS_I(inode)->fd != -1) {
235 close_file(&HOSTFS_I(inode)->fd);
236 HOSTFS_I(inode)->fd = -1;
237 HOSTFS_I(inode)->dev = 0;
241 static void hostfs_free_inode(struct inode *inode)
243 kmem_cache_free(hostfs_inode_cache, HOSTFS_I(inode));
246 static int hostfs_show_options(struct seq_file *seq, struct dentry *root)
248 const char *root_path = root->d_sb->s_fs_info;
249 size_t offset = strlen(root_ino) + 1;
251 if (strlen(root_path) > offset)
252 seq_show_option(seq, root_path + offset, NULL);
255 seq_puts(seq, ",append");
260 static const struct super_operations hostfs_sbops = {
261 .alloc_inode = hostfs_alloc_inode,
262 .free_inode = hostfs_free_inode,
263 .drop_inode = generic_delete_inode,
264 .evict_inode = hostfs_evict_inode,
265 .statfs = hostfs_statfs,
266 .show_options = hostfs_show_options,
269 static int hostfs_readdir(struct file *file, struct dir_context *ctx)
273 unsigned long long next, ino;
277 name = dentry_name(file->f_path.dentry);
280 dir = open_dir(name, &error);
286 while ((name = read_dir(dir, &next, &ino, &len, &type)) != NULL) {
287 if (!dir_emit(ctx, name, len, ino, type))
295 static int hostfs_open(struct inode *ino, struct file *file)
302 mode = file->f_mode & (FMODE_READ | FMODE_WRITE);
303 if ((mode & HOSTFS_I(ino)->mode) == mode)
306 mode |= HOSTFS_I(ino)->mode;
311 if (mode & FMODE_READ)
313 if (mode & FMODE_WRITE)
316 name = dentry_name(file_dentry(file));
320 fd = open_file(name, r, w, append);
325 mutex_lock(&HOSTFS_I(ino)->open_mutex);
326 /* somebody else had handled it first? */
327 if ((mode & HOSTFS_I(ino)->mode) == mode) {
328 mutex_unlock(&HOSTFS_I(ino)->open_mutex);
332 if ((mode | HOSTFS_I(ino)->mode) != mode) {
333 mode |= HOSTFS_I(ino)->mode;
334 mutex_unlock(&HOSTFS_I(ino)->open_mutex);
338 if (HOSTFS_I(ino)->fd == -1) {
339 HOSTFS_I(ino)->fd = fd;
341 err = replace_file(fd, HOSTFS_I(ino)->fd);
344 mutex_unlock(&HOSTFS_I(ino)->open_mutex);
348 HOSTFS_I(ino)->mode = mode;
349 mutex_unlock(&HOSTFS_I(ino)->open_mutex);
354 static int hostfs_file_release(struct inode *inode, struct file *file)
356 filemap_write_and_wait(inode->i_mapping);
361 static int hostfs_fsync(struct file *file, loff_t start, loff_t end,
364 struct inode *inode = file->f_mapping->host;
367 ret = file_write_and_wait_range(file, start, end);
372 ret = fsync_file(HOSTFS_I(inode)->fd, datasync);
378 static const struct file_operations hostfs_file_fops = {
379 .llseek = generic_file_llseek,
380 .splice_read = filemap_splice_read,
381 .splice_write = iter_file_splice_write,
382 .read_iter = generic_file_read_iter,
383 .write_iter = generic_file_write_iter,
384 .mmap = generic_file_mmap,
386 .release = hostfs_file_release,
387 .fsync = hostfs_fsync,
390 static const struct file_operations hostfs_dir_fops = {
391 .llseek = generic_file_llseek,
392 .iterate_shared = hostfs_readdir,
393 .read = generic_read_dir,
395 .fsync = hostfs_fsync,
398 static int hostfs_writepage(struct page *page, struct writeback_control *wbc)
400 struct address_space *mapping = page->mapping;
401 struct inode *inode = mapping->host;
403 loff_t base = page_offset(page);
404 int count = PAGE_SIZE;
405 int end_index = inode->i_size >> PAGE_SHIFT;
408 if (page->index >= end_index)
409 count = inode->i_size & (PAGE_SIZE-1);
411 buffer = kmap_local_page(page);
413 err = write_file(HOSTFS_I(inode)->fd, &base, buffer, count);
417 mapping_set_error(mapping, err);
421 if (base > inode->i_size)
422 inode->i_size = base;
427 kunmap_local(buffer);
433 static int hostfs_read_folio(struct file *file, struct folio *folio)
435 struct page *page = &folio->page;
437 loff_t start = page_offset(page);
438 int bytes_read, ret = 0;
440 buffer = kmap_local_page(page);
441 bytes_read = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer,
443 if (bytes_read < 0) {
444 ClearPageUptodate(page);
450 memset(buffer + bytes_read, 0, PAGE_SIZE - bytes_read);
452 ClearPageError(page);
453 SetPageUptodate(page);
456 flush_dcache_page(page);
457 kunmap_local(buffer);
463 static int hostfs_write_begin(struct file *file, struct address_space *mapping,
464 loff_t pos, unsigned len,
465 struct page **pagep, void **fsdata)
467 pgoff_t index = pos >> PAGE_SHIFT;
469 *pagep = grab_cache_page_write_begin(mapping, index);
475 static int hostfs_write_end(struct file *file, struct address_space *mapping,
476 loff_t pos, unsigned len, unsigned copied,
477 struct page *page, void *fsdata)
479 struct inode *inode = mapping->host;
481 unsigned from = pos & (PAGE_SIZE - 1);
484 buffer = kmap_local_page(page);
485 err = write_file(FILE_HOSTFS_I(file)->fd, &pos, buffer + from, copied);
486 kunmap_local(buffer);
488 if (!PageUptodate(page) && err == PAGE_SIZE)
489 SetPageUptodate(page);
492 * If err > 0, write_file has added err to pos, so we are comparing
493 * i_size against the last byte written.
495 if (err > 0 && (pos > inode->i_size))
503 static const struct address_space_operations hostfs_aops = {
504 .writepage = hostfs_writepage,
505 .read_folio = hostfs_read_folio,
506 .dirty_folio = filemap_dirty_folio,
507 .write_begin = hostfs_write_begin,
508 .write_end = hostfs_write_end,
511 static int hostfs_inode_update(struct inode *ino, const struct hostfs_stat *st)
513 set_nlink(ino, st->nlink);
514 i_uid_write(ino, st->uid);
515 i_gid_write(ino, st->gid);
517 (struct timespec64){ st->atime.tv_sec, st->atime.tv_nsec };
519 (struct timespec64){ st->mtime.tv_sec, st->mtime.tv_nsec };
520 inode_set_ctime(ino, st->ctime.tv_sec, st->ctime.tv_nsec);
521 ino->i_size = st->size;
522 ino->i_blocks = st->blocks;
526 static int hostfs_inode_set(struct inode *ino, void *data)
528 struct hostfs_stat *st = data;
531 /* Reencode maj and min with the kernel encoding.*/
532 rdev = MKDEV(st->maj, st->min);
534 switch (st->mode & S_IFMT) {
536 ino->i_op = &hostfs_link_iops;
539 ino->i_op = &hostfs_dir_iops;
540 ino->i_fop = &hostfs_dir_fops;
546 init_special_inode(ino, st->mode & S_IFMT, rdev);
547 ino->i_op = &hostfs_iops;
550 ino->i_op = &hostfs_iops;
551 ino->i_fop = &hostfs_file_fops;
552 ino->i_mapping->a_ops = &hostfs_aops;
558 HOSTFS_I(ino)->dev = st->dev;
559 ino->i_ino = st->ino;
560 ino->i_mode = st->mode;
561 return hostfs_inode_update(ino, st);
564 static int hostfs_inode_test(struct inode *inode, void *data)
566 const struct hostfs_stat *st = data;
568 return inode->i_ino == st->ino && HOSTFS_I(inode)->dev == st->dev;
571 static struct inode *hostfs_iget(struct super_block *sb, char *name)
574 struct hostfs_stat st;
575 int err = stat_file(name, &st, -1);
580 inode = iget5_locked(sb, st.ino, hostfs_inode_test, hostfs_inode_set,
583 return ERR_PTR(-ENOMEM);
585 if (inode->i_state & I_NEW) {
586 unlock_new_inode(inode);
588 spin_lock(&inode->i_lock);
589 hostfs_inode_update(inode, &st);
590 spin_unlock(&inode->i_lock);
596 static int hostfs_create(struct mnt_idmap *idmap, struct inode *dir,
597 struct dentry *dentry, umode_t mode, bool excl)
603 name = dentry_name(dentry);
607 fd = file_create(name, mode & 0777);
613 inode = hostfs_iget(dir->i_sb, name);
616 return PTR_ERR(inode);
618 HOSTFS_I(inode)->fd = fd;
619 HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE;
620 d_instantiate(dentry, inode);
624 static struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry,
627 struct inode *inode = NULL;
630 name = dentry_name(dentry);
632 return ERR_PTR(-ENOMEM);
634 inode = hostfs_iget(ino->i_sb, name);
637 if (PTR_ERR(inode) == -ENOENT)
640 return ERR_CAST(inode);
643 return d_splice_alias(inode, dentry);
646 static int hostfs_link(struct dentry *to, struct inode *ino,
649 char *from_name, *to_name;
652 if ((from_name = dentry_name(from)) == NULL)
654 to_name = dentry_name(to);
655 if (to_name == NULL) {
656 __putname(from_name);
659 err = link_file(to_name, from_name);
660 __putname(from_name);
665 static int hostfs_unlink(struct inode *ino, struct dentry *dentry)
673 if ((file = dentry_name(dentry)) == NULL)
676 err = unlink_file(file);
681 static int hostfs_symlink(struct mnt_idmap *idmap, struct inode *ino,
682 struct dentry *dentry, const char *to)
687 if ((file = dentry_name(dentry)) == NULL)
689 err = make_symlink(file, to);
694 static int hostfs_mkdir(struct mnt_idmap *idmap, struct inode *ino,
695 struct dentry *dentry, umode_t mode)
700 if ((file = dentry_name(dentry)) == NULL)
702 err = do_mkdir(file, mode);
707 static int hostfs_rmdir(struct inode *ino, struct dentry *dentry)
712 if ((file = dentry_name(dentry)) == NULL)
714 err = hostfs_do_rmdir(file);
719 static int hostfs_mknod(struct mnt_idmap *idmap, struct inode *dir,
720 struct dentry *dentry, umode_t mode, dev_t dev)
726 name = dentry_name(dentry);
730 err = do_mknod(name, mode, MAJOR(dev), MINOR(dev));
736 inode = hostfs_iget(dir->i_sb, name);
739 return PTR_ERR(inode);
741 d_instantiate(dentry, inode);
745 static int hostfs_rename2(struct mnt_idmap *idmap,
746 struct inode *old_dir, struct dentry *old_dentry,
747 struct inode *new_dir, struct dentry *new_dentry,
750 char *old_name, *new_name;
753 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
756 old_name = dentry_name(old_dentry);
757 if (old_name == NULL)
759 new_name = dentry_name(new_dentry);
760 if (new_name == NULL) {
765 err = rename_file(old_name, new_name);
767 err = rename2_file(old_name, new_name, flags);
774 static int hostfs_permission(struct mnt_idmap *idmap,
775 struct inode *ino, int desired)
778 int r = 0, w = 0, x = 0, err;
780 if (desired & MAY_NOT_BLOCK)
783 if (desired & MAY_READ) r = 1;
784 if (desired & MAY_WRITE) w = 1;
785 if (desired & MAY_EXEC) x = 1;
786 name = inode_name(ino);
790 if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) ||
791 S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode))
794 err = access_file(name, r, w, x);
797 err = generic_permission(&nop_mnt_idmap, ino, desired);
801 static int hostfs_setattr(struct mnt_idmap *idmap,
802 struct dentry *dentry, struct iattr *attr)
804 struct inode *inode = d_inode(dentry);
805 struct hostfs_iattr attrs;
809 int fd = HOSTFS_I(inode)->fd;
811 err = setattr_prepare(&nop_mnt_idmap, dentry, attr);
816 attr->ia_valid &= ~ATTR_SIZE;
819 if (attr->ia_valid & ATTR_MODE) {
820 attrs.ia_valid |= HOSTFS_ATTR_MODE;
821 attrs.ia_mode = attr->ia_mode;
823 if (attr->ia_valid & ATTR_UID) {
824 attrs.ia_valid |= HOSTFS_ATTR_UID;
825 attrs.ia_uid = from_kuid(&init_user_ns, attr->ia_uid);
827 if (attr->ia_valid & ATTR_GID) {
828 attrs.ia_valid |= HOSTFS_ATTR_GID;
829 attrs.ia_gid = from_kgid(&init_user_ns, attr->ia_gid);
831 if (attr->ia_valid & ATTR_SIZE) {
832 attrs.ia_valid |= HOSTFS_ATTR_SIZE;
833 attrs.ia_size = attr->ia_size;
835 if (attr->ia_valid & ATTR_ATIME) {
836 attrs.ia_valid |= HOSTFS_ATTR_ATIME;
837 attrs.ia_atime = (struct hostfs_timespec)
838 { attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec };
840 if (attr->ia_valid & ATTR_MTIME) {
841 attrs.ia_valid |= HOSTFS_ATTR_MTIME;
842 attrs.ia_mtime = (struct hostfs_timespec)
843 { attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec };
845 if (attr->ia_valid & ATTR_CTIME) {
846 attrs.ia_valid |= HOSTFS_ATTR_CTIME;
847 attrs.ia_ctime = (struct hostfs_timespec)
848 { attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec };
850 if (attr->ia_valid & ATTR_ATIME_SET) {
851 attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET;
853 if (attr->ia_valid & ATTR_MTIME_SET) {
854 attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET;
856 name = dentry_name(dentry);
859 err = set_attr(name, &attrs, fd);
864 if ((attr->ia_valid & ATTR_SIZE) &&
865 attr->ia_size != i_size_read(inode))
866 truncate_setsize(inode, attr->ia_size);
868 setattr_copy(&nop_mnt_idmap, inode, attr);
869 mark_inode_dirty(inode);
873 static const struct inode_operations hostfs_iops = {
874 .permission = hostfs_permission,
875 .setattr = hostfs_setattr,
878 static const struct inode_operations hostfs_dir_iops = {
879 .create = hostfs_create,
880 .lookup = hostfs_lookup,
882 .unlink = hostfs_unlink,
883 .symlink = hostfs_symlink,
884 .mkdir = hostfs_mkdir,
885 .rmdir = hostfs_rmdir,
886 .mknod = hostfs_mknod,
887 .rename = hostfs_rename2,
888 .permission = hostfs_permission,
889 .setattr = hostfs_setattr,
892 static const char *hostfs_get_link(struct dentry *dentry,
894 struct delayed_call *done)
898 return ERR_PTR(-ECHILD);
899 link = kmalloc(PATH_MAX, GFP_KERNEL);
901 char *path = dentry_name(dentry);
904 err = hostfs_do_readlink(path, link, PATH_MAX);
914 return ERR_PTR(-ENOMEM);
917 set_delayed_call(done, kfree_link, link);
921 static const struct inode_operations hostfs_link_iops = {
922 .get_link = hostfs_get_link,
925 static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent)
927 struct inode *root_inode;
928 char *host_root_path, *req_root = d;
931 sb->s_blocksize = 1024;
932 sb->s_blocksize_bits = 10;
933 sb->s_magic = HOSTFS_SUPER_MAGIC;
934 sb->s_op = &hostfs_sbops;
935 sb->s_d_op = &simple_dentry_operations;
936 sb->s_maxbytes = MAX_LFS_FILESIZE;
937 err = super_setup_bdi(sb);
941 /* NULL is printed as '(null)' by printf(): avoid that. */
942 if (req_root == NULL)
945 sb->s_fs_info = host_root_path =
946 kasprintf(GFP_KERNEL, "%s/%s", root_ino, req_root);
947 if (host_root_path == NULL)
950 root_inode = hostfs_iget(sb, host_root_path);
951 if (IS_ERR(root_inode))
952 return PTR_ERR(root_inode);
954 if (S_ISLNK(root_inode->i_mode)) {
958 name = follow_link(host_root_path);
960 return PTR_ERR(name);
962 root_inode = hostfs_iget(sb, name);
964 if (IS_ERR(root_inode))
965 return PTR_ERR(root_inode);
968 sb->s_root = d_make_root(root_inode);
969 if (sb->s_root == NULL)
975 static struct dentry *hostfs_read_sb(struct file_system_type *type,
976 int flags, const char *dev_name,
979 return mount_nodev(type, flags, data, hostfs_fill_sb_common);
982 static void hostfs_kill_sb(struct super_block *s)
988 static struct file_system_type hostfs_type = {
989 .owner = THIS_MODULE,
991 .mount = hostfs_read_sb,
992 .kill_sb = hostfs_kill_sb,
995 MODULE_ALIAS_FS("hostfs");
997 static int __init init_hostfs(void)
999 hostfs_inode_cache = KMEM_CACHE(hostfs_inode_info, 0);
1000 if (!hostfs_inode_cache)
1002 return register_filesystem(&hostfs_type);
1005 static void __exit exit_hostfs(void)
1007 unregister_filesystem(&hostfs_type);
1008 kmem_cache_destroy(hostfs_inode_cache);
1011 module_init(init_hostfs)
1012 module_exit(exit_hostfs)
1013 MODULE_LICENSE("GPL");