hppfs.c 16.4 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 284 285 286 287 288
		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;
289
		if (count > 0)
L
Linus Torvalds 已提交
290 291 292 293
			*ppos += count;
	}
	else count = read_proc(hppfs->proc_file, buf, count, ppos, 1);

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

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

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

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);
317 318
	if (fd > 0)
		return fd;
L
Linus Torvalds 已提交
319 320 321 322

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

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

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

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

	INIT_LIST_HEAD(&data->list);

	head = data;
	*size_out = 0;

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

		*size_out += n;

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

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

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

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

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

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

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

static int file_mode(int fmode)
{
423 424 425 426 427 428 429
	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 已提交
430 431 432 433
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

L
Linus Torvalds 已提交
631 632 633 634 635 636 637 638
	clear_inode(ino);
}

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

639
static const struct super_operations hppfs_sbops = {
L
Linus Torvalds 已提交
640 641 642 643 644 645
	.alloc_inode	= hppfs_alloc_inode,
	.destroy_inode	= hppfs_destroy_inode,
	.delete_inode	= hppfs_delete_inode,
	.statfs		= hppfs_statfs,
};

646 647
static int hppfs_readlink(struct dentry *dentry, char __user *buffer,
			  int buflen)
L
Linus Torvalds 已提交
648 649 650 651
{
	struct dentry *proc_dentry;

	proc_dentry = HPPFS_I(dentry->d_inode)->proc_dentry;
J
Jeff Dike 已提交
652 653
	return proc_dentry->d_inode->i_op->readlink(proc_dentry, buffer,
						    buflen);
L
Linus Torvalds 已提交
654 655
}

J
Jeff Dike 已提交
656
static void *hppfs_follow_link(struct dentry *dentry, struct nameidata *nd)
L
Linus Torvalds 已提交
657 658 659 660 661
{
	struct dentry *proc_dentry;

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

J
Jeff Dike 已提交
662 663
	return proc_dentry->d_inode->i_op->follow_link(proc_dentry, nd);
}
L
Linus Torvalds 已提交
664

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

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

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 679 680 681
	struct inode *proc_ino = dentry->d_inode;
	struct inode *inode = new_inode(sb);

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

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

J
Jeff Dike 已提交
693
	HPPFS_I(inode)->proc_dentry = dget(dentry);
A
Al Viro 已提交
694 695 696 697 698 699 700 701 702 703 704

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

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

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

A
Al Viro 已提交
715
	proc_mnt = do_kern_mount("proc", 0, "proc", NULL);
716
	if (IS_ERR(proc_mnt))
L
Linus Torvalds 已提交
717 718 719 720 721 722
		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 已提交
723
	sb->s_fs_info = proc_mnt;
L
Linus Torvalds 已提交
724 725

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

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

734
	return 0;
L
Linus Torvalds 已提交
735

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

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

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)
{
761
	return register_filesystem(&hppfs_type);
L
Linus Torvalds 已提交
762 763 764 765 766 767 768 769 770 771
}

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

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