^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * This file is part of UBIFS.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2006-2008 Nokia Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Authors: Adrian Hunter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Artem Bityutskiy (Битюцкий Артём)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * This file implements the scan which is a general-purpose function for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * determining what nodes are in an eraseblock. The scan is used to replay the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * journal, to do garbage collection. for the TNC in-the-gaps method, and by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * debugging functions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include "ubifs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * scan_padding_bytes - scan for padding bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * @buf: buffer to scan
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * @len: length of buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * This function returns the number of padding bytes on success and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * %SCANNED_GARBAGE on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static int scan_padding_bytes(void *buf, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) int pad_len = 0, max_pad_len = min_t(int, UBIFS_PAD_NODE_SZ, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) uint8_t *p = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) dbg_scan("not a node");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) while (pad_len < max_pad_len && *p++ == UBIFS_PADDING_BYTE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) pad_len += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (!pad_len || (pad_len & 7))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return SCANNED_GARBAGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) dbg_scan("%d padding bytes", pad_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) return pad_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) * ubifs_scan_a_node - scan for a node or padding.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * @buf: buffer to scan
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * @len: length of buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * @lnum: logical eraseblock number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * @offs: offset within the logical eraseblock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * @quiet: print no messages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * This function returns a scanning code to indicate what was scanned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) int ubifs_scan_a_node(const struct ubifs_info *c, void *buf, int len, int lnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) int offs, int quiet)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) struct ubifs_ch *ch = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) uint32_t magic;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) magic = le32_to_cpu(ch->magic);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if (magic == 0xFFFFFFFF) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) dbg_scan("hit empty space at LEB %d:%d", lnum, offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) return SCANNED_EMPTY_SPACE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) if (magic != UBIFS_NODE_MAGIC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) return scan_padding_bytes(buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if (len < UBIFS_CH_SZ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return SCANNED_GARBAGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) dbg_scan("scanning %s at LEB %d:%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) dbg_ntype(ch->node_type), lnum, offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (ubifs_check_node(c, buf, lnum, offs, quiet, 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return SCANNED_A_CORRUPT_NODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (ch->node_type == UBIFS_PAD_NODE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) struct ubifs_pad_node *pad = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) int pad_len = le32_to_cpu(pad->pad_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) int node_len = le32_to_cpu(ch->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) /* Validate the padding node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) if (pad_len < 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) offs + node_len + pad_len > c->leb_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (!quiet) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) ubifs_err(c, "bad pad node at LEB %d:%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) lnum, offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) ubifs_dump_node(c, pad);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return SCANNED_A_BAD_PAD_NODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) /* Make the node pads to 8-byte boundary */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) if ((node_len + pad_len) & 7) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if (!quiet)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) ubifs_err(c, "bad padding length %d - %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) offs, offs + node_len + pad_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return SCANNED_A_BAD_PAD_NODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) dbg_scan("%d bytes padded at LEB %d:%d, offset now %d", pad_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) lnum, offs, ALIGN(offs + node_len + pad_len, 8));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) return node_len + pad_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) return SCANNED_A_NODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * ubifs_start_scan - create LEB scanning information at start of scan.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * @lnum: logical eraseblock number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * @offs: offset to start at (usually zero)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * @sbuf: scan buffer (must be c->leb_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) * This function returns the scanned information on success and a negative error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * code on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) struct ubifs_scan_leb *ubifs_start_scan(const struct ubifs_info *c, int lnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) int offs, void *sbuf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) struct ubifs_scan_leb *sleb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) dbg_scan("scan LEB %d:%d", lnum, offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) sleb = kzalloc(sizeof(struct ubifs_scan_leb), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (!sleb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) sleb->lnum = lnum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) INIT_LIST_HEAD(&sleb->nodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) sleb->buf = sbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) err = ubifs_leb_read(c, lnum, sbuf + offs, offs, c->leb_size - offs, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if (err && err != -EBADMSG) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) ubifs_err(c, "cannot read %d bytes from LEB %d:%d, error %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) c->leb_size - offs, lnum, offs, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) kfree(sleb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) * Note, we ignore integrity errors (EBASMSG) because all the nodes are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * protected by CRC checksums.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) return sleb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) * ubifs_end_scan - update LEB scanning information at end of scan.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) * @sleb: scanning information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) * @lnum: logical eraseblock number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) * @offs: offset to start at (usually zero)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) void ubifs_end_scan(const struct ubifs_info *c, struct ubifs_scan_leb *sleb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) int lnum, int offs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) dbg_scan("stop scanning LEB %d at offset %d", lnum, offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) ubifs_assert(c, offs % c->min_io_size == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) sleb->endpt = ALIGN(offs, c->min_io_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) * ubifs_add_snod - add a scanned node to LEB scanning information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) * @sleb: scanning information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) * @buf: buffer containing node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) * @offs: offset of node on flash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) * This function returns %0 on success and a negative error code on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) int ubifs_add_snod(const struct ubifs_info *c, struct ubifs_scan_leb *sleb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) void *buf, int offs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) struct ubifs_ch *ch = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) struct ubifs_ino_node *ino = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) struct ubifs_scan_node *snod;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) snod = kmalloc(sizeof(struct ubifs_scan_node), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) if (!snod)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) snod->sqnum = le64_to_cpu(ch->sqnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) snod->type = ch->node_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) snod->offs = offs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) snod->len = le32_to_cpu(ch->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) snod->node = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) switch (ch->node_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) case UBIFS_INO_NODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) case UBIFS_DENT_NODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) case UBIFS_XENT_NODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) case UBIFS_DATA_NODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) * The key is in the same place in all keyed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) * nodes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) key_read(c, &ino->key, &snod->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) invalid_key_init(c, &snod->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) list_add_tail(&snod->list, &sleb->nodes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) sleb->nodes_cnt += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) * ubifs_scanned_corruption - print information after UBIFS scanned corruption.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) * @lnum: LEB number of corruption
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) * @offs: offset of corruption
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) * @buf: buffer containing corruption
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) void ubifs_scanned_corruption(const struct ubifs_info *c, int lnum, int offs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) void *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) ubifs_err(c, "corruption at LEB %d:%d", lnum, offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) len = c->leb_size - offs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) if (len > 8192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) len = 8192;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) ubifs_err(c, "first %d bytes from LEB %d:%d", len, lnum, offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 4, buf, len, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) * ubifs_scan - scan a logical eraseblock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) * @c: UBIFS file-system description object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) * @lnum: logical eraseblock number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) * @offs: offset to start at (usually zero)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) * @sbuf: scan buffer (must be of @c->leb_size bytes in size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) * @quiet: print no messages
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) * This function scans LEB number @lnum and returns complete information about
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) * its contents. Returns the scanned information in case of success and,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) * %-EUCLEAN if the LEB neads recovery, and other negative error codes in case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) * of failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * If @quiet is non-zero, this function does not print large and scary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) * error messages and flash dumps in case of errors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) struct ubifs_scan_leb *ubifs_scan(const struct ubifs_info *c, int lnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) int offs, void *sbuf, int quiet)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) void *buf = sbuf + offs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) int err, len = c->leb_size - offs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) struct ubifs_scan_leb *sleb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) sleb = ubifs_start_scan(c, lnum, offs, sbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) if (IS_ERR(sleb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) return sleb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) while (len >= 8) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) struct ubifs_ch *ch = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) int node_len, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) dbg_scan("look at LEB %d:%d (%d bytes left)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) lnum, offs, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) ret = ubifs_scan_a_node(c, buf, len, lnum, offs, quiet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) if (ret > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) /* Padding bytes or a valid padding node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) offs += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) buf += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) len -= ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) if (ret == SCANNED_EMPTY_SPACE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) /* Empty space is checked later */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) switch (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) case SCANNED_GARBAGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) ubifs_err(c, "garbage");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) goto corrupted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) case SCANNED_A_NODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) case SCANNED_A_CORRUPT_NODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) case SCANNED_A_BAD_PAD_NODE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) ubifs_err(c, "bad node");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) goto corrupted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) ubifs_err(c, "unknown");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) err = ubifs_add_snod(c, sleb, buf, offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) node_len = ALIGN(le32_to_cpu(ch->len), 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) offs += node_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) buf += node_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) len -= node_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) if (offs % c->min_io_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) if (!quiet)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) ubifs_err(c, "empty space starts at non-aligned offset %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) goto corrupted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) ubifs_end_scan(c, sleb, lnum, offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) for (; len > 4; offs += 4, buf = buf + 4, len -= 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) if (*(uint32_t *)buf != 0xffffffff)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) for (; len; offs++, buf++, len--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) if (*(uint8_t *)buf != 0xff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) if (!quiet)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) ubifs_err(c, "corrupt empty space at LEB %d:%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) lnum, offs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) goto corrupted;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) return sleb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) corrupted:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) if (!quiet) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) ubifs_scanned_corruption(c, lnum, offs, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) ubifs_err(c, "LEB %d scanning failed", lnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) err = -EUCLEAN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) ubifs_scan_destroy(sleb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) ubifs_err(c, "LEB %d scanning failed, error %d", lnum, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) ubifs_scan_destroy(sleb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) * ubifs_scan_destroy - destroy LEB scanning information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) * @sleb: scanning information to free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) void ubifs_scan_destroy(struct ubifs_scan_leb *sleb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) struct ubifs_scan_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) struct list_head *head;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) head = &sleb->nodes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) while (!list_empty(head)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) node = list_entry(head->next, struct ubifs_scan_node, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) list_del(&node->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) kfree(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) kfree(sleb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) }