]>
git.ipfire.org Git - people/ms/u-boot.git/blob - fs/ubifs/scan.c
2 * This file is part of UBIFS.
4 * Copyright (C) 2006-2008 Nokia Corporation
6 * SPDX-License-Identifier: GPL-2.0+
8 * Authors: Adrian Hunter
9 * Artem Bityutskiy (Битюцкий Артём)
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.
20 #include <linux/err.h>
25 * scan_padding_bytes - scan for padding bytes.
26 * @buf: buffer to scan
27 * @len: length of buffer
29 * This function returns the number of padding bytes on success and
30 * %SCANNED_GARBAGE on failure.
32 static int scan_padding_bytes(void *buf
, int len
)
34 int pad_len
= 0, max_pad_len
= min_t(int, UBIFS_PAD_NODE_SZ
, len
);
37 dbg_scan("not a node");
39 while (pad_len
< max_pad_len
&& *p
++ == UBIFS_PADDING_BYTE
)
42 if (!pad_len
|| (pad_len
& 7))
43 return SCANNED_GARBAGE
;
45 dbg_scan("%d padding bytes", pad_len
);
51 * ubifs_scan_a_node - scan for a node or padding.
52 * @c: UBIFS file-system description object
53 * @buf: buffer to scan
54 * @len: length of buffer
55 * @lnum: logical eraseblock number
56 * @offs: offset within the logical eraseblock
57 * @quiet: print no messages
59 * This function returns a scanning code to indicate what was scanned.
61 int ubifs_scan_a_node(const struct ubifs_info
*c
, void *buf
, int len
, int lnum
,
64 struct ubifs_ch
*ch
= buf
;
67 magic
= le32_to_cpu(ch
->magic
);
69 if (magic
== 0xFFFFFFFF) {
70 dbg_scan("hit empty space at LEB %d:%d", lnum
, offs
);
71 return SCANNED_EMPTY_SPACE
;
74 if (magic
!= UBIFS_NODE_MAGIC
)
75 return scan_padding_bytes(buf
, len
);
77 if (len
< UBIFS_CH_SZ
)
78 return SCANNED_GARBAGE
;
80 dbg_scan("scanning %s at LEB %d:%d",
81 dbg_ntype(ch
->node_type
), lnum
, offs
);
83 if (ubifs_check_node(c
, buf
, lnum
, offs
, quiet
, 1))
84 return SCANNED_A_CORRUPT_NODE
;
86 if (ch
->node_type
== UBIFS_PAD_NODE
) {
87 struct ubifs_pad_node
*pad
= buf
;
88 int pad_len
= le32_to_cpu(pad
->pad_len
);
89 int node_len
= le32_to_cpu(ch
->len
);
91 /* Validate the padding node */
93 offs
+ node_len
+ pad_len
> c
->leb_size
) {
95 ubifs_err(c
, "bad pad node at LEB %d:%d",
97 ubifs_dump_node(c
, pad
);
99 return SCANNED_A_BAD_PAD_NODE
;
102 /* Make the node pads to 8-byte boundary */
103 if ((node_len
+ pad_len
) & 7) {
105 ubifs_err(c
, "bad padding length %d - %d",
106 offs
, offs
+ node_len
+ pad_len
);
107 return SCANNED_A_BAD_PAD_NODE
;
110 dbg_scan("%d bytes padded at LEB %d:%d, offset now %d", pad_len
,
111 lnum
, offs
, ALIGN(offs
+ node_len
+ pad_len
, 8));
113 return node_len
+ pad_len
;
116 return SCANNED_A_NODE
;
120 * ubifs_start_scan - create LEB scanning information at start of scan.
121 * @c: UBIFS file-system description object
122 * @lnum: logical eraseblock number
123 * @offs: offset to start at (usually zero)
124 * @sbuf: scan buffer (must be c->leb_size)
126 * This function returns the scanned information on success and a negative error
129 struct ubifs_scan_leb
*ubifs_start_scan(const struct ubifs_info
*c
, int lnum
,
130 int offs
, void *sbuf
)
132 struct ubifs_scan_leb
*sleb
;
135 dbg_scan("scan LEB %d:%d", lnum
, offs
);
137 sleb
= kzalloc(sizeof(struct ubifs_scan_leb
), GFP_NOFS
);
139 return ERR_PTR(-ENOMEM
);
142 INIT_LIST_HEAD(&sleb
->nodes
);
145 err
= ubifs_leb_read(c
, lnum
, sbuf
+ offs
, offs
, c
->leb_size
- offs
, 0);
146 if (err
&& err
!= -EBADMSG
) {
147 ubifs_err(c
, "cannot read %d bytes from LEB %d:%d, error %d",
148 c
->leb_size
- offs
, lnum
, offs
, err
);
154 * Note, we ignore integrity errors (EBASMSG) because all the nodes are
155 * protected by CRC checksums.
161 * ubifs_end_scan - update LEB scanning information at end of scan.
162 * @c: UBIFS file-system description object
163 * @sleb: scanning information
164 * @lnum: logical eraseblock number
165 * @offs: offset to start at (usually zero)
167 void ubifs_end_scan(const struct ubifs_info
*c
, struct ubifs_scan_leb
*sleb
,
171 dbg_scan("stop scanning LEB %d at offset %d", lnum
, offs
);
172 ubifs_assert(offs
% c
->min_io_size
== 0);
174 sleb
->endpt
= ALIGN(offs
, c
->min_io_size
);
178 * ubifs_add_snod - add a scanned node to LEB scanning information.
179 * @c: UBIFS file-system description object
180 * @sleb: scanning information
181 * @buf: buffer containing node
182 * @offs: offset of node on flash
184 * This function returns %0 on success and a negative error code on failure.
186 int ubifs_add_snod(const struct ubifs_info
*c
, struct ubifs_scan_leb
*sleb
,
189 struct ubifs_ch
*ch
= buf
;
190 struct ubifs_ino_node
*ino
= buf
;
191 struct ubifs_scan_node
*snod
;
193 snod
= kmalloc(sizeof(struct ubifs_scan_node
), GFP_NOFS
);
197 snod
->sqnum
= le64_to_cpu(ch
->sqnum
);
198 snod
->type
= ch
->node_type
;
200 snod
->len
= le32_to_cpu(ch
->len
);
203 switch (ch
->node_type
) {
205 case UBIFS_DENT_NODE
:
206 case UBIFS_XENT_NODE
:
207 case UBIFS_DATA_NODE
:
209 * The key is in the same place in all keyed
212 key_read(c
, &ino
->key
, &snod
->key
);
215 invalid_key_init(c
, &snod
->key
);
218 list_add_tail(&snod
->list
, &sleb
->nodes
);
219 sleb
->nodes_cnt
+= 1;
224 * ubifs_scanned_corruption - print information after UBIFS scanned corruption.
225 * @c: UBIFS file-system description object
226 * @lnum: LEB number of corruption
227 * @offs: offset of corruption
228 * @buf: buffer containing corruption
230 void ubifs_scanned_corruption(const struct ubifs_info
*c
, int lnum
, int offs
,
235 ubifs_err(c
, "corruption at LEB %d:%d", lnum
, offs
);
236 len
= c
->leb_size
- offs
;
239 ubifs_err(c
, "first %d bytes from LEB %d:%d", len
, lnum
, offs
);
240 print_hex_dump(KERN_DEBUG
, "", DUMP_PREFIX_OFFSET
, 32, 4, buf
, len
, 1);
244 * ubifs_scan - scan a logical eraseblock.
245 * @c: UBIFS file-system description object
246 * @lnum: logical eraseblock number
247 * @offs: offset to start at (usually zero)
248 * @sbuf: scan buffer (must be of @c->leb_size bytes in size)
249 * @quiet: print no messages
251 * This function scans LEB number @lnum and returns complete information about
252 * its contents. Returns the scanned information in case of success and,
253 * %-EUCLEAN if the LEB neads recovery, and other negative error codes in case
256 * If @quiet is non-zero, this function does not print large and scary
257 * error messages and flash dumps in case of errors.
259 struct ubifs_scan_leb
*ubifs_scan(const struct ubifs_info
*c
, int lnum
,
260 int offs
, void *sbuf
, int quiet
)
262 void *buf
= sbuf
+ offs
;
263 int err
, len
= c
->leb_size
- offs
;
264 struct ubifs_scan_leb
*sleb
;
266 sleb
= ubifs_start_scan(c
, lnum
, offs
, sbuf
);
271 struct ubifs_ch
*ch
= buf
;
274 dbg_scan("look at LEB %d:%d (%d bytes left)",
279 ret
= ubifs_scan_a_node(c
, buf
, len
, lnum
, offs
, quiet
);
281 /* Padding bytes or a valid padding node */
288 if (ret
== SCANNED_EMPTY_SPACE
)
289 /* Empty space is checked later */
293 case SCANNED_GARBAGE
:
294 ubifs_err(c
, "garbage");
298 case SCANNED_A_CORRUPT_NODE
:
299 case SCANNED_A_BAD_PAD_NODE
:
300 ubifs_err(c
, "bad node");
303 ubifs_err(c
, "unknown");
308 err
= ubifs_add_snod(c
, sleb
, buf
, offs
);
312 node_len
= ALIGN(le32_to_cpu(ch
->len
), 8);
318 if (offs
% c
->min_io_size
) {
320 ubifs_err(c
, "empty space starts at non-aligned offset %d",
325 ubifs_end_scan(c
, sleb
, lnum
, offs
);
327 for (; len
> 4; offs
+= 4, buf
= buf
+ 4, len
-= 4)
328 if (*(uint32_t *)buf
!= 0xffffffff)
330 for (; len
; offs
++, buf
++, len
--)
331 if (*(uint8_t *)buf
!= 0xff) {
333 ubifs_err(c
, "corrupt empty space at LEB %d:%d",
342 ubifs_scanned_corruption(c
, lnum
, offs
, buf
);
343 ubifs_err(c
, "LEB %d scanning failed", lnum
);
346 ubifs_scan_destroy(sleb
);
350 ubifs_err(c
, "LEB %d scanning failed, error %d", lnum
, err
);
351 ubifs_scan_destroy(sleb
);
356 * ubifs_scan_destroy - destroy LEB scanning information.
357 * @sleb: scanning information to free
359 void ubifs_scan_destroy(struct ubifs_scan_leb
*sleb
)
361 struct ubifs_scan_node
*node
;
362 struct list_head
*head
;
365 while (!list_empty(head
)) {
366 node
= list_entry(head
->next
, struct ubifs_scan_node
, list
);
367 list_del(&node
->list
);