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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * This file implements UBIFS superblock. The superblock is stored at the first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * LEB of the volume and is never changed by UBIFS. Only user-space tools may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * change it. The superblock node mostly contains geometry information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include "ubifs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/math64.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/uuid.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * Default journal size in logical eraseblocks as a percent of total
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * flash size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #define DEFAULT_JNL_PERCENT 5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) /* Default maximum journal size in bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #define DEFAULT_MAX_JNL (32*1024*1024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) /* Default indexing tree fanout */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define DEFAULT_FANOUT 8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) /* Default number of data journal heads */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define DEFAULT_JHEADS_CNT 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) /* Default positions of different LEBs in the main area */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #define DEFAULT_IDX_LEB  0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #define DEFAULT_DATA_LEB 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #define DEFAULT_GC_LEB   2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) /* Default number of LEB numbers in LPT's save table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) #define DEFAULT_LSAVE_CNT 256
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) /* Default reserved pool size as a percent of maximum free space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) #define DEFAULT_RP_PERCENT 5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) /* The default maximum size of reserved pool in bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) #define DEFAULT_MAX_RP_SIZE (5*1024*1024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) /* Default time granularity in nanoseconds */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) #define DEFAULT_TIME_GRAN 1000000000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) static int get_default_compressor(struct ubifs_info *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	if (ubifs_compr_present(c, UBIFS_COMPR_LZO))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		return UBIFS_COMPR_LZO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	if (ubifs_compr_present(c, UBIFS_COMPR_ZLIB))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		return UBIFS_COMPR_ZLIB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	return UBIFS_COMPR_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66)  * create_default_filesystem - format empty UBI volume.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67)  * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69)  * This function creates default empty file-system. Returns zero in case of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70)  * success and a negative error code in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) static int create_default_filesystem(struct ubifs_info *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	struct ubifs_sb_node *sup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	struct ubifs_mst_node *mst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	struct ubifs_idx_node *idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	struct ubifs_branch *br;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	struct ubifs_ino_node *ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	struct ubifs_cs_node *cs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	union ubifs_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	int err, tmp, jnl_lebs, log_lebs, max_buds, main_lebs, main_first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	int lpt_lebs, lpt_first, orph_lebs, big_lpt, ino_waste, sup_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	int min_leb_cnt = UBIFS_MIN_LEB_CNT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	int idx_node_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	long long tmp64, main_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	__le64 tmp_le64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	struct timespec64 ts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	u8 hash[UBIFS_HASH_ARR_SZ];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	u8 hash_lpt[UBIFS_HASH_ARR_SZ];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	/* Some functions called from here depend on the @c->key_len filed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	c->key_len = UBIFS_SK_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	 * First of all, we have to calculate default file-system geometry -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	 * log size, journal size, etc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	if (c->leb_cnt < 0x7FFFFFFF / DEFAULT_JNL_PERCENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		/* We can first multiply then divide and have no overflow */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		jnl_lebs = c->leb_cnt * DEFAULT_JNL_PERCENT / 100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		jnl_lebs = (c->leb_cnt / 100) * DEFAULT_JNL_PERCENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	if (jnl_lebs < UBIFS_MIN_JNL_LEBS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 		jnl_lebs = UBIFS_MIN_JNL_LEBS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	if (jnl_lebs * c->leb_size > DEFAULT_MAX_JNL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		jnl_lebs = DEFAULT_MAX_JNL / c->leb_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	 * The log should be large enough to fit reference nodes for all bud
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	 * LEBs. Because buds do not have to start from the beginning of LEBs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	 * (half of the LEB may contain committed data), the log should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	 * generally be larger, make it twice as large.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	tmp = 2 * (c->ref_node_alsz * jnl_lebs) + c->leb_size - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	log_lebs = tmp / c->leb_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	/* Plus one LEB reserved for commit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	log_lebs += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	if (c->leb_cnt - min_leb_cnt > 8) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 		/* And some extra space to allow writes while committing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		log_lebs += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		min_leb_cnt += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	max_buds = jnl_lebs - log_lebs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	if (max_buds < UBIFS_MIN_BUD_LEBS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		max_buds = UBIFS_MIN_BUD_LEBS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	 * Orphan nodes are stored in a separate area. One node can store a lot
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	 * of orphan inode numbers, but when new orphan comes we just add a new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	 * orphan node. At some point the nodes are consolidated into one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	 * orphan node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	orph_lebs = UBIFS_MIN_ORPH_LEBS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	if (c->leb_cnt - min_leb_cnt > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		 * For debugging purposes it is better to have at least 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		 * orphan LEBs, because the orphan subsystem would need to do
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		 * consolidations and would be stressed more.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		orph_lebs += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	main_lebs = c->leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS - log_lebs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	main_lebs -= orph_lebs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	lpt_first = UBIFS_LOG_LNUM + log_lebs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	c->lsave_cnt = DEFAULT_LSAVE_CNT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	c->max_leb_cnt = c->leb_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	err = ubifs_create_dflt_lpt(c, &main_lebs, lpt_first, &lpt_lebs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 				    &big_lpt, hash_lpt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	dbg_gen("LEB Properties Tree created (LEBs %d-%d)", lpt_first,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 		lpt_first + lpt_lebs - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	main_first = c->leb_cnt - main_lebs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	sup = kzalloc(ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	mst = kzalloc(c->mst_node_alsz, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	idx_node_size = ubifs_idx_node_sz(c, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	idx = kzalloc(ALIGN(idx_node_size, c->min_io_size), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	ino = kzalloc(ALIGN(UBIFS_INO_NODE_SZ, c->min_io_size), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	cs = kzalloc(ALIGN(UBIFS_CS_NODE_SZ, c->min_io_size), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	if (!sup || !mst || !idx || !ino || !cs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	/* Create default superblock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	tmp64 = (long long)max_buds * c->leb_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	if (big_lpt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		sup_flags |= UBIFS_FLG_BIGLPT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	if (ubifs_default_version > 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 		sup_flags |= UBIFS_FLG_DOUBLE_HASH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	if (ubifs_authenticated(c)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 		sup_flags |= UBIFS_FLG_AUTHENTICATION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 		sup->hash_algo = cpu_to_le16(c->auth_hash_algo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		err = ubifs_hmac_wkm(c, sup->hmac_wkm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 		sup->hash_algo = cpu_to_le16(0xffff);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	sup->ch.node_type  = UBIFS_SB_NODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	sup->key_hash      = UBIFS_KEY_HASH_R5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	sup->flags         = cpu_to_le32(sup_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	sup->min_io_size   = cpu_to_le32(c->min_io_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	sup->leb_size      = cpu_to_le32(c->leb_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	sup->leb_cnt       = cpu_to_le32(c->leb_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	sup->max_leb_cnt   = cpu_to_le32(c->max_leb_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	sup->max_bud_bytes = cpu_to_le64(tmp64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	sup->log_lebs      = cpu_to_le32(log_lebs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	sup->lpt_lebs      = cpu_to_le32(lpt_lebs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	sup->orph_lebs     = cpu_to_le32(orph_lebs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	sup->jhead_cnt     = cpu_to_le32(DEFAULT_JHEADS_CNT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	sup->fanout        = cpu_to_le32(DEFAULT_FANOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	sup->lsave_cnt     = cpu_to_le32(c->lsave_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	sup->fmt_version   = cpu_to_le32(ubifs_default_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	sup->time_gran     = cpu_to_le32(DEFAULT_TIME_GRAN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	if (c->mount_opts.override_compr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 		sup->default_compr = cpu_to_le16(c->mount_opts.compr_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		sup->default_compr = cpu_to_le16(get_default_compressor(c));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	generate_random_uuid(sup->uuid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	main_bytes = (long long)main_lebs * c->leb_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	tmp64 = div_u64(main_bytes * DEFAULT_RP_PERCENT, 100);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	if (tmp64 > DEFAULT_MAX_RP_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		tmp64 = DEFAULT_MAX_RP_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	sup->rp_size = cpu_to_le64(tmp64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	sup->ro_compat_version = cpu_to_le32(UBIFS_RO_COMPAT_VERSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	dbg_gen("default superblock created at LEB 0:0");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	/* Create default master node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	mst->ch.node_type = UBIFS_MST_NODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	mst->log_lnum     = cpu_to_le32(UBIFS_LOG_LNUM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	mst->highest_inum = cpu_to_le64(UBIFS_FIRST_INO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	mst->cmt_no       = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	mst->root_lnum    = cpu_to_le32(main_first + DEFAULT_IDX_LEB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	mst->root_offs    = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	tmp = ubifs_idx_node_sz(c, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	mst->root_len     = cpu_to_le32(tmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	mst->gc_lnum      = cpu_to_le32(main_first + DEFAULT_GC_LEB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	mst->ihead_lnum   = cpu_to_le32(main_first + DEFAULT_IDX_LEB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	mst->ihead_offs   = cpu_to_le32(ALIGN(tmp, c->min_io_size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	mst->index_size   = cpu_to_le64(ALIGN(tmp, 8));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	mst->lpt_lnum     = cpu_to_le32(c->lpt_lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	mst->lpt_offs     = cpu_to_le32(c->lpt_offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	mst->nhead_lnum   = cpu_to_le32(c->nhead_lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	mst->nhead_offs   = cpu_to_le32(c->nhead_offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	mst->ltab_lnum    = cpu_to_le32(c->ltab_lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	mst->ltab_offs    = cpu_to_le32(c->ltab_offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	mst->lsave_lnum   = cpu_to_le32(c->lsave_lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	mst->lsave_offs   = cpu_to_le32(c->lsave_offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	mst->lscan_lnum   = cpu_to_le32(main_first);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	mst->empty_lebs   = cpu_to_le32(main_lebs - 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	mst->idx_lebs     = cpu_to_le32(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	mst->leb_cnt      = cpu_to_le32(c->leb_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	ubifs_copy_hash(c, hash_lpt, mst->hash_lpt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	/* Calculate lprops statistics */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	tmp64 = main_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	tmp64 -= ALIGN(ubifs_idx_node_sz(c, 1), c->min_io_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	tmp64 -= ALIGN(UBIFS_INO_NODE_SZ, c->min_io_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	mst->total_free = cpu_to_le64(tmp64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	tmp64 = ALIGN(ubifs_idx_node_sz(c, 1), c->min_io_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	ino_waste = ALIGN(UBIFS_INO_NODE_SZ, c->min_io_size) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 			  UBIFS_INO_NODE_SZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	tmp64 += ino_waste;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	tmp64 -= ALIGN(ubifs_idx_node_sz(c, 1), 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	mst->total_dirty = cpu_to_le64(tmp64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	/*  The indexing LEB does not contribute to dark space */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	tmp64 = ((long long)(c->main_lebs - 1) * c->dark_wm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	mst->total_dark = cpu_to_le64(tmp64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	mst->total_used = cpu_to_le64(UBIFS_INO_NODE_SZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	dbg_gen("default master node created at LEB %d:0", UBIFS_MST_LNUM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	/* Create the root indexing node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	c->key_fmt = UBIFS_SIMPLE_KEY_FMT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	c->key_hash = key_r5_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	idx->ch.node_type = UBIFS_IDX_NODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	idx->child_cnt = cpu_to_le16(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	ino_key_init(c, &key, UBIFS_ROOT_INO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	br = ubifs_idx_branch(c, idx, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	key_write_idx(c, &key, &br->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	br->lnum = cpu_to_le32(main_first + DEFAULT_DATA_LEB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	br->len  = cpu_to_le32(UBIFS_INO_NODE_SZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	dbg_gen("default root indexing node created LEB %d:0",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		main_first + DEFAULT_IDX_LEB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	/* Create default root inode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	ino_key_init_flash(c, &ino->key, UBIFS_ROOT_INO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	ino->ch.node_type = UBIFS_INO_NODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	ino->creat_sqnum = cpu_to_le64(++c->max_sqnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	ino->nlink = cpu_to_le32(2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	ktime_get_coarse_real_ts64(&ts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	tmp_le64 = cpu_to_le64(ts.tv_sec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	ino->atime_sec   = tmp_le64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	ino->ctime_sec   = tmp_le64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	ino->mtime_sec   = tmp_le64;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	ino->atime_nsec  = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	ino->ctime_nsec  = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	ino->mtime_nsec  = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	ino->mode = cpu_to_le32(S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	ino->size = cpu_to_le64(UBIFS_INO_NODE_SZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	/* Set compression enabled by default */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	ino->flags = cpu_to_le32(UBIFS_COMPR_FL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	dbg_gen("root inode created at LEB %d:0",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		main_first + DEFAULT_DATA_LEB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	 * The first node in the log has to be the commit start node. This is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	 * always the case during normal file-system operation. Write a fake
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	 * commit start node to the log.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	cs->ch.node_type = UBIFS_CS_NODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	err = ubifs_write_node_hmac(c, sup, UBIFS_SB_NODE_SZ, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 				    offsetof(struct ubifs_sb_node, hmac));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	err = ubifs_write_node(c, ino, UBIFS_INO_NODE_SZ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 			       main_first + DEFAULT_DATA_LEB, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	ubifs_node_calc_hash(c, ino, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	ubifs_copy_hash(c, hash, ubifs_branch_hash(c, br));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	err = ubifs_write_node(c, idx, idx_node_size, main_first + DEFAULT_IDX_LEB, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	ubifs_node_calc_hash(c, idx, hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	ubifs_copy_hash(c, hash, mst->hash_root_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	err = ubifs_write_node_hmac(c, mst, UBIFS_MST_NODE_SZ, UBIFS_MST_LNUM, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		offsetof(struct ubifs_mst_node, hmac));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	err = ubifs_write_node_hmac(c, mst, UBIFS_MST_NODE_SZ, UBIFS_MST_LNUM + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 			       0, offsetof(struct ubifs_mst_node, hmac));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	err = ubifs_write_node(c, cs, UBIFS_CS_NODE_SZ, UBIFS_LOG_LNUM, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	ubifs_msg(c, "default file-system created");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	kfree(sup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	kfree(mst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	kfree(idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	kfree(ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	kfree(cs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)  * validate_sb - validate superblock node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)  * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)  * @sup: superblock node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)  * This function validates superblock node @sup. Since most of data was read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)  * from the superblock and stored in @c, the function validates fields in @c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)  * instead. Returns zero in case of success and %-EINVAL in case of validation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)  * failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) static int validate_sb(struct ubifs_info *c, struct ubifs_sb_node *sup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	long long max_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	int err = 1, min_leb_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	if (!c->key_hash) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 		err = 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 		goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	if (sup->key_fmt != UBIFS_SIMPLE_KEY_FMT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		err = 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 		goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	if (le32_to_cpu(sup->min_io_size) != c->min_io_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		ubifs_err(c, "min. I/O unit mismatch: %d in superblock, %d real",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 			  le32_to_cpu(sup->min_io_size), c->min_io_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 		goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	if (le32_to_cpu(sup->leb_size) != c->leb_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		ubifs_err(c, "LEB size mismatch: %d in superblock, %d real",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 			  le32_to_cpu(sup->leb_size), c->leb_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	if (c->log_lebs < UBIFS_MIN_LOG_LEBS ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	    c->lpt_lebs < UBIFS_MIN_LPT_LEBS ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	    c->orph_lebs < UBIFS_MIN_ORPH_LEBS ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	    c->main_lebs < UBIFS_MIN_MAIN_LEBS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 		err = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 		goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	 * Calculate minimum allowed amount of main area LEBs. This is very
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	 * similar to %UBIFS_MIN_LEB_CNT, but we take into account real what we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	 * have just read from the superblock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	min_leb_cnt = UBIFS_SB_LEBS + UBIFS_MST_LEBS + c->log_lebs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	min_leb_cnt += c->lpt_lebs + c->orph_lebs + c->jhead_cnt + 6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	if (c->leb_cnt < min_leb_cnt || c->leb_cnt > c->vi.size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 		ubifs_err(c, "bad LEB count: %d in superblock, %d on UBI volume, %d minimum required",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 			  c->leb_cnt, c->vi.size, min_leb_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 		goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 	if (c->max_leb_cnt < c->leb_cnt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 		ubifs_err(c, "max. LEB count %d less than LEB count %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 			  c->max_leb_cnt, c->leb_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 		goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	if (c->main_lebs < UBIFS_MIN_MAIN_LEBS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 		ubifs_err(c, "too few main LEBs count %d, must be at least %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 			  c->main_lebs, UBIFS_MIN_MAIN_LEBS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 		goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	max_bytes = (long long)c->leb_size * UBIFS_MIN_BUD_LEBS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	if (c->max_bud_bytes < max_bytes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 		ubifs_err(c, "too small journal (%lld bytes), must be at least %lld bytes",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 			  c->max_bud_bytes, max_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 		goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	max_bytes = (long long)c->leb_size * c->main_lebs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	if (c->max_bud_bytes > max_bytes) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		ubifs_err(c, "too large journal size (%lld bytes), only %lld bytes available in the main area",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 			  c->max_bud_bytes, max_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 		goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	if (c->jhead_cnt < NONDATA_JHEADS_CNT + 1 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	    c->jhead_cnt > NONDATA_JHEADS_CNT + UBIFS_MAX_JHEADS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 		err = 9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 		goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	if (c->fanout < UBIFS_MIN_FANOUT ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	    ubifs_idx_node_sz(c, c->fanout) > c->leb_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 		err = 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 		goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	if (c->lsave_cnt < 0 || (c->lsave_cnt > DEFAULT_LSAVE_CNT &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	    c->lsave_cnt > c->max_leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	    c->log_lebs - c->lpt_lebs - c->orph_lebs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 		err = 11;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 		goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 	if (UBIFS_SB_LEBS + UBIFS_MST_LEBS + c->log_lebs + c->lpt_lebs +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 	    c->orph_lebs + c->main_lebs != c->leb_cnt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 		err = 12;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 		goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	if (c->default_compr >= UBIFS_COMPR_TYPES_CNT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 		err = 13;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 		goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	if (c->rp_size < 0 || max_bytes < c->rp_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 		err = 14;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 		goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 	if (le32_to_cpu(sup->time_gran) > 1000000000 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	    le32_to_cpu(sup->time_gran) < 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 		err = 15;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 		goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	if (!c->double_hash && c->fmt_version >= 5) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 		err = 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 		goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	if (c->encrypted && c->fmt_version < 5) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 		err = 17;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 		goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	ubifs_err(c, "bad superblock, error %d", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	ubifs_dump_node(c, sup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511)  * ubifs_read_sb_node - read superblock node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512)  * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514)  * This function returns a pointer to the superblock node or a negative error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515)  * code. Note, the user of this function is responsible of kfree()'ing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516)  * returned superblock buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) static struct ubifs_sb_node *ubifs_read_sb_node(struct ubifs_info *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 	struct ubifs_sb_node *sup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 	sup = kmalloc(ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	if (!sup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	err = ubifs_read_node(c, sup, UBIFS_SB_NODE, UBIFS_SB_NODE_SZ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 			      UBIFS_SB_LNUM, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 		kfree(sup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 		return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	return sup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) static int authenticate_sb_node(struct ubifs_info *c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 				const struct ubifs_sb_node *sup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	unsigned int sup_flags = le32_to_cpu(sup->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	u8 hmac_wkm[UBIFS_HMAC_ARR_SZ];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	int authenticated = !!(sup_flags & UBIFS_FLG_AUTHENTICATION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	int hash_algo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	if (c->authenticated && !authenticated) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 		ubifs_err(c, "authenticated FS forced, but found FS without authentication");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	if (!c->authenticated && authenticated) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 		ubifs_err(c, "authenticated FS found, but no key given");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	ubifs_msg(c, "Mounting in %sauthenticated mode",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 		  c->authenticated ? "" : "un");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 	if (!c->authenticated)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	if (!IS_ENABLED(CONFIG_UBIFS_FS_AUTHENTICATION))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	hash_algo = le16_to_cpu(sup->hash_algo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 	if (hash_algo >= HASH_ALGO__LAST) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 		ubifs_err(c, "superblock uses unknown hash algo %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 			  hash_algo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 	if (strcmp(hash_algo_name[hash_algo], c->auth_hash_name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 		ubifs_err(c, "This filesystem uses %s for hashing,"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 			     " but %s is specified", hash_algo_name[hash_algo],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 			     c->auth_hash_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 	 * The super block node can either be authenticated by a HMAC or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 	 * by a signature in a ubifs_sig_node directly following the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 	 * super block node to support offline image creation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 	if (ubifs_hmac_zero(c, sup->hmac)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 		err = ubifs_sb_verify_signature(c, sup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 		err = ubifs_hmac_wkm(c, hmac_wkm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 		if (ubifs_check_hmac(c, hmac_wkm, sup->hmac_wkm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 			ubifs_err(c, "provided key does not fit");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 			return -ENOKEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 		err = ubifs_node_verify_hmac(c, sup, sizeof(*sup),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 					     offsetof(struct ubifs_sb_node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 						      hmac));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 		ubifs_err(c, "Failed to authenticate superblock: %d", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606)  * ubifs_write_sb_node - write superblock node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607)  * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608)  * @sup: superblock node read with 'ubifs_read_sb_node()'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610)  * This function returns %0 on success and a negative error code on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) int ubifs_write_sb_node(struct ubifs_info *c, struct ubifs_sb_node *sup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 	int len = ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 	err = ubifs_prepare_node_hmac(c, sup, UBIFS_SB_NODE_SZ,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 				      offsetof(struct ubifs_sb_node, hmac), 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 	return ubifs_leb_change(c, UBIFS_SB_LNUM, sup, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626)  * ubifs_read_superblock - read superblock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627)  * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629)  * This function finds, reads and checks the superblock. If an empty UBI volume
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630)  * is being mounted, this function creates default superblock. Returns zero in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631)  * case of success, and a negative error code in case of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) int ubifs_read_superblock(struct ubifs_info *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 	int err, sup_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 	struct ubifs_sb_node *sup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 	if (c->empty) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 		err = create_default_filesystem(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 	sup = ubifs_read_sb_node(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) 	if (IS_ERR(sup))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 		return PTR_ERR(sup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 	c->sup_node = sup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 	c->fmt_version = le32_to_cpu(sup->fmt_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 	c->ro_compat_version = le32_to_cpu(sup->ro_compat_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 	 * The software supports all previous versions but not future versions,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 	 * due to the unavailability of time-travelling equipment.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 	if (c->fmt_version > UBIFS_FORMAT_VERSION) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 		ubifs_assert(c, !c->ro_media || c->ro_mount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 		if (!c->ro_mount ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 		    c->ro_compat_version > UBIFS_RO_COMPAT_VERSION) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 			ubifs_err(c, "on-flash format version is w%d/r%d, but software only supports up to version w%d/r%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 				  c->fmt_version, c->ro_compat_version,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 				  UBIFS_FORMAT_VERSION,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 				  UBIFS_RO_COMPAT_VERSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 			if (c->ro_compat_version <= UBIFS_RO_COMPAT_VERSION) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 				ubifs_msg(c, "only R/O mounting is possible");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 				err = -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 			} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 				err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 		 * The FS is mounted R/O, and the media format is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 		 * R/O-compatible with the UBIFS implementation, so we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 		 * mount.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 		c->rw_incompat = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 	if (c->fmt_version < 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 		ubifs_err(c, "on-flash format version %d is not supported",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 			  c->fmt_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 	switch (sup->key_hash) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 	case UBIFS_KEY_HASH_R5:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 		c->key_hash = key_r5_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 		c->key_hash_type = UBIFS_KEY_HASH_R5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 	case UBIFS_KEY_HASH_TEST:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 		c->key_hash = key_test_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 		c->key_hash_type = UBIFS_KEY_HASH_TEST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 	c->key_fmt = sup->key_fmt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 	switch (c->key_fmt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 	case UBIFS_SIMPLE_KEY_FMT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 		c->key_len = UBIFS_SK_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 		ubifs_err(c, "unsupported key format");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 	c->leb_cnt       = le32_to_cpu(sup->leb_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 	c->max_leb_cnt   = le32_to_cpu(sup->max_leb_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 	c->max_bud_bytes = le64_to_cpu(sup->max_bud_bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 	c->log_lebs      = le32_to_cpu(sup->log_lebs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 	c->lpt_lebs      = le32_to_cpu(sup->lpt_lebs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 	c->orph_lebs     = le32_to_cpu(sup->orph_lebs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 	c->jhead_cnt     = le32_to_cpu(sup->jhead_cnt) + NONDATA_JHEADS_CNT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 	c->fanout        = le32_to_cpu(sup->fanout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 	c->lsave_cnt     = le32_to_cpu(sup->lsave_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 	c->rp_size       = le64_to_cpu(sup->rp_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 	c->rp_uid        = make_kuid(&init_user_ns, le32_to_cpu(sup->rp_uid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 	c->rp_gid        = make_kgid(&init_user_ns, le32_to_cpu(sup->rp_gid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 	sup_flags        = le32_to_cpu(sup->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 	if (!c->mount_opts.override_compr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 		c->default_compr = le16_to_cpu(sup->default_compr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 	c->vfs_sb->s_time_gran = le32_to_cpu(sup->time_gran);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 	memcpy(&c->uuid, &sup->uuid, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 	c->big_lpt = !!(sup_flags & UBIFS_FLG_BIGLPT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 	c->space_fixup = !!(sup_flags & UBIFS_FLG_SPACE_FIXUP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 	c->double_hash = !!(sup_flags & UBIFS_FLG_DOUBLE_HASH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 	c->encrypted = !!(sup_flags & UBIFS_FLG_ENCRYPTION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 	err = authenticate_sb_node(c, sup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 	if ((sup_flags & ~UBIFS_FLG_MASK) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 		ubifs_err(c, "Unknown feature flags found: %#x",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 			  sup_flags & ~UBIFS_FLG_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 	if (!IS_ENABLED(CONFIG_FS_ENCRYPTION) && c->encrypted) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 		ubifs_err(c, "file system contains encrypted files but UBIFS"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 			     " was built without crypto support.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 		err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) 	/* Automatically increase file system size to the maximum size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) 	if (c->leb_cnt < c->vi.size && c->leb_cnt < c->max_leb_cnt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 		int old_leb_cnt = c->leb_cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) 		c->leb_cnt = min_t(int, c->max_leb_cnt, c->vi.size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) 		sup->leb_cnt = cpu_to_le32(c->leb_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) 		c->superblock_need_write = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 		dbg_mnt("Auto resizing from %d LEBs to %d LEBs",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) 			old_leb_cnt, c->leb_cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 	c->log_bytes = (long long)c->log_lebs * c->leb_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) 	c->log_last = UBIFS_LOG_LNUM + c->log_lebs - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) 	c->lpt_first = UBIFS_LOG_LNUM + c->log_lebs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) 	c->lpt_last = c->lpt_first + c->lpt_lebs - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) 	c->orph_first = c->lpt_last + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) 	c->orph_last = c->orph_first + c->orph_lebs - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) 	c->main_lebs = c->leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) 	c->main_lebs -= c->log_lebs + c->lpt_lebs + c->orph_lebs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) 	c->main_first = c->leb_cnt - c->main_lebs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) 	err = validate_sb(c, sup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782)  * fixup_leb - fixup/unmap an LEB containing free space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783)  * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784)  * @lnum: the LEB number to fix up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785)  * @len: number of used bytes in LEB (starting at offset 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787)  * This function reads the contents of the given LEB number @lnum, then fixes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788)  * it up, so that empty min. I/O units in the end of LEB are actually erased on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789)  * flash (rather than being just all-0xff real data). If the LEB is completely
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790)  * empty, it is simply unmapped.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) static int fixup_leb(struct ubifs_info *c, int lnum, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) 	ubifs_assert(c, len >= 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) 	ubifs_assert(c, len % c->min_io_size == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) 	ubifs_assert(c, len < c->leb_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) 	if (len == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) 		dbg_mnt("unmap empty LEB %d", lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) 		return ubifs_leb_unmap(c, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) 	dbg_mnt("fixup LEB %d, data len %d", lnum, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) 	err = ubifs_leb_read(c, lnum, c->sbuf, 0, len, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) 	return ubifs_leb_change(c, lnum, c->sbuf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814)  * fixup_free_space - find & remap all LEBs containing free space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815)  * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817)  * This function walks through all LEBs in the filesystem and fiexes up those
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818)  * containing free/empty space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) static int fixup_free_space(struct ubifs_info *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) 	int lnum, err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) 	struct ubifs_lprops *lprops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) 	ubifs_get_lprops(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) 	/* Fixup LEBs in the master area */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) 	for (lnum = UBIFS_MST_LNUM; lnum < UBIFS_LOG_LNUM; lnum++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) 		err = fixup_leb(c, lnum, c->mst_offs + c->mst_node_alsz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) 	/* Unmap unused log LEBs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) 	lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) 	while (lnum != c->ltail_lnum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) 		err = fixup_leb(c, lnum, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) 		lnum = ubifs_next_log_lnum(c, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) 	 * Fixup the log head which contains the only a CS node at the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) 	 * beginning.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) 	err = fixup_leb(c, c->lhead_lnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) 			ALIGN(UBIFS_CS_NODE_SZ, c->min_io_size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) 	/* Fixup LEBs in the LPT area */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) 	for (lnum = c->lpt_first; lnum <= c->lpt_last; lnum++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) 		int free = c->ltab[lnum - c->lpt_first].free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) 		if (free > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) 			err = fixup_leb(c, lnum, c->leb_size - free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) 	/* Unmap LEBs in the orphans area */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) 	for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) 		err = fixup_leb(c, lnum, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) 	/* Fixup LEBs in the main area */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) 	for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) 		lprops = ubifs_lpt_lookup(c, lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) 		if (IS_ERR(lprops)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) 			err = PTR_ERR(lprops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) 		if (lprops->free > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) 			err = fixup_leb(c, lnum, c->leb_size - lprops->free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) 	ubifs_release_lprops(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891)  * ubifs_fixup_free_space - find & fix all LEBs with free space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892)  * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894)  * This function fixes up LEBs containing free space on first mount, if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895)  * appropriate flag was set when the FS was created. Each LEB with one or more
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896)  * empty min. I/O unit (i.e. free-space-count > 0) is re-written, to make sure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897)  * the free space is actually erased. E.g., this is necessary for some NAND
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898)  * chips, since the free space may have been programmed like real "0xff" data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899)  * (generating a non-0xff ECC), causing future writes to the not-really-erased
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900)  * NAND pages to behave badly. After the space is fixed up, the superblock flag
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901)  * is cleared, so that this is skipped for all future mounts.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) int ubifs_fixup_free_space(struct ubifs_info *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) 	struct ubifs_sb_node *sup = c->sup_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) 	ubifs_assert(c, c->space_fixup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) 	ubifs_assert(c, !c->ro_mount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) 	ubifs_msg(c, "start fixing up free space");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) 	err = fixup_free_space(c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) 	/* Free-space fixup is no longer required */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) 	c->space_fixup = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) 	sup->flags &= cpu_to_le32(~UBIFS_FLG_SPACE_FIXUP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) 	c->superblock_need_write = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) 	ubifs_msg(c, "free space fixup complete");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) int ubifs_enable_encryption(struct ubifs_info *c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) 	struct ubifs_sb_node *sup = c->sup_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) 	if (!IS_ENABLED(CONFIG_FS_ENCRYPTION))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) 		return -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) 	if (c->encrypted)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) 	if (c->ro_mount || c->ro_media)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) 		return -EROFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) 	if (c->fmt_version < 5) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) 		ubifs_err(c, "on-flash format version 5 is needed for encryption");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) 	sup->flags |= cpu_to_le32(UBIFS_FLG_ENCRYPTION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) 	err = ubifs_write_sb_node(c, sup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) 		c->encrypted = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) }