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