1 // SPDX-License-Identifier: GPL-2.0-only
3 * This file is part of UBIFS.
5 * Copyright (C) 2006-2008 Nokia Corporation.
7 * Authors: Artem Bityutskiy (Битюцкий Артём)
11 /* This file implements reading and writing the master node */
16 * ubifs_compare_master_node - compare two UBIFS master nodes
17 * @c: UBIFS file-system description object
19 * @m2: the second node
21 * This function compares two UBIFS master nodes. Returns 0 if they are equal
24 int ubifs_compare_master_node(struct ubifs_info
*c
, void *m1
, void *m2
)
28 int hmac_offs
= offsetof(struct ubifs_mst_node
, hmac
);
31 * Do not compare the common node header since the sequence number and
32 * hence the CRC are different.
34 ret
= memcmp(m1
+ UBIFS_CH_SZ
, m2
+ UBIFS_CH_SZ
,
35 hmac_offs
- UBIFS_CH_SZ
);
40 * Do not compare the embedded HMAC aswell which also must be different
41 * due to the different common node header.
43 behind
= hmac_offs
+ UBIFS_MAX_HMAC_LEN
;
45 if (UBIFS_MST_NODE_SZ
> behind
)
46 return memcmp(m1
+ behind
, m2
+ behind
, UBIFS_MST_NODE_SZ
- behind
);
51 /* mst_node_check_hash - Check hash of a master node
52 * @c: UBIFS file-system description object
53 * @mst: The master node
54 * @expected: The expected hash of the master node
56 * This checks the hash of a master node against a given expected hash.
57 * Note that we have two master nodes on a UBIFS image which have different
58 * sequence numbers and consequently different CRCs. To be able to match
59 * both master nodes we exclude the common node header containing the sequence
60 * number and CRC from the hash.
62 * Returns 0 if the hashes are equal, a negative error code otherwise.
64 static int mst_node_check_hash(const struct ubifs_info
*c
,
65 const struct ubifs_mst_node
*mst
,
68 u8 calc
[UBIFS_MAX_HASH_LEN
];
69 const void *node
= mst
;
71 crypto_shash_tfm_digest(c
->hash_tfm
, node
+ sizeof(struct ubifs_ch
),
72 UBIFS_MST_NODE_SZ
- sizeof(struct ubifs_ch
),
75 if (ubifs_check_hash(c
, expected
, calc
))
82 * scan_for_master - search the valid master node.
83 * @c: UBIFS file-system description object
85 * This function scans the master node LEBs and search for the latest master
86 * node. Returns zero in case of success, %-EUCLEAN if there master area is
87 * corrupted and requires recovery, and a negative error code in case of
90 static int scan_for_master(struct ubifs_info
*c
)
92 struct ubifs_scan_leb
*sleb
;
93 struct ubifs_scan_node
*snod
;
94 int lnum
, offs
= 0, nodes_cnt
, err
;
96 lnum
= UBIFS_MST_LNUM
;
98 sleb
= ubifs_scan(c
, lnum
, 0, c
->sbuf
, 1);
100 return PTR_ERR(sleb
);
101 nodes_cnt
= sleb
->nodes_cnt
;
103 snod
= list_entry(sleb
->nodes
.prev
, struct ubifs_scan_node
,
105 if (snod
->type
!= UBIFS_MST_NODE
)
107 memcpy(c
->mst_node
, snod
->node
, snod
->len
);
110 ubifs_scan_destroy(sleb
);
114 sleb
= ubifs_scan(c
, lnum
, 0, c
->sbuf
, 1);
116 return PTR_ERR(sleb
);
117 if (sleb
->nodes_cnt
!= nodes_cnt
)
119 if (!sleb
->nodes_cnt
)
121 snod
= list_entry(sleb
->nodes
.prev
, struct ubifs_scan_node
, list
);
122 if (snod
->type
!= UBIFS_MST_NODE
)
124 if (snod
->offs
!= offs
)
126 if (ubifs_compare_master_node(c
, c
->mst_node
, snod
->node
))
130 ubifs_scan_destroy(sleb
);
132 if (!ubifs_authenticated(c
))
135 if (ubifs_hmac_zero(c
, c
->mst_node
->hmac
)) {
136 err
= mst_node_check_hash(c
, c
->mst_node
,
137 c
->sup_node
->hash_mst
);
139 ubifs_err(c
, "Failed to verify master node hash");
141 err
= ubifs_node_verify_hmac(c
, c
->mst_node
,
142 sizeof(struct ubifs_mst_node
),
143 offsetof(struct ubifs_mst_node
, hmac
));
145 ubifs_err(c
, "Failed to verify master node HMAC");
154 ubifs_scan_destroy(sleb
);
158 ubifs_err(c
, "unexpected node type %d master LEB %d:%d",
159 snod
->type
, lnum
, snod
->offs
);
160 ubifs_scan_destroy(sleb
);
165 * validate_master - validate master node.
166 * @c: UBIFS file-system description object
168 * This function validates data which was read from master node. Returns zero
169 * if the data is all right and %-EINVAL if not.
171 static int validate_master(const struct ubifs_info
*c
)
176 if (c
->max_sqnum
>= SQNUM_WATERMARK
) {
181 if (c
->cmt_no
>= c
->max_sqnum
) {
186 if (c
->highest_inum
>= INUM_WATERMARK
) {
191 if (c
->lhead_lnum
< UBIFS_LOG_LNUM
||
192 c
->lhead_lnum
>= UBIFS_LOG_LNUM
+ c
->log_lebs
||
193 c
->lhead_offs
< 0 || c
->lhead_offs
>= c
->leb_size
||
194 c
->lhead_offs
& (c
->min_io_size
- 1)) {
199 if (c
->zroot
.lnum
>= c
->leb_cnt
|| c
->zroot
.lnum
< c
->main_first
||
200 c
->zroot
.offs
>= c
->leb_size
|| c
->zroot
.offs
& 7) {
205 if (c
->zroot
.len
< c
->ranges
[UBIFS_IDX_NODE
].min_len
||
206 c
->zroot
.len
> c
->ranges
[UBIFS_IDX_NODE
].max_len
) {
211 if (c
->gc_lnum
>= c
->leb_cnt
|| c
->gc_lnum
< c
->main_first
) {
216 if (c
->ihead_lnum
>= c
->leb_cnt
|| c
->ihead_lnum
< c
->main_first
||
217 c
->ihead_offs
% c
->min_io_size
|| c
->ihead_offs
< 0 ||
218 c
->ihead_offs
> c
->leb_size
|| c
->ihead_offs
& 7) {
223 main_sz
= (long long)c
->main_lebs
* c
->leb_size
;
224 if (c
->bi
.old_idx_sz
& 7 || c
->bi
.old_idx_sz
>= main_sz
) {
229 if (c
->lpt_lnum
< c
->lpt_first
|| c
->lpt_lnum
> c
->lpt_last
||
230 c
->lpt_offs
< 0 || c
->lpt_offs
+ c
->nnode_sz
> c
->leb_size
) {
235 if (c
->nhead_lnum
< c
->lpt_first
|| c
->nhead_lnum
> c
->lpt_last
||
236 c
->nhead_offs
< 0 || c
->nhead_offs
% c
->min_io_size
||
237 c
->nhead_offs
> c
->leb_size
) {
242 if (c
->ltab_lnum
< c
->lpt_first
|| c
->ltab_lnum
> c
->lpt_last
||
244 c
->ltab_offs
+ c
->ltab_sz
> c
->leb_size
) {
249 if (c
->big_lpt
&& (c
->lsave_lnum
< c
->lpt_first
||
250 c
->lsave_lnum
> c
->lpt_last
|| c
->lsave_offs
< 0 ||
251 c
->lsave_offs
+ c
->lsave_sz
> c
->leb_size
)) {
256 if (c
->lscan_lnum
< c
->main_first
|| c
->lscan_lnum
>= c
->leb_cnt
) {
261 if (c
->lst
.empty_lebs
< 0 || c
->lst
.empty_lebs
> c
->main_lebs
- 2) {
266 if (c
->lst
.idx_lebs
< 0 || c
->lst
.idx_lebs
> c
->main_lebs
- 1) {
271 if (c
->lst
.total_free
< 0 || c
->lst
.total_free
> main_sz
||
272 c
->lst
.total_free
& 7) {
277 if (c
->lst
.total_dirty
< 0 || (c
->lst
.total_dirty
& 7)) {
282 if (c
->lst
.total_used
< 0 || (c
->lst
.total_used
& 7)) {
287 if (c
->lst
.total_free
+ c
->lst
.total_dirty
+
288 c
->lst
.total_used
> main_sz
) {
293 if (c
->lst
.total_dead
+ c
->lst
.total_dark
+
294 c
->lst
.total_used
+ c
->bi
.old_idx_sz
> main_sz
) {
299 if (c
->lst
.total_dead
< 0 ||
300 c
->lst
.total_dead
> c
->lst
.total_free
+ c
->lst
.total_dirty
||
301 c
->lst
.total_dead
& 7) {
306 if (c
->lst
.total_dark
< 0 ||
307 c
->lst
.total_dark
> c
->lst
.total_free
+ c
->lst
.total_dirty
||
308 c
->lst
.total_dark
& 7) {
316 ubifs_err(c
, "bad master node at offset %d error %d", c
->mst_offs
, err
);
317 ubifs_dump_node(c
, c
->mst_node
);
322 * ubifs_read_master - read master node.
323 * @c: UBIFS file-system description object
325 * This function finds and reads the master node during file-system mount. If
326 * the flash is empty, it creates default master node as well. Returns zero in
327 * case of success and a negative error code in case of failure.
329 int ubifs_read_master(struct ubifs_info
*c
)
331 int err
, old_leb_cnt
;
333 c
->mst_node
= kzalloc(c
->mst_node_alsz
, GFP_KERNEL
);
337 err
= scan_for_master(c
);
340 err
= ubifs_recover_master_node(c
);
343 * Note, we do not free 'c->mst_node' here because the
344 * unmount routine will take care of this.
349 /* Make sure that the recovery flag is clear */
350 c
->mst_node
->flags
&= cpu_to_le32(~UBIFS_MST_RCVRY
);
352 c
->max_sqnum
= le64_to_cpu(c
->mst_node
->ch
.sqnum
);
353 c
->highest_inum
= le64_to_cpu(c
->mst_node
->highest_inum
);
354 c
->cmt_no
= le64_to_cpu(c
->mst_node
->cmt_no
);
355 c
->zroot
.lnum
= le32_to_cpu(c
->mst_node
->root_lnum
);
356 c
->zroot
.offs
= le32_to_cpu(c
->mst_node
->root_offs
);
357 c
->zroot
.len
= le32_to_cpu(c
->mst_node
->root_len
);
358 c
->lhead_lnum
= le32_to_cpu(c
->mst_node
->log_lnum
);
359 c
->gc_lnum
= le32_to_cpu(c
->mst_node
->gc_lnum
);
360 c
->ihead_lnum
= le32_to_cpu(c
->mst_node
->ihead_lnum
);
361 c
->ihead_offs
= le32_to_cpu(c
->mst_node
->ihead_offs
);
362 c
->bi
.old_idx_sz
= le64_to_cpu(c
->mst_node
->index_size
);
363 c
->lpt_lnum
= le32_to_cpu(c
->mst_node
->lpt_lnum
);
364 c
->lpt_offs
= le32_to_cpu(c
->mst_node
->lpt_offs
);
365 c
->nhead_lnum
= le32_to_cpu(c
->mst_node
->nhead_lnum
);
366 c
->nhead_offs
= le32_to_cpu(c
->mst_node
->nhead_offs
);
367 c
->ltab_lnum
= le32_to_cpu(c
->mst_node
->ltab_lnum
);
368 c
->ltab_offs
= le32_to_cpu(c
->mst_node
->ltab_offs
);
369 c
->lsave_lnum
= le32_to_cpu(c
->mst_node
->lsave_lnum
);
370 c
->lsave_offs
= le32_to_cpu(c
->mst_node
->lsave_offs
);
371 c
->lscan_lnum
= le32_to_cpu(c
->mst_node
->lscan_lnum
);
372 c
->lst
.empty_lebs
= le32_to_cpu(c
->mst_node
->empty_lebs
);
373 c
->lst
.idx_lebs
= le32_to_cpu(c
->mst_node
->idx_lebs
);
374 old_leb_cnt
= le32_to_cpu(c
->mst_node
->leb_cnt
);
375 c
->lst
.total_free
= le64_to_cpu(c
->mst_node
->total_free
);
376 c
->lst
.total_dirty
= le64_to_cpu(c
->mst_node
->total_dirty
);
377 c
->lst
.total_used
= le64_to_cpu(c
->mst_node
->total_used
);
378 c
->lst
.total_dead
= le64_to_cpu(c
->mst_node
->total_dead
);
379 c
->lst
.total_dark
= le64_to_cpu(c
->mst_node
->total_dark
);
381 ubifs_copy_hash(c
, c
->mst_node
->hash_root_idx
, c
->zroot
.hash
);
383 c
->calc_idx_sz
= c
->bi
.old_idx_sz
;
385 if (c
->mst_node
->flags
& cpu_to_le32(UBIFS_MST_NO_ORPHS
))
388 if (old_leb_cnt
!= c
->leb_cnt
) {
389 /* The file system has been resized */
390 int growth
= c
->leb_cnt
- old_leb_cnt
;
392 if (c
->leb_cnt
< old_leb_cnt
||
393 c
->leb_cnt
< UBIFS_MIN_LEB_CNT
) {
394 ubifs_err(c
, "bad leb_cnt on master node");
395 ubifs_dump_node(c
, c
->mst_node
);
399 dbg_mnt("Auto resizing (master) from %d LEBs to %d LEBs",
400 old_leb_cnt
, c
->leb_cnt
);
401 c
->lst
.empty_lebs
+= growth
;
402 c
->lst
.total_free
+= growth
* (long long)c
->leb_size
;
403 c
->lst
.total_dark
+= growth
* (long long)c
->dark_wm
;
406 * Reflect changes back onto the master node. N.B. the master
407 * node gets written immediately whenever mounting (or
408 * remounting) in read-write mode, so we do not need to write it
411 c
->mst_node
->leb_cnt
= cpu_to_le32(c
->leb_cnt
);
412 c
->mst_node
->empty_lebs
= cpu_to_le32(c
->lst
.empty_lebs
);
413 c
->mst_node
->total_free
= cpu_to_le64(c
->lst
.total_free
);
414 c
->mst_node
->total_dark
= cpu_to_le64(c
->lst
.total_dark
);
417 err
= validate_master(c
);
421 err
= dbg_old_index_check_init(c
, &c
->zroot
);
427 * ubifs_write_master - write master node.
428 * @c: UBIFS file-system description object
430 * This function writes the master node. Returns zero in case of success and a
431 * negative error code in case of failure. The master node is written twice to
434 int ubifs_write_master(struct ubifs_info
*c
)
436 int err
, lnum
, offs
, len
;
438 ubifs_assert(c
, !c
->ro_media
&& !c
->ro_mount
);
442 lnum
= UBIFS_MST_LNUM
;
443 offs
= c
->mst_offs
+ c
->mst_node_alsz
;
444 len
= UBIFS_MST_NODE_SZ
;
446 if (offs
+ UBIFS_MST_NODE_SZ
> c
->leb_size
) {
447 err
= ubifs_leb_unmap(c
, lnum
);
454 c
->mst_node
->highest_inum
= cpu_to_le64(c
->highest_inum
);
456 ubifs_copy_hash(c
, c
->zroot
.hash
, c
->mst_node
->hash_root_idx
);
457 err
= ubifs_write_node_hmac(c
, c
->mst_node
, len
, lnum
, offs
,
458 offsetof(struct ubifs_mst_node
, hmac
));
465 err
= ubifs_leb_unmap(c
, lnum
);
469 err
= ubifs_write_node_hmac(c
, c
->mst_node
, len
, lnum
, offs
,
470 offsetof(struct ubifs_mst_node
, hmac
));