Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Stefan Roese | 2fc10f6 | 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 | 2fc10f6 | 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 | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 18 | #ifdef __UBOOT__ |
Alexey Brodkin | 2d2fa49 | 2018-06-05 17:17:57 +0300 | [diff] [blame] | 19 | #include <hexdump.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 20 | #include <dm/devres.h> |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 21 | #include <linux/err.h> |
| 22 | #endif |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 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) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 71 | dbg_scan("hit empty space at LEB %d:%d", lnum, offs); |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 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 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 81 | dbg_scan("scanning %s at LEB %d:%d", |
| 82 | dbg_ntype(ch->node_type), lnum, offs); |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 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) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 96 | ubifs_err(c, "bad pad node at LEB %d:%d", |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 97 | lnum, offs); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 98 | ubifs_dump_node(c, pad); |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 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) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 105 | if (!quiet) |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 106 | ubifs_err(c, "bad padding length %d - %d", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 107 | offs, offs + node_len + pad_len); |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 108 | return SCANNED_A_BAD_PAD_NODE; |
| 109 | } |
| 110 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 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)); |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 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 | * |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 127 | * This function returns the scanned information on success and a negative error |
| 128 | * code on failure. |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 129 | */ |
| 130 | struct ubifs_scan_leb *ubifs_start_scan(const struct ubifs_info *c, int lnum, |
| 131 | int offs, void *sbuf) |
| 132 | { |
| 133 | struct ubifs_scan_leb *sleb; |
| 134 | int err; |
| 135 | |
| 136 | dbg_scan("scan LEB %d:%d", lnum, offs); |
| 137 | |
| 138 | sleb = kzalloc(sizeof(struct ubifs_scan_leb), GFP_NOFS); |
| 139 | if (!sleb) |
| 140 | return ERR_PTR(-ENOMEM); |
| 141 | |
| 142 | sleb->lnum = lnum; |
| 143 | INIT_LIST_HEAD(&sleb->nodes); |
| 144 | sleb->buf = sbuf; |
| 145 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 146 | err = ubifs_leb_read(c, lnum, sbuf + offs, offs, c->leb_size - offs, 0); |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 147 | if (err && err != -EBADMSG) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 148 | ubifs_err(c, "cannot read %d bytes from LEB %d:%d, error %d", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 149 | c->leb_size - offs, lnum, offs, err); |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 150 | kfree(sleb); |
| 151 | return ERR_PTR(err); |
| 152 | } |
| 153 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 154 | /* |
| 155 | * Note, we ignore integrity errors (EBASMSG) because all the nodes are |
| 156 | * protected by CRC checksums. |
| 157 | */ |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 158 | return sleb; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * ubifs_end_scan - update LEB scanning information at end of scan. |
| 163 | * @c: UBIFS file-system description object |
| 164 | * @sleb: scanning information |
| 165 | * @lnum: logical eraseblock number |
| 166 | * @offs: offset to start at (usually zero) |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 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 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 194 | snod = kmalloc(sizeof(struct ubifs_scan_node), GFP_NOFS); |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 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: |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 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; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 215 | default: |
| 216 | invalid_key_init(c, &snod->key); |
| 217 | break; |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 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 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 236 | ubifs_err(c, "corruption at LEB %d:%d", lnum, offs); |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 237 | len = c->leb_size - offs; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 238 | if (len > 8192) |
| 239 | len = 8192; |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 240 | ubifs_err(c, "first %d bytes from LEB %d:%d", len, lnum, offs); |
Alexey Brodkin | 2d2fa49 | 2018-06-05 17:17:57 +0300 | [diff] [blame] | 241 | print_hex_dump("", DUMP_PREFIX_OFFSET, 32, 4, buf, len, 1); |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 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) |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 249 | * @sbuf: scan buffer (must be of @c->leb_size bytes in size) |
| 250 | * @quiet: print no messages |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 251 | * |
| 252 | * This function scans LEB number @lnum and returns complete information about |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 253 | * its contents. Returns the scanned information in case of success and, |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 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. |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 259 | */ |
| 260 | struct ubifs_scan_leb *ubifs_scan(const struct ubifs_info *c, int lnum, |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 261 | int offs, void *sbuf, int quiet) |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 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 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 280 | ret = ubifs_scan_a_node(c, buf, len, lnum, offs, quiet); |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 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: |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 295 | ubifs_err(c, "garbage"); |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 296 | goto corrupted; |
| 297 | case SCANNED_A_NODE: |
| 298 | break; |
| 299 | case SCANNED_A_CORRUPT_NODE: |
| 300 | case SCANNED_A_BAD_PAD_NODE: |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 301 | ubifs_err(c, "bad node"); |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 302 | goto corrupted; |
| 303 | default: |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 304 | ubifs_err(c, "unknown"); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 305 | err = -EINVAL; |
| 306 | goto error; |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 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 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 319 | if (offs % c->min_io_size) { |
| 320 | if (!quiet) |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 321 | ubifs_err(c, "empty space starts at non-aligned offset %d", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 322 | offs); |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 323 | goto corrupted; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 324 | } |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 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) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 333 | if (!quiet) |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 334 | ubifs_err(c, "corrupt empty space at LEB %d:%d", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 335 | lnum, offs); |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 336 | goto corrupted; |
| 337 | } |
| 338 | |
| 339 | return sleb; |
| 340 | |
| 341 | corrupted: |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 342 | if (!quiet) { |
| 343 | ubifs_scanned_corruption(c, lnum, offs, buf); |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 344 | ubifs_err(c, "LEB %d scanning failed", lnum); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 345 | } |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 346 | err = -EUCLEAN; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 347 | ubifs_scan_destroy(sleb); |
| 348 | return ERR_PTR(err); |
| 349 | |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 350 | error: |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 351 | ubifs_err(c, "LEB %d scanning failed, error %d", lnum, err); |
Stefan Roese | 2fc10f6 | 2009-03-19 15:35:05 +0100 | [diff] [blame] | 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 | } |