hppfs.c 16.3 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>
A
Al Viro 已提交
19
#include <linux/namei.h>
L
Linus Torvalds 已提交
20
#include <asm/uaccess.h>
21
#include <os.h>
L
Linus Torvalds 已提交
22

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

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 已提交
38
	struct dentry *proc_dentry;
L
Linus Torvalds 已提交
39 40 41 42 43
	struct inode vfs_inode;
};

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

#define HPPFS_SUPER_MAGIC 0xb00000ee

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

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

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

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

static char *dentry_name(struct dentry *dentry, int extra)
{
	struct dentry *parent;
	char *root, *name;
	const char *seg_name;
72
	int len, seg_len, root_len;
L
Linus Torvalds 已提交
73 74 75

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

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

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

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

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

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

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

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

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

static struct dentry *hppfs_lookup(struct inode *ino, struct dentry *dentry,
A
Al Viro 已提交
142
				   unsigned int flags)
L
Linus Torvalds 已提交
143
{
144 145
	struct dentry *proc_dentry, *parent;
	struct qstr *name = &dentry->d_name;
L
Linus Torvalds 已提交
146 147 148 149
	struct inode *inode;
	int err, deleted;

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

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

160 161
	if (IS_ERR(proc_dentry))
		return proc_dentry;
L
Linus Torvalds 已提交
162

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

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

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

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

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

A
Al Viro 已提交
184
	read = file_inode(file)->i_fop->read;
L
Linus Torvalds 已提交
185

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

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

191
	if (!is_user)
L
Linus Torvalds 已提交
192 193
		set_fs(USER_DS);

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

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

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

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

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

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

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

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

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

282
	return count;
L
Linus Torvalds 已提交
283 284
}

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

A
Al Viro 已提交
292
	write = file_inode(proc_file)->i_fop->write;
J
Jeff Dike 已提交
293
	return (*write)(proc_file, buf, len, ppos);
L
Linus Torvalds 已提交
294 295 296 297 298 299 300 301 302 303 304
}

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

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

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

319 320
	if (head == NULL)
		return;
L
Linus Torvalds 已提交
321

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

	INIT_LIST_HEAD(&data->list);

	head = data;
	*size_out = 0;

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

		*size_out += n;

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

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

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

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

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

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

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

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

static int hppfs_open(struct inode *inode, struct file *file)
{
D
David Howells 已提交
422
	const struct cred *cred = file->f_cred;
L
Linus Torvalds 已提交
423
	struct hppfs_private *data;
424
	struct path path;
L
Linus Torvalds 已提交
425 426 427 428 429
	char *host_file;
	int err, fd, type, filter;

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

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

437 438
	path.mnt = inode->i_sb->s_fs_info;
	path.dentry = HPPFS_I(inode)->proc_dentry;
L
Linus Torvalds 已提交
439

440
	data->proc_file = dentry_open(&path, file_mode(file->f_mode), cred);
L
Linus Torvalds 已提交
441
	err = PTR_ERR(data->proc_file);
442
	if (IS_ERR(data->proc_file))
L
Linus Torvalds 已提交
443 444 445
		goto out_free1;

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

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

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

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

static int hppfs_dir_open(struct inode *inode, struct file *file)
{
D
David Howells 已提交
483
	const struct cred *cred = file->f_cred;
L
Linus Torvalds 已提交
484
	struct hppfs_private *data;
485
	struct path path;
L
Linus Torvalds 已提交
486 487 488 489
	int err;

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

493 494 495
	path.mnt = inode->i_sb->s_fs_info;
	path.dentry = HPPFS_I(inode)->proc_dentry;
	data->proc_file = dentry_open(&path, file_mode(file->f_mode), cred);
L
Linus Torvalds 已提交
496
	err = PTR_ERR(data->proc_file);
497
	if (IS_ERR(data->proc_file))
L
Linus Torvalds 已提交
498 499 500
		goto out_free;

	file->private_data = data;
501
	return 0;
L
Linus Torvalds 已提交
502 503 504 505

 out_free:
	kfree(data);
 out:
506
	return err;
L
Linus Torvalds 已提交
507 508 509 510 511
}

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

A
Al Viro 已提交
516
	llseek = file_inode(proc_file)->i_fop->llseek;
517
	if (llseek != NULL) {
L
Linus Torvalds 已提交
518
		ret = (*llseek)(proc_file, off, where);
519 520
		if (ret < 0)
			return ret;
L
Linus Torvalds 已提交
521 522
	}

523
	return default_llseek(file, off, where);
L
Linus Torvalds 已提交
524 525
}

A
Al Viro 已提交
526 527 528 529 530 531 532 533 534 535
static int hppfs_release(struct inode *inode, struct file *file)
{
	struct hppfs_private *data = file->private_data;
	struct file *proc_file = data->proc_file;
	if (proc_file)
		fput(proc_file);
	kfree(data);
	return 0;
}

536
static const struct file_operations hppfs_file_fops = {
L
Linus Torvalds 已提交
537 538 539 540 541
	.owner		= NULL,
	.llseek		= hppfs_llseek,
	.read		= hppfs_read,
	.write		= hppfs_write,
	.open		= hppfs_open,
A
Al Viro 已提交
542
	.release	= hppfs_release,
L
Linus Torvalds 已提交
543 544 545
};

struct hppfs_dirent {
A
Al Viro 已提交
546 547
	struct dir_context ctx;
	struct dir_context *caller;
L
Linus Torvalds 已提交
548 549 550 551
	struct dentry *dentry;
};

static int hppfs_filldir(void *d, const char *name, int size,
552
			 loff_t offset, u64 inode, unsigned int type)
L
Linus Torvalds 已提交
553 554 555
{
	struct hppfs_dirent *dirent = d;

556 557
	if (file_removed(dirent->dentry, name))
		return 0;
L
Linus Torvalds 已提交
558

A
Al Viro 已提交
559 560
	dirent->caller->pos = dirent->ctx.pos;
	return !dir_emit(dirent->caller, name, size, inode, type);
L
Linus Torvalds 已提交
561 562
}

A
Al Viro 已提交
563
static int hppfs_readdir(struct file *file, struct dir_context *ctx)
L
Linus Torvalds 已提交
564 565
{
	struct hppfs_private *data = file->private_data;
566
	struct file *proc_file = data->proc_file;
A
Al Viro 已提交
567 568 569 570 571
	struct hppfs_dirent d = {
		.ctx.actor	= hppfs_filldir,
		.caller		= ctx,
		.dentry  	= file->f_path.dentry
	};
L
Linus Torvalds 已提交
572
	int err;
A
Al Viro 已提交
573 574 575
	proc_file->f_pos = ctx->pos;
	err = iterate_dir(proc_file, &d.ctx);
	ctx->pos = d.ctx.pos;
576
	return err;
L
Linus Torvalds 已提交
577 578
}

579
static const struct file_operations hppfs_dir_fops = {
L
Linus Torvalds 已提交
580
	.owner		= NULL,
A
Al Viro 已提交
581
	.iterate	= hppfs_readdir,
L
Linus Torvalds 已提交
582
	.open		= hppfs_dir_open,
583
	.llseek		= default_llseek,
A
Al Viro 已提交
584
	.release	= hppfs_release,
L
Linus Torvalds 已提交
585 586
};

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

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

	hi = kmalloc(sizeof(*hi), GFP_KERNEL);
603 604
	if (!hi)
		return NULL;
L
Linus Torvalds 已提交
605

606
	hi->proc_dentry = NULL;
L
Linus Torvalds 已提交
607
	inode_init_once(&hi->vfs_inode);
608
	return &hi->vfs_inode;
L
Linus Torvalds 已提交
609 610
}

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

N
Nick Piggin 已提交
618
static void hppfs_i_callback(struct rcu_head *head)
L
Linus Torvalds 已提交
619
{
N
Nick Piggin 已提交
620
	struct inode *inode = container_of(head, struct inode, i_rcu);
L
Linus Torvalds 已提交
621 622 623
	kfree(HPPFS_I(inode));
}

N
Nick Piggin 已提交
624 625 626 627 628
static void hppfs_destroy_inode(struct inode *inode)
{
	call_rcu(&inode->i_rcu, hppfs_i_callback);
}

629
static const struct super_operations hppfs_sbops = {
L
Linus Torvalds 已提交
630 631
	.alloc_inode	= hppfs_alloc_inode,
	.destroy_inode	= hppfs_destroy_inode,
A
Al Viro 已提交
632
	.evict_inode	= hppfs_evict_inode,
L
Linus Torvalds 已提交
633 634 635
	.statfs		= hppfs_statfs,
};

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

J
Jeff Dike 已提交
644
static void *hppfs_follow_link(struct dentry *dentry, struct nameidata *nd)
L
Linus Torvalds 已提交
645
{
A
Al Viro 已提交
646
	struct dentry *proc_dentry = HPPFS_I(dentry->d_inode)->proc_dentry;
L
Linus Torvalds 已提交
647

J
Jeff Dike 已提交
648 649
	return proc_dentry->d_inode->i_op->follow_link(proc_dentry, nd);
}
L
Linus Torvalds 已提交
650

A
Al Viro 已提交
651 652 653 654 655 656 657 658 659
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);
}

660
static const struct inode_operations hppfs_dir_iops = {
L
Linus Torvalds 已提交
661 662 663
	.lookup		= hppfs_lookup,
};

664
static const struct inode_operations hppfs_link_iops = {
L
Linus Torvalds 已提交
665 666
	.readlink	= hppfs_readlink,
	.follow_link	= hppfs_follow_link,
A
Al Viro 已提交
667
	.put_link	= hppfs_put_link,
L
Linus Torvalds 已提交
668 669
};

A
Al Viro 已提交
670
static struct inode *get_inode(struct super_block *sb, struct dentry *dentry)
L
Linus Torvalds 已提交
671
{
A
Al Viro 已提交
672 673 674
	struct inode *proc_ino = dentry->d_inode;
	struct inode *inode = new_inode(sb);

A
Al Viro 已提交
675 676
	if (!inode) {
		dput(dentry);
677
		return NULL;
A
Al Viro 已提交
678
	}
A
Al Viro 已提交
679

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

A
Al Viro 已提交
691
	HPPFS_I(inode)->proc_dentry = dentry;
A
Al Viro 已提交
692 693 694 695 696 697 698 699

	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;
M
Miklos Szeredi 已提交
700
	set_nlink(inode, proc_ino->i_nlink);
A
Al Viro 已提交
701 702
	inode->i_size = proc_ino->i_size;
	inode->i_blocks = proc_ino->i_blocks;
L
Linus Torvalds 已提交
703

J
Jeff Dike 已提交
704
	return inode;
L
Linus Torvalds 已提交
705 706 707 708 709
}

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

713
	proc_mnt = mntget(task_active_pid_ns(current)->proc_mnt);
714
	if (IS_ERR(proc_mnt))
L
Linus Torvalds 已提交
715 716 717 718 719 720
		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 已提交
721
	sb->s_fs_info = proc_mnt;
L
Linus Torvalds 已提交
722 723

	err = -ENOMEM;
724
	root_inode = get_inode(sb, dget(proc_mnt->mnt_root));
725
	sb->s_root = d_make_root(root_inode);
726
	if (!sb->s_root)
727
		goto out_mntput;
L
Linus Torvalds 已提交
728

729
	return 0;
L
Linus Torvalds 已提交
730

731 732
 out_mntput:
	mntput(proc_mnt);
L
Linus Torvalds 已提交
733 734 735 736
 out:
	return(err);
}

A
Al Viro 已提交
737
static struct dentry *hppfs_read_super(struct file_system_type *type,
738
			    int flags, const char *dev_name,
A
Al Viro 已提交
739
			    void *data)
L
Linus Torvalds 已提交
740
{
A
Al Viro 已提交
741
	return mount_nodev(type, flags, data, hppfs_fill_super);
L
Linus Torvalds 已提交
742 743 744 745 746
}

static struct file_system_type hppfs_type = {
	.owner 		= THIS_MODULE,
	.name 		= "hppfs",
A
Al Viro 已提交
747
	.mount 		= hppfs_read_super,
L
Linus Torvalds 已提交
748 749 750
	.kill_sb	= kill_anon_super,
	.fs_flags 	= 0,
};
751
MODULE_ALIAS_FS("hppfs");
L
Linus Torvalds 已提交
752 753 754

static int __init init_hppfs(void)
{
755
	return register_filesystem(&hppfs_type);
L
Linus Torvalds 已提交
756 757 758 759 760 761 762 763 764 765
}

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

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