blob: e5b7c591841b9a09e24aefd5261476ab60d83ca9 [file] [log] [blame]
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001/*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors: Adrian Hunter
20 * Artem Bityutskiy (Битюцкий Артём)
21 */
22
23/*
24 * This file implements the scan which is a general-purpose function for
25 * determining what nodes are in an eraseblock. The scan is used to replay the
26 * journal, to do garbage collection. for the TNC in-the-gaps method, and by
27 * debugging functions.
28 */
29
30#include "ubifs.h"
31
32/**
33 * scan_padding_bytes - scan for padding bytes.
34 * @buf: buffer to scan
35 * @len: length of buffer
36 *
37 * This function returns the number of padding bytes on success and
38 * %SCANNED_GARBAGE on failure.
39 */
40static int scan_padding_bytes(void *buf, int len)
41{
42 int pad_len = 0, max_pad_len = min_t(int, UBIFS_PAD_NODE_SZ, len);
43 uint8_t *p = buf;
44
45 dbg_scan("not a node");
46
47 while (pad_len < max_pad_len && *p++ == UBIFS_PADDING_BYTE)
48 pad_len += 1;
49
50 if (!pad_len || (pad_len & 7))
51 return SCANNED_GARBAGE;
52
53 dbg_scan("%d padding bytes", pad_len);
54
55 return pad_len;
56}
57
58/**
59 * ubifs_scan_a_node - scan for a node or padding.
60 * @c: UBIFS file-system description object
61 * @buf: buffer to scan
62 * @len: length of buffer
63 * @lnum: logical eraseblock number
64 * @offs: offset within the logical eraseblock
65 * @quiet: print no messages
66 *
67 * This function returns a scanning code to indicate what was scanned.
68 */
69int ubifs_scan_a_node(const struct ubifs_info *c, void *buf, int len, int lnum,
70 int offs, int quiet)
71{
72 struct ubifs_ch *ch = buf;
73 uint32_t magic;
74
75 magic = le32_to_cpu(ch->magic);
76
77 if (magic == 0xFFFFFFFF) {
Artem Bityutskiy4b3e58a2012-08-23 17:28:58 +030078 dbg_scan("hit empty space at LEB %d:%d", lnum, offs);
Artem Bityutskiy1e517642008-07-14 19:08:37 +030079 return SCANNED_EMPTY_SPACE;
80 }
81
82 if (magic != UBIFS_NODE_MAGIC)
83 return scan_padding_bytes(buf, len);
84
85 if (len < UBIFS_CH_SZ)
86 return SCANNED_GARBAGE;
87
Artem Bityutskiy79fda512012-08-27 13:34:09 +030088 dbg_scan("scanning %s at LEB %d:%d",
89 dbg_ntype(ch->node_type), lnum, offs);
Artem Bityutskiy1e517642008-07-14 19:08:37 +030090
Adrian Hunter2953e732008-09-04 16:26:00 +030091 if (ubifs_check_node(c, buf, lnum, offs, quiet, 1))
Artem Bityutskiy1e517642008-07-14 19:08:37 +030092 return SCANNED_A_CORRUPT_NODE;
93
94 if (ch->node_type == UBIFS_PAD_NODE) {
95 struct ubifs_pad_node *pad = buf;
96 int pad_len = le32_to_cpu(pad->pad_len);
97 int node_len = le32_to_cpu(ch->len);
98
99 /* Validate the padding node */
100 if (pad_len < 0 ||
101 offs + node_len + pad_len > c->leb_size) {
102 if (!quiet) {
103 ubifs_err("bad pad node at LEB %d:%d",
104 lnum, offs);
Artem Bityutskiyedf6be22012-05-16 19:15:56 +0300105 ubifs_dump_node(c, pad);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300106 }
107 return SCANNED_A_BAD_PAD_NODE;
108 }
109
110 /* Make the node pads to 8-byte boundary */
111 if ((node_len + pad_len) & 7) {
Artem Bityutskiy348709b2009-08-25 15:00:55 +0300112 if (!quiet)
Artem Bityutskiya6aae4d2012-05-16 20:11:23 +0300113 ubifs_err("bad padding length %d - %d",
114 offs, offs + node_len + pad_len);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300115 return SCANNED_A_BAD_PAD_NODE;
116 }
117
Artem Bityutskiy4b3e58a2012-08-23 17:28:58 +0300118 dbg_scan("%d bytes padded at LEB %d:%d, offset now %d", pad_len,
119 lnum, offs, ALIGN(offs + node_len + pad_len, 8));
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300120
121 return node_len + pad_len;
122 }
123
124 return SCANNED_A_NODE;
125}
126
127/**
128 * ubifs_start_scan - create LEB scanning information at start of scan.
129 * @c: UBIFS file-system description object
130 * @lnum: logical eraseblock number
131 * @offs: offset to start at (usually zero)
132 * @sbuf: scan buffer (must be c->leb_size)
133 *
134 * This function returns %0 on success and a negative error code on failure.
135 */
136struct ubifs_scan_leb *ubifs_start_scan(const struct ubifs_info *c, int lnum,
137 int offs, void *sbuf)
138{
139 struct ubifs_scan_leb *sleb;
140 int err;
141
142 dbg_scan("scan LEB %d:%d", lnum, offs);
143
144 sleb = kzalloc(sizeof(struct ubifs_scan_leb), GFP_NOFS);
145 if (!sleb)
146 return ERR_PTR(-ENOMEM);
147
148 sleb->lnum = lnum;
149 INIT_LIST_HEAD(&sleb->nodes);
150 sleb->buf = sbuf;
151
Artem Bityutskiyd3048202011-06-03 14:03:25 +0300152 err = ubifs_leb_read(c, lnum, sbuf + offs, offs, c->leb_size - offs, 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300153 if (err && err != -EBADMSG) {
Artem Bityutskiy79fda512012-08-27 13:34:09 +0300154 ubifs_err("cannot read %d bytes from LEB %d:%d, error %d",
155 c->leb_size - offs, lnum, offs, err);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300156 kfree(sleb);
157 return ERR_PTR(err);
158 }
159
hujianyangc7b5bb02014-06-24 11:46:36 +0800160 /*
161 * Note, we ignore integrity errors (EBASMSG) because all the nodes are
162 * protected by CRC checksums.
163 */
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300164 return sleb;
165}
166
167/**
168 * ubifs_end_scan - update LEB scanning information at end of scan.
169 * @c: UBIFS file-system description object
170 * @sleb: scanning information
171 * @lnum: logical eraseblock number
172 * @offs: offset to start at (usually zero)
173 *
174 * This function returns %0 on success and a negative error code on failure.
175 */
176void ubifs_end_scan(const struct ubifs_info *c, struct ubifs_scan_leb *sleb,
177 int lnum, int offs)
178{
179 lnum = lnum;
180 dbg_scan("stop scanning LEB %d at offset %d", lnum, offs);
181 ubifs_assert(offs % c->min_io_size == 0);
182
183 sleb->endpt = ALIGN(offs, c->min_io_size);
184}
185
186/**
187 * ubifs_add_snod - add a scanned node to LEB scanning information.
188 * @c: UBIFS file-system description object
189 * @sleb: scanning information
190 * @buf: buffer containing node
191 * @offs: offset of node on flash
192 *
193 * This function returns %0 on success and a negative error code on failure.
194 */
195int ubifs_add_snod(const struct ubifs_info *c, struct ubifs_scan_leb *sleb,
196 void *buf, int offs)
197{
198 struct ubifs_ch *ch = buf;
199 struct ubifs_ino_node *ino = buf;
200 struct ubifs_scan_node *snod;
201
Artem Bityutskiyba2f48f2010-08-22 07:10:12 +0300202 snod = kmalloc(sizeof(struct ubifs_scan_node), GFP_NOFS);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300203 if (!snod)
204 return -ENOMEM;
205
206 snod->sqnum = le64_to_cpu(ch->sqnum);
207 snod->type = ch->node_type;
208 snod->offs = offs;
209 snod->len = le32_to_cpu(ch->len);
210 snod->node = buf;
211
212 switch (ch->node_type) {
213 case UBIFS_INO_NODE:
214 case UBIFS_DENT_NODE:
215 case UBIFS_XENT_NODE:
216 case UBIFS_DATA_NODE:
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300217 /*
218 * The key is in the same place in all keyed
219 * nodes.
220 */
221 key_read(c, &ino->key, &snod->key);
222 break;
Artem Bityutskiyba2f48f2010-08-22 07:10:12 +0300223 default:
224 invalid_key_init(c, &snod->key);
225 break;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300226 }
227 list_add_tail(&snod->list, &sleb->nodes);
228 sleb->nodes_cnt += 1;
229 return 0;
230}
231
232/**
233 * ubifs_scanned_corruption - print information after UBIFS scanned corruption.
234 * @c: UBIFS file-system description object
235 * @lnum: LEB number of corruption
236 * @offs: offset of corruption
237 * @buf: buffer containing corruption
238 */
239void ubifs_scanned_corruption(const struct ubifs_info *c, int lnum, int offs,
240 void *buf)
241{
242 int len;
243
Artem Bityutskiy086b3642009-06-29 16:25:33 +0300244 ubifs_err("corruption at LEB %d:%d", lnum, offs);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300245 len = c->leb_size - offs;
Artem Bityutskiy086b3642009-06-29 16:25:33 +0300246 if (len > 8192)
247 len = 8192;
Artem Bityutskiya6aae4d2012-05-16 20:11:23 +0300248 ubifs_err("first %d bytes from LEB %d:%d", len, lnum, offs);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300249 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 4, buf, len, 1);
250}
251
252/**
253 * ubifs_scan - scan a logical eraseblock.
254 * @c: UBIFS file-system description object
255 * @lnum: logical eraseblock number
256 * @offs: offset to start at (usually zero)
Artem Bityutskiy348709b2009-08-25 15:00:55 +0300257 * @sbuf: scan buffer (must be of @c->leb_size bytes in size)
258 * @quiet: print no messages
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300259 *
260 * This function scans LEB number @lnum and returns complete information about
Artem Bityutskiyed43f2f2009-06-29 17:59:23 +0300261 * its contents. Returns the scaned information in case of success and,
262 * %-EUCLEAN if the LEB neads recovery, and other negative error codes in case
263 * of failure.
Artem Bityutskiy348709b2009-08-25 15:00:55 +0300264 *
265 * If @quiet is non-zero, this function does not print large and scary
266 * error messages and flash dumps in case of errors.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300267 */
268struct ubifs_scan_leb *ubifs_scan(const struct ubifs_info *c, int lnum,
Artem Bityutskiy348709b2009-08-25 15:00:55 +0300269 int offs, void *sbuf, int quiet)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300270{
271 void *buf = sbuf + offs;
272 int err, len = c->leb_size - offs;
273 struct ubifs_scan_leb *sleb;
274
275 sleb = ubifs_start_scan(c, lnum, offs, sbuf);
276 if (IS_ERR(sleb))
277 return sleb;
278
279 while (len >= 8) {
280 struct ubifs_ch *ch = buf;
281 int node_len, ret;
282
283 dbg_scan("look at LEB %d:%d (%d bytes left)",
284 lnum, offs, len);
285
286 cond_resched();
287
Artem Bityutskiy348709b2009-08-25 15:00:55 +0300288 ret = ubifs_scan_a_node(c, buf, len, lnum, offs, quiet);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300289 if (ret > 0) {
290 /* Padding bytes or a valid padding node */
291 offs += ret;
292 buf += ret;
293 len -= ret;
294 continue;
295 }
296
297 if (ret == SCANNED_EMPTY_SPACE)
298 /* Empty space is checked later */
299 break;
300
301 switch (ret) {
302 case SCANNED_GARBAGE:
Artem Bityutskiya6aae4d2012-05-16 20:11:23 +0300303 ubifs_err("garbage");
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300304 goto corrupted;
305 case SCANNED_A_NODE:
306 break;
307 case SCANNED_A_CORRUPT_NODE:
308 case SCANNED_A_BAD_PAD_NODE:
Artem Bityutskiya6aae4d2012-05-16 20:11:23 +0300309 ubifs_err("bad node");
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300310 goto corrupted;
311 default:
Artem Bityutskiya6aae4d2012-05-16 20:11:23 +0300312 ubifs_err("unknown");
Artem Bityutskiyed43f2f2009-06-29 17:59:23 +0300313 err = -EINVAL;
314 goto error;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300315 }
316
317 err = ubifs_add_snod(c, sleb, buf, offs);
318 if (err)
319 goto error;
320
321 node_len = ALIGN(le32_to_cpu(ch->len), 8);
322 offs += node_len;
323 buf += node_len;
324 len -= node_len;
325 }
326
Artem Bityutskiyed43f2f2009-06-29 17:59:23 +0300327 if (offs % c->min_io_size) {
Artem Bityutskiy348709b2009-08-25 15:00:55 +0300328 if (!quiet)
329 ubifs_err("empty space starts at non-aligned offset %d",
330 offs);
Artem Bityutskiy822ed642011-02-06 14:49:26 +0200331 goto corrupted;
Artem Bityutskiyed43f2f2009-06-29 17:59:23 +0300332 }
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300333
334 ubifs_end_scan(c, sleb, lnum, offs);
335
336 for (; len > 4; offs += 4, buf = buf + 4, len -= 4)
337 if (*(uint32_t *)buf != 0xffffffff)
338 break;
339 for (; len; offs++, buf++, len--)
340 if (*(uint8_t *)buf != 0xff) {
Artem Bityutskiy348709b2009-08-25 15:00:55 +0300341 if (!quiet)
342 ubifs_err("corrupt empty space at LEB %d:%d",
343 lnum, offs);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300344 goto corrupted;
345 }
346
347 return sleb;
348
349corrupted:
Artem Bityutskiy348709b2009-08-25 15:00:55 +0300350 if (!quiet) {
351 ubifs_scanned_corruption(c, lnum, offs, buf);
352 ubifs_err("LEB %d scanning failed", lnum);
353 }
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300354 err = -EUCLEAN;
Artem Bityutskiy348709b2009-08-25 15:00:55 +0300355 ubifs_scan_destroy(sleb);
356 return ERR_PTR(err);
357
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300358error:
Artem Bityutskiy348709b2009-08-25 15:00:55 +0300359 ubifs_err("LEB %d scanning failed, error %d", lnum, err);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300360 ubifs_scan_destroy(sleb);
361 return ERR_PTR(err);
362}
363
364/**
365 * ubifs_scan_destroy - destroy LEB scanning information.
366 * @sleb: scanning information to free
367 */
368void ubifs_scan_destroy(struct ubifs_scan_leb *sleb)
369{
370 struct ubifs_scan_node *node;
371 struct list_head *head;
372
373 head = &sleb->nodes;
374 while (!list_empty(head)) {
375 node = list_entry(head->next, struct ubifs_scan_node, list);
376 list_del(&node->list);
377 kfree(node);
378 }
379 kfree(sleb);
380}