Lines Matching refs:c
29 static int dbg_populate_lsave(struct ubifs_info *c);
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()
117 cnode->cnext = c->lpt_cnext; in get_cnodes_to_commit()
126 ubifs_assert(cnt == c->dirty_nn_cnt + c->dirty_pn_cnt); in get_cnodes_to_commit()
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()
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()
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()
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()
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()
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()
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()
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()
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()
413 alen = ALIGN(wlen, c->min_io_size); 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()
448 ubifs_pack_nnode(c, buf + offs, in write_cnodes()
451 ubifs_pack_pnode(c, buf + offs, in write_cnodes()
464 dbg_chk_lpt_sz(c, 1, len); 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()
472 alen = ALIGN(wlen, c->min_io_size); 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()
498 alen = ALIGN(wlen, c->min_io_size); 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()
521 alen = ALIGN(wlen, c->min_io_size); 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()
562 static struct ubifs_pnode *next_pnode_to_dirty(struct ubifs_info *c, in next_pnode_to_dirty() argument
572 return ubifs_get_pnode(c, nnode, iip); in next_pnode_to_dirty()
588 nnode = ubifs_get_nnode(c, nnode, iip); in next_pnode_to_dirty()
605 nnode = ubifs_get_nnode(c, nnode, iip); in next_pnode_to_dirty()
616 return ubifs_get_pnode(c, nnode, iip); in next_pnode_to_dirty()
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()
643 nnode = ubifs_get_nnode(c, nnode, iip); in pnode_lookup()
648 return ubifs_get_pnode(c, nnode, iip); in pnode_lookup()
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()
667 static void do_make_pnode_dirty(struct ubifs_info *c, struct ubifs_pnode *pnode) in do_make_pnode_dirty() argument
673 c->dirty_pn_cnt += 1; in do_make_pnode_dirty()
674 add_pnode_dirt(c, pnode); 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()
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()
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()
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()
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()
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()
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
1101 *node_num = ubifs_unpack_bits(&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
1124 node_len = get_lpt_node_len(c, node_type); 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()
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->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()
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()
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()
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()
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()
1415 static struct ubifs_nnode *next_nnode(struct ubifs_info *c, in next_nnode() argument
1437 for (h = *hght + 1; h < c->lpt_hght; h++) { in next_nnode()
1458 void ubifs_lpt_free(struct ubifs_info *c, int wr_only) in ubifs_lpt_free() argument
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()
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()
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()
1538 if (c->lpt_lnum != lnum || c->lpt_offs != offs) in dbg_is_nnode_dirty()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
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()
1823 chk_lpt_sz = c->leb_size; in dbg_chk_lpt_sz()
1825 chk_lpt_sz += len - c->nhead_offs; in dbg_chk_lpt_sz()
1827 ubifs_err(c, "LPT wrote %lld but space used was %lld", 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()
1837 ubifs_err(c, "LPT layout size %lld but wrote %lld", in dbg_chk_lpt_sz()
1842 ubifs_err(c, "LPT new nhead offs: expected %d was %d", 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()
1852 ubifs_err(c, "LPT chk_lpt_sz %lld + waste %lld exceeds %lld", 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()
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()
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()
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()
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()
1990 void ubifs_dump_lpt_lebs(const struct ubifs_info *c) in ubifs_dump_lpt_lebs() argument
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()
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()
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()
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()
2038 c->lsave[prandom_u32() % c->lsave_cnt] = heap->arr[i]->lnum; in dbg_populate_lsave()