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 sucessfully. If not, the process of mounting
27 * incorparates 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>
37 * is_empty - determine whether a buffer is empty (contains all 0xff).
38 * @buf: buffer to clean
39 * @len: length of buffer
41 * This function returns %1 if the buffer is empty (contains all 0xff) otherwise
44 static int is_empty(void *buf
, int len
)
49 for (i
= 0; i
< len
; i
++)
56 * get_master_node - get the last valid master node allowing for corruption.
57 * @c: UBIFS file-system description object
59 * @pbuf: buffer containing the LEB read, is returned here
60 * @mst: master node, if found, is returned here
61 * @cor: corruption, if found, is returned here
63 * This function allocates a buffer, reads the LEB into it, and finds and
64 * returns the last valid master node allowing for one area of corruption.
65 * The corrupt area, if there is one, must be consistent with the assumption
66 * that it is the result of an unclean unmount while the master node was being
67 * written. Under those circumstances, it is valid to use the previously written
70 * This function returns %0 on success and a negative error code on failure.
72 static int get_master_node(const struct ubifs_info
*c
, int lnum
, void **pbuf
,
73 struct ubifs_mst_node
**mst
, void **cor
)
75 const int sz
= c
->mst_node_alsz
;
79 sbuf
= vmalloc(c
->leb_size
);
83 err
= ubi_read(c
->ubi
, lnum
, sbuf
, 0, c
->leb_size
);
84 if (err
&& err
!= -EBADMSG
)
87 /* Find the first position that is definitely not a node */
91 while (offs
+ UBIFS_MST_NODE_SZ
<= c
->leb_size
) {
92 struct ubifs_ch
*ch
= buf
;
94 if (le32_to_cpu(ch
->magic
) != UBIFS_NODE_MAGIC
)
100 /* See if there was a valid master node before that */
107 ret
= ubifs_scan_a_node(c
, buf
, len
, lnum
, offs
, 1);
108 if (ret
!= SCANNED_A_NODE
&& offs
) {
109 /* Could have been corruption so check one place back */
113 ret
= ubifs_scan_a_node(c
, buf
, len
, lnum
, offs
, 1);
114 if (ret
!= SCANNED_A_NODE
)
116 * We accept only one area of corruption because
117 * we are assuming that it was caused while
118 * trying to write a master node.
122 if (ret
== SCANNED_A_NODE
) {
123 struct ubifs_ch
*ch
= buf
;
125 if (ch
->node_type
!= UBIFS_MST_NODE
)
127 dbg_rcvry("found a master node at %d:%d", lnum
, offs
);
134 /* Check for corruption */
135 if (offs
< c
->leb_size
) {
136 if (!is_empty(buf
, min_t(int, len
, sz
))) {
138 dbg_rcvry("found corruption at %d:%d", lnum
, offs
);
144 /* Check remaining empty space */
145 if (offs
< c
->leb_size
)
146 if (!is_empty(buf
, len
))
161 * write_rcvrd_mst_node - write recovered master node.
162 * @c: UBIFS file-system description object
165 * This function returns %0 on success and a negative error code on failure.
167 static int write_rcvrd_mst_node(struct ubifs_info
*c
,
168 struct ubifs_mst_node
*mst
)
170 int err
= 0, lnum
= UBIFS_MST_LNUM
, sz
= c
->mst_node_alsz
;
173 dbg_rcvry("recovery");
175 save_flags
= mst
->flags
;
176 mst
->flags
|= cpu_to_le32(UBIFS_MST_RCVRY
);
178 ubifs_prepare_node(c
, mst
, UBIFS_MST_NODE_SZ
, 1);
179 err
= ubi_leb_change(c
->ubi
, lnum
, mst
, sz
, UBI_SHORTTERM
);
182 err
= ubi_leb_change(c
->ubi
, lnum
+ 1, mst
, sz
, UBI_SHORTTERM
);
186 mst
->flags
= save_flags
;
191 * ubifs_recover_master_node - recover the master node.
192 * @c: UBIFS file-system description object
194 * This function recovers the master node from corruption that may occur due to
195 * an unclean unmount.
197 * This function returns %0 on success and a negative error code on failure.
199 int ubifs_recover_master_node(struct ubifs_info
*c
)
201 void *buf1
= NULL
, *buf2
= NULL
, *cor1
= NULL
, *cor2
= NULL
;
202 struct ubifs_mst_node
*mst1
= NULL
, *mst2
= NULL
, *mst
;
203 const int sz
= c
->mst_node_alsz
;
204 int err
, offs1
, offs2
;
206 dbg_rcvry("recovery");
208 err
= get_master_node(c
, UBIFS_MST_LNUM
, &buf1
, &mst1
, &cor1
);
212 err
= get_master_node(c
, UBIFS_MST_LNUM
+ 1, &buf2
, &mst2
, &cor2
);
217 offs1
= (void *)mst1
- buf1
;
218 if ((le32_to_cpu(mst1
->flags
) & UBIFS_MST_RCVRY
) &&
219 (offs1
== 0 && !cor1
)) {
221 * mst1 was written by recovery at offset 0 with no
224 dbg_rcvry("recovery recovery");
227 offs2
= (void *)mst2
- buf2
;
228 if (offs1
== offs2
) {
229 /* Same offset, so must be the same */
230 if (memcmp((void *)mst1
+ UBIFS_CH_SZ
,
231 (void *)mst2
+ UBIFS_CH_SZ
,
232 UBIFS_MST_NODE_SZ
- UBIFS_CH_SZ
))
235 } else if (offs2
+ sz
== offs1
) {
236 /* 1st LEB was written, 2nd was not */
240 } else if (offs1
== 0 && offs2
+ sz
>= c
->leb_size
) {
241 /* 1st LEB was unmapped and written, 2nd not */
249 * 2nd LEB was unmapped and about to be written, so
250 * there must be only one master node in the first LEB
253 if (offs1
!= 0 || cor1
)
261 * 1st LEB was unmapped and about to be written, so there must
262 * be no room left in 2nd LEB.
264 offs2
= (void *)mst2
- buf2
;
265 if (offs2
+ sz
+ sz
<= c
->leb_size
)
270 dbg_rcvry("recovered master node from LEB %d",
271 (mst
== mst1
? UBIFS_MST_LNUM
: UBIFS_MST_LNUM
+ 1));
273 memcpy(c
->mst_node
, mst
, UBIFS_MST_NODE_SZ
);
275 if ((c
->vfs_sb
->s_flags
& MS_RDONLY
)) {
276 /* Read-only mode. Keep a copy for switching to rw mode */
277 c
->rcvrd_mst_node
= kmalloc(sz
, GFP_KERNEL
);
278 if (!c
->rcvrd_mst_node
) {
282 memcpy(c
->rcvrd_mst_node
, c
->mst_node
, UBIFS_MST_NODE_SZ
);
284 /* Write the recovered master node */
285 c
->max_sqnum
= le64_to_cpu(mst
->ch
.sqnum
) - 1;
286 err
= write_rcvrd_mst_node(c
, c
->mst_node
);
299 ubifs_err("failed to recover master node");
301 dbg_err("dumping first master node");
302 dbg_dump_node(c
, mst1
);
305 dbg_err("dumping second master node");
306 dbg_dump_node(c
, mst2
);
314 * ubifs_write_rcvrd_mst_node - write the recovered master node.
315 * @c: UBIFS file-system description object
317 * This function writes the master node that was recovered during mounting in
318 * read-only mode and must now be written because we are remounting rw.
320 * This function returns %0 on success and a negative error code on failure.
322 int ubifs_write_rcvrd_mst_node(struct ubifs_info
*c
)
326 if (!c
->rcvrd_mst_node
)
328 c
->rcvrd_mst_node
->flags
|= cpu_to_le32(UBIFS_MST_DIRTY
);
329 c
->mst_node
->flags
|= cpu_to_le32(UBIFS_MST_DIRTY
);
330 err
= write_rcvrd_mst_node(c
, c
->rcvrd_mst_node
);
333 kfree(c
->rcvrd_mst_node
);
334 c
->rcvrd_mst_node
= NULL
;
339 * is_last_write - determine if an offset was in the last write to a LEB.
340 * @c: UBIFS file-system description object
341 * @buf: buffer to check
342 * @offs: offset to check
344 * This function returns %1 if @offs was in the last write to the LEB whose data
345 * is in @buf, otherwise %0 is returned. The determination is made by checking
346 * for subsequent empty space starting from the next min_io_size boundary (or a
347 * bit less than the common header size if min_io_size is one).
349 static int is_last_write(const struct ubifs_info
*c
, void *buf
, int offs
)
355 if (c
->min_io_size
== 1) {
356 check_len
= c
->leb_size
- offs
;
358 for (; check_len
> 0; check_len
--)
362 * 'check_len' is the size of the corruption which cannot be
363 * more than the size of 1 node if it was caused by an unclean
366 if (check_len
> UBIFS_MAX_NODE_SZ
)
372 * Round up to the next c->min_io_size boundary i.e. 'offs' is in the
373 * last wbuf written. After that should be empty space.
375 empty_offs
= ALIGN(offs
+ 1, c
->min_io_size
);
376 check_len
= c
->leb_size
- empty_offs
;
377 p
= buf
+ empty_offs
- offs
;
379 for (; check_len
> 0; check_len
--)
386 * clean_buf - clean the data from an LEB sitting in a buffer.
387 * @c: UBIFS file-system description object
388 * @buf: buffer to clean
389 * @lnum: LEB number to clean
390 * @offs: offset from which to clean
391 * @len: length of buffer
393 * This function pads up to the next min_io_size boundary (if there is one) and
394 * sets empty space to all 0xff. @buf, @offs and @len are updated to the next
395 * min_io_size boundary (if there is one).
397 static void clean_buf(const struct ubifs_info
*c
, void **buf
, int lnum
,
400 int empty_offs
, pad_len
;
403 dbg_rcvry("cleaning corruption at %d:%d", lnum
, *offs
);
405 if (c
->min_io_size
== 1) {
406 memset(*buf
, 0xff, c
->leb_size
- *offs
);
410 ubifs_assert(!(*offs
& 7));
411 empty_offs
= ALIGN(*offs
, c
->min_io_size
);
412 pad_len
= empty_offs
- *offs
;
413 ubifs_pad(c
, *buf
, pad_len
);
417 memset(*buf
, 0xff, c
->leb_size
- empty_offs
);
421 * no_more_nodes - determine if there are no more nodes in a buffer.
422 * @c: UBIFS file-system description object
423 * @buf: buffer to check
424 * @len: length of buffer
425 * @lnum: LEB number of the LEB from which @buf was read
426 * @offs: offset from which @buf was read
428 * This function scans @buf for more nodes and returns %0 is a node is found and
429 * %1 if no more nodes are found.
431 static int no_more_nodes(const struct ubifs_info
*c
, void *buf
, int len
,
434 int skip
, next_offs
= 0;
436 if (len
> UBIFS_DATA_NODE_SZ
) {
437 struct ubifs_ch
*ch
= buf
;
438 int dlen
= le32_to_cpu(ch
->len
);
440 if (ch
->node_type
== UBIFS_DATA_NODE
&& dlen
>= UBIFS_CH_SZ
&&
441 dlen
<= UBIFS_MAX_DATA_NODE_SZ
)
442 /* The corrupt node looks like a data node */
443 next_offs
= ALIGN(offs
+ dlen
, 8);
446 if (c
->min_io_size
== 1)
449 skip
= ALIGN(offs
+ 1, c
->min_io_size
) - offs
;
455 struct ubifs_ch
*ch
= buf
;
456 uint32_t magic
= le32_to_cpu(ch
->magic
);
459 if (magic
== UBIFS_NODE_MAGIC
) {
460 ret
= ubifs_scan_a_node(c
, buf
, len
, lnum
, offs
, 1);
461 if (ret
== SCANNED_A_NODE
|| ret
> 0) {
463 * There is a small chance this is just data in
464 * a data node, so check that possibility. e.g.
465 * this is part of a file that itself contains
468 if (next_offs
&& offs
+ le32_to_cpu(ch
->len
) <=
471 dbg_rcvry("unexpected node at %d:%d", lnum
,
484 * fix_unclean_leb - fix an unclean LEB.
485 * @c: UBIFS file-system description object
486 * @sleb: scanned LEB information
487 * @start: offset where scan started
489 static int fix_unclean_leb(struct ubifs_info
*c
, struct ubifs_scan_leb
*sleb
,
492 int lnum
= sleb
->lnum
, endpt
= start
;
494 /* Get the end offset of the last node we are keeping */
495 if (!list_empty(&sleb
->nodes
)) {
496 struct ubifs_scan_node
*snod
;
498 snod
= list_entry(sleb
->nodes
.prev
,
499 struct ubifs_scan_node
, list
);
500 endpt
= snod
->offs
+ snod
->len
;
503 if ((c
->vfs_sb
->s_flags
& MS_RDONLY
) && !c
->remounting_rw
) {
504 /* Add to recovery list */
505 struct ubifs_unclean_leb
*ucleb
;
507 dbg_rcvry("need to fix LEB %d start %d endpt %d",
508 lnum
, start
, sleb
->endpt
);
509 ucleb
= kzalloc(sizeof(struct ubifs_unclean_leb
), GFP_NOFS
);
513 ucleb
->endpt
= endpt
;
514 list_add_tail(&ucleb
->list
, &c
->unclean_leb_list
);
516 /* Write the fixed LEB back to flash */
519 dbg_rcvry("fixing LEB %d start %d endpt %d",
520 lnum
, start
, sleb
->endpt
);
522 err
= ubifs_leb_unmap(c
, lnum
);
526 int len
= ALIGN(endpt
, c
->min_io_size
);
529 err
= ubi_read(c
->ubi
, lnum
, sleb
->buf
, 0,
534 /* Pad to min_io_size */
536 int pad_len
= len
- ALIGN(endpt
, 8);
539 void *buf
= sleb
->buf
+ len
- pad_len
;
541 ubifs_pad(c
, buf
, pad_len
);
544 err
= ubi_leb_change(c
->ubi
, lnum
, sleb
->buf
, len
,
554 * drop_incomplete_group - drop nodes from an incomplete group.
555 * @sleb: scanned LEB information
556 * @offs: offset of dropped nodes is returned here
558 * This function returns %1 if nodes are dropped and %0 otherwise.
560 static int drop_incomplete_group(struct ubifs_scan_leb
*sleb
, int *offs
)
564 while (!list_empty(&sleb
->nodes
)) {
565 struct ubifs_scan_node
*snod
;
568 snod
= list_entry(sleb
->nodes
.prev
, struct ubifs_scan_node
,
571 if (ch
->group_type
!= UBIFS_IN_NODE_GROUP
)
573 dbg_rcvry("dropping node at %d:%d", sleb
->lnum
, snod
->offs
);
575 list_del(&snod
->list
);
577 sleb
->nodes_cnt
-= 1;
584 * ubifs_recover_leb - scan and recover a LEB.
585 * @c: UBIFS file-system description object
588 * @sbuf: LEB-sized buffer to use
589 * @grouped: nodes may be grouped for recovery
591 * This function does a scan of a LEB, but caters for errors that might have
592 * been caused by the unclean unmount from which we are attempting to recover.
594 * This function returns %0 on success and a negative error code on failure.
596 struct ubifs_scan_leb
*ubifs_recover_leb(struct ubifs_info
*c
, int lnum
,
597 int offs
, void *sbuf
, int grouped
)
599 int err
, len
= c
->leb_size
- offs
, need_clean
= 0, quiet
= 1;
600 int empty_chkd
= 0, start
= offs
;
601 struct ubifs_scan_leb
*sleb
;
602 void *buf
= sbuf
+ offs
;
604 dbg_rcvry("%d:%d", lnum
, offs
);
606 sleb
= ubifs_start_scan(c
, lnum
, offs
, sbuf
);
616 dbg_scan("look at LEB %d:%d (%d bytes left)",
622 * Scan quietly until there is an error from which we cannot
625 ret
= ubifs_scan_a_node(c
, buf
, len
, lnum
, offs
, quiet
);
627 if (ret
== SCANNED_A_NODE
) {
628 /* A valid node, and not a padding node */
629 struct ubifs_ch
*ch
= buf
;
632 err
= ubifs_add_snod(c
, sleb
, buf
, offs
);
635 node_len
= ALIGN(le32_to_cpu(ch
->len
), 8);
643 /* Padding bytes or a valid padding node */
650 if (ret
== SCANNED_EMPTY_SPACE
) {
651 if (!is_empty(buf
, len
)) {
652 if (!is_last_write(c
, buf
, offs
))
654 clean_buf(c
, &buf
, lnum
, &offs
, &len
);
661 if (ret
== SCANNED_GARBAGE
|| ret
== SCANNED_A_BAD_PAD_NODE
)
662 if (is_last_write(c
, buf
, offs
)) {
663 clean_buf(c
, &buf
, lnum
, &offs
, &len
);
669 if (ret
== SCANNED_A_CORRUPT_NODE
)
670 if (no_more_nodes(c
, buf
, len
, lnum
, offs
)) {
671 clean_buf(c
, &buf
, lnum
, &offs
, &len
);
678 /* Redo the last scan but noisily */
684 case SCANNED_GARBAGE
:
687 case SCANNED_A_CORRUPT_NODE
:
688 case SCANNED_A_BAD_PAD_NODE
:
697 if (!empty_chkd
&& !is_empty(buf
, len
)) {
698 if (is_last_write(c
, buf
, offs
)) {
699 clean_buf(c
, &buf
, lnum
, &offs
, &len
);
702 ubifs_err("corrupt empty space at LEB %d:%d",
708 /* Drop nodes from incomplete group */
709 if (grouped
&& drop_incomplete_group(sleb
, &offs
)) {
711 len
= c
->leb_size
- offs
;
712 clean_buf(c
, &buf
, lnum
, &offs
, &len
);
716 if (offs
% c
->min_io_size
) {
717 clean_buf(c
, &buf
, lnum
, &offs
, &len
);
721 ubifs_end_scan(c
, sleb
, lnum
, offs
);
724 err
= fix_unclean_leb(c
, sleb
, start
);
732 ubifs_scanned_corruption(c
, lnum
, offs
, buf
);
735 ubifs_err("LEB %d scanning failed", lnum
);
736 ubifs_scan_destroy(sleb
);
741 * get_cs_sqnum - get commit start sequence number.
742 * @c: UBIFS file-system description object
743 * @lnum: LEB number of commit start node
744 * @offs: offset of commit start node
745 * @cs_sqnum: commit start sequence number is returned here
747 * This function returns %0 on success and a negative error code on failure.
749 static int get_cs_sqnum(struct ubifs_info
*c
, int lnum
, int offs
,
750 unsigned long long *cs_sqnum
)
752 struct ubifs_cs_node
*cs_node
= NULL
;
755 dbg_rcvry("at %d:%d", lnum
, offs
);
756 cs_node
= kmalloc(UBIFS_CS_NODE_SZ
, GFP_KERNEL
);
759 if (c
->leb_size
- offs
< UBIFS_CS_NODE_SZ
)
761 err
= ubi_read(c
->ubi
, lnum
, (void *)cs_node
, offs
, UBIFS_CS_NODE_SZ
);
762 if (err
&& err
!= -EBADMSG
)
764 ret
= ubifs_scan_a_node(c
, cs_node
, UBIFS_CS_NODE_SZ
, lnum
, offs
, 0);
765 if (ret
!= SCANNED_A_NODE
) {
766 dbg_err("Not a valid node");
769 if (cs_node
->ch
.node_type
!= UBIFS_CS_NODE
) {
770 dbg_err("Node a CS node, type is %d", cs_node
->ch
.node_type
);
773 if (le64_to_cpu(cs_node
->cmt_no
) != c
->cmt_no
) {
774 dbg_err("CS node cmt_no %llu != current cmt_no %llu",
775 (unsigned long long)le64_to_cpu(cs_node
->cmt_no
),
779 *cs_sqnum
= le64_to_cpu(cs_node
->ch
.sqnum
);
780 dbg_rcvry("commit start sqnum %llu", *cs_sqnum
);
787 ubifs_err("failed to get CS sqnum");
793 * ubifs_recover_log_leb - scan and recover a log LEB.
794 * @c: UBIFS file-system description object
797 * @sbuf: LEB-sized buffer to use
799 * This function does a scan of a LEB, but caters for errors that might have
800 * been caused by the unclean unmount from which we are attempting to recover.
802 * This function returns %0 on success and a negative error code on failure.
804 struct ubifs_scan_leb
*ubifs_recover_log_leb(struct ubifs_info
*c
, int lnum
,
805 int offs
, void *sbuf
)
807 struct ubifs_scan_leb
*sleb
;
810 dbg_rcvry("LEB %d", lnum
);
811 next_lnum
= lnum
+ 1;
812 if (next_lnum
>= UBIFS_LOG_LNUM
+ c
->log_lebs
)
813 next_lnum
= UBIFS_LOG_LNUM
;
814 if (next_lnum
!= c
->ltail_lnum
) {
816 * We can only recover at the end of the log, so check that the
817 * next log LEB is empty or out of date.
819 sleb
= ubifs_scan(c
, next_lnum
, 0, sbuf
);
822 if (sleb
->nodes_cnt
) {
823 struct ubifs_scan_node
*snod
;
824 unsigned long long cs_sqnum
= c
->cs_sqnum
;
826 snod
= list_entry(sleb
->nodes
.next
,
827 struct ubifs_scan_node
, list
);
831 err
= get_cs_sqnum(c
, lnum
, offs
, &cs_sqnum
);
833 ubifs_scan_destroy(sleb
);
837 if (snod
->sqnum
> cs_sqnum
) {
838 ubifs_err("unrecoverable log corruption "
840 ubifs_scan_destroy(sleb
);
841 return ERR_PTR(-EUCLEAN
);
844 ubifs_scan_destroy(sleb
);
846 return ubifs_recover_leb(c
, lnum
, offs
, sbuf
, 0);
850 * recover_head - recover a head.
851 * @c: UBIFS file-system description object
852 * @lnum: LEB number of head to recover
853 * @offs: offset of head to recover
854 * @sbuf: LEB-sized buffer to use
856 * This function ensures that there is no data on the flash at a head location.
858 * This function returns %0 on success and a negative error code on failure.
860 static int recover_head(const struct ubifs_info
*c
, int lnum
, int offs
,
863 int len
, err
, need_clean
= 0;
865 if (c
->min_io_size
> 1)
866 len
= c
->min_io_size
;
869 if (offs
+ len
> c
->leb_size
)
870 len
= c
->leb_size
- offs
;
875 /* Read at the head location and check it is empty flash */
876 err
= ubi_read(c
->ubi
, lnum
, sbuf
, offs
, len
);
890 dbg_rcvry("cleaning head at %d:%d", lnum
, offs
);
892 return ubifs_leb_unmap(c
, lnum
);
893 err
= ubi_read(c
->ubi
, lnum
, sbuf
, 0, offs
);
896 return ubi_leb_change(c
->ubi
, lnum
, sbuf
, offs
, UBI_UNKNOWN
);
903 * ubifs_recover_inl_heads - recover index and LPT heads.
904 * @c: UBIFS file-system description object
905 * @sbuf: LEB-sized buffer to use
907 * This function ensures that there is no data on the flash at the index and
908 * LPT head locations.
910 * This deals with the recovery of a half-completed journal commit. UBIFS is
911 * careful never to overwrite the last version of the index or the LPT. Because
912 * the index and LPT are wandering trees, data from a half-completed commit will
913 * not be referenced anywhere in UBIFS. The data will be either in LEBs that are
914 * assumed to be empty and will be unmapped anyway before use, or in the index
917 * This function returns %0 on success and a negative error code on failure.
919 int ubifs_recover_inl_heads(const struct ubifs_info
*c
, void *sbuf
)
923 ubifs_assert(!(c
->vfs_sb
->s_flags
& MS_RDONLY
) || c
->remounting_rw
);
925 dbg_rcvry("checking index head at %d:%d", c
->ihead_lnum
, c
->ihead_offs
);
926 err
= recover_head(c
, c
->ihead_lnum
, c
->ihead_offs
, sbuf
);
930 dbg_rcvry("checking LPT head at %d:%d", c
->nhead_lnum
, c
->nhead_offs
);
931 err
= recover_head(c
, c
->nhead_lnum
, c
->nhead_offs
, sbuf
);
939 * clean_an_unclean_leb - read and write a LEB to remove corruption.
940 * @c: UBIFS file-system description object
941 * @ucleb: unclean LEB information
942 * @sbuf: LEB-sized buffer to use
944 * This function reads a LEB up to a point pre-determined by the mount recovery,
945 * checks the nodes, and writes the result back to the flash, thereby cleaning
946 * off any following corruption, or non-fatal ECC errors.
948 * This function returns %0 on success and a negative error code on failure.
950 static int clean_an_unclean_leb(const struct ubifs_info
*c
,
951 struct ubifs_unclean_leb
*ucleb
, void *sbuf
)
953 int err
, lnum
= ucleb
->lnum
, offs
= 0, len
= ucleb
->endpt
, quiet
= 1;
956 dbg_rcvry("LEB %d len %d", lnum
, len
);
959 /* Nothing to read, just unmap it */
960 err
= ubifs_leb_unmap(c
, lnum
);
966 err
= ubi_read(c
->ubi
, lnum
, buf
, offs
, len
);
967 if (err
&& err
!= -EBADMSG
)
975 /* Scan quietly until there is an error */
976 ret
= ubifs_scan_a_node(c
, buf
, len
, lnum
, offs
, quiet
);
978 if (ret
== SCANNED_A_NODE
) {
979 /* A valid node, and not a padding node */
980 struct ubifs_ch
*ch
= buf
;
983 node_len
= ALIGN(le32_to_cpu(ch
->len
), 8);
991 /* Padding bytes or a valid padding node */
998 if (ret
== SCANNED_EMPTY_SPACE
) {
999 ubifs_err("unexpected empty space at %d:%d",
1005 /* Redo the last scan but noisily */
1010 ubifs_scanned_corruption(c
, lnum
, offs
, buf
);
1014 /* Pad to min_io_size */
1015 len
= ALIGN(ucleb
->endpt
, c
->min_io_size
);
1016 if (len
> ucleb
->endpt
) {
1017 int pad_len
= len
- ALIGN(ucleb
->endpt
, 8);
1020 buf
= c
->sbuf
+ len
- pad_len
;
1021 ubifs_pad(c
, buf
, pad_len
);
1025 /* Write back the LEB atomically */
1026 err
= ubi_leb_change(c
->ubi
, lnum
, sbuf
, len
, UBI_UNKNOWN
);
1030 dbg_rcvry("cleaned LEB %d", lnum
);
1036 * ubifs_clean_lebs - clean LEBs recovered during read-only mount.
1037 * @c: UBIFS file-system description object
1038 * @sbuf: LEB-sized buffer to use
1040 * This function cleans a LEB identified during recovery that needs to be
1041 * written but was not because UBIFS was mounted read-only. This happens when
1042 * remounting to read-write mode.
1044 * This function returns %0 on success and a negative error code on failure.
1046 int ubifs_clean_lebs(const struct ubifs_info
*c
, void *sbuf
)
1048 dbg_rcvry("recovery");
1049 while (!list_empty(&c
->unclean_leb_list
)) {
1050 struct ubifs_unclean_leb
*ucleb
;
1053 ucleb
= list_entry(c
->unclean_leb_list
.next
,
1054 struct ubifs_unclean_leb
, list
);
1055 err
= clean_an_unclean_leb(c
, ucleb
, sbuf
);
1058 list_del(&ucleb
->list
);
1065 * ubifs_rcvry_gc_commit - recover the GC LEB number and run the commit.
1066 * @c: UBIFS file-system description object
1068 * Out-of-place garbage collection requires always one empty LEB with which to
1069 * start garbage collection. The LEB number is recorded in c->gc_lnum and is
1070 * written to the master node on unmounting. In the case of an unclean unmount
1071 * the value of gc_lnum recorded in the master node is out of date and cannot
1072 * be used. Instead, recovery must allocate an empty LEB for this purpose.
1073 * However, there may not be enough empty space, in which case it must be
1074 * possible to GC the dirtiest LEB into the GC head LEB.
1076 * This function also runs the commit which causes the TNC updates from
1077 * size-recovery and orphans to be written to the flash. That is important to
1078 * ensure correct replay order for subsequent mounts.
1080 * This function returns %0 on success and a negative error code on failure.
1082 int ubifs_rcvry_gc_commit(struct ubifs_info
*c
)
1084 struct ubifs_wbuf
*wbuf
= &c
->jheads
[GCHD
].wbuf
;
1085 struct ubifs_lprops lp
;
1089 if (wbuf
->lnum
== -1) {
1090 dbg_rcvry("no GC head LEB");
1094 * See whether the used space in the dirtiest LEB fits in the GC head
1097 if (wbuf
->offs
== c
->leb_size
) {
1098 dbg_rcvry("no room in GC head LEB");
1101 err
= ubifs_find_dirty_leb(c
, &lp
, wbuf
->offs
, 2);
1104 dbg_err("could not find a dirty LEB");
1107 ubifs_assert(!(lp
.flags
& LPROPS_INDEX
));
1109 if (lp
.free
+ lp
.dirty
== c
->leb_size
) {
1110 /* An empty LEB was returned */
1111 if (lp
.free
!= c
->leb_size
) {
1112 err
= ubifs_change_one_lp(c
, lnum
, c
->leb_size
,
1117 err
= ubifs_leb_unmap(c
, lnum
);
1121 dbg_rcvry("allocated LEB %d for GC", lnum
);
1122 /* Run the commit */
1123 dbg_rcvry("committing");
1124 return ubifs_run_commit(c
);
1127 * There was no empty LEB so the used space in the dirtiest LEB must fit
1128 * in the GC head LEB.
1130 if (lp
.free
+ lp
.dirty
< wbuf
->offs
) {
1131 dbg_rcvry("LEB %d doesn't fit in GC head LEB %d:%d",
1132 lnum
, wbuf
->lnum
, wbuf
->offs
);
1133 err
= ubifs_return_leb(c
, lnum
);
1139 * We run the commit before garbage collection otherwise subsequent
1140 * mounts will see the GC and orphan deletion in a different order.
1142 dbg_rcvry("committing");
1143 err
= ubifs_run_commit(c
);
1147 * The data in the dirtiest LEB fits in the GC head LEB, so do the GC
1148 * - use locking to keep 'ubifs_assert()' happy.
1150 dbg_rcvry("GC'ing LEB %d", lnum
);
1151 mutex_lock_nested(&wbuf
->io_mutex
, wbuf
->jhead
);
1152 err
= ubifs_garbage_collect_leb(c
, &lp
);
1154 int err2
= ubifs_wbuf_sync_nolock(wbuf
);
1159 mutex_unlock(&wbuf
->io_mutex
);
1161 dbg_err("GC failed, error %d", err
);
1166 if (err
!= LEB_RETAINED
) {
1167 dbg_err("GC returned %d", err
);
1170 err
= ubifs_leb_unmap(c
, c
->gc_lnum
);
1173 dbg_rcvry("allocated LEB %d for GC", lnum
);
1178 * There is no GC head LEB or the free space in the GC head LEB is too
1179 * small. Allocate gc_lnum by calling 'ubifs_find_free_leb_for_idx()' so
1182 lnum
= ubifs_find_free_leb_for_idx(c
);
1184 dbg_err("could not find an empty LEB");
1187 /* And reset the index flag */
1188 err
= ubifs_change_one_lp(c
, lnum
, LPROPS_NC
, LPROPS_NC
, 0,
1193 dbg_rcvry("allocated LEB %d for GC", lnum
);
1194 /* Run the commit */
1195 dbg_rcvry("committing");
1196 return ubifs_run_commit(c
);
1200 * struct size_entry - inode size information for recovery.
1201 * @rb: link in the RB-tree of sizes
1202 * @inum: inode number
1203 * @i_size: size on inode
1204 * @d_size: maximum size based on data nodes
1205 * @exists: indicates whether the inode exists
1206 * @inode: inode if pinned in memory awaiting rw mode to fix it
1214 struct inode
*inode
;
1218 * add_ino - add an entry to the size tree.
1219 * @c: UBIFS file-system description object
1220 * @inum: inode number
1221 * @i_size: size on inode
1222 * @d_size: maximum size based on data nodes
1223 * @exists: indicates whether the inode exists
1225 static int add_ino(struct ubifs_info
*c
, ino_t inum
, loff_t i_size
,
1226 loff_t d_size
, int exists
)
1228 struct rb_node
**p
= &c
->size_tree
.rb_node
, *parent
= NULL
;
1229 struct size_entry
*e
;
1233 e
= rb_entry(parent
, struct size_entry
, rb
);
1237 p
= &(*p
)->rb_right
;
1240 e
= kzalloc(sizeof(struct size_entry
), GFP_KERNEL
);
1249 rb_link_node(&e
->rb
, parent
, p
);
1250 rb_insert_color(&e
->rb
, &c
->size_tree
);
1256 * find_ino - find an entry on the size tree.
1257 * @c: UBIFS file-system description object
1258 * @inum: inode number
1260 static struct size_entry
*find_ino(struct ubifs_info
*c
, ino_t inum
)
1262 struct rb_node
*p
= c
->size_tree
.rb_node
;
1263 struct size_entry
*e
;
1266 e
= rb_entry(p
, struct size_entry
, rb
);
1269 else if (inum
> e
->inum
)
1278 * remove_ino - remove an entry from the size tree.
1279 * @c: UBIFS file-system description object
1280 * @inum: inode number
1282 static void remove_ino(struct ubifs_info
*c
, ino_t inum
)
1284 struct size_entry
*e
= find_ino(c
, inum
);
1288 rb_erase(&e
->rb
, &c
->size_tree
);
1293 * ubifs_destroy_size_tree - free resources related to the size tree.
1294 * @c: UBIFS file-system description object
1296 void ubifs_destroy_size_tree(struct ubifs_info
*c
)
1298 struct rb_node
*this = c
->size_tree
.rb_node
;
1299 struct size_entry
*e
;
1302 if (this->rb_left
) {
1303 this = this->rb_left
;
1305 } else if (this->rb_right
) {
1306 this = this->rb_right
;
1309 e
= rb_entry(this, struct size_entry
, rb
);
1312 this = rb_parent(this);
1314 if (this->rb_left
== &e
->rb
)
1315 this->rb_left
= NULL
;
1317 this->rb_right
= NULL
;
1321 c
->size_tree
= RB_ROOT
;
1325 * ubifs_recover_size_accum - accumulate inode sizes for recovery.
1326 * @c: UBIFS file-system description object
1328 * @deletion: node is for a deletion
1329 * @new_size: inode size
1331 * This function has two purposes:
1332 * 1) to ensure there are no data nodes that fall outside the inode size
1333 * 2) to ensure there are no data nodes for inodes that do not exist
1334 * To accomplish those purposes, a rb-tree is constructed containing an entry
1335 * for each inode number in the journal that has not been deleted, and recording
1336 * the size from the inode node, the maximum size of any data node (also altered
1337 * by truncations) and a flag indicating a inode number for which no inode node
1338 * was present in the journal.
1340 * Note that there is still the possibility that there are data nodes that have
1341 * been committed that are beyond the inode size, however the only way to find
1342 * them would be to scan the entire index. Alternatively, some provision could
1343 * be made to record the size of inodes at the start of commit, which would seem
1344 * very cumbersome for a scenario that is quite unlikely and the only negative
1345 * consequence of which is wasted space.
1347 * This functions returns %0 on success and a negative error code on failure.
1349 int ubifs_recover_size_accum(struct ubifs_info
*c
, union ubifs_key
*key
,
1350 int deletion
, loff_t new_size
)
1352 ino_t inum
= key_inum(c
, key
);
1353 struct size_entry
*e
;
1356 switch (key_type(c
, key
)) {
1359 remove_ino(c
, inum
);
1361 e
= find_ino(c
, inum
);
1363 e
->i_size
= new_size
;
1366 err
= add_ino(c
, inum
, new_size
, 0, 1);
1372 case UBIFS_DATA_KEY
:
1373 e
= find_ino(c
, inum
);
1375 if (new_size
> e
->d_size
)
1376 e
->d_size
= new_size
;
1378 err
= add_ino(c
, inum
, 0, new_size
, 0);
1383 case UBIFS_TRUN_KEY
:
1384 e
= find_ino(c
, inum
);
1386 e
->d_size
= new_size
;
1393 * fix_size_in_place - fix inode size in place on flash.
1394 * @c: UBIFS file-system description object
1395 * @e: inode size information for recovery
1397 static int fix_size_in_place(struct ubifs_info
*c
, struct size_entry
*e
)
1399 struct ubifs_ino_node
*ino
= c
->sbuf
;
1401 union ubifs_key key
;
1402 int err
, lnum
, offs
, len
;
1406 /* Locate the inode node LEB number and offset */
1407 ino_key_init(c
, &key
, e
->inum
);
1408 err
= ubifs_tnc_locate(c
, &key
, ino
, &lnum
, &offs
);
1412 * If the size recorded on the inode node is greater than the size that
1413 * was calculated from nodes in the journal then don't change the inode.
1415 i_size
= le64_to_cpu(ino
->size
);
1416 if (i_size
>= e
->d_size
)
1419 err
= ubi_read(c
->ubi
, lnum
, c
->sbuf
, 0, c
->leb_size
);
1422 /* Change the size field and recalculate the CRC */
1423 ino
= c
->sbuf
+ offs
;
1424 ino
->size
= cpu_to_le64(e
->d_size
);
1425 len
= le32_to_cpu(ino
->ch
.len
);
1426 crc
= crc32(UBIFS_CRC32_INIT
, (void *)ino
+ 8, len
- 8);
1427 ino
->ch
.crc
= cpu_to_le32(crc
);
1428 /* Work out where data in the LEB ends and free space begins */
1430 len
= c
->leb_size
- 1;
1431 while (p
[len
] == 0xff)
1433 len
= ALIGN(len
+ 1, c
->min_io_size
);
1434 /* Atomically write the fixed LEB back again */
1435 err
= ubi_leb_change(c
->ubi
, lnum
, c
->sbuf
, len
, UBI_UNKNOWN
);
1438 dbg_rcvry("inode %lu at %d:%d size %lld -> %lld ",
1439 (unsigned long)e
->inum
, lnum
, offs
, i_size
, e
->d_size
);
1443 ubifs_warn("inode %lu failed to fix size %lld -> %lld error %d",
1444 (unsigned long)e
->inum
, e
->i_size
, e
->d_size
, err
);
1449 * ubifs_recover_size - recover inode size.
1450 * @c: UBIFS file-system description object
1452 * This function attempts to fix inode size discrepancies identified by the
1453 * 'ubifs_recover_size_accum()' function.
1455 * This functions returns %0 on success and a negative error code on failure.
1457 int ubifs_recover_size(struct ubifs_info
*c
)
1459 struct rb_node
*this = rb_first(&c
->size_tree
);
1462 struct size_entry
*e
;
1465 e
= rb_entry(this, struct size_entry
, rb
);
1467 union ubifs_key key
;
1469 ino_key_init(c
, &key
, e
->inum
);
1470 err
= ubifs_tnc_lookup(c
, &key
, c
->sbuf
);
1471 if (err
&& err
!= -ENOENT
)
1473 if (err
== -ENOENT
) {
1474 /* Remove data nodes that have no inode */
1475 dbg_rcvry("removing ino %lu",
1476 (unsigned long)e
->inum
);
1477 err
= ubifs_tnc_remove_ino(c
, e
->inum
);
1481 struct ubifs_ino_node
*ino
= c
->sbuf
;
1484 e
->i_size
= le64_to_cpu(ino
->size
);
1487 if (e
->exists
&& e
->i_size
< e
->d_size
) {
1488 if (!e
->inode
&& (c
->vfs_sb
->s_flags
& MS_RDONLY
)) {
1489 /* Fix the inode size and pin it in memory */
1490 struct inode
*inode
;
1492 inode
= ubifs_iget(c
->vfs_sb
, e
->inum
);
1494 return PTR_ERR(inode
);
1495 if (inode
->i_size
< e
->d_size
) {
1496 dbg_rcvry("ino %lu size %lld -> %lld",
1497 (unsigned long)e
->inum
,
1498 e
->d_size
, inode
->i_size
);
1499 inode
->i_size
= e
->d_size
;
1500 ubifs_inode(inode
)->ui_size
= e
->d_size
;
1502 this = rb_next(this);
1507 /* Fix the size in place */
1508 err
= fix_size_in_place(c
, e
);
1515 this = rb_next(this);
1516 rb_erase(&e
->rb
, &c
->size_tree
);