hppfs pass vfsmount to dentry_open()
[deliverable/linux.git] / fs / hppfs / hppfs_kern.c
CommitLineData
1da177e4 1/*
1dd0dd11 2 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
1da177e4
LT
3 * Licensed under the GPL
4 */
5
1dd0dd11
DH
6#include <linux/ctype.h>
7#include <linux/dcache.h>
3f580470 8#include <linux/file.h>
1dd0dd11 9#include <linux/fs.h>
1da177e4 10#include <linux/init.h>
1da177e4 11#include <linux/kernel.h>
1dd0dd11
DH
12#include <linux/list.h>
13#include <linux/module.h>
14#include <linux/mount.h>
15#include <linux/slab.h>
1da177e4 16#include <linux/statfs.h>
1dd0dd11 17#include <linux/types.h>
1da177e4 18#include <asm/uaccess.h>
1da177e4
LT
19#include "os.h"
20
1dd0dd11
DH
21static int init_inode(struct inode *inode, struct dentry *dentry,
22 struct vfsmount *mnt);
1da177e4
LT
23
24struct hppfs_data {
25 struct list_head list;
26 char contents[PAGE_SIZE - sizeof(struct list_head)];
27};
28
29struct hppfs_private {
30 struct file *proc_file;
31 int host_fd;
32 loff_t len;
33 struct hppfs_data *contents;
34};
35
36struct hppfs_inode_info {
37 struct dentry *proc_dentry;
1dd0dd11 38 struct vfsmount *proc_mnt;
1da177e4
LT
39 struct inode vfs_inode;
40};
41
42static inline struct hppfs_inode_info *HPPFS_I(struct inode *inode)
43{
fd589e0b 44 return container_of(inode, struct hppfs_inode_info, vfs_inode);
1da177e4
LT
45}
46
47#define HPPFS_SUPER_MAGIC 0xb00000ee
48
ee9b6d61 49static const struct super_operations hppfs_sbops;
1da177e4
LT
50
51static int is_pid(struct dentry *dentry)
52{
53 struct super_block *sb;
54 int i;
55
56 sb = dentry->d_sb;
1dd0dd11
DH
57 if ((sb->s_op != &hppfs_sbops) || (dentry->d_parent != sb->s_root))
58 return 0;
1da177e4 59
1dd0dd11
DH
60 for (i = 0; i < dentry->d_name.len; i++) {
61 if (!isdigit(dentry->d_name.name[i]))
62 return 0;
1da177e4 63 }
1dd0dd11 64 return 1;
1da177e4
LT
65}
66
67static char *dentry_name(struct dentry *dentry, int extra)
68{
69 struct dentry *parent;
70 char *root, *name;
71 const char *seg_name;
72 int len, seg_len;
73
74 len = 0;
75 parent = dentry;
1dd0dd11
DH
76 while (parent->d_parent != parent) {
77 if (is_pid(parent))
1da177e4
LT
78 len += strlen("pid") + 1;
79 else len += parent->d_name.len + 1;
80 parent = parent->d_parent;
81 }
82
83 root = "proc";
84 len += strlen(root);
85 name = kmalloc(len + extra + 1, GFP_KERNEL);
1dd0dd11
DH
86 if (name == NULL)
87 return NULL;
1da177e4
LT
88
89 name[len] = '\0';
90 parent = dentry;
1dd0dd11
DH
91 while (parent->d_parent != parent) {
92 if (is_pid(parent)) {
1da177e4
LT
93 seg_name = "pid";
94 seg_len = strlen("pid");
95 }
96 else {
97 seg_name = parent->d_name.name;
98 seg_len = parent->d_name.len;
99 }
100
101 len -= seg_len + 1;
102 name[len] = '/';
103 strncpy(&name[len + 1], seg_name, seg_len);
104 parent = parent->d_parent;
105 }
106 strncpy(name, root, strlen(root));
1dd0dd11 107 return name;
1da177e4
LT
108}
109
1da177e4
LT
110static int file_removed(struct dentry *dentry, const char *file)
111{
112 char *host_file;
113 int extra, fd;
114
115 extra = 0;
1dd0dd11
DH
116 if (file != NULL)
117 extra += strlen(file) + 1;
1da177e4
LT
118
119 host_file = dentry_name(dentry, extra + strlen("/remove"));
1dd0dd11
DH
120 if (host_file == NULL) {
121 printk(KERN_ERR "file_removed : allocation failed\n");
122 return -ENOMEM;
1da177e4
LT
123 }
124
1dd0dd11 125 if (file != NULL) {
1da177e4
LT
126 strcat(host_file, "/");
127 strcat(host_file, file);
128 }
129 strcat(host_file, "/remove");
130
131 fd = os_open_file(host_file, of_read(OPENFLAGS()), 0);
132 kfree(host_file);
1dd0dd11 133 if (fd > 0) {
1da177e4 134 os_close_file(fd);
1dd0dd11 135 return 1;
1da177e4 136 }
1dd0dd11 137 return 0;
1da177e4
LT
138}
139
140static void hppfs_read_inode(struct inode *ino)
141{
142 struct inode *proc_ino;
143
1dd0dd11 144 if (HPPFS_I(ino)->proc_dentry == NULL)
1da177e4
LT
145 return;
146
147 proc_ino = HPPFS_I(ino)->proc_dentry->d_inode;
148 ino->i_uid = proc_ino->i_uid;
149 ino->i_gid = proc_ino->i_gid;
150 ino->i_atime = proc_ino->i_atime;
151 ino->i_mtime = proc_ino->i_mtime;
152 ino->i_ctime = proc_ino->i_ctime;
153 ino->i_ino = proc_ino->i_ino;
154 ino->i_mode = proc_ino->i_mode;
155 ino->i_nlink = proc_ino->i_nlink;
156 ino->i_size = proc_ino->i_size;
1da177e4
LT
157 ino->i_blocks = proc_ino->i_blocks;
158}
159
755aedc1
DH
160static struct inode *hppfs_iget(struct super_block *sb)
161{
162 struct inode *inode;
163
164 inode = iget_locked(sb, 0);
165 if (!inode)
166 return ERR_PTR(-ENOMEM);
167 if (inode->i_state & I_NEW) {
168 hppfs_read_inode(inode);
169 unlock_new_inode(inode);
170 }
171 return inode;
172}
173
1da177e4
LT
174static struct dentry *hppfs_lookup(struct inode *ino, struct dentry *dentry,
175 struct nameidata *nd)
176{
177 struct dentry *proc_dentry, *new, *parent;
178 struct inode *inode;
179 int err, deleted;
180
181 deleted = file_removed(dentry, NULL);
1dd0dd11
DH
182 if (deleted < 0)
183 return ERR_PTR(deleted);
184 else if (deleted)
185 return ERR_PTR(-ENOENT);
1da177e4
LT
186
187 err = -ENOMEM;
188 parent = HPPFS_I(ino)->proc_dentry;
1b1dcc1b 189 mutex_lock(&parent->d_inode->i_mutex);
1da177e4 190 proc_dentry = d_lookup(parent, &dentry->d_name);
1dd0dd11 191 if (proc_dentry == NULL) {
1da177e4 192 proc_dentry = d_alloc(parent, &dentry->d_name);
1dd0dd11 193 if (proc_dentry == NULL) {
1b1dcc1b 194 mutex_unlock(&parent->d_inode->i_mutex);
1da177e4
LT
195 goto out;
196 }
197 new = (*parent->d_inode->i_op->lookup)(parent->d_inode,
198 proc_dentry, NULL);
1dd0dd11 199 if (new) {
1da177e4
LT
200 dput(proc_dentry);
201 proc_dentry = new;
202 }
203 }
1b1dcc1b 204 mutex_unlock(&parent->d_inode->i_mutex);
1da177e4 205
1dd0dd11
DH
206 if (IS_ERR(proc_dentry))
207 return proc_dentry;
1da177e4 208
755aedc1
DH
209 inode = hppfs_iget(ino->i_sb);
210 if (IS_ERR(inode)) {
211 err = PTR_ERR(inode);
1da177e4 212 goto out_dput;
755aedc1 213 }
1da177e4 214
1dd0dd11
DH
215 err = init_inode(inode, proc_dentry, HPPFS_I(ino)->proc_mnt);
216 if (err)
1da177e4
LT
217 goto out_put;
218
219 hppfs_read_inode(inode);
220
221 d_add(dentry, inode);
1dd0dd11 222 return NULL;
1da177e4
LT
223
224 out_put:
225 iput(inode);
226 out_dput:
227 dput(proc_dentry);
228 out:
1dd0dd11 229 return ERR_PTR(err);
1da177e4
LT
230}
231
92e1d5be 232static const struct inode_operations hppfs_file_iops = {
1da177e4
LT
233};
234
347f217c 235static ssize_t read_proc(struct file *file, char __user *buf, ssize_t count,
1da177e4
LT
236 loff_t *ppos, int is_user)
237{
347f217c 238 ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
1da177e4
LT
239 ssize_t n;
240
471b17e7 241 read = file->f_path.dentry->d_inode->i_fop->read;
1da177e4 242
1dd0dd11 243 if (!is_user)
1da177e4
LT
244 set_fs(KERNEL_DS);
245
246 n = (*read)(file, buf, count, &file->f_pos);
247
1dd0dd11 248 if (!is_user)
1da177e4
LT
249 set_fs(USER_DS);
250
1dd0dd11
DH
251 if (ppos)
252 *ppos = file->f_pos;
8e0a2181 253 return n;
1da177e4
LT
254}
255
347f217c 256static ssize_t hppfs_read_file(int fd, char __user *buf, ssize_t count)
1da177e4
LT
257{
258 ssize_t n;
259 int cur, err;
260 char *new_buf;
261
262 n = -ENOMEM;
263 new_buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
1dd0dd11
DH
264 if (new_buf == NULL) {
265 printk(KERN_ERR "hppfs_read_file : kmalloc failed\n");
1da177e4
LT
266 goto out;
267 }
268 n = 0;
1dd0dd11 269 while (count > 0) {
1da177e4
LT
270 cur = min_t(ssize_t, count, PAGE_SIZE);
271 err = os_read_file(fd, new_buf, cur);
1dd0dd11
DH
272 if (err < 0) {
273 printk(KERN_ERR "hppfs_read : read failed, "
274 "errno = %d\n", err);
1da177e4
LT
275 n = err;
276 goto out_free;
1dd0dd11 277 } else if (err == 0)
1da177e4
LT
278 break;
279
1dd0dd11 280 if (copy_to_user(buf, new_buf, err)) {
1da177e4
LT
281 n = -EFAULT;
282 goto out_free;
283 }
284 n += err;
285 count -= err;
286 }
287 out_free:
288 kfree(new_buf);
289 out:
8e0a2181 290 return n;
1da177e4
LT
291}
292
347f217c 293static ssize_t hppfs_read(struct file *file, char __user *buf, size_t count,
1da177e4
LT
294 loff_t *ppos)
295{
296 struct hppfs_private *hppfs = file->private_data;
297 struct hppfs_data *data;
298 loff_t off;
299 int err;
300
1dd0dd11
DH
301 if (hppfs->contents != NULL) {
302 if (*ppos >= hppfs->len)
303 return 0;
1da177e4
LT
304
305 data = hppfs->contents;
306 off = *ppos;
1dd0dd11 307 while (off >= sizeof(data->contents)) {
1da177e4
LT
308 data = list_entry(data->list.next, struct hppfs_data,
309 list);
310 off -= sizeof(data->contents);
311 }
312
1dd0dd11 313 if (off + count > hppfs->len)
1da177e4
LT
314 count = hppfs->len - off;
315 copy_to_user(buf, &data->contents[off], count);
316 *ppos += count;
1dd0dd11 317 } else if (hppfs->host_fd != -1) {
1da177e4 318 err = os_seek_file(hppfs->host_fd, *ppos);
1dd0dd11
DH
319 if (err) {
320 printk(KERN_ERR "hppfs_read : seek failed, "
321 "errno = %d\n", err);
322 return err;
1da177e4
LT
323 }
324 count = hppfs_read_file(hppfs->host_fd, buf, count);
1dd0dd11 325 if (count > 0)
1da177e4
LT
326 *ppos += count;
327 }
328 else count = read_proc(hppfs->proc_file, buf, count, ppos, 1);
329
1dd0dd11 330 return count;
1da177e4
LT
331}
332
347f217c 333static ssize_t hppfs_write(struct file *file, const char __user *buf, size_t len,
1da177e4
LT
334 loff_t *ppos)
335{
336 struct hppfs_private *data = file->private_data;
337 struct file *proc_file = data->proc_file;
347f217c 338 ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
1da177e4
LT
339 int err;
340
471b17e7 341 write = proc_file->f_path.dentry->d_inode->i_fop->write;
1da177e4
LT
342
343 proc_file->f_pos = file->f_pos;
344 err = (*write)(proc_file, buf, len, &proc_file->f_pos);
345 file->f_pos = proc_file->f_pos;
346
1dd0dd11 347 return err;
1da177e4
LT
348}
349
350static int open_host_sock(char *host_file, int *filter_out)
351{
352 char *end;
353 int fd;
354
355 end = &host_file[strlen(host_file)];
356 strcpy(end, "/rw");
357 *filter_out = 1;
358 fd = os_connect_socket(host_file);
1dd0dd11
DH
359 if (fd > 0)
360 return fd;
1da177e4
LT
361
362 strcpy(end, "/r");
363 *filter_out = 0;
364 fd = os_connect_socket(host_file);
1dd0dd11 365 return fd;
1da177e4
LT
366}
367
368static void free_contents(struct hppfs_data *head)
369{
370 struct hppfs_data *data;
371 struct list_head *ele, *next;
372
1dd0dd11
DH
373 if (head == NULL)
374 return;
1da177e4 375
1dd0dd11 376 list_for_each_safe(ele, next, &head->list) {
1da177e4
LT
377 data = list_entry(ele, struct hppfs_data, list);
378 kfree(data);
379 }
380 kfree(head);
381}
382
383static struct hppfs_data *hppfs_get_data(int fd, int filter,
384 struct file *proc_file,
385 struct file *hppfs_file,
386 loff_t *size_out)
387{
388 struct hppfs_data *data, *new, *head;
389 int n, err;
390
391 err = -ENOMEM;
392 data = kmalloc(sizeof(*data), GFP_KERNEL);
1dd0dd11
DH
393 if (data == NULL) {
394 printk(KERN_ERR "hppfs_get_data : head allocation failed\n");
1da177e4
LT
395 goto failed;
396 }
397
398 INIT_LIST_HEAD(&data->list);
399
400 head = data;
401 *size_out = 0;
402
1dd0dd11
DH
403 if (filter) {
404 while ((n = read_proc(proc_file, data->contents,
1da177e4
LT
405 sizeof(data->contents), NULL, 0)) > 0)
406 os_write_file(fd, data->contents, n);
407 err = os_shutdown_socket(fd, 0, 1);
1dd0dd11
DH
408 if (err) {
409 printk(KERN_ERR "hppfs_get_data : failed to shut down "
1da177e4
LT
410 "socket\n");
411 goto failed_free;
412 }
413 }
1dd0dd11 414 while (1) {
1da177e4 415 n = os_read_file(fd, data->contents, sizeof(data->contents));
1dd0dd11 416 if (n < 0) {
1da177e4 417 err = n;
1dd0dd11
DH
418 printk(KERN_ERR "hppfs_get_data : read failed, "
419 "errno = %d\n", err);
1da177e4 420 goto failed_free;
1dd0dd11 421 } else if (n == 0)
1da177e4
LT
422 break;
423
424 *size_out += n;
425
1dd0dd11 426 if (n < sizeof(data->contents))
1da177e4
LT
427 break;
428
429 new = kmalloc(sizeof(*data), GFP_KERNEL);
1dd0dd11
DH
430 if (new == 0) {
431 printk(KERN_ERR "hppfs_get_data : data allocation "
432 "failed\n");
1da177e4
LT
433 err = -ENOMEM;
434 goto failed_free;
435 }
436
437 INIT_LIST_HEAD(&new->list);
438 list_add(&new->list, &data->list);
439 data = new;
440 }
1dd0dd11 441 return head;
1da177e4
LT
442
443 failed_free:
444 free_contents(head);
445 failed:
1dd0dd11 446 return ERR_PTR(err);
1da177e4
LT
447}
448
449static struct hppfs_private *hppfs_data(void)
450{
451 struct hppfs_private *data;
452
453 data = kmalloc(sizeof(*data), GFP_KERNEL);
1dd0dd11
DH
454 if (data == NULL)
455 return data;
1da177e4
LT
456
457 *data = ((struct hppfs_private ) { .host_fd = -1,
458 .len = -1,
459 .contents = NULL } );
1dd0dd11 460 return data;
1da177e4
LT
461}
462
463static int file_mode(int fmode)
464{
1dd0dd11
DH
465 if (fmode == (FMODE_READ | FMODE_WRITE))
466 return O_RDWR;
467 if (fmode == FMODE_READ)
468 return O_RDONLY;
469 if (fmode == FMODE_WRITE)
470 return O_WRONLY;
471 return 0;
1da177e4
LT
472}
473
474static int hppfs_open(struct inode *inode, struct file *file)
475{
476 struct hppfs_private *data;
477 struct dentry *proc_dentry;
1dd0dd11 478 struct vfsmount *proc_mnt;
1da177e4
LT
479 char *host_file;
480 int err, fd, type, filter;
481
482 err = -ENOMEM;
483 data = hppfs_data();
1dd0dd11 484 if (data == NULL)
1da177e4
LT
485 goto out;
486
471b17e7 487 host_file = dentry_name(file->f_path.dentry, strlen("/rw"));
1dd0dd11 488 if (host_file == NULL)
1da177e4
LT
489 goto out_free2;
490
491 proc_dentry = HPPFS_I(inode)->proc_dentry;
1dd0dd11 492 proc_mnt = HPPFS_I(inode)->proc_mnt;
1da177e4
LT
493
494 /* XXX This isn't closed anywhere */
1dd0dd11 495 data->proc_file = dentry_open(dget(proc_dentry), mntget(proc_mnt),
1da177e4
LT
496 file_mode(file->f_mode));
497 err = PTR_ERR(data->proc_file);
1dd0dd11 498 if (IS_ERR(data->proc_file))
1da177e4
LT
499 goto out_free1;
500
501 type = os_file_type(host_file);
1dd0dd11 502 if (type == OS_TYPE_FILE) {
1da177e4 503 fd = os_open_file(host_file, of_read(OPENFLAGS()), 0);
1dd0dd11 504 if (fd >= 0)
1da177e4 505 data->host_fd = fd;
1dd0dd11
DH
506 else
507 printk(KERN_ERR "hppfs_open : failed to open '%s', "
508 "errno = %d\n", host_file, -fd);
1da177e4
LT
509
510 data->contents = NULL;
1dd0dd11 511 } else if (type == OS_TYPE_DIR) {
1da177e4 512 fd = open_host_sock(host_file, &filter);
1dd0dd11 513 if (fd > 0) {
1da177e4 514 data->contents = hppfs_get_data(fd, filter,
3f580470 515 data->proc_file,
1da177e4 516 file, &data->len);
1dd0dd11 517 if (!IS_ERR(data->contents))
1da177e4 518 data->host_fd = fd;
1dd0dd11
DH
519 } else
520 printk(KERN_ERR "hppfs_open : failed to open a socket "
521 "in '%s', errno = %d\n", host_file, -fd);
1da177e4
LT
522 }
523 kfree(host_file);
524
525 file->private_data = data;
1dd0dd11 526 return 0;
1da177e4
LT
527
528 out_free1:
529 kfree(host_file);
530 out_free2:
531 free_contents(data->contents);
532 kfree(data);
533 out:
1dd0dd11 534 return err;
1da177e4
LT
535}
536
537static int hppfs_dir_open(struct inode *inode, struct file *file)
538{
539 struct hppfs_private *data;
540 struct dentry *proc_dentry;
1dd0dd11 541 struct vfsmount *proc_mnt;
1da177e4
LT
542 int err;
543
544 err = -ENOMEM;
545 data = hppfs_data();
1dd0dd11 546 if (data == NULL)
1da177e4
LT
547 goto out;
548
549 proc_dentry = HPPFS_I(inode)->proc_dentry;
1dd0dd11
DH
550 proc_mnt = HPPFS_I(inode)->proc_mnt;
551 data->proc_file = dentry_open(dget(proc_dentry), mntget(proc_mnt),
1da177e4
LT
552 file_mode(file->f_mode));
553 err = PTR_ERR(data->proc_file);
1dd0dd11 554 if (IS_ERR(data->proc_file))
1da177e4
LT
555 goto out_free;
556
557 file->private_data = data;
1dd0dd11 558 return 0;
1da177e4
LT
559
560 out_free:
561 kfree(data);
562 out:
1dd0dd11 563 return err;
1da177e4
LT
564}
565
566static loff_t hppfs_llseek(struct file *file, loff_t off, int where)
567{
568 struct hppfs_private *data = file->private_data;
3f580470 569 struct file *proc_file = data->proc_file;
1da177e4
LT
570 loff_t (*llseek)(struct file *, loff_t, int);
571 loff_t ret;
572
471b17e7 573 llseek = proc_file->f_path.dentry->d_inode->i_fop->llseek;
1dd0dd11 574 if (llseek != NULL) {
1da177e4 575 ret = (*llseek)(proc_file, off, where);
1dd0dd11
DH
576 if (ret < 0)
577 return ret;
1da177e4
LT
578 }
579
1dd0dd11 580 return default_llseek(file, off, where);
1da177e4
LT
581}
582
4b6f5d20 583static const struct file_operations hppfs_file_fops = {
1da177e4
LT
584 .owner = NULL,
585 .llseek = hppfs_llseek,
586 .read = hppfs_read,
587 .write = hppfs_write,
588 .open = hppfs_open,
589};
590
591struct hppfs_dirent {
592 void *vfs_dirent;
593 filldir_t filldir;
594 struct dentry *dentry;
595};
596
597static int hppfs_filldir(void *d, const char *name, int size,
c8adf94a 598 loff_t offset, u64 inode, unsigned int type)
1da177e4
LT
599{
600 struct hppfs_dirent *dirent = d;
601
1dd0dd11
DH
602 if (file_removed(dirent->dentry, name))
603 return 0;
1da177e4 604
1dd0dd11
DH
605 return (*dirent->filldir)(dirent->vfs_dirent, name, size, offset,
606 inode, type);
1da177e4
LT
607}
608
609static int hppfs_readdir(struct file *file, void *ent, filldir_t filldir)
610{
611 struct hppfs_private *data = file->private_data;
3f580470 612 struct file *proc_file = data->proc_file;
1da177e4
LT
613 int (*readdir)(struct file *, void *, filldir_t);
614 struct hppfs_dirent dirent = ((struct hppfs_dirent)
615 { .vfs_dirent = ent,
616 .filldir = filldir,
1dd0dd11
DH
617 .dentry = file->f_path.dentry
618 });
1da177e4
LT
619 int err;
620
471b17e7 621 readdir = proc_file->f_path.dentry->d_inode->i_fop->readdir;
1da177e4
LT
622
623 proc_file->f_pos = file->f_pos;
624 err = (*readdir)(proc_file, &dirent, hppfs_filldir);
625 file->f_pos = proc_file->f_pos;
626
1dd0dd11 627 return err;
1da177e4
LT
628}
629
630static int hppfs_fsync(struct file *file, struct dentry *dentry, int datasync)
631{
1dd0dd11 632 return 0;
1da177e4
LT
633}
634
4b6f5d20 635static const struct file_operations hppfs_dir_fops = {
1da177e4
LT
636 .owner = NULL,
637 .readdir = hppfs_readdir,
638 .open = hppfs_dir_open,
639 .fsync = hppfs_fsync,
640};
641
726c3342 642static int hppfs_statfs(struct dentry *dentry, struct kstatfs *sf)
1da177e4
LT
643{
644 sf->f_blocks = 0;
645 sf->f_bfree = 0;
646 sf->f_bavail = 0;
647 sf->f_files = 0;
648 sf->f_ffree = 0;
649 sf->f_type = HPPFS_SUPER_MAGIC;
1dd0dd11 650 return 0;
1da177e4
LT
651}
652
653static struct inode *hppfs_alloc_inode(struct super_block *sb)
654{
655 struct hppfs_inode_info *hi;
656
657 hi = kmalloc(sizeof(*hi), GFP_KERNEL);
1dd0dd11
DH
658 if (!hi)
659 return NULL;
1da177e4 660
1dd0dd11
DH
661 hi->proc_dentry = NULL;
662 hi->proc_mnt = NULL;
1da177e4 663 inode_init_once(&hi->vfs_inode);
1dd0dd11 664 return &hi->vfs_inode;
1da177e4
LT
665}
666
667void hppfs_delete_inode(struct inode *ino)
668{
669 clear_inode(ino);
670}
671
672static void hppfs_destroy_inode(struct inode *inode)
673{
674 kfree(HPPFS_I(inode));
675}
676
1dd0dd11
DH
677static void hppfs_put_super(struct super_block *sb)
678{
679 mntput(HPPFS_I(sb->s_root->d_inode)->proc_mnt);
680}
681
ee9b6d61 682static const struct super_operations hppfs_sbops = {
1da177e4
LT
683 .alloc_inode = hppfs_alloc_inode,
684 .destroy_inode = hppfs_destroy_inode,
1da177e4
LT
685 .delete_inode = hppfs_delete_inode,
686 .statfs = hppfs_statfs,
1dd0dd11 687 .put_super = hppfs_put_super,
1da177e4
LT
688};
689
1dd0dd11
DH
690static int hppfs_readlink(struct dentry *dentry, char __user *buffer,
691 int buflen)
1da177e4
LT
692{
693 struct file *proc_file;
694 struct dentry *proc_dentry;
1dd0dd11 695 struct vfsmount *proc_mnt;
fd589e0b 696 int ret;
1da177e4
LT
697
698 proc_dentry = HPPFS_I(dentry->d_inode)->proc_dentry;
1dd0dd11
DH
699 proc_mnt = HPPFS_I(dentry->d_inode)->proc_mnt;
700
701 proc_file = dentry_open(dget(proc_dentry), mntget(proc_mnt), O_RDONLY);
fd589e0b
PBG
702 if (IS_ERR(proc_file))
703 return PTR_ERR(proc_file);
1da177e4 704
fd589e0b 705 ret = proc_dentry->d_inode->i_op->readlink(proc_dentry, buffer, buflen);
1da177e4
LT
706
707 fput(proc_file);
708
fd589e0b 709 return ret;
1da177e4
LT
710}
711
d7a60d50 712static void* hppfs_follow_link(struct dentry *dentry, struct nameidata *nd)
1da177e4
LT
713{
714 struct file *proc_file;
715 struct dentry *proc_dentry;
1dd0dd11 716 struct vfsmount *proc_mnt;
d7a60d50 717 void *ret;
1da177e4
LT
718
719 proc_dentry = HPPFS_I(dentry->d_inode)->proc_dentry;
1dd0dd11
DH
720 proc_mnt = HPPFS_I(dentry->d_inode)->proc_mnt;
721
722 proc_file = dentry_open(dget(proc_dentry), mntget(proc_mnt), O_RDONLY);
fd589e0b
PBG
723 if (IS_ERR(proc_file))
724 return proc_file;
d7a60d50 725
fd589e0b 726 ret = proc_dentry->d_inode->i_op->follow_link(proc_dentry, nd);
1da177e4
LT
727
728 fput(proc_file);
729
d7a60d50 730 return ret;
1da177e4
LT
731}
732
92e1d5be 733static const struct inode_operations hppfs_dir_iops = {
1da177e4
LT
734 .lookup = hppfs_lookup,
735};
736
92e1d5be 737static const struct inode_operations hppfs_link_iops = {
1da177e4
LT
738 .readlink = hppfs_readlink,
739 .follow_link = hppfs_follow_link,
740};
741
1dd0dd11
DH
742static int init_inode(struct inode *inode, struct dentry *dentry,
743 struct vfsmount *mnt)
1da177e4 744{
1dd0dd11 745 if (S_ISDIR(dentry->d_inode->i_mode)) {
1da177e4
LT
746 inode->i_op = &hppfs_dir_iops;
747 inode->i_fop = &hppfs_dir_fops;
1dd0dd11 748 } else if (S_ISLNK(dentry->d_inode->i_mode)) {
1da177e4
LT
749 inode->i_op = &hppfs_link_iops;
750 inode->i_fop = &hppfs_file_fops;
1dd0dd11 751 } else {
1da177e4
LT
752 inode->i_op = &hppfs_file_iops;
753 inode->i_fop = &hppfs_file_fops;
754 }
755
756 HPPFS_I(inode)->proc_dentry = dentry;
1dd0dd11 757 HPPFS_I(inode)->proc_mnt = mnt;
1da177e4 758
1dd0dd11 759 return 0;
1da177e4
LT
760}
761
762static int hppfs_fill_super(struct super_block *sb, void *d, int silent)
763{
764 struct inode *root_inode;
765 struct file_system_type *procfs;
1dd0dd11 766 struct vfsmount *proc_mnt;
1da177e4
LT
767 int err;
768
769 err = -ENOENT;
770 procfs = get_fs_type("proc");
1dd0dd11 771 if (!procfs)
1da177e4
LT
772 goto out;
773
1dd0dd11
DH
774 proc_mnt = vfs_kern_mount(procfs, 0, procfs->name, NULL);
775 put_filesystem(procfs);
776 if (IS_ERR(proc_mnt))
1da177e4
LT
777 goto out;
778
1da177e4
LT
779 sb->s_blocksize = 1024;
780 sb->s_blocksize_bits = 10;
781 sb->s_magic = HPPFS_SUPER_MAGIC;
782 sb->s_op = &hppfs_sbops;
783
755aedc1
DH
784 root_inode = hppfs_iget(sb);
785 if (IS_ERR(root_inode)) {
786 err = PTR_ERR(root_inode);
1da177e4 787 goto out;
755aedc1 788 }
1da177e4 789
1dd0dd11
DH
790 err = init_inode(root_inode, proc_mnt->mnt_sb->s_root, proc_mnt);
791 if (err)
792 goto out_iput;
1da177e4
LT
793
794 err = -ENOMEM;
795 sb->s_root = d_alloc_root(root_inode);
1dd0dd11
DH
796 if (!sb->s_root)
797 goto out_iput;
1da177e4
LT
798
799 hppfs_read_inode(root_inode);
800
1dd0dd11 801 return 0;
1da177e4 802
1dd0dd11 803 out_iput:
1da177e4 804 iput(root_inode);
1dd0dd11
DH
805 out_mntput:
806 mntput(proc_mnt);
1da177e4
LT
807 out:
808 return(err);
809}
810
454e2398
DH
811static int hppfs_read_super(struct file_system_type *type,
812 int flags, const char *dev_name,
813 void *data, struct vfsmount *mnt)
1da177e4 814{
454e2398 815 return get_sb_nodev(type, flags, data, hppfs_fill_super, mnt);
1da177e4
LT
816}
817
818static struct file_system_type hppfs_type = {
819 .owner = THIS_MODULE,
820 .name = "hppfs",
821 .get_sb = hppfs_read_super,
822 .kill_sb = kill_anon_super,
823 .fs_flags = 0,
824};
825
826static int __init init_hppfs(void)
827{
1dd0dd11 828 return register_filesystem(&hppfs_type);
1da177e4
LT
829}
830
831static void __exit exit_hppfs(void)
832{
833 unregister_filesystem(&hppfs_type);
834}
835
836module_init(init_hppfs)
837module_exit(exit_hppfs)
838MODULE_LICENSE("GPL");
This page took 0.54143 seconds and 5 git commands to generate.