1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
|
// SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0
/*
* Created by Li Guifu <blucerlee@gmail.com>
*/
#include <string.h>
#include <stdlib.h>
#include "erofs/print.h"
#include "erofs/xattr.h"
#include "liberofs_cache.h"
#include "liberofs_compress.h"
#include "liberofs_metabox.h"
static bool check_layout_compatibility(struct erofs_sb_info *sbi,
struct erofs_super_block *dsb)
{
const unsigned int feature = le32_to_cpu(dsb->feature_incompat);
sbi->feature_incompat = feature;
/* check if current kernel meets all mandatory requirements */
if (feature & ~EROFS_ALL_FEATURE_INCOMPAT) {
erofs_err("unidentified incompatible feature %x, please upgrade kernel version",
feature & ~EROFS_ALL_FEATURE_INCOMPAT);
return false;
}
return true;
}
static int erofs_init_devices(struct erofs_sb_info *sbi,
struct erofs_super_block *dsb)
{
unsigned int ondisk_extradevs, i;
erofs_off_t pos;
sbi->total_blocks = sbi->primarydevice_blocks;
if (!erofs_sb_has_device_table(sbi))
ondisk_extradevs = 0;
else
ondisk_extradevs = le16_to_cpu(dsb->extra_devices);
if (sbi->extra_devices &&
ondisk_extradevs != sbi->extra_devices) {
erofs_err("extra devices don't match (ondisk %u, given %u)",
ondisk_extradevs, sbi->extra_devices);
return -EINVAL;
}
if (!ondisk_extradevs)
return 0;
sbi->extra_devices = ondisk_extradevs;
sbi->device_id_mask = roundup_pow_of_two(ondisk_extradevs + 1) - 1;
sbi->devs = calloc(ondisk_extradevs, sizeof(*sbi->devs));
if (!sbi->devs)
return -ENOMEM;
pos = le16_to_cpu(dsb->devt_slotoff) * EROFS_DEVT_SLOT_SIZE;
for (i = 0; i < ondisk_extradevs; ++i) {
struct erofs_deviceslot dis;
int ret;
ret = erofs_dev_read(sbi, 0, &dis, pos, sizeof(dis));
if (ret < 0) {
free(sbi->devs);
sbi->devs = NULL;
return ret;
}
sbi->devs[i].uniaddr = le32_to_cpu(dis.uniaddr_lo);
sbi->devs[i].blocks = le32_to_cpu(dis.blocks_lo);
memcpy(sbi->devs[i].tag, dis.tag, sizeof(dis.tag));
sbi->total_blocks += sbi->devs[i].blocks;
pos += EROFS_DEVT_SLOT_SIZE;
}
return 0;
}
int erofs_read_superblock(struct erofs_sb_info *sbi)
{
u8 data[EROFS_MAX_BLOCK_SIZE];
struct erofs_super_block *dsb;
int read, ret;
if (sbi->sb_valid)
return 0;
read = erofs_io_pread(&sbi->bdev, data, EROFS_MAX_BLOCK_SIZE, 0);
if (read < EROFS_SUPER_OFFSET + sizeof(*dsb)) {
ret = read < 0 ? read : -EIO;
erofs_err("cannot read erofs superblock: %s",
erofs_strerror(ret));
return ret;
}
dsb = (struct erofs_super_block *)(data + EROFS_SUPER_OFFSET);
ret = -EINVAL;
if (le32_to_cpu(dsb->magic) != EROFS_SUPER_MAGIC_V1) {
erofs_err("cannot find valid erofs superblock");
return ret;
}
sbi->feature_compat = le32_to_cpu(dsb->feature_compat);
sbi->blkszbits = dsb->blkszbits;
if (sbi->blkszbits < 9 ||
sbi->blkszbits > ilog2(EROFS_MAX_BLOCK_SIZE)) {
erofs_err("blksize %llu isn't supported on this platform",
erofs_blksiz(sbi) | 0ULL);
return ret;
} else if (!check_layout_compatibility(sbi, dsb)) {
return ret;
}
sbi->sb_size = 128 + dsb->sb_extslots * EROFS_SB_EXTSLOT_SIZE;
if (sbi->sb_size > read - EROFS_SUPER_OFFSET) {
erofs_err("invalid sb_extslots %u", dsb->sb_extslots);
return -EINVAL;
}
sbi->primarydevice_blocks = le32_to_cpu(dsb->blocks_lo);
sbi->meta_blkaddr = le32_to_cpu(dsb->meta_blkaddr);
sbi->xattr_blkaddr = le32_to_cpu(dsb->xattr_blkaddr);
sbi->xattr_prefix_start = le32_to_cpu(dsb->xattr_prefix_start);
sbi->xattr_prefix_count = dsb->xattr_prefix_count;
if (erofs_sb_has_48bit(sbi) && dsb->rootnid_8b) {
sbi->root_nid = le64_to_cpu(dsb->rootnid_8b);
sbi->primarydevice_blocks = (sbi->primarydevice_blocks << 32) |
le16_to_cpu(dsb->rb.blocks_hi);
} else {
sbi->root_nid = le16_to_cpu(dsb->rb.rootnid_2b);
}
sbi->packed_nid = le64_to_cpu(dsb->packed_nid);
if (erofs_sb_has_metabox(sbi)) {
if (sbi->sb_size <= offsetof(struct erofs_super_block,
metabox_nid))
return -EFSCORRUPTED;
sbi->metabox_nid = le64_to_cpu(dsb->metabox_nid);
if (sbi->metabox_nid & BIT_ULL(EROFS_DIRENT_NID_METABOX_BIT))
return -EFSCORRUPTED; /* self-loop detection */
} else {
sbi->metabox_nid = 0;
}
sbi->inos = le64_to_cpu(dsb->inos);
sbi->checksum = le32_to_cpu(dsb->checksum);
sbi->epoch = (s64)le64_to_cpu(dsb->epoch);
sbi->fixed_nsec = le32_to_cpu(dsb->fixed_nsec);
sbi->build_time = le32_to_cpu(dsb->build_time);
memcpy(&sbi->uuid, dsb->uuid, sizeof(dsb->uuid));
if (erofs_sb_has_ishare_xattrs(sbi)) {
if (dsb->ishare_xattr_prefix_id >= sbi->xattr_prefix_count) {
erofs_err("invalid ishare xattr prefix id %d",
dsb->ishare_xattr_prefix_id);
return -EFSCORRUPTED;
}
sbi->ishare_xattr_prefix_id =
dsb->ishare_xattr_prefix_id | EROFS_XATTR_LONG_PREFIX;
}
ret = z_erofs_parse_cfgs(sbi, dsb);
if (ret)
return ret;
ret = erofs_init_devices(sbi, dsb);
if (ret)
return ret;
ret = erofs_xattr_prefixes_init(sbi);
if (ret && sbi->devs) {
free(sbi->devs);
sbi->devs = NULL;
}
sbi->sb_valid = !ret;
if (erofs_sb_has_48bit(sbi))
erofs_info("EXPERIMENTAL 48-bit layout support in use. Use at your own risk!");
if (erofs_sb_has_metabox(sbi)) {
erofs_info("EXPERIMENTAL metadata compression support in use. Use at your own risk!");
erofs_info("No in-memory cache for metadata compression: userspace parser for metabox remains slow.");
}
return ret;
}
void erofs_put_super(struct erofs_sb_info *sbi)
{
if (sbi->devs) {
int i;
DBG_BUGON(!sbi->extra_devices);
for (i = 0; i < sbi->extra_devices; ++i)
free(sbi->devs[i].src_path);
free(sbi->devs);
sbi->devs = NULL;
}
if (sbi->bmgr) {
erofs_buffer_exit(sbi->bmgr);
sbi->bmgr = NULL;
}
z_erofs_compress_exit(sbi);
erofs_xattr_exit(sbi);
sbi->sb_valid = false;
}
int erofs_writesb(struct erofs_sb_info *sbi)
{
struct erofs_buffer_head *sb_bh = sbi->bh_sb;
struct erofs_super_block sb = {
.magic = cpu_to_le32(EROFS_SUPER_MAGIC_V1),
.blkszbits = sbi->blkszbits,
.rb.rootnid_2b = cpu_to_le16(sbi->root_nid),
.inos = cpu_to_le64(sbi->inos),
.epoch = cpu_to_le64(sbi->epoch),
.build_time = cpu_to_le64(sbi->build_time),
.fixed_nsec = cpu_to_le32(sbi->fixed_nsec),
.meta_blkaddr = cpu_to_le32(sbi->meta_blkaddr),
.xattr_blkaddr = cpu_to_le32(sbi->xattr_blkaddr),
.xattr_prefix_count = sbi->xattr_prefix_count,
.xattr_prefix_start = cpu_to_le32(sbi->xattr_prefix_start),
.feature_incompat = cpu_to_le32(sbi->feature_incompat),
.feature_compat = cpu_to_le32(sbi->feature_compat &
~EROFS_FEATURE_COMPAT_SB_CHKSUM),
.extra_devices = cpu_to_le16(sbi->extra_devices),
.devt_slotoff = cpu_to_le16(sbi->devt_slotoff),
.packed_nid = cpu_to_le64(sbi->packed_nid),
.ishare_xattr_prefix_id = sbi->ishare_xattr_prefix_id &
EROFS_XATTR_LONG_PREFIX_MASK,
};
char *buf;
int ret;
sb.blocks_lo = cpu_to_le32(sbi->primarydevice_blocks);
if (sbi->primarydevice_blocks > UINT32_MAX ||
sbi->root_nid > UINT16_MAX) {
sb.rb.blocks_hi = cpu_to_le16(sbi->primarydevice_blocks >> 32);
sb.rootnid_8b = cpu_to_le64(sbi->root_nid);
}
memcpy(sb.uuid, sbi->uuid, sizeof(sb.uuid));
memcpy(sb.volume_name, sbi->volume_name, sizeof(sb.volume_name));
if (erofs_sb_has_compr_cfgs(sbi))
sb.u1.available_compr_algs = cpu_to_le16(sbi->available_compr_algs);
else
sb.u1.lz4_max_distance = cpu_to_le16(sbi->lz4.max_distance);
if (erofs_sb_has_metabox(sbi))
sb.metabox_nid = cpu_to_le64(sbi->metabox_nid);
sb.sb_extslots = (sbi->sb_size - 128) >> 4;
buf = calloc(round_up(EROFS_SUPER_OFFSET + sbi->sb_size,
erofs_blksiz(sbi)), 1);
if (!buf) {
erofs_err("failed to allocate memory for sb: %s",
erofs_strerror(-errno));
return -ENOMEM;
}
memcpy(buf + EROFS_SUPER_OFFSET, &sb, sbi->sb_size);
ret = erofs_dev_write(sbi, buf, sb_bh ? erofs_btell(sb_bh, false) : 0,
EROFS_SUPER_OFFSET + sbi->sb_size);
free(buf);
if (sb_bh)
erofs_bdrop(sb_bh, false);
return ret;
}
struct erofs_buffer_head *erofs_reserve_sb(struct erofs_bufmgr *bmgr)
{
struct erofs_sb_info *sbi = bmgr->sbi;
struct erofs_buffer_head *bh;
unsigned int sb_size = 128;
int err;
if (erofs_sb_has_metabox(sbi) &&
sb_size <= offsetof(struct erofs_super_block, metabox_nid))
sb_size = offsetof(struct erofs_super_block, metabox_nid) + 8;
sbi->sb_size = round_up(sb_size, 16);
bh = erofs_balloc(bmgr, META, 0, 0);
if (IS_ERR(bh)) {
erofs_err("failed to allocate super: %s",
erofs_strerror(PTR_ERR(bh)));
return bh;
}
bh->op = &erofs_skip_write_bhops;
err = erofs_bh_balloon(bh, EROFS_SUPER_OFFSET + sbi->sb_size);
if (err < 0) {
erofs_err("failed to balloon super: %s", erofs_strerror(err));
goto err_bdrop;
}
/* make sure that the super block should be the very first blocks */
(void)erofs_mapbh(NULL, bh->block);
if (erofs_btell(bh, false) != 0) {
erofs_err("failed to pin super block @ 0");
err = -EFAULT;
goto err_bdrop;
}
return bh;
err_bdrop:
erofs_bdrop(bh, true);
return ERR_PTR(err);
}
int erofs_enable_sb_chksum(struct erofs_sb_info *sbi, u32 *crc)
{
int ret;
u8 buf[EROFS_MAX_BLOCK_SIZE];
unsigned int len;
struct erofs_super_block *sb;
/*
* skip the first 1024 bytes, to allow for the installation
* of x86 boot sectors and other oddities.
*/
if (erofs_blksiz(sbi) > EROFS_SUPER_OFFSET)
len = erofs_blksiz(sbi) - EROFS_SUPER_OFFSET;
else
len = erofs_blksiz(sbi);
ret = erofs_dev_read(sbi, 0, buf, EROFS_SUPER_OFFSET, len);
if (ret) {
erofs_err("failed to read superblock to set checksum: %s",
erofs_strerror(ret));
return ret;
}
sb = (struct erofs_super_block *)buf;
if (le32_to_cpu(sb->magic) != EROFS_SUPER_MAGIC_V1) {
erofs_err("internal error: not an erofs valid image");
return -EFAULT;
}
/* turn on checksum feature */
sb->feature_compat = cpu_to_le32(le32_to_cpu(sb->feature_compat) |
EROFS_FEATURE_COMPAT_SB_CHKSUM);
*crc = erofs_crc32c(~0, (u8 *)sb, len);
/* set up checksum field to erofs_super_block */
sb->checksum = cpu_to_le32(*crc);
ret = erofs_dev_write(sbi, buf, EROFS_SUPER_OFFSET, len);
if (ret) {
erofs_err("failed to write checksummed superblock: %s",
erofs_strerror(ret));
return ret;
}
return 0;
}
int erofs_superblock_csum_verify(struct erofs_sb_info *sbi)
{
u32 len = erofs_blksiz(sbi), crc;
u8 buf[EROFS_MAX_BLOCK_SIZE];
struct erofs_super_block *sb;
int ret;
if (len > EROFS_SUPER_OFFSET)
len -= EROFS_SUPER_OFFSET;
ret = erofs_dev_read(sbi, 0, buf, EROFS_SUPER_OFFSET, len);
if (ret) {
erofs_err("failed to read superblock to calculate sbcsum: %d",
ret);
return -1;
}
sb = (struct erofs_super_block *)buf;
sb->checksum = 0;
crc = erofs_crc32c(~0, (u8 *)sb, len);
if (crc == sbi->checksum)
return 0;
erofs_err("invalid checksum 0x%08x, 0x%08x expected",
sbi->checksum, crc);
return -EBADMSG;
}
int erofs_mkfs_init_devices(struct erofs_sb_info *sbi, unsigned int devices)
{
struct erofs_buffer_head *bh;
if (!devices)
return 0;
sbi->devs = calloc(devices, sizeof(sbi->devs[0]));
if (!sbi->devs)
return -ENOMEM;
bh = erofs_balloc(sbi->bmgr, DEVT,
sizeof(struct erofs_deviceslot) * devices, 0);
if (IS_ERR(bh)) {
free(sbi->devs);
sbi->devs = NULL;
return PTR_ERR(bh);
}
erofs_mapbh(NULL, bh->block);
bh->op = &erofs_skip_write_bhops;
sbi->bh_devt = bh;
sbi->devt_slotoff = erofs_btell(bh, false) / EROFS_DEVT_SLOT_SIZE;
sbi->extra_devices = devices;
erofs_sb_set_device_table(sbi);
return 0;
}
int erofs_write_device_table(struct erofs_sb_info *sbi)
{
erofs_blk_t nblocks = sbi->primarydevice_blocks;
struct erofs_buffer_head *bh = sbi->bh_devt;
erofs_off_t pos;
unsigned int i, ret;
if (!sbi->extra_devices)
goto out;
if (!bh) {
if (erofs_sb_has_device_table(sbi))
return 0;
return -EINVAL;
}
pos = erofs_btell(bh, false);
if (pos == EROFS_NULL_ADDR) {
DBG_BUGON(1);
return -EINVAL;
}
i = 0;
do {
struct erofs_deviceslot dis = {
.uniaddr_lo = cpu_to_le32(nblocks),
.blocks_lo = cpu_to_le32(sbi->devs[i].blocks),
};
memcpy(dis.tag, sbi->devs[i].tag, sizeof(dis.tag));
ret = erofs_dev_write(sbi, &dis, pos, sizeof(dis));
if (ret)
return ret;
pos += sizeof(dis);
nblocks += sbi->devs[i].blocks;
} while (++i < sbi->extra_devices);
bh->op = &erofs_drop_directly_bhops;
erofs_bdrop(bh, false);
sbi->bh_devt = NULL;
out:
sbi->total_blocks = nblocks;
return 0;
}
int erofs_mkfs_format_fs(struct erofs_sb_info *sbi, unsigned int blkszbits,
unsigned int dsunit, bool metazone)
{
struct erofs_buffer_head *bh;
struct erofs_bufmgr *bmgr;
sbi->blkszbits = blkszbits;
bmgr = erofs_buffer_init(sbi, 0, NULL);
if (!bmgr)
return -ENOMEM;
sbi->bmgr = bmgr;
bmgr->dsunit = dsunit;
if (metazone)
sbi->metazone_startblk = EROFS_META_NEW_ADDR;
else
sbi->metazone_startblk = 0;
bh = erofs_reserve_sb(bmgr);
if (IS_ERR(bh))
return PTR_ERR(bh);
sbi->bh_sb = bh;
return 0;
}
int erofs_mkfs_load_fs(struct erofs_sb_info *sbi, unsigned int dsunit)
{
union {
struct stat st;
erofs_blk_t startblk;
} u;
struct erofs_bufmgr *bmgr;
int err;
sbi->bh_sb = NULL;
erofs_warn("EXPERIMENTAL incremental build in use. Use at your own risk!");
err = erofs_read_superblock(sbi);
if (err) {
erofs_err("failed to read superblock of %s: %s", sbi->devname,
erofs_strerror(err));
return err;
}
err = erofs_io_fstat(&sbi->bdev, &u.st);
if (!err && S_ISREG(u.st.st_mode))
u.startblk = DIV_ROUND_UP(u.st.st_size, erofs_blksiz(sbi));
else
u.startblk = sbi->primarydevice_blocks;
bmgr = erofs_buffer_init(sbi, u.startblk, NULL);
if (!bmgr)
return -ENOMEM;
sbi->bmgr = bmgr;
bmgr->dsunit = dsunit;
return 0;
}
|