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