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