blob: 5523d4e6608f39d16e98641eb584128fc67a6e09 [file] [log] [blame]
Stefan Roese2fc10f62009-03-19 15:35:05 +01001/*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation
5 *
Heiko Schocherf5895d12014-06-24 10:10:04 +02006 * SPDX-License-Identifier: GPL-2.0+
Stefan Roese2fc10f62009-03-19 15:35:05 +01007 *
8 * Authors: Adrian Hunter
9 * Artem Bityutskiy (Битюцкий Артём)
10 */
11
12/*
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.
17 */
18
Heiko Schocherf5895d12014-06-24 10:10:04 +020019#define __UBOOT__
20#ifdef __UBOOT__
21#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) {
96 ubifs_err("bad pad node at LEB %d:%d",
97 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)
106 ubifs_err("bad padding length %d - %d",
107 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 *
127 * This function returns %0 on success and a negative error code on failure.
128 */
129struct ubifs_scan_leb *ubifs_start_scan(const struct ubifs_info *c, int lnum,
130 int offs, void *sbuf)
131{
132 struct ubifs_scan_leb *sleb;
133 int err;
134
135 dbg_scan("scan LEB %d:%d", lnum, offs);
136
137 sleb = kzalloc(sizeof(struct ubifs_scan_leb), GFP_NOFS);
138 if (!sleb)
139 return ERR_PTR(-ENOMEM);
140
141 sleb->lnum = lnum;
142 INIT_LIST_HEAD(&sleb->nodes);
143 sleb->buf = sbuf;
144
Heiko Schocherf5895d12014-06-24 10:10:04 +0200145 err = ubifs_leb_read(c, lnum, sbuf + offs, offs, c->leb_size - offs, 0);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100146 if (err && err != -EBADMSG) {
Heiko Schocherf5895d12014-06-24 10:10:04 +0200147 ubifs_err("cannot read %d bytes from LEB %d:%d, error %d",
148 c->leb_size - offs, lnum, offs, err);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100149 kfree(sleb);
150 return ERR_PTR(err);
151 }
152
153 if (err == -EBADMSG)
154 sleb->ecc = 1;
155
156 return sleb;
157}
158
159/**
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)
165 *
166 * This function returns %0 on success and a negative error code on failure.
167 */
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 Schocherf5895d12014-06-24 10:10:04 +0200236 ubifs_err("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;
240 ubifs_err("first %d bytes from LEB %d:%d", len, lnum, offs);
Stefan Roese2fc10f62009-03-19 15:35:05 +0100241 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 4, buf, len, 1);
242}
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 Schocherf5895d12014-06-24 10:10:04 +0200253 * 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
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 Schocherf5895d12014-06-24 10:10:04 +0200295 ubifs_err("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 Schocherf5895d12014-06-24 10:10:04 +0200301 ubifs_err("bad node");
Stefan Roese2fc10f62009-03-19 15:35:05 +0100302 goto corrupted;
303 default:
Heiko Schocherf5895d12014-06-24 10:10:04 +0200304 ubifs_err("unknown");
305 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)
321 ubifs_err("empty space starts at non-aligned offset %d",
322 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)
334 ubifs_err("corrupt empty space at LEB %d:%d",
335 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);
344 ubifs_err("LEB %d scanning failed", lnum);
345 }
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 Schocherf5895d12014-06-24 10:10:04 +0200351 ubifs_err("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}