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/module.h>
12 #include <linux/pagemap.h>
13 #include <linux/statfs.h>
14 #include <linux/slab.h>
15 #include <linux/seq_file.h>
16 #include <linux/mount.h>
21 struct hostfs_inode_info {
25 struct inode vfs_inode;
28 static inline struct hostfs_inode_info *HOSTFS_I(struct inode *inode)
30 return list_entry(inode, struct hostfs_inode_info, vfs_inode);
33 #define FILE_HOSTFS_I(file) HOSTFS_I((file)->f_path.dentry->d_inode)
35 static int hostfs_d_delete(struct dentry *dentry)
40 static const struct dentry_operations hostfs_dentry_ops = {
41 .d_delete = hostfs_d_delete,
44 /* Changed in hostfs_args before the kernel starts running */
45 static char *root_ino = "";
46 static int append = 0;
48 #define HOSTFS_SUPER_MAGIC 0x00c0ffee
50 static const struct inode_operations hostfs_iops;
51 static const struct inode_operations hostfs_dir_iops;
52 static const struct address_space_operations hostfs_link_aops;
55 static int __init hostfs_args(char *options, int *add)
59 ptr = strchr(options, ',');
67 ptr = strchr(options, ',');
70 if (*options != '\0') {
71 if (!strcmp(options, "append"))
73 else printf("hostfs_args - unsupported option - %s\n",
81 __uml_setup("hostfs=", hostfs_args,
82 "hostfs=<root dir>,<flags>,...\n"
83 " This is used to set hostfs parameters. The root directory argument\n"
84 " is used to confine all hostfs mounts to within the specified directory\n"
85 " tree on the host. If this isn't specified, then a user inside UML can\n"
86 " mount anything on the host that's accessible to the user that's running\n"
88 " The only flag currently supported is 'append', which specifies that all\n"
89 " files opened by hostfs will be opened in append mode.\n\n"
93 static char *dentry_name(struct dentry *dentry, int extra)
95 struct dentry *parent;
101 while (parent->d_parent != parent) {
102 len += parent->d_name.len + 1;
103 parent = parent->d_parent;
106 root = HOSTFS_I(parent->d_inode)->host_filename;
108 name = kmalloc(len + extra + 1, GFP_KERNEL);
114 while (parent->d_parent != parent) {
115 len -= parent->d_name.len + 1;
117 strncpy(&name[len + 1], parent->d_name.name,
119 parent = parent->d_parent;
121 strncpy(name, root, strlen(root));
125 static char *inode_name(struct inode *ino, int extra)
127 struct dentry *dentry;
129 dentry = list_entry(ino->i_dentry.next, struct dentry, d_alias);
130 return dentry_name(dentry, extra);
133 static int read_name(struct inode *ino, char *name)
136 * The non-int inode fields are copied into ints by stat_file and
137 * then copied into the inode because passing the actual pointers
138 * in and having them treated as int * breaks on big-endian machines
141 int i_mode, i_nlink, i_blksize;
142 unsigned long long i_size;
143 unsigned long long i_ino;
144 unsigned long long i_blocks;
146 err = stat_file(name, &i_ino, &i_mode, &i_nlink, &ino->i_uid,
147 &ino->i_gid, &i_size, &ino->i_atime, &ino->i_mtime,
148 &ino->i_ctime, &i_blksize, &i_blocks, -1);
153 ino->i_mode = i_mode;
154 ino->i_nlink = i_nlink;
155 ino->i_size = i_size;
156 ino->i_blocks = i_blocks;
160 static char *follow_link(char *link)
163 char *name, *resolved, *end;
168 name = kmalloc(len, GFP_KERNEL);
172 n = hostfs_do_readlink(link, name, len);
184 end = strrchr(link, '/');
189 len = strlen(link) + strlen(name) + 1;
191 resolved = kmalloc(len, GFP_KERNEL);
192 if (resolved == NULL) {
197 sprintf(resolved, "%s%s", link, name);
208 static int hostfs_read_inode(struct inode *ino)
214 * Unfortunately, we are called from iget() when we don't have a dentry
217 if (list_empty(&ino->i_dentry))
221 name = inode_name(ino, 0);
225 if (file_type(name, NULL, NULL) == OS_TYPE_SYMLINK) {
226 name = follow_link(name);
233 err = read_name(ino, name);
239 static struct inode *hostfs_iget(struct super_block *sb)
244 inode = new_inode(sb);
246 return ERR_PTR(-ENOMEM);
247 ret = hostfs_read_inode(inode);
255 int hostfs_statfs(struct dentry *dentry, struct kstatfs *sf)
258 * do_statfs uses struct statfs64 internally, but the linux kernel
259 * struct statfs still has 32-bit versions for most of these fields,
260 * so we convert them here
269 err = do_statfs(HOSTFS_I(dentry->d_sb->s_root->d_inode)->host_filename,
270 &sf->f_bsize, &f_blocks, &f_bfree, &f_bavail, &f_files,
271 &f_ffree, &sf->f_fsid, sizeof(sf->f_fsid),
272 &sf->f_namelen, sf->f_spare);
275 sf->f_blocks = f_blocks;
276 sf->f_bfree = f_bfree;
277 sf->f_bavail = f_bavail;
278 sf->f_files = f_files;
279 sf->f_ffree = f_ffree;
280 sf->f_type = HOSTFS_SUPER_MAGIC;
284 static struct inode *hostfs_alloc_inode(struct super_block *sb)
286 struct hostfs_inode_info *hi;
288 hi = kmalloc(sizeof(*hi), GFP_KERNEL);
292 *hi = ((struct hostfs_inode_info) { .host_filename = NULL,
295 inode_init_once(&hi->vfs_inode);
296 return &hi->vfs_inode;
299 static void hostfs_evict_inode(struct inode *inode)
301 truncate_inode_pages(&inode->i_data, 0);
302 end_writeback(inode);
303 if (HOSTFS_I(inode)->fd != -1) {
304 close_file(&HOSTFS_I(inode)->fd);
305 HOSTFS_I(inode)->fd = -1;
309 static void hostfs_destroy_inode(struct inode *inode)
311 kfree(HOSTFS_I(inode)->host_filename);
312 kfree(HOSTFS_I(inode));
315 static int hostfs_show_options(struct seq_file *seq, struct vfsmount *vfs)
317 struct inode *root = vfs->mnt_sb->s_root->d_inode;
318 const char *root_path = HOSTFS_I(root)->host_filename;
319 size_t offset = strlen(root_ino) + 1;
321 if (strlen(root_path) > offset)
322 seq_printf(seq, ",%s", root_path + offset);
327 static const struct super_operations hostfs_sbops = {
328 .alloc_inode = hostfs_alloc_inode,
329 .destroy_inode = hostfs_destroy_inode,
330 .evict_inode = hostfs_evict_inode,
331 .statfs = hostfs_statfs,
332 .show_options = hostfs_show_options,
335 int hostfs_readdir(struct file *file, void *ent, filldir_t filldir)
339 unsigned long long next, ino;
342 name = dentry_name(file->f_path.dentry, 0);
345 dir = open_dir(name, &error);
350 while ((name = read_dir(dir, &next, &ino, &len)) != NULL) {
351 error = (*filldir)(ent, name, len, file->f_pos,
360 int hostfs_file_open(struct inode *ino, struct file *file)
364 int r = 0, w = 0, fd;
366 mode = file->f_mode & (FMODE_READ | FMODE_WRITE);
367 if ((mode & HOSTFS_I(ino)->mode) == mode)
371 * The file may already have been opened, but with the wrong access,
372 * so this resets things and reopens the file with the new access.
374 if (HOSTFS_I(ino)->fd != -1) {
375 close_file(&HOSTFS_I(ino)->fd);
376 HOSTFS_I(ino)->fd = -1;
379 HOSTFS_I(ino)->mode |= mode;
380 if (HOSTFS_I(ino)->mode & FMODE_READ)
382 if (HOSTFS_I(ino)->mode & FMODE_WRITE)
387 name = dentry_name(file->f_path.dentry, 0);
391 fd = open_file(name, r, w, append);
395 FILE_HOSTFS_I(file)->fd = fd;
400 int hostfs_fsync(struct file *file, int datasync)
402 return fsync_file(HOSTFS_I(file->f_mapping->host)->fd, datasync);
405 static const struct file_operations hostfs_file_fops = {
406 .llseek = generic_file_llseek,
407 .read = do_sync_read,
408 .splice_read = generic_file_splice_read,
409 .aio_read = generic_file_aio_read,
410 .aio_write = generic_file_aio_write,
411 .write = do_sync_write,
412 .mmap = generic_file_mmap,
413 .open = hostfs_file_open,
415 .fsync = hostfs_fsync,
418 static const struct file_operations hostfs_dir_fops = {
419 .llseek = generic_file_llseek,
420 .readdir = hostfs_readdir,
421 .read = generic_read_dir,
424 int hostfs_writepage(struct page *page, struct writeback_control *wbc)
426 struct address_space *mapping = page->mapping;
427 struct inode *inode = mapping->host;
429 unsigned long long base;
430 int count = PAGE_CACHE_SIZE;
431 int end_index = inode->i_size >> PAGE_CACHE_SHIFT;
434 if (page->index >= end_index)
435 count = inode->i_size & (PAGE_CACHE_SIZE-1);
438 base = ((unsigned long long) page->index) << PAGE_CACHE_SHIFT;
440 err = write_file(HOSTFS_I(inode)->fd, &base, buffer, count);
442 ClearPageUptodate(page);
446 if (base > inode->i_size)
447 inode->i_size = base;
450 ClearPageError(page);
460 int hostfs_readpage(struct file *file, struct page *page)
466 start = (long long) page->index << PAGE_CACHE_SHIFT;
468 err = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer,
473 memset(&buffer[err], 0, PAGE_CACHE_SIZE - err);
475 flush_dcache_page(page);
476 SetPageUptodate(page);
477 if (PageError(page)) ClearPageError(page);
485 int hostfs_write_begin(struct file *file, struct address_space *mapping,
486 loff_t pos, unsigned len, unsigned flags,
487 struct page **pagep, void **fsdata)
489 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
491 *pagep = grab_cache_page_write_begin(mapping, index, flags);
497 int hostfs_write_end(struct file *file, struct address_space *mapping,
498 loff_t pos, unsigned len, unsigned copied,
499 struct page *page, void *fsdata)
501 struct inode *inode = mapping->host;
503 unsigned from = pos & (PAGE_CACHE_SIZE - 1);
507 err = write_file(FILE_HOSTFS_I(file)->fd, &pos, buffer + from, copied);
510 if (!PageUptodate(page) && err == PAGE_CACHE_SIZE)
511 SetPageUptodate(page);
514 * If err > 0, write_file has added err to pos, so we are comparing
515 * i_size against the last byte written.
517 if (err > 0 && (pos > inode->i_size))
520 page_cache_release(page);
525 static const struct address_space_operations hostfs_aops = {
526 .writepage = hostfs_writepage,
527 .readpage = hostfs_readpage,
528 .set_page_dirty = __set_page_dirty_nobuffers,
529 .write_begin = hostfs_write_begin,
530 .write_end = hostfs_write_end,
533 static int init_inode(struct inode *inode, struct dentry *dentry)
536 int type, err = -ENOMEM;
541 name = dentry_name(dentry, 0);
544 type = file_type(name, &maj, &min);
545 /* Reencode maj and min with the kernel encoding.*/
546 rdev = MKDEV(maj, min);
549 else type = OS_TYPE_DIR;
552 if (type == OS_TYPE_SYMLINK)
553 inode->i_op = &page_symlink_inode_operations;
554 else if (type == OS_TYPE_DIR)
555 inode->i_op = &hostfs_dir_iops;
556 else inode->i_op = &hostfs_iops;
558 if (type == OS_TYPE_DIR) inode->i_fop = &hostfs_dir_fops;
559 else inode->i_fop = &hostfs_file_fops;
561 if (type == OS_TYPE_SYMLINK)
562 inode->i_mapping->a_ops = &hostfs_link_aops;
563 else inode->i_mapping->a_ops = &hostfs_aops;
566 case OS_TYPE_CHARDEV:
567 init_special_inode(inode, S_IFCHR, rdev);
569 case OS_TYPE_BLOCKDEV:
570 init_special_inode(inode, S_IFBLK, rdev);
573 init_special_inode(inode, S_IFIFO, 0);
576 init_special_inode(inode, S_IFSOCK, 0);
583 int hostfs_create(struct inode *dir, struct dentry *dentry, int mode,
584 struct nameidata *nd)
590 inode = hostfs_iget(dir->i_sb);
592 error = PTR_ERR(inode);
596 error = init_inode(inode, dentry);
601 name = dentry_name(dentry, 0);
605 fd = file_create(name,
606 mode & S_IRUSR, mode & S_IWUSR, mode & S_IXUSR,
607 mode & S_IRGRP, mode & S_IWGRP, mode & S_IXGRP,
608 mode & S_IROTH, mode & S_IWOTH, mode & S_IXOTH);
611 else error = read_name(inode, name);
617 HOSTFS_I(inode)->fd = fd;
618 HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE;
619 d_instantiate(dentry, inode);
628 struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry,
629 struct nameidata *nd)
635 inode = hostfs_iget(ino->i_sb);
637 err = PTR_ERR(inode);
641 err = init_inode(inode, dentry);
646 name = dentry_name(dentry, 0);
650 err = read_name(inode, name);
652 if (err == -ENOENT) {
659 d_add(dentry, inode);
660 dentry->d_op = &hostfs_dentry_ops;
669 static char *inode_dentry_name(struct inode *ino, struct dentry *dentry)
674 file = inode_name(ino, dentry->d_name.len + 1);
679 strncat(file, dentry->d_name.name, dentry->d_name.len);
680 file[len + dentry->d_name.len] = '\0';
684 int hostfs_link(struct dentry *to, struct inode *ino, struct dentry *from)
686 char *from_name, *to_name;
689 if ((from_name = inode_dentry_name(ino, from)) == NULL)
691 to_name = dentry_name(to, 0);
692 if (to_name == NULL) {
696 err = link_file(to_name, from_name);
702 int hostfs_unlink(struct inode *ino, struct dentry *dentry)
707 if ((file = inode_dentry_name(ino, dentry)) == NULL)
712 err = unlink_file(file);
717 int hostfs_symlink(struct inode *ino, struct dentry *dentry, const char *to)
722 if ((file = inode_dentry_name(ino, dentry)) == NULL)
724 err = make_symlink(file, to);
729 int hostfs_mkdir(struct inode *ino, struct dentry *dentry, int mode)
734 if ((file = inode_dentry_name(ino, dentry)) == NULL)
736 err = do_mkdir(file, mode);
741 int hostfs_rmdir(struct inode *ino, struct dentry *dentry)
746 if ((file = inode_dentry_name(ino, dentry)) == NULL)
748 err = do_rmdir(file);
753 int hostfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
759 inode = hostfs_iget(dir->i_sb);
761 err = PTR_ERR(inode);
765 err = init_inode(inode, dentry);
770 name = dentry_name(dentry, 0);
774 init_special_inode(inode, mode, dev);
775 err = do_mknod(name, mode, MAJOR(dev), MINOR(dev));
779 err = read_name(inode, name);
784 d_instantiate(dentry, inode);
795 int hostfs_rename(struct inode *from_ino, struct dentry *from,
796 struct inode *to_ino, struct dentry *to)
798 char *from_name, *to_name;
801 if ((from_name = inode_dentry_name(from_ino, from)) == NULL)
803 if ((to_name = inode_dentry_name(to_ino, to)) == NULL) {
807 err = rename_file(from_name, to_name);
813 int hostfs_permission(struct inode *ino, int desired)
816 int r = 0, w = 0, x = 0, err;
818 if (desired & MAY_READ) r = 1;
819 if (desired & MAY_WRITE) w = 1;
820 if (desired & MAY_EXEC) x = 1;
821 name = inode_name(ino, 0);
825 if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) ||
826 S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode))
829 err = access_file(name, r, w, x);
832 err = generic_permission(ino, desired, NULL);
836 int hostfs_setattr(struct dentry *dentry, struct iattr *attr)
838 struct inode *inode = dentry->d_inode;
839 struct hostfs_iattr attrs;
843 int fd = HOSTFS_I(inode)->fd;
845 err = inode_change_ok(inode, attr);
850 attr->ia_valid &= ~ATTR_SIZE;
853 if (attr->ia_valid & ATTR_MODE) {
854 attrs.ia_valid |= HOSTFS_ATTR_MODE;
855 attrs.ia_mode = attr->ia_mode;
857 if (attr->ia_valid & ATTR_UID) {
858 attrs.ia_valid |= HOSTFS_ATTR_UID;
859 attrs.ia_uid = attr->ia_uid;
861 if (attr->ia_valid & ATTR_GID) {
862 attrs.ia_valid |= HOSTFS_ATTR_GID;
863 attrs.ia_gid = attr->ia_gid;
865 if (attr->ia_valid & ATTR_SIZE) {
866 attrs.ia_valid |= HOSTFS_ATTR_SIZE;
867 attrs.ia_size = attr->ia_size;
869 if (attr->ia_valid & ATTR_ATIME) {
870 attrs.ia_valid |= HOSTFS_ATTR_ATIME;
871 attrs.ia_atime = attr->ia_atime;
873 if (attr->ia_valid & ATTR_MTIME) {
874 attrs.ia_valid |= HOSTFS_ATTR_MTIME;
875 attrs.ia_mtime = attr->ia_mtime;
877 if (attr->ia_valid & ATTR_CTIME) {
878 attrs.ia_valid |= HOSTFS_ATTR_CTIME;
879 attrs.ia_ctime = attr->ia_ctime;
881 if (attr->ia_valid & ATTR_ATIME_SET) {
882 attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET;
884 if (attr->ia_valid & ATTR_MTIME_SET) {
885 attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET;
887 name = dentry_name(dentry, 0);
890 err = set_attr(name, &attrs, fd);
895 if ((attr->ia_valid & ATTR_SIZE) &&
896 attr->ia_size != i_size_read(inode)) {
899 error = vmtruncate(inode, attr->ia_size);
904 setattr_copy(inode, attr);
905 mark_inode_dirty(inode);
909 static const struct inode_operations hostfs_iops = {
910 .create = hostfs_create,
912 .unlink = hostfs_unlink,
913 .symlink = hostfs_symlink,
914 .mkdir = hostfs_mkdir,
915 .rmdir = hostfs_rmdir,
916 .mknod = hostfs_mknod,
917 .rename = hostfs_rename,
918 .permission = hostfs_permission,
919 .setattr = hostfs_setattr,
922 static const struct inode_operations hostfs_dir_iops = {
923 .create = hostfs_create,
924 .lookup = hostfs_lookup,
926 .unlink = hostfs_unlink,
927 .symlink = hostfs_symlink,
928 .mkdir = hostfs_mkdir,
929 .rmdir = hostfs_rmdir,
930 .mknod = hostfs_mknod,
931 .rename = hostfs_rename,
932 .permission = hostfs_permission,
933 .setattr = hostfs_setattr,
936 int hostfs_link_readpage(struct file *file, struct page *page)
942 name = inode_name(page->mapping->host, 0);
945 err = hostfs_do_readlink(name, buffer, PAGE_CACHE_SIZE);
947 if (err == PAGE_CACHE_SIZE)
950 flush_dcache_page(page);
951 SetPageUptodate(page);
952 if (PageError(page)) ClearPageError(page);
960 static const struct address_space_operations hostfs_link_aops = {
961 .readpage = hostfs_link_readpage,
964 static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent)
966 struct inode *root_inode;
967 char *host_root_path, *req_root = d;
970 sb->s_blocksize = 1024;
971 sb->s_blocksize_bits = 10;
972 sb->s_magic = HOSTFS_SUPER_MAGIC;
973 sb->s_op = &hostfs_sbops;
974 sb->s_maxbytes = MAX_LFS_FILESIZE;
976 /* NULL is printed as <NULL> by sprintf: avoid that. */
977 if (req_root == NULL)
981 host_root_path = kmalloc(strlen(root_ino) + 1
982 + strlen(req_root) + 1, GFP_KERNEL);
983 if (host_root_path == NULL)
986 sprintf(host_root_path, "%s/%s", root_ino, req_root);
988 root_inode = hostfs_iget(sb);
989 if (IS_ERR(root_inode)) {
990 err = PTR_ERR(root_inode);
994 err = init_inode(root_inode, NULL);
998 HOSTFS_I(root_inode)->host_filename = host_root_path;
1000 * Avoid that in the error path, iput(root_inode) frees again
1001 * host_root_path through hostfs_destroy_inode!
1003 host_root_path = NULL;
1006 sb->s_root = d_alloc_root(root_inode);
1007 if (sb->s_root == NULL)
1010 err = hostfs_read_inode(root_inode);
1012 /* No iput in this case because the dput does that for us */
1023 kfree(host_root_path);
1028 static int hostfs_read_sb(struct file_system_type *type,
1029 int flags, const char *dev_name,
1030 void *data, struct vfsmount *mnt)
1032 return get_sb_nodev(type, flags, data, hostfs_fill_sb_common, mnt);
1035 static struct file_system_type hostfs_type = {
1036 .owner = THIS_MODULE,
1038 .get_sb = hostfs_read_sb,
1039 .kill_sb = kill_anon_super,
1043 static int __init init_hostfs(void)
1045 return register_filesystem(&hostfs_type);
1048 static void __exit exit_hostfs(void)
1050 unregister_filesystem(&hostfs_type);
1053 module_init(init_hostfs)
1054 module_exit(exit_hostfs)
1055 MODULE_LICENSE("GPL");