UBIFS: remove Kconfig debugging option
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / ubifs / tnc_commit.c
blobef5df1e24dca4f4a09e548241b5e6e116dc55ed6
1 /*
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
13 * more details.
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 */
25 #include <linux/random.h>
26 #include "ubifs.h"
28 /**
29 * make_idx_node - make an index node for fill-the-gaps method of TNC commit.
30 * @c: UBIFS file-system description object
31 * @idx: buffer in which to place new index node
32 * @znode: znode from which to make new index node
33 * @lnum: LEB number where new index node will be written
34 * @offs: offset where new index node will be written
35 * @len: length of new index node
37 static int make_idx_node(struct ubifs_info *c, struct ubifs_idx_node *idx,
38 struct ubifs_znode *znode, int lnum, int offs, int len)
40 struct ubifs_znode *zp;
41 int i, err;
43 /* Make index node */
44 idx->ch.node_type = UBIFS_IDX_NODE;
45 idx->child_cnt = cpu_to_le16(znode->child_cnt);
46 idx->level = cpu_to_le16(znode->level);
47 for (i = 0; i < znode->child_cnt; i++) {
48 struct ubifs_branch *br = ubifs_idx_branch(c, idx, i);
49 struct ubifs_zbranch *zbr = &znode->zbranch[i];
51 key_write_idx(c, &zbr->key, &br->key);
52 br->lnum = cpu_to_le32(zbr->lnum);
53 br->offs = cpu_to_le32(zbr->offs);
54 br->len = cpu_to_le32(zbr->len);
55 if (!zbr->lnum || !zbr->len) {
56 ubifs_err("bad ref in znode");
57 ubifs_dump_znode(c, znode);
58 if (zbr->znode)
59 ubifs_dump_znode(c, zbr->znode);
62 ubifs_prepare_node(c, idx, len, 0);
64 znode->lnum = lnum;
65 znode->offs = offs;
66 znode->len = len;
68 err = insert_old_idx_znode(c, znode);
70 /* Update the parent */
71 zp = znode->parent;
72 if (zp) {
73 struct ubifs_zbranch *zbr;
75 zbr = &zp->zbranch[znode->iip];
76 zbr->lnum = lnum;
77 zbr->offs = offs;
78 zbr->len = len;
79 } else {
80 c->zroot.lnum = lnum;
81 c->zroot.offs = offs;
82 c->zroot.len = len;
84 c->calc_idx_sz += ALIGN(len, 8);
86 atomic_long_dec(&c->dirty_zn_cnt);
88 ubifs_assert(ubifs_zn_dirty(znode));
89 ubifs_assert(ubifs_zn_cow(znode));
92 * Note, unlike 'write_index()' we do not add memory barriers here
93 * because this function is called with @c->tnc_mutex locked.
95 __clear_bit(DIRTY_ZNODE, &znode->flags);
96 __clear_bit(COW_ZNODE, &znode->flags);
98 return err;
102 * fill_gap - make index nodes in gaps in dirty index LEBs.
103 * @c: UBIFS file-system description object
104 * @lnum: LEB number that gap appears in
105 * @gap_start: offset of start of gap
106 * @gap_end: offset of end of gap
107 * @dirt: adds dirty space to this
109 * This function returns the number of index nodes written into the gap.
111 static int fill_gap(struct ubifs_info *c, int lnum, int gap_start, int gap_end,
112 int *dirt)
114 int len, gap_remains, gap_pos, written, pad_len;
116 ubifs_assert((gap_start & 7) == 0);
117 ubifs_assert((gap_end & 7) == 0);
118 ubifs_assert(gap_end >= gap_start);
120 gap_remains = gap_end - gap_start;
121 if (!gap_remains)
122 return 0;
123 gap_pos = gap_start;
124 written = 0;
125 while (c->enext) {
126 len = ubifs_idx_node_sz(c, c->enext->child_cnt);
127 if (len < gap_remains) {
128 struct ubifs_znode *znode = c->enext;
129 const int alen = ALIGN(len, 8);
130 int err;
132 ubifs_assert(alen <= gap_remains);
133 err = make_idx_node(c, c->ileb_buf + gap_pos, znode,
134 lnum, gap_pos, len);
135 if (err)
136 return err;
137 gap_remains -= alen;
138 gap_pos += alen;
139 c->enext = znode->cnext;
140 if (c->enext == c->cnext)
141 c->enext = NULL;
142 written += 1;
143 } else
144 break;
146 if (gap_end == c->leb_size) {
147 c->ileb_len = ALIGN(gap_pos, c->min_io_size);
148 /* Pad to end of min_io_size */
149 pad_len = c->ileb_len - gap_pos;
150 } else
151 /* Pad to end of gap */
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);
155 ubifs_pad(c, c->ileb_buf + gap_pos, pad_len);
156 *dirt += pad_len;
157 return written;
161 * find_old_idx - find an index node obsoleted since the last commit start.
162 * @c: UBIFS file-system description object
163 * @lnum: LEB number of obsoleted index node
164 * @offs: offset of obsoleted index node
166 * Returns %1 if found and %0 otherwise.
168 static int find_old_idx(struct ubifs_info *c, int lnum, int offs)
170 struct ubifs_old_idx *o;
171 struct rb_node *p;
173 p = c->old_idx.rb_node;
174 while (p) {
175 o = rb_entry(p, struct ubifs_old_idx, rb);
176 if (lnum < o->lnum)
177 p = p->rb_left;
178 else if (lnum > o->lnum)
179 p = p->rb_right;
180 else if (offs < o->offs)
181 p = p->rb_left;
182 else if (offs > o->offs)
183 p = p->rb_right;
184 else
185 return 1;
187 return 0;
191 * is_idx_node_in_use - determine if an index node can be overwritten.
192 * @c: UBIFS file-system description object
193 * @key: key of index node
194 * @level: index node level
195 * @lnum: LEB number of index node
196 * @offs: offset of index node
198 * If @key / @lnum / @offs identify an index node that was not part of the old
199 * index, then this function returns %0 (obsolete). Else if the index node was
200 * part of the old index but is now dirty %1 is returned, else if it is clean %2
201 * is returned. A negative error code is returned on failure.
203 static int is_idx_node_in_use(struct ubifs_info *c, union ubifs_key *key,
204 int level, int lnum, int offs)
206 int ret;
208 ret = is_idx_node_in_tnc(c, key, level, lnum, offs);
209 if (ret < 0)
210 return ret; /* Error code */
211 if (ret == 0)
212 if (find_old_idx(c, lnum, offs))
213 return 1;
214 return ret;
218 * layout_leb_in_gaps - layout index nodes using in-the-gaps method.
219 * @c: UBIFS file-system description object
220 * @p: return LEB number here
222 * This function lays out new index nodes for dirty znodes using in-the-gaps
223 * method of TNC commit.
224 * This function merely puts the next znode into the next gap, making no attempt
225 * to try to maximise the number of znodes that fit.
226 * This function returns the number of index nodes written into the gaps, or a
227 * negative error code on failure.
229 static int layout_leb_in_gaps(struct ubifs_info *c, int *p)
231 struct ubifs_scan_leb *sleb;
232 struct ubifs_scan_node *snod;
233 int lnum, dirt = 0, gap_start, gap_end, err, written, tot_written;
235 tot_written = 0;
236 /* Get an index LEB with lots of obsolete index nodes */
237 lnum = ubifs_find_dirty_idx_leb(c);
238 if (lnum < 0)
240 * There also may be dirt in the index head that could be
241 * filled, however we do not check there at present.
243 return lnum; /* Error code */
244 *p = lnum;
245 dbg_gc("LEB %d", lnum);
247 * Scan the index LEB. We use the generic scan for this even though
248 * it is more comprehensive and less efficient than is needed for this
249 * purpose.
251 sleb = ubifs_scan(c, lnum, 0, c->ileb_buf, 0);
252 c->ileb_len = 0;
253 if (IS_ERR(sleb))
254 return PTR_ERR(sleb);
255 gap_start = 0;
256 list_for_each_entry(snod, &sleb->nodes, list) {
257 struct ubifs_idx_node *idx;
258 int in_use, level;
260 ubifs_assert(snod->type == UBIFS_IDX_NODE);
261 idx = snod->node;
262 key_read(c, ubifs_idx_key(c, idx), &snod->key);
263 level = le16_to_cpu(idx->level);
264 /* Determine if the index node is in use (not obsolete) */
265 in_use = is_idx_node_in_use(c, &snod->key, level, lnum,
266 snod->offs);
267 if (in_use < 0) {
268 ubifs_scan_destroy(sleb);
269 return in_use; /* Error code */
271 if (in_use) {
272 if (in_use == 1)
273 dirt += ALIGN(snod->len, 8);
275 * The obsolete index nodes form gaps that can be
276 * overwritten. This gap has ended because we have
277 * found an index node that is still in use
278 * i.e. not obsolete
280 gap_end = snod->offs;
281 /* Try to fill gap */
282 written = fill_gap(c, lnum, gap_start, gap_end, &dirt);
283 if (written < 0) {
284 ubifs_scan_destroy(sleb);
285 return written; /* Error code */
287 tot_written += written;
288 gap_start = ALIGN(snod->offs + snod->len, 8);
291 ubifs_scan_destroy(sleb);
292 c->ileb_len = c->leb_size;
293 gap_end = c->leb_size;
294 /* Try to fill gap */
295 written = fill_gap(c, lnum, gap_start, gap_end, &dirt);
296 if (written < 0)
297 return written; /* Error code */
298 tot_written += written;
299 if (tot_written == 0) {
300 struct ubifs_lprops lp;
302 dbg_gc("LEB %d wrote %d index nodes", lnum, tot_written);
303 err = ubifs_read_one_lp(c, lnum, &lp);
304 if (err)
305 return err;
306 if (lp.free == c->leb_size) {
308 * We must have snatched this LEB from the idx_gc list
309 * so we need to correct the free and dirty space.
311 err = ubifs_change_one_lp(c, lnum,
312 c->leb_size - c->ileb_len,
313 dirt, 0, 0, 0);
314 if (err)
315 return err;
317 return 0;
319 err = ubifs_change_one_lp(c, lnum, c->leb_size - c->ileb_len, dirt,
320 0, 0, 0);
321 if (err)
322 return err;
323 err = ubifs_leb_change(c, lnum, c->ileb_buf, c->ileb_len,
324 UBI_SHORTTERM);
325 if (err)
326 return err;
327 dbg_gc("LEB %d wrote %d index nodes", lnum, tot_written);
328 return tot_written;
332 * get_leb_cnt - calculate the number of empty LEBs needed to commit.
333 * @c: UBIFS file-system description object
334 * @cnt: number of znodes to commit
336 * This function returns the number of empty LEBs needed to commit @cnt znodes
337 * to the current index head. The number is not exact and may be more than
338 * needed.
340 static int get_leb_cnt(struct ubifs_info *c, int cnt)
342 int d;
344 /* Assume maximum index node size (i.e. overestimate space needed) */
345 cnt -= (c->leb_size - c->ihead_offs) / c->max_idx_node_sz;
346 if (cnt < 0)
347 cnt = 0;
348 d = c->leb_size / c->max_idx_node_sz;
349 return DIV_ROUND_UP(cnt, d);
353 * layout_in_gaps - in-the-gaps method of committing TNC.
354 * @c: UBIFS file-system description object
355 * @cnt: number of dirty znodes to commit.
357 * This function lays out new index nodes for dirty znodes using in-the-gaps
358 * method of TNC commit.
360 * This function returns %0 on success and a negative error code on failure.
362 static int layout_in_gaps(struct ubifs_info *c, int cnt)
364 int err, leb_needed_cnt, written, *p;
366 dbg_gc("%d znodes to write", cnt);
368 c->gap_lebs = kmalloc(sizeof(int) * (c->lst.idx_lebs + 1), GFP_NOFS);
369 if (!c->gap_lebs)
370 return -ENOMEM;
372 p = c->gap_lebs;
373 do {
374 ubifs_assert(p < c->gap_lebs + sizeof(int) * c->lst.idx_lebs);
375 written = layout_leb_in_gaps(c, p);
376 if (written < 0) {
377 err = written;
378 if (err != -ENOSPC) {
379 kfree(c->gap_lebs);
380 c->gap_lebs = NULL;
381 return err;
383 if (!dbg_is_chk_index(c)) {
385 * Do not print scary warnings if the debugging
386 * option which forces in-the-gaps is enabled.
388 ubifs_warn("out of space");
389 ubifs_dump_budg(c, &c->bi);
390 ubifs_dump_lprops(c);
392 /* Try to commit anyway */
393 err = 0;
394 break;
396 p++;
397 cnt -= written;
398 leb_needed_cnt = get_leb_cnt(c, cnt);
399 dbg_gc("%d znodes remaining, need %d LEBs, have %d", cnt,
400 leb_needed_cnt, c->ileb_cnt);
401 } while (leb_needed_cnt > c->ileb_cnt);
403 *p = -1;
404 return 0;
408 * layout_in_empty_space - layout index nodes in empty space.
409 * @c: UBIFS file-system description object
411 * This function lays out new index nodes for dirty znodes using empty LEBs.
413 * This function returns %0 on success and a negative error code on failure.
415 static int layout_in_empty_space(struct ubifs_info *c)
417 struct ubifs_znode *znode, *cnext, *zp;
418 int lnum, offs, len, next_len, buf_len, buf_offs, used, avail;
419 int wlen, blen, err;
421 cnext = c->enext;
422 if (!cnext)
423 return 0;
425 lnum = c->ihead_lnum;
426 buf_offs = c->ihead_offs;
428 buf_len = ubifs_idx_node_sz(c, c->fanout);
429 buf_len = ALIGN(buf_len, c->min_io_size);
430 used = 0;
431 avail = buf_len;
433 /* Ensure there is enough room for first write */
434 next_len = ubifs_idx_node_sz(c, cnext->child_cnt);
435 if (buf_offs + next_len > c->leb_size)
436 lnum = -1;
438 while (1) {
439 znode = cnext;
441 len = ubifs_idx_node_sz(c, znode->child_cnt);
443 /* Determine the index node position */
444 if (lnum == -1) {
445 if (c->ileb_nxt >= c->ileb_cnt) {
446 ubifs_err("out of space");
447 return -ENOSPC;
449 lnum = c->ilebs[c->ileb_nxt++];
450 buf_offs = 0;
451 used = 0;
452 avail = buf_len;
455 offs = buf_offs + used;
457 znode->lnum = lnum;
458 znode->offs = offs;
459 znode->len = len;
461 /* Update the parent */
462 zp = znode->parent;
463 if (zp) {
464 struct ubifs_zbranch *zbr;
465 int i;
467 i = znode->iip;
468 zbr = &zp->zbranch[i];
469 zbr->lnum = lnum;
470 zbr->offs = offs;
471 zbr->len = len;
472 } else {
473 c->zroot.lnum = lnum;
474 c->zroot.offs = offs;
475 c->zroot.len = len;
477 c->calc_idx_sz += ALIGN(len, 8);
480 * Once lprops is updated, we can decrease the dirty znode count
481 * but it is easier to just do it here.
483 atomic_long_dec(&c->dirty_zn_cnt);
486 * Calculate the next index node length to see if there is
487 * enough room for it
489 cnext = znode->cnext;
490 if (cnext == c->cnext)
491 next_len = 0;
492 else
493 next_len = ubifs_idx_node_sz(c, cnext->child_cnt);
495 /* Update buffer positions */
496 wlen = used + len;
497 used += ALIGN(len, 8);
498 avail -= ALIGN(len, 8);
500 if (next_len != 0 &&
501 buf_offs + used + next_len <= c->leb_size &&
502 avail > 0)
503 continue;
505 if (avail <= 0 && next_len &&
506 buf_offs + used + next_len <= c->leb_size)
507 blen = buf_len;
508 else
509 blen = ALIGN(wlen, c->min_io_size);
511 /* The buffer is full or there are no more znodes to do */
512 buf_offs += blen;
513 if (next_len) {
514 if (buf_offs + next_len > c->leb_size) {
515 err = ubifs_update_one_lp(c, lnum,
516 c->leb_size - buf_offs, blen - used,
517 0, 0);
518 if (err)
519 return err;
520 lnum = -1;
522 used -= blen;
523 if (used < 0)
524 used = 0;
525 avail = buf_len - used;
526 continue;
528 err = ubifs_update_one_lp(c, lnum, c->leb_size - buf_offs,
529 blen - used, 0, 0);
530 if (err)
531 return err;
532 break;
535 c->dbg->new_ihead_lnum = lnum;
536 c->dbg->new_ihead_offs = buf_offs;
538 return 0;
542 * layout_commit - determine positions of index nodes to commit.
543 * @c: UBIFS file-system description object
544 * @no_space: indicates that insufficient empty LEBs were allocated
545 * @cnt: number of znodes to commit
547 * Calculate and update the positions of index nodes to commit. If there were
548 * an insufficient number of empty LEBs allocated, then index nodes are placed
549 * into the gaps created by obsolete index nodes in non-empty index LEBs. For
550 * this purpose, an obsolete index node is one that was not in the index as at
551 * the end of the last commit. To write "in-the-gaps" requires that those index
552 * LEBs are updated atomically in-place.
554 static int layout_commit(struct ubifs_info *c, int no_space, int cnt)
556 int err;
558 if (no_space) {
559 err = layout_in_gaps(c, cnt);
560 if (err)
561 return err;
563 err = layout_in_empty_space(c);
564 return err;
568 * find_first_dirty - find first dirty znode.
569 * @znode: znode to begin searching from
571 static struct ubifs_znode *find_first_dirty(struct ubifs_znode *znode)
573 int i, cont;
575 if (!znode)
576 return NULL;
578 while (1) {
579 if (znode->level == 0) {
580 if (ubifs_zn_dirty(znode))
581 return znode;
582 return NULL;
584 cont = 0;
585 for (i = 0; i < znode->child_cnt; i++) {
586 struct ubifs_zbranch *zbr = &znode->zbranch[i];
588 if (zbr->znode && ubifs_zn_dirty(zbr->znode)) {
589 znode = zbr->znode;
590 cont = 1;
591 break;
594 if (!cont) {
595 if (ubifs_zn_dirty(znode))
596 return znode;
597 return NULL;
603 * find_next_dirty - find next dirty znode.
604 * @znode: znode to begin searching from
606 static struct ubifs_znode *find_next_dirty(struct ubifs_znode *znode)
608 int n = znode->iip + 1;
610 znode = znode->parent;
611 if (!znode)
612 return NULL;
613 for (; n < znode->child_cnt; n++) {
614 struct ubifs_zbranch *zbr = &znode->zbranch[n];
616 if (zbr->znode && ubifs_zn_dirty(zbr->znode))
617 return find_first_dirty(zbr->znode);
619 return znode;
623 * get_znodes_to_commit - create list of dirty znodes to commit.
624 * @c: UBIFS file-system description object
626 * This function returns the number of znodes to commit.
628 static int get_znodes_to_commit(struct ubifs_info *c)
630 struct ubifs_znode *znode, *cnext;
631 int cnt = 0;
633 c->cnext = find_first_dirty(c->zroot.znode);
634 znode = c->enext = c->cnext;
635 if (!znode) {
636 dbg_cmt("no znodes to commit");
637 return 0;
639 cnt += 1;
640 while (1) {
641 ubifs_assert(!ubifs_zn_cow(znode));
642 __set_bit(COW_ZNODE, &znode->flags);
643 znode->alt = 0;
644 cnext = find_next_dirty(znode);
645 if (!cnext) {
646 znode->cnext = c->cnext;
647 break;
649 znode->cnext = cnext;
650 znode = cnext;
651 cnt += 1;
653 dbg_cmt("committing %d znodes", cnt);
654 ubifs_assert(cnt == atomic_long_read(&c->dirty_zn_cnt));
655 return cnt;
659 * alloc_idx_lebs - allocate empty LEBs to be used to commit.
660 * @c: UBIFS file-system description object
661 * @cnt: number of znodes to commit
663 * This function returns %-ENOSPC if it cannot allocate a sufficient number of
664 * empty LEBs. %0 is returned on success, otherwise a negative error code
665 * is returned.
667 static int alloc_idx_lebs(struct ubifs_info *c, int cnt)
669 int i, leb_cnt, lnum;
671 c->ileb_cnt = 0;
672 c->ileb_nxt = 0;
673 leb_cnt = get_leb_cnt(c, cnt);
674 dbg_cmt("need about %d empty LEBS for TNC commit", leb_cnt);
675 if (!leb_cnt)
676 return 0;
677 c->ilebs = kmalloc(leb_cnt * sizeof(int), GFP_NOFS);
678 if (!c->ilebs)
679 return -ENOMEM;
680 for (i = 0; i < leb_cnt; i++) {
681 lnum = ubifs_find_free_leb_for_idx(c);
682 if (lnum < 0)
683 return lnum;
684 c->ilebs[c->ileb_cnt++] = lnum;
685 dbg_cmt("LEB %d", lnum);
687 if (dbg_is_chk_index(c) && !(random32() & 7))
688 return -ENOSPC;
689 return 0;
693 * free_unused_idx_lebs - free unused LEBs that were allocated for the commit.
694 * @c: UBIFS file-system description object
696 * It is possible that we allocate more empty LEBs for the commit than we need.
697 * This functions frees the surplus.
699 * This function returns %0 on success and a negative error code on failure.
701 static int free_unused_idx_lebs(struct ubifs_info *c)
703 int i, err = 0, lnum, er;
705 for (i = c->ileb_nxt; i < c->ileb_cnt; i++) {
706 lnum = c->ilebs[i];
707 dbg_cmt("LEB %d", lnum);
708 er = ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0,
709 LPROPS_INDEX | LPROPS_TAKEN, 0);
710 if (!err)
711 err = er;
713 return err;
717 * free_idx_lebs - free unused LEBs after commit end.
718 * @c: UBIFS file-system description object
720 * This function returns %0 on success and a negative error code on failure.
722 static int free_idx_lebs(struct ubifs_info *c)
724 int err;
726 err = free_unused_idx_lebs(c);
727 kfree(c->ilebs);
728 c->ilebs = NULL;
729 return err;
733 * ubifs_tnc_start_commit - start TNC commit.
734 * @c: UBIFS file-system description object
735 * @zroot: new index root position is returned here
737 * This function prepares the list of indexing nodes to commit and lays out
738 * their positions on flash. If there is not enough free space it uses the
739 * in-gap commit method. Returns zero in case of success and a negative error
740 * code in case of failure.
742 int ubifs_tnc_start_commit(struct ubifs_info *c, struct ubifs_zbranch *zroot)
744 int err = 0, cnt;
746 mutex_lock(&c->tnc_mutex);
747 err = dbg_check_tnc(c, 1);
748 if (err)
749 goto out;
750 cnt = get_znodes_to_commit(c);
751 if (cnt != 0) {
752 int no_space = 0;
754 err = alloc_idx_lebs(c, cnt);
755 if (err == -ENOSPC)
756 no_space = 1;
757 else if (err)
758 goto out_free;
759 err = layout_commit(c, no_space, cnt);
760 if (err)
761 goto out_free;
762 ubifs_assert(atomic_long_read(&c->dirty_zn_cnt) == 0);
763 err = free_unused_idx_lebs(c);
764 if (err)
765 goto out;
767 destroy_old_idx(c);
768 memcpy(zroot, &c->zroot, sizeof(struct ubifs_zbranch));
770 err = ubifs_save_dirty_idx_lnums(c);
771 if (err)
772 goto out;
774 spin_lock(&c->space_lock);
776 * Although we have not finished committing yet, update size of the
777 * committed index ('c->bi.old_idx_sz') and zero out the index growth
778 * budget. It is OK to do this now, because we've reserved all the
779 * space which is needed to commit the index, and it is save for the
780 * budgeting subsystem to assume the index is already committed,
781 * even though it is not.
783 ubifs_assert(c->bi.min_idx_lebs == ubifs_calc_min_idx_lebs(c));
784 c->bi.old_idx_sz = c->calc_idx_sz;
785 c->bi.uncommitted_idx = 0;
786 c->bi.min_idx_lebs = ubifs_calc_min_idx_lebs(c);
787 spin_unlock(&c->space_lock);
788 mutex_unlock(&c->tnc_mutex);
790 dbg_cmt("number of index LEBs %d", c->lst.idx_lebs);
791 dbg_cmt("size of index %llu", c->calc_idx_sz);
792 return err;
794 out_free:
795 free_idx_lebs(c);
796 out:
797 mutex_unlock(&c->tnc_mutex);
798 return err;
802 * write_index - write index nodes.
803 * @c: UBIFS file-system description object
805 * This function writes the index nodes whose positions were laid out in the
806 * layout_in_empty_space function.
808 static int write_index(struct ubifs_info *c)
810 struct ubifs_idx_node *idx;
811 struct ubifs_znode *znode, *cnext;
812 int i, lnum, offs, len, next_len, buf_len, buf_offs, used;
813 int avail, wlen, err, lnum_pos = 0, blen, nxt_offs;
815 cnext = c->enext;
816 if (!cnext)
817 return 0;
820 * Always write index nodes to the index head so that index nodes and
821 * other types of nodes are never mixed in the same erase block.
823 lnum = c->ihead_lnum;
824 buf_offs = c->ihead_offs;
826 /* Allocate commit buffer */
827 buf_len = ALIGN(c->max_idx_node_sz, c->min_io_size);
828 used = 0;
829 avail = buf_len;
831 /* Ensure there is enough room for first write */
832 next_len = ubifs_idx_node_sz(c, cnext->child_cnt);
833 if (buf_offs + next_len > c->leb_size) {
834 err = ubifs_update_one_lp(c, lnum, LPROPS_NC, 0, 0,
835 LPROPS_TAKEN);
836 if (err)
837 return err;
838 lnum = -1;
841 while (1) {
842 cond_resched();
844 znode = cnext;
845 idx = c->cbuf + used;
847 /* Make index node */
848 idx->ch.node_type = UBIFS_IDX_NODE;
849 idx->child_cnt = cpu_to_le16(znode->child_cnt);
850 idx->level = cpu_to_le16(znode->level);
851 for (i = 0; i < znode->child_cnt; i++) {
852 struct ubifs_branch *br = ubifs_idx_branch(c, idx, i);
853 struct ubifs_zbranch *zbr = &znode->zbranch[i];
855 key_write_idx(c, &zbr->key, &br->key);
856 br->lnum = cpu_to_le32(zbr->lnum);
857 br->offs = cpu_to_le32(zbr->offs);
858 br->len = cpu_to_le32(zbr->len);
859 if (!zbr->lnum || !zbr->len) {
860 ubifs_err("bad ref in znode");
861 ubifs_dump_znode(c, znode);
862 if (zbr->znode)
863 ubifs_dump_znode(c, zbr->znode);
866 len = ubifs_idx_node_sz(c, znode->child_cnt);
867 ubifs_prepare_node(c, idx, len, 0);
869 /* Determine the index node position */
870 if (lnum == -1) {
871 lnum = c->ilebs[lnum_pos++];
872 buf_offs = 0;
873 used = 0;
874 avail = buf_len;
876 offs = buf_offs + used;
878 if (lnum != znode->lnum || offs != znode->offs ||
879 len != znode->len) {
880 ubifs_err("inconsistent znode posn");
881 return -EINVAL;
884 /* Grab some stuff from znode while we still can */
885 cnext = znode->cnext;
887 ubifs_assert(ubifs_zn_dirty(znode));
888 ubifs_assert(ubifs_zn_cow(znode));
891 * It is important that other threads should see %DIRTY_ZNODE
892 * flag cleared before %COW_ZNODE. Specifically, it matters in
893 * the 'dirty_cow_znode()' function. This is the reason for the
894 * first barrier. Also, we want the bit changes to be seen to
895 * other threads ASAP, to avoid unnecesarry copying, which is
896 * the reason for the second barrier.
898 clear_bit(DIRTY_ZNODE, &znode->flags);
899 smp_mb__before_clear_bit();
900 clear_bit(COW_ZNODE, &znode->flags);
901 smp_mb__after_clear_bit();
904 * We have marked the znode as clean but have not updated the
905 * @c->clean_zn_cnt counter. If this znode becomes dirty again
906 * before 'free_obsolete_znodes()' is called, then
907 * @c->clean_zn_cnt will be decremented before it gets
908 * incremented (resulting in 2 decrements for the same znode).
909 * This means that @c->clean_zn_cnt may become negative for a
910 * while.
912 * Q: why we cannot increment @c->clean_zn_cnt?
913 * A: because we do not have the @c->tnc_mutex locked, and the
914 * following code would be racy and buggy:
916 * if (!ubifs_zn_obsolete(znode)) {
917 * atomic_long_inc(&c->clean_zn_cnt);
918 * atomic_long_inc(&ubifs_clean_zn_cnt);
921 * Thus, we just delay the @c->clean_zn_cnt update until we
922 * have the mutex locked.
925 /* Do not access znode from this point on */
927 /* Update buffer positions */
928 wlen = used + len;
929 used += ALIGN(len, 8);
930 avail -= ALIGN(len, 8);
933 * Calculate the next index node length to see if there is
934 * enough room for it
936 if (cnext == c->cnext)
937 next_len = 0;
938 else
939 next_len = ubifs_idx_node_sz(c, cnext->child_cnt);
941 nxt_offs = buf_offs + used + next_len;
942 if (next_len && nxt_offs <= c->leb_size) {
943 if (avail > 0)
944 continue;
945 else
946 blen = buf_len;
947 } else {
948 wlen = ALIGN(wlen, 8);
949 blen = ALIGN(wlen, c->min_io_size);
950 ubifs_pad(c, c->cbuf + wlen, blen - wlen);
953 /* The buffer is full or there are no more znodes to do */
954 err = ubifs_leb_write(c, lnum, c->cbuf, buf_offs, blen,
955 UBI_SHORTTERM);
956 if (err)
957 return err;
958 buf_offs += blen;
959 if (next_len) {
960 if (nxt_offs > c->leb_size) {
961 err = ubifs_update_one_lp(c, lnum, LPROPS_NC, 0,
962 0, LPROPS_TAKEN);
963 if (err)
964 return err;
965 lnum = -1;
967 used -= blen;
968 if (used < 0)
969 used = 0;
970 avail = buf_len - used;
971 memmove(c->cbuf, c->cbuf + blen, used);
972 continue;
974 break;
977 if (lnum != c->dbg->new_ihead_lnum ||
978 buf_offs != c->dbg->new_ihead_offs) {
979 ubifs_err("inconsistent ihead");
980 return -EINVAL;
983 c->ihead_lnum = lnum;
984 c->ihead_offs = buf_offs;
986 return 0;
990 * free_obsolete_znodes - free obsolete znodes.
991 * @c: UBIFS file-system description object
993 * At the end of commit end, obsolete znodes are freed.
995 static void free_obsolete_znodes(struct ubifs_info *c)
997 struct ubifs_znode *znode, *cnext;
999 cnext = c->cnext;
1000 do {
1001 znode = cnext;
1002 cnext = znode->cnext;
1003 if (ubifs_zn_obsolete(znode))
1004 kfree(znode);
1005 else {
1006 znode->cnext = NULL;
1007 atomic_long_inc(&c->clean_zn_cnt);
1008 atomic_long_inc(&ubifs_clean_zn_cnt);
1010 } while (cnext != c->cnext);
1014 * return_gap_lebs - return LEBs used by the in-gap commit method.
1015 * @c: UBIFS file-system description object
1017 * This function clears the "taken" flag for the LEBs which were used by the
1018 * "commit in-the-gaps" method.
1020 static int return_gap_lebs(struct ubifs_info *c)
1022 int *p, err;
1024 if (!c->gap_lebs)
1025 return 0;
1027 dbg_cmt("");
1028 for (p = c->gap_lebs; *p != -1; p++) {
1029 err = ubifs_change_one_lp(c, *p, LPROPS_NC, LPROPS_NC, 0,
1030 LPROPS_TAKEN, 0);
1031 if (err)
1032 return err;
1035 kfree(c->gap_lebs);
1036 c->gap_lebs = NULL;
1037 return 0;
1041 * ubifs_tnc_end_commit - update the TNC for commit end.
1042 * @c: UBIFS file-system description object
1044 * Write the dirty znodes.
1046 int ubifs_tnc_end_commit(struct ubifs_info *c)
1048 int err;
1050 if (!c->cnext)
1051 return 0;
1053 err = return_gap_lebs(c);
1054 if (err)
1055 return err;
1057 err = write_index(c);
1058 if (err)
1059 return err;
1061 mutex_lock(&c->tnc_mutex);
1063 dbg_cmt("TNC height is %d", c->zroot.znode->level + 1);
1065 free_obsolete_znodes(c);
1067 c->cnext = NULL;
1068 kfree(c->ilebs);
1069 c->ilebs = NULL;
1071 mutex_unlock(&c->tnc_mutex);
1073 return 0;