1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2017-2018 HUAWEI, Inc. 4 * https://www.huawei.com/ 5 * Copyright (C) 2021, Alibaba Cloud 6 */ 7 #include <linux/statfs.h> 8 #include <linux/seq_file.h> 9 #include <linux/crc32c.h> 10 #include <linux/fs_context.h> 11 #include <linux/fs_parser.h> 12 #include <linux/exportfs.h> 13 #include <linux/backing-dev.h> 14 #include "xattr.h" 15 16 #define CREATE_TRACE_POINTS 17 #include <trace/events/erofs.h> 18 19 static struct kmem_cache *erofs_inode_cachep __read_mostly; 20 21 void _erofs_printk(struct super_block *sb, const char *fmt, ...) 22 { 23 struct va_format vaf; 24 va_list args; 25 int level; 26 27 va_start(args, fmt); 28 29 level = printk_get_level(fmt); 30 vaf.fmt = printk_skip_level(fmt); 31 vaf.va = &args; 32 if (sb) 33 printk("%c%cerofs (device %s): %pV", 34 KERN_SOH_ASCII, level, sb->s_id, &vaf); 35 else 36 printk("%c%cerofs: %pV", KERN_SOH_ASCII, level, &vaf); 37 va_end(args); 38 } 39 40 static int erofs_superblock_csum_verify(struct super_block *sb, void *sbdata) 41 { 42 size_t len = 1 << EROFS_SB(sb)->blkszbits; 43 struct erofs_super_block *dsb; 44 u32 expected_crc, crc; 45 46 if (len > EROFS_SUPER_OFFSET) 47 len -= EROFS_SUPER_OFFSET; 48 49 dsb = kmemdup(sbdata + EROFS_SUPER_OFFSET, len, GFP_KERNEL); 50 if (!dsb) 51 return -ENOMEM; 52 53 expected_crc = le32_to_cpu(dsb->checksum); 54 dsb->checksum = 0; 55 /* to allow for x86 boot sectors and other oddities. */ 56 crc = crc32c(~0, dsb, len); 57 kfree(dsb); 58 59 if (crc != expected_crc) { 60 erofs_err(sb, "invalid checksum 0x%08x, 0x%08x expected", 61 crc, expected_crc); 62 return -EBADMSG; 63 } 64 return 0; 65 } 66 67 static void erofs_inode_init_once(void *ptr) 68 { 69 struct erofs_inode *vi = ptr; 70 71 inode_init_once(&vi->vfs_inode); 72 } 73 74 static struct inode *erofs_alloc_inode(struct super_block *sb) 75 { 76 struct erofs_inode *vi = 77 alloc_inode_sb(sb, erofs_inode_cachep, GFP_KERNEL); 78 79 if (!vi) 80 return NULL; 81 82 /* zero out everything except vfs_inode */ 83 memset(vi, 0, offsetof(struct erofs_inode, vfs_inode)); 84 return &vi->vfs_inode; 85 } 86 87 static void erofs_free_inode(struct inode *inode) 88 { 89 struct erofs_inode *vi = EROFS_I(inode); 90 91 if (inode->i_op == &erofs_fast_symlink_iops) 92 kfree(inode->i_link); 93 kfree(vi->xattr_shared_xattrs); 94 kmem_cache_free(erofs_inode_cachep, vi); 95 } 96 97 /* read variable-sized metadata, offset will be aligned by 4-byte */ 98 void *erofs_read_metadata(struct super_block *sb, struct erofs_buf *buf, 99 erofs_off_t *offset, int *lengthp) 100 { 101 u8 *buffer, *ptr; 102 int len, i, cnt; 103 104 *offset = round_up(*offset, 4); 105 ptr = erofs_bread(buf, *offset, EROFS_KMAP); 106 if (IS_ERR(ptr)) 107 return ptr; 108 109 len = le16_to_cpu(*(__le16 *)ptr); 110 if (!len) 111 len = U16_MAX + 1; 112 buffer = kmalloc(len, GFP_KERNEL); 113 if (!buffer) 114 return ERR_PTR(-ENOMEM); 115 *offset += sizeof(__le16); 116 *lengthp = len; 117 118 for (i = 0; i < len; i += cnt) { 119 cnt = min_t(int, sb->s_blocksize - erofs_blkoff(sb, *offset), 120 len - i); 121 ptr = erofs_bread(buf, *offset, EROFS_KMAP); 122 if (IS_ERR(ptr)) { 123 kfree(buffer); 124 return ptr; 125 } 126 memcpy(buffer + i, ptr, cnt); 127 *offset += cnt; 128 } 129 return buffer; 130 } 131 132 #ifndef CONFIG_EROFS_FS_ZIP 133 static int z_erofs_parse_cfgs(struct super_block *sb, 134 struct erofs_super_block *dsb) 135 { 136 if (!dsb->u1.available_compr_algs) 137 return 0; 138 139 erofs_err(sb, "compression disabled, unable to mount compressed EROFS"); 140 return -EOPNOTSUPP; 141 } 142 #endif 143 144 static int erofs_init_device(struct erofs_buf *buf, struct super_block *sb, 145 struct erofs_device_info *dif, erofs_off_t *pos) 146 { 147 struct erofs_sb_info *sbi = EROFS_SB(sb); 148 struct erofs_fscache *fscache; 149 struct erofs_deviceslot *dis; 150 struct file *file; 151 152 dis = erofs_read_metabuf(buf, sb, *pos, EROFS_KMAP); 153 if (IS_ERR(dis)) 154 return PTR_ERR(dis); 155 156 if (!sbi->devs->flatdev && !dif->path) { 157 if (!dis->tag[0]) { 158 erofs_err(sb, "empty device tag @ pos %llu", *pos); 159 return -EINVAL; 160 } 161 dif->path = kmemdup_nul(dis->tag, sizeof(dis->tag), GFP_KERNEL); 162 if (!dif->path) 163 return -ENOMEM; 164 } 165 166 if (erofs_is_fscache_mode(sb)) { 167 fscache = erofs_fscache_register_cookie(sb, dif->path, 0); 168 if (IS_ERR(fscache)) 169 return PTR_ERR(fscache); 170 dif->fscache = fscache; 171 } else if (!sbi->devs->flatdev) { 172 file = erofs_is_fileio_mode(sbi) ? 173 filp_open(dif->path, O_RDONLY | O_LARGEFILE, 0) : 174 bdev_file_open_by_path(dif->path, 175 BLK_OPEN_READ, sb->s_type, NULL); 176 if (IS_ERR(file)) 177 return PTR_ERR(file); 178 179 if (!erofs_is_fileio_mode(sbi)) { 180 dif->dax_dev = fs_dax_get_by_bdev(file_bdev(file), 181 &dif->dax_part_off, NULL, NULL); 182 } else if (!S_ISREG(file_inode(file)->i_mode)) { 183 fput(file); 184 return -EINVAL; 185 } 186 dif->file = file; 187 } 188 189 dif->blocks = le32_to_cpu(dis->blocks); 190 dif->mapped_blkaddr = le32_to_cpu(dis->mapped_blkaddr); 191 sbi->total_blocks += dif->blocks; 192 *pos += EROFS_DEVT_SLOT_SIZE; 193 return 0; 194 } 195 196 static int erofs_scan_devices(struct super_block *sb, 197 struct erofs_super_block *dsb) 198 { 199 struct erofs_sb_info *sbi = EROFS_SB(sb); 200 unsigned int ondisk_extradevs; 201 erofs_off_t pos; 202 struct erofs_buf buf = __EROFS_BUF_INITIALIZER; 203 struct erofs_device_info *dif; 204 int id, err = 0; 205 206 sbi->total_blocks = sbi->primarydevice_blocks; 207 if (!erofs_sb_has_device_table(sbi)) 208 ondisk_extradevs = 0; 209 else 210 ondisk_extradevs = le16_to_cpu(dsb->extra_devices); 211 212 if (sbi->devs->extra_devices && 213 ondisk_extradevs != sbi->devs->extra_devices) { 214 erofs_err(sb, "extra devices don't match (ondisk %u, given %u)", 215 ondisk_extradevs, sbi->devs->extra_devices); 216 return -EINVAL; 217 } 218 if (!ondisk_extradevs) 219 return 0; 220 221 if (!sbi->devs->extra_devices && !erofs_is_fscache_mode(sb)) 222 sbi->devs->flatdev = true; 223 224 sbi->device_id_mask = roundup_pow_of_two(ondisk_extradevs + 1) - 1; 225 pos = le16_to_cpu(dsb->devt_slotoff) * EROFS_DEVT_SLOT_SIZE; 226 down_read(&sbi->devs->rwsem); 227 if (sbi->devs->extra_devices) { 228 idr_for_each_entry(&sbi->devs->tree, dif, id) { 229 err = erofs_init_device(&buf, sb, dif, &pos); 230 if (err) 231 break; 232 } 233 } else { 234 for (id = 0; id < ondisk_extradevs; id++) { 235 dif = kzalloc(sizeof(*dif), GFP_KERNEL); 236 if (!dif) { 237 err = -ENOMEM; 238 break; 239 } 240 241 err = idr_alloc(&sbi->devs->tree, dif, 0, 0, GFP_KERNEL); 242 if (err < 0) { 243 kfree(dif); 244 break; 245 } 246 ++sbi->devs->extra_devices; 247 248 err = erofs_init_device(&buf, sb, dif, &pos); 249 if (err) 250 break; 251 } 252 } 253 up_read(&sbi->devs->rwsem); 254 erofs_put_metabuf(&buf); 255 return err; 256 } 257 258 static int erofs_read_superblock(struct super_block *sb) 259 { 260 struct erofs_sb_info *sbi = EROFS_SB(sb); 261 struct erofs_buf buf = __EROFS_BUF_INITIALIZER; 262 struct erofs_super_block *dsb; 263 void *data; 264 int ret; 265 266 data = erofs_read_metabuf(&buf, sb, 0, EROFS_KMAP); 267 if (IS_ERR(data)) { 268 erofs_err(sb, "cannot read erofs superblock"); 269 return PTR_ERR(data); 270 } 271 272 dsb = (struct erofs_super_block *)(data + EROFS_SUPER_OFFSET); 273 ret = -EINVAL; 274 if (le32_to_cpu(dsb->magic) != EROFS_SUPER_MAGIC_V1) { 275 erofs_err(sb, "cannot find valid erofs superblock"); 276 goto out; 277 } 278 279 sbi->blkszbits = dsb->blkszbits; 280 if (sbi->blkszbits < 9 || sbi->blkszbits > PAGE_SHIFT) { 281 erofs_err(sb, "blkszbits %u isn't supported", sbi->blkszbits); 282 goto out; 283 } 284 if (dsb->dirblkbits) { 285 erofs_err(sb, "dirblkbits %u isn't supported", dsb->dirblkbits); 286 goto out; 287 } 288 289 sbi->feature_compat = le32_to_cpu(dsb->feature_compat); 290 if (erofs_sb_has_sb_chksum(sbi)) { 291 ret = erofs_superblock_csum_verify(sb, data); 292 if (ret) 293 goto out; 294 } 295 296 ret = -EINVAL; 297 sbi->feature_incompat = le32_to_cpu(dsb->feature_incompat); 298 if (sbi->feature_incompat & ~EROFS_ALL_FEATURE_INCOMPAT) { 299 erofs_err(sb, "unidentified incompatible feature %x, please upgrade kernel", 300 sbi->feature_incompat & ~EROFS_ALL_FEATURE_INCOMPAT); 301 goto out; 302 } 303 304 sbi->sb_size = 128 + dsb->sb_extslots * EROFS_SB_EXTSLOT_SIZE; 305 if (sbi->sb_size > PAGE_SIZE - EROFS_SUPER_OFFSET) { 306 erofs_err(sb, "invalid sb_extslots %u (more than a fs block)", 307 sbi->sb_size); 308 goto out; 309 } 310 sbi->primarydevice_blocks = le32_to_cpu(dsb->blocks); 311 sbi->meta_blkaddr = le32_to_cpu(dsb->meta_blkaddr); 312 #ifdef CONFIG_EROFS_FS_XATTR 313 sbi->xattr_blkaddr = le32_to_cpu(dsb->xattr_blkaddr); 314 sbi->xattr_prefix_start = le32_to_cpu(dsb->xattr_prefix_start); 315 sbi->xattr_prefix_count = dsb->xattr_prefix_count; 316 sbi->xattr_filter_reserved = dsb->xattr_filter_reserved; 317 #endif 318 sbi->islotbits = ilog2(sizeof(struct erofs_inode_compact)); 319 sbi->root_nid = le16_to_cpu(dsb->root_nid); 320 sbi->packed_nid = le64_to_cpu(dsb->packed_nid); 321 sbi->inos = le64_to_cpu(dsb->inos); 322 323 sbi->build_time = le64_to_cpu(dsb->build_time); 324 sbi->build_time_nsec = le32_to_cpu(dsb->build_time_nsec); 325 326 super_set_uuid(sb, (void *)dsb->uuid, sizeof(dsb->uuid)); 327 328 ret = strscpy(sbi->volume_name, dsb->volume_name, 329 sizeof(dsb->volume_name)); 330 if (ret < 0) { /* -E2BIG */ 331 erofs_err(sb, "bad volume name without NIL terminator"); 332 ret = -EFSCORRUPTED; 333 goto out; 334 } 335 336 /* parse on-disk compression configurations */ 337 ret = z_erofs_parse_cfgs(sb, dsb); 338 if (ret < 0) 339 goto out; 340 341 /* handle multiple devices */ 342 ret = erofs_scan_devices(sb, dsb); 343 344 if (erofs_is_fscache_mode(sb)) 345 erofs_info(sb, "[deprecated] fscache-based on-demand read feature in use. Use at your own risk!"); 346 out: 347 erofs_put_metabuf(&buf); 348 return ret; 349 } 350 351 static void erofs_default_options(struct erofs_sb_info *sbi) 352 { 353 #ifdef CONFIG_EROFS_FS_ZIP 354 sbi->opt.cache_strategy = EROFS_ZIP_CACHE_READAROUND; 355 sbi->opt.max_sync_decompress_pages = 3; 356 sbi->opt.sync_decompress = EROFS_SYNC_DECOMPRESS_AUTO; 357 #endif 358 #ifdef CONFIG_EROFS_FS_XATTR 359 set_opt(&sbi->opt, XATTR_USER); 360 #endif 361 #ifdef CONFIG_EROFS_FS_POSIX_ACL 362 set_opt(&sbi->opt, POSIX_ACL); 363 #endif 364 } 365 366 enum { 367 Opt_user_xattr, 368 Opt_acl, 369 Opt_cache_strategy, 370 Opt_dax, 371 Opt_dax_enum, 372 Opt_device, 373 Opt_fsid, 374 Opt_domain_id, 375 Opt_err 376 }; 377 378 static const struct constant_table erofs_param_cache_strategy[] = { 379 {"disabled", EROFS_ZIP_CACHE_DISABLED}, 380 {"readahead", EROFS_ZIP_CACHE_READAHEAD}, 381 {"readaround", EROFS_ZIP_CACHE_READAROUND}, 382 {} 383 }; 384 385 static const struct constant_table erofs_dax_param_enums[] = { 386 {"always", EROFS_MOUNT_DAX_ALWAYS}, 387 {"never", EROFS_MOUNT_DAX_NEVER}, 388 {} 389 }; 390 391 static const struct fs_parameter_spec erofs_fs_parameters[] = { 392 fsparam_flag_no("user_xattr", Opt_user_xattr), 393 fsparam_flag_no("acl", Opt_acl), 394 fsparam_enum("cache_strategy", Opt_cache_strategy, 395 erofs_param_cache_strategy), 396 fsparam_flag("dax", Opt_dax), 397 fsparam_enum("dax", Opt_dax_enum, erofs_dax_param_enums), 398 fsparam_string("device", Opt_device), 399 fsparam_string("fsid", Opt_fsid), 400 fsparam_string("domain_id", Opt_domain_id), 401 {} 402 }; 403 404 static bool erofs_fc_set_dax_mode(struct fs_context *fc, unsigned int mode) 405 { 406 #ifdef CONFIG_FS_DAX 407 struct erofs_sb_info *sbi = fc->s_fs_info; 408 409 switch (mode) { 410 case EROFS_MOUNT_DAX_ALWAYS: 411 set_opt(&sbi->opt, DAX_ALWAYS); 412 clear_opt(&sbi->opt, DAX_NEVER); 413 return true; 414 case EROFS_MOUNT_DAX_NEVER: 415 set_opt(&sbi->opt, DAX_NEVER); 416 clear_opt(&sbi->opt, DAX_ALWAYS); 417 return true; 418 default: 419 DBG_BUGON(1); 420 return false; 421 } 422 #else 423 errorfc(fc, "dax options not supported"); 424 return false; 425 #endif 426 } 427 428 static int erofs_fc_parse_param(struct fs_context *fc, 429 struct fs_parameter *param) 430 { 431 struct erofs_sb_info *sbi = fc->s_fs_info; 432 struct fs_parse_result result; 433 struct erofs_device_info *dif; 434 int opt, ret; 435 436 opt = fs_parse(fc, erofs_fs_parameters, param, &result); 437 if (opt < 0) 438 return opt; 439 440 switch (opt) { 441 case Opt_user_xattr: 442 #ifdef CONFIG_EROFS_FS_XATTR 443 if (result.boolean) 444 set_opt(&sbi->opt, XATTR_USER); 445 else 446 clear_opt(&sbi->opt, XATTR_USER); 447 #else 448 errorfc(fc, "{,no}user_xattr options not supported"); 449 #endif 450 break; 451 case Opt_acl: 452 #ifdef CONFIG_EROFS_FS_POSIX_ACL 453 if (result.boolean) 454 set_opt(&sbi->opt, POSIX_ACL); 455 else 456 clear_opt(&sbi->opt, POSIX_ACL); 457 #else 458 errorfc(fc, "{,no}acl options not supported"); 459 #endif 460 break; 461 case Opt_cache_strategy: 462 #ifdef CONFIG_EROFS_FS_ZIP 463 sbi->opt.cache_strategy = result.uint_32; 464 #else 465 errorfc(fc, "compression not supported, cache_strategy ignored"); 466 #endif 467 break; 468 case Opt_dax: 469 if (!erofs_fc_set_dax_mode(fc, EROFS_MOUNT_DAX_ALWAYS)) 470 return -EINVAL; 471 break; 472 case Opt_dax_enum: 473 if (!erofs_fc_set_dax_mode(fc, result.uint_32)) 474 return -EINVAL; 475 break; 476 case Opt_device: 477 dif = kzalloc(sizeof(*dif), GFP_KERNEL); 478 if (!dif) 479 return -ENOMEM; 480 dif->path = kstrdup(param->string, GFP_KERNEL); 481 if (!dif->path) { 482 kfree(dif); 483 return -ENOMEM; 484 } 485 down_write(&sbi->devs->rwsem); 486 ret = idr_alloc(&sbi->devs->tree, dif, 0, 0, GFP_KERNEL); 487 up_write(&sbi->devs->rwsem); 488 if (ret < 0) { 489 kfree(dif->path); 490 kfree(dif); 491 return ret; 492 } 493 ++sbi->devs->extra_devices; 494 break; 495 #ifdef CONFIG_EROFS_FS_ONDEMAND 496 case Opt_fsid: 497 kfree(sbi->fsid); 498 sbi->fsid = kstrdup(param->string, GFP_KERNEL); 499 if (!sbi->fsid) 500 return -ENOMEM; 501 break; 502 case Opt_domain_id: 503 kfree(sbi->domain_id); 504 sbi->domain_id = kstrdup(param->string, GFP_KERNEL); 505 if (!sbi->domain_id) 506 return -ENOMEM; 507 break; 508 #else 509 case Opt_fsid: 510 case Opt_domain_id: 511 errorfc(fc, "%s option not supported", erofs_fs_parameters[opt].name); 512 break; 513 #endif 514 default: 515 return -ENOPARAM; 516 } 517 return 0; 518 } 519 520 static struct inode *erofs_nfs_get_inode(struct super_block *sb, 521 u64 ino, u32 generation) 522 { 523 return erofs_iget(sb, ino); 524 } 525 526 static struct dentry *erofs_fh_to_dentry(struct super_block *sb, 527 struct fid *fid, int fh_len, int fh_type) 528 { 529 return generic_fh_to_dentry(sb, fid, fh_len, fh_type, 530 erofs_nfs_get_inode); 531 } 532 533 static struct dentry *erofs_fh_to_parent(struct super_block *sb, 534 struct fid *fid, int fh_len, int fh_type) 535 { 536 return generic_fh_to_parent(sb, fid, fh_len, fh_type, 537 erofs_nfs_get_inode); 538 } 539 540 static struct dentry *erofs_get_parent(struct dentry *child) 541 { 542 erofs_nid_t nid; 543 unsigned int d_type; 544 int err; 545 546 err = erofs_namei(d_inode(child), &dotdot_name, &nid, &d_type); 547 if (err) 548 return ERR_PTR(err); 549 return d_obtain_alias(erofs_iget(child->d_sb, nid)); 550 } 551 552 static const struct export_operations erofs_export_ops = { 553 .encode_fh = generic_encode_ino32_fh, 554 .fh_to_dentry = erofs_fh_to_dentry, 555 .fh_to_parent = erofs_fh_to_parent, 556 .get_parent = erofs_get_parent, 557 }; 558 559 static void erofs_set_sysfs_name(struct super_block *sb) 560 { 561 struct erofs_sb_info *sbi = EROFS_SB(sb); 562 563 if (sbi->domain_id) 564 super_set_sysfs_name_generic(sb, "%s,%s", sbi->domain_id, 565 sbi->fsid); 566 else if (sbi->fsid) 567 super_set_sysfs_name_generic(sb, "%s", sbi->fsid); 568 else if (erofs_is_fileio_mode(sbi)) 569 super_set_sysfs_name_generic(sb, "%s", 570 bdi_dev_name(sb->s_bdi)); 571 else 572 super_set_sysfs_name_id(sb); 573 } 574 575 static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc) 576 { 577 struct inode *inode; 578 struct erofs_sb_info *sbi = EROFS_SB(sb); 579 int err; 580 581 sb->s_magic = EROFS_SUPER_MAGIC; 582 sb->s_flags |= SB_RDONLY | SB_NOATIME; 583 sb->s_maxbytes = MAX_LFS_FILESIZE; 584 sb->s_op = &erofs_sops; 585 586 sbi->blkszbits = PAGE_SHIFT; 587 if (!sb->s_bdev) { 588 sb->s_blocksize = PAGE_SIZE; 589 sb->s_blocksize_bits = PAGE_SHIFT; 590 591 if (erofs_is_fscache_mode(sb)) { 592 err = erofs_fscache_register_fs(sb); 593 if (err) 594 return err; 595 } 596 err = super_setup_bdi(sb); 597 if (err) 598 return err; 599 } else { 600 if (!sb_set_blocksize(sb, PAGE_SIZE)) { 601 errorfc(fc, "failed to set initial blksize"); 602 return -EINVAL; 603 } 604 605 sbi->dax_dev = fs_dax_get_by_bdev(sb->s_bdev, 606 &sbi->dax_part_off, 607 NULL, NULL); 608 } 609 610 err = erofs_read_superblock(sb); 611 if (err) 612 return err; 613 614 if (sb->s_blocksize_bits != sbi->blkszbits) { 615 if (erofs_is_fscache_mode(sb)) { 616 errorfc(fc, "unsupported blksize for fscache mode"); 617 return -EINVAL; 618 } 619 620 if (erofs_is_fileio_mode(sbi)) { 621 sb->s_blocksize = 1 << sbi->blkszbits; 622 sb->s_blocksize_bits = sbi->blkszbits; 623 } else if (!sb_set_blocksize(sb, 1 << sbi->blkszbits)) { 624 errorfc(fc, "failed to set erofs blksize"); 625 return -EINVAL; 626 } 627 } 628 629 if (test_opt(&sbi->opt, DAX_ALWAYS)) { 630 if (!sbi->dax_dev) { 631 errorfc(fc, "DAX unsupported by block device. Turning off DAX."); 632 clear_opt(&sbi->opt, DAX_ALWAYS); 633 } else if (sbi->blkszbits != PAGE_SHIFT) { 634 errorfc(fc, "unsupported blocksize for DAX"); 635 clear_opt(&sbi->opt, DAX_ALWAYS); 636 } 637 } 638 639 sb->s_time_gran = 1; 640 sb->s_xattr = erofs_xattr_handlers; 641 sb->s_export_op = &erofs_export_ops; 642 643 if (test_opt(&sbi->opt, POSIX_ACL)) 644 sb->s_flags |= SB_POSIXACL; 645 else 646 sb->s_flags &= ~SB_POSIXACL; 647 648 #ifdef CONFIG_EROFS_FS_ZIP 649 xa_init(&sbi->managed_pslots); 650 #endif 651 652 inode = erofs_iget(sb, sbi->root_nid); 653 if (IS_ERR(inode)) 654 return PTR_ERR(inode); 655 656 if (!S_ISDIR(inode->i_mode)) { 657 erofs_err(sb, "rootino(nid %llu) is not a directory(i_mode %o)", 658 sbi->root_nid, inode->i_mode); 659 iput(inode); 660 return -EINVAL; 661 } 662 663 sb->s_root = d_make_root(inode); 664 if (!sb->s_root) 665 return -ENOMEM; 666 667 erofs_shrinker_register(sb); 668 if (erofs_sb_has_fragments(sbi) && sbi->packed_nid) { 669 sbi->packed_inode = erofs_iget(sb, sbi->packed_nid); 670 if (IS_ERR(sbi->packed_inode)) { 671 err = PTR_ERR(sbi->packed_inode); 672 sbi->packed_inode = NULL; 673 return err; 674 } 675 } 676 err = erofs_init_managed_cache(sb); 677 if (err) 678 return err; 679 680 err = erofs_xattr_prefixes_init(sb); 681 if (err) 682 return err; 683 684 erofs_set_sysfs_name(sb); 685 err = erofs_register_sysfs(sb); 686 if (err) 687 return err; 688 689 erofs_info(sb, "mounted with root inode @ nid %llu.", sbi->root_nid); 690 return 0; 691 } 692 693 static int erofs_fc_get_tree(struct fs_context *fc) 694 { 695 struct erofs_sb_info *sbi = fc->s_fs_info; 696 int ret; 697 698 if (IS_ENABLED(CONFIG_EROFS_FS_ONDEMAND) && sbi->fsid) 699 return get_tree_nodev(fc, erofs_fc_fill_super); 700 701 ret = get_tree_bdev_flags(fc, erofs_fc_fill_super, 702 IS_ENABLED(CONFIG_EROFS_FS_BACKED_BY_FILE) ? 703 GET_TREE_BDEV_QUIET_LOOKUP : 0); 704 #ifdef CONFIG_EROFS_FS_BACKED_BY_FILE 705 if (ret == -ENOTBLK) { 706 if (!fc->source) 707 return invalf(fc, "No source specified"); 708 sbi->fdev = filp_open(fc->source, O_RDONLY | O_LARGEFILE, 0); 709 if (IS_ERR(sbi->fdev)) 710 return PTR_ERR(sbi->fdev); 711 712 if (S_ISREG(file_inode(sbi->fdev)->i_mode) && 713 sbi->fdev->f_mapping->a_ops->read_folio) 714 return get_tree_nodev(fc, erofs_fc_fill_super); 715 fput(sbi->fdev); 716 } 717 #endif 718 return ret; 719 } 720 721 static int erofs_fc_reconfigure(struct fs_context *fc) 722 { 723 struct super_block *sb = fc->root->d_sb; 724 struct erofs_sb_info *sbi = EROFS_SB(sb); 725 struct erofs_sb_info *new_sbi = fc->s_fs_info; 726 727 DBG_BUGON(!sb_rdonly(sb)); 728 729 if (new_sbi->fsid || new_sbi->domain_id) 730 erofs_info(sb, "ignoring reconfiguration for fsid|domain_id."); 731 732 if (test_opt(&new_sbi->opt, POSIX_ACL)) 733 fc->sb_flags |= SB_POSIXACL; 734 else 735 fc->sb_flags &= ~SB_POSIXACL; 736 737 sbi->opt = new_sbi->opt; 738 739 fc->sb_flags |= SB_RDONLY; 740 return 0; 741 } 742 743 static int erofs_release_device_info(int id, void *ptr, void *data) 744 { 745 struct erofs_device_info *dif = ptr; 746 747 fs_put_dax(dif->dax_dev, NULL); 748 if (dif->file) 749 fput(dif->file); 750 erofs_fscache_unregister_cookie(dif->fscache); 751 dif->fscache = NULL; 752 kfree(dif->path); 753 kfree(dif); 754 return 0; 755 } 756 757 static void erofs_free_dev_context(struct erofs_dev_context *devs) 758 { 759 if (!devs) 760 return; 761 idr_for_each(&devs->tree, &erofs_release_device_info, NULL); 762 idr_destroy(&devs->tree); 763 kfree(devs); 764 } 765 766 static void erofs_fc_free(struct fs_context *fc) 767 { 768 struct erofs_sb_info *sbi = fc->s_fs_info; 769 770 if (!sbi) 771 return; 772 773 erofs_free_dev_context(sbi->devs); 774 kfree(sbi->fsid); 775 kfree(sbi->domain_id); 776 kfree(sbi); 777 } 778 779 static const struct fs_context_operations erofs_context_ops = { 780 .parse_param = erofs_fc_parse_param, 781 .get_tree = erofs_fc_get_tree, 782 .reconfigure = erofs_fc_reconfigure, 783 .free = erofs_fc_free, 784 }; 785 786 static int erofs_init_fs_context(struct fs_context *fc) 787 { 788 struct erofs_sb_info *sbi; 789 790 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL); 791 if (!sbi) 792 return -ENOMEM; 793 794 sbi->devs = kzalloc(sizeof(struct erofs_dev_context), GFP_KERNEL); 795 if (!sbi->devs) { 796 kfree(sbi); 797 return -ENOMEM; 798 } 799 fc->s_fs_info = sbi; 800 801 idr_init(&sbi->devs->tree); 802 init_rwsem(&sbi->devs->rwsem); 803 erofs_default_options(sbi); 804 fc->ops = &erofs_context_ops; 805 return 0; 806 } 807 808 static void erofs_kill_sb(struct super_block *sb) 809 { 810 struct erofs_sb_info *sbi = EROFS_SB(sb); 811 812 if ((IS_ENABLED(CONFIG_EROFS_FS_ONDEMAND) && sbi->fsid) || sbi->fdev) 813 kill_anon_super(sb); 814 else 815 kill_block_super(sb); 816 817 erofs_free_dev_context(sbi->devs); 818 fs_put_dax(sbi->dax_dev, NULL); 819 erofs_fscache_unregister_fs(sb); 820 kfree(sbi->fsid); 821 kfree(sbi->domain_id); 822 if (sbi->fdev) 823 fput(sbi->fdev); 824 kfree(sbi); 825 sb->s_fs_info = NULL; 826 } 827 828 static void erofs_put_super(struct super_block *sb) 829 { 830 struct erofs_sb_info *const sbi = EROFS_SB(sb); 831 832 DBG_BUGON(!sbi); 833 834 erofs_unregister_sysfs(sb); 835 erofs_shrinker_unregister(sb); 836 erofs_xattr_prefixes_cleanup(sb); 837 #ifdef CONFIG_EROFS_FS_ZIP 838 iput(sbi->managed_cache); 839 sbi->managed_cache = NULL; 840 #endif 841 iput(sbi->packed_inode); 842 sbi->packed_inode = NULL; 843 erofs_free_dev_context(sbi->devs); 844 sbi->devs = NULL; 845 erofs_fscache_unregister_fs(sb); 846 } 847 848 static struct file_system_type erofs_fs_type = { 849 .owner = THIS_MODULE, 850 .name = "erofs", 851 .init_fs_context = erofs_init_fs_context, 852 .kill_sb = erofs_kill_sb, 853 .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP, 854 }; 855 MODULE_ALIAS_FS("erofs"); 856 857 static int __init erofs_module_init(void) 858 { 859 int err; 860 861 erofs_check_ondisk_layout_definitions(); 862 863 erofs_inode_cachep = kmem_cache_create("erofs_inode", 864 sizeof(struct erofs_inode), 0, 865 SLAB_RECLAIM_ACCOUNT | SLAB_ACCOUNT, 866 erofs_inode_init_once); 867 if (!erofs_inode_cachep) 868 return -ENOMEM; 869 870 err = erofs_init_shrinker(); 871 if (err) 872 goto shrinker_err; 873 874 err = z_erofs_init_subsystem(); 875 if (err) 876 goto zip_err; 877 878 err = erofs_init_sysfs(); 879 if (err) 880 goto sysfs_err; 881 882 err = register_filesystem(&erofs_fs_type); 883 if (err) 884 goto fs_err; 885 886 return 0; 887 888 fs_err: 889 erofs_exit_sysfs(); 890 sysfs_err: 891 z_erofs_exit_subsystem(); 892 zip_err: 893 erofs_exit_shrinker(); 894 shrinker_err: 895 kmem_cache_destroy(erofs_inode_cachep); 896 return err; 897 } 898 899 static void __exit erofs_module_exit(void) 900 { 901 unregister_filesystem(&erofs_fs_type); 902 903 /* Ensure all RCU free inodes / pclusters are safe to be destroyed. */ 904 rcu_barrier(); 905 906 erofs_exit_sysfs(); 907 z_erofs_exit_subsystem(); 908 erofs_exit_shrinker(); 909 kmem_cache_destroy(erofs_inode_cachep); 910 } 911 912 static int erofs_statfs(struct dentry *dentry, struct kstatfs *buf) 913 { 914 struct super_block *sb = dentry->d_sb; 915 struct erofs_sb_info *sbi = EROFS_SB(sb); 916 917 buf->f_type = sb->s_magic; 918 buf->f_bsize = sb->s_blocksize; 919 buf->f_blocks = sbi->total_blocks; 920 buf->f_bfree = buf->f_bavail = 0; 921 buf->f_files = ULLONG_MAX; 922 buf->f_ffree = ULLONG_MAX - sbi->inos; 923 buf->f_namelen = EROFS_NAME_LEN; 924 925 if (uuid_is_null(&sb->s_uuid)) 926 buf->f_fsid = u64_to_fsid(!sb->s_bdev ? 0 : 927 huge_encode_dev(sb->s_bdev->bd_dev)); 928 else 929 buf->f_fsid = uuid_to_fsid(sb->s_uuid.b); 930 return 0; 931 } 932 933 static int erofs_show_options(struct seq_file *seq, struct dentry *root) 934 { 935 struct erofs_sb_info *sbi = EROFS_SB(root->d_sb); 936 struct erofs_mount_opts *opt = &sbi->opt; 937 938 if (IS_ENABLED(CONFIG_EROFS_FS_XATTR)) 939 seq_puts(seq, test_opt(opt, XATTR_USER) ? 940 ",user_xattr" : ",nouser_xattr"); 941 if (IS_ENABLED(CONFIG_EROFS_FS_POSIX_ACL)) 942 seq_puts(seq, test_opt(opt, POSIX_ACL) ? ",acl" : ",noacl"); 943 if (IS_ENABLED(CONFIG_EROFS_FS_ZIP)) 944 seq_printf(seq, ",cache_strategy=%s", 945 erofs_param_cache_strategy[opt->cache_strategy].name); 946 if (test_opt(opt, DAX_ALWAYS)) 947 seq_puts(seq, ",dax=always"); 948 if (test_opt(opt, DAX_NEVER)) 949 seq_puts(seq, ",dax=never"); 950 #ifdef CONFIG_EROFS_FS_ONDEMAND 951 if (sbi->fsid) 952 seq_printf(seq, ",fsid=%s", sbi->fsid); 953 if (sbi->domain_id) 954 seq_printf(seq, ",domain_id=%s", sbi->domain_id); 955 #endif 956 return 0; 957 } 958 959 const struct super_operations erofs_sops = { 960 .put_super = erofs_put_super, 961 .alloc_inode = erofs_alloc_inode, 962 .free_inode = erofs_free_inode, 963 .statfs = erofs_statfs, 964 .show_options = erofs_show_options, 965 }; 966 967 module_init(erofs_module_init); 968 module_exit(erofs_module_exit); 969 970 MODULE_DESCRIPTION("Enhanced ROM File System"); 971 MODULE_AUTHOR("Gao Xiang, Chao Yu, Miao Xie, CONSUMER BG, HUAWEI Inc."); 972 MODULE_LICENSE("GPL"); 973