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