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