2 * This file is part of UBIFS.
4 * Copyright (C) 2006-2008 Nokia Corporation
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.
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
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
19 * Authors: Adrian Hunter
20 * Artem Bityutskiy (Битюцкий Артём)
24 * This file implements functions needed to recover from unclean un-mounts.
25 * When UBIFS is mounted, it checks a flag on the master node to determine if
26 * an un-mount was completed successfully. If not, the process of mounting
27 * incorporates additional checking and fixing of on-flash data structures.
28 * UBIFS always cleans away all remnants of an unclean un-mount, so that
29 * errors do not accumulate. However UBIFS defers recovery if it is mounted
30 * read-only, and the flash is not modified in that case.
33 #include <linux/crc32.h>
34 #include <linux/slab.h>
38 * is_empty - determine whether a buffer is empty (contains all 0xff).
39 * @buf: buffer to clean
40 * @len: length of buffer
42 * This function returns %1 if the buffer is empty (contains all 0xff) otherwise
45 static int is_empty(void *buf
, int len
)
50 for (i
= 0; i
< len
; i
++)
57 * first_non_ff - find offset of the first non-0xff byte.
58 * @buf: buffer to search in
59 * @len: length of buffer
61 * This function returns offset of the first non-0xff byte in @buf or %-1 if
62 * the buffer contains only 0xff bytes.
64 static int first_non_ff(void *buf
, int len
)
69 for (i
= 0; i
< len
; i
++)
76 * get_master_node - get the last valid master node allowing for corruption.
77 * @c: UBIFS file-system description object
79 * @pbuf: buffer containing the LEB read, is returned here
80 * @mst: master node, if found, is returned here
81 * @cor: corruption, if found, is returned here
83 * This function allocates a buffer, reads the LEB into it, and finds and
84 * returns the last valid master node allowing for one area of corruption.
85 * The corrupt area, if there is one, must be consistent with the assumption
86 * that it is the result of an unclean unmount while the master node was being
87 * written. Under those circumstances, it is valid to use the previously written
90 * This function returns %0 on success and a negative error code on failure.
92 static int get_master_node(const struct ubifs_info
*c
, int lnum
, void **pbuf
,
93 struct ubifs_mst_node
**mst
, void **cor
)
95 const int sz
= c
->mst_node_alsz
;
99 sbuf
= vmalloc(c
->leb_size
);
103 err
= ubi_read(c
->ubi
, lnum
, sbuf
, 0, c
->leb_size
);
104 if (err
&& err
!= -EBADMSG
)
107 /* Find the first position that is definitely not a node */
111 while (offs
+ UBIFS_MST_NODE_SZ
<= c
->leb_size
) {
112 struct ubifs_ch
*ch
= buf
;
114 if (le32_to_cpu(ch
->magic
) != UBIFS_NODE_MAGIC
)
120 /* See if there was a valid master node before that */
127 ret
= ubifs_scan_a_node(c
, buf
, len
, lnum
, offs
, 1);
128 if (ret
!= SCANNED_A_NODE
&& offs
) {
129 /* Could have been corruption so check one place back */
133 ret
= ubifs_scan_a_node(c
, buf
, len
, lnum
, offs
, 1);
134 if (ret
!= SCANNED_A_NODE
)
136 * We accept only one area of corruption because
137 * we are assuming that it was caused while
138 * trying to write a master node.
142 if (ret
== SCANNED_A_NODE
) {
143 struct ubifs_ch
*ch
= buf
;
145 if (ch
->node_type
!= UBIFS_MST_NODE
)
147 dbg_rcvry("found a master node at %d:%d", lnum
, offs
);
154 /* Check for corruption */
155 if (offs
< c
->leb_size
) {
156 if (!is_empty(buf
, min_t(int, len
, sz
))) {
158 dbg_rcvry("found corruption at %d:%d", lnum
, offs
);
164 /* Check remaining empty space */
165 if (offs
< c
->leb_size
)
166 if (!is_empty(buf
, len
))
181 * write_rcvrd_mst_node - write recovered master node.
182 * @c: UBIFS file-system description object
185 * This function returns %0 on success and a negative error code on failure.
187 static int write_rcvrd_mst_node(struct ubifs_info
*c
,
188 struct ubifs_mst_node
*mst
)
190 int err
= 0, lnum
= UBIFS_MST_LNUM
, sz
= c
->mst_node_alsz
;
193 dbg_rcvry("recovery");
195 save_flags
= mst
->flags
;
196 mst
->flags
|= cpu_to_le32(UBIFS_MST_RCVRY
);
198 ubifs_prepare_node(c
, mst
, UBIFS_MST_NODE_SZ
, 1);
199 err
= ubi_leb_change(c
->ubi
, lnum
, mst
, sz
, UBI_SHORTTERM
);
202 err
= ubi_leb_change(c
->ubi
, lnum
+ 1, mst
, sz
, UBI_SHORTTERM
);
206 mst
->flags
= save_flags
;
211 * ubifs_recover_master_node - recover the master node.
212 * @c: UBIFS file-system description object
214 * This function recovers the master node from corruption that may occur due to
215 * an unclean unmount.
217 * This function returns %0 on success and a negative error code on failure.
219 int ubifs_recover_master_node(struct ubifs_info
*c
)
221 void *buf1
= NULL
, *buf2
= NULL
, *cor1
= NULL
, *cor2
= NULL
;
222 struct ubifs_mst_node
*mst1
= NULL
, *mst2
= NULL
, *mst
;
223 const int sz
= c
->mst_node_alsz
;
224 int err
, offs1
, offs2
;
226 dbg_rcvry("recovery");
228 err
= get_master_node(c
, UBIFS_MST_LNUM
, &buf1
, &mst1
, &cor1
);
232 err
= get_master_node(c
, UBIFS_MST_LNUM
+ 1, &buf2
, &mst2
, &cor2
);
237 offs1
= (void *)mst1
- buf1
;
238 if ((le32_to_cpu(mst1
->flags
) & UBIFS_MST_RCVRY
) &&
239 (offs1
== 0 && !cor1
)) {
241 * mst1 was written by recovery at offset 0 with no
244 dbg_rcvry("recovery recovery");
247 offs2
= (void *)mst2
- buf2
;
248 if (offs1
== offs2
) {
249 /* Same offset, so must be the same */
250 if (memcmp((void *)mst1
+ UBIFS_CH_SZ
,
251 (void *)mst2
+ UBIFS_CH_SZ
,
252 UBIFS_MST_NODE_SZ
- UBIFS_CH_SZ
))
255 } else if (offs2
+ sz
== offs1
) {
256 /* 1st LEB was written, 2nd was not */
260 } else if (offs1
== 0 && offs2
+ sz
>= c
->leb_size
) {
261 /* 1st LEB was unmapped and written, 2nd not */
269 * 2nd LEB was unmapped and about to be written, so
270 * there must be only one master node in the first LEB
273 if (offs1
!= 0 || cor1
)
281 * 1st LEB was unmapped and about to be written, so there must
282 * be no room left in 2nd LEB.
284 offs2
= (void *)mst2
- buf2
;
285 if (offs2
+ sz
+ sz
<= c
->leb_size
)
290 ubifs_msg("recovered master node from LEB %d",
291 (mst
== mst1
? UBIFS_MST_LNUM
: UBIFS_MST_LNUM
+ 1));
293 memcpy(c
->mst_node
, mst
, UBIFS_MST_NODE_SZ
);
296 /* Read-only mode. Keep a copy for switching to rw mode */
297 c
->rcvrd_mst_node
= kmalloc(sz
, GFP_KERNEL
);
298 if (!c
->rcvrd_mst_node
) {
302 memcpy(c
->rcvrd_mst_node
, c
->mst_node
, UBIFS_MST_NODE_SZ
);
304 /* Write the recovered master node */
305 c
->max_sqnum
= le64_to_cpu(mst
->ch
.sqnum
) - 1;
306 err
= write_rcvrd_mst_node(c
, c
->mst_node
);
319 ubifs_err("failed to recover master node");
321 dbg_err("dumping first master node");
322 dbg_dump_node(c
, mst1
);
325 dbg_err("dumping second master node");
326 dbg_dump_node(c
, mst2
);
334 * ubifs_write_rcvrd_mst_node - write the recovered master node.
335 * @c: UBIFS file-system description object
337 * This function writes the master node that was recovered during mounting in
338 * read-only mode and must now be written because we are remounting rw.
340 * This function returns %0 on success and a negative error code on failure.
342 int ubifs_write_rcvrd_mst_node(struct ubifs_info
*c
)
346 if (!c
->rcvrd_mst_node
)
348 c
->rcvrd_mst_node
->flags
|= cpu_to_le32(UBIFS_MST_DIRTY
);
349 c
->mst_node
->flags
|= cpu_to_le32(UBIFS_MST_DIRTY
);
350 err
= write_rcvrd_mst_node(c
, c
->rcvrd_mst_node
);
353 kfree(c
->rcvrd_mst_node
);
354 c
->rcvrd_mst_node
= NULL
;
359 * is_last_write - determine if an offset was in the last write to a LEB.
360 * @c: UBIFS file-system description object
361 * @buf: buffer to check
362 * @offs: offset to check
364 * 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.
368 static int is_last_write(const struct ubifs_info
*c
, void *buf
, int offs
)
370 int empty_offs
, check_len
;
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.
377 empty_offs
= ALIGN(offs
+ 1, c
->min_io_size
);
378 check_len
= c
->leb_size
- empty_offs
;
379 p
= buf
+ empty_offs
- offs
;
380 return is_empty(p
, check_len
);
384 * clean_buf - clean the data from an LEB sitting in a buffer.
385 * @c: UBIFS file-system description object
386 * @buf: buffer to clean
387 * @lnum: LEB number to clean
388 * @offs: offset from which to clean
389 * @len: length of buffer
391 * This function pads up to the next min_io_size boundary (if there is one) and
392 * sets empty space to all 0xff. @buf, @offs and @len are updated to the next
393 * @c->min_io_size boundary.
395 static void clean_buf(const struct ubifs_info
*c
, void **buf
, int lnum
,
398 int empty_offs
, pad_len
;
401 dbg_rcvry("cleaning corruption at %d:%d", lnum
, *offs
);
403 ubifs_assert(!(*offs
& 7));
404 empty_offs
= ALIGN(*offs
, c
->min_io_size
);
405 pad_len
= empty_offs
- *offs
;
406 ubifs_pad(c
, *buf
, pad_len
);
410 memset(*buf
, 0xff, c
->leb_size
- empty_offs
);
414 * no_more_nodes - determine if there are no more nodes in a buffer.
415 * @c: UBIFS file-system description object
416 * @buf: buffer to check
417 * @len: length of buffer
418 * @lnum: LEB number of the LEB from which @buf was read
419 * @offs: offset from which @buf was read
421 * This function ensures that the corrupted node at @offs is the last thing
422 * written to a LEB. This function returns %1 if more data is not found and
423 * %0 if more data is found.
425 static int no_more_nodes(const struct ubifs_info
*c
, void *buf
, int len
,
428 struct ubifs_ch
*ch
= buf
;
429 int skip
, dlen
= le32_to_cpu(ch
->len
);
431 /* Check for empty space after the corrupt node's common header */
432 skip
= ALIGN(offs
+ UBIFS_CH_SZ
, c
->min_io_size
) - offs
;
433 if (is_empty(buf
+ skip
, len
- skip
))
436 * The area after the common header size is not empty, so the common
437 * header must be intact. Check it.
439 if (ubifs_check_node(c
, buf
, lnum
, offs
, 1, 0) != -EUCLEAN
) {
440 dbg_rcvry("unexpected bad common header at %d:%d", lnum
, offs
);
443 /* Now we know the corrupt node's length we can skip over it */
444 skip
= ALIGN(offs
+ dlen
, c
->min_io_size
) - offs
;
445 /* After which there should be empty space */
446 if (is_empty(buf
+ skip
, len
- skip
))
448 dbg_rcvry("unexpected data at %d:%d", lnum
, offs
+ skip
);
453 * fix_unclean_leb - fix an unclean LEB.
454 * @c: UBIFS file-system description object
455 * @sleb: scanned LEB information
456 * @start: offset where scan started
458 static int fix_unclean_leb(struct ubifs_info
*c
, struct ubifs_scan_leb
*sleb
,
461 int lnum
= sleb
->lnum
, endpt
= start
;
463 /* Get the end offset of the last node we are keeping */
464 if (!list_empty(&sleb
->nodes
)) {
465 struct ubifs_scan_node
*snod
;
467 snod
= list_entry(sleb
->nodes
.prev
,
468 struct ubifs_scan_node
, list
);
469 endpt
= snod
->offs
+ snod
->len
;
472 if (c
->ro_mount
&& !c
->remounting_rw
) {
473 /* Add to recovery list */
474 struct ubifs_unclean_leb
*ucleb
;
476 dbg_rcvry("need to fix LEB %d start %d endpt %d",
477 lnum
, start
, sleb
->endpt
);
478 ucleb
= kzalloc(sizeof(struct ubifs_unclean_leb
), GFP_NOFS
);
482 ucleb
->endpt
= endpt
;
483 list_add_tail(&ucleb
->list
, &c
->unclean_leb_list
);
485 /* Write the fixed LEB back to flash */
488 dbg_rcvry("fixing LEB %d start %d endpt %d",
489 lnum
, start
, sleb
->endpt
);
491 err
= ubifs_leb_unmap(c
, lnum
);
495 int len
= ALIGN(endpt
, c
->min_io_size
);
498 err
= ubi_read(c
->ubi
, lnum
, sleb
->buf
, 0,
503 /* Pad to min_io_size */
505 int pad_len
= len
- ALIGN(endpt
, 8);
508 void *buf
= sleb
->buf
+ len
- pad_len
;
510 ubifs_pad(c
, buf
, pad_len
);
513 err
= ubi_leb_change(c
->ubi
, lnum
, sleb
->buf
, len
,
523 * drop_incomplete_group - drop nodes from an incomplete group.
524 * @sleb: scanned LEB information
525 * @offs: offset of dropped nodes is returned here
527 * This function returns %1 if nodes are dropped and %0 otherwise.
529 static int drop_incomplete_group(struct ubifs_scan_leb
*sleb
, int *offs
)
533 while (!list_empty(&sleb
->nodes
)) {
534 struct ubifs_scan_node
*snod
;
537 snod
= list_entry(sleb
->nodes
.prev
, struct ubifs_scan_node
,
540 if (ch
->group_type
!= UBIFS_IN_NODE_GROUP
)
542 dbg_rcvry("dropping node at %d:%d", sleb
->lnum
, snod
->offs
);
544 list_del(&snod
->list
);
546 sleb
->nodes_cnt
-= 1;
553 * ubifs_recover_leb - scan and recover a LEB.
554 * @c: UBIFS file-system description object
557 * @sbuf: LEB-sized buffer to use
558 * @grouped: nodes may be grouped for recovery
560 * This function does a scan of a LEB, but caters for errors that might have
561 * been caused by the unclean unmount from which we are attempting to recover.
562 * Returns %0 in case of success, %-EUCLEAN if an unrecoverable corruption is
563 * found, and a negative error code in case of failure.
565 struct ubifs_scan_leb
*ubifs_recover_leb(struct ubifs_info
*c
, int lnum
,
566 int offs
, void *sbuf
, int grouped
)
568 int err
, len
= c
->leb_size
- offs
, need_clean
= 0, quiet
= 1;
569 int empty_chkd
= 0, start
= offs
;
570 struct ubifs_scan_leb
*sleb
;
571 void *buf
= sbuf
+ offs
;
573 dbg_rcvry("%d:%d", lnum
, offs
);
575 sleb
= ubifs_start_scan(c
, lnum
, offs
, sbuf
);
585 dbg_scan("look at LEB %d:%d (%d bytes left)",
591 * Scan quietly until there is an error from which we cannot
594 ret
= ubifs_scan_a_node(c
, buf
, len
, lnum
, offs
, quiet
);
596 if (ret
== SCANNED_A_NODE
) {
597 /* A valid node, and not a padding node */
598 struct ubifs_ch
*ch
= buf
;
601 err
= ubifs_add_snod(c
, sleb
, buf
, offs
);
604 node_len
= ALIGN(le32_to_cpu(ch
->len
), 8);
612 /* Padding bytes or a valid padding node */
619 if (ret
== SCANNED_EMPTY_SPACE
) {
620 if (!is_empty(buf
, len
)) {
621 if (!is_last_write(c
, buf
, offs
))
623 clean_buf(c
, &buf
, lnum
, &offs
, &len
);
630 if (ret
== SCANNED_GARBAGE
|| ret
== SCANNED_A_BAD_PAD_NODE
)
631 if (is_last_write(c
, buf
, offs
)) {
632 clean_buf(c
, &buf
, lnum
, &offs
, &len
);
638 if (ret
== SCANNED_A_CORRUPT_NODE
)
639 if (no_more_nodes(c
, buf
, len
, lnum
, offs
)) {
640 clean_buf(c
, &buf
, lnum
, &offs
, &len
);
647 /* Redo the last scan but noisily */
653 case SCANNED_GARBAGE
:
656 case SCANNED_A_CORRUPT_NODE
:
657 case SCANNED_A_BAD_PAD_NODE
:
667 if (!empty_chkd
&& !is_empty(buf
, len
)) {
668 if (is_last_write(c
, buf
, offs
)) {
669 clean_buf(c
, &buf
, lnum
, &offs
, &len
);
672 int corruption
= first_non_ff(buf
, len
);
674 ubifs_err("corrupt empty space LEB %d:%d, corruption "
675 "starts at %d", lnum
, offs
, corruption
);
676 /* Make sure we dump interesting non-0xFF data */
683 /* Drop nodes from incomplete group */
684 if (grouped
&& drop_incomplete_group(sleb
, &offs
)) {
686 len
= c
->leb_size
- offs
;
687 clean_buf(c
, &buf
, lnum
, &offs
, &len
);
691 if (offs
% c
->min_io_size
) {
692 clean_buf(c
, &buf
, lnum
, &offs
, &len
);
696 ubifs_end_scan(c
, sleb
, lnum
, offs
);
699 err
= fix_unclean_leb(c
, sleb
, start
);
707 ubifs_scanned_corruption(c
, lnum
, offs
, buf
);
710 ubifs_err("LEB %d scanning failed", lnum
);
711 ubifs_scan_destroy(sleb
);
716 * get_cs_sqnum - get commit start sequence number.
717 * @c: UBIFS file-system description object
718 * @lnum: LEB number of commit start node
719 * @offs: offset of commit start node
720 * @cs_sqnum: commit start sequence number is returned here
722 * This function returns %0 on success and a negative error code on failure.
724 static int get_cs_sqnum(struct ubifs_info
*c
, int lnum
, int offs
,
725 unsigned long long *cs_sqnum
)
727 struct ubifs_cs_node
*cs_node
= NULL
;
730 dbg_rcvry("at %d:%d", lnum
, offs
);
731 cs_node
= kmalloc(UBIFS_CS_NODE_SZ
, GFP_KERNEL
);
734 if (c
->leb_size
- offs
< UBIFS_CS_NODE_SZ
)
736 err
= ubi_read(c
->ubi
, lnum
, (void *)cs_node
, offs
, UBIFS_CS_NODE_SZ
);
737 if (err
&& err
!= -EBADMSG
)
739 ret
= ubifs_scan_a_node(c
, cs_node
, UBIFS_CS_NODE_SZ
, lnum
, offs
, 0);
740 if (ret
!= SCANNED_A_NODE
) {
741 dbg_err("Not a valid node");
744 if (cs_node
->ch
.node_type
!= UBIFS_CS_NODE
) {
745 dbg_err("Node a CS node, type is %d", cs_node
->ch
.node_type
);
748 if (le64_to_cpu(cs_node
->cmt_no
) != c
->cmt_no
) {
749 dbg_err("CS node cmt_no %llu != current cmt_no %llu",
750 (unsigned long long)le64_to_cpu(cs_node
->cmt_no
),
754 *cs_sqnum
= le64_to_cpu(cs_node
->ch
.sqnum
);
755 dbg_rcvry("commit start sqnum %llu", *cs_sqnum
);
762 ubifs_err("failed to get CS sqnum");
768 * ubifs_recover_log_leb - scan and recover a log LEB.
769 * @c: UBIFS file-system description object
772 * @sbuf: LEB-sized buffer to use
774 * This function does a scan of a LEB, but caters for errors that might have
775 * been caused by unclean reboots from which we are attempting to recover
776 * (assume that only the last log LEB can be corrupted by an unclean reboot).
778 * This function returns %0 on success and a negative error code on failure.
780 struct ubifs_scan_leb
*ubifs_recover_log_leb(struct ubifs_info
*c
, int lnum
,
781 int offs
, void *sbuf
)
783 struct ubifs_scan_leb
*sleb
;
786 dbg_rcvry("LEB %d", lnum
);
787 next_lnum
= lnum
+ 1;
788 if (next_lnum
>= UBIFS_LOG_LNUM
+ c
->log_lebs
)
789 next_lnum
= UBIFS_LOG_LNUM
;
790 if (next_lnum
!= c
->ltail_lnum
) {
792 * We can only recover at the end of the log, so check that the
793 * next log LEB is empty or out of date.
795 sleb
= ubifs_scan(c
, next_lnum
, 0, sbuf
, 0);
798 if (sleb
->nodes_cnt
) {
799 struct ubifs_scan_node
*snod
;
800 unsigned long long cs_sqnum
= c
->cs_sqnum
;
802 snod
= list_entry(sleb
->nodes
.next
,
803 struct ubifs_scan_node
, list
);
807 err
= get_cs_sqnum(c
, lnum
, offs
, &cs_sqnum
);
809 ubifs_scan_destroy(sleb
);
813 if (snod
->sqnum
> cs_sqnum
) {
814 ubifs_err("unrecoverable log corruption "
816 ubifs_scan_destroy(sleb
);
817 return ERR_PTR(-EUCLEAN
);
820 ubifs_scan_destroy(sleb
);
822 return ubifs_recover_leb(c
, lnum
, offs
, sbuf
, 0);
826 * recover_head - recover a head.
827 * @c: UBIFS file-system description object
828 * @lnum: LEB number of head to recover
829 * @offs: offset of head to recover
830 * @sbuf: LEB-sized buffer to use
832 * This function ensures that there is no data on the flash at a head location.
834 * This function returns %0 on success and a negative error code on failure.
836 static int recover_head(const struct ubifs_info
*c
, int lnum
, int offs
,
841 if (c
->min_io_size
> 1)
842 len
= c
->min_io_size
;
845 if (offs
+ len
> c
->leb_size
)
846 len
= c
->leb_size
- offs
;
851 /* Read at the head location and check it is empty flash */
852 err
= ubi_read(c
->ubi
, lnum
, sbuf
, offs
, len
);
853 if (err
|| !is_empty(sbuf
, len
)) {
854 dbg_rcvry("cleaning head at %d:%d", lnum
, offs
);
856 return ubifs_leb_unmap(c
, lnum
);
857 err
= ubi_read(c
->ubi
, lnum
, sbuf
, 0, offs
);
860 return ubi_leb_change(c
->ubi
, lnum
, sbuf
, offs
, UBI_UNKNOWN
);
867 * ubifs_recover_inl_heads - recover index and LPT heads.
868 * @c: UBIFS file-system description object
869 * @sbuf: LEB-sized buffer to use
871 * This function ensures that there is no data on the flash at the index and
872 * LPT head locations.
874 * This deals with the recovery of a half-completed journal commit. UBIFS is
875 * careful never to overwrite the last version of the index or the LPT. Because
876 * the index and LPT are wandering trees, data from a half-completed commit will
877 * not be referenced anywhere in UBIFS. The data will be either in LEBs that are
878 * assumed to be empty and will be unmapped anyway before use, or in the index
881 * This function returns %0 on success and a negative error code on failure.
883 int ubifs_recover_inl_heads(const struct ubifs_info
*c
, void *sbuf
)
887 ubifs_assert(!c
->ro_mount
|| c
->remounting_rw
);
889 dbg_rcvry("checking index head at %d:%d", c
->ihead_lnum
, c
->ihead_offs
);
890 err
= recover_head(c
, c
->ihead_lnum
, c
->ihead_offs
, sbuf
);
894 dbg_rcvry("checking LPT head at %d:%d", c
->nhead_lnum
, c
->nhead_offs
);
895 err
= recover_head(c
, c
->nhead_lnum
, c
->nhead_offs
, sbuf
);
903 * clean_an_unclean_leb - read and write a LEB to remove corruption.
904 * @c: UBIFS file-system description object
905 * @ucleb: unclean LEB information
906 * @sbuf: LEB-sized buffer to use
908 * This function reads a LEB up to a point pre-determined by the mount recovery,
909 * checks the nodes, and writes the result back to the flash, thereby cleaning
910 * off any following corruption, or non-fatal ECC errors.
912 * This function returns %0 on success and a negative error code on failure.
914 static int clean_an_unclean_leb(const struct ubifs_info
*c
,
915 struct ubifs_unclean_leb
*ucleb
, void *sbuf
)
917 int err
, lnum
= ucleb
->lnum
, offs
= 0, len
= ucleb
->endpt
, quiet
= 1;
920 dbg_rcvry("LEB %d len %d", lnum
, len
);
923 /* Nothing to read, just unmap it */
924 err
= ubifs_leb_unmap(c
, lnum
);
930 err
= ubi_read(c
->ubi
, lnum
, buf
, offs
, len
);
931 if (err
&& err
!= -EBADMSG
)
939 /* Scan quietly until there is an error */
940 ret
= ubifs_scan_a_node(c
, buf
, len
, lnum
, offs
, quiet
);
942 if (ret
== SCANNED_A_NODE
) {
943 /* A valid node, and not a padding node */
944 struct ubifs_ch
*ch
= buf
;
947 node_len
= ALIGN(le32_to_cpu(ch
->len
), 8);
955 /* Padding bytes or a valid padding node */
962 if (ret
== SCANNED_EMPTY_SPACE
) {
963 ubifs_err("unexpected empty space at %d:%d",
969 /* Redo the last scan but noisily */
974 ubifs_scanned_corruption(c
, lnum
, offs
, buf
);
978 /* Pad to min_io_size */
979 len
= ALIGN(ucleb
->endpt
, c
->min_io_size
);
980 if (len
> ucleb
->endpt
) {
981 int pad_len
= len
- ALIGN(ucleb
->endpt
, 8);
984 buf
= c
->sbuf
+ len
- pad_len
;
985 ubifs_pad(c
, buf
, pad_len
);
989 /* Write back the LEB atomically */
990 err
= ubi_leb_change(c
->ubi
, lnum
, sbuf
, len
, UBI_UNKNOWN
);
994 dbg_rcvry("cleaned LEB %d", lnum
);
1000 * ubifs_clean_lebs - clean LEBs recovered during read-only mount.
1001 * @c: UBIFS file-system description object
1002 * @sbuf: LEB-sized buffer to use
1004 * This function cleans a LEB identified during recovery that needs to be
1005 * written but was not because UBIFS was mounted read-only. This happens when
1006 * remounting to read-write mode.
1008 * This function returns %0 on success and a negative error code on failure.
1010 int ubifs_clean_lebs(const struct ubifs_info
*c
, void *sbuf
)
1012 dbg_rcvry("recovery");
1013 while (!list_empty(&c
->unclean_leb_list
)) {
1014 struct ubifs_unclean_leb
*ucleb
;
1017 ucleb
= list_entry(c
->unclean_leb_list
.next
,
1018 struct ubifs_unclean_leb
, list
);
1019 err
= clean_an_unclean_leb(c
, ucleb
, sbuf
);
1022 list_del(&ucleb
->list
);
1029 * ubifs_rcvry_gc_commit - recover the GC LEB number and run the commit.
1030 * @c: UBIFS file-system description object
1032 * Out-of-place garbage collection requires always one empty LEB with which to
1033 * start garbage collection. The LEB number is recorded in c->gc_lnum and is
1034 * written to the master node on unmounting. In the case of an unclean unmount
1035 * the value of gc_lnum recorded in the master node is out of date and cannot
1036 * be used. Instead, recovery must allocate an empty LEB for this purpose.
1037 * However, there may not be enough empty space, in which case it must be
1038 * possible to GC the dirtiest LEB into the GC head LEB.
1040 * This function also runs the commit which causes the TNC updates from
1041 * size-recovery and orphans to be written to the flash. That is important to
1042 * ensure correct replay order for subsequent mounts.
1044 * This function returns %0 on success and a negative error code on failure.
1046 int ubifs_rcvry_gc_commit(struct ubifs_info
*c
)
1048 struct ubifs_wbuf
*wbuf
= &c
->jheads
[GCHD
].wbuf
;
1049 struct ubifs_lprops lp
;
1053 if (wbuf
->lnum
== -1) {
1054 dbg_rcvry("no GC head LEB");
1058 * See whether the used space in the dirtiest LEB fits in the GC head
1061 if (wbuf
->offs
== c
->leb_size
) {
1062 dbg_rcvry("no room in GC head LEB");
1065 err
= ubifs_find_dirty_leb(c
, &lp
, wbuf
->offs
, 2);
1068 * There are no dirty or empty LEBs subject to here being
1069 * enough for the index. Try to use
1070 * 'ubifs_find_free_leb_for_idx()', which will return any empty
1071 * LEBs (ignoring index requirements). If the index then
1072 * doesn't have enough LEBs the recovery commit will fail -
1073 * which is the same result anyway i.e. recovery fails. So
1074 * there is no problem ignoring index requirements and just
1075 * grabbing a free LEB since we have already established there
1076 * is not a dirty LEB we could have used instead.
1078 if (err
== -ENOSPC
) {
1079 dbg_rcvry("could not find a dirty LEB");
1084 ubifs_assert(!(lp
.flags
& LPROPS_INDEX
));
1086 if (lp
.free
+ lp
.dirty
== c
->leb_size
) {
1087 /* An empty LEB was returned */
1088 if (lp
.free
!= c
->leb_size
) {
1089 err
= ubifs_change_one_lp(c
, lnum
, c
->leb_size
,
1094 err
= ubifs_leb_unmap(c
, lnum
);
1098 dbg_rcvry("allocated LEB %d for GC", lnum
);
1099 /* Run the commit */
1100 dbg_rcvry("committing");
1101 return ubifs_run_commit(c
);
1104 * There was no empty LEB so the used space in the dirtiest LEB must fit
1105 * in the GC head LEB.
1107 if (lp
.free
+ lp
.dirty
< wbuf
->offs
) {
1108 dbg_rcvry("LEB %d doesn't fit in GC head LEB %d:%d",
1109 lnum
, wbuf
->lnum
, wbuf
->offs
);
1110 err
= ubifs_return_leb(c
, lnum
);
1116 * We run the commit before garbage collection otherwise subsequent
1117 * mounts will see the GC and orphan deletion in a different order.
1119 dbg_rcvry("committing");
1120 err
= ubifs_run_commit(c
);
1124 * The data in the dirtiest LEB fits in the GC head LEB, so do the GC
1125 * - use locking to keep 'ubifs_assert()' happy.
1127 dbg_rcvry("GC'ing LEB %d", lnum
);
1128 mutex_lock_nested(&wbuf
->io_mutex
, wbuf
->jhead
);
1129 err
= ubifs_garbage_collect_leb(c
, &lp
);
1131 int err2
= ubifs_wbuf_sync_nolock(wbuf
);
1136 mutex_unlock(&wbuf
->io_mutex
);
1138 dbg_err("GC failed, error %d", err
);
1143 if (err
!= LEB_RETAINED
) {
1144 dbg_err("GC returned %d", err
);
1147 err
= ubifs_leb_unmap(c
, c
->gc_lnum
);
1150 dbg_rcvry("allocated LEB %d for GC", lnum
);
1155 * There is no GC head LEB or the free space in the GC head LEB is too
1156 * small, or there are not dirty LEBs. Allocate gc_lnum by calling
1157 * 'ubifs_find_free_leb_for_idx()' so GC is not run.
1159 lnum
= ubifs_find_free_leb_for_idx(c
);
1161 dbg_err("could not find an empty LEB");
1164 /* And reset the index flag */
1165 err
= ubifs_change_one_lp(c
, lnum
, LPROPS_NC
, LPROPS_NC
, 0,
1170 dbg_rcvry("allocated LEB %d for GC", lnum
);
1171 /* Run the commit */
1172 dbg_rcvry("committing");
1173 return ubifs_run_commit(c
);
1177 * struct size_entry - inode size information for recovery.
1178 * @rb: link in the RB-tree of sizes
1179 * @inum: inode number
1180 * @i_size: size on inode
1181 * @d_size: maximum size based on data nodes
1182 * @exists: indicates whether the inode exists
1183 * @inode: inode if pinned in memory awaiting rw mode to fix it
1191 struct inode
*inode
;
1195 * add_ino - add an entry to the size tree.
1196 * @c: UBIFS file-system description object
1197 * @inum: inode number
1198 * @i_size: size on inode
1199 * @d_size: maximum size based on data nodes
1200 * @exists: indicates whether the inode exists
1202 static int add_ino(struct ubifs_info
*c
, ino_t inum
, loff_t i_size
,
1203 loff_t d_size
, int exists
)
1205 struct rb_node
**p
= &c
->size_tree
.rb_node
, *parent
= NULL
;
1206 struct size_entry
*e
;
1210 e
= rb_entry(parent
, struct size_entry
, rb
);
1214 p
= &(*p
)->rb_right
;
1217 e
= kzalloc(sizeof(struct size_entry
), GFP_KERNEL
);
1226 rb_link_node(&e
->rb
, parent
, p
);
1227 rb_insert_color(&e
->rb
, &c
->size_tree
);
1233 * find_ino - find an entry on the size tree.
1234 * @c: UBIFS file-system description object
1235 * @inum: inode number
1237 static struct size_entry
*find_ino(struct ubifs_info
*c
, ino_t inum
)
1239 struct rb_node
*p
= c
->size_tree
.rb_node
;
1240 struct size_entry
*e
;
1243 e
= rb_entry(p
, struct size_entry
, rb
);
1246 else if (inum
> e
->inum
)
1255 * remove_ino - remove an entry from the size tree.
1256 * @c: UBIFS file-system description object
1257 * @inum: inode number
1259 static void remove_ino(struct ubifs_info
*c
, ino_t inum
)
1261 struct size_entry
*e
= find_ino(c
, inum
);
1265 rb_erase(&e
->rb
, &c
->size_tree
);
1270 * ubifs_destroy_size_tree - free resources related to the size tree.
1271 * @c: UBIFS file-system description object
1273 void ubifs_destroy_size_tree(struct ubifs_info
*c
)
1275 struct rb_node
*this = c
->size_tree
.rb_node
;
1276 struct size_entry
*e
;
1279 if (this->rb_left
) {
1280 this = this->rb_left
;
1282 } else if (this->rb_right
) {
1283 this = this->rb_right
;
1286 e
= rb_entry(this, struct size_entry
, rb
);
1289 this = rb_parent(this);
1291 if (this->rb_left
== &e
->rb
)
1292 this->rb_left
= NULL
;
1294 this->rb_right
= NULL
;
1298 c
->size_tree
= RB_ROOT
;
1302 * ubifs_recover_size_accum - accumulate inode sizes for recovery.
1303 * @c: UBIFS file-system description object
1305 * @deletion: node is for a deletion
1306 * @new_size: inode size
1308 * This function has two purposes:
1309 * 1) to ensure there are no data nodes that fall outside the inode size
1310 * 2) to ensure there are no data nodes for inodes that do not exist
1311 * To accomplish those purposes, a rb-tree is constructed containing an entry
1312 * for each inode number in the journal that has not been deleted, and recording
1313 * the size from the inode node, the maximum size of any data node (also altered
1314 * by truncations) and a flag indicating a inode number for which no inode node
1315 * was present in the journal.
1317 * Note that there is still the possibility that there are data nodes that have
1318 * been committed that are beyond the inode size, however the only way to find
1319 * them would be to scan the entire index. Alternatively, some provision could
1320 * be made to record the size of inodes at the start of commit, which would seem
1321 * very cumbersome for a scenario that is quite unlikely and the only negative
1322 * consequence of which is wasted space.
1324 * This functions returns %0 on success and a negative error code on failure.
1326 int ubifs_recover_size_accum(struct ubifs_info
*c
, union ubifs_key
*key
,
1327 int deletion
, loff_t new_size
)
1329 ino_t inum
= key_inum(c
, key
);
1330 struct size_entry
*e
;
1333 switch (key_type(c
, key
)) {
1336 remove_ino(c
, inum
);
1338 e
= find_ino(c
, inum
);
1340 e
->i_size
= new_size
;
1343 err
= add_ino(c
, inum
, new_size
, 0, 1);
1349 case UBIFS_DATA_KEY
:
1350 e
= find_ino(c
, inum
);
1352 if (new_size
> e
->d_size
)
1353 e
->d_size
= new_size
;
1355 err
= add_ino(c
, inum
, 0, new_size
, 0);
1360 case UBIFS_TRUN_KEY
:
1361 e
= find_ino(c
, inum
);
1363 e
->d_size
= new_size
;
1370 * fix_size_in_place - fix inode size in place on flash.
1371 * @c: UBIFS file-system description object
1372 * @e: inode size information for recovery
1374 static int fix_size_in_place(struct ubifs_info
*c
, struct size_entry
*e
)
1376 struct ubifs_ino_node
*ino
= c
->sbuf
;
1378 union ubifs_key key
;
1379 int err
, lnum
, offs
, len
;
1383 /* Locate the inode node LEB number and offset */
1384 ino_key_init(c
, &key
, e
->inum
);
1385 err
= ubifs_tnc_locate(c
, &key
, ino
, &lnum
, &offs
);
1389 * If the size recorded on the inode node is greater than the size that
1390 * was calculated from nodes in the journal then don't change the inode.
1392 i_size
= le64_to_cpu(ino
->size
);
1393 if (i_size
>= e
->d_size
)
1396 err
= ubi_read(c
->ubi
, lnum
, c
->sbuf
, 0, c
->leb_size
);
1399 /* Change the size field and recalculate the CRC */
1400 ino
= c
->sbuf
+ offs
;
1401 ino
->size
= cpu_to_le64(e
->d_size
);
1402 len
= le32_to_cpu(ino
->ch
.len
);
1403 crc
= crc32(UBIFS_CRC32_INIT
, (void *)ino
+ 8, len
- 8);
1404 ino
->ch
.crc
= cpu_to_le32(crc
);
1405 /* Work out where data in the LEB ends and free space begins */
1407 len
= c
->leb_size
- 1;
1408 while (p
[len
] == 0xff)
1410 len
= ALIGN(len
+ 1, c
->min_io_size
);
1411 /* Atomically write the fixed LEB back again */
1412 err
= ubi_leb_change(c
->ubi
, lnum
, c
->sbuf
, len
, UBI_UNKNOWN
);
1415 dbg_rcvry("inode %lu at %d:%d size %lld -> %lld ",
1416 (unsigned long)e
->inum
, lnum
, offs
, i_size
, e
->d_size
);
1420 ubifs_warn("inode %lu failed to fix size %lld -> %lld error %d",
1421 (unsigned long)e
->inum
, e
->i_size
, e
->d_size
, err
);
1426 * ubifs_recover_size - recover inode size.
1427 * @c: UBIFS file-system description object
1429 * This function attempts to fix inode size discrepancies identified by the
1430 * 'ubifs_recover_size_accum()' function.
1432 * This functions returns %0 on success and a negative error code on failure.
1434 int ubifs_recover_size(struct ubifs_info
*c
)
1436 struct rb_node
*this = rb_first(&c
->size_tree
);
1439 struct size_entry
*e
;
1442 e
= rb_entry(this, struct size_entry
, rb
);
1444 union ubifs_key key
;
1446 ino_key_init(c
, &key
, e
->inum
);
1447 err
= ubifs_tnc_lookup(c
, &key
, c
->sbuf
);
1448 if (err
&& err
!= -ENOENT
)
1450 if (err
== -ENOENT
) {
1451 /* Remove data nodes that have no inode */
1452 dbg_rcvry("removing ino %lu",
1453 (unsigned long)e
->inum
);
1454 err
= ubifs_tnc_remove_ino(c
, e
->inum
);
1458 struct ubifs_ino_node
*ino
= c
->sbuf
;
1461 e
->i_size
= le64_to_cpu(ino
->size
);
1464 if (e
->exists
&& e
->i_size
< e
->d_size
) {
1465 if (!e
->inode
&& c
->ro_mount
) {
1466 /* Fix the inode size and pin it in memory */
1467 struct inode
*inode
;
1469 inode
= ubifs_iget(c
->vfs_sb
, e
->inum
);
1471 return PTR_ERR(inode
);
1472 if (inode
->i_size
< e
->d_size
) {
1473 dbg_rcvry("ino %lu size %lld -> %lld",
1474 (unsigned long)e
->inum
,
1475 e
->d_size
, inode
->i_size
);
1476 inode
->i_size
= e
->d_size
;
1477 ubifs_inode(inode
)->ui_size
= e
->d_size
;
1479 this = rb_next(this);
1484 /* Fix the size in place */
1485 err
= fix_size_in_place(c
, e
);
1492 this = rb_next(this);
1493 rb_erase(&e
->rb
, &c
->size_tree
);