xref: /openbmc/linux/fs/ubifs/log.c (revision 0898782247ae533d1f4e47a06bc5d4870931b284)
12b27bdccSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21e51764aSArtem Bityutskiy /*
31e51764aSArtem Bityutskiy  * This file is part of UBIFS.
41e51764aSArtem Bityutskiy  *
51e51764aSArtem Bityutskiy  * Copyright (C) 2006-2008 Nokia Corporation.
61e51764aSArtem Bityutskiy  *
71e51764aSArtem Bityutskiy  * Authors: Artem Bityutskiy (Битюцкий Артём)
81e51764aSArtem Bityutskiy  *          Adrian Hunter
91e51764aSArtem Bityutskiy  */
101e51764aSArtem Bityutskiy 
111e51764aSArtem Bityutskiy /*
121e51764aSArtem Bityutskiy  * This file is a part of UBIFS journal implementation and contains various
131e51764aSArtem Bityutskiy  * functions which manipulate the log. The log is a fixed area on the flash
141e51764aSArtem Bityutskiy  * which does not contain any data but refers to buds. The log is a part of the
151e51764aSArtem Bityutskiy  * journal.
161e51764aSArtem Bityutskiy  */
171e51764aSArtem Bityutskiy 
181e51764aSArtem Bityutskiy #include "ubifs.h"
191e51764aSArtem Bityutskiy 
201e51764aSArtem Bityutskiy static int dbg_check_bud_bytes(struct ubifs_info *c);
211e51764aSArtem Bityutskiy 
221e51764aSArtem Bityutskiy /**
231e51764aSArtem Bityutskiy  * ubifs_search_bud - search bud LEB.
241e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
251e51764aSArtem Bityutskiy  * @lnum: logical eraseblock number to search
261e51764aSArtem Bityutskiy  *
271e51764aSArtem Bityutskiy  * This function searches bud LEB @lnum. Returns bud description object in case
281e51764aSArtem Bityutskiy  * of success and %NULL if there is no bud with this LEB number.
291e51764aSArtem Bityutskiy  */
ubifs_search_bud(struct ubifs_info * c,int lnum)301e51764aSArtem Bityutskiy struct ubifs_bud *ubifs_search_bud(struct ubifs_info *c, int lnum)
311e51764aSArtem Bityutskiy {
321e51764aSArtem Bityutskiy 	struct rb_node *p;
331e51764aSArtem Bityutskiy 	struct ubifs_bud *bud;
341e51764aSArtem Bityutskiy 
351e51764aSArtem Bityutskiy 	spin_lock(&c->buds_lock);
361e51764aSArtem Bityutskiy 	p = c->buds.rb_node;
371e51764aSArtem Bityutskiy 	while (p) {
381e51764aSArtem Bityutskiy 		bud = rb_entry(p, struct ubifs_bud, rb);
391e51764aSArtem Bityutskiy 		if (lnum < bud->lnum)
401e51764aSArtem Bityutskiy 			p = p->rb_left;
411e51764aSArtem Bityutskiy 		else if (lnum > bud->lnum)
421e51764aSArtem Bityutskiy 			p = p->rb_right;
431e51764aSArtem Bityutskiy 		else {
441e51764aSArtem Bityutskiy 			spin_unlock(&c->buds_lock);
451e51764aSArtem Bityutskiy 			return bud;
461e51764aSArtem Bityutskiy 		}
471e51764aSArtem Bityutskiy 	}
481e51764aSArtem Bityutskiy 	spin_unlock(&c->buds_lock);
491e51764aSArtem Bityutskiy 	return NULL;
501e51764aSArtem Bityutskiy }
511e51764aSArtem Bityutskiy 
521e51764aSArtem Bityutskiy /**
531e51764aSArtem Bityutskiy  * ubifs_get_wbuf - get the wbuf associated with a LEB, if there is one.
541e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
551e51764aSArtem Bityutskiy  * @lnum: logical eraseblock number to search
561e51764aSArtem Bityutskiy  *
571e51764aSArtem Bityutskiy  * This functions returns the wbuf for @lnum or %NULL if there is not one.
581e51764aSArtem Bityutskiy  */
ubifs_get_wbuf(struct ubifs_info * c,int lnum)591e51764aSArtem Bityutskiy struct ubifs_wbuf *ubifs_get_wbuf(struct ubifs_info *c, int lnum)
601e51764aSArtem Bityutskiy {
611e51764aSArtem Bityutskiy 	struct rb_node *p;
621e51764aSArtem Bityutskiy 	struct ubifs_bud *bud;
631e51764aSArtem Bityutskiy 	int jhead;
641e51764aSArtem Bityutskiy 
651e51764aSArtem Bityutskiy 	if (!c->jheads)
661e51764aSArtem Bityutskiy 		return NULL;
671e51764aSArtem Bityutskiy 
681e51764aSArtem Bityutskiy 	spin_lock(&c->buds_lock);
691e51764aSArtem Bityutskiy 	p = c->buds.rb_node;
701e51764aSArtem Bityutskiy 	while (p) {
711e51764aSArtem Bityutskiy 		bud = rb_entry(p, struct ubifs_bud, rb);
721e51764aSArtem Bityutskiy 		if (lnum < bud->lnum)
731e51764aSArtem Bityutskiy 			p = p->rb_left;
741e51764aSArtem Bityutskiy 		else if (lnum > bud->lnum)
751e51764aSArtem Bityutskiy 			p = p->rb_right;
761e51764aSArtem Bityutskiy 		else {
771e51764aSArtem Bityutskiy 			jhead = bud->jhead;
781e51764aSArtem Bityutskiy 			spin_unlock(&c->buds_lock);
791e51764aSArtem Bityutskiy 			return &c->jheads[jhead].wbuf;
801e51764aSArtem Bityutskiy 		}
811e51764aSArtem Bityutskiy 	}
821e51764aSArtem Bityutskiy 	spin_unlock(&c->buds_lock);
831e51764aSArtem Bityutskiy 	return NULL;
841e51764aSArtem Bityutskiy }
851e51764aSArtem Bityutskiy 
861e51764aSArtem Bityutskiy /**
871e51764aSArtem Bityutskiy  * empty_log_bytes - calculate amount of empty space in the log.
881e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
891e51764aSArtem Bityutskiy  */
empty_log_bytes(const struct ubifs_info * c)901e51764aSArtem Bityutskiy static inline long long empty_log_bytes(const struct ubifs_info *c)
911e51764aSArtem Bityutskiy {
921e51764aSArtem Bityutskiy 	long long h, t;
931e51764aSArtem Bityutskiy 
941e51764aSArtem Bityutskiy 	h = (long long)c->lhead_lnum * c->leb_size + c->lhead_offs;
951e51764aSArtem Bityutskiy 	t = (long long)c->ltail_lnum * c->leb_size;
961e51764aSArtem Bityutskiy 
97ba29e721SArtem Bityutskiy 	if (h > t)
981e51764aSArtem Bityutskiy 		return c->log_bytes - h + t;
99ba29e721SArtem Bityutskiy 	else if (h != t)
1001e51764aSArtem Bityutskiy 		return t - h;
101ba29e721SArtem Bityutskiy 	else if (c->lhead_lnum != c->ltail_lnum)
102ba29e721SArtem Bityutskiy 		return 0;
103ba29e721SArtem Bityutskiy 	else
104ba29e721SArtem Bityutskiy 		return c->log_bytes;
1051e51764aSArtem Bityutskiy }
1061e51764aSArtem Bityutskiy 
1071e51764aSArtem Bityutskiy /**
1081e51764aSArtem Bityutskiy  * ubifs_add_bud - add bud LEB to the tree of buds and its journal head list.
1091e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
1101e51764aSArtem Bityutskiy  * @bud: the bud to add
1111e51764aSArtem Bityutskiy  */
ubifs_add_bud(struct ubifs_info * c,struct ubifs_bud * bud)1121e51764aSArtem Bityutskiy void ubifs_add_bud(struct ubifs_info *c, struct ubifs_bud *bud)
1131e51764aSArtem Bityutskiy {
1141e51764aSArtem Bityutskiy 	struct rb_node **p, *parent = NULL;
1151e51764aSArtem Bityutskiy 	struct ubifs_bud *b;
1161e51764aSArtem Bityutskiy 	struct ubifs_jhead *jhead;
1171e51764aSArtem Bityutskiy 
1181e51764aSArtem Bityutskiy 	spin_lock(&c->buds_lock);
1191e51764aSArtem Bityutskiy 	p = &c->buds.rb_node;
1201e51764aSArtem Bityutskiy 	while (*p) {
1211e51764aSArtem Bityutskiy 		parent = *p;
1221e51764aSArtem Bityutskiy 		b = rb_entry(parent, struct ubifs_bud, rb);
1236eb61d58SRichard Weinberger 		ubifs_assert(c, bud->lnum != b->lnum);
1241e51764aSArtem Bityutskiy 		if (bud->lnum < b->lnum)
1251e51764aSArtem Bityutskiy 			p = &(*p)->rb_left;
1261e51764aSArtem Bityutskiy 		else
1271e51764aSArtem Bityutskiy 			p = &(*p)->rb_right;
1281e51764aSArtem Bityutskiy 	}
1291e51764aSArtem Bityutskiy 
1301e51764aSArtem Bityutskiy 	rb_link_node(&bud->rb, parent, p);
1311e51764aSArtem Bityutskiy 	rb_insert_color(&bud->rb, &c->buds);
1321e51764aSArtem Bityutskiy 	if (c->jheads) {
1331e51764aSArtem Bityutskiy 		jhead = &c->jheads[bud->jhead];
1341e51764aSArtem Bityutskiy 		list_add_tail(&bud->list, &jhead->buds_list);
1351e51764aSArtem Bityutskiy 	} else
1366eb61d58SRichard Weinberger 		ubifs_assert(c, c->replaying && c->ro_mount);
1371e51764aSArtem Bityutskiy 
1381e51764aSArtem Bityutskiy 	/*
1391e51764aSArtem Bityutskiy 	 * Note, although this is a new bud, we anyway account this space now,
1401e51764aSArtem Bityutskiy 	 * before any data has been written to it, because this is about to
1411e51764aSArtem Bityutskiy 	 * guarantee fixed mount time, and this bud will anyway be read and
1421e51764aSArtem Bityutskiy 	 * scanned.
1431e51764aSArtem Bityutskiy 	 */
1441e51764aSArtem Bityutskiy 	c->bud_bytes += c->leb_size - bud->start;
1451e51764aSArtem Bityutskiy 
14677a7ae58SArtem Bityutskiy 	dbg_log("LEB %d:%d, jhead %s, bud_bytes %lld", bud->lnum,
14777a7ae58SArtem Bityutskiy 		bud->start, dbg_jhead(bud->jhead), c->bud_bytes);
1481e51764aSArtem Bityutskiy 	spin_unlock(&c->buds_lock);
1491e51764aSArtem Bityutskiy }
1501e51764aSArtem Bityutskiy 
1511e51764aSArtem Bityutskiy /**
1521e51764aSArtem Bityutskiy  * ubifs_add_bud_to_log - add a new bud to the log.
1531e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
1541e51764aSArtem Bityutskiy  * @jhead: journal head the bud belongs to
1551e51764aSArtem Bityutskiy  * @lnum: LEB number of the bud
1561e51764aSArtem Bityutskiy  * @offs: starting offset of the bud
1571e51764aSArtem Bityutskiy  *
15871d561f0SSascha Hauer  * This function writes a reference node for the new bud LEB @lnum to the log,
15971d561f0SSascha Hauer  * and adds it to the buds trees. It also makes sure that log size does not
1601e51764aSArtem Bityutskiy  * exceed the 'c->max_bud_bytes' limit. Returns zero in case of success,
16171d561f0SSascha Hauer  * %-EAGAIN if commit is required, and a negative error code in case of
1621e51764aSArtem Bityutskiy  * failure.
1631e51764aSArtem Bityutskiy  */
ubifs_add_bud_to_log(struct ubifs_info * c,int jhead,int lnum,int offs)1641e51764aSArtem Bityutskiy int ubifs_add_bud_to_log(struct ubifs_info *c, int jhead, int lnum, int offs)
1651e51764aSArtem Bityutskiy {
1661e51764aSArtem Bityutskiy 	int err;
1671e51764aSArtem Bityutskiy 	struct ubifs_bud *bud;
1681e51764aSArtem Bityutskiy 	struct ubifs_ref_node *ref;
1691e51764aSArtem Bityutskiy 
1701e51764aSArtem Bityutskiy 	bud = kmalloc(sizeof(struct ubifs_bud), GFP_NOFS);
1711e51764aSArtem Bityutskiy 	if (!bud)
1721e51764aSArtem Bityutskiy 		return -ENOMEM;
1731e51764aSArtem Bityutskiy 	ref = kzalloc(c->ref_node_alsz, GFP_NOFS);
1741e51764aSArtem Bityutskiy 	if (!ref) {
1751e51764aSArtem Bityutskiy 		kfree(bud);
1761e51764aSArtem Bityutskiy 		return -ENOMEM;
1771e51764aSArtem Bityutskiy 	}
1781e51764aSArtem Bityutskiy 
1791e51764aSArtem Bityutskiy 	mutex_lock(&c->log_mutex);
1806eb61d58SRichard Weinberger 	ubifs_assert(c, !c->ro_media && !c->ro_mount);
1812680d722SArtem Bityutskiy 	if (c->ro_error) {
1821e51764aSArtem Bityutskiy 		err = -EROFS;
1831e51764aSArtem Bityutskiy 		goto out_unlock;
1841e51764aSArtem Bityutskiy 	}
1851e51764aSArtem Bityutskiy 
1861e51764aSArtem Bityutskiy 	/* Make sure we have enough space in the log */
1871e51764aSArtem Bityutskiy 	if (empty_log_bytes(c) - c->ref_node_alsz < c->min_log_bytes) {
1881e51764aSArtem Bityutskiy 		dbg_log("not enough log space - %lld, required %d",
1891e51764aSArtem Bityutskiy 			empty_log_bytes(c), c->min_log_bytes);
1901e51764aSArtem Bityutskiy 		ubifs_commit_required(c);
1911e51764aSArtem Bityutskiy 		err = -EAGAIN;
1921e51764aSArtem Bityutskiy 		goto out_unlock;
1931e51764aSArtem Bityutskiy 	}
1941e51764aSArtem Bityutskiy 
1951e51764aSArtem Bityutskiy 	/*
1967d4e9ccbSArtem Bityutskiy 	 * Make sure the amount of space in buds will not exceed the
1971e51764aSArtem Bityutskiy 	 * 'c->max_bud_bytes' limit, because we want to guarantee mount time
1981e51764aSArtem Bityutskiy 	 * limits.
1991e51764aSArtem Bityutskiy 	 *
2001e51764aSArtem Bityutskiy 	 * It is not necessary to hold @c->buds_lock when reading @c->bud_bytes
2011e51764aSArtem Bityutskiy 	 * because we are holding @c->log_mutex. All @c->bud_bytes take place
2021e51764aSArtem Bityutskiy 	 * when both @c->log_mutex and @c->bud_bytes are locked.
2031e51764aSArtem Bityutskiy 	 */
2041e51764aSArtem Bityutskiy 	if (c->bud_bytes + c->leb_size - offs > c->max_bud_bytes) {
2051e51764aSArtem Bityutskiy 		dbg_log("bud bytes %lld (%lld max), require commit",
2061e51764aSArtem Bityutskiy 			c->bud_bytes, c->max_bud_bytes);
2071e51764aSArtem Bityutskiy 		ubifs_commit_required(c);
2081e51764aSArtem Bityutskiy 		err = -EAGAIN;
2091e51764aSArtem Bityutskiy 		goto out_unlock;
2101e51764aSArtem Bityutskiy 	}
2111e51764aSArtem Bityutskiy 
2121e51764aSArtem Bityutskiy 	/*
2131e51764aSArtem Bityutskiy 	 * If the journal is full enough - start background commit. Note, it is
2141e51764aSArtem Bityutskiy 	 * OK to read 'c->cmt_state' without spinlock because integer reads
2151e51764aSArtem Bityutskiy 	 * are atomic in the kernel.
2161e51764aSArtem Bityutskiy 	 */
2171e51764aSArtem Bityutskiy 	if (c->bud_bytes >= c->bg_bud_bytes &&
2181e51764aSArtem Bityutskiy 	    c->cmt_state == COMMIT_RESTING) {
2191e51764aSArtem Bityutskiy 		dbg_log("bud bytes %lld (%lld max), initiate BG commit",
2201e51764aSArtem Bityutskiy 			c->bud_bytes, c->max_bud_bytes);
2211e51764aSArtem Bityutskiy 		ubifs_request_bg_commit(c);
2221e51764aSArtem Bityutskiy 	}
2231e51764aSArtem Bityutskiy 
2241e51764aSArtem Bityutskiy 	bud->lnum = lnum;
2251e51764aSArtem Bityutskiy 	bud->start = offs;
2261e51764aSArtem Bityutskiy 	bud->jhead = jhead;
2276a98bc46SSascha Hauer 	bud->log_hash = NULL;
2281e51764aSArtem Bityutskiy 
2291e51764aSArtem Bityutskiy 	ref->ch.node_type = UBIFS_REF_NODE;
2301e51764aSArtem Bityutskiy 	ref->lnum = cpu_to_le32(bud->lnum);
2311e51764aSArtem Bityutskiy 	ref->offs = cpu_to_le32(bud->start);
2321e51764aSArtem Bityutskiy 	ref->jhead = cpu_to_le32(jhead);
2331e51764aSArtem Bityutskiy 
2341e51764aSArtem Bityutskiy 	if (c->lhead_offs > c->leb_size - c->ref_node_alsz) {
235e11602eaSArtem Bityutskiy 		c->lhead_lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
2366eb61d58SRichard Weinberger 		ubifs_assert(c, c->lhead_lnum != c->ltail_lnum);
2371e51764aSArtem Bityutskiy 		c->lhead_offs = 0;
2381e51764aSArtem Bityutskiy 	}
2391e51764aSArtem Bityutskiy 
2401e51764aSArtem Bityutskiy 	if (c->lhead_offs == 0) {
2411e51764aSArtem Bityutskiy 		/* Must ensure next log LEB has been unmapped */
2421e51764aSArtem Bityutskiy 		err = ubifs_leb_unmap(c, c->lhead_lnum);
2431e51764aSArtem Bityutskiy 		if (err)
2441e51764aSArtem Bityutskiy 			goto out_unlock;
2451e51764aSArtem Bityutskiy 	}
2461e51764aSArtem Bityutskiy 
2471e51764aSArtem Bityutskiy 	if (bud->start == 0) {
2481e51764aSArtem Bityutskiy 		/*
2491e51764aSArtem Bityutskiy 		 * Before writing the LEB reference which refers an empty LEB
2501e51764aSArtem Bityutskiy 		 * to the log, we have to make sure it is mapped, because
2511e51764aSArtem Bityutskiy 		 * otherwise we'd risk to refer an LEB with garbage in case of
2521e51764aSArtem Bityutskiy 		 * an unclean reboot, because the target LEB might have been
2531e51764aSArtem Bityutskiy 		 * unmapped, but not yet physically erased.
2541e51764aSArtem Bityutskiy 		 */
255b36a261eSRichard Weinberger 		err = ubifs_leb_map(c, bud->lnum);
2561e51764aSArtem Bityutskiy 		if (err)
2571e51764aSArtem Bityutskiy 			goto out_unlock;
2581e51764aSArtem Bityutskiy 	}
2591e51764aSArtem Bityutskiy 
2601e51764aSArtem Bityutskiy 	dbg_log("write ref LEB %d:%d",
2611e51764aSArtem Bityutskiy 		c->lhead_lnum, c->lhead_offs);
2621e51764aSArtem Bityutskiy 	err = ubifs_write_node(c, ref, UBIFS_REF_NODE_SZ, c->lhead_lnum,
263b36a261eSRichard Weinberger 			       c->lhead_offs);
2641e51764aSArtem Bityutskiy 	if (err)
2651e51764aSArtem Bityutskiy 		goto out_unlock;
2661e51764aSArtem Bityutskiy 
2676a98bc46SSascha Hauer 	err = ubifs_shash_update(c, c->log_hash, ref, UBIFS_REF_NODE_SZ);
2686a98bc46SSascha Hauer 	if (err)
2696a98bc46SSascha Hauer 		goto out_unlock;
2706a98bc46SSascha Hauer 
2716a98bc46SSascha Hauer 	err = ubifs_shash_copy_state(c, c->log_hash, c->jheads[jhead].log_hash);
2726a98bc46SSascha Hauer 	if (err)
2736a98bc46SSascha Hauer 		goto out_unlock;
2746a98bc46SSascha Hauer 
2751e51764aSArtem Bityutskiy 	c->lhead_offs += c->ref_node_alsz;
2761e51764aSArtem Bityutskiy 
2771e51764aSArtem Bityutskiy 	ubifs_add_bud(c, bud);
2781e51764aSArtem Bityutskiy 
2791e51764aSArtem Bityutskiy 	mutex_unlock(&c->log_mutex);
2801e51764aSArtem Bityutskiy 	kfree(ref);
2811e51764aSArtem Bityutskiy 	return 0;
2821e51764aSArtem Bityutskiy 
2831e51764aSArtem Bityutskiy out_unlock:
2841e51764aSArtem Bityutskiy 	mutex_unlock(&c->log_mutex);
2851e51764aSArtem Bityutskiy 	kfree(ref);
2861e51764aSArtem Bityutskiy 	kfree(bud);
2871e51764aSArtem Bityutskiy 	return err;
2881e51764aSArtem Bityutskiy }
2891e51764aSArtem Bityutskiy 
2901e51764aSArtem Bityutskiy /**
2911e51764aSArtem Bityutskiy  * remove_buds - remove used buds.
2921e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
2931e51764aSArtem Bityutskiy  *
2941e51764aSArtem Bityutskiy  * This function removes use buds from the buds tree. It does not remove the
2951e51764aSArtem Bityutskiy  * buds which are pointed to by journal heads.
2961e51764aSArtem Bityutskiy  */
remove_buds(struct ubifs_info * c)2971e51764aSArtem Bityutskiy static void remove_buds(struct ubifs_info *c)
2981e51764aSArtem Bityutskiy {
2991e51764aSArtem Bityutskiy 	struct rb_node *p;
3001e51764aSArtem Bityutskiy 
3016eb61d58SRichard Weinberger 	ubifs_assert(c, list_empty(&c->old_buds));
3021e51764aSArtem Bityutskiy 	c->cmt_bud_bytes = 0;
3031e51764aSArtem Bityutskiy 	spin_lock(&c->buds_lock);
3041e51764aSArtem Bityutskiy 	p = rb_first(&c->buds);
3051e51764aSArtem Bityutskiy 	while (p) {
3061e51764aSArtem Bityutskiy 		struct rb_node *p1 = p;
3071e51764aSArtem Bityutskiy 		struct ubifs_bud *bud;
3081e51764aSArtem Bityutskiy 		struct ubifs_wbuf *wbuf;
3091e51764aSArtem Bityutskiy 
3101e51764aSArtem Bityutskiy 		p = rb_next(p);
3111e51764aSArtem Bityutskiy 		bud = rb_entry(p1, struct ubifs_bud, rb);
3121e51764aSArtem Bityutskiy 		wbuf = &c->jheads[bud->jhead].wbuf;
3131e51764aSArtem Bityutskiy 
3141e51764aSArtem Bityutskiy 		if (wbuf->lnum == bud->lnum) {
3151e51764aSArtem Bityutskiy 			/*
3161e51764aSArtem Bityutskiy 			 * Do not remove buds which are pointed to by journal
3171e51764aSArtem Bityutskiy 			 * heads (non-closed buds).
3181e51764aSArtem Bityutskiy 			 */
3191e51764aSArtem Bityutskiy 			c->cmt_bud_bytes += wbuf->offs - bud->start;
32079fda517SArtem Bityutskiy 			dbg_log("preserve %d:%d, jhead %s, bud bytes %d, cmt_bud_bytes %lld",
32179fda517SArtem Bityutskiy 				bud->lnum, bud->start, dbg_jhead(bud->jhead),
32279fda517SArtem Bityutskiy 				wbuf->offs - bud->start, c->cmt_bud_bytes);
3231e51764aSArtem Bityutskiy 			bud->start = wbuf->offs;
3241e51764aSArtem Bityutskiy 		} else {
3251e51764aSArtem Bityutskiy 			c->cmt_bud_bytes += c->leb_size - bud->start;
32679fda517SArtem Bityutskiy 			dbg_log("remove %d:%d, jhead %s, bud bytes %d, cmt_bud_bytes %lld",
32779fda517SArtem Bityutskiy 				bud->lnum, bud->start, dbg_jhead(bud->jhead),
32879fda517SArtem Bityutskiy 				c->leb_size - bud->start, c->cmt_bud_bytes);
3291e51764aSArtem Bityutskiy 			rb_erase(p1, &c->buds);
3301e51764aSArtem Bityutskiy 			/*
3311e51764aSArtem Bityutskiy 			 * If the commit does not finish, the recovery will need
3321e51764aSArtem Bityutskiy 			 * to replay the journal, in which case the old buds
3331e51764aSArtem Bityutskiy 			 * must be unchanged. Do not release them until post
3341e51764aSArtem Bityutskiy 			 * commit i.e. do not allow them to be garbage
3351e51764aSArtem Bityutskiy 			 * collected.
3361e51764aSArtem Bityutskiy 			 */
337ec32816fSEric Sesterhenn 			list_move(&bud->list, &c->old_buds);
3381e51764aSArtem Bityutskiy 		}
3391e51764aSArtem Bityutskiy 	}
3401e51764aSArtem Bityutskiy 	spin_unlock(&c->buds_lock);
3411e51764aSArtem Bityutskiy }
3421e51764aSArtem Bityutskiy 
3431e51764aSArtem Bityutskiy /**
3441e51764aSArtem Bityutskiy  * ubifs_log_start_commit - start commit.
3451e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
3461e51764aSArtem Bityutskiy  * @ltail_lnum: return new log tail LEB number
3471e51764aSArtem Bityutskiy  *
3481e51764aSArtem Bityutskiy  * The commit operation starts with writing "commit start" node to the log and
3491e51764aSArtem Bityutskiy  * reference nodes for all journal heads which will define new journal after
3501e51764aSArtem Bityutskiy  * the commit has been finished. The commit start and reference nodes are
3511e51764aSArtem Bityutskiy  * written in one go to the nearest empty log LEB (hence, when commit is
3521e51764aSArtem Bityutskiy  * finished UBIFS may safely unmap all the previous log LEBs). This function
3531e51764aSArtem Bityutskiy  * returns zero in case of success and a negative error code in case of
3541e51764aSArtem Bityutskiy  * failure.
3551e51764aSArtem Bityutskiy  */
ubifs_log_start_commit(struct ubifs_info * c,int * ltail_lnum)3561e51764aSArtem Bityutskiy int ubifs_log_start_commit(struct ubifs_info *c, int *ltail_lnum)
3571e51764aSArtem Bityutskiy {
3581e51764aSArtem Bityutskiy 	void *buf;
3591e51764aSArtem Bityutskiy 	struct ubifs_cs_node *cs;
3601e51764aSArtem Bityutskiy 	struct ubifs_ref_node *ref;
3611e51764aSArtem Bityutskiy 	int err, i, max_len, len;
3621e51764aSArtem Bityutskiy 
3631e51764aSArtem Bityutskiy 	err = dbg_check_bud_bytes(c);
3641e51764aSArtem Bityutskiy 	if (err)
3651e51764aSArtem Bityutskiy 		return err;
3661e51764aSArtem Bityutskiy 
3671e51764aSArtem Bityutskiy 	max_len = UBIFS_CS_NODE_SZ + c->jhead_cnt * UBIFS_REF_NODE_SZ;
3681e51764aSArtem Bityutskiy 	max_len = ALIGN(max_len, c->min_io_size);
3691e51764aSArtem Bityutskiy 	buf = cs = kmalloc(max_len, GFP_NOFS);
3701e51764aSArtem Bityutskiy 	if (!buf)
3711e51764aSArtem Bityutskiy 		return -ENOMEM;
3721e51764aSArtem Bityutskiy 
3731e51764aSArtem Bityutskiy 	cs->ch.node_type = UBIFS_CS_NODE;
374014eb04bSArtem Bityutskiy 	cs->cmt_no = cpu_to_le64(c->cmt_no);
3751e51764aSArtem Bityutskiy 	ubifs_prepare_node(c, cs, UBIFS_CS_NODE_SZ, 0);
3761e51764aSArtem Bityutskiy 
3776a98bc46SSascha Hauer 	err = ubifs_shash_init(c, c->log_hash);
3786a98bc46SSascha Hauer 	if (err)
3796a98bc46SSascha Hauer 		goto out;
3806a98bc46SSascha Hauer 
3816a98bc46SSascha Hauer 	err = ubifs_shash_update(c, c->log_hash, cs, UBIFS_CS_NODE_SZ);
3826a98bc46SSascha Hauer 	if (err < 0)
3836a98bc46SSascha Hauer 		goto out;
3846a98bc46SSascha Hauer 
3851e51764aSArtem Bityutskiy 	/*
3861e51764aSArtem Bityutskiy 	 * Note, we do not lock 'c->log_mutex' because this is the commit start
3871e51764aSArtem Bityutskiy 	 * phase and we are exclusively using the log. And we do not lock
3881e51764aSArtem Bityutskiy 	 * write-buffer because nobody can write to the file-system at this
3891e51764aSArtem Bityutskiy 	 * phase.
3901e51764aSArtem Bityutskiy 	 */
3911e51764aSArtem Bityutskiy 
3921e51764aSArtem Bityutskiy 	len = UBIFS_CS_NODE_SZ;
3931e51764aSArtem Bityutskiy 	for (i = 0; i < c->jhead_cnt; i++) {
3941e51764aSArtem Bityutskiy 		int lnum = c->jheads[i].wbuf.lnum;
3951e51764aSArtem Bityutskiy 		int offs = c->jheads[i].wbuf.offs;
3961e51764aSArtem Bityutskiy 
3971e51764aSArtem Bityutskiy 		if (lnum == -1 || offs == c->leb_size)
3981e51764aSArtem Bityutskiy 			continue;
3991e51764aSArtem Bityutskiy 
40077a7ae58SArtem Bityutskiy 		dbg_log("add ref to LEB %d:%d for jhead %s",
40177a7ae58SArtem Bityutskiy 			lnum, offs, dbg_jhead(i));
4021e51764aSArtem Bityutskiy 		ref = buf + len;
4031e51764aSArtem Bityutskiy 		ref->ch.node_type = UBIFS_REF_NODE;
4041e51764aSArtem Bityutskiy 		ref->lnum = cpu_to_le32(lnum);
4051e51764aSArtem Bityutskiy 		ref->offs = cpu_to_le32(offs);
4061e51764aSArtem Bityutskiy 		ref->jhead = cpu_to_le32(i);
4071e51764aSArtem Bityutskiy 
4081e51764aSArtem Bityutskiy 		ubifs_prepare_node(c, ref, UBIFS_REF_NODE_SZ, 0);
4091e51764aSArtem Bityutskiy 		len += UBIFS_REF_NODE_SZ;
4106a98bc46SSascha Hauer 
4116a98bc46SSascha Hauer 		err = ubifs_shash_update(c, c->log_hash, ref,
4126a98bc46SSascha Hauer 					 UBIFS_REF_NODE_SZ);
4136a98bc46SSascha Hauer 		if (err)
4146a98bc46SSascha Hauer 			goto out;
4156a98bc46SSascha Hauer 		ubifs_shash_copy_state(c, c->log_hash, c->jheads[i].log_hash);
4161e51764aSArtem Bityutskiy 	}
4171e51764aSArtem Bityutskiy 
4181e51764aSArtem Bityutskiy 	ubifs_pad(c, buf + len, ALIGN(len, c->min_io_size) - len);
4191e51764aSArtem Bityutskiy 
4201e51764aSArtem Bityutskiy 	/* Switch to the next log LEB */
4211e51764aSArtem Bityutskiy 	if (c->lhead_offs) {
422e11602eaSArtem Bityutskiy 		c->lhead_lnum = ubifs_next_log_lnum(c, c->lhead_lnum);
4236eb61d58SRichard Weinberger 		ubifs_assert(c, c->lhead_lnum != c->ltail_lnum);
4241e51764aSArtem Bityutskiy 		c->lhead_offs = 0;
4251e51764aSArtem Bityutskiy 	}
4261e51764aSArtem Bityutskiy 
4271e51764aSArtem Bityutskiy 	/* Must ensure next LEB has been unmapped */
4281e51764aSArtem Bityutskiy 	err = ubifs_leb_unmap(c, c->lhead_lnum);
4291e51764aSArtem Bityutskiy 	if (err)
4301e51764aSArtem Bityutskiy 		goto out;
4311e51764aSArtem Bityutskiy 
4321e51764aSArtem Bityutskiy 	len = ALIGN(len, c->min_io_size);
4331e51764aSArtem Bityutskiy 	dbg_log("writing commit start at LEB %d:0, len %d", c->lhead_lnum, len);
434b36a261eSRichard Weinberger 	err = ubifs_leb_write(c, c->lhead_lnum, cs, 0, len);
4351e51764aSArtem Bityutskiy 	if (err)
4361e51764aSArtem Bityutskiy 		goto out;
4371e51764aSArtem Bityutskiy 
4381e51764aSArtem Bityutskiy 	*ltail_lnum = c->lhead_lnum;
4391e51764aSArtem Bityutskiy 
4401e51764aSArtem Bityutskiy 	c->lhead_offs += len;
441*8ba0a2abSLiu Song 	ubifs_assert(c, c->lhead_offs < c->leb_size);
4421e51764aSArtem Bityutskiy 
4431e51764aSArtem Bityutskiy 	remove_buds(c);
4441e51764aSArtem Bityutskiy 
4451e51764aSArtem Bityutskiy 	/*
4461e51764aSArtem Bityutskiy 	 * We have started the commit and now users may use the rest of the log
4471e51764aSArtem Bityutskiy 	 * for new writes.
4481e51764aSArtem Bityutskiy 	 */
4491e51764aSArtem Bityutskiy 	c->min_log_bytes = 0;
4501e51764aSArtem Bityutskiy 
4511e51764aSArtem Bityutskiy out:
4521e51764aSArtem Bityutskiy 	kfree(buf);
4531e51764aSArtem Bityutskiy 	return err;
4541e51764aSArtem Bityutskiy }
4551e51764aSArtem Bityutskiy 
4561e51764aSArtem Bityutskiy /**
4571e51764aSArtem Bityutskiy  * ubifs_log_end_commit - end commit.
4581e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
4591e51764aSArtem Bityutskiy  * @ltail_lnum: new log tail LEB number
4601e51764aSArtem Bityutskiy  *
4611e51764aSArtem Bityutskiy  * This function is called on when the commit operation was finished. It
462052c2807SArtem Bityutskiy  * moves log tail to new position and updates the master node so that it stores
463052c2807SArtem Bityutskiy  * the new log tail LEB number. Returns zero in case of success and a negative
464052c2807SArtem Bityutskiy  * error code in case of failure.
4651e51764aSArtem Bityutskiy  */
ubifs_log_end_commit(struct ubifs_info * c,int ltail_lnum)4661e51764aSArtem Bityutskiy int ubifs_log_end_commit(struct ubifs_info *c, int ltail_lnum)
4671e51764aSArtem Bityutskiy {
4681e51764aSArtem Bityutskiy 	int err;
4691e51764aSArtem Bityutskiy 
4701e51764aSArtem Bityutskiy 	/*
4711e51764aSArtem Bityutskiy 	 * At this phase we have to lock 'c->log_mutex' because UBIFS allows FS
4721e51764aSArtem Bityutskiy 	 * writes during commit. Its only short "commit" start phase when
4731e51764aSArtem Bityutskiy 	 * writers are blocked.
4741e51764aSArtem Bityutskiy 	 */
4751e51764aSArtem Bityutskiy 	mutex_lock(&c->log_mutex);
4761e51764aSArtem Bityutskiy 
4771e51764aSArtem Bityutskiy 	dbg_log("old tail was LEB %d:0, new tail is LEB %d:0",
4781e51764aSArtem Bityutskiy 		c->ltail_lnum, ltail_lnum);
4791e51764aSArtem Bityutskiy 
4801e51764aSArtem Bityutskiy 	c->ltail_lnum = ltail_lnum;
4811e51764aSArtem Bityutskiy 	/*
4821e51764aSArtem Bityutskiy 	 * The commit is finished and from now on it must be guaranteed that
4831e51764aSArtem Bityutskiy 	 * there is always enough space for the next commit.
4841e51764aSArtem Bityutskiy 	 */
4851e51764aSArtem Bityutskiy 	c->min_log_bytes = c->leb_size;
4861e51764aSArtem Bityutskiy 
4871e51764aSArtem Bityutskiy 	spin_lock(&c->buds_lock);
4881e51764aSArtem Bityutskiy 	c->bud_bytes -= c->cmt_bud_bytes;
4891e51764aSArtem Bityutskiy 	spin_unlock(&c->buds_lock);
4901e51764aSArtem Bityutskiy 
4911e51764aSArtem Bityutskiy 	err = dbg_check_bud_bytes(c);
492052c2807SArtem Bityutskiy 	if (err)
493052c2807SArtem Bityutskiy 		goto out;
4941e51764aSArtem Bityutskiy 
495052c2807SArtem Bityutskiy 	err = ubifs_write_master(c);
496052c2807SArtem Bityutskiy 
497052c2807SArtem Bityutskiy out:
4981e51764aSArtem Bityutskiy 	mutex_unlock(&c->log_mutex);
4991e51764aSArtem Bityutskiy 	return err;
5001e51764aSArtem Bityutskiy }
5011e51764aSArtem Bityutskiy 
5021e51764aSArtem Bityutskiy /**
5031e51764aSArtem Bityutskiy  * ubifs_log_post_commit - things to do after commit is completed.
5041e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
5051e51764aSArtem Bityutskiy  * @old_ltail_lnum: old log tail LEB number
5061e51764aSArtem Bityutskiy  *
5071e51764aSArtem Bityutskiy  * Release buds only after commit is completed, because they must be unchanged
5081e51764aSArtem Bityutskiy  * if recovery is needed.
5091e51764aSArtem Bityutskiy  *
5101e51764aSArtem Bityutskiy  * Unmap log LEBs only after commit is completed, because they may be needed for
5111e51764aSArtem Bityutskiy  * recovery.
5121e51764aSArtem Bityutskiy  *
5131e51764aSArtem Bityutskiy  * This function returns %0 on success and a negative error code on failure.
5141e51764aSArtem Bityutskiy  */
ubifs_log_post_commit(struct ubifs_info * c,int old_ltail_lnum)5151e51764aSArtem Bityutskiy int ubifs_log_post_commit(struct ubifs_info *c, int old_ltail_lnum)
5161e51764aSArtem Bityutskiy {
5171e51764aSArtem Bityutskiy 	int lnum, err = 0;
5181e51764aSArtem Bityutskiy 
5191e51764aSArtem Bityutskiy 	while (!list_empty(&c->old_buds)) {
5201e51764aSArtem Bityutskiy 		struct ubifs_bud *bud;
5211e51764aSArtem Bityutskiy 
5221e51764aSArtem Bityutskiy 		bud = list_entry(c->old_buds.next, struct ubifs_bud, list);
5231e51764aSArtem Bityutskiy 		err = ubifs_return_leb(c, bud->lnum);
5241e51764aSArtem Bityutskiy 		if (err)
5251e51764aSArtem Bityutskiy 			return err;
5261e51764aSArtem Bityutskiy 		list_del(&bud->list);
5276a98bc46SSascha Hauer 		kfree(bud->log_hash);
5281e51764aSArtem Bityutskiy 		kfree(bud);
5291e51764aSArtem Bityutskiy 	}
5301e51764aSArtem Bityutskiy 	mutex_lock(&c->log_mutex);
5311e51764aSArtem Bityutskiy 	for (lnum = old_ltail_lnum; lnum != c->ltail_lnum;
532e11602eaSArtem Bityutskiy 	     lnum = ubifs_next_log_lnum(c, lnum)) {
5331e51764aSArtem Bityutskiy 		dbg_log("unmap log LEB %d", lnum);
5341e51764aSArtem Bityutskiy 		err = ubifs_leb_unmap(c, lnum);
5351e51764aSArtem Bityutskiy 		if (err)
5361e51764aSArtem Bityutskiy 			goto out;
5371e51764aSArtem Bityutskiy 	}
5381e51764aSArtem Bityutskiy out:
5391e51764aSArtem Bityutskiy 	mutex_unlock(&c->log_mutex);
5401e51764aSArtem Bityutskiy 	return err;
5411e51764aSArtem Bityutskiy }
5421e51764aSArtem Bityutskiy 
5431e51764aSArtem Bityutskiy /**
5441e51764aSArtem Bityutskiy  * struct done_ref - references that have been done.
5451e51764aSArtem Bityutskiy  * @rb: rb-tree node
5461e51764aSArtem Bityutskiy  * @lnum: LEB number
5471e51764aSArtem Bityutskiy  */
5481e51764aSArtem Bityutskiy struct done_ref {
5491e51764aSArtem Bityutskiy 	struct rb_node rb;
5501e51764aSArtem Bityutskiy 	int lnum;
5511e51764aSArtem Bityutskiy };
5521e51764aSArtem Bityutskiy 
5531e51764aSArtem Bityutskiy /**
5541e51764aSArtem Bityutskiy  * done_already - determine if a reference has been done already.
5551e51764aSArtem Bityutskiy  * @done_tree: rb-tree to store references that have been done
5561e51764aSArtem Bityutskiy  * @lnum: LEB number of reference
5571e51764aSArtem Bityutskiy  *
5581e51764aSArtem Bityutskiy  * This function returns %1 if the reference has been done, %0 if not, otherwise
5591e51764aSArtem Bityutskiy  * a negative error code is returned.
5601e51764aSArtem Bityutskiy  */
done_already(struct rb_root * done_tree,int lnum)5611e51764aSArtem Bityutskiy static int done_already(struct rb_root *done_tree, int lnum)
5621e51764aSArtem Bityutskiy {
5631e51764aSArtem Bityutskiy 	struct rb_node **p = &done_tree->rb_node, *parent = NULL;
5641e51764aSArtem Bityutskiy 	struct done_ref *dr;
5651e51764aSArtem Bityutskiy 
5661e51764aSArtem Bityutskiy 	while (*p) {
5671e51764aSArtem Bityutskiy 		parent = *p;
5681e51764aSArtem Bityutskiy 		dr = rb_entry(parent, struct done_ref, rb);
5691e51764aSArtem Bityutskiy 		if (lnum < dr->lnum)
5701e51764aSArtem Bityutskiy 			p = &(*p)->rb_left;
5711e51764aSArtem Bityutskiy 		else if (lnum > dr->lnum)
5721e51764aSArtem Bityutskiy 			p = &(*p)->rb_right;
5731e51764aSArtem Bityutskiy 		else
5741e51764aSArtem Bityutskiy 			return 1;
5751e51764aSArtem Bityutskiy 	}
5761e51764aSArtem Bityutskiy 
5771e51764aSArtem Bityutskiy 	dr = kzalloc(sizeof(struct done_ref), GFP_NOFS);
5781e51764aSArtem Bityutskiy 	if (!dr)
5791e51764aSArtem Bityutskiy 		return -ENOMEM;
5801e51764aSArtem Bityutskiy 
5811e51764aSArtem Bityutskiy 	dr->lnum = lnum;
5821e51764aSArtem Bityutskiy 
5831e51764aSArtem Bityutskiy 	rb_link_node(&dr->rb, parent, p);
5841e51764aSArtem Bityutskiy 	rb_insert_color(&dr->rb, done_tree);
5851e51764aSArtem Bityutskiy 
5861e51764aSArtem Bityutskiy 	return 0;
5871e51764aSArtem Bityutskiy }
5881e51764aSArtem Bityutskiy 
5891e51764aSArtem Bityutskiy /**
5901e51764aSArtem Bityutskiy  * destroy_done_tree - destroy the done tree.
5911e51764aSArtem Bityutskiy  * @done_tree: done tree to destroy
5921e51764aSArtem Bityutskiy  */
destroy_done_tree(struct rb_root * done_tree)5931e51764aSArtem Bityutskiy static void destroy_done_tree(struct rb_root *done_tree)
5941e51764aSArtem Bityutskiy {
595bb25e49fSCody P Schafer 	struct done_ref *dr, *n;
5961e51764aSArtem Bityutskiy 
597bb25e49fSCody P Schafer 	rbtree_postorder_for_each_entry_safe(dr, n, done_tree, rb)
5981e51764aSArtem Bityutskiy 		kfree(dr);
5991e51764aSArtem Bityutskiy }
6001e51764aSArtem Bityutskiy 
6011e51764aSArtem Bityutskiy /**
6021e51764aSArtem Bityutskiy  * add_node - add a node to the consolidated log.
6031e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
6041e51764aSArtem Bityutskiy  * @buf: buffer to which to add
6051e51764aSArtem Bityutskiy  * @lnum: LEB number to which to write is passed and returned here
6061e51764aSArtem Bityutskiy  * @offs: offset to where to write is passed and returned here
6071e51764aSArtem Bityutskiy  * @node: node to add
6081e51764aSArtem Bityutskiy  *
6091e51764aSArtem Bityutskiy  * This function returns %0 on success and a negative error code on failure.
6101e51764aSArtem Bityutskiy  */
add_node(struct ubifs_info * c,void * buf,int * lnum,int * offs,void * node)6111e51764aSArtem Bityutskiy static int add_node(struct ubifs_info *c, void *buf, int *lnum, int *offs,
6121e51764aSArtem Bityutskiy 		    void *node)
6131e51764aSArtem Bityutskiy {
6141e51764aSArtem Bityutskiy 	struct ubifs_ch *ch = node;
6151e51764aSArtem Bityutskiy 	int len = le32_to_cpu(ch->len), remains = c->leb_size - *offs;
6161e51764aSArtem Bityutskiy 
6171e51764aSArtem Bityutskiy 	if (len > remains) {
6181e51764aSArtem Bityutskiy 		int sz = ALIGN(*offs, c->min_io_size), err;
6191e51764aSArtem Bityutskiy 
6201e51764aSArtem Bityutskiy 		ubifs_pad(c, buf + *offs, sz - *offs);
621b36a261eSRichard Weinberger 		err = ubifs_leb_change(c, *lnum, buf, sz);
6221e51764aSArtem Bityutskiy 		if (err)
6231e51764aSArtem Bityutskiy 			return err;
624e11602eaSArtem Bityutskiy 		*lnum = ubifs_next_log_lnum(c, *lnum);
6251e51764aSArtem Bityutskiy 		*offs = 0;
6261e51764aSArtem Bityutskiy 	}
6271e51764aSArtem Bityutskiy 	memcpy(buf + *offs, node, len);
6281e51764aSArtem Bityutskiy 	*offs += ALIGN(len, 8);
6291e51764aSArtem Bityutskiy 	return 0;
6301e51764aSArtem Bityutskiy }
6311e51764aSArtem Bityutskiy 
6321e51764aSArtem Bityutskiy /**
6331e51764aSArtem Bityutskiy  * ubifs_consolidate_log - consolidate the log.
6341e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
6351e51764aSArtem Bityutskiy  *
6361e51764aSArtem Bityutskiy  * Repeated failed commits could cause the log to be full, but at least 1 LEB is
6371e51764aSArtem Bityutskiy  * needed for commit. This function rewrites the reference nodes in the log
6381e51764aSArtem Bityutskiy  * omitting duplicates, and failed CS nodes, and leaving no gaps.
6391e51764aSArtem Bityutskiy  *
6401e51764aSArtem Bityutskiy  * This function returns %0 on success and a negative error code on failure.
6411e51764aSArtem Bityutskiy  */
ubifs_consolidate_log(struct ubifs_info * c)6421e51764aSArtem Bityutskiy int ubifs_consolidate_log(struct ubifs_info *c)
6431e51764aSArtem Bityutskiy {
6441e51764aSArtem Bityutskiy 	struct ubifs_scan_leb *sleb;
6451e51764aSArtem Bityutskiy 	struct ubifs_scan_node *snod;
6461e51764aSArtem Bityutskiy 	struct rb_root done_tree = RB_ROOT;
6471e51764aSArtem Bityutskiy 	int lnum, err, first = 1, write_lnum, offs = 0;
6481e51764aSArtem Bityutskiy 	void *buf;
6491e51764aSArtem Bityutskiy 
6501e51764aSArtem Bityutskiy 	dbg_rcvry("log tail LEB %d, log head LEB %d", c->ltail_lnum,
6511e51764aSArtem Bityutskiy 		  c->lhead_lnum);
6521e51764aSArtem Bityutskiy 	buf = vmalloc(c->leb_size);
6531e51764aSArtem Bityutskiy 	if (!buf)
6541e51764aSArtem Bityutskiy 		return -ENOMEM;
6551e51764aSArtem Bityutskiy 	lnum = c->ltail_lnum;
6561e51764aSArtem Bityutskiy 	write_lnum = lnum;
6571e51764aSArtem Bityutskiy 	while (1) {
658348709baSArtem Bityutskiy 		sleb = ubifs_scan(c, lnum, 0, c->sbuf, 0);
6591e51764aSArtem Bityutskiy 		if (IS_ERR(sleb)) {
6601e51764aSArtem Bityutskiy 			err = PTR_ERR(sleb);
6611e51764aSArtem Bityutskiy 			goto out_free;
6621e51764aSArtem Bityutskiy 		}
6631e51764aSArtem Bityutskiy 		list_for_each_entry(snod, &sleb->nodes, list) {
6641e51764aSArtem Bityutskiy 			switch (snod->type) {
6651e51764aSArtem Bityutskiy 			case UBIFS_REF_NODE: {
6661e51764aSArtem Bityutskiy 				struct ubifs_ref_node *ref = snod->node;
6671e51764aSArtem Bityutskiy 				int ref_lnum = le32_to_cpu(ref->lnum);
6681e51764aSArtem Bityutskiy 
6691e51764aSArtem Bityutskiy 				err = done_already(&done_tree, ref_lnum);
6701e51764aSArtem Bityutskiy 				if (err < 0)
6711e51764aSArtem Bityutskiy 					goto out_scan;
6721e51764aSArtem Bityutskiy 				if (err != 1) {
6731e51764aSArtem Bityutskiy 					err = add_node(c, buf, &write_lnum,
6741e51764aSArtem Bityutskiy 						       &offs, snod->node);
6751e51764aSArtem Bityutskiy 					if (err)
6761e51764aSArtem Bityutskiy 						goto out_scan;
6771e51764aSArtem Bityutskiy 				}
6781e51764aSArtem Bityutskiy 				break;
6791e51764aSArtem Bityutskiy 			}
6801e51764aSArtem Bityutskiy 			case UBIFS_CS_NODE:
6811e51764aSArtem Bityutskiy 				if (!first)
6821e51764aSArtem Bityutskiy 					break;
6831e51764aSArtem Bityutskiy 				err = add_node(c, buf, &write_lnum, &offs,
6841e51764aSArtem Bityutskiy 					       snod->node);
6851e51764aSArtem Bityutskiy 				if (err)
6861e51764aSArtem Bityutskiy 					goto out_scan;
6871e51764aSArtem Bityutskiy 				first = 0;
6881e51764aSArtem Bityutskiy 				break;
6891e51764aSArtem Bityutskiy 			}
6901e51764aSArtem Bityutskiy 		}
6911e51764aSArtem Bityutskiy 		ubifs_scan_destroy(sleb);
6921e51764aSArtem Bityutskiy 		if (lnum == c->lhead_lnum)
6931e51764aSArtem Bityutskiy 			break;
694e11602eaSArtem Bityutskiy 		lnum = ubifs_next_log_lnum(c, lnum);
6951e51764aSArtem Bityutskiy 	}
6961e51764aSArtem Bityutskiy 	if (offs) {
6971e51764aSArtem Bityutskiy 		int sz = ALIGN(offs, c->min_io_size);
6981e51764aSArtem Bityutskiy 
6991e51764aSArtem Bityutskiy 		ubifs_pad(c, buf + offs, sz - offs);
700b36a261eSRichard Weinberger 		err = ubifs_leb_change(c, write_lnum, buf, sz);
7011e51764aSArtem Bityutskiy 		if (err)
7021e51764aSArtem Bityutskiy 			goto out_free;
7031e51764aSArtem Bityutskiy 		offs = ALIGN(offs, c->min_io_size);
7041e51764aSArtem Bityutskiy 	}
7051e51764aSArtem Bityutskiy 	destroy_done_tree(&done_tree);
7061e51764aSArtem Bityutskiy 	vfree(buf);
7071e51764aSArtem Bityutskiy 	if (write_lnum == c->lhead_lnum) {
708235c362bSSheng Yong 		ubifs_err(c, "log is too full");
7091e51764aSArtem Bityutskiy 		return -EINVAL;
7101e51764aSArtem Bityutskiy 	}
7111e51764aSArtem Bityutskiy 	/* Unmap remaining LEBs */
7121e51764aSArtem Bityutskiy 	lnum = write_lnum;
7131e51764aSArtem Bityutskiy 	do {
714e11602eaSArtem Bityutskiy 		lnum = ubifs_next_log_lnum(c, lnum);
7151e51764aSArtem Bityutskiy 		err = ubifs_leb_unmap(c, lnum);
7161e51764aSArtem Bityutskiy 		if (err)
7171e51764aSArtem Bityutskiy 			return err;
7181e51764aSArtem Bityutskiy 	} while (lnum != c->lhead_lnum);
7191e51764aSArtem Bityutskiy 	c->lhead_lnum = write_lnum;
7201e51764aSArtem Bityutskiy 	c->lhead_offs = offs;
7211e51764aSArtem Bityutskiy 	dbg_rcvry("new log head at %d:%d", c->lhead_lnum, c->lhead_offs);
7221e51764aSArtem Bityutskiy 	return 0;
7231e51764aSArtem Bityutskiy 
7241e51764aSArtem Bityutskiy out_scan:
7251e51764aSArtem Bityutskiy 	ubifs_scan_destroy(sleb);
7261e51764aSArtem Bityutskiy out_free:
7271e51764aSArtem Bityutskiy 	destroy_done_tree(&done_tree);
7281e51764aSArtem Bityutskiy 	vfree(buf);
7291e51764aSArtem Bityutskiy 	return err;
7301e51764aSArtem Bityutskiy }
7311e51764aSArtem Bityutskiy 
7321e51764aSArtem Bityutskiy /**
7331e51764aSArtem Bityutskiy  * dbg_check_bud_bytes - make sure bud bytes calculation are all right.
7341e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
7351e51764aSArtem Bityutskiy  *
7361e51764aSArtem Bityutskiy  * This function makes sure the amount of flash space used by closed buds
7371e51764aSArtem Bityutskiy  * ('c->bud_bytes' is correct). Returns zero in case of success and %-EINVAL in
7381e51764aSArtem Bityutskiy  * case of failure.
7391e51764aSArtem Bityutskiy  */
dbg_check_bud_bytes(struct ubifs_info * c)7401e51764aSArtem Bityutskiy static int dbg_check_bud_bytes(struct ubifs_info *c)
7411e51764aSArtem Bityutskiy {
7421e51764aSArtem Bityutskiy 	int i, err = 0;
7431e51764aSArtem Bityutskiy 	struct ubifs_bud *bud;
7441e51764aSArtem Bityutskiy 	long long bud_bytes = 0;
7451e51764aSArtem Bityutskiy 
7462b1844a8SArtem Bityutskiy 	if (!dbg_is_chk_gen(c))
7471e51764aSArtem Bityutskiy 		return 0;
7481e51764aSArtem Bityutskiy 
7491e51764aSArtem Bityutskiy 	spin_lock(&c->buds_lock);
7501e51764aSArtem Bityutskiy 	for (i = 0; i < c->jhead_cnt; i++)
7511e51764aSArtem Bityutskiy 		list_for_each_entry(bud, &c->jheads[i].buds_list, list)
7521e51764aSArtem Bityutskiy 			bud_bytes += c->leb_size - bud->start;
7531e51764aSArtem Bityutskiy 
7541e51764aSArtem Bityutskiy 	if (c->bud_bytes != bud_bytes) {
755235c362bSSheng Yong 		ubifs_err(c, "bad bud_bytes %lld, calculated %lld",
7561e51764aSArtem Bityutskiy 			  c->bud_bytes, bud_bytes);
7571e51764aSArtem Bityutskiy 		err = -EINVAL;
7581e51764aSArtem Bityutskiy 	}
7591e51764aSArtem Bityutskiy 	spin_unlock(&c->buds_lock);
7601e51764aSArtem Bityutskiy 
7611e51764aSArtem Bityutskiy 	return err;
7621e51764aSArtem Bityutskiy }
763