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