xref: /openbmc/u-boot/fs/ubifs/scan.c (revision 47539e23)
1 /*
2  * This file is part of UBIFS.
3  *
4  * Copyright (C) 2006-2008 Nokia Corporation
5  *
6  * SPDX-License-Identifier:	GPL-2.0+
7  *
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 
19 #define __UBOOT__
20 #ifdef __UBOOT__
21 #include <linux/err.h>
22 #endif
23 #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  */
33 static 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  */
62 int 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) {
71 		dbg_scan("hit empty space at LEB %d:%d", lnum, offs);
72 		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 
81 	dbg_scan("scanning %s at LEB %d:%d",
82 		 dbg_ntype(ch->node_type), lnum, offs);
83 
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);
98 				ubifs_dump_node(c, pad);
99 			}
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) {
105 			if (!quiet)
106 				ubifs_err("bad padding length %d - %d",
107 					  offs, offs + node_len + pad_len);
108 			return SCANNED_A_BAD_PAD_NODE;
109 		}
110 
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));
113 
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  */
129 struct 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 
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);
149 		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  */
168 void 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  */
187 int 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 
194 	snod = kmalloc(sizeof(struct ubifs_scan_node), GFP_NOFS);
195 	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:
209 		/*
210 		 * The key is in the same place in all keyed
211 		 * nodes.
212 		 */
213 		key_read(c, &ino->key, &snod->key);
214 		break;
215 	default:
216 		invalid_key_init(c, &snod->key);
217 		break;
218 	}
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  */
231 void ubifs_scanned_corruption(const struct ubifs_info *c, int lnum, int offs,
232 			      void *buf)
233 {
234 	int len;
235 
236 	ubifs_err("corruption at LEB %d:%d", lnum, offs);
237 	len = c->leb_size - offs;
238 	if (len > 8192)
239 		len = 8192;
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);
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)
249  * @sbuf: scan buffer (must be of @c->leb_size bytes in size)
250  * @quiet: print no messages
251  *
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
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.
259  */
260 struct ubifs_scan_leb *ubifs_scan(const struct ubifs_info *c, int lnum,
261 				  int offs, void *sbuf, int quiet)
262 {
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 
280 		ret = ubifs_scan_a_node(c, buf, len, lnum, offs, quiet);
281 		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:
295 			ubifs_err("garbage");
296 			goto corrupted;
297 		case SCANNED_A_NODE:
298 			break;
299 		case SCANNED_A_CORRUPT_NODE:
300 		case SCANNED_A_BAD_PAD_NODE:
301 			ubifs_err("bad node");
302 			goto corrupted;
303 		default:
304 			ubifs_err("unknown");
305 			err = -EINVAL;
306 			goto error;
307 		}
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 
319 	if (offs % c->min_io_size) {
320 		if (!quiet)
321 			ubifs_err("empty space starts at non-aligned offset %d",
322 				  offs);
323 		goto corrupted;
324 	}
325 
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) {
333 			if (!quiet)
334 				ubifs_err("corrupt empty space at LEB %d:%d",
335 					  lnum, offs);
336 			goto corrupted;
337 		}
338 
339 	return sleb;
340 
341 corrupted:
342 	if (!quiet) {
343 		ubifs_scanned_corruption(c, lnum, offs, buf);
344 		ubifs_err("LEB %d scanning failed", lnum);
345 	}
346 	err = -EUCLEAN;
347 	ubifs_scan_destroy(sleb);
348 	return ERR_PTR(err);
349 
350 error:
351 	ubifs_err("LEB %d scanning failed, error %d", lnum, err);
352 	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  */
360 void 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 }
373