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