Lines Matching +full:- +full:c
1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2006-2008 Nokia Corporation.
12 * This file implements commit-related functionality of the LEB properties
21 static int dbg_populate_lsave(struct ubifs_info *c);
24 * first_dirty_cnode - find first dirty cnode.
25 * @c: UBIFS file-system description object
30 static struct ubifs_cnode *first_dirty_cnode(const struct ubifs_info *c, struct ubifs_nnode *nnode) in first_dirty_cnode() argument
32 ubifs_assert(c, nnode); in first_dirty_cnode()
39 cnode = nnode->nbranch[i].cnode; in first_dirty_cnode()
41 test_bit(DIRTY_CNODE, &cnode->flags)) { in first_dirty_cnode()
42 if (cnode->level == 0) in first_dirty_cnode()
55 * next_dirty_cnode - find next dirty cnode.
56 * @c: UBIFS file-system description object
61 static struct ubifs_cnode *next_dirty_cnode(const struct ubifs_info *c, struct ubifs_cnode *cnode) in next_dirty_cnode() argument
66 ubifs_assert(c, cnode); in next_dirty_cnode()
67 nnode = cnode->parent; in next_dirty_cnode()
70 for (i = cnode->iip + 1; i < UBIFS_LPT_FANOUT; i++) { in next_dirty_cnode()
71 cnode = nnode->nbranch[i].cnode; in next_dirty_cnode()
72 if (cnode && test_bit(DIRTY_CNODE, &cnode->flags)) { in next_dirty_cnode()
73 if (cnode->level == 0) in next_dirty_cnode()
76 return first_dirty_cnode(c, (struct ubifs_nnode *)cnode); in next_dirty_cnode()
83 * get_cnodes_to_commit - create list of dirty cnodes to commit.
84 * @c: UBIFS file-system description object
88 static int get_cnodes_to_commit(struct ubifs_info *c) in get_cnodes_to_commit() argument
93 if (!c->nroot) in get_cnodes_to_commit()
96 if (!test_bit(DIRTY_CNODE, &c->nroot->flags)) in get_cnodes_to_commit()
99 c->lpt_cnext = first_dirty_cnode(c, c->nroot); in get_cnodes_to_commit()
100 cnode = c->lpt_cnext; in get_cnodes_to_commit()
105 ubifs_assert(c, !test_bit(COW_CNODE, &cnode->flags)); in get_cnodes_to_commit()
106 __set_bit(COW_CNODE, &cnode->flags); in get_cnodes_to_commit()
107 cnext = next_dirty_cnode(c, cnode); in get_cnodes_to_commit()
109 cnode->cnext = c->lpt_cnext; in get_cnodes_to_commit()
112 cnode->cnext = cnext; in get_cnodes_to_commit()
118 ubifs_assert(c, cnt == c->dirty_nn_cnt + c->dirty_pn_cnt); in get_cnodes_to_commit()
123 * upd_ltab - update LPT LEB properties.
124 * @c: UBIFS file-system description object
129 static void upd_ltab(struct ubifs_info *c, int lnum, int free, int dirty) in upd_ltab() argument
132 lnum, c->ltab[lnum - c->lpt_first].free, in upd_ltab()
133 c->ltab[lnum - c->lpt_first].dirty, free, dirty); in upd_ltab()
134 ubifs_assert(c, lnum >= c->lpt_first && lnum <= c->lpt_last); in upd_ltab()
135 c->ltab[lnum - c->lpt_first].free = free; in upd_ltab()
136 c->ltab[lnum - c->lpt_first].dirty += dirty; in upd_ltab()
140 * alloc_lpt_leb - allocate an LPT LEB that is empty.
141 * @c: UBIFS file-system description object
146 * Otherwise the function returns -ENOSPC. Note however, that LPT is designed
149 static int alloc_lpt_leb(struct ubifs_info *c, int *lnum) in alloc_lpt_leb() argument
153 n = *lnum - c->lpt_first + 1; in alloc_lpt_leb()
154 for (i = n; i < c->lpt_lebs; i++) { in alloc_lpt_leb()
155 if (c->ltab[i].tgc || c->ltab[i].cmt) in alloc_lpt_leb()
157 if (c->ltab[i].free == c->leb_size) { in alloc_lpt_leb()
158 c->ltab[i].cmt = 1; in alloc_lpt_leb()
159 *lnum = i + c->lpt_first; in alloc_lpt_leb()
165 if (c->ltab[i].tgc || c->ltab[i].cmt) in alloc_lpt_leb()
167 if (c->ltab[i].free == c->leb_size) { in alloc_lpt_leb()
168 c->ltab[i].cmt = 1; in alloc_lpt_leb()
169 *lnum = i + c->lpt_first; in alloc_lpt_leb()
173 return -ENOSPC; in alloc_lpt_leb()
177 * layout_cnodes - layout cnodes for commit.
178 * @c: UBIFS file-system description object
182 static int layout_cnodes(struct ubifs_info *c) in layout_cnodes() argument
187 err = dbg_chk_lpt_sz(c, 0, 0); in layout_cnodes()
190 cnode = c->lpt_cnext; in layout_cnodes()
193 lnum = c->nhead_lnum; in layout_cnodes()
194 offs = c->nhead_offs; in layout_cnodes()
196 done_lsave = !c->big_lpt; in layout_cnodes()
198 if (!done_lsave && offs + c->lsave_sz <= c->leb_size) { in layout_cnodes()
200 c->lsave_lnum = lnum; in layout_cnodes()
201 c->lsave_offs = offs; in layout_cnodes()
202 offs += c->lsave_sz; in layout_cnodes()
203 dbg_chk_lpt_sz(c, 1, c->lsave_sz); in layout_cnodes()
206 if (offs + c->ltab_sz <= c->leb_size) { in layout_cnodes()
208 c->ltab_lnum = lnum; in layout_cnodes()
209 c->ltab_offs = offs; in layout_cnodes()
210 offs += c->ltab_sz; in layout_cnodes()
211 dbg_chk_lpt_sz(c, 1, c->ltab_sz); in layout_cnodes()
215 if (cnode->level) { in layout_cnodes()
216 len = c->nnode_sz; in layout_cnodes()
217 c->dirty_nn_cnt -= 1; in layout_cnodes()
219 len = c->pnode_sz; in layout_cnodes()
220 c->dirty_pn_cnt -= 1; in layout_cnodes()
222 while (offs + len > c->leb_size) { in layout_cnodes()
223 alen = ALIGN(offs, c->min_io_size); in layout_cnodes()
224 upd_ltab(c, lnum, c->leb_size - alen, alen - offs); in layout_cnodes()
225 dbg_chk_lpt_sz(c, 2, c->leb_size - offs); in layout_cnodes()
226 err = alloc_lpt_leb(c, &lnum); in layout_cnodes()
230 ubifs_assert(c, lnum >= c->lpt_first && in layout_cnodes()
231 lnum <= c->lpt_last); in layout_cnodes()
235 c->lsave_lnum = lnum; in layout_cnodes()
236 c->lsave_offs = offs; in layout_cnodes()
237 offs += c->lsave_sz; in layout_cnodes()
238 dbg_chk_lpt_sz(c, 1, c->lsave_sz); in layout_cnodes()
243 c->ltab_lnum = lnum; in layout_cnodes()
244 c->ltab_offs = offs; in layout_cnodes()
245 offs += c->ltab_sz; in layout_cnodes()
246 dbg_chk_lpt_sz(c, 1, c->ltab_sz); in layout_cnodes()
251 if (cnode->parent) { in layout_cnodes()
252 cnode->parent->nbranch[cnode->iip].lnum = lnum; in layout_cnodes()
253 cnode->parent->nbranch[cnode->iip].offs = offs; in layout_cnodes()
255 c->lpt_lnum = lnum; in layout_cnodes()
256 c->lpt_offs = offs; in layout_cnodes()
259 dbg_chk_lpt_sz(c, 1, len); in layout_cnodes()
260 cnode = cnode->cnext; in layout_cnodes()
261 } while (cnode && cnode != c->lpt_cnext); in layout_cnodes()
265 if (offs + c->lsave_sz > c->leb_size) { in layout_cnodes()
266 alen = ALIGN(offs, c->min_io_size); in layout_cnodes()
267 upd_ltab(c, lnum, c->leb_size - alen, alen - offs); in layout_cnodes()
268 dbg_chk_lpt_sz(c, 2, c->leb_size - offs); in layout_cnodes()
269 err = alloc_lpt_leb(c, &lnum); in layout_cnodes()
273 ubifs_assert(c, lnum >= c->lpt_first && in layout_cnodes()
274 lnum <= c->lpt_last); in layout_cnodes()
277 c->lsave_lnum = lnum; in layout_cnodes()
278 c->lsave_offs = offs; in layout_cnodes()
279 offs += c->lsave_sz; in layout_cnodes()
280 dbg_chk_lpt_sz(c, 1, c->lsave_sz); in layout_cnodes()
285 if (offs + c->ltab_sz > c->leb_size) { in layout_cnodes()
286 alen = ALIGN(offs, c->min_io_size); in layout_cnodes()
287 upd_ltab(c, lnum, c->leb_size - alen, alen - offs); in layout_cnodes()
288 dbg_chk_lpt_sz(c, 2, c->leb_size - offs); in layout_cnodes()
289 err = alloc_lpt_leb(c, &lnum); in layout_cnodes()
293 ubifs_assert(c, lnum >= c->lpt_first && in layout_cnodes()
294 lnum <= c->lpt_last); in layout_cnodes()
296 c->ltab_lnum = lnum; in layout_cnodes()
297 c->ltab_offs = offs; in layout_cnodes()
298 offs += c->ltab_sz; in layout_cnodes()
299 dbg_chk_lpt_sz(c, 1, c->ltab_sz); in layout_cnodes()
302 alen = ALIGN(offs, c->min_io_size); in layout_cnodes()
303 upd_ltab(c, lnum, c->leb_size - alen, alen - offs); in layout_cnodes()
304 dbg_chk_lpt_sz(c, 4, alen - offs); in layout_cnodes()
305 err = dbg_chk_lpt_sz(c, 3, alen); in layout_cnodes()
311 ubifs_err(c, "LPT out of space at LEB %d:%d needing %d, done_ltab %d, done_lsave %d", in layout_cnodes()
313 ubifs_dump_lpt_info(c); in layout_cnodes()
314 ubifs_dump_lpt_lebs(c); in layout_cnodes()
320 * realloc_lpt_leb - allocate an LPT LEB that is empty.
321 * @c: UBIFS file-system description object
330 * the function returns %0. Otherwise the function returns -ENOSPC.
333 static int realloc_lpt_leb(struct ubifs_info *c, int *lnum) in realloc_lpt_leb() argument
337 n = *lnum - c->lpt_first + 1; in realloc_lpt_leb()
338 for (i = n; i < c->lpt_lebs; i++) in realloc_lpt_leb()
339 if (c->ltab[i].cmt) { in realloc_lpt_leb()
340 c->ltab[i].cmt = 0; in realloc_lpt_leb()
341 *lnum = i + c->lpt_first; in realloc_lpt_leb()
346 if (c->ltab[i].cmt) { in realloc_lpt_leb()
347 c->ltab[i].cmt = 0; in realloc_lpt_leb()
348 *lnum = i + c->lpt_first; in realloc_lpt_leb()
351 return -ENOSPC; in realloc_lpt_leb()
355 * write_cnodes - write cnodes for commit.
356 * @c: UBIFS file-system description object
360 static int write_cnodes(struct ubifs_info *c) in write_cnodes() argument
364 void *buf = c->lpt_buf; in write_cnodes()
366 cnode = c->lpt_cnext; in write_cnodes()
369 lnum = c->nhead_lnum; in write_cnodes()
370 offs = c->nhead_offs; in write_cnodes()
374 err = ubifs_leb_unmap(c, lnum); in write_cnodes()
379 done_lsave = !c->big_lpt; in write_cnodes()
381 if (!done_lsave && offs + c->lsave_sz <= c->leb_size) { in write_cnodes()
383 ubifs_pack_lsave(c, buf + offs, c->lsave); in write_cnodes()
384 offs += c->lsave_sz; in write_cnodes()
385 dbg_chk_lpt_sz(c, 1, c->lsave_sz); in write_cnodes()
388 if (offs + c->ltab_sz <= c->leb_size) { in write_cnodes()
390 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt); in write_cnodes()
391 offs += c->ltab_sz; in write_cnodes()
392 dbg_chk_lpt_sz(c, 1, c->ltab_sz); in write_cnodes()
397 if (cnode->level) in write_cnodes()
398 len = c->nnode_sz; in write_cnodes()
400 len = c->pnode_sz; in write_cnodes()
401 while (offs + len > c->leb_size) { in write_cnodes()
402 wlen = offs - from; in write_cnodes()
404 alen = ALIGN(wlen, c->min_io_size); in write_cnodes()
405 memset(buf + offs, 0xff, alen - wlen); in write_cnodes()
406 err = ubifs_leb_write(c, lnum, buf + from, from, in write_cnodes()
411 dbg_chk_lpt_sz(c, 2, c->leb_size - offs); in write_cnodes()
412 err = realloc_lpt_leb(c, &lnum); in write_cnodes()
416 ubifs_assert(c, lnum >= c->lpt_first && in write_cnodes()
417 lnum <= c->lpt_last); in write_cnodes()
418 err = ubifs_leb_unmap(c, lnum); in write_cnodes()
424 ubifs_pack_lsave(c, buf + offs, c->lsave); in write_cnodes()
425 offs += c->lsave_sz; in write_cnodes()
426 dbg_chk_lpt_sz(c, 1, c->lsave_sz); in write_cnodes()
431 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt); in write_cnodes()
432 offs += c->ltab_sz; in write_cnodes()
433 dbg_chk_lpt_sz(c, 1, c->ltab_sz); in write_cnodes()
438 if (cnode->level) in write_cnodes()
439 ubifs_pack_nnode(c, buf + offs, in write_cnodes()
442 ubifs_pack_pnode(c, buf + offs, in write_cnodes()
450 clear_bit(DIRTY_CNODE, &cnode->flags); in write_cnodes()
452 clear_bit(COW_CNODE, &cnode->flags); in write_cnodes()
455 dbg_chk_lpt_sz(c, 1, len); in write_cnodes()
456 cnode = cnode->cnext; in write_cnodes()
457 } while (cnode && cnode != c->lpt_cnext); in write_cnodes()
461 if (offs + c->lsave_sz > c->leb_size) { in write_cnodes()
462 wlen = offs - from; in write_cnodes()
463 alen = ALIGN(wlen, c->min_io_size); in write_cnodes()
464 memset(buf + offs, 0xff, alen - wlen); in write_cnodes()
465 err = ubifs_leb_write(c, lnum, buf + from, from, alen); in write_cnodes()
468 dbg_chk_lpt_sz(c, 2, c->leb_size - offs); in write_cnodes()
469 err = realloc_lpt_leb(c, &lnum); in write_cnodes()
473 ubifs_assert(c, lnum >= c->lpt_first && in write_cnodes()
474 lnum <= c->lpt_last); in write_cnodes()
475 err = ubifs_leb_unmap(c, lnum); in write_cnodes()
480 ubifs_pack_lsave(c, buf + offs, c->lsave); in write_cnodes()
481 offs += c->lsave_sz; in write_cnodes()
482 dbg_chk_lpt_sz(c, 1, c->lsave_sz); in write_cnodes()
487 if (offs + c->ltab_sz > c->leb_size) { in write_cnodes()
488 wlen = offs - from; in write_cnodes()
489 alen = ALIGN(wlen, c->min_io_size); in write_cnodes()
490 memset(buf + offs, 0xff, alen - wlen); in write_cnodes()
491 err = ubifs_leb_write(c, lnum, buf + from, from, alen); in write_cnodes()
494 dbg_chk_lpt_sz(c, 2, c->leb_size - offs); in write_cnodes()
495 err = realloc_lpt_leb(c, &lnum); in write_cnodes()
499 ubifs_assert(c, lnum >= c->lpt_first && in write_cnodes()
500 lnum <= c->lpt_last); in write_cnodes()
501 err = ubifs_leb_unmap(c, lnum); in write_cnodes()
505 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt); in write_cnodes()
506 offs += c->ltab_sz; in write_cnodes()
507 dbg_chk_lpt_sz(c, 1, c->ltab_sz); in write_cnodes()
511 wlen = offs - from; in write_cnodes()
512 alen = ALIGN(wlen, c->min_io_size); in write_cnodes()
513 memset(buf + offs, 0xff, alen - wlen); in write_cnodes()
514 err = ubifs_leb_write(c, lnum, buf + from, from, alen); in write_cnodes()
518 dbg_chk_lpt_sz(c, 4, alen - wlen); in write_cnodes()
519 err = dbg_chk_lpt_sz(c, 3, ALIGN(offs, c->min_io_size)); in write_cnodes()
523 c->nhead_lnum = lnum; in write_cnodes()
524 c->nhead_offs = ALIGN(offs, c->min_io_size); in write_cnodes()
526 dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs); in write_cnodes()
527 dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs); in write_cnodes()
528 dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs); in write_cnodes()
529 if (c->big_lpt) in write_cnodes()
530 dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs); in write_cnodes()
535 ubifs_err(c, "LPT out of space mismatch at LEB %d:%d needing %d, done_ltab %d, done_lsave %d", in write_cnodes()
537 ubifs_dump_lpt_info(c); in write_cnodes()
538 ubifs_dump_lpt_lebs(c); in write_cnodes()
544 * next_pnode_to_dirty - find next pnode to dirty.
545 * @c: UBIFS file-system description object
552 static struct ubifs_pnode *next_pnode_to_dirty(struct ubifs_info *c, in next_pnode_to_dirty() argument
559 nnode = pnode->parent; in next_pnode_to_dirty()
560 for (iip = pnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) { in next_pnode_to_dirty()
561 if (nnode->nbranch[iip].lnum) in next_pnode_to_dirty()
562 return ubifs_get_pnode(c, nnode, iip); in next_pnode_to_dirty()
567 iip = nnode->iip + 1; in next_pnode_to_dirty()
568 nnode = nnode->parent; in next_pnode_to_dirty()
572 if (nnode->nbranch[iip].lnum) in next_pnode_to_dirty()
578 nnode = ubifs_get_nnode(c, nnode, iip); in next_pnode_to_dirty()
583 while (nnode->level > 1) { in next_pnode_to_dirty()
585 if (nnode->nbranch[iip].lnum) in next_pnode_to_dirty()
595 nnode = ubifs_get_nnode(c, nnode, iip); in next_pnode_to_dirty()
601 if (nnode->nbranch[iip].lnum) in next_pnode_to_dirty()
606 return ubifs_get_pnode(c, nnode, iip); in next_pnode_to_dirty()
610 * add_pnode_dirt - add dirty space to LPT LEB properties.
611 * @c: UBIFS file-system description object
614 static void add_pnode_dirt(struct ubifs_info *c, struct ubifs_pnode *pnode) in add_pnode_dirt() argument
616 ubifs_add_lpt_dirt(c, pnode->parent->nbranch[pnode->iip].lnum, in add_pnode_dirt()
617 c->pnode_sz); in add_pnode_dirt()
621 * do_make_pnode_dirty - mark a pnode dirty.
622 * @c: UBIFS file-system description object
625 static void do_make_pnode_dirty(struct ubifs_info *c, struct ubifs_pnode *pnode) in do_make_pnode_dirty() argument
628 if (!test_and_set_bit(DIRTY_CNODE, &pnode->flags)) { in do_make_pnode_dirty()
631 c->dirty_pn_cnt += 1; in do_make_pnode_dirty()
632 add_pnode_dirt(c, pnode); in do_make_pnode_dirty()
634 nnode = pnode->parent; in do_make_pnode_dirty()
636 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) { in do_make_pnode_dirty()
637 c->dirty_nn_cnt += 1; in do_make_pnode_dirty()
638 ubifs_add_nnode_dirt(c, nnode); in do_make_pnode_dirty()
639 nnode = nnode->parent; in do_make_pnode_dirty()
647 * make_tree_dirty - mark the entire LEB properties tree dirty.
648 * @c: UBIFS file-system description object
657 static int make_tree_dirty(struct ubifs_info *c) in make_tree_dirty() argument
661 pnode = ubifs_pnode_lookup(c, 0); in make_tree_dirty()
666 do_make_pnode_dirty(c, pnode); in make_tree_dirty()
667 pnode = next_pnode_to_dirty(c, pnode); in make_tree_dirty()
675 * need_write_all - determine if the LPT area is running out of free space.
676 * @c: UBIFS file-system description object
681 static int need_write_all(struct ubifs_info *c) in need_write_all() argument
686 for (i = 0; i < c->lpt_lebs; i++) { in need_write_all()
687 if (i + c->lpt_first == c->nhead_lnum) in need_write_all()
688 free += c->leb_size - c->nhead_offs; in need_write_all()
689 else if (c->ltab[i].free == c->leb_size) in need_write_all()
690 free += c->leb_size; in need_write_all()
691 else if (c->ltab[i].free + c->ltab[i].dirty == c->leb_size) in need_write_all()
692 free += c->leb_size; in need_write_all()
695 if (free <= c->lpt_sz * 2) in need_write_all()
701 * lpt_tgc_start - start trivial garbage collection of LPT LEBs.
702 * @c: UBIFS file-system description object
708 static void lpt_tgc_start(struct ubifs_info *c) in lpt_tgc_start() argument
712 for (i = 0; i < c->lpt_lebs; i++) { in lpt_tgc_start()
713 if (i + c->lpt_first == c->nhead_lnum) in lpt_tgc_start()
715 if (c->ltab[i].dirty > 0 && in lpt_tgc_start()
716 c->ltab[i].free + c->ltab[i].dirty == c->leb_size) { in lpt_tgc_start()
717 c->ltab[i].tgc = 1; in lpt_tgc_start()
718 c->ltab[i].free = c->leb_size; in lpt_tgc_start()
719 c->ltab[i].dirty = 0; in lpt_tgc_start()
720 dbg_lp("LEB %d", i + c->lpt_first); in lpt_tgc_start()
726 * lpt_tgc_end - end trivial garbage collection of LPT LEBs.
727 * @c: UBIFS file-system description object
732 * written) and un-maps LPT LEBs that were marked for trivial GC.
734 static int lpt_tgc_end(struct ubifs_info *c) in lpt_tgc_end() argument
738 for (i = 0; i < c->lpt_lebs; i++) in lpt_tgc_end()
739 if (c->ltab[i].tgc) { in lpt_tgc_end()
740 err = ubifs_leb_unmap(c, i + c->lpt_first); in lpt_tgc_end()
743 c->ltab[i].tgc = 0; in lpt_tgc_end()
744 dbg_lp("LEB %d", i + c->lpt_first); in lpt_tgc_end()
750 * populate_lsave - fill the lsave array with important LEB numbers.
751 * @c: the UBIFS file-system description object
761 static void populate_lsave(struct ubifs_info *c) in populate_lsave() argument
767 ubifs_assert(c, c->big_lpt); in populate_lsave()
768 if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) { in populate_lsave()
769 c->lpt_drty_flgs |= LSAVE_DIRTY; in populate_lsave()
770 ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz); in populate_lsave()
773 if (dbg_populate_lsave(c)) in populate_lsave()
776 list_for_each_entry(lprops, &c->empty_list, list) { in populate_lsave()
777 c->lsave[cnt++] = lprops->lnum; in populate_lsave()
778 if (cnt >= c->lsave_cnt) in populate_lsave()
781 list_for_each_entry(lprops, &c->freeable_list, list) { in populate_lsave()
782 c->lsave[cnt++] = lprops->lnum; in populate_lsave()
783 if (cnt >= c->lsave_cnt) in populate_lsave()
786 list_for_each_entry(lprops, &c->frdi_idx_list, list) { in populate_lsave()
787 c->lsave[cnt++] = lprops->lnum; in populate_lsave()
788 if (cnt >= c->lsave_cnt) in populate_lsave()
791 heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1]; in populate_lsave()
792 for (i = 0; i < heap->cnt; i++) { in populate_lsave()
793 c->lsave[cnt++] = heap->arr[i]->lnum; in populate_lsave()
794 if (cnt >= c->lsave_cnt) in populate_lsave()
797 heap = &c->lpt_heap[LPROPS_DIRTY - 1]; in populate_lsave()
798 for (i = 0; i < heap->cnt; i++) { in populate_lsave()
799 c->lsave[cnt++] = heap->arr[i]->lnum; in populate_lsave()
800 if (cnt >= c->lsave_cnt) in populate_lsave()
803 heap = &c->lpt_heap[LPROPS_FREE - 1]; in populate_lsave()
804 for (i = 0; i < heap->cnt; i++) { in populate_lsave()
805 c->lsave[cnt++] = heap->arr[i]->lnum; in populate_lsave()
806 if (cnt >= c->lsave_cnt) in populate_lsave()
810 while (cnt < c->lsave_cnt) in populate_lsave()
811 c->lsave[cnt++] = c->main_first; in populate_lsave()
815 * nnode_lookup - lookup a nnode in the LPT.
816 * @c: UBIFS file-system description object
822 static struct ubifs_nnode *nnode_lookup(struct ubifs_info *c, int i) in nnode_lookup() argument
827 if (!c->nroot) { in nnode_lookup()
828 err = ubifs_read_nnode(c, NULL, 0); in nnode_lookup()
832 nnode = c->nroot; in nnode_lookup()
834 iip = i & (UBIFS_LPT_FANOUT - 1); in nnode_lookup()
838 nnode = ubifs_get_nnode(c, nnode, iip); in nnode_lookup()
846 * make_nnode_dirty - find a nnode and, if found, make it dirty.
847 * @c: UBIFS file-system description object
853 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
854 * simply involves marking all the nodes in the LEB being garbage-collected as
860 static int make_nnode_dirty(struct ubifs_info *c, int node_num, int lnum, in make_nnode_dirty() argument
865 nnode = nnode_lookup(c, node_num); in make_nnode_dirty()
868 if (nnode->parent) { in make_nnode_dirty()
871 branch = &nnode->parent->nbranch[nnode->iip]; in make_nnode_dirty()
872 if (branch->lnum != lnum || branch->offs != offs) in make_nnode_dirty()
874 } else if (c->lpt_lnum != lnum || c->lpt_offs != offs) in make_nnode_dirty()
877 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) { in make_nnode_dirty()
878 c->dirty_nn_cnt += 1; in make_nnode_dirty()
879 ubifs_add_nnode_dirt(c, nnode); in make_nnode_dirty()
881 nnode = nnode->parent; in make_nnode_dirty()
883 if (!test_and_set_bit(DIRTY_CNODE, &nnode->flags)) { in make_nnode_dirty()
884 c->dirty_nn_cnt += 1; in make_nnode_dirty()
885 ubifs_add_nnode_dirt(c, nnode); in make_nnode_dirty()
886 nnode = nnode->parent; in make_nnode_dirty()
895 * make_pnode_dirty - find a pnode and, if found, make it dirty.
896 * @c: UBIFS file-system description object
902 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
903 * simply involves marking all the nodes in the LEB being garbage-collected as
909 static int make_pnode_dirty(struct ubifs_info *c, int node_num, int lnum, in make_pnode_dirty() argument
915 pnode = ubifs_pnode_lookup(c, node_num); in make_pnode_dirty()
918 branch = &pnode->parent->nbranch[pnode->iip]; in make_pnode_dirty()
919 if (branch->lnum != lnum || branch->offs != offs) in make_pnode_dirty()
921 do_make_pnode_dirty(c, pnode); in make_pnode_dirty()
926 * make_ltab_dirty - make ltab node dirty.
927 * @c: UBIFS file-system description object
932 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
933 * simply involves marking all the nodes in the LEB being garbage-collected as
939 static int make_ltab_dirty(struct ubifs_info *c, int lnum, int offs) in make_ltab_dirty() argument
941 if (lnum != c->ltab_lnum || offs != c->ltab_offs) in make_ltab_dirty()
943 if (!(c->lpt_drty_flgs & LTAB_DIRTY)) { in make_ltab_dirty()
944 c->lpt_drty_flgs |= LTAB_DIRTY; in make_ltab_dirty()
945 ubifs_add_lpt_dirt(c, c->ltab_lnum, c->ltab_sz); in make_ltab_dirty()
951 * make_lsave_dirty - make lsave node dirty.
952 * @c: UBIFS file-system description object
957 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
958 * simply involves marking all the nodes in the LEB being garbage-collected as
964 static int make_lsave_dirty(struct ubifs_info *c, int lnum, int offs) in make_lsave_dirty() argument
966 if (lnum != c->lsave_lnum || offs != c->lsave_offs) in make_lsave_dirty()
968 if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) { in make_lsave_dirty()
969 c->lpt_drty_flgs |= LSAVE_DIRTY; in make_lsave_dirty()
970 ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz); in make_lsave_dirty()
976 * make_node_dirty - make node dirty.
977 * @c: UBIFS file-system description object
984 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
985 * simply involves marking all the nodes in the LEB being garbage-collected as
991 static int make_node_dirty(struct ubifs_info *c, int node_type, int node_num, in make_node_dirty() argument
996 return make_nnode_dirty(c, node_num, lnum, offs); in make_node_dirty()
998 return make_pnode_dirty(c, node_num, lnum, offs); in make_node_dirty()
1000 return make_ltab_dirty(c, lnum, offs); in make_node_dirty()
1002 return make_lsave_dirty(c, lnum, offs); in make_node_dirty()
1004 return -EINVAL; in make_node_dirty()
1008 * get_lpt_node_len - return the length of a node based on its type.
1009 * @c: UBIFS file-system description object
1012 static int get_lpt_node_len(const struct ubifs_info *c, int node_type) in get_lpt_node_len() argument
1016 return c->nnode_sz; in get_lpt_node_len()
1018 return c->pnode_sz; in get_lpt_node_len()
1020 return c->ltab_sz; in get_lpt_node_len()
1022 return c->lsave_sz; in get_lpt_node_len()
1028 * get_pad_len - return the length of padding in a buffer.
1029 * @c: UBIFS file-system description object
1033 static int get_pad_len(const struct ubifs_info *c, uint8_t *buf, int len) in get_pad_len() argument
1037 if (c->min_io_size == 1) in get_pad_len()
1039 offs = c->leb_size - len; in get_pad_len()
1040 pad_len = ALIGN(offs, c->min_io_size) - offs; in get_pad_len()
1045 * get_lpt_node_type - return type (and node number) of a node in a buffer.
1046 * @c: UBIFS file-system description object
1050 static int get_lpt_node_type(const struct ubifs_info *c, uint8_t *buf, in get_lpt_node_type() argument
1056 node_type = ubifs_unpack_bits(c, &addr, &pos, UBIFS_LPT_TYPE_BITS); in get_lpt_node_type()
1057 *node_num = ubifs_unpack_bits(c, &addr, &pos, c->pcnt_bits); in get_lpt_node_type()
1062 * is_a_node - determine if a buffer contains a node.
1063 * @c: UBIFS file-system description object
1069 static int is_a_node(const struct ubifs_info *c, uint8_t *buf, int len) in is_a_node() argument
1077 node_type = ubifs_unpack_bits(c, &addr, &pos, UBIFS_LPT_TYPE_BITS); in is_a_node()
1080 node_len = get_lpt_node_len(c, node_type); in is_a_node()
1085 crc = ubifs_unpack_bits(c, &addr, &pos, UBIFS_LPT_CRC_BITS); in is_a_node()
1086 calc_crc = crc16(-1, buf + UBIFS_LPT_CRC_BYTES, in is_a_node()
1087 node_len - UBIFS_LPT_CRC_BYTES); in is_a_node()
1094 * lpt_gc_lnum - garbage collect a LPT LEB.
1095 * @c: UBIFS file-system description object
1099 * (c->big_lpt == 1). Garbage collection simply involves marking all the nodes
1100 * in the LEB being garbage-collected as dirty. The dirty nodes are written
1105 static int lpt_gc_lnum(struct ubifs_info *c, int lnum) in lpt_gc_lnum() argument
1107 int err, len = c->leb_size, node_type, node_num, node_len, offs; in lpt_gc_lnum()
1108 void *buf = c->lpt_buf; in lpt_gc_lnum()
1112 err = ubifs_leb_read(c, lnum, buf, 0, c->leb_size, 1); in lpt_gc_lnum()
1117 if (!is_a_node(c, buf, len)) { in lpt_gc_lnum()
1120 pad_len = get_pad_len(c, buf, len); in lpt_gc_lnum()
1123 len -= pad_len; in lpt_gc_lnum()
1128 node_type = get_lpt_node_type(c, buf, &node_num); in lpt_gc_lnum()
1129 node_len = get_lpt_node_len(c, node_type); in lpt_gc_lnum()
1130 offs = c->leb_size - len; in lpt_gc_lnum()
1131 ubifs_assert(c, node_len != 0); in lpt_gc_lnum()
1132 mutex_lock(&c->lp_mutex); in lpt_gc_lnum()
1133 err = make_node_dirty(c, node_type, node_num, lnum, offs); in lpt_gc_lnum()
1134 mutex_unlock(&c->lp_mutex); in lpt_gc_lnum()
1138 len -= node_len; in lpt_gc_lnum()
1144 * lpt_gc - LPT garbage collection.
1145 * @c: UBIFS file-system description object
1150 static int lpt_gc(struct ubifs_info *c) in lpt_gc() argument
1152 int i, lnum = -1, dirty = 0; in lpt_gc()
1154 mutex_lock(&c->lp_mutex); in lpt_gc()
1155 for (i = 0; i < c->lpt_lebs; i++) { in lpt_gc()
1156 ubifs_assert(c, !c->ltab[i].tgc); in lpt_gc()
1157 if (i + c->lpt_first == c->nhead_lnum || in lpt_gc()
1158 c->ltab[i].free + c->ltab[i].dirty == c->leb_size) in lpt_gc()
1160 if (c->ltab[i].dirty > dirty) { in lpt_gc()
1161 dirty = c->ltab[i].dirty; in lpt_gc()
1162 lnum = i + c->lpt_first; in lpt_gc()
1165 mutex_unlock(&c->lp_mutex); in lpt_gc()
1166 if (lnum == -1) in lpt_gc()
1167 return -ENOSPC; in lpt_gc()
1168 return lpt_gc_lnum(c, lnum); in lpt_gc()
1172 * ubifs_lpt_start_commit - UBIFS commit starts.
1173 * @c: the UBIFS file-system description object
1181 int ubifs_lpt_start_commit(struct ubifs_info *c) in ubifs_lpt_start_commit() argument
1187 mutex_lock(&c->lp_mutex); in ubifs_lpt_start_commit()
1188 err = dbg_chk_lpt_free_spc(c); in ubifs_lpt_start_commit()
1191 err = dbg_check_ltab(c); in ubifs_lpt_start_commit()
1195 if (c->check_lpt_free) { in ubifs_lpt_start_commit()
1202 c->check_lpt_free = 0; in ubifs_lpt_start_commit()
1203 while (need_write_all(c)) { in ubifs_lpt_start_commit()
1204 mutex_unlock(&c->lp_mutex); in ubifs_lpt_start_commit()
1205 err = lpt_gc(c); in ubifs_lpt_start_commit()
1208 mutex_lock(&c->lp_mutex); in ubifs_lpt_start_commit()
1212 lpt_tgc_start(c); in ubifs_lpt_start_commit()
1214 if (!c->dirty_pn_cnt) { in ubifs_lpt_start_commit()
1220 if (!c->big_lpt && need_write_all(c)) { in ubifs_lpt_start_commit()
1222 err = make_tree_dirty(c); in ubifs_lpt_start_commit()
1225 lpt_tgc_start(c); in ubifs_lpt_start_commit()
1228 if (c->big_lpt) in ubifs_lpt_start_commit()
1229 populate_lsave(c); in ubifs_lpt_start_commit()
1231 cnt = get_cnodes_to_commit(c); in ubifs_lpt_start_commit()
1232 ubifs_assert(c, cnt != 0); in ubifs_lpt_start_commit()
1234 err = layout_cnodes(c); in ubifs_lpt_start_commit()
1238 err = ubifs_lpt_calc_hash(c, c->mst_node->hash_lpt); in ubifs_lpt_start_commit()
1243 memcpy(c->ltab_cmt, c->ltab, in ubifs_lpt_start_commit()
1244 sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs); in ubifs_lpt_start_commit()
1245 c->lpt_drty_flgs &= ~(LTAB_DIRTY | LSAVE_DIRTY); in ubifs_lpt_start_commit()
1248 mutex_unlock(&c->lp_mutex); in ubifs_lpt_start_commit()
1253 * free_obsolete_cnodes - free obsolete cnodes for commit end.
1254 * @c: UBIFS file-system description object
1256 static void free_obsolete_cnodes(struct ubifs_info *c) in free_obsolete_cnodes() argument
1260 cnext = c->lpt_cnext; in free_obsolete_cnodes()
1265 cnext = cnode->cnext; in free_obsolete_cnodes()
1266 if (test_bit(OBSOLETE_CNODE, &cnode->flags)) in free_obsolete_cnodes()
1269 cnode->cnext = NULL; in free_obsolete_cnodes()
1270 } while (cnext != c->lpt_cnext); in free_obsolete_cnodes()
1271 c->lpt_cnext = NULL; in free_obsolete_cnodes()
1275 * ubifs_lpt_end_commit - finish the commit operation.
1276 * @c: the UBIFS file-system description object
1283 int ubifs_lpt_end_commit(struct ubifs_info *c) in ubifs_lpt_end_commit() argument
1289 if (!c->lpt_cnext) in ubifs_lpt_end_commit()
1292 err = write_cnodes(c); in ubifs_lpt_end_commit()
1296 mutex_lock(&c->lp_mutex); in ubifs_lpt_end_commit()
1297 free_obsolete_cnodes(c); in ubifs_lpt_end_commit()
1298 mutex_unlock(&c->lp_mutex); in ubifs_lpt_end_commit()
1304 * ubifs_lpt_post_commit - post commit LPT trivial GC and LPT GC.
1305 * @c: UBIFS file-system description object
1310 int ubifs_lpt_post_commit(struct ubifs_info *c) in ubifs_lpt_post_commit() argument
1314 mutex_lock(&c->lp_mutex); in ubifs_lpt_post_commit()
1315 err = lpt_tgc_end(c); in ubifs_lpt_post_commit()
1318 if (c->big_lpt) in ubifs_lpt_post_commit()
1319 while (need_write_all(c)) { in ubifs_lpt_post_commit()
1320 mutex_unlock(&c->lp_mutex); in ubifs_lpt_post_commit()
1321 err = lpt_gc(c); in ubifs_lpt_post_commit()
1324 mutex_lock(&c->lp_mutex); in ubifs_lpt_post_commit()
1327 mutex_unlock(&c->lp_mutex); in ubifs_lpt_post_commit()
1332 * first_nnode - find the first nnode in memory.
1333 * @c: UBIFS file-system description object
1339 static struct ubifs_nnode *first_nnode(struct ubifs_info *c, int *hght) in first_nnode() argument
1344 nnode = c->nroot; in first_nnode()
1348 for (h = 1; h < c->lpt_hght; h++) { in first_nnode()
1351 if (nnode->nbranch[i].nnode) { in first_nnode()
1353 nnode = nnode->nbranch[i].nnode; in first_nnode()
1365 * next_nnode - find the next nnode in memory.
1366 * @c: UBIFS file-system description object
1373 static struct ubifs_nnode *next_nnode(struct ubifs_info *c, in next_nnode() argument
1379 parent = nnode->parent; in next_nnode()
1382 if (nnode->iip == UBIFS_LPT_FANOUT - 1) { in next_nnode()
1383 *hght -= 1; in next_nnode()
1386 for (iip = nnode->iip + 1; iip < UBIFS_LPT_FANOUT; iip++) { in next_nnode()
1387 nnode = parent->nbranch[iip].nnode; in next_nnode()
1392 *hght -= 1; in next_nnode()
1395 for (h = *hght + 1; h < c->lpt_hght; h++) { in next_nnode()
1398 if (nnode->nbranch[i].nnode) { in next_nnode()
1400 nnode = nnode->nbranch[i].nnode; in next_nnode()
1412 * ubifs_lpt_free - free resources owned by the LPT.
1413 * @c: UBIFS file-system description object
1416 void ubifs_lpt_free(struct ubifs_info *c, int wr_only) in ubifs_lpt_free() argument
1421 /* Free write-only things first */ in ubifs_lpt_free()
1423 free_obsolete_cnodes(c); /* Leftover from a failed commit */ in ubifs_lpt_free()
1425 vfree(c->ltab_cmt); in ubifs_lpt_free()
1426 c->ltab_cmt = NULL; in ubifs_lpt_free()
1427 vfree(c->lpt_buf); in ubifs_lpt_free()
1428 c->lpt_buf = NULL; in ubifs_lpt_free()
1429 kfree(c->lsave); in ubifs_lpt_free()
1430 c->lsave = NULL; in ubifs_lpt_free()
1437 nnode = first_nnode(c, &hght); in ubifs_lpt_free()
1440 kfree(nnode->nbranch[i].nnode); in ubifs_lpt_free()
1441 nnode = next_nnode(c, nnode, &hght); in ubifs_lpt_free()
1444 kfree(c->lpt_heap[i].arr); in ubifs_lpt_free()
1445 kfree(c->dirty_idx.arr); in ubifs_lpt_free()
1446 kfree(c->nroot); in ubifs_lpt_free()
1447 vfree(c->ltab); in ubifs_lpt_free()
1448 kfree(c->lpt_nod_buf); in ubifs_lpt_free()
1456 * dbg_is_all_ff - determine if a buffer contains only 0xFF bytes.
1471 * dbg_is_nnode_dirty - determine if a nnode is dirty.
1472 * @c: the UBIFS file-system description object
1476 static int dbg_is_nnode_dirty(struct ubifs_info *c, int lnum, int offs) in dbg_is_nnode_dirty() argument
1482 nnode = first_nnode(c, &hght); in dbg_is_nnode_dirty()
1483 for (; nnode; nnode = next_nnode(c, nnode, &hght)) { in dbg_is_nnode_dirty()
1487 if (nnode->parent) { in dbg_is_nnode_dirty()
1488 branch = &nnode->parent->nbranch[nnode->iip]; in dbg_is_nnode_dirty()
1489 if (branch->lnum != lnum || branch->offs != offs) in dbg_is_nnode_dirty()
1491 if (test_bit(DIRTY_CNODE, &nnode->flags)) in dbg_is_nnode_dirty()
1495 if (c->lpt_lnum != lnum || c->lpt_offs != offs) in dbg_is_nnode_dirty()
1497 if (test_bit(DIRTY_CNODE, &nnode->flags)) in dbg_is_nnode_dirty()
1506 * dbg_is_pnode_dirty - determine if a pnode is dirty.
1507 * @c: the UBIFS file-system description object
1511 static int dbg_is_pnode_dirty(struct ubifs_info *c, int lnum, int offs) in dbg_is_pnode_dirty() argument
1515 cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT); in dbg_is_pnode_dirty()
1521 pnode = ubifs_pnode_lookup(c, i); in dbg_is_pnode_dirty()
1524 branch = &pnode->parent->nbranch[pnode->iip]; in dbg_is_pnode_dirty()
1525 if (branch->lnum != lnum || branch->offs != offs) in dbg_is_pnode_dirty()
1527 if (test_bit(DIRTY_CNODE, &pnode->flags)) in dbg_is_pnode_dirty()
1535 * dbg_is_ltab_dirty - determine if a ltab node is dirty.
1536 * @c: the UBIFS file-system description object
1540 static int dbg_is_ltab_dirty(struct ubifs_info *c, int lnum, int offs) in dbg_is_ltab_dirty() argument
1542 if (lnum != c->ltab_lnum || offs != c->ltab_offs) in dbg_is_ltab_dirty()
1544 return (c->lpt_drty_flgs & LTAB_DIRTY) != 0; in dbg_is_ltab_dirty()
1548 * dbg_is_lsave_dirty - determine if a lsave node is dirty.
1549 * @c: the UBIFS file-system description object
1553 static int dbg_is_lsave_dirty(struct ubifs_info *c, int lnum, int offs) in dbg_is_lsave_dirty() argument
1555 if (lnum != c->lsave_lnum || offs != c->lsave_offs) in dbg_is_lsave_dirty()
1557 return (c->lpt_drty_flgs & LSAVE_DIRTY) != 0; in dbg_is_lsave_dirty()
1561 * dbg_is_node_dirty - determine if a node is dirty.
1562 * @c: the UBIFS file-system description object
1567 static int dbg_is_node_dirty(struct ubifs_info *c, int node_type, int lnum, in dbg_is_node_dirty() argument
1572 return dbg_is_nnode_dirty(c, lnum, offs); in dbg_is_node_dirty()
1574 return dbg_is_pnode_dirty(c, lnum, offs); in dbg_is_node_dirty()
1576 return dbg_is_ltab_dirty(c, lnum, offs); in dbg_is_node_dirty()
1578 return dbg_is_lsave_dirty(c, lnum, offs); in dbg_is_node_dirty()
1584 * dbg_check_ltab_lnum - check the ltab for a LPT LEB number.
1585 * @c: the UBIFS file-system description object
1590 static int dbg_check_ltab_lnum(struct ubifs_info *c, int lnum) in dbg_check_ltab_lnum() argument
1592 int err, len = c->leb_size, dirty = 0, node_type, node_num, node_len; in dbg_check_ltab_lnum()
1596 if (!dbg_is_chk_lprops(c)) in dbg_check_ltab_lnum()
1599 buf = p = __vmalloc(c->leb_size, GFP_NOFS); in dbg_check_ltab_lnum()
1601 ubifs_err(c, "cannot allocate memory for ltab checking"); in dbg_check_ltab_lnum()
1607 err = ubifs_leb_read(c, lnum, buf, 0, c->leb_size, 1); in dbg_check_ltab_lnum()
1612 if (!is_a_node(c, p, len)) { in dbg_check_ltab_lnum()
1615 pad_len = get_pad_len(c, p, len); in dbg_check_ltab_lnum()
1618 len -= pad_len; in dbg_check_ltab_lnum()
1623 ubifs_err(c, "invalid empty space in LEB %d at %d", in dbg_check_ltab_lnum()
1624 lnum, c->leb_size - len); in dbg_check_ltab_lnum()
1625 err = -EINVAL; in dbg_check_ltab_lnum()
1627 i = lnum - c->lpt_first; in dbg_check_ltab_lnum()
1628 if (len != c->ltab[i].free) { in dbg_check_ltab_lnum()
1629 ubifs_err(c, "invalid free space in LEB %d (free %d, expected %d)", in dbg_check_ltab_lnum()
1630 lnum, len, c->ltab[i].free); in dbg_check_ltab_lnum()
1631 err = -EINVAL; in dbg_check_ltab_lnum()
1633 if (dirty != c->ltab[i].dirty) { in dbg_check_ltab_lnum()
1634 ubifs_err(c, "invalid dirty space in LEB %d (dirty %d, expected %d)", in dbg_check_ltab_lnum()
1635 lnum, dirty, c->ltab[i].dirty); in dbg_check_ltab_lnum()
1636 err = -EINVAL; in dbg_check_ltab_lnum()
1640 node_type = get_lpt_node_type(c, p, &node_num); in dbg_check_ltab_lnum()
1641 node_len = get_lpt_node_len(c, node_type); in dbg_check_ltab_lnum()
1642 ret = dbg_is_node_dirty(c, node_type, lnum, c->leb_size - len); in dbg_check_ltab_lnum()
1646 len -= node_len; in dbg_check_ltab_lnum()
1655 * dbg_check_ltab - check the free and dirty space in the ltab.
1656 * @c: the UBIFS file-system description object
1660 int dbg_check_ltab(struct ubifs_info *c) in dbg_check_ltab() argument
1664 if (!dbg_is_chk_lprops(c)) in dbg_check_ltab()
1668 cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT); in dbg_check_ltab()
1672 pnode = ubifs_pnode_lookup(c, i); in dbg_check_ltab()
1679 err = dbg_check_lpt_nodes(c, (struct ubifs_cnode *)c->nroot, 0, 0); in dbg_check_ltab()
1684 for (lnum = c->lpt_first; lnum <= c->lpt_last; lnum++) { in dbg_check_ltab()
1685 err = dbg_check_ltab_lnum(c, lnum); in dbg_check_ltab()
1687 ubifs_err(c, "failed at LEB %d", lnum); in dbg_check_ltab()
1697 * dbg_chk_lpt_free_spc - check LPT free space is enough to write entire LPT.
1698 * @c: the UBIFS file-system description object
1702 int dbg_chk_lpt_free_spc(struct ubifs_info *c) in dbg_chk_lpt_free_spc() argument
1707 if (!dbg_is_chk_lprops(c)) in dbg_chk_lpt_free_spc()
1710 for (i = 0; i < c->lpt_lebs; i++) { in dbg_chk_lpt_free_spc()
1711 if (c->ltab[i].tgc || c->ltab[i].cmt) in dbg_chk_lpt_free_spc()
1713 if (i + c->lpt_first == c->nhead_lnum) in dbg_chk_lpt_free_spc()
1714 free += c->leb_size - c->nhead_offs; in dbg_chk_lpt_free_spc()
1715 else if (c->ltab[i].free == c->leb_size) in dbg_chk_lpt_free_spc()
1716 free += c->leb_size; in dbg_chk_lpt_free_spc()
1718 if (free < c->lpt_sz) { in dbg_chk_lpt_free_spc()
1719 ubifs_err(c, "LPT space error: free %lld lpt_sz %lld", in dbg_chk_lpt_free_spc()
1720 free, c->lpt_sz); in dbg_chk_lpt_free_spc()
1721 ubifs_dump_lpt_info(c); in dbg_chk_lpt_free_spc()
1722 ubifs_dump_lpt_lebs(c); in dbg_chk_lpt_free_spc()
1724 return -EINVAL; in dbg_chk_lpt_free_spc()
1730 * dbg_chk_lpt_sz - check LPT does not write more than LPT size.
1731 * @c: the UBIFS file-system description object
1737 * o %0 - LPT debugging checking starts, initialize debugging variables;
1738 * o %1 - wrote an LPT node, increase LPT size by @len bytes;
1739 * o %2 - switched to a different LEB and wasted @len bytes;
1740 * o %3 - check that we've written the right number of bytes.
1741 * o %4 - wasted @len bytes;
1743 int dbg_chk_lpt_sz(struct ubifs_info *c, int action, int len) in dbg_chk_lpt_sz() argument
1745 struct ubifs_debug_info *d = c->dbg; in dbg_chk_lpt_sz()
1749 if (!dbg_is_chk_lprops(c)) in dbg_chk_lpt_sz()
1754 d->chk_lpt_sz = 0; in dbg_chk_lpt_sz()
1755 d->chk_lpt_sz2 = 0; in dbg_chk_lpt_sz()
1756 d->chk_lpt_lebs = 0; in dbg_chk_lpt_sz()
1757 d->chk_lpt_wastage = 0; in dbg_chk_lpt_sz()
1758 if (c->dirty_pn_cnt > c->pnode_cnt) { in dbg_chk_lpt_sz()
1759 ubifs_err(c, "dirty pnodes %d exceed max %d", in dbg_chk_lpt_sz()
1760 c->dirty_pn_cnt, c->pnode_cnt); in dbg_chk_lpt_sz()
1761 err = -EINVAL; in dbg_chk_lpt_sz()
1763 if (c->dirty_nn_cnt > c->nnode_cnt) { in dbg_chk_lpt_sz()
1764 ubifs_err(c, "dirty nnodes %d exceed max %d", in dbg_chk_lpt_sz()
1765 c->dirty_nn_cnt, c->nnode_cnt); in dbg_chk_lpt_sz()
1766 err = -EINVAL; in dbg_chk_lpt_sz()
1770 d->chk_lpt_sz += len; in dbg_chk_lpt_sz()
1773 d->chk_lpt_sz += len; in dbg_chk_lpt_sz()
1774 d->chk_lpt_wastage += len; in dbg_chk_lpt_sz()
1775 d->chk_lpt_lebs += 1; in dbg_chk_lpt_sz()
1778 chk_lpt_sz = c->leb_size; in dbg_chk_lpt_sz()
1779 chk_lpt_sz *= d->chk_lpt_lebs; in dbg_chk_lpt_sz()
1780 chk_lpt_sz += len - c->nhead_offs; in dbg_chk_lpt_sz()
1781 if (d->chk_lpt_sz != chk_lpt_sz) { in dbg_chk_lpt_sz()
1782 ubifs_err(c, "LPT wrote %lld but space used was %lld", in dbg_chk_lpt_sz()
1783 d->chk_lpt_sz, chk_lpt_sz); in dbg_chk_lpt_sz()
1784 err = -EINVAL; in dbg_chk_lpt_sz()
1786 if (d->chk_lpt_sz > c->lpt_sz) { in dbg_chk_lpt_sz()
1787 ubifs_err(c, "LPT wrote %lld but lpt_sz is %lld", in dbg_chk_lpt_sz()
1788 d->chk_lpt_sz, c->lpt_sz); in dbg_chk_lpt_sz()
1789 err = -EINVAL; in dbg_chk_lpt_sz()
1791 if (d->chk_lpt_sz2 && d->chk_lpt_sz != d->chk_lpt_sz2) { in dbg_chk_lpt_sz()
1792 ubifs_err(c, "LPT layout size %lld but wrote %lld", in dbg_chk_lpt_sz()
1793 d->chk_lpt_sz, d->chk_lpt_sz2); in dbg_chk_lpt_sz()
1794 err = -EINVAL; in dbg_chk_lpt_sz()
1796 if (d->chk_lpt_sz2 && d->new_nhead_offs != len) { in dbg_chk_lpt_sz()
1797 ubifs_err(c, "LPT new nhead offs: expected %d was %d", in dbg_chk_lpt_sz()
1798 d->new_nhead_offs, len); in dbg_chk_lpt_sz()
1799 err = -EINVAL; in dbg_chk_lpt_sz()
1801 lpt_sz = (long long)c->pnode_cnt * c->pnode_sz; in dbg_chk_lpt_sz()
1802 lpt_sz += (long long)c->nnode_cnt * c->nnode_sz; in dbg_chk_lpt_sz()
1803 lpt_sz += c->ltab_sz; in dbg_chk_lpt_sz()
1804 if (c->big_lpt) in dbg_chk_lpt_sz()
1805 lpt_sz += c->lsave_sz; in dbg_chk_lpt_sz()
1806 if (d->chk_lpt_sz - d->chk_lpt_wastage > lpt_sz) { in dbg_chk_lpt_sz()
1807 ubifs_err(c, "LPT chk_lpt_sz %lld + waste %lld exceeds %lld", in dbg_chk_lpt_sz()
1808 d->chk_lpt_sz, d->chk_lpt_wastage, lpt_sz); in dbg_chk_lpt_sz()
1809 err = -EINVAL; in dbg_chk_lpt_sz()
1812 ubifs_dump_lpt_info(c); in dbg_chk_lpt_sz()
1813 ubifs_dump_lpt_lebs(c); in dbg_chk_lpt_sz()
1816 d->chk_lpt_sz2 = d->chk_lpt_sz; in dbg_chk_lpt_sz()
1817 d->chk_lpt_sz = 0; in dbg_chk_lpt_sz()
1818 d->chk_lpt_wastage = 0; in dbg_chk_lpt_sz()
1819 d->chk_lpt_lebs = 0; in dbg_chk_lpt_sz()
1820 d->new_nhead_offs = len; in dbg_chk_lpt_sz()
1823 d->chk_lpt_sz += len; in dbg_chk_lpt_sz()
1824 d->chk_lpt_wastage += len; in dbg_chk_lpt_sz()
1827 return -EINVAL; in dbg_chk_lpt_sz()
1832 * dump_lpt_leb - dump an LPT LEB.
1833 * @c: UBIFS file-system description object
1838 * they do not have 8-byte alignments, etc), so we have a separate function to
1841 static void dump_lpt_leb(const struct ubifs_info *c, int lnum) in dump_lpt_leb() argument
1843 int err, len = c->leb_size, node_type, node_num, node_len, offs; in dump_lpt_leb()
1846 pr_err("(pid %d) start dumping LEB %d\n", current->pid, lnum); in dump_lpt_leb()
1847 buf = p = __vmalloc(c->leb_size, GFP_NOFS); in dump_lpt_leb()
1849 ubifs_err(c, "cannot allocate memory to dump LPT"); in dump_lpt_leb()
1853 err = ubifs_leb_read(c, lnum, buf, 0, c->leb_size, 1); in dump_lpt_leb()
1858 offs = c->leb_size - len; in dump_lpt_leb()
1859 if (!is_a_node(c, p, len)) { in dump_lpt_leb()
1862 pad_len = get_pad_len(c, p, len); in dump_lpt_leb()
1867 len -= pad_len; in dump_lpt_leb()
1876 node_type = get_lpt_node_type(c, p, &node_num); in dump_lpt_leb()
1880 node_len = c->pnode_sz; in dump_lpt_leb()
1881 if (c->big_lpt) in dump_lpt_leb()
1893 node_len = c->nnode_sz; in dump_lpt_leb()
1894 if (c->big_lpt) in dump_lpt_leb()
1900 err = ubifs_unpack_nnode(c, p, &nnode); in dump_lpt_leb()
1909 if (i != UBIFS_LPT_FANOUT - 1) in dump_lpt_leb()
1916 node_len = c->ltab_sz; in dump_lpt_leb()
1920 node_len = c->lsave_sz; in dump_lpt_leb()
1924 ubifs_err(c, "LPT node type %d not recognized", node_type); in dump_lpt_leb()
1929 len -= node_len; in dump_lpt_leb()
1932 pr_err("(pid %d) finish dumping LEB %d\n", current->pid, lnum); in dump_lpt_leb()
1938 * ubifs_dump_lpt_lebs - dump LPT lebs.
1939 * @c: UBIFS file-system description object
1944 void ubifs_dump_lpt_lebs(const struct ubifs_info *c) in ubifs_dump_lpt_lebs() argument
1948 pr_err("(pid %d) start dumping all LPT LEBs\n", current->pid); in ubifs_dump_lpt_lebs()
1949 for (i = 0; i < c->lpt_lebs; i++) in ubifs_dump_lpt_lebs()
1950 dump_lpt_leb(c, i + c->lpt_first); in ubifs_dump_lpt_lebs()
1951 pr_err("(pid %d) finish dumping all LPT LEBs\n", current->pid); in ubifs_dump_lpt_lebs()
1955 * dbg_populate_lsave - debugging version of 'populate_lsave()'
1956 * @c: UBIFS file-system description object
1961 * disabled) an non-zero if lsave has been populated.
1963 static int dbg_populate_lsave(struct ubifs_info *c) in dbg_populate_lsave() argument
1969 if (!dbg_is_chk_gen(c)) in dbg_populate_lsave()
1974 for (i = 0; i < c->lsave_cnt; i++) in dbg_populate_lsave()
1975 c->lsave[i] = c->main_first; in dbg_populate_lsave()
1977 list_for_each_entry(lprops, &c->empty_list, list) in dbg_populate_lsave()
1978 c->lsave[get_random_u32_below(c->lsave_cnt)] = lprops->lnum; in dbg_populate_lsave()
1979 list_for_each_entry(lprops, &c->freeable_list, list) in dbg_populate_lsave()
1980 c->lsave[get_random_u32_below(c->lsave_cnt)] = lprops->lnum; in dbg_populate_lsave()
1981 list_for_each_entry(lprops, &c->frdi_idx_list, list) in dbg_populate_lsave()
1982 c->lsave[get_random_u32_below(c->lsave_cnt)] = lprops->lnum; in dbg_populate_lsave()
1984 heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1]; in dbg_populate_lsave()
1985 for (i = 0; i < heap->cnt; i++) in dbg_populate_lsave()
1986 c->lsave[get_random_u32_below(c->lsave_cnt)] = heap->arr[i]->lnum; in dbg_populate_lsave()
1987 heap = &c->lpt_heap[LPROPS_DIRTY - 1]; in dbg_populate_lsave()
1988 for (i = 0; i < heap->cnt; i++) in dbg_populate_lsave()
1989 c->lsave[get_random_u32_below(c->lsave_cnt)] = heap->arr[i]->lnum; in dbg_populate_lsave()
1990 heap = &c->lpt_heap[LPROPS_FREE - 1]; in dbg_populate_lsave()
1991 for (i = 0; i < heap->cnt; i++) in dbg_populate_lsave()
1992 c->lsave[get_random_u32_below(c->lsave_cnt)] = heap->arr[i]->lnum; in dbg_populate_lsave()