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