Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * This file is part of UBIFS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2006-2008 Nokia Corporation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Authors: Artem Bityutskiy (Битюцкий Артём)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *          Adrian Hunter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) /* This file implements reading and writing the master node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include "ubifs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  * ubifs_compare_master_node - compare two UBIFS master nodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * @m1: the first node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * @m2: the second node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  * This function compares two UBIFS master nodes. Returns 0 if they are equal
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * and nonzero if not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) int ubifs_compare_master_node(struct ubifs_info *c, void *m1, void *m2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	int behind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	int hmac_offs = offsetof(struct ubifs_mst_node, hmac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	 * Do not compare the common node header since the sequence number and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	 * hence the CRC are different.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	ret = memcmp(m1 + UBIFS_CH_SZ, m2 + UBIFS_CH_SZ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 		     hmac_offs - UBIFS_CH_SZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	 * Do not compare the embedded HMAC aswell which also must be different
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	 * due to the different common node header.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	behind = hmac_offs + UBIFS_MAX_HMAC_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	if (UBIFS_MST_NODE_SZ > behind)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 		return memcmp(m1 + behind, m2 + behind, UBIFS_MST_NODE_SZ - behind);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) /* mst_node_check_hash - Check hash of a master node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52)  * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53)  * @mst: The master node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54)  * @expected: The expected hash of the master node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56)  * This checks the hash of a master node against a given expected hash.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57)  * Note that we have two master nodes on a UBIFS image which have different
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58)  * sequence numbers and consequently different CRCs. To be able to match
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59)  * both master nodes we exclude the common node header containing the sequence
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60)  * number and CRC from the hash.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62)  * Returns 0 if the hashes are equal, a negative error code otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) static int mst_node_check_hash(const struct ubifs_info *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 			       const struct ubifs_mst_node *mst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 			       const u8 *expected)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	u8 calc[UBIFS_MAX_HASH_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	const void *node = mst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	crypto_shash_tfm_digest(c->hash_tfm, node + sizeof(struct ubifs_ch),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 				UBIFS_MST_NODE_SZ - sizeof(struct ubifs_ch),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 				calc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	if (ubifs_check_hash(c, expected, calc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82)  * scan_for_master - search the valid master node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83)  * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85)  * This function scans the master node LEBs and search for the latest master
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86)  * node. Returns zero in case of success, %-EUCLEAN if there master area is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87)  * corrupted and requires recovery, and a negative error code in case of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88)  * failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) static int scan_for_master(struct ubifs_info *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	struct ubifs_scan_leb *sleb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	struct ubifs_scan_node *snod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	int lnum, offs = 0, nodes_cnt, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	lnum = UBIFS_MST_LNUM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	sleb = ubifs_scan(c, lnum, 0, c->sbuf, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	if (IS_ERR(sleb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		return PTR_ERR(sleb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	nodes_cnt = sleb->nodes_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	if (nodes_cnt > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 				  list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		if (snod->type != UBIFS_MST_NODE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 			goto out_dump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		memcpy(c->mst_node, snod->node, snod->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		offs = snod->offs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	ubifs_scan_destroy(sleb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	lnum += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	sleb = ubifs_scan(c, lnum, 0, c->sbuf, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	if (IS_ERR(sleb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		return PTR_ERR(sleb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	if (sleb->nodes_cnt != nodes_cnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	if (!sleb->nodes_cnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	if (snod->type != UBIFS_MST_NODE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		goto out_dump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	if (snod->offs != offs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	if (ubifs_compare_master_node(c, c->mst_node, snod->node))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	c->mst_offs = offs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	ubifs_scan_destroy(sleb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	if (!ubifs_authenticated(c))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	if (ubifs_hmac_zero(c, c->mst_node->hmac)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		err = mst_node_check_hash(c, c->mst_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 					  c->sup_node->hash_mst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 			ubifs_err(c, "Failed to verify master node hash");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		err = ubifs_node_verify_hmac(c, c->mst_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 					sizeof(struct ubifs_mst_node),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 					offsetof(struct ubifs_mst_node, hmac));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 			ubifs_err(c, "Failed to verify master node HMAC");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	ubifs_scan_destroy(sleb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	return -EUCLEAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) out_dump:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	ubifs_err(c, "unexpected node type %d master LEB %d:%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		  snod->type, lnum, snod->offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	ubifs_scan_destroy(sleb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)  * validate_master - validate master node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)  * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)  * This function validates data which was read from master node. Returns zero
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)  * if the data is all right and %-EINVAL if not.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) static int validate_master(const struct ubifs_info *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	long long main_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	if (c->max_sqnum >= SQNUM_WATERMARK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		err = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	if (c->cmt_no >= c->max_sqnum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		err = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	if (c->highest_inum >= INUM_WATERMARK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		err = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	if (c->lhead_lnum < UBIFS_LOG_LNUM ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	    c->lhead_lnum >= UBIFS_LOG_LNUM + c->log_lebs ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	    c->lhead_offs < 0 || c->lhead_offs >= c->leb_size ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	    c->lhead_offs & (c->min_io_size - 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		err = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	if (c->zroot.lnum >= c->leb_cnt || c->zroot.lnum < c->main_first ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	    c->zroot.offs >= c->leb_size || c->zroot.offs & 7) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		err = 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	if (c->zroot.len < c->ranges[UBIFS_IDX_NODE].min_len ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	    c->zroot.len > c->ranges[UBIFS_IDX_NODE].max_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		err = 6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	if (c->gc_lnum >= c->leb_cnt || c->gc_lnum < c->main_first) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		err = 7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	if (c->ihead_lnum >= c->leb_cnt || c->ihead_lnum < c->main_first ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	    c->ihead_offs % c->min_io_size || c->ihead_offs < 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	    c->ihead_offs > c->leb_size || c->ihead_offs & 7) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 		err = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	main_sz = (long long)c->main_lebs * c->leb_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	if (c->bi.old_idx_sz & 7 || c->bi.old_idx_sz >= main_sz) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		err = 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	if (c->lpt_lnum < c->lpt_first || c->lpt_lnum > c->lpt_last ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	    c->lpt_offs < 0 || c->lpt_offs + c->nnode_sz > c->leb_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 		err = 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	if (c->nhead_lnum < c->lpt_first || c->nhead_lnum > c->lpt_last ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	    c->nhead_offs < 0 || c->nhead_offs % c->min_io_size ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	    c->nhead_offs > c->leb_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		err = 11;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	if (c->ltab_lnum < c->lpt_first || c->ltab_lnum > c->lpt_last ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	    c->ltab_offs < 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	    c->ltab_offs + c->ltab_sz > c->leb_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		err = 12;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	if (c->big_lpt && (c->lsave_lnum < c->lpt_first ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	    c->lsave_lnum > c->lpt_last || c->lsave_offs < 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	    c->lsave_offs + c->lsave_sz > c->leb_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		err = 13;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	if (c->lscan_lnum < c->main_first || c->lscan_lnum >= c->leb_cnt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		err = 14;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	if (c->lst.empty_lebs < 0 || c->lst.empty_lebs > c->main_lebs - 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		err = 15;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	if (c->lst.idx_lebs < 0 || c->lst.idx_lebs > c->main_lebs - 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		err = 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	if (c->lst.total_free < 0 || c->lst.total_free > main_sz ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	    c->lst.total_free & 7) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		err = 17;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	if (c->lst.total_dirty < 0 || (c->lst.total_dirty & 7)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		err = 18;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	if (c->lst.total_used < 0 || (c->lst.total_used & 7)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		err = 19;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	if (c->lst.total_free + c->lst.total_dirty +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	    c->lst.total_used > main_sz) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		err = 20;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	if (c->lst.total_dead + c->lst.total_dark +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	    c->lst.total_used + c->bi.old_idx_sz > main_sz) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		err = 21;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	if (c->lst.total_dead < 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	    c->lst.total_dead > c->lst.total_free + c->lst.total_dirty ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	    c->lst.total_dead & 7) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		err = 22;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	if (c->lst.total_dark < 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	    c->lst.total_dark > c->lst.total_free + c->lst.total_dirty ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	    c->lst.total_dark & 7) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		err = 23;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	ubifs_err(c, "bad master node at offset %d error %d", c->mst_offs, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	ubifs_dump_node(c, c->mst_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)  * ubifs_read_master - read master node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)  * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)  * This function finds and reads the master node during file-system mount. If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)  * the flash is empty, it creates default master node as well. Returns zero in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)  * case of success and a negative error code in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) int ubifs_read_master(struct ubifs_info *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	int err, old_leb_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	c->mst_node = kzalloc(c->mst_node_alsz, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	if (!c->mst_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	err = scan_for_master(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		if (err == -EUCLEAN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 			err = ubifs_recover_master_node(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 			 * Note, we do not free 'c->mst_node' here because the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 			 * unmount routine will take care of this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	/* Make sure that the recovery flag is clear */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	c->mst_node->flags &= cpu_to_le32(~UBIFS_MST_RCVRY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	c->max_sqnum       = le64_to_cpu(c->mst_node->ch.sqnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	c->highest_inum    = le64_to_cpu(c->mst_node->highest_inum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	c->cmt_no          = le64_to_cpu(c->mst_node->cmt_no);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	c->zroot.lnum      = le32_to_cpu(c->mst_node->root_lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 	c->zroot.offs      = le32_to_cpu(c->mst_node->root_offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	c->zroot.len       = le32_to_cpu(c->mst_node->root_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	c->lhead_lnum      = le32_to_cpu(c->mst_node->log_lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	c->gc_lnum         = le32_to_cpu(c->mst_node->gc_lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	c->ihead_lnum      = le32_to_cpu(c->mst_node->ihead_lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	c->ihead_offs      = le32_to_cpu(c->mst_node->ihead_offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	c->bi.old_idx_sz   = le64_to_cpu(c->mst_node->index_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	c->lpt_lnum        = le32_to_cpu(c->mst_node->lpt_lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	c->lpt_offs        = le32_to_cpu(c->mst_node->lpt_offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	c->nhead_lnum      = le32_to_cpu(c->mst_node->nhead_lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	c->nhead_offs      = le32_to_cpu(c->mst_node->nhead_offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	c->ltab_lnum       = le32_to_cpu(c->mst_node->ltab_lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	c->ltab_offs       = le32_to_cpu(c->mst_node->ltab_offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	c->lsave_lnum      = le32_to_cpu(c->mst_node->lsave_lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	c->lsave_offs      = le32_to_cpu(c->mst_node->lsave_offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	c->lscan_lnum      = le32_to_cpu(c->mst_node->lscan_lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	c->lst.empty_lebs  = le32_to_cpu(c->mst_node->empty_lebs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	c->lst.idx_lebs    = le32_to_cpu(c->mst_node->idx_lebs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	old_leb_cnt        = le32_to_cpu(c->mst_node->leb_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	c->lst.total_free  = le64_to_cpu(c->mst_node->total_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	c->lst.total_dirty = le64_to_cpu(c->mst_node->total_dirty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	c->lst.total_used  = le64_to_cpu(c->mst_node->total_used);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	c->lst.total_dead  = le64_to_cpu(c->mst_node->total_dead);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	c->lst.total_dark  = le64_to_cpu(c->mst_node->total_dark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	ubifs_copy_hash(c, c->mst_node->hash_root_idx, c->zroot.hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	c->calc_idx_sz = c->bi.old_idx_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	if (c->mst_node->flags & cpu_to_le32(UBIFS_MST_NO_ORPHS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 		c->no_orphs = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	if (old_leb_cnt != c->leb_cnt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		/* The file system has been resized */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 		int growth = c->leb_cnt - old_leb_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		if (c->leb_cnt < old_leb_cnt ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		    c->leb_cnt < UBIFS_MIN_LEB_CNT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 			ubifs_err(c, "bad leb_cnt on master node");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 			ubifs_dump_node(c, c->mst_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 		dbg_mnt("Auto resizing (master) from %d LEBs to %d LEBs",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 			old_leb_cnt, c->leb_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		c->lst.empty_lebs += growth;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		c->lst.total_free += growth * (long long)c->leb_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		c->lst.total_dark += growth * (long long)c->dark_wm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 		 * Reflect changes back onto the master node. N.B. the master
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 		 * node gets written immediately whenever mounting (or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 		 * remounting) in read-write mode, so we do not need to write it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 		 * here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 		c->mst_node->leb_cnt = cpu_to_le32(c->leb_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 		c->mst_node->empty_lebs = cpu_to_le32(c->lst.empty_lebs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 		c->mst_node->total_free = cpu_to_le64(c->lst.total_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 		c->mst_node->total_dark = cpu_to_le64(c->lst.total_dark);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	err = validate_master(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	err = dbg_old_index_check_init(c, &c->zroot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)  * ubifs_write_master - write master node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)  * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)  * This function writes the master node. Returns zero in case of success and a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)  * negative error code in case of failure. The master node is written twice to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432)  * enable recovery.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) int ubifs_write_master(struct ubifs_info *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	int err, lnum, offs, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	ubifs_assert(c, !c->ro_media && !c->ro_mount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	if (c->ro_error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 		return -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	lnum = UBIFS_MST_LNUM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	offs = c->mst_offs + c->mst_node_alsz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	len = UBIFS_MST_NODE_SZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	if (offs + UBIFS_MST_NODE_SZ > c->leb_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 		err = ubifs_leb_unmap(c, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 		offs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	c->mst_offs = offs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	c->mst_node->highest_inum = cpu_to_le64(c->highest_inum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 	ubifs_copy_hash(c, c->zroot.hash, c->mst_node->hash_root_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	err = ubifs_write_node_hmac(c, c->mst_node, len, lnum, offs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 				    offsetof(struct ubifs_mst_node, hmac));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 	lnum += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	if (offs == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 		err = ubifs_leb_unmap(c, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	err = ubifs_write_node_hmac(c, c->mst_node, len, lnum, offs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 				    offsetof(struct ubifs_mst_node, hmac));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) }