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/buffer_head.h>
19 #include <linux/root_dev.h>
20 #include <linux/statfs.h>
21 #include <asm/uaccess.h>
23 #include "kern_util.h"
25 #include "user_util.h"
26 #include "2_5compat.h"
29 struct hostfs_inode_info {
33 struct inode vfs_inode;
36 static inline struct hostfs_inode_info *HOSTFS_I(struct inode *inode)
38 return(list_entry(inode, struct hostfs_inode_info, vfs_inode));
41 #define FILE_HOSTFS_I(file) HOSTFS_I((file)->f_dentry->d_inode)
43 int hostfs_d_delete(struct dentry *dentry)
48 struct dentry_operations hostfs_dentry_ops = {
49 .d_delete = hostfs_d_delete,
52 /* Changed in hostfs_args before the kernel starts running */
53 static char *root_ino = "/";
54 static int append = 0;
56 #define HOSTFS_SUPER_MAGIC 0x00c0ffee
58 static struct inode_operations hostfs_iops;
59 static struct inode_operations hostfs_dir_iops;
60 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"
99 static char *dentry_name(struct dentry *dentry, int extra)
101 struct dentry *parent;
107 while(parent->d_parent != parent){
108 len += parent->d_name.len + 1;
109 parent = parent->d_parent;
112 root = HOSTFS_I(parent->d_inode)->host_filename;
114 name = kmalloc(len + extra + 1, GFP_KERNEL);
115 if(name == NULL) return(NULL);
119 while(parent->d_parent != parent){
120 len -= parent->d_name.len + 1;
122 strncpy(&name[len + 1], parent->d_name.name,
124 parent = parent->d_parent;
126 strncpy(name, root, strlen(root));
130 static char *inode_name(struct inode *ino, int extra)
132 struct dentry *dentry;
134 dentry = list_entry(ino->i_dentry.next, struct dentry, d_alias);
135 return(dentry_name(dentry, extra));
138 static int read_name(struct inode *ino, char *name)
140 /* The non-int inode fields are copied into ints by stat_file and
141 * then copied into the inode because passing the actual pointers
142 * in and having them treated as int * breaks on big-endian machines
145 int i_mode, i_nlink, i_blksize;
146 unsigned long long i_size;
147 unsigned long long i_ino;
148 unsigned long long i_blocks;
150 err = stat_file(name, &i_ino, &i_mode, &i_nlink, &ino->i_uid,
151 &ino->i_gid, &i_size, &ino->i_atime, &ino->i_mtime,
152 &ino->i_ctime, &i_blksize, &i_blocks);
157 ino->i_mode = i_mode;
158 ino->i_nlink = i_nlink;
159 ino->i_size = i_size;
160 ino->i_blksize = i_blksize;
161 ino->i_blocks = i_blocks;
162 if((ino->i_sb->s_dev == ROOT_DEV) && (ino->i_uid == getuid()))
167 static char *follow_link(char *link)
170 char *name, *resolved, *end;
175 name = kmalloc(len, GFP_KERNEL);
179 n = do_readlink(link, name, len);
191 end = strrchr(link, '/');
196 len = strlen(link) + strlen(name) + 1;
198 resolved = kmalloc(len, GFP_KERNEL);
199 if(resolved == NULL){
204 sprintf(resolved, "%s%s", link, name);
215 static int read_inode(struct inode *ino)
220 /* Unfortunately, we are called from iget() when we don't have a dentry
223 if(list_empty(&ino->i_dentry))
227 name = inode_name(ino, 0);
231 if(file_type(name, NULL) == OS_TYPE_SYMLINK){
232 name = follow_link(name);
239 err = read_name(ino, name);
245 int hostfs_statfs(struct super_block *sb, struct kstatfs *sf)
247 /* do_statfs uses struct statfs64 internally, but the linux kernel
248 * struct statfs still has 32-bit versions for most of these fields,
249 * so we convert them here
258 err = do_statfs(HOSTFS_I(sb->s_root->d_inode)->host_filename,
259 &sf->f_bsize, &f_blocks, &f_bfree, &f_bavail, &f_files,
260 &f_ffree, &sf->f_fsid, sizeof(sf->f_fsid),
261 &sf->f_namelen, sf->f_spare);
263 sf->f_blocks = f_blocks;
264 sf->f_bfree = f_bfree;
265 sf->f_bavail = f_bavail;
266 sf->f_files = f_files;
267 sf->f_ffree = f_ffree;
268 sf->f_type = HOSTFS_SUPER_MAGIC;
272 static struct inode *hostfs_alloc_inode(struct super_block *sb)
274 struct hostfs_inode_info *hi;
276 hi = kmalloc(sizeof(*hi), GFP_KERNEL);
280 *hi = ((struct hostfs_inode_info) { .host_filename = NULL,
283 inode_init_once(&hi->vfs_inode);
284 return(&hi->vfs_inode);
287 static void hostfs_destroy_inode(struct inode *inode)
289 if(HOSTFS_I(inode)->host_filename)
290 kfree(HOSTFS_I(inode)->host_filename);
292 if(HOSTFS_I(inode)->fd != -1)
293 close_file(&HOSTFS_I(inode)->fd);
295 kfree(HOSTFS_I(inode));
298 static void hostfs_read_inode(struct inode *inode)
303 static struct super_operations hostfs_sbops = {
304 .alloc_inode = hostfs_alloc_inode,
305 .destroy_inode = hostfs_destroy_inode,
306 .read_inode = hostfs_read_inode,
307 .statfs = hostfs_statfs,
310 int hostfs_readdir(struct file *file, void *ent, filldir_t filldir)
314 unsigned long long next, ino;
317 name = dentry_name(file->f_dentry, 0);
318 if(name == NULL) return(-ENOMEM);
319 dir = open_dir(name, &error);
321 if(dir == NULL) return(-error);
323 while((name = read_dir(dir, &next, &ino, &len)) != NULL){
324 error = (*filldir)(ent, name, len, file->f_pos,
333 int hostfs_file_open(struct inode *ino, struct file *file)
336 int mode = 0, r = 0, w = 0, fd;
338 mode = file->f_mode & (FMODE_READ | FMODE_WRITE);
339 if((mode & HOSTFS_I(ino)->mode) == mode)
342 /* The file may already have been opened, but with the wrong access,
343 * so this resets things and reopens the file with the new access.
345 if(HOSTFS_I(ino)->fd != -1){
346 close_file(&HOSTFS_I(ino)->fd);
347 HOSTFS_I(ino)->fd = -1;
350 HOSTFS_I(ino)->mode |= mode;
351 if(HOSTFS_I(ino)->mode & FMODE_READ)
353 if(HOSTFS_I(ino)->mode & FMODE_WRITE)
358 name = dentry_name(file->f_dentry, 0);
362 fd = open_file(name, r, w, append);
364 if(fd < 0) return(fd);
365 FILE_HOSTFS_I(file)->fd = fd;
370 int hostfs_fsync(struct file *file, struct dentry *dentry, int datasync)
375 static struct file_operations hostfs_file_fops = {
376 .llseek = generic_file_llseek,
377 .read = generic_file_read,
378 .write = generic_file_write,
379 .mmap = generic_file_mmap,
380 .open = hostfs_file_open,
382 .fsync = hostfs_fsync,
385 static struct file_operations hostfs_dir_fops = {
386 .readdir = hostfs_readdir,
387 .read = generic_read_dir,
390 int hostfs_writepage(struct page *page, struct writeback_control *wbc)
392 struct address_space *mapping = page->mapping;
393 struct inode *inode = mapping->host;
395 unsigned long long base;
396 int count = PAGE_CACHE_SIZE;
397 int end_index = inode->i_size >> PAGE_CACHE_SHIFT;
400 if (page->index >= end_index)
401 count = inode->i_size & (PAGE_CACHE_SIZE-1);
404 base = ((unsigned long long) page->index) << PAGE_CACHE_SHIFT;
406 err = write_file(HOSTFS_I(inode)->fd, &base, buffer, count);
408 ClearPageUptodate(page);
412 if (base > inode->i_size)
413 inode->i_size = base;
416 ClearPageError(page);
426 int hostfs_readpage(struct file *file, struct page *page)
432 start = (long long) page->index << PAGE_CACHE_SHIFT;
434 err = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer,
436 if(err < 0) goto out;
438 memset(&buffer[err], 0, PAGE_CACHE_SIZE - err);
440 flush_dcache_page(page);
441 SetPageUptodate(page);
442 if (PageError(page)) ClearPageError(page);
450 int hostfs_prepare_write(struct file *file, struct page *page,
451 unsigned int from, unsigned int to)
454 long long start, tmp;
457 start = (long long) page->index << PAGE_CACHE_SHIFT;
461 err = read_file(FILE_HOSTFS_I(file)->fd, &tmp, buffer,
463 if(err < 0) goto out;
465 if(to != PAGE_CACHE_SIZE){
467 err = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer + to,
468 PAGE_CACHE_SIZE - to);
469 if(err < 0) goto out;
477 int hostfs_commit_write(struct file *file, struct page *page, unsigned from,
480 struct address_space *mapping = page->mapping;
481 struct inode *inode = mapping->host;
486 start = (long long) (page->index << PAGE_CACHE_SHIFT) + from;
488 err = write_file(FILE_HOSTFS_I(file)->fd, &start, buffer + from,
491 if(!err && (start > inode->i_size))
492 inode->i_size = start;
498 static struct address_space_operations hostfs_aops = {
499 .writepage = hostfs_writepage,
500 .readpage = hostfs_readpage,
501 /* .set_page_dirty = __set_page_dirty_nobuffers, */
502 .prepare_write = hostfs_prepare_write,
503 .commit_write = hostfs_commit_write
506 static int init_inode(struct inode *inode, struct dentry *dentry)
509 int type, err = -ENOMEM, rdev;
512 name = dentry_name(dentry, 0);
515 type = file_type(name, &rdev);
518 else type = OS_TYPE_DIR;
521 if(type == OS_TYPE_SYMLINK)
522 inode->i_op = &page_symlink_inode_operations;
523 else if(type == OS_TYPE_DIR)
524 inode->i_op = &hostfs_dir_iops;
525 else inode->i_op = &hostfs_iops;
527 if(type == OS_TYPE_DIR) inode->i_fop = &hostfs_dir_fops;
528 else inode->i_fop = &hostfs_file_fops;
530 if(type == OS_TYPE_SYMLINK)
531 inode->i_mapping->a_ops = &hostfs_link_aops;
532 else inode->i_mapping->a_ops = &hostfs_aops;
535 case OS_TYPE_CHARDEV:
536 init_special_inode(inode, S_IFCHR, rdev);
538 case OS_TYPE_BLOCKDEV:
539 init_special_inode(inode, S_IFBLK, rdev);
542 init_special_inode(inode, S_IFIFO, 0);
545 init_special_inode(inode, S_IFSOCK, 0);
552 int hostfs_create(struct inode *dir, struct dentry *dentry, int mode,
553 struct nameidata *nd)
560 inode = iget(dir->i_sb, 0);
561 if(inode == NULL) goto out;
563 error = init_inode(inode, dentry);
568 name = dentry_name(dentry, 0);
572 fd = file_create(name,
573 mode & S_IRUSR, mode & S_IWUSR, mode & S_IXUSR,
574 mode & S_IRGRP, mode & S_IWGRP, mode & S_IXGRP,
575 mode & S_IROTH, mode & S_IWOTH, mode & S_IXOTH);
578 else error = read_name(inode, name);
584 HOSTFS_I(inode)->fd = fd;
585 HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE;
586 d_instantiate(dentry, inode);
595 struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry,
596 struct nameidata *nd)
603 inode = iget(ino->i_sb, 0);
607 err = init_inode(inode, dentry);
612 name = dentry_name(dentry, 0);
616 err = read_name(inode, name);
625 d_add(dentry, inode);
626 dentry->d_op = &hostfs_dentry_ops;
632 return(ERR_PTR(err));
635 static char *inode_dentry_name(struct inode *ino, struct dentry *dentry)
640 file = inode_name(ino, dentry->d_name.len + 1);
641 if(file == NULL) return(NULL);
644 strncat(file, dentry->d_name.name, dentry->d_name.len);
645 file[len + dentry->d_name.len] = '\0';
649 int hostfs_link(struct dentry *to, struct inode *ino, struct dentry *from)
651 char *from_name, *to_name;
654 if((from_name = inode_dentry_name(ino, from)) == NULL)
656 to_name = dentry_name(to, 0);
661 err = link_file(to_name, from_name);
667 int hostfs_unlink(struct inode *ino, struct dentry *dentry)
672 if((file = inode_dentry_name(ino, dentry)) == NULL) return(-ENOMEM);
676 err = unlink_file(file);
681 int hostfs_symlink(struct inode *ino, struct dentry *dentry, const char *to)
686 if((file = inode_dentry_name(ino, dentry)) == NULL) return(-ENOMEM);
687 err = make_symlink(file, to);
692 int hostfs_mkdir(struct inode *ino, struct dentry *dentry, int mode)
697 if((file = inode_dentry_name(ino, dentry)) == NULL) return(-ENOMEM);
698 err = do_mkdir(file, mode);
703 int hostfs_rmdir(struct inode *ino, struct dentry *dentry)
708 if((file = inode_dentry_name(ino, dentry)) == NULL) return(-ENOMEM);
709 err = do_rmdir(file);
714 int hostfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
720 inode = iget(dir->i_sb, 0);
724 err = init_inode(inode, dentry);
729 name = dentry_name(dentry, 0);
733 init_special_inode(inode, mode, dev);
734 err = do_mknod(name, mode, dev);
738 err = read_name(inode, name);
743 d_instantiate(dentry, inode);
754 int hostfs_rename(struct inode *from_ino, struct dentry *from,
755 struct inode *to_ino, struct dentry *to)
757 char *from_name, *to_name;
760 if((from_name = inode_dentry_name(from_ino, from)) == NULL)
762 if((to_name = inode_dentry_name(to_ino, to)) == NULL){
766 err = rename_file(from_name, to_name);
772 void hostfs_truncate(struct inode *ino)
777 int hostfs_permission(struct inode *ino, int desired, struct nameidata *nd)
780 int r = 0, w = 0, x = 0, err;
782 if(desired & MAY_READ) r = 1;
783 if(desired & MAY_WRITE) w = 1;
784 if(desired & MAY_EXEC) x = 1;
785 name = inode_name(ino, 0);
786 if(name == NULL) return(-ENOMEM);
787 err = access_file(name, r, w, x);
789 if(!err) err = vfs_permission(ino, desired);
793 int hostfs_setattr(struct dentry *dentry, struct iattr *attr)
795 struct hostfs_iattr attrs;
800 attr->ia_valid &= ~ATTR_SIZE;
803 if(attr->ia_valid & ATTR_MODE){
804 attrs.ia_valid |= HOSTFS_ATTR_MODE;
805 attrs.ia_mode = attr->ia_mode;
807 if(attr->ia_valid & ATTR_UID){
808 if((dentry->d_inode->i_sb->s_dev == ROOT_DEV) &&
810 attr->ia_uid = getuid();
811 attrs.ia_valid |= HOSTFS_ATTR_UID;
812 attrs.ia_uid = attr->ia_uid;
814 if(attr->ia_valid & ATTR_GID){
815 if((dentry->d_inode->i_sb->s_dev == ROOT_DEV) &&
817 attr->ia_gid = getuid();
818 attrs.ia_valid |= HOSTFS_ATTR_GID;
819 attrs.ia_gid = attr->ia_gid;
821 if(attr->ia_valid & ATTR_SIZE){
822 attrs.ia_valid |= HOSTFS_ATTR_SIZE;
823 attrs.ia_size = attr->ia_size;
825 if(attr->ia_valid & ATTR_ATIME){
826 attrs.ia_valid |= HOSTFS_ATTR_ATIME;
827 attrs.ia_atime = attr->ia_atime;
829 if(attr->ia_valid & ATTR_MTIME){
830 attrs.ia_valid |= HOSTFS_ATTR_MTIME;
831 attrs.ia_mtime = attr->ia_mtime;
833 if(attr->ia_valid & ATTR_CTIME){
834 attrs.ia_valid |= HOSTFS_ATTR_CTIME;
835 attrs.ia_ctime = attr->ia_ctime;
837 if(attr->ia_valid & ATTR_ATIME_SET){
838 attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET;
840 if(attr->ia_valid & ATTR_MTIME_SET){
841 attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET;
843 name = dentry_name(dentry, 0);
844 if(name == NULL) return(-ENOMEM);
845 err = set_attr(name, &attrs);
850 return(inode_setattr(dentry->d_inode, attr));
853 int hostfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
856 generic_fillattr(dentry->d_inode, stat);
860 static struct inode_operations hostfs_iops = {
861 .create = hostfs_create,
863 .unlink = hostfs_unlink,
864 .symlink = hostfs_symlink,
865 .mkdir = hostfs_mkdir,
866 .rmdir = hostfs_rmdir,
867 .mknod = hostfs_mknod,
868 .rename = hostfs_rename,
869 .truncate = hostfs_truncate,
870 .permission = hostfs_permission,
871 .setattr = hostfs_setattr,
872 .getattr = hostfs_getattr,
875 static struct inode_operations hostfs_dir_iops = {
876 .create = hostfs_create,
877 .lookup = hostfs_lookup,
879 .unlink = hostfs_unlink,
880 .symlink = hostfs_symlink,
881 .mkdir = hostfs_mkdir,
882 .rmdir = hostfs_rmdir,
883 .mknod = hostfs_mknod,
884 .rename = hostfs_rename,
885 .truncate = hostfs_truncate,
886 .permission = hostfs_permission,
887 .setattr = hostfs_setattr,
888 .getattr = hostfs_getattr,
891 int hostfs_link_readpage(struct file *file, struct page *page)
897 start = page->index << PAGE_CACHE_SHIFT;
899 name = inode_name(page->mapping->host, 0);
900 if(name == NULL) return(-ENOMEM);
901 err = do_readlink(name, buffer, PAGE_CACHE_SIZE);
903 if(err == PAGE_CACHE_SIZE)
906 flush_dcache_page(page);
907 SetPageUptodate(page);
908 if (PageError(page)) ClearPageError(page);
916 static struct address_space_operations hostfs_link_aops = {
917 .readpage = hostfs_link_readpage,
920 static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent)
922 struct inode *root_inode;
923 char *name, *data = d;
926 sb->s_blocksize = 1024;
927 sb->s_blocksize_bits = 10;
928 sb->s_magic = HOSTFS_SUPER_MAGIC;
929 sb->s_op = &hostfs_sbops;
931 if((data == NULL) || (*data == '\0'))
935 name = kmalloc(strlen(data) + 1, GFP_KERNEL);
941 root_inode = iget(sb, 0);
942 if(root_inode == NULL)
945 err = init_inode(root_inode, NULL);
949 HOSTFS_I(root_inode)->host_filename = name;
952 sb->s_root = d_alloc_root(root_inode);
953 if(sb->s_root == NULL)
956 err = read_inode(root_inode);
970 static struct super_block *hostfs_read_sb(struct file_system_type *type,
971 int flags, const char *dev_name,
974 return(get_sb_nodev(type, flags, data, hostfs_fill_sb_common));
977 static struct file_system_type hostfs_type = {
978 .owner = THIS_MODULE,
980 .get_sb = hostfs_read_sb,
981 .kill_sb = kill_anon_super,
985 static int __init init_hostfs(void)
987 return(register_filesystem(&hostfs_type));
990 static void __exit exit_hostfs(void)
992 unregister_filesystem(&hostfs_type);
995 module_init(init_hostfs)
996 module_exit(exit_hostfs)
997 MODULE_LICENSE("GPL");
1000 * Overrides for Emacs so that we follow Linus's tabbing style.
1001 * Emacs will notice this stuff at the end of the file and automatically
1002 * adjust the settings for this buffer only. This must remain at the end
1004 * ---------------------------------------------------------------------------
1006 * c-file-style: "linux"