xref: /linux/fs/ubifs/replay.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
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>
261e51764aSArtem Bityutskiy 
271e51764aSArtem Bityutskiy /**
28debf12d5SArtem Bityutskiy  * struct replay_entry - replay list entry.
291e51764aSArtem Bityutskiy  * @lnum: logical eraseblock number of the node
301e51764aSArtem Bityutskiy  * @offs: node offset
311e51764aSArtem Bityutskiy  * @len: node length
32*39986148SJeff Johnson  * @hash: node hash
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  */
set_bud_lprops(struct ubifs_info * c,struct bud_entry * b)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,
1097296c8afSAlexander Dahl 		 * and there was no commit in between. 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  */
set_buds_lprops(struct ubifs_info * c)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  */
trun_remove_range(struct ubifs_info * c,struct replay_entry * r)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  */
inode_still_linked(struct ubifs_info * c,struct replay_entry * rino)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  */
apply_replay_entry(struct ubifs_info * c,struct replay_entry * r)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
29907c32de4SZheng 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  */
replay_entries_cmp(void * priv,const struct list_head * a,const struct list_head * b)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  */
apply_replay_list(struct ubifs_info * c)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  */
destroy_replay_list(struct ubifs_info * c)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
3692ba5b489SSascha Hauer  * @hash: node hash
3701e51764aSArtem Bityutskiy  * @key: node key
3711e51764aSArtem Bityutskiy  * @sqnum: sequence number
3721e51764aSArtem Bityutskiy  * @deletion: non-zero if this is a deletion
3731e51764aSArtem Bityutskiy  * @used: number of bytes in use in a LEB
3741e51764aSArtem Bityutskiy  * @old_size: truncation old size
3751e51764aSArtem Bityutskiy  * @new_size: truncation new size
3761e51764aSArtem Bityutskiy  *
377debf12d5SArtem Bityutskiy  * This function inserts a scanned non-direntry node to the replay list. The
378debf12d5SArtem Bityutskiy  * replay list contains @struct replay_entry elements, and we sort this list in
379debf12d5SArtem Bityutskiy  * sequence number order before applying it. The replay list is applied at the
380debf12d5SArtem Bityutskiy  * very end of the replay process. Since the list is sorted in sequence number
381debf12d5SArtem Bityutskiy  * order, the older modifications are applied first. This function returns zero
382debf12d5SArtem Bityutskiy  * in case of success and a negative error code in case of failure.
3831e51764aSArtem Bityutskiy  */
insert_node(struct ubifs_info * c,int lnum,int offs,int len,const u8 * hash,union ubifs_key * key,unsigned long long sqnum,int deletion,int * used,loff_t old_size,loff_t new_size)3841e51764aSArtem Bityutskiy static int insert_node(struct ubifs_info *c, int lnum, int offs, int len,
38516a26b20SSascha Hauer 		       const u8 *hash, union ubifs_key *key,
38616a26b20SSascha Hauer 		       unsigned long long sqnum, int deletion, int *used,
38716a26b20SSascha Hauer 		       loff_t old_size, loff_t new_size)
3881e51764aSArtem Bityutskiy {
3891e51764aSArtem Bityutskiy 	struct replay_entry *r;
3901e51764aSArtem Bityutskiy 
391515315a1SArtem Bityutskiy 	dbg_mntk(key, "add LEB %d:%d, key ", lnum, offs);
392debf12d5SArtem Bityutskiy 
3931e51764aSArtem Bityutskiy 	if (key_inum(c, key) >= c->highest_inum)
3941e51764aSArtem Bityutskiy 		c->highest_inum = key_inum(c, key);
3951e51764aSArtem Bityutskiy 
3961e51764aSArtem Bityutskiy 	r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL);
3971e51764aSArtem Bityutskiy 	if (!r)
3981e51764aSArtem Bityutskiy 		return -ENOMEM;
3991e51764aSArtem Bityutskiy 
4001e51764aSArtem Bityutskiy 	if (!deletion)
4011e51764aSArtem Bityutskiy 		*used += ALIGN(len, 8);
4021e51764aSArtem Bityutskiy 	r->lnum = lnum;
4031e51764aSArtem Bityutskiy 	r->offs = offs;
4041e51764aSArtem Bityutskiy 	r->len = len;
40516a26b20SSascha Hauer 	ubifs_copy_hash(c, hash, r->hash);
406074bcb9bSArtem Bityutskiy 	r->deletion = !!deletion;
4071e51764aSArtem Bityutskiy 	r->sqnum = sqnum;
408074bcb9bSArtem Bityutskiy 	key_copy(c, key, &r->key);
4091e51764aSArtem Bityutskiy 	r->old_size = old_size;
4101e51764aSArtem Bityutskiy 	r->new_size = new_size;
4111e51764aSArtem Bityutskiy 
412debf12d5SArtem Bityutskiy 	list_add_tail(&r->list, &c->replay_list);
4131e51764aSArtem Bityutskiy 	return 0;
4141e51764aSArtem Bityutskiy }
4151e51764aSArtem Bityutskiy 
4161e51764aSArtem Bityutskiy /**
417debf12d5SArtem Bityutskiy  * insert_dent - insert a directory entry node into the replay list.
4181e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
4191e51764aSArtem Bityutskiy  * @lnum: node logical eraseblock number
4201e51764aSArtem Bityutskiy  * @offs: node offset
4211e51764aSArtem Bityutskiy  * @len: node length
4222ba5b489SSascha Hauer  * @hash: node hash
4231e51764aSArtem Bityutskiy  * @key: node key
4241e51764aSArtem Bityutskiy  * @name: directory entry name
4251e51764aSArtem Bityutskiy  * @nlen: directory entry name length
4261e51764aSArtem Bityutskiy  * @sqnum: sequence number
4271e51764aSArtem Bityutskiy  * @deletion: non-zero if this is a deletion
4281e51764aSArtem Bityutskiy  * @used: number of bytes in use in a LEB
4291e51764aSArtem Bityutskiy  *
430debf12d5SArtem Bityutskiy  * This function inserts a scanned directory entry node or an extended
431debf12d5SArtem Bityutskiy  * attribute entry to the replay list. Returns zero in case of success and a
432debf12d5SArtem Bityutskiy  * negative error code in case of failure.
4331e51764aSArtem Bityutskiy  */
insert_dent(struct ubifs_info * c,int lnum,int offs,int len,const u8 * hash,union ubifs_key * key,const char * name,int nlen,unsigned long long sqnum,int deletion,int * used)4341e51764aSArtem Bityutskiy static int insert_dent(struct ubifs_info *c, int lnum, int offs, int len,
43516a26b20SSascha Hauer 		       const u8 *hash, union ubifs_key *key,
43616a26b20SSascha Hauer 		       const char *name, int nlen, unsigned long long sqnum,
43716a26b20SSascha Hauer 		       int deletion, int *used)
4381e51764aSArtem Bityutskiy {
4391e51764aSArtem Bityutskiy 	struct replay_entry *r;
4401e51764aSArtem Bityutskiy 	char *nbuf;
4411e51764aSArtem Bityutskiy 
442515315a1SArtem Bityutskiy 	dbg_mntk(key, "add LEB %d:%d, key ", lnum, offs);
4431e51764aSArtem Bityutskiy 	if (key_inum(c, key) >= c->highest_inum)
4441e51764aSArtem Bityutskiy 		c->highest_inum = key_inum(c, key);
4451e51764aSArtem Bityutskiy 
4461e51764aSArtem Bityutskiy 	r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL);
4471e51764aSArtem Bityutskiy 	if (!r)
4481e51764aSArtem Bityutskiy 		return -ENOMEM;
449debf12d5SArtem Bityutskiy 
4501e51764aSArtem Bityutskiy 	nbuf = kmalloc(nlen + 1, GFP_KERNEL);
4511e51764aSArtem Bityutskiy 	if (!nbuf) {
4521e51764aSArtem Bityutskiy 		kfree(r);
4531e51764aSArtem Bityutskiy 		return -ENOMEM;
4541e51764aSArtem Bityutskiy 	}
4551e51764aSArtem Bityutskiy 
4561e51764aSArtem Bityutskiy 	if (!deletion)
4571e51764aSArtem Bityutskiy 		*used += ALIGN(len, 8);
4581e51764aSArtem Bityutskiy 	r->lnum = lnum;
4591e51764aSArtem Bityutskiy 	r->offs = offs;
4601e51764aSArtem Bityutskiy 	r->len = len;
46116a26b20SSascha Hauer 	ubifs_copy_hash(c, hash, r->hash);
462074bcb9bSArtem Bityutskiy 	r->deletion = !!deletion;
4631e51764aSArtem Bityutskiy 	r->sqnum = sqnum;
464074bcb9bSArtem Bityutskiy 	key_copy(c, key, &r->key);
465f4f61d2cSRichard Weinberger 	fname_len(&r->nm) = nlen;
4661e51764aSArtem Bityutskiy 	memcpy(nbuf, name, nlen);
4671e51764aSArtem Bityutskiy 	nbuf[nlen] = '\0';
468f4f61d2cSRichard Weinberger 	fname_name(&r->nm) = nbuf;
4691e51764aSArtem Bityutskiy 
470debf12d5SArtem Bityutskiy 	list_add_tail(&r->list, &c->replay_list);
4711e51764aSArtem Bityutskiy 	return 0;
4721e51764aSArtem Bityutskiy }
4731e51764aSArtem Bityutskiy 
4741e51764aSArtem Bityutskiy /**
4751e51764aSArtem Bityutskiy  * ubifs_validate_entry - validate directory or extended attribute entry node.
4761e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
4771e51764aSArtem Bityutskiy  * @dent: the node to validate
4781e51764aSArtem Bityutskiy  *
4791e51764aSArtem Bityutskiy  * This function validates directory or extended attribute entry node @dent.
4801e51764aSArtem Bityutskiy  * Returns zero if the node is all right and a %-EINVAL if not.
4811e51764aSArtem Bityutskiy  */
ubifs_validate_entry(struct ubifs_info * c,const struct ubifs_dent_node * dent)4821e51764aSArtem Bityutskiy int ubifs_validate_entry(struct ubifs_info *c,
4831e51764aSArtem Bityutskiy 			 const struct ubifs_dent_node *dent)
4841e51764aSArtem Bityutskiy {
4851e51764aSArtem Bityutskiy 	int key_type = key_type_flash(c, dent->key);
4861e51764aSArtem Bityutskiy 	int nlen = le16_to_cpu(dent->nlen);
4871e51764aSArtem Bityutskiy 
4881e51764aSArtem Bityutskiy 	if (le32_to_cpu(dent->ch.len) != nlen + UBIFS_DENT_NODE_SZ + 1 ||
4891e51764aSArtem Bityutskiy 	    dent->type >= UBIFS_ITYPES_CNT ||
4901e51764aSArtem Bityutskiy 	    nlen > UBIFS_MAX_NLEN || dent->name[nlen] != 0 ||
491304790c0SRichard Weinberger 	    (key_type == UBIFS_XENT_KEY && strnlen(dent->name, nlen) != nlen) ||
4921e51764aSArtem Bityutskiy 	    le64_to_cpu(dent->inum) > MAX_INUM) {
493235c362bSSheng Yong 		ubifs_err(c, "bad %s node", key_type == UBIFS_DENT_KEY ?
4941e51764aSArtem Bityutskiy 			  "directory entry" : "extended attribute entry");
4951e51764aSArtem Bityutskiy 		return -EINVAL;
4961e51764aSArtem Bityutskiy 	}
4971e51764aSArtem Bityutskiy 
4981e51764aSArtem Bityutskiy 	if (key_type != UBIFS_DENT_KEY && key_type != UBIFS_XENT_KEY) {
499235c362bSSheng Yong 		ubifs_err(c, "bad key type %d", key_type);
5001e51764aSArtem Bityutskiy 		return -EINVAL;
5011e51764aSArtem Bityutskiy 	}
5021e51764aSArtem Bityutskiy 
5031e51764aSArtem Bityutskiy 	return 0;
5041e51764aSArtem Bityutskiy }
5051e51764aSArtem Bityutskiy 
5061e51764aSArtem Bityutskiy /**
50791c66083SArtem Bityutskiy  * is_last_bud - check if the bud is the last in the journal head.
50891c66083SArtem Bityutskiy  * @c: UBIFS file-system description object
50991c66083SArtem Bityutskiy  * @bud: bud description object
51091c66083SArtem Bityutskiy  *
51191c66083SArtem Bityutskiy  * This function checks if bud @bud is the last bud in its journal head. This
51291c66083SArtem Bityutskiy  * information is then used by 'replay_bud()' to decide whether the bud can
51391c66083SArtem Bityutskiy  * have corruptions or not. Indeed, only last buds can be corrupted by power
51491c66083SArtem Bityutskiy  * cuts. Returns %1 if this is the last bud, and %0 if not.
51591c66083SArtem Bityutskiy  */
is_last_bud(struct ubifs_info * c,struct ubifs_bud * bud)51691c66083SArtem Bityutskiy static int is_last_bud(struct ubifs_info *c, struct ubifs_bud *bud)
51791c66083SArtem Bityutskiy {
51891c66083SArtem Bityutskiy 	struct ubifs_jhead *jh = &c->jheads[bud->jhead];
51991c66083SArtem Bityutskiy 	struct ubifs_bud *next;
52091c66083SArtem Bityutskiy 	uint32_t data;
52191c66083SArtem Bityutskiy 	int err;
52291c66083SArtem Bityutskiy 
52391c66083SArtem Bityutskiy 	if (list_is_last(&bud->list, &jh->buds_list))
52491c66083SArtem Bityutskiy 		return 1;
52591c66083SArtem Bityutskiy 
52691c66083SArtem Bityutskiy 	/*
52791c66083SArtem Bityutskiy 	 * The following is a quirk to make sure we work correctly with UBIFS
52891c66083SArtem Bityutskiy 	 * images used with older UBIFS.
52991c66083SArtem Bityutskiy 	 *
53091c66083SArtem Bityutskiy 	 * Normally, the last bud will be the last in the journal head's list
53191c66083SArtem Bityutskiy 	 * of bud. However, there is one exception if the UBIFS image belongs
53291c66083SArtem Bityutskiy 	 * to older UBIFS. This is fairly unlikely: one would need to use old
53391c66083SArtem Bityutskiy 	 * UBIFS, then have a power cut exactly at the right point, and then
53491c66083SArtem Bityutskiy 	 * try to mount this image with new UBIFS.
53591c66083SArtem Bityutskiy 	 *
53691c66083SArtem Bityutskiy 	 * The exception is: it is possible to have 2 buds A and B, A goes
53791c66083SArtem Bityutskiy 	 * before B, and B is the last, bud B is contains no data, and bud A is
53891c66083SArtem Bityutskiy 	 * corrupted at the end. The reason is that in older versions when the
53991c66083SArtem Bityutskiy 	 * journal code switched the next bud (from A to B), it first added a
54091c66083SArtem Bityutskiy 	 * log reference node for the new bud (B), and only after this it
54191c66083SArtem Bityutskiy 	 * synchronized the write-buffer of current bud (A). But later this was
54291c66083SArtem Bityutskiy 	 * changed and UBIFS started to always synchronize the write-buffer of
54391c66083SArtem Bityutskiy 	 * the bud (A) before writing the log reference for the new bud (B).
54491c66083SArtem Bityutskiy 	 *
54591c66083SArtem Bityutskiy 	 * But because older UBIFS always synchronized A's write-buffer before
54691c66083SArtem Bityutskiy 	 * writing to B, we can recognize this exceptional situation but
54791c66083SArtem Bityutskiy 	 * checking the contents of bud B - if it is empty, then A can be
54891c66083SArtem Bityutskiy 	 * treated as the last and we can recover it.
54991c66083SArtem Bityutskiy 	 *
55091c66083SArtem Bityutskiy 	 * TODO: remove this piece of code in a couple of years (today it is
55191c66083SArtem Bityutskiy 	 * 16.05.2011).
55291c66083SArtem Bityutskiy 	 */
55391c66083SArtem Bityutskiy 	next = list_entry(bud->list.next, struct ubifs_bud, list);
55491c66083SArtem Bityutskiy 	if (!list_is_last(&next->list, &jh->buds_list))
55591c66083SArtem Bityutskiy 		return 0;
55691c66083SArtem Bityutskiy 
557d304820aSArtem Bityutskiy 	err = ubifs_leb_read(c, next->lnum, (char *)&data, next->start, 4, 1);
55891c66083SArtem Bityutskiy 	if (err)
55991c66083SArtem Bityutskiy 		return 0;
56091c66083SArtem Bityutskiy 
56191c66083SArtem Bityutskiy 	return data == 0xFFFFFFFF;
56291c66083SArtem Bityutskiy }
56391c66083SArtem Bityutskiy 
564f80df385SEric Biggers /* authenticate_sleb_hash is split out for stack usage */
565410b6de7SArnd Bergmann static int noinline_for_stack
authenticate_sleb_hash(struct ubifs_info * c,struct shash_desc * log_hash,u8 * hash)566410b6de7SArnd Bergmann authenticate_sleb_hash(struct ubifs_info *c,
567410b6de7SArnd Bergmann 		       struct shash_desc *log_hash, u8 *hash)
568eb66eff6SArnd Bergmann {
569eb66eff6SArnd Bergmann 	SHASH_DESC_ON_STACK(hash_desc, c->hash_tfm);
570eb66eff6SArnd Bergmann 
571eb66eff6SArnd Bergmann 	hash_desc->tfm = c->hash_tfm;
572eb66eff6SArnd Bergmann 
573eb66eff6SArnd Bergmann 	ubifs_shash_copy_state(c, log_hash, hash_desc);
574eb66eff6SArnd Bergmann 	return crypto_shash_final(hash_desc, hash);
575eb66eff6SArnd Bergmann }
576eb66eff6SArnd Bergmann 
57791c66083SArtem Bityutskiy /**
578da8ef65fSSascha Hauer  * authenticate_sleb - authenticate one scan LEB
579da8ef65fSSascha Hauer  * @c: UBIFS file-system description object
580da8ef65fSSascha Hauer  * @sleb: the scan LEB to authenticate
581da8ef65fSSascha Hauer  * @log_hash:
582b8f1da98SRandy Dunlap  * @is_last: if true, this is the last LEB
583da8ef65fSSascha Hauer  *
584da8ef65fSSascha Hauer  * This function iterates over the buds of a single LEB authenticating all buds
585da8ef65fSSascha Hauer  * with the authentication nodes on this LEB. Authentication nodes are written
586da8ef65fSSascha Hauer  * after some buds and contain a HMAC covering the authentication node itself
587da8ef65fSSascha Hauer  * and the buds between the last authentication node and the current
588da8ef65fSSascha Hauer  * authentication node. It can happen that the last buds cannot be authenticated
589da8ef65fSSascha Hauer  * because a powercut happened when some nodes were written but not the
590da8ef65fSSascha Hauer  * corresponding authentication node. This function returns the number of nodes
591da8ef65fSSascha Hauer  * that could be authenticated or a negative error code.
592da8ef65fSSascha Hauer  */
authenticate_sleb(struct ubifs_info * c,struct ubifs_scan_leb * sleb,struct shash_desc * log_hash,int is_last)593da8ef65fSSascha Hauer static int authenticate_sleb(struct ubifs_info *c, struct ubifs_scan_leb *sleb,
594da8ef65fSSascha Hauer 			     struct shash_desc *log_hash, int is_last)
595da8ef65fSSascha Hauer {
596da8ef65fSSascha Hauer 	int n_not_auth = 0;
597da8ef65fSSascha Hauer 	struct ubifs_scan_node *snod;
598da8ef65fSSascha Hauer 	int n_nodes = 0;
599da8ef65fSSascha Hauer 	int err;
6003c3c32f8SEric Biggers 	u8 hash[UBIFS_HASH_ARR_SZ];
6013c3c32f8SEric Biggers 	u8 hmac[UBIFS_HMAC_ARR_SZ];
602da8ef65fSSascha Hauer 
603da8ef65fSSascha Hauer 	if (!ubifs_authenticated(c))
604da8ef65fSSascha Hauer 		return sleb->nodes_cnt;
605da8ef65fSSascha Hauer 
606da8ef65fSSascha Hauer 	list_for_each_entry(snod, &sleb->nodes, list) {
607da8ef65fSSascha Hauer 
608da8ef65fSSascha Hauer 		n_nodes++;
609da8ef65fSSascha Hauer 
610da8ef65fSSascha Hauer 		if (snod->type == UBIFS_AUTH_NODE) {
611da8ef65fSSascha Hauer 			struct ubifs_auth_node *auth = snod->node;
612da8ef65fSSascha Hauer 
613eb66eff6SArnd Bergmann 			err = authenticate_sleb_hash(c, log_hash, hash);
614da8ef65fSSascha Hauer 			if (err)
615da8ef65fSSascha Hauer 				goto out;
616da8ef65fSSascha Hauer 
617f80df385SEric Biggers 			err = crypto_shash_tfm_digest(c->hmac_tfm, hash,
618f80df385SEric Biggers 						      c->hash_len, hmac);
619da8ef65fSSascha Hauer 			if (err)
620da8ef65fSSascha Hauer 				goto out;
621da8ef65fSSascha Hauer 
622da8ef65fSSascha Hauer 			err = ubifs_check_hmac(c, auth->hmac, hmac);
623da8ef65fSSascha Hauer 			if (err) {
624da8ef65fSSascha Hauer 				err = -EPERM;
625da8ef65fSSascha Hauer 				goto out;
626da8ef65fSSascha Hauer 			}
627da8ef65fSSascha Hauer 			n_not_auth = 0;
628da8ef65fSSascha Hauer 		} else {
629da8ef65fSSascha Hauer 			err = crypto_shash_update(log_hash, snod->node,
630da8ef65fSSascha Hauer 						  snod->len);
631da8ef65fSSascha Hauer 			if (err)
632da8ef65fSSascha Hauer 				goto out;
633da8ef65fSSascha Hauer 			n_not_auth++;
634da8ef65fSSascha Hauer 		}
635da8ef65fSSascha Hauer 	}
636da8ef65fSSascha Hauer 
637da8ef65fSSascha Hauer 	/*
638da8ef65fSSascha Hauer 	 * A powercut can happen when some nodes were written, but not yet
639da8ef65fSSascha Hauer 	 * the corresponding authentication node. This may only happen on
640da8ef65fSSascha Hauer 	 * the last bud though.
641da8ef65fSSascha Hauer 	 */
642da8ef65fSSascha Hauer 	if (n_not_auth) {
643da8ef65fSSascha Hauer 		if (is_last) {
644da8ef65fSSascha Hauer 			dbg_mnt("%d unauthenticated nodes found on LEB %d, Ignoring them",
645da8ef65fSSascha Hauer 				n_not_auth, sleb->lnum);
646da8ef65fSSascha Hauer 			err = 0;
647da8ef65fSSascha Hauer 		} else {
648da8ef65fSSascha Hauer 			dbg_mnt("%d unauthenticated nodes found on non-last LEB %d",
649da8ef65fSSascha Hauer 				n_not_auth, sleb->lnum);
650da8ef65fSSascha Hauer 			err = -EPERM;
651da8ef65fSSascha Hauer 		}
652da8ef65fSSascha Hauer 	} else {
653da8ef65fSSascha Hauer 		err = 0;
654da8ef65fSSascha Hauer 	}
655da8ef65fSSascha Hauer out:
656da8ef65fSSascha Hauer 	return err ? err : n_nodes - n_not_auth;
657da8ef65fSSascha Hauer }
658da8ef65fSSascha Hauer 
659da8ef65fSSascha Hauer /**
6601e51764aSArtem Bityutskiy  * replay_bud - replay a bud logical eraseblock.
6611e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
662e76a4526SArtem Bityutskiy  * @b: bud entry which describes the bud
6631e51764aSArtem Bityutskiy  *
664e76a4526SArtem Bityutskiy  * This function replays bud @bud, recovers it if needed, and adds all nodes
665e76a4526SArtem Bityutskiy  * from this bud to the replay list. Returns zero in case of success and a
666e76a4526SArtem Bityutskiy  * negative error code in case of failure.
6671e51764aSArtem Bityutskiy  */
replay_bud(struct ubifs_info * c,struct bud_entry * b)668e76a4526SArtem Bityutskiy static int replay_bud(struct ubifs_info *c, struct bud_entry *b)
6691e51764aSArtem Bityutskiy {
67091c66083SArtem Bityutskiy 	int is_last = is_last_bud(c, b->bud);
671e76a4526SArtem Bityutskiy 	int err = 0, used = 0, lnum = b->bud->lnum, offs = b->bud->start;
672da8ef65fSSascha Hauer 	int n_nodes, n = 0;
6731e51764aSArtem Bityutskiy 	struct ubifs_scan_leb *sleb;
6741e51764aSArtem Bityutskiy 	struct ubifs_scan_node *snod;
6751e51764aSArtem Bityutskiy 
67691c66083SArtem Bityutskiy 	dbg_mnt("replay bud LEB %d, head %d, offs %d, is_last %d",
67791c66083SArtem Bityutskiy 		lnum, b->bud->jhead, offs, is_last);
678e76a4526SArtem Bityutskiy 
67991c66083SArtem Bityutskiy 	if (c->need_recovery && is_last)
68091c66083SArtem Bityutskiy 		/*
68191c66083SArtem Bityutskiy 		 * Recover only last LEBs in the journal heads, because power
68291c66083SArtem Bityutskiy 		 * cuts may cause corruptions only in these LEBs, because only
68391c66083SArtem Bityutskiy 		 * these LEBs could possibly be written to at the power cut
68491c66083SArtem Bityutskiy 		 * time.
68591c66083SArtem Bityutskiy 		 */
686efcfde54SArtem Bityutskiy 		sleb = ubifs_recover_leb(c, lnum, offs, c->sbuf, b->bud->jhead);
6871e51764aSArtem Bityutskiy 	else
688348709baSArtem Bityutskiy 		sleb = ubifs_scan(c, lnum, offs, c->sbuf, 0);
6891e51764aSArtem Bityutskiy 	if (IS_ERR(sleb))
6901e51764aSArtem Bityutskiy 		return PTR_ERR(sleb);
6911e51764aSArtem Bityutskiy 
692da8ef65fSSascha Hauer 	n_nodes = authenticate_sleb(c, sleb, b->bud->log_hash, is_last);
693da8ef65fSSascha Hauer 	if (n_nodes < 0) {
694da8ef65fSSascha Hauer 		err = n_nodes;
695da8ef65fSSascha Hauer 		goto out;
696da8ef65fSSascha Hauer 	}
697da8ef65fSSascha Hauer 
698da8ef65fSSascha Hauer 	ubifs_shash_copy_state(c, b->bud->log_hash,
699da8ef65fSSascha Hauer 			       c->jheads[b->bud->jhead].log_hash);
700da8ef65fSSascha Hauer 
7011e51764aSArtem Bityutskiy 	/*
7021e51764aSArtem Bityutskiy 	 * The bud does not have to start from offset zero - the beginning of
7031e51764aSArtem Bityutskiy 	 * the 'lnum' LEB may contain previously committed data. One of the
7041e51764aSArtem Bityutskiy 	 * things we have to do in replay is to correctly update lprops with
7051e51764aSArtem Bityutskiy 	 * newer information about this LEB.
7061e51764aSArtem Bityutskiy 	 *
7071e51764aSArtem Bityutskiy 	 * At this point lprops thinks that this LEB has 'c->leb_size - offs'
7081e51764aSArtem Bityutskiy 	 * bytes of free space because it only contain information about
7091e51764aSArtem Bityutskiy 	 * committed data.
7101e51764aSArtem Bityutskiy 	 *
7111e51764aSArtem Bityutskiy 	 * But we know that real amount of free space is 'c->leb_size -
7121e51764aSArtem Bityutskiy 	 * sleb->endpt', and the space in the 'lnum' LEB between 'offs' and
7131e51764aSArtem Bityutskiy 	 * 'sleb->endpt' is used by bud data. We have to correctly calculate
7141e51764aSArtem Bityutskiy 	 * how much of these data are dirty and update lprops with this
7151e51764aSArtem Bityutskiy 	 * information.
7161e51764aSArtem Bityutskiy 	 *
7171e51764aSArtem Bityutskiy 	 * The dirt in that LEB region is comprised of padding nodes, deletion
7181e51764aSArtem Bityutskiy 	 * nodes, truncation nodes and nodes which are obsoleted by subsequent
7191e51764aSArtem Bityutskiy 	 * nodes in this LEB. So instead of calculating clean space, we
7201e51764aSArtem Bityutskiy 	 * calculate used space ('used' variable).
7211e51764aSArtem Bityutskiy 	 */
7221e51764aSArtem Bityutskiy 
7231e51764aSArtem Bityutskiy 	list_for_each_entry(snod, &sleb->nodes, list) {
72416a26b20SSascha Hauer 		u8 hash[UBIFS_HASH_ARR_SZ];
7251e51764aSArtem Bityutskiy 		int deletion = 0;
7261e51764aSArtem Bityutskiy 
7271e51764aSArtem Bityutskiy 		cond_resched();
7281e51764aSArtem Bityutskiy 
7291e51764aSArtem Bityutskiy 		if (snod->sqnum >= SQNUM_WATERMARK) {
730235c362bSSheng Yong 			ubifs_err(c, "file system's life ended");
7311e51764aSArtem Bityutskiy 			goto out_dump;
7321e51764aSArtem Bityutskiy 		}
7331e51764aSArtem Bityutskiy 
73416a26b20SSascha Hauer 		ubifs_node_calc_hash(c, snod->node, hash);
73516a26b20SSascha Hauer 
7361e51764aSArtem Bityutskiy 		if (snod->sqnum > c->max_sqnum)
7371e51764aSArtem Bityutskiy 			c->max_sqnum = snod->sqnum;
7381e51764aSArtem Bityutskiy 
7391e51764aSArtem Bityutskiy 		switch (snod->type) {
7401e51764aSArtem Bityutskiy 		case UBIFS_INO_NODE:
7411e51764aSArtem Bityutskiy 		{
7421e51764aSArtem Bityutskiy 			struct ubifs_ino_node *ino = snod->node;
7431e51764aSArtem Bityutskiy 			loff_t new_size = le64_to_cpu(ino->size);
7441e51764aSArtem Bityutskiy 
7451e51764aSArtem Bityutskiy 			if (le32_to_cpu(ino->nlink) == 0)
7461e51764aSArtem Bityutskiy 				deletion = 1;
74716a26b20SSascha Hauer 			err = insert_node(c, lnum, snod->offs, snod->len, hash,
7481e51764aSArtem Bityutskiy 					  &snod->key, snod->sqnum, deletion,
7491e51764aSArtem Bityutskiy 					  &used, 0, new_size);
7501e51764aSArtem Bityutskiy 			break;
7511e51764aSArtem Bityutskiy 		}
7521e51764aSArtem Bityutskiy 		case UBIFS_DATA_NODE:
7531e51764aSArtem Bityutskiy 		{
7541e51764aSArtem Bityutskiy 			struct ubifs_data_node *dn = snod->node;
7551e51764aSArtem Bityutskiy 			loff_t new_size = le32_to_cpu(dn->size) +
7561e51764aSArtem Bityutskiy 					  key_block(c, &snod->key) *
7571e51764aSArtem Bityutskiy 					  UBIFS_BLOCK_SIZE;
7581e51764aSArtem Bityutskiy 
75916a26b20SSascha Hauer 			err = insert_node(c, lnum, snod->offs, snod->len, hash,
7601e51764aSArtem Bityutskiy 					  &snod->key, snod->sqnum, deletion,
7611e51764aSArtem Bityutskiy 					  &used, 0, new_size);
7621e51764aSArtem Bityutskiy 			break;
7631e51764aSArtem Bityutskiy 		}
7641e51764aSArtem Bityutskiy 		case UBIFS_DENT_NODE:
7651e51764aSArtem Bityutskiy 		case UBIFS_XENT_NODE:
7661e51764aSArtem Bityutskiy 		{
7671e51764aSArtem Bityutskiy 			struct ubifs_dent_node *dent = snod->node;
7681e51764aSArtem Bityutskiy 
7691e51764aSArtem Bityutskiy 			err = ubifs_validate_entry(c, dent);
7701e51764aSArtem Bityutskiy 			if (err)
7711e51764aSArtem Bityutskiy 				goto out_dump;
7721e51764aSArtem Bityutskiy 
77316a26b20SSascha Hauer 			err = insert_dent(c, lnum, snod->offs, snod->len, hash,
7741e51764aSArtem Bityutskiy 					  &snod->key, dent->name,
7751e51764aSArtem Bityutskiy 					  le16_to_cpu(dent->nlen), snod->sqnum,
7761e51764aSArtem Bityutskiy 					  !le64_to_cpu(dent->inum), &used);
7771e51764aSArtem Bityutskiy 			break;
7781e51764aSArtem Bityutskiy 		}
7791e51764aSArtem Bityutskiy 		case UBIFS_TRUN_NODE:
7801e51764aSArtem Bityutskiy 		{
7811e51764aSArtem Bityutskiy 			struct ubifs_trun_node *trun = snod->node;
7821e51764aSArtem Bityutskiy 			loff_t old_size = le64_to_cpu(trun->old_size);
7831e51764aSArtem Bityutskiy 			loff_t new_size = le64_to_cpu(trun->new_size);
7841e51764aSArtem Bityutskiy 			union ubifs_key key;
7851e51764aSArtem Bityutskiy 
7861e51764aSArtem Bityutskiy 			/* Validate truncation node */
7871e51764aSArtem Bityutskiy 			if (old_size < 0 || old_size > c->max_inode_sz ||
7881e51764aSArtem Bityutskiy 			    new_size < 0 || new_size > c->max_inode_sz ||
7891e51764aSArtem Bityutskiy 			    old_size <= new_size) {
790235c362bSSheng Yong 				ubifs_err(c, "bad truncation node");
7911e51764aSArtem Bityutskiy 				goto out_dump;
7921e51764aSArtem Bityutskiy 			}
7931e51764aSArtem Bityutskiy 
7941e51764aSArtem Bityutskiy 			/*
7951e51764aSArtem Bityutskiy 			 * Create a fake truncation key just to use the same
7961e51764aSArtem Bityutskiy 			 * functions which expect nodes to have keys.
7971e51764aSArtem Bityutskiy 			 */
7981e51764aSArtem Bityutskiy 			trun_key_init(c, &key, le32_to_cpu(trun->inum));
79916a26b20SSascha Hauer 			err = insert_node(c, lnum, snod->offs, snod->len, hash,
8001e51764aSArtem Bityutskiy 					  &key, snod->sqnum, 1, &used,
8011e51764aSArtem Bityutskiy 					  old_size, new_size);
8021e51764aSArtem Bityutskiy 			break;
8031e51764aSArtem Bityutskiy 		}
8046a98bc46SSascha Hauer 		case UBIFS_AUTH_NODE:
8056a98bc46SSascha Hauer 			break;
8061e51764aSArtem Bityutskiy 		default:
807235c362bSSheng Yong 			ubifs_err(c, "unexpected node type %d in bud LEB %d:%d",
8081e51764aSArtem Bityutskiy 				  snod->type, lnum, snod->offs);
8091e51764aSArtem Bityutskiy 			err = -EINVAL;
8101e51764aSArtem Bityutskiy 			goto out_dump;
8111e51764aSArtem Bityutskiy 		}
8121e51764aSArtem Bityutskiy 		if (err)
8131e51764aSArtem Bityutskiy 			goto out;
814da8ef65fSSascha Hauer 
815da8ef65fSSascha Hauer 		n++;
816da8ef65fSSascha Hauer 		if (n == n_nodes)
817da8ef65fSSascha Hauer 			break;
8181e51764aSArtem Bityutskiy 	}
8191e51764aSArtem Bityutskiy 
8206eb61d58SRichard Weinberger 	ubifs_assert(c, ubifs_search_bud(c, lnum));
8216eb61d58SRichard Weinberger 	ubifs_assert(c, sleb->endpt - offs >= used);
8226eb61d58SRichard Weinberger 	ubifs_assert(c, sleb->endpt % c->min_io_size == 0);
8231e51764aSArtem Bityutskiy 
824e76a4526SArtem Bityutskiy 	b->dirty = sleb->endpt - offs - used;
825e76a4526SArtem Bityutskiy 	b->free = c->leb_size - sleb->endpt;
82679fda517SArtem Bityutskiy 	dbg_mnt("bud LEB %d replied: dirty %d, free %d",
82779fda517SArtem Bityutskiy 		lnum, b->dirty, b->free);
8281e51764aSArtem Bityutskiy 
8291e51764aSArtem Bityutskiy out:
8301e51764aSArtem Bityutskiy 	ubifs_scan_destroy(sleb);
8311e51764aSArtem Bityutskiy 	return err;
8321e51764aSArtem Bityutskiy 
8331e51764aSArtem Bityutskiy out_dump:
834235c362bSSheng Yong 	ubifs_err(c, "bad node is at LEB %d:%d", lnum, snod->offs);
835a33e30a0SZhihao Cheng 	ubifs_dump_node(c, snod->node, c->leb_size - snod->offs);
8361e51764aSArtem Bityutskiy 	ubifs_scan_destroy(sleb);
8371e51764aSArtem Bityutskiy 	return -EINVAL;
8381e51764aSArtem Bityutskiy }
8391e51764aSArtem Bityutskiy 
8401e51764aSArtem Bityutskiy /**
8411e51764aSArtem Bityutskiy  * replay_buds - replay all buds.
8421e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
8431e51764aSArtem Bityutskiy  *
8441e51764aSArtem Bityutskiy  * This function returns zero in case of success and a negative error code in
8451e51764aSArtem Bityutskiy  * case of failure.
8461e51764aSArtem Bityutskiy  */
replay_buds(struct ubifs_info * c)8471e51764aSArtem Bityutskiy static int replay_buds(struct ubifs_info *c)
8481e51764aSArtem Bityutskiy {
8491e51764aSArtem Bityutskiy 	struct bud_entry *b;
850074bcb9bSArtem Bityutskiy 	int err;
8517703f09dSArtem Bityutskiy 	unsigned long long prev_sqnum = 0;
8521e51764aSArtem Bityutskiy 
8531e51764aSArtem Bityutskiy 	list_for_each_entry(b, &c->replay_buds, list) {
854e76a4526SArtem Bityutskiy 		err = replay_bud(c, b);
8551e51764aSArtem Bityutskiy 		if (err)
8561e51764aSArtem Bityutskiy 			return err;
8577703f09dSArtem Bityutskiy 
8586eb61d58SRichard Weinberger 		ubifs_assert(c, b->sqnum > prev_sqnum);
8597703f09dSArtem Bityutskiy 		prev_sqnum = b->sqnum;
8601e51764aSArtem Bityutskiy 	}
8611e51764aSArtem Bityutskiy 
8621e51764aSArtem Bityutskiy 	return 0;
8631e51764aSArtem Bityutskiy }
8641e51764aSArtem Bityutskiy 
8651e51764aSArtem Bityutskiy /**
8661e51764aSArtem Bityutskiy  * destroy_bud_list - destroy the list of buds to replay.
8671e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
8681e51764aSArtem Bityutskiy  */
destroy_bud_list(struct ubifs_info * c)8691e51764aSArtem Bityutskiy static void destroy_bud_list(struct ubifs_info *c)
8701e51764aSArtem Bityutskiy {
8711e51764aSArtem Bityutskiy 	struct bud_entry *b;
8721e51764aSArtem Bityutskiy 
8731e51764aSArtem Bityutskiy 	while (!list_empty(&c->replay_buds)) {
8741e51764aSArtem Bityutskiy 		b = list_entry(c->replay_buds.next, struct bud_entry, list);
8751e51764aSArtem Bityutskiy 		list_del(&b->list);
8761e51764aSArtem Bityutskiy 		kfree(b);
8771e51764aSArtem Bityutskiy 	}
8781e51764aSArtem Bityutskiy }
8791e51764aSArtem Bityutskiy 
8801e51764aSArtem Bityutskiy /**
8811e51764aSArtem Bityutskiy  * add_replay_bud - add a bud to the list of buds to replay.
8821e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
8831e51764aSArtem Bityutskiy  * @lnum: bud logical eraseblock number to replay
8841e51764aSArtem Bityutskiy  * @offs: bud start offset
8851e51764aSArtem Bityutskiy  * @jhead: journal head to which this bud belongs
8861e51764aSArtem Bityutskiy  * @sqnum: reference node sequence number
8871e51764aSArtem Bityutskiy  *
8881e51764aSArtem Bityutskiy  * This function returns zero in case of success and a negative error code in
8891e51764aSArtem Bityutskiy  * case of failure.
8901e51764aSArtem Bityutskiy  */
add_replay_bud(struct ubifs_info * c,int lnum,int offs,int jhead,unsigned long long sqnum)8911e51764aSArtem Bityutskiy static int add_replay_bud(struct ubifs_info *c, int lnum, int offs, int jhead,
8921e51764aSArtem Bityutskiy 			  unsigned long long sqnum)
8931e51764aSArtem Bityutskiy {
8941e51764aSArtem Bityutskiy 	struct ubifs_bud *bud;
8951e51764aSArtem Bityutskiy 	struct bud_entry *b;
896da8ef65fSSascha Hauer 	int err;
8971e51764aSArtem Bityutskiy 
8981e51764aSArtem Bityutskiy 	dbg_mnt("add replay bud LEB %d:%d, head %d", lnum, offs, jhead);
8991e51764aSArtem Bityutskiy 
9001e51764aSArtem Bityutskiy 	bud = kmalloc(sizeof(struct ubifs_bud), GFP_KERNEL);
9011e51764aSArtem Bityutskiy 	if (!bud)
9021e51764aSArtem Bityutskiy 		return -ENOMEM;
9031e51764aSArtem Bityutskiy 
9041e51764aSArtem Bityutskiy 	b = kmalloc(sizeof(struct bud_entry), GFP_KERNEL);
9051e51764aSArtem Bityutskiy 	if (!b) {
906da8ef65fSSascha Hauer 		err = -ENOMEM;
907da8ef65fSSascha Hauer 		goto out;
9081e51764aSArtem Bityutskiy 	}
9091e51764aSArtem Bityutskiy 
9101e51764aSArtem Bityutskiy 	bud->lnum = lnum;
9111e51764aSArtem Bityutskiy 	bud->start = offs;
9121e51764aSArtem Bityutskiy 	bud->jhead = jhead;
913da8ef65fSSascha Hauer 	bud->log_hash = ubifs_hash_get_desc(c);
914da8ef65fSSascha Hauer 	if (IS_ERR(bud->log_hash)) {
915da8ef65fSSascha Hauer 		err = PTR_ERR(bud->log_hash);
916da8ef65fSSascha Hauer 		goto out;
917da8ef65fSSascha Hauer 	}
918da8ef65fSSascha Hauer 
919da8ef65fSSascha Hauer 	ubifs_shash_copy_state(c, c->log_hash, bud->log_hash);
920da8ef65fSSascha Hauer 
9211e51764aSArtem Bityutskiy 	ubifs_add_bud(c, bud);
9221e51764aSArtem Bityutskiy 
9231e51764aSArtem Bityutskiy 	b->bud = bud;
9241e51764aSArtem Bityutskiy 	b->sqnum = sqnum;
9251e51764aSArtem Bityutskiy 	list_add_tail(&b->list, &c->replay_buds);
9261e51764aSArtem Bityutskiy 
9271e51764aSArtem Bityutskiy 	return 0;
928da8ef65fSSascha Hauer out:
929da8ef65fSSascha Hauer 	kfree(bud);
930da8ef65fSSascha Hauer 	kfree(b);
931da8ef65fSSascha Hauer 
932da8ef65fSSascha Hauer 	return err;
9331e51764aSArtem Bityutskiy }
9341e51764aSArtem Bityutskiy 
9351e51764aSArtem Bityutskiy /**
9361e51764aSArtem Bityutskiy  * validate_ref - validate a reference node.
9371e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
9381e51764aSArtem Bityutskiy  * @ref: the reference node to validate
9391e51764aSArtem Bityutskiy  *
9401e51764aSArtem Bityutskiy  * This function returns %1 if a bud reference already exists for the LEB. %0 is
9411e51764aSArtem Bityutskiy  * returned if the reference node is new, otherwise %-EINVAL is returned if
9421e51764aSArtem Bityutskiy  * validation failed.
9431e51764aSArtem Bityutskiy  */
validate_ref(struct ubifs_info * c,const struct ubifs_ref_node * ref)9441e51764aSArtem Bityutskiy static int validate_ref(struct ubifs_info *c, const struct ubifs_ref_node *ref)
9451e51764aSArtem Bityutskiy {
9461e51764aSArtem Bityutskiy 	struct ubifs_bud *bud;
9471e51764aSArtem Bityutskiy 	int lnum = le32_to_cpu(ref->lnum);
9481e51764aSArtem Bityutskiy 	unsigned int offs = le32_to_cpu(ref->offs);
9491e51764aSArtem Bityutskiy 	unsigned int jhead = le32_to_cpu(ref->jhead);
9501e51764aSArtem Bityutskiy 
9511e51764aSArtem Bityutskiy 	/*
9521e51764aSArtem Bityutskiy 	 * ref->offs may point to the end of LEB when the journal head points
9531e51764aSArtem Bityutskiy 	 * to the end of LEB and we write reference node for it during commit.
9541e51764aSArtem Bityutskiy 	 * So this is why we require 'offs > c->leb_size'.
9551e51764aSArtem Bityutskiy 	 */
9561e51764aSArtem Bityutskiy 	if (jhead >= c->jhead_cnt || lnum >= c->leb_cnt ||
9571e51764aSArtem Bityutskiy 	    lnum < c->main_first || offs > c->leb_size ||
9581e51764aSArtem Bityutskiy 	    offs & (c->min_io_size - 1))
9591e51764aSArtem Bityutskiy 		return -EINVAL;
9601e51764aSArtem Bityutskiy 
9611e51764aSArtem Bityutskiy 	/* Make sure we have not already looked at this bud */
9621e51764aSArtem Bityutskiy 	bud = ubifs_search_bud(c, lnum);
9631e51764aSArtem Bityutskiy 	if (bud) {
9641e51764aSArtem Bityutskiy 		if (bud->jhead == jhead && bud->start <= offs)
9651e51764aSArtem Bityutskiy 			return 1;
966235c362bSSheng Yong 		ubifs_err(c, "bud at LEB %d:%d was already referred", lnum, offs);
9671e51764aSArtem Bityutskiy 		return -EINVAL;
9681e51764aSArtem Bityutskiy 	}
9691e51764aSArtem Bityutskiy 
9701e51764aSArtem Bityutskiy 	return 0;
9711e51764aSArtem Bityutskiy }
9721e51764aSArtem Bityutskiy 
9731e51764aSArtem Bityutskiy /**
9741e51764aSArtem Bityutskiy  * replay_log_leb - replay a log logical eraseblock.
9751e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
9761e51764aSArtem Bityutskiy  * @lnum: log logical eraseblock to replay
9771e51764aSArtem Bityutskiy  * @offs: offset to start replaying from
9781e51764aSArtem Bityutskiy  * @sbuf: scan buffer
9791e51764aSArtem Bityutskiy  *
9801e51764aSArtem Bityutskiy  * This function replays a log LEB and returns zero in case of success, %1 if
9811e51764aSArtem Bityutskiy  * this is the last LEB in the log, and a negative error code in case of
9821e51764aSArtem Bityutskiy  * failure.
9831e51764aSArtem Bityutskiy  */
replay_log_leb(struct ubifs_info * c,int lnum,int offs,void * sbuf)9841e51764aSArtem Bityutskiy static int replay_log_leb(struct ubifs_info *c, int lnum, int offs, void *sbuf)
9851e51764aSArtem Bityutskiy {
9861e51764aSArtem Bityutskiy 	int err;
9871e51764aSArtem Bityutskiy 	struct ubifs_scan_leb *sleb;
9881e51764aSArtem Bityutskiy 	struct ubifs_scan_node *snod;
9891e51764aSArtem Bityutskiy 	const struct ubifs_cs_node *node;
9901e51764aSArtem Bityutskiy 
9911e51764aSArtem Bityutskiy 	dbg_mnt("replay log LEB %d:%d", lnum, offs);
992348709baSArtem Bityutskiy 	sleb = ubifs_scan(c, lnum, offs, sbuf, c->need_recovery);
9931e51764aSArtem Bityutskiy 	if (IS_ERR(sleb)) {
994ed43f2f0SArtem Bityutskiy 		if (PTR_ERR(sleb) != -EUCLEAN || !c->need_recovery)
995ed43f2f0SArtem Bityutskiy 			return PTR_ERR(sleb);
9967d08ae3cSArtem Bityutskiy 		/*
9977d08ae3cSArtem Bityutskiy 		 * Note, the below function will recover this log LEB only if
9987d08ae3cSArtem Bityutskiy 		 * it is the last, because unclean reboots can possibly corrupt
9997d08ae3cSArtem Bityutskiy 		 * only the tail of the log.
10007d08ae3cSArtem Bityutskiy 		 */
10011e51764aSArtem Bityutskiy 		sleb = ubifs_recover_log_leb(c, lnum, offs, sbuf);
10021e51764aSArtem Bityutskiy 		if (IS_ERR(sleb))
10031e51764aSArtem Bityutskiy 			return PTR_ERR(sleb);
10041e51764aSArtem Bityutskiy 	}
10051e51764aSArtem Bityutskiy 
10061e51764aSArtem Bityutskiy 	if (sleb->nodes_cnt == 0) {
10071e51764aSArtem Bityutskiy 		err = 1;
10081e51764aSArtem Bityutskiy 		goto out;
10091e51764aSArtem Bityutskiy 	}
10101e51764aSArtem Bityutskiy 
10111e51764aSArtem Bityutskiy 	node = sleb->buf;
10121e51764aSArtem Bityutskiy 	snod = list_entry(sleb->nodes.next, struct ubifs_scan_node, list);
10131e51764aSArtem Bityutskiy 	if (c->cs_sqnum == 0) {
10141e51764aSArtem Bityutskiy 		/*
10151e51764aSArtem Bityutskiy 		 * This is the first log LEB we are looking at, make sure that
10161e51764aSArtem Bityutskiy 		 * the first node is a commit start node. Also record its
10171e51764aSArtem Bityutskiy 		 * sequence number so that UBIFS can determine where the log
10181e51764aSArtem Bityutskiy 		 * ends, because all nodes which were have higher sequence
10191e51764aSArtem Bityutskiy 		 * numbers.
10201e51764aSArtem Bityutskiy 		 */
10211e51764aSArtem Bityutskiy 		if (snod->type != UBIFS_CS_NODE) {
1022235c362bSSheng Yong 			ubifs_err(c, "first log node at LEB %d:%d is not CS node",
10231e51764aSArtem Bityutskiy 				  lnum, offs);
10241e51764aSArtem Bityutskiy 			goto out_dump;
10251e51764aSArtem Bityutskiy 		}
10261e51764aSArtem Bityutskiy 		if (le64_to_cpu(node->cmt_no) != c->cmt_no) {
1027235c362bSSheng Yong 			ubifs_err(c, "first CS node at LEB %d:%d has wrong commit number %llu expected %llu",
10281e51764aSArtem Bityutskiy 				  lnum, offs,
10291e51764aSArtem Bityutskiy 				  (unsigned long long)le64_to_cpu(node->cmt_no),
10301e51764aSArtem Bityutskiy 				  c->cmt_no);
10311e51764aSArtem Bityutskiy 			goto out_dump;
10321e51764aSArtem Bityutskiy 		}
10331e51764aSArtem Bityutskiy 
10341e51764aSArtem Bityutskiy 		c->cs_sqnum = le64_to_cpu(node->ch.sqnum);
10351e51764aSArtem Bityutskiy 		dbg_mnt("commit start sqnum %llu", c->cs_sqnum);
1036da8ef65fSSascha Hauer 
1037da8ef65fSSascha Hauer 		err = ubifs_shash_init(c, c->log_hash);
1038da8ef65fSSascha Hauer 		if (err)
1039da8ef65fSSascha Hauer 			goto out;
1040da8ef65fSSascha Hauer 
1041da8ef65fSSascha Hauer 		err = ubifs_shash_update(c, c->log_hash, node, UBIFS_CS_NODE_SZ);
1042da8ef65fSSascha Hauer 		if (err < 0)
1043da8ef65fSSascha Hauer 			goto out;
10441e51764aSArtem Bityutskiy 	}
10451e51764aSArtem Bityutskiy 
10461e51764aSArtem Bityutskiy 	if (snod->sqnum < c->cs_sqnum) {
10471e51764aSArtem Bityutskiy 		/*
10481e51764aSArtem Bityutskiy 		 * This means that we reached end of log and now
10491e51764aSArtem Bityutskiy 		 * look to the older log data, which was already
10501e51764aSArtem Bityutskiy 		 * committed but the eraseblock was not erased (UBIFS
10516edbfafdSArtem Bityutskiy 		 * only un-maps it). So this basically means we have to
10521e51764aSArtem Bityutskiy 		 * exit with "end of log" code.
10531e51764aSArtem Bityutskiy 		 */
10541e51764aSArtem Bityutskiy 		err = 1;
10551e51764aSArtem Bityutskiy 		goto out;
10561e51764aSArtem Bityutskiy 	}
10571e51764aSArtem Bityutskiy 
10581e51764aSArtem Bityutskiy 	/* Make sure the first node sits at offset zero of the LEB */
10591e51764aSArtem Bityutskiy 	if (snod->offs != 0) {
1060235c362bSSheng Yong 		ubifs_err(c, "first node is not at zero offset");
10611e51764aSArtem Bityutskiy 		goto out_dump;
10621e51764aSArtem Bityutskiy 	}
10631e51764aSArtem Bityutskiy 
10641e51764aSArtem Bityutskiy 	list_for_each_entry(snod, &sleb->nodes, list) {
10651e51764aSArtem Bityutskiy 		cond_resched();
10661e51764aSArtem Bityutskiy 
10671e51764aSArtem Bityutskiy 		if (snod->sqnum >= SQNUM_WATERMARK) {
1068235c362bSSheng Yong 			ubifs_err(c, "file system's life ended");
10691e51764aSArtem Bityutskiy 			goto out_dump;
10701e51764aSArtem Bityutskiy 		}
10711e51764aSArtem Bityutskiy 
10721e51764aSArtem Bityutskiy 		if (snod->sqnum < c->cs_sqnum) {
1073235c362bSSheng Yong 			ubifs_err(c, "bad sqnum %llu, commit sqnum %llu",
10741e51764aSArtem Bityutskiy 				  snod->sqnum, c->cs_sqnum);
10751e51764aSArtem Bityutskiy 			goto out_dump;
10761e51764aSArtem Bityutskiy 		}
10771e51764aSArtem Bityutskiy 
10781e51764aSArtem Bityutskiy 		if (snod->sqnum > c->max_sqnum)
10791e51764aSArtem Bityutskiy 			c->max_sqnum = snod->sqnum;
10801e51764aSArtem Bityutskiy 
10811e51764aSArtem Bityutskiy 		switch (snod->type) {
10821e51764aSArtem Bityutskiy 		case UBIFS_REF_NODE: {
10831e51764aSArtem Bityutskiy 			const struct ubifs_ref_node *ref = snod->node;
10841e51764aSArtem Bityutskiy 
10851e51764aSArtem Bityutskiy 			err = validate_ref(c, ref);
10861e51764aSArtem Bityutskiy 			if (err == 1)
10871e51764aSArtem Bityutskiy 				break; /* Already have this bud */
10881e51764aSArtem Bityutskiy 			if (err)
10891e51764aSArtem Bityutskiy 				goto out_dump;
10901e51764aSArtem Bityutskiy 
1091da8ef65fSSascha Hauer 			err = ubifs_shash_update(c, c->log_hash, ref,
1092da8ef65fSSascha Hauer 						 UBIFS_REF_NODE_SZ);
1093da8ef65fSSascha Hauer 			if (err)
1094da8ef65fSSascha Hauer 				goto out;
1095da8ef65fSSascha Hauer 
10961e51764aSArtem Bityutskiy 			err = add_replay_bud(c, le32_to_cpu(ref->lnum),
10971e51764aSArtem Bityutskiy 					     le32_to_cpu(ref->offs),
10981e51764aSArtem Bityutskiy 					     le32_to_cpu(ref->jhead),
10991e51764aSArtem Bityutskiy 					     snod->sqnum);
11001e51764aSArtem Bityutskiy 			if (err)
11011e51764aSArtem Bityutskiy 				goto out;
11021e51764aSArtem Bityutskiy 
11031e51764aSArtem Bityutskiy 			break;
11041e51764aSArtem Bityutskiy 		}
11051e51764aSArtem Bityutskiy 		case UBIFS_CS_NODE:
11061e51764aSArtem Bityutskiy 			/* Make sure it sits at the beginning of LEB */
11071e51764aSArtem Bityutskiy 			if (snod->offs != 0) {
1108235c362bSSheng Yong 				ubifs_err(c, "unexpected node in log");
11091e51764aSArtem Bityutskiy 				goto out_dump;
11101e51764aSArtem Bityutskiy 			}
11111e51764aSArtem Bityutskiy 			break;
11121e51764aSArtem Bityutskiy 		default:
1113235c362bSSheng Yong 			ubifs_err(c, "unexpected node in log");
11141e51764aSArtem Bityutskiy 			goto out_dump;
11151e51764aSArtem Bityutskiy 		}
11161e51764aSArtem Bityutskiy 	}
11171e51764aSArtem Bityutskiy 
11181e51764aSArtem Bityutskiy 	if (sleb->endpt || c->lhead_offs >= c->leb_size) {
11191e51764aSArtem Bityutskiy 		c->lhead_lnum = lnum;
11201e51764aSArtem Bityutskiy 		c->lhead_offs = sleb->endpt;
11211e51764aSArtem Bityutskiy 	}
11221e51764aSArtem Bityutskiy 
11231e51764aSArtem Bityutskiy 	err = !sleb->endpt;
11241e51764aSArtem Bityutskiy out:
11251e51764aSArtem Bityutskiy 	ubifs_scan_destroy(sleb);
11261e51764aSArtem Bityutskiy 	return err;
11271e51764aSArtem Bityutskiy 
11281e51764aSArtem Bityutskiy out_dump:
1129235c362bSSheng Yong 	ubifs_err(c, "log error detected while replaying the log at LEB %d:%d",
11301e51764aSArtem Bityutskiy 		  lnum, offs + snod->offs);
1131a33e30a0SZhihao Cheng 	ubifs_dump_node(c, snod->node, c->leb_size - snod->offs);
11321e51764aSArtem Bityutskiy 	ubifs_scan_destroy(sleb);
11331e51764aSArtem Bityutskiy 	return -EINVAL;
11341e51764aSArtem Bityutskiy }
11351e51764aSArtem Bityutskiy 
11361e51764aSArtem Bityutskiy /**
11371e51764aSArtem Bityutskiy  * take_ihead - update the status of the index head in lprops to 'taken'.
11381e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
11391e51764aSArtem Bityutskiy  *
11401e51764aSArtem Bityutskiy  * This function returns the amount of free space in the index head LEB or a
11411e51764aSArtem Bityutskiy  * negative error code.
11421e51764aSArtem Bityutskiy  */
take_ihead(struct ubifs_info * c)11431e51764aSArtem Bityutskiy static int take_ihead(struct ubifs_info *c)
11441e51764aSArtem Bityutskiy {
11451e51764aSArtem Bityutskiy 	const struct ubifs_lprops *lp;
11461e51764aSArtem Bityutskiy 	int err, free;
11471e51764aSArtem Bityutskiy 
11481e51764aSArtem Bityutskiy 	ubifs_get_lprops(c);
11491e51764aSArtem Bityutskiy 
11501e51764aSArtem Bityutskiy 	lp = ubifs_lpt_lookup_dirty(c, c->ihead_lnum);
11511e51764aSArtem Bityutskiy 	if (IS_ERR(lp)) {
11521e51764aSArtem Bityutskiy 		err = PTR_ERR(lp);
11531e51764aSArtem Bityutskiy 		goto out;
11541e51764aSArtem Bityutskiy 	}
11551e51764aSArtem Bityutskiy 
11561e51764aSArtem Bityutskiy 	free = lp->free;
11571e51764aSArtem Bityutskiy 
11581e51764aSArtem Bityutskiy 	lp = ubifs_change_lp(c, lp, LPROPS_NC, LPROPS_NC,
11591e51764aSArtem Bityutskiy 			     lp->flags | LPROPS_TAKEN, 0);
11601e51764aSArtem Bityutskiy 	if (IS_ERR(lp)) {
11611e51764aSArtem Bityutskiy 		err = PTR_ERR(lp);
11621e51764aSArtem Bityutskiy 		goto out;
11631e51764aSArtem Bityutskiy 	}
11641e51764aSArtem Bityutskiy 
11651e51764aSArtem Bityutskiy 	err = free;
11661e51764aSArtem Bityutskiy out:
11671e51764aSArtem Bityutskiy 	ubifs_release_lprops(c);
11681e51764aSArtem Bityutskiy 	return err;
11691e51764aSArtem Bityutskiy }
11701e51764aSArtem Bityutskiy 
11711e51764aSArtem Bityutskiy /**
11721e51764aSArtem Bityutskiy  * ubifs_replay_journal - replay journal.
11731e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
11741e51764aSArtem Bityutskiy  *
11751e51764aSArtem Bityutskiy  * This function scans the journal, replays and cleans it up. It makes sure all
11761e51764aSArtem Bityutskiy  * memory data structures related to uncommitted journal are built (dirty TNC
11771e51764aSArtem Bityutskiy  * tree, tree of buds, modified lprops, etc).
11781e51764aSArtem Bityutskiy  */
ubifs_replay_journal(struct ubifs_info * c)11791e51764aSArtem Bityutskiy int ubifs_replay_journal(struct ubifs_info *c)
11801e51764aSArtem Bityutskiy {
1181d51f17eaSArtem Bityutskiy 	int err, lnum, free;
11821e51764aSArtem Bityutskiy 
11831e51764aSArtem Bityutskiy 	BUILD_BUG_ON(UBIFS_TRUN_KEY > 5);
11841e51764aSArtem Bityutskiy 
11851e51764aSArtem Bityutskiy 	/* Update the status of the index head in lprops to 'taken' */
11861e51764aSArtem Bityutskiy 	free = take_ihead(c);
11871e51764aSArtem Bityutskiy 	if (free < 0)
11881e51764aSArtem Bityutskiy 		return free; /* Error code */
11891e51764aSArtem Bityutskiy 
11901e51764aSArtem Bityutskiy 	if (c->ihead_offs != c->leb_size - free) {
1191235c362bSSheng Yong 		ubifs_err(c, "bad index head LEB %d:%d", c->ihead_lnum,
11921e51764aSArtem Bityutskiy 			  c->ihead_offs);
11931e51764aSArtem Bityutskiy 		return -EINVAL;
11941e51764aSArtem Bityutskiy 	}
11951e51764aSArtem Bityutskiy 
11961e51764aSArtem Bityutskiy 	dbg_mnt("start replaying the journal");
11971e51764aSArtem Bityutskiy 	c->replaying = 1;
11981e51764aSArtem Bityutskiy 	lnum = c->ltail_lnum = c->lhead_lnum;
11991e51764aSArtem Bityutskiy 
1200d51f17eaSArtem Bityutskiy 	do {
1201d51f17eaSArtem Bityutskiy 		err = replay_log_leb(c, lnum, 0, c->sbuf);
120288cff0f0Shujianyang 		if (err == 1) {
120388cff0f0Shujianyang 			if (lnum != c->lhead_lnum)
12041e51764aSArtem Bityutskiy 				/* We hit the end of the log */
12051e51764aSArtem Bityutskiy 				break;
120688cff0f0Shujianyang 
120788cff0f0Shujianyang 			/*
120888cff0f0Shujianyang 			 * The head of the log must always start with the
120988cff0f0Shujianyang 			 * "commit start" node on a properly formatted UBIFS.
121088cff0f0Shujianyang 			 * But we found no nodes at all, which means that
1211c7e593b3SSascha Hauer 			 * something went wrong and we cannot proceed mounting
121288cff0f0Shujianyang 			 * the file-system.
121388cff0f0Shujianyang 			 */
1214235c362bSSheng Yong 			ubifs_err(c, "no UBIFS nodes found at the log head LEB %d:%d, possibly corrupted",
121588cff0f0Shujianyang 				  lnum, 0);
121688cff0f0Shujianyang 			err = -EINVAL;
121788cff0f0Shujianyang 		}
12181e51764aSArtem Bityutskiy 		if (err)
12191e51764aSArtem Bityutskiy 			goto out;
1220d51f17eaSArtem Bityutskiy 		lnum = ubifs_next_log_lnum(c, lnum);
1221c212f402SArtem Bityutskiy 	} while (lnum != c->ltail_lnum);
12221e51764aSArtem Bityutskiy 
12231e51764aSArtem Bityutskiy 	err = replay_buds(c);
12241e51764aSArtem Bityutskiy 	if (err)
12251e51764aSArtem Bityutskiy 		goto out;
12261e51764aSArtem Bityutskiy 
1227debf12d5SArtem Bityutskiy 	err = apply_replay_list(c);
12281e51764aSArtem Bityutskiy 	if (err)
12291e51764aSArtem Bityutskiy 		goto out;
12301e51764aSArtem Bityutskiy 
1231074bcb9bSArtem Bityutskiy 	err = set_buds_lprops(c);
1232074bcb9bSArtem Bityutskiy 	if (err)
1233074bcb9bSArtem Bityutskiy 		goto out;
1234074bcb9bSArtem Bityutskiy 
12356edbfafdSArtem Bityutskiy 	/*
1236b137545cSArtem Bityutskiy 	 * UBIFS budgeting calculations use @c->bi.uncommitted_idx variable
1237b137545cSArtem Bityutskiy 	 * to roughly estimate index growth. Things like @c->bi.min_idx_lebs
12386edbfafdSArtem Bityutskiy 	 * depend on it. This means we have to initialize it to make sure
12396edbfafdSArtem Bityutskiy 	 * budgeting works properly.
12406edbfafdSArtem Bityutskiy 	 */
1241b137545cSArtem Bityutskiy 	c->bi.uncommitted_idx = atomic_long_read(&c->dirty_zn_cnt);
1242b137545cSArtem Bityutskiy 	c->bi.uncommitted_idx *= c->max_idx_node_sz;
12436edbfafdSArtem Bityutskiy 
12446eb61d58SRichard Weinberger 	ubifs_assert(c, c->bud_bytes <= c->max_bud_bytes || c->need_recovery);
124579fda517SArtem Bityutskiy 	dbg_mnt("finished, log head LEB %d:%d, max_sqnum %llu, highest_inum %lu",
124679fda517SArtem Bityutskiy 		c->lhead_lnum, c->lhead_offs, c->max_sqnum,
1247e84461adSArtem Bityutskiy 		(unsigned long)c->highest_inum);
12481e51764aSArtem Bityutskiy out:
1249debf12d5SArtem Bityutskiy 	destroy_replay_list(c);
12501e51764aSArtem Bityutskiy 	destroy_bud_list(c);
12511e51764aSArtem Bityutskiy 	c->replaying = 0;
12521e51764aSArtem Bityutskiy 	return err;
12531e51764aSArtem Bityutskiy }
1254