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>
17 #include <linux/namei.h>
22 struct hostfs_inode_info
{
25 struct inode vfs_inode
;
28 static inline struct hostfs_inode_info
*HOSTFS_I(struct inode
*inode
)
30 return list_entry(inode
, struct hostfs_inode_info
, vfs_inode
);
33 #define FILE_HOSTFS_I(file) HOSTFS_I((file)->f_path.dentry->d_inode)
35 static int hostfs_d_delete(struct dentry
*dentry
)
40 static const struct dentry_operations hostfs_dentry_ops
= {
41 .d_delete
= hostfs_d_delete
,
44 /* Changed in hostfs_args before the kernel starts running */
45 static char *root_ino
= "";
46 static int append
= 0;
48 #define HOSTFS_SUPER_MAGIC 0x00c0ffee
50 static const struct inode_operations hostfs_iops
;
51 static const struct inode_operations hostfs_dir_iops
;
52 static const struct inode_operations hostfs_link_iops
;
55 static int __init
hostfs_args(char *options
, int *add
)
59 ptr
= strchr(options
, ',');
67 ptr
= strchr(options
, ',');
70 if (*options
!= '\0') {
71 if (!strcmp(options
, "append"))
73 else printf("hostfs_args - unsupported option - %s\n",
81 __uml_setup("hostfs=", hostfs_args
,
82 "hostfs=<root dir>,<flags>,...\n"
83 " This is used to set hostfs parameters. The root directory argument\n"
84 " is used to confine all hostfs mounts to within the specified directory\n"
85 " tree on the host. If this isn't specified, then a user inside UML can\n"
86 " mount anything on the host that's accessible to the user that's running\n"
88 " The only flag currently supported is 'append', which specifies that all\n"
89 " files opened by hostfs will be opened in append mode.\n\n"
93 static char *__dentry_name(struct dentry
*dentry
, char *name
)
95 char *p
= __dentry_path(dentry
, name
, PATH_MAX
);
99 spin_unlock(&dcache_lock
);
101 root
= dentry
->d_sb
->s_fs_info
;
107 strncpy(name
, root
, PATH_MAX
);
108 if (len
> p
- name
) {
112 if (p
> name
+ len
) {
113 char *s
= name
+ len
;
114 while ((*s
++ = *p
++) != '\0')
120 static char *dentry_name(struct dentry
*dentry
)
122 char *name
= __getname();
126 spin_lock(&dcache_lock
);
127 return __dentry_name(dentry
, name
); /* will unlock */
130 static char *inode_name(struct inode
*ino
)
132 struct dentry
*dentry
;
133 char *name
= __getname();
137 spin_lock(&dcache_lock
);
138 if (list_empty(&ino
->i_dentry
)) {
139 spin_unlock(&dcache_lock
);
143 dentry
= list_first_entry(&ino
->i_dentry
, struct dentry
, d_alias
);
144 return __dentry_name(dentry
, name
); /* will unlock */
147 static char *follow_link(char *link
)
150 char *name
, *resolved
, *end
;
155 name
= kmalloc(len
, GFP_KERNEL
);
159 n
= hostfs_do_readlink(link
, name
, len
);
171 end
= strrchr(link
, '/');
176 len
= strlen(link
) + strlen(name
) + 1;
178 resolved
= kmalloc(len
, GFP_KERNEL
);
179 if (resolved
== NULL
) {
184 sprintf(resolved
, "%s%s", link
, name
);
195 static struct inode
*hostfs_iget(struct super_block
*sb
)
197 struct inode
*inode
= new_inode(sb
);
199 return ERR_PTR(-ENOMEM
);
203 int hostfs_statfs(struct dentry
*dentry
, struct kstatfs
*sf
)
206 * do_statfs uses struct statfs64 internally, but the linux kernel
207 * struct statfs still has 32-bit versions for most of these fields,
208 * so we convert them here
217 err
= do_statfs(dentry
->d_sb
->s_fs_info
,
218 &sf
->f_bsize
, &f_blocks
, &f_bfree
, &f_bavail
, &f_files
,
219 &f_ffree
, &sf
->f_fsid
, sizeof(sf
->f_fsid
),
220 &sf
->f_namelen
, sf
->f_spare
);
223 sf
->f_blocks
= f_blocks
;
224 sf
->f_bfree
= f_bfree
;
225 sf
->f_bavail
= f_bavail
;
226 sf
->f_files
= f_files
;
227 sf
->f_ffree
= f_ffree
;
228 sf
->f_type
= HOSTFS_SUPER_MAGIC
;
232 static struct inode
*hostfs_alloc_inode(struct super_block
*sb
)
234 struct hostfs_inode_info
*hi
;
236 hi
= kzalloc(sizeof(*hi
), GFP_KERNEL
);
240 inode_init_once(&hi
->vfs_inode
);
241 return &hi
->vfs_inode
;
244 static void hostfs_evict_inode(struct inode
*inode
)
246 truncate_inode_pages(&inode
->i_data
, 0);
247 end_writeback(inode
);
248 if (HOSTFS_I(inode
)->fd
!= -1) {
249 close_file(&HOSTFS_I(inode
)->fd
);
250 HOSTFS_I(inode
)->fd
= -1;
254 static void hostfs_destroy_inode(struct inode
*inode
)
256 kfree(HOSTFS_I(inode
));
259 static int hostfs_show_options(struct seq_file
*seq
, struct vfsmount
*vfs
)
261 const char *root_path
= vfs
->mnt_sb
->s_fs_info
;
262 size_t offset
= strlen(root_ino
) + 1;
264 if (strlen(root_path
) > offset
)
265 seq_printf(seq
, ",%s", root_path
+ offset
);
270 static const struct super_operations hostfs_sbops
= {
271 .alloc_inode
= hostfs_alloc_inode
,
272 .destroy_inode
= hostfs_destroy_inode
,
273 .evict_inode
= hostfs_evict_inode
,
274 .statfs
= hostfs_statfs
,
275 .show_options
= hostfs_show_options
,
278 int hostfs_readdir(struct file
*file
, void *ent
, filldir_t filldir
)
282 unsigned long long next
, ino
;
285 name
= dentry_name(file
->f_path
.dentry
);
288 dir
= open_dir(name
, &error
);
293 while ((name
= read_dir(dir
, &next
, &ino
, &len
)) != NULL
) {
294 error
= (*filldir
)(ent
, name
, len
, file
->f_pos
,
303 int hostfs_file_open(struct inode
*ino
, struct file
*file
)
307 int r
= 0, w
= 0, fd
;
309 mode
= file
->f_mode
& (FMODE_READ
| FMODE_WRITE
);
310 if ((mode
& HOSTFS_I(ino
)->mode
) == mode
)
314 * The file may already have been opened, but with the wrong access,
315 * so this resets things and reopens the file with the new access.
317 if (HOSTFS_I(ino
)->fd
!= -1) {
318 close_file(&HOSTFS_I(ino
)->fd
);
319 HOSTFS_I(ino
)->fd
= -1;
322 HOSTFS_I(ino
)->mode
|= mode
;
323 if (HOSTFS_I(ino
)->mode
& FMODE_READ
)
325 if (HOSTFS_I(ino
)->mode
& FMODE_WRITE
)
330 name
= dentry_name(file
->f_path
.dentry
);
334 fd
= open_file(name
, r
, w
, append
);
338 FILE_HOSTFS_I(file
)->fd
= fd
;
343 int hostfs_fsync(struct file
*file
, int datasync
)
345 return fsync_file(HOSTFS_I(file
->f_mapping
->host
)->fd
, datasync
);
348 static const struct file_operations hostfs_file_fops
= {
349 .llseek
= generic_file_llseek
,
350 .read
= do_sync_read
,
351 .splice_read
= generic_file_splice_read
,
352 .aio_read
= generic_file_aio_read
,
353 .aio_write
= generic_file_aio_write
,
354 .write
= do_sync_write
,
355 .mmap
= generic_file_mmap
,
356 .open
= hostfs_file_open
,
358 .fsync
= hostfs_fsync
,
361 static const struct file_operations hostfs_dir_fops
= {
362 .llseek
= generic_file_llseek
,
363 .readdir
= hostfs_readdir
,
364 .read
= generic_read_dir
,
367 int hostfs_writepage(struct page
*page
, struct writeback_control
*wbc
)
369 struct address_space
*mapping
= page
->mapping
;
370 struct inode
*inode
= mapping
->host
;
372 unsigned long long base
;
373 int count
= PAGE_CACHE_SIZE
;
374 int end_index
= inode
->i_size
>> PAGE_CACHE_SHIFT
;
377 if (page
->index
>= end_index
)
378 count
= inode
->i_size
& (PAGE_CACHE_SIZE
-1);
381 base
= ((unsigned long long) page
->index
) << PAGE_CACHE_SHIFT
;
383 err
= write_file(HOSTFS_I(inode
)->fd
, &base
, buffer
, count
);
385 ClearPageUptodate(page
);
389 if (base
> inode
->i_size
)
390 inode
->i_size
= base
;
393 ClearPageError(page
);
403 int hostfs_readpage(struct file
*file
, struct page
*page
)
409 start
= (long long) page
->index
<< PAGE_CACHE_SHIFT
;
411 err
= read_file(FILE_HOSTFS_I(file
)->fd
, &start
, buffer
,
416 memset(&buffer
[err
], 0, PAGE_CACHE_SIZE
- err
);
418 flush_dcache_page(page
);
419 SetPageUptodate(page
);
420 if (PageError(page
)) ClearPageError(page
);
428 int hostfs_write_begin(struct file
*file
, struct address_space
*mapping
,
429 loff_t pos
, unsigned len
, unsigned flags
,
430 struct page
**pagep
, void **fsdata
)
432 pgoff_t index
= pos
>> PAGE_CACHE_SHIFT
;
434 *pagep
= grab_cache_page_write_begin(mapping
, index
, flags
);
440 int hostfs_write_end(struct file
*file
, struct address_space
*mapping
,
441 loff_t pos
, unsigned len
, unsigned copied
,
442 struct page
*page
, void *fsdata
)
444 struct inode
*inode
= mapping
->host
;
446 unsigned from
= pos
& (PAGE_CACHE_SIZE
- 1);
450 err
= write_file(FILE_HOSTFS_I(file
)->fd
, &pos
, buffer
+ from
, copied
);
453 if (!PageUptodate(page
) && err
== PAGE_CACHE_SIZE
)
454 SetPageUptodate(page
);
457 * If err > 0, write_file has added err to pos, so we are comparing
458 * i_size against the last byte written.
460 if (err
> 0 && (pos
> inode
->i_size
))
463 page_cache_release(page
);
468 static const struct address_space_operations hostfs_aops
= {
469 .writepage
= hostfs_writepage
,
470 .readpage
= hostfs_readpage
,
471 .set_page_dirty
= __set_page_dirty_nobuffers
,
472 .write_begin
= hostfs_write_begin
,
473 .write_end
= hostfs_write_end
,
476 static int read_name(struct inode
*ino
, char *name
)
479 struct hostfs_stat st
;
480 int err
= stat_file(name
, &st
, -1);
484 /* Reencode maj and min with the kernel encoding.*/
485 rdev
= MKDEV(st
.maj
, st
.min
);
487 switch (st
.mode
& S_IFMT
) {
489 ino
->i_op
= &hostfs_link_iops
;
492 ino
->i_op
= &hostfs_dir_iops
;
493 ino
->i_fop
= &hostfs_dir_fops
;
499 init_special_inode(ino
, st
.mode
& S_IFMT
, rdev
);
500 ino
->i_op
= &hostfs_iops
;
504 ino
->i_op
= &hostfs_iops
;
505 ino
->i_fop
= &hostfs_file_fops
;
506 ino
->i_mapping
->a_ops
= &hostfs_aops
;
510 ino
->i_mode
= st
.mode
;
511 ino
->i_nlink
= st
.nlink
;
514 ino
->i_atime
= st
.atime
;
515 ino
->i_mtime
= st
.mtime
;
516 ino
->i_ctime
= st
.ctime
;
517 ino
->i_size
= st
.size
;
518 ino
->i_blocks
= st
.blocks
;
522 int hostfs_create(struct inode
*dir
, struct dentry
*dentry
, int mode
,
523 struct nameidata
*nd
)
529 inode
= hostfs_iget(dir
->i_sb
);
531 error
= PTR_ERR(inode
);
536 name
= dentry_name(dentry
);
540 fd
= file_create(name
,
541 mode
& S_IRUSR
, mode
& S_IWUSR
, mode
& S_IXUSR
,
542 mode
& S_IRGRP
, mode
& S_IWGRP
, mode
& S_IXGRP
,
543 mode
& S_IROTH
, mode
& S_IWOTH
, mode
& S_IXOTH
);
547 error
= read_name(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
);
582 err
= read_name(inode
, name
);
585 if (err
== -ENOENT
) {
592 d_add(dentry
, inode
);
593 dentry
->d_op
= &hostfs_dentry_ops
;
602 int hostfs_link(struct dentry
*to
, struct inode
*ino
, struct dentry
*from
)
604 char *from_name
, *to_name
;
607 if ((from_name
= dentry_name(from
)) == NULL
)
609 to_name
= dentry_name(to
);
610 if (to_name
== NULL
) {
611 __putname(from_name
);
614 err
= link_file(to_name
, from_name
);
615 __putname(from_name
);
620 int hostfs_unlink(struct inode
*ino
, struct dentry
*dentry
)
625 if ((file
= dentry_name(dentry
)) == NULL
)
630 err
= unlink_file(file
);
635 int hostfs_symlink(struct inode
*ino
, struct dentry
*dentry
, const char *to
)
640 if ((file
= dentry_name(dentry
)) == NULL
)
642 err
= make_symlink(file
, to
);
647 int hostfs_mkdir(struct inode
*ino
, struct dentry
*dentry
, int mode
)
652 if ((file
= dentry_name(dentry
)) == NULL
)
654 err
= do_mkdir(file
, mode
);
659 int hostfs_rmdir(struct inode
*ino
, struct dentry
*dentry
)
664 if ((file
= dentry_name(dentry
)) == NULL
)
666 err
= do_rmdir(file
);
671 int hostfs_mknod(struct inode
*dir
, struct dentry
*dentry
, int mode
, dev_t dev
)
677 inode
= hostfs_iget(dir
->i_sb
);
679 err
= PTR_ERR(inode
);
684 name
= dentry_name(dentry
);
688 init_special_inode(inode
, mode
, dev
);
689 err
= do_mknod(name
, mode
, MAJOR(dev
), MINOR(dev
));
693 err
= read_name(inode
, name
);
700 d_instantiate(dentry
, inode
);
711 int hostfs_rename(struct inode
*from_ino
, struct dentry
*from
,
712 struct inode
*to_ino
, struct dentry
*to
)
714 char *from_name
, *to_name
;
717 if ((from_name
= dentry_name(from
)) == NULL
)
719 if ((to_name
= dentry_name(to
)) == NULL
) {
720 __putname(from_name
);
723 err
= rename_file(from_name
, to_name
);
724 __putname(from_name
);
729 int hostfs_permission(struct inode
*ino
, int desired
)
732 int r
= 0, w
= 0, x
= 0, err
;
734 if (desired
& MAY_READ
) r
= 1;
735 if (desired
& MAY_WRITE
) w
= 1;
736 if (desired
& MAY_EXEC
) x
= 1;
737 name
= inode_name(ino
);
741 if (S_ISCHR(ino
->i_mode
) || S_ISBLK(ino
->i_mode
) ||
742 S_ISFIFO(ino
->i_mode
) || S_ISSOCK(ino
->i_mode
))
745 err
= access_file(name
, r
, w
, x
);
748 err
= generic_permission(ino
, desired
, NULL
);
752 int hostfs_setattr(struct dentry
*dentry
, struct iattr
*attr
)
754 struct inode
*inode
= dentry
->d_inode
;
755 struct hostfs_iattr attrs
;
759 int fd
= HOSTFS_I(inode
)->fd
;
761 err
= inode_change_ok(inode
, attr
);
766 attr
->ia_valid
&= ~ATTR_SIZE
;
769 if (attr
->ia_valid
& ATTR_MODE
) {
770 attrs
.ia_valid
|= HOSTFS_ATTR_MODE
;
771 attrs
.ia_mode
= attr
->ia_mode
;
773 if (attr
->ia_valid
& ATTR_UID
) {
774 attrs
.ia_valid
|= HOSTFS_ATTR_UID
;
775 attrs
.ia_uid
= attr
->ia_uid
;
777 if (attr
->ia_valid
& ATTR_GID
) {
778 attrs
.ia_valid
|= HOSTFS_ATTR_GID
;
779 attrs
.ia_gid
= attr
->ia_gid
;
781 if (attr
->ia_valid
& ATTR_SIZE
) {
782 attrs
.ia_valid
|= HOSTFS_ATTR_SIZE
;
783 attrs
.ia_size
= attr
->ia_size
;
785 if (attr
->ia_valid
& ATTR_ATIME
) {
786 attrs
.ia_valid
|= HOSTFS_ATTR_ATIME
;
787 attrs
.ia_atime
= attr
->ia_atime
;
789 if (attr
->ia_valid
& ATTR_MTIME
) {
790 attrs
.ia_valid
|= HOSTFS_ATTR_MTIME
;
791 attrs
.ia_mtime
= attr
->ia_mtime
;
793 if (attr
->ia_valid
& ATTR_CTIME
) {
794 attrs
.ia_valid
|= HOSTFS_ATTR_CTIME
;
795 attrs
.ia_ctime
= attr
->ia_ctime
;
797 if (attr
->ia_valid
& ATTR_ATIME_SET
) {
798 attrs
.ia_valid
|= HOSTFS_ATTR_ATIME_SET
;
800 if (attr
->ia_valid
& ATTR_MTIME_SET
) {
801 attrs
.ia_valid
|= HOSTFS_ATTR_MTIME_SET
;
803 name
= dentry_name(dentry
);
806 err
= set_attr(name
, &attrs
, fd
);
811 if ((attr
->ia_valid
& ATTR_SIZE
) &&
812 attr
->ia_size
!= i_size_read(inode
)) {
815 error
= vmtruncate(inode
, attr
->ia_size
);
820 setattr_copy(inode
, attr
);
821 mark_inode_dirty(inode
);
825 static const struct inode_operations hostfs_iops
= {
826 .create
= hostfs_create
,
828 .unlink
= hostfs_unlink
,
829 .symlink
= hostfs_symlink
,
830 .mkdir
= hostfs_mkdir
,
831 .rmdir
= hostfs_rmdir
,
832 .mknod
= hostfs_mknod
,
833 .rename
= hostfs_rename
,
834 .permission
= hostfs_permission
,
835 .setattr
= hostfs_setattr
,
838 static const struct inode_operations hostfs_dir_iops
= {
839 .create
= hostfs_create
,
840 .lookup
= hostfs_lookup
,
842 .unlink
= hostfs_unlink
,
843 .symlink
= hostfs_symlink
,
844 .mkdir
= hostfs_mkdir
,
845 .rmdir
= hostfs_rmdir
,
846 .mknod
= hostfs_mknod
,
847 .rename
= hostfs_rename
,
848 .permission
= hostfs_permission
,
849 .setattr
= hostfs_setattr
,
852 static void *hostfs_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
854 char *link
= __getname();
856 char *path
= dentry_name(dentry
);
859 int err
= hostfs_do_readlink(path
, link
, PATH_MAX
);
869 link
= ERR_PTR(-ENOMEM
);
872 nd_set_link(nd
, link
);
876 static void hostfs_put_link(struct dentry
*dentry
, struct nameidata
*nd
, void *cookie
)
878 char *s
= nd_get_link(nd
);
883 static const struct inode_operations hostfs_link_iops
= {
884 .readlink
= generic_readlink
,
885 .follow_link
= hostfs_follow_link
,
886 .put_link
= hostfs_put_link
,
889 static int hostfs_fill_sb_common(struct super_block
*sb
, void *d
, int silent
)
891 struct inode
*root_inode
;
892 char *host_root_path
, *req_root
= d
;
895 sb
->s_blocksize
= 1024;
896 sb
->s_blocksize_bits
= 10;
897 sb
->s_magic
= HOSTFS_SUPER_MAGIC
;
898 sb
->s_op
= &hostfs_sbops
;
899 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
901 /* NULL is printed as <NULL> by sprintf: avoid that. */
902 if (req_root
== NULL
)
906 sb
->s_fs_info
= host_root_path
=
907 kmalloc(strlen(root_ino
) + strlen(req_root
) + 2, GFP_KERNEL
);
908 if (host_root_path
== NULL
)
911 sprintf(host_root_path
, "%s/%s", root_ino
, req_root
);
913 root_inode
= new_inode(sb
);
917 err
= read_name(root_inode
, host_root_path
);
921 if (S_ISLNK(root_inode
->i_mode
)) {
922 char *name
= follow_link(host_root_path
);
926 err
= read_name(root_inode
, name
);
933 sb
->s_root
= d_alloc_root(root_inode
);
934 if (sb
->s_root
== NULL
)
945 static int hostfs_read_sb(struct file_system_type
*type
,
946 int flags
, const char *dev_name
,
947 void *data
, struct vfsmount
*mnt
)
949 return get_sb_nodev(type
, flags
, data
, hostfs_fill_sb_common
, mnt
);
952 static void hostfs_kill_sb(struct super_block
*s
)
958 static struct file_system_type hostfs_type
= {
959 .owner
= THIS_MODULE
,
961 .get_sb
= hostfs_read_sb
,
962 .kill_sb
= hostfs_kill_sb
,
966 static int __init
init_hostfs(void)
968 return register_filesystem(&hostfs_type
);
971 static void __exit
exit_hostfs(void)
973 unregister_filesystem(&hostfs_type
);
976 module_init(init_hostfs
)
977 module_exit(exit_hostfs
)
978 MODULE_LICENSE("GPL");