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>
14 #include <linux/slab.h>
15 #include <linux/seq_file.h>
16 #include <linux/mount.h>
21 struct hostfs_inode_info
{
24 struct inode vfs_inode
;
27 static inline struct hostfs_inode_info
*HOSTFS_I(struct inode
*inode
)
29 return list_entry(inode
, struct hostfs_inode_info
, vfs_inode
);
32 #define FILE_HOSTFS_I(file) HOSTFS_I((file)->f_path.dentry->d_inode)
34 static int hostfs_d_delete(struct dentry
*dentry
)
39 static const struct dentry_operations hostfs_dentry_ops
= {
40 .d_delete
= hostfs_d_delete
,
43 /* Changed in hostfs_args before the kernel starts running */
44 static char *root_ino
= "";
45 static int append
= 0;
47 #define HOSTFS_SUPER_MAGIC 0x00c0ffee
49 static const struct inode_operations hostfs_iops
;
50 static const struct inode_operations hostfs_dir_iops
;
51 static const struct address_space_operations hostfs_link_aops
;
54 static int __init
hostfs_args(char *options
, int *add
)
58 ptr
= strchr(options
, ',');
66 ptr
= strchr(options
, ',');
69 if (*options
!= '\0') {
70 if (!strcmp(options
, "append"))
72 else printf("hostfs_args - unsupported option - %s\n",
80 __uml_setup("hostfs=", hostfs_args
,
81 "hostfs=<root dir>,<flags>,...\n"
82 " This is used to set hostfs parameters. The root directory argument\n"
83 " is used to confine all hostfs mounts to within the specified directory\n"
84 " tree on the host. If this isn't specified, then a user inside UML can\n"
85 " mount anything on the host that's accessible to the user that's running\n"
87 " The only flag currently supported is 'append', which specifies that all\n"
88 " files opened by hostfs will be opened in append mode.\n\n"
92 static char *dentry_name(struct dentry
*dentry
, int extra
)
94 struct dentry
*parent
;
100 while (parent
->d_parent
!= parent
) {
101 len
+= parent
->d_name
.len
+ 1;
102 parent
= parent
->d_parent
;
105 root
= parent
->d_sb
->s_fs_info
;
107 name
= kmalloc(len
+ extra
+ 1, GFP_KERNEL
);
113 while (parent
->d_parent
!= parent
) {
114 len
-= parent
->d_name
.len
+ 1;
116 strncpy(&name
[len
+ 1], parent
->d_name
.name
,
118 parent
= parent
->d_parent
;
120 strncpy(name
, root
, strlen(root
));
124 static char *inode_name(struct inode
*ino
, int extra
)
126 struct dentry
*dentry
;
128 dentry
= list_entry(ino
->i_dentry
.next
, struct dentry
, d_alias
);
129 return dentry_name(dentry
, extra
);
132 static int read_name(struct inode
*ino
, char *name
)
134 struct hostfs_stat st
;
135 int err
= stat_file(name
, &st
, -1);
140 ino
->i_mode
= st
.mode
;
141 ino
->i_nlink
= st
.nlink
;
144 ino
->i_atime
= st
.atime
;
145 ino
->i_mtime
= st
.mtime
;
146 ino
->i_ctime
= st
.ctime
;
147 ino
->i_size
= st
.size
;
148 ino
->i_blocks
= st
.blocks
;
152 static char *follow_link(char *link
)
155 char *name
, *resolved
, *end
;
160 name
= kmalloc(len
, GFP_KERNEL
);
164 n
= hostfs_do_readlink(link
, name
, len
);
176 end
= strrchr(link
, '/');
181 len
= strlen(link
) + strlen(name
) + 1;
183 resolved
= kmalloc(len
, GFP_KERNEL
);
184 if (resolved
== NULL
) {
189 sprintf(resolved
, "%s%s", link
, name
);
200 static struct inode
*hostfs_iget(struct super_block
*sb
)
202 struct inode
*inode
= new_inode(sb
);
204 return ERR_PTR(-ENOMEM
);
208 int hostfs_statfs(struct dentry
*dentry
, struct kstatfs
*sf
)
211 * do_statfs uses struct statfs64 internally, but the linux kernel
212 * struct statfs still has 32-bit versions for most of these fields,
213 * so we convert them here
222 err
= do_statfs(dentry
->d_sb
->s_fs_info
,
223 &sf
->f_bsize
, &f_blocks
, &f_bfree
, &f_bavail
, &f_files
,
224 &f_ffree
, &sf
->f_fsid
, sizeof(sf
->f_fsid
),
225 &sf
->f_namelen
, sf
->f_spare
);
228 sf
->f_blocks
= f_blocks
;
229 sf
->f_bfree
= f_bfree
;
230 sf
->f_bavail
= f_bavail
;
231 sf
->f_files
= f_files
;
232 sf
->f_ffree
= f_ffree
;
233 sf
->f_type
= HOSTFS_SUPER_MAGIC
;
237 static struct inode
*hostfs_alloc_inode(struct super_block
*sb
)
239 struct hostfs_inode_info
*hi
;
241 hi
= kzalloc(sizeof(*hi
), GFP_KERNEL
);
245 inode_init_once(&hi
->vfs_inode
);
246 return &hi
->vfs_inode
;
249 static void hostfs_evict_inode(struct inode
*inode
)
251 truncate_inode_pages(&inode
->i_data
, 0);
252 end_writeback(inode
);
253 if (HOSTFS_I(inode
)->fd
!= -1) {
254 close_file(&HOSTFS_I(inode
)->fd
);
255 HOSTFS_I(inode
)->fd
= -1;
259 static void hostfs_destroy_inode(struct inode
*inode
)
261 kfree(HOSTFS_I(inode
));
264 static int hostfs_show_options(struct seq_file
*seq
, struct vfsmount
*vfs
)
266 const char *root_path
= vfs
->mnt_sb
->s_fs_info
;
267 size_t offset
= strlen(root_ino
) + 1;
269 if (strlen(root_path
) > offset
)
270 seq_printf(seq
, ",%s", root_path
+ offset
);
275 static const struct super_operations hostfs_sbops
= {
276 .alloc_inode
= hostfs_alloc_inode
,
277 .destroy_inode
= hostfs_destroy_inode
,
278 .evict_inode
= hostfs_evict_inode
,
279 .statfs
= hostfs_statfs
,
280 .show_options
= hostfs_show_options
,
283 int hostfs_readdir(struct file
*file
, void *ent
, filldir_t filldir
)
287 unsigned long long next
, ino
;
290 name
= dentry_name(file
->f_path
.dentry
, 0);
293 dir
= open_dir(name
, &error
);
298 while ((name
= read_dir(dir
, &next
, &ino
, &len
)) != NULL
) {
299 error
= (*filldir
)(ent
, name
, len
, file
->f_pos
,
308 int hostfs_file_open(struct inode
*ino
, struct file
*file
)
312 int r
= 0, w
= 0, fd
;
314 mode
= file
->f_mode
& (FMODE_READ
| FMODE_WRITE
);
315 if ((mode
& HOSTFS_I(ino
)->mode
) == mode
)
319 * The file may already have been opened, but with the wrong access,
320 * so this resets things and reopens the file with the new access.
322 if (HOSTFS_I(ino
)->fd
!= -1) {
323 close_file(&HOSTFS_I(ino
)->fd
);
324 HOSTFS_I(ino
)->fd
= -1;
327 HOSTFS_I(ino
)->mode
|= mode
;
328 if (HOSTFS_I(ino
)->mode
& FMODE_READ
)
330 if (HOSTFS_I(ino
)->mode
& FMODE_WRITE
)
335 name
= dentry_name(file
->f_path
.dentry
, 0);
339 fd
= open_file(name
, r
, w
, append
);
343 FILE_HOSTFS_I(file
)->fd
= fd
;
348 int hostfs_fsync(struct file
*file
, int datasync
)
350 return fsync_file(HOSTFS_I(file
->f_mapping
->host
)->fd
, datasync
);
353 static const struct file_operations hostfs_file_fops
= {
354 .llseek
= generic_file_llseek
,
355 .read
= do_sync_read
,
356 .splice_read
= generic_file_splice_read
,
357 .aio_read
= generic_file_aio_read
,
358 .aio_write
= generic_file_aio_write
,
359 .write
= do_sync_write
,
360 .mmap
= generic_file_mmap
,
361 .open
= hostfs_file_open
,
363 .fsync
= hostfs_fsync
,
366 static const struct file_operations hostfs_dir_fops
= {
367 .llseek
= generic_file_llseek
,
368 .readdir
= hostfs_readdir
,
369 .read
= generic_read_dir
,
372 int hostfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
374 struct address_space
*mapping
= page
->mapping
;
375 struct inode
*inode
= mapping
->host
;
377 unsigned long long base
;
378 int count
= PAGE_CACHE_SIZE
;
379 int end_index
= inode
->i_size
>> PAGE_CACHE_SHIFT
;
382 if (page
->index
>= end_index
)
383 count
= inode
->i_size
& (PAGE_CACHE_SIZE
-1);
386 base
= ((unsigned long long) page
->index
) << PAGE_CACHE_SHIFT
;
388 err
= write_file(HOSTFS_I(inode
)->fd
, &base
, buffer
, count
);
390 ClearPageUptodate(page
);
394 if (base
> inode
->i_size
)
395 inode
->i_size
= base
;
398 ClearPageError(page
);
408 int hostfs_readpage(struct file
*file
, struct page
*page
)
414 start
= (long long) page
->index
<< PAGE_CACHE_SHIFT
;
416 err
= read_file(FILE_HOSTFS_I(file
)->fd
, &start
, buffer
,
421 memset(&buffer
[err
], 0, PAGE_CACHE_SIZE
- err
);
423 flush_dcache_page(page
);
424 SetPageUptodate(page
);
425 if (PageError(page
)) ClearPageError(page
);
433 int hostfs_write_begin(struct file
*file
, struct address_space
*mapping
,
434 loff_t pos
, unsigned len
, unsigned flags
,
435 struct page
**pagep
, void **fsdata
)
437 pgoff_t index
= pos
>> PAGE_CACHE_SHIFT
;
439 *pagep
= grab_cache_page_write_begin(mapping
, index
, flags
);
445 int hostfs_write_end(struct file
*file
, struct address_space
*mapping
,
446 loff_t pos
, unsigned len
, unsigned copied
,
447 struct page
*page
, void *fsdata
)
449 struct inode
*inode
= mapping
->host
;
451 unsigned from
= pos
& (PAGE_CACHE_SIZE
- 1);
455 err
= write_file(FILE_HOSTFS_I(file
)->fd
, &pos
, buffer
+ from
, copied
);
458 if (!PageUptodate(page
) && err
== PAGE_CACHE_SIZE
)
459 SetPageUptodate(page
);
462 * If err > 0, write_file has added err to pos, so we are comparing
463 * i_size against the last byte written.
465 if (err
> 0 && (pos
> inode
->i_size
))
468 page_cache_release(page
);
473 static const struct address_space_operations hostfs_aops
= {
474 .writepage
= hostfs_writepage
,
475 .readpage
= hostfs_readpage
,
476 .set_page_dirty
= __set_page_dirty_nobuffers
,
477 .write_begin
= hostfs_write_begin
,
478 .write_end
= hostfs_write_end
,
481 static void init_inode(struct inode
*inode
, char *path
)
487 type
= file_type(path
, &maj
, &min
);
488 /* Reencode maj and min with the kernel encoding.*/
489 rdev
= MKDEV(maj
, min
);
491 if (type
== OS_TYPE_SYMLINK
)
492 inode
->i_op
= &page_symlink_inode_operations
;
493 else if (type
== OS_TYPE_DIR
)
494 inode
->i_op
= &hostfs_dir_iops
;
495 else inode
->i_op
= &hostfs_iops
;
497 if (type
== OS_TYPE_DIR
) inode
->i_fop
= &hostfs_dir_fops
;
498 else inode
->i_fop
= &hostfs_file_fops
;
500 if (type
== OS_TYPE_SYMLINK
)
501 inode
->i_mapping
->a_ops
= &hostfs_link_aops
;
502 else inode
->i_mapping
->a_ops
= &hostfs_aops
;
505 case OS_TYPE_CHARDEV
:
506 init_special_inode(inode
, S_IFCHR
, rdev
);
508 case OS_TYPE_BLOCKDEV
:
509 init_special_inode(inode
, S_IFBLK
, rdev
);
512 init_special_inode(inode
, S_IFIFO
, 0);
515 init_special_inode(inode
, S_IFSOCK
, 0);
520 int hostfs_create(struct inode
*dir
, struct dentry
*dentry
, int mode
,
521 struct nameidata
*nd
)
527 inode
= hostfs_iget(dir
->i_sb
);
529 error
= PTR_ERR(inode
);
534 name
= dentry_name(dentry
, 0);
538 fd
= file_create(name
,
539 mode
& S_IRUSR
, mode
& S_IWUSR
, mode
& S_IXUSR
,
540 mode
& S_IRGRP
, mode
& S_IWGRP
, mode
& S_IXGRP
,
541 mode
& S_IROTH
, mode
& S_IWOTH
, mode
& S_IXOTH
);
545 error
= read_name(inode
, name
);
546 init_inode(inode
, name
);
553 HOSTFS_I(inode
)->fd
= fd
;
554 HOSTFS_I(inode
)->mode
= FMODE_READ
| FMODE_WRITE
;
555 d_instantiate(dentry
, inode
);
564 struct dentry
*hostfs_lookup(struct inode
*ino
, struct dentry
*dentry
,
565 struct nameidata
*nd
)
571 inode
= hostfs_iget(ino
->i_sb
);
573 err
= PTR_ERR(inode
);
578 name
= dentry_name(dentry
, 0);
582 err
= read_name(inode
, name
);
583 init_inode(inode
, name
);
586 if (err
== -ENOENT
) {
593 d_add(dentry
, inode
);
594 dentry
->d_op
= &hostfs_dentry_ops
;
603 static char *inode_dentry_name(struct inode
*ino
, struct dentry
*dentry
)
608 file
= inode_name(ino
, dentry
->d_name
.len
+ 1);
613 strncat(file
, dentry
->d_name
.name
, dentry
->d_name
.len
);
614 file
[len
+ dentry
->d_name
.len
] = '\0';
618 int hostfs_link(struct dentry
*to
, struct inode
*ino
, struct dentry
*from
)
620 char *from_name
, *to_name
;
623 if ((from_name
= inode_dentry_name(ino
, from
)) == NULL
)
625 to_name
= dentry_name(to
, 0);
626 if (to_name
== NULL
) {
630 err
= link_file(to_name
, from_name
);
636 int hostfs_unlink(struct inode
*ino
, struct dentry
*dentry
)
641 if ((file
= inode_dentry_name(ino
, dentry
)) == NULL
)
646 err
= unlink_file(file
);
651 int hostfs_symlink(struct inode
*ino
, struct dentry
*dentry
, const char *to
)
656 if ((file
= inode_dentry_name(ino
, dentry
)) == NULL
)
658 err
= make_symlink(file
, to
);
663 int hostfs_mkdir(struct inode
*ino
, struct dentry
*dentry
, int mode
)
668 if ((file
= inode_dentry_name(ino
, dentry
)) == NULL
)
670 err
= do_mkdir(file
, mode
);
675 int hostfs_rmdir(struct inode
*ino
, struct dentry
*dentry
)
680 if ((file
= inode_dentry_name(ino
, dentry
)) == NULL
)
682 err
= do_rmdir(file
);
687 int hostfs_mknod(struct inode
*dir
, struct dentry
*dentry
, int mode
, dev_t dev
)
693 inode
= hostfs_iget(dir
->i_sb
);
695 err
= PTR_ERR(inode
);
700 name
= dentry_name(dentry
, 0);
704 init_special_inode(inode
, mode
, dev
);
705 err
= do_mknod(name
, mode
, MAJOR(dev
), MINOR(dev
));
709 err
= read_name(inode
, name
);
710 init_inode(inode
, name
);
717 d_instantiate(dentry
, inode
);
728 int hostfs_rename(struct inode
*from_ino
, struct dentry
*from
,
729 struct inode
*to_ino
, struct dentry
*to
)
731 char *from_name
, *to_name
;
734 if ((from_name
= inode_dentry_name(from_ino
, from
)) == NULL
)
736 if ((to_name
= inode_dentry_name(to_ino
, to
)) == NULL
) {
740 err
= rename_file(from_name
, to_name
);
746 int hostfs_permission(struct inode
*ino
, int desired
)
749 int r
= 0, w
= 0, x
= 0, err
;
751 if (desired
& MAY_READ
) r
= 1;
752 if (desired
& MAY_WRITE
) w
= 1;
753 if (desired
& MAY_EXEC
) x
= 1;
754 name
= inode_name(ino
, 0);
758 if (S_ISCHR(ino
->i_mode
) || S_ISBLK(ino
->i_mode
) ||
759 S_ISFIFO(ino
->i_mode
) || S_ISSOCK(ino
->i_mode
))
762 err
= access_file(name
, r
, w
, x
);
765 err
= generic_permission(ino
, desired
, NULL
);
769 int hostfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
771 struct inode
*inode
= dentry
->d_inode
;
772 struct hostfs_iattr attrs
;
776 int fd
= HOSTFS_I(inode
)->fd
;
778 err
= inode_change_ok(inode
, attr
);
783 attr
->ia_valid
&= ~ATTR_SIZE
;
786 if (attr
->ia_valid
& ATTR_MODE
) {
787 attrs
.ia_valid
|= HOSTFS_ATTR_MODE
;
788 attrs
.ia_mode
= attr
->ia_mode
;
790 if (attr
->ia_valid
& ATTR_UID
) {
791 attrs
.ia_valid
|= HOSTFS_ATTR_UID
;
792 attrs
.ia_uid
= attr
->ia_uid
;
794 if (attr
->ia_valid
& ATTR_GID
) {
795 attrs
.ia_valid
|= HOSTFS_ATTR_GID
;
796 attrs
.ia_gid
= attr
->ia_gid
;
798 if (attr
->ia_valid
& ATTR_SIZE
) {
799 attrs
.ia_valid
|= HOSTFS_ATTR_SIZE
;
800 attrs
.ia_size
= attr
->ia_size
;
802 if (attr
->ia_valid
& ATTR_ATIME
) {
803 attrs
.ia_valid
|= HOSTFS_ATTR_ATIME
;
804 attrs
.ia_atime
= attr
->ia_atime
;
806 if (attr
->ia_valid
& ATTR_MTIME
) {
807 attrs
.ia_valid
|= HOSTFS_ATTR_MTIME
;
808 attrs
.ia_mtime
= attr
->ia_mtime
;
810 if (attr
->ia_valid
& ATTR_CTIME
) {
811 attrs
.ia_valid
|= HOSTFS_ATTR_CTIME
;
812 attrs
.ia_ctime
= attr
->ia_ctime
;
814 if (attr
->ia_valid
& ATTR_ATIME_SET
) {
815 attrs
.ia_valid
|= HOSTFS_ATTR_ATIME_SET
;
817 if (attr
->ia_valid
& ATTR_MTIME_SET
) {
818 attrs
.ia_valid
|= HOSTFS_ATTR_MTIME_SET
;
820 name
= dentry_name(dentry
, 0);
823 err
= set_attr(name
, &attrs
, fd
);
828 if ((attr
->ia_valid
& ATTR_SIZE
) &&
829 attr
->ia_size
!= i_size_read(inode
)) {
832 error
= vmtruncate(inode
, attr
->ia_size
);
837 setattr_copy(inode
, attr
);
838 mark_inode_dirty(inode
);
842 static const struct inode_operations hostfs_iops
= {
843 .create
= hostfs_create
,
845 .unlink
= hostfs_unlink
,
846 .symlink
= hostfs_symlink
,
847 .mkdir
= hostfs_mkdir
,
848 .rmdir
= hostfs_rmdir
,
849 .mknod
= hostfs_mknod
,
850 .rename
= hostfs_rename
,
851 .permission
= hostfs_permission
,
852 .setattr
= hostfs_setattr
,
855 static const struct inode_operations hostfs_dir_iops
= {
856 .create
= hostfs_create
,
857 .lookup
= hostfs_lookup
,
859 .unlink
= hostfs_unlink
,
860 .symlink
= hostfs_symlink
,
861 .mkdir
= hostfs_mkdir
,
862 .rmdir
= hostfs_rmdir
,
863 .mknod
= hostfs_mknod
,
864 .rename
= hostfs_rename
,
865 .permission
= hostfs_permission
,
866 .setattr
= hostfs_setattr
,
869 int hostfs_link_readpage(struct file
*file
, struct page
*page
)
875 name
= inode_name(page
->mapping
->host
, 0);
878 err
= hostfs_do_readlink(name
, buffer
, PAGE_CACHE_SIZE
);
880 if (err
== PAGE_CACHE_SIZE
)
883 flush_dcache_page(page
);
884 SetPageUptodate(page
);
885 if (PageError(page
)) ClearPageError(page
);
893 static const struct address_space_operations hostfs_link_aops
= {
894 .readpage
= hostfs_link_readpage
,
897 static int hostfs_fill_sb_common(struct super_block
*sb
, void *d
, int silent
)
899 struct inode
*root_inode
;
900 char *host_root_path
, *req_root
= d
;
903 sb
->s_blocksize
= 1024;
904 sb
->s_blocksize_bits
= 10;
905 sb
->s_magic
= HOSTFS_SUPER_MAGIC
;
906 sb
->s_op
= &hostfs_sbops
;
907 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
909 /* NULL is printed as <NULL> by sprintf: avoid that. */
910 if (req_root
== NULL
)
914 sb
->s_fs_info
= host_root_path
=
915 kmalloc(strlen(root_ino
) + strlen(req_root
) + 2, GFP_KERNEL
);
916 if (host_root_path
== NULL
)
919 sprintf(host_root_path
, "%s/%s", root_ino
, req_root
);
921 root_inode
= new_inode(sb
);
925 root_inode
->i_op
= &hostfs_dir_iops
;
926 root_inode
->i_fop
= &hostfs_dir_fops
;
928 if (file_type(host_root_path
, NULL
, NULL
) == OS_TYPE_SYMLINK
) {
929 char *name
= follow_link(host_root_path
);
933 err
= read_name(root_inode
, name
);
936 err
= read_name(root_inode
, host_root_path
);
942 sb
->s_root
= d_alloc_root(root_inode
);
943 if (sb
->s_root
== NULL
)
954 static int hostfs_read_sb(struct file_system_type
*type
,
955 int flags
, const char *dev_name
,
956 void *data
, struct vfsmount
*mnt
)
958 return get_sb_nodev(type
, flags
, data
, hostfs_fill_sb_common
, mnt
);
961 static void hostfs_kill_sb(struct super_block
*s
)
967 static struct file_system_type hostfs_type
= {
968 .owner
= THIS_MODULE
,
970 .get_sb
= hostfs_read_sb
,
971 .kill_sb
= hostfs_kill_sb
,
975 static int __init
init_hostfs(void)
977 return register_filesystem(&hostfs_type
);
980 static void __exit
exit_hostfs(void)
982 unregister_filesystem(&hostfs_type
);
985 module_init(init_hostfs
)
986 module_exit(exit_hostfs
)
987 MODULE_LICENSE("GPL");