// SPDX-License-Identifier: GPL-2.0-only /* * This file is part of UBIFS. * * Copyright (C) 2006-2008 Nokia Corporation. * * Authors: Artem Bityutskiy (Битюцкий Артём) * Adrian Hunter
*/
/* * This file implements UBIFS superblock. The superblock is stored at the first * LEB of the volume and is never changed by UBIFS. Only user-space tools may * change it. The superblock node mostly contains geometry information.
*/
/* * Default journal size in logical eraseblocks as a percent of total * flash size.
*/ #define DEFAULT_JNL_PERCENT 5
/* Default maximum journal size in bytes */ #define DEFAULT_MAX_JNL (32*1024*1024)
/* Default indexing tree fanout */ #define DEFAULT_FANOUT 8
/* Default number of data journal heads */ #define DEFAULT_JHEADS_CNT 1
/* Default positions of different LEBs in the main area */ #define DEFAULT_IDX_LEB 0 #define DEFAULT_DATA_LEB 1 #define DEFAULT_GC_LEB 2
/* Default number of LEB numbers in LPT's save table */ #define DEFAULT_LSAVE_CNT 256
/* Default reserved pool size as a percent of maximum free space */ #define DEFAULT_RP_PERCENT 5
/* The default maximum size of reserved pool in bytes */ #define DEFAULT_MAX_RP_SIZE (5*1024*1024)
/* Default time granularity in nanoseconds */ #define DEFAULT_TIME_GRAN 1000000000
staticint get_default_compressor(struct ubifs_info *c)
{ if (ubifs_compr_present(c, UBIFS_COMPR_ZSTD)) return UBIFS_COMPR_ZSTD;
if (ubifs_compr_present(c, UBIFS_COMPR_LZO)) return UBIFS_COMPR_LZO;
if (ubifs_compr_present(c, UBIFS_COMPR_ZLIB)) return UBIFS_COMPR_ZLIB;
return UBIFS_COMPR_NONE;
}
/** * create_default_filesystem - format empty UBI volume. * @c: UBIFS file-system description object * * This function creates default empty file-system. Returns zero in case of * success and a negative error code in case of failure.
*/ staticint create_default_filesystem(struct ubifs_info *c)
{ struct ubifs_sb_node *sup; struct ubifs_mst_node *mst; struct ubifs_idx_node *idx; struct ubifs_branch *br; struct ubifs_ino_node *ino; struct ubifs_cs_node *cs; union ubifs_key key; int err, tmp, jnl_lebs, log_lebs, max_buds, main_lebs, main_first; int lpt_lebs, lpt_first, orph_lebs, big_lpt, ino_waste, sup_flags = 0; int min_leb_cnt = UBIFS_MIN_LEB_CNT; int idx_node_size; longlong tmp64, main_bytes;
__le64 tmp_le64; struct timespec64 ts;
u8 hash[UBIFS_HASH_ARR_SZ];
u8 hash_lpt[UBIFS_HASH_ARR_SZ];
/* Some functions called from here depend on the @c->key_len filed */
c->key_len = UBIFS_SK_LEN;
/* * First of all, we have to calculate default file-system geometry - * log size, journal size, etc.
*/ if (c->leb_cnt < 0x7FFFFFFF / DEFAULT_JNL_PERCENT) /* We can first multiply then divide and have no overflow */
jnl_lebs = c->leb_cnt * DEFAULT_JNL_PERCENT / 100; else
jnl_lebs = (c->leb_cnt / 100) * DEFAULT_JNL_PERCENT;
if (jnl_lebs < UBIFS_MIN_JNL_LEBS)
jnl_lebs = UBIFS_MIN_JNL_LEBS; if (jnl_lebs * c->leb_size > DEFAULT_MAX_JNL)
jnl_lebs = DEFAULT_MAX_JNL / c->leb_size;
/* * The log should be large enough to fit reference nodes for all bud * LEBs. Because buds do not have to start from the beginning of LEBs * (half of the LEB may contain committed data), the log should * generally be larger, make it twice as large.
*/
tmp = 2 * (c->ref_node_alsz * jnl_lebs) + c->leb_size - 1;
log_lebs = tmp / c->leb_size; /* Plus one LEB reserved for commit */
log_lebs += 1; if (c->leb_cnt - min_leb_cnt > 8) { /* And some extra space to allow writes while committing */
log_lebs += 1;
min_leb_cnt += 1;
}
/* * Orphan nodes are stored in a separate area. One node can store a lot * of orphan inode numbers, but when new orphan comes we just add a new * orphan node. At some point the nodes are consolidated into one * orphan node.
*/
orph_lebs = UBIFS_MIN_ORPH_LEBS; if (c->leb_cnt - min_leb_cnt > 1) /* * For debugging purposes it is better to have at least 2 * orphan LEBs, because the orphan subsystem would need to do * consolidations and would be stressed more.
*/
orph_lebs += 1;
/* Set compression enabled by default */
ino->flags = cpu_to_le32(UBIFS_COMPR_FL);
dbg_gen("root inode created at LEB %d:0",
main_first + DEFAULT_DATA_LEB);
/* * The first node in the log has to be the commit start node. This is * always the case during normal file-system operation. Write a fake * commit start node to the log.
*/
/** * validate_sb - validate superblock node. * @c: UBIFS file-system description object * @sup: superblock node * * This function validates superblock node @sup. Since most of data was read * from the superblock and stored in @c, the function validates fields in @c * instead. Returns zero in case of success and %-EINVAL in case of validation * failure.
*/ staticint validate_sb(struct ubifs_info *c, struct ubifs_sb_node *sup)
{ longlong max_bytes; int err = 1, min_leb_cnt;
/* * Calculate minimum allowed amount of main area LEBs. This is very * similar to %UBIFS_MIN_LEB_CNT, but we take into account real what we * have just read from the superblock.
*/
min_leb_cnt = UBIFS_SB_LEBS + UBIFS_MST_LEBS + c->log_lebs;
min_leb_cnt += c->lpt_lebs + c->orph_lebs + c->jhead_cnt + 6;
if (c->leb_cnt < min_leb_cnt || c->leb_cnt > c->vi.size) {
ubifs_err(c, "bad LEB count: %d in superblock, %d on UBI volume, %d minimum required",
c->leb_cnt, c->vi.size, min_leb_cnt); goto failed;
}
if (c->max_leb_cnt < c->leb_cnt) {
ubifs_err(c, "max. LEB count %d less than LEB count %d",
c->max_leb_cnt, c->leb_cnt); goto failed;
}
if (c->main_lebs < UBIFS_MIN_MAIN_LEBS) {
ubifs_err(c, "too few main LEBs count %d, must be at least %d",
c->main_lebs, UBIFS_MIN_MAIN_LEBS); goto failed;
}
max_bytes = (longlong)c->leb_size * UBIFS_MIN_BUD_LEBS; if (c->max_bud_bytes < max_bytes) {
ubifs_err(c, "too small journal (%lld bytes), must be at least %lld bytes",
c->max_bud_bytes, max_bytes); goto failed;
}
max_bytes = (longlong)c->leb_size * c->main_lebs; if (c->max_bud_bytes > max_bytes) {
ubifs_err(c, "too large journal size (%lld bytes), only %lld bytes available in the main area",
c->max_bud_bytes, max_bytes); goto failed;
}
/** * ubifs_read_sb_node - read superblock node. * @c: UBIFS file-system description object * * This function returns a pointer to the superblock node or a negative error * code. Note, the user of this function is responsible of kfree()'ing the * returned superblock buffer.
*/ staticstruct ubifs_sb_node *ubifs_read_sb_node(struct ubifs_info *c)
{ struct ubifs_sb_node *sup; int err;
sup = kmalloc(ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size), GFP_NOFS); if (!sup) return ERR_PTR(-ENOMEM);
if (strcmp(hash_algo_name[hash_algo], c->auth_hash_name)) {
ubifs_err(c, "This filesystem uses %s for hashing," " but %s is specified", hash_algo_name[hash_algo],
c->auth_hash_name); return -EINVAL;
}
/* * The super block node can either be authenticated by a HMAC or * by a signature in a ubifs_sig_node directly following the * super block node to support offline image creation.
*/ if (ubifs_hmac_zero(c, sup->hmac)) {
err = ubifs_sb_verify_signature(c, sup);
} else {
err = ubifs_hmac_wkm(c, hmac_wkm); if (err) return err; if (ubifs_check_hmac(c, hmac_wkm, sup->hmac_wkm)) {
ubifs_err(c, "provided key does not fit"); return -ENOKEY;
}
err = ubifs_node_verify_hmac(c, sup, sizeof(*sup),
offsetof(struct ubifs_sb_node,
hmac));
}
if (err)
ubifs_err(c, "Failed to authenticate superblock: %d", err);
return err;
}
/** * ubifs_write_sb_node - write superblock node. * @c: UBIFS file-system description object * @sup: superblock node read with 'ubifs_read_sb_node()' * * This function returns %0 on success and a negative error code on failure.
*/ int ubifs_write_sb_node(struct ubifs_info *c, struct ubifs_sb_node *sup)
{ int len = ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size); int err;
/** * ubifs_read_superblock - read superblock. * @c: UBIFS file-system description object * * This function finds, reads and checks the superblock. If an empty UBI volume * is being mounted, this function creates default superblock. Returns zero in * case of success, and a negative error code in case of failure.
*/ int ubifs_read_superblock(struct ubifs_info *c)
{ int err, sup_flags; struct ubifs_sb_node *sup;
if (c->empty) {
err = create_default_filesystem(c); if (err) return err;
}
sup = ubifs_read_sb_node(c); if (IS_ERR(sup)) return PTR_ERR(sup);
/* * The software supports all previous versions but not future versions, * due to the unavailability of time-travelling equipment.
*/ if (c->fmt_version > UBIFS_FORMAT_VERSION) {
ubifs_assert(c, !c->ro_media || c->ro_mount); if (!c->ro_mount ||
c->ro_compat_version > UBIFS_RO_COMPAT_VERSION) {
ubifs_err(c, "on-flash format version is w%d/r%d, but software only supports up to version w%d/r%d",
c->fmt_version, c->ro_compat_version,
UBIFS_FORMAT_VERSION,
UBIFS_RO_COMPAT_VERSION); if (c->ro_compat_version <= UBIFS_RO_COMPAT_VERSION) {
ubifs_msg(c, "only R/O mounting is possible");
err = -EROFS;
} else
err = -EINVAL; goto out;
}
/* * The FS is mounted R/O, and the media format is * R/O-compatible with the UBIFS implementation, so we can * mount.
*/
c->rw_incompat = 1;
}
if (c->fmt_version < 3) {
ubifs_err(c, "on-flash format version %d is not supported",
c->fmt_version);
err = -EINVAL; goto out;
}
if (!IS_ENABLED(CONFIG_FS_ENCRYPTION) && c->encrypted) {
ubifs_err(c, "file system contains encrypted files but UBIFS" " was built without crypto support.");
err = -EINVAL; goto out;
}
/* Automatically increase file system size to the maximum size */ if (c->leb_cnt < c->vi.size && c->leb_cnt < c->max_leb_cnt) { int old_leb_cnt = c->leb_cnt;
/** * fixup_leb - fixup/unmap an LEB containing free space. * @c: UBIFS file-system description object * @lnum: the LEB number to fix up * @len: number of used bytes in LEB (starting at offset 0) * * This function reads the contents of the given LEB number @lnum, then fixes * it up, so that empty min. I/O units in the end of LEB are actually erased on * flash (rather than being just all-0xff real data). If the LEB is completely * empty, it is simply unmapped.
*/ staticint fixup_leb(struct ubifs_info *c, int lnum, int len)
{ int err;
ubifs_assert(c, len >= 0);
ubifs_assert(c, len % c->min_io_size == 0);
ubifs_assert(c, len < c->leb_size);
dbg_mnt("fixup LEB %d, data len %d", lnum, len);
err = ubifs_leb_read(c, lnum, c->sbuf, 0, len, 1); if (err) return err;
return ubifs_leb_change(c, lnum, c->sbuf, len);
}
/** * fixup_free_space - find & remap all LEBs containing free space. * @c: UBIFS file-system description object * * This function walks through all LEBs in the filesystem and fiexes up those * containing free/empty space.
*/ staticint fixup_free_space(struct ubifs_info *c)
{ int lnum, err = 0; struct ubifs_lprops *lprops;
ubifs_get_lprops(c);
/* Fixup LEBs in the master area */ for (lnum = UBIFS_MST_LNUM; lnum < UBIFS_LOG_LNUM; lnum++) {
err = fixup_leb(c, lnum, c->mst_offs + c->mst_node_alsz); if (err) goto out;
}
/* * Fixup the log head which contains the only a CS node at the * beginning.
*/
err = fixup_leb(c, c->lhead_lnum,
ALIGN(UBIFS_CS_NODE_SZ, c->min_io_size)); if (err) goto out;
/* Fixup LEBs in the LPT area */ for (lnum = c->lpt_first; lnum <= c->lpt_last; lnum++) { int free = c->ltab[lnum - c->lpt_first].free;
if (free > 0) {
err = fixup_leb(c, lnum, c->leb_size - free); if (err) goto out;
}
}
/* Unmap LEBs in the orphans area */ for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
err = fixup_leb(c, lnum, 0); if (err) goto out;
}
/* Fixup LEBs in the main area */ for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) {
lprops = ubifs_lpt_lookup(c, lnum); if (IS_ERR(lprops)) {
err = PTR_ERR(lprops); goto out;
}
if (lprops->free > 0) {
err = fixup_leb(c, lnum, c->leb_size - lprops->free); if (err) goto out;
}
}
out:
ubifs_release_lprops(c); return err;
}
/** * ubifs_fixup_free_space - find & fix all LEBs with free space. * @c: UBIFS file-system description object * * This function fixes up LEBs containing free space on first mount, if the * appropriate flag was set when the FS was created. Each LEB with one or more * empty min. I/O unit (i.e. free-space-count > 0) is re-written, to make sure * the free space is actually erased. E.g., this is necessary for some NAND * chips, since the free space may have been programmed like real "0xff" data * (generating a non-0xff ECC), causing future writes to the not-really-erased * NAND pages to behave badly. After the space is fixed up, the superblock flag * is cleared, so that this is skipped for all future mounts.
*/ int ubifs_fixup_free_space(struct ubifs_info *c)
{ int err; struct ubifs_sb_node *sup = c->sup_node;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.