Line data Source code
1 : // SPDX-License-Identifier: GPL-2.0-only
2 : /*
3 : * This file is part of UBIFS.
4 : *
5 : * Copyright (C) 2006-2008 Nokia Corporation.
6 : *
7 : * Authors: Artem Bityutskiy (Битюцкий Артём)
8 : * Adrian Hunter
9 : */
10 :
11 : /*
12 : * This file implements UBIFS superblock. The superblock is stored at the first
13 : * LEB of the volume and is never changed by UBIFS. Only user-space tools may
14 : * change it. The superblock node mostly contains geometry information.
15 : */
16 :
17 : #include "linux_err.h"
18 : #include "bitops.h"
19 : #include "kmem.h"
20 : #include "ubifs.h"
21 : #include "defs.h"
22 : #include "debug.h"
23 : #include "key.h"
24 : #include "misc.h"
25 :
26 : /* Default number of LEB numbers in LPT's save table */
27 : #define DEFAULT_LSAVE_CNT 256
28 :
29 : /**
30 : * validate_sb - validate superblock node.
31 : * @c: UBIFS file-system description object
32 : * @sup: superblock node
33 : *
34 : * This function validates superblock node @sup. Since most of data was read
35 : * from the superblock and stored in @c, the function validates fields in @c
36 : * instead. Returns zero in case of success and %-EINVAL in case of validation
37 : * failure.
38 : */
39 2320 : static int validate_sb(struct ubifs_info *c, struct ubifs_sb_node *sup)
40 : {
41 : long long max_bytes;
42 2320 : int err = 1, min_leb_cnt;
43 :
44 2320 : if (!c->key_hash) {
45 : err = 2;
46 : goto failed;
47 : }
48 :
49 2320 : if (sup->key_fmt != UBIFS_SIMPLE_KEY_FMT) {
50 : err = 3;
51 : goto failed;
52 : }
53 :
54 2320 : if (le32_to_cpu(sup->min_io_size) != c->min_io_size) {
55 9 : ubifs_err(c, "min. I/O unit mismatch: %d in superblock, %d real",
56 : le32_to_cpu(sup->min_io_size), c->min_io_size);
57 : goto failed;
58 : }
59 :
60 2311 : if (le32_to_cpu(sup->leb_size) != c->leb_size) {
61 9 : ubifs_err(c, "LEB size mismatch: %d in superblock, %d real",
62 : le32_to_cpu(sup->leb_size), c->leb_size);
63 : goto failed;
64 : }
65 :
66 4595 : if (c->log_lebs < UBIFS_MIN_LOG_LEBS ||
67 4586 : c->lpt_lebs < UBIFS_MIN_LPT_LEBS ||
68 4586 : c->orph_lebs < UBIFS_MIN_ORPH_LEBS ||
69 2293 : c->main_lebs < UBIFS_MIN_MAIN_LEBS) {
70 : err = 4;
71 : goto failed;
72 : }
73 :
74 : /*
75 : * Calculate minimum allowed amount of main area LEBs. This is very
76 : * similar to %UBIFS_MIN_LEB_CNT, but we take into account real what we
77 : * have just read from the superblock.
78 : */
79 2293 : min_leb_cnt = UBIFS_SB_LEBS + UBIFS_MST_LEBS + c->log_lebs;
80 2293 : min_leb_cnt += c->lpt_lebs + c->orph_lebs + c->jhead_cnt + 6;
81 :
82 2293 : if (c->leb_cnt < min_leb_cnt || c->leb_cnt > c->vi.rsvd_lebs) {
83 0 : ubifs_err(c, "bad LEB count: %d in superblock, %d on UBI volume, %d minimum required",
84 : c->leb_cnt, c->vi.rsvd_lebs, min_leb_cnt);
85 : goto failed;
86 : }
87 :
88 2293 : if (c->max_leb_cnt < c->leb_cnt) {
89 0 : ubifs_err(c, "max. LEB count %d less than LEB count %d",
90 : c->max_leb_cnt, c->leb_cnt);
91 : goto failed;
92 : }
93 :
94 : if (c->main_lebs < UBIFS_MIN_MAIN_LEBS) {
95 : ubifs_err(c, "too few main LEBs count %d, must be at least %d",
96 : c->main_lebs, UBIFS_MIN_MAIN_LEBS);
97 : goto failed;
98 : }
99 :
100 2293 : max_bytes = (long long)c->leb_size * UBIFS_MIN_BUD_LEBS;
101 2293 : if (c->max_bud_bytes < max_bytes) {
102 0 : ubifs_err(c, "too small journal (%lld bytes), must be at least %lld bytes",
103 : c->max_bud_bytes, max_bytes);
104 : goto failed;
105 : }
106 :
107 2293 : max_bytes = (long long)c->leb_size * c->main_lebs;
108 2293 : if (c->max_bud_bytes > max_bytes) {
109 0 : ubifs_err(c, "too large journal size (%lld bytes), only %lld bytes available in the main area",
110 : c->max_bud_bytes, max_bytes);
111 : goto failed;
112 : }
113 :
114 2293 : if (c->jhead_cnt < NONDATA_JHEADS_CNT + 1 ||
115 : c->jhead_cnt > NONDATA_JHEADS_CNT + UBIFS_MAX_JHEADS) {
116 : err = 9;
117 : goto failed;
118 : }
119 :
120 4586 : if (c->fanout < UBIFS_MIN_FANOUT ||
121 4586 : ubifs_idx_node_sz(c, c->fanout) > c->leb_size) {
122 : err = 10;
123 : goto failed;
124 : }
125 :
126 2284 : if (c->lsave_cnt < 0 || (c->lsave_cnt > DEFAULT_LSAVE_CNT &&
127 0 : c->lsave_cnt > c->max_leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS -
128 0 : c->log_lebs - c->lpt_lebs - c->orph_lebs)) {
129 : err = 11;
130 : goto failed;
131 : }
132 :
133 4568 : if (UBIFS_SB_LEBS + UBIFS_MST_LEBS + c->log_lebs + c->lpt_lebs +
134 2284 : c->orph_lebs + c->main_lebs != c->leb_cnt) {
135 : err = 12;
136 : goto failed;
137 : }
138 :
139 2284 : if (c->default_compr >= UBIFS_COMPR_TYPES_CNT) {
140 : err = 13;
141 : goto failed;
142 : }
143 :
144 2284 : if (c->rp_size < 0 || max_bytes < c->rp_size) {
145 : err = 14;
146 : goto failed;
147 : }
148 :
149 4568 : if (le32_to_cpu(sup->time_gran) > 1000000000 ||
150 2284 : le32_to_cpu(sup->time_gran) < 1) {
151 : err = 15;
152 : goto failed;
153 : }
154 :
155 2284 : if (!c->double_hash && c->fmt_version >= 5) {
156 : err = 16;
157 : goto failed;
158 : }
159 :
160 2284 : if (c->encrypted && c->fmt_version < 5) {
161 : err = 17;
162 : goto failed;
163 : }
164 :
165 : return 0;
166 :
167 36 : failed:
168 36 : ubifs_err(c, "bad superblock, error %d", err);
169 36 : ubifs_dump_node(c, sup, ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size));
170 36 : return -EINVAL;
171 : }
172 :
173 : /**
174 : * ubifs_read_sb_node - read superblock node.
175 : * @c: UBIFS file-system description object
176 : *
177 : * This function returns a pointer to the superblock node or a negative error
178 : * code. Note, the user of this function is responsible of kfree()'ing the
179 : * returned superblock buffer.
180 : */
181 2332 : static struct ubifs_sb_node *ubifs_read_sb_node(struct ubifs_info *c)
182 : {
183 : struct ubifs_sb_node *sup;
184 : int err;
185 :
186 2332 : sup = kmalloc(ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size), GFP_NOFS);
187 2332 : if (!sup)
188 : return ERR_PTR(-ENOMEM);
189 :
190 2332 : err = ubifs_read_node(c, sup, UBIFS_SB_NODE, UBIFS_SB_NODE_SZ,
191 : UBIFS_SB_LNUM, 0);
192 2332 : if (err) {
193 0 : kfree(sup);
194 0 : return ERR_PTR(err);
195 : }
196 :
197 : return sup;
198 : }
199 :
200 2323 : static int authenticate_sb_node(__unused struct ubifs_info *c,
201 : const struct ubifs_sb_node *sup)
202 : {
203 2323 : unsigned int sup_flags = le32_to_cpu(sup->flags);
204 2323 : int authenticated = !!(sup_flags & UBIFS_FLG_AUTHENTICATION);
205 :
206 2323 : if (authenticated) {
207 : // To be implemented
208 3 : ubifs_err(c, "not support authentication");
209 : return -EOPNOTSUPP;
210 : }
211 :
212 : return 0;
213 : }
214 :
215 : /**
216 : * ubifs_write_sb_node - write superblock node.
217 : * @c: UBIFS file-system description object
218 : * @sup: superblock node read with 'ubifs_read_sb_node()'
219 : *
220 : * This function returns %0 on success and a negative error code on failure.
221 : */
222 260 : int ubifs_write_sb_node(struct ubifs_info *c, struct ubifs_sb_node *sup)
223 : {
224 260 : int len = ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size);
225 : int err;
226 :
227 260 : err = ubifs_prepare_node_hmac(c, sup, UBIFS_SB_NODE_SZ,
228 : offsetof(struct ubifs_sb_node, hmac), 1);
229 260 : if (err)
230 : return err;
231 :
232 260 : return ubifs_leb_change(c, UBIFS_SB_LNUM, sup, len);
233 : }
234 :
235 : /**
236 : * ubifs_read_superblock - read superblock.
237 : * @c: UBIFS file-system description object
238 : *
239 : * This function finds, reads and checks the superblock. If an empty UBI volume
240 : * is being mounted, this function creates default superblock. Returns zero in
241 : * case of success, and a negative error code in case of failure.
242 : */
243 2332 : int ubifs_read_superblock(struct ubifs_info *c)
244 : {
245 : int err, sup_flags;
246 : struct ubifs_sb_node *sup;
247 :
248 2332 : sup = ubifs_read_sb_node(c);
249 2332 : if (IS_ERR(sup))
250 0 : return PTR_ERR(sup);
251 :
252 2332 : c->sup_node = sup;
253 :
254 2332 : c->fmt_version = le32_to_cpu(sup->fmt_version);
255 2332 : c->ro_compat_version = le32_to_cpu(sup->ro_compat_version);
256 :
257 : /*
258 : * The software supports all previous versions but not future versions,
259 : * due to the unavailability of time-travelling equipment.
260 : */
261 2332 : if (c->fmt_version > UBIFS_FORMAT_VERSION) {
262 0 : ubifs_assert(c, !c->ro_media || c->ro_mount);
263 0 : if (!c->ro_mount ||
264 0 : c->ro_compat_version > UBIFS_RO_COMPAT_VERSION) {
265 0 : ubifs_err(c, "on-flash format version is w%d/r%d, but software only supports up to version w%d/r%d",
266 : c->fmt_version, c->ro_compat_version,
267 : UBIFS_FORMAT_VERSION,
268 : UBIFS_RO_COMPAT_VERSION);
269 0 : if (c->ro_compat_version <= UBIFS_RO_COMPAT_VERSION) {
270 0 : ubifs_msg(c, "only R/O mounting is possible");
271 : err = -EROFS;
272 : } else
273 : err = -EINVAL;
274 : goto out;
275 : }
276 : }
277 :
278 2332 : if (c->fmt_version < 3) {
279 9 : ubifs_err(c, "on-flash format version %d is not supported",
280 : c->fmt_version);
281 : err = -EINVAL;
282 : goto out;
283 : }
284 :
285 2323 : switch (sup->key_hash) {
286 2323 : case UBIFS_KEY_HASH_R5:
287 2323 : c->key_hash = key_r5_hash;
288 2323 : c->key_hash_type = UBIFS_KEY_HASH_R5;
289 2323 : break;
290 :
291 0 : case UBIFS_KEY_HASH_TEST:
292 0 : c->key_hash = key_test_hash;
293 0 : c->key_hash_type = UBIFS_KEY_HASH_TEST;
294 0 : break;
295 : }
296 :
297 2323 : c->key_fmt = sup->key_fmt;
298 :
299 2323 : switch (c->key_fmt) {
300 2323 : case UBIFS_SIMPLE_KEY_FMT:
301 2323 : c->key_len = UBIFS_SK_LEN;
302 : break;
303 0 : default:
304 0 : ubifs_err(c, "unsupported key format");
305 : err = -EINVAL;
306 : goto out;
307 : }
308 :
309 2323 : c->leb_cnt = le32_to_cpu(sup->leb_cnt);
310 2323 : c->max_leb_cnt = le32_to_cpu(sup->max_leb_cnt);
311 2323 : c->max_bud_bytes = le64_to_cpu(sup->max_bud_bytes);
312 2323 : c->log_lebs = le32_to_cpu(sup->log_lebs);
313 2323 : c->lpt_lebs = le32_to_cpu(sup->lpt_lebs);
314 2323 : c->orph_lebs = le32_to_cpu(sup->orph_lebs);
315 2323 : c->jhead_cnt = le32_to_cpu(sup->jhead_cnt) + NONDATA_JHEADS_CNT;
316 2323 : c->fanout = le32_to_cpu(sup->fanout);
317 2323 : c->lsave_cnt = le32_to_cpu(sup->lsave_cnt);
318 2323 : c->rp_size = le64_to_cpu(sup->rp_size);
319 2323 : sup_flags = le32_to_cpu(sup->flags);
320 2323 : c->default_compr = le16_to_cpu(sup->default_compr);
321 :
322 2323 : c->big_lpt = !!(sup_flags & UBIFS_FLG_BIGLPT);
323 2323 : c->space_fixup = !!(sup_flags & UBIFS_FLG_SPACE_FIXUP);
324 2323 : c->double_hash = !!(sup_flags & UBIFS_FLG_DOUBLE_HASH);
325 2323 : c->encrypted = !!(sup_flags & UBIFS_FLG_ENCRYPTION);
326 :
327 2323 : err = authenticate_sb_node(c, sup);
328 2323 : if (err)
329 : goto out;
330 :
331 2320 : if ((sup_flags & ~UBIFS_FLG_MASK) != 0) {
332 0 : ubifs_err(c, "Unknown feature flags found: %#x",
333 : sup_flags & ~UBIFS_FLG_MASK);
334 : err = -EINVAL;
335 : goto out;
336 : }
337 :
338 : /* Automatically increase file system size to the maximum size */
339 2320 : if (c->leb_cnt < c->vi.rsvd_lebs && c->leb_cnt < c->max_leb_cnt) {
340 264 : int old_leb_cnt = c->leb_cnt;
341 :
342 264 : c->leb_cnt = min_t(int, c->max_leb_cnt, c->vi.rsvd_lebs);
343 264 : sup->leb_cnt = cpu_to_le32(c->leb_cnt);
344 :
345 264 : c->superblock_need_write = 1;
346 :
347 264 : dbg_mnt("Auto resizing from %d LEBs to %d LEBs",
348 : old_leb_cnt, c->leb_cnt);
349 : }
350 :
351 2320 : c->log_bytes = (long long)c->log_lebs * c->leb_size;
352 2320 : c->log_last = UBIFS_LOG_LNUM + c->log_lebs - 1;
353 2320 : c->lpt_first = UBIFS_LOG_LNUM + c->log_lebs;
354 2320 : c->lpt_last = c->lpt_first + c->lpt_lebs - 1;
355 2320 : c->orph_first = c->lpt_last + 1;
356 2320 : c->orph_last = c->orph_first + c->orph_lebs - 1;
357 2320 : c->main_lebs = c->leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS;
358 2320 : c->main_lebs -= c->log_lebs + c->lpt_lebs + c->orph_lebs;
359 2320 : c->main_first = c->leb_cnt - c->main_lebs;
360 :
361 2320 : err = validate_sb(c, sup);
362 2326 : out:
363 2323 : if (err)
364 : set_failure_reason_callback(c, FR_DATA_CORRUPTED);
365 : return err;
366 : }
367 :
368 : /**
369 : * fixup_leb - fixup/unmap an LEB containing free space.
370 : * @c: UBIFS file-system description object
371 : * @lnum: the LEB number to fix up
372 : * @len: number of used bytes in LEB (starting at offset 0)
373 : *
374 : * This function reads the contents of the given LEB number @lnum, then fixes
375 : * it up, so that empty min. I/O units in the end of LEB are actually erased on
376 : * flash (rather than being just all-0xff real data). If the LEB is completely
377 : * empty, it is simply unmapped.
378 : */
379 129064 : static int fixup_leb(struct ubifs_info *c, int lnum, int len)
380 : {
381 : int err;
382 :
383 129064 : ubifs_assert(c, len >= 0);
384 129064 : ubifs_assert(c, len % c->min_io_size == 0);
385 129064 : ubifs_assert(c, len < c->leb_size);
386 :
387 129064 : if (len == 0) {
388 125072 : dbg_mnt("unmap empty LEB %d", lnum);
389 125072 : return ubifs_leb_unmap(c, lnum);
390 : }
391 :
392 3992 : dbg_mnt("fixup LEB %d, data len %d", lnum, len);
393 3992 : err = ubifs_leb_read(c, lnum, c->sbuf, 0, len, 1);
394 3992 : if (err && err != -EBADMSG)
395 : return err;
396 :
397 3992 : return ubifs_leb_change(c, lnum, c->sbuf, len);
398 : }
399 :
400 : /**
401 : * fixup_free_space - find & remap all LEBs containing free space.
402 : * @c: UBIFS file-system description object
403 : *
404 : * This function walks through all LEBs in the filesystem and fiexes up those
405 : * containing free/empty space.
406 : */
407 128 : static int fixup_free_space(struct ubifs_info *c)
408 : {
409 128 : int lnum, err = 0;
410 : struct ubifs_lprops *lprops;
411 :
412 128 : ubifs_get_lprops(c);
413 :
414 : /* Fixup LEBs in the master area */
415 384 : for (lnum = UBIFS_MST_LNUM; lnum < UBIFS_LOG_LNUM; lnum++) {
416 256 : err = fixup_leb(c, lnum, c->mst_offs + c->mst_node_alsz);
417 256 : if (err)
418 : goto out;
419 : }
420 :
421 : /* Unmap unused log LEBs */
422 128 : lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
423 592 : while (lnum != c->ltail_lnum) {
424 464 : err = fixup_leb(c, lnum, 0);
425 464 : if (err)
426 : goto out;
427 : lnum = ubifs_next_log_lnum(c, lnum);
428 : }
429 :
430 : /*
431 : * Fixup the log head which contains the only a CS node at the
432 : * beginning.
433 : */
434 128 : err = fixup_leb(c, c->lhead_lnum,
435 128 : ALIGN(UBIFS_CS_NODE_SZ, c->min_io_size));
436 128 : if (err)
437 : goto out;
438 :
439 : /* Fixup LEBs in the LPT area */
440 384 : for (lnum = c->lpt_first; lnum <= c->lpt_last; lnum++) {
441 256 : int free = c->ltab[lnum - c->lpt_first].free;
442 :
443 256 : if (free > 0) {
444 256 : err = fixup_leb(c, lnum, c->leb_size - free);
445 256 : if (err)
446 : goto out;
447 : }
448 : }
449 :
450 : /* Unmap LEBs in the orphans area */
451 256 : for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
452 128 : err = fixup_leb(c, lnum, 0);
453 128 : if (err)
454 : goto out;
455 : }
456 :
457 : /* Fixup LEBs in the main area */
458 128304 : for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) {
459 128176 : lprops = ubifs_lpt_lookup(c, lnum);
460 128176 : if (IS_ERR(lprops)) {
461 0 : err = PTR_ERR(lprops);
462 0 : goto out;
463 : }
464 :
465 128176 : if (lprops->free > 0) {
466 127832 : err = fixup_leb(c, lnum, c->leb_size - lprops->free);
467 127832 : if (err)
468 : goto out;
469 : }
470 : }
471 :
472 128 : out:
473 128 : ubifs_release_lprops(c);
474 128 : return err;
475 : }
476 :
477 : /**
478 : * ubifs_fixup_free_space - find & fix all LEBs with free space.
479 : * @c: UBIFS file-system description object
480 : *
481 : * This function fixes up LEBs containing free space on first mount, if the
482 : * appropriate flag was set when the FS was created. Each LEB with one or more
483 : * empty min. I/O unit (i.e. free-space-count > 0) is re-written, to make sure
484 : * the free space is actually erased. E.g., this is necessary for some NAND
485 : * chips, since the free space may have been programmed like real "0xff" data
486 : * (generating a non-0xff ECC), causing future writes to the not-really-erased
487 : * NAND pages to behave badly. After the space is fixed up, the superblock flag
488 : * is cleared, so that this is skipped for all future mounts.
489 : */
490 128 : int ubifs_fixup_free_space(struct ubifs_info *c)
491 : {
492 : int err;
493 128 : struct ubifs_sb_node *sup = c->sup_node;
494 :
495 128 : ubifs_assert(c, c->space_fixup);
496 128 : ubifs_assert(c, !c->ro_mount);
497 :
498 128 : ubifs_msg(c, "start fixing up free space");
499 :
500 128 : err = fixup_free_space(c);
501 128 : if (err)
502 : return err;
503 :
504 : /* Free-space fixup is no longer required */
505 128 : c->space_fixup = 0;
506 128 : sup->flags &= cpu_to_le32(~UBIFS_FLG_SPACE_FIXUP);
507 :
508 128 : c->superblock_need_write = 1;
509 :
510 128 : ubifs_msg(c, "free space fixup complete");
511 : return err;
512 : }
|