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 rdev; 536 537 /* Reencode maj and min with the kernel encoding.*/ 538 rdev = MKDEV(st->maj, st->min); 539 540 switch (st->mode & S_IFMT) { 541 case S_IFLNK: 542 ino->i_op = &hostfs_link_iops; 543 break; 544 case S_IFDIR: 545 ino->i_op = &hostfs_dir_iops; 546 ino->i_fop = &hostfs_dir_fops; 547 break; 548 case S_IFCHR: 549 case S_IFBLK: 550 case S_IFIFO: 551 case S_IFSOCK: 552 init_special_inode(ino, st->mode & S_IFMT, rdev); 553 ino->i_op = &hostfs_iops; 554 break; 555 case S_IFREG: 556 ino->i_op = &hostfs_iops; 557 ino->i_fop = &hostfs_file_fops; 558 ino->i_mapping->a_ops = &hostfs_aops; 559 break; 560 default: 561 return -EIO; 562 } 563 564 HOSTFS_I(ino)->dev = st->dev; 565 ino->i_ino = st->ino; 566 ino->i_mode = st->mode; 567 return hostfs_inode_update(ino, st); 568 } 569 570 static int hostfs_inode_test(struct inode *inode, void *data) 571 { 572 const struct hostfs_stat *st = data; 573 574 return inode->i_ino == st->ino && HOSTFS_I(inode)->dev == st->dev; 575 } 576 577 static struct inode *hostfs_iget(struct super_block *sb, char *name) 578 { 579 struct inode *inode; 580 struct hostfs_stat st; 581 int err = stat_file(name, &st, -1); 582 583 if (err) 584 return ERR_PTR(err); 585 586 inode = iget5_locked(sb, st.ino, hostfs_inode_test, hostfs_inode_set, 587 &st); 588 if (!inode) 589 return ERR_PTR(-ENOMEM); 590 591 if (inode->i_state & I_NEW) { 592 unlock_new_inode(inode); 593 } else { 594 spin_lock(&inode->i_lock); 595 hostfs_inode_update(inode, &st); 596 spin_unlock(&inode->i_lock); 597 } 598 599 return inode; 600 } 601 602 static int hostfs_create(struct mnt_idmap *idmap, struct inode *dir, 603 struct dentry *dentry, umode_t mode, bool excl) 604 { 605 struct inode *inode; 606 char *name; 607 int fd; 608 609 name = dentry_name(dentry); 610 if (name == NULL) 611 return -ENOMEM; 612 613 fd = file_create(name, mode & 0777); 614 if (fd < 0) { 615 __putname(name); 616 return fd; 617 } 618 619 inode = hostfs_iget(dir->i_sb, name); 620 __putname(name); 621 if (IS_ERR(inode)) 622 return PTR_ERR(inode); 623 624 HOSTFS_I(inode)->fd = fd; 625 HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE; 626 d_instantiate(dentry, inode); 627 return 0; 628 } 629 630 static struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry, 631 unsigned int flags) 632 { 633 struct inode *inode = NULL; 634 char *name; 635 636 name = dentry_name(dentry); 637 if (name == NULL) 638 return ERR_PTR(-ENOMEM); 639 640 inode = hostfs_iget(ino->i_sb, name); 641 __putname(name); 642 if (inode == ERR_PTR(-ENOENT)) 643 inode = NULL; 644 645 return d_splice_alias(inode, dentry); 646 } 647 648 static int hostfs_link(struct dentry *to, struct inode *ino, 649 struct dentry *from) 650 { 651 char *from_name, *to_name; 652 int err; 653 654 if ((from_name = dentry_name(from)) == NULL) 655 return -ENOMEM; 656 to_name = dentry_name(to); 657 if (to_name == NULL) { 658 __putname(from_name); 659 return -ENOMEM; 660 } 661 err = link_file(to_name, from_name); 662 __putname(from_name); 663 __putname(to_name); 664 return err; 665 } 666 667 static int hostfs_unlink(struct inode *ino, struct dentry *dentry) 668 { 669 char *file; 670 int err; 671 672 if (append) 673 return -EPERM; 674 675 if ((file = dentry_name(dentry)) == NULL) 676 return -ENOMEM; 677 678 err = unlink_file(file); 679 __putname(file); 680 return err; 681 } 682 683 static int hostfs_symlink(struct mnt_idmap *idmap, struct inode *ino, 684 struct dentry *dentry, const char *to) 685 { 686 char *file; 687 int err; 688 689 if ((file = dentry_name(dentry)) == NULL) 690 return -ENOMEM; 691 err = make_symlink(file, to); 692 __putname(file); 693 return err; 694 } 695 696 static int hostfs_mkdir(struct mnt_idmap *idmap, struct inode *ino, 697 struct dentry *dentry, umode_t mode) 698 { 699 char *file; 700 int err; 701 702 if ((file = dentry_name(dentry)) == NULL) 703 return -ENOMEM; 704 err = do_mkdir(file, mode); 705 __putname(file); 706 return err; 707 } 708 709 static int hostfs_rmdir(struct inode *ino, struct dentry *dentry) 710 { 711 char *file; 712 int err; 713 714 if ((file = dentry_name(dentry)) == NULL) 715 return -ENOMEM; 716 err = hostfs_do_rmdir(file); 717 __putname(file); 718 return err; 719 } 720 721 static int hostfs_mknod(struct mnt_idmap *idmap, struct inode *dir, 722 struct dentry *dentry, umode_t mode, dev_t dev) 723 { 724 struct inode *inode; 725 char *name; 726 int err; 727 728 name = dentry_name(dentry); 729 if (name == NULL) 730 return -ENOMEM; 731 732 err = do_mknod(name, mode, MAJOR(dev), MINOR(dev)); 733 if (err) { 734 __putname(name); 735 return err; 736 } 737 738 inode = hostfs_iget(dir->i_sb, name); 739 __putname(name); 740 if (IS_ERR(inode)) 741 return PTR_ERR(inode); 742 743 d_instantiate(dentry, inode); 744 return 0; 745 } 746 747 static int hostfs_rename2(struct mnt_idmap *idmap, 748 struct inode *old_dir, struct dentry *old_dentry, 749 struct inode *new_dir, struct dentry *new_dentry, 750 unsigned int flags) 751 { 752 char *old_name, *new_name; 753 int err; 754 755 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE)) 756 return -EINVAL; 757 758 old_name = dentry_name(old_dentry); 759 if (old_name == NULL) 760 return -ENOMEM; 761 new_name = dentry_name(new_dentry); 762 if (new_name == NULL) { 763 __putname(old_name); 764 return -ENOMEM; 765 } 766 if (!flags) 767 err = rename_file(old_name, new_name); 768 else 769 err = rename2_file(old_name, new_name, flags); 770 771 __putname(old_name); 772 __putname(new_name); 773 return err; 774 } 775 776 static int hostfs_permission(struct mnt_idmap *idmap, 777 struct inode *ino, int desired) 778 { 779 char *name; 780 int r = 0, w = 0, x = 0, err; 781 782 if (desired & MAY_NOT_BLOCK) 783 return -ECHILD; 784 785 if (desired & MAY_READ) r = 1; 786 if (desired & MAY_WRITE) w = 1; 787 if (desired & MAY_EXEC) x = 1; 788 name = inode_name(ino); 789 if (name == NULL) 790 return -ENOMEM; 791 792 if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) || 793 S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode)) 794 err = 0; 795 else 796 err = access_file(name, r, w, x); 797 __putname(name); 798 if (!err) 799 err = generic_permission(&nop_mnt_idmap, ino, desired); 800 return err; 801 } 802 803 static int hostfs_setattr(struct mnt_idmap *idmap, 804 struct dentry *dentry, struct iattr *attr) 805 { 806 struct inode *inode = d_inode(dentry); 807 struct hostfs_iattr attrs; 808 char *name; 809 int err; 810 811 int fd = HOSTFS_I(inode)->fd; 812 813 err = setattr_prepare(&nop_mnt_idmap, dentry, attr); 814 if (err) 815 return err; 816 817 if (append) 818 attr->ia_valid &= ~ATTR_SIZE; 819 820 attrs.ia_valid = 0; 821 if (attr->ia_valid & ATTR_MODE) { 822 attrs.ia_valid |= HOSTFS_ATTR_MODE; 823 attrs.ia_mode = attr->ia_mode; 824 } 825 if (attr->ia_valid & ATTR_UID) { 826 attrs.ia_valid |= HOSTFS_ATTR_UID; 827 attrs.ia_uid = from_kuid(&init_user_ns, attr->ia_uid); 828 } 829 if (attr->ia_valid & ATTR_GID) { 830 attrs.ia_valid |= HOSTFS_ATTR_GID; 831 attrs.ia_gid = from_kgid(&init_user_ns, attr->ia_gid); 832 } 833 if (attr->ia_valid & ATTR_SIZE) { 834 attrs.ia_valid |= HOSTFS_ATTR_SIZE; 835 attrs.ia_size = attr->ia_size; 836 } 837 if (attr->ia_valid & ATTR_ATIME) { 838 attrs.ia_valid |= HOSTFS_ATTR_ATIME; 839 attrs.ia_atime = (struct hostfs_timespec) 840 { attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec }; 841 } 842 if (attr->ia_valid & ATTR_MTIME) { 843 attrs.ia_valid |= HOSTFS_ATTR_MTIME; 844 attrs.ia_mtime = (struct hostfs_timespec) 845 { attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec }; 846 } 847 if (attr->ia_valid & ATTR_CTIME) { 848 attrs.ia_valid |= HOSTFS_ATTR_CTIME; 849 attrs.ia_ctime = (struct hostfs_timespec) 850 { attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec }; 851 } 852 if (attr->ia_valid & ATTR_ATIME_SET) { 853 attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET; 854 } 855 if (attr->ia_valid & ATTR_MTIME_SET) { 856 attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET; 857 } 858 name = dentry_name(dentry); 859 if (name == NULL) 860 return -ENOMEM; 861 err = set_attr(name, &attrs, fd); 862 __putname(name); 863 if (err) 864 return err; 865 866 if ((attr->ia_valid & ATTR_SIZE) && 867 attr->ia_size != i_size_read(inode)) 868 truncate_setsize(inode, attr->ia_size); 869 870 setattr_copy(&nop_mnt_idmap, inode, attr); 871 mark_inode_dirty(inode); 872 return 0; 873 } 874 875 static const struct inode_operations hostfs_iops = { 876 .permission = hostfs_permission, 877 .setattr = hostfs_setattr, 878 }; 879 880 static const struct inode_operations hostfs_dir_iops = { 881 .create = hostfs_create, 882 .lookup = hostfs_lookup, 883 .link = hostfs_link, 884 .unlink = hostfs_unlink, 885 .symlink = hostfs_symlink, 886 .mkdir = hostfs_mkdir, 887 .rmdir = hostfs_rmdir, 888 .mknod = hostfs_mknod, 889 .rename = hostfs_rename2, 890 .permission = hostfs_permission, 891 .setattr = hostfs_setattr, 892 }; 893 894 static const char *hostfs_get_link(struct dentry *dentry, 895 struct inode *inode, 896 struct delayed_call *done) 897 { 898 char *link; 899 if (!dentry) 900 return ERR_PTR(-ECHILD); 901 link = kmalloc(PATH_MAX, GFP_KERNEL); 902 if (link) { 903 char *path = dentry_name(dentry); 904 int err = -ENOMEM; 905 if (path) { 906 err = hostfs_do_readlink(path, link, PATH_MAX); 907 if (err == PATH_MAX) 908 err = -E2BIG; 909 __putname(path); 910 } 911 if (err < 0) { 912 kfree(link); 913 return ERR_PTR(err); 914 } 915 } else { 916 return ERR_PTR(-ENOMEM); 917 } 918 919 set_delayed_call(done, kfree_link, link); 920 return link; 921 } 922 923 static const struct inode_operations hostfs_link_iops = { 924 .get_link = hostfs_get_link, 925 }; 926 927 static int hostfs_fill_super(struct super_block *sb, struct fs_context *fc) 928 { 929 struct hostfs_fs_info *fsi = sb->s_fs_info; 930 const char *host_root = fc->source; 931 struct inode *root_inode; 932 int err; 933 934 sb->s_blocksize = 1024; 935 sb->s_blocksize_bits = 10; 936 sb->s_magic = HOSTFS_SUPER_MAGIC; 937 sb->s_op = &hostfs_sbops; 938 sb->s_d_op = &simple_dentry_operations; 939 sb->s_maxbytes = MAX_LFS_FILESIZE; 940 err = super_setup_bdi(sb); 941 if (err) 942 return err; 943 944 /* NULL is printed as '(null)' by printf(): avoid that. */ 945 if (fc->source == NULL) 946 host_root = ""; 947 948 fsi->host_root_path = 949 kasprintf(GFP_KERNEL, "%s/%s", root_ino, host_root); 950 if (fsi->host_root_path == NULL) 951 return -ENOMEM; 952 953 root_inode = hostfs_iget(sb, fsi->host_root_path); 954 if (IS_ERR(root_inode)) 955 return PTR_ERR(root_inode); 956 957 if (S_ISLNK(root_inode->i_mode)) { 958 char *name; 959 960 iput(root_inode); 961 name = follow_link(fsi->host_root_path); 962 if (IS_ERR(name)) 963 return PTR_ERR(name); 964 965 root_inode = hostfs_iget(sb, name); 966 kfree(name); 967 if (IS_ERR(root_inode)) 968 return PTR_ERR(root_inode); 969 } 970 971 sb->s_root = d_make_root(root_inode); 972 if (sb->s_root == NULL) 973 return -ENOMEM; 974 975 return 0; 976 } 977 978 static int hostfs_fc_get_tree(struct fs_context *fc) 979 { 980 return get_tree_nodev(fc, hostfs_fill_super); 981 } 982 983 static void hostfs_fc_free(struct fs_context *fc) 984 { 985 struct hostfs_fs_info *fsi = fc->s_fs_info; 986 987 if (!fsi) 988 return; 989 990 kfree(fsi->host_root_path); 991 kfree(fsi); 992 } 993 994 static const struct fs_context_operations hostfs_context_ops = { 995 .get_tree = hostfs_fc_get_tree, 996 .free = hostfs_fc_free, 997 }; 998 999 static int hostfs_init_fs_context(struct fs_context *fc) 1000 { 1001 struct hostfs_fs_info *fsi; 1002 1003 fsi = kzalloc(sizeof(*fsi), GFP_KERNEL); 1004 if (!fsi) 1005 return -ENOMEM; 1006 1007 fc->s_fs_info = fsi; 1008 fc->ops = &hostfs_context_ops; 1009 return 0; 1010 } 1011 1012 static void hostfs_kill_sb(struct super_block *s) 1013 { 1014 kill_anon_super(s); 1015 kfree(s->s_fs_info); 1016 } 1017 1018 static struct file_system_type hostfs_type = { 1019 .owner = THIS_MODULE, 1020 .name = "hostfs", 1021 .init_fs_context = hostfs_init_fs_context, 1022 .kill_sb = hostfs_kill_sb, 1023 .fs_flags = 0, 1024 }; 1025 MODULE_ALIAS_FS("hostfs"); 1026 1027 static int __init init_hostfs(void) 1028 { 1029 hostfs_inode_cache = KMEM_CACHE(hostfs_inode_info, 0); 1030 if (!hostfs_inode_cache) 1031 return -ENOMEM; 1032 return register_filesystem(&hostfs_type); 1033 } 1034 1035 static void __exit exit_hostfs(void) 1036 { 1037 unregister_filesystem(&hostfs_type); 1038 kmem_cache_destroy(hostfs_inode_cache); 1039 } 1040 1041 module_init(init_hostfs) 1042 module_exit(exit_hostfs) 1043 MODULE_LICENSE("GPL"); 1044