hppfs.c 16.6 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
	struct dentry *proc_dentry, *parent;
	struct qstr *name = &dentry->d_name;
L
Linus Torvalds 已提交
144 145 146 147
	struct inode *inode;
	int err, deleted;

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

	parent = HPPFS_I(ino)->proc_dentry;
154
	mutex_lock(&parent->d_inode->i_mutex);
155
	proc_dentry = lookup_one_len(name->name, parent, name->len);
156
	mutex_unlock(&parent->d_inode->i_mutex);
L
Linus Torvalds 已提交
157

158 159
	if (IS_ERR(proc_dentry))
		return proc_dentry;
L
Linus Torvalds 已提交
160

A
Al Viro 已提交
161 162 163
	err = -ENOMEM;
	inode = get_inode(ino->i_sb, proc_dentry);
	if (!inode)
A
Al Viro 已提交
164
		goto out;
L
Linus Torvalds 已提交
165 166

 	d_add(dentry, inode);
167
	return NULL;
L
Linus Torvalds 已提交
168 169

 out:
170
	return ERR_PTR(err);
L
Linus Torvalds 已提交
171 172
}

173
static const struct inode_operations hppfs_file_iops = {
L
Linus Torvalds 已提交
174 175
};

A
Al Viro 已提交
176
static ssize_t read_proc(struct file *file, char __user *buf, ssize_t count,
L
Linus Torvalds 已提交
177 178
			 loff_t *ppos, int is_user)
{
A
Al Viro 已提交
179
	ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
L
Linus Torvalds 已提交
180 181
	ssize_t n;

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

184
	if (!is_user)
L
Linus Torvalds 已提交
185 186 187 188
		set_fs(KERNEL_DS);

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

189
	if (!is_user)
L
Linus Torvalds 已提交
190 191
		set_fs(USER_DS);

192 193
	if (ppos)
		*ppos = file->f_pos;
194
	return n;
L
Linus Torvalds 已提交
195 196
}

A
Al Viro 已提交
197
static ssize_t hppfs_read_file(int fd, char __user *buf, ssize_t count)
L
Linus Torvalds 已提交
198 199 200 201 202 203 204
{
	ssize_t n;
	int cur, err;
	char *new_buf;

	n = -ENOMEM;
	new_buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
205 206
	if (new_buf == NULL) {
		printk(KERN_ERR "hppfs_read_file : kmalloc failed\n");
L
Linus Torvalds 已提交
207 208 209
		goto out;
	}
	n = 0;
210
	while (count > 0) {
L
Linus Torvalds 已提交
211 212
		cur = min_t(ssize_t, count, PAGE_SIZE);
		err = os_read_file(fd, new_buf, cur);
213 214 215
		if (err < 0) {
			printk(KERN_ERR "hppfs_read : read failed, "
			       "errno = %d\n", err);
L
Linus Torvalds 已提交
216 217
			n = err;
			goto out_free;
218
		} else if (err == 0)
L
Linus Torvalds 已提交
219 220
			break;

221
		if (copy_to_user(buf, new_buf, err)) {
L
Linus Torvalds 已提交
222 223 224 225 226 227 228 229 230
			n = -EFAULT;
			goto out_free;
		}
		n += err;
		count -= err;
	}
 out_free:
	kfree(new_buf);
 out:
231
	return n;
L
Linus Torvalds 已提交
232 233
}

A
Al Viro 已提交
234
static ssize_t hppfs_read(struct file *file, char __user *buf, size_t count,
L
Linus Torvalds 已提交
235 236 237 238 239 240 241
			  loff_t *ppos)
{
	struct hppfs_private *hppfs = file->private_data;
	struct hppfs_data *data;
	loff_t off;
	int err;

242
	if (hppfs->contents != NULL) {
J
Jeff Dike 已提交
243 244
		int rem;

245 246
		if (*ppos >= hppfs->len)
			return 0;
L
Linus Torvalds 已提交
247 248 249

		data = hppfs->contents;
		off = *ppos;
250
		while (off >= sizeof(data->contents)) {
L
Linus Torvalds 已提交
251 252 253 254 255
			data = list_entry(data->list.next, struct hppfs_data,
					  list);
			off -= sizeof(data->contents);
		}

256
		if (off + count > hppfs->len)
L
Linus Torvalds 已提交
257
			count = hppfs->len - off;
J
Jeff Dike 已提交
258 259 260 261
		rem = copy_to_user(buf, &data->contents[off], count);
		*ppos += count - rem;
		if (rem > 0)
			return -EFAULT;
262
	} else if (hppfs->host_fd != -1) {
L
Linus Torvalds 已提交
263
		err = os_seek_file(hppfs->host_fd, *ppos);
264 265 266 267
		if (err) {
			printk(KERN_ERR "hppfs_read : seek failed, "
			       "errno = %d\n", err);
			return err;
L
Linus Torvalds 已提交
268
		}
269 270 271 272 273 274
		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;
275
		if (count > 0)
L
Linus Torvalds 已提交
276 277 278 279
			*ppos += count;
	}
	else count = read_proc(hppfs->proc_file, buf, count, ppos, 1);

280
	return count;
L
Linus Torvalds 已提交
281 282
}

J
Jeff Dike 已提交
283 284
static ssize_t hppfs_write(struct file *file, const char __user *buf,
			   size_t len, loff_t *ppos)
L
Linus Torvalds 已提交
285 286 287
{
	struct hppfs_private *data = file->private_data;
	struct file *proc_file = data->proc_file;
A
Al Viro 已提交
288
	ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
L
Linus Torvalds 已提交
289

J
Josef Sipek 已提交
290
	write = proc_file->f_path.dentry->d_inode->i_fop->write;
J
Jeff Dike 已提交
291
	return (*write)(proc_file, buf, len, ppos);
L
Linus Torvalds 已提交
292 293 294 295 296 297 298 299 300 301 302
}

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);
303 304
	if (fd > 0)
		return fd;
L
Linus Torvalds 已提交
305 306 307 308

	strcpy(end, "/r");
	*filter_out = 0;
	fd = os_connect_socket(host_file);
309
	return fd;
L
Linus Torvalds 已提交
310 311 312 313 314 315 316
}

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

317 318
	if (head == NULL)
		return;
L
Linus Torvalds 已提交
319

320
	list_for_each_safe(ele, next, &head->list) {
L
Linus Torvalds 已提交
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
		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);
337 338
	if (data == NULL) {
		printk(KERN_ERR "hppfs_get_data : head allocation failed\n");
L
Linus Torvalds 已提交
339 340 341 342 343 344 345 346
		goto failed;
	}

	INIT_LIST_HEAD(&data->list);

	head = data;
	*size_out = 0;

347 348
	if (filter) {
		while ((n = read_proc(proc_file, data->contents,
J
Jeff Dike 已提交
349
				      sizeof(data->contents), NULL, 0)) > 0)
L
Linus Torvalds 已提交
350 351
			os_write_file(fd, data->contents, n);
		err = os_shutdown_socket(fd, 0, 1);
352 353
		if (err) {
			printk(KERN_ERR "hppfs_get_data : failed to shut down "
L
Linus Torvalds 已提交
354 355 356 357
			       "socket\n");
			goto failed_free;
		}
	}
358
	while (1) {
L
Linus Torvalds 已提交
359
		n = os_read_file(fd, data->contents, sizeof(data->contents));
360
		if (n < 0) {
L
Linus Torvalds 已提交
361
			err = n;
362 363
			printk(KERN_ERR "hppfs_get_data : read failed, "
			       "errno = %d\n", err);
L
Linus Torvalds 已提交
364
			goto failed_free;
365
		} else if (n == 0)
L
Linus Torvalds 已提交
366 367 368 369
			break;

		*size_out += n;

370
		if (n < sizeof(data->contents))
L
Linus Torvalds 已提交
371 372 373
			break;

		new = kmalloc(sizeof(*data), GFP_KERNEL);
374 375 376
		if (new == 0) {
			printk(KERN_ERR "hppfs_get_data : data allocation "
			       "failed\n");
L
Linus Torvalds 已提交
377 378 379 380 381 382 383 384
			err = -ENOMEM;
			goto failed_free;
		}

		INIT_LIST_HEAD(&new->list);
		list_add(&new->list, &data->list);
		data = new;
	}
385
	return head;
L
Linus Torvalds 已提交
386 387 388 389

 failed_free:
	free_contents(head);
 failed:
390
	return ERR_PTR(err);
L
Linus Torvalds 已提交
391 392 393 394 395 396 397
}

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

	data = kmalloc(sizeof(*data), GFP_KERNEL);
398 399
	if (data == NULL)
		return data;
L
Linus Torvalds 已提交
400 401 402 403

	*data = ((struct hppfs_private ) { .host_fd  		= -1,
					   .len  		= -1,
					   .contents 		= NULL } );
404
	return data;
L
Linus Torvalds 已提交
405 406 407 408
}

static int file_mode(int fmode)
{
409 410 411 412 413 414 415
	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 已提交
416 417 418 419
}

static int hppfs_open(struct inode *inode, struct file *file)
{
D
David Howells 已提交
420
	const struct cred *cred = file->f_cred;
L
Linus Torvalds 已提交
421
	struct hppfs_private *data;
422
	struct vfsmount *proc_mnt;
J
Jeff Dike 已提交
423
	struct dentry *proc_dentry;
L
Linus Torvalds 已提交
424 425 426 427 428
	char *host_file;
	int err, fd, type, filter;

	err = -ENOMEM;
	data = hppfs_data();
429
	if (data == NULL)
L
Linus Torvalds 已提交
430 431
		goto out;

J
Josef Sipek 已提交
432
	host_file = dentry_name(file->f_path.dentry, strlen("/rw"));
433
	if (host_file == NULL)
L
Linus Torvalds 已提交
434 435 436
		goto out_free2;

	proc_dentry = HPPFS_I(inode)->proc_dentry;
A
Al Viro 已提交
437
	proc_mnt = inode->i_sb->s_fs_info;
L
Linus Torvalds 已提交
438 439

	/* XXX This isn't closed anywhere */
440
	data->proc_file = dentry_open(dget(proc_dentry), mntget(proc_mnt),
441
				      file_mode(file->f_mode), cred);
L
Linus Torvalds 已提交
442
	err = PTR_ERR(data->proc_file);
443
	if (IS_ERR(data->proc_file))
L
Linus Torvalds 已提交
444 445 446
		goto out_free1;

	type = os_file_type(host_file);
447
	if (type == OS_TYPE_FILE) {
L
Linus Torvalds 已提交
448
		fd = os_open_file(host_file, of_read(OPENFLAGS()), 0);
449
		if (fd >= 0)
L
Linus Torvalds 已提交
450
			data->host_fd = fd;
451 452 453
		else
			printk(KERN_ERR "hppfs_open : failed to open '%s', "
			       "errno = %d\n", host_file, -fd);
L
Linus Torvalds 已提交
454 455

		data->contents = NULL;
456
	} else if (type == OS_TYPE_DIR) {
L
Linus Torvalds 已提交
457
		fd = open_host_sock(host_file, &filter);
458
		if (fd > 0) {
L
Linus Torvalds 已提交
459
			data->contents = hppfs_get_data(fd, filter,
460
							data->proc_file,
L
Linus Torvalds 已提交
461
							file, &data->len);
462
			if (!IS_ERR(data->contents))
L
Linus Torvalds 已提交
463
				data->host_fd = fd;
464 465 466
		} else
			printk(KERN_ERR "hppfs_open : failed to open a socket "
			       "in '%s', errno = %d\n", host_file, -fd);
L
Linus Torvalds 已提交
467 468 469 470
	}
	kfree(host_file);

	file->private_data = data;
471
	return 0;
L
Linus Torvalds 已提交
472 473 474 475 476 477 478

 out_free1:
	kfree(host_file);
 out_free2:
	free_contents(data->contents);
	kfree(data);
 out:
479
	return err;
L
Linus Torvalds 已提交
480 481 482 483
}

static int hppfs_dir_open(struct inode *inode, struct file *file)
{
D
David Howells 已提交
484
	const struct cred *cred = file->f_cred;
L
Linus Torvalds 已提交
485
	struct hppfs_private *data;
486
	struct vfsmount *proc_mnt;
J
Jeff Dike 已提交
487
	struct dentry *proc_dentry;
L
Linus Torvalds 已提交
488 489 490 491
	int err;

	err = -ENOMEM;
	data = hppfs_data();
492
	if (data == NULL)
L
Linus Torvalds 已提交
493 494 495
		goto out;

	proc_dentry = HPPFS_I(inode)->proc_dentry;
A
Al Viro 已提交
496
	proc_mnt = inode->i_sb->s_fs_info;
497
	data->proc_file = dentry_open(dget(proc_dentry), mntget(proc_mnt),
498
				      file_mode(file->f_mode), cred);
L
Linus Torvalds 已提交
499
	err = PTR_ERR(data->proc_file);
500
	if (IS_ERR(data->proc_file))
L
Linus Torvalds 已提交
501 502 503
		goto out_free;

	file->private_data = data;
504
	return 0;
L
Linus Torvalds 已提交
505 506 507 508

 out_free:
	kfree(data);
 out:
509
	return err;
L
Linus Torvalds 已提交
510 511 512 513 514
}

static loff_t hppfs_llseek(struct file *file, loff_t off, int where)
{
	struct hppfs_private *data = file->private_data;
515
	struct file *proc_file = data->proc_file;
L
Linus Torvalds 已提交
516 517 518
	loff_t (*llseek)(struct file *, loff_t, int);
	loff_t ret;

J
Josef Sipek 已提交
519
	llseek = proc_file->f_path.dentry->d_inode->i_fop->llseek;
520
	if (llseek != NULL) {
L
Linus Torvalds 已提交
521
		ret = (*llseek)(proc_file, off, where);
522 523
		if (ret < 0)
			return ret;
L
Linus Torvalds 已提交
524 525
	}

526
	return default_llseek(file, off, where);
L
Linus Torvalds 已提交
527 528
}

529
static const struct file_operations hppfs_file_fops = {
L
Linus Torvalds 已提交
530 531 532 533 534 535 536 537 538 539 540 541 542 543
	.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,
544
			 loff_t offset, u64 inode, unsigned int type)
L
Linus Torvalds 已提交
545 546 547
{
	struct hppfs_dirent *dirent = d;

548 549
	if (file_removed(dirent->dentry, name))
		return 0;
L
Linus Torvalds 已提交
550

551 552
	return (*dirent->filldir)(dirent->vfs_dirent, name, size, offset,
				  inode, type);
L
Linus Torvalds 已提交
553 554 555 556 557
}

static int hppfs_readdir(struct file *file, void *ent, filldir_t filldir)
{
	struct hppfs_private *data = file->private_data;
558
	struct file *proc_file = data->proc_file;
L
Linus Torvalds 已提交
559 560 561 562
	int (*readdir)(struct file *, void *, filldir_t);
	struct hppfs_dirent dirent = ((struct hppfs_dirent)
		                      { .vfs_dirent  	= ent,
					.filldir 	= filldir,
563 564
					.dentry  	= file->f_path.dentry
				      });
L
Linus Torvalds 已提交
565 566
	int err;

J
Josef Sipek 已提交
567
	readdir = proc_file->f_path.dentry->d_inode->i_fop->readdir;
L
Linus Torvalds 已提交
568 569 570 571 572

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

573
	return err;
L
Linus Torvalds 已提交
574 575
}

576 577
static int hppfs_fsync(struct file *file, loff_t start, loff_t end,
		       int datasync)
L
Linus Torvalds 已提交
578
{
579
	return filemap_write_and_wait_range(file->f_mapping, start, end);
L
Linus Torvalds 已提交
580 581
}

582
static const struct file_operations hppfs_dir_fops = {
L
Linus Torvalds 已提交
583 584 585 586
	.owner		= NULL,
	.readdir	= hppfs_readdir,
	.open		= hppfs_dir_open,
	.fsync		= hppfs_fsync,
587
	.llseek		= default_llseek,
L
Linus Torvalds 已提交
588 589
};

590
static int hppfs_statfs(struct dentry *dentry, struct kstatfs *sf)
L
Linus Torvalds 已提交
591 592 593 594 595 596 597
{
	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;
598
	return 0;
L
Linus Torvalds 已提交
599 600 601 602 603 604 605
}

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

	hi = kmalloc(sizeof(*hi), GFP_KERNEL);
606 607
	if (!hi)
		return NULL;
L
Linus Torvalds 已提交
608

609
	hi->proc_dentry = NULL;
L
Linus Torvalds 已提交
610
	inode_init_once(&hi->vfs_inode);
611
	return &hi->vfs_inode;
L
Linus Torvalds 已提交
612 613
}

A
Al Viro 已提交
614
void hppfs_evict_inode(struct inode *ino)
L
Linus Torvalds 已提交
615
{
A
Al Viro 已提交
616
	end_writeback(ino);
J
Jeff Dike 已提交
617 618
	dput(HPPFS_I(ino)->proc_dentry);
	mntput(ino->i_sb->s_fs_info);
L
Linus Torvalds 已提交
619 620
}

N
Nick Piggin 已提交
621
static void hppfs_i_callback(struct rcu_head *head)
L
Linus Torvalds 已提交
622
{
N
Nick Piggin 已提交
623 624
	struct inode *inode = container_of(head, struct inode, i_rcu);
	INIT_LIST_HEAD(&inode->i_dentry);
L
Linus Torvalds 已提交
625 626 627
	kfree(HPPFS_I(inode));
}

N
Nick Piggin 已提交
628 629 630 631 632
static void hppfs_destroy_inode(struct inode *inode)
{
	call_rcu(&inode->i_rcu, hppfs_i_callback);
}

633
static const struct super_operations hppfs_sbops = {
L
Linus Torvalds 已提交
634 635
	.alloc_inode	= hppfs_alloc_inode,
	.destroy_inode	= hppfs_destroy_inode,
A
Al Viro 已提交
636
	.evict_inode	= hppfs_evict_inode,
L
Linus Torvalds 已提交
637 638 639
	.statfs		= hppfs_statfs,
};

640 641
static int hppfs_readlink(struct dentry *dentry, char __user *buffer,
			  int buflen)
L
Linus Torvalds 已提交
642
{
A
Al Viro 已提交
643
	struct dentry *proc_dentry = HPPFS_I(dentry->d_inode)->proc_dentry;
J
Jeff Dike 已提交
644 645
	return proc_dentry->d_inode->i_op->readlink(proc_dentry, buffer,
						    buflen);
L
Linus Torvalds 已提交
646 647
}

J
Jeff Dike 已提交
648
static void *hppfs_follow_link(struct dentry *dentry, struct nameidata *nd)
L
Linus Torvalds 已提交
649
{
A
Al Viro 已提交
650
	struct dentry *proc_dentry = HPPFS_I(dentry->d_inode)->proc_dentry;
L
Linus Torvalds 已提交
651

J
Jeff Dike 已提交
652 653
	return proc_dentry->d_inode->i_op->follow_link(proc_dentry, nd);
}
L
Linus Torvalds 已提交
654

A
Al Viro 已提交
655 656 657 658 659 660 661 662 663
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);
}

664
static const struct inode_operations hppfs_dir_iops = {
L
Linus Torvalds 已提交
665 666 667
	.lookup		= hppfs_lookup,
};

668
static const struct inode_operations hppfs_link_iops = {
L
Linus Torvalds 已提交
669 670
	.readlink	= hppfs_readlink,
	.follow_link	= hppfs_follow_link,
A
Al Viro 已提交
671
	.put_link	= hppfs_put_link,
L
Linus Torvalds 已提交
672 673
};

A
Al Viro 已提交
674
static struct inode *get_inode(struct super_block *sb, struct dentry *dentry)
L
Linus Torvalds 已提交
675
{
A
Al Viro 已提交
676 677 678
	struct inode *proc_ino = dentry->d_inode;
	struct inode *inode = new_inode(sb);

A
Al Viro 已提交
679 680
	if (!inode) {
		dput(dentry);
A
Al Viro 已提交
681
		return ERR_PTR(-ENOMEM);
A
Al Viro 已提交
682
	}
A
Al Viro 已提交
683

684
	if (S_ISDIR(dentry->d_inode->i_mode)) {
L
Linus Torvalds 已提交
685 686
		inode->i_op = &hppfs_dir_iops;
		inode->i_fop = &hppfs_dir_fops;
687
	} else if (S_ISLNK(dentry->d_inode->i_mode)) {
L
Linus Torvalds 已提交
688 689
		inode->i_op = &hppfs_link_iops;
		inode->i_fop = &hppfs_file_fops;
690
	} else {
L
Linus Torvalds 已提交
691 692 693 694
		inode->i_op = &hppfs_file_iops;
		inode->i_fop = &hppfs_file_fops;
	}

A
Al Viro 已提交
695
	HPPFS_I(inode)->proc_dentry = dentry;
A
Al Viro 已提交
696 697 698 699 700 701 702 703 704 705 706

	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 已提交
707

J
Jeff Dike 已提交
708
	return inode;
L
Linus Torvalds 已提交
709 710 711 712 713
}

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

717
	proc_mnt = mntget(current->nsproxy->pid_ns->proc_mnt);
718
	if (IS_ERR(proc_mnt))
L
Linus Torvalds 已提交
719 720 721 722 723 724
		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 已提交
725
	sb->s_fs_info = proc_mnt;
L
Linus Torvalds 已提交
726 727

	err = -ENOMEM;
A
Al Viro 已提交
728
	root_inode = get_inode(sb, dget(proc_mnt->mnt_sb->s_root));
A
Al Viro 已提交
729 730 731
	if (!root_inode)
		goto out_mntput;

L
Linus Torvalds 已提交
732
	sb->s_root = d_alloc_root(root_inode);
733 734
	if (!sb->s_root)
		goto out_iput;
L
Linus Torvalds 已提交
735

736
	return 0;
L
Linus Torvalds 已提交
737

738
 out_iput:
L
Linus Torvalds 已提交
739
	iput(root_inode);
740 741
 out_mntput:
	mntput(proc_mnt);
L
Linus Torvalds 已提交
742 743 744 745
 out:
	return(err);
}

A
Al Viro 已提交
746
static struct dentry *hppfs_read_super(struct file_system_type *type,
747
			    int flags, const char *dev_name,
A
Al Viro 已提交
748
			    void *data)
L
Linus Torvalds 已提交
749
{
A
Al Viro 已提交
750
	return mount_nodev(type, flags, data, hppfs_fill_super);
L
Linus Torvalds 已提交
751 752 753 754 755
}

static struct file_system_type hppfs_type = {
	.owner 		= THIS_MODULE,
	.name 		= "hppfs",
A
Al Viro 已提交
756
	.mount 		= hppfs_read_super,
L
Linus Torvalds 已提交
757 758 759 760 761 762
	.kill_sb	= kill_anon_super,
	.fs_flags 	= 0,
};

static int __init init_hppfs(void)
{
763
	return register_filesystem(&hppfs_type);
L
Linus Torvalds 已提交
764 765 766 767 768 769 770 771 772 773
}

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

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