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 <asm/uaccess.h>
21 static int init_inode(struct inode
*inode
, struct dentry
*dentry
,
22 struct vfsmount
*mnt
);
25 struct list_head list
;
26 char contents
[PAGE_SIZE
- sizeof(struct list_head
)];
29 struct hppfs_private
{
30 struct file
*proc_file
;
33 struct hppfs_data
*contents
;
36 struct hppfs_inode_info
{
37 struct dentry
*proc_dentry
;
38 struct vfsmount
*proc_mnt
;
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 ((sb
->s_op
!= &hppfs_sbops
) || (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 void hppfs_read_inode(struct inode
*ino
)
142 struct inode
*proc_ino
;
144 if (HPPFS_I(ino
)->proc_dentry
== NULL
)
147 proc_ino
= HPPFS_I(ino
)->proc_dentry
->d_inode
;
148 ino
->i_uid
= proc_ino
->i_uid
;
149 ino
->i_gid
= proc_ino
->i_gid
;
150 ino
->i_atime
= proc_ino
->i_atime
;
151 ino
->i_mtime
= proc_ino
->i_mtime
;
152 ino
->i_ctime
= proc_ino
->i_ctime
;
153 ino
->i_ino
= proc_ino
->i_ino
;
154 ino
->i_mode
= proc_ino
->i_mode
;
155 ino
->i_nlink
= proc_ino
->i_nlink
;
156 ino
->i_size
= proc_ino
->i_size
;
157 ino
->i_blocks
= proc_ino
->i_blocks
;
160 static struct inode
*hppfs_iget(struct super_block
*sb
)
164 inode
= iget_locked(sb
, 0);
166 return ERR_PTR(-ENOMEM
);
167 if (inode
->i_state
& I_NEW
) {
168 hppfs_read_inode(inode
);
169 unlock_new_inode(inode
);
174 static struct dentry
*hppfs_lookup(struct inode
*ino
, struct dentry
*dentry
,
175 struct nameidata
*nd
)
177 struct dentry
*proc_dentry
, *new, *parent
;
181 deleted
= file_removed(dentry
, NULL
);
183 return ERR_PTR(deleted
);
185 return ERR_PTR(-ENOENT
);
188 parent
= HPPFS_I(ino
)->proc_dentry
;
189 mutex_lock(&parent
->d_inode
->i_mutex
);
190 proc_dentry
= d_lookup(parent
, &dentry
->d_name
);
191 if (proc_dentry
== NULL
) {
192 proc_dentry
= d_alloc(parent
, &dentry
->d_name
);
193 if (proc_dentry
== NULL
) {
194 mutex_unlock(&parent
->d_inode
->i_mutex
);
197 new = (*parent
->d_inode
->i_op
->lookup
)(parent
->d_inode
,
204 mutex_unlock(&parent
->d_inode
->i_mutex
);
206 if (IS_ERR(proc_dentry
))
209 inode
= hppfs_iget(ino
->i_sb
);
211 err
= PTR_ERR(inode
);
215 err
= init_inode(inode
, proc_dentry
, HPPFS_I(ino
)->proc_mnt
);
219 hppfs_read_inode(inode
);
221 d_add(dentry
, inode
);
232 static const struct inode_operations hppfs_file_iops
= {
235 static ssize_t
read_proc(struct file
*file
, char __user
*buf
, ssize_t count
,
236 loff_t
*ppos
, int is_user
)
238 ssize_t (*read
)(struct file
*, char __user
*, size_t, loff_t
*);
241 read
= file
->f_path
.dentry
->d_inode
->i_fop
->read
;
246 n
= (*read
)(file
, buf
, count
, &file
->f_pos
);
256 static ssize_t
hppfs_read_file(int fd
, char __user
*buf
, ssize_t count
)
263 new_buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
264 if (new_buf
== NULL
) {
265 printk(KERN_ERR
"hppfs_read_file : kmalloc failed\n");
270 cur
= min_t(ssize_t
, count
, PAGE_SIZE
);
271 err
= os_read_file(fd
, new_buf
, cur
);
273 printk(KERN_ERR
"hppfs_read : read failed, "
274 "errno = %d\n", err
);
280 if (copy_to_user(buf
, new_buf
, err
)) {
293 static ssize_t
hppfs_read(struct file
*file
, char __user
*buf
, size_t count
,
296 struct hppfs_private
*hppfs
= file
->private_data
;
297 struct hppfs_data
*data
;
301 if (hppfs
->contents
!= NULL
) {
302 if (*ppos
>= hppfs
->len
)
305 data
= hppfs
->contents
;
307 while (off
>= sizeof(data
->contents
)) {
308 data
= list_entry(data
->list
.next
, struct hppfs_data
,
310 off
-= sizeof(data
->contents
);
313 if (off
+ count
> hppfs
->len
)
314 count
= hppfs
->len
- off
;
315 copy_to_user(buf
, &data
->contents
[off
], count
);
317 } else if (hppfs
->host_fd
!= -1) {
318 err
= os_seek_file(hppfs
->host_fd
, *ppos
);
320 printk(KERN_ERR
"hppfs_read : seek failed, "
321 "errno = %d\n", err
);
324 count
= hppfs_read_file(hppfs
->host_fd
, buf
, count
);
328 else count
= read_proc(hppfs
->proc_file
, buf
, count
, ppos
, 1);
333 static ssize_t
hppfs_write(struct file
*file
, const char __user
*buf
, size_t len
,
336 struct hppfs_private
*data
= file
->private_data
;
337 struct file
*proc_file
= data
->proc_file
;
338 ssize_t (*write
)(struct file
*, const char __user
*, size_t, loff_t
*);
341 write
= proc_file
->f_path
.dentry
->d_inode
->i_fop
->write
;
343 proc_file
->f_pos
= file
->f_pos
;
344 err
= (*write
)(proc_file
, buf
, len
, &proc_file
->f_pos
);
345 file
->f_pos
= proc_file
->f_pos
;
350 static int open_host_sock(char *host_file
, int *filter_out
)
355 end
= &host_file
[strlen(host_file
)];
358 fd
= os_connect_socket(host_file
);
364 fd
= os_connect_socket(host_file
);
368 static void free_contents(struct hppfs_data
*head
)
370 struct hppfs_data
*data
;
371 struct list_head
*ele
, *next
;
376 list_for_each_safe(ele
, next
, &head
->list
) {
377 data
= list_entry(ele
, struct hppfs_data
, list
);
383 static struct hppfs_data
*hppfs_get_data(int fd
, int filter
,
384 struct file
*proc_file
,
385 struct file
*hppfs_file
,
388 struct hppfs_data
*data
, *new, *head
;
392 data
= kmalloc(sizeof(*data
), GFP_KERNEL
);
394 printk(KERN_ERR
"hppfs_get_data : head allocation failed\n");
398 INIT_LIST_HEAD(&data
->list
);
404 while ((n
= read_proc(proc_file
, data
->contents
,
405 sizeof(data
->contents
), NULL
, 0)) > 0)
406 os_write_file(fd
, data
->contents
, n
);
407 err
= os_shutdown_socket(fd
, 0, 1);
409 printk(KERN_ERR
"hppfs_get_data : failed to shut down "
415 n
= os_read_file(fd
, data
->contents
, sizeof(data
->contents
));
418 printk(KERN_ERR
"hppfs_get_data : read failed, "
419 "errno = %d\n", err
);
426 if (n
< sizeof(data
->contents
))
429 new = kmalloc(sizeof(*data
), GFP_KERNEL
);
431 printk(KERN_ERR
"hppfs_get_data : data allocation "
437 INIT_LIST_HEAD(&new->list
);
438 list_add(&new->list
, &data
->list
);
449 static struct hppfs_private
*hppfs_data(void)
451 struct hppfs_private
*data
;
453 data
= kmalloc(sizeof(*data
), GFP_KERNEL
);
457 *data
= ((struct hppfs_private
) { .host_fd
= -1,
459 .contents
= NULL
} );
463 static int file_mode(int fmode
)
465 if (fmode
== (FMODE_READ
| FMODE_WRITE
))
467 if (fmode
== FMODE_READ
)
469 if (fmode
== FMODE_WRITE
)
474 static int hppfs_open(struct inode
*inode
, struct file
*file
)
476 struct hppfs_private
*data
;
477 struct dentry
*proc_dentry
;
478 struct vfsmount
*proc_mnt
;
480 int err
, fd
, type
, filter
;
487 host_file
= dentry_name(file
->f_path
.dentry
, strlen("/rw"));
488 if (host_file
== NULL
)
491 proc_dentry
= HPPFS_I(inode
)->proc_dentry
;
492 proc_mnt
= HPPFS_I(inode
)->proc_mnt
;
494 /* XXX This isn't closed anywhere */
495 data
->proc_file
= dentry_open(dget(proc_dentry
), mntget(proc_mnt
),
496 file_mode(file
->f_mode
));
497 err
= PTR_ERR(data
->proc_file
);
498 if (IS_ERR(data
->proc_file
))
501 type
= os_file_type(host_file
);
502 if (type
== OS_TYPE_FILE
) {
503 fd
= os_open_file(host_file
, of_read(OPENFLAGS()), 0);
507 printk(KERN_ERR
"hppfs_open : failed to open '%s', "
508 "errno = %d\n", host_file
, -fd
);
510 data
->contents
= NULL
;
511 } else if (type
== OS_TYPE_DIR
) {
512 fd
= open_host_sock(host_file
, &filter
);
514 data
->contents
= hppfs_get_data(fd
, filter
,
517 if (!IS_ERR(data
->contents
))
520 printk(KERN_ERR
"hppfs_open : failed to open a socket "
521 "in '%s', errno = %d\n", host_file
, -fd
);
525 file
->private_data
= data
;
531 free_contents(data
->contents
);
537 static int hppfs_dir_open(struct inode
*inode
, struct file
*file
)
539 struct hppfs_private
*data
;
540 struct dentry
*proc_dentry
;
541 struct vfsmount
*proc_mnt
;
549 proc_dentry
= HPPFS_I(inode
)->proc_dentry
;
550 proc_mnt
= HPPFS_I(inode
)->proc_mnt
;
551 data
->proc_file
= dentry_open(dget(proc_dentry
), mntget(proc_mnt
),
552 file_mode(file
->f_mode
));
553 err
= PTR_ERR(data
->proc_file
);
554 if (IS_ERR(data
->proc_file
))
557 file
->private_data
= data
;
566 static loff_t
hppfs_llseek(struct file
*file
, loff_t off
, int where
)
568 struct hppfs_private
*data
= file
->private_data
;
569 struct file
*proc_file
= data
->proc_file
;
570 loff_t (*llseek
)(struct file
*, loff_t
, int);
573 llseek
= proc_file
->f_path
.dentry
->d_inode
->i_fop
->llseek
;
574 if (llseek
!= NULL
) {
575 ret
= (*llseek
)(proc_file
, off
, where
);
580 return default_llseek(file
, off
, where
);
583 static const struct file_operations hppfs_file_fops
= {
585 .llseek
= hppfs_llseek
,
587 .write
= hppfs_write
,
591 struct hppfs_dirent
{
594 struct dentry
*dentry
;
597 static int hppfs_filldir(void *d
, const char *name
, int size
,
598 loff_t offset
, u64 inode
, unsigned int type
)
600 struct hppfs_dirent
*dirent
= d
;
602 if (file_removed(dirent
->dentry
, name
))
605 return (*dirent
->filldir
)(dirent
->vfs_dirent
, name
, size
, offset
,
609 static int hppfs_readdir(struct file
*file
, void *ent
, filldir_t filldir
)
611 struct hppfs_private
*data
= file
->private_data
;
612 struct file
*proc_file
= data
->proc_file
;
613 int (*readdir
)(struct file
*, void *, filldir_t
);
614 struct hppfs_dirent dirent
= ((struct hppfs_dirent
)
617 .dentry
= file
->f_path
.dentry
621 readdir
= proc_file
->f_path
.dentry
->d_inode
->i_fop
->readdir
;
623 proc_file
->f_pos
= file
->f_pos
;
624 err
= (*readdir
)(proc_file
, &dirent
, hppfs_filldir
);
625 file
->f_pos
= proc_file
->f_pos
;
630 static int hppfs_fsync(struct file
*file
, struct dentry
*dentry
, int datasync
)
635 static const struct file_operations hppfs_dir_fops
= {
637 .readdir
= hppfs_readdir
,
638 .open
= hppfs_dir_open
,
639 .fsync
= hppfs_fsync
,
642 static int hppfs_statfs(struct dentry
*dentry
, struct kstatfs
*sf
)
649 sf
->f_type
= HPPFS_SUPER_MAGIC
;
653 static struct inode
*hppfs_alloc_inode(struct super_block
*sb
)
655 struct hppfs_inode_info
*hi
;
657 hi
= kmalloc(sizeof(*hi
), GFP_KERNEL
);
661 hi
->proc_dentry
= NULL
;
663 inode_init_once(&hi
->vfs_inode
);
664 return &hi
->vfs_inode
;
667 void hppfs_delete_inode(struct inode
*ino
)
672 static void hppfs_destroy_inode(struct inode
*inode
)
674 kfree(HPPFS_I(inode
));
677 static void hppfs_put_super(struct super_block
*sb
)
679 mntput(HPPFS_I(sb
->s_root
->d_inode
)->proc_mnt
);
682 static const struct super_operations hppfs_sbops
= {
683 .alloc_inode
= hppfs_alloc_inode
,
684 .destroy_inode
= hppfs_destroy_inode
,
685 .delete_inode
= hppfs_delete_inode
,
686 .statfs
= hppfs_statfs
,
687 .put_super
= hppfs_put_super
,
690 static int hppfs_readlink(struct dentry
*dentry
, char __user
*buffer
,
693 struct file
*proc_file
;
694 struct dentry
*proc_dentry
;
695 struct vfsmount
*proc_mnt
;
698 proc_dentry
= HPPFS_I(dentry
->d_inode
)->proc_dentry
;
699 proc_mnt
= HPPFS_I(dentry
->d_inode
)->proc_mnt
;
701 proc_file
= dentry_open(dget(proc_dentry
), mntget(proc_mnt
), O_RDONLY
);
702 if (IS_ERR(proc_file
))
703 return PTR_ERR(proc_file
);
705 ret
= proc_dentry
->d_inode
->i_op
->readlink(proc_dentry
, buffer
, buflen
);
712 static void* hppfs_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
714 struct file
*proc_file
;
715 struct dentry
*proc_dentry
;
716 struct vfsmount
*proc_mnt
;
719 proc_dentry
= HPPFS_I(dentry
->d_inode
)->proc_dentry
;
720 proc_mnt
= HPPFS_I(dentry
->d_inode
)->proc_mnt
;
722 proc_file
= dentry_open(dget(proc_dentry
), mntget(proc_mnt
), O_RDONLY
);
723 if (IS_ERR(proc_file
))
726 ret
= proc_dentry
->d_inode
->i_op
->follow_link(proc_dentry
, nd
);
733 static const struct inode_operations hppfs_dir_iops
= {
734 .lookup
= hppfs_lookup
,
737 static const struct inode_operations hppfs_link_iops
= {
738 .readlink
= hppfs_readlink
,
739 .follow_link
= hppfs_follow_link
,
742 static int init_inode(struct inode
*inode
, struct dentry
*dentry
,
743 struct vfsmount
*mnt
)
745 if (S_ISDIR(dentry
->d_inode
->i_mode
)) {
746 inode
->i_op
= &hppfs_dir_iops
;
747 inode
->i_fop
= &hppfs_dir_fops
;
748 } else if (S_ISLNK(dentry
->d_inode
->i_mode
)) {
749 inode
->i_op
= &hppfs_link_iops
;
750 inode
->i_fop
= &hppfs_file_fops
;
752 inode
->i_op
= &hppfs_file_iops
;
753 inode
->i_fop
= &hppfs_file_fops
;
756 HPPFS_I(inode
)->proc_dentry
= dentry
;
757 HPPFS_I(inode
)->proc_mnt
= mnt
;
762 static int hppfs_fill_super(struct super_block
*sb
, void *d
, int silent
)
764 struct inode
*root_inode
;
765 struct file_system_type
*procfs
;
766 struct vfsmount
*proc_mnt
;
770 procfs
= get_fs_type("proc");
774 proc_mnt
= vfs_kern_mount(procfs
, 0, procfs
->name
, NULL
);
775 put_filesystem(procfs
);
776 if (IS_ERR(proc_mnt
))
779 sb
->s_blocksize
= 1024;
780 sb
->s_blocksize_bits
= 10;
781 sb
->s_magic
= HPPFS_SUPER_MAGIC
;
782 sb
->s_op
= &hppfs_sbops
;
784 root_inode
= hppfs_iget(sb
);
785 if (IS_ERR(root_inode
)) {
786 err
= PTR_ERR(root_inode
);
790 err
= init_inode(root_inode
, proc_mnt
->mnt_sb
->s_root
, proc_mnt
);
795 sb
->s_root
= d_alloc_root(root_inode
);
799 hppfs_read_inode(root_inode
);
811 static int hppfs_read_super(struct file_system_type
*type
,
812 int flags
, const char *dev_name
,
813 void *data
, struct vfsmount
*mnt
)
815 return get_sb_nodev(type
, flags
, data
, hppfs_fill_super
, mnt
);
818 static struct file_system_type hppfs_type
= {
819 .owner
= THIS_MODULE
,
821 .get_sb
= hppfs_read_super
,
822 .kill_sb
= kill_anon_super
,
826 static int __init
init_hppfs(void)
828 return register_filesystem(&hppfs_type
);
831 static void __exit
exit_hppfs(void)
833 unregister_filesystem(&hppfs_type
);
836 module_init(init_hppfs
)
837 module_exit(exit_hppfs
)
838 MODULE_LICENSE("GPL");