blackfin: twi: read twi mmr via bfin_read macro
[linux-2.6/libata-dev.git] / fs / hppfs / hppfs.c
blobd4f93b52cec512723447fb1390a9c9b6cca8b45f
1 /*
2 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
4 */
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 <linux/pid_namespace.h>
19 #include <linux/namei.h>
20 #include <asm/uaccess.h>
21 #include "os.h"
23 static struct inode *get_inode(struct super_block *, struct dentry *);
25 struct hppfs_data {
26 struct list_head list;
27 char contents[PAGE_SIZE - sizeof(struct list_head)];
30 struct hppfs_private {
31 struct file *proc_file;
32 int host_fd;
33 loff_t len;
34 struct hppfs_data *contents;
37 struct hppfs_inode_info {
38 struct dentry *proc_dentry;
39 struct inode vfs_inode;
42 static inline struct hppfs_inode_info *HPPFS_I(struct inode *inode)
44 return container_of(inode, struct hppfs_inode_info, vfs_inode);
47 #define HPPFS_SUPER_MAGIC 0xb00000ee
49 static const struct super_operations hppfs_sbops;
51 static int is_pid(struct dentry *dentry)
53 struct super_block *sb;
54 int i;
56 sb = dentry->d_sb;
57 if (dentry->d_parent != sb->s_root)
58 return 0;
60 for (i = 0; i < dentry->d_name.len; i++) {
61 if (!isdigit(dentry->d_name.name[i]))
62 return 0;
64 return 1;
67 static char *dentry_name(struct dentry *dentry, int extra)
69 struct dentry *parent;
70 char *root, *name;
71 const char *seg_name;
72 int len, seg_len;
74 len = 0;
75 parent = dentry;
76 while (parent->d_parent != parent) {
77 if (is_pid(parent))
78 len += strlen("pid") + 1;
79 else len += parent->d_name.len + 1;
80 parent = parent->d_parent;
83 root = "proc";
84 len += strlen(root);
85 name = kmalloc(len + extra + 1, GFP_KERNEL);
86 if (name == NULL)
87 return NULL;
89 name[len] = '\0';
90 parent = dentry;
91 while (parent->d_parent != parent) {
92 if (is_pid(parent)) {
93 seg_name = "pid";
94 seg_len = strlen("pid");
96 else {
97 seg_name = parent->d_name.name;
98 seg_len = parent->d_name.len;
101 len -= seg_len + 1;
102 name[len] = '/';
103 strncpy(&name[len + 1], seg_name, seg_len);
104 parent = parent->d_parent;
106 strncpy(name, root, strlen(root));
107 return name;
110 static int file_removed(struct dentry *dentry, const char *file)
112 char *host_file;
113 int extra, fd;
115 extra = 0;
116 if (file != NULL)
117 extra += strlen(file) + 1;
119 host_file = dentry_name(dentry, extra + strlen("/remove"));
120 if (host_file == NULL) {
121 printk(KERN_ERR "file_removed : allocation failed\n");
122 return -ENOMEM;
125 if (file != NULL) {
126 strcat(host_file, "/");
127 strcat(host_file, file);
129 strcat(host_file, "/remove");
131 fd = os_open_file(host_file, of_read(OPENFLAGS()), 0);
132 kfree(host_file);
133 if (fd > 0) {
134 os_close_file(fd);
135 return 1;
137 return 0;
140 static struct dentry *hppfs_lookup(struct inode *ino, struct dentry *dentry,
141 struct nameidata *nd)
143 struct dentry *proc_dentry, *parent;
144 struct qstr *name = &dentry->d_name;
145 struct inode *inode;
146 int err, deleted;
148 deleted = file_removed(dentry, NULL);
149 if (deleted < 0)
150 return ERR_PTR(deleted);
151 else if (deleted)
152 return ERR_PTR(-ENOENT);
154 parent = HPPFS_I(ino)->proc_dentry;
155 mutex_lock(&parent->d_inode->i_mutex);
156 proc_dentry = lookup_one_len(name->name, parent, name->len);
157 mutex_unlock(&parent->d_inode->i_mutex);
159 if (IS_ERR(proc_dentry))
160 return proc_dentry;
162 err = -ENOMEM;
163 inode = get_inode(ino->i_sb, proc_dentry);
164 if (!inode)
165 goto out;
167 d_add(dentry, inode);
168 return NULL;
170 out:
171 return ERR_PTR(err);
174 static const struct inode_operations hppfs_file_iops = {
177 static ssize_t read_proc(struct file *file, char __user *buf, ssize_t count,
178 loff_t *ppos, int is_user)
180 ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
181 ssize_t n;
183 read = file->f_path.dentry->d_inode->i_fop->read;
185 if (!is_user)
186 set_fs(KERNEL_DS);
188 n = (*read)(file, buf, count, &file->f_pos);
190 if (!is_user)
191 set_fs(USER_DS);
193 if (ppos)
194 *ppos = file->f_pos;
195 return n;
198 static ssize_t hppfs_read_file(int fd, char __user *buf, ssize_t count)
200 ssize_t n;
201 int cur, err;
202 char *new_buf;
204 n = -ENOMEM;
205 new_buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
206 if (new_buf == NULL) {
207 printk(KERN_ERR "hppfs_read_file : kmalloc failed\n");
208 goto out;
210 n = 0;
211 while (count > 0) {
212 cur = min_t(ssize_t, count, PAGE_SIZE);
213 err = os_read_file(fd, new_buf, cur);
214 if (err < 0) {
215 printk(KERN_ERR "hppfs_read : read failed, "
216 "errno = %d\n", err);
217 n = err;
218 goto out_free;
219 } else if (err == 0)
220 break;
222 if (copy_to_user(buf, new_buf, err)) {
223 n = -EFAULT;
224 goto out_free;
226 n += err;
227 count -= err;
229 out_free:
230 kfree(new_buf);
231 out:
232 return n;
235 static ssize_t hppfs_read(struct file *file, char __user *buf, size_t count,
236 loff_t *ppos)
238 struct hppfs_private *hppfs = file->private_data;
239 struct hppfs_data *data;
240 loff_t off;
241 int err;
243 if (hppfs->contents != NULL) {
244 int rem;
246 if (*ppos >= hppfs->len)
247 return 0;
249 data = hppfs->contents;
250 off = *ppos;
251 while (off >= sizeof(data->contents)) {
252 data = list_entry(data->list.next, struct hppfs_data,
253 list);
254 off -= sizeof(data->contents);
257 if (off + count > hppfs->len)
258 count = hppfs->len - off;
259 rem = copy_to_user(buf, &data->contents[off], count);
260 *ppos += count - rem;
261 if (rem > 0)
262 return -EFAULT;
263 } else if (hppfs->host_fd != -1) {
264 err = os_seek_file(hppfs->host_fd, *ppos);
265 if (err) {
266 printk(KERN_ERR "hppfs_read : seek failed, "
267 "errno = %d\n", err);
268 return err;
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;
275 count = err;
276 if (count > 0)
277 *ppos += count;
279 else count = read_proc(hppfs->proc_file, buf, count, ppos, 1);
281 return count;
284 static ssize_t hppfs_write(struct file *file, const char __user *buf,
285 size_t len, loff_t *ppos)
287 struct hppfs_private *data = file->private_data;
288 struct file *proc_file = data->proc_file;
289 ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *);
291 write = proc_file->f_path.dentry->d_inode->i_fop->write;
292 return (*write)(proc_file, buf, len, ppos);
295 static int open_host_sock(char *host_file, int *filter_out)
297 char *end;
298 int fd;
300 end = &host_file[strlen(host_file)];
301 strcpy(end, "/rw");
302 *filter_out = 1;
303 fd = os_connect_socket(host_file);
304 if (fd > 0)
305 return fd;
307 strcpy(end, "/r");
308 *filter_out = 0;
309 fd = os_connect_socket(host_file);
310 return fd;
313 static void free_contents(struct hppfs_data *head)
315 struct hppfs_data *data;
316 struct list_head *ele, *next;
318 if (head == NULL)
319 return;
321 list_for_each_safe(ele, next, &head->list) {
322 data = list_entry(ele, struct hppfs_data, list);
323 kfree(data);
325 kfree(head);
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)
333 struct hppfs_data *data, *new, *head;
334 int n, err;
336 err = -ENOMEM;
337 data = kmalloc(sizeof(*data), GFP_KERNEL);
338 if (data == NULL) {
339 printk(KERN_ERR "hppfs_get_data : head allocation failed\n");
340 goto failed;
343 INIT_LIST_HEAD(&data->list);
345 head = data;
346 *size_out = 0;
348 if (filter) {
349 while ((n = read_proc(proc_file, data->contents,
350 sizeof(data->contents), NULL, 0)) > 0)
351 os_write_file(fd, data->contents, n);
352 err = os_shutdown_socket(fd, 0, 1);
353 if (err) {
354 printk(KERN_ERR "hppfs_get_data : failed to shut down "
355 "socket\n");
356 goto failed_free;
359 while (1) {
360 n = os_read_file(fd, data->contents, sizeof(data->contents));
361 if (n < 0) {
362 err = n;
363 printk(KERN_ERR "hppfs_get_data : read failed, "
364 "errno = %d\n", err);
365 goto failed_free;
366 } else if (n == 0)
367 break;
369 *size_out += n;
371 if (n < sizeof(data->contents))
372 break;
374 new = kmalloc(sizeof(*data), GFP_KERNEL);
375 if (new == 0) {
376 printk(KERN_ERR "hppfs_get_data : data allocation "
377 "failed\n");
378 err = -ENOMEM;
379 goto failed_free;
382 INIT_LIST_HEAD(&new->list);
383 list_add(&new->list, &data->list);
384 data = new;
386 return head;
388 failed_free:
389 free_contents(head);
390 failed:
391 return ERR_PTR(err);
394 static struct hppfs_private *hppfs_data(void)
396 struct hppfs_private *data;
398 data = kmalloc(sizeof(*data), GFP_KERNEL);
399 if (data == NULL)
400 return data;
402 *data = ((struct hppfs_private ) { .host_fd = -1,
403 .len = -1,
404 .contents = NULL } );
405 return data;
408 static int file_mode(int fmode)
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;
419 static int hppfs_open(struct inode *inode, struct file *file)
421 const struct cred *cred = file->f_cred;
422 struct hppfs_private *data;
423 struct vfsmount *proc_mnt;
424 struct dentry *proc_dentry;
425 char *host_file;
426 int err, fd, type, filter;
428 err = -ENOMEM;
429 data = hppfs_data();
430 if (data == NULL)
431 goto out;
433 host_file = dentry_name(file->f_path.dentry, strlen("/rw"));
434 if (host_file == NULL)
435 goto out_free2;
437 proc_dentry = HPPFS_I(inode)->proc_dentry;
438 proc_mnt = inode->i_sb->s_fs_info;
440 /* XXX This isn't closed anywhere */
441 data->proc_file = dentry_open(dget(proc_dentry), mntget(proc_mnt),
442 file_mode(file->f_mode), cred);
443 err = PTR_ERR(data->proc_file);
444 if (IS_ERR(data->proc_file))
445 goto out_free1;
447 type = os_file_type(host_file);
448 if (type == OS_TYPE_FILE) {
449 fd = os_open_file(host_file, of_read(OPENFLAGS()), 0);
450 if (fd >= 0)
451 data->host_fd = fd;
452 else
453 printk(KERN_ERR "hppfs_open : failed to open '%s', "
454 "errno = %d\n", host_file, -fd);
456 data->contents = NULL;
457 } else if (type == OS_TYPE_DIR) {
458 fd = open_host_sock(host_file, &filter);
459 if (fd > 0) {
460 data->contents = hppfs_get_data(fd, filter,
461 data->proc_file,
462 file, &data->len);
463 if (!IS_ERR(data->contents))
464 data->host_fd = fd;
465 } else
466 printk(KERN_ERR "hppfs_open : failed to open a socket "
467 "in '%s', errno = %d\n", host_file, -fd);
469 kfree(host_file);
471 file->private_data = data;
472 return 0;
474 out_free1:
475 kfree(host_file);
476 out_free2:
477 free_contents(data->contents);
478 kfree(data);
479 out:
480 return err;
483 static int hppfs_dir_open(struct inode *inode, struct file *file)
485 const struct cred *cred = file->f_cred;
486 struct hppfs_private *data;
487 struct vfsmount *proc_mnt;
488 struct dentry *proc_dentry;
489 int err;
491 err = -ENOMEM;
492 data = hppfs_data();
493 if (data == NULL)
494 goto out;
496 proc_dentry = HPPFS_I(inode)->proc_dentry;
497 proc_mnt = inode->i_sb->s_fs_info;
498 data->proc_file = dentry_open(dget(proc_dentry), mntget(proc_mnt),
499 file_mode(file->f_mode), cred);
500 err = PTR_ERR(data->proc_file);
501 if (IS_ERR(data->proc_file))
502 goto out_free;
504 file->private_data = data;
505 return 0;
507 out_free:
508 kfree(data);
509 out:
510 return err;
513 static loff_t hppfs_llseek(struct file *file, loff_t off, int where)
515 struct hppfs_private *data = file->private_data;
516 struct file *proc_file = data->proc_file;
517 loff_t (*llseek)(struct file *, loff_t, int);
518 loff_t ret;
520 llseek = proc_file->f_path.dentry->d_inode->i_fop->llseek;
521 if (llseek != NULL) {
522 ret = (*llseek)(proc_file, off, where);
523 if (ret < 0)
524 return ret;
527 return default_llseek(file, off, where);
530 static const struct file_operations hppfs_file_fops = {
531 .owner = NULL,
532 .llseek = hppfs_llseek,
533 .read = hppfs_read,
534 .write = hppfs_write,
535 .open = hppfs_open,
538 struct hppfs_dirent {
539 void *vfs_dirent;
540 filldir_t filldir;
541 struct dentry *dentry;
544 static int hppfs_filldir(void *d, const char *name, int size,
545 loff_t offset, u64 inode, unsigned int type)
547 struct hppfs_dirent *dirent = d;
549 if (file_removed(dirent->dentry, name))
550 return 0;
552 return (*dirent->filldir)(dirent->vfs_dirent, name, size, offset,
553 inode, type);
556 static int hppfs_readdir(struct file *file, void *ent, filldir_t filldir)
558 struct hppfs_private *data = file->private_data;
559 struct file *proc_file = data->proc_file;
560 int (*readdir)(struct file *, void *, filldir_t);
561 struct hppfs_dirent dirent = ((struct hppfs_dirent)
562 { .vfs_dirent = ent,
563 .filldir = filldir,
564 .dentry = file->f_path.dentry
566 int err;
568 readdir = proc_file->f_path.dentry->d_inode->i_fop->readdir;
570 proc_file->f_pos = file->f_pos;
571 err = (*readdir)(proc_file, &dirent, hppfs_filldir);
572 file->f_pos = proc_file->f_pos;
574 return err;
577 static int hppfs_fsync(struct file *file, loff_t start, loff_t end,
578 int datasync)
580 return filemap_write_and_wait_range(file->f_mapping, start, end);
583 static const struct file_operations hppfs_dir_fops = {
584 .owner = NULL,
585 .readdir = hppfs_readdir,
586 .open = hppfs_dir_open,
587 .fsync = hppfs_fsync,
588 .llseek = default_llseek,
591 static int hppfs_statfs(struct dentry *dentry, struct kstatfs *sf)
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;
599 return 0;
602 static struct inode *hppfs_alloc_inode(struct super_block *sb)
604 struct hppfs_inode_info *hi;
606 hi = kmalloc(sizeof(*hi), GFP_KERNEL);
607 if (!hi)
608 return NULL;
610 hi->proc_dentry = NULL;
611 inode_init_once(&hi->vfs_inode);
612 return &hi->vfs_inode;
615 void hppfs_evict_inode(struct inode *ino)
617 clear_inode(ino);
618 dput(HPPFS_I(ino)->proc_dentry);
619 mntput(ino->i_sb->s_fs_info);
622 static void hppfs_i_callback(struct rcu_head *head)
624 struct inode *inode = container_of(head, struct inode, i_rcu);
625 kfree(HPPFS_I(inode));
628 static void hppfs_destroy_inode(struct inode *inode)
630 call_rcu(&inode->i_rcu, hppfs_i_callback);
633 static const struct super_operations hppfs_sbops = {
634 .alloc_inode = hppfs_alloc_inode,
635 .destroy_inode = hppfs_destroy_inode,
636 .evict_inode = hppfs_evict_inode,
637 .statfs = hppfs_statfs,
640 static int hppfs_readlink(struct dentry *dentry, char __user *buffer,
641 int buflen)
643 struct dentry *proc_dentry = HPPFS_I(dentry->d_inode)->proc_dentry;
644 return proc_dentry->d_inode->i_op->readlink(proc_dentry, buffer,
645 buflen);
648 static void *hppfs_follow_link(struct dentry *dentry, struct nameidata *nd)
650 struct dentry *proc_dentry = HPPFS_I(dentry->d_inode)->proc_dentry;
652 return proc_dentry->d_inode->i_op->follow_link(proc_dentry, nd);
655 static void hppfs_put_link(struct dentry *dentry, struct nameidata *nd,
656 void *cookie)
658 struct dentry *proc_dentry = HPPFS_I(dentry->d_inode)->proc_dentry;
660 if (proc_dentry->d_inode->i_op->put_link)
661 proc_dentry->d_inode->i_op->put_link(proc_dentry, nd, cookie);
664 static const struct inode_operations hppfs_dir_iops = {
665 .lookup = hppfs_lookup,
668 static const struct inode_operations hppfs_link_iops = {
669 .readlink = hppfs_readlink,
670 .follow_link = hppfs_follow_link,
671 .put_link = hppfs_put_link,
674 static struct inode *get_inode(struct super_block *sb, struct dentry *dentry)
676 struct inode *proc_ino = dentry->d_inode;
677 struct inode *inode = new_inode(sb);
679 if (!inode) {
680 dput(dentry);
681 return ERR_PTR(-ENOMEM);
684 if (S_ISDIR(dentry->d_inode->i_mode)) {
685 inode->i_op = &hppfs_dir_iops;
686 inode->i_fop = &hppfs_dir_fops;
687 } else if (S_ISLNK(dentry->d_inode->i_mode)) {
688 inode->i_op = &hppfs_link_iops;
689 inode->i_fop = &hppfs_file_fops;
690 } else {
691 inode->i_op = &hppfs_file_iops;
692 inode->i_fop = &hppfs_file_fops;
695 HPPFS_I(inode)->proc_dentry = dentry;
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 set_nlink(inode, proc_ino->i_nlink);
705 inode->i_size = proc_ino->i_size;
706 inode->i_blocks = proc_ino->i_blocks;
708 return inode;
711 static int hppfs_fill_super(struct super_block *sb, void *d, int silent)
713 struct inode *root_inode;
714 struct vfsmount *proc_mnt;
715 int err = -ENOENT;
717 proc_mnt = mntget(current->nsproxy->pid_ns->proc_mnt);
718 if (IS_ERR(proc_mnt))
719 goto out;
721 sb->s_blocksize = 1024;
722 sb->s_blocksize_bits = 10;
723 sb->s_magic = HPPFS_SUPER_MAGIC;
724 sb->s_op = &hppfs_sbops;
725 sb->s_fs_info = proc_mnt;
727 err = -ENOMEM;
728 root_inode = get_inode(sb, dget(proc_mnt->mnt_root));
729 sb->s_root = d_make_root(root_inode);
730 if (!sb->s_root)
731 goto out_mntput;
733 return 0;
735 out_mntput:
736 mntput(proc_mnt);
737 out:
738 return(err);
741 static struct dentry *hppfs_read_super(struct file_system_type *type,
742 int flags, const char *dev_name,
743 void *data)
745 return mount_nodev(type, flags, data, hppfs_fill_super);
748 static struct file_system_type hppfs_type = {
749 .owner = THIS_MODULE,
750 .name = "hppfs",
751 .mount = hppfs_read_super,
752 .kill_sb = kill_anon_super,
753 .fs_flags = 0,
756 static int __init init_hppfs(void)
758 return register_filesystem(&hppfs_type);
761 static void __exit exit_hppfs(void)
763 unregister_filesystem(&hppfs_type);
766 module_init(init_hppfs)
767 module_exit(exit_hppfs)
768 MODULE_LICENSE("GPL");