md: Fix unfortunate interaction with evms
[linux-2.6/mini2440.git] / fs / ubifs / scan.c
blob892ebfee4fe538fe033596c5d42d13860d5fe82a
1 /*
2 * This file is part of UBIFS.
4 * Copyright (C) 2006-2008 Nokia Corporation
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.
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.
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
19 * Authors: Adrian Hunter
20 * Artem Bityutskiy (Битюцкий Артём)
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.
30 #include "ubifs.h"
32 /**
33 * scan_padding_bytes - scan for padding bytes.
34 * @buf: buffer to scan
35 * @len: length of buffer
37 * This function returns the number of padding bytes on success and
38 * %SCANNED_GARBAGE on failure.
40 static int scan_padding_bytes(void *buf, int len)
42 int pad_len = 0, max_pad_len = min_t(int, UBIFS_PAD_NODE_SZ, len);
43 uint8_t *p = buf;
45 dbg_scan("not a node");
47 while (pad_len < max_pad_len && *p++ == UBIFS_PADDING_BYTE)
48 pad_len += 1;
50 if (!pad_len || (pad_len & 7))
51 return SCANNED_GARBAGE;
53 dbg_scan("%d padding bytes", pad_len);
55 return pad_len;
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
67 * This function returns a scanning code to indicate what was scanned.
69 int ubifs_scan_a_node(const struct ubifs_info *c, void *buf, int len, int lnum,
70 int offs, int quiet)
72 struct ubifs_ch *ch = buf;
73 uint32_t magic;
75 magic = le32_to_cpu(ch->magic);
77 if (magic == 0xFFFFFFFF) {
78 dbg_scan("hit empty space");
79 return SCANNED_EMPTY_SPACE;
82 if (magic != UBIFS_NODE_MAGIC)
83 return scan_padding_bytes(buf, len);
85 if (len < UBIFS_CH_SZ)
86 return SCANNED_GARBAGE;
88 dbg_scan("scanning %s", dbg_ntype(ch->node_type));
90 if (ubifs_check_node(c, buf, lnum, offs, quiet, 1))
91 return SCANNED_A_CORRUPT_NODE;
93 if (ch->node_type == UBIFS_PAD_NODE) {
94 struct ubifs_pad_node *pad = buf;
95 int pad_len = le32_to_cpu(pad->pad_len);
96 int node_len = le32_to_cpu(ch->len);
98 /* Validate the padding node */
99 if (pad_len < 0 ||
100 offs + node_len + pad_len > c->leb_size) {
101 if (!quiet) {
102 ubifs_err("bad pad node at LEB %d:%d",
103 lnum, offs);
104 dbg_dump_node(c, pad);
106 return SCANNED_A_BAD_PAD_NODE;
109 /* Make the node pads to 8-byte boundary */
110 if ((node_len + pad_len) & 7) {
111 if (!quiet) {
112 dbg_err("bad padding length %d - %d",
113 offs, offs + node_len + pad_len);
115 return SCANNED_A_BAD_PAD_NODE;
118 dbg_scan("%d bytes padded, offset now %d",
119 pad_len, ALIGN(offs + node_len + pad_len, 8));
121 return node_len + pad_len;
124 return SCANNED_A_NODE;
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)
134 * This function returns %0 on success and a negative error code on failure.
136 struct ubifs_scan_leb *ubifs_start_scan(const struct ubifs_info *c, int lnum,
137 int offs, void *sbuf)
139 struct ubifs_scan_leb *sleb;
140 int err;
142 dbg_scan("scan LEB %d:%d", lnum, offs);
144 sleb = kzalloc(sizeof(struct ubifs_scan_leb), GFP_NOFS);
145 if (!sleb)
146 return ERR_PTR(-ENOMEM);
148 sleb->lnum = lnum;
149 INIT_LIST_HEAD(&sleb->nodes);
150 sleb->buf = sbuf;
152 err = ubi_read(c->ubi, lnum, sbuf + offs, offs, c->leb_size - offs);
153 if (err && err != -EBADMSG) {
154 ubifs_err("cannot read %d bytes from LEB %d:%d,"
155 " error %d", c->leb_size - offs, lnum, offs, err);
156 kfree(sleb);
157 return ERR_PTR(err);
160 if (err == -EBADMSG)
161 sleb->ecc = 1;
163 return sleb;
167 * ubifs_end_scan - update LEB scanning information at end of scan.
168 * @c: UBIFS file-system description object
169 * @sleb: scanning information
170 * @lnum: logical eraseblock number
171 * @offs: offset to start at (usually zero)
173 * This function returns %0 on success and a negative error code on failure.
175 void ubifs_end_scan(const struct ubifs_info *c, struct ubifs_scan_leb *sleb,
176 int lnum, int offs)
178 lnum = lnum;
179 dbg_scan("stop scanning LEB %d at offset %d", lnum, offs);
180 ubifs_assert(offs % c->min_io_size == 0);
182 sleb->endpt = ALIGN(offs, c->min_io_size);
186 * ubifs_add_snod - add a scanned node to LEB scanning information.
187 * @c: UBIFS file-system description object
188 * @sleb: scanning information
189 * @buf: buffer containing node
190 * @offs: offset of node on flash
192 * This function returns %0 on success and a negative error code on failure.
194 int ubifs_add_snod(const struct ubifs_info *c, struct ubifs_scan_leb *sleb,
195 void *buf, int offs)
197 struct ubifs_ch *ch = buf;
198 struct ubifs_ino_node *ino = buf;
199 struct ubifs_scan_node *snod;
201 snod = kzalloc(sizeof(struct ubifs_scan_node), GFP_NOFS);
202 if (!snod)
203 return -ENOMEM;
205 snod->sqnum = le64_to_cpu(ch->sqnum);
206 snod->type = ch->node_type;
207 snod->offs = offs;
208 snod->len = le32_to_cpu(ch->len);
209 snod->node = buf;
211 switch (ch->node_type) {
212 case UBIFS_INO_NODE:
213 case UBIFS_DENT_NODE:
214 case UBIFS_XENT_NODE:
215 case UBIFS_DATA_NODE:
216 case UBIFS_TRUN_NODE:
218 * The key is in the same place in all keyed
219 * nodes.
221 key_read(c, &ino->key, &snod->key);
222 break;
224 list_add_tail(&snod->list, &sleb->nodes);
225 sleb->nodes_cnt += 1;
226 return 0;
230 * ubifs_scanned_corruption - print information after UBIFS scanned corruption.
231 * @c: UBIFS file-system description object
232 * @lnum: LEB number of corruption
233 * @offs: offset of corruption
234 * @buf: buffer containing corruption
236 void ubifs_scanned_corruption(const struct ubifs_info *c, int lnum, int offs,
237 void *buf)
239 int len;
241 ubifs_err("corruption at LEB %d:%d", lnum, offs);
242 if (dbg_failure_mode)
243 return;
244 len = c->leb_size - offs;
245 if (len > 8192)
246 len = 8192;
247 dbg_err("first %d bytes from LEB %d:%d", len, lnum, offs);
248 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 4, buf, len, 1);
252 * ubifs_scan - scan a logical eraseblock.
253 * @c: UBIFS file-system description object
254 * @lnum: logical eraseblock number
255 * @offs: offset to start at (usually zero)
256 * @sbuf: scan buffer (must be c->leb_size)
258 * This function scans LEB number @lnum and returns complete information about
259 * its contents. Returns the scaned information in case of success and,
260 * %-EUCLEAN if the LEB neads recovery, and other negative error codes in case
261 * of failure.
263 struct ubifs_scan_leb *ubifs_scan(const struct ubifs_info *c, int lnum,
264 int offs, void *sbuf)
266 void *buf = sbuf + offs;
267 int err, len = c->leb_size - offs;
268 struct ubifs_scan_leb *sleb;
270 sleb = ubifs_start_scan(c, lnum, offs, sbuf);
271 if (IS_ERR(sleb))
272 return sleb;
274 while (len >= 8) {
275 struct ubifs_ch *ch = buf;
276 int node_len, ret;
278 dbg_scan("look at LEB %d:%d (%d bytes left)",
279 lnum, offs, len);
281 cond_resched();
283 ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 0);
284 if (ret > 0) {
285 /* Padding bytes or a valid padding node */
286 offs += ret;
287 buf += ret;
288 len -= ret;
289 continue;
292 if (ret == SCANNED_EMPTY_SPACE)
293 /* Empty space is checked later */
294 break;
296 switch (ret) {
297 case SCANNED_GARBAGE:
298 dbg_err("garbage");
299 goto corrupted;
300 case SCANNED_A_NODE:
301 break;
302 case SCANNED_A_CORRUPT_NODE:
303 case SCANNED_A_BAD_PAD_NODE:
304 dbg_err("bad node");
305 goto corrupted;
306 default:
307 dbg_err("unknown");
308 err = -EINVAL;
309 goto error;
312 err = ubifs_add_snod(c, sleb, buf, offs);
313 if (err)
314 goto error;
316 node_len = ALIGN(le32_to_cpu(ch->len), 8);
317 offs += node_len;
318 buf += node_len;
319 len -= node_len;
322 if (offs % c->min_io_size) {
323 ubifs_err("empty space starts at non-aligned offset %d", offs);
324 goto corrupted;;
327 ubifs_end_scan(c, sleb, lnum, offs);
329 for (; len > 4; offs += 4, buf = buf + 4, len -= 4)
330 if (*(uint32_t *)buf != 0xffffffff)
331 break;
332 for (; len; offs++, buf++, len--)
333 if (*(uint8_t *)buf != 0xff) {
334 ubifs_err("corrupt empty space at LEB %d:%d",
335 lnum, offs);
336 goto corrupted;
339 return sleb;
341 corrupted:
342 ubifs_scanned_corruption(c, lnum, offs, buf);
343 err = -EUCLEAN;
344 error:
345 ubifs_err("LEB %d scanning failed", lnum);
346 ubifs_scan_destroy(sleb);
347 return ERR_PTR(err);
351 * ubifs_scan_destroy - destroy LEB scanning information.
352 * @sleb: scanning information to free
354 void ubifs_scan_destroy(struct ubifs_scan_leb *sleb)
356 struct ubifs_scan_node *node;
357 struct list_head *head;
359 head = &sleb->nodes;
360 while (!list_empty(head)) {
361 node = list_entry(head->next, struct ubifs_scan_node, list);
362 list_del(&node->list);
363 kfree(node);
365 kfree(sleb);