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>
15 #include <linux/mount.h>
20 struct hostfs_inode_info {
24 struct inode vfs_inode;
27 static inline struct hostfs_inode_info *HOSTFS_I(struct inode *inode)
29 return list_entry(inode, struct hostfs_inode_info, vfs_inode);
32 #define FILE_HOSTFS_I(file) HOSTFS_I((file)->f_path.dentry->d_inode)
34 static int hostfs_d_delete(struct dentry *dentry)
39 static const struct dentry_operations hostfs_dentry_ops = {
40 .d_delete = hostfs_d_delete,
43 /* Changed in hostfs_args before the kernel starts running */
44 static char *root_ino = "";
45 static int append = 0;
47 #define HOSTFS_SUPER_MAGIC 0x00c0ffee
49 static const struct inode_operations hostfs_iops;
50 static const struct inode_operations hostfs_dir_iops;
51 static const struct address_space_operations hostfs_link_aops;
54 static int __init hostfs_args(char *options, int *add)
58 ptr = strchr(options, ',');
66 ptr = strchr(options, ',');
69 if (*options != '\0') {
70 if (!strcmp(options, "append"))
72 else printf("hostfs_args - unsupported option - %s\n",
80 __uml_setup("hostfs=", hostfs_args,
81 "hostfs=<root dir>,<flags>,...\n"
82 " This is used to set hostfs parameters. The root directory argument\n"
83 " is used to confine all hostfs mounts to within the specified directory\n"
84 " tree on the host. If this isn't specified, then a user inside UML can\n"
85 " mount anything on the host that's accessible to the user that's running\n"
87 " The only flag currently supported is 'append', which specifies that all\n"
88 " files opened by hostfs will be opened in append mode.\n\n"
92 static char *dentry_name(struct dentry *dentry, int extra)
94 struct dentry *parent;
100 while (parent->d_parent != parent) {
101 len += parent->d_name.len + 1;
102 parent = parent->d_parent;
105 root = HOSTFS_I(parent->d_inode)->host_filename;
107 name = kmalloc(len + extra + 1, GFP_KERNEL);
113 while (parent->d_parent != parent) {
114 len -= parent->d_name.len + 1;
116 strncpy(&name[len + 1], parent->d_name.name,
118 parent = parent->d_parent;
120 strncpy(name, root, strlen(root));
124 static char *inode_name(struct inode *ino, int extra)
126 struct dentry *dentry;
128 dentry = list_entry(ino->i_dentry.next, struct dentry, d_alias);
129 return dentry_name(dentry, extra);
132 static int read_name(struct inode *ino, char *name)
135 * The non-int inode fields are copied into ints by stat_file and
136 * then copied into the inode because passing the actual pointers
137 * in and having them treated as int * breaks on big-endian machines
140 int i_mode, i_nlink, i_blksize;
141 unsigned long long i_size;
142 unsigned long long i_ino;
143 unsigned long long i_blocks;
145 err = stat_file(name, &i_ino, &i_mode, &i_nlink, &ino->i_uid,
146 &ino->i_gid, &i_size, &ino->i_atime, &ino->i_mtime,
147 &ino->i_ctime, &i_blksize, &i_blocks, -1);
152 ino->i_mode = i_mode;
153 ino->i_nlink = i_nlink;
154 ino->i_size = i_size;
155 ino->i_blocks = i_blocks;
159 static char *follow_link(char *link)
162 char *name, *resolved, *end;
167 name = kmalloc(len, GFP_KERNEL);
171 n = hostfs_do_readlink(link, name, len);
183 end = strrchr(link, '/');
188 len = strlen(link) + strlen(name) + 1;
190 resolved = kmalloc(len, GFP_KERNEL);
191 if (resolved == NULL) {
196 sprintf(resolved, "%s%s", link, name);
207 static int hostfs_read_inode(struct inode *ino)
213 * Unfortunately, we are called from iget() when we don't have a dentry
216 if (list_empty(&ino->i_dentry))
220 name = inode_name(ino, 0);
224 if (file_type(name, NULL, NULL) == OS_TYPE_SYMLINK) {
225 name = follow_link(name);
232 err = read_name(ino, name);
238 static struct inode *hostfs_iget(struct super_block *sb)
243 inode = iget_locked(sb, 0);
245 return ERR_PTR(-ENOMEM);
246 if (inode->i_state & I_NEW) {
247 ret = hostfs_read_inode(inode);
252 unlock_new_inode(inode);
257 int hostfs_statfs(struct dentry *dentry, struct kstatfs *sf)
260 * do_statfs uses struct statfs64 internally, but the linux kernel
261 * struct statfs still has 32-bit versions for most of these fields,
262 * so we convert them here
271 err = do_statfs(HOSTFS_I(dentry->d_sb->s_root->d_inode)->host_filename,
272 &sf->f_bsize, &f_blocks, &f_bfree, &f_bavail, &f_files,
273 &f_ffree, &sf->f_fsid, sizeof(sf->f_fsid),
274 &sf->f_namelen, sf->f_spare);
277 sf->f_blocks = f_blocks;
278 sf->f_bfree = f_bfree;
279 sf->f_bavail = f_bavail;
280 sf->f_files = f_files;
281 sf->f_ffree = f_ffree;
282 sf->f_type = HOSTFS_SUPER_MAGIC;
286 static struct inode *hostfs_alloc_inode(struct super_block *sb)
288 struct hostfs_inode_info *hi;
290 hi = kmalloc(sizeof(*hi), GFP_KERNEL);
294 *hi = ((struct hostfs_inode_info) { .host_filename = NULL,
297 inode_init_once(&hi->vfs_inode);
298 return &hi->vfs_inode;
301 static void hostfs_delete_inode(struct inode *inode)
303 truncate_inode_pages(&inode->i_data, 0);
304 if (HOSTFS_I(inode)->fd != -1) {
305 close_file(&HOSTFS_I(inode)->fd);
306 HOSTFS_I(inode)->fd = -1;
311 static void hostfs_destroy_inode(struct inode *inode)
313 kfree(HOSTFS_I(inode)->host_filename);
316 * XXX: This should not happen, probably. The check is here for
319 if (HOSTFS_I(inode)->fd != -1) {
320 close_file(&HOSTFS_I(inode)->fd);
321 printk(KERN_DEBUG "Closing host fd in .destroy_inode\n");
324 kfree(HOSTFS_I(inode));
327 static int hostfs_show_options(struct seq_file *seq, struct vfsmount *vfs)
329 struct inode *root = vfs->mnt_sb->s_root->d_inode;
330 const char *root_path = HOSTFS_I(root)->host_filename;
331 size_t offset = strlen(root_ino) + 1;
333 if (strlen(root_path) > offset)
334 seq_printf(seq, ",%s", root_path + offset);
339 static const struct super_operations hostfs_sbops = {
340 .alloc_inode = hostfs_alloc_inode,
341 .drop_inode = generic_delete_inode,
342 .delete_inode = hostfs_delete_inode,
343 .destroy_inode = hostfs_destroy_inode,
344 .statfs = hostfs_statfs,
345 .show_options = hostfs_show_options,
348 int hostfs_readdir(struct file *file, void *ent, filldir_t filldir)
352 unsigned long long next, ino;
355 name = dentry_name(file->f_path.dentry, 0);
358 dir = open_dir(name, &error);
363 while ((name = read_dir(dir, &next, &ino, &len)) != NULL) {
364 error = (*filldir)(ent, name, len, file->f_pos,
373 int hostfs_file_open(struct inode *ino, struct file *file)
377 int r = 0, w = 0, fd;
379 mode = file->f_mode & (FMODE_READ | FMODE_WRITE);
380 if ((mode & HOSTFS_I(ino)->mode) == mode)
384 * The file may already have been opened, but with the wrong access,
385 * so this resets things and reopens the file with the new access.
387 if (HOSTFS_I(ino)->fd != -1) {
388 close_file(&HOSTFS_I(ino)->fd);
389 HOSTFS_I(ino)->fd = -1;
392 HOSTFS_I(ino)->mode |= mode;
393 if (HOSTFS_I(ino)->mode & FMODE_READ)
395 if (HOSTFS_I(ino)->mode & FMODE_WRITE)
400 name = dentry_name(file->f_path.dentry, 0);
404 fd = open_file(name, r, w, append);
408 FILE_HOSTFS_I(file)->fd = fd;
413 int hostfs_fsync(struct file *file, struct dentry *dentry, int datasync)
415 return fsync_file(HOSTFS_I(dentry->d_inode)->fd, datasync);
418 static const struct file_operations hostfs_file_fops = {
419 .llseek = generic_file_llseek,
420 .read = do_sync_read,
421 .splice_read = generic_file_splice_read,
422 .aio_read = generic_file_aio_read,
423 .aio_write = generic_file_aio_write,
424 .write = do_sync_write,
425 .mmap = generic_file_mmap,
426 .open = hostfs_file_open,
428 .fsync = hostfs_fsync,
431 static const struct file_operations hostfs_dir_fops = {
432 .llseek = generic_file_llseek,
433 .readdir = hostfs_readdir,
434 .read = generic_read_dir,
437 int hostfs_writepage(struct page *page, struct writeback_control *wbc)
439 struct address_space *mapping = page->mapping;
440 struct inode *inode = mapping->host;
442 unsigned long long base;
443 int count = PAGE_CACHE_SIZE;
444 int end_index = inode->i_size >> PAGE_CACHE_SHIFT;
447 if (page->index >= end_index)
448 count = inode->i_size & (PAGE_CACHE_SIZE-1);
451 base = ((unsigned long long) page->index) << PAGE_CACHE_SHIFT;
453 err = write_file(HOSTFS_I(inode)->fd, &base, buffer, count);
455 ClearPageUptodate(page);
459 if (base > inode->i_size)
460 inode->i_size = base;
463 ClearPageError(page);
473 int hostfs_readpage(struct file *file, struct page *page)
479 start = (long long) page->index << PAGE_CACHE_SHIFT;
481 err = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer,
486 memset(&buffer[err], 0, PAGE_CACHE_SIZE - err);
488 flush_dcache_page(page);
489 SetPageUptodate(page);
490 if (PageError(page)) ClearPageError(page);
498 int hostfs_write_begin(struct file *file, struct address_space *mapping,
499 loff_t pos, unsigned len, unsigned flags,
500 struct page **pagep, void **fsdata)
502 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
504 *pagep = grab_cache_page_write_begin(mapping, index, flags);
510 int hostfs_write_end(struct file *file, struct address_space *mapping,
511 loff_t pos, unsigned len, unsigned copied,
512 struct page *page, void *fsdata)
514 struct inode *inode = mapping->host;
516 unsigned from = pos & (PAGE_CACHE_SIZE - 1);
520 err = write_file(FILE_HOSTFS_I(file)->fd, &pos, buffer + from, copied);
523 if (!PageUptodate(page) && err == PAGE_CACHE_SIZE)
524 SetPageUptodate(page);
527 * If err > 0, write_file has added err to pos, so we are comparing
528 * i_size against the last byte written.
530 if (err > 0 && (pos > inode->i_size))
533 page_cache_release(page);
538 static const struct address_space_operations hostfs_aops = {
539 .writepage = hostfs_writepage,
540 .readpage = hostfs_readpage,
541 .set_page_dirty = __set_page_dirty_nobuffers,
542 .write_begin = hostfs_write_begin,
543 .write_end = hostfs_write_end,
546 static int init_inode(struct inode *inode, struct dentry *dentry)
549 int type, err = -ENOMEM;
554 name = dentry_name(dentry, 0);
557 type = file_type(name, &maj, &min);
558 /* Reencode maj and min with the kernel encoding.*/
559 rdev = MKDEV(maj, min);
562 else type = OS_TYPE_DIR;
565 if (type == OS_TYPE_SYMLINK)
566 inode->i_op = &page_symlink_inode_operations;
567 else if (type == OS_TYPE_DIR)
568 inode->i_op = &hostfs_dir_iops;
569 else inode->i_op = &hostfs_iops;
571 if (type == OS_TYPE_DIR) inode->i_fop = &hostfs_dir_fops;
572 else inode->i_fop = &hostfs_file_fops;
574 if (type == OS_TYPE_SYMLINK)
575 inode->i_mapping->a_ops = &hostfs_link_aops;
576 else inode->i_mapping->a_ops = &hostfs_aops;
579 case OS_TYPE_CHARDEV:
580 init_special_inode(inode, S_IFCHR, rdev);
582 case OS_TYPE_BLOCKDEV:
583 init_special_inode(inode, S_IFBLK, rdev);
586 init_special_inode(inode, S_IFIFO, 0);
589 init_special_inode(inode, S_IFSOCK, 0);
596 int hostfs_create(struct inode *dir, struct dentry *dentry, int mode,
597 struct nameidata *nd)
603 inode = hostfs_iget(dir->i_sb);
605 error = PTR_ERR(inode);
609 error = init_inode(inode, dentry);
614 name = dentry_name(dentry, 0);
618 fd = file_create(name,
619 mode & S_IRUSR, mode & S_IWUSR, mode & S_IXUSR,
620 mode & S_IRGRP, mode & S_IWGRP, mode & S_IXGRP,
621 mode & S_IROTH, mode & S_IWOTH, mode & S_IXOTH);
624 else error = read_name(inode, name);
630 HOSTFS_I(inode)->fd = fd;
631 HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE;
632 d_instantiate(dentry, inode);
641 struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry,
642 struct nameidata *nd)
648 inode = hostfs_iget(ino->i_sb);
650 err = PTR_ERR(inode);
654 err = init_inode(inode, dentry);
659 name = dentry_name(dentry, 0);
663 err = read_name(inode, name);
665 if (err == -ENOENT) {
672 d_add(dentry, inode);
673 dentry->d_op = &hostfs_dentry_ops;
682 static char *inode_dentry_name(struct inode *ino, struct dentry *dentry)
687 file = inode_name(ino, dentry->d_name.len + 1);
692 strncat(file, dentry->d_name.name, dentry->d_name.len);
693 file[len + dentry->d_name.len] = '\0';
697 int hostfs_link(struct dentry *to, struct inode *ino, struct dentry *from)
699 char *from_name, *to_name;
702 if ((from_name = inode_dentry_name(ino, from)) == NULL)
704 to_name = dentry_name(to, 0);
705 if (to_name == NULL) {
709 err = link_file(to_name, from_name);
715 int hostfs_unlink(struct inode *ino, struct dentry *dentry)
720 if ((file = inode_dentry_name(ino, dentry)) == NULL)
725 err = unlink_file(file);
730 int hostfs_symlink(struct inode *ino, struct dentry *dentry, const char *to)
735 if ((file = inode_dentry_name(ino, dentry)) == NULL)
737 err = make_symlink(file, to);
742 int hostfs_mkdir(struct inode *ino, struct dentry *dentry, int mode)
747 if ((file = inode_dentry_name(ino, dentry)) == NULL)
749 err = do_mkdir(file, mode);
754 int hostfs_rmdir(struct inode *ino, struct dentry *dentry)
759 if ((file = inode_dentry_name(ino, dentry)) == NULL)
761 err = do_rmdir(file);
766 int hostfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
772 inode = hostfs_iget(dir->i_sb);
774 err = PTR_ERR(inode);
778 err = init_inode(inode, dentry);
783 name = dentry_name(dentry, 0);
787 init_special_inode(inode, mode, dev);
788 err = do_mknod(name, mode, MAJOR(dev), MINOR(dev));
792 err = read_name(inode, name);
797 d_instantiate(dentry, inode);
808 int hostfs_rename(struct inode *from_ino, struct dentry *from,
809 struct inode *to_ino, struct dentry *to)
811 char *from_name, *to_name;
814 if ((from_name = inode_dentry_name(from_ino, from)) == NULL)
816 if ((to_name = inode_dentry_name(to_ino, to)) == NULL) {
820 err = rename_file(from_name, to_name);
826 int hostfs_permission(struct inode *ino, int desired)
829 int r = 0, w = 0, x = 0, err;
831 if (desired & MAY_READ) r = 1;
832 if (desired & MAY_WRITE) w = 1;
833 if (desired & MAY_EXEC) x = 1;
834 name = inode_name(ino, 0);
838 if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) ||
839 S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode))
842 err = access_file(name, r, w, x);
845 err = generic_permission(ino, desired, NULL);
849 int hostfs_setattr(struct dentry *dentry, struct iattr *attr)
851 struct hostfs_iattr attrs;
855 int fd = HOSTFS_I(dentry->d_inode)->fd;
857 err = inode_change_ok(dentry->d_inode, attr);
862 attr->ia_valid &= ~ATTR_SIZE;
865 if (attr->ia_valid & ATTR_MODE) {
866 attrs.ia_valid |= HOSTFS_ATTR_MODE;
867 attrs.ia_mode = attr->ia_mode;
869 if (attr->ia_valid & ATTR_UID) {
870 attrs.ia_valid |= HOSTFS_ATTR_UID;
871 attrs.ia_uid = attr->ia_uid;
873 if (attr->ia_valid & ATTR_GID) {
874 attrs.ia_valid |= HOSTFS_ATTR_GID;
875 attrs.ia_gid = attr->ia_gid;
877 if (attr->ia_valid & ATTR_SIZE) {
878 attrs.ia_valid |= HOSTFS_ATTR_SIZE;
879 attrs.ia_size = attr->ia_size;
881 if (attr->ia_valid & ATTR_ATIME) {
882 attrs.ia_valid |= HOSTFS_ATTR_ATIME;
883 attrs.ia_atime = attr->ia_atime;
885 if (attr->ia_valid & ATTR_MTIME) {
886 attrs.ia_valid |= HOSTFS_ATTR_MTIME;
887 attrs.ia_mtime = attr->ia_mtime;
889 if (attr->ia_valid & ATTR_CTIME) {
890 attrs.ia_valid |= HOSTFS_ATTR_CTIME;
891 attrs.ia_ctime = attr->ia_ctime;
893 if (attr->ia_valid & ATTR_ATIME_SET) {
894 attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET;
896 if (attr->ia_valid & ATTR_MTIME_SET) {
897 attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET;
899 name = dentry_name(dentry, 0);
902 err = set_attr(name, &attrs, fd);
907 return inode_setattr(dentry->d_inode, attr);
910 static const struct inode_operations hostfs_iops = {
911 .create = hostfs_create,
913 .unlink = hostfs_unlink,
914 .symlink = hostfs_symlink,
915 .mkdir = hostfs_mkdir,
916 .rmdir = hostfs_rmdir,
917 .mknod = hostfs_mknod,
918 .rename = hostfs_rename,
919 .permission = hostfs_permission,
920 .setattr = hostfs_setattr,
923 static const struct inode_operations hostfs_dir_iops = {
924 .create = hostfs_create,
925 .lookup = hostfs_lookup,
927 .unlink = hostfs_unlink,
928 .symlink = hostfs_symlink,
929 .mkdir = hostfs_mkdir,
930 .rmdir = hostfs_rmdir,
931 .mknod = hostfs_mknod,
932 .rename = hostfs_rename,
933 .permission = hostfs_permission,
934 .setattr = hostfs_setattr,
937 int hostfs_link_readpage(struct file *file, struct page *page)
943 name = inode_name(page->mapping->host, 0);
946 err = hostfs_do_readlink(name, buffer, PAGE_CACHE_SIZE);
948 if (err == PAGE_CACHE_SIZE)
951 flush_dcache_page(page);
952 SetPageUptodate(page);
953 if (PageError(page)) ClearPageError(page);
961 static const struct address_space_operations hostfs_link_aops = {
962 .readpage = hostfs_link_readpage,
965 static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent)
967 struct inode *root_inode;
968 char *host_root_path, *req_root = d;
971 sb->s_blocksize = 1024;
972 sb->s_blocksize_bits = 10;
973 sb->s_magic = HOSTFS_SUPER_MAGIC;
974 sb->s_op = &hostfs_sbops;
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");