blob: 74f55703be498ca0220f9150ed29a08818cd5dfd [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>
Al Virod6b722a2011-07-27 22:21:58 -040019#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/uaccess.h>
Al Viro37185b32012-10-08 03:27:32 +010021#include <os.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Al Virof382d6e2008-02-23 04:53:53 -050023static struct inode *get_inode(struct super_block *, struct dentry *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25struct hppfs_data {
26 struct list_head list;
27 char contents[PAGE_SIZE - sizeof(struct list_head)];
28};
29
30struct hppfs_private {
31 struct file *proc_file;
32 int host_fd;
33 loff_t len;
34 struct hppfs_data *contents;
35};
36
37struct hppfs_inode_info {
Jeff Dikea0612b12008-05-12 14:01:49 -070038 struct dentry *proc_dentry;
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;
Jeff Dikea0612b12008-05-12 14:01:49 -070057 if (dentry->d_parent != sb->s_root)
Dave Hansen1dd0dd12008-02-15 18:37:00 -050058 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140static struct dentry *hppfs_lookup(struct inode *ino, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -0400141 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Al Viro0916a5e2011-07-17 22:27:22 -0400143 struct dentry *proc_dentry, *parent;
144 struct qstr *name = &dentry->d_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 struct inode *inode;
146 int err, deleted;
147
148 deleted = file_removed(dentry, NULL);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500149 if (deleted < 0)
150 return ERR_PTR(deleted);
151 else if (deleted)
152 return ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 parent = HPPFS_I(ino)->proc_dentry;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800155 mutex_lock(&parent->d_inode->i_mutex);
Al Viro0916a5e2011-07-17 22:27:22 -0400156 proc_dentry = lookup_one_len(name->name, parent, name->len);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800157 mutex_unlock(&parent->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500159 if (IS_ERR(proc_dentry))
160 return proc_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Al Virof382d6e2008-02-23 04:53:53 -0500162 err = -ENOMEM;
163 inode = get_inode(ino->i_sb, proc_dentry);
164 if (!inode)
Al Viro3cc06582011-07-17 22:24:15 -0400165 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 d_add(dentry, inode);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500168 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 out:
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500171 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800174static const struct inode_operations hppfs_file_iops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175};
176
Al Viro347f2172006-03-31 02:30:16 -0800177static ssize_t read_proc(struct file *file, char __user *buf, ssize_t count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 loff_t *ppos, int is_user)
179{
Al Viro347f2172006-03-31 02:30:16 -0800180 ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 ssize_t n;
182
Al Viro496ad9a2013-01-23 17:07:38 -0500183 read = file_inode(file)->i_fop->read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500185 if (!is_user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 set_fs(KERNEL_DS);
187
188 n = (*read)(file, buf, count, &file->f_pos);
189
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500190 if (!is_user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 set_fs(USER_DS);
192
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500193 if (ppos)
194 *ppos = file->f_pos;
Paolo 'Blaisorblade' Giarrusso8e0a2182005-07-14 00:33:41 -0700195 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
Al Viro347f2172006-03-31 02:30:16 -0800198static ssize_t hppfs_read_file(int fd, char __user *buf, ssize_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
200 ssize_t n;
201 int cur, err;
202 char *new_buf;
203
204 n = -ENOMEM;
205 new_buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500206 if (new_buf == NULL) {
207 printk(KERN_ERR "hppfs_read_file : kmalloc failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 goto out;
209 }
210 n = 0;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500211 while (count > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 cur = min_t(ssize_t, count, PAGE_SIZE);
213 err = os_read_file(fd, new_buf, cur);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500214 if (err < 0) {
215 printk(KERN_ERR "hppfs_read : read failed, "
216 "errno = %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 n = err;
218 goto out_free;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500219 } else if (err == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 break;
221
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500222 if (copy_to_user(buf, new_buf, err)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 n = -EFAULT;
224 goto out_free;
225 }
226 n += err;
227 count -= err;
228 }
229 out_free:
230 kfree(new_buf);
231 out:
Paolo 'Blaisorblade' Giarrusso8e0a2182005-07-14 00:33:41 -0700232 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
Al Viro347f2172006-03-31 02:30:16 -0800235static ssize_t hppfs_read(struct file *file, char __user *buf, size_t count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 loff_t *ppos)
237{
238 struct hppfs_private *hppfs = file->private_data;
239 struct hppfs_data *data;
240 loff_t off;
241 int err;
242
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500243 if (hppfs->contents != NULL) {
Jeff Dikea0612b12008-05-12 14:01:49 -0700244 int rem;
245
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500246 if (*ppos >= hppfs->len)
247 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 data = hppfs->contents;
250 off = *ppos;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500251 while (off >= sizeof(data->contents)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 data = list_entry(data->list.next, struct hppfs_data,
253 list);
254 off -= sizeof(data->contents);
255 }
256
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500257 if (off + count > hppfs->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 count = hppfs->len - off;
Jeff Dikea0612b12008-05-12 14:01:49 -0700259 rem = copy_to_user(buf, &data->contents[off], count);
260 *ppos += count - rem;
261 if (rem > 0)
262 return -EFAULT;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500263 } else if (hppfs->host_fd != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 err = os_seek_file(hppfs->host_fd, *ppos);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500265 if (err) {
266 printk(KERN_ERR "hppfs_read : seek failed, "
267 "errno = %d\n", err);
268 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
Roel Kluin880fe762009-04-02 16:57:18 -0700270 err = hppfs_read_file(hppfs->host_fd, buf, count);
271 if (err < 0) {
272 printk(KERN_ERR "hppfs_read: read failed: %d\n", err);
273 return err;
274 }
275 count = err;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500276 if (count > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 *ppos += count;
278 }
279 else count = read_proc(hppfs->proc_file, buf, count, ppos, 1);
280
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500281 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
Jeff Dikea0612b12008-05-12 14:01:49 -0700284static ssize_t hppfs_write(struct file *file, const char __user *buf,
285 size_t len, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 struct hppfs_private *data = file->private_data;
288 struct file *proc_file = data->proc_file;
Al Viro347f2172006-03-31 02:30:16 -0800289 ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Al Viro496ad9a2013-01-23 17:07:38 -0500291 write = file_inode(proc_file)->i_fop->write;
Jeff Dikea0612b12008-05-12 14:01:49 -0700292 return (*write)(proc_file, buf, len, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
294
295static int open_host_sock(char *host_file, int *filter_out)
296{
297 char *end;
298 int fd;
299
300 end = &host_file[strlen(host_file)];
301 strcpy(end, "/rw");
302 *filter_out = 1;
303 fd = os_connect_socket(host_file);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500304 if (fd > 0)
305 return fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 strcpy(end, "/r");
308 *filter_out = 0;
309 fd = os_connect_socket(host_file);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500310 return fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
313static void free_contents(struct hppfs_data *head)
314{
315 struct hppfs_data *data;
316 struct list_head *ele, *next;
317
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500318 if (head == NULL)
319 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500321 list_for_each_safe(ele, next, &head->list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 data = list_entry(ele, struct hppfs_data, list);
323 kfree(data);
324 }
325 kfree(head);
326}
327
328static struct hppfs_data *hppfs_get_data(int fd, int filter,
329 struct file *proc_file,
330 struct file *hppfs_file,
331 loff_t *size_out)
332{
333 struct hppfs_data *data, *new, *head;
334 int n, err;
335
336 err = -ENOMEM;
337 data = kmalloc(sizeof(*data), GFP_KERNEL);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500338 if (data == NULL) {
339 printk(KERN_ERR "hppfs_get_data : head allocation failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 goto failed;
341 }
342
343 INIT_LIST_HEAD(&data->list);
344
345 head = data;
346 *size_out = 0;
347
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500348 if (filter) {
349 while ((n = read_proc(proc_file, data->contents,
Jeff Dikea0612b12008-05-12 14:01:49 -0700350 sizeof(data->contents), NULL, 0)) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 os_write_file(fd, data->contents, n);
352 err = os_shutdown_socket(fd, 0, 1);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500353 if (err) {
354 printk(KERN_ERR "hppfs_get_data : failed to shut down "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 "socket\n");
356 goto failed_free;
357 }
358 }
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500359 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 n = os_read_file(fd, data->contents, sizeof(data->contents));
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500361 if (n < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 err = n;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500363 printk(KERN_ERR "hppfs_get_data : read failed, "
364 "errno = %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 goto failed_free;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500366 } else if (n == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 break;
368
369 *size_out += n;
370
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500371 if (n < sizeof(data->contents))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 break;
373
374 new = kmalloc(sizeof(*data), GFP_KERNEL);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500375 if (new == 0) {
376 printk(KERN_ERR "hppfs_get_data : data allocation "
377 "failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 err = -ENOMEM;
379 goto failed_free;
380 }
381
382 INIT_LIST_HEAD(&new->list);
383 list_add(&new->list, &data->list);
384 data = new;
385 }
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500386 return head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 failed_free:
389 free_contents(head);
390 failed:
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500391 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
394static struct hppfs_private *hppfs_data(void)
395{
396 struct hppfs_private *data;
397
398 data = kmalloc(sizeof(*data), GFP_KERNEL);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500399 if (data == NULL)
400 return data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 *data = ((struct hppfs_private ) { .host_fd = -1,
403 .len = -1,
404 .contents = NULL } );
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500405 return data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
408static int file_mode(int fmode)
409{
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500410 if (fmode == (FMODE_READ | FMODE_WRITE))
411 return O_RDWR;
412 if (fmode == FMODE_READ)
413 return O_RDONLY;
414 if (fmode == FMODE_WRITE)
415 return O_WRONLY;
416 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
418
419static int hppfs_open(struct inode *inode, struct file *file)
420{
David Howellsd76b0d92008-11-14 10:39:25 +1100421 const struct cred *cred = file->f_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 struct hppfs_private *data;
Al Viro765927b2012-06-26 21:58:53 +0400423 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 char *host_file;
425 int err, fd, type, filter;
426
427 err = -ENOMEM;
428 data = hppfs_data();
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500429 if (data == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 goto out;
431
Josef Sipek471b17e2006-12-08 02:37:07 -0800432 host_file = dentry_name(file->f_path.dentry, strlen("/rw"));
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500433 if (host_file == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 goto out_free2;
435
Al Viro765927b2012-06-26 21:58:53 +0400436 path.mnt = inode->i_sb->s_fs_info;
437 path.dentry = HPPFS_I(inode)->proc_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 /* XXX This isn't closed anywhere */
Al Viro765927b2012-06-26 21:58:53 +0400440 data->proc_file = dentry_open(&path, file_mode(file->f_mode), cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 err = PTR_ERR(data->proc_file);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500442 if (IS_ERR(data->proc_file))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 goto out_free1;
444
445 type = os_file_type(host_file);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500446 if (type == OS_TYPE_FILE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 fd = os_open_file(host_file, of_read(OPENFLAGS()), 0);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500448 if (fd >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 data->host_fd = fd;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500450 else
451 printk(KERN_ERR "hppfs_open : failed to open '%s', "
452 "errno = %d\n", host_file, -fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 data->contents = NULL;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500455 } else if (type == OS_TYPE_DIR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 fd = open_host_sock(host_file, &filter);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500457 if (fd > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 data->contents = hppfs_get_data(fd, filter,
Paolo 'Blaisorblade' Giarrusso3f580472005-07-07 17:56:51 -0700459 data->proc_file,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 file, &data->len);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500461 if (!IS_ERR(data->contents))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 data->host_fd = fd;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500463 } else
464 printk(KERN_ERR "hppfs_open : failed to open a socket "
465 "in '%s', errno = %d\n", host_file, -fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
467 kfree(host_file);
468
469 file->private_data = data;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500470 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 out_free1:
473 kfree(host_file);
474 out_free2:
475 free_contents(data->contents);
476 kfree(data);
477 out:
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500478 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
481static int hppfs_dir_open(struct inode *inode, struct file *file)
482{
David Howellsd76b0d92008-11-14 10:39:25 +1100483 const struct cred *cred = file->f_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 struct hppfs_private *data;
Al Viro765927b2012-06-26 21:58:53 +0400485 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 int err;
487
488 err = -ENOMEM;
489 data = hppfs_data();
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500490 if (data == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 goto out;
492
Al Viro765927b2012-06-26 21:58:53 +0400493 path.mnt = inode->i_sb->s_fs_info;
494 path.dentry = HPPFS_I(inode)->proc_dentry;
495 data->proc_file = dentry_open(&path, file_mode(file->f_mode), cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 err = PTR_ERR(data->proc_file);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500497 if (IS_ERR(data->proc_file))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 goto out_free;
499
500 file->private_data = data;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500501 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
503 out_free:
504 kfree(data);
505 out:
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500506 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
509static loff_t hppfs_llseek(struct file *file, loff_t off, int where)
510{
511 struct hppfs_private *data = file->private_data;
Paolo 'Blaisorblade' Giarrusso3f580472005-07-07 17:56:51 -0700512 struct file *proc_file = data->proc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 loff_t (*llseek)(struct file *, loff_t, int);
514 loff_t ret;
515
Al Viro496ad9a2013-01-23 17:07:38 -0500516 llseek = file_inode(proc_file)->i_fop->llseek;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500517 if (llseek != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 ret = (*llseek)(proc_file, off, where);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500519 if (ret < 0)
520 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
522
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500523 return default_llseek(file, off, where);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524}
525
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800526static const struct file_operations hppfs_file_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 .owner = NULL,
528 .llseek = hppfs_llseek,
529 .read = hppfs_read,
530 .write = hppfs_write,
531 .open = hppfs_open,
532};
533
534struct hppfs_dirent {
535 void *vfs_dirent;
536 filldir_t filldir;
537 struct dentry *dentry;
538};
539
540static int hppfs_filldir(void *d, const char *name, int size,
Al Viroc8adf942006-10-09 20:26:58 +0100541 loff_t offset, u64 inode, unsigned int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
543 struct hppfs_dirent *dirent = d;
544
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500545 if (file_removed(dirent->dentry, name))
546 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500548 return (*dirent->filldir)(dirent->vfs_dirent, name, size, offset,
549 inode, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
552static int hppfs_readdir(struct file *file, void *ent, filldir_t filldir)
553{
554 struct hppfs_private *data = file->private_data;
Paolo 'Blaisorblade' Giarrusso3f580472005-07-07 17:56:51 -0700555 struct file *proc_file = data->proc_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 int (*readdir)(struct file *, void *, filldir_t);
557 struct hppfs_dirent dirent = ((struct hppfs_dirent)
558 { .vfs_dirent = ent,
559 .filldir = filldir,
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500560 .dentry = file->f_path.dentry
561 });
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 int err;
563
Al Viro496ad9a2013-01-23 17:07:38 -0500564 readdir = file_inode(proc_file)->i_fop->readdir;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 proc_file->f_pos = file->f_pos;
567 err = (*readdir)(proc_file, &dirent, hppfs_filldir);
568 file->f_pos = proc_file->f_pos;
569
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500570 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
Josef Bacik02c24a82011-07-16 20:44:56 -0400573static int hppfs_fsync(struct file *file, loff_t start, loff_t end,
574 int datasync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
Josef Bacik02c24a82011-07-16 20:44:56 -0400576 return filemap_write_and_wait_range(file->f_mapping, start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
578
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800579static const struct file_operations hppfs_dir_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 .owner = NULL,
581 .readdir = hppfs_readdir,
582 .open = hppfs_dir_open,
583 .fsync = hppfs_fsync,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200584 .llseek = default_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585};
586
David Howells726c3342006-06-23 02:02:58 -0700587static int hppfs_statfs(struct dentry *dentry, struct kstatfs *sf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
589 sf->f_blocks = 0;
590 sf->f_bfree = 0;
591 sf->f_bavail = 0;
592 sf->f_files = 0;
593 sf->f_ffree = 0;
594 sf->f_type = HPPFS_SUPER_MAGIC;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500595 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596}
597
598static struct inode *hppfs_alloc_inode(struct super_block *sb)
599{
600 struct hppfs_inode_info *hi;
601
602 hi = kmalloc(sizeof(*hi), GFP_KERNEL);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500603 if (!hi)
604 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500606 hi->proc_dentry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 inode_init_once(&hi->vfs_inode);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500608 return &hi->vfs_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609}
610
Al Viro33b0daa2010-06-07 00:12:50 -0400611void hppfs_evict_inode(struct inode *ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Jan Karadbd57682012-05-03 14:48:02 +0200613 clear_inode(ino);
Jeff Dikea0612b12008-05-12 14:01:49 -0700614 dput(HPPFS_I(ino)->proc_dentry);
615 mntput(ino->i_sb->s_fs_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
617
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100618static void hppfs_i_callback(struct rcu_head *head)
619{
620 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100621 kfree(HPPFS_I(inode));
622}
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624static void hppfs_destroy_inode(struct inode *inode)
625{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100626 call_rcu(&inode->i_rcu, hppfs_i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800629static const struct super_operations hppfs_sbops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 .alloc_inode = hppfs_alloc_inode,
631 .destroy_inode = hppfs_destroy_inode,
Al Viro33b0daa2010-06-07 00:12:50 -0400632 .evict_inode = hppfs_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 .statfs = hppfs_statfs,
634};
635
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500636static int hppfs_readlink(struct dentry *dentry, char __user *buffer,
637 int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Al Viro7b264fc2009-12-23 00:43:50 -0500639 struct dentry *proc_dentry = HPPFS_I(dentry->d_inode)->proc_dentry;
Jeff Dikea0612b12008-05-12 14:01:49 -0700640 return proc_dentry->d_inode->i_op->readlink(proc_dentry, buffer,
641 buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
Jeff Dikea0612b12008-05-12 14:01:49 -0700644static void *hppfs_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Al Viro7b264fc2009-12-23 00:43:50 -0500646 struct dentry *proc_dentry = HPPFS_I(dentry->d_inode)->proc_dentry;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500647
Jeff Dikea0612b12008-05-12 14:01:49 -0700648 return proc_dentry->d_inode->i_op->follow_link(proc_dentry, nd);
649}
Paolo 'Blaisorblade' Giarrussod7a60d52005-08-26 16:57:44 +0200650
Al Viro7b264fc2009-12-23 00:43:50 -0500651static void hppfs_put_link(struct dentry *dentry, struct nameidata *nd,
652 void *cookie)
653{
654 struct dentry *proc_dentry = HPPFS_I(dentry->d_inode)->proc_dentry;
655
656 if (proc_dentry->d_inode->i_op->put_link)
657 proc_dentry->d_inode->i_op->put_link(proc_dentry, nd, cookie);
658}
659
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800660static const struct inode_operations hppfs_dir_iops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 .lookup = hppfs_lookup,
662};
663
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800664static const struct inode_operations hppfs_link_iops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 .readlink = hppfs_readlink,
666 .follow_link = hppfs_follow_link,
Al Viro7b264fc2009-12-23 00:43:50 -0500667 .put_link = hppfs_put_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668};
669
Al Virof382d6e2008-02-23 04:53:53 -0500670static struct inode *get_inode(struct super_block *sb, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
Al Virof382d6e2008-02-23 04:53:53 -0500672 struct inode *proc_ino = dentry->d_inode;
673 struct inode *inode = new_inode(sb);
674
Al Viro3cc06582011-07-17 22:24:15 -0400675 if (!inode) {
676 dput(dentry);
Wei Yongjun91312c52012-09-21 12:55:02 +0800677 return NULL;
Al Viro3cc06582011-07-17 22:24:15 -0400678 }
Al Virof382d6e2008-02-23 04:53:53 -0500679
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500680 if (S_ISDIR(dentry->d_inode->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 inode->i_op = &hppfs_dir_iops;
682 inode->i_fop = &hppfs_dir_fops;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500683 } else if (S_ISLNK(dentry->d_inode->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 inode->i_op = &hppfs_link_iops;
685 inode->i_fop = &hppfs_file_fops;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500686 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 inode->i_op = &hppfs_file_iops;
688 inode->i_fop = &hppfs_file_fops;
689 }
690
Al Viro3cc06582011-07-17 22:24:15 -0400691 HPPFS_I(inode)->proc_dentry = dentry;
Al Virof382d6e2008-02-23 04:53:53 -0500692
693 inode->i_uid = proc_ino->i_uid;
694 inode->i_gid = proc_ino->i_gid;
695 inode->i_atime = proc_ino->i_atime;
696 inode->i_mtime = proc_ino->i_mtime;
697 inode->i_ctime = proc_ino->i_ctime;
698 inode->i_ino = proc_ino->i_ino;
699 inode->i_mode = proc_ino->i_mode;
Miklos Szeredibfe86842011-10-28 14:13:29 +0200700 set_nlink(inode, proc_ino->i_nlink);
Al Virof382d6e2008-02-23 04:53:53 -0500701 inode->i_size = proc_ino->i_size;
702 inode->i_blocks = proc_ino->i_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Jeff Dikea0612b12008-05-12 14:01:49 -0700704 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
706
707static int hppfs_fill_super(struct super_block *sb, void *d, int silent)
708{
709 struct inode *root_inode;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500710 struct vfsmount *proc_mnt;
Al Virof382d6e2008-02-23 04:53:53 -0500711 int err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Eric W. Biederman17cf22c2010-03-02 14:51:53 -0800713 proc_mnt = mntget(task_active_pid_ns(current)->proc_mnt);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500714 if (IS_ERR(proc_mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 goto out;
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 sb->s_blocksize = 1024;
718 sb->s_blocksize_bits = 10;
719 sb->s_magic = HPPFS_SUPER_MAGIC;
720 sb->s_op = &hppfs_sbops;
Al Virof382d6e2008-02-23 04:53:53 -0500721 sb->s_fs_info = proc_mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 err = -ENOMEM;
Al Viro4c1d5a62011-12-07 18:21:57 -0500724 root_inode = get_inode(sb, dget(proc_mnt->mnt_root));
Al Viro48fde702012-01-08 22:15:13 -0500725 sb->s_root = d_make_root(root_inode);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500726 if (!sb->s_root)
Al Viro48fde702012-01-08 22:15:13 -0500727 goto out_mntput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500729 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500731 out_mntput:
732 mntput(proc_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 out:
734 return(err);
735}
736
Al Viro3c26ff62010-07-25 11:46:36 +0400737static struct dentry *hppfs_read_super(struct file_system_type *type,
David Howells454e2392006-06-23 02:02:57 -0700738 int flags, const char *dev_name,
Al Viro3c26ff62010-07-25 11:46:36 +0400739 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
Al Viro3c26ff62010-07-25 11:46:36 +0400741 return mount_nodev(type, flags, data, hppfs_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742}
743
744static struct file_system_type hppfs_type = {
745 .owner = THIS_MODULE,
746 .name = "hppfs",
Al Viro3c26ff62010-07-25 11:46:36 +0400747 .mount = hppfs_read_super,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 .kill_sb = kill_anon_super,
749 .fs_flags = 0,
750};
751
752static int __init init_hppfs(void)
753{
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500754 return register_filesystem(&hppfs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756
757static void __exit exit_hppfs(void)
758{
759 unregister_filesystem(&hppfs_type);
760}
761
762module_init(init_hppfs)
763module_exit(exit_hppfs)
764MODULE_LICENSE("GPL");