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, 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 {} 419 }; 420 421 static bool erofs_fc_set_dax_mode(struct fs_context *fc, unsigned int mode) 422 { 423 if (IS_ENABLED(CONFIG_FS_DAX)) { 424 struct erofs_sb_info *sbi = fc->s_fs_info; 425 426 if (mode == EROFS_MOUNT_DAX_ALWAYS) { 427 set_opt(&sbi->opt, DAX_ALWAYS); 428 clear_opt(&sbi->opt, DAX_NEVER); 429 return true; 430 } else if (mode == EROFS_MOUNT_DAX_NEVER) { 431 set_opt(&sbi->opt, DAX_NEVER); 432 clear_opt(&sbi->opt, DAX_ALWAYS); 433 return true; 434 } 435 DBG_BUGON(1); 436 return false; 437 } 438 errorfc(fc, "dax options not supported"); 439 return false; 440 } 441 442 static int erofs_fc_parse_source(struct fs_context *fc, 443 struct fs_parameter *param) 444 { 445 struct erofs_sb_info *sbi = fc->s_fs_info; 446 447 if (fc->source || sbi->dif0.file) 448 return invalf(fc, "Multiple sources"); 449 450 switch (param->type) { 451 case fs_value_is_string: 452 fc->source = param->string; 453 param->string = NULL; 454 return 0; 455 case fs_value_is_file: { 456 char *buf __free(kfree) = kmalloc(PATH_MAX, GFP_KERNEL); 457 char *p; 458 459 if (!buf) 460 return -ENOMEM; 461 p = file_path(param->file, buf, PATH_MAX); 462 if (IS_ERR(p)) 463 return PTR_ERR(p); 464 fc->source = kstrdup(p, GFP_KERNEL); 465 if (!fc->source) 466 return -ENOMEM; 467 sbi->dif0.file = no_free_ptr(param->file); 468 return 0; 469 } 470 default: 471 WARN_ON_ONCE(true); 472 return -EINVAL; 473 } 474 } 475 476 static int erofs_fc_parse_param(struct fs_context *fc, 477 struct fs_parameter *param) 478 { 479 struct erofs_sb_info *sbi = fc->s_fs_info; 480 struct fs_parse_result result; 481 struct erofs_device_info *dif; 482 int opt, ret; 483 484 opt = fs_parse(fc, erofs_fs_parameters, param, &result); 485 if (opt < 0) 486 return opt; 487 488 switch (opt) { 489 case Opt_user_xattr: 490 if (!IS_ENABLED(CONFIG_EROFS_FS_XATTR)) 491 errorfc(fc, "{,no}user_xattr options not supported"); 492 else if (result.boolean) 493 set_opt(&sbi->opt, XATTR_USER); 494 else 495 clear_opt(&sbi->opt, XATTR_USER); 496 break; 497 case Opt_acl: 498 if (!IS_ENABLED(CONFIG_EROFS_FS_POSIX_ACL)) 499 errorfc(fc, "{,no}acl options not supported"); 500 else if (result.boolean) 501 set_opt(&sbi->opt, POSIX_ACL); 502 else 503 clear_opt(&sbi->opt, POSIX_ACL); 504 break; 505 case Opt_cache_strategy: 506 if (!IS_ENABLED(CONFIG_EROFS_FS_ZIP)) 507 errorfc(fc, "compression not supported, cache_strategy ignored"); 508 else 509 sbi->opt.cache_strategy = result.uint_32; 510 break; 511 case Opt_dax: 512 if (!erofs_fc_set_dax_mode(fc, EROFS_MOUNT_DAX_ALWAYS)) 513 return -EINVAL; 514 break; 515 case Opt_dax_enum: 516 if (!erofs_fc_set_dax_mode(fc, result.uint_32)) 517 return -EINVAL; 518 break; 519 case Opt_device: 520 dif = kzalloc_obj(*dif); 521 if (!dif) 522 return -ENOMEM; 523 dif->path = kstrdup(param->string, GFP_KERNEL); 524 if (!dif->path) { 525 kfree(dif); 526 return -ENOMEM; 527 } 528 down_write(&sbi->devs->rwsem); 529 ret = idr_alloc(&sbi->devs->tree, dif, 0, 0, GFP_KERNEL); 530 up_write(&sbi->devs->rwsem); 531 if (ret < 0) { 532 kfree(dif->path); 533 kfree(dif); 534 return ret; 535 } 536 ++sbi->devs->extra_devices; 537 break; 538 case Opt_domain_id: 539 if (!IS_ENABLED(CONFIG_EROFS_FS_PAGE_CACHE_SHARE)) { 540 errorfc(fc, "%s option not supported", erofs_fs_parameters[opt].name); 541 } else { 542 kfree_sensitive(sbi->domain_id); 543 sbi->domain_id = no_free_ptr(param->string); 544 } 545 break; 546 case Opt_directio: 547 if (!IS_ENABLED(CONFIG_EROFS_FS_BACKED_BY_FILE)) 548 errorfc(fc, "%s option not supported", erofs_fs_parameters[opt].name); 549 else if (result.boolean) 550 set_opt(&sbi->opt, DIRECT_IO); 551 else 552 clear_opt(&sbi->opt, DIRECT_IO); 553 break; 554 case Opt_fsoffset: 555 sbi->dif0.fsoff = result.uint_64; 556 break; 557 case Opt_inode_share: 558 if (!IS_ENABLED(CONFIG_EROFS_FS_PAGE_CACHE_SHARE)) 559 errorfc(fc, "%s option not supported", erofs_fs_parameters[opt].name); 560 else 561 set_opt(&sbi->opt, INODE_SHARE); 562 break; 563 case Opt_source: 564 return erofs_fc_parse_source(fc, param); 565 } 566 return 0; 567 } 568 569 static int erofs_encode_fh(struct inode *inode, u32 *fh, int *max_len, 570 struct inode *parent) 571 { 572 erofs_nid_t nid = EROFS_I(inode)->nid; 573 int len = parent ? 6 : 3; 574 575 if (*max_len < len) { 576 *max_len = len; 577 return FILEID_INVALID; 578 } 579 580 fh[0] = (u32)(nid >> 32); 581 fh[1] = (u32)(nid & 0xffffffff); 582 fh[2] = inode->i_generation; 583 584 if (parent) { 585 nid = EROFS_I(parent)->nid; 586 587 fh[3] = (u32)(nid >> 32); 588 fh[4] = (u32)(nid & 0xffffffff); 589 fh[5] = parent->i_generation; 590 } 591 592 *max_len = len; 593 return parent ? FILEID_INO64_GEN_PARENT : FILEID_INO64_GEN; 594 } 595 596 static struct dentry *erofs_fh_to_dentry(struct super_block *sb, 597 struct fid *fid, int fh_len, int fh_type) 598 { 599 if ((fh_type != FILEID_INO64_GEN && 600 fh_type != FILEID_INO64_GEN_PARENT) || fh_len < 3) 601 return NULL; 602 603 return d_obtain_alias(erofs_iget(sb, 604 ((u64)fid->raw[0] << 32) | fid->raw[1])); 605 } 606 607 static struct dentry *erofs_fh_to_parent(struct super_block *sb, 608 struct fid *fid, int fh_len, int fh_type) 609 { 610 if (fh_type != FILEID_INO64_GEN_PARENT || fh_len < 6) 611 return NULL; 612 613 return d_obtain_alias(erofs_iget(sb, 614 ((u64)fid->raw[3] << 32) | fid->raw[4])); 615 } 616 617 static struct dentry *erofs_get_parent(struct dentry *child) 618 { 619 erofs_nid_t nid; 620 unsigned int d_type; 621 int err; 622 623 err = erofs_namei(d_inode(child), &dotdot_name, &nid, &d_type); 624 if (err) 625 return ERR_PTR(err); 626 return d_obtain_alias(erofs_iget(child->d_sb, nid)); 627 } 628 629 static const struct export_operations erofs_export_ops = { 630 .encode_fh = erofs_encode_fh, 631 .fh_to_dentry = erofs_fh_to_dentry, 632 .fh_to_parent = erofs_fh_to_parent, 633 .get_parent = erofs_get_parent, 634 }; 635 636 int erofs_setup_managed_cache(struct super_block *sb) 637 { 638 if (!EROFS_SB(sb)->managed_cache) { 639 struct inode *inode = new_inode(sb); 640 641 if (!inode) 642 return -ENOMEM; 643 set_nlink(inode, 1); 644 inode->i_size = OFFSET_MAX; 645 mapping_set_gfp_mask(inode->i_mapping, GFP_KERNEL); 646 EROFS_SB(sb)->managed_cache = inode; 647 } 648 return 0; 649 } 650 651 static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc) 652 { 653 struct inode *inode; 654 struct erofs_sb_info *sbi = EROFS_SB(sb); 655 int err; 656 657 sb->s_magic = EROFS_SUPER_MAGIC; 658 sb->s_flags |= SB_RDONLY | SB_NOATIME; 659 sb->s_maxbytes = MAX_LFS_FILESIZE; 660 sb->s_op = &erofs_sops; 661 662 if (!sbi->domain_id && test_opt(&sbi->opt, INODE_SHARE)) { 663 errorfc(fc, "domain_id is needed when inode_share is on"); 664 return -EINVAL; 665 } 666 if (test_opt(&sbi->opt, DAX_ALWAYS) && test_opt(&sbi->opt, INODE_SHARE)) { 667 errorfc(fc, "FSDAX is not allowed when inode_share is on"); 668 return -EINVAL; 669 } 670 671 sbi->blkszbits = PAGE_SHIFT; 672 if (erofs_is_fileio_mode(sbi)) { 673 /* 674 * (File-backed mounts) EROFS claims it's safe to nest other 675 * fs contexts (including its own) due to self-controlled RO 676 * accesses/contexts and no side-effect changes that need to 677 * context save & restore so it can reuse the current thread 678 * context. 679 * However, we still need to prevent kernel stack overflow due 680 * to filesystem nesting: just ensure that s_stack_depth is 0 681 * to disallow mounting EROFS on stacked filesystems. 682 * Note: s_stack_depth is not incremented here for now, since 683 * EROFS is the only fs supporting file-backed mounts for now. 684 * It MUST change if another fs plans to support them, which 685 * may also require adjusting FILESYSTEM_MAX_STACK_DEPTH. 686 */ 687 inode = file_inode(sbi->dif0.file); 688 if ((inode->i_sb->s_op == &erofs_sops && 689 !inode->i_sb->s_bdev) || inode->i_sb->s_stack_depth) { 690 erofs_err(sb, "file-backed mounts cannot be applied to stacked fses"); 691 return -ENOTBLK; 692 } 693 sb->s_blocksize = PAGE_SIZE; 694 sb->s_blocksize_bits = PAGE_SHIFT; 695 696 err = super_setup_bdi(sb); 697 if (err) 698 return err; 699 err = erofs_setup_managed_cache(sb); 700 if (err) 701 return err; 702 703 snprintf(sb->s_id, sizeof(sb->s_id), 704 "%u:%u", MAJOR(sb->s_dev), MINOR(sb->s_dev)); 705 } else { 706 if (!sb_set_blocksize(sb, PAGE_SIZE)) { 707 errorfc(fc, "failed to set initial blksize"); 708 return -EINVAL; 709 } 710 sbi->dif0.dax_dev = fs_dax_get_by_bdev(sb->s_bdev, 711 &sbi->dif0.dax_part_off, NULL, NULL); 712 } 713 714 err = erofs_read_superblock(sb); 715 if (err) 716 return err; 717 718 if (sb->s_blocksize_bits != sbi->blkszbits) { 719 if (erofs_is_fileio_mode(sbi)) { 720 sb->s_blocksize = 1 << sbi->blkszbits; 721 sb->s_blocksize_bits = sbi->blkszbits; 722 } else if (!sb_set_blocksize(sb, 1 << sbi->blkszbits)) { 723 errorfc(fc, "failed to set erofs blksize"); 724 return -EINVAL; 725 } 726 } 727 if (sbi->dif0.fsoff & (sb->s_blocksize - 1)) 728 return invalfc(fc, "fsoffset %llu is not aligned to block size %lu", 729 sbi->dif0.fsoff, sb->s_blocksize); 730 731 if (test_opt(&sbi->opt, DAX_ALWAYS) && sbi->blkszbits != PAGE_SHIFT) { 732 erofs_info(sb, "unsupported blocksize for DAX"); 733 clear_opt(&sbi->opt, DAX_ALWAYS); 734 } 735 if (test_opt(&sbi->opt, INODE_SHARE) && !erofs_sb_has_ishare_xattrs(sbi)) { 736 erofs_info(sb, "on-disk ishare xattrs not found. Turning off inode_share."); 737 clear_opt(&sbi->opt, INODE_SHARE); 738 } 739 if (test_opt(&sbi->opt, INODE_SHARE)) 740 erofs_info(sb, "EXPERIMENTAL EROFS page cache share support in use. Use at your own risk!"); 741 742 sb->s_time_gran = 1; 743 sb->s_xattr = erofs_xattr_handlers; 744 sb->s_export_op = &erofs_export_ops; 745 746 if (test_opt(&sbi->opt, POSIX_ACL)) 747 sb->s_flags |= SB_POSIXACL; 748 else 749 sb->s_flags &= ~SB_POSIXACL; 750 751 err = z_erofs_init_super(sb); 752 if (err) 753 return err; 754 755 if (erofs_sb_has_fragments(sbi) && sbi->packed_nid) { 756 inode = erofs_iget(sb, sbi->packed_nid); 757 if (IS_ERR(inode)) 758 return PTR_ERR(inode); 759 sbi->packed_inode = inode; 760 } 761 if (erofs_sb_has_metabox(sbi)) { 762 inode = erofs_iget(sb, sbi->metabox_nid); 763 if (IS_ERR(inode)) 764 return PTR_ERR(inode); 765 sbi->metabox_inode = inode; 766 } 767 768 inode = erofs_iget(sb, sbi->root_nid); 769 if (IS_ERR(inode)) 770 return PTR_ERR(inode); 771 772 if (!S_ISDIR(inode->i_mode)) { 773 erofs_err(sb, "rootino(nid %llu) is not a directory(i_mode %o)", 774 sbi->root_nid, inode->i_mode); 775 iput(inode); 776 return -EINVAL; 777 } 778 sb->s_root = d_make_root(inode); 779 if (!sb->s_root) 780 return -ENOMEM; 781 782 erofs_shrinker_register(sb); 783 err = erofs_xattr_prefixes_init(sb); 784 if (err) 785 return err; 786 787 super_set_sysfs_name_id(sb); 788 err = erofs_register_sysfs(sb); 789 if (err) 790 return err; 791 792 sbi->dir_ra_bytes = EROFS_DIR_RA_BYTES; 793 erofs_info(sb, "mounted with root inode @ nid %llu.", sbi->root_nid); 794 return 0; 795 } 796 797 static int erofs_fc_get_tree(struct fs_context *fc) 798 { 799 struct erofs_sb_info *sbi = fc->s_fs_info; 800 int ret; 801 802 if (sbi->dif0.file) { 803 if (!IS_ENABLED(CONFIG_EROFS_FS_BACKED_BY_FILE)) { 804 errorfc(fc, "source fd option not supported"); 805 return -EINVAL; 806 } 807 if (!S_ISREG(file_inode(sbi->dif0.file)->i_mode) || 808 !sbi->dif0.file->f_mapping->a_ops->read_folio) { 809 errorfc(fc, "source is unsupported"); 810 return -EINVAL; 811 } 812 return get_tree_nodev(fc, erofs_fc_fill_super); 813 } 814 815 ret = get_tree_bdev_flags(fc, erofs_fc_fill_super, 816 IS_ENABLED(CONFIG_EROFS_FS_BACKED_BY_FILE) ? 817 GET_TREE_BDEV_QUIET_LOOKUP : 0); 818 if (IS_ENABLED(CONFIG_EROFS_FS_BACKED_BY_FILE) && ret == -ENOTBLK) { 819 struct file *file; 820 821 if (!fc->source) 822 return invalf(fc, "No source specified"); 823 file = filp_open(fc->source, O_RDONLY | O_LARGEFILE, 0); 824 if (IS_ERR(file)) 825 return PTR_ERR(file); 826 sbi->dif0.file = file; 827 828 if (S_ISREG(file_inode(sbi->dif0.file)->i_mode) && 829 sbi->dif0.file->f_mapping->a_ops->read_folio) 830 return get_tree_nodev(fc, erofs_fc_fill_super); 831 } 832 return ret; 833 } 834 835 static int erofs_fc_reconfigure(struct fs_context *fc) 836 { 837 struct super_block *sb = fc->root->d_sb; 838 struct erofs_sb_info *sbi = EROFS_SB(sb); 839 struct erofs_sb_info *new_sbi = fc->s_fs_info; 840 841 DBG_BUGON(!sb_rdonly(sb)); 842 843 if (new_sbi->domain_id) 844 erofs_info(sb, "ignoring reconfiguration for domain_id."); 845 846 if (test_opt(&new_sbi->opt, POSIX_ACL)) 847 fc->sb_flags |= SB_POSIXACL; 848 else 849 fc->sb_flags &= ~SB_POSIXACL; 850 851 sbi->opt = new_sbi->opt; 852 853 fc->sb_flags |= SB_RDONLY; 854 return 0; 855 } 856 857 static int erofs_release_device_info(int id, void *ptr, void *data) 858 { 859 struct super_block *sb = data; 860 struct erofs_device_info *dif = ptr; 861 862 fs_put_dax(dif->dax_dev, NULL); 863 if (dif->file) { 864 if (S_ISBLK(file_inode(dif->file)->i_mode)) 865 fs_bdev_file_release(dif->file, sb); 866 else 867 fput(dif->file); 868 } 869 kfree(dif->path); 870 kfree(dif); 871 return 0; 872 } 873 874 static void erofs_free_dev_context(struct erofs_dev_context *devs, 875 struct super_block *sb) 876 { 877 if (!devs) 878 return; 879 idr_for_each(&devs->tree, &erofs_release_device_info, sb); 880 idr_destroy(&devs->tree); 881 kfree(devs); 882 } 883 884 static void erofs_sb_free(struct erofs_sb_info *sbi, struct super_block *sb) 885 { 886 erofs_free_dev_context(sbi->devs, sb); 887 kfree_sensitive(sbi->domain_id); 888 if (sbi->dif0.file) 889 fput(sbi->dif0.file); 890 kfree(sbi->volume_name); 891 kfree(sbi); 892 } 893 894 static void erofs_fc_free(struct fs_context *fc) 895 { 896 struct erofs_sb_info *sbi = fc->s_fs_info; 897 898 /* 899 * Freed here only if an error occurs before the sb is set up; at that 900 * point no block-backed device has been claimed (that happens in 901 * fill_super), so the NULL sb never reaches fs_bdev_file_release(). 902 */ 903 if (sbi) 904 erofs_sb_free(sbi, NULL); 905 } 906 907 static const struct fs_context_operations erofs_context_ops = { 908 .parse_param = erofs_fc_parse_param, 909 .get_tree = erofs_fc_get_tree, 910 .reconfigure = erofs_fc_reconfigure, 911 .free = erofs_fc_free, 912 }; 913 914 static int erofs_init_fs_context(struct fs_context *fc) 915 { 916 struct erofs_sb_info *sbi; 917 918 sbi = kzalloc_obj(*sbi); 919 if (!sbi) 920 return -ENOMEM; 921 922 sbi->devs = kzalloc_obj(struct erofs_dev_context); 923 if (!sbi->devs) { 924 kfree(sbi); 925 return -ENOMEM; 926 } 927 fc->s_fs_info = sbi; 928 929 idr_init(&sbi->devs->tree); 930 init_rwsem(&sbi->devs->rwsem); 931 erofs_default_options(sbi); 932 fc->ops = &erofs_context_ops; 933 return 0; 934 } 935 936 static void erofs_drop_internal_inodes(struct erofs_sb_info *sbi) 937 { 938 iput(sbi->packed_inode); 939 sbi->packed_inode = NULL; 940 iput(sbi->metabox_inode); 941 sbi->metabox_inode = NULL; 942 iput(sbi->managed_cache); 943 sbi->managed_cache = NULL; 944 } 945 946 static void erofs_kill_sb(struct super_block *sb) 947 { 948 struct erofs_sb_info *sbi = EROFS_SB(sb); 949 950 if (sbi->dif0.file) 951 kill_anon_super(sb); 952 else 953 kill_block_super(sb); 954 erofs_drop_internal_inodes(sbi); 955 fs_put_dax(sbi->dif0.dax_dev, NULL); 956 erofs_sb_free(sbi, sb); 957 sb->s_fs_info = NULL; 958 } 959 960 static void erofs_put_super(struct super_block *sb) 961 { 962 struct erofs_sb_info *const sbi = EROFS_SB(sb); 963 964 erofs_unregister_sysfs(sb); 965 erofs_shrinker_unregister(sb); 966 erofs_xattr_prefixes_cleanup(sb); 967 erofs_drop_internal_inodes(sbi); 968 erofs_free_dev_context(sbi->devs, sb); 969 sbi->devs = NULL; 970 } 971 972 static struct file_system_type erofs_fs_type = { 973 .owner = THIS_MODULE, 974 .name = "erofs", 975 .init_fs_context = erofs_init_fs_context, 976 .kill_sb = erofs_kill_sb, 977 .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP, 978 }; 979 MODULE_ALIAS_FS("erofs"); 980 981 #ifdef CONFIG_EROFS_FS_PAGE_CACHE_SHARE 982 static void erofs_free_anon_inode(struct inode *inode) 983 { 984 struct erofs_inode *vi = EROFS_I(inode); 985 986 kfree(vi->fingerprint.opaque); 987 kmem_cache_free(erofs_inode_cachep, vi); 988 } 989 990 static const struct super_operations erofs_anon_sops = { 991 .alloc_inode = erofs_alloc_inode, 992 .drop_inode = inode_just_drop, 993 .free_inode = erofs_free_anon_inode, 994 }; 995 996 static int erofs_anon_init_fs_context(struct fs_context *fc) 997 { 998 struct pseudo_fs_context *ctx; 999 1000 ctx = init_pseudo(fc, EROFS_SUPER_MAGIC); 1001 if (!ctx) 1002 return -ENOMEM; 1003 ctx->ops = &erofs_anon_sops; 1004 return 0; 1005 } 1006 1007 struct file_system_type erofs_anon_fs_type = { 1008 .name = "pseudo_erofs", 1009 .init_fs_context = erofs_anon_init_fs_context, 1010 .kill_sb = kill_anon_super, 1011 }; 1012 #endif 1013 1014 static int __init erofs_module_init(void) 1015 { 1016 int err; 1017 1018 erofs_check_ondisk_layout_definitions(); 1019 1020 erofs_inode_cachep = kmem_cache_create("erofs_inode", 1021 sizeof(struct erofs_inode), 0, 1022 SLAB_RECLAIM_ACCOUNT | SLAB_ACCOUNT, 1023 erofs_inode_init_once); 1024 if (!erofs_inode_cachep) 1025 return -ENOMEM; 1026 1027 err = erofs_init_shrinker(); 1028 if (err) 1029 goto shrinker_err; 1030 1031 err = z_erofs_init_subsystem(); 1032 if (err) 1033 goto zip_err; 1034 1035 err = erofs_init_sysfs(); 1036 if (err) 1037 goto sysfs_err; 1038 1039 err = erofs_init_ishare(); 1040 if (err) 1041 goto ishare_err; 1042 1043 err = register_filesystem(&erofs_fs_type); 1044 if (err) 1045 goto fs_err; 1046 1047 return 0; 1048 1049 fs_err: 1050 erofs_exit_ishare(); 1051 ishare_err: 1052 erofs_exit_sysfs(); 1053 sysfs_err: 1054 z_erofs_exit_subsystem(); 1055 zip_err: 1056 erofs_exit_shrinker(); 1057 shrinker_err: 1058 kmem_cache_destroy(erofs_inode_cachep); 1059 return err; 1060 } 1061 1062 static void __exit erofs_module_exit(void) 1063 { 1064 unregister_filesystem(&erofs_fs_type); 1065 erofs_exit_ishare(); 1066 1067 /* ensure all delayed rcu free inodes & pclusters are flushed */ 1068 rcu_barrier(); 1069 1070 erofs_exit_sysfs(); 1071 z_erofs_exit_subsystem(); 1072 erofs_exit_shrinker(); 1073 kmem_cache_destroy(erofs_inode_cachep); 1074 } 1075 1076 static int erofs_statfs(struct dentry *dentry, struct kstatfs *buf) 1077 { 1078 struct super_block *sb = dentry->d_sb; 1079 struct erofs_sb_info *sbi = EROFS_SB(sb); 1080 1081 buf->f_type = sb->s_magic; 1082 buf->f_bsize = sb->s_blocksize; 1083 buf->f_blocks = sbi->total_blocks; 1084 buf->f_bfree = buf->f_bavail = 0; 1085 buf->f_files = ULLONG_MAX; 1086 buf->f_ffree = ULLONG_MAX - sbi->inos; 1087 buf->f_namelen = EROFS_NAME_LEN; 1088 1089 if (uuid_is_null(&sb->s_uuid)) 1090 buf->f_fsid = u64_to_fsid(!sb->s_bdev ? 0 : 1091 huge_encode_dev(sb->s_bdev->bd_dev)); 1092 else 1093 buf->f_fsid = uuid_to_fsid(sb->s_uuid.b); 1094 return 0; 1095 } 1096 1097 static int erofs_show_options(struct seq_file *seq, struct dentry *root) 1098 { 1099 struct erofs_sb_info *sbi = EROFS_SB(root->d_sb); 1100 struct erofs_mount_opts *opt = &sbi->opt; 1101 1102 if (IS_ENABLED(CONFIG_EROFS_FS_XATTR)) 1103 seq_puts(seq, test_opt(opt, XATTR_USER) ? 1104 ",user_xattr" : ",nouser_xattr"); 1105 if (IS_ENABLED(CONFIG_EROFS_FS_POSIX_ACL)) 1106 seq_puts(seq, test_opt(opt, POSIX_ACL) ? ",acl" : ",noacl"); 1107 if (IS_ENABLED(CONFIG_EROFS_FS_ZIP) && sbi->available_compr_algs) 1108 seq_printf(seq, ",cache_strategy=%s", 1109 erofs_param_cache_strategy[opt->cache_strategy].name); 1110 if (test_opt(opt, DAX_ALWAYS)) 1111 seq_puts(seq, ",dax=always"); 1112 if (test_opt(opt, DAX_NEVER)) 1113 seq_puts(seq, ",dax=never"); 1114 if (erofs_is_fileio_mode(sbi) && test_opt(opt, DIRECT_IO)) 1115 seq_puts(seq, ",directio"); 1116 if (sbi->dif0.fsoff) 1117 seq_printf(seq, ",fsoffset=%llu", sbi->dif0.fsoff); 1118 if (test_opt(opt, INODE_SHARE)) 1119 seq_puts(seq, ",inode_share"); 1120 return 0; 1121 } 1122 1123 static void erofs_evict_inode(struct inode *inode) 1124 { 1125 if (IS_DAX(inode)) 1126 dax_break_layout_final(inode); 1127 erofs_ishare_free_inode(inode); 1128 truncate_inode_pages_final(&inode->i_data); 1129 clear_inode(inode); 1130 } 1131 1132 const struct super_operations erofs_sops = { 1133 .put_super = erofs_put_super, 1134 .alloc_inode = erofs_alloc_inode, 1135 .free_inode = erofs_free_inode, 1136 .evict_inode = erofs_evict_inode, 1137 .statfs = erofs_statfs, 1138 .show_options = erofs_show_options, 1139 }; 1140 1141 module_init(erofs_module_init); 1142 module_exit(erofs_module_exit); 1143 1144 MODULE_DESCRIPTION("Enhanced ROM File System"); 1145 MODULE_AUTHOR("Gao Xiang, Chao Yu, Miao Xie, CONSUMER BG, HUAWEI Inc."); 1146 MODULE_LICENSE("GPL"); 1147