Lines Matching full:c
5 * Copyright (C) 2006-2008 Nokia Corporation.
28 static int dbg_populate_lsave(struct ubifs_info *c);
33 * @c: UBIFS file-system description object
91 * @c: UBIFS file-system description object
95 static int get_cnodes_to_commit(struct ubifs_info *c) in get_cnodes_to_commit() argument
100 if (!c->nroot) in get_cnodes_to_commit()
103 if (!test_bit(DIRTY_CNODE, &c->nroot->flags)) in get_cnodes_to_commit()
106 c->lpt_cnext = first_dirty_cnode(c->nroot); in get_cnodes_to_commit()
107 cnode = c->lpt_cnext; in get_cnodes_to_commit()
116 cnode->cnext = c->lpt_cnext; in get_cnodes_to_commit()
125 ubifs_assert(cnt == c->dirty_nn_cnt + c->dirty_pn_cnt); in get_cnodes_to_commit()
131 * @c: UBIFS file-system description object
136 static void upd_ltab(struct ubifs_info *c, int lnum, int free, int dirty) in upd_ltab() argument
139 lnum, c->ltab[lnum - c->lpt_first].free, in upd_ltab()
140 c->ltab[lnum - c->lpt_first].dirty, free, dirty); in upd_ltab()
141 ubifs_assert(lnum >= c->lpt_first && lnum <= c->lpt_last); in upd_ltab()
142 c->ltab[lnum - c->lpt_first].free = free; in upd_ltab()
143 c->ltab[lnum - c->lpt_first].dirty += dirty; in upd_ltab()
148 * @c: UBIFS file-system description object
156 static int alloc_lpt_leb(struct ubifs_info *c, int *lnum) in alloc_lpt_leb() argument
160 n = *lnum - c->lpt_first + 1; in alloc_lpt_leb()
161 for (i = n; i < c->lpt_lebs; i++) { in alloc_lpt_leb()
162 if (c->ltab[i].tgc || c->ltab[i].cmt) in alloc_lpt_leb()
164 if (c->ltab[i].free == c->leb_size) { in alloc_lpt_leb()
165 c->ltab[i].cmt = 1; in alloc_lpt_leb()
166 *lnum = i + c->lpt_first; in alloc_lpt_leb()
172 if (c->ltab[i].tgc || c->ltab[i].cmt) in alloc_lpt_leb()
174 if (c->ltab[i].free == c->leb_size) { in alloc_lpt_leb()
175 c->ltab[i].cmt = 1; in alloc_lpt_leb()
176 *lnum = i + c->lpt_first; in alloc_lpt_leb()
185 * @c: UBIFS file-system description object
189 static int layout_cnodes(struct ubifs_info *c) in layout_cnodes() argument
194 err = dbg_chk_lpt_sz(c, 0, 0); in layout_cnodes()
197 cnode = c->lpt_cnext; in layout_cnodes()
200 lnum = c->nhead_lnum; in layout_cnodes()
201 offs = c->nhead_offs; in layout_cnodes()
203 done_lsave = !c->big_lpt; in layout_cnodes()
205 if (!done_lsave && offs + c->lsave_sz <= c->leb_size) { in layout_cnodes()
207 c->lsave_lnum = lnum; in layout_cnodes()
208 c->lsave_offs = offs; in layout_cnodes()
209 offs += c->lsave_sz; in layout_cnodes()
210 dbg_chk_lpt_sz(c, 1, c->lsave_sz); in layout_cnodes()
213 if (offs + c->ltab_sz <= c->leb_size) { in layout_cnodes()
215 c->ltab_lnum = lnum; in layout_cnodes()
216 c->ltab_offs = offs; in layout_cnodes()
217 offs += c->ltab_sz; in layout_cnodes()
218 dbg_chk_lpt_sz(c, 1, c->ltab_sz); in layout_cnodes()
223 len = c->nnode_sz; in layout_cnodes()
224 c->dirty_nn_cnt -= 1; in layout_cnodes()
226 len = c->pnode_sz; in layout_cnodes()
227 c->dirty_pn_cnt -= 1; in layout_cnodes()
229 while (offs + len > c->leb_size) { in layout_cnodes()
230 alen = ALIGN(offs, c->min_io_size); in layout_cnodes()
231 upd_ltab(c, lnum, c->leb_size - alen, alen - offs); in layout_cnodes()
232 dbg_chk_lpt_sz(c, 2, c->leb_size - offs); in layout_cnodes()
233 err = alloc_lpt_leb(c, &lnum); in layout_cnodes()
237 ubifs_assert(lnum >= c->lpt_first && in layout_cnodes()
238 lnum <= c->lpt_last); in layout_cnodes()
242 c->lsave_lnum = lnum; in layout_cnodes()
243 c->lsave_offs = offs; in layout_cnodes()
244 offs += c->lsave_sz; in layout_cnodes()
245 dbg_chk_lpt_sz(c, 1, c->lsave_sz); in layout_cnodes()
250 c->ltab_lnum = lnum; in layout_cnodes()
251 c->ltab_offs = offs; in layout_cnodes()
252 offs += c->ltab_sz; in layout_cnodes()
253 dbg_chk_lpt_sz(c, 1, c->ltab_sz); in layout_cnodes()
262 c->lpt_lnum = lnum; in layout_cnodes()
263 c->lpt_offs = offs; in layout_cnodes()
266 dbg_chk_lpt_sz(c, 1, len); in layout_cnodes()
268 } while (cnode && cnode != c->lpt_cnext); in layout_cnodes()
272 if (offs + c->lsave_sz > c->leb_size) { in layout_cnodes()
273 alen = ALIGN(offs, c->min_io_size); in layout_cnodes()
274 upd_ltab(c, lnum, c->leb_size - alen, alen - offs); in layout_cnodes()
275 dbg_chk_lpt_sz(c, 2, c->leb_size - offs); in layout_cnodes()
276 err = alloc_lpt_leb(c, &lnum); in layout_cnodes()
280 ubifs_assert(lnum >= c->lpt_first && in layout_cnodes()
281 lnum <= c->lpt_last); in layout_cnodes()
284 c->lsave_lnum = lnum; in layout_cnodes()
285 c->lsave_offs = offs; in layout_cnodes()
286 offs += c->lsave_sz; in layout_cnodes()
287 dbg_chk_lpt_sz(c, 1, c->lsave_sz); in layout_cnodes()
292 if (offs + c->ltab_sz > c->leb_size) { in layout_cnodes()
293 alen = ALIGN(offs, c->min_io_size); in layout_cnodes()
294 upd_ltab(c, lnum, c->leb_size - alen, alen - offs); in layout_cnodes()
295 dbg_chk_lpt_sz(c, 2, c->leb_size - offs); in layout_cnodes()
296 err = alloc_lpt_leb(c, &lnum); in layout_cnodes()
300 ubifs_assert(lnum >= c->lpt_first && in layout_cnodes()
301 lnum <= c->lpt_last); in layout_cnodes()
303 c->ltab_lnum = lnum; in layout_cnodes()
304 c->ltab_offs = offs; in layout_cnodes()
305 offs += c->ltab_sz; in layout_cnodes()
306 dbg_chk_lpt_sz(c, 1, c->ltab_sz); in layout_cnodes()
309 alen = ALIGN(offs, c->min_io_size); in layout_cnodes()
310 upd_ltab(c, lnum, c->leb_size - alen, alen - offs); in layout_cnodes()
311 dbg_chk_lpt_sz(c, 4, alen - offs); in layout_cnodes()
312 err = dbg_chk_lpt_sz(c, 3, alen); in layout_cnodes()
318 ubifs_err(c, "LPT out of space at LEB %d:%d needing %d, done_ltab %d, done_lsave %d", in layout_cnodes()
320 ubifs_dump_lpt_info(c); in layout_cnodes()
321 ubifs_dump_lpt_lebs(c); in layout_cnodes()
329 * @c: UBIFS file-system description object
341 static int realloc_lpt_leb(struct ubifs_info *c, int *lnum) in realloc_lpt_leb() argument
345 n = *lnum - c->lpt_first + 1; in realloc_lpt_leb()
346 for (i = n; i < c->lpt_lebs; i++) in realloc_lpt_leb()
347 if (c->ltab[i].cmt) { in realloc_lpt_leb()
348 c->ltab[i].cmt = 0; in realloc_lpt_leb()
349 *lnum = i + c->lpt_first; in realloc_lpt_leb()
354 if (c->ltab[i].cmt) { in realloc_lpt_leb()
355 c->ltab[i].cmt = 0; in realloc_lpt_leb()
356 *lnum = i + c->lpt_first; in realloc_lpt_leb()
364 * @c: UBIFS file-system description object
368 static int write_cnodes(struct ubifs_info *c) in write_cnodes() argument
372 void *buf = c->lpt_buf; in write_cnodes()
374 cnode = c->lpt_cnext; in write_cnodes()
377 lnum = c->nhead_lnum; in write_cnodes()
378 offs = c->nhead_offs; in write_cnodes()
382 err = ubifs_leb_unmap(c, lnum); in write_cnodes()
387 done_lsave = !c->big_lpt; in write_cnodes()
389 if (!done_lsave && offs + c->lsave_sz <= c->leb_size) { in write_cnodes()
391 ubifs_pack_lsave(c, buf + offs, c->lsave); in write_cnodes()
392 offs += c->lsave_sz; in write_cnodes()
393 dbg_chk_lpt_sz(c, 1, c->lsave_sz); in write_cnodes()
396 if (offs + c->ltab_sz <= c->leb_size) { in write_cnodes()
398 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt); in write_cnodes()
399 offs += c->ltab_sz; in write_cnodes()
400 dbg_chk_lpt_sz(c, 1, c->ltab_sz); in write_cnodes()
406 len = c->nnode_sz; in write_cnodes()
408 len = c->pnode_sz; in write_cnodes()
409 while (offs + len > c->leb_size) { in write_cnodes()
412 alen = ALIGN(wlen, c->min_io_size); in write_cnodes()
414 err = ubifs_leb_write(c, lnum, buf + from, from, in write_cnodes()
419 dbg_chk_lpt_sz(c, 2, c->leb_size - offs); in write_cnodes()
420 err = realloc_lpt_leb(c, &lnum); in write_cnodes()
424 ubifs_assert(lnum >= c->lpt_first && in write_cnodes()
425 lnum <= c->lpt_last); in write_cnodes()
426 err = ubifs_leb_unmap(c, lnum); in write_cnodes()
432 ubifs_pack_lsave(c, buf + offs, c->lsave); in write_cnodes()
433 offs += c->lsave_sz; in write_cnodes()
434 dbg_chk_lpt_sz(c, 1, c->lsave_sz); in write_cnodes()
439 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt); in write_cnodes()
440 offs += c->ltab_sz; in write_cnodes()
441 dbg_chk_lpt_sz(c, 1, c->ltab_sz); in write_cnodes()
447 ubifs_pack_nnode(c, buf + offs, in write_cnodes()
450 ubifs_pack_pnode(c, buf + offs, in write_cnodes()
463 dbg_chk_lpt_sz(c, 1, len); in write_cnodes()
465 } while (cnode && cnode != c->lpt_cnext); in write_cnodes()
469 if (offs + c->lsave_sz > c->leb_size) { in write_cnodes()
471 alen = ALIGN(wlen, c->min_io_size); in write_cnodes()
473 err = ubifs_leb_write(c, lnum, buf + from, from, alen); in write_cnodes()
476 dbg_chk_lpt_sz(c, 2, c->leb_size - offs); in write_cnodes()
477 err = realloc_lpt_leb(c, &lnum); in write_cnodes()
481 ubifs_assert(lnum >= c->lpt_first && in write_cnodes()
482 lnum <= c->lpt_last); in write_cnodes()
483 err = ubifs_leb_unmap(c, lnum); in write_cnodes()
488 ubifs_pack_lsave(c, buf + offs, c->lsave); in write_cnodes()
489 offs += c->lsave_sz; in write_cnodes()
490 dbg_chk_lpt_sz(c, 1, c->lsave_sz); in write_cnodes()
495 if (offs + c->ltab_sz > c->leb_size) { in write_cnodes()
497 alen = ALIGN(wlen, c->min_io_size); in write_cnodes()
499 err = ubifs_leb_write(c, lnum, buf + from, from, alen); in write_cnodes()
502 dbg_chk_lpt_sz(c, 2, c->leb_size - offs); in write_cnodes()
503 err = realloc_lpt_leb(c, &lnum); in write_cnodes()
507 ubifs_assert(lnum >= c->lpt_first && in write_cnodes()
508 lnum <= c->lpt_last); in write_cnodes()
509 err = ubifs_leb_unmap(c, lnum); in write_cnodes()
513 ubifs_pack_ltab(c, buf + offs, c->ltab_cmt); in write_cnodes()
514 offs += c->ltab_sz; in write_cnodes()
515 dbg_chk_lpt_sz(c, 1, c->ltab_sz); in write_cnodes()
520 alen = ALIGN(wlen, c->min_io_size); in write_cnodes()
522 err = ubifs_leb_write(c, lnum, buf + from, from, alen); in write_cnodes()
526 dbg_chk_lpt_sz(c, 4, alen - wlen); in write_cnodes()
527 err = dbg_chk_lpt_sz(c, 3, ALIGN(offs, c->min_io_size)); in write_cnodes()
531 c->nhead_lnum = lnum; in write_cnodes()
532 c->nhead_offs = ALIGN(offs, c->min_io_size); in write_cnodes()
534 dbg_lp("LPT root is at %d:%d", c->lpt_lnum, c->lpt_offs); in write_cnodes()
535 dbg_lp("LPT head is at %d:%d", c->nhead_lnum, c->nhead_offs); in write_cnodes()
536 dbg_lp("LPT ltab is at %d:%d", c->ltab_lnum, c->ltab_offs); in write_cnodes()
537 if (c->big_lpt) in write_cnodes()
538 dbg_lp("LPT lsave is at %d:%d", c->lsave_lnum, c->lsave_offs); in write_cnodes()
543 ubifs_err(c, "LPT out of space mismatch at LEB %d:%d needing %d, done_ltab %d, done_lsave %d", in write_cnodes()
545 ubifs_dump_lpt_info(c); in write_cnodes()
546 ubifs_dump_lpt_lebs(c); in write_cnodes()
554 * @c: UBIFS file-system description object
561 static struct ubifs_pnode *next_pnode_to_dirty(struct ubifs_info *c, in next_pnode_to_dirty() argument
571 return ubifs_get_pnode(c, nnode, iip); in next_pnode_to_dirty()
587 nnode = ubifs_get_nnode(c, nnode, iip); in next_pnode_to_dirty()
604 nnode = ubifs_get_nnode(c, nnode, iip); in next_pnode_to_dirty()
615 return ubifs_get_pnode(c, nnode, iip); in next_pnode_to_dirty()
620 * @c: UBIFS file-system description object
626 static struct ubifs_pnode *pnode_lookup(struct ubifs_info *c, int i) in pnode_lookup() argument
631 if (!c->nroot) { in pnode_lookup()
632 err = ubifs_read_nnode(c, NULL, 0); in pnode_lookup()
637 nnode = c->nroot; in pnode_lookup()
638 shft = c->lpt_hght * UBIFS_LPT_FANOUT_SHIFT; in pnode_lookup()
639 for (h = 1; h < c->lpt_hght; h++) { in pnode_lookup()
642 nnode = ubifs_get_nnode(c, nnode, iip); in pnode_lookup()
647 return ubifs_get_pnode(c, nnode, iip); in pnode_lookup()
652 * @c: UBIFS file-system description object
655 static void add_pnode_dirt(struct ubifs_info *c, struct ubifs_pnode *pnode) in add_pnode_dirt() argument
657 ubifs_add_lpt_dirt(c, pnode->parent->nbranch[pnode->iip].lnum, in add_pnode_dirt()
658 c->pnode_sz); in add_pnode_dirt()
663 * @c: UBIFS file-system description object
666 static void do_make_pnode_dirty(struct ubifs_info *c, struct ubifs_pnode *pnode) in do_make_pnode_dirty() argument
672 c->dirty_pn_cnt += 1; in do_make_pnode_dirty()
673 add_pnode_dirt(c, pnode); in do_make_pnode_dirty()
678 c->dirty_nn_cnt += 1; in do_make_pnode_dirty()
679 ubifs_add_nnode_dirt(c, nnode); in do_make_pnode_dirty()
689 * @c: UBIFS file-system description object
698 static int make_tree_dirty(struct ubifs_info *c) in make_tree_dirty() argument
702 pnode = pnode_lookup(c, 0); in make_tree_dirty()
707 do_make_pnode_dirty(c, pnode); in make_tree_dirty()
708 pnode = next_pnode_to_dirty(c, pnode); in make_tree_dirty()
717 * @c: UBIFS file-system description object
722 static int need_write_all(struct ubifs_info *c) in need_write_all() argument
727 for (i = 0; i < c->lpt_lebs; i++) { in need_write_all()
728 if (i + c->lpt_first == c->nhead_lnum) in need_write_all()
729 free += c->leb_size - c->nhead_offs; in need_write_all()
730 else if (c->ltab[i].free == c->leb_size) in need_write_all()
731 free += c->leb_size; in need_write_all()
732 else if (c->ltab[i].free + c->ltab[i].dirty == c->leb_size) in need_write_all()
733 free += c->leb_size; in need_write_all()
736 if (free <= c->lpt_sz * 2) in need_write_all()
743 * @c: UBIFS file-system description object
749 static void lpt_tgc_start(struct ubifs_info *c) in lpt_tgc_start() argument
753 for (i = 0; i < c->lpt_lebs; i++) { in lpt_tgc_start()
754 if (i + c->lpt_first == c->nhead_lnum) in lpt_tgc_start()
756 if (c->ltab[i].dirty > 0 && in lpt_tgc_start()
757 c->ltab[i].free + c->ltab[i].dirty == c->leb_size) { in lpt_tgc_start()
758 c->ltab[i].tgc = 1; in lpt_tgc_start()
759 c->ltab[i].free = c->leb_size; in lpt_tgc_start()
760 c->ltab[i].dirty = 0; in lpt_tgc_start()
761 dbg_lp("LEB %d", i + c->lpt_first); in lpt_tgc_start()
768 * @c: UBIFS file-system description object
775 static int lpt_tgc_end(struct ubifs_info *c) in lpt_tgc_end() argument
779 for (i = 0; i < c->lpt_lebs; i++) in lpt_tgc_end()
780 if (c->ltab[i].tgc) { in lpt_tgc_end()
781 err = ubifs_leb_unmap(c, i + c->lpt_first); in lpt_tgc_end()
784 c->ltab[i].tgc = 0; in lpt_tgc_end()
785 dbg_lp("LEB %d", i + c->lpt_first); in lpt_tgc_end()
792 * @c: the UBIFS file-system description object
802 static void populate_lsave(struct ubifs_info *c) in populate_lsave() argument
808 ubifs_assert(c->big_lpt); in populate_lsave()
809 if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) { in populate_lsave()
810 c->lpt_drty_flgs |= LSAVE_DIRTY; in populate_lsave()
811 ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz); in populate_lsave()
815 if (dbg_populate_lsave(c)) in populate_lsave()
819 list_for_each_entry(lprops, &c->empty_list, list) { in populate_lsave()
820 c->lsave[cnt++] = lprops->lnum; in populate_lsave()
821 if (cnt >= c->lsave_cnt) in populate_lsave()
824 list_for_each_entry(lprops, &c->freeable_list, list) { in populate_lsave()
825 c->lsave[cnt++] = lprops->lnum; in populate_lsave()
826 if (cnt >= c->lsave_cnt) in populate_lsave()
829 list_for_each_entry(lprops, &c->frdi_idx_list, list) { in populate_lsave()
830 c->lsave[cnt++] = lprops->lnum; in populate_lsave()
831 if (cnt >= c->lsave_cnt) in populate_lsave()
834 heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 1]; in populate_lsave()
836 c->lsave[cnt++] = heap->arr[i]->lnum; in populate_lsave()
837 if (cnt >= c->lsave_cnt) in populate_lsave()
840 heap = &c->lpt_heap[LPROPS_DIRTY - 1]; in populate_lsave()
842 c->lsave[cnt++] = heap->arr[i]->lnum; in populate_lsave()
843 if (cnt >= c->lsave_cnt) in populate_lsave()
846 heap = &c->lpt_heap[LPROPS_FREE - 1]; in populate_lsave()
848 c->lsave[cnt++] = heap->arr[i]->lnum; in populate_lsave()
849 if (cnt >= c->lsave_cnt) in populate_lsave()
853 while (cnt < c->lsave_cnt) in populate_lsave()
854 c->lsave[cnt++] = c->main_first; in populate_lsave()
859 * @c: UBIFS file-system description object
865 static struct ubifs_nnode *nnode_lookup(struct ubifs_info *c, int i) in nnode_lookup() argument
870 if (!c->nroot) { in nnode_lookup()
871 err = ubifs_read_nnode(c, NULL, 0); in nnode_lookup()
875 nnode = c->nroot; in nnode_lookup()
881 nnode = ubifs_get_nnode(c, nnode, iip); in nnode_lookup()
890 * @c: UBIFS file-system description object
896 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
903 static int make_nnode_dirty(struct ubifs_info *c, int node_num, int lnum, in make_nnode_dirty() argument
908 nnode = nnode_lookup(c, node_num); in make_nnode_dirty()
917 } else if (c->lpt_lnum != lnum || c->lpt_offs != offs) in make_nnode_dirty()
921 c->dirty_nn_cnt += 1; in make_nnode_dirty()
922 ubifs_add_nnode_dirt(c, nnode); in make_nnode_dirty()
927 c->dirty_nn_cnt += 1; in make_nnode_dirty()
928 ubifs_add_nnode_dirt(c, nnode); in make_nnode_dirty()
939 * @c: UBIFS file-system description object
945 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
952 static int make_pnode_dirty(struct ubifs_info *c, int node_num, int lnum, in make_pnode_dirty() argument
958 pnode = pnode_lookup(c, node_num); in make_pnode_dirty()
964 do_make_pnode_dirty(c, pnode); in make_pnode_dirty()
970 * @c: UBIFS file-system description object
975 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
982 static int make_ltab_dirty(struct ubifs_info *c, int lnum, int offs) in make_ltab_dirty() argument
984 if (lnum != c->ltab_lnum || offs != c->ltab_offs) in make_ltab_dirty()
986 if (!(c->lpt_drty_flgs & LTAB_DIRTY)) { in make_ltab_dirty()
987 c->lpt_drty_flgs |= LTAB_DIRTY; in make_ltab_dirty()
988 ubifs_add_lpt_dirt(c, c->ltab_lnum, c->ltab_sz); in make_ltab_dirty()
995 * @c: UBIFS file-system description object
1000 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
1007 static int make_lsave_dirty(struct ubifs_info *c, int lnum, int offs) in make_lsave_dirty() argument
1009 if (lnum != c->lsave_lnum || offs != c->lsave_offs) in make_lsave_dirty()
1011 if (!(c->lpt_drty_flgs & LSAVE_DIRTY)) { in make_lsave_dirty()
1012 c->lpt_drty_flgs |= LSAVE_DIRTY; in make_lsave_dirty()
1013 ubifs_add_lpt_dirt(c, c->lsave_lnum, c->lsave_sz); in make_lsave_dirty()
1020 * @c: UBIFS file-system description object
1027 * used only for the "big" LPT model (c->big_lpt == 1). Garbage collection
1034 static int make_node_dirty(struct ubifs_info *c, int node_type, int node_num, in make_node_dirty() argument
1039 return make_nnode_dirty(c, node_num, lnum, offs); in make_node_dirty()
1041 return make_pnode_dirty(c, node_num, lnum, offs); in make_node_dirty()
1043 return make_ltab_dirty(c, lnum, offs); in make_node_dirty()
1045 return make_lsave_dirty(c, lnum, offs); in make_node_dirty()
1052 * @c: UBIFS file-system description object
1055 static int get_lpt_node_len(const struct ubifs_info *c, int node_type) in get_lpt_node_len() argument
1059 return c->nnode_sz; in get_lpt_node_len()
1061 return c->pnode_sz; in get_lpt_node_len()
1063 return c->ltab_sz; in get_lpt_node_len()
1065 return c->lsave_sz; in get_lpt_node_len()
1072 * @c: UBIFS file-system description object
1076 static int get_pad_len(const struct ubifs_info *c, uint8_t *buf, int len) in get_pad_len() argument
1080 if (c->min_io_size == 1) in get_pad_len()
1082 offs = c->leb_size - len; in get_pad_len()
1083 pad_len = ALIGN(offs, c->min_io_size) - offs; in get_pad_len()
1089 * @c: UBIFS file-system description object
1093 static int get_lpt_node_type(const struct ubifs_info *c, uint8_t *buf, in get_lpt_node_type() argument
1100 *node_num = ubifs_unpack_bits(&addr, &pos, c->pcnt_bits); in get_lpt_node_type()
1106 * @c: UBIFS file-system description object
1112 static int is_a_node(const struct ubifs_info *c, uint8_t *buf, int len) in is_a_node() argument
1123 node_len = get_lpt_node_len(c, node_type); in is_a_node()
1138 * @c: UBIFS file-system description object
1142 * (c->big_lpt == 1). Garbage collection simply involves marking all the nodes
1148 static int lpt_gc_lnum(struct ubifs_info *c, int lnum) in lpt_gc_lnum() argument
1150 int err, len = c->leb_size, node_type, node_num, node_len, offs; in lpt_gc_lnum()
1151 void *buf = c->lpt_buf; in lpt_gc_lnum()
1155 err = ubifs_leb_read(c, lnum, buf, 0, c->leb_size, 1); in lpt_gc_lnum()
1160 if (!is_a_node(c, buf, len)) { in lpt_gc_lnum()
1163 pad_len = get_pad_len(c, buf, len); in lpt_gc_lnum()
1171 node_type = get_lpt_node_type(c, buf, &node_num); in lpt_gc_lnum()
1172 node_len = get_lpt_node_len(c, node_type); in lpt_gc_lnum()
1173 offs = c->leb_size - len; in lpt_gc_lnum()
1175 mutex_lock(&c->lp_mutex); in lpt_gc_lnum()
1176 err = make_node_dirty(c, node_type, node_num, lnum, offs); in lpt_gc_lnum()
1177 mutex_unlock(&c->lp_mutex); in lpt_gc_lnum()
1188 * @c: UBIFS file-system description object
1193 static int lpt_gc(struct ubifs_info *c) in lpt_gc() argument
1197 mutex_lock(&c->lp_mutex); in lpt_gc()
1198 for (i = 0; i < c->lpt_lebs; i++) { in lpt_gc()
1199 ubifs_assert(!c->ltab[i].tgc); in lpt_gc()
1200 if (i + c->lpt_first == c->nhead_lnum || in lpt_gc()
1201 c->ltab[i].free + c->ltab[i].dirty == c->leb_size) in lpt_gc()
1203 if (c->ltab[i].dirty > dirty) { in lpt_gc()
1204 dirty = c->ltab[i].dirty; in lpt_gc()
1205 lnum = i + c->lpt_first; in lpt_gc()
1208 mutex_unlock(&c->lp_mutex); in lpt_gc()
1211 return lpt_gc_lnum(c, lnum); in lpt_gc()
1216 * @c: the UBIFS file-system description object
1224 int ubifs_lpt_start_commit(struct ubifs_info *c) in ubifs_lpt_start_commit() argument
1230 mutex_lock(&c->lp_mutex); in ubifs_lpt_start_commit()
1231 err = dbg_chk_lpt_free_spc(c); in ubifs_lpt_start_commit()
1234 err = dbg_check_ltab(c); in ubifs_lpt_start_commit()
1238 if (c->check_lpt_free) { in ubifs_lpt_start_commit()
1245 c->check_lpt_free = 0; in ubifs_lpt_start_commit()
1246 while (need_write_all(c)) { in ubifs_lpt_start_commit()
1247 mutex_unlock(&c->lp_mutex); in ubifs_lpt_start_commit()
1248 err = lpt_gc(c); in ubifs_lpt_start_commit()
1251 mutex_lock(&c->lp_mutex); in ubifs_lpt_start_commit()
1255 lpt_tgc_start(c); in ubifs_lpt_start_commit()
1257 if (!c->dirty_pn_cnt) { in ubifs_lpt_start_commit()
1263 if (!c->big_lpt && need_write_all(c)) { in ubifs_lpt_start_commit()
1265 err = make_tree_dirty(c); in ubifs_lpt_start_commit()
1268 lpt_tgc_start(c); in ubifs_lpt_start_commit()
1271 if (c->big_lpt) in ubifs_lpt_start_commit()
1272 populate_lsave(c); in ubifs_lpt_start_commit()
1274 cnt = get_cnodes_to_commit(c); in ubifs_lpt_start_commit()
1277 err = layout_cnodes(c); in ubifs_lpt_start_commit()
1282 memcpy(c->ltab_cmt, c->ltab, in ubifs_lpt_start_commit()
1283 sizeof(struct ubifs_lpt_lprops) * c->lpt_lebs); in ubifs_lpt_start_commit()
1284 c->lpt_drty_flgs &= ~(LTAB_DIRTY | LSAVE_DIRTY); in ubifs_lpt_start_commit()
1287 mutex_unlock(&c->lp_mutex); in ubifs_lpt_start_commit()
1293 * @c: UBIFS file-system description object
1295 static void free_obsolete_cnodes(struct ubifs_info *c) in free_obsolete_cnodes() argument
1299 cnext = c->lpt_cnext; in free_obsolete_cnodes()
1309 } while (cnext != c->lpt_cnext); in free_obsolete_cnodes()
1310 c->lpt_cnext = NULL; in free_obsolete_cnodes()
1316 * @c: the UBIFS file-system description object
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()
1346 * @c: UBIFS file-system description object
1351 int ubifs_lpt_post_commit(struct ubifs_info *c) in ubifs_lpt_post_commit() argument
1355 mutex_lock(&c->lp_mutex); in ubifs_lpt_post_commit()
1356 err = lpt_tgc_end(c); in ubifs_lpt_post_commit()
1359 if (c->big_lpt) in ubifs_lpt_post_commit()
1360 while (need_write_all(c)) { in ubifs_lpt_post_commit()
1361 mutex_unlock(&c->lp_mutex); in ubifs_lpt_post_commit()
1362 err = lpt_gc(c); in ubifs_lpt_post_commit()
1365 mutex_lock(&c->lp_mutex); in ubifs_lpt_post_commit()
1368 mutex_unlock(&c->lp_mutex); in ubifs_lpt_post_commit()
1374 * @c: UBIFS file-system description object
1380 static struct ubifs_nnode *first_nnode(struct ubifs_info *c, int *hght) in first_nnode() argument
1385 nnode = c->nroot; in first_nnode()
1389 for (h = 1; h < c->lpt_hght; h++) { in first_nnode()
1407 * @c: UBIFS file-system description object
1414 static struct ubifs_nnode *next_nnode(struct ubifs_info *c, in next_nnode() argument
1436 for (h = *hght + 1; h < c->lpt_hght; h++) { in next_nnode()
1454 * @c: UBIFS file-system description object
1457 void ubifs_lpt_free(struct ubifs_info *c, int wr_only) in ubifs_lpt_free() argument
1464 free_obsolete_cnodes(c); /* Leftover from a failed commit */ in ubifs_lpt_free()
1466 vfree(c->ltab_cmt); in ubifs_lpt_free()
1467 c->ltab_cmt = NULL; in ubifs_lpt_free()
1468 vfree(c->lpt_buf); in ubifs_lpt_free()
1469 c->lpt_buf = NULL; in ubifs_lpt_free()
1470 kfree(c->lsave); in ubifs_lpt_free()
1471 c->lsave = NULL; in ubifs_lpt_free()
1478 nnode = first_nnode(c, &hght); in ubifs_lpt_free()
1482 nnode = next_nnode(c, nnode, &hght); in ubifs_lpt_free()
1485 kfree(c->lpt_heap[i].arr); in ubifs_lpt_free()
1486 kfree(c->dirty_idx.arr); in ubifs_lpt_free()
1487 kfree(c->nroot); in ubifs_lpt_free()
1488 vfree(c->ltab); in ubifs_lpt_free()
1489 kfree(c->lpt_nod_buf); in ubifs_lpt_free()
1514 * @c: the UBIFS file-system description object
1518 static int dbg_is_nnode_dirty(struct ubifs_info *c, int lnum, int offs) in dbg_is_nnode_dirty() argument
1524 nnode = first_nnode(c, &hght); in dbg_is_nnode_dirty()
1525 for (; nnode; nnode = next_nnode(c, nnode, &hght)) { in dbg_is_nnode_dirty()
1537 if (c->lpt_lnum != lnum || c->lpt_offs != offs) in dbg_is_nnode_dirty()
1549 * @c: the UBIFS file-system description object
1553 static int dbg_is_pnode_dirty(struct ubifs_info *c, int lnum, int offs) in dbg_is_pnode_dirty() argument
1557 cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT); in dbg_is_pnode_dirty()
1563 pnode = pnode_lookup(c, i); in dbg_is_pnode_dirty()
1578 * @c: the UBIFS file-system description object
1582 static int dbg_is_ltab_dirty(struct ubifs_info *c, int lnum, int offs) in dbg_is_ltab_dirty() argument
1584 if (lnum != c->ltab_lnum || offs != c->ltab_offs) in dbg_is_ltab_dirty()
1586 return (c->lpt_drty_flgs & LTAB_DIRTY) != 0; in dbg_is_ltab_dirty()
1591 * @c: the UBIFS file-system description object
1595 static int dbg_is_lsave_dirty(struct ubifs_info *c, int lnum, int offs) in dbg_is_lsave_dirty() argument
1597 if (lnum != c->lsave_lnum || offs != c->lsave_offs) in dbg_is_lsave_dirty()
1599 return (c->lpt_drty_flgs & LSAVE_DIRTY) != 0; in dbg_is_lsave_dirty()
1604 * @c: the UBIFS file-system description object
1609 static int dbg_is_node_dirty(struct ubifs_info *c, int node_type, int lnum, in dbg_is_node_dirty() argument
1614 return dbg_is_nnode_dirty(c, lnum, offs); in dbg_is_node_dirty()
1616 return dbg_is_pnode_dirty(c, lnum, offs); in dbg_is_node_dirty()
1618 return dbg_is_ltab_dirty(c, lnum, offs); in dbg_is_node_dirty()
1620 return dbg_is_lsave_dirty(c, lnum, offs); in dbg_is_node_dirty()
1627 * @c: the UBIFS file-system description object
1633 static int dbg_check_ltab_lnum(struct ubifs_info *c, int lnum) in dbg_check_ltab_lnum() argument
1635 int err, len = c->leb_size, dirty = 0, node_type, node_num, node_len; in dbg_check_ltab_lnum()
1639 if (!dbg_is_chk_lprops(c)) in dbg_check_ltab_lnum()
1642 buf = p = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL); in dbg_check_ltab_lnum()
1644 ubifs_err(c, "cannot allocate memory for ltab checking"); in dbg_check_ltab_lnum()
1650 err = ubifs_leb_read(c, lnum, buf, 0, c->leb_size, 1); in dbg_check_ltab_lnum()
1655 if (!is_a_node(c, p, len)) { in dbg_check_ltab_lnum()
1658 pad_len = get_pad_len(c, p, len); in dbg_check_ltab_lnum()
1666 ubifs_err(c, "invalid empty space in LEB %d at %d", in dbg_check_ltab_lnum()
1667 lnum, c->leb_size - len); in dbg_check_ltab_lnum()
1670 i = lnum - c->lpt_first; in dbg_check_ltab_lnum()
1671 if (len != c->ltab[i].free) { in dbg_check_ltab_lnum()
1672 ubifs_err(c, "invalid free space in LEB %d (free %d, expected %d)", in dbg_check_ltab_lnum()
1673 lnum, len, c->ltab[i].free); in dbg_check_ltab_lnum()
1676 if (dirty != c->ltab[i].dirty) { in dbg_check_ltab_lnum()
1677 ubifs_err(c, "invalid dirty space in LEB %d (dirty %d, expected %d)", in dbg_check_ltab_lnum()
1678 lnum, dirty, c->ltab[i].dirty); in dbg_check_ltab_lnum()
1683 node_type = get_lpt_node_type(c, p, &node_num); in dbg_check_ltab_lnum()
1684 node_len = get_lpt_node_len(c, node_type); in dbg_check_ltab_lnum()
1685 ret = dbg_is_node_dirty(c, node_type, lnum, c->leb_size - len); in dbg_check_ltab_lnum()
1700 * @c: the UBIFS file-system description object
1704 int dbg_check_ltab(struct ubifs_info *c) in dbg_check_ltab() argument
1708 if (!dbg_is_chk_lprops(c)) in dbg_check_ltab()
1712 cnt = DIV_ROUND_UP(c->main_lebs, UBIFS_LPT_FANOUT); in dbg_check_ltab()
1716 pnode = pnode_lookup(c, i); in dbg_check_ltab()
1723 err = dbg_check_lpt_nodes(c, (struct ubifs_cnode *)c->nroot, 0, 0); in dbg_check_ltab()
1728 for (lnum = c->lpt_first; lnum <= c->lpt_last; lnum++) { in dbg_check_ltab()
1729 err = dbg_check_ltab_lnum(c, lnum); in dbg_check_ltab()
1731 ubifs_err(c, "failed at LEB %d", lnum); in dbg_check_ltab()
1742 * @c: the UBIFS file-system description object
1746 int dbg_chk_lpt_free_spc(struct ubifs_info *c) in dbg_chk_lpt_free_spc() argument
1751 if (!dbg_is_chk_lprops(c)) in dbg_chk_lpt_free_spc()
1754 for (i = 0; i < c->lpt_lebs; i++) { in dbg_chk_lpt_free_spc()
1755 if (c->ltab[i].tgc || c->ltab[i].cmt) in dbg_chk_lpt_free_spc()
1757 if (i + c->lpt_first == c->nhead_lnum) in dbg_chk_lpt_free_spc()
1758 free += c->leb_size - c->nhead_offs; in dbg_chk_lpt_free_spc()
1759 else if (c->ltab[i].free == c->leb_size) in dbg_chk_lpt_free_spc()
1760 free += c->leb_size; in dbg_chk_lpt_free_spc()
1762 if (free < c->lpt_sz) { in dbg_chk_lpt_free_spc()
1763 ubifs_err(c, "LPT space error: free %lld lpt_sz %lld", in dbg_chk_lpt_free_spc()
1764 free, c->lpt_sz); in dbg_chk_lpt_free_spc()
1765 ubifs_dump_lpt_info(c); in dbg_chk_lpt_free_spc()
1766 ubifs_dump_lpt_lebs(c); in dbg_chk_lpt_free_spc()
1775 * @c: the UBIFS file-system description object
1787 int dbg_chk_lpt_sz(struct ubifs_info *c, int action, int len) in dbg_chk_lpt_sz() argument
1789 struct ubifs_debug_info *d = c->dbg; in dbg_chk_lpt_sz()
1793 if (!dbg_is_chk_lprops(c)) in dbg_chk_lpt_sz()
1802 if (c->dirty_pn_cnt > c->pnode_cnt) { in dbg_chk_lpt_sz()
1803 ubifs_err(c, "dirty pnodes %d exceed max %d", in dbg_chk_lpt_sz()
1804 c->dirty_pn_cnt, c->pnode_cnt); in dbg_chk_lpt_sz()
1807 if (c->dirty_nn_cnt > c->nnode_cnt) { in dbg_chk_lpt_sz()
1808 ubifs_err(c, "dirty nnodes %d exceed max %d", in dbg_chk_lpt_sz()
1809 c->dirty_nn_cnt, c->nnode_cnt); in dbg_chk_lpt_sz()
1822 chk_lpt_sz = c->leb_size; in dbg_chk_lpt_sz()
1824 chk_lpt_sz += len - c->nhead_offs; in dbg_chk_lpt_sz()
1826 ubifs_err(c, "LPT wrote %lld but space used was %lld", in dbg_chk_lpt_sz()
1830 if (d->chk_lpt_sz > c->lpt_sz) { in dbg_chk_lpt_sz()
1831 ubifs_err(c, "LPT wrote %lld but lpt_sz is %lld", in dbg_chk_lpt_sz()
1832 d->chk_lpt_sz, c->lpt_sz); in dbg_chk_lpt_sz()
1836 ubifs_err(c, "LPT layout size %lld but wrote %lld", in dbg_chk_lpt_sz()
1841 ubifs_err(c, "LPT new nhead offs: expected %d was %d", in dbg_chk_lpt_sz()
1845 lpt_sz = (long long)c->pnode_cnt * c->pnode_sz; in dbg_chk_lpt_sz()
1846 lpt_sz += (long long)c->nnode_cnt * c->nnode_sz; in dbg_chk_lpt_sz()
1847 lpt_sz += c->ltab_sz; in dbg_chk_lpt_sz()
1848 if (c->big_lpt) in dbg_chk_lpt_sz()
1849 lpt_sz += c->lsave_sz; in dbg_chk_lpt_sz()
1851 ubifs_err(c, "LPT chk_lpt_sz %lld + waste %lld exceeds %lld", in dbg_chk_lpt_sz()
1856 ubifs_dump_lpt_info(c); in dbg_chk_lpt_sz()
1857 ubifs_dump_lpt_lebs(c); in dbg_chk_lpt_sz()
1877 * @c: UBIFS file-system description object
1885 static void dump_lpt_leb(const struct ubifs_info *c, int lnum) in dump_lpt_leb() argument
1887 int err, len = c->leb_size, node_type, node_num, node_len, offs; in dump_lpt_leb()
1891 buf = p = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL); in dump_lpt_leb()
1893 ubifs_err(c, "cannot allocate memory to dump LPT"); in dump_lpt_leb()
1897 err = ubifs_leb_read(c, lnum, buf, 0, c->leb_size, 1); in dump_lpt_leb()
1902 offs = c->leb_size - len; in dump_lpt_leb()
1903 if (!is_a_node(c, p, len)) { in dump_lpt_leb()
1906 pad_len = get_pad_len(c, p, len); in dump_lpt_leb()
1920 node_type = get_lpt_node_type(c, p, &node_num); in dump_lpt_leb()
1924 node_len = c->pnode_sz; in dump_lpt_leb()
1925 if (c->big_lpt) in dump_lpt_leb()
1937 node_len = c->nnode_sz; in dump_lpt_leb()
1938 if (c->big_lpt) in dump_lpt_leb()
1944 err = ubifs_unpack_nnode(c, p, &nnode); in dump_lpt_leb()
1960 node_len = c->ltab_sz; in dump_lpt_leb()
1964 node_len = c->lsave_sz; in dump_lpt_leb()
1968 ubifs_err(c, "LPT node type %d not recognized", node_type); in dump_lpt_leb()
1984 * @c: UBIFS file-system description object
1989 void ubifs_dump_lpt_lebs(const struct ubifs_info *c) in ubifs_dump_lpt_lebs() argument
1994 for (i = 0; i < c->lpt_lebs; i++) in ubifs_dump_lpt_lebs()
1995 dump_lpt_leb(c, i + c->lpt_first); in ubifs_dump_lpt_lebs()
2001 * @c: UBIFS file-system description object
2008 static int dbg_populate_lsave(struct ubifs_info *c) in dbg_populate_lsave() argument
2014 if (!dbg_is_chk_gen(c)) in dbg_populate_lsave()
2019 for (i = 0; i < c->lsave_cnt; i++) in dbg_populate_lsave()
2020 c->lsave[i] = c->main_first; in dbg_populate_lsave()
2022 list_for_each_entry(lprops, &c->empty_list, list) in dbg_populate_lsave()
2023 c->lsave[prandom_u32() % c->lsave_cnt] = lprops->lnum; in dbg_populate_lsave()
2024 list_for_each_entry(lprops, &c->freeable_list, list) in dbg_populate_lsave()
2025 c->lsave[prandom_u32() % c->lsave_cnt] = lprops->lnum; in dbg_populate_lsave()
2026 list_for_each_entry(lprops, &c->frdi_idx_list, list) in dbg_populate_lsave()
2027 c->lsave[prandom_u32() % c->lsave_cnt] = lprops->lnum; in dbg_populate_lsave()
2029 heap = &c->lpt_heap[LPROPS_DIRTY_IDX - 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_DIRTY - 1]; in dbg_populate_lsave()
2034 c->lsave[prandom_u32() % c->lsave_cnt] = heap->arr[i]->lnum; in dbg_populate_lsave()
2035 heap = &c->lpt_heap[LPROPS_FREE - 1]; in dbg_populate_lsave()
2037 c->lsave[prandom_u32() % c->lsave_cnt] = heap->arr[i]->lnum; in dbg_populate_lsave()