xref: /linux/fs/ubifs/replay.c (revision 07c32de44e67882e66f4f81f78d2a16bb72337e4)
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: Adrian Hunter
81e51764aSArtem Bityutskiy  *          Artem Bityutskiy (Битюцкий Артём)
91e51764aSArtem Bityutskiy  */
101e51764aSArtem Bityutskiy 
111e51764aSArtem Bityutskiy /*
121e51764aSArtem Bityutskiy  * This file contains journal replay code. It runs when the file-system is being
131e51764aSArtem Bityutskiy  * mounted and requires no locking.
141e51764aSArtem Bityutskiy  *
151e51764aSArtem Bityutskiy  * The larger is the journal, the longer it takes to scan it, so the longer it
161e51764aSArtem Bityutskiy  * takes to mount UBIFS. This is why the journal has limited size which may be
171e51764aSArtem Bityutskiy  * changed depending on the system requirements. But a larger journal gives
181e51764aSArtem Bityutskiy  * faster I/O speed because it writes the index less frequently. So this is a
191e51764aSArtem Bityutskiy  * trade-off. Also, the journal is indexed by the in-memory index (TNC), so the
201e51764aSArtem Bityutskiy  * larger is the journal, the more memory its index may consume.
211e51764aSArtem Bityutskiy  */
221e51764aSArtem Bityutskiy 
231e51764aSArtem Bityutskiy #include "ubifs.h"
24debf12d5SArtem Bityutskiy #include <linux/list_sort.h>
25da8ef65fSSascha Hauer #include <crypto/hash.h>
26da8ef65fSSascha Hauer #include <crypto/algapi.h>
271e51764aSArtem Bityutskiy 
281e51764aSArtem Bityutskiy /**
29debf12d5SArtem Bityutskiy  * struct replay_entry - replay list entry.
301e51764aSArtem Bityutskiy  * @lnum: logical eraseblock number of the node
311e51764aSArtem Bityutskiy  * @offs: node offset
321e51764aSArtem Bityutskiy  * @len: node length
33074bcb9bSArtem Bityutskiy  * @deletion: non-zero if this entry corresponds to a node deletion
341e51764aSArtem Bityutskiy  * @sqnum: node sequence number
35debf12d5SArtem Bityutskiy  * @list: links the replay list
361e51764aSArtem Bityutskiy  * @key: node key
371e51764aSArtem Bityutskiy  * @nm: directory entry name
381e51764aSArtem Bityutskiy  * @old_size: truncation old size
391e51764aSArtem Bityutskiy  * @new_size: truncation new size
401e51764aSArtem Bityutskiy  *
41debf12d5SArtem Bityutskiy  * The replay process first scans all buds and builds the replay list, then
42debf12d5SArtem Bityutskiy  * sorts the replay list in nodes sequence number order, and then inserts all
43debf12d5SArtem Bityutskiy  * the replay entries to the TNC.
441e51764aSArtem Bityutskiy  */
451e51764aSArtem Bityutskiy struct replay_entry {
461e51764aSArtem Bityutskiy 	int lnum;
471e51764aSArtem Bityutskiy 	int offs;
481e51764aSArtem Bityutskiy 	int len;
4916a26b20SSascha Hauer 	u8 hash[UBIFS_HASH_ARR_SZ];
50074bcb9bSArtem Bityutskiy 	unsigned int deletion:1;
511e51764aSArtem Bityutskiy 	unsigned long long sqnum;
52debf12d5SArtem Bityutskiy 	struct list_head list;
531e51764aSArtem Bityutskiy 	union ubifs_key key;
541e51764aSArtem Bityutskiy 	union {
55f4f61d2cSRichard Weinberger 		struct fscrypt_name nm;
561e51764aSArtem Bityutskiy 		struct {
571e51764aSArtem Bityutskiy 			loff_t old_size;
581e51764aSArtem Bityutskiy 			loff_t new_size;
591e51764aSArtem Bityutskiy 		};
601e51764aSArtem Bityutskiy 	};
611e51764aSArtem Bityutskiy };
621e51764aSArtem Bityutskiy 
631e51764aSArtem Bityutskiy /**
641e51764aSArtem Bityutskiy  * struct bud_entry - entry in the list of buds to replay.
651e51764aSArtem Bityutskiy  * @list: next bud in the list
661e51764aSArtem Bityutskiy  * @bud: bud description object
671e51764aSArtem Bityutskiy  * @sqnum: reference node sequence number
68af1dd412SArtem Bityutskiy  * @free: free bytes in the bud
69af1dd412SArtem Bityutskiy  * @dirty: dirty bytes in the bud
701e51764aSArtem Bityutskiy  */
711e51764aSArtem Bityutskiy struct bud_entry {
721e51764aSArtem Bityutskiy 	struct list_head list;
731e51764aSArtem Bityutskiy 	struct ubifs_bud *bud;
741e51764aSArtem Bityutskiy 	unsigned long long sqnum;
75af1dd412SArtem Bityutskiy 	int free;
76af1dd412SArtem Bityutskiy 	int dirty;
771e51764aSArtem Bityutskiy };
781e51764aSArtem Bityutskiy 
791e51764aSArtem Bityutskiy /**
801e51764aSArtem Bityutskiy  * set_bud_lprops - set free and dirty space used by a bud.
811e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
82074bcb9bSArtem Bityutskiy  * @b: bud entry which describes the bud
83074bcb9bSArtem Bityutskiy  *
84074bcb9bSArtem Bityutskiy  * This function makes sure the LEB properties of bud @b are set correctly
85074bcb9bSArtem Bityutskiy  * after the replay. Returns zero in case of success and a negative error code
86074bcb9bSArtem Bityutskiy  * in case of failure.
871e51764aSArtem Bityutskiy  */
88074bcb9bSArtem Bityutskiy static int set_bud_lprops(struct ubifs_info *c, struct bud_entry *b)
891e51764aSArtem Bityutskiy {
901e51764aSArtem Bityutskiy 	const struct ubifs_lprops *lp;
911e51764aSArtem Bityutskiy 	int err = 0, dirty;
921e51764aSArtem Bityutskiy 
931e51764aSArtem Bityutskiy 	ubifs_get_lprops(c);
941e51764aSArtem Bityutskiy 
95074bcb9bSArtem Bityutskiy 	lp = ubifs_lpt_lookup_dirty(c, b->bud->lnum);
961e51764aSArtem Bityutskiy 	if (IS_ERR(lp)) {
971e51764aSArtem Bityutskiy 		err = PTR_ERR(lp);
981e51764aSArtem Bityutskiy 		goto out;
991e51764aSArtem Bityutskiy 	}
1001e51764aSArtem Bityutskiy 
1011e51764aSArtem Bityutskiy 	dirty = lp->dirty;
102074bcb9bSArtem Bityutskiy 	if (b->bud->start == 0 && (lp->free != c->leb_size || lp->dirty != 0)) {
1031e51764aSArtem Bityutskiy 		/*
1041e51764aSArtem Bityutskiy 		 * The LEB was added to the journal with a starting offset of
1051e51764aSArtem Bityutskiy 		 * zero which means the LEB must have been empty. The LEB
106074bcb9bSArtem Bityutskiy 		 * property values should be @lp->free == @c->leb_size and
107074bcb9bSArtem Bityutskiy 		 * @lp->dirty == 0, but that is not the case. The reason is that
1087a9c3e39SArtem Bityutskiy 		 * the LEB had been garbage collected before it became the bud,
1097a9c3e39SArtem Bityutskiy 		 * and there was not commit inbetween. The garbage collector
1107a9c3e39SArtem Bityutskiy 		 * resets the free and dirty space without recording it
1117a9c3e39SArtem Bityutskiy 		 * anywhere except lprops, so if there was no commit then
1127a9c3e39SArtem Bityutskiy 		 * lprops does not have that information.
1131e51764aSArtem Bityutskiy 		 *
1141e51764aSArtem Bityutskiy 		 * We do not need to adjust free space because the scan has told
1151e51764aSArtem Bityutskiy 		 * us the exact value which is recorded in the replay entry as
116074bcb9bSArtem Bityutskiy 		 * @b->free.
1171e51764aSArtem Bityutskiy 		 *
1181e51764aSArtem Bityutskiy 		 * However we do need to subtract from the dirty space the
1191e51764aSArtem Bityutskiy 		 * amount of space that the garbage collector reclaimed, which
1201e51764aSArtem Bityutskiy 		 * is the whole LEB minus the amount of space that was free.
1211e51764aSArtem Bityutskiy 		 */
122074bcb9bSArtem Bityutskiy 		dbg_mnt("bud LEB %d was GC'd (%d free, %d dirty)", b->bud->lnum,
1231e51764aSArtem Bityutskiy 			lp->free, lp->dirty);
124074bcb9bSArtem Bityutskiy 		dbg_gc("bud LEB %d was GC'd (%d free, %d dirty)", b->bud->lnum,
1251e51764aSArtem Bityutskiy 			lp->free, lp->dirty);
1261e51764aSArtem Bityutskiy 		dirty -= c->leb_size - lp->free;
1271e51764aSArtem Bityutskiy 		/*
1281e51764aSArtem Bityutskiy 		 * If the replay order was perfect the dirty space would now be
1297d4e9ccbSArtem Bityutskiy 		 * zero. The order is not perfect because the journal heads
1301e51764aSArtem Bityutskiy 		 * race with each other. This is not a problem but is does mean
1311e51764aSArtem Bityutskiy 		 * that the dirty space may temporarily exceed c->leb_size
1321e51764aSArtem Bityutskiy 		 * during the replay.
1331e51764aSArtem Bityutskiy 		 */
1341e51764aSArtem Bityutskiy 		if (dirty != 0)
1353668b70fSArtem Bityutskiy 			dbg_mnt("LEB %d lp: %d free %d dirty replay: %d free %d dirty",
13679fda517SArtem Bityutskiy 				b->bud->lnum, lp->free, lp->dirty, b->free,
13779fda517SArtem Bityutskiy 				b->dirty);
1381e51764aSArtem Bityutskiy 	}
139074bcb9bSArtem Bityutskiy 	lp = ubifs_change_lp(c, lp, b->free, dirty + b->dirty,
1401e51764aSArtem Bityutskiy 			     lp->flags | LPROPS_TAKEN, 0);
1411e51764aSArtem Bityutskiy 	if (IS_ERR(lp)) {
1421e51764aSArtem Bityutskiy 		err = PTR_ERR(lp);
1431e51764aSArtem Bityutskiy 		goto out;
1441e51764aSArtem Bityutskiy 	}
14552c6e6f9SArtem Bityutskiy 
14652c6e6f9SArtem Bityutskiy 	/* Make sure the journal head points to the latest bud */
147074bcb9bSArtem Bityutskiy 	err = ubifs_wbuf_seek_nolock(&c->jheads[b->bud->jhead].wbuf,
148b36a261eSRichard Weinberger 				     b->bud->lnum, c->leb_size - b->free);
14952c6e6f9SArtem Bityutskiy 
1501e51764aSArtem Bityutskiy out:
1511e51764aSArtem Bityutskiy 	ubifs_release_lprops(c);
1521e51764aSArtem Bityutskiy 	return err;
1531e51764aSArtem Bityutskiy }
1541e51764aSArtem Bityutskiy 
1551e51764aSArtem Bityutskiy /**
156074bcb9bSArtem Bityutskiy  * set_buds_lprops - set free and dirty space for all replayed buds.
157074bcb9bSArtem Bityutskiy  * @c: UBIFS file-system description object
158074bcb9bSArtem Bityutskiy  *
159074bcb9bSArtem Bityutskiy  * This function sets LEB properties for all replayed buds. Returns zero in
160074bcb9bSArtem Bityutskiy  * case of success and a negative error code in case of failure.
161074bcb9bSArtem Bityutskiy  */
162074bcb9bSArtem Bityutskiy static int set_buds_lprops(struct ubifs_info *c)
163074bcb9bSArtem Bityutskiy {
164074bcb9bSArtem Bityutskiy 	struct bud_entry *b;
165074bcb9bSArtem Bityutskiy 	int err;
166074bcb9bSArtem Bityutskiy 
167074bcb9bSArtem Bityutskiy 	list_for_each_entry(b, &c->replay_buds, list) {
168074bcb9bSArtem Bityutskiy 		err = set_bud_lprops(c, b);
169074bcb9bSArtem Bityutskiy 		if (err)
170074bcb9bSArtem Bityutskiy 			return err;
171074bcb9bSArtem Bityutskiy 	}
172074bcb9bSArtem Bityutskiy 
173074bcb9bSArtem Bityutskiy 	return 0;
174074bcb9bSArtem Bityutskiy }
175074bcb9bSArtem Bityutskiy 
176074bcb9bSArtem Bityutskiy /**
1771e51764aSArtem Bityutskiy  * trun_remove_range - apply a replay entry for a truncation to the TNC.
1781e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
1791e51764aSArtem Bityutskiy  * @r: replay entry of truncation
1801e51764aSArtem Bityutskiy  */
1811e51764aSArtem Bityutskiy static int trun_remove_range(struct ubifs_info *c, struct replay_entry *r)
1821e51764aSArtem Bityutskiy {
1831e51764aSArtem Bityutskiy 	unsigned min_blk, max_blk;
1841e51764aSArtem Bityutskiy 	union ubifs_key min_key, max_key;
1851e51764aSArtem Bityutskiy 	ino_t ino;
1861e51764aSArtem Bityutskiy 
1871e51764aSArtem Bityutskiy 	min_blk = r->new_size / UBIFS_BLOCK_SIZE;
1881e51764aSArtem Bityutskiy 	if (r->new_size & (UBIFS_BLOCK_SIZE - 1))
1891e51764aSArtem Bityutskiy 		min_blk += 1;
1901e51764aSArtem Bityutskiy 
1911e51764aSArtem Bityutskiy 	max_blk = r->old_size / UBIFS_BLOCK_SIZE;
1921e51764aSArtem Bityutskiy 	if ((r->old_size & (UBIFS_BLOCK_SIZE - 1)) == 0)
1931e51764aSArtem Bityutskiy 		max_blk -= 1;
1941e51764aSArtem Bityutskiy 
1951e51764aSArtem Bityutskiy 	ino = key_inum(c, &r->key);
1961e51764aSArtem Bityutskiy 
1971e51764aSArtem Bityutskiy 	data_key_init(c, &min_key, ino, min_blk);
1981e51764aSArtem Bityutskiy 	data_key_init(c, &max_key, ino, max_blk);
1991e51764aSArtem Bityutskiy 
2001e51764aSArtem Bityutskiy 	return ubifs_tnc_remove_range(c, &min_key, &max_key);
2011e51764aSArtem Bityutskiy }
2021e51764aSArtem Bityutskiy 
2031e51764aSArtem Bityutskiy /**
204e58725d5SRichard Weinberger  * inode_still_linked - check whether inode in question will be re-linked.
205e58725d5SRichard Weinberger  * @c: UBIFS file-system description object
206e58725d5SRichard Weinberger  * @rino: replay entry to test
207e58725d5SRichard Weinberger  *
208e58725d5SRichard Weinberger  * O_TMPFILE files can be re-linked, this means link count goes from 0 to 1.
209e58725d5SRichard Weinberger  * This case needs special care, otherwise all references to the inode will
210e58725d5SRichard Weinberger  * be removed upon the first replay entry of an inode with link count 0
211e58725d5SRichard Weinberger  * is found.
212e58725d5SRichard Weinberger  */
213e58725d5SRichard Weinberger static bool inode_still_linked(struct ubifs_info *c, struct replay_entry *rino)
214e58725d5SRichard Weinberger {
215e58725d5SRichard Weinberger 	struct replay_entry *r;
216e58725d5SRichard Weinberger 
217e58725d5SRichard Weinberger 	ubifs_assert(c, rino->deletion);
218e58725d5SRichard Weinberger 	ubifs_assert(c, key_type(c, &rino->key) == UBIFS_INO_KEY);
219e58725d5SRichard Weinberger 
220e58725d5SRichard Weinberger 	/*
221e58725d5SRichard Weinberger 	 * Find the most recent entry for the inode behind @rino and check
222e58725d5SRichard Weinberger 	 * whether it is a deletion.
223e58725d5SRichard Weinberger 	 */
224e58725d5SRichard Weinberger 	list_for_each_entry_reverse(r, &c->replay_list, list) {
225e58725d5SRichard Weinberger 		ubifs_assert(c, r->sqnum >= rino->sqnum);
2263e903315SGuochun Mao 		if (key_inum(c, &r->key) == key_inum(c, &rino->key) &&
2273e903315SGuochun Mao 		    key_type(c, &r->key) == UBIFS_INO_KEY)
228e58725d5SRichard Weinberger 			return r->deletion == 0;
229e58725d5SRichard Weinberger 
230e58725d5SRichard Weinberger 	}
231e58725d5SRichard Weinberger 
232e58725d5SRichard Weinberger 	ubifs_assert(c, 0);
233e58725d5SRichard Weinberger 	return false;
234e58725d5SRichard Weinberger }
235e58725d5SRichard Weinberger 
236e58725d5SRichard Weinberger /**
2371e51764aSArtem Bityutskiy  * apply_replay_entry - apply a replay entry to the TNC.
2381e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
2391e51764aSArtem Bityutskiy  * @r: replay entry to apply
2401e51764aSArtem Bityutskiy  *
2411e51764aSArtem Bityutskiy  * Apply a replay entry to the TNC.
2421e51764aSArtem Bityutskiy  */
2431e51764aSArtem Bityutskiy static int apply_replay_entry(struct ubifs_info *c, struct replay_entry *r)
2441e51764aSArtem Bityutskiy {
245074bcb9bSArtem Bityutskiy 	int err;
2461e51764aSArtem Bityutskiy 
247515315a1SArtem Bityutskiy 	dbg_mntk(&r->key, "LEB %d:%d len %d deletion %d sqnum %llu key ",
248515315a1SArtem Bityutskiy 		 r->lnum, r->offs, r->len, r->deletion, r->sqnum);
2491e51764aSArtem Bityutskiy 
250074bcb9bSArtem Bityutskiy 	if (is_hash_key(c, &r->key)) {
251074bcb9bSArtem Bityutskiy 		if (r->deletion)
2521e51764aSArtem Bityutskiy 			err = ubifs_tnc_remove_nm(c, &r->key, &r->nm);
2531e51764aSArtem Bityutskiy 		else
2541e51764aSArtem Bityutskiy 			err = ubifs_tnc_add_nm(c, &r->key, r->lnum, r->offs,
25516a26b20SSascha Hauer 					       r->len, r->hash, &r->nm);
2561e51764aSArtem Bityutskiy 	} else {
257074bcb9bSArtem Bityutskiy 		if (r->deletion)
2581e51764aSArtem Bityutskiy 			switch (key_type(c, &r->key)) {
2591e51764aSArtem Bityutskiy 			case UBIFS_INO_KEY:
2601e51764aSArtem Bityutskiy 			{
2611e51764aSArtem Bityutskiy 				ino_t inum = key_inum(c, &r->key);
2621e51764aSArtem Bityutskiy 
263e58725d5SRichard Weinberger 				if (inode_still_linked(c, r)) {
264e58725d5SRichard Weinberger 					err = 0;
265e58725d5SRichard Weinberger 					break;
266e58725d5SRichard Weinberger 				}
267e58725d5SRichard Weinberger 
2681e51764aSArtem Bityutskiy 				err = ubifs_tnc_remove_ino(c, inum);
2691e51764aSArtem Bityutskiy 				break;
2701e51764aSArtem Bityutskiy 			}
2711e51764aSArtem Bityutskiy 			case UBIFS_TRUN_KEY:
2721e51764aSArtem Bityutskiy 				err = trun_remove_range(c, r);
2731e51764aSArtem Bityutskiy 				break;
2741e51764aSArtem Bityutskiy 			default:
2751e51764aSArtem Bityutskiy 				err = ubifs_tnc_remove(c, &r->key);
2761e51764aSArtem Bityutskiy 				break;
2771e51764aSArtem Bityutskiy 			}
2781e51764aSArtem Bityutskiy 		else
2791e51764aSArtem Bityutskiy 			err = ubifs_tnc_add(c, &r->key, r->lnum, r->offs,
28016a26b20SSascha Hauer 					    r->len, r->hash);
2811e51764aSArtem Bityutskiy 		if (err)
2821e51764aSArtem Bityutskiy 			return err;
2831e51764aSArtem Bityutskiy 
2841e51764aSArtem Bityutskiy 		if (c->need_recovery)
285074bcb9bSArtem Bityutskiy 			err = ubifs_recover_size_accum(c, &r->key, r->deletion,
2861e51764aSArtem Bityutskiy 						       r->new_size);
2871e51764aSArtem Bityutskiy 	}
2881e51764aSArtem Bityutskiy 
2891e51764aSArtem Bityutskiy 	return err;
2901e51764aSArtem Bityutskiy }
2911e51764aSArtem Bityutskiy 
2921e51764aSArtem Bityutskiy /**
293debf12d5SArtem Bityutskiy  * replay_entries_cmp - compare 2 replay entries.
294debf12d5SArtem Bityutskiy  * @priv: UBIFS file-system description object
295debf12d5SArtem Bityutskiy  * @a: first replay entry
296ec037dfcSJulia Lawall  * @b: second replay entry
2971e51764aSArtem Bityutskiy  *
298debf12d5SArtem Bityutskiy  * This is a comparios function for 'list_sort()' which compares 2 replay
299*07c32de4SZheng Yongjun  * entries @a and @b by comparing their sequence number.  Returns %1 if @a has
300debf12d5SArtem Bityutskiy  * greater sequence number and %-1 otherwise.
3011e51764aSArtem Bityutskiy  */
3024f0f586bSSami Tolvanen static int replay_entries_cmp(void *priv, const struct list_head *a,
3034f0f586bSSami Tolvanen 			      const struct list_head *b)
3041e51764aSArtem Bityutskiy {
3056eb61d58SRichard Weinberger 	struct ubifs_info *c = priv;
306debf12d5SArtem Bityutskiy 	struct replay_entry *ra, *rb;
3071e51764aSArtem Bityutskiy 
308debf12d5SArtem Bityutskiy 	cond_resched();
309debf12d5SArtem Bityutskiy 	if (a == b)
310debf12d5SArtem Bityutskiy 		return 0;
311debf12d5SArtem Bityutskiy 
312debf12d5SArtem Bityutskiy 	ra = list_entry(a, struct replay_entry, list);
313debf12d5SArtem Bityutskiy 	rb = list_entry(b, struct replay_entry, list);
3146eb61d58SRichard Weinberger 	ubifs_assert(c, ra->sqnum != rb->sqnum);
315debf12d5SArtem Bityutskiy 	if (ra->sqnum > rb->sqnum)
316debf12d5SArtem Bityutskiy 		return 1;
317debf12d5SArtem Bityutskiy 	return -1;
3181e51764aSArtem Bityutskiy }
3191e51764aSArtem Bityutskiy 
3201e51764aSArtem Bityutskiy /**
321debf12d5SArtem Bityutskiy  * apply_replay_list - apply the replay list to the TNC.
3221e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
3231e51764aSArtem Bityutskiy  *
324debf12d5SArtem Bityutskiy  * Apply all entries in the replay list to the TNC. Returns zero in case of
325debf12d5SArtem Bityutskiy  * success and a negative error code in case of failure.
3261e51764aSArtem Bityutskiy  */
327debf12d5SArtem Bityutskiy static int apply_replay_list(struct ubifs_info *c)
3281e51764aSArtem Bityutskiy {
3291e51764aSArtem Bityutskiy 	struct replay_entry *r;
3301e51764aSArtem Bityutskiy 	int err;
3311e51764aSArtem Bityutskiy 
332debf12d5SArtem Bityutskiy 	list_sort(c, &c->replay_list, &replay_entries_cmp);
333debf12d5SArtem Bityutskiy 
334debf12d5SArtem Bityutskiy 	list_for_each_entry(r, &c->replay_list, list) {
3351e51764aSArtem Bityutskiy 		cond_resched();
3361e51764aSArtem Bityutskiy 
3371e51764aSArtem Bityutskiy 		err = apply_replay_entry(c, r);
3381e51764aSArtem Bityutskiy 		if (err)
3391e51764aSArtem Bityutskiy 			return err;
3401e51764aSArtem Bityutskiy 	}
341debf12d5SArtem Bityutskiy 
3421e51764aSArtem Bityutskiy 	return 0;
3431e51764aSArtem Bityutskiy }
3441e51764aSArtem Bityutskiy 
3451e51764aSArtem Bityutskiy /**
346debf12d5SArtem Bityutskiy  * destroy_replay_list - destroy the replay.
347debf12d5SArtem Bityutskiy  * @c: UBIFS file-system description object
348debf12d5SArtem Bityutskiy  *
349debf12d5SArtem Bityutskiy  * Destroy the replay list.
350debf12d5SArtem Bityutskiy  */
351debf12d5SArtem Bityutskiy static void destroy_replay_list(struct ubifs_info *c)
352debf12d5SArtem Bityutskiy {
353debf12d5SArtem Bityutskiy 	struct replay_entry *r, *tmp;
354debf12d5SArtem Bityutskiy 
355debf12d5SArtem Bityutskiy 	list_for_each_entry_safe(r, tmp, &c->replay_list, list) {
356debf12d5SArtem Bityutskiy 		if (is_hash_key(c, &r->key))
357f4f61d2cSRichard Weinberger 			kfree(fname_name(&r->nm));
358debf12d5SArtem Bityutskiy 		list_del(&r->list);
359debf12d5SArtem Bityutskiy 		kfree(r);
360debf12d5SArtem Bityutskiy 	}
361debf12d5SArtem Bityutskiy }
362debf12d5SArtem Bityutskiy 
363debf12d5SArtem Bityutskiy /**
364debf12d5SArtem Bityutskiy  * insert_node - insert a node to the replay list
3651e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
3661e51764aSArtem Bityutskiy  * @lnum: node logical eraseblock number
3671e51764aSArtem Bityutskiy  * @offs: node offset
3681e51764aSArtem Bityutskiy  * @len: node length
3691e51764aSArtem Bityutskiy  * @key: node key
3701e51764aSArtem Bityutskiy  * @sqnum: sequence number
3711e51764aSArtem Bityutskiy  * @deletion: non-zero if this is a deletion
3721e51764aSArtem Bityutskiy  * @used: number of bytes in use in a LEB
3731e51764aSArtem Bityutskiy  * @old_size: truncation old size
3741e51764aSArtem Bityutskiy  * @new_size: truncation new size
3751e51764aSArtem Bityutskiy  *
376debf12d5SArtem Bityutskiy  * This function inserts a scanned non-direntry node to the replay list. The
377debf12d5SArtem Bityutskiy  * replay list contains @struct replay_entry elements, and we sort this list in
378debf12d5SArtem Bityutskiy  * sequence number order before applying it. The replay list is applied at the
379debf12d5SArtem Bityutskiy  * very end of the replay process. Since the list is sorted in sequence number
380debf12d5SArtem Bityutskiy  * order, the older modifications are applied first. This function returns zero
381debf12d5SArtem Bityutskiy  * in case of success and a negative error code in case of failure.
3821e51764aSArtem Bityutskiy  */
3831e51764aSArtem Bityutskiy static int insert_node(struct ubifs_info *c, int lnum, int offs, int len,
38416a26b20SSascha Hauer 		       const u8 *hash, union ubifs_key *key,
38516a26b20SSascha Hauer 		       unsigned long long sqnum, int deletion, int *used,
38616a26b20SSascha Hauer 		       loff_t old_size, loff_t new_size)
3871e51764aSArtem Bityutskiy {
3881e51764aSArtem Bityutskiy 	struct replay_entry *r;
3891e51764aSArtem Bityutskiy 
390515315a1SArtem Bityutskiy 	dbg_mntk(key, "add LEB %d:%d, key ", lnum, offs);
391debf12d5SArtem Bityutskiy 
3921e51764aSArtem Bityutskiy 	if (key_inum(c, key) >= c->highest_inum)
3931e51764aSArtem Bityutskiy 		c->highest_inum = key_inum(c, key);
3941e51764aSArtem Bityutskiy 
3951e51764aSArtem Bityutskiy 	r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL);
3961e51764aSArtem Bityutskiy 	if (!r)
3971e51764aSArtem Bityutskiy 		return -ENOMEM;
3981e51764aSArtem Bityutskiy 
3991e51764aSArtem Bityutskiy 	if (!deletion)
4001e51764aSArtem Bityutskiy 		*used += ALIGN(len, 8);
4011e51764aSArtem Bityutskiy 	r->lnum = lnum;
4021e51764aSArtem Bityutskiy 	r->offs = offs;
4031e51764aSArtem Bityutskiy 	r->len = len;
40416a26b20SSascha Hauer 	ubifs_copy_hash(c, hash, r->hash);
405074bcb9bSArtem Bityutskiy 	r->deletion = !!deletion;
4061e51764aSArtem Bityutskiy 	r->sqnum = sqnum;
407074bcb9bSArtem Bityutskiy 	key_copy(c, key, &r->key);
4081e51764aSArtem Bityutskiy 	r->old_size = old_size;
4091e51764aSArtem Bityutskiy 	r->new_size = new_size;
4101e51764aSArtem Bityutskiy 
411debf12d5SArtem Bityutskiy 	list_add_tail(&r->list, &c->replay_list);
4121e51764aSArtem Bityutskiy 	return 0;
4131e51764aSArtem Bityutskiy }
4141e51764aSArtem Bityutskiy 
4151e51764aSArtem Bityutskiy /**
416debf12d5SArtem Bityutskiy  * insert_dent - insert a directory entry node into the replay list.
4171e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
4181e51764aSArtem Bityutskiy  * @lnum: node logical eraseblock number
4191e51764aSArtem Bityutskiy  * @offs: node offset
4201e51764aSArtem Bityutskiy  * @len: node length
4211e51764aSArtem Bityutskiy  * @key: node key
4221e51764aSArtem Bityutskiy  * @name: directory entry name
4231e51764aSArtem Bityutskiy  * @nlen: directory entry name length
4241e51764aSArtem Bityutskiy  * @sqnum: sequence number
4251e51764aSArtem Bityutskiy  * @deletion: non-zero if this is a deletion
4261e51764aSArtem Bityutskiy  * @used: number of bytes in use in a LEB
4271e51764aSArtem Bityutskiy  *
428debf12d5SArtem Bityutskiy  * This function inserts a scanned directory entry node or an extended
429debf12d5SArtem Bityutskiy  * attribute entry to the replay list. Returns zero in case of success and a
430debf12d5SArtem Bityutskiy  * negative error code in case of failure.
4311e51764aSArtem Bityutskiy  */
4321e51764aSArtem Bityutskiy static int insert_dent(struct ubifs_info *c, int lnum, int offs, int len,
43316a26b20SSascha Hauer 		       const u8 *hash, union ubifs_key *key,
43416a26b20SSascha Hauer 		       const char *name, int nlen, unsigned long long sqnum,
43516a26b20SSascha Hauer 		       int deletion, int *used)
4361e51764aSArtem Bityutskiy {
4371e51764aSArtem Bityutskiy 	struct replay_entry *r;
4381e51764aSArtem Bityutskiy 	char *nbuf;
4391e51764aSArtem Bityutskiy 
440515315a1SArtem Bityutskiy 	dbg_mntk(key, "add LEB %d:%d, key ", lnum, offs);
4411e51764aSArtem Bityutskiy 	if (key_inum(c, key) >= c->highest_inum)
4421e51764aSArtem Bityutskiy 		c->highest_inum = key_inum(c, key);
4431e51764aSArtem Bityutskiy 
4441e51764aSArtem Bityutskiy 	r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL);
4451e51764aSArtem Bityutskiy 	if (!r)
4461e51764aSArtem Bityutskiy 		return -ENOMEM;
447debf12d5SArtem Bityutskiy 
4481e51764aSArtem Bityutskiy 	nbuf = kmalloc(nlen + 1, GFP_KERNEL);
4491e51764aSArtem Bityutskiy 	if (!nbuf) {
4501e51764aSArtem Bityutskiy 		kfree(r);
4511e51764aSArtem Bityutskiy 		return -ENOMEM;
4521e51764aSArtem Bityutskiy 	}
4531e51764aSArtem Bityutskiy 
4541e51764aSArtem Bityutskiy 	if (!deletion)
4551e51764aSArtem Bityutskiy 		*used += ALIGN(len, 8);
4561e51764aSArtem Bityutskiy 	r->lnum = lnum;
4571e51764aSArtem Bityutskiy 	r->offs = offs;
4581e51764aSArtem Bityutskiy 	r->len = len;
45916a26b20SSascha Hauer 	ubifs_copy_hash(c, hash, r->hash);
460074bcb9bSArtem Bityutskiy 	r->deletion = !!deletion;
4611e51764aSArtem Bityutskiy 	r->sqnum = sqnum;
462074bcb9bSArtem Bityutskiy 	key_copy(c, key, &r->key);
463f4f61d2cSRichard Weinberger 	fname_len(&r->nm) = nlen;
4641e51764aSArtem Bityutskiy 	memcpy(nbuf, name, nlen);
4651e51764aSArtem Bityutskiy 	nbuf[nlen] = '\0';
466f4f61d2cSRichard Weinberger 	fname_name(&r->nm) = nbuf;
4671e51764aSArtem Bityutskiy 
468debf12d5SArtem Bityutskiy 	list_add_tail(&r->list, &c->replay_list);
4691e51764aSArtem Bityutskiy 	return 0;
4701e51764aSArtem Bityutskiy }
4711e51764aSArtem Bityutskiy 
4721e51764aSArtem Bityutskiy /**
4731e51764aSArtem Bityutskiy  * ubifs_validate_entry - validate directory or extended attribute entry node.
4741e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
4751e51764aSArtem Bityutskiy  * @dent: the node to validate
4761e51764aSArtem Bityutskiy  *
4771e51764aSArtem Bityutskiy  * This function validates directory or extended attribute entry node @dent.
4781e51764aSArtem Bityutskiy  * Returns zero if the node is all right and a %-EINVAL if not.
4791e51764aSArtem Bityutskiy  */
4801e51764aSArtem Bityutskiy int ubifs_validate_entry(struct ubifs_info *c,
4811e51764aSArtem Bityutskiy 			 const struct ubifs_dent_node *dent)
4821e51764aSArtem Bityutskiy {
4831e51764aSArtem Bityutskiy 	int key_type = key_type_flash(c, dent->key);
4841e51764aSArtem Bityutskiy 	int nlen = le16_to_cpu(dent->nlen);
4851e51764aSArtem Bityutskiy 
4861e51764aSArtem Bityutskiy 	if (le32_to_cpu(dent->ch.len) != nlen + UBIFS_DENT_NODE_SZ + 1 ||
4871e51764aSArtem Bityutskiy 	    dent->type >= UBIFS_ITYPES_CNT ||
4881e51764aSArtem Bityutskiy 	    nlen > UBIFS_MAX_NLEN || dent->name[nlen] != 0 ||
489304790c0SRichard Weinberger 	    (key_type == UBIFS_XENT_KEY && strnlen(dent->name, nlen) != nlen) ||
4901e51764aSArtem Bityutskiy 	    le64_to_cpu(dent->inum) > MAX_INUM) {
491235c362bSSheng Yong 		ubifs_err(c, "bad %s node", key_type == UBIFS_DENT_KEY ?
4921e51764aSArtem Bityutskiy 			  "directory entry" : "extended attribute entry");
4931e51764aSArtem Bityutskiy 		return -EINVAL;
4941e51764aSArtem Bityutskiy 	}
4951e51764aSArtem Bityutskiy 
4961e51764aSArtem Bityutskiy 	if (key_type != UBIFS_DENT_KEY && key_type != UBIFS_XENT_KEY) {
497235c362bSSheng Yong 		ubifs_err(c, "bad key type %d", key_type);
4981e51764aSArtem Bityutskiy 		return -EINVAL;
4991e51764aSArtem Bityutskiy 	}
5001e51764aSArtem Bityutskiy 
5011e51764aSArtem Bityutskiy 	return 0;
5021e51764aSArtem Bityutskiy }
5031e51764aSArtem Bityutskiy 
5041e51764aSArtem Bityutskiy /**
50591c66083SArtem Bityutskiy  * is_last_bud - check if the bud is the last in the journal head.
50691c66083SArtem Bityutskiy  * @c: UBIFS file-system description object
50791c66083SArtem Bityutskiy  * @bud: bud description object
50891c66083SArtem Bityutskiy  *
50991c66083SArtem Bityutskiy  * This function checks if bud @bud is the last bud in its journal head. This
51091c66083SArtem Bityutskiy  * information is then used by 'replay_bud()' to decide whether the bud can
51191c66083SArtem Bityutskiy  * have corruptions or not. Indeed, only last buds can be corrupted by power
51291c66083SArtem Bityutskiy  * cuts. Returns %1 if this is the last bud, and %0 if not.
51391c66083SArtem Bityutskiy  */
51491c66083SArtem Bityutskiy static int is_last_bud(struct ubifs_info *c, struct ubifs_bud *bud)
51591c66083SArtem Bityutskiy {
51691c66083SArtem Bityutskiy 	struct ubifs_jhead *jh = &c->jheads[bud->jhead];
51791c66083SArtem Bityutskiy 	struct ubifs_bud *next;
51891c66083SArtem Bityutskiy 	uint32_t data;
51991c66083SArtem Bityutskiy 	int err;
52091c66083SArtem Bityutskiy 
52191c66083SArtem Bityutskiy 	if (list_is_last(&bud->list, &jh->buds_list))
52291c66083SArtem Bityutskiy 		return 1;
52391c66083SArtem Bityutskiy 
52491c66083SArtem Bityutskiy 	/*
52591c66083SArtem Bityutskiy 	 * The following is a quirk to make sure we work correctly with UBIFS
52691c66083SArtem Bityutskiy 	 * images used with older UBIFS.
52791c66083SArtem Bityutskiy 	 *
52891c66083SArtem Bityutskiy 	 * Normally, the last bud will be the last in the journal head's list
52991c66083SArtem Bityutskiy 	 * of bud. However, there is one exception if the UBIFS image belongs
53091c66083SArtem Bityutskiy 	 * to older UBIFS. This is fairly unlikely: one would need to use old
53191c66083SArtem Bityutskiy 	 * UBIFS, then have a power cut exactly at the right point, and then
53291c66083SArtem Bityutskiy 	 * try to mount this image with new UBIFS.
53391c66083SArtem Bityutskiy 	 *
53491c66083SArtem Bityutskiy 	 * The exception is: it is possible to have 2 buds A and B, A goes
53591c66083SArtem Bityutskiy 	 * before B, and B is the last, bud B is contains no data, and bud A is
53691c66083SArtem Bityutskiy 	 * corrupted at the end. The reason is that in older versions when the
53791c66083SArtem Bityutskiy 	 * journal code switched the next bud (from A to B), it first added a
53891c66083SArtem Bityutskiy 	 * log reference node for the new bud (B), and only after this it
53991c66083SArtem Bityutskiy 	 * synchronized the write-buffer of current bud (A). But later this was
54091c66083SArtem Bityutskiy 	 * changed and UBIFS started to always synchronize the write-buffer of
54191c66083SArtem Bityutskiy 	 * the bud (A) before writing the log reference for the new bud (B).
54291c66083SArtem Bityutskiy 	 *
54391c66083SArtem Bityutskiy 	 * But because older UBIFS always synchronized A's write-buffer before
54491c66083SArtem Bityutskiy 	 * writing to B, we can recognize this exceptional situation but
54591c66083SArtem Bityutskiy 	 * checking the contents of bud B - if it is empty, then A can be
54691c66083SArtem Bityutskiy 	 * treated as the last and we can recover it.
54791c66083SArtem Bityutskiy 	 *
54891c66083SArtem Bityutskiy 	 * TODO: remove this piece of code in a couple of years (today it is
54991c66083SArtem Bityutskiy 	 * 16.05.2011).
55091c66083SArtem Bityutskiy 	 */
55191c66083SArtem Bityutskiy 	next = list_entry(bud->list.next, struct ubifs_bud, list);
55291c66083SArtem Bityutskiy 	if (!list_is_last(&next->list, &jh->buds_list))
55391c66083SArtem Bityutskiy 		return 0;
55491c66083SArtem Bityutskiy 
555d304820aSArtem Bityutskiy 	err = ubifs_leb_read(c, next->lnum, (char *)&data, next->start, 4, 1);
55691c66083SArtem Bityutskiy 	if (err)
55791c66083SArtem Bityutskiy 		return 0;
55891c66083SArtem Bityutskiy 
55991c66083SArtem Bityutskiy 	return data == 0xFFFFFFFF;
56091c66083SArtem Bityutskiy }
56191c66083SArtem Bityutskiy 
562f80df385SEric Biggers /* authenticate_sleb_hash is split out for stack usage */
563410b6de7SArnd Bergmann static int noinline_for_stack
564410b6de7SArnd Bergmann authenticate_sleb_hash(struct ubifs_info *c,
565410b6de7SArnd Bergmann 		       struct shash_desc *log_hash, u8 *hash)
566eb66eff6SArnd Bergmann {
567eb66eff6SArnd Bergmann 	SHASH_DESC_ON_STACK(hash_desc, c->hash_tfm);
568eb66eff6SArnd Bergmann 
569eb66eff6SArnd Bergmann 	hash_desc->tfm = c->hash_tfm;
570eb66eff6SArnd Bergmann 
571eb66eff6SArnd Bergmann 	ubifs_shash_copy_state(c, log_hash, hash_desc);
572eb66eff6SArnd Bergmann 	return crypto_shash_final(hash_desc, hash);
573eb66eff6SArnd Bergmann }
574eb66eff6SArnd Bergmann 
57591c66083SArtem Bityutskiy /**
576da8ef65fSSascha Hauer  * authenticate_sleb - authenticate one scan LEB
577da8ef65fSSascha Hauer  * @c: UBIFS file-system description object
578da8ef65fSSascha Hauer  * @sleb: the scan LEB to authenticate
579da8ef65fSSascha Hauer  * @log_hash:
580b8f1da98SRandy Dunlap  * @is_last: if true, this is the last LEB
581da8ef65fSSascha Hauer  *
582da8ef65fSSascha Hauer  * This function iterates over the buds of a single LEB authenticating all buds
583da8ef65fSSascha Hauer  * with the authentication nodes on this LEB. Authentication nodes are written
584da8ef65fSSascha Hauer  * after some buds and contain a HMAC covering the authentication node itself
585da8ef65fSSascha Hauer  * and the buds between the last authentication node and the current
586da8ef65fSSascha Hauer  * authentication node. It can happen that the last buds cannot be authenticated
587da8ef65fSSascha Hauer  * because a powercut happened when some nodes were written but not the
588da8ef65fSSascha Hauer  * corresponding authentication node. This function returns the number of nodes
589da8ef65fSSascha Hauer  * that could be authenticated or a negative error code.
590da8ef65fSSascha Hauer  */
591da8ef65fSSascha Hauer static int authenticate_sleb(struct ubifs_info *c, struct ubifs_scan_leb *sleb,
592da8ef65fSSascha Hauer 			     struct shash_desc *log_hash, int is_last)
593da8ef65fSSascha Hauer {
594da8ef65fSSascha Hauer 	int n_not_auth = 0;
595da8ef65fSSascha Hauer 	struct ubifs_scan_node *snod;
596da8ef65fSSascha Hauer 	int n_nodes = 0;
597da8ef65fSSascha Hauer 	int err;
5983c3c32f8SEric Biggers 	u8 hash[UBIFS_HASH_ARR_SZ];
5993c3c32f8SEric Biggers 	u8 hmac[UBIFS_HMAC_ARR_SZ];
600da8ef65fSSascha Hauer 
601da8ef65fSSascha Hauer 	if (!ubifs_authenticated(c))
602da8ef65fSSascha Hauer 		return sleb->nodes_cnt;
603da8ef65fSSascha Hauer 
604da8ef65fSSascha Hauer 	list_for_each_entry(snod, &sleb->nodes, list) {
605da8ef65fSSascha Hauer 
606da8ef65fSSascha Hauer 		n_nodes++;
607da8ef65fSSascha Hauer 
608da8ef65fSSascha Hauer 		if (snod->type == UBIFS_AUTH_NODE) {
609da8ef65fSSascha Hauer 			struct ubifs_auth_node *auth = snod->node;
610da8ef65fSSascha Hauer 
611eb66eff6SArnd Bergmann 			err = authenticate_sleb_hash(c, log_hash, hash);
612da8ef65fSSascha Hauer 			if (err)
613da8ef65fSSascha Hauer 				goto out;
614da8ef65fSSascha Hauer 
615f80df385SEric Biggers 			err = crypto_shash_tfm_digest(c->hmac_tfm, hash,
616f80df385SEric Biggers 						      c->hash_len, hmac);
617da8ef65fSSascha Hauer 			if (err)
618da8ef65fSSascha Hauer 				goto out;
619da8ef65fSSascha Hauer 
620da8ef65fSSascha Hauer 			err = ubifs_check_hmac(c, auth->hmac, hmac);
621da8ef65fSSascha Hauer 			if (err) {
622da8ef65fSSascha Hauer 				err = -EPERM;
623da8ef65fSSascha Hauer 				goto out;
624da8ef65fSSascha Hauer 			}
625da8ef65fSSascha Hauer 			n_not_auth = 0;
626da8ef65fSSascha Hauer 		} else {
627da8ef65fSSascha Hauer 			err = crypto_shash_update(log_hash, snod->node,
628da8ef65fSSascha Hauer 						  snod->len);
629da8ef65fSSascha Hauer 			if (err)
630da8ef65fSSascha Hauer 				goto out;
631da8ef65fSSascha Hauer 			n_not_auth++;
632da8ef65fSSascha Hauer 		}
633da8ef65fSSascha Hauer 	}
634da8ef65fSSascha Hauer 
635da8ef65fSSascha Hauer 	/*
636da8ef65fSSascha Hauer 	 * A powercut can happen when some nodes were written, but not yet
637da8ef65fSSascha Hauer 	 * the corresponding authentication node. This may only happen on
638da8ef65fSSascha Hauer 	 * the last bud though.
639da8ef65fSSascha Hauer 	 */
640da8ef65fSSascha Hauer 	if (n_not_auth) {
641da8ef65fSSascha Hauer 		if (is_last) {
642da8ef65fSSascha Hauer 			dbg_mnt("%d unauthenticated nodes found on LEB %d, Ignoring them",
643da8ef65fSSascha Hauer 				n_not_auth, sleb->lnum);
644da8ef65fSSascha Hauer 			err = 0;
645da8ef65fSSascha Hauer 		} else {
646da8ef65fSSascha Hauer 			dbg_mnt("%d unauthenticated nodes found on non-last LEB %d",
647da8ef65fSSascha Hauer 				n_not_auth, sleb->lnum);
648da8ef65fSSascha Hauer 			err = -EPERM;
649da8ef65fSSascha Hauer 		}
650da8ef65fSSascha Hauer 	} else {
651da8ef65fSSascha Hauer 		err = 0;
652da8ef65fSSascha Hauer 	}
653da8ef65fSSascha Hauer out:
654da8ef65fSSascha Hauer 	return err ? err : n_nodes - n_not_auth;
655da8ef65fSSascha Hauer }
656da8ef65fSSascha Hauer 
657da8ef65fSSascha Hauer /**
6581e51764aSArtem Bityutskiy  * replay_bud - replay a bud logical eraseblock.
6591e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
660e76a4526SArtem Bityutskiy  * @b: bud entry which describes the bud
6611e51764aSArtem Bityutskiy  *
662e76a4526SArtem Bityutskiy  * This function replays bud @bud, recovers it if needed, and adds all nodes
663e76a4526SArtem Bityutskiy  * from this bud to the replay list. Returns zero in case of success and a
664e76a4526SArtem Bityutskiy  * negative error code in case of failure.
6651e51764aSArtem Bityutskiy  */
666e76a4526SArtem Bityutskiy static int replay_bud(struct ubifs_info *c, struct bud_entry *b)
6671e51764aSArtem Bityutskiy {
66891c66083SArtem Bityutskiy 	int is_last = is_last_bud(c, b->bud);
669e76a4526SArtem Bityutskiy 	int err = 0, used = 0, lnum = b->bud->lnum, offs = b->bud->start;
670da8ef65fSSascha Hauer 	int n_nodes, n = 0;
6711e51764aSArtem Bityutskiy 	struct ubifs_scan_leb *sleb;
6721e51764aSArtem Bityutskiy 	struct ubifs_scan_node *snod;
6731e51764aSArtem Bityutskiy 
67491c66083SArtem Bityutskiy 	dbg_mnt("replay bud LEB %d, head %d, offs %d, is_last %d",
67591c66083SArtem Bityutskiy 		lnum, b->bud->jhead, offs, is_last);
676e76a4526SArtem Bityutskiy 
67791c66083SArtem Bityutskiy 	if (c->need_recovery && is_last)
67891c66083SArtem Bityutskiy 		/*
67991c66083SArtem Bityutskiy 		 * Recover only last LEBs in the journal heads, because power
68091c66083SArtem Bityutskiy 		 * cuts may cause corruptions only in these LEBs, because only
68191c66083SArtem Bityutskiy 		 * these LEBs could possibly be written to at the power cut
68291c66083SArtem Bityutskiy 		 * time.
68391c66083SArtem Bityutskiy 		 */
684efcfde54SArtem Bityutskiy 		sleb = ubifs_recover_leb(c, lnum, offs, c->sbuf, b->bud->jhead);
6851e51764aSArtem Bityutskiy 	else
686348709baSArtem Bityutskiy 		sleb = ubifs_scan(c, lnum, offs, c->sbuf, 0);
6871e51764aSArtem Bityutskiy 	if (IS_ERR(sleb))
6881e51764aSArtem Bityutskiy 		return PTR_ERR(sleb);
6891e51764aSArtem Bityutskiy 
690da8ef65fSSascha Hauer 	n_nodes = authenticate_sleb(c, sleb, b->bud->log_hash, is_last);
691da8ef65fSSascha Hauer 	if (n_nodes < 0) {
692da8ef65fSSascha Hauer 		err = n_nodes;
693da8ef65fSSascha Hauer 		goto out;
694da8ef65fSSascha Hauer 	}
695da8ef65fSSascha Hauer 
696da8ef65fSSascha Hauer 	ubifs_shash_copy_state(c, b->bud->log_hash,
697da8ef65fSSascha Hauer 			       c->jheads[b->bud->jhead].log_hash);
698da8ef65fSSascha Hauer 
6991e51764aSArtem Bityutskiy 	/*
7001e51764aSArtem Bityutskiy 	 * The bud does not have to start from offset zero - the beginning of
7011e51764aSArtem Bityutskiy 	 * the 'lnum' LEB may contain previously committed data. One of the
7021e51764aSArtem Bityutskiy 	 * things we have to do in replay is to correctly update lprops with
7031e51764aSArtem Bityutskiy 	 * newer information about this LEB.
7041e51764aSArtem Bityutskiy 	 *
7051e51764aSArtem Bityutskiy 	 * At this point lprops thinks that this LEB has 'c->leb_size - offs'
7061e51764aSArtem Bityutskiy 	 * bytes of free space because it only contain information about
7071e51764aSArtem Bityutskiy 	 * committed data.
7081e51764aSArtem Bityutskiy 	 *
7091e51764aSArtem Bityutskiy 	 * But we know that real amount of free space is 'c->leb_size -
7101e51764aSArtem Bityutskiy 	 * sleb->endpt', and the space in the 'lnum' LEB between 'offs' and
7111e51764aSArtem Bityutskiy 	 * 'sleb->endpt' is used by bud data. We have to correctly calculate
7121e51764aSArtem Bityutskiy 	 * how much of these data are dirty and update lprops with this
7131e51764aSArtem Bityutskiy 	 * information.
7141e51764aSArtem Bityutskiy 	 *
7151e51764aSArtem Bityutskiy 	 * The dirt in that LEB region is comprised of padding nodes, deletion
7161e51764aSArtem Bityutskiy 	 * nodes, truncation nodes and nodes which are obsoleted by subsequent
7171e51764aSArtem Bityutskiy 	 * nodes in this LEB. So instead of calculating clean space, we
7181e51764aSArtem Bityutskiy 	 * calculate used space ('used' variable).
7191e51764aSArtem Bityutskiy 	 */
7201e51764aSArtem Bityutskiy 
7211e51764aSArtem Bityutskiy 	list_for_each_entry(snod, &sleb->nodes, list) {
72216a26b20SSascha Hauer 		u8 hash[UBIFS_HASH_ARR_SZ];
7231e51764aSArtem Bityutskiy 		int deletion = 0;
7241e51764aSArtem Bityutskiy 
7251e51764aSArtem Bityutskiy 		cond_resched();
7261e51764aSArtem Bityutskiy 
7271e51764aSArtem Bityutskiy 		if (snod->sqnum >= SQNUM_WATERMARK) {
728235c362bSSheng Yong 			ubifs_err(c, "file system's life ended");
7291e51764aSArtem Bityutskiy 			goto out_dump;
7301e51764aSArtem Bityutskiy 		}
7311e51764aSArtem Bityutskiy 
73216a26b20SSascha Hauer 		ubifs_node_calc_hash(c, snod->node, hash);
73316a26b20SSascha Hauer 
7341e51764aSArtem Bityutskiy 		if (snod->sqnum > c->max_sqnum)
7351e51764aSArtem Bityutskiy 			c->max_sqnum = snod->sqnum;
7361e51764aSArtem Bityutskiy 
7371e51764aSArtem Bityutskiy 		switch (snod->type) {
7381e51764aSArtem Bityutskiy 		case UBIFS_INO_NODE:
7391e51764aSArtem Bityutskiy 		{
7401e51764aSArtem Bityutskiy 			struct ubifs_ino_node *ino = snod->node;
7411e51764aSArtem Bityutskiy 			loff_t new_size = le64_to_cpu(ino->size);
7421e51764aSArtem Bityutskiy 
7431e51764aSArtem Bityutskiy 			if (le32_to_cpu(ino->nlink) == 0)
7441e51764aSArtem Bityutskiy 				deletion = 1;
74516a26b20SSascha Hauer 			err = insert_node(c, lnum, snod->offs, snod->len, hash,
7461e51764aSArtem Bityutskiy 					  &snod->key, snod->sqnum, deletion,
7471e51764aSArtem Bityutskiy 					  &used, 0, new_size);
7481e51764aSArtem Bityutskiy 			break;
7491e51764aSArtem Bityutskiy 		}
7501e51764aSArtem Bityutskiy 		case UBIFS_DATA_NODE:
7511e51764aSArtem Bityutskiy 		{
7521e51764aSArtem Bityutskiy 			struct ubifs_data_node *dn = snod->node;
7531e51764aSArtem Bityutskiy 			loff_t new_size = le32_to_cpu(dn->size) +
7541e51764aSArtem Bityutskiy 					  key_block(c, &snod->key) *
7551e51764aSArtem Bityutskiy 					  UBIFS_BLOCK_SIZE;
7561e51764aSArtem Bityutskiy 
75716a26b20SSascha Hauer 			err = insert_node(c, lnum, snod->offs, snod->len, hash,
7581e51764aSArtem Bityutskiy 					  &snod->key, snod->sqnum, deletion,
7591e51764aSArtem Bityutskiy 					  &used, 0, new_size);
7601e51764aSArtem Bityutskiy 			break;
7611e51764aSArtem Bityutskiy 		}
7621e51764aSArtem Bityutskiy 		case UBIFS_DENT_NODE:
7631e51764aSArtem Bityutskiy 		case UBIFS_XENT_NODE:
7641e51764aSArtem Bityutskiy 		{
7651e51764aSArtem Bityutskiy 			struct ubifs_dent_node *dent = snod->node;
7661e51764aSArtem Bityutskiy 
7671e51764aSArtem Bityutskiy 			err = ubifs_validate_entry(c, dent);
7681e51764aSArtem Bityutskiy 			if (err)
7691e51764aSArtem Bityutskiy 				goto out_dump;
7701e51764aSArtem Bityutskiy 
77116a26b20SSascha Hauer 			err = insert_dent(c, lnum, snod->offs, snod->len, hash,
7721e51764aSArtem Bityutskiy 					  &snod->key, dent->name,
7731e51764aSArtem Bityutskiy 					  le16_to_cpu(dent->nlen), snod->sqnum,
7741e51764aSArtem Bityutskiy 					  !le64_to_cpu(dent->inum), &used);
7751e51764aSArtem Bityutskiy 			break;
7761e51764aSArtem Bityutskiy 		}
7771e51764aSArtem Bityutskiy 		case UBIFS_TRUN_NODE:
7781e51764aSArtem Bityutskiy 		{
7791e51764aSArtem Bityutskiy 			struct ubifs_trun_node *trun = snod->node;
7801e51764aSArtem Bityutskiy 			loff_t old_size = le64_to_cpu(trun->old_size);
7811e51764aSArtem Bityutskiy 			loff_t new_size = le64_to_cpu(trun->new_size);
7821e51764aSArtem Bityutskiy 			union ubifs_key key;
7831e51764aSArtem Bityutskiy 
7841e51764aSArtem Bityutskiy 			/* Validate truncation node */
7851e51764aSArtem Bityutskiy 			if (old_size < 0 || old_size > c->max_inode_sz ||
7861e51764aSArtem Bityutskiy 			    new_size < 0 || new_size > c->max_inode_sz ||
7871e51764aSArtem Bityutskiy 			    old_size <= new_size) {
788235c362bSSheng Yong 				ubifs_err(c, "bad truncation node");
7891e51764aSArtem Bityutskiy 				goto out_dump;
7901e51764aSArtem Bityutskiy 			}
7911e51764aSArtem Bityutskiy 
7921e51764aSArtem Bityutskiy 			/*
7931e51764aSArtem Bityutskiy 			 * Create a fake truncation key just to use the same
7941e51764aSArtem Bityutskiy 			 * functions which expect nodes to have keys.
7951e51764aSArtem Bityutskiy 			 */
7961e51764aSArtem Bityutskiy 			trun_key_init(c, &key, le32_to_cpu(trun->inum));
79716a26b20SSascha Hauer 			err = insert_node(c, lnum, snod->offs, snod->len, hash,
7981e51764aSArtem Bityutskiy 					  &key, snod->sqnum, 1, &used,
7991e51764aSArtem Bityutskiy 					  old_size, new_size);
8001e51764aSArtem Bityutskiy 			break;
8011e51764aSArtem Bityutskiy 		}
8026a98bc46SSascha Hauer 		case UBIFS_AUTH_NODE:
8036a98bc46SSascha Hauer 			break;
8041e51764aSArtem Bityutskiy 		default:
805235c362bSSheng Yong 			ubifs_err(c, "unexpected node type %d in bud LEB %d:%d",
8061e51764aSArtem Bityutskiy 				  snod->type, lnum, snod->offs);
8071e51764aSArtem Bityutskiy 			err = -EINVAL;
8081e51764aSArtem Bityutskiy 			goto out_dump;
8091e51764aSArtem Bityutskiy 		}
8101e51764aSArtem Bityutskiy 		if (err)
8111e51764aSArtem Bityutskiy 			goto out;
812da8ef65fSSascha Hauer 
813da8ef65fSSascha Hauer 		n++;
814da8ef65fSSascha Hauer 		if (n == n_nodes)
815da8ef65fSSascha Hauer 			break;
8161e51764aSArtem Bityutskiy 	}
8171e51764aSArtem Bityutskiy 
8186eb61d58SRichard Weinberger 	ubifs_assert(c, ubifs_search_bud(c, lnum));
8196eb61d58SRichard Weinberger 	ubifs_assert(c, sleb->endpt - offs >= used);
8206eb61d58SRichard Weinberger 	ubifs_assert(c, sleb->endpt % c->min_io_size == 0);
8211e51764aSArtem Bityutskiy 
822e76a4526SArtem Bityutskiy 	b->dirty = sleb->endpt - offs - used;
823e76a4526SArtem Bityutskiy 	b->free = c->leb_size - sleb->endpt;
82479fda517SArtem Bityutskiy 	dbg_mnt("bud LEB %d replied: dirty %d, free %d",
82579fda517SArtem Bityutskiy 		lnum, b->dirty, b->free);
8261e51764aSArtem Bityutskiy 
8271e51764aSArtem Bityutskiy out:
8281e51764aSArtem Bityutskiy 	ubifs_scan_destroy(sleb);
8291e51764aSArtem Bityutskiy 	return err;
8301e51764aSArtem Bityutskiy 
8311e51764aSArtem Bityutskiy out_dump:
832235c362bSSheng Yong 	ubifs_err(c, "bad node is at LEB %d:%d", lnum, snod->offs);
833a33e30a0SZhihao Cheng 	ubifs_dump_node(c, snod->node, c->leb_size - snod->offs);
8341e51764aSArtem Bityutskiy 	ubifs_scan_destroy(sleb);
8351e51764aSArtem Bityutskiy 	return -EINVAL;
8361e51764aSArtem Bityutskiy }
8371e51764aSArtem Bityutskiy 
8381e51764aSArtem Bityutskiy /**
8391e51764aSArtem Bityutskiy  * replay_buds - replay all buds.
8401e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
8411e51764aSArtem Bityutskiy  *
8421e51764aSArtem Bityutskiy  * This function returns zero in case of success and a negative error code in
8431e51764aSArtem Bityutskiy  * case of failure.
8441e51764aSArtem Bityutskiy  */
8451e51764aSArtem Bityutskiy static int replay_buds(struct ubifs_info *c)
8461e51764aSArtem Bityutskiy {
8471e51764aSArtem Bityutskiy 	struct bud_entry *b;
848074bcb9bSArtem Bityutskiy 	int err;
8497703f09dSArtem Bityutskiy 	unsigned long long prev_sqnum = 0;
8501e51764aSArtem Bityutskiy 
8511e51764aSArtem Bityutskiy 	list_for_each_entry(b, &c->replay_buds, list) {
852e76a4526SArtem Bityutskiy 		err = replay_bud(c, b);
8531e51764aSArtem Bityutskiy 		if (err)
8541e51764aSArtem Bityutskiy 			return err;
8557703f09dSArtem Bityutskiy 
8566eb61d58SRichard Weinberger 		ubifs_assert(c, b->sqnum > prev_sqnum);
8577703f09dSArtem Bityutskiy 		prev_sqnum = b->sqnum;
8581e51764aSArtem Bityutskiy 	}
8591e51764aSArtem Bityutskiy 
8601e51764aSArtem Bityutskiy 	return 0;
8611e51764aSArtem Bityutskiy }
8621e51764aSArtem Bityutskiy 
8631e51764aSArtem Bityutskiy /**
8641e51764aSArtem Bityutskiy  * destroy_bud_list - destroy the list of buds to replay.
8651e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
8661e51764aSArtem Bityutskiy  */
8671e51764aSArtem Bityutskiy static void destroy_bud_list(struct ubifs_info *c)
8681e51764aSArtem Bityutskiy {
8691e51764aSArtem Bityutskiy 	struct bud_entry *b;
8701e51764aSArtem Bityutskiy 
8711e51764aSArtem Bityutskiy 	while (!list_empty(&c->replay_buds)) {
8721e51764aSArtem Bityutskiy 		b = list_entry(c->replay_buds.next, struct bud_entry, list);
8731e51764aSArtem Bityutskiy 		list_del(&b->list);
8741e51764aSArtem Bityutskiy 		kfree(b);
8751e51764aSArtem Bityutskiy 	}
8761e51764aSArtem Bityutskiy }
8771e51764aSArtem Bityutskiy 
8781e51764aSArtem Bityutskiy /**
8791e51764aSArtem Bityutskiy  * add_replay_bud - add a bud to the list of buds to replay.
8801e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
8811e51764aSArtem Bityutskiy  * @lnum: bud logical eraseblock number to replay
8821e51764aSArtem Bityutskiy  * @offs: bud start offset
8831e51764aSArtem Bityutskiy  * @jhead: journal head to which this bud belongs
8841e51764aSArtem Bityutskiy  * @sqnum: reference node sequence number
8851e51764aSArtem Bityutskiy  *
8861e51764aSArtem Bityutskiy  * This function returns zero in case of success and a negative error code in
8871e51764aSArtem Bityutskiy  * case of failure.
8881e51764aSArtem Bityutskiy  */
8891e51764aSArtem Bityutskiy static int add_replay_bud(struct ubifs_info *c, int lnum, int offs, int jhead,
8901e51764aSArtem Bityutskiy 			  unsigned long long sqnum)
8911e51764aSArtem Bityutskiy {
8921e51764aSArtem Bityutskiy 	struct ubifs_bud *bud;
8931e51764aSArtem Bityutskiy 	struct bud_entry *b;
894da8ef65fSSascha Hauer 	int err;
8951e51764aSArtem Bityutskiy 
8961e51764aSArtem Bityutskiy 	dbg_mnt("add replay bud LEB %d:%d, head %d", lnum, offs, jhead);
8971e51764aSArtem Bityutskiy 
8981e51764aSArtem Bityutskiy 	bud = kmalloc(sizeof(struct ubifs_bud), GFP_KERNEL);
8991e51764aSArtem Bityutskiy 	if (!bud)
9001e51764aSArtem Bityutskiy 		return -ENOMEM;
9011e51764aSArtem Bityutskiy 
9021e51764aSArtem Bityutskiy 	b = kmalloc(sizeof(struct bud_entry), GFP_KERNEL);
9031e51764aSArtem Bityutskiy 	if (!b) {
904da8ef65fSSascha Hauer 		err = -ENOMEM;
905da8ef65fSSascha Hauer 		goto out;
9061e51764aSArtem Bityutskiy 	}
9071e51764aSArtem Bityutskiy 
9081e51764aSArtem Bityutskiy 	bud->lnum = lnum;
9091e51764aSArtem Bityutskiy 	bud->start = offs;
9101e51764aSArtem Bityutskiy 	bud->jhead = jhead;
911da8ef65fSSascha Hauer 	bud->log_hash = ubifs_hash_get_desc(c);
912da8ef65fSSascha Hauer 	if (IS_ERR(bud->log_hash)) {
913da8ef65fSSascha Hauer 		err = PTR_ERR(bud->log_hash);
914da8ef65fSSascha Hauer 		goto out;
915da8ef65fSSascha Hauer 	}
916da8ef65fSSascha Hauer 
917da8ef65fSSascha Hauer 	ubifs_shash_copy_state(c, c->log_hash, bud->log_hash);
918da8ef65fSSascha Hauer 
9191e51764aSArtem Bityutskiy 	ubifs_add_bud(c, bud);
9201e51764aSArtem Bityutskiy 
9211e51764aSArtem Bityutskiy 	b->bud = bud;
9221e51764aSArtem Bityutskiy 	b->sqnum = sqnum;
9231e51764aSArtem Bityutskiy 	list_add_tail(&b->list, &c->replay_buds);
9241e51764aSArtem Bityutskiy 
9251e51764aSArtem Bityutskiy 	return 0;
926da8ef65fSSascha Hauer out:
927da8ef65fSSascha Hauer 	kfree(bud);
928da8ef65fSSascha Hauer 	kfree(b);
929da8ef65fSSascha Hauer 
930da8ef65fSSascha Hauer 	return err;
9311e51764aSArtem Bityutskiy }
9321e51764aSArtem Bityutskiy 
9331e51764aSArtem Bityutskiy /**
9341e51764aSArtem Bityutskiy  * validate_ref - validate a reference node.
9351e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
9361e51764aSArtem Bityutskiy  * @ref: the reference node to validate
9371e51764aSArtem Bityutskiy  *
9381e51764aSArtem Bityutskiy  * This function returns %1 if a bud reference already exists for the LEB. %0 is
9391e51764aSArtem Bityutskiy  * returned if the reference node is new, otherwise %-EINVAL is returned if
9401e51764aSArtem Bityutskiy  * validation failed.
9411e51764aSArtem Bityutskiy  */
9421e51764aSArtem Bityutskiy static int validate_ref(struct ubifs_info *c, const struct ubifs_ref_node *ref)
9431e51764aSArtem Bityutskiy {
9441e51764aSArtem Bityutskiy 	struct ubifs_bud *bud;
9451e51764aSArtem Bityutskiy 	int lnum = le32_to_cpu(ref->lnum);
9461e51764aSArtem Bityutskiy 	unsigned int offs = le32_to_cpu(ref->offs);
9471e51764aSArtem Bityutskiy 	unsigned int jhead = le32_to_cpu(ref->jhead);
9481e51764aSArtem Bityutskiy 
9491e51764aSArtem Bityutskiy 	/*
9501e51764aSArtem Bityutskiy 	 * ref->offs may point to the end of LEB when the journal head points
9511e51764aSArtem Bityutskiy 	 * to the end of LEB and we write reference node for it during commit.
9521e51764aSArtem Bityutskiy 	 * So this is why we require 'offs > c->leb_size'.
9531e51764aSArtem Bityutskiy 	 */
9541e51764aSArtem Bityutskiy 	if (jhead >= c->jhead_cnt || lnum >= c->leb_cnt ||
9551e51764aSArtem Bityutskiy 	    lnum < c->main_first || offs > c->leb_size ||
9561e51764aSArtem Bityutskiy 	    offs & (c->min_io_size - 1))
9571e51764aSArtem Bityutskiy 		return -EINVAL;
9581e51764aSArtem Bityutskiy 
9591e51764aSArtem Bityutskiy 	/* Make sure we have not already looked at this bud */
9601e51764aSArtem Bityutskiy 	bud = ubifs_search_bud(c, lnum);
9611e51764aSArtem Bityutskiy 	if (bud) {
9621e51764aSArtem Bityutskiy 		if (bud->jhead == jhead && bud->start <= offs)
9631e51764aSArtem Bityutskiy 			return 1;
964235c362bSSheng Yong 		ubifs_err(c, "bud at LEB %d:%d was already referred", lnum, offs);
9651e51764aSArtem Bityutskiy 		return -EINVAL;
9661e51764aSArtem Bityutskiy 	}
9671e51764aSArtem Bityutskiy 
9681e51764aSArtem Bityutskiy 	return 0;
9691e51764aSArtem Bityutskiy }
9701e51764aSArtem Bityutskiy 
9711e51764aSArtem Bityutskiy /**
9721e51764aSArtem Bityutskiy  * replay_log_leb - replay a log logical eraseblock.
9731e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
9741e51764aSArtem Bityutskiy  * @lnum: log logical eraseblock to replay
9751e51764aSArtem Bityutskiy  * @offs: offset to start replaying from
9761e51764aSArtem Bityutskiy  * @sbuf: scan buffer
9771e51764aSArtem Bityutskiy  *
9781e51764aSArtem Bityutskiy  * This function replays a log LEB and returns zero in case of success, %1 if
9791e51764aSArtem Bityutskiy  * this is the last LEB in the log, and a negative error code in case of
9801e51764aSArtem Bityutskiy  * failure.
9811e51764aSArtem Bityutskiy  */
9821e51764aSArtem Bityutskiy static int replay_log_leb(struct ubifs_info *c, int lnum, int offs, void *sbuf)
9831e51764aSArtem Bityutskiy {
9841e51764aSArtem Bityutskiy 	int err;
9851e51764aSArtem Bityutskiy 	struct ubifs_scan_leb *sleb;
9861e51764aSArtem Bityutskiy 	struct ubifs_scan_node *snod;
9871e51764aSArtem Bityutskiy 	const struct ubifs_cs_node *node;
9881e51764aSArtem Bityutskiy 
9891e51764aSArtem Bityutskiy 	dbg_mnt("replay log LEB %d:%d", lnum, offs);
990348709baSArtem Bityutskiy 	sleb = ubifs_scan(c, lnum, offs, sbuf, c->need_recovery);
9911e51764aSArtem Bityutskiy 	if (IS_ERR(sleb)) {
992ed43f2f0SArtem Bityutskiy 		if (PTR_ERR(sleb) != -EUCLEAN || !c->need_recovery)
993ed43f2f0SArtem Bityutskiy 			return PTR_ERR(sleb);
9947d08ae3cSArtem Bityutskiy 		/*
9957d08ae3cSArtem Bityutskiy 		 * Note, the below function will recover this log LEB only if
9967d08ae3cSArtem Bityutskiy 		 * it is the last, because unclean reboots can possibly corrupt
9977d08ae3cSArtem Bityutskiy 		 * only the tail of the log.
9987d08ae3cSArtem Bityutskiy 		 */
9991e51764aSArtem Bityutskiy 		sleb = ubifs_recover_log_leb(c, lnum, offs, sbuf);
10001e51764aSArtem Bityutskiy 		if (IS_ERR(sleb))
10011e51764aSArtem Bityutskiy 			return PTR_ERR(sleb);
10021e51764aSArtem Bityutskiy 	}
10031e51764aSArtem Bityutskiy 
10041e51764aSArtem Bityutskiy 	if (sleb->nodes_cnt == 0) {
10051e51764aSArtem Bityutskiy 		err = 1;
10061e51764aSArtem Bityutskiy 		goto out;
10071e51764aSArtem Bityutskiy 	}
10081e51764aSArtem Bityutskiy 
10091e51764aSArtem Bityutskiy 	node = sleb->buf;
10101e51764aSArtem Bityutskiy 	snod = list_entry(sleb->nodes.next, struct ubifs_scan_node, list);
10111e51764aSArtem Bityutskiy 	if (c->cs_sqnum == 0) {
10121e51764aSArtem Bityutskiy 		/*
10131e51764aSArtem Bityutskiy 		 * This is the first log LEB we are looking at, make sure that
10141e51764aSArtem Bityutskiy 		 * the first node is a commit start node. Also record its
10151e51764aSArtem Bityutskiy 		 * sequence number so that UBIFS can determine where the log
10161e51764aSArtem Bityutskiy 		 * ends, because all nodes which were have higher sequence
10171e51764aSArtem Bityutskiy 		 * numbers.
10181e51764aSArtem Bityutskiy 		 */
10191e51764aSArtem Bityutskiy 		if (snod->type != UBIFS_CS_NODE) {
1020235c362bSSheng Yong 			ubifs_err(c, "first log node at LEB %d:%d is not CS node",
10211e51764aSArtem Bityutskiy 				  lnum, offs);
10221e51764aSArtem Bityutskiy 			goto out_dump;
10231e51764aSArtem Bityutskiy 		}
10241e51764aSArtem Bityutskiy 		if (le64_to_cpu(node->cmt_no) != c->cmt_no) {
1025235c362bSSheng Yong 			ubifs_err(c, "first CS node at LEB %d:%d has wrong commit number %llu expected %llu",
10261e51764aSArtem Bityutskiy 				  lnum, offs,
10271e51764aSArtem Bityutskiy 				  (unsigned long long)le64_to_cpu(node->cmt_no),
10281e51764aSArtem Bityutskiy 				  c->cmt_no);
10291e51764aSArtem Bityutskiy 			goto out_dump;
10301e51764aSArtem Bityutskiy 		}
10311e51764aSArtem Bityutskiy 
10321e51764aSArtem Bityutskiy 		c->cs_sqnum = le64_to_cpu(node->ch.sqnum);
10331e51764aSArtem Bityutskiy 		dbg_mnt("commit start sqnum %llu", c->cs_sqnum);
1034da8ef65fSSascha Hauer 
1035da8ef65fSSascha Hauer 		err = ubifs_shash_init(c, c->log_hash);
1036da8ef65fSSascha Hauer 		if (err)
1037da8ef65fSSascha Hauer 			goto out;
1038da8ef65fSSascha Hauer 
1039da8ef65fSSascha Hauer 		err = ubifs_shash_update(c, c->log_hash, node, UBIFS_CS_NODE_SZ);
1040da8ef65fSSascha Hauer 		if (err < 0)
1041da8ef65fSSascha Hauer 			goto out;
10421e51764aSArtem Bityutskiy 	}
10431e51764aSArtem Bityutskiy 
10441e51764aSArtem Bityutskiy 	if (snod->sqnum < c->cs_sqnum) {
10451e51764aSArtem Bityutskiy 		/*
10461e51764aSArtem Bityutskiy 		 * This means that we reached end of log and now
10471e51764aSArtem Bityutskiy 		 * look to the older log data, which was already
10481e51764aSArtem Bityutskiy 		 * committed but the eraseblock was not erased (UBIFS
10496edbfafdSArtem Bityutskiy 		 * only un-maps it). So this basically means we have to
10501e51764aSArtem Bityutskiy 		 * exit with "end of log" code.
10511e51764aSArtem Bityutskiy 		 */
10521e51764aSArtem Bityutskiy 		err = 1;
10531e51764aSArtem Bityutskiy 		goto out;
10541e51764aSArtem Bityutskiy 	}
10551e51764aSArtem Bityutskiy 
10561e51764aSArtem Bityutskiy 	/* Make sure the first node sits at offset zero of the LEB */
10571e51764aSArtem Bityutskiy 	if (snod->offs != 0) {
1058235c362bSSheng Yong 		ubifs_err(c, "first node is not at zero offset");
10591e51764aSArtem Bityutskiy 		goto out_dump;
10601e51764aSArtem Bityutskiy 	}
10611e51764aSArtem Bityutskiy 
10621e51764aSArtem Bityutskiy 	list_for_each_entry(snod, &sleb->nodes, list) {
10631e51764aSArtem Bityutskiy 		cond_resched();
10641e51764aSArtem Bityutskiy 
10651e51764aSArtem Bityutskiy 		if (snod->sqnum >= SQNUM_WATERMARK) {
1066235c362bSSheng Yong 			ubifs_err(c, "file system's life ended");
10671e51764aSArtem Bityutskiy 			goto out_dump;
10681e51764aSArtem Bityutskiy 		}
10691e51764aSArtem Bityutskiy 
10701e51764aSArtem Bityutskiy 		if (snod->sqnum < c->cs_sqnum) {
1071235c362bSSheng Yong 			ubifs_err(c, "bad sqnum %llu, commit sqnum %llu",
10721e51764aSArtem Bityutskiy 				  snod->sqnum, c->cs_sqnum);
10731e51764aSArtem Bityutskiy 			goto out_dump;
10741e51764aSArtem Bityutskiy 		}
10751e51764aSArtem Bityutskiy 
10761e51764aSArtem Bityutskiy 		if (snod->sqnum > c->max_sqnum)
10771e51764aSArtem Bityutskiy 			c->max_sqnum = snod->sqnum;
10781e51764aSArtem Bityutskiy 
10791e51764aSArtem Bityutskiy 		switch (snod->type) {
10801e51764aSArtem Bityutskiy 		case UBIFS_REF_NODE: {
10811e51764aSArtem Bityutskiy 			const struct ubifs_ref_node *ref = snod->node;
10821e51764aSArtem Bityutskiy 
10831e51764aSArtem Bityutskiy 			err = validate_ref(c, ref);
10841e51764aSArtem Bityutskiy 			if (err == 1)
10851e51764aSArtem Bityutskiy 				break; /* Already have this bud */
10861e51764aSArtem Bityutskiy 			if (err)
10871e51764aSArtem Bityutskiy 				goto out_dump;
10881e51764aSArtem Bityutskiy 
1089da8ef65fSSascha Hauer 			err = ubifs_shash_update(c, c->log_hash, ref,
1090da8ef65fSSascha Hauer 						 UBIFS_REF_NODE_SZ);
1091da8ef65fSSascha Hauer 			if (err)
1092da8ef65fSSascha Hauer 				goto out;
1093da8ef65fSSascha Hauer 
10941e51764aSArtem Bityutskiy 			err = add_replay_bud(c, le32_to_cpu(ref->lnum),
10951e51764aSArtem Bityutskiy 					     le32_to_cpu(ref->offs),
10961e51764aSArtem Bityutskiy 					     le32_to_cpu(ref->jhead),
10971e51764aSArtem Bityutskiy 					     snod->sqnum);
10981e51764aSArtem Bityutskiy 			if (err)
10991e51764aSArtem Bityutskiy 				goto out;
11001e51764aSArtem Bityutskiy 
11011e51764aSArtem Bityutskiy 			break;
11021e51764aSArtem Bityutskiy 		}
11031e51764aSArtem Bityutskiy 		case UBIFS_CS_NODE:
11041e51764aSArtem Bityutskiy 			/* Make sure it sits at the beginning of LEB */
11051e51764aSArtem Bityutskiy 			if (snod->offs != 0) {
1106235c362bSSheng Yong 				ubifs_err(c, "unexpected node in log");
11071e51764aSArtem Bityutskiy 				goto out_dump;
11081e51764aSArtem Bityutskiy 			}
11091e51764aSArtem Bityutskiy 			break;
11101e51764aSArtem Bityutskiy 		default:
1111235c362bSSheng Yong 			ubifs_err(c, "unexpected node in log");
11121e51764aSArtem Bityutskiy 			goto out_dump;
11131e51764aSArtem Bityutskiy 		}
11141e51764aSArtem Bityutskiy 	}
11151e51764aSArtem Bityutskiy 
11161e51764aSArtem Bityutskiy 	if (sleb->endpt || c->lhead_offs >= c->leb_size) {
11171e51764aSArtem Bityutskiy 		c->lhead_lnum = lnum;
11181e51764aSArtem Bityutskiy 		c->lhead_offs = sleb->endpt;
11191e51764aSArtem Bityutskiy 	}
11201e51764aSArtem Bityutskiy 
11211e51764aSArtem Bityutskiy 	err = !sleb->endpt;
11221e51764aSArtem Bityutskiy out:
11231e51764aSArtem Bityutskiy 	ubifs_scan_destroy(sleb);
11241e51764aSArtem Bityutskiy 	return err;
11251e51764aSArtem Bityutskiy 
11261e51764aSArtem Bityutskiy out_dump:
1127235c362bSSheng Yong 	ubifs_err(c, "log error detected while replaying the log at LEB %d:%d",
11281e51764aSArtem Bityutskiy 		  lnum, offs + snod->offs);
1129a33e30a0SZhihao Cheng 	ubifs_dump_node(c, snod->node, c->leb_size - snod->offs);
11301e51764aSArtem Bityutskiy 	ubifs_scan_destroy(sleb);
11311e51764aSArtem Bityutskiy 	return -EINVAL;
11321e51764aSArtem Bityutskiy }
11331e51764aSArtem Bityutskiy 
11341e51764aSArtem Bityutskiy /**
11351e51764aSArtem Bityutskiy  * take_ihead - update the status of the index head in lprops to 'taken'.
11361e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
11371e51764aSArtem Bityutskiy  *
11381e51764aSArtem Bityutskiy  * This function returns the amount of free space in the index head LEB or a
11391e51764aSArtem Bityutskiy  * negative error code.
11401e51764aSArtem Bityutskiy  */
11411e51764aSArtem Bityutskiy static int take_ihead(struct ubifs_info *c)
11421e51764aSArtem Bityutskiy {
11431e51764aSArtem Bityutskiy 	const struct ubifs_lprops *lp;
11441e51764aSArtem Bityutskiy 	int err, free;
11451e51764aSArtem Bityutskiy 
11461e51764aSArtem Bityutskiy 	ubifs_get_lprops(c);
11471e51764aSArtem Bityutskiy 
11481e51764aSArtem Bityutskiy 	lp = ubifs_lpt_lookup_dirty(c, c->ihead_lnum);
11491e51764aSArtem Bityutskiy 	if (IS_ERR(lp)) {
11501e51764aSArtem Bityutskiy 		err = PTR_ERR(lp);
11511e51764aSArtem Bityutskiy 		goto out;
11521e51764aSArtem Bityutskiy 	}
11531e51764aSArtem Bityutskiy 
11541e51764aSArtem Bityutskiy 	free = lp->free;
11551e51764aSArtem Bityutskiy 
11561e51764aSArtem Bityutskiy 	lp = ubifs_change_lp(c, lp, LPROPS_NC, LPROPS_NC,
11571e51764aSArtem Bityutskiy 			     lp->flags | LPROPS_TAKEN, 0);
11581e51764aSArtem Bityutskiy 	if (IS_ERR(lp)) {
11591e51764aSArtem Bityutskiy 		err = PTR_ERR(lp);
11601e51764aSArtem Bityutskiy 		goto out;
11611e51764aSArtem Bityutskiy 	}
11621e51764aSArtem Bityutskiy 
11631e51764aSArtem Bityutskiy 	err = free;
11641e51764aSArtem Bityutskiy out:
11651e51764aSArtem Bityutskiy 	ubifs_release_lprops(c);
11661e51764aSArtem Bityutskiy 	return err;
11671e51764aSArtem Bityutskiy }
11681e51764aSArtem Bityutskiy 
11691e51764aSArtem Bityutskiy /**
11701e51764aSArtem Bityutskiy  * ubifs_replay_journal - replay journal.
11711e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
11721e51764aSArtem Bityutskiy  *
11731e51764aSArtem Bityutskiy  * This function scans the journal, replays and cleans it up. It makes sure all
11741e51764aSArtem Bityutskiy  * memory data structures related to uncommitted journal are built (dirty TNC
11751e51764aSArtem Bityutskiy  * tree, tree of buds, modified lprops, etc).
11761e51764aSArtem Bityutskiy  */
11771e51764aSArtem Bityutskiy int ubifs_replay_journal(struct ubifs_info *c)
11781e51764aSArtem Bityutskiy {
1179d51f17eaSArtem Bityutskiy 	int err, lnum, free;
11801e51764aSArtem Bityutskiy 
11811e51764aSArtem Bityutskiy 	BUILD_BUG_ON(UBIFS_TRUN_KEY > 5);
11821e51764aSArtem Bityutskiy 
11831e51764aSArtem Bityutskiy 	/* Update the status of the index head in lprops to 'taken' */
11841e51764aSArtem Bityutskiy 	free = take_ihead(c);
11851e51764aSArtem Bityutskiy 	if (free < 0)
11861e51764aSArtem Bityutskiy 		return free; /* Error code */
11871e51764aSArtem Bityutskiy 
11881e51764aSArtem Bityutskiy 	if (c->ihead_offs != c->leb_size - free) {
1189235c362bSSheng Yong 		ubifs_err(c, "bad index head LEB %d:%d", c->ihead_lnum,
11901e51764aSArtem Bityutskiy 			  c->ihead_offs);
11911e51764aSArtem Bityutskiy 		return -EINVAL;
11921e51764aSArtem Bityutskiy 	}
11931e51764aSArtem Bityutskiy 
11941e51764aSArtem Bityutskiy 	dbg_mnt("start replaying the journal");
11951e51764aSArtem Bityutskiy 	c->replaying = 1;
11961e51764aSArtem Bityutskiy 	lnum = c->ltail_lnum = c->lhead_lnum;
11971e51764aSArtem Bityutskiy 
1198d51f17eaSArtem Bityutskiy 	do {
1199d51f17eaSArtem Bityutskiy 		err = replay_log_leb(c, lnum, 0, c->sbuf);
120088cff0f0Shujianyang 		if (err == 1) {
120188cff0f0Shujianyang 			if (lnum != c->lhead_lnum)
12021e51764aSArtem Bityutskiy 				/* We hit the end of the log */
12031e51764aSArtem Bityutskiy 				break;
120488cff0f0Shujianyang 
120588cff0f0Shujianyang 			/*
120688cff0f0Shujianyang 			 * The head of the log must always start with the
120788cff0f0Shujianyang 			 * "commit start" node on a properly formatted UBIFS.
120888cff0f0Shujianyang 			 * But we found no nodes at all, which means that
1209c7e593b3SSascha Hauer 			 * something went wrong and we cannot proceed mounting
121088cff0f0Shujianyang 			 * the file-system.
121188cff0f0Shujianyang 			 */
1212235c362bSSheng Yong 			ubifs_err(c, "no UBIFS nodes found at the log head LEB %d:%d, possibly corrupted",
121388cff0f0Shujianyang 				  lnum, 0);
121488cff0f0Shujianyang 			err = -EINVAL;
121588cff0f0Shujianyang 		}
12161e51764aSArtem Bityutskiy 		if (err)
12171e51764aSArtem Bityutskiy 			goto out;
1218d51f17eaSArtem Bityutskiy 		lnum = ubifs_next_log_lnum(c, lnum);
1219c212f402SArtem Bityutskiy 	} while (lnum != c->ltail_lnum);
12201e51764aSArtem Bityutskiy 
12211e51764aSArtem Bityutskiy 	err = replay_buds(c);
12221e51764aSArtem Bityutskiy 	if (err)
12231e51764aSArtem Bityutskiy 		goto out;
12241e51764aSArtem Bityutskiy 
1225debf12d5SArtem Bityutskiy 	err = apply_replay_list(c);
12261e51764aSArtem Bityutskiy 	if (err)
12271e51764aSArtem Bityutskiy 		goto out;
12281e51764aSArtem Bityutskiy 
1229074bcb9bSArtem Bityutskiy 	err = set_buds_lprops(c);
1230074bcb9bSArtem Bityutskiy 	if (err)
1231074bcb9bSArtem Bityutskiy 		goto out;
1232074bcb9bSArtem Bityutskiy 
12336edbfafdSArtem Bityutskiy 	/*
1234b137545cSArtem Bityutskiy 	 * UBIFS budgeting calculations use @c->bi.uncommitted_idx variable
1235b137545cSArtem Bityutskiy 	 * to roughly estimate index growth. Things like @c->bi.min_idx_lebs
12366edbfafdSArtem Bityutskiy 	 * depend on it. This means we have to initialize it to make sure
12376edbfafdSArtem Bityutskiy 	 * budgeting works properly.
12386edbfafdSArtem Bityutskiy 	 */
1239b137545cSArtem Bityutskiy 	c->bi.uncommitted_idx = atomic_long_read(&c->dirty_zn_cnt);
1240b137545cSArtem Bityutskiy 	c->bi.uncommitted_idx *= c->max_idx_node_sz;
12416edbfafdSArtem Bityutskiy 
12426eb61d58SRichard Weinberger 	ubifs_assert(c, c->bud_bytes <= c->max_bud_bytes || c->need_recovery);
124379fda517SArtem Bityutskiy 	dbg_mnt("finished, log head LEB %d:%d, max_sqnum %llu, highest_inum %lu",
124479fda517SArtem Bityutskiy 		c->lhead_lnum, c->lhead_offs, c->max_sqnum,
1245e84461adSArtem Bityutskiy 		(unsigned long)c->highest_inum);
12461e51764aSArtem Bityutskiy out:
1247debf12d5SArtem Bityutskiy 	destroy_replay_list(c);
12481e51764aSArtem Bityutskiy 	destroy_bud_list(c);
12491e51764aSArtem Bityutskiy 	c->replaying = 0;
12501e51764aSArtem Bityutskiy 	return err;
12511e51764aSArtem Bityutskiy }
1252