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