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 <linux/pseudo_fs.h> 15 #include "xattr.h" 16 17 #define CREATE_TRACE_POINTS 18 #include <trace/events/erofs.h> 19 20 static struct kmem_cache *erofs_inode_cachep __read_mostly; 21 22 void _erofs_printk(struct super_block *sb, const char *fmt, ...) 23 { 24 struct va_format vaf; 25 va_list args; 26 int level; 27 28 va_start(args, fmt); 29 30 level = printk_get_level(fmt); 31 vaf.fmt = printk_skip_level(fmt); 32 vaf.va = &args; 33 if (sb) 34 printk("%c%cerofs (device %s): %pV", 35 KERN_SOH_ASCII, level, sb->s_id, &vaf); 36 else 37 printk("%c%cerofs: %pV", KERN_SOH_ASCII, level, &vaf); 38 va_end(args); 39 } 40 41 static int erofs_superblock_csum_verify(struct super_block *sb, void *sbdata) 42 { 43 struct erofs_super_block *dsb = sbdata + EROFS_SUPER_OFFSET; 44 u32 len = 1 << EROFS_SB(sb)->blkszbits, crc; 45 46 if (len > EROFS_SUPER_OFFSET) 47 len -= EROFS_SUPER_OFFSET; 48 len -= offsetof(struct erofs_super_block, checksum) + 49 sizeof(dsb->checksum); 50 51 /* skip .magic(pre-verified) and .checksum(0) fields */ 52 crc = crc32c(0x5045B54A, (&dsb->checksum) + 1, len); 53 if (crc == le32_to_cpu(dsb->checksum)) 54 return 0; 55 erofs_err(sb, "invalid checksum 0x%08x, 0x%08x expected", 56 crc, le32_to_cpu(dsb->checksum)); 57 return -EBADMSG; 58 } 59 60 static void erofs_inode_init_once(void *ptr) 61 { 62 struct erofs_inode *vi = ptr; 63 64 inode_init_once(&vi->vfs_inode); 65 } 66 67 static struct inode *erofs_alloc_inode(struct super_block *sb) 68 { 69 struct erofs_inode *vi = 70 alloc_inode_sb(sb, erofs_inode_cachep, GFP_KERNEL); 71 72 if (!vi) 73 return NULL; 74 75 /* zero out everything except vfs_inode */ 76 memset(vi, 0, offsetof(struct erofs_inode, vfs_inode)); 77 return &vi->vfs_inode; 78 } 79 80 static void erofs_free_inode(struct inode *inode) 81 { 82 struct erofs_inode *vi = EROFS_I(inode); 83 84 if (inode->i_op == &erofs_fast_symlink_iops) 85 kfree(inode->i_link); 86 kfree(vi->xattr_shared_xattrs); 87 kmem_cache_free(erofs_inode_cachep, vi); 88 } 89 90 /* read variable-sized metadata, offset will be aligned by 4-byte */ 91 void *erofs_read_metadata(struct super_block *sb, struct erofs_buf *buf, 92 erofs_off_t *offset, int *lengthp) 93 { 94 u8 *buffer, *ptr; 95 int len, i, cnt; 96 97 *offset = round_up(*offset, 4); 98 ptr = erofs_bread(buf, *offset, true); 99 if (IS_ERR(ptr)) 100 return ptr; 101 102 len = le16_to_cpu(*(__le16 *)ptr); 103 if (!len) 104 len = U16_MAX + 1; 105 buffer = kmalloc(len, GFP_KERNEL); 106 if (!buffer) 107 return ERR_PTR(-ENOMEM); 108 *offset += sizeof(__le16); 109 *lengthp = len; 110 111 for (i = 0; i < len; i += cnt) { 112 cnt = min_t(int, sb->s_blocksize - erofs_blkoff(sb, *offset), 113 len - i); 114 ptr = erofs_bread(buf, *offset, true); 115 if (IS_ERR(ptr)) { 116 kfree(buffer); 117 return ptr; 118 } 119 memcpy(buffer + i, ptr, cnt); 120 *offset += cnt; 121 } 122 return buffer; 123 } 124 125 static int erofs_init_device(struct erofs_buf *buf, struct super_block *sb, 126 struct erofs_device_info *dif, erofs_off_t *pos) 127 { 128 struct erofs_sb_info *sbi = EROFS_SB(sb); 129 struct erofs_deviceslot *dis; 130 struct file *file; 131 bool _48bit; 132 133 dis = erofs_read_metabuf(buf, sb, *pos, false); 134 if (IS_ERR(dis)) 135 return PTR_ERR(dis); 136 137 if (!sbi->devs->flatdev && !dif->path) { 138 if (!dis->tag[0]) { 139 erofs_err(sb, "empty device tag @ pos %llu", *pos); 140 return -EINVAL; 141 } 142 dif->path = kmemdup_nul(dis->tag, sizeof(dis->tag), GFP_KERNEL); 143 if (!dif->path) 144 return -ENOMEM; 145 } 146 147 if (!sbi->devs->flatdev) { 148 file = erofs_is_fileio_mode(sbi) ? 149 filp_open(dif->path, O_RDONLY | O_LARGEFILE, 0) : 150 fs_bdev_file_open_by_path(dif->path, 151 BLK_OPEN_READ, sb->s_type, sb); 152 if (IS_ERR(file)) { 153 if (file == ERR_PTR(-ENOTBLK)) 154 return -EINVAL; 155 return PTR_ERR(file); 156 } 157 158 if (!erofs_is_fileio_mode(sbi)) { 159 dif->dax_dev = fs_dax_get_by_bdev(file_bdev(file), 160 &dif->dax_part_off, NULL, NULL); 161 } else if (!S_ISREG(file_inode(file)->i_mode)) { 162 fput(file); 163 return -EINVAL; 164 } 165 if (!dif->dax_dev && test_opt(&sbi->opt, DAX_ALWAYS)) { 166 erofs_info(sb, "DAX unsupported by %s. Turning off DAX.", 167 dif->path); 168 clear_opt(&sbi->opt, DAX_ALWAYS); 169 } 170 dif->file = file; 171 } 172 173 _48bit = erofs_sb_has_48bit(sbi); 174 dif->blocks = le32_to_cpu(dis->blocks_lo) | 175 (_48bit ? (u64)le16_to_cpu(dis->blocks_hi) << 32 : 0); 176 dif->uniaddr = le32_to_cpu(dis->uniaddr_lo) | 177 (_48bit ? (u64)le16_to_cpu(dis->uniaddr_hi) << 32 : 0); 178 sbi->total_blocks += dif->blocks; 179 *pos += EROFS_DEVT_SLOT_SIZE; 180 return 0; 181 } 182 183 static int erofs_scan_devices(struct super_block *sb, 184 struct erofs_super_block *dsb) 185 { 186 struct erofs_sb_info *sbi = EROFS_SB(sb); 187 unsigned int ondisk_extradevs; 188 erofs_off_t pos; 189 struct erofs_buf buf = __EROFS_BUF_INITIALIZER; 190 struct erofs_device_info *dif; 191 int id, err = 0; 192 193 sbi->total_blocks = sbi->dif0.blocks; 194 if (!erofs_sb_has_device_table(sbi)) 195 ondisk_extradevs = 0; 196 else 197 ondisk_extradevs = le16_to_cpu(dsb->extra_devices); 198 199 if (sbi->devs->extra_devices && 200 ondisk_extradevs != sbi->devs->extra_devices) { 201 erofs_err(sb, "extra devices don't match (ondisk %u, given %u)", 202 ondisk_extradevs, sbi->devs->extra_devices); 203 return -EINVAL; 204 } 205 206 if (test_opt(&sbi->opt, DAX_ALWAYS) && !sbi->dif0.dax_dev) { 207 erofs_info(sb, "DAX unsupported by block device. Turning off DAX."); 208 clear_opt(&sbi->opt, DAX_ALWAYS); 209 } 210 if (!ondisk_extradevs) 211 return 0; 212 213 if (!sbi->devs->extra_devices) 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_obj(*dif); 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, false); 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 if (erofs_sb_has_ishare_xattrs(sbi)) { 310 if (dsb->ishare_xattr_prefix_id >= sbi->xattr_prefix_count) { 311 erofs_err(sb, "invalid ishare xattr prefix id %u", 312 dsb->ishare_xattr_prefix_id); 313 ret = -EFSCORRUPTED; 314 goto out; 315 } 316 sbi->ishare_xattr_prefix_id = dsb->ishare_xattr_prefix_id; 317 } 318 #endif 319 sbi->islotbits = ilog2(sizeof(struct erofs_inode_compact)); 320 if (erofs_sb_has_48bit(sbi) && dsb->rootnid_8b) { 321 sbi->root_nid = le64_to_cpu(dsb->rootnid_8b); 322 sbi->dif0.blocks = sbi->dif0.blocks | 323 ((u64)le16_to_cpu(dsb->rb.blocks_hi) << 32); 324 } else { 325 sbi->root_nid = le16_to_cpu(dsb->rb.rootnid_2b); 326 } 327 sbi->packed_nid = le64_to_cpu(dsb->packed_nid); 328 if (erofs_sb_has_metabox(sbi)) { 329 ret = -EFSCORRUPTED; 330 if (sbi->sb_size <= offsetof(struct erofs_super_block, 331 metabox_nid)) 332 goto out; 333 sbi->metabox_nid = le64_to_cpu(dsb->metabox_nid); 334 if (sbi->metabox_nid & BIT_ULL(EROFS_DIRENT_NID_METABOX_BIT)) 335 goto out; /* self-loop detection */ 336 } 337 sbi->inos = le64_to_cpu(dsb->inos); 338 339 sbi->epoch = (s64)le64_to_cpu(dsb->epoch); 340 sbi->fixed_nsec = le32_to_cpu(dsb->fixed_nsec); 341 super_set_uuid(sb, (void *)dsb->uuid, sizeof(dsb->uuid)); 342 343 if (dsb->volume_name[0]) { 344 sbi->volume_name = kstrndup(dsb->volume_name, 345 sizeof(dsb->volume_name), GFP_KERNEL); 346 if (!sbi->volume_name) { 347 ret = -ENOMEM; 348 goto out; 349 } 350 } 351 352 if (IS_ENABLED(CONFIG_EROFS_FS_ZIP)) { 353 ret = z_erofs_parse_cfgs(sb, dsb); 354 if (ret < 0) 355 goto out; 356 } else if (dsb->u1.available_compr_algs || 357 erofs_sb_has_lz4_0padding(sbi)) { 358 erofs_err(sb, "compression disabled, unable to mount compressed EROFS"); 359 ret = -EOPNOTSUPP; 360 goto out; 361 } 362 363 ret = erofs_scan_devices(sb, dsb); 364 365 if (erofs_sb_has_48bit(sbi)) 366 erofs_info(sb, "EXPERIMENTAL 48-bit layout support in use. Use at your own risk!"); 367 if (erofs_sb_has_metabox(sbi)) 368 erofs_info(sb, "EXPERIMENTAL metadata compression support in use. Use at your own risk!"); 369 out: 370 erofs_put_metabuf(&buf); 371 return ret; 372 } 373 374 static void erofs_default_options(struct erofs_sb_info *sbi) 375 { 376 #ifdef CONFIG_EROFS_FS_ZIP 377 sbi->opt.cache_strategy = EROFS_ZIP_CACHE_READAROUND; 378 sbi->sync_decompress = EROFS_SYNC_DECOMPRESS_AUTO; 379 #endif 380 if (IS_ENABLED(CONFIG_EROFS_FS_XATTR)) 381 set_opt(&sbi->opt, XATTR_USER); 382 if (IS_ENABLED(CONFIG_EROFS_FS_POSIX_ACL)) 383 set_opt(&sbi->opt, POSIX_ACL); 384 } 385 386 enum { 387 Opt_user_xattr, Opt_acl, Opt_cache_strategy, Opt_dax, Opt_dax_enum, 388 Opt_device, Opt_domain_id, Opt_directio, Opt_fsoffset, Opt_inode_share, 389 Opt_source, Opt_superblock_share, 390 }; 391 392 static const struct constant_table erofs_param_cache_strategy[] = { 393 {"disabled", EROFS_ZIP_CACHE_DISABLED}, 394 {"readahead", EROFS_ZIP_CACHE_READAHEAD}, 395 {"readaround", EROFS_ZIP_CACHE_READAROUND}, 396 {} 397 }; 398 399 static const struct constant_table erofs_dax_param_enums[] = { 400 {"always", EROFS_MOUNT_DAX_ALWAYS}, 401 {"never", EROFS_MOUNT_DAX_NEVER}, 402 {} 403 }; 404 405 static const struct fs_parameter_spec erofs_fs_parameters[] = { 406 fsparam_flag_no("user_xattr", Opt_user_xattr), 407 fsparam_flag_no("acl", Opt_acl), 408 fsparam_enum("cache_strategy", Opt_cache_strategy, 409 erofs_param_cache_strategy), 410 fsparam_flag("dax", Opt_dax), 411 fsparam_enum("dax", Opt_dax_enum, erofs_dax_param_enums), 412 fsparam_string("device", Opt_device), 413 fsparam_string("domain_id", Opt_domain_id), 414 fsparam_flag_no("directio", Opt_directio), 415 fsparam_u64("fsoffset", Opt_fsoffset), 416 fsparam_flag("inode_share", Opt_inode_share), 417 fsparam_file_or_string("source", Opt_source), 418 fsparam_flag("superblock_share", Opt_superblock_share), 419 {} 420 }; 421 422 static bool erofs_fc_set_dax_mode(struct fs_context *fc, unsigned int mode) 423 { 424 if (IS_ENABLED(CONFIG_FS_DAX)) { 425 struct erofs_sb_info *sbi = fc->s_fs_info; 426 427 if (mode == EROFS_MOUNT_DAX_ALWAYS) { 428 set_opt(&sbi->opt, DAX_ALWAYS); 429 clear_opt(&sbi->opt, DAX_NEVER); 430 return true; 431 } else if (mode == EROFS_MOUNT_DAX_NEVER) { 432 set_opt(&sbi->opt, DAX_NEVER); 433 clear_opt(&sbi->opt, DAX_ALWAYS); 434 return true; 435 } 436 DBG_BUGON(1); 437 return false; 438 } 439 errorfc(fc, "dax options not supported"); 440 return false; 441 } 442 443 static int erofs_fc_parse_source(struct fs_context *fc, 444 struct fs_parameter *param) 445 { 446 struct erofs_sb_info *sbi = fc->s_fs_info; 447 448 if (fc->source || sbi->dif0.file) 449 return invalf(fc, "Multiple sources"); 450 451 switch (param->type) { 452 case fs_value_is_string: 453 fc->source = param->string; 454 param->string = NULL; 455 return 0; 456 case fs_value_is_file: { 457 char *buf __free(kfree) = kmalloc(PATH_MAX, GFP_KERNEL); 458 char *p; 459 460 if (!buf) 461 return -ENOMEM; 462 p = file_path(param->file, buf, PATH_MAX); 463 if (IS_ERR(p)) 464 return PTR_ERR(p); 465 fc->source = kstrdup(p, GFP_KERNEL); 466 if (!fc->source) 467 return -ENOMEM; 468 sbi->dif0.file = no_free_ptr(param->file); 469 return 0; 470 } 471 default: 472 WARN_ON_ONCE(true); 473 return -EINVAL; 474 } 475 } 476 477 static int erofs_fc_parse_param(struct fs_context *fc, 478 struct fs_parameter *param) 479 { 480 struct erofs_sb_info *sbi = fc->s_fs_info; 481 struct fs_parse_result result; 482 struct erofs_device_info *dif; 483 int opt, ret; 484 485 opt = fs_parse(fc, erofs_fs_parameters, param, &result); 486 if (opt < 0) 487 return opt; 488 489 switch (opt) { 490 case Opt_user_xattr: 491 if (!IS_ENABLED(CONFIG_EROFS_FS_XATTR)) 492 errorfc(fc, "{,no}user_xattr options not supported"); 493 else if (result.boolean) 494 set_opt(&sbi->opt, XATTR_USER); 495 else 496 clear_opt(&sbi->opt, XATTR_USER); 497 break; 498 case Opt_acl: 499 if (!IS_ENABLED(CONFIG_EROFS_FS_POSIX_ACL)) 500 errorfc(fc, "{,no}acl options not supported"); 501 else if (result.boolean) 502 set_opt(&sbi->opt, POSIX_ACL); 503 else 504 clear_opt(&sbi->opt, POSIX_ACL); 505 break; 506 case Opt_cache_strategy: 507 if (!IS_ENABLED(CONFIG_EROFS_FS_ZIP)) 508 errorfc(fc, "compression not supported, cache_strategy ignored"); 509 else 510 sbi->opt.cache_strategy = result.uint_32; 511 break; 512 case Opt_dax: 513 if (!erofs_fc_set_dax_mode(fc, EROFS_MOUNT_DAX_ALWAYS)) 514 return -EINVAL; 515 break; 516 case Opt_dax_enum: 517 if (!erofs_fc_set_dax_mode(fc, result.uint_32)) 518 return -EINVAL; 519 break; 520 case Opt_device: 521 dif = kzalloc_obj(*dif); 522 if (!dif) 523 return -ENOMEM; 524 dif->path = kstrdup(param->string, GFP_KERNEL); 525 if (!dif->path) { 526 kfree(dif); 527 return -ENOMEM; 528 } 529 down_write(&sbi->devs->rwsem); 530 ret = idr_alloc(&sbi->devs->tree, dif, 0, 0, GFP_KERNEL); 531 up_write(&sbi->devs->rwsem); 532 if (ret < 0) { 533 kfree(dif->path); 534 kfree(dif); 535 return ret; 536 } 537 ++sbi->devs->extra_devices; 538 break; 539 case Opt_domain_id: 540 if (!IS_ENABLED(CONFIG_EROFS_FS_PAGE_CACHE_SHARE)) { 541 errorfc(fc, "%s option not supported", erofs_fs_parameters[opt].name); 542 } else { 543 kfree_sensitive(sbi->domain_id); 544 sbi->domain_id = no_free_ptr(param->string); 545 } 546 break; 547 case Opt_directio: 548 if (!IS_ENABLED(CONFIG_EROFS_FS_BACKED_BY_FILE)) 549 errorfc(fc, "%s option not supported", erofs_fs_parameters[opt].name); 550 else if (result.boolean) 551 set_opt(&sbi->opt, DIRECT_IO); 552 else 553 clear_opt(&sbi->opt, DIRECT_IO); 554 break; 555 case Opt_fsoffset: 556 sbi->dif0.fsoff = result.uint_64; 557 break; 558 case Opt_inode_share: 559 if (!IS_ENABLED(CONFIG_EROFS_FS_PAGE_CACHE_SHARE)) 560 errorfc(fc, "%s option not supported", erofs_fs_parameters[opt].name); 561 else 562 set_opt(&sbi->opt, INODE_SHARE); 563 break; 564 case Opt_superblock_share: 565 if (!IS_ENABLED(CONFIG_EROFS_FS_BACKED_BY_FILE)) 566 errorfc(fc, "%s option not supported", erofs_fs_parameters[opt].name); 567 else 568 set_opt(&sbi->opt, SUPERBLOCK_SHARE); 569 break; 570 case Opt_source: 571 return erofs_fc_parse_source(fc, param); 572 } 573 return 0; 574 } 575 576 static int erofs_encode_fh(struct inode *inode, u32 *fh, int *max_len, 577 struct inode *parent) 578 { 579 erofs_nid_t nid = EROFS_I(inode)->nid; 580 int len = parent ? 6 : 3; 581 582 if (*max_len < len) { 583 *max_len = len; 584 return FILEID_INVALID; 585 } 586 587 fh[0] = (u32)(nid >> 32); 588 fh[1] = (u32)(nid & 0xffffffff); 589 fh[2] = inode->i_generation; 590 591 if (parent) { 592 nid = EROFS_I(parent)->nid; 593 594 fh[3] = (u32)(nid >> 32); 595 fh[4] = (u32)(nid & 0xffffffff); 596 fh[5] = parent->i_generation; 597 } 598 599 *max_len = len; 600 return parent ? FILEID_INO64_GEN_PARENT : FILEID_INO64_GEN; 601 } 602 603 static struct dentry *erofs_fh_to_dentry(struct super_block *sb, 604 struct fid *fid, int fh_len, int fh_type) 605 { 606 if ((fh_type != FILEID_INO64_GEN && 607 fh_type != FILEID_INO64_GEN_PARENT) || fh_len < 3) 608 return NULL; 609 610 return d_obtain_alias(erofs_iget(sb, 611 ((u64)fid->raw[0] << 32) | fid->raw[1])); 612 } 613 614 static struct dentry *erofs_fh_to_parent(struct super_block *sb, 615 struct fid *fid, int fh_len, int fh_type) 616 { 617 if (fh_type != FILEID_INO64_GEN_PARENT || fh_len < 6) 618 return NULL; 619 620 return d_obtain_alias(erofs_iget(sb, 621 ((u64)fid->raw[3] << 32) | fid->raw[4])); 622 } 623 624 static struct dentry *erofs_get_parent(struct dentry *child) 625 { 626 erofs_nid_t nid; 627 unsigned int d_type; 628 int err; 629 630 err = erofs_namei(d_inode(child), &dotdot_name, &nid, &d_type); 631 if (err) 632 return ERR_PTR(err); 633 return d_obtain_alias(erofs_iget(child->d_sb, nid)); 634 } 635 636 static const struct export_operations erofs_export_ops = { 637 .encode_fh = erofs_encode_fh, 638 .fh_to_dentry = erofs_fh_to_dentry, 639 .fh_to_parent = erofs_fh_to_parent, 640 .get_parent = erofs_get_parent, 641 }; 642 643 int erofs_setup_managed_cache(struct super_block *sb) 644 { 645 if (!EROFS_SB(sb)->managed_cache) { 646 struct inode *inode = new_inode(sb); 647 648 if (!inode) 649 return -ENOMEM; 650 set_nlink(inode, 1); 651 inode->i_size = OFFSET_MAX; 652 mapping_set_gfp_mask(inode->i_mapping, GFP_KERNEL); 653 EROFS_SB(sb)->managed_cache = inode; 654 } 655 return 0; 656 } 657 658 static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc) 659 { 660 struct inode *inode; 661 struct erofs_sb_info *sbi = EROFS_SB(sb); 662 int err; 663 664 sb->s_magic = EROFS_SUPER_MAGIC; 665 sb->s_flags |= SB_RDONLY | SB_NOATIME; 666 sb->s_maxbytes = MAX_LFS_FILESIZE; 667 sb->s_op = &erofs_sops; 668 669 if (!sbi->domain_id && test_opt(&sbi->opt, INODE_SHARE)) { 670 errorfc(fc, "domain_id is needed when inode_share is on"); 671 return -EINVAL; 672 } 673 if (test_opt(&sbi->opt, DAX_ALWAYS) && test_opt(&sbi->opt, INODE_SHARE)) { 674 errorfc(fc, "FSDAX is not allowed when inode_share is on"); 675 return -EINVAL; 676 } 677 if (test_opt(&sbi->opt, SUPERBLOCK_SHARE) && 678 test_opt(&sbi->opt, INODE_SHARE)) { 679 errorfc(fc, "superblock_share is not allowed when inode_share is on"); 680 return -EINVAL; 681 } 682 if (test_opt(&sbi->opt, SUPERBLOCK_SHARE) && 683 sbi->devs->extra_devices) { 684 errorfc(fc, "superblock_share does not support extra devices"); 685 return -EINVAL; 686 } 687 688 sbi->blkszbits = PAGE_SHIFT; 689 if (erofs_is_fileio_mode(sbi)) { 690 /* 691 * (File-backed mounts) EROFS claims it's safe to nest other 692 * fs contexts (including its own) due to self-controlled RO 693 * accesses/contexts and no side-effect changes that need to 694 * context save & restore so it can reuse the current thread 695 * context. 696 * However, we still need to prevent kernel stack overflow due 697 * to filesystem nesting: just ensure that s_stack_depth is 0 698 * to disallow mounting EROFS on stacked filesystems. 699 * Note: s_stack_depth is not incremented here for now, since 700 * EROFS is the only fs supporting file-backed mounts for now. 701 * It MUST change if another fs plans to support them, which 702 * may also require adjusting FILESYSTEM_MAX_STACK_DEPTH. 703 */ 704 inode = file_inode(sbi->dif0.file); 705 if ((inode->i_sb->s_op == &erofs_sops && 706 !inode->i_sb->s_bdev) || inode->i_sb->s_stack_depth) { 707 erofs_err(sb, "file-backed mounts cannot be applied to stacked fses"); 708 return -ENOTBLK; 709 } 710 sb->s_blocksize = PAGE_SIZE; 711 sb->s_blocksize_bits = PAGE_SHIFT; 712 713 err = super_setup_bdi(sb); 714 if (err) 715 return err; 716 err = erofs_setup_managed_cache(sb); 717 if (err) 718 return err; 719 720 snprintf(sb->s_id, sizeof(sb->s_id), 721 "%u:%u", MAJOR(sb->s_dev), MINOR(sb->s_dev)); 722 } else { 723 if (!sb_set_blocksize(sb, PAGE_SIZE)) { 724 errorfc(fc, "failed to set initial blksize"); 725 return -EINVAL; 726 } 727 sbi->dif0.dax_dev = fs_dax_get_by_bdev(sb->s_bdev, 728 &sbi->dif0.dax_part_off, NULL, NULL); 729 } 730 731 err = erofs_read_superblock(sb); 732 if (err) 733 return err; 734 735 if (sb->s_blocksize_bits != sbi->blkszbits) { 736 if (erofs_is_fileio_mode(sbi)) { 737 sb->s_blocksize = 1 << sbi->blkszbits; 738 sb->s_blocksize_bits = sbi->blkszbits; 739 } else if (!sb_set_blocksize(sb, 1 << sbi->blkszbits)) { 740 errorfc(fc, "failed to set erofs blksize"); 741 return -EINVAL; 742 } 743 } 744 if (sbi->dif0.fsoff & (sb->s_blocksize - 1)) 745 return invalfc(fc, "fsoffset %llu is not aligned to block size %lu", 746 sbi->dif0.fsoff, sb->s_blocksize); 747 748 if (test_opt(&sbi->opt, DAX_ALWAYS) && sbi->blkszbits != PAGE_SHIFT) { 749 erofs_info(sb, "unsupported blocksize for DAX"); 750 clear_opt(&sbi->opt, DAX_ALWAYS); 751 } 752 if (test_opt(&sbi->opt, INODE_SHARE) && !erofs_sb_has_ishare_xattrs(sbi)) { 753 erofs_info(sb, "on-disk ishare xattrs not found. Turning off inode_share."); 754 clear_opt(&sbi->opt, INODE_SHARE); 755 } 756 if (test_opt(&sbi->opt, INODE_SHARE)) 757 erofs_info(sb, "EXPERIMENTAL EROFS page cache share support in use. Use at your own risk!"); 758 759 sb->s_time_gran = 1; 760 sb->s_xattr = erofs_xattr_handlers; 761 sb->s_export_op = &erofs_export_ops; 762 763 if (test_opt(&sbi->opt, POSIX_ACL)) 764 sb->s_flags |= SB_POSIXACL; 765 else 766 sb->s_flags &= ~SB_POSIXACL; 767 768 err = z_erofs_init_super(sb); 769 if (err) 770 return err; 771 772 if (erofs_sb_has_fragments(sbi) && sbi->packed_nid) { 773 inode = erofs_iget(sb, sbi->packed_nid); 774 if (IS_ERR(inode)) 775 return PTR_ERR(inode); 776 sbi->packed_inode = inode; 777 } 778 if (erofs_sb_has_metabox(sbi)) { 779 inode = erofs_iget(sb, sbi->metabox_nid); 780 if (IS_ERR(inode)) 781 return PTR_ERR(inode); 782 sbi->metabox_inode = inode; 783 } 784 785 inode = erofs_iget(sb, sbi->root_nid); 786 if (IS_ERR(inode)) 787 return PTR_ERR(inode); 788 789 if (!S_ISDIR(inode->i_mode)) { 790 erofs_err(sb, "rootino(nid %llu) is not a directory(i_mode %o)", 791 sbi->root_nid, inode->i_mode); 792 iput(inode); 793 return -EINVAL; 794 } 795 sb->s_root = d_make_root(inode); 796 if (!sb->s_root) 797 return -ENOMEM; 798 799 erofs_shrinker_register(sb); 800 err = erofs_xattr_prefixes_init(sb); 801 if (err) 802 return err; 803 804 super_set_sysfs_name_id(sb); 805 err = erofs_register_sysfs(sb); 806 if (err) 807 return err; 808 809 sbi->dir_ra_bytes = EROFS_DIR_RA_BYTES; 810 erofs_info(sb, "mounted with root inode @ nid %llu.", sbi->root_nid); 811 return 0; 812 } 813 814 /* 815 * The function is used only with a sbi that has SUPERBLOCK_SHARE set, 816 * so that the memcmp later ensures new_sbi is also shareable. 817 */ 818 static int erofs_sb_share_test_super(struct super_block *sb, 819 struct fs_context *fc) 820 { 821 struct erofs_sb_info *sbi = EROFS_SB(sb); 822 struct erofs_sb_info *new_sbi = fc->s_fs_info; 823 824 if (sb->s_iflags & SB_I_RETIRED) 825 return 0; 826 if (!sbi->dif0.file || !new_sbi->dif0.file) 827 return 0; 828 return file_inode(sbi->dif0.file) == file_inode(new_sbi->dif0.file) && 829 sbi->dif0.fsoff == new_sbi->dif0.fsoff && 830 !memcmp(&sbi->opt, &new_sbi->opt, sizeof(sbi->opt)); 831 } 832 833 static int erofs_file_get_tree(struct fs_context *fc) 834 { 835 struct erofs_sb_info *sbi = fc->s_fs_info; 836 837 if (!test_opt(&sbi->opt, SUPERBLOCK_SHARE)) 838 return get_tree_nodev(fc, erofs_fc_fill_super); 839 840 return get_tree_super(fc, erofs_sb_share_test_super, erofs_fc_fill_super); 841 } 842 843 static int erofs_fc_get_tree(struct fs_context *fc) 844 { 845 struct erofs_sb_info *sbi = fc->s_fs_info; 846 int ret; 847 848 if (sbi->dif0.file) { 849 if (!IS_ENABLED(CONFIG_EROFS_FS_BACKED_BY_FILE)) { 850 errorfc(fc, "source fd option not supported"); 851 return -EINVAL; 852 } 853 if (!S_ISREG(file_inode(sbi->dif0.file)->i_mode) || 854 !sbi->dif0.file->f_mapping->a_ops->read_folio) { 855 errorfc(fc, "source is unsupported"); 856 return -EINVAL; 857 } 858 return erofs_file_get_tree(fc); 859 } 860 861 ret = get_tree_bdev_flags(fc, erofs_fc_fill_super, 862 IS_ENABLED(CONFIG_EROFS_FS_BACKED_BY_FILE) ? 863 GET_TREE_BDEV_QUIET_LOOKUP : 0); 864 if (IS_ENABLED(CONFIG_EROFS_FS_BACKED_BY_FILE) && ret == -ENOTBLK) { 865 struct file *file; 866 867 if (!fc->source) 868 return invalf(fc, "No source specified"); 869 file = filp_open(fc->source, O_RDONLY | O_LARGEFILE, 0); 870 if (IS_ERR(file)) 871 return PTR_ERR(file); 872 sbi->dif0.file = file; 873 874 if (S_ISREG(file_inode(sbi->dif0.file)->i_mode) && 875 sbi->dif0.file->f_mapping->a_ops->read_folio) 876 return erofs_file_get_tree(fc); 877 } 878 return ret; 879 } 880 881 static int erofs_fc_reconfigure(struct fs_context *fc) 882 { 883 struct super_block *sb = fc->root->d_sb; 884 struct erofs_sb_info *sbi = EROFS_SB(sb); 885 struct erofs_sb_info *new_sbi = fc->s_fs_info; 886 887 DBG_BUGON(!sb_rdonly(sb)); 888 889 /* Shared superblocks must not be reconfigured. */ 890 if (test_opt(&sbi->opt, SUPERBLOCK_SHARE)) 891 return -EBUSY; 892 893 if (new_sbi->domain_id) 894 erofs_info(sb, "ignoring reconfiguration for domain_id."); 895 896 if (test_opt(&new_sbi->opt, POSIX_ACL)) 897 fc->sb_flags |= SB_POSIXACL; 898 else 899 fc->sb_flags &= ~SB_POSIXACL; 900 901 sbi->opt = new_sbi->opt; 902 903 fc->sb_flags |= SB_RDONLY; 904 return 0; 905 } 906 907 static int erofs_release_device_info(int id, void *ptr, void *data) 908 { 909 struct super_block *sb = data; 910 struct erofs_device_info *dif = ptr; 911 912 fs_put_dax(dif->dax_dev, NULL); 913 if (dif->file) { 914 if (S_ISBLK(file_inode(dif->file)->i_mode)) 915 fs_bdev_file_release(dif->file, sb); 916 else 917 fput(dif->file); 918 } 919 kfree(dif->path); 920 kfree(dif); 921 return 0; 922 } 923 924 static void erofs_free_dev_context(struct erofs_dev_context *devs, 925 struct super_block *sb) 926 { 927 if (!devs) 928 return; 929 idr_for_each(&devs->tree, &erofs_release_device_info, sb); 930 idr_destroy(&devs->tree); 931 kfree(devs); 932 } 933 934 static void erofs_sb_free(struct erofs_sb_info *sbi, struct super_block *sb) 935 { 936 erofs_free_dev_context(sbi->devs, sb); 937 kfree_sensitive(sbi->domain_id); 938 if (sbi->dif0.file) 939 fput(sbi->dif0.file); 940 kfree(sbi->volume_name); 941 kfree(sbi); 942 } 943 944 static void erofs_fc_free(struct fs_context *fc) 945 { 946 struct erofs_sb_info *sbi = fc->s_fs_info; 947 948 /* 949 * Freed here only if an error occurs before the sb is set up; at that 950 * point no block-backed device has been claimed (that happens in 951 * fill_super), so the NULL sb never reaches fs_bdev_file_release(). 952 */ 953 if (sbi) 954 erofs_sb_free(sbi, NULL); 955 } 956 957 static const struct fs_context_operations erofs_context_ops = { 958 .parse_param = erofs_fc_parse_param, 959 .get_tree = erofs_fc_get_tree, 960 .reconfigure = erofs_fc_reconfigure, 961 .free = erofs_fc_free, 962 }; 963 964 static int erofs_init_fs_context(struct fs_context *fc) 965 { 966 struct erofs_sb_info *sbi; 967 968 sbi = kzalloc_obj(*sbi); 969 if (!sbi) 970 return -ENOMEM; 971 972 sbi->devs = kzalloc_obj(struct erofs_dev_context); 973 if (!sbi->devs) { 974 kfree(sbi); 975 return -ENOMEM; 976 } 977 fc->s_fs_info = sbi; 978 979 idr_init(&sbi->devs->tree); 980 init_rwsem(&sbi->devs->rwsem); 981 erofs_default_options(sbi); 982 fc->ops = &erofs_context_ops; 983 return 0; 984 } 985 986 static void erofs_drop_internal_inodes(struct erofs_sb_info *sbi) 987 { 988 iput(sbi->packed_inode); 989 sbi->packed_inode = NULL; 990 iput(sbi->metabox_inode); 991 sbi->metabox_inode = NULL; 992 iput(sbi->managed_cache); 993 sbi->managed_cache = NULL; 994 } 995 996 static void erofs_kill_sb(struct super_block *sb) 997 { 998 struct erofs_sb_info *sbi = EROFS_SB(sb); 999 1000 if (sbi->dif0.file) 1001 kill_anon_super(sb); 1002 else 1003 kill_block_super(sb); 1004 erofs_drop_internal_inodes(sbi); 1005 fs_put_dax(sbi->dif0.dax_dev, NULL); 1006 erofs_sb_free(sbi, sb); 1007 sb->s_fs_info = NULL; 1008 } 1009 1010 static void erofs_put_super(struct super_block *sb) 1011 { 1012 struct erofs_sb_info *const sbi = EROFS_SB(sb); 1013 1014 erofs_unregister_sysfs(sb); 1015 erofs_shrinker_unregister(sb); 1016 erofs_xattr_prefixes_cleanup(sb); 1017 erofs_drop_internal_inodes(sbi); 1018 erofs_free_dev_context(sbi->devs, sb); 1019 sbi->devs = NULL; 1020 } 1021 1022 static struct file_system_type erofs_fs_type = { 1023 .owner = THIS_MODULE, 1024 .name = "erofs", 1025 .init_fs_context = erofs_init_fs_context, 1026 .kill_sb = erofs_kill_sb, 1027 .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP, 1028 }; 1029 MODULE_ALIAS_FS("erofs"); 1030 1031 #ifdef CONFIG_EROFS_FS_PAGE_CACHE_SHARE 1032 static void erofs_free_anon_inode(struct inode *inode) 1033 { 1034 struct erofs_inode *vi = EROFS_I(inode); 1035 1036 kfree(vi->fingerprint.opaque); 1037 kmem_cache_free(erofs_inode_cachep, vi); 1038 } 1039 1040 static const struct super_operations erofs_anon_sops = { 1041 .alloc_inode = erofs_alloc_inode, 1042 .drop_inode = inode_just_drop, 1043 .free_inode = erofs_free_anon_inode, 1044 }; 1045 1046 static int erofs_anon_init_fs_context(struct fs_context *fc) 1047 { 1048 struct pseudo_fs_context *ctx; 1049 1050 ctx = init_pseudo(fc, EROFS_SUPER_MAGIC); 1051 if (!ctx) 1052 return -ENOMEM; 1053 ctx->ops = &erofs_anon_sops; 1054 return 0; 1055 } 1056 1057 struct file_system_type erofs_anon_fs_type = { 1058 .name = "pseudo_erofs", 1059 .init_fs_context = erofs_anon_init_fs_context, 1060 .kill_sb = kill_anon_super, 1061 }; 1062 #endif 1063 1064 static int __init erofs_module_init(void) 1065 { 1066 int err; 1067 1068 erofs_check_ondisk_layout_definitions(); 1069 1070 erofs_inode_cachep = kmem_cache_create("erofs_inode", 1071 sizeof(struct erofs_inode), 0, 1072 SLAB_RECLAIM_ACCOUNT | SLAB_ACCOUNT, 1073 erofs_inode_init_once); 1074 if (!erofs_inode_cachep) 1075 return -ENOMEM; 1076 1077 err = erofs_init_shrinker(); 1078 if (err) 1079 goto shrinker_err; 1080 1081 err = z_erofs_init_subsystem(); 1082 if (err) 1083 goto zip_err; 1084 1085 err = erofs_init_sysfs(); 1086 if (err) 1087 goto sysfs_err; 1088 1089 err = erofs_init_ishare(); 1090 if (err) 1091 goto ishare_err; 1092 1093 err = register_filesystem(&erofs_fs_type); 1094 if (err) 1095 goto fs_err; 1096 1097 return 0; 1098 1099 fs_err: 1100 erofs_exit_ishare(); 1101 ishare_err: 1102 erofs_exit_sysfs(); 1103 sysfs_err: 1104 z_erofs_exit_subsystem(); 1105 zip_err: 1106 erofs_exit_shrinker(); 1107 shrinker_err: 1108 kmem_cache_destroy(erofs_inode_cachep); 1109 return err; 1110 } 1111 1112 static void __exit erofs_module_exit(void) 1113 { 1114 unregister_filesystem(&erofs_fs_type); 1115 erofs_exit_ishare(); 1116 1117 /* ensure all delayed rcu free inodes & pclusters are flushed */ 1118 rcu_barrier(); 1119 1120 erofs_exit_sysfs(); 1121 z_erofs_exit_subsystem(); 1122 erofs_exit_shrinker(); 1123 kmem_cache_destroy(erofs_inode_cachep); 1124 } 1125 1126 static int erofs_statfs(struct dentry *dentry, struct kstatfs *buf) 1127 { 1128 struct super_block *sb = dentry->d_sb; 1129 struct erofs_sb_info *sbi = EROFS_SB(sb); 1130 1131 buf->f_type = sb->s_magic; 1132 buf->f_bsize = sb->s_blocksize; 1133 buf->f_blocks = sbi->total_blocks; 1134 buf->f_bfree = buf->f_bavail = 0; 1135 buf->f_files = ULLONG_MAX; 1136 buf->f_ffree = ULLONG_MAX - sbi->inos; 1137 buf->f_namelen = EROFS_NAME_LEN; 1138 1139 if (uuid_is_null(&sb->s_uuid)) 1140 buf->f_fsid = u64_to_fsid(!sb->s_bdev ? 0 : 1141 huge_encode_dev(sb->s_bdev->bd_dev)); 1142 else 1143 buf->f_fsid = uuid_to_fsid(sb->s_uuid.b); 1144 return 0; 1145 } 1146 1147 static int erofs_show_options(struct seq_file *seq, struct dentry *root) 1148 { 1149 struct erofs_sb_info *sbi = EROFS_SB(root->d_sb); 1150 struct erofs_mount_opts *opt = &sbi->opt; 1151 1152 if (IS_ENABLED(CONFIG_EROFS_FS_XATTR)) 1153 seq_puts(seq, test_opt(opt, XATTR_USER) ? 1154 ",user_xattr" : ",nouser_xattr"); 1155 if (IS_ENABLED(CONFIG_EROFS_FS_POSIX_ACL)) 1156 seq_puts(seq, test_opt(opt, POSIX_ACL) ? ",acl" : ",noacl"); 1157 if (IS_ENABLED(CONFIG_EROFS_FS_ZIP) && sbi->available_compr_algs) 1158 seq_printf(seq, ",cache_strategy=%s", 1159 erofs_param_cache_strategy[opt->cache_strategy].name); 1160 if (test_opt(opt, DAX_ALWAYS)) 1161 seq_puts(seq, ",dax=always"); 1162 if (test_opt(opt, DAX_NEVER)) 1163 seq_puts(seq, ",dax=never"); 1164 if (erofs_is_fileio_mode(sbi) && test_opt(opt, DIRECT_IO)) 1165 seq_puts(seq, ",directio"); 1166 if (sbi->dif0.fsoff) 1167 seq_printf(seq, ",fsoffset=%llu", sbi->dif0.fsoff); 1168 if (test_opt(opt, INODE_SHARE)) 1169 seq_puts(seq, ",inode_share"); 1170 return 0; 1171 } 1172 1173 static void erofs_evict_inode(struct inode *inode) 1174 { 1175 if (IS_DAX(inode)) 1176 dax_break_layout_final(inode); 1177 erofs_ishare_free_inode(inode); 1178 truncate_inode_pages_final(&inode->i_data); 1179 clear_inode(inode); 1180 } 1181 1182 const struct super_operations erofs_sops = { 1183 .put_super = erofs_put_super, 1184 .alloc_inode = erofs_alloc_inode, 1185 .free_inode = erofs_free_inode, 1186 .evict_inode = erofs_evict_inode, 1187 .statfs = erofs_statfs, 1188 .show_options = erofs_show_options, 1189 }; 1190 1191 module_init(erofs_module_init); 1192 module_exit(erofs_module_exit); 1193 1194 MODULE_DESCRIPTION("Enhanced ROM File System"); 1195 MODULE_AUTHOR("Gao Xiang, Chao Yu, Miao Xie, CONSUMER BG, HUAWEI Inc."); 1196 MODULE_LICENSE("GPL"); 1197