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.
36 * is_empty - determine whether a buffer is empty (contains all 0xff).
37 * @buf: buffer to clean
38 * @len: length of buffer
40 * This function returns %1 if the buffer is empty (contains all 0xff) otherwise
43 static int is_empty(void *buf, int len)
48 for (i = 0; i < len; i++)
55 * get_master_node - get the last valid master node allowing for corruption.
56 * @c: UBIFS file-system description object
58 * @pbuf: buffer containing the LEB read, is returned here
59 * @mst: master node, if found, is returned here
60 * @cor: corruption, if found, is returned here
62 * This function allocates a buffer, reads the LEB into it, and finds and
63 * returns the last valid master node allowing for one area of corruption.
64 * The corrupt area, if there is one, must be consistent with the assumption
65 * that it is the result of an unclean unmount while the master node was being
66 * written. Under those circumstances, it is valid to use the previously written
69 * This function returns %0 on success and a negative error code on failure.
71 static int get_master_node(const struct ubifs_info *c, int lnum, void **pbuf,
72 struct ubifs_mst_node **mst, void **cor)
74 const int sz = c->mst_node_alsz;
78 sbuf = vmalloc(c->leb_size);
82 err = ubi_read(c->ubi, lnum, sbuf, 0, c->leb_size);
83 if (err && err != -EBADMSG)
86 /* Find the first position that is definitely not a node */
90 while (offs + UBIFS_MST_NODE_SZ <= c->leb_size) {
91 struct ubifs_ch *ch = buf;
93 if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC)
99 /* See if there was a valid master node before that */
106 ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 1);
107 if (ret != SCANNED_A_NODE && offs) {
108 /* Could have been corruption so check one place back */
112 ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 1);
113 if (ret != SCANNED_A_NODE)
115 * We accept only one area of corruption because
116 * we are assuming that it was caused while
117 * trying to write a master node.
121 if (ret == SCANNED_A_NODE) {
122 struct ubifs_ch *ch = buf;
124 if (ch->node_type != UBIFS_MST_NODE)
126 dbg_rcvry("found a master node at %d:%d", lnum, offs);
133 /* Check for corruption */
134 if (offs < c->leb_size) {
135 if (!is_empty(buf, min_t(int, len, sz))) {
137 dbg_rcvry("found corruption at %d:%d", lnum, offs);
143 /* Check remaining empty space */
144 if (offs < c->leb_size)
145 if (!is_empty(buf, len))
160 * write_rcvrd_mst_node - write recovered master node.
161 * @c: UBIFS file-system description object
164 * This function returns %0 on success and a negative error code on failure.
166 static int write_rcvrd_mst_node(struct ubifs_info *c,
167 struct ubifs_mst_node *mst)
169 int err = 0, lnum = UBIFS_MST_LNUM, sz = c->mst_node_alsz;
172 dbg_rcvry("recovery");
174 save_flags = mst->flags;
175 mst->flags |= cpu_to_le32(UBIFS_MST_RCVRY);
177 ubifs_prepare_node(c, mst, UBIFS_MST_NODE_SZ, 1);
178 err = ubi_leb_change(c->ubi, lnum, mst, sz, UBI_SHORTTERM);
181 err = ubi_leb_change(c->ubi, lnum + 1, mst, sz, UBI_SHORTTERM);
185 mst->flags = save_flags;
190 * ubifs_recover_master_node - recover the master node.
191 * @c: UBIFS file-system description object
193 * This function recovers the master node from corruption that may occur due to
194 * an unclean unmount.
196 * This function returns %0 on success and a negative error code on failure.
198 int ubifs_recover_master_node(struct ubifs_info *c)
200 void *buf1 = NULL, *buf2 = NULL, *cor1 = NULL, *cor2 = NULL;
201 struct ubifs_mst_node *mst1 = NULL, *mst2 = NULL, *mst;
202 const int sz = c->mst_node_alsz;
203 int err, offs1, offs2;
205 dbg_rcvry("recovery");
207 err = get_master_node(c, UBIFS_MST_LNUM, &buf1, &mst1, &cor1);
211 err = get_master_node(c, UBIFS_MST_LNUM + 1, &buf2, &mst2, &cor2);
216 offs1 = (void *)mst1 - buf1;
217 if ((le32_to_cpu(mst1->flags) & UBIFS_MST_RCVRY) &&
218 (offs1 == 0 && !cor1)) {
220 * mst1 was written by recovery at offset 0 with no
223 dbg_rcvry("recovery recovery");
226 offs2 = (void *)mst2 - buf2;
227 if (offs1 == offs2) {
228 /* Same offset, so must be the same */
229 if (memcmp((void *)mst1 + UBIFS_CH_SZ,
230 (void *)mst2 + UBIFS_CH_SZ,
231 UBIFS_MST_NODE_SZ - UBIFS_CH_SZ))
234 } else if (offs2 + sz == offs1) {
235 /* 1st LEB was written, 2nd was not */
239 } else if (offs1 == 0 && offs2 + sz >= c->leb_size) {
240 /* 1st LEB was unmapped and written, 2nd not */
248 * 2nd LEB was unmapped and about to be written, so
249 * there must be only one master node in the first LEB
252 if (offs1 != 0 || cor1)
260 * 1st LEB was unmapped and about to be written, so there must
261 * be no room left in 2nd LEB.
263 offs2 = (void *)mst2 - buf2;
264 if (offs2 + sz + sz <= c->leb_size)
269 dbg_rcvry("recovered master node from LEB %d",
270 (mst == mst1 ? UBIFS_MST_LNUM : UBIFS_MST_LNUM + 1));
272 memcpy(c->mst_node, mst, UBIFS_MST_NODE_SZ);
274 if ((c->vfs_sb->s_flags & MS_RDONLY)) {
275 /* Read-only mode. Keep a copy for switching to rw mode */
276 c->rcvrd_mst_node = kmalloc(sz, GFP_KERNEL);
277 if (!c->rcvrd_mst_node) {
281 memcpy(c->rcvrd_mst_node, c->mst_node, UBIFS_MST_NODE_SZ);
292 ubifs_err("failed to recover master node");
294 dbg_err("dumping first master node");
295 dbg_dump_node(c, mst1);
298 dbg_err("dumping second master node");
299 dbg_dump_node(c, mst2);
307 * ubifs_write_rcvrd_mst_node - write the recovered master node.
308 * @c: UBIFS file-system description object
310 * This function writes the master node that was recovered during mounting in
311 * read-only mode and must now be written because we are remounting rw.
313 * This function returns %0 on success and a negative error code on failure.
315 int ubifs_write_rcvrd_mst_node(struct ubifs_info *c)
319 if (!c->rcvrd_mst_node)
321 c->rcvrd_mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
322 c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
323 err = write_rcvrd_mst_node(c, c->rcvrd_mst_node);
326 kfree(c->rcvrd_mst_node);
327 c->rcvrd_mst_node = NULL;
332 * is_last_write - determine if an offset was in the last write to a LEB.
333 * @c: UBIFS file-system description object
334 * @buf: buffer to check
335 * @offs: offset to check
337 * This function returns %1 if @offs was in the last write to the LEB whose data
338 * is in @buf, otherwise %0 is returned. The determination is made by checking
339 * for subsequent empty space starting from the next min_io_size boundary (or a
340 * bit less than the common header size if min_io_size is one).
342 static int is_last_write(const struct ubifs_info *c, void *buf, int offs)
348 if (c->min_io_size == 1) {
349 check_len = c->leb_size - offs;
351 for (; check_len > 0; check_len--)
355 * 'check_len' is the size of the corruption which cannot be
356 * more than the size of 1 node if it was caused by an unclean
359 if (check_len > UBIFS_MAX_NODE_SZ)
365 * Round up to the next c->min_io_size boundary i.e. 'offs' is in the
366 * last wbuf written. After that should be empty space.
368 empty_offs = ALIGN(offs + 1, c->min_io_size);
369 check_len = c->leb_size - empty_offs;
370 p = buf + empty_offs - offs;
372 for (; check_len > 0; check_len--)
379 * clean_buf - clean the data from an LEB sitting in a buffer.
380 * @c: UBIFS file-system description object
381 * @buf: buffer to clean
382 * @lnum: LEB number to clean
383 * @offs: offset from which to clean
384 * @len: length of buffer
386 * This function pads up to the next min_io_size boundary (if there is one) and
387 * sets empty space to all 0xff. @buf, @offs and @len are updated to the next
388 * min_io_size boundary (if there is one).
390 static void clean_buf(const struct ubifs_info *c, void **buf, int lnum,
393 int empty_offs, pad_len;
396 dbg_rcvry("cleaning corruption at %d:%d", lnum, *offs);
398 if (c->min_io_size == 1) {
399 memset(*buf, 0xff, c->leb_size - *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->vfs_sb->s_flags & MS_RDONLY) && !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);
489 * drop_incomplete_group - drop nodes from an incomplete group.
490 * @sleb: scanned LEB information
491 * @offs: offset of dropped nodes is returned here
493 * This function returns %1 if nodes are dropped and %0 otherwise.
495 static int drop_incomplete_group(struct ubifs_scan_leb *sleb, int *offs)
499 while (!list_empty(&sleb->nodes)) {
500 struct ubifs_scan_node *snod;
503 snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node,
506 if (ch->group_type != UBIFS_IN_NODE_GROUP)
508 dbg_rcvry("dropping node at %d:%d", sleb->lnum, snod->offs);
510 list_del(&snod->list);
512 sleb->nodes_cnt -= 1;
519 * ubifs_recover_leb - scan and recover a LEB.
520 * @c: UBIFS file-system description object
523 * @sbuf: LEB-sized buffer to use
524 * @grouped: nodes may be grouped for recovery
526 * This function does a scan of a LEB, but caters for errors that might have
527 * been caused by the unclean unmount from which we are attempting to recover.
529 * This function returns %0 on success and a negative error code on failure.
531 struct ubifs_scan_leb *ubifs_recover_leb(struct ubifs_info *c, int lnum,
532 int offs, void *sbuf, int grouped)
534 int err, len = c->leb_size - offs, need_clean = 0, quiet = 1;
535 int empty_chkd = 0, start = offs;
536 struct ubifs_scan_leb *sleb;
537 void *buf = sbuf + offs;
539 dbg_rcvry("%d:%d", lnum, offs);
541 sleb = ubifs_start_scan(c, lnum, offs, sbuf);
551 dbg_scan("look at LEB %d:%d (%d bytes left)",
557 * Scan quietly until there is an error from which we cannot
560 ret = ubifs_scan_a_node(c, buf, len, lnum, offs, quiet);
562 if (ret == SCANNED_A_NODE) {
563 /* A valid node, and not a padding node */
564 struct ubifs_ch *ch = buf;
567 err = ubifs_add_snod(c, sleb, buf, offs);
570 node_len = ALIGN(le32_to_cpu(ch->len), 8);
578 /* Padding bytes or a valid padding node */
585 if (ret == SCANNED_EMPTY_SPACE) {
586 if (!is_empty(buf, len)) {
587 if (!is_last_write(c, buf, offs))
589 clean_buf(c, &buf, lnum, &offs, &len);
596 if (ret == SCANNED_GARBAGE || ret == SCANNED_A_BAD_PAD_NODE)
597 if (is_last_write(c, buf, offs)) {
598 clean_buf(c, &buf, lnum, &offs, &len);
604 if (ret == SCANNED_A_CORRUPT_NODE)
605 if (no_more_nodes(c, buf, len, lnum, offs)) {
606 clean_buf(c, &buf, lnum, &offs, &len);
613 /* Redo the last scan but noisily */
619 case SCANNED_GARBAGE:
622 case SCANNED_A_CORRUPT_NODE:
623 case SCANNED_A_BAD_PAD_NODE:
632 if (!empty_chkd && !is_empty(buf, len)) {
633 if (is_last_write(c, buf, offs)) {
634 clean_buf(c, &buf, lnum, &offs, &len);
637 ubifs_err("corrupt empty space at LEB %d:%d",
643 /* Drop nodes from incomplete group */
644 if (grouped && drop_incomplete_group(sleb, &offs)) {
646 len = c->leb_size - offs;
647 clean_buf(c, &buf, lnum, &offs, &len);
651 if (offs % c->min_io_size) {
652 clean_buf(c, &buf, lnum, &offs, &len);
656 ubifs_end_scan(c, sleb, lnum, offs);
659 err = fix_unclean_leb(c, sleb, start);
667 ubifs_scanned_corruption(c, lnum, offs, buf);
670 ubifs_err("LEB %d scanning failed", lnum);
671 ubifs_scan_destroy(sleb);
676 * get_cs_sqnum - get commit start sequence number.
677 * @c: UBIFS file-system description object
678 * @lnum: LEB number of commit start node
679 * @offs: offset of commit start node
680 * @cs_sqnum: commit start sequence number is returned here
682 * This function returns %0 on success and a negative error code on failure.
684 static int get_cs_sqnum(struct ubifs_info *c, int lnum, int offs,
685 unsigned long long *cs_sqnum)
687 struct ubifs_cs_node *cs_node = NULL;
690 dbg_rcvry("at %d:%d", lnum, offs);
691 cs_node = kmalloc(UBIFS_CS_NODE_SZ, GFP_KERNEL);
694 if (c->leb_size - offs < UBIFS_CS_NODE_SZ)
696 err = ubi_read(c->ubi, lnum, (void *)cs_node, offs, UBIFS_CS_NODE_SZ);
697 if (err && err != -EBADMSG)
699 ret = ubifs_scan_a_node(c, cs_node, UBIFS_CS_NODE_SZ, lnum, offs, 0);
700 if (ret != SCANNED_A_NODE) {
701 dbg_err("Not a valid node");
704 if (cs_node->ch.node_type != UBIFS_CS_NODE) {
705 dbg_err("Node a CS node, type is %d", cs_node->ch.node_type);
708 if (le64_to_cpu(cs_node->cmt_no) != c->cmt_no) {
709 dbg_err("CS node cmt_no %llu != current cmt_no %llu",
710 (unsigned long long)le64_to_cpu(cs_node->cmt_no),
714 *cs_sqnum = le64_to_cpu(cs_node->ch.sqnum);
715 dbg_rcvry("commit start sqnum %llu", *cs_sqnum);
722 ubifs_err("failed to get CS sqnum");
728 * ubifs_recover_log_leb - scan and recover a log LEB.
729 * @c: UBIFS file-system description object
732 * @sbuf: LEB-sized buffer to use
734 * This function does a scan of a LEB, but caters for errors that might have
735 * been caused by the unclean unmount from which we are attempting to recover.
737 * This function returns %0 on success and a negative error code on failure.
739 struct ubifs_scan_leb *ubifs_recover_log_leb(struct ubifs_info *c, int lnum,
740 int offs, void *sbuf)
742 struct ubifs_scan_leb *sleb;
745 dbg_rcvry("LEB %d", lnum);
746 next_lnum = lnum + 1;
747 if (next_lnum >= UBIFS_LOG_LNUM + c->log_lebs)
748 next_lnum = UBIFS_LOG_LNUM;
749 if (next_lnum != c->ltail_lnum) {
751 * We can only recover at the end of the log, so check that the
752 * next log LEB is empty or out of date.
754 sleb = ubifs_scan(c, next_lnum, 0, sbuf);
757 if (sleb->nodes_cnt) {
758 struct ubifs_scan_node *snod;
759 unsigned long long cs_sqnum = c->cs_sqnum;
761 snod = list_entry(sleb->nodes.next,
762 struct ubifs_scan_node, list);
766 err = get_cs_sqnum(c, lnum, offs, &cs_sqnum);
768 ubifs_scan_destroy(sleb);
772 if (snod->sqnum > cs_sqnum) {
773 ubifs_err("unrecoverable log corruption "
775 ubifs_scan_destroy(sleb);
776 return ERR_PTR(-EUCLEAN);
779 ubifs_scan_destroy(sleb);
781 return ubifs_recover_leb(c, lnum, offs, sbuf, 0);
785 * recover_head - recover a head.
786 * @c: UBIFS file-system description object
787 * @lnum: LEB number of head to recover
788 * @offs: offset of head to recover
789 * @sbuf: LEB-sized buffer to use
791 * This function ensures that there is no data on the flash at a head location.
793 * This function returns %0 on success and a negative error code on failure.
795 static int recover_head(const struct ubifs_info *c, int lnum, int offs,
798 int len, err, need_clean = 0;
800 if (c->min_io_size > 1)
801 len = c->min_io_size;
804 if (offs + len > c->leb_size)
805 len = c->leb_size - offs;
810 /* Read at the head location and check it is empty flash */
811 err = ubi_read(c->ubi, lnum, sbuf, offs, len);
825 dbg_rcvry("cleaning head at %d:%d", lnum, offs);
827 return ubifs_leb_unmap(c, lnum);
828 err = ubi_read(c->ubi, lnum, sbuf, 0, offs);
831 return ubi_leb_change(c->ubi, lnum, sbuf, offs, UBI_UNKNOWN);
838 * ubifs_recover_inl_heads - recover index and LPT heads.
839 * @c: UBIFS file-system description object
840 * @sbuf: LEB-sized buffer to use
842 * This function ensures that there is no data on the flash at the index and
843 * LPT head locations.
845 * This deals with the recovery of a half-completed journal commit. UBIFS is
846 * careful never to overwrite the last version of the index or the LPT. Because
847 * the index and LPT are wandering trees, data from a half-completed commit will
848 * not be referenced anywhere in UBIFS. The data will be either in LEBs that are
849 * assumed to be empty and will be unmapped anyway before use, or in the index
852 * This function returns %0 on success and a negative error code on failure.
854 int ubifs_recover_inl_heads(const struct ubifs_info *c, void *sbuf)
858 ubifs_assert(!(c->vfs_sb->s_flags & MS_RDONLY) || c->remounting_rw);
860 dbg_rcvry("checking index head at %d:%d", c->ihead_lnum, c->ihead_offs);
861 err = recover_head(c, c->ihead_lnum, c->ihead_offs, sbuf);
865 dbg_rcvry("checking LPT head at %d:%d", c->nhead_lnum, c->nhead_offs);
866 err = recover_head(c, c->nhead_lnum, c->nhead_offs, sbuf);
874 * clean_an_unclean_leb - read and write a LEB to remove corruption.
875 * @c: UBIFS file-system description object
876 * @ucleb: unclean LEB information
877 * @sbuf: LEB-sized buffer to use
879 * This function reads a LEB up to a point pre-determined by the mount recovery,
880 * checks the nodes, and writes the result back to the flash, thereby cleaning
881 * off any following corruption, or non-fatal ECC errors.
883 * This function returns %0 on success and a negative error code on failure.
885 static int clean_an_unclean_leb(const struct ubifs_info *c,
886 struct ubifs_unclean_leb *ucleb, void *sbuf)
888 int err, lnum = ucleb->lnum, offs = 0, len = ucleb->endpt, quiet = 1;
891 dbg_rcvry("LEB %d len %d", lnum, len);
894 /* Nothing to read, just unmap it */
895 err = ubifs_leb_unmap(c, lnum);
901 err = ubi_read(c->ubi, lnum, buf, offs, len);
902 if (err && err != -EBADMSG)
910 /* Scan quietly until there is an error */
911 ret = ubifs_scan_a_node(c, buf, len, lnum, offs, quiet);
913 if (ret == SCANNED_A_NODE) {
914 /* A valid node, and not a padding node */
915 struct ubifs_ch *ch = buf;
918 node_len = ALIGN(le32_to_cpu(ch->len), 8);
926 /* Padding bytes or a valid padding node */
933 if (ret == SCANNED_EMPTY_SPACE) {
934 ubifs_err("unexpected empty space at %d:%d",
940 /* Redo the last scan but noisily */
945 ubifs_scanned_corruption(c, lnum, offs, buf);
949 /* Pad to min_io_size */
950 len = ALIGN(ucleb->endpt, c->min_io_size);
951 if (len > ucleb->endpt) {
952 int pad_len = len - ALIGN(ucleb->endpt, 8);
955 buf = c->sbuf + len - pad_len;
956 ubifs_pad(c, buf, pad_len);
960 /* Write back the LEB atomically */
961 err = ubi_leb_change(c->ubi, lnum, sbuf, len, UBI_UNKNOWN);
965 dbg_rcvry("cleaned LEB %d", lnum);
971 * ubifs_clean_lebs - clean LEBs recovered during read-only mount.
972 * @c: UBIFS file-system description object
973 * @sbuf: LEB-sized buffer to use
975 * This function cleans a LEB identified during recovery that needs to be
976 * written but was not because UBIFS was mounted read-only. This happens when
977 * remounting to read-write mode.
979 * This function returns %0 on success and a negative error code on failure.
981 int ubifs_clean_lebs(const struct ubifs_info *c, void *sbuf)
983 dbg_rcvry("recovery");
984 while (!list_empty(&c->unclean_leb_list)) {
985 struct ubifs_unclean_leb *ucleb;
988 ucleb = list_entry(c->unclean_leb_list.next,
989 struct ubifs_unclean_leb, list);
990 err = clean_an_unclean_leb(c, ucleb, sbuf);
993 list_del(&ucleb->list);
1000 * struct size_entry - inode size information for recovery.
1001 * @rb: link in the RB-tree of sizes
1002 * @inum: inode number
1003 * @i_size: size on inode
1004 * @d_size: maximum size based on data nodes
1005 * @exists: indicates whether the inode exists
1006 * @inode: inode if pinned in memory awaiting rw mode to fix it
1014 struct inode *inode;
1018 * add_ino - add an entry to the size tree.
1019 * @c: UBIFS file-system description object
1020 * @inum: inode number
1021 * @i_size: size on inode
1022 * @d_size: maximum size based on data nodes
1023 * @exists: indicates whether the inode exists
1025 static int add_ino(struct ubifs_info *c, ino_t inum, loff_t i_size,
1026 loff_t d_size, int exists)
1028 struct rb_node **p = &c->size_tree.rb_node, *parent = NULL;
1029 struct size_entry *e;
1033 e = rb_entry(parent, struct size_entry, rb);
1037 p = &(*p)->rb_right;
1040 e = kzalloc(sizeof(struct size_entry), GFP_KERNEL);
1049 rb_link_node(&e->rb, parent, p);
1050 rb_insert_color(&e->rb, &c->size_tree);
1056 * find_ino - find an entry on the size tree.
1057 * @c: UBIFS file-system description object
1058 * @inum: inode number
1060 static struct size_entry *find_ino(struct ubifs_info *c, ino_t inum)
1062 struct rb_node *p = c->size_tree.rb_node;
1063 struct size_entry *e;
1066 e = rb_entry(p, struct size_entry, rb);
1069 else if (inum > e->inum)
1078 * remove_ino - remove an entry from the size tree.
1079 * @c: UBIFS file-system description object
1080 * @inum: inode number
1082 static void remove_ino(struct ubifs_info *c, ino_t inum)
1084 struct size_entry *e = find_ino(c, inum);
1088 rb_erase(&e->rb, &c->size_tree);
1093 * ubifs_recover_size_accum - accumulate inode sizes for recovery.
1094 * @c: UBIFS file-system description object
1096 * @deletion: node is for a deletion
1097 * @new_size: inode size
1099 * This function has two purposes:
1100 * 1) to ensure there are no data nodes that fall outside the inode size
1101 * 2) to ensure there are no data nodes for inodes that do not exist
1102 * To accomplish those purposes, a rb-tree is constructed containing an entry
1103 * for each inode number in the journal that has not been deleted, and recording
1104 * the size from the inode node, the maximum size of any data node (also altered
1105 * by truncations) and a flag indicating a inode number for which no inode node
1106 * was present in the journal.
1108 * Note that there is still the possibility that there are data nodes that have
1109 * been committed that are beyond the inode size, however the only way to find
1110 * them would be to scan the entire index. Alternatively, some provision could
1111 * be made to record the size of inodes at the start of commit, which would seem
1112 * very cumbersome for a scenario that is quite unlikely and the only negative
1113 * consequence of which is wasted space.
1115 * This functions returns %0 on success and a negative error code on failure.
1117 int ubifs_recover_size_accum(struct ubifs_info *c, union ubifs_key *key,
1118 int deletion, loff_t new_size)
1120 ino_t inum = key_inum(c, key);
1121 struct size_entry *e;
1124 switch (key_type(c, key)) {
1127 remove_ino(c, inum);
1129 e = find_ino(c, inum);
1131 e->i_size = new_size;
1134 err = add_ino(c, inum, new_size, 0, 1);
1140 case UBIFS_DATA_KEY:
1141 e = find_ino(c, inum);
1143 if (new_size > e->d_size)
1144 e->d_size = new_size;
1146 err = add_ino(c, inum, 0, new_size, 0);
1151 case UBIFS_TRUN_KEY:
1152 e = find_ino(c, inum);
1154 e->d_size = new_size;
1161 * ubifs_recover_size - recover inode size.
1162 * @c: UBIFS file-system description object
1164 * This function attempts to fix inode size discrepancies identified by the
1165 * 'ubifs_recover_size_accum()' function.
1167 * This functions returns %0 on success and a negative error code on failure.
1169 int ubifs_recover_size(struct ubifs_info *c)
1171 struct rb_node *this = rb_first(&c->size_tree);
1174 struct size_entry *e;
1177 e = rb_entry(this, struct size_entry, rb);
1179 union ubifs_key key;
1181 ino_key_init(c, &key, e->inum);
1182 err = ubifs_tnc_lookup(c, &key, c->sbuf);
1183 if (err && err != -ENOENT)
1185 if (err == -ENOENT) {
1186 /* Remove data nodes that have no inode */
1187 dbg_rcvry("removing ino %lu",
1188 (unsigned long)e->inum);
1189 err = ubifs_tnc_remove_ino(c, e->inum);
1193 struct ubifs_ino_node *ino = c->sbuf;
1196 e->i_size = le64_to_cpu(ino->size);
1199 if (e->exists && e->i_size < e->d_size) {
1200 if (!e->inode && (c->vfs_sb->s_flags & MS_RDONLY)) {
1201 /* Fix the inode size and pin it in memory */
1202 struct inode *inode;
1204 inode = ubifs_iget(c->vfs_sb, e->inum);
1206 return PTR_ERR(inode);
1207 if (inode->i_size < e->d_size) {
1208 dbg_rcvry("ino %lu size %lld -> %lld",
1209 (unsigned long)e->inum,
1210 e->d_size, inode->i_size);
1211 inode->i_size = e->d_size;
1212 ubifs_inode(inode)->ui_size = e->d_size;
1214 this = rb_next(this);
1220 this = rb_next(this);
1221 rb_erase(&e->rb, &c->size_tree);