Blame view
fs/ubifs/recovery.c
43.2 KB
1e51764a3 UBIFS: add new fl... |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
/* * This file is part of UBIFS. * * Copyright (C) 2006-2008 Nokia Corporation * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 as published by * the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * * You should have received a copy of the GNU General Public License along with * this program; if not, write to the Free Software Foundation, Inc., 51 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * * Authors: Adrian Hunter * Artem Bityutskiy (Битюцкий Артём) */ /* * This file implements functions needed to recover from unclean un-mounts. * When UBIFS is mounted, it checks a flag on the master node to determine if |
af901ca18 tree-wide: fix as... |
26 |
* an un-mount was completed successfully. If not, the process of mounting |
6fb4374f6 UBIFS: fix GC LEB... |
27 |
* incorporates additional checking and fixing of on-flash data structures. |
1e51764a3 UBIFS: add new fl... |
28 29 30 |
* UBIFS always cleans away all remnants of an unclean un-mount, so that * errors do not accumulate. However UBIFS defers recovery if it is mounted * read-only, and the flash is not modified in that case. |
be7b42a5c UBIFS: describe U... |
31 32 33 34 35 36 37 38 39 40 |
* * The general UBIFS approach to the recovery is that it recovers from * corruptions which could be caused by power cuts, but it refuses to recover * from corruption caused by other reasons. And UBIFS tries to distinguish * between these 2 reasons of corruptions and silently recover in the former * case and loudly complain in the latter case. * * UBIFS writes only to erased LEBs, so it writes only to the flash space * containing only 0xFFs. UBIFS also always writes strictly from the beginning * of the LEB to the end. And UBIFS assumes that the underlying flash media |
2765df7da UBIFS: use max_wr... |
41 |
* writes in @c->max_write_size bytes at a time. |
be7b42a5c UBIFS: describe U... |
42 43 44 45 46 47 |
* * Hence, if UBIFS finds a corrupted node at offset X, it expects only the min. * I/O unit corresponding to offset X to contain corrupted data, all the * following min. I/O units have to contain empty space (all 0xFFs). If this is * not true, the corruption cannot be the result of a power cut, and UBIFS * refuses to mount. |
1e51764a3 UBIFS: add new fl... |
48 49 50 |
*/ #include <linux/crc32.h> |
5a0e3ad6a include cleanup: ... |
51 |
#include <linux/slab.h> |
1e51764a3 UBIFS: add new fl... |
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
#include "ubifs.h" /** * is_empty - determine whether a buffer is empty (contains all 0xff). * @buf: buffer to clean * @len: length of buffer * * This function returns %1 if the buffer is empty (contains all 0xff) otherwise * %0 is returned. */ static int is_empty(void *buf, int len) { uint8_t *p = buf; int i; for (i = 0; i < len; i++) if (*p++ != 0xff) return 0; return 1; } /** |
061125476 UBIFS: fix corrup... |
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
* first_non_ff - find offset of the first non-0xff byte. * @buf: buffer to search in * @len: length of buffer * * This function returns offset of the first non-0xff byte in @buf or %-1 if * the buffer contains only 0xff bytes. */ static int first_non_ff(void *buf, int len) { uint8_t *p = buf; int i; for (i = 0; i < len; i++) if (*p++ != 0xff) return i; return -1; } /** |
1e51764a3 UBIFS: add new fl... |
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
* get_master_node - get the last valid master node allowing for corruption. * @c: UBIFS file-system description object * @lnum: LEB number * @pbuf: buffer containing the LEB read, is returned here * @mst: master node, if found, is returned here * @cor: corruption, if found, is returned here * * This function allocates a buffer, reads the LEB into it, and finds and * returns the last valid master node allowing for one area of corruption. * The corrupt area, if there is one, must be consistent with the assumption * that it is the result of an unclean unmount while the master node was being * written. Under those circumstances, it is valid to use the previously written * master node. * * This function returns %0 on success and a negative error code on failure. */ static int get_master_node(const struct ubifs_info *c, int lnum, void **pbuf, struct ubifs_mst_node **mst, void **cor) { const int sz = c->mst_node_alsz; int err, offs, len; void *sbuf, *buf; sbuf = vmalloc(c->leb_size); if (!sbuf) return -ENOMEM; |
d304820a1 UBIFS: switch to ... |
119 |
err = ubifs_leb_read(c, lnum, sbuf, 0, c->leb_size, 0); |
1e51764a3 UBIFS: add new fl... |
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
if (err && err != -EBADMSG) goto out_free; /* Find the first position that is definitely not a node */ offs = 0; buf = sbuf; len = c->leb_size; while (offs + UBIFS_MST_NODE_SZ <= c->leb_size) { struct ubifs_ch *ch = buf; if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC) break; offs += sz; buf += sz; len -= sz; } /* See if there was a valid master node before that */ if (offs) { int ret; offs -= sz; buf -= sz; len += sz; ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 1); if (ret != SCANNED_A_NODE && offs) { /* Could have been corruption so check one place back */ offs -= sz; buf -= sz; len += sz; ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 1); if (ret != SCANNED_A_NODE) /* * We accept only one area of corruption because * we are assuming that it was caused while * trying to write a master node. */ goto out_err; } if (ret == SCANNED_A_NODE) { struct ubifs_ch *ch = buf; if (ch->node_type != UBIFS_MST_NODE) goto out_err; dbg_rcvry("found a master node at %d:%d", lnum, offs); *mst = buf; offs += sz; buf += sz; len -= sz; } } /* Check for corruption */ if (offs < c->leb_size) { if (!is_empty(buf, min_t(int, len, sz))) { *cor = buf; dbg_rcvry("found corruption at %d:%d", lnum, offs); } offs += sz; buf += sz; len -= sz; } /* Check remaining empty space */ if (offs < c->leb_size) if (!is_empty(buf, len)) goto out_err; *pbuf = sbuf; return 0; out_err: err = -EINVAL; out_free: vfree(sbuf); *mst = NULL; *cor = NULL; return err; } /** * write_rcvrd_mst_node - write recovered master node. * @c: UBIFS file-system description object * @mst: master node * * This function returns %0 on success and a negative error code on failure. */ static int write_rcvrd_mst_node(struct ubifs_info *c, struct ubifs_mst_node *mst) { int err = 0, lnum = UBIFS_MST_LNUM, sz = c->mst_node_alsz; |
0ecb9529a UBIFS: endian han... |
207 |
__le32 save_flags; |
1e51764a3 UBIFS: add new fl... |
208 209 210 211 |
dbg_rcvry("recovery"); save_flags = mst->flags; |
0ecb9529a UBIFS: endian han... |
212 |
mst->flags |= cpu_to_le32(UBIFS_MST_RCVRY); |
1e51764a3 UBIFS: add new fl... |
213 214 |
ubifs_prepare_node(c, mst, UBIFS_MST_NODE_SZ, 1); |
d3b2578f5 UBIFS: switch to ... |
215 |
err = ubifs_leb_change(c, lnum, mst, sz, UBI_SHORTTERM); |
1e51764a3 UBIFS: add new fl... |
216 217 |
if (err) goto out; |
d3b2578f5 UBIFS: switch to ... |
218 |
err = ubifs_leb_change(c, lnum + 1, mst, sz, UBI_SHORTTERM); |
1e51764a3 UBIFS: add new fl... |
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
if (err) goto out; out: mst->flags = save_flags; return err; } /** * ubifs_recover_master_node - recover the master node. * @c: UBIFS file-system description object * * This function recovers the master node from corruption that may occur due to * an unclean unmount. * * This function returns %0 on success and a negative error code on failure. */ int ubifs_recover_master_node(struct ubifs_info *c) { void *buf1 = NULL, *buf2 = NULL, *cor1 = NULL, *cor2 = NULL; struct ubifs_mst_node *mst1 = NULL, *mst2 = NULL, *mst; const int sz = c->mst_node_alsz; int err, offs1, offs2; dbg_rcvry("recovery"); err = get_master_node(c, UBIFS_MST_LNUM, &buf1, &mst1, &cor1); if (err) goto out_free; err = get_master_node(c, UBIFS_MST_LNUM + 1, &buf2, &mst2, &cor2); if (err) goto out_free; if (mst1) { offs1 = (void *)mst1 - buf1; if ((le32_to_cpu(mst1->flags) & UBIFS_MST_RCVRY) && (offs1 == 0 && !cor1)) { /* * mst1 was written by recovery at offset 0 with no * corruption. */ dbg_rcvry("recovery recovery"); mst = mst1; } else if (mst2) { offs2 = (void *)mst2 - buf2; if (offs1 == offs2) { /* Same offset, so must be the same */ if (memcmp((void *)mst1 + UBIFS_CH_SZ, (void *)mst2 + UBIFS_CH_SZ, UBIFS_MST_NODE_SZ - UBIFS_CH_SZ)) goto out_err; mst = mst1; } else if (offs2 + sz == offs1) { /* 1st LEB was written, 2nd was not */ if (cor1) goto out_err; mst = mst1; |
19495f70d UBIFS: fix master... |
276 277 |
} else if (offs1 == 0 && c->leb_size - offs2 - sz < sz) { |
1e51764a3 UBIFS: add new fl... |
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
/* 1st LEB was unmapped and written, 2nd not */ if (cor1) goto out_err; mst = mst1; } else goto out_err; } else { /* * 2nd LEB was unmapped and about to be written, so * there must be only one master node in the first LEB * and no corruption. */ if (offs1 != 0 || cor1) goto out_err; mst = mst1; } } else { if (!mst2) goto out_err; /* * 1st LEB was unmapped and about to be written, so there must * be no room left in 2nd LEB. */ offs2 = (void *)mst2 - buf2; if (offs2 + sz + sz <= c->leb_size) goto out_err; mst = mst2; } |
348709bad UBIFS: do not pri... |
306 |
ubifs_msg("recovered master node from LEB %d", |
1e51764a3 UBIFS: add new fl... |
307 308 309 |
(mst == mst1 ? UBIFS_MST_LNUM : UBIFS_MST_LNUM + 1)); memcpy(c->mst_node, mst, UBIFS_MST_NODE_SZ); |
2ef13294d UBIFS: introduce ... |
310 |
if (c->ro_mount) { |
1e51764a3 UBIFS: add new fl... |
311 312 313 314 315 316 317 |
/* Read-only mode. Keep a copy for switching to rw mode */ c->rcvrd_mst_node = kmalloc(sz, GFP_KERNEL); if (!c->rcvrd_mst_node) { err = -ENOMEM; goto out_free; } memcpy(c->rcvrd_mst_node, c->mst_node, UBIFS_MST_NODE_SZ); |
6e0d9fd38 UBIFS: fix master... |
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
/* * We had to recover the master node, which means there was an * unclean reboot. However, it is possible that the master node * is clean at this point, i.e., %UBIFS_MST_DIRTY is not set. * E.g., consider the following chain of events: * * 1. UBIFS was cleanly unmounted, so the master node is clean * 2. UBIFS is being mounted R/W and starts changing the master * node in the first (%UBIFS_MST_LNUM). A power cut happens, * so this LEB ends up with some amount of garbage at the * end. * 3. UBIFS is being mounted R/O. We reach this place and * recover the master node from the second LEB * (%UBIFS_MST_LNUM + 1). But we cannot update the media * because we are being mounted R/O. We have to defer the * operation. * 4. However, this master node (@c->mst_node) is marked as * clean (since the step 1). And if we just return, the * mount code will be confused and won't recover the master * node when it is re-mounter R/W later. * * Thus, to force the recovery by marking the master node as * dirty. */ c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY); |
1e51764a3 UBIFS: add new fl... |
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 |
} else { /* Write the recovered master node */ c->max_sqnum = le64_to_cpu(mst->ch.sqnum) - 1; err = write_rcvrd_mst_node(c, c->mst_node); if (err) goto out_free; } vfree(buf2); vfree(buf1); return 0; out_err: err = -EINVAL; out_free: ubifs_err("failed to recover master node"); if (mst1) { dbg_err("dumping first master node"); dbg_dump_node(c, mst1); } if (mst2) { dbg_err("dumping second master node"); dbg_dump_node(c, mst2); } vfree(buf2); vfree(buf1); return err; } /** * ubifs_write_rcvrd_mst_node - write the recovered master node. * @c: UBIFS file-system description object * * This function writes the master node that was recovered during mounting in * read-only mode and must now be written because we are remounting rw. * * This function returns %0 on success and a negative error code on failure. */ int ubifs_write_rcvrd_mst_node(struct ubifs_info *c) { int err; if (!c->rcvrd_mst_node) return 0; c->rcvrd_mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY); c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY); err = write_rcvrd_mst_node(c, c->rcvrd_mst_node); if (err) return err; kfree(c->rcvrd_mst_node); c->rcvrd_mst_node = NULL; return 0; } /** * is_last_write - determine if an offset was in the last write to a LEB. * @c: UBIFS file-system description object * @buf: buffer to check * @offs: offset to check * * This function returns %1 if @offs was in the last write to the LEB whose data |
2765df7da UBIFS: use max_wr... |
406 407 408 |
* is in @buf, otherwise %0 is returned. The determination is made by checking * for subsequent empty space starting from the next @c->max_write_size * boundary. |
1e51764a3 UBIFS: add new fl... |
409 410 411 |
*/ static int is_last_write(const struct ubifs_info *c, void *buf, int offs) { |
428ff9d2e UBIFS: remove dea... |
412 |
int empty_offs, check_len; |
1e51764a3 UBIFS: add new fl... |
413 |
uint8_t *p; |
1e51764a3 UBIFS: add new fl... |
414 |
/* |
2765df7da UBIFS: use max_wr... |
415 416 |
* Round up to the next @c->max_write_size boundary i.e. @offs is in * the last wbuf written. After that should be empty space. |
1e51764a3 UBIFS: add new fl... |
417 |
*/ |
2765df7da UBIFS: use max_wr... |
418 |
empty_offs = ALIGN(offs + 1, c->max_write_size); |
1e51764a3 UBIFS: add new fl... |
419 420 |
check_len = c->leb_size - empty_offs; p = buf + empty_offs - offs; |
431102fed UBIFS: clean up f... |
421 |
return is_empty(p, check_len); |
1e51764a3 UBIFS: add new fl... |
422 423 424 425 426 427 428 429 430 431 432 433 |
} /** * clean_buf - clean the data from an LEB sitting in a buffer. * @c: UBIFS file-system description object * @buf: buffer to clean * @lnum: LEB number to clean * @offs: offset from which to clean * @len: length of buffer * * This function pads up to the next min_io_size boundary (if there is one) and * sets empty space to all 0xff. @buf, @offs and @len are updated to the next |
428ff9d2e UBIFS: remove dea... |
434 |
* @c->min_io_size boundary. |
1e51764a3 UBIFS: add new fl... |
435 436 437 438 439 440 441 442 |
*/ static void clean_buf(const struct ubifs_info *c, void **buf, int lnum, int *offs, int *len) { int empty_offs, pad_len; lnum = lnum; dbg_rcvry("cleaning corruption at %d:%d", lnum, *offs); |
1e51764a3 UBIFS: add new fl... |
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 |
ubifs_assert(!(*offs & 7)); empty_offs = ALIGN(*offs, c->min_io_size); pad_len = empty_offs - *offs; ubifs_pad(c, *buf, pad_len); *offs += pad_len; *buf += pad_len; *len -= pad_len; memset(*buf, 0xff, c->leb_size - empty_offs); } /** * no_more_nodes - determine if there are no more nodes in a buffer. * @c: UBIFS file-system description object * @buf: buffer to check * @len: length of buffer * @lnum: LEB number of the LEB from which @buf was read * @offs: offset from which @buf was read * |
de0975781 UBIFS: fix recove... |
461 462 463 |
* This function ensures that the corrupted node at @offs is the last thing * written to a LEB. This function returns %1 if more data is not found and * %0 if more data is found. |
1e51764a3 UBIFS: add new fl... |
464 465 466 467 |
*/ static int no_more_nodes(const struct ubifs_info *c, void *buf, int len, int lnum, int offs) { |
de0975781 UBIFS: fix recove... |
468 469 |
struct ubifs_ch *ch = buf; int skip, dlen = le32_to_cpu(ch->len); |
1e51764a3 UBIFS: add new fl... |
470 |
|
de0975781 UBIFS: fix recove... |
471 |
/* Check for empty space after the corrupt node's common header */ |
2765df7da UBIFS: use max_wr... |
472 |
skip = ALIGN(offs + UBIFS_CH_SZ, c->max_write_size) - offs; |
de0975781 UBIFS: fix recove... |
473 474 475 476 477 478 479 480 481 |
if (is_empty(buf + skip, len - skip)) return 1; /* * The area after the common header size is not empty, so the common * header must be intact. Check it. */ if (ubifs_check_node(c, buf, lnum, offs, 1, 0) != -EUCLEAN) { dbg_rcvry("unexpected bad common header at %d:%d", lnum, offs); return 0; |
1e51764a3 UBIFS: add new fl... |
482 |
} |
de0975781 UBIFS: fix recove... |
483 |
/* Now we know the corrupt node's length we can skip over it */ |
2765df7da UBIFS: use max_wr... |
484 |
skip = ALIGN(offs + dlen, c->max_write_size) - offs; |
de0975781 UBIFS: fix recove... |
485 486 487 488 489 |
/* After which there should be empty space */ if (is_empty(buf + skip, len - skip)) return 1; dbg_rcvry("unexpected data at %d:%d", lnum, offs + skip); return 0; |
1e51764a3 UBIFS: add new fl... |
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 |
} /** * fix_unclean_leb - fix an unclean LEB. * @c: UBIFS file-system description object * @sleb: scanned LEB information * @start: offset where scan started */ static int fix_unclean_leb(struct ubifs_info *c, struct ubifs_scan_leb *sleb, int start) { int lnum = sleb->lnum, endpt = start; /* Get the end offset of the last node we are keeping */ if (!list_empty(&sleb->nodes)) { struct ubifs_scan_node *snod; snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node, list); endpt = snod->offs + snod->len; } |
2ef13294d UBIFS: introduce ... |
511 |
if (c->ro_mount && !c->remounting_rw) { |
1e51764a3 UBIFS: add new fl... |
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 |
/* Add to recovery list */ struct ubifs_unclean_leb *ucleb; dbg_rcvry("need to fix LEB %d start %d endpt %d", lnum, start, sleb->endpt); ucleb = kzalloc(sizeof(struct ubifs_unclean_leb), GFP_NOFS); if (!ucleb) return -ENOMEM; ucleb->lnum = lnum; ucleb->endpt = endpt; list_add_tail(&ucleb->list, &c->unclean_leb_list); } else { /* Write the fixed LEB back to flash */ int err; dbg_rcvry("fixing LEB %d start %d endpt %d", lnum, start, sleb->endpt); if (endpt == 0) { err = ubifs_leb_unmap(c, lnum); if (err) return err; } else { int len = ALIGN(endpt, c->min_io_size); if (start) { |
d304820a1 UBIFS: switch to ... |
537 538 |
err = ubifs_leb_read(c, lnum, sleb->buf, 0, start, 1); |
1e51764a3 UBIFS: add new fl... |
539 540 541 542 543 544 545 546 547 548 549 550 551 |
if (err) return err; } /* Pad to min_io_size */ if (len > endpt) { int pad_len = len - ALIGN(endpt, 8); if (pad_len > 0) { void *buf = sleb->buf + len - pad_len; ubifs_pad(c, buf, pad_len); } } |
d3b2578f5 UBIFS: switch to ... |
552 553 |
err = ubifs_leb_change(c, lnum, sleb->buf, len, UBI_UNKNOWN); |
1e51764a3 UBIFS: add new fl... |
554 555 556 557 558 559 560 561 |
if (err) return err; } } return 0; } /** |
da8b94ea6 UBIFS: fix recove... |
562 |
* drop_last_group - drop the last group of nodes. |
1e51764a3 UBIFS: add new fl... |
563 564 565 |
* @sleb: scanned LEB information * @offs: offset of dropped nodes is returned here * |
bbf2b37a9 UBIFS: fix extrem... |
566 |
* This is a helper function for 'ubifs_recover_leb()' which drops the last |
da8b94ea6 UBIFS: fix recove... |
567 |
* group of nodes of the scanned LEB. |
1e51764a3 UBIFS: add new fl... |
568 |
*/ |
da8b94ea6 UBIFS: fix recove... |
569 |
static void drop_last_group(struct ubifs_scan_leb *sleb, int *offs) |
1e51764a3 UBIFS: add new fl... |
570 |
{ |
1e51764a3 UBIFS: add new fl... |
571 572 573 574 575 576 577 578 |
while (!list_empty(&sleb->nodes)) { struct ubifs_scan_node *snod; struct ubifs_ch *ch; snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node, list); ch = snod->node; if (ch->group_type != UBIFS_IN_NODE_GROUP) |
da8b94ea6 UBIFS: fix recove... |
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 |
break; dbg_rcvry("dropping grouped node at %d:%d", sleb->lnum, snod->offs); *offs = snod->offs; list_del(&snod->list); kfree(snod); sleb->nodes_cnt -= 1; } } /** * drop_last_node - drop the last node. * @sleb: scanned LEB information * @offs: offset of dropped nodes is returned here * @grouped: non-zero if whole group of nodes have to be dropped * * This is a helper function for 'ubifs_recover_leb()' which drops the last * node of the scanned LEB. */ static void drop_last_node(struct ubifs_scan_leb *sleb, int *offs) { struct ubifs_scan_node *snod; if (!list_empty(&sleb->nodes)) { snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node, list); dbg_rcvry("dropping last node at %d:%d", sleb->lnum, snod->offs); |
1e51764a3 UBIFS: add new fl... |
608 609 610 611 |
*offs = snod->offs; list_del(&snod->list); kfree(snod); sleb->nodes_cnt -= 1; |
1e51764a3 UBIFS: add new fl... |
612 |
} |
1e51764a3 UBIFS: add new fl... |
613 614 615 616 617 618 619 620 |
} /** * ubifs_recover_leb - scan and recover a LEB. * @c: UBIFS file-system description object * @lnum: LEB number * @offs: offset * @sbuf: LEB-sized buffer to use |
efcfde54c UBIFS: amend ubif... |
621 622 |
* @jhead: journal head number this LEB belongs to (%-1 if the LEB does not * belong to any journal head) |
1e51764a3 UBIFS: add new fl... |
623 624 625 |
* * This function does a scan of a LEB, but caters for errors that might have * been caused by the unclean unmount from which we are attempting to recover. |
ed43f2f06 UBIFS: small amen... |
626 627 |
* Returns %0 in case of success, %-EUCLEAN if an unrecoverable corruption is * found, and a negative error code in case of failure. |
1e51764a3 UBIFS: add new fl... |
628 629 |
*/ struct ubifs_scan_leb *ubifs_recover_leb(struct ubifs_info *c, int lnum, |
efcfde54c UBIFS: amend ubif... |
630 |
int offs, void *sbuf, int jhead) |
1e51764a3 UBIFS: add new fl... |
631 |
{ |
bbf2b37a9 UBIFS: fix extrem... |
632 |
int ret = 0, err, len = c->leb_size - offs, start = offs, min_io_unit; |
efcfde54c UBIFS: amend ubif... |
633 |
int grouped = jhead == -1 ? 0 : c->jheads[jhead].grouped; |
1e51764a3 UBIFS: add new fl... |
634 635 |
struct ubifs_scan_leb *sleb; void *buf = sbuf + offs; |
efcfde54c UBIFS: amend ubif... |
636 |
dbg_rcvry("%d:%d, jhead %d, grouped %d", lnum, offs, jhead, grouped); |
1e51764a3 UBIFS: add new fl... |
637 638 639 640 |
sleb = ubifs_start_scan(c, lnum, offs, sbuf); if (IS_ERR(sleb)) return sleb; |
bbf2b37a9 UBIFS: fix extrem... |
641 |
ubifs_assert(len >= 8); |
1e51764a3 UBIFS: add new fl... |
642 |
while (len >= 8) { |
1e51764a3 UBIFS: add new fl... |
643 644 645 646 647 648 649 650 651 |
dbg_scan("look at LEB %d:%d (%d bytes left)", lnum, offs, len); cond_resched(); /* * Scan quietly until there is an error from which we cannot * recover */ |
ab75950b1 UBIFS: supress fa... |
652 |
ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 1); |
1e51764a3 UBIFS: add new fl... |
653 654 655 656 657 658 659 660 661 662 663 664 |
if (ret == SCANNED_A_NODE) { /* A valid node, and not a padding node */ struct ubifs_ch *ch = buf; int node_len; err = ubifs_add_snod(c, sleb, buf, offs); if (err) goto error; node_len = ALIGN(le32_to_cpu(ch->len), 8); offs += node_len; buf += node_len; len -= node_len; |
617992069 UBIFS: clean up L... |
665 |
} else if (ret > 0) { |
1e51764a3 UBIFS: add new fl... |
666 667 668 669 |
/* Padding bytes or a valid padding node */ offs += ret; buf += ret; len -= ret; |
617992069 UBIFS: clean up L... |
670 671 672 673 674 |
} else if (ret == SCANNED_EMPTY_SPACE || ret == SCANNED_GARBAGE || ret == SCANNED_A_BAD_PAD_NODE || ret == SCANNED_A_CORRUPT_NODE) { dbg_rcvry("found corruption - %d", ret); |
1e51764a3 UBIFS: add new fl... |
675 |
break; |
617992069 UBIFS: clean up L... |
676 677 |
} else { dbg_err("unexpected return value %d", ret); |
ed43f2f06 UBIFS: small amen... |
678 679 |
err = -EINVAL; goto error; |
1e51764a3 UBIFS: add new fl... |
680 681 |
} } |
617992069 UBIFS: clean up L... |
682 |
if (ret == SCANNED_GARBAGE || ret == SCANNED_A_BAD_PAD_NODE) { |
43e070738 UBIFS: simplify L... |
683 |
if (!is_last_write(c, buf, offs)) |
617992069 UBIFS: clean up L... |
684 685 |
goto corrupted_rescan; } else if (ret == SCANNED_A_CORRUPT_NODE) { |
43e070738 UBIFS: simplify L... |
686 |
if (!no_more_nodes(c, buf, len, lnum, offs)) |
617992069 UBIFS: clean up L... |
687 688 |
goto corrupted_rescan; } else if (!is_empty(buf, len)) { |
43e070738 UBIFS: simplify L... |
689 |
if (!is_last_write(c, buf, offs)) { |
061125476 UBIFS: fix corrup... |
690 |
int corruption = first_non_ff(buf, len); |
be7b42a5c UBIFS: describe U... |
691 692 693 694 |
/* * See header comment for this file for more * explanations about the reasons we have this check. */ |
061125476 UBIFS: fix corrup... |
695 696 697 |
ubifs_err("corrupt empty space LEB %d:%d, corruption " "starts at %d", lnum, offs, corruption); /* Make sure we dump interesting non-0xFF data */ |
10ac27970 UBIFS: fix LEB nu... |
698 |
offs += corruption; |
061125476 UBIFS: fix corrup... |
699 |
buf += corruption; |
1e51764a3 UBIFS: add new fl... |
700 701 702 |
goto corrupted; } } |
bbf2b37a9 UBIFS: fix extrem... |
703 704 705 706 707 708 |
min_io_unit = round_down(offs, c->min_io_size); if (grouped) /* * If nodes are grouped, always drop the incomplete group at * the end. */ |
da8b94ea6 UBIFS: fix recove... |
709 |
drop_last_group(sleb, &offs); |
bbf2b37a9 UBIFS: fix extrem... |
710 |
|
da8b94ea6 UBIFS: fix recove... |
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 |
if (jhead == GCHD) { /* * If this LEB belongs to the GC head then while we are in the * middle of the same min. I/O unit keep dropping nodes. So * basically, what we want is to make sure that the last min. * I/O unit where we saw the corruption is dropped completely * with all the uncorrupted nodes which may possibly sit there. * * In other words, let's name the min. I/O unit where the * corruption starts B, and the previous min. I/O unit A. The * below code tries to deal with a situation when half of B * contains valid nodes or the end of a valid node, and the * second half of B contains corrupted data or garbage. This * means that UBIFS had been writing to B just before the power * cut happened. I do not know how realistic is this scenario * that half of the min. I/O unit had been written successfully * and the other half not, but this is possible in our 'failure * mode emulation' infrastructure at least. * * So what is the problem, why we need to drop those nodes? Why * can't we just clean-up the second half of B by putting a * padding node there? We can, and this works fine with one * exception which was reproduced with power cut emulation * testing and happens extremely rarely. * * Imagine the file-system is full, we run GC which starts * moving valid nodes from LEB X to LEB Y (obviously, LEB Y is * the current GC head LEB). The @c->gc_lnum is -1, which means * that GC will retain LEB X and will try to continue. Imagine * that LEB X is currently the dirtiest LEB, and the amount of * used space in LEB Y is exactly the same as amount of free * space in LEB X. * * And a power cut happens when nodes are moved from LEB X to * LEB Y. We are here trying to recover LEB Y which is the GC * head LEB. We find the min. I/O unit B as described above. * Then we clean-up LEB Y by padding min. I/O unit. And later * 'ubifs_rcvry_gc_commit()' function fails, because it cannot * find a dirty LEB which could be GC'd into LEB Y! Even LEB X * does not match because the amount of valid nodes there does * not fit the free space in LEB Y any more! And this is * because of the padding node which we added to LEB Y. The * user-visible effect of this which I once observed and * analysed is that we cannot mount the file-system with * -ENOSPC error. * * So obviously, to make sure that situation does not happen we * should free min. I/O unit B in LEB Y completely and the last * used min. I/O unit in LEB Y should be A. This is basically * what the below code tries to do. */ while (offs > min_io_unit) drop_last_node(sleb, &offs); } |
bbf2b37a9 UBIFS: fix extrem... |
765 766 767 |
buf = sbuf + offs; len = c->leb_size - offs; |
1e51764a3 UBIFS: add new fl... |
768 |
|
43e070738 UBIFS: simplify L... |
769 |
clean_buf(c, &buf, lnum, &offs, &len); |
1e51764a3 UBIFS: add new fl... |
770 |
ubifs_end_scan(c, sleb, lnum, offs); |
7c47bfd0d UBIFS: always cle... |
771 772 773 |
err = fix_unclean_leb(c, sleb, start); if (err) goto error; |
1e51764a3 UBIFS: add new fl... |
774 775 |
return sleb; |
617992069 UBIFS: clean up L... |
776 777 778 779 |
corrupted_rescan: /* Re-scan the corrupted data with verbose messages */ dbg_err("corruptio %d", ret); ubifs_scan_a_node(c, buf, len, lnum, offs, 1); |
1e51764a3 UBIFS: add new fl... |
780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 |
corrupted: ubifs_scanned_corruption(c, lnum, offs, buf); err = -EUCLEAN; error: ubifs_err("LEB %d scanning failed", lnum); ubifs_scan_destroy(sleb); return ERR_PTR(err); } /** * get_cs_sqnum - get commit start sequence number. * @c: UBIFS file-system description object * @lnum: LEB number of commit start node * @offs: offset of commit start node * @cs_sqnum: commit start sequence number is returned here * * This function returns %0 on success and a negative error code on failure. */ static int get_cs_sqnum(struct ubifs_info *c, int lnum, int offs, unsigned long long *cs_sqnum) { struct ubifs_cs_node *cs_node = NULL; int err, ret; dbg_rcvry("at %d:%d", lnum, offs); cs_node = kmalloc(UBIFS_CS_NODE_SZ, GFP_KERNEL); if (!cs_node) return -ENOMEM; if (c->leb_size - offs < UBIFS_CS_NODE_SZ) goto out_err; |
d304820a1 UBIFS: switch to ... |
810 811 |
err = ubifs_leb_read(c, lnum, (void *)cs_node, offs, UBIFS_CS_NODE_SZ, 0); |
1e51764a3 UBIFS: add new fl... |
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 |
if (err && err != -EBADMSG) goto out_free; ret = ubifs_scan_a_node(c, cs_node, UBIFS_CS_NODE_SZ, lnum, offs, 0); if (ret != SCANNED_A_NODE) { dbg_err("Not a valid node"); goto out_err; } if (cs_node->ch.node_type != UBIFS_CS_NODE) { dbg_err("Node a CS node, type is %d", cs_node->ch.node_type); goto out_err; } if (le64_to_cpu(cs_node->cmt_no) != c->cmt_no) { dbg_err("CS node cmt_no %llu != current cmt_no %llu", (unsigned long long)le64_to_cpu(cs_node->cmt_no), c->cmt_no); goto out_err; } *cs_sqnum = le64_to_cpu(cs_node->ch.sqnum); dbg_rcvry("commit start sqnum %llu", *cs_sqnum); kfree(cs_node); return 0; out_err: err = -EINVAL; out_free: ubifs_err("failed to get CS sqnum"); kfree(cs_node); return err; } /** * ubifs_recover_log_leb - scan and recover a log LEB. * @c: UBIFS file-system description object * @lnum: LEB number * @offs: offset * @sbuf: LEB-sized buffer to use * * This function does a scan of a LEB, but caters for errors that might have |
7d08ae3c9 UBIFS: add a comm... |
850 851 |
* been caused by unclean reboots from which we are attempting to recover * (assume that only the last log LEB can be corrupted by an unclean reboot). |
1e51764a3 UBIFS: add new fl... |
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 |
* * This function returns %0 on success and a negative error code on failure. */ struct ubifs_scan_leb *ubifs_recover_log_leb(struct ubifs_info *c, int lnum, int offs, void *sbuf) { struct ubifs_scan_leb *sleb; int next_lnum; dbg_rcvry("LEB %d", lnum); next_lnum = lnum + 1; if (next_lnum >= UBIFS_LOG_LNUM + c->log_lebs) next_lnum = UBIFS_LOG_LNUM; if (next_lnum != c->ltail_lnum) { /* * We can only recover at the end of the log, so check that the * next log LEB is empty or out of date. */ |
348709bad UBIFS: do not pri... |
870 |
sleb = ubifs_scan(c, next_lnum, 0, sbuf, 0); |
1e51764a3 UBIFS: add new fl... |
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 |
if (IS_ERR(sleb)) return sleb; if (sleb->nodes_cnt) { struct ubifs_scan_node *snod; unsigned long long cs_sqnum = c->cs_sqnum; snod = list_entry(sleb->nodes.next, struct ubifs_scan_node, list); if (cs_sqnum == 0) { int err; err = get_cs_sqnum(c, lnum, offs, &cs_sqnum); if (err) { ubifs_scan_destroy(sleb); return ERR_PTR(err); } } if (snod->sqnum > cs_sqnum) { ubifs_err("unrecoverable log corruption " "in LEB %d", lnum); ubifs_scan_destroy(sleb); return ERR_PTR(-EUCLEAN); } } ubifs_scan_destroy(sleb); } |
efcfde54c UBIFS: amend ubif... |
897 |
return ubifs_recover_leb(c, lnum, offs, sbuf, -1); |
1e51764a3 UBIFS: add new fl... |
898 899 900 901 902 903 904 905 906 907 908 909 910 |
} /** * recover_head - recover a head. * @c: UBIFS file-system description object * @lnum: LEB number of head to recover * @offs: offset of head to recover * @sbuf: LEB-sized buffer to use * * This function ensures that there is no data on the flash at a head location. * * This function returns %0 on success and a negative error code on failure. */ |
83cef708c UBIFS: introduce ... |
911 |
static int recover_head(struct ubifs_info *c, int lnum, int offs, void *sbuf) |
1e51764a3 UBIFS: add new fl... |
912 |
{ |
2765df7da UBIFS: use max_wr... |
913 |
int len = c->max_write_size, err; |
1e51764a3 UBIFS: add new fl... |
914 |
|
1e51764a3 UBIFS: add new fl... |
915 916 917 918 919 920 921 |
if (offs + len > c->leb_size) len = c->leb_size - offs; if (!len) return 0; /* Read at the head location and check it is empty flash */ |
d304820a1 UBIFS: switch to ... |
922 |
err = ubifs_leb_read(c, lnum, sbuf, offs, len, 1); |
431102fed UBIFS: clean up f... |
923 |
if (err || !is_empty(sbuf, len)) { |
1e51764a3 UBIFS: add new fl... |
924 925 926 |
dbg_rcvry("cleaning head at %d:%d", lnum, offs); if (offs == 0) return ubifs_leb_unmap(c, lnum); |
d304820a1 UBIFS: switch to ... |
927 |
err = ubifs_leb_read(c, lnum, sbuf, 0, offs, 1); |
1e51764a3 UBIFS: add new fl... |
928 929 |
if (err) return err; |
d3b2578f5 UBIFS: switch to ... |
930 |
return ubifs_leb_change(c, lnum, sbuf, offs, UBI_UNKNOWN); |
1e51764a3 UBIFS: add new fl... |
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 |
} return 0; } /** * ubifs_recover_inl_heads - recover index and LPT heads. * @c: UBIFS file-system description object * @sbuf: LEB-sized buffer to use * * This function ensures that there is no data on the flash at the index and * LPT head locations. * * This deals with the recovery of a half-completed journal commit. UBIFS is * careful never to overwrite the last version of the index or the LPT. Because * the index and LPT are wandering trees, data from a half-completed commit will * not be referenced anywhere in UBIFS. The data will be either in LEBs that are * assumed to be empty and will be unmapped anyway before use, or in the index * and LPT heads. * * This function returns %0 on success and a negative error code on failure. */ |
83cef708c UBIFS: introduce ... |
953 |
int ubifs_recover_inl_heads(struct ubifs_info *c, void *sbuf) |
1e51764a3 UBIFS: add new fl... |
954 955 |
{ int err; |
2ef13294d UBIFS: introduce ... |
956 |
ubifs_assert(!c->ro_mount || c->remounting_rw); |
1e51764a3 UBIFS: add new fl... |
957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 |
dbg_rcvry("checking index head at %d:%d", c->ihead_lnum, c->ihead_offs); err = recover_head(c, c->ihead_lnum, c->ihead_offs, sbuf); if (err) return err; dbg_rcvry("checking LPT head at %d:%d", c->nhead_lnum, c->nhead_offs); err = recover_head(c, c->nhead_lnum, c->nhead_offs, sbuf); if (err) return err; return 0; } /** |
7606f85a7 UBIFS: fix the da... |
972 |
* clean_an_unclean_leb - read and write a LEB to remove corruption. |
1e51764a3 UBIFS: add new fl... |
973 974 975 976 977 978 979 980 981 982 |
* @c: UBIFS file-system description object * @ucleb: unclean LEB information * @sbuf: LEB-sized buffer to use * * This function reads a LEB up to a point pre-determined by the mount recovery, * checks the nodes, and writes the result back to the flash, thereby cleaning * off any following corruption, or non-fatal ECC errors. * * This function returns %0 on success and a negative error code on failure. */ |
83cef708c UBIFS: introduce ... |
983 |
static int clean_an_unclean_leb(struct ubifs_info *c, |
1e51764a3 UBIFS: add new fl... |
984 985 986 987 988 989 990 991 992 993 994 995 996 997 |
struct ubifs_unclean_leb *ucleb, void *sbuf) { int err, lnum = ucleb->lnum, offs = 0, len = ucleb->endpt, quiet = 1; void *buf = sbuf; dbg_rcvry("LEB %d len %d", lnum, len); if (len == 0) { /* Nothing to read, just unmap it */ err = ubifs_leb_unmap(c, lnum); if (err) return err; return 0; } |
d304820a1 UBIFS: switch to ... |
998 |
err = ubifs_leb_read(c, lnum, buf, offs, len, 0); |
1e51764a3 UBIFS: add new fl... |
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 |
if (err && err != -EBADMSG) return err; while (len >= 8) { int ret; cond_resched(); /* Scan quietly until there is an error */ ret = ubifs_scan_a_node(c, buf, len, lnum, offs, quiet); if (ret == SCANNED_A_NODE) { /* A valid node, and not a padding node */ struct ubifs_ch *ch = buf; int node_len; node_len = ALIGN(le32_to_cpu(ch->len), 8); offs += node_len; buf += node_len; len -= node_len; continue; } if (ret > 0) { /* Padding bytes or a valid padding node */ offs += ret; buf += ret; len -= ret; continue; } if (ret == SCANNED_EMPTY_SPACE) { ubifs_err("unexpected empty space at %d:%d", lnum, offs); return -EUCLEAN; } if (quiet) { /* Redo the last scan but noisily */ quiet = 0; continue; } ubifs_scanned_corruption(c, lnum, offs, buf); return -EUCLEAN; } /* Pad to min_io_size */ len = ALIGN(ucleb->endpt, c->min_io_size); if (len > ucleb->endpt) { int pad_len = len - ALIGN(ucleb->endpt, 8); if (pad_len > 0) { buf = c->sbuf + len - pad_len; ubifs_pad(c, buf, pad_len); } } /* Write back the LEB atomically */ |
d3b2578f5 UBIFS: switch to ... |
1058 |
err = ubifs_leb_change(c, lnum, sbuf, len, UBI_UNKNOWN); |
1e51764a3 UBIFS: add new fl... |
1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 |
if (err) return err; dbg_rcvry("cleaned LEB %d", lnum); return 0; } /** * ubifs_clean_lebs - clean LEBs recovered during read-only mount. * @c: UBIFS file-system description object * @sbuf: LEB-sized buffer to use * * This function cleans a LEB identified during recovery that needs to be * written but was not because UBIFS was mounted read-only. This happens when * remounting to read-write mode. * * This function returns %0 on success and a negative error code on failure. */ |
83cef708c UBIFS: introduce ... |
1078 |
int ubifs_clean_lebs(struct ubifs_info *c, void *sbuf) |
1e51764a3 UBIFS: add new fl... |
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 |
{ dbg_rcvry("recovery"); while (!list_empty(&c->unclean_leb_list)) { struct ubifs_unclean_leb *ucleb; int err; ucleb = list_entry(c->unclean_leb_list.next, struct ubifs_unclean_leb, list); err = clean_an_unclean_leb(c, ucleb, sbuf); if (err) return err; list_del(&ucleb->list); kfree(ucleb); } return 0; } /** |
447442139 UBIFS: split ubif... |
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 |
* grab_empty_leb - grab an empty LEB to use as GC LEB and run commit. * @c: UBIFS file-system description object * * This is a helper function for 'ubifs_rcvry_gc_commit()' which grabs an empty * LEB to be used as GC LEB (@c->gc_lnum), and then runs the commit. Returns * zero in case of success and a negative error code in case of failure. */ static int grab_empty_leb(struct ubifs_info *c) { int lnum, err; /* * Note, it is very important to first search for an empty LEB and then * run the commit, not vice-versa. The reason is that there might be * only one empty LEB at the moment, the one which has been the * @c->gc_lnum just before the power cut happened. During the regular * UBIFS operation (not now) @c->gc_lnum is marked as "taken", so no * one but GC can grab it. But at this moment this single empty LEB is * not marked as taken, so if we run commit - what happens? Right, the * commit will grab it and write the index there. Remember that the * index always expands as long as there is free space, and it only * starts consolidating when we run out of space. * * IOW, if we run commit now, we might not be able to find a free LEB * after this. */ lnum = ubifs_find_free_leb_for_idx(c); if (lnum < 0) { dbg_err("could not find an empty LEB"); dbg_dump_lprops(c); dbg_dump_budg(c, &c->bi); return lnum; } /* Reset the index flag */ err = ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0, LPROPS_INDEX, 0); if (err) return err; c->gc_lnum = lnum; dbg_rcvry("found empty LEB %d, run commit", lnum); return ubifs_run_commit(c); } /** |
1e51764a3 UBIFS: add new fl... |
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 |
* ubifs_rcvry_gc_commit - recover the GC LEB number and run the commit. * @c: UBIFS file-system description object * * Out-of-place garbage collection requires always one empty LEB with which to * start garbage collection. The LEB number is recorded in c->gc_lnum and is * written to the master node on unmounting. In the case of an unclean unmount * the value of gc_lnum recorded in the master node is out of date and cannot * be used. Instead, recovery must allocate an empty LEB for this purpose. * However, there may not be enough empty space, in which case it must be * possible to GC the dirtiest LEB into the GC head LEB. * * This function also runs the commit which causes the TNC updates from * size-recovery and orphans to be written to the flash. That is important to * ensure correct replay order for subsequent mounts. * * This function returns %0 on success and a negative error code on failure. */ int ubifs_rcvry_gc_commit(struct ubifs_info *c) { struct ubifs_wbuf *wbuf = &c->jheads[GCHD].wbuf; struct ubifs_lprops lp; |
fe79c05f0 UBIFS: refactor u... |
1165 |
int err; |
1e51764a3 UBIFS: add new fl... |
1166 |
|
c839e2976 UBIFS: improve de... |
1167 |
dbg_rcvry("GC head LEB %d, offs %d", wbuf->lnum, wbuf->offs); |
1e51764a3 UBIFS: add new fl... |
1168 |
c->gc_lnum = -1; |
c839e2976 UBIFS: improve de... |
1169 |
if (wbuf->lnum == -1 || wbuf->offs == c->leb_size) |
447442139 UBIFS: split ubif... |
1170 |
return grab_empty_leb(c); |
fe79c05f0 UBIFS: refactor u... |
1171 |
|
1e51764a3 UBIFS: add new fl... |
1172 1173 |
err = ubifs_find_dirty_leb(c, &lp, wbuf->offs, 2); if (err) { |
fe79c05f0 UBIFS: refactor u... |
1174 1175 1176 1177 1178 |
if (err != -ENOSPC) return err; dbg_rcvry("could not find a dirty LEB"); return grab_empty_leb(c); |
1e51764a3 UBIFS: add new fl... |
1179 |
} |
2405f5948 UBIFS: remove dup... |
1180 |
|
1e51764a3 UBIFS: add new fl... |
1181 |
ubifs_assert(!(lp.flags & LPROPS_INDEX)); |
bcdca3e10 UBIFS: remove dea... |
1182 |
ubifs_assert(lp.free + lp.dirty >= wbuf->offs); |
2405f5948 UBIFS: remove dup... |
1183 |
|
1e51764a3 UBIFS: add new fl... |
1184 |
/* |
1e51764a3 UBIFS: add new fl... |
1185 1186 1187 1188 1189 1190 1191 |
* We run the commit before garbage collection otherwise subsequent * mounts will see the GC and orphan deletion in a different order. */ dbg_rcvry("committing"); err = ubifs_run_commit(c); if (err) return err; |
fe79c05f0 UBIFS: refactor u... |
1192 1193 |
dbg_rcvry("GC'ing LEB %d", lp.lnum); |
1e51764a3 UBIFS: add new fl... |
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 |
mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead); err = ubifs_garbage_collect_leb(c, &lp); if (err >= 0) { int err2 = ubifs_wbuf_sync_nolock(wbuf); if (err2) err = err2; } mutex_unlock(&wbuf->io_mutex); if (err < 0) { dbg_err("GC failed, error %d", err); if (err == -EAGAIN) err = -EINVAL; return err; } |
fe79c05f0 UBIFS: refactor u... |
1209 1210 1211 |
ubifs_assert(err == LEB_RETAINED); if (err != LEB_RETAINED) |
1e51764a3 UBIFS: add new fl... |
1212 |
return -EINVAL; |
fe79c05f0 UBIFS: refactor u... |
1213 |
|
1e51764a3 UBIFS: add new fl... |
1214 1215 1216 |
err = ubifs_leb_unmap(c, c->gc_lnum); if (err) return err; |
fe79c05f0 UBIFS: refactor u... |
1217 1218 |
dbg_rcvry("allocated LEB %d for GC", lp.lnum); |
1e51764a3 UBIFS: add new fl... |
1219 |
return 0; |
1e51764a3 UBIFS: add new fl... |
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 |
} /** * struct size_entry - inode size information for recovery. * @rb: link in the RB-tree of sizes * @inum: inode number * @i_size: size on inode * @d_size: maximum size based on data nodes * @exists: indicates whether the inode exists * @inode: inode if pinned in memory awaiting rw mode to fix it */ struct size_entry { struct rb_node rb; ino_t inum; loff_t i_size; loff_t d_size; int exists; struct inode *inode; }; /** * add_ino - add an entry to the size tree. * @c: UBIFS file-system description object * @inum: inode number * @i_size: size on inode * @d_size: maximum size based on data nodes * @exists: indicates whether the inode exists */ static int add_ino(struct ubifs_info *c, ino_t inum, loff_t i_size, loff_t d_size, int exists) { struct rb_node **p = &c->size_tree.rb_node, *parent = NULL; struct size_entry *e; while (*p) { parent = *p; e = rb_entry(parent, struct size_entry, rb); if (inum < e->inum) p = &(*p)->rb_left; else p = &(*p)->rb_right; } e = kzalloc(sizeof(struct size_entry), GFP_KERNEL); if (!e) return -ENOMEM; e->inum = inum; e->i_size = i_size; e->d_size = d_size; e->exists = exists; rb_link_node(&e->rb, parent, p); rb_insert_color(&e->rb, &c->size_tree); return 0; } /** * find_ino - find an entry on the size tree. * @c: UBIFS file-system description object * @inum: inode number */ static struct size_entry *find_ino(struct ubifs_info *c, ino_t inum) { struct rb_node *p = c->size_tree.rb_node; struct size_entry *e; while (p) { e = rb_entry(p, struct size_entry, rb); if (inum < e->inum) p = p->rb_left; else if (inum > e->inum) p = p->rb_right; else return e; } return NULL; } /** * remove_ino - remove an entry from the size tree. * @c: UBIFS file-system description object * @inum: inode number */ static void remove_ino(struct ubifs_info *c, ino_t inum) { struct size_entry *e = find_ino(c, inum); if (!e) return; rb_erase(&e->rb, &c->size_tree); kfree(e); } /** * ubifs_destroy_size_tree - free resources related to the size tree. * @c: UBIFS file-system description object */ void ubifs_destroy_size_tree(struct ubifs_info *c) { struct rb_node *this = c->size_tree.rb_node; struct size_entry *e; while (this) { if (this->rb_left) { this = this->rb_left; continue; } else if (this->rb_right) { this = this->rb_right; continue; } e = rb_entry(this, struct size_entry, rb); if (e->inode) iput(e->inode); this = rb_parent(this); if (this) { if (this->rb_left == &e->rb) this->rb_left = NULL; else this->rb_right = NULL; } kfree(e); } c->size_tree = RB_ROOT; } /** * ubifs_recover_size_accum - accumulate inode sizes for recovery. * @c: UBIFS file-system description object * @key: node key * @deletion: node is for a deletion * @new_size: inode size * * This function has two purposes: * 1) to ensure there are no data nodes that fall outside the inode size * 2) to ensure there are no data nodes for inodes that do not exist * To accomplish those purposes, a rb-tree is constructed containing an entry * for each inode number in the journal that has not been deleted, and recording * the size from the inode node, the maximum size of any data node (also altered * by truncations) and a flag indicating a inode number for which no inode node * was present in the journal. * * Note that there is still the possibility that there are data nodes that have * been committed that are beyond the inode size, however the only way to find * them would be to scan the entire index. Alternatively, some provision could * be made to record the size of inodes at the start of commit, which would seem * very cumbersome for a scenario that is quite unlikely and the only negative * consequence of which is wasted space. * * This functions returns %0 on success and a negative error code on failure. */ int ubifs_recover_size_accum(struct ubifs_info *c, union ubifs_key *key, int deletion, loff_t new_size) { ino_t inum = key_inum(c, key); struct size_entry *e; int err; switch (key_type(c, key)) { case UBIFS_INO_KEY: if (deletion) remove_ino(c, inum); else { e = find_ino(c, inum); if (e) { e->i_size = new_size; e->exists = 1; } else { err = add_ino(c, inum, new_size, 0, 1); if (err) return err; } } break; case UBIFS_DATA_KEY: e = find_ino(c, inum); if (e) { if (new_size > e->d_size) e->d_size = new_size; } else { err = add_ino(c, inum, 0, new_size, 0); if (err) return err; } break; case UBIFS_TRUN_KEY: e = find_ino(c, inum); if (e) e->d_size = new_size; break; } return 0; } /** * fix_size_in_place - fix inode size in place on flash. * @c: UBIFS file-system description object * @e: inode size information for recovery */ static int fix_size_in_place(struct ubifs_info *c, struct size_entry *e) { struct ubifs_ino_node *ino = c->sbuf; unsigned char *p; union ubifs_key key; int err, lnum, offs, len; loff_t i_size; uint32_t crc; /* Locate the inode node LEB number and offset */ ino_key_init(c, &key, e->inum); err = ubifs_tnc_locate(c, &key, ino, &lnum, &offs); if (err) goto out; /* * If the size recorded on the inode node is greater than the size that * was calculated from nodes in the journal then don't change the inode. */ i_size = le64_to_cpu(ino->size); if (i_size >= e->d_size) return 0; /* Read the LEB */ |
d304820a1 UBIFS: switch to ... |
1442 |
err = ubifs_leb_read(c, lnum, c->sbuf, 0, c->leb_size, 1); |
1e51764a3 UBIFS: add new fl... |
1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 |
if (err) goto out; /* Change the size field and recalculate the CRC */ ino = c->sbuf + offs; ino->size = cpu_to_le64(e->d_size); len = le32_to_cpu(ino->ch.len); crc = crc32(UBIFS_CRC32_INIT, (void *)ino + 8, len - 8); ino->ch.crc = cpu_to_le32(crc); /* Work out where data in the LEB ends and free space begins */ p = c->sbuf; len = c->leb_size - 1; while (p[len] == 0xff) len -= 1; len = ALIGN(len + 1, c->min_io_size); /* Atomically write the fixed LEB back again */ |
d3b2578f5 UBIFS: switch to ... |
1458 |
err = ubifs_leb_change(c, lnum, c->sbuf, len, UBI_UNKNOWN); |
1e51764a3 UBIFS: add new fl... |
1459 1460 |
if (err) goto out; |
69f8a75a7 UBIFS: remove an ... |
1461 |
dbg_rcvry("inode %lu at %d:%d size %lld -> %lld", |
e84461ad9 UBIFS: fix compil... |
1462 |
(unsigned long)e->inum, lnum, offs, i_size, e->d_size); |
1e51764a3 UBIFS: add new fl... |
1463 1464 1465 1466 |
return 0; out: ubifs_warn("inode %lu failed to fix size %lld -> %lld error %d", |
e84461ad9 UBIFS: fix compil... |
1467 |
(unsigned long)e->inum, e->i_size, e->d_size, err); |
1e51764a3 UBIFS: add new fl... |
1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 |
return err; } /** * ubifs_recover_size - recover inode size. * @c: UBIFS file-system description object * * This function attempts to fix inode size discrepancies identified by the * 'ubifs_recover_size_accum()' function. * * This functions returns %0 on success and a negative error code on failure. */ int ubifs_recover_size(struct ubifs_info *c) { struct rb_node *this = rb_first(&c->size_tree); while (this) { struct size_entry *e; int err; e = rb_entry(this, struct size_entry, rb); if (!e->exists) { union ubifs_key key; ino_key_init(c, &key, e->inum); err = ubifs_tnc_lookup(c, &key, c->sbuf); if (err && err != -ENOENT) return err; if (err == -ENOENT) { /* Remove data nodes that have no inode */ |
e84461ad9 UBIFS: fix compil... |
1498 1499 |
dbg_rcvry("removing ino %lu", (unsigned long)e->inum); |
1e51764a3 UBIFS: add new fl... |
1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 |
err = ubifs_tnc_remove_ino(c, e->inum); if (err) return err; } else { struct ubifs_ino_node *ino = c->sbuf; e->exists = 1; e->i_size = le64_to_cpu(ino->size); } } |
69f8a75a7 UBIFS: remove an ... |
1510 |
|
1e51764a3 UBIFS: add new fl... |
1511 |
if (e->exists && e->i_size < e->d_size) { |
69f8a75a7 UBIFS: remove an ... |
1512 |
if (c->ro_mount) { |
1e51764a3 UBIFS: add new fl... |
1513 1514 |
/* Fix the inode size and pin it in memory */ struct inode *inode; |
c1f1f91d2 UBIFS: fix inode ... |
1515 |
struct ubifs_inode *ui; |
1e51764a3 UBIFS: add new fl... |
1516 |
|
69f8a75a7 UBIFS: remove an ... |
1517 |
ubifs_assert(!e->inode); |
1e51764a3 UBIFS: add new fl... |
1518 1519 1520 |
inode = ubifs_iget(c->vfs_sb, e->inum); if (IS_ERR(inode)) return PTR_ERR(inode); |
c1f1f91d2 UBIFS: fix inode ... |
1521 1522 |
ui = ubifs_inode(inode); |
1e51764a3 UBIFS: add new fl... |
1523 1524 |
if (inode->i_size < e->d_size) { dbg_rcvry("ino %lu size %lld -> %lld", |
e84461ad9 UBIFS: fix compil... |
1525 |
(unsigned long)e->inum, |
4c9545200 UBIFS: fix debugg... |
1526 |
inode->i_size, e->d_size); |
1e51764a3 UBIFS: add new fl... |
1527 |
inode->i_size = e->d_size; |
c1f1f91d2 UBIFS: fix inode ... |
1528 1529 |
ui->ui_size = e->d_size; ui->synced_i_size = e->d_size; |
1e51764a3 UBIFS: add new fl... |
1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 |
e->inode = inode; this = rb_next(this); continue; } iput(inode); } else { /* Fix the size in place */ err = fix_size_in_place(c, e); if (err) return err; if (e->inode) iput(e->inode); } } |
69f8a75a7 UBIFS: remove an ... |
1544 |
|
1e51764a3 UBIFS: add new fl... |
1545 1546 1547 1548 |
this = rb_next(this); rb_erase(&e->rb, &c->size_tree); kfree(e); } |
69f8a75a7 UBIFS: remove an ... |
1549 |
|
1e51764a3 UBIFS: add new fl... |
1550 1551 |
return 0; } |