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