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 <asm/uaccess.h>
22 static struct inode *get_inode(struct super_block *, struct dentry *);
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 inode vfs_inode;
41 static inline struct hppfs_inode_info *HPPFS_I(struct inode *inode)
43 return container_of(inode, struct hppfs_inode_info, vfs_inode);
46 #define HPPFS_SUPER_MAGIC 0xb00000ee
48 static const struct super_operations hppfs_sbops;
50 static int is_pid(struct dentry *dentry)
52 struct super_block *sb;
56 if (dentry->d_parent != sb->s_root)
59 for (i = 0; i < dentry->d_name.len; i++) {
60 if (!isdigit(dentry->d_name.name[i]))
66 static char *dentry_name(struct dentry *dentry, int extra)
68 struct dentry *parent;
75 while (parent->d_parent != parent) {
77 len += strlen("pid") + 1;
78 else len += parent->d_name.len + 1;
79 parent = parent->d_parent;
84 name = kmalloc(len + extra + 1, GFP_KERNEL);
90 while (parent->d_parent != parent) {
93 seg_len = strlen("pid");
96 seg_name = parent->d_name.name;
97 seg_len = parent->d_name.len;
102 strncpy(&name[len + 1], seg_name, seg_len);
103 parent = parent->d_parent;
105 strncpy(name, root, strlen(root));
109 static int file_removed(struct dentry *dentry, const char *file)
116 extra += strlen(file) + 1;
118 host_file = dentry_name(dentry, extra + strlen("/remove"));
119 if (host_file == NULL) {
120 printk(KERN_ERR "file_removed : allocation failed\n");
125 strcat(host_file, "/");
126 strcat(host_file, file);
128 strcat(host_file, "/remove");
130 fd = os_open_file(host_file, of_read(OPENFLAGS()), 0);
139 static struct dentry *hppfs_lookup(struct inode *ino, struct dentry *dentry,
140 struct nameidata *nd)
142 struct dentry *proc_dentry, *parent;
143 struct qstr *name = &dentry->d_name;
147 deleted = file_removed(dentry, NULL);
149 return ERR_PTR(deleted);
151 return ERR_PTR(-ENOENT);
153 parent = HPPFS_I(ino)->proc_dentry;
154 mutex_lock(&parent->d_inode->i_mutex);
155 proc_dentry = lookup_one_len(name->name, parent, name->len);
156 mutex_unlock(&parent->d_inode->i_mutex);
158 if (IS_ERR(proc_dentry))
162 inode = get_inode(ino->i_sb, proc_dentry);
166 d_add(dentry, inode);
173 static const struct inode_operations hppfs_file_iops = {
176 static ssize_t read_proc(struct file *file, char __user *buf, ssize_t count,
177 loff_t *ppos, int is_user)
179 ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
182 read = file->f_path.dentry->d_inode->i_fop->read;
187 n = (*read)(file, buf, count, &file->f_pos);
197 static ssize_t hppfs_read_file(int fd, char __user *buf, ssize_t count)
204 new_buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
205 if (new_buf == NULL) {
206 printk(KERN_ERR "hppfs_read_file : kmalloc failed\n");
211 cur = min_t(ssize_t, count, PAGE_SIZE);
212 err = os_read_file(fd, new_buf, cur);
214 printk(KERN_ERR "hppfs_read : read failed, "
215 "errno = %d\n", err);
221 if (copy_to_user(buf, new_buf, err)) {
234 static ssize_t hppfs_read(struct file *file, char __user *buf, size_t count,
237 struct hppfs_private *hppfs = file->private_data;
238 struct hppfs_data *data;
242 if (hppfs->contents != NULL) {
245 if (*ppos >= hppfs->len)
248 data = hppfs->contents;
250 while (off >= sizeof(data->contents)) {
251 data = list_entry(data->list.next, struct hppfs_data,
253 off -= sizeof(data->contents);
256 if (off + count > hppfs->len)
257 count = hppfs->len - off;
258 rem = copy_to_user(buf, &data->contents[off], count);
259 *ppos += count - rem;
262 } else if (hppfs->host_fd != -1) {
263 err = os_seek_file(hppfs->host_fd, *ppos);
265 printk(KERN_ERR "hppfs_read : seek failed, "
266 "errno = %d\n", err);
269 err = hppfs_read_file(hppfs->host_fd, buf, count);
271 printk(KERN_ERR "hppfs_read: read failed: %d\n", err);
278 else count = read_proc(hppfs->proc_file, buf, count, ppos, 1);
283 static ssize_t hppfs_write(struct file *file, const char __user *buf,
284 size_t len, loff_t *ppos)
286 struct hppfs_private *data = file->private_data;
287 struct file *proc_file = data->proc_file;
288 ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
290 write = proc_file->f_path.dentry->d_inode->i_fop->write;
291 return (*write)(proc_file, buf, len, ppos);
294 static int open_host_sock(char *host_file, int *filter_out)
299 end = &host_file[strlen(host_file)];
302 fd = os_connect_socket(host_file);
308 fd = os_connect_socket(host_file);
312 static void free_contents(struct hppfs_data *head)
314 struct hppfs_data *data;
315 struct list_head *ele, *next;
320 list_for_each_safe(ele, next, &head->list) {
321 data = list_entry(ele, struct hppfs_data, list);
327 static struct hppfs_data *hppfs_get_data(int fd, int filter,
328 struct file *proc_file,
329 struct file *hppfs_file,
332 struct hppfs_data *data, *new, *head;
336 data = kmalloc(sizeof(*data), GFP_KERNEL);
338 printk(KERN_ERR "hppfs_get_data : head allocation failed\n");
342 INIT_LIST_HEAD(&data->list);
348 while ((n = read_proc(proc_file, data->contents,
349 sizeof(data->contents), NULL, 0)) > 0)
350 os_write_file(fd, data->contents, n);
351 err = os_shutdown_socket(fd, 0, 1);
353 printk(KERN_ERR "hppfs_get_data : failed to shut down "
359 n = os_read_file(fd, data->contents, sizeof(data->contents));
362 printk(KERN_ERR "hppfs_get_data : read failed, "
363 "errno = %d\n", err);
370 if (n < sizeof(data->contents))
373 new = kmalloc(sizeof(*data), GFP_KERNEL);
375 printk(KERN_ERR "hppfs_get_data : data allocation "
381 INIT_LIST_HEAD(&new->list);
382 list_add(&new->list, &data->list);
393 static struct hppfs_private *hppfs_data(void)
395 struct hppfs_private *data;
397 data = kmalloc(sizeof(*data), GFP_KERNEL);
401 *data = ((struct hppfs_private ) { .host_fd = -1,
403 .contents = NULL } );
407 static int file_mode(int fmode)
409 if (fmode == (FMODE_READ | FMODE_WRITE))
411 if (fmode == FMODE_READ)
413 if (fmode == FMODE_WRITE)
418 static int hppfs_open(struct inode *inode, struct file *file)
420 const struct cred *cred = file->f_cred;
421 struct hppfs_private *data;
422 struct vfsmount *proc_mnt;
423 struct dentry *proc_dentry;
425 int err, fd, type, filter;
432 host_file = dentry_name(file->f_path.dentry, strlen("/rw"));
433 if (host_file == NULL)
436 proc_dentry = HPPFS_I(inode)->proc_dentry;
437 proc_mnt = inode->i_sb->s_fs_info;
439 /* XXX This isn't closed anywhere */
440 data->proc_file = dentry_open(dget(proc_dentry), mntget(proc_mnt),
441 file_mode(file->f_mode), cred);
442 err = PTR_ERR(data->proc_file);
443 if (IS_ERR(data->proc_file))
446 type = os_file_type(host_file);
447 if (type == OS_TYPE_FILE) {
448 fd = os_open_file(host_file, of_read(OPENFLAGS()), 0);
452 printk(KERN_ERR "hppfs_open : failed to open '%s', "
453 "errno = %d\n", host_file, -fd);
455 data->contents = NULL;
456 } else if (type == OS_TYPE_DIR) {
457 fd = open_host_sock(host_file, &filter);
459 data->contents = hppfs_get_data(fd, filter,
462 if (!IS_ERR(data->contents))
465 printk(KERN_ERR "hppfs_open : failed to open a socket "
466 "in '%s', errno = %d\n", host_file, -fd);
470 file->private_data = data;
476 free_contents(data->contents);
482 static int hppfs_dir_open(struct inode *inode, struct file *file)
484 const struct cred *cred = file->f_cred;
485 struct hppfs_private *data;
486 struct vfsmount *proc_mnt;
487 struct dentry *proc_dentry;
495 proc_dentry = HPPFS_I(inode)->proc_dentry;
496 proc_mnt = inode->i_sb->s_fs_info;
497 data->proc_file = dentry_open(dget(proc_dentry), mntget(proc_mnt),
498 file_mode(file->f_mode), cred);
499 err = PTR_ERR(data->proc_file);
500 if (IS_ERR(data->proc_file))
503 file->private_data = data;
512 static loff_t hppfs_llseek(struct file *file, loff_t off, int where)
514 struct hppfs_private *data = file->private_data;
515 struct file *proc_file = data->proc_file;
516 loff_t (*llseek)(struct file *, loff_t, int);
519 llseek = proc_file->f_path.dentry->d_inode->i_fop->llseek;
520 if (llseek != NULL) {
521 ret = (*llseek)(proc_file, off, where);
526 return default_llseek(file, off, where);
529 static const struct file_operations hppfs_file_fops = {
531 .llseek = hppfs_llseek,
533 .write = hppfs_write,
537 struct hppfs_dirent {
540 struct dentry *dentry;
543 static int hppfs_filldir(void *d, const char *name, int size,
544 loff_t offset, u64 inode, unsigned int type)
546 struct hppfs_dirent *dirent = d;
548 if (file_removed(dirent->dentry, name))
551 return (*dirent->filldir)(dirent->vfs_dirent, name, size, offset,
555 static int hppfs_readdir(struct file *file, void *ent, filldir_t filldir)
557 struct hppfs_private *data = file->private_data;
558 struct file *proc_file = data->proc_file;
559 int (*readdir)(struct file *, void *, filldir_t);
560 struct hppfs_dirent dirent = ((struct hppfs_dirent)
563 .dentry = file->f_path.dentry
567 readdir = proc_file->f_path.dentry->d_inode->i_fop->readdir;
569 proc_file->f_pos = file->f_pos;
570 err = (*readdir)(proc_file, &dirent, hppfs_filldir);
571 file->f_pos = proc_file->f_pos;
576 static int hppfs_fsync(struct file *file, int datasync)
581 static const struct file_operations hppfs_dir_fops = {
583 .readdir = hppfs_readdir,
584 .open = hppfs_dir_open,
585 .fsync = hppfs_fsync,
586 .llseek = default_llseek,
589 static int hppfs_statfs(struct dentry *dentry, struct kstatfs *sf)
596 sf->f_type = HPPFS_SUPER_MAGIC;
600 static struct inode *hppfs_alloc_inode(struct super_block *sb)
602 struct hppfs_inode_info *hi;
604 hi = kmalloc(sizeof(*hi), GFP_KERNEL);
608 hi->proc_dentry = NULL;
609 inode_init_once(&hi->vfs_inode);
610 return &hi->vfs_inode;
613 void hppfs_evict_inode(struct inode *ino)
616 dput(HPPFS_I(ino)->proc_dentry);
617 mntput(ino->i_sb->s_fs_info);
620 static void hppfs_i_callback(struct rcu_head *head)
622 struct inode *inode = container_of(head, struct inode, i_rcu);
623 INIT_LIST_HEAD(&inode->i_dentry);
624 kfree(HPPFS_I(inode));
627 static void hppfs_destroy_inode(struct inode *inode)
629 call_rcu(&inode->i_rcu, hppfs_i_callback);
632 static const struct super_operations hppfs_sbops = {
633 .alloc_inode = hppfs_alloc_inode,
634 .destroy_inode = hppfs_destroy_inode,
635 .evict_inode = hppfs_evict_inode,
636 .statfs = hppfs_statfs,
639 static int hppfs_readlink(struct dentry *dentry, char __user *buffer,
642 struct dentry *proc_dentry = HPPFS_I(dentry->d_inode)->proc_dentry;
643 return proc_dentry->d_inode->i_op->readlink(proc_dentry, buffer,
647 static void *hppfs_follow_link(struct dentry *dentry, struct nameidata *nd)
649 struct dentry *proc_dentry = HPPFS_I(dentry->d_inode)->proc_dentry;
651 return proc_dentry->d_inode->i_op->follow_link(proc_dentry, nd);
654 static void hppfs_put_link(struct dentry *dentry, struct nameidata *nd,
657 struct dentry *proc_dentry = HPPFS_I(dentry->d_inode)->proc_dentry;
659 if (proc_dentry->d_inode->i_op->put_link)
660 proc_dentry->d_inode->i_op->put_link(proc_dentry, nd, cookie);
663 static const struct inode_operations hppfs_dir_iops = {
664 .lookup = hppfs_lookup,
667 static const struct inode_operations hppfs_link_iops = {
668 .readlink = hppfs_readlink,
669 .follow_link = hppfs_follow_link,
670 .put_link = hppfs_put_link,
673 static struct inode *get_inode(struct super_block *sb, struct dentry *dentry)
675 struct inode *proc_ino = dentry->d_inode;
676 struct inode *inode = new_inode(sb);
680 return ERR_PTR(-ENOMEM);
683 if (S_ISDIR(dentry->d_inode->i_mode)) {
684 inode->i_op = &hppfs_dir_iops;
685 inode->i_fop = &hppfs_dir_fops;
686 } else if (S_ISLNK(dentry->d_inode->i_mode)) {
687 inode->i_op = &hppfs_link_iops;
688 inode->i_fop = &hppfs_file_fops;
690 inode->i_op = &hppfs_file_iops;
691 inode->i_fop = &hppfs_file_fops;
694 HPPFS_I(inode)->proc_dentry = dentry;
696 inode->i_uid = proc_ino->i_uid;
697 inode->i_gid = proc_ino->i_gid;
698 inode->i_atime = proc_ino->i_atime;
699 inode->i_mtime = proc_ino->i_mtime;
700 inode->i_ctime = proc_ino->i_ctime;
701 inode->i_ino = proc_ino->i_ino;
702 inode->i_mode = proc_ino->i_mode;
703 inode->i_nlink = proc_ino->i_nlink;
704 inode->i_size = proc_ino->i_size;
705 inode->i_blocks = proc_ino->i_blocks;
710 static int hppfs_fill_super(struct super_block *sb, void *d, int silent)
712 struct inode *root_inode;
713 struct vfsmount *proc_mnt;
716 proc_mnt = mntget(current->nsproxy->pid_ns->proc_mnt);
717 if (IS_ERR(proc_mnt))
720 sb->s_blocksize = 1024;
721 sb->s_blocksize_bits = 10;
722 sb->s_magic = HPPFS_SUPER_MAGIC;
723 sb->s_op = &hppfs_sbops;
724 sb->s_fs_info = proc_mnt;
727 root_inode = get_inode(sb, dget(proc_mnt->mnt_sb->s_root));
731 sb->s_root = d_alloc_root(root_inode);
745 static struct dentry *hppfs_read_super(struct file_system_type *type,
746 int flags, const char *dev_name,
749 return mount_nodev(type, flags, data, hppfs_fill_super);
752 static struct file_system_type hppfs_type = {
753 .owner = THIS_MODULE,
755 .mount = hppfs_read_super,
756 .kill_sb = kill_anon_super,
760 static int __init init_hppfs(void)
762 return register_filesystem(&hppfs_type);
765 static void __exit exit_hppfs(void)
767 unregister_filesystem(&hppfs_type);
770 module_init(init_hppfs)
771 module_exit(exit_hppfs)
772 MODULE_LICENSE("GPL");