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