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