blob: 9708fda95229643da8f819feecba16ee199d6460 [file] [log] [blame]
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001/*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation.
5 *
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.
9 *
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
13 * more details.
14 *
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
18 *
19 * Authors: Artem Bityutskiy (Битюцкий Артём)
20 * Adrian Hunter
21 */
22
23/*
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.
27 */
28
29#include "ubifs.h"
30
31/*
32 * Default journal size in logical eraseblocks as a percent of total
33 * flash size.
34 */
35#define DEFAULT_JNL_PERCENT 5
36
37/* Default maximum journal size in bytes */
38#define DEFAULT_MAX_JNL (32*1024*1024)
39
40/* Default indexing tree fanout */
41#define DEFAULT_FANOUT 8
42
43/* Default number of data journal heads */
44#define DEFAULT_JHEADS_CNT 1
45
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
50
51/* Default number of LEB numbers in LPT's save table */
52#define DEFAULT_LSAVE_CNT 256
53
54/* Default reserved pool size as a percent of maximum free space */
55#define DEFAULT_RP_PERCENT 5
56
57/* The default maximum size of reserved pool in bytes */
58#define DEFAULT_MAX_RP_SIZE (5*1024*1024)
59
60/* Default time granularity in nanoseconds */
61#define DEFAULT_TIME_GRAN 1000000000
62
63/**
64 * validate_sb - validate superblock node.
65 * @c: UBIFS file-system description object
66 * @sup: superblock node
67 *
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
71 * failure.
72 */
73static int validate_sb(struct ubifs_info *c, struct ubifs_sb_node *sup)
74{
75 long long max_bytes;
76 int err = 1, min_leb_cnt;
77
78 if (!c->key_hash) {
79 err = 2;
80 goto failed;
81 }
82
83 if (sup->key_fmt != UBIFS_SIMPLE_KEY_FMT) {
84 err = 3;
85 goto failed;
86 }
87
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);
91 goto failed;
92 }
93
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);
97 goto failed;
98 }
99
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) {
104 err = 4;
105 goto failed;
106 }
107
108 /*
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.
112 */
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;
115
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,
119 min_leb_cnt);
120 goto failed;
121 }
122
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);
126 goto failed;
127 }
128
129 if (c->main_lebs < UBIFS_MIN_MAIN_LEBS) {
130 err = 7;
131 goto failed;
132 }
133
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) {
136 err = 8;
137 goto failed;
138 }
139
140 if (c->jhead_cnt < NONDATA_JHEADS_CNT + 1 ||
141 c->jhead_cnt > NONDATA_JHEADS_CNT + UBIFS_MAX_JHEADS) {
142 err = 9;
143 goto failed;
144 }
145
146 if (c->fanout < UBIFS_MIN_FANOUT ||
147 ubifs_idx_node_sz(c, c->fanout) > c->leb_size) {
148 err = 10;
149 goto failed;
150 }
151
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)) {
155 err = 11;
156 goto failed;
157 }
158
159 if (UBIFS_SB_LEBS + UBIFS_MST_LEBS + c->log_lebs + c->lpt_lebs +
160 c->orph_lebs + c->main_lebs != c->leb_cnt) {
161 err = 12;
162 goto failed;
163 }
164
165 if (c->default_compr < 0 || c->default_compr >= UBIFS_COMPR_TYPES_CNT) {
166 err = 13;
167 goto failed;
168 }
169
170 max_bytes = c->main_lebs * (long long)c->leb_size;
171 if (c->rp_size < 0 || max_bytes < c->rp_size) {
172 err = 14;
173 goto failed;
174 }
175
176 if (le32_to_cpu(sup->time_gran) > 1000000000 ||
177 le32_to_cpu(sup->time_gran) < 1) {
178 err = 15;
179 goto failed;
180 }
181
182 return 0;
183
184failed:
185 ubifs_err("bad superblock, error %d", err);
186 dbg_dump_node(c, sup);
187 return -EINVAL;
188}
189
190/**
191 * ubifs_read_sb_node - read superblock node.
192 * @c: UBIFS file-system description object
193 *
194 * This function returns a pointer to the superblock node or a negative error
195 * code.
196 */
197struct ubifs_sb_node *ubifs_read_sb_node(struct ubifs_info *c)
198{
199 struct ubifs_sb_node *sup;
200 int err;
201
202 sup = kmalloc(ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size), GFP_NOFS);
203 if (!sup)
204 return ERR_PTR(-ENOMEM);
205
206 err = ubifs_read_node(c, sup, UBIFS_SB_NODE, UBIFS_SB_NODE_SZ,
207 UBIFS_SB_LNUM, 0);
208 if (err) {
209 kfree(sup);
210 return ERR_PTR(err);
211 }
212
213 return sup;
214}
215
216/**
217 * ubifs_read_superblock - read superblock.
218 * @c: UBIFS file-system description object
219 *
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.
223 */
224int ubifs_read_superblock(struct ubifs_info *c)
225{
226 int err, sup_flags;
227 struct ubifs_sb_node *sup;
228
229 if (c->empty) {
230 printf("No UBIFS filesystem found!\n");
231 return -1;
232 }
233
234 sup = ubifs_read_sb_node(c);
235 if (IS_ERR(sup))
236 return PTR_ERR(sup);
237
238 /*
239 * The software supports all previous versions but not future versions,
240 * due to the unavailability of time-travelling equipment.
241 */
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);
247 err = -EINVAL;
248 goto out;
249 }
250
251 if (c->fmt_version < 3) {
252 ubifs_err("on-flash format version %d is not supported",
253 c->fmt_version);
254 err = -EINVAL;
255 goto out;
256 }
257
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;
262 break;
263
264 case UBIFS_KEY_HASH_TEST:
265 c->key_hash = key_test_hash;
266 c->key_hash_type = UBIFS_KEY_HASH_TEST;
267 break;
268 };
269
270 c->key_fmt = sup->key_fmt;
271
272 switch (c->key_fmt) {
273 case UBIFS_SIMPLE_KEY_FMT:
274 c->key_len = UBIFS_SK_LEN;
275 break;
276 default:
277 ubifs_err("unsupported key format");
278 err = -EINVAL;
279 goto out;
280 }
281
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);
296
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);
300
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);
307 }
308
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);
319
320 err = validate_sb(c, sup);
321out:
322 kfree(sup);
323 return err;
324}