1f88caffdf2acfae85113a064c68f0e3fbefb9a5
[linux-2.6-microblaze.git] / fs / ubifs / lpt_commit.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 commit-related functionality of the LEB properties
25  * subsystem.
26  */
27
28 #include <linux/crc16.h>
29 #include <linux/slab.h>
30 #include <linux/random.h>
31 #include "ubifs.h"
32
33 static int dbg_populate_lsave(struct ubifs_info *c);
34
35 /**
36  * first_dirty_cnode - find first dirty cnode.
37  * @c: UBIFS file-system description object
38  * @nnode: nnode at which to start
39  *
40  * This function returns the first dirty cnode or %NULL if there is not one.
41  */
42 static struct ubifs_cnode *first_dirty_cnode(const struct ubifs_info *c, struct ubifs_nnode *nnode)
43 {
44         ubifs_assert(c, nnode);
45         while (1) {
46                 int i, cont = 0;
47
48                 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
49                         struct ubifs_cnode *cnode;
50
51                         cnode = nnode->nbranch[i].cnode;
52                         if (cnode &&
53                             test_bit(DIRTY_CNODE, &cnode->flags)) {
54                                 if (cnode->level == 0)
55                                         return cnode;
56                                 nnode = (struct ubifs_nnode *)cnode;
57                                 cont = 1;
58                                 break;
59                         }
60                 }
61                 if (!cont)
62                         return (struct ubifs_cnode *)nnode;
63         }
64 }
65
66 /**
67  * next_dirty_cnode - find next dirty cnode.
68  * @c: UBIFS file-system description object
69  * @cnode: cnode from which to begin searching
70  *
71  * This function returns the next dirty cnode or %NULL if there is not one.
72  */
73 static struct ubifs_cnode *next_dirty_cnode(const struct ubifs_info *c, struct ubifs_cnode *cnode)
74 {
75         struct ubifs_nnode *nnode;
76         int i;
77
78         ubifs_assert(c, cnode);
79         nnode = cnode->parent;
80         if (!nnode)
81                 return NULL;
82         for (i = cnode->iip + 1; i < UBIFS_LPT_FANOUT; i++) {
83                 cnode = nnode->nbranch[i].cnode;
84                 if (cnode && test_bit(DIRTY_CNODE, &cnode->flags)) {
85                         if (cnode->level == 0)
86                                 return cnode; /* cnode is a pnode */
87                         /* cnode is a nnode */
88                         return first_dirty_cnode(c, (struct ubifs_nnode *)cnode);
89                 }
90         }
91         return (struct ubifs_cnode *)nnode;
92 }
93
94 /**
95  * get_cnodes_to_commit - create list of dirty cnodes to commit.
96  * @c: UBIFS file-system description object
97  *
98  * This function returns the number of cnodes to commit.
99  */
100 static int get_cnodes_to_commit(struct ubifs_info *c)
101 {
102         struct ubifs_cnode *cnode, *cnext;
103         int cnt = 0;
104
105         if (!c->nroot)
106                 return 0;
107
108         if (!test_bit(DIRTY_CNODE, &c->nroot->flags))
109                 return 0;
110
111         c->lpt_cnext = first_dirty_cnode(c, c->nroot);
112         cnode = c->lpt_cnext;
113         if (!cnode)
114                 return 0;
115         cnt += 1;
116         while (1) {
117                 ubifs_assert(c, !test_bit(COW_CNODE, &cnode->flags));
118                 __set_bit(COW_CNODE, &cnode->flags);
119                 cnext = next_dirty_cnode(c, cnode);
120                 if (!cnext) {
121                         cnode->cnext = c->lpt_cnext;
122                         break;
123                 }
124                 cnode->cnext = cnext;
125                 cnode = cnext;
126                 cnt += 1;
127         }
128         dbg_cmt("committing %d cnodes", cnt);
129         dbg_lp("committing %d cnodes", cnt);
130         ubifs_assert(c, cnt == c->dirty_nn_cnt + c->dirty_pn_cnt);
131         return cnt;
132 }
133
134 /**
135  * upd_ltab - update LPT LEB properties.
136  * @c: UBIFS file-system description object
137  * @lnum: LEB number
138  * @free: amount of free space
139  * @dirty: amount of dirty space to add
140  */
141 static void upd_ltab(struct ubifs_info *c, int lnum, int free, int dirty)
142 {
143         dbg_lp("LEB %d free %d dirty %d to %d +%d",
144                lnum, c->ltab[lnum - c->lpt_first].free,
145                c->ltab[lnum - c->lpt_first].dirty, free, dirty);
146         ubifs_assert(c, lnum >= c->lpt_first && lnum <= c->lpt_last);
147         c->ltab[lnum - c->lpt_first].free = free;
148         c->ltab[lnum - c->lpt_first].dirty += dirty;
149 }
150
151 /**
152  * alloc_lpt_leb - allocate an LPT LEB that is empty.
153  * @c: UBIFS file-system description object
154  * @lnum: LEB number is passed and returned here
155  *
156  * This function finds the next empty LEB in the ltab starting from @lnum. If a
157  * an empty LEB is found it is returned in @lnum and the function returns %0.
158  * Otherwise the function returns -ENOSPC.  Note however, that LPT is designed
159  * never to run out of space.
160  */
161 static int alloc_lpt_leb(struct ubifs_info *c, int *lnum)
162 {
163         int i, n;
164
165         n = *lnum - c->lpt_first + 1;
166         for (i = n; i < c->lpt_lebs; i++) {
167                 if (c->ltab[i].tgc || c->ltab[i].cmt)
168                         continue;
169                 if (c->ltab[i].free == c->leb_size) {
170                         c->ltab[i].cmt = 1;
171                         *lnum = i + c->lpt_first;
172                         return 0;
173                 }
174         }
175
176         for (i = 0; i < n; i++) {
177                 if (c->ltab[i].tgc || c->ltab[i].cmt)
178                         continue;
179                 if (c->ltab[i].free == c->leb_size) {
180                         c->ltab[i].cmt = 1;
181                         *lnum = i + c->lpt_first;
182                         return 0;
183                 }
184         }
185         return -ENOSPC;
186 }
187
188 /**
189  * layout_cnodes - layout cnodes for commit.
190  * @c: UBIFS file-system description object
191  *
192  * This function returns %0 on success and a negative error code on failure.
193  */
194 static int layout_cnodes(struct ubifs_info *c)
195 {
196         int lnum, offs, len, alen, done_lsave, done_ltab, err;
197         struct ubifs_cnode *cnode;
198
199         err = dbg_chk_lpt_sz(c, 0, 0);
200         if (err)
201                 return err;
202         cnode = c->lpt_cnext;
203         if (!cnode)
204                 return 0;
205         lnum = c->nhead_lnum;
206         offs = c->nhead_offs;
207         /* Try to place lsave and ltab nicely */
208         done_lsave = !c->big_lpt;
209         done_ltab = 0;
210         if (!done_lsave && offs + c->lsave_sz <= c->leb_size) {
211                 done_lsave = 1;
212                 c->lsave_lnum = lnum;
213                 c->lsave_offs = offs;
214                 offs += c->lsave_sz;
215                 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
216         }
217
218         if (offs + c->ltab_sz <= c->leb_size) {
219                 done_ltab = 1;
220                 c->ltab_lnum = lnum;
221                 c->ltab_offs = offs;
222                 offs += c->ltab_sz;
223                 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
224         }
225
226         do {
227                 if (cnode->level) {
228                         len = c->nnode_sz;
229                         c->dirty_nn_cnt -= 1;
230                 } else {
231                         len = c->pnode_sz;
232                         c->dirty_pn_cnt -= 1;
233                 }
234                 while (offs + len > c->leb_size) {
235                         alen = ALIGN(offs, c->min_io_size);
236                         upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
237                         dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
238                         err = alloc_lpt_leb(c, &lnum);
239                         if (err)
240                                 goto no_space;
241                         offs = 0;
242                         ubifs_assert(c, lnum >= c->lpt_first &&
243                                      lnum <= c->lpt_last);
244                         /* Try to place lsave and ltab nicely */
245                         if (!done_lsave) {
246                                 done_lsave = 1;
247                                 c->lsave_lnum = lnum;
248                                 c->lsave_offs = offs;
249                                 offs += c->lsave_sz;
250                                 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
251                                 continue;
252                         }
253                         if (!done_ltab) {
254                                 done_ltab = 1;
255                                 c->ltab_lnum = lnum;
256                                 c->ltab_offs = offs;
257                                 offs += c->ltab_sz;
258                                 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
259                                 continue;
260                         }
261                         break;
262                 }
263                 if (cnode->parent) {
264                         cnode->parent->nbranch[cnode->iip].lnum = lnum;
265                         cnode->parent->nbranch[cnode->iip].offs = offs;
266                 } else {
267                         c->lpt_lnum = lnum;
268                         c->lpt_offs = offs;
269                 }
270                 offs += len;
271                 dbg_chk_lpt_sz(c, 1, len);
272                 cnode = cnode->cnext;
273         } while (cnode && cnode != c->lpt_cnext);
274
275         /* Make sure to place LPT's save table */
276         if (!done_lsave) {
277                 if (offs + c->lsave_sz > c->leb_size) {
278                         alen = ALIGN(offs, c->min_io_size);
279                         upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
280                         dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
281                         err = alloc_lpt_leb(c, &lnum);
282                         if (err)
283                                 goto no_space;
284                         offs = 0;
285                         ubifs_assert(c, lnum >= c->lpt_first &&
286                                      lnum <= c->lpt_last);
287                 }
288                 done_lsave = 1;
289                 c->lsave_lnum = lnum;
290                 c->lsave_offs = offs;
291                 offs += c->lsave_sz;
292                 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
293         }
294
295         /* Make sure to place LPT's own lprops table */
296         if (!done_ltab) {
297                 if (offs + c->ltab_sz > c->leb_size) {
298                         alen = ALIGN(offs, c->min_io_size);
299                         upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
300                         dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
301                         err = alloc_lpt_leb(c, &lnum);
302                         if (err)
303                                 goto no_space;
304                         offs = 0;
305                         ubifs_assert(c, lnum >= c->lpt_first &&
306                                      lnum <= c->lpt_last);
307                 }
308                 c->ltab_lnum = lnum;
309                 c->ltab_offs = offs;
310                 offs += c->ltab_sz;
311                 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
312         }
313
314         alen = ALIGN(offs, c->min_io_size);
315         upd_ltab(c, lnum, c->leb_size - alen, alen - offs);
316         dbg_chk_lpt_sz(c, 4, alen - offs);
317         err = dbg_chk_lpt_sz(c, 3, alen);
318         if (err)
319                 return err;
320         return 0;
321
322 no_space:
323         ubifs_err(c, "LPT out of space at LEB %d:%d needing %d, done_ltab %d, done_lsave %d",
324                   lnum, offs, len, done_ltab, done_lsave);
325         ubifs_dump_lpt_info(c);
326         ubifs_dump_lpt_lebs(c);
327         dump_stack();
328         return err;
329 }
330
331 /**
332  * realloc_lpt_leb - allocate an LPT LEB that is empty.
333  * @c: UBIFS file-system description object
334  * @lnum: LEB number is passed and returned here
335  *
336  * This function duplicates exactly the results of the function alloc_lpt_leb.
337  * It is used during end commit to reallocate the same LEB numbers that were
338  * allocated by alloc_lpt_leb during start commit.
339  *
340  * This function finds the next LEB that was allocated by the alloc_lpt_leb
341  * function starting from @lnum. If a LEB is found it is returned in @lnum and
342  * the function returns %0. Otherwise the function returns -ENOSPC.
343  * Note however, that LPT is designed never to run out of space.
344  */
345 static int realloc_lpt_leb(struct ubifs_info *c, int *lnum)
346 {
347         int i, n;
348
349         n = *lnum - c->lpt_first + 1;
350         for (i = n; i < c->lpt_lebs; i++)
351                 if (c->ltab[i].cmt) {
352                         c->ltab[i].cmt = 0;
353                         *lnum = i + c->lpt_first;
354                         return 0;
355                 }
356
357         for (i = 0; i < n; i++)
358                 if (c->ltab[i].cmt) {
359                         c->ltab[i].cmt = 0;
360                         *lnum = i + c->lpt_first;
361                         return 0;
362                 }
363         return -ENOSPC;
364 }
365
366 /**
367  * write_cnodes - write cnodes for commit.
368  * @c: UBIFS file-system description object
369  *
370  * This function returns %0 on success and a negative error code on failure.
371  */
372 static int write_cnodes(struct ubifs_info *c)
373 {
374         int lnum, offs, len, from, err, wlen, alen, done_ltab, done_lsave;
375         struct ubifs_cnode *cnode;
376         void *buf = c->lpt_buf;
377
378         cnode = c->lpt_cnext;
379         if (!cnode)
380                 return 0;
381         lnum = c->nhead_lnum;
382         offs = c->nhead_offs;
383         from = offs;
384         /* Ensure empty LEB is unmapped */
385         if (offs == 0) {
386                 err = ubifs_leb_unmap(c, lnum);
387                 if (err)
388                         return err;
389         }
390         /* Try to place lsave and ltab nicely */
391         done_lsave = !c->big_lpt;
392         done_ltab = 0;
393         if (!done_lsave && offs + c->lsave_sz <= c->leb_size) {
394                 done_lsave = 1;
395                 ubifs_pack_lsave(c, buf + offs, c->lsave);
396                 offs += c->lsave_sz;
397                 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
398         }
399
400         if (offs + c->ltab_sz <= c->leb_size) {
401                 done_ltab = 1;
402                 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
403                 offs += c->ltab_sz;
404                 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
405         }
406
407         /* Loop for each cnode */
408         do {
409                 if (cnode->level)
410                         len = c->nnode_sz;
411                 else
412                         len = c->pnode_sz;
413                 while (offs + len > c->leb_size) {
414                         wlen = offs - from;
415                         if (wlen) {
416                                 alen = ALIGN(wlen, c->min_io_size);
417                                 memset(buf + offs, 0xff, alen - wlen);
418                                 err = ubifs_leb_write(c, lnum, buf + from, from,
419                                                        alen);
420                                 if (err)
421                                         return err;
422                         }
423                         dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
424                         err = realloc_lpt_leb(c, &lnum);
425                         if (err)
426                                 goto no_space;
427                         offs = from = 0;
428                         ubifs_assert(c, lnum >= c->lpt_first &&
429                                      lnum <= c->lpt_last);
430                         err = ubifs_leb_unmap(c, lnum);
431                         if (err)
432                                 return err;
433                         /* Try to place lsave and ltab nicely */
434                         if (!done_lsave) {
435                                 done_lsave = 1;
436                                 ubifs_pack_lsave(c, buf + offs, c->lsave);
437                                 offs += c->lsave_sz;
438                                 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
439                                 continue;
440                         }
441                         if (!done_ltab) {
442                                 done_ltab = 1;
443                                 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
444                                 offs += c->ltab_sz;
445                                 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
446                                 continue;
447                         }
448                         break;
449                 }
450                 if (cnode->level)
451                         ubifs_pack_nnode(c, buf + offs,
452                                          (struct ubifs_nnode *)cnode);
453                 else
454                         ubifs_pack_pnode(c, buf + offs,
455                                          (struct ubifs_pnode *)cnode);
456                 /*
457                  * The reason for the barriers is the same as in case of TNC.
458                  * See comment in 'write_index()'. 'dirty_cow_nnode()' and
459                  * 'dirty_cow_pnode()' are the functions for which this is
460                  * important.
461                  */
462                 clear_bit(DIRTY_CNODE, &cnode->flags);
463                 smp_mb__before_atomic();
464                 clear_bit(COW_CNODE, &cnode->flags);
465                 smp_mb__after_atomic();
466                 offs += len;
467                 dbg_chk_lpt_sz(c, 1, len);
468                 cnode = cnode->cnext;
469         } while (cnode && cnode != c->lpt_cnext);
470
471         /* Make sure to place LPT's save table */
472         if (!done_lsave) {
473                 if (offs + c->lsave_sz > c->leb_size) {
474                         wlen = offs - from;
475                         alen = ALIGN(wlen, c->min_io_size);
476                         memset(buf + offs, 0xff, alen - wlen);
477                         err = ubifs_leb_write(c, lnum, buf + from, from, alen);
478                         if (err)
479                                 return err;
480                         dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
481                         err = realloc_lpt_leb(c, &lnum);
482                         if (err)
483                                 goto no_space;
484                         offs = from = 0;
485                         ubifs_assert(c, lnum >= c->lpt_first &&
486                                      lnum <= c->lpt_last);
487                         err = ubifs_leb_unmap(c, lnum);
488                         if (err)
489                                 return err;
490                 }
491                 done_lsave = 1;
492                 ubifs_pack_lsave(c, buf + offs, c->lsave);
493                 offs += c->lsave_sz;
494                 dbg_chk_lpt_sz(c, 1, c->lsave_sz);
495         }
496
497         /* Make sure to place LPT's own lprops table */
498         if (!done_ltab) {
499                 if (offs + c->ltab_sz > c->leb_size) {
500                         wlen = offs - from;
501                         alen = ALIGN(wlen, c->min_io_size);
502                         memset(buf + offs, 0xff, alen - wlen);
503                         err = ubifs_leb_write(c, lnum, buf + from, from, alen);
504                         if (err)
505                                 return err;
506                         dbg_chk_lpt_sz(c, 2, c->leb_size - offs);
507                         err = realloc_lpt_leb(c, &lnum);
508                         if (err)
509                                 goto no_space;
510                         offs = from = 0;
511                         ubifs_assert(c, lnum >= c->lpt_first &&
512                                      lnum <= c->lpt_last);
513                         err = ubifs_leb_unmap(c, lnum);
514                         if (err)
515                                 return err;
516                 }
517                 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt);
518                 offs += c->ltab_sz;
519                 dbg_chk_lpt_sz(c, 1, c->ltab_sz);
520         }
521
522         /* Write remaining data in buffer */
523         wlen = offs - from;
524         alen = ALIGN(wlen, c->min_io_size);
525         memset(buf + offs, 0xff, alen - wlen);
526         err = ubifs_leb_write(c, lnum, buf + from, from, alen);
527         if (err)
528                 return err;
529
530         dbg_chk_lpt_sz(c, 4, alen - wlen);
531         err = dbg_chk_lpt_sz(c, 3, ALIGN(offs, c->min_io_size));
532         if (err)
533                 return err;
534
535         c->nhead_lnum = lnum;
536         c->nhead_offs = ALIGN(offs, c->min_io_size);
537
538         dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs);
539         dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs);
540         dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs);
541         if (c->big_lpt)
542                 dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs);
543
544         return 0;
545
546 no_space:
547         ubifs_err(c, "LPT out of space mismatch at LEB %d:%d needing %d, done_ltab %d, done_lsave %d",
548                   lnum, offs, len, done_ltab, done_lsave);
549         ubifs_dump_lpt_info(c);
550         ubifs_dump_lpt_lebs(c);
551         dump_stack();
552         return err;
553 }
554
555 /**
556  * next_pnode_to_dirty - find next pnode to dirty.
557  * @c: UBIFS file-system description object
558  * @pnode: pnode
559  *
560  * This function returns the next pnode to dirty or %NULL if there are no more
561  * pnodes.  Note that pnodes that have never been written (lnum == 0) are
562  * skipped.
563  */
564 static struct ubifs_pnode *next_pnode_to_dirty(struct ubifs_info *c,
565                                                struct ubifs_pnode *pnode)
566 {
567         struct ubifs_nnode *nnode;
568         int iip;
569
570         /* Try to go right */
571         nnode = pnode->parent;
572         for (iip = pnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) {
573                 if (nnode->nbranch[iip].lnum)
574                         return ubifs_get_pnode(c, nnode, iip);
575         }
576
577         /* Go up while can't go right */
578         do {
579                 iip = nnode->iip + 1;
580                 nnode = nnode->parent;
581                 if (!nnode)
582                         return NULL;
583                 for (; iip < UBIFS_LPT_FANOUT; iip++) {
584                         if (nnode->nbranch[iip].lnum)
585                                 break;
586                 }
587         } while (iip >= UBIFS_LPT_FANOUT);
588
589         /* Go right */
590         nnode = ubifs_get_nnode(c, nnode, iip);
591         if (IS_ERR(nnode))
592                 return (void *)nnode;
593
594         /* Go down to level 1 */
595         while (nnode->level > 1) {
596                 for (iip = 0; iip < UBIFS_LPT_FANOUT; iip++) {
597                         if (nnode->nbranch[iip].lnum)
598                                 break;
599                 }
600                 if (iip >= UBIFS_LPT_FANOUT) {
601                         /*
602                          * Should not happen, but we need to keep going
603                          * if it does.
604                          */
605                         iip = 0;
606                 }
607                 nnode = ubifs_get_nnode(c, nnode, iip);
608                 if (IS_ERR(nnode))
609                         return (void *)nnode;
610         }
611
612         for (iip = 0; iip < UBIFS_LPT_FANOUT; iip++)
613                 if (nnode->nbranch[iip].lnum)
614                         break;
615         if (iip >= UBIFS_LPT_FANOUT)
616                 /* Should not happen, but we need to keep going if it does */
617                 iip = 0;
618         return ubifs_get_pnode(c, nnode, iip);
619 }
620
621 /**
622  * add_pnode_dirt - add dirty space to LPT LEB properties.
623  * @c: UBIFS file-system description object
624  * @pnode: pnode for which to add dirt
625  */
626 static void add_pnode_dirt(struct ubifs_info *c, struct ubifs_pnode *pnode)
627 {
628         ubifs_add_lpt_dirt(c, pnode->parent->nbranch[pnode->iip].lnum,
629                            c->pnode_sz);
630 }
631
632 /**
633  * do_make_pnode_dirty - mark a pnode dirty.
634  * @c: UBIFS file-system description object
635  * @pnode: pnode to mark dirty
636  */
637 static void do_make_pnode_dirty(struct ubifs_info *c, struct ubifs_pnode *pnode)
638 {
639         /* Assumes cnext list is empty i.e. not called during commit */
640         if (!test_and_set_bit(DIRTY_CNODE, &pnode->flags)) {
641                 struct ubifs_nnode *nnode;
642
643                 c->dirty_pn_cnt += 1;
644                 add_pnode_dirt(c, pnode);
645                 /* Mark parent and ancestors dirty too */
646                 nnode = pnode->parent;
647                 while (nnode) {
648                         if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
649                                 c->dirty_nn_cnt += 1;
650                                 ubifs_add_nnode_dirt(c, nnode);
651                                 nnode = nnode->parent;
652                         } else
653                                 break;
654                 }
655         }
656 }
657
658 /**
659  * make_tree_dirty - mark the entire LEB properties tree dirty.
660  * @c: UBIFS file-system description object
661  *
662  * This function is used by the "small" LPT model to cause the entire LEB
663  * properties tree to be written.  The "small" LPT model does not use LPT
664  * garbage collection because it is more efficient to write the entire tree
665  * (because it is small).
666  *
667  * This function returns %0 on success and a negative error code on failure.
668  */
669 static int make_tree_dirty(struct ubifs_info *c)
670 {
671         struct ubifs_pnode *pnode;
672
673         pnode = ubifs_pnode_lookup(c, 0);
674         if (IS_ERR(pnode))
675                 return PTR_ERR(pnode);
676
677         while (pnode) {
678                 do_make_pnode_dirty(c, pnode);
679                 pnode = next_pnode_to_dirty(c, pnode);
680                 if (IS_ERR(pnode))
681                         return PTR_ERR(pnode);
682         }
683         return 0;
684 }
685
686 /**
687  * need_write_all - determine if the LPT area is running out of free space.
688  * @c: UBIFS file-system description object
689  *
690  * This function returns %1 if the LPT area is running out of free space and %0
691  * if it is not.
692  */
693 static int need_write_all(struct ubifs_info *c)
694 {
695         long long free = 0;
696         int i;
697
698         for (i = 0; i < c->lpt_lebs; i++) {
699                 if (i + c->lpt_first == c->nhead_lnum)
700                         free += c->leb_size - c->nhead_offs;
701                 else if (c->ltab[i].free == c->leb_size)
702                         free += c->leb_size;
703                 else if (c->ltab[i].free + c->ltab[i].dirty == c->leb_size)
704                         free += c->leb_size;
705         }
706         /* Less than twice the size left */
707         if (free <= c->lpt_sz * 2)
708                 return 1;
709         return 0;
710 }
711
712 /**
713  * lpt_tgc_start - start trivial garbage collection of LPT LEBs.
714  * @c: UBIFS file-system description object
715  *
716  * LPT trivial garbage collection is where a LPT LEB contains only dirty and
717  * free space and so may be reused as soon as the next commit is completed.
718  * This function is called during start commit to mark LPT LEBs for trivial GC.
719  */
720 static void lpt_tgc_start(struct ubifs_info *c)
721 {
722         int i;
723
724         for (i = 0; i < c->lpt_lebs; i++) {
725                 if (i + c->lpt_first == c->nhead_lnum)
726                         continue;
727                 if (c->ltab[i].dirty > 0 &&
728                     c->ltab[i].free + c->ltab[i].dirty == c->leb_size) {
729                         c->ltab[i].tgc = 1;
730                         c->ltab[i].free = c->leb_size;
731                         c->ltab[i].dirty = 0;
732                         dbg_lp("LEB %d", i + c->lpt_first);
733                 }
734         }
735 }
736
737 /**
738  * lpt_tgc_end - end trivial garbage collection of LPT LEBs.
739  * @c: UBIFS file-system description object
740  *
741  * LPT trivial garbage collection is where a LPT LEB contains only dirty and
742  * free space and so may be reused as soon as the next commit is completed.
743  * This function is called after the commit is completed (master node has been
744  * written) and un-maps LPT LEBs that were marked for trivial GC.
745  */
746 static int lpt_tgc_end(struct ubifs_info *c)
747 {
748         int i, err;
749
750         for (i = 0; i < c->lpt_lebs; i++)
751                 if (c->ltab[i].tgc) {
752                         err = ubifs_leb_unmap(c, i + c->lpt_first);
753                         if (err)
754                                 return err;
755                         c->ltab[i].tgc = 0;
756                         dbg_lp("LEB %d", i + c->lpt_first);
757                 }
758         return 0;
759 }
760
761 /**
762  * populate_lsave - fill the lsave array with important LEB numbers.
763  * @c: the UBIFS file-system description object
764  *
765  * This function is only called for the "big" model. It records a small number
766  * of LEB numbers of important LEBs.  Important LEBs are ones that are (from
767  * most important to least important): empty, freeable, freeable index, dirty
768  * index, dirty or free. Upon mount, we read this list of LEB numbers and bring
769  * their pnodes into memory.  That will stop us from having to scan the LPT
770  * straight away. For the "small" model we assume that scanning the LPT is no
771  * big deal.
772  */
773 static void populate_lsave(struct ubifs_info *c)
774 {
775         struct ubifs_lprops *lprops;
776         struct ubifs_lpt_heap *heap;
777         int i, cnt = 0;
778
779         ubifs_assert(c, c->big_lpt);
780         if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) {
781                 c->lpt_drty_flgs |= LSAVE_DIRTY;
782                 ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz);
783         }
784
785         if (dbg_populate_lsave(c))
786                 return;
787
788         list_for_each_entry(lprops, &c->empty_list, list) {
789                 c->lsave[cnt++] = lprops->lnum;
790                 if (cnt >= c->lsave_cnt)
791                         return;
792         }
793         list_for_each_entry(lprops, &c->freeable_list, list) {
794                 c->lsave[cnt++] = lprops->lnum;
795                 if (cnt >= c->lsave_cnt)
796                         return;
797         }
798         list_for_each_entry(lprops, &c->frdi_idx_list, list) {
799                 c->lsave[cnt++] = lprops->lnum;
800                 if (cnt >= c->lsave_cnt)
801                         return;
802         }
803         heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1];
804         for (i = 0; i < heap->cnt; i++) {
805                 c->lsave[cnt++] = heap->arr[i]->lnum;
806                 if (cnt >= c->lsave_cnt)
807                         return;
808         }
809         heap = &c->lpt_heap[LPROPS_DIRTY - 1];
810         for (i = 0; i < heap->cnt; i++) {
811                 c->lsave[cnt++] = heap->arr[i]->lnum;
812                 if (cnt >= c->lsave_cnt)
813                         return;
814         }
815         heap = &c->lpt_heap[LPROPS_FREE - 1];
816         for (i = 0; i < heap->cnt; i++) {
817                 c->lsave[cnt++] = heap->arr[i]->lnum;
818                 if (cnt >= c->lsave_cnt)
819                         return;
820         }
821         /* Fill it up completely */
822         while (cnt < c->lsave_cnt)
823                 c->lsave[cnt++] = c->main_first;
824 }
825
826 /**
827  * nnode_lookup - lookup a nnode in the LPT.
828  * @c: UBIFS file-system description object
829  * @i: nnode number
830  *
831  * This function returns a pointer to the nnode on success or a negative
832  * error code on failure.
833  */
834 static struct ubifs_nnode *nnode_lookup(struct ubifs_info *c, int i)
835 {
836         int err, iip;
837         struct ubifs_nnode *nnode;
838
839         if (!c->nroot) {
840                 err = ubifs_read_nnode(c, NULL, 0);
841                 if (err)
842                         return ERR_PTR(err);
843         }
844         nnode = c->nroot;
845         while (1) {
846                 iip = i & (UBIFS_LPT_FANOUT - 1);
847                 i >>= UBIFS_LPT_FANOUT_SHIFT;
848                 if (!i)
849                         break;
850                 nnode = ubifs_get_nnode(c, nnode, iip);
851                 if (IS_ERR(nnode))
852                         return nnode;
853         }
854         return nnode;
855 }
856
857 /**
858  * make_nnode_dirty - find a nnode and, if found, make it dirty.
859  * @c: UBIFS file-system description object
860  * @node_num: nnode number of nnode to make dirty
861  * @lnum: LEB number where nnode was written
862  * @offs: offset where nnode was written
863  *
864  * This function is used by LPT garbage collection.  LPT garbage collection is
865  * used only for the "big" LPT model (c->big_lpt == 1).  Garbage collection
866  * simply involves marking all the nodes in the LEB being garbage-collected as
867  * dirty.  The dirty nodes are written next commit, after which the LEB is free
868  * to be reused.
869  *
870  * This function returns %0 on success and a negative error code on failure.
871  */
872 static int make_nnode_dirty(struct ubifs_info *c, int node_num, int lnum,
873                             int offs)
874 {
875         struct ubifs_nnode *nnode;
876
877         nnode = nnode_lookup(c, node_num);
878         if (IS_ERR(nnode))
879                 return PTR_ERR(nnode);
880         if (nnode->parent) {
881                 struct ubifs_nbranch *branch;
882
883                 branch = &nnode->parent->nbranch[nnode->iip];
884                 if (branch->lnum != lnum || branch->offs != offs)
885                         return 0; /* nnode is obsolete */
886         } else if (c->lpt_lnum != lnum || c->lpt_offs != offs)
887                         return 0; /* nnode is obsolete */
888         /* Assumes cnext list is empty i.e. not called during commit */
889         if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
890                 c->dirty_nn_cnt += 1;
891                 ubifs_add_nnode_dirt(c, nnode);
892                 /* Mark parent and ancestors dirty too */
893                 nnode = nnode->parent;
894                 while (nnode) {
895                         if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) {
896                                 c->dirty_nn_cnt += 1;
897                                 ubifs_add_nnode_dirt(c, nnode);
898                                 nnode = nnode->parent;
899                         } else
900                                 break;
901                 }
902         }
903         return 0;
904 }
905
906 /**
907  * make_pnode_dirty - find a pnode and, if found, make it dirty.
908  * @c: UBIFS file-system description object
909  * @node_num: pnode number of pnode to make dirty
910  * @lnum: LEB number where pnode was written
911  * @offs: offset where pnode was written
912  *
913  * This function is used by LPT garbage collection.  LPT garbage collection is
914  * used only for the "big" LPT model (c->big_lpt == 1).  Garbage collection
915  * simply involves marking all the nodes in the LEB being garbage-collected as
916  * dirty.  The dirty nodes are written next commit, after which the LEB is free
917  * to be reused.
918  *
919  * This function returns %0 on success and a negative error code on failure.
920  */
921 static int make_pnode_dirty(struct ubifs_info *c, int node_num, int lnum,
922                             int offs)
923 {
924         struct ubifs_pnode *pnode;
925         struct ubifs_nbranch *branch;
926
927         pnode = ubifs_pnode_lookup(c, node_num);
928         if (IS_ERR(pnode))
929                 return PTR_ERR(pnode);
930         branch = &pnode->parent->nbranch[pnode->iip];
931         if (branch->lnum != lnum || branch->offs != offs)
932                 return 0;
933         do_make_pnode_dirty(c, pnode);
934         return 0;
935 }
936
937 /**
938  * make_ltab_dirty - make ltab node dirty.
939  * @c: UBIFS file-system description object
940  * @lnum: LEB number where ltab was written
941  * @offs: offset where ltab was written
942  *
943  * This function is used by LPT garbage collection.  LPT garbage collection is
944  * used only for the "big" LPT model (c->big_lpt == 1).  Garbage collection
945  * simply involves marking all the nodes in the LEB being garbage-collected as
946  * dirty.  The dirty nodes are written next commit, after which the LEB is free
947  * to be reused.
948  *
949  * This function returns %0 on success and a negative error code on failure.
950  */
951 static int make_ltab_dirty(struct ubifs_info *c, int lnum, int offs)
952 {
953         if (lnum != c->ltab_lnum || offs != c->ltab_offs)
954                 return 0; /* This ltab node is obsolete */
955         if (!(c->lpt_drty_flgs & LTAB_DIRTY)) {
956                 c->lpt_drty_flgs |= LTAB_DIRTY;
957                 ubifs_add_lpt_dirt(c, c->ltab_lnum, c->ltab_sz);
958         }
959         return 0;
960 }
961
962 /**
963  * make_lsave_dirty - make lsave node dirty.
964  * @c: UBIFS file-system description object
965  * @lnum: LEB number where lsave was written
966  * @offs: offset where lsave was written
967  *
968  * This function is used by LPT garbage collection.  LPT garbage collection is
969  * used only for the "big" LPT model (c->big_lpt == 1).  Garbage collection
970  * simply involves marking all the nodes in the LEB being garbage-collected as
971  * dirty.  The dirty nodes are written next commit, after which the LEB is free
972  * to be reused.
973  *
974  * This function returns %0 on success and a negative error code on failure.
975  */
976 static int make_lsave_dirty(struct ubifs_info *c, int lnum, int offs)
977 {
978         if (lnum != c->lsave_lnum || offs != c->lsave_offs)
979                 return 0; /* This lsave node is obsolete */
980         if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) {
981                 c->lpt_drty_flgs |= LSAVE_DIRTY;
982                 ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz);
983         }
984         return 0;
985 }
986
987 /**
988  * make_node_dirty - make node dirty.
989  * @c: UBIFS file-system description object
990  * @node_type: LPT node type
991  * @node_num: node number
992  * @lnum: LEB number where node was written
993  * @offs: offset where node was written
994  *
995  * This function is used by LPT garbage collection.  LPT garbage collection is
996  * used only for the "big" LPT model (c->big_lpt == 1).  Garbage collection
997  * simply involves marking all the nodes in the LEB being garbage-collected as
998  * dirty.  The dirty nodes are written next commit, after which the LEB is free
999  * to be reused.
1000  *
1001  * This function returns %0 on success and a negative error code on failure.
1002  */
1003 static int make_node_dirty(struct ubifs_info *c, int node_type, int node_num,
1004                            int lnum, int offs)
1005 {
1006         switch (node_type) {
1007         case UBIFS_LPT_NNODE:
1008                 return make_nnode_dirty(c, node_num, lnum, offs);
1009         case UBIFS_LPT_PNODE:
1010                 return make_pnode_dirty(c, node_num, lnum, offs);
1011         case UBIFS_LPT_LTAB:
1012                 return make_ltab_dirty(c, lnum, offs);
1013         case UBIFS_LPT_LSAVE:
1014                 return make_lsave_dirty(c, lnum, offs);
1015         }
1016         return -EINVAL;
1017 }
1018
1019 /**
1020  * get_lpt_node_len - return the length of a node based on its type.
1021  * @c: UBIFS file-system description object
1022  * @node_type: LPT node type
1023  */
1024 static int get_lpt_node_len(const struct ubifs_info *c, int node_type)
1025 {
1026         switch (node_type) {
1027         case UBIFS_LPT_NNODE:
1028                 return c->nnode_sz;
1029         case UBIFS_LPT_PNODE:
1030                 return c->pnode_sz;
1031         case UBIFS_LPT_LTAB:
1032                 return c->ltab_sz;
1033         case UBIFS_LPT_LSAVE:
1034                 return c->lsave_sz;
1035         }
1036         return 0;
1037 }
1038
1039 /**
1040  * get_pad_len - return the length of padding in a buffer.
1041  * @c: UBIFS file-system description object
1042  * @buf: buffer
1043  * @len: length of buffer
1044  */
1045 static int get_pad_len(const struct ubifs_info *c, uint8_t *buf, int len)
1046 {
1047         int offs, pad_len;
1048
1049         if (c->min_io_size == 1)
1050                 return 0;
1051         offs = c->leb_size - len;
1052         pad_len = ALIGN(offs, c->min_io_size) - offs;
1053         return pad_len;
1054 }
1055
1056 /**
1057  * get_lpt_node_type - return type (and node number) of a node in a buffer.
1058  * @c: UBIFS file-system description object
1059  * @buf: buffer
1060  * @node_num: node number is returned here
1061  */
1062 static int get_lpt_node_type(const struct ubifs_info *c, uint8_t *buf,
1063                              int *node_num)
1064 {
1065         uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1066         int pos = 0, node_type;
1067
1068         node_type = ubifs_unpack_bits(c, &addr, &pos, UBIFS_LPT_TYPE_BITS);
1069         *node_num = ubifs_unpack_bits(c, &addr, &pos, c->pcnt_bits);
1070         return node_type;
1071 }
1072
1073 /**
1074  * is_a_node - determine if a buffer contains a node.
1075  * @c: UBIFS file-system description object
1076  * @buf: buffer
1077  * @len: length of buffer
1078  *
1079  * This function returns %1 if the buffer contains a node or %0 if it does not.
1080  */
1081 static int is_a_node(const struct ubifs_info *c, uint8_t *buf, int len)
1082 {
1083         uint8_t *addr = buf + UBIFS_LPT_CRC_BYTES;
1084         int pos = 0, node_type, node_len;
1085         uint16_t crc, calc_crc;
1086
1087         if (len < UBIFS_LPT_CRC_BYTES + (UBIFS_LPT_TYPE_BITS + 7) / 8)
1088                 return 0;
1089         node_type = ubifs_unpack_bits(c, &addr, &pos, UBIFS_LPT_TYPE_BITS);
1090         if (node_type == UBIFS_LPT_NOT_A_NODE)
1091                 return 0;
1092         node_len = get_lpt_node_len(c, node_type);
1093         if (!node_len || node_len > len)
1094                 return 0;
1095         pos = 0;
1096         addr = buf;
1097         crc = ubifs_unpack_bits(c, &addr, &pos, UBIFS_LPT_CRC_BITS);
1098         calc_crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES,
1099                          node_len - UBIFS_LPT_CRC_BYTES);
1100         if (crc != calc_crc)
1101                 return 0;
1102         return 1;
1103 }
1104
1105 /**
1106  * lpt_gc_lnum - garbage collect a LPT LEB.
1107  * @c: UBIFS file-system description object
1108  * @lnum: LEB number to garbage collect
1109  *
1110  * LPT garbage collection is used only for the "big" LPT model
1111  * (c->big_lpt == 1).  Garbage collection simply involves marking all the nodes
1112  * in the LEB being garbage-collected as dirty.  The dirty nodes are written
1113  * next commit, after which the LEB is free to be reused.
1114  *
1115  * This function returns %0 on success and a negative error code on failure.
1116  */
1117 static int lpt_gc_lnum(struct ubifs_info *c, int lnum)
1118 {
1119         int err, len = c->leb_size, node_type, node_num, node_len, offs;
1120         void *buf = c->lpt_buf;
1121
1122         dbg_lp("LEB %d", lnum);
1123
1124         err = ubifs_leb_read(c, lnum, buf, 0, c->leb_size, 1);
1125         if (err)
1126                 return err;
1127
1128         while (1) {
1129                 if (!is_a_node(c, buf, len)) {
1130                         int pad_len;
1131
1132                         pad_len = get_pad_len(c, buf, len);
1133                         if (pad_len) {
1134                                 buf += pad_len;
1135                                 len -= pad_len;
1136                                 continue;
1137                         }
1138                         return 0;
1139                 }
1140                 node_type = get_lpt_node_type(c, buf, &node_num);
1141                 node_len = get_lpt_node_len(c, node_type);
1142                 offs = c->leb_size - len;
1143                 ubifs_assert(c, node_len != 0);
1144                 mutex_lock(&c->lp_mutex);
1145                 err = make_node_dirty(c, node_type, node_num, lnum, offs);
1146                 mutex_unlock(&c->lp_mutex);
1147                 if (err)
1148                         return err;
1149                 buf += node_len;
1150                 len -= node_len;
1151         }
1152         return 0;
1153 }
1154
1155 /**
1156  * lpt_gc - LPT garbage collection.
1157  * @c: UBIFS file-system description object
1158  *
1159  * Select a LPT LEB for LPT garbage collection and call 'lpt_gc_lnum()'.
1160  * Returns %0 on success and a negative error code on failure.
1161  */
1162 static int lpt_gc(struct ubifs_info *c)
1163 {
1164         int i, lnum = -1, dirty = 0;
1165
1166         mutex_lock(&c->lp_mutex);
1167         for (i = 0; i < c->lpt_lebs; i++) {
1168                 ubifs_assert(c, !c->ltab[i].tgc);
1169                 if (i + c->lpt_first == c->nhead_lnum ||
1170                     c->ltab[i].free + c->ltab[i].dirty == c->leb_size)
1171                         continue;
1172                 if (c->ltab[i].dirty > dirty) {
1173                         dirty = c->ltab[i].dirty;
1174                         lnum = i + c->lpt_first;
1175                 }
1176         }
1177         mutex_unlock(&c->lp_mutex);
1178         if (lnum == -1)
1179                 return -ENOSPC;
1180         return lpt_gc_lnum(c, lnum);
1181 }
1182
1183 /**
1184  * ubifs_lpt_start_commit - UBIFS commit starts.
1185  * @c: the UBIFS file-system description object
1186  *
1187  * This function has to be called when UBIFS starts the commit operation.
1188  * This function "freezes" all currently dirty LEB properties and does not
1189  * change them anymore. Further changes are saved and tracked separately
1190  * because they are not part of this commit. This function returns zero in case
1191  * of success and a negative error code in case of failure.
1192  */
1193 int ubifs_lpt_start_commit(struct ubifs_info *c)
1194 {
1195         int err, cnt;
1196
1197         dbg_lp("");
1198
1199         mutex_lock(&c->lp_mutex);
1200         err = dbg_chk_lpt_free_spc(c);
1201         if (err)
1202                 goto out;
1203         err = dbg_check_ltab(c);
1204         if (err)
1205                 goto out;
1206
1207         if (c->check_lpt_free) {
1208                 /*
1209                  * We ensure there is enough free space in
1210                  * ubifs_lpt_post_commit() by marking nodes dirty. That
1211                  * information is lost when we unmount, so we also need
1212                  * to check free space once after mounting also.
1213                  */
1214                 c->check_lpt_free = 0;
1215                 while (need_write_all(c)) {
1216                         mutex_unlock(&c->lp_mutex);
1217                         err = lpt_gc(c);
1218                         if (err)
1219                                 return err;
1220                         mutex_lock(&c->lp_mutex);
1221                 }
1222         }
1223
1224         lpt_tgc_start(c);
1225
1226         if (!c->dirty_pn_cnt) {
1227                 dbg_cmt("no cnodes to commit");
1228                 err = 0;
1229                 goto out;
1230         }
1231
1232         if (!c->big_lpt && need_write_all(c)) {
1233                 /* If needed, write everything */
1234                 err = make_tree_dirty(c);
1235                 if (err)
1236                         goto out;
1237                 lpt_tgc_start(c);
1238         }
1239
1240         if (c->big_lpt)
1241                 populate_lsave(c);
1242
1243         cnt = get_cnodes_to_commit(c);
1244         ubifs_assert(c, cnt != 0);
1245
1246         err = layout_cnodes(c);
1247         if (err)
1248                 goto out;
1249
1250         err = ubifs_lpt_calc_hash(c, c->mst_node->hash_lpt);
1251         if (err)
1252                 goto out;
1253
1254         /* Copy the LPT's own lprops for end commit to write */
1255         memcpy(c->ltab_cmt, c->ltab,
1256                sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs);
1257         c->lpt_drty_flgs &= ~(LTAB_DIRTY | LSAVE_DIRTY);
1258
1259 out:
1260         mutex_unlock(&c->lp_mutex);
1261         return err;
1262 }
1263
1264 /**
1265  * free_obsolete_cnodes - free obsolete cnodes for commit end.
1266  * @c: UBIFS file-system description object
1267  */
1268 static void free_obsolete_cnodes(struct ubifs_info *c)
1269 {
1270         struct ubifs_cnode *cnode, *cnext;
1271
1272         cnext = c->lpt_cnext;
1273         if (!cnext)
1274                 return;
1275         do {
1276                 cnode = cnext;
1277                 cnext = cnode->cnext;
1278                 if (test_bit(OBSOLETE_CNODE, &cnode->flags))
1279                         kfree(cnode);
1280                 else
1281                         cnode->cnext = NULL;
1282         } while (cnext != c->lpt_cnext);
1283         c->lpt_cnext = NULL;
1284 }
1285
1286 /**
1287  * ubifs_lpt_end_commit - finish the commit operation.
1288  * @c: the UBIFS file-system description object
1289  *
1290  * This function has to be called when the commit operation finishes. It
1291  * flushes the changes which were "frozen" by 'ubifs_lprops_start_commit()' to
1292  * the media. Returns zero in case of success and a negative error code in case
1293  * of failure.
1294  */
1295 int ubifs_lpt_end_commit(struct ubifs_info *c)
1296 {
1297         int err;
1298
1299         dbg_lp("");
1300
1301         if (!c->lpt_cnext)
1302                 return 0;
1303
1304         err = write_cnodes(c);
1305         if (err)
1306                 return err;
1307
1308         mutex_lock(&c->lp_mutex);
1309         free_obsolete_cnodes(c);
1310         mutex_unlock(&c->lp_mutex);
1311
1312         return 0;
1313 }
1314
1315 /**
1316  * ubifs_lpt_post_commit - post commit LPT trivial GC and LPT GC.
1317  * @c: UBIFS file-system description object
1318  *
1319  * LPT trivial GC is completed after a commit. Also LPT GC is done after a
1320  * commit for the "big" LPT model.
1321  */
1322 int ubifs_lpt_post_commit(struct ubifs_info *c)
1323 {
1324         int err;
1325
1326         mutex_lock(&c->lp_mutex);
1327         err = lpt_tgc_end(c);
1328         if (err)
1329                 goto out;
1330         if (c->big_lpt)
1331                 while (need_write_all(c)) {
1332                         mutex_unlock(&c->lp_mutex);
1333                         err = lpt_gc(c);
1334                         if (err)
1335                                 return err;
1336                         mutex_lock(&c->lp_mutex);
1337                 }
1338 out:
1339         mutex_unlock(&c->lp_mutex);
1340         return err;
1341 }
1342
1343 /**
1344  * first_nnode - find the first nnode in memory.
1345  * @c: UBIFS file-system description object
1346  * @hght: height of tree where nnode found is returned here
1347  *
1348  * This function returns a pointer to the nnode found or %NULL if no nnode is
1349  * found. This function is a helper to 'ubifs_lpt_free()'.
1350  */
1351 static struct ubifs_nnode *first_nnode(struct ubifs_info *c, int *hght)
1352 {
1353         struct ubifs_nnode *nnode;
1354         int h, i, found;
1355
1356         nnode = c->nroot;
1357         *hght = 0;
1358         if (!nnode)
1359                 return NULL;
1360         for (h = 1; h < c->lpt_hght; h++) {
1361                 found = 0;
1362                 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1363                         if (nnode->nbranch[i].nnode) {
1364                                 found = 1;
1365                                 nnode = nnode->nbranch[i].nnode;
1366                                 *hght = h;
1367                                 break;
1368                         }
1369                 }
1370                 if (!found)
1371                         break;
1372         }
1373         return nnode;
1374 }
1375
1376 /**
1377  * next_nnode - find the next nnode in memory.
1378  * @c: UBIFS file-system description object
1379  * @nnode: nnode from which to start.
1380  * @hght: height of tree where nnode is, is passed and returned here
1381  *
1382  * This function returns a pointer to the nnode found or %NULL if no nnode is
1383  * found. This function is a helper to 'ubifs_lpt_free()'.
1384  */
1385 static struct ubifs_nnode *next_nnode(struct ubifs_info *c,
1386                                       struct ubifs_nnode *nnode, int *hght)
1387 {
1388         struct ubifs_nnode *parent;
1389         int iip, h, i, found;
1390
1391         parent = nnode->parent;
1392         if (!parent)
1393                 return NULL;
1394         if (nnode->iip == UBIFS_LPT_FANOUT - 1) {
1395                 *hght -= 1;
1396                 return parent;
1397         }
1398         for (iip = nnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) {
1399                 nnode = parent->nbranch[iip].nnode;
1400                 if (nnode)
1401                         break;
1402         }
1403         if (!nnode) {
1404                 *hght -= 1;
1405                 return parent;
1406         }
1407         for (h = *hght + 1; h < c->lpt_hght; h++) {
1408                 found = 0;
1409                 for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1410                         if (nnode->nbranch[i].nnode) {
1411                                 found = 1;
1412                                 nnode = nnode->nbranch[i].nnode;
1413                                 *hght = h;
1414                                 break;
1415                         }
1416                 }
1417                 if (!found)
1418                         break;
1419         }
1420         return nnode;
1421 }
1422
1423 /**
1424  * ubifs_lpt_free - free resources owned by the LPT.
1425  * @c: UBIFS file-system description object
1426  * @wr_only: free only resources used for writing
1427  */
1428 void ubifs_lpt_free(struct ubifs_info *c, int wr_only)
1429 {
1430         struct ubifs_nnode *nnode;
1431         int i, hght;
1432
1433         /* Free write-only things first */
1434
1435         free_obsolete_cnodes(c); /* Leftover from a failed commit */
1436
1437         vfree(c->ltab_cmt);
1438         c->ltab_cmt = NULL;
1439         vfree(c->lpt_buf);
1440         c->lpt_buf = NULL;
1441         kfree(c->lsave);
1442         c->lsave = NULL;
1443
1444         if (wr_only)
1445                 return;
1446
1447         /* Now free the rest */
1448
1449         nnode = first_nnode(c, &hght);
1450         while (nnode) {
1451                 for (i = 0; i < UBIFS_LPT_FANOUT; i++)
1452                         kfree(nnode->nbranch[i].nnode);
1453                 nnode = next_nnode(c, nnode, &hght);
1454         }
1455         for (i = 0; i < LPROPS_HEAP_CNT; i++)
1456                 kfree(c->lpt_heap[i].arr);
1457         kfree(c->dirty_idx.arr);
1458         kfree(c->nroot);
1459         vfree(c->ltab);
1460         kfree(c->lpt_nod_buf);
1461 }
1462
1463 /*
1464  * Everything below is related to debugging.
1465  */
1466
1467 /**
1468  * dbg_is_all_ff - determine if a buffer contains only 0xFF bytes.
1469  * @buf: buffer
1470  * @len: buffer length
1471  */
1472 static int dbg_is_all_ff(uint8_t *buf, int len)
1473 {
1474         int i;
1475
1476         for (i = 0; i < len; i++)
1477                 if (buf[i] != 0xff)
1478                         return 0;
1479         return 1;
1480 }
1481
1482 /**
1483  * dbg_is_nnode_dirty - determine if a nnode is dirty.
1484  * @c: the UBIFS file-system description object
1485  * @lnum: LEB number where nnode was written
1486  * @offs: offset where nnode was written
1487  */
1488 static int dbg_is_nnode_dirty(struct ubifs_info *c, int lnum, int offs)
1489 {
1490         struct ubifs_nnode *nnode;
1491         int hght;
1492
1493         /* Entire tree is in memory so first_nnode / next_nnode are OK */
1494         nnode = first_nnode(c, &hght);
1495         for (; nnode; nnode = next_nnode(c, nnode, &hght)) {
1496                 struct ubifs_nbranch *branch;
1497
1498                 cond_resched();
1499                 if (nnode->parent) {
1500                         branch = &nnode->parent->nbranch[nnode->iip];
1501                         if (branch->lnum != lnum || branch->offs != offs)
1502                                 continue;
1503                         if (test_bit(DIRTY_CNODE, &nnode->flags))
1504                                 return 1;
1505                         return 0;
1506                 } else {
1507                         if (c->lpt_lnum != lnum || c->lpt_offs != offs)
1508                                 continue;
1509                         if (test_bit(DIRTY_CNODE, &nnode->flags))
1510                                 return 1;
1511                         return 0;
1512                 }
1513         }
1514         return 1;
1515 }
1516
1517 /**
1518  * dbg_is_pnode_dirty - determine if a pnode is dirty.
1519  * @c: the UBIFS file-system description object
1520  * @lnum: LEB number where pnode was written
1521  * @offs: offset where pnode was written
1522  */
1523 static int dbg_is_pnode_dirty(struct ubifs_info *c, int lnum, int offs)
1524 {
1525         int i, cnt;
1526
1527         cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
1528         for (i = 0; i < cnt; i++) {
1529                 struct ubifs_pnode *pnode;
1530                 struct ubifs_nbranch *branch;
1531
1532                 cond_resched();
1533                 pnode = ubifs_pnode_lookup(c, i);
1534                 if (IS_ERR(pnode))
1535                         return PTR_ERR(pnode);
1536                 branch = &pnode->parent->nbranch[pnode->iip];
1537                 if (branch->lnum != lnum || branch->offs != offs)
1538                         continue;
1539                 if (test_bit(DIRTY_CNODE, &pnode->flags))
1540                         return 1;
1541                 return 0;
1542         }
1543         return 1;
1544 }
1545
1546 /**
1547  * dbg_is_ltab_dirty - determine if a ltab node is dirty.
1548  * @c: the UBIFS file-system description object
1549  * @lnum: LEB number where ltab node was written
1550  * @offs: offset where ltab node was written
1551  */
1552 static int dbg_is_ltab_dirty(struct ubifs_info *c, int lnum, int offs)
1553 {
1554         if (lnum != c->ltab_lnum || offs != c->ltab_offs)
1555                 return 1;
1556         return (c->lpt_drty_flgs & LTAB_DIRTY) != 0;
1557 }
1558
1559 /**
1560  * dbg_is_lsave_dirty - determine if a lsave node is dirty.
1561  * @c: the UBIFS file-system description object
1562  * @lnum: LEB number where lsave node was written
1563  * @offs: offset where lsave node was written
1564  */
1565 static int dbg_is_lsave_dirty(struct ubifs_info *c, int lnum, int offs)
1566 {
1567         if (lnum != c->lsave_lnum || offs != c->lsave_offs)
1568                 return 1;
1569         return (c->lpt_drty_flgs & LSAVE_DIRTY) != 0;
1570 }
1571
1572 /**
1573  * dbg_is_node_dirty - determine if a node is dirty.
1574  * @c: the UBIFS file-system description object
1575  * @node_type: node type
1576  * @lnum: LEB number where node was written
1577  * @offs: offset where node was written
1578  */
1579 static int dbg_is_node_dirty(struct ubifs_info *c, int node_type, int lnum,
1580                              int offs)
1581 {
1582         switch (node_type) {
1583         case UBIFS_LPT_NNODE:
1584                 return dbg_is_nnode_dirty(c, lnum, offs);
1585         case UBIFS_LPT_PNODE:
1586                 return dbg_is_pnode_dirty(c, lnum, offs);
1587         case UBIFS_LPT_LTAB:
1588                 return dbg_is_ltab_dirty(c, lnum, offs);
1589         case UBIFS_LPT_LSAVE:
1590                 return dbg_is_lsave_dirty(c, lnum, offs);
1591         }
1592         return 1;
1593 }
1594
1595 /**
1596  * dbg_check_ltab_lnum - check the ltab for a LPT LEB number.
1597  * @c: the UBIFS file-system description object
1598  * @lnum: LEB number where node was written
1599  *
1600  * This function returns %0 on success and a negative error code on failure.
1601  */
1602 static int dbg_check_ltab_lnum(struct ubifs_info *c, int lnum)
1603 {
1604         int err, len = c->leb_size, dirty = 0, node_type, node_num, node_len;
1605         int ret;
1606         void *buf, *p;
1607
1608         if (!dbg_is_chk_lprops(c))
1609                 return 0;
1610
1611         buf = p = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
1612         if (!buf) {
1613                 ubifs_err(c, "cannot allocate memory for ltab checking");
1614                 return 0;
1615         }
1616
1617         dbg_lp("LEB %d", lnum);
1618
1619         err = ubifs_leb_read(c, lnum, buf, 0, c->leb_size, 1);
1620         if (err)
1621                 goto out;
1622
1623         while (1) {
1624                 if (!is_a_node(c, p, len)) {
1625                         int i, pad_len;
1626
1627                         pad_len = get_pad_len(c, p, len);
1628                         if (pad_len) {
1629                                 p += pad_len;
1630                                 len -= pad_len;
1631                                 dirty += pad_len;
1632                                 continue;
1633                         }
1634                         if (!dbg_is_all_ff(p, len)) {
1635                                 ubifs_err(c, "invalid empty space in LEB %d at %d",
1636                                           lnum, c->leb_size - len);
1637                                 err = -EINVAL;
1638                         }
1639                         i = lnum - c->lpt_first;
1640                         if (len != c->ltab[i].free) {
1641                                 ubifs_err(c, "invalid free space in LEB %d (free %d, expected %d)",
1642                                           lnum, len, c->ltab[i].free);
1643                                 err = -EINVAL;
1644                         }
1645                         if (dirty != c->ltab[i].dirty) {
1646                                 ubifs_err(c, "invalid dirty space in LEB %d (dirty %d, expected %d)",
1647                                           lnum, dirty, c->ltab[i].dirty);
1648                                 err = -EINVAL;
1649                         }
1650                         goto out;
1651                 }
1652                 node_type = get_lpt_node_type(c, p, &node_num);
1653                 node_len = get_lpt_node_len(c, node_type);
1654                 ret = dbg_is_node_dirty(c, node_type, lnum, c->leb_size - len);
1655                 if (ret == 1)
1656                         dirty += node_len;
1657                 p += node_len;
1658                 len -= node_len;
1659         }
1660
1661         err = 0;
1662 out:
1663         vfree(buf);
1664         return err;
1665 }
1666
1667 /**
1668  * dbg_check_ltab - check the free and dirty space in the ltab.
1669  * @c: the UBIFS file-system description object
1670  *
1671  * This function returns %0 on success and a negative error code on failure.
1672  */
1673 int dbg_check_ltab(struct ubifs_info *c)
1674 {
1675         int lnum, err, i, cnt;
1676
1677         if (!dbg_is_chk_lprops(c))
1678                 return 0;
1679
1680         /* Bring the entire tree into memory */
1681         cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT);
1682         for (i = 0; i < cnt; i++) {
1683                 struct ubifs_pnode *pnode;
1684
1685                 pnode = ubifs_pnode_lookup(c, i);
1686                 if (IS_ERR(pnode))
1687                         return PTR_ERR(pnode);
1688                 cond_resched();
1689         }
1690
1691         /* Check nodes */
1692         err = dbg_check_lpt_nodes(c, (struct ubifs_cnode *)c->nroot, 0, 0);
1693         if (err)
1694                 return err;
1695
1696         /* Check each LEB */
1697         for (lnum = c->lpt_first; lnum <= c->lpt_last; lnum++) {
1698                 err = dbg_check_ltab_lnum(c, lnum);
1699                 if (err) {
1700                         ubifs_err(c, "failed at LEB %d", lnum);
1701                         return err;
1702                 }
1703         }
1704
1705         dbg_lp("succeeded");
1706         return 0;
1707 }
1708
1709 /**
1710  * dbg_chk_lpt_free_spc - check LPT free space is enough to write entire LPT.
1711  * @c: the UBIFS file-system description object
1712  *
1713  * This function returns %0 on success and a negative error code on failure.
1714  */
1715 int dbg_chk_lpt_free_spc(struct ubifs_info *c)
1716 {
1717         long long free = 0;
1718         int i;
1719
1720         if (!dbg_is_chk_lprops(c))
1721                 return 0;
1722
1723         for (i = 0; i < c->lpt_lebs; i++) {
1724                 if (c->ltab[i].tgc || c->ltab[i].cmt)
1725                         continue;
1726                 if (i + c->lpt_first == c->nhead_lnum)
1727                         free += c->leb_size - c->nhead_offs;
1728                 else if (c->ltab[i].free == c->leb_size)
1729                         free += c->leb_size;
1730         }
1731         if (free < c->lpt_sz) {
1732                 ubifs_err(c, "LPT space error: free %lld lpt_sz %lld",
1733                           free, c->lpt_sz);
1734                 ubifs_dump_lpt_info(c);
1735                 ubifs_dump_lpt_lebs(c);
1736                 dump_stack();
1737                 return -EINVAL;
1738         }
1739         return 0;
1740 }
1741
1742 /**
1743  * dbg_chk_lpt_sz - check LPT does not write more than LPT size.
1744  * @c: the UBIFS file-system description object
1745  * @action: what to do
1746  * @len: length written
1747  *
1748  * This function returns %0 on success and a negative error code on failure.
1749  * The @action argument may be one of:
1750  *   o %0 - LPT debugging checking starts, initialize debugging variables;
1751  *   o %1 - wrote an LPT node, increase LPT size by @len bytes;
1752  *   o %2 - switched to a different LEB and wasted @len bytes;
1753  *   o %3 - check that we've written the right number of bytes.
1754  *   o %4 - wasted @len bytes;
1755  */
1756 int dbg_chk_lpt_sz(struct ubifs_info *c, int action, int len)
1757 {
1758         struct ubifs_debug_info *d = c->dbg;
1759         long long chk_lpt_sz, lpt_sz;
1760         int err = 0;
1761
1762         if (!dbg_is_chk_lprops(c))
1763                 return 0;
1764
1765         switch (action) {
1766         case 0:
1767                 d->chk_lpt_sz = 0;
1768                 d->chk_lpt_sz2 = 0;
1769                 d->chk_lpt_lebs = 0;
1770                 d->chk_lpt_wastage = 0;
1771                 if (c->dirty_pn_cnt > c->pnode_cnt) {
1772                         ubifs_err(c, "dirty pnodes %d exceed max %d",
1773                                   c->dirty_pn_cnt, c->pnode_cnt);
1774                         err = -EINVAL;
1775                 }
1776                 if (c->dirty_nn_cnt > c->nnode_cnt) {
1777                         ubifs_err(c, "dirty nnodes %d exceed max %d",
1778                                   c->dirty_nn_cnt, c->nnode_cnt);
1779                         err = -EINVAL;
1780                 }
1781                 return err;
1782         case 1:
1783                 d->chk_lpt_sz += len;
1784                 return 0;
1785         case 2:
1786                 d->chk_lpt_sz += len;
1787                 d->chk_lpt_wastage += len;
1788                 d->chk_lpt_lebs += 1;
1789                 return 0;
1790         case 3:
1791                 chk_lpt_sz = c->leb_size;
1792                 chk_lpt_sz *= d->chk_lpt_lebs;
1793                 chk_lpt_sz += len - c->nhead_offs;
1794                 if (d->chk_lpt_sz != chk_lpt_sz) {
1795                         ubifs_err(c, "LPT wrote %lld but space used was %lld",
1796                                   d->chk_lpt_sz, chk_lpt_sz);
1797                         err = -EINVAL;
1798                 }
1799                 if (d->chk_lpt_sz > c->lpt_sz) {
1800                         ubifs_err(c, "LPT wrote %lld but lpt_sz is %lld",
1801                                   d->chk_lpt_sz, c->lpt_sz);
1802                         err = -EINVAL;
1803                 }
1804                 if (d->chk_lpt_sz2 && d->chk_lpt_sz != d->chk_lpt_sz2) {
1805                         ubifs_err(c, "LPT layout size %lld but wrote %lld",
1806                                   d->chk_lpt_sz, d->chk_lpt_sz2);
1807                         err = -EINVAL;
1808                 }
1809                 if (d->chk_lpt_sz2 && d->new_nhead_offs != len) {
1810                         ubifs_err(c, "LPT new nhead offs: expected %d was %d",
1811                                   d->new_nhead_offs, len);
1812                         err = -EINVAL;
1813                 }
1814                 lpt_sz = (long long)c->pnode_cnt * c->pnode_sz;
1815                 lpt_sz += (long long)c->nnode_cnt * c->nnode_sz;
1816                 lpt_sz += c->ltab_sz;
1817                 if (c->big_lpt)
1818                         lpt_sz += c->lsave_sz;
1819                 if (d->chk_lpt_sz - d->chk_lpt_wastage > lpt_sz) {
1820                         ubifs_err(c, "LPT chk_lpt_sz %lld + waste %lld exceeds %lld",
1821                                   d->chk_lpt_sz, d->chk_lpt_wastage, lpt_sz);
1822                         err = -EINVAL;
1823                 }
1824                 if (err) {
1825                         ubifs_dump_lpt_info(c);
1826                         ubifs_dump_lpt_lebs(c);
1827                         dump_stack();
1828                 }
1829                 d->chk_lpt_sz2 = d->chk_lpt_sz;
1830                 d->chk_lpt_sz = 0;
1831                 d->chk_lpt_wastage = 0;
1832                 d->chk_lpt_lebs = 0;
1833                 d->new_nhead_offs = len;
1834                 return err;
1835         case 4:
1836                 d->chk_lpt_sz += len;
1837                 d->chk_lpt_wastage += len;
1838                 return 0;
1839         default:
1840                 return -EINVAL;
1841         }
1842 }
1843
1844 /**
1845  * dump_lpt_leb - dump an LPT LEB.
1846  * @c: UBIFS file-system description object
1847  * @lnum: LEB number to dump
1848  *
1849  * This function dumps an LEB from LPT area. Nodes in this area are very
1850  * different to nodes in the main area (e.g., they do not have common headers,
1851  * they do not have 8-byte alignments, etc), so we have a separate function to
1852  * dump LPT area LEBs. Note, LPT has to be locked by the caller.
1853  */
1854 static void dump_lpt_leb(const struct ubifs_info *c, int lnum)
1855 {
1856         int err, len = c->leb_size, node_type, node_num, node_len, offs;
1857         void *buf, *p;
1858
1859         pr_err("(pid %d) start dumping LEB %d\n", current->pid, lnum);
1860         buf = p = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
1861         if (!buf) {
1862                 ubifs_err(c, "cannot allocate memory to dump LPT");
1863                 return;
1864         }
1865
1866         err = ubifs_leb_read(c, lnum, buf, 0, c->leb_size, 1);
1867         if (err)
1868                 goto out;
1869
1870         while (1) {
1871                 offs = c->leb_size - len;
1872                 if (!is_a_node(c, p, len)) {
1873                         int pad_len;
1874
1875                         pad_len = get_pad_len(c, p, len);
1876                         if (pad_len) {
1877                                 pr_err("LEB %d:%d, pad %d bytes\n",
1878                                        lnum, offs, pad_len);
1879                                 p += pad_len;
1880                                 len -= pad_len;
1881                                 continue;
1882                         }
1883                         if (len)
1884                                 pr_err("LEB %d:%d, free %d bytes\n",
1885                                        lnum, offs, len);
1886                         break;
1887                 }
1888
1889                 node_type = get_lpt_node_type(c, p, &node_num);
1890                 switch (node_type) {
1891                 case UBIFS_LPT_PNODE:
1892                 {
1893                         node_len = c->pnode_sz;
1894                         if (c->big_lpt)
1895                                 pr_err("LEB %d:%d, pnode num %d\n",
1896                                        lnum, offs, node_num);
1897                         else
1898                                 pr_err("LEB %d:%d, pnode\n", lnum, offs);
1899                         break;
1900                 }
1901                 case UBIFS_LPT_NNODE:
1902                 {
1903                         int i;
1904                         struct ubifs_nnode nnode;
1905
1906                         node_len = c->nnode_sz;
1907                         if (c->big_lpt)
1908                                 pr_err("LEB %d:%d, nnode num %d, ",
1909                                        lnum, offs, node_num);
1910                         else
1911                                 pr_err("LEB %d:%d, nnode, ",
1912                                        lnum, offs);
1913                         err = ubifs_unpack_nnode(c, p, &nnode);
1914                         if (err) {
1915                                 pr_err("failed to unpack_node, error %d\n",
1916                                        err);
1917                                 break;
1918                         }
1919                         for (i = 0; i < UBIFS_LPT_FANOUT; i++) {
1920                                 pr_cont("%d:%d", nnode.nbranch[i].lnum,
1921                                        nnode.nbranch[i].offs);
1922                                 if (i != UBIFS_LPT_FANOUT - 1)
1923                                         pr_cont(", ");
1924                         }
1925                         pr_cont("\n");
1926                         break;
1927                 }
1928                 case UBIFS_LPT_LTAB:
1929                         node_len = c->ltab_sz;
1930                         pr_err("LEB %d:%d, ltab\n", lnum, offs);
1931                         break;
1932                 case UBIFS_LPT_LSAVE:
1933                         node_len = c->lsave_sz;
1934                         pr_err("LEB %d:%d, lsave len\n", lnum, offs);
1935                         break;
1936                 default:
1937                         ubifs_err(c, "LPT node type %d not recognized", node_type);
1938                         goto out;
1939                 }
1940
1941                 p += node_len;
1942                 len -= node_len;
1943         }
1944
1945         pr_err("(pid %d) finish dumping LEB %d\n", current->pid, lnum);
1946 out:
1947         vfree(buf);
1948         return;
1949 }
1950
1951 /**
1952  * ubifs_dump_lpt_lebs - dump LPT lebs.
1953  * @c: UBIFS file-system description object
1954  *
1955  * This function dumps all LPT LEBs. The caller has to make sure the LPT is
1956  * locked.
1957  */
1958 void ubifs_dump_lpt_lebs(const struct ubifs_info *c)
1959 {
1960         int i;
1961
1962         pr_err("(pid %d) start dumping all LPT LEBs\n", current->pid);
1963         for (i = 0; i < c->lpt_lebs; i++)
1964                 dump_lpt_leb(c, i + c->lpt_first);
1965         pr_err("(pid %d) finish dumping all LPT LEBs\n", current->pid);
1966 }
1967
1968 /**
1969  * dbg_populate_lsave - debugging version of 'populate_lsave()'
1970  * @c: UBIFS file-system description object
1971  *
1972  * This is a debugging version for 'populate_lsave()' which populates lsave
1973  * with random LEBs instead of useful LEBs, which is good for test coverage.
1974  * Returns zero if lsave has not been populated (this debugging feature is
1975  * disabled) an non-zero if lsave has been populated.
1976  */
1977 static int dbg_populate_lsave(struct ubifs_info *c)
1978 {
1979         struct ubifs_lprops *lprops;
1980         struct ubifs_lpt_heap *heap;
1981         int i;
1982
1983         if (!dbg_is_chk_gen(c))
1984                 return 0;
1985         if (prandom_u32() & 3)
1986                 return 0;
1987
1988         for (i = 0; i < c->lsave_cnt; i++)
1989                 c->lsave[i] = c->main_first;
1990
1991         list_for_each_entry(lprops, &c->empty_list, list)
1992                 c->lsave[prandom_u32() % c->lsave_cnt] = lprops->lnum;
1993         list_for_each_entry(lprops, &c->freeable_list, list)
1994                 c->lsave[prandom_u32() % c->lsave_cnt] = lprops->lnum;
1995         list_for_each_entry(lprops, &c->frdi_idx_list, list)
1996                 c->lsave[prandom_u32() % c->lsave_cnt] = lprops->lnum;
1997
1998         heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1];
1999         for (i = 0; i < heap->cnt; i++)
2000                 c->lsave[prandom_u32() % c->lsave_cnt] = heap->arr[i]->lnum;
2001         heap = &c->lpt_heap[LPROPS_DIRTY - 1];
2002         for (i = 0; i < heap->cnt; i++)
2003                 c->lsave[prandom_u32() % c->lsave_cnt] = heap->arr[i]->lnum;
2004         heap = &c->lpt_heap[LPROPS_FREE - 1];
2005         for (i = 0; i < heap->cnt; i++)
2006                 c->lsave[prandom_u32() % c->lsave_cnt] = heap->arr[i]->lnum;
2007
2008         return 1;
2009 }