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);
238
c->fmt_version = le32_to_cpu(sup->fmt_version);
239
c->ro_compat_version = le32_to_cpu(sup->ro_compat_version);
242
* The software supports all previous versions but not future versions,
243
* due to the unavailability of time-travelling equipment.
245
if (c->fmt_version > UBIFS_FORMAT_VERSION) {
246
struct super_block *sb = c->vfs_sb;
247
int mounting_ro = sb->s_flags & MS_RDONLY;
249
ubifs_assert(!c->ro_media || mounting_ro);
251
c->ro_compat_version > UBIFS_RO_COMPAT_VERSION) {
252
ubifs_err("on-flash format version is w%d/r%d, but "
253
"software only supports up to version "
254
"w%d/r%d", c->fmt_version,
255
c->ro_compat_version, UBIFS_FORMAT_VERSION,
256
UBIFS_RO_COMPAT_VERSION);
257
if (c->ro_compat_version <= UBIFS_RO_COMPAT_VERSION) {
258
ubifs_msg("only R/O mounting is possible");
266
* The FS is mounted R/O, and the media format is
267
* R/O-compatible with the UBIFS implementation, so we can
273
if (c->fmt_version < 3) {
274
ubifs_err("on-flash format version %d is not supported",
280
switch (sup->key_hash) {
281
case UBIFS_KEY_HASH_R5:
282
c->key_hash = key_r5_hash;
283
c->key_hash_type = UBIFS_KEY_HASH_R5;
286
case UBIFS_KEY_HASH_TEST:
287
c->key_hash = key_test_hash;
288
c->key_hash_type = UBIFS_KEY_HASH_TEST;
292
c->key_fmt = sup->key_fmt;
294
switch (c->key_fmt) {
295
case UBIFS_SIMPLE_KEY_FMT:
296
c->key_len = UBIFS_SK_LEN;
299
ubifs_err("unsupported key format");
304
c->leb_cnt = le32_to_cpu(sup->leb_cnt);
305
c->max_leb_cnt = le32_to_cpu(sup->max_leb_cnt);
306
c->max_bud_bytes = le64_to_cpu(sup->max_bud_bytes);
307
c->log_lebs = le32_to_cpu(sup->log_lebs);
308
c->lpt_lebs = le32_to_cpu(sup->lpt_lebs);
309
c->orph_lebs = le32_to_cpu(sup->orph_lebs);
310
c->jhead_cnt = le32_to_cpu(sup->jhead_cnt) + NONDATA_JHEADS_CNT;
311
c->fanout = le32_to_cpu(sup->fanout);
312
c->lsave_cnt = le32_to_cpu(sup->lsave_cnt);
313
c->default_compr = le16_to_cpu(sup->default_compr);
314
c->rp_size = le64_to_cpu(sup->rp_size);
315
c->rp_uid = le32_to_cpu(sup->rp_uid);
316
c->rp_gid = le32_to_cpu(sup->rp_gid);
317
sup_flags = le32_to_cpu(sup->flags);
319
c->vfs_sb->s_time_gran = le32_to_cpu(sup->time_gran);
320
memcpy(&c->uuid, &sup->uuid, 16);
321
c->big_lpt = !!(sup_flags & UBIFS_FLG_BIGLPT);
323
/* Automatically increase file system size to the maximum size */
324
c->old_leb_cnt = c->leb_cnt;
325
if (c->leb_cnt < c->vi.size && c->leb_cnt < c->max_leb_cnt) {
326
c->leb_cnt = min_t(int, c->max_leb_cnt, c->vi.size);
327
dbg_mnt("Auto resizing (ro) from %d LEBs to %d LEBs",
328
c->old_leb_cnt, c->leb_cnt);
331
c->log_bytes = (long long)c->log_lebs * c->leb_size;
332
c->log_last = UBIFS_LOG_LNUM + c->log_lebs - 1;
333
c->lpt_first = UBIFS_LOG_LNUM + c->log_lebs;
334
c->lpt_last = c->lpt_first + c->lpt_lebs - 1;
335
c->orph_first = c->lpt_last + 1;
336
c->orph_last = c->orph_first + c->orph_lebs - 1;
337
c->main_lebs = c->leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS;
338
c->main_lebs -= c->log_lebs + c->lpt_lebs + c->orph_lebs;
339
c->main_first = c->leb_cnt - c->main_lebs;
340
c->report_rp_size = ubifs_reported_space(c, c->rp_size);
342
err = validate_sb(c, sup);