25 #include <linux/random.h>
51 key_write_idx(c, &zbr->
key, &br->
key);
111 static int fill_gap(
struct ubifs_info *c,
int lnum,
int gap_start,
int gap_end,
114 int len, gap_remains, gap_pos, written,
pad_len;
120 gap_remains = gap_end - gap_start;
126 len = ubifs_idx_node_sz(c, c->
enext->child_cnt);
127 if (len < gap_remains) {
129 const int alen =
ALIGN(len, 8);
133 err = make_idx_node(c, c->
ileb_buf + gap_pos, znode,
152 pad_len = gap_remains;
153 dbg_gc(
"LEB %d:%d to %d len %d nodes written %d wasted bytes %d",
154 lnum, gap_start, gap_end, gap_end - gap_start, written, pad_len);
168 static int find_old_idx(
struct ubifs_info *c,
int lnum,
int offs)
178 else if (lnum > o->
lnum)
180 else if (offs < o->offs)
182 else if (offs > o->
offs)
204 int level,
int lnum,
int offs)
212 if (find_old_idx(c, lnum, offs))
229 static int layout_leb_in_gaps(
struct ubifs_info *c,
int *p)
233 int lnum, dirt = 0, gap_start, gap_end,
err, written, tot_written;
254 return PTR_ERR(sleb);
262 key_read(c, ubifs_idx_key(c, idx), &snod->
key);
265 in_use = is_idx_node_in_use(c, &snod->
key, level, lnum,
280 gap_end = snod->
offs;
282 written = fill_gap(c, lnum, gap_start, gap_end, &dirt);
287 tot_written += written;
295 written = fill_gap(c, lnum, gap_start, gap_end, &dirt);
298 tot_written += written;
299 if (tot_written == 0) {
302 dbg_gc(
"LEB %d wrote %d index nodes", lnum, tot_written);
326 dbg_gc(
"LEB %d wrote %d index nodes", lnum, tot_written);
361 static int layout_in_gaps(
struct ubifs_info *c,
int cnt)
363 int err, leb_needed_cnt, written, *
p;
365 dbg_gc(
"%d znodes to write", cnt);
374 written = layout_leb_in_gaps(c, p);
382 if (!dbg_is_chk_index(c)) {
397 leb_needed_cnt = get_leb_cnt(c, cnt);
398 dbg_gc(
"%d znodes remaining, need %d LEBs, have %d", cnt,
400 }
while (leb_needed_cnt > c->
ileb_cnt);
414 static int layout_in_empty_space(
struct ubifs_info *c)
427 buf_len = ubifs_idx_node_sz(c, c->
fanout);
433 next_len = ubifs_idx_node_sz(c, cnext->
child_cnt);
434 if (buf_offs + next_len > c->
leb_size)
440 len = ubifs_idx_node_sz(c, znode->
child_cnt);
454 offs = buf_offs +
used;
488 cnext = znode->
cnext;
489 if (cnext == c->
cnext)
492 next_len = ubifs_idx_node_sz(c, cnext->
child_cnt);
496 used +=
ALIGN(len, 8);
497 avail -=
ALIGN(len, 8);
500 buf_offs + used + next_len <= c->leb_size &&
504 if (avail <= 0 && next_len &&
505 buf_offs + used + next_len <= c->leb_size)
513 if (buf_offs + next_len > c->
leb_size) {
515 c->
leb_size - buf_offs, blen - used,
524 avail = buf_len -
used;
535 c->
dbg->new_ihead_offs = buf_offs;
553 static int layout_commit(
struct ubifs_info *c,
int no_space,
int cnt)
558 err = layout_in_gaps(c, cnt);
562 err = layout_in_empty_space(c);
578 if (znode->
level == 0) {
579 if (ubifs_zn_dirty(znode))
587 if (zbr->
znode && ubifs_zn_dirty(zbr->
znode)) {
594 if (ubifs_zn_dirty(znode))
607 int n = znode->
iip + 1;
616 return find_first_dirty(zbr->
znode);
627 static int get_znodes_to_commit(
struct ubifs_info *c)
635 dbg_cmt(
"no znodes to commit");
643 cnext = find_next_dirty(znode);
652 dbg_cmt(
"committing %d znodes", cnt);
666 static int alloc_idx_lebs(
struct ubifs_info *c,
int cnt)
668 int i, leb_cnt,
lnum;
672 leb_cnt = get_leb_cnt(c, cnt);
673 dbg_cmt(
"need about %d empty LEBS for TNC commit", leb_cnt);
679 for (i = 0; i < leb_cnt; i++) {
686 if (dbg_is_chk_index(c) && !(
random32() & 7))
700 static int free_unused_idx_lebs(
struct ubifs_info *c)
702 int i, err = 0,
lnum, er;
704 for (i = c->
ileb_nxt; i < c->ileb_cnt; i++) {
721 static int free_idx_lebs(
struct ubifs_info *c)
725 err = free_unused_idx_lebs(c);
749 cnt = get_znodes_to_commit(c);
753 err = alloc_idx_lebs(c, cnt);
758 err = layout_commit(c, no_space, cnt);
762 err = free_unused_idx_lebs(c);
784 c->
bi.uncommitted_idx = 0;
789 dbg_cmt(
"number of index LEBs %d", c->
lst.idx_lebs);
812 int avail, wlen,
err, lnum_pos = 0, blen, nxt_offs;
831 next_len = ubifs_idx_node_sz(c, cnext->
child_cnt);
832 if (buf_offs + next_len > c->
leb_size) {
854 key_write_idx(c, &zbr->
key, &br->
key);
865 len = ubifs_idx_node_sz(c, znode->
child_cnt);
870 lnum = c->
ilebs[lnum_pos++];
875 offs = buf_offs +
used;
877 if (lnum != znode->
lnum || offs != znode->
offs ||
884 cnext = znode->
cnext;
928 used +=
ALIGN(len, 8);
929 avail -=
ALIGN(len, 8);
935 if (cnext == c->
cnext)
938 next_len = ubifs_idx_node_sz(c, cnext->
child_cnt);
940 nxt_offs = buf_offs + used + next_len;
941 if (next_len && nxt_offs <= c->leb_size) {
947 wlen =
ALIGN(wlen, 8);
968 avail = buf_len -
used;
975 if (lnum != c->
dbg->new_ihead_lnum ||
976 buf_offs != c->
dbg->new_ihead_offs) {
993 static void free_obsolete_znodes(
struct ubifs_info *c)
1000 cnext = znode->
cnext;
1001 if (ubifs_zn_obsolete(znode))
1008 }
while (cnext != c->
cnext);
1018 static int return_gap_lebs(
struct ubifs_info *c)
1026 for (p = c->
gap_lebs; *p != -1; p++) {
1051 err = return_gap_lebs(c);
1055 err = write_index(c);
1061 dbg_cmt(
"TNC height is %d", c->
zroot.znode->level + 1);
1063 free_obsolete_znodes(c);