Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * This file is part of UBIFS.
0004  *
0005  * Copyright (C) 2006-2008 Nokia Corporation.
0006  *
0007  * Authors: Artem Bityutskiy (Битюцкий Артём)
0008  *          Adrian Hunter
0009  */
0010 
0011 /*
0012  * This file implements UBIFS superblock. The superblock is stored at the first
0013  * LEB of the volume and is never changed by UBIFS. Only user-space tools may
0014  * change it. The superblock node mostly contains geometry information.
0015  */
0016 
0017 #include "ubifs.h"
0018 #include <linux/slab.h>
0019 #include <linux/math64.h>
0020 #include <linux/uuid.h>
0021 
0022 /*
0023  * Default journal size in logical eraseblocks as a percent of total
0024  * flash size.
0025  */
0026 #define DEFAULT_JNL_PERCENT 5
0027 
0028 /* Default maximum journal size in bytes */
0029 #define DEFAULT_MAX_JNL (32*1024*1024)
0030 
0031 /* Default indexing tree fanout */
0032 #define DEFAULT_FANOUT 8
0033 
0034 /* Default number of data journal heads */
0035 #define DEFAULT_JHEADS_CNT 1
0036 
0037 /* Default positions of different LEBs in the main area */
0038 #define DEFAULT_IDX_LEB  0
0039 #define DEFAULT_DATA_LEB 1
0040 #define DEFAULT_GC_LEB   2
0041 
0042 /* Default number of LEB numbers in LPT's save table */
0043 #define DEFAULT_LSAVE_CNT 256
0044 
0045 /* Default reserved pool size as a percent of maximum free space */
0046 #define DEFAULT_RP_PERCENT 5
0047 
0048 /* The default maximum size of reserved pool in bytes */
0049 #define DEFAULT_MAX_RP_SIZE (5*1024*1024)
0050 
0051 /* Default time granularity in nanoseconds */
0052 #define DEFAULT_TIME_GRAN 1000000000
0053 
0054 static int get_default_compressor(struct ubifs_info *c)
0055 {
0056     if (ubifs_compr_present(c, UBIFS_COMPR_ZSTD))
0057         return UBIFS_COMPR_ZSTD;
0058 
0059     if (ubifs_compr_present(c, UBIFS_COMPR_LZO))
0060         return UBIFS_COMPR_LZO;
0061 
0062     if (ubifs_compr_present(c, UBIFS_COMPR_ZLIB))
0063         return UBIFS_COMPR_ZLIB;
0064 
0065     return UBIFS_COMPR_NONE;
0066 }
0067 
0068 /**
0069  * create_default_filesystem - format empty UBI volume.
0070  * @c: UBIFS file-system description object
0071  *
0072  * This function creates default empty file-system. Returns zero in case of
0073  * success and a negative error code in case of failure.
0074  */
0075 static int create_default_filesystem(struct ubifs_info *c)
0076 {
0077     struct ubifs_sb_node *sup;
0078     struct ubifs_mst_node *mst;
0079     struct ubifs_idx_node *idx;
0080     struct ubifs_branch *br;
0081     struct ubifs_ino_node *ino;
0082     struct ubifs_cs_node *cs;
0083     union ubifs_key key;
0084     int err, tmp, jnl_lebs, log_lebs, max_buds, main_lebs, main_first;
0085     int lpt_lebs, lpt_first, orph_lebs, big_lpt, ino_waste, sup_flags = 0;
0086     int min_leb_cnt = UBIFS_MIN_LEB_CNT;
0087     int idx_node_size;
0088     long long tmp64, main_bytes;
0089     __le64 tmp_le64;
0090     struct timespec64 ts;
0091     u8 hash[UBIFS_HASH_ARR_SZ];
0092     u8 hash_lpt[UBIFS_HASH_ARR_SZ];
0093 
0094     /* Some functions called from here depend on the @c->key_len filed */
0095     c->key_len = UBIFS_SK_LEN;
0096 
0097     /*
0098      * First of all, we have to calculate default file-system geometry -
0099      * log size, journal size, etc.
0100      */
0101     if (c->leb_cnt < 0x7FFFFFFF / DEFAULT_JNL_PERCENT)
0102         /* We can first multiply then divide and have no overflow */
0103         jnl_lebs = c->leb_cnt * DEFAULT_JNL_PERCENT / 100;
0104     else
0105         jnl_lebs = (c->leb_cnt / 100) * DEFAULT_JNL_PERCENT;
0106 
0107     if (jnl_lebs < UBIFS_MIN_JNL_LEBS)
0108         jnl_lebs = UBIFS_MIN_JNL_LEBS;
0109     if (jnl_lebs * c->leb_size > DEFAULT_MAX_JNL)
0110         jnl_lebs = DEFAULT_MAX_JNL / c->leb_size;
0111 
0112     /*
0113      * The log should be large enough to fit reference nodes for all bud
0114      * LEBs. Because buds do not have to start from the beginning of LEBs
0115      * (half of the LEB may contain committed data), the log should
0116      * generally be larger, make it twice as large.
0117      */
0118     tmp = 2 * (c->ref_node_alsz * jnl_lebs) + c->leb_size - 1;
0119     log_lebs = tmp / c->leb_size;
0120     /* Plus one LEB reserved for commit */
0121     log_lebs += 1;
0122     if (c->leb_cnt - min_leb_cnt > 8) {
0123         /* And some extra space to allow writes while committing */
0124         log_lebs += 1;
0125         min_leb_cnt += 1;
0126     }
0127 
0128     max_buds = jnl_lebs - log_lebs;
0129     if (max_buds < UBIFS_MIN_BUD_LEBS)
0130         max_buds = UBIFS_MIN_BUD_LEBS;
0131 
0132     /*
0133      * Orphan nodes are stored in a separate area. One node can store a lot
0134      * of orphan inode numbers, but when new orphan comes we just add a new
0135      * orphan node. At some point the nodes are consolidated into one
0136      * orphan node.
0137      */
0138     orph_lebs = UBIFS_MIN_ORPH_LEBS;
0139     if (c->leb_cnt - min_leb_cnt > 1)
0140         /*
0141          * For debugging purposes it is better to have at least 2
0142          * orphan LEBs, because the orphan subsystem would need to do
0143          * consolidations and would be stressed more.
0144          */
0145         orph_lebs += 1;
0146 
0147     main_lebs = c->leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS - log_lebs;
0148     main_lebs -= orph_lebs;
0149 
0150     lpt_first = UBIFS_LOG_LNUM + log_lebs;
0151     c->lsave_cnt = DEFAULT_LSAVE_CNT;
0152     c->max_leb_cnt = c->leb_cnt;
0153     err = ubifs_create_dflt_lpt(c, &main_lebs, lpt_first, &lpt_lebs,
0154                     &big_lpt, hash_lpt);
0155     if (err)
0156         return err;
0157 
0158     dbg_gen("LEB Properties Tree created (LEBs %d-%d)", lpt_first,
0159         lpt_first + lpt_lebs - 1);
0160 
0161     main_first = c->leb_cnt - main_lebs;
0162 
0163     sup = kzalloc(ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size), GFP_KERNEL);
0164     mst = kzalloc(c->mst_node_alsz, GFP_KERNEL);
0165     idx_node_size = ubifs_idx_node_sz(c, 1);
0166     idx = kzalloc(ALIGN(idx_node_size, c->min_io_size), GFP_KERNEL);
0167     ino = kzalloc(ALIGN(UBIFS_INO_NODE_SZ, c->min_io_size), GFP_KERNEL);
0168     cs = kzalloc(ALIGN(UBIFS_CS_NODE_SZ, c->min_io_size), GFP_KERNEL);
0169 
0170     if (!sup || !mst || !idx || !ino || !cs) {
0171         err = -ENOMEM;
0172         goto out;
0173     }
0174 
0175     /* Create default superblock */
0176 
0177     tmp64 = (long long)max_buds * c->leb_size;
0178     if (big_lpt)
0179         sup_flags |= UBIFS_FLG_BIGLPT;
0180     if (ubifs_default_version > 4)
0181         sup_flags |= UBIFS_FLG_DOUBLE_HASH;
0182 
0183     if (ubifs_authenticated(c)) {
0184         sup_flags |= UBIFS_FLG_AUTHENTICATION;
0185         sup->hash_algo = cpu_to_le16(c->auth_hash_algo);
0186         err = ubifs_hmac_wkm(c, sup->hmac_wkm);
0187         if (err)
0188             goto out;
0189     } else {
0190         sup->hash_algo = cpu_to_le16(0xffff);
0191     }
0192 
0193     sup->ch.node_type  = UBIFS_SB_NODE;
0194     sup->key_hash      = UBIFS_KEY_HASH_R5;
0195     sup->flags         = cpu_to_le32(sup_flags);
0196     sup->min_io_size   = cpu_to_le32(c->min_io_size);
0197     sup->leb_size      = cpu_to_le32(c->leb_size);
0198     sup->leb_cnt       = cpu_to_le32(c->leb_cnt);
0199     sup->max_leb_cnt   = cpu_to_le32(c->max_leb_cnt);
0200     sup->max_bud_bytes = cpu_to_le64(tmp64);
0201     sup->log_lebs      = cpu_to_le32(log_lebs);
0202     sup->lpt_lebs      = cpu_to_le32(lpt_lebs);
0203     sup->orph_lebs     = cpu_to_le32(orph_lebs);
0204     sup->jhead_cnt     = cpu_to_le32(DEFAULT_JHEADS_CNT);
0205     sup->fanout        = cpu_to_le32(DEFAULT_FANOUT);
0206     sup->lsave_cnt     = cpu_to_le32(c->lsave_cnt);
0207     sup->fmt_version   = cpu_to_le32(ubifs_default_version);
0208     sup->time_gran     = cpu_to_le32(DEFAULT_TIME_GRAN);
0209     if (c->mount_opts.override_compr)
0210         sup->default_compr = cpu_to_le16(c->mount_opts.compr_type);
0211     else
0212         sup->default_compr = cpu_to_le16(get_default_compressor(c));
0213 
0214     generate_random_uuid(sup->uuid);
0215 
0216     main_bytes = (long long)main_lebs * c->leb_size;
0217     tmp64 = div_u64(main_bytes * DEFAULT_RP_PERCENT, 100);
0218     if (tmp64 > DEFAULT_MAX_RP_SIZE)
0219         tmp64 = DEFAULT_MAX_RP_SIZE;
0220     sup->rp_size = cpu_to_le64(tmp64);
0221     sup->ro_compat_version = cpu_to_le32(UBIFS_RO_COMPAT_VERSION);
0222 
0223     dbg_gen("default superblock created at LEB 0:0");
0224 
0225     /* Create default master node */
0226 
0227     mst->ch.node_type = UBIFS_MST_NODE;
0228     mst->log_lnum     = cpu_to_le32(UBIFS_LOG_LNUM);
0229     mst->highest_inum = cpu_to_le64(UBIFS_FIRST_INO);
0230     mst->cmt_no       = 0;
0231     mst->root_lnum    = cpu_to_le32(main_first + DEFAULT_IDX_LEB);
0232     mst->root_offs    = 0;
0233     tmp = ubifs_idx_node_sz(c, 1);
0234     mst->root_len     = cpu_to_le32(tmp);
0235     mst->gc_lnum      = cpu_to_le32(main_first + DEFAULT_GC_LEB);
0236     mst->ihead_lnum   = cpu_to_le32(main_first + DEFAULT_IDX_LEB);
0237     mst->ihead_offs   = cpu_to_le32(ALIGN(tmp, c->min_io_size));
0238     mst->index_size   = cpu_to_le64(ALIGN(tmp, 8));
0239     mst->lpt_lnum     = cpu_to_le32(c->lpt_lnum);
0240     mst->lpt_offs     = cpu_to_le32(c->lpt_offs);
0241     mst->nhead_lnum   = cpu_to_le32(c->nhead_lnum);
0242     mst->nhead_offs   = cpu_to_le32(c->nhead_offs);
0243     mst->ltab_lnum    = cpu_to_le32(c->ltab_lnum);
0244     mst->ltab_offs    = cpu_to_le32(c->ltab_offs);
0245     mst->lsave_lnum   = cpu_to_le32(c->lsave_lnum);
0246     mst->lsave_offs   = cpu_to_le32(c->lsave_offs);
0247     mst->lscan_lnum   = cpu_to_le32(main_first);
0248     mst->empty_lebs   = cpu_to_le32(main_lebs - 2);
0249     mst->idx_lebs     = cpu_to_le32(1);
0250     mst->leb_cnt      = cpu_to_le32(c->leb_cnt);
0251     ubifs_copy_hash(c, hash_lpt, mst->hash_lpt);
0252 
0253     /* Calculate lprops statistics */
0254     tmp64 = main_bytes;
0255     tmp64 -= ALIGN(ubifs_idx_node_sz(c, 1), c->min_io_size);
0256     tmp64 -= ALIGN(UBIFS_INO_NODE_SZ, c->min_io_size);
0257     mst->total_free = cpu_to_le64(tmp64);
0258 
0259     tmp64 = ALIGN(ubifs_idx_node_sz(c, 1), c->min_io_size);
0260     ino_waste = ALIGN(UBIFS_INO_NODE_SZ, c->min_io_size) -
0261               UBIFS_INO_NODE_SZ;
0262     tmp64 += ino_waste;
0263     tmp64 -= ALIGN(ubifs_idx_node_sz(c, 1), 8);
0264     mst->total_dirty = cpu_to_le64(tmp64);
0265 
0266     /*  The indexing LEB does not contribute to dark space */
0267     tmp64 = ((long long)(c->main_lebs - 1) * c->dark_wm);
0268     mst->total_dark = cpu_to_le64(tmp64);
0269 
0270     mst->total_used = cpu_to_le64(UBIFS_INO_NODE_SZ);
0271 
0272     dbg_gen("default master node created at LEB %d:0", UBIFS_MST_LNUM);
0273 
0274     /* Create the root indexing node */
0275 
0276     c->key_fmt = UBIFS_SIMPLE_KEY_FMT;
0277     c->key_hash = key_r5_hash;
0278 
0279     idx->ch.node_type = UBIFS_IDX_NODE;
0280     idx->child_cnt = cpu_to_le16(1);
0281     ino_key_init(c, &key, UBIFS_ROOT_INO);
0282     br = ubifs_idx_branch(c, idx, 0);
0283     key_write_idx(c, &key, &br->key);
0284     br->lnum = cpu_to_le32(main_first + DEFAULT_DATA_LEB);
0285     br->len  = cpu_to_le32(UBIFS_INO_NODE_SZ);
0286 
0287     dbg_gen("default root indexing node created LEB %d:0",
0288         main_first + DEFAULT_IDX_LEB);
0289 
0290     /* Create default root inode */
0291 
0292     ino_key_init_flash(c, &ino->key, UBIFS_ROOT_INO);
0293     ino->ch.node_type = UBIFS_INO_NODE;
0294     ino->creat_sqnum = cpu_to_le64(++c->max_sqnum);
0295     ino->nlink = cpu_to_le32(2);
0296 
0297     ktime_get_coarse_real_ts64(&ts);
0298     tmp_le64 = cpu_to_le64(ts.tv_sec);
0299     ino->atime_sec   = tmp_le64;
0300     ino->ctime_sec   = tmp_le64;
0301     ino->mtime_sec   = tmp_le64;
0302     ino->atime_nsec  = 0;
0303     ino->ctime_nsec  = 0;
0304     ino->mtime_nsec  = 0;
0305     ino->mode = cpu_to_le32(S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO);
0306     ino->size = cpu_to_le64(UBIFS_INO_NODE_SZ);
0307 
0308     /* Set compression enabled by default */
0309     ino->flags = cpu_to_le32(UBIFS_COMPR_FL);
0310 
0311     dbg_gen("root inode created at LEB %d:0",
0312         main_first + DEFAULT_DATA_LEB);
0313 
0314     /*
0315      * The first node in the log has to be the commit start node. This is
0316      * always the case during normal file-system operation. Write a fake
0317      * commit start node to the log.
0318      */
0319 
0320     cs->ch.node_type = UBIFS_CS_NODE;
0321 
0322     err = ubifs_write_node_hmac(c, sup, UBIFS_SB_NODE_SZ, 0, 0,
0323                     offsetof(struct ubifs_sb_node, hmac));
0324     if (err)
0325         goto out;
0326 
0327     err = ubifs_write_node(c, ino, UBIFS_INO_NODE_SZ,
0328                    main_first + DEFAULT_DATA_LEB, 0);
0329     if (err)
0330         goto out;
0331 
0332     ubifs_node_calc_hash(c, ino, hash);
0333     ubifs_copy_hash(c, hash, ubifs_branch_hash(c, br));
0334 
0335     err = ubifs_write_node(c, idx, idx_node_size, main_first + DEFAULT_IDX_LEB, 0);
0336     if (err)
0337         goto out;
0338 
0339     ubifs_node_calc_hash(c, idx, hash);
0340     ubifs_copy_hash(c, hash, mst->hash_root_idx);
0341 
0342     err = ubifs_write_node_hmac(c, mst, UBIFS_MST_NODE_SZ, UBIFS_MST_LNUM, 0,
0343         offsetof(struct ubifs_mst_node, hmac));
0344     if (err)
0345         goto out;
0346 
0347     err = ubifs_write_node_hmac(c, mst, UBIFS_MST_NODE_SZ, UBIFS_MST_LNUM + 1,
0348                    0, offsetof(struct ubifs_mst_node, hmac));
0349     if (err)
0350         goto out;
0351 
0352     err = ubifs_write_node(c, cs, UBIFS_CS_NODE_SZ, UBIFS_LOG_LNUM, 0);
0353     if (err)
0354         goto out;
0355 
0356     ubifs_msg(c, "default file-system created");
0357 
0358     err = 0;
0359 out:
0360     kfree(sup);
0361     kfree(mst);
0362     kfree(idx);
0363     kfree(ino);
0364     kfree(cs);
0365 
0366     return err;
0367 }
0368 
0369 /**
0370  * validate_sb - validate superblock node.
0371  * @c: UBIFS file-system description object
0372  * @sup: superblock node
0373  *
0374  * This function validates superblock node @sup. Since most of data was read
0375  * from the superblock and stored in @c, the function validates fields in @c
0376  * instead. Returns zero in case of success and %-EINVAL in case of validation
0377  * failure.
0378  */
0379 static int validate_sb(struct ubifs_info *c, struct ubifs_sb_node *sup)
0380 {
0381     long long max_bytes;
0382     int err = 1, min_leb_cnt;
0383 
0384     if (!c->key_hash) {
0385         err = 2;
0386         goto failed;
0387     }
0388 
0389     if (sup->key_fmt != UBIFS_SIMPLE_KEY_FMT) {
0390         err = 3;
0391         goto failed;
0392     }
0393 
0394     if (le32_to_cpu(sup->min_io_size) != c->min_io_size) {
0395         ubifs_err(c, "min. I/O unit mismatch: %d in superblock, %d real",
0396               le32_to_cpu(sup->min_io_size), c->min_io_size);
0397         goto failed;
0398     }
0399 
0400     if (le32_to_cpu(sup->leb_size) != c->leb_size) {
0401         ubifs_err(c, "LEB size mismatch: %d in superblock, %d real",
0402               le32_to_cpu(sup->leb_size), c->leb_size);
0403         goto failed;
0404     }
0405 
0406     if (c->log_lebs < UBIFS_MIN_LOG_LEBS ||
0407         c->lpt_lebs < UBIFS_MIN_LPT_LEBS ||
0408         c->orph_lebs < UBIFS_MIN_ORPH_LEBS ||
0409         c->main_lebs < UBIFS_MIN_MAIN_LEBS) {
0410         err = 4;
0411         goto failed;
0412     }
0413 
0414     /*
0415      * Calculate minimum allowed amount of main area LEBs. This is very
0416      * similar to %UBIFS_MIN_LEB_CNT, but we take into account real what we
0417      * have just read from the superblock.
0418      */
0419     min_leb_cnt = UBIFS_SB_LEBS + UBIFS_MST_LEBS + c->log_lebs;
0420     min_leb_cnt += c->lpt_lebs + c->orph_lebs + c->jhead_cnt + 6;
0421 
0422     if (c->leb_cnt < min_leb_cnt || c->leb_cnt > c->vi.size) {
0423         ubifs_err(c, "bad LEB count: %d in superblock, %d on UBI volume, %d minimum required",
0424               c->leb_cnt, c->vi.size, min_leb_cnt);
0425         goto failed;
0426     }
0427 
0428     if (c->max_leb_cnt < c->leb_cnt) {
0429         ubifs_err(c, "max. LEB count %d less than LEB count %d",
0430               c->max_leb_cnt, c->leb_cnt);
0431         goto failed;
0432     }
0433 
0434     if (c->main_lebs < UBIFS_MIN_MAIN_LEBS) {
0435         ubifs_err(c, "too few main LEBs count %d, must be at least %d",
0436               c->main_lebs, UBIFS_MIN_MAIN_LEBS);
0437         goto failed;
0438     }
0439 
0440     max_bytes = (long long)c->leb_size * UBIFS_MIN_BUD_LEBS;
0441     if (c->max_bud_bytes < max_bytes) {
0442         ubifs_err(c, "too small journal (%lld bytes), must be at least %lld bytes",
0443               c->max_bud_bytes, max_bytes);
0444         goto failed;
0445     }
0446 
0447     max_bytes = (long long)c->leb_size * c->main_lebs;
0448     if (c->max_bud_bytes > max_bytes) {
0449         ubifs_err(c, "too large journal size (%lld bytes), only %lld bytes available in the main area",
0450               c->max_bud_bytes, max_bytes);
0451         goto failed;
0452     }
0453 
0454     if (c->jhead_cnt < NONDATA_JHEADS_CNT + 1 ||
0455         c->jhead_cnt > NONDATA_JHEADS_CNT + UBIFS_MAX_JHEADS) {
0456         err = 9;
0457         goto failed;
0458     }
0459 
0460     if (c->fanout < UBIFS_MIN_FANOUT ||
0461         ubifs_idx_node_sz(c, c->fanout) > c->leb_size) {
0462         err = 10;
0463         goto failed;
0464     }
0465 
0466     if (c->lsave_cnt < 0 || (c->lsave_cnt > DEFAULT_LSAVE_CNT &&
0467         c->lsave_cnt > c->max_leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS -
0468         c->log_lebs - c->lpt_lebs - c->orph_lebs)) {
0469         err = 11;
0470         goto failed;
0471     }
0472 
0473     if (UBIFS_SB_LEBS + UBIFS_MST_LEBS + c->log_lebs + c->lpt_lebs +
0474         c->orph_lebs + c->main_lebs != c->leb_cnt) {
0475         err = 12;
0476         goto failed;
0477     }
0478 
0479     if (c->default_compr >= UBIFS_COMPR_TYPES_CNT) {
0480         err = 13;
0481         goto failed;
0482     }
0483 
0484     if (c->rp_size < 0 || max_bytes < c->rp_size) {
0485         err = 14;
0486         goto failed;
0487     }
0488 
0489     if (le32_to_cpu(sup->time_gran) > 1000000000 ||
0490         le32_to_cpu(sup->time_gran) < 1) {
0491         err = 15;
0492         goto failed;
0493     }
0494 
0495     if (!c->double_hash && c->fmt_version >= 5) {
0496         err = 16;
0497         goto failed;
0498     }
0499 
0500     if (c->encrypted && c->fmt_version < 5) {
0501         err = 17;
0502         goto failed;
0503     }
0504 
0505     return 0;
0506 
0507 failed:
0508     ubifs_err(c, "bad superblock, error %d", err);
0509     ubifs_dump_node(c, sup, ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size));
0510     return -EINVAL;
0511 }
0512 
0513 /**
0514  * ubifs_read_sb_node - read superblock node.
0515  * @c: UBIFS file-system description object
0516  *
0517  * This function returns a pointer to the superblock node or a negative error
0518  * code. Note, the user of this function is responsible of kfree()'ing the
0519  * returned superblock buffer.
0520  */
0521 static struct ubifs_sb_node *ubifs_read_sb_node(struct ubifs_info *c)
0522 {
0523     struct ubifs_sb_node *sup;
0524     int err;
0525 
0526     sup = kmalloc(ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size), GFP_NOFS);
0527     if (!sup)
0528         return ERR_PTR(-ENOMEM);
0529 
0530     err = ubifs_read_node(c, sup, UBIFS_SB_NODE, UBIFS_SB_NODE_SZ,
0531                   UBIFS_SB_LNUM, 0);
0532     if (err) {
0533         kfree(sup);
0534         return ERR_PTR(err);
0535     }
0536 
0537     return sup;
0538 }
0539 
0540 static int authenticate_sb_node(struct ubifs_info *c,
0541                 const struct ubifs_sb_node *sup)
0542 {
0543     unsigned int sup_flags = le32_to_cpu(sup->flags);
0544     u8 hmac_wkm[UBIFS_HMAC_ARR_SZ];
0545     int authenticated = !!(sup_flags & UBIFS_FLG_AUTHENTICATION);
0546     int hash_algo;
0547     int err;
0548 
0549     if (c->authenticated && !authenticated) {
0550         ubifs_err(c, "authenticated FS forced, but found FS without authentication");
0551         return -EINVAL;
0552     }
0553 
0554     if (!c->authenticated && authenticated) {
0555         ubifs_err(c, "authenticated FS found, but no key given");
0556         return -EINVAL;
0557     }
0558 
0559     ubifs_msg(c, "Mounting in %sauthenticated mode",
0560           c->authenticated ? "" : "un");
0561 
0562     if (!c->authenticated)
0563         return 0;
0564 
0565     if (!IS_ENABLED(CONFIG_UBIFS_FS_AUTHENTICATION))
0566         return -EOPNOTSUPP;
0567 
0568     hash_algo = le16_to_cpu(sup->hash_algo);
0569     if (hash_algo >= HASH_ALGO__LAST) {
0570         ubifs_err(c, "superblock uses unknown hash algo %d",
0571               hash_algo);
0572         return -EINVAL;
0573     }
0574 
0575     if (strcmp(hash_algo_name[hash_algo], c->auth_hash_name)) {
0576         ubifs_err(c, "This filesystem uses %s for hashing,"
0577                  " but %s is specified", hash_algo_name[hash_algo],
0578                  c->auth_hash_name);
0579         return -EINVAL;
0580     }
0581 
0582     /*
0583      * The super block node can either be authenticated by a HMAC or
0584      * by a signature in a ubifs_sig_node directly following the
0585      * super block node to support offline image creation.
0586      */
0587     if (ubifs_hmac_zero(c, sup->hmac)) {
0588         err = ubifs_sb_verify_signature(c, sup);
0589     } else {
0590         err = ubifs_hmac_wkm(c, hmac_wkm);
0591         if (err)
0592             return err;
0593         if (ubifs_check_hmac(c, hmac_wkm, sup->hmac_wkm)) {
0594             ubifs_err(c, "provided key does not fit");
0595             return -ENOKEY;
0596         }
0597         err = ubifs_node_verify_hmac(c, sup, sizeof(*sup),
0598                          offsetof(struct ubifs_sb_node,
0599                               hmac));
0600     }
0601 
0602     if (err)
0603         ubifs_err(c, "Failed to authenticate superblock: %d", err);
0604 
0605     return err;
0606 }
0607 
0608 /**
0609  * ubifs_write_sb_node - write superblock node.
0610  * @c: UBIFS file-system description object
0611  * @sup: superblock node read with 'ubifs_read_sb_node()'
0612  *
0613  * This function returns %0 on success and a negative error code on failure.
0614  */
0615 int ubifs_write_sb_node(struct ubifs_info *c, struct ubifs_sb_node *sup)
0616 {
0617     int len = ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size);
0618     int err;
0619 
0620     err = ubifs_prepare_node_hmac(c, sup, UBIFS_SB_NODE_SZ,
0621                       offsetof(struct ubifs_sb_node, hmac), 1);
0622     if (err)
0623         return err;
0624 
0625     return ubifs_leb_change(c, UBIFS_SB_LNUM, sup, len);
0626 }
0627 
0628 /**
0629  * ubifs_read_superblock - read superblock.
0630  * @c: UBIFS file-system description object
0631  *
0632  * This function finds, reads and checks the superblock. If an empty UBI volume
0633  * is being mounted, this function creates default superblock. Returns zero in
0634  * case of success, and a negative error code in case of failure.
0635  */
0636 int ubifs_read_superblock(struct ubifs_info *c)
0637 {
0638     int err, sup_flags;
0639     struct ubifs_sb_node *sup;
0640 
0641     if (c->empty) {
0642         err = create_default_filesystem(c);
0643         if (err)
0644             return err;
0645     }
0646 
0647     sup = ubifs_read_sb_node(c);
0648     if (IS_ERR(sup))
0649         return PTR_ERR(sup);
0650 
0651     c->sup_node = sup;
0652 
0653     c->fmt_version = le32_to_cpu(sup->fmt_version);
0654     c->ro_compat_version = le32_to_cpu(sup->ro_compat_version);
0655 
0656     /*
0657      * The software supports all previous versions but not future versions,
0658      * due to the unavailability of time-travelling equipment.
0659      */
0660     if (c->fmt_version > UBIFS_FORMAT_VERSION) {
0661         ubifs_assert(c, !c->ro_media || c->ro_mount);
0662         if (!c->ro_mount ||
0663             c->ro_compat_version > UBIFS_RO_COMPAT_VERSION) {
0664             ubifs_err(c, "on-flash format version is w%d/r%d, but software only supports up to version w%d/r%d",
0665                   c->fmt_version, c->ro_compat_version,
0666                   UBIFS_FORMAT_VERSION,
0667                   UBIFS_RO_COMPAT_VERSION);
0668             if (c->ro_compat_version <= UBIFS_RO_COMPAT_VERSION) {
0669                 ubifs_msg(c, "only R/O mounting is possible");
0670                 err = -EROFS;
0671             } else
0672                 err = -EINVAL;
0673             goto out;
0674         }
0675 
0676         /*
0677          * The FS is mounted R/O, and the media format is
0678          * R/O-compatible with the UBIFS implementation, so we can
0679          * mount.
0680          */
0681         c->rw_incompat = 1;
0682     }
0683 
0684     if (c->fmt_version < 3) {
0685         ubifs_err(c, "on-flash format version %d is not supported",
0686               c->fmt_version);
0687         err = -EINVAL;
0688         goto out;
0689     }
0690 
0691     switch (sup->key_hash) {
0692     case UBIFS_KEY_HASH_R5:
0693         c->key_hash = key_r5_hash;
0694         c->key_hash_type = UBIFS_KEY_HASH_R5;
0695         break;
0696 
0697     case UBIFS_KEY_HASH_TEST:
0698         c->key_hash = key_test_hash;
0699         c->key_hash_type = UBIFS_KEY_HASH_TEST;
0700         break;
0701     }
0702 
0703     c->key_fmt = sup->key_fmt;
0704 
0705     switch (c->key_fmt) {
0706     case UBIFS_SIMPLE_KEY_FMT:
0707         c->key_len = UBIFS_SK_LEN;
0708         break;
0709     default:
0710         ubifs_err(c, "unsupported key format");
0711         err = -EINVAL;
0712         goto out;
0713     }
0714 
0715     c->leb_cnt       = le32_to_cpu(sup->leb_cnt);
0716     c->max_leb_cnt   = le32_to_cpu(sup->max_leb_cnt);
0717     c->max_bud_bytes = le64_to_cpu(sup->max_bud_bytes);
0718     c->log_lebs      = le32_to_cpu(sup->log_lebs);
0719     c->lpt_lebs      = le32_to_cpu(sup->lpt_lebs);
0720     c->orph_lebs     = le32_to_cpu(sup->orph_lebs);
0721     c->jhead_cnt     = le32_to_cpu(sup->jhead_cnt) + NONDATA_JHEADS_CNT;
0722     c->fanout        = le32_to_cpu(sup->fanout);
0723     c->lsave_cnt     = le32_to_cpu(sup->lsave_cnt);
0724     c->rp_size       = le64_to_cpu(sup->rp_size);
0725     c->rp_uid        = make_kuid(&init_user_ns, le32_to_cpu(sup->rp_uid));
0726     c->rp_gid        = make_kgid(&init_user_ns, le32_to_cpu(sup->rp_gid));
0727     sup_flags        = le32_to_cpu(sup->flags);
0728     if (!c->mount_opts.override_compr)
0729         c->default_compr = le16_to_cpu(sup->default_compr);
0730 
0731     c->vfs_sb->s_time_gran = le32_to_cpu(sup->time_gran);
0732     memcpy(&c->uuid, &sup->uuid, 16);
0733     c->big_lpt = !!(sup_flags & UBIFS_FLG_BIGLPT);
0734     c->space_fixup = !!(sup_flags & UBIFS_FLG_SPACE_FIXUP);
0735     c->double_hash = !!(sup_flags & UBIFS_FLG_DOUBLE_HASH);
0736     c->encrypted = !!(sup_flags & UBIFS_FLG_ENCRYPTION);
0737 
0738     err = authenticate_sb_node(c, sup);
0739     if (err)
0740         goto out;
0741 
0742     if ((sup_flags & ~UBIFS_FLG_MASK) != 0) {
0743         ubifs_err(c, "Unknown feature flags found: %#x",
0744               sup_flags & ~UBIFS_FLG_MASK);
0745         err = -EINVAL;
0746         goto out;
0747     }
0748 
0749     if (!IS_ENABLED(CONFIG_FS_ENCRYPTION) && c->encrypted) {
0750         ubifs_err(c, "file system contains encrypted files but UBIFS"
0751                  " was built without crypto support.");
0752         err = -EINVAL;
0753         goto out;
0754     }
0755 
0756     /* Automatically increase file system size to the maximum size */
0757     if (c->leb_cnt < c->vi.size && c->leb_cnt < c->max_leb_cnt) {
0758         int old_leb_cnt = c->leb_cnt;
0759 
0760         c->leb_cnt = min_t(int, c->max_leb_cnt, c->vi.size);
0761         sup->leb_cnt = cpu_to_le32(c->leb_cnt);
0762 
0763         c->superblock_need_write = 1;
0764 
0765         dbg_mnt("Auto resizing from %d LEBs to %d LEBs",
0766             old_leb_cnt, c->leb_cnt);
0767     }
0768 
0769     c->log_bytes = (long long)c->log_lebs * c->leb_size;
0770     c->log_last = UBIFS_LOG_LNUM + c->log_lebs - 1;
0771     c->lpt_first = UBIFS_LOG_LNUM + c->log_lebs;
0772     c->lpt_last = c->lpt_first + c->lpt_lebs - 1;
0773     c->orph_first = c->lpt_last + 1;
0774     c->orph_last = c->orph_first + c->orph_lebs - 1;
0775     c->main_lebs = c->leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS;
0776     c->main_lebs -= c->log_lebs + c->lpt_lebs + c->orph_lebs;
0777     c->main_first = c->leb_cnt - c->main_lebs;
0778 
0779     err = validate_sb(c, sup);
0780 out:
0781     return err;
0782 }
0783 
0784 /**
0785  * fixup_leb - fixup/unmap an LEB containing free space.
0786  * @c: UBIFS file-system description object
0787  * @lnum: the LEB number to fix up
0788  * @len: number of used bytes in LEB (starting at offset 0)
0789  *
0790  * This function reads the contents of the given LEB number @lnum, then fixes
0791  * it up, so that empty min. I/O units in the end of LEB are actually erased on
0792  * flash (rather than being just all-0xff real data). If the LEB is completely
0793  * empty, it is simply unmapped.
0794  */
0795 static int fixup_leb(struct ubifs_info *c, int lnum, int len)
0796 {
0797     int err;
0798 
0799     ubifs_assert(c, len >= 0);
0800     ubifs_assert(c, len % c->min_io_size == 0);
0801     ubifs_assert(c, len < c->leb_size);
0802 
0803     if (len == 0) {
0804         dbg_mnt("unmap empty LEB %d", lnum);
0805         return ubifs_leb_unmap(c, lnum);
0806     }
0807 
0808     dbg_mnt("fixup LEB %d, data len %d", lnum, len);
0809     err = ubifs_leb_read(c, lnum, c->sbuf, 0, len, 1);
0810     if (err)
0811         return err;
0812 
0813     return ubifs_leb_change(c, lnum, c->sbuf, len);
0814 }
0815 
0816 /**
0817  * fixup_free_space - find & remap all LEBs containing free space.
0818  * @c: UBIFS file-system description object
0819  *
0820  * This function walks through all LEBs in the filesystem and fiexes up those
0821  * containing free/empty space.
0822  */
0823 static int fixup_free_space(struct ubifs_info *c)
0824 {
0825     int lnum, err = 0;
0826     struct ubifs_lprops *lprops;
0827 
0828     ubifs_get_lprops(c);
0829 
0830     /* Fixup LEBs in the master area */
0831     for (lnum = UBIFS_MST_LNUM; lnum < UBIFS_LOG_LNUM; lnum++) {
0832         err = fixup_leb(c, lnum, c->mst_offs + c->mst_node_alsz);
0833         if (err)
0834             goto out;
0835     }
0836 
0837     /* Unmap unused log LEBs */
0838     lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
0839     while (lnum != c->ltail_lnum) {
0840         err = fixup_leb(c, lnum, 0);
0841         if (err)
0842             goto out;
0843         lnum = ubifs_next_log_lnum(c, lnum);
0844     }
0845 
0846     /*
0847      * Fixup the log head which contains the only a CS node at the
0848      * beginning.
0849      */
0850     err = fixup_leb(c, c->lhead_lnum,
0851             ALIGN(UBIFS_CS_NODE_SZ, c->min_io_size));
0852     if (err)
0853         goto out;
0854 
0855     /* Fixup LEBs in the LPT area */
0856     for (lnum = c->lpt_first; lnum <= c->lpt_last; lnum++) {
0857         int free = c->ltab[lnum - c->lpt_first].free;
0858 
0859         if (free > 0) {
0860             err = fixup_leb(c, lnum, c->leb_size - free);
0861             if (err)
0862                 goto out;
0863         }
0864     }
0865 
0866     /* Unmap LEBs in the orphans area */
0867     for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
0868         err = fixup_leb(c, lnum, 0);
0869         if (err)
0870             goto out;
0871     }
0872 
0873     /* Fixup LEBs in the main area */
0874     for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) {
0875         lprops = ubifs_lpt_lookup(c, lnum);
0876         if (IS_ERR(lprops)) {
0877             err = PTR_ERR(lprops);
0878             goto out;
0879         }
0880 
0881         if (lprops->free > 0) {
0882             err = fixup_leb(c, lnum, c->leb_size - lprops->free);
0883             if (err)
0884                 goto out;
0885         }
0886     }
0887 
0888 out:
0889     ubifs_release_lprops(c);
0890     return err;
0891 }
0892 
0893 /**
0894  * ubifs_fixup_free_space - find & fix all LEBs with free space.
0895  * @c: UBIFS file-system description object
0896  *
0897  * This function fixes up LEBs containing free space on first mount, if the
0898  * appropriate flag was set when the FS was created. Each LEB with one or more
0899  * empty min. I/O unit (i.e. free-space-count > 0) is re-written, to make sure
0900  * the free space is actually erased. E.g., this is necessary for some NAND
0901  * chips, since the free space may have been programmed like real "0xff" data
0902  * (generating a non-0xff ECC), causing future writes to the not-really-erased
0903  * NAND pages to behave badly. After the space is fixed up, the superblock flag
0904  * is cleared, so that this is skipped for all future mounts.
0905  */
0906 int ubifs_fixup_free_space(struct ubifs_info *c)
0907 {
0908     int err;
0909     struct ubifs_sb_node *sup = c->sup_node;
0910 
0911     ubifs_assert(c, c->space_fixup);
0912     ubifs_assert(c, !c->ro_mount);
0913 
0914     ubifs_msg(c, "start fixing up free space");
0915 
0916     err = fixup_free_space(c);
0917     if (err)
0918         return err;
0919 
0920     /* Free-space fixup is no longer required */
0921     c->space_fixup = 0;
0922     sup->flags &= cpu_to_le32(~UBIFS_FLG_SPACE_FIXUP);
0923 
0924     c->superblock_need_write = 1;
0925 
0926     ubifs_msg(c, "free space fixup complete");
0927     return err;
0928 }
0929 
0930 int ubifs_enable_encryption(struct ubifs_info *c)
0931 {
0932     int err;
0933     struct ubifs_sb_node *sup = c->sup_node;
0934 
0935     if (!IS_ENABLED(CONFIG_FS_ENCRYPTION))
0936         return -EOPNOTSUPP;
0937 
0938     if (c->encrypted)
0939         return 0;
0940 
0941     if (c->ro_mount || c->ro_media)
0942         return -EROFS;
0943 
0944     if (c->fmt_version < 5) {
0945         ubifs_err(c, "on-flash format version 5 is needed for encryption");
0946         return -EINVAL;
0947     }
0948 
0949     sup->flags |= cpu_to_le32(UBIFS_FLG_ENCRYPTION);
0950 
0951     err = ubifs_write_sb_node(c, sup);
0952     if (!err)
0953         c->encrypted = 1;
0954 
0955     return err;
0956 }