2 * This file is part of UBIFS.
4 * Copyright (C) 2006-2008 Nokia Corporation.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Authors: Artem Bityutskiy (Битюцкий Артём)
24 * This file implements UBIFS superblock. The superblock is stored at the first
25 * LEB of the volume and is never changed by UBIFS. Only user-space tools may
26 * change it. The superblock node mostly contains geometry information.
32 * Default journal size in logical eraseblocks as a percent of total
35 #define DEFAULT_JNL_PERCENT 5
37 /* Default maximum journal size in bytes */
38 #define DEFAULT_MAX_JNL (32*1024*1024)
40 /* Default indexing tree fanout */
41 #define DEFAULT_FANOUT 8
43 /* Default number of data journal heads */
44 #define DEFAULT_JHEADS_CNT 1
46 /* Default positions of different LEBs in the main area */
47 #define DEFAULT_IDX_LEB 0
48 #define DEFAULT_DATA_LEB 1
49 #define DEFAULT_GC_LEB 2
51 /* Default number of LEB numbers in LPT's save table */
52 #define DEFAULT_LSAVE_CNT 256
54 /* Default reserved pool size as a percent of maximum free space */
55 #define DEFAULT_RP_PERCENT 5
57 /* The default maximum size of reserved pool in bytes */
58 #define DEFAULT_MAX_RP_SIZE (5*1024*1024)
60 /* Default time granularity in nanoseconds */
61 #define DEFAULT_TIME_GRAN 1000000000
64 * validate_sb - validate superblock node.
65 * @c: UBIFS file-system description object
66 * @sup: superblock node
68 * This function validates superblock node @sup. Since most of data was read
69 * from the superblock and stored in @c, the function validates fields in @c
70 * instead. Returns zero in case of success and %-EINVAL in case of validation
73 static int validate_sb(struct ubifs_info *c, struct ubifs_sb_node *sup)
76 int err = 1, min_leb_cnt;
83 if (sup->key_fmt != UBIFS_SIMPLE_KEY_FMT) {
88 if (le32_to_cpu(sup->min_io_size) != c->min_io_size) {
89 ubifs_err("min. I/O unit mismatch: %d in superblock, %d real",
90 le32_to_cpu(sup->min_io_size), c->min_io_size);
94 if (le32_to_cpu(sup->leb_size) != c->leb_size) {
95 ubifs_err("LEB size mismatch: %d in superblock, %d real",
96 le32_to_cpu(sup->leb_size), c->leb_size);
100 if (c->log_lebs < UBIFS_MIN_LOG_LEBS ||
101 c->lpt_lebs < UBIFS_MIN_LPT_LEBS ||
102 c->orph_lebs < UBIFS_MIN_ORPH_LEBS ||
103 c->main_lebs < UBIFS_MIN_MAIN_LEBS) {
109 * Calculate minimum allowed amount of main area LEBs. This is very
110 * similar to %UBIFS_MIN_LEB_CNT, but we take into account real what we
111 * have just read from the superblock.
113 min_leb_cnt = UBIFS_SB_LEBS + UBIFS_MST_LEBS + c->log_lebs;
114 min_leb_cnt += c->lpt_lebs + c->orph_lebs + c->jhead_cnt + 6;
116 if (c->leb_cnt < min_leb_cnt || c->leb_cnt > c->vi.size) {
117 ubifs_err("bad LEB count: %d in superblock, %d on UBI volume, "
118 "%d minimum required", c->leb_cnt, c->vi.size,
123 if (c->max_leb_cnt < c->leb_cnt) {
124 ubifs_err("max. LEB count %d less than LEB count %d",
125 c->max_leb_cnt, c->leb_cnt);
129 if (c->main_lebs < UBIFS_MIN_MAIN_LEBS) {
134 if (c->max_bud_bytes < (long long)c->leb_size * UBIFS_MIN_BUD_LEBS ||
135 c->max_bud_bytes > (long long)c->leb_size * c->main_lebs) {
140 if (c->jhead_cnt < NONDATA_JHEADS_CNT + 1 ||
141 c->jhead_cnt > NONDATA_JHEADS_CNT + UBIFS_MAX_JHEADS) {
146 if (c->fanout < UBIFS_MIN_FANOUT ||
147 ubifs_idx_node_sz(c, c->fanout) > c->leb_size) {
152 if (c->lsave_cnt < 0 || (c->lsave_cnt > DEFAULT_LSAVE_CNT &&
153 c->lsave_cnt > c->max_leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS -
154 c->log_lebs - c->lpt_lebs - c->orph_lebs)) {
159 if (UBIFS_SB_LEBS + UBIFS_MST_LEBS + c->log_lebs + c->lpt_lebs +
160 c->orph_lebs + c->main_lebs != c->leb_cnt) {
165 if (c->default_compr < 0 || c->default_compr >= UBIFS_COMPR_TYPES_CNT) {
170 max_bytes = c->main_lebs * (long long)c->leb_size;
171 if (c->rp_size < 0 || max_bytes < c->rp_size) {
176 if (le32_to_cpu(sup->time_gran) > 1000000000 ||
177 le32_to_cpu(sup->time_gran) < 1) {
185 ubifs_err("bad superblock, error %d", err);
186 dbg_dump_node(c, sup);
191 * ubifs_read_sb_node - read superblock node.
192 * @c: UBIFS file-system description object
194 * This function returns a pointer to the superblock node or a negative error
197 struct ubifs_sb_node *ubifs_read_sb_node(struct ubifs_info *c)
199 struct ubifs_sb_node *sup;
202 sup = kmalloc(ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size), GFP_NOFS);
204 return ERR_PTR(-ENOMEM);
206 err = ubifs_read_node(c, sup, UBIFS_SB_NODE, UBIFS_SB_NODE_SZ,
217 * ubifs_read_superblock - read superblock.
218 * @c: UBIFS file-system description object
220 * This function finds, reads and checks the superblock. If an empty UBI volume
221 * is being mounted, this function creates default superblock. Returns zero in
222 * case of success, and a negative error code in case of failure.
224 int ubifs_read_superblock(struct ubifs_info *c)
227 struct ubifs_sb_node *sup;
230 printf("No UBIFS filesystem found!\n");
234 sup = ubifs_read_sb_node(c);
239 * The software supports all previous versions but not future versions,
240 * due to the unavailability of time-travelling equipment.
242 c->fmt_version = le32_to_cpu(sup->fmt_version);
243 if (c->fmt_version > UBIFS_FORMAT_VERSION) {
244 ubifs_err("on-flash format version is %d, but software only "
245 "supports up to version %d", c->fmt_version,
246 UBIFS_FORMAT_VERSION);
251 if (c->fmt_version < 3) {
252 ubifs_err("on-flash format version %d is not supported",
258 switch (sup->key_hash) {
259 case UBIFS_KEY_HASH_R5:
260 c->key_hash = key_r5_hash;
261 c->key_hash_type = UBIFS_KEY_HASH_R5;
264 case UBIFS_KEY_HASH_TEST:
265 c->key_hash = key_test_hash;
266 c->key_hash_type = UBIFS_KEY_HASH_TEST;
270 c->key_fmt = sup->key_fmt;
272 switch (c->key_fmt) {
273 case UBIFS_SIMPLE_KEY_FMT:
274 c->key_len = UBIFS_SK_LEN;
277 ubifs_err("unsupported key format");
282 c->leb_cnt = le32_to_cpu(sup->leb_cnt);
283 c->max_leb_cnt = le32_to_cpu(sup->max_leb_cnt);
284 c->max_bud_bytes = le64_to_cpu(sup->max_bud_bytes);
285 c->log_lebs = le32_to_cpu(sup->log_lebs);
286 c->lpt_lebs = le32_to_cpu(sup->lpt_lebs);
287 c->orph_lebs = le32_to_cpu(sup->orph_lebs);
288 c->jhead_cnt = le32_to_cpu(sup->jhead_cnt) + NONDATA_JHEADS_CNT;
289 c->fanout = le32_to_cpu(sup->fanout);
290 c->lsave_cnt = le32_to_cpu(sup->lsave_cnt);
291 c->default_compr = le16_to_cpu(sup->default_compr);
292 c->rp_size = le64_to_cpu(sup->rp_size);
293 c->rp_uid = le32_to_cpu(sup->rp_uid);
294 c->rp_gid = le32_to_cpu(sup->rp_gid);
295 sup_flags = le32_to_cpu(sup->flags);
297 c->vfs_sb->s_time_gran = le32_to_cpu(sup->time_gran);
298 memcpy(&c->uuid, &sup->uuid, 16);
299 c->big_lpt = !!(sup_flags & UBIFS_FLG_BIGLPT);
301 /* Automatically increase file system size to the maximum size */
302 c->old_leb_cnt = c->leb_cnt;
303 if (c->leb_cnt < c->vi.size && c->leb_cnt < c->max_leb_cnt) {
304 c->leb_cnt = min_t(int, c->max_leb_cnt, c->vi.size);
305 dbg_mnt("Auto resizing (ro) from %d LEBs to %d LEBs",
306 c->old_leb_cnt, c->leb_cnt);
309 c->log_bytes = (long long)c->log_lebs * c->leb_size;
310 c->log_last = UBIFS_LOG_LNUM + c->log_lebs - 1;
311 c->lpt_first = UBIFS_LOG_LNUM + c->log_lebs;
312 c->lpt_last = c->lpt_first + c->lpt_lebs - 1;
313 c->orph_first = c->lpt_last + 1;
314 c->orph_last = c->orph_first + c->orph_lebs - 1;
315 c->main_lebs = c->leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS;
316 c->main_lebs -= c->log_lebs + c->lpt_lebs + c->orph_lebs;
317 c->main_first = c->leb_cnt - c->main_lebs;
318 c->report_rp_size = ubifs_reported_space(c, c->rp_size);
320 err = validate_sb(c, sup);