2 * This file is part of UBIFS.
4 * Copyright (C) 2006-2008 Nokia Corporation.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Authors: Adrian Hunter
20 * Artem Bityutskiy (Битюцкий Артём)
23 /* This file implements TNC functions for committing */
28 * make_idx_node - make an index node for fill-the-gaps method of TNC commit.
29 * @c: UBIFS file-system description object
30 * @idx: buffer in which to place new index node
31 * @znode: znode from which to make new index node
32 * @lnum: LEB number where new index node will be written
33 * @offs: offset where new index node will be written
34 * @len: length of new index node
36 static int make_idx_node(struct ubifs_info *c, struct ubifs_idx_node *idx,
37 struct ubifs_znode *znode, int lnum, int offs, int len)
39 struct ubifs_znode *zp;
43 idx->ch.node_type = UBIFS_IDX_NODE;
44 idx->child_cnt = cpu_to_le16(znode->child_cnt);
45 idx->level = cpu_to_le16(znode->level);
46 for (i = 0; i < znode->child_cnt; i++) {
47 struct ubifs_branch *br = ubifs_idx_branch(c, idx, i);
48 struct ubifs_zbranch *zbr = &znode->zbranch[i];
50 key_write_idx(c, &zbr->key, &br->key);
51 br->lnum = cpu_to_le32(zbr->lnum);
52 br->offs = cpu_to_le32(zbr->offs);
53 br->len = cpu_to_le32(zbr->len);
54 if (!zbr->lnum || !zbr->len) {
55 ubifs_err("bad ref in znode");
56 dbg_dump_znode(c, znode);
58 dbg_dump_znode(c, zbr->znode);
61 ubifs_prepare_node(c, idx, len, 0);
63 #ifdef CONFIG_UBIFS_FS_DEBUG
69 err = insert_old_idx_znode(c, znode);
71 /* Update the parent */
74 struct ubifs_zbranch *zbr;
76 zbr = &zp->zbranch[znode->iip];
85 c->calc_idx_sz += ALIGN(len, 8);
87 atomic_long_dec(&c->dirty_zn_cnt);
89 ubifs_assert(ubifs_zn_dirty(znode));
90 ubifs_assert(ubifs_zn_cow(znode));
93 * Note, unlike 'write_index()' we do not add memory barriers here
94 * because this function is called with @c->tnc_mutex locked.
96 __clear_bit(DIRTY_ZNODE, &znode->flags);
97 __clear_bit(COW_ZNODE, &znode->flags);
103 * fill_gap - make index nodes in gaps in dirty index LEBs.
104 * @c: UBIFS file-system description object
105 * @lnum: LEB number that gap appears in
106 * @gap_start: offset of start of gap
107 * @gap_end: offset of end of gap
108 * @dirt: adds dirty space to this
110 * This function returns the number of index nodes written into the gap.
112 static int fill_gap(struct ubifs_info *c, int lnum, int gap_start, int gap_end,
115 int len, gap_remains, gap_pos, written, pad_len;
117 ubifs_assert((gap_start & 7) == 0);
118 ubifs_assert((gap_end & 7) == 0);
119 ubifs_assert(gap_end >= gap_start);
121 gap_remains = gap_end - gap_start;
127 len = ubifs_idx_node_sz(c, c->enext->child_cnt);
128 if (len < gap_remains) {
129 struct ubifs_znode *znode = c->enext;
130 const int alen = ALIGN(len, 8);
133 ubifs_assert(alen <= gap_remains);
134 err = make_idx_node(c, c->ileb_buf + gap_pos, znode,
140 c->enext = znode->cnext;
141 if (c->enext == c->cnext)
147 if (gap_end == c->leb_size) {
148 c->ileb_len = ALIGN(gap_pos, c->min_io_size);
149 /* Pad to end of min_io_size */
150 pad_len = c->ileb_len - gap_pos;
152 /* Pad to end of gap */
153 pad_len = gap_remains;
154 dbg_gc("LEB %d:%d to %d len %d nodes written %d wasted bytes %d",
155 lnum, gap_start, gap_end, gap_end - gap_start, written, pad_len);
156 ubifs_pad(c, c->ileb_buf + gap_pos, pad_len);
162 * find_old_idx - find an index node obsoleted since the last commit start.
163 * @c: UBIFS file-system description object
164 * @lnum: LEB number of obsoleted index node
165 * @offs: offset of obsoleted index node
167 * Returns %1 if found and %0 otherwise.
169 static int find_old_idx(struct ubifs_info *c, int lnum, int offs)
171 struct ubifs_old_idx *o;
174 p = c->old_idx.rb_node;
176 o = rb_entry(p, struct ubifs_old_idx, rb);
179 else if (lnum > o->lnum)
181 else if (offs < o->offs)
183 else if (offs > o->offs)
192 * is_idx_node_in_use - determine if an index node can be overwritten.
193 * @c: UBIFS file-system description object
194 * @key: key of index node
195 * @level: index node level
196 * @lnum: LEB number of index node
197 * @offs: offset of index node
199 * If @key / @lnum / @offs identify an index node that was not part of the old
200 * index, then this function returns %0 (obsolete). Else if the index node was
201 * part of the old index but is now dirty %1 is returned, else if it is clean %2
202 * is returned. A negative error code is returned on failure.
204 static int is_idx_node_in_use(struct ubifs_info *c, union ubifs_key *key,
205 int level, int lnum, int offs)
209 ret = is_idx_node_in_tnc(c, key, level, lnum, offs);
211 return ret; /* Error code */
213 if (find_old_idx(c, lnum, offs))
219 * layout_leb_in_gaps - layout index nodes using in-the-gaps method.
220 * @c: UBIFS file-system description object
221 * @p: return LEB number here
223 * This function lays out new index nodes for dirty znodes using in-the-gaps
224 * method of TNC commit.
225 * This function merely puts the next znode into the next gap, making no attempt
226 * to try to maximise the number of znodes that fit.
227 * This function returns the number of index nodes written into the gaps, or a
228 * negative error code on failure.
230 static int layout_leb_in_gaps(struct ubifs_info *c, int *p)
232 struct ubifs_scan_leb *sleb;
233 struct ubifs_scan_node *snod;
234 int lnum, dirt = 0, gap_start, gap_end, err, written, tot_written;
237 /* Get an index LEB with lots of obsolete index nodes */
238 lnum = ubifs_find_dirty_idx_leb(c);
241 * There also may be dirt in the index head that could be
242 * filled, however we do not check there at present.
244 return lnum; /* Error code */
246 dbg_gc("LEB %d", lnum);
248 * Scan the index LEB. We use the generic scan for this even though
249 * it is more comprehensive and less efficient than is needed for this
252 sleb = ubifs_scan(c, lnum, 0, c->ileb_buf, 0);
255 return PTR_ERR(sleb);
257 list_for_each_entry(snod, &sleb->nodes, list) {
258 struct ubifs_idx_node *idx;
261 ubifs_assert(snod->type == UBIFS_IDX_NODE);
263 key_read(c, ubifs_idx_key(c, idx), &snod->key);
264 level = le16_to_cpu(idx->level);
265 /* Determine if the index node is in use (not obsolete) */
266 in_use = is_idx_node_in_use(c, &snod->key, level, lnum,
269 ubifs_scan_destroy(sleb);
270 return in_use; /* Error code */
274 dirt += ALIGN(snod->len, 8);
276 * The obsolete index nodes form gaps that can be
277 * overwritten. This gap has ended because we have
278 * found an index node that is still in use
281 gap_end = snod->offs;
282 /* Try to fill gap */
283 written = fill_gap(c, lnum, gap_start, gap_end, &dirt);
285 ubifs_scan_destroy(sleb);
286 return written; /* Error code */
288 tot_written += written;
289 gap_start = ALIGN(snod->offs + snod->len, 8);
292 ubifs_scan_destroy(sleb);
293 c->ileb_len = c->leb_size;
294 gap_end = c->leb_size;
295 /* Try to fill gap */
296 written = fill_gap(c, lnum, gap_start, gap_end, &dirt);
298 return written; /* Error code */
299 tot_written += written;
300 if (tot_written == 0) {
301 struct ubifs_lprops lp;
303 dbg_gc("LEB %d wrote %d index nodes", lnum, tot_written);
304 err = ubifs_read_one_lp(c, lnum, &lp);
307 if (lp.free == c->leb_size) {
309 * We must have snatched this LEB from the idx_gc list
310 * so we need to correct the free and dirty space.
312 err = ubifs_change_one_lp(c, lnum,
313 c->leb_size - c->ileb_len,
320 err = ubifs_change_one_lp(c, lnum, c->leb_size - c->ileb_len, dirt,
324 err = ubifs_leb_change(c, lnum, c->ileb_buf, c->ileb_len,
328 dbg_gc("LEB %d wrote %d index nodes", lnum, tot_written);
333 * get_leb_cnt - calculate the number of empty LEBs needed to commit.
334 * @c: UBIFS file-system description object
335 * @cnt: number of znodes to commit
337 * This function returns the number of empty LEBs needed to commit @cnt znodes
338 * to the current index head. The number is not exact and may be more than
341 static int get_leb_cnt(struct ubifs_info *c, int cnt)
345 /* Assume maximum index node size (i.e. overestimate space needed) */
346 cnt -= (c->leb_size - c->ihead_offs) / c->max_idx_node_sz;
349 d = c->leb_size / c->max_idx_node_sz;
350 return DIV_ROUND_UP(cnt, d);
354 * layout_in_gaps - in-the-gaps method of committing TNC.
355 * @c: UBIFS file-system description object
356 * @cnt: number of dirty znodes to commit.
358 * This function lays out new index nodes for dirty znodes using in-the-gaps
359 * method of TNC commit.
361 * This function returns %0 on success and a negative error code on failure.
363 static int layout_in_gaps(struct ubifs_info *c, int cnt)
365 int err, leb_needed_cnt, written, *p;
367 dbg_gc("%d znodes to write", cnt);
369 c->gap_lebs = kmalloc(sizeof(int) * (c->lst.idx_lebs + 1), GFP_NOFS);
375 ubifs_assert(p < c->gap_lebs + sizeof(int) * c->lst.idx_lebs);
376 written = layout_leb_in_gaps(c, p);
379 if (err != -ENOSPC) {
384 if (dbg_force_in_the_gaps_enabled()) {
386 * Do not print scary warnings if the debugging
387 * option which forces in-the-gaps is enabled.
389 ubifs_warn("out of space");
390 dbg_dump_budg(c, &c->bi);
393 /* Try to commit anyway */
399 leb_needed_cnt = get_leb_cnt(c, cnt);
400 dbg_gc("%d znodes remaining, need %d LEBs, have %d", cnt,
401 leb_needed_cnt, c->ileb_cnt);
402 } while (leb_needed_cnt > c->ileb_cnt);
409 * layout_in_empty_space - layout index nodes in empty space.
410 * @c: UBIFS file-system description object
412 * This function lays out new index nodes for dirty znodes using empty LEBs.
414 * This function returns %0 on success and a negative error code on failure.
416 static int layout_in_empty_space(struct ubifs_info *c)
418 struct ubifs_znode *znode, *cnext, *zp;
419 int lnum, offs, len, next_len, buf_len, buf_offs, used, avail;
426 lnum = c->ihead_lnum;
427 buf_offs = c->ihead_offs;
429 buf_len = ubifs_idx_node_sz(c, c->fanout);
430 buf_len = ALIGN(buf_len, c->min_io_size);
434 /* Ensure there is enough room for first write */
435 next_len = ubifs_idx_node_sz(c, cnext->child_cnt);
436 if (buf_offs + next_len > c->leb_size)
442 len = ubifs_idx_node_sz(c, znode->child_cnt);
444 /* Determine the index node position */
446 if (c->ileb_nxt >= c->ileb_cnt) {
447 ubifs_err("out of space");
450 lnum = c->ilebs[c->ileb_nxt++];
456 offs = buf_offs + used;
458 #ifdef CONFIG_UBIFS_FS_DEBUG
464 /* Update the parent */
467 struct ubifs_zbranch *zbr;
471 zbr = &zp->zbranch[i];
476 c->zroot.lnum = lnum;
477 c->zroot.offs = offs;
480 c->calc_idx_sz += ALIGN(len, 8);
483 * Once lprops is updated, we can decrease the dirty znode count
484 * but it is easier to just do it here.
486 atomic_long_dec(&c->dirty_zn_cnt);
489 * Calculate the next index node length to see if there is
492 cnext = znode->cnext;
493 if (cnext == c->cnext)
496 next_len = ubifs_idx_node_sz(c, cnext->child_cnt);
498 /* Update buffer positions */
500 used += ALIGN(len, 8);
501 avail -= ALIGN(len, 8);
504 buf_offs + used + next_len <= c->leb_size &&
508 if (avail <= 0 && next_len &&
509 buf_offs + used + next_len <= c->leb_size)
512 blen = ALIGN(wlen, c->min_io_size);
514 /* The buffer is full or there are no more znodes to do */
517 if (buf_offs + next_len > c->leb_size) {
518 err = ubifs_update_one_lp(c, lnum,
519 c->leb_size - buf_offs, blen - used,
528 avail = buf_len - used;
531 err = ubifs_update_one_lp(c, lnum, c->leb_size - buf_offs,
538 #ifdef CONFIG_UBIFS_FS_DEBUG
539 c->dbg->new_ihead_lnum = lnum;
540 c->dbg->new_ihead_offs = buf_offs;
547 * layout_commit - determine positions of index nodes to commit.
548 * @c: UBIFS file-system description object
549 * @no_space: indicates that insufficient empty LEBs were allocated
550 * @cnt: number of znodes to commit
552 * Calculate and update the positions of index nodes to commit. If there were
553 * an insufficient number of empty LEBs allocated, then index nodes are placed
554 * into the gaps created by obsolete index nodes in non-empty index LEBs. For
555 * this purpose, an obsolete index node is one that was not in the index as at
556 * the end of the last commit. To write "in-the-gaps" requires that those index
557 * LEBs are updated atomically in-place.
559 static int layout_commit(struct ubifs_info *c, int no_space, int cnt)
564 err = layout_in_gaps(c, cnt);
568 err = layout_in_empty_space(c);
573 * find_first_dirty - find first dirty znode.
574 * @znode: znode to begin searching from
576 static struct ubifs_znode *find_first_dirty(struct ubifs_znode *znode)
584 if (znode->level == 0) {
585 if (ubifs_zn_dirty(znode))
590 for (i = 0; i < znode->child_cnt; i++) {
591 struct ubifs_zbranch *zbr = &znode->zbranch[i];
593 if (zbr->znode && ubifs_zn_dirty(zbr->znode)) {
600 if (ubifs_zn_dirty(znode))
608 * find_next_dirty - find next dirty znode.
609 * @znode: znode to begin searching from
611 static struct ubifs_znode *find_next_dirty(struct ubifs_znode *znode)
613 int n = znode->iip + 1;
615 znode = znode->parent;
618 for (; n < znode->child_cnt; n++) {
619 struct ubifs_zbranch *zbr = &znode->zbranch[n];
621 if (zbr->znode && ubifs_zn_dirty(zbr->znode))
622 return find_first_dirty(zbr->znode);
628 * get_znodes_to_commit - create list of dirty znodes to commit.
629 * @c: UBIFS file-system description object
631 * This function returns the number of znodes to commit.
633 static int get_znodes_to_commit(struct ubifs_info *c)
635 struct ubifs_znode *znode, *cnext;
638 c->cnext = find_first_dirty(c->zroot.znode);
639 znode = c->enext = c->cnext;
641 dbg_cmt("no znodes to commit");
646 ubifs_assert(!ubifs_zn_cow(znode));
647 __set_bit(COW_ZNODE, &znode->flags);
649 cnext = find_next_dirty(znode);
651 znode->cnext = c->cnext;
654 znode->cnext = cnext;
658 dbg_cmt("committing %d znodes", cnt);
659 ubifs_assert(cnt == atomic_long_read(&c->dirty_zn_cnt));
664 * alloc_idx_lebs - allocate empty LEBs to be used to commit.
665 * @c: UBIFS file-system description object
666 * @cnt: number of znodes to commit
668 * This function returns %-ENOSPC if it cannot allocate a sufficient number of
669 * empty LEBs. %0 is returned on success, otherwise a negative error code
672 static int alloc_idx_lebs(struct ubifs_info *c, int cnt)
674 int i, leb_cnt, lnum;
678 leb_cnt = get_leb_cnt(c, cnt);
679 dbg_cmt("need about %d empty LEBS for TNC commit", leb_cnt);
682 c->ilebs = kmalloc(leb_cnt * sizeof(int), GFP_NOFS);
685 for (i = 0; i < leb_cnt; i++) {
686 lnum = ubifs_find_free_leb_for_idx(c);
689 c->ilebs[c->ileb_cnt++] = lnum;
690 dbg_cmt("LEB %d", lnum);
692 if (dbg_force_in_the_gaps())
698 * free_unused_idx_lebs - free unused LEBs that were allocated for the commit.
699 * @c: UBIFS file-system description object
701 * It is possible that we allocate more empty LEBs for the commit than we need.
702 * This functions frees the surplus.
704 * This function returns %0 on success and a negative error code on failure.
706 static int free_unused_idx_lebs(struct ubifs_info *c)
708 int i, err = 0, lnum, er;
710 for (i = c->ileb_nxt; i < c->ileb_cnt; i++) {
712 dbg_cmt("LEB %d", lnum);
713 er = ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0,
714 LPROPS_INDEX | LPROPS_TAKEN, 0);
722 * free_idx_lebs - free unused LEBs after commit end.
723 * @c: UBIFS file-system description object
725 * This function returns %0 on success and a negative error code on failure.
727 static int free_idx_lebs(struct ubifs_info *c)
731 err = free_unused_idx_lebs(c);
738 * ubifs_tnc_start_commit - start TNC commit.
739 * @c: UBIFS file-system description object
740 * @zroot: new index root position is returned here
742 * This function prepares the list of indexing nodes to commit and lays out
743 * their positions on flash. If there is not enough free space it uses the
744 * in-gap commit method. Returns zero in case of success and a negative error
745 * code in case of failure.
747 int ubifs_tnc_start_commit(struct ubifs_info *c, struct ubifs_zbranch *zroot)
751 mutex_lock(&c->tnc_mutex);
752 err = dbg_check_tnc(c, 1);
755 cnt = get_znodes_to_commit(c);
759 err = alloc_idx_lebs(c, cnt);
764 err = layout_commit(c, no_space, cnt);
767 ubifs_assert(atomic_long_read(&c->dirty_zn_cnt) == 0);
768 err = free_unused_idx_lebs(c);
773 memcpy(zroot, &c->zroot, sizeof(struct ubifs_zbranch));
775 err = ubifs_save_dirty_idx_lnums(c);
779 spin_lock(&c->space_lock);
781 * Although we have not finished committing yet, update size of the
782 * committed index ('c->bi.old_idx_sz') and zero out the index growth
783 * budget. It is OK to do this now, because we've reserved all the
784 * space which is needed to commit the index, and it is save for the
785 * budgeting subsystem to assume the index is already committed,
786 * even though it is not.
788 ubifs_assert(c->bi.min_idx_lebs == ubifs_calc_min_idx_lebs(c));
789 c->bi.old_idx_sz = c->calc_idx_sz;
790 c->bi.uncommitted_idx = 0;
791 c->bi.min_idx_lebs = ubifs_calc_min_idx_lebs(c);
792 spin_unlock(&c->space_lock);
793 mutex_unlock(&c->tnc_mutex);
795 dbg_cmt("number of index LEBs %d", c->lst.idx_lebs);
796 dbg_cmt("size of index %llu", c->calc_idx_sz);
802 mutex_unlock(&c->tnc_mutex);
807 * write_index - write index nodes.
808 * @c: UBIFS file-system description object
810 * This function writes the index nodes whose positions were laid out in the
811 * layout_in_empty_space function.
813 static int write_index(struct ubifs_info *c)
815 struct ubifs_idx_node *idx;
816 struct ubifs_znode *znode, *cnext;
817 int i, lnum, offs, len, next_len, buf_len, buf_offs, used;
818 int avail, wlen, err, lnum_pos = 0, blen, nxt_offs;
825 * Always write index nodes to the index head so that index nodes and
826 * other types of nodes are never mixed in the same erase block.
828 lnum = c->ihead_lnum;
829 buf_offs = c->ihead_offs;
831 /* Allocate commit buffer */
832 buf_len = ALIGN(c->max_idx_node_sz, c->min_io_size);
836 /* Ensure there is enough room for first write */
837 next_len = ubifs_idx_node_sz(c, cnext->child_cnt);
838 if (buf_offs + next_len > c->leb_size) {
839 err = ubifs_update_one_lp(c, lnum, LPROPS_NC, 0, 0,
850 idx = c->cbuf + used;
852 /* Make index node */
853 idx->ch.node_type = UBIFS_IDX_NODE;
854 idx->child_cnt = cpu_to_le16(znode->child_cnt);
855 idx->level = cpu_to_le16(znode->level);
856 for (i = 0; i < znode->child_cnt; i++) {
857 struct ubifs_branch *br = ubifs_idx_branch(c, idx, i);
858 struct ubifs_zbranch *zbr = &znode->zbranch[i];
860 key_write_idx(c, &zbr->key, &br->key);
861 br->lnum = cpu_to_le32(zbr->lnum);
862 br->offs = cpu_to_le32(zbr->offs);
863 br->len = cpu_to_le32(zbr->len);
864 if (!zbr->lnum || !zbr->len) {
865 ubifs_err("bad ref in znode");
866 dbg_dump_znode(c, znode);
868 dbg_dump_znode(c, zbr->znode);
871 len = ubifs_idx_node_sz(c, znode->child_cnt);
872 ubifs_prepare_node(c, idx, len, 0);
874 /* Determine the index node position */
876 lnum = c->ilebs[lnum_pos++];
881 offs = buf_offs + used;
883 #ifdef CONFIG_UBIFS_FS_DEBUG
884 if (lnum != znode->lnum || offs != znode->offs ||
886 ubifs_err("inconsistent znode posn");
891 /* Grab some stuff from znode while we still can */
892 cnext = znode->cnext;
894 ubifs_assert(ubifs_zn_dirty(znode));
895 ubifs_assert(ubifs_zn_cow(znode));
898 * It is important that other threads should see %DIRTY_ZNODE
899 * flag cleared before %COW_ZNODE. Specifically, it matters in
900 * the 'dirty_cow_znode()' function. This is the reason for the
901 * first barrier. Also, we want the bit changes to be seen to
902 * other threads ASAP, to avoid unnecesarry copying, which is
903 * the reason for the second barrier.
905 clear_bit(DIRTY_ZNODE, &znode->flags);
906 smp_mb__before_clear_bit();
907 clear_bit(COW_ZNODE, &znode->flags);
908 smp_mb__after_clear_bit();
911 * We have marked the znode as clean but have not updated the
912 * @c->clean_zn_cnt counter. If this znode becomes dirty again
913 * before 'free_obsolete_znodes()' is called, then
914 * @c->clean_zn_cnt will be decremented before it gets
915 * incremented (resulting in 2 decrements for the same znode).
916 * This means that @c->clean_zn_cnt may become negative for a
919 * Q: why we cannot increment @c->clean_zn_cnt?
920 * A: because we do not have the @c->tnc_mutex locked, and the
921 * following code would be racy and buggy:
923 * if (!ubifs_zn_obsolete(znode)) {
924 * atomic_long_inc(&c->clean_zn_cnt);
925 * atomic_long_inc(&ubifs_clean_zn_cnt);
928 * Thus, we just delay the @c->clean_zn_cnt update until we
929 * have the mutex locked.
932 /* Do not access znode from this point on */
934 /* Update buffer positions */
936 used += ALIGN(len, 8);
937 avail -= ALIGN(len, 8);
940 * Calculate the next index node length to see if there is
943 if (cnext == c->cnext)
946 next_len = ubifs_idx_node_sz(c, cnext->child_cnt);
948 nxt_offs = buf_offs + used + next_len;
949 if (next_len && nxt_offs <= c->leb_size) {
955 wlen = ALIGN(wlen, 8);
956 blen = ALIGN(wlen, c->min_io_size);
957 ubifs_pad(c, c->cbuf + wlen, blen - wlen);
960 /* The buffer is full or there are no more znodes to do */
961 err = ubifs_leb_write(c, lnum, c->cbuf, buf_offs, blen,
967 if (nxt_offs > c->leb_size) {
968 err = ubifs_update_one_lp(c, lnum, LPROPS_NC, 0,
977 avail = buf_len - used;
978 memmove(c->cbuf, c->cbuf + blen, used);
984 #ifdef CONFIG_UBIFS_FS_DEBUG
985 if (lnum != c->dbg->new_ihead_lnum ||
986 buf_offs != c->dbg->new_ihead_offs) {
987 ubifs_err("inconsistent ihead");
992 c->ihead_lnum = lnum;
993 c->ihead_offs = buf_offs;
999 * free_obsolete_znodes - free obsolete znodes.
1000 * @c: UBIFS file-system description object
1002 * At the end of commit end, obsolete znodes are freed.
1004 static void free_obsolete_znodes(struct ubifs_info *c)
1006 struct ubifs_znode *znode, *cnext;
1011 cnext = znode->cnext;
1012 if (ubifs_zn_obsolete(znode))
1015 znode->cnext = NULL;
1016 atomic_long_inc(&c->clean_zn_cnt);
1017 atomic_long_inc(&ubifs_clean_zn_cnt);
1019 } while (cnext != c->cnext);
1023 * return_gap_lebs - return LEBs used by the in-gap commit method.
1024 * @c: UBIFS file-system description object
1026 * This function clears the "taken" flag for the LEBs which were used by the
1027 * "commit in-the-gaps" method.
1029 static int return_gap_lebs(struct ubifs_info *c)
1037 for (p = c->gap_lebs; *p != -1; p++) {
1038 err = ubifs_change_one_lp(c, *p, LPROPS_NC, LPROPS_NC, 0,
1050 * ubifs_tnc_end_commit - update the TNC for commit end.
1051 * @c: UBIFS file-system description object
1053 * Write the dirty znodes.
1055 int ubifs_tnc_end_commit(struct ubifs_info *c)
1062 err = return_gap_lebs(c);
1066 err = write_index(c);
1070 mutex_lock(&c->tnc_mutex);
1072 dbg_cmt("TNC height is %d", c->zroot.znode->level + 1);
1074 free_obsolete_znodes(c);
1080 mutex_unlock(&c->tnc_mutex);