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 (Битюцкий Артём)
23 /* This file implements reading and writing the master node */
28 * ubifs_compare_master_node - compare two UBIFS master nodes
29 * @c: UBIFS file-system description object
31 * @m2: the second node
33 * This function compares two UBIFS master nodes. Returns 0 if they are equal
36 int ubifs_compare_master_node(struct ubifs_info
*c
, void *m1
, void *m2
)
40 int hmac_offs
= offsetof(struct ubifs_mst_node
, hmac
);
43 * Do not compare the common node header since the sequence number and
44 * hence the CRC are different.
46 ret
= memcmp(m1
+ UBIFS_CH_SZ
, m2
+ UBIFS_CH_SZ
,
47 hmac_offs
- UBIFS_CH_SZ
);
52 * Do not compare the embedded HMAC aswell which also must be different
53 * due to the different common node header.
55 behind
= hmac_offs
+ UBIFS_MAX_HMAC_LEN
;
57 if (UBIFS_MST_NODE_SZ
> behind
)
58 return memcmp(m1
+ behind
, m2
+ behind
, UBIFS_MST_NODE_SZ
- behind
);
64 * scan_for_master - search the valid master node.
65 * @c: UBIFS file-system description object
67 * This function scans the master node LEBs and search for the latest master
68 * node. Returns zero in case of success, %-EUCLEAN if there master area is
69 * corrupted and requires recovery, and a negative error code in case of
72 static int scan_for_master(struct ubifs_info
*c
)
74 struct ubifs_scan_leb
*sleb
;
75 struct ubifs_scan_node
*snod
;
76 int lnum
, offs
= 0, nodes_cnt
, err
;
78 lnum
= UBIFS_MST_LNUM
;
80 sleb
= ubifs_scan(c
, lnum
, 0, c
->sbuf
, 1);
83 nodes_cnt
= sleb
->nodes_cnt
;
85 snod
= list_entry(sleb
->nodes
.prev
, struct ubifs_scan_node
,
87 if (snod
->type
!= UBIFS_MST_NODE
)
89 memcpy(c
->mst_node
, snod
->node
, snod
->len
);
92 ubifs_scan_destroy(sleb
);
96 sleb
= ubifs_scan(c
, lnum
, 0, c
->sbuf
, 1);
99 if (sleb
->nodes_cnt
!= nodes_cnt
)
101 if (!sleb
->nodes_cnt
)
103 snod
= list_entry(sleb
->nodes
.prev
, struct ubifs_scan_node
, list
);
104 if (snod
->type
!= UBIFS_MST_NODE
)
106 if (snod
->offs
!= offs
)
108 if (ubifs_compare_master_node(c
, c
->mst_node
, snod
->node
))
112 ubifs_scan_destroy(sleb
);
114 if (!ubifs_authenticated(c
))
117 err
= ubifs_node_verify_hmac(c
, c
->mst_node
,
118 sizeof(struct ubifs_mst_node
),
119 offsetof(struct ubifs_mst_node
, hmac
));
121 ubifs_err(c
, "Failed to verify master node HMAC");
128 ubifs_scan_destroy(sleb
);
132 ubifs_err(c
, "unexpected node type %d master LEB %d:%d",
133 snod
->type
, lnum
, snod
->offs
);
134 ubifs_scan_destroy(sleb
);
139 * validate_master - validate master node.
140 * @c: UBIFS file-system description object
142 * This function validates data which was read from master node. Returns zero
143 * if the data is all right and %-EINVAL if not.
145 static int validate_master(const struct ubifs_info
*c
)
150 if (c
->max_sqnum
>= SQNUM_WATERMARK
) {
155 if (c
->cmt_no
>= c
->max_sqnum
) {
160 if (c
->highest_inum
>= INUM_WATERMARK
) {
165 if (c
->lhead_lnum
< UBIFS_LOG_LNUM
||
166 c
->lhead_lnum
>= UBIFS_LOG_LNUM
+ c
->log_lebs
||
167 c
->lhead_offs
< 0 || c
->lhead_offs
>= c
->leb_size
||
168 c
->lhead_offs
& (c
->min_io_size
- 1)) {
173 if (c
->zroot
.lnum
>= c
->leb_cnt
|| c
->zroot
.lnum
< c
->main_first
||
174 c
->zroot
.offs
>= c
->leb_size
|| c
->zroot
.offs
& 7) {
179 if (c
->zroot
.len
< c
->ranges
[UBIFS_IDX_NODE
].min_len
||
180 c
->zroot
.len
> c
->ranges
[UBIFS_IDX_NODE
].max_len
) {
185 if (c
->gc_lnum
>= c
->leb_cnt
|| c
->gc_lnum
< c
->main_first
) {
190 if (c
->ihead_lnum
>= c
->leb_cnt
|| c
->ihead_lnum
< c
->main_first
||
191 c
->ihead_offs
% c
->min_io_size
|| c
->ihead_offs
< 0 ||
192 c
->ihead_offs
> c
->leb_size
|| c
->ihead_offs
& 7) {
197 main_sz
= (long long)c
->main_lebs
* c
->leb_size
;
198 if (c
->bi
.old_idx_sz
& 7 || c
->bi
.old_idx_sz
>= main_sz
) {
203 if (c
->lpt_lnum
< c
->lpt_first
|| c
->lpt_lnum
> c
->lpt_last
||
204 c
->lpt_offs
< 0 || c
->lpt_offs
+ c
->nnode_sz
> c
->leb_size
) {
209 if (c
->nhead_lnum
< c
->lpt_first
|| c
->nhead_lnum
> c
->lpt_last
||
210 c
->nhead_offs
< 0 || c
->nhead_offs
% c
->min_io_size
||
211 c
->nhead_offs
> c
->leb_size
) {
216 if (c
->ltab_lnum
< c
->lpt_first
|| c
->ltab_lnum
> c
->lpt_last
||
218 c
->ltab_offs
+ c
->ltab_sz
> c
->leb_size
) {
223 if (c
->big_lpt
&& (c
->lsave_lnum
< c
->lpt_first
||
224 c
->lsave_lnum
> c
->lpt_last
|| c
->lsave_offs
< 0 ||
225 c
->lsave_offs
+ c
->lsave_sz
> c
->leb_size
)) {
230 if (c
->lscan_lnum
< c
->main_first
|| c
->lscan_lnum
>= c
->leb_cnt
) {
235 if (c
->lst
.empty_lebs
< 0 || c
->lst
.empty_lebs
> c
->main_lebs
- 2) {
240 if (c
->lst
.idx_lebs
< 0 || c
->lst
.idx_lebs
> c
->main_lebs
- 1) {
245 if (c
->lst
.total_free
< 0 || c
->lst
.total_free
> main_sz
||
246 c
->lst
.total_free
& 7) {
251 if (c
->lst
.total_dirty
< 0 || (c
->lst
.total_dirty
& 7)) {
256 if (c
->lst
.total_used
< 0 || (c
->lst
.total_used
& 7)) {
261 if (c
->lst
.total_free
+ c
->lst
.total_dirty
+
262 c
->lst
.total_used
> main_sz
) {
267 if (c
->lst
.total_dead
+ c
->lst
.total_dark
+
268 c
->lst
.total_used
+ c
->bi
.old_idx_sz
> main_sz
) {
273 if (c
->lst
.total_dead
< 0 ||
274 c
->lst
.total_dead
> c
->lst
.total_free
+ c
->lst
.total_dirty
||
275 c
->lst
.total_dead
& 7) {
280 if (c
->lst
.total_dark
< 0 ||
281 c
->lst
.total_dark
> c
->lst
.total_free
+ c
->lst
.total_dirty
||
282 c
->lst
.total_dark
& 7) {
290 ubifs_err(c
, "bad master node at offset %d error %d", c
->mst_offs
, err
);
291 ubifs_dump_node(c
, c
->mst_node
);
296 * ubifs_read_master - read master node.
297 * @c: UBIFS file-system description object
299 * This function finds and reads the master node during file-system mount. If
300 * the flash is empty, it creates default master node as well. Returns zero in
301 * case of success and a negative error code in case of failure.
303 int ubifs_read_master(struct ubifs_info
*c
)
305 int err
, old_leb_cnt
;
307 c
->mst_node
= kzalloc(c
->mst_node_alsz
, GFP_KERNEL
);
311 err
= scan_for_master(c
);
314 err
= ubifs_recover_master_node(c
);
317 * Note, we do not free 'c->mst_node' here because the
318 * unmount routine will take care of this.
323 /* Make sure that the recovery flag is clear */
324 c
->mst_node
->flags
&= cpu_to_le32(~UBIFS_MST_RCVRY
);
326 c
->max_sqnum
= le64_to_cpu(c
->mst_node
->ch
.sqnum
);
327 c
->highest_inum
= le64_to_cpu(c
->mst_node
->highest_inum
);
328 c
->cmt_no
= le64_to_cpu(c
->mst_node
->cmt_no
);
329 c
->zroot
.lnum
= le32_to_cpu(c
->mst_node
->root_lnum
);
330 c
->zroot
.offs
= le32_to_cpu(c
->mst_node
->root_offs
);
331 c
->zroot
.len
= le32_to_cpu(c
->mst_node
->root_len
);
332 c
->lhead_lnum
= le32_to_cpu(c
->mst_node
->log_lnum
);
333 c
->gc_lnum
= le32_to_cpu(c
->mst_node
->gc_lnum
);
334 c
->ihead_lnum
= le32_to_cpu(c
->mst_node
->ihead_lnum
);
335 c
->ihead_offs
= le32_to_cpu(c
->mst_node
->ihead_offs
);
336 c
->bi
.old_idx_sz
= le64_to_cpu(c
->mst_node
->index_size
);
337 c
->lpt_lnum
= le32_to_cpu(c
->mst_node
->lpt_lnum
);
338 c
->lpt_offs
= le32_to_cpu(c
->mst_node
->lpt_offs
);
339 c
->nhead_lnum
= le32_to_cpu(c
->mst_node
->nhead_lnum
);
340 c
->nhead_offs
= le32_to_cpu(c
->mst_node
->nhead_offs
);
341 c
->ltab_lnum
= le32_to_cpu(c
->mst_node
->ltab_lnum
);
342 c
->ltab_offs
= le32_to_cpu(c
->mst_node
->ltab_offs
);
343 c
->lsave_lnum
= le32_to_cpu(c
->mst_node
->lsave_lnum
);
344 c
->lsave_offs
= le32_to_cpu(c
->mst_node
->lsave_offs
);
345 c
->lscan_lnum
= le32_to_cpu(c
->mst_node
->lscan_lnum
);
346 c
->lst
.empty_lebs
= le32_to_cpu(c
->mst_node
->empty_lebs
);
347 c
->lst
.idx_lebs
= le32_to_cpu(c
->mst_node
->idx_lebs
);
348 old_leb_cnt
= le32_to_cpu(c
->mst_node
->leb_cnt
);
349 c
->lst
.total_free
= le64_to_cpu(c
->mst_node
->total_free
);
350 c
->lst
.total_dirty
= le64_to_cpu(c
->mst_node
->total_dirty
);
351 c
->lst
.total_used
= le64_to_cpu(c
->mst_node
->total_used
);
352 c
->lst
.total_dead
= le64_to_cpu(c
->mst_node
->total_dead
);
353 c
->lst
.total_dark
= le64_to_cpu(c
->mst_node
->total_dark
);
355 ubifs_copy_hash(c
, c
->mst_node
->hash_root_idx
, c
->zroot
.hash
);
357 c
->calc_idx_sz
= c
->bi
.old_idx_sz
;
359 if (c
->mst_node
->flags
& cpu_to_le32(UBIFS_MST_NO_ORPHS
))
362 if (old_leb_cnt
!= c
->leb_cnt
) {
363 /* The file system has been resized */
364 int growth
= c
->leb_cnt
- old_leb_cnt
;
366 if (c
->leb_cnt
< old_leb_cnt
||
367 c
->leb_cnt
< UBIFS_MIN_LEB_CNT
) {
368 ubifs_err(c
, "bad leb_cnt on master node");
369 ubifs_dump_node(c
, c
->mst_node
);
373 dbg_mnt("Auto resizing (master) from %d LEBs to %d LEBs",
374 old_leb_cnt
, c
->leb_cnt
);
375 c
->lst
.empty_lebs
+= growth
;
376 c
->lst
.total_free
+= growth
* (long long)c
->leb_size
;
377 c
->lst
.total_dark
+= growth
* (long long)c
->dark_wm
;
380 * Reflect changes back onto the master node. N.B. the master
381 * node gets written immediately whenever mounting (or
382 * remounting) in read-write mode, so we do not need to write it
385 c
->mst_node
->leb_cnt
= cpu_to_le32(c
->leb_cnt
);
386 c
->mst_node
->empty_lebs
= cpu_to_le32(c
->lst
.empty_lebs
);
387 c
->mst_node
->total_free
= cpu_to_le64(c
->lst
.total_free
);
388 c
->mst_node
->total_dark
= cpu_to_le64(c
->lst
.total_dark
);
391 err
= validate_master(c
);
395 err
= dbg_old_index_check_init(c
, &c
->zroot
);
401 * ubifs_write_master - write master node.
402 * @c: UBIFS file-system description object
404 * This function writes the master node. Returns zero in case of success and a
405 * negative error code in case of failure. The master node is written twice to
408 int ubifs_write_master(struct ubifs_info
*c
)
410 int err
, lnum
, offs
, len
;
412 ubifs_assert(c
, !c
->ro_media
&& !c
->ro_mount
);
416 lnum
= UBIFS_MST_LNUM
;
417 offs
= c
->mst_offs
+ c
->mst_node_alsz
;
418 len
= UBIFS_MST_NODE_SZ
;
420 if (offs
+ UBIFS_MST_NODE_SZ
> c
->leb_size
) {
421 err
= ubifs_leb_unmap(c
, lnum
);
428 c
->mst_node
->highest_inum
= cpu_to_le64(c
->highest_inum
);
430 ubifs_copy_hash(c
, c
->zroot
.hash
, c
->mst_node
->hash_root_idx
);
431 err
= ubifs_write_node_hmac(c
, c
->mst_node
, len
, lnum
, offs
,
432 offsetof(struct ubifs_mst_node
, hmac
));
439 err
= ubifs_leb_unmap(c
, lnum
);
443 err
= ubifs_write_node_hmac(c
, c
->mst_node
, len
, lnum
, offs
,
444 offsetof(struct ubifs_mst_node
, hmac
));