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/seq_file.h>
19 struct hostfs_inode_info {
23 struct inode vfs_inode;
26 static inline struct hostfs_inode_info *HOSTFS_I(struct inode *inode)
28 return list_entry(inode, struct hostfs_inode_info, vfs_inode);
31 #define FILE_HOSTFS_I(file) HOSTFS_I((file)->f_path.dentry->d_inode)
33 int hostfs_d_delete(struct dentry *dentry)
38 struct dentry_operations hostfs_dentry_ops = {
39 .d_delete = hostfs_d_delete,
42 /* Changed in hostfs_args before the kernel starts running */
43 static char *root_ino = "";
44 static int append = 0;
46 #define HOSTFS_SUPER_MAGIC 0x00c0ffee
48 static const struct inode_operations hostfs_iops;
49 static const struct inode_operations hostfs_dir_iops;
50 static const struct address_space_operations hostfs_link_aops;
53 static int __init hostfs_args(char *options, int *add)
57 ptr = strchr(options, ',');
65 ptr = strchr(options, ',');
68 if (*options != '\0') {
69 if (!strcmp(options, "append"))
71 else printf("hostfs_args - unsupported option - %s\n",
79 __uml_setup("hostfs=", hostfs_args,
80 "hostfs=<root dir>,<flags>,...\n"
81 " This is used to set hostfs parameters. The root directory argument\n"
82 " is used to confine all hostfs mounts to within the specified directory\n"
83 " tree on the host. If this isn't specified, then a user inside UML can\n"
84 " mount anything on the host that's accessible to the user that's running\n"
86 " The only flag currently supported is 'append', which specifies that all\n"
87 " files opened by hostfs will be opened in append mode.\n\n"
91 static char *dentry_name(struct dentry *dentry, int extra)
93 struct dentry *parent;
99 while (parent->d_parent != parent) {
100 len += parent->d_name.len + 1;
101 parent = parent->d_parent;
104 root = HOSTFS_I(parent->d_inode)->host_filename;
106 name = kmalloc(len + extra + 1, GFP_KERNEL);
112 while (parent->d_parent != parent) {
113 len -= parent->d_name.len + 1;
115 strncpy(&name[len + 1], parent->d_name.name,
117 parent = parent->d_parent;
119 strncpy(name, root, strlen(root));
123 static char *inode_name(struct inode *ino, int extra)
125 struct dentry *dentry;
127 dentry = list_entry(ino->i_dentry.next, struct dentry, d_alias);
128 return dentry_name(dentry, extra);
131 static int read_name(struct inode *ino, char *name)
134 * The non-int inode fields are copied into ints by stat_file and
135 * then copied into the inode because passing the actual pointers
136 * in and having them treated as int * breaks on big-endian machines
139 int i_mode, i_nlink, i_blksize;
140 unsigned long long i_size;
141 unsigned long long i_ino;
142 unsigned long long i_blocks;
144 err = stat_file(name, &i_ino, &i_mode, &i_nlink, &ino->i_uid,
145 &ino->i_gid, &i_size, &ino->i_atime, &ino->i_mtime,
146 &ino->i_ctime, &i_blksize, &i_blocks, -1);
151 ino->i_mode = i_mode;
152 ino->i_nlink = i_nlink;
153 ino->i_size = i_size;
154 ino->i_blocks = i_blocks;
158 static char *follow_link(char *link)
161 char *name, *resolved, *end;
166 name = kmalloc(len, GFP_KERNEL);
170 n = do_readlink(link, name, len);
182 end = strrchr(link, '/');
187 len = strlen(link) + strlen(name) + 1;
189 resolved = kmalloc(len, GFP_KERNEL);
190 if (resolved == NULL) {
195 sprintf(resolved, "%s%s", link, name);
206 static int hostfs_read_inode(struct inode *ino)
212 * Unfortunately, we are called from iget() when we don't have a dentry
215 if (list_empty(&ino->i_dentry))
219 name = inode_name(ino, 0);
223 if (file_type(name, NULL, NULL) == OS_TYPE_SYMLINK) {
224 name = follow_link(name);
231 err = read_name(ino, name);
237 static struct inode *hostfs_iget(struct super_block *sb)
242 inode = iget_locked(sb, 0);
244 return ERR_PTR(-ENOMEM);
245 if (inode->i_state & I_NEW) {
246 ret = hostfs_read_inode(inode);
251 unlock_new_inode(inode);
256 int hostfs_statfs(struct dentry *dentry, struct kstatfs *sf)
259 * do_statfs uses struct statfs64 internally, but the linux kernel
260 * struct statfs still has 32-bit versions for most of these fields,
261 * so we convert them here
270 err = do_statfs(HOSTFS_I(dentry->d_sb->s_root->d_inode)->host_filename,
271 &sf->f_bsize, &f_blocks, &f_bfree, &f_bavail, &f_files,
272 &f_ffree, &sf->f_fsid, sizeof(sf->f_fsid),
273 &sf->f_namelen, sf->f_spare);
276 sf->f_blocks = f_blocks;
277 sf->f_bfree = f_bfree;
278 sf->f_bavail = f_bavail;
279 sf->f_files = f_files;
280 sf->f_ffree = f_ffree;
281 sf->f_type = HOSTFS_SUPER_MAGIC;
285 static struct inode *hostfs_alloc_inode(struct super_block *sb)
287 struct hostfs_inode_info *hi;
289 hi = kmalloc(sizeof(*hi), GFP_KERNEL);
293 *hi = ((struct hostfs_inode_info) { .host_filename = NULL,
296 inode_init_once(&hi->vfs_inode);
297 return &hi->vfs_inode;
300 static void hostfs_delete_inode(struct inode *inode)
302 truncate_inode_pages(&inode->i_data, 0);
303 if (HOSTFS_I(inode)->fd != -1) {
304 close_file(&HOSTFS_I(inode)->fd);
305 HOSTFS_I(inode)->fd = -1;
310 static void hostfs_destroy_inode(struct inode *inode)
312 kfree(HOSTFS_I(inode)->host_filename);
315 * XXX: This should not happen, probably. The check is here for
318 if (HOSTFS_I(inode)->fd != -1) {
319 close_file(&HOSTFS_I(inode)->fd);
320 printk(KERN_DEBUG "Closing host fd in .destroy_inode\n");
323 kfree(HOSTFS_I(inode));
326 static int hostfs_show_options(struct seq_file *seq, struct vfsmount *vfs)
328 struct inode *root = vfs->mnt_sb->s_root->d_inode;
329 const char *root_path = HOSTFS_I(root)->host_filename;
330 size_t offset = strlen(root_ino) + 1;
332 if (strlen(root_path) > offset)
333 seq_printf(seq, ",%s", root_path + offset);
338 static const struct super_operations hostfs_sbops = {
339 .alloc_inode = hostfs_alloc_inode,
340 .drop_inode = generic_delete_inode,
341 .delete_inode = hostfs_delete_inode,
342 .destroy_inode = hostfs_destroy_inode,
343 .statfs = hostfs_statfs,
344 .show_options = hostfs_show_options,
347 int hostfs_readdir(struct file *file, void *ent, filldir_t filldir)
351 unsigned long long next, ino;
354 name = dentry_name(file->f_path.dentry, 0);
357 dir = open_dir(name, &error);
362 while ((name = read_dir(dir, &next, &ino, &len)) != NULL) {
363 error = (*filldir)(ent, name, len, file->f_pos,
372 int hostfs_file_open(struct inode *ino, struct file *file)
375 int mode = 0, r = 0, w = 0, fd;
377 mode = file->f_mode & (FMODE_READ | FMODE_WRITE);
378 if ((mode & HOSTFS_I(ino)->mode) == mode)
382 * The file may already have been opened, but with the wrong access,
383 * so this resets things and reopens the file with the new access.
385 if (HOSTFS_I(ino)->fd != -1) {
386 close_file(&HOSTFS_I(ino)->fd);
387 HOSTFS_I(ino)->fd = -1;
390 HOSTFS_I(ino)->mode |= mode;
391 if (HOSTFS_I(ino)->mode & FMODE_READ)
393 if (HOSTFS_I(ino)->mode & FMODE_WRITE)
398 name = dentry_name(file->f_path.dentry, 0);
402 fd = open_file(name, r, w, append);
406 FILE_HOSTFS_I(file)->fd = fd;
411 int hostfs_fsync(struct file *file, struct dentry *dentry, int datasync)
413 return fsync_file(HOSTFS_I(dentry->d_inode)->fd, datasync);
416 static const struct file_operations hostfs_file_fops = {
417 .llseek = generic_file_llseek,
418 .read = do_sync_read,
419 .splice_read = generic_file_splice_read,
420 .aio_read = generic_file_aio_read,
421 .aio_write = generic_file_aio_write,
422 .write = do_sync_write,
423 .mmap = generic_file_mmap,
424 .open = hostfs_file_open,
426 .fsync = hostfs_fsync,
429 static const struct file_operations hostfs_dir_fops = {
430 .llseek = generic_file_llseek,
431 .readdir = hostfs_readdir,
432 .read = generic_read_dir,
435 int hostfs_writepage(struct page *page, struct writeback_control *wbc)
437 struct address_space *mapping = page->mapping;
438 struct inode *inode = mapping->host;
440 unsigned long long base;
441 int count = PAGE_CACHE_SIZE;
442 int end_index = inode->i_size >> PAGE_CACHE_SHIFT;
445 if (page->index >= end_index)
446 count = inode->i_size & (PAGE_CACHE_SIZE-1);
449 base = ((unsigned long long) page->index) << PAGE_CACHE_SHIFT;
451 err = write_file(HOSTFS_I(inode)->fd, &base, buffer, count);
453 ClearPageUptodate(page);
457 if (base > inode->i_size)
458 inode->i_size = base;
461 ClearPageError(page);
471 int hostfs_readpage(struct file *file, struct page *page)
477 start = (long long) page->index << PAGE_CACHE_SHIFT;
479 err = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer,
484 memset(&buffer[err], 0, PAGE_CACHE_SIZE - err);
486 flush_dcache_page(page);
487 SetPageUptodate(page);
488 if (PageError(page)) ClearPageError(page);
496 int hostfs_write_begin(struct file *file, struct address_space *mapping,
497 loff_t pos, unsigned len, unsigned flags,
498 struct page **pagep, void **fsdata)
500 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
502 *pagep = __grab_cache_page(mapping, index);
508 int hostfs_write_end(struct file *file, struct address_space *mapping,
509 loff_t pos, unsigned len, unsigned copied,
510 struct page *page, void *fsdata)
512 struct inode *inode = mapping->host;
514 unsigned from = pos & (PAGE_CACHE_SIZE - 1);
518 err = write_file(FILE_HOSTFS_I(file)->fd, &pos, buffer + from, copied);
521 if (!PageUptodate(page) && err == PAGE_CACHE_SIZE)
522 SetPageUptodate(page);
525 * If err > 0, write_file has added err to pos, so we are comparing
526 * i_size against the last byte written.
528 if (err > 0 && (pos > inode->i_size))
531 page_cache_release(page);
536 static const struct address_space_operations hostfs_aops = {
537 .writepage = hostfs_writepage,
538 .readpage = hostfs_readpage,
539 .set_page_dirty = __set_page_dirty_nobuffers,
540 .write_begin = hostfs_write_begin,
541 .write_end = hostfs_write_end,
544 static int init_inode(struct inode *inode, struct dentry *dentry)
547 int type, err = -ENOMEM;
552 name = dentry_name(dentry, 0);
555 type = file_type(name, &maj, &min);
556 /* Reencode maj and min with the kernel encoding.*/
557 rdev = MKDEV(maj, min);
560 else type = OS_TYPE_DIR;
563 if (type == OS_TYPE_SYMLINK)
564 inode->i_op = &page_symlink_inode_operations;
565 else if (type == OS_TYPE_DIR)
566 inode->i_op = &hostfs_dir_iops;
567 else inode->i_op = &hostfs_iops;
569 if (type == OS_TYPE_DIR) inode->i_fop = &hostfs_dir_fops;
570 else inode->i_fop = &hostfs_file_fops;
572 if (type == OS_TYPE_SYMLINK)
573 inode->i_mapping->a_ops = &hostfs_link_aops;
574 else inode->i_mapping->a_ops = &hostfs_aops;
577 case OS_TYPE_CHARDEV:
578 init_special_inode(inode, S_IFCHR, rdev);
580 case OS_TYPE_BLOCKDEV:
581 init_special_inode(inode, S_IFBLK, rdev);
584 init_special_inode(inode, S_IFIFO, 0);
587 init_special_inode(inode, S_IFSOCK, 0);
594 int hostfs_create(struct inode *dir, struct dentry *dentry, int mode,
595 struct nameidata *nd)
601 inode = hostfs_iget(dir->i_sb);
603 error = PTR_ERR(inode);
607 error = init_inode(inode, dentry);
612 name = dentry_name(dentry, 0);
616 fd = file_create(name,
617 mode & S_IRUSR, mode & S_IWUSR, mode & S_IXUSR,
618 mode & S_IRGRP, mode & S_IWGRP, mode & S_IXGRP,
619 mode & S_IROTH, mode & S_IWOTH, mode & S_IXOTH);
622 else error = read_name(inode, name);
628 HOSTFS_I(inode)->fd = fd;
629 HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE;
630 d_instantiate(dentry, inode);
639 struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry,
640 struct nameidata *nd)
646 inode = hostfs_iget(ino->i_sb);
648 err = PTR_ERR(inode);
652 err = init_inode(inode, dentry);
657 name = dentry_name(dentry, 0);
661 err = read_name(inode, name);
663 if (err == -ENOENT) {
670 d_add(dentry, inode);
671 dentry->d_op = &hostfs_dentry_ops;
680 static char *inode_dentry_name(struct inode *ino, struct dentry *dentry)
685 file = inode_name(ino, dentry->d_name.len + 1);
690 strncat(file, dentry->d_name.name, dentry->d_name.len);
691 file[len + dentry->d_name.len] = '\0';
695 int hostfs_link(struct dentry *to, struct inode *ino, struct dentry *from)
697 char *from_name, *to_name;
700 if ((from_name = inode_dentry_name(ino, from)) == NULL)
702 to_name = dentry_name(to, 0);
703 if (to_name == NULL) {
707 err = link_file(to_name, from_name);
713 int hostfs_unlink(struct inode *ino, struct dentry *dentry)
718 if ((file = inode_dentry_name(ino, dentry)) == NULL)
723 err = unlink_file(file);
728 int hostfs_symlink(struct inode *ino, struct dentry *dentry, const char *to)
733 if ((file = inode_dentry_name(ino, dentry)) == NULL)
735 err = make_symlink(file, to);
740 int hostfs_mkdir(struct inode *ino, struct dentry *dentry, int mode)
745 if ((file = inode_dentry_name(ino, dentry)) == NULL)
747 err = do_mkdir(file, mode);
752 int hostfs_rmdir(struct inode *ino, struct dentry *dentry)
757 if ((file = inode_dentry_name(ino, dentry)) == NULL)
759 err = do_rmdir(file);
764 int hostfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
770 inode = hostfs_iget(dir->i_sb);
772 err = PTR_ERR(inode);
776 err = init_inode(inode, dentry);
781 name = dentry_name(dentry, 0);
785 init_special_inode(inode, mode, dev);
786 err = do_mknod(name, mode, MAJOR(dev), MINOR(dev));
790 err = read_name(inode, name);
795 d_instantiate(dentry, inode);
806 int hostfs_rename(struct inode *from_ino, struct dentry *from,
807 struct inode *to_ino, struct dentry *to)
809 char *from_name, *to_name;
812 if ((from_name = inode_dentry_name(from_ino, from)) == NULL)
814 if ((to_name = inode_dentry_name(to_ino, to)) == NULL) {
818 err = rename_file(from_name, to_name);
824 int hostfs_permission(struct inode *ino, int desired, struct nameidata *nd)
827 int r = 0, w = 0, x = 0, err;
829 if (desired & MAY_READ) r = 1;
830 if (desired & MAY_WRITE) w = 1;
831 if (desired & MAY_EXEC) x = 1;
832 name = inode_name(ino, 0);
836 if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) ||
837 S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode))
840 err = access_file(name, r, w, x);
843 err = generic_permission(ino, desired, NULL);
847 int hostfs_setattr(struct dentry *dentry, struct iattr *attr)
849 struct hostfs_iattr attrs;
853 int fd = HOSTFS_I(dentry->d_inode)->fd;
855 err = inode_change_ok(dentry->d_inode, attr);
860 attr->ia_valid &= ~ATTR_SIZE;
863 if (attr->ia_valid & ATTR_MODE) {
864 attrs.ia_valid |= HOSTFS_ATTR_MODE;
865 attrs.ia_mode = attr->ia_mode;
867 if (attr->ia_valid & ATTR_UID) {
868 attrs.ia_valid |= HOSTFS_ATTR_UID;
869 attrs.ia_uid = attr->ia_uid;
871 if (attr->ia_valid & ATTR_GID) {
872 attrs.ia_valid |= HOSTFS_ATTR_GID;
873 attrs.ia_gid = attr->ia_gid;
875 if (attr->ia_valid & ATTR_SIZE) {
876 attrs.ia_valid |= HOSTFS_ATTR_SIZE;
877 attrs.ia_size = attr->ia_size;
879 if (attr->ia_valid & ATTR_ATIME) {
880 attrs.ia_valid |= HOSTFS_ATTR_ATIME;
881 attrs.ia_atime = attr->ia_atime;
883 if (attr->ia_valid & ATTR_MTIME) {
884 attrs.ia_valid |= HOSTFS_ATTR_MTIME;
885 attrs.ia_mtime = attr->ia_mtime;
887 if (attr->ia_valid & ATTR_CTIME) {
888 attrs.ia_valid |= HOSTFS_ATTR_CTIME;
889 attrs.ia_ctime = attr->ia_ctime;
891 if (attr->ia_valid & ATTR_ATIME_SET) {
892 attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET;
894 if (attr->ia_valid & ATTR_MTIME_SET) {
895 attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET;
897 name = dentry_name(dentry, 0);
900 err = set_attr(name, &attrs, fd);
905 return inode_setattr(dentry->d_inode, attr);
908 static const struct inode_operations hostfs_iops = {
909 .create = hostfs_create,
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 static const struct inode_operations hostfs_dir_iops = {
922 .create = hostfs_create,
923 .lookup = hostfs_lookup,
925 .unlink = hostfs_unlink,
926 .symlink = hostfs_symlink,
927 .mkdir = hostfs_mkdir,
928 .rmdir = hostfs_rmdir,
929 .mknod = hostfs_mknod,
930 .rename = hostfs_rename,
931 .permission = hostfs_permission,
932 .setattr = hostfs_setattr,
935 int hostfs_link_readpage(struct file *file, struct page *page)
941 name = inode_name(page->mapping->host, 0);
944 err = do_readlink(name, buffer, PAGE_CACHE_SIZE);
946 if (err == PAGE_CACHE_SIZE)
949 flush_dcache_page(page);
950 SetPageUptodate(page);
951 if (PageError(page)) ClearPageError(page);
959 static const struct address_space_operations hostfs_link_aops = {
960 .readpage = hostfs_link_readpage,
963 static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent)
965 struct inode *root_inode;
966 char *host_root_path, *req_root = d;
969 sb->s_blocksize = 1024;
970 sb->s_blocksize_bits = 10;
971 sb->s_magic = HOSTFS_SUPER_MAGIC;
972 sb->s_op = &hostfs_sbops;
974 /* NULL is printed as <NULL> by sprintf: avoid that. */
975 if (req_root == NULL)
979 host_root_path = kmalloc(strlen(root_ino) + 1
980 + strlen(req_root) + 1, GFP_KERNEL);
981 if (host_root_path == NULL)
984 sprintf(host_root_path, "%s/%s", root_ino, req_root);
986 root_inode = hostfs_iget(sb);
987 if (IS_ERR(root_inode)) {
988 err = PTR_ERR(root_inode);
992 err = init_inode(root_inode, NULL);
996 HOSTFS_I(root_inode)->host_filename = host_root_path;
998 * Avoid that in the error path, iput(root_inode) frees again
999 * host_root_path through hostfs_destroy_inode!
1001 host_root_path = NULL;
1004 sb->s_root = d_alloc_root(root_inode);
1005 if (sb->s_root == NULL)
1008 err = hostfs_read_inode(root_inode);
1010 /* No iput in this case because the dput does that for us */
1021 kfree(host_root_path);
1026 static int hostfs_read_sb(struct file_system_type *type,
1027 int flags, const char *dev_name,
1028 void *data, struct vfsmount *mnt)
1030 return get_sb_nodev(type, flags, data, hostfs_fill_sb_common, mnt);
1033 static struct file_system_type hostfs_type = {
1034 .owner = THIS_MODULE,
1036 .get_sb = hostfs_read_sb,
1037 .kill_sb = kill_anon_super,
1041 static int __init init_hostfs(void)
1043 return register_filesystem(&hostfs_type);
1046 static void __exit exit_hostfs(void)
1048 unregister_filesystem(&hostfs_type);
1051 module_init(init_hostfs)
1052 module_exit(exit_hostfs)
1053 MODULE_LICENSE("GPL");