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>
L
Linus Torvalds 已提交
18 19 20
#include <asm/uaccess.h>
#include "os.h"

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

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

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

#define HPPFS_SUPER_MAGIC 0xb00000ee

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

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

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

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

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;
74 75
	while (parent->d_parent != parent) {
		if (is_pid(parent))
L
Linus Torvalds 已提交
76 77 78 79 80 81 82 83
			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);
84 85
	if (name == NULL)
		return NULL;
L
Linus Torvalds 已提交
86 87 88

	name[len] = '\0';
	parent = dentry;
89 90
	while (parent->d_parent != parent) {
		if (is_pid(parent)) {
L
Linus Torvalds 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104
			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));
105
	return name;
L
Linus Torvalds 已提交
106 107 108 109 110 111 112 113
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

289
	return count;
L
Linus Torvalds 已提交
290 291
}

J
Jeff Dike 已提交
292 293
static ssize_t hppfs_write(struct file *file, const char __user *buf,
			   size_t len, loff_t *ppos)
L
Linus Torvalds 已提交
294 295 296
{
	struct hppfs_private *data = file->private_data;
	struct file *proc_file = data->proc_file;
A
Al Viro 已提交
297
	ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
L
Linus Torvalds 已提交
298

J
Josef Sipek 已提交
299
	write = proc_file->f_path.dentry->d_inode->i_fop->write;
J
Jeff Dike 已提交
300
	return (*write)(proc_file, buf, len, ppos);
L
Linus Torvalds 已提交
301 302 303 304 305 306 307 308 309 310 311
}

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);
312 313
	if (fd > 0)
		return fd;
L
Linus Torvalds 已提交
314 315 316 317

	strcpy(end, "/r");
	*filter_out = 0;
	fd = os_connect_socket(host_file);
318
	return fd;
L
Linus Torvalds 已提交
319 320 321 322 323 324 325
}

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

326 327
	if (head == NULL)
		return;
L
Linus Torvalds 已提交
328

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

	INIT_LIST_HEAD(&data->list);

	head = data;
	*size_out = 0;

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

		*size_out += n;

379
		if (n < sizeof(data->contents))
L
Linus Torvalds 已提交
380 381 382
			break;

		new = kmalloc(sizeof(*data), GFP_KERNEL);
383 384 385
		if (new == 0) {
			printk(KERN_ERR "hppfs_get_data : data allocation "
			       "failed\n");
L
Linus Torvalds 已提交
386 387 388 389 390 391 392 393
			err = -ENOMEM;
			goto failed_free;
		}

		INIT_LIST_HEAD(&new->list);
		list_add(&new->list, &data->list);
		data = new;
	}
394
	return head;
L
Linus Torvalds 已提交
395 396 397 398

 failed_free:
	free_contents(head);
 failed:
399
	return ERR_PTR(err);
L
Linus Torvalds 已提交
400 401 402 403 404 405 406
}

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

	data = kmalloc(sizeof(*data), GFP_KERNEL);
407 408
	if (data == NULL)
		return data;
L
Linus Torvalds 已提交
409 410 411 412

	*data = ((struct hppfs_private ) { .host_fd  		= -1,
					   .len  		= -1,
					   .contents 		= NULL } );
413
	return data;
L
Linus Torvalds 已提交
414 415 416 417
}

static int file_mode(int fmode)
{
418 419 420 421 422 423 424
	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 已提交
425 426 427 428
}

static int hppfs_open(struct inode *inode, struct file *file)
{
429
	const struct cred *cred = current_cred();
L
Linus Torvalds 已提交
430
	struct hppfs_private *data;
431
	struct vfsmount *proc_mnt;
J
Jeff Dike 已提交
432
	struct dentry *proc_dentry;
L
Linus Torvalds 已提交
433 434 435 436 437
	char *host_file;
	int err, fd, type, filter;

	err = -ENOMEM;
	data = hppfs_data();
438
	if (data == NULL)
L
Linus Torvalds 已提交
439 440
		goto out;

J
Josef Sipek 已提交
441
	host_file = dentry_name(file->f_path.dentry, strlen("/rw"));
442
	if (host_file == NULL)
L
Linus Torvalds 已提交
443 444 445
		goto out_free2;

	proc_dentry = HPPFS_I(inode)->proc_dentry;
A
Al Viro 已提交
446
	proc_mnt = inode->i_sb->s_fs_info;
L
Linus Torvalds 已提交
447 448

	/* XXX This isn't closed anywhere */
449
	data->proc_file = dentry_open(dget(proc_dentry), mntget(proc_mnt),
450
				      file_mode(file->f_mode), cred);
L
Linus Torvalds 已提交
451
	err = PTR_ERR(data->proc_file);
452
	if (IS_ERR(data->proc_file))
L
Linus Torvalds 已提交
453 454 455
		goto out_free1;

	type = os_file_type(host_file);
456
	if (type == OS_TYPE_FILE) {
L
Linus Torvalds 已提交
457
		fd = os_open_file(host_file, of_read(OPENFLAGS()), 0);
458
		if (fd >= 0)
L
Linus Torvalds 已提交
459
			data->host_fd = fd;
460 461 462
		else
			printk(KERN_ERR "hppfs_open : failed to open '%s', "
			       "errno = %d\n", host_file, -fd);
L
Linus Torvalds 已提交
463 464

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

	file->private_data = data;
480
	return 0;
L
Linus Torvalds 已提交
481 482 483 484 485 486 487

 out_free1:
	kfree(host_file);
 out_free2:
	free_contents(data->contents);
	kfree(data);
 out:
488
	return err;
L
Linus Torvalds 已提交
489 490 491 492
}

static int hppfs_dir_open(struct inode *inode, struct file *file)
{
493
	const struct cred *cred = current_cred();
L
Linus Torvalds 已提交
494
	struct hppfs_private *data;
495
	struct vfsmount *proc_mnt;
J
Jeff Dike 已提交
496
	struct dentry *proc_dentry;
L
Linus Torvalds 已提交
497 498 499 500
	int err;

	err = -ENOMEM;
	data = hppfs_data();
501
	if (data == NULL)
L
Linus Torvalds 已提交
502 503 504
		goto out;

	proc_dentry = HPPFS_I(inode)->proc_dentry;
A
Al Viro 已提交
505
	proc_mnt = inode->i_sb->s_fs_info;
506
	data->proc_file = dentry_open(dget(proc_dentry), mntget(proc_mnt),
507
				      file_mode(file->f_mode), cred);
L
Linus Torvalds 已提交
508
	err = PTR_ERR(data->proc_file);
509
	if (IS_ERR(data->proc_file))
L
Linus Torvalds 已提交
510 511 512
		goto out_free;

	file->private_data = data;
513
	return 0;
L
Linus Torvalds 已提交
514 515 516 517

 out_free:
	kfree(data);
 out:
518
	return err;
L
Linus Torvalds 已提交
519 520 521 522 523
}

static loff_t hppfs_llseek(struct file *file, loff_t off, int where)
{
	struct hppfs_private *data = file->private_data;
524
	struct file *proc_file = data->proc_file;
L
Linus Torvalds 已提交
525 526 527
	loff_t (*llseek)(struct file *, loff_t, int);
	loff_t ret;

J
Josef Sipek 已提交
528
	llseek = proc_file->f_path.dentry->d_inode->i_fop->llseek;
529
	if (llseek != NULL) {
L
Linus Torvalds 已提交
530
		ret = (*llseek)(proc_file, off, where);
531 532
		if (ret < 0)
			return ret;
L
Linus Torvalds 已提交
533 534
	}

535
	return default_llseek(file, off, where);
L
Linus Torvalds 已提交
536 537
}

538
static const struct file_operations hppfs_file_fops = {
L
Linus Torvalds 已提交
539 540 541 542 543 544 545 546 547 548 549 550 551 552
	.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,
553
			 loff_t offset, u64 inode, unsigned int type)
L
Linus Torvalds 已提交
554 555 556
{
	struct hppfs_dirent *dirent = d;

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

560 561
	return (*dirent->filldir)(dirent->vfs_dirent, name, size, offset,
				  inode, type);
L
Linus Torvalds 已提交
562 563 564 565 566
}

static int hppfs_readdir(struct file *file, void *ent, filldir_t filldir)
{
	struct hppfs_private *data = file->private_data;
567
	struct file *proc_file = data->proc_file;
L
Linus Torvalds 已提交
568 569 570 571
	int (*readdir)(struct file *, void *, filldir_t);
	struct hppfs_dirent dirent = ((struct hppfs_dirent)
		                      { .vfs_dirent  	= ent,
					.filldir 	= filldir,
572 573
					.dentry  	= file->f_path.dentry
				      });
L
Linus Torvalds 已提交
574 575
	int err;

J
Josef Sipek 已提交
576
	readdir = proc_file->f_path.dentry->d_inode->i_fop->readdir;
L
Linus Torvalds 已提交
577 578 579 580 581

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

582
	return err;
L
Linus Torvalds 已提交
583 584 585 586
}

static int hppfs_fsync(struct file *file, struct dentry *dentry, int datasync)
{
587
	return 0;
L
Linus Torvalds 已提交
588 589
}

590
static const struct file_operations hppfs_dir_fops = {
L
Linus Torvalds 已提交
591 592 593 594 595 596
	.owner		= NULL,
	.readdir	= hppfs_readdir,
	.open		= hppfs_dir_open,
	.fsync		= hppfs_fsync,
};

597
static int hppfs_statfs(struct dentry *dentry, struct kstatfs *sf)
L
Linus Torvalds 已提交
598 599 600 601 602 603 604
{
	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;
605
	return 0;
L
Linus Torvalds 已提交
606 607 608 609 610 611 612
}

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

	hi = kmalloc(sizeof(*hi), GFP_KERNEL);
613 614
	if (!hi)
		return NULL;
L
Linus Torvalds 已提交
615

616
	hi->proc_dentry = NULL;
L
Linus Torvalds 已提交
617
	inode_init_once(&hi->vfs_inode);
618
	return &hi->vfs_inode;
L
Linus Torvalds 已提交
619 620 621 622
}

void hppfs_delete_inode(struct inode *ino)
{
J
Jeff Dike 已提交
623 624 625
	dput(HPPFS_I(ino)->proc_dentry);
	mntput(ino->i_sb->s_fs_info);

L
Linus Torvalds 已提交
626 627 628 629 630 631 632 633
	clear_inode(ino);
}

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

634
static const struct super_operations hppfs_sbops = {
L
Linus Torvalds 已提交
635 636 637 638 639 640
	.alloc_inode	= hppfs_alloc_inode,
	.destroy_inode	= hppfs_destroy_inode,
	.delete_inode	= hppfs_delete_inode,
	.statfs		= hppfs_statfs,
};

641 642
static int hppfs_readlink(struct dentry *dentry, char __user *buffer,
			  int buflen)
L
Linus Torvalds 已提交
643 644 645 646
{
	struct dentry *proc_dentry;

	proc_dentry = HPPFS_I(dentry->d_inode)->proc_dentry;
J
Jeff Dike 已提交
647 648
	return proc_dentry->d_inode->i_op->readlink(proc_dentry, buffer,
						    buflen);
L
Linus Torvalds 已提交
649 650
}

J
Jeff Dike 已提交
651
static void *hppfs_follow_link(struct dentry *dentry, struct nameidata *nd)
L
Linus Torvalds 已提交
652 653 654 655 656
{
	struct dentry *proc_dentry;

	proc_dentry = HPPFS_I(dentry->d_inode)->proc_dentry;

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

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 667 668
	.readlink	= hppfs_readlink,
	.follow_link	= hppfs_follow_link,
};

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

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

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

J
Jeff Dike 已提交
688
	HPPFS_I(inode)->proc_dentry = dget(dentry);
A
Al Viro 已提交
689 690 691 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;
	inode->i_nlink = proc_ino->i_nlink;
	inode->i_size = proc_ino->i_size;
	inode->i_blocks = proc_ino->i_blocks;
L
Linus Torvalds 已提交
700

J
Jeff Dike 已提交
701
	return inode;
L
Linus Torvalds 已提交
702 703 704 705 706
}

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

A
Al Viro 已提交
710
	proc_mnt = do_kern_mount("proc", 0, "proc", NULL);
711
	if (IS_ERR(proc_mnt))
L
Linus Torvalds 已提交
712 713 714 715 716 717
		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 已提交
718
	sb->s_fs_info = proc_mnt;
L
Linus Torvalds 已提交
719 720

	err = -ENOMEM;
A
Al Viro 已提交
721 722 723 724
	root_inode = get_inode(sb, proc_mnt->mnt_sb->s_root);
	if (!root_inode)
		goto out_mntput;

L
Linus Torvalds 已提交
725
	sb->s_root = d_alloc_root(root_inode);
726 727
	if (!sb->s_root)
		goto out_iput;
L
Linus Torvalds 已提交
728

729
	return 0;
L
Linus Torvalds 已提交
730

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

739 740 741
static int hppfs_read_super(struct file_system_type *type,
			    int flags, const char *dev_name,
			    void *data, struct vfsmount *mnt)
L
Linus Torvalds 已提交
742
{
743
	return get_sb_nodev(type, flags, data, hppfs_fill_super, mnt);
L
Linus Torvalds 已提交
744 745 746 747 748 749 750 751 752 753 754 755
}

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)
{
756
	return register_filesystem(&hppfs_type);
L
Linus Torvalds 已提交
757 758 759 760 761 762 763 764 765 766
}

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

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