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/statfs.h>
19 #include <linux/kdev_t.h>
20 #include <asm/uaccess.h>
22 #include "kern_util.h"
24 #include "user_util.h"
27 struct hostfs_inode_info {
31 struct inode vfs_inode;
34 static inline struct hostfs_inode_info *HOSTFS_I(struct inode *inode)
36 return(list_entry(inode, struct hostfs_inode_info, vfs_inode));
39 #define FILE_HOSTFS_I(file) HOSTFS_I((file)->f_dentry->d_inode)
41 int hostfs_d_delete(struct dentry *dentry)
46 struct dentry_operations hostfs_dentry_ops = {
47 .d_delete = hostfs_d_delete,
50 /* Changed in hostfs_args before the kernel starts running */
51 static char *root_ino = "/";
52 static int append = 0;
54 #define HOSTFS_SUPER_MAGIC 0x00c0ffee
56 static struct inode_operations hostfs_iops;
57 static struct inode_operations hostfs_dir_iops;
58 static struct address_space_operations hostfs_link_aops;
61 static int __init hostfs_args(char *options, int *add)
65 ptr = strchr(options, ',');
73 ptr = strchr(options, ',');
77 if(!strcmp(options, "append"))
79 else printf("hostfs_args - unsupported option - %s\n",
87 __uml_setup("hostfs=", hostfs_args,
88 "hostfs=<root dir>,<flags>,...\n"
89 " This is used to set hostfs parameters. The root directory argument\n"
90 " is used to confine all hostfs mounts to within the specified directory\n"
91 " tree on the host. If this isn't specified, then a user inside UML can\n"
92 " mount anything on the host that's accessible to the user that's running\n"
94 " The only flag currently supported is 'append', which specifies that all\n"
95 " 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;
165 static char *follow_link(char *link)
168 char *name, *resolved, *end;
173 name = kmalloc(len, GFP_KERNEL);
177 n = do_readlink(link, name, len);
189 end = strrchr(link, '/');
194 len = strlen(link) + strlen(name) + 1;
196 resolved = kmalloc(len, GFP_KERNEL);
197 if(resolved == NULL){
202 sprintf(resolved, "%s%s", link, name);
213 static int read_inode(struct inode *ino)
218 /* Unfortunately, we are called from iget() when we don't have a dentry
221 if(list_empty(&ino->i_dentry))
225 name = inode_name(ino, 0);
229 if(file_type(name, NULL, NULL) == OS_TYPE_SYMLINK){
230 name = follow_link(name);
237 err = read_name(ino, name);
243 int hostfs_statfs(struct super_block *sb, struct kstatfs *sf)
245 /* do_statfs uses struct statfs64 internally, but the linux kernel
246 * struct statfs still has 32-bit versions for most of these fields,
247 * so we convert them here
256 err = do_statfs(HOSTFS_I(sb->s_root->d_inode)->host_filename,
257 &sf->f_bsize, &f_blocks, &f_bfree, &f_bavail, &f_files,
258 &f_ffree, &sf->f_fsid, sizeof(sf->f_fsid),
259 &sf->f_namelen, sf->f_spare);
261 sf->f_blocks = f_blocks;
262 sf->f_bfree = f_bfree;
263 sf->f_bavail = f_bavail;
264 sf->f_files = f_files;
265 sf->f_ffree = f_ffree;
266 sf->f_type = HOSTFS_SUPER_MAGIC;
270 static struct inode *hostfs_alloc_inode(struct super_block *sb)
272 struct hostfs_inode_info *hi;
274 hi = kmalloc(sizeof(*hi), GFP_KERNEL);
278 *hi = ((struct hostfs_inode_info) { .host_filename = NULL,
281 inode_init_once(&hi->vfs_inode);
282 return(&hi->vfs_inode);
285 static void hostfs_delete_inode(struct inode *inode)
287 truncate_inode_pages(&inode->i_data, 0);
288 if(HOSTFS_I(inode)->fd != -1) {
289 close_file(&HOSTFS_I(inode)->fd);
290 HOSTFS_I(inode)->fd = -1;
295 static void hostfs_destroy_inode(struct inode *inode)
297 if(HOSTFS_I(inode)->host_filename)
298 kfree(HOSTFS_I(inode)->host_filename);
300 /*XXX: This should not happen, probably. The check is here for
301 * additional safety.*/
302 if(HOSTFS_I(inode)->fd != -1) {
303 close_file(&HOSTFS_I(inode)->fd);
304 printk(KERN_DEBUG "Closing host fd in .destroy_inode\n");
307 kfree(HOSTFS_I(inode));
310 static void hostfs_read_inode(struct inode *inode)
315 static struct super_operations hostfs_sbops = {
316 .alloc_inode = hostfs_alloc_inode,
317 .drop_inode = generic_delete_inode,
318 .delete_inode = hostfs_delete_inode,
319 .destroy_inode = hostfs_destroy_inode,
320 .read_inode = hostfs_read_inode,
321 .statfs = hostfs_statfs,
324 int hostfs_readdir(struct file *file, void *ent, filldir_t filldir)
328 unsigned long long next, ino;
331 name = dentry_name(file->f_dentry, 0);
332 if(name == NULL) return(-ENOMEM);
333 dir = open_dir(name, &error);
335 if(dir == NULL) return(-error);
337 while((name = read_dir(dir, &next, &ino, &len)) != NULL){
338 error = (*filldir)(ent, name, len, file->f_pos,
347 int hostfs_file_open(struct inode *ino, struct file *file)
350 int mode = 0, r = 0, w = 0, fd;
352 mode = file->f_mode & (FMODE_READ | FMODE_WRITE);
353 if((mode & HOSTFS_I(ino)->mode) == mode)
356 /* The file may already have been opened, but with the wrong access,
357 * so this resets things and reopens the file with the new access.
359 if(HOSTFS_I(ino)->fd != -1){
360 close_file(&HOSTFS_I(ino)->fd);
361 HOSTFS_I(ino)->fd = -1;
364 HOSTFS_I(ino)->mode |= mode;
365 if(HOSTFS_I(ino)->mode & FMODE_READ)
367 if(HOSTFS_I(ino)->mode & FMODE_WRITE)
372 name = dentry_name(file->f_dentry, 0);
376 fd = open_file(name, r, w, append);
378 if(fd < 0) return(fd);
379 FILE_HOSTFS_I(file)->fd = fd;
384 int hostfs_fsync(struct file *file, struct dentry *dentry, int datasync)
386 return fsync_file(HOSTFS_I(dentry->d_inode)->fd, datasync);
389 static struct file_operations hostfs_file_fops = {
390 .llseek = generic_file_llseek,
391 .read = generic_file_read,
392 .sendfile = generic_file_sendfile,
393 .aio_read = generic_file_aio_read,
394 .aio_write = generic_file_aio_write,
395 .readv = generic_file_readv,
396 .writev = generic_file_writev,
397 .write = generic_file_write,
398 .mmap = generic_file_mmap,
399 .open = hostfs_file_open,
401 .fsync = hostfs_fsync,
404 static struct file_operations hostfs_dir_fops = {
405 .llseek = generic_file_llseek,
406 .readdir = hostfs_readdir,
407 .read = generic_read_dir,
410 int hostfs_writepage(struct page *page, struct writeback_control *wbc)
412 struct address_space *mapping = page->mapping;
413 struct inode *inode = mapping->host;
415 unsigned long long base;
416 int count = PAGE_CACHE_SIZE;
417 int end_index = inode->i_size >> PAGE_CACHE_SHIFT;
420 if (page->index >= end_index)
421 count = inode->i_size & (PAGE_CACHE_SIZE-1);
424 base = ((unsigned long long) page->index) << PAGE_CACHE_SHIFT;
426 err = write_file(HOSTFS_I(inode)->fd, &base, buffer, count);
428 ClearPageUptodate(page);
432 if (base > inode->i_size)
433 inode->i_size = base;
436 ClearPageError(page);
446 int hostfs_readpage(struct file *file, struct page *page)
452 start = (long long) page->index << PAGE_CACHE_SHIFT;
454 err = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer,
456 if(err < 0) goto out;
458 memset(&buffer[err], 0, PAGE_CACHE_SIZE - err);
460 flush_dcache_page(page);
461 SetPageUptodate(page);
462 if (PageError(page)) ClearPageError(page);
470 int hostfs_prepare_write(struct file *file, struct page *page,
471 unsigned int from, unsigned int to)
474 long long start, tmp;
477 start = (long long) page->index << PAGE_CACHE_SHIFT;
481 err = read_file(FILE_HOSTFS_I(file)->fd, &tmp, buffer,
483 if(err < 0) goto out;
485 if(to != PAGE_CACHE_SIZE){
487 err = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer + to,
488 PAGE_CACHE_SIZE - to);
489 if(err < 0) goto out;
497 int hostfs_commit_write(struct file *file, struct page *page, unsigned from,
500 struct address_space *mapping = page->mapping;
501 struct inode *inode = mapping->host;
506 start = (long long) (page->index << PAGE_CACHE_SHIFT) + from;
508 err = write_file(FILE_HOSTFS_I(file)->fd, &start, buffer + from,
511 if(!err && (start > inode->i_size))
512 inode->i_size = start;
518 static struct address_space_operations hostfs_aops = {
519 .writepage = hostfs_writepage,
520 .readpage = hostfs_readpage,
521 .set_page_dirty = __set_page_dirty_nobuffers,
522 .prepare_write = hostfs_prepare_write,
523 .commit_write = hostfs_commit_write
526 static int init_inode(struct inode *inode, struct dentry *dentry)
529 int type, err = -ENOMEM;
534 name = dentry_name(dentry, 0);
537 type = file_type(name, &maj, &min);
538 /*Reencode maj and min with the kernel encoding.*/
539 rdev = MKDEV(maj, min);
542 else type = OS_TYPE_DIR;
545 if(type == OS_TYPE_SYMLINK)
546 inode->i_op = &page_symlink_inode_operations;
547 else if(type == OS_TYPE_DIR)
548 inode->i_op = &hostfs_dir_iops;
549 else inode->i_op = &hostfs_iops;
551 if(type == OS_TYPE_DIR) inode->i_fop = &hostfs_dir_fops;
552 else inode->i_fop = &hostfs_file_fops;
554 if(type == OS_TYPE_SYMLINK)
555 inode->i_mapping->a_ops = &hostfs_link_aops;
556 else inode->i_mapping->a_ops = &hostfs_aops;
559 case OS_TYPE_CHARDEV:
560 init_special_inode(inode, S_IFCHR, rdev);
562 case OS_TYPE_BLOCKDEV:
563 init_special_inode(inode, S_IFBLK, rdev);
566 init_special_inode(inode, S_IFIFO, 0);
569 init_special_inode(inode, S_IFSOCK, 0);
576 int hostfs_create(struct inode *dir, struct dentry *dentry, int mode,
577 struct nameidata *nd)
584 inode = iget(dir->i_sb, 0);
585 if(inode == NULL) goto out;
587 error = init_inode(inode, dentry);
592 name = dentry_name(dentry, 0);
596 fd = file_create(name,
597 mode & S_IRUSR, mode & S_IWUSR, mode & S_IXUSR,
598 mode & S_IRGRP, mode & S_IWGRP, mode & S_IXGRP,
599 mode & S_IROTH, mode & S_IWOTH, mode & S_IXOTH);
602 else error = read_name(inode, name);
608 HOSTFS_I(inode)->fd = fd;
609 HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE;
610 d_instantiate(dentry, inode);
619 struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry,
620 struct nameidata *nd)
627 inode = iget(ino->i_sb, 0);
631 err = init_inode(inode, dentry);
636 name = dentry_name(dentry, 0);
640 err = read_name(inode, name);
649 d_add(dentry, inode);
650 dentry->d_op = &hostfs_dentry_ops;
656 return(ERR_PTR(err));
659 static char *inode_dentry_name(struct inode *ino, struct dentry *dentry)
664 file = inode_name(ino, dentry->d_name.len + 1);
665 if(file == NULL) return(NULL);
668 strncat(file, dentry->d_name.name, dentry->d_name.len);
669 file[len + dentry->d_name.len] = '\0';
673 int hostfs_link(struct dentry *to, struct inode *ino, struct dentry *from)
675 char *from_name, *to_name;
678 if((from_name = inode_dentry_name(ino, from)) == NULL)
680 to_name = dentry_name(to, 0);
685 err = link_file(to_name, from_name);
691 int hostfs_unlink(struct inode *ino, struct dentry *dentry)
696 if((file = inode_dentry_name(ino, dentry)) == NULL) return(-ENOMEM);
700 err = unlink_file(file);
705 int hostfs_symlink(struct inode *ino, struct dentry *dentry, const char *to)
710 if((file = inode_dentry_name(ino, dentry)) == NULL) return(-ENOMEM);
711 err = make_symlink(file, to);
716 int hostfs_mkdir(struct inode *ino, struct dentry *dentry, int mode)
721 if((file = inode_dentry_name(ino, dentry)) == NULL) return(-ENOMEM);
722 err = do_mkdir(file, mode);
727 int hostfs_rmdir(struct inode *ino, struct dentry *dentry)
732 if((file = inode_dentry_name(ino, dentry)) == NULL) return(-ENOMEM);
733 err = do_rmdir(file);
738 int hostfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
744 inode = iget(dir->i_sb, 0);
748 err = init_inode(inode, dentry);
753 name = dentry_name(dentry, 0);
757 init_special_inode(inode, mode, dev);
758 err = do_mknod(name, mode, dev);
762 err = read_name(inode, name);
767 d_instantiate(dentry, inode);
778 int hostfs_rename(struct inode *from_ino, struct dentry *from,
779 struct inode *to_ino, struct dentry *to)
781 char *from_name, *to_name;
784 if((from_name = inode_dentry_name(from_ino, from)) == NULL)
786 if((to_name = inode_dentry_name(to_ino, to)) == NULL){
790 err = rename_file(from_name, to_name);
796 int hostfs_permission(struct inode *ino, int desired, struct nameidata *nd)
799 int r = 0, w = 0, x = 0, err;
801 if (desired & MAY_READ) r = 1;
802 if (desired & MAY_WRITE) w = 1;
803 if (desired & MAY_EXEC) x = 1;
804 name = inode_name(ino, 0);
805 if (name == NULL) return(-ENOMEM);
807 if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) ||
808 S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode))
811 err = access_file(name, r, w, x);
814 err = generic_permission(ino, desired, NULL);
818 int hostfs_setattr(struct dentry *dentry, struct iattr *attr)
820 struct hostfs_iattr attrs;
824 err = inode_change_ok(dentry->d_inode, attr);
829 attr->ia_valid &= ~ATTR_SIZE;
832 if(attr->ia_valid & ATTR_MODE){
833 attrs.ia_valid |= HOSTFS_ATTR_MODE;
834 attrs.ia_mode = attr->ia_mode;
836 if(attr->ia_valid & ATTR_UID){
837 attrs.ia_valid |= HOSTFS_ATTR_UID;
838 attrs.ia_uid = attr->ia_uid;
840 if(attr->ia_valid & ATTR_GID){
841 attrs.ia_valid |= HOSTFS_ATTR_GID;
842 attrs.ia_gid = attr->ia_gid;
844 if(attr->ia_valid & ATTR_SIZE){
845 attrs.ia_valid |= HOSTFS_ATTR_SIZE;
846 attrs.ia_size = attr->ia_size;
848 if(attr->ia_valid & ATTR_ATIME){
849 attrs.ia_valid |= HOSTFS_ATTR_ATIME;
850 attrs.ia_atime = attr->ia_atime;
852 if(attr->ia_valid & ATTR_MTIME){
853 attrs.ia_valid |= HOSTFS_ATTR_MTIME;
854 attrs.ia_mtime = attr->ia_mtime;
856 if(attr->ia_valid & ATTR_CTIME){
857 attrs.ia_valid |= HOSTFS_ATTR_CTIME;
858 attrs.ia_ctime = attr->ia_ctime;
860 if(attr->ia_valid & ATTR_ATIME_SET){
861 attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET;
863 if(attr->ia_valid & ATTR_MTIME_SET){
864 attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET;
866 name = dentry_name(dentry, 0);
867 if(name == NULL) return(-ENOMEM);
868 err = set_attr(name, &attrs);
873 return(inode_setattr(dentry->d_inode, attr));
876 int hostfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
879 generic_fillattr(dentry->d_inode, stat);
883 static struct inode_operations hostfs_iops = {
884 .create = hostfs_create,
886 .unlink = hostfs_unlink,
887 .symlink = hostfs_symlink,
888 .mkdir = hostfs_mkdir,
889 .rmdir = hostfs_rmdir,
890 .mknod = hostfs_mknod,
891 .rename = hostfs_rename,
892 .permission = hostfs_permission,
893 .setattr = hostfs_setattr,
894 .getattr = hostfs_getattr,
897 static struct inode_operations hostfs_dir_iops = {
898 .create = hostfs_create,
899 .lookup = hostfs_lookup,
901 .unlink = hostfs_unlink,
902 .symlink = hostfs_symlink,
903 .mkdir = hostfs_mkdir,
904 .rmdir = hostfs_rmdir,
905 .mknod = hostfs_mknod,
906 .rename = hostfs_rename,
907 .permission = hostfs_permission,
908 .setattr = hostfs_setattr,
909 .getattr = hostfs_getattr,
912 int hostfs_link_readpage(struct file *file, struct page *page)
918 start = page->index << PAGE_CACHE_SHIFT;
920 name = inode_name(page->mapping->host, 0);
921 if(name == NULL) return(-ENOMEM);
922 err = do_readlink(name, buffer, PAGE_CACHE_SIZE);
924 if(err == PAGE_CACHE_SIZE)
927 flush_dcache_page(page);
928 SetPageUptodate(page);
929 if (PageError(page)) ClearPageError(page);
937 static struct address_space_operations hostfs_link_aops = {
938 .readpage = hostfs_link_readpage,
941 static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent)
943 struct inode *root_inode;
944 char *name, *data = d;
947 sb->s_blocksize = 1024;
948 sb->s_blocksize_bits = 10;
949 sb->s_magic = HOSTFS_SUPER_MAGIC;
950 sb->s_op = &hostfs_sbops;
952 if((data == NULL) || (*data == '\0'))
956 name = kmalloc(strlen(data) + 1, GFP_KERNEL);
962 root_inode = iget(sb, 0);
963 if(root_inode == NULL)
966 err = init_inode(root_inode, NULL);
970 HOSTFS_I(root_inode)->host_filename = name;
973 sb->s_root = d_alloc_root(root_inode);
974 if(sb->s_root == NULL)
977 err = read_inode(root_inode);
979 /* No iput in this case because the dput does that for us */
995 static struct super_block *hostfs_read_sb(struct file_system_type *type,
996 int flags, const char *dev_name,
999 return(get_sb_nodev(type, flags, data, hostfs_fill_sb_common));
1002 static struct file_system_type hostfs_type = {
1003 .owner = THIS_MODULE,
1005 .get_sb = hostfs_read_sb,
1006 .kill_sb = kill_anon_super,
1010 static int __init init_hostfs(void)
1012 return(register_filesystem(&hostfs_type));
1015 static void __exit exit_hostfs(void)
1017 unregister_filesystem(&hostfs_type);
1020 module_init(init_hostfs)
1021 module_exit(exit_hostfs)
1022 MODULE_LICENSE("GPL");
1025 * Overrides for Emacs so that we follow Linus's tabbing style.
1026 * Emacs will notice this stuff at the end of the file and automatically
1027 * adjust the settings for this buffer only. This must remain at the end
1029 * ---------------------------------------------------------------------------
1031 * c-file-style: "linux"