xref: /linux-6.15/fs/ubifs/tnc_commit.c (revision bc3f07f0)
11e51764aSArtem Bityutskiy /*
21e51764aSArtem Bityutskiy  * This file is part of UBIFS.
31e51764aSArtem Bityutskiy  *
41e51764aSArtem Bityutskiy  * Copyright (C) 2006-2008 Nokia Corporation.
51e51764aSArtem Bityutskiy  *
61e51764aSArtem Bityutskiy  * This program is free software; you can redistribute it and/or modify it
71e51764aSArtem Bityutskiy  * under the terms of the GNU General Public License version 2 as published by
81e51764aSArtem Bityutskiy  * the Free Software Foundation.
91e51764aSArtem Bityutskiy  *
101e51764aSArtem Bityutskiy  * This program is distributed in the hope that it will be useful, but WITHOUT
111e51764aSArtem Bityutskiy  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
121e51764aSArtem Bityutskiy  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
131e51764aSArtem Bityutskiy  * more details.
141e51764aSArtem Bityutskiy  *
151e51764aSArtem Bityutskiy  * You should have received a copy of the GNU General Public License along with
161e51764aSArtem Bityutskiy  * this program; if not, write to the Free Software Foundation, Inc., 51
171e51764aSArtem Bityutskiy  * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
181e51764aSArtem Bityutskiy  *
191e51764aSArtem Bityutskiy  * Authors: Adrian Hunter
201e51764aSArtem Bityutskiy  *          Artem Bityutskiy (Битюцкий Артём)
211e51764aSArtem Bityutskiy  */
221e51764aSArtem Bityutskiy 
231e51764aSArtem Bityutskiy /* This file implements TNC functions for committing */
241e51764aSArtem Bityutskiy 
251e51764aSArtem Bityutskiy #include "ubifs.h"
261e51764aSArtem Bityutskiy 
271e51764aSArtem Bityutskiy /**
281e51764aSArtem Bityutskiy  * make_idx_node - make an index node for fill-the-gaps method of TNC commit.
291e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
301e51764aSArtem Bityutskiy  * @idx: buffer in which to place new index node
311e51764aSArtem Bityutskiy  * @znode: znode from which to make new index node
321e51764aSArtem Bityutskiy  * @lnum: LEB number where new index node will be written
331e51764aSArtem Bityutskiy  * @offs: offset where new index node will be written
341e51764aSArtem Bityutskiy  * @len: length of new index node
351e51764aSArtem Bityutskiy  */
361e51764aSArtem Bityutskiy static int make_idx_node(struct ubifs_info *c, struct ubifs_idx_node *idx,
371e51764aSArtem Bityutskiy 			 struct ubifs_znode *znode, int lnum, int offs, int len)
381e51764aSArtem Bityutskiy {
391e51764aSArtem Bityutskiy 	struct ubifs_znode *zp;
401e51764aSArtem Bityutskiy 	int i, err;
411e51764aSArtem Bityutskiy 
421e51764aSArtem Bityutskiy 	/* Make index node */
431e51764aSArtem Bityutskiy 	idx->ch.node_type = UBIFS_IDX_NODE;
441e51764aSArtem Bityutskiy 	idx->child_cnt = cpu_to_le16(znode->child_cnt);
451e51764aSArtem Bityutskiy 	idx->level = cpu_to_le16(znode->level);
461e51764aSArtem Bityutskiy 	for (i = 0; i < znode->child_cnt; i++) {
471e51764aSArtem Bityutskiy 		struct ubifs_branch *br = ubifs_idx_branch(c, idx, i);
481e51764aSArtem Bityutskiy 		struct ubifs_zbranch *zbr = &znode->zbranch[i];
491e51764aSArtem Bityutskiy 
501e51764aSArtem Bityutskiy 		key_write_idx(c, &zbr->key, &br->key);
511e51764aSArtem Bityutskiy 		br->lnum = cpu_to_le32(zbr->lnum);
521e51764aSArtem Bityutskiy 		br->offs = cpu_to_le32(zbr->offs);
531e51764aSArtem Bityutskiy 		br->len = cpu_to_le32(zbr->len);
541e51764aSArtem Bityutskiy 		if (!zbr->lnum || !zbr->len) {
551e51764aSArtem Bityutskiy 			ubifs_err("bad ref in znode");
561e51764aSArtem Bityutskiy 			dbg_dump_znode(c, znode);
571e51764aSArtem Bityutskiy 			if (zbr->znode)
581e51764aSArtem Bityutskiy 				dbg_dump_znode(c, zbr->znode);
591e51764aSArtem Bityutskiy 		}
601e51764aSArtem Bityutskiy 	}
611e51764aSArtem Bityutskiy 	ubifs_prepare_node(c, idx, len, 0);
621e51764aSArtem Bityutskiy 
631e51764aSArtem Bityutskiy #ifdef CONFIG_UBIFS_FS_DEBUG
641e51764aSArtem Bityutskiy 	znode->lnum = lnum;
651e51764aSArtem Bityutskiy 	znode->offs = offs;
661e51764aSArtem Bityutskiy 	znode->len = len;
671e51764aSArtem Bityutskiy #endif
681e51764aSArtem Bityutskiy 
691e51764aSArtem Bityutskiy 	err = insert_old_idx_znode(c, znode);
701e51764aSArtem Bityutskiy 
711e51764aSArtem Bityutskiy 	/* Update the parent */
721e51764aSArtem Bityutskiy 	zp = znode->parent;
731e51764aSArtem Bityutskiy 	if (zp) {
741e51764aSArtem Bityutskiy 		struct ubifs_zbranch *zbr;
751e51764aSArtem Bityutskiy 
761e51764aSArtem Bityutskiy 		zbr = &zp->zbranch[znode->iip];
771e51764aSArtem Bityutskiy 		zbr->lnum = lnum;
781e51764aSArtem Bityutskiy 		zbr->offs = offs;
791e51764aSArtem Bityutskiy 		zbr->len = len;
801e51764aSArtem Bityutskiy 	} else {
811e51764aSArtem Bityutskiy 		c->zroot.lnum = lnum;
821e51764aSArtem Bityutskiy 		c->zroot.offs = offs;
831e51764aSArtem Bityutskiy 		c->zroot.len = len;
841e51764aSArtem Bityutskiy 	}
851e51764aSArtem Bityutskiy 	c->calc_idx_sz += ALIGN(len, 8);
861e51764aSArtem Bityutskiy 
871e51764aSArtem Bityutskiy 	atomic_long_dec(&c->dirty_zn_cnt);
881e51764aSArtem Bityutskiy 
891e51764aSArtem Bityutskiy 	ubifs_assert(ubifs_zn_dirty(znode));
901e51764aSArtem Bityutskiy 	ubifs_assert(test_bit(COW_ZNODE, &znode->flags));
911e51764aSArtem Bityutskiy 
921e51764aSArtem Bityutskiy 	__clear_bit(DIRTY_ZNODE, &znode->flags);
931e51764aSArtem Bityutskiy 	__clear_bit(COW_ZNODE, &znode->flags);
941e51764aSArtem Bityutskiy 
951e51764aSArtem Bityutskiy 	return err;
961e51764aSArtem Bityutskiy }
971e51764aSArtem Bityutskiy 
981e51764aSArtem Bityutskiy /**
991e51764aSArtem Bityutskiy  * fill_gap - make index nodes in gaps in dirty index LEBs.
1001e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
1011e51764aSArtem Bityutskiy  * @lnum: LEB number that gap appears in
1021e51764aSArtem Bityutskiy  * @gap_start: offset of start of gap
1031e51764aSArtem Bityutskiy  * @gap_end: offset of end of gap
1041e51764aSArtem Bityutskiy  * @dirt: adds dirty space to this
1051e51764aSArtem Bityutskiy  *
1061e51764aSArtem Bityutskiy  * This function returns the number of index nodes written into the gap.
1071e51764aSArtem Bityutskiy  */
1081e51764aSArtem Bityutskiy static int fill_gap(struct ubifs_info *c, int lnum, int gap_start, int gap_end,
1091e51764aSArtem Bityutskiy 		    int *dirt)
1101e51764aSArtem Bityutskiy {
1111e51764aSArtem Bityutskiy 	int len, gap_remains, gap_pos, written, pad_len;
1121e51764aSArtem Bityutskiy 
1131e51764aSArtem Bityutskiy 	ubifs_assert((gap_start & 7) == 0);
1141e51764aSArtem Bityutskiy 	ubifs_assert((gap_end & 7) == 0);
1151e51764aSArtem Bityutskiy 	ubifs_assert(gap_end >= gap_start);
1161e51764aSArtem Bityutskiy 
1171e51764aSArtem Bityutskiy 	gap_remains = gap_end - gap_start;
1181e51764aSArtem Bityutskiy 	if (!gap_remains)
1191e51764aSArtem Bityutskiy 		return 0;
1201e51764aSArtem Bityutskiy 	gap_pos = gap_start;
1211e51764aSArtem Bityutskiy 	written = 0;
1221e51764aSArtem Bityutskiy 	while (c->enext) {
1231e51764aSArtem Bityutskiy 		len = ubifs_idx_node_sz(c, c->enext->child_cnt);
1241e51764aSArtem Bityutskiy 		if (len < gap_remains) {
1251e51764aSArtem Bityutskiy 			struct ubifs_znode *znode = c->enext;
1261e51764aSArtem Bityutskiy 			const int alen = ALIGN(len, 8);
1271e51764aSArtem Bityutskiy 			int err;
1281e51764aSArtem Bityutskiy 
1291e51764aSArtem Bityutskiy 			ubifs_assert(alen <= gap_remains);
1301e51764aSArtem Bityutskiy 			err = make_idx_node(c, c->ileb_buf + gap_pos, znode,
1311e51764aSArtem Bityutskiy 					    lnum, gap_pos, len);
1321e51764aSArtem Bityutskiy 			if (err)
1331e51764aSArtem Bityutskiy 				return err;
1341e51764aSArtem Bityutskiy 			gap_remains -= alen;
1351e51764aSArtem Bityutskiy 			gap_pos += alen;
1361e51764aSArtem Bityutskiy 			c->enext = znode->cnext;
1371e51764aSArtem Bityutskiy 			if (c->enext == c->cnext)
1381e51764aSArtem Bityutskiy 				c->enext = NULL;
1391e51764aSArtem Bityutskiy 			written += 1;
1401e51764aSArtem Bityutskiy 		} else
1411e51764aSArtem Bityutskiy 			break;
1421e51764aSArtem Bityutskiy 	}
1431e51764aSArtem Bityutskiy 	if (gap_end == c->leb_size) {
1441e51764aSArtem Bityutskiy 		c->ileb_len = ALIGN(gap_pos, c->min_io_size);
1451e51764aSArtem Bityutskiy 		/* Pad to end of min_io_size */
1461e51764aSArtem Bityutskiy 		pad_len = c->ileb_len - gap_pos;
1471e51764aSArtem Bityutskiy 	} else
1481e51764aSArtem Bityutskiy 		/* Pad to end of gap */
1491e51764aSArtem Bityutskiy 		pad_len = gap_remains;
1501e51764aSArtem Bityutskiy 	dbg_gc("LEB %d:%d to %d len %d nodes written %d wasted bytes %d",
1511e51764aSArtem Bityutskiy 	       lnum, gap_start, gap_end, gap_end - gap_start, written, pad_len);
1521e51764aSArtem Bityutskiy 	ubifs_pad(c, c->ileb_buf + gap_pos, pad_len);
1531e51764aSArtem Bityutskiy 	*dirt += pad_len;
1541e51764aSArtem Bityutskiy 	return written;
1551e51764aSArtem Bityutskiy }
1561e51764aSArtem Bityutskiy 
1571e51764aSArtem Bityutskiy /**
1581e51764aSArtem Bityutskiy  * find_old_idx - find an index node obsoleted since the last commit start.
1591e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
1601e51764aSArtem Bityutskiy  * @lnum: LEB number of obsoleted index node
1611e51764aSArtem Bityutskiy  * @offs: offset of obsoleted index node
1621e51764aSArtem Bityutskiy  *
1631e51764aSArtem Bityutskiy  * Returns %1 if found and %0 otherwise.
1641e51764aSArtem Bityutskiy  */
1651e51764aSArtem Bityutskiy static int find_old_idx(struct ubifs_info *c, int lnum, int offs)
1661e51764aSArtem Bityutskiy {
1671e51764aSArtem Bityutskiy 	struct ubifs_old_idx *o;
1681e51764aSArtem Bityutskiy 	struct rb_node *p;
1691e51764aSArtem Bityutskiy 
1701e51764aSArtem Bityutskiy 	p = c->old_idx.rb_node;
1711e51764aSArtem Bityutskiy 	while (p) {
1721e51764aSArtem Bityutskiy 		o = rb_entry(p, struct ubifs_old_idx, rb);
1731e51764aSArtem Bityutskiy 		if (lnum < o->lnum)
1741e51764aSArtem Bityutskiy 			p = p->rb_left;
1751e51764aSArtem Bityutskiy 		else if (lnum > o->lnum)
1761e51764aSArtem Bityutskiy 			p = p->rb_right;
1771e51764aSArtem Bityutskiy 		else if (offs < o->offs)
1781e51764aSArtem Bityutskiy 			p = p->rb_left;
1791e51764aSArtem Bityutskiy 		else if (offs > o->offs)
1801e51764aSArtem Bityutskiy 			p = p->rb_right;
1811e51764aSArtem Bityutskiy 		else
1821e51764aSArtem Bityutskiy 			return 1;
1831e51764aSArtem Bityutskiy 	}
1841e51764aSArtem Bityutskiy 	return 0;
1851e51764aSArtem Bityutskiy }
1861e51764aSArtem Bityutskiy 
1871e51764aSArtem Bityutskiy /**
1881e51764aSArtem Bityutskiy  * is_idx_node_in_use - determine if an index node can be overwritten.
1891e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
1901e51764aSArtem Bityutskiy  * @key: key of index node
1911e51764aSArtem Bityutskiy  * @level: index node level
1921e51764aSArtem Bityutskiy  * @lnum: LEB number of index node
1931e51764aSArtem Bityutskiy  * @offs: offset of index node
1941e51764aSArtem Bityutskiy  *
1951e51764aSArtem Bityutskiy  * If @key / @lnum / @offs identify an index node that was not part of the old
1961e51764aSArtem Bityutskiy  * index, then this function returns %0 (obsolete).  Else if the index node was
1971e51764aSArtem Bityutskiy  * part of the old index but is now dirty %1 is returned, else if it is clean %2
1981e51764aSArtem Bityutskiy  * is returned. A negative error code is returned on failure.
1991e51764aSArtem Bityutskiy  */
2001e51764aSArtem Bityutskiy static int is_idx_node_in_use(struct ubifs_info *c, union ubifs_key *key,
2011e51764aSArtem Bityutskiy 			      int level, int lnum, int offs)
2021e51764aSArtem Bityutskiy {
2031e51764aSArtem Bityutskiy 	int ret;
2041e51764aSArtem Bityutskiy 
2051e51764aSArtem Bityutskiy 	ret = is_idx_node_in_tnc(c, key, level, lnum, offs);
2061e51764aSArtem Bityutskiy 	if (ret < 0)
2071e51764aSArtem Bityutskiy 		return ret; /* Error code */
2081e51764aSArtem Bityutskiy 	if (ret == 0)
2091e51764aSArtem Bityutskiy 		if (find_old_idx(c, lnum, offs))
2101e51764aSArtem Bityutskiy 			return 1;
2111e51764aSArtem Bityutskiy 	return ret;
2121e51764aSArtem Bityutskiy }
2131e51764aSArtem Bityutskiy 
2141e51764aSArtem Bityutskiy /**
2151e51764aSArtem Bityutskiy  * layout_leb_in_gaps - layout index nodes using in-the-gaps method.
2161e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
2171e51764aSArtem Bityutskiy  * @p: return LEB number here
2181e51764aSArtem Bityutskiy  *
2191e51764aSArtem Bityutskiy  * This function lays out new index nodes for dirty znodes using in-the-gaps
2201e51764aSArtem Bityutskiy  * method of TNC commit.
2211e51764aSArtem Bityutskiy  * This function merely puts the next znode into the next gap, making no attempt
2221e51764aSArtem Bityutskiy  * to try to maximise the number of znodes that fit.
2231e51764aSArtem Bityutskiy  * This function returns the number of index nodes written into the gaps, or a
2241e51764aSArtem Bityutskiy  * negative error code on failure.
2251e51764aSArtem Bityutskiy  */
2261e51764aSArtem Bityutskiy static int layout_leb_in_gaps(struct ubifs_info *c, int *p)
2271e51764aSArtem Bityutskiy {
2281e51764aSArtem Bityutskiy 	struct ubifs_scan_leb *sleb;
2291e51764aSArtem Bityutskiy 	struct ubifs_scan_node *snod;
2301e51764aSArtem Bityutskiy 	int lnum, dirt = 0, gap_start, gap_end, err, written, tot_written;
2311e51764aSArtem Bityutskiy 
2321e51764aSArtem Bityutskiy 	tot_written = 0;
2331e51764aSArtem Bityutskiy 	/* Get an index LEB with lots of obsolete index nodes */
2341e51764aSArtem Bityutskiy 	lnum = ubifs_find_dirty_idx_leb(c);
2351e51764aSArtem Bityutskiy 	if (lnum < 0)
2361e51764aSArtem Bityutskiy 		/*
2371e51764aSArtem Bityutskiy 		 * There also may be dirt in the index head that could be
2381e51764aSArtem Bityutskiy 		 * filled, however we do not check there at present.
2391e51764aSArtem Bityutskiy 		 */
2401e51764aSArtem Bityutskiy 		return lnum; /* Error code */
2411e51764aSArtem Bityutskiy 	*p = lnum;
2421e51764aSArtem Bityutskiy 	dbg_gc("LEB %d", lnum);
2431e51764aSArtem Bityutskiy 	/*
2441e51764aSArtem Bityutskiy 	 * Scan the index LEB.  We use the generic scan for this even though
2451e51764aSArtem Bityutskiy 	 * it is more comprehensive and less efficient than is needed for this
2461e51764aSArtem Bityutskiy 	 * purpose.
2471e51764aSArtem Bityutskiy 	 */
248348709baSArtem Bityutskiy 	sleb = ubifs_scan(c, lnum, 0, c->ileb_buf, 0);
2491e51764aSArtem Bityutskiy 	c->ileb_len = 0;
2501e51764aSArtem Bityutskiy 	if (IS_ERR(sleb))
2511e51764aSArtem Bityutskiy 		return PTR_ERR(sleb);
2521e51764aSArtem Bityutskiy 	gap_start = 0;
2531e51764aSArtem Bityutskiy 	list_for_each_entry(snod, &sleb->nodes, list) {
2541e51764aSArtem Bityutskiy 		struct ubifs_idx_node *idx;
2551e51764aSArtem Bityutskiy 		int in_use, level;
2561e51764aSArtem Bityutskiy 
2571e51764aSArtem Bityutskiy 		ubifs_assert(snod->type == UBIFS_IDX_NODE);
2581e51764aSArtem Bityutskiy 		idx = snod->node;
2591e51764aSArtem Bityutskiy 		key_read(c, ubifs_idx_key(c, idx), &snod->key);
2601e51764aSArtem Bityutskiy 		level = le16_to_cpu(idx->level);
2611e51764aSArtem Bityutskiy 		/* Determine if the index node is in use (not obsolete) */
2621e51764aSArtem Bityutskiy 		in_use = is_idx_node_in_use(c, &snod->key, level, lnum,
2631e51764aSArtem Bityutskiy 					    snod->offs);
2641e51764aSArtem Bityutskiy 		if (in_use < 0) {
2651e51764aSArtem Bityutskiy 			ubifs_scan_destroy(sleb);
2661e51764aSArtem Bityutskiy 			return in_use; /* Error code */
2671e51764aSArtem Bityutskiy 		}
2681e51764aSArtem Bityutskiy 		if (in_use) {
2691e51764aSArtem Bityutskiy 			if (in_use == 1)
2701e51764aSArtem Bityutskiy 				dirt += ALIGN(snod->len, 8);
2711e51764aSArtem Bityutskiy 			/*
2721e51764aSArtem Bityutskiy 			 * The obsolete index nodes form gaps that can be
2731e51764aSArtem Bityutskiy 			 * overwritten.  This gap has ended because we have
2741e51764aSArtem Bityutskiy 			 * found an index node that is still in use
2751e51764aSArtem Bityutskiy 			 * i.e. not obsolete
2761e51764aSArtem Bityutskiy 			 */
2771e51764aSArtem Bityutskiy 			gap_end = snod->offs;
2781e51764aSArtem Bityutskiy 			/* Try to fill gap */
2791e51764aSArtem Bityutskiy 			written = fill_gap(c, lnum, gap_start, gap_end, &dirt);
2801e51764aSArtem Bityutskiy 			if (written < 0) {
2811e51764aSArtem Bityutskiy 				ubifs_scan_destroy(sleb);
2821e51764aSArtem Bityutskiy 				return written; /* Error code */
2831e51764aSArtem Bityutskiy 			}
2841e51764aSArtem Bityutskiy 			tot_written += written;
2851e51764aSArtem Bityutskiy 			gap_start = ALIGN(snod->offs + snod->len, 8);
2861e51764aSArtem Bityutskiy 		}
2871e51764aSArtem Bityutskiy 	}
2881e51764aSArtem Bityutskiy 	ubifs_scan_destroy(sleb);
2891e51764aSArtem Bityutskiy 	c->ileb_len = c->leb_size;
2901e51764aSArtem Bityutskiy 	gap_end = c->leb_size;
2911e51764aSArtem Bityutskiy 	/* Try to fill gap */
2921e51764aSArtem Bityutskiy 	written = fill_gap(c, lnum, gap_start, gap_end, &dirt);
2931e51764aSArtem Bityutskiy 	if (written < 0)
2941e51764aSArtem Bityutskiy 		return written; /* Error code */
2951e51764aSArtem Bityutskiy 	tot_written += written;
2961e51764aSArtem Bityutskiy 	if (tot_written == 0) {
2971e51764aSArtem Bityutskiy 		struct ubifs_lprops lp;
2981e51764aSArtem Bityutskiy 
2991e51764aSArtem Bityutskiy 		dbg_gc("LEB %d wrote %d index nodes", lnum, tot_written);
3001e51764aSArtem Bityutskiy 		err = ubifs_read_one_lp(c, lnum, &lp);
3011e51764aSArtem Bityutskiy 		if (err)
3021e51764aSArtem Bityutskiy 			return err;
3031e51764aSArtem Bityutskiy 		if (lp.free == c->leb_size) {
3041e51764aSArtem Bityutskiy 			/*
3051e51764aSArtem Bityutskiy 			 * We must have snatched this LEB from the idx_gc list
3061e51764aSArtem Bityutskiy 			 * so we need to correct the free and dirty space.
3071e51764aSArtem Bityutskiy 			 */
3081e51764aSArtem Bityutskiy 			err = ubifs_change_one_lp(c, lnum,
3091e51764aSArtem Bityutskiy 						  c->leb_size - c->ileb_len,
3101e51764aSArtem Bityutskiy 						  dirt, 0, 0, 0);
3111e51764aSArtem Bityutskiy 			if (err)
3121e51764aSArtem Bityutskiy 				return err;
3131e51764aSArtem Bityutskiy 		}
3141e51764aSArtem Bityutskiy 		return 0;
3151e51764aSArtem Bityutskiy 	}
3161e51764aSArtem Bityutskiy 	err = ubifs_change_one_lp(c, lnum, c->leb_size - c->ileb_len, dirt,
3171e51764aSArtem Bityutskiy 				  0, 0, 0);
3181e51764aSArtem Bityutskiy 	if (err)
3191e51764aSArtem Bityutskiy 		return err;
3201e51764aSArtem Bityutskiy 	err = ubifs_leb_change(c, lnum, c->ileb_buf, c->ileb_len,
3211e51764aSArtem Bityutskiy 			       UBI_SHORTTERM);
3221e51764aSArtem Bityutskiy 	if (err)
3231e51764aSArtem Bityutskiy 		return err;
3241e51764aSArtem Bityutskiy 	dbg_gc("LEB %d wrote %d index nodes", lnum, tot_written);
3251e51764aSArtem Bityutskiy 	return tot_written;
3261e51764aSArtem Bityutskiy }
3271e51764aSArtem Bityutskiy 
3281e51764aSArtem Bityutskiy /**
3291e51764aSArtem Bityutskiy  * get_leb_cnt - calculate the number of empty LEBs needed to commit.
3301e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
3311e51764aSArtem Bityutskiy  * @cnt: number of znodes to commit
3321e51764aSArtem Bityutskiy  *
3331e51764aSArtem Bityutskiy  * This function returns the number of empty LEBs needed to commit @cnt znodes
3341e51764aSArtem Bityutskiy  * to the current index head.  The number is not exact and may be more than
3351e51764aSArtem Bityutskiy  * needed.
3361e51764aSArtem Bityutskiy  */
3371e51764aSArtem Bityutskiy static int get_leb_cnt(struct ubifs_info *c, int cnt)
3381e51764aSArtem Bityutskiy {
3391e51764aSArtem Bityutskiy 	int d;
3401e51764aSArtem Bityutskiy 
3411e51764aSArtem Bityutskiy 	/* Assume maximum index node size (i.e. overestimate space needed) */
3421e51764aSArtem Bityutskiy 	cnt -= (c->leb_size - c->ihead_offs) / c->max_idx_node_sz;
3431e51764aSArtem Bityutskiy 	if (cnt < 0)
3441e51764aSArtem Bityutskiy 		cnt = 0;
3451e51764aSArtem Bityutskiy 	d = c->leb_size / c->max_idx_node_sz;
3461e51764aSArtem Bityutskiy 	return DIV_ROUND_UP(cnt, d);
3471e51764aSArtem Bityutskiy }
3481e51764aSArtem Bityutskiy 
3491e51764aSArtem Bityutskiy /**
3501e51764aSArtem Bityutskiy  * layout_in_gaps - in-the-gaps method of committing TNC.
3511e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
3521e51764aSArtem Bityutskiy  * @cnt: number of dirty znodes to commit.
3531e51764aSArtem Bityutskiy  *
3541e51764aSArtem Bityutskiy  * This function lays out new index nodes for dirty znodes using in-the-gaps
3551e51764aSArtem Bityutskiy  * method of TNC commit.
3561e51764aSArtem Bityutskiy  *
3571e51764aSArtem Bityutskiy  * This function returns %0 on success and a negative error code on failure.
3581e51764aSArtem Bityutskiy  */
3591e51764aSArtem Bityutskiy static int layout_in_gaps(struct ubifs_info *c, int cnt)
3601e51764aSArtem Bityutskiy {
3611e51764aSArtem Bityutskiy 	int err, leb_needed_cnt, written, *p;
3621e51764aSArtem Bityutskiy 
3631e51764aSArtem Bityutskiy 	dbg_gc("%d znodes to write", cnt);
3641e51764aSArtem Bityutskiy 
3651e51764aSArtem Bityutskiy 	c->gap_lebs = kmalloc(sizeof(int) * (c->lst.idx_lebs + 1), GFP_NOFS);
3661e51764aSArtem Bityutskiy 	if (!c->gap_lebs)
3671e51764aSArtem Bityutskiy 		return -ENOMEM;
3681e51764aSArtem Bityutskiy 
3691e51764aSArtem Bityutskiy 	p = c->gap_lebs;
3701e51764aSArtem Bityutskiy 	do {
3711e51764aSArtem Bityutskiy 		ubifs_assert(p < c->gap_lebs + sizeof(int) * c->lst.idx_lebs);
3721e51764aSArtem Bityutskiy 		written = layout_leb_in_gaps(c, p);
3731e51764aSArtem Bityutskiy 		if (written < 0) {
3741e51764aSArtem Bityutskiy 			err = written;
3750010f18aSArtem Bityutskiy 			if (err != -ENOSPC) {
3760010f18aSArtem Bityutskiy 				kfree(c->gap_lebs);
3770010f18aSArtem Bityutskiy 				c->gap_lebs = NULL;
3780010f18aSArtem Bityutskiy 				return err;
3790010f18aSArtem Bityutskiy 			}
380*bc3f07f0SArtem Bityutskiy 			if (dbg_force_in_the_gaps_enabled()) {
3811e51764aSArtem Bityutskiy 				/*
3820010f18aSArtem Bityutskiy 				 * Do not print scary warnings if the debugging
3830010f18aSArtem Bityutskiy 				 * option which forces in-the-gaps is enabled.
3841e51764aSArtem Bityutskiy 				 */
385*bc3f07f0SArtem Bityutskiy 				ubifs_warn("out of space");
386f1bd66afSArtem Bityutskiy 				dbg_dump_budg(c, &c->bi);
3871e51764aSArtem Bityutskiy 				dbg_dump_lprops(c);
3881e51764aSArtem Bityutskiy 			}
3891e51764aSArtem Bityutskiy 			/* Try to commit anyway */
3901e51764aSArtem Bityutskiy 			err = 0;
3911e51764aSArtem Bityutskiy 			break;
3921e51764aSArtem Bityutskiy 		}
3931e51764aSArtem Bityutskiy 		p++;
3941e51764aSArtem Bityutskiy 		cnt -= written;
3951e51764aSArtem Bityutskiy 		leb_needed_cnt = get_leb_cnt(c, cnt);
3961e51764aSArtem Bityutskiy 		dbg_gc("%d znodes remaining, need %d LEBs, have %d", cnt,
3971e51764aSArtem Bityutskiy 		       leb_needed_cnt, c->ileb_cnt);
3981e51764aSArtem Bityutskiy 	} while (leb_needed_cnt > c->ileb_cnt);
3991e51764aSArtem Bityutskiy 
4001e51764aSArtem Bityutskiy 	*p = -1;
4011e51764aSArtem Bityutskiy 	return 0;
4021e51764aSArtem Bityutskiy }
4031e51764aSArtem Bityutskiy 
4041e51764aSArtem Bityutskiy /**
4051e51764aSArtem Bityutskiy  * layout_in_empty_space - layout index nodes in empty space.
4061e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
4071e51764aSArtem Bityutskiy  *
4081e51764aSArtem Bityutskiy  * This function lays out new index nodes for dirty znodes using empty LEBs.
4091e51764aSArtem Bityutskiy  *
4101e51764aSArtem Bityutskiy  * This function returns %0 on success and a negative error code on failure.
4111e51764aSArtem Bityutskiy  */
4121e51764aSArtem Bityutskiy static int layout_in_empty_space(struct ubifs_info *c)
4131e51764aSArtem Bityutskiy {
4141e51764aSArtem Bityutskiy 	struct ubifs_znode *znode, *cnext, *zp;
4151e51764aSArtem Bityutskiy 	int lnum, offs, len, next_len, buf_len, buf_offs, used, avail;
4161e51764aSArtem Bityutskiy 	int wlen, blen, err;
4171e51764aSArtem Bityutskiy 
4181e51764aSArtem Bityutskiy 	cnext = c->enext;
4191e51764aSArtem Bityutskiy 	if (!cnext)
4201e51764aSArtem Bityutskiy 		return 0;
4211e51764aSArtem Bityutskiy 
4221e51764aSArtem Bityutskiy 	lnum = c->ihead_lnum;
4231e51764aSArtem Bityutskiy 	buf_offs = c->ihead_offs;
4241e51764aSArtem Bityutskiy 
4251e51764aSArtem Bityutskiy 	buf_len = ubifs_idx_node_sz(c, c->fanout);
4261e51764aSArtem Bityutskiy 	buf_len = ALIGN(buf_len, c->min_io_size);
4271e51764aSArtem Bityutskiy 	used = 0;
4281e51764aSArtem Bityutskiy 	avail = buf_len;
4291e51764aSArtem Bityutskiy 
4301e51764aSArtem Bityutskiy 	/* Ensure there is enough room for first write */
4311e51764aSArtem Bityutskiy 	next_len = ubifs_idx_node_sz(c, cnext->child_cnt);
4321e51764aSArtem Bityutskiy 	if (buf_offs + next_len > c->leb_size)
4331e51764aSArtem Bityutskiy 		lnum = -1;
4341e51764aSArtem Bityutskiy 
4351e51764aSArtem Bityutskiy 	while (1) {
4361e51764aSArtem Bityutskiy 		znode = cnext;
4371e51764aSArtem Bityutskiy 
4381e51764aSArtem Bityutskiy 		len = ubifs_idx_node_sz(c, znode->child_cnt);
4391e51764aSArtem Bityutskiy 
4401e51764aSArtem Bityutskiy 		/* Determine the index node position */
4411e51764aSArtem Bityutskiy 		if (lnum == -1) {
4421e51764aSArtem Bityutskiy 			if (c->ileb_nxt >= c->ileb_cnt) {
4431e51764aSArtem Bityutskiy 				ubifs_err("out of space");
4441e51764aSArtem Bityutskiy 				return -ENOSPC;
4451e51764aSArtem Bityutskiy 			}
4461e51764aSArtem Bityutskiy 			lnum = c->ilebs[c->ileb_nxt++];
4471e51764aSArtem Bityutskiy 			buf_offs = 0;
4481e51764aSArtem Bityutskiy 			used = 0;
4491e51764aSArtem Bityutskiy 			avail = buf_len;
4501e51764aSArtem Bityutskiy 		}
4511e51764aSArtem Bityutskiy 
4521e51764aSArtem Bityutskiy 		offs = buf_offs + used;
4531e51764aSArtem Bityutskiy 
4541e51764aSArtem Bityutskiy #ifdef CONFIG_UBIFS_FS_DEBUG
4551e51764aSArtem Bityutskiy 		znode->lnum = lnum;
4561e51764aSArtem Bityutskiy 		znode->offs = offs;
4571e51764aSArtem Bityutskiy 		znode->len = len;
4581e51764aSArtem Bityutskiy #endif
4591e51764aSArtem Bityutskiy 
4601e51764aSArtem Bityutskiy 		/* Update the parent */
4611e51764aSArtem Bityutskiy 		zp = znode->parent;
4621e51764aSArtem Bityutskiy 		if (zp) {
4631e51764aSArtem Bityutskiy 			struct ubifs_zbranch *zbr;
4641e51764aSArtem Bityutskiy 			int i;
4651e51764aSArtem Bityutskiy 
4661e51764aSArtem Bityutskiy 			i = znode->iip;
4671e51764aSArtem Bityutskiy 			zbr = &zp->zbranch[i];
4681e51764aSArtem Bityutskiy 			zbr->lnum = lnum;
4691e51764aSArtem Bityutskiy 			zbr->offs = offs;
4701e51764aSArtem Bityutskiy 			zbr->len = len;
4711e51764aSArtem Bityutskiy 		} else {
4721e51764aSArtem Bityutskiy 			c->zroot.lnum = lnum;
4731e51764aSArtem Bityutskiy 			c->zroot.offs = offs;
4741e51764aSArtem Bityutskiy 			c->zroot.len = len;
4751e51764aSArtem Bityutskiy 		}
4761e51764aSArtem Bityutskiy 		c->calc_idx_sz += ALIGN(len, 8);
4771e51764aSArtem Bityutskiy 
4781e51764aSArtem Bityutskiy 		/*
4791e51764aSArtem Bityutskiy 		 * Once lprops is updated, we can decrease the dirty znode count
4801e51764aSArtem Bityutskiy 		 * but it is easier to just do it here.
4811e51764aSArtem Bityutskiy 		 */
4821e51764aSArtem Bityutskiy 		atomic_long_dec(&c->dirty_zn_cnt);
4831e51764aSArtem Bityutskiy 
4841e51764aSArtem Bityutskiy 		/*
4851e51764aSArtem Bityutskiy 		 * Calculate the next index node length to see if there is
4861e51764aSArtem Bityutskiy 		 * enough room for it
4871e51764aSArtem Bityutskiy 		 */
4881e51764aSArtem Bityutskiy 		cnext = znode->cnext;
4891e51764aSArtem Bityutskiy 		if (cnext == c->cnext)
4901e51764aSArtem Bityutskiy 			next_len = 0;
4911e51764aSArtem Bityutskiy 		else
4921e51764aSArtem Bityutskiy 			next_len = ubifs_idx_node_sz(c, cnext->child_cnt);
4931e51764aSArtem Bityutskiy 
4941e51764aSArtem Bityutskiy 		if (c->min_io_size == 1) {
4951e51764aSArtem Bityutskiy 			buf_offs += ALIGN(len, 8);
4961e51764aSArtem Bityutskiy 			if (next_len) {
4971e51764aSArtem Bityutskiy 				if (buf_offs + next_len <= c->leb_size)
4981e51764aSArtem Bityutskiy 					continue;
4991e51764aSArtem Bityutskiy 				err = ubifs_update_one_lp(c, lnum, 0,
5001e51764aSArtem Bityutskiy 						c->leb_size - buf_offs, 0, 0);
5011e51764aSArtem Bityutskiy 				if (err)
5021e51764aSArtem Bityutskiy 					return err;
5031e51764aSArtem Bityutskiy 				lnum = -1;
5041e51764aSArtem Bityutskiy 				continue;
5051e51764aSArtem Bityutskiy 			}
5061e51764aSArtem Bityutskiy 			err = ubifs_update_one_lp(c, lnum,
5071e51764aSArtem Bityutskiy 					c->leb_size - buf_offs, 0, 0, 0);
5081e51764aSArtem Bityutskiy 			if (err)
5091e51764aSArtem Bityutskiy 				return err;
5101e51764aSArtem Bityutskiy 			break;
5111e51764aSArtem Bityutskiy 		}
5121e51764aSArtem Bityutskiy 
5131e51764aSArtem Bityutskiy 		/* Update buffer positions */
5141e51764aSArtem Bityutskiy 		wlen = used + len;
5151e51764aSArtem Bityutskiy 		used += ALIGN(len, 8);
5161e51764aSArtem Bityutskiy 		avail -= ALIGN(len, 8);
5171e51764aSArtem Bityutskiy 
5181e51764aSArtem Bityutskiy 		if (next_len != 0 &&
5191e51764aSArtem Bityutskiy 		    buf_offs + used + next_len <= c->leb_size &&
5201e51764aSArtem Bityutskiy 		    avail > 0)
5211e51764aSArtem Bityutskiy 			continue;
5221e51764aSArtem Bityutskiy 
5231e51764aSArtem Bityutskiy 		if (avail <= 0 && next_len &&
5241e51764aSArtem Bityutskiy 		    buf_offs + used + next_len <= c->leb_size)
5251e51764aSArtem Bityutskiy 			blen = buf_len;
5261e51764aSArtem Bityutskiy 		else
5271e51764aSArtem Bityutskiy 			blen = ALIGN(wlen, c->min_io_size);
5281e51764aSArtem Bityutskiy 
5291e51764aSArtem Bityutskiy 		/* The buffer is full or there are no more znodes to do */
5301e51764aSArtem Bityutskiy 		buf_offs += blen;
5311e51764aSArtem Bityutskiy 		if (next_len) {
5321e51764aSArtem Bityutskiy 			if (buf_offs + next_len > c->leb_size) {
5331e51764aSArtem Bityutskiy 				err = ubifs_update_one_lp(c, lnum,
5341e51764aSArtem Bityutskiy 					c->leb_size - buf_offs, blen - used,
5351e51764aSArtem Bityutskiy 					0, 0);
5361e51764aSArtem Bityutskiy 				if (err)
5371e51764aSArtem Bityutskiy 					return err;
5381e51764aSArtem Bityutskiy 				lnum = -1;
5391e51764aSArtem Bityutskiy 			}
5401e51764aSArtem Bityutskiy 			used -= blen;
5411e51764aSArtem Bityutskiy 			if (used < 0)
5421e51764aSArtem Bityutskiy 				used = 0;
5431e51764aSArtem Bityutskiy 			avail = buf_len - used;
5441e51764aSArtem Bityutskiy 			continue;
5451e51764aSArtem Bityutskiy 		}
5461e51764aSArtem Bityutskiy 		err = ubifs_update_one_lp(c, lnum, c->leb_size - buf_offs,
5471e51764aSArtem Bityutskiy 					  blen - used, 0, 0);
5481e51764aSArtem Bityutskiy 		if (err)
5491e51764aSArtem Bityutskiy 			return err;
5501e51764aSArtem Bityutskiy 		break;
5511e51764aSArtem Bityutskiy 	}
5521e51764aSArtem Bityutskiy 
5531e51764aSArtem Bityutskiy #ifdef CONFIG_UBIFS_FS_DEBUG
55417c2f9f8SArtem Bityutskiy 	c->dbg->new_ihead_lnum = lnum;
55517c2f9f8SArtem Bityutskiy 	c->dbg->new_ihead_offs = buf_offs;
5561e51764aSArtem Bityutskiy #endif
5571e51764aSArtem Bityutskiy 
5581e51764aSArtem Bityutskiy 	return 0;
5591e51764aSArtem Bityutskiy }
5601e51764aSArtem Bityutskiy 
5611e51764aSArtem Bityutskiy /**
5621e51764aSArtem Bityutskiy  * layout_commit - determine positions of index nodes to commit.
5631e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
5641e51764aSArtem Bityutskiy  * @no_space: indicates that insufficient empty LEBs were allocated
5651e51764aSArtem Bityutskiy  * @cnt: number of znodes to commit
5661e51764aSArtem Bityutskiy  *
5671e51764aSArtem Bityutskiy  * Calculate and update the positions of index nodes to commit.  If there were
5681e51764aSArtem Bityutskiy  * an insufficient number of empty LEBs allocated, then index nodes are placed
5691e51764aSArtem Bityutskiy  * into the gaps created by obsolete index nodes in non-empty index LEBs.  For
5701e51764aSArtem Bityutskiy  * this purpose, an obsolete index node is one that was not in the index as at
5711e51764aSArtem Bityutskiy  * the end of the last commit.  To write "in-the-gaps" requires that those index
5721e51764aSArtem Bityutskiy  * LEBs are updated atomically in-place.
5731e51764aSArtem Bityutskiy  */
5741e51764aSArtem Bityutskiy static int layout_commit(struct ubifs_info *c, int no_space, int cnt)
5751e51764aSArtem Bityutskiy {
5761e51764aSArtem Bityutskiy 	int err;
5771e51764aSArtem Bityutskiy 
5781e51764aSArtem Bityutskiy 	if (no_space) {
5791e51764aSArtem Bityutskiy 		err = layout_in_gaps(c, cnt);
5801e51764aSArtem Bityutskiy 		if (err)
5811e51764aSArtem Bityutskiy 			return err;
5821e51764aSArtem Bityutskiy 	}
5831e51764aSArtem Bityutskiy 	err = layout_in_empty_space(c);
5841e51764aSArtem Bityutskiy 	return err;
5851e51764aSArtem Bityutskiy }
5861e51764aSArtem Bityutskiy 
5871e51764aSArtem Bityutskiy /**
5881e51764aSArtem Bityutskiy  * find_first_dirty - find first dirty znode.
5891e51764aSArtem Bityutskiy  * @znode: znode to begin searching from
5901e51764aSArtem Bityutskiy  */
5911e51764aSArtem Bityutskiy static struct ubifs_znode *find_first_dirty(struct ubifs_znode *znode)
5921e51764aSArtem Bityutskiy {
5931e51764aSArtem Bityutskiy 	int i, cont;
5941e51764aSArtem Bityutskiy 
5951e51764aSArtem Bityutskiy 	if (!znode)
5961e51764aSArtem Bityutskiy 		return NULL;
5971e51764aSArtem Bityutskiy 
5981e51764aSArtem Bityutskiy 	while (1) {
5991e51764aSArtem Bityutskiy 		if (znode->level == 0) {
6001e51764aSArtem Bityutskiy 			if (ubifs_zn_dirty(znode))
6011e51764aSArtem Bityutskiy 				return znode;
6021e51764aSArtem Bityutskiy 			return NULL;
6031e51764aSArtem Bityutskiy 		}
6041e51764aSArtem Bityutskiy 		cont = 0;
6051e51764aSArtem Bityutskiy 		for (i = 0; i < znode->child_cnt; i++) {
6061e51764aSArtem Bityutskiy 			struct ubifs_zbranch *zbr = &znode->zbranch[i];
6071e51764aSArtem Bityutskiy 
6081e51764aSArtem Bityutskiy 			if (zbr->znode && ubifs_zn_dirty(zbr->znode)) {
6091e51764aSArtem Bityutskiy 				znode = zbr->znode;
6101e51764aSArtem Bityutskiy 				cont = 1;
6111e51764aSArtem Bityutskiy 				break;
6121e51764aSArtem Bityutskiy 			}
6131e51764aSArtem Bityutskiy 		}
6141e51764aSArtem Bityutskiy 		if (!cont) {
6151e51764aSArtem Bityutskiy 			if (ubifs_zn_dirty(znode))
6161e51764aSArtem Bityutskiy 				return znode;
6171e51764aSArtem Bityutskiy 			return NULL;
6181e51764aSArtem Bityutskiy 		}
6191e51764aSArtem Bityutskiy 	}
6201e51764aSArtem Bityutskiy }
6211e51764aSArtem Bityutskiy 
6221e51764aSArtem Bityutskiy /**
6231e51764aSArtem Bityutskiy  * find_next_dirty - find next dirty znode.
6241e51764aSArtem Bityutskiy  * @znode: znode to begin searching from
6251e51764aSArtem Bityutskiy  */
6261e51764aSArtem Bityutskiy static struct ubifs_znode *find_next_dirty(struct ubifs_znode *znode)
6271e51764aSArtem Bityutskiy {
6281e51764aSArtem Bityutskiy 	int n = znode->iip + 1;
6291e51764aSArtem Bityutskiy 
6301e51764aSArtem Bityutskiy 	znode = znode->parent;
6311e51764aSArtem Bityutskiy 	if (!znode)
6321e51764aSArtem Bityutskiy 		return NULL;
6331e51764aSArtem Bityutskiy 	for (; n < znode->child_cnt; n++) {
6341e51764aSArtem Bityutskiy 		struct ubifs_zbranch *zbr = &znode->zbranch[n];
6351e51764aSArtem Bityutskiy 
6361e51764aSArtem Bityutskiy 		if (zbr->znode && ubifs_zn_dirty(zbr->znode))
6371e51764aSArtem Bityutskiy 			return find_first_dirty(zbr->znode);
6381e51764aSArtem Bityutskiy 	}
6391e51764aSArtem Bityutskiy 	return znode;
6401e51764aSArtem Bityutskiy }
6411e51764aSArtem Bityutskiy 
6421e51764aSArtem Bityutskiy /**
6431e51764aSArtem Bityutskiy  * get_znodes_to_commit - create list of dirty znodes to commit.
6441e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
6451e51764aSArtem Bityutskiy  *
6461e51764aSArtem Bityutskiy  * This function returns the number of znodes to commit.
6471e51764aSArtem Bityutskiy  */
6481e51764aSArtem Bityutskiy static int get_znodes_to_commit(struct ubifs_info *c)
6491e51764aSArtem Bityutskiy {
6501e51764aSArtem Bityutskiy 	struct ubifs_znode *znode, *cnext;
6511e51764aSArtem Bityutskiy 	int cnt = 0;
6521e51764aSArtem Bityutskiy 
6531e51764aSArtem Bityutskiy 	c->cnext = find_first_dirty(c->zroot.znode);
6541e51764aSArtem Bityutskiy 	znode = c->enext = c->cnext;
6551e51764aSArtem Bityutskiy 	if (!znode) {
6561e51764aSArtem Bityutskiy 		dbg_cmt("no znodes to commit");
6571e51764aSArtem Bityutskiy 		return 0;
6581e51764aSArtem Bityutskiy 	}
6591e51764aSArtem Bityutskiy 	cnt += 1;
6601e51764aSArtem Bityutskiy 	while (1) {
6611e51764aSArtem Bityutskiy 		ubifs_assert(!test_bit(COW_ZNODE, &znode->flags));
6621e51764aSArtem Bityutskiy 		__set_bit(COW_ZNODE, &znode->flags);
6631e51764aSArtem Bityutskiy 		znode->alt = 0;
6641e51764aSArtem Bityutskiy 		cnext = find_next_dirty(znode);
6651e51764aSArtem Bityutskiy 		if (!cnext) {
6661e51764aSArtem Bityutskiy 			znode->cnext = c->cnext;
6671e51764aSArtem Bityutskiy 			break;
6681e51764aSArtem Bityutskiy 		}
6691e51764aSArtem Bityutskiy 		znode->cnext = cnext;
6701e51764aSArtem Bityutskiy 		znode = cnext;
6711e51764aSArtem Bityutskiy 		cnt += 1;
6721e51764aSArtem Bityutskiy 	}
6731e51764aSArtem Bityutskiy 	dbg_cmt("committing %d znodes", cnt);
6741e51764aSArtem Bityutskiy 	ubifs_assert(cnt == atomic_long_read(&c->dirty_zn_cnt));
6751e51764aSArtem Bityutskiy 	return cnt;
6761e51764aSArtem Bityutskiy }
6771e51764aSArtem Bityutskiy 
6781e51764aSArtem Bityutskiy /**
6791e51764aSArtem Bityutskiy  * alloc_idx_lebs - allocate empty LEBs to be used to commit.
6801e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
6811e51764aSArtem Bityutskiy  * @cnt: number of znodes to commit
6821e51764aSArtem Bityutskiy  *
6831e51764aSArtem Bityutskiy  * This function returns %-ENOSPC if it cannot allocate a sufficient number of
6841e51764aSArtem Bityutskiy  * empty LEBs.  %0 is returned on success, otherwise a negative error code
6851e51764aSArtem Bityutskiy  * is returned.
6861e51764aSArtem Bityutskiy  */
6871e51764aSArtem Bityutskiy static int alloc_idx_lebs(struct ubifs_info *c, int cnt)
6881e51764aSArtem Bityutskiy {
6891e51764aSArtem Bityutskiy 	int i, leb_cnt, lnum;
6901e51764aSArtem Bityutskiy 
6911e51764aSArtem Bityutskiy 	c->ileb_cnt = 0;
6921e51764aSArtem Bityutskiy 	c->ileb_nxt = 0;
6931e51764aSArtem Bityutskiy 	leb_cnt = get_leb_cnt(c, cnt);
6941e51764aSArtem Bityutskiy 	dbg_cmt("need about %d empty LEBS for TNC commit", leb_cnt);
6951e51764aSArtem Bityutskiy 	if (!leb_cnt)
6961e51764aSArtem Bityutskiy 		return 0;
6971e51764aSArtem Bityutskiy 	c->ilebs = kmalloc(leb_cnt * sizeof(int), GFP_NOFS);
6981e51764aSArtem Bityutskiy 	if (!c->ilebs)
6991e51764aSArtem Bityutskiy 		return -ENOMEM;
7001e51764aSArtem Bityutskiy 	for (i = 0; i < leb_cnt; i++) {
7011e51764aSArtem Bityutskiy 		lnum = ubifs_find_free_leb_for_idx(c);
7021e51764aSArtem Bityutskiy 		if (lnum < 0)
7031e51764aSArtem Bityutskiy 			return lnum;
7041e51764aSArtem Bityutskiy 		c->ilebs[c->ileb_cnt++] = lnum;
7051e51764aSArtem Bityutskiy 		dbg_cmt("LEB %d", lnum);
7061e51764aSArtem Bityutskiy 	}
7071e51764aSArtem Bityutskiy 	if (dbg_force_in_the_gaps())
7081e51764aSArtem Bityutskiy 		return -ENOSPC;
7091e51764aSArtem Bityutskiy 	return 0;
7101e51764aSArtem Bityutskiy }
7111e51764aSArtem Bityutskiy 
7121e51764aSArtem Bityutskiy /**
7131e51764aSArtem Bityutskiy  * free_unused_idx_lebs - free unused LEBs that were allocated for the commit.
7141e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
7151e51764aSArtem Bityutskiy  *
7161e51764aSArtem Bityutskiy  * It is possible that we allocate more empty LEBs for the commit than we need.
7171e51764aSArtem Bityutskiy  * This functions frees the surplus.
7181e51764aSArtem Bityutskiy  *
7191e51764aSArtem Bityutskiy  * This function returns %0 on success and a negative error code on failure.
7201e51764aSArtem Bityutskiy  */
7211e51764aSArtem Bityutskiy static int free_unused_idx_lebs(struct ubifs_info *c)
7221e51764aSArtem Bityutskiy {
7231e51764aSArtem Bityutskiy 	int i, err = 0, lnum, er;
7241e51764aSArtem Bityutskiy 
7251e51764aSArtem Bityutskiy 	for (i = c->ileb_nxt; i < c->ileb_cnt; i++) {
7261e51764aSArtem Bityutskiy 		lnum = c->ilebs[i];
7271e51764aSArtem Bityutskiy 		dbg_cmt("LEB %d", lnum);
7281e51764aSArtem Bityutskiy 		er = ubifs_change_one_lp(c, lnum, LPROPS_NC, LPROPS_NC, 0,
7291e51764aSArtem Bityutskiy 					 LPROPS_INDEX | LPROPS_TAKEN, 0);
7301e51764aSArtem Bityutskiy 		if (!err)
7311e51764aSArtem Bityutskiy 			err = er;
7321e51764aSArtem Bityutskiy 	}
7331e51764aSArtem Bityutskiy 	return err;
7341e51764aSArtem Bityutskiy }
7351e51764aSArtem Bityutskiy 
7361e51764aSArtem Bityutskiy /**
7371e51764aSArtem Bityutskiy  * free_idx_lebs - free unused LEBs after commit end.
7381e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
7391e51764aSArtem Bityutskiy  *
7401e51764aSArtem Bityutskiy  * This function returns %0 on success and a negative error code on failure.
7411e51764aSArtem Bityutskiy  */
7421e51764aSArtem Bityutskiy static int free_idx_lebs(struct ubifs_info *c)
7431e51764aSArtem Bityutskiy {
7441e51764aSArtem Bityutskiy 	int err;
7451e51764aSArtem Bityutskiy 
7461e51764aSArtem Bityutskiy 	err = free_unused_idx_lebs(c);
7471e51764aSArtem Bityutskiy 	kfree(c->ilebs);
7481e51764aSArtem Bityutskiy 	c->ilebs = NULL;
7491e51764aSArtem Bityutskiy 	return err;
7501e51764aSArtem Bityutskiy }
7511e51764aSArtem Bityutskiy 
7521e51764aSArtem Bityutskiy /**
7531e51764aSArtem Bityutskiy  * ubifs_tnc_start_commit - start TNC commit.
7541e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
7551e51764aSArtem Bityutskiy  * @zroot: new index root position is returned here
7561e51764aSArtem Bityutskiy  *
7571e51764aSArtem Bityutskiy  * This function prepares the list of indexing nodes to commit and lays out
7581e51764aSArtem Bityutskiy  * their positions on flash. If there is not enough free space it uses the
7591e51764aSArtem Bityutskiy  * in-gap commit method. Returns zero in case of success and a negative error
7601e51764aSArtem Bityutskiy  * code in case of failure.
7611e51764aSArtem Bityutskiy  */
7621e51764aSArtem Bityutskiy int ubifs_tnc_start_commit(struct ubifs_info *c, struct ubifs_zbranch *zroot)
7631e51764aSArtem Bityutskiy {
7641e51764aSArtem Bityutskiy 	int err = 0, cnt;
7651e51764aSArtem Bityutskiy 
7661e51764aSArtem Bityutskiy 	mutex_lock(&c->tnc_mutex);
7671e51764aSArtem Bityutskiy 	err = dbg_check_tnc(c, 1);
7681e51764aSArtem Bityutskiy 	if (err)
7691e51764aSArtem Bityutskiy 		goto out;
7701e51764aSArtem Bityutskiy 	cnt = get_znodes_to_commit(c);
7711e51764aSArtem Bityutskiy 	if (cnt != 0) {
7721e51764aSArtem Bityutskiy 		int no_space = 0;
7731e51764aSArtem Bityutskiy 
7741e51764aSArtem Bityutskiy 		err = alloc_idx_lebs(c, cnt);
7751e51764aSArtem Bityutskiy 		if (err == -ENOSPC)
7761e51764aSArtem Bityutskiy 			no_space = 1;
7771e51764aSArtem Bityutskiy 		else if (err)
7781e51764aSArtem Bityutskiy 			goto out_free;
7791e51764aSArtem Bityutskiy 		err = layout_commit(c, no_space, cnt);
7801e51764aSArtem Bityutskiy 		if (err)
7811e51764aSArtem Bityutskiy 			goto out_free;
7821e51764aSArtem Bityutskiy 		ubifs_assert(atomic_long_read(&c->dirty_zn_cnt) == 0);
7831e51764aSArtem Bityutskiy 		err = free_unused_idx_lebs(c);
7841e51764aSArtem Bityutskiy 		if (err)
7851e51764aSArtem Bityutskiy 			goto out;
7861e51764aSArtem Bityutskiy 	}
7871e51764aSArtem Bityutskiy 	destroy_old_idx(c);
7881e51764aSArtem Bityutskiy 	memcpy(zroot, &c->zroot, sizeof(struct ubifs_zbranch));
7891e51764aSArtem Bityutskiy 
7901e51764aSArtem Bityutskiy 	err = ubifs_save_dirty_idx_lnums(c);
7911e51764aSArtem Bityutskiy 	if (err)
7921e51764aSArtem Bityutskiy 		goto out;
7931e51764aSArtem Bityutskiy 
7941e51764aSArtem Bityutskiy 	spin_lock(&c->space_lock);
7951e51764aSArtem Bityutskiy 	/*
7961e51764aSArtem Bityutskiy 	 * Although we have not finished committing yet, update size of the
797b137545cSArtem Bityutskiy 	 * committed index ('c->bi.old_idx_sz') and zero out the index growth
7981e51764aSArtem Bityutskiy 	 * budget. It is OK to do this now, because we've reserved all the
7991e51764aSArtem Bityutskiy 	 * space which is needed to commit the index, and it is save for the
8001e51764aSArtem Bityutskiy 	 * budgeting subsystem to assume the index is already committed,
8011e51764aSArtem Bityutskiy 	 * even though it is not.
8021e51764aSArtem Bityutskiy 	 */
803b137545cSArtem Bityutskiy 	ubifs_assert(c->bi.min_idx_lebs == ubifs_calc_min_idx_lebs(c));
804b137545cSArtem Bityutskiy 	c->bi.old_idx_sz = c->calc_idx_sz;
805b137545cSArtem Bityutskiy 	c->bi.uncommitted_idx = 0;
806b137545cSArtem Bityutskiy 	c->bi.min_idx_lebs = ubifs_calc_min_idx_lebs(c);
8071e51764aSArtem Bityutskiy 	spin_unlock(&c->space_lock);
8081e51764aSArtem Bityutskiy 	mutex_unlock(&c->tnc_mutex);
8091e51764aSArtem Bityutskiy 
8101e51764aSArtem Bityutskiy 	dbg_cmt("number of index LEBs %d", c->lst.idx_lebs);
8111e51764aSArtem Bityutskiy 	dbg_cmt("size of index %llu", c->calc_idx_sz);
8121e51764aSArtem Bityutskiy 	return err;
8131e51764aSArtem Bityutskiy 
8141e51764aSArtem Bityutskiy out_free:
8151e51764aSArtem Bityutskiy 	free_idx_lebs(c);
8161e51764aSArtem Bityutskiy out:
8171e51764aSArtem Bityutskiy 	mutex_unlock(&c->tnc_mutex);
8181e51764aSArtem Bityutskiy 	return err;
8191e51764aSArtem Bityutskiy }
8201e51764aSArtem Bityutskiy 
8211e51764aSArtem Bityutskiy /**
8221e51764aSArtem Bityutskiy  * write_index - write index nodes.
8231e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
8241e51764aSArtem Bityutskiy  *
8251e51764aSArtem Bityutskiy  * This function writes the index nodes whose positions were laid out in the
8261e51764aSArtem Bityutskiy  * layout_in_empty_space function.
8271e51764aSArtem Bityutskiy  */
8281e51764aSArtem Bityutskiy static int write_index(struct ubifs_info *c)
8291e51764aSArtem Bityutskiy {
8301e51764aSArtem Bityutskiy 	struct ubifs_idx_node *idx;
8311e51764aSArtem Bityutskiy 	struct ubifs_znode *znode, *cnext;
8321e51764aSArtem Bityutskiy 	int i, lnum, offs, len, next_len, buf_len, buf_offs, used;
8331e51764aSArtem Bityutskiy 	int avail, wlen, err, lnum_pos = 0;
8341e51764aSArtem Bityutskiy 
8351e51764aSArtem Bityutskiy 	cnext = c->enext;
8361e51764aSArtem Bityutskiy 	if (!cnext)
8371e51764aSArtem Bityutskiy 		return 0;
8381e51764aSArtem Bityutskiy 
8391e51764aSArtem Bityutskiy 	/*
8401e51764aSArtem Bityutskiy 	 * Always write index nodes to the index head so that index nodes and
8411e51764aSArtem Bityutskiy 	 * other types of nodes are never mixed in the same erase block.
8421e51764aSArtem Bityutskiy 	 */
8431e51764aSArtem Bityutskiy 	lnum = c->ihead_lnum;
8441e51764aSArtem Bityutskiy 	buf_offs = c->ihead_offs;
8451e51764aSArtem Bityutskiy 
8461e51764aSArtem Bityutskiy 	/* Allocate commit buffer */
8471e51764aSArtem Bityutskiy 	buf_len = ALIGN(c->max_idx_node_sz, c->min_io_size);
8481e51764aSArtem Bityutskiy 	used = 0;
8491e51764aSArtem Bityutskiy 	avail = buf_len;
8501e51764aSArtem Bityutskiy 
8511e51764aSArtem Bityutskiy 	/* Ensure there is enough room for first write */
8521e51764aSArtem Bityutskiy 	next_len = ubifs_idx_node_sz(c, cnext->child_cnt);
8531e51764aSArtem Bityutskiy 	if (buf_offs + next_len > c->leb_size) {
8541e51764aSArtem Bityutskiy 		err = ubifs_update_one_lp(c, lnum, LPROPS_NC, 0, 0,
8551e51764aSArtem Bityutskiy 					  LPROPS_TAKEN);
8561e51764aSArtem Bityutskiy 		if (err)
8571e51764aSArtem Bityutskiy 			return err;
8581e51764aSArtem Bityutskiy 		lnum = -1;
8591e51764aSArtem Bityutskiy 	}
8601e51764aSArtem Bityutskiy 
8611e51764aSArtem Bityutskiy 	while (1) {
8621e51764aSArtem Bityutskiy 		cond_resched();
8631e51764aSArtem Bityutskiy 
8641e51764aSArtem Bityutskiy 		znode = cnext;
8651e51764aSArtem Bityutskiy 		idx = c->cbuf + used;
8661e51764aSArtem Bityutskiy 
8671e51764aSArtem Bityutskiy 		/* Make index node */
8681e51764aSArtem Bityutskiy 		idx->ch.node_type = UBIFS_IDX_NODE;
8691e51764aSArtem Bityutskiy 		idx->child_cnt = cpu_to_le16(znode->child_cnt);
8701e51764aSArtem Bityutskiy 		idx->level = cpu_to_le16(znode->level);
8711e51764aSArtem Bityutskiy 		for (i = 0; i < znode->child_cnt; i++) {
8721e51764aSArtem Bityutskiy 			struct ubifs_branch *br = ubifs_idx_branch(c, idx, i);
8731e51764aSArtem Bityutskiy 			struct ubifs_zbranch *zbr = &znode->zbranch[i];
8741e51764aSArtem Bityutskiy 
8751e51764aSArtem Bityutskiy 			key_write_idx(c, &zbr->key, &br->key);
8761e51764aSArtem Bityutskiy 			br->lnum = cpu_to_le32(zbr->lnum);
8771e51764aSArtem Bityutskiy 			br->offs = cpu_to_le32(zbr->offs);
8781e51764aSArtem Bityutskiy 			br->len = cpu_to_le32(zbr->len);
8791e51764aSArtem Bityutskiy 			if (!zbr->lnum || !zbr->len) {
8801e51764aSArtem Bityutskiy 				ubifs_err("bad ref in znode");
8811e51764aSArtem Bityutskiy 				dbg_dump_znode(c, znode);
8821e51764aSArtem Bityutskiy 				if (zbr->znode)
8831e51764aSArtem Bityutskiy 					dbg_dump_znode(c, zbr->znode);
8841e51764aSArtem Bityutskiy 			}
8851e51764aSArtem Bityutskiy 		}
8861e51764aSArtem Bityutskiy 		len = ubifs_idx_node_sz(c, znode->child_cnt);
8871e51764aSArtem Bityutskiy 		ubifs_prepare_node(c, idx, len, 0);
8881e51764aSArtem Bityutskiy 
8891e51764aSArtem Bityutskiy 		/* Determine the index node position */
8901e51764aSArtem Bityutskiy 		if (lnum == -1) {
8911e51764aSArtem Bityutskiy 			lnum = c->ilebs[lnum_pos++];
8921e51764aSArtem Bityutskiy 			buf_offs = 0;
8931e51764aSArtem Bityutskiy 			used = 0;
8941e51764aSArtem Bityutskiy 			avail = buf_len;
8951e51764aSArtem Bityutskiy 		}
8961e51764aSArtem Bityutskiy 		offs = buf_offs + used;
8971e51764aSArtem Bityutskiy 
8981e51764aSArtem Bityutskiy #ifdef CONFIG_UBIFS_FS_DEBUG
8991e51764aSArtem Bityutskiy 		if (lnum != znode->lnum || offs != znode->offs ||
9001e51764aSArtem Bityutskiy 		    len != znode->len) {
9011e51764aSArtem Bityutskiy 			ubifs_err("inconsistent znode posn");
9021e51764aSArtem Bityutskiy 			return -EINVAL;
9031e51764aSArtem Bityutskiy 		}
9041e51764aSArtem Bityutskiy #endif
9051e51764aSArtem Bityutskiy 
9061e51764aSArtem Bityutskiy 		/* Grab some stuff from znode while we still can */
9071e51764aSArtem Bityutskiy 		cnext = znode->cnext;
9081e51764aSArtem Bityutskiy 
9091e51764aSArtem Bityutskiy 		ubifs_assert(ubifs_zn_dirty(znode));
9101e51764aSArtem Bityutskiy 		ubifs_assert(test_bit(COW_ZNODE, &znode->flags));
9111e51764aSArtem Bityutskiy 
9121e51764aSArtem Bityutskiy 		/*
9131e51764aSArtem Bityutskiy 		 * It is important that other threads should see %DIRTY_ZNODE
9141e51764aSArtem Bityutskiy 		 * flag cleared before %COW_ZNODE. Specifically, it matters in
9151e51764aSArtem Bityutskiy 		 * the 'dirty_cow_znode()' function. This is the reason for the
9161e51764aSArtem Bityutskiy 		 * first barrier. Also, we want the bit changes to be seen to
9171e51764aSArtem Bityutskiy 		 * other threads ASAP, to avoid unnecesarry copying, which is
9181e51764aSArtem Bityutskiy 		 * the reason for the second barrier.
9191e51764aSArtem Bityutskiy 		 */
9201e51764aSArtem Bityutskiy 		clear_bit(DIRTY_ZNODE, &znode->flags);
9211e51764aSArtem Bityutskiy 		smp_mb__before_clear_bit();
9221e51764aSArtem Bityutskiy 		clear_bit(COW_ZNODE, &znode->flags);
9231e51764aSArtem Bityutskiy 		smp_mb__after_clear_bit();
9241e51764aSArtem Bityutskiy 
9251e51764aSArtem Bityutskiy 		/* Do not access znode from this point on */
9261e51764aSArtem Bityutskiy 
9271e51764aSArtem Bityutskiy 		/* Update buffer positions */
9281e51764aSArtem Bityutskiy 		wlen = used + len;
9291e51764aSArtem Bityutskiy 		used += ALIGN(len, 8);
9301e51764aSArtem Bityutskiy 		avail -= ALIGN(len, 8);
9311e51764aSArtem Bityutskiy 
9321e51764aSArtem Bityutskiy 		/*
9331e51764aSArtem Bityutskiy 		 * Calculate the next index node length to see if there is
9341e51764aSArtem Bityutskiy 		 * enough room for it
9351e51764aSArtem Bityutskiy 		 */
9361e51764aSArtem Bityutskiy 		if (cnext == c->cnext)
9371e51764aSArtem Bityutskiy 			next_len = 0;
9381e51764aSArtem Bityutskiy 		else
9391e51764aSArtem Bityutskiy 			next_len = ubifs_idx_node_sz(c, cnext->child_cnt);
9401e51764aSArtem Bityutskiy 
9411e51764aSArtem Bityutskiy 		if (c->min_io_size == 1) {
9421e51764aSArtem Bityutskiy 			/*
9431e51764aSArtem Bityutskiy 			 * Write the prepared index node immediately if there is
9441e51764aSArtem Bityutskiy 			 * no minimum IO size
9451e51764aSArtem Bityutskiy 			 */
9461e51764aSArtem Bityutskiy 			err = ubifs_leb_write(c, lnum, c->cbuf, buf_offs,
9471e51764aSArtem Bityutskiy 					      wlen, UBI_SHORTTERM);
9481e51764aSArtem Bityutskiy 			if (err)
9491e51764aSArtem Bityutskiy 				return err;
9501e51764aSArtem Bityutskiy 			buf_offs += ALIGN(wlen, 8);
9511e51764aSArtem Bityutskiy 			if (next_len) {
9521e51764aSArtem Bityutskiy 				used = 0;
9531e51764aSArtem Bityutskiy 				avail = buf_len;
9541e51764aSArtem Bityutskiy 				if (buf_offs + next_len > c->leb_size) {
9551e51764aSArtem Bityutskiy 					err = ubifs_update_one_lp(c, lnum,
9561e51764aSArtem Bityutskiy 						LPROPS_NC, 0, 0, LPROPS_TAKEN);
9571e51764aSArtem Bityutskiy 					if (err)
9581e51764aSArtem Bityutskiy 						return err;
9591e51764aSArtem Bityutskiy 					lnum = -1;
9601e51764aSArtem Bityutskiy 				}
9611e51764aSArtem Bityutskiy 				continue;
9621e51764aSArtem Bityutskiy 			}
9631e51764aSArtem Bityutskiy 		} else {
9641e51764aSArtem Bityutskiy 			int blen, nxt_offs = buf_offs + used + next_len;
9651e51764aSArtem Bityutskiy 
9661e51764aSArtem Bityutskiy 			if (next_len && nxt_offs <= c->leb_size) {
9671e51764aSArtem Bityutskiy 				if (avail > 0)
9681e51764aSArtem Bityutskiy 					continue;
9691e51764aSArtem Bityutskiy 				else
9701e51764aSArtem Bityutskiy 					blen = buf_len;
9711e51764aSArtem Bityutskiy 			} else {
9721e51764aSArtem Bityutskiy 				wlen = ALIGN(wlen, 8);
9731e51764aSArtem Bityutskiy 				blen = ALIGN(wlen, c->min_io_size);
9741e51764aSArtem Bityutskiy 				ubifs_pad(c, c->cbuf + wlen, blen - wlen);
9751e51764aSArtem Bityutskiy 			}
9761e51764aSArtem Bityutskiy 			/*
9771e51764aSArtem Bityutskiy 			 * The buffer is full or there are no more znodes
9781e51764aSArtem Bityutskiy 			 * to do
9791e51764aSArtem Bityutskiy 			 */
9801e51764aSArtem Bityutskiy 			err = ubifs_leb_write(c, lnum, c->cbuf, buf_offs,
9811e51764aSArtem Bityutskiy 					      blen, UBI_SHORTTERM);
9821e51764aSArtem Bityutskiy 			if (err)
9831e51764aSArtem Bityutskiy 				return err;
9841e51764aSArtem Bityutskiy 			buf_offs += blen;
9851e51764aSArtem Bityutskiy 			if (next_len) {
9861e51764aSArtem Bityutskiy 				if (nxt_offs > c->leb_size) {
9871e51764aSArtem Bityutskiy 					err = ubifs_update_one_lp(c, lnum,
9881e51764aSArtem Bityutskiy 						LPROPS_NC, 0, 0, LPROPS_TAKEN);
9891e51764aSArtem Bityutskiy 					if (err)
9901e51764aSArtem Bityutskiy 						return err;
9911e51764aSArtem Bityutskiy 					lnum = -1;
9921e51764aSArtem Bityutskiy 				}
9931e51764aSArtem Bityutskiy 				used -= blen;
9941e51764aSArtem Bityutskiy 				if (used < 0)
9951e51764aSArtem Bityutskiy 					used = 0;
9961e51764aSArtem Bityutskiy 				avail = buf_len - used;
9971e51764aSArtem Bityutskiy 				memmove(c->cbuf, c->cbuf + blen, used);
9981e51764aSArtem Bityutskiy 				continue;
9991e51764aSArtem Bityutskiy 			}
10001e51764aSArtem Bityutskiy 		}
10011e51764aSArtem Bityutskiy 		break;
10021e51764aSArtem Bityutskiy 	}
10031e51764aSArtem Bityutskiy 
10041e51764aSArtem Bityutskiy #ifdef CONFIG_UBIFS_FS_DEBUG
100517c2f9f8SArtem Bityutskiy 	if (lnum != c->dbg->new_ihead_lnum ||
100617c2f9f8SArtem Bityutskiy 	    buf_offs != c->dbg->new_ihead_offs) {
10071e51764aSArtem Bityutskiy 		ubifs_err("inconsistent ihead");
10081e51764aSArtem Bityutskiy 		return -EINVAL;
10091e51764aSArtem Bityutskiy 	}
10101e51764aSArtem Bityutskiy #endif
10111e51764aSArtem Bityutskiy 
10121e51764aSArtem Bityutskiy 	c->ihead_lnum = lnum;
10131e51764aSArtem Bityutskiy 	c->ihead_offs = buf_offs;
10141e51764aSArtem Bityutskiy 
10151e51764aSArtem Bityutskiy 	return 0;
10161e51764aSArtem Bityutskiy }
10171e51764aSArtem Bityutskiy 
10181e51764aSArtem Bityutskiy /**
10191e51764aSArtem Bityutskiy  * free_obsolete_znodes - free obsolete znodes.
10201e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
10211e51764aSArtem Bityutskiy  *
10221e51764aSArtem Bityutskiy  * At the end of commit end, obsolete znodes are freed.
10231e51764aSArtem Bityutskiy  */
10241e51764aSArtem Bityutskiy static void free_obsolete_znodes(struct ubifs_info *c)
10251e51764aSArtem Bityutskiy {
10261e51764aSArtem Bityutskiy 	struct ubifs_znode *znode, *cnext;
10271e51764aSArtem Bityutskiy 
10281e51764aSArtem Bityutskiy 	cnext = c->cnext;
10291e51764aSArtem Bityutskiy 	do {
10301e51764aSArtem Bityutskiy 		znode = cnext;
10311e51764aSArtem Bityutskiy 		cnext = znode->cnext;
10321e51764aSArtem Bityutskiy 		if (test_bit(OBSOLETE_ZNODE, &znode->flags))
10331e51764aSArtem Bityutskiy 			kfree(znode);
10341e51764aSArtem Bityutskiy 		else {
10351e51764aSArtem Bityutskiy 			znode->cnext = NULL;
10361e51764aSArtem Bityutskiy 			atomic_long_inc(&c->clean_zn_cnt);
10371e51764aSArtem Bityutskiy 			atomic_long_inc(&ubifs_clean_zn_cnt);
10381e51764aSArtem Bityutskiy 		}
10391e51764aSArtem Bityutskiy 	} while (cnext != c->cnext);
10401e51764aSArtem Bityutskiy }
10411e51764aSArtem Bityutskiy 
10421e51764aSArtem Bityutskiy /**
10431e51764aSArtem Bityutskiy  * return_gap_lebs - return LEBs used by the in-gap commit method.
10441e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
10451e51764aSArtem Bityutskiy  *
10461e51764aSArtem Bityutskiy  * This function clears the "taken" flag for the LEBs which were used by the
10471e51764aSArtem Bityutskiy  * "commit in-the-gaps" method.
10481e51764aSArtem Bityutskiy  */
10491e51764aSArtem Bityutskiy static int return_gap_lebs(struct ubifs_info *c)
10501e51764aSArtem Bityutskiy {
10511e51764aSArtem Bityutskiy 	int *p, err;
10521e51764aSArtem Bityutskiy 
10531e51764aSArtem Bityutskiy 	if (!c->gap_lebs)
10541e51764aSArtem Bityutskiy 		return 0;
10551e51764aSArtem Bityutskiy 
10561e51764aSArtem Bityutskiy 	dbg_cmt("");
10571e51764aSArtem Bityutskiy 	for (p = c->gap_lebs; *p != -1; p++) {
10581e51764aSArtem Bityutskiy 		err = ubifs_change_one_lp(c, *p, LPROPS_NC, LPROPS_NC, 0,
10591e51764aSArtem Bityutskiy 					  LPROPS_TAKEN, 0);
10601e51764aSArtem Bityutskiy 		if (err)
10611e51764aSArtem Bityutskiy 			return err;
10621e51764aSArtem Bityutskiy 	}
10631e51764aSArtem Bityutskiy 
10641e51764aSArtem Bityutskiy 	kfree(c->gap_lebs);
10651e51764aSArtem Bityutskiy 	c->gap_lebs = NULL;
10661e51764aSArtem Bityutskiy 	return 0;
10671e51764aSArtem Bityutskiy }
10681e51764aSArtem Bityutskiy 
10691e51764aSArtem Bityutskiy /**
10701e51764aSArtem Bityutskiy  * ubifs_tnc_end_commit - update the TNC for commit end.
10711e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
10721e51764aSArtem Bityutskiy  *
10731e51764aSArtem Bityutskiy  * Write the dirty znodes.
10741e51764aSArtem Bityutskiy  */
10751e51764aSArtem Bityutskiy int ubifs_tnc_end_commit(struct ubifs_info *c)
10761e51764aSArtem Bityutskiy {
10771e51764aSArtem Bityutskiy 	int err;
10781e51764aSArtem Bityutskiy 
10791e51764aSArtem Bityutskiy 	if (!c->cnext)
10801e51764aSArtem Bityutskiy 		return 0;
10811e51764aSArtem Bityutskiy 
10821e51764aSArtem Bityutskiy 	err = return_gap_lebs(c);
10831e51764aSArtem Bityutskiy 	if (err)
10841e51764aSArtem Bityutskiy 		return err;
10851e51764aSArtem Bityutskiy 
10861e51764aSArtem Bityutskiy 	err = write_index(c);
10871e51764aSArtem Bityutskiy 	if (err)
10881e51764aSArtem Bityutskiy 		return err;
10891e51764aSArtem Bityutskiy 
10901e51764aSArtem Bityutskiy 	mutex_lock(&c->tnc_mutex);
10911e51764aSArtem Bityutskiy 
10921e51764aSArtem Bityutskiy 	dbg_cmt("TNC height is %d", c->zroot.znode->level + 1);
10931e51764aSArtem Bityutskiy 
10941e51764aSArtem Bityutskiy 	free_obsolete_znodes(c);
10951e51764aSArtem Bityutskiy 
10961e51764aSArtem Bityutskiy 	c->cnext = NULL;
10971e51764aSArtem Bityutskiy 	kfree(c->ilebs);
10981e51764aSArtem Bityutskiy 	c->ilebs = NULL;
10991e51764aSArtem Bityutskiy 
11001e51764aSArtem Bityutskiy 	mutex_unlock(&c->tnc_mutex);
11011e51764aSArtem Bityutskiy 
11021e51764aSArtem Bityutskiy 	return 0;
11031e51764aSArtem Bityutskiy }
1104