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