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 folio **foliop, void **fsdata) 469 { 470 pgoff_t index = pos >> PAGE_SHIFT; 471 472 *foliop = __filemap_get_folio(mapping, index, FGP_WRITEBEGIN, 473 mapping_gfp_mask(mapping)); 474 if (!*foliop) 475 return -ENOMEM; 476 return 0; 477 } 478 479 static int hostfs_write_end(struct file *file, struct address_space *mapping, 480 loff_t pos, unsigned len, unsigned copied, 481 struct folio *folio, void *fsdata) 482 { 483 struct inode *inode = mapping->host; 484 void *buffer; 485 size_t from = offset_in_folio(folio, pos); 486 int err; 487 488 buffer = kmap_local_folio(folio, from); 489 err = write_file(FILE_HOSTFS_I(file)->fd, &pos, buffer, copied); 490 kunmap_local(buffer); 491 492 if (!folio_test_uptodate(folio) && err == folio_size(folio)) 493 folio_mark_uptodate(folio); 494 495 /* 496 * If err > 0, write_file has added err to pos, so we are comparing 497 * i_size against the last byte written. 498 */ 499 if (err > 0 && (pos > inode->i_size)) 500 inode->i_size = pos; 501 folio_unlock(folio); 502 folio_put(folio); 503 504 return err; 505 } 506 507 static const struct address_space_operations hostfs_aops = { 508 .writepage = hostfs_writepage, 509 .read_folio = hostfs_read_folio, 510 .dirty_folio = filemap_dirty_folio, 511 .write_begin = hostfs_write_begin, 512 .write_end = hostfs_write_end, 513 }; 514 515 static int hostfs_inode_update(struct inode *ino, const struct hostfs_stat *st) 516 { 517 set_nlink(ino, st->nlink); 518 i_uid_write(ino, st->uid); 519 i_gid_write(ino, st->gid); 520 inode_set_atime_to_ts(ino, (struct timespec64){ 521 st->atime.tv_sec, 522 st->atime.tv_nsec, 523 }); 524 inode_set_mtime_to_ts(ino, (struct timespec64){ 525 st->mtime.tv_sec, 526 st->mtime.tv_nsec, 527 }); 528 inode_set_ctime(ino, st->ctime.tv_sec, st->ctime.tv_nsec); 529 ino->i_size = st->size; 530 ino->i_blocks = st->blocks; 531 return 0; 532 } 533 534 static int hostfs_inode_set(struct inode *ino, void *data) 535 { 536 struct hostfs_stat *st = data; 537 dev_t dev, rdev; 538 539 /* Reencode maj and min with the kernel encoding.*/ 540 rdev = MKDEV(st->rdev.maj, st->rdev.min); 541 dev = MKDEV(st->dev.maj, st->dev.min); 542 543 switch (st->mode & S_IFMT) { 544 case S_IFLNK: 545 ino->i_op = &hostfs_link_iops; 546 break; 547 case S_IFDIR: 548 ino->i_op = &hostfs_dir_iops; 549 ino->i_fop = &hostfs_dir_fops; 550 break; 551 case S_IFCHR: 552 case S_IFBLK: 553 case S_IFIFO: 554 case S_IFSOCK: 555 init_special_inode(ino, st->mode & S_IFMT, rdev); 556 ino->i_op = &hostfs_iops; 557 break; 558 case S_IFREG: 559 ino->i_op = &hostfs_iops; 560 ino->i_fop = &hostfs_file_fops; 561 ino->i_mapping->a_ops = &hostfs_aops; 562 break; 563 default: 564 return -EIO; 565 } 566 567 HOSTFS_I(ino)->dev = dev; 568 ino->i_ino = st->ino; 569 ino->i_mode = st->mode; 570 return hostfs_inode_update(ino, st); 571 } 572 573 static int hostfs_inode_test(struct inode *inode, void *data) 574 { 575 const struct hostfs_stat *st = data; 576 dev_t dev = MKDEV(st->dev.maj, st->dev.min); 577 578 return inode->i_ino == st->ino && HOSTFS_I(inode)->dev == dev; 579 } 580 581 static struct inode *hostfs_iget(struct super_block *sb, char *name) 582 { 583 struct inode *inode; 584 struct hostfs_stat st; 585 int err = stat_file(name, &st, -1); 586 587 if (err) 588 return ERR_PTR(err); 589 590 inode = iget5_locked(sb, st.ino, hostfs_inode_test, hostfs_inode_set, 591 &st); 592 if (!inode) 593 return ERR_PTR(-ENOMEM); 594 595 if (inode->i_state & I_NEW) { 596 unlock_new_inode(inode); 597 } else { 598 spin_lock(&inode->i_lock); 599 hostfs_inode_update(inode, &st); 600 spin_unlock(&inode->i_lock); 601 } 602 603 return inode; 604 } 605 606 static int hostfs_create(struct mnt_idmap *idmap, struct inode *dir, 607 struct dentry *dentry, umode_t mode, bool excl) 608 { 609 struct inode *inode; 610 char *name; 611 int fd; 612 613 name = dentry_name(dentry); 614 if (name == NULL) 615 return -ENOMEM; 616 617 fd = file_create(name, mode & 0777); 618 if (fd < 0) { 619 __putname(name); 620 return fd; 621 } 622 623 inode = hostfs_iget(dir->i_sb, name); 624 __putname(name); 625 if (IS_ERR(inode)) 626 return PTR_ERR(inode); 627 628 HOSTFS_I(inode)->fd = fd; 629 HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE; 630 d_instantiate(dentry, inode); 631 return 0; 632 } 633 634 static struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry, 635 unsigned int flags) 636 { 637 struct inode *inode = NULL; 638 char *name; 639 640 name = dentry_name(dentry); 641 if (name == NULL) 642 return ERR_PTR(-ENOMEM); 643 644 inode = hostfs_iget(ino->i_sb, name); 645 __putname(name); 646 if (inode == ERR_PTR(-ENOENT)) 647 inode = NULL; 648 649 return d_splice_alias(inode, dentry); 650 } 651 652 static int hostfs_link(struct dentry *to, struct inode *ino, 653 struct dentry *from) 654 { 655 char *from_name, *to_name; 656 int err; 657 658 if ((from_name = dentry_name(from)) == NULL) 659 return -ENOMEM; 660 to_name = dentry_name(to); 661 if (to_name == NULL) { 662 __putname(from_name); 663 return -ENOMEM; 664 } 665 err = link_file(to_name, from_name); 666 __putname(from_name); 667 __putname(to_name); 668 return err; 669 } 670 671 static int hostfs_unlink(struct inode *ino, struct dentry *dentry) 672 { 673 char *file; 674 int err; 675 676 if (append) 677 return -EPERM; 678 679 if ((file = dentry_name(dentry)) == NULL) 680 return -ENOMEM; 681 682 err = unlink_file(file); 683 __putname(file); 684 return err; 685 } 686 687 static int hostfs_symlink(struct mnt_idmap *idmap, struct inode *ino, 688 struct dentry *dentry, const char *to) 689 { 690 char *file; 691 int err; 692 693 if ((file = dentry_name(dentry)) == NULL) 694 return -ENOMEM; 695 err = make_symlink(file, to); 696 __putname(file); 697 return err; 698 } 699 700 static int hostfs_mkdir(struct mnt_idmap *idmap, struct inode *ino, 701 struct dentry *dentry, umode_t mode) 702 { 703 char *file; 704 int err; 705 706 if ((file = dentry_name(dentry)) == NULL) 707 return -ENOMEM; 708 err = do_mkdir(file, mode); 709 __putname(file); 710 return err; 711 } 712 713 static int hostfs_rmdir(struct inode *ino, struct dentry *dentry) 714 { 715 char *file; 716 int err; 717 718 if ((file = dentry_name(dentry)) == NULL) 719 return -ENOMEM; 720 err = hostfs_do_rmdir(file); 721 __putname(file); 722 return err; 723 } 724 725 static int hostfs_mknod(struct mnt_idmap *idmap, struct inode *dir, 726 struct dentry *dentry, umode_t mode, dev_t dev) 727 { 728 struct inode *inode; 729 char *name; 730 int err; 731 732 name = dentry_name(dentry); 733 if (name == NULL) 734 return -ENOMEM; 735 736 err = do_mknod(name, mode, MAJOR(dev), MINOR(dev)); 737 if (err) { 738 __putname(name); 739 return err; 740 } 741 742 inode = hostfs_iget(dir->i_sb, name); 743 __putname(name); 744 if (IS_ERR(inode)) 745 return PTR_ERR(inode); 746 747 d_instantiate(dentry, inode); 748 return 0; 749 } 750 751 static int hostfs_rename2(struct mnt_idmap *idmap, 752 struct inode *old_dir, struct dentry *old_dentry, 753 struct inode *new_dir, struct dentry *new_dentry, 754 unsigned int flags) 755 { 756 char *old_name, *new_name; 757 int err; 758 759 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE)) 760 return -EINVAL; 761 762 old_name = dentry_name(old_dentry); 763 if (old_name == NULL) 764 return -ENOMEM; 765 new_name = dentry_name(new_dentry); 766 if (new_name == NULL) { 767 __putname(old_name); 768 return -ENOMEM; 769 } 770 if (!flags) 771 err = rename_file(old_name, new_name); 772 else 773 err = rename2_file(old_name, new_name, flags); 774 775 __putname(old_name); 776 __putname(new_name); 777 return err; 778 } 779 780 static int hostfs_permission(struct mnt_idmap *idmap, 781 struct inode *ino, int desired) 782 { 783 char *name; 784 int r = 0, w = 0, x = 0, err; 785 786 if (desired & MAY_NOT_BLOCK) 787 return -ECHILD; 788 789 if (desired & MAY_READ) r = 1; 790 if (desired & MAY_WRITE) w = 1; 791 if (desired & MAY_EXEC) x = 1; 792 name = inode_name(ino); 793 if (name == NULL) 794 return -ENOMEM; 795 796 if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) || 797 S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode)) 798 err = 0; 799 else 800 err = access_file(name, r, w, x); 801 __putname(name); 802 if (!err) 803 err = generic_permission(&nop_mnt_idmap, ino, desired); 804 return err; 805 } 806 807 static int hostfs_setattr(struct mnt_idmap *idmap, 808 struct dentry *dentry, struct iattr *attr) 809 { 810 struct inode *inode = d_inode(dentry); 811 struct hostfs_iattr attrs; 812 char *name; 813 int err; 814 815 int fd = HOSTFS_I(inode)->fd; 816 817 err = setattr_prepare(&nop_mnt_idmap, dentry, attr); 818 if (err) 819 return err; 820 821 if (append) 822 attr->ia_valid &= ~ATTR_SIZE; 823 824 attrs.ia_valid = 0; 825 if (attr->ia_valid & ATTR_MODE) { 826 attrs.ia_valid |= HOSTFS_ATTR_MODE; 827 attrs.ia_mode = attr->ia_mode; 828 } 829 if (attr->ia_valid & ATTR_UID) { 830 attrs.ia_valid |= HOSTFS_ATTR_UID; 831 attrs.ia_uid = from_kuid(&init_user_ns, attr->ia_uid); 832 } 833 if (attr->ia_valid & ATTR_GID) { 834 attrs.ia_valid |= HOSTFS_ATTR_GID; 835 attrs.ia_gid = from_kgid(&init_user_ns, attr->ia_gid); 836 } 837 if (attr->ia_valid & ATTR_SIZE) { 838 attrs.ia_valid |= HOSTFS_ATTR_SIZE; 839 attrs.ia_size = attr->ia_size; 840 } 841 if (attr->ia_valid & ATTR_ATIME) { 842 attrs.ia_valid |= HOSTFS_ATTR_ATIME; 843 attrs.ia_atime = (struct hostfs_timespec) 844 { attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec }; 845 } 846 if (attr->ia_valid & ATTR_MTIME) { 847 attrs.ia_valid |= HOSTFS_ATTR_MTIME; 848 attrs.ia_mtime = (struct hostfs_timespec) 849 { attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec }; 850 } 851 if (attr->ia_valid & ATTR_CTIME) { 852 attrs.ia_valid |= HOSTFS_ATTR_CTIME; 853 attrs.ia_ctime = (struct hostfs_timespec) 854 { attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec }; 855 } 856 if (attr->ia_valid & ATTR_ATIME_SET) { 857 attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET; 858 } 859 if (attr->ia_valid & ATTR_MTIME_SET) { 860 attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET; 861 } 862 name = dentry_name(dentry); 863 if (name == NULL) 864 return -ENOMEM; 865 err = set_attr(name, &attrs, fd); 866 __putname(name); 867 if (err) 868 return err; 869 870 if ((attr->ia_valid & ATTR_SIZE) && 871 attr->ia_size != i_size_read(inode)) 872 truncate_setsize(inode, attr->ia_size); 873 874 setattr_copy(&nop_mnt_idmap, inode, attr); 875 mark_inode_dirty(inode); 876 return 0; 877 } 878 879 static const struct inode_operations hostfs_iops = { 880 .permission = hostfs_permission, 881 .setattr = hostfs_setattr, 882 }; 883 884 static const struct inode_operations hostfs_dir_iops = { 885 .create = hostfs_create, 886 .lookup = hostfs_lookup, 887 .link = hostfs_link, 888 .unlink = hostfs_unlink, 889 .symlink = hostfs_symlink, 890 .mkdir = hostfs_mkdir, 891 .rmdir = hostfs_rmdir, 892 .mknod = hostfs_mknod, 893 .rename = hostfs_rename2, 894 .permission = hostfs_permission, 895 .setattr = hostfs_setattr, 896 }; 897 898 static const char *hostfs_get_link(struct dentry *dentry, 899 struct inode *inode, 900 struct delayed_call *done) 901 { 902 char *link; 903 if (!dentry) 904 return ERR_PTR(-ECHILD); 905 link = kmalloc(PATH_MAX, GFP_KERNEL); 906 if (link) { 907 char *path = dentry_name(dentry); 908 int err = -ENOMEM; 909 if (path) { 910 err = hostfs_do_readlink(path, link, PATH_MAX); 911 if (err == PATH_MAX) 912 err = -E2BIG; 913 __putname(path); 914 } 915 if (err < 0) { 916 kfree(link); 917 return ERR_PTR(err); 918 } 919 } else { 920 return ERR_PTR(-ENOMEM); 921 } 922 923 set_delayed_call(done, kfree_link, link); 924 return link; 925 } 926 927 static const struct inode_operations hostfs_link_iops = { 928 .get_link = hostfs_get_link, 929 }; 930 931 static int hostfs_fill_super(struct super_block *sb, struct fs_context *fc) 932 { 933 struct hostfs_fs_info *fsi = sb->s_fs_info; 934 struct inode *root_inode; 935 int err; 936 937 sb->s_blocksize = 1024; 938 sb->s_blocksize_bits = 10; 939 sb->s_magic = HOSTFS_SUPER_MAGIC; 940 sb->s_op = &hostfs_sbops; 941 sb->s_d_op = &simple_dentry_operations; 942 sb->s_maxbytes = MAX_LFS_FILESIZE; 943 err = super_setup_bdi(sb); 944 if (err) 945 return err; 946 947 root_inode = hostfs_iget(sb, fsi->host_root_path); 948 if (IS_ERR(root_inode)) 949 return PTR_ERR(root_inode); 950 951 if (S_ISLNK(root_inode->i_mode)) { 952 char *name; 953 954 iput(root_inode); 955 name = follow_link(fsi->host_root_path); 956 if (IS_ERR(name)) 957 return PTR_ERR(name); 958 959 root_inode = hostfs_iget(sb, name); 960 kfree(name); 961 if (IS_ERR(root_inode)) 962 return PTR_ERR(root_inode); 963 } 964 965 sb->s_root = d_make_root(root_inode); 966 if (sb->s_root == NULL) 967 return -ENOMEM; 968 969 return 0; 970 } 971 972 enum hostfs_parma { 973 Opt_hostfs, 974 }; 975 976 static const struct fs_parameter_spec hostfs_param_specs[] = { 977 fsparam_string_empty("hostfs", Opt_hostfs), 978 {} 979 }; 980 981 static int hostfs_parse_param(struct fs_context *fc, struct fs_parameter *param) 982 { 983 struct hostfs_fs_info *fsi = fc->s_fs_info; 984 struct fs_parse_result result; 985 char *host_root; 986 int opt; 987 988 opt = fs_parse(fc, hostfs_param_specs, param, &result); 989 if (opt < 0) 990 return opt; 991 992 switch (opt) { 993 case Opt_hostfs: 994 host_root = param->string; 995 if (!*host_root) 996 host_root = ""; 997 fsi->host_root_path = 998 kasprintf(GFP_KERNEL, "%s/%s", root_ino, host_root); 999 if (fsi->host_root_path == NULL) 1000 return -ENOMEM; 1001 break; 1002 } 1003 1004 return 0; 1005 } 1006 1007 static int hostfs_parse_monolithic(struct fs_context *fc, void *data) 1008 { 1009 struct hostfs_fs_info *fsi = fc->s_fs_info; 1010 char *host_root = (char *)data; 1011 1012 /* NULL is printed as '(null)' by printf(): avoid that. */ 1013 if (host_root == NULL) 1014 host_root = ""; 1015 1016 fsi->host_root_path = 1017 kasprintf(GFP_KERNEL, "%s/%s", root_ino, host_root); 1018 if (fsi->host_root_path == NULL) 1019 return -ENOMEM; 1020 1021 return 0; 1022 } 1023 1024 static int hostfs_fc_get_tree(struct fs_context *fc) 1025 { 1026 return get_tree_nodev(fc, hostfs_fill_super); 1027 } 1028 1029 static void hostfs_fc_free(struct fs_context *fc) 1030 { 1031 struct hostfs_fs_info *fsi = fc->s_fs_info; 1032 1033 if (!fsi) 1034 return; 1035 1036 kfree(fsi->host_root_path); 1037 kfree(fsi); 1038 } 1039 1040 static const struct fs_context_operations hostfs_context_ops = { 1041 .parse_monolithic = hostfs_parse_monolithic, 1042 .parse_param = hostfs_parse_param, 1043 .get_tree = hostfs_fc_get_tree, 1044 .free = hostfs_fc_free, 1045 }; 1046 1047 static int hostfs_init_fs_context(struct fs_context *fc) 1048 { 1049 struct hostfs_fs_info *fsi; 1050 1051 fsi = kzalloc(sizeof(*fsi), GFP_KERNEL); 1052 if (!fsi) 1053 return -ENOMEM; 1054 1055 fc->s_fs_info = fsi; 1056 fc->ops = &hostfs_context_ops; 1057 return 0; 1058 } 1059 1060 static void hostfs_kill_sb(struct super_block *s) 1061 { 1062 kill_anon_super(s); 1063 kfree(s->s_fs_info); 1064 } 1065 1066 static struct file_system_type hostfs_type = { 1067 .owner = THIS_MODULE, 1068 .name = "hostfs", 1069 .init_fs_context = hostfs_init_fs_context, 1070 .kill_sb = hostfs_kill_sb, 1071 .fs_flags = 0, 1072 }; 1073 MODULE_ALIAS_FS("hostfs"); 1074 1075 static int __init init_hostfs(void) 1076 { 1077 hostfs_inode_cache = KMEM_CACHE(hostfs_inode_info, 0); 1078 if (!hostfs_inode_cache) 1079 return -ENOMEM; 1080 return register_filesystem(&hostfs_type); 1081 } 1082 1083 static void __exit exit_hostfs(void) 1084 { 1085 unregister_filesystem(&hostfs_type); 1086 kmem_cache_destroy(hostfs_inode_cache); 1087 } 1088 1089 module_init(init_hostfs) 1090 module_exit(exit_hostfs) 1091 MODULE_DESCRIPTION("User-Mode Linux Host filesystem"); 1092 MODULE_LICENSE("GPL"); 1093