2 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
6 #include <linux/ctype.h>
7 #include <linux/dcache.h>
8 #include <linux/file.h>
10 #include <linux/init.h>
11 #include <linux/kernel.h>
12 #include <linux/list.h>
13 #include <linux/module.h>
14 #include <linux/mount.h>
15 #include <linux/slab.h>
16 #include <linux/statfs.h>
17 #include <linux/types.h>
18 #include <linux/pid_namespace.h>
19 #include <linux/namei.h>
20 #include <asm/uaccess.h>
23 static struct inode
*get_inode(struct super_block
*, struct dentry
*);
26 struct list_head list
;
27 char contents
[PAGE_SIZE
- sizeof(struct list_head
)];
30 struct hppfs_private
{
31 struct file
*proc_file
;
34 struct hppfs_data
*contents
;
37 struct hppfs_inode_info
{
38 struct dentry
*proc_dentry
;
39 struct inode vfs_inode
;
42 static inline struct hppfs_inode_info
*HPPFS_I(struct inode
*inode
)
44 return container_of(inode
, struct hppfs_inode_info
, vfs_inode
);
47 #define HPPFS_SUPER_MAGIC 0xb00000ee
49 static const struct super_operations hppfs_sbops
;
51 static int is_pid(struct dentry
*dentry
)
53 struct super_block
*sb
;
57 if (dentry
->d_parent
!= sb
->s_root
)
60 for (i
= 0; i
< dentry
->d_name
.len
; i
++) {
61 if (!isdigit(dentry
->d_name
.name
[i
]))
67 static char *dentry_name(struct dentry
*dentry
, int extra
)
69 struct dentry
*parent
;
76 while (parent
->d_parent
!= parent
) {
78 len
+= strlen("pid") + 1;
79 else len
+= parent
->d_name
.len
+ 1;
80 parent
= parent
->d_parent
;
85 name
= kmalloc(len
+ extra
+ 1, GFP_KERNEL
);
91 while (parent
->d_parent
!= parent
) {
94 seg_len
= strlen("pid");
97 seg_name
= parent
->d_name
.name
;
98 seg_len
= parent
->d_name
.len
;
103 strncpy(&name
[len
+ 1], seg_name
, seg_len
);
104 parent
= parent
->d_parent
;
106 strncpy(name
, root
, strlen(root
));
110 static int file_removed(struct dentry
*dentry
, const char *file
)
117 extra
+= strlen(file
) + 1;
119 host_file
= dentry_name(dentry
, extra
+ strlen("/remove"));
120 if (host_file
== NULL
) {
121 printk(KERN_ERR
"file_removed : allocation failed\n");
126 strcat(host_file
, "/");
127 strcat(host_file
, file
);
129 strcat(host_file
, "/remove");
131 fd
= os_open_file(host_file
, of_read(OPENFLAGS()), 0);
140 static struct dentry
*hppfs_lookup(struct inode
*ino
, struct dentry
*dentry
,
143 struct dentry
*proc_dentry
, *parent
;
144 struct qstr
*name
= &dentry
->d_name
;
148 deleted
= file_removed(dentry
, NULL
);
150 return ERR_PTR(deleted
);
152 return ERR_PTR(-ENOENT
);
154 parent
= HPPFS_I(ino
)->proc_dentry
;
155 mutex_lock(&parent
->d_inode
->i_mutex
);
156 proc_dentry
= lookup_one_len(name
->name
, parent
, name
->len
);
157 mutex_unlock(&parent
->d_inode
->i_mutex
);
159 if (IS_ERR(proc_dentry
))
163 inode
= get_inode(ino
->i_sb
, proc_dentry
);
167 d_add(dentry
, inode
);
174 static const struct inode_operations hppfs_file_iops
= {
177 static ssize_t
read_proc(struct file
*file
, char __user
*buf
, ssize_t count
,
178 loff_t
*ppos
, int is_user
)
180 ssize_t (*read
)(struct file
*, char __user
*, size_t, loff_t
*);
183 read
= file_inode(file
)->i_fop
->read
;
188 n
= (*read
)(file
, buf
, count
, &file
->f_pos
);
198 static ssize_t
hppfs_read_file(int fd
, char __user
*buf
, ssize_t count
)
205 new_buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
206 if (new_buf
== NULL
) {
207 printk(KERN_ERR
"hppfs_read_file : kmalloc failed\n");
212 cur
= min_t(ssize_t
, count
, PAGE_SIZE
);
213 err
= os_read_file(fd
, new_buf
, cur
);
215 printk(KERN_ERR
"hppfs_read : read failed, "
216 "errno = %d\n", err
);
222 if (copy_to_user(buf
, new_buf
, err
)) {
235 static ssize_t
hppfs_read(struct file
*file
, char __user
*buf
, size_t count
,
238 struct hppfs_private
*hppfs
= file
->private_data
;
239 struct hppfs_data
*data
;
243 if (hppfs
->contents
!= NULL
) {
246 if (*ppos
>= hppfs
->len
)
249 data
= hppfs
->contents
;
251 while (off
>= sizeof(data
->contents
)) {
252 data
= list_entry(data
->list
.next
, struct hppfs_data
,
254 off
-= sizeof(data
->contents
);
257 if (off
+ count
> hppfs
->len
)
258 count
= hppfs
->len
- off
;
259 rem
= copy_to_user(buf
, &data
->contents
[off
], count
);
260 *ppos
+= count
- rem
;
263 } else if (hppfs
->host_fd
!= -1) {
264 err
= os_seek_file(hppfs
->host_fd
, *ppos
);
266 printk(KERN_ERR
"hppfs_read : seek failed, "
267 "errno = %d\n", err
);
270 err
= hppfs_read_file(hppfs
->host_fd
, buf
, count
);
272 printk(KERN_ERR
"hppfs_read: read failed: %d\n", err
);
279 else count
= read_proc(hppfs
->proc_file
, buf
, count
, ppos
, 1);
284 static ssize_t
hppfs_write(struct file
*file
, const char __user
*buf
,
285 size_t len
, loff_t
*ppos
)
287 struct hppfs_private
*data
= file
->private_data
;
288 struct file
*proc_file
= data
->proc_file
;
289 ssize_t (*write
)(struct file
*, const char __user
*, size_t, loff_t
*);
291 write
= file_inode(proc_file
)->i_fop
->write
;
292 return (*write
)(proc_file
, buf
, len
, ppos
);
295 static int open_host_sock(char *host_file
, int *filter_out
)
300 end
= &host_file
[strlen(host_file
)];
303 fd
= os_connect_socket(host_file
);
309 fd
= os_connect_socket(host_file
);
313 static void free_contents(struct hppfs_data
*head
)
315 struct hppfs_data
*data
;
316 struct list_head
*ele
, *next
;
321 list_for_each_safe(ele
, next
, &head
->list
) {
322 data
= list_entry(ele
, struct hppfs_data
, list
);
328 static struct hppfs_data
*hppfs_get_data(int fd
, int filter
,
329 struct file
*proc_file
,
330 struct file
*hppfs_file
,
333 struct hppfs_data
*data
, *new, *head
;
337 data
= kmalloc(sizeof(*data
), GFP_KERNEL
);
339 printk(KERN_ERR
"hppfs_get_data : head allocation failed\n");
343 INIT_LIST_HEAD(&data
->list
);
349 while ((n
= read_proc(proc_file
, data
->contents
,
350 sizeof(data
->contents
), NULL
, 0)) > 0)
351 os_write_file(fd
, data
->contents
, n
);
352 err
= os_shutdown_socket(fd
, 0, 1);
354 printk(KERN_ERR
"hppfs_get_data : failed to shut down "
360 n
= os_read_file(fd
, data
->contents
, sizeof(data
->contents
));
363 printk(KERN_ERR
"hppfs_get_data : read failed, "
364 "errno = %d\n", err
);
371 if (n
< sizeof(data
->contents
))
374 new = kmalloc(sizeof(*data
), GFP_KERNEL
);
376 printk(KERN_ERR
"hppfs_get_data : data allocation "
382 INIT_LIST_HEAD(&new->list
);
383 list_add(&new->list
, &data
->list
);
394 static struct hppfs_private
*hppfs_data(void)
396 struct hppfs_private
*data
;
398 data
= kmalloc(sizeof(*data
), GFP_KERNEL
);
402 *data
= ((struct hppfs_private
) { .host_fd
= -1,
404 .contents
= NULL
} );
408 static int file_mode(int fmode
)
410 if (fmode
== (FMODE_READ
| FMODE_WRITE
))
412 if (fmode
== FMODE_READ
)
414 if (fmode
== FMODE_WRITE
)
419 static int hppfs_open(struct inode
*inode
, struct file
*file
)
421 const struct cred
*cred
= file
->f_cred
;
422 struct hppfs_private
*data
;
425 int err
, fd
, type
, filter
;
432 host_file
= dentry_name(file
->f_path
.dentry
, strlen("/rw"));
433 if (host_file
== NULL
)
436 path
.mnt
= inode
->i_sb
->s_fs_info
;
437 path
.dentry
= HPPFS_I(inode
)->proc_dentry
;
439 data
->proc_file
= dentry_open(&path
, file_mode(file
->f_mode
), cred
);
440 err
= PTR_ERR(data
->proc_file
);
441 if (IS_ERR(data
->proc_file
))
444 type
= os_file_type(host_file
);
445 if (type
== OS_TYPE_FILE
) {
446 fd
= os_open_file(host_file
, of_read(OPENFLAGS()), 0);
450 printk(KERN_ERR
"hppfs_open : failed to open '%s', "
451 "errno = %d\n", host_file
, -fd
);
453 data
->contents
= NULL
;
454 } else if (type
== OS_TYPE_DIR
) {
455 fd
= open_host_sock(host_file
, &filter
);
457 data
->contents
= hppfs_get_data(fd
, filter
,
460 if (!IS_ERR(data
->contents
))
463 printk(KERN_ERR
"hppfs_open : failed to open a socket "
464 "in '%s', errno = %d\n", host_file
, -fd
);
468 file
->private_data
= data
;
474 free_contents(data
->contents
);
480 static int hppfs_dir_open(struct inode
*inode
, struct file
*file
)
482 const struct cred
*cred
= file
->f_cred
;
483 struct hppfs_private
*data
;
492 path
.mnt
= inode
->i_sb
->s_fs_info
;
493 path
.dentry
= HPPFS_I(inode
)->proc_dentry
;
494 data
->proc_file
= dentry_open(&path
, file_mode(file
->f_mode
), cred
);
495 err
= PTR_ERR(data
->proc_file
);
496 if (IS_ERR(data
->proc_file
))
499 file
->private_data
= data
;
508 static loff_t
hppfs_llseek(struct file
*file
, loff_t off
, int where
)
510 struct hppfs_private
*data
= file
->private_data
;
511 struct file
*proc_file
= data
->proc_file
;
512 loff_t (*llseek
)(struct file
*, loff_t
, int);
515 llseek
= file_inode(proc_file
)->i_fop
->llseek
;
516 if (llseek
!= NULL
) {
517 ret
= (*llseek
)(proc_file
, off
, where
);
522 return default_llseek(file
, off
, where
);
525 static int hppfs_release(struct inode
*inode
, struct file
*file
)
527 struct hppfs_private
*data
= file
->private_data
;
528 struct file
*proc_file
= data
->proc_file
;
535 static const struct file_operations hppfs_file_fops
= {
537 .llseek
= hppfs_llseek
,
539 .write
= hppfs_write
,
541 .release
= hppfs_release
,
544 struct hppfs_dirent
{
545 struct dir_context ctx
;
546 struct dir_context
*caller
;
547 struct dentry
*dentry
;
550 static int hppfs_filldir(void *d
, const char *name
, int size
,
551 loff_t offset
, u64 inode
, unsigned int type
)
553 struct hppfs_dirent
*dirent
= d
;
555 if (file_removed(dirent
->dentry
, name
))
558 dirent
->caller
->pos
= dirent
->ctx
.pos
;
559 return !dir_emit(dirent
->caller
, name
, size
, inode
, type
);
562 static int hppfs_readdir(struct file
*file
, struct dir_context
*ctx
)
564 struct hppfs_private
*data
= file
->private_data
;
565 struct file
*proc_file
= data
->proc_file
;
566 struct hppfs_dirent d
= {
567 .ctx
.actor
= hppfs_filldir
,
569 .dentry
= file
->f_path
.dentry
572 proc_file
->f_pos
= ctx
->pos
;
573 err
= iterate_dir(proc_file
, &d
.ctx
);
574 ctx
->pos
= d
.ctx
.pos
;
578 static const struct file_operations hppfs_dir_fops
= {
580 .iterate
= hppfs_readdir
,
581 .open
= hppfs_dir_open
,
582 .llseek
= default_llseek
,
583 .release
= hppfs_release
,
586 static int hppfs_statfs(struct dentry
*dentry
, struct kstatfs
*sf
)
593 sf
->f_type
= HPPFS_SUPER_MAGIC
;
597 static struct inode
*hppfs_alloc_inode(struct super_block
*sb
)
599 struct hppfs_inode_info
*hi
;
601 hi
= kmalloc(sizeof(*hi
), GFP_KERNEL
);
605 hi
->proc_dentry
= NULL
;
606 inode_init_once(&hi
->vfs_inode
);
607 return &hi
->vfs_inode
;
610 void hppfs_evict_inode(struct inode
*ino
)
613 dput(HPPFS_I(ino
)->proc_dentry
);
614 mntput(ino
->i_sb
->s_fs_info
);
617 static void hppfs_i_callback(struct rcu_head
*head
)
619 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
620 kfree(HPPFS_I(inode
));
623 static void hppfs_destroy_inode(struct inode
*inode
)
625 call_rcu(&inode
->i_rcu
, hppfs_i_callback
);
628 static const struct super_operations hppfs_sbops
= {
629 .alloc_inode
= hppfs_alloc_inode
,
630 .destroy_inode
= hppfs_destroy_inode
,
631 .evict_inode
= hppfs_evict_inode
,
632 .statfs
= hppfs_statfs
,
635 static int hppfs_readlink(struct dentry
*dentry
, char __user
*buffer
,
638 struct dentry
*proc_dentry
= HPPFS_I(dentry
->d_inode
)->proc_dentry
;
639 return proc_dentry
->d_inode
->i_op
->readlink(proc_dentry
, buffer
,
643 static void *hppfs_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
645 struct dentry
*proc_dentry
= HPPFS_I(dentry
->d_inode
)->proc_dentry
;
647 return proc_dentry
->d_inode
->i_op
->follow_link(proc_dentry
, nd
);
650 static void hppfs_put_link(struct dentry
*dentry
, struct nameidata
*nd
,
653 struct dentry
*proc_dentry
= HPPFS_I(dentry
->d_inode
)->proc_dentry
;
655 if (proc_dentry
->d_inode
->i_op
->put_link
)
656 proc_dentry
->d_inode
->i_op
->put_link(proc_dentry
, nd
, cookie
);
659 static const struct inode_operations hppfs_dir_iops
= {
660 .lookup
= hppfs_lookup
,
663 static const struct inode_operations hppfs_link_iops
= {
664 .readlink
= hppfs_readlink
,
665 .follow_link
= hppfs_follow_link
,
666 .put_link
= hppfs_put_link
,
669 static struct inode
*get_inode(struct super_block
*sb
, struct dentry
*dentry
)
671 struct inode
*proc_ino
= dentry
->d_inode
;
672 struct inode
*inode
= new_inode(sb
);
679 if (S_ISDIR(dentry
->d_inode
->i_mode
)) {
680 inode
->i_op
= &hppfs_dir_iops
;
681 inode
->i_fop
= &hppfs_dir_fops
;
682 } else if (S_ISLNK(dentry
->d_inode
->i_mode
)) {
683 inode
->i_op
= &hppfs_link_iops
;
684 inode
->i_fop
= &hppfs_file_fops
;
686 inode
->i_op
= &hppfs_file_iops
;
687 inode
->i_fop
= &hppfs_file_fops
;
690 HPPFS_I(inode
)->proc_dentry
= dentry
;
692 inode
->i_uid
= proc_ino
->i_uid
;
693 inode
->i_gid
= proc_ino
->i_gid
;
694 inode
->i_atime
= proc_ino
->i_atime
;
695 inode
->i_mtime
= proc_ino
->i_mtime
;
696 inode
->i_ctime
= proc_ino
->i_ctime
;
697 inode
->i_ino
= proc_ino
->i_ino
;
698 inode
->i_mode
= proc_ino
->i_mode
;
699 set_nlink(inode
, proc_ino
->i_nlink
);
700 inode
->i_size
= proc_ino
->i_size
;
701 inode
->i_blocks
= proc_ino
->i_blocks
;
706 static int hppfs_fill_super(struct super_block
*sb
, void *d
, int silent
)
708 struct inode
*root_inode
;
709 struct vfsmount
*proc_mnt
;
712 proc_mnt
= mntget(task_active_pid_ns(current
)->proc_mnt
);
713 if (IS_ERR(proc_mnt
))
716 sb
->s_blocksize
= 1024;
717 sb
->s_blocksize_bits
= 10;
718 sb
->s_magic
= HPPFS_SUPER_MAGIC
;
719 sb
->s_op
= &hppfs_sbops
;
720 sb
->s_fs_info
= proc_mnt
;
723 root_inode
= get_inode(sb
, dget(proc_mnt
->mnt_root
));
724 sb
->s_root
= d_make_root(root_inode
);
736 static struct dentry
*hppfs_read_super(struct file_system_type
*type
,
737 int flags
, const char *dev_name
,
740 return mount_nodev(type
, flags
, data
, hppfs_fill_super
);
743 static struct file_system_type hppfs_type
= {
744 .owner
= THIS_MODULE
,
746 .mount
= hppfs_read_super
,
747 .kill_sb
= kill_anon_super
,
750 MODULE_ALIAS_FS("hppfs");
752 static int __init
init_hppfs(void)
754 return register_filesystem(&hppfs_type
);
757 static void __exit
exit_hppfs(void)
759 unregister_filesystem(&hppfs_type
);
762 module_init(init_hppfs
)
763 module_exit(exit_hppfs
)
764 MODULE_LICENSE("GPL");