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