Full support for Ginger Console
[linux-ginger.git] / fs / hppfs / hppfs.c
bloba5089a6dd67a741b79279ffb435d94e2162f510e
1 /*
2 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
4 */
6 #include <linux/ctype.h>
7 #include <linux/dcache.h>
8 #include <linux/file.h>
9 #include <linux/fs.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>
19 #include "os.h"
21 static struct inode *get_inode(struct super_block *, struct dentry *);
23 struct hppfs_data {
24 struct list_head list;
25 char contents[PAGE_SIZE - sizeof(struct list_head)];
28 struct hppfs_private {
29 struct file *proc_file;
30 int host_fd;
31 loff_t len;
32 struct hppfs_data *contents;
35 struct hppfs_inode_info {
36 struct dentry *proc_dentry;
37 struct inode vfs_inode;
40 static inline struct hppfs_inode_info *HPPFS_I(struct inode *inode)
42 return container_of(inode, struct hppfs_inode_info, vfs_inode);
45 #define HPPFS_SUPER_MAGIC 0xb00000ee
47 static const struct super_operations hppfs_sbops;
49 static int is_pid(struct dentry *dentry)
51 struct super_block *sb;
52 int i;
54 sb = dentry->d_sb;
55 if (dentry->d_parent != sb->s_root)
56 return 0;
58 for (i = 0; i < dentry->d_name.len; i++) {
59 if (!isdigit(dentry->d_name.name[i]))
60 return 0;
62 return 1;
65 static char *dentry_name(struct dentry *dentry, int extra)
67 struct dentry *parent;
68 char *root, *name;
69 const char *seg_name;
70 int len, seg_len;
72 len = 0;
73 parent = dentry;
74 while (parent->d_parent != parent) {
75 if (is_pid(parent))
76 len += strlen("pid") + 1;
77 else len += parent->d_name.len + 1;
78 parent = parent->d_parent;
81 root = "proc";
82 len += strlen(root);
83 name = kmalloc(len + extra + 1, GFP_KERNEL);
84 if (name == NULL)
85 return NULL;
87 name[len] = '\0';
88 parent = dentry;
89 while (parent->d_parent != parent) {
90 if (is_pid(parent)) {
91 seg_name = "pid";
92 seg_len = strlen("pid");
94 else {
95 seg_name = parent->d_name.name;
96 seg_len = parent->d_name.len;
99 len -= seg_len + 1;
100 name[len] = '/';
101 strncpy(&name[len + 1], seg_name, seg_len);
102 parent = parent->d_parent;
104 strncpy(name, root, strlen(root));
105 return name;
108 static int file_removed(struct dentry *dentry, const char *file)
110 char *host_file;
111 int extra, fd;
113 extra = 0;
114 if (file != NULL)
115 extra += strlen(file) + 1;
117 host_file = dentry_name(dentry, extra + strlen("/remove"));
118 if (host_file == NULL) {
119 printk(KERN_ERR "file_removed : allocation failed\n");
120 return -ENOMEM;
123 if (file != NULL) {
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);
130 kfree(host_file);
131 if (fd > 0) {
132 os_close_file(fd);
133 return 1;
135 return 0;
138 static struct dentry *hppfs_lookup(struct inode *ino, struct dentry *dentry,
139 struct nameidata *nd)
141 struct dentry *proc_dentry, *new, *parent;
142 struct inode *inode;
143 int err, deleted;
145 deleted = file_removed(dentry, NULL);
146 if (deleted < 0)
147 return ERR_PTR(deleted);
148 else if (deleted)
149 return ERR_PTR(-ENOENT);
151 err = -ENOMEM;
152 parent = HPPFS_I(ino)->proc_dentry;
153 mutex_lock(&parent->d_inode->i_mutex);
154 proc_dentry = d_lookup(parent, &dentry->d_name);
155 if (proc_dentry == NULL) {
156 proc_dentry = d_alloc(parent, &dentry->d_name);
157 if (proc_dentry == NULL) {
158 mutex_unlock(&parent->d_inode->i_mutex);
159 goto out;
161 new = (*parent->d_inode->i_op->lookup)(parent->d_inode,
162 proc_dentry, NULL);
163 if (new) {
164 dput(proc_dentry);
165 proc_dentry = new;
168 mutex_unlock(&parent->d_inode->i_mutex);
170 if (IS_ERR(proc_dentry))
171 return proc_dentry;
173 err = -ENOMEM;
174 inode = get_inode(ino->i_sb, proc_dentry);
175 if (!inode)
176 goto out_dput;
178 d_add(dentry, inode);
179 return NULL;
181 out_dput:
182 dput(proc_dentry);
183 out:
184 return ERR_PTR(err);
187 static const struct inode_operations hppfs_file_iops = {
190 static ssize_t read_proc(struct file *file, char __user *buf, ssize_t count,
191 loff_t *ppos, int is_user)
193 ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
194 ssize_t n;
196 read = file->f_path.dentry->d_inode->i_fop->read;
198 if (!is_user)
199 set_fs(KERNEL_DS);
201 n = (*read)(file, buf, count, &file->f_pos);
203 if (!is_user)
204 set_fs(USER_DS);
206 if (ppos)
207 *ppos = file->f_pos;
208 return n;
211 static ssize_t hppfs_read_file(int fd, char __user *buf, ssize_t count)
213 ssize_t n;
214 int cur, err;
215 char *new_buf;
217 n = -ENOMEM;
218 new_buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
219 if (new_buf == NULL) {
220 printk(KERN_ERR "hppfs_read_file : kmalloc failed\n");
221 goto out;
223 n = 0;
224 while (count > 0) {
225 cur = min_t(ssize_t, count, PAGE_SIZE);
226 err = os_read_file(fd, new_buf, cur);
227 if (err < 0) {
228 printk(KERN_ERR "hppfs_read : read failed, "
229 "errno = %d\n", err);
230 n = err;
231 goto out_free;
232 } else if (err == 0)
233 break;
235 if (copy_to_user(buf, new_buf, err)) {
236 n = -EFAULT;
237 goto out_free;
239 n += err;
240 count -= err;
242 out_free:
243 kfree(new_buf);
244 out:
245 return n;
248 static ssize_t hppfs_read(struct file *file, char __user *buf, size_t count,
249 loff_t *ppos)
251 struct hppfs_private *hppfs = file->private_data;
252 struct hppfs_data *data;
253 loff_t off;
254 int err;
256 if (hppfs->contents != NULL) {
257 int rem;
259 if (*ppos >= hppfs->len)
260 return 0;
262 data = hppfs->contents;
263 off = *ppos;
264 while (off >= sizeof(data->contents)) {
265 data = list_entry(data->list.next, struct hppfs_data,
266 list);
267 off -= sizeof(data->contents);
270 if (off + count > hppfs->len)
271 count = hppfs->len - off;
272 rem = copy_to_user(buf, &data->contents[off], count);
273 *ppos += count - rem;
274 if (rem > 0)
275 return -EFAULT;
276 } else if (hppfs->host_fd != -1) {
277 err = os_seek_file(hppfs->host_fd, *ppos);
278 if (err) {
279 printk(KERN_ERR "hppfs_read : seek failed, "
280 "errno = %d\n", err);
281 return err;
283 err = hppfs_read_file(hppfs->host_fd, buf, count);
284 if (err < 0) {
285 printk(KERN_ERR "hppfs_read: read failed: %d\n", err);
286 return err;
288 count = err;
289 if (count > 0)
290 *ppos += count;
292 else count = read_proc(hppfs->proc_file, buf, count, ppos, 1);
294 return count;
297 static ssize_t hppfs_write(struct file *file, const char __user *buf,
298 size_t len, loff_t *ppos)
300 struct hppfs_private *data = file->private_data;
301 struct file *proc_file = data->proc_file;
302 ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
304 write = proc_file->f_path.dentry->d_inode->i_fop->write;
305 return (*write)(proc_file, buf, len, ppos);
308 static int open_host_sock(char *host_file, int *filter_out)
310 char *end;
311 int fd;
313 end = &host_file[strlen(host_file)];
314 strcpy(end, "/rw");
315 *filter_out = 1;
316 fd = os_connect_socket(host_file);
317 if (fd > 0)
318 return fd;
320 strcpy(end, "/r");
321 *filter_out = 0;
322 fd = os_connect_socket(host_file);
323 return fd;
326 static void free_contents(struct hppfs_data *head)
328 struct hppfs_data *data;
329 struct list_head *ele, *next;
331 if (head == NULL)
332 return;
334 list_for_each_safe(ele, next, &head->list) {
335 data = list_entry(ele, struct hppfs_data, list);
336 kfree(data);
338 kfree(head);
341 static struct hppfs_data *hppfs_get_data(int fd, int filter,
342 struct file *proc_file,
343 struct file *hppfs_file,
344 loff_t *size_out)
346 struct hppfs_data *data, *new, *head;
347 int n, err;
349 err = -ENOMEM;
350 data = kmalloc(sizeof(*data), GFP_KERNEL);
351 if (data == NULL) {
352 printk(KERN_ERR "hppfs_get_data : head allocation failed\n");
353 goto failed;
356 INIT_LIST_HEAD(&data->list);
358 head = data;
359 *size_out = 0;
361 if (filter) {
362 while ((n = read_proc(proc_file, data->contents,
363 sizeof(data->contents), NULL, 0)) > 0)
364 os_write_file(fd, data->contents, n);
365 err = os_shutdown_socket(fd, 0, 1);
366 if (err) {
367 printk(KERN_ERR "hppfs_get_data : failed to shut down "
368 "socket\n");
369 goto failed_free;
372 while (1) {
373 n = os_read_file(fd, data->contents, sizeof(data->contents));
374 if (n < 0) {
375 err = n;
376 printk(KERN_ERR "hppfs_get_data : read failed, "
377 "errno = %d\n", err);
378 goto failed_free;
379 } else if (n == 0)
380 break;
382 *size_out += n;
384 if (n < sizeof(data->contents))
385 break;
387 new = kmalloc(sizeof(*data), GFP_KERNEL);
388 if (new == 0) {
389 printk(KERN_ERR "hppfs_get_data : data allocation "
390 "failed\n");
391 err = -ENOMEM;
392 goto failed_free;
395 INIT_LIST_HEAD(&new->list);
396 list_add(&new->list, &data->list);
397 data = new;
399 return head;
401 failed_free:
402 free_contents(head);
403 failed:
404 return ERR_PTR(err);
407 static struct hppfs_private *hppfs_data(void)
409 struct hppfs_private *data;
411 data = kmalloc(sizeof(*data), GFP_KERNEL);
412 if (data == NULL)
413 return data;
415 *data = ((struct hppfs_private ) { .host_fd = -1,
416 .len = -1,
417 .contents = NULL } );
418 return data;
421 static int file_mode(int fmode)
423 if (fmode == (FMODE_READ | FMODE_WRITE))
424 return O_RDWR;
425 if (fmode == FMODE_READ)
426 return O_RDONLY;
427 if (fmode == FMODE_WRITE)
428 return O_WRONLY;
429 return 0;
432 static int hppfs_open(struct inode *inode, struct file *file)
434 const struct cred *cred = file->f_cred;
435 struct hppfs_private *data;
436 struct vfsmount *proc_mnt;
437 struct dentry *proc_dentry;
438 char *host_file;
439 int err, fd, type, filter;
441 err = -ENOMEM;
442 data = hppfs_data();
443 if (data == NULL)
444 goto out;
446 host_file = dentry_name(file->f_path.dentry, strlen("/rw"));
447 if (host_file == NULL)
448 goto out_free2;
450 proc_dentry = HPPFS_I(inode)->proc_dentry;
451 proc_mnt = inode->i_sb->s_fs_info;
453 /* XXX This isn't closed anywhere */
454 data->proc_file = dentry_open(dget(proc_dentry), mntget(proc_mnt),
455 file_mode(file->f_mode), cred);
456 err = PTR_ERR(data->proc_file);
457 if (IS_ERR(data->proc_file))
458 goto out_free1;
460 type = os_file_type(host_file);
461 if (type == OS_TYPE_FILE) {
462 fd = os_open_file(host_file, of_read(OPENFLAGS()), 0);
463 if (fd >= 0)
464 data->host_fd = fd;
465 else
466 printk(KERN_ERR "hppfs_open : failed to open '%s', "
467 "errno = %d\n", host_file, -fd);
469 data->contents = NULL;
470 } else if (type == OS_TYPE_DIR) {
471 fd = open_host_sock(host_file, &filter);
472 if (fd > 0) {
473 data->contents = hppfs_get_data(fd, filter,
474 data->proc_file,
475 file, &data->len);
476 if (!IS_ERR(data->contents))
477 data->host_fd = fd;
478 } else
479 printk(KERN_ERR "hppfs_open : failed to open a socket "
480 "in '%s', errno = %d\n", host_file, -fd);
482 kfree(host_file);
484 file->private_data = data;
485 return 0;
487 out_free1:
488 kfree(host_file);
489 out_free2:
490 free_contents(data->contents);
491 kfree(data);
492 out:
493 return err;
496 static int hppfs_dir_open(struct inode *inode, struct file *file)
498 const struct cred *cred = file->f_cred;
499 struct hppfs_private *data;
500 struct vfsmount *proc_mnt;
501 struct dentry *proc_dentry;
502 int err;
504 err = -ENOMEM;
505 data = hppfs_data();
506 if (data == NULL)
507 goto out;
509 proc_dentry = HPPFS_I(inode)->proc_dentry;
510 proc_mnt = inode->i_sb->s_fs_info;
511 data->proc_file = dentry_open(dget(proc_dentry), mntget(proc_mnt),
512 file_mode(file->f_mode), cred);
513 err = PTR_ERR(data->proc_file);
514 if (IS_ERR(data->proc_file))
515 goto out_free;
517 file->private_data = data;
518 return 0;
520 out_free:
521 kfree(data);
522 out:
523 return err;
526 static loff_t hppfs_llseek(struct file *file, loff_t off, int where)
528 struct hppfs_private *data = file->private_data;
529 struct file *proc_file = data->proc_file;
530 loff_t (*llseek)(struct file *, loff_t, int);
531 loff_t ret;
533 llseek = proc_file->f_path.dentry->d_inode->i_fop->llseek;
534 if (llseek != NULL) {
535 ret = (*llseek)(proc_file, off, where);
536 if (ret < 0)
537 return ret;
540 return default_llseek(file, off, where);
543 static const struct file_operations hppfs_file_fops = {
544 .owner = NULL,
545 .llseek = hppfs_llseek,
546 .read = hppfs_read,
547 .write = hppfs_write,
548 .open = hppfs_open,
551 struct hppfs_dirent {
552 void *vfs_dirent;
553 filldir_t filldir;
554 struct dentry *dentry;
557 static int hppfs_filldir(void *d, const char *name, int size,
558 loff_t offset, u64 inode, unsigned int type)
560 struct hppfs_dirent *dirent = d;
562 if (file_removed(dirent->dentry, name))
563 return 0;
565 return (*dirent->filldir)(dirent->vfs_dirent, name, size, offset,
566 inode, type);
569 static int hppfs_readdir(struct file *file, void *ent, filldir_t filldir)
571 struct hppfs_private *data = file->private_data;
572 struct file *proc_file = data->proc_file;
573 int (*readdir)(struct file *, void *, filldir_t);
574 struct hppfs_dirent dirent = ((struct hppfs_dirent)
575 { .vfs_dirent = ent,
576 .filldir = filldir,
577 .dentry = file->f_path.dentry
579 int err;
581 readdir = proc_file->f_path.dentry->d_inode->i_fop->readdir;
583 proc_file->f_pos = file->f_pos;
584 err = (*readdir)(proc_file, &dirent, hppfs_filldir);
585 file->f_pos = proc_file->f_pos;
587 return err;
590 static int hppfs_fsync(struct file *file, struct dentry *dentry, int datasync)
592 return 0;
595 static const struct file_operations hppfs_dir_fops = {
596 .owner = NULL,
597 .readdir = hppfs_readdir,
598 .open = hppfs_dir_open,
599 .fsync = hppfs_fsync,
602 static int hppfs_statfs(struct dentry *dentry, struct kstatfs *sf)
604 sf->f_blocks = 0;
605 sf->f_bfree = 0;
606 sf->f_bavail = 0;
607 sf->f_files = 0;
608 sf->f_ffree = 0;
609 sf->f_type = HPPFS_SUPER_MAGIC;
610 return 0;
613 static struct inode *hppfs_alloc_inode(struct super_block *sb)
615 struct hppfs_inode_info *hi;
617 hi = kmalloc(sizeof(*hi), GFP_KERNEL);
618 if (!hi)
619 return NULL;
621 hi->proc_dentry = NULL;
622 inode_init_once(&hi->vfs_inode);
623 return &hi->vfs_inode;
626 void hppfs_delete_inode(struct inode *ino)
628 dput(HPPFS_I(ino)->proc_dentry);
629 mntput(ino->i_sb->s_fs_info);
631 clear_inode(ino);
634 static void hppfs_destroy_inode(struct inode *inode)
636 kfree(HPPFS_I(inode));
639 static const struct super_operations hppfs_sbops = {
640 .alloc_inode = hppfs_alloc_inode,
641 .destroy_inode = hppfs_destroy_inode,
642 .delete_inode = hppfs_delete_inode,
643 .statfs = hppfs_statfs,
646 static int hppfs_readlink(struct dentry *dentry, char __user *buffer,
647 int buflen)
649 struct dentry *proc_dentry;
651 proc_dentry = HPPFS_I(dentry->d_inode)->proc_dentry;
652 return proc_dentry->d_inode->i_op->readlink(proc_dentry, buffer,
653 buflen);
656 static void *hppfs_follow_link(struct dentry *dentry, struct nameidata *nd)
658 struct dentry *proc_dentry;
660 proc_dentry = HPPFS_I(dentry->d_inode)->proc_dentry;
662 return proc_dentry->d_inode->i_op->follow_link(proc_dentry, nd);
665 static const struct inode_operations hppfs_dir_iops = {
666 .lookup = hppfs_lookup,
669 static const struct inode_operations hppfs_link_iops = {
670 .readlink = hppfs_readlink,
671 .follow_link = hppfs_follow_link,
674 static struct inode *get_inode(struct super_block *sb, struct dentry *dentry)
676 struct inode *proc_ino = dentry->d_inode;
677 struct inode *inode = new_inode(sb);
679 if (!inode)
680 return ERR_PTR(-ENOMEM);
682 if (S_ISDIR(dentry->d_inode->i_mode)) {
683 inode->i_op = &hppfs_dir_iops;
684 inode->i_fop = &hppfs_dir_fops;
685 } else if (S_ISLNK(dentry->d_inode->i_mode)) {
686 inode->i_op = &hppfs_link_iops;
687 inode->i_fop = &hppfs_file_fops;
688 } else {
689 inode->i_op = &hppfs_file_iops;
690 inode->i_fop = &hppfs_file_fops;
693 HPPFS_I(inode)->proc_dentry = dget(dentry);
695 inode->i_uid = proc_ino->i_uid;
696 inode->i_gid = proc_ino->i_gid;
697 inode->i_atime = proc_ino->i_atime;
698 inode->i_mtime = proc_ino->i_mtime;
699 inode->i_ctime = proc_ino->i_ctime;
700 inode->i_ino = proc_ino->i_ino;
701 inode->i_mode = proc_ino->i_mode;
702 inode->i_nlink = proc_ino->i_nlink;
703 inode->i_size = proc_ino->i_size;
704 inode->i_blocks = proc_ino->i_blocks;
706 return inode;
709 static int hppfs_fill_super(struct super_block *sb, void *d, int silent)
711 struct inode *root_inode;
712 struct vfsmount *proc_mnt;
713 int err = -ENOENT;
715 proc_mnt = do_kern_mount("proc", 0, "proc", NULL);
716 if (IS_ERR(proc_mnt))
717 goto out;
719 sb->s_blocksize = 1024;
720 sb->s_blocksize_bits = 10;
721 sb->s_magic = HPPFS_SUPER_MAGIC;
722 sb->s_op = &hppfs_sbops;
723 sb->s_fs_info = proc_mnt;
725 err = -ENOMEM;
726 root_inode = get_inode(sb, proc_mnt->mnt_sb->s_root);
727 if (!root_inode)
728 goto out_mntput;
730 sb->s_root = d_alloc_root(root_inode);
731 if (!sb->s_root)
732 goto out_iput;
734 return 0;
736 out_iput:
737 iput(root_inode);
738 out_mntput:
739 mntput(proc_mnt);
740 out:
741 return(err);
744 static int hppfs_read_super(struct file_system_type *type,
745 int flags, const char *dev_name,
746 void *data, struct vfsmount *mnt)
748 return get_sb_nodev(type, flags, data, hppfs_fill_super, mnt);
751 static struct file_system_type hppfs_type = {
752 .owner = THIS_MODULE,
753 .name = "hppfs",
754 .get_sb = hppfs_read_super,
755 .kill_sb = kill_anon_super,
756 .fs_flags = 0,
759 static int __init init_hppfs(void)
761 return register_filesystem(&hppfs_type);
764 static void __exit exit_hppfs(void)
766 unregister_filesystem(&hppfs_type);
769 module_init(init_hppfs)
770 module_exit(exit_hppfs)
771 MODULE_LICENSE("GPL");