1 // SPDX-License-Identifier: GPL-2.0-only
3 * This file is part of UBIFS.
5 * Copyright (C) 2006-2008 Nokia Corporation.
7 * Authors: Adrian Hunter
8 * Artem Bityutskiy (Битюцкий Артём)
11 /* This file implements TNC functions for committing */
13 #include <linux/random.h>
17 * make_idx_node - make an index node for fill-the-gaps method of TNC commit.
18 * @c: UBIFS file-system description object
19 * @idx: buffer in which to place new index node
20 * @znode: znode from which to make new index node
21 * @lnum: LEB number where new index node will be written
22 * @offs: offset where new index node will be written
23 * @len: length of new index node
25 static int make_idx_node(struct ubifs_info *c, struct ubifs_idx_node *idx,
26 struct ubifs_znode *znode, int lnum, int offs, int len)
28 struct ubifs_znode *zp;
29 u8 hash[UBIFS_HASH_ARR_SZ];
33 idx->ch.node_type = UBIFS_IDX_NODE;
34 idx->child_cnt = cpu_to_le16(znode->child_cnt);
35 idx->level = cpu_to_le16(znode->level);
36 for (i = 0; i < znode->child_cnt; i++) {
37 struct ubifs_branch *br = ubifs_idx_branch(c, idx, i);
38 struct ubifs_zbranch *zbr = &znode->zbranch[i];
40 key_write_idx(c, &zbr->key, &br->key);
41 br->lnum = cpu_to_le32(zbr->lnum);
42 br->offs = cpu_to_le32(zbr->offs);
43 br->len = cpu_to_le32(zbr->len);
44 ubifs_copy_hash(c, zbr->hash, ubifs_branch_hash(c, br));
45 if (!zbr->lnum || !zbr->len) {
46 ubifs_err(c, "bad ref in znode");
47 ubifs_dump_znode(c, znode);
49 ubifs_dump_znode(c, zbr->znode);
54 ubifs_prepare_node(c, idx, len, 0);
55 ubifs_node_calc_hash(c, idx, hash);
61 err = insert_old_idx_znode(c, znode);
63 /* Update the parent */
66 struct ubifs_zbranch *zbr;
68 zbr = &zp->zbranch[znode->iip];
72 ubifs_copy_hash(c, hash, zbr->hash);
77 ubifs_copy_hash(c, hash, c->zroot.hash);
79 c->calc_idx_sz += ALIGN(len, 8);
81 atomic_long_dec(&c->dirty_zn_cnt);
83 ubifs_assert(c, ubifs_zn_dirty(znode));
84 ubifs_assert(c, ubifs_zn_cow(znode));
87 * Note, unlike 'write_index()' we do not add memory barriers here
88 * because this function is called with @c->tnc_mutex locked.
90 __clear_bit(DIRTY_ZNODE, &znode->flags);
91 __clear_bit(COW_ZNODE, &znode->flags);
97 * fill_gap - make index nodes in gaps in dirty index LEBs.
98 * @c: UBIFS file-system description object
99 * @lnum: LEB number that gap appears in
100 * @gap_start: offset of start of gap
101 * @gap_end: offset of end of gap
102 * @dirt: adds dirty space to this
104 * This function returns the number of index nodes written into the gap.
106 static int fill_gap(struct ubifs_info *c, int lnum, int gap_start, int gap_end,
109 int len, gap_remains, gap_pos, written, pad_len;
111 ubifs_assert(c, (gap_start & 7) == 0);
112 ubifs_assert(c, (gap_end & 7) == 0);
113 ubifs_assert(c, gap_end >= gap_start);
115 gap_remains = gap_end - gap_start;
121 len = ubifs_idx_node_sz(c, c->enext->child_cnt);
122 if (len < gap_remains) {
123 struct ubifs_znode *znode = c->enext;
124 const int alen = ALIGN(len, 8);
127 ubifs_assert(c, alen <= gap_remains);
128 err = make_idx_node(c, c->ileb_buf + gap_pos, znode,
134 c->enext = znode->cnext;
135 if (c->enext == c->cnext)
141 if (gap_end == c->leb_size) {
142 c->ileb_len = ALIGN(gap_pos, c->min_io_size);
143 /* Pad to end of min_io_size */
144 pad_len = c->ileb_len - gap_pos;
146 /* Pad to end of gap */
147 pad_len = gap_remains;
148 dbg_gc("LEB %d:%d to %d len %d nodes written %d wasted bytes %d",
149 lnum, gap_start, gap_end, gap_end - gap_start, written, pad_len);
150 ubifs_pad(c, c->ileb_buf + gap_pos, pad_len);
156 * find_old_idx - find an index node obsoleted since the last commit start.
157 * @c: UBIFS file-system description object
158 * @lnum: LEB number of obsoleted index node
159 * @offs: offset of obsoleted index node
161 * Returns %1 if found and %0 otherwise.
163 static int find_old_idx(struct ubifs_info *c, int lnum, int offs)
165 struct ubifs_old_idx *o;
168 p = c->old_idx.rb_node;
170 o = rb_entry(p, struct ubifs_old_idx, rb);
173 else if (lnum > o->lnum)
175 else if (offs < o->offs)
177 else if (offs > o->offs)
186 * is_idx_node_in_use - determine if an index node can be overwritten.
187 * @c: UBIFS file-system description object
188 * @key: key of index node
189 * @level: index node level
190 * @lnum: LEB number of index node
191 * @offs: offset of index node
193 * If @key / @lnum / @offs identify an index node that was not part of the old
194 * index, then this function returns %0 (obsolete). Else if the index node was
195 * part of the old index but is now dirty %1 is returned, else if it is clean %2
196 * is returned. A negative error code is returned on failure.
198 static int is_idx_node_in_use(struct ubifs_info *c, union ubifs_key *key,
199 int level, int lnum, int offs)
203 ret = is_idx_node_in_tnc(c, key, level, lnum, offs);
205 return ret; /* Error code */
207 if (find_old_idx(c, lnum, offs))
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
218 * method of TNC commit.
219 * This function merely puts the next znode into the next gap, making no attempt
220 * to try to maximise the number of znodes that fit.
221 * This function returns the number of index nodes written into the gaps, or a
222 * negative error code on failure.
224 static int layout_leb_in_gaps(struct ubifs_info *c, int p)
226 struct ubifs_scan_leb *sleb;
227 struct ubifs_scan_node *snod;
228 int lnum, dirt = 0, gap_start, gap_end, err, written, tot_written;
231 /* Get an index LEB with lots of obsolete index nodes */
232 lnum = ubifs_find_dirty_idx_leb(c);
235 * There also may be dirt in the index head that could be
236 * filled, however we do not check there at present.
238 return lnum; /* Error code */
239 c->gap_lebs[p] = lnum;
240 dbg_gc("LEB %d", lnum);
242 * Scan the index LEB. We use the generic scan for this even though
243 * it is more comprehensive and less efficient than is needed for this
246 sleb = ubifs_scan(c, lnum, 0, c->ileb_buf, 0);
249 return PTR_ERR(sleb);
251 list_for_each_entry(snod, &sleb->nodes, list) {
252 struct ubifs_idx_node *idx;
255 ubifs_assert(c, snod->type == UBIFS_IDX_NODE);
257 key_read(c, ubifs_idx_key(c, idx), &snod->key);
258 level = le16_to_cpu(idx->level);
259 /* Determine if the index node is in use (not obsolete) */
260 in_use = is_idx_node_in_use(c, &snod->key, level, lnum,
263 ubifs_scan_destroy(sleb);
264 return in_use; /* Error code */
268 dirt += ALIGN(snod->len, 8);
270 * The obsolete index nodes form gaps that can be
271 * overwritten. This gap has ended because we have
272 * found an index node that is still in use
275 gap_end = snod->offs;
276 /* Try to fill gap */
277 written = fill_gap(c, lnum, gap_start, gap_end, &dirt);
279 ubifs_scan_destroy(sleb);
280 return written; /* Error code */
282 tot_written += written;
283 gap_start = ALIGN(snod->offs + snod->len, 8);
286 ubifs_scan_destroy(sleb);
287 c->ileb_len = c->leb_size;
288 gap_end = c->leb_size;
289 /* Try to fill gap */
290 written = fill_gap(c, lnum, gap_start, gap_end, &dirt);
292 return written; /* Error code */
293 tot_written += written;
294 if (tot_written == 0) {
295 struct ubifs_lprops lp;
297 dbg_gc("LEB %d wrote %d index nodes", lnum, tot_written);
298 err = ubifs_read_one_lp(c, lnum, &lp);
301 if (lp.free == c->leb_size) {
303 * We must have snatched this LEB from the idx_gc list
304 * so we need to correct the free and dirty space.
306 err = ubifs_change_one_lp(c, lnum,
307 c->leb_size - c->ileb_len,
314 err = ubifs_change_one_lp(c, lnum, c->leb_size - c->ileb_len, dirt,
318 err = ubifs_leb_change(c, lnum, c->ileb_buf, c->ileb_len);
321 dbg_gc("LEB %d wrote %d index nodes", lnum, tot_written);
326 * get_leb_cnt - calculate the number of empty LEBs needed to commit.
327 * @c: UBIFS file-system description object
328 * @cnt: number of znodes to commit
330 * This function returns the number of empty LEBs needed to commit @cnt znodes
331 * to the current index head. The number is not exact and may be more than
334 static int get_leb_cnt(struct ubifs_info *c, int cnt)
338 /* Assume maximum index node size (i.e. overestimate space needed) */
339 cnt -= (c->leb_size - c->ihead_offs) / c->max_idx_node_sz;
342 d = c->leb_size / c->max_idx_node_sz;
343 return DIV_ROUND_UP(cnt, d);
347 * layout_in_gaps - in-the-gaps method of committing TNC.
348 * @c: UBIFS file-system description object
349 * @cnt: number of dirty znodes to commit.
351 * This function lays out new index nodes for dirty znodes using in-the-gaps
352 * method of TNC commit.
354 * This function returns %0 on success and a negative error code on failure.
356 static int layout_in_gaps(struct ubifs_info *c, int cnt)
358 int err, leb_needed_cnt, written, p = 0, old_idx_lebs, *gap_lebs;
360 dbg_gc("%d znodes to write", cnt);
362 c->gap_lebs = kmalloc_array(c->lst.idx_lebs + 1, sizeof(int),
367 old_idx_lebs = c->lst.idx_lebs;
369 ubifs_assert(c, p < c->lst.idx_lebs);
370 written = layout_leb_in_gaps(c, p);
373 if (err != -ENOSPC) {
378 if (!dbg_is_chk_index(c)) {
380 * Do not print scary warnings if the debugging
381 * option which forces in-the-gaps is enabled.
383 ubifs_warn(c, "out of space");
384 ubifs_dump_budg(c, &c->bi);
385 ubifs_dump_lprops(c);
387 /* Try to commit anyway */
392 leb_needed_cnt = get_leb_cnt(c, cnt);
393 dbg_gc("%d znodes remaining, need %d LEBs, have %d", cnt,
394 leb_needed_cnt, c->ileb_cnt);
396 * Dynamically change the size of @c->gap_lebs to prevent
397 * oob, because @c->lst.idx_lebs could be increased by
398 * function @get_idx_gc_leb (called by layout_leb_in_gaps->
399 * ubifs_find_dirty_idx_leb) during loop. Only enlarge
400 * @c->gap_lebs when needed.
403 if (leb_needed_cnt > c->ileb_cnt && p >= old_idx_lebs &&
404 old_idx_lebs < c->lst.idx_lebs) {
405 old_idx_lebs = c->lst.idx_lebs;
406 gap_lebs = krealloc(c->gap_lebs, sizeof(int) *
407 (old_idx_lebs + 1), GFP_NOFS);
413 c->gap_lebs = gap_lebs;
415 } while (leb_needed_cnt > c->ileb_cnt);
422 * layout_in_empty_space - layout index nodes in empty space.
423 * @c: UBIFS file-system description object
425 * This function lays out new index nodes for dirty znodes using empty LEBs.
427 * This function returns %0 on success and a negative error code on failure.
429 static int layout_in_empty_space(struct ubifs_info *c)
431 struct ubifs_znode *znode, *cnext, *zp;
432 int lnum, offs, len, next_len, buf_len, buf_offs, used, avail;
439 lnum = c->ihead_lnum;
440 buf_offs = c->ihead_offs;
442 buf_len = ubifs_idx_node_sz(c, c->fanout);
443 buf_len = ALIGN(buf_len, c->min_io_size);
447 /* Ensure there is enough room for first write */
448 next_len = ubifs_idx_node_sz(c, cnext->child_cnt);
449 if (buf_offs + next_len > c->leb_size)
455 len = ubifs_idx_node_sz(c, znode->child_cnt);
457 /* Determine the index node position */
459 if (c->ileb_nxt >= c->ileb_cnt) {
460 ubifs_err(c, "out of space");
463 lnum = c->ilebs[c->ileb_nxt++];
469 offs = buf_offs + used;
475 /* Update the parent */
478 struct ubifs_zbranch *zbr;
482 zbr = &zp->zbranch[i];
487 c->zroot.lnum = lnum;
488 c->zroot.offs = offs;
491 c->calc_idx_sz += ALIGN(len, 8);
494 * Once lprops is updated, we can decrease the dirty znode count
495 * but it is easier to just do it here.
497 atomic_long_dec(&c->dirty_zn_cnt);
500 * Calculate the next index node length to see if there is
503 cnext = znode->cnext;
504 if (cnext == c->cnext)
507 next_len = ubifs_idx_node_sz(c, cnext->child_cnt);
509 /* Update buffer positions */
511 used += ALIGN(len, 8);
512 avail -= ALIGN(len, 8);
515 buf_offs + used + next_len <= c->leb_size &&
519 if (avail <= 0 && next_len &&
520 buf_offs + used + next_len <= c->leb_size)
523 blen = ALIGN(wlen, c->min_io_size);
525 /* The buffer is full or there are no more znodes to do */
528 if (buf_offs + next_len > c->leb_size) {
529 err = ubifs_update_one_lp(c, lnum,
530 c->leb_size - buf_offs, blen - used,
539 avail = buf_len - used;
542 err = ubifs_update_one_lp(c, lnum, c->leb_size - buf_offs,
549 c->dbg->new_ihead_lnum = lnum;
550 c->dbg->new_ihead_offs = buf_offs;
556 * layout_commit - determine positions of index nodes to commit.
557 * @c: UBIFS file-system description object
558 * @no_space: indicates that insufficient empty LEBs were allocated
559 * @cnt: number of znodes to commit
561 * Calculate and update the positions of index nodes to commit. If there were
562 * an insufficient number of empty LEBs allocated, then index nodes are placed
563 * into the gaps created by obsolete index nodes in non-empty index LEBs. For
564 * this purpose, an obsolete index node is one that was not in the index as at
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)
573 err = layout_in_gaps(c, cnt);
577 err = layout_in_empty_space(c);
582 * find_first_dirty - find first dirty znode.
583 * @znode: znode to begin searching from
585 static struct ubifs_znode *find_first_dirty(struct ubifs_znode *znode)
593 if (znode->level == 0) {
594 if (ubifs_zn_dirty(znode))
599 for (i = 0; i < znode->child_cnt; i++) {
600 struct ubifs_zbranch *zbr = &znode->zbranch[i];
602 if (zbr->znode && ubifs_zn_dirty(zbr->znode)) {
609 if (ubifs_zn_dirty(znode))
617 * find_next_dirty - find next dirty znode.
618 * @znode: znode to begin searching from
620 static struct ubifs_znode *find_next_dirty(struct ubifs_znode *znode)
622 int n = znode->iip + 1;
624 znode = znode->parent;
627 for (; n < znode->child_cnt; n++) {
628 struct ubifs_zbranch *zbr = &znode->zbranch[n];
630 if (zbr->znode && ubifs_zn_dirty(zbr->znode))
631 return find_first_dirty(zbr->znode);
637 * get_znodes_to_commit - create list of dirty znodes to commit.
638 * @c: UBIFS file-system description object
640 * This function returns the number of znodes to commit.
642 static int get_znodes_to_commit(struct ubifs_info *c)
644 struct ubifs_znode *znode, *cnext;
647 c->cnext = find_first_dirty(c->zroot.znode);
648 znode = c->enext = c->cnext;
650 dbg_cmt("no znodes to commit");
655 ubifs_assert(c, !ubifs_zn_cow(znode));
656 __set_bit(COW_ZNODE, &znode->flags);
658 cnext = find_next_dirty(znode);
660 znode->cnext = c->cnext;
663 znode->cparent = znode->parent;
664 znode->ciip = znode->iip;
665 znode->cnext = cnext;
669 dbg_cmt("committing %d znodes", cnt);
670 ubifs_assert(c, cnt == atomic_long_read(&c->dirty_zn_cnt));
675 * alloc_idx_lebs - allocate empty LEBs to be used to commit.
676 * @c: UBIFS file-system description object
677 * @cnt: number of znodes to commit
679 * This function returns %-ENOSPC if it cannot allocate a sufficient number of
680 * empty LEBs. %0 is returned on success, otherwise a negative error code
683 static int alloc_idx_lebs(struct ubifs_info *c, int cnt)
685 int i, leb_cnt, lnum;
689 leb_cnt = get_leb_cnt(c, cnt);
690 dbg_cmt("need about %d empty LEBS for TNC commit", leb_cnt);
693 c->ilebs = kmalloc_array(leb_cnt, sizeof(int), GFP_NOFS);
696 for (i = 0; i < leb_cnt; i++) {
697 lnum = ubifs_find_free_leb_for_idx(c);
700 c->ilebs[c->ileb_cnt++] = lnum;
701 dbg_cmt("LEB %d", lnum);
703 if (dbg_is_chk_index(c) && !get_random_u32_below(8))
709 * free_unused_idx_lebs - free unused LEBs that were allocated for the commit.
710 * @c: UBIFS file-system description object
712 * It is possible that we allocate more empty LEBs for the commit than we need.
713 * This functions frees the surplus.
715 * This function returns %0 on success and a negative error code on failure.
717 static int free_unused_idx_lebs(struct ubifs_info *c)
719 int i, err = 0, lnum, er;
721 for (i = c->ileb_nxt; i < c->ileb_cnt; i++) {
723 dbg_cmt("LEB %d", lnum);
724 er = ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0,
725 LPROPS_INDEX | LPROPS_TAKEN, 0);
733 * free_idx_lebs - free unused LEBs after commit end.
734 * @c: UBIFS file-system description object
736 * This function returns %0 on success and a negative error code on failure.
738 static int free_idx_lebs(struct ubifs_info *c)
742 err = free_unused_idx_lebs(c);
749 * ubifs_tnc_start_commit - start TNC commit.
750 * @c: UBIFS file-system description object
751 * @zroot: new index root position is returned here
753 * This function prepares the list of indexing nodes to commit and lays out
754 * their positions on flash. If there is not enough free space it uses the
755 * in-gap commit method. Returns zero in case of success and a negative error
756 * code in case of failure.
758 int ubifs_tnc_start_commit(struct ubifs_info *c, struct ubifs_zbranch *zroot)
762 mutex_lock(&c->tnc_mutex);
763 err = dbg_check_tnc(c, 1);
766 cnt = get_znodes_to_commit(c);
770 err = alloc_idx_lebs(c, cnt);
775 err = layout_commit(c, no_space, cnt);
778 ubifs_assert(c, atomic_long_read(&c->dirty_zn_cnt) == 0);
779 err = free_unused_idx_lebs(c);
784 memcpy(zroot, &c->zroot, sizeof(struct ubifs_zbranch));
786 err = ubifs_save_dirty_idx_lnums(c);
790 spin_lock(&c->space_lock);
792 * Although we have not finished committing yet, update size of the
793 * committed index ('c->bi.old_idx_sz') and zero out the index growth
794 * budget. It is OK to do this now, because we've reserved all the
795 * space which is needed to commit the index, and it is save for the
796 * budgeting subsystem to assume the index is already committed,
797 * even though it is not.
799 ubifs_assert(c, c->bi.min_idx_lebs == ubifs_calc_min_idx_lebs(c));
800 c->bi.old_idx_sz = c->calc_idx_sz;
801 c->bi.uncommitted_idx = 0;
802 c->bi.min_idx_lebs = ubifs_calc_min_idx_lebs(c);
803 spin_unlock(&c->space_lock);
804 mutex_unlock(&c->tnc_mutex);
806 dbg_cmt("number of index LEBs %d", c->lst.idx_lebs);
807 dbg_cmt("size of index %llu", c->calc_idx_sz);
813 mutex_unlock(&c->tnc_mutex);
818 * write_index - write index nodes.
819 * @c: UBIFS file-system description object
821 * This function writes the index nodes whose positions were laid out in the
822 * layout_in_empty_space function.
824 static int write_index(struct ubifs_info *c)
826 struct ubifs_idx_node *idx;
827 struct ubifs_znode *znode, *cnext;
828 int i, lnum, offs, len, next_len, buf_len, buf_offs, used;
829 int avail, wlen, err, lnum_pos = 0, blen, nxt_offs;
836 * Always write index nodes to the index head so that index nodes and
837 * other types of nodes are never mixed in the same erase block.
839 lnum = c->ihead_lnum;
840 buf_offs = c->ihead_offs;
842 /* Allocate commit buffer */
843 buf_len = ALIGN(c->max_idx_node_sz, c->min_io_size);
847 /* Ensure there is enough room for first write */
848 next_len = ubifs_idx_node_sz(c, cnext->child_cnt);
849 if (buf_offs + next_len > c->leb_size) {
850 err = ubifs_update_one_lp(c, lnum, LPROPS_NC, 0, 0,
858 u8 hash[UBIFS_HASH_ARR_SZ];
863 idx = c->cbuf + used;
865 /* Make index node */
866 idx->ch.node_type = UBIFS_IDX_NODE;
867 idx->child_cnt = cpu_to_le16(znode->child_cnt);
868 idx->level = cpu_to_le16(znode->level);
869 for (i = 0; i < znode->child_cnt; i++) {
870 struct ubifs_branch *br = ubifs_idx_branch(c, idx, i);
871 struct ubifs_zbranch *zbr = &znode->zbranch[i];
873 key_write_idx(c, &zbr->key, &br->key);
874 br->lnum = cpu_to_le32(zbr->lnum);
875 br->offs = cpu_to_le32(zbr->offs);
876 br->len = cpu_to_le32(zbr->len);
877 ubifs_copy_hash(c, zbr->hash, ubifs_branch_hash(c, br));
878 if (!zbr->lnum || !zbr->len) {
879 ubifs_err(c, "bad ref in znode");
880 ubifs_dump_znode(c, znode);
882 ubifs_dump_znode(c, zbr->znode);
887 len = ubifs_idx_node_sz(c, znode->child_cnt);
888 ubifs_prepare_node(c, idx, len, 0);
889 ubifs_node_calc_hash(c, idx, hash);
891 mutex_lock(&c->tnc_mutex);
894 ubifs_copy_hash(c, hash,
895 znode->cparent->zbranch[znode->ciip].hash);
898 if (!ubifs_zn_obsolete(znode))
899 ubifs_copy_hash(c, hash,
900 znode->parent->zbranch[znode->iip].hash);
902 ubifs_copy_hash(c, hash, c->zroot.hash);
905 mutex_unlock(&c->tnc_mutex);
907 /* Determine the index node position */
909 lnum = c->ilebs[lnum_pos++];
914 offs = buf_offs + used;
916 if (lnum != znode->lnum || offs != znode->offs ||
918 ubifs_err(c, "inconsistent znode posn");
922 /* Grab some stuff from znode while we still can */
923 cnext = znode->cnext;
925 ubifs_assert(c, ubifs_zn_dirty(znode));
926 ubifs_assert(c, ubifs_zn_cow(znode));
929 * It is important that other threads should see %DIRTY_ZNODE
930 * flag cleared before %COW_ZNODE. Specifically, it matters in
931 * the 'dirty_cow_znode()' function. This is the reason for the
932 * first barrier. Also, we want the bit changes to be seen to
933 * other threads ASAP, to avoid unnecessary copying, which is
934 * the reason for the second barrier.
936 clear_bit(DIRTY_ZNODE, &znode->flags);
937 smp_mb__before_atomic();
938 clear_bit(COW_ZNODE, &znode->flags);
939 smp_mb__after_atomic();
942 * We have marked the znode as clean but have not updated the
943 * @c->clean_zn_cnt counter. If this znode becomes dirty again
944 * before 'free_obsolete_znodes()' is called, then
945 * @c->clean_zn_cnt will be decremented before it gets
946 * incremented (resulting in 2 decrements for the same znode).
947 * This means that @c->clean_zn_cnt may become negative for a
950 * Q: why we cannot increment @c->clean_zn_cnt?
951 * A: because we do not have the @c->tnc_mutex locked, and the
952 * following code would be racy and buggy:
954 * if (!ubifs_zn_obsolete(znode)) {
955 * atomic_long_inc(&c->clean_zn_cnt);
956 * atomic_long_inc(&ubifs_clean_zn_cnt);
959 * Thus, we just delay the @c->clean_zn_cnt update until we
960 * have the mutex locked.
963 /* Do not access znode from this point on */
965 /* Update buffer positions */
967 used += ALIGN(len, 8);
968 avail -= ALIGN(len, 8);
971 * Calculate the next index node length to see if there is
974 if (cnext == c->cnext)
977 next_len = ubifs_idx_node_sz(c, cnext->child_cnt);
979 nxt_offs = buf_offs + used + next_len;
980 if (next_len && nxt_offs <= c->leb_size) {
986 wlen = ALIGN(wlen, 8);
987 blen = ALIGN(wlen, c->min_io_size);
988 ubifs_pad(c, c->cbuf + wlen, blen - wlen);
991 /* The buffer is full or there are no more znodes to do */
992 err = ubifs_leb_write(c, lnum, c->cbuf, buf_offs, blen);
997 if (nxt_offs > c->leb_size) {
998 err = ubifs_update_one_lp(c, lnum, LPROPS_NC, 0,
1007 avail = buf_len - used;
1008 memmove(c->cbuf, c->cbuf + blen, used);
1014 if (lnum != c->dbg->new_ihead_lnum ||
1015 buf_offs != c->dbg->new_ihead_offs) {
1016 ubifs_err(c, "inconsistent ihead");
1020 c->ihead_lnum = lnum;
1021 c->ihead_offs = buf_offs;
1027 * free_obsolete_znodes - free obsolete znodes.
1028 * @c: UBIFS file-system description object
1030 * At the end of commit end, obsolete znodes are freed.
1032 static void free_obsolete_znodes(struct ubifs_info *c)
1034 struct ubifs_znode *znode, *cnext;
1039 cnext = znode->cnext;
1040 if (ubifs_zn_obsolete(znode))
1043 znode->cnext = NULL;
1044 atomic_long_inc(&c->clean_zn_cnt);
1045 atomic_long_inc(&ubifs_clean_zn_cnt);
1047 } while (cnext != c->cnext);
1051 * return_gap_lebs - return LEBs used by the in-gap commit method.
1052 * @c: UBIFS file-system description object
1054 * This function clears the "taken" flag for the LEBs which were used by the
1055 * "commit in-the-gaps" method.
1057 static int return_gap_lebs(struct ubifs_info *c)
1065 for (p = c->gap_lebs; *p != -1; p++) {
1066 err = ubifs_change_one_lp(c, *p, LPROPS_NC, LPROPS_NC, 0,
1078 * ubifs_tnc_end_commit - update the TNC for commit end.
1079 * @c: UBIFS file-system description object
1081 * Write the dirty znodes.
1083 int ubifs_tnc_end_commit(struct ubifs_info *c)
1090 err = return_gap_lebs(c);
1094 err = write_index(c);
1098 mutex_lock(&c->tnc_mutex);
1100 dbg_cmt("TNC height is %d", c->zroot.znode->level + 1);
1102 free_obsolete_znodes(c);
1108 mutex_unlock(&c->tnc_mutex);