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