2 * This file is part of UBIFS.
4 * Copyright (C) 2006-2008 Nokia Corporation
6 * SPDX-License-Identifier: GPL-2.0+
8 * Authors: Adrian Hunter
9 * Artem Bityutskiy (Битюцкий Артём)
13 * This file implements the scan which is a general-purpose function for
14 * determining what nodes are in an eraseblock. The scan is used to replay the
15 * journal, to do garbage collection. for the TNC in-the-gaps method, and by
16 * debugging functions.
21 #include <linux/err.h>
26 * scan_padding_bytes - scan for padding bytes.
27 * @buf: buffer to scan
28 * @len: length of buffer
30 * This function returns the number of padding bytes on success and
31 * %SCANNED_GARBAGE on failure.
33 static int scan_padding_bytes(void *buf, int len)
35 int pad_len = 0, max_pad_len = min_t(int, UBIFS_PAD_NODE_SZ, len);
38 dbg_scan("not a node");
40 while (pad_len < max_pad_len && *p++ == UBIFS_PADDING_BYTE)
43 if (!pad_len || (pad_len & 7))
44 return SCANNED_GARBAGE;
46 dbg_scan("%d padding bytes", pad_len);
52 * ubifs_scan_a_node - scan for a node or padding.
53 * @c: UBIFS file-system description object
54 * @buf: buffer to scan
55 * @len: length of buffer
56 * @lnum: logical eraseblock number
57 * @offs: offset within the logical eraseblock
58 * @quiet: print no messages
60 * This function returns a scanning code to indicate what was scanned.
62 int ubifs_scan_a_node(const struct ubifs_info *c, void *buf, int len, int lnum,
65 struct ubifs_ch *ch = buf;
68 magic = le32_to_cpu(ch->magic);
70 if (magic == 0xFFFFFFFF) {
71 dbg_scan("hit empty space at LEB %d:%d", lnum, offs);
72 return SCANNED_EMPTY_SPACE;
75 if (magic != UBIFS_NODE_MAGIC)
76 return scan_padding_bytes(buf, len);
78 if (len < UBIFS_CH_SZ)
79 return SCANNED_GARBAGE;
81 dbg_scan("scanning %s at LEB %d:%d",
82 dbg_ntype(ch->node_type), lnum, offs);
84 if (ubifs_check_node(c, buf, lnum, offs, quiet, 1))
85 return SCANNED_A_CORRUPT_NODE;
87 if (ch->node_type == UBIFS_PAD_NODE) {
88 struct ubifs_pad_node *pad = buf;
89 int pad_len = le32_to_cpu(pad->pad_len);
90 int node_len = le32_to_cpu(ch->len);
92 /* Validate the padding node */
94 offs + node_len + pad_len > c->leb_size) {
96 ubifs_err("bad pad node at LEB %d:%d",
98 ubifs_dump_node(c, pad);
100 return SCANNED_A_BAD_PAD_NODE;
103 /* Make the node pads to 8-byte boundary */
104 if ((node_len + pad_len) & 7) {
106 ubifs_err("bad padding length %d - %d",
107 offs, offs + node_len + pad_len);
108 return SCANNED_A_BAD_PAD_NODE;
111 dbg_scan("%d bytes padded at LEB %d:%d, offset now %d", pad_len,
112 lnum, offs, ALIGN(offs + node_len + pad_len, 8));
114 return node_len + pad_len;
117 return SCANNED_A_NODE;
121 * ubifs_start_scan - create LEB scanning information at start of scan.
122 * @c: UBIFS file-system description object
123 * @lnum: logical eraseblock number
124 * @offs: offset to start at (usually zero)
125 * @sbuf: scan buffer (must be c->leb_size)
127 * This function returns %0 on success and a negative error code on failure.
129 struct ubifs_scan_leb *ubifs_start_scan(const struct ubifs_info *c, int lnum,
130 int offs, void *sbuf)
132 struct ubifs_scan_leb *sleb;
135 dbg_scan("scan LEB %d:%d", lnum, offs);
137 sleb = kzalloc(sizeof(struct ubifs_scan_leb), GFP_NOFS);
139 return ERR_PTR(-ENOMEM);
142 INIT_LIST_HEAD(&sleb->nodes);
145 err = ubifs_leb_read(c, lnum, sbuf + offs, offs, c->leb_size - offs, 0);
146 if (err && err != -EBADMSG) {
147 ubifs_err("cannot read %d bytes from LEB %d:%d, error %d",
148 c->leb_size - offs, lnum, offs, err);
160 * ubifs_end_scan - update LEB scanning information at end of scan.
161 * @c: UBIFS file-system description object
162 * @sleb: scanning information
163 * @lnum: logical eraseblock number
164 * @offs: offset to start at (usually zero)
166 * This function returns %0 on success and a negative error code on failure.
168 void ubifs_end_scan(const struct ubifs_info *c, struct ubifs_scan_leb *sleb,
172 dbg_scan("stop scanning LEB %d at offset %d", lnum, offs);
173 ubifs_assert(offs % c->min_io_size == 0);
175 sleb->endpt = ALIGN(offs, c->min_io_size);
179 * ubifs_add_snod - add a scanned node to LEB scanning information.
180 * @c: UBIFS file-system description object
181 * @sleb: scanning information
182 * @buf: buffer containing node
183 * @offs: offset of node on flash
185 * This function returns %0 on success and a negative error code on failure.
187 int ubifs_add_snod(const struct ubifs_info *c, struct ubifs_scan_leb *sleb,
190 struct ubifs_ch *ch = buf;
191 struct ubifs_ino_node *ino = buf;
192 struct ubifs_scan_node *snod;
194 snod = kmalloc(sizeof(struct ubifs_scan_node), GFP_NOFS);
198 snod->sqnum = le64_to_cpu(ch->sqnum);
199 snod->type = ch->node_type;
201 snod->len = le32_to_cpu(ch->len);
204 switch (ch->node_type) {
206 case UBIFS_DENT_NODE:
207 case UBIFS_XENT_NODE:
208 case UBIFS_DATA_NODE:
210 * The key is in the same place in all keyed
213 key_read(c, &ino->key, &snod->key);
216 invalid_key_init(c, &snod->key);
219 list_add_tail(&snod->list, &sleb->nodes);
220 sleb->nodes_cnt += 1;
225 * ubifs_scanned_corruption - print information after UBIFS scanned corruption.
226 * @c: UBIFS file-system description object
227 * @lnum: LEB number of corruption
228 * @offs: offset of corruption
229 * @buf: buffer containing corruption
231 void ubifs_scanned_corruption(const struct ubifs_info *c, int lnum, int offs,
236 ubifs_err("corruption at LEB %d:%d", lnum, offs);
237 len = c->leb_size - offs;
240 ubifs_err("first %d bytes from LEB %d:%d", len, lnum, offs);
241 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 4, buf, len, 1);
245 * ubifs_scan - scan a logical eraseblock.
246 * @c: UBIFS file-system description object
247 * @lnum: logical eraseblock number
248 * @offs: offset to start at (usually zero)
249 * @sbuf: scan buffer (must be of @c->leb_size bytes in size)
250 * @quiet: print no messages
252 * This function scans LEB number @lnum and returns complete information about
253 * its contents. Returns the scaned information in case of success and,
254 * %-EUCLEAN if the LEB neads recovery, and other negative error codes in case
257 * If @quiet is non-zero, this function does not print large and scary
258 * error messages and flash dumps in case of errors.
260 struct ubifs_scan_leb *ubifs_scan(const struct ubifs_info *c, int lnum,
261 int offs, void *sbuf, int quiet)
263 void *buf = sbuf + offs;
264 int err, len = c->leb_size - offs;
265 struct ubifs_scan_leb *sleb;
267 sleb = ubifs_start_scan(c, lnum, offs, sbuf);
272 struct ubifs_ch *ch = buf;
275 dbg_scan("look at LEB %d:%d (%d bytes left)",
280 ret = ubifs_scan_a_node(c, buf, len, lnum, offs, quiet);
282 /* Padding bytes or a valid padding node */
289 if (ret == SCANNED_EMPTY_SPACE)
290 /* Empty space is checked later */
294 case SCANNED_GARBAGE:
295 ubifs_err("garbage");
299 case SCANNED_A_CORRUPT_NODE:
300 case SCANNED_A_BAD_PAD_NODE:
301 ubifs_err("bad node");
304 ubifs_err("unknown");
309 err = ubifs_add_snod(c, sleb, buf, offs);
313 node_len = ALIGN(le32_to_cpu(ch->len), 8);
319 if (offs % c->min_io_size) {
321 ubifs_err("empty space starts at non-aligned offset %d",
326 ubifs_end_scan(c, sleb, lnum, offs);
328 for (; len > 4; offs += 4, buf = buf + 4, len -= 4)
329 if (*(uint32_t *)buf != 0xffffffff)
331 for (; len; offs++, buf++, len--)
332 if (*(uint8_t *)buf != 0xff) {
334 ubifs_err("corrupt empty space at LEB %d:%d",
343 ubifs_scanned_corruption(c, lnum, offs, buf);
344 ubifs_err("LEB %d scanning failed", lnum);
347 ubifs_scan_destroy(sleb);
351 ubifs_err("LEB %d scanning failed, error %d", lnum, err);
352 ubifs_scan_destroy(sleb);
357 * ubifs_scan_destroy - destroy LEB scanning information.
358 * @sleb: scanning information to free
360 void ubifs_scan_destroy(struct ubifs_scan_leb *sleb)
362 struct ubifs_scan_node *node;
363 struct list_head *head;
366 while (!list_empty(head)) {
367 node = list_entry(head->next, struct ubifs_scan_node, list);
368 list_del(&node->list);