blob: 4d10b6e36ec48099e60dc702665177e13d784d87 [file] [log] [blame]
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001/*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors: Adrian Hunter
20 * Artem Bityutskiy (Битюцкий Артём)
21 */
22
23/*
24 * This file implements functions needed to recover from unclean un-mounts.
25 * When UBIFS is mounted, it checks a flag on the master node to determine if
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020026 * an un-mount was completed successfully. If not, the process of mounting
Artem Bityutskiy6fb43742010-05-23 15:20:21 +030027 * incorporates additional checking and fixing of on-flash data structures.
Artem Bityutskiy1e517642008-07-14 19:08:37 +030028 * UBIFS always cleans away all remnants of an unclean un-mount, so that
29 * errors do not accumulate. However UBIFS defers recovery if it is mounted
30 * read-only, and the flash is not modified in that case.
Artem Bityutskiybe7b42a2011-02-06 16:41:06 +020031 *
32 * The general UBIFS approach to the recovery is that it recovers from
33 * corruptions which could be caused by power cuts, but it refuses to recover
34 * from corruption caused by other reasons. And UBIFS tries to distinguish
35 * between these 2 reasons of corruptions and silently recover in the former
36 * case and loudly complain in the latter case.
37 *
38 * UBIFS writes only to erased LEBs, so it writes only to the flash space
39 * containing only 0xFFs. UBIFS also always writes strictly from the beginning
40 * of the LEB to the end. And UBIFS assumes that the underlying flash media
Artem Bityutskiy2765df72011-02-02 09:22:54 +020041 * writes in @c->max_write_size bytes at a time.
Artem Bityutskiybe7b42a2011-02-06 16:41:06 +020042 *
43 * Hence, if UBIFS finds a corrupted node at offset X, it expects only the min.
44 * I/O unit corresponding to offset X to contain corrupted data, all the
45 * following min. I/O units have to contain empty space (all 0xFFs). If this is
46 * not true, the corruption cannot be the result of a power cut, and UBIFS
47 * refuses to mount.
Artem Bityutskiy1e517642008-07-14 19:08:37 +030048 */
49
50#include <linux/crc32.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090051#include <linux/slab.h>
Artem Bityutskiy1e517642008-07-14 19:08:37 +030052#include "ubifs.h"
53
54/**
55 * is_empty - determine whether a buffer is empty (contains all 0xff).
56 * @buf: buffer to clean
57 * @len: length of buffer
58 *
59 * This function returns %1 if the buffer is empty (contains all 0xff) otherwise
60 * %0 is returned.
61 */
62static int is_empty(void *buf, int len)
63{
64 uint8_t *p = buf;
65 int i;
66
67 for (i = 0; i < len; i++)
68 if (*p++ != 0xff)
69 return 0;
70 return 1;
71}
72
73/**
Artem Bityutskiy06112542009-06-29 19:27:14 +030074 * first_non_ff - find offset of the first non-0xff byte.
75 * @buf: buffer to search in
76 * @len: length of buffer
77 *
78 * This function returns offset of the first non-0xff byte in @buf or %-1 if
79 * the buffer contains only 0xff bytes.
80 */
81static int first_non_ff(void *buf, int len)
82{
83 uint8_t *p = buf;
84 int i;
85
86 for (i = 0; i < len; i++)
87 if (*p++ != 0xff)
88 return i;
89 return -1;
90}
91
92/**
Artem Bityutskiy1e517642008-07-14 19:08:37 +030093 * get_master_node - get the last valid master node allowing for corruption.
94 * @c: UBIFS file-system description object
95 * @lnum: LEB number
96 * @pbuf: buffer containing the LEB read, is returned here
97 * @mst: master node, if found, is returned here
98 * @cor: corruption, if found, is returned here
99 *
100 * This function allocates a buffer, reads the LEB into it, and finds and
101 * returns the last valid master node allowing for one area of corruption.
102 * The corrupt area, if there is one, must be consistent with the assumption
103 * that it is the result of an unclean unmount while the master node was being
104 * written. Under those circumstances, it is valid to use the previously written
105 * master node.
106 *
107 * This function returns %0 on success and a negative error code on failure.
108 */
109static int get_master_node(const struct ubifs_info *c, int lnum, void **pbuf,
110 struct ubifs_mst_node **mst, void **cor)
111{
112 const int sz = c->mst_node_alsz;
113 int err, offs, len;
114 void *sbuf, *buf;
115
116 sbuf = vmalloc(c->leb_size);
117 if (!sbuf)
118 return -ENOMEM;
119
120 err = ubi_read(c->ubi, lnum, sbuf, 0, c->leb_size);
121 if (err && err != -EBADMSG)
122 goto out_free;
123
124 /* Find the first position that is definitely not a node */
125 offs = 0;
126 buf = sbuf;
127 len = c->leb_size;
128 while (offs + UBIFS_MST_NODE_SZ <= c->leb_size) {
129 struct ubifs_ch *ch = buf;
130
131 if (le32_to_cpu(ch->magic) != UBIFS_NODE_MAGIC)
132 break;
133 offs += sz;
134 buf += sz;
135 len -= sz;
136 }
137 /* See if there was a valid master node before that */
138 if (offs) {
139 int ret;
140
141 offs -= sz;
142 buf -= sz;
143 len += sz;
144 ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 1);
145 if (ret != SCANNED_A_NODE && offs) {
146 /* Could have been corruption so check one place back */
147 offs -= sz;
148 buf -= sz;
149 len += sz;
150 ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 1);
151 if (ret != SCANNED_A_NODE)
152 /*
153 * We accept only one area of corruption because
154 * we are assuming that it was caused while
155 * trying to write a master node.
156 */
157 goto out_err;
158 }
159 if (ret == SCANNED_A_NODE) {
160 struct ubifs_ch *ch = buf;
161
162 if (ch->node_type != UBIFS_MST_NODE)
163 goto out_err;
164 dbg_rcvry("found a master node at %d:%d", lnum, offs);
165 *mst = buf;
166 offs += sz;
167 buf += sz;
168 len -= sz;
169 }
170 }
171 /* Check for corruption */
172 if (offs < c->leb_size) {
173 if (!is_empty(buf, min_t(int, len, sz))) {
174 *cor = buf;
175 dbg_rcvry("found corruption at %d:%d", lnum, offs);
176 }
177 offs += sz;
178 buf += sz;
179 len -= sz;
180 }
181 /* Check remaining empty space */
182 if (offs < c->leb_size)
183 if (!is_empty(buf, len))
184 goto out_err;
185 *pbuf = sbuf;
186 return 0;
187
188out_err:
189 err = -EINVAL;
190out_free:
191 vfree(sbuf);
192 *mst = NULL;
193 *cor = NULL;
194 return err;
195}
196
197/**
198 * write_rcvrd_mst_node - write recovered master node.
199 * @c: UBIFS file-system description object
200 * @mst: master node
201 *
202 * This function returns %0 on success and a negative error code on failure.
203 */
204static int write_rcvrd_mst_node(struct ubifs_info *c,
205 struct ubifs_mst_node *mst)
206{
207 int err = 0, lnum = UBIFS_MST_LNUM, sz = c->mst_node_alsz;
Harvey Harrison0ecb9522008-10-24 10:52:57 -0700208 __le32 save_flags;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300209
210 dbg_rcvry("recovery");
211
212 save_flags = mst->flags;
Harvey Harrison0ecb9522008-10-24 10:52:57 -0700213 mst->flags |= cpu_to_le32(UBIFS_MST_RCVRY);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300214
215 ubifs_prepare_node(c, mst, UBIFS_MST_NODE_SZ, 1);
216 err = ubi_leb_change(c->ubi, lnum, mst, sz, UBI_SHORTTERM);
217 if (err)
218 goto out;
219 err = ubi_leb_change(c->ubi, lnum + 1, mst, sz, UBI_SHORTTERM);
220 if (err)
221 goto out;
222out:
223 mst->flags = save_flags;
224 return err;
225}
226
227/**
228 * ubifs_recover_master_node - recover the master node.
229 * @c: UBIFS file-system description object
230 *
231 * This function recovers the master node from corruption that may occur due to
232 * an unclean unmount.
233 *
234 * This function returns %0 on success and a negative error code on failure.
235 */
236int ubifs_recover_master_node(struct ubifs_info *c)
237{
238 void *buf1 = NULL, *buf2 = NULL, *cor1 = NULL, *cor2 = NULL;
239 struct ubifs_mst_node *mst1 = NULL, *mst2 = NULL, *mst;
240 const int sz = c->mst_node_alsz;
241 int err, offs1, offs2;
242
243 dbg_rcvry("recovery");
244
245 err = get_master_node(c, UBIFS_MST_LNUM, &buf1, &mst1, &cor1);
246 if (err)
247 goto out_free;
248
249 err = get_master_node(c, UBIFS_MST_LNUM + 1, &buf2, &mst2, &cor2);
250 if (err)
251 goto out_free;
252
253 if (mst1) {
254 offs1 = (void *)mst1 - buf1;
255 if ((le32_to_cpu(mst1->flags) & UBIFS_MST_RCVRY) &&
256 (offs1 == 0 && !cor1)) {
257 /*
258 * mst1 was written by recovery at offset 0 with no
259 * corruption.
260 */
261 dbg_rcvry("recovery recovery");
262 mst = mst1;
263 } else if (mst2) {
264 offs2 = (void *)mst2 - buf2;
265 if (offs1 == offs2) {
266 /* Same offset, so must be the same */
267 if (memcmp((void *)mst1 + UBIFS_CH_SZ,
268 (void *)mst2 + UBIFS_CH_SZ,
269 UBIFS_MST_NODE_SZ - UBIFS_CH_SZ))
270 goto out_err;
271 mst = mst1;
272 } else if (offs2 + sz == offs1) {
273 /* 1st LEB was written, 2nd was not */
274 if (cor1)
275 goto out_err;
276 mst = mst1;
277 } else if (offs1 == 0 && offs2 + sz >= c->leb_size) {
278 /* 1st LEB was unmapped and written, 2nd not */
279 if (cor1)
280 goto out_err;
281 mst = mst1;
282 } else
283 goto out_err;
284 } else {
285 /*
286 * 2nd LEB was unmapped and about to be written, so
287 * there must be only one master node in the first LEB
288 * and no corruption.
289 */
290 if (offs1 != 0 || cor1)
291 goto out_err;
292 mst = mst1;
293 }
294 } else {
295 if (!mst2)
296 goto out_err;
297 /*
298 * 1st LEB was unmapped and about to be written, so there must
299 * be no room left in 2nd LEB.
300 */
301 offs2 = (void *)mst2 - buf2;
302 if (offs2 + sz + sz <= c->leb_size)
303 goto out_err;
304 mst = mst2;
305 }
306
Artem Bityutskiy348709b2009-08-25 15:00:55 +0300307 ubifs_msg("recovered master node from LEB %d",
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300308 (mst == mst1 ? UBIFS_MST_LNUM : UBIFS_MST_LNUM + 1));
309
310 memcpy(c->mst_node, mst, UBIFS_MST_NODE_SZ);
311
Artem Bityutskiy2ef13292010-09-19 18:34:26 +0300312 if (c->ro_mount) {
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300313 /* Read-only mode. Keep a copy for switching to rw mode */
314 c->rcvrd_mst_node = kmalloc(sz, GFP_KERNEL);
315 if (!c->rcvrd_mst_node) {
316 err = -ENOMEM;
317 goto out_free;
318 }
319 memcpy(c->rcvrd_mst_node, c->mst_node, UBIFS_MST_NODE_SZ);
Artem Bityutskiy6e0d9fd2011-04-21 14:49:55 +0300320
321 /*
322 * We had to recover the master node, which means there was an
323 * unclean reboot. However, it is possible that the master node
324 * is clean at this point, i.e., %UBIFS_MST_DIRTY is not set.
325 * E.g., consider the following chain of events:
326 *
327 * 1. UBIFS was cleanly unmounted, so the master node is clean
328 * 2. UBIFS is being mounted R/W and starts changing the master
329 * node in the first (%UBIFS_MST_LNUM). A power cut happens,
330 * so this LEB ends up with some amount of garbage at the
331 * end.
332 * 3. UBIFS is being mounted R/O. We reach this place and
333 * recover the master node from the second LEB
334 * (%UBIFS_MST_LNUM + 1). But we cannot update the media
335 * because we are being mounted R/O. We have to defer the
336 * operation.
337 * 4. However, this master node (@c->mst_node) is marked as
338 * clean (since the step 1). And if we just return, the
339 * mount code will be confused and won't recover the master
340 * node when it is re-mounter R/W later.
341 *
342 * Thus, to force the recovery by marking the master node as
343 * dirty.
344 */
345 c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300346 } else {
347 /* Write the recovered master node */
348 c->max_sqnum = le64_to_cpu(mst->ch.sqnum) - 1;
349 err = write_rcvrd_mst_node(c, c->mst_node);
350 if (err)
351 goto out_free;
352 }
353
354 vfree(buf2);
355 vfree(buf1);
356
357 return 0;
358
359out_err:
360 err = -EINVAL;
361out_free:
362 ubifs_err("failed to recover master node");
363 if (mst1) {
364 dbg_err("dumping first master node");
365 dbg_dump_node(c, mst1);
366 }
367 if (mst2) {
368 dbg_err("dumping second master node");
369 dbg_dump_node(c, mst2);
370 }
371 vfree(buf2);
372 vfree(buf1);
373 return err;
374}
375
376/**
377 * ubifs_write_rcvrd_mst_node - write the recovered master node.
378 * @c: UBIFS file-system description object
379 *
380 * This function writes the master node that was recovered during mounting in
381 * read-only mode and must now be written because we are remounting rw.
382 *
383 * This function returns %0 on success and a negative error code on failure.
384 */
385int ubifs_write_rcvrd_mst_node(struct ubifs_info *c)
386{
387 int err;
388
389 if (!c->rcvrd_mst_node)
390 return 0;
391 c->rcvrd_mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
392 c->mst_node->flags |= cpu_to_le32(UBIFS_MST_DIRTY);
393 err = write_rcvrd_mst_node(c, c->rcvrd_mst_node);
394 if (err)
395 return err;
396 kfree(c->rcvrd_mst_node);
397 c->rcvrd_mst_node = NULL;
398 return 0;
399}
400
401/**
402 * is_last_write - determine if an offset was in the last write to a LEB.
403 * @c: UBIFS file-system description object
404 * @buf: buffer to check
405 * @offs: offset to check
406 *
407 * This function returns %1 if @offs was in the last write to the LEB whose data
Artem Bityutskiy2765df72011-02-02 09:22:54 +0200408 * is in @buf, otherwise %0 is returned. The determination is made by checking
409 * for subsequent empty space starting from the next @c->max_write_size
410 * boundary.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300411 */
412static int is_last_write(const struct ubifs_info *c, void *buf, int offs)
413{
Artem Bityutskiy428ff9d2009-05-25 16:59:28 +0300414 int empty_offs, check_len;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300415 uint8_t *p;
416
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300417 /*
Artem Bityutskiy2765df72011-02-02 09:22:54 +0200418 * Round up to the next @c->max_write_size boundary i.e. @offs is in
419 * the last wbuf written. After that should be empty space.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300420 */
Artem Bityutskiy2765df72011-02-02 09:22:54 +0200421 empty_offs = ALIGN(offs + 1, c->max_write_size);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300422 check_len = c->leb_size - empty_offs;
423 p = buf + empty_offs - offs;
Artem Bityutskiy431102f2009-06-29 18:58:34 +0300424 return is_empty(p, check_len);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300425}
426
427/**
428 * clean_buf - clean the data from an LEB sitting in a buffer.
429 * @c: UBIFS file-system description object
430 * @buf: buffer to clean
431 * @lnum: LEB number to clean
432 * @offs: offset from which to clean
433 * @len: length of buffer
434 *
435 * This function pads up to the next min_io_size boundary (if there is one) and
436 * sets empty space to all 0xff. @buf, @offs and @len are updated to the next
Artem Bityutskiy428ff9d2009-05-25 16:59:28 +0300437 * @c->min_io_size boundary.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300438 */
439static void clean_buf(const struct ubifs_info *c, void **buf, int lnum,
440 int *offs, int *len)
441{
442 int empty_offs, pad_len;
443
444 lnum = lnum;
445 dbg_rcvry("cleaning corruption at %d:%d", lnum, *offs);
446
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300447 ubifs_assert(!(*offs & 7));
448 empty_offs = ALIGN(*offs, c->min_io_size);
449 pad_len = empty_offs - *offs;
450 ubifs_pad(c, *buf, pad_len);
451 *offs += pad_len;
452 *buf += pad_len;
453 *len -= pad_len;
454 memset(*buf, 0xff, c->leb_size - empty_offs);
455}
456
457/**
458 * no_more_nodes - determine if there are no more nodes in a buffer.
459 * @c: UBIFS file-system description object
460 * @buf: buffer to check
461 * @len: length of buffer
462 * @lnum: LEB number of the LEB from which @buf was read
463 * @offs: offset from which @buf was read
464 *
Adrian Hunterde097572009-03-20 11:09:04 +0100465 * This function ensures that the corrupted node at @offs is the last thing
466 * written to a LEB. This function returns %1 if more data is not found and
467 * %0 if more data is found.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300468 */
469static int no_more_nodes(const struct ubifs_info *c, void *buf, int len,
470 int lnum, int offs)
471{
Adrian Hunterde097572009-03-20 11:09:04 +0100472 struct ubifs_ch *ch = buf;
473 int skip, dlen = le32_to_cpu(ch->len);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300474
Adrian Hunterde097572009-03-20 11:09:04 +0100475 /* Check for empty space after the corrupt node's common header */
Artem Bityutskiy2765df72011-02-02 09:22:54 +0200476 skip = ALIGN(offs + UBIFS_CH_SZ, c->max_write_size) - offs;
Adrian Hunterde097572009-03-20 11:09:04 +0100477 if (is_empty(buf + skip, len - skip))
478 return 1;
479 /*
480 * The area after the common header size is not empty, so the common
481 * header must be intact. Check it.
482 */
483 if (ubifs_check_node(c, buf, lnum, offs, 1, 0) != -EUCLEAN) {
484 dbg_rcvry("unexpected bad common header at %d:%d", lnum, offs);
485 return 0;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300486 }
Adrian Hunterde097572009-03-20 11:09:04 +0100487 /* Now we know the corrupt node's length we can skip over it */
Artem Bityutskiy2765df72011-02-02 09:22:54 +0200488 skip = ALIGN(offs + dlen, c->max_write_size) - offs;
Adrian Hunterde097572009-03-20 11:09:04 +0100489 /* After which there should be empty space */
490 if (is_empty(buf + skip, len - skip))
491 return 1;
492 dbg_rcvry("unexpected data at %d:%d", lnum, offs + skip);
493 return 0;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300494}
495
496/**
497 * fix_unclean_leb - fix an unclean LEB.
498 * @c: UBIFS file-system description object
499 * @sleb: scanned LEB information
500 * @start: offset where scan started
501 */
502static int fix_unclean_leb(struct ubifs_info *c, struct ubifs_scan_leb *sleb,
503 int start)
504{
505 int lnum = sleb->lnum, endpt = start;
506
507 /* Get the end offset of the last node we are keeping */
508 if (!list_empty(&sleb->nodes)) {
509 struct ubifs_scan_node *snod;
510
511 snod = list_entry(sleb->nodes.prev,
512 struct ubifs_scan_node, list);
513 endpt = snod->offs + snod->len;
514 }
515
Artem Bityutskiy2ef13292010-09-19 18:34:26 +0300516 if (c->ro_mount && !c->remounting_rw) {
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300517 /* Add to recovery list */
518 struct ubifs_unclean_leb *ucleb;
519
520 dbg_rcvry("need to fix LEB %d start %d endpt %d",
521 lnum, start, sleb->endpt);
522 ucleb = kzalloc(sizeof(struct ubifs_unclean_leb), GFP_NOFS);
523 if (!ucleb)
524 return -ENOMEM;
525 ucleb->lnum = lnum;
526 ucleb->endpt = endpt;
527 list_add_tail(&ucleb->list, &c->unclean_leb_list);
528 } else {
529 /* Write the fixed LEB back to flash */
530 int err;
531
532 dbg_rcvry("fixing LEB %d start %d endpt %d",
533 lnum, start, sleb->endpt);
534 if (endpt == 0) {
535 err = ubifs_leb_unmap(c, lnum);
536 if (err)
537 return err;
538 } else {
539 int len = ALIGN(endpt, c->min_io_size);
540
541 if (start) {
542 err = ubi_read(c->ubi, lnum, sleb->buf, 0,
543 start);
544 if (err)
545 return err;
546 }
547 /* Pad to min_io_size */
548 if (len > endpt) {
549 int pad_len = len - ALIGN(endpt, 8);
550
551 if (pad_len > 0) {
552 void *buf = sleb->buf + len - pad_len;
553
554 ubifs_pad(c, buf, pad_len);
555 }
556 }
557 err = ubi_leb_change(c->ubi, lnum, sleb->buf, len,
558 UBI_UNKNOWN);
559 if (err)
560 return err;
561 }
562 }
563 return 0;
564}
565
566/**
567 * drop_incomplete_group - drop nodes from an incomplete group.
568 * @sleb: scanned LEB information
569 * @offs: offset of dropped nodes is returned here
570 *
571 * This function returns %1 if nodes are dropped and %0 otherwise.
572 */
573static int drop_incomplete_group(struct ubifs_scan_leb *sleb, int *offs)
574{
575 int dropped = 0;
576
577 while (!list_empty(&sleb->nodes)) {
578 struct ubifs_scan_node *snod;
579 struct ubifs_ch *ch;
580
581 snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node,
582 list);
583 ch = snod->node;
584 if (ch->group_type != UBIFS_IN_NODE_GROUP)
585 return dropped;
586 dbg_rcvry("dropping node at %d:%d", sleb->lnum, snod->offs);
587 *offs = snod->offs;
588 list_del(&snod->list);
589 kfree(snod);
590 sleb->nodes_cnt -= 1;
591 dropped = 1;
592 }
593 return dropped;
594}
595
596/**
597 * ubifs_recover_leb - scan and recover a LEB.
598 * @c: UBIFS file-system description object
599 * @lnum: LEB number
600 * @offs: offset
601 * @sbuf: LEB-sized buffer to use
602 * @grouped: nodes may be grouped for recovery
603 *
604 * This function does a scan of a LEB, but caters for errors that might have
605 * been caused by the unclean unmount from which we are attempting to recover.
Artem Bityutskiyed43f2f2009-06-29 17:59:23 +0300606 * Returns %0 in case of success, %-EUCLEAN if an unrecoverable corruption is
607 * found, and a negative error code in case of failure.
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300608 */
609struct ubifs_scan_leb *ubifs_recover_leb(struct ubifs_info *c, int lnum,
610 int offs, void *sbuf, int grouped)
611{
Artem Bityutskiy7c47bfd2011-05-16 13:44:48 +0300612 int ret = 0, err, len = c->leb_size - offs;
Artem Bityutskiy61799202011-05-16 13:41:55 +0300613 int start = offs;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300614 struct ubifs_scan_leb *sleb;
615 void *buf = sbuf + offs;
616
617 dbg_rcvry("%d:%d", lnum, offs);
618
619 sleb = ubifs_start_scan(c, lnum, offs, sbuf);
620 if (IS_ERR(sleb))
621 return sleb;
622
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300623 while (len >= 8) {
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300624 dbg_scan("look at LEB %d:%d (%d bytes left)",
625 lnum, offs, len);
626
627 cond_resched();
628
629 /*
630 * Scan quietly until there is an error from which we cannot
631 * recover
632 */
Artem Bityutskiy61799202011-05-16 13:41:55 +0300633 ret = ubifs_scan_a_node(c, buf, len, lnum, offs, 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300634 if (ret == SCANNED_A_NODE) {
635 /* A valid node, and not a padding node */
636 struct ubifs_ch *ch = buf;
637 int node_len;
638
639 err = ubifs_add_snod(c, sleb, buf, offs);
640 if (err)
641 goto error;
642 node_len = ALIGN(le32_to_cpu(ch->len), 8);
643 offs += node_len;
644 buf += node_len;
645 len -= node_len;
Artem Bityutskiy61799202011-05-16 13:41:55 +0300646 } else if (ret > 0) {
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300647 /* Padding bytes or a valid padding node */
648 offs += ret;
649 buf += ret;
650 len -= ret;
Artem Bityutskiy61799202011-05-16 13:41:55 +0300651 } else if (ret == SCANNED_EMPTY_SPACE ||
652 ret == SCANNED_GARBAGE ||
653 ret == SCANNED_A_BAD_PAD_NODE ||
654 ret == SCANNED_A_CORRUPT_NODE) {
655 dbg_rcvry("found corruption - %d", ret);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300656 break;
Artem Bityutskiy61799202011-05-16 13:41:55 +0300657 } else {
658 dbg_err("unexpected return value %d", ret);
Artem Bityutskiyed43f2f2009-06-29 17:59:23 +0300659 err = -EINVAL;
660 goto error;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300661 }
662 }
663
Artem Bityutskiy61799202011-05-16 13:41:55 +0300664 if (ret == SCANNED_GARBAGE || ret == SCANNED_A_BAD_PAD_NODE) {
Artem Bityutskiy7c47bfd2011-05-16 13:44:48 +0300665 if (is_last_write(c, buf, offs))
Artem Bityutskiy61799202011-05-16 13:41:55 +0300666 clean_buf(c, &buf, lnum, &offs, &len);
Artem Bityutskiy7c47bfd2011-05-16 13:44:48 +0300667 else
Artem Bityutskiy61799202011-05-16 13:41:55 +0300668 goto corrupted_rescan;
669 } else if (ret == SCANNED_A_CORRUPT_NODE) {
Artem Bityutskiy7c47bfd2011-05-16 13:44:48 +0300670 if (no_more_nodes(c, buf, len, lnum, offs))
Artem Bityutskiy61799202011-05-16 13:41:55 +0300671 clean_buf(c, &buf, lnum, &offs, &len);
Artem Bityutskiy7c47bfd2011-05-16 13:44:48 +0300672 else
Artem Bityutskiy61799202011-05-16 13:41:55 +0300673 goto corrupted_rescan;
674 } else if (!is_empty(buf, len)) {
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300675 if (is_last_write(c, buf, offs)) {
676 clean_buf(c, &buf, lnum, &offs, &len);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300677 } else {
Artem Bityutskiy06112542009-06-29 19:27:14 +0300678 int corruption = first_non_ff(buf, len);
679
Artem Bityutskiybe7b42a2011-02-06 16:41:06 +0200680 /*
681 * See header comment for this file for more
682 * explanations about the reasons we have this check.
683 */
Artem Bityutskiy06112542009-06-29 19:27:14 +0300684 ubifs_err("corrupt empty space LEB %d:%d, corruption "
685 "starts at %d", lnum, offs, corruption);
686 /* Make sure we dump interesting non-0xFF data */
Artem Bityutskiy10ac2792011-02-08 17:21:11 +0200687 offs += corruption;
Artem Bityutskiy06112542009-06-29 19:27:14 +0300688 buf += corruption;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300689 goto corrupted;
690 }
691 }
692
693 /* Drop nodes from incomplete group */
694 if (grouped && drop_incomplete_group(sleb, &offs)) {
695 buf = sbuf + offs;
696 len = c->leb_size - offs;
697 clean_buf(c, &buf, lnum, &offs, &len);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300698 }
699
Artem Bityutskiy7c47bfd2011-05-16 13:44:48 +0300700 if (offs % c->min_io_size)
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300701 clean_buf(c, &buf, lnum, &offs, &len);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300702
703 ubifs_end_scan(c, sleb, lnum, offs);
704
Artem Bityutskiy7c47bfd2011-05-16 13:44:48 +0300705 err = fix_unclean_leb(c, sleb, start);
706 if (err)
707 goto error;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300708
709 return sleb;
710
Artem Bityutskiy61799202011-05-16 13:41:55 +0300711corrupted_rescan:
712 /* Re-scan the corrupted data with verbose messages */
713 dbg_err("corruptio %d", ret);
714 ubifs_scan_a_node(c, buf, len, lnum, offs, 1);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300715corrupted:
716 ubifs_scanned_corruption(c, lnum, offs, buf);
717 err = -EUCLEAN;
718error:
719 ubifs_err("LEB %d scanning failed", lnum);
720 ubifs_scan_destroy(sleb);
721 return ERR_PTR(err);
722}
723
724/**
725 * get_cs_sqnum - get commit start sequence number.
726 * @c: UBIFS file-system description object
727 * @lnum: LEB number of commit start node
728 * @offs: offset of commit start node
729 * @cs_sqnum: commit start sequence number is returned here
730 *
731 * This function returns %0 on success and a negative error code on failure.
732 */
733static int get_cs_sqnum(struct ubifs_info *c, int lnum, int offs,
734 unsigned long long *cs_sqnum)
735{
736 struct ubifs_cs_node *cs_node = NULL;
737 int err, ret;
738
739 dbg_rcvry("at %d:%d", lnum, offs);
740 cs_node = kmalloc(UBIFS_CS_NODE_SZ, GFP_KERNEL);
741 if (!cs_node)
742 return -ENOMEM;
743 if (c->leb_size - offs < UBIFS_CS_NODE_SZ)
744 goto out_err;
745 err = ubi_read(c->ubi, lnum, (void *)cs_node, offs, UBIFS_CS_NODE_SZ);
746 if (err && err != -EBADMSG)
747 goto out_free;
748 ret = ubifs_scan_a_node(c, cs_node, UBIFS_CS_NODE_SZ, lnum, offs, 0);
749 if (ret != SCANNED_A_NODE) {
750 dbg_err("Not a valid node");
751 goto out_err;
752 }
753 if (cs_node->ch.node_type != UBIFS_CS_NODE) {
754 dbg_err("Node a CS node, type is %d", cs_node->ch.node_type);
755 goto out_err;
756 }
757 if (le64_to_cpu(cs_node->cmt_no) != c->cmt_no) {
758 dbg_err("CS node cmt_no %llu != current cmt_no %llu",
759 (unsigned long long)le64_to_cpu(cs_node->cmt_no),
760 c->cmt_no);
761 goto out_err;
762 }
763 *cs_sqnum = le64_to_cpu(cs_node->ch.sqnum);
764 dbg_rcvry("commit start sqnum %llu", *cs_sqnum);
765 kfree(cs_node);
766 return 0;
767
768out_err:
769 err = -EINVAL;
770out_free:
771 ubifs_err("failed to get CS sqnum");
772 kfree(cs_node);
773 return err;
774}
775
776/**
777 * ubifs_recover_log_leb - scan and recover a log LEB.
778 * @c: UBIFS file-system description object
779 * @lnum: LEB number
780 * @offs: offset
781 * @sbuf: LEB-sized buffer to use
782 *
783 * This function does a scan of a LEB, but caters for errors that might have
Artem Bityutskiy7d08ae32010-10-17 15:50:19 +0300784 * been caused by unclean reboots from which we are attempting to recover
785 * (assume that only the last log LEB can be corrupted by an unclean reboot).
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300786 *
787 * This function returns %0 on success and a negative error code on failure.
788 */
789struct ubifs_scan_leb *ubifs_recover_log_leb(struct ubifs_info *c, int lnum,
790 int offs, void *sbuf)
791{
792 struct ubifs_scan_leb *sleb;
793 int next_lnum;
794
795 dbg_rcvry("LEB %d", lnum);
796 next_lnum = lnum + 1;
797 if (next_lnum >= UBIFS_LOG_LNUM + c->log_lebs)
798 next_lnum = UBIFS_LOG_LNUM;
799 if (next_lnum != c->ltail_lnum) {
800 /*
801 * We can only recover at the end of the log, so check that the
802 * next log LEB is empty or out of date.
803 */
Artem Bityutskiy348709b2009-08-25 15:00:55 +0300804 sleb = ubifs_scan(c, next_lnum, 0, sbuf, 0);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300805 if (IS_ERR(sleb))
806 return sleb;
807 if (sleb->nodes_cnt) {
808 struct ubifs_scan_node *snod;
809 unsigned long long cs_sqnum = c->cs_sqnum;
810
811 snod = list_entry(sleb->nodes.next,
812 struct ubifs_scan_node, list);
813 if (cs_sqnum == 0) {
814 int err;
815
816 err = get_cs_sqnum(c, lnum, offs, &cs_sqnum);
817 if (err) {
818 ubifs_scan_destroy(sleb);
819 return ERR_PTR(err);
820 }
821 }
822 if (snod->sqnum > cs_sqnum) {
823 ubifs_err("unrecoverable log corruption "
824 "in LEB %d", lnum);
825 ubifs_scan_destroy(sleb);
826 return ERR_PTR(-EUCLEAN);
827 }
828 }
829 ubifs_scan_destroy(sleb);
830 }
831 return ubifs_recover_leb(c, lnum, offs, sbuf, 0);
832}
833
834/**
835 * recover_head - recover a head.
836 * @c: UBIFS file-system description object
837 * @lnum: LEB number of head to recover
838 * @offs: offset of head to recover
839 * @sbuf: LEB-sized buffer to use
840 *
841 * This function ensures that there is no data on the flash at a head location.
842 *
843 * This function returns %0 on success and a negative error code on failure.
844 */
845static int recover_head(const struct ubifs_info *c, int lnum, int offs,
846 void *sbuf)
847{
Artem Bityutskiy2765df72011-02-02 09:22:54 +0200848 int len = c->max_write_size, err;
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300849
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300850 if (offs + len > c->leb_size)
851 len = c->leb_size - offs;
852
853 if (!len)
854 return 0;
855
856 /* Read at the head location and check it is empty flash */
857 err = ubi_read(c->ubi, lnum, sbuf, offs, len);
Artem Bityutskiy431102f2009-06-29 18:58:34 +0300858 if (err || !is_empty(sbuf, len)) {
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300859 dbg_rcvry("cleaning head at %d:%d", lnum, offs);
860 if (offs == 0)
861 return ubifs_leb_unmap(c, lnum);
862 err = ubi_read(c->ubi, lnum, sbuf, 0, offs);
863 if (err)
864 return err;
865 return ubi_leb_change(c->ubi, lnum, sbuf, offs, UBI_UNKNOWN);
866 }
867
868 return 0;
869}
870
871/**
872 * ubifs_recover_inl_heads - recover index and LPT heads.
873 * @c: UBIFS file-system description object
874 * @sbuf: LEB-sized buffer to use
875 *
876 * This function ensures that there is no data on the flash at the index and
877 * LPT head locations.
878 *
879 * This deals with the recovery of a half-completed journal commit. UBIFS is
880 * careful never to overwrite the last version of the index or the LPT. Because
881 * the index and LPT are wandering trees, data from a half-completed commit will
882 * not be referenced anywhere in UBIFS. The data will be either in LEBs that are
883 * assumed to be empty and will be unmapped anyway before use, or in the index
884 * and LPT heads.
885 *
886 * This function returns %0 on success and a negative error code on failure.
887 */
888int ubifs_recover_inl_heads(const struct ubifs_info *c, void *sbuf)
889{
890 int err;
891
Artem Bityutskiy2ef13292010-09-19 18:34:26 +0300892 ubifs_assert(!c->ro_mount || c->remounting_rw);
Artem Bityutskiy1e517642008-07-14 19:08:37 +0300893
894 dbg_rcvry("checking index head at %d:%d", c->ihead_lnum, c->ihead_offs);
895 err = recover_head(c, c->ihead_lnum, c->ihead_offs, sbuf);
896 if (err)
897 return err;
898
899 dbg_rcvry("checking LPT head at %d:%d", c->nhead_lnum, c->nhead_offs);
900 err = recover_head(c, c->nhead_lnum, c->nhead_offs, sbuf);
901 if (err)
902 return err;
903
904 return 0;
905}
906
907/**
908 * clean_an_unclean_leb - read and write a LEB to remove corruption.
909 * @c: UBIFS file-system description object
910 * @ucleb: unclean LEB information
911 * @sbuf: LEB-sized buffer to use
912 *
913 * This function reads a LEB up to a point pre-determined by the mount recovery,
914 * checks the nodes, and writes the result back to the flash, thereby cleaning
915 * off any following corruption, or non-fatal ECC errors.
916 *
917 * This function returns %0 on success and a negative error code on failure.
918 */
919static int clean_an_unclean_leb(const struct ubifs_info *c,
920 struct ubifs_unclean_leb *ucleb, void *sbuf)
921{
922 int err, lnum = ucleb->lnum, offs = 0, len = ucleb->endpt, quiet = 1;
923 void *buf = sbuf;
924
925 dbg_rcvry("LEB %d len %d", lnum, len);
926
927 if (len == 0) {
928 /* Nothing to read, just unmap it */
929 err = ubifs_leb_unmap(c, lnum);
930 if (err)
931 return err;
932 return 0;
933 }
934
935 err = ubi_read(c->ubi, lnum, buf, offs, len);
936 if (err && err != -EBADMSG)
937 return err;
938
939 while (len >= 8) {
940 int ret;
941
942 cond_resched();
943
944 /* Scan quietly until there is an error */
945 ret = ubifs_scan_a_node(c, buf, len, lnum, offs, quiet);
946
947 if (ret == SCANNED_A_NODE) {
948 /* A valid node, and not a padding node */
949 struct ubifs_ch *ch = buf;
950 int node_len;
951
952 node_len = ALIGN(le32_to_cpu(ch->len), 8);
953 offs += node_len;
954 buf += node_len;
955 len -= node_len;
956 continue;
957 }
958
959 if (ret > 0) {
960 /* Padding bytes or a valid padding node */
961 offs += ret;
962 buf += ret;
963 len -= ret;
964 continue;
965 }
966
967 if (ret == SCANNED_EMPTY_SPACE) {
968 ubifs_err("unexpected empty space at %d:%d",
969 lnum, offs);
970 return -EUCLEAN;
971 }
972
973 if (quiet) {
974 /* Redo the last scan but noisily */
975 quiet = 0;
976 continue;
977 }
978
979 ubifs_scanned_corruption(c, lnum, offs, buf);
980 return -EUCLEAN;
981 }
982
983 /* Pad to min_io_size */
984 len = ALIGN(ucleb->endpt, c->min_io_size);
985 if (len > ucleb->endpt) {
986 int pad_len = len - ALIGN(ucleb->endpt, 8);
987
988 if (pad_len > 0) {
989 buf = c->sbuf + len - pad_len;
990 ubifs_pad(c, buf, pad_len);
991 }
992 }
993
994 /* Write back the LEB atomically */
995 err = ubi_leb_change(c->ubi, lnum, sbuf, len, UBI_UNKNOWN);
996 if (err)
997 return err;
998
999 dbg_rcvry("cleaned LEB %d", lnum);
1000
1001 return 0;
1002}
1003
1004/**
1005 * ubifs_clean_lebs - clean LEBs recovered during read-only mount.
1006 * @c: UBIFS file-system description object
1007 * @sbuf: LEB-sized buffer to use
1008 *
1009 * This function cleans a LEB identified during recovery that needs to be
1010 * written but was not because UBIFS was mounted read-only. This happens when
1011 * remounting to read-write mode.
1012 *
1013 * This function returns %0 on success and a negative error code on failure.
1014 */
1015int ubifs_clean_lebs(const struct ubifs_info *c, void *sbuf)
1016{
1017 dbg_rcvry("recovery");
1018 while (!list_empty(&c->unclean_leb_list)) {
1019 struct ubifs_unclean_leb *ucleb;
1020 int err;
1021
1022 ucleb = list_entry(c->unclean_leb_list.next,
1023 struct ubifs_unclean_leb, list);
1024 err = clean_an_unclean_leb(c, ucleb, sbuf);
1025 if (err)
1026 return err;
1027 list_del(&ucleb->list);
1028 kfree(ucleb);
1029 }
1030 return 0;
1031}
1032
1033/**
Artem Bityutskiy44744212011-04-27 14:52:35 +03001034 * grab_empty_leb - grab an empty LEB to use as GC LEB and run commit.
1035 * @c: UBIFS file-system description object
1036 *
1037 * This is a helper function for 'ubifs_rcvry_gc_commit()' which grabs an empty
1038 * LEB to be used as GC LEB (@c->gc_lnum), and then runs the commit. Returns
1039 * zero in case of success and a negative error code in case of failure.
1040 */
1041static int grab_empty_leb(struct ubifs_info *c)
1042{
1043 int lnum, err;
1044
1045 /*
1046 * Note, it is very important to first search for an empty LEB and then
1047 * run the commit, not vice-versa. The reason is that there might be
1048 * only one empty LEB at the moment, the one which has been the
1049 * @c->gc_lnum just before the power cut happened. During the regular
1050 * UBIFS operation (not now) @c->gc_lnum is marked as "taken", so no
1051 * one but GC can grab it. But at this moment this single empty LEB is
1052 * not marked as taken, so if we run commit - what happens? Right, the
1053 * commit will grab it and write the index there. Remember that the
1054 * index always expands as long as there is free space, and it only
1055 * starts consolidating when we run out of space.
1056 *
1057 * IOW, if we run commit now, we might not be able to find a free LEB
1058 * after this.
1059 */
1060 lnum = ubifs_find_free_leb_for_idx(c);
1061 if (lnum < 0) {
1062 dbg_err("could not find an empty LEB");
1063 dbg_dump_lprops(c);
1064 dbg_dump_budg(c, &c->bi);
1065 return lnum;
1066 }
1067
1068 /* Reset the index flag */
1069 err = ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0,
1070 LPROPS_INDEX, 0);
1071 if (err)
1072 return err;
1073
1074 c->gc_lnum = lnum;
1075 dbg_rcvry("found empty LEB %d, run commit", lnum);
1076
1077 return ubifs_run_commit(c);
1078}
1079
1080/**
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001081 * ubifs_rcvry_gc_commit - recover the GC LEB number and run the commit.
1082 * @c: UBIFS file-system description object
1083 *
1084 * Out-of-place garbage collection requires always one empty LEB with which to
1085 * start garbage collection. The LEB number is recorded in c->gc_lnum and is
1086 * written to the master node on unmounting. In the case of an unclean unmount
1087 * the value of gc_lnum recorded in the master node is out of date and cannot
1088 * be used. Instead, recovery must allocate an empty LEB for this purpose.
1089 * However, there may not be enough empty space, in which case it must be
1090 * possible to GC the dirtiest LEB into the GC head LEB.
1091 *
1092 * This function also runs the commit which causes the TNC updates from
1093 * size-recovery and orphans to be written to the flash. That is important to
1094 * ensure correct replay order for subsequent mounts.
1095 *
1096 * This function returns %0 on success and a negative error code on failure.
1097 */
1098int ubifs_rcvry_gc_commit(struct ubifs_info *c)
1099{
1100 struct ubifs_wbuf *wbuf = &c->jheads[GCHD].wbuf;
1101 struct ubifs_lprops lp;
Artem Bityutskiyfe79c052011-04-29 16:35:46 +03001102 int err;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001103
Artem Bityutskiyc839e292011-05-13 12:26:54 +03001104 dbg_rcvry("GC head LEB %d, offs %d", wbuf->lnum, wbuf->offs);
1105
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001106 c->gc_lnum = -1;
Artem Bityutskiyc839e292011-05-13 12:26:54 +03001107 if (wbuf->lnum == -1 || wbuf->offs == c->leb_size)
Artem Bityutskiy44744212011-04-27 14:52:35 +03001108 return grab_empty_leb(c);
Artem Bityutskiyfe79c052011-04-29 16:35:46 +03001109
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001110 err = ubifs_find_dirty_leb(c, &lp, wbuf->offs, 2);
1111 if (err) {
Artem Bityutskiyfe79c052011-04-29 16:35:46 +03001112 if (err != -ENOSPC)
1113 return err;
1114
1115 dbg_rcvry("could not find a dirty LEB");
1116 return grab_empty_leb(c);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001117 }
Artem Bityutskiy2405f592011-04-26 09:49:32 +03001118
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001119 ubifs_assert(!(lp.flags & LPROPS_INDEX));
Artem Bityutskiybcdca3e2011-04-26 10:07:50 +03001120 ubifs_assert(lp.free + lp.dirty >= wbuf->offs);
Artem Bityutskiy2405f592011-04-26 09:49:32 +03001121
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001122 /*
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001123 * We run the commit before garbage collection otherwise subsequent
1124 * mounts will see the GC and orphan deletion in a different order.
1125 */
1126 dbg_rcvry("committing");
1127 err = ubifs_run_commit(c);
1128 if (err)
1129 return err;
Artem Bityutskiyfe79c052011-04-29 16:35:46 +03001130
1131 dbg_rcvry("GC'ing LEB %d", lp.lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001132 mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
1133 err = ubifs_garbage_collect_leb(c, &lp);
1134 if (err >= 0) {
1135 int err2 = ubifs_wbuf_sync_nolock(wbuf);
1136
1137 if (err2)
1138 err = err2;
1139 }
1140 mutex_unlock(&wbuf->io_mutex);
1141 if (err < 0) {
1142 dbg_err("GC failed, error %d", err);
1143 if (err == -EAGAIN)
1144 err = -EINVAL;
1145 return err;
1146 }
Artem Bityutskiyfe79c052011-04-29 16:35:46 +03001147
1148 ubifs_assert(err == LEB_RETAINED);
1149 if (err != LEB_RETAINED)
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001150 return -EINVAL;
Artem Bityutskiyfe79c052011-04-29 16:35:46 +03001151
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001152 err = ubifs_leb_unmap(c, c->gc_lnum);
1153 if (err)
1154 return err;
Artem Bityutskiyfe79c052011-04-29 16:35:46 +03001155
1156 dbg_rcvry("allocated LEB %d for GC", lp.lnum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001157 return 0;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001158}
1159
1160/**
1161 * struct size_entry - inode size information for recovery.
1162 * @rb: link in the RB-tree of sizes
1163 * @inum: inode number
1164 * @i_size: size on inode
1165 * @d_size: maximum size based on data nodes
1166 * @exists: indicates whether the inode exists
1167 * @inode: inode if pinned in memory awaiting rw mode to fix it
1168 */
1169struct size_entry {
1170 struct rb_node rb;
1171 ino_t inum;
1172 loff_t i_size;
1173 loff_t d_size;
1174 int exists;
1175 struct inode *inode;
1176};
1177
1178/**
1179 * add_ino - add an entry to the size tree.
1180 * @c: UBIFS file-system description object
1181 * @inum: inode number
1182 * @i_size: size on inode
1183 * @d_size: maximum size based on data nodes
1184 * @exists: indicates whether the inode exists
1185 */
1186static int add_ino(struct ubifs_info *c, ino_t inum, loff_t i_size,
1187 loff_t d_size, int exists)
1188{
1189 struct rb_node **p = &c->size_tree.rb_node, *parent = NULL;
1190 struct size_entry *e;
1191
1192 while (*p) {
1193 parent = *p;
1194 e = rb_entry(parent, struct size_entry, rb);
1195 if (inum < e->inum)
1196 p = &(*p)->rb_left;
1197 else
1198 p = &(*p)->rb_right;
1199 }
1200
1201 e = kzalloc(sizeof(struct size_entry), GFP_KERNEL);
1202 if (!e)
1203 return -ENOMEM;
1204
1205 e->inum = inum;
1206 e->i_size = i_size;
1207 e->d_size = d_size;
1208 e->exists = exists;
1209
1210 rb_link_node(&e->rb, parent, p);
1211 rb_insert_color(&e->rb, &c->size_tree);
1212
1213 return 0;
1214}
1215
1216/**
1217 * find_ino - find an entry on the size tree.
1218 * @c: UBIFS file-system description object
1219 * @inum: inode number
1220 */
1221static struct size_entry *find_ino(struct ubifs_info *c, ino_t inum)
1222{
1223 struct rb_node *p = c->size_tree.rb_node;
1224 struct size_entry *e;
1225
1226 while (p) {
1227 e = rb_entry(p, struct size_entry, rb);
1228 if (inum < e->inum)
1229 p = p->rb_left;
1230 else if (inum > e->inum)
1231 p = p->rb_right;
1232 else
1233 return e;
1234 }
1235 return NULL;
1236}
1237
1238/**
1239 * remove_ino - remove an entry from the size tree.
1240 * @c: UBIFS file-system description object
1241 * @inum: inode number
1242 */
1243static void remove_ino(struct ubifs_info *c, ino_t inum)
1244{
1245 struct size_entry *e = find_ino(c, inum);
1246
1247 if (!e)
1248 return;
1249 rb_erase(&e->rb, &c->size_tree);
1250 kfree(e);
1251}
1252
1253/**
1254 * ubifs_destroy_size_tree - free resources related to the size tree.
1255 * @c: UBIFS file-system description object
1256 */
1257void ubifs_destroy_size_tree(struct ubifs_info *c)
1258{
1259 struct rb_node *this = c->size_tree.rb_node;
1260 struct size_entry *e;
1261
1262 while (this) {
1263 if (this->rb_left) {
1264 this = this->rb_left;
1265 continue;
1266 } else if (this->rb_right) {
1267 this = this->rb_right;
1268 continue;
1269 }
1270 e = rb_entry(this, struct size_entry, rb);
1271 if (e->inode)
1272 iput(e->inode);
1273 this = rb_parent(this);
1274 if (this) {
1275 if (this->rb_left == &e->rb)
1276 this->rb_left = NULL;
1277 else
1278 this->rb_right = NULL;
1279 }
1280 kfree(e);
1281 }
1282 c->size_tree = RB_ROOT;
1283}
1284
1285/**
1286 * ubifs_recover_size_accum - accumulate inode sizes for recovery.
1287 * @c: UBIFS file-system description object
1288 * @key: node key
1289 * @deletion: node is for a deletion
1290 * @new_size: inode size
1291 *
1292 * This function has two purposes:
1293 * 1) to ensure there are no data nodes that fall outside the inode size
1294 * 2) to ensure there are no data nodes for inodes that do not exist
1295 * To accomplish those purposes, a rb-tree is constructed containing an entry
1296 * for each inode number in the journal that has not been deleted, and recording
1297 * the size from the inode node, the maximum size of any data node (also altered
1298 * by truncations) and a flag indicating a inode number for which no inode node
1299 * was present in the journal.
1300 *
1301 * Note that there is still the possibility that there are data nodes that have
1302 * been committed that are beyond the inode size, however the only way to find
1303 * them would be to scan the entire index. Alternatively, some provision could
1304 * be made to record the size of inodes at the start of commit, which would seem
1305 * very cumbersome for a scenario that is quite unlikely and the only negative
1306 * consequence of which is wasted space.
1307 *
1308 * This functions returns %0 on success and a negative error code on failure.
1309 */
1310int ubifs_recover_size_accum(struct ubifs_info *c, union ubifs_key *key,
1311 int deletion, loff_t new_size)
1312{
1313 ino_t inum = key_inum(c, key);
1314 struct size_entry *e;
1315 int err;
1316
1317 switch (key_type(c, key)) {
1318 case UBIFS_INO_KEY:
1319 if (deletion)
1320 remove_ino(c, inum);
1321 else {
1322 e = find_ino(c, inum);
1323 if (e) {
1324 e->i_size = new_size;
1325 e->exists = 1;
1326 } else {
1327 err = add_ino(c, inum, new_size, 0, 1);
1328 if (err)
1329 return err;
1330 }
1331 }
1332 break;
1333 case UBIFS_DATA_KEY:
1334 e = find_ino(c, inum);
1335 if (e) {
1336 if (new_size > e->d_size)
1337 e->d_size = new_size;
1338 } else {
1339 err = add_ino(c, inum, 0, new_size, 0);
1340 if (err)
1341 return err;
1342 }
1343 break;
1344 case UBIFS_TRUN_KEY:
1345 e = find_ino(c, inum);
1346 if (e)
1347 e->d_size = new_size;
1348 break;
1349 }
1350 return 0;
1351}
1352
1353/**
1354 * fix_size_in_place - fix inode size in place on flash.
1355 * @c: UBIFS file-system description object
1356 * @e: inode size information for recovery
1357 */
1358static int fix_size_in_place(struct ubifs_info *c, struct size_entry *e)
1359{
1360 struct ubifs_ino_node *ino = c->sbuf;
1361 unsigned char *p;
1362 union ubifs_key key;
1363 int err, lnum, offs, len;
1364 loff_t i_size;
1365 uint32_t crc;
1366
1367 /* Locate the inode node LEB number and offset */
1368 ino_key_init(c, &key, e->inum);
1369 err = ubifs_tnc_locate(c, &key, ino, &lnum, &offs);
1370 if (err)
1371 goto out;
1372 /*
1373 * If the size recorded on the inode node is greater than the size that
1374 * was calculated from nodes in the journal then don't change the inode.
1375 */
1376 i_size = le64_to_cpu(ino->size);
1377 if (i_size >= e->d_size)
1378 return 0;
1379 /* Read the LEB */
1380 err = ubi_read(c->ubi, lnum, c->sbuf, 0, c->leb_size);
1381 if (err)
1382 goto out;
1383 /* Change the size field and recalculate the CRC */
1384 ino = c->sbuf + offs;
1385 ino->size = cpu_to_le64(e->d_size);
1386 len = le32_to_cpu(ino->ch.len);
1387 crc = crc32(UBIFS_CRC32_INIT, (void *)ino + 8, len - 8);
1388 ino->ch.crc = cpu_to_le32(crc);
1389 /* Work out where data in the LEB ends and free space begins */
1390 p = c->sbuf;
1391 len = c->leb_size - 1;
1392 while (p[len] == 0xff)
1393 len -= 1;
1394 len = ALIGN(len + 1, c->min_io_size);
1395 /* Atomically write the fixed LEB back again */
1396 err = ubi_leb_change(c->ubi, lnum, c->sbuf, len, UBI_UNKNOWN);
1397 if (err)
1398 goto out;
Artem Bityutskiy69f8a752011-05-02 21:51:17 +03001399 dbg_rcvry("inode %lu at %d:%d size %lld -> %lld",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02001400 (unsigned long)e->inum, lnum, offs, i_size, e->d_size);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001401 return 0;
1402
1403out:
1404 ubifs_warn("inode %lu failed to fix size %lld -> %lld error %d",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02001405 (unsigned long)e->inum, e->i_size, e->d_size, err);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001406 return err;
1407}
1408
1409/**
1410 * ubifs_recover_size - recover inode size.
1411 * @c: UBIFS file-system description object
1412 *
1413 * This function attempts to fix inode size discrepancies identified by the
1414 * 'ubifs_recover_size_accum()' function.
1415 *
1416 * This functions returns %0 on success and a negative error code on failure.
1417 */
1418int ubifs_recover_size(struct ubifs_info *c)
1419{
1420 struct rb_node *this = rb_first(&c->size_tree);
1421
1422 while (this) {
1423 struct size_entry *e;
1424 int err;
1425
1426 e = rb_entry(this, struct size_entry, rb);
1427 if (!e->exists) {
1428 union ubifs_key key;
1429
1430 ino_key_init(c, &key, e->inum);
1431 err = ubifs_tnc_lookup(c, &key, c->sbuf);
1432 if (err && err != -ENOENT)
1433 return err;
1434 if (err == -ENOENT) {
1435 /* Remove data nodes that have no inode */
Artem Bityutskiye84461a2008-10-29 12:08:43 +02001436 dbg_rcvry("removing ino %lu",
1437 (unsigned long)e->inum);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001438 err = ubifs_tnc_remove_ino(c, e->inum);
1439 if (err)
1440 return err;
1441 } else {
1442 struct ubifs_ino_node *ino = c->sbuf;
1443
1444 e->exists = 1;
1445 e->i_size = le64_to_cpu(ino->size);
1446 }
1447 }
Artem Bityutskiy69f8a752011-05-02 21:51:17 +03001448
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001449 if (e->exists && e->i_size < e->d_size) {
Artem Bityutskiy69f8a752011-05-02 21:51:17 +03001450 if (c->ro_mount) {
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001451 /* Fix the inode size and pin it in memory */
1452 struct inode *inode;
Artem Bityutskiyc1f1f912011-05-05 14:16:32 +03001453 struct ubifs_inode *ui;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001454
Artem Bityutskiy69f8a752011-05-02 21:51:17 +03001455 ubifs_assert(!e->inode);
1456
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001457 inode = ubifs_iget(c->vfs_sb, e->inum);
1458 if (IS_ERR(inode))
1459 return PTR_ERR(inode);
Artem Bityutskiyc1f1f912011-05-05 14:16:32 +03001460
1461 ui = ubifs_inode(inode);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001462 if (inode->i_size < e->d_size) {
1463 dbg_rcvry("ino %lu size %lld -> %lld",
Artem Bityutskiye84461a2008-10-29 12:08:43 +02001464 (unsigned long)e->inum,
Artem Bityutskiy4c954522011-05-02 21:43:54 +03001465 inode->i_size, e->d_size);
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001466 inode->i_size = e->d_size;
Artem Bityutskiyc1f1f912011-05-05 14:16:32 +03001467 ui->ui_size = e->d_size;
1468 ui->synced_i_size = e->d_size;
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001469 e->inode = inode;
1470 this = rb_next(this);
1471 continue;
1472 }
1473 iput(inode);
1474 } else {
1475 /* Fix the size in place */
1476 err = fix_size_in_place(c, e);
1477 if (err)
1478 return err;
1479 if (e->inode)
1480 iput(e->inode);
1481 }
1482 }
Artem Bityutskiy69f8a752011-05-02 21:51:17 +03001483
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001484 this = rb_next(this);
1485 rb_erase(&e->rb, &c->size_tree);
1486 kfree(e);
1487 }
Artem Bityutskiy69f8a752011-05-02 21:51:17 +03001488
Artem Bityutskiy1e517642008-07-14 19:08:37 +03001489 return 0;
1490}