1 /* 2 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) 3 * Licensed under the GPL 4 * 5 * Ported the filesystem routines to 2.5. 6 * 2003-02-10 Petr Baudis <pasky@ucw.cz> 7 */ 8 9 #include <linux/fs.h> 10 #include <linux/module.h> 11 #include <linux/mm.h> 12 #include <linux/pagemap.h> 13 #include <linux/statfs.h> 14 #include <linux/seq_file.h> 15 #include <linux/mount.h> 16 #include "hostfs.h" 17 #include "init.h" 18 #include "kern.h" 19 20 struct hostfs_inode_info { 21 char *host_filename; 22 int fd; 23 int mode; 24 struct inode vfs_inode; 25 }; 26 27 static inline struct hostfs_inode_info *HOSTFS_I(struct inode *inode) 28 { 29 return list_entry(inode, struct hostfs_inode_info, vfs_inode); 30 } 31 32 #define FILE_HOSTFS_I(file) HOSTFS_I((file)->f_path.dentry->d_inode) 33 34 int hostfs_d_delete(struct dentry *dentry) 35 { 36 return 1; 37 } 38 39 struct dentry_operations hostfs_dentry_ops = { 40 .d_delete = hostfs_d_delete, 41 }; 42 43 /* Changed in hostfs_args before the kernel starts running */ 44 static char *root_ino = ""; 45 static int append = 0; 46 47 #define HOSTFS_SUPER_MAGIC 0x00c0ffee 48 49 static const struct inode_operations hostfs_iops; 50 static const struct inode_operations hostfs_dir_iops; 51 static const struct address_space_operations hostfs_link_aops; 52 53 #ifndef MODULE 54 static int __init hostfs_args(char *options, int *add) 55 { 56 char *ptr; 57 58 ptr = strchr(options, ','); 59 if (ptr != NULL) 60 *ptr++ = '\0'; 61 if (*options != '\0') 62 root_ino = options; 63 64 options = ptr; 65 while (options) { 66 ptr = strchr(options, ','); 67 if (ptr != NULL) 68 *ptr++ = '\0'; 69 if (*options != '\0') { 70 if (!strcmp(options, "append")) 71 append = 1; 72 else printf("hostfs_args - unsupported option - %s\n", 73 options); 74 } 75 options = ptr; 76 } 77 return 0; 78 } 79 80 __uml_setup("hostfs=", hostfs_args, 81 "hostfs=<root dir>,<flags>,...\n" 82 " This is used to set hostfs parameters. The root directory argument\n" 83 " is used to confine all hostfs mounts to within the specified directory\n" 84 " tree on the host. If this isn't specified, then a user inside UML can\n" 85 " mount anything on the host that's accessible to the user that's running\n" 86 " it.\n" 87 " The only flag currently supported is 'append', which specifies that all\n" 88 " files opened by hostfs will be opened in append mode.\n\n" 89 ); 90 #endif 91 92 static char *dentry_name(struct dentry *dentry, int extra) 93 { 94 struct dentry *parent; 95 char *root, *name; 96 int len; 97 98 len = 0; 99 parent = dentry; 100 while (parent->d_parent != parent) { 101 len += parent->d_name.len + 1; 102 parent = parent->d_parent; 103 } 104 105 root = HOSTFS_I(parent->d_inode)->host_filename; 106 len += strlen(root); 107 name = kmalloc(len + extra + 1, GFP_KERNEL); 108 if (name == NULL) 109 return NULL; 110 111 name[len] = '\0'; 112 parent = dentry; 113 while (parent->d_parent != parent) { 114 len -= parent->d_name.len + 1; 115 name[len] = '/'; 116 strncpy(&name[len + 1], parent->d_name.name, 117 parent->d_name.len); 118 parent = parent->d_parent; 119 } 120 strncpy(name, root, strlen(root)); 121 return name; 122 } 123 124 static char *inode_name(struct inode *ino, int extra) 125 { 126 struct dentry *dentry; 127 128 dentry = list_entry(ino->i_dentry.next, struct dentry, d_alias); 129 return dentry_name(dentry, extra); 130 } 131 132 static int read_name(struct inode *ino, char *name) 133 { 134 /* 135 * The non-int inode fields are copied into ints by stat_file and 136 * then copied into the inode because passing the actual pointers 137 * in and having them treated as int * breaks on big-endian machines 138 */ 139 int err; 140 int i_mode, i_nlink, i_blksize; 141 unsigned long long i_size; 142 unsigned long long i_ino; 143 unsigned long long i_blocks; 144 145 err = stat_file(name, &i_ino, &i_mode, &i_nlink, &ino->i_uid, 146 &ino->i_gid, &i_size, &ino->i_atime, &ino->i_mtime, 147 &ino->i_ctime, &i_blksize, &i_blocks, -1); 148 if (err) 149 return err; 150 151 ino->i_ino = i_ino; 152 ino->i_mode = i_mode; 153 ino->i_nlink = i_nlink; 154 ino->i_size = i_size; 155 ino->i_blocks = i_blocks; 156 return 0; 157 } 158 159 static char *follow_link(char *link) 160 { 161 int len, n; 162 char *name, *resolved, *end; 163 164 len = 64; 165 while (1) { 166 n = -ENOMEM; 167 name = kmalloc(len, GFP_KERNEL); 168 if (name == NULL) 169 goto out; 170 171 n = do_readlink(link, name, len); 172 if (n < len) 173 break; 174 len *= 2; 175 kfree(name); 176 } 177 if (n < 0) 178 goto out_free; 179 180 if (*name == '/') 181 return name; 182 183 end = strrchr(link, '/'); 184 if (end == NULL) 185 return name; 186 187 *(end + 1) = '\0'; 188 len = strlen(link) + strlen(name) + 1; 189 190 resolved = kmalloc(len, GFP_KERNEL); 191 if (resolved == NULL) { 192 n = -ENOMEM; 193 goto out_free; 194 } 195 196 sprintf(resolved, "%s%s", link, name); 197 kfree(name); 198 kfree(link); 199 return resolved; 200 201 out_free: 202 kfree(name); 203 out: 204 return ERR_PTR(n); 205 } 206 207 static int hostfs_read_inode(struct inode *ino) 208 { 209 char *name; 210 int err = 0; 211 212 /* 213 * Unfortunately, we are called from iget() when we don't have a dentry 214 * allocated yet. 215 */ 216 if (list_empty(&ino->i_dentry)) 217 goto out; 218 219 err = -ENOMEM; 220 name = inode_name(ino, 0); 221 if (name == NULL) 222 goto out; 223 224 if (file_type(name, NULL, NULL) == OS_TYPE_SYMLINK) { 225 name = follow_link(name); 226 if (IS_ERR(name)) { 227 err = PTR_ERR(name); 228 goto out; 229 } 230 } 231 232 err = read_name(ino, name); 233 kfree(name); 234 out: 235 return err; 236 } 237 238 static struct inode *hostfs_iget(struct super_block *sb) 239 { 240 struct inode *inode; 241 long ret; 242 243 inode = iget_locked(sb, 0); 244 if (!inode) 245 return ERR_PTR(-ENOMEM); 246 if (inode->i_state & I_NEW) { 247 ret = hostfs_read_inode(inode); 248 if (ret < 0) { 249 iget_failed(inode); 250 return ERR_PTR(ret); 251 } 252 unlock_new_inode(inode); 253 } 254 return inode; 255 } 256 257 int hostfs_statfs(struct dentry *dentry, struct kstatfs *sf) 258 { 259 /* 260 * do_statfs uses struct statfs64 internally, but the linux kernel 261 * struct statfs still has 32-bit versions for most of these fields, 262 * so we convert them here 263 */ 264 int err; 265 long long f_blocks; 266 long long f_bfree; 267 long long f_bavail; 268 long long f_files; 269 long long f_ffree; 270 271 err = do_statfs(HOSTFS_I(dentry->d_sb->s_root->d_inode)->host_filename, 272 &sf->f_bsize, &f_blocks, &f_bfree, &f_bavail, &f_files, 273 &f_ffree, &sf->f_fsid, sizeof(sf->f_fsid), 274 &sf->f_namelen, sf->f_spare); 275 if (err) 276 return err; 277 sf->f_blocks = f_blocks; 278 sf->f_bfree = f_bfree; 279 sf->f_bavail = f_bavail; 280 sf->f_files = f_files; 281 sf->f_ffree = f_ffree; 282 sf->f_type = HOSTFS_SUPER_MAGIC; 283 return 0; 284 } 285 286 static struct inode *hostfs_alloc_inode(struct super_block *sb) 287 { 288 struct hostfs_inode_info *hi; 289 290 hi = kmalloc(sizeof(*hi), GFP_KERNEL); 291 if (hi == NULL) 292 return NULL; 293 294 *hi = ((struct hostfs_inode_info) { .host_filename = NULL, 295 .fd = -1, 296 .mode = 0 }); 297 inode_init_once(&hi->vfs_inode); 298 return &hi->vfs_inode; 299 } 300 301 static void hostfs_delete_inode(struct inode *inode) 302 { 303 truncate_inode_pages(&inode->i_data, 0); 304 if (HOSTFS_I(inode)->fd != -1) { 305 close_file(&HOSTFS_I(inode)->fd); 306 HOSTFS_I(inode)->fd = -1; 307 } 308 clear_inode(inode); 309 } 310 311 static void hostfs_destroy_inode(struct inode *inode) 312 { 313 kfree(HOSTFS_I(inode)->host_filename); 314 315 /* 316 * XXX: This should not happen, probably. The check is here for 317 * additional safety. 318 */ 319 if (HOSTFS_I(inode)->fd != -1) { 320 close_file(&HOSTFS_I(inode)->fd); 321 printk(KERN_DEBUG "Closing host fd in .destroy_inode\n"); 322 } 323 324 kfree(HOSTFS_I(inode)); 325 } 326 327 static int hostfs_show_options(struct seq_file *seq, struct vfsmount *vfs) 328 { 329 struct inode *root = vfs->mnt_sb->s_root->d_inode; 330 const char *root_path = HOSTFS_I(root)->host_filename; 331 size_t offset = strlen(root_ino) + 1; 332 333 if (strlen(root_path) > offset) 334 seq_printf(seq, ",%s", root_path + offset); 335 336 return 0; 337 } 338 339 static const struct super_operations hostfs_sbops = { 340 .alloc_inode = hostfs_alloc_inode, 341 .drop_inode = generic_delete_inode, 342 .delete_inode = hostfs_delete_inode, 343 .destroy_inode = hostfs_destroy_inode, 344 .statfs = hostfs_statfs, 345 .show_options = hostfs_show_options, 346 }; 347 348 int hostfs_readdir(struct file *file, void *ent, filldir_t filldir) 349 { 350 void *dir; 351 char *name; 352 unsigned long long next, ino; 353 int error, len; 354 355 name = dentry_name(file->f_path.dentry, 0); 356 if (name == NULL) 357 return -ENOMEM; 358 dir = open_dir(name, &error); 359 kfree(name); 360 if (dir == NULL) 361 return -error; 362 next = file->f_pos; 363 while ((name = read_dir(dir, &next, &ino, &len)) != NULL) { 364 error = (*filldir)(ent, name, len, file->f_pos, 365 ino, DT_UNKNOWN); 366 if (error) break; 367 file->f_pos = next; 368 } 369 close_dir(dir); 370 return 0; 371 } 372 373 int hostfs_file_open(struct inode *ino, struct file *file) 374 { 375 char *name; 376 int mode = 0, r = 0, w = 0, fd; 377 378 mode = file->f_mode & (FMODE_READ | FMODE_WRITE); 379 if ((mode & HOSTFS_I(ino)->mode) == mode) 380 return 0; 381 382 /* 383 * The file may already have been opened, but with the wrong access, 384 * so this resets things and reopens the file with the new access. 385 */ 386 if (HOSTFS_I(ino)->fd != -1) { 387 close_file(&HOSTFS_I(ino)->fd); 388 HOSTFS_I(ino)->fd = -1; 389 } 390 391 HOSTFS_I(ino)->mode |= mode; 392 if (HOSTFS_I(ino)->mode & FMODE_READ) 393 r = 1; 394 if (HOSTFS_I(ino)->mode & FMODE_WRITE) 395 w = 1; 396 if (w) 397 r = 1; 398 399 name = dentry_name(file->f_path.dentry, 0); 400 if (name == NULL) 401 return -ENOMEM; 402 403 fd = open_file(name, r, w, append); 404 kfree(name); 405 if (fd < 0) 406 return fd; 407 FILE_HOSTFS_I(file)->fd = fd; 408 409 return 0; 410 } 411 412 int hostfs_fsync(struct file *file, struct dentry *dentry, int datasync) 413 { 414 return fsync_file(HOSTFS_I(dentry->d_inode)->fd, datasync); 415 } 416 417 static const struct file_operations hostfs_file_fops = { 418 .llseek = generic_file_llseek, 419 .read = do_sync_read, 420 .splice_read = generic_file_splice_read, 421 .aio_read = generic_file_aio_read, 422 .aio_write = generic_file_aio_write, 423 .write = do_sync_write, 424 .mmap = generic_file_mmap, 425 .open = hostfs_file_open, 426 .release = NULL, 427 .fsync = hostfs_fsync, 428 }; 429 430 static const struct file_operations hostfs_dir_fops = { 431 .llseek = generic_file_llseek, 432 .readdir = hostfs_readdir, 433 .read = generic_read_dir, 434 }; 435 436 int hostfs_writepage(struct page *page, struct writeback_control *wbc) 437 { 438 struct address_space *mapping = page->mapping; 439 struct inode *inode = mapping->host; 440 char *buffer; 441 unsigned long long base; 442 int count = PAGE_CACHE_SIZE; 443 int end_index = inode->i_size >> PAGE_CACHE_SHIFT; 444 int err; 445 446 if (page->index >= end_index) 447 count = inode->i_size & (PAGE_CACHE_SIZE-1); 448 449 buffer = kmap(page); 450 base = ((unsigned long long) page->index) << PAGE_CACHE_SHIFT; 451 452 err = write_file(HOSTFS_I(inode)->fd, &base, buffer, count); 453 if (err != count) { 454 ClearPageUptodate(page); 455 goto out; 456 } 457 458 if (base > inode->i_size) 459 inode->i_size = base; 460 461 if (PageError(page)) 462 ClearPageError(page); 463 err = 0; 464 465 out: 466 kunmap(page); 467 468 unlock_page(page); 469 return err; 470 } 471 472 int hostfs_readpage(struct file *file, struct page *page) 473 { 474 char *buffer; 475 long long start; 476 int err = 0; 477 478 start = (long long) page->index << PAGE_CACHE_SHIFT; 479 buffer = kmap(page); 480 err = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer, 481 PAGE_CACHE_SIZE); 482 if (err < 0) 483 goto out; 484 485 memset(&buffer[err], 0, PAGE_CACHE_SIZE - err); 486 487 flush_dcache_page(page); 488 SetPageUptodate(page); 489 if (PageError(page)) ClearPageError(page); 490 err = 0; 491 out: 492 kunmap(page); 493 unlock_page(page); 494 return err; 495 } 496 497 int hostfs_write_begin(struct file *file, struct address_space *mapping, 498 loff_t pos, unsigned len, unsigned flags, 499 struct page **pagep, void **fsdata) 500 { 501 pgoff_t index = pos >> PAGE_CACHE_SHIFT; 502 503 *pagep = __grab_cache_page(mapping, index); 504 if (!*pagep) 505 return -ENOMEM; 506 return 0; 507 } 508 509 int hostfs_write_end(struct file *file, struct address_space *mapping, 510 loff_t pos, unsigned len, unsigned copied, 511 struct page *page, void *fsdata) 512 { 513 struct inode *inode = mapping->host; 514 void *buffer; 515 unsigned from = pos & (PAGE_CACHE_SIZE - 1); 516 int err; 517 518 buffer = kmap(page); 519 err = write_file(FILE_HOSTFS_I(file)->fd, &pos, buffer + from, copied); 520 kunmap(page); 521 522 if (!PageUptodate(page) && err == PAGE_CACHE_SIZE) 523 SetPageUptodate(page); 524 525 /* 526 * If err > 0, write_file has added err to pos, so we are comparing 527 * i_size against the last byte written. 528 */ 529 if (err > 0 && (pos > inode->i_size)) 530 inode->i_size = pos; 531 unlock_page(page); 532 page_cache_release(page); 533 534 return err; 535 } 536 537 static const struct address_space_operations hostfs_aops = { 538 .writepage = hostfs_writepage, 539 .readpage = hostfs_readpage, 540 .set_page_dirty = __set_page_dirty_nobuffers, 541 .write_begin = hostfs_write_begin, 542 .write_end = hostfs_write_end, 543 }; 544 545 static int init_inode(struct inode *inode, struct dentry *dentry) 546 { 547 char *name; 548 int type, err = -ENOMEM; 549 int maj, min; 550 dev_t rdev = 0; 551 552 if (dentry) { 553 name = dentry_name(dentry, 0); 554 if (name == NULL) 555 goto out; 556 type = file_type(name, &maj, &min); 557 /* Reencode maj and min with the kernel encoding.*/ 558 rdev = MKDEV(maj, min); 559 kfree(name); 560 } 561 else type = OS_TYPE_DIR; 562 563 err = 0; 564 if (type == OS_TYPE_SYMLINK) 565 inode->i_op = &page_symlink_inode_operations; 566 else if (type == OS_TYPE_DIR) 567 inode->i_op = &hostfs_dir_iops; 568 else inode->i_op = &hostfs_iops; 569 570 if (type == OS_TYPE_DIR) inode->i_fop = &hostfs_dir_fops; 571 else inode->i_fop = &hostfs_file_fops; 572 573 if (type == OS_TYPE_SYMLINK) 574 inode->i_mapping->a_ops = &hostfs_link_aops; 575 else inode->i_mapping->a_ops = &hostfs_aops; 576 577 switch (type) { 578 case OS_TYPE_CHARDEV: 579 init_special_inode(inode, S_IFCHR, rdev); 580 break; 581 case OS_TYPE_BLOCKDEV: 582 init_special_inode(inode, S_IFBLK, rdev); 583 break; 584 case OS_TYPE_FIFO: 585 init_special_inode(inode, S_IFIFO, 0); 586 break; 587 case OS_TYPE_SOCK: 588 init_special_inode(inode, S_IFSOCK, 0); 589 break; 590 } 591 out: 592 return err; 593 } 594 595 int hostfs_create(struct inode *dir, struct dentry *dentry, int mode, 596 struct nameidata *nd) 597 { 598 struct inode *inode; 599 char *name; 600 int error, fd; 601 602 inode = hostfs_iget(dir->i_sb); 603 if (IS_ERR(inode)) { 604 error = PTR_ERR(inode); 605 goto out; 606 } 607 608 error = init_inode(inode, dentry); 609 if (error) 610 goto out_put; 611 612 error = -ENOMEM; 613 name = dentry_name(dentry, 0); 614 if (name == NULL) 615 goto out_put; 616 617 fd = file_create(name, 618 mode & S_IRUSR, mode & S_IWUSR, mode & S_IXUSR, 619 mode & S_IRGRP, mode & S_IWGRP, mode & S_IXGRP, 620 mode & S_IROTH, mode & S_IWOTH, mode & S_IXOTH); 621 if (fd < 0) 622 error = fd; 623 else error = read_name(inode, name); 624 625 kfree(name); 626 if (error) 627 goto out_put; 628 629 HOSTFS_I(inode)->fd = fd; 630 HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE; 631 d_instantiate(dentry, inode); 632 return 0; 633 634 out_put: 635 iput(inode); 636 out: 637 return error; 638 } 639 640 struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry, 641 struct nameidata *nd) 642 { 643 struct inode *inode; 644 char *name; 645 int err; 646 647 inode = hostfs_iget(ino->i_sb); 648 if (IS_ERR(inode)) { 649 err = PTR_ERR(inode); 650 goto out; 651 } 652 653 err = init_inode(inode, dentry); 654 if (err) 655 goto out_put; 656 657 err = -ENOMEM; 658 name = dentry_name(dentry, 0); 659 if (name == NULL) 660 goto out_put; 661 662 err = read_name(inode, name); 663 kfree(name); 664 if (err == -ENOENT) { 665 iput(inode); 666 inode = NULL; 667 } 668 else if (err) 669 goto out_put; 670 671 d_add(dentry, inode); 672 dentry->d_op = &hostfs_dentry_ops; 673 return NULL; 674 675 out_put: 676 iput(inode); 677 out: 678 return ERR_PTR(err); 679 } 680 681 static char *inode_dentry_name(struct inode *ino, struct dentry *dentry) 682 { 683 char *file; 684 int len; 685 686 file = inode_name(ino, dentry->d_name.len + 1); 687 if (file == NULL) 688 return NULL; 689 strcat(file, "/"); 690 len = strlen(file); 691 strncat(file, dentry->d_name.name, dentry->d_name.len); 692 file[len + dentry->d_name.len] = '\0'; 693 return file; 694 } 695 696 int hostfs_link(struct dentry *to, struct inode *ino, struct dentry *from) 697 { 698 char *from_name, *to_name; 699 int err; 700 701 if ((from_name = inode_dentry_name(ino, from)) == NULL) 702 return -ENOMEM; 703 to_name = dentry_name(to, 0); 704 if (to_name == NULL) { 705 kfree(from_name); 706 return -ENOMEM; 707 } 708 err = link_file(to_name, from_name); 709 kfree(from_name); 710 kfree(to_name); 711 return err; 712 } 713 714 int hostfs_unlink(struct inode *ino, struct dentry *dentry) 715 { 716 char *file; 717 int err; 718 719 if ((file = inode_dentry_name(ino, dentry)) == NULL) 720 return -ENOMEM; 721 if (append) 722 return -EPERM; 723 724 err = unlink_file(file); 725 kfree(file); 726 return err; 727 } 728 729 int hostfs_symlink(struct inode *ino, struct dentry *dentry, const char *to) 730 { 731 char *file; 732 int err; 733 734 if ((file = inode_dentry_name(ino, dentry)) == NULL) 735 return -ENOMEM; 736 err = make_symlink(file, to); 737 kfree(file); 738 return err; 739 } 740 741 int hostfs_mkdir(struct inode *ino, struct dentry *dentry, int mode) 742 { 743 char *file; 744 int err; 745 746 if ((file = inode_dentry_name(ino, dentry)) == NULL) 747 return -ENOMEM; 748 err = do_mkdir(file, mode); 749 kfree(file); 750 return err; 751 } 752 753 int hostfs_rmdir(struct inode *ino, struct dentry *dentry) 754 { 755 char *file; 756 int err; 757 758 if ((file = inode_dentry_name(ino, dentry)) == NULL) 759 return -ENOMEM; 760 err = do_rmdir(file); 761 kfree(file); 762 return err; 763 } 764 765 int hostfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) 766 { 767 struct inode *inode; 768 char *name; 769 int err; 770 771 inode = hostfs_iget(dir->i_sb); 772 if (IS_ERR(inode)) { 773 err = PTR_ERR(inode); 774 goto out; 775 } 776 777 err = init_inode(inode, dentry); 778 if (err) 779 goto out_put; 780 781 err = -ENOMEM; 782 name = dentry_name(dentry, 0); 783 if (name == NULL) 784 goto out_put; 785 786 init_special_inode(inode, mode, dev); 787 err = do_mknod(name, mode, MAJOR(dev), MINOR(dev)); 788 if (err) 789 goto out_free; 790 791 err = read_name(inode, name); 792 kfree(name); 793 if (err) 794 goto out_put; 795 796 d_instantiate(dentry, inode); 797 return 0; 798 799 out_free: 800 kfree(name); 801 out_put: 802 iput(inode); 803 out: 804 return err; 805 } 806 807 int hostfs_rename(struct inode *from_ino, struct dentry *from, 808 struct inode *to_ino, struct dentry *to) 809 { 810 char *from_name, *to_name; 811 int err; 812 813 if ((from_name = inode_dentry_name(from_ino, from)) == NULL) 814 return -ENOMEM; 815 if ((to_name = inode_dentry_name(to_ino, to)) == NULL) { 816 kfree(from_name); 817 return -ENOMEM; 818 } 819 err = rename_file(from_name, to_name); 820 kfree(from_name); 821 kfree(to_name); 822 return err; 823 } 824 825 int hostfs_permission(struct inode *ino, int desired, struct nameidata *nd) 826 { 827 char *name; 828 int r = 0, w = 0, x = 0, err; 829 830 if (desired & MAY_READ) r = 1; 831 if (desired & MAY_WRITE) w = 1; 832 if (desired & MAY_EXEC) x = 1; 833 name = inode_name(ino, 0); 834 if (name == NULL) 835 return -ENOMEM; 836 837 if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) || 838 S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode)) 839 err = 0; 840 else 841 err = access_file(name, r, w, x); 842 kfree(name); 843 if (!err) 844 err = generic_permission(ino, desired, NULL); 845 return err; 846 } 847 848 int hostfs_setattr(struct dentry *dentry, struct iattr *attr) 849 { 850 struct hostfs_iattr attrs; 851 char *name; 852 int err; 853 854 int fd = HOSTFS_I(dentry->d_inode)->fd; 855 856 err = inode_change_ok(dentry->d_inode, attr); 857 if (err) 858 return err; 859 860 if (append) 861 attr->ia_valid &= ~ATTR_SIZE; 862 863 attrs.ia_valid = 0; 864 if (attr->ia_valid & ATTR_MODE) { 865 attrs.ia_valid |= HOSTFS_ATTR_MODE; 866 attrs.ia_mode = attr->ia_mode; 867 } 868 if (attr->ia_valid & ATTR_UID) { 869 attrs.ia_valid |= HOSTFS_ATTR_UID; 870 attrs.ia_uid = attr->ia_uid; 871 } 872 if (attr->ia_valid & ATTR_GID) { 873 attrs.ia_valid |= HOSTFS_ATTR_GID; 874 attrs.ia_gid = attr->ia_gid; 875 } 876 if (attr->ia_valid & ATTR_SIZE) { 877 attrs.ia_valid |= HOSTFS_ATTR_SIZE; 878 attrs.ia_size = attr->ia_size; 879 } 880 if (attr->ia_valid & ATTR_ATIME) { 881 attrs.ia_valid |= HOSTFS_ATTR_ATIME; 882 attrs.ia_atime = attr->ia_atime; 883 } 884 if (attr->ia_valid & ATTR_MTIME) { 885 attrs.ia_valid |= HOSTFS_ATTR_MTIME; 886 attrs.ia_mtime = attr->ia_mtime; 887 } 888 if (attr->ia_valid & ATTR_CTIME) { 889 attrs.ia_valid |= HOSTFS_ATTR_CTIME; 890 attrs.ia_ctime = attr->ia_ctime; 891 } 892 if (attr->ia_valid & ATTR_ATIME_SET) { 893 attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET; 894 } 895 if (attr->ia_valid & ATTR_MTIME_SET) { 896 attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET; 897 } 898 name = dentry_name(dentry, 0); 899 if (name == NULL) 900 return -ENOMEM; 901 err = set_attr(name, &attrs, fd); 902 kfree(name); 903 if (err) 904 return err; 905 906 return inode_setattr(dentry->d_inode, attr); 907 } 908 909 static const struct inode_operations hostfs_iops = { 910 .create = hostfs_create, 911 .link = hostfs_link, 912 .unlink = hostfs_unlink, 913 .symlink = hostfs_symlink, 914 .mkdir = hostfs_mkdir, 915 .rmdir = hostfs_rmdir, 916 .mknod = hostfs_mknod, 917 .rename = hostfs_rename, 918 .permission = hostfs_permission, 919 .setattr = hostfs_setattr, 920 }; 921 922 static const struct inode_operations hostfs_dir_iops = { 923 .create = hostfs_create, 924 .lookup = hostfs_lookup, 925 .link = hostfs_link, 926 .unlink = hostfs_unlink, 927 .symlink = hostfs_symlink, 928 .mkdir = hostfs_mkdir, 929 .rmdir = hostfs_rmdir, 930 .mknod = hostfs_mknod, 931 .rename = hostfs_rename, 932 .permission = hostfs_permission, 933 .setattr = hostfs_setattr, 934 }; 935 936 int hostfs_link_readpage(struct file *file, struct page *page) 937 { 938 char *buffer, *name; 939 int err; 940 941 buffer = kmap(page); 942 name = inode_name(page->mapping->host, 0); 943 if (name == NULL) 944 return -ENOMEM; 945 err = do_readlink(name, buffer, PAGE_CACHE_SIZE); 946 kfree(name); 947 if (err == PAGE_CACHE_SIZE) 948 err = -E2BIG; 949 else if (err > 0) { 950 flush_dcache_page(page); 951 SetPageUptodate(page); 952 if (PageError(page)) ClearPageError(page); 953 err = 0; 954 } 955 kunmap(page); 956 unlock_page(page); 957 return err; 958 } 959 960 static const struct address_space_operations hostfs_link_aops = { 961 .readpage = hostfs_link_readpage, 962 }; 963 964 static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent) 965 { 966 struct inode *root_inode; 967 char *host_root_path, *req_root = d; 968 int err; 969 970 sb->s_blocksize = 1024; 971 sb->s_blocksize_bits = 10; 972 sb->s_magic = HOSTFS_SUPER_MAGIC; 973 sb->s_op = &hostfs_sbops; 974 975 /* NULL is printed as <NULL> by sprintf: avoid that. */ 976 if (req_root == NULL) 977 req_root = ""; 978 979 err = -ENOMEM; 980 host_root_path = kmalloc(strlen(root_ino) + 1 981 + strlen(req_root) + 1, GFP_KERNEL); 982 if (host_root_path == NULL) 983 goto out; 984 985 sprintf(host_root_path, "%s/%s", root_ino, req_root); 986 987 root_inode = hostfs_iget(sb); 988 if (IS_ERR(root_inode)) { 989 err = PTR_ERR(root_inode); 990 goto out_free; 991 } 992 993 err = init_inode(root_inode, NULL); 994 if (err) 995 goto out_put; 996 997 HOSTFS_I(root_inode)->host_filename = host_root_path; 998 /* 999 * Avoid that in the error path, iput(root_inode) frees again 1000 * host_root_path through hostfs_destroy_inode! 1001 */ 1002 host_root_path = NULL; 1003 1004 err = -ENOMEM; 1005 sb->s_root = d_alloc_root(root_inode); 1006 if (sb->s_root == NULL) 1007 goto out_put; 1008 1009 err = hostfs_read_inode(root_inode); 1010 if (err) { 1011 /* No iput in this case because the dput does that for us */ 1012 dput(sb->s_root); 1013 sb->s_root = NULL; 1014 goto out; 1015 } 1016 1017 return 0; 1018 1019 out_put: 1020 iput(root_inode); 1021 out_free: 1022 kfree(host_root_path); 1023 out: 1024 return err; 1025 } 1026 1027 static int hostfs_read_sb(struct file_system_type *type, 1028 int flags, const char *dev_name, 1029 void *data, struct vfsmount *mnt) 1030 { 1031 return get_sb_nodev(type, flags, data, hostfs_fill_sb_common, mnt); 1032 } 1033 1034 static struct file_system_type hostfs_type = { 1035 .owner = THIS_MODULE, 1036 .name = "hostfs", 1037 .get_sb = hostfs_read_sb, 1038 .kill_sb = kill_anon_super, 1039 .fs_flags = 0, 1040 }; 1041 1042 static int __init init_hostfs(void) 1043 { 1044 return register_filesystem(&hostfs_type); 1045 } 1046 1047 static void __exit exit_hostfs(void) 1048 { 1049 unregister_filesystem(&hostfs_type); 1050 } 1051 1052 module_init(init_hostfs) 1053 module_exit(exit_hostfs) 1054 MODULE_LICENSE("GPL"); 1055