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>
18 struct hostfs_inode_info {
22 struct inode vfs_inode;
25 static inline struct hostfs_inode_info *HOSTFS_I(struct inode *inode)
27 return list_entry(inode, struct hostfs_inode_info, vfs_inode);
30 #define FILE_HOSTFS_I(file) HOSTFS_I((file)->f_path.dentry->d_inode)
32 int hostfs_d_delete(struct dentry *dentry)
37 struct dentry_operations hostfs_dentry_ops = {
38 .d_delete = hostfs_d_delete,
41 /* Changed in hostfs_args before the kernel starts running */
42 static char *root_ino = "";
43 static int append = 0;
45 #define HOSTFS_SUPER_MAGIC 0x00c0ffee
47 static const struct inode_operations hostfs_iops;
48 static const struct inode_operations hostfs_dir_iops;
49 static const struct address_space_operations hostfs_link_aops;
52 static int __init hostfs_args(char *options, int *add)
56 ptr = strchr(options, ',');
64 ptr = strchr(options, ',');
67 if (*options != '\0') {
68 if (!strcmp(options, "append"))
70 else printf("hostfs_args - unsupported option - %s\n",
78 __uml_setup("hostfs=", hostfs_args,
79 "hostfs=<root dir>,<flags>,...\n"
80 " This is used to set hostfs parameters. The root directory argument\n"
81 " is used to confine all hostfs mounts to within the specified directory\n"
82 " tree on the host. If this isn't specified, then a user inside UML can\n"
83 " mount anything on the host that's accessible to the user that's running\n"
85 " The only flag currently supported is 'append', which specifies that all\n"
86 " files opened by hostfs will be opened in append mode.\n\n"
90 static char *dentry_name(struct dentry *dentry, int extra)
92 struct dentry *parent;
98 while (parent->d_parent != parent) {
99 len += parent->d_name.len + 1;
100 parent = parent->d_parent;
103 root = HOSTFS_I(parent->d_inode)->host_filename;
105 name = kmalloc(len + extra + 1, GFP_KERNEL);
111 while (parent->d_parent != parent) {
112 len -= parent->d_name.len + 1;
114 strncpy(&name[len + 1], parent->d_name.name,
116 parent = parent->d_parent;
118 strncpy(name, root, strlen(root));
122 static char *inode_name(struct inode *ino, int extra)
124 struct dentry *dentry;
126 dentry = list_entry(ino->i_dentry.next, struct dentry, d_alias);
127 return dentry_name(dentry, extra);
130 static int read_name(struct inode *ino, char *name)
133 * The non-int inode fields are copied into ints by stat_file and
134 * then copied into the inode because passing the actual pointers
135 * in and having them treated as int * breaks on big-endian machines
138 int i_mode, i_nlink, i_blksize;
139 unsigned long long i_size;
140 unsigned long long i_ino;
141 unsigned long long i_blocks;
143 err = stat_file(name, &i_ino, &i_mode, &i_nlink, &ino->i_uid,
144 &ino->i_gid, &i_size, &ino->i_atime, &ino->i_mtime,
145 &ino->i_ctime, &i_blksize, &i_blocks, -1);
150 ino->i_mode = i_mode;
151 ino->i_nlink = i_nlink;
152 ino->i_size = i_size;
153 ino->i_blocks = i_blocks;
157 static char *follow_link(char *link)
160 char *name, *resolved, *end;
165 name = kmalloc(len, GFP_KERNEL);
169 n = do_readlink(link, name, len);
181 end = strrchr(link, '/');
186 len = strlen(link) + strlen(name) + 1;
188 resolved = kmalloc(len, GFP_KERNEL);
189 if (resolved == NULL) {
194 sprintf(resolved, "%s%s", link, name);
205 static int hostfs_read_inode(struct inode *ino)
211 * Unfortunately, we are called from iget() when we don't have a dentry
214 if (list_empty(&ino->i_dentry))
218 name = inode_name(ino, 0);
222 if (file_type(name, NULL, NULL) == OS_TYPE_SYMLINK) {
223 name = follow_link(name);
230 err = read_name(ino, name);
236 static struct inode *hostfs_iget(struct super_block *sb)
241 inode = iget_locked(sb, 0);
243 return ERR_PTR(-ENOMEM);
244 if (inode->i_state & I_NEW) {
245 ret = hostfs_read_inode(inode);
250 unlock_new_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_delete_inode(struct inode *inode)
301 truncate_inode_pages(&inode->i_data, 0);
302 if (HOSTFS_I(inode)->fd != -1) {
303 close_file(&HOSTFS_I(inode)->fd);
304 HOSTFS_I(inode)->fd = -1;
309 static void hostfs_destroy_inode(struct inode *inode)
311 kfree(HOSTFS_I(inode)->host_filename);
314 * XXX: This should not happen, probably. The check is here for
317 if (HOSTFS_I(inode)->fd != -1) {
318 close_file(&HOSTFS_I(inode)->fd);
319 printk(KERN_DEBUG "Closing host fd in .destroy_inode\n");
322 kfree(HOSTFS_I(inode));
325 static const struct super_operations hostfs_sbops = {
326 .alloc_inode = hostfs_alloc_inode,
327 .drop_inode = generic_delete_inode,
328 .delete_inode = hostfs_delete_inode,
329 .destroy_inode = hostfs_destroy_inode,
330 .statfs = hostfs_statfs,
333 int hostfs_readdir(struct file *file, void *ent, filldir_t filldir)
337 unsigned long long next, ino;
340 name = dentry_name(file->f_path.dentry, 0);
343 dir = open_dir(name, &error);
348 while ((name = read_dir(dir, &next, &ino, &len)) != NULL) {
349 error = (*filldir)(ent, name, len, file->f_pos,
358 int hostfs_file_open(struct inode *ino, struct file *file)
361 int mode = 0, r = 0, w = 0, fd;
363 mode = file->f_mode & (FMODE_READ | FMODE_WRITE);
364 if ((mode & HOSTFS_I(ino)->mode) == mode)
368 * The file may already have been opened, but with the wrong access,
369 * so this resets things and reopens the file with the new access.
371 if (HOSTFS_I(ino)->fd != -1) {
372 close_file(&HOSTFS_I(ino)->fd);
373 HOSTFS_I(ino)->fd = -1;
376 HOSTFS_I(ino)->mode |= mode;
377 if (HOSTFS_I(ino)->mode & FMODE_READ)
379 if (HOSTFS_I(ino)->mode & FMODE_WRITE)
384 name = dentry_name(file->f_path.dentry, 0);
388 fd = open_file(name, r, w, append);
392 FILE_HOSTFS_I(file)->fd = fd;
397 int hostfs_fsync(struct file *file, struct dentry *dentry, int datasync)
399 return fsync_file(HOSTFS_I(dentry->d_inode)->fd, datasync);
402 static const struct file_operations hostfs_file_fops = {
403 .llseek = generic_file_llseek,
404 .read = do_sync_read,
405 .splice_read = generic_file_splice_read,
406 .aio_read = generic_file_aio_read,
407 .aio_write = generic_file_aio_write,
408 .write = do_sync_write,
409 .mmap = generic_file_mmap,
410 .open = hostfs_file_open,
412 .fsync = hostfs_fsync,
415 static const struct file_operations hostfs_dir_fops = {
416 .llseek = generic_file_llseek,
417 .readdir = hostfs_readdir,
418 .read = generic_read_dir,
421 int hostfs_writepage(struct page *page, struct writeback_control *wbc)
423 struct address_space *mapping = page->mapping;
424 struct inode *inode = mapping->host;
426 unsigned long long base;
427 int count = PAGE_CACHE_SIZE;
428 int end_index = inode->i_size >> PAGE_CACHE_SHIFT;
431 if (page->index >= end_index)
432 count = inode->i_size & (PAGE_CACHE_SIZE-1);
435 base = ((unsigned long long) page->index) << PAGE_CACHE_SHIFT;
437 err = write_file(HOSTFS_I(inode)->fd, &base, buffer, count);
439 ClearPageUptodate(page);
443 if (base > inode->i_size)
444 inode->i_size = base;
447 ClearPageError(page);
457 int hostfs_readpage(struct file *file, struct page *page)
463 start = (long long) page->index << PAGE_CACHE_SHIFT;
465 err = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer,
470 memset(&buffer[err], 0, PAGE_CACHE_SIZE - err);
472 flush_dcache_page(page);
473 SetPageUptodate(page);
474 if (PageError(page)) ClearPageError(page);
482 int hostfs_write_begin(struct file *file, struct address_space *mapping,
483 loff_t pos, unsigned len, unsigned flags,
484 struct page **pagep, void **fsdata)
486 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
488 *pagep = __grab_cache_page(mapping, index);
494 int hostfs_write_end(struct file *file, struct address_space *mapping,
495 loff_t pos, unsigned len, unsigned copied,
496 struct page *page, void *fsdata)
498 struct inode *inode = mapping->host;
500 unsigned from = pos & (PAGE_CACHE_SIZE - 1);
504 err = write_file(FILE_HOSTFS_I(file)->fd, &pos, buffer + from, copied);
507 if (!PageUptodate(page) && err == PAGE_CACHE_SIZE)
508 SetPageUptodate(page);
511 * If err > 0, write_file has added err to pos, so we are comparing
512 * i_size against the last byte written.
514 if (err > 0 && (pos > inode->i_size))
517 page_cache_release(page);
522 static const struct address_space_operations hostfs_aops = {
523 .writepage = hostfs_writepage,
524 .readpage = hostfs_readpage,
525 .set_page_dirty = __set_page_dirty_nobuffers,
526 .write_begin = hostfs_write_begin,
527 .write_end = hostfs_write_end,
530 static int init_inode(struct inode *inode, struct dentry *dentry)
533 int type, err = -ENOMEM;
538 name = dentry_name(dentry, 0);
541 type = file_type(name, &maj, &min);
542 /* Reencode maj and min with the kernel encoding.*/
543 rdev = MKDEV(maj, min);
546 else type = OS_TYPE_DIR;
549 if (type == OS_TYPE_SYMLINK)
550 inode->i_op = &page_symlink_inode_operations;
551 else if (type == OS_TYPE_DIR)
552 inode->i_op = &hostfs_dir_iops;
553 else inode->i_op = &hostfs_iops;
555 if (type == OS_TYPE_DIR) inode->i_fop = &hostfs_dir_fops;
556 else inode->i_fop = &hostfs_file_fops;
558 if (type == OS_TYPE_SYMLINK)
559 inode->i_mapping->a_ops = &hostfs_link_aops;
560 else inode->i_mapping->a_ops = &hostfs_aops;
563 case OS_TYPE_CHARDEV:
564 init_special_inode(inode, S_IFCHR, rdev);
566 case OS_TYPE_BLOCKDEV:
567 init_special_inode(inode, S_IFBLK, rdev);
570 init_special_inode(inode, S_IFIFO, 0);
573 init_special_inode(inode, S_IFSOCK, 0);
580 int hostfs_create(struct inode *dir, struct dentry *dentry, int mode,
581 struct nameidata *nd)
587 inode = hostfs_iget(dir->i_sb);
589 error = PTR_ERR(inode);
593 error = init_inode(inode, dentry);
598 name = dentry_name(dentry, 0);
602 fd = file_create(name,
603 mode & S_IRUSR, mode & S_IWUSR, mode & S_IXUSR,
604 mode & S_IRGRP, mode & S_IWGRP, mode & S_IXGRP,
605 mode & S_IROTH, mode & S_IWOTH, mode & S_IXOTH);
608 else error = read_name(inode, name);
614 HOSTFS_I(inode)->fd = fd;
615 HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE;
616 d_instantiate(dentry, inode);
625 struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry,
626 struct nameidata *nd)
632 inode = hostfs_iget(ino->i_sb);
634 err = PTR_ERR(inode);
638 err = init_inode(inode, dentry);
643 name = dentry_name(dentry, 0);
647 err = read_name(inode, name);
649 if (err == -ENOENT) {
656 d_add(dentry, inode);
657 dentry->d_op = &hostfs_dentry_ops;
666 static char *inode_dentry_name(struct inode *ino, struct dentry *dentry)
671 file = inode_name(ino, dentry->d_name.len + 1);
676 strncat(file, dentry->d_name.name, dentry->d_name.len);
677 file[len + dentry->d_name.len] = '\0';
681 int hostfs_link(struct dentry *to, struct inode *ino, struct dentry *from)
683 char *from_name, *to_name;
686 if ((from_name = inode_dentry_name(ino, from)) == NULL)
688 to_name = dentry_name(to, 0);
689 if (to_name == NULL) {
693 err = link_file(to_name, from_name);
699 int hostfs_unlink(struct inode *ino, struct dentry *dentry)
704 if ((file = inode_dentry_name(ino, dentry)) == NULL)
709 err = unlink_file(file);
714 int hostfs_symlink(struct inode *ino, struct dentry *dentry, const char *to)
719 if ((file = inode_dentry_name(ino, dentry)) == NULL)
721 err = make_symlink(file, to);
726 int hostfs_mkdir(struct inode *ino, struct dentry *dentry, int mode)
731 if ((file = inode_dentry_name(ino, dentry)) == NULL)
733 err = do_mkdir(file, mode);
738 int hostfs_rmdir(struct inode *ino, struct dentry *dentry)
743 if ((file = inode_dentry_name(ino, dentry)) == NULL)
745 err = do_rmdir(file);
750 int hostfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
756 inode = hostfs_iget(dir->i_sb);
758 err = PTR_ERR(inode);
762 err = init_inode(inode, dentry);
767 name = dentry_name(dentry, 0);
771 init_special_inode(inode, mode, dev);
772 err = do_mknod(name, mode, MAJOR(dev), MINOR(dev));
776 err = read_name(inode, name);
781 d_instantiate(dentry, inode);
792 int hostfs_rename(struct inode *from_ino, struct dentry *from,
793 struct inode *to_ino, struct dentry *to)
795 char *from_name, *to_name;
798 if ((from_name = inode_dentry_name(from_ino, from)) == NULL)
800 if ((to_name = inode_dentry_name(to_ino, to)) == NULL) {
804 err = rename_file(from_name, to_name);
810 int hostfs_permission(struct inode *ino, int desired, struct nameidata *nd)
813 int r = 0, w = 0, x = 0, err;
815 if (desired & MAY_READ) r = 1;
816 if (desired & MAY_WRITE) w = 1;
817 if (desired & MAY_EXEC) x = 1;
818 name = inode_name(ino, 0);
822 if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) ||
823 S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode))
826 err = access_file(name, r, w, x);
829 err = generic_permission(ino, desired, NULL);
833 int hostfs_setattr(struct dentry *dentry, struct iattr *attr)
835 struct hostfs_iattr attrs;
839 int fd = HOSTFS_I(dentry->d_inode)->fd;
841 err = inode_change_ok(dentry->d_inode, attr);
846 attr->ia_valid &= ~ATTR_SIZE;
849 if (attr->ia_valid & ATTR_MODE) {
850 attrs.ia_valid |= HOSTFS_ATTR_MODE;
851 attrs.ia_mode = attr->ia_mode;
853 if (attr->ia_valid & ATTR_UID) {
854 attrs.ia_valid |= HOSTFS_ATTR_UID;
855 attrs.ia_uid = attr->ia_uid;
857 if (attr->ia_valid & ATTR_GID) {
858 attrs.ia_valid |= HOSTFS_ATTR_GID;
859 attrs.ia_gid = attr->ia_gid;
861 if (attr->ia_valid & ATTR_SIZE) {
862 attrs.ia_valid |= HOSTFS_ATTR_SIZE;
863 attrs.ia_size = attr->ia_size;
865 if (attr->ia_valid & ATTR_ATIME) {
866 attrs.ia_valid |= HOSTFS_ATTR_ATIME;
867 attrs.ia_atime = attr->ia_atime;
869 if (attr->ia_valid & ATTR_MTIME) {
870 attrs.ia_valid |= HOSTFS_ATTR_MTIME;
871 attrs.ia_mtime = attr->ia_mtime;
873 if (attr->ia_valid & ATTR_CTIME) {
874 attrs.ia_valid |= HOSTFS_ATTR_CTIME;
875 attrs.ia_ctime = attr->ia_ctime;
877 if (attr->ia_valid & ATTR_ATIME_SET) {
878 attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET;
880 if (attr->ia_valid & ATTR_MTIME_SET) {
881 attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET;
883 name = dentry_name(dentry, 0);
886 err = set_attr(name, &attrs, fd);
891 return inode_setattr(dentry->d_inode, attr);
894 static const struct inode_operations hostfs_iops = {
895 .create = hostfs_create,
897 .unlink = hostfs_unlink,
898 .symlink = hostfs_symlink,
899 .mkdir = hostfs_mkdir,
900 .rmdir = hostfs_rmdir,
901 .mknod = hostfs_mknod,
902 .rename = hostfs_rename,
903 .permission = hostfs_permission,
904 .setattr = hostfs_setattr,
907 static const struct inode_operations hostfs_dir_iops = {
908 .create = hostfs_create,
909 .lookup = hostfs_lookup,
911 .unlink = hostfs_unlink,
912 .symlink = hostfs_symlink,
913 .mkdir = hostfs_mkdir,
914 .rmdir = hostfs_rmdir,
915 .mknod = hostfs_mknod,
916 .rename = hostfs_rename,
917 .permission = hostfs_permission,
918 .setattr = hostfs_setattr,
921 int hostfs_link_readpage(struct file *file, struct page *page)
927 name = inode_name(page->mapping->host, 0);
930 err = do_readlink(name, buffer, PAGE_CACHE_SIZE);
932 if (err == PAGE_CACHE_SIZE)
935 flush_dcache_page(page);
936 SetPageUptodate(page);
937 if (PageError(page)) ClearPageError(page);
945 static const struct address_space_operations hostfs_link_aops = {
946 .readpage = hostfs_link_readpage,
949 static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent)
951 struct inode *root_inode;
952 char *host_root_path, *req_root = d;
955 sb->s_blocksize = 1024;
956 sb->s_blocksize_bits = 10;
957 sb->s_magic = HOSTFS_SUPER_MAGIC;
958 sb->s_op = &hostfs_sbops;
960 /* NULL is printed as <NULL> by sprintf: avoid that. */
961 if (req_root == NULL)
965 host_root_path = kmalloc(strlen(root_ino) + 1
966 + strlen(req_root) + 1, GFP_KERNEL);
967 if (host_root_path == NULL)
970 sprintf(host_root_path, "%s/%s", root_ino, req_root);
972 root_inode = hostfs_iget(sb);
973 if (IS_ERR(root_inode)) {
974 err = PTR_ERR(root_inode);
978 err = init_inode(root_inode, NULL);
982 HOSTFS_I(root_inode)->host_filename = host_root_path;
984 * Avoid that in the error path, iput(root_inode) frees again
985 * host_root_path through hostfs_destroy_inode!
987 host_root_path = NULL;
990 sb->s_root = d_alloc_root(root_inode);
991 if (sb->s_root == NULL)
994 err = hostfs_read_inode(root_inode);
996 /* No iput in this case because the dput does that for us */
1007 kfree(host_root_path);
1012 static int hostfs_read_sb(struct file_system_type *type,
1013 int flags, const char *dev_name,
1014 void *data, struct vfsmount *mnt)
1016 return get_sb_nodev(type, flags, data, hostfs_fill_sb_common, mnt);
1019 static struct file_system_type hostfs_type = {
1020 .owner = THIS_MODULE,
1022 .get_sb = hostfs_read_sb,
1023 .kill_sb = kill_anon_super,
1027 static int __init init_hostfs(void)
1029 return register_filesystem(&hostfs_type);
1032 static void __exit exit_hostfs(void)
1034 unregister_filesystem(&hostfs_type);
1037 module_init(init_hostfs)
1038 module_exit(exit_hostfs)
1039 MODULE_LICENSE("GPL");