Linux Audio

Check our new training course

Loading...
v5.4
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * This file is part of UBIFS.
   4 *
   5 * Copyright (C) 2006-2008 Nokia Corporation.
   6 *
 
 
 
 
 
 
 
 
 
 
 
 
 
   7 * Authors: Adrian Hunter
   8 *          Artem Bityutskiy (Битюцкий Артём)
   9 */
  10
  11/* This file implements TNC functions for committing */
  12
  13#include <linux/random.h>
  14#include "ubifs.h"
  15
  16/**
  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
  24 */
  25static int make_idx_node(struct ubifs_info *c, struct ubifs_idx_node *idx,
  26			 struct ubifs_znode *znode, int lnum, int offs, int len)
  27{
  28	struct ubifs_znode *zp;
  29	u8 hash[UBIFS_HASH_ARR_SZ];
  30	int i, err;
  31
  32	/* Make index node */
  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];
  39
  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);
  48			if (zbr->znode)
  49				ubifs_dump_znode(c, zbr->znode);
  50
  51			return -EINVAL;
  52		}
  53	}
  54	ubifs_prepare_node(c, idx, len, 0);
  55	ubifs_node_calc_hash(c, idx, hash);
  56
 
  57	znode->lnum = lnum;
  58	znode->offs = offs;
  59	znode->len = len;
 
  60
  61	err = insert_old_idx_znode(c, znode);
  62
  63	/* Update the parent */
  64	zp = znode->parent;
  65	if (zp) {
  66		struct ubifs_zbranch *zbr;
  67
  68		zbr = &zp->zbranch[znode->iip];
  69		zbr->lnum = lnum;
  70		zbr->offs = offs;
  71		zbr->len = len;
  72		ubifs_copy_hash(c, hash, zbr->hash);
  73	} else {
  74		c->zroot.lnum = lnum;
  75		c->zroot.offs = offs;
  76		c->zroot.len = len;
  77		ubifs_copy_hash(c, hash, c->zroot.hash);
  78	}
  79	c->calc_idx_sz += ALIGN(len, 8);
  80
  81	atomic_long_dec(&c->dirty_zn_cnt);
  82
  83	ubifs_assert(c, ubifs_zn_dirty(znode));
  84	ubifs_assert(c, ubifs_zn_cow(znode));
  85
  86	/*
  87	 * Note, unlike 'write_index()' we do not add memory barriers here
  88	 * because this function is called with @c->tnc_mutex locked.
  89	 */
  90	__clear_bit(DIRTY_ZNODE, &znode->flags);
  91	__clear_bit(COW_ZNODE, &znode->flags);
  92
  93	return err;
  94}
  95
  96/**
  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
 103 *
 104 * This function returns the number of index nodes written into the gap.
 105 */
 106static int fill_gap(struct ubifs_info *c, int lnum, int gap_start, int gap_end,
 107		    int *dirt)
 108{
 109	int len, gap_remains, gap_pos, written, pad_len;
 110
 111	ubifs_assert(c, (gap_start & 7) == 0);
 112	ubifs_assert(c, (gap_end & 7) == 0);
 113	ubifs_assert(c, gap_end >= gap_start);
 114
 115	gap_remains = gap_end - gap_start;
 116	if (!gap_remains)
 117		return 0;
 118	gap_pos = gap_start;
 119	written = 0;
 120	while (c->enext) {
 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);
 125			int err;
 126
 127			ubifs_assert(c, alen <= gap_remains);
 128			err = make_idx_node(c, c->ileb_buf + gap_pos, znode,
 129					    lnum, gap_pos, len);
 130			if (err)
 131				return err;
 132			gap_remains -= alen;
 133			gap_pos += alen;
 134			c->enext = znode->cnext;
 135			if (c->enext == c->cnext)
 136				c->enext = NULL;
 137			written += 1;
 138		} else
 139			break;
 140	}
 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;
 145	} else
 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);
 151	*dirt += pad_len;
 152	return written;
 153}
 154
 155/**
 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
 160 *
 161 * Returns %1 if found and %0 otherwise.
 162 */
 163static int find_old_idx(struct ubifs_info *c, int lnum, int offs)
 164{
 165	struct ubifs_old_idx *o;
 166	struct rb_node *p;
 167
 168	p = c->old_idx.rb_node;
 169	while (p) {
 170		o = rb_entry(p, struct ubifs_old_idx, rb);
 171		if (lnum < o->lnum)
 172			p = p->rb_left;
 173		else if (lnum > o->lnum)
 174			p = p->rb_right;
 175		else if (offs < o->offs)
 176			p = p->rb_left;
 177		else if (offs > o->offs)
 178			p = p->rb_right;
 179		else
 180			return 1;
 181	}
 182	return 0;
 183}
 184
 185/**
 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
 192 *
 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.
 197 */
 198static int is_idx_node_in_use(struct ubifs_info *c, union ubifs_key *key,
 199			      int level, int lnum, int offs)
 200{
 201	int ret;
 202
 203	ret = is_idx_node_in_tnc(c, key, level, lnum, offs);
 204	if (ret < 0)
 205		return ret; /* Error code */
 206	if (ret == 0)
 207		if (find_old_idx(c, lnum, offs))
 208			return 1;
 209	return ret;
 210}
 211
 212/**
 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 here
 216 *
 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.
 223 */
 224static int layout_leb_in_gaps(struct ubifs_info *c, int *p)
 225{
 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;
 229
 230	tot_written = 0;
 231	/* Get an index LEB with lots of obsolete index nodes */
 232	lnum = ubifs_find_dirty_idx_leb(c);
 233	if (lnum < 0)
 234		/*
 235		 * There also may be dirt in the index head that could be
 236		 * filled, however we do not check there at present.
 237		 */
 238		return lnum; /* Error code */
 239	*p = lnum;
 240	dbg_gc("LEB %d", lnum);
 241	/*
 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
 244	 * purpose.
 245	 */
 246	sleb = ubifs_scan(c, lnum, 0, c->ileb_buf, 0);
 247	c->ileb_len = 0;
 248	if (IS_ERR(sleb))
 249		return PTR_ERR(sleb);
 250	gap_start = 0;
 251	list_for_each_entry(snod, &sleb->nodes, list) {
 252		struct ubifs_idx_node *idx;
 253		int in_use, level;
 254
 255		ubifs_assert(c, snod->type == UBIFS_IDX_NODE);
 256		idx = snod->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,
 261					    snod->offs);
 262		if (in_use < 0) {
 263			ubifs_scan_destroy(sleb);
 264			return in_use; /* Error code */
 265		}
 266		if (in_use) {
 267			if (in_use == 1)
 268				dirt += ALIGN(snod->len, 8);
 269			/*
 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
 273			 * i.e. not obsolete
 274			 */
 275			gap_end = snod->offs;
 276			/* Try to fill gap */
 277			written = fill_gap(c, lnum, gap_start, gap_end, &dirt);
 278			if (written < 0) {
 279				ubifs_scan_destroy(sleb);
 280				return written; /* Error code */
 281			}
 282			tot_written += written;
 283			gap_start = ALIGN(snod->offs + snod->len, 8);
 284		}
 285	}
 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);
 291	if (written < 0)
 292		return written; /* Error code */
 293	tot_written += written;
 294	if (tot_written == 0) {
 295		struct ubifs_lprops lp;
 296
 297		dbg_gc("LEB %d wrote %d index nodes", lnum, tot_written);
 298		err = ubifs_read_one_lp(c, lnum, &lp);
 299		if (err)
 300			return err;
 301		if (lp.free == c->leb_size) {
 302			/*
 303			 * We must have snatched this LEB from the idx_gc list
 304			 * so we need to correct the free and dirty space.
 305			 */
 306			err = ubifs_change_one_lp(c, lnum,
 307						  c->leb_size - c->ileb_len,
 308						  dirt, 0, 0, 0);
 309			if (err)
 310				return err;
 311		}
 312		return 0;
 313	}
 314	err = ubifs_change_one_lp(c, lnum, c->leb_size - c->ileb_len, dirt,
 315				  0, 0, 0);
 316	if (err)
 317		return err;
 318	err = ubifs_leb_change(c, lnum, c->ileb_buf, c->ileb_len);
 
 319	if (err)
 320		return err;
 321	dbg_gc("LEB %d wrote %d index nodes", lnum, tot_written);
 322	return tot_written;
 323}
 324
 325/**
 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
 329 *
 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
 332 * needed.
 333 */
 334static int get_leb_cnt(struct ubifs_info *c, int cnt)
 335{
 336	int d;
 337
 338	/* Assume maximum index node size (i.e. overestimate space needed) */
 339	cnt -= (c->leb_size - c->ihead_offs) / c->max_idx_node_sz;
 340	if (cnt < 0)
 341		cnt = 0;
 342	d = c->leb_size / c->max_idx_node_sz;
 343	return DIV_ROUND_UP(cnt, d);
 344}
 345
 346/**
 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.
 350 *
 351 * This function lays out new index nodes for dirty znodes using in-the-gaps
 352 * method of TNC commit.
 353 *
 354 * This function returns %0 on success and a negative error code on failure.
 355 */
 356static int layout_in_gaps(struct ubifs_info *c, int cnt)
 357{
 358	int err, leb_needed_cnt, written, *p;
 359
 360	dbg_gc("%d znodes to write", cnt);
 361
 362	c->gap_lebs = kmalloc_array(c->lst.idx_lebs + 1, sizeof(int),
 363				    GFP_NOFS);
 364	if (!c->gap_lebs)
 365		return -ENOMEM;
 366
 367	p = c->gap_lebs;
 368	do {
 369		ubifs_assert(c, p < c->gap_lebs + c->lst.idx_lebs);
 370		written = layout_leb_in_gaps(c, p);
 371		if (written < 0) {
 372			err = written;
 373			if (err != -ENOSPC) {
 374				kfree(c->gap_lebs);
 375				c->gap_lebs = NULL;
 376				return err;
 377			}
 378			if (!dbg_is_chk_index(c)) {
 379				/*
 380				 * Do not print scary warnings if the debugging
 381				 * option which forces in-the-gaps is enabled.
 382				 */
 383				ubifs_warn(c, "out of space");
 384				ubifs_dump_budg(c, &c->bi);
 385				ubifs_dump_lprops(c);
 386			}
 387			/* Try to commit anyway */
 
 388			break;
 389		}
 390		p++;
 391		cnt -= written;
 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);
 395	} while (leb_needed_cnt > c->ileb_cnt);
 396
 397	*p = -1;
 398	return 0;
 399}
 400
 401/**
 402 * layout_in_empty_space - layout index nodes in empty space.
 403 * @c: UBIFS file-system description object
 404 *
 405 * This function lays out new index nodes for dirty znodes using empty LEBs.
 406 *
 407 * This function returns %0 on success and a negative error code on failure.
 408 */
 409static int layout_in_empty_space(struct ubifs_info *c)
 410{
 411	struct ubifs_znode *znode, *cnext, *zp;
 412	int lnum, offs, len, next_len, buf_len, buf_offs, used, avail;
 413	int wlen, blen, err;
 414
 415	cnext = c->enext;
 416	if (!cnext)
 417		return 0;
 418
 419	lnum = c->ihead_lnum;
 420	buf_offs = c->ihead_offs;
 421
 422	buf_len = ubifs_idx_node_sz(c, c->fanout);
 423	buf_len = ALIGN(buf_len, c->min_io_size);
 424	used = 0;
 425	avail = buf_len;
 426
 427	/* Ensure there is enough room for first write */
 428	next_len = ubifs_idx_node_sz(c, cnext->child_cnt);
 429	if (buf_offs + next_len > c->leb_size)
 430		lnum = -1;
 431
 432	while (1) {
 433		znode = cnext;
 434
 435		len = ubifs_idx_node_sz(c, znode->child_cnt);
 436
 437		/* Determine the index node position */
 438		if (lnum == -1) {
 439			if (c->ileb_nxt >= c->ileb_cnt) {
 440				ubifs_err(c, "out of space");
 441				return -ENOSPC;
 442			}
 443			lnum = c->ilebs[c->ileb_nxt++];
 444			buf_offs = 0;
 445			used = 0;
 446			avail = buf_len;
 447		}
 448
 449		offs = buf_offs + used;
 450
 
 451		znode->lnum = lnum;
 452		znode->offs = offs;
 453		znode->len = len;
 
 454
 455		/* Update the parent */
 456		zp = znode->parent;
 457		if (zp) {
 458			struct ubifs_zbranch *zbr;
 459			int i;
 460
 461			i = znode->iip;
 462			zbr = &zp->zbranch[i];
 463			zbr->lnum = lnum;
 464			zbr->offs = offs;
 465			zbr->len = len;
 466		} else {
 467			c->zroot.lnum = lnum;
 468			c->zroot.offs = offs;
 469			c->zroot.len = len;
 470		}
 471		c->calc_idx_sz += ALIGN(len, 8);
 472
 473		/*
 474		 * Once lprops is updated, we can decrease the dirty znode count
 475		 * but it is easier to just do it here.
 476		 */
 477		atomic_long_dec(&c->dirty_zn_cnt);
 478
 479		/*
 480		 * Calculate the next index node length to see if there is
 481		 * enough room for it
 482		 */
 483		cnext = znode->cnext;
 484		if (cnext == c->cnext)
 485			next_len = 0;
 486		else
 487			next_len = ubifs_idx_node_sz(c, cnext->child_cnt);
 488
 489		/* Update buffer positions */
 490		wlen = used + len;
 491		used += ALIGN(len, 8);
 492		avail -= ALIGN(len, 8);
 493
 494		if (next_len != 0 &&
 495		    buf_offs + used + next_len <= c->leb_size &&
 496		    avail > 0)
 497			continue;
 498
 499		if (avail <= 0 && next_len &&
 500		    buf_offs + used + next_len <= c->leb_size)
 501			blen = buf_len;
 502		else
 503			blen = ALIGN(wlen, c->min_io_size);
 504
 505		/* The buffer is full or there are no more znodes to do */
 506		buf_offs += blen;
 507		if (next_len) {
 508			if (buf_offs + next_len > c->leb_size) {
 509				err = ubifs_update_one_lp(c, lnum,
 510					c->leb_size - buf_offs, blen - used,
 511					0, 0);
 512				if (err)
 513					return err;
 514				lnum = -1;
 515			}
 516			used -= blen;
 517			if (used < 0)
 518				used = 0;
 519			avail = buf_len - used;
 520			continue;
 521		}
 522		err = ubifs_update_one_lp(c, lnum, c->leb_size - buf_offs,
 523					  blen - used, 0, 0);
 524		if (err)
 525			return err;
 526		break;
 527	}
 528
 
 529	c->dbg->new_ihead_lnum = lnum;
 530	c->dbg->new_ihead_offs = buf_offs;
 
 531
 532	return 0;
 533}
 534
 535/**
 536 * layout_commit - determine positions of index nodes to commit.
 537 * @c: UBIFS file-system description object
 538 * @no_space: indicates that insufficient empty LEBs were allocated
 539 * @cnt: number of znodes to commit
 540 *
 541 * Calculate and update the positions of index nodes to commit.  If there were
 542 * an insufficient number of empty LEBs allocated, then index nodes are placed
 543 * into the gaps created by obsolete index nodes in non-empty index LEBs.  For
 544 * this purpose, an obsolete index node is one that was not in the index as at
 545 * the end of the last commit.  To write "in-the-gaps" requires that those index
 546 * LEBs are updated atomically in-place.
 547 */
 548static int layout_commit(struct ubifs_info *c, int no_space, int cnt)
 549{
 550	int err;
 551
 552	if (no_space) {
 553		err = layout_in_gaps(c, cnt);
 554		if (err)
 555			return err;
 556	}
 557	err = layout_in_empty_space(c);
 558	return err;
 559}
 560
 561/**
 562 * find_first_dirty - find first dirty znode.
 563 * @znode: znode to begin searching from
 564 */
 565static struct ubifs_znode *find_first_dirty(struct ubifs_znode *znode)
 566{
 567	int i, cont;
 568
 569	if (!znode)
 570		return NULL;
 571
 572	while (1) {
 573		if (znode->level == 0) {
 574			if (ubifs_zn_dirty(znode))
 575				return znode;
 576			return NULL;
 577		}
 578		cont = 0;
 579		for (i = 0; i < znode->child_cnt; i++) {
 580			struct ubifs_zbranch *zbr = &znode->zbranch[i];
 581
 582			if (zbr->znode && ubifs_zn_dirty(zbr->znode)) {
 583				znode = zbr->znode;
 584				cont = 1;
 585				break;
 586			}
 587		}
 588		if (!cont) {
 589			if (ubifs_zn_dirty(znode))
 590				return znode;
 591			return NULL;
 592		}
 593	}
 594}
 595
 596/**
 597 * find_next_dirty - find next dirty znode.
 598 * @znode: znode to begin searching from
 599 */
 600static struct ubifs_znode *find_next_dirty(struct ubifs_znode *znode)
 601{
 602	int n = znode->iip + 1;
 603
 604	znode = znode->parent;
 605	if (!znode)
 606		return NULL;
 607	for (; n < znode->child_cnt; n++) {
 608		struct ubifs_zbranch *zbr = &znode->zbranch[n];
 609
 610		if (zbr->znode && ubifs_zn_dirty(zbr->znode))
 611			return find_first_dirty(zbr->znode);
 612	}
 613	return znode;
 614}
 615
 616/**
 617 * get_znodes_to_commit - create list of dirty znodes to commit.
 618 * @c: UBIFS file-system description object
 619 *
 620 * This function returns the number of znodes to commit.
 621 */
 622static int get_znodes_to_commit(struct ubifs_info *c)
 623{
 624	struct ubifs_znode *znode, *cnext;
 625	int cnt = 0;
 626
 627	c->cnext = find_first_dirty(c->zroot.znode);
 628	znode = c->enext = c->cnext;
 629	if (!znode) {
 630		dbg_cmt("no znodes to commit");
 631		return 0;
 632	}
 633	cnt += 1;
 634	while (1) {
 635		ubifs_assert(c, !ubifs_zn_cow(znode));
 636		__set_bit(COW_ZNODE, &znode->flags);
 637		znode->alt = 0;
 638		cnext = find_next_dirty(znode);
 639		if (!cnext) {
 640			znode->cnext = c->cnext;
 641			break;
 642		}
 643		znode->cparent = znode->parent;
 644		znode->ciip = znode->iip;
 645		znode->cnext = cnext;
 646		znode = cnext;
 647		cnt += 1;
 648	}
 649	dbg_cmt("committing %d znodes", cnt);
 650	ubifs_assert(c, cnt == atomic_long_read(&c->dirty_zn_cnt));
 651	return cnt;
 652}
 653
 654/**
 655 * alloc_idx_lebs - allocate empty LEBs to be used to commit.
 656 * @c: UBIFS file-system description object
 657 * @cnt: number of znodes to commit
 658 *
 659 * This function returns %-ENOSPC if it cannot allocate a sufficient number of
 660 * empty LEBs.  %0 is returned on success, otherwise a negative error code
 661 * is returned.
 662 */
 663static int alloc_idx_lebs(struct ubifs_info *c, int cnt)
 664{
 665	int i, leb_cnt, lnum;
 666
 667	c->ileb_cnt = 0;
 668	c->ileb_nxt = 0;
 669	leb_cnt = get_leb_cnt(c, cnt);
 670	dbg_cmt("need about %d empty LEBS for TNC commit", leb_cnt);
 671	if (!leb_cnt)
 672		return 0;
 673	c->ilebs = kmalloc_array(leb_cnt, sizeof(int), GFP_NOFS);
 674	if (!c->ilebs)
 675		return -ENOMEM;
 676	for (i = 0; i < leb_cnt; i++) {
 677		lnum = ubifs_find_free_leb_for_idx(c);
 678		if (lnum < 0)
 679			return lnum;
 680		c->ilebs[c->ileb_cnt++] = lnum;
 681		dbg_cmt("LEB %d", lnum);
 682	}
 683	if (dbg_is_chk_index(c) && !(prandom_u32() & 7))
 684		return -ENOSPC;
 685	return 0;
 686}
 687
 688/**
 689 * free_unused_idx_lebs - free unused LEBs that were allocated for the commit.
 690 * @c: UBIFS file-system description object
 691 *
 692 * It is possible that we allocate more empty LEBs for the commit than we need.
 693 * This functions frees the surplus.
 694 *
 695 * This function returns %0 on success and a negative error code on failure.
 696 */
 697static int free_unused_idx_lebs(struct ubifs_info *c)
 698{
 699	int i, err = 0, lnum, er;
 700
 701	for (i = c->ileb_nxt; i < c->ileb_cnt; i++) {
 702		lnum = c->ilebs[i];
 703		dbg_cmt("LEB %d", lnum);
 704		er = ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0,
 705					 LPROPS_INDEX | LPROPS_TAKEN, 0);
 706		if (!err)
 707			err = er;
 708	}
 709	return err;
 710}
 711
 712/**
 713 * free_idx_lebs - free unused LEBs after commit end.
 714 * @c: UBIFS file-system description object
 715 *
 716 * This function returns %0 on success and a negative error code on failure.
 717 */
 718static int free_idx_lebs(struct ubifs_info *c)
 719{
 720	int err;
 721
 722	err = free_unused_idx_lebs(c);
 723	kfree(c->ilebs);
 724	c->ilebs = NULL;
 725	return err;
 726}
 727
 728/**
 729 * ubifs_tnc_start_commit - start TNC commit.
 730 * @c: UBIFS file-system description object
 731 * @zroot: new index root position is returned here
 732 *
 733 * This function prepares the list of indexing nodes to commit and lays out
 734 * their positions on flash. If there is not enough free space it uses the
 735 * in-gap commit method. Returns zero in case of success and a negative error
 736 * code in case of failure.
 737 */
 738int ubifs_tnc_start_commit(struct ubifs_info *c, struct ubifs_zbranch *zroot)
 739{
 740	int err = 0, cnt;
 741
 742	mutex_lock(&c->tnc_mutex);
 743	err = dbg_check_tnc(c, 1);
 744	if (err)
 745		goto out;
 746	cnt = get_znodes_to_commit(c);
 747	if (cnt != 0) {
 748		int no_space = 0;
 749
 750		err = alloc_idx_lebs(c, cnt);
 751		if (err == -ENOSPC)
 752			no_space = 1;
 753		else if (err)
 754			goto out_free;
 755		err = layout_commit(c, no_space, cnt);
 756		if (err)
 757			goto out_free;
 758		ubifs_assert(c, atomic_long_read(&c->dirty_zn_cnt) == 0);
 759		err = free_unused_idx_lebs(c);
 760		if (err)
 761			goto out;
 762	}
 763	destroy_old_idx(c);
 764	memcpy(zroot, &c->zroot, sizeof(struct ubifs_zbranch));
 765
 766	err = ubifs_save_dirty_idx_lnums(c);
 767	if (err)
 768		goto out;
 769
 770	spin_lock(&c->space_lock);
 771	/*
 772	 * Although we have not finished committing yet, update size of the
 773	 * committed index ('c->bi.old_idx_sz') and zero out the index growth
 774	 * budget. It is OK to do this now, because we've reserved all the
 775	 * space which is needed to commit the index, and it is save for the
 776	 * budgeting subsystem to assume the index is already committed,
 777	 * even though it is not.
 778	 */
 779	ubifs_assert(c, c->bi.min_idx_lebs == ubifs_calc_min_idx_lebs(c));
 780	c->bi.old_idx_sz = c->calc_idx_sz;
 781	c->bi.uncommitted_idx = 0;
 782	c->bi.min_idx_lebs = ubifs_calc_min_idx_lebs(c);
 783	spin_unlock(&c->space_lock);
 784	mutex_unlock(&c->tnc_mutex);
 785
 786	dbg_cmt("number of index LEBs %d", c->lst.idx_lebs);
 787	dbg_cmt("size of index %llu", c->calc_idx_sz);
 788	return err;
 789
 790out_free:
 791	free_idx_lebs(c);
 792out:
 793	mutex_unlock(&c->tnc_mutex);
 794	return err;
 795}
 796
 797/**
 798 * write_index - write index nodes.
 799 * @c: UBIFS file-system description object
 800 *
 801 * This function writes the index nodes whose positions were laid out in the
 802 * layout_in_empty_space function.
 803 */
 804static int write_index(struct ubifs_info *c)
 805{
 806	struct ubifs_idx_node *idx;
 807	struct ubifs_znode *znode, *cnext;
 808	int i, lnum, offs, len, next_len, buf_len, buf_offs, used;
 809	int avail, wlen, err, lnum_pos = 0, blen, nxt_offs;
 810
 811	cnext = c->enext;
 812	if (!cnext)
 813		return 0;
 814
 815	/*
 816	 * Always write index nodes to the index head so that index nodes and
 817	 * other types of nodes are never mixed in the same erase block.
 818	 */
 819	lnum = c->ihead_lnum;
 820	buf_offs = c->ihead_offs;
 821
 822	/* Allocate commit buffer */
 823	buf_len = ALIGN(c->max_idx_node_sz, c->min_io_size);
 824	used = 0;
 825	avail = buf_len;
 826
 827	/* Ensure there is enough room for first write */
 828	next_len = ubifs_idx_node_sz(c, cnext->child_cnt);
 829	if (buf_offs + next_len > c->leb_size) {
 830		err = ubifs_update_one_lp(c, lnum, LPROPS_NC, 0, 0,
 831					  LPROPS_TAKEN);
 832		if (err)
 833			return err;
 834		lnum = -1;
 835	}
 836
 837	while (1) {
 838		u8 hash[UBIFS_HASH_ARR_SZ];
 839
 840		cond_resched();
 841
 842		znode = cnext;
 843		idx = c->cbuf + used;
 844
 845		/* Make index node */
 846		idx->ch.node_type = UBIFS_IDX_NODE;
 847		idx->child_cnt = cpu_to_le16(znode->child_cnt);
 848		idx->level = cpu_to_le16(znode->level);
 849		for (i = 0; i < znode->child_cnt; i++) {
 850			struct ubifs_branch *br = ubifs_idx_branch(c, idx, i);
 851			struct ubifs_zbranch *zbr = &znode->zbranch[i];
 852
 853			key_write_idx(c, &zbr->key, &br->key);
 854			br->lnum = cpu_to_le32(zbr->lnum);
 855			br->offs = cpu_to_le32(zbr->offs);
 856			br->len = cpu_to_le32(zbr->len);
 857			ubifs_copy_hash(c, zbr->hash, ubifs_branch_hash(c, br));
 858			if (!zbr->lnum || !zbr->len) {
 859				ubifs_err(c, "bad ref in znode");
 860				ubifs_dump_znode(c, znode);
 861				if (zbr->znode)
 862					ubifs_dump_znode(c, zbr->znode);
 863
 864				return -EINVAL;
 865			}
 866		}
 867		len = ubifs_idx_node_sz(c, znode->child_cnt);
 868		ubifs_prepare_node(c, idx, len, 0);
 869		ubifs_node_calc_hash(c, idx, hash);
 870
 871		mutex_lock(&c->tnc_mutex);
 872
 873		if (znode->cparent)
 874			ubifs_copy_hash(c, hash,
 875					znode->cparent->zbranch[znode->ciip].hash);
 876
 877		if (znode->parent) {
 878			if (!ubifs_zn_obsolete(znode))
 879				ubifs_copy_hash(c, hash,
 880					znode->parent->zbranch[znode->iip].hash);
 881		} else {
 882			ubifs_copy_hash(c, hash, c->zroot.hash);
 883		}
 884
 885		mutex_unlock(&c->tnc_mutex);
 886
 887		/* Determine the index node position */
 888		if (lnum == -1) {
 889			lnum = c->ilebs[lnum_pos++];
 890			buf_offs = 0;
 891			used = 0;
 892			avail = buf_len;
 893		}
 894		offs = buf_offs + used;
 895
 
 896		if (lnum != znode->lnum || offs != znode->offs ||
 897		    len != znode->len) {
 898			ubifs_err(c, "inconsistent znode posn");
 899			return -EINVAL;
 900		}
 
 901
 902		/* Grab some stuff from znode while we still can */
 903		cnext = znode->cnext;
 904
 905		ubifs_assert(c, ubifs_zn_dirty(znode));
 906		ubifs_assert(c, ubifs_zn_cow(znode));
 907
 908		/*
 909		 * It is important that other threads should see %DIRTY_ZNODE
 910		 * flag cleared before %COW_ZNODE. Specifically, it matters in
 911		 * the 'dirty_cow_znode()' function. This is the reason for the
 912		 * first barrier. Also, we want the bit changes to be seen to
 913		 * other threads ASAP, to avoid unnecesarry copying, which is
 914		 * the reason for the second barrier.
 915		 */
 916		clear_bit(DIRTY_ZNODE, &znode->flags);
 917		smp_mb__before_atomic();
 918		clear_bit(COW_ZNODE, &znode->flags);
 919		smp_mb__after_atomic();
 920
 921		/*
 922		 * We have marked the znode as clean but have not updated the
 923		 * @c->clean_zn_cnt counter. If this znode becomes dirty again
 924		 * before 'free_obsolete_znodes()' is called, then
 925		 * @c->clean_zn_cnt will be decremented before it gets
 926		 * incremented (resulting in 2 decrements for the same znode).
 927		 * This means that @c->clean_zn_cnt may become negative for a
 928		 * while.
 929		 *
 930		 * Q: why we cannot increment @c->clean_zn_cnt?
 931		 * A: because we do not have the @c->tnc_mutex locked, and the
 932		 *    following code would be racy and buggy:
 933		 *
 934		 *    if (!ubifs_zn_obsolete(znode)) {
 935		 *            atomic_long_inc(&c->clean_zn_cnt);
 936		 *            atomic_long_inc(&ubifs_clean_zn_cnt);
 937		 *    }
 938		 *
 939		 *    Thus, we just delay the @c->clean_zn_cnt update until we
 940		 *    have the mutex locked.
 941		 */
 942
 943		/* Do not access znode from this point on */
 944
 945		/* Update buffer positions */
 946		wlen = used + len;
 947		used += ALIGN(len, 8);
 948		avail -= ALIGN(len, 8);
 949
 950		/*
 951		 * Calculate the next index node length to see if there is
 952		 * enough room for it
 953		 */
 954		if (cnext == c->cnext)
 955			next_len = 0;
 956		else
 957			next_len = ubifs_idx_node_sz(c, cnext->child_cnt);
 958
 959		nxt_offs = buf_offs + used + next_len;
 960		if (next_len && nxt_offs <= c->leb_size) {
 961			if (avail > 0)
 962				continue;
 963			else
 964				blen = buf_len;
 965		} else {
 966			wlen = ALIGN(wlen, 8);
 967			blen = ALIGN(wlen, c->min_io_size);
 968			ubifs_pad(c, c->cbuf + wlen, blen - wlen);
 969		}
 970
 971		/* The buffer is full or there are no more znodes to do */
 972		err = ubifs_leb_write(c, lnum, c->cbuf, buf_offs, blen);
 
 973		if (err)
 974			return err;
 975		buf_offs += blen;
 976		if (next_len) {
 977			if (nxt_offs > c->leb_size) {
 978				err = ubifs_update_one_lp(c, lnum, LPROPS_NC, 0,
 979							  0, LPROPS_TAKEN);
 980				if (err)
 981					return err;
 982				lnum = -1;
 983			}
 984			used -= blen;
 985			if (used < 0)
 986				used = 0;
 987			avail = buf_len - used;
 988			memmove(c->cbuf, c->cbuf + blen, used);
 989			continue;
 990		}
 991		break;
 992	}
 993
 
 994	if (lnum != c->dbg->new_ihead_lnum ||
 995	    buf_offs != c->dbg->new_ihead_offs) {
 996		ubifs_err(c, "inconsistent ihead");
 997		return -EINVAL;
 998	}
 
 999
1000	c->ihead_lnum = lnum;
1001	c->ihead_offs = buf_offs;
1002
1003	return 0;
1004}
1005
1006/**
1007 * free_obsolete_znodes - free obsolete znodes.
1008 * @c: UBIFS file-system description object
1009 *
1010 * At the end of commit end, obsolete znodes are freed.
1011 */
1012static void free_obsolete_znodes(struct ubifs_info *c)
1013{
1014	struct ubifs_znode *znode, *cnext;
1015
1016	cnext = c->cnext;
1017	do {
1018		znode = cnext;
1019		cnext = znode->cnext;
1020		if (ubifs_zn_obsolete(znode))
1021			kfree(znode);
1022		else {
1023			znode->cnext = NULL;
1024			atomic_long_inc(&c->clean_zn_cnt);
1025			atomic_long_inc(&ubifs_clean_zn_cnt);
1026		}
1027	} while (cnext != c->cnext);
1028}
1029
1030/**
1031 * return_gap_lebs - return LEBs used by the in-gap commit method.
1032 * @c: UBIFS file-system description object
1033 *
1034 * This function clears the "taken" flag for the LEBs which were used by the
1035 * "commit in-the-gaps" method.
1036 */
1037static int return_gap_lebs(struct ubifs_info *c)
1038{
1039	int *p, err;
1040
1041	if (!c->gap_lebs)
1042		return 0;
1043
1044	dbg_cmt("");
1045	for (p = c->gap_lebs; *p != -1; p++) {
1046		err = ubifs_change_one_lp(c, *p, LPROPS_NC, LPROPS_NC, 0,
1047					  LPROPS_TAKEN, 0);
1048		if (err)
1049			return err;
1050	}
1051
1052	kfree(c->gap_lebs);
1053	c->gap_lebs = NULL;
1054	return 0;
1055}
1056
1057/**
1058 * ubifs_tnc_end_commit - update the TNC for commit end.
1059 * @c: UBIFS file-system description object
1060 *
1061 * Write the dirty znodes.
1062 */
1063int ubifs_tnc_end_commit(struct ubifs_info *c)
1064{
1065	int err;
1066
1067	if (!c->cnext)
1068		return 0;
1069
1070	err = return_gap_lebs(c);
1071	if (err)
1072		return err;
1073
1074	err = write_index(c);
1075	if (err)
1076		return err;
1077
1078	mutex_lock(&c->tnc_mutex);
1079
1080	dbg_cmt("TNC height is %d", c->zroot.znode->level + 1);
1081
1082	free_obsolete_znodes(c);
1083
1084	c->cnext = NULL;
1085	kfree(c->ilebs);
1086	c->ilebs = NULL;
1087
1088	mutex_unlock(&c->tnc_mutex);
1089
1090	return 0;
1091}
v3.1
 
   1/*
   2 * This file is part of UBIFS.
   3 *
   4 * Copyright (C) 2006-2008 Nokia Corporation.
   5 *
   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.
   9 *
  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.
  14 *
  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
  18 *
  19 * Authors: Adrian Hunter
  20 *          Artem Bityutskiy (Битюцкий Артём)
  21 */
  22
  23/* This file implements TNC functions for committing */
  24
  25#include <linux/random.h>
  26#include "ubifs.h"
  27
  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
  36 */
  37static int make_idx_node(struct ubifs_info *c, struct ubifs_idx_node *idx,
  38			 struct ubifs_znode *znode, int lnum, int offs, int len)
  39{
  40	struct ubifs_znode *zp;
 
  41	int i, err;
  42
  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];
  50
  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			dbg_dump_znode(c, znode);
  58			if (zbr->znode)
  59				dbg_dump_znode(c, zbr->znode);
 
 
  60		}
  61	}
  62	ubifs_prepare_node(c, idx, len, 0);
 
  63
  64#ifdef CONFIG_UBIFS_FS_DEBUG
  65	znode->lnum = lnum;
  66	znode->offs = offs;
  67	znode->len = len;
  68#endif
  69
  70	err = insert_old_idx_znode(c, znode);
  71
  72	/* Update the parent */
  73	zp = znode->parent;
  74	if (zp) {
  75		struct ubifs_zbranch *zbr;
  76
  77		zbr = &zp->zbranch[znode->iip];
  78		zbr->lnum = lnum;
  79		zbr->offs = offs;
  80		zbr->len = len;
 
  81	} else {
  82		c->zroot.lnum = lnum;
  83		c->zroot.offs = offs;
  84		c->zroot.len = len;
 
  85	}
  86	c->calc_idx_sz += ALIGN(len, 8);
  87
  88	atomic_long_dec(&c->dirty_zn_cnt);
  89
  90	ubifs_assert(ubifs_zn_dirty(znode));
  91	ubifs_assert(ubifs_zn_cow(znode));
  92
  93	/*
  94	 * Note, unlike 'write_index()' we do not add memory barriers here
  95	 * because this function is called with @c->tnc_mutex locked.
  96	 */
  97	__clear_bit(DIRTY_ZNODE, &znode->flags);
  98	__clear_bit(COW_ZNODE, &znode->flags);
  99
 100	return err;
 101}
 102
 103/**
 104 * fill_gap - make index nodes in gaps in dirty index LEBs.
 105 * @c: UBIFS file-system description object
 106 * @lnum: LEB number that gap appears in
 107 * @gap_start: offset of start of gap
 108 * @gap_end: offset of end of gap
 109 * @dirt: adds dirty space to this
 110 *
 111 * This function returns the number of index nodes written into the gap.
 112 */
 113static int fill_gap(struct ubifs_info *c, int lnum, int gap_start, int gap_end,
 114		    int *dirt)
 115{
 116	int len, gap_remains, gap_pos, written, pad_len;
 117
 118	ubifs_assert((gap_start & 7) == 0);
 119	ubifs_assert((gap_end & 7) == 0);
 120	ubifs_assert(gap_end >= gap_start);
 121
 122	gap_remains = gap_end - gap_start;
 123	if (!gap_remains)
 124		return 0;
 125	gap_pos = gap_start;
 126	written = 0;
 127	while (c->enext) {
 128		len = ubifs_idx_node_sz(c, c->enext->child_cnt);
 129		if (len < gap_remains) {
 130			struct ubifs_znode *znode = c->enext;
 131			const int alen = ALIGN(len, 8);
 132			int err;
 133
 134			ubifs_assert(alen <= gap_remains);
 135			err = make_idx_node(c, c->ileb_buf + gap_pos, znode,
 136					    lnum, gap_pos, len);
 137			if (err)
 138				return err;
 139			gap_remains -= alen;
 140			gap_pos += alen;
 141			c->enext = znode->cnext;
 142			if (c->enext == c->cnext)
 143				c->enext = NULL;
 144			written += 1;
 145		} else
 146			break;
 147	}
 148	if (gap_end == c->leb_size) {
 149		c->ileb_len = ALIGN(gap_pos, c->min_io_size);
 150		/* Pad to end of min_io_size */
 151		pad_len = c->ileb_len - gap_pos;
 152	} else
 153		/* Pad to end of gap */
 154		pad_len = gap_remains;
 155	dbg_gc("LEB %d:%d to %d len %d nodes written %d wasted bytes %d",
 156	       lnum, gap_start, gap_end, gap_end - gap_start, written, pad_len);
 157	ubifs_pad(c, c->ileb_buf + gap_pos, pad_len);
 158	*dirt += pad_len;
 159	return written;
 160}
 161
 162/**
 163 * find_old_idx - find an index node obsoleted since the last commit start.
 164 * @c: UBIFS file-system description object
 165 * @lnum: LEB number of obsoleted index node
 166 * @offs: offset of obsoleted index node
 167 *
 168 * Returns %1 if found and %0 otherwise.
 169 */
 170static int find_old_idx(struct ubifs_info *c, int lnum, int offs)
 171{
 172	struct ubifs_old_idx *o;
 173	struct rb_node *p;
 174
 175	p = c->old_idx.rb_node;
 176	while (p) {
 177		o = rb_entry(p, struct ubifs_old_idx, rb);
 178		if (lnum < o->lnum)
 179			p = p->rb_left;
 180		else if (lnum > o->lnum)
 181			p = p->rb_right;
 182		else if (offs < o->offs)
 183			p = p->rb_left;
 184		else if (offs > o->offs)
 185			p = p->rb_right;
 186		else
 187			return 1;
 188	}
 189	return 0;
 190}
 191
 192/**
 193 * is_idx_node_in_use - determine if an index node can be overwritten.
 194 * @c: UBIFS file-system description object
 195 * @key: key of index node
 196 * @level: index node level
 197 * @lnum: LEB number of index node
 198 * @offs: offset of index node
 199 *
 200 * If @key / @lnum / @offs identify an index node that was not part of the old
 201 * index, then this function returns %0 (obsolete).  Else if the index node was
 202 * part of the old index but is now dirty %1 is returned, else if it is clean %2
 203 * is returned. A negative error code is returned on failure.
 204 */
 205static int is_idx_node_in_use(struct ubifs_info *c, union ubifs_key *key,
 206			      int level, int lnum, int offs)
 207{
 208	int ret;
 209
 210	ret = is_idx_node_in_tnc(c, key, level, lnum, offs);
 211	if (ret < 0)
 212		return ret; /* Error code */
 213	if (ret == 0)
 214		if (find_old_idx(c, lnum, offs))
 215			return 1;
 216	return ret;
 217}
 218
 219/**
 220 * layout_leb_in_gaps - layout index nodes using in-the-gaps method.
 221 * @c: UBIFS file-system description object
 222 * @p: return LEB number here
 223 *
 224 * This function lays out new index nodes for dirty znodes using in-the-gaps
 225 * method of TNC commit.
 226 * This function merely puts the next znode into the next gap, making no attempt
 227 * to try to maximise the number of znodes that fit.
 228 * This function returns the number of index nodes written into the gaps, or a
 229 * negative error code on failure.
 230 */
 231static int layout_leb_in_gaps(struct ubifs_info *c, int *p)
 232{
 233	struct ubifs_scan_leb *sleb;
 234	struct ubifs_scan_node *snod;
 235	int lnum, dirt = 0, gap_start, gap_end, err, written, tot_written;
 236
 237	tot_written = 0;
 238	/* Get an index LEB with lots of obsolete index nodes */
 239	lnum = ubifs_find_dirty_idx_leb(c);
 240	if (lnum < 0)
 241		/*
 242		 * There also may be dirt in the index head that could be
 243		 * filled, however we do not check there at present.
 244		 */
 245		return lnum; /* Error code */
 246	*p = lnum;
 247	dbg_gc("LEB %d", lnum);
 248	/*
 249	 * Scan the index LEB.  We use the generic scan for this even though
 250	 * it is more comprehensive and less efficient than is needed for this
 251	 * purpose.
 252	 */
 253	sleb = ubifs_scan(c, lnum, 0, c->ileb_buf, 0);
 254	c->ileb_len = 0;
 255	if (IS_ERR(sleb))
 256		return PTR_ERR(sleb);
 257	gap_start = 0;
 258	list_for_each_entry(snod, &sleb->nodes, list) {
 259		struct ubifs_idx_node *idx;
 260		int in_use, level;
 261
 262		ubifs_assert(snod->type == UBIFS_IDX_NODE);
 263		idx = snod->node;
 264		key_read(c, ubifs_idx_key(c, idx), &snod->key);
 265		level = le16_to_cpu(idx->level);
 266		/* Determine if the index node is in use (not obsolete) */
 267		in_use = is_idx_node_in_use(c, &snod->key, level, lnum,
 268					    snod->offs);
 269		if (in_use < 0) {
 270			ubifs_scan_destroy(sleb);
 271			return in_use; /* Error code */
 272		}
 273		if (in_use) {
 274			if (in_use == 1)
 275				dirt += ALIGN(snod->len, 8);
 276			/*
 277			 * The obsolete index nodes form gaps that can be
 278			 * overwritten.  This gap has ended because we have
 279			 * found an index node that is still in use
 280			 * i.e. not obsolete
 281			 */
 282			gap_end = snod->offs;
 283			/* Try to fill gap */
 284			written = fill_gap(c, lnum, gap_start, gap_end, &dirt);
 285			if (written < 0) {
 286				ubifs_scan_destroy(sleb);
 287				return written; /* Error code */
 288			}
 289			tot_written += written;
 290			gap_start = ALIGN(snod->offs + snod->len, 8);
 291		}
 292	}
 293	ubifs_scan_destroy(sleb);
 294	c->ileb_len = c->leb_size;
 295	gap_end = c->leb_size;
 296	/* Try to fill gap */
 297	written = fill_gap(c, lnum, gap_start, gap_end, &dirt);
 298	if (written < 0)
 299		return written; /* Error code */
 300	tot_written += written;
 301	if (tot_written == 0) {
 302		struct ubifs_lprops lp;
 303
 304		dbg_gc("LEB %d wrote %d index nodes", lnum, tot_written);
 305		err = ubifs_read_one_lp(c, lnum, &lp);
 306		if (err)
 307			return err;
 308		if (lp.free == c->leb_size) {
 309			/*
 310			 * We must have snatched this LEB from the idx_gc list
 311			 * so we need to correct the free and dirty space.
 312			 */
 313			err = ubifs_change_one_lp(c, lnum,
 314						  c->leb_size - c->ileb_len,
 315						  dirt, 0, 0, 0);
 316			if (err)
 317				return err;
 318		}
 319		return 0;
 320	}
 321	err = ubifs_change_one_lp(c, lnum, c->leb_size - c->ileb_len, dirt,
 322				  0, 0, 0);
 323	if (err)
 324		return err;
 325	err = ubifs_leb_change(c, lnum, c->ileb_buf, c->ileb_len,
 326			       UBI_SHORTTERM);
 327	if (err)
 328		return err;
 329	dbg_gc("LEB %d wrote %d index nodes", lnum, tot_written);
 330	return tot_written;
 331}
 332
 333/**
 334 * get_leb_cnt - calculate the number of empty LEBs needed to commit.
 335 * @c: UBIFS file-system description object
 336 * @cnt: number of znodes to commit
 337 *
 338 * This function returns the number of empty LEBs needed to commit @cnt znodes
 339 * to the current index head.  The number is not exact and may be more than
 340 * needed.
 341 */
 342static int get_leb_cnt(struct ubifs_info *c, int cnt)
 343{
 344	int d;
 345
 346	/* Assume maximum index node size (i.e. overestimate space needed) */
 347	cnt -= (c->leb_size - c->ihead_offs) / c->max_idx_node_sz;
 348	if (cnt < 0)
 349		cnt = 0;
 350	d = c->leb_size / c->max_idx_node_sz;
 351	return DIV_ROUND_UP(cnt, d);
 352}
 353
 354/**
 355 * layout_in_gaps - in-the-gaps method of committing TNC.
 356 * @c: UBIFS file-system description object
 357 * @cnt: number of dirty znodes to commit.
 358 *
 359 * This function lays out new index nodes for dirty znodes using in-the-gaps
 360 * method of TNC commit.
 361 *
 362 * This function returns %0 on success and a negative error code on failure.
 363 */
 364static int layout_in_gaps(struct ubifs_info *c, int cnt)
 365{
 366	int err, leb_needed_cnt, written, *p;
 367
 368	dbg_gc("%d znodes to write", cnt);
 369
 370	c->gap_lebs = kmalloc(sizeof(int) * (c->lst.idx_lebs + 1), GFP_NOFS);
 
 371	if (!c->gap_lebs)
 372		return -ENOMEM;
 373
 374	p = c->gap_lebs;
 375	do {
 376		ubifs_assert(p < c->gap_lebs + sizeof(int) * c->lst.idx_lebs);
 377		written = layout_leb_in_gaps(c, p);
 378		if (written < 0) {
 379			err = written;
 380			if (err != -ENOSPC) {
 381				kfree(c->gap_lebs);
 382				c->gap_lebs = NULL;
 383				return err;
 384			}
 385			if (!dbg_is_chk_index(c)) {
 386				/*
 387				 * Do not print scary warnings if the debugging
 388				 * option which forces in-the-gaps is enabled.
 389				 */
 390				ubifs_warn("out of space");
 391				dbg_dump_budg(c, &c->bi);
 392				dbg_dump_lprops(c);
 393			}
 394			/* Try to commit anyway */
 395			err = 0;
 396			break;
 397		}
 398		p++;
 399		cnt -= written;
 400		leb_needed_cnt = get_leb_cnt(c, cnt);
 401		dbg_gc("%d znodes remaining, need %d LEBs, have %d", cnt,
 402		       leb_needed_cnt, c->ileb_cnt);
 403	} while (leb_needed_cnt > c->ileb_cnt);
 404
 405	*p = -1;
 406	return 0;
 407}
 408
 409/**
 410 * layout_in_empty_space - layout index nodes in empty space.
 411 * @c: UBIFS file-system description object
 412 *
 413 * This function lays out new index nodes for dirty znodes using empty LEBs.
 414 *
 415 * This function returns %0 on success and a negative error code on failure.
 416 */
 417static int layout_in_empty_space(struct ubifs_info *c)
 418{
 419	struct ubifs_znode *znode, *cnext, *zp;
 420	int lnum, offs, len, next_len, buf_len, buf_offs, used, avail;
 421	int wlen, blen, err;
 422
 423	cnext = c->enext;
 424	if (!cnext)
 425		return 0;
 426
 427	lnum = c->ihead_lnum;
 428	buf_offs = c->ihead_offs;
 429
 430	buf_len = ubifs_idx_node_sz(c, c->fanout);
 431	buf_len = ALIGN(buf_len, c->min_io_size);
 432	used = 0;
 433	avail = buf_len;
 434
 435	/* Ensure there is enough room for first write */
 436	next_len = ubifs_idx_node_sz(c, cnext->child_cnt);
 437	if (buf_offs + next_len > c->leb_size)
 438		lnum = -1;
 439
 440	while (1) {
 441		znode = cnext;
 442
 443		len = ubifs_idx_node_sz(c, znode->child_cnt);
 444
 445		/* Determine the index node position */
 446		if (lnum == -1) {
 447			if (c->ileb_nxt >= c->ileb_cnt) {
 448				ubifs_err("out of space");
 449				return -ENOSPC;
 450			}
 451			lnum = c->ilebs[c->ileb_nxt++];
 452			buf_offs = 0;
 453			used = 0;
 454			avail = buf_len;
 455		}
 456
 457		offs = buf_offs + used;
 458
 459#ifdef CONFIG_UBIFS_FS_DEBUG
 460		znode->lnum = lnum;
 461		znode->offs = offs;
 462		znode->len = len;
 463#endif
 464
 465		/* Update the parent */
 466		zp = znode->parent;
 467		if (zp) {
 468			struct ubifs_zbranch *zbr;
 469			int i;
 470
 471			i = znode->iip;
 472			zbr = &zp->zbranch[i];
 473			zbr->lnum = lnum;
 474			zbr->offs = offs;
 475			zbr->len = len;
 476		} else {
 477			c->zroot.lnum = lnum;
 478			c->zroot.offs = offs;
 479			c->zroot.len = len;
 480		}
 481		c->calc_idx_sz += ALIGN(len, 8);
 482
 483		/*
 484		 * Once lprops is updated, we can decrease the dirty znode count
 485		 * but it is easier to just do it here.
 486		 */
 487		atomic_long_dec(&c->dirty_zn_cnt);
 488
 489		/*
 490		 * Calculate the next index node length to see if there is
 491		 * enough room for it
 492		 */
 493		cnext = znode->cnext;
 494		if (cnext == c->cnext)
 495			next_len = 0;
 496		else
 497			next_len = ubifs_idx_node_sz(c, cnext->child_cnt);
 498
 499		/* Update buffer positions */
 500		wlen = used + len;
 501		used += ALIGN(len, 8);
 502		avail -= ALIGN(len, 8);
 503
 504		if (next_len != 0 &&
 505		    buf_offs + used + next_len <= c->leb_size &&
 506		    avail > 0)
 507			continue;
 508
 509		if (avail <= 0 && next_len &&
 510		    buf_offs + used + next_len <= c->leb_size)
 511			blen = buf_len;
 512		else
 513			blen = ALIGN(wlen, c->min_io_size);
 514
 515		/* The buffer is full or there are no more znodes to do */
 516		buf_offs += blen;
 517		if (next_len) {
 518			if (buf_offs + next_len > c->leb_size) {
 519				err = ubifs_update_one_lp(c, lnum,
 520					c->leb_size - buf_offs, blen - used,
 521					0, 0);
 522				if (err)
 523					return err;
 524				lnum = -1;
 525			}
 526			used -= blen;
 527			if (used < 0)
 528				used = 0;
 529			avail = buf_len - used;
 530			continue;
 531		}
 532		err = ubifs_update_one_lp(c, lnum, c->leb_size - buf_offs,
 533					  blen - used, 0, 0);
 534		if (err)
 535			return err;
 536		break;
 537	}
 538
 539#ifdef CONFIG_UBIFS_FS_DEBUG
 540	c->dbg->new_ihead_lnum = lnum;
 541	c->dbg->new_ihead_offs = buf_offs;
 542#endif
 543
 544	return 0;
 545}
 546
 547/**
 548 * layout_commit - determine positions of index nodes to commit.
 549 * @c: UBIFS file-system description object
 550 * @no_space: indicates that insufficient empty LEBs were allocated
 551 * @cnt: number of znodes to commit
 552 *
 553 * Calculate and update the positions of index nodes to commit.  If there were
 554 * an insufficient number of empty LEBs allocated, then index nodes are placed
 555 * into the gaps created by obsolete index nodes in non-empty index LEBs.  For
 556 * this purpose, an obsolete index node is one that was not in the index as at
 557 * the end of the last commit.  To write "in-the-gaps" requires that those index
 558 * LEBs are updated atomically in-place.
 559 */
 560static int layout_commit(struct ubifs_info *c, int no_space, int cnt)
 561{
 562	int err;
 563
 564	if (no_space) {
 565		err = layout_in_gaps(c, cnt);
 566		if (err)
 567			return err;
 568	}
 569	err = layout_in_empty_space(c);
 570	return err;
 571}
 572
 573/**
 574 * find_first_dirty - find first dirty znode.
 575 * @znode: znode to begin searching from
 576 */
 577static struct ubifs_znode *find_first_dirty(struct ubifs_znode *znode)
 578{
 579	int i, cont;
 580
 581	if (!znode)
 582		return NULL;
 583
 584	while (1) {
 585		if (znode->level == 0) {
 586			if (ubifs_zn_dirty(znode))
 587				return znode;
 588			return NULL;
 589		}
 590		cont = 0;
 591		for (i = 0; i < znode->child_cnt; i++) {
 592			struct ubifs_zbranch *zbr = &znode->zbranch[i];
 593
 594			if (zbr->znode && ubifs_zn_dirty(zbr->znode)) {
 595				znode = zbr->znode;
 596				cont = 1;
 597				break;
 598			}
 599		}
 600		if (!cont) {
 601			if (ubifs_zn_dirty(znode))
 602				return znode;
 603			return NULL;
 604		}
 605	}
 606}
 607
 608/**
 609 * find_next_dirty - find next dirty znode.
 610 * @znode: znode to begin searching from
 611 */
 612static struct ubifs_znode *find_next_dirty(struct ubifs_znode *znode)
 613{
 614	int n = znode->iip + 1;
 615
 616	znode = znode->parent;
 617	if (!znode)
 618		return NULL;
 619	for (; n < znode->child_cnt; n++) {
 620		struct ubifs_zbranch *zbr = &znode->zbranch[n];
 621
 622		if (zbr->znode && ubifs_zn_dirty(zbr->znode))
 623			return find_first_dirty(zbr->znode);
 624	}
 625	return znode;
 626}
 627
 628/**
 629 * get_znodes_to_commit - create list of dirty znodes to commit.
 630 * @c: UBIFS file-system description object
 631 *
 632 * This function returns the number of znodes to commit.
 633 */
 634static int get_znodes_to_commit(struct ubifs_info *c)
 635{
 636	struct ubifs_znode *znode, *cnext;
 637	int cnt = 0;
 638
 639	c->cnext = find_first_dirty(c->zroot.znode);
 640	znode = c->enext = c->cnext;
 641	if (!znode) {
 642		dbg_cmt("no znodes to commit");
 643		return 0;
 644	}
 645	cnt += 1;
 646	while (1) {
 647		ubifs_assert(!ubifs_zn_cow(znode));
 648		__set_bit(COW_ZNODE, &znode->flags);
 649		znode->alt = 0;
 650		cnext = find_next_dirty(znode);
 651		if (!cnext) {
 652			znode->cnext = c->cnext;
 653			break;
 654		}
 
 
 655		znode->cnext = cnext;
 656		znode = cnext;
 657		cnt += 1;
 658	}
 659	dbg_cmt("committing %d znodes", cnt);
 660	ubifs_assert(cnt == atomic_long_read(&c->dirty_zn_cnt));
 661	return cnt;
 662}
 663
 664/**
 665 * alloc_idx_lebs - allocate empty LEBs to be used to commit.
 666 * @c: UBIFS file-system description object
 667 * @cnt: number of znodes to commit
 668 *
 669 * This function returns %-ENOSPC if it cannot allocate a sufficient number of
 670 * empty LEBs.  %0 is returned on success, otherwise a negative error code
 671 * is returned.
 672 */
 673static int alloc_idx_lebs(struct ubifs_info *c, int cnt)
 674{
 675	int i, leb_cnt, lnum;
 676
 677	c->ileb_cnt = 0;
 678	c->ileb_nxt = 0;
 679	leb_cnt = get_leb_cnt(c, cnt);
 680	dbg_cmt("need about %d empty LEBS for TNC commit", leb_cnt);
 681	if (!leb_cnt)
 682		return 0;
 683	c->ilebs = kmalloc(leb_cnt * sizeof(int), GFP_NOFS);
 684	if (!c->ilebs)
 685		return -ENOMEM;
 686	for (i = 0; i < leb_cnt; i++) {
 687		lnum = ubifs_find_free_leb_for_idx(c);
 688		if (lnum < 0)
 689			return lnum;
 690		c->ilebs[c->ileb_cnt++] = lnum;
 691		dbg_cmt("LEB %d", lnum);
 692	}
 693	if (dbg_is_chk_index(c) && !(random32() & 7))
 694		return -ENOSPC;
 695	return 0;
 696}
 697
 698/**
 699 * free_unused_idx_lebs - free unused LEBs that were allocated for the commit.
 700 * @c: UBIFS file-system description object
 701 *
 702 * It is possible that we allocate more empty LEBs for the commit than we need.
 703 * This functions frees the surplus.
 704 *
 705 * This function returns %0 on success and a negative error code on failure.
 706 */
 707static int free_unused_idx_lebs(struct ubifs_info *c)
 708{
 709	int i, err = 0, lnum, er;
 710
 711	for (i = c->ileb_nxt; i < c->ileb_cnt; i++) {
 712		lnum = c->ilebs[i];
 713		dbg_cmt("LEB %d", lnum);
 714		er = ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0,
 715					 LPROPS_INDEX | LPROPS_TAKEN, 0);
 716		if (!err)
 717			err = er;
 718	}
 719	return err;
 720}
 721
 722/**
 723 * free_idx_lebs - free unused LEBs after commit end.
 724 * @c: UBIFS file-system description object
 725 *
 726 * This function returns %0 on success and a negative error code on failure.
 727 */
 728static int free_idx_lebs(struct ubifs_info *c)
 729{
 730	int err;
 731
 732	err = free_unused_idx_lebs(c);
 733	kfree(c->ilebs);
 734	c->ilebs = NULL;
 735	return err;
 736}
 737
 738/**
 739 * ubifs_tnc_start_commit - start TNC commit.
 740 * @c: UBIFS file-system description object
 741 * @zroot: new index root position is returned here
 742 *
 743 * This function prepares the list of indexing nodes to commit and lays out
 744 * their positions on flash. If there is not enough free space it uses the
 745 * in-gap commit method. Returns zero in case of success and a negative error
 746 * code in case of failure.
 747 */
 748int ubifs_tnc_start_commit(struct ubifs_info *c, struct ubifs_zbranch *zroot)
 749{
 750	int err = 0, cnt;
 751
 752	mutex_lock(&c->tnc_mutex);
 753	err = dbg_check_tnc(c, 1);
 754	if (err)
 755		goto out;
 756	cnt = get_znodes_to_commit(c);
 757	if (cnt != 0) {
 758		int no_space = 0;
 759
 760		err = alloc_idx_lebs(c, cnt);
 761		if (err == -ENOSPC)
 762			no_space = 1;
 763		else if (err)
 764			goto out_free;
 765		err = layout_commit(c, no_space, cnt);
 766		if (err)
 767			goto out_free;
 768		ubifs_assert(atomic_long_read(&c->dirty_zn_cnt) == 0);
 769		err = free_unused_idx_lebs(c);
 770		if (err)
 771			goto out;
 772	}
 773	destroy_old_idx(c);
 774	memcpy(zroot, &c->zroot, sizeof(struct ubifs_zbranch));
 775
 776	err = ubifs_save_dirty_idx_lnums(c);
 777	if (err)
 778		goto out;
 779
 780	spin_lock(&c->space_lock);
 781	/*
 782	 * Although we have not finished committing yet, update size of the
 783	 * committed index ('c->bi.old_idx_sz') and zero out the index growth
 784	 * budget. It is OK to do this now, because we've reserved all the
 785	 * space which is needed to commit the index, and it is save for the
 786	 * budgeting subsystem to assume the index is already committed,
 787	 * even though it is not.
 788	 */
 789	ubifs_assert(c->bi.min_idx_lebs == ubifs_calc_min_idx_lebs(c));
 790	c->bi.old_idx_sz = c->calc_idx_sz;
 791	c->bi.uncommitted_idx = 0;
 792	c->bi.min_idx_lebs = ubifs_calc_min_idx_lebs(c);
 793	spin_unlock(&c->space_lock);
 794	mutex_unlock(&c->tnc_mutex);
 795
 796	dbg_cmt("number of index LEBs %d", c->lst.idx_lebs);
 797	dbg_cmt("size of index %llu", c->calc_idx_sz);
 798	return err;
 799
 800out_free:
 801	free_idx_lebs(c);
 802out:
 803	mutex_unlock(&c->tnc_mutex);
 804	return err;
 805}
 806
 807/**
 808 * write_index - write index nodes.
 809 * @c: UBIFS file-system description object
 810 *
 811 * This function writes the index nodes whose positions were laid out in the
 812 * layout_in_empty_space function.
 813 */
 814static int write_index(struct ubifs_info *c)
 815{
 816	struct ubifs_idx_node *idx;
 817	struct ubifs_znode *znode, *cnext;
 818	int i, lnum, offs, len, next_len, buf_len, buf_offs, used;
 819	int avail, wlen, err, lnum_pos = 0, blen, nxt_offs;
 820
 821	cnext = c->enext;
 822	if (!cnext)
 823		return 0;
 824
 825	/*
 826	 * Always write index nodes to the index head so that index nodes and
 827	 * other types of nodes are never mixed in the same erase block.
 828	 */
 829	lnum = c->ihead_lnum;
 830	buf_offs = c->ihead_offs;
 831
 832	/* Allocate commit buffer */
 833	buf_len = ALIGN(c->max_idx_node_sz, c->min_io_size);
 834	used = 0;
 835	avail = buf_len;
 836
 837	/* Ensure there is enough room for first write */
 838	next_len = ubifs_idx_node_sz(c, cnext->child_cnt);
 839	if (buf_offs + next_len > c->leb_size) {
 840		err = ubifs_update_one_lp(c, lnum, LPROPS_NC, 0, 0,
 841					  LPROPS_TAKEN);
 842		if (err)
 843			return err;
 844		lnum = -1;
 845	}
 846
 847	while (1) {
 
 
 848		cond_resched();
 849
 850		znode = cnext;
 851		idx = c->cbuf + used;
 852
 853		/* Make index node */
 854		idx->ch.node_type = UBIFS_IDX_NODE;
 855		idx->child_cnt = cpu_to_le16(znode->child_cnt);
 856		idx->level = cpu_to_le16(znode->level);
 857		for (i = 0; i < znode->child_cnt; i++) {
 858			struct ubifs_branch *br = ubifs_idx_branch(c, idx, i);
 859			struct ubifs_zbranch *zbr = &znode->zbranch[i];
 860
 861			key_write_idx(c, &zbr->key, &br->key);
 862			br->lnum = cpu_to_le32(zbr->lnum);
 863			br->offs = cpu_to_le32(zbr->offs);
 864			br->len = cpu_to_le32(zbr->len);
 
 865			if (!zbr->lnum || !zbr->len) {
 866				ubifs_err("bad ref in znode");
 867				dbg_dump_znode(c, znode);
 868				if (zbr->znode)
 869					dbg_dump_znode(c, zbr->znode);
 
 
 870			}
 871		}
 872		len = ubifs_idx_node_sz(c, znode->child_cnt);
 873		ubifs_prepare_node(c, idx, len, 0);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 874
 875		/* Determine the index node position */
 876		if (lnum == -1) {
 877			lnum = c->ilebs[lnum_pos++];
 878			buf_offs = 0;
 879			used = 0;
 880			avail = buf_len;
 881		}
 882		offs = buf_offs + used;
 883
 884#ifdef CONFIG_UBIFS_FS_DEBUG
 885		if (lnum != znode->lnum || offs != znode->offs ||
 886		    len != znode->len) {
 887			ubifs_err("inconsistent znode posn");
 888			return -EINVAL;
 889		}
 890#endif
 891
 892		/* Grab some stuff from znode while we still can */
 893		cnext = znode->cnext;
 894
 895		ubifs_assert(ubifs_zn_dirty(znode));
 896		ubifs_assert(ubifs_zn_cow(znode));
 897
 898		/*
 899		 * It is important that other threads should see %DIRTY_ZNODE
 900		 * flag cleared before %COW_ZNODE. Specifically, it matters in
 901		 * the 'dirty_cow_znode()' function. This is the reason for the
 902		 * first barrier. Also, we want the bit changes to be seen to
 903		 * other threads ASAP, to avoid unnecesarry copying, which is
 904		 * the reason for the second barrier.
 905		 */
 906		clear_bit(DIRTY_ZNODE, &znode->flags);
 907		smp_mb__before_clear_bit();
 908		clear_bit(COW_ZNODE, &znode->flags);
 909		smp_mb__after_clear_bit();
 910
 911		/*
 912		 * We have marked the znode as clean but have not updated the
 913		 * @c->clean_zn_cnt counter. If this znode becomes dirty again
 914		 * before 'free_obsolete_znodes()' is called, then
 915		 * @c->clean_zn_cnt will be decremented before it gets
 916		 * incremented (resulting in 2 decrements for the same znode).
 917		 * This means that @c->clean_zn_cnt may become negative for a
 918		 * while.
 919		 *
 920		 * Q: why we cannot increment @c->clean_zn_cnt?
 921		 * A: because we do not have the @c->tnc_mutex locked, and the
 922		 *    following code would be racy and buggy:
 923		 *
 924		 *    if (!ubifs_zn_obsolete(znode)) {
 925		 *            atomic_long_inc(&c->clean_zn_cnt);
 926		 *            atomic_long_inc(&ubifs_clean_zn_cnt);
 927		 *    }
 928		 *
 929		 *    Thus, we just delay the @c->clean_zn_cnt update until we
 930		 *    have the mutex locked.
 931		 */
 932
 933		/* Do not access znode from this point on */
 934
 935		/* Update buffer positions */
 936		wlen = used + len;
 937		used += ALIGN(len, 8);
 938		avail -= ALIGN(len, 8);
 939
 940		/*
 941		 * Calculate the next index node length to see if there is
 942		 * enough room for it
 943		 */
 944		if (cnext == c->cnext)
 945			next_len = 0;
 946		else
 947			next_len = ubifs_idx_node_sz(c, cnext->child_cnt);
 948
 949		nxt_offs = buf_offs + used + next_len;
 950		if (next_len && nxt_offs <= c->leb_size) {
 951			if (avail > 0)
 952				continue;
 953			else
 954				blen = buf_len;
 955		} else {
 956			wlen = ALIGN(wlen, 8);
 957			blen = ALIGN(wlen, c->min_io_size);
 958			ubifs_pad(c, c->cbuf + wlen, blen - wlen);
 959		}
 960
 961		/* The buffer is full or there are no more znodes to do */
 962		err = ubifs_leb_write(c, lnum, c->cbuf, buf_offs, blen,
 963				      UBI_SHORTTERM);
 964		if (err)
 965			return err;
 966		buf_offs += blen;
 967		if (next_len) {
 968			if (nxt_offs > c->leb_size) {
 969				err = ubifs_update_one_lp(c, lnum, LPROPS_NC, 0,
 970							  0, LPROPS_TAKEN);
 971				if (err)
 972					return err;
 973				lnum = -1;
 974			}
 975			used -= blen;
 976			if (used < 0)
 977				used = 0;
 978			avail = buf_len - used;
 979			memmove(c->cbuf, c->cbuf + blen, used);
 980			continue;
 981		}
 982		break;
 983	}
 984
 985#ifdef CONFIG_UBIFS_FS_DEBUG
 986	if (lnum != c->dbg->new_ihead_lnum ||
 987	    buf_offs != c->dbg->new_ihead_offs) {
 988		ubifs_err("inconsistent ihead");
 989		return -EINVAL;
 990	}
 991#endif
 992
 993	c->ihead_lnum = lnum;
 994	c->ihead_offs = buf_offs;
 995
 996	return 0;
 997}
 998
 999/**
1000 * free_obsolete_znodes - free obsolete znodes.
1001 * @c: UBIFS file-system description object
1002 *
1003 * At the end of commit end, obsolete znodes are freed.
1004 */
1005static void free_obsolete_znodes(struct ubifs_info *c)
1006{
1007	struct ubifs_znode *znode, *cnext;
1008
1009	cnext = c->cnext;
1010	do {
1011		znode = cnext;
1012		cnext = znode->cnext;
1013		if (ubifs_zn_obsolete(znode))
1014			kfree(znode);
1015		else {
1016			znode->cnext = NULL;
1017			atomic_long_inc(&c->clean_zn_cnt);
1018			atomic_long_inc(&ubifs_clean_zn_cnt);
1019		}
1020	} while (cnext != c->cnext);
1021}
1022
1023/**
1024 * return_gap_lebs - return LEBs used by the in-gap commit method.
1025 * @c: UBIFS file-system description object
1026 *
1027 * This function clears the "taken" flag for the LEBs which were used by the
1028 * "commit in-the-gaps" method.
1029 */
1030static int return_gap_lebs(struct ubifs_info *c)
1031{
1032	int *p, err;
1033
1034	if (!c->gap_lebs)
1035		return 0;
1036
1037	dbg_cmt("");
1038	for (p = c->gap_lebs; *p != -1; p++) {
1039		err = ubifs_change_one_lp(c, *p, LPROPS_NC, LPROPS_NC, 0,
1040					  LPROPS_TAKEN, 0);
1041		if (err)
1042			return err;
1043	}
1044
1045	kfree(c->gap_lebs);
1046	c->gap_lebs = NULL;
1047	return 0;
1048}
1049
1050/**
1051 * ubifs_tnc_end_commit - update the TNC for commit end.
1052 * @c: UBIFS file-system description object
1053 *
1054 * Write the dirty znodes.
1055 */
1056int ubifs_tnc_end_commit(struct ubifs_info *c)
1057{
1058	int err;
1059
1060	if (!c->cnext)
1061		return 0;
1062
1063	err = return_gap_lebs(c);
1064	if (err)
1065		return err;
1066
1067	err = write_index(c);
1068	if (err)
1069		return err;
1070
1071	mutex_lock(&c->tnc_mutex);
1072
1073	dbg_cmt("TNC height is %d", c->zroot.znode->level + 1);
1074
1075	free_obsolete_znodes(c);
1076
1077	c->cnext = NULL;
1078	kfree(c->ilebs);
1079	c->ilebs = NULL;
1080
1081	mutex_unlock(&c->tnc_mutex);
1082
1083	return 0;
1084}