xref: /linux/fs/ubifs/replay.c (revision e58725d51fa8da9133f3f1c54170aa2e43056b91)
11e51764aSArtem Bityutskiy /*
21e51764aSArtem Bityutskiy  * This file is part of UBIFS.
31e51764aSArtem Bityutskiy  *
41e51764aSArtem Bityutskiy  * Copyright (C) 2006-2008 Nokia Corporation.
51e51764aSArtem Bityutskiy  *
61e51764aSArtem Bityutskiy  * This program is free software; you can redistribute it and/or modify it
71e51764aSArtem Bityutskiy  * under the terms of the GNU General Public License version 2 as published by
81e51764aSArtem Bityutskiy  * the Free Software Foundation.
91e51764aSArtem Bityutskiy  *
101e51764aSArtem Bityutskiy  * This program is distributed in the hope that it will be useful, but WITHOUT
111e51764aSArtem Bityutskiy  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
121e51764aSArtem Bityutskiy  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
131e51764aSArtem Bityutskiy  * more details.
141e51764aSArtem Bityutskiy  *
151e51764aSArtem Bityutskiy  * You should have received a copy of the GNU General Public License along with
161e51764aSArtem Bityutskiy  * this program; if not, write to the Free Software Foundation, Inc., 51
171e51764aSArtem Bityutskiy  * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
181e51764aSArtem Bityutskiy  *
191e51764aSArtem Bityutskiy  * Authors: Adrian Hunter
201e51764aSArtem Bityutskiy  *          Artem Bityutskiy (Битюцкий Артём)
211e51764aSArtem Bityutskiy  */
221e51764aSArtem Bityutskiy 
231e51764aSArtem Bityutskiy /*
241e51764aSArtem Bityutskiy  * This file contains journal replay code. It runs when the file-system is being
251e51764aSArtem Bityutskiy  * mounted and requires no locking.
261e51764aSArtem Bityutskiy  *
271e51764aSArtem Bityutskiy  * The larger is the journal, the longer it takes to scan it, so the longer it
281e51764aSArtem Bityutskiy  * takes to mount UBIFS. This is why the journal has limited size which may be
291e51764aSArtem Bityutskiy  * changed depending on the system requirements. But a larger journal gives
301e51764aSArtem Bityutskiy  * faster I/O speed because it writes the index less frequently. So this is a
311e51764aSArtem Bityutskiy  * trade-off. Also, the journal is indexed by the in-memory index (TNC), so the
321e51764aSArtem Bityutskiy  * larger is the journal, the more memory its index may consume.
331e51764aSArtem Bityutskiy  */
341e51764aSArtem Bityutskiy 
351e51764aSArtem Bityutskiy #include "ubifs.h"
36debf12d5SArtem Bityutskiy #include <linux/list_sort.h>
37da8ef65fSSascha Hauer #include <crypto/hash.h>
38da8ef65fSSascha Hauer #include <crypto/algapi.h>
391e51764aSArtem Bityutskiy 
401e51764aSArtem Bityutskiy /**
41debf12d5SArtem Bityutskiy  * struct replay_entry - replay list entry.
421e51764aSArtem Bityutskiy  * @lnum: logical eraseblock number of the node
431e51764aSArtem Bityutskiy  * @offs: node offset
441e51764aSArtem Bityutskiy  * @len: node length
45074bcb9bSArtem Bityutskiy  * @deletion: non-zero if this entry corresponds to a node deletion
461e51764aSArtem Bityutskiy  * @sqnum: node sequence number
47debf12d5SArtem Bityutskiy  * @list: links the replay list
481e51764aSArtem Bityutskiy  * @key: node key
491e51764aSArtem Bityutskiy  * @nm: directory entry name
501e51764aSArtem Bityutskiy  * @old_size: truncation old size
511e51764aSArtem Bityutskiy  * @new_size: truncation new size
521e51764aSArtem Bityutskiy  *
53debf12d5SArtem Bityutskiy  * The replay process first scans all buds and builds the replay list, then
54debf12d5SArtem Bityutskiy  * sorts the replay list in nodes sequence number order, and then inserts all
55debf12d5SArtem Bityutskiy  * the replay entries to the TNC.
561e51764aSArtem Bityutskiy  */
571e51764aSArtem Bityutskiy struct replay_entry {
581e51764aSArtem Bityutskiy 	int lnum;
591e51764aSArtem Bityutskiy 	int offs;
601e51764aSArtem Bityutskiy 	int len;
6116a26b20SSascha Hauer 	u8 hash[UBIFS_HASH_ARR_SZ];
62074bcb9bSArtem Bityutskiy 	unsigned int deletion:1;
631e51764aSArtem Bityutskiy 	unsigned long long sqnum;
64debf12d5SArtem Bityutskiy 	struct list_head list;
651e51764aSArtem Bityutskiy 	union ubifs_key key;
661e51764aSArtem Bityutskiy 	union {
67f4f61d2cSRichard Weinberger 		struct fscrypt_name nm;
681e51764aSArtem Bityutskiy 		struct {
691e51764aSArtem Bityutskiy 			loff_t old_size;
701e51764aSArtem Bityutskiy 			loff_t new_size;
711e51764aSArtem Bityutskiy 		};
721e51764aSArtem Bityutskiy 	};
731e51764aSArtem Bityutskiy };
741e51764aSArtem Bityutskiy 
751e51764aSArtem Bityutskiy /**
761e51764aSArtem Bityutskiy  * struct bud_entry - entry in the list of buds to replay.
771e51764aSArtem Bityutskiy  * @list: next bud in the list
781e51764aSArtem Bityutskiy  * @bud: bud description object
791e51764aSArtem Bityutskiy  * @sqnum: reference node sequence number
80af1dd412SArtem Bityutskiy  * @free: free bytes in the bud
81af1dd412SArtem Bityutskiy  * @dirty: dirty bytes in the bud
821e51764aSArtem Bityutskiy  */
831e51764aSArtem Bityutskiy struct bud_entry {
841e51764aSArtem Bityutskiy 	struct list_head list;
851e51764aSArtem Bityutskiy 	struct ubifs_bud *bud;
861e51764aSArtem Bityutskiy 	unsigned long long sqnum;
87af1dd412SArtem Bityutskiy 	int free;
88af1dd412SArtem Bityutskiy 	int dirty;
891e51764aSArtem Bityutskiy };
901e51764aSArtem Bityutskiy 
911e51764aSArtem Bityutskiy /**
921e51764aSArtem Bityutskiy  * set_bud_lprops - set free and dirty space used by a bud.
931e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
94074bcb9bSArtem Bityutskiy  * @b: bud entry which describes the bud
95074bcb9bSArtem Bityutskiy  *
96074bcb9bSArtem Bityutskiy  * This function makes sure the LEB properties of bud @b are set correctly
97074bcb9bSArtem Bityutskiy  * after the replay. Returns zero in case of success and a negative error code
98074bcb9bSArtem Bityutskiy  * in case of failure.
991e51764aSArtem Bityutskiy  */
100074bcb9bSArtem Bityutskiy static int set_bud_lprops(struct ubifs_info *c, struct bud_entry *b)
1011e51764aSArtem Bityutskiy {
1021e51764aSArtem Bityutskiy 	const struct ubifs_lprops *lp;
1031e51764aSArtem Bityutskiy 	int err = 0, dirty;
1041e51764aSArtem Bityutskiy 
1051e51764aSArtem Bityutskiy 	ubifs_get_lprops(c);
1061e51764aSArtem Bityutskiy 
107074bcb9bSArtem Bityutskiy 	lp = ubifs_lpt_lookup_dirty(c, b->bud->lnum);
1081e51764aSArtem Bityutskiy 	if (IS_ERR(lp)) {
1091e51764aSArtem Bityutskiy 		err = PTR_ERR(lp);
1101e51764aSArtem Bityutskiy 		goto out;
1111e51764aSArtem Bityutskiy 	}
1121e51764aSArtem Bityutskiy 
1131e51764aSArtem Bityutskiy 	dirty = lp->dirty;
114074bcb9bSArtem Bityutskiy 	if (b->bud->start == 0 && (lp->free != c->leb_size || lp->dirty != 0)) {
1151e51764aSArtem Bityutskiy 		/*
1161e51764aSArtem Bityutskiy 		 * The LEB was added to the journal with a starting offset of
1171e51764aSArtem Bityutskiy 		 * zero which means the LEB must have been empty. The LEB
118074bcb9bSArtem Bityutskiy 		 * property values should be @lp->free == @c->leb_size and
119074bcb9bSArtem Bityutskiy 		 * @lp->dirty == 0, but that is not the case. The reason is that
1207a9c3e39SArtem Bityutskiy 		 * the LEB had been garbage collected before it became the bud,
1217a9c3e39SArtem Bityutskiy 		 * and there was not commit inbetween. The garbage collector
1227a9c3e39SArtem Bityutskiy 		 * resets the free and dirty space without recording it
1237a9c3e39SArtem Bityutskiy 		 * anywhere except lprops, so if there was no commit then
1247a9c3e39SArtem Bityutskiy 		 * lprops does not have that information.
1251e51764aSArtem Bityutskiy 		 *
1261e51764aSArtem Bityutskiy 		 * We do not need to adjust free space because the scan has told
1271e51764aSArtem Bityutskiy 		 * us the exact value which is recorded in the replay entry as
128074bcb9bSArtem Bityutskiy 		 * @b->free.
1291e51764aSArtem Bityutskiy 		 *
1301e51764aSArtem Bityutskiy 		 * However we do need to subtract from the dirty space the
1311e51764aSArtem Bityutskiy 		 * amount of space that the garbage collector reclaimed, which
1321e51764aSArtem Bityutskiy 		 * is the whole LEB minus the amount of space that was free.
1331e51764aSArtem Bityutskiy 		 */
134074bcb9bSArtem Bityutskiy 		dbg_mnt("bud LEB %d was GC'd (%d free, %d dirty)", b->bud->lnum,
1351e51764aSArtem Bityutskiy 			lp->free, lp->dirty);
136074bcb9bSArtem Bityutskiy 		dbg_gc("bud LEB %d was GC'd (%d free, %d dirty)", b->bud->lnum,
1371e51764aSArtem Bityutskiy 			lp->free, lp->dirty);
1381e51764aSArtem Bityutskiy 		dirty -= c->leb_size - lp->free;
1391e51764aSArtem Bityutskiy 		/*
1401e51764aSArtem Bityutskiy 		 * If the replay order was perfect the dirty space would now be
1417d4e9ccbSArtem Bityutskiy 		 * zero. The order is not perfect because the journal heads
1421e51764aSArtem Bityutskiy 		 * race with each other. This is not a problem but is does mean
1431e51764aSArtem Bityutskiy 		 * that the dirty space may temporarily exceed c->leb_size
1441e51764aSArtem Bityutskiy 		 * during the replay.
1451e51764aSArtem Bityutskiy 		 */
1461e51764aSArtem Bityutskiy 		if (dirty != 0)
1473668b70fSArtem Bityutskiy 			dbg_mnt("LEB %d lp: %d free %d dirty replay: %d free %d dirty",
14879fda517SArtem Bityutskiy 				b->bud->lnum, lp->free, lp->dirty, b->free,
14979fda517SArtem Bityutskiy 				b->dirty);
1501e51764aSArtem Bityutskiy 	}
151074bcb9bSArtem Bityutskiy 	lp = ubifs_change_lp(c, lp, b->free, dirty + b->dirty,
1521e51764aSArtem Bityutskiy 			     lp->flags | LPROPS_TAKEN, 0);
1531e51764aSArtem Bityutskiy 	if (IS_ERR(lp)) {
1541e51764aSArtem Bityutskiy 		err = PTR_ERR(lp);
1551e51764aSArtem Bityutskiy 		goto out;
1561e51764aSArtem Bityutskiy 	}
15752c6e6f9SArtem Bityutskiy 
15852c6e6f9SArtem Bityutskiy 	/* Make sure the journal head points to the latest bud */
159074bcb9bSArtem Bityutskiy 	err = ubifs_wbuf_seek_nolock(&c->jheads[b->bud->jhead].wbuf,
160b36a261eSRichard Weinberger 				     b->bud->lnum, c->leb_size - b->free);
16152c6e6f9SArtem Bityutskiy 
1621e51764aSArtem Bityutskiy out:
1631e51764aSArtem Bityutskiy 	ubifs_release_lprops(c);
1641e51764aSArtem Bityutskiy 	return err;
1651e51764aSArtem Bityutskiy }
1661e51764aSArtem Bityutskiy 
1671e51764aSArtem Bityutskiy /**
168074bcb9bSArtem Bityutskiy  * set_buds_lprops - set free and dirty space for all replayed buds.
169074bcb9bSArtem Bityutskiy  * @c: UBIFS file-system description object
170074bcb9bSArtem Bityutskiy  *
171074bcb9bSArtem Bityutskiy  * This function sets LEB properties for all replayed buds. Returns zero in
172074bcb9bSArtem Bityutskiy  * case of success and a negative error code in case of failure.
173074bcb9bSArtem Bityutskiy  */
174074bcb9bSArtem Bityutskiy static int set_buds_lprops(struct ubifs_info *c)
175074bcb9bSArtem Bityutskiy {
176074bcb9bSArtem Bityutskiy 	struct bud_entry *b;
177074bcb9bSArtem Bityutskiy 	int err;
178074bcb9bSArtem Bityutskiy 
179074bcb9bSArtem Bityutskiy 	list_for_each_entry(b, &c->replay_buds, list) {
180074bcb9bSArtem Bityutskiy 		err = set_bud_lprops(c, b);
181074bcb9bSArtem Bityutskiy 		if (err)
182074bcb9bSArtem Bityutskiy 			return err;
183074bcb9bSArtem Bityutskiy 	}
184074bcb9bSArtem Bityutskiy 
185074bcb9bSArtem Bityutskiy 	return 0;
186074bcb9bSArtem Bityutskiy }
187074bcb9bSArtem Bityutskiy 
188074bcb9bSArtem Bityutskiy /**
1891e51764aSArtem Bityutskiy  * trun_remove_range - apply a replay entry for a truncation to the TNC.
1901e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
1911e51764aSArtem Bityutskiy  * @r: replay entry of truncation
1921e51764aSArtem Bityutskiy  */
1931e51764aSArtem Bityutskiy static int trun_remove_range(struct ubifs_info *c, struct replay_entry *r)
1941e51764aSArtem Bityutskiy {
1951e51764aSArtem Bityutskiy 	unsigned min_blk, max_blk;
1961e51764aSArtem Bityutskiy 	union ubifs_key min_key, max_key;
1971e51764aSArtem Bityutskiy 	ino_t ino;
1981e51764aSArtem Bityutskiy 
1991e51764aSArtem Bityutskiy 	min_blk = r->new_size / UBIFS_BLOCK_SIZE;
2001e51764aSArtem Bityutskiy 	if (r->new_size & (UBIFS_BLOCK_SIZE - 1))
2011e51764aSArtem Bityutskiy 		min_blk += 1;
2021e51764aSArtem Bityutskiy 
2031e51764aSArtem Bityutskiy 	max_blk = r->old_size / UBIFS_BLOCK_SIZE;
2041e51764aSArtem Bityutskiy 	if ((r->old_size & (UBIFS_BLOCK_SIZE - 1)) == 0)
2051e51764aSArtem Bityutskiy 		max_blk -= 1;
2061e51764aSArtem Bityutskiy 
2071e51764aSArtem Bityutskiy 	ino = key_inum(c, &r->key);
2081e51764aSArtem Bityutskiy 
2091e51764aSArtem Bityutskiy 	data_key_init(c, &min_key, ino, min_blk);
2101e51764aSArtem Bityutskiy 	data_key_init(c, &max_key, ino, max_blk);
2111e51764aSArtem Bityutskiy 
2121e51764aSArtem Bityutskiy 	return ubifs_tnc_remove_range(c, &min_key, &max_key);
2131e51764aSArtem Bityutskiy }
2141e51764aSArtem Bityutskiy 
2151e51764aSArtem Bityutskiy /**
216*e58725d5SRichard Weinberger  * inode_still_linked - check whether inode in question will be re-linked.
217*e58725d5SRichard Weinberger  * @c: UBIFS file-system description object
218*e58725d5SRichard Weinberger  * @rino: replay entry to test
219*e58725d5SRichard Weinberger  *
220*e58725d5SRichard Weinberger  * O_TMPFILE files can be re-linked, this means link count goes from 0 to 1.
221*e58725d5SRichard Weinberger  * This case needs special care, otherwise all references to the inode will
222*e58725d5SRichard Weinberger  * be removed upon the first replay entry of an inode with link count 0
223*e58725d5SRichard Weinberger  * is found.
224*e58725d5SRichard Weinberger  */
225*e58725d5SRichard Weinberger static bool inode_still_linked(struct ubifs_info *c, struct replay_entry *rino)
226*e58725d5SRichard Weinberger {
227*e58725d5SRichard Weinberger 	struct replay_entry *r;
228*e58725d5SRichard Weinberger 
229*e58725d5SRichard Weinberger 	ubifs_assert(c, rino->deletion);
230*e58725d5SRichard Weinberger 	ubifs_assert(c, key_type(c, &rino->key) == UBIFS_INO_KEY);
231*e58725d5SRichard Weinberger 
232*e58725d5SRichard Weinberger 	/*
233*e58725d5SRichard Weinberger 	 * Find the most recent entry for the inode behind @rino and check
234*e58725d5SRichard Weinberger 	 * whether it is a deletion.
235*e58725d5SRichard Weinberger 	 */
236*e58725d5SRichard Weinberger 	list_for_each_entry_reverse(r, &c->replay_list, list) {
237*e58725d5SRichard Weinberger 		ubifs_assert(c, r->sqnum >= rino->sqnum);
238*e58725d5SRichard Weinberger 		if (key_inum(c, &r->key) == key_inum(c, &rino->key))
239*e58725d5SRichard Weinberger 			return r->deletion == 0;
240*e58725d5SRichard Weinberger 
241*e58725d5SRichard Weinberger 	}
242*e58725d5SRichard Weinberger 
243*e58725d5SRichard Weinberger 	ubifs_assert(c, 0);
244*e58725d5SRichard Weinberger 	return false;
245*e58725d5SRichard Weinberger }
246*e58725d5SRichard Weinberger 
247*e58725d5SRichard Weinberger /**
2481e51764aSArtem Bityutskiy  * apply_replay_entry - apply a replay entry to the TNC.
2491e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
2501e51764aSArtem Bityutskiy  * @r: replay entry to apply
2511e51764aSArtem Bityutskiy  *
2521e51764aSArtem Bityutskiy  * Apply a replay entry to the TNC.
2531e51764aSArtem Bityutskiy  */
2541e51764aSArtem Bityutskiy static int apply_replay_entry(struct ubifs_info *c, struct replay_entry *r)
2551e51764aSArtem Bityutskiy {
256074bcb9bSArtem Bityutskiy 	int err;
2571e51764aSArtem Bityutskiy 
258515315a1SArtem Bityutskiy 	dbg_mntk(&r->key, "LEB %d:%d len %d deletion %d sqnum %llu key ",
259515315a1SArtem Bityutskiy 		 r->lnum, r->offs, r->len, r->deletion, r->sqnum);
2601e51764aSArtem Bityutskiy 
261074bcb9bSArtem Bityutskiy 	if (is_hash_key(c, &r->key)) {
262074bcb9bSArtem Bityutskiy 		if (r->deletion)
2631e51764aSArtem Bityutskiy 			err = ubifs_tnc_remove_nm(c, &r->key, &r->nm);
2641e51764aSArtem Bityutskiy 		else
2651e51764aSArtem Bityutskiy 			err = ubifs_tnc_add_nm(c, &r->key, r->lnum, r->offs,
26616a26b20SSascha Hauer 					       r->len, r->hash, &r->nm);
2671e51764aSArtem Bityutskiy 	} else {
268074bcb9bSArtem Bityutskiy 		if (r->deletion)
2691e51764aSArtem Bityutskiy 			switch (key_type(c, &r->key)) {
2701e51764aSArtem Bityutskiy 			case UBIFS_INO_KEY:
2711e51764aSArtem Bityutskiy 			{
2721e51764aSArtem Bityutskiy 				ino_t inum = key_inum(c, &r->key);
2731e51764aSArtem Bityutskiy 
274*e58725d5SRichard Weinberger 				if (inode_still_linked(c, r)) {
275*e58725d5SRichard Weinberger 					err = 0;
276*e58725d5SRichard Weinberger 					break;
277*e58725d5SRichard Weinberger 				}
278*e58725d5SRichard Weinberger 
2791e51764aSArtem Bityutskiy 				err = ubifs_tnc_remove_ino(c, inum);
2801e51764aSArtem Bityutskiy 				break;
2811e51764aSArtem Bityutskiy 			}
2821e51764aSArtem Bityutskiy 			case UBIFS_TRUN_KEY:
2831e51764aSArtem Bityutskiy 				err = trun_remove_range(c, r);
2841e51764aSArtem Bityutskiy 				break;
2851e51764aSArtem Bityutskiy 			default:
2861e51764aSArtem Bityutskiy 				err = ubifs_tnc_remove(c, &r->key);
2871e51764aSArtem Bityutskiy 				break;
2881e51764aSArtem Bityutskiy 			}
2891e51764aSArtem Bityutskiy 		else
2901e51764aSArtem Bityutskiy 			err = ubifs_tnc_add(c, &r->key, r->lnum, r->offs,
29116a26b20SSascha Hauer 					    r->len, r->hash);
2921e51764aSArtem Bityutskiy 		if (err)
2931e51764aSArtem Bityutskiy 			return err;
2941e51764aSArtem Bityutskiy 
2951e51764aSArtem Bityutskiy 		if (c->need_recovery)
296074bcb9bSArtem Bityutskiy 			err = ubifs_recover_size_accum(c, &r->key, r->deletion,
2971e51764aSArtem Bityutskiy 						       r->new_size);
2981e51764aSArtem Bityutskiy 	}
2991e51764aSArtem Bityutskiy 
3001e51764aSArtem Bityutskiy 	return err;
3011e51764aSArtem Bityutskiy }
3021e51764aSArtem Bityutskiy 
3031e51764aSArtem Bityutskiy /**
304debf12d5SArtem Bityutskiy  * replay_entries_cmp - compare 2 replay entries.
305debf12d5SArtem Bityutskiy  * @priv: UBIFS file-system description object
306debf12d5SArtem Bityutskiy  * @a: first replay entry
307ec037dfcSJulia Lawall  * @b: second replay entry
3081e51764aSArtem Bityutskiy  *
309debf12d5SArtem Bityutskiy  * This is a comparios function for 'list_sort()' which compares 2 replay
310debf12d5SArtem Bityutskiy  * entries @a and @b by comparing their sequence numer.  Returns %1 if @a has
311debf12d5SArtem Bityutskiy  * greater sequence number and %-1 otherwise.
3121e51764aSArtem Bityutskiy  */
313debf12d5SArtem Bityutskiy static int replay_entries_cmp(void *priv, struct list_head *a,
314debf12d5SArtem Bityutskiy 			      struct list_head *b)
3151e51764aSArtem Bityutskiy {
3166eb61d58SRichard Weinberger 	struct ubifs_info *c = priv;
317debf12d5SArtem Bityutskiy 	struct replay_entry *ra, *rb;
3181e51764aSArtem Bityutskiy 
319debf12d5SArtem Bityutskiy 	cond_resched();
320debf12d5SArtem Bityutskiy 	if (a == b)
321debf12d5SArtem Bityutskiy 		return 0;
322debf12d5SArtem Bityutskiy 
323debf12d5SArtem Bityutskiy 	ra = list_entry(a, struct replay_entry, list);
324debf12d5SArtem Bityutskiy 	rb = list_entry(b, struct replay_entry, list);
3256eb61d58SRichard Weinberger 	ubifs_assert(c, ra->sqnum != rb->sqnum);
326debf12d5SArtem Bityutskiy 	if (ra->sqnum > rb->sqnum)
327debf12d5SArtem Bityutskiy 		return 1;
328debf12d5SArtem Bityutskiy 	return -1;
3291e51764aSArtem Bityutskiy }
3301e51764aSArtem Bityutskiy 
3311e51764aSArtem Bityutskiy /**
332debf12d5SArtem Bityutskiy  * apply_replay_list - apply the replay list to the TNC.
3331e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
3341e51764aSArtem Bityutskiy  *
335debf12d5SArtem Bityutskiy  * Apply all entries in the replay list to the TNC. Returns zero in case of
336debf12d5SArtem Bityutskiy  * success and a negative error code in case of failure.
3371e51764aSArtem Bityutskiy  */
338debf12d5SArtem Bityutskiy static int apply_replay_list(struct ubifs_info *c)
3391e51764aSArtem Bityutskiy {
3401e51764aSArtem Bityutskiy 	struct replay_entry *r;
3411e51764aSArtem Bityutskiy 	int err;
3421e51764aSArtem Bityutskiy 
343debf12d5SArtem Bityutskiy 	list_sort(c, &c->replay_list, &replay_entries_cmp);
344debf12d5SArtem Bityutskiy 
345debf12d5SArtem Bityutskiy 	list_for_each_entry(r, &c->replay_list, list) {
3461e51764aSArtem Bityutskiy 		cond_resched();
3471e51764aSArtem Bityutskiy 
3481e51764aSArtem Bityutskiy 		err = apply_replay_entry(c, r);
3491e51764aSArtem Bityutskiy 		if (err)
3501e51764aSArtem Bityutskiy 			return err;
3511e51764aSArtem Bityutskiy 	}
352debf12d5SArtem Bityutskiy 
3531e51764aSArtem Bityutskiy 	return 0;
3541e51764aSArtem Bityutskiy }
3551e51764aSArtem Bityutskiy 
3561e51764aSArtem Bityutskiy /**
357debf12d5SArtem Bityutskiy  * destroy_replay_list - destroy the replay.
358debf12d5SArtem Bityutskiy  * @c: UBIFS file-system description object
359debf12d5SArtem Bityutskiy  *
360debf12d5SArtem Bityutskiy  * Destroy the replay list.
361debf12d5SArtem Bityutskiy  */
362debf12d5SArtem Bityutskiy static void destroy_replay_list(struct ubifs_info *c)
363debf12d5SArtem Bityutskiy {
364debf12d5SArtem Bityutskiy 	struct replay_entry *r, *tmp;
365debf12d5SArtem Bityutskiy 
366debf12d5SArtem Bityutskiy 	list_for_each_entry_safe(r, tmp, &c->replay_list, list) {
367debf12d5SArtem Bityutskiy 		if (is_hash_key(c, &r->key))
368f4f61d2cSRichard Weinberger 			kfree(fname_name(&r->nm));
369debf12d5SArtem Bityutskiy 		list_del(&r->list);
370debf12d5SArtem Bityutskiy 		kfree(r);
371debf12d5SArtem Bityutskiy 	}
372debf12d5SArtem Bityutskiy }
373debf12d5SArtem Bityutskiy 
374debf12d5SArtem Bityutskiy /**
375debf12d5SArtem Bityutskiy  * insert_node - insert a node to the replay list
3761e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
3771e51764aSArtem Bityutskiy  * @lnum: node logical eraseblock number
3781e51764aSArtem Bityutskiy  * @offs: node offset
3791e51764aSArtem Bityutskiy  * @len: node length
3801e51764aSArtem Bityutskiy  * @key: node key
3811e51764aSArtem Bityutskiy  * @sqnum: sequence number
3821e51764aSArtem Bityutskiy  * @deletion: non-zero if this is a deletion
3831e51764aSArtem Bityutskiy  * @used: number of bytes in use in a LEB
3841e51764aSArtem Bityutskiy  * @old_size: truncation old size
3851e51764aSArtem Bityutskiy  * @new_size: truncation new size
3861e51764aSArtem Bityutskiy  *
387debf12d5SArtem Bityutskiy  * This function inserts a scanned non-direntry node to the replay list. The
388debf12d5SArtem Bityutskiy  * replay list contains @struct replay_entry elements, and we sort this list in
389debf12d5SArtem Bityutskiy  * sequence number order before applying it. The replay list is applied at the
390debf12d5SArtem Bityutskiy  * very end of the replay process. Since the list is sorted in sequence number
391debf12d5SArtem Bityutskiy  * order, the older modifications are applied first. This function returns zero
392debf12d5SArtem Bityutskiy  * in case of success and a negative error code in case of failure.
3931e51764aSArtem Bityutskiy  */
3941e51764aSArtem Bityutskiy static int insert_node(struct ubifs_info *c, int lnum, int offs, int len,
39516a26b20SSascha Hauer 		       const u8 *hash, union ubifs_key *key,
39616a26b20SSascha Hauer 		       unsigned long long sqnum, int deletion, int *used,
39716a26b20SSascha Hauer 		       loff_t old_size, loff_t new_size)
3981e51764aSArtem Bityutskiy {
3991e51764aSArtem Bityutskiy 	struct replay_entry *r;
4001e51764aSArtem Bityutskiy 
401515315a1SArtem Bityutskiy 	dbg_mntk(key, "add LEB %d:%d, key ", lnum, offs);
402debf12d5SArtem Bityutskiy 
4031e51764aSArtem Bityutskiy 	if (key_inum(c, key) >= c->highest_inum)
4041e51764aSArtem Bityutskiy 		c->highest_inum = key_inum(c, key);
4051e51764aSArtem Bityutskiy 
4061e51764aSArtem Bityutskiy 	r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL);
4071e51764aSArtem Bityutskiy 	if (!r)
4081e51764aSArtem Bityutskiy 		return -ENOMEM;
4091e51764aSArtem Bityutskiy 
4101e51764aSArtem Bityutskiy 	if (!deletion)
4111e51764aSArtem Bityutskiy 		*used += ALIGN(len, 8);
4121e51764aSArtem Bityutskiy 	r->lnum = lnum;
4131e51764aSArtem Bityutskiy 	r->offs = offs;
4141e51764aSArtem Bityutskiy 	r->len = len;
41516a26b20SSascha Hauer 	ubifs_copy_hash(c, hash, r->hash);
416074bcb9bSArtem Bityutskiy 	r->deletion = !!deletion;
4171e51764aSArtem Bityutskiy 	r->sqnum = sqnum;
418074bcb9bSArtem Bityutskiy 	key_copy(c, key, &r->key);
4191e51764aSArtem Bityutskiy 	r->old_size = old_size;
4201e51764aSArtem Bityutskiy 	r->new_size = new_size;
4211e51764aSArtem Bityutskiy 
422debf12d5SArtem Bityutskiy 	list_add_tail(&r->list, &c->replay_list);
4231e51764aSArtem Bityutskiy 	return 0;
4241e51764aSArtem Bityutskiy }
4251e51764aSArtem Bityutskiy 
4261e51764aSArtem Bityutskiy /**
427debf12d5SArtem Bityutskiy  * insert_dent - insert a directory entry node into the replay list.
4281e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
4291e51764aSArtem Bityutskiy  * @lnum: node logical eraseblock number
4301e51764aSArtem Bityutskiy  * @offs: node offset
4311e51764aSArtem Bityutskiy  * @len: node length
4321e51764aSArtem Bityutskiy  * @key: node key
4331e51764aSArtem Bityutskiy  * @name: directory entry name
4341e51764aSArtem Bityutskiy  * @nlen: directory entry name length
4351e51764aSArtem Bityutskiy  * @sqnum: sequence number
4361e51764aSArtem Bityutskiy  * @deletion: non-zero if this is a deletion
4371e51764aSArtem Bityutskiy  * @used: number of bytes in use in a LEB
4381e51764aSArtem Bityutskiy  *
439debf12d5SArtem Bityutskiy  * This function inserts a scanned directory entry node or an extended
440debf12d5SArtem Bityutskiy  * attribute entry to the replay list. Returns zero in case of success and a
441debf12d5SArtem Bityutskiy  * negative error code in case of failure.
4421e51764aSArtem Bityutskiy  */
4431e51764aSArtem Bityutskiy static int insert_dent(struct ubifs_info *c, int lnum, int offs, int len,
44416a26b20SSascha Hauer 		       const u8 *hash, union ubifs_key *key,
44516a26b20SSascha Hauer 		       const char *name, int nlen, unsigned long long sqnum,
44616a26b20SSascha Hauer 		       int deletion, int *used)
4471e51764aSArtem Bityutskiy {
4481e51764aSArtem Bityutskiy 	struct replay_entry *r;
4491e51764aSArtem Bityutskiy 	char *nbuf;
4501e51764aSArtem Bityutskiy 
451515315a1SArtem Bityutskiy 	dbg_mntk(key, "add LEB %d:%d, key ", lnum, offs);
4521e51764aSArtem Bityutskiy 	if (key_inum(c, key) >= c->highest_inum)
4531e51764aSArtem Bityutskiy 		c->highest_inum = key_inum(c, key);
4541e51764aSArtem Bityutskiy 
4551e51764aSArtem Bityutskiy 	r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL);
4561e51764aSArtem Bityutskiy 	if (!r)
4571e51764aSArtem Bityutskiy 		return -ENOMEM;
458debf12d5SArtem Bityutskiy 
4591e51764aSArtem Bityutskiy 	nbuf = kmalloc(nlen + 1, GFP_KERNEL);
4601e51764aSArtem Bityutskiy 	if (!nbuf) {
4611e51764aSArtem Bityutskiy 		kfree(r);
4621e51764aSArtem Bityutskiy 		return -ENOMEM;
4631e51764aSArtem Bityutskiy 	}
4641e51764aSArtem Bityutskiy 
4651e51764aSArtem Bityutskiy 	if (!deletion)
4661e51764aSArtem Bityutskiy 		*used += ALIGN(len, 8);
4671e51764aSArtem Bityutskiy 	r->lnum = lnum;
4681e51764aSArtem Bityutskiy 	r->offs = offs;
4691e51764aSArtem Bityutskiy 	r->len = len;
47016a26b20SSascha Hauer 	ubifs_copy_hash(c, hash, r->hash);
471074bcb9bSArtem Bityutskiy 	r->deletion = !!deletion;
4721e51764aSArtem Bityutskiy 	r->sqnum = sqnum;
473074bcb9bSArtem Bityutskiy 	key_copy(c, key, &r->key);
474f4f61d2cSRichard Weinberger 	fname_len(&r->nm) = nlen;
4751e51764aSArtem Bityutskiy 	memcpy(nbuf, name, nlen);
4761e51764aSArtem Bityutskiy 	nbuf[nlen] = '\0';
477f4f61d2cSRichard Weinberger 	fname_name(&r->nm) = nbuf;
4781e51764aSArtem Bityutskiy 
479debf12d5SArtem Bityutskiy 	list_add_tail(&r->list, &c->replay_list);
4801e51764aSArtem Bityutskiy 	return 0;
4811e51764aSArtem Bityutskiy }
4821e51764aSArtem Bityutskiy 
4831e51764aSArtem Bityutskiy /**
4841e51764aSArtem Bityutskiy  * ubifs_validate_entry - validate directory or extended attribute entry node.
4851e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
4861e51764aSArtem Bityutskiy  * @dent: the node to validate
4871e51764aSArtem Bityutskiy  *
4881e51764aSArtem Bityutskiy  * This function validates directory or extended attribute entry node @dent.
4891e51764aSArtem Bityutskiy  * Returns zero if the node is all right and a %-EINVAL if not.
4901e51764aSArtem Bityutskiy  */
4911e51764aSArtem Bityutskiy int ubifs_validate_entry(struct ubifs_info *c,
4921e51764aSArtem Bityutskiy 			 const struct ubifs_dent_node *dent)
4931e51764aSArtem Bityutskiy {
4941e51764aSArtem Bityutskiy 	int key_type = key_type_flash(c, dent->key);
4951e51764aSArtem Bityutskiy 	int nlen = le16_to_cpu(dent->nlen);
4961e51764aSArtem Bityutskiy 
4971e51764aSArtem Bityutskiy 	if (le32_to_cpu(dent->ch.len) != nlen + UBIFS_DENT_NODE_SZ + 1 ||
4981e51764aSArtem Bityutskiy 	    dent->type >= UBIFS_ITYPES_CNT ||
4991e51764aSArtem Bityutskiy 	    nlen > UBIFS_MAX_NLEN || dent->name[nlen] != 0 ||
500304790c0SRichard Weinberger 	    (key_type == UBIFS_XENT_KEY && strnlen(dent->name, nlen) != nlen) ||
5011e51764aSArtem Bityutskiy 	    le64_to_cpu(dent->inum) > MAX_INUM) {
502235c362bSSheng Yong 		ubifs_err(c, "bad %s node", key_type == UBIFS_DENT_KEY ?
5031e51764aSArtem Bityutskiy 			  "directory entry" : "extended attribute entry");
5041e51764aSArtem Bityutskiy 		return -EINVAL;
5051e51764aSArtem Bityutskiy 	}
5061e51764aSArtem Bityutskiy 
5071e51764aSArtem Bityutskiy 	if (key_type != UBIFS_DENT_KEY && key_type != UBIFS_XENT_KEY) {
508235c362bSSheng Yong 		ubifs_err(c, "bad key type %d", key_type);
5091e51764aSArtem Bityutskiy 		return -EINVAL;
5101e51764aSArtem Bityutskiy 	}
5111e51764aSArtem Bityutskiy 
5121e51764aSArtem Bityutskiy 	return 0;
5131e51764aSArtem Bityutskiy }
5141e51764aSArtem Bityutskiy 
5151e51764aSArtem Bityutskiy /**
51691c66083SArtem Bityutskiy  * is_last_bud - check if the bud is the last in the journal head.
51791c66083SArtem Bityutskiy  * @c: UBIFS file-system description object
51891c66083SArtem Bityutskiy  * @bud: bud description object
51991c66083SArtem Bityutskiy  *
52091c66083SArtem Bityutskiy  * This function checks if bud @bud is the last bud in its journal head. This
52191c66083SArtem Bityutskiy  * information is then used by 'replay_bud()' to decide whether the bud can
52291c66083SArtem Bityutskiy  * have corruptions or not. Indeed, only last buds can be corrupted by power
52391c66083SArtem Bityutskiy  * cuts. Returns %1 if this is the last bud, and %0 if not.
52491c66083SArtem Bityutskiy  */
52591c66083SArtem Bityutskiy static int is_last_bud(struct ubifs_info *c, struct ubifs_bud *bud)
52691c66083SArtem Bityutskiy {
52791c66083SArtem Bityutskiy 	struct ubifs_jhead *jh = &c->jheads[bud->jhead];
52891c66083SArtem Bityutskiy 	struct ubifs_bud *next;
52991c66083SArtem Bityutskiy 	uint32_t data;
53091c66083SArtem Bityutskiy 	int err;
53191c66083SArtem Bityutskiy 
53291c66083SArtem Bityutskiy 	if (list_is_last(&bud->list, &jh->buds_list))
53391c66083SArtem Bityutskiy 		return 1;
53491c66083SArtem Bityutskiy 
53591c66083SArtem Bityutskiy 	/*
53691c66083SArtem Bityutskiy 	 * The following is a quirk to make sure we work correctly with UBIFS
53791c66083SArtem Bityutskiy 	 * images used with older UBIFS.
53891c66083SArtem Bityutskiy 	 *
53991c66083SArtem Bityutskiy 	 * Normally, the last bud will be the last in the journal head's list
54091c66083SArtem Bityutskiy 	 * of bud. However, there is one exception if the UBIFS image belongs
54191c66083SArtem Bityutskiy 	 * to older UBIFS. This is fairly unlikely: one would need to use old
54291c66083SArtem Bityutskiy 	 * UBIFS, then have a power cut exactly at the right point, and then
54391c66083SArtem Bityutskiy 	 * try to mount this image with new UBIFS.
54491c66083SArtem Bityutskiy 	 *
54591c66083SArtem Bityutskiy 	 * The exception is: it is possible to have 2 buds A and B, A goes
54691c66083SArtem Bityutskiy 	 * before B, and B is the last, bud B is contains no data, and bud A is
54791c66083SArtem Bityutskiy 	 * corrupted at the end. The reason is that in older versions when the
54891c66083SArtem Bityutskiy 	 * journal code switched the next bud (from A to B), it first added a
54991c66083SArtem Bityutskiy 	 * log reference node for the new bud (B), and only after this it
55091c66083SArtem Bityutskiy 	 * synchronized the write-buffer of current bud (A). But later this was
55191c66083SArtem Bityutskiy 	 * changed and UBIFS started to always synchronize the write-buffer of
55291c66083SArtem Bityutskiy 	 * the bud (A) before writing the log reference for the new bud (B).
55391c66083SArtem Bityutskiy 	 *
55491c66083SArtem Bityutskiy 	 * But because older UBIFS always synchronized A's write-buffer before
55591c66083SArtem Bityutskiy 	 * writing to B, we can recognize this exceptional situation but
55691c66083SArtem Bityutskiy 	 * checking the contents of bud B - if it is empty, then A can be
55791c66083SArtem Bityutskiy 	 * treated as the last and we can recover it.
55891c66083SArtem Bityutskiy 	 *
55991c66083SArtem Bityutskiy 	 * TODO: remove this piece of code in a couple of years (today it is
56091c66083SArtem Bityutskiy 	 * 16.05.2011).
56191c66083SArtem Bityutskiy 	 */
56291c66083SArtem Bityutskiy 	next = list_entry(bud->list.next, struct ubifs_bud, list);
56391c66083SArtem Bityutskiy 	if (!list_is_last(&next->list, &jh->buds_list))
56491c66083SArtem Bityutskiy 		return 0;
56591c66083SArtem Bityutskiy 
566d304820aSArtem Bityutskiy 	err = ubifs_leb_read(c, next->lnum, (char *)&data, next->start, 4, 1);
56791c66083SArtem Bityutskiy 	if (err)
56891c66083SArtem Bityutskiy 		return 0;
56991c66083SArtem Bityutskiy 
57091c66083SArtem Bityutskiy 	return data == 0xFFFFFFFF;
57191c66083SArtem Bityutskiy }
57291c66083SArtem Bityutskiy 
573eb66eff6SArnd Bergmann /* authenticate_sleb_hash and authenticate_sleb_hmac are split out for stack usage */
574eb66eff6SArnd Bergmann static int authenticate_sleb_hash(struct ubifs_info *c, struct shash_desc *log_hash, u8 *hash)
575eb66eff6SArnd Bergmann {
576eb66eff6SArnd Bergmann 	SHASH_DESC_ON_STACK(hash_desc, c->hash_tfm);
577eb66eff6SArnd Bergmann 
578eb66eff6SArnd Bergmann 	hash_desc->tfm = c->hash_tfm;
579eb66eff6SArnd Bergmann 	hash_desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
580eb66eff6SArnd Bergmann 
581eb66eff6SArnd Bergmann 	ubifs_shash_copy_state(c, log_hash, hash_desc);
582eb66eff6SArnd Bergmann 	return crypto_shash_final(hash_desc, hash);
583eb66eff6SArnd Bergmann }
584eb66eff6SArnd Bergmann 
585eb66eff6SArnd Bergmann static int authenticate_sleb_hmac(struct ubifs_info *c, u8 *hash, u8 *hmac)
586eb66eff6SArnd Bergmann {
587eb66eff6SArnd Bergmann 	SHASH_DESC_ON_STACK(hmac_desc, c->hmac_tfm);
588eb66eff6SArnd Bergmann 
589eb66eff6SArnd Bergmann 	hmac_desc->tfm = c->hmac_tfm;
590eb66eff6SArnd Bergmann 	hmac_desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP;
591eb66eff6SArnd Bergmann 
592eb66eff6SArnd Bergmann 	return crypto_shash_digest(hmac_desc, hash, c->hash_len, hmac);
593eb66eff6SArnd Bergmann }
594eb66eff6SArnd Bergmann 
59591c66083SArtem Bityutskiy /**
596da8ef65fSSascha Hauer  * authenticate_sleb - authenticate one scan LEB
597da8ef65fSSascha Hauer  * @c: UBIFS file-system description object
598da8ef65fSSascha Hauer  * @sleb: the scan LEB to authenticate
599da8ef65fSSascha Hauer  * @log_hash:
600da8ef65fSSascha Hauer  * @is_last: if true, this is is the last LEB
601da8ef65fSSascha Hauer  *
602da8ef65fSSascha Hauer  * This function iterates over the buds of a single LEB authenticating all buds
603da8ef65fSSascha Hauer  * with the authentication nodes on this LEB. Authentication nodes are written
604da8ef65fSSascha Hauer  * after some buds and contain a HMAC covering the authentication node itself
605da8ef65fSSascha Hauer  * and the buds between the last authentication node and the current
606da8ef65fSSascha Hauer  * authentication node. It can happen that the last buds cannot be authenticated
607da8ef65fSSascha Hauer  * because a powercut happened when some nodes were written but not the
608da8ef65fSSascha Hauer  * corresponding authentication node. This function returns the number of nodes
609da8ef65fSSascha Hauer  * that could be authenticated or a negative error code.
610da8ef65fSSascha Hauer  */
611da8ef65fSSascha Hauer static int authenticate_sleb(struct ubifs_info *c, struct ubifs_scan_leb *sleb,
612da8ef65fSSascha Hauer 			     struct shash_desc *log_hash, int is_last)
613da8ef65fSSascha Hauer {
614da8ef65fSSascha Hauer 	int n_not_auth = 0;
615da8ef65fSSascha Hauer 	struct ubifs_scan_node *snod;
616da8ef65fSSascha Hauer 	int n_nodes = 0;
617da8ef65fSSascha Hauer 	int err;
618da8ef65fSSascha Hauer 	u8 *hash, *hmac;
619da8ef65fSSascha Hauer 
620da8ef65fSSascha Hauer 	if (!ubifs_authenticated(c))
621da8ef65fSSascha Hauer 		return sleb->nodes_cnt;
622da8ef65fSSascha Hauer 
623da8ef65fSSascha Hauer 	hash = kmalloc(crypto_shash_descsize(c->hash_tfm), GFP_NOFS);
624da8ef65fSSascha Hauer 	hmac = kmalloc(c->hmac_desc_len, GFP_NOFS);
625da8ef65fSSascha Hauer 	if (!hash || !hmac) {
626da8ef65fSSascha Hauer 		err = -ENOMEM;
627da8ef65fSSascha Hauer 		goto out;
628da8ef65fSSascha Hauer 	}
629da8ef65fSSascha Hauer 
630da8ef65fSSascha Hauer 	list_for_each_entry(snod, &sleb->nodes, list) {
631da8ef65fSSascha Hauer 
632da8ef65fSSascha Hauer 		n_nodes++;
633da8ef65fSSascha Hauer 
634da8ef65fSSascha Hauer 		if (snod->type == UBIFS_AUTH_NODE) {
635da8ef65fSSascha Hauer 			struct ubifs_auth_node *auth = snod->node;
636da8ef65fSSascha Hauer 
637eb66eff6SArnd Bergmann 			err = authenticate_sleb_hash(c, log_hash, hash);
638da8ef65fSSascha Hauer 			if (err)
639da8ef65fSSascha Hauer 				goto out;
640da8ef65fSSascha Hauer 
641eb66eff6SArnd Bergmann 			err = authenticate_sleb_hmac(c, hash, hmac);
642da8ef65fSSascha Hauer 			if (err)
643da8ef65fSSascha Hauer 				goto out;
644da8ef65fSSascha Hauer 
645da8ef65fSSascha Hauer 			err = ubifs_check_hmac(c, auth->hmac, hmac);
646da8ef65fSSascha Hauer 			if (err) {
647da8ef65fSSascha Hauer 				err = -EPERM;
648da8ef65fSSascha Hauer 				goto out;
649da8ef65fSSascha Hauer 			}
650da8ef65fSSascha Hauer 			n_not_auth = 0;
651da8ef65fSSascha Hauer 		} else {
652da8ef65fSSascha Hauer 			err = crypto_shash_update(log_hash, snod->node,
653da8ef65fSSascha Hauer 						  snod->len);
654da8ef65fSSascha Hauer 			if (err)
655da8ef65fSSascha Hauer 				goto out;
656da8ef65fSSascha Hauer 			n_not_auth++;
657da8ef65fSSascha Hauer 		}
658da8ef65fSSascha Hauer 	}
659da8ef65fSSascha Hauer 
660da8ef65fSSascha Hauer 	/*
661da8ef65fSSascha Hauer 	 * A powercut can happen when some nodes were written, but not yet
662da8ef65fSSascha Hauer 	 * the corresponding authentication node. This may only happen on
663da8ef65fSSascha Hauer 	 * the last bud though.
664da8ef65fSSascha Hauer 	 */
665da8ef65fSSascha Hauer 	if (n_not_auth) {
666da8ef65fSSascha Hauer 		if (is_last) {
667da8ef65fSSascha Hauer 			dbg_mnt("%d unauthenticated nodes found on LEB %d, Ignoring them",
668da8ef65fSSascha Hauer 				n_not_auth, sleb->lnum);
669da8ef65fSSascha Hauer 			err = 0;
670da8ef65fSSascha Hauer 		} else {
671da8ef65fSSascha Hauer 			dbg_mnt("%d unauthenticated nodes found on non-last LEB %d",
672da8ef65fSSascha Hauer 				n_not_auth, sleb->lnum);
673da8ef65fSSascha Hauer 			err = -EPERM;
674da8ef65fSSascha Hauer 		}
675da8ef65fSSascha Hauer 	} else {
676da8ef65fSSascha Hauer 		err = 0;
677da8ef65fSSascha Hauer 	}
678da8ef65fSSascha Hauer out:
679da8ef65fSSascha Hauer 	kfree(hash);
680da8ef65fSSascha Hauer 	kfree(hmac);
681da8ef65fSSascha Hauer 
682da8ef65fSSascha Hauer 	return err ? err : n_nodes - n_not_auth;
683da8ef65fSSascha Hauer }
684da8ef65fSSascha Hauer 
685da8ef65fSSascha Hauer /**
6861e51764aSArtem Bityutskiy  * replay_bud - replay a bud logical eraseblock.
6871e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
688e76a4526SArtem Bityutskiy  * @b: bud entry which describes the bud
6891e51764aSArtem Bityutskiy  *
690e76a4526SArtem Bityutskiy  * This function replays bud @bud, recovers it if needed, and adds all nodes
691e76a4526SArtem Bityutskiy  * from this bud to the replay list. Returns zero in case of success and a
692e76a4526SArtem Bityutskiy  * negative error code in case of failure.
6931e51764aSArtem Bityutskiy  */
694e76a4526SArtem Bityutskiy static int replay_bud(struct ubifs_info *c, struct bud_entry *b)
6951e51764aSArtem Bityutskiy {
69691c66083SArtem Bityutskiy 	int is_last = is_last_bud(c, b->bud);
697e76a4526SArtem Bityutskiy 	int err = 0, used = 0, lnum = b->bud->lnum, offs = b->bud->start;
698da8ef65fSSascha Hauer 	int n_nodes, n = 0;
6991e51764aSArtem Bityutskiy 	struct ubifs_scan_leb *sleb;
7001e51764aSArtem Bityutskiy 	struct ubifs_scan_node *snod;
7011e51764aSArtem Bityutskiy 
70291c66083SArtem Bityutskiy 	dbg_mnt("replay bud LEB %d, head %d, offs %d, is_last %d",
70391c66083SArtem Bityutskiy 		lnum, b->bud->jhead, offs, is_last);
704e76a4526SArtem Bityutskiy 
70591c66083SArtem Bityutskiy 	if (c->need_recovery && is_last)
70691c66083SArtem Bityutskiy 		/*
70791c66083SArtem Bityutskiy 		 * Recover only last LEBs in the journal heads, because power
70891c66083SArtem Bityutskiy 		 * cuts may cause corruptions only in these LEBs, because only
70991c66083SArtem Bityutskiy 		 * these LEBs could possibly be written to at the power cut
71091c66083SArtem Bityutskiy 		 * time.
71191c66083SArtem Bityutskiy 		 */
712efcfde54SArtem Bityutskiy 		sleb = ubifs_recover_leb(c, lnum, offs, c->sbuf, b->bud->jhead);
7131e51764aSArtem Bityutskiy 	else
714348709baSArtem Bityutskiy 		sleb = ubifs_scan(c, lnum, offs, c->sbuf, 0);
7151e51764aSArtem Bityutskiy 	if (IS_ERR(sleb))
7161e51764aSArtem Bityutskiy 		return PTR_ERR(sleb);
7171e51764aSArtem Bityutskiy 
718da8ef65fSSascha Hauer 	n_nodes = authenticate_sleb(c, sleb, b->bud->log_hash, is_last);
719da8ef65fSSascha Hauer 	if (n_nodes < 0) {
720da8ef65fSSascha Hauer 		err = n_nodes;
721da8ef65fSSascha Hauer 		goto out;
722da8ef65fSSascha Hauer 	}
723da8ef65fSSascha Hauer 
724da8ef65fSSascha Hauer 	ubifs_shash_copy_state(c, b->bud->log_hash,
725da8ef65fSSascha Hauer 			       c->jheads[b->bud->jhead].log_hash);
726da8ef65fSSascha Hauer 
7271e51764aSArtem Bityutskiy 	/*
7281e51764aSArtem Bityutskiy 	 * The bud does not have to start from offset zero - the beginning of
7291e51764aSArtem Bityutskiy 	 * the 'lnum' LEB may contain previously committed data. One of the
7301e51764aSArtem Bityutskiy 	 * things we have to do in replay is to correctly update lprops with
7311e51764aSArtem Bityutskiy 	 * newer information about this LEB.
7321e51764aSArtem Bityutskiy 	 *
7331e51764aSArtem Bityutskiy 	 * At this point lprops thinks that this LEB has 'c->leb_size - offs'
7341e51764aSArtem Bityutskiy 	 * bytes of free space because it only contain information about
7351e51764aSArtem Bityutskiy 	 * committed data.
7361e51764aSArtem Bityutskiy 	 *
7371e51764aSArtem Bityutskiy 	 * But we know that real amount of free space is 'c->leb_size -
7381e51764aSArtem Bityutskiy 	 * sleb->endpt', and the space in the 'lnum' LEB between 'offs' and
7391e51764aSArtem Bityutskiy 	 * 'sleb->endpt' is used by bud data. We have to correctly calculate
7401e51764aSArtem Bityutskiy 	 * how much of these data are dirty and update lprops with this
7411e51764aSArtem Bityutskiy 	 * information.
7421e51764aSArtem Bityutskiy 	 *
7431e51764aSArtem Bityutskiy 	 * The dirt in that LEB region is comprised of padding nodes, deletion
7441e51764aSArtem Bityutskiy 	 * nodes, truncation nodes and nodes which are obsoleted by subsequent
7451e51764aSArtem Bityutskiy 	 * nodes in this LEB. So instead of calculating clean space, we
7461e51764aSArtem Bityutskiy 	 * calculate used space ('used' variable).
7471e51764aSArtem Bityutskiy 	 */
7481e51764aSArtem Bityutskiy 
7491e51764aSArtem Bityutskiy 	list_for_each_entry(snod, &sleb->nodes, list) {
75016a26b20SSascha Hauer 		u8 hash[UBIFS_HASH_ARR_SZ];
7511e51764aSArtem Bityutskiy 		int deletion = 0;
7521e51764aSArtem Bityutskiy 
7531e51764aSArtem Bityutskiy 		cond_resched();
7541e51764aSArtem Bityutskiy 
7551e51764aSArtem Bityutskiy 		if (snod->sqnum >= SQNUM_WATERMARK) {
756235c362bSSheng Yong 			ubifs_err(c, "file system's life ended");
7571e51764aSArtem Bityutskiy 			goto out_dump;
7581e51764aSArtem Bityutskiy 		}
7591e51764aSArtem Bityutskiy 
76016a26b20SSascha Hauer 		ubifs_node_calc_hash(c, snod->node, hash);
76116a26b20SSascha Hauer 
7621e51764aSArtem Bityutskiy 		if (snod->sqnum > c->max_sqnum)
7631e51764aSArtem Bityutskiy 			c->max_sqnum = snod->sqnum;
7641e51764aSArtem Bityutskiy 
7651e51764aSArtem Bityutskiy 		switch (snod->type) {
7661e51764aSArtem Bityutskiy 		case UBIFS_INO_NODE:
7671e51764aSArtem Bityutskiy 		{
7681e51764aSArtem Bityutskiy 			struct ubifs_ino_node *ino = snod->node;
7691e51764aSArtem Bityutskiy 			loff_t new_size = le64_to_cpu(ino->size);
7701e51764aSArtem Bityutskiy 
7711e51764aSArtem Bityutskiy 			if (le32_to_cpu(ino->nlink) == 0)
7721e51764aSArtem Bityutskiy 				deletion = 1;
77316a26b20SSascha Hauer 			err = insert_node(c, lnum, snod->offs, snod->len, hash,
7741e51764aSArtem Bityutskiy 					  &snod->key, snod->sqnum, deletion,
7751e51764aSArtem Bityutskiy 					  &used, 0, new_size);
7761e51764aSArtem Bityutskiy 			break;
7771e51764aSArtem Bityutskiy 		}
7781e51764aSArtem Bityutskiy 		case UBIFS_DATA_NODE:
7791e51764aSArtem Bityutskiy 		{
7801e51764aSArtem Bityutskiy 			struct ubifs_data_node *dn = snod->node;
7811e51764aSArtem Bityutskiy 			loff_t new_size = le32_to_cpu(dn->size) +
7821e51764aSArtem Bityutskiy 					  key_block(c, &snod->key) *
7831e51764aSArtem Bityutskiy 					  UBIFS_BLOCK_SIZE;
7841e51764aSArtem Bityutskiy 
78516a26b20SSascha Hauer 			err = insert_node(c, lnum, snod->offs, snod->len, hash,
7861e51764aSArtem Bityutskiy 					  &snod->key, snod->sqnum, deletion,
7871e51764aSArtem Bityutskiy 					  &used, 0, new_size);
7881e51764aSArtem Bityutskiy 			break;
7891e51764aSArtem Bityutskiy 		}
7901e51764aSArtem Bityutskiy 		case UBIFS_DENT_NODE:
7911e51764aSArtem Bityutskiy 		case UBIFS_XENT_NODE:
7921e51764aSArtem Bityutskiy 		{
7931e51764aSArtem Bityutskiy 			struct ubifs_dent_node *dent = snod->node;
7941e51764aSArtem Bityutskiy 
7951e51764aSArtem Bityutskiy 			err = ubifs_validate_entry(c, dent);
7961e51764aSArtem Bityutskiy 			if (err)
7971e51764aSArtem Bityutskiy 				goto out_dump;
7981e51764aSArtem Bityutskiy 
79916a26b20SSascha Hauer 			err = insert_dent(c, lnum, snod->offs, snod->len, hash,
8001e51764aSArtem Bityutskiy 					  &snod->key, dent->name,
8011e51764aSArtem Bityutskiy 					  le16_to_cpu(dent->nlen), snod->sqnum,
8021e51764aSArtem Bityutskiy 					  !le64_to_cpu(dent->inum), &used);
8031e51764aSArtem Bityutskiy 			break;
8041e51764aSArtem Bityutskiy 		}
8051e51764aSArtem Bityutskiy 		case UBIFS_TRUN_NODE:
8061e51764aSArtem Bityutskiy 		{
8071e51764aSArtem Bityutskiy 			struct ubifs_trun_node *trun = snod->node;
8081e51764aSArtem Bityutskiy 			loff_t old_size = le64_to_cpu(trun->old_size);
8091e51764aSArtem Bityutskiy 			loff_t new_size = le64_to_cpu(trun->new_size);
8101e51764aSArtem Bityutskiy 			union ubifs_key key;
8111e51764aSArtem Bityutskiy 
8121e51764aSArtem Bityutskiy 			/* Validate truncation node */
8131e51764aSArtem Bityutskiy 			if (old_size < 0 || old_size > c->max_inode_sz ||
8141e51764aSArtem Bityutskiy 			    new_size < 0 || new_size > c->max_inode_sz ||
8151e51764aSArtem Bityutskiy 			    old_size <= new_size) {
816235c362bSSheng Yong 				ubifs_err(c, "bad truncation node");
8171e51764aSArtem Bityutskiy 				goto out_dump;
8181e51764aSArtem Bityutskiy 			}
8191e51764aSArtem Bityutskiy 
8201e51764aSArtem Bityutskiy 			/*
8211e51764aSArtem Bityutskiy 			 * Create a fake truncation key just to use the same
8221e51764aSArtem Bityutskiy 			 * functions which expect nodes to have keys.
8231e51764aSArtem Bityutskiy 			 */
8241e51764aSArtem Bityutskiy 			trun_key_init(c, &key, le32_to_cpu(trun->inum));
82516a26b20SSascha Hauer 			err = insert_node(c, lnum, snod->offs, snod->len, hash,
8261e51764aSArtem Bityutskiy 					  &key, snod->sqnum, 1, &used,
8271e51764aSArtem Bityutskiy 					  old_size, new_size);
8281e51764aSArtem Bityutskiy 			break;
8291e51764aSArtem Bityutskiy 		}
8306a98bc46SSascha Hauer 		case UBIFS_AUTH_NODE:
8316a98bc46SSascha Hauer 			break;
8321e51764aSArtem Bityutskiy 		default:
833235c362bSSheng Yong 			ubifs_err(c, "unexpected node type %d in bud LEB %d:%d",
8341e51764aSArtem Bityutskiy 				  snod->type, lnum, snod->offs);
8351e51764aSArtem Bityutskiy 			err = -EINVAL;
8361e51764aSArtem Bityutskiy 			goto out_dump;
8371e51764aSArtem Bityutskiy 		}
8381e51764aSArtem Bityutskiy 		if (err)
8391e51764aSArtem Bityutskiy 			goto out;
840da8ef65fSSascha Hauer 
841da8ef65fSSascha Hauer 		n++;
842da8ef65fSSascha Hauer 		if (n == n_nodes)
843da8ef65fSSascha Hauer 			break;
8441e51764aSArtem Bityutskiy 	}
8451e51764aSArtem Bityutskiy 
8466eb61d58SRichard Weinberger 	ubifs_assert(c, ubifs_search_bud(c, lnum));
8476eb61d58SRichard Weinberger 	ubifs_assert(c, sleb->endpt - offs >= used);
8486eb61d58SRichard Weinberger 	ubifs_assert(c, sleb->endpt % c->min_io_size == 0);
8491e51764aSArtem Bityutskiy 
850e76a4526SArtem Bityutskiy 	b->dirty = sleb->endpt - offs - used;
851e76a4526SArtem Bityutskiy 	b->free = c->leb_size - sleb->endpt;
85279fda517SArtem Bityutskiy 	dbg_mnt("bud LEB %d replied: dirty %d, free %d",
85379fda517SArtem Bityutskiy 		lnum, b->dirty, b->free);
8541e51764aSArtem Bityutskiy 
8551e51764aSArtem Bityutskiy out:
8561e51764aSArtem Bityutskiy 	ubifs_scan_destroy(sleb);
8571e51764aSArtem Bityutskiy 	return err;
8581e51764aSArtem Bityutskiy 
8591e51764aSArtem Bityutskiy out_dump:
860235c362bSSheng Yong 	ubifs_err(c, "bad node is at LEB %d:%d", lnum, snod->offs);
861edf6be24SArtem Bityutskiy 	ubifs_dump_node(c, snod->node);
8621e51764aSArtem Bityutskiy 	ubifs_scan_destroy(sleb);
8631e51764aSArtem Bityutskiy 	return -EINVAL;
8641e51764aSArtem Bityutskiy }
8651e51764aSArtem Bityutskiy 
8661e51764aSArtem Bityutskiy /**
8671e51764aSArtem Bityutskiy  * replay_buds - replay all buds.
8681e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
8691e51764aSArtem Bityutskiy  *
8701e51764aSArtem Bityutskiy  * This function returns zero in case of success and a negative error code in
8711e51764aSArtem Bityutskiy  * case of failure.
8721e51764aSArtem Bityutskiy  */
8731e51764aSArtem Bityutskiy static int replay_buds(struct ubifs_info *c)
8741e51764aSArtem Bityutskiy {
8751e51764aSArtem Bityutskiy 	struct bud_entry *b;
876074bcb9bSArtem Bityutskiy 	int err;
8777703f09dSArtem Bityutskiy 	unsigned long long prev_sqnum = 0;
8781e51764aSArtem Bityutskiy 
8791e51764aSArtem Bityutskiy 	list_for_each_entry(b, &c->replay_buds, list) {
880e76a4526SArtem Bityutskiy 		err = replay_bud(c, b);
8811e51764aSArtem Bityutskiy 		if (err)
8821e51764aSArtem Bityutskiy 			return err;
8837703f09dSArtem Bityutskiy 
8846eb61d58SRichard Weinberger 		ubifs_assert(c, b->sqnum > prev_sqnum);
8857703f09dSArtem Bityutskiy 		prev_sqnum = b->sqnum;
8861e51764aSArtem Bityutskiy 	}
8871e51764aSArtem Bityutskiy 
8881e51764aSArtem Bityutskiy 	return 0;
8891e51764aSArtem Bityutskiy }
8901e51764aSArtem Bityutskiy 
8911e51764aSArtem Bityutskiy /**
8921e51764aSArtem Bityutskiy  * destroy_bud_list - destroy the list of buds to replay.
8931e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
8941e51764aSArtem Bityutskiy  */
8951e51764aSArtem Bityutskiy static void destroy_bud_list(struct ubifs_info *c)
8961e51764aSArtem Bityutskiy {
8971e51764aSArtem Bityutskiy 	struct bud_entry *b;
8981e51764aSArtem Bityutskiy 
8991e51764aSArtem Bityutskiy 	while (!list_empty(&c->replay_buds)) {
9001e51764aSArtem Bityutskiy 		b = list_entry(c->replay_buds.next, struct bud_entry, list);
9011e51764aSArtem Bityutskiy 		list_del(&b->list);
9021e51764aSArtem Bityutskiy 		kfree(b);
9031e51764aSArtem Bityutskiy 	}
9041e51764aSArtem Bityutskiy }
9051e51764aSArtem Bityutskiy 
9061e51764aSArtem Bityutskiy /**
9071e51764aSArtem Bityutskiy  * add_replay_bud - add a bud to the list of buds to replay.
9081e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
9091e51764aSArtem Bityutskiy  * @lnum: bud logical eraseblock number to replay
9101e51764aSArtem Bityutskiy  * @offs: bud start offset
9111e51764aSArtem Bityutskiy  * @jhead: journal head to which this bud belongs
9121e51764aSArtem Bityutskiy  * @sqnum: reference node sequence number
9131e51764aSArtem Bityutskiy  *
9141e51764aSArtem Bityutskiy  * This function returns zero in case of success and a negative error code in
9151e51764aSArtem Bityutskiy  * case of failure.
9161e51764aSArtem Bityutskiy  */
9171e51764aSArtem Bityutskiy static int add_replay_bud(struct ubifs_info *c, int lnum, int offs, int jhead,
9181e51764aSArtem Bityutskiy 			  unsigned long long sqnum)
9191e51764aSArtem Bityutskiy {
9201e51764aSArtem Bityutskiy 	struct ubifs_bud *bud;
9211e51764aSArtem Bityutskiy 	struct bud_entry *b;
922da8ef65fSSascha Hauer 	int err;
9231e51764aSArtem Bityutskiy 
9241e51764aSArtem Bityutskiy 	dbg_mnt("add replay bud LEB %d:%d, head %d", lnum, offs, jhead);
9251e51764aSArtem Bityutskiy 
9261e51764aSArtem Bityutskiy 	bud = kmalloc(sizeof(struct ubifs_bud), GFP_KERNEL);
9271e51764aSArtem Bityutskiy 	if (!bud)
9281e51764aSArtem Bityutskiy 		return -ENOMEM;
9291e51764aSArtem Bityutskiy 
9301e51764aSArtem Bityutskiy 	b = kmalloc(sizeof(struct bud_entry), GFP_KERNEL);
9311e51764aSArtem Bityutskiy 	if (!b) {
932da8ef65fSSascha Hauer 		err = -ENOMEM;
933da8ef65fSSascha Hauer 		goto out;
9341e51764aSArtem Bityutskiy 	}
9351e51764aSArtem Bityutskiy 
9361e51764aSArtem Bityutskiy 	bud->lnum = lnum;
9371e51764aSArtem Bityutskiy 	bud->start = offs;
9381e51764aSArtem Bityutskiy 	bud->jhead = jhead;
939da8ef65fSSascha Hauer 	bud->log_hash = ubifs_hash_get_desc(c);
940da8ef65fSSascha Hauer 	if (IS_ERR(bud->log_hash)) {
941da8ef65fSSascha Hauer 		err = PTR_ERR(bud->log_hash);
942da8ef65fSSascha Hauer 		goto out;
943da8ef65fSSascha Hauer 	}
944da8ef65fSSascha Hauer 
945da8ef65fSSascha Hauer 	ubifs_shash_copy_state(c, c->log_hash, bud->log_hash);
946da8ef65fSSascha Hauer 
9471e51764aSArtem Bityutskiy 	ubifs_add_bud(c, bud);
9481e51764aSArtem Bityutskiy 
9491e51764aSArtem Bityutskiy 	b->bud = bud;
9501e51764aSArtem Bityutskiy 	b->sqnum = sqnum;
9511e51764aSArtem Bityutskiy 	list_add_tail(&b->list, &c->replay_buds);
9521e51764aSArtem Bityutskiy 
9531e51764aSArtem Bityutskiy 	return 0;
954da8ef65fSSascha Hauer out:
955da8ef65fSSascha Hauer 	kfree(bud);
956da8ef65fSSascha Hauer 	kfree(b);
957da8ef65fSSascha Hauer 
958da8ef65fSSascha Hauer 	return err;
9591e51764aSArtem Bityutskiy }
9601e51764aSArtem Bityutskiy 
9611e51764aSArtem Bityutskiy /**
9621e51764aSArtem Bityutskiy  * validate_ref - validate a reference node.
9631e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
9641e51764aSArtem Bityutskiy  * @ref: the reference node to validate
9651e51764aSArtem Bityutskiy  * @ref_lnum: LEB number of the reference node
9661e51764aSArtem Bityutskiy  * @ref_offs: reference node offset
9671e51764aSArtem Bityutskiy  *
9681e51764aSArtem Bityutskiy  * This function returns %1 if a bud reference already exists for the LEB. %0 is
9691e51764aSArtem Bityutskiy  * returned if the reference node is new, otherwise %-EINVAL is returned if
9701e51764aSArtem Bityutskiy  * validation failed.
9711e51764aSArtem Bityutskiy  */
9721e51764aSArtem Bityutskiy static int validate_ref(struct ubifs_info *c, const struct ubifs_ref_node *ref)
9731e51764aSArtem Bityutskiy {
9741e51764aSArtem Bityutskiy 	struct ubifs_bud *bud;
9751e51764aSArtem Bityutskiy 	int lnum = le32_to_cpu(ref->lnum);
9761e51764aSArtem Bityutskiy 	unsigned int offs = le32_to_cpu(ref->offs);
9771e51764aSArtem Bityutskiy 	unsigned int jhead = le32_to_cpu(ref->jhead);
9781e51764aSArtem Bityutskiy 
9791e51764aSArtem Bityutskiy 	/*
9801e51764aSArtem Bityutskiy 	 * ref->offs may point to the end of LEB when the journal head points
9811e51764aSArtem Bityutskiy 	 * to the end of LEB and we write reference node for it during commit.
9821e51764aSArtem Bityutskiy 	 * So this is why we require 'offs > c->leb_size'.
9831e51764aSArtem Bityutskiy 	 */
9841e51764aSArtem Bityutskiy 	if (jhead >= c->jhead_cnt || lnum >= c->leb_cnt ||
9851e51764aSArtem Bityutskiy 	    lnum < c->main_first || offs > c->leb_size ||
9861e51764aSArtem Bityutskiy 	    offs & (c->min_io_size - 1))
9871e51764aSArtem Bityutskiy 		return -EINVAL;
9881e51764aSArtem Bityutskiy 
9891e51764aSArtem Bityutskiy 	/* Make sure we have not already looked at this bud */
9901e51764aSArtem Bityutskiy 	bud = ubifs_search_bud(c, lnum);
9911e51764aSArtem Bityutskiy 	if (bud) {
9921e51764aSArtem Bityutskiy 		if (bud->jhead == jhead && bud->start <= offs)
9931e51764aSArtem Bityutskiy 			return 1;
994235c362bSSheng Yong 		ubifs_err(c, "bud at LEB %d:%d was already referred", lnum, offs);
9951e51764aSArtem Bityutskiy 		return -EINVAL;
9961e51764aSArtem Bityutskiy 	}
9971e51764aSArtem Bityutskiy 
9981e51764aSArtem Bityutskiy 	return 0;
9991e51764aSArtem Bityutskiy }
10001e51764aSArtem Bityutskiy 
10011e51764aSArtem Bityutskiy /**
10021e51764aSArtem Bityutskiy  * replay_log_leb - replay a log logical eraseblock.
10031e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
10041e51764aSArtem Bityutskiy  * @lnum: log logical eraseblock to replay
10051e51764aSArtem Bityutskiy  * @offs: offset to start replaying from
10061e51764aSArtem Bityutskiy  * @sbuf: scan buffer
10071e51764aSArtem Bityutskiy  *
10081e51764aSArtem Bityutskiy  * This function replays a log LEB and returns zero in case of success, %1 if
10091e51764aSArtem Bityutskiy  * this is the last LEB in the log, and a negative error code in case of
10101e51764aSArtem Bityutskiy  * failure.
10111e51764aSArtem Bityutskiy  */
10121e51764aSArtem Bityutskiy static int replay_log_leb(struct ubifs_info *c, int lnum, int offs, void *sbuf)
10131e51764aSArtem Bityutskiy {
10141e51764aSArtem Bityutskiy 	int err;
10151e51764aSArtem Bityutskiy 	struct ubifs_scan_leb *sleb;
10161e51764aSArtem Bityutskiy 	struct ubifs_scan_node *snod;
10171e51764aSArtem Bityutskiy 	const struct ubifs_cs_node *node;
10181e51764aSArtem Bityutskiy 
10191e51764aSArtem Bityutskiy 	dbg_mnt("replay log LEB %d:%d", lnum, offs);
1020348709baSArtem Bityutskiy 	sleb = ubifs_scan(c, lnum, offs, sbuf, c->need_recovery);
10211e51764aSArtem Bityutskiy 	if (IS_ERR(sleb)) {
1022ed43f2f0SArtem Bityutskiy 		if (PTR_ERR(sleb) != -EUCLEAN || !c->need_recovery)
1023ed43f2f0SArtem Bityutskiy 			return PTR_ERR(sleb);
10247d08ae3cSArtem Bityutskiy 		/*
10257d08ae3cSArtem Bityutskiy 		 * Note, the below function will recover this log LEB only if
10267d08ae3cSArtem Bityutskiy 		 * it is the last, because unclean reboots can possibly corrupt
10277d08ae3cSArtem Bityutskiy 		 * only the tail of the log.
10287d08ae3cSArtem Bityutskiy 		 */
10291e51764aSArtem Bityutskiy 		sleb = ubifs_recover_log_leb(c, lnum, offs, sbuf);
10301e51764aSArtem Bityutskiy 		if (IS_ERR(sleb))
10311e51764aSArtem Bityutskiy 			return PTR_ERR(sleb);
10321e51764aSArtem Bityutskiy 	}
10331e51764aSArtem Bityutskiy 
10341e51764aSArtem Bityutskiy 	if (sleb->nodes_cnt == 0) {
10351e51764aSArtem Bityutskiy 		err = 1;
10361e51764aSArtem Bityutskiy 		goto out;
10371e51764aSArtem Bityutskiy 	}
10381e51764aSArtem Bityutskiy 
10391e51764aSArtem Bityutskiy 	node = sleb->buf;
10401e51764aSArtem Bityutskiy 	snod = list_entry(sleb->nodes.next, struct ubifs_scan_node, list);
10411e51764aSArtem Bityutskiy 	if (c->cs_sqnum == 0) {
10421e51764aSArtem Bityutskiy 		/*
10431e51764aSArtem Bityutskiy 		 * This is the first log LEB we are looking at, make sure that
10441e51764aSArtem Bityutskiy 		 * the first node is a commit start node. Also record its
10451e51764aSArtem Bityutskiy 		 * sequence number so that UBIFS can determine where the log
10461e51764aSArtem Bityutskiy 		 * ends, because all nodes which were have higher sequence
10471e51764aSArtem Bityutskiy 		 * numbers.
10481e51764aSArtem Bityutskiy 		 */
10491e51764aSArtem Bityutskiy 		if (snod->type != UBIFS_CS_NODE) {
1050235c362bSSheng Yong 			ubifs_err(c, "first log node at LEB %d:%d is not CS node",
10511e51764aSArtem Bityutskiy 				  lnum, offs);
10521e51764aSArtem Bityutskiy 			goto out_dump;
10531e51764aSArtem Bityutskiy 		}
10541e51764aSArtem Bityutskiy 		if (le64_to_cpu(node->cmt_no) != c->cmt_no) {
1055235c362bSSheng Yong 			ubifs_err(c, "first CS node at LEB %d:%d has wrong commit number %llu expected %llu",
10561e51764aSArtem Bityutskiy 				  lnum, offs,
10571e51764aSArtem Bityutskiy 				  (unsigned long long)le64_to_cpu(node->cmt_no),
10581e51764aSArtem Bityutskiy 				  c->cmt_no);
10591e51764aSArtem Bityutskiy 			goto out_dump;
10601e51764aSArtem Bityutskiy 		}
10611e51764aSArtem Bityutskiy 
10621e51764aSArtem Bityutskiy 		c->cs_sqnum = le64_to_cpu(node->ch.sqnum);
10631e51764aSArtem Bityutskiy 		dbg_mnt("commit start sqnum %llu", c->cs_sqnum);
1064da8ef65fSSascha Hauer 
1065da8ef65fSSascha Hauer 		err = ubifs_shash_init(c, c->log_hash);
1066da8ef65fSSascha Hauer 		if (err)
1067da8ef65fSSascha Hauer 			goto out;
1068da8ef65fSSascha Hauer 
1069da8ef65fSSascha Hauer 		err = ubifs_shash_update(c, c->log_hash, node, UBIFS_CS_NODE_SZ);
1070da8ef65fSSascha Hauer 		if (err < 0)
1071da8ef65fSSascha Hauer 			goto out;
10721e51764aSArtem Bityutskiy 	}
10731e51764aSArtem Bityutskiy 
10741e51764aSArtem Bityutskiy 	if (snod->sqnum < c->cs_sqnum) {
10751e51764aSArtem Bityutskiy 		/*
10761e51764aSArtem Bityutskiy 		 * This means that we reached end of log and now
10771e51764aSArtem Bityutskiy 		 * look to the older log data, which was already
10781e51764aSArtem Bityutskiy 		 * committed but the eraseblock was not erased (UBIFS
10796edbfafdSArtem Bityutskiy 		 * only un-maps it). So this basically means we have to
10801e51764aSArtem Bityutskiy 		 * exit with "end of log" code.
10811e51764aSArtem Bityutskiy 		 */
10821e51764aSArtem Bityutskiy 		err = 1;
10831e51764aSArtem Bityutskiy 		goto out;
10841e51764aSArtem Bityutskiy 	}
10851e51764aSArtem Bityutskiy 
10861e51764aSArtem Bityutskiy 	/* Make sure the first node sits at offset zero of the LEB */
10871e51764aSArtem Bityutskiy 	if (snod->offs != 0) {
1088235c362bSSheng Yong 		ubifs_err(c, "first node is not at zero offset");
10891e51764aSArtem Bityutskiy 		goto out_dump;
10901e51764aSArtem Bityutskiy 	}
10911e51764aSArtem Bityutskiy 
10921e51764aSArtem Bityutskiy 	list_for_each_entry(snod, &sleb->nodes, list) {
10931e51764aSArtem Bityutskiy 		cond_resched();
10941e51764aSArtem Bityutskiy 
10951e51764aSArtem Bityutskiy 		if (snod->sqnum >= SQNUM_WATERMARK) {
1096235c362bSSheng Yong 			ubifs_err(c, "file system's life ended");
10971e51764aSArtem Bityutskiy 			goto out_dump;
10981e51764aSArtem Bityutskiy 		}
10991e51764aSArtem Bityutskiy 
11001e51764aSArtem Bityutskiy 		if (snod->sqnum < c->cs_sqnum) {
1101235c362bSSheng Yong 			ubifs_err(c, "bad sqnum %llu, commit sqnum %llu",
11021e51764aSArtem Bityutskiy 				  snod->sqnum, c->cs_sqnum);
11031e51764aSArtem Bityutskiy 			goto out_dump;
11041e51764aSArtem Bityutskiy 		}
11051e51764aSArtem Bityutskiy 
11061e51764aSArtem Bityutskiy 		if (snod->sqnum > c->max_sqnum)
11071e51764aSArtem Bityutskiy 			c->max_sqnum = snod->sqnum;
11081e51764aSArtem Bityutskiy 
11091e51764aSArtem Bityutskiy 		switch (snod->type) {
11101e51764aSArtem Bityutskiy 		case UBIFS_REF_NODE: {
11111e51764aSArtem Bityutskiy 			const struct ubifs_ref_node *ref = snod->node;
11121e51764aSArtem Bityutskiy 
11131e51764aSArtem Bityutskiy 			err = validate_ref(c, ref);
11141e51764aSArtem Bityutskiy 			if (err == 1)
11151e51764aSArtem Bityutskiy 				break; /* Already have this bud */
11161e51764aSArtem Bityutskiy 			if (err)
11171e51764aSArtem Bityutskiy 				goto out_dump;
11181e51764aSArtem Bityutskiy 
1119da8ef65fSSascha Hauer 			err = ubifs_shash_update(c, c->log_hash, ref,
1120da8ef65fSSascha Hauer 						 UBIFS_REF_NODE_SZ);
1121da8ef65fSSascha Hauer 			if (err)
1122da8ef65fSSascha Hauer 				goto out;
1123da8ef65fSSascha Hauer 
11241e51764aSArtem Bityutskiy 			err = add_replay_bud(c, le32_to_cpu(ref->lnum),
11251e51764aSArtem Bityutskiy 					     le32_to_cpu(ref->offs),
11261e51764aSArtem Bityutskiy 					     le32_to_cpu(ref->jhead),
11271e51764aSArtem Bityutskiy 					     snod->sqnum);
11281e51764aSArtem Bityutskiy 			if (err)
11291e51764aSArtem Bityutskiy 				goto out;
11301e51764aSArtem Bityutskiy 
11311e51764aSArtem Bityutskiy 			break;
11321e51764aSArtem Bityutskiy 		}
11331e51764aSArtem Bityutskiy 		case UBIFS_CS_NODE:
11341e51764aSArtem Bityutskiy 			/* Make sure it sits at the beginning of LEB */
11351e51764aSArtem Bityutskiy 			if (snod->offs != 0) {
1136235c362bSSheng Yong 				ubifs_err(c, "unexpected node in log");
11371e51764aSArtem Bityutskiy 				goto out_dump;
11381e51764aSArtem Bityutskiy 			}
11391e51764aSArtem Bityutskiy 			break;
11401e51764aSArtem Bityutskiy 		default:
1141235c362bSSheng Yong 			ubifs_err(c, "unexpected node in log");
11421e51764aSArtem Bityutskiy 			goto out_dump;
11431e51764aSArtem Bityutskiy 		}
11441e51764aSArtem Bityutskiy 	}
11451e51764aSArtem Bityutskiy 
11461e51764aSArtem Bityutskiy 	if (sleb->endpt || c->lhead_offs >= c->leb_size) {
11471e51764aSArtem Bityutskiy 		c->lhead_lnum = lnum;
11481e51764aSArtem Bityutskiy 		c->lhead_offs = sleb->endpt;
11491e51764aSArtem Bityutskiy 	}
11501e51764aSArtem Bityutskiy 
11511e51764aSArtem Bityutskiy 	err = !sleb->endpt;
11521e51764aSArtem Bityutskiy out:
11531e51764aSArtem Bityutskiy 	ubifs_scan_destroy(sleb);
11541e51764aSArtem Bityutskiy 	return err;
11551e51764aSArtem Bityutskiy 
11561e51764aSArtem Bityutskiy out_dump:
1157235c362bSSheng Yong 	ubifs_err(c, "log error detected while replaying the log at LEB %d:%d",
11581e51764aSArtem Bityutskiy 		  lnum, offs + snod->offs);
1159edf6be24SArtem Bityutskiy 	ubifs_dump_node(c, snod->node);
11601e51764aSArtem Bityutskiy 	ubifs_scan_destroy(sleb);
11611e51764aSArtem Bityutskiy 	return -EINVAL;
11621e51764aSArtem Bityutskiy }
11631e51764aSArtem Bityutskiy 
11641e51764aSArtem Bityutskiy /**
11651e51764aSArtem Bityutskiy  * take_ihead - update the status of the index head in lprops to 'taken'.
11661e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
11671e51764aSArtem Bityutskiy  *
11681e51764aSArtem Bityutskiy  * This function returns the amount of free space in the index head LEB or a
11691e51764aSArtem Bityutskiy  * negative error code.
11701e51764aSArtem Bityutskiy  */
11711e51764aSArtem Bityutskiy static int take_ihead(struct ubifs_info *c)
11721e51764aSArtem Bityutskiy {
11731e51764aSArtem Bityutskiy 	const struct ubifs_lprops *lp;
11741e51764aSArtem Bityutskiy 	int err, free;
11751e51764aSArtem Bityutskiy 
11761e51764aSArtem Bityutskiy 	ubifs_get_lprops(c);
11771e51764aSArtem Bityutskiy 
11781e51764aSArtem Bityutskiy 	lp = ubifs_lpt_lookup_dirty(c, c->ihead_lnum);
11791e51764aSArtem Bityutskiy 	if (IS_ERR(lp)) {
11801e51764aSArtem Bityutskiy 		err = PTR_ERR(lp);
11811e51764aSArtem Bityutskiy 		goto out;
11821e51764aSArtem Bityutskiy 	}
11831e51764aSArtem Bityutskiy 
11841e51764aSArtem Bityutskiy 	free = lp->free;
11851e51764aSArtem Bityutskiy 
11861e51764aSArtem Bityutskiy 	lp = ubifs_change_lp(c, lp, LPROPS_NC, LPROPS_NC,
11871e51764aSArtem Bityutskiy 			     lp->flags | LPROPS_TAKEN, 0);
11881e51764aSArtem Bityutskiy 	if (IS_ERR(lp)) {
11891e51764aSArtem Bityutskiy 		err = PTR_ERR(lp);
11901e51764aSArtem Bityutskiy 		goto out;
11911e51764aSArtem Bityutskiy 	}
11921e51764aSArtem Bityutskiy 
11931e51764aSArtem Bityutskiy 	err = free;
11941e51764aSArtem Bityutskiy out:
11951e51764aSArtem Bityutskiy 	ubifs_release_lprops(c);
11961e51764aSArtem Bityutskiy 	return err;
11971e51764aSArtem Bityutskiy }
11981e51764aSArtem Bityutskiy 
11991e51764aSArtem Bityutskiy /**
12001e51764aSArtem Bityutskiy  * ubifs_replay_journal - replay journal.
12011e51764aSArtem Bityutskiy  * @c: UBIFS file-system description object
12021e51764aSArtem Bityutskiy  *
12031e51764aSArtem Bityutskiy  * This function scans the journal, replays and cleans it up. It makes sure all
12041e51764aSArtem Bityutskiy  * memory data structures related to uncommitted journal are built (dirty TNC
12051e51764aSArtem Bityutskiy  * tree, tree of buds, modified lprops, etc).
12061e51764aSArtem Bityutskiy  */
12071e51764aSArtem Bityutskiy int ubifs_replay_journal(struct ubifs_info *c)
12081e51764aSArtem Bityutskiy {
1209d51f17eaSArtem Bityutskiy 	int err, lnum, free;
12101e51764aSArtem Bityutskiy 
12111e51764aSArtem Bityutskiy 	BUILD_BUG_ON(UBIFS_TRUN_KEY > 5);
12121e51764aSArtem Bityutskiy 
12131e51764aSArtem Bityutskiy 	/* Update the status of the index head in lprops to 'taken' */
12141e51764aSArtem Bityutskiy 	free = take_ihead(c);
12151e51764aSArtem Bityutskiy 	if (free < 0)
12161e51764aSArtem Bityutskiy 		return free; /* Error code */
12171e51764aSArtem Bityutskiy 
12181e51764aSArtem Bityutskiy 	if (c->ihead_offs != c->leb_size - free) {
1219235c362bSSheng Yong 		ubifs_err(c, "bad index head LEB %d:%d", c->ihead_lnum,
12201e51764aSArtem Bityutskiy 			  c->ihead_offs);
12211e51764aSArtem Bityutskiy 		return -EINVAL;
12221e51764aSArtem Bityutskiy 	}
12231e51764aSArtem Bityutskiy 
12241e51764aSArtem Bityutskiy 	dbg_mnt("start replaying the journal");
12251e51764aSArtem Bityutskiy 	c->replaying = 1;
12261e51764aSArtem Bityutskiy 	lnum = c->ltail_lnum = c->lhead_lnum;
12271e51764aSArtem Bityutskiy 
1228d51f17eaSArtem Bityutskiy 	do {
1229d51f17eaSArtem Bityutskiy 		err = replay_log_leb(c, lnum, 0, c->sbuf);
123088cff0f0Shujianyang 		if (err == 1) {
123188cff0f0Shujianyang 			if (lnum != c->lhead_lnum)
12321e51764aSArtem Bityutskiy 				/* We hit the end of the log */
12331e51764aSArtem Bityutskiy 				break;
123488cff0f0Shujianyang 
123588cff0f0Shujianyang 			/*
123688cff0f0Shujianyang 			 * The head of the log must always start with the
123788cff0f0Shujianyang 			 * "commit start" node on a properly formatted UBIFS.
123888cff0f0Shujianyang 			 * But we found no nodes at all, which means that
1239c7e593b3SSascha Hauer 			 * something went wrong and we cannot proceed mounting
124088cff0f0Shujianyang 			 * the file-system.
124188cff0f0Shujianyang 			 */
1242235c362bSSheng Yong 			ubifs_err(c, "no UBIFS nodes found at the log head LEB %d:%d, possibly corrupted",
124388cff0f0Shujianyang 				  lnum, 0);
124488cff0f0Shujianyang 			err = -EINVAL;
124588cff0f0Shujianyang 		}
12461e51764aSArtem Bityutskiy 		if (err)
12471e51764aSArtem Bityutskiy 			goto out;
1248d51f17eaSArtem Bityutskiy 		lnum = ubifs_next_log_lnum(c, lnum);
1249c212f402SArtem Bityutskiy 	} while (lnum != c->ltail_lnum);
12501e51764aSArtem Bityutskiy 
12511e51764aSArtem Bityutskiy 	err = replay_buds(c);
12521e51764aSArtem Bityutskiy 	if (err)
12531e51764aSArtem Bityutskiy 		goto out;
12541e51764aSArtem Bityutskiy 
1255debf12d5SArtem Bityutskiy 	err = apply_replay_list(c);
12561e51764aSArtem Bityutskiy 	if (err)
12571e51764aSArtem Bityutskiy 		goto out;
12581e51764aSArtem Bityutskiy 
1259074bcb9bSArtem Bityutskiy 	err = set_buds_lprops(c);
1260074bcb9bSArtem Bityutskiy 	if (err)
1261074bcb9bSArtem Bityutskiy 		goto out;
1262074bcb9bSArtem Bityutskiy 
12636edbfafdSArtem Bityutskiy 	/*
1264b137545cSArtem Bityutskiy 	 * UBIFS budgeting calculations use @c->bi.uncommitted_idx variable
1265b137545cSArtem Bityutskiy 	 * to roughly estimate index growth. Things like @c->bi.min_idx_lebs
12666edbfafdSArtem Bityutskiy 	 * depend on it. This means we have to initialize it to make sure
12676edbfafdSArtem Bityutskiy 	 * budgeting works properly.
12686edbfafdSArtem Bityutskiy 	 */
1269b137545cSArtem Bityutskiy 	c->bi.uncommitted_idx = atomic_long_read(&c->dirty_zn_cnt);
1270b137545cSArtem Bityutskiy 	c->bi.uncommitted_idx *= c->max_idx_node_sz;
12716edbfafdSArtem Bityutskiy 
12726eb61d58SRichard Weinberger 	ubifs_assert(c, c->bud_bytes <= c->max_bud_bytes || c->need_recovery);
127379fda517SArtem Bityutskiy 	dbg_mnt("finished, log head LEB %d:%d, max_sqnum %llu, highest_inum %lu",
127479fda517SArtem Bityutskiy 		c->lhead_lnum, c->lhead_offs, c->max_sqnum,
1275e84461adSArtem Bityutskiy 		(unsigned long)c->highest_inum);
12761e51764aSArtem Bityutskiy out:
1277debf12d5SArtem Bityutskiy 	destroy_replay_list(c);
12781e51764aSArtem Bityutskiy 	destroy_bud_list(c);
12791e51764aSArtem Bityutskiy 	c->replaying = 0;
12801e51764aSArtem Bityutskiy 	return err;
12811e51764aSArtem Bityutskiy }
1282