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 * first_non_ff - find offset of the first non-0xff byte.
57 * @buf: buffer to search in
58 * @len: length of buffer
60 * This function returns offset of the first non-0xff byte in @buf or %-1 if
61 * the buffer contains only 0xff bytes.
63 static int first_non_ff(void *buf, int len)
68 for (i = 0; i < len; i++)
75 * get_master_node - get the last valid master node allowing for corruption.
76 * @c: UBIFS file-system description object
78 * @pbuf: buffer containing the LEB read, is returned here
79 * @mst: master node, if found, is returned here
80 * @cor: corruption, if found, is returned here
82 * This function allocates a buffer, reads the LEB into it, and finds and
83 * returns the last valid master node allowing for one area of corruption.
84 * The corrupt area, if there is one, must be consistent with the assumption
85 * that it is the result of an unclean unmount while the master node was being
86 * written. Under those circumstances, it is valid to use the previously written
89 * This function returns %0 on success and a negative error code on failure.
91 static int get_master_node(const struct ubifs_info *c, int lnum, void **pbuf,
92 struct ubifs_mst_node **mst, void **cor)
94 const int sz = c->mst_node_alsz;
98 sbuf = vmalloc(c->leb_size);
102 err = ubi_read(c->ubi, lnum, sbuf, 0, c->leb_size);
103 if (err && err != -EBADMSG)
106 /* Find the first position that is definitely not a node */
110 while (offs + UBIFS_MST_NODE_SZ <= c->leb_size) {
111 struct ubifs_ch *ch = buf;
113 if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC)
119 /* See if there was a valid master node before that */
126 ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 1);
127 if (ret != SCANNED_A_NODE && offs) {
128 /* Could have been corruption so check one place back */
132 ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 1);
133 if (ret != SCANNED_A_NODE)
135 * We accept only one area of corruption because
136 * we are assuming that it was caused while
137 * trying to write a master node.
141 if (ret == SCANNED_A_NODE) {
142 struct ubifs_ch *ch = buf;
144 if (ch->node_type != UBIFS_MST_NODE)
146 dbg_rcvry("found a master node at %d:%d", lnum, offs);
153 /* Check for corruption */
154 if (offs < c->leb_size) {
155 if (!is_empty(buf, min_t(int, len, sz))) {
157 dbg_rcvry("found corruption at %d:%d", lnum, offs);
163 /* Check remaining empty space */
164 if (offs < c->leb_size)
165 if (!is_empty(buf, len))
180 * write_rcvrd_mst_node - write recovered master node.
181 * @c: UBIFS file-system description object
184 * This function returns %0 on success and a negative error code on failure.
186 static int write_rcvrd_mst_node(struct ubifs_info *c,
187 struct ubifs_mst_node *mst)
189 int err = 0, lnum = UBIFS_MST_LNUM, sz = c->mst_node_alsz;
192 dbg_rcvry("recovery");
194 save_flags = mst->flags;
195 mst->flags |= cpu_to_le32(UBIFS_MST_RCVRY);
197 ubifs_prepare_node(c, mst, UBIFS_MST_NODE_SZ, 1);
198 err = ubi_leb_change(c->ubi, lnum, mst, sz, UBI_SHORTTERM);
201 err = ubi_leb_change(c->ubi, lnum + 1, mst, sz, UBI_SHORTTERM);
205 mst->flags = save_flags;
210 * ubifs_recover_master_node - recover the master node.
211 * @c: UBIFS file-system description object
213 * This function recovers the master node from corruption that may occur due to
214 * an unclean unmount.
216 * This function returns %0 on success and a negative error code on failure.
218 int ubifs_recover_master_node(struct ubifs_info *c)
220 void *buf1 = NULL, *buf2 = NULL, *cor1 = NULL, *cor2 = NULL;
221 struct ubifs_mst_node *mst1 = NULL, *mst2 = NULL, *mst;
222 const int sz = c->mst_node_alsz;
223 int err, offs1, offs2;
225 dbg_rcvry("recovery");
227 err = get_master_node(c, UBIFS_MST_LNUM, &buf1, &mst1, &cor1);
231 err = get_master_node(c, UBIFS_MST_LNUM + 1, &buf2, &mst2, &cor2);
236 offs1 = (void *)mst1 - buf1;
237 if ((le32_to_cpu(mst1->flags) & UBIFS_MST_RCVRY) &&
238 (offs1 == 0 && !cor1)) {
240 * mst1 was written by recovery at offset 0 with no
243 dbg_rcvry("recovery recovery");
246 offs2 = (void *)mst2 - buf2;
247 if (offs1 == offs2) {
248 /* Same offset, so must be the same */
249 if (memcmp((void *)mst1 + UBIFS_CH_SZ,
250 (void *)mst2 + UBIFS_CH_SZ,
251 UBIFS_MST_NODE_SZ - UBIFS_CH_SZ))
254 } else if (offs2 + sz == offs1) {
255 /* 1st LEB was written, 2nd was not */
259 } else if (offs1 == 0 && offs2 + sz >= c->leb_size) {
260 /* 1st LEB was unmapped and written, 2nd not */
268 * 2nd LEB was unmapped and about to be written, so
269 * there must be only one master node in the first LEB
272 if (offs1 != 0 || cor1)
280 * 1st LEB was unmapped and about to be written, so there must
281 * be no room left in 2nd LEB.
283 offs2 = (void *)mst2 - buf2;
284 if (offs2 + sz + sz <= c->leb_size)
289 ubifs_msg("recovered master node from LEB %d",
290 (mst == mst1 ? UBIFS_MST_LNUM : UBIFS_MST_LNUM + 1));
292 memcpy(c->mst_node, mst, UBIFS_MST_NODE_SZ);
294 if ((c->vfs_sb->s_flags & MS_RDONLY)) {
295 /* Read-only mode. Keep a copy for switching to rw mode */
296 c->rcvrd_mst_node = kmalloc(sz, GFP_KERNEL);
297 if (!c->rcvrd_mst_node) {
301 memcpy(c->rcvrd_mst_node, c->mst_node, UBIFS_MST_NODE_SZ);
304 * We had to recover the master node, which means there was an
305 * unclean reboot. However, it is possible that the master node
306 * is clean at this point, i.e., %UBIFS_MST_DIRTY is not set.
307 * E.g., consider the following chain of events:
309 * 1. UBIFS was cleanly unmounted, so the master node is clean
310 * 2. UBIFS is being mounted R/W and starts changing the master
311 * node in the first (%UBIFS_MST_LNUM). A power cut happens,
312 * so this LEB ends up with some amount of garbage at the
314 * 3. UBIFS is being mounted R/O. We reach this place and
315 * recover the master node from the second LEB
316 * (%UBIFS_MST_LNUM + 1). But we cannot update the media
317 * because we are being mounted R/O. We have to defer the
319 * 4. However, this master node (@c->mst_node) is marked as
320 * clean (since the step 1). And if we just return, the
321 * mount code will be confused and won't recover the master
322 * node when it is re-mounter R/W later.
324 * Thus, to force the recovery by marking the master node as
327 c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
329 /* Write the recovered master node */
330 c->max_sqnum = le64_to_cpu(mst->ch.sqnum) - 1;
331 err = write_rcvrd_mst_node(c, c->mst_node);
344 ubifs_err("failed to recover master node");
346 dbg_err("dumping first master node");
347 dbg_dump_node(c, mst1);
350 dbg_err("dumping second master node");
351 dbg_dump_node(c, mst2);
359 * ubifs_write_rcvrd_mst_node - write the recovered master node.
360 * @c: UBIFS file-system description object
362 * This function writes the master node that was recovered during mounting in
363 * read-only mode and must now be written because we are remounting rw.
365 * This function returns %0 on success and a negative error code on failure.
367 int ubifs_write_rcvrd_mst_node(struct ubifs_info *c)
371 if (!c->rcvrd_mst_node)
373 c->rcvrd_mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
374 c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
375 err = write_rcvrd_mst_node(c, c->rcvrd_mst_node);
378 kfree(c->rcvrd_mst_node);
379 c->rcvrd_mst_node = NULL;
384 * is_last_write - determine if an offset was in the last write to a LEB.
385 * @c: UBIFS file-system description object
386 * @buf: buffer to check
387 * @offs: offset to check
389 * This function returns %1 if @offs was in the last write to the LEB whose data
390 * is in @buf, otherwise %0 is returned. The determination is made by checking
391 * for subsequent empty space starting from the next @c->min_io_size boundary.
393 static int is_last_write(const struct ubifs_info *c, void *buf, int offs)
395 int empty_offs, check_len;
399 * Round up to the next @c->min_io_size boundary i.e. @offs is in the
400 * last wbuf written. After that should be empty space.
402 empty_offs = ALIGN(offs + 1, c->min_io_size);
403 check_len = c->leb_size - empty_offs;
404 p = buf + empty_offs - offs;
405 return is_empty(p, check_len);
409 * clean_buf - clean the data from an LEB sitting in a buffer.
410 * @c: UBIFS file-system description object
411 * @buf: buffer to clean
412 * @lnum: LEB number to clean
413 * @offs: offset from which to clean
414 * @len: length of buffer
416 * This function pads up to the next min_io_size boundary (if there is one) and
417 * sets empty space to all 0xff. @buf, @offs and @len are updated to the next
418 * @c->min_io_size boundary.
420 static void clean_buf(const struct ubifs_info *c, void **buf, int lnum,
423 int empty_offs, pad_len;
426 dbg_rcvry("cleaning corruption at %d:%d", lnum, *offs);
428 ubifs_assert(!(*offs & 7));
429 empty_offs = ALIGN(*offs, c->min_io_size);
430 pad_len = empty_offs - *offs;
431 ubifs_pad(c, *buf, pad_len);
435 memset(*buf, 0xff, c->leb_size - empty_offs);
439 * no_more_nodes - determine if there are no more nodes in a buffer.
440 * @c: UBIFS file-system description object
441 * @buf: buffer to check
442 * @len: length of buffer
443 * @lnum: LEB number of the LEB from which @buf was read
444 * @offs: offset from which @buf was read
446 * This function ensures that the corrupted node at @offs is the last thing
447 * written to a LEB. This function returns %1 if more data is not found and
448 * %0 if more data is found.
450 static int no_more_nodes(const struct ubifs_info *c, void *buf, int len,
453 struct ubifs_ch *ch = buf;
454 int skip, dlen = le32_to_cpu(ch->len);
456 /* Check for empty space after the corrupt node's common header */
457 skip = ALIGN(offs + UBIFS_CH_SZ, c->min_io_size) - offs;
458 if (is_empty(buf + skip, len - skip))
461 * The area after the common header size is not empty, so the common
462 * header must be intact. Check it.
464 if (ubifs_check_node(c, buf, lnum, offs, 1, 0) != -EUCLEAN) {
465 dbg_rcvry("unexpected bad common header at %d:%d", lnum, offs);
468 /* Now we know the corrupt node's length we can skip over it */
469 skip = ALIGN(offs + dlen, c->min_io_size) - offs;
470 /* After which there should be empty space */
471 if (is_empty(buf + skip, len - skip))
473 dbg_rcvry("unexpected data at %d:%d", lnum, offs + skip);
478 * fix_unclean_leb - fix an unclean LEB.
479 * @c: UBIFS file-system description object
480 * @sleb: scanned LEB information
481 * @start: offset where scan started
483 static int fix_unclean_leb(struct ubifs_info *c, struct ubifs_scan_leb *sleb,
486 int lnum = sleb->lnum, endpt = start;
488 /* Get the end offset of the last node we are keeping */
489 if (!list_empty(&sleb->nodes)) {
490 struct ubifs_scan_node *snod;
492 snod = list_entry(sleb->nodes.prev,
493 struct ubifs_scan_node, list);
494 endpt = snod->offs + snod->len;
497 if ((c->vfs_sb->s_flags & MS_RDONLY) && !c->remounting_rw) {
498 /* Add to recovery list */
499 struct ubifs_unclean_leb *ucleb;
501 dbg_rcvry("need to fix LEB %d start %d endpt %d",
502 lnum, start, sleb->endpt);
503 ucleb = kzalloc(sizeof(struct ubifs_unclean_leb), GFP_NOFS);
507 ucleb->endpt = endpt;
508 list_add_tail(&ucleb->list, &c->unclean_leb_list);
510 /* Write the fixed LEB back to flash */
513 dbg_rcvry("fixing LEB %d start %d endpt %d",
514 lnum, start, sleb->endpt);
516 err = ubifs_leb_unmap(c, lnum);
520 int len = ALIGN(endpt, c->min_io_size);
523 err = ubi_read(c->ubi, lnum, sleb->buf, 0,
528 /* Pad to min_io_size */
530 int pad_len = len - ALIGN(endpt, 8);
533 void *buf = sleb->buf + len - pad_len;
535 ubifs_pad(c, buf, pad_len);
538 err = ubi_leb_change(c->ubi, lnum, sleb->buf, len,
548 * drop_incomplete_group - drop nodes from an incomplete group.
549 * @sleb: scanned LEB information
550 * @offs: offset of dropped nodes is returned here
552 * This function returns %1 if nodes are dropped and %0 otherwise.
554 static int drop_incomplete_group(struct ubifs_scan_leb *sleb, int *offs)
558 while (!list_empty(&sleb->nodes)) {
559 struct ubifs_scan_node *snod;
562 snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node,
565 if (ch->group_type != UBIFS_IN_NODE_GROUP)
567 dbg_rcvry("dropping node at %d:%d", sleb->lnum, snod->offs);
569 list_del(&snod->list);
571 sleb->nodes_cnt -= 1;
578 * ubifs_recover_leb - scan and recover a LEB.
579 * @c: UBIFS file-system description object
582 * @sbuf: LEB-sized buffer to use
583 * @grouped: nodes may be grouped for recovery
585 * This function does a scan of a LEB, but caters for errors that might have
586 * been caused by the unclean unmount from which we are attempting to recover.
587 * Returns %0 in case of success, %-EUCLEAN if an unrecoverable corruption is
588 * found, and a negative error code in case of failure.
590 struct ubifs_scan_leb *ubifs_recover_leb(struct ubifs_info *c, int lnum,
591 int offs, void *sbuf, int grouped)
593 int err, len = c->leb_size - offs, need_clean = 0, quiet = 1;
594 int empty_chkd = 0, start = offs;
595 struct ubifs_scan_leb *sleb;
596 void *buf = sbuf + offs;
598 dbg_rcvry("%d:%d", lnum, offs);
600 sleb = ubifs_start_scan(c, lnum, offs, sbuf);
610 dbg_scan("look at LEB %d:%d (%d bytes left)",
616 * Scan quietly until there is an error from which we cannot
619 ret = ubifs_scan_a_node(c, buf, len, lnum, offs, quiet);
621 if (ret == SCANNED_A_NODE) {
622 /* A valid node, and not a padding node */
623 struct ubifs_ch *ch = buf;
626 err = ubifs_add_snod(c, sleb, buf, offs);
629 node_len = ALIGN(le32_to_cpu(ch->len), 8);
637 /* Padding bytes or a valid padding node */
644 if (ret == SCANNED_EMPTY_SPACE) {
645 if (!is_empty(buf, len)) {
646 if (!is_last_write(c, buf, offs))
648 clean_buf(c, &buf, lnum, &offs, &len);
655 if (ret == SCANNED_GARBAGE || ret == SCANNED_A_BAD_PAD_NODE)
656 if (is_last_write(c, buf, offs)) {
657 clean_buf(c, &buf, lnum, &offs, &len);
663 if (ret == SCANNED_A_CORRUPT_NODE)
664 if (no_more_nodes(c, buf, len, lnum, offs)) {
665 clean_buf(c, &buf, lnum, &offs, &len);
672 /* Redo the last scan but noisily */
678 case SCANNED_GARBAGE:
681 case SCANNED_A_CORRUPT_NODE:
682 case SCANNED_A_BAD_PAD_NODE:
692 if (!empty_chkd && !is_empty(buf, len)) {
693 if (is_last_write(c, buf, offs)) {
694 clean_buf(c, &buf, lnum, &offs, &len);
697 int corruption = first_non_ff(buf, len);
699 ubifs_err("corrupt empty space LEB %d:%d, corruption "
700 "starts at %d", lnum, offs, corruption);
701 /* Make sure we dump interesting non-0xFF data */
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, 0);
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,
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);
877 if (err || !is_empty(sbuf, len)) {
878 dbg_rcvry("cleaning head at %d:%d", lnum, offs);
880 return ubifs_leb_unmap(c, lnum);
881 err = ubi_read(c->ubi, lnum, sbuf, 0, offs);
884 return ubi_leb_change(c->ubi, lnum, sbuf, offs, UBI_UNKNOWN);
891 * ubifs_recover_inl_heads - recover index and LPT heads.
892 * @c: UBIFS file-system description object
893 * @sbuf: LEB-sized buffer to use
895 * This function ensures that there is no data on the flash at the index and
896 * LPT head locations.
898 * This deals with the recovery of a half-completed journal commit. UBIFS is
899 * careful never to overwrite the last version of the index or the LPT. Because
900 * the index and LPT are wandering trees, data from a half-completed commit will
901 * not be referenced anywhere in UBIFS. The data will be either in LEBs that are
902 * assumed to be empty and will be unmapped anyway before use, or in the index
905 * This function returns %0 on success and a negative error code on failure.
907 int ubifs_recover_inl_heads(const struct ubifs_info *c, void *sbuf)
911 ubifs_assert(!(c->vfs_sb->s_flags & MS_RDONLY) || c->remounting_rw);
913 dbg_rcvry("checking index head at %d:%d", c->ihead_lnum, c->ihead_offs);
914 err = recover_head(c, c->ihead_lnum, c->ihead_offs, sbuf);
918 dbg_rcvry("checking LPT head at %d:%d", c->nhead_lnum, c->nhead_offs);
919 err = recover_head(c, c->nhead_lnum, c->nhead_offs, sbuf);
927 * clean_an_unclean_leb - read and write a LEB to remove corruption.
928 * @c: UBIFS file-system description object
929 * @ucleb: unclean LEB information
930 * @sbuf: LEB-sized buffer to use
932 * This function reads a LEB up to a point pre-determined by the mount recovery,
933 * checks the nodes, and writes the result back to the flash, thereby cleaning
934 * off any following corruption, or non-fatal ECC errors.
936 * This function returns %0 on success and a negative error code on failure.
938 static int clean_an_unclean_leb(const struct ubifs_info *c,
939 struct ubifs_unclean_leb *ucleb, void *sbuf)
941 int err, lnum = ucleb->lnum, offs = 0, len = ucleb->endpt, quiet = 1;
944 dbg_rcvry("LEB %d len %d", lnum, len);
947 /* Nothing to read, just unmap it */
948 err = ubifs_leb_unmap(c, lnum);
954 err = ubi_read(c->ubi, lnum, buf, offs, len);
955 if (err && err != -EBADMSG)
963 /* Scan quietly until there is an error */
964 ret = ubifs_scan_a_node(c, buf, len, lnum, offs, quiet);
966 if (ret == SCANNED_A_NODE) {
967 /* A valid node, and not a padding node */
968 struct ubifs_ch *ch = buf;
971 node_len = ALIGN(le32_to_cpu(ch->len), 8);
979 /* Padding bytes or a valid padding node */
986 if (ret == SCANNED_EMPTY_SPACE) {
987 ubifs_err("unexpected empty space at %d:%d",
993 /* Redo the last scan but noisily */
998 ubifs_scanned_corruption(c, lnum, offs, buf);
1002 /* Pad to min_io_size */
1003 len = ALIGN(ucleb->endpt, c->min_io_size);
1004 if (len > ucleb->endpt) {
1005 int pad_len = len - ALIGN(ucleb->endpt, 8);
1008 buf = c->sbuf + len - pad_len;
1009 ubifs_pad(c, buf, pad_len);
1013 /* Write back the LEB atomically */
1014 err = ubi_leb_change(c->ubi, lnum, sbuf, len, UBI_UNKNOWN);
1018 dbg_rcvry("cleaned LEB %d", lnum);
1024 * ubifs_clean_lebs - clean LEBs recovered during read-only mount.
1025 * @c: UBIFS file-system description object
1026 * @sbuf: LEB-sized buffer to use
1028 * This function cleans a LEB identified during recovery that needs to be
1029 * written but was not because UBIFS was mounted read-only. This happens when
1030 * remounting to read-write mode.
1032 * This function returns %0 on success and a negative error code on failure.
1034 int ubifs_clean_lebs(const struct ubifs_info *c, void *sbuf)
1036 dbg_rcvry("recovery");
1037 while (!list_empty(&c->unclean_leb_list)) {
1038 struct ubifs_unclean_leb *ucleb;
1041 ucleb = list_entry(c->unclean_leb_list.next,
1042 struct ubifs_unclean_leb, list);
1043 err = clean_an_unclean_leb(c, ucleb, sbuf);
1046 list_del(&ucleb->list);
1053 * ubifs_rcvry_gc_commit - recover the GC LEB number and run the commit.
1054 * @c: UBIFS file-system description object
1056 * Out-of-place garbage collection requires always one empty LEB with which to
1057 * start garbage collection. The LEB number is recorded in c->gc_lnum and is
1058 * written to the master node on unmounting. In the case of an unclean unmount
1059 * the value of gc_lnum recorded in the master node is out of date and cannot
1060 * be used. Instead, recovery must allocate an empty LEB for this purpose.
1061 * However, there may not be enough empty space, in which case it must be
1062 * possible to GC the dirtiest LEB into the GC head LEB.
1064 * This function also runs the commit which causes the TNC updates from
1065 * size-recovery and orphans to be written to the flash. That is important to
1066 * ensure correct replay order for subsequent mounts.
1068 * This function returns %0 on success and a negative error code on failure.
1070 int ubifs_rcvry_gc_commit(struct ubifs_info *c)
1072 struct ubifs_wbuf *wbuf = &c->jheads[GCHD].wbuf;
1073 struct ubifs_lprops lp;
1077 if (wbuf->lnum == -1) {
1078 dbg_rcvry("no GC head LEB");
1082 * See whether the used space in the dirtiest LEB fits in the GC head
1085 if (wbuf->offs == c->leb_size) {
1086 dbg_rcvry("no room in GC head LEB");
1089 err = ubifs_find_dirty_leb(c, &lp, wbuf->offs, 2);
1092 dbg_err("could not find a dirty LEB");
1095 ubifs_assert(!(lp.flags & LPROPS_INDEX));
1097 if (lp.free + lp.dirty == c->leb_size) {
1098 /* An empty LEB was returned */
1099 if (lp.free != c->leb_size) {
1100 err = ubifs_change_one_lp(c, lnum, c->leb_size,
1105 err = ubifs_leb_unmap(c, lnum);
1109 dbg_rcvry("allocated LEB %d for GC", lnum);
1110 /* Run the commit */
1111 dbg_rcvry("committing");
1112 return ubifs_run_commit(c);
1115 * There was no empty LEB so the used space in the dirtiest LEB must fit
1116 * in the GC head LEB.
1118 if (lp.free + lp.dirty < wbuf->offs) {
1119 dbg_rcvry("LEB %d doesn't fit in GC head LEB %d:%d",
1120 lnum, wbuf->lnum, wbuf->offs);
1121 err = ubifs_return_leb(c, lnum);
1127 * We run the commit before garbage collection otherwise subsequent
1128 * mounts will see the GC and orphan deletion in a different order.
1130 dbg_rcvry("committing");
1131 err = ubifs_run_commit(c);
1135 * The data in the dirtiest LEB fits in the GC head LEB, so do the GC
1136 * - use locking to keep 'ubifs_assert()' happy.
1138 dbg_rcvry("GC'ing LEB %d", lnum);
1139 mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
1140 err = ubifs_garbage_collect_leb(c, &lp);
1142 int err2 = ubifs_wbuf_sync_nolock(wbuf);
1147 mutex_unlock(&wbuf->io_mutex);
1149 dbg_err("GC failed, error %d", err);
1154 if (err != LEB_RETAINED) {
1155 dbg_err("GC returned %d", err);
1158 err = ubifs_leb_unmap(c, c->gc_lnum);
1161 dbg_rcvry("allocated LEB %d for GC", lnum);
1166 * There is no GC head LEB or the free space in the GC head LEB is too
1167 * small. Allocate gc_lnum by calling 'ubifs_find_free_leb_for_idx()' so
1170 lnum = ubifs_find_free_leb_for_idx(c);
1172 dbg_err("could not find an empty LEB");
1175 /* And reset the index flag */
1176 err = ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0,
1181 dbg_rcvry("allocated LEB %d for GC", lnum);
1182 /* Run the commit */
1183 dbg_rcvry("committing");
1184 return ubifs_run_commit(c);
1188 * struct size_entry - inode size information for recovery.
1189 * @rb: link in the RB-tree of sizes
1190 * @inum: inode number
1191 * @i_size: size on inode
1192 * @d_size: maximum size based on data nodes
1193 * @exists: indicates whether the inode exists
1194 * @inode: inode if pinned in memory awaiting rw mode to fix it
1202 struct inode *inode;
1206 * add_ino - add an entry to the size tree.
1207 * @c: UBIFS file-system description object
1208 * @inum: inode number
1209 * @i_size: size on inode
1210 * @d_size: maximum size based on data nodes
1211 * @exists: indicates whether the inode exists
1213 static int add_ino(struct ubifs_info *c, ino_t inum, loff_t i_size,
1214 loff_t d_size, int exists)
1216 struct rb_node **p = &c->size_tree.rb_node, *parent = NULL;
1217 struct size_entry *e;
1221 e = rb_entry(parent, struct size_entry, rb);
1225 p = &(*p)->rb_right;
1228 e = kzalloc(sizeof(struct size_entry), GFP_KERNEL);
1237 rb_link_node(&e->rb, parent, p);
1238 rb_insert_color(&e->rb, &c->size_tree);
1244 * find_ino - find an entry on the size tree.
1245 * @c: UBIFS file-system description object
1246 * @inum: inode number
1248 static struct size_entry *find_ino(struct ubifs_info *c, ino_t inum)
1250 struct rb_node *p = c->size_tree.rb_node;
1251 struct size_entry *e;
1254 e = rb_entry(p, struct size_entry, rb);
1257 else if (inum > e->inum)
1266 * remove_ino - remove an entry from the size tree.
1267 * @c: UBIFS file-system description object
1268 * @inum: inode number
1270 static void remove_ino(struct ubifs_info *c, ino_t inum)
1272 struct size_entry *e = find_ino(c, inum);
1276 rb_erase(&e->rb, &c->size_tree);
1281 * ubifs_destroy_size_tree - free resources related to the size tree.
1282 * @c: UBIFS file-system description object
1284 void ubifs_destroy_size_tree(struct ubifs_info *c)
1286 struct rb_node *this = c->size_tree.rb_node;
1287 struct size_entry *e;
1290 if (this->rb_left) {
1291 this = this->rb_left;
1293 } else if (this->rb_right) {
1294 this = this->rb_right;
1297 e = rb_entry(this, struct size_entry, rb);
1300 this = rb_parent(this);
1302 if (this->rb_left == &e->rb)
1303 this->rb_left = NULL;
1305 this->rb_right = NULL;
1309 c->size_tree = RB_ROOT;
1313 * ubifs_recover_size_accum - accumulate inode sizes for recovery.
1314 * @c: UBIFS file-system description object
1316 * @deletion: node is for a deletion
1317 * @new_size: inode size
1319 * This function has two purposes:
1320 * 1) to ensure there are no data nodes that fall outside the inode size
1321 * 2) to ensure there are no data nodes for inodes that do not exist
1322 * To accomplish those purposes, a rb-tree is constructed containing an entry
1323 * for each inode number in the journal that has not been deleted, and recording
1324 * the size from the inode node, the maximum size of any data node (also altered
1325 * by truncations) and a flag indicating a inode number for which no inode node
1326 * was present in the journal.
1328 * Note that there is still the possibility that there are data nodes that have
1329 * been committed that are beyond the inode size, however the only way to find
1330 * them would be to scan the entire index. Alternatively, some provision could
1331 * be made to record the size of inodes at the start of commit, which would seem
1332 * very cumbersome for a scenario that is quite unlikely and the only negative
1333 * consequence of which is wasted space.
1335 * This functions returns %0 on success and a negative error code on failure.
1337 int ubifs_recover_size_accum(struct ubifs_info *c, union ubifs_key *key,
1338 int deletion, loff_t new_size)
1340 ino_t inum = key_inum(c, key);
1341 struct size_entry *e;
1344 switch (key_type(c, key)) {
1347 remove_ino(c, inum);
1349 e = find_ino(c, inum);
1351 e->i_size = new_size;
1354 err = add_ino(c, inum, new_size, 0, 1);
1360 case UBIFS_DATA_KEY:
1361 e = find_ino(c, inum);
1363 if (new_size > e->d_size)
1364 e->d_size = new_size;
1366 err = add_ino(c, inum, 0, new_size, 0);
1371 case UBIFS_TRUN_KEY:
1372 e = find_ino(c, inum);
1374 e->d_size = new_size;
1381 * fix_size_in_place - fix inode size in place on flash.
1382 * @c: UBIFS file-system description object
1383 * @e: inode size information for recovery
1385 static int fix_size_in_place(struct ubifs_info *c, struct size_entry *e)
1387 struct ubifs_ino_node *ino = c->sbuf;
1389 union ubifs_key key;
1390 int err, lnum, offs, len;
1394 /* Locate the inode node LEB number and offset */
1395 ino_key_init(c, &key, e->inum);
1396 err = ubifs_tnc_locate(c, &key, ino, &lnum, &offs);
1400 * If the size recorded on the inode node is greater than the size that
1401 * was calculated from nodes in the journal then don't change the inode.
1403 i_size = le64_to_cpu(ino->size);
1404 if (i_size >= e->d_size)
1407 err = ubi_read(c->ubi, lnum, c->sbuf, 0, c->leb_size);
1410 /* Change the size field and recalculate the CRC */
1411 ino = c->sbuf + offs;
1412 ino->size = cpu_to_le64(e->d_size);
1413 len = le32_to_cpu(ino->ch.len);
1414 crc = crc32(UBIFS_CRC32_INIT, (void *)ino + 8, len - 8);
1415 ino->ch.crc = cpu_to_le32(crc);
1416 /* Work out where data in the LEB ends and free space begins */
1418 len = c->leb_size - 1;
1419 while (p[len] == 0xff)
1421 len = ALIGN(len + 1, c->min_io_size);
1422 /* Atomically write the fixed LEB back again */
1423 err = ubi_leb_change(c->ubi, lnum, c->sbuf, len, UBI_UNKNOWN);
1426 dbg_rcvry("inode %lu at %d:%d size %lld -> %lld ",
1427 (unsigned long)e->inum, lnum, offs, i_size, e->d_size);
1431 ubifs_warn("inode %lu failed to fix size %lld -> %lld error %d",
1432 (unsigned long)e->inum, e->i_size, e->d_size, err);
1437 * ubifs_recover_size - recover inode size.
1438 * @c: UBIFS file-system description object
1440 * This function attempts to fix inode size discrepancies identified by the
1441 * 'ubifs_recover_size_accum()' function.
1443 * This functions returns %0 on success and a negative error code on failure.
1445 int ubifs_recover_size(struct ubifs_info *c)
1447 struct rb_node *this = rb_first(&c->size_tree);
1450 struct size_entry *e;
1453 e = rb_entry(this, struct size_entry, rb);
1455 union ubifs_key key;
1457 ino_key_init(c, &key, e->inum);
1458 err = ubifs_tnc_lookup(c, &key, c->sbuf);
1459 if (err && err != -ENOENT)
1461 if (err == -ENOENT) {
1462 /* Remove data nodes that have no inode */
1463 dbg_rcvry("removing ino %lu",
1464 (unsigned long)e->inum);
1465 err = ubifs_tnc_remove_ino(c, e->inum);
1469 struct ubifs_ino_node *ino = c->sbuf;
1472 e->i_size = le64_to_cpu(ino->size);
1475 if (e->exists && e->i_size < e->d_size) {
1476 if (!e->inode && (c->vfs_sb->s_flags & MS_RDONLY)) {
1477 /* Fix the inode size and pin it in memory */
1478 struct inode *inode;
1480 inode = ubifs_iget(c->vfs_sb, e->inum);
1482 return PTR_ERR(inode);
1483 if (inode->i_size < e->d_size) {
1484 dbg_rcvry("ino %lu size %lld -> %lld",
1485 (unsigned long)e->inum,
1486 e->d_size, inode->i_size);
1487 inode->i_size = e->d_size;
1488 ubifs_inode(inode)->ui_size = e->d_size;
1490 this = rb_next(this);
1495 /* Fix the size in place */
1496 err = fix_size_in_place(c, e);
1503 this = rb_next(this);
1504 rb_erase(&e->rb, &c->size_tree);