blob: 7d6a0e92bcf0931da896a69cdbbf00a3ade2b6ed [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>
Al Viro918377b2010-06-06 23:56:02 -040018#include <linux/pid_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "os.h"
21
Al Virof382d6e2008-02-23 04:53:53 -050022static struct inode *get_inode(struct super_block *, struct dentry *);
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 {
Jeff Dikea0612b12008-05-12 14:01:49 -070037 struct dentry *proc_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 struct inode vfs_inode;
39};
40
41static inline struct hppfs_inode_info *HPPFS_I(struct inode *inode)
42{
Paolo 'Blaisorblade' Giarrussofd589e02005-08-26 16:57:53 +020043 return container_of(inode, struct hppfs_inode_info, vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044}
45
46#define HPPFS_SUPER_MAGIC 0xb00000ee
47
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -080048static const struct super_operations hppfs_sbops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50static int is_pid(struct dentry *dentry)
51{
52 struct super_block *sb;
53 int i;
54
55 sb = dentry->d_sb;
Jeff Dikea0612b12008-05-12 14:01:49 -070056 if (dentry->d_parent != sb->s_root)
Dave Hansen1dd0dd12008-02-15 18:37:00 -050057 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Dave Hansen1dd0dd12008-02-15 18:37:00 -050059 for (i = 0; i < dentry->d_name.len; i++) {
60 if (!isdigit(dentry->d_name.name[i]))
61 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 }
Dave Hansen1dd0dd12008-02-15 18:37:00 -050063 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064}
65
66static char *dentry_name(struct dentry *dentry, int extra)
67{
68 struct dentry *parent;
69 char *root, *name;
70 const char *seg_name;
71 int len, seg_len;
72
73 len = 0;
74 parent = dentry;
Dave Hansen1dd0dd12008-02-15 18:37:00 -050075 while (parent->d_parent != parent) {
76 if (is_pid(parent))
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 len += strlen("pid") + 1;
78 else len += parent->d_name.len + 1;
79 parent = parent->d_parent;
80 }
81
82 root = "proc";
83 len += strlen(root);
84 name = kmalloc(len + extra + 1, GFP_KERNEL);
Dave Hansen1dd0dd12008-02-15 18:37:00 -050085 if (name == NULL)
86 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 name[len] = '\0';
89 parent = dentry;
Dave Hansen1dd0dd12008-02-15 18:37:00 -050090 while (parent->d_parent != parent) {
91 if (is_pid(parent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 seg_name = "pid";
93 seg_len = strlen("pid");
94 }
95 else {
96 seg_name = parent->d_name.name;
97 seg_len = parent->d_name.len;
98 }
99
100 len -= seg_len + 1;
101 name[len] = '/';
102 strncpy(&name[len + 1], seg_name, seg_len);
103 parent = parent->d_parent;
104 }
105 strncpy(name, root, strlen(root));
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500106 return name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109static int file_removed(struct dentry *dentry, const char *file)
110{
111 char *host_file;
112 int extra, fd;
113
114 extra = 0;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500115 if (file != NULL)
116 extra += strlen(file) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 host_file = dentry_name(dentry, extra + strlen("/remove"));
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500119 if (host_file == NULL) {
120 printk(KERN_ERR "file_removed : allocation failed\n");
121 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }
123
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500124 if (file != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 strcat(host_file, "/");
126 strcat(host_file, file);
127 }
128 strcat(host_file, "/remove");
129
130 fd = os_open_file(host_file, of_read(OPENFLAGS()), 0);
131 kfree(host_file);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500132 if (fd > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 os_close_file(fd);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500134 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 }
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500136 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139static struct dentry *hppfs_lookup(struct inode *ino, struct dentry *dentry,
Jeff Dikea0612b12008-05-12 14:01:49 -0700140 struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 struct dentry *proc_dentry, *new, *parent;
143 struct inode *inode;
144 int err, deleted;
145
146 deleted = file_removed(dentry, NULL);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500147 if (deleted < 0)
148 return ERR_PTR(deleted);
149 else if (deleted)
150 return ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152 err = -ENOMEM;
153 parent = HPPFS_I(ino)->proc_dentry;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800154 mutex_lock(&parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 proc_dentry = d_lookup(parent, &dentry->d_name);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500156 if (proc_dentry == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 proc_dentry = d_alloc(parent, &dentry->d_name);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500158 if (proc_dentry == NULL) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800159 mutex_unlock(&parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 goto out;
161 }
162 new = (*parent->d_inode->i_op->lookup)(parent->d_inode,
163 proc_dentry, NULL);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500164 if (new) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 dput(proc_dentry);
166 proc_dentry = new;
167 }
168 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800169 mutex_unlock(&parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500171 if (IS_ERR(proc_dentry))
172 return proc_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Al Virof382d6e2008-02-23 04:53:53 -0500174 err = -ENOMEM;
175 inode = get_inode(ino->i_sb, proc_dentry);
176 if (!inode)
Al Viro3cc06582011-07-17 22:24:15 -0400177 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 d_add(dentry, inode);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500180 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 out:
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500183 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800186static const struct inode_operations hppfs_file_iops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187};
188
Al Viro347f2172006-03-31 02:30:16 -0800189static ssize_t read_proc(struct file *file, char __user *buf, ssize_t count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 loff_t *ppos, int is_user)
191{
Al Viro347f2172006-03-31 02:30:16 -0800192 ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 ssize_t n;
194
Josef Sipek471b17e2006-12-08 02:37:07 -0800195 read = file->f_path.dentry->d_inode->i_fop->read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500197 if (!is_user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 set_fs(KERNEL_DS);
199
200 n = (*read)(file, buf, count, &file->f_pos);
201
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500202 if (!is_user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 set_fs(USER_DS);
204
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500205 if (ppos)
206 *ppos = file->f_pos;
Paolo 'Blaisorblade' Giarrusso8e0a2182005-07-14 00:33:41 -0700207 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
Al Viro347f2172006-03-31 02:30:16 -0800210static ssize_t hppfs_read_file(int fd, char __user *buf, ssize_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
212 ssize_t n;
213 int cur, err;
214 char *new_buf;
215
216 n = -ENOMEM;
217 new_buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500218 if (new_buf == NULL) {
219 printk(KERN_ERR "hppfs_read_file : kmalloc failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 goto out;
221 }
222 n = 0;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500223 while (count > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 cur = min_t(ssize_t, count, PAGE_SIZE);
225 err = os_read_file(fd, new_buf, cur);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500226 if (err < 0) {
227 printk(KERN_ERR "hppfs_read : read failed, "
228 "errno = %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 n = err;
230 goto out_free;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500231 } else if (err == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 break;
233
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500234 if (copy_to_user(buf, new_buf, err)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 n = -EFAULT;
236 goto out_free;
237 }
238 n += err;
239 count -= err;
240 }
241 out_free:
242 kfree(new_buf);
243 out:
Paolo 'Blaisorblade' Giarrusso8e0a2182005-07-14 00:33:41 -0700244 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
246
Al Viro347f2172006-03-31 02:30:16 -0800247static ssize_t hppfs_read(struct file *file, char __user *buf, size_t count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 loff_t *ppos)
249{
250 struct hppfs_private *hppfs = file->private_data;
251 struct hppfs_data *data;
252 loff_t off;
253 int err;
254
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500255 if (hppfs->contents != NULL) {
Jeff Dikea0612b12008-05-12 14:01:49 -0700256 int rem;
257
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500258 if (*ppos >= hppfs->len)
259 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 data = hppfs->contents;
262 off = *ppos;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500263 while (off >= sizeof(data->contents)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 data = list_entry(data->list.next, struct hppfs_data,
265 list);
266 off -= sizeof(data->contents);
267 }
268
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500269 if (off + count > hppfs->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 count = hppfs->len - off;
Jeff Dikea0612b12008-05-12 14:01:49 -0700271 rem = copy_to_user(buf, &data->contents[off], count);
272 *ppos += count - rem;
273 if (rem > 0)
274 return -EFAULT;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500275 } else if (hppfs->host_fd != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 err = os_seek_file(hppfs->host_fd, *ppos);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500277 if (err) {
278 printk(KERN_ERR "hppfs_read : seek failed, "
279 "errno = %d\n", err);
280 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
Roel Kluin880fe762009-04-02 16:57:18 -0700282 err = hppfs_read_file(hppfs->host_fd, buf, count);
283 if (err < 0) {
284 printk(KERN_ERR "hppfs_read: read failed: %d\n", err);
285 return err;
286 }
287 count = err;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500288 if (count > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 *ppos += count;
290 }
291 else count = read_proc(hppfs->proc_file, buf, count, ppos, 1);
292
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500293 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
Jeff Dikea0612b12008-05-12 14:01:49 -0700296static ssize_t hppfs_write(struct file *file, const char __user *buf,
297 size_t len, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 struct hppfs_private *data = file->private_data;
300 struct file *proc_file = data->proc_file;
Al Viro347f2172006-03-31 02:30:16 -0800301 ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Josef Sipek471b17e2006-12-08 02:37:07 -0800303 write = proc_file->f_path.dentry->d_inode->i_fop->write;
Jeff Dikea0612b12008-05-12 14:01:49 -0700304 return (*write)(proc_file, buf, len, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
307static int open_host_sock(char *host_file, int *filter_out)
308{
309 char *end;
310 int fd;
311
312 end = &host_file[strlen(host_file)];
313 strcpy(end, "/rw");
314 *filter_out = 1;
315 fd = os_connect_socket(host_file);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500316 if (fd > 0)
317 return fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 strcpy(end, "/r");
320 *filter_out = 0;
321 fd = os_connect_socket(host_file);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500322 return fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
325static void free_contents(struct hppfs_data *head)
326{
327 struct hppfs_data *data;
328 struct list_head *ele, *next;
329
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500330 if (head == NULL)
331 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500333 list_for_each_safe(ele, next, &head->list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 data = list_entry(ele, struct hppfs_data, list);
335 kfree(data);
336 }
337 kfree(head);
338}
339
340static struct hppfs_data *hppfs_get_data(int fd, int filter,
341 struct file *proc_file,
342 struct file *hppfs_file,
343 loff_t *size_out)
344{
345 struct hppfs_data *data, *new, *head;
346 int n, err;
347
348 err = -ENOMEM;
349 data = kmalloc(sizeof(*data), GFP_KERNEL);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500350 if (data == NULL) {
351 printk(KERN_ERR "hppfs_get_data : head allocation failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 goto failed;
353 }
354
355 INIT_LIST_HEAD(&data->list);
356
357 head = data;
358 *size_out = 0;
359
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500360 if (filter) {
361 while ((n = read_proc(proc_file, data->contents,
Jeff Dikea0612b12008-05-12 14:01:49 -0700362 sizeof(data->contents), NULL, 0)) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 os_write_file(fd, data->contents, n);
364 err = os_shutdown_socket(fd, 0, 1);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500365 if (err) {
366 printk(KERN_ERR "hppfs_get_data : failed to shut down "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 "socket\n");
368 goto failed_free;
369 }
370 }
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500371 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 n = os_read_file(fd, data->contents, sizeof(data->contents));
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500373 if (n < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 err = n;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500375 printk(KERN_ERR "hppfs_get_data : read failed, "
376 "errno = %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 goto failed_free;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500378 } else if (n == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 break;
380
381 *size_out += n;
382
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500383 if (n < sizeof(data->contents))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 break;
385
386 new = kmalloc(sizeof(*data), GFP_KERNEL);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500387 if (new == 0) {
388 printk(KERN_ERR "hppfs_get_data : data allocation "
389 "failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 err = -ENOMEM;
391 goto failed_free;
392 }
393
394 INIT_LIST_HEAD(&new->list);
395 list_add(&new->list, &data->list);
396 data = new;
397 }
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500398 return head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 failed_free:
401 free_contents(head);
402 failed:
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500403 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
406static struct hppfs_private *hppfs_data(void)
407{
408 struct hppfs_private *data;
409
410 data = kmalloc(sizeof(*data), GFP_KERNEL);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500411 if (data == NULL)
412 return data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 *data = ((struct hppfs_private ) { .host_fd = -1,
415 .len = -1,
416 .contents = NULL } );
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500417 return data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
420static int file_mode(int fmode)
421{
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500422 if (fmode == (FMODE_READ | FMODE_WRITE))
423 return O_RDWR;
424 if (fmode == FMODE_READ)
425 return O_RDONLY;
426 if (fmode == FMODE_WRITE)
427 return O_WRONLY;
428 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
431static int hppfs_open(struct inode *inode, struct file *file)
432{
David Howellsd76b0d92008-11-14 10:39:25 +1100433 const struct cred *cred = file->f_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 struct hppfs_private *data;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500435 struct vfsmount *proc_mnt;
Jeff Dikea0612b12008-05-12 14:01:49 -0700436 struct dentry *proc_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 char *host_file;
438 int err, fd, type, filter;
439
440 err = -ENOMEM;
441 data = hppfs_data();
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500442 if (data == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 goto out;
444
Josef Sipek471b17e2006-12-08 02:37:07 -0800445 host_file = dentry_name(file->f_path.dentry, strlen("/rw"));
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500446 if (host_file == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 goto out_free2;
448
449 proc_dentry = HPPFS_I(inode)->proc_dentry;
Al Virof382d6e2008-02-23 04:53:53 -0500450 proc_mnt = inode->i_sb->s_fs_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 /* XXX This isn't closed anywhere */
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500453 data->proc_file = dentry_open(dget(proc_dentry), mntget(proc_mnt),
David Howells745ca242008-11-14 10:39:22 +1100454 file_mode(file->f_mode), cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 err = PTR_ERR(data->proc_file);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500456 if (IS_ERR(data->proc_file))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 goto out_free1;
458
459 type = os_file_type(host_file);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500460 if (type == OS_TYPE_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 fd = os_open_file(host_file, of_read(OPENFLAGS()), 0);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500462 if (fd >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 data->host_fd = fd;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500464 else
465 printk(KERN_ERR "hppfs_open : failed to open '%s', "
466 "errno = %d\n", host_file, -fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 data->contents = NULL;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500469 } else if (type == OS_TYPE_DIR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 fd = open_host_sock(host_file, &filter);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500471 if (fd > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 data->contents = hppfs_get_data(fd, filter,
Paolo 'Blaisorblade' Giarrusso3f580472005-07-07 17:56:51 -0700473 data->proc_file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 file, &data->len);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500475 if (!IS_ERR(data->contents))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 data->host_fd = fd;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500477 } else
478 printk(KERN_ERR "hppfs_open : failed to open a socket "
479 "in '%s', errno = %d\n", host_file, -fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
481 kfree(host_file);
482
483 file->private_data = data;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500484 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 out_free1:
487 kfree(host_file);
488 out_free2:
489 free_contents(data->contents);
490 kfree(data);
491 out:
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500492 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
494
495static int hppfs_dir_open(struct inode *inode, struct file *file)
496{
David Howellsd76b0d92008-11-14 10:39:25 +1100497 const struct cred *cred = file->f_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 struct hppfs_private *data;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500499 struct vfsmount *proc_mnt;
Jeff Dikea0612b12008-05-12 14:01:49 -0700500 struct dentry *proc_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 int err;
502
503 err = -ENOMEM;
504 data = hppfs_data();
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500505 if (data == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 goto out;
507
508 proc_dentry = HPPFS_I(inode)->proc_dentry;
Al Virof382d6e2008-02-23 04:53:53 -0500509 proc_mnt = inode->i_sb->s_fs_info;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500510 data->proc_file = dentry_open(dget(proc_dentry), mntget(proc_mnt),
David Howells745ca242008-11-14 10:39:22 +1100511 file_mode(file->f_mode), cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 err = PTR_ERR(data->proc_file);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500513 if (IS_ERR(data->proc_file))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 goto out_free;
515
516 file->private_data = data;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500517 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 out_free:
520 kfree(data);
521 out:
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500522 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523}
524
525static loff_t hppfs_llseek(struct file *file, loff_t off, int where)
526{
527 struct hppfs_private *data = file->private_data;
Paolo 'Blaisorblade' Giarrusso3f580472005-07-07 17:56:51 -0700528 struct file *proc_file = data->proc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 loff_t (*llseek)(struct file *, loff_t, int);
530 loff_t ret;
531
Josef Sipek471b17e2006-12-08 02:37:07 -0800532 llseek = proc_file->f_path.dentry->d_inode->i_fop->llseek;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500533 if (llseek != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 ret = (*llseek)(proc_file, off, where);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500535 if (ret < 0)
536 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
538
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500539 return default_llseek(file, off, where);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
541
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800542static const struct file_operations hppfs_file_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 .owner = NULL,
544 .llseek = hppfs_llseek,
545 .read = hppfs_read,
546 .write = hppfs_write,
547 .open = hppfs_open,
548};
549
550struct hppfs_dirent {
551 void *vfs_dirent;
552 filldir_t filldir;
553 struct dentry *dentry;
554};
555
556static int hppfs_filldir(void *d, const char *name, int size,
Al Viroc8adf942006-10-09 20:26:58 +0100557 loff_t offset, u64 inode, unsigned int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
559 struct hppfs_dirent *dirent = d;
560
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500561 if (file_removed(dirent->dentry, name))
562 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500564 return (*dirent->filldir)(dirent->vfs_dirent, name, size, offset,
565 inode, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
568static int hppfs_readdir(struct file *file, void *ent, filldir_t filldir)
569{
570 struct hppfs_private *data = file->private_data;
Paolo 'Blaisorblade' Giarrusso3f580472005-07-07 17:56:51 -0700571 struct file *proc_file = data->proc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 int (*readdir)(struct file *, void *, filldir_t);
573 struct hppfs_dirent dirent = ((struct hppfs_dirent)
574 { .vfs_dirent = ent,
575 .filldir = filldir,
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500576 .dentry = file->f_path.dentry
577 });
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 int err;
579
Josef Sipek471b17e2006-12-08 02:37:07 -0800580 readdir = proc_file->f_path.dentry->d_inode->i_fop->readdir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 proc_file->f_pos = file->f_pos;
583 err = (*readdir)(proc_file, &dirent, hppfs_filldir);
584 file->f_pos = proc_file->f_pos;
585
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500586 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
588
Christoph Hellwig7ea80852010-05-26 17:53:25 +0200589static int hppfs_fsync(struct file *file, int datasync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500591 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
593
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800594static const struct file_operations hppfs_dir_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 .owner = NULL,
596 .readdir = hppfs_readdir,
597 .open = hppfs_dir_open,
598 .fsync = hppfs_fsync,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200599 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600};
601
David Howells726c3342006-06-23 02:02:58 -0700602static int hppfs_statfs(struct dentry *dentry, struct kstatfs *sf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
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;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500610 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
613static struct inode *hppfs_alloc_inode(struct super_block *sb)
614{
615 struct hppfs_inode_info *hi;
616
617 hi = kmalloc(sizeof(*hi), GFP_KERNEL);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500618 if (!hi)
619 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500621 hi->proc_dentry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 inode_init_once(&hi->vfs_inode);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500623 return &hi->vfs_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
625
Al Viro33b0daa2010-06-07 00:12:50 -0400626void hppfs_evict_inode(struct inode *ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
Al Viro33b0daa2010-06-07 00:12:50 -0400628 end_writeback(ino);
Jeff Dikea0612b12008-05-12 14:01:49 -0700629 dput(HPPFS_I(ino)->proc_dentry);
630 mntput(ino->i_sb->s_fs_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100633static void hppfs_i_callback(struct rcu_head *head)
634{
635 struct inode *inode = container_of(head, struct inode, i_rcu);
636 INIT_LIST_HEAD(&inode->i_dentry);
637 kfree(HPPFS_I(inode));
638}
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640static void hppfs_destroy_inode(struct inode *inode)
641{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100642 call_rcu(&inode->i_rcu, hppfs_i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800645static const struct super_operations hppfs_sbops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 .alloc_inode = hppfs_alloc_inode,
647 .destroy_inode = hppfs_destroy_inode,
Al Viro33b0daa2010-06-07 00:12:50 -0400648 .evict_inode = hppfs_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 .statfs = hppfs_statfs,
650};
651
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500652static int hppfs_readlink(struct dentry *dentry, char __user *buffer,
653 int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
Al Viro7b264fc2009-12-23 00:43:50 -0500655 struct dentry *proc_dentry = HPPFS_I(dentry->d_inode)->proc_dentry;
Jeff Dikea0612b12008-05-12 14:01:49 -0700656 return proc_dentry->d_inode->i_op->readlink(proc_dentry, buffer,
657 buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
Jeff Dikea0612b12008-05-12 14:01:49 -0700660static void *hppfs_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Al Viro7b264fc2009-12-23 00:43:50 -0500662 struct dentry *proc_dentry = HPPFS_I(dentry->d_inode)->proc_dentry;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500663
Jeff Dikea0612b12008-05-12 14:01:49 -0700664 return proc_dentry->d_inode->i_op->follow_link(proc_dentry, nd);
665}
Paolo 'Blaisorblade' Giarrussod7a60d52005-08-26 16:57:44 +0200666
Al Viro7b264fc2009-12-23 00:43:50 -0500667static void hppfs_put_link(struct dentry *dentry, struct nameidata *nd,
668 void *cookie)
669{
670 struct dentry *proc_dentry = HPPFS_I(dentry->d_inode)->proc_dentry;
671
672 if (proc_dentry->d_inode->i_op->put_link)
673 proc_dentry->d_inode->i_op->put_link(proc_dentry, nd, cookie);
674}
675
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800676static const struct inode_operations hppfs_dir_iops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 .lookup = hppfs_lookup,
678};
679
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800680static const struct inode_operations hppfs_link_iops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 .readlink = hppfs_readlink,
682 .follow_link = hppfs_follow_link,
Al Viro7b264fc2009-12-23 00:43:50 -0500683 .put_link = hppfs_put_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684};
685
Al Virof382d6e2008-02-23 04:53:53 -0500686static struct inode *get_inode(struct super_block *sb, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
Al Virof382d6e2008-02-23 04:53:53 -0500688 struct inode *proc_ino = dentry->d_inode;
689 struct inode *inode = new_inode(sb);
690
Al Viro3cc06582011-07-17 22:24:15 -0400691 if (!inode) {
692 dput(dentry);
Al Virof382d6e2008-02-23 04:53:53 -0500693 return ERR_PTR(-ENOMEM);
Al Viro3cc06582011-07-17 22:24:15 -0400694 }
Al Virof382d6e2008-02-23 04:53:53 -0500695
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500696 if (S_ISDIR(dentry->d_inode->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 inode->i_op = &hppfs_dir_iops;
698 inode->i_fop = &hppfs_dir_fops;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500699 } else if (S_ISLNK(dentry->d_inode->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 inode->i_op = &hppfs_link_iops;
701 inode->i_fop = &hppfs_file_fops;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500702 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 inode->i_op = &hppfs_file_iops;
704 inode->i_fop = &hppfs_file_fops;
705 }
706
Al Viro3cc06582011-07-17 22:24:15 -0400707 HPPFS_I(inode)->proc_dentry = dentry;
Al Virof382d6e2008-02-23 04:53:53 -0500708
709 inode->i_uid = proc_ino->i_uid;
710 inode->i_gid = proc_ino->i_gid;
711 inode->i_atime = proc_ino->i_atime;
712 inode->i_mtime = proc_ino->i_mtime;
713 inode->i_ctime = proc_ino->i_ctime;
714 inode->i_ino = proc_ino->i_ino;
715 inode->i_mode = proc_ino->i_mode;
716 inode->i_nlink = proc_ino->i_nlink;
717 inode->i_size = proc_ino->i_size;
718 inode->i_blocks = proc_ino->i_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Jeff Dikea0612b12008-05-12 14:01:49 -0700720 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721}
722
723static int hppfs_fill_super(struct super_block *sb, void *d, int silent)
724{
725 struct inode *root_inode;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500726 struct vfsmount *proc_mnt;
Al Virof382d6e2008-02-23 04:53:53 -0500727 int err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Al Viro0ceeca52010-02-05 09:34:36 -0500729 proc_mnt = mntget(current->nsproxy->pid_ns->proc_mnt);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500730 if (IS_ERR(proc_mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 goto out;
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 sb->s_blocksize = 1024;
734 sb->s_blocksize_bits = 10;
735 sb->s_magic = HPPFS_SUPER_MAGIC;
736 sb->s_op = &hppfs_sbops;
Al Virof382d6e2008-02-23 04:53:53 -0500737 sb->s_fs_info = proc_mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 err = -ENOMEM;
Al Viro3cc06582011-07-17 22:24:15 -0400740 root_inode = get_inode(sb, dget(proc_mnt->mnt_sb->s_root));
Al Virof382d6e2008-02-23 04:53:53 -0500741 if (!root_inode)
742 goto out_mntput;
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 sb->s_root = d_alloc_root(root_inode);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500745 if (!sb->s_root)
746 goto out_iput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500748 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500750 out_iput:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 iput(root_inode);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500752 out_mntput:
753 mntput(proc_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 out:
755 return(err);
756}
757
Al Viro3c26ff62010-07-25 11:46:36 +0400758static struct dentry *hppfs_read_super(struct file_system_type *type,
David Howells454e2392006-06-23 02:02:57 -0700759 int flags, const char *dev_name,
Al Viro3c26ff62010-07-25 11:46:36 +0400760 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
Al Viro3c26ff62010-07-25 11:46:36 +0400762 return mount_nodev(type, flags, data, hppfs_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764
765static struct file_system_type hppfs_type = {
766 .owner = THIS_MODULE,
767 .name = "hppfs",
Al Viro3c26ff62010-07-25 11:46:36 +0400768 .mount = hppfs_read_super,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 .kill_sb = kill_anon_super,
770 .fs_flags = 0,
771};
772
773static int __init init_hppfs(void)
774{
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500775 return register_filesystem(&hppfs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776}
777
778static void __exit exit_hppfs(void)
779{
780 unregister_filesystem(&hppfs_type);
781}
782
783module_init(init_hppfs)
784module_exit(exit_hppfs)
785MODULE_LICENSE("GPL");