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>
18 struct hostfs_inode_info
{
22 struct inode vfs_inode
;
25 static inline struct hostfs_inode_info
*HOSTFS_I(struct inode
*inode
)
27 return list_entry(inode
, struct hostfs_inode_info
, vfs_inode
);
30 #define FILE_HOSTFS_I(file) HOSTFS_I((file)->f_path.dentry->d_inode)
32 int hostfs_d_delete(struct dentry
*dentry
)
37 struct dentry_operations hostfs_dentry_ops
= {
38 .d_delete
= hostfs_d_delete
,
41 /* Changed in hostfs_args before the kernel starts running */
42 static char *root_ino
= "";
43 static int append
= 0;
45 #define HOSTFS_SUPER_MAGIC 0x00c0ffee
47 static const struct inode_operations hostfs_iops
;
48 static const struct inode_operations hostfs_dir_iops
;
49 static const struct address_space_operations hostfs_link_aops
;
52 static int __init
hostfs_args(char *options
, int *add
)
56 ptr
= strchr(options
, ',');
64 ptr
= strchr(options
, ',');
67 if (*options
!= '\0') {
68 if (!strcmp(options
, "append"))
70 else printf("hostfs_args - unsupported option - %s\n",
78 __uml_setup("hostfs=", hostfs_args
,
79 "hostfs=<root dir>,<flags>,...\n"
80 " This is used to set hostfs parameters. The root directory argument\n"
81 " is used to confine all hostfs mounts to within the specified directory\n"
82 " tree on the host. If this isn't specified, then a user inside UML can\n"
83 " mount anything on the host that's accessible to the user that's running\n"
85 " The only flag currently supported is 'append', which specifies that all\n"
86 " files opened by hostfs will be opened in append mode.\n\n"
90 static char *dentry_name(struct dentry
*dentry
, int extra
)
92 struct dentry
*parent
;
98 while (parent
->d_parent
!= parent
) {
99 len
+= parent
->d_name
.len
+ 1;
100 parent
= parent
->d_parent
;
103 root
= HOSTFS_I(parent
->d_inode
)->host_filename
;
105 name
= kmalloc(len
+ extra
+ 1, GFP_KERNEL
);
111 while (parent
->d_parent
!= parent
) {
112 len
-= parent
->d_name
.len
+ 1;
114 strncpy(&name
[len
+ 1], parent
->d_name
.name
,
116 parent
= parent
->d_parent
;
118 strncpy(name
, root
, strlen(root
));
122 static char *inode_name(struct inode
*ino
, int extra
)
124 struct dentry
*dentry
;
126 dentry
= list_entry(ino
->i_dentry
.next
, struct dentry
, d_alias
);
127 return dentry_name(dentry
, extra
);
130 static int read_name(struct inode
*ino
, char *name
)
133 * The non-int inode fields are copied into ints by stat_file and
134 * then copied into the inode because passing the actual pointers
135 * in and having them treated as int * breaks on big-endian machines
138 int i_mode
, i_nlink
, i_blksize
;
139 unsigned long long i_size
;
140 unsigned long long i_ino
;
141 unsigned long long i_blocks
;
143 err
= stat_file(name
, &i_ino
, &i_mode
, &i_nlink
, &ino
->i_uid
,
144 &ino
->i_gid
, &i_size
, &ino
->i_atime
, &ino
->i_mtime
,
145 &ino
->i_ctime
, &i_blksize
, &i_blocks
, -1);
150 ino
->i_mode
= i_mode
;
151 ino
->i_nlink
= i_nlink
;
152 ino
->i_size
= i_size
;
153 ino
->i_blocks
= i_blocks
;
157 static char *follow_link(char *link
)
160 char *name
, *resolved
, *end
;
165 name
= kmalloc(len
, GFP_KERNEL
);
169 n
= do_readlink(link
, name
, len
);
181 end
= strrchr(link
, '/');
186 len
= strlen(link
) + strlen(name
) + 1;
188 resolved
= kmalloc(len
, GFP_KERNEL
);
189 if (resolved
== NULL
) {
194 sprintf(resolved
, "%s%s", link
, name
);
205 static int read_inode(struct inode
*ino
)
211 * Unfortunately, we are called from iget() when we don't have a dentry
214 if (list_empty(&ino
->i_dentry
))
218 name
= inode_name(ino
, 0);
222 if (file_type(name
, NULL
, NULL
) == OS_TYPE_SYMLINK
) {
223 name
= follow_link(name
);
230 err
= read_name(ino
, name
);
236 int hostfs_statfs(struct dentry
*dentry
, struct kstatfs
*sf
)
239 * do_statfs uses struct statfs64 internally, but the linux kernel
240 * struct statfs still has 32-bit versions for most of these fields,
241 * so we convert them here
250 err
= do_statfs(HOSTFS_I(dentry
->d_sb
->s_root
->d_inode
)->host_filename
,
251 &sf
->f_bsize
, &f_blocks
, &f_bfree
, &f_bavail
, &f_files
,
252 &f_ffree
, &sf
->f_fsid
, sizeof(sf
->f_fsid
),
253 &sf
->f_namelen
, sf
->f_spare
);
256 sf
->f_blocks
= f_blocks
;
257 sf
->f_bfree
= f_bfree
;
258 sf
->f_bavail
= f_bavail
;
259 sf
->f_files
= f_files
;
260 sf
->f_ffree
= f_ffree
;
261 sf
->f_type
= HOSTFS_SUPER_MAGIC
;
265 static struct inode
*hostfs_alloc_inode(struct super_block
*sb
)
267 struct hostfs_inode_info
*hi
;
269 hi
= kmalloc(sizeof(*hi
), GFP_KERNEL
);
273 *hi
= ((struct hostfs_inode_info
) { .host_filename
= NULL
,
276 inode_init_once(&hi
->vfs_inode
);
277 return &hi
->vfs_inode
;
280 static void hostfs_delete_inode(struct inode
*inode
)
282 truncate_inode_pages(&inode
->i_data
, 0);
283 if (HOSTFS_I(inode
)->fd
!= -1) {
284 close_file(&HOSTFS_I(inode
)->fd
);
285 HOSTFS_I(inode
)->fd
= -1;
290 static void hostfs_destroy_inode(struct inode
*inode
)
292 kfree(HOSTFS_I(inode
)->host_filename
);
295 * XXX: This should not happen, probably. The check is here for
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);
330 dir
= open_dir(name
, &error
);
335 while ((name
= read_dir(dir
, &next
, &ino
, &len
)) != NULL
) {
336 error
= (*filldir
)(ent
, name
, len
, file
->f_pos
,
345 int hostfs_file_open(struct inode
*ino
, struct file
*file
)
348 int mode
= 0, r
= 0, w
= 0, fd
;
350 mode
= file
->f_mode
& (FMODE_READ
| FMODE_WRITE
);
351 if ((mode
& HOSTFS_I(ino
)->mode
) == mode
)
355 * The file may already have been opened, but with the wrong access,
356 * so this resets things and reopens the file with the new access.
358 if (HOSTFS_I(ino
)->fd
!= -1) {
359 close_file(&HOSTFS_I(ino
)->fd
);
360 HOSTFS_I(ino
)->fd
= -1;
363 HOSTFS_I(ino
)->mode
|= mode
;
364 if (HOSTFS_I(ino
)->mode
& FMODE_READ
)
366 if (HOSTFS_I(ino
)->mode
& FMODE_WRITE
)
371 name
= dentry_name(file
->f_path
.dentry
, 0);
375 fd
= open_file(name
, r
, w
, append
);
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 const struct file_operations hostfs_file_fops
= {
390 .llseek
= generic_file_llseek
,
391 .read
= do_sync_read
,
392 .splice_read
= generic_file_splice_read
,
393 .aio_read
= generic_file_aio_read
,
394 .aio_write
= generic_file_aio_write
,
395 .write
= do_sync_write
,
396 .mmap
= generic_file_mmap
,
397 .open
= hostfs_file_open
,
399 .fsync
= hostfs_fsync
,
402 static const struct file_operations hostfs_dir_fops
= {
403 .llseek
= generic_file_llseek
,
404 .readdir
= hostfs_readdir
,
405 .read
= generic_read_dir
,
408 int hostfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
410 struct address_space
*mapping
= page
->mapping
;
411 struct inode
*inode
= mapping
->host
;
413 unsigned long long base
;
414 int count
= PAGE_CACHE_SIZE
;
415 int end_index
= inode
->i_size
>> PAGE_CACHE_SHIFT
;
418 if (page
->index
>= end_index
)
419 count
= inode
->i_size
& (PAGE_CACHE_SIZE
-1);
422 base
= ((unsigned long long) page
->index
) << PAGE_CACHE_SHIFT
;
424 err
= write_file(HOSTFS_I(inode
)->fd
, &base
, buffer
, count
);
426 ClearPageUptodate(page
);
430 if (base
> inode
->i_size
)
431 inode
->i_size
= base
;
434 ClearPageError(page
);
444 int hostfs_readpage(struct file
*file
, struct page
*page
)
450 start
= (long long) page
->index
<< PAGE_CACHE_SHIFT
;
452 err
= read_file(FILE_HOSTFS_I(file
)->fd
, &start
, buffer
,
457 memset(&buffer
[err
], 0, PAGE_CACHE_SIZE
- err
);
459 flush_dcache_page(page
);
460 SetPageUptodate(page
);
461 if (PageError(page
)) ClearPageError(page
);
469 int hostfs_write_begin(struct file
*file
, struct address_space
*mapping
,
470 loff_t pos
, unsigned len
, unsigned flags
,
471 struct page
**pagep
, void **fsdata
)
473 pgoff_t index
= pos
>> PAGE_CACHE_SHIFT
;
475 *pagep
= __grab_cache_page(mapping
, index
);
481 int hostfs_write_end(struct file
*file
, struct address_space
*mapping
,
482 loff_t pos
, unsigned len
, unsigned copied
,
483 struct page
*page
, void *fsdata
)
485 struct inode
*inode
= mapping
->host
;
487 unsigned from
= pos
& (PAGE_CACHE_SIZE
- 1);
491 err
= write_file(FILE_HOSTFS_I(file
)->fd
, &pos
, buffer
+ from
, copied
);
494 if (!PageUptodate(page
) && err
== PAGE_CACHE_SIZE
)
495 SetPageUptodate(page
);
498 * If err > 0, write_file has added err to pos, so we are comparing
499 * i_size against the last byte written.
501 if (err
> 0 && (pos
> inode
->i_size
))
504 page_cache_release(page
);
509 static const struct address_space_operations hostfs_aops
= {
510 .writepage
= hostfs_writepage
,
511 .readpage
= hostfs_readpage
,
512 .set_page_dirty
= __set_page_dirty_nobuffers
,
513 .write_begin
= hostfs_write_begin
,
514 .write_end
= hostfs_write_end
,
517 static int init_inode(struct inode
*inode
, struct dentry
*dentry
)
520 int type
, err
= -ENOMEM
;
525 name
= dentry_name(dentry
, 0);
528 type
= file_type(name
, &maj
, &min
);
529 /* Reencode maj and min with the kernel encoding.*/
530 rdev
= MKDEV(maj
, min
);
533 else type
= OS_TYPE_DIR
;
536 if (type
== OS_TYPE_SYMLINK
)
537 inode
->i_op
= &page_symlink_inode_operations
;
538 else if (type
== OS_TYPE_DIR
)
539 inode
->i_op
= &hostfs_dir_iops
;
540 else inode
->i_op
= &hostfs_iops
;
542 if (type
== OS_TYPE_DIR
) inode
->i_fop
= &hostfs_dir_fops
;
543 else inode
->i_fop
= &hostfs_file_fops
;
545 if (type
== OS_TYPE_SYMLINK
)
546 inode
->i_mapping
->a_ops
= &hostfs_link_aops
;
547 else inode
->i_mapping
->a_ops
= &hostfs_aops
;
550 case OS_TYPE_CHARDEV
:
551 init_special_inode(inode
, S_IFCHR
, rdev
);
553 case OS_TYPE_BLOCKDEV
:
554 init_special_inode(inode
, S_IFBLK
, rdev
);
557 init_special_inode(inode
, S_IFIFO
, 0);
560 init_special_inode(inode
, S_IFSOCK
, 0);
567 int hostfs_create(struct inode
*dir
, struct dentry
*dentry
, int mode
,
568 struct nameidata
*nd
)
575 inode
= iget(dir
->i_sb
, 0);
579 error
= init_inode(inode
, dentry
);
584 name
= dentry_name(dentry
, 0);
588 fd
= file_create(name
,
589 mode
& S_IRUSR
, mode
& S_IWUSR
, mode
& S_IXUSR
,
590 mode
& S_IRGRP
, mode
& S_IWGRP
, mode
& S_IXGRP
,
591 mode
& S_IROTH
, mode
& S_IWOTH
, mode
& S_IXOTH
);
594 else error
= read_name(inode
, name
);
600 HOSTFS_I(inode
)->fd
= fd
;
601 HOSTFS_I(inode
)->mode
= FMODE_READ
| FMODE_WRITE
;
602 d_instantiate(dentry
, inode
);
611 struct dentry
*hostfs_lookup(struct inode
*ino
, struct dentry
*dentry
,
612 struct nameidata
*nd
)
619 inode
= iget(ino
->i_sb
, 0);
623 err
= init_inode(inode
, dentry
);
628 name
= dentry_name(dentry
, 0);
632 err
= read_name(inode
, name
);
634 if (err
== -ENOENT
) {
641 d_add(dentry
, inode
);
642 dentry
->d_op
= &hostfs_dentry_ops
;
651 static char *inode_dentry_name(struct inode
*ino
, struct dentry
*dentry
)
656 file
= inode_name(ino
, dentry
->d_name
.len
+ 1);
661 strncat(file
, dentry
->d_name
.name
, dentry
->d_name
.len
);
662 file
[len
+ dentry
->d_name
.len
] = '\0';
666 int hostfs_link(struct dentry
*to
, struct inode
*ino
, struct dentry
*from
)
668 char *from_name
, *to_name
;
671 if ((from_name
= inode_dentry_name(ino
, from
)) == NULL
)
673 to_name
= dentry_name(to
, 0);
674 if (to_name
== NULL
) {
678 err
= link_file(to_name
, from_name
);
684 int hostfs_unlink(struct inode
*ino
, struct dentry
*dentry
)
689 if ((file
= inode_dentry_name(ino
, dentry
)) == NULL
)
694 err
= unlink_file(file
);
699 int hostfs_symlink(struct inode
*ino
, struct dentry
*dentry
, const char *to
)
704 if ((file
= inode_dentry_name(ino
, dentry
)) == NULL
)
706 err
= make_symlink(file
, to
);
711 int hostfs_mkdir(struct inode
*ino
, struct dentry
*dentry
, int mode
)
716 if ((file
= inode_dentry_name(ino
, dentry
)) == NULL
)
718 err
= do_mkdir(file
, mode
);
723 int hostfs_rmdir(struct inode
*ino
, struct dentry
*dentry
)
728 if ((file
= inode_dentry_name(ino
, dentry
)) == NULL
)
730 err
= do_rmdir(file
);
735 int hostfs_mknod(struct inode
*dir
, struct dentry
*dentry
, int mode
, dev_t dev
)
741 inode
= iget(dir
->i_sb
, 0);
745 err
= init_inode(inode
, dentry
);
750 name
= dentry_name(dentry
, 0);
754 init_special_inode(inode
, mode
, dev
);
755 err
= do_mknod(name
, mode
, MAJOR(dev
), MINOR(dev
));
759 err
= read_name(inode
, name
);
764 d_instantiate(dentry
, inode
);
775 int hostfs_rename(struct inode
*from_ino
, struct dentry
*from
,
776 struct inode
*to_ino
, struct dentry
*to
)
778 char *from_name
, *to_name
;
781 if ((from_name
= inode_dentry_name(from_ino
, from
)) == NULL
)
783 if ((to_name
= inode_dentry_name(to_ino
, to
)) == NULL
) {
787 err
= rename_file(from_name
, to_name
);
793 int hostfs_permission(struct inode
*ino
, int desired
, struct nameidata
*nd
)
796 int r
= 0, w
= 0, x
= 0, err
;
798 if (desired
& MAY_READ
) r
= 1;
799 if (desired
& MAY_WRITE
) w
= 1;
800 if (desired
& MAY_EXEC
) x
= 1;
801 name
= inode_name(ino
, 0);
805 if (S_ISCHR(ino
->i_mode
) || S_ISBLK(ino
->i_mode
) ||
806 S_ISFIFO(ino
->i_mode
) || S_ISSOCK(ino
->i_mode
))
809 err
= access_file(name
, r
, w
, x
);
812 err
= generic_permission(ino
, desired
, NULL
);
816 int hostfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
818 struct hostfs_iattr attrs
;
822 int fd
= HOSTFS_I(dentry
->d_inode
)->fd
;
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);
869 err
= set_attr(name
, &attrs
, fd
);
874 return inode_setattr(dentry
->d_inode
, attr
);
877 static const struct inode_operations hostfs_iops
= {
878 .create
= hostfs_create
,
880 .unlink
= hostfs_unlink
,
881 .symlink
= hostfs_symlink
,
882 .mkdir
= hostfs_mkdir
,
883 .rmdir
= hostfs_rmdir
,
884 .mknod
= hostfs_mknod
,
885 .rename
= hostfs_rename
,
886 .permission
= hostfs_permission
,
887 .setattr
= hostfs_setattr
,
890 static const struct inode_operations hostfs_dir_iops
= {
891 .create
= hostfs_create
,
892 .lookup
= hostfs_lookup
,
894 .unlink
= hostfs_unlink
,
895 .symlink
= hostfs_symlink
,
896 .mkdir
= hostfs_mkdir
,
897 .rmdir
= hostfs_rmdir
,
898 .mknod
= hostfs_mknod
,
899 .rename
= hostfs_rename
,
900 .permission
= hostfs_permission
,
901 .setattr
= hostfs_setattr
,
904 int hostfs_link_readpage(struct file
*file
, struct page
*page
)
910 name
= inode_name(page
->mapping
->host
, 0);
913 err
= do_readlink(name
, buffer
, PAGE_CACHE_SIZE
);
915 if (err
== PAGE_CACHE_SIZE
)
918 flush_dcache_page(page
);
919 SetPageUptodate(page
);
920 if (PageError(page
)) ClearPageError(page
);
928 static const struct address_space_operations hostfs_link_aops
= {
929 .readpage
= hostfs_link_readpage
,
932 static int hostfs_fill_sb_common(struct super_block
*sb
, void *d
, int silent
)
934 struct inode
*root_inode
;
935 char *host_root_path
, *req_root
= d
;
938 sb
->s_blocksize
= 1024;
939 sb
->s_blocksize_bits
= 10;
940 sb
->s_magic
= HOSTFS_SUPER_MAGIC
;
941 sb
->s_op
= &hostfs_sbops
;
943 /* NULL is printed as <NULL> by sprintf: avoid that. */
944 if (req_root
== NULL
)
948 host_root_path
= kmalloc(strlen(root_ino
) + 1
949 + strlen(req_root
) + 1, GFP_KERNEL
);
950 if (host_root_path
== NULL
)
953 sprintf(host_root_path
, "%s/%s", root_ino
, req_root
);
955 root_inode
= iget(sb
, 0);
956 if (root_inode
== NULL
)
959 err
= init_inode(root_inode
, NULL
);
963 HOSTFS_I(root_inode
)->host_filename
= host_root_path
;
965 * Avoid that in the error path, iput(root_inode) frees again
966 * host_root_path through hostfs_destroy_inode!
968 host_root_path
= NULL
;
971 sb
->s_root
= d_alloc_root(root_inode
);
972 if (sb
->s_root
== NULL
)
975 err
= read_inode(root_inode
);
977 /* No iput in this case because the dput does that for us */
988 kfree(host_root_path
);
993 static int hostfs_read_sb(struct file_system_type
*type
,
994 int flags
, const char *dev_name
,
995 void *data
, struct vfsmount
*mnt
)
997 return get_sb_nodev(type
, flags
, data
, hostfs_fill_sb_common
, mnt
);
1000 static struct file_system_type hostfs_type
= {
1001 .owner
= THIS_MODULE
,
1003 .get_sb
= hostfs_read_sb
,
1004 .kill_sb
= kill_anon_super
,
1008 static int __init
init_hostfs(void)
1010 return register_filesystem(&hostfs_type
);
1013 static void __exit
exit_hostfs(void)
1015 unregister_filesystem(&hostfs_type
);
1018 module_init(init_hostfs
)
1019 module_exit(exit_hostfs
)
1020 MODULE_LICENSE("GPL");