blob: fa2bd5366ecf1f4c3d9b81c2d80336a621831dcf [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;
Kees Cook096a8aa2013-07-03 15:04:55 -070072 int len, seg_len, root_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
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";
Kees Cook096a8aa2013-07-03 15:04:55 -070084 root_len = strlen(root);
85 len += root_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 name = kmalloc(len + extra + 1, GFP_KERNEL);
Dave Hansen1dd0dd12008-02-15 18:37:00 -050087 if (name == NULL)
88 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90 name[len] = '\0';
91 parent = dentry;
Dave Hansen1dd0dd12008-02-15 18:37:00 -050092 while (parent->d_parent != parent) {
93 if (is_pid(parent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 seg_name = "pid";
Kees Cook096a8aa2013-07-03 15:04:55 -070095 seg_len = strlen(seg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 }
97 else {
98 seg_name = parent->d_name.name;
99 seg_len = parent->d_name.len;
100 }
101
102 len -= seg_len + 1;
103 name[len] = '/';
Kees Cook096a8aa2013-07-03 15:04:55 -0700104 memcpy(&name[len + 1], seg_name, seg_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 parent = parent->d_parent;
106 }
Kees Cook096a8aa2013-07-03 15:04:55 -0700107 memcpy(name, root, root_len);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500108 return name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111static int file_removed(struct dentry *dentry, const char *file)
112{
113 char *host_file;
114 int extra, fd;
115
116 extra = 0;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500117 if (file != NULL)
118 extra += strlen(file) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 host_file = dentry_name(dentry, extra + strlen("/remove"));
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500121 if (host_file == NULL) {
122 printk(KERN_ERR "file_removed : allocation failed\n");
123 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
125
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500126 if (file != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 strcat(host_file, "/");
128 strcat(host_file, file);
129 }
130 strcat(host_file, "/remove");
131
132 fd = os_open_file(host_file, of_read(OPENFLAGS()), 0);
133 kfree(host_file);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500134 if (fd > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 os_close_file(fd);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500136 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 }
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500138 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141static struct dentry *hppfs_lookup(struct inode *ino, struct dentry *dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -0400142 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Al Viro0916a5e2011-07-17 22:27:22 -0400144 struct dentry *proc_dentry, *parent;
145 struct qstr *name = &dentry->d_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 struct inode *inode;
147 int err, deleted;
148
149 deleted = file_removed(dentry, NULL);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500150 if (deleted < 0)
151 return ERR_PTR(deleted);
152 else if (deleted)
153 return ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 parent = HPPFS_I(ino)->proc_dentry;
David Howells2b0143b2015-03-17 22:25:59 +0000156 mutex_lock(&d_inode(parent)->i_mutex);
Al Viro0916a5e2011-07-17 22:27:22 -0400157 proc_dentry = lookup_one_len(name->name, parent, name->len);
David Howells2b0143b2015-03-17 22:25:59 +0000158 mutex_unlock(&d_inode(parent)->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500160 if (IS_ERR(proc_dentry))
161 return proc_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Al Virof382d6e2008-02-23 04:53:53 -0500163 err = -ENOMEM;
164 inode = get_inode(ino->i_sb, proc_dentry);
165 if (!inode)
Al Viro3cc06582011-07-17 22:24:15 -0400166 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 d_add(dentry, inode);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500169 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 out:
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500172 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800175static const struct inode_operations hppfs_file_iops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176};
177
Al Viro347f2172006-03-31 02:30:16 -0800178static ssize_t read_proc(struct file *file, char __user *buf, ssize_t count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 loff_t *ppos, int is_user)
180{
Al Viro347f2172006-03-31 02:30:16 -0800181 ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 ssize_t n;
183
Al Viro496ad9a2013-01-23 17:07:38 -0500184 read = file_inode(file)->i_fop->read;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500186 if (!is_user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 set_fs(KERNEL_DS);
188
189 n = (*read)(file, buf, count, &file->f_pos);
190
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500191 if (!is_user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 set_fs(USER_DS);
193
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500194 if (ppos)
195 *ppos = file->f_pos;
Paolo 'Blaisorblade' Giarrusso8e0a2182005-07-14 00:33:41 -0700196 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
Al Viro347f2172006-03-31 02:30:16 -0800199static ssize_t hppfs_read_file(int fd, char __user *buf, ssize_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
201 ssize_t n;
202 int cur, err;
203 char *new_buf;
204
205 n = -ENOMEM;
206 new_buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500207 if (new_buf == NULL) {
208 printk(KERN_ERR "hppfs_read_file : kmalloc failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 goto out;
210 }
211 n = 0;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500212 while (count > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 cur = min_t(ssize_t, count, PAGE_SIZE);
214 err = os_read_file(fd, new_buf, cur);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500215 if (err < 0) {
216 printk(KERN_ERR "hppfs_read : read failed, "
217 "errno = %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 n = err;
219 goto out_free;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500220 } else if (err == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 break;
222
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500223 if (copy_to_user(buf, new_buf, err)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 n = -EFAULT;
225 goto out_free;
226 }
227 n += err;
228 count -= err;
229 }
230 out_free:
231 kfree(new_buf);
232 out:
Paolo 'Blaisorblade' Giarrusso8e0a2182005-07-14 00:33:41 -0700233 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
Al Viro347f2172006-03-31 02:30:16 -0800236static ssize_t hppfs_read(struct file *file, char __user *buf, size_t count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 loff_t *ppos)
238{
239 struct hppfs_private *hppfs = file->private_data;
240 struct hppfs_data *data;
241 loff_t off;
242 int err;
243
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500244 if (hppfs->contents != NULL) {
Jeff Dikea0612b12008-05-12 14:01:49 -0700245 int rem;
246
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500247 if (*ppos >= hppfs->len)
248 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 data = hppfs->contents;
251 off = *ppos;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500252 while (off >= sizeof(data->contents)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 data = list_entry(data->list.next, struct hppfs_data,
254 list);
255 off -= sizeof(data->contents);
256 }
257
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500258 if (off + count > hppfs->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 count = hppfs->len - off;
Jeff Dikea0612b12008-05-12 14:01:49 -0700260 rem = copy_to_user(buf, &data->contents[off], count);
261 *ppos += count - rem;
262 if (rem > 0)
263 return -EFAULT;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500264 } else if (hppfs->host_fd != -1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 err = os_seek_file(hppfs->host_fd, *ppos);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500266 if (err) {
267 printk(KERN_ERR "hppfs_read : seek failed, "
268 "errno = %d\n", err);
269 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 }
Roel Kluin880fe762009-04-02 16:57:18 -0700271 err = hppfs_read_file(hppfs->host_fd, buf, count);
272 if (err < 0) {
273 printk(KERN_ERR "hppfs_read: read failed: %d\n", err);
274 return err;
275 }
276 count = err;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500277 if (count > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 *ppos += count;
279 }
280 else count = read_proc(hppfs->proc_file, buf, count, ppos, 1);
281
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500282 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
Jeff Dikea0612b12008-05-12 14:01:49 -0700285static ssize_t hppfs_write(struct file *file, const char __user *buf,
286 size_t len, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287{
288 struct hppfs_private *data = file->private_data;
289 struct file *proc_file = data->proc_file;
Al Viro347f2172006-03-31 02:30:16 -0800290 ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Al Viro496ad9a2013-01-23 17:07:38 -0500292 write = file_inode(proc_file)->i_fop->write;
Jeff Dikea0612b12008-05-12 14:01:49 -0700293 return (*write)(proc_file, buf, len, ppos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
296static int open_host_sock(char *host_file, int *filter_out)
297{
298 char *end;
299 int fd;
300
301 end = &host_file[strlen(host_file)];
302 strcpy(end, "/rw");
303 *filter_out = 1;
304 fd = os_connect_socket(host_file);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500305 if (fd > 0)
306 return fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 strcpy(end, "/r");
309 *filter_out = 0;
310 fd = os_connect_socket(host_file);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500311 return fd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
314static void free_contents(struct hppfs_data *head)
315{
316 struct hppfs_data *data;
317 struct list_head *ele, *next;
318
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500319 if (head == NULL)
320 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500322 list_for_each_safe(ele, next, &head->list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 data = list_entry(ele, struct hppfs_data, list);
324 kfree(data);
325 }
326 kfree(head);
327}
328
329static struct hppfs_data *hppfs_get_data(int fd, int filter,
330 struct file *proc_file,
331 struct file *hppfs_file,
332 loff_t *size_out)
333{
334 struct hppfs_data *data, *new, *head;
335 int n, err;
336
337 err = -ENOMEM;
338 data = kmalloc(sizeof(*data), GFP_KERNEL);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500339 if (data == NULL) {
340 printk(KERN_ERR "hppfs_get_data : head allocation failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 goto failed;
342 }
343
344 INIT_LIST_HEAD(&data->list);
345
346 head = data;
347 *size_out = 0;
348
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500349 if (filter) {
350 while ((n = read_proc(proc_file, data->contents,
Jeff Dikea0612b12008-05-12 14:01:49 -0700351 sizeof(data->contents), NULL, 0)) > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 os_write_file(fd, data->contents, n);
353 err = os_shutdown_socket(fd, 0, 1);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500354 if (err) {
355 printk(KERN_ERR "hppfs_get_data : failed to shut down "
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 "socket\n");
357 goto failed_free;
358 }
359 }
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500360 while (1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 n = os_read_file(fd, data->contents, sizeof(data->contents));
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500362 if (n < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 err = n;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500364 printk(KERN_ERR "hppfs_get_data : read failed, "
365 "errno = %d\n", err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 goto failed_free;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500367 } else if (n == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 break;
369
370 *size_out += n;
371
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500372 if (n < sizeof(data->contents))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 break;
374
375 new = kmalloc(sizeof(*data), GFP_KERNEL);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500376 if (new == 0) {
377 printk(KERN_ERR "hppfs_get_data : data allocation "
378 "failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 err = -ENOMEM;
380 goto failed_free;
381 }
382
383 INIT_LIST_HEAD(&new->list);
384 list_add(&new->list, &data->list);
385 data = new;
386 }
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500387 return head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 failed_free:
390 free_contents(head);
391 failed:
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500392 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
395static struct hppfs_private *hppfs_data(void)
396{
397 struct hppfs_private *data;
398
399 data = kmalloc(sizeof(*data), GFP_KERNEL);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500400 if (data == NULL)
401 return data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 *data = ((struct hppfs_private ) { .host_fd = -1,
404 .len = -1,
405 .contents = NULL } );
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500406 return data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
408
409static int file_mode(int fmode)
410{
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500411 if (fmode == (FMODE_READ | FMODE_WRITE))
412 return O_RDWR;
413 if (fmode == FMODE_READ)
414 return O_RDONLY;
415 if (fmode == FMODE_WRITE)
416 return O_WRONLY;
417 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
420static int hppfs_open(struct inode *inode, struct file *file)
421{
David Howellsd76b0d92008-11-14 10:39:25 +1100422 const struct cred *cred = file->f_cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 struct hppfs_private *data;
Al Viro765927b2012-06-26 21:58:53 +0400424 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 char *host_file;
426 int err, fd, type, filter;
427
428 err = -ENOMEM;
429 data = hppfs_data();
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500430 if (data == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 goto out;
432
Josef Sipek471b17e2006-12-08 02:37:07 -0800433 host_file = dentry_name(file->f_path.dentry, strlen("/rw"));
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500434 if (host_file == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 goto out_free2;
436
Al Viro765927b2012-06-26 21:58:53 +0400437 path.mnt = inode->i_sb->s_fs_info;
438 path.dentry = HPPFS_I(inode)->proc_dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
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
Al Virob5edfd22013-04-14 15:34:01 -0400526static int hppfs_release(struct inode *inode, struct file *file)
527{
528 struct hppfs_private *data = file->private_data;
529 struct file *proc_file = data->proc_file;
530 if (proc_file)
531 fput(proc_file);
532 kfree(data);
533 return 0;
534}
535
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800536static const struct file_operations hppfs_file_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 .owner = NULL,
538 .llseek = hppfs_llseek,
539 .read = hppfs_read,
540 .write = hppfs_write,
541 .open = hppfs_open,
Al Virob5edfd22013-04-14 15:34:01 -0400542 .release = hppfs_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543};
544
545struct hppfs_dirent {
Al Virof0c3b502013-05-16 12:07:31 -0400546 struct dir_context ctx;
547 struct dir_context *caller;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 struct dentry *dentry;
549};
550
Miklos Szerediac7576f2014-10-30 17:37:34 +0100551static int hppfs_filldir(struct dir_context *ctx, const char *name, int size,
Al Viroc8adf942006-10-09 20:26:58 +0100552 loff_t offset, u64 inode, unsigned int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Miklos Szerediac7576f2014-10-30 17:37:34 +0100554 struct hppfs_dirent *dirent =
555 container_of(ctx, struct hppfs_dirent, ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
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
Al Virof0c3b502013-05-16 12:07:31 -0400560 dirent->caller->pos = dirent->ctx.pos;
561 return !dir_emit(dirent->caller, name, size, inode, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
Al Virof0c3b502013-05-16 12:07:31 -0400564static int hppfs_readdir(struct file *file, struct dir_context *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
566 struct hppfs_private *data = file->private_data;
Paolo 'Blaisorblade' Giarrusso3f580472005-07-07 17:56:51 -0700567 struct file *proc_file = data->proc_file;
Al Virof0c3b502013-05-16 12:07:31 -0400568 struct hppfs_dirent d = {
569 .ctx.actor = hppfs_filldir,
570 .caller = ctx,
571 .dentry = file->f_path.dentry
572 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 int err;
Al Virof0c3b502013-05-16 12:07:31 -0400574 proc_file->f_pos = ctx->pos;
575 err = iterate_dir(proc_file, &d.ctx);
576 ctx->pos = d.ctx.pos;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500577 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800580static const struct file_operations hppfs_dir_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 .owner = NULL,
Al Virof0c3b502013-05-16 12:07:31 -0400582 .iterate = hppfs_readdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 .open = hppfs_dir_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200584 .llseek = default_llseek,
Al Virob5edfd22013-04-14 15:34:01 -0400585 .release = hppfs_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586};
587
David Howells726c3342006-06-23 02:02:58 -0700588static int hppfs_statfs(struct dentry *dentry, struct kstatfs *sf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
590 sf->f_blocks = 0;
591 sf->f_bfree = 0;
592 sf->f_bavail = 0;
593 sf->f_files = 0;
594 sf->f_ffree = 0;
595 sf->f_type = HPPFS_SUPER_MAGIC;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500596 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
598
599static struct inode *hppfs_alloc_inode(struct super_block *sb)
600{
601 struct hppfs_inode_info *hi;
602
603 hi = kmalloc(sizeof(*hi), GFP_KERNEL);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500604 if (!hi)
605 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500607 hi->proc_dentry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 inode_init_once(&hi->vfs_inode);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500609 return &hi->vfs_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
Al Viro33b0daa2010-06-07 00:12:50 -0400612void hppfs_evict_inode(struct inode *ino)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
Jan Karadbd57682012-05-03 14:48:02 +0200614 clear_inode(ino);
Jeff Dikea0612b12008-05-12 14:01:49 -0700615 dput(HPPFS_I(ino)->proc_dentry);
616 mntput(ino->i_sb->s_fs_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617}
618
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100619static void hppfs_i_callback(struct rcu_head *head)
620{
621 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100622 kfree(HPPFS_I(inode));
623}
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625static void hppfs_destroy_inode(struct inode *inode)
626{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100627 call_rcu(&inode->i_rcu, hppfs_i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
629
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800630static const struct super_operations hppfs_sbops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 .alloc_inode = hppfs_alloc_inode,
632 .destroy_inode = hppfs_destroy_inode,
Al Viro33b0daa2010-06-07 00:12:50 -0400633 .evict_inode = hppfs_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 .statfs = hppfs_statfs,
635};
636
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500637static int hppfs_readlink(struct dentry *dentry, char __user *buffer,
638 int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
David Howells2b0143b2015-03-17 22:25:59 +0000640 struct dentry *proc_dentry = HPPFS_I(d_inode(dentry))->proc_dentry;
641 return d_inode(proc_dentry)->i_op->readlink(proc_dentry, buffer,
Jeff Dikea0612b12008-05-12 14:01:49 -0700642 buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
Jeff Dikea0612b12008-05-12 14:01:49 -0700645static void *hppfs_follow_link(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
David Howells2b0143b2015-03-17 22:25:59 +0000647 struct dentry *proc_dentry = HPPFS_I(d_inode(dentry))->proc_dentry;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500648
David Howells2b0143b2015-03-17 22:25:59 +0000649 return d_inode(proc_dentry)->i_op->follow_link(proc_dentry, nd);
Jeff Dikea0612b12008-05-12 14:01:49 -0700650}
Paolo 'Blaisorblade' Giarrussod7a60d52005-08-26 16:57:44 +0200651
Al Viro7b264fc2009-12-23 00:43:50 -0500652static void hppfs_put_link(struct dentry *dentry, struct nameidata *nd,
653 void *cookie)
654{
David Howells2b0143b2015-03-17 22:25:59 +0000655 struct dentry *proc_dentry = HPPFS_I(d_inode(dentry))->proc_dentry;
Al Viro7b264fc2009-12-23 00:43:50 -0500656
David Howells2b0143b2015-03-17 22:25:59 +0000657 if (d_inode(proc_dentry)->i_op->put_link)
658 d_inode(proc_dentry)->i_op->put_link(proc_dentry, nd, cookie);
Al Viro7b264fc2009-12-23 00:43:50 -0500659}
660
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800661static const struct inode_operations hppfs_dir_iops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 .lookup = hppfs_lookup,
663};
664
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -0800665static const struct inode_operations hppfs_link_iops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 .readlink = hppfs_readlink,
667 .follow_link = hppfs_follow_link,
Al Viro7b264fc2009-12-23 00:43:50 -0500668 .put_link = hppfs_put_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669};
670
Al Virof382d6e2008-02-23 04:53:53 -0500671static struct inode *get_inode(struct super_block *sb, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
David Howells2b0143b2015-03-17 22:25:59 +0000673 struct inode *proc_ino = d_inode(dentry);
Al Virof382d6e2008-02-23 04:53:53 -0500674 struct inode *inode = new_inode(sb);
675
Al Viro3cc06582011-07-17 22:24:15 -0400676 if (!inode) {
677 dput(dentry);
Wei Yongjun91312c52012-09-21 12:55:02 +0800678 return NULL;
Al Viro3cc06582011-07-17 22:24:15 -0400679 }
Al Virof382d6e2008-02-23 04:53:53 -0500680
David Howellse36cb0b2015-01-29 12:02:35 +0000681 if (d_is_dir(dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 inode->i_op = &hppfs_dir_iops;
683 inode->i_fop = &hppfs_dir_fops;
David Howellse36cb0b2015-01-29 12:02:35 +0000684 } else if (d_is_symlink(dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 inode->i_op = &hppfs_link_iops;
686 inode->i_fop = &hppfs_file_fops;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500687 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 inode->i_op = &hppfs_file_iops;
689 inode->i_fop = &hppfs_file_fops;
690 }
691
Al Viro3cc06582011-07-17 22:24:15 -0400692 HPPFS_I(inode)->proc_dentry = dentry;
Al Virof382d6e2008-02-23 04:53:53 -0500693
694 inode->i_uid = proc_ino->i_uid;
695 inode->i_gid = proc_ino->i_gid;
696 inode->i_atime = proc_ino->i_atime;
697 inode->i_mtime = proc_ino->i_mtime;
698 inode->i_ctime = proc_ino->i_ctime;
699 inode->i_ino = proc_ino->i_ino;
700 inode->i_mode = proc_ino->i_mode;
Miklos Szeredibfe86842011-10-28 14:13:29 +0200701 set_nlink(inode, proc_ino->i_nlink);
Al Virof382d6e2008-02-23 04:53:53 -0500702 inode->i_size = proc_ino->i_size;
703 inode->i_blocks = proc_ino->i_blocks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
Jeff Dikea0612b12008-05-12 14:01:49 -0700705 return inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
708static int hppfs_fill_super(struct super_block *sb, void *d, int silent)
709{
710 struct inode *root_inode;
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500711 struct vfsmount *proc_mnt;
Al Virof382d6e2008-02-23 04:53:53 -0500712 int err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Eric W. Biederman17cf22c2010-03-02 14:51:53 -0800714 proc_mnt = mntget(task_active_pid_ns(current)->proc_mnt);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500715 if (IS_ERR(proc_mnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 goto out;
717
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 sb->s_blocksize = 1024;
719 sb->s_blocksize_bits = 10;
720 sb->s_magic = HPPFS_SUPER_MAGIC;
721 sb->s_op = &hppfs_sbops;
Al Virof382d6e2008-02-23 04:53:53 -0500722 sb->s_fs_info = proc_mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 err = -ENOMEM;
Al Viro4c1d5a62011-12-07 18:21:57 -0500725 root_inode = get_inode(sb, dget(proc_mnt->mnt_root));
Al Viro48fde702012-01-08 22:15:13 -0500726 sb->s_root = d_make_root(root_inode);
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500727 if (!sb->s_root)
Al Viro48fde702012-01-08 22:15:13 -0500728 goto out_mntput;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500730 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500732 out_mntput:
733 mntput(proc_mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 out:
735 return(err);
736}
737
Al Viro3c26ff62010-07-25 11:46:36 +0400738static struct dentry *hppfs_read_super(struct file_system_type *type,
David Howells454e2392006-06-23 02:02:57 -0700739 int flags, const char *dev_name,
Al Viro3c26ff62010-07-25 11:46:36 +0400740 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
Al Viro3c26ff62010-07-25 11:46:36 +0400742 return mount_nodev(type, flags, data, hppfs_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
745static struct file_system_type hppfs_type = {
746 .owner = THIS_MODULE,
747 .name = "hppfs",
Al Viro3c26ff62010-07-25 11:46:36 +0400748 .mount = hppfs_read_super,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 .kill_sb = kill_anon_super,
750 .fs_flags = 0,
751};
Eric W. Biederman7f78e032013-03-02 19:39:14 -0800752MODULE_ALIAS_FS("hppfs");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754static int __init init_hppfs(void)
755{
Dave Hansen1dd0dd12008-02-15 18:37:00 -0500756 return register_filesystem(&hppfs_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
759static void __exit exit_hppfs(void)
760{
761 unregister_filesystem(&hppfs_type);
762}
763
764module_init(init_hppfs)
765module_exit(exit_hppfs)
766MODULE_LICENSE("GPL");