2 * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.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/version.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/slab.h>
15 #include <linux/pagemap.h>
16 #include <linux/blkdev.h>
17 #include <linux/list.h>
18 #include <linux/root_dev.h>
19 #include <linux/statfs.h>
20 #include <linux/kdev_t.h>
21 #include <asm/uaccess.h>
23 #include "kern_util.h"
25 #include "user_util.h"
28 struct hostfs_inode_info {
32 struct inode vfs_inode;
35 static inline struct hostfs_inode_info *HOSTFS_I(struct inode *inode)
37 return(list_entry(inode, struct hostfs_inode_info, vfs_inode));
40 #define FILE_HOSTFS_I(file) HOSTFS_I((file)->f_dentry->d_inode)
42 int hostfs_d_delete(struct dentry *dentry)
47 struct dentry_operations hostfs_dentry_ops = {
48 .d_delete = hostfs_d_delete,
51 /* Changed in hostfs_args before the kernel starts running */
52 static char *root_ino = "/";
53 static int append = 0;
55 #define HOSTFS_SUPER_MAGIC 0x00c0ffee
57 static struct inode_operations hostfs_iops;
58 static struct inode_operations hostfs_dir_iops;
59 static struct address_space_operations hostfs_link_aops;
62 static int __init hostfs_args(char *options, int *add)
66 ptr = strchr(options, ',');
74 ptr = strchr(options, ',');
78 if(!strcmp(options, "append"))
80 else printf("hostfs_args - unsupported option - %s\n",
88 __uml_setup("hostfs=", hostfs_args,
89 "hostfs=<root dir>,<flags>,...\n"
90 " This is used to set hostfs parameters. The root directory argument\n"
91 " is used to confine all hostfs mounts to within the specified directory\n"
92 " tree on the host. If this isn't specified, then a user inside UML can\n"
93 " mount anything on the host that's accessible to the user that's running\n"
95 " The only flag currently supported is 'append', which specifies that all\n"
96 " files opened by hostfs will be opened in append mode.\n\n"
100 static char *dentry_name(struct dentry *dentry, int extra)
102 struct dentry *parent;
108 while(parent->d_parent != parent){
109 len += parent->d_name.len + 1;
110 parent = parent->d_parent;
113 root = HOSTFS_I(parent->d_inode)->host_filename;
115 name = kmalloc(len + extra + 1, GFP_KERNEL);
116 if(name == NULL) return(NULL);
120 while(parent->d_parent != parent){
121 len -= parent->d_name.len + 1;
123 strncpy(&name[len + 1], parent->d_name.name,
125 parent = parent->d_parent;
127 strncpy(name, root, strlen(root));
131 static char *inode_name(struct inode *ino, int extra)
133 struct dentry *dentry;
135 dentry = list_entry(ino->i_dentry.next, struct dentry, d_alias);
136 return(dentry_name(dentry, extra));
139 static int read_name(struct inode *ino, char *name)
141 /* The non-int inode fields are copied into ints by stat_file and
142 * then copied into the inode because passing the actual pointers
143 * in and having them treated as int * breaks on big-endian machines
146 int i_mode, i_nlink, i_blksize;
147 unsigned long long i_size;
148 unsigned long long i_ino;
149 unsigned long long i_blocks;
151 err = stat_file(name, &i_ino, &i_mode, &i_nlink, &ino->i_uid,
152 &ino->i_gid, &i_size, &ino->i_atime, &ino->i_mtime,
153 &ino->i_ctime, &i_blksize, &i_blocks);
158 ino->i_mode = i_mode;
159 ino->i_nlink = i_nlink;
160 ino->i_size = i_size;
161 ino->i_blksize = i_blksize;
162 ino->i_blocks = i_blocks;
163 if((ino->i_sb->s_dev == ROOT_DEV) && (ino->i_uid == getuid()))
168 static char *follow_link(char *link)
171 char *name, *resolved, *end;
176 name = kmalloc(len, GFP_KERNEL);
180 n = do_readlink(link, name, len);
192 end = strrchr(link, '/');
197 len = strlen(link) + strlen(name) + 1;
199 resolved = kmalloc(len, GFP_KERNEL);
200 if(resolved == NULL){
205 sprintf(resolved, "%s%s", link, name);
216 static int read_inode(struct inode *ino)
221 /* Unfortunately, we are called from iget() when we don't have a dentry
224 if(list_empty(&ino->i_dentry))
228 name = inode_name(ino, 0);
232 if(file_type(name, NULL, NULL) == OS_TYPE_SYMLINK){
233 name = follow_link(name);
240 err = read_name(ino, name);
246 int hostfs_statfs(struct super_block *sb, struct kstatfs *sf)
248 /* do_statfs uses struct statfs64 internally, but the linux kernel
249 * struct statfs still has 32-bit versions for most of these fields,
250 * so we convert them here
259 err = do_statfs(HOSTFS_I(sb->s_root->d_inode)->host_filename,
260 &sf->f_bsize, &f_blocks, &f_bfree, &f_bavail, &f_files,
261 &f_ffree, &sf->f_fsid, sizeof(sf->f_fsid),
262 &sf->f_namelen, sf->f_spare);
264 sf->f_blocks = f_blocks;
265 sf->f_bfree = f_bfree;
266 sf->f_bavail = f_bavail;
267 sf->f_files = f_files;
268 sf->f_ffree = f_ffree;
269 sf->f_type = HOSTFS_SUPER_MAGIC;
273 static struct inode *hostfs_alloc_inode(struct super_block *sb)
275 struct hostfs_inode_info *hi;
277 hi = kmalloc(sizeof(*hi), GFP_KERNEL);
281 *hi = ((struct hostfs_inode_info) { .host_filename = NULL,
284 inode_init_once(&hi->vfs_inode);
285 return(&hi->vfs_inode);
288 static void hostfs_delete_inode(struct inode *inode)
290 if(HOSTFS_I(inode)->fd != -1) {
291 close_file(&HOSTFS_I(inode)->fd);
292 HOSTFS_I(inode)->fd = -1;
297 static void hostfs_destroy_inode(struct inode *inode)
299 if(HOSTFS_I(inode)->host_filename)
300 kfree(HOSTFS_I(inode)->host_filename);
302 /*XXX: This should not happen, probably. The check is here for
303 * additional safety.*/
304 if(HOSTFS_I(inode)->fd != -1) {
305 close_file(&HOSTFS_I(inode)->fd);
306 printk(KERN_DEBUG "Closing host fd in .destroy_inode\n");
309 kfree(HOSTFS_I(inode));
312 static void hostfs_read_inode(struct inode *inode)
317 static struct super_operations hostfs_sbops = {
318 .alloc_inode = hostfs_alloc_inode,
319 .drop_inode = generic_delete_inode,
320 .delete_inode = hostfs_delete_inode,
321 .destroy_inode = hostfs_destroy_inode,
322 .read_inode = hostfs_read_inode,
323 .statfs = hostfs_statfs,
326 int hostfs_readdir(struct file *file, void *ent, filldir_t filldir)
330 unsigned long long next, ino;
333 name = dentry_name(file->f_dentry, 0);
334 if(name == NULL) return(-ENOMEM);
335 dir = open_dir(name, &error);
337 if(dir == NULL) return(-error);
339 while((name = read_dir(dir, &next, &ino, &len)) != NULL){
340 error = (*filldir)(ent, name, len, file->f_pos,
349 int hostfs_file_open(struct inode *ino, struct file *file)
352 int mode = 0, r = 0, w = 0, fd;
354 mode = file->f_mode & (FMODE_READ | FMODE_WRITE);
355 if((mode & HOSTFS_I(ino)->mode) == mode)
358 /* The file may already have been opened, but with the wrong access,
359 * so this resets things and reopens the file with the new access.
361 if(HOSTFS_I(ino)->fd != -1){
362 close_file(&HOSTFS_I(ino)->fd);
363 HOSTFS_I(ino)->fd = -1;
366 HOSTFS_I(ino)->mode |= mode;
367 if(HOSTFS_I(ino)->mode & FMODE_READ)
369 if(HOSTFS_I(ino)->mode & FMODE_WRITE)
374 name = dentry_name(file->f_dentry, 0);
378 fd = open_file(name, r, w, append);
380 if(fd < 0) return(fd);
381 FILE_HOSTFS_I(file)->fd = fd;
386 int hostfs_fsync(struct file *file, struct dentry *dentry, int datasync)
391 static struct file_operations hostfs_file_fops = {
392 .llseek = generic_file_llseek,
393 .read = generic_file_read,
394 .sendfile = generic_file_sendfile,
395 .aio_read = generic_file_aio_read,
396 .aio_write = generic_file_aio_write,
397 .readv = generic_file_readv,
398 .writev = generic_file_writev,
399 .write = generic_file_write,
400 .mmap = generic_file_mmap,
401 .open = hostfs_file_open,
403 .fsync = hostfs_fsync,
406 static struct file_operations hostfs_dir_fops = {
407 .llseek = generic_file_llseek,
408 .readdir = hostfs_readdir,
409 .read = generic_read_dir,
412 int hostfs_writepage(struct page *page, struct writeback_control *wbc)
414 struct address_space *mapping = page->mapping;
415 struct inode *inode = mapping->host;
417 unsigned long long base;
418 int count = PAGE_CACHE_SIZE;
419 int end_index = inode->i_size >> PAGE_CACHE_SHIFT;
422 if (page->index >= end_index)
423 count = inode->i_size & (PAGE_CACHE_SIZE-1);
426 base = ((unsigned long long) page->index) << PAGE_CACHE_SHIFT;
428 err = write_file(HOSTFS_I(inode)->fd, &base, buffer, count);
430 ClearPageUptodate(page);
434 if (base > inode->i_size)
435 inode->i_size = base;
438 ClearPageError(page);
448 int hostfs_readpage(struct file *file, struct page *page)
454 start = (long long) page->index << PAGE_CACHE_SHIFT;
456 err = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer,
458 if(err < 0) goto out;
460 memset(&buffer[err], 0, PAGE_CACHE_SIZE - err);
462 flush_dcache_page(page);
463 SetPageUptodate(page);
464 if (PageError(page)) ClearPageError(page);
472 int hostfs_prepare_write(struct file *file, struct page *page,
473 unsigned int from, unsigned int to)
476 long long start, tmp;
479 start = (long long) page->index << PAGE_CACHE_SHIFT;
483 err = read_file(FILE_HOSTFS_I(file)->fd, &tmp, buffer,
485 if(err < 0) goto out;
487 if(to != PAGE_CACHE_SIZE){
489 err = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer + to,
490 PAGE_CACHE_SIZE - to);
491 if(err < 0) goto out;
499 int hostfs_commit_write(struct file *file, struct page *page, unsigned from,
502 struct address_space *mapping = page->mapping;
503 struct inode *inode = mapping->host;
508 start = (long long) (page->index << PAGE_CACHE_SHIFT) + from;
510 err = write_file(FILE_HOSTFS_I(file)->fd, &start, buffer + from,
513 if(!err && (start > inode->i_size))
514 inode->i_size = start;
520 static struct address_space_operations hostfs_aops = {
521 .writepage = hostfs_writepage,
522 .readpage = hostfs_readpage,
523 .set_page_dirty = __set_page_dirty_nobuffers,
524 .prepare_write = hostfs_prepare_write,
525 .commit_write = hostfs_commit_write
528 static int init_inode(struct inode *inode, struct dentry *dentry)
531 int type, err = -ENOMEM;
536 name = dentry_name(dentry, 0);
539 type = file_type(name, &maj, &min);
540 /*Reencode maj and min with the kernel encoding.*/
541 rdev = MKDEV(maj, min);
544 else type = OS_TYPE_DIR;
547 if(type == OS_TYPE_SYMLINK)
548 inode->i_op = &page_symlink_inode_operations;
549 else if(type == OS_TYPE_DIR)
550 inode->i_op = &hostfs_dir_iops;
551 else inode->i_op = &hostfs_iops;
553 if(type == OS_TYPE_DIR) inode->i_fop = &hostfs_dir_fops;
554 else inode->i_fop = &hostfs_file_fops;
556 if(type == OS_TYPE_SYMLINK)
557 inode->i_mapping->a_ops = &hostfs_link_aops;
558 else inode->i_mapping->a_ops = &hostfs_aops;
561 case OS_TYPE_CHARDEV:
562 init_special_inode(inode, S_IFCHR, rdev);
564 case OS_TYPE_BLOCKDEV:
565 init_special_inode(inode, S_IFBLK, rdev);
568 init_special_inode(inode, S_IFIFO, 0);
571 init_special_inode(inode, S_IFSOCK, 0);
578 int hostfs_create(struct inode *dir, struct dentry *dentry, int mode,
579 struct nameidata *nd)
586 inode = iget(dir->i_sb, 0);
587 if(inode == NULL) goto out;
589 error = init_inode(inode, dentry);
594 name = dentry_name(dentry, 0);
598 fd = file_create(name,
599 mode & S_IRUSR, mode & S_IWUSR, mode & S_IXUSR,
600 mode & S_IRGRP, mode & S_IWGRP, mode & S_IXGRP,
601 mode & S_IROTH, mode & S_IWOTH, mode & S_IXOTH);
604 else error = read_name(inode, name);
610 HOSTFS_I(inode)->fd = fd;
611 HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE;
612 d_instantiate(dentry, inode);
621 struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry,
622 struct nameidata *nd)
629 inode = iget(ino->i_sb, 0);
633 err = init_inode(inode, dentry);
638 name = dentry_name(dentry, 0);
642 err = read_name(inode, name);
651 d_add(dentry, inode);
652 dentry->d_op = &hostfs_dentry_ops;
658 return(ERR_PTR(err));
661 static char *inode_dentry_name(struct inode *ino, struct dentry *dentry)
666 file = inode_name(ino, dentry->d_name.len + 1);
667 if(file == NULL) return(NULL);
670 strncat(file, dentry->d_name.name, dentry->d_name.len);
671 file[len + dentry->d_name.len] = '\0';
675 int hostfs_link(struct dentry *to, struct inode *ino, struct dentry *from)
677 char *from_name, *to_name;
680 if((from_name = inode_dentry_name(ino, from)) == NULL)
682 to_name = dentry_name(to, 0);
687 err = link_file(to_name, from_name);
693 int hostfs_unlink(struct inode *ino, struct dentry *dentry)
698 if((file = inode_dentry_name(ino, dentry)) == NULL) return(-ENOMEM);
702 err = unlink_file(file);
707 int hostfs_symlink(struct inode *ino, struct dentry *dentry, const char *to)
712 if((file = inode_dentry_name(ino, dentry)) == NULL) return(-ENOMEM);
713 err = make_symlink(file, to);
718 int hostfs_mkdir(struct inode *ino, struct dentry *dentry, int mode)
723 if((file = inode_dentry_name(ino, dentry)) == NULL) return(-ENOMEM);
724 err = do_mkdir(file, mode);
729 int hostfs_rmdir(struct inode *ino, struct dentry *dentry)
734 if((file = inode_dentry_name(ino, dentry)) == NULL) return(-ENOMEM);
735 err = do_rmdir(file);
740 int hostfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
746 inode = iget(dir->i_sb, 0);
750 err = init_inode(inode, dentry);
755 name = dentry_name(dentry, 0);
759 init_special_inode(inode, mode, dev);
760 err = do_mknod(name, mode, dev);
764 err = read_name(inode, name);
769 d_instantiate(dentry, inode);
780 int hostfs_rename(struct inode *from_ino, struct dentry *from,
781 struct inode *to_ino, struct dentry *to)
783 char *from_name, *to_name;
786 if((from_name = inode_dentry_name(from_ino, from)) == NULL)
788 if((to_name = inode_dentry_name(to_ino, to)) == NULL){
792 err = rename_file(from_name, to_name);
798 void hostfs_truncate(struct inode *ino)
803 int hostfs_permission(struct inode *ino, int desired, struct nameidata *nd)
806 int r = 0, w = 0, x = 0, err;
808 if (desired & MAY_READ) r = 1;
809 if (desired & MAY_WRITE) w = 1;
810 if (desired & MAY_EXEC) x = 1;
811 name = inode_name(ino, 0);
812 if (name == NULL) return(-ENOMEM);
814 if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) ||
815 S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode))
818 err = access_file(name, r, w, x);
821 err = generic_permission(ino, desired, NULL);
825 int hostfs_setattr(struct dentry *dentry, struct iattr *attr)
827 struct hostfs_iattr attrs;
831 err = inode_change_ok(dentry->d_inode, attr);
836 attr->ia_valid &= ~ATTR_SIZE;
839 if(attr->ia_valid & ATTR_MODE){
840 attrs.ia_valid |= HOSTFS_ATTR_MODE;
841 attrs.ia_mode = attr->ia_mode;
843 if(attr->ia_valid & ATTR_UID){
844 if((dentry->d_inode->i_sb->s_dev == ROOT_DEV) &&
846 attr->ia_uid = getuid();
847 attrs.ia_valid |= HOSTFS_ATTR_UID;
848 attrs.ia_uid = attr->ia_uid;
850 if(attr->ia_valid & ATTR_GID){
851 if((dentry->d_inode->i_sb->s_dev == ROOT_DEV) &&
853 attr->ia_gid = getgid();
854 attrs.ia_valid |= HOSTFS_ATTR_GID;
855 attrs.ia_gid = attr->ia_gid;
857 if(attr->ia_valid & ATTR_SIZE){
858 attrs.ia_valid |= HOSTFS_ATTR_SIZE;
859 attrs.ia_size = attr->ia_size;
861 if(attr->ia_valid & ATTR_ATIME){
862 attrs.ia_valid |= HOSTFS_ATTR_ATIME;
863 attrs.ia_atime = attr->ia_atime;
865 if(attr->ia_valid & ATTR_MTIME){
866 attrs.ia_valid |= HOSTFS_ATTR_MTIME;
867 attrs.ia_mtime = attr->ia_mtime;
869 if(attr->ia_valid & ATTR_CTIME){
870 attrs.ia_valid |= HOSTFS_ATTR_CTIME;
871 attrs.ia_ctime = attr->ia_ctime;
873 if(attr->ia_valid & ATTR_ATIME_SET){
874 attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET;
876 if(attr->ia_valid & ATTR_MTIME_SET){
877 attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET;
879 name = dentry_name(dentry, 0);
880 if(name == NULL) return(-ENOMEM);
881 err = set_attr(name, &attrs);
886 return(inode_setattr(dentry->d_inode, attr));
889 int hostfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
892 generic_fillattr(dentry->d_inode, stat);
896 static struct inode_operations hostfs_iops = {
897 .create = hostfs_create,
899 .unlink = hostfs_unlink,
900 .symlink = hostfs_symlink,
901 .mkdir = hostfs_mkdir,
902 .rmdir = hostfs_rmdir,
903 .mknod = hostfs_mknod,
904 .rename = hostfs_rename,
905 .truncate = hostfs_truncate,
906 .permission = hostfs_permission,
907 .setattr = hostfs_setattr,
908 .getattr = hostfs_getattr,
911 static struct inode_operations hostfs_dir_iops = {
912 .create = hostfs_create,
913 .lookup = hostfs_lookup,
915 .unlink = hostfs_unlink,
916 .symlink = hostfs_symlink,
917 .mkdir = hostfs_mkdir,
918 .rmdir = hostfs_rmdir,
919 .mknod = hostfs_mknod,
920 .rename = hostfs_rename,
921 .truncate = hostfs_truncate,
922 .permission = hostfs_permission,
923 .setattr = hostfs_setattr,
924 .getattr = hostfs_getattr,
927 int hostfs_link_readpage(struct file *file, struct page *page)
933 start = page->index << PAGE_CACHE_SHIFT;
935 name = inode_name(page->mapping->host, 0);
936 if(name == NULL) return(-ENOMEM);
937 err = do_readlink(name, buffer, PAGE_CACHE_SIZE);
939 if(err == PAGE_CACHE_SIZE)
942 flush_dcache_page(page);
943 SetPageUptodate(page);
944 if (PageError(page)) ClearPageError(page);
952 static struct address_space_operations hostfs_link_aops = {
953 .readpage = hostfs_link_readpage,
956 static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent)
958 struct inode *root_inode;
959 char *name, *data = d;
962 sb->s_blocksize = 1024;
963 sb->s_blocksize_bits = 10;
964 sb->s_magic = HOSTFS_SUPER_MAGIC;
965 sb->s_op = &hostfs_sbops;
967 if((data == NULL) || (*data == '\0'))
971 name = kmalloc(strlen(data) + 1, GFP_KERNEL);
977 root_inode = iget(sb, 0);
978 if(root_inode == NULL)
981 err = init_inode(root_inode, NULL);
985 HOSTFS_I(root_inode)->host_filename = name;
988 sb->s_root = d_alloc_root(root_inode);
989 if(sb->s_root == NULL)
992 err = read_inode(root_inode);
994 /* No iput in this case because the dput does that for us */
1010 static struct super_block *hostfs_read_sb(struct file_system_type *type,
1011 int flags, const char *dev_name,
1014 return(get_sb_nodev(type, flags, data, hostfs_fill_sb_common));
1017 static struct file_system_type hostfs_type = {
1018 .owner = THIS_MODULE,
1020 .get_sb = hostfs_read_sb,
1021 .kill_sb = kill_anon_super,
1025 static int __init init_hostfs(void)
1027 return(register_filesystem(&hostfs_type));
1030 static void __exit exit_hostfs(void)
1032 unregister_filesystem(&hostfs_type);
1035 module_init(init_hostfs)
1036 module_exit(exit_hostfs)
1037 MODULE_LICENSE("GPL");
1040 * Overrides for Emacs so that we follow Linus's tabbing style.
1041 * Emacs will notice this stuff at the end of the file and automatically
1042 * adjust the settings for this buffer only. This must remain at the end
1044 * ---------------------------------------------------------------------------
1046 * c-file-style: "linux"