Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * This file is part of UBIFS.
0004  *
0005  * Copyright (C) 2006-2008 Nokia Corporation
0006  *
0007  * Authors: Adrian Hunter
0008  *          Artem Bityutskiy (Битюцкий Артём)
0009  */
0010 
0011 /*
0012  * This file implements functions needed to recover from unclean un-mounts.
0013  * When UBIFS is mounted, it checks a flag on the master node to determine if
0014  * an un-mount was completed successfully. If not, the process of mounting
0015  * incorporates additional checking and fixing of on-flash data structures.
0016  * UBIFS always cleans away all remnants of an unclean un-mount, so that
0017  * errors do not accumulate. However UBIFS defers recovery if it is mounted
0018  * read-only, and the flash is not modified in that case.
0019  *
0020  * The general UBIFS approach to the recovery is that it recovers from
0021  * corruptions which could be caused by power cuts, but it refuses to recover
0022  * from corruption caused by other reasons. And UBIFS tries to distinguish
0023  * between these 2 reasons of corruptions and silently recover in the former
0024  * case and loudly complain in the latter case.
0025  *
0026  * UBIFS writes only to erased LEBs, so it writes only to the flash space
0027  * containing only 0xFFs. UBIFS also always writes strictly from the beginning
0028  * of the LEB to the end. And UBIFS assumes that the underlying flash media
0029  * writes in @c->max_write_size bytes at a time.
0030  *
0031  * Hence, if UBIFS finds a corrupted node at offset X, it expects only the min.
0032  * I/O unit corresponding to offset X to contain corrupted data, all the
0033  * following min. I/O units have to contain empty space (all 0xFFs). If this is
0034  * not true, the corruption cannot be the result of a power cut, and UBIFS
0035  * refuses to mount.
0036  */
0037 
0038 #include <linux/crc32.h>
0039 #include <linux/slab.h>
0040 #include "ubifs.h"
0041 
0042 /**
0043  * is_empty - determine whether a buffer is empty (contains all 0xff).
0044  * @buf: buffer to clean
0045  * @len: length of buffer
0046  *
0047  * This function returns %1 if the buffer is empty (contains all 0xff) otherwise
0048  * %0 is returned.
0049  */
0050 static int is_empty(void *buf, int len)
0051 {
0052     uint8_t *p = buf;
0053     int i;
0054 
0055     for (i = 0; i < len; i++)
0056         if (*p++ != 0xff)
0057             return 0;
0058     return 1;
0059 }
0060 
0061 /**
0062  * first_non_ff - find offset of the first non-0xff byte.
0063  * @buf: buffer to search in
0064  * @len: length of buffer
0065  *
0066  * This function returns offset of the first non-0xff byte in @buf or %-1 if
0067  * the buffer contains only 0xff bytes.
0068  */
0069 static int first_non_ff(void *buf, int len)
0070 {
0071     uint8_t *p = buf;
0072     int i;
0073 
0074     for (i = 0; i < len; i++)
0075         if (*p++ != 0xff)
0076             return i;
0077     return -1;
0078 }
0079 
0080 /**
0081  * get_master_node - get the last valid master node allowing for corruption.
0082  * @c: UBIFS file-system description object
0083  * @lnum: LEB number
0084  * @pbuf: buffer containing the LEB read, is returned here
0085  * @mst: master node, if found, is returned here
0086  * @cor: corruption, if found, is returned here
0087  *
0088  * This function allocates a buffer, reads the LEB into it, and finds and
0089  * returns the last valid master node allowing for one area of corruption.
0090  * The corrupt area, if there is one, must be consistent with the assumption
0091  * that it is the result of an unclean unmount while the master node was being
0092  * written. Under those circumstances, it is valid to use the previously written
0093  * master node.
0094  *
0095  * This function returns %0 on success and a negative error code on failure.
0096  */
0097 static int get_master_node(const struct ubifs_info *c, int lnum, void **pbuf,
0098                struct ubifs_mst_node **mst, void **cor)
0099 {
0100     const int sz = c->mst_node_alsz;
0101     int err, offs, len;
0102     void *sbuf, *buf;
0103 
0104     sbuf = vmalloc(c->leb_size);
0105     if (!sbuf)
0106         return -ENOMEM;
0107 
0108     err = ubifs_leb_read(c, lnum, sbuf, 0, c->leb_size, 0);
0109     if (err && err != -EBADMSG)
0110         goto out_free;
0111 
0112     /* Find the first position that is definitely not a node */
0113     offs = 0;
0114     buf = sbuf;
0115     len = c->leb_size;
0116     while (offs + UBIFS_MST_NODE_SZ <= c->leb_size) {
0117         struct ubifs_ch *ch = buf;
0118 
0119         if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC)
0120             break;
0121         offs += sz;
0122         buf  += sz;
0123         len  -= sz;
0124     }
0125     /* See if there was a valid master node before that */
0126     if (offs) {
0127         int ret;
0128 
0129         offs -= sz;
0130         buf  -= sz;
0131         len  += sz;
0132         ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 1);
0133         if (ret != SCANNED_A_NODE && offs) {
0134             /* Could have been corruption so check one place back */
0135             offs -= sz;
0136             buf  -= sz;
0137             len  += sz;
0138             ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 1);
0139             if (ret != SCANNED_A_NODE)
0140                 /*
0141                  * We accept only one area of corruption because
0142                  * we are assuming that it was caused while
0143                  * trying to write a master node.
0144                  */
0145                 goto out_err;
0146         }
0147         if (ret == SCANNED_A_NODE) {
0148             struct ubifs_ch *ch = buf;
0149 
0150             if (ch->node_type != UBIFS_MST_NODE)
0151                 goto out_err;
0152             dbg_rcvry("found a master node at %d:%d", lnum, offs);
0153             *mst = buf;
0154             offs += sz;
0155             buf  += sz;
0156             len  -= sz;
0157         }
0158     }
0159     /* Check for corruption */
0160     if (offs < c->leb_size) {
0161         if (!is_empty(buf, min_t(int, len, sz))) {
0162             *cor = buf;
0163             dbg_rcvry("found corruption at %d:%d", lnum, offs);
0164         }
0165         offs += sz;
0166         buf  += sz;
0167         len  -= sz;
0168     }
0169     /* Check remaining empty space */
0170     if (offs < c->leb_size)
0171         if (!is_empty(buf, len))
0172             goto out_err;
0173     *pbuf = sbuf;
0174     return 0;
0175 
0176 out_err:
0177     err = -EINVAL;
0178 out_free:
0179     vfree(sbuf);
0180     *mst = NULL;
0181     *cor = NULL;
0182     return err;
0183 }
0184 
0185 /**
0186  * write_rcvrd_mst_node - write recovered master node.
0187  * @c: UBIFS file-system description object
0188  * @mst: master node
0189  *
0190  * This function returns %0 on success and a negative error code on failure.
0191  */
0192 static int write_rcvrd_mst_node(struct ubifs_info *c,
0193                 struct ubifs_mst_node *mst)
0194 {
0195     int err = 0, lnum = UBIFS_MST_LNUM, sz = c->mst_node_alsz;
0196     __le32 save_flags;
0197 
0198     dbg_rcvry("recovery");
0199 
0200     save_flags = mst->flags;
0201     mst->flags |= cpu_to_le32(UBIFS_MST_RCVRY);
0202 
0203     err = ubifs_prepare_node_hmac(c, mst, UBIFS_MST_NODE_SZ,
0204                       offsetof(struct ubifs_mst_node, hmac), 1);
0205     if (err)
0206         goto out;
0207     err = ubifs_leb_change(c, lnum, mst, sz);
0208     if (err)
0209         goto out;
0210     err = ubifs_leb_change(c, lnum + 1, mst, sz);
0211     if (err)
0212         goto out;
0213 out:
0214     mst->flags = save_flags;
0215     return err;
0216 }
0217 
0218 /**
0219  * ubifs_recover_master_node - recover the master node.
0220  * @c: UBIFS file-system description object
0221  *
0222  * This function recovers the master node from corruption that may occur due to
0223  * an unclean unmount.
0224  *
0225  * This function returns %0 on success and a negative error code on failure.
0226  */
0227 int ubifs_recover_master_node(struct ubifs_info *c)
0228 {
0229     void *buf1 = NULL, *buf2 = NULL, *cor1 = NULL, *cor2 = NULL;
0230     struct ubifs_mst_node *mst1 = NULL, *mst2 = NULL, *mst;
0231     const int sz = c->mst_node_alsz;
0232     int err, offs1, offs2;
0233 
0234     dbg_rcvry("recovery");
0235 
0236     err = get_master_node(c, UBIFS_MST_LNUM, &buf1, &mst1, &cor1);
0237     if (err)
0238         goto out_free;
0239 
0240     err = get_master_node(c, UBIFS_MST_LNUM + 1, &buf2, &mst2, &cor2);
0241     if (err)
0242         goto out_free;
0243 
0244     if (mst1) {
0245         offs1 = (void *)mst1 - buf1;
0246         if ((le32_to_cpu(mst1->flags) & UBIFS_MST_RCVRY) &&
0247             (offs1 == 0 && !cor1)) {
0248             /*
0249              * mst1 was written by recovery at offset 0 with no
0250              * corruption.
0251              */
0252             dbg_rcvry("recovery recovery");
0253             mst = mst1;
0254         } else if (mst2) {
0255             offs2 = (void *)mst2 - buf2;
0256             if (offs1 == offs2) {
0257                 /* Same offset, so must be the same */
0258                 if (ubifs_compare_master_node(c, mst1, mst2))
0259                     goto out_err;
0260                 mst = mst1;
0261             } else if (offs2 + sz == offs1) {
0262                 /* 1st LEB was written, 2nd was not */
0263                 if (cor1)
0264                     goto out_err;
0265                 mst = mst1;
0266             } else if (offs1 == 0 &&
0267                    c->leb_size - offs2 - sz < sz) {
0268                 /* 1st LEB was unmapped and written, 2nd not */
0269                 if (cor1)
0270                     goto out_err;
0271                 mst = mst1;
0272             } else
0273                 goto out_err;
0274         } else {
0275             /*
0276              * 2nd LEB was unmapped and about to be written, so
0277              * there must be only one master node in the first LEB
0278              * and no corruption.
0279              */
0280             if (offs1 != 0 || cor1)
0281                 goto out_err;
0282             mst = mst1;
0283         }
0284     } else {
0285         if (!mst2)
0286             goto out_err;
0287         /*
0288          * 1st LEB was unmapped and about to be written, so there must
0289          * be no room left in 2nd LEB.
0290          */
0291         offs2 = (void *)mst2 - buf2;
0292         if (offs2 + sz + sz <= c->leb_size)
0293             goto out_err;
0294         mst = mst2;
0295     }
0296 
0297     ubifs_msg(c, "recovered master node from LEB %d",
0298           (mst == mst1 ? UBIFS_MST_LNUM : UBIFS_MST_LNUM + 1));
0299 
0300     memcpy(c->mst_node, mst, UBIFS_MST_NODE_SZ);
0301 
0302     if (c->ro_mount) {
0303         /* Read-only mode. Keep a copy for switching to rw mode */
0304         c->rcvrd_mst_node = kmalloc(sz, GFP_KERNEL);
0305         if (!c->rcvrd_mst_node) {
0306             err = -ENOMEM;
0307             goto out_free;
0308         }
0309         memcpy(c->rcvrd_mst_node, c->mst_node, UBIFS_MST_NODE_SZ);
0310 
0311         /*
0312          * We had to recover the master node, which means there was an
0313          * unclean reboot. However, it is possible that the master node
0314          * is clean at this point, i.e., %UBIFS_MST_DIRTY is not set.
0315          * E.g., consider the following chain of events:
0316          *
0317          * 1. UBIFS was cleanly unmounted, so the master node is clean
0318          * 2. UBIFS is being mounted R/W and starts changing the master
0319          *    node in the first (%UBIFS_MST_LNUM). A power cut happens,
0320          *    so this LEB ends up with some amount of garbage at the
0321          *    end.
0322          * 3. UBIFS is being mounted R/O. We reach this place and
0323          *    recover the master node from the second LEB
0324          *    (%UBIFS_MST_LNUM + 1). But we cannot update the media
0325          *    because we are being mounted R/O. We have to defer the
0326          *    operation.
0327          * 4. However, this master node (@c->mst_node) is marked as
0328          *    clean (since the step 1). And if we just return, the
0329          *    mount code will be confused and won't recover the master
0330          *    node when it is re-mounter R/W later.
0331          *
0332          *    Thus, to force the recovery by marking the master node as
0333          *    dirty.
0334          */
0335         c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
0336     } else {
0337         /* Write the recovered master node */
0338         c->max_sqnum = le64_to_cpu(mst->ch.sqnum) - 1;
0339         err = write_rcvrd_mst_node(c, c->mst_node);
0340         if (err)
0341             goto out_free;
0342     }
0343 
0344     vfree(buf2);
0345     vfree(buf1);
0346 
0347     return 0;
0348 
0349 out_err:
0350     err = -EINVAL;
0351 out_free:
0352     ubifs_err(c, "failed to recover master node");
0353     if (mst1) {
0354         ubifs_err(c, "dumping first master node");
0355         ubifs_dump_node(c, mst1, c->leb_size - ((void *)mst1 - buf1));
0356     }
0357     if (mst2) {
0358         ubifs_err(c, "dumping second master node");
0359         ubifs_dump_node(c, mst2, c->leb_size - ((void *)mst2 - buf2));
0360     }
0361     vfree(buf2);
0362     vfree(buf1);
0363     return err;
0364 }
0365 
0366 /**
0367  * ubifs_write_rcvrd_mst_node - write the recovered master node.
0368  * @c: UBIFS file-system description object
0369  *
0370  * This function writes the master node that was recovered during mounting in
0371  * read-only mode and must now be written because we are remounting rw.
0372  *
0373  * This function returns %0 on success and a negative error code on failure.
0374  */
0375 int ubifs_write_rcvrd_mst_node(struct ubifs_info *c)
0376 {
0377     int err;
0378 
0379     if (!c->rcvrd_mst_node)
0380         return 0;
0381     c->rcvrd_mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
0382     c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
0383     err = write_rcvrd_mst_node(c, c->rcvrd_mst_node);
0384     if (err)
0385         return err;
0386     kfree(c->rcvrd_mst_node);
0387     c->rcvrd_mst_node = NULL;
0388     return 0;
0389 }
0390 
0391 /**
0392  * is_last_write - determine if an offset was in the last write to a LEB.
0393  * @c: UBIFS file-system description object
0394  * @buf: buffer to check
0395  * @offs: offset to check
0396  *
0397  * This function returns %1 if @offs was in the last write to the LEB whose data
0398  * is in @buf, otherwise %0 is returned. The determination is made by checking
0399  * for subsequent empty space starting from the next @c->max_write_size
0400  * boundary.
0401  */
0402 static int is_last_write(const struct ubifs_info *c, void *buf, int offs)
0403 {
0404     int empty_offs, check_len;
0405     uint8_t *p;
0406 
0407     /*
0408      * Round up to the next @c->max_write_size boundary i.e. @offs is in
0409      * the last wbuf written. After that should be empty space.
0410      */
0411     empty_offs = ALIGN(offs + 1, c->max_write_size);
0412     check_len = c->leb_size - empty_offs;
0413     p = buf + empty_offs - offs;
0414     return is_empty(p, check_len);
0415 }
0416 
0417 /**
0418  * clean_buf - clean the data from an LEB sitting in a buffer.
0419  * @c: UBIFS file-system description object
0420  * @buf: buffer to clean
0421  * @lnum: LEB number to clean
0422  * @offs: offset from which to clean
0423  * @len: length of buffer
0424  *
0425  * This function pads up to the next min_io_size boundary (if there is one) and
0426  * sets empty space to all 0xff. @buf, @offs and @len are updated to the next
0427  * @c->min_io_size boundary.
0428  */
0429 static void clean_buf(const struct ubifs_info *c, void **buf, int lnum,
0430               int *offs, int *len)
0431 {
0432     int empty_offs, pad_len;
0433 
0434     dbg_rcvry("cleaning corruption at %d:%d", lnum, *offs);
0435 
0436     ubifs_assert(c, !(*offs & 7));
0437     empty_offs = ALIGN(*offs, c->min_io_size);
0438     pad_len = empty_offs - *offs;
0439     ubifs_pad(c, *buf, pad_len);
0440     *offs += pad_len;
0441     *buf += pad_len;
0442     *len -= pad_len;
0443     memset(*buf, 0xff, c->leb_size - empty_offs);
0444 }
0445 
0446 /**
0447  * no_more_nodes - determine if there are no more nodes in a buffer.
0448  * @c: UBIFS file-system description object
0449  * @buf: buffer to check
0450  * @len: length of buffer
0451  * @lnum: LEB number of the LEB from which @buf was read
0452  * @offs: offset from which @buf was read
0453  *
0454  * This function ensures that the corrupted node at @offs is the last thing
0455  * written to a LEB. This function returns %1 if more data is not found and
0456  * %0 if more data is found.
0457  */
0458 static int no_more_nodes(const struct ubifs_info *c, void *buf, int len,
0459             int lnum, int offs)
0460 {
0461     struct ubifs_ch *ch = buf;
0462     int skip, dlen = le32_to_cpu(ch->len);
0463 
0464     /* Check for empty space after the corrupt node's common header */
0465     skip = ALIGN(offs + UBIFS_CH_SZ, c->max_write_size) - offs;
0466     if (is_empty(buf + skip, len - skip))
0467         return 1;
0468     /*
0469      * The area after the common header size is not empty, so the common
0470      * header must be intact. Check it.
0471      */
0472     if (ubifs_check_node(c, buf, len, lnum, offs, 1, 0) != -EUCLEAN) {
0473         dbg_rcvry("unexpected bad common header at %d:%d", lnum, offs);
0474         return 0;
0475     }
0476     /* Now we know the corrupt node's length we can skip over it */
0477     skip = ALIGN(offs + dlen, c->max_write_size) - offs;
0478     /* After which there should be empty space */
0479     if (is_empty(buf + skip, len - skip))
0480         return 1;
0481     dbg_rcvry("unexpected data at %d:%d", lnum, offs + skip);
0482     return 0;
0483 }
0484 
0485 /**
0486  * fix_unclean_leb - fix an unclean LEB.
0487  * @c: UBIFS file-system description object
0488  * @sleb: scanned LEB information
0489  * @start: offset where scan started
0490  */
0491 static int fix_unclean_leb(struct ubifs_info *c, struct ubifs_scan_leb *sleb,
0492                int start)
0493 {
0494     int lnum = sleb->lnum, endpt = start;
0495 
0496     /* Get the end offset of the last node we are keeping */
0497     if (!list_empty(&sleb->nodes)) {
0498         struct ubifs_scan_node *snod;
0499 
0500         snod = list_entry(sleb->nodes.prev,
0501                   struct ubifs_scan_node, list);
0502         endpt = snod->offs + snod->len;
0503     }
0504 
0505     if (c->ro_mount && !c->remounting_rw) {
0506         /* Add to recovery list */
0507         struct ubifs_unclean_leb *ucleb;
0508 
0509         dbg_rcvry("need to fix LEB %d start %d endpt %d",
0510               lnum, start, sleb->endpt);
0511         ucleb = kzalloc(sizeof(struct ubifs_unclean_leb), GFP_NOFS);
0512         if (!ucleb)
0513             return -ENOMEM;
0514         ucleb->lnum = lnum;
0515         ucleb->endpt = endpt;
0516         list_add_tail(&ucleb->list, &c->unclean_leb_list);
0517     } else {
0518         /* Write the fixed LEB back to flash */
0519         int err;
0520 
0521         dbg_rcvry("fixing LEB %d start %d endpt %d",
0522               lnum, start, sleb->endpt);
0523         if (endpt == 0) {
0524             err = ubifs_leb_unmap(c, lnum);
0525             if (err)
0526                 return err;
0527         } else {
0528             int len = ALIGN(endpt, c->min_io_size);
0529 
0530             if (start) {
0531                 err = ubifs_leb_read(c, lnum, sleb->buf, 0,
0532                              start, 1);
0533                 if (err)
0534                     return err;
0535             }
0536             /* Pad to min_io_size */
0537             if (len > endpt) {
0538                 int pad_len = len - ALIGN(endpt, 8);
0539 
0540                 if (pad_len > 0) {
0541                     void *buf = sleb->buf + len - pad_len;
0542 
0543                     ubifs_pad(c, buf, pad_len);
0544                 }
0545             }
0546             err = ubifs_leb_change(c, lnum, sleb->buf, len);
0547             if (err)
0548                 return err;
0549         }
0550     }
0551     return 0;
0552 }
0553 
0554 /**
0555  * drop_last_group - drop the last group of nodes.
0556  * @sleb: scanned LEB information
0557  * @offs: offset of dropped nodes is returned here
0558  *
0559  * This is a helper function for 'ubifs_recover_leb()' which drops the last
0560  * group of nodes of the scanned LEB.
0561  */
0562 static void drop_last_group(struct ubifs_scan_leb *sleb, int *offs)
0563 {
0564     while (!list_empty(&sleb->nodes)) {
0565         struct ubifs_scan_node *snod;
0566         struct ubifs_ch *ch;
0567 
0568         snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node,
0569                   list);
0570         ch = snod->node;
0571         if (ch->group_type != UBIFS_IN_NODE_GROUP)
0572             break;
0573 
0574         dbg_rcvry("dropping grouped node at %d:%d",
0575               sleb->lnum, snod->offs);
0576         *offs = snod->offs;
0577         list_del(&snod->list);
0578         kfree(snod);
0579         sleb->nodes_cnt -= 1;
0580     }
0581 }
0582 
0583 /**
0584  * drop_last_node - drop the last node.
0585  * @sleb: scanned LEB information
0586  * @offs: offset of dropped nodes is returned here
0587  *
0588  * This is a helper function for 'ubifs_recover_leb()' which drops the last
0589  * node of the scanned LEB.
0590  */
0591 static void drop_last_node(struct ubifs_scan_leb *sleb, int *offs)
0592 {
0593     struct ubifs_scan_node *snod;
0594 
0595     if (!list_empty(&sleb->nodes)) {
0596         snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node,
0597                   list);
0598 
0599         dbg_rcvry("dropping last node at %d:%d",
0600               sleb->lnum, snod->offs);
0601         *offs = snod->offs;
0602         list_del(&snod->list);
0603         kfree(snod);
0604         sleb->nodes_cnt -= 1;
0605     }
0606 }
0607 
0608 /**
0609  * ubifs_recover_leb - scan and recover a LEB.
0610  * @c: UBIFS file-system description object
0611  * @lnum: LEB number
0612  * @offs: offset
0613  * @sbuf: LEB-sized buffer to use
0614  * @jhead: journal head number this LEB belongs to (%-1 if the LEB does not
0615  *         belong to any journal head)
0616  *
0617  * This function does a scan of a LEB, but caters for errors that might have
0618  * been caused by the unclean unmount from which we are attempting to recover.
0619  * Returns the scanned information on success and a negative error code on
0620  * failure.
0621  */
0622 struct ubifs_scan_leb *ubifs_recover_leb(struct ubifs_info *c, int lnum,
0623                      int offs, void *sbuf, int jhead)
0624 {
0625     int ret = 0, err, len = c->leb_size - offs, start = offs, min_io_unit;
0626     int grouped = jhead == -1 ? 0 : c->jheads[jhead].grouped;
0627     struct ubifs_scan_leb *sleb;
0628     void *buf = sbuf + offs;
0629 
0630     dbg_rcvry("%d:%d, jhead %d, grouped %d", lnum, offs, jhead, grouped);
0631 
0632     sleb = ubifs_start_scan(c, lnum, offs, sbuf);
0633     if (IS_ERR(sleb))
0634         return sleb;
0635 
0636     ubifs_assert(c, len >= 8);
0637     while (len >= 8) {
0638         dbg_scan("look at LEB %d:%d (%d bytes left)",
0639              lnum, offs, len);
0640 
0641         cond_resched();
0642 
0643         /*
0644          * Scan quietly until there is an error from which we cannot
0645          * recover
0646          */
0647         ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 1);
0648         if (ret == SCANNED_A_NODE) {
0649             /* A valid node, and not a padding node */
0650             struct ubifs_ch *ch = buf;
0651             int node_len;
0652 
0653             err = ubifs_add_snod(c, sleb, buf, offs);
0654             if (err)
0655                 goto error;
0656             node_len = ALIGN(le32_to_cpu(ch->len), 8);
0657             offs += node_len;
0658             buf += node_len;
0659             len -= node_len;
0660         } else if (ret > 0) {
0661             /* Padding bytes or a valid padding node */
0662             offs += ret;
0663             buf += ret;
0664             len -= ret;
0665         } else if (ret == SCANNED_EMPTY_SPACE ||
0666                ret == SCANNED_GARBAGE     ||
0667                ret == SCANNED_A_BAD_PAD_NODE ||
0668                ret == SCANNED_A_CORRUPT_NODE) {
0669             dbg_rcvry("found corruption (%d) at %d:%d",
0670                   ret, lnum, offs);
0671             break;
0672         } else {
0673             ubifs_err(c, "unexpected return value %d", ret);
0674             err = -EINVAL;
0675             goto error;
0676         }
0677     }
0678 
0679     if (ret == SCANNED_GARBAGE || ret == SCANNED_A_BAD_PAD_NODE) {
0680         if (!is_last_write(c, buf, offs))
0681             goto corrupted_rescan;
0682     } else if (ret == SCANNED_A_CORRUPT_NODE) {
0683         if (!no_more_nodes(c, buf, len, lnum, offs))
0684             goto corrupted_rescan;
0685     } else if (!is_empty(buf, len)) {
0686         if (!is_last_write(c, buf, offs)) {
0687             int corruption = first_non_ff(buf, len);
0688 
0689             /*
0690              * See header comment for this file for more
0691              * explanations about the reasons we have this check.
0692              */
0693             ubifs_err(c, "corrupt empty space LEB %d:%d, corruption starts at %d",
0694                   lnum, offs, corruption);
0695             /* Make sure we dump interesting non-0xFF data */
0696             offs += corruption;
0697             buf += corruption;
0698             goto corrupted;
0699         }
0700     }
0701 
0702     min_io_unit = round_down(offs, c->min_io_size);
0703     if (grouped)
0704         /*
0705          * If nodes are grouped, always drop the incomplete group at
0706          * the end.
0707          */
0708         drop_last_group(sleb, &offs);
0709 
0710     if (jhead == GCHD) {
0711         /*
0712          * If this LEB belongs to the GC head then while we are in the
0713          * middle of the same min. I/O unit keep dropping nodes. So
0714          * basically, what we want is to make sure that the last min.
0715          * I/O unit where we saw the corruption is dropped completely
0716          * with all the uncorrupted nodes which may possibly sit there.
0717          *
0718          * In other words, let's name the min. I/O unit where the
0719          * corruption starts B, and the previous min. I/O unit A. The
0720          * below code tries to deal with a situation when half of B
0721          * contains valid nodes or the end of a valid node, and the
0722          * second half of B contains corrupted data or garbage. This
0723          * means that UBIFS had been writing to B just before the power
0724          * cut happened. I do not know how realistic is this scenario
0725          * that half of the min. I/O unit had been written successfully
0726          * and the other half not, but this is possible in our 'failure
0727          * mode emulation' infrastructure at least.
0728          *
0729          * So what is the problem, why we need to drop those nodes? Why
0730          * can't we just clean-up the second half of B by putting a
0731          * padding node there? We can, and this works fine with one
0732          * exception which was reproduced with power cut emulation
0733          * testing and happens extremely rarely.
0734          *
0735          * Imagine the file-system is full, we run GC which starts
0736          * moving valid nodes from LEB X to LEB Y (obviously, LEB Y is
0737          * the current GC head LEB). The @c->gc_lnum is -1, which means
0738          * that GC will retain LEB X and will try to continue. Imagine
0739          * that LEB X is currently the dirtiest LEB, and the amount of
0740          * used space in LEB Y is exactly the same as amount of free
0741          * space in LEB X.
0742          *
0743          * And a power cut happens when nodes are moved from LEB X to
0744          * LEB Y. We are here trying to recover LEB Y which is the GC
0745          * head LEB. We find the min. I/O unit B as described above.
0746          * Then we clean-up LEB Y by padding min. I/O unit. And later
0747          * 'ubifs_rcvry_gc_commit()' function fails, because it cannot
0748          * find a dirty LEB which could be GC'd into LEB Y! Even LEB X
0749          * does not match because the amount of valid nodes there does
0750          * not fit the free space in LEB Y any more! And this is
0751          * because of the padding node which we added to LEB Y. The
0752          * user-visible effect of this which I once observed and
0753          * analysed is that we cannot mount the file-system with
0754          * -ENOSPC error.
0755          *
0756          * So obviously, to make sure that situation does not happen we
0757          * should free min. I/O unit B in LEB Y completely and the last
0758          * used min. I/O unit in LEB Y should be A. This is basically
0759          * what the below code tries to do.
0760          */
0761         while (offs > min_io_unit)
0762             drop_last_node(sleb, &offs);
0763     }
0764 
0765     buf = sbuf + offs;
0766     len = c->leb_size - offs;
0767 
0768     clean_buf(c, &buf, lnum, &offs, &len);
0769     ubifs_end_scan(c, sleb, lnum, offs);
0770 
0771     err = fix_unclean_leb(c, sleb, start);
0772     if (err)
0773         goto error;
0774 
0775     return sleb;
0776 
0777 corrupted_rescan:
0778     /* Re-scan the corrupted data with verbose messages */
0779     ubifs_err(c, "corruption %d", ret);
0780     ubifs_scan_a_node(c, buf, len, lnum, offs, 0);
0781 corrupted:
0782     ubifs_scanned_corruption(c, lnum, offs, buf);
0783     err = -EUCLEAN;
0784 error:
0785     ubifs_err(c, "LEB %d scanning failed", lnum);
0786     ubifs_scan_destroy(sleb);
0787     return ERR_PTR(err);
0788 }
0789 
0790 /**
0791  * get_cs_sqnum - get commit start sequence number.
0792  * @c: UBIFS file-system description object
0793  * @lnum: LEB number of commit start node
0794  * @offs: offset of commit start node
0795  * @cs_sqnum: commit start sequence number is returned here
0796  *
0797  * This function returns %0 on success and a negative error code on failure.
0798  */
0799 static int get_cs_sqnum(struct ubifs_info *c, int lnum, int offs,
0800             unsigned long long *cs_sqnum)
0801 {
0802     struct ubifs_cs_node *cs_node = NULL;
0803     int err, ret;
0804 
0805     dbg_rcvry("at %d:%d", lnum, offs);
0806     cs_node = kmalloc(UBIFS_CS_NODE_SZ, GFP_KERNEL);
0807     if (!cs_node)
0808         return -ENOMEM;
0809     if (c->leb_size - offs < UBIFS_CS_NODE_SZ)
0810         goto out_err;
0811     err = ubifs_leb_read(c, lnum, (void *)cs_node, offs,
0812                  UBIFS_CS_NODE_SZ, 0);
0813     if (err && err != -EBADMSG)
0814         goto out_free;
0815     ret = ubifs_scan_a_node(c, cs_node, UBIFS_CS_NODE_SZ, lnum, offs, 0);
0816     if (ret != SCANNED_A_NODE) {
0817         ubifs_err(c, "Not a valid node");
0818         goto out_err;
0819     }
0820     if (cs_node->ch.node_type != UBIFS_CS_NODE) {
0821         ubifs_err(c, "Not a CS node, type is %d", cs_node->ch.node_type);
0822         goto out_err;
0823     }
0824     if (le64_to_cpu(cs_node->cmt_no) != c->cmt_no) {
0825         ubifs_err(c, "CS node cmt_no %llu != current cmt_no %llu",
0826               (unsigned long long)le64_to_cpu(cs_node->cmt_no),
0827               c->cmt_no);
0828         goto out_err;
0829     }
0830     *cs_sqnum = le64_to_cpu(cs_node->ch.sqnum);
0831     dbg_rcvry("commit start sqnum %llu", *cs_sqnum);
0832     kfree(cs_node);
0833     return 0;
0834 
0835 out_err:
0836     err = -EINVAL;
0837 out_free:
0838     ubifs_err(c, "failed to get CS sqnum");
0839     kfree(cs_node);
0840     return err;
0841 }
0842 
0843 /**
0844  * ubifs_recover_log_leb - scan and recover a log LEB.
0845  * @c: UBIFS file-system description object
0846  * @lnum: LEB number
0847  * @offs: offset
0848  * @sbuf: LEB-sized buffer to use
0849  *
0850  * This function does a scan of a LEB, but caters for errors that might have
0851  * been caused by unclean reboots from which we are attempting to recover
0852  * (assume that only the last log LEB can be corrupted by an unclean reboot).
0853  *
0854  * This function returns %0 on success and a negative error code on failure.
0855  */
0856 struct ubifs_scan_leb *ubifs_recover_log_leb(struct ubifs_info *c, int lnum,
0857                          int offs, void *sbuf)
0858 {
0859     struct ubifs_scan_leb *sleb;
0860     int next_lnum;
0861 
0862     dbg_rcvry("LEB %d", lnum);
0863     next_lnum = lnum + 1;
0864     if (next_lnum >= UBIFS_LOG_LNUM + c->log_lebs)
0865         next_lnum = UBIFS_LOG_LNUM;
0866     if (next_lnum != c->ltail_lnum) {
0867         /*
0868          * We can only recover at the end of the log, so check that the
0869          * next log LEB is empty or out of date.
0870          */
0871         sleb = ubifs_scan(c, next_lnum, 0, sbuf, 0);
0872         if (IS_ERR(sleb))
0873             return sleb;
0874         if (sleb->nodes_cnt) {
0875             struct ubifs_scan_node *snod;
0876             unsigned long long cs_sqnum = c->cs_sqnum;
0877 
0878             snod = list_entry(sleb->nodes.next,
0879                       struct ubifs_scan_node, list);
0880             if (cs_sqnum == 0) {
0881                 int err;
0882 
0883                 err = get_cs_sqnum(c, lnum, offs, &cs_sqnum);
0884                 if (err) {
0885                     ubifs_scan_destroy(sleb);
0886                     return ERR_PTR(err);
0887                 }
0888             }
0889             if (snod->sqnum > cs_sqnum) {
0890                 ubifs_err(c, "unrecoverable log corruption in LEB %d",
0891                       lnum);
0892                 ubifs_scan_destroy(sleb);
0893                 return ERR_PTR(-EUCLEAN);
0894             }
0895         }
0896         ubifs_scan_destroy(sleb);
0897     }
0898     return ubifs_recover_leb(c, lnum, offs, sbuf, -1);
0899 }
0900 
0901 /**
0902  * recover_head - recover a head.
0903  * @c: UBIFS file-system description object
0904  * @lnum: LEB number of head to recover
0905  * @offs: offset of head to recover
0906  * @sbuf: LEB-sized buffer to use
0907  *
0908  * This function ensures that there is no data on the flash at a head location.
0909  *
0910  * This function returns %0 on success and a negative error code on failure.
0911  */
0912 static int recover_head(struct ubifs_info *c, int lnum, int offs, void *sbuf)
0913 {
0914     int len = c->max_write_size, err;
0915 
0916     if (offs + len > c->leb_size)
0917         len = c->leb_size - offs;
0918 
0919     if (!len)
0920         return 0;
0921 
0922     /* Read at the head location and check it is empty flash */
0923     err = ubifs_leb_read(c, lnum, sbuf, offs, len, 1);
0924     if (err || !is_empty(sbuf, len)) {
0925         dbg_rcvry("cleaning head at %d:%d", lnum, offs);
0926         if (offs == 0)
0927             return ubifs_leb_unmap(c, lnum);
0928         err = ubifs_leb_read(c, lnum, sbuf, 0, offs, 1);
0929         if (err)
0930             return err;
0931         return ubifs_leb_change(c, lnum, sbuf, offs);
0932     }
0933 
0934     return 0;
0935 }
0936 
0937 /**
0938  * ubifs_recover_inl_heads - recover index and LPT heads.
0939  * @c: UBIFS file-system description object
0940  * @sbuf: LEB-sized buffer to use
0941  *
0942  * This function ensures that there is no data on the flash at the index and
0943  * LPT head locations.
0944  *
0945  * This deals with the recovery of a half-completed journal commit. UBIFS is
0946  * careful never to overwrite the last version of the index or the LPT. Because
0947  * the index and LPT are wandering trees, data from a half-completed commit will
0948  * not be referenced anywhere in UBIFS. The data will be either in LEBs that are
0949  * assumed to be empty and will be unmapped anyway before use, or in the index
0950  * and LPT heads.
0951  *
0952  * This function returns %0 on success and a negative error code on failure.
0953  */
0954 int ubifs_recover_inl_heads(struct ubifs_info *c, void *sbuf)
0955 {
0956     int err;
0957 
0958     ubifs_assert(c, !c->ro_mount || c->remounting_rw);
0959 
0960     dbg_rcvry("checking index head at %d:%d", c->ihead_lnum, c->ihead_offs);
0961     err = recover_head(c, c->ihead_lnum, c->ihead_offs, sbuf);
0962     if (err)
0963         return err;
0964 
0965     dbg_rcvry("checking LPT head at %d:%d", c->nhead_lnum, c->nhead_offs);
0966 
0967     return recover_head(c, c->nhead_lnum, c->nhead_offs, sbuf);
0968 }
0969 
0970 /**
0971  * clean_an_unclean_leb - read and write a LEB to remove corruption.
0972  * @c: UBIFS file-system description object
0973  * @ucleb: unclean LEB information
0974  * @sbuf: LEB-sized buffer to use
0975  *
0976  * This function reads a LEB up to a point pre-determined by the mount recovery,
0977  * checks the nodes, and writes the result back to the flash, thereby cleaning
0978  * off any following corruption, or non-fatal ECC errors.
0979  *
0980  * This function returns %0 on success and a negative error code on failure.
0981  */
0982 static int clean_an_unclean_leb(struct ubifs_info *c,
0983                 struct ubifs_unclean_leb *ucleb, void *sbuf)
0984 {
0985     int err, lnum = ucleb->lnum, offs = 0, len = ucleb->endpt, quiet = 1;
0986     void *buf = sbuf;
0987 
0988     dbg_rcvry("LEB %d len %d", lnum, len);
0989 
0990     if (len == 0) {
0991         /* Nothing to read, just unmap it */
0992         return ubifs_leb_unmap(c, lnum);
0993     }
0994 
0995     err = ubifs_leb_read(c, lnum, buf, offs, len, 0);
0996     if (err && err != -EBADMSG)
0997         return err;
0998 
0999     while (len >= 8) {
1000         int ret;
1001 
1002         cond_resched();
1003 
1004         /* Scan quietly until there is an error */
1005         ret = ubifs_scan_a_node(c, buf, len, lnum, offs, quiet);
1006 
1007         if (ret == SCANNED_A_NODE) {
1008             /* A valid node, and not a padding node */
1009             struct ubifs_ch *ch = buf;
1010             int node_len;
1011 
1012             node_len = ALIGN(le32_to_cpu(ch->len), 8);
1013             offs += node_len;
1014             buf += node_len;
1015             len -= node_len;
1016             continue;
1017         }
1018 
1019         if (ret > 0) {
1020             /* Padding bytes or a valid padding node */
1021             offs += ret;
1022             buf += ret;
1023             len -= ret;
1024             continue;
1025         }
1026 
1027         if (ret == SCANNED_EMPTY_SPACE) {
1028             ubifs_err(c, "unexpected empty space at %d:%d",
1029                   lnum, offs);
1030             return -EUCLEAN;
1031         }
1032 
1033         if (quiet) {
1034             /* Redo the last scan but noisily */
1035             quiet = 0;
1036             continue;
1037         }
1038 
1039         ubifs_scanned_corruption(c, lnum, offs, buf);
1040         return -EUCLEAN;
1041     }
1042 
1043     /* Pad to min_io_size */
1044     len = ALIGN(ucleb->endpt, c->min_io_size);
1045     if (len > ucleb->endpt) {
1046         int pad_len = len - ALIGN(ucleb->endpt, 8);
1047 
1048         if (pad_len > 0) {
1049             buf = c->sbuf + len - pad_len;
1050             ubifs_pad(c, buf, pad_len);
1051         }
1052     }
1053 
1054     /* Write back the LEB atomically */
1055     err = ubifs_leb_change(c, lnum, sbuf, len);
1056     if (err)
1057         return err;
1058 
1059     dbg_rcvry("cleaned LEB %d", lnum);
1060 
1061     return 0;
1062 }
1063 
1064 /**
1065  * ubifs_clean_lebs - clean LEBs recovered during read-only mount.
1066  * @c: UBIFS file-system description object
1067  * @sbuf: LEB-sized buffer to use
1068  *
1069  * This function cleans a LEB identified during recovery that needs to be
1070  * written but was not because UBIFS was mounted read-only. This happens when
1071  * remounting to read-write mode.
1072  *
1073  * This function returns %0 on success and a negative error code on failure.
1074  */
1075 int ubifs_clean_lebs(struct ubifs_info *c, void *sbuf)
1076 {
1077     dbg_rcvry("recovery");
1078     while (!list_empty(&c->unclean_leb_list)) {
1079         struct ubifs_unclean_leb *ucleb;
1080         int err;
1081 
1082         ucleb = list_entry(c->unclean_leb_list.next,
1083                    struct ubifs_unclean_leb, list);
1084         err = clean_an_unclean_leb(c, ucleb, sbuf);
1085         if (err)
1086             return err;
1087         list_del(&ucleb->list);
1088         kfree(ucleb);
1089     }
1090     return 0;
1091 }
1092 
1093 /**
1094  * grab_empty_leb - grab an empty LEB to use as GC LEB and run commit.
1095  * @c: UBIFS file-system description object
1096  *
1097  * This is a helper function for 'ubifs_rcvry_gc_commit()' which grabs an empty
1098  * LEB to be used as GC LEB (@c->gc_lnum), and then runs the commit. Returns
1099  * zero in case of success and a negative error code in case of failure.
1100  */
1101 static int grab_empty_leb(struct ubifs_info *c)
1102 {
1103     int lnum, err;
1104 
1105     /*
1106      * Note, it is very important to first search for an empty LEB and then
1107      * run the commit, not vice-versa. The reason is that there might be
1108      * only one empty LEB at the moment, the one which has been the
1109      * @c->gc_lnum just before the power cut happened. During the regular
1110      * UBIFS operation (not now) @c->gc_lnum is marked as "taken", so no
1111      * one but GC can grab it. But at this moment this single empty LEB is
1112      * not marked as taken, so if we run commit - what happens? Right, the
1113      * commit will grab it and write the index there. Remember that the
1114      * index always expands as long as there is free space, and it only
1115      * starts consolidating when we run out of space.
1116      *
1117      * IOW, if we run commit now, we might not be able to find a free LEB
1118      * after this.
1119      */
1120     lnum = ubifs_find_free_leb_for_idx(c);
1121     if (lnum < 0) {
1122         ubifs_err(c, "could not find an empty LEB");
1123         ubifs_dump_lprops(c);
1124         ubifs_dump_budg(c, &c->bi);
1125         return lnum;
1126     }
1127 
1128     /* Reset the index flag */
1129     err = ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0,
1130                   LPROPS_INDEX, 0);
1131     if (err)
1132         return err;
1133 
1134     c->gc_lnum = lnum;
1135     dbg_rcvry("found empty LEB %d, run commit", lnum);
1136 
1137     return ubifs_run_commit(c);
1138 }
1139 
1140 /**
1141  * ubifs_rcvry_gc_commit - recover the GC LEB number and run the commit.
1142  * @c: UBIFS file-system description object
1143  *
1144  * Out-of-place garbage collection requires always one empty LEB with which to
1145  * start garbage collection. The LEB number is recorded in c->gc_lnum and is
1146  * written to the master node on unmounting. In the case of an unclean unmount
1147  * the value of gc_lnum recorded in the master node is out of date and cannot
1148  * be used. Instead, recovery must allocate an empty LEB for this purpose.
1149  * However, there may not be enough empty space, in which case it must be
1150  * possible to GC the dirtiest LEB into the GC head LEB.
1151  *
1152  * This function also runs the commit which causes the TNC updates from
1153  * size-recovery and orphans to be written to the flash. That is important to
1154  * ensure correct replay order for subsequent mounts.
1155  *
1156  * This function returns %0 on success and a negative error code on failure.
1157  */
1158 int ubifs_rcvry_gc_commit(struct ubifs_info *c)
1159 {
1160     struct ubifs_wbuf *wbuf = &c->jheads[GCHD].wbuf;
1161     struct ubifs_lprops lp;
1162     int err;
1163 
1164     dbg_rcvry("GC head LEB %d, offs %d", wbuf->lnum, wbuf->offs);
1165 
1166     c->gc_lnum = -1;
1167     if (wbuf->lnum == -1 || wbuf->offs == c->leb_size)
1168         return grab_empty_leb(c);
1169 
1170     err = ubifs_find_dirty_leb(c, &lp, wbuf->offs, 2);
1171     if (err) {
1172         if (err != -ENOSPC)
1173             return err;
1174 
1175         dbg_rcvry("could not find a dirty LEB");
1176         return grab_empty_leb(c);
1177     }
1178 
1179     ubifs_assert(c, !(lp.flags & LPROPS_INDEX));
1180     ubifs_assert(c, lp.free + lp.dirty >= wbuf->offs);
1181 
1182     /*
1183      * We run the commit before garbage collection otherwise subsequent
1184      * mounts will see the GC and orphan deletion in a different order.
1185      */
1186     dbg_rcvry("committing");
1187     err = ubifs_run_commit(c);
1188     if (err)
1189         return err;
1190 
1191     dbg_rcvry("GC'ing LEB %d", lp.lnum);
1192     mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
1193     err = ubifs_garbage_collect_leb(c, &lp);
1194     if (err >= 0) {
1195         int err2 = ubifs_wbuf_sync_nolock(wbuf);
1196 
1197         if (err2)
1198             err = err2;
1199     }
1200     mutex_unlock(&wbuf->io_mutex);
1201     if (err < 0) {
1202         ubifs_err(c, "GC failed, error %d", err);
1203         if (err == -EAGAIN)
1204             err = -EINVAL;
1205         return err;
1206     }
1207 
1208     ubifs_assert(c, err == LEB_RETAINED);
1209     if (err != LEB_RETAINED)
1210         return -EINVAL;
1211 
1212     err = ubifs_leb_unmap(c, c->gc_lnum);
1213     if (err)
1214         return err;
1215 
1216     dbg_rcvry("allocated LEB %d for GC", lp.lnum);
1217     return 0;
1218 }
1219 
1220 /**
1221  * struct size_entry - inode size information for recovery.
1222  * @rb: link in the RB-tree of sizes
1223  * @inum: inode number
1224  * @i_size: size on inode
1225  * @d_size: maximum size based on data nodes
1226  * @exists: indicates whether the inode exists
1227  * @inode: inode if pinned in memory awaiting rw mode to fix it
1228  */
1229 struct size_entry {
1230     struct rb_node rb;
1231     ino_t inum;
1232     loff_t i_size;
1233     loff_t d_size;
1234     int exists;
1235     struct inode *inode;
1236 };
1237 
1238 /**
1239  * add_ino - add an entry to the size tree.
1240  * @c: UBIFS file-system description object
1241  * @inum: inode number
1242  * @i_size: size on inode
1243  * @d_size: maximum size based on data nodes
1244  * @exists: indicates whether the inode exists
1245  */
1246 static int add_ino(struct ubifs_info *c, ino_t inum, loff_t i_size,
1247            loff_t d_size, int exists)
1248 {
1249     struct rb_node **p = &c->size_tree.rb_node, *parent = NULL;
1250     struct size_entry *e;
1251 
1252     while (*p) {
1253         parent = *p;
1254         e = rb_entry(parent, struct size_entry, rb);
1255         if (inum < e->inum)
1256             p = &(*p)->rb_left;
1257         else
1258             p = &(*p)->rb_right;
1259     }
1260 
1261     e = kzalloc(sizeof(struct size_entry), GFP_KERNEL);
1262     if (!e)
1263         return -ENOMEM;
1264 
1265     e->inum = inum;
1266     e->i_size = i_size;
1267     e->d_size = d_size;
1268     e->exists = exists;
1269 
1270     rb_link_node(&e->rb, parent, p);
1271     rb_insert_color(&e->rb, &c->size_tree);
1272 
1273     return 0;
1274 }
1275 
1276 /**
1277  * find_ino - find an entry on the size tree.
1278  * @c: UBIFS file-system description object
1279  * @inum: inode number
1280  */
1281 static struct size_entry *find_ino(struct ubifs_info *c, ino_t inum)
1282 {
1283     struct rb_node *p = c->size_tree.rb_node;
1284     struct size_entry *e;
1285 
1286     while (p) {
1287         e = rb_entry(p, struct size_entry, rb);
1288         if (inum < e->inum)
1289             p = p->rb_left;
1290         else if (inum > e->inum)
1291             p = p->rb_right;
1292         else
1293             return e;
1294     }
1295     return NULL;
1296 }
1297 
1298 /**
1299  * remove_ino - remove an entry from the size tree.
1300  * @c: UBIFS file-system description object
1301  * @inum: inode number
1302  */
1303 static void remove_ino(struct ubifs_info *c, ino_t inum)
1304 {
1305     struct size_entry *e = find_ino(c, inum);
1306 
1307     if (!e)
1308         return;
1309     rb_erase(&e->rb, &c->size_tree);
1310     kfree(e);
1311 }
1312 
1313 /**
1314  * ubifs_destroy_size_tree - free resources related to the size tree.
1315  * @c: UBIFS file-system description object
1316  */
1317 void ubifs_destroy_size_tree(struct ubifs_info *c)
1318 {
1319     struct size_entry *e, *n;
1320 
1321     rbtree_postorder_for_each_entry_safe(e, n, &c->size_tree, rb) {
1322         iput(e->inode);
1323         kfree(e);
1324     }
1325 
1326     c->size_tree = RB_ROOT;
1327 }
1328 
1329 /**
1330  * ubifs_recover_size_accum - accumulate inode sizes for recovery.
1331  * @c: UBIFS file-system description object
1332  * @key: node key
1333  * @deletion: node is for a deletion
1334  * @new_size: inode size
1335  *
1336  * This function has two purposes:
1337  *     1) to ensure there are no data nodes that fall outside the inode size
1338  *     2) to ensure there are no data nodes for inodes that do not exist
1339  * To accomplish those purposes, a rb-tree is constructed containing an entry
1340  * for each inode number in the journal that has not been deleted, and recording
1341  * the size from the inode node, the maximum size of any data node (also altered
1342  * by truncations) and a flag indicating a inode number for which no inode node
1343  * was present in the journal.
1344  *
1345  * Note that there is still the possibility that there are data nodes that have
1346  * been committed that are beyond the inode size, however the only way to find
1347  * them would be to scan the entire index. Alternatively, some provision could
1348  * be made to record the size of inodes at the start of commit, which would seem
1349  * very cumbersome for a scenario that is quite unlikely and the only negative
1350  * consequence of which is wasted space.
1351  *
1352  * This functions returns %0 on success and a negative error code on failure.
1353  */
1354 int ubifs_recover_size_accum(struct ubifs_info *c, union ubifs_key *key,
1355                  int deletion, loff_t new_size)
1356 {
1357     ino_t inum = key_inum(c, key);
1358     struct size_entry *e;
1359     int err;
1360 
1361     switch (key_type(c, key)) {
1362     case UBIFS_INO_KEY:
1363         if (deletion)
1364             remove_ino(c, inum);
1365         else {
1366             e = find_ino(c, inum);
1367             if (e) {
1368                 e->i_size = new_size;
1369                 e->exists = 1;
1370             } else {
1371                 err = add_ino(c, inum, new_size, 0, 1);
1372                 if (err)
1373                     return err;
1374             }
1375         }
1376         break;
1377     case UBIFS_DATA_KEY:
1378         e = find_ino(c, inum);
1379         if (e) {
1380             if (new_size > e->d_size)
1381                 e->d_size = new_size;
1382         } else {
1383             err = add_ino(c, inum, 0, new_size, 0);
1384             if (err)
1385                 return err;
1386         }
1387         break;
1388     case UBIFS_TRUN_KEY:
1389         e = find_ino(c, inum);
1390         if (e)
1391             e->d_size = new_size;
1392         break;
1393     }
1394     return 0;
1395 }
1396 
1397 /**
1398  * fix_size_in_place - fix inode size in place on flash.
1399  * @c: UBIFS file-system description object
1400  * @e: inode size information for recovery
1401  */
1402 static int fix_size_in_place(struct ubifs_info *c, struct size_entry *e)
1403 {
1404     struct ubifs_ino_node *ino = c->sbuf;
1405     unsigned char *p;
1406     union ubifs_key key;
1407     int err, lnum, offs, len;
1408     loff_t i_size;
1409     uint32_t crc;
1410 
1411     /* Locate the inode node LEB number and offset */
1412     ino_key_init(c, &key, e->inum);
1413     err = ubifs_tnc_locate(c, &key, ino, &lnum, &offs);
1414     if (err)
1415         goto out;
1416     /*
1417      * If the size recorded on the inode node is greater than the size that
1418      * was calculated from nodes in the journal then don't change the inode.
1419      */
1420     i_size = le64_to_cpu(ino->size);
1421     if (i_size >= e->d_size)
1422         return 0;
1423     /* Read the LEB */
1424     err = ubifs_leb_read(c, lnum, c->sbuf, 0, c->leb_size, 1);
1425     if (err)
1426         goto out;
1427     /* Change the size field and recalculate the CRC */
1428     ino = c->sbuf + offs;
1429     ino->size = cpu_to_le64(e->d_size);
1430     len = le32_to_cpu(ino->ch.len);
1431     crc = crc32(UBIFS_CRC32_INIT, (void *)ino + 8, len - 8);
1432     ino->ch.crc = cpu_to_le32(crc);
1433     /* Work out where data in the LEB ends and free space begins */
1434     p = c->sbuf;
1435     len = c->leb_size - 1;
1436     while (p[len] == 0xff)
1437         len -= 1;
1438     len = ALIGN(len + 1, c->min_io_size);
1439     /* Atomically write the fixed LEB back again */
1440     err = ubifs_leb_change(c, lnum, c->sbuf, len);
1441     if (err)
1442         goto out;
1443     dbg_rcvry("inode %lu at %d:%d size %lld -> %lld",
1444           (unsigned long)e->inum, lnum, offs, i_size, e->d_size);
1445     return 0;
1446 
1447 out:
1448     ubifs_warn(c, "inode %lu failed to fix size %lld -> %lld error %d",
1449            (unsigned long)e->inum, e->i_size, e->d_size, err);
1450     return err;
1451 }
1452 
1453 /**
1454  * inode_fix_size - fix inode size
1455  * @c: UBIFS file-system description object
1456  * @e: inode size information for recovery
1457  */
1458 static int inode_fix_size(struct ubifs_info *c, struct size_entry *e)
1459 {
1460     struct inode *inode;
1461     struct ubifs_inode *ui;
1462     int err;
1463 
1464     if (c->ro_mount)
1465         ubifs_assert(c, !e->inode);
1466 
1467     if (e->inode) {
1468         /* Remounting rw, pick up inode we stored earlier */
1469         inode = e->inode;
1470     } else {
1471         inode = ubifs_iget(c->vfs_sb, e->inum);
1472         if (IS_ERR(inode))
1473             return PTR_ERR(inode);
1474 
1475         if (inode->i_size >= e->d_size) {
1476             /*
1477              * The original inode in the index already has a size
1478              * big enough, nothing to do
1479              */
1480             iput(inode);
1481             return 0;
1482         }
1483 
1484         dbg_rcvry("ino %lu size %lld -> %lld",
1485               (unsigned long)e->inum,
1486               inode->i_size, e->d_size);
1487 
1488         ui = ubifs_inode(inode);
1489 
1490         inode->i_size = e->d_size;
1491         ui->ui_size = e->d_size;
1492         ui->synced_i_size = e->d_size;
1493 
1494         e->inode = inode;
1495     }
1496 
1497     /*
1498      * In readonly mode just keep the inode pinned in memory until we go
1499      * readwrite. In readwrite mode write the inode to the journal with the
1500      * fixed size.
1501      */
1502     if (c->ro_mount)
1503         return 0;
1504 
1505     err = ubifs_jnl_write_inode(c, inode);
1506 
1507     iput(inode);
1508 
1509     if (err)
1510         return err;
1511 
1512     rb_erase(&e->rb, &c->size_tree);
1513     kfree(e);
1514 
1515     return 0;
1516 }
1517 
1518 /**
1519  * ubifs_recover_size - recover inode size.
1520  * @c: UBIFS file-system description object
1521  * @in_place: If true, do a in-place size fixup
1522  *
1523  * This function attempts to fix inode size discrepancies identified by the
1524  * 'ubifs_recover_size_accum()' function.
1525  *
1526  * This functions returns %0 on success and a negative error code on failure.
1527  */
1528 int ubifs_recover_size(struct ubifs_info *c, bool in_place)
1529 {
1530     struct rb_node *this = rb_first(&c->size_tree);
1531 
1532     while (this) {
1533         struct size_entry *e;
1534         int err;
1535 
1536         e = rb_entry(this, struct size_entry, rb);
1537 
1538         this = rb_next(this);
1539 
1540         if (!e->exists) {
1541             union ubifs_key key;
1542 
1543             ino_key_init(c, &key, e->inum);
1544             err = ubifs_tnc_lookup(c, &key, c->sbuf);
1545             if (err && err != -ENOENT)
1546                 return err;
1547             if (err == -ENOENT) {
1548                 /* Remove data nodes that have no inode */
1549                 dbg_rcvry("removing ino %lu",
1550                       (unsigned long)e->inum);
1551                 err = ubifs_tnc_remove_ino(c, e->inum);
1552                 if (err)
1553                     return err;
1554             } else {
1555                 struct ubifs_ino_node *ino = c->sbuf;
1556 
1557                 e->exists = 1;
1558                 e->i_size = le64_to_cpu(ino->size);
1559             }
1560         }
1561 
1562         if (e->exists && e->i_size < e->d_size) {
1563             ubifs_assert(c, !(c->ro_mount && in_place));
1564 
1565             /*
1566              * We found data that is outside the found inode size,
1567              * fixup the inode size
1568              */
1569 
1570             if (in_place) {
1571                 err = fix_size_in_place(c, e);
1572                 if (err)
1573                     return err;
1574                 iput(e->inode);
1575             } else {
1576                 err = inode_fix_size(c, e);
1577                 if (err)
1578                     return err;
1579                 continue;
1580             }
1581         }
1582 
1583         rb_erase(&e->rb, &c->size_tree);
1584         kfree(e);
1585     }
1586 
1587     return 0;
1588 }