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/module.h>
12 #include <linux/init.h>
13 #include <linux/slab.h>
14 #include <linux/pagemap.h>
15 #include <linux/blkdev.h>
16 #include <linux/list.h>
17 #include <linux/statfs.h>
18 #include <linux/kdev_t.h>
19 #include <asm/uaccess.h>
21 #include "kern_util.h"
25 struct hostfs_inode_info {
29 struct inode vfs_inode;
32 static inline struct hostfs_inode_info *HOSTFS_I(struct inode *inode)
34 return(list_entry(inode, struct hostfs_inode_info, vfs_inode));
37 #define FILE_HOSTFS_I(file) HOSTFS_I((file)->f_path.dentry->d_inode)
39 int hostfs_d_delete(struct dentry *dentry)
44 struct dentry_operations hostfs_dentry_ops = {
45 .d_delete = hostfs_d_delete,
48 /* Changed in hostfs_args before the kernel starts running */
49 static char *root_ino = "";
50 static int append = 0;
52 #define HOSTFS_SUPER_MAGIC 0x00c0ffee
54 static const struct inode_operations hostfs_iops;
55 static const struct inode_operations hostfs_dir_iops;
56 static const struct address_space_operations hostfs_link_aops;
59 static int __init hostfs_args(char *options, int *add)
63 ptr = strchr(options, ',');
71 ptr = strchr(options, ',');
75 if(!strcmp(options, "append"))
77 else printf("hostfs_args - unsupported option - %s\n",
85 __uml_setup("hostfs=", hostfs_args,
86 "hostfs=<root dir>,<flags>,...\n"
87 " This is used to set hostfs parameters. The root directory argument\n"
88 " is used to confine all hostfs mounts to within the specified directory\n"
89 " tree on the host. If this isn't specified, then a user inside UML can\n"
90 " mount anything on the host that's accessible to the user that's running\n"
92 " The only flag currently supported is 'append', which specifies that all\n"
93 " files opened by hostfs will be opened in append mode.\n\n"
97 static char *dentry_name(struct dentry *dentry, int extra)
99 struct dentry *parent;
105 while(parent->d_parent != parent){
106 len += parent->d_name.len + 1;
107 parent = parent->d_parent;
110 root = HOSTFS_I(parent->d_inode)->host_filename;
112 name = kmalloc(len + extra + 1, GFP_KERNEL);
113 if(name == NULL) return(NULL);
117 while(parent->d_parent != parent){
118 len -= parent->d_name.len + 1;
120 strncpy(&name[len + 1], parent->d_name.name,
122 parent = parent->d_parent;
124 strncpy(name, root, strlen(root));
128 static char *inode_name(struct inode *ino, int extra)
130 struct dentry *dentry;
132 dentry = list_entry(ino->i_dentry.next, struct dentry, d_alias);
133 return(dentry_name(dentry, extra));
136 static int read_name(struct inode *ino, char *name)
138 /* The non-int inode fields are copied into ints by stat_file and
139 * then copied into the inode because passing the actual pointers
140 * in and having them treated as int * breaks on big-endian machines
143 int i_mode, i_nlink, i_blksize;
144 unsigned long long i_size;
145 unsigned long long i_ino;
146 unsigned long long i_blocks;
148 err = stat_file(name, &i_ino, &i_mode, &i_nlink, &ino->i_uid,
149 &ino->i_gid, &i_size, &ino->i_atime, &ino->i_mtime,
150 &ino->i_ctime, &i_blksize, &i_blocks);
155 ino->i_mode = i_mode;
156 ino->i_nlink = i_nlink;
157 ino->i_size = i_size;
158 ino->i_blocks = i_blocks;
162 static char *follow_link(char *link)
165 char *name, *resolved, *end;
170 name = kmalloc(len, GFP_KERNEL);
174 n = do_readlink(link, name, len);
186 end = strrchr(link, '/');
191 len = strlen(link) + strlen(name) + 1;
193 resolved = kmalloc(len, GFP_KERNEL);
194 if(resolved == NULL){
199 sprintf(resolved, "%s%s", link, name);
210 static int read_inode(struct inode *ino)
215 /* Unfortunately, we are called from iget() when we don't have a dentry
218 if(list_empty(&ino->i_dentry))
222 name = inode_name(ino, 0);
226 if(file_type(name, NULL, NULL) == OS_TYPE_SYMLINK){
227 name = follow_link(name);
234 err = read_name(ino, name);
240 int hostfs_statfs(struct dentry *dentry, struct kstatfs *sf)
242 /* do_statfs uses struct statfs64 internally, but the linux kernel
243 * struct statfs still has 32-bit versions for most of these fields,
244 * so we convert them here
253 err = do_statfs(HOSTFS_I(dentry->d_sb->s_root->d_inode)->host_filename,
254 &sf->f_bsize, &f_blocks, &f_bfree, &f_bavail, &f_files,
255 &f_ffree, &sf->f_fsid, sizeof(sf->f_fsid),
256 &sf->f_namelen, sf->f_spare);
258 sf->f_blocks = f_blocks;
259 sf->f_bfree = f_bfree;
260 sf->f_bavail = f_bavail;
261 sf->f_files = f_files;
262 sf->f_ffree = f_ffree;
263 sf->f_type = HOSTFS_SUPER_MAGIC;
267 static struct inode *hostfs_alloc_inode(struct super_block *sb)
269 struct hostfs_inode_info *hi;
271 hi = kmalloc(sizeof(*hi), GFP_KERNEL);
275 *hi = ((struct hostfs_inode_info) { .host_filename = NULL,
278 inode_init_once(&hi->vfs_inode);
279 return(&hi->vfs_inode);
282 static void hostfs_delete_inode(struct inode *inode)
284 truncate_inode_pages(&inode->i_data, 0);
285 if(HOSTFS_I(inode)->fd != -1) {
286 close_file(&HOSTFS_I(inode)->fd);
287 HOSTFS_I(inode)->fd = -1;
292 static void hostfs_destroy_inode(struct inode *inode)
294 kfree(HOSTFS_I(inode)->host_filename);
296 /*XXX: This should not happen, probably. The check is here for
297 * additional safety.*/
298 if(HOSTFS_I(inode)->fd != -1) {
299 close_file(&HOSTFS_I(inode)->fd);
300 printk(KERN_DEBUG "Closing host fd in .destroy_inode\n");
303 kfree(HOSTFS_I(inode));
306 static void hostfs_read_inode(struct inode *inode)
311 static const struct super_operations hostfs_sbops = {
312 .alloc_inode = hostfs_alloc_inode,
313 .drop_inode = generic_delete_inode,
314 .delete_inode = hostfs_delete_inode,
315 .destroy_inode = hostfs_destroy_inode,
316 .read_inode = hostfs_read_inode,
317 .statfs = hostfs_statfs,
320 int hostfs_readdir(struct file *file, void *ent, filldir_t filldir)
324 unsigned long long next, ino;
327 name = dentry_name(file->f_path.dentry, 0);
328 if(name == NULL) return(-ENOMEM);
329 dir = open_dir(name, &error);
331 if(dir == NULL) return(-error);
333 while((name = read_dir(dir, &next, &ino, &len)) != NULL){
334 error = (*filldir)(ent, name, len, file->f_pos,
343 int hostfs_file_open(struct inode *ino, struct file *file)
346 int mode = 0, r = 0, w = 0, fd;
348 mode = file->f_mode & (FMODE_READ | FMODE_WRITE);
349 if((mode & HOSTFS_I(ino)->mode) == mode)
352 /* The file may already have been opened, but with the wrong access,
353 * so this resets things and reopens the file with the new access.
355 if(HOSTFS_I(ino)->fd != -1){
356 close_file(&HOSTFS_I(ino)->fd);
357 HOSTFS_I(ino)->fd = -1;
360 HOSTFS_I(ino)->mode |= mode;
361 if(HOSTFS_I(ino)->mode & FMODE_READ)
363 if(HOSTFS_I(ino)->mode & FMODE_WRITE)
368 name = dentry_name(file->f_path.dentry, 0);
372 fd = open_file(name, r, w, append);
374 if(fd < 0) return(fd);
375 FILE_HOSTFS_I(file)->fd = fd;
380 int hostfs_fsync(struct file *file, struct dentry *dentry, int datasync)
382 return fsync_file(HOSTFS_I(dentry->d_inode)->fd, datasync);
385 static const struct file_operations hostfs_file_fops = {
386 .llseek = generic_file_llseek,
387 .read = do_sync_read,
388 .sendfile = generic_file_sendfile,
389 .aio_read = generic_file_aio_read,
390 .aio_write = generic_file_aio_write,
391 .write = do_sync_write,
392 .mmap = generic_file_mmap,
393 .open = hostfs_file_open,
395 .fsync = hostfs_fsync,
398 static const struct file_operations hostfs_dir_fops = {
399 .llseek = generic_file_llseek,
400 .readdir = hostfs_readdir,
401 .read = generic_read_dir,
404 int hostfs_writepage(struct page *page, struct writeback_control *wbc)
406 struct address_space *mapping = page->mapping;
407 struct inode *inode = mapping->host;
409 unsigned long long base;
410 int count = PAGE_CACHE_SIZE;
411 int end_index = inode->i_size >> PAGE_CACHE_SHIFT;
414 if (page->index >= end_index)
415 count = inode->i_size & (PAGE_CACHE_SIZE-1);
418 base = ((unsigned long long) page->index) << PAGE_CACHE_SHIFT;
420 err = write_file(HOSTFS_I(inode)->fd, &base, buffer, count);
422 ClearPageUptodate(page);
426 if (base > inode->i_size)
427 inode->i_size = base;
430 ClearPageError(page);
440 int hostfs_readpage(struct file *file, struct page *page)
446 start = (long long) page->index << PAGE_CACHE_SHIFT;
448 err = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer,
450 if(err < 0) goto out;
452 memset(&buffer[err], 0, PAGE_CACHE_SIZE - err);
454 flush_dcache_page(page);
455 SetPageUptodate(page);
456 if (PageError(page)) ClearPageError(page);
464 int hostfs_prepare_write(struct file *file, struct page *page,
465 unsigned int from, unsigned int to)
468 long long start, tmp;
471 start = (long long) page->index << PAGE_CACHE_SHIFT;
475 err = read_file(FILE_HOSTFS_I(file)->fd, &tmp, buffer,
477 if(err < 0) goto out;
479 if(to != PAGE_CACHE_SIZE){
481 err = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer + to,
482 PAGE_CACHE_SIZE - to);
483 if(err < 0) goto out;
491 int hostfs_commit_write(struct file *file, struct page *page, unsigned from,
494 struct address_space *mapping = page->mapping;
495 struct inode *inode = mapping->host;
500 start = (((long long) page->index) << PAGE_CACHE_SHIFT) + from;
502 err = write_file(FILE_HOSTFS_I(file)->fd, &start, buffer + from,
506 /* Actually, if !err, write_file has added to-from to start, so, despite
507 * the appearance, we are comparing i_size against the _last_ written
508 * location, as we should. */
510 if(!err && (start > inode->i_size))
511 inode->i_size = start;
517 static const struct address_space_operations hostfs_aops = {
518 .writepage = hostfs_writepage,
519 .readpage = hostfs_readpage,
520 .set_page_dirty = __set_page_dirty_nobuffers,
521 .prepare_write = hostfs_prepare_write,
522 .commit_write = hostfs_commit_write
525 static int init_inode(struct inode *inode, struct dentry *dentry)
528 int type, err = -ENOMEM;
533 name = dentry_name(dentry, 0);
536 type = file_type(name, &maj, &min);
537 /*Reencode maj and min with the kernel encoding.*/
538 rdev = MKDEV(maj, min);
541 else type = OS_TYPE_DIR;
544 if(type == OS_TYPE_SYMLINK)
545 inode->i_op = &page_symlink_inode_operations;
546 else if(type == OS_TYPE_DIR)
547 inode->i_op = &hostfs_dir_iops;
548 else inode->i_op = &hostfs_iops;
550 if(type == OS_TYPE_DIR) inode->i_fop = &hostfs_dir_fops;
551 else inode->i_fop = &hostfs_file_fops;
553 if(type == OS_TYPE_SYMLINK)
554 inode->i_mapping->a_ops = &hostfs_link_aops;
555 else inode->i_mapping->a_ops = &hostfs_aops;
558 case OS_TYPE_CHARDEV:
559 init_special_inode(inode, S_IFCHR, rdev);
561 case OS_TYPE_BLOCKDEV:
562 init_special_inode(inode, S_IFBLK, rdev);
565 init_special_inode(inode, S_IFIFO, 0);
568 init_special_inode(inode, S_IFSOCK, 0);
575 int hostfs_create(struct inode *dir, struct dentry *dentry, int mode,
576 struct nameidata *nd)
583 inode = iget(dir->i_sb, 0);
584 if(inode == NULL) goto out;
586 error = init_inode(inode, dentry);
591 name = dentry_name(dentry, 0);
595 fd = file_create(name,
596 mode & S_IRUSR, mode & S_IWUSR, mode & S_IXUSR,
597 mode & S_IRGRP, mode & S_IWGRP, mode & S_IXGRP,
598 mode & S_IROTH, mode & S_IWOTH, mode & S_IXOTH);
601 else error = read_name(inode, name);
607 HOSTFS_I(inode)->fd = fd;
608 HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE;
609 d_instantiate(dentry, inode);
618 struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry,
619 struct nameidata *nd)
626 inode = iget(ino->i_sb, 0);
630 err = init_inode(inode, dentry);
635 name = dentry_name(dentry, 0);
639 err = read_name(inode, name);
648 d_add(dentry, inode);
649 dentry->d_op = &hostfs_dentry_ops;
655 return(ERR_PTR(err));
658 static char *inode_dentry_name(struct inode *ino, struct dentry *dentry)
663 file = inode_name(ino, dentry->d_name.len + 1);
664 if(file == NULL) return(NULL);
667 strncat(file, dentry->d_name.name, dentry->d_name.len);
668 file[len + dentry->d_name.len] = '\0';
672 int hostfs_link(struct dentry *to, struct inode *ino, struct dentry *from)
674 char *from_name, *to_name;
677 if((from_name = inode_dentry_name(ino, from)) == NULL)
679 to_name = dentry_name(to, 0);
684 err = link_file(to_name, from_name);
690 int hostfs_unlink(struct inode *ino, struct dentry *dentry)
695 if((file = inode_dentry_name(ino, dentry)) == NULL) return(-ENOMEM);
699 err = unlink_file(file);
704 int hostfs_symlink(struct inode *ino, struct dentry *dentry, const char *to)
709 if((file = inode_dentry_name(ino, dentry)) == NULL) return(-ENOMEM);
710 err = make_symlink(file, to);
715 int hostfs_mkdir(struct inode *ino, struct dentry *dentry, int mode)
720 if((file = inode_dentry_name(ino, dentry)) == NULL) return(-ENOMEM);
721 err = do_mkdir(file, mode);
726 int hostfs_rmdir(struct inode *ino, struct dentry *dentry)
731 if((file = inode_dentry_name(ino, dentry)) == NULL) return(-ENOMEM);
732 err = do_rmdir(file);
737 int hostfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
743 inode = iget(dir->i_sb, 0);
747 err = init_inode(inode, dentry);
752 name = dentry_name(dentry, 0);
756 init_special_inode(inode, mode, dev);
757 err = do_mknod(name, mode, MAJOR(dev), MINOR(dev));
761 err = read_name(inode, name);
766 d_instantiate(dentry, inode);
777 int hostfs_rename(struct inode *from_ino, struct dentry *from,
778 struct inode *to_ino, struct dentry *to)
780 char *from_name, *to_name;
783 if((from_name = inode_dentry_name(from_ino, from)) == NULL)
785 if((to_name = inode_dentry_name(to_ino, to)) == NULL){
789 err = rename_file(from_name, to_name);
795 int hostfs_permission(struct inode *ino, int desired, struct nameidata *nd)
798 int r = 0, w = 0, x = 0, err;
800 if (desired & MAY_READ) r = 1;
801 if (desired & MAY_WRITE) w = 1;
802 if (desired & MAY_EXEC) x = 1;
803 name = inode_name(ino, 0);
804 if (name == NULL) return(-ENOMEM);
806 if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) ||
807 S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode))
810 err = access_file(name, r, w, x);
813 err = generic_permission(ino, desired, NULL);
817 int hostfs_setattr(struct dentry *dentry, struct iattr *attr)
819 struct hostfs_iattr attrs;
823 err = inode_change_ok(dentry->d_inode, attr);
828 attr->ia_valid &= ~ATTR_SIZE;
831 if(attr->ia_valid & ATTR_MODE){
832 attrs.ia_valid |= HOSTFS_ATTR_MODE;
833 attrs.ia_mode = attr->ia_mode;
835 if(attr->ia_valid & ATTR_UID){
836 attrs.ia_valid |= HOSTFS_ATTR_UID;
837 attrs.ia_uid = attr->ia_uid;
839 if(attr->ia_valid & ATTR_GID){
840 attrs.ia_valid |= HOSTFS_ATTR_GID;
841 attrs.ia_gid = attr->ia_gid;
843 if(attr->ia_valid & ATTR_SIZE){
844 attrs.ia_valid |= HOSTFS_ATTR_SIZE;
845 attrs.ia_size = attr->ia_size;
847 if(attr->ia_valid & ATTR_ATIME){
848 attrs.ia_valid |= HOSTFS_ATTR_ATIME;
849 attrs.ia_atime = attr->ia_atime;
851 if(attr->ia_valid & ATTR_MTIME){
852 attrs.ia_valid |= HOSTFS_ATTR_MTIME;
853 attrs.ia_mtime = attr->ia_mtime;
855 if(attr->ia_valid & ATTR_CTIME){
856 attrs.ia_valid |= HOSTFS_ATTR_CTIME;
857 attrs.ia_ctime = attr->ia_ctime;
859 if(attr->ia_valid & ATTR_ATIME_SET){
860 attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET;
862 if(attr->ia_valid & ATTR_MTIME_SET){
863 attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET;
865 name = dentry_name(dentry, 0);
866 if(name == NULL) return(-ENOMEM);
867 err = set_attr(name, &attrs);
872 return(inode_setattr(dentry->d_inode, attr));
875 int hostfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
878 generic_fillattr(dentry->d_inode, stat);
882 static const struct inode_operations hostfs_iops = {
883 .create = hostfs_create,
885 .unlink = hostfs_unlink,
886 .symlink = hostfs_symlink,
887 .mkdir = hostfs_mkdir,
888 .rmdir = hostfs_rmdir,
889 .mknod = hostfs_mknod,
890 .rename = hostfs_rename,
891 .permission = hostfs_permission,
892 .setattr = hostfs_setattr,
893 .getattr = hostfs_getattr,
896 static const struct inode_operations hostfs_dir_iops = {
897 .create = hostfs_create,
898 .lookup = hostfs_lookup,
900 .unlink = hostfs_unlink,
901 .symlink = hostfs_symlink,
902 .mkdir = hostfs_mkdir,
903 .rmdir = hostfs_rmdir,
904 .mknod = hostfs_mknod,
905 .rename = hostfs_rename,
906 .permission = hostfs_permission,
907 .setattr = hostfs_setattr,
908 .getattr = hostfs_getattr,
911 int hostfs_link_readpage(struct file *file, struct page *page)
917 name = inode_name(page->mapping->host, 0);
918 if(name == NULL) return(-ENOMEM);
919 err = do_readlink(name, buffer, PAGE_CACHE_SIZE);
921 if(err == PAGE_CACHE_SIZE)
924 flush_dcache_page(page);
925 SetPageUptodate(page);
926 if (PageError(page)) ClearPageError(page);
934 static const struct address_space_operations hostfs_link_aops = {
935 .readpage = hostfs_link_readpage,
938 static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent)
940 struct inode *root_inode;
941 char *host_root_path, *req_root = d;
944 sb->s_blocksize = 1024;
945 sb->s_blocksize_bits = 10;
946 sb->s_magic = HOSTFS_SUPER_MAGIC;
947 sb->s_op = &hostfs_sbops;
949 /* NULL is printed as <NULL> by sprintf: avoid that. */
950 if (req_root == NULL)
954 host_root_path = kmalloc(strlen(root_ino) + 1
955 + strlen(req_root) + 1, GFP_KERNEL);
956 if(host_root_path == NULL)
959 sprintf(host_root_path, "%s/%s", root_ino, req_root);
961 root_inode = iget(sb, 0);
962 if(root_inode == NULL)
965 err = init_inode(root_inode, NULL);
969 HOSTFS_I(root_inode)->host_filename = host_root_path;
970 /* Avoid that in the error path, iput(root_inode) frees again
971 * host_root_path through hostfs_destroy_inode! */
972 host_root_path = NULL;
975 sb->s_root = d_alloc_root(root_inode);
976 if(sb->s_root == NULL)
979 err = read_inode(root_inode);
981 /* No iput in this case because the dput does that for us */
992 kfree(host_root_path);
997 static int hostfs_read_sb(struct file_system_type *type,
998 int flags, const char *dev_name,
999 void *data, struct vfsmount *mnt)
1001 return get_sb_nodev(type, flags, data, hostfs_fill_sb_common, mnt);
1004 static struct file_system_type hostfs_type = {
1005 .owner = THIS_MODULE,
1007 .get_sb = hostfs_read_sb,
1008 .kill_sb = kill_anon_super,
1012 static int __init init_hostfs(void)
1014 return(register_filesystem(&hostfs_type));
1017 static void __exit exit_hostfs(void)
1019 unregister_filesystem(&hostfs_type);
1022 module_init(init_hostfs)
1023 module_exit(exit_hostfs)
1024 MODULE_LICENSE("GPL");
1027 * Overrides for Emacs so that we follow Linus's tabbing style.
1028 * Emacs will notice this stuff at the end of the file and automatically
1029 * adjust the settings for this buffer only. This must remain at the end
1031 * ---------------------------------------------------------------------------
1033 * c-file-style: "linux"