1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * This file is part of UBIFS.
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright (C) 2006-2008 Nokia Corporation
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0+
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * Authors: Adrian Hunter
9*4882a593Smuzhiyun * Artem Bityutskiy (Битюцкий Артём)
10*4882a593Smuzhiyun */
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun /*
13*4882a593Smuzhiyun * This file implements functions needed to recover from unclean un-mounts.
14*4882a593Smuzhiyun * When UBIFS is mounted, it checks a flag on the master node to determine if
15*4882a593Smuzhiyun * an un-mount was completed successfully. If not, the process of mounting
16*4882a593Smuzhiyun * incorporates additional checking and fixing of on-flash data structures.
17*4882a593Smuzhiyun * UBIFS always cleans away all remnants of an unclean un-mount, so that
18*4882a593Smuzhiyun * errors do not accumulate. However UBIFS defers recovery if it is mounted
19*4882a593Smuzhiyun * read-only, and the flash is not modified in that case.
20*4882a593Smuzhiyun *
21*4882a593Smuzhiyun * The general UBIFS approach to the recovery is that it recovers from
22*4882a593Smuzhiyun * corruptions which could be caused by power cuts, but it refuses to recover
23*4882a593Smuzhiyun * from corruption caused by other reasons. And UBIFS tries to distinguish
24*4882a593Smuzhiyun * between these 2 reasons of corruptions and silently recover in the former
25*4882a593Smuzhiyun * case and loudly complain in the latter case.
26*4882a593Smuzhiyun *
27*4882a593Smuzhiyun * UBIFS writes only to erased LEBs, so it writes only to the flash space
28*4882a593Smuzhiyun * containing only 0xFFs. UBIFS also always writes strictly from the beginning
29*4882a593Smuzhiyun * of the LEB to the end. And UBIFS assumes that the underlying flash media
30*4882a593Smuzhiyun * writes in @c->max_write_size bytes at a time.
31*4882a593Smuzhiyun *
32*4882a593Smuzhiyun * Hence, if UBIFS finds a corrupted node at offset X, it expects only the min.
33*4882a593Smuzhiyun * I/O unit corresponding to offset X to contain corrupted data, all the
34*4882a593Smuzhiyun * following min. I/O units have to contain empty space (all 0xFFs). If this is
35*4882a593Smuzhiyun * not true, the corruption cannot be the result of a power cut, and UBIFS
36*4882a593Smuzhiyun * refuses to mount.
37*4882a593Smuzhiyun */
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun #ifndef __UBOOT__
40*4882a593Smuzhiyun #include <linux/crc32.h>
41*4882a593Smuzhiyun #include <linux/slab.h>
42*4882a593Smuzhiyun #else
43*4882a593Smuzhiyun #include <linux/err.h>
44*4882a593Smuzhiyun #endif
45*4882a593Smuzhiyun #include "ubifs.h"
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun /**
48*4882a593Smuzhiyun * is_empty - determine whether a buffer is empty (contains all 0xff).
49*4882a593Smuzhiyun * @buf: buffer to clean
50*4882a593Smuzhiyun * @len: length of buffer
51*4882a593Smuzhiyun *
52*4882a593Smuzhiyun * This function returns %1 if the buffer is empty (contains all 0xff) otherwise
53*4882a593Smuzhiyun * %0 is returned.
54*4882a593Smuzhiyun */
is_empty(void * buf,int len)55*4882a593Smuzhiyun static int is_empty(void *buf, int len)
56*4882a593Smuzhiyun {
57*4882a593Smuzhiyun uint8_t *p = buf;
58*4882a593Smuzhiyun int i;
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun for (i = 0; i < len; i++)
61*4882a593Smuzhiyun if (*p++ != 0xff)
62*4882a593Smuzhiyun return 0;
63*4882a593Smuzhiyun return 1;
64*4882a593Smuzhiyun }
65*4882a593Smuzhiyun
66*4882a593Smuzhiyun /**
67*4882a593Smuzhiyun * first_non_ff - find offset of the first non-0xff byte.
68*4882a593Smuzhiyun * @buf: buffer to search in
69*4882a593Smuzhiyun * @len: length of buffer
70*4882a593Smuzhiyun *
71*4882a593Smuzhiyun * This function returns offset of the first non-0xff byte in @buf or %-1 if
72*4882a593Smuzhiyun * the buffer contains only 0xff bytes.
73*4882a593Smuzhiyun */
first_non_ff(void * buf,int len)74*4882a593Smuzhiyun static int first_non_ff(void *buf, int len)
75*4882a593Smuzhiyun {
76*4882a593Smuzhiyun uint8_t *p = buf;
77*4882a593Smuzhiyun int i;
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun for (i = 0; i < len; i++)
80*4882a593Smuzhiyun if (*p++ != 0xff)
81*4882a593Smuzhiyun return i;
82*4882a593Smuzhiyun return -1;
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun /**
86*4882a593Smuzhiyun * get_master_node - get the last valid master node allowing for corruption.
87*4882a593Smuzhiyun * @c: UBIFS file-system description object
88*4882a593Smuzhiyun * @lnum: LEB number
89*4882a593Smuzhiyun * @pbuf: buffer containing the LEB read, is returned here
90*4882a593Smuzhiyun * @mst: master node, if found, is returned here
91*4882a593Smuzhiyun * @cor: corruption, if found, is returned here
92*4882a593Smuzhiyun *
93*4882a593Smuzhiyun * This function allocates a buffer, reads the LEB into it, and finds and
94*4882a593Smuzhiyun * returns the last valid master node allowing for one area of corruption.
95*4882a593Smuzhiyun * The corrupt area, if there is one, must be consistent with the assumption
96*4882a593Smuzhiyun * that it is the result of an unclean unmount while the master node was being
97*4882a593Smuzhiyun * written. Under those circumstances, it is valid to use the previously written
98*4882a593Smuzhiyun * master node.
99*4882a593Smuzhiyun *
100*4882a593Smuzhiyun * This function returns %0 on success and a negative error code on failure.
101*4882a593Smuzhiyun */
get_master_node(const struct ubifs_info * c,int lnum,void ** pbuf,struct ubifs_mst_node ** mst,void ** cor)102*4882a593Smuzhiyun static int get_master_node(const struct ubifs_info *c, int lnum, void **pbuf,
103*4882a593Smuzhiyun struct ubifs_mst_node **mst, void **cor)
104*4882a593Smuzhiyun {
105*4882a593Smuzhiyun const int sz = c->mst_node_alsz;
106*4882a593Smuzhiyun int err, offs, len;
107*4882a593Smuzhiyun void *sbuf, *buf;
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun sbuf = vmalloc(c->leb_size);
110*4882a593Smuzhiyun if (!sbuf)
111*4882a593Smuzhiyun return -ENOMEM;
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun err = ubifs_leb_read(c, lnum, sbuf, 0, c->leb_size, 0);
114*4882a593Smuzhiyun if (err && err != -EBADMSG)
115*4882a593Smuzhiyun goto out_free;
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun /* Find the first position that is definitely not a node */
118*4882a593Smuzhiyun offs = 0;
119*4882a593Smuzhiyun buf = sbuf;
120*4882a593Smuzhiyun len = c->leb_size;
121*4882a593Smuzhiyun while (offs + UBIFS_MST_NODE_SZ <= c->leb_size) {
122*4882a593Smuzhiyun struct ubifs_ch *ch = buf;
123*4882a593Smuzhiyun
124*4882a593Smuzhiyun if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC)
125*4882a593Smuzhiyun break;
126*4882a593Smuzhiyun offs += sz;
127*4882a593Smuzhiyun buf += sz;
128*4882a593Smuzhiyun len -= sz;
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun /* See if there was a valid master node before that */
131*4882a593Smuzhiyun if (offs) {
132*4882a593Smuzhiyun int ret;
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun offs -= sz;
135*4882a593Smuzhiyun buf -= sz;
136*4882a593Smuzhiyun len += sz;
137*4882a593Smuzhiyun ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 1);
138*4882a593Smuzhiyun if (ret != SCANNED_A_NODE && offs) {
139*4882a593Smuzhiyun /* Could have been corruption so check one place back */
140*4882a593Smuzhiyun offs -= sz;
141*4882a593Smuzhiyun buf -= sz;
142*4882a593Smuzhiyun len += sz;
143*4882a593Smuzhiyun ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 1);
144*4882a593Smuzhiyun if (ret != SCANNED_A_NODE)
145*4882a593Smuzhiyun /*
146*4882a593Smuzhiyun * We accept only one area of corruption because
147*4882a593Smuzhiyun * we are assuming that it was caused while
148*4882a593Smuzhiyun * trying to write a master node.
149*4882a593Smuzhiyun */
150*4882a593Smuzhiyun goto out_err;
151*4882a593Smuzhiyun }
152*4882a593Smuzhiyun if (ret == SCANNED_A_NODE) {
153*4882a593Smuzhiyun struct ubifs_ch *ch = buf;
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun if (ch->node_type != UBIFS_MST_NODE)
156*4882a593Smuzhiyun goto out_err;
157*4882a593Smuzhiyun dbg_rcvry("found a master node at %d:%d", lnum, offs);
158*4882a593Smuzhiyun *mst = buf;
159*4882a593Smuzhiyun offs += sz;
160*4882a593Smuzhiyun buf += sz;
161*4882a593Smuzhiyun len -= sz;
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun }
164*4882a593Smuzhiyun /* Check for corruption */
165*4882a593Smuzhiyun if (offs < c->leb_size) {
166*4882a593Smuzhiyun if (!is_empty(buf, min_t(int, len, sz))) {
167*4882a593Smuzhiyun *cor = buf;
168*4882a593Smuzhiyun dbg_rcvry("found corruption at %d:%d", lnum, offs);
169*4882a593Smuzhiyun }
170*4882a593Smuzhiyun offs += sz;
171*4882a593Smuzhiyun buf += sz;
172*4882a593Smuzhiyun len -= sz;
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun /* Check remaining empty space */
175*4882a593Smuzhiyun if (offs < c->leb_size)
176*4882a593Smuzhiyun if (!is_empty(buf, len))
177*4882a593Smuzhiyun goto out_err;
178*4882a593Smuzhiyun *pbuf = sbuf;
179*4882a593Smuzhiyun return 0;
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun out_err:
182*4882a593Smuzhiyun err = -EINVAL;
183*4882a593Smuzhiyun out_free:
184*4882a593Smuzhiyun vfree(sbuf);
185*4882a593Smuzhiyun *mst = NULL;
186*4882a593Smuzhiyun *cor = NULL;
187*4882a593Smuzhiyun return err;
188*4882a593Smuzhiyun }
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun /**
191*4882a593Smuzhiyun * write_rcvrd_mst_node - write recovered master node.
192*4882a593Smuzhiyun * @c: UBIFS file-system description object
193*4882a593Smuzhiyun * @mst: master node
194*4882a593Smuzhiyun *
195*4882a593Smuzhiyun * This function returns %0 on success and a negative error code on failure.
196*4882a593Smuzhiyun */
write_rcvrd_mst_node(struct ubifs_info * c,struct ubifs_mst_node * mst)197*4882a593Smuzhiyun static int write_rcvrd_mst_node(struct ubifs_info *c,
198*4882a593Smuzhiyun struct ubifs_mst_node *mst)
199*4882a593Smuzhiyun {
200*4882a593Smuzhiyun int err = 0, lnum = UBIFS_MST_LNUM, sz = c->mst_node_alsz;
201*4882a593Smuzhiyun __le32 save_flags;
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun dbg_rcvry("recovery");
204*4882a593Smuzhiyun
205*4882a593Smuzhiyun save_flags = mst->flags;
206*4882a593Smuzhiyun mst->flags |= cpu_to_le32(UBIFS_MST_RCVRY);
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun ubifs_prepare_node(c, mst, UBIFS_MST_NODE_SZ, 1);
209*4882a593Smuzhiyun err = ubifs_leb_change(c, lnum, mst, sz);
210*4882a593Smuzhiyun if (err)
211*4882a593Smuzhiyun goto out;
212*4882a593Smuzhiyun err = ubifs_leb_change(c, lnum + 1, mst, sz);
213*4882a593Smuzhiyun if (err)
214*4882a593Smuzhiyun goto out;
215*4882a593Smuzhiyun out:
216*4882a593Smuzhiyun mst->flags = save_flags;
217*4882a593Smuzhiyun return err;
218*4882a593Smuzhiyun }
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun /**
221*4882a593Smuzhiyun * ubifs_recover_master_node - recover the master node.
222*4882a593Smuzhiyun * @c: UBIFS file-system description object
223*4882a593Smuzhiyun *
224*4882a593Smuzhiyun * This function recovers the master node from corruption that may occur due to
225*4882a593Smuzhiyun * an unclean unmount.
226*4882a593Smuzhiyun *
227*4882a593Smuzhiyun * This function returns %0 on success and a negative error code on failure.
228*4882a593Smuzhiyun */
ubifs_recover_master_node(struct ubifs_info * c)229*4882a593Smuzhiyun int ubifs_recover_master_node(struct ubifs_info *c)
230*4882a593Smuzhiyun {
231*4882a593Smuzhiyun void *buf1 = NULL, *buf2 = NULL, *cor1 = NULL, *cor2 = NULL;
232*4882a593Smuzhiyun struct ubifs_mst_node *mst1 = NULL, *mst2 = NULL, *mst;
233*4882a593Smuzhiyun const int sz = c->mst_node_alsz;
234*4882a593Smuzhiyun int err, offs1, offs2;
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun dbg_rcvry("recovery");
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun err = get_master_node(c, UBIFS_MST_LNUM, &buf1, &mst1, &cor1);
239*4882a593Smuzhiyun if (err)
240*4882a593Smuzhiyun goto out_free;
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun err = get_master_node(c, UBIFS_MST_LNUM + 1, &buf2, &mst2, &cor2);
243*4882a593Smuzhiyun if (err)
244*4882a593Smuzhiyun goto out_free;
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun if (mst1) {
247*4882a593Smuzhiyun offs1 = (void *)mst1 - buf1;
248*4882a593Smuzhiyun if ((le32_to_cpu(mst1->flags) & UBIFS_MST_RCVRY) &&
249*4882a593Smuzhiyun (offs1 == 0 && !cor1)) {
250*4882a593Smuzhiyun /*
251*4882a593Smuzhiyun * mst1 was written by recovery at offset 0 with no
252*4882a593Smuzhiyun * corruption.
253*4882a593Smuzhiyun */
254*4882a593Smuzhiyun dbg_rcvry("recovery recovery");
255*4882a593Smuzhiyun mst = mst1;
256*4882a593Smuzhiyun } else if (mst2) {
257*4882a593Smuzhiyun offs2 = (void *)mst2 - buf2;
258*4882a593Smuzhiyun if (offs1 == offs2) {
259*4882a593Smuzhiyun /* Same offset, so must be the same */
260*4882a593Smuzhiyun if (memcmp((void *)mst1 + UBIFS_CH_SZ,
261*4882a593Smuzhiyun (void *)mst2 + UBIFS_CH_SZ,
262*4882a593Smuzhiyun UBIFS_MST_NODE_SZ - UBIFS_CH_SZ))
263*4882a593Smuzhiyun goto out_err;
264*4882a593Smuzhiyun mst = mst1;
265*4882a593Smuzhiyun } else if (offs2 + sz == offs1) {
266*4882a593Smuzhiyun /* 1st LEB was written, 2nd was not */
267*4882a593Smuzhiyun if (cor1)
268*4882a593Smuzhiyun goto out_err;
269*4882a593Smuzhiyun mst = mst1;
270*4882a593Smuzhiyun } else if (offs1 == 0 &&
271*4882a593Smuzhiyun c->leb_size - offs2 - sz < sz) {
272*4882a593Smuzhiyun /* 1st LEB was unmapped and written, 2nd not */
273*4882a593Smuzhiyun if (cor1)
274*4882a593Smuzhiyun goto out_err;
275*4882a593Smuzhiyun mst = mst1;
276*4882a593Smuzhiyun } else
277*4882a593Smuzhiyun goto out_err;
278*4882a593Smuzhiyun } else {
279*4882a593Smuzhiyun /*
280*4882a593Smuzhiyun * 2nd LEB was unmapped and about to be written, so
281*4882a593Smuzhiyun * there must be only one master node in the first LEB
282*4882a593Smuzhiyun * and no corruption.
283*4882a593Smuzhiyun */
284*4882a593Smuzhiyun if (offs1 != 0 || cor1)
285*4882a593Smuzhiyun goto out_err;
286*4882a593Smuzhiyun mst = mst1;
287*4882a593Smuzhiyun }
288*4882a593Smuzhiyun } else {
289*4882a593Smuzhiyun if (!mst2)
290*4882a593Smuzhiyun goto out_err;
291*4882a593Smuzhiyun /*
292*4882a593Smuzhiyun * 1st LEB was unmapped and about to be written, so there must
293*4882a593Smuzhiyun * be no room left in 2nd LEB.
294*4882a593Smuzhiyun */
295*4882a593Smuzhiyun offs2 = (void *)mst2 - buf2;
296*4882a593Smuzhiyun if (offs2 + sz + sz <= c->leb_size)
297*4882a593Smuzhiyun goto out_err;
298*4882a593Smuzhiyun mst = mst2;
299*4882a593Smuzhiyun }
300*4882a593Smuzhiyun
301*4882a593Smuzhiyun ubifs_msg(c, "recovered master node from LEB %d",
302*4882a593Smuzhiyun (mst == mst1 ? UBIFS_MST_LNUM : UBIFS_MST_LNUM + 1));
303*4882a593Smuzhiyun
304*4882a593Smuzhiyun memcpy(c->mst_node, mst, UBIFS_MST_NODE_SZ);
305*4882a593Smuzhiyun
306*4882a593Smuzhiyun if (c->ro_mount) {
307*4882a593Smuzhiyun /* Read-only mode. Keep a copy for switching to rw mode */
308*4882a593Smuzhiyun c->rcvrd_mst_node = kmalloc(sz, GFP_KERNEL);
309*4882a593Smuzhiyun if (!c->rcvrd_mst_node) {
310*4882a593Smuzhiyun err = -ENOMEM;
311*4882a593Smuzhiyun goto out_free;
312*4882a593Smuzhiyun }
313*4882a593Smuzhiyun memcpy(c->rcvrd_mst_node, c->mst_node, UBIFS_MST_NODE_SZ);
314*4882a593Smuzhiyun
315*4882a593Smuzhiyun /*
316*4882a593Smuzhiyun * We had to recover the master node, which means there was an
317*4882a593Smuzhiyun * unclean reboot. However, it is possible that the master node
318*4882a593Smuzhiyun * is clean at this point, i.e., %UBIFS_MST_DIRTY is not set.
319*4882a593Smuzhiyun * E.g., consider the following chain of events:
320*4882a593Smuzhiyun *
321*4882a593Smuzhiyun * 1. UBIFS was cleanly unmounted, so the master node is clean
322*4882a593Smuzhiyun * 2. UBIFS is being mounted R/W and starts changing the master
323*4882a593Smuzhiyun * node in the first (%UBIFS_MST_LNUM). A power cut happens,
324*4882a593Smuzhiyun * so this LEB ends up with some amount of garbage at the
325*4882a593Smuzhiyun * end.
326*4882a593Smuzhiyun * 3. UBIFS is being mounted R/O. We reach this place and
327*4882a593Smuzhiyun * recover the master node from the second LEB
328*4882a593Smuzhiyun * (%UBIFS_MST_LNUM + 1). But we cannot update the media
329*4882a593Smuzhiyun * because we are being mounted R/O. We have to defer the
330*4882a593Smuzhiyun * operation.
331*4882a593Smuzhiyun * 4. However, this master node (@c->mst_node) is marked as
332*4882a593Smuzhiyun * clean (since the step 1). And if we just return, the
333*4882a593Smuzhiyun * mount code will be confused and won't recover the master
334*4882a593Smuzhiyun * node when it is re-mounter R/W later.
335*4882a593Smuzhiyun *
336*4882a593Smuzhiyun * Thus, to force the recovery by marking the master node as
337*4882a593Smuzhiyun * dirty.
338*4882a593Smuzhiyun */
339*4882a593Smuzhiyun c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
340*4882a593Smuzhiyun #ifndef __UBOOT__
341*4882a593Smuzhiyun } else {
342*4882a593Smuzhiyun /* Write the recovered master node */
343*4882a593Smuzhiyun c->max_sqnum = le64_to_cpu(mst->ch.sqnum) - 1;
344*4882a593Smuzhiyun err = write_rcvrd_mst_node(c, c->mst_node);
345*4882a593Smuzhiyun if (err)
346*4882a593Smuzhiyun goto out_free;
347*4882a593Smuzhiyun #endif
348*4882a593Smuzhiyun }
349*4882a593Smuzhiyun
350*4882a593Smuzhiyun vfree(buf2);
351*4882a593Smuzhiyun vfree(buf1);
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun return 0;
354*4882a593Smuzhiyun
355*4882a593Smuzhiyun out_err:
356*4882a593Smuzhiyun err = -EINVAL;
357*4882a593Smuzhiyun out_free:
358*4882a593Smuzhiyun ubifs_err(c, "failed to recover master node");
359*4882a593Smuzhiyun if (mst1) {
360*4882a593Smuzhiyun ubifs_err(c, "dumping first master node");
361*4882a593Smuzhiyun ubifs_dump_node(c, mst1);
362*4882a593Smuzhiyun }
363*4882a593Smuzhiyun if (mst2) {
364*4882a593Smuzhiyun ubifs_err(c, "dumping second master node");
365*4882a593Smuzhiyun ubifs_dump_node(c, mst2);
366*4882a593Smuzhiyun }
367*4882a593Smuzhiyun vfree(buf2);
368*4882a593Smuzhiyun vfree(buf1);
369*4882a593Smuzhiyun return err;
370*4882a593Smuzhiyun }
371*4882a593Smuzhiyun
372*4882a593Smuzhiyun /**
373*4882a593Smuzhiyun * ubifs_write_rcvrd_mst_node - write the recovered master node.
374*4882a593Smuzhiyun * @c: UBIFS file-system description object
375*4882a593Smuzhiyun *
376*4882a593Smuzhiyun * This function writes the master node that was recovered during mounting in
377*4882a593Smuzhiyun * read-only mode and must now be written because we are remounting rw.
378*4882a593Smuzhiyun *
379*4882a593Smuzhiyun * This function returns %0 on success and a negative error code on failure.
380*4882a593Smuzhiyun */
ubifs_write_rcvrd_mst_node(struct ubifs_info * c)381*4882a593Smuzhiyun int ubifs_write_rcvrd_mst_node(struct ubifs_info *c)
382*4882a593Smuzhiyun {
383*4882a593Smuzhiyun int err;
384*4882a593Smuzhiyun
385*4882a593Smuzhiyun if (!c->rcvrd_mst_node)
386*4882a593Smuzhiyun return 0;
387*4882a593Smuzhiyun c->rcvrd_mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
388*4882a593Smuzhiyun c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
389*4882a593Smuzhiyun err = write_rcvrd_mst_node(c, c->rcvrd_mst_node);
390*4882a593Smuzhiyun if (err)
391*4882a593Smuzhiyun return err;
392*4882a593Smuzhiyun kfree(c->rcvrd_mst_node);
393*4882a593Smuzhiyun c->rcvrd_mst_node = NULL;
394*4882a593Smuzhiyun return 0;
395*4882a593Smuzhiyun }
396*4882a593Smuzhiyun
397*4882a593Smuzhiyun /**
398*4882a593Smuzhiyun * is_last_write - determine if an offset was in the last write to a LEB.
399*4882a593Smuzhiyun * @c: UBIFS file-system description object
400*4882a593Smuzhiyun * @buf: buffer to check
401*4882a593Smuzhiyun * @offs: offset to check
402*4882a593Smuzhiyun *
403*4882a593Smuzhiyun * This function returns %1 if @offs was in the last write to the LEB whose data
404*4882a593Smuzhiyun * is in @buf, otherwise %0 is returned. The determination is made by checking
405*4882a593Smuzhiyun * for subsequent empty space starting from the next @c->max_write_size
406*4882a593Smuzhiyun * boundary.
407*4882a593Smuzhiyun */
is_last_write(const struct ubifs_info * c,void * buf,int offs)408*4882a593Smuzhiyun static int is_last_write(const struct ubifs_info *c, void *buf, int offs)
409*4882a593Smuzhiyun {
410*4882a593Smuzhiyun int empty_offs, check_len;
411*4882a593Smuzhiyun uint8_t *p;
412*4882a593Smuzhiyun
413*4882a593Smuzhiyun /*
414*4882a593Smuzhiyun * Round up to the next @c->max_write_size boundary i.e. @offs is in
415*4882a593Smuzhiyun * the last wbuf written. After that should be empty space.
416*4882a593Smuzhiyun */
417*4882a593Smuzhiyun empty_offs = ALIGN(offs + 1, c->max_write_size);
418*4882a593Smuzhiyun check_len = c->leb_size - empty_offs;
419*4882a593Smuzhiyun p = buf + empty_offs - offs;
420*4882a593Smuzhiyun return is_empty(p, check_len);
421*4882a593Smuzhiyun }
422*4882a593Smuzhiyun
423*4882a593Smuzhiyun /**
424*4882a593Smuzhiyun * clean_buf - clean the data from an LEB sitting in a buffer.
425*4882a593Smuzhiyun * @c: UBIFS file-system description object
426*4882a593Smuzhiyun * @buf: buffer to clean
427*4882a593Smuzhiyun * @lnum: LEB number to clean
428*4882a593Smuzhiyun * @offs: offset from which to clean
429*4882a593Smuzhiyun * @len: length of buffer
430*4882a593Smuzhiyun *
431*4882a593Smuzhiyun * This function pads up to the next min_io_size boundary (if there is one) and
432*4882a593Smuzhiyun * sets empty space to all 0xff. @buf, @offs and @len are updated to the next
433*4882a593Smuzhiyun * @c->min_io_size boundary.
434*4882a593Smuzhiyun */
clean_buf(const struct ubifs_info * c,void ** buf,int lnum,int * offs,int * len)435*4882a593Smuzhiyun static void clean_buf(const struct ubifs_info *c, void **buf, int lnum,
436*4882a593Smuzhiyun int *offs, int *len)
437*4882a593Smuzhiyun {
438*4882a593Smuzhiyun int empty_offs, pad_len;
439*4882a593Smuzhiyun
440*4882a593Smuzhiyun lnum = lnum;
441*4882a593Smuzhiyun dbg_rcvry("cleaning corruption at %d:%d", lnum, *offs);
442*4882a593Smuzhiyun
443*4882a593Smuzhiyun ubifs_assert(!(*offs & 7));
444*4882a593Smuzhiyun empty_offs = ALIGN(*offs, c->min_io_size);
445*4882a593Smuzhiyun pad_len = empty_offs - *offs;
446*4882a593Smuzhiyun ubifs_pad(c, *buf, pad_len);
447*4882a593Smuzhiyun *offs += pad_len;
448*4882a593Smuzhiyun *buf += pad_len;
449*4882a593Smuzhiyun *len -= pad_len;
450*4882a593Smuzhiyun memset(*buf, 0xff, c->leb_size - empty_offs);
451*4882a593Smuzhiyun }
452*4882a593Smuzhiyun
453*4882a593Smuzhiyun /**
454*4882a593Smuzhiyun * no_more_nodes - determine if there are no more nodes in a buffer.
455*4882a593Smuzhiyun * @c: UBIFS file-system description object
456*4882a593Smuzhiyun * @buf: buffer to check
457*4882a593Smuzhiyun * @len: length of buffer
458*4882a593Smuzhiyun * @lnum: LEB number of the LEB from which @buf was read
459*4882a593Smuzhiyun * @offs: offset from which @buf was read
460*4882a593Smuzhiyun *
461*4882a593Smuzhiyun * This function ensures that the corrupted node at @offs is the last thing
462*4882a593Smuzhiyun * written to a LEB. This function returns %1 if more data is not found and
463*4882a593Smuzhiyun * %0 if more data is found.
464*4882a593Smuzhiyun */
no_more_nodes(const struct ubifs_info * c,void * buf,int len,int lnum,int offs)465*4882a593Smuzhiyun static int no_more_nodes(const struct ubifs_info *c, void *buf, int len,
466*4882a593Smuzhiyun int lnum, int offs)
467*4882a593Smuzhiyun {
468*4882a593Smuzhiyun struct ubifs_ch *ch = buf;
469*4882a593Smuzhiyun int skip, dlen = le32_to_cpu(ch->len);
470*4882a593Smuzhiyun
471*4882a593Smuzhiyun /* Check for empty space after the corrupt node's common header */
472*4882a593Smuzhiyun skip = ALIGN(offs + UBIFS_CH_SZ, c->max_write_size) - offs;
473*4882a593Smuzhiyun if (is_empty(buf + skip, len - skip))
474*4882a593Smuzhiyun return 1;
475*4882a593Smuzhiyun /*
476*4882a593Smuzhiyun * The area after the common header size is not empty, so the common
477*4882a593Smuzhiyun * header must be intact. Check it.
478*4882a593Smuzhiyun */
479*4882a593Smuzhiyun if (ubifs_check_node(c, buf, lnum, offs, 1, 0) != -EUCLEAN) {
480*4882a593Smuzhiyun dbg_rcvry("unexpected bad common header at %d:%d", lnum, offs);
481*4882a593Smuzhiyun return 0;
482*4882a593Smuzhiyun }
483*4882a593Smuzhiyun /* Now we know the corrupt node's length we can skip over it */
484*4882a593Smuzhiyun skip = ALIGN(offs + dlen, c->max_write_size) - offs;
485*4882a593Smuzhiyun /* After which there should be empty space */
486*4882a593Smuzhiyun if (is_empty(buf + skip, len - skip))
487*4882a593Smuzhiyun return 1;
488*4882a593Smuzhiyun dbg_rcvry("unexpected data at %d:%d", lnum, offs + skip);
489*4882a593Smuzhiyun return 0;
490*4882a593Smuzhiyun }
491*4882a593Smuzhiyun
492*4882a593Smuzhiyun /**
493*4882a593Smuzhiyun * fix_unclean_leb - fix an unclean LEB.
494*4882a593Smuzhiyun * @c: UBIFS file-system description object
495*4882a593Smuzhiyun * @sleb: scanned LEB information
496*4882a593Smuzhiyun * @start: offset where scan started
497*4882a593Smuzhiyun */
fix_unclean_leb(struct ubifs_info * c,struct ubifs_scan_leb * sleb,int start)498*4882a593Smuzhiyun static int fix_unclean_leb(struct ubifs_info *c, struct ubifs_scan_leb *sleb,
499*4882a593Smuzhiyun int start)
500*4882a593Smuzhiyun {
501*4882a593Smuzhiyun int lnum = sleb->lnum, endpt = start;
502*4882a593Smuzhiyun
503*4882a593Smuzhiyun /* Get the end offset of the last node we are keeping */
504*4882a593Smuzhiyun if (!list_empty(&sleb->nodes)) {
505*4882a593Smuzhiyun struct ubifs_scan_node *snod;
506*4882a593Smuzhiyun
507*4882a593Smuzhiyun snod = list_entry(sleb->nodes.prev,
508*4882a593Smuzhiyun struct ubifs_scan_node, list);
509*4882a593Smuzhiyun endpt = snod->offs + snod->len;
510*4882a593Smuzhiyun }
511*4882a593Smuzhiyun
512*4882a593Smuzhiyun if (c->ro_mount && !c->remounting_rw) {
513*4882a593Smuzhiyun /* Add to recovery list */
514*4882a593Smuzhiyun struct ubifs_unclean_leb *ucleb;
515*4882a593Smuzhiyun
516*4882a593Smuzhiyun dbg_rcvry("need to fix LEB %d start %d endpt %d",
517*4882a593Smuzhiyun lnum, start, sleb->endpt);
518*4882a593Smuzhiyun ucleb = kzalloc(sizeof(struct ubifs_unclean_leb), GFP_NOFS);
519*4882a593Smuzhiyun if (!ucleb)
520*4882a593Smuzhiyun return -ENOMEM;
521*4882a593Smuzhiyun ucleb->lnum = lnum;
522*4882a593Smuzhiyun ucleb->endpt = endpt;
523*4882a593Smuzhiyun list_add_tail(&ucleb->list, &c->unclean_leb_list);
524*4882a593Smuzhiyun #ifndef __UBOOT__
525*4882a593Smuzhiyun } else {
526*4882a593Smuzhiyun /* Write the fixed LEB back to flash */
527*4882a593Smuzhiyun int err;
528*4882a593Smuzhiyun
529*4882a593Smuzhiyun dbg_rcvry("fixing LEB %d start %d endpt %d",
530*4882a593Smuzhiyun lnum, start, sleb->endpt);
531*4882a593Smuzhiyun if (endpt == 0) {
532*4882a593Smuzhiyun err = ubifs_leb_unmap(c, lnum);
533*4882a593Smuzhiyun if (err)
534*4882a593Smuzhiyun return err;
535*4882a593Smuzhiyun } else {
536*4882a593Smuzhiyun int len = ALIGN(endpt, c->min_io_size);
537*4882a593Smuzhiyun
538*4882a593Smuzhiyun if (start) {
539*4882a593Smuzhiyun err = ubifs_leb_read(c, lnum, sleb->buf, 0,
540*4882a593Smuzhiyun start, 1);
541*4882a593Smuzhiyun if (err)
542*4882a593Smuzhiyun return err;
543*4882a593Smuzhiyun }
544*4882a593Smuzhiyun /* Pad to min_io_size */
545*4882a593Smuzhiyun if (len > endpt) {
546*4882a593Smuzhiyun int pad_len = len - ALIGN(endpt, 8);
547*4882a593Smuzhiyun
548*4882a593Smuzhiyun if (pad_len > 0) {
549*4882a593Smuzhiyun void *buf = sleb->buf + len - pad_len;
550*4882a593Smuzhiyun
551*4882a593Smuzhiyun ubifs_pad(c, buf, pad_len);
552*4882a593Smuzhiyun }
553*4882a593Smuzhiyun }
554*4882a593Smuzhiyun err = ubifs_leb_change(c, lnum, sleb->buf, len);
555*4882a593Smuzhiyun if (err)
556*4882a593Smuzhiyun return err;
557*4882a593Smuzhiyun }
558*4882a593Smuzhiyun #endif
559*4882a593Smuzhiyun }
560*4882a593Smuzhiyun return 0;
561*4882a593Smuzhiyun }
562*4882a593Smuzhiyun
563*4882a593Smuzhiyun /**
564*4882a593Smuzhiyun * drop_last_group - drop the last group of nodes.
565*4882a593Smuzhiyun * @sleb: scanned LEB information
566*4882a593Smuzhiyun * @offs: offset of dropped nodes is returned here
567*4882a593Smuzhiyun *
568*4882a593Smuzhiyun * This is a helper function for 'ubifs_recover_leb()' which drops the last
569*4882a593Smuzhiyun * group of nodes of the scanned LEB.
570*4882a593Smuzhiyun */
drop_last_group(struct ubifs_scan_leb * sleb,int * offs)571*4882a593Smuzhiyun static void drop_last_group(struct ubifs_scan_leb *sleb, int *offs)
572*4882a593Smuzhiyun {
573*4882a593Smuzhiyun while (!list_empty(&sleb->nodes)) {
574*4882a593Smuzhiyun struct ubifs_scan_node *snod;
575*4882a593Smuzhiyun struct ubifs_ch *ch;
576*4882a593Smuzhiyun
577*4882a593Smuzhiyun snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node,
578*4882a593Smuzhiyun list);
579*4882a593Smuzhiyun ch = snod->node;
580*4882a593Smuzhiyun if (ch->group_type != UBIFS_IN_NODE_GROUP)
581*4882a593Smuzhiyun break;
582*4882a593Smuzhiyun
583*4882a593Smuzhiyun dbg_rcvry("dropping grouped node at %d:%d",
584*4882a593Smuzhiyun sleb->lnum, snod->offs);
585*4882a593Smuzhiyun *offs = snod->offs;
586*4882a593Smuzhiyun list_del(&snod->list);
587*4882a593Smuzhiyun kfree(snod);
588*4882a593Smuzhiyun sleb->nodes_cnt -= 1;
589*4882a593Smuzhiyun }
590*4882a593Smuzhiyun }
591*4882a593Smuzhiyun
592*4882a593Smuzhiyun /**
593*4882a593Smuzhiyun * drop_last_node - drop the last node.
594*4882a593Smuzhiyun * @sleb: scanned LEB information
595*4882a593Smuzhiyun * @offs: offset of dropped nodes is returned here
596*4882a593Smuzhiyun *
597*4882a593Smuzhiyun * This is a helper function for 'ubifs_recover_leb()' which drops the last
598*4882a593Smuzhiyun * node of the scanned LEB.
599*4882a593Smuzhiyun */
drop_last_node(struct ubifs_scan_leb * sleb,int * offs)600*4882a593Smuzhiyun static void drop_last_node(struct ubifs_scan_leb *sleb, int *offs)
601*4882a593Smuzhiyun {
602*4882a593Smuzhiyun struct ubifs_scan_node *snod;
603*4882a593Smuzhiyun
604*4882a593Smuzhiyun if (!list_empty(&sleb->nodes)) {
605*4882a593Smuzhiyun snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node,
606*4882a593Smuzhiyun list);
607*4882a593Smuzhiyun
608*4882a593Smuzhiyun dbg_rcvry("dropping last node at %d:%d",
609*4882a593Smuzhiyun sleb->lnum, snod->offs);
610*4882a593Smuzhiyun *offs = snod->offs;
611*4882a593Smuzhiyun list_del(&snod->list);
612*4882a593Smuzhiyun kfree(snod);
613*4882a593Smuzhiyun sleb->nodes_cnt -= 1;
614*4882a593Smuzhiyun }
615*4882a593Smuzhiyun }
616*4882a593Smuzhiyun
617*4882a593Smuzhiyun /**
618*4882a593Smuzhiyun * ubifs_recover_leb - scan and recover a LEB.
619*4882a593Smuzhiyun * @c: UBIFS file-system description object
620*4882a593Smuzhiyun * @lnum: LEB number
621*4882a593Smuzhiyun * @offs: offset
622*4882a593Smuzhiyun * @sbuf: LEB-sized buffer to use
623*4882a593Smuzhiyun * @jhead: journal head number this LEB belongs to (%-1 if the LEB does not
624*4882a593Smuzhiyun * belong to any journal head)
625*4882a593Smuzhiyun *
626*4882a593Smuzhiyun * This function does a scan of a LEB, but caters for errors that might have
627*4882a593Smuzhiyun * been caused by the unclean unmount from which we are attempting to recover.
628*4882a593Smuzhiyun * Returns the scanned information on success and a negative error code on
629*4882a593Smuzhiyun * failure.
630*4882a593Smuzhiyun */
ubifs_recover_leb(struct ubifs_info * c,int lnum,int offs,void * sbuf,int jhead)631*4882a593Smuzhiyun struct ubifs_scan_leb *ubifs_recover_leb(struct ubifs_info *c, int lnum,
632*4882a593Smuzhiyun int offs, void *sbuf, int jhead)
633*4882a593Smuzhiyun {
634*4882a593Smuzhiyun int ret = 0, err, len = c->leb_size - offs, start = offs, min_io_unit;
635*4882a593Smuzhiyun int grouped = jhead == -1 ? 0 : c->jheads[jhead].grouped;
636*4882a593Smuzhiyun struct ubifs_scan_leb *sleb;
637*4882a593Smuzhiyun void *buf = sbuf + offs;
638*4882a593Smuzhiyun
639*4882a593Smuzhiyun dbg_rcvry("%d:%d, jhead %d, grouped %d", lnum, offs, jhead, grouped);
640*4882a593Smuzhiyun
641*4882a593Smuzhiyun sleb = ubifs_start_scan(c, lnum, offs, sbuf);
642*4882a593Smuzhiyun if (IS_ERR(sleb))
643*4882a593Smuzhiyun return sleb;
644*4882a593Smuzhiyun
645*4882a593Smuzhiyun ubifs_assert(len >= 8);
646*4882a593Smuzhiyun while (len >= 8) {
647*4882a593Smuzhiyun dbg_scan("look at LEB %d:%d (%d bytes left)",
648*4882a593Smuzhiyun lnum, offs, len);
649*4882a593Smuzhiyun
650*4882a593Smuzhiyun cond_resched();
651*4882a593Smuzhiyun
652*4882a593Smuzhiyun /*
653*4882a593Smuzhiyun * Scan quietly until there is an error from which we cannot
654*4882a593Smuzhiyun * recover
655*4882a593Smuzhiyun */
656*4882a593Smuzhiyun ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 1);
657*4882a593Smuzhiyun if (ret == SCANNED_A_NODE) {
658*4882a593Smuzhiyun /* A valid node, and not a padding node */
659*4882a593Smuzhiyun struct ubifs_ch *ch = buf;
660*4882a593Smuzhiyun int node_len;
661*4882a593Smuzhiyun
662*4882a593Smuzhiyun err = ubifs_add_snod(c, sleb, buf, offs);
663*4882a593Smuzhiyun if (err)
664*4882a593Smuzhiyun goto error;
665*4882a593Smuzhiyun node_len = ALIGN(le32_to_cpu(ch->len), 8);
666*4882a593Smuzhiyun offs += node_len;
667*4882a593Smuzhiyun buf += node_len;
668*4882a593Smuzhiyun len -= node_len;
669*4882a593Smuzhiyun } else if (ret > 0) {
670*4882a593Smuzhiyun /* Padding bytes or a valid padding node */
671*4882a593Smuzhiyun offs += ret;
672*4882a593Smuzhiyun buf += ret;
673*4882a593Smuzhiyun len -= ret;
674*4882a593Smuzhiyun } else if (ret == SCANNED_EMPTY_SPACE ||
675*4882a593Smuzhiyun ret == SCANNED_GARBAGE ||
676*4882a593Smuzhiyun ret == SCANNED_A_BAD_PAD_NODE ||
677*4882a593Smuzhiyun ret == SCANNED_A_CORRUPT_NODE) {
678*4882a593Smuzhiyun dbg_rcvry("found corruption (%d) at %d:%d",
679*4882a593Smuzhiyun ret, lnum, offs);
680*4882a593Smuzhiyun break;
681*4882a593Smuzhiyun } else {
682*4882a593Smuzhiyun ubifs_err(c, "unexpected return value %d", ret);
683*4882a593Smuzhiyun err = -EINVAL;
684*4882a593Smuzhiyun goto error;
685*4882a593Smuzhiyun }
686*4882a593Smuzhiyun }
687*4882a593Smuzhiyun
688*4882a593Smuzhiyun if (ret == SCANNED_GARBAGE || ret == SCANNED_A_BAD_PAD_NODE) {
689*4882a593Smuzhiyun if (!is_last_write(c, buf, offs))
690*4882a593Smuzhiyun goto corrupted_rescan;
691*4882a593Smuzhiyun } else if (ret == SCANNED_A_CORRUPT_NODE) {
692*4882a593Smuzhiyun if (!no_more_nodes(c, buf, len, lnum, offs))
693*4882a593Smuzhiyun goto corrupted_rescan;
694*4882a593Smuzhiyun } else if (!is_empty(buf, len)) {
695*4882a593Smuzhiyun if (!is_last_write(c, buf, offs)) {
696*4882a593Smuzhiyun int corruption = first_non_ff(buf, len);
697*4882a593Smuzhiyun
698*4882a593Smuzhiyun /*
699*4882a593Smuzhiyun * See header comment for this file for more
700*4882a593Smuzhiyun * explanations about the reasons we have this check.
701*4882a593Smuzhiyun */
702*4882a593Smuzhiyun ubifs_err(c, "corrupt empty space LEB %d:%d, corruption starts at %d",
703*4882a593Smuzhiyun lnum, offs, corruption);
704*4882a593Smuzhiyun /* Make sure we dump interesting non-0xFF data */
705*4882a593Smuzhiyun offs += corruption;
706*4882a593Smuzhiyun buf += corruption;
707*4882a593Smuzhiyun goto corrupted;
708*4882a593Smuzhiyun }
709*4882a593Smuzhiyun }
710*4882a593Smuzhiyun
711*4882a593Smuzhiyun min_io_unit = round_down(offs, c->min_io_size);
712*4882a593Smuzhiyun if (grouped)
713*4882a593Smuzhiyun /*
714*4882a593Smuzhiyun * If nodes are grouped, always drop the incomplete group at
715*4882a593Smuzhiyun * the end.
716*4882a593Smuzhiyun */
717*4882a593Smuzhiyun drop_last_group(sleb, &offs);
718*4882a593Smuzhiyun
719*4882a593Smuzhiyun if (jhead == GCHD) {
720*4882a593Smuzhiyun /*
721*4882a593Smuzhiyun * If this LEB belongs to the GC head then while we are in the
722*4882a593Smuzhiyun * middle of the same min. I/O unit keep dropping nodes. So
723*4882a593Smuzhiyun * basically, what we want is to make sure that the last min.
724*4882a593Smuzhiyun * I/O unit where we saw the corruption is dropped completely
725*4882a593Smuzhiyun * with all the uncorrupted nodes which may possibly sit there.
726*4882a593Smuzhiyun *
727*4882a593Smuzhiyun * In other words, let's name the min. I/O unit where the
728*4882a593Smuzhiyun * corruption starts B, and the previous min. I/O unit A. The
729*4882a593Smuzhiyun * below code tries to deal with a situation when half of B
730*4882a593Smuzhiyun * contains valid nodes or the end of a valid node, and the
731*4882a593Smuzhiyun * second half of B contains corrupted data or garbage. This
732*4882a593Smuzhiyun * means that UBIFS had been writing to B just before the power
733*4882a593Smuzhiyun * cut happened. I do not know how realistic is this scenario
734*4882a593Smuzhiyun * that half of the min. I/O unit had been written successfully
735*4882a593Smuzhiyun * and the other half not, but this is possible in our 'failure
736*4882a593Smuzhiyun * mode emulation' infrastructure at least.
737*4882a593Smuzhiyun *
738*4882a593Smuzhiyun * So what is the problem, why we need to drop those nodes? Why
739*4882a593Smuzhiyun * can't we just clean-up the second half of B by putting a
740*4882a593Smuzhiyun * padding node there? We can, and this works fine with one
741*4882a593Smuzhiyun * exception which was reproduced with power cut emulation
742*4882a593Smuzhiyun * testing and happens extremely rarely.
743*4882a593Smuzhiyun *
744*4882a593Smuzhiyun * Imagine the file-system is full, we run GC which starts
745*4882a593Smuzhiyun * moving valid nodes from LEB X to LEB Y (obviously, LEB Y is
746*4882a593Smuzhiyun * the current GC head LEB). The @c->gc_lnum is -1, which means
747*4882a593Smuzhiyun * that GC will retain LEB X and will try to continue. Imagine
748*4882a593Smuzhiyun * that LEB X is currently the dirtiest LEB, and the amount of
749*4882a593Smuzhiyun * used space in LEB Y is exactly the same as amount of free
750*4882a593Smuzhiyun * space in LEB X.
751*4882a593Smuzhiyun *
752*4882a593Smuzhiyun * And a power cut happens when nodes are moved from LEB X to
753*4882a593Smuzhiyun * LEB Y. We are here trying to recover LEB Y which is the GC
754*4882a593Smuzhiyun * head LEB. We find the min. I/O unit B as described above.
755*4882a593Smuzhiyun * Then we clean-up LEB Y by padding min. I/O unit. And later
756*4882a593Smuzhiyun * 'ubifs_rcvry_gc_commit()' function fails, because it cannot
757*4882a593Smuzhiyun * find a dirty LEB which could be GC'd into LEB Y! Even LEB X
758*4882a593Smuzhiyun * does not match because the amount of valid nodes there does
759*4882a593Smuzhiyun * not fit the free space in LEB Y any more! And this is
760*4882a593Smuzhiyun * because of the padding node which we added to LEB Y. The
761*4882a593Smuzhiyun * user-visible effect of this which I once observed and
762*4882a593Smuzhiyun * analysed is that we cannot mount the file-system with
763*4882a593Smuzhiyun * -ENOSPC error.
764*4882a593Smuzhiyun *
765*4882a593Smuzhiyun * So obviously, to make sure that situation does not happen we
766*4882a593Smuzhiyun * should free min. I/O unit B in LEB Y completely and the last
767*4882a593Smuzhiyun * used min. I/O unit in LEB Y should be A. This is basically
768*4882a593Smuzhiyun * what the below code tries to do.
769*4882a593Smuzhiyun */
770*4882a593Smuzhiyun while (offs > min_io_unit)
771*4882a593Smuzhiyun drop_last_node(sleb, &offs);
772*4882a593Smuzhiyun }
773*4882a593Smuzhiyun
774*4882a593Smuzhiyun buf = sbuf + offs;
775*4882a593Smuzhiyun len = c->leb_size - offs;
776*4882a593Smuzhiyun
777*4882a593Smuzhiyun clean_buf(c, &buf, lnum, &offs, &len);
778*4882a593Smuzhiyun ubifs_end_scan(c, sleb, lnum, offs);
779*4882a593Smuzhiyun
780*4882a593Smuzhiyun err = fix_unclean_leb(c, sleb, start);
781*4882a593Smuzhiyun if (err)
782*4882a593Smuzhiyun goto error;
783*4882a593Smuzhiyun
784*4882a593Smuzhiyun return sleb;
785*4882a593Smuzhiyun
786*4882a593Smuzhiyun corrupted_rescan:
787*4882a593Smuzhiyun /* Re-scan the corrupted data with verbose messages */
788*4882a593Smuzhiyun ubifs_err(c, "corruption %d", ret);
789*4882a593Smuzhiyun ubifs_scan_a_node(c, buf, len, lnum, offs, 1);
790*4882a593Smuzhiyun corrupted:
791*4882a593Smuzhiyun ubifs_scanned_corruption(c, lnum, offs, buf);
792*4882a593Smuzhiyun err = -EUCLEAN;
793*4882a593Smuzhiyun error:
794*4882a593Smuzhiyun ubifs_err(c, "LEB %d scanning failed", lnum);
795*4882a593Smuzhiyun ubifs_scan_destroy(sleb);
796*4882a593Smuzhiyun return ERR_PTR(err);
797*4882a593Smuzhiyun }
798*4882a593Smuzhiyun
799*4882a593Smuzhiyun /**
800*4882a593Smuzhiyun * get_cs_sqnum - get commit start sequence number.
801*4882a593Smuzhiyun * @c: UBIFS file-system description object
802*4882a593Smuzhiyun * @lnum: LEB number of commit start node
803*4882a593Smuzhiyun * @offs: offset of commit start node
804*4882a593Smuzhiyun * @cs_sqnum: commit start sequence number is returned here
805*4882a593Smuzhiyun *
806*4882a593Smuzhiyun * This function returns %0 on success and a negative error code on failure.
807*4882a593Smuzhiyun */
get_cs_sqnum(struct ubifs_info * c,int lnum,int offs,unsigned long long * cs_sqnum)808*4882a593Smuzhiyun static int get_cs_sqnum(struct ubifs_info *c, int lnum, int offs,
809*4882a593Smuzhiyun unsigned long long *cs_sqnum)
810*4882a593Smuzhiyun {
811*4882a593Smuzhiyun struct ubifs_cs_node *cs_node = NULL;
812*4882a593Smuzhiyun int err, ret;
813*4882a593Smuzhiyun
814*4882a593Smuzhiyun dbg_rcvry("at %d:%d", lnum, offs);
815*4882a593Smuzhiyun cs_node = kmalloc(UBIFS_CS_NODE_SZ, GFP_KERNEL);
816*4882a593Smuzhiyun if (!cs_node)
817*4882a593Smuzhiyun return -ENOMEM;
818*4882a593Smuzhiyun if (c->leb_size - offs < UBIFS_CS_NODE_SZ)
819*4882a593Smuzhiyun goto out_err;
820*4882a593Smuzhiyun err = ubifs_leb_read(c, lnum, (void *)cs_node, offs,
821*4882a593Smuzhiyun UBIFS_CS_NODE_SZ, 0);
822*4882a593Smuzhiyun if (err && err != -EBADMSG)
823*4882a593Smuzhiyun goto out_free;
824*4882a593Smuzhiyun ret = ubifs_scan_a_node(c, cs_node, UBIFS_CS_NODE_SZ, lnum, offs, 0);
825*4882a593Smuzhiyun if (ret != SCANNED_A_NODE) {
826*4882a593Smuzhiyun ubifs_err(c, "Not a valid node");
827*4882a593Smuzhiyun goto out_err;
828*4882a593Smuzhiyun }
829*4882a593Smuzhiyun if (cs_node->ch.node_type != UBIFS_CS_NODE) {
830*4882a593Smuzhiyun ubifs_err(c, "Node a CS node, type is %d", cs_node->ch.node_type);
831*4882a593Smuzhiyun goto out_err;
832*4882a593Smuzhiyun }
833*4882a593Smuzhiyun if (le64_to_cpu(cs_node->cmt_no) != c->cmt_no) {
834*4882a593Smuzhiyun ubifs_err(c, "CS node cmt_no %llu != current cmt_no %llu",
835*4882a593Smuzhiyun (unsigned long long)le64_to_cpu(cs_node->cmt_no),
836*4882a593Smuzhiyun c->cmt_no);
837*4882a593Smuzhiyun goto out_err;
838*4882a593Smuzhiyun }
839*4882a593Smuzhiyun *cs_sqnum = le64_to_cpu(cs_node->ch.sqnum);
840*4882a593Smuzhiyun dbg_rcvry("commit start sqnum %llu", *cs_sqnum);
841*4882a593Smuzhiyun kfree(cs_node);
842*4882a593Smuzhiyun return 0;
843*4882a593Smuzhiyun
844*4882a593Smuzhiyun out_err:
845*4882a593Smuzhiyun err = -EINVAL;
846*4882a593Smuzhiyun out_free:
847*4882a593Smuzhiyun ubifs_err(c, "failed to get CS sqnum");
848*4882a593Smuzhiyun kfree(cs_node);
849*4882a593Smuzhiyun return err;
850*4882a593Smuzhiyun }
851*4882a593Smuzhiyun
852*4882a593Smuzhiyun /**
853*4882a593Smuzhiyun * ubifs_recover_log_leb - scan and recover a log LEB.
854*4882a593Smuzhiyun * @c: UBIFS file-system description object
855*4882a593Smuzhiyun * @lnum: LEB number
856*4882a593Smuzhiyun * @offs: offset
857*4882a593Smuzhiyun * @sbuf: LEB-sized buffer to use
858*4882a593Smuzhiyun *
859*4882a593Smuzhiyun * This function does a scan of a LEB, but caters for errors that might have
860*4882a593Smuzhiyun * been caused by unclean reboots from which we are attempting to recover
861*4882a593Smuzhiyun * (assume that only the last log LEB can be corrupted by an unclean reboot).
862*4882a593Smuzhiyun *
863*4882a593Smuzhiyun * This function returns %0 on success and a negative error code on failure.
864*4882a593Smuzhiyun */
ubifs_recover_log_leb(struct ubifs_info * c,int lnum,int offs,void * sbuf)865*4882a593Smuzhiyun struct ubifs_scan_leb *ubifs_recover_log_leb(struct ubifs_info *c, int lnum,
866*4882a593Smuzhiyun int offs, void *sbuf)
867*4882a593Smuzhiyun {
868*4882a593Smuzhiyun struct ubifs_scan_leb *sleb;
869*4882a593Smuzhiyun int next_lnum;
870*4882a593Smuzhiyun
871*4882a593Smuzhiyun dbg_rcvry("LEB %d", lnum);
872*4882a593Smuzhiyun next_lnum = lnum + 1;
873*4882a593Smuzhiyun if (next_lnum >= UBIFS_LOG_LNUM + c->log_lebs)
874*4882a593Smuzhiyun next_lnum = UBIFS_LOG_LNUM;
875*4882a593Smuzhiyun if (next_lnum != c->ltail_lnum) {
876*4882a593Smuzhiyun /*
877*4882a593Smuzhiyun * We can only recover at the end of the log, so check that the
878*4882a593Smuzhiyun * next log LEB is empty or out of date.
879*4882a593Smuzhiyun */
880*4882a593Smuzhiyun sleb = ubifs_scan(c, next_lnum, 0, sbuf, 0);
881*4882a593Smuzhiyun if (IS_ERR(sleb))
882*4882a593Smuzhiyun return sleb;
883*4882a593Smuzhiyun if (sleb->nodes_cnt) {
884*4882a593Smuzhiyun struct ubifs_scan_node *snod;
885*4882a593Smuzhiyun unsigned long long cs_sqnum = c->cs_sqnum;
886*4882a593Smuzhiyun
887*4882a593Smuzhiyun snod = list_entry(sleb->nodes.next,
888*4882a593Smuzhiyun struct ubifs_scan_node, list);
889*4882a593Smuzhiyun if (cs_sqnum == 0) {
890*4882a593Smuzhiyun int err;
891*4882a593Smuzhiyun
892*4882a593Smuzhiyun err = get_cs_sqnum(c, lnum, offs, &cs_sqnum);
893*4882a593Smuzhiyun if (err) {
894*4882a593Smuzhiyun ubifs_scan_destroy(sleb);
895*4882a593Smuzhiyun return ERR_PTR(err);
896*4882a593Smuzhiyun }
897*4882a593Smuzhiyun }
898*4882a593Smuzhiyun if (snod->sqnum > cs_sqnum) {
899*4882a593Smuzhiyun ubifs_err(c, "unrecoverable log corruption in LEB %d",
900*4882a593Smuzhiyun lnum);
901*4882a593Smuzhiyun ubifs_scan_destroy(sleb);
902*4882a593Smuzhiyun return ERR_PTR(-EUCLEAN);
903*4882a593Smuzhiyun }
904*4882a593Smuzhiyun }
905*4882a593Smuzhiyun ubifs_scan_destroy(sleb);
906*4882a593Smuzhiyun }
907*4882a593Smuzhiyun return ubifs_recover_leb(c, lnum, offs, sbuf, -1);
908*4882a593Smuzhiyun }
909*4882a593Smuzhiyun
910*4882a593Smuzhiyun /**
911*4882a593Smuzhiyun * recover_head - recover a head.
912*4882a593Smuzhiyun * @c: UBIFS file-system description object
913*4882a593Smuzhiyun * @lnum: LEB number of head to recover
914*4882a593Smuzhiyun * @offs: offset of head to recover
915*4882a593Smuzhiyun * @sbuf: LEB-sized buffer to use
916*4882a593Smuzhiyun *
917*4882a593Smuzhiyun * This function ensures that there is no data on the flash at a head location.
918*4882a593Smuzhiyun *
919*4882a593Smuzhiyun * This function returns %0 on success and a negative error code on failure.
920*4882a593Smuzhiyun */
recover_head(struct ubifs_info * c,int lnum,int offs,void * sbuf)921*4882a593Smuzhiyun static int recover_head(struct ubifs_info *c, int lnum, int offs, void *sbuf)
922*4882a593Smuzhiyun {
923*4882a593Smuzhiyun int len = c->max_write_size, err;
924*4882a593Smuzhiyun
925*4882a593Smuzhiyun if (offs + len > c->leb_size)
926*4882a593Smuzhiyun len = c->leb_size - offs;
927*4882a593Smuzhiyun
928*4882a593Smuzhiyun if (!len)
929*4882a593Smuzhiyun return 0;
930*4882a593Smuzhiyun
931*4882a593Smuzhiyun /* Read at the head location and check it is empty flash */
932*4882a593Smuzhiyun err = ubifs_leb_read(c, lnum, sbuf, offs, len, 1);
933*4882a593Smuzhiyun if (err || !is_empty(sbuf, len)) {
934*4882a593Smuzhiyun dbg_rcvry("cleaning head at %d:%d", lnum, offs);
935*4882a593Smuzhiyun if (offs == 0)
936*4882a593Smuzhiyun return ubifs_leb_unmap(c, lnum);
937*4882a593Smuzhiyun err = ubifs_leb_read(c, lnum, sbuf, 0, offs, 1);
938*4882a593Smuzhiyun if (err)
939*4882a593Smuzhiyun return err;
940*4882a593Smuzhiyun return ubifs_leb_change(c, lnum, sbuf, offs);
941*4882a593Smuzhiyun }
942*4882a593Smuzhiyun
943*4882a593Smuzhiyun return 0;
944*4882a593Smuzhiyun }
945*4882a593Smuzhiyun
946*4882a593Smuzhiyun /**
947*4882a593Smuzhiyun * ubifs_recover_inl_heads - recover index and LPT heads.
948*4882a593Smuzhiyun * @c: UBIFS file-system description object
949*4882a593Smuzhiyun * @sbuf: LEB-sized buffer to use
950*4882a593Smuzhiyun *
951*4882a593Smuzhiyun * This function ensures that there is no data on the flash at the index and
952*4882a593Smuzhiyun * LPT head locations.
953*4882a593Smuzhiyun *
954*4882a593Smuzhiyun * This deals with the recovery of a half-completed journal commit. UBIFS is
955*4882a593Smuzhiyun * careful never to overwrite the last version of the index or the LPT. Because
956*4882a593Smuzhiyun * the index and LPT are wandering trees, data from a half-completed commit will
957*4882a593Smuzhiyun * not be referenced anywhere in UBIFS. The data will be either in LEBs that are
958*4882a593Smuzhiyun * assumed to be empty and will be unmapped anyway before use, or in the index
959*4882a593Smuzhiyun * and LPT heads.
960*4882a593Smuzhiyun *
961*4882a593Smuzhiyun * This function returns %0 on success and a negative error code on failure.
962*4882a593Smuzhiyun */
ubifs_recover_inl_heads(struct ubifs_info * c,void * sbuf)963*4882a593Smuzhiyun int ubifs_recover_inl_heads(struct ubifs_info *c, void *sbuf)
964*4882a593Smuzhiyun {
965*4882a593Smuzhiyun int err;
966*4882a593Smuzhiyun
967*4882a593Smuzhiyun ubifs_assert(!c->ro_mount || c->remounting_rw);
968*4882a593Smuzhiyun
969*4882a593Smuzhiyun dbg_rcvry("checking index head at %d:%d", c->ihead_lnum, c->ihead_offs);
970*4882a593Smuzhiyun err = recover_head(c, c->ihead_lnum, c->ihead_offs, sbuf);
971*4882a593Smuzhiyun if (err)
972*4882a593Smuzhiyun return err;
973*4882a593Smuzhiyun
974*4882a593Smuzhiyun dbg_rcvry("checking LPT head at %d:%d", c->nhead_lnum, c->nhead_offs);
975*4882a593Smuzhiyun
976*4882a593Smuzhiyun return recover_head(c, c->nhead_lnum, c->nhead_offs, sbuf);
977*4882a593Smuzhiyun }
978*4882a593Smuzhiyun
979*4882a593Smuzhiyun /**
980*4882a593Smuzhiyun * clean_an_unclean_leb - read and write a LEB to remove corruption.
981*4882a593Smuzhiyun * @c: UBIFS file-system description object
982*4882a593Smuzhiyun * @ucleb: unclean LEB information
983*4882a593Smuzhiyun * @sbuf: LEB-sized buffer to use
984*4882a593Smuzhiyun *
985*4882a593Smuzhiyun * This function reads a LEB up to a point pre-determined by the mount recovery,
986*4882a593Smuzhiyun * checks the nodes, and writes the result back to the flash, thereby cleaning
987*4882a593Smuzhiyun * off any following corruption, or non-fatal ECC errors.
988*4882a593Smuzhiyun *
989*4882a593Smuzhiyun * This function returns %0 on success and a negative error code on failure.
990*4882a593Smuzhiyun */
clean_an_unclean_leb(struct ubifs_info * c,struct ubifs_unclean_leb * ucleb,void * sbuf)991*4882a593Smuzhiyun static int clean_an_unclean_leb(struct ubifs_info *c,
992*4882a593Smuzhiyun struct ubifs_unclean_leb *ucleb, void *sbuf)
993*4882a593Smuzhiyun {
994*4882a593Smuzhiyun int err, lnum = ucleb->lnum, offs = 0, len = ucleb->endpt, quiet = 1;
995*4882a593Smuzhiyun void *buf = sbuf;
996*4882a593Smuzhiyun
997*4882a593Smuzhiyun dbg_rcvry("LEB %d len %d", lnum, len);
998*4882a593Smuzhiyun
999*4882a593Smuzhiyun if (len == 0) {
1000*4882a593Smuzhiyun /* Nothing to read, just unmap it */
1001*4882a593Smuzhiyun return ubifs_leb_unmap(c, lnum);
1002*4882a593Smuzhiyun }
1003*4882a593Smuzhiyun
1004*4882a593Smuzhiyun err = ubifs_leb_read(c, lnum, buf, offs, len, 0);
1005*4882a593Smuzhiyun if (err && err != -EBADMSG)
1006*4882a593Smuzhiyun return err;
1007*4882a593Smuzhiyun
1008*4882a593Smuzhiyun while (len >= 8) {
1009*4882a593Smuzhiyun int ret;
1010*4882a593Smuzhiyun
1011*4882a593Smuzhiyun cond_resched();
1012*4882a593Smuzhiyun
1013*4882a593Smuzhiyun /* Scan quietly until there is an error */
1014*4882a593Smuzhiyun ret = ubifs_scan_a_node(c, buf, len, lnum, offs, quiet);
1015*4882a593Smuzhiyun
1016*4882a593Smuzhiyun if (ret == SCANNED_A_NODE) {
1017*4882a593Smuzhiyun /* A valid node, and not a padding node */
1018*4882a593Smuzhiyun struct ubifs_ch *ch = buf;
1019*4882a593Smuzhiyun int node_len;
1020*4882a593Smuzhiyun
1021*4882a593Smuzhiyun node_len = ALIGN(le32_to_cpu(ch->len), 8);
1022*4882a593Smuzhiyun offs += node_len;
1023*4882a593Smuzhiyun buf += node_len;
1024*4882a593Smuzhiyun len -= node_len;
1025*4882a593Smuzhiyun continue;
1026*4882a593Smuzhiyun }
1027*4882a593Smuzhiyun
1028*4882a593Smuzhiyun if (ret > 0) {
1029*4882a593Smuzhiyun /* Padding bytes or a valid padding node */
1030*4882a593Smuzhiyun offs += ret;
1031*4882a593Smuzhiyun buf += ret;
1032*4882a593Smuzhiyun len -= ret;
1033*4882a593Smuzhiyun continue;
1034*4882a593Smuzhiyun }
1035*4882a593Smuzhiyun
1036*4882a593Smuzhiyun if (ret == SCANNED_EMPTY_SPACE) {
1037*4882a593Smuzhiyun ubifs_err(c, "unexpected empty space at %d:%d",
1038*4882a593Smuzhiyun lnum, offs);
1039*4882a593Smuzhiyun return -EUCLEAN;
1040*4882a593Smuzhiyun }
1041*4882a593Smuzhiyun
1042*4882a593Smuzhiyun if (quiet) {
1043*4882a593Smuzhiyun /* Redo the last scan but noisily */
1044*4882a593Smuzhiyun quiet = 0;
1045*4882a593Smuzhiyun continue;
1046*4882a593Smuzhiyun }
1047*4882a593Smuzhiyun
1048*4882a593Smuzhiyun ubifs_scanned_corruption(c, lnum, offs, buf);
1049*4882a593Smuzhiyun return -EUCLEAN;
1050*4882a593Smuzhiyun }
1051*4882a593Smuzhiyun
1052*4882a593Smuzhiyun /* Pad to min_io_size */
1053*4882a593Smuzhiyun len = ALIGN(ucleb->endpt, c->min_io_size);
1054*4882a593Smuzhiyun if (len > ucleb->endpt) {
1055*4882a593Smuzhiyun int pad_len = len - ALIGN(ucleb->endpt, 8);
1056*4882a593Smuzhiyun
1057*4882a593Smuzhiyun if (pad_len > 0) {
1058*4882a593Smuzhiyun buf = c->sbuf + len - pad_len;
1059*4882a593Smuzhiyun ubifs_pad(c, buf, pad_len);
1060*4882a593Smuzhiyun }
1061*4882a593Smuzhiyun }
1062*4882a593Smuzhiyun
1063*4882a593Smuzhiyun /* Write back the LEB atomically */
1064*4882a593Smuzhiyun err = ubifs_leb_change(c, lnum, sbuf, len);
1065*4882a593Smuzhiyun if (err)
1066*4882a593Smuzhiyun return err;
1067*4882a593Smuzhiyun
1068*4882a593Smuzhiyun dbg_rcvry("cleaned LEB %d", lnum);
1069*4882a593Smuzhiyun
1070*4882a593Smuzhiyun return 0;
1071*4882a593Smuzhiyun }
1072*4882a593Smuzhiyun
1073*4882a593Smuzhiyun /**
1074*4882a593Smuzhiyun * ubifs_clean_lebs - clean LEBs recovered during read-only mount.
1075*4882a593Smuzhiyun * @c: UBIFS file-system description object
1076*4882a593Smuzhiyun * @sbuf: LEB-sized buffer to use
1077*4882a593Smuzhiyun *
1078*4882a593Smuzhiyun * This function cleans a LEB identified during recovery that needs to be
1079*4882a593Smuzhiyun * written but was not because UBIFS was mounted read-only. This happens when
1080*4882a593Smuzhiyun * remounting to read-write mode.
1081*4882a593Smuzhiyun *
1082*4882a593Smuzhiyun * This function returns %0 on success and a negative error code on failure.
1083*4882a593Smuzhiyun */
ubifs_clean_lebs(struct ubifs_info * c,void * sbuf)1084*4882a593Smuzhiyun int ubifs_clean_lebs(struct ubifs_info *c, void *sbuf)
1085*4882a593Smuzhiyun {
1086*4882a593Smuzhiyun dbg_rcvry("recovery");
1087*4882a593Smuzhiyun while (!list_empty(&c->unclean_leb_list)) {
1088*4882a593Smuzhiyun struct ubifs_unclean_leb *ucleb;
1089*4882a593Smuzhiyun int err;
1090*4882a593Smuzhiyun
1091*4882a593Smuzhiyun ucleb = list_entry(c->unclean_leb_list.next,
1092*4882a593Smuzhiyun struct ubifs_unclean_leb, list);
1093*4882a593Smuzhiyun err = clean_an_unclean_leb(c, ucleb, sbuf);
1094*4882a593Smuzhiyun if (err)
1095*4882a593Smuzhiyun return err;
1096*4882a593Smuzhiyun list_del(&ucleb->list);
1097*4882a593Smuzhiyun kfree(ucleb);
1098*4882a593Smuzhiyun }
1099*4882a593Smuzhiyun return 0;
1100*4882a593Smuzhiyun }
1101*4882a593Smuzhiyun
1102*4882a593Smuzhiyun #ifndef __UBOOT__
1103*4882a593Smuzhiyun /**
1104*4882a593Smuzhiyun * grab_empty_leb - grab an empty LEB to use as GC LEB and run commit.
1105*4882a593Smuzhiyun * @c: UBIFS file-system description object
1106*4882a593Smuzhiyun *
1107*4882a593Smuzhiyun * This is a helper function for 'ubifs_rcvry_gc_commit()' which grabs an empty
1108*4882a593Smuzhiyun * LEB to be used as GC LEB (@c->gc_lnum), and then runs the commit. Returns
1109*4882a593Smuzhiyun * zero in case of success and a negative error code in case of failure.
1110*4882a593Smuzhiyun */
grab_empty_leb(struct ubifs_info * c)1111*4882a593Smuzhiyun static int grab_empty_leb(struct ubifs_info *c)
1112*4882a593Smuzhiyun {
1113*4882a593Smuzhiyun int lnum, err;
1114*4882a593Smuzhiyun
1115*4882a593Smuzhiyun /*
1116*4882a593Smuzhiyun * Note, it is very important to first search for an empty LEB and then
1117*4882a593Smuzhiyun * run the commit, not vice-versa. The reason is that there might be
1118*4882a593Smuzhiyun * only one empty LEB at the moment, the one which has been the
1119*4882a593Smuzhiyun * @c->gc_lnum just before the power cut happened. During the regular
1120*4882a593Smuzhiyun * UBIFS operation (not now) @c->gc_lnum is marked as "taken", so no
1121*4882a593Smuzhiyun * one but GC can grab it. But at this moment this single empty LEB is
1122*4882a593Smuzhiyun * not marked as taken, so if we run commit - what happens? Right, the
1123*4882a593Smuzhiyun * commit will grab it and write the index there. Remember that the
1124*4882a593Smuzhiyun * index always expands as long as there is free space, and it only
1125*4882a593Smuzhiyun * starts consolidating when we run out of space.
1126*4882a593Smuzhiyun *
1127*4882a593Smuzhiyun * IOW, if we run commit now, we might not be able to find a free LEB
1128*4882a593Smuzhiyun * after this.
1129*4882a593Smuzhiyun */
1130*4882a593Smuzhiyun lnum = ubifs_find_free_leb_for_idx(c);
1131*4882a593Smuzhiyun if (lnum < 0) {
1132*4882a593Smuzhiyun ubifs_err(c, "could not find an empty LEB");
1133*4882a593Smuzhiyun ubifs_dump_lprops(c);
1134*4882a593Smuzhiyun ubifs_dump_budg(c, &c->bi);
1135*4882a593Smuzhiyun return lnum;
1136*4882a593Smuzhiyun }
1137*4882a593Smuzhiyun
1138*4882a593Smuzhiyun /* Reset the index flag */
1139*4882a593Smuzhiyun err = ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0,
1140*4882a593Smuzhiyun LPROPS_INDEX, 0);
1141*4882a593Smuzhiyun if (err)
1142*4882a593Smuzhiyun return err;
1143*4882a593Smuzhiyun
1144*4882a593Smuzhiyun c->gc_lnum = lnum;
1145*4882a593Smuzhiyun dbg_rcvry("found empty LEB %d, run commit", lnum);
1146*4882a593Smuzhiyun
1147*4882a593Smuzhiyun return ubifs_run_commit(c);
1148*4882a593Smuzhiyun }
1149*4882a593Smuzhiyun
1150*4882a593Smuzhiyun /**
1151*4882a593Smuzhiyun * ubifs_rcvry_gc_commit - recover the GC LEB number and run the commit.
1152*4882a593Smuzhiyun * @c: UBIFS file-system description object
1153*4882a593Smuzhiyun *
1154*4882a593Smuzhiyun * Out-of-place garbage collection requires always one empty LEB with which to
1155*4882a593Smuzhiyun * start garbage collection. The LEB number is recorded in c->gc_lnum and is
1156*4882a593Smuzhiyun * written to the master node on unmounting. In the case of an unclean unmount
1157*4882a593Smuzhiyun * the value of gc_lnum recorded in the master node is out of date and cannot
1158*4882a593Smuzhiyun * be used. Instead, recovery must allocate an empty LEB for this purpose.
1159*4882a593Smuzhiyun * However, there may not be enough empty space, in which case it must be
1160*4882a593Smuzhiyun * possible to GC the dirtiest LEB into the GC head LEB.
1161*4882a593Smuzhiyun *
1162*4882a593Smuzhiyun * This function also runs the commit which causes the TNC updates from
1163*4882a593Smuzhiyun * size-recovery and orphans to be written to the flash. That is important to
1164*4882a593Smuzhiyun * ensure correct replay order for subsequent mounts.
1165*4882a593Smuzhiyun *
1166*4882a593Smuzhiyun * This function returns %0 on success and a negative error code on failure.
1167*4882a593Smuzhiyun */
ubifs_rcvry_gc_commit(struct ubifs_info * c)1168*4882a593Smuzhiyun int ubifs_rcvry_gc_commit(struct ubifs_info *c)
1169*4882a593Smuzhiyun {
1170*4882a593Smuzhiyun struct ubifs_wbuf *wbuf = &c->jheads[GCHD].wbuf;
1171*4882a593Smuzhiyun struct ubifs_lprops lp;
1172*4882a593Smuzhiyun int err;
1173*4882a593Smuzhiyun
1174*4882a593Smuzhiyun dbg_rcvry("GC head LEB %d, offs %d", wbuf->lnum, wbuf->offs);
1175*4882a593Smuzhiyun
1176*4882a593Smuzhiyun c->gc_lnum = -1;
1177*4882a593Smuzhiyun if (wbuf->lnum == -1 || wbuf->offs == c->leb_size)
1178*4882a593Smuzhiyun return grab_empty_leb(c);
1179*4882a593Smuzhiyun
1180*4882a593Smuzhiyun err = ubifs_find_dirty_leb(c, &lp, wbuf->offs, 2);
1181*4882a593Smuzhiyun if (err) {
1182*4882a593Smuzhiyun if (err != -ENOSPC)
1183*4882a593Smuzhiyun return err;
1184*4882a593Smuzhiyun
1185*4882a593Smuzhiyun dbg_rcvry("could not find a dirty LEB");
1186*4882a593Smuzhiyun return grab_empty_leb(c);
1187*4882a593Smuzhiyun }
1188*4882a593Smuzhiyun
1189*4882a593Smuzhiyun ubifs_assert(!(lp.flags & LPROPS_INDEX));
1190*4882a593Smuzhiyun ubifs_assert(lp.free + lp.dirty >= wbuf->offs);
1191*4882a593Smuzhiyun
1192*4882a593Smuzhiyun /*
1193*4882a593Smuzhiyun * We run the commit before garbage collection otherwise subsequent
1194*4882a593Smuzhiyun * mounts will see the GC and orphan deletion in a different order.
1195*4882a593Smuzhiyun */
1196*4882a593Smuzhiyun dbg_rcvry("committing");
1197*4882a593Smuzhiyun err = ubifs_run_commit(c);
1198*4882a593Smuzhiyun if (err)
1199*4882a593Smuzhiyun return err;
1200*4882a593Smuzhiyun
1201*4882a593Smuzhiyun dbg_rcvry("GC'ing LEB %d", lp.lnum);
1202*4882a593Smuzhiyun mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
1203*4882a593Smuzhiyun err = ubifs_garbage_collect_leb(c, &lp);
1204*4882a593Smuzhiyun if (err >= 0) {
1205*4882a593Smuzhiyun int err2 = ubifs_wbuf_sync_nolock(wbuf);
1206*4882a593Smuzhiyun
1207*4882a593Smuzhiyun if (err2)
1208*4882a593Smuzhiyun err = err2;
1209*4882a593Smuzhiyun }
1210*4882a593Smuzhiyun mutex_unlock(&wbuf->io_mutex);
1211*4882a593Smuzhiyun if (err < 0) {
1212*4882a593Smuzhiyun ubifs_err(c, "GC failed, error %d", err);
1213*4882a593Smuzhiyun if (err == -EAGAIN)
1214*4882a593Smuzhiyun err = -EINVAL;
1215*4882a593Smuzhiyun return err;
1216*4882a593Smuzhiyun }
1217*4882a593Smuzhiyun
1218*4882a593Smuzhiyun ubifs_assert(err == LEB_RETAINED);
1219*4882a593Smuzhiyun if (err != LEB_RETAINED)
1220*4882a593Smuzhiyun return -EINVAL;
1221*4882a593Smuzhiyun
1222*4882a593Smuzhiyun err = ubifs_leb_unmap(c, c->gc_lnum);
1223*4882a593Smuzhiyun if (err)
1224*4882a593Smuzhiyun return err;
1225*4882a593Smuzhiyun
1226*4882a593Smuzhiyun dbg_rcvry("allocated LEB %d for GC", lp.lnum);
1227*4882a593Smuzhiyun return 0;
1228*4882a593Smuzhiyun }
1229*4882a593Smuzhiyun #else
ubifs_rcvry_gc_commit(struct ubifs_info * c)1230*4882a593Smuzhiyun int ubifs_rcvry_gc_commit(struct ubifs_info *c)
1231*4882a593Smuzhiyun {
1232*4882a593Smuzhiyun return 0;
1233*4882a593Smuzhiyun }
1234*4882a593Smuzhiyun #endif
1235*4882a593Smuzhiyun
1236*4882a593Smuzhiyun /**
1237*4882a593Smuzhiyun * struct size_entry - inode size information for recovery.
1238*4882a593Smuzhiyun * @rb: link in the RB-tree of sizes
1239*4882a593Smuzhiyun * @inum: inode number
1240*4882a593Smuzhiyun * @i_size: size on inode
1241*4882a593Smuzhiyun * @d_size: maximum size based on data nodes
1242*4882a593Smuzhiyun * @exists: indicates whether the inode exists
1243*4882a593Smuzhiyun * @inode: inode if pinned in memory awaiting rw mode to fix it
1244*4882a593Smuzhiyun */
1245*4882a593Smuzhiyun struct size_entry {
1246*4882a593Smuzhiyun struct rb_node rb;
1247*4882a593Smuzhiyun ino_t inum;
1248*4882a593Smuzhiyun loff_t i_size;
1249*4882a593Smuzhiyun loff_t d_size;
1250*4882a593Smuzhiyun int exists;
1251*4882a593Smuzhiyun struct inode *inode;
1252*4882a593Smuzhiyun };
1253*4882a593Smuzhiyun
1254*4882a593Smuzhiyun /**
1255*4882a593Smuzhiyun * add_ino - add an entry to the size tree.
1256*4882a593Smuzhiyun * @c: UBIFS file-system description object
1257*4882a593Smuzhiyun * @inum: inode number
1258*4882a593Smuzhiyun * @i_size: size on inode
1259*4882a593Smuzhiyun * @d_size: maximum size based on data nodes
1260*4882a593Smuzhiyun * @exists: indicates whether the inode exists
1261*4882a593Smuzhiyun */
add_ino(struct ubifs_info * c,ino_t inum,loff_t i_size,loff_t d_size,int exists)1262*4882a593Smuzhiyun static int add_ino(struct ubifs_info *c, ino_t inum, loff_t i_size,
1263*4882a593Smuzhiyun loff_t d_size, int exists)
1264*4882a593Smuzhiyun {
1265*4882a593Smuzhiyun struct rb_node **p = &c->size_tree.rb_node, *parent = NULL;
1266*4882a593Smuzhiyun struct size_entry *e;
1267*4882a593Smuzhiyun
1268*4882a593Smuzhiyun while (*p) {
1269*4882a593Smuzhiyun parent = *p;
1270*4882a593Smuzhiyun e = rb_entry(parent, struct size_entry, rb);
1271*4882a593Smuzhiyun if (inum < e->inum)
1272*4882a593Smuzhiyun p = &(*p)->rb_left;
1273*4882a593Smuzhiyun else
1274*4882a593Smuzhiyun p = &(*p)->rb_right;
1275*4882a593Smuzhiyun }
1276*4882a593Smuzhiyun
1277*4882a593Smuzhiyun e = kzalloc(sizeof(struct size_entry), GFP_KERNEL);
1278*4882a593Smuzhiyun if (!e)
1279*4882a593Smuzhiyun return -ENOMEM;
1280*4882a593Smuzhiyun
1281*4882a593Smuzhiyun e->inum = inum;
1282*4882a593Smuzhiyun e->i_size = i_size;
1283*4882a593Smuzhiyun e->d_size = d_size;
1284*4882a593Smuzhiyun e->exists = exists;
1285*4882a593Smuzhiyun
1286*4882a593Smuzhiyun rb_link_node(&e->rb, parent, p);
1287*4882a593Smuzhiyun rb_insert_color(&e->rb, &c->size_tree);
1288*4882a593Smuzhiyun
1289*4882a593Smuzhiyun return 0;
1290*4882a593Smuzhiyun }
1291*4882a593Smuzhiyun
1292*4882a593Smuzhiyun /**
1293*4882a593Smuzhiyun * find_ino - find an entry on the size tree.
1294*4882a593Smuzhiyun * @c: UBIFS file-system description object
1295*4882a593Smuzhiyun * @inum: inode number
1296*4882a593Smuzhiyun */
find_ino(struct ubifs_info * c,ino_t inum)1297*4882a593Smuzhiyun static struct size_entry *find_ino(struct ubifs_info *c, ino_t inum)
1298*4882a593Smuzhiyun {
1299*4882a593Smuzhiyun struct rb_node *p = c->size_tree.rb_node;
1300*4882a593Smuzhiyun struct size_entry *e;
1301*4882a593Smuzhiyun
1302*4882a593Smuzhiyun while (p) {
1303*4882a593Smuzhiyun e = rb_entry(p, struct size_entry, rb);
1304*4882a593Smuzhiyun if (inum < e->inum)
1305*4882a593Smuzhiyun p = p->rb_left;
1306*4882a593Smuzhiyun else if (inum > e->inum)
1307*4882a593Smuzhiyun p = p->rb_right;
1308*4882a593Smuzhiyun else
1309*4882a593Smuzhiyun return e;
1310*4882a593Smuzhiyun }
1311*4882a593Smuzhiyun return NULL;
1312*4882a593Smuzhiyun }
1313*4882a593Smuzhiyun
1314*4882a593Smuzhiyun /**
1315*4882a593Smuzhiyun * remove_ino - remove an entry from the size tree.
1316*4882a593Smuzhiyun * @c: UBIFS file-system description object
1317*4882a593Smuzhiyun * @inum: inode number
1318*4882a593Smuzhiyun */
remove_ino(struct ubifs_info * c,ino_t inum)1319*4882a593Smuzhiyun static void remove_ino(struct ubifs_info *c, ino_t inum)
1320*4882a593Smuzhiyun {
1321*4882a593Smuzhiyun struct size_entry *e = find_ino(c, inum);
1322*4882a593Smuzhiyun
1323*4882a593Smuzhiyun if (!e)
1324*4882a593Smuzhiyun return;
1325*4882a593Smuzhiyun rb_erase(&e->rb, &c->size_tree);
1326*4882a593Smuzhiyun kfree(e);
1327*4882a593Smuzhiyun }
1328*4882a593Smuzhiyun
1329*4882a593Smuzhiyun /**
1330*4882a593Smuzhiyun * ubifs_destroy_size_tree - free resources related to the size tree.
1331*4882a593Smuzhiyun * @c: UBIFS file-system description object
1332*4882a593Smuzhiyun */
ubifs_destroy_size_tree(struct ubifs_info * c)1333*4882a593Smuzhiyun void ubifs_destroy_size_tree(struct ubifs_info *c)
1334*4882a593Smuzhiyun {
1335*4882a593Smuzhiyun struct size_entry *e, *n;
1336*4882a593Smuzhiyun
1337*4882a593Smuzhiyun rbtree_postorder_for_each_entry_safe(e, n, &c->size_tree, rb) {
1338*4882a593Smuzhiyun if (e->inode)
1339*4882a593Smuzhiyun iput(e->inode);
1340*4882a593Smuzhiyun kfree(e);
1341*4882a593Smuzhiyun }
1342*4882a593Smuzhiyun
1343*4882a593Smuzhiyun c->size_tree = RB_ROOT;
1344*4882a593Smuzhiyun }
1345*4882a593Smuzhiyun
1346*4882a593Smuzhiyun /**
1347*4882a593Smuzhiyun * ubifs_recover_size_accum - accumulate inode sizes for recovery.
1348*4882a593Smuzhiyun * @c: UBIFS file-system description object
1349*4882a593Smuzhiyun * @key: node key
1350*4882a593Smuzhiyun * @deletion: node is for a deletion
1351*4882a593Smuzhiyun * @new_size: inode size
1352*4882a593Smuzhiyun *
1353*4882a593Smuzhiyun * This function has two purposes:
1354*4882a593Smuzhiyun * 1) to ensure there are no data nodes that fall outside the inode size
1355*4882a593Smuzhiyun * 2) to ensure there are no data nodes for inodes that do not exist
1356*4882a593Smuzhiyun * To accomplish those purposes, a rb-tree is constructed containing an entry
1357*4882a593Smuzhiyun * for each inode number in the journal that has not been deleted, and recording
1358*4882a593Smuzhiyun * the size from the inode node, the maximum size of any data node (also altered
1359*4882a593Smuzhiyun * by truncations) and a flag indicating a inode number for which no inode node
1360*4882a593Smuzhiyun * was present in the journal.
1361*4882a593Smuzhiyun *
1362*4882a593Smuzhiyun * Note that there is still the possibility that there are data nodes that have
1363*4882a593Smuzhiyun * been committed that are beyond the inode size, however the only way to find
1364*4882a593Smuzhiyun * them would be to scan the entire index. Alternatively, some provision could
1365*4882a593Smuzhiyun * be made to record the size of inodes at the start of commit, which would seem
1366*4882a593Smuzhiyun * very cumbersome for a scenario that is quite unlikely and the only negative
1367*4882a593Smuzhiyun * consequence of which is wasted space.
1368*4882a593Smuzhiyun *
1369*4882a593Smuzhiyun * This functions returns %0 on success and a negative error code on failure.
1370*4882a593Smuzhiyun */
ubifs_recover_size_accum(struct ubifs_info * c,union ubifs_key * key,int deletion,loff_t new_size)1371*4882a593Smuzhiyun int ubifs_recover_size_accum(struct ubifs_info *c, union ubifs_key *key,
1372*4882a593Smuzhiyun int deletion, loff_t new_size)
1373*4882a593Smuzhiyun {
1374*4882a593Smuzhiyun ino_t inum = key_inum(c, key);
1375*4882a593Smuzhiyun struct size_entry *e;
1376*4882a593Smuzhiyun int err;
1377*4882a593Smuzhiyun
1378*4882a593Smuzhiyun switch (key_type(c, key)) {
1379*4882a593Smuzhiyun case UBIFS_INO_KEY:
1380*4882a593Smuzhiyun if (deletion)
1381*4882a593Smuzhiyun remove_ino(c, inum);
1382*4882a593Smuzhiyun else {
1383*4882a593Smuzhiyun e = find_ino(c, inum);
1384*4882a593Smuzhiyun if (e) {
1385*4882a593Smuzhiyun e->i_size = new_size;
1386*4882a593Smuzhiyun e->exists = 1;
1387*4882a593Smuzhiyun } else {
1388*4882a593Smuzhiyun err = add_ino(c, inum, new_size, 0, 1);
1389*4882a593Smuzhiyun if (err)
1390*4882a593Smuzhiyun return err;
1391*4882a593Smuzhiyun }
1392*4882a593Smuzhiyun }
1393*4882a593Smuzhiyun break;
1394*4882a593Smuzhiyun case UBIFS_DATA_KEY:
1395*4882a593Smuzhiyun e = find_ino(c, inum);
1396*4882a593Smuzhiyun if (e) {
1397*4882a593Smuzhiyun if (new_size > e->d_size)
1398*4882a593Smuzhiyun e->d_size = new_size;
1399*4882a593Smuzhiyun } else {
1400*4882a593Smuzhiyun err = add_ino(c, inum, 0, new_size, 0);
1401*4882a593Smuzhiyun if (err)
1402*4882a593Smuzhiyun return err;
1403*4882a593Smuzhiyun }
1404*4882a593Smuzhiyun break;
1405*4882a593Smuzhiyun case UBIFS_TRUN_KEY:
1406*4882a593Smuzhiyun e = find_ino(c, inum);
1407*4882a593Smuzhiyun if (e)
1408*4882a593Smuzhiyun e->d_size = new_size;
1409*4882a593Smuzhiyun break;
1410*4882a593Smuzhiyun }
1411*4882a593Smuzhiyun return 0;
1412*4882a593Smuzhiyun }
1413*4882a593Smuzhiyun
1414*4882a593Smuzhiyun #ifndef __UBOOT__
1415*4882a593Smuzhiyun /**
1416*4882a593Smuzhiyun * fix_size_in_place - fix inode size in place on flash.
1417*4882a593Smuzhiyun * @c: UBIFS file-system description object
1418*4882a593Smuzhiyun * @e: inode size information for recovery
1419*4882a593Smuzhiyun */
fix_size_in_place(struct ubifs_info * c,struct size_entry * e)1420*4882a593Smuzhiyun static int fix_size_in_place(struct ubifs_info *c, struct size_entry *e)
1421*4882a593Smuzhiyun {
1422*4882a593Smuzhiyun struct ubifs_ino_node *ino = c->sbuf;
1423*4882a593Smuzhiyun unsigned char *p;
1424*4882a593Smuzhiyun union ubifs_key key;
1425*4882a593Smuzhiyun int err, lnum, offs, len;
1426*4882a593Smuzhiyun loff_t i_size;
1427*4882a593Smuzhiyun uint32_t crc;
1428*4882a593Smuzhiyun
1429*4882a593Smuzhiyun /* Locate the inode node LEB number and offset */
1430*4882a593Smuzhiyun ino_key_init(c, &key, e->inum);
1431*4882a593Smuzhiyun err = ubifs_tnc_locate(c, &key, ino, &lnum, &offs);
1432*4882a593Smuzhiyun if (err)
1433*4882a593Smuzhiyun goto out;
1434*4882a593Smuzhiyun /*
1435*4882a593Smuzhiyun * If the size recorded on the inode node is greater than the size that
1436*4882a593Smuzhiyun * was calculated from nodes in the journal then don't change the inode.
1437*4882a593Smuzhiyun */
1438*4882a593Smuzhiyun i_size = le64_to_cpu(ino->size);
1439*4882a593Smuzhiyun if (i_size >= e->d_size)
1440*4882a593Smuzhiyun return 0;
1441*4882a593Smuzhiyun /* Read the LEB */
1442*4882a593Smuzhiyun err = ubifs_leb_read(c, lnum, c->sbuf, 0, c->leb_size, 1);
1443*4882a593Smuzhiyun if (err)
1444*4882a593Smuzhiyun goto out;
1445*4882a593Smuzhiyun /* Change the size field and recalculate the CRC */
1446*4882a593Smuzhiyun ino = c->sbuf + offs;
1447*4882a593Smuzhiyun ino->size = cpu_to_le64(e->d_size);
1448*4882a593Smuzhiyun len = le32_to_cpu(ino->ch.len);
1449*4882a593Smuzhiyun crc = crc32(UBIFS_CRC32_INIT, (void *)ino + 8, len - 8);
1450*4882a593Smuzhiyun ino->ch.crc = cpu_to_le32(crc);
1451*4882a593Smuzhiyun /* Work out where data in the LEB ends and free space begins */
1452*4882a593Smuzhiyun p = c->sbuf;
1453*4882a593Smuzhiyun len = c->leb_size - 1;
1454*4882a593Smuzhiyun while (p[len] == 0xff)
1455*4882a593Smuzhiyun len -= 1;
1456*4882a593Smuzhiyun len = ALIGN(len + 1, c->min_io_size);
1457*4882a593Smuzhiyun /* Atomically write the fixed LEB back again */
1458*4882a593Smuzhiyun err = ubifs_leb_change(c, lnum, c->sbuf, len);
1459*4882a593Smuzhiyun if (err)
1460*4882a593Smuzhiyun goto out;
1461*4882a593Smuzhiyun dbg_rcvry("inode %lu at %d:%d size %lld -> %lld",
1462*4882a593Smuzhiyun (unsigned long)e->inum, lnum, offs, i_size, e->d_size);
1463*4882a593Smuzhiyun return 0;
1464*4882a593Smuzhiyun
1465*4882a593Smuzhiyun out:
1466*4882a593Smuzhiyun ubifs_warn(c, "inode %lu failed to fix size %lld -> %lld error %d",
1467*4882a593Smuzhiyun (unsigned long)e->inum, e->i_size, e->d_size, err);
1468*4882a593Smuzhiyun return err;
1469*4882a593Smuzhiyun }
1470*4882a593Smuzhiyun #endif
1471*4882a593Smuzhiyun
1472*4882a593Smuzhiyun /**
1473*4882a593Smuzhiyun * ubifs_recover_size - recover inode size.
1474*4882a593Smuzhiyun * @c: UBIFS file-system description object
1475*4882a593Smuzhiyun *
1476*4882a593Smuzhiyun * This function attempts to fix inode size discrepancies identified by the
1477*4882a593Smuzhiyun * 'ubifs_recover_size_accum()' function.
1478*4882a593Smuzhiyun *
1479*4882a593Smuzhiyun * This functions returns %0 on success and a negative error code on failure.
1480*4882a593Smuzhiyun */
ubifs_recover_size(struct ubifs_info * c)1481*4882a593Smuzhiyun int ubifs_recover_size(struct ubifs_info *c)
1482*4882a593Smuzhiyun {
1483*4882a593Smuzhiyun struct rb_node *this = rb_first(&c->size_tree);
1484*4882a593Smuzhiyun
1485*4882a593Smuzhiyun while (this) {
1486*4882a593Smuzhiyun struct size_entry *e;
1487*4882a593Smuzhiyun int err;
1488*4882a593Smuzhiyun
1489*4882a593Smuzhiyun e = rb_entry(this, struct size_entry, rb);
1490*4882a593Smuzhiyun if (!e->exists) {
1491*4882a593Smuzhiyun union ubifs_key key;
1492*4882a593Smuzhiyun
1493*4882a593Smuzhiyun ino_key_init(c, &key, e->inum);
1494*4882a593Smuzhiyun err = ubifs_tnc_lookup(c, &key, c->sbuf);
1495*4882a593Smuzhiyun if (err && err != -ENOENT)
1496*4882a593Smuzhiyun return err;
1497*4882a593Smuzhiyun if (err == -ENOENT) {
1498*4882a593Smuzhiyun /* Remove data nodes that have no inode */
1499*4882a593Smuzhiyun dbg_rcvry("removing ino %lu",
1500*4882a593Smuzhiyun (unsigned long)e->inum);
1501*4882a593Smuzhiyun err = ubifs_tnc_remove_ino(c, e->inum);
1502*4882a593Smuzhiyun if (err)
1503*4882a593Smuzhiyun return err;
1504*4882a593Smuzhiyun } else {
1505*4882a593Smuzhiyun struct ubifs_ino_node *ino = c->sbuf;
1506*4882a593Smuzhiyun
1507*4882a593Smuzhiyun e->exists = 1;
1508*4882a593Smuzhiyun e->i_size = le64_to_cpu(ino->size);
1509*4882a593Smuzhiyun }
1510*4882a593Smuzhiyun }
1511*4882a593Smuzhiyun
1512*4882a593Smuzhiyun if (e->exists && e->i_size < e->d_size) {
1513*4882a593Smuzhiyun if (c->ro_mount) {
1514*4882a593Smuzhiyun /* Fix the inode size and pin it in memory */
1515*4882a593Smuzhiyun struct inode *inode;
1516*4882a593Smuzhiyun struct ubifs_inode *ui;
1517*4882a593Smuzhiyun
1518*4882a593Smuzhiyun ubifs_assert(!e->inode);
1519*4882a593Smuzhiyun
1520*4882a593Smuzhiyun inode = ubifs_iget(c->vfs_sb, e->inum);
1521*4882a593Smuzhiyun if (IS_ERR(inode))
1522*4882a593Smuzhiyun return PTR_ERR(inode);
1523*4882a593Smuzhiyun
1524*4882a593Smuzhiyun ui = ubifs_inode(inode);
1525*4882a593Smuzhiyun if (inode->i_size < e->d_size) {
1526*4882a593Smuzhiyun dbg_rcvry("ino %lu size %lld -> %lld",
1527*4882a593Smuzhiyun (unsigned long)e->inum,
1528*4882a593Smuzhiyun inode->i_size, e->d_size);
1529*4882a593Smuzhiyun inode->i_size = e->d_size;
1530*4882a593Smuzhiyun ui->ui_size = e->d_size;
1531*4882a593Smuzhiyun ui->synced_i_size = e->d_size;
1532*4882a593Smuzhiyun e->inode = inode;
1533*4882a593Smuzhiyun this = rb_next(this);
1534*4882a593Smuzhiyun continue;
1535*4882a593Smuzhiyun }
1536*4882a593Smuzhiyun iput(inode);
1537*4882a593Smuzhiyun #ifndef __UBOOT__
1538*4882a593Smuzhiyun } else {
1539*4882a593Smuzhiyun /* Fix the size in place */
1540*4882a593Smuzhiyun err = fix_size_in_place(c, e);
1541*4882a593Smuzhiyun if (err)
1542*4882a593Smuzhiyun return err;
1543*4882a593Smuzhiyun if (e->inode)
1544*4882a593Smuzhiyun iput(e->inode);
1545*4882a593Smuzhiyun #endif
1546*4882a593Smuzhiyun }
1547*4882a593Smuzhiyun }
1548*4882a593Smuzhiyun
1549*4882a593Smuzhiyun this = rb_next(this);
1550*4882a593Smuzhiyun rb_erase(&e->rb, &c->size_tree);
1551*4882a593Smuzhiyun kfree(e);
1552*4882a593Smuzhiyun }
1553*4882a593Smuzhiyun
1554*4882a593Smuzhiyun return 0;
1555*4882a593Smuzhiyun }
1556