blob: 876a6ee6616b0eed2d7ff3d7371a528ed9fbf715 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stefan Roese2fc10f62009-03-19 15:35:05 +01002/*
3 * This file is part of UBIFS.
4 *
5 * Copyright (C) 2006-2008 Nokia Corporation
6 *
Stefan Roese2fc10f62009-03-19 15:35:05 +01007 * Authors: Adrian Hunter
8 * Artem Bityutskiy (Битюцкий Артём)
9 */
10
11/*
12 * This file implements the scan which is a general-purpose function for
13 * determining what nodes are in an eraseblock. The scan is used to replay the
14 * journal, to do garbage collection. for the TNC in-the-gaps method, and by
15 * debugging functions.
16 */
17
Heiko Schocherf5895d12014-06-24 10:10:04 +020018#ifdef __UBOOT__
Alexey Brodkin2d2fa492018-06-05 17:17:57 +030019#include <hexdump.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070020#include <dm/devres.h>
Heiko Schocherf5895d12014-06-24 10:10:04 +020021#include <linux/err.h>
22#endif
Stefan Roese2fc10f62009-03-19 15:35:05 +010023#include "ubifs.h"
24
25/**
26 * scan_padding_bytes - scan for padding bytes.
27 * @buf: buffer to scan
28 * @len: length of buffer
29 *
30 * This function returns the number of padding bytes on success and
31 * %SCANNED_GARBAGE on failure.
32 */
33static int scan_padding_bytes(void *buf, int len)
34{
35 int pad_len = 0, max_pad_len = min_t(int, UBIFS_PAD_NODE_SZ, len);
36 uint8_t *p = buf;
37
38 dbg_scan("not a node");
39
40 while (pad_len < max_pad_len && *p++ == UBIFS_PADDING_BYTE)
41 pad_len += 1;
42
43 if (!pad_len || (pad_len & 7))
44 return SCANNED_GARBAGE;
45
46 dbg_scan("%d padding bytes", pad_len);
47
48 return pad_len;
49}
50
51/**
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
59 *
60 * This function returns a scanning code to indicate what was scanned.
61 */
62int ubifs_scan_a_node(const struct ubifs_info *c, void *buf, int len, int lnum,
63 int offs, int quiet)
64{
65 struct ubifs_ch *ch = buf;
66 uint32_t magic;
67
68 magic = le32_to_cpu(ch->magic);
69
70 if (magic == 0xFFFFFFFF) {
Heiko Schocherf5895d12014-06-24 10:10:04 +020071 dbg_scan("hit empty space at LEB %d:%d", lnum, offs);
Stefan Roese2fc10f62009-03-19 15:35:05 +010072 return SCANNED_EMPTY_SPACE;
73 }
74
75 if (magic != UBIFS_NODE_MAGIC)
76 return scan_padding_bytes(buf, len);
77
78 if (len < UBIFS_CH_SZ)
79 return SCANNED_GARBAGE;
80
Heiko Schocherf5895d12014-06-24 10:10:04 +020081 dbg_scan("scanning %s at LEB %d:%d",
82 dbg_ntype(ch->node_type), lnum, offs);
Stefan Roese2fc10f62009-03-19 15:35:05 +010083
84 if (ubifs_check_node(c, buf, lnum, offs, quiet, 1))
85 return SCANNED_A_CORRUPT_NODE;
86
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);
91
92 /* Validate the padding node */
93 if (pad_len < 0 ||
94 offs + node_len + pad_len > c->leb_size) {
95 if (!quiet) {
Heiko Schocher94b66de2015-10-22 06:19:21 +020096 ubifs_err(c, "bad pad node at LEB %d:%d",
Stefan Roese2fc10f62009-03-19 15:35:05 +010097 lnum, offs);
Heiko Schocherf5895d12014-06-24 10:10:04 +020098 ubifs_dump_node(c, pad);
Stefan Roese2fc10f62009-03-19 15:35:05 +010099 }
100 return SCANNED_A_BAD_PAD_NODE;
101 }
102
103 /* Make the node pads to 8-byte boundary */
104 if ((node_len + pad_len) & 7) {
Heiko Schocherf5895d12014-06-24 10:10:04 +0200105 if (!quiet)
Heiko Schocher94b66de2015-10-22 06:19:21 +0200106 ubifs_err(c, "bad padding length %d - %d",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200107 offs, offs + node_len + pad_len);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100108 return SCANNED_A_BAD_PAD_NODE;
109 }
110
Heiko Schocherf5895d12014-06-24 10:10:04 +0200111 dbg_scan("%d bytes padded at LEB %d:%d, offset now %d", pad_len,
112 lnum, offs, ALIGN(offs + node_len + pad_len, 8));
Stefan Roese2fc10f62009-03-19 15:35:05 +0100113
114 return node_len + pad_len;
115 }
116
117 return SCANNED_A_NODE;
118}
119
120/**
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)
126 *
Heiko Schocher94b66de2015-10-22 06:19:21 +0200127 * This function returns the scanned information on success and a negative error
128 * code on failure.
Stefan Roese2fc10f62009-03-19 15:35:05 +0100129 */
130struct ubifs_scan_leb *ubifs_start_scan(const struct ubifs_info *c, int lnum,
131 int offs, void *sbuf)
132{
133 struct ubifs_scan_leb *sleb;
134 int err;
135
136 dbg_scan("scan LEB %d:%d", lnum, offs);
137
138 sleb = kzalloc(sizeof(struct ubifs_scan_leb), GFP_NOFS);
139 if (!sleb)
140 return ERR_PTR(-ENOMEM);
141
142 sleb->lnum = lnum;
143 INIT_LIST_HEAD(&sleb->nodes);
144 sleb->buf = sbuf;
145
Heiko Schocherf5895d12014-06-24 10:10:04 +0200146 err = ubifs_leb_read(c, lnum, sbuf + offs, offs, c->leb_size - offs, 0);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100147 if (err && err != -EBADMSG) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200148 ubifs_err(c, "cannot read %d bytes from LEB %d:%d, error %d",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200149 c->leb_size - offs, lnum, offs, err);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100150 kfree(sleb);
151 return ERR_PTR(err);
152 }
153
Heiko Schocher94b66de2015-10-22 06:19:21 +0200154 /*
155 * Note, we ignore integrity errors (EBASMSG) because all the nodes are
156 * protected by CRC checksums.
157 */
Stefan Roese2fc10f62009-03-19 15:35:05 +0100158 return sleb;
159}
160
161/**
162 * ubifs_end_scan - update LEB scanning information at end of scan.
163 * @c: UBIFS file-system description object
164 * @sleb: scanning information
165 * @lnum: logical eraseblock number
166 * @offs: offset to start at (usually zero)
Stefan Roese2fc10f62009-03-19 15:35:05 +0100167 */
168void ubifs_end_scan(const struct ubifs_info *c, struct ubifs_scan_leb *sleb,
169 int lnum, int offs)
170{
171 lnum = lnum;
172 dbg_scan("stop scanning LEB %d at offset %d", lnum, offs);
173 ubifs_assert(offs % c->min_io_size == 0);
174
175 sleb->endpt = ALIGN(offs, c->min_io_size);
176}
177
178/**
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
184 *
185 * This function returns %0 on success and a negative error code on failure.
186 */
187int ubifs_add_snod(const struct ubifs_info *c, struct ubifs_scan_leb *sleb,
188 void *buf, int offs)
189{
190 struct ubifs_ch *ch = buf;
191 struct ubifs_ino_node *ino = buf;
192 struct ubifs_scan_node *snod;
193
Heiko Schocherf5895d12014-06-24 10:10:04 +0200194 snod = kmalloc(sizeof(struct ubifs_scan_node), GFP_NOFS);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100195 if (!snod)
196 return -ENOMEM;
197
198 snod->sqnum = le64_to_cpu(ch->sqnum);
199 snod->type = ch->node_type;
200 snod->offs = offs;
201 snod->len = le32_to_cpu(ch->len);
202 snod->node = buf;
203
204 switch (ch->node_type) {
205 case UBIFS_INO_NODE:
206 case UBIFS_DENT_NODE:
207 case UBIFS_XENT_NODE:
208 case UBIFS_DATA_NODE:
Stefan Roese2fc10f62009-03-19 15:35:05 +0100209 /*
210 * The key is in the same place in all keyed
211 * nodes.
212 */
213 key_read(c, &ino->key, &snod->key);
214 break;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200215 default:
216 invalid_key_init(c, &snod->key);
217 break;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100218 }
219 list_add_tail(&snod->list, &sleb->nodes);
220 sleb->nodes_cnt += 1;
221 return 0;
222}
223
224/**
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
230 */
231void ubifs_scanned_corruption(const struct ubifs_info *c, int lnum, int offs,
232 void *buf)
233{
234 int len;
235
Heiko Schocher94b66de2015-10-22 06:19:21 +0200236 ubifs_err(c, "corruption at LEB %d:%d", lnum, offs);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100237 len = c->leb_size - offs;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200238 if (len > 8192)
239 len = 8192;
Heiko Schocher94b66de2015-10-22 06:19:21 +0200240 ubifs_err(c, "first %d bytes from LEB %d:%d", len, lnum, offs);
Alexey Brodkin2d2fa492018-06-05 17:17:57 +0300241 print_hex_dump("", DUMP_PREFIX_OFFSET, 32, 4, buf, len, 1);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100242}
243
244/**
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)
Heiko Schocherf5895d12014-06-24 10:10:04 +0200249 * @sbuf: scan buffer (must be of @c->leb_size bytes in size)
250 * @quiet: print no messages
Stefan Roese2fc10f62009-03-19 15:35:05 +0100251 *
252 * This function scans LEB number @lnum and returns complete information about
Heiko Schocher94b66de2015-10-22 06:19:21 +0200253 * its contents. Returns the scanned information in case of success and,
Heiko Schocherf5895d12014-06-24 10:10:04 +0200254 * %-EUCLEAN if the LEB neads recovery, and other negative error codes in case
255 * of failure.
256 *
257 * If @quiet is non-zero, this function does not print large and scary
258 * error messages and flash dumps in case of errors.
Stefan Roese2fc10f62009-03-19 15:35:05 +0100259 */
260struct ubifs_scan_leb *ubifs_scan(const struct ubifs_info *c, int lnum,
Heiko Schocherf5895d12014-06-24 10:10:04 +0200261 int offs, void *sbuf, int quiet)
Stefan Roese2fc10f62009-03-19 15:35:05 +0100262{
263 void *buf = sbuf + offs;
264 int err, len = c->leb_size - offs;
265 struct ubifs_scan_leb *sleb;
266
267 sleb = ubifs_start_scan(c, lnum, offs, sbuf);
268 if (IS_ERR(sleb))
269 return sleb;
270
271 while (len >= 8) {
272 struct ubifs_ch *ch = buf;
273 int node_len, ret;
274
275 dbg_scan("look at LEB %d:%d (%d bytes left)",
276 lnum, offs, len);
277
278 cond_resched();
279
Heiko Schocherf5895d12014-06-24 10:10:04 +0200280 ret = ubifs_scan_a_node(c, buf, len, lnum, offs, quiet);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100281 if (ret > 0) {
282 /* Padding bytes or a valid padding node */
283 offs += ret;
284 buf += ret;
285 len -= ret;
286 continue;
287 }
288
289 if (ret == SCANNED_EMPTY_SPACE)
290 /* Empty space is checked later */
291 break;
292
293 switch (ret) {
294 case SCANNED_GARBAGE:
Heiko Schocher94b66de2015-10-22 06:19:21 +0200295 ubifs_err(c, "garbage");
Stefan Roese2fc10f62009-03-19 15:35:05 +0100296 goto corrupted;
297 case SCANNED_A_NODE:
298 break;
299 case SCANNED_A_CORRUPT_NODE:
300 case SCANNED_A_BAD_PAD_NODE:
Heiko Schocher94b66de2015-10-22 06:19:21 +0200301 ubifs_err(c, "bad node");
Stefan Roese2fc10f62009-03-19 15:35:05 +0100302 goto corrupted;
303 default:
Heiko Schocher94b66de2015-10-22 06:19:21 +0200304 ubifs_err(c, "unknown");
Heiko Schocherf5895d12014-06-24 10:10:04 +0200305 err = -EINVAL;
306 goto error;
Stefan Roese2fc10f62009-03-19 15:35:05 +0100307 }
308
309 err = ubifs_add_snod(c, sleb, buf, offs);
310 if (err)
311 goto error;
312
313 node_len = ALIGN(le32_to_cpu(ch->len), 8);
314 offs += node_len;
315 buf += node_len;
316 len -= node_len;
317 }
318
Heiko Schocherf5895d12014-06-24 10:10:04 +0200319 if (offs % c->min_io_size) {
320 if (!quiet)
Heiko Schocher94b66de2015-10-22 06:19:21 +0200321 ubifs_err(c, "empty space starts at non-aligned offset %d",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200322 offs);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100323 goto corrupted;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200324 }
Stefan Roese2fc10f62009-03-19 15:35:05 +0100325
326 ubifs_end_scan(c, sleb, lnum, offs);
327
328 for (; len > 4; offs += 4, buf = buf + 4, len -= 4)
329 if (*(uint32_t *)buf != 0xffffffff)
330 break;
331 for (; len; offs++, buf++, len--)
332 if (*(uint8_t *)buf != 0xff) {
Heiko Schocherf5895d12014-06-24 10:10:04 +0200333 if (!quiet)
Heiko Schocher94b66de2015-10-22 06:19:21 +0200334 ubifs_err(c, "corrupt empty space at LEB %d:%d",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200335 lnum, offs);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100336 goto corrupted;
337 }
338
339 return sleb;
340
341corrupted:
Heiko Schocherf5895d12014-06-24 10:10:04 +0200342 if (!quiet) {
343 ubifs_scanned_corruption(c, lnum, offs, buf);
Heiko Schocher94b66de2015-10-22 06:19:21 +0200344 ubifs_err(c, "LEB %d scanning failed", lnum);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200345 }
Stefan Roese2fc10f62009-03-19 15:35:05 +0100346 err = -EUCLEAN;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200347 ubifs_scan_destroy(sleb);
348 return ERR_PTR(err);
349
Stefan Roese2fc10f62009-03-19 15:35:05 +0100350error:
Heiko Schocher94b66de2015-10-22 06:19:21 +0200351 ubifs_err(c, "LEB %d scanning failed, error %d", lnum, err);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100352 ubifs_scan_destroy(sleb);
353 return ERR_PTR(err);
354}
355
356/**
357 * ubifs_scan_destroy - destroy LEB scanning information.
358 * @sleb: scanning information to free
359 */
360void ubifs_scan_destroy(struct ubifs_scan_leb *sleb)
361{
362 struct ubifs_scan_node *node;
363 struct list_head *head;
364
365 head = &sleb->nodes;
366 while (!list_empty(head)) {
367 node = list_entry(head->next, struct ubifs_scan_node, list);
368 list_del(&node->list);
369 kfree(node);
370 }
371 kfree(sleb);
372}