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