UBIFS: small amendments in the LEB scanning code
[safe/jmp/linux-2.6] / fs / ubifs / recovery.c
1 /*
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  */
44 static 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  */
72 static 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
151 out_err:
152         err = -EINVAL;
153 out_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  */
167 static 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;
171         __le32 save_flags;
172
173         dbg_rcvry("recovery");
174
175         save_flags = mst->flags;
176         mst->flags |= cpu_to_le32(UBIFS_MST_RCVRY);
177
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;
185 out:
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  */
199 int 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
296 out_err:
297         err = -EINVAL;
298 out_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  */
322 int 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
346  * for subsequent empty space starting from the next @c->min_io_size boundary.
347  */
348 static int is_last_write(const struct ubifs_info *c, void *buf, int offs)
349 {
350         int empty_offs, check_len;
351         uint8_t *p;
352
353         /*
354          * Round up to the next @c->min_io_size boundary i.e. @offs is in the
355          * 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;
360
361         for (; check_len > 0; check_len--)
362                 if (*p++ != 0xff)
363                         return 0;
364         return 1;
365 }
366
367 /**
368  * clean_buf - clean the data from an LEB sitting in a buffer.
369  * @c: UBIFS file-system description object
370  * @buf: buffer to clean
371  * @lnum: LEB number to clean
372  * @offs: offset from which to clean
373  * @len: length of buffer
374  *
375  * This function pads up to the next min_io_size boundary (if there is one) and
376  * sets empty space to all 0xff. @buf, @offs and @len are updated to the next
377  * @c->min_io_size boundary.
378  */
379 static void clean_buf(const struct ubifs_info *c, void **buf, int lnum,
380                       int *offs, int *len)
381 {
382         int empty_offs, pad_len;
383
384         lnum = lnum;
385         dbg_rcvry("cleaning corruption at %d:%d", lnum, *offs);
386
387         ubifs_assert(!(*offs & 7));
388         empty_offs = ALIGN(*offs, c->min_io_size);
389         pad_len = empty_offs - *offs;
390         ubifs_pad(c, *buf, pad_len);
391         *offs += pad_len;
392         *buf += pad_len;
393         *len -= pad_len;
394         memset(*buf, 0xff, c->leb_size - empty_offs);
395 }
396
397 /**
398  * no_more_nodes - determine if there are no more nodes in a buffer.
399  * @c: UBIFS file-system description object
400  * @buf: buffer to check
401  * @len: length of buffer
402  * @lnum: LEB number of the LEB from which @buf was read
403  * @offs: offset from which @buf was read
404  *
405  * This function ensures that the corrupted node at @offs is the last thing
406  * written to a LEB. This function returns %1 if more data is not found and
407  * %0 if more data is found.
408  */
409 static int no_more_nodes(const struct ubifs_info *c, void *buf, int len,
410                         int lnum, int offs)
411 {
412         struct ubifs_ch *ch = buf;
413         int skip, dlen = le32_to_cpu(ch->len);
414
415         /* Check for empty space after the corrupt node's common header */
416         skip = ALIGN(offs + UBIFS_CH_SZ, c->min_io_size) - offs;
417         if (is_empty(buf + skip, len - skip))
418                 return 1;
419         /*
420          * The area after the common header size is not empty, so the common
421          * header must be intact. Check it.
422          */
423         if (ubifs_check_node(c, buf, lnum, offs, 1, 0) != -EUCLEAN) {
424                 dbg_rcvry("unexpected bad common header at %d:%d", lnum, offs);
425                 return 0;
426         }
427         /* Now we know the corrupt node's length we can skip over it */
428         skip = ALIGN(offs + dlen, c->min_io_size) - offs;
429         /* After which there should be empty space */
430         if (is_empty(buf + skip, len - skip))
431                 return 1;
432         dbg_rcvry("unexpected data at %d:%d", lnum, offs + skip);
433         return 0;
434 }
435
436 /**
437  * fix_unclean_leb - fix an unclean LEB.
438  * @c: UBIFS file-system description object
439  * @sleb: scanned LEB information
440  * @start: offset where scan started
441  */
442 static int fix_unclean_leb(struct ubifs_info *c, struct ubifs_scan_leb *sleb,
443                            int start)
444 {
445         int lnum = sleb->lnum, endpt = start;
446
447         /* Get the end offset of the last node we are keeping */
448         if (!list_empty(&sleb->nodes)) {
449                 struct ubifs_scan_node *snod;
450
451                 snod = list_entry(sleb->nodes.prev,
452                                   struct ubifs_scan_node, list);
453                 endpt = snod->offs + snod->len;
454         }
455
456         if ((c->vfs_sb->s_flags & MS_RDONLY) && !c->remounting_rw) {
457                 /* Add to recovery list */
458                 struct ubifs_unclean_leb *ucleb;
459
460                 dbg_rcvry("need to fix LEB %d start %d endpt %d",
461                           lnum, start, sleb->endpt);
462                 ucleb = kzalloc(sizeof(struct ubifs_unclean_leb), GFP_NOFS);
463                 if (!ucleb)
464                         return -ENOMEM;
465                 ucleb->lnum = lnum;
466                 ucleb->endpt = endpt;
467                 list_add_tail(&ucleb->list, &c->unclean_leb_list);
468         } else {
469                 /* Write the fixed LEB back to flash */
470                 int err;
471
472                 dbg_rcvry("fixing LEB %d start %d endpt %d",
473                           lnum, start, sleb->endpt);
474                 if (endpt == 0) {
475                         err = ubifs_leb_unmap(c, lnum);
476                         if (err)
477                                 return err;
478                 } else {
479                         int len = ALIGN(endpt, c->min_io_size);
480
481                         if (start) {
482                                 err = ubi_read(c->ubi, lnum, sleb->buf, 0,
483                                                start);
484                                 if (err)
485                                         return err;
486                         }
487                         /* Pad to min_io_size */
488                         if (len > endpt) {
489                                 int pad_len = len - ALIGN(endpt, 8);
490
491                                 if (pad_len > 0) {
492                                         void *buf = sleb->buf + len - pad_len;
493
494                                         ubifs_pad(c, buf, pad_len);
495                                 }
496                         }
497                         err = ubi_leb_change(c->ubi, lnum, sleb->buf, len,
498                                              UBI_UNKNOWN);
499                         if (err)
500                                 return err;
501                 }
502         }
503         return 0;
504 }
505
506 /**
507  * drop_incomplete_group - drop nodes from an incomplete group.
508  * @sleb: scanned LEB information
509  * @offs: offset of dropped nodes is returned here
510  *
511  * This function returns %1 if nodes are dropped and %0 otherwise.
512  */
513 static int drop_incomplete_group(struct ubifs_scan_leb *sleb, int *offs)
514 {
515         int dropped = 0;
516
517         while (!list_empty(&sleb->nodes)) {
518                 struct ubifs_scan_node *snod;
519                 struct ubifs_ch *ch;
520
521                 snod = list_entry(sleb->nodes.prev, struct ubifs_scan_node,
522                                   list);
523                 ch = snod->node;
524                 if (ch->group_type != UBIFS_IN_NODE_GROUP)
525                         return dropped;
526                 dbg_rcvry("dropping node at %d:%d", sleb->lnum, snod->offs);
527                 *offs = snod->offs;
528                 list_del(&snod->list);
529                 kfree(snod);
530                 sleb->nodes_cnt -= 1;
531                 dropped = 1;
532         }
533         return dropped;
534 }
535
536 /**
537  * ubifs_recover_leb - scan and recover a LEB.
538  * @c: UBIFS file-system description object
539  * @lnum: LEB number
540  * @offs: offset
541  * @sbuf: LEB-sized buffer to use
542  * @grouped: nodes may be grouped for recovery
543  *
544  * This function does a scan of a LEB, but caters for errors that might have
545  * been caused by the unclean unmount from which we are attempting to recover.
546  * Returns %0 in case of success, %-EUCLEAN if an unrecoverable corruption is
547  * found, and a negative error code in case of failure.
548  */
549 struct ubifs_scan_leb *ubifs_recover_leb(struct ubifs_info *c, int lnum,
550                                          int offs, void *sbuf, int grouped)
551 {
552         int err, len = c->leb_size - offs, need_clean = 0, quiet = 1;
553         int empty_chkd = 0, start = offs;
554         struct ubifs_scan_leb *sleb;
555         void *buf = sbuf + offs;
556
557         dbg_rcvry("%d:%d", lnum, offs);
558
559         sleb = ubifs_start_scan(c, lnum, offs, sbuf);
560         if (IS_ERR(sleb))
561                 return sleb;
562
563         if (sleb->ecc)
564                 need_clean = 1;
565
566         while (len >= 8) {
567                 int ret;
568
569                 dbg_scan("look at LEB %d:%d (%d bytes left)",
570                          lnum, offs, len);
571
572                 cond_resched();
573
574                 /*
575                  * Scan quietly until there is an error from which we cannot
576                  * recover
577                  */
578                 ret = ubifs_scan_a_node(c, buf, len, lnum, offs, quiet);
579
580                 if (ret == SCANNED_A_NODE) {
581                         /* A valid node, and not a padding node */
582                         struct ubifs_ch *ch = buf;
583                         int node_len;
584
585                         err = ubifs_add_snod(c, sleb, buf, offs);
586                         if (err)
587                                 goto error;
588                         node_len = ALIGN(le32_to_cpu(ch->len), 8);
589                         offs += node_len;
590                         buf += node_len;
591                         len -= node_len;
592                         continue;
593                 }
594
595                 if (ret > 0) {
596                         /* Padding bytes or a valid padding node */
597                         offs += ret;
598                         buf += ret;
599                         len -= ret;
600                         continue;
601                 }
602
603                 if (ret == SCANNED_EMPTY_SPACE) {
604                         if (!is_empty(buf, len)) {
605                                 if (!is_last_write(c, buf, offs))
606                                         break;
607                                 clean_buf(c, &buf, lnum, &offs, &len);
608                                 need_clean = 1;
609                         }
610                         empty_chkd = 1;
611                         break;
612                 }
613
614                 if (ret == SCANNED_GARBAGE || ret == SCANNED_A_BAD_PAD_NODE)
615                         if (is_last_write(c, buf, offs)) {
616                                 clean_buf(c, &buf, lnum, &offs, &len);
617                                 need_clean = 1;
618                                 empty_chkd = 1;
619                                 break;
620                         }
621
622                 if (ret == SCANNED_A_CORRUPT_NODE)
623                         if (no_more_nodes(c, buf, len, lnum, offs)) {
624                                 clean_buf(c, &buf, lnum, &offs, &len);
625                                 need_clean = 1;
626                                 empty_chkd = 1;
627                                 break;
628                         }
629
630                 if (quiet) {
631                         /* Redo the last scan but noisily */
632                         quiet = 0;
633                         continue;
634                 }
635
636                 switch (ret) {
637                 case SCANNED_GARBAGE:
638                         dbg_err("garbage");
639                         goto corrupted;
640                 case SCANNED_A_CORRUPT_NODE:
641                 case SCANNED_A_BAD_PAD_NODE:
642                         dbg_err("bad node");
643                         goto corrupted;
644                 default:
645                         dbg_err("unknown");
646                         err = -EINVAL;
647                         goto error;
648                 }
649         }
650
651         if (!empty_chkd && !is_empty(buf, len)) {
652                 if (is_last_write(c, buf, offs)) {
653                         clean_buf(c, &buf, lnum, &offs, &len);
654                         need_clean = 1;
655                 } else {
656                         ubifs_err("corrupt empty space at LEB %d:%d",
657                                   lnum, offs);
658                         goto corrupted;
659                 }
660         }
661
662         /* Drop nodes from incomplete group */
663         if (grouped && drop_incomplete_group(sleb, &offs)) {
664                 buf = sbuf + offs;
665                 len = c->leb_size - offs;
666                 clean_buf(c, &buf, lnum, &offs, &len);
667                 need_clean = 1;
668         }
669
670         if (offs % c->min_io_size) {
671                 clean_buf(c, &buf, lnum, &offs, &len);
672                 need_clean = 1;
673         }
674
675         ubifs_end_scan(c, sleb, lnum, offs);
676
677         if (need_clean) {
678                 err = fix_unclean_leb(c, sleb, start);
679                 if (err)
680                         goto error;
681         }
682
683         return sleb;
684
685 corrupted:
686         ubifs_scanned_corruption(c, lnum, offs, buf);
687         err = -EUCLEAN;
688 error:
689         ubifs_err("LEB %d scanning failed", lnum);
690         ubifs_scan_destroy(sleb);
691         return ERR_PTR(err);
692 }
693
694 /**
695  * get_cs_sqnum - get commit start sequence number.
696  * @c: UBIFS file-system description object
697  * @lnum: LEB number of commit start node
698  * @offs: offset of commit start node
699  * @cs_sqnum: commit start sequence number is returned here
700  *
701  * This function returns %0 on success and a negative error code on failure.
702  */
703 static int get_cs_sqnum(struct ubifs_info *c, int lnum, int offs,
704                         unsigned long long *cs_sqnum)
705 {
706         struct ubifs_cs_node *cs_node = NULL;
707         int err, ret;
708
709         dbg_rcvry("at %d:%d", lnum, offs);
710         cs_node = kmalloc(UBIFS_CS_NODE_SZ, GFP_KERNEL);
711         if (!cs_node)
712                 return -ENOMEM;
713         if (c->leb_size - offs < UBIFS_CS_NODE_SZ)
714                 goto out_err;
715         err = ubi_read(c->ubi, lnum, (void *)cs_node, offs, UBIFS_CS_NODE_SZ);
716         if (err && err != -EBADMSG)
717                 goto out_free;
718         ret = ubifs_scan_a_node(c, cs_node, UBIFS_CS_NODE_SZ, lnum, offs, 0);
719         if (ret != SCANNED_A_NODE) {
720                 dbg_err("Not a valid node");
721                 goto out_err;
722         }
723         if (cs_node->ch.node_type != UBIFS_CS_NODE) {
724                 dbg_err("Node a CS node, type is %d", cs_node->ch.node_type);
725                 goto out_err;
726         }
727         if (le64_to_cpu(cs_node->cmt_no) != c->cmt_no) {
728                 dbg_err("CS node cmt_no %llu != current cmt_no %llu",
729                         (unsigned long long)le64_to_cpu(cs_node->cmt_no),
730                         c->cmt_no);
731                 goto out_err;
732         }
733         *cs_sqnum = le64_to_cpu(cs_node->ch.sqnum);
734         dbg_rcvry("commit start sqnum %llu", *cs_sqnum);
735         kfree(cs_node);
736         return 0;
737
738 out_err:
739         err = -EINVAL;
740 out_free:
741         ubifs_err("failed to get CS sqnum");
742         kfree(cs_node);
743         return err;
744 }
745
746 /**
747  * ubifs_recover_log_leb - scan and recover a log LEB.
748  * @c: UBIFS file-system description object
749  * @lnum: LEB number
750  * @offs: offset
751  * @sbuf: LEB-sized buffer to use
752  *
753  * This function does a scan of a LEB, but caters for errors that might have
754  * been caused by the unclean unmount from which we are attempting to recover.
755  *
756  * This function returns %0 on success and a negative error code on failure.
757  */
758 struct ubifs_scan_leb *ubifs_recover_log_leb(struct ubifs_info *c, int lnum,
759                                              int offs, void *sbuf)
760 {
761         struct ubifs_scan_leb *sleb;
762         int next_lnum;
763
764         dbg_rcvry("LEB %d", lnum);
765         next_lnum = lnum + 1;
766         if (next_lnum >= UBIFS_LOG_LNUM + c->log_lebs)
767                 next_lnum = UBIFS_LOG_LNUM;
768         if (next_lnum != c->ltail_lnum) {
769                 /*
770                  * We can only recover at the end of the log, so check that the
771                  * next log LEB is empty or out of date.
772                  */
773                 sleb = ubifs_scan(c, next_lnum, 0, sbuf);
774                 if (IS_ERR(sleb))
775                         return sleb;
776                 if (sleb->nodes_cnt) {
777                         struct ubifs_scan_node *snod;
778                         unsigned long long cs_sqnum = c->cs_sqnum;
779
780                         snod = list_entry(sleb->nodes.next,
781                                           struct ubifs_scan_node, list);
782                         if (cs_sqnum == 0) {
783                                 int err;
784
785                                 err = get_cs_sqnum(c, lnum, offs, &cs_sqnum);
786                                 if (err) {
787                                         ubifs_scan_destroy(sleb);
788                                         return ERR_PTR(err);
789                                 }
790                         }
791                         if (snod->sqnum > cs_sqnum) {
792                                 ubifs_err("unrecoverable log corruption "
793                                           "in LEB %d", lnum);
794                                 ubifs_scan_destroy(sleb);
795                                 return ERR_PTR(-EUCLEAN);
796                         }
797                 }
798                 ubifs_scan_destroy(sleb);
799         }
800         return ubifs_recover_leb(c, lnum, offs, sbuf, 0);
801 }
802
803 /**
804  * recover_head - recover a head.
805  * @c: UBIFS file-system description object
806  * @lnum: LEB number of head to recover
807  * @offs: offset of head to recover
808  * @sbuf: LEB-sized buffer to use
809  *
810  * This function ensures that there is no data on the flash at a head location.
811  *
812  * This function returns %0 on success and a negative error code on failure.
813  */
814 static int recover_head(const struct ubifs_info *c, int lnum, int offs,
815                         void *sbuf)
816 {
817         int len, err, need_clean = 0;
818
819         if (c->min_io_size > 1)
820                 len = c->min_io_size;
821         else
822                 len = 512;
823         if (offs + len > c->leb_size)
824                 len = c->leb_size - offs;
825
826         if (!len)
827                 return 0;
828
829         /* Read at the head location and check it is empty flash */
830         err = ubi_read(c->ubi, lnum, sbuf, offs, len);
831         if (err)
832                 need_clean = 1;
833         else {
834                 uint8_t *p = sbuf;
835
836                 while (len--)
837                         if (*p++ != 0xff) {
838                                 need_clean = 1;
839                                 break;
840                         }
841         }
842
843         if (need_clean) {
844                 dbg_rcvry("cleaning head at %d:%d", lnum, offs);
845                 if (offs == 0)
846                         return ubifs_leb_unmap(c, lnum);
847                 err = ubi_read(c->ubi, lnum, sbuf, 0, offs);
848                 if (err)
849                         return err;
850                 return ubi_leb_change(c->ubi, lnum, sbuf, offs, UBI_UNKNOWN);
851         }
852
853         return 0;
854 }
855
856 /**
857  * ubifs_recover_inl_heads - recover index and LPT heads.
858  * @c: UBIFS file-system description object
859  * @sbuf: LEB-sized buffer to use
860  *
861  * This function ensures that there is no data on the flash at the index and
862  * LPT head locations.
863  *
864  * This deals with the recovery of a half-completed journal commit. UBIFS is
865  * careful never to overwrite the last version of the index or the LPT. Because
866  * the index and LPT are wandering trees, data from a half-completed commit will
867  * not be referenced anywhere in UBIFS. The data will be either in LEBs that are
868  * assumed to be empty and will be unmapped anyway before use, or in the index
869  * and LPT heads.
870  *
871  * This function returns %0 on success and a negative error code on failure.
872  */
873 int ubifs_recover_inl_heads(const struct ubifs_info *c, void *sbuf)
874 {
875         int err;
876
877         ubifs_assert(!(c->vfs_sb->s_flags & MS_RDONLY) || c->remounting_rw);
878
879         dbg_rcvry("checking index head at %d:%d", c->ihead_lnum, c->ihead_offs);
880         err = recover_head(c, c->ihead_lnum, c->ihead_offs, sbuf);
881         if (err)
882                 return err;
883
884         dbg_rcvry("checking LPT head at %d:%d", c->nhead_lnum, c->nhead_offs);
885         err = recover_head(c, c->nhead_lnum, c->nhead_offs, sbuf);
886         if (err)
887                 return err;
888
889         return 0;
890 }
891
892 /**
893  *  clean_an_unclean_leb - read and write a LEB to remove corruption.
894  * @c: UBIFS file-system description object
895  * @ucleb: unclean LEB information
896  * @sbuf: LEB-sized buffer to use
897  *
898  * This function reads a LEB up to a point pre-determined by the mount recovery,
899  * checks the nodes, and writes the result back to the flash, thereby cleaning
900  * off any following corruption, or non-fatal ECC errors.
901  *
902  * This function returns %0 on success and a negative error code on failure.
903  */
904 static int clean_an_unclean_leb(const struct ubifs_info *c,
905                                 struct ubifs_unclean_leb *ucleb, void *sbuf)
906 {
907         int err, lnum = ucleb->lnum, offs = 0, len = ucleb->endpt, quiet = 1;
908         void *buf = sbuf;
909
910         dbg_rcvry("LEB %d len %d", lnum, len);
911
912         if (len == 0) {
913                 /* Nothing to read, just unmap it */
914                 err = ubifs_leb_unmap(c, lnum);
915                 if (err)
916                         return err;
917                 return 0;
918         }
919
920         err = ubi_read(c->ubi, lnum, buf, offs, len);
921         if (err && err != -EBADMSG)
922                 return err;
923
924         while (len >= 8) {
925                 int ret;
926
927                 cond_resched();
928
929                 /* Scan quietly until there is an error */
930                 ret = ubifs_scan_a_node(c, buf, len, lnum, offs, quiet);
931
932                 if (ret == SCANNED_A_NODE) {
933                         /* A valid node, and not a padding node */
934                         struct ubifs_ch *ch = buf;
935                         int node_len;
936
937                         node_len = ALIGN(le32_to_cpu(ch->len), 8);
938                         offs += node_len;
939                         buf += node_len;
940                         len -= node_len;
941                         continue;
942                 }
943
944                 if (ret > 0) {
945                         /* Padding bytes or a valid padding node */
946                         offs += ret;
947                         buf += ret;
948                         len -= ret;
949                         continue;
950                 }
951
952                 if (ret == SCANNED_EMPTY_SPACE) {
953                         ubifs_err("unexpected empty space at %d:%d",
954                                   lnum, offs);
955                         return -EUCLEAN;
956                 }
957
958                 if (quiet) {
959                         /* Redo the last scan but noisily */
960                         quiet = 0;
961                         continue;
962                 }
963
964                 ubifs_scanned_corruption(c, lnum, offs, buf);
965                 return -EUCLEAN;
966         }
967
968         /* Pad to min_io_size */
969         len = ALIGN(ucleb->endpt, c->min_io_size);
970         if (len > ucleb->endpt) {
971                 int pad_len = len - ALIGN(ucleb->endpt, 8);
972
973                 if (pad_len > 0) {
974                         buf = c->sbuf + len - pad_len;
975                         ubifs_pad(c, buf, pad_len);
976                 }
977         }
978
979         /* Write back the LEB atomically */
980         err = ubi_leb_change(c->ubi, lnum, sbuf, len, UBI_UNKNOWN);
981         if (err)
982                 return err;
983
984         dbg_rcvry("cleaned LEB %d", lnum);
985
986         return 0;
987 }
988
989 /**
990  * ubifs_clean_lebs - clean LEBs recovered during read-only mount.
991  * @c: UBIFS file-system description object
992  * @sbuf: LEB-sized buffer to use
993  *
994  * This function cleans a LEB identified during recovery that needs to be
995  * written but was not because UBIFS was mounted read-only. This happens when
996  * remounting to read-write mode.
997  *
998  * This function returns %0 on success and a negative error code on failure.
999  */
1000 int ubifs_clean_lebs(const struct ubifs_info *c, void *sbuf)
1001 {
1002         dbg_rcvry("recovery");
1003         while (!list_empty(&c->unclean_leb_list)) {
1004                 struct ubifs_unclean_leb *ucleb;
1005                 int err;
1006
1007                 ucleb = list_entry(c->unclean_leb_list.next,
1008                                    struct ubifs_unclean_leb, list);
1009                 err = clean_an_unclean_leb(c, ucleb, sbuf);
1010                 if (err)
1011                         return err;
1012                 list_del(&ucleb->list);
1013                 kfree(ucleb);
1014         }
1015         return 0;
1016 }
1017
1018 /**
1019  * ubifs_rcvry_gc_commit - recover the GC LEB number and run the commit.
1020  * @c: UBIFS file-system description object
1021  *
1022  * Out-of-place garbage collection requires always one empty LEB with which to
1023  * start garbage collection. The LEB number is recorded in c->gc_lnum and is
1024  * written to the master node on unmounting. In the case of an unclean unmount
1025  * the value of gc_lnum recorded in the master node is out of date and cannot
1026  * be used. Instead, recovery must allocate an empty LEB for this purpose.
1027  * However, there may not be enough empty space, in which case it must be
1028  * possible to GC the dirtiest LEB into the GC head LEB.
1029  *
1030  * This function also runs the commit which causes the TNC updates from
1031  * size-recovery and orphans to be written to the flash. That is important to
1032  * ensure correct replay order for subsequent mounts.
1033  *
1034  * This function returns %0 on success and a negative error code on failure.
1035  */
1036 int ubifs_rcvry_gc_commit(struct ubifs_info *c)
1037 {
1038         struct ubifs_wbuf *wbuf = &c->jheads[GCHD].wbuf;
1039         struct ubifs_lprops lp;
1040         int lnum, err;
1041
1042         c->gc_lnum = -1;
1043         if (wbuf->lnum == -1) {
1044                 dbg_rcvry("no GC head LEB");
1045                 goto find_free;
1046         }
1047         /*
1048          * See whether the used space in the dirtiest LEB fits in the GC head
1049          * LEB.
1050          */
1051         if (wbuf->offs == c->leb_size) {
1052                 dbg_rcvry("no room in GC head LEB");
1053                 goto find_free;
1054         }
1055         err = ubifs_find_dirty_leb(c, &lp, wbuf->offs, 2);
1056         if (err) {
1057                 if (err == -ENOSPC)
1058                         dbg_err("could not find a dirty LEB");
1059                 return err;
1060         }
1061         ubifs_assert(!(lp.flags & LPROPS_INDEX));
1062         lnum = lp.lnum;
1063         if (lp.free + lp.dirty == c->leb_size) {
1064                 /* An empty LEB was returned */
1065                 if (lp.free != c->leb_size) {
1066                         err = ubifs_change_one_lp(c, lnum, c->leb_size,
1067                                                   0, 0, 0, 0);
1068                         if (err)
1069                                 return err;
1070                 }
1071                 err = ubifs_leb_unmap(c, lnum);
1072                 if (err)
1073                         return err;
1074                 c->gc_lnum = lnum;
1075                 dbg_rcvry("allocated LEB %d for GC", lnum);
1076                 /* Run the commit */
1077                 dbg_rcvry("committing");
1078                 return ubifs_run_commit(c);
1079         }
1080         /*
1081          * There was no empty LEB so the used space in the dirtiest LEB must fit
1082          * in the GC head LEB.
1083          */
1084         if (lp.free + lp.dirty < wbuf->offs) {
1085                 dbg_rcvry("LEB %d doesn't fit in GC head LEB %d:%d",
1086                           lnum, wbuf->lnum, wbuf->offs);
1087                 err = ubifs_return_leb(c, lnum);
1088                 if (err)
1089                         return err;
1090                 goto find_free;
1091         }
1092         /*
1093          * We run the commit before garbage collection otherwise subsequent
1094          * mounts will see the GC and orphan deletion in a different order.
1095          */
1096         dbg_rcvry("committing");
1097         err = ubifs_run_commit(c);
1098         if (err)
1099                 return err;
1100         /*
1101          * The data in the dirtiest LEB fits in the GC head LEB, so do the GC
1102          * - use locking to keep 'ubifs_assert()' happy.
1103          */
1104         dbg_rcvry("GC'ing LEB %d", lnum);
1105         mutex_lock_nested(&wbuf->io_mutex, wbuf->jhead);
1106         err = ubifs_garbage_collect_leb(c, &lp);
1107         if (err >= 0) {
1108                 int err2 = ubifs_wbuf_sync_nolock(wbuf);
1109
1110                 if (err2)
1111                         err = err2;
1112         }
1113         mutex_unlock(&wbuf->io_mutex);
1114         if (err < 0) {
1115                 dbg_err("GC failed, error %d", err);
1116                 if (err == -EAGAIN)
1117                         err = -EINVAL;
1118                 return err;
1119         }
1120         if (err != LEB_RETAINED) {
1121                 dbg_err("GC returned %d", err);
1122                 return -EINVAL;
1123         }
1124         err = ubifs_leb_unmap(c, c->gc_lnum);
1125         if (err)
1126                 return err;
1127         dbg_rcvry("allocated LEB %d for GC", lnum);
1128         return 0;
1129
1130 find_free:
1131         /*
1132          * There is no GC head LEB or the free space in the GC head LEB is too
1133          * small. Allocate gc_lnum by calling 'ubifs_find_free_leb_for_idx()' so
1134          * GC is not run.
1135          */
1136         lnum = ubifs_find_free_leb_for_idx(c);
1137         if (lnum < 0) {
1138                 dbg_err("could not find an empty LEB");
1139                 return lnum;
1140         }
1141         /* And reset the index flag */
1142         err = ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0,
1143                                   LPROPS_INDEX, 0);
1144         if (err)
1145                 return err;
1146         c->gc_lnum = lnum;
1147         dbg_rcvry("allocated LEB %d for GC", lnum);
1148         /* Run the commit */
1149         dbg_rcvry("committing");
1150         return ubifs_run_commit(c);
1151 }
1152
1153 /**
1154  * struct size_entry - inode size information for recovery.
1155  * @rb: link in the RB-tree of sizes
1156  * @inum: inode number
1157  * @i_size: size on inode
1158  * @d_size: maximum size based on data nodes
1159  * @exists: indicates whether the inode exists
1160  * @inode: inode if pinned in memory awaiting rw mode to fix it
1161  */
1162 struct size_entry {
1163         struct rb_node rb;
1164         ino_t inum;
1165         loff_t i_size;
1166         loff_t d_size;
1167         int exists;
1168         struct inode *inode;
1169 };
1170
1171 /**
1172  * add_ino - add an entry to the size tree.
1173  * @c: UBIFS file-system description object
1174  * @inum: inode number
1175  * @i_size: size on inode
1176  * @d_size: maximum size based on data nodes
1177  * @exists: indicates whether the inode exists
1178  */
1179 static int add_ino(struct ubifs_info *c, ino_t inum, loff_t i_size,
1180                    loff_t d_size, int exists)
1181 {
1182         struct rb_node **p = &c->size_tree.rb_node, *parent = NULL;
1183         struct size_entry *e;
1184
1185         while (*p) {
1186                 parent = *p;
1187                 e = rb_entry(parent, struct size_entry, rb);
1188                 if (inum < e->inum)
1189                         p = &(*p)->rb_left;
1190                 else
1191                         p = &(*p)->rb_right;
1192         }
1193
1194         e = kzalloc(sizeof(struct size_entry), GFP_KERNEL);
1195         if (!e)
1196                 return -ENOMEM;
1197
1198         e->inum = inum;
1199         e->i_size = i_size;
1200         e->d_size = d_size;
1201         e->exists = exists;
1202
1203         rb_link_node(&e->rb, parent, p);
1204         rb_insert_color(&e->rb, &c->size_tree);
1205
1206         return 0;
1207 }
1208
1209 /**
1210  * find_ino - find an entry on the size tree.
1211  * @c: UBIFS file-system description object
1212  * @inum: inode number
1213  */
1214 static struct size_entry *find_ino(struct ubifs_info *c, ino_t inum)
1215 {
1216         struct rb_node *p = c->size_tree.rb_node;
1217         struct size_entry *e;
1218
1219         while (p) {
1220                 e = rb_entry(p, struct size_entry, rb);
1221                 if (inum < e->inum)
1222                         p = p->rb_left;
1223                 else if (inum > e->inum)
1224                         p = p->rb_right;
1225                 else
1226                         return e;
1227         }
1228         return NULL;
1229 }
1230
1231 /**
1232  * remove_ino - remove an entry from the size tree.
1233  * @c: UBIFS file-system description object
1234  * @inum: inode number
1235  */
1236 static void remove_ino(struct ubifs_info *c, ino_t inum)
1237 {
1238         struct size_entry *e = find_ino(c, inum);
1239
1240         if (!e)
1241                 return;
1242         rb_erase(&e->rb, &c->size_tree);
1243         kfree(e);
1244 }
1245
1246 /**
1247  * ubifs_destroy_size_tree - free resources related to the size tree.
1248  * @c: UBIFS file-system description object
1249  */
1250 void ubifs_destroy_size_tree(struct ubifs_info *c)
1251 {
1252         struct rb_node *this = c->size_tree.rb_node;
1253         struct size_entry *e;
1254
1255         while (this) {
1256                 if (this->rb_left) {
1257                         this = this->rb_left;
1258                         continue;
1259                 } else if (this->rb_right) {
1260                         this = this->rb_right;
1261                         continue;
1262                 }
1263                 e = rb_entry(this, struct size_entry, rb);
1264                 if (e->inode)
1265                         iput(e->inode);
1266                 this = rb_parent(this);
1267                 if (this) {
1268                         if (this->rb_left == &e->rb)
1269                                 this->rb_left = NULL;
1270                         else
1271                                 this->rb_right = NULL;
1272                 }
1273                 kfree(e);
1274         }
1275         c->size_tree = RB_ROOT;
1276 }
1277
1278 /**
1279  * ubifs_recover_size_accum - accumulate inode sizes for recovery.
1280  * @c: UBIFS file-system description object
1281  * @key: node key
1282  * @deletion: node is for a deletion
1283  * @new_size: inode size
1284  *
1285  * This function has two purposes:
1286  *     1) to ensure there are no data nodes that fall outside the inode size
1287  *     2) to ensure there are no data nodes for inodes that do not exist
1288  * To accomplish those purposes, a rb-tree is constructed containing an entry
1289  * for each inode number in the journal that has not been deleted, and recording
1290  * the size from the inode node, the maximum size of any data node (also altered
1291  * by truncations) and a flag indicating a inode number for which no inode node
1292  * was present in the journal.
1293  *
1294  * Note that there is still the possibility that there are data nodes that have
1295  * been committed that are beyond the inode size, however the only way to find
1296  * them would be to scan the entire index. Alternatively, some provision could
1297  * be made to record the size of inodes at the start of commit, which would seem
1298  * very cumbersome for a scenario that is quite unlikely and the only negative
1299  * consequence of which is wasted space.
1300  *
1301  * This functions returns %0 on success and a negative error code on failure.
1302  */
1303 int ubifs_recover_size_accum(struct ubifs_info *c, union ubifs_key *key,
1304                              int deletion, loff_t new_size)
1305 {
1306         ino_t inum = key_inum(c, key);
1307         struct size_entry *e;
1308         int err;
1309
1310         switch (key_type(c, key)) {
1311         case UBIFS_INO_KEY:
1312                 if (deletion)
1313                         remove_ino(c, inum);
1314                 else {
1315                         e = find_ino(c, inum);
1316                         if (e) {
1317                                 e->i_size = new_size;
1318                                 e->exists = 1;
1319                         } else {
1320                                 err = add_ino(c, inum, new_size, 0, 1);
1321                                 if (err)
1322                                         return err;
1323                         }
1324                 }
1325                 break;
1326         case UBIFS_DATA_KEY:
1327                 e = find_ino(c, inum);
1328                 if (e) {
1329                         if (new_size > e->d_size)
1330                                 e->d_size = new_size;
1331                 } else {
1332                         err = add_ino(c, inum, 0, new_size, 0);
1333                         if (err)
1334                                 return err;
1335                 }
1336                 break;
1337         case UBIFS_TRUN_KEY:
1338                 e = find_ino(c, inum);
1339                 if (e)
1340                         e->d_size = new_size;
1341                 break;
1342         }
1343         return 0;
1344 }
1345
1346 /**
1347  * fix_size_in_place - fix inode size in place on flash.
1348  * @c: UBIFS file-system description object
1349  * @e: inode size information for recovery
1350  */
1351 static int fix_size_in_place(struct ubifs_info *c, struct size_entry *e)
1352 {
1353         struct ubifs_ino_node *ino = c->sbuf;
1354         unsigned char *p;
1355         union ubifs_key key;
1356         int err, lnum, offs, len;
1357         loff_t i_size;
1358         uint32_t crc;
1359
1360         /* Locate the inode node LEB number and offset */
1361         ino_key_init(c, &key, e->inum);
1362         err = ubifs_tnc_locate(c, &key, ino, &lnum, &offs);
1363         if (err)
1364                 goto out;
1365         /*
1366          * If the size recorded on the inode node is greater than the size that
1367          * was calculated from nodes in the journal then don't change the inode.
1368          */
1369         i_size = le64_to_cpu(ino->size);
1370         if (i_size >= e->d_size)
1371                 return 0;
1372         /* Read the LEB */
1373         err = ubi_read(c->ubi, lnum, c->sbuf, 0, c->leb_size);
1374         if (err)
1375                 goto out;
1376         /* Change the size field and recalculate the CRC */
1377         ino = c->sbuf + offs;
1378         ino->size = cpu_to_le64(e->d_size);
1379         len = le32_to_cpu(ino->ch.len);
1380         crc = crc32(UBIFS_CRC32_INIT, (void *)ino + 8, len - 8);
1381         ino->ch.crc = cpu_to_le32(crc);
1382         /* Work out where data in the LEB ends and free space begins */
1383         p = c->sbuf;
1384         len = c->leb_size - 1;
1385         while (p[len] == 0xff)
1386                 len -= 1;
1387         len = ALIGN(len + 1, c->min_io_size);
1388         /* Atomically write the fixed LEB back again */
1389         err = ubi_leb_change(c->ubi, lnum, c->sbuf, len, UBI_UNKNOWN);
1390         if (err)
1391                 goto out;
1392         dbg_rcvry("inode %lu at %d:%d size %lld -> %lld ",
1393                   (unsigned long)e->inum, lnum, offs, i_size, e->d_size);
1394         return 0;
1395
1396 out:
1397         ubifs_warn("inode %lu failed to fix size %lld -> %lld error %d",
1398                    (unsigned long)e->inum, e->i_size, e->d_size, err);
1399         return err;
1400 }
1401
1402 /**
1403  * ubifs_recover_size - recover inode size.
1404  * @c: UBIFS file-system description object
1405  *
1406  * This function attempts to fix inode size discrepancies identified by the
1407  * 'ubifs_recover_size_accum()' function.
1408  *
1409  * This functions returns %0 on success and a negative error code on failure.
1410  */
1411 int ubifs_recover_size(struct ubifs_info *c)
1412 {
1413         struct rb_node *this = rb_first(&c->size_tree);
1414
1415         while (this) {
1416                 struct size_entry *e;
1417                 int err;
1418
1419                 e = rb_entry(this, struct size_entry, rb);
1420                 if (!e->exists) {
1421                         union ubifs_key key;
1422
1423                         ino_key_init(c, &key, e->inum);
1424                         err = ubifs_tnc_lookup(c, &key, c->sbuf);
1425                         if (err && err != -ENOENT)
1426                                 return err;
1427                         if (err == -ENOENT) {
1428                                 /* Remove data nodes that have no inode */
1429                                 dbg_rcvry("removing ino %lu",
1430                                           (unsigned long)e->inum);
1431                                 err = ubifs_tnc_remove_ino(c, e->inum);
1432                                 if (err)
1433                                         return err;
1434                         } else {
1435                                 struct ubifs_ino_node *ino = c->sbuf;
1436
1437                                 e->exists = 1;
1438                                 e->i_size = le64_to_cpu(ino->size);
1439                         }
1440                 }
1441                 if (e->exists && e->i_size < e->d_size) {
1442                         if (!e->inode && (c->vfs_sb->s_flags & MS_RDONLY)) {
1443                                 /* Fix the inode size and pin it in memory */
1444                                 struct inode *inode;
1445
1446                                 inode = ubifs_iget(c->vfs_sb, e->inum);
1447                                 if (IS_ERR(inode))
1448                                         return PTR_ERR(inode);
1449                                 if (inode->i_size < e->d_size) {
1450                                         dbg_rcvry("ino %lu size %lld -> %lld",
1451                                                   (unsigned long)e->inum,
1452                                                   e->d_size, inode->i_size);
1453                                         inode->i_size = e->d_size;
1454                                         ubifs_inode(inode)->ui_size = e->d_size;
1455                                         e->inode = inode;
1456                                         this = rb_next(this);
1457                                         continue;
1458                                 }
1459                                 iput(inode);
1460                         } else {
1461                                 /* Fix the size in place */
1462                                 err = fix_size_in_place(c, e);
1463                                 if (err)
1464                                         return err;
1465                                 if (e->inode)
1466                                         iput(e->inode);
1467                         }
1468                 }
1469                 this = rb_next(this);
1470                 rb_erase(&e->rb, &c->size_tree);
1471                 kfree(e);
1472         }
1473         return 0;
1474 }