blob: 8601d8ef3b550d1c803c3a5bb7cd37f831717fd2 [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
Al Virof382d6e2008-02-23 04:53:53 -050021static struct inode *get_inode(struct super_block *, struct dentry *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23struct hppfs_data {
24 struct list_head list;
25 char contents[PAGE_SIZE - sizeof(struct list_head)];
26};
27
28struct hppfs_private {
29 struct file *proc_file;
30 int host_fd;
31 loff_t len;
32 struct hppfs_data *contents;
33};
34
35struct hppfs_inode_info {
36 struct dentry *proc_dentry;
37 struct inode vfs_inode;
38};
39
40static inline struct hppfs_inode_info *HPPFS_I(struct inode *inode)
41{
Paolo 'Blaisorblade' Giarrussofd589e02005-08-26 16:57:53 +020042 return container_of(inode, struct hppfs_inode_info, vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043}
44
45#define HPPFS_SUPER_MAGIC 0xb00000ee
46
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -080047static const struct super_operations hppfs_sbops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49static int is_pid(struct dentry *dentry)
50{
51 struct super_block *sb;
52 int i;
53
54 sb = dentry->d_sb;
Dave Hansen1dd0dd12008-02-15 18:37:00 -050055 if ((sb->s_op != &hppfs_sbops) || (dentry->d_parent != sb->s_root))
56 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Dave Hansen1dd0dd12008-02-15 18:37:00 -050058 for (i = 0; i < dentry->d_name.len; i++) {
59 if (!isdigit(dentry->d_name.name[i]))
60 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 }
Dave Hansen1dd0dd12008-02-15 18:37:00 -050062 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063}
64
65static char *dentry_name(struct dentry *dentry, int extra)
66{
67 struct dentry *parent;
68 char *root, *name;
69 const char *seg_name;
70 int len, seg_len;
71
72 len = 0;
73 parent = dentry;
Dave Hansen1dd0dd12008-02-15 18:37:00 -050074 while (parent->d_parent != parent) {
75 if (is_pid(parent))
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 len += strlen("pid") + 1;
77 else len += parent->d_name.len + 1;
78 parent = parent->d_parent;
79 }
80
81 root = "proc";
82 len += strlen(root);
83 name = kmalloc(len + extra + 1, GFP_KERNEL);
Dave Hansen1dd0dd12008-02-15 18:37:00 -050084 if (name == NULL)
85 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 name[len] = '\0';
88 parent = dentry;
Dave Hansen1dd0dd12008-02-15 18:37:00 -050089 while (parent->d_parent != parent) {
90 if (is_pid(parent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 seg_name = "pid";
92 seg_len = strlen("pid");
93 }
94 else {
95 seg_name = parent->d_name.name;
96 seg_len = parent->d_name.len;
97 }
98
99 len -= seg_len + 1;
100 name[len] = '/';
101 strncpy(&name[len + 1], seg_name, seg_len);
102 parent = parent->d_parent;
103 }
104 strncpy(name, root, strlen(root));
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500105 return name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108static int file_removed(struct dentry *dentry, const char *file)
109{
110 char *host_file;
111 int extra, fd;
112
113 extra = 0;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500114 if (file != NULL)
115 extra += strlen(file) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117 host_file = dentry_name(dentry, extra + strlen("/remove"));
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500118 if (host_file == NULL) {
119 printk(KERN_ERR "file_removed : allocation failed\n");
120 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
122
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500123 if (file != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 strcat(host_file, "/");
125 strcat(host_file, file);
126 }
127 strcat(host_file, "/remove");
128
129 fd = os_open_file(host_file, of_read(OPENFLAGS()), 0);
130 kfree(host_file);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500131 if (fd > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 os_close_file(fd);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500133 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500135 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138static struct dentry *hppfs_lookup(struct inode *ino, struct dentry *dentry,
139 struct nameidata *nd)
140{
141 struct dentry *proc_dentry, *new, *parent;
142 struct inode *inode;
143 int err, deleted;
144
145 deleted = file_removed(dentry, NULL);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500146 if (deleted < 0)
147 return ERR_PTR(deleted);
148 else if (deleted)
149 return ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 err = -ENOMEM;
152 parent = HPPFS_I(ino)->proc_dentry;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800153 mutex_lock(&parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 proc_dentry = d_lookup(parent, &dentry->d_name);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500155 if (proc_dentry == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 proc_dentry = d_alloc(parent, &dentry->d_name);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500157 if (proc_dentry == NULL) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800158 mutex_unlock(&parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 goto out;
160 }
161 new = (*parent->d_inode->i_op->lookup)(parent->d_inode,
162 proc_dentry, NULL);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500163 if (new) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 dput(proc_dentry);
165 proc_dentry = new;
166 }
167 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800168 mutex_unlock(&parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500170 if (IS_ERR(proc_dentry))
171 return proc_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Al Virof382d6e2008-02-23 04:53:53 -0500173 err = -ENOMEM;
174 inode = get_inode(ino->i_sb, proc_dentry);
175 if (!inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 goto out_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 d_add(dentry, inode);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500179 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 out_dput:
182 dput(proc_dentry);
183 out:
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500184 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800187static const struct inode_operations hppfs_file_iops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188};
189
Al Viro347f2172006-03-31 02:30:16 -0800190static ssize_t read_proc(struct file *file, char __user *buf, ssize_t count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 loff_t *ppos, int is_user)
192{
Al Viro347f2172006-03-31 02:30:16 -0800193 ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 ssize_t n;
195
Josef Sipek471b17e2006-12-08 02:37:07 -0800196 read = file->f_path.dentry->d_inode->i_fop->read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500198 if (!is_user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 set_fs(KERNEL_DS);
200
201 n = (*read)(file, buf, count, &file->f_pos);
202
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500203 if (!is_user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 set_fs(USER_DS);
205
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500206 if (ppos)
207 *ppos = file->f_pos;
Paolo 'Blaisorblade' Giarrusso8e0a2182005-07-14 00:33:41 -0700208 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
Al Viro347f2172006-03-31 02:30:16 -0800211static ssize_t hppfs_read_file(int fd, char __user *buf, ssize_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 ssize_t n;
214 int cur, err;
215 char *new_buf;
216
217 n = -ENOMEM;
218 new_buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500219 if (new_buf == NULL) {
220 printk(KERN_ERR "hppfs_read_file : kmalloc failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 goto out;
222 }
223 n = 0;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500224 while (count > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 cur = min_t(ssize_t, count, PAGE_SIZE);
226 err = os_read_file(fd, new_buf, cur);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500227 if (err < 0) {
228 printk(KERN_ERR "hppfs_read : read failed, "
229 "errno = %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 n = err;
231 goto out_free;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500232 } else if (err == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 break;
234
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500235 if (copy_to_user(buf, new_buf, err)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 n = -EFAULT;
237 goto out_free;
238 }
239 n += err;
240 count -= err;
241 }
242 out_free:
243 kfree(new_buf);
244 out:
Paolo 'Blaisorblade' Giarrusso8e0a2182005-07-14 00:33:41 -0700245 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
Al Viro347f2172006-03-31 02:30:16 -0800248static ssize_t hppfs_read(struct file *file, char __user *buf, size_t count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 loff_t *ppos)
250{
251 struct hppfs_private *hppfs = file->private_data;
252 struct hppfs_data *data;
253 loff_t off;
254 int err;
255
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500256 if (hppfs->contents != NULL) {
257 if (*ppos >= hppfs->len)
258 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 data = hppfs->contents;
261 off = *ppos;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500262 while (off >= sizeof(data->contents)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 data = list_entry(data->list.next, struct hppfs_data,
264 list);
265 off -= sizeof(data->contents);
266 }
267
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500268 if (off + count > hppfs->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 count = hppfs->len - off;
270 copy_to_user(buf, &data->contents[off], count);
271 *ppos += count;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500272 } else if (hppfs->host_fd != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 err = os_seek_file(hppfs->host_fd, *ppos);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500274 if (err) {
275 printk(KERN_ERR "hppfs_read : seek failed, "
276 "errno = %d\n", err);
277 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 }
279 count = hppfs_read_file(hppfs->host_fd, buf, count);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500280 if (count > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 *ppos += count;
282 }
283 else count = read_proc(hppfs->proc_file, buf, count, ppos, 1);
284
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500285 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
Al Viro347f2172006-03-31 02:30:16 -0800288static ssize_t hppfs_write(struct file *file, const char __user *buf, size_t len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 loff_t *ppos)
290{
291 struct hppfs_private *data = file->private_data;
292 struct file *proc_file = data->proc_file;
Al Viro347f2172006-03-31 02:30:16 -0800293 ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 int err;
295
Josef Sipek471b17e2006-12-08 02:37:07 -0800296 write = proc_file->f_path.dentry->d_inode->i_fop->write;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 proc_file->f_pos = file->f_pos;
299 err = (*write)(proc_file, buf, len, &proc_file->f_pos);
300 file->f_pos = proc_file->f_pos;
301
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500302 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303}
304
305static int open_host_sock(char *host_file, int *filter_out)
306{
307 char *end;
308 int fd;
309
310 end = &host_file[strlen(host_file)];
311 strcpy(end, "/rw");
312 *filter_out = 1;
313 fd = os_connect_socket(host_file);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500314 if (fd > 0)
315 return fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 strcpy(end, "/r");
318 *filter_out = 0;
319 fd = os_connect_socket(host_file);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500320 return fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
323static void free_contents(struct hppfs_data *head)
324{
325 struct hppfs_data *data;
326 struct list_head *ele, *next;
327
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500328 if (head == NULL)
329 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500331 list_for_each_safe(ele, next, &head->list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 data = list_entry(ele, struct hppfs_data, list);
333 kfree(data);
334 }
335 kfree(head);
336}
337
338static struct hppfs_data *hppfs_get_data(int fd, int filter,
339 struct file *proc_file,
340 struct file *hppfs_file,
341 loff_t *size_out)
342{
343 struct hppfs_data *data, *new, *head;
344 int n, err;
345
346 err = -ENOMEM;
347 data = kmalloc(sizeof(*data), GFP_KERNEL);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500348 if (data == NULL) {
349 printk(KERN_ERR "hppfs_get_data : head allocation failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 goto failed;
351 }
352
353 INIT_LIST_HEAD(&data->list);
354
355 head = data;
356 *size_out = 0;
357
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500358 if (filter) {
359 while ((n = read_proc(proc_file, data->contents,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 sizeof(data->contents), NULL, 0)) > 0)
361 os_write_file(fd, data->contents, n);
362 err = os_shutdown_socket(fd, 0, 1);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500363 if (err) {
364 printk(KERN_ERR "hppfs_get_data : failed to shut down "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 "socket\n");
366 goto failed_free;
367 }
368 }
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500369 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 n = os_read_file(fd, data->contents, sizeof(data->contents));
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500371 if (n < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 err = n;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500373 printk(KERN_ERR "hppfs_get_data : read failed, "
374 "errno = %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 goto failed_free;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500376 } else if (n == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 break;
378
379 *size_out += n;
380
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500381 if (n < sizeof(data->contents))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 break;
383
384 new = kmalloc(sizeof(*data), GFP_KERNEL);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500385 if (new == 0) {
386 printk(KERN_ERR "hppfs_get_data : data allocation "
387 "failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 err = -ENOMEM;
389 goto failed_free;
390 }
391
392 INIT_LIST_HEAD(&new->list);
393 list_add(&new->list, &data->list);
394 data = new;
395 }
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500396 return head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 failed_free:
399 free_contents(head);
400 failed:
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500401 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
404static struct hppfs_private *hppfs_data(void)
405{
406 struct hppfs_private *data;
407
408 data = kmalloc(sizeof(*data), GFP_KERNEL);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500409 if (data == NULL)
410 return data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 *data = ((struct hppfs_private ) { .host_fd = -1,
413 .len = -1,
414 .contents = NULL } );
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500415 return data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
417
418static int file_mode(int fmode)
419{
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500420 if (fmode == (FMODE_READ | FMODE_WRITE))
421 return O_RDWR;
422 if (fmode == FMODE_READ)
423 return O_RDONLY;
424 if (fmode == FMODE_WRITE)
425 return O_WRONLY;
426 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
429static int hppfs_open(struct inode *inode, struct file *file)
430{
431 struct hppfs_private *data;
432 struct dentry *proc_dentry;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500433 struct vfsmount *proc_mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 char *host_file;
435 int err, fd, type, filter;
436
437 err = -ENOMEM;
438 data = hppfs_data();
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500439 if (data == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 goto out;
441
Josef Sipek471b17e2006-12-08 02:37:07 -0800442 host_file = dentry_name(file->f_path.dentry, strlen("/rw"));
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500443 if (host_file == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 goto out_free2;
445
446 proc_dentry = HPPFS_I(inode)->proc_dentry;
Al Virof382d6e2008-02-23 04:53:53 -0500447 proc_mnt = inode->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 /* XXX This isn't closed anywhere */
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500450 data->proc_file = dentry_open(dget(proc_dentry), mntget(proc_mnt),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 file_mode(file->f_mode));
452 err = PTR_ERR(data->proc_file);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500453 if (IS_ERR(data->proc_file))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 goto out_free1;
455
456 type = os_file_type(host_file);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500457 if (type == OS_TYPE_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 fd = os_open_file(host_file, of_read(OPENFLAGS()), 0);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500459 if (fd >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 data->host_fd = fd;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500461 else
462 printk(KERN_ERR "hppfs_open : failed to open '%s', "
463 "errno = %d\n", host_file, -fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 data->contents = NULL;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500466 } else if (type == OS_TYPE_DIR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 fd = open_host_sock(host_file, &filter);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500468 if (fd > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 data->contents = hppfs_get_data(fd, filter,
Paolo 'Blaisorblade' Giarrusso3f580472005-07-07 17:56:51 -0700470 data->proc_file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 file, &data->len);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500472 if (!IS_ERR(data->contents))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 data->host_fd = fd;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500474 } else
475 printk(KERN_ERR "hppfs_open : failed to open a socket "
476 "in '%s', errno = %d\n", host_file, -fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
478 kfree(host_file);
479
480 file->private_data = data;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500481 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 out_free1:
484 kfree(host_file);
485 out_free2:
486 free_contents(data->contents);
487 kfree(data);
488 out:
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500489 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
492static int hppfs_dir_open(struct inode *inode, struct file *file)
493{
494 struct hppfs_private *data;
495 struct dentry *proc_dentry;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500496 struct vfsmount *proc_mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 int err;
498
499 err = -ENOMEM;
500 data = hppfs_data();
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500501 if (data == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 goto out;
503
504 proc_dentry = HPPFS_I(inode)->proc_dentry;
Al Virof382d6e2008-02-23 04:53:53 -0500505 proc_mnt = inode->i_sb->s_fs_info;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500506 data->proc_file = dentry_open(dget(proc_dentry), mntget(proc_mnt),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 file_mode(file->f_mode));
508 err = PTR_ERR(data->proc_file);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500509 if (IS_ERR(data->proc_file))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 goto out_free;
511
512 file->private_data = data;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500513 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 out_free:
516 kfree(data);
517 out:
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500518 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519}
520
521static loff_t hppfs_llseek(struct file *file, loff_t off, int where)
522{
523 struct hppfs_private *data = file->private_data;
Paolo 'Blaisorblade' Giarrusso3f580472005-07-07 17:56:51 -0700524 struct file *proc_file = data->proc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 loff_t (*llseek)(struct file *, loff_t, int);
526 loff_t ret;
527
Josef Sipek471b17e2006-12-08 02:37:07 -0800528 llseek = proc_file->f_path.dentry->d_inode->i_fop->llseek;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500529 if (llseek != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 ret = (*llseek)(proc_file, off, where);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500531 if (ret < 0)
532 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
534
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500535 return default_llseek(file, off, where);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
537
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800538static const struct file_operations hppfs_file_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 .owner = NULL,
540 .llseek = hppfs_llseek,
541 .read = hppfs_read,
542 .write = hppfs_write,
543 .open = hppfs_open,
544};
545
546struct hppfs_dirent {
547 void *vfs_dirent;
548 filldir_t filldir;
549 struct dentry *dentry;
550};
551
552static int hppfs_filldir(void *d, const char *name, int size,
Al Viroc8adf942006-10-09 20:26:58 +0100553 loff_t offset, u64 inode, unsigned int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
555 struct hppfs_dirent *dirent = d;
556
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500557 if (file_removed(dirent->dentry, name))
558 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500560 return (*dirent->filldir)(dirent->vfs_dirent, name, size, offset,
561 inode, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
564static int hppfs_readdir(struct file *file, void *ent, filldir_t filldir)
565{
566 struct hppfs_private *data = file->private_data;
Paolo 'Blaisorblade' Giarrusso3f580472005-07-07 17:56:51 -0700567 struct file *proc_file = data->proc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 int (*readdir)(struct file *, void *, filldir_t);
569 struct hppfs_dirent dirent = ((struct hppfs_dirent)
570 { .vfs_dirent = ent,
571 .filldir = filldir,
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500572 .dentry = file->f_path.dentry
573 });
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 int err;
575
Josef Sipek471b17e2006-12-08 02:37:07 -0800576 readdir = proc_file->f_path.dentry->d_inode->i_fop->readdir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 proc_file->f_pos = file->f_pos;
579 err = (*readdir)(proc_file, &dirent, hppfs_filldir);
580 file->f_pos = proc_file->f_pos;
581
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500582 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
585static int hppfs_fsync(struct file *file, struct dentry *dentry, int datasync)
586{
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500587 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800590static const struct file_operations hppfs_dir_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 .owner = NULL,
592 .readdir = hppfs_readdir,
593 .open = hppfs_dir_open,
594 .fsync = hppfs_fsync,
595};
596
David Howells726c3342006-06-23 02:02:58 -0700597static int hppfs_statfs(struct dentry *dentry, struct kstatfs *sf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
599 sf->f_blocks = 0;
600 sf->f_bfree = 0;
601 sf->f_bavail = 0;
602 sf->f_files = 0;
603 sf->f_ffree = 0;
604 sf->f_type = HPPFS_SUPER_MAGIC;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500605 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
608static struct inode *hppfs_alloc_inode(struct super_block *sb)
609{
610 struct hppfs_inode_info *hi;
611
612 hi = kmalloc(sizeof(*hi), GFP_KERNEL);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500613 if (!hi)
614 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500616 hi->proc_dentry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 inode_init_once(&hi->vfs_inode);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500618 return &hi->vfs_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
621void hppfs_delete_inode(struct inode *ino)
622{
623 clear_inode(ino);
624}
625
626static void hppfs_destroy_inode(struct inode *inode)
627{
628 kfree(HPPFS_I(inode));
629}
630
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500631static void hppfs_put_super(struct super_block *sb)
632{
Al Virof382d6e2008-02-23 04:53:53 -0500633 mntput(sb->s_fs_info);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500634}
635
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800636static const struct super_operations hppfs_sbops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 .alloc_inode = hppfs_alloc_inode,
638 .destroy_inode = hppfs_destroy_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 .delete_inode = hppfs_delete_inode,
640 .statfs = hppfs_statfs,
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500641 .put_super = hppfs_put_super,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642};
643
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500644static int hppfs_readlink(struct dentry *dentry, char __user *buffer,
645 int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
647 struct file *proc_file;
648 struct dentry *proc_dentry;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500649 struct vfsmount *proc_mnt;
Paolo 'Blaisorblade' Giarrussofd589e02005-08-26 16:57:53 +0200650 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 proc_dentry = HPPFS_I(dentry->d_inode)->proc_dentry;
Al Virof382d6e2008-02-23 04:53:53 -0500653 proc_mnt = dentry->d_sb->s_fs_info;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500654
655 proc_file = dentry_open(dget(proc_dentry), mntget(proc_mnt), O_RDONLY);
Paolo 'Blaisorblade' Giarrussofd589e02005-08-26 16:57:53 +0200656 if (IS_ERR(proc_file))
657 return PTR_ERR(proc_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Paolo 'Blaisorblade' Giarrussofd589e02005-08-26 16:57:53 +0200659 ret = proc_dentry->d_inode->i_op->readlink(proc_dentry, buffer, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 fput(proc_file);
662
Paolo 'Blaisorblade' Giarrussofd589e02005-08-26 16:57:53 +0200663 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
665
Paolo 'Blaisorblade' Giarrussod7a60d52005-08-26 16:57:44 +0200666static void* hppfs_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
668 struct file *proc_file;
669 struct dentry *proc_dentry;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500670 struct vfsmount *proc_mnt;
Paolo 'Blaisorblade' Giarrussod7a60d52005-08-26 16:57:44 +0200671 void *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 proc_dentry = HPPFS_I(dentry->d_inode)->proc_dentry;
Al Virof382d6e2008-02-23 04:53:53 -0500674 proc_mnt = dentry->d_sb->s_fs_info;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500675
676 proc_file = dentry_open(dget(proc_dentry), mntget(proc_mnt), O_RDONLY);
Paolo 'Blaisorblade' Giarrussofd589e02005-08-26 16:57:53 +0200677 if (IS_ERR(proc_file))
678 return proc_file;
Paolo 'Blaisorblade' Giarrussod7a60d52005-08-26 16:57:44 +0200679
Paolo 'Blaisorblade' Giarrussofd589e02005-08-26 16:57:53 +0200680 ret = proc_dentry->d_inode->i_op->follow_link(proc_dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 fput(proc_file);
683
Paolo 'Blaisorblade' Giarrussod7a60d52005-08-26 16:57:44 +0200684 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800687static const struct inode_operations hppfs_dir_iops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 .lookup = hppfs_lookup,
689};
690
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800691static const struct inode_operations hppfs_link_iops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 .readlink = hppfs_readlink,
693 .follow_link = hppfs_follow_link,
694};
695
Al Virof382d6e2008-02-23 04:53:53 -0500696static struct inode *get_inode(struct super_block *sb, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
Al Virof382d6e2008-02-23 04:53:53 -0500698 struct inode *proc_ino = dentry->d_inode;
699 struct inode *inode = new_inode(sb);
700
701 if (!inode)
702 return ERR_PTR(-ENOMEM);
703
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500704 if (S_ISDIR(dentry->d_inode->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 inode->i_op = &hppfs_dir_iops;
706 inode->i_fop = &hppfs_dir_fops;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500707 } else if (S_ISLNK(dentry->d_inode->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 inode->i_op = &hppfs_link_iops;
709 inode->i_fop = &hppfs_file_fops;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500710 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 inode->i_op = &hppfs_file_iops;
712 inode->i_fop = &hppfs_file_fops;
713 }
714
715 HPPFS_I(inode)->proc_dentry = dentry;
Al Virof382d6e2008-02-23 04:53:53 -0500716
717 inode->i_uid = proc_ino->i_uid;
718 inode->i_gid = proc_ino->i_gid;
719 inode->i_atime = proc_ino->i_atime;
720 inode->i_mtime = proc_ino->i_mtime;
721 inode->i_ctime = proc_ino->i_ctime;
722 inode->i_ino = proc_ino->i_ino;
723 inode->i_mode = proc_ino->i_mode;
724 inode->i_nlink = proc_ino->i_nlink;
725 inode->i_size = proc_ino->i_size;
726 inode->i_blocks = proc_ino->i_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500728 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729}
730
731static int hppfs_fill_super(struct super_block *sb, void *d, int silent)
732{
733 struct inode *root_inode;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500734 struct vfsmount *proc_mnt;
Al Virof382d6e2008-02-23 04:53:53 -0500735 int err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Al Virof382d6e2008-02-23 04:53:53 -0500737 proc_mnt = do_kern_mount("proc", 0, "proc", NULL);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500738 if (IS_ERR(proc_mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 goto out;
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 sb->s_blocksize = 1024;
742 sb->s_blocksize_bits = 10;
743 sb->s_magic = HPPFS_SUPER_MAGIC;
744 sb->s_op = &hppfs_sbops;
Al Virof382d6e2008-02-23 04:53:53 -0500745 sb->s_fs_info = proc_mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 err = -ENOMEM;
Al Virof382d6e2008-02-23 04:53:53 -0500748 root_inode = get_inode(sb, proc_mnt->mnt_sb->s_root);
749 if (!root_inode)
750 goto out_mntput;
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 sb->s_root = d_alloc_root(root_inode);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500753 if (!sb->s_root)
754 goto out_iput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500756 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500758 out_iput:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 iput(root_inode);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500760 out_mntput:
761 mntput(proc_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 out:
763 return(err);
764}
765
David Howells454e2392006-06-23 02:02:57 -0700766static int hppfs_read_super(struct file_system_type *type,
767 int flags, const char *dev_name,
768 void *data, struct vfsmount *mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
David Howells454e2392006-06-23 02:02:57 -0700770 return get_sb_nodev(type, flags, data, hppfs_fill_super, mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
772
773static struct file_system_type hppfs_type = {
774 .owner = THIS_MODULE,
775 .name = "hppfs",
776 .get_sb = hppfs_read_super,
777 .kill_sb = kill_anon_super,
778 .fs_flags = 0,
779};
780
781static int __init init_hppfs(void)
782{
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500783 return register_filesystem(&hppfs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
785
786static void __exit exit_hppfs(void)
787{
788 unregister_filesystem(&hppfs_type);
789}
790
791module_init(init_hppfs)
792module_exit(exit_hppfs)
793MODULE_LICENSE("GPL");