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/magic.h> 11 #include <linux/module.h> 12 #include <linux/mm.h> 13 #include <linux/pagemap.h> 14 #include <linux/statfs.h> 15 #include <linux/slab.h> 16 #include <linux/seq_file.h> 17 #include <linux/writeback.h> 18 #include <linux/mount.h> 19 #include <linux/fs_context.h> 20 #include <linux/fs_parser.h> 21 #include <linux/namei.h> 22 #include "hostfs.h" 23 #include <init.h> 24 #include <kern.h> 25 26 struct hostfs_fs_info { 27 char *host_root_path; 28 }; 29 30 struct hostfs_inode_info { 31 int fd; 32 fmode_t mode; 33 struct inode vfs_inode; 34 struct mutex open_mutex; 35 dev_t dev; 36 }; 37 38 static inline struct hostfs_inode_info *HOSTFS_I(struct inode *inode) 39 { 40 return list_entry(inode, struct hostfs_inode_info, vfs_inode); 41 } 42 43 #define FILE_HOSTFS_I(file) HOSTFS_I(file_inode(file)) 44 45 static struct kmem_cache *hostfs_inode_cache; 46 47 /* Changed in hostfs_args before the kernel starts running */ 48 static char *root_ino = ""; 49 static int append = 0; 50 51 static const struct inode_operations hostfs_iops; 52 static const struct inode_operations hostfs_dir_iops; 53 static const struct inode_operations hostfs_link_iops; 54 55 #ifndef MODULE 56 static int __init hostfs_args(char *options, int *add) 57 { 58 char *ptr; 59 60 ptr = strchr(options, ','); 61 if (ptr != NULL) 62 *ptr++ = '\0'; 63 if (*options != '\0') 64 root_ino = options; 65 66 options = ptr; 67 while (options) { 68 ptr = strchr(options, ','); 69 if (ptr != NULL) 70 *ptr++ = '\0'; 71 if (*options != '\0') { 72 if (!strcmp(options, "append")) 73 append = 1; 74 else printf("hostfs_args - unsupported option - %s\n", 75 options); 76 } 77 options = ptr; 78 } 79 return 0; 80 } 81 82 __uml_setup("hostfs=", hostfs_args, 83 "hostfs=<root dir>,<flags>,...\n" 84 " This is used to set hostfs parameters. The root directory argument\n" 85 " is used to confine all hostfs mounts to within the specified directory\n" 86 " tree on the host. If this isn't specified, then a user inside UML can\n" 87 " mount anything on the host that's accessible to the user that's running\n" 88 " it.\n" 89 " The only flag currently supported is 'append', which specifies that all\n" 90 " files opened by hostfs will be opened in append mode.\n\n" 91 ); 92 #endif 93 94 static char *__dentry_name(struct dentry *dentry, char *name) 95 { 96 char *p = dentry_path_raw(dentry, name, PATH_MAX); 97 char *root; 98 size_t len; 99 struct hostfs_fs_info *fsi; 100 101 fsi = dentry->d_sb->s_fs_info; 102 root = fsi->host_root_path; 103 len = strlen(root); 104 if (IS_ERR(p)) { 105 __putname(name); 106 return NULL; 107 } 108 109 /* 110 * This function relies on the fact that dentry_path_raw() will place 111 * the path name at the end of the provided buffer. 112 */ 113 BUG_ON(p + strlen(p) + 1 != name + PATH_MAX); 114 115 strscpy(name, root, PATH_MAX); 116 if (len > p - name) { 117 __putname(name); 118 return NULL; 119 } 120 121 if (p > name + len) 122 strcpy(name + len, p); 123 124 return name; 125 } 126 127 static char *dentry_name(struct dentry *dentry) 128 { 129 char *name = __getname(); 130 if (!name) 131 return NULL; 132 133 return __dentry_name(dentry, name); 134 } 135 136 static char *inode_name(struct inode *ino) 137 { 138 struct dentry *dentry; 139 char *name; 140 141 dentry = d_find_alias(ino); 142 if (!dentry) 143 return NULL; 144 145 name = dentry_name(dentry); 146 147 dput(dentry); 148 149 return name; 150 } 151 152 static char *follow_link(char *link) 153 { 154 char *name, *resolved, *end; 155 int n; 156 157 name = kmalloc(PATH_MAX, GFP_KERNEL); 158 if (!name) { 159 n = -ENOMEM; 160 goto out_free; 161 } 162 163 n = hostfs_do_readlink(link, name, PATH_MAX); 164 if (n < 0) 165 goto out_free; 166 else if (n == PATH_MAX) { 167 n = -E2BIG; 168 goto out_free; 169 } 170 171 if (*name == '/') 172 return name; 173 174 end = strrchr(link, '/'); 175 if (end == NULL) 176 return name; 177 178 *(end + 1) = '\0'; 179 180 resolved = kasprintf(GFP_KERNEL, "%s%s", link, name); 181 if (resolved == NULL) { 182 n = -ENOMEM; 183 goto out_free; 184 } 185 186 kfree(name); 187 return resolved; 188 189 out_free: 190 kfree(name); 191 return ERR_PTR(n); 192 } 193 194 static int hostfs_statfs(struct dentry *dentry, struct kstatfs *sf) 195 { 196 /* 197 * do_statfs uses struct statfs64 internally, but the linux kernel 198 * struct statfs still has 32-bit versions for most of these fields, 199 * so we convert them here 200 */ 201 int err; 202 long long f_blocks; 203 long long f_bfree; 204 long long f_bavail; 205 long long f_files; 206 long long f_ffree; 207 struct hostfs_fs_info *fsi; 208 209 fsi = dentry->d_sb->s_fs_info; 210 err = do_statfs(fsi->host_root_path, 211 &sf->f_bsize, &f_blocks, &f_bfree, &f_bavail, &f_files, 212 &f_ffree, &sf->f_fsid, sizeof(sf->f_fsid), 213 &sf->f_namelen); 214 if (err) 215 return err; 216 sf->f_blocks = f_blocks; 217 sf->f_bfree = f_bfree; 218 sf->f_bavail = f_bavail; 219 sf->f_files = f_files; 220 sf->f_ffree = f_ffree; 221 sf->f_type = HOSTFS_SUPER_MAGIC; 222 return 0; 223 } 224 225 static struct inode *hostfs_alloc_inode(struct super_block *sb) 226 { 227 struct hostfs_inode_info *hi; 228 229 hi = alloc_inode_sb(sb, hostfs_inode_cache, GFP_KERNEL_ACCOUNT); 230 if (hi == NULL) 231 return NULL; 232 hi->fd = -1; 233 hi->mode = 0; 234 hi->dev = 0; 235 inode_init_once(&hi->vfs_inode); 236 mutex_init(&hi->open_mutex); 237 return &hi->vfs_inode; 238 } 239 240 static void hostfs_evict_inode(struct inode *inode) 241 { 242 truncate_inode_pages_final(&inode->i_data); 243 clear_inode(inode); 244 if (HOSTFS_I(inode)->fd != -1) { 245 close_file(&HOSTFS_I(inode)->fd); 246 HOSTFS_I(inode)->fd = -1; 247 HOSTFS_I(inode)->dev = 0; 248 } 249 } 250 251 static void hostfs_free_inode(struct inode *inode) 252 { 253 kmem_cache_free(hostfs_inode_cache, HOSTFS_I(inode)); 254 } 255 256 static int hostfs_show_options(struct seq_file *seq, struct dentry *root) 257 { 258 struct hostfs_fs_info *fsi; 259 const char *root_path; 260 261 fsi = root->d_sb->s_fs_info; 262 root_path = fsi->host_root_path; 263 size_t offset = strlen(root_ino) + 1; 264 265 if (strlen(root_path) > offset) 266 seq_show_option(seq, root_path + offset, NULL); 267 268 if (append) 269 seq_puts(seq, ",append"); 270 271 return 0; 272 } 273 274 static const struct super_operations hostfs_sbops = { 275 .alloc_inode = hostfs_alloc_inode, 276 .free_inode = hostfs_free_inode, 277 .drop_inode = generic_delete_inode, 278 .evict_inode = hostfs_evict_inode, 279 .statfs = hostfs_statfs, 280 .show_options = hostfs_show_options, 281 }; 282 283 static int hostfs_readdir(struct file *file, struct dir_context *ctx) 284 { 285 void *dir; 286 char *name; 287 unsigned long long next, ino; 288 int error, len; 289 unsigned int type; 290 291 name = dentry_name(file->f_path.dentry); 292 if (name == NULL) 293 return -ENOMEM; 294 dir = open_dir(name, &error); 295 __putname(name); 296 if (dir == NULL) 297 return -error; 298 next = ctx->pos; 299 seek_dir(dir, next); 300 while ((name = read_dir(dir, &next, &ino, &len, &type)) != NULL) { 301 if (!dir_emit(ctx, name, len, ino, type)) 302 break; 303 ctx->pos = next; 304 } 305 close_dir(dir); 306 return 0; 307 } 308 309 static int hostfs_open(struct inode *ino, struct file *file) 310 { 311 char *name; 312 fmode_t mode; 313 int err; 314 int r, w, fd; 315 316 mode = file->f_mode & (FMODE_READ | FMODE_WRITE); 317 if ((mode & HOSTFS_I(ino)->mode) == mode) 318 return 0; 319 320 mode |= HOSTFS_I(ino)->mode; 321 322 retry: 323 r = w = 0; 324 325 if (mode & FMODE_READ) 326 r = 1; 327 if (mode & FMODE_WRITE) 328 r = w = 1; 329 330 name = dentry_name(file_dentry(file)); 331 if (name == NULL) 332 return -ENOMEM; 333 334 fd = open_file(name, r, w, append); 335 __putname(name); 336 if (fd < 0) 337 return fd; 338 339 mutex_lock(&HOSTFS_I(ino)->open_mutex); 340 /* somebody else had handled it first? */ 341 if ((mode & HOSTFS_I(ino)->mode) == mode) { 342 mutex_unlock(&HOSTFS_I(ino)->open_mutex); 343 close_file(&fd); 344 return 0; 345 } 346 if ((mode | HOSTFS_I(ino)->mode) != mode) { 347 mode |= HOSTFS_I(ino)->mode; 348 mutex_unlock(&HOSTFS_I(ino)->open_mutex); 349 close_file(&fd); 350 goto retry; 351 } 352 if (HOSTFS_I(ino)->fd == -1) { 353 HOSTFS_I(ino)->fd = fd; 354 } else { 355 err = replace_file(fd, HOSTFS_I(ino)->fd); 356 close_file(&fd); 357 if (err < 0) { 358 mutex_unlock(&HOSTFS_I(ino)->open_mutex); 359 return err; 360 } 361 } 362 HOSTFS_I(ino)->mode = mode; 363 mutex_unlock(&HOSTFS_I(ino)->open_mutex); 364 365 return 0; 366 } 367 368 static int hostfs_file_release(struct inode *inode, struct file *file) 369 { 370 filemap_write_and_wait(inode->i_mapping); 371 372 return 0; 373 } 374 375 static int hostfs_fsync(struct file *file, loff_t start, loff_t end, 376 int datasync) 377 { 378 struct inode *inode = file->f_mapping->host; 379 int ret; 380 381 ret = file_write_and_wait_range(file, start, end); 382 if (ret) 383 return ret; 384 385 inode_lock(inode); 386 ret = fsync_file(HOSTFS_I(inode)->fd, datasync); 387 inode_unlock(inode); 388 389 return ret; 390 } 391 392 static const struct file_operations hostfs_file_fops = { 393 .llseek = generic_file_llseek, 394 .splice_read = filemap_splice_read, 395 .splice_write = iter_file_splice_write, 396 .read_iter = generic_file_read_iter, 397 .write_iter = generic_file_write_iter, 398 .mmap = generic_file_mmap, 399 .open = hostfs_open, 400 .release = hostfs_file_release, 401 .fsync = hostfs_fsync, 402 }; 403 404 static const struct file_operations hostfs_dir_fops = { 405 .llseek = generic_file_llseek, 406 .iterate_shared = hostfs_readdir, 407 .read = generic_read_dir, 408 .open = hostfs_open, 409 .fsync = hostfs_fsync, 410 }; 411 412 static int hostfs_writepage(struct page *page, struct writeback_control *wbc) 413 { 414 struct address_space *mapping = page->mapping; 415 struct inode *inode = mapping->host; 416 char *buffer; 417 loff_t base = page_offset(page); 418 int count = PAGE_SIZE; 419 int end_index = inode->i_size >> PAGE_SHIFT; 420 int err; 421 422 if (page->index >= end_index) 423 count = inode->i_size & (PAGE_SIZE-1); 424 425 buffer = kmap_local_page(page); 426 427 err = write_file(HOSTFS_I(inode)->fd, &base, buffer, count); 428 if (err != count) { 429 if (err >= 0) 430 err = -EIO; 431 mapping_set_error(mapping, err); 432 goto out; 433 } 434 435 if (base > inode->i_size) 436 inode->i_size = base; 437 438 err = 0; 439 440 out: 441 kunmap_local(buffer); 442 unlock_page(page); 443 444 return err; 445 } 446 447 static int hostfs_read_folio(struct file *file, struct folio *folio) 448 { 449 char *buffer; 450 loff_t start = folio_pos(folio); 451 int bytes_read, ret = 0; 452 453 buffer = kmap_local_folio(folio, 0); 454 bytes_read = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer, 455 PAGE_SIZE); 456 if (bytes_read < 0) 457 ret = bytes_read; 458 else 459 buffer = folio_zero_tail(folio, bytes_read, buffer + bytes_read); 460 kunmap_local(buffer); 461 462 folio_end_read(folio, ret == 0); 463 return ret; 464 } 465 466 static int hostfs_write_begin(struct file *file, struct address_space *mapping, 467 loff_t pos, unsigned len, 468 struct page **pagep, void **fsdata) 469 { 470 pgoff_t index = pos >> PAGE_SHIFT; 471 472 *pagep = grab_cache_page_write_begin(mapping, index); 473 if (!*pagep) 474 return -ENOMEM; 475 return 0; 476 } 477 478 static int hostfs_write_end(struct file *file, struct address_space *mapping, 479 loff_t pos, unsigned len, unsigned copied, 480 struct page *page, void *fsdata) 481 { 482 struct inode *inode = mapping->host; 483 void *buffer; 484 unsigned from = pos & (PAGE_SIZE - 1); 485 int err; 486 487 buffer = kmap_local_page(page); 488 err = write_file(FILE_HOSTFS_I(file)->fd, &pos, buffer + from, copied); 489 kunmap_local(buffer); 490 491 if (!PageUptodate(page) && err == PAGE_SIZE) 492 SetPageUptodate(page); 493 494 /* 495 * If err > 0, write_file has added err to pos, so we are comparing 496 * i_size against the last byte written. 497 */ 498 if (err > 0 && (pos > inode->i_size)) 499 inode->i_size = pos; 500 unlock_page(page); 501 put_page(page); 502 503 return err; 504 } 505 506 static const struct address_space_operations hostfs_aops = { 507 .writepage = hostfs_writepage, 508 .read_folio = hostfs_read_folio, 509 .dirty_folio = filemap_dirty_folio, 510 .write_begin = hostfs_write_begin, 511 .write_end = hostfs_write_end, 512 }; 513 514 static int hostfs_inode_update(struct inode *ino, const struct hostfs_stat *st) 515 { 516 set_nlink(ino, st->nlink); 517 i_uid_write(ino, st->uid); 518 i_gid_write(ino, st->gid); 519 inode_set_atime_to_ts(ino, (struct timespec64){ 520 st->atime.tv_sec, 521 st->atime.tv_nsec, 522 }); 523 inode_set_mtime_to_ts(ino, (struct timespec64){ 524 st->mtime.tv_sec, 525 st->mtime.tv_nsec, 526 }); 527 inode_set_ctime(ino, st->ctime.tv_sec, st->ctime.tv_nsec); 528 ino->i_size = st->size; 529 ino->i_blocks = st->blocks; 530 return 0; 531 } 532 533 static int hostfs_inode_set(struct inode *ino, void *data) 534 { 535 struct hostfs_stat *st = data; 536 dev_t dev, rdev; 537 538 /* Reencode maj and min with the kernel encoding.*/ 539 rdev = MKDEV(st->rdev.maj, st->rdev.min); 540 dev = MKDEV(st->dev.maj, st->dev.min); 541 542 switch (st->mode & S_IFMT) { 543 case S_IFLNK: 544 ino->i_op = &hostfs_link_iops; 545 break; 546 case S_IFDIR: 547 ino->i_op = &hostfs_dir_iops; 548 ino->i_fop = &hostfs_dir_fops; 549 break; 550 case S_IFCHR: 551 case S_IFBLK: 552 case S_IFIFO: 553 case S_IFSOCK: 554 init_special_inode(ino, st->mode & S_IFMT, rdev); 555 ino->i_op = &hostfs_iops; 556 break; 557 case S_IFREG: 558 ino->i_op = &hostfs_iops; 559 ino->i_fop = &hostfs_file_fops; 560 ino->i_mapping->a_ops = &hostfs_aops; 561 break; 562 default: 563 return -EIO; 564 } 565 566 HOSTFS_I(ino)->dev = dev; 567 ino->i_ino = st->ino; 568 ino->i_mode = st->mode; 569 return hostfs_inode_update(ino, st); 570 } 571 572 static int hostfs_inode_test(struct inode *inode, void *data) 573 { 574 const struct hostfs_stat *st = data; 575 dev_t dev = MKDEV(st->dev.maj, st->dev.min); 576 577 return inode->i_ino == st->ino && HOSTFS_I(inode)->dev == dev; 578 } 579 580 static struct inode *hostfs_iget(struct super_block *sb, char *name) 581 { 582 struct inode *inode; 583 struct hostfs_stat st; 584 int err = stat_file(name, &st, -1); 585 586 if (err) 587 return ERR_PTR(err); 588 589 inode = iget5_locked(sb, st.ino, hostfs_inode_test, hostfs_inode_set, 590 &st); 591 if (!inode) 592 return ERR_PTR(-ENOMEM); 593 594 if (inode->i_state & I_NEW) { 595 unlock_new_inode(inode); 596 } else { 597 spin_lock(&inode->i_lock); 598 hostfs_inode_update(inode, &st); 599 spin_unlock(&inode->i_lock); 600 } 601 602 return inode; 603 } 604 605 static int hostfs_create(struct mnt_idmap *idmap, struct inode *dir, 606 struct dentry *dentry, umode_t mode, bool excl) 607 { 608 struct inode *inode; 609 char *name; 610 int fd; 611 612 name = dentry_name(dentry); 613 if (name == NULL) 614 return -ENOMEM; 615 616 fd = file_create(name, mode & 0777); 617 if (fd < 0) { 618 __putname(name); 619 return fd; 620 } 621 622 inode = hostfs_iget(dir->i_sb, name); 623 __putname(name); 624 if (IS_ERR(inode)) 625 return PTR_ERR(inode); 626 627 HOSTFS_I(inode)->fd = fd; 628 HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE; 629 d_instantiate(dentry, inode); 630 return 0; 631 } 632 633 static struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry, 634 unsigned int flags) 635 { 636 struct inode *inode = NULL; 637 char *name; 638 639 name = dentry_name(dentry); 640 if (name == NULL) 641 return ERR_PTR(-ENOMEM); 642 643 inode = hostfs_iget(ino->i_sb, name); 644 __putname(name); 645 if (inode == ERR_PTR(-ENOENT)) 646 inode = NULL; 647 648 return d_splice_alias(inode, dentry); 649 } 650 651 static int hostfs_link(struct dentry *to, struct inode *ino, 652 struct dentry *from) 653 { 654 char *from_name, *to_name; 655 int err; 656 657 if ((from_name = dentry_name(from)) == NULL) 658 return -ENOMEM; 659 to_name = dentry_name(to); 660 if (to_name == NULL) { 661 __putname(from_name); 662 return -ENOMEM; 663 } 664 err = link_file(to_name, from_name); 665 __putname(from_name); 666 __putname(to_name); 667 return err; 668 } 669 670 static int hostfs_unlink(struct inode *ino, struct dentry *dentry) 671 { 672 char *file; 673 int err; 674 675 if (append) 676 return -EPERM; 677 678 if ((file = dentry_name(dentry)) == NULL) 679 return -ENOMEM; 680 681 err = unlink_file(file); 682 __putname(file); 683 return err; 684 } 685 686 static int hostfs_symlink(struct mnt_idmap *idmap, struct inode *ino, 687 struct dentry *dentry, const char *to) 688 { 689 char *file; 690 int err; 691 692 if ((file = dentry_name(dentry)) == NULL) 693 return -ENOMEM; 694 err = make_symlink(file, to); 695 __putname(file); 696 return err; 697 } 698 699 static int hostfs_mkdir(struct mnt_idmap *idmap, struct inode *ino, 700 struct dentry *dentry, umode_t mode) 701 { 702 char *file; 703 int err; 704 705 if ((file = dentry_name(dentry)) == NULL) 706 return -ENOMEM; 707 err = do_mkdir(file, mode); 708 __putname(file); 709 return err; 710 } 711 712 static int hostfs_rmdir(struct inode *ino, struct dentry *dentry) 713 { 714 char *file; 715 int err; 716 717 if ((file = dentry_name(dentry)) == NULL) 718 return -ENOMEM; 719 err = hostfs_do_rmdir(file); 720 __putname(file); 721 return err; 722 } 723 724 static int hostfs_mknod(struct mnt_idmap *idmap, struct inode *dir, 725 struct dentry *dentry, umode_t mode, dev_t dev) 726 { 727 struct inode *inode; 728 char *name; 729 int err; 730 731 name = dentry_name(dentry); 732 if (name == NULL) 733 return -ENOMEM; 734 735 err = do_mknod(name, mode, MAJOR(dev), MINOR(dev)); 736 if (err) { 737 __putname(name); 738 return err; 739 } 740 741 inode = hostfs_iget(dir->i_sb, name); 742 __putname(name); 743 if (IS_ERR(inode)) 744 return PTR_ERR(inode); 745 746 d_instantiate(dentry, inode); 747 return 0; 748 } 749 750 static int hostfs_rename2(struct mnt_idmap *idmap, 751 struct inode *old_dir, struct dentry *old_dentry, 752 struct inode *new_dir, struct dentry *new_dentry, 753 unsigned int flags) 754 { 755 char *old_name, *new_name; 756 int err; 757 758 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE)) 759 return -EINVAL; 760 761 old_name = dentry_name(old_dentry); 762 if (old_name == NULL) 763 return -ENOMEM; 764 new_name = dentry_name(new_dentry); 765 if (new_name == NULL) { 766 __putname(old_name); 767 return -ENOMEM; 768 } 769 if (!flags) 770 err = rename_file(old_name, new_name); 771 else 772 err = rename2_file(old_name, new_name, flags); 773 774 __putname(old_name); 775 __putname(new_name); 776 return err; 777 } 778 779 static int hostfs_permission(struct mnt_idmap *idmap, 780 struct inode *ino, int desired) 781 { 782 char *name; 783 int r = 0, w = 0, x = 0, err; 784 785 if (desired & MAY_NOT_BLOCK) 786 return -ECHILD; 787 788 if (desired & MAY_READ) r = 1; 789 if (desired & MAY_WRITE) w = 1; 790 if (desired & MAY_EXEC) x = 1; 791 name = inode_name(ino); 792 if (name == NULL) 793 return -ENOMEM; 794 795 if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) || 796 S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode)) 797 err = 0; 798 else 799 err = access_file(name, r, w, x); 800 __putname(name); 801 if (!err) 802 err = generic_permission(&nop_mnt_idmap, ino, desired); 803 return err; 804 } 805 806 static int hostfs_setattr(struct mnt_idmap *idmap, 807 struct dentry *dentry, struct iattr *attr) 808 { 809 struct inode *inode = d_inode(dentry); 810 struct hostfs_iattr attrs; 811 char *name; 812 int err; 813 814 int fd = HOSTFS_I(inode)->fd; 815 816 err = setattr_prepare(&nop_mnt_idmap, dentry, attr); 817 if (err) 818 return err; 819 820 if (append) 821 attr->ia_valid &= ~ATTR_SIZE; 822 823 attrs.ia_valid = 0; 824 if (attr->ia_valid & ATTR_MODE) { 825 attrs.ia_valid |= HOSTFS_ATTR_MODE; 826 attrs.ia_mode = attr->ia_mode; 827 } 828 if (attr->ia_valid & ATTR_UID) { 829 attrs.ia_valid |= HOSTFS_ATTR_UID; 830 attrs.ia_uid = from_kuid(&init_user_ns, attr->ia_uid); 831 } 832 if (attr->ia_valid & ATTR_GID) { 833 attrs.ia_valid |= HOSTFS_ATTR_GID; 834 attrs.ia_gid = from_kgid(&init_user_ns, attr->ia_gid); 835 } 836 if (attr->ia_valid & ATTR_SIZE) { 837 attrs.ia_valid |= HOSTFS_ATTR_SIZE; 838 attrs.ia_size = attr->ia_size; 839 } 840 if (attr->ia_valid & ATTR_ATIME) { 841 attrs.ia_valid |= HOSTFS_ATTR_ATIME; 842 attrs.ia_atime = (struct hostfs_timespec) 843 { attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec }; 844 } 845 if (attr->ia_valid & ATTR_MTIME) { 846 attrs.ia_valid |= HOSTFS_ATTR_MTIME; 847 attrs.ia_mtime = (struct hostfs_timespec) 848 { attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec }; 849 } 850 if (attr->ia_valid & ATTR_CTIME) { 851 attrs.ia_valid |= HOSTFS_ATTR_CTIME; 852 attrs.ia_ctime = (struct hostfs_timespec) 853 { attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec }; 854 } 855 if (attr->ia_valid & ATTR_ATIME_SET) { 856 attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET; 857 } 858 if (attr->ia_valid & ATTR_MTIME_SET) { 859 attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET; 860 } 861 name = dentry_name(dentry); 862 if (name == NULL) 863 return -ENOMEM; 864 err = set_attr(name, &attrs, fd); 865 __putname(name); 866 if (err) 867 return err; 868 869 if ((attr->ia_valid & ATTR_SIZE) && 870 attr->ia_size != i_size_read(inode)) 871 truncate_setsize(inode, attr->ia_size); 872 873 setattr_copy(&nop_mnt_idmap, inode, attr); 874 mark_inode_dirty(inode); 875 return 0; 876 } 877 878 static const struct inode_operations hostfs_iops = { 879 .permission = hostfs_permission, 880 .setattr = hostfs_setattr, 881 }; 882 883 static const struct inode_operations hostfs_dir_iops = { 884 .create = hostfs_create, 885 .lookup = hostfs_lookup, 886 .link = hostfs_link, 887 .unlink = hostfs_unlink, 888 .symlink = hostfs_symlink, 889 .mkdir = hostfs_mkdir, 890 .rmdir = hostfs_rmdir, 891 .mknod = hostfs_mknod, 892 .rename = hostfs_rename2, 893 .permission = hostfs_permission, 894 .setattr = hostfs_setattr, 895 }; 896 897 static const char *hostfs_get_link(struct dentry *dentry, 898 struct inode *inode, 899 struct delayed_call *done) 900 { 901 char *link; 902 if (!dentry) 903 return ERR_PTR(-ECHILD); 904 link = kmalloc(PATH_MAX, GFP_KERNEL); 905 if (link) { 906 char *path = dentry_name(dentry); 907 int err = -ENOMEM; 908 if (path) { 909 err = hostfs_do_readlink(path, link, PATH_MAX); 910 if (err == PATH_MAX) 911 err = -E2BIG; 912 __putname(path); 913 } 914 if (err < 0) { 915 kfree(link); 916 return ERR_PTR(err); 917 } 918 } else { 919 return ERR_PTR(-ENOMEM); 920 } 921 922 set_delayed_call(done, kfree_link, link); 923 return link; 924 } 925 926 static const struct inode_operations hostfs_link_iops = { 927 .get_link = hostfs_get_link, 928 }; 929 930 static int hostfs_fill_super(struct super_block *sb, struct fs_context *fc) 931 { 932 struct hostfs_fs_info *fsi = sb->s_fs_info; 933 struct inode *root_inode; 934 int err; 935 936 sb->s_blocksize = 1024; 937 sb->s_blocksize_bits = 10; 938 sb->s_magic = HOSTFS_SUPER_MAGIC; 939 sb->s_op = &hostfs_sbops; 940 sb->s_d_op = &simple_dentry_operations; 941 sb->s_maxbytes = MAX_LFS_FILESIZE; 942 err = super_setup_bdi(sb); 943 if (err) 944 return err; 945 946 root_inode = hostfs_iget(sb, fsi->host_root_path); 947 if (IS_ERR(root_inode)) 948 return PTR_ERR(root_inode); 949 950 if (S_ISLNK(root_inode->i_mode)) { 951 char *name; 952 953 iput(root_inode); 954 name = follow_link(fsi->host_root_path); 955 if (IS_ERR(name)) 956 return PTR_ERR(name); 957 958 root_inode = hostfs_iget(sb, name); 959 kfree(name); 960 if (IS_ERR(root_inode)) 961 return PTR_ERR(root_inode); 962 } 963 964 sb->s_root = d_make_root(root_inode); 965 if (sb->s_root == NULL) 966 return -ENOMEM; 967 968 return 0; 969 } 970 971 enum hostfs_parma { 972 Opt_hostfs, 973 }; 974 975 static const struct fs_parameter_spec hostfs_param_specs[] = { 976 fsparam_string_empty("hostfs", Opt_hostfs), 977 {} 978 }; 979 980 static int hostfs_parse_param(struct fs_context *fc, struct fs_parameter *param) 981 { 982 struct hostfs_fs_info *fsi = fc->s_fs_info; 983 struct fs_parse_result result; 984 char *host_root; 985 int opt; 986 987 opt = fs_parse(fc, hostfs_param_specs, param, &result); 988 if (opt < 0) 989 return opt; 990 991 switch (opt) { 992 case Opt_hostfs: 993 host_root = param->string; 994 if (!*host_root) 995 host_root = ""; 996 fsi->host_root_path = 997 kasprintf(GFP_KERNEL, "%s/%s", root_ino, host_root); 998 if (fsi->host_root_path == NULL) 999 return -ENOMEM; 1000 break; 1001 } 1002 1003 return 0; 1004 } 1005 1006 static int hostfs_parse_monolithic(struct fs_context *fc, void *data) 1007 { 1008 struct hostfs_fs_info *fsi = fc->s_fs_info; 1009 char *host_root = (char *)data; 1010 1011 /* NULL is printed as '(null)' by printf(): avoid that. */ 1012 if (host_root == NULL) 1013 host_root = ""; 1014 1015 fsi->host_root_path = 1016 kasprintf(GFP_KERNEL, "%s/%s", root_ino, host_root); 1017 if (fsi->host_root_path == NULL) 1018 return -ENOMEM; 1019 1020 return 0; 1021 } 1022 1023 static int hostfs_fc_get_tree(struct fs_context *fc) 1024 { 1025 return get_tree_nodev(fc, hostfs_fill_super); 1026 } 1027 1028 static void hostfs_fc_free(struct fs_context *fc) 1029 { 1030 struct hostfs_fs_info *fsi = fc->s_fs_info; 1031 1032 if (!fsi) 1033 return; 1034 1035 kfree(fsi->host_root_path); 1036 kfree(fsi); 1037 } 1038 1039 static const struct fs_context_operations hostfs_context_ops = { 1040 .parse_monolithic = hostfs_parse_monolithic, 1041 .parse_param = hostfs_parse_param, 1042 .get_tree = hostfs_fc_get_tree, 1043 .free = hostfs_fc_free, 1044 }; 1045 1046 static int hostfs_init_fs_context(struct fs_context *fc) 1047 { 1048 struct hostfs_fs_info *fsi; 1049 1050 fsi = kzalloc(sizeof(*fsi), GFP_KERNEL); 1051 if (!fsi) 1052 return -ENOMEM; 1053 1054 fc->s_fs_info = fsi; 1055 fc->ops = &hostfs_context_ops; 1056 return 0; 1057 } 1058 1059 static void hostfs_kill_sb(struct super_block *s) 1060 { 1061 kill_anon_super(s); 1062 kfree(s->s_fs_info); 1063 } 1064 1065 static struct file_system_type hostfs_type = { 1066 .owner = THIS_MODULE, 1067 .name = "hostfs", 1068 .init_fs_context = hostfs_init_fs_context, 1069 .kill_sb = hostfs_kill_sb, 1070 .fs_flags = 0, 1071 }; 1072 MODULE_ALIAS_FS("hostfs"); 1073 1074 static int __init init_hostfs(void) 1075 { 1076 hostfs_inode_cache = KMEM_CACHE(hostfs_inode_info, 0); 1077 if (!hostfs_inode_cache) 1078 return -ENOMEM; 1079 return register_filesystem(&hostfs_type); 1080 } 1081 1082 static void __exit exit_hostfs(void) 1083 { 1084 unregister_filesystem(&hostfs_type); 1085 kmem_cache_destroy(hostfs_inode_cache); 1086 } 1087 1088 module_init(init_hostfs) 1089 module_exit(exit_hostfs) 1090 MODULE_DESCRIPTION("User-Mode Linux Host filesystem"); 1091 MODULE_LICENSE("GPL"); 1092