UBI: remove superfluous "!!" operation
[deliverable/linux.git] / fs / ubifs / sb.c
CommitLineData
1e51764a
AB
1/*
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"
5a0e3ad6 30#include <linux/slab.h>
1e51764a 31#include <linux/random.h>
4d61db4f 32#include <linux/math64.h>
1e51764a
AB
33
34/*
35 * Default journal size in logical eraseblocks as a percent of total
36 * flash size.
37 */
38#define DEFAULT_JNL_PERCENT 5
39
40/* Default maximum journal size in bytes */
41#define DEFAULT_MAX_JNL (32*1024*1024)
42
43/* Default indexing tree fanout */
44#define DEFAULT_FANOUT 8
45
46/* Default number of data journal heads */
47#define DEFAULT_JHEADS_CNT 1
48
49/* Default positions of different LEBs in the main area */
50#define DEFAULT_IDX_LEB 0
51#define DEFAULT_DATA_LEB 1
52#define DEFAULT_GC_LEB 2
53
54/* Default number of LEB numbers in LPT's save table */
55#define DEFAULT_LSAVE_CNT 256
56
57/* Default reserved pool size as a percent of maximum free space */
58#define DEFAULT_RP_PERCENT 5
59
60/* The default maximum size of reserved pool in bytes */
61#define DEFAULT_MAX_RP_SIZE (5*1024*1024)
62
63/* Default time granularity in nanoseconds */
64#define DEFAULT_TIME_GRAN 1000000000
65
66/**
67 * create_default_filesystem - format empty UBI volume.
68 * @c: UBIFS file-system description object
69 *
70 * This function creates default empty file-system. Returns zero in case of
71 * success and a negative error code in case of failure.
72 */
73static int create_default_filesystem(struct ubifs_info *c)
74{
75 struct ubifs_sb_node *sup;
76 struct ubifs_mst_node *mst;
77 struct ubifs_idx_node *idx;
78 struct ubifs_branch *br;
79 struct ubifs_ino_node *ino;
80 struct ubifs_cs_node *cs;
81 union ubifs_key key;
82 int err, tmp, jnl_lebs, log_lebs, max_buds, main_lebs, main_first;
83 int lpt_lebs, lpt_first, orph_lebs, big_lpt, ino_waste, sup_flags = 0;
84 int min_leb_cnt = UBIFS_MIN_LEB_CNT;
4d61db4f 85 long long tmp64, main_bytes;
0ecb9529 86 __le64 tmp_le64;
1e51764a
AB
87
88 /* Some functions called from here depend on the @c->key_len filed */
89 c->key_len = UBIFS_SK_LEN;
90
91 /*
92 * First of all, we have to calculate default file-system geometry -
93 * log size, journal size, etc.
94 */
95 if (c->leb_cnt < 0x7FFFFFFF / DEFAULT_JNL_PERCENT)
96 /* We can first multiply then divide and have no overflow */
97 jnl_lebs = c->leb_cnt * DEFAULT_JNL_PERCENT / 100;
98 else
99 jnl_lebs = (c->leb_cnt / 100) * DEFAULT_JNL_PERCENT;
100
101 if (jnl_lebs < UBIFS_MIN_JNL_LEBS)
102 jnl_lebs = UBIFS_MIN_JNL_LEBS;
103 if (jnl_lebs * c->leb_size > DEFAULT_MAX_JNL)
104 jnl_lebs = DEFAULT_MAX_JNL / c->leb_size;
105
106 /*
107 * The log should be large enough to fit reference nodes for all bud
108 * LEBs. Because buds do not have to start from the beginning of LEBs
109 * (half of the LEB may contain committed data), the log should
110 * generally be larger, make it twice as large.
111 */
112 tmp = 2 * (c->ref_node_alsz * jnl_lebs) + c->leb_size - 1;
113 log_lebs = tmp / c->leb_size;
114 /* Plus one LEB reserved for commit */
115 log_lebs += 1;
116 if (c->leb_cnt - min_leb_cnt > 8) {
117 /* And some extra space to allow writes while committing */
118 log_lebs += 1;
119 min_leb_cnt += 1;
120 }
121
122 max_buds = jnl_lebs - log_lebs;
123 if (max_buds < UBIFS_MIN_BUD_LEBS)
124 max_buds = UBIFS_MIN_BUD_LEBS;
125
126 /*
127 * Orphan nodes are stored in a separate area. One node can store a lot
128 * of orphan inode numbers, but when new orphan comes we just add a new
129 * orphan node. At some point the nodes are consolidated into one
130 * orphan node.
131 */
132 orph_lebs = UBIFS_MIN_ORPH_LEBS;
1e51764a
AB
133 if (c->leb_cnt - min_leb_cnt > 1)
134 /*
135 * For debugging purposes it is better to have at least 2
136 * orphan LEBs, because the orphan subsystem would need to do
137 * consolidations and would be stressed more.
138 */
139 orph_lebs += 1;
1e51764a
AB
140
141 main_lebs = c->leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS - log_lebs;
142 main_lebs -= orph_lebs;
143
144 lpt_first = UBIFS_LOG_LNUM + log_lebs;
145 c->lsave_cnt = DEFAULT_LSAVE_CNT;
146 c->max_leb_cnt = c->leb_cnt;
147 err = ubifs_create_dflt_lpt(c, &main_lebs, lpt_first, &lpt_lebs,
148 &big_lpt);
149 if (err)
150 return err;
151
152 dbg_gen("LEB Properties Tree created (LEBs %d-%d)", lpt_first,
153 lpt_first + lpt_lebs - 1);
154
155 main_first = c->leb_cnt - main_lebs;
156
157 /* Create default superblock */
158 tmp = ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size);
159 sup = kzalloc(tmp, GFP_KERNEL);
160 if (!sup)
161 return -ENOMEM;
162
4d61db4f 163 tmp64 = (long long)max_buds * c->leb_size;
1e51764a
AB
164 if (big_lpt)
165 sup_flags |= UBIFS_FLG_BIGLPT;
166
167 sup->ch.node_type = UBIFS_SB_NODE;
168 sup->key_hash = UBIFS_KEY_HASH_R5;
169 sup->flags = cpu_to_le32(sup_flags);
170 sup->min_io_size = cpu_to_le32(c->min_io_size);
171 sup->leb_size = cpu_to_le32(c->leb_size);
172 sup->leb_cnt = cpu_to_le32(c->leb_cnt);
173 sup->max_leb_cnt = cpu_to_le32(c->max_leb_cnt);
174 sup->max_bud_bytes = cpu_to_le64(tmp64);
175 sup->log_lebs = cpu_to_le32(log_lebs);
176 sup->lpt_lebs = cpu_to_le32(lpt_lebs);
177 sup->orph_lebs = cpu_to_le32(orph_lebs);
178 sup->jhead_cnt = cpu_to_le32(DEFAULT_JHEADS_CNT);
179 sup->fanout = cpu_to_le32(DEFAULT_FANOUT);
180 sup->lsave_cnt = cpu_to_le32(c->lsave_cnt);
181 sup->fmt_version = cpu_to_le32(UBIFS_FORMAT_VERSION);
1e51764a 182 sup->time_gran = cpu_to_le32(DEFAULT_TIME_GRAN);
553dea4d
AB
183 if (c->mount_opts.override_compr)
184 sup->default_compr = cpu_to_le16(c->mount_opts.compr_type);
185 else
186 sup->default_compr = cpu_to_le16(UBIFS_COMPR_LZO);
1e51764a
AB
187
188 generate_random_uuid(sup->uuid);
189
4d61db4f
AB
190 main_bytes = (long long)main_lebs * c->leb_size;
191 tmp64 = div_u64(main_bytes * DEFAULT_RP_PERCENT, 100);
1e51764a
AB
192 if (tmp64 > DEFAULT_MAX_RP_SIZE)
193 tmp64 = DEFAULT_MAX_RP_SIZE;
194 sup->rp_size = cpu_to_le64(tmp64);
963f0cf6 195 sup->ro_compat_version = cpu_to_le32(UBIFS_RO_COMPAT_VERSION);
1e51764a
AB
196
197 err = ubifs_write_node(c, sup, UBIFS_SB_NODE_SZ, 0, 0, UBI_LONGTERM);
198 kfree(sup);
199 if (err)
200 return err;
201
202 dbg_gen("default superblock created at LEB 0:0");
203
204 /* Create default master node */
205 mst = kzalloc(c->mst_node_alsz, GFP_KERNEL);
206 if (!mst)
207 return -ENOMEM;
208
209 mst->ch.node_type = UBIFS_MST_NODE;
210 mst->log_lnum = cpu_to_le32(UBIFS_LOG_LNUM);
211 mst->highest_inum = cpu_to_le64(UBIFS_FIRST_INO);
212 mst->cmt_no = 0;
213 mst->root_lnum = cpu_to_le32(main_first + DEFAULT_IDX_LEB);
214 mst->root_offs = 0;
215 tmp = ubifs_idx_node_sz(c, 1);
216 mst->root_len = cpu_to_le32(tmp);
217 mst->gc_lnum = cpu_to_le32(main_first + DEFAULT_GC_LEB);
218 mst->ihead_lnum = cpu_to_le32(main_first + DEFAULT_IDX_LEB);
219 mst->ihead_offs = cpu_to_le32(ALIGN(tmp, c->min_io_size));
220 mst->index_size = cpu_to_le64(ALIGN(tmp, 8));
221 mst->lpt_lnum = cpu_to_le32(c->lpt_lnum);
222 mst->lpt_offs = cpu_to_le32(c->lpt_offs);
223 mst->nhead_lnum = cpu_to_le32(c->nhead_lnum);
224 mst->nhead_offs = cpu_to_le32(c->nhead_offs);
225 mst->ltab_lnum = cpu_to_le32(c->ltab_lnum);
226 mst->ltab_offs = cpu_to_le32(c->ltab_offs);
227 mst->lsave_lnum = cpu_to_le32(c->lsave_lnum);
228 mst->lsave_offs = cpu_to_le32(c->lsave_offs);
229 mst->lscan_lnum = cpu_to_le32(main_first);
230 mst->empty_lebs = cpu_to_le32(main_lebs - 2);
231 mst->idx_lebs = cpu_to_le32(1);
232 mst->leb_cnt = cpu_to_le32(c->leb_cnt);
233
234 /* Calculate lprops statistics */
235 tmp64 = main_bytes;
236 tmp64 -= ALIGN(ubifs_idx_node_sz(c, 1), c->min_io_size);
237 tmp64 -= ALIGN(UBIFS_INO_NODE_SZ, c->min_io_size);
238 mst->total_free = cpu_to_le64(tmp64);
239
240 tmp64 = ALIGN(ubifs_idx_node_sz(c, 1), c->min_io_size);
241 ino_waste = ALIGN(UBIFS_INO_NODE_SZ, c->min_io_size) -
242 UBIFS_INO_NODE_SZ;
243 tmp64 += ino_waste;
244 tmp64 -= ALIGN(ubifs_idx_node_sz(c, 1), 8);
245 mst->total_dirty = cpu_to_le64(tmp64);
246
247 /* The indexing LEB does not contribute to dark space */
7606f85a 248 tmp64 = ((long long)(c->main_lebs - 1) * c->dark_wm);
1e51764a
AB
249 mst->total_dark = cpu_to_le64(tmp64);
250
251 mst->total_used = cpu_to_le64(UBIFS_INO_NODE_SZ);
252
253 err = ubifs_write_node(c, mst, UBIFS_MST_NODE_SZ, UBIFS_MST_LNUM, 0,
254 UBI_UNKNOWN);
255 if (err) {
256 kfree(mst);
257 return err;
258 }
259 err = ubifs_write_node(c, mst, UBIFS_MST_NODE_SZ, UBIFS_MST_LNUM + 1, 0,
260 UBI_UNKNOWN);
261 kfree(mst);
262 if (err)
263 return err;
264
265 dbg_gen("default master node created at LEB %d:0", UBIFS_MST_LNUM);
266
267 /* Create the root indexing node */
268 tmp = ubifs_idx_node_sz(c, 1);
269 idx = kzalloc(ALIGN(tmp, c->min_io_size), GFP_KERNEL);
270 if (!idx)
271 return -ENOMEM;
272
273 c->key_fmt = UBIFS_SIMPLE_KEY_FMT;
274 c->key_hash = key_r5_hash;
275
276 idx->ch.node_type = UBIFS_IDX_NODE;
277 idx->child_cnt = cpu_to_le16(1);
278 ino_key_init(c, &key, UBIFS_ROOT_INO);
279 br = ubifs_idx_branch(c, idx, 0);
280 key_write_idx(c, &key, &br->key);
281 br->lnum = cpu_to_le32(main_first + DEFAULT_DATA_LEB);
282 br->len = cpu_to_le32(UBIFS_INO_NODE_SZ);
283 err = ubifs_write_node(c, idx, tmp, main_first + DEFAULT_IDX_LEB, 0,
284 UBI_UNKNOWN);
285 kfree(idx);
286 if (err)
287 return err;
288
289 dbg_gen("default root indexing node created LEB %d:0",
290 main_first + DEFAULT_IDX_LEB);
291
292 /* Create default root inode */
293 tmp = ALIGN(UBIFS_INO_NODE_SZ, c->min_io_size);
294 ino = kzalloc(tmp, GFP_KERNEL);
295 if (!ino)
296 return -ENOMEM;
297
298 ino_key_init_flash(c, &ino->key, UBIFS_ROOT_INO);
299 ino->ch.node_type = UBIFS_INO_NODE;
300 ino->creat_sqnum = cpu_to_le64(++c->max_sqnum);
301 ino->nlink = cpu_to_le32(2);
0ecb9529
HH
302 tmp_le64 = cpu_to_le64(CURRENT_TIME_SEC.tv_sec);
303 ino->atime_sec = tmp_le64;
304 ino->ctime_sec = tmp_le64;
305 ino->mtime_sec = tmp_le64;
1e51764a
AB
306 ino->atime_nsec = 0;
307 ino->ctime_nsec = 0;
308 ino->mtime_nsec = 0;
309 ino->mode = cpu_to_le32(S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO);
310 ino->size = cpu_to_le64(UBIFS_INO_NODE_SZ);
311
312 /* Set compression enabled by default */
313 ino->flags = cpu_to_le32(UBIFS_COMPR_FL);
314
315 err = ubifs_write_node(c, ino, UBIFS_INO_NODE_SZ,
316 main_first + DEFAULT_DATA_LEB, 0,
317 UBI_UNKNOWN);
318 kfree(ino);
319 if (err)
320 return err;
321
322 dbg_gen("root inode created at LEB %d:0",
323 main_first + DEFAULT_DATA_LEB);
324
325 /*
326 * The first node in the log has to be the commit start node. This is
327 * always the case during normal file-system operation. Write a fake
328 * commit start node to the log.
329 */
330 tmp = ALIGN(UBIFS_CS_NODE_SZ, c->min_io_size);
331 cs = kzalloc(tmp, GFP_KERNEL);
332 if (!cs)
333 return -ENOMEM;
334
335 cs->ch.node_type = UBIFS_CS_NODE;
336 err = ubifs_write_node(c, cs, UBIFS_CS_NODE_SZ, UBIFS_LOG_LNUM,
337 0, UBI_UNKNOWN);
338 kfree(cs);
339
340 ubifs_msg("default file-system created");
341 return 0;
342}
343
344/**
345 * validate_sb - validate superblock node.
346 * @c: UBIFS file-system description object
347 * @sup: superblock node
348 *
349 * This function validates superblock node @sup. Since most of data was read
350 * from the superblock and stored in @c, the function validates fields in @c
351 * instead. Returns zero in case of success and %-EINVAL in case of validation
352 * failure.
353 */
354static int validate_sb(struct ubifs_info *c, struct ubifs_sb_node *sup)
355{
356 long long max_bytes;
357 int err = 1, min_leb_cnt;
358
359 if (!c->key_hash) {
360 err = 2;
361 goto failed;
362 }
363
364 if (sup->key_fmt != UBIFS_SIMPLE_KEY_FMT) {
365 err = 3;
366 goto failed;
367 }
368
369 if (le32_to_cpu(sup->min_io_size) != c->min_io_size) {
370 ubifs_err("min. I/O unit mismatch: %d in superblock, %d real",
371 le32_to_cpu(sup->min_io_size), c->min_io_size);
372 goto failed;
373 }
374
375 if (le32_to_cpu(sup->leb_size) != c->leb_size) {
376 ubifs_err("LEB size mismatch: %d in superblock, %d real",
377 le32_to_cpu(sup->leb_size), c->leb_size);
378 goto failed;
379 }
380
381 if (c->log_lebs < UBIFS_MIN_LOG_LEBS ||
382 c->lpt_lebs < UBIFS_MIN_LPT_LEBS ||
383 c->orph_lebs < UBIFS_MIN_ORPH_LEBS ||
384 c->main_lebs < UBIFS_MIN_MAIN_LEBS) {
385 err = 4;
386 goto failed;
387 }
388
389 /*
390 * Calculate minimum allowed amount of main area LEBs. This is very
391 * similar to %UBIFS_MIN_LEB_CNT, but we take into account real what we
392 * have just read from the superblock.
393 */
394 min_leb_cnt = UBIFS_SB_LEBS + UBIFS_MST_LEBS + c->log_lebs;
395 min_leb_cnt += c->lpt_lebs + c->orph_lebs + c->jhead_cnt + 6;
396
397 if (c->leb_cnt < min_leb_cnt || c->leb_cnt > c->vi.size) {
398 ubifs_err("bad LEB count: %d in superblock, %d on UBI volume, "
399 "%d minimum required", c->leb_cnt, c->vi.size,
400 min_leb_cnt);
401 goto failed;
402 }
403
404 if (c->max_leb_cnt < c->leb_cnt) {
405 ubifs_err("max. LEB count %d less than LEB count %d",
406 c->max_leb_cnt, c->leb_cnt);
407 goto failed;
408 }
409
410 if (c->main_lebs < UBIFS_MIN_MAIN_LEBS) {
5a1f36c9
AB
411 ubifs_err("too few main LEBs count %d, must be at least %d",
412 c->main_lebs, UBIFS_MIN_MAIN_LEBS);
1e51764a
AB
413 goto failed;
414 }
415
5a1f36c9
AB
416 max_bytes = (long long)c->leb_size * UBIFS_MIN_BUD_LEBS;
417 if (c->max_bud_bytes < max_bytes) {
418 ubifs_err("too small journal (%lld bytes), must be at least "
419 "%lld bytes", c->max_bud_bytes, max_bytes);
420 goto failed;
421 }
422
423 max_bytes = (long long)c->leb_size * c->main_lebs;
424 if (c->max_bud_bytes > max_bytes) {
425 ubifs_err("too large journal size (%lld bytes), only %lld bytes"
426 "available in the main area",
427 c->max_bud_bytes, max_bytes);
1e51764a
AB
428 goto failed;
429 }
430
431 if (c->jhead_cnt < NONDATA_JHEADS_CNT + 1 ||
432 c->jhead_cnt > NONDATA_JHEADS_CNT + UBIFS_MAX_JHEADS) {
433 err = 9;
434 goto failed;
435 }
436
437 if (c->fanout < UBIFS_MIN_FANOUT ||
438 ubifs_idx_node_sz(c, c->fanout) > c->leb_size) {
439 err = 10;
440 goto failed;
441 }
442
443 if (c->lsave_cnt < 0 || (c->lsave_cnt > DEFAULT_LSAVE_CNT &&
444 c->lsave_cnt > c->max_leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS -
445 c->log_lebs - c->lpt_lebs - c->orph_lebs)) {
446 err = 11;
447 goto failed;
448 }
449
450 if (UBIFS_SB_LEBS + UBIFS_MST_LEBS + c->log_lebs + c->lpt_lebs +
451 c->orph_lebs + c->main_lebs != c->leb_cnt) {
452 err = 12;
453 goto failed;
454 }
455
456 if (c->default_compr < 0 || c->default_compr >= UBIFS_COMPR_TYPES_CNT) {
457 err = 13;
458 goto failed;
459 }
460
1e51764a
AB
461 if (c->rp_size < 0 || max_bytes < c->rp_size) {
462 err = 14;
463 goto failed;
464 }
465
466 if (le32_to_cpu(sup->time_gran) > 1000000000 ||
467 le32_to_cpu(sup->time_gran) < 1) {
468 err = 15;
469 goto failed;
470 }
471
472 return 0;
473
474failed:
475 ubifs_err("bad superblock, error %d", err);
edf6be24 476 ubifs_dump_node(c, sup);
1e51764a
AB
477 return -EINVAL;
478}
479
480/**
481 * ubifs_read_sb_node - read superblock node.
482 * @c: UBIFS file-system description object
483 *
484 * This function returns a pointer to the superblock node or a negative error
eaeee242
AB
485 * code. Note, the user of this function is responsible of kfree()'ing the
486 * returned superblock buffer.
1e51764a
AB
487 */
488struct ubifs_sb_node *ubifs_read_sb_node(struct ubifs_info *c)
489{
490 struct ubifs_sb_node *sup;
491 int err;
492
493 sup = kmalloc(ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size), GFP_NOFS);
494 if (!sup)
495 return ERR_PTR(-ENOMEM);
496
497 err = ubifs_read_node(c, sup, UBIFS_SB_NODE, UBIFS_SB_NODE_SZ,
498 UBIFS_SB_LNUM, 0);
499 if (err) {
500 kfree(sup);
501 return ERR_PTR(err);
502 }
503
504 return sup;
505}
506
507/**
508 * ubifs_write_sb_node - write superblock node.
509 * @c: UBIFS file-system description object
510 * @sup: superblock node read with 'ubifs_read_sb_node()'
511 *
512 * This function returns %0 on success and a negative error code on failure.
513 */
514int ubifs_write_sb_node(struct ubifs_info *c, struct ubifs_sb_node *sup)
515{
516 int len = ALIGN(UBIFS_SB_NODE_SZ, c->min_io_size);
517
518 ubifs_prepare_node(c, sup, UBIFS_SB_NODE_SZ, 1);
519 return ubifs_leb_change(c, UBIFS_SB_LNUM, sup, len, UBI_LONGTERM);
520}
521
522/**
523 * ubifs_read_superblock - read superblock.
524 * @c: UBIFS file-system description object
525 *
526 * This function finds, reads and checks the superblock. If an empty UBI volume
527 * is being mounted, this function creates default superblock. Returns zero in
528 * case of success, and a negative error code in case of failure.
529 */
530int ubifs_read_superblock(struct ubifs_info *c)
531{
532 int err, sup_flags;
533 struct ubifs_sb_node *sup;
534
535 if (c->empty) {
536 err = create_default_filesystem(c);
537 if (err)
538 return err;
539 }
540
541 sup = ubifs_read_sb_node(c);
542 if (IS_ERR(sup))
543 return PTR_ERR(sup);
544
963f0cf6
AB
545 c->fmt_version = le32_to_cpu(sup->fmt_version);
546 c->ro_compat_version = le32_to_cpu(sup->ro_compat_version);
547
1e51764a
AB
548 /*
549 * The software supports all previous versions but not future versions,
550 * due to the unavailability of time-travelling equipment.
551 */
1e51764a 552 if (c->fmt_version > UBIFS_FORMAT_VERSION) {
2ef13294
AB
553 ubifs_assert(!c->ro_media || c->ro_mount);
554 if (!c->ro_mount ||
963f0cf6
AB
555 c->ro_compat_version > UBIFS_RO_COMPAT_VERSION) {
556 ubifs_err("on-flash format version is w%d/r%d, but "
557 "software only supports up to version "
558 "w%d/r%d", c->fmt_version,
559 c->ro_compat_version, UBIFS_FORMAT_VERSION,
560 UBIFS_RO_COMPAT_VERSION);
561 if (c->ro_compat_version <= UBIFS_RO_COMPAT_VERSION) {
562 ubifs_msg("only R/O mounting is possible");
563 err = -EROFS;
564 } else
565 err = -EINVAL;
566 goto out;
567 }
568
569 /*
570 * The FS is mounted R/O, and the media format is
571 * R/O-compatible with the UBIFS implementation, so we can
572 * mount.
573 */
574 c->rw_incompat = 1;
1e51764a
AB
575 }
576
577 if (c->fmt_version < 3) {
578 ubifs_err("on-flash format version %d is not supported",
579 c->fmt_version);
580 err = -EINVAL;
581 goto out;
582 }
583
584 switch (sup->key_hash) {
585 case UBIFS_KEY_HASH_R5:
586 c->key_hash = key_r5_hash;
587 c->key_hash_type = UBIFS_KEY_HASH_R5;
588 break;
589
590 case UBIFS_KEY_HASH_TEST:
591 c->key_hash = key_test_hash;
592 c->key_hash_type = UBIFS_KEY_HASH_TEST;
593 break;
594 };
595
596 c->key_fmt = sup->key_fmt;
597
598 switch (c->key_fmt) {
599 case UBIFS_SIMPLE_KEY_FMT:
600 c->key_len = UBIFS_SK_LEN;
601 break;
602 default:
603 ubifs_err("unsupported key format");
604 err = -EINVAL;
605 goto out;
606 }
607
608 c->leb_cnt = le32_to_cpu(sup->leb_cnt);
609 c->max_leb_cnt = le32_to_cpu(sup->max_leb_cnt);
610 c->max_bud_bytes = le64_to_cpu(sup->max_bud_bytes);
611 c->log_lebs = le32_to_cpu(sup->log_lebs);
612 c->lpt_lebs = le32_to_cpu(sup->lpt_lebs);
613 c->orph_lebs = le32_to_cpu(sup->orph_lebs);
614 c->jhead_cnt = le32_to_cpu(sup->jhead_cnt) + NONDATA_JHEADS_CNT;
615 c->fanout = le32_to_cpu(sup->fanout);
616 c->lsave_cnt = le32_to_cpu(sup->lsave_cnt);
1e51764a
AB
617 c->rp_size = le64_to_cpu(sup->rp_size);
618 c->rp_uid = le32_to_cpu(sup->rp_uid);
619 c->rp_gid = le32_to_cpu(sup->rp_gid);
620 sup_flags = le32_to_cpu(sup->flags);
553dea4d
AB
621 if (!c->mount_opts.override_compr)
622 c->default_compr = le16_to_cpu(sup->default_compr);
1e51764a
AB
623
624 c->vfs_sb->s_time_gran = le32_to_cpu(sup->time_gran);
1e51764a 625 memcpy(&c->uuid, &sup->uuid, 16);
1e51764a 626 c->big_lpt = !!(sup_flags & UBIFS_FLG_BIGLPT);
9f58d350 627 c->space_fixup = !!(sup_flags & UBIFS_FLG_SPACE_FIXUP);
1e51764a
AB
628
629 /* Automatically increase file system size to the maximum size */
630 c->old_leb_cnt = c->leb_cnt;
631 if (c->leb_cnt < c->vi.size && c->leb_cnt < c->max_leb_cnt) {
632 c->leb_cnt = min_t(int, c->max_leb_cnt, c->vi.size);
2ef13294 633 if (c->ro_mount)
1e51764a
AB
634 dbg_mnt("Auto resizing (ro) from %d LEBs to %d LEBs",
635 c->old_leb_cnt, c->leb_cnt);
636 else {
637 dbg_mnt("Auto resizing (sb) from %d LEBs to %d LEBs",
638 c->old_leb_cnt, c->leb_cnt);
639 sup->leb_cnt = cpu_to_le32(c->leb_cnt);
640 err = ubifs_write_sb_node(c, sup);
641 if (err)
642 goto out;
643 c->old_leb_cnt = c->leb_cnt;
644 }
645 }
646
647 c->log_bytes = (long long)c->log_lebs * c->leb_size;
648 c->log_last = UBIFS_LOG_LNUM + c->log_lebs - 1;
649 c->lpt_first = UBIFS_LOG_LNUM + c->log_lebs;
650 c->lpt_last = c->lpt_first + c->lpt_lebs - 1;
651 c->orph_first = c->lpt_last + 1;
652 c->orph_last = c->orph_first + c->orph_lebs - 1;
653 c->main_lebs = c->leb_cnt - UBIFS_SB_LEBS - UBIFS_MST_LEBS;
654 c->main_lebs -= c->log_lebs + c->lpt_lebs + c->orph_lebs;
655 c->main_first = c->leb_cnt - c->main_lebs;
1e51764a
AB
656
657 err = validate_sb(c, sup);
658out:
659 kfree(sup);
660 return err;
661}
6554a657
MC
662
663/**
664 * fixup_leb - fixup/unmap an LEB containing free space.
665 * @c: UBIFS file-system description object
666 * @lnum: the LEB number to fix up
667 * @len: number of used bytes in LEB (starting at offset 0)
668 *
669 * This function reads the contents of the given LEB number @lnum, then fixes
670 * it up, so that empty min. I/O units in the end of LEB are actually erased on
671 * flash (rather than being just all-0xff real data). If the LEB is completely
672 * empty, it is simply unmapped.
673 */
674static int fixup_leb(struct ubifs_info *c, int lnum, int len)
675{
676 int err;
677
678 ubifs_assert(len >= 0);
679 ubifs_assert(len % c->min_io_size == 0);
680 ubifs_assert(len < c->leb_size);
681
682 if (len == 0) {
683 dbg_mnt("unmap empty LEB %d", lnum);
d3b2578f 684 return ubifs_leb_unmap(c, lnum);
6554a657
MC
685 }
686
687 dbg_mnt("fixup LEB %d, data len %d", lnum, len);
d304820a 688 err = ubifs_leb_read(c, lnum, c->sbuf, 0, len, 1);
6554a657
MC
689 if (err)
690 return err;
691
d3b2578f 692 return ubifs_leb_change(c, lnum, c->sbuf, len, UBI_UNKNOWN);
6554a657
MC
693}
694
695/**
696 * fixup_free_space - find & remap all LEBs containing free space.
697 * @c: UBIFS file-system description object
698 *
699 * This function walks through all LEBs in the filesystem and fiexes up those
700 * containing free/empty space.
701 */
702static int fixup_free_space(struct ubifs_info *c)
703{
704 int lnum, err = 0;
705 struct ubifs_lprops *lprops;
706
707 ubifs_get_lprops(c);
708
709 /* Fixup LEBs in the master area */
710 for (lnum = UBIFS_MST_LNUM; lnum < UBIFS_LOG_LNUM; lnum++) {
711 err = fixup_leb(c, lnum, c->mst_offs + c->mst_node_alsz);
712 if (err)
713 goto out;
714 }
715
716 /* Unmap unused log LEBs */
717 lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
718 while (lnum != c->ltail_lnum) {
719 err = fixup_leb(c, lnum, 0);
720 if (err)
721 goto out;
722 lnum = ubifs_next_log_lnum(c, lnum);
723 }
724
725 /* Fixup the current log head */
726 err = fixup_leb(c, c->lhead_lnum, c->lhead_offs);
727 if (err)
728 goto out;
729
730 /* Fixup LEBs in the LPT area */
731 for (lnum = c->lpt_first; lnum <= c->lpt_last; lnum++) {
732 int free = c->ltab[lnum - c->lpt_first].free;
733
734 if (free > 0) {
735 err = fixup_leb(c, lnum, c->leb_size - free);
736 if (err)
737 goto out;
738 }
739 }
740
741 /* Unmap LEBs in the orphans area */
742 for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
743 err = fixup_leb(c, lnum, 0);
744 if (err)
745 goto out;
746 }
747
748 /* Fixup LEBs in the main area */
749 for (lnum = c->main_first; lnum < c->leb_cnt; lnum++) {
750 lprops = ubifs_lpt_lookup(c, lnum);
751 if (IS_ERR(lprops)) {
752 err = PTR_ERR(lprops);
753 goto out;
754 }
755
756 if (lprops->free > 0) {
757 err = fixup_leb(c, lnum, c->leb_size - lprops->free);
758 if (err)
759 goto out;
760 }
761 }
762
763out:
764 ubifs_release_lprops(c);
765 return err;
766}
767
768/**
769 * ubifs_fixup_free_space - find & fix all LEBs with free space.
770 * @c: UBIFS file-system description object
771 *
772 * This function fixes up LEBs containing free space on first mount, if the
773 * appropriate flag was set when the FS was created. Each LEB with one or more
774 * empty min. I/O unit (i.e. free-space-count > 0) is re-written, to make sure
775 * the free space is actually erased. E.g., this is necessary for some NAND
776 * chips, since the free space may have been programmed like real "0xff" data
777 * (generating a non-0xff ECC), causing future writes to the not-really-erased
778 * NAND pages to behave badly. After the space is fixed up, the superblock flag
779 * is cleared, so that this is skipped for all future mounts.
780 */
781int ubifs_fixup_free_space(struct ubifs_info *c)
782{
783 int err;
784 struct ubifs_sb_node *sup;
785
786 ubifs_assert(c->space_fixup);
787 ubifs_assert(!c->ro_mount);
788
789 ubifs_msg("start fixing up free space");
790
791 err = fixup_free_space(c);
792 if (err)
793 return err;
794
795 sup = ubifs_read_sb_node(c);
796 if (IS_ERR(sup))
797 return PTR_ERR(sup);
798
799 /* Free-space fixup is no longer required */
800 c->space_fixup = 0;
801 sup->flags &= cpu_to_le32(~UBIFS_FLG_SPACE_FIXUP);
802
803 err = ubifs_write_sb_node(c, sup);
804 kfree(sup);
805 if (err)
806 return err;
807
808 ubifs_msg("free space fixup complete");
809 return err;
810}
This page took 0.311079 seconds and 5 git commands to generate.