2019-05-30 06:57:50 +07:00
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2008-07-14 23:08:37 +07:00
|
|
|
|
/*
|
|
|
|
|
* This file is part of UBIFS.
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2006-2008 Nokia Corporation.
|
|
|
|
|
*
|
|
|
|
|
* Authors: Artem Bityutskiy (Битюцкий Артём)
|
|
|
|
|
* Adrian Hunter
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* This file implements reading and writing the master node */
|
|
|
|
|
|
|
|
|
|
#include "ubifs.h"
|
|
|
|
|
|
2018-09-07 19:36:40 +07:00
|
|
|
|
/**
|
|
|
|
|
* ubifs_compare_master_node - compare two UBIFS master nodes
|
|
|
|
|
* @c: UBIFS file-system description object
|
|
|
|
|
* @m1: the first node
|
|
|
|
|
* @m2: the second node
|
|
|
|
|
*
|
|
|
|
|
* This function compares two UBIFS master nodes. Returns 0 if they are equal
|
|
|
|
|
* and nonzero if not.
|
|
|
|
|
*/
|
|
|
|
|
int ubifs_compare_master_node(struct ubifs_info *c, void *m1, void *m2)
|
|
|
|
|
{
|
|
|
|
|
int ret;
|
|
|
|
|
int behind;
|
|
|
|
|
int hmac_offs = offsetof(struct ubifs_mst_node, hmac);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Do not compare the common node header since the sequence number and
|
|
|
|
|
* hence the CRC are different.
|
|
|
|
|
*/
|
|
|
|
|
ret = memcmp(m1 + UBIFS_CH_SZ, m2 + UBIFS_CH_SZ,
|
|
|
|
|
hmac_offs - UBIFS_CH_SZ);
|
|
|
|
|
if (ret)
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Do not compare the embedded HMAC aswell which also must be different
|
|
|
|
|
* due to the different common node header.
|
|
|
|
|
*/
|
|
|
|
|
behind = hmac_offs + UBIFS_MAX_HMAC_LEN;
|
|
|
|
|
|
|
|
|
|
if (UBIFS_MST_NODE_SZ > behind)
|
|
|
|
|
return memcmp(m1 + behind, m2 + behind, UBIFS_MST_NODE_SZ - behind);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-14 15:33:22 +07:00
|
|
|
|
/* mst_node_check_hash - Check hash of a master node
|
|
|
|
|
* @c: UBIFS file-system description object
|
|
|
|
|
* @mst: The master node
|
|
|
|
|
* @expected: The expected hash of the master node
|
|
|
|
|
*
|
|
|
|
|
* This checks the hash of a master node against a given expected hash.
|
|
|
|
|
* Note that we have two master nodes on a UBIFS image which have different
|
|
|
|
|
* sequence numbers and consequently different CRCs. To be able to match
|
|
|
|
|
* both master nodes we exclude the common node header containing the sequence
|
|
|
|
|
* number and CRC from the hash.
|
|
|
|
|
*
|
|
|
|
|
* Returns 0 if the hashes are equal, a negative error code otherwise.
|
|
|
|
|
*/
|
|
|
|
|
static int mst_node_check_hash(const struct ubifs_info *c,
|
|
|
|
|
const struct ubifs_mst_node *mst,
|
|
|
|
|
const u8 *expected)
|
|
|
|
|
{
|
|
|
|
|
u8 calc[UBIFS_MAX_HASH_LEN];
|
|
|
|
|
const void *node = mst;
|
|
|
|
|
|
|
|
|
|
SHASH_DESC_ON_STACK(shash, c->hash_tfm);
|
|
|
|
|
|
|
|
|
|
shash->tfm = c->hash_tfm;
|
|
|
|
|
|
|
|
|
|
crypto_shash_digest(shash, node + sizeof(struct ubifs_ch),
|
|
|
|
|
UBIFS_MST_NODE_SZ - sizeof(struct ubifs_ch), calc);
|
|
|
|
|
|
|
|
|
|
if (ubifs_check_hash(c, expected, calc))
|
|
|
|
|
return -EPERM;
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2008-07-14 23:08:37 +07:00
|
|
|
|
/**
|
|
|
|
|
* scan_for_master - search the valid master node.
|
|
|
|
|
* @c: UBIFS file-system description object
|
|
|
|
|
*
|
|
|
|
|
* This function scans the master node LEBs and search for the latest master
|
2009-08-25 20:22:53 +07:00
|
|
|
|
* node. Returns zero in case of success, %-EUCLEAN if there master area is
|
|
|
|
|
* corrupted and requires recovery, and a negative error code in case of
|
2008-07-14 23:08:37 +07:00
|
|
|
|
* failure.
|
|
|
|
|
*/
|
|
|
|
|
static int scan_for_master(struct ubifs_info *c)
|
|
|
|
|
{
|
|
|
|
|
struct ubifs_scan_leb *sleb;
|
|
|
|
|
struct ubifs_scan_node *snod;
|
2018-09-07 19:36:40 +07:00
|
|
|
|
int lnum, offs = 0, nodes_cnt, err;
|
2008-07-14 23:08:37 +07:00
|
|
|
|
|
|
|
|
|
lnum = UBIFS_MST_LNUM;
|
|
|
|
|
|
2009-08-25 19:00:55 +07:00
|
|
|
|
sleb = ubifs_scan(c, lnum, 0, c->sbuf, 1);
|
2008-07-14 23:08:37 +07:00
|
|
|
|
if (IS_ERR(sleb))
|
|
|
|
|
return PTR_ERR(sleb);
|
|
|
|
|
nodes_cnt = sleb->nodes_cnt;
|
|
|
|
|
if (nodes_cnt > 0) {
|
|
|
|
|
snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node,
|
|
|
|
|
list);
|
|
|
|
|
if (snod->type != UBIFS_MST_NODE)
|
2009-08-25 20:22:53 +07:00
|
|
|
|
goto out_dump;
|
2008-07-14 23:08:37 +07:00
|
|
|
|
memcpy(c->mst_node, snod->node, snod->len);
|
|
|
|
|
offs = snod->offs;
|
|
|
|
|
}
|
|
|
|
|
ubifs_scan_destroy(sleb);
|
|
|
|
|
|
|
|
|
|
lnum += 1;
|
|
|
|
|
|
2009-08-25 19:00:55 +07:00
|
|
|
|
sleb = ubifs_scan(c, lnum, 0, c->sbuf, 1);
|
2008-07-14 23:08:37 +07:00
|
|
|
|
if (IS_ERR(sleb))
|
|
|
|
|
return PTR_ERR(sleb);
|
|
|
|
|
if (sleb->nodes_cnt != nodes_cnt)
|
|
|
|
|
goto out;
|
|
|
|
|
if (!sleb->nodes_cnt)
|
|
|
|
|
goto out;
|
|
|
|
|
snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node, list);
|
|
|
|
|
if (snod->type != UBIFS_MST_NODE)
|
2009-08-25 20:22:53 +07:00
|
|
|
|
goto out_dump;
|
2008-07-14 23:08:37 +07:00
|
|
|
|
if (snod->offs != offs)
|
|
|
|
|
goto out;
|
2018-09-07 19:36:40 +07:00
|
|
|
|
if (ubifs_compare_master_node(c, c->mst_node, snod->node))
|
2008-07-14 23:08:37 +07:00
|
|
|
|
goto out;
|
2018-09-07 19:36:40 +07:00
|
|
|
|
|
2008-07-14 23:08:37 +07:00
|
|
|
|
c->mst_offs = offs;
|
|
|
|
|
ubifs_scan_destroy(sleb);
|
2018-09-07 19:36:40 +07:00
|
|
|
|
|
|
|
|
|
if (!ubifs_authenticated(c))
|
|
|
|
|
return 0;
|
|
|
|
|
|
2019-05-14 15:33:22 +07:00
|
|
|
|
if (ubifs_hmac_zero(c, c->mst_node->hmac)) {
|
|
|
|
|
err = mst_node_check_hash(c, c->mst_node,
|
|
|
|
|
c->sup_node->hash_mst);
|
|
|
|
|
if (err)
|
|
|
|
|
ubifs_err(c, "Failed to verify master node hash");
|
|
|
|
|
} else {
|
|
|
|
|
err = ubifs_node_verify_hmac(c, c->mst_node,
|
|
|
|
|
sizeof(struct ubifs_mst_node),
|
|
|
|
|
offsetof(struct ubifs_mst_node, hmac));
|
|
|
|
|
if (err)
|
|
|
|
|
ubifs_err(c, "Failed to verify master node HMAC");
|
2018-09-07 19:36:40 +07:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-14 15:33:22 +07:00
|
|
|
|
if (err)
|
|
|
|
|
return -EPERM;
|
|
|
|
|
|
2008-07-14 23:08:37 +07:00
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
ubifs_scan_destroy(sleb);
|
2009-08-25 20:22:53 +07:00
|
|
|
|
return -EUCLEAN;
|
|
|
|
|
|
|
|
|
|
out_dump:
|
UBIFS: extend debug/message capabilities
In the case where we have more than one volumes on different UBI
devices, it may be not that easy to tell which volume prints the
messages. Add ubi number and volume id in ubifs_msg/warn/error
to help debug. These two values are passed by struct ubifs_info.
For those where ubifs_info is not initialized yet, ubifs_* is
replaced by pr_*. For those where ubifs_info is not avaliable,
ubifs_info is passed to the calling function as a const parameter.
The output looks like,
[ 95.444879] UBIFS (ubi0:1): background thread "ubifs_bgt0_1" started, PID 696
[ 95.484688] UBIFS (ubi0:1): UBIFS: mounted UBI device 0, volume 1, name "test1"
[ 95.484694] UBIFS (ubi0:1): LEB size: 126976 bytes (124 KiB), min./max. I/O unit sizes: 2048 bytes/2048 bytes
[ 95.484699] UBIFS (ubi0:1): FS size: 30220288 bytes (28 MiB, 238 LEBs), journal size 1523712 bytes (1 MiB, 12 LEBs)
[ 95.484703] UBIFS (ubi0:1): reserved for root: 1427378 bytes (1393 KiB)
[ 95.484709] UBIFS (ubi0:1): media format: w4/r0 (latest is w4/r0), UUID 40DFFC0E-70BE-4193-8905-F7D6DFE60B17, small LPT model
[ 95.489875] UBIFS (ubi1:0): background thread "ubifs_bgt1_0" started, PID 699
[ 95.529713] UBIFS (ubi1:0): UBIFS: mounted UBI device 1, volume 0, name "test2"
[ 95.529718] UBIFS (ubi1:0): LEB size: 126976 bytes (124 KiB), min./max. I/O unit sizes: 2048 bytes/2048 bytes
[ 95.529724] UBIFS (ubi1:0): FS size: 19808256 bytes (18 MiB, 156 LEBs), journal size 1015809 bytes (0 MiB, 8 LEBs)
[ 95.529727] UBIFS (ubi1:0): reserved for root: 935592 bytes (913 KiB)
[ 95.529733] UBIFS (ubi1:0): media format: w4/r0 (latest is w4/r0), UUID EEB7779D-F419-4CA9-811B-831CAC7233D4, small LPT model
[ 954.264767] UBIFS error (ubi1:0 pid 756): ubifs_read_node: bad node type (255 but expected 6)
[ 954.367030] UBIFS error (ubi1:0 pid 756): ubifs_read_node: bad node at LEB 0:0, LEB mapping status 1
Signed-off-by: Sheng Yong <shengyong1@huawei.com>
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@linux.intel.com>
2015-03-20 17:39:42 +07:00
|
|
|
|
ubifs_err(c, "unexpected node type %d master LEB %d:%d",
|
2009-08-25 20:22:53 +07:00
|
|
|
|
snod->type, lnum, snod->offs);
|
|
|
|
|
ubifs_scan_destroy(sleb);
|
2008-07-14 23:08:37 +07:00
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* validate_master - validate master node.
|
|
|
|
|
* @c: UBIFS file-system description object
|
|
|
|
|
*
|
|
|
|
|
* This function validates data which was read from master node. Returns zero
|
|
|
|
|
* if the data is all right and %-EINVAL if not.
|
|
|
|
|
*/
|
|
|
|
|
static int validate_master(const struct ubifs_info *c)
|
|
|
|
|
{
|
|
|
|
|
long long main_sz;
|
|
|
|
|
int err;
|
|
|
|
|
|
|
|
|
|
if (c->max_sqnum >= SQNUM_WATERMARK) {
|
|
|
|
|
err = 1;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (c->cmt_no >= c->max_sqnum) {
|
|
|
|
|
err = 2;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (c->highest_inum >= INUM_WATERMARK) {
|
|
|
|
|
err = 3;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (c->lhead_lnum < UBIFS_LOG_LNUM ||
|
|
|
|
|
c->lhead_lnum >= UBIFS_LOG_LNUM + c->log_lebs ||
|
|
|
|
|
c->lhead_offs < 0 || c->lhead_offs >= c->leb_size ||
|
|
|
|
|
c->lhead_offs & (c->min_io_size - 1)) {
|
|
|
|
|
err = 4;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (c->zroot.lnum >= c->leb_cnt || c->zroot.lnum < c->main_first ||
|
|
|
|
|
c->zroot.offs >= c->leb_size || c->zroot.offs & 7) {
|
|
|
|
|
err = 5;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (c->zroot.len < c->ranges[UBIFS_IDX_NODE].min_len ||
|
|
|
|
|
c->zroot.len > c->ranges[UBIFS_IDX_NODE].max_len) {
|
|
|
|
|
err = 6;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (c->gc_lnum >= c->leb_cnt || c->gc_lnum < c->main_first) {
|
|
|
|
|
err = 7;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (c->ihead_lnum >= c->leb_cnt || c->ihead_lnum < c->main_first ||
|
|
|
|
|
c->ihead_offs % c->min_io_size || c->ihead_offs < 0 ||
|
|
|
|
|
c->ihead_offs > c->leb_size || c->ihead_offs & 7) {
|
|
|
|
|
err = 8;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
main_sz = (long long)c->main_lebs * c->leb_size;
|
2011-03-29 22:04:05 +07:00
|
|
|
|
if (c->bi.old_idx_sz & 7 || c->bi.old_idx_sz >= main_sz) {
|
2008-07-14 23:08:37 +07:00
|
|
|
|
err = 9;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (c->lpt_lnum < c->lpt_first || c->lpt_lnum > c->lpt_last ||
|
|
|
|
|
c->lpt_offs < 0 || c->lpt_offs + c->nnode_sz > c->leb_size) {
|
|
|
|
|
err = 10;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (c->nhead_lnum < c->lpt_first || c->nhead_lnum > c->lpt_last ||
|
|
|
|
|
c->nhead_offs < 0 || c->nhead_offs % c->min_io_size ||
|
|
|
|
|
c->nhead_offs > c->leb_size) {
|
|
|
|
|
err = 11;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (c->ltab_lnum < c->lpt_first || c->ltab_lnum > c->lpt_last ||
|
|
|
|
|
c->ltab_offs < 0 ||
|
|
|
|
|
c->ltab_offs + c->ltab_sz > c->leb_size) {
|
|
|
|
|
err = 12;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (c->big_lpt && (c->lsave_lnum < c->lpt_first ||
|
|
|
|
|
c->lsave_lnum > c->lpt_last || c->lsave_offs < 0 ||
|
|
|
|
|
c->lsave_offs + c->lsave_sz > c->leb_size)) {
|
|
|
|
|
err = 13;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (c->lscan_lnum < c->main_first || c->lscan_lnum >= c->leb_cnt) {
|
|
|
|
|
err = 14;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (c->lst.empty_lebs < 0 || c->lst.empty_lebs > c->main_lebs - 2) {
|
|
|
|
|
err = 15;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (c->lst.idx_lebs < 0 || c->lst.idx_lebs > c->main_lebs - 1) {
|
|
|
|
|
err = 16;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (c->lst.total_free < 0 || c->lst.total_free > main_sz ||
|
|
|
|
|
c->lst.total_free & 7) {
|
|
|
|
|
err = 17;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (c->lst.total_dirty < 0 || (c->lst.total_dirty & 7)) {
|
|
|
|
|
err = 18;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (c->lst.total_used < 0 || (c->lst.total_used & 7)) {
|
|
|
|
|
err = 19;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (c->lst.total_free + c->lst.total_dirty +
|
|
|
|
|
c->lst.total_used > main_sz) {
|
|
|
|
|
err = 20;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (c->lst.total_dead + c->lst.total_dark +
|
2011-03-29 22:04:05 +07:00
|
|
|
|
c->lst.total_used + c->bi.old_idx_sz > main_sz) {
|
2008-07-14 23:08:37 +07:00
|
|
|
|
err = 21;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (c->lst.total_dead < 0 ||
|
|
|
|
|
c->lst.total_dead > c->lst.total_free + c->lst.total_dirty ||
|
|
|
|
|
c->lst.total_dead & 7) {
|
|
|
|
|
err = 22;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (c->lst.total_dark < 0 ||
|
|
|
|
|
c->lst.total_dark > c->lst.total_free + c->lst.total_dirty ||
|
|
|
|
|
c->lst.total_dark & 7) {
|
|
|
|
|
err = 23;
|
|
|
|
|
goto out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
out:
|
UBIFS: extend debug/message capabilities
In the case where we have more than one volumes on different UBI
devices, it may be not that easy to tell which volume prints the
messages. Add ubi number and volume id in ubifs_msg/warn/error
to help debug. These two values are passed by struct ubifs_info.
For those where ubifs_info is not initialized yet, ubifs_* is
replaced by pr_*. For those where ubifs_info is not avaliable,
ubifs_info is passed to the calling function as a const parameter.
The output looks like,
[ 95.444879] UBIFS (ubi0:1): background thread "ubifs_bgt0_1" started, PID 696
[ 95.484688] UBIFS (ubi0:1): UBIFS: mounted UBI device 0, volume 1, name "test1"
[ 95.484694] UBIFS (ubi0:1): LEB size: 126976 bytes (124 KiB), min./max. I/O unit sizes: 2048 bytes/2048 bytes
[ 95.484699] UBIFS (ubi0:1): FS size: 30220288 bytes (28 MiB, 238 LEBs), journal size 1523712 bytes (1 MiB, 12 LEBs)
[ 95.484703] UBIFS (ubi0:1): reserved for root: 1427378 bytes (1393 KiB)
[ 95.484709] UBIFS (ubi0:1): media format: w4/r0 (latest is w4/r0), UUID 40DFFC0E-70BE-4193-8905-F7D6DFE60B17, small LPT model
[ 95.489875] UBIFS (ubi1:0): background thread "ubifs_bgt1_0" started, PID 699
[ 95.529713] UBIFS (ubi1:0): UBIFS: mounted UBI device 1, volume 0, name "test2"
[ 95.529718] UBIFS (ubi1:0): LEB size: 126976 bytes (124 KiB), min./max. I/O unit sizes: 2048 bytes/2048 bytes
[ 95.529724] UBIFS (ubi1:0): FS size: 19808256 bytes (18 MiB, 156 LEBs), journal size 1015809 bytes (0 MiB, 8 LEBs)
[ 95.529727] UBIFS (ubi1:0): reserved for root: 935592 bytes (913 KiB)
[ 95.529733] UBIFS (ubi1:0): media format: w4/r0 (latest is w4/r0), UUID EEB7779D-F419-4CA9-811B-831CAC7233D4, small LPT model
[ 954.264767] UBIFS error (ubi1:0 pid 756): ubifs_read_node: bad node type (255 but expected 6)
[ 954.367030] UBIFS error (ubi1:0 pid 756): ubifs_read_node: bad node at LEB 0:0, LEB mapping status 1
Signed-off-by: Sheng Yong <shengyong1@huawei.com>
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@linux.intel.com>
2015-03-20 17:39:42 +07:00
|
|
|
|
ubifs_err(c, "bad master node at offset %d error %d", c->mst_offs, err);
|
2012-05-16 23:15:56 +07:00
|
|
|
|
ubifs_dump_node(c, c->mst_node);
|
2008-07-14 23:08:37 +07:00
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ubifs_read_master - read master node.
|
|
|
|
|
* @c: UBIFS file-system description object
|
|
|
|
|
*
|
|
|
|
|
* This function finds and reads the master node during file-system mount. If
|
|
|
|
|
* the flash is empty, it creates default master node as well. Returns zero in
|
|
|
|
|
* case of success and a negative error code in case of failure.
|
|
|
|
|
*/
|
|
|
|
|
int ubifs_read_master(struct ubifs_info *c)
|
|
|
|
|
{
|
|
|
|
|
int err, old_leb_cnt;
|
|
|
|
|
|
|
|
|
|
c->mst_node = kzalloc(c->mst_node_alsz, GFP_KERNEL);
|
|
|
|
|
if (!c->mst_node)
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
|
|
err = scan_for_master(c);
|
|
|
|
|
if (err) {
|
2009-08-25 20:22:53 +07:00
|
|
|
|
if (err == -EUCLEAN)
|
|
|
|
|
err = ubifs_recover_master_node(c);
|
2008-07-14 23:08:37 +07:00
|
|
|
|
if (err)
|
|
|
|
|
/*
|
|
|
|
|
* Note, we do not free 'c->mst_node' here because the
|
|
|
|
|
* unmount routine will take care of this.
|
|
|
|
|
*/
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Make sure that the recovery flag is clear */
|
|
|
|
|
c->mst_node->flags &= cpu_to_le32(~UBIFS_MST_RCVRY);
|
|
|
|
|
|
|
|
|
|
c->max_sqnum = le64_to_cpu(c->mst_node->ch.sqnum);
|
|
|
|
|
c->highest_inum = le64_to_cpu(c->mst_node->highest_inum);
|
|
|
|
|
c->cmt_no = le64_to_cpu(c->mst_node->cmt_no);
|
|
|
|
|
c->zroot.lnum = le32_to_cpu(c->mst_node->root_lnum);
|
|
|
|
|
c->zroot.offs = le32_to_cpu(c->mst_node->root_offs);
|
|
|
|
|
c->zroot.len = le32_to_cpu(c->mst_node->root_len);
|
|
|
|
|
c->lhead_lnum = le32_to_cpu(c->mst_node->log_lnum);
|
|
|
|
|
c->gc_lnum = le32_to_cpu(c->mst_node->gc_lnum);
|
|
|
|
|
c->ihead_lnum = le32_to_cpu(c->mst_node->ihead_lnum);
|
|
|
|
|
c->ihead_offs = le32_to_cpu(c->mst_node->ihead_offs);
|
2011-03-29 22:04:05 +07:00
|
|
|
|
c->bi.old_idx_sz = le64_to_cpu(c->mst_node->index_size);
|
2008-07-14 23:08:37 +07:00
|
|
|
|
c->lpt_lnum = le32_to_cpu(c->mst_node->lpt_lnum);
|
|
|
|
|
c->lpt_offs = le32_to_cpu(c->mst_node->lpt_offs);
|
|
|
|
|
c->nhead_lnum = le32_to_cpu(c->mst_node->nhead_lnum);
|
|
|
|
|
c->nhead_offs = le32_to_cpu(c->mst_node->nhead_offs);
|
|
|
|
|
c->ltab_lnum = le32_to_cpu(c->mst_node->ltab_lnum);
|
|
|
|
|
c->ltab_offs = le32_to_cpu(c->mst_node->ltab_offs);
|
|
|
|
|
c->lsave_lnum = le32_to_cpu(c->mst_node->lsave_lnum);
|
|
|
|
|
c->lsave_offs = le32_to_cpu(c->mst_node->lsave_offs);
|
|
|
|
|
c->lscan_lnum = le32_to_cpu(c->mst_node->lscan_lnum);
|
|
|
|
|
c->lst.empty_lebs = le32_to_cpu(c->mst_node->empty_lebs);
|
|
|
|
|
c->lst.idx_lebs = le32_to_cpu(c->mst_node->idx_lebs);
|
|
|
|
|
old_leb_cnt = le32_to_cpu(c->mst_node->leb_cnt);
|
|
|
|
|
c->lst.total_free = le64_to_cpu(c->mst_node->total_free);
|
|
|
|
|
c->lst.total_dirty = le64_to_cpu(c->mst_node->total_dirty);
|
|
|
|
|
c->lst.total_used = le64_to_cpu(c->mst_node->total_used);
|
|
|
|
|
c->lst.total_dead = le64_to_cpu(c->mst_node->total_dead);
|
|
|
|
|
c->lst.total_dark = le64_to_cpu(c->mst_node->total_dark);
|
|
|
|
|
|
2018-09-07 19:36:35 +07:00
|
|
|
|
ubifs_copy_hash(c, c->mst_node->hash_root_idx, c->zroot.hash);
|
|
|
|
|
|
2011-03-29 22:04:05 +07:00
|
|
|
|
c->calc_idx_sz = c->bi.old_idx_sz;
|
2008-07-14 23:08:37 +07:00
|
|
|
|
|
|
|
|
|
if (c->mst_node->flags & cpu_to_le32(UBIFS_MST_NO_ORPHS))
|
|
|
|
|
c->no_orphs = 1;
|
|
|
|
|
|
|
|
|
|
if (old_leb_cnt != c->leb_cnt) {
|
|
|
|
|
/* The file system has been resized */
|
|
|
|
|
int growth = c->leb_cnt - old_leb_cnt;
|
|
|
|
|
|
|
|
|
|
if (c->leb_cnt < old_leb_cnt ||
|
|
|
|
|
c->leb_cnt < UBIFS_MIN_LEB_CNT) {
|
UBIFS: extend debug/message capabilities
In the case where we have more than one volumes on different UBI
devices, it may be not that easy to tell which volume prints the
messages. Add ubi number and volume id in ubifs_msg/warn/error
to help debug. These two values are passed by struct ubifs_info.
For those where ubifs_info is not initialized yet, ubifs_* is
replaced by pr_*. For those where ubifs_info is not avaliable,
ubifs_info is passed to the calling function as a const parameter.
The output looks like,
[ 95.444879] UBIFS (ubi0:1): background thread "ubifs_bgt0_1" started, PID 696
[ 95.484688] UBIFS (ubi0:1): UBIFS: mounted UBI device 0, volume 1, name "test1"
[ 95.484694] UBIFS (ubi0:1): LEB size: 126976 bytes (124 KiB), min./max. I/O unit sizes: 2048 bytes/2048 bytes
[ 95.484699] UBIFS (ubi0:1): FS size: 30220288 bytes (28 MiB, 238 LEBs), journal size 1523712 bytes (1 MiB, 12 LEBs)
[ 95.484703] UBIFS (ubi0:1): reserved for root: 1427378 bytes (1393 KiB)
[ 95.484709] UBIFS (ubi0:1): media format: w4/r0 (latest is w4/r0), UUID 40DFFC0E-70BE-4193-8905-F7D6DFE60B17, small LPT model
[ 95.489875] UBIFS (ubi1:0): background thread "ubifs_bgt1_0" started, PID 699
[ 95.529713] UBIFS (ubi1:0): UBIFS: mounted UBI device 1, volume 0, name "test2"
[ 95.529718] UBIFS (ubi1:0): LEB size: 126976 bytes (124 KiB), min./max. I/O unit sizes: 2048 bytes/2048 bytes
[ 95.529724] UBIFS (ubi1:0): FS size: 19808256 bytes (18 MiB, 156 LEBs), journal size 1015809 bytes (0 MiB, 8 LEBs)
[ 95.529727] UBIFS (ubi1:0): reserved for root: 935592 bytes (913 KiB)
[ 95.529733] UBIFS (ubi1:0): media format: w4/r0 (latest is w4/r0), UUID EEB7779D-F419-4CA9-811B-831CAC7233D4, small LPT model
[ 954.264767] UBIFS error (ubi1:0 pid 756): ubifs_read_node: bad node type (255 but expected 6)
[ 954.367030] UBIFS error (ubi1:0 pid 756): ubifs_read_node: bad node at LEB 0:0, LEB mapping status 1
Signed-off-by: Sheng Yong <shengyong1@huawei.com>
Signed-off-by: Artem Bityutskiy <artem.bityutskiy@linux.intel.com>
2015-03-20 17:39:42 +07:00
|
|
|
|
ubifs_err(c, "bad leb_cnt on master node");
|
2012-05-16 23:15:56 +07:00
|
|
|
|
ubifs_dump_node(c, c->mst_node);
|
2008-07-14 23:08:37 +07:00
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dbg_mnt("Auto resizing (master) from %d LEBs to %d LEBs",
|
|
|
|
|
old_leb_cnt, c->leb_cnt);
|
|
|
|
|
c->lst.empty_lebs += growth;
|
|
|
|
|
c->lst.total_free += growth * (long long)c->leb_size;
|
|
|
|
|
c->lst.total_dark += growth * (long long)c->dark_wm;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Reflect changes back onto the master node. N.B. the master
|
|
|
|
|
* node gets written immediately whenever mounting (or
|
|
|
|
|
* remounting) in read-write mode, so we do not need to write it
|
|
|
|
|
* here.
|
|
|
|
|
*/
|
|
|
|
|
c->mst_node->leb_cnt = cpu_to_le32(c->leb_cnt);
|
|
|
|
|
c->mst_node->empty_lebs = cpu_to_le32(c->lst.empty_lebs);
|
|
|
|
|
c->mst_node->total_free = cpu_to_le64(c->lst.total_free);
|
|
|
|
|
c->mst_node->total_dark = cpu_to_le64(c->lst.total_dark);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = validate_master(c);
|
|
|
|
|
if (err)
|
|
|
|
|
return err;
|
|
|
|
|
|
|
|
|
|
err = dbg_old_index_check_init(c, &c->zroot);
|
|
|
|
|
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ubifs_write_master - write master node.
|
|
|
|
|
* @c: UBIFS file-system description object
|
|
|
|
|
*
|
2014-06-29 20:55:02 +07:00
|
|
|
|
* This function writes the master node. Returns zero in case of success and a
|
|
|
|
|
* negative error code in case of failure. The master node is written twice to
|
|
|
|
|
* enable recovery.
|
2008-07-14 23:08:37 +07:00
|
|
|
|
*/
|
|
|
|
|
int ubifs_write_master(struct ubifs_info *c)
|
|
|
|
|
{
|
|
|
|
|
int err, lnum, offs, len;
|
|
|
|
|
|
2018-07-12 18:01:57 +07:00
|
|
|
|
ubifs_assert(c, !c->ro_media && !c->ro_mount);
|
UBIFS: introduce new flag for RO due to errors
The R/O state may have various reasons:
1. The UBI volume is R/O
2. The FS is mounted R/O
3. The FS switched to R/O mode because of an error
However, in UBIFS we have only one variable which represents cases
1 and 3 - 'c->ro_media'. Indeed, we set this to 1 if we switch to
R/O mode due to an error, and then we test it in many places to
make sure that we stop writing as soon as the error happens.
But this is very unclean. One consequence of this, for example, is
that in 'ubifs_remount_fs()' we use 'c->ro_media' to check whether
we are in R/O mode because on an error, and we print a message
in this case. However, if we are in R/O mode because the media
is R/O, our message is bogus.
This patch introduces new flag - 'c->ro_error' which is set when
we switch to R/O mode because of an error. It also changes all
"if (c->ro_media)" checks to "if (c->ro_error)" checks, because
this is what the checks actually mean. We do not need to check
for 'c->ro_media' because if the UBI volume is in R/O mode, we
do not allow R/W mounting, and now writes can happen. This is
guaranteed by VFS. But it is good to double-check this, so this
patch also adds many "ubifs_assert(!c->ro_media)" checks.
In the 'ubifs_remount_fs()' function this patch makes a bit more
changes - it fixes the error messages as well.
Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
2010-09-17 20:44:28 +07:00
|
|
|
|
if (c->ro_error)
|
2009-01-29 21:22:54 +07:00
|
|
|
|
return -EROFS;
|
2008-07-14 23:08:37 +07:00
|
|
|
|
|
|
|
|
|
lnum = UBIFS_MST_LNUM;
|
|
|
|
|
offs = c->mst_offs + c->mst_node_alsz;
|
|
|
|
|
len = UBIFS_MST_NODE_SZ;
|
|
|
|
|
|
|
|
|
|
if (offs + UBIFS_MST_NODE_SZ > c->leb_size) {
|
|
|
|
|
err = ubifs_leb_unmap(c, lnum);
|
|
|
|
|
if (err)
|
|
|
|
|
return err;
|
|
|
|
|
offs = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c->mst_offs = offs;
|
|
|
|
|
c->mst_node->highest_inum = cpu_to_le64(c->highest_inum);
|
|
|
|
|
|
2018-09-07 19:36:35 +07:00
|
|
|
|
ubifs_copy_hash(c, c->zroot.hash, c->mst_node->hash_root_idx);
|
2018-09-07 19:36:40 +07:00
|
|
|
|
err = ubifs_write_node_hmac(c, c->mst_node, len, lnum, offs,
|
|
|
|
|
offsetof(struct ubifs_mst_node, hmac));
|
2008-07-14 23:08:37 +07:00
|
|
|
|
if (err)
|
|
|
|
|
return err;
|
|
|
|
|
|
|
|
|
|
lnum += 1;
|
|
|
|
|
|
|
|
|
|
if (offs == 0) {
|
|
|
|
|
err = ubifs_leb_unmap(c, lnum);
|
|
|
|
|
if (err)
|
|
|
|
|
return err;
|
|
|
|
|
}
|
2018-09-07 19:36:40 +07:00
|
|
|
|
err = ubifs_write_node_hmac(c, c->mst_node, len, lnum, offs,
|
|
|
|
|
offsetof(struct ubifs_mst_node, hmac));
|
2008-07-14 23:08:37 +07:00
|
|
|
|
|
|
|
|
|
return err;
|
|
|
|
|
}
|