Lines Matching +full:- +full:c
1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2006-2008 Nokia Corporation.
17 * make_idx_node - make an index node for fill-the-gaps method of TNC commit.
18 * @c: UBIFS file-system description object
25 static int make_idx_node(struct ubifs_info *c, struct ubifs_idx_node *idx, in make_idx_node() argument
33 idx->ch.node_type = UBIFS_IDX_NODE; in make_idx_node()
34 idx->child_cnt = cpu_to_le16(znode->child_cnt); in make_idx_node()
35 idx->level = cpu_to_le16(znode->level); in make_idx_node()
36 for (i = 0; i < znode->child_cnt; i++) { in make_idx_node()
37 struct ubifs_branch *br = ubifs_idx_branch(c, idx, i); in make_idx_node()
38 struct ubifs_zbranch *zbr = &znode->zbranch[i]; in make_idx_node()
40 key_write_idx(c, &zbr->key, &br->key); in make_idx_node()
41 br->lnum = cpu_to_le32(zbr->lnum); in make_idx_node()
42 br->offs = cpu_to_le32(zbr->offs); in make_idx_node()
43 br->len = cpu_to_le32(zbr->len); in make_idx_node()
44 ubifs_copy_hash(c, zbr->hash, ubifs_branch_hash(c, br)); in make_idx_node()
45 if (!zbr->lnum || !zbr->len) { in make_idx_node()
46 ubifs_err(c, "bad ref in znode"); in make_idx_node()
47 ubifs_dump_znode(c, znode); in make_idx_node()
48 if (zbr->znode) in make_idx_node()
49 ubifs_dump_znode(c, zbr->znode); in make_idx_node()
51 return -EINVAL; in make_idx_node()
54 ubifs_prepare_node(c, idx, len, 0); in make_idx_node()
55 ubifs_node_calc_hash(c, idx, hash); in make_idx_node()
57 znode->lnum = lnum; in make_idx_node()
58 znode->offs = offs; in make_idx_node()
59 znode->len = len; in make_idx_node()
61 err = insert_old_idx_znode(c, znode); in make_idx_node()
64 zp = znode->parent; in make_idx_node()
68 zbr = &zp->zbranch[znode->iip]; in make_idx_node()
69 zbr->lnum = lnum; in make_idx_node()
70 zbr->offs = offs; in make_idx_node()
71 zbr->len = len; in make_idx_node()
72 ubifs_copy_hash(c, hash, zbr->hash); in make_idx_node()
74 c->zroot.lnum = lnum; in make_idx_node()
75 c->zroot.offs = offs; in make_idx_node()
76 c->zroot.len = len; in make_idx_node()
77 ubifs_copy_hash(c, hash, c->zroot.hash); in make_idx_node()
79 c->calc_idx_sz += ALIGN(len, 8); in make_idx_node()
81 atomic_long_dec(&c->dirty_zn_cnt); in make_idx_node()
83 ubifs_assert(c, ubifs_zn_dirty(znode)); in make_idx_node()
84 ubifs_assert(c, ubifs_zn_cow(znode)); in make_idx_node()
88 * because this function is called with @c->tnc_mutex locked. in make_idx_node()
90 __clear_bit(DIRTY_ZNODE, &znode->flags); in make_idx_node()
91 __clear_bit(COW_ZNODE, &znode->flags); in make_idx_node()
97 * fill_gap - make index nodes in gaps in dirty index LEBs.
98 * @c: UBIFS file-system description object
106 static int fill_gap(struct ubifs_info *c, int lnum, int gap_start, int gap_end, in fill_gap() argument
111 ubifs_assert(c, (gap_start & 7) == 0); in fill_gap()
112 ubifs_assert(c, (gap_end & 7) == 0); in fill_gap()
113 ubifs_assert(c, gap_end >= gap_start); in fill_gap()
115 gap_remains = gap_end - gap_start; in fill_gap()
120 while (c->enext) { in fill_gap()
121 len = ubifs_idx_node_sz(c, c->enext->child_cnt); in fill_gap()
123 struct ubifs_znode *znode = c->enext; in fill_gap()
127 ubifs_assert(c, alen <= gap_remains); in fill_gap()
128 err = make_idx_node(c, c->ileb_buf + gap_pos, znode, in fill_gap()
132 gap_remains -= alen; in fill_gap()
134 c->enext = znode->cnext; in fill_gap()
135 if (c->enext == c->cnext) in fill_gap()
136 c->enext = NULL; in fill_gap()
141 if (gap_end == c->leb_size) { in fill_gap()
142 c->ileb_len = ALIGN(gap_pos, c->min_io_size); in fill_gap()
144 pad_len = c->ileb_len - gap_pos; in fill_gap()
149 lnum, gap_start, gap_end, gap_end - gap_start, written, pad_len); in fill_gap()
150 ubifs_pad(c, c->ileb_buf + gap_pos, pad_len); in fill_gap()
156 * find_old_idx - find an index node obsoleted since the last commit start.
157 * @c: UBIFS file-system description object
163 static int find_old_idx(struct ubifs_info *c, int lnum, int offs) in find_old_idx() argument
168 p = c->old_idx.rb_node; in find_old_idx()
171 if (lnum < o->lnum) in find_old_idx()
172 p = p->rb_left; in find_old_idx()
173 else if (lnum > o->lnum) in find_old_idx()
174 p = p->rb_right; in find_old_idx()
175 else if (offs < o->offs) in find_old_idx()
176 p = p->rb_left; in find_old_idx()
177 else if (offs > o->offs) in find_old_idx()
178 p = p->rb_right; in find_old_idx()
186 * is_idx_node_in_use - determine if an index node can be overwritten.
187 * @c: UBIFS file-system description object
198 static int is_idx_node_in_use(struct ubifs_info *c, union ubifs_key *key, in is_idx_node_in_use() argument
203 ret = is_idx_node_in_tnc(c, key, level, lnum, offs); in is_idx_node_in_use()
207 if (find_old_idx(c, lnum, offs)) in is_idx_node_in_use()
213 * layout_leb_in_gaps - layout index nodes using in-the-gaps method.
214 * @c: UBIFS file-system description object
215 * @p: return LEB number in @c->gap_lebs[p]
217 * This function lays out new index nodes for dirty znodes using in-the-gaps
224 static int layout_leb_in_gaps(struct ubifs_info *c, int p) in layout_leb_in_gaps() argument
232 lnum = ubifs_find_dirty_idx_leb(c); in layout_leb_in_gaps()
239 c->gap_lebs[p] = lnum; in layout_leb_in_gaps()
246 sleb = ubifs_scan(c, lnum, 0, c->ileb_buf, 0); in layout_leb_in_gaps()
247 c->ileb_len = 0; in layout_leb_in_gaps()
251 list_for_each_entry(snod, &sleb->nodes, list) { in layout_leb_in_gaps()
255 ubifs_assert(c, snod->type == UBIFS_IDX_NODE); in layout_leb_in_gaps()
256 idx = snod->node; in layout_leb_in_gaps()
257 key_read(c, ubifs_idx_key(c, idx), &snod->key); in layout_leb_in_gaps()
258 level = le16_to_cpu(idx->level); in layout_leb_in_gaps()
260 in_use = is_idx_node_in_use(c, &snod->key, level, lnum, in layout_leb_in_gaps()
261 snod->offs); in layout_leb_in_gaps()
268 dirt += ALIGN(snod->len, 8); in layout_leb_in_gaps()
275 gap_end = snod->offs; in layout_leb_in_gaps()
277 written = fill_gap(c, lnum, gap_start, gap_end, &dirt); in layout_leb_in_gaps()
283 gap_start = ALIGN(snod->offs + snod->len, 8); in layout_leb_in_gaps()
287 c->ileb_len = c->leb_size; in layout_leb_in_gaps()
288 gap_end = c->leb_size; in layout_leb_in_gaps()
290 written = fill_gap(c, lnum, gap_start, gap_end, &dirt); in layout_leb_in_gaps()
298 err = ubifs_read_one_lp(c, lnum, &lp); in layout_leb_in_gaps()
301 if (lp.free == c->leb_size) { in layout_leb_in_gaps()
306 err = ubifs_change_one_lp(c, lnum, in layout_leb_in_gaps()
307 c->leb_size - c->ileb_len, in layout_leb_in_gaps()
314 err = ubifs_change_one_lp(c, lnum, c->leb_size - c->ileb_len, dirt, in layout_leb_in_gaps()
318 err = ubifs_leb_change(c, lnum, c->ileb_buf, c->ileb_len); in layout_leb_in_gaps()
326 * get_leb_cnt - calculate the number of empty LEBs needed to commit.
327 * @c: UBIFS file-system description object
334 static int get_leb_cnt(struct ubifs_info *c, int cnt) in get_leb_cnt() argument
339 cnt -= (c->leb_size - c->ihead_offs) / c->max_idx_node_sz; in get_leb_cnt()
342 d = c->leb_size / c->max_idx_node_sz; in get_leb_cnt()
347 * layout_in_gaps - in-the-gaps method of committing TNC.
348 * @c: UBIFS file-system description object
351 * This function lays out new index nodes for dirty znodes using in-the-gaps
356 static int layout_in_gaps(struct ubifs_info *c, int cnt) in layout_in_gaps() argument
362 c->gap_lebs = kmalloc_array(c->lst.idx_lebs + 1, sizeof(int), in layout_in_gaps()
364 if (!c->gap_lebs) in layout_in_gaps()
365 return -ENOMEM; in layout_in_gaps()
367 old_idx_lebs = c->lst.idx_lebs; in layout_in_gaps()
369 ubifs_assert(c, p < c->lst.idx_lebs); in layout_in_gaps()
370 written = layout_leb_in_gaps(c, p); in layout_in_gaps()
373 if (err != -ENOSPC) { in layout_in_gaps()
374 kfree(c->gap_lebs); in layout_in_gaps()
375 c->gap_lebs = NULL; in layout_in_gaps()
378 if (!dbg_is_chk_index(c)) { in layout_in_gaps()
381 * option which forces in-the-gaps is enabled. in layout_in_gaps()
383 ubifs_warn(c, "out of space"); in layout_in_gaps()
384 ubifs_dump_budg(c, &c->bi); in layout_in_gaps()
385 ubifs_dump_lprops(c); in layout_in_gaps()
391 cnt -= written; in layout_in_gaps()
392 leb_needed_cnt = get_leb_cnt(c, cnt); in layout_in_gaps()
394 leb_needed_cnt, c->ileb_cnt); in layout_in_gaps()
396 * Dynamically change the size of @c->gap_lebs to prevent in layout_in_gaps()
397 * oob, because @c->lst.idx_lebs could be increased by in layout_in_gaps()
398 * function @get_idx_gc_leb (called by layout_leb_in_gaps-> in layout_in_gaps()
400 * @c->gap_lebs when needed. in layout_in_gaps()
403 if (leb_needed_cnt > c->ileb_cnt && p >= old_idx_lebs && in layout_in_gaps()
404 old_idx_lebs < c->lst.idx_lebs) { in layout_in_gaps()
405 old_idx_lebs = c->lst.idx_lebs; in layout_in_gaps()
406 gap_lebs = krealloc(c->gap_lebs, sizeof(int) * in layout_in_gaps()
409 kfree(c->gap_lebs); in layout_in_gaps()
410 c->gap_lebs = NULL; in layout_in_gaps()
411 return -ENOMEM; in layout_in_gaps()
413 c->gap_lebs = gap_lebs; in layout_in_gaps()
415 } while (leb_needed_cnt > c->ileb_cnt); in layout_in_gaps()
417 c->gap_lebs[p] = -1; in layout_in_gaps()
422 * layout_in_empty_space - layout index nodes in empty space.
423 * @c: UBIFS file-system description object
429 static int layout_in_empty_space(struct ubifs_info *c) in layout_in_empty_space() argument
435 cnext = c->enext; in layout_in_empty_space()
439 lnum = c->ihead_lnum; in layout_in_empty_space()
440 buf_offs = c->ihead_offs; in layout_in_empty_space()
442 buf_len = ubifs_idx_node_sz(c, c->fanout); in layout_in_empty_space()
443 buf_len = ALIGN(buf_len, c->min_io_size); in layout_in_empty_space()
448 next_len = ubifs_idx_node_sz(c, cnext->child_cnt); in layout_in_empty_space()
449 if (buf_offs + next_len > c->leb_size) in layout_in_empty_space()
450 lnum = -1; in layout_in_empty_space()
455 len = ubifs_idx_node_sz(c, znode->child_cnt); in layout_in_empty_space()
458 if (lnum == -1) { in layout_in_empty_space()
459 if (c->ileb_nxt >= c->ileb_cnt) { in layout_in_empty_space()
460 ubifs_err(c, "out of space"); in layout_in_empty_space()
461 return -ENOSPC; in layout_in_empty_space()
463 lnum = c->ilebs[c->ileb_nxt++]; in layout_in_empty_space()
471 znode->lnum = lnum; in layout_in_empty_space()
472 znode->offs = offs; in layout_in_empty_space()
473 znode->len = len; in layout_in_empty_space()
476 zp = znode->parent; in layout_in_empty_space()
481 i = znode->iip; in layout_in_empty_space()
482 zbr = &zp->zbranch[i]; in layout_in_empty_space()
483 zbr->lnum = lnum; in layout_in_empty_space()
484 zbr->offs = offs; in layout_in_empty_space()
485 zbr->len = len; in layout_in_empty_space()
487 c->zroot.lnum = lnum; in layout_in_empty_space()
488 c->zroot.offs = offs; in layout_in_empty_space()
489 c->zroot.len = len; in layout_in_empty_space()
491 c->calc_idx_sz += ALIGN(len, 8); in layout_in_empty_space()
497 atomic_long_dec(&c->dirty_zn_cnt); in layout_in_empty_space()
503 cnext = znode->cnext; in layout_in_empty_space()
504 if (cnext == c->cnext) in layout_in_empty_space()
507 next_len = ubifs_idx_node_sz(c, cnext->child_cnt); in layout_in_empty_space()
512 avail -= ALIGN(len, 8); in layout_in_empty_space()
515 buf_offs + used + next_len <= c->leb_size && in layout_in_empty_space()
520 buf_offs + used + next_len <= c->leb_size) in layout_in_empty_space()
523 blen = ALIGN(wlen, c->min_io_size); in layout_in_empty_space()
528 if (buf_offs + next_len > c->leb_size) { in layout_in_empty_space()
529 err = ubifs_update_one_lp(c, lnum, in layout_in_empty_space()
530 c->leb_size - buf_offs, blen - used, in layout_in_empty_space()
534 lnum = -1; in layout_in_empty_space()
536 used -= blen; in layout_in_empty_space()
539 avail = buf_len - used; in layout_in_empty_space()
542 err = ubifs_update_one_lp(c, lnum, c->leb_size - buf_offs, in layout_in_empty_space()
543 blen - used, 0, 0); in layout_in_empty_space()
549 c->dbg->new_ihead_lnum = lnum; in layout_in_empty_space()
550 c->dbg->new_ihead_offs = buf_offs; in layout_in_empty_space()
556 * layout_commit - determine positions of index nodes to commit.
557 * @c: UBIFS file-system description object
563 * into the gaps created by obsolete index nodes in non-empty index LEBs. For
565 * the end of the last commit. To write "in-the-gaps" requires that those index
566 * LEBs are updated atomically in-place.
568 static int layout_commit(struct ubifs_info *c, int no_space, int cnt) in layout_commit() argument
573 err = layout_in_gaps(c, cnt); in layout_commit()
577 err = layout_in_empty_space(c); in layout_commit()
582 * find_first_dirty - find first dirty znode.
593 if (znode->level == 0) { in find_first_dirty()
599 for (i = 0; i < znode->child_cnt; i++) { in find_first_dirty()
600 struct ubifs_zbranch *zbr = &znode->zbranch[i]; in find_first_dirty()
602 if (zbr->znode && ubifs_zn_dirty(zbr->znode)) { in find_first_dirty()
603 znode = zbr->znode; in find_first_dirty()
617 * find_next_dirty - find next dirty znode.
622 int n = znode->iip + 1; in find_next_dirty()
624 znode = znode->parent; in find_next_dirty()
627 for (; n < znode->child_cnt; n++) { in find_next_dirty()
628 struct ubifs_zbranch *zbr = &znode->zbranch[n]; in find_next_dirty()
630 if (zbr->znode && ubifs_zn_dirty(zbr->znode)) in find_next_dirty()
631 return find_first_dirty(zbr->znode); in find_next_dirty()
637 * get_znodes_to_commit - create list of dirty znodes to commit.
638 * @c: UBIFS file-system description object
642 static int get_znodes_to_commit(struct ubifs_info *c) in get_znodes_to_commit() argument
647 c->cnext = find_first_dirty(c->zroot.znode); in get_znodes_to_commit()
648 znode = c->enext = c->cnext; in get_znodes_to_commit()
655 ubifs_assert(c, !ubifs_zn_cow(znode)); in get_znodes_to_commit()
656 __set_bit(COW_ZNODE, &znode->flags); in get_znodes_to_commit()
657 znode->alt = 0; in get_znodes_to_commit()
660 ubifs_assert(c, !znode->parent); in get_znodes_to_commit()
661 znode->cparent = NULL; in get_znodes_to_commit()
662 znode->cnext = c->cnext; in get_znodes_to_commit()
665 znode->cparent = znode->parent; in get_znodes_to_commit()
666 znode->ciip = znode->iip; in get_znodes_to_commit()
667 znode->cnext = cnext; in get_znodes_to_commit()
672 ubifs_assert(c, cnt == atomic_long_read(&c->dirty_zn_cnt)); in get_znodes_to_commit()
677 * alloc_idx_lebs - allocate empty LEBs to be used to commit.
678 * @c: UBIFS file-system description object
681 * This function returns %-ENOSPC if it cannot allocate a sufficient number of
685 static int alloc_idx_lebs(struct ubifs_info *c, int cnt) in alloc_idx_lebs() argument
689 c->ileb_cnt = 0; in alloc_idx_lebs()
690 c->ileb_nxt = 0; in alloc_idx_lebs()
691 leb_cnt = get_leb_cnt(c, cnt); in alloc_idx_lebs()
695 c->ilebs = kmalloc_array(leb_cnt, sizeof(int), GFP_NOFS); in alloc_idx_lebs()
696 if (!c->ilebs) in alloc_idx_lebs()
697 return -ENOMEM; in alloc_idx_lebs()
699 lnum = ubifs_find_free_leb_for_idx(c); in alloc_idx_lebs()
702 c->ilebs[c->ileb_cnt++] = lnum; in alloc_idx_lebs()
705 if (dbg_is_chk_index(c) && !get_random_u32_below(8)) in alloc_idx_lebs()
706 return -ENOSPC; in alloc_idx_lebs()
711 * free_unused_idx_lebs - free unused LEBs that were allocated for the commit.
712 * @c: UBIFS file-system description object
719 static int free_unused_idx_lebs(struct ubifs_info *c) in free_unused_idx_lebs() argument
723 for (i = c->ileb_nxt; i < c->ileb_cnt; i++) { in free_unused_idx_lebs()
724 lnum = c->ilebs[i]; in free_unused_idx_lebs()
726 er = ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0, in free_unused_idx_lebs()
735 * free_idx_lebs - free unused LEBs after commit end.
736 * @c: UBIFS file-system description object
740 static int free_idx_lebs(struct ubifs_info *c) in free_idx_lebs() argument
744 err = free_unused_idx_lebs(c); in free_idx_lebs()
745 kfree(c->ilebs); in free_idx_lebs()
746 c->ilebs = NULL; in free_idx_lebs()
751 * ubifs_tnc_start_commit - start TNC commit.
752 * @c: UBIFS file-system description object
757 * in-gap commit method. Returns zero in case of success and a negative error
760 int ubifs_tnc_start_commit(struct ubifs_info *c, struct ubifs_zbranch *zroot) in ubifs_tnc_start_commit() argument
764 mutex_lock(&c->tnc_mutex); in ubifs_tnc_start_commit()
765 err = dbg_check_tnc(c, 1); in ubifs_tnc_start_commit()
768 cnt = get_znodes_to_commit(c); in ubifs_tnc_start_commit()
772 err = alloc_idx_lebs(c, cnt); in ubifs_tnc_start_commit()
773 if (err == -ENOSPC) in ubifs_tnc_start_commit()
777 err = layout_commit(c, no_space, cnt); in ubifs_tnc_start_commit()
780 ubifs_assert(c, atomic_long_read(&c->dirty_zn_cnt) == 0); in ubifs_tnc_start_commit()
781 err = free_unused_idx_lebs(c); in ubifs_tnc_start_commit()
785 destroy_old_idx(c); in ubifs_tnc_start_commit()
786 memcpy(zroot, &c->zroot, sizeof(struct ubifs_zbranch)); in ubifs_tnc_start_commit()
788 err = ubifs_save_dirty_idx_lnums(c); in ubifs_tnc_start_commit()
792 spin_lock(&c->space_lock); in ubifs_tnc_start_commit()
795 * committed index ('c->bi.old_idx_sz') and zero out the index growth in ubifs_tnc_start_commit()
801 ubifs_assert(c, c->bi.min_idx_lebs == ubifs_calc_min_idx_lebs(c)); in ubifs_tnc_start_commit()
802 c->bi.old_idx_sz = c->calc_idx_sz; in ubifs_tnc_start_commit()
803 c->bi.uncommitted_idx = 0; in ubifs_tnc_start_commit()
804 c->bi.min_idx_lebs = ubifs_calc_min_idx_lebs(c); in ubifs_tnc_start_commit()
805 spin_unlock(&c->space_lock); in ubifs_tnc_start_commit()
806 mutex_unlock(&c->tnc_mutex); in ubifs_tnc_start_commit()
808 dbg_cmt("number of index LEBs %d", c->lst.idx_lebs); in ubifs_tnc_start_commit()
809 dbg_cmt("size of index %llu", c->calc_idx_sz); in ubifs_tnc_start_commit()
813 free_idx_lebs(c); in ubifs_tnc_start_commit()
815 mutex_unlock(&c->tnc_mutex); in ubifs_tnc_start_commit()
820 * write_index - write index nodes.
821 * @c: UBIFS file-system description object
826 static int write_index(struct ubifs_info *c) in write_index() argument
833 cnext = c->enext; in write_index()
841 lnum = c->ihead_lnum; in write_index()
842 buf_offs = c->ihead_offs; in write_index()
845 buf_len = ALIGN(c->max_idx_node_sz, c->min_io_size); in write_index()
850 next_len = ubifs_idx_node_sz(c, cnext->child_cnt); in write_index()
851 if (buf_offs + next_len > c->leb_size) { in write_index()
852 err = ubifs_update_one_lp(c, lnum, LPROPS_NC, 0, 0, in write_index()
856 lnum = -1; in write_index()
865 idx = c->cbuf + used; in write_index()
868 idx->ch.node_type = UBIFS_IDX_NODE; in write_index()
869 idx->child_cnt = cpu_to_le16(znode->child_cnt); in write_index()
870 idx->level = cpu_to_le16(znode->level); in write_index()
871 for (i = 0; i < znode->child_cnt; i++) { in write_index()
872 struct ubifs_branch *br = ubifs_idx_branch(c, idx, i); in write_index()
873 struct ubifs_zbranch *zbr = &znode->zbranch[i]; in write_index()
875 key_write_idx(c, &zbr->key, &br->key); in write_index()
876 br->lnum = cpu_to_le32(zbr->lnum); in write_index()
877 br->offs = cpu_to_le32(zbr->offs); in write_index()
878 br->len = cpu_to_le32(zbr->len); in write_index()
879 ubifs_copy_hash(c, zbr->hash, ubifs_branch_hash(c, br)); in write_index()
880 if (!zbr->lnum || !zbr->len) { in write_index()
881 ubifs_err(c, "bad ref in znode"); in write_index()
882 ubifs_dump_znode(c, znode); in write_index()
883 if (zbr->znode) in write_index()
884 ubifs_dump_znode(c, zbr->znode); in write_index()
886 return -EINVAL; in write_index()
889 len = ubifs_idx_node_sz(c, znode->child_cnt); in write_index()
890 ubifs_prepare_node(c, idx, len, 0); in write_index()
891 ubifs_node_calc_hash(c, idx, hash); in write_index()
893 mutex_lock(&c->tnc_mutex); in write_index()
895 if (znode->cparent) in write_index()
896 ubifs_copy_hash(c, hash, in write_index()
897 znode->cparent->zbranch[znode->ciip].hash); in write_index()
899 if (znode->parent) { in write_index()
901 ubifs_copy_hash(c, hash, in write_index()
902 znode->parent->zbranch[znode->iip].hash); in write_index()
904 ubifs_copy_hash(c, hash, c->zroot.hash); in write_index()
907 mutex_unlock(&c->tnc_mutex); in write_index()
910 if (lnum == -1) { in write_index()
911 lnum = c->ilebs[lnum_pos++]; in write_index()
918 if (lnum != znode->lnum || offs != znode->offs || in write_index()
919 len != znode->len) { in write_index()
920 ubifs_err(c, "inconsistent znode posn"); in write_index()
921 return -EINVAL; in write_index()
925 cnext = znode->cnext; in write_index()
927 ubifs_assert(c, ubifs_zn_dirty(znode)); in write_index()
928 ubifs_assert(c, ubifs_zn_cow(znode)); in write_index()
938 clear_bit(DIRTY_ZNODE, &znode->flags); in write_index()
940 clear_bit(COW_ZNODE, &znode->flags); in write_index()
945 * @c->clean_zn_cnt counter. If this znode becomes dirty again in write_index()
947 * @c->clean_zn_cnt will be decremented before it gets in write_index()
949 * This means that @c->clean_zn_cnt may become negative for a in write_index()
952 * Q: why we cannot increment @c->clean_zn_cnt? in write_index()
953 * A: because we do not have the @c->tnc_mutex locked, and the in write_index()
957 * atomic_long_inc(&c->clean_zn_cnt); in write_index()
961 * Thus, we just delay the @c->clean_zn_cnt update until we in write_index()
970 avail -= ALIGN(len, 8); in write_index()
976 if (cnext == c->cnext) in write_index()
979 next_len = ubifs_idx_node_sz(c, cnext->child_cnt); in write_index()
982 if (next_len && nxt_offs <= c->leb_size) { in write_index()
989 blen = ALIGN(wlen, c->min_io_size); in write_index()
990 ubifs_pad(c, c->cbuf + wlen, blen - wlen); in write_index()
994 err = ubifs_leb_write(c, lnum, c->cbuf, buf_offs, blen); in write_index()
999 if (nxt_offs > c->leb_size) { in write_index()
1000 err = ubifs_update_one_lp(c, lnum, LPROPS_NC, 0, in write_index()
1004 lnum = -1; in write_index()
1006 used -= blen; in write_index()
1009 avail = buf_len - used; in write_index()
1010 memmove(c->cbuf, c->cbuf + blen, used); in write_index()
1016 if (lnum != c->dbg->new_ihead_lnum || in write_index()
1017 buf_offs != c->dbg->new_ihead_offs) { in write_index()
1018 ubifs_err(c, "inconsistent ihead"); in write_index()
1019 return -EINVAL; in write_index()
1022 c->ihead_lnum = lnum; in write_index()
1023 c->ihead_offs = buf_offs; in write_index()
1029 * free_obsolete_znodes - free obsolete znodes.
1030 * @c: UBIFS file-system description object
1034 static void free_obsolete_znodes(struct ubifs_info *c) in free_obsolete_znodes() argument
1038 cnext = c->cnext; in free_obsolete_znodes()
1041 cnext = znode->cnext; in free_obsolete_znodes()
1045 znode->cnext = NULL; in free_obsolete_znodes()
1046 atomic_long_inc(&c->clean_zn_cnt); in free_obsolete_znodes()
1049 } while (cnext != c->cnext); in free_obsolete_znodes()
1053 * return_gap_lebs - return LEBs used by the in-gap commit method.
1054 * @c: UBIFS file-system description object
1057 * "commit in-the-gaps" method.
1059 static int return_gap_lebs(struct ubifs_info *c) in return_gap_lebs() argument
1063 if (!c->gap_lebs) in return_gap_lebs()
1067 for (p = c->gap_lebs; *p != -1; p++) { in return_gap_lebs()
1068 err = ubifs_change_one_lp(c, *p, LPROPS_NC, LPROPS_NC, 0, in return_gap_lebs()
1074 kfree(c->gap_lebs); in return_gap_lebs()
1075 c->gap_lebs = NULL; in return_gap_lebs()
1080 * ubifs_tnc_end_commit - update the TNC for commit end.
1081 * @c: UBIFS file-system description object
1085 int ubifs_tnc_end_commit(struct ubifs_info *c) in ubifs_tnc_end_commit() argument
1089 if (!c->cnext) in ubifs_tnc_end_commit()
1092 err = return_gap_lebs(c); in ubifs_tnc_end_commit()
1096 err = write_index(c); in ubifs_tnc_end_commit()
1100 mutex_lock(&c->tnc_mutex); in ubifs_tnc_end_commit()
1102 dbg_cmt("TNC height is %d", c->zroot.znode->level + 1); in ubifs_tnc_end_commit()
1104 free_obsolete_znodes(c); in ubifs_tnc_end_commit()
1106 c->cnext = NULL; in ubifs_tnc_end_commit()
1107 kfree(c->ilebs); in ubifs_tnc_end_commit()
1108 c->ilebs = NULL; in ubifs_tnc_end_commit()
1110 mutex_unlock(&c->tnc_mutex); in ubifs_tnc_end_commit()