hppfs.c 16.7 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
L
Linus Torvalds 已提交
3 4 5
 * Licensed under the GPL
 */

6 7
#include <linux/ctype.h>
#include <linux/dcache.h>
8
#include <linux/file.h>
9
#include <linux/fs.h>
L
Linus Torvalds 已提交
10 11
#include <linux/init.h>
#include <linux/kernel.h>
12 13 14 15
#include <linux/list.h>
#include <linux/module.h>
#include <linux/mount.h>
#include <linux/slab.h>
L
Linus Torvalds 已提交
16
#include <linux/statfs.h>
17
#include <linux/types.h>
A
Al Viro 已提交
18
#include <linux/pid_namespace.h>
L
Linus Torvalds 已提交
19 20 21
#include <asm/uaccess.h>
#include "os.h"

A
Al Viro 已提交
22
static struct inode *get_inode(struct super_block *, struct dentry *);
L
Linus Torvalds 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36

struct hppfs_data {
	struct list_head list;
	char contents[PAGE_SIZE - sizeof(struct list_head)];
};

struct hppfs_private {
	struct file *proc_file;
	int host_fd;
	loff_t len;
	struct hppfs_data *contents;
};

struct hppfs_inode_info {
J
Jeff Dike 已提交
37
	struct dentry *proc_dentry;
L
Linus Torvalds 已提交
38 39 40 41 42
	struct inode vfs_inode;
};

static inline struct hppfs_inode_info *HPPFS_I(struct inode *inode)
{
43
	return container_of(inode, struct hppfs_inode_info, vfs_inode);
L
Linus Torvalds 已提交
44 45 46 47
}

#define HPPFS_SUPER_MAGIC 0xb00000ee

48
static const struct super_operations hppfs_sbops;
L
Linus Torvalds 已提交
49 50 51 52 53 54 55

static int is_pid(struct dentry *dentry)
{
	struct super_block *sb;
	int i;

	sb = dentry->d_sb;
J
Jeff Dike 已提交
56
	if (dentry->d_parent != sb->s_root)
57
		return 0;
L
Linus Torvalds 已提交
58

59 60 61
	for (i = 0; i < dentry->d_name.len; i++) {
		if (!isdigit(dentry->d_name.name[i]))
			return 0;
L
Linus Torvalds 已提交
62
	}
63
	return 1;
L
Linus Torvalds 已提交
64 65 66 67 68 69 70 71 72 73 74
}

static char *dentry_name(struct dentry *dentry, int extra)
{
	struct dentry *parent;
	char *root, *name;
	const char *seg_name;
	int len, seg_len;

	len = 0;
	parent = dentry;
75 76
	while (parent->d_parent != parent) {
		if (is_pid(parent))
L
Linus Torvalds 已提交
77 78 79 80 81 82 83 84
			len += strlen("pid") + 1;
		else len += parent->d_name.len + 1;
		parent = parent->d_parent;
	}

	root = "proc";
	len += strlen(root);
	name = kmalloc(len + extra + 1, GFP_KERNEL);
85 86
	if (name == NULL)
		return NULL;
L
Linus Torvalds 已提交
87 88 89

	name[len] = '\0';
	parent = dentry;
90 91
	while (parent->d_parent != parent) {
		if (is_pid(parent)) {
L
Linus Torvalds 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105
			seg_name = "pid";
			seg_len = strlen("pid");
		}
		else {
			seg_name = parent->d_name.name;
			seg_len = parent->d_name.len;
		}

		len -= seg_len + 1;
		name[len] = '/';
		strncpy(&name[len + 1], seg_name, seg_len);
		parent = parent->d_parent;
	}
	strncpy(name, root, strlen(root));
106
	return name;
L
Linus Torvalds 已提交
107 108 109 110 111 112 113 114
}

static int file_removed(struct dentry *dentry, const char *file)
{
	char *host_file;
	int extra, fd;

	extra = 0;
115 116
	if (file != NULL)
		extra += strlen(file) + 1;
L
Linus Torvalds 已提交
117 118

	host_file = dentry_name(dentry, extra + strlen("/remove"));
119 120 121
	if (host_file == NULL) {
		printk(KERN_ERR "file_removed : allocation failed\n");
		return -ENOMEM;
L
Linus Torvalds 已提交
122 123
	}

124
	if (file != NULL) {
L
Linus Torvalds 已提交
125 126 127 128 129 130 131
		strcat(host_file, "/");
		strcat(host_file, file);
	}
	strcat(host_file, "/remove");

	fd = os_open_file(host_file, of_read(OPENFLAGS()), 0);
	kfree(host_file);
132
	if (fd > 0) {
L
Linus Torvalds 已提交
133
		os_close_file(fd);
134
		return 1;
L
Linus Torvalds 已提交
135
	}
136
	return 0;
L
Linus Torvalds 已提交
137 138 139
}

static struct dentry *hppfs_lookup(struct inode *ino, struct dentry *dentry,
J
Jeff Dike 已提交
140
				   struct nameidata *nd)
L
Linus Torvalds 已提交
141 142 143 144 145 146
{
	struct dentry *proc_dentry, *new, *parent;
	struct inode *inode;
	int err, deleted;

	deleted = file_removed(dentry, NULL);
147 148 149 150
	if (deleted < 0)
		return ERR_PTR(deleted);
	else if (deleted)
		return ERR_PTR(-ENOENT);
L
Linus Torvalds 已提交
151 152 153

	err = -ENOMEM;
	parent = HPPFS_I(ino)->proc_dentry;
154
	mutex_lock(&parent->d_inode->i_mutex);
L
Linus Torvalds 已提交
155
	proc_dentry = d_lookup(parent, &dentry->d_name);
156
	if (proc_dentry == NULL) {
L
Linus Torvalds 已提交
157
		proc_dentry = d_alloc(parent, &dentry->d_name);
158
		if (proc_dentry == NULL) {
159
			mutex_unlock(&parent->d_inode->i_mutex);
L
Linus Torvalds 已提交
160 161 162 163
			goto out;
		}
		new = (*parent->d_inode->i_op->lookup)(parent->d_inode,
						       proc_dentry, NULL);
164
		if (new) {
L
Linus Torvalds 已提交
165 166 167 168
			dput(proc_dentry);
			proc_dentry = new;
		}
	}
169
	mutex_unlock(&parent->d_inode->i_mutex);
L
Linus Torvalds 已提交
170

171 172
	if (IS_ERR(proc_dentry))
		return proc_dentry;
L
Linus Torvalds 已提交
173

A
Al Viro 已提交
174 175 176
	err = -ENOMEM;
	inode = get_inode(ino->i_sb, proc_dentry);
	if (!inode)
L
Linus Torvalds 已提交
177 178 179
		goto out_dput;

 	d_add(dentry, inode);
180
	return NULL;
L
Linus Torvalds 已提交
181 182 183 184

 out_dput:
	dput(proc_dentry);
 out:
185
	return ERR_PTR(err);
L
Linus Torvalds 已提交
186 187
}

188
static const struct inode_operations hppfs_file_iops = {
L
Linus Torvalds 已提交
189 190
};

A
Al Viro 已提交
191
static ssize_t read_proc(struct file *file, char __user *buf, ssize_t count,
L
Linus Torvalds 已提交
192 193
			 loff_t *ppos, int is_user)
{
A
Al Viro 已提交
194
	ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
L
Linus Torvalds 已提交
195 196
	ssize_t n;

J
Josef Sipek 已提交
197
	read = file->f_path.dentry->d_inode->i_fop->read;
L
Linus Torvalds 已提交
198

199
	if (!is_user)
L
Linus Torvalds 已提交
200 201 202 203
		set_fs(KERNEL_DS);

	n = (*read)(file, buf, count, &file->f_pos);

204
	if (!is_user)
L
Linus Torvalds 已提交
205 206
		set_fs(USER_DS);

207 208
	if (ppos)
		*ppos = file->f_pos;
209
	return n;
L
Linus Torvalds 已提交
210 211
}

A
Al Viro 已提交
212
static ssize_t hppfs_read_file(int fd, char __user *buf, ssize_t count)
L
Linus Torvalds 已提交
213 214 215 216 217 218 219
{
	ssize_t n;
	int cur, err;
	char *new_buf;

	n = -ENOMEM;
	new_buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
220 221
	if (new_buf == NULL) {
		printk(KERN_ERR "hppfs_read_file : kmalloc failed\n");
L
Linus Torvalds 已提交
222 223 224
		goto out;
	}
	n = 0;
225
	while (count > 0) {
L
Linus Torvalds 已提交
226 227
		cur = min_t(ssize_t, count, PAGE_SIZE);
		err = os_read_file(fd, new_buf, cur);
228 229 230
		if (err < 0) {
			printk(KERN_ERR "hppfs_read : read failed, "
			       "errno = %d\n", err);
L
Linus Torvalds 已提交
231 232
			n = err;
			goto out_free;
233
		} else if (err == 0)
L
Linus Torvalds 已提交
234 235
			break;

236
		if (copy_to_user(buf, new_buf, err)) {
L
Linus Torvalds 已提交
237 238 239 240 241 242 243 244 245
			n = -EFAULT;
			goto out_free;
		}
		n += err;
		count -= err;
	}
 out_free:
	kfree(new_buf);
 out:
246
	return n;
L
Linus Torvalds 已提交
247 248
}

A
Al Viro 已提交
249
static ssize_t hppfs_read(struct file *file, char __user *buf, size_t count,
L
Linus Torvalds 已提交
250 251 252 253 254 255 256
			  loff_t *ppos)
{
	struct hppfs_private *hppfs = file->private_data;
	struct hppfs_data *data;
	loff_t off;
	int err;

257
	if (hppfs->contents != NULL) {
J
Jeff Dike 已提交
258 259
		int rem;

260 261
		if (*ppos >= hppfs->len)
			return 0;
L
Linus Torvalds 已提交
262 263 264

		data = hppfs->contents;
		off = *ppos;
265
		while (off >= sizeof(data->contents)) {
L
Linus Torvalds 已提交
266 267 268 269 270
			data = list_entry(data->list.next, struct hppfs_data,
					  list);
			off -= sizeof(data->contents);
		}

271
		if (off + count > hppfs->len)
L
Linus Torvalds 已提交
272
			count = hppfs->len - off;
J
Jeff Dike 已提交
273 274 275 276
		rem = copy_to_user(buf, &data->contents[off], count);
		*ppos += count - rem;
		if (rem > 0)
			return -EFAULT;
277
	} else if (hppfs->host_fd != -1) {
L
Linus Torvalds 已提交
278
		err = os_seek_file(hppfs->host_fd, *ppos);
279 280 281 282
		if (err) {
			printk(KERN_ERR "hppfs_read : seek failed, "
			       "errno = %d\n", err);
			return err;
L
Linus Torvalds 已提交
283
		}
284 285 286 287 288 289
		err = hppfs_read_file(hppfs->host_fd, buf, count);
		if (err < 0) {
			printk(KERN_ERR "hppfs_read: read failed: %d\n", err);
			return err;
		}
		count = err;
290
		if (count > 0)
L
Linus Torvalds 已提交
291 292 293 294
			*ppos += count;
	}
	else count = read_proc(hppfs->proc_file, buf, count, ppos, 1);

295
	return count;
L
Linus Torvalds 已提交
296 297
}

J
Jeff Dike 已提交
298 299
static ssize_t hppfs_write(struct file *file, const char __user *buf,
			   size_t len, loff_t *ppos)
L
Linus Torvalds 已提交
300 301 302
{
	struct hppfs_private *data = file->private_data;
	struct file *proc_file = data->proc_file;
A
Al Viro 已提交
303
	ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
L
Linus Torvalds 已提交
304

J
Josef Sipek 已提交
305
	write = proc_file->f_path.dentry->d_inode->i_fop->write;
J
Jeff Dike 已提交
306
	return (*write)(proc_file, buf, len, ppos);
L
Linus Torvalds 已提交
307 308 309 310 311 312 313 314 315 316 317
}

static int open_host_sock(char *host_file, int *filter_out)
{
	char *end;
	int fd;

	end = &host_file[strlen(host_file)];
	strcpy(end, "/rw");
	*filter_out = 1;
	fd = os_connect_socket(host_file);
318 319
	if (fd > 0)
		return fd;
L
Linus Torvalds 已提交
320 321 322 323

	strcpy(end, "/r");
	*filter_out = 0;
	fd = os_connect_socket(host_file);
324
	return fd;
L
Linus Torvalds 已提交
325 326 327 328 329 330 331
}

static void free_contents(struct hppfs_data *head)
{
	struct hppfs_data *data;
	struct list_head *ele, *next;

332 333
	if (head == NULL)
		return;
L
Linus Torvalds 已提交
334

335
	list_for_each_safe(ele, next, &head->list) {
L
Linus Torvalds 已提交
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
		data = list_entry(ele, struct hppfs_data, list);
		kfree(data);
	}
	kfree(head);
}

static struct hppfs_data *hppfs_get_data(int fd, int filter,
					 struct file *proc_file,
					 struct file *hppfs_file,
					 loff_t *size_out)
{
	struct hppfs_data *data, *new, *head;
	int n, err;

	err = -ENOMEM;
	data = kmalloc(sizeof(*data), GFP_KERNEL);
352 353
	if (data == NULL) {
		printk(KERN_ERR "hppfs_get_data : head allocation failed\n");
L
Linus Torvalds 已提交
354 355 356 357 358 359 360 361
		goto failed;
	}

	INIT_LIST_HEAD(&data->list);

	head = data;
	*size_out = 0;

362 363
	if (filter) {
		while ((n = read_proc(proc_file, data->contents,
J
Jeff Dike 已提交
364
				      sizeof(data->contents), NULL, 0)) > 0)
L
Linus Torvalds 已提交
365 366
			os_write_file(fd, data->contents, n);
		err = os_shutdown_socket(fd, 0, 1);
367 368
		if (err) {
			printk(KERN_ERR "hppfs_get_data : failed to shut down "
L
Linus Torvalds 已提交
369 370 371 372
			       "socket\n");
			goto failed_free;
		}
	}
373
	while (1) {
L
Linus Torvalds 已提交
374
		n = os_read_file(fd, data->contents, sizeof(data->contents));
375
		if (n < 0) {
L
Linus Torvalds 已提交
376
			err = n;
377 378
			printk(KERN_ERR "hppfs_get_data : read failed, "
			       "errno = %d\n", err);
L
Linus Torvalds 已提交
379
			goto failed_free;
380
		} else if (n == 0)
L
Linus Torvalds 已提交
381 382 383 384
			break;

		*size_out += n;

385
		if (n < sizeof(data->contents))
L
Linus Torvalds 已提交
386 387 388
			break;

		new = kmalloc(sizeof(*data), GFP_KERNEL);
389 390 391
		if (new == 0) {
			printk(KERN_ERR "hppfs_get_data : data allocation "
			       "failed\n");
L
Linus Torvalds 已提交
392 393 394 395 396 397 398 399
			err = -ENOMEM;
			goto failed_free;
		}

		INIT_LIST_HEAD(&new->list);
		list_add(&new->list, &data->list);
		data = new;
	}
400
	return head;
L
Linus Torvalds 已提交
401 402 403 404

 failed_free:
	free_contents(head);
 failed:
405
	return ERR_PTR(err);
L
Linus Torvalds 已提交
406 407 408 409 410 411 412
}

static struct hppfs_private *hppfs_data(void)
{
	struct hppfs_private *data;

	data = kmalloc(sizeof(*data), GFP_KERNEL);
413 414
	if (data == NULL)
		return data;
L
Linus Torvalds 已提交
415 416 417 418

	*data = ((struct hppfs_private ) { .host_fd  		= -1,
					   .len  		= -1,
					   .contents 		= NULL } );
419
	return data;
L
Linus Torvalds 已提交
420 421 422 423
}

static int file_mode(int fmode)
{
424 425 426 427 428 429 430
	if (fmode == (FMODE_READ | FMODE_WRITE))
		return O_RDWR;
	if (fmode == FMODE_READ)
		return O_RDONLY;
	if (fmode == FMODE_WRITE)
		return O_WRONLY;
	return 0;
L
Linus Torvalds 已提交
431 432 433 434
}

static int hppfs_open(struct inode *inode, struct file *file)
{
D
David Howells 已提交
435
	const struct cred *cred = file->f_cred;
L
Linus Torvalds 已提交
436
	struct hppfs_private *data;
437
	struct vfsmount *proc_mnt;
J
Jeff Dike 已提交
438
	struct dentry *proc_dentry;
L
Linus Torvalds 已提交
439 440 441 442 443
	char *host_file;
	int err, fd, type, filter;

	err = -ENOMEM;
	data = hppfs_data();
444
	if (data == NULL)
L
Linus Torvalds 已提交
445 446
		goto out;

J
Josef Sipek 已提交
447
	host_file = dentry_name(file->f_path.dentry, strlen("/rw"));
448
	if (host_file == NULL)
L
Linus Torvalds 已提交
449 450 451
		goto out_free2;

	proc_dentry = HPPFS_I(inode)->proc_dentry;
A
Al Viro 已提交
452
	proc_mnt = inode->i_sb->s_fs_info;
L
Linus Torvalds 已提交
453 454

	/* XXX This isn't closed anywhere */
455
	data->proc_file = dentry_open(dget(proc_dentry), mntget(proc_mnt),
456
				      file_mode(file->f_mode), cred);
L
Linus Torvalds 已提交
457
	err = PTR_ERR(data->proc_file);
458
	if (IS_ERR(data->proc_file))
L
Linus Torvalds 已提交
459 460 461
		goto out_free1;

	type = os_file_type(host_file);
462
	if (type == OS_TYPE_FILE) {
L
Linus Torvalds 已提交
463
		fd = os_open_file(host_file, of_read(OPENFLAGS()), 0);
464
		if (fd >= 0)
L
Linus Torvalds 已提交
465
			data->host_fd = fd;
466 467 468
		else
			printk(KERN_ERR "hppfs_open : failed to open '%s', "
			       "errno = %d\n", host_file, -fd);
L
Linus Torvalds 已提交
469 470

		data->contents = NULL;
471
	} else if (type == OS_TYPE_DIR) {
L
Linus Torvalds 已提交
472
		fd = open_host_sock(host_file, &filter);
473
		if (fd > 0) {
L
Linus Torvalds 已提交
474
			data->contents = hppfs_get_data(fd, filter,
475
							data->proc_file,
L
Linus Torvalds 已提交
476
							file, &data->len);
477
			if (!IS_ERR(data->contents))
L
Linus Torvalds 已提交
478
				data->host_fd = fd;
479 480 481
		} else
			printk(KERN_ERR "hppfs_open : failed to open a socket "
			       "in '%s', errno = %d\n", host_file, -fd);
L
Linus Torvalds 已提交
482 483 484 485
	}
	kfree(host_file);

	file->private_data = data;
486
	return 0;
L
Linus Torvalds 已提交
487 488 489 490 491 492 493

 out_free1:
	kfree(host_file);
 out_free2:
	free_contents(data->contents);
	kfree(data);
 out:
494
	return err;
L
Linus Torvalds 已提交
495 496 497 498
}

static int hppfs_dir_open(struct inode *inode, struct file *file)
{
D
David Howells 已提交
499
	const struct cred *cred = file->f_cred;
L
Linus Torvalds 已提交
500
	struct hppfs_private *data;
501
	struct vfsmount *proc_mnt;
J
Jeff Dike 已提交
502
	struct dentry *proc_dentry;
L
Linus Torvalds 已提交
503 504 505 506
	int err;

	err = -ENOMEM;
	data = hppfs_data();
507
	if (data == NULL)
L
Linus Torvalds 已提交
508 509 510
		goto out;

	proc_dentry = HPPFS_I(inode)->proc_dentry;
A
Al Viro 已提交
511
	proc_mnt = inode->i_sb->s_fs_info;
512
	data->proc_file = dentry_open(dget(proc_dentry), mntget(proc_mnt),
513
				      file_mode(file->f_mode), cred);
L
Linus Torvalds 已提交
514
	err = PTR_ERR(data->proc_file);
515
	if (IS_ERR(data->proc_file))
L
Linus Torvalds 已提交
516 517 518
		goto out_free;

	file->private_data = data;
519
	return 0;
L
Linus Torvalds 已提交
520 521 522 523

 out_free:
	kfree(data);
 out:
524
	return err;
L
Linus Torvalds 已提交
525 526 527 528 529
}

static loff_t hppfs_llseek(struct file *file, loff_t off, int where)
{
	struct hppfs_private *data = file->private_data;
530
	struct file *proc_file = data->proc_file;
L
Linus Torvalds 已提交
531 532 533
	loff_t (*llseek)(struct file *, loff_t, int);
	loff_t ret;

J
Josef Sipek 已提交
534
	llseek = proc_file->f_path.dentry->d_inode->i_fop->llseek;
535
	if (llseek != NULL) {
L
Linus Torvalds 已提交
536
		ret = (*llseek)(proc_file, off, where);
537 538
		if (ret < 0)
			return ret;
L
Linus Torvalds 已提交
539 540
	}

541
	return default_llseek(file, off, where);
L
Linus Torvalds 已提交
542 543
}

544
static const struct file_operations hppfs_file_fops = {
L
Linus Torvalds 已提交
545 546 547 548 549 550 551 552 553 554 555 556 557 558
	.owner		= NULL,
	.llseek		= hppfs_llseek,
	.read		= hppfs_read,
	.write		= hppfs_write,
	.open		= hppfs_open,
};

struct hppfs_dirent {
	void *vfs_dirent;
	filldir_t filldir;
	struct dentry *dentry;
};

static int hppfs_filldir(void *d, const char *name, int size,
559
			 loff_t offset, u64 inode, unsigned int type)
L
Linus Torvalds 已提交
560 561 562
{
	struct hppfs_dirent *dirent = d;

563 564
	if (file_removed(dirent->dentry, name))
		return 0;
L
Linus Torvalds 已提交
565

566 567
	return (*dirent->filldir)(dirent->vfs_dirent, name, size, offset,
				  inode, type);
L
Linus Torvalds 已提交
568 569 570 571 572
}

static int hppfs_readdir(struct file *file, void *ent, filldir_t filldir)
{
	struct hppfs_private *data = file->private_data;
573
	struct file *proc_file = data->proc_file;
L
Linus Torvalds 已提交
574 575 576 577
	int (*readdir)(struct file *, void *, filldir_t);
	struct hppfs_dirent dirent = ((struct hppfs_dirent)
		                      { .vfs_dirent  	= ent,
					.filldir 	= filldir,
578 579
					.dentry  	= file->f_path.dentry
				      });
L
Linus Torvalds 已提交
580 581
	int err;

J
Josef Sipek 已提交
582
	readdir = proc_file->f_path.dentry->d_inode->i_fop->readdir;
L
Linus Torvalds 已提交
583 584 585 586 587

	proc_file->f_pos = file->f_pos;
	err = (*readdir)(proc_file, &dirent, hppfs_filldir);
	file->f_pos = proc_file->f_pos;

588
	return err;
L
Linus Torvalds 已提交
589 590
}

591
static int hppfs_fsync(struct file *file, int datasync)
L
Linus Torvalds 已提交
592
{
593
	return 0;
L
Linus Torvalds 已提交
594 595
}

596
static const struct file_operations hppfs_dir_fops = {
L
Linus Torvalds 已提交
597 598 599 600
	.owner		= NULL,
	.readdir	= hppfs_readdir,
	.open		= hppfs_dir_open,
	.fsync		= hppfs_fsync,
601
	.llseek		= default_llseek,
L
Linus Torvalds 已提交
602 603
};

604
static int hppfs_statfs(struct dentry *dentry, struct kstatfs *sf)
L
Linus Torvalds 已提交
605 606 607 608 609 610 611
{
	sf->f_blocks = 0;
	sf->f_bfree = 0;
	sf->f_bavail = 0;
	sf->f_files = 0;
	sf->f_ffree = 0;
	sf->f_type = HPPFS_SUPER_MAGIC;
612
	return 0;
L
Linus Torvalds 已提交
613 614 615 616 617 618 619
}

static struct inode *hppfs_alloc_inode(struct super_block *sb)
{
	struct hppfs_inode_info *hi;

	hi = kmalloc(sizeof(*hi), GFP_KERNEL);
620 621
	if (!hi)
		return NULL;
L
Linus Torvalds 已提交
622

623
	hi->proc_dentry = NULL;
L
Linus Torvalds 已提交
624
	inode_init_once(&hi->vfs_inode);
625
	return &hi->vfs_inode;
L
Linus Torvalds 已提交
626 627
}

A
Al Viro 已提交
628
void hppfs_evict_inode(struct inode *ino)
L
Linus Torvalds 已提交
629
{
A
Al Viro 已提交
630
	end_writeback(ino);
J
Jeff Dike 已提交
631 632
	dput(HPPFS_I(ino)->proc_dentry);
	mntput(ino->i_sb->s_fs_info);
L
Linus Torvalds 已提交
633 634 635 636 637 638 639
}

static void hppfs_destroy_inode(struct inode *inode)
{
	kfree(HPPFS_I(inode));
}

640
static const struct super_operations hppfs_sbops = {
L
Linus Torvalds 已提交
641 642
	.alloc_inode	= hppfs_alloc_inode,
	.destroy_inode	= hppfs_destroy_inode,
A
Al Viro 已提交
643
	.evict_inode	= hppfs_evict_inode,
L
Linus Torvalds 已提交
644 645 646
	.statfs		= hppfs_statfs,
};

647 648
static int hppfs_readlink(struct dentry *dentry, char __user *buffer,
			  int buflen)
L
Linus Torvalds 已提交
649
{
A
Al Viro 已提交
650
	struct dentry *proc_dentry = HPPFS_I(dentry->d_inode)->proc_dentry;
J
Jeff Dike 已提交
651 652
	return proc_dentry->d_inode->i_op->readlink(proc_dentry, buffer,
						    buflen);
L
Linus Torvalds 已提交
653 654
}

J
Jeff Dike 已提交
655
static void *hppfs_follow_link(struct dentry *dentry, struct nameidata *nd)
L
Linus Torvalds 已提交
656
{
A
Al Viro 已提交
657
	struct dentry *proc_dentry = HPPFS_I(dentry->d_inode)->proc_dentry;
L
Linus Torvalds 已提交
658

J
Jeff Dike 已提交
659 660
	return proc_dentry->d_inode->i_op->follow_link(proc_dentry, nd);
}
L
Linus Torvalds 已提交
661

A
Al Viro 已提交
662 663 664 665 666 667 668 669 670
static void hppfs_put_link(struct dentry *dentry, struct nameidata *nd,
			   void *cookie)
{
	struct dentry *proc_dentry = HPPFS_I(dentry->d_inode)->proc_dentry;

	if (proc_dentry->d_inode->i_op->put_link)
		proc_dentry->d_inode->i_op->put_link(proc_dentry, nd, cookie);
}

671
static const struct inode_operations hppfs_dir_iops = {
L
Linus Torvalds 已提交
672 673 674
	.lookup		= hppfs_lookup,
};

675
static const struct inode_operations hppfs_link_iops = {
L
Linus Torvalds 已提交
676 677
	.readlink	= hppfs_readlink,
	.follow_link	= hppfs_follow_link,
A
Al Viro 已提交
678
	.put_link	= hppfs_put_link,
L
Linus Torvalds 已提交
679 680
};

A
Al Viro 已提交
681
static struct inode *get_inode(struct super_block *sb, struct dentry *dentry)
L
Linus Torvalds 已提交
682
{
A
Al Viro 已提交
683 684 685 686 687 688
	struct inode *proc_ino = dentry->d_inode;
	struct inode *inode = new_inode(sb);

	if (!inode)
		return ERR_PTR(-ENOMEM);

689
	if (S_ISDIR(dentry->d_inode->i_mode)) {
L
Linus Torvalds 已提交
690 691
		inode->i_op = &hppfs_dir_iops;
		inode->i_fop = &hppfs_dir_fops;
692
	} else if (S_ISLNK(dentry->d_inode->i_mode)) {
L
Linus Torvalds 已提交
693 694
		inode->i_op = &hppfs_link_iops;
		inode->i_fop = &hppfs_file_fops;
695
	} else {
L
Linus Torvalds 已提交
696 697 698 699
		inode->i_op = &hppfs_file_iops;
		inode->i_fop = &hppfs_file_fops;
	}

J
Jeff Dike 已提交
700
	HPPFS_I(inode)->proc_dentry = dget(dentry);
A
Al Viro 已提交
701 702 703 704 705 706 707 708 709 710 711

	inode->i_uid = proc_ino->i_uid;
	inode->i_gid = proc_ino->i_gid;
	inode->i_atime = proc_ino->i_atime;
	inode->i_mtime = proc_ino->i_mtime;
	inode->i_ctime = proc_ino->i_ctime;
	inode->i_ino = proc_ino->i_ino;
	inode->i_mode = proc_ino->i_mode;
	inode->i_nlink = proc_ino->i_nlink;
	inode->i_size = proc_ino->i_size;
	inode->i_blocks = proc_ino->i_blocks;
L
Linus Torvalds 已提交
712

J
Jeff Dike 已提交
713
	return inode;
L
Linus Torvalds 已提交
714 715 716 717 718
}

static int hppfs_fill_super(struct super_block *sb, void *d, int silent)
{
	struct inode *root_inode;
719
	struct vfsmount *proc_mnt;
A
Al Viro 已提交
720
	int err = -ENOENT;
L
Linus Torvalds 已提交
721

722
	proc_mnt = mntget(current->nsproxy->pid_ns->proc_mnt);
723
	if (IS_ERR(proc_mnt))
L
Linus Torvalds 已提交
724 725 726 727 728 729
		goto out;

	sb->s_blocksize = 1024;
	sb->s_blocksize_bits = 10;
	sb->s_magic = HPPFS_SUPER_MAGIC;
	sb->s_op = &hppfs_sbops;
A
Al Viro 已提交
730
	sb->s_fs_info = proc_mnt;
L
Linus Torvalds 已提交
731 732

	err = -ENOMEM;
A
Al Viro 已提交
733 734 735 736
	root_inode = get_inode(sb, proc_mnt->mnt_sb->s_root);
	if (!root_inode)
		goto out_mntput;

L
Linus Torvalds 已提交
737
	sb->s_root = d_alloc_root(root_inode);
738 739
	if (!sb->s_root)
		goto out_iput;
L
Linus Torvalds 已提交
740

741
	return 0;
L
Linus Torvalds 已提交
742

743
 out_iput:
L
Linus Torvalds 已提交
744
	iput(root_inode);
745 746
 out_mntput:
	mntput(proc_mnt);
L
Linus Torvalds 已提交
747 748 749 750
 out:
	return(err);
}

751 752 753
static int hppfs_read_super(struct file_system_type *type,
			    int flags, const char *dev_name,
			    void *data, struct vfsmount *mnt)
L
Linus Torvalds 已提交
754
{
755
	return get_sb_nodev(type, flags, data, hppfs_fill_super, mnt);
L
Linus Torvalds 已提交
756 757 758 759 760 761 762 763 764 765 766 767
}

static struct file_system_type hppfs_type = {
	.owner 		= THIS_MODULE,
	.name 		= "hppfs",
	.get_sb 	= hppfs_read_super,
	.kill_sb	= kill_anon_super,
	.fs_flags 	= 0,
};

static int __init init_hppfs(void)
{
768
	return register_filesystem(&hppfs_type);
L
Linus Torvalds 已提交
769 770 771 772 773 774 775 776 777 778
}

static void __exit exit_hppfs(void)
{
	unregister_filesystem(&hppfs_type);
}

module_init(init_hppfs)
module_exit(exit_hppfs)
MODULE_LICENSE("GPL");