xref: /linux/fs/ubifs/replay.c (revision 410b6de702ef84fea6e7abcb6620ef8bfc112fae)
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);
226e58725d5SRichard Weinberger 		if (key_inum(c, &r->key) == key_inum(c, &rino->key))
227e58725d5SRichard Weinberger 			return r->deletion == 0;
228e58725d5SRichard Weinberger 
229e58725d5SRichard Weinberger 	}
230e58725d5SRichard Weinberger 
231e58725d5SRichard Weinberger 	ubifs_assert(c, 0);
232e58725d5SRichard Weinberger 	return false;
233e58725d5SRichard Weinberger }
234e58725d5SRichard Weinberger 
235e58725d5SRichard Weinberger /**
2361e51764aSArtem Bityutskiy  * apply_replay_entry - apply a replay entry to the TNC.
2371e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
2381e51764aSArtem Bityutskiy  * @r: replay entry to apply
2391e51764aSArtem Bityutskiy  *
2401e51764aSArtem Bityutskiy  * Apply a replay entry to the TNC.
2411e51764aSArtem Bityutskiy  */
2421e51764aSArtem Bityutskiy static int apply_replay_entry(struct ubifs_info *c, struct replay_entry *r)
2431e51764aSArtem Bityutskiy {
244074bcb9bSArtem Bityutskiy 	int err;
2451e51764aSArtem Bityutskiy 
246515315a1SArtem Bityutskiy 	dbg_mntk(&r->key, "LEB %d:%d len %d deletion %d sqnum %llu key ",
247515315a1SArtem Bityutskiy 		 r->lnum, r->offs, r->len, r->deletion, r->sqnum);
2481e51764aSArtem Bityutskiy 
249074bcb9bSArtem Bityutskiy 	if (is_hash_key(c, &r->key)) {
250074bcb9bSArtem Bityutskiy 		if (r->deletion)
2511e51764aSArtem Bityutskiy 			err = ubifs_tnc_remove_nm(c, &r->key, &r->nm);
2521e51764aSArtem Bityutskiy 		else
2531e51764aSArtem Bityutskiy 			err = ubifs_tnc_add_nm(c, &r->key, r->lnum, r->offs,
25416a26b20SSascha Hauer 					       r->len, r->hash, &r->nm);
2551e51764aSArtem Bityutskiy 	} else {
256074bcb9bSArtem Bityutskiy 		if (r->deletion)
2571e51764aSArtem Bityutskiy 			switch (key_type(c, &r->key)) {
2581e51764aSArtem Bityutskiy 			case UBIFS_INO_KEY:
2591e51764aSArtem Bityutskiy 			{
2601e51764aSArtem Bityutskiy 				ino_t inum = key_inum(c, &r->key);
2611e51764aSArtem Bityutskiy 
262e58725d5SRichard Weinberger 				if (inode_still_linked(c, r)) {
263e58725d5SRichard Weinberger 					err = 0;
264e58725d5SRichard Weinberger 					break;
265e58725d5SRichard Weinberger 				}
266e58725d5SRichard Weinberger 
2671e51764aSArtem Bityutskiy 				err = ubifs_tnc_remove_ino(c, inum);
2681e51764aSArtem Bityutskiy 				break;
2691e51764aSArtem Bityutskiy 			}
2701e51764aSArtem Bityutskiy 			case UBIFS_TRUN_KEY:
2711e51764aSArtem Bityutskiy 				err = trun_remove_range(c, r);
2721e51764aSArtem Bityutskiy 				break;
2731e51764aSArtem Bityutskiy 			default:
2741e51764aSArtem Bityutskiy 				err = ubifs_tnc_remove(c, &r->key);
2751e51764aSArtem Bityutskiy 				break;
2761e51764aSArtem Bityutskiy 			}
2771e51764aSArtem Bityutskiy 		else
2781e51764aSArtem Bityutskiy 			err = ubifs_tnc_add(c, &r->key, r->lnum, r->offs,
27916a26b20SSascha Hauer 					    r->len, r->hash);
2801e51764aSArtem Bityutskiy 		if (err)
2811e51764aSArtem Bityutskiy 			return err;
2821e51764aSArtem Bityutskiy 
2831e51764aSArtem Bityutskiy 		if (c->need_recovery)
284074bcb9bSArtem Bityutskiy 			err = ubifs_recover_size_accum(c, &r->key, r->deletion,
2851e51764aSArtem Bityutskiy 						       r->new_size);
2861e51764aSArtem Bityutskiy 	}
2871e51764aSArtem Bityutskiy 
2881e51764aSArtem Bityutskiy 	return err;
2891e51764aSArtem Bityutskiy }
2901e51764aSArtem Bityutskiy 
2911e51764aSArtem Bityutskiy /**
292debf12d5SArtem Bityutskiy  * replay_entries_cmp - compare 2 replay entries.
293debf12d5SArtem Bityutskiy  * @priv: UBIFS file-system description object
294debf12d5SArtem Bityutskiy  * @a: first replay entry
295ec037dfcSJulia Lawall  * @b: second replay entry
2961e51764aSArtem Bityutskiy  *
297debf12d5SArtem Bityutskiy  * This is a comparios function for 'list_sort()' which compares 2 replay
298debf12d5SArtem Bityutskiy  * entries @a and @b by comparing their sequence numer.  Returns %1 if @a has
299debf12d5SArtem Bityutskiy  * greater sequence number and %-1 otherwise.
3001e51764aSArtem Bityutskiy  */
301debf12d5SArtem Bityutskiy static int replay_entries_cmp(void *priv, struct list_head *a,
302debf12d5SArtem Bityutskiy 			      struct list_head *b)
3031e51764aSArtem Bityutskiy {
3046eb61d58SRichard Weinberger 	struct ubifs_info *c = priv;
305debf12d5SArtem Bityutskiy 	struct replay_entry *ra, *rb;
3061e51764aSArtem Bityutskiy 
307debf12d5SArtem Bityutskiy 	cond_resched();
308debf12d5SArtem Bityutskiy 	if (a == b)
309debf12d5SArtem Bityutskiy 		return 0;
310debf12d5SArtem Bityutskiy 
311debf12d5SArtem Bityutskiy 	ra = list_entry(a, struct replay_entry, list);
312debf12d5SArtem Bityutskiy 	rb = list_entry(b, struct replay_entry, list);
3136eb61d58SRichard Weinberger 	ubifs_assert(c, ra->sqnum != rb->sqnum);
314debf12d5SArtem Bityutskiy 	if (ra->sqnum > rb->sqnum)
315debf12d5SArtem Bityutskiy 		return 1;
316debf12d5SArtem Bityutskiy 	return -1;
3171e51764aSArtem Bityutskiy }
3181e51764aSArtem Bityutskiy 
3191e51764aSArtem Bityutskiy /**
320debf12d5SArtem Bityutskiy  * apply_replay_list - apply the replay list to the TNC.
3211e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
3221e51764aSArtem Bityutskiy  *
323debf12d5SArtem Bityutskiy  * Apply all entries in the replay list to the TNC. Returns zero in case of
324debf12d5SArtem Bityutskiy  * success and a negative error code in case of failure.
3251e51764aSArtem Bityutskiy  */
326debf12d5SArtem Bityutskiy static int apply_replay_list(struct ubifs_info *c)
3271e51764aSArtem Bityutskiy {
3281e51764aSArtem Bityutskiy 	struct replay_entry *r;
3291e51764aSArtem Bityutskiy 	int err;
3301e51764aSArtem Bityutskiy 
331debf12d5SArtem Bityutskiy 	list_sort(c, &c->replay_list, &replay_entries_cmp);
332debf12d5SArtem Bityutskiy 
333debf12d5SArtem Bityutskiy 	list_for_each_entry(r, &c->replay_list, list) {
3341e51764aSArtem Bityutskiy 		cond_resched();
3351e51764aSArtem Bityutskiy 
3361e51764aSArtem Bityutskiy 		err = apply_replay_entry(c, r);
3371e51764aSArtem Bityutskiy 		if (err)
3381e51764aSArtem Bityutskiy 			return err;
3391e51764aSArtem Bityutskiy 	}
340debf12d5SArtem Bityutskiy 
3411e51764aSArtem Bityutskiy 	return 0;
3421e51764aSArtem Bityutskiy }
3431e51764aSArtem Bityutskiy 
3441e51764aSArtem Bityutskiy /**
345debf12d5SArtem Bityutskiy  * destroy_replay_list - destroy the replay.
346debf12d5SArtem Bityutskiy  * @c: UBIFS file-system description object
347debf12d5SArtem Bityutskiy  *
348debf12d5SArtem Bityutskiy  * Destroy the replay list.
349debf12d5SArtem Bityutskiy  */
350debf12d5SArtem Bityutskiy static void destroy_replay_list(struct ubifs_info *c)
351debf12d5SArtem Bityutskiy {
352debf12d5SArtem Bityutskiy 	struct replay_entry *r, *tmp;
353debf12d5SArtem Bityutskiy 
354debf12d5SArtem Bityutskiy 	list_for_each_entry_safe(r, tmp, &c->replay_list, list) {
355debf12d5SArtem Bityutskiy 		if (is_hash_key(c, &r->key))
356f4f61d2cSRichard Weinberger 			kfree(fname_name(&r->nm));
357debf12d5SArtem Bityutskiy 		list_del(&r->list);
358debf12d5SArtem Bityutskiy 		kfree(r);
359debf12d5SArtem Bityutskiy 	}
360debf12d5SArtem Bityutskiy }
361debf12d5SArtem Bityutskiy 
362debf12d5SArtem Bityutskiy /**
363debf12d5SArtem Bityutskiy  * insert_node - insert a node to the replay list
3641e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
3651e51764aSArtem Bityutskiy  * @lnum: node logical eraseblock number
3661e51764aSArtem Bityutskiy  * @offs: node offset
3671e51764aSArtem Bityutskiy  * @len: node length
3681e51764aSArtem Bityutskiy  * @key: node key
3691e51764aSArtem Bityutskiy  * @sqnum: sequence number
3701e51764aSArtem Bityutskiy  * @deletion: non-zero if this is a deletion
3711e51764aSArtem Bityutskiy  * @used: number of bytes in use in a LEB
3721e51764aSArtem Bityutskiy  * @old_size: truncation old size
3731e51764aSArtem Bityutskiy  * @new_size: truncation new size
3741e51764aSArtem Bityutskiy  *
375debf12d5SArtem Bityutskiy  * This function inserts a scanned non-direntry node to the replay list. The
376debf12d5SArtem Bityutskiy  * replay list contains @struct replay_entry elements, and we sort this list in
377debf12d5SArtem Bityutskiy  * sequence number order before applying it. The replay list is applied at the
378debf12d5SArtem Bityutskiy  * very end of the replay process. Since the list is sorted in sequence number
379debf12d5SArtem Bityutskiy  * order, the older modifications are applied first. This function returns zero
380debf12d5SArtem Bityutskiy  * in case of success and a negative error code in case of failure.
3811e51764aSArtem Bityutskiy  */
3821e51764aSArtem Bityutskiy static int insert_node(struct ubifs_info *c, int lnum, int offs, int len,
38316a26b20SSascha Hauer 		       const u8 *hash, union ubifs_key *key,
38416a26b20SSascha Hauer 		       unsigned long long sqnum, int deletion, int *used,
38516a26b20SSascha Hauer 		       loff_t old_size, loff_t new_size)
3861e51764aSArtem Bityutskiy {
3871e51764aSArtem Bityutskiy 	struct replay_entry *r;
3881e51764aSArtem Bityutskiy 
389515315a1SArtem Bityutskiy 	dbg_mntk(key, "add LEB %d:%d, key ", lnum, offs);
390debf12d5SArtem Bityutskiy 
3911e51764aSArtem Bityutskiy 	if (key_inum(c, key) >= c->highest_inum)
3921e51764aSArtem Bityutskiy 		c->highest_inum = key_inum(c, key);
3931e51764aSArtem Bityutskiy 
3941e51764aSArtem Bityutskiy 	r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL);
3951e51764aSArtem Bityutskiy 	if (!r)
3961e51764aSArtem Bityutskiy 		return -ENOMEM;
3971e51764aSArtem Bityutskiy 
3981e51764aSArtem Bityutskiy 	if (!deletion)
3991e51764aSArtem Bityutskiy 		*used += ALIGN(len, 8);
4001e51764aSArtem Bityutskiy 	r->lnum = lnum;
4011e51764aSArtem Bityutskiy 	r->offs = offs;
4021e51764aSArtem Bityutskiy 	r->len = len;
40316a26b20SSascha Hauer 	ubifs_copy_hash(c, hash, r->hash);
404074bcb9bSArtem Bityutskiy 	r->deletion = !!deletion;
4051e51764aSArtem Bityutskiy 	r->sqnum = sqnum;
406074bcb9bSArtem Bityutskiy 	key_copy(c, key, &r->key);
4071e51764aSArtem Bityutskiy 	r->old_size = old_size;
4081e51764aSArtem Bityutskiy 	r->new_size = new_size;
4091e51764aSArtem Bityutskiy 
410debf12d5SArtem Bityutskiy 	list_add_tail(&r->list, &c->replay_list);
4111e51764aSArtem Bityutskiy 	return 0;
4121e51764aSArtem Bityutskiy }
4131e51764aSArtem Bityutskiy 
4141e51764aSArtem Bityutskiy /**
415debf12d5SArtem Bityutskiy  * insert_dent - insert a directory entry node into the replay list.
4161e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
4171e51764aSArtem Bityutskiy  * @lnum: node logical eraseblock number
4181e51764aSArtem Bityutskiy  * @offs: node offset
4191e51764aSArtem Bityutskiy  * @len: node length
4201e51764aSArtem Bityutskiy  * @key: node key
4211e51764aSArtem Bityutskiy  * @name: directory entry name
4221e51764aSArtem Bityutskiy  * @nlen: directory entry name length
4231e51764aSArtem Bityutskiy  * @sqnum: sequence number
4241e51764aSArtem Bityutskiy  * @deletion: non-zero if this is a deletion
4251e51764aSArtem Bityutskiy  * @used: number of bytes in use in a LEB
4261e51764aSArtem Bityutskiy  *
427debf12d5SArtem Bityutskiy  * This function inserts a scanned directory entry node or an extended
428debf12d5SArtem Bityutskiy  * attribute entry to the replay list. Returns zero in case of success and a
429debf12d5SArtem Bityutskiy  * negative error code in case of failure.
4301e51764aSArtem Bityutskiy  */
4311e51764aSArtem Bityutskiy static int insert_dent(struct ubifs_info *c, int lnum, int offs, int len,
43216a26b20SSascha Hauer 		       const u8 *hash, union ubifs_key *key,
43316a26b20SSascha Hauer 		       const char *name, int nlen, unsigned long long sqnum,
43416a26b20SSascha Hauer 		       int deletion, int *used)
4351e51764aSArtem Bityutskiy {
4361e51764aSArtem Bityutskiy 	struct replay_entry *r;
4371e51764aSArtem Bityutskiy 	char *nbuf;
4381e51764aSArtem Bityutskiy 
439515315a1SArtem Bityutskiy 	dbg_mntk(key, "add LEB %d:%d, key ", lnum, offs);
4401e51764aSArtem Bityutskiy 	if (key_inum(c, key) >= c->highest_inum)
4411e51764aSArtem Bityutskiy 		c->highest_inum = key_inum(c, key);
4421e51764aSArtem Bityutskiy 
4431e51764aSArtem Bityutskiy 	r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL);
4441e51764aSArtem Bityutskiy 	if (!r)
4451e51764aSArtem Bityutskiy 		return -ENOMEM;
446debf12d5SArtem Bityutskiy 
4471e51764aSArtem Bityutskiy 	nbuf = kmalloc(nlen + 1, GFP_KERNEL);
4481e51764aSArtem Bityutskiy 	if (!nbuf) {
4491e51764aSArtem Bityutskiy 		kfree(r);
4501e51764aSArtem Bityutskiy 		return -ENOMEM;
4511e51764aSArtem Bityutskiy 	}
4521e51764aSArtem Bityutskiy 
4531e51764aSArtem Bityutskiy 	if (!deletion)
4541e51764aSArtem Bityutskiy 		*used += ALIGN(len, 8);
4551e51764aSArtem Bityutskiy 	r->lnum = lnum;
4561e51764aSArtem Bityutskiy 	r->offs = offs;
4571e51764aSArtem Bityutskiy 	r->len = len;
45816a26b20SSascha Hauer 	ubifs_copy_hash(c, hash, r->hash);
459074bcb9bSArtem Bityutskiy 	r->deletion = !!deletion;
4601e51764aSArtem Bityutskiy 	r->sqnum = sqnum;
461074bcb9bSArtem Bityutskiy 	key_copy(c, key, &r->key);
462f4f61d2cSRichard Weinberger 	fname_len(&r->nm) = nlen;
4631e51764aSArtem Bityutskiy 	memcpy(nbuf, name, nlen);
4641e51764aSArtem Bityutskiy 	nbuf[nlen] = '\0';
465f4f61d2cSRichard Weinberger 	fname_name(&r->nm) = nbuf;
4661e51764aSArtem Bityutskiy 
467debf12d5SArtem Bityutskiy 	list_add_tail(&r->list, &c->replay_list);
4681e51764aSArtem Bityutskiy 	return 0;
4691e51764aSArtem Bityutskiy }
4701e51764aSArtem Bityutskiy 
4711e51764aSArtem Bityutskiy /**
4721e51764aSArtem Bityutskiy  * ubifs_validate_entry - validate directory or extended attribute entry node.
4731e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
4741e51764aSArtem Bityutskiy  * @dent: the node to validate
4751e51764aSArtem Bityutskiy  *
4761e51764aSArtem Bityutskiy  * This function validates directory or extended attribute entry node @dent.
4771e51764aSArtem Bityutskiy  * Returns zero if the node is all right and a %-EINVAL if not.
4781e51764aSArtem Bityutskiy  */
4791e51764aSArtem Bityutskiy int ubifs_validate_entry(struct ubifs_info *c,
4801e51764aSArtem Bityutskiy 			 const struct ubifs_dent_node *dent)
4811e51764aSArtem Bityutskiy {
4821e51764aSArtem Bityutskiy 	int key_type = key_type_flash(c, dent->key);
4831e51764aSArtem Bityutskiy 	int nlen = le16_to_cpu(dent->nlen);
4841e51764aSArtem Bityutskiy 
4851e51764aSArtem Bityutskiy 	if (le32_to_cpu(dent->ch.len) != nlen + UBIFS_DENT_NODE_SZ + 1 ||
4861e51764aSArtem Bityutskiy 	    dent->type >= UBIFS_ITYPES_CNT ||
4871e51764aSArtem Bityutskiy 	    nlen > UBIFS_MAX_NLEN || dent->name[nlen] != 0 ||
488304790c0SRichard Weinberger 	    (key_type == UBIFS_XENT_KEY && strnlen(dent->name, nlen) != nlen) ||
4891e51764aSArtem Bityutskiy 	    le64_to_cpu(dent->inum) > MAX_INUM) {
490235c362bSSheng Yong 		ubifs_err(c, "bad %s node", key_type == UBIFS_DENT_KEY ?
4911e51764aSArtem Bityutskiy 			  "directory entry" : "extended attribute entry");
4921e51764aSArtem Bityutskiy 		return -EINVAL;
4931e51764aSArtem Bityutskiy 	}
4941e51764aSArtem Bityutskiy 
4951e51764aSArtem Bityutskiy 	if (key_type != UBIFS_DENT_KEY && key_type != UBIFS_XENT_KEY) {
496235c362bSSheng Yong 		ubifs_err(c, "bad key type %d", key_type);
4971e51764aSArtem Bityutskiy 		return -EINVAL;
4981e51764aSArtem Bityutskiy 	}
4991e51764aSArtem Bityutskiy 
5001e51764aSArtem Bityutskiy 	return 0;
5011e51764aSArtem Bityutskiy }
5021e51764aSArtem Bityutskiy 
5031e51764aSArtem Bityutskiy /**
50491c66083SArtem Bityutskiy  * is_last_bud - check if the bud is the last in the journal head.
50591c66083SArtem Bityutskiy  * @c: UBIFS file-system description object
50691c66083SArtem Bityutskiy  * @bud: bud description object
50791c66083SArtem Bityutskiy  *
50891c66083SArtem Bityutskiy  * This function checks if bud @bud is the last bud in its journal head. This
50991c66083SArtem Bityutskiy  * information is then used by 'replay_bud()' to decide whether the bud can
51091c66083SArtem Bityutskiy  * have corruptions or not. Indeed, only last buds can be corrupted by power
51191c66083SArtem Bityutskiy  * cuts. Returns %1 if this is the last bud, and %0 if not.
51291c66083SArtem Bityutskiy  */
51391c66083SArtem Bityutskiy static int is_last_bud(struct ubifs_info *c, struct ubifs_bud *bud)
51491c66083SArtem Bityutskiy {
51591c66083SArtem Bityutskiy 	struct ubifs_jhead *jh = &c->jheads[bud->jhead];
51691c66083SArtem Bityutskiy 	struct ubifs_bud *next;
51791c66083SArtem Bityutskiy 	uint32_t data;
51891c66083SArtem Bityutskiy 	int err;
51991c66083SArtem Bityutskiy 
52091c66083SArtem Bityutskiy 	if (list_is_last(&bud->list, &jh->buds_list))
52191c66083SArtem Bityutskiy 		return 1;
52291c66083SArtem Bityutskiy 
52391c66083SArtem Bityutskiy 	/*
52491c66083SArtem Bityutskiy 	 * The following is a quirk to make sure we work correctly with UBIFS
52591c66083SArtem Bityutskiy 	 * images used with older UBIFS.
52691c66083SArtem Bityutskiy 	 *
52791c66083SArtem Bityutskiy 	 * Normally, the last bud will be the last in the journal head's list
52891c66083SArtem Bityutskiy 	 * of bud. However, there is one exception if the UBIFS image belongs
52991c66083SArtem Bityutskiy 	 * to older UBIFS. This is fairly unlikely: one would need to use old
53091c66083SArtem Bityutskiy 	 * UBIFS, then have a power cut exactly at the right point, and then
53191c66083SArtem Bityutskiy 	 * try to mount this image with new UBIFS.
53291c66083SArtem Bityutskiy 	 *
53391c66083SArtem Bityutskiy 	 * The exception is: it is possible to have 2 buds A and B, A goes
53491c66083SArtem Bityutskiy 	 * before B, and B is the last, bud B is contains no data, and bud A is
53591c66083SArtem Bityutskiy 	 * corrupted at the end. The reason is that in older versions when the
53691c66083SArtem Bityutskiy 	 * journal code switched the next bud (from A to B), it first added a
53791c66083SArtem Bityutskiy 	 * log reference node for the new bud (B), and only after this it
53891c66083SArtem Bityutskiy 	 * synchronized the write-buffer of current bud (A). But later this was
53991c66083SArtem Bityutskiy 	 * changed and UBIFS started to always synchronize the write-buffer of
54091c66083SArtem Bityutskiy 	 * the bud (A) before writing the log reference for the new bud (B).
54191c66083SArtem Bityutskiy 	 *
54291c66083SArtem Bityutskiy 	 * But because older UBIFS always synchronized A's write-buffer before
54391c66083SArtem Bityutskiy 	 * writing to B, we can recognize this exceptional situation but
54491c66083SArtem Bityutskiy 	 * checking the contents of bud B - if it is empty, then A can be
54591c66083SArtem Bityutskiy 	 * treated as the last and we can recover it.
54691c66083SArtem Bityutskiy 	 *
54791c66083SArtem Bityutskiy 	 * TODO: remove this piece of code in a couple of years (today it is
54891c66083SArtem Bityutskiy 	 * 16.05.2011).
54991c66083SArtem Bityutskiy 	 */
55091c66083SArtem Bityutskiy 	next = list_entry(bud->list.next, struct ubifs_bud, list);
55191c66083SArtem Bityutskiy 	if (!list_is_last(&next->list, &jh->buds_list))
55291c66083SArtem Bityutskiy 		return 0;
55391c66083SArtem Bityutskiy 
554d304820aSArtem Bityutskiy 	err = ubifs_leb_read(c, next->lnum, (char *)&data, next->start, 4, 1);
55591c66083SArtem Bityutskiy 	if (err)
55691c66083SArtem Bityutskiy 		return 0;
55791c66083SArtem Bityutskiy 
55891c66083SArtem Bityutskiy 	return data == 0xFFFFFFFF;
55991c66083SArtem Bityutskiy }
56091c66083SArtem Bityutskiy 
561f80df385SEric Biggers /* authenticate_sleb_hash is split out for stack usage */
562*410b6de7SArnd Bergmann static int noinline_for_stack
563*410b6de7SArnd Bergmann authenticate_sleb_hash(struct ubifs_info *c,
564*410b6de7SArnd Bergmann 		       struct shash_desc *log_hash, u8 *hash)
565eb66eff6SArnd Bergmann {
566eb66eff6SArnd Bergmann 	SHASH_DESC_ON_STACK(hash_desc, c->hash_tfm);
567eb66eff6SArnd Bergmann 
568eb66eff6SArnd Bergmann 	hash_desc->tfm = c->hash_tfm;
569eb66eff6SArnd Bergmann 
570eb66eff6SArnd Bergmann 	ubifs_shash_copy_state(c, log_hash, hash_desc);
571eb66eff6SArnd Bergmann 	return crypto_shash_final(hash_desc, hash);
572eb66eff6SArnd Bergmann }
573eb66eff6SArnd Bergmann 
57491c66083SArtem Bityutskiy /**
575da8ef65fSSascha Hauer  * authenticate_sleb - authenticate one scan LEB
576da8ef65fSSascha Hauer  * @c: UBIFS file-system description object
577da8ef65fSSascha Hauer  * @sleb: the scan LEB to authenticate
578da8ef65fSSascha Hauer  * @log_hash:
579b8f1da98SRandy Dunlap  * @is_last: if true, this is the last LEB
580da8ef65fSSascha Hauer  *
581da8ef65fSSascha Hauer  * This function iterates over the buds of a single LEB authenticating all buds
582da8ef65fSSascha Hauer  * with the authentication nodes on this LEB. Authentication nodes are written
583da8ef65fSSascha Hauer  * after some buds and contain a HMAC covering the authentication node itself
584da8ef65fSSascha Hauer  * and the buds between the last authentication node and the current
585da8ef65fSSascha Hauer  * authentication node. It can happen that the last buds cannot be authenticated
586da8ef65fSSascha Hauer  * because a powercut happened when some nodes were written but not the
587da8ef65fSSascha Hauer  * corresponding authentication node. This function returns the number of nodes
588da8ef65fSSascha Hauer  * that could be authenticated or a negative error code.
589da8ef65fSSascha Hauer  */
590da8ef65fSSascha Hauer static int authenticate_sleb(struct ubifs_info *c, struct ubifs_scan_leb *sleb,
591da8ef65fSSascha Hauer 			     struct shash_desc *log_hash, int is_last)
592da8ef65fSSascha Hauer {
593da8ef65fSSascha Hauer 	int n_not_auth = 0;
594da8ef65fSSascha Hauer 	struct ubifs_scan_node *snod;
595da8ef65fSSascha Hauer 	int n_nodes = 0;
596da8ef65fSSascha Hauer 	int err;
5973c3c32f8SEric Biggers 	u8 hash[UBIFS_HASH_ARR_SZ];
5983c3c32f8SEric Biggers 	u8 hmac[UBIFS_HMAC_ARR_SZ];
599da8ef65fSSascha Hauer 
600da8ef65fSSascha Hauer 	if (!ubifs_authenticated(c))
601da8ef65fSSascha Hauer 		return sleb->nodes_cnt;
602da8ef65fSSascha Hauer 
603da8ef65fSSascha Hauer 	list_for_each_entry(snod, &sleb->nodes, list) {
604da8ef65fSSascha Hauer 
605da8ef65fSSascha Hauer 		n_nodes++;
606da8ef65fSSascha Hauer 
607da8ef65fSSascha Hauer 		if (snod->type == UBIFS_AUTH_NODE) {
608da8ef65fSSascha Hauer 			struct ubifs_auth_node *auth = snod->node;
609da8ef65fSSascha Hauer 
610eb66eff6SArnd Bergmann 			err = authenticate_sleb_hash(c, log_hash, hash);
611da8ef65fSSascha Hauer 			if (err)
612da8ef65fSSascha Hauer 				goto out;
613da8ef65fSSascha Hauer 
614f80df385SEric Biggers 			err = crypto_shash_tfm_digest(c->hmac_tfm, hash,
615f80df385SEric Biggers 						      c->hash_len, hmac);
616da8ef65fSSascha Hauer 			if (err)
617da8ef65fSSascha Hauer 				goto out;
618da8ef65fSSascha Hauer 
619da8ef65fSSascha Hauer 			err = ubifs_check_hmac(c, auth->hmac, hmac);
620da8ef65fSSascha Hauer 			if (err) {
621da8ef65fSSascha Hauer 				err = -EPERM;
622da8ef65fSSascha Hauer 				goto out;
623da8ef65fSSascha Hauer 			}
624da8ef65fSSascha Hauer 			n_not_auth = 0;
625da8ef65fSSascha Hauer 		} else {
626da8ef65fSSascha Hauer 			err = crypto_shash_update(log_hash, snod->node,
627da8ef65fSSascha Hauer 						  snod->len);
628da8ef65fSSascha Hauer 			if (err)
629da8ef65fSSascha Hauer 				goto out;
630da8ef65fSSascha Hauer 			n_not_auth++;
631da8ef65fSSascha Hauer 		}
632da8ef65fSSascha Hauer 	}
633da8ef65fSSascha Hauer 
634da8ef65fSSascha Hauer 	/*
635da8ef65fSSascha Hauer 	 * A powercut can happen when some nodes were written, but not yet
636da8ef65fSSascha Hauer 	 * the corresponding authentication node. This may only happen on
637da8ef65fSSascha Hauer 	 * the last bud though.
638da8ef65fSSascha Hauer 	 */
639da8ef65fSSascha Hauer 	if (n_not_auth) {
640da8ef65fSSascha Hauer 		if (is_last) {
641da8ef65fSSascha Hauer 			dbg_mnt("%d unauthenticated nodes found on LEB %d, Ignoring them",
642da8ef65fSSascha Hauer 				n_not_auth, sleb->lnum);
643da8ef65fSSascha Hauer 			err = 0;
644da8ef65fSSascha Hauer 		} else {
645da8ef65fSSascha Hauer 			dbg_mnt("%d unauthenticated nodes found on non-last LEB %d",
646da8ef65fSSascha Hauer 				n_not_auth, sleb->lnum);
647da8ef65fSSascha Hauer 			err = -EPERM;
648da8ef65fSSascha Hauer 		}
649da8ef65fSSascha Hauer 	} else {
650da8ef65fSSascha Hauer 		err = 0;
651da8ef65fSSascha Hauer 	}
652da8ef65fSSascha Hauer out:
653da8ef65fSSascha Hauer 	return err ? err : n_nodes - n_not_auth;
654da8ef65fSSascha Hauer }
655da8ef65fSSascha Hauer 
656da8ef65fSSascha Hauer /**
6571e51764aSArtem Bityutskiy  * replay_bud - replay a bud logical eraseblock.
6581e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
659e76a4526SArtem Bityutskiy  * @b: bud entry which describes the bud
6601e51764aSArtem Bityutskiy  *
661e76a4526SArtem Bityutskiy  * This function replays bud @bud, recovers it if needed, and adds all nodes
662e76a4526SArtem Bityutskiy  * from this bud to the replay list. Returns zero in case of success and a
663e76a4526SArtem Bityutskiy  * negative error code in case of failure.
6641e51764aSArtem Bityutskiy  */
665e76a4526SArtem Bityutskiy static int replay_bud(struct ubifs_info *c, struct bud_entry *b)
6661e51764aSArtem Bityutskiy {
66791c66083SArtem Bityutskiy 	int is_last = is_last_bud(c, b->bud);
668e76a4526SArtem Bityutskiy 	int err = 0, used = 0, lnum = b->bud->lnum, offs = b->bud->start;
669da8ef65fSSascha Hauer 	int n_nodes, n = 0;
6701e51764aSArtem Bityutskiy 	struct ubifs_scan_leb *sleb;
6711e51764aSArtem Bityutskiy 	struct ubifs_scan_node *snod;
6721e51764aSArtem Bityutskiy 
67391c66083SArtem Bityutskiy 	dbg_mnt("replay bud LEB %d, head %d, offs %d, is_last %d",
67491c66083SArtem Bityutskiy 		lnum, b->bud->jhead, offs, is_last);
675e76a4526SArtem Bityutskiy 
67691c66083SArtem Bityutskiy 	if (c->need_recovery && is_last)
67791c66083SArtem Bityutskiy 		/*
67891c66083SArtem Bityutskiy 		 * Recover only last LEBs in the journal heads, because power
67991c66083SArtem Bityutskiy 		 * cuts may cause corruptions only in these LEBs, because only
68091c66083SArtem Bityutskiy 		 * these LEBs could possibly be written to at the power cut
68191c66083SArtem Bityutskiy 		 * time.
68291c66083SArtem Bityutskiy 		 */
683efcfde54SArtem Bityutskiy 		sleb = ubifs_recover_leb(c, lnum, offs, c->sbuf, b->bud->jhead);
6841e51764aSArtem Bityutskiy 	else
685348709baSArtem Bityutskiy 		sleb = ubifs_scan(c, lnum, offs, c->sbuf, 0);
6861e51764aSArtem Bityutskiy 	if (IS_ERR(sleb))
6871e51764aSArtem Bityutskiy 		return PTR_ERR(sleb);
6881e51764aSArtem Bityutskiy 
689da8ef65fSSascha Hauer 	n_nodes = authenticate_sleb(c, sleb, b->bud->log_hash, is_last);
690da8ef65fSSascha Hauer 	if (n_nodes < 0) {
691da8ef65fSSascha Hauer 		err = n_nodes;
692da8ef65fSSascha Hauer 		goto out;
693da8ef65fSSascha Hauer 	}
694da8ef65fSSascha Hauer 
695da8ef65fSSascha Hauer 	ubifs_shash_copy_state(c, b->bud->log_hash,
696da8ef65fSSascha Hauer 			       c->jheads[b->bud->jhead].log_hash);
697da8ef65fSSascha Hauer 
6981e51764aSArtem Bityutskiy 	/*
6991e51764aSArtem Bityutskiy 	 * The bud does not have to start from offset zero - the beginning of
7001e51764aSArtem Bityutskiy 	 * the 'lnum' LEB may contain previously committed data. One of the
7011e51764aSArtem Bityutskiy 	 * things we have to do in replay is to correctly update lprops with
7021e51764aSArtem Bityutskiy 	 * newer information about this LEB.
7031e51764aSArtem Bityutskiy 	 *
7041e51764aSArtem Bityutskiy 	 * At this point lprops thinks that this LEB has 'c->leb_size - offs'
7051e51764aSArtem Bityutskiy 	 * bytes of free space because it only contain information about
7061e51764aSArtem Bityutskiy 	 * committed data.
7071e51764aSArtem Bityutskiy 	 *
7081e51764aSArtem Bityutskiy 	 * But we know that real amount of free space is 'c->leb_size -
7091e51764aSArtem Bityutskiy 	 * sleb->endpt', and the space in the 'lnum' LEB between 'offs' and
7101e51764aSArtem Bityutskiy 	 * 'sleb->endpt' is used by bud data. We have to correctly calculate
7111e51764aSArtem Bityutskiy 	 * how much of these data are dirty and update lprops with this
7121e51764aSArtem Bityutskiy 	 * information.
7131e51764aSArtem Bityutskiy 	 *
7141e51764aSArtem Bityutskiy 	 * The dirt in that LEB region is comprised of padding nodes, deletion
7151e51764aSArtem Bityutskiy 	 * nodes, truncation nodes and nodes which are obsoleted by subsequent
7161e51764aSArtem Bityutskiy 	 * nodes in this LEB. So instead of calculating clean space, we
7171e51764aSArtem Bityutskiy 	 * calculate used space ('used' variable).
7181e51764aSArtem Bityutskiy 	 */
7191e51764aSArtem Bityutskiy 
7201e51764aSArtem Bityutskiy 	list_for_each_entry(snod, &sleb->nodes, list) {
72116a26b20SSascha Hauer 		u8 hash[UBIFS_HASH_ARR_SZ];
7221e51764aSArtem Bityutskiy 		int deletion = 0;
7231e51764aSArtem Bityutskiy 
7241e51764aSArtem Bityutskiy 		cond_resched();
7251e51764aSArtem Bityutskiy 
7261e51764aSArtem Bityutskiy 		if (snod->sqnum >= SQNUM_WATERMARK) {
727235c362bSSheng Yong 			ubifs_err(c, "file system's life ended");
7281e51764aSArtem Bityutskiy 			goto out_dump;
7291e51764aSArtem Bityutskiy 		}
7301e51764aSArtem Bityutskiy 
73116a26b20SSascha Hauer 		ubifs_node_calc_hash(c, snod->node, hash);
73216a26b20SSascha Hauer 
7331e51764aSArtem Bityutskiy 		if (snod->sqnum > c->max_sqnum)
7341e51764aSArtem Bityutskiy 			c->max_sqnum = snod->sqnum;
7351e51764aSArtem Bityutskiy 
7361e51764aSArtem Bityutskiy 		switch (snod->type) {
7371e51764aSArtem Bityutskiy 		case UBIFS_INO_NODE:
7381e51764aSArtem Bityutskiy 		{
7391e51764aSArtem Bityutskiy 			struct ubifs_ino_node *ino = snod->node;
7401e51764aSArtem Bityutskiy 			loff_t new_size = le64_to_cpu(ino->size);
7411e51764aSArtem Bityutskiy 
7421e51764aSArtem Bityutskiy 			if (le32_to_cpu(ino->nlink) == 0)
7431e51764aSArtem Bityutskiy 				deletion = 1;
74416a26b20SSascha Hauer 			err = insert_node(c, lnum, snod->offs, snod->len, hash,
7451e51764aSArtem Bityutskiy 					  &snod->key, snod->sqnum, deletion,
7461e51764aSArtem Bityutskiy 					  &used, 0, new_size);
7471e51764aSArtem Bityutskiy 			break;
7481e51764aSArtem Bityutskiy 		}
7491e51764aSArtem Bityutskiy 		case UBIFS_DATA_NODE:
7501e51764aSArtem Bityutskiy 		{
7511e51764aSArtem Bityutskiy 			struct ubifs_data_node *dn = snod->node;
7521e51764aSArtem Bityutskiy 			loff_t new_size = le32_to_cpu(dn->size) +
7531e51764aSArtem Bityutskiy 					  key_block(c, &snod->key) *
7541e51764aSArtem Bityutskiy 					  UBIFS_BLOCK_SIZE;
7551e51764aSArtem Bityutskiy 
75616a26b20SSascha Hauer 			err = insert_node(c, lnum, snod->offs, snod->len, hash,
7571e51764aSArtem Bityutskiy 					  &snod->key, snod->sqnum, deletion,
7581e51764aSArtem Bityutskiy 					  &used, 0, new_size);
7591e51764aSArtem Bityutskiy 			break;
7601e51764aSArtem Bityutskiy 		}
7611e51764aSArtem Bityutskiy 		case UBIFS_DENT_NODE:
7621e51764aSArtem Bityutskiy 		case UBIFS_XENT_NODE:
7631e51764aSArtem Bityutskiy 		{
7641e51764aSArtem Bityutskiy 			struct ubifs_dent_node *dent = snod->node;
7651e51764aSArtem Bityutskiy 
7661e51764aSArtem Bityutskiy 			err = ubifs_validate_entry(c, dent);
7671e51764aSArtem Bityutskiy 			if (err)
7681e51764aSArtem Bityutskiy 				goto out_dump;
7691e51764aSArtem Bityutskiy 
77016a26b20SSascha Hauer 			err = insert_dent(c, lnum, snod->offs, snod->len, hash,
7711e51764aSArtem Bityutskiy 					  &snod->key, dent->name,
7721e51764aSArtem Bityutskiy 					  le16_to_cpu(dent->nlen), snod->sqnum,
7731e51764aSArtem Bityutskiy 					  !le64_to_cpu(dent->inum), &used);
7741e51764aSArtem Bityutskiy 			break;
7751e51764aSArtem Bityutskiy 		}
7761e51764aSArtem Bityutskiy 		case UBIFS_TRUN_NODE:
7771e51764aSArtem Bityutskiy 		{
7781e51764aSArtem Bityutskiy 			struct ubifs_trun_node *trun = snod->node;
7791e51764aSArtem Bityutskiy 			loff_t old_size = le64_to_cpu(trun->old_size);
7801e51764aSArtem Bityutskiy 			loff_t new_size = le64_to_cpu(trun->new_size);
7811e51764aSArtem Bityutskiy 			union ubifs_key key;
7821e51764aSArtem Bityutskiy 
7831e51764aSArtem Bityutskiy 			/* Validate truncation node */
7841e51764aSArtem Bityutskiy 			if (old_size < 0 || old_size > c->max_inode_sz ||
7851e51764aSArtem Bityutskiy 			    new_size < 0 || new_size > c->max_inode_sz ||
7861e51764aSArtem Bityutskiy 			    old_size <= new_size) {
787235c362bSSheng Yong 				ubifs_err(c, "bad truncation node");
7881e51764aSArtem Bityutskiy 				goto out_dump;
7891e51764aSArtem Bityutskiy 			}
7901e51764aSArtem Bityutskiy 
7911e51764aSArtem Bityutskiy 			/*
7921e51764aSArtem Bityutskiy 			 * Create a fake truncation key just to use the same
7931e51764aSArtem Bityutskiy 			 * functions which expect nodes to have keys.
7941e51764aSArtem Bityutskiy 			 */
7951e51764aSArtem Bityutskiy 			trun_key_init(c, &key, le32_to_cpu(trun->inum));
79616a26b20SSascha Hauer 			err = insert_node(c, lnum, snod->offs, snod->len, hash,
7971e51764aSArtem Bityutskiy 					  &key, snod->sqnum, 1, &used,
7981e51764aSArtem Bityutskiy 					  old_size, new_size);
7991e51764aSArtem Bityutskiy 			break;
8001e51764aSArtem Bityutskiy 		}
8016a98bc46SSascha Hauer 		case UBIFS_AUTH_NODE:
8026a98bc46SSascha Hauer 			break;
8031e51764aSArtem Bityutskiy 		default:
804235c362bSSheng Yong 			ubifs_err(c, "unexpected node type %d in bud LEB %d:%d",
8051e51764aSArtem Bityutskiy 				  snod->type, lnum, snod->offs);
8061e51764aSArtem Bityutskiy 			err = -EINVAL;
8071e51764aSArtem Bityutskiy 			goto out_dump;
8081e51764aSArtem Bityutskiy 		}
8091e51764aSArtem Bityutskiy 		if (err)
8101e51764aSArtem Bityutskiy 			goto out;
811da8ef65fSSascha Hauer 
812da8ef65fSSascha Hauer 		n++;
813da8ef65fSSascha Hauer 		if (n == n_nodes)
814da8ef65fSSascha Hauer 			break;
8151e51764aSArtem Bityutskiy 	}
8161e51764aSArtem Bityutskiy 
8176eb61d58SRichard Weinberger 	ubifs_assert(c, ubifs_search_bud(c, lnum));
8186eb61d58SRichard Weinberger 	ubifs_assert(c, sleb->endpt - offs >= used);
8196eb61d58SRichard Weinberger 	ubifs_assert(c, sleb->endpt % c->min_io_size == 0);
8201e51764aSArtem Bityutskiy 
821e76a4526SArtem Bityutskiy 	b->dirty = sleb->endpt - offs - used;
822e76a4526SArtem Bityutskiy 	b->free = c->leb_size - sleb->endpt;
82379fda517SArtem Bityutskiy 	dbg_mnt("bud LEB %d replied: dirty %d, free %d",
82479fda517SArtem Bityutskiy 		lnum, b->dirty, b->free);
8251e51764aSArtem Bityutskiy 
8261e51764aSArtem Bityutskiy out:
8271e51764aSArtem Bityutskiy 	ubifs_scan_destroy(sleb);
8281e51764aSArtem Bityutskiy 	return err;
8291e51764aSArtem Bityutskiy 
8301e51764aSArtem Bityutskiy out_dump:
831235c362bSSheng Yong 	ubifs_err(c, "bad node is at LEB %d:%d", lnum, snod->offs);
832a33e30a0SZhihao Cheng 	ubifs_dump_node(c, snod->node, c->leb_size - snod->offs);
8331e51764aSArtem Bityutskiy 	ubifs_scan_destroy(sleb);
8341e51764aSArtem Bityutskiy 	return -EINVAL;
8351e51764aSArtem Bityutskiy }
8361e51764aSArtem Bityutskiy 
8371e51764aSArtem Bityutskiy /**
8381e51764aSArtem Bityutskiy  * replay_buds - replay all buds.
8391e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
8401e51764aSArtem Bityutskiy  *
8411e51764aSArtem Bityutskiy  * This function returns zero in case of success and a negative error code in
8421e51764aSArtem Bityutskiy  * case of failure.
8431e51764aSArtem Bityutskiy  */
8441e51764aSArtem Bityutskiy static int replay_buds(struct ubifs_info *c)
8451e51764aSArtem Bityutskiy {
8461e51764aSArtem Bityutskiy 	struct bud_entry *b;
847074bcb9bSArtem Bityutskiy 	int err;
8487703f09dSArtem Bityutskiy 	unsigned long long prev_sqnum = 0;
8491e51764aSArtem Bityutskiy 
8501e51764aSArtem Bityutskiy 	list_for_each_entry(b, &c->replay_buds, list) {
851e76a4526SArtem Bityutskiy 		err = replay_bud(c, b);
8521e51764aSArtem Bityutskiy 		if (err)
8531e51764aSArtem Bityutskiy 			return err;
8547703f09dSArtem Bityutskiy 
8556eb61d58SRichard Weinberger 		ubifs_assert(c, b->sqnum > prev_sqnum);
8567703f09dSArtem Bityutskiy 		prev_sqnum = b->sqnum;
8571e51764aSArtem Bityutskiy 	}
8581e51764aSArtem Bityutskiy 
8591e51764aSArtem Bityutskiy 	return 0;
8601e51764aSArtem Bityutskiy }
8611e51764aSArtem Bityutskiy 
8621e51764aSArtem Bityutskiy /**
8631e51764aSArtem Bityutskiy  * destroy_bud_list - destroy the list of buds to replay.
8641e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
8651e51764aSArtem Bityutskiy  */
8661e51764aSArtem Bityutskiy static void destroy_bud_list(struct ubifs_info *c)
8671e51764aSArtem Bityutskiy {
8681e51764aSArtem Bityutskiy 	struct bud_entry *b;
8691e51764aSArtem Bityutskiy 
8701e51764aSArtem Bityutskiy 	while (!list_empty(&c->replay_buds)) {
8711e51764aSArtem Bityutskiy 		b = list_entry(c->replay_buds.next, struct bud_entry, list);
8721e51764aSArtem Bityutskiy 		list_del(&b->list);
8731e51764aSArtem Bityutskiy 		kfree(b);
8741e51764aSArtem Bityutskiy 	}
8751e51764aSArtem Bityutskiy }
8761e51764aSArtem Bityutskiy 
8771e51764aSArtem Bityutskiy /**
8781e51764aSArtem Bityutskiy  * add_replay_bud - add a bud to the list of buds to replay.
8791e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
8801e51764aSArtem Bityutskiy  * @lnum: bud logical eraseblock number to replay
8811e51764aSArtem Bityutskiy  * @offs: bud start offset
8821e51764aSArtem Bityutskiy  * @jhead: journal head to which this bud belongs
8831e51764aSArtem Bityutskiy  * @sqnum: reference node sequence number
8841e51764aSArtem Bityutskiy  *
8851e51764aSArtem Bityutskiy  * This function returns zero in case of success and a negative error code in
8861e51764aSArtem Bityutskiy  * case of failure.
8871e51764aSArtem Bityutskiy  */
8881e51764aSArtem Bityutskiy static int add_replay_bud(struct ubifs_info *c, int lnum, int offs, int jhead,
8891e51764aSArtem Bityutskiy 			  unsigned long long sqnum)
8901e51764aSArtem Bityutskiy {
8911e51764aSArtem Bityutskiy 	struct ubifs_bud *bud;
8921e51764aSArtem Bityutskiy 	struct bud_entry *b;
893da8ef65fSSascha Hauer 	int err;
8941e51764aSArtem Bityutskiy 
8951e51764aSArtem Bityutskiy 	dbg_mnt("add replay bud LEB %d:%d, head %d", lnum, offs, jhead);
8961e51764aSArtem Bityutskiy 
8971e51764aSArtem Bityutskiy 	bud = kmalloc(sizeof(struct ubifs_bud), GFP_KERNEL);
8981e51764aSArtem Bityutskiy 	if (!bud)
8991e51764aSArtem Bityutskiy 		return -ENOMEM;
9001e51764aSArtem Bityutskiy 
9011e51764aSArtem Bityutskiy 	b = kmalloc(sizeof(struct bud_entry), GFP_KERNEL);
9021e51764aSArtem Bityutskiy 	if (!b) {
903da8ef65fSSascha Hauer 		err = -ENOMEM;
904da8ef65fSSascha Hauer 		goto out;
9051e51764aSArtem Bityutskiy 	}
9061e51764aSArtem Bityutskiy 
9071e51764aSArtem Bityutskiy 	bud->lnum = lnum;
9081e51764aSArtem Bityutskiy 	bud->start = offs;
9091e51764aSArtem Bityutskiy 	bud->jhead = jhead;
910da8ef65fSSascha Hauer 	bud->log_hash = ubifs_hash_get_desc(c);
911da8ef65fSSascha Hauer 	if (IS_ERR(bud->log_hash)) {
912da8ef65fSSascha Hauer 		err = PTR_ERR(bud->log_hash);
913da8ef65fSSascha Hauer 		goto out;
914da8ef65fSSascha Hauer 	}
915da8ef65fSSascha Hauer 
916da8ef65fSSascha Hauer 	ubifs_shash_copy_state(c, c->log_hash, bud->log_hash);
917da8ef65fSSascha Hauer 
9181e51764aSArtem Bityutskiy 	ubifs_add_bud(c, bud);
9191e51764aSArtem Bityutskiy 
9201e51764aSArtem Bityutskiy 	b->bud = bud;
9211e51764aSArtem Bityutskiy 	b->sqnum = sqnum;
9221e51764aSArtem Bityutskiy 	list_add_tail(&b->list, &c->replay_buds);
9231e51764aSArtem Bityutskiy 
9241e51764aSArtem Bityutskiy 	return 0;
925da8ef65fSSascha Hauer out:
926da8ef65fSSascha Hauer 	kfree(bud);
927da8ef65fSSascha Hauer 	kfree(b);
928da8ef65fSSascha Hauer 
929da8ef65fSSascha Hauer 	return err;
9301e51764aSArtem Bityutskiy }
9311e51764aSArtem Bityutskiy 
9321e51764aSArtem Bityutskiy /**
9331e51764aSArtem Bityutskiy  * validate_ref - validate a reference node.
9341e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
9351e51764aSArtem Bityutskiy  * @ref: the reference node to validate
9361e51764aSArtem Bityutskiy  *
9371e51764aSArtem Bityutskiy  * This function returns %1 if a bud reference already exists for the LEB. %0 is
9381e51764aSArtem Bityutskiy  * returned if the reference node is new, otherwise %-EINVAL is returned if
9391e51764aSArtem Bityutskiy  * validation failed.
9401e51764aSArtem Bityutskiy  */
9411e51764aSArtem Bityutskiy static int validate_ref(struct ubifs_info *c, const struct ubifs_ref_node *ref)
9421e51764aSArtem Bityutskiy {
9431e51764aSArtem Bityutskiy 	struct ubifs_bud *bud;
9441e51764aSArtem Bityutskiy 	int lnum = le32_to_cpu(ref->lnum);
9451e51764aSArtem Bityutskiy 	unsigned int offs = le32_to_cpu(ref->offs);
9461e51764aSArtem Bityutskiy 	unsigned int jhead = le32_to_cpu(ref->jhead);
9471e51764aSArtem Bityutskiy 
9481e51764aSArtem Bityutskiy 	/*
9491e51764aSArtem Bityutskiy 	 * ref->offs may point to the end of LEB when the journal head points
9501e51764aSArtem Bityutskiy 	 * to the end of LEB and we write reference node for it during commit.
9511e51764aSArtem Bityutskiy 	 * So this is why we require 'offs > c->leb_size'.
9521e51764aSArtem Bityutskiy 	 */
9531e51764aSArtem Bityutskiy 	if (jhead >= c->jhead_cnt || lnum >= c->leb_cnt ||
9541e51764aSArtem Bityutskiy 	    lnum < c->main_first || offs > c->leb_size ||
9551e51764aSArtem Bityutskiy 	    offs & (c->min_io_size - 1))
9561e51764aSArtem Bityutskiy 		return -EINVAL;
9571e51764aSArtem Bityutskiy 
9581e51764aSArtem Bityutskiy 	/* Make sure we have not already looked at this bud */
9591e51764aSArtem Bityutskiy 	bud = ubifs_search_bud(c, lnum);
9601e51764aSArtem Bityutskiy 	if (bud) {
9611e51764aSArtem Bityutskiy 		if (bud->jhead == jhead && bud->start <= offs)
9621e51764aSArtem Bityutskiy 			return 1;
963235c362bSSheng Yong 		ubifs_err(c, "bud at LEB %d:%d was already referred", lnum, offs);
9641e51764aSArtem Bityutskiy 		return -EINVAL;
9651e51764aSArtem Bityutskiy 	}
9661e51764aSArtem Bityutskiy 
9671e51764aSArtem Bityutskiy 	return 0;
9681e51764aSArtem Bityutskiy }
9691e51764aSArtem Bityutskiy 
9701e51764aSArtem Bityutskiy /**
9711e51764aSArtem Bityutskiy  * replay_log_leb - replay a log logical eraseblock.
9721e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
9731e51764aSArtem Bityutskiy  * @lnum: log logical eraseblock to replay
9741e51764aSArtem Bityutskiy  * @offs: offset to start replaying from
9751e51764aSArtem Bityutskiy  * @sbuf: scan buffer
9761e51764aSArtem Bityutskiy  *
9771e51764aSArtem Bityutskiy  * This function replays a log LEB and returns zero in case of success, %1 if
9781e51764aSArtem Bityutskiy  * this is the last LEB in the log, and a negative error code in case of
9791e51764aSArtem Bityutskiy  * failure.
9801e51764aSArtem Bityutskiy  */
9811e51764aSArtem Bityutskiy static int replay_log_leb(struct ubifs_info *c, int lnum, int offs, void *sbuf)
9821e51764aSArtem Bityutskiy {
9831e51764aSArtem Bityutskiy 	int err;
9841e51764aSArtem Bityutskiy 	struct ubifs_scan_leb *sleb;
9851e51764aSArtem Bityutskiy 	struct ubifs_scan_node *snod;
9861e51764aSArtem Bityutskiy 	const struct ubifs_cs_node *node;
9871e51764aSArtem Bityutskiy 
9881e51764aSArtem Bityutskiy 	dbg_mnt("replay log LEB %d:%d", lnum, offs);
989348709baSArtem Bityutskiy 	sleb = ubifs_scan(c, lnum, offs, sbuf, c->need_recovery);
9901e51764aSArtem Bityutskiy 	if (IS_ERR(sleb)) {
991ed43f2f0SArtem Bityutskiy 		if (PTR_ERR(sleb) != -EUCLEAN || !c->need_recovery)
992ed43f2f0SArtem Bityutskiy 			return PTR_ERR(sleb);
9937d08ae3cSArtem Bityutskiy 		/*
9947d08ae3cSArtem Bityutskiy 		 * Note, the below function will recover this log LEB only if
9957d08ae3cSArtem Bityutskiy 		 * it is the last, because unclean reboots can possibly corrupt
9967d08ae3cSArtem Bityutskiy 		 * only the tail of the log.
9977d08ae3cSArtem Bityutskiy 		 */
9981e51764aSArtem Bityutskiy 		sleb = ubifs_recover_log_leb(c, lnum, offs, sbuf);
9991e51764aSArtem Bityutskiy 		if (IS_ERR(sleb))
10001e51764aSArtem Bityutskiy 			return PTR_ERR(sleb);
10011e51764aSArtem Bityutskiy 	}
10021e51764aSArtem Bityutskiy 
10031e51764aSArtem Bityutskiy 	if (sleb->nodes_cnt == 0) {
10041e51764aSArtem Bityutskiy 		err = 1;
10051e51764aSArtem Bityutskiy 		goto out;
10061e51764aSArtem Bityutskiy 	}
10071e51764aSArtem Bityutskiy 
10081e51764aSArtem Bityutskiy 	node = sleb->buf;
10091e51764aSArtem Bityutskiy 	snod = list_entry(sleb->nodes.next, struct ubifs_scan_node, list);
10101e51764aSArtem Bityutskiy 	if (c->cs_sqnum == 0) {
10111e51764aSArtem Bityutskiy 		/*
10121e51764aSArtem Bityutskiy 		 * This is the first log LEB we are looking at, make sure that
10131e51764aSArtem Bityutskiy 		 * the first node is a commit start node. Also record its
10141e51764aSArtem Bityutskiy 		 * sequence number so that UBIFS can determine where the log
10151e51764aSArtem Bityutskiy 		 * ends, because all nodes which were have higher sequence
10161e51764aSArtem Bityutskiy 		 * numbers.
10171e51764aSArtem Bityutskiy 		 */
10181e51764aSArtem Bityutskiy 		if (snod->type != UBIFS_CS_NODE) {
1019235c362bSSheng Yong 			ubifs_err(c, "first log node at LEB %d:%d is not CS node",
10201e51764aSArtem Bityutskiy 				  lnum, offs);
10211e51764aSArtem Bityutskiy 			goto out_dump;
10221e51764aSArtem Bityutskiy 		}
10231e51764aSArtem Bityutskiy 		if (le64_to_cpu(node->cmt_no) != c->cmt_no) {
1024235c362bSSheng Yong 			ubifs_err(c, "first CS node at LEB %d:%d has wrong commit number %llu expected %llu",
10251e51764aSArtem Bityutskiy 				  lnum, offs,
10261e51764aSArtem Bityutskiy 				  (unsigned long long)le64_to_cpu(node->cmt_no),
10271e51764aSArtem Bityutskiy 				  c->cmt_no);
10281e51764aSArtem Bityutskiy 			goto out_dump;
10291e51764aSArtem Bityutskiy 		}
10301e51764aSArtem Bityutskiy 
10311e51764aSArtem Bityutskiy 		c->cs_sqnum = le64_to_cpu(node->ch.sqnum);
10321e51764aSArtem Bityutskiy 		dbg_mnt("commit start sqnum %llu", c->cs_sqnum);
1033da8ef65fSSascha Hauer 
1034da8ef65fSSascha Hauer 		err = ubifs_shash_init(c, c->log_hash);
1035da8ef65fSSascha Hauer 		if (err)
1036da8ef65fSSascha Hauer 			goto out;
1037da8ef65fSSascha Hauer 
1038da8ef65fSSascha Hauer 		err = ubifs_shash_update(c, c->log_hash, node, UBIFS_CS_NODE_SZ);
1039da8ef65fSSascha Hauer 		if (err < 0)
1040da8ef65fSSascha Hauer 			goto out;
10411e51764aSArtem Bityutskiy 	}
10421e51764aSArtem Bityutskiy 
10431e51764aSArtem Bityutskiy 	if (snod->sqnum < c->cs_sqnum) {
10441e51764aSArtem Bityutskiy 		/*
10451e51764aSArtem Bityutskiy 		 * This means that we reached end of log and now
10461e51764aSArtem Bityutskiy 		 * look to the older log data, which was already
10471e51764aSArtem Bityutskiy 		 * committed but the eraseblock was not erased (UBIFS
10486edbfafdSArtem Bityutskiy 		 * only un-maps it). So this basically means we have to
10491e51764aSArtem Bityutskiy 		 * exit with "end of log" code.
10501e51764aSArtem Bityutskiy 		 */
10511e51764aSArtem Bityutskiy 		err = 1;
10521e51764aSArtem Bityutskiy 		goto out;
10531e51764aSArtem Bityutskiy 	}
10541e51764aSArtem Bityutskiy 
10551e51764aSArtem Bityutskiy 	/* Make sure the first node sits at offset zero of the LEB */
10561e51764aSArtem Bityutskiy 	if (snod->offs != 0) {
1057235c362bSSheng Yong 		ubifs_err(c, "first node is not at zero offset");
10581e51764aSArtem Bityutskiy 		goto out_dump;
10591e51764aSArtem Bityutskiy 	}
10601e51764aSArtem Bityutskiy 
10611e51764aSArtem Bityutskiy 	list_for_each_entry(snod, &sleb->nodes, list) {
10621e51764aSArtem Bityutskiy 		cond_resched();
10631e51764aSArtem Bityutskiy 
10641e51764aSArtem Bityutskiy 		if (snod->sqnum >= SQNUM_WATERMARK) {
1065235c362bSSheng Yong 			ubifs_err(c, "file system's life ended");
10661e51764aSArtem Bityutskiy 			goto out_dump;
10671e51764aSArtem Bityutskiy 		}
10681e51764aSArtem Bityutskiy 
10691e51764aSArtem Bityutskiy 		if (snod->sqnum < c->cs_sqnum) {
1070235c362bSSheng Yong 			ubifs_err(c, "bad sqnum %llu, commit sqnum %llu",
10711e51764aSArtem Bityutskiy 				  snod->sqnum, c->cs_sqnum);
10721e51764aSArtem Bityutskiy 			goto out_dump;
10731e51764aSArtem Bityutskiy 		}
10741e51764aSArtem Bityutskiy 
10751e51764aSArtem Bityutskiy 		if (snod->sqnum > c->max_sqnum)
10761e51764aSArtem Bityutskiy 			c->max_sqnum = snod->sqnum;
10771e51764aSArtem Bityutskiy 
10781e51764aSArtem Bityutskiy 		switch (snod->type) {
10791e51764aSArtem Bityutskiy 		case UBIFS_REF_NODE: {
10801e51764aSArtem Bityutskiy 			const struct ubifs_ref_node *ref = snod->node;
10811e51764aSArtem Bityutskiy 
10821e51764aSArtem Bityutskiy 			err = validate_ref(c, ref);
10831e51764aSArtem Bityutskiy 			if (err == 1)
10841e51764aSArtem Bityutskiy 				break; /* Already have this bud */
10851e51764aSArtem Bityutskiy 			if (err)
10861e51764aSArtem Bityutskiy 				goto out_dump;
10871e51764aSArtem Bityutskiy 
1088da8ef65fSSascha Hauer 			err = ubifs_shash_update(c, c->log_hash, ref,
1089da8ef65fSSascha Hauer 						 UBIFS_REF_NODE_SZ);
1090da8ef65fSSascha Hauer 			if (err)
1091da8ef65fSSascha Hauer 				goto out;
1092da8ef65fSSascha Hauer 
10931e51764aSArtem Bityutskiy 			err = add_replay_bud(c, le32_to_cpu(ref->lnum),
10941e51764aSArtem Bityutskiy 					     le32_to_cpu(ref->offs),
10951e51764aSArtem Bityutskiy 					     le32_to_cpu(ref->jhead),
10961e51764aSArtem Bityutskiy 					     snod->sqnum);
10971e51764aSArtem Bityutskiy 			if (err)
10981e51764aSArtem Bityutskiy 				goto out;
10991e51764aSArtem Bityutskiy 
11001e51764aSArtem Bityutskiy 			break;
11011e51764aSArtem Bityutskiy 		}
11021e51764aSArtem Bityutskiy 		case UBIFS_CS_NODE:
11031e51764aSArtem Bityutskiy 			/* Make sure it sits at the beginning of LEB */
11041e51764aSArtem Bityutskiy 			if (snod->offs != 0) {
1105235c362bSSheng Yong 				ubifs_err(c, "unexpected node in log");
11061e51764aSArtem Bityutskiy 				goto out_dump;
11071e51764aSArtem Bityutskiy 			}
11081e51764aSArtem Bityutskiy 			break;
11091e51764aSArtem Bityutskiy 		default:
1110235c362bSSheng Yong 			ubifs_err(c, "unexpected node in log");
11111e51764aSArtem Bityutskiy 			goto out_dump;
11121e51764aSArtem Bityutskiy 		}
11131e51764aSArtem Bityutskiy 	}
11141e51764aSArtem Bityutskiy 
11151e51764aSArtem Bityutskiy 	if (sleb->endpt || c->lhead_offs >= c->leb_size) {
11161e51764aSArtem Bityutskiy 		c->lhead_lnum = lnum;
11171e51764aSArtem Bityutskiy 		c->lhead_offs = sleb->endpt;
11181e51764aSArtem Bityutskiy 	}
11191e51764aSArtem Bityutskiy 
11201e51764aSArtem Bityutskiy 	err = !sleb->endpt;
11211e51764aSArtem Bityutskiy out:
11221e51764aSArtem Bityutskiy 	ubifs_scan_destroy(sleb);
11231e51764aSArtem Bityutskiy 	return err;
11241e51764aSArtem Bityutskiy 
11251e51764aSArtem Bityutskiy out_dump:
1126235c362bSSheng Yong 	ubifs_err(c, "log error detected while replaying the log at LEB %d:%d",
11271e51764aSArtem Bityutskiy 		  lnum, offs + snod->offs);
1128a33e30a0SZhihao Cheng 	ubifs_dump_node(c, snod->node, c->leb_size - snod->offs);
11291e51764aSArtem Bityutskiy 	ubifs_scan_destroy(sleb);
11301e51764aSArtem Bityutskiy 	return -EINVAL;
11311e51764aSArtem Bityutskiy }
11321e51764aSArtem Bityutskiy 
11331e51764aSArtem Bityutskiy /**
11341e51764aSArtem Bityutskiy  * take_ihead - update the status of the index head in lprops to 'taken'.
11351e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
11361e51764aSArtem Bityutskiy  *
11371e51764aSArtem Bityutskiy  * This function returns the amount of free space in the index head LEB or a
11381e51764aSArtem Bityutskiy  * negative error code.
11391e51764aSArtem Bityutskiy  */
11401e51764aSArtem Bityutskiy static int take_ihead(struct ubifs_info *c)
11411e51764aSArtem Bityutskiy {
11421e51764aSArtem Bityutskiy 	const struct ubifs_lprops *lp;
11431e51764aSArtem Bityutskiy 	int err, free;
11441e51764aSArtem Bityutskiy 
11451e51764aSArtem Bityutskiy 	ubifs_get_lprops(c);
11461e51764aSArtem Bityutskiy 
11471e51764aSArtem Bityutskiy 	lp = ubifs_lpt_lookup_dirty(c, c->ihead_lnum);
11481e51764aSArtem Bityutskiy 	if (IS_ERR(lp)) {
11491e51764aSArtem Bityutskiy 		err = PTR_ERR(lp);
11501e51764aSArtem Bityutskiy 		goto out;
11511e51764aSArtem Bityutskiy 	}
11521e51764aSArtem Bityutskiy 
11531e51764aSArtem Bityutskiy 	free = lp->free;
11541e51764aSArtem Bityutskiy 
11551e51764aSArtem Bityutskiy 	lp = ubifs_change_lp(c, lp, LPROPS_NC, LPROPS_NC,
11561e51764aSArtem Bityutskiy 			     lp->flags | LPROPS_TAKEN, 0);
11571e51764aSArtem Bityutskiy 	if (IS_ERR(lp)) {
11581e51764aSArtem Bityutskiy 		err = PTR_ERR(lp);
11591e51764aSArtem Bityutskiy 		goto out;
11601e51764aSArtem Bityutskiy 	}
11611e51764aSArtem Bityutskiy 
11621e51764aSArtem Bityutskiy 	err = free;
11631e51764aSArtem Bityutskiy out:
11641e51764aSArtem Bityutskiy 	ubifs_release_lprops(c);
11651e51764aSArtem Bityutskiy 	return err;
11661e51764aSArtem Bityutskiy }
11671e51764aSArtem Bityutskiy 
11681e51764aSArtem Bityutskiy /**
11691e51764aSArtem Bityutskiy  * ubifs_replay_journal - replay journal.
11701e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
11711e51764aSArtem Bityutskiy  *
11721e51764aSArtem Bityutskiy  * This function scans the journal, replays and cleans it up. It makes sure all
11731e51764aSArtem Bityutskiy  * memory data structures related to uncommitted journal are built (dirty TNC
11741e51764aSArtem Bityutskiy  * tree, tree of buds, modified lprops, etc).
11751e51764aSArtem Bityutskiy  */
11761e51764aSArtem Bityutskiy int ubifs_replay_journal(struct ubifs_info *c)
11771e51764aSArtem Bityutskiy {
1178d51f17eaSArtem Bityutskiy 	int err, lnum, free;
11791e51764aSArtem Bityutskiy 
11801e51764aSArtem Bityutskiy 	BUILD_BUG_ON(UBIFS_TRUN_KEY > 5);
11811e51764aSArtem Bityutskiy 
11821e51764aSArtem Bityutskiy 	/* Update the status of the index head in lprops to 'taken' */
11831e51764aSArtem Bityutskiy 	free = take_ihead(c);
11841e51764aSArtem Bityutskiy 	if (free < 0)
11851e51764aSArtem Bityutskiy 		return free; /* Error code */
11861e51764aSArtem Bityutskiy 
11871e51764aSArtem Bityutskiy 	if (c->ihead_offs != c->leb_size - free) {
1188235c362bSSheng Yong 		ubifs_err(c, "bad index head LEB %d:%d", c->ihead_lnum,
11891e51764aSArtem Bityutskiy 			  c->ihead_offs);
11901e51764aSArtem Bityutskiy 		return -EINVAL;
11911e51764aSArtem Bityutskiy 	}
11921e51764aSArtem Bityutskiy 
11931e51764aSArtem Bityutskiy 	dbg_mnt("start replaying the journal");
11941e51764aSArtem Bityutskiy 	c->replaying = 1;
11951e51764aSArtem Bityutskiy 	lnum = c->ltail_lnum = c->lhead_lnum;
11961e51764aSArtem Bityutskiy 
1197d51f17eaSArtem Bityutskiy 	do {
1198d51f17eaSArtem Bityutskiy 		err = replay_log_leb(c, lnum, 0, c->sbuf);
119988cff0f0Shujianyang 		if (err == 1) {
120088cff0f0Shujianyang 			if (lnum != c->lhead_lnum)
12011e51764aSArtem Bityutskiy 				/* We hit the end of the log */
12021e51764aSArtem Bityutskiy 				break;
120388cff0f0Shujianyang 
120488cff0f0Shujianyang 			/*
120588cff0f0Shujianyang 			 * The head of the log must always start with the
120688cff0f0Shujianyang 			 * "commit start" node on a properly formatted UBIFS.
120788cff0f0Shujianyang 			 * But we found no nodes at all, which means that
1208c7e593b3SSascha Hauer 			 * something went wrong and we cannot proceed mounting
120988cff0f0Shujianyang 			 * the file-system.
121088cff0f0Shujianyang 			 */
1211235c362bSSheng Yong 			ubifs_err(c, "no UBIFS nodes found at the log head LEB %d:%d, possibly corrupted",
121288cff0f0Shujianyang 				  lnum, 0);
121388cff0f0Shujianyang 			err = -EINVAL;
121488cff0f0Shujianyang 		}
12151e51764aSArtem Bityutskiy 		if (err)
12161e51764aSArtem Bityutskiy 			goto out;
1217d51f17eaSArtem Bityutskiy 		lnum = ubifs_next_log_lnum(c, lnum);
1218c212f402SArtem Bityutskiy 	} while (lnum != c->ltail_lnum);
12191e51764aSArtem Bityutskiy 
12201e51764aSArtem Bityutskiy 	err = replay_buds(c);
12211e51764aSArtem Bityutskiy 	if (err)
12221e51764aSArtem Bityutskiy 		goto out;
12231e51764aSArtem Bityutskiy 
1224debf12d5SArtem Bityutskiy 	err = apply_replay_list(c);
12251e51764aSArtem Bityutskiy 	if (err)
12261e51764aSArtem Bityutskiy 		goto out;
12271e51764aSArtem Bityutskiy 
1228074bcb9bSArtem Bityutskiy 	err = set_buds_lprops(c);
1229074bcb9bSArtem Bityutskiy 	if (err)
1230074bcb9bSArtem Bityutskiy 		goto out;
1231074bcb9bSArtem Bityutskiy 
12326edbfafdSArtem Bityutskiy 	/*
1233b137545cSArtem Bityutskiy 	 * UBIFS budgeting calculations use @c->bi.uncommitted_idx variable
1234b137545cSArtem Bityutskiy 	 * to roughly estimate index growth. Things like @c->bi.min_idx_lebs
12356edbfafdSArtem Bityutskiy 	 * depend on it. This means we have to initialize it to make sure
12366edbfafdSArtem Bityutskiy 	 * budgeting works properly.
12376edbfafdSArtem Bityutskiy 	 */
1238b137545cSArtem Bityutskiy 	c->bi.uncommitted_idx = atomic_long_read(&c->dirty_zn_cnt);
1239b137545cSArtem Bityutskiy 	c->bi.uncommitted_idx *= c->max_idx_node_sz;
12406edbfafdSArtem Bityutskiy 
12416eb61d58SRichard Weinberger 	ubifs_assert(c, c->bud_bytes <= c->max_bud_bytes || c->need_recovery);
124279fda517SArtem Bityutskiy 	dbg_mnt("finished, log head LEB %d:%d, max_sqnum %llu, highest_inum %lu",
124379fda517SArtem Bityutskiy 		c->lhead_lnum, c->lhead_offs, c->max_sqnum,
1244e84461adSArtem Bityutskiy 		(unsigned long)c->highest_inum);
12451e51764aSArtem Bityutskiy out:
1246debf12d5SArtem Bityutskiy 	destroy_replay_list(c);
12471e51764aSArtem Bityutskiy 	destroy_bud_list(c);
12481e51764aSArtem Bityutskiy 	c->replaying = 0;
12491e51764aSArtem Bityutskiy 	return err;
12501e51764aSArtem Bityutskiy }
1251