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>
9 #include <linux/stddef.h>
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/slab.h>
14 #include <linux/pagemap.h>
15 #include <linux/blkdev.h>
16 #include <linux/list.h>
17 #include <linux/statfs.h>
18 #include <linux/kdev_t.h>
19 #include <asm/uaccess.h>
21 #include "kern_util.h"
25 struct hostfs_inode_info {
29 struct inode vfs_inode;
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)->f_path.dentry->d_inode)
39 int hostfs_d_delete(struct dentry *dentry)
44 struct dentry_operations hostfs_dentry_ops = {
45 .d_delete = hostfs_d_delete,
48 /* Changed in hostfs_args before the kernel starts running */
49 static char *root_ino = "";
50 static int append = 0;
52 #define HOSTFS_SUPER_MAGIC 0x00c0ffee
54 static const struct inode_operations hostfs_iops;
55 static const struct inode_operations hostfs_dir_iops;
56 static const struct address_space_operations hostfs_link_aops;
59 static int __init hostfs_args(char *options, int *add)
63 ptr = strchr(options, ',');
71 ptr = strchr(options, ',');
75 if(!strcmp(options, "append"))
77 else printf("hostfs_args - unsupported option - %s\n",
85 __uml_setup("hostfs=", hostfs_args,
86 "hostfs=<root dir>,<flags>,...\n"
87 " This is used to set hostfs parameters. The root directory argument\n"
88 " is used to confine all hostfs mounts to within the specified directory\n"
89 " tree on the host. If this isn't specified, then a user inside UML can\n"
90 " mount anything on the host that's accessible to the user that's running\n"
92 " The only flag currently supported is 'append', which specifies that all\n"
93 " files opened by hostfs will be opened in append mode.\n\n"
97 static char *dentry_name(struct dentry *dentry, int extra)
99 struct dentry *parent;
105 while(parent->d_parent != parent){
106 len += parent->d_name.len + 1;
107 parent = parent->d_parent;
110 root = HOSTFS_I(parent->d_inode)->host_filename;
112 name = kmalloc(len + extra + 1, GFP_KERNEL);
118 while(parent->d_parent != parent){
119 len -= parent->d_name.len + 1;
121 strncpy(&name[len + 1], parent->d_name.name,
123 parent = parent->d_parent;
125 strncpy(name, root, strlen(root));
129 static char *inode_name(struct inode *ino, int extra)
131 struct dentry *dentry;
133 dentry = list_entry(ino->i_dentry.next, struct dentry, d_alias);
134 return dentry_name(dentry, extra);
137 static int read_name(struct inode *ino, char *name)
139 /* The non-int inode fields are copied into ints by stat_file and
140 * then copied into the inode because passing the actual pointers
141 * in and having them treated as int * breaks on big-endian machines
144 int i_mode, i_nlink, i_blksize;
145 unsigned long long i_size;
146 unsigned long long i_ino;
147 unsigned long long i_blocks;
149 err = stat_file(name, &i_ino, &i_mode, &i_nlink, &ino->i_uid,
150 &ino->i_gid, &i_size, &ino->i_atime, &ino->i_mtime,
151 &ino->i_ctime, &i_blksize, &i_blocks, -1);
156 ino->i_mode = i_mode;
157 ino->i_nlink = i_nlink;
158 ino->i_size = i_size;
159 ino->i_blocks = i_blocks;
163 static char *follow_link(char *link)
166 char *name, *resolved, *end;
171 name = kmalloc(len, GFP_KERNEL);
175 n = do_readlink(link, name, len);
187 end = strrchr(link, '/');
192 len = strlen(link) + strlen(name) + 1;
194 resolved = kmalloc(len, GFP_KERNEL);
195 if(resolved == NULL){
200 sprintf(resolved, "%s%s", link, name);
211 static int read_inode(struct inode *ino)
216 /* Unfortunately, we are called from iget() when we don't have a dentry
219 if(list_empty(&ino->i_dentry))
223 name = inode_name(ino, 0);
227 if(file_type(name, NULL, NULL) == OS_TYPE_SYMLINK){
228 name = follow_link(name);
235 err = read_name(ino, name);
241 int hostfs_statfs(struct dentry *dentry, struct kstatfs *sf)
243 /* do_statfs uses struct statfs64 internally, but the linux kernel
244 * struct statfs still has 32-bit versions for most of these fields,
245 * so we convert them here
254 err = do_statfs(HOSTFS_I(dentry->d_sb->s_root->d_inode)->host_filename,
255 &sf->f_bsize, &f_blocks, &f_bfree, &f_bavail, &f_files,
256 &f_ffree, &sf->f_fsid, sizeof(sf->f_fsid),
257 &sf->f_namelen, sf->f_spare);
260 sf->f_blocks = f_blocks;
261 sf->f_bfree = f_bfree;
262 sf->f_bavail = f_bavail;
263 sf->f_files = f_files;
264 sf->f_ffree = f_ffree;
265 sf->f_type = HOSTFS_SUPER_MAGIC;
269 static struct inode *hostfs_alloc_inode(struct super_block *sb)
271 struct hostfs_inode_info *hi;
273 hi = kmalloc(sizeof(*hi), GFP_KERNEL);
277 *hi = ((struct hostfs_inode_info) { .host_filename = NULL,
280 inode_init_once(&hi->vfs_inode);
281 return &hi->vfs_inode;
284 static void hostfs_delete_inode(struct inode *inode)
286 truncate_inode_pages(&inode->i_data, 0);
287 if(HOSTFS_I(inode)->fd != -1) {
288 close_file(&HOSTFS_I(inode)->fd);
289 HOSTFS_I(inode)->fd = -1;
294 static void hostfs_destroy_inode(struct inode *inode)
296 kfree(HOSTFS_I(inode)->host_filename);
298 /*XXX: This should not happen, probably. The check is here for
299 * additional safety.*/
300 if(HOSTFS_I(inode)->fd != -1) {
301 close_file(&HOSTFS_I(inode)->fd);
302 printk(KERN_DEBUG "Closing host fd in .destroy_inode\n");
305 kfree(HOSTFS_I(inode));
308 static void hostfs_read_inode(struct inode *inode)
313 static const struct super_operations hostfs_sbops = {
314 .alloc_inode = hostfs_alloc_inode,
315 .drop_inode = generic_delete_inode,
316 .delete_inode = hostfs_delete_inode,
317 .destroy_inode = hostfs_destroy_inode,
318 .read_inode = hostfs_read_inode,
319 .statfs = hostfs_statfs,
322 int hostfs_readdir(struct file *file, void *ent, filldir_t filldir)
326 unsigned long long next, ino;
329 name = dentry_name(file->f_path.dentry, 0);
332 dir = open_dir(name, &error);
337 while((name = read_dir(dir, &next, &ino, &len)) != NULL){
338 error = (*filldir)(ent, name, len, file->f_pos,
347 int hostfs_file_open(struct inode *ino, struct file *file)
350 int mode = 0, r = 0, w = 0, fd;
352 mode = file->f_mode & (FMODE_READ | FMODE_WRITE);
353 if((mode & HOSTFS_I(ino)->mode) == mode)
356 /* The file may already have been opened, but with the wrong access,
357 * so this resets things and reopens the file with the new access.
359 if(HOSTFS_I(ino)->fd != -1){
360 close_file(&HOSTFS_I(ino)->fd);
361 HOSTFS_I(ino)->fd = -1;
364 HOSTFS_I(ino)->mode |= mode;
365 if(HOSTFS_I(ino)->mode & FMODE_READ)
367 if(HOSTFS_I(ino)->mode & FMODE_WRITE)
372 name = dentry_name(file->f_path.dentry, 0);
376 fd = open_file(name, r, w, append);
380 FILE_HOSTFS_I(file)->fd = fd;
385 int hostfs_fsync(struct file *file, struct dentry *dentry, int datasync)
387 return fsync_file(HOSTFS_I(dentry->d_inode)->fd, datasync);
390 static const struct file_operations hostfs_file_fops = {
391 .llseek = generic_file_llseek,
392 .read = do_sync_read,
393 .splice_read = generic_file_splice_read,
394 .aio_read = generic_file_aio_read,
395 .aio_write = generic_file_aio_write,
396 .write = do_sync_write,
397 .mmap = generic_file_mmap,
398 .open = hostfs_file_open,
400 .fsync = hostfs_fsync,
403 static const struct file_operations hostfs_dir_fops = {
404 .llseek = generic_file_llseek,
405 .readdir = hostfs_readdir,
406 .read = generic_read_dir,
409 int hostfs_writepage(struct page *page, struct writeback_control *wbc)
411 struct address_space *mapping = page->mapping;
412 struct inode *inode = mapping->host;
414 unsigned long long base;
415 int count = PAGE_CACHE_SIZE;
416 int end_index = inode->i_size >> PAGE_CACHE_SHIFT;
419 if (page->index >= end_index)
420 count = inode->i_size & (PAGE_CACHE_SIZE-1);
423 base = ((unsigned long long) page->index) << PAGE_CACHE_SHIFT;
425 err = write_file(HOSTFS_I(inode)->fd, &base, buffer, count);
427 ClearPageUptodate(page);
431 if (base > inode->i_size)
432 inode->i_size = base;
435 ClearPageError(page);
445 int hostfs_readpage(struct file *file, struct page *page)
451 start = (long long) page->index << PAGE_CACHE_SHIFT;
453 err = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer,
455 if(err < 0) goto out;
457 memset(&buffer[err], 0, PAGE_CACHE_SIZE - err);
459 flush_dcache_page(page);
460 SetPageUptodate(page);
461 if (PageError(page)) ClearPageError(page);
469 int hostfs_write_begin(struct file *file, struct address_space *mapping,
470 loff_t pos, unsigned len, unsigned flags,
471 struct page **pagep, void **fsdata)
473 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
475 *pagep = __grab_cache_page(mapping, index);
481 int hostfs_write_end(struct file *file, struct address_space *mapping,
482 loff_t pos, unsigned len, unsigned copied,
483 struct page *page, void *fsdata)
485 struct inode *inode = mapping->host;
487 unsigned from = pos & (PAGE_CACHE_SIZE - 1);
491 err = write_file(FILE_HOSTFS_I(file)->fd, &pos, buffer + from, copied);
494 if (!PageUptodate(page) && err == PAGE_CACHE_SIZE)
495 SetPageUptodate(page);
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))
503 page_cache_release(page);
508 static const struct address_space_operations hostfs_aops = {
509 .writepage = hostfs_writepage,
510 .readpage = hostfs_readpage,
511 .set_page_dirty = __set_page_dirty_nobuffers,
512 .write_begin = hostfs_write_begin,
513 .write_end = hostfs_write_end,
516 static int init_inode(struct inode *inode, struct dentry *dentry)
519 int type, err = -ENOMEM;
524 name = dentry_name(dentry, 0);
527 type = file_type(name, &maj, &min);
528 /*Reencode maj and min with the kernel encoding.*/
529 rdev = MKDEV(maj, min);
532 else type = OS_TYPE_DIR;
535 if(type == OS_TYPE_SYMLINK)
536 inode->i_op = &page_symlink_inode_operations;
537 else if(type == OS_TYPE_DIR)
538 inode->i_op = &hostfs_dir_iops;
539 else inode->i_op = &hostfs_iops;
541 if(type == OS_TYPE_DIR) inode->i_fop = &hostfs_dir_fops;
542 else inode->i_fop = &hostfs_file_fops;
544 if(type == OS_TYPE_SYMLINK)
545 inode->i_mapping->a_ops = &hostfs_link_aops;
546 else inode->i_mapping->a_ops = &hostfs_aops;
549 case OS_TYPE_CHARDEV:
550 init_special_inode(inode, S_IFCHR, rdev);
552 case OS_TYPE_BLOCKDEV:
553 init_special_inode(inode, S_IFBLK, rdev);
556 init_special_inode(inode, S_IFIFO, 0);
559 init_special_inode(inode, S_IFSOCK, 0);
566 int hostfs_create(struct inode *dir, struct dentry *dentry, int mode,
567 struct nameidata *nd)
574 inode = iget(dir->i_sb, 0);
575 if(inode == NULL) goto out;
577 error = init_inode(inode, dentry);
582 name = dentry_name(dentry, 0);
586 fd = file_create(name,
587 mode & S_IRUSR, mode & S_IWUSR, mode & S_IXUSR,
588 mode & S_IRGRP, mode & S_IWGRP, mode & S_IXGRP,
589 mode & S_IROTH, mode & S_IWOTH, mode & S_IXOTH);
592 else error = read_name(inode, name);
598 HOSTFS_I(inode)->fd = fd;
599 HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE;
600 d_instantiate(dentry, inode);
609 struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry,
610 struct nameidata *nd)
617 inode = iget(ino->i_sb, 0);
621 err = init_inode(inode, dentry);
626 name = dentry_name(dentry, 0);
630 err = read_name(inode, name);
639 d_add(dentry, inode);
640 dentry->d_op = &hostfs_dentry_ops;
649 static char *inode_dentry_name(struct inode *ino, struct dentry *dentry)
654 file = inode_name(ino, dentry->d_name.len + 1);
659 strncat(file, dentry->d_name.name, dentry->d_name.len);
660 file[len + dentry->d_name.len] = '\0';
664 int hostfs_link(struct dentry *to, struct inode *ino, struct dentry *from)
666 char *from_name, *to_name;
669 if((from_name = inode_dentry_name(ino, from)) == NULL)
671 to_name = dentry_name(to, 0);
676 err = link_file(to_name, from_name);
682 int hostfs_unlink(struct inode *ino, struct dentry *dentry)
687 if((file = inode_dentry_name(ino, dentry)) == NULL)
692 err = unlink_file(file);
697 int hostfs_symlink(struct inode *ino, struct dentry *dentry, const char *to)
702 if((file = inode_dentry_name(ino, dentry)) == NULL)
704 err = make_symlink(file, to);
709 int hostfs_mkdir(struct inode *ino, struct dentry *dentry, int mode)
714 if((file = inode_dentry_name(ino, dentry)) == NULL)
716 err = do_mkdir(file, mode);
721 int hostfs_rmdir(struct inode *ino, struct dentry *dentry)
726 if((file = inode_dentry_name(ino, dentry)) == NULL)
728 err = do_rmdir(file);
733 int hostfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
739 inode = iget(dir->i_sb, 0);
743 err = init_inode(inode, dentry);
748 name = dentry_name(dentry, 0);
752 init_special_inode(inode, mode, dev);
753 err = do_mknod(name, mode, MAJOR(dev), MINOR(dev));
757 err = read_name(inode, name);
762 d_instantiate(dentry, inode);
773 int hostfs_rename(struct inode *from_ino, struct dentry *from,
774 struct inode *to_ino, struct dentry *to)
776 char *from_name, *to_name;
779 if((from_name = inode_dentry_name(from_ino, from)) == NULL)
781 if((to_name = inode_dentry_name(to_ino, to)) == NULL){
785 err = rename_file(from_name, to_name);
791 int hostfs_permission(struct inode *ino, int desired, struct nameidata *nd)
794 int r = 0, w = 0, x = 0, err;
796 if (desired & MAY_READ) r = 1;
797 if (desired & MAY_WRITE) w = 1;
798 if (desired & MAY_EXEC) x = 1;
799 name = inode_name(ino, 0);
803 if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) ||
804 S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode))
807 err = access_file(name, r, w, x);
810 err = generic_permission(ino, desired, NULL);
814 int hostfs_setattr(struct dentry *dentry, struct iattr *attr)
816 struct hostfs_iattr attrs;
820 int fd = HOSTFS_I(dentry->d_inode)->fd;
822 err = inode_change_ok(dentry->d_inode, attr);
827 attr->ia_valid &= ~ATTR_SIZE;
830 if(attr->ia_valid & ATTR_MODE){
831 attrs.ia_valid |= HOSTFS_ATTR_MODE;
832 attrs.ia_mode = attr->ia_mode;
834 if(attr->ia_valid & ATTR_UID){
835 attrs.ia_valid |= HOSTFS_ATTR_UID;
836 attrs.ia_uid = attr->ia_uid;
838 if(attr->ia_valid & ATTR_GID){
839 attrs.ia_valid |= HOSTFS_ATTR_GID;
840 attrs.ia_gid = attr->ia_gid;
842 if(attr->ia_valid & ATTR_SIZE){
843 attrs.ia_valid |= HOSTFS_ATTR_SIZE;
844 attrs.ia_size = attr->ia_size;
846 if(attr->ia_valid & ATTR_ATIME){
847 attrs.ia_valid |= HOSTFS_ATTR_ATIME;
848 attrs.ia_atime = attr->ia_atime;
850 if(attr->ia_valid & ATTR_MTIME){
851 attrs.ia_valid |= HOSTFS_ATTR_MTIME;
852 attrs.ia_mtime = attr->ia_mtime;
854 if(attr->ia_valid & ATTR_CTIME){
855 attrs.ia_valid |= HOSTFS_ATTR_CTIME;
856 attrs.ia_ctime = attr->ia_ctime;
858 if(attr->ia_valid & ATTR_ATIME_SET){
859 attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET;
861 if(attr->ia_valid & ATTR_MTIME_SET){
862 attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET;
864 name = dentry_name(dentry, 0);
867 err = set_attr(name, &attrs, fd);
872 return inode_setattr(dentry->d_inode, attr);
875 int hostfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
878 generic_fillattr(dentry->d_inode, stat);
882 static const struct inode_operations hostfs_iops = {
883 .create = hostfs_create,
885 .unlink = hostfs_unlink,
886 .symlink = hostfs_symlink,
887 .mkdir = hostfs_mkdir,
888 .rmdir = hostfs_rmdir,
889 .mknod = hostfs_mknod,
890 .rename = hostfs_rename,
891 .permission = hostfs_permission,
892 .setattr = hostfs_setattr,
893 .getattr = hostfs_getattr,
896 static const struct inode_operations hostfs_dir_iops = {
897 .create = hostfs_create,
898 .lookup = hostfs_lookup,
900 .unlink = hostfs_unlink,
901 .symlink = hostfs_symlink,
902 .mkdir = hostfs_mkdir,
903 .rmdir = hostfs_rmdir,
904 .mknod = hostfs_mknod,
905 .rename = hostfs_rename,
906 .permission = hostfs_permission,
907 .setattr = hostfs_setattr,
908 .getattr = hostfs_getattr,
911 int hostfs_link_readpage(struct file *file, struct page *page)
917 name = inode_name(page->mapping->host, 0);
920 err = do_readlink(name, buffer, PAGE_CACHE_SIZE);
922 if(err == PAGE_CACHE_SIZE)
925 flush_dcache_page(page);
926 SetPageUptodate(page);
927 if (PageError(page)) ClearPageError(page);
935 static const struct address_space_operations hostfs_link_aops = {
936 .readpage = hostfs_link_readpage,
939 static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent)
941 struct inode *root_inode;
942 char *host_root_path, *req_root = d;
945 sb->s_blocksize = 1024;
946 sb->s_blocksize_bits = 10;
947 sb->s_magic = HOSTFS_SUPER_MAGIC;
948 sb->s_op = &hostfs_sbops;
950 /* NULL is printed as <NULL> by sprintf: avoid that. */
951 if (req_root == NULL)
955 host_root_path = kmalloc(strlen(root_ino) + 1
956 + strlen(req_root) + 1, GFP_KERNEL);
957 if(host_root_path == NULL)
960 sprintf(host_root_path, "%s/%s", root_ino, req_root);
962 root_inode = iget(sb, 0);
963 if(root_inode == NULL)
966 err = init_inode(root_inode, NULL);
970 HOSTFS_I(root_inode)->host_filename = host_root_path;
971 /* Avoid that in the error path, iput(root_inode) frees again
972 * host_root_path through hostfs_destroy_inode! */
973 host_root_path = NULL;
976 sb->s_root = d_alloc_root(root_inode);
977 if(sb->s_root == NULL)
980 err = read_inode(root_inode);
982 /* No iput in this case because the dput does that for us */
993 kfree(host_root_path);
998 static int hostfs_read_sb(struct file_system_type *type,
999 int flags, const char *dev_name,
1000 void *data, struct vfsmount *mnt)
1002 return get_sb_nodev(type, flags, data, hostfs_fill_sb_common, mnt);
1005 static struct file_system_type hostfs_type = {
1006 .owner = THIS_MODULE,
1008 .get_sb = hostfs_read_sb,
1009 .kill_sb = kill_anon_super,
1013 static int __init init_hostfs(void)
1015 return register_filesystem(&hostfs_type);
1018 static void __exit exit_hostfs(void)
1020 unregister_filesystem(&hostfs_type);
1023 module_init(init_hostfs)
1024 module_exit(exit_hostfs)
1025 MODULE_LICENSE("GPL");