xref: /linux/fs/ubifs/replay.c (revision 235c362bd0f6afcf767bc72aa0c647e1434cc631)
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 /*
241e51764aSArtem Bityutskiy  * This file contains journal replay code. It runs when the file-system is being
251e51764aSArtem Bityutskiy  * mounted and requires no locking.
261e51764aSArtem Bityutskiy  *
271e51764aSArtem Bityutskiy  * The larger is the journal, the longer it takes to scan it, so the longer it
281e51764aSArtem Bityutskiy  * takes to mount UBIFS. This is why the journal has limited size which may be
291e51764aSArtem Bityutskiy  * changed depending on the system requirements. But a larger journal gives
301e51764aSArtem Bityutskiy  * faster I/O speed because it writes the index less frequently. So this is a
311e51764aSArtem Bityutskiy  * trade-off. Also, the journal is indexed by the in-memory index (TNC), so the
321e51764aSArtem Bityutskiy  * larger is the journal, the more memory its index may consume.
331e51764aSArtem Bityutskiy  */
341e51764aSArtem Bityutskiy 
351e51764aSArtem Bityutskiy #include "ubifs.h"
36debf12d5SArtem Bityutskiy #include <linux/list_sort.h>
371e51764aSArtem Bityutskiy 
381e51764aSArtem Bityutskiy /**
39debf12d5SArtem Bityutskiy  * struct replay_entry - replay list entry.
401e51764aSArtem Bityutskiy  * @lnum: logical eraseblock number of the node
411e51764aSArtem Bityutskiy  * @offs: node offset
421e51764aSArtem Bityutskiy  * @len: node length
43074bcb9bSArtem Bityutskiy  * @deletion: non-zero if this entry corresponds to a node deletion
441e51764aSArtem Bityutskiy  * @sqnum: node sequence number
45debf12d5SArtem Bityutskiy  * @list: links the replay list
461e51764aSArtem Bityutskiy  * @key: node key
471e51764aSArtem Bityutskiy  * @nm: directory entry name
481e51764aSArtem Bityutskiy  * @old_size: truncation old size
491e51764aSArtem Bityutskiy  * @new_size: truncation new size
501e51764aSArtem Bityutskiy  *
51debf12d5SArtem Bityutskiy  * The replay process first scans all buds and builds the replay list, then
52debf12d5SArtem Bityutskiy  * sorts the replay list in nodes sequence number order, and then inserts all
53debf12d5SArtem Bityutskiy  * the replay entries to the TNC.
541e51764aSArtem Bityutskiy  */
551e51764aSArtem Bityutskiy struct replay_entry {
561e51764aSArtem Bityutskiy 	int lnum;
571e51764aSArtem Bityutskiy 	int offs;
581e51764aSArtem Bityutskiy 	int len;
59074bcb9bSArtem Bityutskiy 	unsigned int deletion:1;
601e51764aSArtem Bityutskiy 	unsigned long long sqnum;
61debf12d5SArtem Bityutskiy 	struct list_head list;
621e51764aSArtem Bityutskiy 	union ubifs_key key;
631e51764aSArtem Bityutskiy 	union {
641e51764aSArtem Bityutskiy 		struct qstr nm;
651e51764aSArtem Bityutskiy 		struct {
661e51764aSArtem Bityutskiy 			loff_t old_size;
671e51764aSArtem Bityutskiy 			loff_t new_size;
681e51764aSArtem Bityutskiy 		};
691e51764aSArtem Bityutskiy 	};
701e51764aSArtem Bityutskiy };
711e51764aSArtem Bityutskiy 
721e51764aSArtem Bityutskiy /**
731e51764aSArtem Bityutskiy  * struct bud_entry - entry in the list of buds to replay.
741e51764aSArtem Bityutskiy  * @list: next bud in the list
751e51764aSArtem Bityutskiy  * @bud: bud description object
761e51764aSArtem Bityutskiy  * @sqnum: reference node sequence number
77af1dd412SArtem Bityutskiy  * @free: free bytes in the bud
78af1dd412SArtem Bityutskiy  * @dirty: dirty bytes in the bud
791e51764aSArtem Bityutskiy  */
801e51764aSArtem Bityutskiy struct bud_entry {
811e51764aSArtem Bityutskiy 	struct list_head list;
821e51764aSArtem Bityutskiy 	struct ubifs_bud *bud;
831e51764aSArtem Bityutskiy 	unsigned long long sqnum;
84af1dd412SArtem Bityutskiy 	int free;
85af1dd412SArtem Bityutskiy 	int dirty;
861e51764aSArtem Bityutskiy };
871e51764aSArtem Bityutskiy 
881e51764aSArtem Bityutskiy /**
891e51764aSArtem Bityutskiy  * set_bud_lprops - set free and dirty space used by a bud.
901e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
91074bcb9bSArtem Bityutskiy  * @b: bud entry which describes the bud
92074bcb9bSArtem Bityutskiy  *
93074bcb9bSArtem Bityutskiy  * This function makes sure the LEB properties of bud @b are set correctly
94074bcb9bSArtem Bityutskiy  * after the replay. Returns zero in case of success and a negative error code
95074bcb9bSArtem Bityutskiy  * in case of failure.
961e51764aSArtem Bityutskiy  */
97074bcb9bSArtem Bityutskiy static int set_bud_lprops(struct ubifs_info *c, struct bud_entry *b)
981e51764aSArtem Bityutskiy {
991e51764aSArtem Bityutskiy 	const struct ubifs_lprops *lp;
1001e51764aSArtem Bityutskiy 	int err = 0, dirty;
1011e51764aSArtem Bityutskiy 
1021e51764aSArtem Bityutskiy 	ubifs_get_lprops(c);
1031e51764aSArtem Bityutskiy 
104074bcb9bSArtem Bityutskiy 	lp = ubifs_lpt_lookup_dirty(c, b->bud->lnum);
1051e51764aSArtem Bityutskiy 	if (IS_ERR(lp)) {
1061e51764aSArtem Bityutskiy 		err = PTR_ERR(lp);
1071e51764aSArtem Bityutskiy 		goto out;
1081e51764aSArtem Bityutskiy 	}
1091e51764aSArtem Bityutskiy 
1101e51764aSArtem Bityutskiy 	dirty = lp->dirty;
111074bcb9bSArtem Bityutskiy 	if (b->bud->start == 0 && (lp->free != c->leb_size || lp->dirty != 0)) {
1121e51764aSArtem Bityutskiy 		/*
1131e51764aSArtem Bityutskiy 		 * The LEB was added to the journal with a starting offset of
1141e51764aSArtem Bityutskiy 		 * zero which means the LEB must have been empty. The LEB
115074bcb9bSArtem Bityutskiy 		 * property values should be @lp->free == @c->leb_size and
116074bcb9bSArtem Bityutskiy 		 * @lp->dirty == 0, but that is not the case. The reason is that
1177a9c3e39SArtem Bityutskiy 		 * the LEB had been garbage collected before it became the bud,
1187a9c3e39SArtem Bityutskiy 		 * and there was not commit inbetween. The garbage collector
1197a9c3e39SArtem Bityutskiy 		 * resets the free and dirty space without recording it
1207a9c3e39SArtem Bityutskiy 		 * anywhere except lprops, so if there was no commit then
1217a9c3e39SArtem Bityutskiy 		 * lprops does not have that information.
1221e51764aSArtem Bityutskiy 		 *
1231e51764aSArtem Bityutskiy 		 * We do not need to adjust free space because the scan has told
1241e51764aSArtem Bityutskiy 		 * us the exact value which is recorded in the replay entry as
125074bcb9bSArtem Bityutskiy 		 * @b->free.
1261e51764aSArtem Bityutskiy 		 *
1271e51764aSArtem Bityutskiy 		 * However we do need to subtract from the dirty space the
1281e51764aSArtem Bityutskiy 		 * amount of space that the garbage collector reclaimed, which
1291e51764aSArtem Bityutskiy 		 * is the whole LEB minus the amount of space that was free.
1301e51764aSArtem Bityutskiy 		 */
131074bcb9bSArtem Bityutskiy 		dbg_mnt("bud LEB %d was GC'd (%d free, %d dirty)", b->bud->lnum,
1321e51764aSArtem Bityutskiy 			lp->free, lp->dirty);
133074bcb9bSArtem Bityutskiy 		dbg_gc("bud LEB %d was GC'd (%d free, %d dirty)", b->bud->lnum,
1341e51764aSArtem Bityutskiy 			lp->free, lp->dirty);
1351e51764aSArtem Bityutskiy 		dirty -= c->leb_size - lp->free;
1361e51764aSArtem Bityutskiy 		/*
1371e51764aSArtem Bityutskiy 		 * If the replay order was perfect the dirty space would now be
1387d4e9ccbSArtem Bityutskiy 		 * zero. The order is not perfect because the journal heads
1391e51764aSArtem Bityutskiy 		 * race with each other. This is not a problem but is does mean
1401e51764aSArtem Bityutskiy 		 * that the dirty space may temporarily exceed c->leb_size
1411e51764aSArtem Bityutskiy 		 * during the replay.
1421e51764aSArtem Bityutskiy 		 */
1431e51764aSArtem Bityutskiy 		if (dirty != 0)
1443668b70fSArtem Bityutskiy 			dbg_mnt("LEB %d lp: %d free %d dirty replay: %d free %d dirty",
14579fda517SArtem Bityutskiy 				b->bud->lnum, lp->free, lp->dirty, b->free,
14679fda517SArtem Bityutskiy 				b->dirty);
1471e51764aSArtem Bityutskiy 	}
148074bcb9bSArtem Bityutskiy 	lp = ubifs_change_lp(c, lp, b->free, dirty + b->dirty,
1491e51764aSArtem Bityutskiy 			     lp->flags | LPROPS_TAKEN, 0);
1501e51764aSArtem Bityutskiy 	if (IS_ERR(lp)) {
1511e51764aSArtem Bityutskiy 		err = PTR_ERR(lp);
1521e51764aSArtem Bityutskiy 		goto out;
1531e51764aSArtem Bityutskiy 	}
15452c6e6f9SArtem Bityutskiy 
15552c6e6f9SArtem Bityutskiy 	/* Make sure the journal head points to the latest bud */
156074bcb9bSArtem Bityutskiy 	err = ubifs_wbuf_seek_nolock(&c->jheads[b->bud->jhead].wbuf,
157b36a261eSRichard Weinberger 				     b->bud->lnum, c->leb_size - b->free);
15852c6e6f9SArtem Bityutskiy 
1591e51764aSArtem Bityutskiy out:
1601e51764aSArtem Bityutskiy 	ubifs_release_lprops(c);
1611e51764aSArtem Bityutskiy 	return err;
1621e51764aSArtem Bityutskiy }
1631e51764aSArtem Bityutskiy 
1641e51764aSArtem Bityutskiy /**
165074bcb9bSArtem Bityutskiy  * set_buds_lprops - set free and dirty space for all replayed buds.
166074bcb9bSArtem Bityutskiy  * @c: UBIFS file-system description object
167074bcb9bSArtem Bityutskiy  *
168074bcb9bSArtem Bityutskiy  * This function sets LEB properties for all replayed buds. Returns zero in
169074bcb9bSArtem Bityutskiy  * case of success and a negative error code in case of failure.
170074bcb9bSArtem Bityutskiy  */
171074bcb9bSArtem Bityutskiy static int set_buds_lprops(struct ubifs_info *c)
172074bcb9bSArtem Bityutskiy {
173074bcb9bSArtem Bityutskiy 	struct bud_entry *b;
174074bcb9bSArtem Bityutskiy 	int err;
175074bcb9bSArtem Bityutskiy 
176074bcb9bSArtem Bityutskiy 	list_for_each_entry(b, &c->replay_buds, list) {
177074bcb9bSArtem Bityutskiy 		err = set_bud_lprops(c, b);
178074bcb9bSArtem Bityutskiy 		if (err)
179074bcb9bSArtem Bityutskiy 			return err;
180074bcb9bSArtem Bityutskiy 	}
181074bcb9bSArtem Bityutskiy 
182074bcb9bSArtem Bityutskiy 	return 0;
183074bcb9bSArtem Bityutskiy }
184074bcb9bSArtem Bityutskiy 
185074bcb9bSArtem Bityutskiy /**
1861e51764aSArtem Bityutskiy  * trun_remove_range - apply a replay entry for a truncation to the TNC.
1871e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
1881e51764aSArtem Bityutskiy  * @r: replay entry of truncation
1891e51764aSArtem Bityutskiy  */
1901e51764aSArtem Bityutskiy static int trun_remove_range(struct ubifs_info *c, struct replay_entry *r)
1911e51764aSArtem Bityutskiy {
1921e51764aSArtem Bityutskiy 	unsigned min_blk, max_blk;
1931e51764aSArtem Bityutskiy 	union ubifs_key min_key, max_key;
1941e51764aSArtem Bityutskiy 	ino_t ino;
1951e51764aSArtem Bityutskiy 
1961e51764aSArtem Bityutskiy 	min_blk = r->new_size / UBIFS_BLOCK_SIZE;
1971e51764aSArtem Bityutskiy 	if (r->new_size & (UBIFS_BLOCK_SIZE - 1))
1981e51764aSArtem Bityutskiy 		min_blk += 1;
1991e51764aSArtem Bityutskiy 
2001e51764aSArtem Bityutskiy 	max_blk = r->old_size / UBIFS_BLOCK_SIZE;
2011e51764aSArtem Bityutskiy 	if ((r->old_size & (UBIFS_BLOCK_SIZE - 1)) == 0)
2021e51764aSArtem Bityutskiy 		max_blk -= 1;
2031e51764aSArtem Bityutskiy 
2041e51764aSArtem Bityutskiy 	ino = key_inum(c, &r->key);
2051e51764aSArtem Bityutskiy 
2061e51764aSArtem Bityutskiy 	data_key_init(c, &min_key, ino, min_blk);
2071e51764aSArtem Bityutskiy 	data_key_init(c, &max_key, ino, max_blk);
2081e51764aSArtem Bityutskiy 
2091e51764aSArtem Bityutskiy 	return ubifs_tnc_remove_range(c, &min_key, &max_key);
2101e51764aSArtem Bityutskiy }
2111e51764aSArtem Bityutskiy 
2121e51764aSArtem Bityutskiy /**
2131e51764aSArtem Bityutskiy  * apply_replay_entry - apply a replay entry to the TNC.
2141e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
2151e51764aSArtem Bityutskiy  * @r: replay entry to apply
2161e51764aSArtem Bityutskiy  *
2171e51764aSArtem Bityutskiy  * Apply a replay entry to the TNC.
2181e51764aSArtem Bityutskiy  */
2191e51764aSArtem Bityutskiy static int apply_replay_entry(struct ubifs_info *c, struct replay_entry *r)
2201e51764aSArtem Bityutskiy {
221074bcb9bSArtem Bityutskiy 	int err;
2221e51764aSArtem Bityutskiy 
223515315a1SArtem Bityutskiy 	dbg_mntk(&r->key, "LEB %d:%d len %d deletion %d sqnum %llu key ",
224515315a1SArtem Bityutskiy 		 r->lnum, r->offs, r->len, r->deletion, r->sqnum);
2251e51764aSArtem Bityutskiy 
2261e51764aSArtem Bityutskiy 	/* Set c->replay_sqnum to help deal with dangling branches. */
2271e51764aSArtem Bityutskiy 	c->replay_sqnum = r->sqnum;
2281e51764aSArtem Bityutskiy 
229074bcb9bSArtem Bityutskiy 	if (is_hash_key(c, &r->key)) {
230074bcb9bSArtem Bityutskiy 		if (r->deletion)
2311e51764aSArtem Bityutskiy 			err = ubifs_tnc_remove_nm(c, &r->key, &r->nm);
2321e51764aSArtem Bityutskiy 		else
2331e51764aSArtem Bityutskiy 			err = ubifs_tnc_add_nm(c, &r->key, r->lnum, r->offs,
2341e51764aSArtem Bityutskiy 					       r->len, &r->nm);
2351e51764aSArtem Bityutskiy 	} else {
236074bcb9bSArtem Bityutskiy 		if (r->deletion)
2371e51764aSArtem Bityutskiy 			switch (key_type(c, &r->key)) {
2381e51764aSArtem Bityutskiy 			case UBIFS_INO_KEY:
2391e51764aSArtem Bityutskiy 			{
2401e51764aSArtem Bityutskiy 				ino_t inum = key_inum(c, &r->key);
2411e51764aSArtem Bityutskiy 
2421e51764aSArtem Bityutskiy 				err = ubifs_tnc_remove_ino(c, inum);
2431e51764aSArtem Bityutskiy 				break;
2441e51764aSArtem Bityutskiy 			}
2451e51764aSArtem Bityutskiy 			case UBIFS_TRUN_KEY:
2461e51764aSArtem Bityutskiy 				err = trun_remove_range(c, r);
2471e51764aSArtem Bityutskiy 				break;
2481e51764aSArtem Bityutskiy 			default:
2491e51764aSArtem Bityutskiy 				err = ubifs_tnc_remove(c, &r->key);
2501e51764aSArtem Bityutskiy 				break;
2511e51764aSArtem Bityutskiy 			}
2521e51764aSArtem Bityutskiy 		else
2531e51764aSArtem Bityutskiy 			err = ubifs_tnc_add(c, &r->key, r->lnum, r->offs,
2541e51764aSArtem Bityutskiy 					    r->len);
2551e51764aSArtem Bityutskiy 		if (err)
2561e51764aSArtem Bityutskiy 			return err;
2571e51764aSArtem Bityutskiy 
2581e51764aSArtem Bityutskiy 		if (c->need_recovery)
259074bcb9bSArtem Bityutskiy 			err = ubifs_recover_size_accum(c, &r->key, r->deletion,
2601e51764aSArtem Bityutskiy 						       r->new_size);
2611e51764aSArtem Bityutskiy 	}
2621e51764aSArtem Bityutskiy 
2631e51764aSArtem Bityutskiy 	return err;
2641e51764aSArtem Bityutskiy }
2651e51764aSArtem Bityutskiy 
2661e51764aSArtem Bityutskiy /**
267debf12d5SArtem Bityutskiy  * replay_entries_cmp - compare 2 replay entries.
268debf12d5SArtem Bityutskiy  * @priv: UBIFS file-system description object
269debf12d5SArtem Bityutskiy  * @a: first replay entry
270debf12d5SArtem Bityutskiy  * @a: second replay entry
2711e51764aSArtem Bityutskiy  *
272debf12d5SArtem Bityutskiy  * This is a comparios function for 'list_sort()' which compares 2 replay
273debf12d5SArtem Bityutskiy  * entries @a and @b by comparing their sequence numer.  Returns %1 if @a has
274debf12d5SArtem Bityutskiy  * greater sequence number and %-1 otherwise.
2751e51764aSArtem Bityutskiy  */
276debf12d5SArtem Bityutskiy static int replay_entries_cmp(void *priv, struct list_head *a,
277debf12d5SArtem Bityutskiy 			      struct list_head *b)
2781e51764aSArtem Bityutskiy {
279debf12d5SArtem Bityutskiy 	struct replay_entry *ra, *rb;
2801e51764aSArtem Bityutskiy 
281debf12d5SArtem Bityutskiy 	cond_resched();
282debf12d5SArtem Bityutskiy 	if (a == b)
283debf12d5SArtem Bityutskiy 		return 0;
284debf12d5SArtem Bityutskiy 
285debf12d5SArtem Bityutskiy 	ra = list_entry(a, struct replay_entry, list);
286debf12d5SArtem Bityutskiy 	rb = list_entry(b, struct replay_entry, list);
287debf12d5SArtem Bityutskiy 	ubifs_assert(ra->sqnum != rb->sqnum);
288debf12d5SArtem Bityutskiy 	if (ra->sqnum > rb->sqnum)
289debf12d5SArtem Bityutskiy 		return 1;
290debf12d5SArtem Bityutskiy 	return -1;
2911e51764aSArtem Bityutskiy }
2921e51764aSArtem Bityutskiy 
2931e51764aSArtem Bityutskiy /**
294debf12d5SArtem Bityutskiy  * apply_replay_list - apply the replay list to the TNC.
2951e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
2961e51764aSArtem Bityutskiy  *
297debf12d5SArtem Bityutskiy  * Apply all entries in the replay list to the TNC. Returns zero in case of
298debf12d5SArtem Bityutskiy  * success and a negative error code in case of failure.
2991e51764aSArtem Bityutskiy  */
300debf12d5SArtem Bityutskiy static int apply_replay_list(struct ubifs_info *c)
3011e51764aSArtem Bityutskiy {
3021e51764aSArtem Bityutskiy 	struct replay_entry *r;
3031e51764aSArtem Bityutskiy 	int err;
3041e51764aSArtem Bityutskiy 
305debf12d5SArtem Bityutskiy 	list_sort(c, &c->replay_list, &replay_entries_cmp);
306debf12d5SArtem Bityutskiy 
307debf12d5SArtem Bityutskiy 	list_for_each_entry(r, &c->replay_list, list) {
3081e51764aSArtem Bityutskiy 		cond_resched();
3091e51764aSArtem Bityutskiy 
3101e51764aSArtem Bityutskiy 		err = apply_replay_entry(c, r);
3111e51764aSArtem Bityutskiy 		if (err)
3121e51764aSArtem Bityutskiy 			return err;
3131e51764aSArtem Bityutskiy 	}
314debf12d5SArtem Bityutskiy 
3151e51764aSArtem Bityutskiy 	return 0;
3161e51764aSArtem Bityutskiy }
3171e51764aSArtem Bityutskiy 
3181e51764aSArtem Bityutskiy /**
319debf12d5SArtem Bityutskiy  * destroy_replay_list - destroy the replay.
320debf12d5SArtem Bityutskiy  * @c: UBIFS file-system description object
321debf12d5SArtem Bityutskiy  *
322debf12d5SArtem Bityutskiy  * Destroy the replay list.
323debf12d5SArtem Bityutskiy  */
324debf12d5SArtem Bityutskiy static void destroy_replay_list(struct ubifs_info *c)
325debf12d5SArtem Bityutskiy {
326debf12d5SArtem Bityutskiy 	struct replay_entry *r, *tmp;
327debf12d5SArtem Bityutskiy 
328debf12d5SArtem Bityutskiy 	list_for_each_entry_safe(r, tmp, &c->replay_list, list) {
329debf12d5SArtem Bityutskiy 		if (is_hash_key(c, &r->key))
330debf12d5SArtem Bityutskiy 			kfree(r->nm.name);
331debf12d5SArtem Bityutskiy 		list_del(&r->list);
332debf12d5SArtem Bityutskiy 		kfree(r);
333debf12d5SArtem Bityutskiy 	}
334debf12d5SArtem Bityutskiy }
335debf12d5SArtem Bityutskiy 
336debf12d5SArtem Bityutskiy /**
337debf12d5SArtem Bityutskiy  * insert_node - insert a node to the replay list
3381e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
3391e51764aSArtem Bityutskiy  * @lnum: node logical eraseblock number
3401e51764aSArtem Bityutskiy  * @offs: node offset
3411e51764aSArtem Bityutskiy  * @len: node length
3421e51764aSArtem Bityutskiy  * @key: node key
3431e51764aSArtem Bityutskiy  * @sqnum: sequence number
3441e51764aSArtem Bityutskiy  * @deletion: non-zero if this is a deletion
3451e51764aSArtem Bityutskiy  * @used: number of bytes in use in a LEB
3461e51764aSArtem Bityutskiy  * @old_size: truncation old size
3471e51764aSArtem Bityutskiy  * @new_size: truncation new size
3481e51764aSArtem Bityutskiy  *
349debf12d5SArtem Bityutskiy  * This function inserts a scanned non-direntry node to the replay list. The
350debf12d5SArtem Bityutskiy  * replay list contains @struct replay_entry elements, and we sort this list in
351debf12d5SArtem Bityutskiy  * sequence number order before applying it. The replay list is applied at the
352debf12d5SArtem Bityutskiy  * very end of the replay process. Since the list is sorted in sequence number
353debf12d5SArtem Bityutskiy  * order, the older modifications are applied first. This function returns zero
354debf12d5SArtem Bityutskiy  * in case of success and a negative error code in case of failure.
3551e51764aSArtem Bityutskiy  */
3561e51764aSArtem Bityutskiy static int insert_node(struct ubifs_info *c, int lnum, int offs, int len,
3571e51764aSArtem Bityutskiy 		       union ubifs_key *key, unsigned long long sqnum,
3581e51764aSArtem Bityutskiy 		       int deletion, int *used, loff_t old_size,
3591e51764aSArtem Bityutskiy 		       loff_t new_size)
3601e51764aSArtem Bityutskiy {
3611e51764aSArtem Bityutskiy 	struct replay_entry *r;
3621e51764aSArtem Bityutskiy 
363515315a1SArtem Bityutskiy 	dbg_mntk(key, "add LEB %d:%d, key ", lnum, offs);
364debf12d5SArtem Bityutskiy 
3651e51764aSArtem Bityutskiy 	if (key_inum(c, key) >= c->highest_inum)
3661e51764aSArtem Bityutskiy 		c->highest_inum = key_inum(c, key);
3671e51764aSArtem Bityutskiy 
3681e51764aSArtem Bityutskiy 	r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL);
3691e51764aSArtem Bityutskiy 	if (!r)
3701e51764aSArtem Bityutskiy 		return -ENOMEM;
3711e51764aSArtem Bityutskiy 
3721e51764aSArtem Bityutskiy 	if (!deletion)
3731e51764aSArtem Bityutskiy 		*used += ALIGN(len, 8);
3741e51764aSArtem Bityutskiy 	r->lnum = lnum;
3751e51764aSArtem Bityutskiy 	r->offs = offs;
3761e51764aSArtem Bityutskiy 	r->len = len;
377074bcb9bSArtem Bityutskiy 	r->deletion = !!deletion;
3781e51764aSArtem Bityutskiy 	r->sqnum = sqnum;
379074bcb9bSArtem Bityutskiy 	key_copy(c, key, &r->key);
3801e51764aSArtem Bityutskiy 	r->old_size = old_size;
3811e51764aSArtem Bityutskiy 	r->new_size = new_size;
3821e51764aSArtem Bityutskiy 
383debf12d5SArtem Bityutskiy 	list_add_tail(&r->list, &c->replay_list);
3841e51764aSArtem Bityutskiy 	return 0;
3851e51764aSArtem Bityutskiy }
3861e51764aSArtem Bityutskiy 
3871e51764aSArtem Bityutskiy /**
388debf12d5SArtem Bityutskiy  * insert_dent - insert a directory entry node into the replay list.
3891e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
3901e51764aSArtem Bityutskiy  * @lnum: node logical eraseblock number
3911e51764aSArtem Bityutskiy  * @offs: node offset
3921e51764aSArtem Bityutskiy  * @len: node length
3931e51764aSArtem Bityutskiy  * @key: node key
3941e51764aSArtem Bityutskiy  * @name: directory entry name
3951e51764aSArtem Bityutskiy  * @nlen: directory entry name length
3961e51764aSArtem Bityutskiy  * @sqnum: sequence number
3971e51764aSArtem Bityutskiy  * @deletion: non-zero if this is a deletion
3981e51764aSArtem Bityutskiy  * @used: number of bytes in use in a LEB
3991e51764aSArtem Bityutskiy  *
400debf12d5SArtem Bityutskiy  * This function inserts a scanned directory entry node or an extended
401debf12d5SArtem Bityutskiy  * attribute entry to the replay list. Returns zero in case of success and a
402debf12d5SArtem Bityutskiy  * negative error code in case of failure.
4031e51764aSArtem Bityutskiy  */
4041e51764aSArtem Bityutskiy static int insert_dent(struct ubifs_info *c, int lnum, int offs, int len,
4051e51764aSArtem Bityutskiy 		       union ubifs_key *key, const char *name, int nlen,
4061e51764aSArtem Bityutskiy 		       unsigned long long sqnum, int deletion, int *used)
4071e51764aSArtem Bityutskiy {
4081e51764aSArtem Bityutskiy 	struct replay_entry *r;
4091e51764aSArtem Bityutskiy 	char *nbuf;
4101e51764aSArtem Bityutskiy 
411515315a1SArtem Bityutskiy 	dbg_mntk(key, "add LEB %d:%d, key ", lnum, offs);
4121e51764aSArtem Bityutskiy 	if (key_inum(c, key) >= c->highest_inum)
4131e51764aSArtem Bityutskiy 		c->highest_inum = key_inum(c, key);
4141e51764aSArtem Bityutskiy 
4151e51764aSArtem Bityutskiy 	r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL);
4161e51764aSArtem Bityutskiy 	if (!r)
4171e51764aSArtem Bityutskiy 		return -ENOMEM;
418debf12d5SArtem Bityutskiy 
4191e51764aSArtem Bityutskiy 	nbuf = kmalloc(nlen + 1, GFP_KERNEL);
4201e51764aSArtem Bityutskiy 	if (!nbuf) {
4211e51764aSArtem Bityutskiy 		kfree(r);
4221e51764aSArtem Bityutskiy 		return -ENOMEM;
4231e51764aSArtem Bityutskiy 	}
4241e51764aSArtem Bityutskiy 
4251e51764aSArtem Bityutskiy 	if (!deletion)
4261e51764aSArtem Bityutskiy 		*used += ALIGN(len, 8);
4271e51764aSArtem Bityutskiy 	r->lnum = lnum;
4281e51764aSArtem Bityutskiy 	r->offs = offs;
4291e51764aSArtem Bityutskiy 	r->len = len;
430074bcb9bSArtem Bityutskiy 	r->deletion = !!deletion;
4311e51764aSArtem Bityutskiy 	r->sqnum = sqnum;
432074bcb9bSArtem Bityutskiy 	key_copy(c, key, &r->key);
4331e51764aSArtem Bityutskiy 	r->nm.len = nlen;
4341e51764aSArtem Bityutskiy 	memcpy(nbuf, name, nlen);
4351e51764aSArtem Bityutskiy 	nbuf[nlen] = '\0';
4361e51764aSArtem Bityutskiy 	r->nm.name = nbuf;
4371e51764aSArtem Bityutskiy 
438debf12d5SArtem Bityutskiy 	list_add_tail(&r->list, &c->replay_list);
4391e51764aSArtem Bityutskiy 	return 0;
4401e51764aSArtem Bityutskiy }
4411e51764aSArtem Bityutskiy 
4421e51764aSArtem Bityutskiy /**
4431e51764aSArtem Bityutskiy  * ubifs_validate_entry - validate directory or extended attribute entry node.
4441e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
4451e51764aSArtem Bityutskiy  * @dent: the node to validate
4461e51764aSArtem Bityutskiy  *
4471e51764aSArtem Bityutskiy  * This function validates directory or extended attribute entry node @dent.
4481e51764aSArtem Bityutskiy  * Returns zero if the node is all right and a %-EINVAL if not.
4491e51764aSArtem Bityutskiy  */
4501e51764aSArtem Bityutskiy int ubifs_validate_entry(struct ubifs_info *c,
4511e51764aSArtem Bityutskiy 			 const struct ubifs_dent_node *dent)
4521e51764aSArtem Bityutskiy {
4531e51764aSArtem Bityutskiy 	int key_type = key_type_flash(c, dent->key);
4541e51764aSArtem Bityutskiy 	int nlen = le16_to_cpu(dent->nlen);
4551e51764aSArtem Bityutskiy 
4561e51764aSArtem Bityutskiy 	if (le32_to_cpu(dent->ch.len) != nlen + UBIFS_DENT_NODE_SZ + 1 ||
4571e51764aSArtem Bityutskiy 	    dent->type >= UBIFS_ITYPES_CNT ||
4581e51764aSArtem Bityutskiy 	    nlen > UBIFS_MAX_NLEN || dent->name[nlen] != 0 ||
4591e51764aSArtem Bityutskiy 	    strnlen(dent->name, nlen) != nlen ||
4601e51764aSArtem Bityutskiy 	    le64_to_cpu(dent->inum) > MAX_INUM) {
461*235c362bSSheng Yong 		ubifs_err(c, "bad %s node", key_type == UBIFS_DENT_KEY ?
4621e51764aSArtem Bityutskiy 			  "directory entry" : "extended attribute entry");
4631e51764aSArtem Bityutskiy 		return -EINVAL;
4641e51764aSArtem Bityutskiy 	}
4651e51764aSArtem Bityutskiy 
4661e51764aSArtem Bityutskiy 	if (key_type != UBIFS_DENT_KEY && key_type != UBIFS_XENT_KEY) {
467*235c362bSSheng Yong 		ubifs_err(c, "bad key type %d", key_type);
4681e51764aSArtem Bityutskiy 		return -EINVAL;
4691e51764aSArtem Bityutskiy 	}
4701e51764aSArtem Bityutskiy 
4711e51764aSArtem Bityutskiy 	return 0;
4721e51764aSArtem Bityutskiy }
4731e51764aSArtem Bityutskiy 
4741e51764aSArtem Bityutskiy /**
47591c66083SArtem Bityutskiy  * is_last_bud - check if the bud is the last in the journal head.
47691c66083SArtem Bityutskiy  * @c: UBIFS file-system description object
47791c66083SArtem Bityutskiy  * @bud: bud description object
47891c66083SArtem Bityutskiy  *
47991c66083SArtem Bityutskiy  * This function checks if bud @bud is the last bud in its journal head. This
48091c66083SArtem Bityutskiy  * information is then used by 'replay_bud()' to decide whether the bud can
48191c66083SArtem Bityutskiy  * have corruptions or not. Indeed, only last buds can be corrupted by power
48291c66083SArtem Bityutskiy  * cuts. Returns %1 if this is the last bud, and %0 if not.
48391c66083SArtem Bityutskiy  */
48491c66083SArtem Bityutskiy static int is_last_bud(struct ubifs_info *c, struct ubifs_bud *bud)
48591c66083SArtem Bityutskiy {
48691c66083SArtem Bityutskiy 	struct ubifs_jhead *jh = &c->jheads[bud->jhead];
48791c66083SArtem Bityutskiy 	struct ubifs_bud *next;
48891c66083SArtem Bityutskiy 	uint32_t data;
48991c66083SArtem Bityutskiy 	int err;
49091c66083SArtem Bityutskiy 
49191c66083SArtem Bityutskiy 	if (list_is_last(&bud->list, &jh->buds_list))
49291c66083SArtem Bityutskiy 		return 1;
49391c66083SArtem Bityutskiy 
49491c66083SArtem Bityutskiy 	/*
49591c66083SArtem Bityutskiy 	 * The following is a quirk to make sure we work correctly with UBIFS
49691c66083SArtem Bityutskiy 	 * images used with older UBIFS.
49791c66083SArtem Bityutskiy 	 *
49891c66083SArtem Bityutskiy 	 * Normally, the last bud will be the last in the journal head's list
49991c66083SArtem Bityutskiy 	 * of bud. However, there is one exception if the UBIFS image belongs
50091c66083SArtem Bityutskiy 	 * to older UBIFS. This is fairly unlikely: one would need to use old
50191c66083SArtem Bityutskiy 	 * UBIFS, then have a power cut exactly at the right point, and then
50291c66083SArtem Bityutskiy 	 * try to mount this image with new UBIFS.
50391c66083SArtem Bityutskiy 	 *
50491c66083SArtem Bityutskiy 	 * The exception is: it is possible to have 2 buds A and B, A goes
50591c66083SArtem Bityutskiy 	 * before B, and B is the last, bud B is contains no data, and bud A is
50691c66083SArtem Bityutskiy 	 * corrupted at the end. The reason is that in older versions when the
50791c66083SArtem Bityutskiy 	 * journal code switched the next bud (from A to B), it first added a
50891c66083SArtem Bityutskiy 	 * log reference node for the new bud (B), and only after this it
50991c66083SArtem Bityutskiy 	 * synchronized the write-buffer of current bud (A). But later this was
51091c66083SArtem Bityutskiy 	 * changed and UBIFS started to always synchronize the write-buffer of
51191c66083SArtem Bityutskiy 	 * the bud (A) before writing the log reference for the new bud (B).
51291c66083SArtem Bityutskiy 	 *
51391c66083SArtem Bityutskiy 	 * But because older UBIFS always synchronized A's write-buffer before
51491c66083SArtem Bityutskiy 	 * writing to B, we can recognize this exceptional situation but
51591c66083SArtem Bityutskiy 	 * checking the contents of bud B - if it is empty, then A can be
51691c66083SArtem Bityutskiy 	 * treated as the last and we can recover it.
51791c66083SArtem Bityutskiy 	 *
51891c66083SArtem Bityutskiy 	 * TODO: remove this piece of code in a couple of years (today it is
51991c66083SArtem Bityutskiy 	 * 16.05.2011).
52091c66083SArtem Bityutskiy 	 */
52191c66083SArtem Bityutskiy 	next = list_entry(bud->list.next, struct ubifs_bud, list);
52291c66083SArtem Bityutskiy 	if (!list_is_last(&next->list, &jh->buds_list))
52391c66083SArtem Bityutskiy 		return 0;
52491c66083SArtem Bityutskiy 
525d304820aSArtem Bityutskiy 	err = ubifs_leb_read(c, next->lnum, (char *)&data, next->start, 4, 1);
52691c66083SArtem Bityutskiy 	if (err)
52791c66083SArtem Bityutskiy 		return 0;
52891c66083SArtem Bityutskiy 
52991c66083SArtem Bityutskiy 	return data == 0xFFFFFFFF;
53091c66083SArtem Bityutskiy }
53191c66083SArtem Bityutskiy 
53291c66083SArtem Bityutskiy /**
5331e51764aSArtem Bityutskiy  * replay_bud - replay a bud logical eraseblock.
5341e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
535e76a4526SArtem Bityutskiy  * @b: bud entry which describes the bud
5361e51764aSArtem Bityutskiy  *
537e76a4526SArtem Bityutskiy  * This function replays bud @bud, recovers it if needed, and adds all nodes
538e76a4526SArtem Bityutskiy  * from this bud to the replay list. Returns zero in case of success and a
539e76a4526SArtem Bityutskiy  * negative error code in case of failure.
5401e51764aSArtem Bityutskiy  */
541e76a4526SArtem Bityutskiy static int replay_bud(struct ubifs_info *c, struct bud_entry *b)
5421e51764aSArtem Bityutskiy {
54391c66083SArtem Bityutskiy 	int is_last = is_last_bud(c, b->bud);
544e76a4526SArtem Bityutskiy 	int err = 0, used = 0, lnum = b->bud->lnum, offs = b->bud->start;
5451e51764aSArtem Bityutskiy 	struct ubifs_scan_leb *sleb;
5461e51764aSArtem Bityutskiy 	struct ubifs_scan_node *snod;
5471e51764aSArtem Bityutskiy 
54891c66083SArtem Bityutskiy 	dbg_mnt("replay bud LEB %d, head %d, offs %d, is_last %d",
54991c66083SArtem Bityutskiy 		lnum, b->bud->jhead, offs, is_last);
550e76a4526SArtem Bityutskiy 
55191c66083SArtem Bityutskiy 	if (c->need_recovery && is_last)
55291c66083SArtem Bityutskiy 		/*
55391c66083SArtem Bityutskiy 		 * Recover only last LEBs in the journal heads, because power
55491c66083SArtem Bityutskiy 		 * cuts may cause corruptions only in these LEBs, because only
55591c66083SArtem Bityutskiy 		 * these LEBs could possibly be written to at the power cut
55691c66083SArtem Bityutskiy 		 * time.
55791c66083SArtem Bityutskiy 		 */
558efcfde54SArtem Bityutskiy 		sleb = ubifs_recover_leb(c, lnum, offs, c->sbuf, b->bud->jhead);
5591e51764aSArtem Bityutskiy 	else
560348709baSArtem Bityutskiy 		sleb = ubifs_scan(c, lnum, offs, c->sbuf, 0);
5611e51764aSArtem Bityutskiy 	if (IS_ERR(sleb))
5621e51764aSArtem Bityutskiy 		return PTR_ERR(sleb);
5631e51764aSArtem Bityutskiy 
5641e51764aSArtem Bityutskiy 	/*
5651e51764aSArtem Bityutskiy 	 * The bud does not have to start from offset zero - the beginning of
5661e51764aSArtem Bityutskiy 	 * the 'lnum' LEB may contain previously committed data. One of the
5671e51764aSArtem Bityutskiy 	 * things we have to do in replay is to correctly update lprops with
5681e51764aSArtem Bityutskiy 	 * newer information about this LEB.
5691e51764aSArtem Bityutskiy 	 *
5701e51764aSArtem Bityutskiy 	 * At this point lprops thinks that this LEB has 'c->leb_size - offs'
5711e51764aSArtem Bityutskiy 	 * bytes of free space because it only contain information about
5721e51764aSArtem Bityutskiy 	 * committed data.
5731e51764aSArtem Bityutskiy 	 *
5741e51764aSArtem Bityutskiy 	 * But we know that real amount of free space is 'c->leb_size -
5751e51764aSArtem Bityutskiy 	 * sleb->endpt', and the space in the 'lnum' LEB between 'offs' and
5761e51764aSArtem Bityutskiy 	 * 'sleb->endpt' is used by bud data. We have to correctly calculate
5771e51764aSArtem Bityutskiy 	 * how much of these data are dirty and update lprops with this
5781e51764aSArtem Bityutskiy 	 * information.
5791e51764aSArtem Bityutskiy 	 *
5801e51764aSArtem Bityutskiy 	 * The dirt in that LEB region is comprised of padding nodes, deletion
5811e51764aSArtem Bityutskiy 	 * nodes, truncation nodes and nodes which are obsoleted by subsequent
5821e51764aSArtem Bityutskiy 	 * nodes in this LEB. So instead of calculating clean space, we
5831e51764aSArtem Bityutskiy 	 * calculate used space ('used' variable).
5841e51764aSArtem Bityutskiy 	 */
5851e51764aSArtem Bityutskiy 
5861e51764aSArtem Bityutskiy 	list_for_each_entry(snod, &sleb->nodes, list) {
5871e51764aSArtem Bityutskiy 		int deletion = 0;
5881e51764aSArtem Bityutskiy 
5891e51764aSArtem Bityutskiy 		cond_resched();
5901e51764aSArtem Bityutskiy 
5911e51764aSArtem Bityutskiy 		if (snod->sqnum >= SQNUM_WATERMARK) {
592*235c362bSSheng Yong 			ubifs_err(c, "file system's life ended");
5931e51764aSArtem Bityutskiy 			goto out_dump;
5941e51764aSArtem Bityutskiy 		}
5951e51764aSArtem Bityutskiy 
5961e51764aSArtem Bityutskiy 		if (snod->sqnum > c->max_sqnum)
5971e51764aSArtem Bityutskiy 			c->max_sqnum = snod->sqnum;
5981e51764aSArtem Bityutskiy 
5991e51764aSArtem Bityutskiy 		switch (snod->type) {
6001e51764aSArtem Bityutskiy 		case UBIFS_INO_NODE:
6011e51764aSArtem Bityutskiy 		{
6021e51764aSArtem Bityutskiy 			struct ubifs_ino_node *ino = snod->node;
6031e51764aSArtem Bityutskiy 			loff_t new_size = le64_to_cpu(ino->size);
6041e51764aSArtem Bityutskiy 
6051e51764aSArtem Bityutskiy 			if (le32_to_cpu(ino->nlink) == 0)
6061e51764aSArtem Bityutskiy 				deletion = 1;
6071e51764aSArtem Bityutskiy 			err = insert_node(c, lnum, snod->offs, snod->len,
6081e51764aSArtem Bityutskiy 					  &snod->key, snod->sqnum, deletion,
6091e51764aSArtem Bityutskiy 					  &used, 0, new_size);
6101e51764aSArtem Bityutskiy 			break;
6111e51764aSArtem Bityutskiy 		}
6121e51764aSArtem Bityutskiy 		case UBIFS_DATA_NODE:
6131e51764aSArtem Bityutskiy 		{
6141e51764aSArtem Bityutskiy 			struct ubifs_data_node *dn = snod->node;
6151e51764aSArtem Bityutskiy 			loff_t new_size = le32_to_cpu(dn->size) +
6161e51764aSArtem Bityutskiy 					  key_block(c, &snod->key) *
6171e51764aSArtem Bityutskiy 					  UBIFS_BLOCK_SIZE;
6181e51764aSArtem Bityutskiy 
6191e51764aSArtem Bityutskiy 			err = insert_node(c, lnum, snod->offs, snod->len,
6201e51764aSArtem Bityutskiy 					  &snod->key, snod->sqnum, deletion,
6211e51764aSArtem Bityutskiy 					  &used, 0, new_size);
6221e51764aSArtem Bityutskiy 			break;
6231e51764aSArtem Bityutskiy 		}
6241e51764aSArtem Bityutskiy 		case UBIFS_DENT_NODE:
6251e51764aSArtem Bityutskiy 		case UBIFS_XENT_NODE:
6261e51764aSArtem Bityutskiy 		{
6271e51764aSArtem Bityutskiy 			struct ubifs_dent_node *dent = snod->node;
6281e51764aSArtem Bityutskiy 
6291e51764aSArtem Bityutskiy 			err = ubifs_validate_entry(c, dent);
6301e51764aSArtem Bityutskiy 			if (err)
6311e51764aSArtem Bityutskiy 				goto out_dump;
6321e51764aSArtem Bityutskiy 
6331e51764aSArtem Bityutskiy 			err = insert_dent(c, lnum, snod->offs, snod->len,
6341e51764aSArtem Bityutskiy 					  &snod->key, dent->name,
6351e51764aSArtem Bityutskiy 					  le16_to_cpu(dent->nlen), snod->sqnum,
6361e51764aSArtem Bityutskiy 					  !le64_to_cpu(dent->inum), &used);
6371e51764aSArtem Bityutskiy 			break;
6381e51764aSArtem Bityutskiy 		}
6391e51764aSArtem Bityutskiy 		case UBIFS_TRUN_NODE:
6401e51764aSArtem Bityutskiy 		{
6411e51764aSArtem Bityutskiy 			struct ubifs_trun_node *trun = snod->node;
6421e51764aSArtem Bityutskiy 			loff_t old_size = le64_to_cpu(trun->old_size);
6431e51764aSArtem Bityutskiy 			loff_t new_size = le64_to_cpu(trun->new_size);
6441e51764aSArtem Bityutskiy 			union ubifs_key key;
6451e51764aSArtem Bityutskiy 
6461e51764aSArtem Bityutskiy 			/* Validate truncation node */
6471e51764aSArtem Bityutskiy 			if (old_size < 0 || old_size > c->max_inode_sz ||
6481e51764aSArtem Bityutskiy 			    new_size < 0 || new_size > c->max_inode_sz ||
6491e51764aSArtem Bityutskiy 			    old_size <= new_size) {
650*235c362bSSheng Yong 				ubifs_err(c, "bad truncation node");
6511e51764aSArtem Bityutskiy 				goto out_dump;
6521e51764aSArtem Bityutskiy 			}
6531e51764aSArtem Bityutskiy 
6541e51764aSArtem Bityutskiy 			/*
6551e51764aSArtem Bityutskiy 			 * Create a fake truncation key just to use the same
6561e51764aSArtem Bityutskiy 			 * functions which expect nodes to have keys.
6571e51764aSArtem Bityutskiy 			 */
6581e51764aSArtem Bityutskiy 			trun_key_init(c, &key, le32_to_cpu(trun->inum));
6591e51764aSArtem Bityutskiy 			err = insert_node(c, lnum, snod->offs, snod->len,
6601e51764aSArtem Bityutskiy 					  &key, snod->sqnum, 1, &used,
6611e51764aSArtem Bityutskiy 					  old_size, new_size);
6621e51764aSArtem Bityutskiy 			break;
6631e51764aSArtem Bityutskiy 		}
6641e51764aSArtem Bityutskiy 		default:
665*235c362bSSheng Yong 			ubifs_err(c, "unexpected node type %d in bud LEB %d:%d",
6661e51764aSArtem Bityutskiy 				  snod->type, lnum, snod->offs);
6671e51764aSArtem Bityutskiy 			err = -EINVAL;
6681e51764aSArtem Bityutskiy 			goto out_dump;
6691e51764aSArtem Bityutskiy 		}
6701e51764aSArtem Bityutskiy 		if (err)
6711e51764aSArtem Bityutskiy 			goto out;
6721e51764aSArtem Bityutskiy 	}
6731e51764aSArtem Bityutskiy 
674c49139d8SArtem Bityutskiy 	ubifs_assert(ubifs_search_bud(c, lnum));
6751e51764aSArtem Bityutskiy 	ubifs_assert(sleb->endpt - offs >= used);
6761e51764aSArtem Bityutskiy 	ubifs_assert(sleb->endpt % c->min_io_size == 0);
6771e51764aSArtem Bityutskiy 
678e76a4526SArtem Bityutskiy 	b->dirty = sleb->endpt - offs - used;
679e76a4526SArtem Bityutskiy 	b->free = c->leb_size - sleb->endpt;
68079fda517SArtem Bityutskiy 	dbg_mnt("bud LEB %d replied: dirty %d, free %d",
68179fda517SArtem Bityutskiy 		lnum, b->dirty, b->free);
6821e51764aSArtem Bityutskiy 
6831e51764aSArtem Bityutskiy out:
6841e51764aSArtem Bityutskiy 	ubifs_scan_destroy(sleb);
6851e51764aSArtem Bityutskiy 	return err;
6861e51764aSArtem Bityutskiy 
6871e51764aSArtem Bityutskiy out_dump:
688*235c362bSSheng Yong 	ubifs_err(c, "bad node is at LEB %d:%d", lnum, snod->offs);
689edf6be24SArtem Bityutskiy 	ubifs_dump_node(c, snod->node);
6901e51764aSArtem Bityutskiy 	ubifs_scan_destroy(sleb);
6911e51764aSArtem Bityutskiy 	return -EINVAL;
6921e51764aSArtem Bityutskiy }
6931e51764aSArtem Bityutskiy 
6941e51764aSArtem Bityutskiy /**
6951e51764aSArtem Bityutskiy  * replay_buds - replay all buds.
6961e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
6971e51764aSArtem Bityutskiy  *
6981e51764aSArtem Bityutskiy  * This function returns zero in case of success and a negative error code in
6991e51764aSArtem Bityutskiy  * case of failure.
7001e51764aSArtem Bityutskiy  */
7011e51764aSArtem Bityutskiy static int replay_buds(struct ubifs_info *c)
7021e51764aSArtem Bityutskiy {
7031e51764aSArtem Bityutskiy 	struct bud_entry *b;
704074bcb9bSArtem Bityutskiy 	int err;
7057703f09dSArtem Bityutskiy 	unsigned long long prev_sqnum = 0;
7061e51764aSArtem Bityutskiy 
7071e51764aSArtem Bityutskiy 	list_for_each_entry(b, &c->replay_buds, list) {
708e76a4526SArtem Bityutskiy 		err = replay_bud(c, b);
7091e51764aSArtem Bityutskiy 		if (err)
7101e51764aSArtem Bityutskiy 			return err;
7117703f09dSArtem Bityutskiy 
7127703f09dSArtem Bityutskiy 		ubifs_assert(b->sqnum > prev_sqnum);
7137703f09dSArtem Bityutskiy 		prev_sqnum = b->sqnum;
7141e51764aSArtem Bityutskiy 	}
7151e51764aSArtem Bityutskiy 
7161e51764aSArtem Bityutskiy 	return 0;
7171e51764aSArtem Bityutskiy }
7181e51764aSArtem Bityutskiy 
7191e51764aSArtem Bityutskiy /**
7201e51764aSArtem Bityutskiy  * destroy_bud_list - destroy the list of buds to replay.
7211e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
7221e51764aSArtem Bityutskiy  */
7231e51764aSArtem Bityutskiy static void destroy_bud_list(struct ubifs_info *c)
7241e51764aSArtem Bityutskiy {
7251e51764aSArtem Bityutskiy 	struct bud_entry *b;
7261e51764aSArtem Bityutskiy 
7271e51764aSArtem Bityutskiy 	while (!list_empty(&c->replay_buds)) {
7281e51764aSArtem Bityutskiy 		b = list_entry(c->replay_buds.next, struct bud_entry, list);
7291e51764aSArtem Bityutskiy 		list_del(&b->list);
7301e51764aSArtem Bityutskiy 		kfree(b);
7311e51764aSArtem Bityutskiy 	}
7321e51764aSArtem Bityutskiy }
7331e51764aSArtem Bityutskiy 
7341e51764aSArtem Bityutskiy /**
7351e51764aSArtem Bityutskiy  * add_replay_bud - add a bud to the list of buds to replay.
7361e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
7371e51764aSArtem Bityutskiy  * @lnum: bud logical eraseblock number to replay
7381e51764aSArtem Bityutskiy  * @offs: bud start offset
7391e51764aSArtem Bityutskiy  * @jhead: journal head to which this bud belongs
7401e51764aSArtem Bityutskiy  * @sqnum: reference node sequence number
7411e51764aSArtem Bityutskiy  *
7421e51764aSArtem Bityutskiy  * This function returns zero in case of success and a negative error code in
7431e51764aSArtem Bityutskiy  * case of failure.
7441e51764aSArtem Bityutskiy  */
7451e51764aSArtem Bityutskiy static int add_replay_bud(struct ubifs_info *c, int lnum, int offs, int jhead,
7461e51764aSArtem Bityutskiy 			  unsigned long long sqnum)
7471e51764aSArtem Bityutskiy {
7481e51764aSArtem Bityutskiy 	struct ubifs_bud *bud;
7491e51764aSArtem Bityutskiy 	struct bud_entry *b;
7501e51764aSArtem Bityutskiy 
7511e51764aSArtem Bityutskiy 	dbg_mnt("add replay bud LEB %d:%d, head %d", lnum, offs, jhead);
7521e51764aSArtem Bityutskiy 
7531e51764aSArtem Bityutskiy 	bud = kmalloc(sizeof(struct ubifs_bud), GFP_KERNEL);
7541e51764aSArtem Bityutskiy 	if (!bud)
7551e51764aSArtem Bityutskiy 		return -ENOMEM;
7561e51764aSArtem Bityutskiy 
7571e51764aSArtem Bityutskiy 	b = kmalloc(sizeof(struct bud_entry), GFP_KERNEL);
7581e51764aSArtem Bityutskiy 	if (!b) {
7591e51764aSArtem Bityutskiy 		kfree(bud);
7601e51764aSArtem Bityutskiy 		return -ENOMEM;
7611e51764aSArtem Bityutskiy 	}
7621e51764aSArtem Bityutskiy 
7631e51764aSArtem Bityutskiy 	bud->lnum = lnum;
7641e51764aSArtem Bityutskiy 	bud->start = offs;
7651e51764aSArtem Bityutskiy 	bud->jhead = jhead;
7661e51764aSArtem Bityutskiy 	ubifs_add_bud(c, bud);
7671e51764aSArtem Bityutskiy 
7681e51764aSArtem Bityutskiy 	b->bud = bud;
7691e51764aSArtem Bityutskiy 	b->sqnum = sqnum;
7701e51764aSArtem Bityutskiy 	list_add_tail(&b->list, &c->replay_buds);
7711e51764aSArtem Bityutskiy 
7721e51764aSArtem Bityutskiy 	return 0;
7731e51764aSArtem Bityutskiy }
7741e51764aSArtem Bityutskiy 
7751e51764aSArtem Bityutskiy /**
7761e51764aSArtem Bityutskiy  * validate_ref - validate a reference node.
7771e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
7781e51764aSArtem Bityutskiy  * @ref: the reference node to validate
7791e51764aSArtem Bityutskiy  * @ref_lnum: LEB number of the reference node
7801e51764aSArtem Bityutskiy  * @ref_offs: reference node offset
7811e51764aSArtem Bityutskiy  *
7821e51764aSArtem Bityutskiy  * This function returns %1 if a bud reference already exists for the LEB. %0 is
7831e51764aSArtem Bityutskiy  * returned if the reference node is new, otherwise %-EINVAL is returned if
7841e51764aSArtem Bityutskiy  * validation failed.
7851e51764aSArtem Bityutskiy  */
7861e51764aSArtem Bityutskiy static int validate_ref(struct ubifs_info *c, const struct ubifs_ref_node *ref)
7871e51764aSArtem Bityutskiy {
7881e51764aSArtem Bityutskiy 	struct ubifs_bud *bud;
7891e51764aSArtem Bityutskiy 	int lnum = le32_to_cpu(ref->lnum);
7901e51764aSArtem Bityutskiy 	unsigned int offs = le32_to_cpu(ref->offs);
7911e51764aSArtem Bityutskiy 	unsigned int jhead = le32_to_cpu(ref->jhead);
7921e51764aSArtem Bityutskiy 
7931e51764aSArtem Bityutskiy 	/*
7941e51764aSArtem Bityutskiy 	 * ref->offs may point to the end of LEB when the journal head points
7951e51764aSArtem Bityutskiy 	 * to the end of LEB and we write reference node for it during commit.
7961e51764aSArtem Bityutskiy 	 * So this is why we require 'offs > c->leb_size'.
7971e51764aSArtem Bityutskiy 	 */
7981e51764aSArtem Bityutskiy 	if (jhead >= c->jhead_cnt || lnum >= c->leb_cnt ||
7991e51764aSArtem Bityutskiy 	    lnum < c->main_first || offs > c->leb_size ||
8001e51764aSArtem Bityutskiy 	    offs & (c->min_io_size - 1))
8011e51764aSArtem Bityutskiy 		return -EINVAL;
8021e51764aSArtem Bityutskiy 
8031e51764aSArtem Bityutskiy 	/* Make sure we have not already looked at this bud */
8041e51764aSArtem Bityutskiy 	bud = ubifs_search_bud(c, lnum);
8051e51764aSArtem Bityutskiy 	if (bud) {
8061e51764aSArtem Bityutskiy 		if (bud->jhead == jhead && bud->start <= offs)
8071e51764aSArtem Bityutskiy 			return 1;
808*235c362bSSheng Yong 		ubifs_err(c, "bud at LEB %d:%d was already referred", lnum, offs);
8091e51764aSArtem Bityutskiy 		return -EINVAL;
8101e51764aSArtem Bityutskiy 	}
8111e51764aSArtem Bityutskiy 
8121e51764aSArtem Bityutskiy 	return 0;
8131e51764aSArtem Bityutskiy }
8141e51764aSArtem Bityutskiy 
8151e51764aSArtem Bityutskiy /**
8161e51764aSArtem Bityutskiy  * replay_log_leb - replay a log logical eraseblock.
8171e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
8181e51764aSArtem Bityutskiy  * @lnum: log logical eraseblock to replay
8191e51764aSArtem Bityutskiy  * @offs: offset to start replaying from
8201e51764aSArtem Bityutskiy  * @sbuf: scan buffer
8211e51764aSArtem Bityutskiy  *
8221e51764aSArtem Bityutskiy  * This function replays a log LEB and returns zero in case of success, %1 if
8231e51764aSArtem Bityutskiy  * this is the last LEB in the log, and a negative error code in case of
8241e51764aSArtem Bityutskiy  * failure.
8251e51764aSArtem Bityutskiy  */
8261e51764aSArtem Bityutskiy static int replay_log_leb(struct ubifs_info *c, int lnum, int offs, void *sbuf)
8271e51764aSArtem Bityutskiy {
8281e51764aSArtem Bityutskiy 	int err;
8291e51764aSArtem Bityutskiy 	struct ubifs_scan_leb *sleb;
8301e51764aSArtem Bityutskiy 	struct ubifs_scan_node *snod;
8311e51764aSArtem Bityutskiy 	const struct ubifs_cs_node *node;
8321e51764aSArtem Bityutskiy 
8331e51764aSArtem Bityutskiy 	dbg_mnt("replay log LEB %d:%d", lnum, offs);
834348709baSArtem Bityutskiy 	sleb = ubifs_scan(c, lnum, offs, sbuf, c->need_recovery);
8351e51764aSArtem Bityutskiy 	if (IS_ERR(sleb)) {
836ed43f2f0SArtem Bityutskiy 		if (PTR_ERR(sleb) != -EUCLEAN || !c->need_recovery)
837ed43f2f0SArtem Bityutskiy 			return PTR_ERR(sleb);
8387d08ae3cSArtem Bityutskiy 		/*
8397d08ae3cSArtem Bityutskiy 		 * Note, the below function will recover this log LEB only if
8407d08ae3cSArtem Bityutskiy 		 * it is the last, because unclean reboots can possibly corrupt
8417d08ae3cSArtem Bityutskiy 		 * only the tail of the log.
8427d08ae3cSArtem Bityutskiy 		 */
8431e51764aSArtem Bityutskiy 		sleb = ubifs_recover_log_leb(c, lnum, offs, sbuf);
8441e51764aSArtem Bityutskiy 		if (IS_ERR(sleb))
8451e51764aSArtem Bityutskiy 			return PTR_ERR(sleb);
8461e51764aSArtem Bityutskiy 	}
8471e51764aSArtem Bityutskiy 
8481e51764aSArtem Bityutskiy 	if (sleb->nodes_cnt == 0) {
8491e51764aSArtem Bityutskiy 		err = 1;
8501e51764aSArtem Bityutskiy 		goto out;
8511e51764aSArtem Bityutskiy 	}
8521e51764aSArtem Bityutskiy 
8531e51764aSArtem Bityutskiy 	node = sleb->buf;
8541e51764aSArtem Bityutskiy 	snod = list_entry(sleb->nodes.next, struct ubifs_scan_node, list);
8551e51764aSArtem Bityutskiy 	if (c->cs_sqnum == 0) {
8561e51764aSArtem Bityutskiy 		/*
8571e51764aSArtem Bityutskiy 		 * This is the first log LEB we are looking at, make sure that
8581e51764aSArtem Bityutskiy 		 * the first node is a commit start node. Also record its
8591e51764aSArtem Bityutskiy 		 * sequence number so that UBIFS can determine where the log
8601e51764aSArtem Bityutskiy 		 * ends, because all nodes which were have higher sequence
8611e51764aSArtem Bityutskiy 		 * numbers.
8621e51764aSArtem Bityutskiy 		 */
8631e51764aSArtem Bityutskiy 		if (snod->type != UBIFS_CS_NODE) {
864*235c362bSSheng Yong 			ubifs_err(c, "first log node at LEB %d:%d is not CS node",
8651e51764aSArtem Bityutskiy 				  lnum, offs);
8661e51764aSArtem Bityutskiy 			goto out_dump;
8671e51764aSArtem Bityutskiy 		}
8681e51764aSArtem Bityutskiy 		if (le64_to_cpu(node->cmt_no) != c->cmt_no) {
869*235c362bSSheng Yong 			ubifs_err(c, "first CS node at LEB %d:%d has wrong commit number %llu expected %llu",
8701e51764aSArtem Bityutskiy 				  lnum, offs,
8711e51764aSArtem Bityutskiy 				  (unsigned long long)le64_to_cpu(node->cmt_no),
8721e51764aSArtem Bityutskiy 				  c->cmt_no);
8731e51764aSArtem Bityutskiy 			goto out_dump;
8741e51764aSArtem Bityutskiy 		}
8751e51764aSArtem Bityutskiy 
8761e51764aSArtem Bityutskiy 		c->cs_sqnum = le64_to_cpu(node->ch.sqnum);
8771e51764aSArtem Bityutskiy 		dbg_mnt("commit start sqnum %llu", c->cs_sqnum);
8781e51764aSArtem Bityutskiy 	}
8791e51764aSArtem Bityutskiy 
8801e51764aSArtem Bityutskiy 	if (snod->sqnum < c->cs_sqnum) {
8811e51764aSArtem Bityutskiy 		/*
8821e51764aSArtem Bityutskiy 		 * This means that we reached end of log and now
8831e51764aSArtem Bityutskiy 		 * look to the older log data, which was already
8841e51764aSArtem Bityutskiy 		 * committed but the eraseblock was not erased (UBIFS
8856edbfafdSArtem Bityutskiy 		 * only un-maps it). So this basically means we have to
8861e51764aSArtem Bityutskiy 		 * exit with "end of log" code.
8871e51764aSArtem Bityutskiy 		 */
8881e51764aSArtem Bityutskiy 		err = 1;
8891e51764aSArtem Bityutskiy 		goto out;
8901e51764aSArtem Bityutskiy 	}
8911e51764aSArtem Bityutskiy 
8921e51764aSArtem Bityutskiy 	/* Make sure the first node sits at offset zero of the LEB */
8931e51764aSArtem Bityutskiy 	if (snod->offs != 0) {
894*235c362bSSheng Yong 		ubifs_err(c, "first node is not at zero offset");
8951e51764aSArtem Bityutskiy 		goto out_dump;
8961e51764aSArtem Bityutskiy 	}
8971e51764aSArtem Bityutskiy 
8981e51764aSArtem Bityutskiy 	list_for_each_entry(snod, &sleb->nodes, list) {
8991e51764aSArtem Bityutskiy 		cond_resched();
9001e51764aSArtem Bityutskiy 
9011e51764aSArtem Bityutskiy 		if (snod->sqnum >= SQNUM_WATERMARK) {
902*235c362bSSheng Yong 			ubifs_err(c, "file system's life ended");
9031e51764aSArtem Bityutskiy 			goto out_dump;
9041e51764aSArtem Bityutskiy 		}
9051e51764aSArtem Bityutskiy 
9061e51764aSArtem Bityutskiy 		if (snod->sqnum < c->cs_sqnum) {
907*235c362bSSheng Yong 			ubifs_err(c, "bad sqnum %llu, commit sqnum %llu",
9081e51764aSArtem Bityutskiy 				  snod->sqnum, c->cs_sqnum);
9091e51764aSArtem Bityutskiy 			goto out_dump;
9101e51764aSArtem Bityutskiy 		}
9111e51764aSArtem Bityutskiy 
9121e51764aSArtem Bityutskiy 		if (snod->sqnum > c->max_sqnum)
9131e51764aSArtem Bityutskiy 			c->max_sqnum = snod->sqnum;
9141e51764aSArtem Bityutskiy 
9151e51764aSArtem Bityutskiy 		switch (snod->type) {
9161e51764aSArtem Bityutskiy 		case UBIFS_REF_NODE: {
9171e51764aSArtem Bityutskiy 			const struct ubifs_ref_node *ref = snod->node;
9181e51764aSArtem Bityutskiy 
9191e51764aSArtem Bityutskiy 			err = validate_ref(c, ref);
9201e51764aSArtem Bityutskiy 			if (err == 1)
9211e51764aSArtem Bityutskiy 				break; /* Already have this bud */
9221e51764aSArtem Bityutskiy 			if (err)
9231e51764aSArtem Bityutskiy 				goto out_dump;
9241e51764aSArtem Bityutskiy 
9251e51764aSArtem Bityutskiy 			err = add_replay_bud(c, le32_to_cpu(ref->lnum),
9261e51764aSArtem Bityutskiy 					     le32_to_cpu(ref->offs),
9271e51764aSArtem Bityutskiy 					     le32_to_cpu(ref->jhead),
9281e51764aSArtem Bityutskiy 					     snod->sqnum);
9291e51764aSArtem Bityutskiy 			if (err)
9301e51764aSArtem Bityutskiy 				goto out;
9311e51764aSArtem Bityutskiy 
9321e51764aSArtem Bityutskiy 			break;
9331e51764aSArtem Bityutskiy 		}
9341e51764aSArtem Bityutskiy 		case UBIFS_CS_NODE:
9351e51764aSArtem Bityutskiy 			/* Make sure it sits at the beginning of LEB */
9361e51764aSArtem Bityutskiy 			if (snod->offs != 0) {
937*235c362bSSheng Yong 				ubifs_err(c, "unexpected node in log");
9381e51764aSArtem Bityutskiy 				goto out_dump;
9391e51764aSArtem Bityutskiy 			}
9401e51764aSArtem Bityutskiy 			break;
9411e51764aSArtem Bityutskiy 		default:
942*235c362bSSheng Yong 			ubifs_err(c, "unexpected node in log");
9431e51764aSArtem Bityutskiy 			goto out_dump;
9441e51764aSArtem Bityutskiy 		}
9451e51764aSArtem Bityutskiy 	}
9461e51764aSArtem Bityutskiy 
9471e51764aSArtem Bityutskiy 	if (sleb->endpt || c->lhead_offs >= c->leb_size) {
9481e51764aSArtem Bityutskiy 		c->lhead_lnum = lnum;
9491e51764aSArtem Bityutskiy 		c->lhead_offs = sleb->endpt;
9501e51764aSArtem Bityutskiy 	}
9511e51764aSArtem Bityutskiy 
9521e51764aSArtem Bityutskiy 	err = !sleb->endpt;
9531e51764aSArtem Bityutskiy out:
9541e51764aSArtem Bityutskiy 	ubifs_scan_destroy(sleb);
9551e51764aSArtem Bityutskiy 	return err;
9561e51764aSArtem Bityutskiy 
9571e51764aSArtem Bityutskiy out_dump:
958*235c362bSSheng Yong 	ubifs_err(c, "log error detected while replaying the log at LEB %d:%d",
9591e51764aSArtem Bityutskiy 		  lnum, offs + snod->offs);
960edf6be24SArtem Bityutskiy 	ubifs_dump_node(c, snod->node);
9611e51764aSArtem Bityutskiy 	ubifs_scan_destroy(sleb);
9621e51764aSArtem Bityutskiy 	return -EINVAL;
9631e51764aSArtem Bityutskiy }
9641e51764aSArtem Bityutskiy 
9651e51764aSArtem Bityutskiy /**
9661e51764aSArtem Bityutskiy  * take_ihead - update the status of the index head in lprops to 'taken'.
9671e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
9681e51764aSArtem Bityutskiy  *
9691e51764aSArtem Bityutskiy  * This function returns the amount of free space in the index head LEB or a
9701e51764aSArtem Bityutskiy  * negative error code.
9711e51764aSArtem Bityutskiy  */
9721e51764aSArtem Bityutskiy static int take_ihead(struct ubifs_info *c)
9731e51764aSArtem Bityutskiy {
9741e51764aSArtem Bityutskiy 	const struct ubifs_lprops *lp;
9751e51764aSArtem Bityutskiy 	int err, free;
9761e51764aSArtem Bityutskiy 
9771e51764aSArtem Bityutskiy 	ubifs_get_lprops(c);
9781e51764aSArtem Bityutskiy 
9791e51764aSArtem Bityutskiy 	lp = ubifs_lpt_lookup_dirty(c, c->ihead_lnum);
9801e51764aSArtem Bityutskiy 	if (IS_ERR(lp)) {
9811e51764aSArtem Bityutskiy 		err = PTR_ERR(lp);
9821e51764aSArtem Bityutskiy 		goto out;
9831e51764aSArtem Bityutskiy 	}
9841e51764aSArtem Bityutskiy 
9851e51764aSArtem Bityutskiy 	free = lp->free;
9861e51764aSArtem Bityutskiy 
9871e51764aSArtem Bityutskiy 	lp = ubifs_change_lp(c, lp, LPROPS_NC, LPROPS_NC,
9881e51764aSArtem Bityutskiy 			     lp->flags | LPROPS_TAKEN, 0);
9891e51764aSArtem Bityutskiy 	if (IS_ERR(lp)) {
9901e51764aSArtem Bityutskiy 		err = PTR_ERR(lp);
9911e51764aSArtem Bityutskiy 		goto out;
9921e51764aSArtem Bityutskiy 	}
9931e51764aSArtem Bityutskiy 
9941e51764aSArtem Bityutskiy 	err = free;
9951e51764aSArtem Bityutskiy out:
9961e51764aSArtem Bityutskiy 	ubifs_release_lprops(c);
9971e51764aSArtem Bityutskiy 	return err;
9981e51764aSArtem Bityutskiy }
9991e51764aSArtem Bityutskiy 
10001e51764aSArtem Bityutskiy /**
10011e51764aSArtem Bityutskiy  * ubifs_replay_journal - replay journal.
10021e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
10031e51764aSArtem Bityutskiy  *
10041e51764aSArtem Bityutskiy  * This function scans the journal, replays and cleans it up. It makes sure all
10051e51764aSArtem Bityutskiy  * memory data structures related to uncommitted journal are built (dirty TNC
10061e51764aSArtem Bityutskiy  * tree, tree of buds, modified lprops, etc).
10071e51764aSArtem Bityutskiy  */
10081e51764aSArtem Bityutskiy int ubifs_replay_journal(struct ubifs_info *c)
10091e51764aSArtem Bityutskiy {
1010d51f17eaSArtem Bityutskiy 	int err, lnum, free;
10111e51764aSArtem Bityutskiy 
10121e51764aSArtem Bityutskiy 	BUILD_BUG_ON(UBIFS_TRUN_KEY > 5);
10131e51764aSArtem Bityutskiy 
10141e51764aSArtem Bityutskiy 	/* Update the status of the index head in lprops to 'taken' */
10151e51764aSArtem Bityutskiy 	free = take_ihead(c);
10161e51764aSArtem Bityutskiy 	if (free < 0)
10171e51764aSArtem Bityutskiy 		return free; /* Error code */
10181e51764aSArtem Bityutskiy 
10191e51764aSArtem Bityutskiy 	if (c->ihead_offs != c->leb_size - free) {
1020*235c362bSSheng Yong 		ubifs_err(c, "bad index head LEB %d:%d", c->ihead_lnum,
10211e51764aSArtem Bityutskiy 			  c->ihead_offs);
10221e51764aSArtem Bityutskiy 		return -EINVAL;
10231e51764aSArtem Bityutskiy 	}
10241e51764aSArtem Bityutskiy 
10251e51764aSArtem Bityutskiy 	dbg_mnt("start replaying the journal");
10261e51764aSArtem Bityutskiy 	c->replaying = 1;
10271e51764aSArtem Bityutskiy 	lnum = c->ltail_lnum = c->lhead_lnum;
10281e51764aSArtem Bityutskiy 
1029d51f17eaSArtem Bityutskiy 	do {
1030d51f17eaSArtem Bityutskiy 		err = replay_log_leb(c, lnum, 0, c->sbuf);
103188cff0f0Shujianyang 		if (err == 1) {
103288cff0f0Shujianyang 			if (lnum != c->lhead_lnum)
10331e51764aSArtem Bityutskiy 				/* We hit the end of the log */
10341e51764aSArtem Bityutskiy 				break;
103588cff0f0Shujianyang 
103688cff0f0Shujianyang 			/*
103788cff0f0Shujianyang 			 * The head of the log must always start with the
103888cff0f0Shujianyang 			 * "commit start" node on a properly formatted UBIFS.
103988cff0f0Shujianyang 			 * But we found no nodes at all, which means that
104088cff0f0Shujianyang 			 * someting went wrong and we cannot proceed mounting
104188cff0f0Shujianyang 			 * the file-system.
104288cff0f0Shujianyang 			 */
1043*235c362bSSheng Yong 			ubifs_err(c, "no UBIFS nodes found at the log head LEB %d:%d, possibly corrupted",
104488cff0f0Shujianyang 				  lnum, 0);
104588cff0f0Shujianyang 			err = -EINVAL;
104688cff0f0Shujianyang 		}
10471e51764aSArtem Bityutskiy 		if (err)
10481e51764aSArtem Bityutskiy 			goto out;
1049d51f17eaSArtem Bityutskiy 		lnum = ubifs_next_log_lnum(c, lnum);
1050c212f402SArtem Bityutskiy 	} while (lnum != c->ltail_lnum);
10511e51764aSArtem Bityutskiy 
10521e51764aSArtem Bityutskiy 	err = replay_buds(c);
10531e51764aSArtem Bityutskiy 	if (err)
10541e51764aSArtem Bityutskiy 		goto out;
10551e51764aSArtem Bityutskiy 
1056debf12d5SArtem Bityutskiy 	err = apply_replay_list(c);
10571e51764aSArtem Bityutskiy 	if (err)
10581e51764aSArtem Bityutskiy 		goto out;
10591e51764aSArtem Bityutskiy 
1060074bcb9bSArtem Bityutskiy 	err = set_buds_lprops(c);
1061074bcb9bSArtem Bityutskiy 	if (err)
1062074bcb9bSArtem Bityutskiy 		goto out;
1063074bcb9bSArtem Bityutskiy 
10646edbfafdSArtem Bityutskiy 	/*
1065b137545cSArtem Bityutskiy 	 * UBIFS budgeting calculations use @c->bi.uncommitted_idx variable
1066b137545cSArtem Bityutskiy 	 * to roughly estimate index growth. Things like @c->bi.min_idx_lebs
10676edbfafdSArtem Bityutskiy 	 * depend on it. This means we have to initialize it to make sure
10686edbfafdSArtem Bityutskiy 	 * budgeting works properly.
10696edbfafdSArtem Bityutskiy 	 */
1070b137545cSArtem Bityutskiy 	c->bi.uncommitted_idx = atomic_long_read(&c->dirty_zn_cnt);
1071b137545cSArtem Bityutskiy 	c->bi.uncommitted_idx *= c->max_idx_node_sz;
10726edbfafdSArtem Bityutskiy 
10731e51764aSArtem Bityutskiy 	ubifs_assert(c->bud_bytes <= c->max_bud_bytes || c->need_recovery);
107479fda517SArtem Bityutskiy 	dbg_mnt("finished, log head LEB %d:%d, max_sqnum %llu, highest_inum %lu",
107579fda517SArtem Bityutskiy 		c->lhead_lnum, c->lhead_offs, c->max_sqnum,
1076e84461adSArtem Bityutskiy 		(unsigned long)c->highest_inum);
10771e51764aSArtem Bityutskiy out:
1078debf12d5SArtem Bityutskiy 	destroy_replay_list(c);
10791e51764aSArtem Bityutskiy 	destroy_bud_list(c);
10801e51764aSArtem Bityutskiy 	c->replaying = 0;
10811e51764aSArtem Bityutskiy 	return err;
10821e51764aSArtem Bityutskiy }
1083