// SPDX-License-Identifier: GPL-2.0-only /* * This file is part of UBIFS. * * Copyright (C) 2006-2008 Nokia Corporation * * Authors: Adrian Hunter * Artem Bityutskiy (Битюцкий Артём)
*/
/* * This file implements the scan which is a general-purpose function for * determining what nodes are in an eraseblock. The scan is used to replay the * journal, to do garbage collection. for the TNC in-the-gaps method, and by * debugging functions.
*/
#include"ubifs.h"
/** * scan_padding_bytes - scan for padding bytes. * @buf: buffer to scan * @len: length of buffer * * This function returns the number of padding bytes on success and * %SCANNED_GARBAGE on failure.
*/ staticint scan_padding_bytes(void *buf, int len)
{ int pad_len = 0, max_pad_len = min_t(int, UBIFS_PAD_NODE_SZ, len);
uint8_t *p = buf;
if (!pad_len || (pad_len & 7)) return SCANNED_GARBAGE;
dbg_scan("%d padding bytes", pad_len);
return pad_len;
}
/** * ubifs_scan_a_node - scan for a node or padding. * @c: UBIFS file-system description object * @buf: buffer to scan * @len: length of buffer * @lnum: logical eraseblock number * @offs: offset within the logical eraseblock * @quiet: print no messages * * This function returns a scanning code to indicate what was scanned.
*/ int ubifs_scan_a_node(conststruct ubifs_info *c, void *buf, int len, int lnum, int offs, int quiet)
{ struct ubifs_ch *ch = buf;
uint32_t magic;
magic = le32_to_cpu(ch->magic);
if (magic == 0xFFFFFFFF) {
dbg_scan("hit empty space at LEB %d:%d", lnum, offs); return SCANNED_EMPTY_SPACE;
}
if (magic != UBIFS_NODE_MAGIC) return scan_padding_bytes(buf, len);
if (len < UBIFS_CH_SZ) return SCANNED_GARBAGE;
dbg_scan("scanning %s at LEB %d:%d",
dbg_ntype(ch->node_type), lnum, offs);
if (ubifs_check_node(c, buf, len, lnum, offs, quiet, 1)) return SCANNED_A_CORRUPT_NODE;
if (ch->node_type == UBIFS_PAD_NODE) { struct ubifs_pad_node *pad = buf; int pad_len = le32_to_cpu(pad->pad_len); int node_len = le32_to_cpu(ch->len);
/* Validate the padding node */ if (pad_len < 0 ||
offs + node_len + pad_len > c->leb_size) { if (!quiet) {
ubifs_err(c, "bad pad node at LEB %d:%d",
lnum, offs);
ubifs_dump_node(c, pad, len);
} return SCANNED_A_BAD_PAD_NODE;
}
/* Make the node pads to 8-byte boundary */ if ((node_len + pad_len) & 7) { if (!quiet)
ubifs_err(c, "bad padding length %d - %d",
offs, offs + node_len + pad_len); return SCANNED_A_BAD_PAD_NODE;
}
dbg_scan("%d bytes padded at LEB %d:%d, offset now %d", pad_len,
lnum, offs, ALIGN(offs + node_len + pad_len, 8));
return node_len + pad_len;
}
return SCANNED_A_NODE;
}
/** * ubifs_start_scan - create LEB scanning information at start of scan. * @c: UBIFS file-system description object * @lnum: logical eraseblock number * @offs: offset to start at (usually zero) * @sbuf: scan buffer (must be c->leb_size) * * This function returns the scanned information on success and a negative error * code on failure.
*/ struct ubifs_scan_leb *ubifs_start_scan(conststruct ubifs_info *c, int lnum, int offs, void *sbuf)
{ struct ubifs_scan_leb *sleb; int err;
dbg_scan("scan LEB %d:%d", lnum, offs);
sleb = kzalloc(sizeof(struct ubifs_scan_leb), GFP_NOFS); if (!sleb) return ERR_PTR(-ENOMEM);
switch (ch->node_type) { case UBIFS_INO_NODE: case UBIFS_DENT_NODE: case UBIFS_XENT_NODE: case UBIFS_DATA_NODE: /* * The key is in the same place in all keyed * nodes.
*/
key_read(c, &ino->key, &snod->key); break; default:
invalid_key_init(c, &snod->key); break;
}
list_add_tail(&snod->list, &sleb->nodes);
sleb->nodes_cnt += 1; return 0;
}
/** * ubifs_scanned_corruption - print information after UBIFS scanned corruption. * @c: UBIFS file-system description object * @lnum: LEB number of corruption * @offs: offset of corruption * @buf: buffer containing corruption
*/ void ubifs_scanned_corruption(conststruct ubifs_info *c, int lnum, int offs, void *buf)
{ int len;
ubifs_err(c, "corruption at LEB %d:%d", lnum, offs);
len = c->leb_size - offs; if (len > 8192)
len = 8192;
ubifs_err(c, "first %d bytes from LEB %d:%d", len, lnum, offs);
print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 4, buf, len, 1);
}
/** * ubifs_scan - scan a logical eraseblock. * @c: UBIFS file-system description object * @lnum: logical eraseblock number * @offs: offset to start at (usually zero) * @sbuf: scan buffer (must be of @c->leb_size bytes in size) * @quiet: print no messages * * This function scans LEB number @lnum and returns complete information about * its contents. Returns the scanned information in case of success and, * %-EUCLEAN if the LEB neads recovery, and other negative error codes in case * of failure. * * If @quiet is non-zero, this function does not print large and scary * error messages and flash dumps in case of errors.
*/ struct ubifs_scan_leb *ubifs_scan(conststruct ubifs_info *c, int lnum, int offs, void *sbuf, int quiet)
{ void *buf = sbuf + offs; int err, len = c->leb_size - offs; struct ubifs_scan_leb *sleb;
sleb = ubifs_start_scan(c, lnum, offs, sbuf); if (IS_ERR(sleb)) return sleb;
while (len >= 8) { struct ubifs_ch *ch = buf; int node_len, ret;
dbg_scan("look at LEB %d:%d (%d bytes left)",
lnum, offs, len);
cond_resched();
ret = ubifs_scan_a_node(c, buf, len, lnum, offs, quiet); if (ret > 0) { /* Padding bytes or a valid padding node */
offs += ret;
buf += ret;
len -= ret; continue;
}
if (ret == SCANNED_EMPTY_SPACE) /* Empty space is checked later */ break;
switch (ret) { case SCANNED_GARBAGE:
ubifs_err(c, "garbage"); goto corrupted; case SCANNED_A_NODE: break; case SCANNED_A_CORRUPT_NODE: case SCANNED_A_BAD_PAD_NODE:
ubifs_err(c, "bad node"); goto corrupted; default:
ubifs_err(c, "unknown");
err = -EINVAL; goto error;
}
err = ubifs_add_snod(c, sleb, buf, offs); if (err) goto error;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.