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 = iget_locked(sb, 0);
246 return ERR_PTR(-ENOMEM);
247 if (inode->i_state & I_NEW) {
248 ret = hostfs_read_inode(inode);
253 unlock_new_inode(inode);
258 int hostfs_statfs(struct dentry *dentry, struct kstatfs *sf)
261 * do_statfs uses struct statfs64 internally, but the linux kernel
262 * struct statfs still has 32-bit versions for most of these fields,
263 * so we convert them here
272 err = do_statfs(HOSTFS_I(dentry->d_sb->s_root->d_inode)->host_filename,
273 &sf->f_bsize, &f_blocks, &f_bfree, &f_bavail, &f_files,
274 &f_ffree, &sf->f_fsid, sizeof(sf->f_fsid),
275 &sf->f_namelen, sf->f_spare);
278 sf->f_blocks = f_blocks;
279 sf->f_bfree = f_bfree;
280 sf->f_bavail = f_bavail;
281 sf->f_files = f_files;
282 sf->f_ffree = f_ffree;
283 sf->f_type = HOSTFS_SUPER_MAGIC;
287 static struct inode *hostfs_alloc_inode(struct super_block *sb)
289 struct hostfs_inode_info *hi;
291 hi = kmalloc(sizeof(*hi), GFP_KERNEL);
295 *hi = ((struct hostfs_inode_info) { .host_filename = NULL,
298 inode_init_once(&hi->vfs_inode);
299 return &hi->vfs_inode;
302 static void hostfs_delete_inode(struct inode *inode)
304 truncate_inode_pages(&inode->i_data, 0);
305 if (HOSTFS_I(inode)->fd != -1) {
306 close_file(&HOSTFS_I(inode)->fd);
307 HOSTFS_I(inode)->fd = -1;
312 static void hostfs_destroy_inode(struct inode *inode)
314 kfree(HOSTFS_I(inode)->host_filename);
317 * XXX: This should not happen, probably. The check is here for
320 if (HOSTFS_I(inode)->fd != -1) {
321 close_file(&HOSTFS_I(inode)->fd);
322 printk(KERN_DEBUG "Closing host fd in .destroy_inode\n");
325 kfree(HOSTFS_I(inode));
328 static int hostfs_show_options(struct seq_file *seq, struct vfsmount *vfs)
330 struct inode *root = vfs->mnt_sb->s_root->d_inode;
331 const char *root_path = HOSTFS_I(root)->host_filename;
332 size_t offset = strlen(root_ino) + 1;
334 if (strlen(root_path) > offset)
335 seq_printf(seq, ",%s", root_path + offset);
340 static const struct super_operations hostfs_sbops = {
341 .alloc_inode = hostfs_alloc_inode,
342 .drop_inode = generic_delete_inode,
343 .delete_inode = hostfs_delete_inode,
344 .destroy_inode = hostfs_destroy_inode,
345 .statfs = hostfs_statfs,
346 .show_options = hostfs_show_options,
349 int hostfs_readdir(struct file *file, void *ent, filldir_t filldir)
353 unsigned long long next, ino;
356 name = dentry_name(file->f_path.dentry, 0);
359 dir = open_dir(name, &error);
364 while ((name = read_dir(dir, &next, &ino, &len)) != NULL) {
365 error = (*filldir)(ent, name, len, file->f_pos,
374 int hostfs_file_open(struct inode *ino, struct file *file)
378 int r = 0, w = 0, fd;
380 mode = file->f_mode & (FMODE_READ | FMODE_WRITE);
381 if ((mode & HOSTFS_I(ino)->mode) == mode)
385 * The file may already have been opened, but with the wrong access,
386 * so this resets things and reopens the file with the new access.
388 if (HOSTFS_I(ino)->fd != -1) {
389 close_file(&HOSTFS_I(ino)->fd);
390 HOSTFS_I(ino)->fd = -1;
393 HOSTFS_I(ino)->mode |= mode;
394 if (HOSTFS_I(ino)->mode & FMODE_READ)
396 if (HOSTFS_I(ino)->mode & FMODE_WRITE)
401 name = dentry_name(file->f_path.dentry, 0);
405 fd = open_file(name, r, w, append);
409 FILE_HOSTFS_I(file)->fd = fd;
414 int hostfs_fsync(struct file *file, int datasync)
416 return fsync_file(HOSTFS_I(file->f_mapping->host)->fd, datasync);
419 static const struct file_operations hostfs_file_fops = {
420 .llseek = generic_file_llseek,
421 .read = do_sync_read,
422 .splice_read = generic_file_splice_read,
423 .aio_read = generic_file_aio_read,
424 .aio_write = generic_file_aio_write,
425 .write = do_sync_write,
426 .mmap = generic_file_mmap,
427 .open = hostfs_file_open,
429 .fsync = hostfs_fsync,
432 static const struct file_operations hostfs_dir_fops = {
433 .llseek = generic_file_llseek,
434 .readdir = hostfs_readdir,
435 .read = generic_read_dir,
438 int hostfs_writepage(struct page *page, struct writeback_control *wbc)
440 struct address_space *mapping = page->mapping;
441 struct inode *inode = mapping->host;
443 unsigned long long base;
444 int count = PAGE_CACHE_SIZE;
445 int end_index = inode->i_size >> PAGE_CACHE_SHIFT;
448 if (page->index >= end_index)
449 count = inode->i_size & (PAGE_CACHE_SIZE-1);
452 base = ((unsigned long long) page->index) << PAGE_CACHE_SHIFT;
454 err = write_file(HOSTFS_I(inode)->fd, &base, buffer, count);
456 ClearPageUptodate(page);
460 if (base > inode->i_size)
461 inode->i_size = base;
464 ClearPageError(page);
474 int hostfs_readpage(struct file *file, struct page *page)
480 start = (long long) page->index << PAGE_CACHE_SHIFT;
482 err = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer,
487 memset(&buffer[err], 0, PAGE_CACHE_SIZE - err);
489 flush_dcache_page(page);
490 SetPageUptodate(page);
491 if (PageError(page)) ClearPageError(page);
499 int hostfs_write_begin(struct file *file, struct address_space *mapping,
500 loff_t pos, unsigned len, unsigned flags,
501 struct page **pagep, void **fsdata)
503 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
505 *pagep = grab_cache_page_write_begin(mapping, index, flags);
511 int hostfs_write_end(struct file *file, struct address_space *mapping,
512 loff_t pos, unsigned len, unsigned copied,
513 struct page *page, void *fsdata)
515 struct inode *inode = mapping->host;
517 unsigned from = pos & (PAGE_CACHE_SIZE - 1);
521 err = write_file(FILE_HOSTFS_I(file)->fd, &pos, buffer + from, copied);
524 if (!PageUptodate(page) && err == PAGE_CACHE_SIZE)
525 SetPageUptodate(page);
528 * If err > 0, write_file has added err to pos, so we are comparing
529 * i_size against the last byte written.
531 if (err > 0 && (pos > inode->i_size))
534 page_cache_release(page);
539 static const struct address_space_operations hostfs_aops = {
540 .writepage = hostfs_writepage,
541 .readpage = hostfs_readpage,
542 .set_page_dirty = __set_page_dirty_nobuffers,
543 .write_begin = hostfs_write_begin,
544 .write_end = hostfs_write_end,
547 static int init_inode(struct inode *inode, struct dentry *dentry)
550 int type, err = -ENOMEM;
555 name = dentry_name(dentry, 0);
558 type = file_type(name, &maj, &min);
559 /* Reencode maj and min with the kernel encoding.*/
560 rdev = MKDEV(maj, min);
563 else type = OS_TYPE_DIR;
566 if (type == OS_TYPE_SYMLINK)
567 inode->i_op = &page_symlink_inode_operations;
568 else if (type == OS_TYPE_DIR)
569 inode->i_op = &hostfs_dir_iops;
570 else inode->i_op = &hostfs_iops;
572 if (type == OS_TYPE_DIR) inode->i_fop = &hostfs_dir_fops;
573 else inode->i_fop = &hostfs_file_fops;
575 if (type == OS_TYPE_SYMLINK)
576 inode->i_mapping->a_ops = &hostfs_link_aops;
577 else inode->i_mapping->a_ops = &hostfs_aops;
580 case OS_TYPE_CHARDEV:
581 init_special_inode(inode, S_IFCHR, rdev);
583 case OS_TYPE_BLOCKDEV:
584 init_special_inode(inode, S_IFBLK, rdev);
587 init_special_inode(inode, S_IFIFO, 0);
590 init_special_inode(inode, S_IFSOCK, 0);
597 int hostfs_create(struct inode *dir, struct dentry *dentry, int mode,
598 struct nameidata *nd)
604 inode = hostfs_iget(dir->i_sb);
606 error = PTR_ERR(inode);
610 error = init_inode(inode, dentry);
615 name = dentry_name(dentry, 0);
619 fd = file_create(name,
620 mode & S_IRUSR, mode & S_IWUSR, mode & S_IXUSR,
621 mode & S_IRGRP, mode & S_IWGRP, mode & S_IXGRP,
622 mode & S_IROTH, mode & S_IWOTH, mode & S_IXOTH);
625 else error = read_name(inode, name);
631 HOSTFS_I(inode)->fd = fd;
632 HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE;
633 d_instantiate(dentry, inode);
642 struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry,
643 struct nameidata *nd)
649 inode = hostfs_iget(ino->i_sb);
651 err = PTR_ERR(inode);
655 err = init_inode(inode, dentry);
660 name = dentry_name(dentry, 0);
664 err = read_name(inode, name);
666 if (err == -ENOENT) {
673 d_add(dentry, inode);
674 dentry->d_op = &hostfs_dentry_ops;
683 static char *inode_dentry_name(struct inode *ino, struct dentry *dentry)
688 file = inode_name(ino, dentry->d_name.len + 1);
693 strncat(file, dentry->d_name.name, dentry->d_name.len);
694 file[len + dentry->d_name.len] = '\0';
698 int hostfs_link(struct dentry *to, struct inode *ino, struct dentry *from)
700 char *from_name, *to_name;
703 if ((from_name = inode_dentry_name(ino, from)) == NULL)
705 to_name = dentry_name(to, 0);
706 if (to_name == NULL) {
710 err = link_file(to_name, from_name);
716 int hostfs_unlink(struct inode *ino, struct dentry *dentry)
721 if ((file = inode_dentry_name(ino, dentry)) == NULL)
726 err = unlink_file(file);
731 int hostfs_symlink(struct inode *ino, struct dentry *dentry, const char *to)
736 if ((file = inode_dentry_name(ino, dentry)) == NULL)
738 err = make_symlink(file, to);
743 int hostfs_mkdir(struct inode *ino, struct dentry *dentry, int mode)
748 if ((file = inode_dentry_name(ino, dentry)) == NULL)
750 err = do_mkdir(file, mode);
755 int hostfs_rmdir(struct inode *ino, struct dentry *dentry)
760 if ((file = inode_dentry_name(ino, dentry)) == NULL)
762 err = do_rmdir(file);
767 int hostfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
773 inode = hostfs_iget(dir->i_sb);
775 err = PTR_ERR(inode);
779 err = init_inode(inode, dentry);
784 name = dentry_name(dentry, 0);
788 init_special_inode(inode, mode, dev);
789 err = do_mknod(name, mode, MAJOR(dev), MINOR(dev));
793 err = read_name(inode, name);
798 d_instantiate(dentry, inode);
809 int hostfs_rename(struct inode *from_ino, struct dentry *from,
810 struct inode *to_ino, struct dentry *to)
812 char *from_name, *to_name;
815 if ((from_name = inode_dentry_name(from_ino, from)) == NULL)
817 if ((to_name = inode_dentry_name(to_ino, to)) == NULL) {
821 err = rename_file(from_name, to_name);
827 int hostfs_permission(struct inode *ino, int desired)
830 int r = 0, w = 0, x = 0, err;
832 if (desired & MAY_READ) r = 1;
833 if (desired & MAY_WRITE) w = 1;
834 if (desired & MAY_EXEC) x = 1;
835 name = inode_name(ino, 0);
839 if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) ||
840 S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode))
843 err = access_file(name, r, w, x);
846 err = generic_permission(ino, desired, NULL);
850 int hostfs_setattr(struct dentry *dentry, struct iattr *attr)
852 struct inode *inode = dentry->d_inode;
853 struct hostfs_iattr attrs;
857 int fd = HOSTFS_I(inode)->fd;
859 err = inode_change_ok(inode, attr);
864 attr->ia_valid &= ~ATTR_SIZE;
867 if (attr->ia_valid & ATTR_MODE) {
868 attrs.ia_valid |= HOSTFS_ATTR_MODE;
869 attrs.ia_mode = attr->ia_mode;
871 if (attr->ia_valid & ATTR_UID) {
872 attrs.ia_valid |= HOSTFS_ATTR_UID;
873 attrs.ia_uid = attr->ia_uid;
875 if (attr->ia_valid & ATTR_GID) {
876 attrs.ia_valid |= HOSTFS_ATTR_GID;
877 attrs.ia_gid = attr->ia_gid;
879 if (attr->ia_valid & ATTR_SIZE) {
880 attrs.ia_valid |= HOSTFS_ATTR_SIZE;
881 attrs.ia_size = attr->ia_size;
883 if (attr->ia_valid & ATTR_ATIME) {
884 attrs.ia_valid |= HOSTFS_ATTR_ATIME;
885 attrs.ia_atime = attr->ia_atime;
887 if (attr->ia_valid & ATTR_MTIME) {
888 attrs.ia_valid |= HOSTFS_ATTR_MTIME;
889 attrs.ia_mtime = attr->ia_mtime;
891 if (attr->ia_valid & ATTR_CTIME) {
892 attrs.ia_valid |= HOSTFS_ATTR_CTIME;
893 attrs.ia_ctime = attr->ia_ctime;
895 if (attr->ia_valid & ATTR_ATIME_SET) {
896 attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET;
898 if (attr->ia_valid & ATTR_MTIME_SET) {
899 attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET;
901 name = dentry_name(dentry, 0);
904 err = set_attr(name, &attrs, fd);
909 if ((attr->ia_valid & ATTR_SIZE) &&
910 attr->ia_size != i_size_read(inode)) {
913 error = vmtruncate(inode, attr->ia_size);
918 setattr_copy(inode, attr);
919 mark_inode_dirty(inode);
923 static const struct inode_operations hostfs_iops = {
924 .create = hostfs_create,
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 static const struct inode_operations hostfs_dir_iops = {
937 .create = hostfs_create,
938 .lookup = hostfs_lookup,
940 .unlink = hostfs_unlink,
941 .symlink = hostfs_symlink,
942 .mkdir = hostfs_mkdir,
943 .rmdir = hostfs_rmdir,
944 .mknod = hostfs_mknod,
945 .rename = hostfs_rename,
946 .permission = hostfs_permission,
947 .setattr = hostfs_setattr,
950 int hostfs_link_readpage(struct file *file, struct page *page)
956 name = inode_name(page->mapping->host, 0);
959 err = hostfs_do_readlink(name, buffer, PAGE_CACHE_SIZE);
961 if (err == PAGE_CACHE_SIZE)
964 flush_dcache_page(page);
965 SetPageUptodate(page);
966 if (PageError(page)) ClearPageError(page);
974 static const struct address_space_operations hostfs_link_aops = {
975 .readpage = hostfs_link_readpage,
978 static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent)
980 struct inode *root_inode;
981 char *host_root_path, *req_root = d;
984 sb->s_blocksize = 1024;
985 sb->s_blocksize_bits = 10;
986 sb->s_magic = HOSTFS_SUPER_MAGIC;
987 sb->s_op = &hostfs_sbops;
988 sb->s_maxbytes = MAX_LFS_FILESIZE;
990 /* NULL is printed as <NULL> by sprintf: avoid that. */
991 if (req_root == NULL)
995 host_root_path = kmalloc(strlen(root_ino) + 1
996 + strlen(req_root) + 1, GFP_KERNEL);
997 if (host_root_path == NULL)
1000 sprintf(host_root_path, "%s/%s", root_ino, req_root);
1002 root_inode = hostfs_iget(sb);
1003 if (IS_ERR(root_inode)) {
1004 err = PTR_ERR(root_inode);
1008 err = init_inode(root_inode, NULL);
1012 HOSTFS_I(root_inode)->host_filename = host_root_path;
1014 * Avoid that in the error path, iput(root_inode) frees again
1015 * host_root_path through hostfs_destroy_inode!
1017 host_root_path = NULL;
1020 sb->s_root = d_alloc_root(root_inode);
1021 if (sb->s_root == NULL)
1024 err = hostfs_read_inode(root_inode);
1026 /* No iput in this case because the dput does that for us */
1037 kfree(host_root_path);
1042 static int hostfs_read_sb(struct file_system_type *type,
1043 int flags, const char *dev_name,
1044 void *data, struct vfsmount *mnt)
1046 return get_sb_nodev(type, flags, data, hostfs_fill_sb_common, mnt);
1049 static struct file_system_type hostfs_type = {
1050 .owner = THIS_MODULE,
1052 .get_sb = hostfs_read_sb,
1053 .kill_sb = kill_anon_super,
1057 static int __init init_hostfs(void)
1059 return register_filesystem(&hostfs_type);
1062 static void __exit exit_hostfs(void)
1064 unregister_filesystem(&hostfs_type);
1067 module_init(init_hostfs)
1068 module_exit(exit_hostfs)
1069 MODULE_LICENSE("GPL");