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