Lines Matching +full:- +full:c
4 * Copyright (C) 2006-2008 Nokia Corporation.
6 * SPDX-License-Identifier: GPL-2.0+
13 * This file implements commit-related functionality of the LEB properties
29 static int dbg_populate_lsave(struct ubifs_info *c);
33 * first_dirty_cnode - find first dirty cnode.
34 * @c: UBIFS file-system description object
48 cnode = nnode->nbranch[i].cnode; in first_dirty_cnode()
50 test_bit(DIRTY_CNODE, &cnode->flags)) { in first_dirty_cnode()
51 if (cnode->level == 0) in first_dirty_cnode()
64 * next_dirty_cnode - find next dirty cnode.
75 nnode = cnode->parent; in next_dirty_cnode()
78 for (i = cnode->iip + 1; i < UBIFS_LPT_FANOUT; i++) { in next_dirty_cnode()
79 cnode = nnode->nbranch[i].cnode; in next_dirty_cnode()
80 if (cnode && test_bit(DIRTY_CNODE, &cnode->flags)) { in next_dirty_cnode()
81 if (cnode->level == 0) in next_dirty_cnode()
91 * get_cnodes_to_commit - create list of dirty cnodes to commit.
92 * @c: UBIFS file-system description object
96 static int get_cnodes_to_commit(struct ubifs_info *c) in get_cnodes_to_commit() argument
101 if (!c->nroot) in get_cnodes_to_commit()
104 if (!test_bit(DIRTY_CNODE, &c->nroot->flags)) in get_cnodes_to_commit()
107 c->lpt_cnext = first_dirty_cnode(c->nroot); in get_cnodes_to_commit()
108 cnode = c->lpt_cnext; in get_cnodes_to_commit()
113 ubifs_assert(!test_bit(COW_CNODE, &cnode->flags)); in get_cnodes_to_commit()
114 __set_bit(COW_CNODE, &cnode->flags); in get_cnodes_to_commit()
117 cnode->cnext = c->lpt_cnext; in get_cnodes_to_commit()
120 cnode->cnext = cnext; in get_cnodes_to_commit()
126 ubifs_assert(cnt == c->dirty_nn_cnt + c->dirty_pn_cnt); in get_cnodes_to_commit()
131 * upd_ltab - update LPT LEB properties.
132 * @c: UBIFS file-system description object
137 static void upd_ltab(struct ubifs_info *c, int lnum, int free, int dirty) in upd_ltab() argument
140 lnum, c->ltab[lnum - c->lpt_first].free, in upd_ltab()
141 c->ltab[lnum - c->lpt_first].dirty, free, dirty); in upd_ltab()
142 ubifs_assert(lnum >= c->lpt_first && lnum <= c->lpt_last); in upd_ltab()
143 c->ltab[lnum - c->lpt_first].free = free; in upd_ltab()
144 c->ltab[lnum - c->lpt_first].dirty += dirty; in upd_ltab()
148 * alloc_lpt_leb - allocate an LPT LEB that is empty.
149 * @c: UBIFS file-system description object
154 * Otherwise the function returns -ENOSPC. Note however, that LPT is designed
157 static int alloc_lpt_leb(struct ubifs_info *c, int *lnum) in alloc_lpt_leb() argument
161 n = *lnum - c->lpt_first + 1; in alloc_lpt_leb()
162 for (i = n; i < c->lpt_lebs; i++) { in alloc_lpt_leb()
163 if (c->ltab[i].tgc || c->ltab[i].cmt) in alloc_lpt_leb()
165 if (c->ltab[i].free == c->leb_size) { in alloc_lpt_leb()
166 c->ltab[i].cmt = 1; in alloc_lpt_leb()
167 *lnum = i + c->lpt_first; in alloc_lpt_leb()
173 if (c->ltab[i].tgc || c->ltab[i].cmt) in alloc_lpt_leb()
175 if (c->ltab[i].free == c->leb_size) { in alloc_lpt_leb()
176 c->ltab[i].cmt = 1; in alloc_lpt_leb()
177 *lnum = i + c->lpt_first; in alloc_lpt_leb()
181 return -ENOSPC; in alloc_lpt_leb()
185 * layout_cnodes - layout cnodes for commit.
186 * @c: UBIFS file-system description object
190 static int layout_cnodes(struct ubifs_info *c) in layout_cnodes() argument
195 err = dbg_chk_lpt_sz(c, 0, 0); in layout_cnodes()
198 cnode = c->lpt_cnext; in layout_cnodes()
201 lnum = c->nhead_lnum; in layout_cnodes()
202 offs = c->nhead_offs; in layout_cnodes()
204 done_lsave = !c->big_lpt; in layout_cnodes()
206 if (!done_lsave && offs + c->lsave_sz <= c->leb_size) { in layout_cnodes()
208 c->lsave_lnum = lnum; in layout_cnodes()
209 c->lsave_offs = offs; in layout_cnodes()
210 offs += c->lsave_sz; in layout_cnodes()
211 dbg_chk_lpt_sz(c, 1, c->lsave_sz); in layout_cnodes()
214 if (offs + c->ltab_sz <= c->leb_size) { in layout_cnodes()
216 c->ltab_lnum = lnum; in layout_cnodes()
217 c->ltab_offs = offs; in layout_cnodes()
218 offs += c->ltab_sz; in layout_cnodes()
219 dbg_chk_lpt_sz(c, 1, c->ltab_sz); in layout_cnodes()
223 if (cnode->level) { in layout_cnodes()
224 len = c->nnode_sz; in layout_cnodes()
225 c->dirty_nn_cnt -= 1; in layout_cnodes()
227 len = c->pnode_sz; in layout_cnodes()
228 c->dirty_pn_cnt -= 1; in layout_cnodes()
230 while (offs + len > c->leb_size) { in layout_cnodes()
231 alen = ALIGN(offs, c->min_io_size); in layout_cnodes()
232 upd_ltab(c, lnum, c->leb_size - alen, alen - offs); in layout_cnodes()
233 dbg_chk_lpt_sz(c, 2, c->leb_size - offs); in layout_cnodes()
234 err = alloc_lpt_leb(c, &lnum); in layout_cnodes()
238 ubifs_assert(lnum >= c->lpt_first && in layout_cnodes()
239 lnum <= c->lpt_last); in layout_cnodes()
243 c->lsave_lnum = lnum; in layout_cnodes()
244 c->lsave_offs = offs; in layout_cnodes()
245 offs += c->lsave_sz; in layout_cnodes()
246 dbg_chk_lpt_sz(c, 1, c->lsave_sz); in layout_cnodes()
251 c->ltab_lnum = lnum; in layout_cnodes()
252 c->ltab_offs = offs; in layout_cnodes()
253 offs += c->ltab_sz; in layout_cnodes()
254 dbg_chk_lpt_sz(c, 1, c->ltab_sz); in layout_cnodes()
259 if (cnode->parent) { in layout_cnodes()
260 cnode->parent->nbranch[cnode->iip].lnum = lnum; in layout_cnodes()
261 cnode->parent->nbranch[cnode->iip].offs = offs; in layout_cnodes()
263 c->lpt_lnum = lnum; in layout_cnodes()
264 c->lpt_offs = offs; in layout_cnodes()
267 dbg_chk_lpt_sz(c, 1, len); in layout_cnodes()
268 cnode = cnode->cnext; in layout_cnodes()
269 } while (cnode && cnode != c->lpt_cnext); in layout_cnodes()
273 if (offs + c->lsave_sz > c->leb_size) { in layout_cnodes()
274 alen = ALIGN(offs, c->min_io_size); in layout_cnodes()
275 upd_ltab(c, lnum, c->leb_size - alen, alen - offs); in layout_cnodes()
276 dbg_chk_lpt_sz(c, 2, c->leb_size - offs); in layout_cnodes()
277 err = alloc_lpt_leb(c, &lnum); in layout_cnodes()
281 ubifs_assert(lnum >= c->lpt_first && in layout_cnodes()
282 lnum <= c->lpt_last); in layout_cnodes()
285 c->lsave_lnum = lnum; in layout_cnodes()
286 c->lsave_offs = offs; in layout_cnodes()
287 offs += c->lsave_sz; in layout_cnodes()
288 dbg_chk_lpt_sz(c, 1, c->lsave_sz); in layout_cnodes()
293 if (offs + c->ltab_sz > c->leb_size) { in layout_cnodes()
294 alen = ALIGN(offs, c->min_io_size); in layout_cnodes()
295 upd_ltab(c, lnum, c->leb_size - alen, alen - offs); in layout_cnodes()
296 dbg_chk_lpt_sz(c, 2, c->leb_size - offs); in layout_cnodes()
297 err = alloc_lpt_leb(c, &lnum); in layout_cnodes()
301 ubifs_assert(lnum >= c->lpt_first && in layout_cnodes()
302 lnum <= c->lpt_last); in layout_cnodes()
304 c->ltab_lnum = lnum; in layout_cnodes()
305 c->ltab_offs = offs; in layout_cnodes()
306 offs += c->ltab_sz; in layout_cnodes()
307 dbg_chk_lpt_sz(c, 1, c->ltab_sz); in layout_cnodes()
310 alen = ALIGN(offs, c->min_io_size); in layout_cnodes()
311 upd_ltab(c, lnum, c->leb_size - alen, alen - offs); in layout_cnodes()
312 dbg_chk_lpt_sz(c, 4, alen - offs); in layout_cnodes()
313 err = dbg_chk_lpt_sz(c, 3, alen); in layout_cnodes()
319 ubifs_err(c, "LPT out of space at LEB %d:%d needing %d, done_ltab %d, done_lsave %d", in layout_cnodes()
321 ubifs_dump_lpt_info(c); in layout_cnodes()
322 ubifs_dump_lpt_lebs(c); in layout_cnodes()
329 * realloc_lpt_leb - allocate an LPT LEB that is empty.
330 * @c: UBIFS file-system description object
339 * the function returns %0. Otherwise the function returns -ENOSPC.
342 static int realloc_lpt_leb(struct ubifs_info *c, int *lnum) in realloc_lpt_leb() argument
346 n = *lnum - c->lpt_first + 1; in realloc_lpt_leb()
347 for (i = n; i < c->lpt_lebs; i++) in realloc_lpt_leb()
348 if (c->ltab[i].cmt) { in realloc_lpt_leb()
349 c->ltab[i].cmt = 0; in realloc_lpt_leb()
350 *lnum = i + c->lpt_first; in realloc_lpt_leb()
355 if (c->ltab[i].cmt) { in realloc_lpt_leb()
356 c->ltab[i].cmt = 0; in realloc_lpt_leb()
357 *lnum = i + c->lpt_first; in realloc_lpt_leb()
360 return -ENOSPC; in realloc_lpt_leb()
364 * write_cnodes - write cnodes for commit.
365 * @c: UBIFS file-system description object
369 static int write_cnodes(struct ubifs_info *c) in write_cnodes() argument
373 void *buf = c->lpt_buf; in write_cnodes()
375 cnode = c->lpt_cnext; in write_cnodes()
378 lnum = c->nhead_lnum; in write_cnodes()
379 offs = c->nhead_offs; in write_cnodes()
383 err = ubifs_leb_unmap(c, lnum); in write_cnodes()
388 done_lsave = !c->big_lpt; in write_cnodes()
390 if (!done_lsave && offs + c->lsave_sz <= c->leb_size) { in write_cnodes()
392 ubifs_pack_lsave(c, buf + offs, c->lsave); in write_cnodes()
393 offs += c->lsave_sz; in write_cnodes()
394 dbg_chk_lpt_sz(c, 1, c->lsave_sz); in write_cnodes()
397 if (offs + c->ltab_sz <= c->leb_size) { in write_cnodes()
399 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt); in write_cnodes()
400 offs += c->ltab_sz; in write_cnodes()
401 dbg_chk_lpt_sz(c, 1, c->ltab_sz); in write_cnodes()
406 if (cnode->level) in write_cnodes()
407 len = c->nnode_sz; in write_cnodes()
409 len = c->pnode_sz; in write_cnodes()
410 while (offs + len > c->leb_size) { in write_cnodes()
411 wlen = offs - from; in write_cnodes()
413 alen = ALIGN(wlen, c->min_io_size); in write_cnodes()
414 memset(buf + offs, 0xff, alen - wlen); in write_cnodes()
415 err = ubifs_leb_write(c, lnum, buf + from, from, in write_cnodes()
420 dbg_chk_lpt_sz(c, 2, c->leb_size - offs); in write_cnodes()
421 err = realloc_lpt_leb(c, &lnum); in write_cnodes()
425 ubifs_assert(lnum >= c->lpt_first && in write_cnodes()
426 lnum <= c->lpt_last); in write_cnodes()
427 err = ubifs_leb_unmap(c, lnum); in write_cnodes()
433 ubifs_pack_lsave(c, buf + offs, c->lsave); in write_cnodes()
434 offs += c->lsave_sz; in write_cnodes()
435 dbg_chk_lpt_sz(c, 1, c->lsave_sz); in write_cnodes()
440 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt); in write_cnodes()
441 offs += c->ltab_sz; in write_cnodes()
442 dbg_chk_lpt_sz(c, 1, c->ltab_sz); in write_cnodes()
447 if (cnode->level) in write_cnodes()
448 ubifs_pack_nnode(c, buf + offs, in write_cnodes()
451 ubifs_pack_pnode(c, buf + offs, in write_cnodes()
459 clear_bit(DIRTY_CNODE, &cnode->flags); in write_cnodes()
461 clear_bit(COW_CNODE, &cnode->flags); in write_cnodes()
464 dbg_chk_lpt_sz(c, 1, len); in write_cnodes()
465 cnode = cnode->cnext; in write_cnodes()
466 } while (cnode && cnode != c->lpt_cnext); in write_cnodes()
470 if (offs + c->lsave_sz > c->leb_size) { in write_cnodes()
471 wlen = offs - from; in write_cnodes()
472 alen = ALIGN(wlen, c->min_io_size); in write_cnodes()
473 memset(buf + offs, 0xff, alen - wlen); in write_cnodes()
474 err = ubifs_leb_write(c, lnum, buf + from, from, alen); in write_cnodes()
477 dbg_chk_lpt_sz(c, 2, c->leb_size - offs); in write_cnodes()
478 err = realloc_lpt_leb(c, &lnum); in write_cnodes()
482 ubifs_assert(lnum >= c->lpt_first && in write_cnodes()
483 lnum <= c->lpt_last); in write_cnodes()
484 err = ubifs_leb_unmap(c, lnum); in write_cnodes()
489 ubifs_pack_lsave(c, buf + offs, c->lsave); in write_cnodes()
490 offs += c->lsave_sz; in write_cnodes()
491 dbg_chk_lpt_sz(c, 1, c->lsave_sz); in write_cnodes()
496 if (offs + c->ltab_sz > c->leb_size) { in write_cnodes()
497 wlen = offs - from; in write_cnodes()
498 alen = ALIGN(wlen, c->min_io_size); in write_cnodes()
499 memset(buf + offs, 0xff, alen - wlen); in write_cnodes()
500 err = ubifs_leb_write(c, lnum, buf + from, from, alen); in write_cnodes()
503 dbg_chk_lpt_sz(c, 2, c->leb_size - offs); in write_cnodes()
504 err = realloc_lpt_leb(c, &lnum); in write_cnodes()
508 ubifs_assert(lnum >= c->lpt_first && in write_cnodes()
509 lnum <= c->lpt_last); in write_cnodes()
510 err = ubifs_leb_unmap(c, lnum); in write_cnodes()
514 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt); in write_cnodes()
515 offs += c->ltab_sz; in write_cnodes()
516 dbg_chk_lpt_sz(c, 1, c->ltab_sz); in write_cnodes()
520 wlen = offs - from; in write_cnodes()
521 alen = ALIGN(wlen, c->min_io_size); in write_cnodes()
522 memset(buf + offs, 0xff, alen - wlen); in write_cnodes()
523 err = ubifs_leb_write(c, lnum, buf + from, from, alen); in write_cnodes()
527 dbg_chk_lpt_sz(c, 4, alen - wlen); in write_cnodes()
528 err = dbg_chk_lpt_sz(c, 3, ALIGN(offs, c->min_io_size)); in write_cnodes()
532 c->nhead_lnum = lnum; in write_cnodes()
533 c->nhead_offs = ALIGN(offs, c->min_io_size); in write_cnodes()
535 dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs); in write_cnodes()
536 dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs); in write_cnodes()
537 dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs); in write_cnodes()
538 if (c->big_lpt) in write_cnodes()
539 dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs); in write_cnodes()
544 ubifs_err(c, "LPT out of space mismatch at LEB %d:%d needing %d, done_ltab %d, done_lsave %d", in write_cnodes()
546 ubifs_dump_lpt_info(c); in write_cnodes()
547 ubifs_dump_lpt_lebs(c); in write_cnodes()
554 * next_pnode_to_dirty - find next pnode to dirty.
555 * @c: UBIFS file-system description object
562 static struct ubifs_pnode *next_pnode_to_dirty(struct ubifs_info *c, in next_pnode_to_dirty() argument
569 nnode = pnode->parent; in next_pnode_to_dirty()
570 for (iip = pnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) { in next_pnode_to_dirty()
571 if (nnode->nbranch[iip].lnum) in next_pnode_to_dirty()
572 return ubifs_get_pnode(c, nnode, iip); in next_pnode_to_dirty()
577 iip = nnode->iip + 1; in next_pnode_to_dirty()
578 nnode = nnode->parent; in next_pnode_to_dirty()
582 if (nnode->nbranch[iip].lnum) in next_pnode_to_dirty()
588 nnode = ubifs_get_nnode(c, nnode, iip); in next_pnode_to_dirty()
593 while (nnode->level > 1) { in next_pnode_to_dirty()
595 if (nnode->nbranch[iip].lnum) in next_pnode_to_dirty()
605 nnode = ubifs_get_nnode(c, nnode, iip); in next_pnode_to_dirty()
611 if (nnode->nbranch[iip].lnum) in next_pnode_to_dirty()
616 return ubifs_get_pnode(c, nnode, iip); in next_pnode_to_dirty()
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)
627 static struct ubifs_pnode *pnode_lookup(struct ubifs_info *c, int i) in pnode_lookup() argument
632 if (!c->nroot) { in pnode_lookup()
633 err = ubifs_read_nnode(c, NULL, 0); in pnode_lookup()
638 nnode = c->nroot; in pnode_lookup()
639 shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT; in pnode_lookup()
640 for (h = 1; h < c->lpt_hght; h++) { in pnode_lookup()
641 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1)); in pnode_lookup()
642 shft -= UBIFS_LPT_FANOUT_SHIFT; in pnode_lookup()
643 nnode = ubifs_get_nnode(c, nnode, iip); in pnode_lookup()
647 iip = ((i >> shft) & (UBIFS_LPT_FANOUT - 1)); in pnode_lookup()
648 return ubifs_get_pnode(c, nnode, iip); in pnode_lookup()
652 * add_pnode_dirt - add dirty space to LPT LEB properties.
653 * @c: UBIFS file-system description object
656 static void add_pnode_dirt(struct ubifs_info *c, struct ubifs_pnode *pnode) in add_pnode_dirt() argument
658 ubifs_add_lpt_dirt(c, pnode->parent->nbranch[pnode->iip].lnum, in add_pnode_dirt()
659 c->pnode_sz); in add_pnode_dirt()
663 * do_make_pnode_dirty - mark a pnode dirty.
664 * @c: UBIFS file-system description object
667 static void do_make_pnode_dirty(struct ubifs_info *c, struct ubifs_pnode *pnode) in do_make_pnode_dirty() argument
670 if (!test_and_set_bit(DIRTY_CNODE, &pnode->flags)) { in do_make_pnode_dirty()
673 c->dirty_pn_cnt += 1; in do_make_pnode_dirty()
674 add_pnode_dirt(c, pnode); in do_make_pnode_dirty()
676 nnode = pnode->parent; in do_make_pnode_dirty()
678 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) { in do_make_pnode_dirty()
679 c->dirty_nn_cnt += 1; in do_make_pnode_dirty()
680 ubifs_add_nnode_dirt(c, nnode); in do_make_pnode_dirty()
681 nnode = nnode->parent; in do_make_pnode_dirty()
689 * make_tree_dirty - mark the entire LEB properties tree dirty.
690 * @c: UBIFS file-system description object
699 static int make_tree_dirty(struct ubifs_info *c) in make_tree_dirty() argument
703 pnode = pnode_lookup(c, 0); in make_tree_dirty()
708 do_make_pnode_dirty(c, pnode); in make_tree_dirty()
709 pnode = next_pnode_to_dirty(c, pnode); in make_tree_dirty()
717 * need_write_all - determine if the LPT area is running out of free space.
718 * @c: UBIFS file-system description object
723 static int need_write_all(struct ubifs_info *c) in need_write_all() argument
728 for (i = 0; i < c->lpt_lebs; i++) { in need_write_all()
729 if (i + c->lpt_first == c->nhead_lnum) in need_write_all()
730 free += c->leb_size - c->nhead_offs; in need_write_all()
731 else if (c->ltab[i].free == c->leb_size) in need_write_all()
732 free += c->leb_size; in need_write_all()
733 else if (c->ltab[i].free + c->ltab[i].dirty == c->leb_size) in need_write_all()
734 free += c->leb_size; in need_write_all()
737 if (free <= c->lpt_sz * 2) in need_write_all()
743 * lpt_tgc_start - start trivial garbage collection of LPT LEBs.
744 * @c: UBIFS file-system description object
750 static void lpt_tgc_start(struct ubifs_info *c) in lpt_tgc_start() argument
754 for (i = 0; i < c->lpt_lebs; i++) { in lpt_tgc_start()
755 if (i + c->lpt_first == c->nhead_lnum) in lpt_tgc_start()
757 if (c->ltab[i].dirty > 0 && in lpt_tgc_start()
758 c->ltab[i].free + c->ltab[i].dirty == c->leb_size) { in lpt_tgc_start()
759 c->ltab[i].tgc = 1; in lpt_tgc_start()
760 c->ltab[i].free = c->leb_size; in lpt_tgc_start()
761 c->ltab[i].dirty = 0; in lpt_tgc_start()
762 dbg_lp("LEB %d", i + c->lpt_first); in lpt_tgc_start()
768 * lpt_tgc_end - end trivial garbage collection of LPT LEBs.
769 * @c: UBIFS file-system description object
774 * written) and un-maps LPT LEBs that were marked for trivial GC.
776 static int lpt_tgc_end(struct ubifs_info *c) in lpt_tgc_end() argument
780 for (i = 0; i < c->lpt_lebs; i++) in lpt_tgc_end()
781 if (c->ltab[i].tgc) { in lpt_tgc_end()
782 err = ubifs_leb_unmap(c, i + c->lpt_first); in lpt_tgc_end()
785 c->ltab[i].tgc = 0; in lpt_tgc_end()
786 dbg_lp("LEB %d", i + c->lpt_first); in lpt_tgc_end()
792 * populate_lsave - fill the lsave array with important LEB numbers.
793 * @c: the UBIFS file-system description object
803 static void populate_lsave(struct ubifs_info *c) in populate_lsave() argument
809 ubifs_assert(c->big_lpt); in populate_lsave()
810 if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) { in populate_lsave()
811 c->lpt_drty_flgs |= LSAVE_DIRTY; in populate_lsave()
812 ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz); in populate_lsave()
816 if (dbg_populate_lsave(c)) in populate_lsave()
820 list_for_each_entry(lprops, &c->empty_list, list) { in populate_lsave()
821 c->lsave[cnt++] = lprops->lnum; in populate_lsave()
822 if (cnt >= c->lsave_cnt) in populate_lsave()
825 list_for_each_entry(lprops, &c->freeable_list, list) { in populate_lsave()
826 c->lsave[cnt++] = lprops->lnum; in populate_lsave()
827 if (cnt >= c->lsave_cnt) in populate_lsave()
830 list_for_each_entry(lprops, &c->frdi_idx_list, list) { in populate_lsave()
831 c->lsave[cnt++] = lprops->lnum; in populate_lsave()
832 if (cnt >= c->lsave_cnt) in populate_lsave()
835 heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1]; in populate_lsave()
836 for (i = 0; i < heap->cnt; i++) { in populate_lsave()
837 c->lsave[cnt++] = heap->arr[i]->lnum; in populate_lsave()
838 if (cnt >= c->lsave_cnt) in populate_lsave()
841 heap = &c->lpt_heap[LPROPS_DIRTY - 1]; in populate_lsave()
842 for (i = 0; i < heap->cnt; i++) { in populate_lsave()
843 c->lsave[cnt++] = heap->arr[i]->lnum; in populate_lsave()
844 if (cnt >= c->lsave_cnt) in populate_lsave()
847 heap = &c->lpt_heap[LPROPS_FREE - 1]; in populate_lsave()
848 for (i = 0; i < heap->cnt; i++) { in populate_lsave()
849 c->lsave[cnt++] = heap->arr[i]->lnum; in populate_lsave()
850 if (cnt >= c->lsave_cnt) in populate_lsave()
854 while (cnt < c->lsave_cnt) in populate_lsave()
855 c->lsave[cnt++] = c->main_first; in populate_lsave()
859 * nnode_lookup - lookup a nnode in the LPT.
860 * @c: UBIFS file-system description object
866 static struct ubifs_nnode *nnode_lookup(struct ubifs_info *c, int i) in nnode_lookup() argument
871 if (!c->nroot) { in nnode_lookup()
872 err = ubifs_read_nnode(c, NULL, 0); in nnode_lookup()
876 nnode = c->nroot; in nnode_lookup()
878 iip = i & (UBIFS_LPT_FANOUT - 1); in nnode_lookup()
882 nnode = ubifs_get_nnode(c, nnode, iip); in nnode_lookup()
890 * make_nnode_dirty - find a nnode and, if found, make it dirty.
891 * @c: UBIFS file-system description object
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
904 static int make_nnode_dirty(struct ubifs_info *c, int node_num, int lnum, in make_nnode_dirty() argument
909 nnode = nnode_lookup(c, node_num); in make_nnode_dirty()
912 if (nnode->parent) { in make_nnode_dirty()
915 branch = &nnode->parent->nbranch[nnode->iip]; in make_nnode_dirty()
916 if (branch->lnum != lnum || branch->offs != offs) in make_nnode_dirty()
918 } else if (c->lpt_lnum != lnum || c->lpt_offs != offs) in make_nnode_dirty()
921 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) { in make_nnode_dirty()
922 c->dirty_nn_cnt += 1; in make_nnode_dirty()
923 ubifs_add_nnode_dirt(c, nnode); in make_nnode_dirty()
925 nnode = nnode->parent; in make_nnode_dirty()
927 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) { in make_nnode_dirty()
928 c->dirty_nn_cnt += 1; in make_nnode_dirty()
929 ubifs_add_nnode_dirt(c, nnode); in make_nnode_dirty()
930 nnode = nnode->parent; in make_nnode_dirty()
939 * make_pnode_dirty - find a pnode and, if found, make it dirty.
940 * @c: UBIFS file-system description object
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
953 static int make_pnode_dirty(struct ubifs_info *c, int node_num, int lnum, in make_pnode_dirty() argument
959 pnode = pnode_lookup(c, node_num); in make_pnode_dirty()
962 branch = &pnode->parent->nbranch[pnode->iip]; in make_pnode_dirty()
963 if (branch->lnum != lnum || branch->offs != offs) in make_pnode_dirty()
965 do_make_pnode_dirty(c, pnode); in make_pnode_dirty()
970 * make_ltab_dirty - make ltab node dirty.
971 * @c: UBIFS file-system description object
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
983 static int make_ltab_dirty(struct ubifs_info *c, int lnum, int offs) in make_ltab_dirty() argument
985 if (lnum != c->ltab_lnum || offs != c->ltab_offs) in make_ltab_dirty()
987 if (!(c->lpt_drty_flgs & LTAB_DIRTY)) { in make_ltab_dirty()
988 c->lpt_drty_flgs |= LTAB_DIRTY; in make_ltab_dirty()
989 ubifs_add_lpt_dirt(c, c->ltab_lnum, c->ltab_sz); in make_ltab_dirty()
995 * make_lsave_dirty - make lsave node dirty.
996 * @c: UBIFS file-system description object
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
1008 static int make_lsave_dirty(struct ubifs_info *c, int lnum, int offs) in make_lsave_dirty() argument
1010 if (lnum != c->lsave_lnum || offs != c->lsave_offs) in make_lsave_dirty()
1012 if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) { in make_lsave_dirty()
1013 c->lpt_drty_flgs |= LSAVE_DIRTY; in make_lsave_dirty()
1014 ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz); in make_lsave_dirty()
1020 * make_node_dirty - make node dirty.
1021 * @c: UBIFS file-system description object
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
1035 static int make_node_dirty(struct ubifs_info *c, int node_type, int node_num, in make_node_dirty() argument
1040 return make_nnode_dirty(c, node_num, lnum, offs); in make_node_dirty()
1042 return make_pnode_dirty(c, node_num, lnum, offs); in make_node_dirty()
1044 return make_ltab_dirty(c, lnum, offs); in make_node_dirty()
1046 return make_lsave_dirty(c, lnum, offs); in make_node_dirty()
1048 return -EINVAL; in make_node_dirty()
1052 * get_lpt_node_len - return the length of a node based on its type.
1053 * @c: UBIFS file-system description object
1056 static int get_lpt_node_len(const struct ubifs_info *c, int node_type) in get_lpt_node_len() argument
1060 return c->nnode_sz; in get_lpt_node_len()
1062 return c->pnode_sz; in get_lpt_node_len()
1064 return c->ltab_sz; in get_lpt_node_len()
1066 return c->lsave_sz; in get_lpt_node_len()
1072 * get_pad_len - return the length of padding in a buffer.
1073 * @c: UBIFS file-system description object
1077 static int get_pad_len(const struct ubifs_info *c, uint8_t *buf, int len) in get_pad_len() argument
1081 if (c->min_io_size == 1) in get_pad_len()
1083 offs = c->leb_size - len; in get_pad_len()
1084 pad_len = ALIGN(offs, c->min_io_size) - offs; in get_pad_len()
1089 * get_lpt_node_type - return type (and node number) of a node in a buffer.
1090 * @c: UBIFS file-system description object
1094 static int get_lpt_node_type(const struct ubifs_info *c, uint8_t *buf, in get_lpt_node_type() argument
1101 *node_num = ubifs_unpack_bits(&addr, &pos, c->pcnt_bits); in get_lpt_node_type()
1106 * is_a_node - determine if a buffer contains a node.
1107 * @c: UBIFS file-system description object
1113 static int is_a_node(const struct ubifs_info *c, uint8_t *buf, int len) in is_a_node() argument
1124 node_len = get_lpt_node_len(c, node_type); in is_a_node()
1130 calc_crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES, in is_a_node()
1131 node_len - UBIFS_LPT_CRC_BYTES); in is_a_node()
1138 * lpt_gc_lnum - garbage collect a LPT LEB.
1139 * @c: UBIFS file-system description object
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
1149 static int lpt_gc_lnum(struct ubifs_info *c, int lnum) in lpt_gc_lnum() argument
1151 int err, len = c->leb_size, node_type, node_num, node_len, offs; in lpt_gc_lnum()
1152 void *buf = c->lpt_buf; in lpt_gc_lnum()
1156 err = ubifs_leb_read(c, lnum, buf, 0, c->leb_size, 1); in lpt_gc_lnum()
1161 if (!is_a_node(c, buf, len)) { in lpt_gc_lnum()
1164 pad_len = get_pad_len(c, buf, len); in lpt_gc_lnum()
1167 len -= pad_len; in lpt_gc_lnum()
1172 node_type = get_lpt_node_type(c, buf, &node_num); in lpt_gc_lnum()
1173 node_len = get_lpt_node_len(c, node_type); in lpt_gc_lnum()
1174 offs = c->leb_size - len; in lpt_gc_lnum()
1176 mutex_lock(&c->lp_mutex); in lpt_gc_lnum()
1177 err = make_node_dirty(c, node_type, node_num, lnum, offs); in lpt_gc_lnum()
1178 mutex_unlock(&c->lp_mutex); in lpt_gc_lnum()
1182 len -= node_len; in lpt_gc_lnum()
1188 * lpt_gc - LPT garbage collection.
1189 * @c: UBIFS file-system description object
1194 static int lpt_gc(struct ubifs_info *c) in lpt_gc() argument
1196 int i, lnum = -1, dirty = 0; in lpt_gc()
1198 mutex_lock(&c->lp_mutex); in lpt_gc()
1199 for (i = 0; i < c->lpt_lebs; i++) { in lpt_gc()
1200 ubifs_assert(!c->ltab[i].tgc); in lpt_gc()
1201 if (i + c->lpt_first == c->nhead_lnum || in lpt_gc()
1202 c->ltab[i].free + c->ltab[i].dirty == c->leb_size) in lpt_gc()
1204 if (c->ltab[i].dirty > dirty) { in lpt_gc()
1205 dirty = c->ltab[i].dirty; in lpt_gc()
1206 lnum = i + c->lpt_first; in lpt_gc()
1209 mutex_unlock(&c->lp_mutex); in lpt_gc()
1210 if (lnum == -1) in lpt_gc()
1211 return -ENOSPC; in lpt_gc()
1212 return lpt_gc_lnum(c, lnum); in lpt_gc()
1216 * ubifs_lpt_start_commit - UBIFS commit starts.
1217 * @c: the UBIFS file-system description object
1225 int ubifs_lpt_start_commit(struct ubifs_info *c) in ubifs_lpt_start_commit() argument
1231 mutex_lock(&c->lp_mutex); in ubifs_lpt_start_commit()
1232 err = dbg_chk_lpt_free_spc(c); in ubifs_lpt_start_commit()
1235 err = dbg_check_ltab(c); in ubifs_lpt_start_commit()
1239 if (c->check_lpt_free) { in ubifs_lpt_start_commit()
1246 c->check_lpt_free = 0; in ubifs_lpt_start_commit()
1247 while (need_write_all(c)) { in ubifs_lpt_start_commit()
1248 mutex_unlock(&c->lp_mutex); in ubifs_lpt_start_commit()
1249 err = lpt_gc(c); in ubifs_lpt_start_commit()
1252 mutex_lock(&c->lp_mutex); in ubifs_lpt_start_commit()
1256 lpt_tgc_start(c); in ubifs_lpt_start_commit()
1258 if (!c->dirty_pn_cnt) { in ubifs_lpt_start_commit()
1264 if (!c->big_lpt && need_write_all(c)) { in ubifs_lpt_start_commit()
1266 err = make_tree_dirty(c); in ubifs_lpt_start_commit()
1269 lpt_tgc_start(c); in ubifs_lpt_start_commit()
1272 if (c->big_lpt) in ubifs_lpt_start_commit()
1273 populate_lsave(c); in ubifs_lpt_start_commit()
1275 cnt = get_cnodes_to_commit(c); in ubifs_lpt_start_commit()
1278 err = layout_cnodes(c); in ubifs_lpt_start_commit()
1283 memcpy(c->ltab_cmt, c->ltab, in ubifs_lpt_start_commit()
1284 sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs); in ubifs_lpt_start_commit()
1285 c->lpt_drty_flgs &= ~(LTAB_DIRTY | LSAVE_DIRTY); in ubifs_lpt_start_commit()
1288 mutex_unlock(&c->lp_mutex); in ubifs_lpt_start_commit()
1293 * free_obsolete_cnodes - free obsolete cnodes for commit end.
1294 * @c: UBIFS file-system description object
1296 static void free_obsolete_cnodes(struct ubifs_info *c) in free_obsolete_cnodes() argument
1300 cnext = c->lpt_cnext; in free_obsolete_cnodes()
1305 cnext = cnode->cnext; in free_obsolete_cnodes()
1306 if (test_bit(OBSOLETE_CNODE, &cnode->flags)) in free_obsolete_cnodes()
1309 cnode->cnext = NULL; in free_obsolete_cnodes()
1310 } while (cnext != c->lpt_cnext); in free_obsolete_cnodes()
1311 c->lpt_cnext = NULL; in free_obsolete_cnodes()
1316 * ubifs_lpt_end_commit - finish the commit operation.
1317 * @c: the UBIFS file-system description object
1324 int ubifs_lpt_end_commit(struct ubifs_info *c) in ubifs_lpt_end_commit() argument
1330 if (!c->lpt_cnext) in ubifs_lpt_end_commit()
1333 err = write_cnodes(c); in ubifs_lpt_end_commit()
1337 mutex_lock(&c->lp_mutex); in ubifs_lpt_end_commit()
1338 free_obsolete_cnodes(c); in ubifs_lpt_end_commit()
1339 mutex_unlock(&c->lp_mutex); in ubifs_lpt_end_commit()
1346 * ubifs_lpt_post_commit - post commit LPT trivial GC and LPT GC.
1347 * @c: UBIFS file-system description object
1352 int ubifs_lpt_post_commit(struct ubifs_info *c) in ubifs_lpt_post_commit() argument
1356 mutex_lock(&c->lp_mutex); in ubifs_lpt_post_commit()
1357 err = lpt_tgc_end(c); in ubifs_lpt_post_commit()
1360 if (c->big_lpt) in ubifs_lpt_post_commit()
1361 while (need_write_all(c)) { in ubifs_lpt_post_commit()
1362 mutex_unlock(&c->lp_mutex); in ubifs_lpt_post_commit()
1363 err = lpt_gc(c); in ubifs_lpt_post_commit()
1366 mutex_lock(&c->lp_mutex); in ubifs_lpt_post_commit()
1369 mutex_unlock(&c->lp_mutex); in ubifs_lpt_post_commit()
1374 * first_nnode - find the first nnode in memory.
1375 * @c: UBIFS file-system description object
1381 static struct ubifs_nnode *first_nnode(struct ubifs_info *c, int *hght) in first_nnode() argument
1386 nnode = c->nroot; in first_nnode()
1390 for (h = 1; h < c->lpt_hght; h++) { in first_nnode()
1393 if (nnode->nbranch[i].nnode) { in first_nnode()
1395 nnode = nnode->nbranch[i].nnode; in first_nnode()
1407 * next_nnode - find the next nnode in memory.
1408 * @c: UBIFS file-system description object
1415 static struct ubifs_nnode *next_nnode(struct ubifs_info *c, in next_nnode() argument
1421 parent = nnode->parent; in next_nnode()
1424 if (nnode->iip == UBIFS_LPT_FANOUT - 1) { in next_nnode()
1425 *hght -= 1; in next_nnode()
1428 for (iip = nnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) { in next_nnode()
1429 nnode = parent->nbranch[iip].nnode; in next_nnode()
1434 *hght -= 1; in next_nnode()
1437 for (h = *hght + 1; h < c->lpt_hght; h++) { in next_nnode()
1440 if (nnode->nbranch[i].nnode) { in next_nnode()
1442 nnode = nnode->nbranch[i].nnode; in next_nnode()
1454 * ubifs_lpt_free - free resources owned by the LPT.
1455 * @c: UBIFS file-system description object
1458 void ubifs_lpt_free(struct ubifs_info *c, int wr_only) in ubifs_lpt_free() argument
1463 /* Free write-only things first */ in ubifs_lpt_free()
1465 free_obsolete_cnodes(c); /* Leftover from a failed commit */ in ubifs_lpt_free()
1467 vfree(c->ltab_cmt); in ubifs_lpt_free()
1468 c->ltab_cmt = NULL; in ubifs_lpt_free()
1469 vfree(c->lpt_buf); in ubifs_lpt_free()
1470 c->lpt_buf = NULL; in ubifs_lpt_free()
1471 kfree(c->lsave); in ubifs_lpt_free()
1472 c->lsave = NULL; in ubifs_lpt_free()
1479 nnode = first_nnode(c, &hght); in ubifs_lpt_free()
1482 kfree(nnode->nbranch[i].nnode); in ubifs_lpt_free()
1483 nnode = next_nnode(c, nnode, &hght); in ubifs_lpt_free()
1486 kfree(c->lpt_heap[i].arr); in ubifs_lpt_free()
1487 kfree(c->dirty_idx.arr); in ubifs_lpt_free()
1488 kfree(c->nroot); in ubifs_lpt_free()
1489 vfree(c->ltab); in ubifs_lpt_free()
1490 kfree(c->lpt_nod_buf); in ubifs_lpt_free()
1499 * dbg_is_all_ff - determine if a buffer contains only 0xFF bytes.
1514 * dbg_is_nnode_dirty - determine if a nnode is dirty.
1515 * @c: the UBIFS file-system description object
1519 static int dbg_is_nnode_dirty(struct ubifs_info *c, int lnum, int offs) in dbg_is_nnode_dirty() argument
1525 nnode = first_nnode(c, &hght); in dbg_is_nnode_dirty()
1526 for (; nnode; nnode = next_nnode(c, nnode, &hght)) { in dbg_is_nnode_dirty()
1530 if (nnode->parent) { in dbg_is_nnode_dirty()
1531 branch = &nnode->parent->nbranch[nnode->iip]; in dbg_is_nnode_dirty()
1532 if (branch->lnum != lnum || branch->offs != offs) in dbg_is_nnode_dirty()
1534 if (test_bit(DIRTY_CNODE, &nnode->flags)) in dbg_is_nnode_dirty()
1538 if (c->lpt_lnum != lnum || c->lpt_offs != offs) in dbg_is_nnode_dirty()
1540 if (test_bit(DIRTY_CNODE, &nnode->flags)) in dbg_is_nnode_dirty()
1549 * dbg_is_pnode_dirty - determine if a pnode is dirty.
1550 * @c: the UBIFS file-system description object
1554 static int dbg_is_pnode_dirty(struct ubifs_info *c, int lnum, int offs) in dbg_is_pnode_dirty() argument
1558 cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT); in dbg_is_pnode_dirty()
1564 pnode = pnode_lookup(c, i); in dbg_is_pnode_dirty()
1567 branch = &pnode->parent->nbranch[pnode->iip]; in dbg_is_pnode_dirty()
1568 if (branch->lnum != lnum || branch->offs != offs) in dbg_is_pnode_dirty()
1570 if (test_bit(DIRTY_CNODE, &pnode->flags)) in dbg_is_pnode_dirty()
1578 * dbg_is_ltab_dirty - determine if a ltab node is dirty.
1579 * @c: the UBIFS file-system description object
1583 static int dbg_is_ltab_dirty(struct ubifs_info *c, int lnum, int offs) in dbg_is_ltab_dirty() argument
1585 if (lnum != c->ltab_lnum || offs != c->ltab_offs) in dbg_is_ltab_dirty()
1587 return (c->lpt_drty_flgs & LTAB_DIRTY) != 0; in dbg_is_ltab_dirty()
1591 * dbg_is_lsave_dirty - determine if a lsave node is dirty.
1592 * @c: the UBIFS file-system description object
1596 static int dbg_is_lsave_dirty(struct ubifs_info *c, int lnum, int offs) in dbg_is_lsave_dirty() argument
1598 if (lnum != c->lsave_lnum || offs != c->lsave_offs) in dbg_is_lsave_dirty()
1600 return (c->lpt_drty_flgs & LSAVE_DIRTY) != 0; in dbg_is_lsave_dirty()
1604 * dbg_is_node_dirty - determine if a node is dirty.
1605 * @c: the UBIFS file-system description object
1610 static int dbg_is_node_dirty(struct ubifs_info *c, int node_type, int lnum, in dbg_is_node_dirty() argument
1615 return dbg_is_nnode_dirty(c, lnum, offs); in dbg_is_node_dirty()
1617 return dbg_is_pnode_dirty(c, lnum, offs); in dbg_is_node_dirty()
1619 return dbg_is_ltab_dirty(c, lnum, offs); in dbg_is_node_dirty()
1621 return dbg_is_lsave_dirty(c, lnum, offs); in dbg_is_node_dirty()
1627 * dbg_check_ltab_lnum - check the ltab for a LPT LEB number.
1628 * @c: the UBIFS file-system description object
1634 static int dbg_check_ltab_lnum(struct ubifs_info *c, int lnum) in dbg_check_ltab_lnum() argument
1636 int err, len = c->leb_size, dirty = 0, node_type, node_num, node_len; in dbg_check_ltab_lnum()
1640 if (!dbg_is_chk_lprops(c)) in dbg_check_ltab_lnum()
1643 buf = p = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL); in dbg_check_ltab_lnum()
1645 ubifs_err(c, "cannot allocate memory for ltab checking"); in dbg_check_ltab_lnum()
1651 err = ubifs_leb_read(c, lnum, buf, 0, c->leb_size, 1); in dbg_check_ltab_lnum()
1656 if (!is_a_node(c, p, len)) { in dbg_check_ltab_lnum()
1659 pad_len = get_pad_len(c, p, len); in dbg_check_ltab_lnum()
1662 len -= pad_len; in dbg_check_ltab_lnum()
1667 ubifs_err(c, "invalid empty space in LEB %d at %d", in dbg_check_ltab_lnum()
1668 lnum, c->leb_size - len); in dbg_check_ltab_lnum()
1669 err = -EINVAL; in dbg_check_ltab_lnum()
1671 i = lnum - c->lpt_first; in dbg_check_ltab_lnum()
1672 if (len != c->ltab[i].free) { in dbg_check_ltab_lnum()
1673 ubifs_err(c, "invalid free space in LEB %d (free %d, expected %d)", in dbg_check_ltab_lnum()
1674 lnum, len, c->ltab[i].free); in dbg_check_ltab_lnum()
1675 err = -EINVAL; in dbg_check_ltab_lnum()
1677 if (dirty != c->ltab[i].dirty) { in dbg_check_ltab_lnum()
1678 ubifs_err(c, "invalid dirty space in LEB %d (dirty %d, expected %d)", in dbg_check_ltab_lnum()
1679 lnum, dirty, c->ltab[i].dirty); in dbg_check_ltab_lnum()
1680 err = -EINVAL; in dbg_check_ltab_lnum()
1684 node_type = get_lpt_node_type(c, p, &node_num); in dbg_check_ltab_lnum()
1685 node_len = get_lpt_node_len(c, node_type); in dbg_check_ltab_lnum()
1686 ret = dbg_is_node_dirty(c, node_type, lnum, c->leb_size - len); in dbg_check_ltab_lnum()
1690 len -= node_len; in dbg_check_ltab_lnum()
1700 * dbg_check_ltab - check the free and dirty space in the ltab.
1701 * @c: the UBIFS file-system description object
1705 int dbg_check_ltab(struct ubifs_info *c) in dbg_check_ltab() argument
1709 if (!dbg_is_chk_lprops(c)) in dbg_check_ltab()
1713 cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT); in dbg_check_ltab()
1717 pnode = pnode_lookup(c, i); in dbg_check_ltab()
1724 err = dbg_check_lpt_nodes(c, (struct ubifs_cnode *)c->nroot, 0, 0); in dbg_check_ltab()
1729 for (lnum = c->lpt_first; lnum <= c->lpt_last; lnum++) { in dbg_check_ltab()
1730 err = dbg_check_ltab_lnum(c, lnum); in dbg_check_ltab()
1732 ubifs_err(c, "failed at LEB %d", lnum); in dbg_check_ltab()
1742 * dbg_chk_lpt_free_spc - check LPT free space is enough to write entire LPT.
1743 * @c: the UBIFS file-system description object
1747 int dbg_chk_lpt_free_spc(struct ubifs_info *c) in dbg_chk_lpt_free_spc() argument
1752 if (!dbg_is_chk_lprops(c)) in dbg_chk_lpt_free_spc()
1755 for (i = 0; i < c->lpt_lebs; i++) { in dbg_chk_lpt_free_spc()
1756 if (c->ltab[i].tgc || c->ltab[i].cmt) in dbg_chk_lpt_free_spc()
1758 if (i + c->lpt_first == c->nhead_lnum) in dbg_chk_lpt_free_spc()
1759 free += c->leb_size - c->nhead_offs; in dbg_chk_lpt_free_spc()
1760 else if (c->ltab[i].free == c->leb_size) in dbg_chk_lpt_free_spc()
1761 free += c->leb_size; in dbg_chk_lpt_free_spc()
1763 if (free < c->lpt_sz) { in dbg_chk_lpt_free_spc()
1764 ubifs_err(c, "LPT space error: free %lld lpt_sz %lld", in dbg_chk_lpt_free_spc()
1765 free, c->lpt_sz); in dbg_chk_lpt_free_spc()
1766 ubifs_dump_lpt_info(c); in dbg_chk_lpt_free_spc()
1767 ubifs_dump_lpt_lebs(c); in dbg_chk_lpt_free_spc()
1769 return -EINVAL; in dbg_chk_lpt_free_spc()
1775 * dbg_chk_lpt_sz - check LPT does not write more than LPT size.
1776 * @c: the UBIFS file-system description object
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;
1788 int dbg_chk_lpt_sz(struct ubifs_info *c, int action, int len) in dbg_chk_lpt_sz() argument
1790 struct ubifs_debug_info *d = c->dbg; in dbg_chk_lpt_sz()
1794 if (!dbg_is_chk_lprops(c)) in dbg_chk_lpt_sz()
1799 d->chk_lpt_sz = 0; in dbg_chk_lpt_sz()
1800 d->chk_lpt_sz2 = 0; in dbg_chk_lpt_sz()
1801 d->chk_lpt_lebs = 0; in dbg_chk_lpt_sz()
1802 d->chk_lpt_wastage = 0; in dbg_chk_lpt_sz()
1803 if (c->dirty_pn_cnt > c->pnode_cnt) { in dbg_chk_lpt_sz()
1804 ubifs_err(c, "dirty pnodes %d exceed max %d", in dbg_chk_lpt_sz()
1805 c->dirty_pn_cnt, c->pnode_cnt); in dbg_chk_lpt_sz()
1806 err = -EINVAL; in dbg_chk_lpt_sz()
1808 if (c->dirty_nn_cnt > c->nnode_cnt) { in dbg_chk_lpt_sz()
1809 ubifs_err(c, "dirty nnodes %d exceed max %d", in dbg_chk_lpt_sz()
1810 c->dirty_nn_cnt, c->nnode_cnt); in dbg_chk_lpt_sz()
1811 err = -EINVAL; in dbg_chk_lpt_sz()
1815 d->chk_lpt_sz += len; in dbg_chk_lpt_sz()
1818 d->chk_lpt_sz += len; in dbg_chk_lpt_sz()
1819 d->chk_lpt_wastage += len; in dbg_chk_lpt_sz()
1820 d->chk_lpt_lebs += 1; in dbg_chk_lpt_sz()
1823 chk_lpt_sz = c->leb_size; in dbg_chk_lpt_sz()
1824 chk_lpt_sz *= d->chk_lpt_lebs; in dbg_chk_lpt_sz()
1825 chk_lpt_sz += len - c->nhead_offs; in dbg_chk_lpt_sz()
1826 if (d->chk_lpt_sz != chk_lpt_sz) { in dbg_chk_lpt_sz()
1827 ubifs_err(c, "LPT wrote %lld but space used was %lld", in dbg_chk_lpt_sz()
1828 d->chk_lpt_sz, chk_lpt_sz); in dbg_chk_lpt_sz()
1829 err = -EINVAL; in dbg_chk_lpt_sz()
1831 if (d->chk_lpt_sz > c->lpt_sz) { in dbg_chk_lpt_sz()
1832 ubifs_err(c, "LPT wrote %lld but lpt_sz is %lld", in dbg_chk_lpt_sz()
1833 d->chk_lpt_sz, c->lpt_sz); in dbg_chk_lpt_sz()
1834 err = -EINVAL; in dbg_chk_lpt_sz()
1836 if (d->chk_lpt_sz2 && d->chk_lpt_sz != d->chk_lpt_sz2) { in dbg_chk_lpt_sz()
1837 ubifs_err(c, "LPT layout size %lld but wrote %lld", in dbg_chk_lpt_sz()
1838 d->chk_lpt_sz, d->chk_lpt_sz2); in dbg_chk_lpt_sz()
1839 err = -EINVAL; in dbg_chk_lpt_sz()
1841 if (d->chk_lpt_sz2 && d->new_nhead_offs != len) { in dbg_chk_lpt_sz()
1842 ubifs_err(c, "LPT new nhead offs: expected %d was %d", in dbg_chk_lpt_sz()
1843 d->new_nhead_offs, len); in dbg_chk_lpt_sz()
1844 err = -EINVAL; in dbg_chk_lpt_sz()
1846 lpt_sz = (long long)c->pnode_cnt * c->pnode_sz; in dbg_chk_lpt_sz()
1847 lpt_sz += (long long)c->nnode_cnt * c->nnode_sz; in dbg_chk_lpt_sz()
1848 lpt_sz += c->ltab_sz; in dbg_chk_lpt_sz()
1849 if (c->big_lpt) in dbg_chk_lpt_sz()
1850 lpt_sz += c->lsave_sz; in dbg_chk_lpt_sz()
1851 if (d->chk_lpt_sz - d->chk_lpt_wastage > lpt_sz) { in dbg_chk_lpt_sz()
1852 ubifs_err(c, "LPT chk_lpt_sz %lld + waste %lld exceeds %lld", in dbg_chk_lpt_sz()
1853 d->chk_lpt_sz, d->chk_lpt_wastage, lpt_sz); in dbg_chk_lpt_sz()
1854 err = -EINVAL; in dbg_chk_lpt_sz()
1857 ubifs_dump_lpt_info(c); in dbg_chk_lpt_sz()
1858 ubifs_dump_lpt_lebs(c); in dbg_chk_lpt_sz()
1861 d->chk_lpt_sz2 = d->chk_lpt_sz; in dbg_chk_lpt_sz()
1862 d->chk_lpt_sz = 0; in dbg_chk_lpt_sz()
1863 d->chk_lpt_wastage = 0; in dbg_chk_lpt_sz()
1864 d->chk_lpt_lebs = 0; in dbg_chk_lpt_sz()
1865 d->new_nhead_offs = len; in dbg_chk_lpt_sz()
1868 d->chk_lpt_sz += len; in dbg_chk_lpt_sz()
1869 d->chk_lpt_wastage += len; in dbg_chk_lpt_sz()
1872 return -EINVAL; in dbg_chk_lpt_sz()
1877 * ubifs_dump_lpt_leb - dump an LPT LEB.
1878 * @c: UBIFS file-system description object
1883 * they do not have 8-byte alignments, etc), so we have a separate function to
1886 static void dump_lpt_leb(const struct ubifs_info *c, int lnum) in dump_lpt_leb() argument
1888 int err, len = c->leb_size, node_type, node_num, node_len, offs; in dump_lpt_leb()
1891 pr_err("(pid %d) start dumping LEB %d\n", current->pid, lnum); in dump_lpt_leb()
1892 buf = p = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL); in dump_lpt_leb()
1894 ubifs_err(c, "cannot allocate memory to dump LPT"); in dump_lpt_leb()
1898 err = ubifs_leb_read(c, lnum, buf, 0, c->leb_size, 1); in dump_lpt_leb()
1903 offs = c->leb_size - len; in dump_lpt_leb()
1904 if (!is_a_node(c, p, len)) { in dump_lpt_leb()
1907 pad_len = get_pad_len(c, p, len); in dump_lpt_leb()
1912 len -= pad_len; in dump_lpt_leb()
1921 node_type = get_lpt_node_type(c, p, &node_num); in dump_lpt_leb()
1925 node_len = c->pnode_sz; in dump_lpt_leb()
1926 if (c->big_lpt) in dump_lpt_leb()
1938 node_len = c->nnode_sz; in dump_lpt_leb()
1939 if (c->big_lpt) in dump_lpt_leb()
1945 err = ubifs_unpack_nnode(c, p, &nnode); in dump_lpt_leb()
1954 if (i != UBIFS_LPT_FANOUT - 1) in dump_lpt_leb()
1961 node_len = c->ltab_sz; in dump_lpt_leb()
1965 node_len = c->lsave_sz; in dump_lpt_leb()
1969 ubifs_err(c, "LPT node type %d not recognized", node_type); in dump_lpt_leb()
1974 len -= node_len; in dump_lpt_leb()
1977 pr_err("(pid %d) finish dumping LEB %d\n", current->pid, lnum); in dump_lpt_leb()
1984 * ubifs_dump_lpt_lebs - dump LPT lebs.
1985 * @c: UBIFS file-system description object
1990 void ubifs_dump_lpt_lebs(const struct ubifs_info *c) in ubifs_dump_lpt_lebs() argument
1994 pr_err("(pid %d) start dumping all LPT LEBs\n", current->pid); in ubifs_dump_lpt_lebs()
1995 for (i = 0; i < c->lpt_lebs; i++) in ubifs_dump_lpt_lebs()
1996 dump_lpt_leb(c, i + c->lpt_first); in ubifs_dump_lpt_lebs()
1997 pr_err("(pid %d) finish dumping all LPT LEBs\n", current->pid); in ubifs_dump_lpt_lebs()
2001 * dbg_populate_lsave - debugging version of 'populate_lsave()'
2002 * @c: UBIFS file-system description object
2007 * disabled) an non-zero if lsave has been populated.
2009 static int dbg_populate_lsave(struct ubifs_info *c) in dbg_populate_lsave() argument
2015 if (!dbg_is_chk_gen(c)) in dbg_populate_lsave()
2020 for (i = 0; i < c->lsave_cnt; i++) in dbg_populate_lsave()
2021 c->lsave[i] = c->main_first; in dbg_populate_lsave()
2023 list_for_each_entry(lprops, &c->empty_list, list) in dbg_populate_lsave()
2024 c->lsave[prandom_u32() % c->lsave_cnt] = lprops->lnum; in dbg_populate_lsave()
2025 list_for_each_entry(lprops, &c->freeable_list, list) in dbg_populate_lsave()
2026 c->lsave[prandom_u32() % c->lsave_cnt] = lprops->lnum; in dbg_populate_lsave()
2027 list_for_each_entry(lprops, &c->frdi_idx_list, list) in dbg_populate_lsave()
2028 c->lsave[prandom_u32() % c->lsave_cnt] = lprops->lnum; in dbg_populate_lsave()
2030 heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1]; in dbg_populate_lsave()
2031 for (i = 0; i < heap->cnt; i++) in dbg_populate_lsave()
2032 c->lsave[prandom_u32() % c->lsave_cnt] = heap->arr[i]->lnum; in dbg_populate_lsave()
2033 heap = &c->lpt_heap[LPROPS_DIRTY - 1]; in dbg_populate_lsave()
2034 for (i = 0; i < heap->cnt; i++) in dbg_populate_lsave()
2035 c->lsave[prandom_u32() % c->lsave_cnt] = heap->arr[i]->lnum; in dbg_populate_lsave()
2036 heap = &c->lpt_heap[LPROPS_FREE - 1]; in dbg_populate_lsave()
2037 for (i = 0; i < heap->cnt; i++) in dbg_populate_lsave()
2038 c->lsave[prandom_u32() % c->lsave_cnt] = heap->arr[i]->lnum; in dbg_populate_lsave()