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