Lines Matching refs:c
33 static int dbg_populate_lsave(struct ubifs_info *c);
42 static struct ubifs_cnode *first_dirty_cnode(const struct ubifs_info *c, struct ubifs_nnode *nnode) in first_dirty_cnode() argument
44 ubifs_assert(c, nnode); in first_dirty_cnode()
73 static struct ubifs_cnode *next_dirty_cnode(const struct ubifs_info *c, struct ubifs_cnode *cnode) in next_dirty_cnode() argument
78 ubifs_assert(c, cnode); in next_dirty_cnode()
88 return first_dirty_cnode(c, (struct ubifs_nnode *)cnode); in next_dirty_cnode()
100 static int get_cnodes_to_commit(struct ubifs_info *c) in get_cnodes_to_commit() argument
105 if (!c->nroot) in get_cnodes_to_commit()
108 if (!test_bit(DIRTY_CNODE, &c->nroot->flags)) in get_cnodes_to_commit()
111 c->lpt_cnext = first_dirty_cnode(c, c->nroot); in get_cnodes_to_commit()
112 cnode = c->lpt_cnext; in get_cnodes_to_commit()
117 ubifs_assert(c, !test_bit(COW_CNODE, &cnode->flags)); in get_cnodes_to_commit()
119 cnext = next_dirty_cnode(c, cnode); in get_cnodes_to_commit()
121 cnode->cnext = c->lpt_cnext; in get_cnodes_to_commit()
130 ubifs_assert(c, cnt == c->dirty_nn_cnt + c->dirty_pn_cnt); in get_cnodes_to_commit()
141 static void upd_ltab(struct ubifs_info *c, int lnum, int free, int dirty) in upd_ltab() argument
144 lnum, c->ltab[lnum - c->lpt_first].free, in upd_ltab()
145 c->ltab[lnum - c->lpt_first].dirty, free, dirty); in upd_ltab()
146 ubifs_assert(c, lnum >= c->lpt_first && lnum <= c->lpt_last); in upd_ltab()
147 c->ltab[lnum - c->lpt_first].free = free; in upd_ltab()
148 c->ltab[lnum - c->lpt_first].dirty += dirty; in upd_ltab()
161 static int alloc_lpt_leb(struct ubifs_info *c, int *lnum) in alloc_lpt_leb() argument
165 n = *lnum - c->lpt_first + 1; in alloc_lpt_leb()
166 for (i = n; i < c->lpt_lebs; i++) { in alloc_lpt_leb()
167 if (c->ltab[i].tgc || c->ltab[i].cmt) in alloc_lpt_leb()
169 if (c->ltab[i].free == c->leb_size) { in alloc_lpt_leb()
170 c->ltab[i].cmt = 1; in alloc_lpt_leb()
171 *lnum = i + c->lpt_first; in alloc_lpt_leb()
177 if (c->ltab[i].tgc || c->ltab[i].cmt) in alloc_lpt_leb()
179 if (c->ltab[i].free == c->leb_size) { in alloc_lpt_leb()
180 c->ltab[i].cmt = 1; in alloc_lpt_leb()
181 *lnum = i + c->lpt_first; in alloc_lpt_leb()
194 static int layout_cnodes(struct ubifs_info *c) in layout_cnodes() argument
199 err = dbg_chk_lpt_sz(c, 0, 0); in layout_cnodes()
202 cnode = c->lpt_cnext; in layout_cnodes()
205 lnum = c->nhead_lnum; in layout_cnodes()
206 offs = c->nhead_offs; in layout_cnodes()
208 done_lsave = !c->big_lpt; in layout_cnodes()
210 if (!done_lsave && offs + c->lsave_sz <= c->leb_size) { in layout_cnodes()
212 c->lsave_lnum = lnum; in layout_cnodes()
213 c->lsave_offs = offs; in layout_cnodes()
214 offs += c->lsave_sz; in layout_cnodes()
215 dbg_chk_lpt_sz(c, 1, c->lsave_sz); in layout_cnodes()
218 if (offs + c->ltab_sz <= c->leb_size) { in layout_cnodes()
220 c->ltab_lnum = lnum; in layout_cnodes()
221 c->ltab_offs = offs; in layout_cnodes()
222 offs += c->ltab_sz; in layout_cnodes()
223 dbg_chk_lpt_sz(c, 1, c->ltab_sz); in layout_cnodes()
228 len = c->nnode_sz; in layout_cnodes()
229 c->dirty_nn_cnt -= 1; in layout_cnodes()
231 len = c->pnode_sz; in layout_cnodes()
232 c->dirty_pn_cnt -= 1; in layout_cnodes()
234 while (offs + len > c->leb_size) { in layout_cnodes()
235 alen = ALIGN(offs, c->min_io_size); in layout_cnodes()
236 upd_ltab(c, lnum, c->leb_size - alen, alen - offs); in layout_cnodes()
237 dbg_chk_lpt_sz(c, 2, c->leb_size - offs); in layout_cnodes()
238 err = alloc_lpt_leb(c, &lnum); in layout_cnodes()
242 ubifs_assert(c, lnum >= c->lpt_first && in layout_cnodes()
243 lnum <= c->lpt_last); in layout_cnodes()
247 c->lsave_lnum = lnum; in layout_cnodes()
248 c->lsave_offs = offs; in layout_cnodes()
249 offs += c->lsave_sz; in layout_cnodes()
250 dbg_chk_lpt_sz(c, 1, c->lsave_sz); in layout_cnodes()
255 c->ltab_lnum = lnum; in layout_cnodes()
256 c->ltab_offs = offs; in layout_cnodes()
257 offs += c->ltab_sz; in layout_cnodes()
258 dbg_chk_lpt_sz(c, 1, c->ltab_sz); in layout_cnodes()
267 c->lpt_lnum = lnum; in layout_cnodes()
268 c->lpt_offs = offs; in layout_cnodes()
271 dbg_chk_lpt_sz(c, 1, len); in layout_cnodes()
273 } while (cnode && cnode != c->lpt_cnext); in layout_cnodes()
277 if (offs + c->lsave_sz > c->leb_size) { in layout_cnodes()
278 alen = ALIGN(offs, c->min_io_size); in layout_cnodes()
279 upd_ltab(c, lnum, c->leb_size - alen, alen - offs); in layout_cnodes()
280 dbg_chk_lpt_sz(c, 2, c->leb_size - offs); in layout_cnodes()
281 err = alloc_lpt_leb(c, &lnum); in layout_cnodes()
285 ubifs_assert(c, lnum >= c->lpt_first && in layout_cnodes()
286 lnum <= c->lpt_last); in layout_cnodes()
289 c->lsave_lnum = lnum; in layout_cnodes()
290 c->lsave_offs = offs; in layout_cnodes()
291 offs += c->lsave_sz; in layout_cnodes()
292 dbg_chk_lpt_sz(c, 1, c->lsave_sz); in layout_cnodes()
297 if (offs + c->ltab_sz > c->leb_size) { in layout_cnodes()
298 alen = ALIGN(offs, c->min_io_size); in layout_cnodes()
299 upd_ltab(c, lnum, c->leb_size - alen, alen - offs); in layout_cnodes()
300 dbg_chk_lpt_sz(c, 2, c->leb_size - offs); in layout_cnodes()
301 err = alloc_lpt_leb(c, &lnum); in layout_cnodes()
305 ubifs_assert(c, lnum >= c->lpt_first && in layout_cnodes()
306 lnum <= c->lpt_last); in layout_cnodes()
308 c->ltab_lnum = lnum; in layout_cnodes()
309 c->ltab_offs = offs; in layout_cnodes()
310 offs += c->ltab_sz; in layout_cnodes()
311 dbg_chk_lpt_sz(c, 1, c->ltab_sz); in layout_cnodes()
314 alen = ALIGN(offs, c->min_io_size); in layout_cnodes()
315 upd_ltab(c, lnum, c->leb_size - alen, alen - offs); in layout_cnodes()
316 dbg_chk_lpt_sz(c, 4, alen - offs); in layout_cnodes()
317 err = dbg_chk_lpt_sz(c, 3, alen); in layout_cnodes()
323 ubifs_err(c, "LPT out of space at LEB %d:%d needing %d, done_ltab %d, done_lsave %d", in layout_cnodes()
325 ubifs_dump_lpt_info(c); in layout_cnodes()
326 ubifs_dump_lpt_lebs(c); in layout_cnodes()
345 static int realloc_lpt_leb(struct ubifs_info *c, int *lnum) in realloc_lpt_leb() argument
349 n = *lnum - c->lpt_first + 1; in realloc_lpt_leb()
350 for (i = n; i < c->lpt_lebs; i++) in realloc_lpt_leb()
351 if (c->ltab[i].cmt) { in realloc_lpt_leb()
352 c->ltab[i].cmt = 0; in realloc_lpt_leb()
353 *lnum = i + c->lpt_first; in realloc_lpt_leb()
358 if (c->ltab[i].cmt) { in realloc_lpt_leb()
359 c->ltab[i].cmt = 0; in realloc_lpt_leb()
360 *lnum = i + c->lpt_first; in realloc_lpt_leb()
372 static int write_cnodes(struct ubifs_info *c) in write_cnodes() argument
376 void *buf = c->lpt_buf; in write_cnodes()
378 cnode = c->lpt_cnext; in write_cnodes()
381 lnum = c->nhead_lnum; in write_cnodes()
382 offs = c->nhead_offs; in write_cnodes()
386 err = ubifs_leb_unmap(c, lnum); in write_cnodes()
391 done_lsave = !c->big_lpt; in write_cnodes()
393 if (!done_lsave && offs + c->lsave_sz <= c->leb_size) { in write_cnodes()
395 ubifs_pack_lsave(c, buf + offs, c->lsave); in write_cnodes()
396 offs += c->lsave_sz; in write_cnodes()
397 dbg_chk_lpt_sz(c, 1, c->lsave_sz); in write_cnodes()
400 if (offs + c->ltab_sz <= c->leb_size) { in write_cnodes()
402 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt); in write_cnodes()
403 offs += c->ltab_sz; in write_cnodes()
404 dbg_chk_lpt_sz(c, 1, c->ltab_sz); in write_cnodes()
410 len = c->nnode_sz; in write_cnodes()
412 len = c->pnode_sz; in write_cnodes()
413 while (offs + len > c->leb_size) { in write_cnodes()
416 alen = ALIGN(wlen, c->min_io_size); in write_cnodes()
418 err = ubifs_leb_write(c, lnum, buf + from, from, in write_cnodes()
423 dbg_chk_lpt_sz(c, 2, c->leb_size - offs); in write_cnodes()
424 err = realloc_lpt_leb(c, &lnum); in write_cnodes()
428 ubifs_assert(c, lnum >= c->lpt_first && in write_cnodes()
429 lnum <= c->lpt_last); in write_cnodes()
430 err = ubifs_leb_unmap(c, lnum); in write_cnodes()
436 ubifs_pack_lsave(c, buf + offs, c->lsave); in write_cnodes()
437 offs += c->lsave_sz; in write_cnodes()
438 dbg_chk_lpt_sz(c, 1, c->lsave_sz); in write_cnodes()
443 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt); in write_cnodes()
444 offs += c->ltab_sz; in write_cnodes()
445 dbg_chk_lpt_sz(c, 1, c->ltab_sz); in write_cnodes()
451 ubifs_pack_nnode(c, buf + offs, in write_cnodes()
454 ubifs_pack_pnode(c, buf + offs, in write_cnodes()
467 dbg_chk_lpt_sz(c, 1, len); in write_cnodes()
469 } while (cnode && cnode != c->lpt_cnext); in write_cnodes()
473 if (offs + c->lsave_sz > c->leb_size) { in write_cnodes()
475 alen = ALIGN(wlen, c->min_io_size); in write_cnodes()
477 err = ubifs_leb_write(c, lnum, buf + from, from, alen); in write_cnodes()
480 dbg_chk_lpt_sz(c, 2, c->leb_size - offs); in write_cnodes()
481 err = realloc_lpt_leb(c, &lnum); in write_cnodes()
485 ubifs_assert(c, lnum >= c->lpt_first && in write_cnodes()
486 lnum <= c->lpt_last); in write_cnodes()
487 err = ubifs_leb_unmap(c, lnum); in write_cnodes()
492 ubifs_pack_lsave(c, buf + offs, c->lsave); in write_cnodes()
493 offs += c->lsave_sz; in write_cnodes()
494 dbg_chk_lpt_sz(c, 1, c->lsave_sz); in write_cnodes()
499 if (offs + c->ltab_sz > c->leb_size) { in write_cnodes()
501 alen = ALIGN(wlen, c->min_io_size); in write_cnodes()
503 err = ubifs_leb_write(c, lnum, buf + from, from, alen); in write_cnodes()
506 dbg_chk_lpt_sz(c, 2, c->leb_size - offs); in write_cnodes()
507 err = realloc_lpt_leb(c, &lnum); in write_cnodes()
511 ubifs_assert(c, lnum >= c->lpt_first && in write_cnodes()
512 lnum <= c->lpt_last); in write_cnodes()
513 err = ubifs_leb_unmap(c, lnum); in write_cnodes()
517 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt); in write_cnodes()
518 offs += c->ltab_sz; in write_cnodes()
519 dbg_chk_lpt_sz(c, 1, c->ltab_sz); in write_cnodes()
524 alen = ALIGN(wlen, c->min_io_size); in write_cnodes()
526 err = ubifs_leb_write(c, lnum, buf + from, from, alen); in write_cnodes()
530 dbg_chk_lpt_sz(c, 4, alen - wlen); in write_cnodes()
531 err = dbg_chk_lpt_sz(c, 3, ALIGN(offs, c->min_io_size)); in write_cnodes()
535 c->nhead_lnum = lnum; in write_cnodes()
536 c->nhead_offs = ALIGN(offs, c->min_io_size); in write_cnodes()
538 dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs); in write_cnodes()
539 dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs); in write_cnodes()
540 dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs); in write_cnodes()
541 if (c->big_lpt) in write_cnodes()
542 dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs); in write_cnodes()
547 ubifs_err(c, "LPT out of space mismatch at LEB %d:%d needing %d, done_ltab %d, done_lsave %d", in write_cnodes()
549 ubifs_dump_lpt_info(c); in write_cnodes()
550 ubifs_dump_lpt_lebs(c); in write_cnodes()
564 static struct ubifs_pnode *next_pnode_to_dirty(struct ubifs_info *c, in next_pnode_to_dirty() argument
574 return ubifs_get_pnode(c, nnode, iip); in next_pnode_to_dirty()
590 nnode = ubifs_get_nnode(c, nnode, iip); in next_pnode_to_dirty()
607 nnode = ubifs_get_nnode(c, nnode, iip); in next_pnode_to_dirty()
618 return ubifs_get_pnode(c, nnode, iip); in next_pnode_to_dirty()
629 static struct ubifs_pnode *pnode_lookup(struct ubifs_info *c, int i) in pnode_lookup() argument
634 if (!c->nroot) { in pnode_lookup()
635 err = ubifs_read_nnode(c, NULL, 0); in pnode_lookup()
640 nnode = c->nroot; in pnode_lookup()
641 shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT; in pnode_lookup()
642 for (h = 1; h < c->lpt_hght; h++) { in pnode_lookup()
645 nnode = ubifs_get_nnode(c, nnode, iip); in pnode_lookup()
650 return ubifs_get_pnode(c, nnode, iip); in pnode_lookup()
658 static void add_pnode_dirt(struct ubifs_info *c, struct ubifs_pnode *pnode) in add_pnode_dirt() argument
660 ubifs_add_lpt_dirt(c, pnode->parent->nbranch[pnode->iip].lnum, in add_pnode_dirt()
661 c->pnode_sz); in add_pnode_dirt()
669 static void do_make_pnode_dirty(struct ubifs_info *c, struct ubifs_pnode *pnode) in do_make_pnode_dirty() argument
675 c->dirty_pn_cnt += 1; in do_make_pnode_dirty()
676 add_pnode_dirt(c, pnode); in do_make_pnode_dirty()
681 c->dirty_nn_cnt += 1; in do_make_pnode_dirty()
682 ubifs_add_nnode_dirt(c, nnode); in do_make_pnode_dirty()
701 static int make_tree_dirty(struct ubifs_info *c) in make_tree_dirty() argument
705 pnode = pnode_lookup(c, 0); in make_tree_dirty()
710 do_make_pnode_dirty(c, pnode); in make_tree_dirty()
711 pnode = next_pnode_to_dirty(c, pnode); in make_tree_dirty()
725 static int need_write_all(struct ubifs_info *c) in need_write_all() argument
730 for (i = 0; i < c->lpt_lebs; i++) { in need_write_all()
731 if (i + c->lpt_first == c->nhead_lnum) in need_write_all()
732 free += c->leb_size - c->nhead_offs; in need_write_all()
733 else if (c->ltab[i].free == c->leb_size) in need_write_all()
734 free += c->leb_size; in need_write_all()
735 else if (c->ltab[i].free + c->ltab[i].dirty == c->leb_size) in need_write_all()
736 free += c->leb_size; in need_write_all()
739 if (free <= c->lpt_sz * 2) in need_write_all()
752 static void lpt_tgc_start(struct ubifs_info *c) in lpt_tgc_start() argument
756 for (i = 0; i < c->lpt_lebs; i++) { in lpt_tgc_start()
757 if (i + c->lpt_first == c->nhead_lnum) in lpt_tgc_start()
759 if (c->ltab[i].dirty > 0 && in lpt_tgc_start()
760 c->ltab[i].free + c->ltab[i].dirty == c->leb_size) { in lpt_tgc_start()
761 c->ltab[i].tgc = 1; in lpt_tgc_start()
762 c->ltab[i].free = c->leb_size; in lpt_tgc_start()
763 c->ltab[i].dirty = 0; in lpt_tgc_start()
764 dbg_lp("LEB %d", i + c->lpt_first); in lpt_tgc_start()
778 static int lpt_tgc_end(struct ubifs_info *c) in lpt_tgc_end() argument
782 for (i = 0; i < c->lpt_lebs; i++) in lpt_tgc_end()
783 if (c->ltab[i].tgc) { in lpt_tgc_end()
784 err = ubifs_leb_unmap(c, i + c->lpt_first); in lpt_tgc_end()
787 c->ltab[i].tgc = 0; in lpt_tgc_end()
788 dbg_lp("LEB %d", i + c->lpt_first); in lpt_tgc_end()
805 static void populate_lsave(struct ubifs_info *c) in populate_lsave() argument
811 ubifs_assert(c, c->big_lpt); in populate_lsave()
812 if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) { in populate_lsave()
813 c->lpt_drty_flgs |= LSAVE_DIRTY; in populate_lsave()
814 ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz); in populate_lsave()
817 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()
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()
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()
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()
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()
882 nnode = ubifs_get_nnode(c, nnode, iip); in nnode_lookup()
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()
918 } else if (c->lpt_lnum != lnum || c->lpt_offs != offs) 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()
928 c->dirty_nn_cnt += 1; in make_nnode_dirty()
929 ubifs_add_nnode_dirt(c, nnode); in make_nnode_dirty()
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()
965 do_make_pnode_dirty(c, pnode); in make_pnode_dirty()
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()
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()
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()
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()
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()
1094 static int get_lpt_node_type(const struct ubifs_info *c, uint8_t *buf, in get_lpt_node_type() argument
1100 node_type = ubifs_unpack_bits(c, &addr, &pos, UBIFS_LPT_TYPE_BITS); in get_lpt_node_type()
1101 *node_num = ubifs_unpack_bits(c, &addr, &pos, c->pcnt_bits); in get_lpt_node_type()
1113 static int is_a_node(const struct ubifs_info *c, uint8_t *buf, int len) in is_a_node() argument
1121 node_type = ubifs_unpack_bits(c, &addr, &pos, UBIFS_LPT_TYPE_BITS); in is_a_node()
1124 node_len = get_lpt_node_len(c, node_type); in is_a_node()
1129 crc = ubifs_unpack_bits(c, &addr, &pos, UBIFS_LPT_CRC_BITS); in is_a_node()
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()
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()
1175 ubifs_assert(c, node_len != 0); 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()
1194 static int lpt_gc(struct ubifs_info *c) in lpt_gc() argument
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, !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()
1212 return lpt_gc_lnum(c, lnum); in lpt_gc()
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()
1276 ubifs_assert(c, cnt != 0); 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()
1296 static void free_obsolete_cnodes(struct ubifs_info *c) in free_obsolete_cnodes() argument
1300 cnext = c->lpt_cnext; in free_obsolete_cnodes()
1310 } while (cnext != c->lpt_cnext); in free_obsolete_cnodes()
1311 c->lpt_cnext = NULL; in free_obsolete_cnodes()
1323 int ubifs_lpt_end_commit(struct ubifs_info *c) in ubifs_lpt_end_commit() argument
1329 if (!c->lpt_cnext) in ubifs_lpt_end_commit()
1332 err = write_cnodes(c); in ubifs_lpt_end_commit()
1336 mutex_lock(&c->lp_mutex); in ubifs_lpt_end_commit()
1337 free_obsolete_cnodes(c); in ubifs_lpt_end_commit()
1338 mutex_unlock(&c->lp_mutex); in ubifs_lpt_end_commit()
1350 int ubifs_lpt_post_commit(struct ubifs_info *c) in ubifs_lpt_post_commit() argument
1354 mutex_lock(&c->lp_mutex); in ubifs_lpt_post_commit()
1355 err = lpt_tgc_end(c); in ubifs_lpt_post_commit()
1358 if (c->big_lpt) in ubifs_lpt_post_commit()
1359 while (need_write_all(c)) { in ubifs_lpt_post_commit()
1360 mutex_unlock(&c->lp_mutex); in ubifs_lpt_post_commit()
1361 err = lpt_gc(c); in ubifs_lpt_post_commit()
1364 mutex_lock(&c->lp_mutex); in ubifs_lpt_post_commit()
1367 mutex_unlock(&c->lp_mutex); in ubifs_lpt_post_commit()
1379 static struct ubifs_nnode *first_nnode(struct ubifs_info *c, int *hght) in first_nnode() argument
1384 nnode = c->nroot; in first_nnode()
1388 for (h = 1; h < c->lpt_hght; h++) { in first_nnode()
1413 static struct ubifs_nnode *next_nnode(struct ubifs_info *c, in next_nnode() argument
1435 for (h = *hght + 1; h < c->lpt_hght; h++) { in next_nnode()
1456 void ubifs_lpt_free(struct ubifs_info *c, int wr_only) in ubifs_lpt_free() argument
1463 free_obsolete_cnodes(c); /* Leftover from a failed commit */ in ubifs_lpt_free()
1465 vfree(c->ltab_cmt); in ubifs_lpt_free()
1466 c->ltab_cmt = NULL; in ubifs_lpt_free()
1467 vfree(c->lpt_buf); in ubifs_lpt_free()
1468 c->lpt_buf = NULL; in ubifs_lpt_free()
1469 kfree(c->lsave); in ubifs_lpt_free()
1470 c->lsave = NULL; in ubifs_lpt_free()
1477 nnode = first_nnode(c, &hght); in ubifs_lpt_free()
1481 nnode = next_nnode(c, nnode, &hght); in ubifs_lpt_free()
1484 kfree(c->lpt_heap[i].arr); in ubifs_lpt_free()
1485 kfree(c->dirty_idx.arr); in ubifs_lpt_free()
1486 kfree(c->nroot); in ubifs_lpt_free()
1487 vfree(c->ltab); in ubifs_lpt_free()
1488 kfree(c->lpt_nod_buf); in ubifs_lpt_free()
1516 static int dbg_is_nnode_dirty(struct ubifs_info *c, int lnum, int offs) in dbg_is_nnode_dirty() argument
1522 nnode = first_nnode(c, &hght); in dbg_is_nnode_dirty()
1523 for (; nnode; nnode = next_nnode(c, nnode, &hght)) { in dbg_is_nnode_dirty()
1535 if (c->lpt_lnum != lnum || c->lpt_offs != offs) in dbg_is_nnode_dirty()
1551 static int dbg_is_pnode_dirty(struct ubifs_info *c, int lnum, int offs) in dbg_is_pnode_dirty() argument
1555 cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT); in dbg_is_pnode_dirty()
1561 pnode = pnode_lookup(c, i); in dbg_is_pnode_dirty()
1580 static int dbg_is_ltab_dirty(struct ubifs_info *c, int lnum, int offs) in dbg_is_ltab_dirty() argument
1582 if (lnum != c->ltab_lnum || offs != c->ltab_offs) in dbg_is_ltab_dirty()
1584 return (c->lpt_drty_flgs & LTAB_DIRTY) != 0; in dbg_is_ltab_dirty()
1593 static int dbg_is_lsave_dirty(struct ubifs_info *c, int lnum, int offs) in dbg_is_lsave_dirty() argument
1595 if (lnum != c->lsave_lnum || offs != c->lsave_offs) in dbg_is_lsave_dirty()
1597 return (c->lpt_drty_flgs & LSAVE_DIRTY) != 0; in dbg_is_lsave_dirty()
1607 static int dbg_is_node_dirty(struct ubifs_info *c, int node_type, int lnum, in dbg_is_node_dirty() argument
1612 return dbg_is_nnode_dirty(c, lnum, offs); in dbg_is_node_dirty()
1614 return dbg_is_pnode_dirty(c, lnum, offs); in dbg_is_node_dirty()
1616 return dbg_is_ltab_dirty(c, lnum, offs); in dbg_is_node_dirty()
1618 return dbg_is_lsave_dirty(c, lnum, offs); in dbg_is_node_dirty()
1630 static int dbg_check_ltab_lnum(struct ubifs_info *c, int lnum) in dbg_check_ltab_lnum() argument
1632 int err, len = c->leb_size, dirty = 0, node_type, node_num, node_len; in dbg_check_ltab_lnum()
1636 if (!dbg_is_chk_lprops(c)) in dbg_check_ltab_lnum()
1639 buf = p = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL); in dbg_check_ltab_lnum()
1641 ubifs_err(c, "cannot allocate memory for ltab checking"); in dbg_check_ltab_lnum()
1647 err = ubifs_leb_read(c, lnum, buf, 0, c->leb_size, 1); in dbg_check_ltab_lnum()
1652 if (!is_a_node(c, p, len)) { in dbg_check_ltab_lnum()
1655 pad_len = get_pad_len(c, p, len); in dbg_check_ltab_lnum()
1663 ubifs_err(c, "invalid empty space in LEB %d at %d", in dbg_check_ltab_lnum()
1664 lnum, c->leb_size - len); in dbg_check_ltab_lnum()
1667 i = lnum - c->lpt_first; in dbg_check_ltab_lnum()
1668 if (len != c->ltab[i].free) { in dbg_check_ltab_lnum()
1669 ubifs_err(c, "invalid free space in LEB %d (free %d, expected %d)", in dbg_check_ltab_lnum()
1670 lnum, len, c->ltab[i].free); in dbg_check_ltab_lnum()
1673 if (dirty != c->ltab[i].dirty) { in dbg_check_ltab_lnum()
1674 ubifs_err(c, "invalid dirty space in LEB %d (dirty %d, expected %d)", in dbg_check_ltab_lnum()
1675 lnum, dirty, c->ltab[i].dirty); in dbg_check_ltab_lnum()
1680 node_type = get_lpt_node_type(c, p, &node_num); in dbg_check_ltab_lnum()
1681 node_len = get_lpt_node_len(c, node_type); in dbg_check_ltab_lnum()
1682 ret = dbg_is_node_dirty(c, node_type, lnum, c->leb_size - len); in dbg_check_ltab_lnum()
1701 int dbg_check_ltab(struct ubifs_info *c) in dbg_check_ltab() argument
1705 if (!dbg_is_chk_lprops(c)) in dbg_check_ltab()
1709 cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT); in dbg_check_ltab()
1713 pnode = pnode_lookup(c, i); in dbg_check_ltab()
1720 err = dbg_check_lpt_nodes(c, (struct ubifs_cnode *)c->nroot, 0, 0); in dbg_check_ltab()
1725 for (lnum = c->lpt_first; lnum <= c->lpt_last; lnum++) { in dbg_check_ltab()
1726 err = dbg_check_ltab_lnum(c, lnum); in dbg_check_ltab()
1728 ubifs_err(c, "failed at LEB %d", lnum); in dbg_check_ltab()
1743 int dbg_chk_lpt_free_spc(struct ubifs_info *c) in dbg_chk_lpt_free_spc() argument
1748 if (!dbg_is_chk_lprops(c)) in dbg_chk_lpt_free_spc()
1751 for (i = 0; i < c->lpt_lebs; i++) { in dbg_chk_lpt_free_spc()
1752 if (c->ltab[i].tgc || c->ltab[i].cmt) in dbg_chk_lpt_free_spc()
1754 if (i + c->lpt_first == c->nhead_lnum) in dbg_chk_lpt_free_spc()
1755 free += c->leb_size - c->nhead_offs; in dbg_chk_lpt_free_spc()
1756 else if (c->ltab[i].free == c->leb_size) in dbg_chk_lpt_free_spc()
1757 free += c->leb_size; in dbg_chk_lpt_free_spc()
1759 if (free < c->lpt_sz) { in dbg_chk_lpt_free_spc()
1760 ubifs_err(c, "LPT space error: free %lld lpt_sz %lld", in dbg_chk_lpt_free_spc()
1761 free, c->lpt_sz); in dbg_chk_lpt_free_spc()
1762 ubifs_dump_lpt_info(c); in dbg_chk_lpt_free_spc()
1763 ubifs_dump_lpt_lebs(c); in dbg_chk_lpt_free_spc()
1784 int dbg_chk_lpt_sz(struct ubifs_info *c, int action, int len) in dbg_chk_lpt_sz() argument
1786 struct ubifs_debug_info *d = c->dbg; in dbg_chk_lpt_sz()
1790 if (!dbg_is_chk_lprops(c)) in dbg_chk_lpt_sz()
1799 if (c->dirty_pn_cnt > c->pnode_cnt) { in dbg_chk_lpt_sz()
1800 ubifs_err(c, "dirty pnodes %d exceed max %d", in dbg_chk_lpt_sz()
1801 c->dirty_pn_cnt, c->pnode_cnt); in dbg_chk_lpt_sz()
1804 if (c->dirty_nn_cnt > c->nnode_cnt) { in dbg_chk_lpt_sz()
1805 ubifs_err(c, "dirty nnodes %d exceed max %d", in dbg_chk_lpt_sz()
1806 c->dirty_nn_cnt, c->nnode_cnt); in dbg_chk_lpt_sz()
1819 chk_lpt_sz = c->leb_size; in dbg_chk_lpt_sz()
1821 chk_lpt_sz += len - c->nhead_offs; in dbg_chk_lpt_sz()
1823 ubifs_err(c, "LPT wrote %lld but space used was %lld", in dbg_chk_lpt_sz()
1827 if (d->chk_lpt_sz > c->lpt_sz) { in dbg_chk_lpt_sz()
1828 ubifs_err(c, "LPT wrote %lld but lpt_sz is %lld", in dbg_chk_lpt_sz()
1829 d->chk_lpt_sz, c->lpt_sz); in dbg_chk_lpt_sz()
1833 ubifs_err(c, "LPT layout size %lld but wrote %lld", in dbg_chk_lpt_sz()
1838 ubifs_err(c, "LPT new nhead offs: expected %d was %d", in dbg_chk_lpt_sz()
1842 lpt_sz = (long long)c->pnode_cnt * c->pnode_sz; in dbg_chk_lpt_sz()
1843 lpt_sz += (long long)c->nnode_cnt * c->nnode_sz; in dbg_chk_lpt_sz()
1844 lpt_sz += c->ltab_sz; in dbg_chk_lpt_sz()
1845 if (c->big_lpt) in dbg_chk_lpt_sz()
1846 lpt_sz += c->lsave_sz; in dbg_chk_lpt_sz()
1848 ubifs_err(c, "LPT chk_lpt_sz %lld + waste %lld exceeds %lld", in dbg_chk_lpt_sz()
1853 ubifs_dump_lpt_info(c); in dbg_chk_lpt_sz()
1854 ubifs_dump_lpt_lebs(c); in dbg_chk_lpt_sz()
1882 static void dump_lpt_leb(const struct ubifs_info *c, int lnum) in dump_lpt_leb() argument
1884 int err, len = c->leb_size, node_type, node_num, node_len, offs; in dump_lpt_leb()
1888 buf = p = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL); in dump_lpt_leb()
1890 ubifs_err(c, "cannot allocate memory to dump LPT"); in dump_lpt_leb()
1894 err = ubifs_leb_read(c, lnum, buf, 0, c->leb_size, 1); in dump_lpt_leb()
1899 offs = c->leb_size - len; in dump_lpt_leb()
1900 if (!is_a_node(c, p, len)) { in dump_lpt_leb()
1903 pad_len = get_pad_len(c, p, len); in dump_lpt_leb()
1917 node_type = get_lpt_node_type(c, p, &node_num); in dump_lpt_leb()
1921 node_len = c->pnode_sz; in dump_lpt_leb()
1922 if (c->big_lpt) in dump_lpt_leb()
1934 node_len = c->nnode_sz; in dump_lpt_leb()
1935 if (c->big_lpt) in dump_lpt_leb()
1941 err = ubifs_unpack_nnode(c, p, &nnode); in dump_lpt_leb()
1957 node_len = c->ltab_sz; in dump_lpt_leb()
1961 node_len = c->lsave_sz; in dump_lpt_leb()
1965 ubifs_err(c, "LPT node type %d not recognized", node_type); in dump_lpt_leb()
1986 void ubifs_dump_lpt_lebs(const struct ubifs_info *c) in ubifs_dump_lpt_lebs() argument
1991 for (i = 0; i < c->lpt_lebs; i++) in ubifs_dump_lpt_lebs()
1992 dump_lpt_leb(c, i + c->lpt_first); in ubifs_dump_lpt_lebs()
2005 static int dbg_populate_lsave(struct ubifs_info *c) in dbg_populate_lsave() argument
2011 if (!dbg_is_chk_gen(c)) in dbg_populate_lsave()
2016 for (i = 0; i < c->lsave_cnt; i++) in dbg_populate_lsave()
2017 c->lsave[i] = c->main_first; in dbg_populate_lsave()
2019 list_for_each_entry(lprops, &c->empty_list, list) in dbg_populate_lsave()
2020 c->lsave[prandom_u32() % c->lsave_cnt] = lprops->lnum; in dbg_populate_lsave()
2021 list_for_each_entry(lprops, &c->freeable_list, list) in dbg_populate_lsave()
2022 c->lsave[prandom_u32() % c->lsave_cnt] = lprops->lnum; in dbg_populate_lsave()
2023 list_for_each_entry(lprops, &c->frdi_idx_list, list) in dbg_populate_lsave()
2024 c->lsave[prandom_u32() % c->lsave_cnt] = lprops->lnum; in dbg_populate_lsave()
2026 heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1]; in dbg_populate_lsave()
2028 c->lsave[prandom_u32() % c->lsave_cnt] = heap->arr[i]->lnum; in dbg_populate_lsave()
2029 heap = &c->lpt_heap[LPROPS_DIRTY - 1]; in dbg_populate_lsave()
2031 c->lsave[prandom_u32() % c->lsave_cnt] = heap->arr[i]->lnum; in dbg_populate_lsave()
2032 heap = &c->lpt_heap[LPROPS_FREE - 1]; in dbg_populate_lsave()
2034 c->lsave[prandom_u32() % c->lsave_cnt] = heap->arr[i]->lnum; in dbg_populate_lsave()