2 * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
7 #include <linux/file.h>
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/slab.h>
11 #include <linux/list.h>
12 #include <linux/kernel.h>
13 #include <linux/ctype.h>
14 #include <linux/dcache.h>
15 #include <linux/statfs.h>
16 #include <asm/uaccess.h>
17 #include <asm/fcntl.h>
20 static int init_inode(struct inode *inode, struct dentry *dentry);
23 struct list_head list;
24 char contents[PAGE_SIZE - sizeof(struct list_head)];
27 struct hppfs_private {
28 struct file *proc_file;
31 struct hppfs_data *contents;
34 struct hppfs_inode_info {
35 struct dentry *proc_dentry;
36 struct inode vfs_inode;
39 static inline struct hppfs_inode_info *HPPFS_I(struct inode *inode)
41 return(list_entry(inode, struct hppfs_inode_info, vfs_inode));
44 #define HPPFS_SUPER_MAGIC 0xb00000ee
46 static struct super_operations hppfs_sbops;
48 static int is_pid(struct dentry *dentry)
50 struct super_block *sb;
54 if((sb->s_op != &hppfs_sbops) || (dentry->d_parent != sb->s_root))
57 for(i = 0; i < dentry->d_name.len; i++){
58 if(!isdigit(dentry->d_name.name[i]))
64 static char *dentry_name(struct dentry *dentry, int extra)
66 struct dentry *parent;
73 while(parent->d_parent != parent){
75 len += strlen("pid") + 1;
76 else len += parent->d_name.len + 1;
77 parent = parent->d_parent;
82 name = kmalloc(len + extra + 1, GFP_KERNEL);
83 if(name == NULL) return(NULL);
87 while(parent->d_parent != parent){
90 seg_len = strlen("pid");
93 seg_name = parent->d_name.name;
94 seg_len = parent->d_name.len;
99 strncpy(&name[len + 1], seg_name, seg_len);
100 parent = parent->d_parent;
102 strncpy(name, root, strlen(root));
106 struct dentry_operations hppfs_dentry_ops = {
109 static int file_removed(struct dentry *dentry, const char *file)
115 if(file != NULL) extra += strlen(file) + 1;
117 host_file = dentry_name(dentry, extra + strlen("/remove"));
118 if(host_file == NULL){
119 printk("file_removed : allocation failed\n");
124 strcat(host_file, "/");
125 strcat(host_file, file);
127 strcat(host_file, "/remove");
129 fd = os_open_file(host_file, of_read(OPENFLAGS()), 0);
138 static void hppfs_read_inode(struct inode *ino)
140 struct inode *proc_ino;
142 if(HPPFS_I(ino)->proc_dentry == NULL)
145 proc_ino = HPPFS_I(ino)->proc_dentry->d_inode;
146 ino->i_uid = proc_ino->i_uid;
147 ino->i_gid = proc_ino->i_gid;
148 ino->i_atime = proc_ino->i_atime;
149 ino->i_mtime = proc_ino->i_mtime;
150 ino->i_ctime = proc_ino->i_ctime;
151 ino->i_ino = proc_ino->i_ino;
152 ino->i_mode = proc_ino->i_mode;
153 ino->i_nlink = proc_ino->i_nlink;
154 ino->i_size = proc_ino->i_size;
155 ino->i_blksize = proc_ino->i_blksize;
156 ino->i_blocks = proc_ino->i_blocks;
159 static struct dentry *hppfs_lookup(struct inode *ino, struct dentry *dentry,
160 struct nameidata *nd)
162 struct dentry *proc_dentry, *new, *parent;
166 deleted = file_removed(dentry, NULL);
168 return(ERR_PTR(deleted));
170 return(ERR_PTR(-ENOENT));
173 parent = HPPFS_I(ino)->proc_dentry;
174 down(&parent->d_inode->i_sem);
175 proc_dentry = d_lookup(parent, &dentry->d_name);
176 if(proc_dentry == NULL){
177 proc_dentry = d_alloc(parent, &dentry->d_name);
178 if(proc_dentry == NULL){
179 up(&parent->d_inode->i_sem);
182 new = (*parent->d_inode->i_op->lookup)(parent->d_inode,
189 up(&parent->d_inode->i_sem);
191 if(IS_ERR(proc_dentry))
194 inode = iget(ino->i_sb, 0);
198 err = init_inode(inode, proc_dentry);
202 hppfs_read_inode(inode);
204 d_add(dentry, inode);
205 dentry->d_op = &hppfs_dentry_ops;
213 return(ERR_PTR(err));
216 static struct inode_operations hppfs_file_iops = {
219 static ssize_t read_proc(struct file *file, char *buf, ssize_t count,
220 loff_t *ppos, int is_user)
222 ssize_t (*read)(struct file *, char *, size_t, loff_t *);
225 read = file->f_dentry->d_inode->i_fop->read;
230 n = (*read)(file, buf, count, &file->f_pos);
235 if(ppos) *ppos = file->f_pos;
239 static ssize_t hppfs_read_file(int fd, char *buf, ssize_t count)
246 new_buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
248 printk("hppfs_read_file : kmalloc failed\n");
253 cur = min_t(ssize_t, count, PAGE_SIZE);
254 err = os_read_file(fd, new_buf, cur);
256 printk("hppfs_read : read failed, errno = %d\n",
264 if(copy_to_user(buf, new_buf, err)){
277 static ssize_t hppfs_read(struct file *file, char *buf, size_t count,
280 struct hppfs_private *hppfs = file->private_data;
281 struct hppfs_data *data;
285 if(hppfs->contents != NULL){
286 if(*ppos >= hppfs->len) return(0);
288 data = hppfs->contents;
290 while(off >= sizeof(data->contents)){
291 data = list_entry(data->list.next, struct hppfs_data,
293 off -= sizeof(data->contents);
296 if(off + count > hppfs->len)
297 count = hppfs->len - off;
298 copy_to_user(buf, &data->contents[off], count);
301 else if(hppfs->host_fd != -1){
302 err = os_seek_file(hppfs->host_fd, *ppos);
304 printk("hppfs_read : seek failed, errno = %d\n", err);
307 count = hppfs_read_file(hppfs->host_fd, buf, count);
311 else count = read_proc(hppfs->proc_file, buf, count, ppos, 1);
316 static ssize_t hppfs_write(struct file *file, const char *buf, size_t len,
319 struct hppfs_private *data = file->private_data;
320 struct file *proc_file = data->proc_file;
321 ssize_t (*write)(struct file *, const char *, size_t, loff_t *);
324 write = proc_file->f_dentry->d_inode->i_fop->write;
326 proc_file->f_pos = file->f_pos;
327 err = (*write)(proc_file, buf, len, &proc_file->f_pos);
328 file->f_pos = proc_file->f_pos;
333 static int open_host_sock(char *host_file, int *filter_out)
338 end = &host_file[strlen(host_file)];
341 fd = os_connect_socket(host_file);
347 fd = os_connect_socket(host_file);
351 static void free_contents(struct hppfs_data *head)
353 struct hppfs_data *data;
354 struct list_head *ele, *next;
356 if(head == NULL) return;
358 list_for_each_safe(ele, next, &head->list){
359 data = list_entry(ele, struct hppfs_data, list);
365 static struct hppfs_data *hppfs_get_data(int fd, int filter,
366 struct file *proc_file,
367 struct file *hppfs_file,
370 struct hppfs_data *data, *new, *head;
374 data = kmalloc(sizeof(*data), GFP_KERNEL);
376 printk("hppfs_get_data : head allocation failed\n");
380 INIT_LIST_HEAD(&data->list);
386 while((n = read_proc(proc_file, data->contents,
387 sizeof(data->contents), NULL, 0)) > 0)
388 os_write_file(fd, data->contents, n);
389 err = os_shutdown_socket(fd, 0, 1);
391 printk("hppfs_get_data : failed to shut down "
397 n = os_read_file(fd, data->contents, sizeof(data->contents));
400 printk("hppfs_get_data : read failed, errno = %d\n",
409 if(n < sizeof(data->contents))
412 new = kmalloc(sizeof(*data), GFP_KERNEL);
414 printk("hppfs_get_data : data allocation failed\n");
419 INIT_LIST_HEAD(&new->list);
420 list_add(&new->list, &data->list);
428 return(ERR_PTR(err));
431 static struct hppfs_private *hppfs_data(void)
433 struct hppfs_private *data;
435 data = kmalloc(sizeof(*data), GFP_KERNEL);
439 *data = ((struct hppfs_private ) { .host_fd = -1,
441 .contents = NULL } );
445 static int file_mode(int fmode)
447 if(fmode == (FMODE_READ | FMODE_WRITE))
449 if(fmode == FMODE_READ)
451 if(fmode == FMODE_WRITE)
456 static int hppfs_open(struct inode *inode, struct file *file)
458 struct hppfs_private *data;
459 struct dentry *proc_dentry;
461 int err, fd, type, filter;
468 host_file = dentry_name(file->f_dentry, strlen("/rw"));
469 if(host_file == NULL)
472 proc_dentry = HPPFS_I(inode)->proc_dentry;
474 /* XXX This isn't closed anywhere */
475 data->proc_file = dentry_open(dget(proc_dentry), NULL,
476 file_mode(file->f_mode));
477 err = PTR_ERR(data->proc_file);
478 if(IS_ERR(data->proc_file))
481 type = os_file_type(host_file);
482 if(type == OS_TYPE_FILE){
483 fd = os_open_file(host_file, of_read(OPENFLAGS()), 0);
486 else printk("hppfs_open : failed to open '%s', errno = %d\n",
489 data->contents = NULL;
491 else if(type == OS_TYPE_DIR){
492 fd = open_host_sock(host_file, &filter);
494 data->contents = hppfs_get_data(fd, filter,
497 if(!IS_ERR(data->contents))
500 else printk("hppfs_open : failed to open a socket in "
501 "'%s', errno = %d\n", host_file, -fd);
505 file->private_data = data;
511 free_contents(data->contents);
517 static int hppfs_dir_open(struct inode *inode, struct file *file)
519 struct hppfs_private *data;
520 struct dentry *proc_dentry;
528 proc_dentry = HPPFS_I(inode)->proc_dentry;
529 data->proc_file = dentry_open(dget(proc_dentry), NULL,
530 file_mode(file->f_mode));
531 err = PTR_ERR(data->proc_file);
532 if(IS_ERR(data->proc_file))
535 file->private_data = data;
544 static loff_t hppfs_llseek(struct file *file, loff_t off, int where)
546 struct hppfs_private *data = file->private_data;
547 struct file *proc_file = data->proc_file;
548 loff_t (*llseek)(struct file *, loff_t, int);
551 llseek = proc_file->f_dentry->d_inode->i_fop->llseek;
553 ret = (*llseek)(proc_file, off, where);
558 return(default_llseek(file, off, where));
561 static struct file_operations hppfs_file_fops = {
563 .llseek = hppfs_llseek,
565 .write = hppfs_write,
569 struct hppfs_dirent {
572 struct dentry *dentry;
575 static int hppfs_filldir(void *d, const char *name, int size,
576 loff_t offset, ino_t inode, unsigned int type)
578 struct hppfs_dirent *dirent = d;
580 if(file_removed(dirent->dentry, name))
583 return((*dirent->filldir)(dirent->vfs_dirent, name, size, offset,
587 static int hppfs_readdir(struct file *file, void *ent, filldir_t filldir)
589 struct hppfs_private *data = file->private_data;
590 struct file *proc_file = data->proc_file;
591 int (*readdir)(struct file *, void *, filldir_t);
592 struct hppfs_dirent dirent = ((struct hppfs_dirent)
595 .dentry = file->f_dentry } );
598 readdir = proc_file->f_dentry->d_inode->i_fop->readdir;
600 proc_file->f_pos = file->f_pos;
601 err = (*readdir)(proc_file, &dirent, hppfs_filldir);
602 file->f_pos = proc_file->f_pos;
607 static int hppfs_fsync(struct file *file, struct dentry *dentry, int datasync)
612 static struct file_operations hppfs_dir_fops = {
614 .readdir = hppfs_readdir,
615 .open = hppfs_dir_open,
616 .fsync = hppfs_fsync,
619 static int hppfs_statfs(struct super_block *sb, struct kstatfs *sf)
626 sf->f_type = HPPFS_SUPER_MAGIC;
630 static struct inode *hppfs_alloc_inode(struct super_block *sb)
632 struct hppfs_inode_info *hi;
634 hi = kmalloc(sizeof(*hi), GFP_KERNEL);
638 *hi = ((struct hppfs_inode_info) { .proc_dentry = NULL });
639 inode_init_once(&hi->vfs_inode);
640 return(&hi->vfs_inode);
643 void hppfs_delete_inode(struct inode *ino)
648 static void hppfs_destroy_inode(struct inode *inode)
650 kfree(HPPFS_I(inode));
653 static struct super_operations hppfs_sbops = {
654 .alloc_inode = hppfs_alloc_inode,
655 .destroy_inode = hppfs_destroy_inode,
656 .read_inode = hppfs_read_inode,
657 .delete_inode = hppfs_delete_inode,
658 .statfs = hppfs_statfs,
661 static int hppfs_readlink(struct dentry *dentry, char *buffer, int buflen)
663 struct file *proc_file;
664 struct dentry *proc_dentry;
665 int (*readlink)(struct dentry *, char *, int);
668 proc_dentry = HPPFS_I(dentry->d_inode)->proc_dentry;
669 proc_file = dentry_open(dget(proc_dentry), NULL, O_RDONLY);
670 err = PTR_ERR(proc_dentry);
671 if(IS_ERR(proc_dentry))
674 readlink = proc_dentry->d_inode->i_op->readlink;
675 n = (*readlink)(proc_dentry, buffer, buflen);
682 static int hppfs_follow_link(struct dentry *dentry, struct nameidata *nd)
684 struct file *proc_file;
685 struct dentry *proc_dentry;
686 int (*follow_link)(struct dentry *, struct nameidata *);
689 proc_dentry = HPPFS_I(dentry->d_inode)->proc_dentry;
690 proc_file = dentry_open(dget(proc_dentry), NULL, O_RDONLY);
691 err = PTR_ERR(proc_dentry);
692 if(IS_ERR(proc_dentry))
695 follow_link = proc_dentry->d_inode->i_op->follow_link;
696 n = (*follow_link)(proc_dentry, nd);
703 static struct inode_operations hppfs_dir_iops = {
704 .lookup = hppfs_lookup,
707 static struct inode_operations hppfs_link_iops = {
708 .readlink = hppfs_readlink,
709 .follow_link = hppfs_follow_link,
712 static int init_inode(struct inode *inode, struct dentry *dentry)
714 if(S_ISDIR(dentry->d_inode->i_mode)){
715 inode->i_op = &hppfs_dir_iops;
716 inode->i_fop = &hppfs_dir_fops;
718 else if(S_ISLNK(dentry->d_inode->i_mode)){
719 inode->i_op = &hppfs_link_iops;
720 inode->i_fop = &hppfs_file_fops;
723 inode->i_op = &hppfs_file_iops;
724 inode->i_fop = &hppfs_file_fops;
727 HPPFS_I(inode)->proc_dentry = dentry;
732 static int hppfs_fill_super(struct super_block *sb, void *d, int silent)
734 struct inode *root_inode;
735 struct file_system_type *procfs;
736 struct super_block *proc_sb;
740 procfs = get_fs_type("proc");
744 if(list_empty(&procfs->fs_supers))
747 proc_sb = list_entry(procfs->fs_supers.next, struct super_block,
750 sb->s_blocksize = 1024;
751 sb->s_blocksize_bits = 10;
752 sb->s_magic = HPPFS_SUPER_MAGIC;
753 sb->s_op = &hppfs_sbops;
755 root_inode = iget(sb, 0);
756 if(root_inode == NULL)
759 err = init_inode(root_inode, proc_sb->s_root);
764 sb->s_root = d_alloc_root(root_inode);
765 if(sb->s_root == NULL)
768 hppfs_read_inode(root_inode);
778 static struct super_block *hppfs_read_super(struct file_system_type *type,
779 int flags, const char *dev_name,
782 return(get_sb_nodev(type, flags, data, hppfs_fill_super));
785 static struct file_system_type hppfs_type = {
786 .owner = THIS_MODULE,
788 .get_sb = hppfs_read_super,
789 .kill_sb = kill_anon_super,
793 static int __init init_hppfs(void)
795 return(register_filesystem(&hppfs_type));
798 static void __exit exit_hppfs(void)
800 unregister_filesystem(&hppfs_type);
803 module_init(init_hppfs)
804 module_exit(exit_hppfs)
805 MODULE_LICENSE("GPL");
808 * Overrides for Emacs so that we follow Linus's tabbing style.
809 * Emacs will notice this stuff at the end of the file and automatically
810 * adjust the settings for this buffer only. This must remain at the end
812 * ---------------------------------------------------------------------------
814 * c-file-style: "linux"