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);
52 * scan_for_master - search the valid master node.
53 * @c: UBIFS file-system description object
55 * This function scans the master node LEBs and search for the latest master
56 * node. Returns zero in case of success, %-EUCLEAN if there master area is
57 * corrupted and requires recovery, and a negative error code in case of
60 static int scan_for_master(struct ubifs_info *c)
62 struct ubifs_scan_leb *sleb;
63 struct ubifs_scan_node *snod;
64 int lnum, offs = 0, nodes_cnt, err;
66 lnum = UBIFS_MST_LNUM;
68 sleb = ubifs_scan(c, lnum, 0, c->sbuf, 1);
71 nodes_cnt = sleb->nodes_cnt;
73 snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node,
75 if (snod->type != UBIFS_MST_NODE)
77 memcpy(c->mst_node, snod->node, snod->len);
80 ubifs_scan_destroy(sleb);
84 sleb = ubifs_scan(c, lnum, 0, c->sbuf, 1);
87 if (sleb->nodes_cnt != nodes_cnt)
91 snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node, list);
92 if (snod->type != UBIFS_MST_NODE)
94 if (snod->offs != offs)
96 if (ubifs_compare_master_node(c, c->mst_node, snod->node))
100 ubifs_scan_destroy(sleb);
102 if (!ubifs_authenticated(c))
105 err = ubifs_node_verify_hmac(c, c->mst_node,
106 sizeof(struct ubifs_mst_node),
107 offsetof(struct ubifs_mst_node, hmac));
109 ubifs_err(c, "Failed to verify master node HMAC");
116 ubifs_scan_destroy(sleb);
120 ubifs_err(c, "unexpected node type %d master LEB %d:%d",
121 snod->type, lnum, snod->offs);
122 ubifs_scan_destroy(sleb);
127 * validate_master - validate master node.
128 * @c: UBIFS file-system description object
130 * This function validates data which was read from master node. Returns zero
131 * if the data is all right and %-EINVAL if not.
133 static int validate_master(const struct ubifs_info *c)
138 if (c->max_sqnum >= SQNUM_WATERMARK) {
143 if (c->cmt_no >= c->max_sqnum) {
148 if (c->highest_inum >= INUM_WATERMARK) {
153 if (c->lhead_lnum < UBIFS_LOG_LNUM ||
154 c->lhead_lnum >= UBIFS_LOG_LNUM + c->log_lebs ||
155 c->lhead_offs < 0 || c->lhead_offs >= c->leb_size ||
156 c->lhead_offs & (c->min_io_size - 1)) {
161 if (c->zroot.lnum >= c->leb_cnt || c->zroot.lnum < c->main_first ||
162 c->zroot.offs >= c->leb_size || c->zroot.offs & 7) {
167 if (c->zroot.len < c->ranges[UBIFS_IDX_NODE].min_len ||
168 c->zroot.len > c->ranges[UBIFS_IDX_NODE].max_len) {
173 if (c->gc_lnum >= c->leb_cnt || c->gc_lnum < c->main_first) {
178 if (c->ihead_lnum >= c->leb_cnt || c->ihead_lnum < c->main_first ||
179 c->ihead_offs % c->min_io_size || c->ihead_offs < 0 ||
180 c->ihead_offs > c->leb_size || c->ihead_offs & 7) {
185 main_sz = (long long)c->main_lebs * c->leb_size;
186 if (c->bi.old_idx_sz & 7 || c->bi.old_idx_sz >= main_sz) {
191 if (c->lpt_lnum < c->lpt_first || c->lpt_lnum > c->lpt_last ||
192 c->lpt_offs < 0 || c->lpt_offs + c->nnode_sz > c->leb_size) {
197 if (c->nhead_lnum < c->lpt_first || c->nhead_lnum > c->lpt_last ||
198 c->nhead_offs < 0 || c->nhead_offs % c->min_io_size ||
199 c->nhead_offs > c->leb_size) {
204 if (c->ltab_lnum < c->lpt_first || c->ltab_lnum > c->lpt_last ||
206 c->ltab_offs + c->ltab_sz > c->leb_size) {
211 if (c->big_lpt && (c->lsave_lnum < c->lpt_first ||
212 c->lsave_lnum > c->lpt_last || c->lsave_offs < 0 ||
213 c->lsave_offs + c->lsave_sz > c->leb_size)) {
218 if (c->lscan_lnum < c->main_first || c->lscan_lnum >= c->leb_cnt) {
223 if (c->lst.empty_lebs < 0 || c->lst.empty_lebs > c->main_lebs - 2) {
228 if (c->lst.idx_lebs < 0 || c->lst.idx_lebs > c->main_lebs - 1) {
233 if (c->lst.total_free < 0 || c->lst.total_free > main_sz ||
234 c->lst.total_free & 7) {
239 if (c->lst.total_dirty < 0 || (c->lst.total_dirty & 7)) {
244 if (c->lst.total_used < 0 || (c->lst.total_used & 7)) {
249 if (c->lst.total_free + c->lst.total_dirty +
250 c->lst.total_used > main_sz) {
255 if (c->lst.total_dead + c->lst.total_dark +
256 c->lst.total_used + c->bi.old_idx_sz > main_sz) {
261 if (c->lst.total_dead < 0 ||
262 c->lst.total_dead > c->lst.total_free + c->lst.total_dirty ||
263 c->lst.total_dead & 7) {
268 if (c->lst.total_dark < 0 ||
269 c->lst.total_dark > c->lst.total_free + c->lst.total_dirty ||
270 c->lst.total_dark & 7) {
278 ubifs_err(c, "bad master node at offset %d error %d", c->mst_offs, err);
279 ubifs_dump_node(c, c->mst_node);
284 * ubifs_read_master - read master node.
285 * @c: UBIFS file-system description object
287 * This function finds and reads the master node during file-system mount. If
288 * the flash is empty, it creates default master node as well. Returns zero in
289 * case of success and a negative error code in case of failure.
291 int ubifs_read_master(struct ubifs_info *c)
293 int err, old_leb_cnt;
295 c->mst_node = kzalloc(c->mst_node_alsz, GFP_KERNEL);
299 err = scan_for_master(c);
302 err = ubifs_recover_master_node(c);
305 * Note, we do not free 'c->mst_node' here because the
306 * unmount routine will take care of this.
311 /* Make sure that the recovery flag is clear */
312 c->mst_node->flags &= cpu_to_le32(~UBIFS_MST_RCVRY);
314 c->max_sqnum = le64_to_cpu(c->mst_node->ch.sqnum);
315 c->highest_inum = le64_to_cpu(c->mst_node->highest_inum);
316 c->cmt_no = le64_to_cpu(c->mst_node->cmt_no);
317 c->zroot.lnum = le32_to_cpu(c->mst_node->root_lnum);
318 c->zroot.offs = le32_to_cpu(c->mst_node->root_offs);
319 c->zroot.len = le32_to_cpu(c->mst_node->root_len);
320 c->lhead_lnum = le32_to_cpu(c->mst_node->log_lnum);
321 c->gc_lnum = le32_to_cpu(c->mst_node->gc_lnum);
322 c->ihead_lnum = le32_to_cpu(c->mst_node->ihead_lnum);
323 c->ihead_offs = le32_to_cpu(c->mst_node->ihead_offs);
324 c->bi.old_idx_sz = le64_to_cpu(c->mst_node->index_size);
325 c->lpt_lnum = le32_to_cpu(c->mst_node->lpt_lnum);
326 c->lpt_offs = le32_to_cpu(c->mst_node->lpt_offs);
327 c->nhead_lnum = le32_to_cpu(c->mst_node->nhead_lnum);
328 c->nhead_offs = le32_to_cpu(c->mst_node->nhead_offs);
329 c->ltab_lnum = le32_to_cpu(c->mst_node->ltab_lnum);
330 c->ltab_offs = le32_to_cpu(c->mst_node->ltab_offs);
331 c->lsave_lnum = le32_to_cpu(c->mst_node->lsave_lnum);
332 c->lsave_offs = le32_to_cpu(c->mst_node->lsave_offs);
333 c->lscan_lnum = le32_to_cpu(c->mst_node->lscan_lnum);
334 c->lst.empty_lebs = le32_to_cpu(c->mst_node->empty_lebs);
335 c->lst.idx_lebs = le32_to_cpu(c->mst_node->idx_lebs);
336 old_leb_cnt = le32_to_cpu(c->mst_node->leb_cnt);
337 c->lst.total_free = le64_to_cpu(c->mst_node->total_free);
338 c->lst.total_dirty = le64_to_cpu(c->mst_node->total_dirty);
339 c->lst.total_used = le64_to_cpu(c->mst_node->total_used);
340 c->lst.total_dead = le64_to_cpu(c->mst_node->total_dead);
341 c->lst.total_dark = le64_to_cpu(c->mst_node->total_dark);
343 ubifs_copy_hash(c, c->mst_node->hash_root_idx, c->zroot.hash);
345 c->calc_idx_sz = c->bi.old_idx_sz;
347 if (c->mst_node->flags & cpu_to_le32(UBIFS_MST_NO_ORPHS))
350 if (old_leb_cnt != c->leb_cnt) {
351 /* The file system has been resized */
352 int growth = c->leb_cnt - old_leb_cnt;
354 if (c->leb_cnt < old_leb_cnt ||
355 c->leb_cnt < UBIFS_MIN_LEB_CNT) {
356 ubifs_err(c, "bad leb_cnt on master node");
357 ubifs_dump_node(c, c->mst_node);
361 dbg_mnt("Auto resizing (master) from %d LEBs to %d LEBs",
362 old_leb_cnt, c->leb_cnt);
363 c->lst.empty_lebs += growth;
364 c->lst.total_free += growth * (long long)c->leb_size;
365 c->lst.total_dark += growth * (long long)c->dark_wm;
368 * Reflect changes back onto the master node. N.B. the master
369 * node gets written immediately whenever mounting (or
370 * remounting) in read-write mode, so we do not need to write it
373 c->mst_node->leb_cnt = cpu_to_le32(c->leb_cnt);
374 c->mst_node->empty_lebs = cpu_to_le32(c->lst.empty_lebs);
375 c->mst_node->total_free = cpu_to_le64(c->lst.total_free);
376 c->mst_node->total_dark = cpu_to_le64(c->lst.total_dark);
379 err = validate_master(c);
383 err = dbg_old_index_check_init(c, &c->zroot);
389 * ubifs_write_master - write master node.
390 * @c: UBIFS file-system description object
392 * This function writes the master node. Returns zero in case of success and a
393 * negative error code in case of failure. The master node is written twice to
396 int ubifs_write_master(struct ubifs_info *c)
398 int err, lnum, offs, len;
400 ubifs_assert(c, !c->ro_media && !c->ro_mount);
404 lnum = UBIFS_MST_LNUM;
405 offs = c->mst_offs + c->mst_node_alsz;
406 len = UBIFS_MST_NODE_SZ;
408 if (offs + UBIFS_MST_NODE_SZ > c->leb_size) {
409 err = ubifs_leb_unmap(c, lnum);
416 c->mst_node->highest_inum = cpu_to_le64(c->highest_inum);
418 ubifs_copy_hash(c, c->zroot.hash, c->mst_node->hash_root_idx);
419 err = ubifs_write_node_hmac(c, c->mst_node, len, lnum, offs,
420 offsetof(struct ubifs_mst_node, hmac));
427 err = ubifs_leb_unmap(c, lnum);
431 err = ubifs_write_node_hmac(c, c->mst_node, len, lnum, offs,
432 offsetof(struct ubifs_mst_node, hmac));