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