blob: 0c0f62f5164fabd7579789a726efb28e7b2ea515 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Dave Hansen1dd0dd12008-02-15 18:37:00 -05002 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Licensed under the GPL
4 */
5
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include <linux/ctype.h>
7#include <linux/dcache.h>
Dave Hansen1dd0dd12008-02-15 18:37:00 -05008#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/statfs.h>
Dave Hansen1dd0dd12008-02-15 18:37:00 -050017#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "os.h"
20
Dave Hansen1dd0dd12008-02-15 18:37:00 -050021static int init_inode(struct inode *inode, struct dentry *dentry,
22 struct vfsmount *mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24struct hppfs_data {
25 struct list_head list;
26 char contents[PAGE_SIZE - sizeof(struct list_head)];
27};
28
29struct hppfs_private {
30 struct file *proc_file;
31 int host_fd;
32 loff_t len;
33 struct hppfs_data *contents;
34};
35
36struct hppfs_inode_info {
37 struct dentry *proc_dentry;
Dave Hansen1dd0dd12008-02-15 18:37:00 -050038 struct vfsmount *proc_mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 struct inode vfs_inode;
40};
41
42static inline struct hppfs_inode_info *HPPFS_I(struct inode *inode)
43{
Paolo 'Blaisorblade' Giarrussofd589e02005-08-26 16:57:53 +020044 return container_of(inode, struct hppfs_inode_info, vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045}
46
47#define HPPFS_SUPER_MAGIC 0xb00000ee
48
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -080049static const struct super_operations hppfs_sbops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51static int is_pid(struct dentry *dentry)
52{
53 struct super_block *sb;
54 int i;
55
56 sb = dentry->d_sb;
Dave Hansen1dd0dd12008-02-15 18:37:00 -050057 if ((sb->s_op != &hppfs_sbops) || (dentry->d_parent != sb->s_root))
58 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Dave Hansen1dd0dd12008-02-15 18:37:00 -050060 for (i = 0; i < dentry->d_name.len; i++) {
61 if (!isdigit(dentry->d_name.name[i]))
62 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 }
Dave Hansen1dd0dd12008-02-15 18:37:00 -050064 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
66
67static char *dentry_name(struct dentry *dentry, int extra)
68{
69 struct dentry *parent;
70 char *root, *name;
71 const char *seg_name;
72 int len, seg_len;
73
74 len = 0;
75 parent = dentry;
Dave Hansen1dd0dd12008-02-15 18:37:00 -050076 while (parent->d_parent != parent) {
77 if (is_pid(parent))
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 len += strlen("pid") + 1;
79 else len += parent->d_name.len + 1;
80 parent = parent->d_parent;
81 }
82
83 root = "proc";
84 len += strlen(root);
85 name = kmalloc(len + extra + 1, GFP_KERNEL);
Dave Hansen1dd0dd12008-02-15 18:37:00 -050086 if (name == NULL)
87 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89 name[len] = '\0';
90 parent = dentry;
Dave Hansen1dd0dd12008-02-15 18:37:00 -050091 while (parent->d_parent != parent) {
92 if (is_pid(parent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 seg_name = "pid";
94 seg_len = strlen("pid");
95 }
96 else {
97 seg_name = parent->d_name.name;
98 seg_len = parent->d_name.len;
99 }
100
101 len -= seg_len + 1;
102 name[len] = '/';
103 strncpy(&name[len + 1], seg_name, seg_len);
104 parent = parent->d_parent;
105 }
106 strncpy(name, root, strlen(root));
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500107 return name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110static int file_removed(struct dentry *dentry, const char *file)
111{
112 char *host_file;
113 int extra, fd;
114
115 extra = 0;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500116 if (file != NULL)
117 extra += strlen(file) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 host_file = dentry_name(dentry, extra + strlen("/remove"));
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500120 if (host_file == NULL) {
121 printk(KERN_ERR "file_removed : allocation failed\n");
122 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 }
124
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500125 if (file != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 strcat(host_file, "/");
127 strcat(host_file, file);
128 }
129 strcat(host_file, "/remove");
130
131 fd = os_open_file(host_file, of_read(OPENFLAGS()), 0);
132 kfree(host_file);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500133 if (fd > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 os_close_file(fd);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500135 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 }
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500137 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
140static void hppfs_read_inode(struct inode *ino)
141{
142 struct inode *proc_ino;
143
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500144 if (HPPFS_I(ino)->proc_dentry == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 return;
146
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 ino->i_blocks = proc_ino->i_blocks;
158}
159
David Howells755aedc2008-02-07 00:15:51 -0800160static struct inode *hppfs_iget(struct super_block *sb)
161{
162 struct inode *inode;
163
164 inode = iget_locked(sb, 0);
165 if (!inode)
166 return ERR_PTR(-ENOMEM);
167 if (inode->i_state & I_NEW) {
168 hppfs_read_inode(inode);
169 unlock_new_inode(inode);
170 }
171 return inode;
172}
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174static struct dentry *hppfs_lookup(struct inode *ino, struct dentry *dentry,
175 struct nameidata *nd)
176{
177 struct dentry *proc_dentry, *new, *parent;
178 struct inode *inode;
179 int err, deleted;
180
181 deleted = file_removed(dentry, NULL);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500182 if (deleted < 0)
183 return ERR_PTR(deleted);
184 else if (deleted)
185 return ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 err = -ENOMEM;
188 parent = HPPFS_I(ino)->proc_dentry;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800189 mutex_lock(&parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 proc_dentry = d_lookup(parent, &dentry->d_name);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500191 if (proc_dentry == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 proc_dentry = d_alloc(parent, &dentry->d_name);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500193 if (proc_dentry == NULL) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800194 mutex_unlock(&parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 goto out;
196 }
197 new = (*parent->d_inode->i_op->lookup)(parent->d_inode,
198 proc_dentry, NULL);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500199 if (new) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 dput(proc_dentry);
201 proc_dentry = new;
202 }
203 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800204 mutex_unlock(&parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500206 if (IS_ERR(proc_dentry))
207 return proc_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
David Howells755aedc2008-02-07 00:15:51 -0800209 inode = hppfs_iget(ino->i_sb);
210 if (IS_ERR(inode)) {
211 err = PTR_ERR(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 goto out_dput;
David Howells755aedc2008-02-07 00:15:51 -0800213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500215 err = init_inode(inode, proc_dentry, HPPFS_I(ino)->proc_mnt);
216 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 goto out_put;
218
219 hppfs_read_inode(inode);
220
221 d_add(dentry, inode);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500222 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 out_put:
225 iput(inode);
226 out_dput:
227 dput(proc_dentry);
228 out:
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500229 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800232static const struct inode_operations hppfs_file_iops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233};
234
Al Viro347f2172006-03-31 02:30:16 -0800235static ssize_t read_proc(struct file *file, char __user *buf, ssize_t count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 loff_t *ppos, int is_user)
237{
Al Viro347f2172006-03-31 02:30:16 -0800238 ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 ssize_t n;
240
Josef Sipek471b17e2006-12-08 02:37:07 -0800241 read = file->f_path.dentry->d_inode->i_fop->read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500243 if (!is_user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 set_fs(KERNEL_DS);
245
246 n = (*read)(file, buf, count, &file->f_pos);
247
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500248 if (!is_user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 set_fs(USER_DS);
250
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500251 if (ppos)
252 *ppos = file->f_pos;
Paolo 'Blaisorblade' Giarrusso8e0a2182005-07-14 00:33:41 -0700253 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
Al Viro347f2172006-03-31 02:30:16 -0800256static ssize_t hppfs_read_file(int fd, char __user *buf, ssize_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
258 ssize_t n;
259 int cur, err;
260 char *new_buf;
261
262 n = -ENOMEM;
263 new_buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500264 if (new_buf == NULL) {
265 printk(KERN_ERR "hppfs_read_file : kmalloc failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 goto out;
267 }
268 n = 0;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500269 while (count > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 cur = min_t(ssize_t, count, PAGE_SIZE);
271 err = os_read_file(fd, new_buf, cur);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500272 if (err < 0) {
273 printk(KERN_ERR "hppfs_read : read failed, "
274 "errno = %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 n = err;
276 goto out_free;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500277 } else if (err == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 break;
279
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500280 if (copy_to_user(buf, new_buf, err)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 n = -EFAULT;
282 goto out_free;
283 }
284 n += err;
285 count -= err;
286 }
287 out_free:
288 kfree(new_buf);
289 out:
Paolo 'Blaisorblade' Giarrusso8e0a2182005-07-14 00:33:41 -0700290 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
Al Viro347f2172006-03-31 02:30:16 -0800293static ssize_t hppfs_read(struct file *file, char __user *buf, size_t count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 loff_t *ppos)
295{
296 struct hppfs_private *hppfs = file->private_data;
297 struct hppfs_data *data;
298 loff_t off;
299 int err;
300
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500301 if (hppfs->contents != NULL) {
302 if (*ppos >= hppfs->len)
303 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 data = hppfs->contents;
306 off = *ppos;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500307 while (off >= sizeof(data->contents)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 data = list_entry(data->list.next, struct hppfs_data,
309 list);
310 off -= sizeof(data->contents);
311 }
312
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500313 if (off + count > hppfs->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 count = hppfs->len - off;
315 copy_to_user(buf, &data->contents[off], count);
316 *ppos += count;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500317 } else if (hppfs->host_fd != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 err = os_seek_file(hppfs->host_fd, *ppos);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500319 if (err) {
320 printk(KERN_ERR "hppfs_read : seek failed, "
321 "errno = %d\n", err);
322 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
324 count = hppfs_read_file(hppfs->host_fd, buf, count);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500325 if (count > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 *ppos += count;
327 }
328 else count = read_proc(hppfs->proc_file, buf, count, ppos, 1);
329
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500330 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
Al Viro347f2172006-03-31 02:30:16 -0800333static ssize_t hppfs_write(struct file *file, const char __user *buf, size_t len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 loff_t *ppos)
335{
336 struct hppfs_private *data = file->private_data;
337 struct file *proc_file = data->proc_file;
Al Viro347f2172006-03-31 02:30:16 -0800338 ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 int err;
340
Josef Sipek471b17e2006-12-08 02:37:07 -0800341 write = proc_file->f_path.dentry->d_inode->i_fop->write;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
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;
346
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500347 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
349
350static int open_host_sock(char *host_file, int *filter_out)
351{
352 char *end;
353 int fd;
354
355 end = &host_file[strlen(host_file)];
356 strcpy(end, "/rw");
357 *filter_out = 1;
358 fd = os_connect_socket(host_file);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500359 if (fd > 0)
360 return fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 strcpy(end, "/r");
363 *filter_out = 0;
364 fd = os_connect_socket(host_file);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500365 return fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367
368static void free_contents(struct hppfs_data *head)
369{
370 struct hppfs_data *data;
371 struct list_head *ele, *next;
372
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500373 if (head == NULL)
374 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500376 list_for_each_safe(ele, next, &head->list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 data = list_entry(ele, struct hppfs_data, list);
378 kfree(data);
379 }
380 kfree(head);
381}
382
383static struct hppfs_data *hppfs_get_data(int fd, int filter,
384 struct file *proc_file,
385 struct file *hppfs_file,
386 loff_t *size_out)
387{
388 struct hppfs_data *data, *new, *head;
389 int n, err;
390
391 err = -ENOMEM;
392 data = kmalloc(sizeof(*data), GFP_KERNEL);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500393 if (data == NULL) {
394 printk(KERN_ERR "hppfs_get_data : head allocation failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 goto failed;
396 }
397
398 INIT_LIST_HEAD(&data->list);
399
400 head = data;
401 *size_out = 0;
402
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500403 if (filter) {
404 while ((n = read_proc(proc_file, data->contents,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 sizeof(data->contents), NULL, 0)) > 0)
406 os_write_file(fd, data->contents, n);
407 err = os_shutdown_socket(fd, 0, 1);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500408 if (err) {
409 printk(KERN_ERR "hppfs_get_data : failed to shut down "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 "socket\n");
411 goto failed_free;
412 }
413 }
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500414 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 n = os_read_file(fd, data->contents, sizeof(data->contents));
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500416 if (n < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 err = n;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500418 printk(KERN_ERR "hppfs_get_data : read failed, "
419 "errno = %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 goto failed_free;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500421 } else if (n == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 break;
423
424 *size_out += n;
425
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500426 if (n < sizeof(data->contents))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 break;
428
429 new = kmalloc(sizeof(*data), GFP_KERNEL);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500430 if (new == 0) {
431 printk(KERN_ERR "hppfs_get_data : data allocation "
432 "failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 err = -ENOMEM;
434 goto failed_free;
435 }
436
437 INIT_LIST_HEAD(&new->list);
438 list_add(&new->list, &data->list);
439 data = new;
440 }
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500441 return head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 failed_free:
444 free_contents(head);
445 failed:
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500446 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
449static struct hppfs_private *hppfs_data(void)
450{
451 struct hppfs_private *data;
452
453 data = kmalloc(sizeof(*data), GFP_KERNEL);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500454 if (data == NULL)
455 return data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457 *data = ((struct hppfs_private ) { .host_fd = -1,
458 .len = -1,
459 .contents = NULL } );
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500460 return data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
463static int file_mode(int fmode)
464{
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500465 if (fmode == (FMODE_READ | FMODE_WRITE))
466 return O_RDWR;
467 if (fmode == FMODE_READ)
468 return O_RDONLY;
469 if (fmode == FMODE_WRITE)
470 return O_WRONLY;
471 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
474static int hppfs_open(struct inode *inode, struct file *file)
475{
476 struct hppfs_private *data;
477 struct dentry *proc_dentry;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500478 struct vfsmount *proc_mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 char *host_file;
480 int err, fd, type, filter;
481
482 err = -ENOMEM;
483 data = hppfs_data();
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500484 if (data == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 goto out;
486
Josef Sipek471b17e2006-12-08 02:37:07 -0800487 host_file = dentry_name(file->f_path.dentry, strlen("/rw"));
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500488 if (host_file == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 goto out_free2;
490
491 proc_dentry = HPPFS_I(inode)->proc_dentry;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500492 proc_mnt = HPPFS_I(inode)->proc_mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 /* XXX This isn't closed anywhere */
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500495 data->proc_file = dentry_open(dget(proc_dentry), mntget(proc_mnt),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 file_mode(file->f_mode));
497 err = PTR_ERR(data->proc_file);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500498 if (IS_ERR(data->proc_file))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 goto out_free1;
500
501 type = os_file_type(host_file);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500502 if (type == OS_TYPE_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 fd = os_open_file(host_file, of_read(OPENFLAGS()), 0);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500504 if (fd >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 data->host_fd = fd;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500506 else
507 printk(KERN_ERR "hppfs_open : failed to open '%s', "
508 "errno = %d\n", host_file, -fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 data->contents = NULL;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500511 } else if (type == OS_TYPE_DIR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 fd = open_host_sock(host_file, &filter);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500513 if (fd > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 data->contents = hppfs_get_data(fd, filter,
Paolo 'Blaisorblade' Giarrusso3f580472005-07-07 17:56:51 -0700515 data->proc_file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 file, &data->len);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500517 if (!IS_ERR(data->contents))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 data->host_fd = fd;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500519 } else
520 printk(KERN_ERR "hppfs_open : failed to open a socket "
521 "in '%s', errno = %d\n", host_file, -fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
523 kfree(host_file);
524
525 file->private_data = data;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500526 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
528 out_free1:
529 kfree(host_file);
530 out_free2:
531 free_contents(data->contents);
532 kfree(data);
533 out:
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500534 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
537static int hppfs_dir_open(struct inode *inode, struct file *file)
538{
539 struct hppfs_private *data;
540 struct dentry *proc_dentry;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500541 struct vfsmount *proc_mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 int err;
543
544 err = -ENOMEM;
545 data = hppfs_data();
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500546 if (data == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 goto out;
548
549 proc_dentry = HPPFS_I(inode)->proc_dentry;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500550 proc_mnt = HPPFS_I(inode)->proc_mnt;
551 data->proc_file = dentry_open(dget(proc_dentry), mntget(proc_mnt),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 file_mode(file->f_mode));
553 err = PTR_ERR(data->proc_file);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500554 if (IS_ERR(data->proc_file))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 goto out_free;
556
557 file->private_data = data;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500558 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 out_free:
561 kfree(data);
562 out:
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500563 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
566static loff_t hppfs_llseek(struct file *file, loff_t off, int where)
567{
568 struct hppfs_private *data = file->private_data;
Paolo 'Blaisorblade' Giarrusso3f580472005-07-07 17:56:51 -0700569 struct file *proc_file = data->proc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 loff_t (*llseek)(struct file *, loff_t, int);
571 loff_t ret;
572
Josef Sipek471b17e2006-12-08 02:37:07 -0800573 llseek = proc_file->f_path.dentry->d_inode->i_fop->llseek;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500574 if (llseek != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 ret = (*llseek)(proc_file, off, where);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500576 if (ret < 0)
577 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
579
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500580 return default_llseek(file, off, where);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
582
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800583static const struct file_operations hppfs_file_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 .owner = NULL,
585 .llseek = hppfs_llseek,
586 .read = hppfs_read,
587 .write = hppfs_write,
588 .open = hppfs_open,
589};
590
591struct hppfs_dirent {
592 void *vfs_dirent;
593 filldir_t filldir;
594 struct dentry *dentry;
595};
596
597static int hppfs_filldir(void *d, const char *name, int size,
Al Viroc8adf942006-10-09 20:26:58 +0100598 loff_t offset, u64 inode, unsigned int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
600 struct hppfs_dirent *dirent = d;
601
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500602 if (file_removed(dirent->dentry, name))
603 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500605 return (*dirent->filldir)(dirent->vfs_dirent, name, size, offset,
606 inode, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
608
609static int hppfs_readdir(struct file *file, void *ent, filldir_t filldir)
610{
611 struct hppfs_private *data = file->private_data;
Paolo 'Blaisorblade' Giarrusso3f580472005-07-07 17:56:51 -0700612 struct file *proc_file = data->proc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 int (*readdir)(struct file *, void *, filldir_t);
614 struct hppfs_dirent dirent = ((struct hppfs_dirent)
615 { .vfs_dirent = ent,
616 .filldir = filldir,
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500617 .dentry = file->f_path.dentry
618 });
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 int err;
620
Josef Sipek471b17e2006-12-08 02:37:07 -0800621 readdir = proc_file->f_path.dentry->d_inode->i_fop->readdir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 proc_file->f_pos = file->f_pos;
624 err = (*readdir)(proc_file, &dirent, hppfs_filldir);
625 file->f_pos = proc_file->f_pos;
626
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500627 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
629
630static int hppfs_fsync(struct file *file, struct dentry *dentry, int datasync)
631{
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500632 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800635static const struct file_operations hppfs_dir_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 .owner = NULL,
637 .readdir = hppfs_readdir,
638 .open = hppfs_dir_open,
639 .fsync = hppfs_fsync,
640};
641
David Howells726c3342006-06-23 02:02:58 -0700642static int hppfs_statfs(struct dentry *dentry, struct kstatfs *sf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
644 sf->f_blocks = 0;
645 sf->f_bfree = 0;
646 sf->f_bavail = 0;
647 sf->f_files = 0;
648 sf->f_ffree = 0;
649 sf->f_type = HPPFS_SUPER_MAGIC;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500650 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651}
652
653static struct inode *hppfs_alloc_inode(struct super_block *sb)
654{
655 struct hppfs_inode_info *hi;
656
657 hi = kmalloc(sizeof(*hi), GFP_KERNEL);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500658 if (!hi)
659 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500661 hi->proc_dentry = NULL;
662 hi->proc_mnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 inode_init_once(&hi->vfs_inode);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500664 return &hi->vfs_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
667void hppfs_delete_inode(struct inode *ino)
668{
669 clear_inode(ino);
670}
671
672static void hppfs_destroy_inode(struct inode *inode)
673{
674 kfree(HPPFS_I(inode));
675}
676
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500677static void hppfs_put_super(struct super_block *sb)
678{
679 mntput(HPPFS_I(sb->s_root->d_inode)->proc_mnt);
680}
681
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800682static const struct super_operations hppfs_sbops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 .alloc_inode = hppfs_alloc_inode,
684 .destroy_inode = hppfs_destroy_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 .delete_inode = hppfs_delete_inode,
686 .statfs = hppfs_statfs,
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500687 .put_super = hppfs_put_super,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688};
689
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500690static int hppfs_readlink(struct dentry *dentry, char __user *buffer,
691 int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
693 struct file *proc_file;
694 struct dentry *proc_dentry;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500695 struct vfsmount *proc_mnt;
Paolo 'Blaisorblade' Giarrussofd589e02005-08-26 16:57:53 +0200696 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 proc_dentry = HPPFS_I(dentry->d_inode)->proc_dentry;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500699 proc_mnt = HPPFS_I(dentry->d_inode)->proc_mnt;
700
701 proc_file = dentry_open(dget(proc_dentry), mntget(proc_mnt), O_RDONLY);
Paolo 'Blaisorblade' Giarrussofd589e02005-08-26 16:57:53 +0200702 if (IS_ERR(proc_file))
703 return PTR_ERR(proc_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Paolo 'Blaisorblade' Giarrussofd589e02005-08-26 16:57:53 +0200705 ret = proc_dentry->d_inode->i_op->readlink(proc_dentry, buffer, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 fput(proc_file);
708
Paolo 'Blaisorblade' Giarrussofd589e02005-08-26 16:57:53 +0200709 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
Paolo 'Blaisorblade' Giarrussod7a60d52005-08-26 16:57:44 +0200712static void* hppfs_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
714 struct file *proc_file;
715 struct dentry *proc_dentry;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500716 struct vfsmount *proc_mnt;
Paolo 'Blaisorblade' Giarrussod7a60d52005-08-26 16:57:44 +0200717 void *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 proc_dentry = HPPFS_I(dentry->d_inode)->proc_dentry;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500720 proc_mnt = HPPFS_I(dentry->d_inode)->proc_mnt;
721
722 proc_file = dentry_open(dget(proc_dentry), mntget(proc_mnt), O_RDONLY);
Paolo 'Blaisorblade' Giarrussofd589e02005-08-26 16:57:53 +0200723 if (IS_ERR(proc_file))
724 return proc_file;
Paolo 'Blaisorblade' Giarrussod7a60d52005-08-26 16:57:44 +0200725
Paolo 'Blaisorblade' Giarrussofd589e02005-08-26 16:57:53 +0200726 ret = proc_dentry->d_inode->i_op->follow_link(proc_dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 fput(proc_file);
729
Paolo 'Blaisorblade' Giarrussod7a60d52005-08-26 16:57:44 +0200730 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800733static const struct inode_operations hppfs_dir_iops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 .lookup = hppfs_lookup,
735};
736
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800737static const struct inode_operations hppfs_link_iops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 .readlink = hppfs_readlink,
739 .follow_link = hppfs_follow_link,
740};
741
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500742static int init_inode(struct inode *inode, struct dentry *dentry,
743 struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500745 if (S_ISDIR(dentry->d_inode->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 inode->i_op = &hppfs_dir_iops;
747 inode->i_fop = &hppfs_dir_fops;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500748 } else if (S_ISLNK(dentry->d_inode->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 inode->i_op = &hppfs_link_iops;
750 inode->i_fop = &hppfs_file_fops;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500751 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 inode->i_op = &hppfs_file_iops;
753 inode->i_fop = &hppfs_file_fops;
754 }
755
756 HPPFS_I(inode)->proc_dentry = dentry;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500757 HPPFS_I(inode)->proc_mnt = mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500759 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
762static int hppfs_fill_super(struct super_block *sb, void *d, int silent)
763{
764 struct inode *root_inode;
765 struct file_system_type *procfs;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500766 struct vfsmount *proc_mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 int err;
768
769 err = -ENOENT;
770 procfs = get_fs_type("proc");
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500771 if (!procfs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 goto out;
773
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500774 proc_mnt = vfs_kern_mount(procfs, 0, procfs->name, NULL);
775 put_filesystem(procfs);
776 if (IS_ERR(proc_mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 goto out;
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 sb->s_blocksize = 1024;
780 sb->s_blocksize_bits = 10;
781 sb->s_magic = HPPFS_SUPER_MAGIC;
782 sb->s_op = &hppfs_sbops;
783
David Howells755aedc2008-02-07 00:15:51 -0800784 root_inode = hppfs_iget(sb);
785 if (IS_ERR(root_inode)) {
786 err = PTR_ERR(root_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 goto out;
David Howells755aedc2008-02-07 00:15:51 -0800788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500790 err = init_inode(root_inode, proc_mnt->mnt_sb->s_root, proc_mnt);
791 if (err)
792 goto out_iput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 err = -ENOMEM;
795 sb->s_root = d_alloc_root(root_inode);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500796 if (!sb->s_root)
797 goto out_iput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799 hppfs_read_inode(root_inode);
800
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500801 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500803 out_iput:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 iput(root_inode);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500805 out_mntput:
806 mntput(proc_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 out:
808 return(err);
809}
810
David Howells454e2392006-06-23 02:02:57 -0700811static int hppfs_read_super(struct file_system_type *type,
812 int flags, const char *dev_name,
813 void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
David Howells454e2392006-06-23 02:02:57 -0700815 return get_sb_nodev(type, flags, data, hppfs_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816}
817
818static struct file_system_type hppfs_type = {
819 .owner = THIS_MODULE,
820 .name = "hppfs",
821 .get_sb = hppfs_read_super,
822 .kill_sb = kill_anon_super,
823 .fs_flags = 0,
824};
825
826static int __init init_hppfs(void)
827{
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500828 return register_filesystem(&hppfs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829}
830
831static void __exit exit_hppfs(void)
832{
833 unregister_filesystem(&hppfs_type);
834}
835
836module_init(init_hppfs)
837module_exit(exit_hppfs)
838MODULE_LICENSE("GPL");