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