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