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 struct dentry *hostfs_mkdir(struct mnt_idmap *idmap, struct inode *ino, 683 struct dentry *dentry, umode_t mode) 684 { 685 struct inode *inode; 686 char *file; 687 int err; 688 689 if ((file = dentry_name(dentry)) == NULL) 690 return ERR_PTR(-ENOMEM); 691 err = do_mkdir(file, mode); 692 if (err) { 693 dentry = ERR_PTR(err); 694 } else { 695 inode = hostfs_iget(dentry->d_sb, file); 696 d_drop(dentry); 697 dentry = d_splice_alias(inode, dentry); 698 } 699 __putname(file); 700 return dentry; 701 } 702 703 static int hostfs_rmdir(struct inode *ino, struct dentry *dentry) 704 { 705 char *file; 706 int err; 707 708 if ((file = dentry_name(dentry)) == NULL) 709 return -ENOMEM; 710 err = hostfs_do_rmdir(file); 711 __putname(file); 712 return err; 713 } 714 715 static int hostfs_mknod(struct mnt_idmap *idmap, struct inode *dir, 716 struct dentry *dentry, umode_t mode, dev_t dev) 717 { 718 struct inode *inode; 719 char *name; 720 int err; 721 722 name = dentry_name(dentry); 723 if (name == NULL) 724 return -ENOMEM; 725 726 err = do_mknod(name, mode, MAJOR(dev), MINOR(dev)); 727 if (err) { 728 __putname(name); 729 return err; 730 } 731 732 inode = hostfs_iget(dir->i_sb, name); 733 __putname(name); 734 if (IS_ERR(inode)) 735 return PTR_ERR(inode); 736 737 d_instantiate(dentry, inode); 738 return 0; 739 } 740 741 static int hostfs_rename2(struct mnt_idmap *idmap, 742 struct inode *old_dir, struct dentry *old_dentry, 743 struct inode *new_dir, struct dentry *new_dentry, 744 unsigned int flags) 745 { 746 char *old_name, *new_name; 747 int err; 748 749 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE)) 750 return -EINVAL; 751 752 old_name = dentry_name(old_dentry); 753 if (old_name == NULL) 754 return -ENOMEM; 755 new_name = dentry_name(new_dentry); 756 if (new_name == NULL) { 757 __putname(old_name); 758 return -ENOMEM; 759 } 760 if (!flags) 761 err = rename_file(old_name, new_name); 762 else 763 err = rename2_file(old_name, new_name, flags); 764 765 __putname(old_name); 766 __putname(new_name); 767 return err; 768 } 769 770 static int hostfs_permission(struct mnt_idmap *idmap, 771 struct inode *ino, int desired) 772 { 773 char *name; 774 int r = 0, w = 0, x = 0, err; 775 776 if (desired & MAY_NOT_BLOCK) 777 return -ECHILD; 778 779 if (desired & MAY_READ) r = 1; 780 if (desired & MAY_WRITE) w = 1; 781 if (desired & MAY_EXEC) x = 1; 782 name = inode_name(ino); 783 if (name == NULL) 784 return -ENOMEM; 785 786 if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) || 787 S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode)) 788 err = 0; 789 else 790 err = access_file(name, r, w, x); 791 __putname(name); 792 if (!err) 793 err = generic_permission(&nop_mnt_idmap, ino, desired); 794 return err; 795 } 796 797 static int hostfs_setattr(struct mnt_idmap *idmap, 798 struct dentry *dentry, struct iattr *attr) 799 { 800 struct inode *inode = d_inode(dentry); 801 struct hostfs_iattr attrs; 802 char *name; 803 int err; 804 805 int fd = HOSTFS_I(inode)->fd; 806 807 err = setattr_prepare(&nop_mnt_idmap, dentry, attr); 808 if (err) 809 return err; 810 811 if (append) 812 attr->ia_valid &= ~ATTR_SIZE; 813 814 attrs.ia_valid = 0; 815 if (attr->ia_valid & ATTR_MODE) { 816 attrs.ia_valid |= HOSTFS_ATTR_MODE; 817 attrs.ia_mode = attr->ia_mode; 818 } 819 if (attr->ia_valid & ATTR_UID) { 820 attrs.ia_valid |= HOSTFS_ATTR_UID; 821 attrs.ia_uid = from_kuid(&init_user_ns, attr->ia_uid); 822 } 823 if (attr->ia_valid & ATTR_GID) { 824 attrs.ia_valid |= HOSTFS_ATTR_GID; 825 attrs.ia_gid = from_kgid(&init_user_ns, attr->ia_gid); 826 } 827 if (attr->ia_valid & ATTR_SIZE) { 828 attrs.ia_valid |= HOSTFS_ATTR_SIZE; 829 attrs.ia_size = attr->ia_size; 830 } 831 if (attr->ia_valid & ATTR_ATIME) { 832 attrs.ia_valid |= HOSTFS_ATTR_ATIME; 833 attrs.ia_atime = (struct hostfs_timespec) 834 { attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec }; 835 } 836 if (attr->ia_valid & ATTR_MTIME) { 837 attrs.ia_valid |= HOSTFS_ATTR_MTIME; 838 attrs.ia_mtime = (struct hostfs_timespec) 839 { attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec }; 840 } 841 if (attr->ia_valid & ATTR_CTIME) { 842 attrs.ia_valid |= HOSTFS_ATTR_CTIME; 843 attrs.ia_ctime = (struct hostfs_timespec) 844 { attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec }; 845 } 846 if (attr->ia_valid & ATTR_ATIME_SET) { 847 attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET; 848 } 849 if (attr->ia_valid & ATTR_MTIME_SET) { 850 attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET; 851 } 852 name = dentry_name(dentry); 853 if (name == NULL) 854 return -ENOMEM; 855 err = set_attr(name, &attrs, fd); 856 __putname(name); 857 if (err) 858 return err; 859 860 if ((attr->ia_valid & ATTR_SIZE) && 861 attr->ia_size != i_size_read(inode)) 862 truncate_setsize(inode, attr->ia_size); 863 864 setattr_copy(&nop_mnt_idmap, inode, attr); 865 mark_inode_dirty(inode); 866 return 0; 867 } 868 869 static const struct inode_operations hostfs_iops = { 870 .permission = hostfs_permission, 871 .setattr = hostfs_setattr, 872 }; 873 874 static const struct inode_operations hostfs_dir_iops = { 875 .create = hostfs_create, 876 .lookup = hostfs_lookup, 877 .link = hostfs_link, 878 .unlink = hostfs_unlink, 879 .symlink = hostfs_symlink, 880 .mkdir = hostfs_mkdir, 881 .rmdir = hostfs_rmdir, 882 .mknod = hostfs_mknod, 883 .rename = hostfs_rename2, 884 .permission = hostfs_permission, 885 .setattr = hostfs_setattr, 886 }; 887 888 static const char *hostfs_get_link(struct dentry *dentry, 889 struct inode *inode, 890 struct delayed_call *done) 891 { 892 char *link; 893 if (!dentry) 894 return ERR_PTR(-ECHILD); 895 link = kmalloc(PATH_MAX, GFP_KERNEL); 896 if (link) { 897 char *path = dentry_name(dentry); 898 int err = -ENOMEM; 899 if (path) { 900 err = hostfs_do_readlink(path, link, PATH_MAX); 901 if (err == PATH_MAX) 902 err = -E2BIG; 903 __putname(path); 904 } 905 if (err < 0) { 906 kfree(link); 907 return ERR_PTR(err); 908 } 909 } else { 910 return ERR_PTR(-ENOMEM); 911 } 912 913 set_delayed_call(done, kfree_link, link); 914 return link; 915 } 916 917 static const struct inode_operations hostfs_link_iops = { 918 .get_link = hostfs_get_link, 919 }; 920 921 static int hostfs_fill_super(struct super_block *sb, struct fs_context *fc) 922 { 923 struct hostfs_fs_info *fsi = sb->s_fs_info; 924 struct inode *root_inode; 925 int err; 926 927 sb->s_blocksize = 1024; 928 sb->s_blocksize_bits = 10; 929 sb->s_magic = HOSTFS_SUPER_MAGIC; 930 sb->s_op = &hostfs_sbops; 931 sb->s_d_op = &simple_dentry_operations; 932 sb->s_maxbytes = MAX_LFS_FILESIZE; 933 err = super_setup_bdi(sb); 934 if (err) 935 return err; 936 937 root_inode = hostfs_iget(sb, fsi->host_root_path); 938 if (IS_ERR(root_inode)) 939 return PTR_ERR(root_inode); 940 941 if (S_ISLNK(root_inode->i_mode)) { 942 char *name; 943 944 iput(root_inode); 945 name = follow_link(fsi->host_root_path); 946 if (IS_ERR(name)) 947 return PTR_ERR(name); 948 949 root_inode = hostfs_iget(sb, name); 950 kfree(name); 951 if (IS_ERR(root_inode)) 952 return PTR_ERR(root_inode); 953 } 954 955 sb->s_root = d_make_root(root_inode); 956 if (sb->s_root == NULL) 957 return -ENOMEM; 958 959 return 0; 960 } 961 962 enum hostfs_parma { 963 Opt_hostfs, 964 }; 965 966 static const struct fs_parameter_spec hostfs_param_specs[] = { 967 fsparam_string_empty("hostfs", Opt_hostfs), 968 {} 969 }; 970 971 static int hostfs_parse_param(struct fs_context *fc, struct fs_parameter *param) 972 { 973 struct hostfs_fs_info *fsi = fc->s_fs_info; 974 struct fs_parse_result result; 975 char *host_root; 976 int opt; 977 978 opt = fs_parse(fc, hostfs_param_specs, param, &result); 979 if (opt < 0) 980 return opt; 981 982 switch (opt) { 983 case Opt_hostfs: 984 host_root = param->string; 985 if (!*host_root) 986 host_root = ""; 987 fsi->host_root_path = 988 kasprintf(GFP_KERNEL, "%s/%s", root_ino, host_root); 989 if (fsi->host_root_path == NULL) 990 return -ENOMEM; 991 break; 992 } 993 994 return 0; 995 } 996 997 static int hostfs_parse_monolithic(struct fs_context *fc, void *data) 998 { 999 struct hostfs_fs_info *fsi = fc->s_fs_info; 1000 char *host_root = (char *)data; 1001 1002 /* NULL is printed as '(null)' by printf(): avoid that. */ 1003 if (host_root == NULL) 1004 host_root = ""; 1005 1006 fsi->host_root_path = 1007 kasprintf(GFP_KERNEL, "%s/%s", root_ino, host_root); 1008 if (fsi->host_root_path == NULL) 1009 return -ENOMEM; 1010 1011 return 0; 1012 } 1013 1014 static int hostfs_fc_get_tree(struct fs_context *fc) 1015 { 1016 return get_tree_nodev(fc, hostfs_fill_super); 1017 } 1018 1019 static void hostfs_fc_free(struct fs_context *fc) 1020 { 1021 struct hostfs_fs_info *fsi = fc->s_fs_info; 1022 1023 if (!fsi) 1024 return; 1025 1026 kfree(fsi->host_root_path); 1027 kfree(fsi); 1028 } 1029 1030 static const struct fs_context_operations hostfs_context_ops = { 1031 .parse_monolithic = hostfs_parse_monolithic, 1032 .parse_param = hostfs_parse_param, 1033 .get_tree = hostfs_fc_get_tree, 1034 .free = hostfs_fc_free, 1035 }; 1036 1037 static int hostfs_init_fs_context(struct fs_context *fc) 1038 { 1039 struct hostfs_fs_info *fsi; 1040 1041 fsi = kzalloc(sizeof(*fsi), GFP_KERNEL); 1042 if (!fsi) 1043 return -ENOMEM; 1044 1045 fc->s_fs_info = fsi; 1046 fc->ops = &hostfs_context_ops; 1047 return 0; 1048 } 1049 1050 static void hostfs_kill_sb(struct super_block *s) 1051 { 1052 kill_anon_super(s); 1053 kfree(s->s_fs_info); 1054 } 1055 1056 static struct file_system_type hostfs_type = { 1057 .owner = THIS_MODULE, 1058 .name = "hostfs", 1059 .init_fs_context = hostfs_init_fs_context, 1060 .kill_sb = hostfs_kill_sb, 1061 .fs_flags = 0, 1062 }; 1063 MODULE_ALIAS_FS("hostfs"); 1064 1065 static int __init init_hostfs(void) 1066 { 1067 hostfs_inode_cache = KMEM_CACHE(hostfs_inode_info, 0); 1068 if (!hostfs_inode_cache) 1069 return -ENOMEM; 1070 return register_filesystem(&hostfs_type); 1071 } 1072 1073 static void __exit exit_hostfs(void) 1074 { 1075 unregister_filesystem(&hostfs_type); 1076 kmem_cache_destroy(hostfs_inode_cache); 1077 } 1078 1079 module_init(init_hostfs) 1080 module_exit(exit_hostfs) 1081 MODULE_DESCRIPTION("User-Mode Linux Host filesystem"); 1082 MODULE_LICENSE("GPL"); 1083