28
28
* UBIFS always cleans away all remnants of an unclean un-mount, so that
29
29
* errors do not accumulate. However UBIFS defers recovery if it is mounted
30
30
* read-only, and the flash is not modified in that case.
32
* The general UBIFS approach to the recovery is that it recovers from
33
* corruptions which could be caused by power cuts, but it refuses to recover
34
* from corruption caused by other reasons. And UBIFS tries to distinguish
35
* between these 2 reasons of corruptions and silently recover in the former
36
* case and loudly complain in the latter case.
38
* UBIFS writes only to erased LEBs, so it writes only to the flash space
39
* containing only 0xFFs. UBIFS also always writes strictly from the beginning
40
* of the LEB to the end. And UBIFS assumes that the underlying flash media
41
* writes in @c->max_write_size bytes at a time.
43
* Hence, if UBIFS finds a corrupted node at offset X, it expects only the min.
44
* I/O unit corresponding to offset X to contain corrupted data, all the
45
* following min. I/O units have to contain empty space (all 0xFFs). If this is
46
* not true, the corruption cannot be the result of a power cut, and UBIFS
33
50
#include <linux/crc32.h>
302
319
memcpy(c->rcvrd_mst_node, c->mst_node, UBIFS_MST_NODE_SZ);
322
* We had to recover the master node, which means there was an
323
* unclean reboot. However, it is possible that the master node
324
* is clean at this point, i.e., %UBIFS_MST_DIRTY is not set.
325
* E.g., consider the following chain of events:
327
* 1. UBIFS was cleanly unmounted, so the master node is clean
328
* 2. UBIFS is being mounted R/W and starts changing the master
329
* node in the first (%UBIFS_MST_LNUM). A power cut happens,
330
* so this LEB ends up with some amount of garbage at the
332
* 3. UBIFS is being mounted R/O. We reach this place and
333
* recover the master node from the second LEB
334
* (%UBIFS_MST_LNUM + 1). But we cannot update the media
335
* because we are being mounted R/O. We have to defer the
337
* 4. However, this master node (@c->mst_node) is marked as
338
* clean (since the step 1). And if we just return, the
339
* mount code will be confused and won't recover the master
340
* node when it is re-mounter R/W later.
342
* Thus, to force the recovery by marking the master node as
345
c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
304
347
/* Write the recovered master node */
305
348
c->max_sqnum = le64_to_cpu(mst->ch.sqnum) - 1;
362
405
* @offs: offset to check
364
407
* This function returns %1 if @offs was in the last write to the LEB whose data
365
* is in @buf, otherwise %0 is returned. The determination is made by checking
366
* for subsequent empty space starting from the next @c->min_io_size boundary.
408
* is in @buf, otherwise %0 is returned. The determination is made by checking
409
* for subsequent empty space starting from the next @c->max_write_size
368
412
static int is_last_write(const struct ubifs_info *c, void *buf, int offs)
374
* Round up to the next @c->min_io_size boundary i.e. @offs is in the
375
* last wbuf written. After that should be empty space.
418
* Round up to the next @c->max_write_size boundary i.e. @offs is in
419
* the last wbuf written. After that should be empty space.
377
empty_offs = ALIGN(offs + 1, c->min_io_size);
421
empty_offs = ALIGN(offs + 1, c->max_write_size);
378
422
check_len = c->leb_size - empty_offs;
379
423
p = buf + empty_offs - offs;
380
424
return is_empty(p, check_len);
429
473
int skip, dlen = le32_to_cpu(ch->len);
431
475
/* Check for empty space after the corrupt node's common header */
432
skip = ALIGN(offs + UBIFS_CH_SZ, c->min_io_size) - offs;
476
skip = ALIGN(offs + UBIFS_CH_SZ, c->max_write_size) - offs;
433
477
if (is_empty(buf + skip, len - skip))
443
487
/* Now we know the corrupt node's length we can skip over it */
444
skip = ALIGN(offs + dlen, c->min_io_size) - offs;
488
skip = ALIGN(offs + dlen, c->max_write_size) - offs;
445
489
/* After which there should be empty space */
446
490
if (is_empty(buf + skip, len - skip))
672
716
int corruption = first_non_ff(buf, len);
719
* See header comment for this file for more
720
* explanations about the reasons we have this check.
674
722
ubifs_err("corrupt empty space LEB %d:%d, corruption "
675
723
"starts at %d", lnum, offs, corruption);
676
724
/* Make sure we dump interesting non-0xFF data */
678
726
buf += corruption;
836
884
static int recover_head(const struct ubifs_info *c, int lnum, int offs,
887
int len = c->max_write_size, err;
841
if (c->min_io_size > 1)
842
len = c->min_io_size;
845
889
if (offs + len > c->leb_size)
846
890
len = c->leb_size - offs;