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