Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Stefan Roese | 9eefe2a | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 2 | /* |
| 3 | * This file is part of UBIFS. |
| 4 | * |
| 5 | * Copyright (C) 2006-2008 Nokia Corporation |
| 6 | * |
Stefan Roese | 9eefe2a | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 7 | * Authors: Adrian Hunter |
| 8 | * Artem Bityutskiy (Битюцкий Артём) |
| 9 | */ |
| 10 | |
| 11 | /* |
| 12 | * This file implements the scan which is a general-purpose function for |
| 13 | * determining what nodes are in an eraseblock. The scan is used to replay the |
| 14 | * journal, to do garbage collection. for the TNC in-the-gaps method, and by |
| 15 | * debugging functions. |
| 16 | */ |
| 17 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 18 | #ifdef __UBOOT__ |
Alexey Brodkin | f8c987f | 2018-06-05 17:17:57 +0300 | [diff] [blame] | 19 | #include <hexdump.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 20 | #include <log.h> |
Simon Glass | 61b29b8 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 21 | #include <dm/devres.h> |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 22 | #include <linux/err.h> |
| 23 | #endif |
Stefan Roese | 9eefe2a | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 24 | #include "ubifs.h" |
| 25 | |
| 26 | /** |
| 27 | * scan_padding_bytes - scan for padding bytes. |
| 28 | * @buf: buffer to scan |
| 29 | * @len: length of buffer |
| 30 | * |
| 31 | * This function returns the number of padding bytes on success and |
| 32 | * %SCANNED_GARBAGE on failure. |
| 33 | */ |
| 34 | static int scan_padding_bytes(void *buf, int len) |
| 35 | { |
| 36 | int pad_len = 0, max_pad_len = min_t(int, UBIFS_PAD_NODE_SZ, len); |
| 37 | uint8_t *p = buf; |
| 38 | |
| 39 | dbg_scan("not a node"); |
| 40 | |
| 41 | while (pad_len < max_pad_len && *p++ == UBIFS_PADDING_BYTE) |
| 42 | pad_len += 1; |
| 43 | |
| 44 | if (!pad_len || (pad_len & 7)) |
| 45 | return SCANNED_GARBAGE; |
| 46 | |
| 47 | dbg_scan("%d padding bytes", pad_len); |
| 48 | |
| 49 | return pad_len; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * ubifs_scan_a_node - scan for a node or padding. |
| 54 | * @c: UBIFS file-system description object |
| 55 | * @buf: buffer to scan |
| 56 | * @len: length of buffer |
| 57 | * @lnum: logical eraseblock number |
| 58 | * @offs: offset within the logical eraseblock |
| 59 | * @quiet: print no messages |
| 60 | * |
| 61 | * This function returns a scanning code to indicate what was scanned. |
| 62 | */ |
| 63 | int ubifs_scan_a_node(const struct ubifs_info *c, void *buf, int len, int lnum, |
| 64 | int offs, int quiet) |
| 65 | { |
| 66 | struct ubifs_ch *ch = buf; |
| 67 | uint32_t magic; |
| 68 | |
| 69 | magic = le32_to_cpu(ch->magic); |
| 70 | |
| 71 | if (magic == 0xFFFFFFFF) { |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 72 | dbg_scan("hit empty space at LEB %d:%d", lnum, offs); |
Stefan Roese | 9eefe2a | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 73 | return SCANNED_EMPTY_SPACE; |
| 74 | } |
| 75 | |
| 76 | if (magic != UBIFS_NODE_MAGIC) |
| 77 | return scan_padding_bytes(buf, len); |
| 78 | |
| 79 | if (len < UBIFS_CH_SZ) |
| 80 | return SCANNED_GARBAGE; |
| 81 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 82 | dbg_scan("scanning %s at LEB %d:%d", |
| 83 | dbg_ntype(ch->node_type), lnum, offs); |
Stefan Roese | 9eefe2a | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 84 | |
| 85 | if (ubifs_check_node(c, buf, lnum, offs, quiet, 1)) |
| 86 | return SCANNED_A_CORRUPT_NODE; |
| 87 | |
| 88 | if (ch->node_type == UBIFS_PAD_NODE) { |
| 89 | struct ubifs_pad_node *pad = buf; |
| 90 | int pad_len = le32_to_cpu(pad->pad_len); |
| 91 | int node_len = le32_to_cpu(ch->len); |
| 92 | |
| 93 | /* Validate the padding node */ |
| 94 | if (pad_len < 0 || |
| 95 | offs + node_len + pad_len > c->leb_size) { |
| 96 | if (!quiet) { |
Heiko Schocher | 0195a7b | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 97 | ubifs_err(c, "bad pad node at LEB %d:%d", |
Stefan Roese | 9eefe2a | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 98 | lnum, offs); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 99 | ubifs_dump_node(c, pad); |
Stefan Roese | 9eefe2a | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 100 | } |
| 101 | return SCANNED_A_BAD_PAD_NODE; |
| 102 | } |
| 103 | |
| 104 | /* Make the node pads to 8-byte boundary */ |
| 105 | if ((node_len + pad_len) & 7) { |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 106 | if (!quiet) |
Heiko Schocher | 0195a7b | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 107 | ubifs_err(c, "bad padding length %d - %d", |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 108 | offs, offs + node_len + pad_len); |
Stefan Roese | 9eefe2a | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 109 | return SCANNED_A_BAD_PAD_NODE; |
| 110 | } |
| 111 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 112 | dbg_scan("%d bytes padded at LEB %d:%d, offset now %d", pad_len, |
| 113 | lnum, offs, ALIGN(offs + node_len + pad_len, 8)); |
Stefan Roese | 9eefe2a | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 114 | |
| 115 | return node_len + pad_len; |
| 116 | } |
| 117 | |
| 118 | return SCANNED_A_NODE; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * ubifs_start_scan - create LEB scanning information at start of scan. |
| 123 | * @c: UBIFS file-system description object |
| 124 | * @lnum: logical eraseblock number |
| 125 | * @offs: offset to start at (usually zero) |
| 126 | * @sbuf: scan buffer (must be c->leb_size) |
| 127 | * |
Heiko Schocher | 0195a7b | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 128 | * This function returns the scanned information on success and a negative error |
| 129 | * code on failure. |
Stefan Roese | 9eefe2a | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 130 | */ |
| 131 | struct ubifs_scan_leb *ubifs_start_scan(const struct ubifs_info *c, int lnum, |
| 132 | int offs, void *sbuf) |
| 133 | { |
| 134 | struct ubifs_scan_leb *sleb; |
| 135 | int err; |
| 136 | |
| 137 | dbg_scan("scan LEB %d:%d", lnum, offs); |
| 138 | |
| 139 | sleb = kzalloc(sizeof(struct ubifs_scan_leb), GFP_NOFS); |
| 140 | if (!sleb) |
| 141 | return ERR_PTR(-ENOMEM); |
| 142 | |
| 143 | sleb->lnum = lnum; |
| 144 | INIT_LIST_HEAD(&sleb->nodes); |
| 145 | sleb->buf = sbuf; |
| 146 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 147 | err = ubifs_leb_read(c, lnum, sbuf + offs, offs, c->leb_size - offs, 0); |
Stefan Roese | 9eefe2a | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 148 | if (err && err != -EBADMSG) { |
Heiko Schocher | 0195a7b | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 149 | ubifs_err(c, "cannot read %d bytes from LEB %d:%d, error %d", |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 150 | c->leb_size - offs, lnum, offs, err); |
Stefan Roese | 9eefe2a | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 151 | kfree(sleb); |
| 152 | return ERR_PTR(err); |
| 153 | } |
| 154 | |
Heiko Schocher | 0195a7b | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 155 | /* |
| 156 | * Note, we ignore integrity errors (EBASMSG) because all the nodes are |
| 157 | * protected by CRC checksums. |
| 158 | */ |
Stefan Roese | 9eefe2a | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 159 | return sleb; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * ubifs_end_scan - update LEB scanning information at end of scan. |
| 164 | * @c: UBIFS file-system description object |
| 165 | * @sleb: scanning information |
| 166 | * @lnum: logical eraseblock number |
| 167 | * @offs: offset to start at (usually zero) |
Stefan Roese | 9eefe2a | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 168 | */ |
| 169 | void ubifs_end_scan(const struct ubifs_info *c, struct ubifs_scan_leb *sleb, |
| 170 | int lnum, int offs) |
| 171 | { |
| 172 | lnum = lnum; |
| 173 | dbg_scan("stop scanning LEB %d at offset %d", lnum, offs); |
| 174 | ubifs_assert(offs % c->min_io_size == 0); |
| 175 | |
| 176 | sleb->endpt = ALIGN(offs, c->min_io_size); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * ubifs_add_snod - add a scanned node to LEB scanning information. |
| 181 | * @c: UBIFS file-system description object |
| 182 | * @sleb: scanning information |
| 183 | * @buf: buffer containing node |
| 184 | * @offs: offset of node on flash |
| 185 | * |
| 186 | * This function returns %0 on success and a negative error code on failure. |
| 187 | */ |
| 188 | int ubifs_add_snod(const struct ubifs_info *c, struct ubifs_scan_leb *sleb, |
| 189 | void *buf, int offs) |
| 190 | { |
| 191 | struct ubifs_ch *ch = buf; |
| 192 | struct ubifs_ino_node *ino = buf; |
| 193 | struct ubifs_scan_node *snod; |
| 194 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 195 | snod = kmalloc(sizeof(struct ubifs_scan_node), GFP_NOFS); |
Stefan Roese | 9eefe2a | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 196 | if (!snod) |
| 197 | return -ENOMEM; |
| 198 | |
| 199 | snod->sqnum = le64_to_cpu(ch->sqnum); |
| 200 | snod->type = ch->node_type; |
| 201 | snod->offs = offs; |
| 202 | snod->len = le32_to_cpu(ch->len); |
| 203 | snod->node = buf; |
| 204 | |
| 205 | switch (ch->node_type) { |
| 206 | case UBIFS_INO_NODE: |
| 207 | case UBIFS_DENT_NODE: |
| 208 | case UBIFS_XENT_NODE: |
| 209 | case UBIFS_DATA_NODE: |
Stefan Roese | 9eefe2a | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 210 | /* |
| 211 | * The key is in the same place in all keyed |
| 212 | * nodes. |
| 213 | */ |
| 214 | key_read(c, &ino->key, &snod->key); |
| 215 | break; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 216 | default: |
| 217 | invalid_key_init(c, &snod->key); |
| 218 | break; |
Stefan Roese | 9eefe2a | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 219 | } |
| 220 | list_add_tail(&snod->list, &sleb->nodes); |
| 221 | sleb->nodes_cnt += 1; |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * ubifs_scanned_corruption - print information after UBIFS scanned corruption. |
| 227 | * @c: UBIFS file-system description object |
| 228 | * @lnum: LEB number of corruption |
| 229 | * @offs: offset of corruption |
| 230 | * @buf: buffer containing corruption |
| 231 | */ |
| 232 | void ubifs_scanned_corruption(const struct ubifs_info *c, int lnum, int offs, |
| 233 | void *buf) |
| 234 | { |
| 235 | int len; |
| 236 | |
Heiko Schocher | 0195a7b | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 237 | ubifs_err(c, "corruption at LEB %d:%d", lnum, offs); |
Stefan Roese | 9eefe2a | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 238 | len = c->leb_size - offs; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 239 | if (len > 8192) |
| 240 | len = 8192; |
Heiko Schocher | 0195a7b | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 241 | ubifs_err(c, "first %d bytes from LEB %d:%d", len, lnum, offs); |
Alexey Brodkin | f8c987f | 2018-06-05 17:17:57 +0300 | [diff] [blame] | 242 | print_hex_dump("", DUMP_PREFIX_OFFSET, 32, 4, buf, len, 1); |
Stefan Roese | 9eefe2a | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | /** |
| 246 | * ubifs_scan - scan a logical eraseblock. |
| 247 | * @c: UBIFS file-system description object |
| 248 | * @lnum: logical eraseblock number |
| 249 | * @offs: offset to start at (usually zero) |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 250 | * @sbuf: scan buffer (must be of @c->leb_size bytes in size) |
| 251 | * @quiet: print no messages |
Stefan Roese | 9eefe2a | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 252 | * |
| 253 | * This function scans LEB number @lnum and returns complete information about |
Heiko Schocher | 0195a7b | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 254 | * its contents. Returns the scanned information in case of success and, |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 255 | * %-EUCLEAN if the LEB neads recovery, and other negative error codes in case |
| 256 | * of failure. |
| 257 | * |
| 258 | * If @quiet is non-zero, this function does not print large and scary |
| 259 | * error messages and flash dumps in case of errors. |
Stefan Roese | 9eefe2a | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 260 | */ |
| 261 | struct ubifs_scan_leb *ubifs_scan(const struct ubifs_info *c, int lnum, |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 262 | int offs, void *sbuf, int quiet) |
Stefan Roese | 9eefe2a | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 263 | { |
| 264 | void *buf = sbuf + offs; |
| 265 | int err, len = c->leb_size - offs; |
| 266 | struct ubifs_scan_leb *sleb; |
| 267 | |
| 268 | sleb = ubifs_start_scan(c, lnum, offs, sbuf); |
| 269 | if (IS_ERR(sleb)) |
| 270 | return sleb; |
| 271 | |
| 272 | while (len >= 8) { |
| 273 | struct ubifs_ch *ch = buf; |
| 274 | int node_len, ret; |
| 275 | |
| 276 | dbg_scan("look at LEB %d:%d (%d bytes left)", |
| 277 | lnum, offs, len); |
| 278 | |
| 279 | cond_resched(); |
| 280 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 281 | ret = ubifs_scan_a_node(c, buf, len, lnum, offs, quiet); |
Stefan Roese | 9eefe2a | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 282 | if (ret > 0) { |
| 283 | /* Padding bytes or a valid padding node */ |
| 284 | offs += ret; |
| 285 | buf += ret; |
| 286 | len -= ret; |
| 287 | continue; |
| 288 | } |
| 289 | |
| 290 | if (ret == SCANNED_EMPTY_SPACE) |
| 291 | /* Empty space is checked later */ |
| 292 | break; |
| 293 | |
| 294 | switch (ret) { |
| 295 | case SCANNED_GARBAGE: |
Heiko Schocher | 0195a7b | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 296 | ubifs_err(c, "garbage"); |
Stefan Roese | 9eefe2a | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 297 | goto corrupted; |
| 298 | case SCANNED_A_NODE: |
| 299 | break; |
| 300 | case SCANNED_A_CORRUPT_NODE: |
| 301 | case SCANNED_A_BAD_PAD_NODE: |
Heiko Schocher | 0195a7b | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 302 | ubifs_err(c, "bad node"); |
Stefan Roese | 9eefe2a | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 303 | goto corrupted; |
| 304 | default: |
Heiko Schocher | 0195a7b | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 305 | ubifs_err(c, "unknown"); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 306 | err = -EINVAL; |
| 307 | goto error; |
Stefan Roese | 9eefe2a | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | err = ubifs_add_snod(c, sleb, buf, offs); |
| 311 | if (err) |
| 312 | goto error; |
| 313 | |
| 314 | node_len = ALIGN(le32_to_cpu(ch->len), 8); |
| 315 | offs += node_len; |
| 316 | buf += node_len; |
| 317 | len -= node_len; |
| 318 | } |
| 319 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 320 | if (offs % c->min_io_size) { |
| 321 | if (!quiet) |
Heiko Schocher | 0195a7b | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 322 | ubifs_err(c, "empty space starts at non-aligned offset %d", |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 323 | offs); |
Stefan Roese | 9eefe2a | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 324 | goto corrupted; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 325 | } |
Stefan Roese | 9eefe2a | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 326 | |
| 327 | ubifs_end_scan(c, sleb, lnum, offs); |
| 328 | |
| 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) { |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 334 | if (!quiet) |
Heiko Schocher | 0195a7b | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 335 | ubifs_err(c, "corrupt empty space at LEB %d:%d", |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 336 | lnum, offs); |
Stefan Roese | 9eefe2a | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 337 | goto corrupted; |
| 338 | } |
| 339 | |
| 340 | return sleb; |
| 341 | |
| 342 | corrupted: |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 343 | if (!quiet) { |
| 344 | ubifs_scanned_corruption(c, lnum, offs, buf); |
Heiko Schocher | 0195a7b | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 345 | ubifs_err(c, "LEB %d scanning failed", lnum); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 346 | } |
Stefan Roese | 9eefe2a | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 347 | err = -EUCLEAN; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 348 | ubifs_scan_destroy(sleb); |
| 349 | return ERR_PTR(err); |
| 350 | |
Stefan Roese | 9eefe2a | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 351 | error: |
Heiko Schocher | 0195a7b | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 352 | ubifs_err(c, "LEB %d scanning failed, error %d", lnum, err); |
Stefan Roese | 9eefe2a | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 353 | ubifs_scan_destroy(sleb); |
| 354 | return ERR_PTR(err); |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * ubifs_scan_destroy - destroy LEB scanning information. |
| 359 | * @sleb: scanning information to free |
| 360 | */ |
| 361 | void ubifs_scan_destroy(struct ubifs_scan_leb *sleb) |
| 362 | { |
| 363 | struct ubifs_scan_node *node; |
| 364 | struct list_head *head; |
| 365 | |
| 366 | head = &sleb->nodes; |
| 367 | while (!list_empty(head)) { |
| 368 | node = list_entry(head->next, struct ubifs_scan_node, list); |
| 369 | list_del(&node->list); |
| 370 | kfree(node); |
| 371 | } |
| 372 | kfree(sleb); |
| 373 | } |