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