12b27bdccSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21e51764aSArtem Bityutskiy /*
31e51764aSArtem Bityutskiy * This file is part of UBIFS.
41e51764aSArtem Bityutskiy *
51e51764aSArtem Bityutskiy * Copyright (C) 2006-2008 Nokia Corporation.
61e51764aSArtem Bityutskiy *
71e51764aSArtem Bityutskiy * Authors: Artem Bityutskiy (Битюцкий Артём)
81e51764aSArtem Bityutskiy * Adrian Hunter
91e51764aSArtem Bityutskiy */
101e51764aSArtem Bityutskiy
111e51764aSArtem Bityutskiy /*
121e51764aSArtem Bityutskiy * This file implements UBIFS superblock. The superblock is stored at the first
131e51764aSArtem Bityutskiy * LEB of the volume and is never changed by UBIFS. Only user-space tools may
141e51764aSArtem Bityutskiy * change it. The superblock node mostly contains geometry information.
151e51764aSArtem Bityutskiy */
161e51764aSArtem Bityutskiy
171e51764aSArtem Bityutskiy #include "ubifs.h"
185a0e3ad6STejun Heo #include <linux/slab.h>
194d61db4fSArtem Bityutskiy #include <linux/math64.h>
208da4b8c4SAndy Shevchenko #include <linux/uuid.h>
211e51764aSArtem Bityutskiy
221e51764aSArtem Bityutskiy /*
231e51764aSArtem Bityutskiy * Default journal size in logical eraseblocks as a percent of total
241e51764aSArtem Bityutskiy * flash size.
251e51764aSArtem Bityutskiy */
261e51764aSArtem Bityutskiy #define DEFAULT_JNL_PERCENT 5
271e51764aSArtem Bityutskiy
281e51764aSArtem Bityutskiy /* Default maximum journal size in bytes */
291e51764aSArtem Bityutskiy #define DEFAULT_MAX_JNL (32*1024*1024)
301e51764aSArtem Bityutskiy
311e51764aSArtem Bityutskiy /* Default indexing tree fanout */
321e51764aSArtem Bityutskiy #define DEFAULT_FANOUT 8
331e51764aSArtem Bityutskiy
341e51764aSArtem Bityutskiy /* Default number of data journal heads */
351e51764aSArtem Bityutskiy #define DEFAULT_JHEADS_CNT 1
361e51764aSArtem Bityutskiy
371e51764aSArtem Bityutskiy /* Default positions of different LEBs in the main area */
381e51764aSArtem Bityutskiy #define DEFAULT_IDX_LEB 0
391e51764aSArtem Bityutskiy #define DEFAULT_DATA_LEB 1
401e51764aSArtem Bityutskiy #define DEFAULT_GC_LEB 2
411e51764aSArtem Bityutskiy
421e51764aSArtem Bityutskiy /* Default number of LEB numbers in LPT's save table */
431e51764aSArtem Bityutskiy #define DEFAULT_LSAVE_CNT 256
441e51764aSArtem Bityutskiy
451e51764aSArtem Bityutskiy /* Default reserved pool size as a percent of maximum free space */
461e51764aSArtem Bityutskiy #define DEFAULT_RP_PERCENT 5
471e51764aSArtem Bityutskiy
481e51764aSArtem Bityutskiy /* The default maximum size of reserved pool in bytes */
491e51764aSArtem Bityutskiy #define DEFAULT_MAX_RP_SIZE (5*1024*1024)
501e51764aSArtem Bityutskiy
511e51764aSArtem Bityutskiy /* Default time granularity in nanoseconds */
521e51764aSArtem Bityutskiy #define DEFAULT_TIME_GRAN 1000000000
531e51764aSArtem Bityutskiy
get_default_compressor(struct ubifs_info * c)54d62e98edSGabor Juhos static int get_default_compressor(struct ubifs_info *c)
55d62e98edSGabor Juhos {
56*ba4884a6SRui Salvaterra if (ubifs_compr_present(c, UBIFS_COMPR_ZSTD))
57*ba4884a6SRui Salvaterra return UBIFS_COMPR_ZSTD;
58*ba4884a6SRui Salvaterra
59d62e98edSGabor Juhos if (ubifs_compr_present(c, UBIFS_COMPR_LZO))
60d62e98edSGabor Juhos return UBIFS_COMPR_LZO;
61d62e98edSGabor Juhos
62d62e98edSGabor Juhos if (ubifs_compr_present(c, UBIFS_COMPR_ZLIB))
63d62e98edSGabor Juhos return UBIFS_COMPR_ZLIB;
64d62e98edSGabor Juhos
65d62e98edSGabor Juhos return UBIFS_COMPR_NONE;
66d62e98edSGabor Juhos }
67d62e98edSGabor Juhos
681e51764aSArtem Bityutskiy /**
691e51764aSArtem Bityutskiy * create_default_filesystem - format empty UBI volume.
701e51764aSArtem Bityutskiy * @c: UBIFS file-system description object
711e51764aSArtem Bityutskiy *
721e51764aSArtem Bityutskiy * This function creates default empty file-system. Returns zero in case of
731e51764aSArtem Bityutskiy * success and a negative error code in case of failure.
741e51764aSArtem Bityutskiy */
create_default_filesystem(struct ubifs_info * c)751e51764aSArtem Bityutskiy static int create_default_filesystem(struct ubifs_info *c)
761e51764aSArtem Bityutskiy {
771e51764aSArtem Bityutskiy struct ubifs_sb_node *sup;
781e51764aSArtem Bityutskiy struct ubifs_mst_node *mst;
791e51764aSArtem Bityutskiy struct ubifs_idx_node *idx;
801e51764aSArtem Bityutskiy struct ubifs_branch *br;
811e51764aSArtem Bityutskiy struct ubifs_ino_node *ino;
821e51764aSArtem Bityutskiy struct ubifs_cs_node *cs;
831e51764aSArtem Bityutskiy union ubifs_key key;
841e51764aSArtem Bityutskiy int err, tmp, jnl_lebs, log_lebs, max_buds, main_lebs, main_first;
851e51764aSArtem Bityutskiy int lpt_lebs, lpt_first, orph_lebs, big_lpt, ino_waste, sup_flags = 0;
861e51764aSArtem Bityutskiy int min_leb_cnt = UBIFS_MIN_LEB_CNT;
87c4de6d7eSSascha Hauer int idx_node_size;
884d61db4fSArtem Bityutskiy long long tmp64, main_bytes;
890ecb9529SHarvey Harrison __le64 tmp_le64;
900eca0b80SArnd Bergmann struct timespec64 ts;
91104115a3SSascha Hauer u8 hash[UBIFS_HASH_ARR_SZ];
92b5b1f083SSascha Hauer u8 hash_lpt[UBIFS_HASH_ARR_SZ];
931e51764aSArtem Bityutskiy
941e51764aSArtem Bityutskiy /* Some functions called from here depend on the @c->key_len filed */
951e51764aSArtem Bityutskiy c->key_len = UBIFS_SK_LEN;
961e51764aSArtem Bityutskiy
971e51764aSArtem Bityutskiy /*
981e51764aSArtem Bityutskiy * First of all, we have to calculate default file-system geometry -
991e51764aSArtem Bityutskiy * log size, journal size, etc.
1001e51764aSArtem Bityutskiy */
1011e51764aSArtem Bityutskiy if (c->leb_cnt < 0x7FFFFFFF / DEFAULT_JNL_PERCENT)
1021e51764aSArtem Bityutskiy /* We can first multiply then divide and have no overflow */
1031e51764aSArtem Bityutskiy jnl_lebs = c->leb_cnt * DEFAULT_JNL_PERCENT / 100;
1041e51764aSArtem Bityutskiy else
1051e51764aSArtem Bityutskiy jnl_lebs = (c->leb_cnt / 100) * DEFAULT_JNL_PERCENT;
1061e51764aSArtem Bityutskiy
1071e51764aSArtem Bityutskiy if (jnl_lebs < UBIFS_MIN_JNL_LEBS)
1081e51764aSArtem Bityutskiy jnl_lebs = UBIFS_MIN_JNL_LEBS;
1091e51764aSArtem Bityutskiy if (jnl_lebs * c->leb_size > DEFAULT_MAX_JNL)
1101e51764aSArtem Bityutskiy jnl_lebs = DEFAULT_MAX_JNL / c->leb_size;
1111e51764aSArtem Bityutskiy
1121e51764aSArtem Bityutskiy /*
1131e51764aSArtem Bityutskiy * The log should be large enough to fit reference nodes for all bud
1141e51764aSArtem Bityutskiy * LEBs. Because buds do not have to start from the beginning of LEBs
1151e51764aSArtem Bityutskiy * (half of the LEB may contain committed data), the log should
1161e51764aSArtem Bityutskiy * generally be larger, make it twice as large.
1171e51764aSArtem Bityutskiy */
1181e51764aSArtem Bityutskiy tmp = 2 * (c->ref_node_alsz * jnl_lebs) + c->leb_size - 1;
1191e51764aSArtem Bityutskiy log_lebs = tmp / c->leb_size;
1201e51764aSArtem Bityutskiy /* Plus one LEB reserved for commit */
1211e51764aSArtem Bityutskiy log_lebs += 1;
1221e51764aSArtem Bityutskiy if (c->leb_cnt - min_leb_cnt > 8) {
1231e51764aSArtem Bityutskiy /* And some extra space to allow writes while committing */
1241e51764aSArtem Bityutskiy log_lebs += 1;
1251e51764aSArtem Bityutskiy min_leb_cnt += 1;
1261e51764aSArtem Bityutskiy }
1271e51764aSArtem Bityutskiy
1281e51764aSArtem Bityutskiy max_buds = jnl_lebs - log_lebs;
1291e51764aSArtem Bityutskiy if (max_buds < UBIFS_MIN_BUD_LEBS)
1301e51764aSArtem Bityutskiy max_buds = UBIFS_MIN_BUD_LEBS;
1311e51764aSArtem Bityutskiy
1321e51764aSArtem Bityutskiy /*
1331e51764aSArtem Bityutskiy * Orphan nodes are stored in a separate area. One node can store a lot
1341e51764aSArtem Bityutskiy * of orphan inode numbers, but when new orphan comes we just add a new
1351e51764aSArtem Bityutskiy * orphan node. At some point the nodes are consolidated into one
1361e51764aSArtem Bityutskiy * orphan node.
1371e51764aSArtem Bityutskiy */
1381e51764aSArtem Bityutskiy orph_lebs = UBIFS_MIN_ORPH_LEBS;
1391e51764aSArtem Bityutskiy if (c->leb_cnt - min_leb_cnt > 1)
1401e51764aSArtem Bityutskiy /*
1411e51764aSArtem Bityutskiy * For debugging purposes it is better to have at least 2
1421e51764aSArtem Bityutskiy * orphan LEBs, because the orphan subsystem would need to do
1431e51764aSArtem Bityutskiy * consolidations and would be stressed more.
1441e51764aSArtem Bityutskiy */
1451e51764aSArtem Bityutskiy orph_lebs += 1;
1461e51764aSArtem Bityutskiy
1471e51764aSArtem Bityutskiy main_lebs = c->leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS - log_lebs;
1481e51764aSArtem Bityutskiy main_lebs -= orph_lebs;
1491e51764aSArtem Bityutskiy
1501e51764aSArtem Bityutskiy lpt_first = UBIFS_LOG_LNUM + log_lebs;
1511e51764aSArtem Bityutskiy c->lsave_cnt = DEFAULT_LSAVE_CNT;
1521e51764aSArtem Bityutskiy c->max_leb_cnt = c->leb_cnt;
1531e51764aSArtem Bityutskiy err = ubifs_create_dflt_lpt(c, &main_lebs, lpt_first, &lpt_lebs,
154b5b1f083SSascha Hauer &big_lpt, hash_lpt);
1551e51764aSArtem Bityutskiy if (err)
1561e51764aSArtem Bityutskiy return err;
1571e51764aSArtem Bityutskiy
1581e51764aSArtem Bityutskiy dbg_gen("LEB Properties Tree created (LEBs %d-%d)", lpt_first,
1591e51764aSArtem Bityutskiy lpt_first + lpt_lebs - 1);
1601e51764aSArtem Bityutskiy
1611e51764aSArtem Bityutskiy main_first = c->leb_cnt - main_lebs;
1621e51764aSArtem Bityutskiy
163c4de6d7eSSascha Hauer sup = kzalloc(ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size), GFP_KERNEL);
164c4de6d7eSSascha Hauer mst = kzalloc(c->mst_node_alsz, GFP_KERNEL);
165c4de6d7eSSascha Hauer idx_node_size = ubifs_idx_node_sz(c, 1);
166edec5137SSascha Hauer idx = kzalloc(ALIGN(idx_node_size, c->min_io_size), GFP_KERNEL);
167c4de6d7eSSascha Hauer ino = kzalloc(ALIGN(UBIFS_INO_NODE_SZ, c->min_io_size), GFP_KERNEL);
168c4de6d7eSSascha Hauer cs = kzalloc(ALIGN(UBIFS_CS_NODE_SZ, c->min_io_size), GFP_KERNEL);
169c4de6d7eSSascha Hauer
170c4de6d7eSSascha Hauer if (!sup || !mst || !idx || !ino || !cs) {
171c4de6d7eSSascha Hauer err = -ENOMEM;
172c4de6d7eSSascha Hauer goto out;
173c4de6d7eSSascha Hauer }
174c4de6d7eSSascha Hauer
1751e51764aSArtem Bityutskiy /* Create default superblock */
1761e51764aSArtem Bityutskiy
1774d61db4fSArtem Bityutskiy tmp64 = (long long)max_buds * c->leb_size;
1781e51764aSArtem Bityutskiy if (big_lpt)
1791e51764aSArtem Bityutskiy sup_flags |= UBIFS_FLG_BIGLPT;
180a7a8f4a1SMartin Kaistra if (ubifs_default_version > 4)
181d63d61c1SRichard Weinberger sup_flags |= UBIFS_FLG_DOUBLE_HASH;
1821e51764aSArtem Bityutskiy
183104115a3SSascha Hauer if (ubifs_authenticated(c)) {
184104115a3SSascha Hauer sup_flags |= UBIFS_FLG_AUTHENTICATION;
185104115a3SSascha Hauer sup->hash_algo = cpu_to_le16(c->auth_hash_algo);
186104115a3SSascha Hauer err = ubifs_hmac_wkm(c, sup->hmac_wkm);
187104115a3SSascha Hauer if (err)
188104115a3SSascha Hauer goto out;
189104115a3SSascha Hauer } else {
1907cc7720fSBen Dooks (Codethink) sup->hash_algo = cpu_to_le16(0xffff);
191104115a3SSascha Hauer }
192104115a3SSascha Hauer
1931e51764aSArtem Bityutskiy sup->ch.node_type = UBIFS_SB_NODE;
1941e51764aSArtem Bityutskiy sup->key_hash = UBIFS_KEY_HASH_R5;
1951e51764aSArtem Bityutskiy sup->flags = cpu_to_le32(sup_flags);
1961e51764aSArtem Bityutskiy sup->min_io_size = cpu_to_le32(c->min_io_size);
1971e51764aSArtem Bityutskiy sup->leb_size = cpu_to_le32(c->leb_size);
1981e51764aSArtem Bityutskiy sup->leb_cnt = cpu_to_le32(c->leb_cnt);
1991e51764aSArtem Bityutskiy sup->max_leb_cnt = cpu_to_le32(c->max_leb_cnt);
2001e51764aSArtem Bityutskiy sup->max_bud_bytes = cpu_to_le64(tmp64);
2011e51764aSArtem Bityutskiy sup->log_lebs = cpu_to_le32(log_lebs);
2021e51764aSArtem Bityutskiy sup->lpt_lebs = cpu_to_le32(lpt_lebs);
2031e51764aSArtem Bityutskiy sup->orph_lebs = cpu_to_le32(orph_lebs);
2041e51764aSArtem Bityutskiy sup->jhead_cnt = cpu_to_le32(DEFAULT_JHEADS_CNT);
2051e51764aSArtem Bityutskiy sup->fanout = cpu_to_le32(DEFAULT_FANOUT);
2061e51764aSArtem Bityutskiy sup->lsave_cnt = cpu_to_le32(c->lsave_cnt);
207a7a8f4a1SMartin Kaistra sup->fmt_version = cpu_to_le32(ubifs_default_version);
2081e51764aSArtem Bityutskiy sup->time_gran = cpu_to_le32(DEFAULT_TIME_GRAN);
209553dea4dSArtem Bityutskiy if (c->mount_opts.override_compr)
210553dea4dSArtem Bityutskiy sup->default_compr = cpu_to_le16(c->mount_opts.compr_type);
211553dea4dSArtem Bityutskiy else
212d62e98edSGabor Juhos sup->default_compr = cpu_to_le16(get_default_compressor(c));
2131e51764aSArtem Bityutskiy
2141e51764aSArtem Bityutskiy generate_random_uuid(sup->uuid);
2151e51764aSArtem Bityutskiy
2164d61db4fSArtem Bityutskiy main_bytes = (long long)main_lebs * c->leb_size;
2174d61db4fSArtem Bityutskiy tmp64 = div_u64(main_bytes * DEFAULT_RP_PERCENT, 100);
2181e51764aSArtem Bityutskiy if (tmp64 > DEFAULT_MAX_RP_SIZE)
2191e51764aSArtem Bityutskiy tmp64 = DEFAULT_MAX_RP_SIZE;
2201e51764aSArtem Bityutskiy sup->rp_size = cpu_to_le64(tmp64);
221963f0cf6SArtem Bityutskiy sup->ro_compat_version = cpu_to_le32(UBIFS_RO_COMPAT_VERSION);
2221e51764aSArtem Bityutskiy
2231e51764aSArtem Bityutskiy dbg_gen("default superblock created at LEB 0:0");
2241e51764aSArtem Bityutskiy
2251e51764aSArtem Bityutskiy /* Create default master node */
2261e51764aSArtem Bityutskiy
2271e51764aSArtem Bityutskiy mst->ch.node_type = UBIFS_MST_NODE;
2281e51764aSArtem Bityutskiy mst->log_lnum = cpu_to_le32(UBIFS_LOG_LNUM);
2291e51764aSArtem Bityutskiy mst->highest_inum = cpu_to_le64(UBIFS_FIRST_INO);
2301e51764aSArtem Bityutskiy mst->cmt_no = 0;
2311e51764aSArtem Bityutskiy mst->root_lnum = cpu_to_le32(main_first + DEFAULT_IDX_LEB);
2321e51764aSArtem Bityutskiy mst->root_offs = 0;
2331e51764aSArtem Bityutskiy tmp = ubifs_idx_node_sz(c, 1);
2341e51764aSArtem Bityutskiy mst->root_len = cpu_to_le32(tmp);
2351e51764aSArtem Bityutskiy mst->gc_lnum = cpu_to_le32(main_first + DEFAULT_GC_LEB);
2361e51764aSArtem Bityutskiy mst->ihead_lnum = cpu_to_le32(main_first + DEFAULT_IDX_LEB);
2371e51764aSArtem Bityutskiy mst->ihead_offs = cpu_to_le32(ALIGN(tmp, c->min_io_size));
2381e51764aSArtem Bityutskiy mst->index_size = cpu_to_le64(ALIGN(tmp, 8));
2391e51764aSArtem Bityutskiy mst->lpt_lnum = cpu_to_le32(c->lpt_lnum);
2401e51764aSArtem Bityutskiy mst->lpt_offs = cpu_to_le32(c->lpt_offs);
2411e51764aSArtem Bityutskiy mst->nhead_lnum = cpu_to_le32(c->nhead_lnum);
2421e51764aSArtem Bityutskiy mst->nhead_offs = cpu_to_le32(c->nhead_offs);
2431e51764aSArtem Bityutskiy mst->ltab_lnum = cpu_to_le32(c->ltab_lnum);
2441e51764aSArtem Bityutskiy mst->ltab_offs = cpu_to_le32(c->ltab_offs);
2451e51764aSArtem Bityutskiy mst->lsave_lnum = cpu_to_le32(c->lsave_lnum);
2461e51764aSArtem Bityutskiy mst->lsave_offs = cpu_to_le32(c->lsave_offs);
2471e51764aSArtem Bityutskiy mst->lscan_lnum = cpu_to_le32(main_first);
2481e51764aSArtem Bityutskiy mst->empty_lebs = cpu_to_le32(main_lebs - 2);
2491e51764aSArtem Bityutskiy mst->idx_lebs = cpu_to_le32(1);
2501e51764aSArtem Bityutskiy mst->leb_cnt = cpu_to_le32(c->leb_cnt);
251104115a3SSascha Hauer ubifs_copy_hash(c, hash_lpt, mst->hash_lpt);
2521e51764aSArtem Bityutskiy
2531e51764aSArtem Bityutskiy /* Calculate lprops statistics */
2541e51764aSArtem Bityutskiy tmp64 = main_bytes;
2551e51764aSArtem Bityutskiy tmp64 -= ALIGN(ubifs_idx_node_sz(c, 1), c->min_io_size);
2561e51764aSArtem Bityutskiy tmp64 -= ALIGN(UBIFS_INO_NODE_SZ, c->min_io_size);
2571e51764aSArtem Bityutskiy mst->total_free = cpu_to_le64(tmp64);
2581e51764aSArtem Bityutskiy
2591e51764aSArtem Bityutskiy tmp64 = ALIGN(ubifs_idx_node_sz(c, 1), c->min_io_size);
2601e51764aSArtem Bityutskiy ino_waste = ALIGN(UBIFS_INO_NODE_SZ, c->min_io_size) -
2611e51764aSArtem Bityutskiy UBIFS_INO_NODE_SZ;
2621e51764aSArtem Bityutskiy tmp64 += ino_waste;
2631e51764aSArtem Bityutskiy tmp64 -= ALIGN(ubifs_idx_node_sz(c, 1), 8);
2641e51764aSArtem Bityutskiy mst->total_dirty = cpu_to_le64(tmp64);
2651e51764aSArtem Bityutskiy
2661e51764aSArtem Bityutskiy /* The indexing LEB does not contribute to dark space */
2677606f85aSsrimugunthan dhandapani tmp64 = ((long long)(c->main_lebs - 1) * c->dark_wm);
2681e51764aSArtem Bityutskiy mst->total_dark = cpu_to_le64(tmp64);
2691e51764aSArtem Bityutskiy
2701e51764aSArtem Bityutskiy mst->total_used = cpu_to_le64(UBIFS_INO_NODE_SZ);
2711e51764aSArtem Bityutskiy
2721e51764aSArtem Bityutskiy dbg_gen("default master node created at LEB %d:0", UBIFS_MST_LNUM);
2731e51764aSArtem Bityutskiy
2741e51764aSArtem Bityutskiy /* Create the root indexing node */
2751e51764aSArtem Bityutskiy
2761e51764aSArtem Bityutskiy c->key_fmt = UBIFS_SIMPLE_KEY_FMT;
2771e51764aSArtem Bityutskiy c->key_hash = key_r5_hash;
2781e51764aSArtem Bityutskiy
2791e51764aSArtem Bityutskiy idx->ch.node_type = UBIFS_IDX_NODE;
2801e51764aSArtem Bityutskiy idx->child_cnt = cpu_to_le16(1);
2811e51764aSArtem Bityutskiy ino_key_init(c, &key, UBIFS_ROOT_INO);
2821e51764aSArtem Bityutskiy br = ubifs_idx_branch(c, idx, 0);
2831e51764aSArtem Bityutskiy key_write_idx(c, &key, &br->key);
2841e51764aSArtem Bityutskiy br->lnum = cpu_to_le32(main_first + DEFAULT_DATA_LEB);
2851e51764aSArtem Bityutskiy br->len = cpu_to_le32(UBIFS_INO_NODE_SZ);
2861e51764aSArtem Bityutskiy
2871e51764aSArtem Bityutskiy dbg_gen("default root indexing node created LEB %d:0",
2881e51764aSArtem Bityutskiy main_first + DEFAULT_IDX_LEB);
2891e51764aSArtem Bityutskiy
2901e51764aSArtem Bityutskiy /* Create default root inode */
2911e51764aSArtem Bityutskiy
2921e51764aSArtem Bityutskiy ino_key_init_flash(c, &ino->key, UBIFS_ROOT_INO);
2931e51764aSArtem Bityutskiy ino->ch.node_type = UBIFS_INO_NODE;
2941e51764aSArtem Bityutskiy ino->creat_sqnum = cpu_to_le64(++c->max_sqnum);
2951e51764aSArtem Bityutskiy ino->nlink = cpu_to_le32(2);
296607a11adSDeepa Dinamani
2971bfad0c0SDeepa Dinamani ktime_get_coarse_real_ts64(&ts);
298607a11adSDeepa Dinamani tmp_le64 = cpu_to_le64(ts.tv_sec);
2990ecb9529SHarvey Harrison ino->atime_sec = tmp_le64;
3000ecb9529SHarvey Harrison ino->ctime_sec = tmp_le64;
3010ecb9529SHarvey Harrison ino->mtime_sec = tmp_le64;
3021bfad0c0SDeepa Dinamani ino->atime_nsec = 0;
3031bfad0c0SDeepa Dinamani ino->ctime_nsec = 0;
3041bfad0c0SDeepa Dinamani ino->mtime_nsec = 0;
3051e51764aSArtem Bityutskiy ino->mode = cpu_to_le32(S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO);
3061e51764aSArtem Bityutskiy ino->size = cpu_to_le64(UBIFS_INO_NODE_SZ);
3071e51764aSArtem Bityutskiy
3081e51764aSArtem Bityutskiy /* Set compression enabled by default */
3091e51764aSArtem Bityutskiy ino->flags = cpu_to_le32(UBIFS_COMPR_FL);
3101e51764aSArtem Bityutskiy
3111e51764aSArtem Bityutskiy dbg_gen("root inode created at LEB %d:0",
3121e51764aSArtem Bityutskiy main_first + DEFAULT_DATA_LEB);
3131e51764aSArtem Bityutskiy
3141e51764aSArtem Bityutskiy /*
3151e51764aSArtem Bityutskiy * The first node in the log has to be the commit start node. This is
3161e51764aSArtem Bityutskiy * always the case during normal file-system operation. Write a fake
3171e51764aSArtem Bityutskiy * commit start node to the log.
3181e51764aSArtem Bityutskiy */
3191e51764aSArtem Bityutskiy
3201e51764aSArtem Bityutskiy cs->ch.node_type = UBIFS_CS_NODE;
321c4de6d7eSSascha Hauer
322104115a3SSascha Hauer err = ubifs_write_node_hmac(c, sup, UBIFS_SB_NODE_SZ, 0, 0,
323104115a3SSascha Hauer offsetof(struct ubifs_sb_node, hmac));
324c4de6d7eSSascha Hauer if (err)
325c4de6d7eSSascha Hauer goto out;
326c4de6d7eSSascha Hauer
327c4de6d7eSSascha Hauer err = ubifs_write_node(c, ino, UBIFS_INO_NODE_SZ,
328c4de6d7eSSascha Hauer main_first + DEFAULT_DATA_LEB, 0);
329c4de6d7eSSascha Hauer if (err)
330c4de6d7eSSascha Hauer goto out;
331c4de6d7eSSascha Hauer
332104115a3SSascha Hauer ubifs_node_calc_hash(c, ino, hash);
333104115a3SSascha Hauer ubifs_copy_hash(c, hash, ubifs_branch_hash(c, br));
334104115a3SSascha Hauer
335104115a3SSascha Hauer err = ubifs_write_node(c, idx, idx_node_size, main_first + DEFAULT_IDX_LEB, 0);
336104115a3SSascha Hauer if (err)
337104115a3SSascha Hauer goto out;
338104115a3SSascha Hauer
339104115a3SSascha Hauer ubifs_node_calc_hash(c, idx, hash);
340104115a3SSascha Hauer ubifs_copy_hash(c, hash, mst->hash_root_idx);
341104115a3SSascha Hauer
342104115a3SSascha Hauer err = ubifs_write_node_hmac(c, mst, UBIFS_MST_NODE_SZ, UBIFS_MST_LNUM, 0,
343104115a3SSascha Hauer offsetof(struct ubifs_mst_node, hmac));
344104115a3SSascha Hauer if (err)
345104115a3SSascha Hauer goto out;
346104115a3SSascha Hauer
347104115a3SSascha Hauer err = ubifs_write_node_hmac(c, mst, UBIFS_MST_NODE_SZ, UBIFS_MST_LNUM + 1,
348104115a3SSascha Hauer 0, offsetof(struct ubifs_mst_node, hmac));
349104115a3SSascha Hauer if (err)
350104115a3SSascha Hauer goto out;
351104115a3SSascha Hauer
352c4de6d7eSSascha Hauer err = ubifs_write_node(c, cs, UBIFS_CS_NODE_SZ, UBIFS_LOG_LNUM, 0);
353c4de6d7eSSascha Hauer if (err)
354c4de6d7eSSascha Hauer goto out;
3551e51764aSArtem Bityutskiy
356235c362bSSheng Yong ubifs_msg(c, "default file-system created");
357c4de6d7eSSascha Hauer
358c4de6d7eSSascha Hauer err = 0;
359c4de6d7eSSascha Hauer out:
360c4de6d7eSSascha Hauer kfree(sup);
361c4de6d7eSSascha Hauer kfree(mst);
362c4de6d7eSSascha Hauer kfree(idx);
363c4de6d7eSSascha Hauer kfree(ino);
364c4de6d7eSSascha Hauer kfree(cs);
365c4de6d7eSSascha Hauer
366c4de6d7eSSascha Hauer return err;
3671e51764aSArtem Bityutskiy }
3681e51764aSArtem Bityutskiy
3691e51764aSArtem Bityutskiy /**
3701e51764aSArtem Bityutskiy * validate_sb - validate superblock node.
3711e51764aSArtem Bityutskiy * @c: UBIFS file-system description object
3721e51764aSArtem Bityutskiy * @sup: superblock node
3731e51764aSArtem Bityutskiy *
3741e51764aSArtem Bityutskiy * This function validates superblock node @sup. Since most of data was read
3751e51764aSArtem Bityutskiy * from the superblock and stored in @c, the function validates fields in @c
3761e51764aSArtem Bityutskiy * instead. Returns zero in case of success and %-EINVAL in case of validation
3771e51764aSArtem Bityutskiy * failure.
3781e51764aSArtem Bityutskiy */
validate_sb(struct ubifs_info * c,struct ubifs_sb_node * sup)3791e51764aSArtem Bityutskiy static int validate_sb(struct ubifs_info *c, struct ubifs_sb_node *sup)
3801e51764aSArtem Bityutskiy {
3811e51764aSArtem Bityutskiy long long max_bytes;
3821e51764aSArtem Bityutskiy int err = 1, min_leb_cnt;
3831e51764aSArtem Bityutskiy
3841e51764aSArtem Bityutskiy if (!c->key_hash) {
3851e51764aSArtem Bityutskiy err = 2;
3861e51764aSArtem Bityutskiy goto failed;
3871e51764aSArtem Bityutskiy }
3881e51764aSArtem Bityutskiy
3891e51764aSArtem Bityutskiy if (sup->key_fmt != UBIFS_SIMPLE_KEY_FMT) {
3901e51764aSArtem Bityutskiy err = 3;
3911e51764aSArtem Bityutskiy goto failed;
3921e51764aSArtem Bityutskiy }
3931e51764aSArtem Bityutskiy
3941e51764aSArtem Bityutskiy if (le32_to_cpu(sup->min_io_size) != c->min_io_size) {
395235c362bSSheng Yong ubifs_err(c, "min. I/O unit mismatch: %d in superblock, %d real",
3961e51764aSArtem Bityutskiy le32_to_cpu(sup->min_io_size), c->min_io_size);
3971e51764aSArtem Bityutskiy goto failed;
3981e51764aSArtem Bityutskiy }
3991e51764aSArtem Bityutskiy
4001e51764aSArtem Bityutskiy if (le32_to_cpu(sup->leb_size) != c->leb_size) {
401235c362bSSheng Yong ubifs_err(c, "LEB size mismatch: %d in superblock, %d real",
4021e51764aSArtem Bityutskiy le32_to_cpu(sup->leb_size), c->leb_size);
4031e51764aSArtem Bityutskiy goto failed;
4041e51764aSArtem Bityutskiy }
4051e51764aSArtem Bityutskiy
4061e51764aSArtem Bityutskiy if (c->log_lebs < UBIFS_MIN_LOG_LEBS ||
4071e51764aSArtem Bityutskiy c->lpt_lebs < UBIFS_MIN_LPT_LEBS ||
4081e51764aSArtem Bityutskiy c->orph_lebs < UBIFS_MIN_ORPH_LEBS ||
4091e51764aSArtem Bityutskiy c->main_lebs < UBIFS_MIN_MAIN_LEBS) {
4101e51764aSArtem Bityutskiy err = 4;
4111e51764aSArtem Bityutskiy goto failed;
4121e51764aSArtem Bityutskiy }
4131e51764aSArtem Bityutskiy
4141e51764aSArtem Bityutskiy /*
4151e51764aSArtem Bityutskiy * Calculate minimum allowed amount of main area LEBs. This is very
4161e51764aSArtem Bityutskiy * similar to %UBIFS_MIN_LEB_CNT, but we take into account real what we
4171e51764aSArtem Bityutskiy * have just read from the superblock.
4181e51764aSArtem Bityutskiy */
4191e51764aSArtem Bityutskiy min_leb_cnt = UBIFS_SB_LEBS + UBIFS_MST_LEBS + c->log_lebs;
4201e51764aSArtem Bityutskiy min_leb_cnt += c->lpt_lebs + c->orph_lebs + c->jhead_cnt + 6;
4211e51764aSArtem Bityutskiy
4221e51764aSArtem Bityutskiy if (c->leb_cnt < min_leb_cnt || c->leb_cnt > c->vi.size) {
423235c362bSSheng Yong ubifs_err(c, "bad LEB count: %d in superblock, %d on UBI volume, %d minimum required",
42479fda517SArtem Bityutskiy c->leb_cnt, c->vi.size, min_leb_cnt);
4251e51764aSArtem Bityutskiy goto failed;
4261e51764aSArtem Bityutskiy }
4271e51764aSArtem Bityutskiy
4281e51764aSArtem Bityutskiy if (c->max_leb_cnt < c->leb_cnt) {
429235c362bSSheng Yong ubifs_err(c, "max. LEB count %d less than LEB count %d",
4301e51764aSArtem Bityutskiy c->max_leb_cnt, c->leb_cnt);
4311e51764aSArtem Bityutskiy goto failed;
4321e51764aSArtem Bityutskiy }
4331e51764aSArtem Bityutskiy
4341e51764aSArtem Bityutskiy if (c->main_lebs < UBIFS_MIN_MAIN_LEBS) {
435235c362bSSheng Yong ubifs_err(c, "too few main LEBs count %d, must be at least %d",
4365a1f36c9SArtem Bityutskiy c->main_lebs, UBIFS_MIN_MAIN_LEBS);
4371e51764aSArtem Bityutskiy goto failed;
4381e51764aSArtem Bityutskiy }
4391e51764aSArtem Bityutskiy
4405a1f36c9SArtem Bityutskiy max_bytes = (long long)c->leb_size * UBIFS_MIN_BUD_LEBS;
4415a1f36c9SArtem Bityutskiy if (c->max_bud_bytes < max_bytes) {
442235c362bSSheng Yong ubifs_err(c, "too small journal (%lld bytes), must be at least %lld bytes",
44379fda517SArtem Bityutskiy c->max_bud_bytes, max_bytes);
4445a1f36c9SArtem Bityutskiy goto failed;
4455a1f36c9SArtem Bityutskiy }
4465a1f36c9SArtem Bityutskiy
4475a1f36c9SArtem Bityutskiy max_bytes = (long long)c->leb_size * c->main_lebs;
4485a1f36c9SArtem Bityutskiy if (c->max_bud_bytes > max_bytes) {
449235c362bSSheng Yong ubifs_err(c, "too large journal size (%lld bytes), only %lld bytes available in the main area",
4505a1f36c9SArtem Bityutskiy c->max_bud_bytes, max_bytes);
4511e51764aSArtem Bityutskiy goto failed;
4521e51764aSArtem Bityutskiy }
4531e51764aSArtem Bityutskiy
4541e51764aSArtem Bityutskiy if (c->jhead_cnt < NONDATA_JHEADS_CNT + 1 ||
4551e51764aSArtem Bityutskiy c->jhead_cnt > NONDATA_JHEADS_CNT + UBIFS_MAX_JHEADS) {
4561e51764aSArtem Bityutskiy err = 9;
4571e51764aSArtem Bityutskiy goto failed;
4581e51764aSArtem Bityutskiy }
4591e51764aSArtem Bityutskiy
4601e51764aSArtem Bityutskiy if (c->fanout < UBIFS_MIN_FANOUT ||
4611e51764aSArtem Bityutskiy ubifs_idx_node_sz(c, c->fanout) > c->leb_size) {
4621e51764aSArtem Bityutskiy err = 10;
4631e51764aSArtem Bityutskiy goto failed;
4641e51764aSArtem Bityutskiy }
4651e51764aSArtem Bityutskiy
4661e51764aSArtem Bityutskiy if (c->lsave_cnt < 0 || (c->lsave_cnt > DEFAULT_LSAVE_CNT &&
4671e51764aSArtem Bityutskiy c->lsave_cnt > c->max_leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS -
4681e51764aSArtem Bityutskiy c->log_lebs - c->lpt_lebs - c->orph_lebs)) {
4691e51764aSArtem Bityutskiy err = 11;
4701e51764aSArtem Bityutskiy goto failed;
4711e51764aSArtem Bityutskiy }
4721e51764aSArtem Bityutskiy
4731e51764aSArtem Bityutskiy if (UBIFS_SB_LEBS + UBIFS_MST_LEBS + c->log_lebs + c->lpt_lebs +
4741e51764aSArtem Bityutskiy c->orph_lebs + c->main_lebs != c->leb_cnt) {
4751e51764aSArtem Bityutskiy err = 12;
4761e51764aSArtem Bityutskiy goto failed;
4771e51764aSArtem Bityutskiy }
4781e51764aSArtem Bityutskiy
479b793a8c8Shujianyang if (c->default_compr >= UBIFS_COMPR_TYPES_CNT) {
4801e51764aSArtem Bityutskiy err = 13;
4811e51764aSArtem Bityutskiy goto failed;
4821e51764aSArtem Bityutskiy }
4831e51764aSArtem Bityutskiy
4841e51764aSArtem Bityutskiy if (c->rp_size < 0 || max_bytes < c->rp_size) {
4851e51764aSArtem Bityutskiy err = 14;
4861e51764aSArtem Bityutskiy goto failed;
4871e51764aSArtem Bityutskiy }
4881e51764aSArtem Bityutskiy
4891e51764aSArtem Bityutskiy if (le32_to_cpu(sup->time_gran) > 1000000000 ||
4901e51764aSArtem Bityutskiy le32_to_cpu(sup->time_gran) < 1) {
4911e51764aSArtem Bityutskiy err = 15;
4921e51764aSArtem Bityutskiy goto failed;
4931e51764aSArtem Bityutskiy }
4941e51764aSArtem Bityutskiy
495fc4b891bSRichard Weinberger if (!c->double_hash && c->fmt_version >= 5) {
496fc4b891bSRichard Weinberger err = 16;
497fc4b891bSRichard Weinberger goto failed;
498fc4b891bSRichard Weinberger }
499fc4b891bSRichard Weinberger
500fc4b891bSRichard Weinberger if (c->encrypted && c->fmt_version < 5) {
501fc4b891bSRichard Weinberger err = 17;
502fc4b891bSRichard Weinberger goto failed;
503fc4b891bSRichard Weinberger }
504fc4b891bSRichard Weinberger
5051e51764aSArtem Bityutskiy return 0;
5061e51764aSArtem Bityutskiy
5071e51764aSArtem Bityutskiy failed:
508235c362bSSheng Yong ubifs_err(c, "bad superblock, error %d", err);
509a33e30a0SZhihao Cheng ubifs_dump_node(c, sup, ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size));
5101e51764aSArtem Bityutskiy return -EINVAL;
5111e51764aSArtem Bityutskiy }
5121e51764aSArtem Bityutskiy
5131e51764aSArtem Bityutskiy /**
5141e51764aSArtem Bityutskiy * ubifs_read_sb_node - read superblock node.
5151e51764aSArtem Bityutskiy * @c: UBIFS file-system description object
5161e51764aSArtem Bityutskiy *
5171e51764aSArtem Bityutskiy * This function returns a pointer to the superblock node or a negative error
518eaeee242SArtem Bityutskiy * code. Note, the user of this function is responsible of kfree()'ing the
519eaeee242SArtem Bityutskiy * returned superblock buffer.
5201e51764aSArtem Bityutskiy */
ubifs_read_sb_node(struct ubifs_info * c)521fd615005SSascha Hauer static struct ubifs_sb_node *ubifs_read_sb_node(struct ubifs_info *c)
5221e51764aSArtem Bityutskiy {
5231e51764aSArtem Bityutskiy struct ubifs_sb_node *sup;
5241e51764aSArtem Bityutskiy int err;
5251e51764aSArtem Bityutskiy
5261e51764aSArtem Bityutskiy sup = kmalloc(ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size), GFP_NOFS);
5271e51764aSArtem Bityutskiy if (!sup)
5281e51764aSArtem Bityutskiy return ERR_PTR(-ENOMEM);
5291e51764aSArtem Bityutskiy
5301e51764aSArtem Bityutskiy err = ubifs_read_node(c, sup, UBIFS_SB_NODE, UBIFS_SB_NODE_SZ,
5311e51764aSArtem Bityutskiy UBIFS_SB_LNUM, 0);
5321e51764aSArtem Bityutskiy if (err) {
5331e51764aSArtem Bityutskiy kfree(sup);
5341e51764aSArtem Bityutskiy return ERR_PTR(err);
5351e51764aSArtem Bityutskiy }
5361e51764aSArtem Bityutskiy
5371e51764aSArtem Bityutskiy return sup;
5381e51764aSArtem Bityutskiy }
5391e51764aSArtem Bityutskiy
authenticate_sb_node(struct ubifs_info * c,const struct ubifs_sb_node * sup)540e158e02fSSascha Hauer static int authenticate_sb_node(struct ubifs_info *c,
541e158e02fSSascha Hauer const struct ubifs_sb_node *sup)
542e158e02fSSascha Hauer {
543e158e02fSSascha Hauer unsigned int sup_flags = le32_to_cpu(sup->flags);
544e158e02fSSascha Hauer u8 hmac_wkm[UBIFS_HMAC_ARR_SZ];
545e158e02fSSascha Hauer int authenticated = !!(sup_flags & UBIFS_FLG_AUTHENTICATION);
546e158e02fSSascha Hauer int hash_algo;
547e158e02fSSascha Hauer int err;
548e158e02fSSascha Hauer
549e158e02fSSascha Hauer if (c->authenticated && !authenticated) {
550e158e02fSSascha Hauer ubifs_err(c, "authenticated FS forced, but found FS without authentication");
551e158e02fSSascha Hauer return -EINVAL;
552e158e02fSSascha Hauer }
553e158e02fSSascha Hauer
554e158e02fSSascha Hauer if (!c->authenticated && authenticated) {
555e158e02fSSascha Hauer ubifs_err(c, "authenticated FS found, but no key given");
556e158e02fSSascha Hauer return -EINVAL;
557e158e02fSSascha Hauer }
558e158e02fSSascha Hauer
559e158e02fSSascha Hauer ubifs_msg(c, "Mounting in %sauthenticated mode",
560e158e02fSSascha Hauer c->authenticated ? "" : "un");
561e158e02fSSascha Hauer
562e158e02fSSascha Hauer if (!c->authenticated)
563e158e02fSSascha Hauer return 0;
564e158e02fSSascha Hauer
565e158e02fSSascha Hauer if (!IS_ENABLED(CONFIG_UBIFS_FS_AUTHENTICATION))
566e158e02fSSascha Hauer return -EOPNOTSUPP;
567e158e02fSSascha Hauer
568e158e02fSSascha Hauer hash_algo = le16_to_cpu(sup->hash_algo);
569e158e02fSSascha Hauer if (hash_algo >= HASH_ALGO__LAST) {
570e158e02fSSascha Hauer ubifs_err(c, "superblock uses unknown hash algo %d",
571e158e02fSSascha Hauer hash_algo);
572e158e02fSSascha Hauer return -EINVAL;
573e158e02fSSascha Hauer }
574e158e02fSSascha Hauer
575e158e02fSSascha Hauer if (strcmp(hash_algo_name[hash_algo], c->auth_hash_name)) {
576e158e02fSSascha Hauer ubifs_err(c, "This filesystem uses %s for hashing,"
577e158e02fSSascha Hauer " but %s is specified", hash_algo_name[hash_algo],
578e158e02fSSascha Hauer c->auth_hash_name);
579e158e02fSSascha Hauer return -EINVAL;
580e158e02fSSascha Hauer }
581e158e02fSSascha Hauer
582817aa094SSascha Hauer /*
583817aa094SSascha Hauer * The super block node can either be authenticated by a HMAC or
584817aa094SSascha Hauer * by a signature in a ubifs_sig_node directly following the
585817aa094SSascha Hauer * super block node to support offline image creation.
586817aa094SSascha Hauer */
587817aa094SSascha Hauer if (ubifs_hmac_zero(c, sup->hmac)) {
588817aa094SSascha Hauer err = ubifs_sb_verify_signature(c, sup);
589817aa094SSascha Hauer } else {
590e158e02fSSascha Hauer err = ubifs_hmac_wkm(c, hmac_wkm);
591e158e02fSSascha Hauer if (err)
592e158e02fSSascha Hauer return err;
593e158e02fSSascha Hauer if (ubifs_check_hmac(c, hmac_wkm, sup->hmac_wkm)) {
594e158e02fSSascha Hauer ubifs_err(c, "provided key does not fit");
595e158e02fSSascha Hauer return -ENOKEY;
596e158e02fSSascha Hauer }
597e158e02fSSascha Hauer err = ubifs_node_verify_hmac(c, sup, sizeof(*sup),
598817aa094SSascha Hauer offsetof(struct ubifs_sb_node,
599817aa094SSascha Hauer hmac));
600817aa094SSascha Hauer }
601817aa094SSascha Hauer
602e158e02fSSascha Hauer if (err)
603e158e02fSSascha Hauer ubifs_err(c, "Failed to authenticate superblock: %d", err);
604e158e02fSSascha Hauer
605e158e02fSSascha Hauer return err;
606e158e02fSSascha Hauer }
607e158e02fSSascha Hauer
6081e51764aSArtem Bityutskiy /**
6091e51764aSArtem Bityutskiy * ubifs_write_sb_node - write superblock node.
6101e51764aSArtem Bityutskiy * @c: UBIFS file-system description object
6111e51764aSArtem Bityutskiy * @sup: superblock node read with 'ubifs_read_sb_node()'
6121e51764aSArtem Bityutskiy *
6131e51764aSArtem Bityutskiy * This function returns %0 on success and a negative error code on failure.
6141e51764aSArtem Bityutskiy */
ubifs_write_sb_node(struct ubifs_info * c,struct ubifs_sb_node * sup)6151e51764aSArtem Bityutskiy int ubifs_write_sb_node(struct ubifs_info *c, struct ubifs_sb_node *sup)
6161e51764aSArtem Bityutskiy {
6171e51764aSArtem Bityutskiy int len = ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size);
618e158e02fSSascha Hauer int err;
6191e51764aSArtem Bityutskiy
620e158e02fSSascha Hauer err = ubifs_prepare_node_hmac(c, sup, UBIFS_SB_NODE_SZ,
621e158e02fSSascha Hauer offsetof(struct ubifs_sb_node, hmac), 1);
622e158e02fSSascha Hauer if (err)
623e158e02fSSascha Hauer return err;
624e158e02fSSascha Hauer
625b36a261eSRichard Weinberger return ubifs_leb_change(c, UBIFS_SB_LNUM, sup, len);
6261e51764aSArtem Bityutskiy }
6271e51764aSArtem Bityutskiy
6281e51764aSArtem Bityutskiy /**
6291e51764aSArtem Bityutskiy * ubifs_read_superblock - read superblock.
6301e51764aSArtem Bityutskiy * @c: UBIFS file-system description object
6311e51764aSArtem Bityutskiy *
6321e51764aSArtem Bityutskiy * This function finds, reads and checks the superblock. If an empty UBI volume
6331e51764aSArtem Bityutskiy * is being mounted, this function creates default superblock. Returns zero in
6341e51764aSArtem Bityutskiy * case of success, and a negative error code in case of failure.
6351e51764aSArtem Bityutskiy */
ubifs_read_superblock(struct ubifs_info * c)6361e51764aSArtem Bityutskiy int ubifs_read_superblock(struct ubifs_info *c)
6371e51764aSArtem Bityutskiy {
6381e51764aSArtem Bityutskiy int err, sup_flags;
6391e51764aSArtem Bityutskiy struct ubifs_sb_node *sup;
6401e51764aSArtem Bityutskiy
6411e51764aSArtem Bityutskiy if (c->empty) {
6421e51764aSArtem Bityutskiy err = create_default_filesystem(c);
6431e51764aSArtem Bityutskiy if (err)
6441e51764aSArtem Bityutskiy return err;
6451e51764aSArtem Bityutskiy }
6461e51764aSArtem Bityutskiy
6471e51764aSArtem Bityutskiy sup = ubifs_read_sb_node(c);
6481e51764aSArtem Bityutskiy if (IS_ERR(sup))
6491e51764aSArtem Bityutskiy return PTR_ERR(sup);
6501e51764aSArtem Bityutskiy
651fd615005SSascha Hauer c->sup_node = sup;
652fd615005SSascha Hauer
653963f0cf6SArtem Bityutskiy c->fmt_version = le32_to_cpu(sup->fmt_version);
654963f0cf6SArtem Bityutskiy c->ro_compat_version = le32_to_cpu(sup->ro_compat_version);
655963f0cf6SArtem Bityutskiy
6561e51764aSArtem Bityutskiy /*
6571e51764aSArtem Bityutskiy * The software supports all previous versions but not future versions,
6581e51764aSArtem Bityutskiy * due to the unavailability of time-travelling equipment.
6591e51764aSArtem Bityutskiy */
6601e51764aSArtem Bityutskiy if (c->fmt_version > UBIFS_FORMAT_VERSION) {
6616eb61d58SRichard Weinberger ubifs_assert(c, !c->ro_media || c->ro_mount);
6622ef13294SArtem Bityutskiy if (!c->ro_mount ||
663963f0cf6SArtem Bityutskiy c->ro_compat_version > UBIFS_RO_COMPAT_VERSION) {
664235c362bSSheng Yong ubifs_err(c, "on-flash format version is w%d/r%d, but software only supports up to version w%d/r%d",
66579fda517SArtem Bityutskiy c->fmt_version, c->ro_compat_version,
66679fda517SArtem Bityutskiy UBIFS_FORMAT_VERSION,
667963f0cf6SArtem Bityutskiy UBIFS_RO_COMPAT_VERSION);
668963f0cf6SArtem Bityutskiy if (c->ro_compat_version <= UBIFS_RO_COMPAT_VERSION) {
669235c362bSSheng Yong ubifs_msg(c, "only R/O mounting is possible");
670963f0cf6SArtem Bityutskiy err = -EROFS;
671963f0cf6SArtem Bityutskiy } else
6721e51764aSArtem Bityutskiy err = -EINVAL;
6731e51764aSArtem Bityutskiy goto out;
6741e51764aSArtem Bityutskiy }
6751e51764aSArtem Bityutskiy
676963f0cf6SArtem Bityutskiy /*
677963f0cf6SArtem Bityutskiy * The FS is mounted R/O, and the media format is
678963f0cf6SArtem Bityutskiy * R/O-compatible with the UBIFS implementation, so we can
679963f0cf6SArtem Bityutskiy * mount.
680963f0cf6SArtem Bityutskiy */
681963f0cf6SArtem Bityutskiy c->rw_incompat = 1;
682963f0cf6SArtem Bityutskiy }
683963f0cf6SArtem Bityutskiy
6841e51764aSArtem Bityutskiy if (c->fmt_version < 3) {
685235c362bSSheng Yong ubifs_err(c, "on-flash format version %d is not supported",
6861e51764aSArtem Bityutskiy c->fmt_version);
6871e51764aSArtem Bityutskiy err = -EINVAL;
6881e51764aSArtem Bityutskiy goto out;
6891e51764aSArtem Bityutskiy }
6901e51764aSArtem Bityutskiy
6911e51764aSArtem Bityutskiy switch (sup->key_hash) {
6921e51764aSArtem Bityutskiy case UBIFS_KEY_HASH_R5:
6931e51764aSArtem Bityutskiy c->key_hash = key_r5_hash;
6941e51764aSArtem Bityutskiy c->key_hash_type = UBIFS_KEY_HASH_R5;
6951e51764aSArtem Bityutskiy break;
6961e51764aSArtem Bityutskiy
6971e51764aSArtem Bityutskiy case UBIFS_KEY_HASH_TEST:
6981e51764aSArtem Bityutskiy c->key_hash = key_test_hash;
6991e51764aSArtem Bityutskiy c->key_hash_type = UBIFS_KEY_HASH_TEST;
7001e51764aSArtem Bityutskiy break;
70184db119fSDing Xiang }
7021e51764aSArtem Bityutskiy
7031e51764aSArtem Bityutskiy c->key_fmt = sup->key_fmt;
7041e51764aSArtem Bityutskiy
7051e51764aSArtem Bityutskiy switch (c->key_fmt) {
7061e51764aSArtem Bityutskiy case UBIFS_SIMPLE_KEY_FMT:
7071e51764aSArtem Bityutskiy c->key_len = UBIFS_SK_LEN;
7081e51764aSArtem Bityutskiy break;
7091e51764aSArtem Bityutskiy default:
710235c362bSSheng Yong ubifs_err(c, "unsupported key format");
7111e51764aSArtem Bityutskiy err = -EINVAL;
7121e51764aSArtem Bityutskiy goto out;
7131e51764aSArtem Bityutskiy }
7141e51764aSArtem Bityutskiy
7151e51764aSArtem Bityutskiy c->leb_cnt = le32_to_cpu(sup->leb_cnt);
7161e51764aSArtem Bityutskiy c->max_leb_cnt = le32_to_cpu(sup->max_leb_cnt);
7171e51764aSArtem Bityutskiy c->max_bud_bytes = le64_to_cpu(sup->max_bud_bytes);
7181e51764aSArtem Bityutskiy c->log_lebs = le32_to_cpu(sup->log_lebs);
7191e51764aSArtem Bityutskiy c->lpt_lebs = le32_to_cpu(sup->lpt_lebs);
7201e51764aSArtem Bityutskiy c->orph_lebs = le32_to_cpu(sup->orph_lebs);
7211e51764aSArtem Bityutskiy c->jhead_cnt = le32_to_cpu(sup->jhead_cnt) + NONDATA_JHEADS_CNT;
7221e51764aSArtem Bityutskiy c->fanout = le32_to_cpu(sup->fanout);
7231e51764aSArtem Bityutskiy c->lsave_cnt = le32_to_cpu(sup->lsave_cnt);
7241e51764aSArtem Bityutskiy c->rp_size = le64_to_cpu(sup->rp_size);
72539241bebSEric W. Biederman c->rp_uid = make_kuid(&init_user_ns, le32_to_cpu(sup->rp_uid));
72639241bebSEric W. Biederman c->rp_gid = make_kgid(&init_user_ns, le32_to_cpu(sup->rp_gid));
7271e51764aSArtem Bityutskiy sup_flags = le32_to_cpu(sup->flags);
728553dea4dSArtem Bityutskiy if (!c->mount_opts.override_compr)
729553dea4dSArtem Bityutskiy c->default_compr = le16_to_cpu(sup->default_compr);
7301e51764aSArtem Bityutskiy
7311e51764aSArtem Bityutskiy c->vfs_sb->s_time_gran = le32_to_cpu(sup->time_gran);
7321e51764aSArtem Bityutskiy memcpy(&c->uuid, &sup->uuid, 16);
7331e51764aSArtem Bityutskiy c->big_lpt = !!(sup_flags & UBIFS_FLG_BIGLPT);
7349f58d350SMatthew L. Creech c->space_fixup = !!(sup_flags & UBIFS_FLG_SPACE_FIXUP);
735d63d61c1SRichard Weinberger c->double_hash = !!(sup_flags & UBIFS_FLG_DOUBLE_HASH);
736e021986eSRichard Weinberger c->encrypted = !!(sup_flags & UBIFS_FLG_ENCRYPTION);
737e021986eSRichard Weinberger
738e158e02fSSascha Hauer err = authenticate_sb_node(c, sup);
739e158e02fSSascha Hauer if (err)
740e158e02fSSascha Hauer goto out;
741e158e02fSSascha Hauer
742fc4b891bSRichard Weinberger if ((sup_flags & ~UBIFS_FLG_MASK) != 0) {
743fc4b891bSRichard Weinberger ubifs_err(c, "Unknown feature flags found: %#x",
744fc4b891bSRichard Weinberger sup_flags & ~UBIFS_FLG_MASK);
745fc4b891bSRichard Weinberger err = -EINVAL;
746fc4b891bSRichard Weinberger goto out;
747fc4b891bSRichard Weinberger }
748fc4b891bSRichard Weinberger
74976aa3494SRichard Weinberger if (!IS_ENABLED(CONFIG_FS_ENCRYPTION) && c->encrypted) {
750e021986eSRichard Weinberger ubifs_err(c, "file system contains encrypted files but UBIFS"
751e021986eSRichard Weinberger " was built without crypto support.");
752e021986eSRichard Weinberger err = -EINVAL;
753e021986eSRichard Weinberger goto out;
754e021986eSRichard Weinberger }
7551e51764aSArtem Bityutskiy
7561e51764aSArtem Bityutskiy /* Automatically increase file system size to the maximum size */
7571e51764aSArtem Bityutskiy if (c->leb_cnt < c->vi.size && c->leb_cnt < c->max_leb_cnt) {
758817aa094SSascha Hauer int old_leb_cnt = c->leb_cnt;
759817aa094SSascha Hauer
7601e51764aSArtem Bityutskiy c->leb_cnt = min_t(int, c->max_leb_cnt, c->vi.size);
7611e51764aSArtem Bityutskiy sup->leb_cnt = cpu_to_le32(c->leb_cnt);
762817aa094SSascha Hauer
763817aa094SSascha Hauer c->superblock_need_write = 1;
764817aa094SSascha Hauer
765817aa094SSascha Hauer dbg_mnt("Auto resizing from %d LEBs to %d LEBs",
766817aa094SSascha Hauer old_leb_cnt, c->leb_cnt);
7671e51764aSArtem Bityutskiy }
7681e51764aSArtem Bityutskiy
7691e51764aSArtem Bityutskiy c->log_bytes = (long long)c->log_lebs * c->leb_size;
7701e51764aSArtem Bityutskiy c->log_last = UBIFS_LOG_LNUM + c->log_lebs - 1;
7711e51764aSArtem Bityutskiy c->lpt_first = UBIFS_LOG_LNUM + c->log_lebs;
7721e51764aSArtem Bityutskiy c->lpt_last = c->lpt_first + c->lpt_lebs - 1;
7731e51764aSArtem Bityutskiy c->orph_first = c->lpt_last + 1;
7741e51764aSArtem Bityutskiy c->orph_last = c->orph_first + c->orph_lebs - 1;
7751e51764aSArtem Bityutskiy c->main_lebs = c->leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS;
7761e51764aSArtem Bityutskiy c->main_lebs -= c->log_lebs + c->lpt_lebs + c->orph_lebs;
7771e51764aSArtem Bityutskiy c->main_first = c->leb_cnt - c->main_lebs;
7781e51764aSArtem Bityutskiy
7791e51764aSArtem Bityutskiy err = validate_sb(c, sup);
7801e51764aSArtem Bityutskiy out:
7811e51764aSArtem Bityutskiy return err;
7821e51764aSArtem Bityutskiy }
7836554a657SMatthew L. Creech
7846554a657SMatthew L. Creech /**
7856554a657SMatthew L. Creech * fixup_leb - fixup/unmap an LEB containing free space.
7866554a657SMatthew L. Creech * @c: UBIFS file-system description object
7876554a657SMatthew L. Creech * @lnum: the LEB number to fix up
7886554a657SMatthew L. Creech * @len: number of used bytes in LEB (starting at offset 0)
7896554a657SMatthew L. Creech *
7906554a657SMatthew L. Creech * This function reads the contents of the given LEB number @lnum, then fixes
7916554a657SMatthew L. Creech * it up, so that empty min. I/O units in the end of LEB are actually erased on
7926554a657SMatthew L. Creech * flash (rather than being just all-0xff real data). If the LEB is completely
7936554a657SMatthew L. Creech * empty, it is simply unmapped.
7946554a657SMatthew L. Creech */
fixup_leb(struct ubifs_info * c,int lnum,int len)7956554a657SMatthew L. Creech static int fixup_leb(struct ubifs_info *c, int lnum, int len)
7966554a657SMatthew L. Creech {
7976554a657SMatthew L. Creech int err;
7986554a657SMatthew L. Creech
7996eb61d58SRichard Weinberger ubifs_assert(c, len >= 0);
8006eb61d58SRichard Weinberger ubifs_assert(c, len % c->min_io_size == 0);
8016eb61d58SRichard Weinberger ubifs_assert(c, len < c->leb_size);
8026554a657SMatthew L. Creech
8036554a657SMatthew L. Creech if (len == 0) {
8046554a657SMatthew L. Creech dbg_mnt("unmap empty LEB %d", lnum);
805d3b2578fSArtem Bityutskiy return ubifs_leb_unmap(c, lnum);
8066554a657SMatthew L. Creech }
8076554a657SMatthew L. Creech
8086554a657SMatthew L. Creech dbg_mnt("fixup LEB %d, data len %d", lnum, len);
809d304820aSArtem Bityutskiy err = ubifs_leb_read(c, lnum, c->sbuf, 0, len, 1);
8106554a657SMatthew L. Creech if (err)
8116554a657SMatthew L. Creech return err;
8126554a657SMatthew L. Creech
813b36a261eSRichard Weinberger return ubifs_leb_change(c, lnum, c->sbuf, len);
8146554a657SMatthew L. Creech }
8156554a657SMatthew L. Creech
8166554a657SMatthew L. Creech /**
8176554a657SMatthew L. Creech * fixup_free_space - find & remap all LEBs containing free space.
8186554a657SMatthew L. Creech * @c: UBIFS file-system description object
8196554a657SMatthew L. Creech *
8206554a657SMatthew L. Creech * This function walks through all LEBs in the filesystem and fiexes up those
8216554a657SMatthew L. Creech * containing free/empty space.
8226554a657SMatthew L. Creech */
fixup_free_space(struct ubifs_info * c)8236554a657SMatthew L. Creech static int fixup_free_space(struct ubifs_info *c)
8246554a657SMatthew L. Creech {
8256554a657SMatthew L. Creech int lnum, err = 0;
8266554a657SMatthew L. Creech struct ubifs_lprops *lprops;
8276554a657SMatthew L. Creech
8286554a657SMatthew L. Creech ubifs_get_lprops(c);
8296554a657SMatthew L. Creech
8306554a657SMatthew L. Creech /* Fixup LEBs in the master area */
8316554a657SMatthew L. Creech for (lnum = UBIFS_MST_LNUM; lnum < UBIFS_LOG_LNUM; lnum++) {
8326554a657SMatthew L. Creech err = fixup_leb(c, lnum, c->mst_offs + c->mst_node_alsz);
8336554a657SMatthew L. Creech if (err)
8346554a657SMatthew L. Creech goto out;
8356554a657SMatthew L. Creech }
8366554a657SMatthew L. Creech
8376554a657SMatthew L. Creech /* Unmap unused log LEBs */
8386554a657SMatthew L. Creech lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
8396554a657SMatthew L. Creech while (lnum != c->ltail_lnum) {
8406554a657SMatthew L. Creech err = fixup_leb(c, lnum, 0);
8416554a657SMatthew L. Creech if (err)
8426554a657SMatthew L. Creech goto out;
8436554a657SMatthew L. Creech lnum = ubifs_next_log_lnum(c, lnum);
8446554a657SMatthew L. Creech }
8456554a657SMatthew L. Creech
846c6727932SArtem Bityutskiy /*
847c6727932SArtem Bityutskiy * Fixup the log head which contains the only a CS node at the
848c6727932SArtem Bityutskiy * beginning.
849c6727932SArtem Bityutskiy */
850c6727932SArtem Bityutskiy err = fixup_leb(c, c->lhead_lnum,
851c6727932SArtem Bityutskiy ALIGN(UBIFS_CS_NODE_SZ, c->min_io_size));
8526554a657SMatthew L. Creech if (err)
8536554a657SMatthew L. Creech goto out;
8546554a657SMatthew L. Creech
8556554a657SMatthew L. Creech /* Fixup LEBs in the LPT area */
8566554a657SMatthew L. Creech for (lnum = c->lpt_first; lnum <= c->lpt_last; lnum++) {
8576554a657SMatthew L. Creech int free = c->ltab[lnum - c->lpt_first].free;
8586554a657SMatthew L. Creech
8596554a657SMatthew L. Creech if (free > 0) {
8606554a657SMatthew L. Creech err = fixup_leb(c, lnum, c->leb_size - free);
8616554a657SMatthew L. Creech if (err)
8626554a657SMatthew L. Creech goto out;
8636554a657SMatthew L. Creech }
8646554a657SMatthew L. Creech }
8656554a657SMatthew L. Creech
8666554a657SMatthew L. Creech /* Unmap LEBs in the orphans area */
8676554a657SMatthew L. Creech for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
8686554a657SMatthew L. Creech err = fixup_leb(c, lnum, 0);
8696554a657SMatthew L. Creech if (err)
8706554a657SMatthew L. Creech goto out;
8716554a657SMatthew L. Creech }
8726554a657SMatthew L. Creech
8736554a657SMatthew L. Creech /* Fixup LEBs in the main area */
8746554a657SMatthew L. Creech for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) {
8756554a657SMatthew L. Creech lprops = ubifs_lpt_lookup(c, lnum);
8766554a657SMatthew L. Creech if (IS_ERR(lprops)) {
8776554a657SMatthew L. Creech err = PTR_ERR(lprops);
8786554a657SMatthew L. Creech goto out;
8796554a657SMatthew L. Creech }
8806554a657SMatthew L. Creech
8816554a657SMatthew L. Creech if (lprops->free > 0) {
8826554a657SMatthew L. Creech err = fixup_leb(c, lnum, c->leb_size - lprops->free);
8836554a657SMatthew L. Creech if (err)
8846554a657SMatthew L. Creech goto out;
8856554a657SMatthew L. Creech }
8866554a657SMatthew L. Creech }
8876554a657SMatthew L. Creech
8886554a657SMatthew L. Creech out:
8896554a657SMatthew L. Creech ubifs_release_lprops(c);
8906554a657SMatthew L. Creech return err;
8916554a657SMatthew L. Creech }
8926554a657SMatthew L. Creech
8936554a657SMatthew L. Creech /**
8946554a657SMatthew L. Creech * ubifs_fixup_free_space - find & fix all LEBs with free space.
8956554a657SMatthew L. Creech * @c: UBIFS file-system description object
8966554a657SMatthew L. Creech *
8976554a657SMatthew L. Creech * This function fixes up LEBs containing free space on first mount, if the
8986554a657SMatthew L. Creech * appropriate flag was set when the FS was created. Each LEB with one or more
8996554a657SMatthew L. Creech * empty min. I/O unit (i.e. free-space-count > 0) is re-written, to make sure
9006554a657SMatthew L. Creech * the free space is actually erased. E.g., this is necessary for some NAND
9016554a657SMatthew L. Creech * chips, since the free space may have been programmed like real "0xff" data
9026554a657SMatthew L. Creech * (generating a non-0xff ECC), causing future writes to the not-really-erased
9036554a657SMatthew L. Creech * NAND pages to behave badly. After the space is fixed up, the superblock flag
9046554a657SMatthew L. Creech * is cleared, so that this is skipped for all future mounts.
9056554a657SMatthew L. Creech */
ubifs_fixup_free_space(struct ubifs_info * c)9066554a657SMatthew L. Creech int ubifs_fixup_free_space(struct ubifs_info *c)
9076554a657SMatthew L. Creech {
9086554a657SMatthew L. Creech int err;
909fd615005SSascha Hauer struct ubifs_sb_node *sup = c->sup_node;
9106554a657SMatthew L. Creech
9116eb61d58SRichard Weinberger ubifs_assert(c, c->space_fixup);
9126eb61d58SRichard Weinberger ubifs_assert(c, !c->ro_mount);
9136554a657SMatthew L. Creech
914235c362bSSheng Yong ubifs_msg(c, "start fixing up free space");
9156554a657SMatthew L. Creech
9166554a657SMatthew L. Creech err = fixup_free_space(c);
9176554a657SMatthew L. Creech if (err)
9186554a657SMatthew L. Creech return err;
9196554a657SMatthew L. Creech
9206554a657SMatthew L. Creech /* Free-space fixup is no longer required */
9216554a657SMatthew L. Creech c->space_fixup = 0;
9226554a657SMatthew L. Creech sup->flags &= cpu_to_le32(~UBIFS_FLG_SPACE_FIXUP);
9236554a657SMatthew L. Creech
924817aa094SSascha Hauer c->superblock_need_write = 1;
9256554a657SMatthew L. Creech
926235c362bSSheng Yong ubifs_msg(c, "free space fixup complete");
9276554a657SMatthew L. Creech return err;
9286554a657SMatthew L. Creech }
929e021986eSRichard Weinberger
ubifs_enable_encryption(struct ubifs_info * c)930e021986eSRichard Weinberger int ubifs_enable_encryption(struct ubifs_info *c)
931e021986eSRichard Weinberger {
932e021986eSRichard Weinberger int err;
933fd615005SSascha Hauer struct ubifs_sb_node *sup = c->sup_node;
934e021986eSRichard Weinberger
93576aa3494SRichard Weinberger if (!IS_ENABLED(CONFIG_FS_ENCRYPTION))
936eea2c05dSSascha Hauer return -EOPNOTSUPP;
937eea2c05dSSascha Hauer
938e021986eSRichard Weinberger if (c->encrypted)
939e021986eSRichard Weinberger return 0;
940e021986eSRichard Weinberger
941e021986eSRichard Weinberger if (c->ro_mount || c->ro_media)
942e021986eSRichard Weinberger return -EROFS;
943e021986eSRichard Weinberger
944e021986eSRichard Weinberger if (c->fmt_version < 5) {
945e021986eSRichard Weinberger ubifs_err(c, "on-flash format version 5 is needed for encryption");
946e021986eSRichard Weinberger return -EINVAL;
947e021986eSRichard Weinberger }
948e021986eSRichard Weinberger
949e021986eSRichard Weinberger sup->flags |= cpu_to_le32(UBIFS_FLG_ENCRYPTION);
950e021986eSRichard Weinberger
951e021986eSRichard Weinberger err = ubifs_write_sb_node(c, sup);
952e021986eSRichard Weinberger if (!err)
953e021986eSRichard Weinberger c->encrypted = 1;
954e021986eSRichard Weinberger
955e021986eSRichard Weinberger return err;
956e021986eSRichard Weinberger }
957