• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * This file is part of UBIFS.
4  *
5  * Copyright (C) 2006-2008 Nokia Corporation.
6  *
7  * Authors: Adrian Hunter
8  *          Artem Bityutskiy (Битюцкий Артём)
9  */
10 
11 /*
12  * This file contains journal replay code. It runs when the file-system is being
13  * mounted and requires no locking.
14  *
15  * The larger is the journal, the longer it takes to scan it, so the longer it
16  * takes to mount UBIFS. This is why the journal has limited size which may be
17  * changed depending on the system requirements. But a larger journal gives
18  * faster I/O speed because it writes the index less frequently. So this is a
19  * trade-off. Also, the journal is indexed by the in-memory index (TNC), so the
20  * larger is the journal, the more memory its index may consume.
21  */
22 
23 #include "ubifs.h"
24 #include <linux/list_sort.h>
25 #include <crypto/hash.h>
26 #include <crypto/algapi.h>
27 
28 /**
29  * struct replay_entry - replay list entry.
30  * @lnum: logical eraseblock number of the node
31  * @offs: node offset
32  * @len: node length
33  * @deletion: non-zero if this entry corresponds to a node deletion
34  * @sqnum: node sequence number
35  * @list: links the replay list
36  * @key: node key
37  * @nm: directory entry name
38  * @old_size: truncation old size
39  * @new_size: truncation new size
40  *
41  * The replay process first scans all buds and builds the replay list, then
42  * sorts the replay list in nodes sequence number order, and then inserts all
43  * the replay entries to the TNC.
44  */
45 struct replay_entry {
46 	int lnum;
47 	int offs;
48 	int len;
49 	u8 hash[UBIFS_HASH_ARR_SZ];
50 	unsigned int deletion:1;
51 	unsigned long long sqnum;
52 	struct list_head list;
53 	union ubifs_key key;
54 	union {
55 		struct fscrypt_name nm;
56 		struct {
57 			loff_t old_size;
58 			loff_t new_size;
59 		};
60 	};
61 };
62 
63 /**
64  * struct bud_entry - entry in the list of buds to replay.
65  * @list: next bud in the list
66  * @bud: bud description object
67  * @sqnum: reference node sequence number
68  * @free: free bytes in the bud
69  * @dirty: dirty bytes in the bud
70  */
71 struct bud_entry {
72 	struct list_head list;
73 	struct ubifs_bud *bud;
74 	unsigned long long sqnum;
75 	int free;
76 	int dirty;
77 };
78 
79 /**
80  * set_bud_lprops - set free and dirty space used by a bud.
81  * @c: UBIFS file-system description object
82  * @b: bud entry which describes the bud
83  *
84  * This function makes sure the LEB properties of bud @b are set correctly
85  * after the replay. Returns zero in case of success and a negative error code
86  * in case of failure.
87  */
set_bud_lprops(struct ubifs_info * c,struct bud_entry * b)88 static int set_bud_lprops(struct ubifs_info *c, struct bud_entry *b)
89 {
90 	const struct ubifs_lprops *lp;
91 	int err = 0, dirty;
92 
93 	ubifs_get_lprops(c);
94 
95 	lp = ubifs_lpt_lookup_dirty(c, b->bud->lnum);
96 	if (IS_ERR(lp)) {
97 		err = PTR_ERR(lp);
98 		goto out;
99 	}
100 
101 	dirty = lp->dirty;
102 	if (b->bud->start == 0 && (lp->free != c->leb_size || lp->dirty != 0)) {
103 		/*
104 		 * The LEB was added to the journal with a starting offset of
105 		 * zero which means the LEB must have been empty. The LEB
106 		 * property values should be @lp->free == @c->leb_size and
107 		 * @lp->dirty == 0, but that is not the case. The reason is that
108 		 * the LEB had been garbage collected before it became the bud,
109 		 * and there was not commit inbetween. The garbage collector
110 		 * resets the free and dirty space without recording it
111 		 * anywhere except lprops, so if there was no commit then
112 		 * lprops does not have that information.
113 		 *
114 		 * We do not need to adjust free space because the scan has told
115 		 * us the exact value which is recorded in the replay entry as
116 		 * @b->free.
117 		 *
118 		 * However we do need to subtract from the dirty space the
119 		 * amount of space that the garbage collector reclaimed, which
120 		 * is the whole LEB minus the amount of space that was free.
121 		 */
122 		dbg_mnt("bud LEB %d was GC'd (%d free, %d dirty)", b->bud->lnum,
123 			lp->free, lp->dirty);
124 		dbg_gc("bud LEB %d was GC'd (%d free, %d dirty)", b->bud->lnum,
125 			lp->free, lp->dirty);
126 		dirty -= c->leb_size - lp->free;
127 		/*
128 		 * If the replay order was perfect the dirty space would now be
129 		 * zero. The order is not perfect because the journal heads
130 		 * race with each other. This is not a problem but is does mean
131 		 * that the dirty space may temporarily exceed c->leb_size
132 		 * during the replay.
133 		 */
134 		if (dirty != 0)
135 			dbg_mnt("LEB %d lp: %d free %d dirty replay: %d free %d dirty",
136 				b->bud->lnum, lp->free, lp->dirty, b->free,
137 				b->dirty);
138 	}
139 	lp = ubifs_change_lp(c, lp, b->free, dirty + b->dirty,
140 			     lp->flags | LPROPS_TAKEN, 0);
141 	if (IS_ERR(lp)) {
142 		err = PTR_ERR(lp);
143 		goto out;
144 	}
145 
146 	/* Make sure the journal head points to the latest bud */
147 	err = ubifs_wbuf_seek_nolock(&c->jheads[b->bud->jhead].wbuf,
148 				     b->bud->lnum, c->leb_size - b->free);
149 
150 out:
151 	ubifs_release_lprops(c);
152 	return err;
153 }
154 
155 /**
156  * set_buds_lprops - set free and dirty space for all replayed buds.
157  * @c: UBIFS file-system description object
158  *
159  * This function sets LEB properties for all replayed buds. Returns zero in
160  * case of success and a negative error code in case of failure.
161  */
set_buds_lprops(struct ubifs_info * c)162 static int set_buds_lprops(struct ubifs_info *c)
163 {
164 	struct bud_entry *b;
165 	int err;
166 
167 	list_for_each_entry(b, &c->replay_buds, list) {
168 		err = set_bud_lprops(c, b);
169 		if (err)
170 			return err;
171 	}
172 
173 	return 0;
174 }
175 
176 /**
177  * trun_remove_range - apply a replay entry for a truncation to the TNC.
178  * @c: UBIFS file-system description object
179  * @r: replay entry of truncation
180  */
trun_remove_range(struct ubifs_info * c,struct replay_entry * r)181 static int trun_remove_range(struct ubifs_info *c, struct replay_entry *r)
182 {
183 	unsigned min_blk, max_blk;
184 	union ubifs_key min_key, max_key;
185 	ino_t ino;
186 
187 	min_blk = r->new_size / UBIFS_BLOCK_SIZE;
188 	if (r->new_size & (UBIFS_BLOCK_SIZE - 1))
189 		min_blk += 1;
190 
191 	max_blk = r->old_size / UBIFS_BLOCK_SIZE;
192 	if ((r->old_size & (UBIFS_BLOCK_SIZE - 1)) == 0)
193 		max_blk -= 1;
194 
195 	ino = key_inum(c, &r->key);
196 
197 	data_key_init(c, &min_key, ino, min_blk);
198 	data_key_init(c, &max_key, ino, max_blk);
199 
200 	return ubifs_tnc_remove_range(c, &min_key, &max_key);
201 }
202 
203 /**
204  * inode_still_linked - check whether inode in question will be re-linked.
205  * @c: UBIFS file-system description object
206  * @rino: replay entry to test
207  *
208  * O_TMPFILE files can be re-linked, this means link count goes from 0 to 1.
209  * This case needs special care, otherwise all references to the inode will
210  * be removed upon the first replay entry of an inode with link count 0
211  * is found.
212  */
inode_still_linked(struct ubifs_info * c,struct replay_entry * rino)213 static bool inode_still_linked(struct ubifs_info *c, struct replay_entry *rino)
214 {
215 	struct replay_entry *r;
216 
217 	ubifs_assert(c, rino->deletion);
218 	ubifs_assert(c, key_type(c, &rino->key) == UBIFS_INO_KEY);
219 
220 	/*
221 	 * Find the most recent entry for the inode behind @rino and check
222 	 * whether it is a deletion.
223 	 */
224 	list_for_each_entry_reverse(r, &c->replay_list, list) {
225 		ubifs_assert(c, r->sqnum >= rino->sqnum);
226 		if (key_inum(c, &r->key) == key_inum(c, &rino->key) &&
227 		    key_type(c, &r->key) == UBIFS_INO_KEY)
228 			return r->deletion == 0;
229 
230 	}
231 
232 	ubifs_assert(c, 0);
233 	return false;
234 }
235 
236 /**
237  * apply_replay_entry - apply a replay entry to the TNC.
238  * @c: UBIFS file-system description object
239  * @r: replay entry to apply
240  *
241  * Apply a replay entry to the TNC.
242  */
apply_replay_entry(struct ubifs_info * c,struct replay_entry * r)243 static int apply_replay_entry(struct ubifs_info *c, struct replay_entry *r)
244 {
245 	int err;
246 
247 	dbg_mntk(&r->key, "LEB %d:%d len %d deletion %d sqnum %llu key ",
248 		 r->lnum, r->offs, r->len, r->deletion, r->sqnum);
249 
250 	if (is_hash_key(c, &r->key)) {
251 		if (r->deletion)
252 			err = ubifs_tnc_remove_nm(c, &r->key, &r->nm);
253 		else
254 			err = ubifs_tnc_add_nm(c, &r->key, r->lnum, r->offs,
255 					       r->len, r->hash, &r->nm);
256 	} else {
257 		if (r->deletion)
258 			switch (key_type(c, &r->key)) {
259 			case UBIFS_INO_KEY:
260 			{
261 				ino_t inum = key_inum(c, &r->key);
262 
263 				if (inode_still_linked(c, r)) {
264 					err = 0;
265 					break;
266 				}
267 
268 				err = ubifs_tnc_remove_ino(c, inum);
269 				break;
270 			}
271 			case UBIFS_TRUN_KEY:
272 				err = trun_remove_range(c, r);
273 				break;
274 			default:
275 				err = ubifs_tnc_remove(c, &r->key);
276 				break;
277 			}
278 		else
279 			err = ubifs_tnc_add(c, &r->key, r->lnum, r->offs,
280 					    r->len, r->hash);
281 		if (err)
282 			return err;
283 
284 		if (c->need_recovery)
285 			err = ubifs_recover_size_accum(c, &r->key, r->deletion,
286 						       r->new_size);
287 	}
288 
289 	return err;
290 }
291 
292 /**
293  * replay_entries_cmp - compare 2 replay entries.
294  * @priv: UBIFS file-system description object
295  * @a: first replay entry
296  * @b: second replay entry
297  *
298  * This is a comparios function for 'list_sort()' which compares 2 replay
299  * entries @a and @b by comparing their sequence numer.  Returns %1 if @a has
300  * greater sequence number and %-1 otherwise.
301  */
replay_entries_cmp(void * priv,struct list_head * a,struct list_head * b)302 static int replay_entries_cmp(void *priv, struct list_head *a,
303 			      struct list_head *b)
304 {
305 	struct ubifs_info *c = priv;
306 	struct replay_entry *ra, *rb;
307 
308 	cond_resched();
309 	if (a == b)
310 		return 0;
311 
312 	ra = list_entry(a, struct replay_entry, list);
313 	rb = list_entry(b, struct replay_entry, list);
314 	ubifs_assert(c, ra->sqnum != rb->sqnum);
315 	if (ra->sqnum > rb->sqnum)
316 		return 1;
317 	return -1;
318 }
319 
320 /**
321  * apply_replay_list - apply the replay list to the TNC.
322  * @c: UBIFS file-system description object
323  *
324  * Apply all entries in the replay list to the TNC. Returns zero in case of
325  * success and a negative error code in case of failure.
326  */
apply_replay_list(struct ubifs_info * c)327 static int apply_replay_list(struct ubifs_info *c)
328 {
329 	struct replay_entry *r;
330 	int err;
331 
332 	list_sort(c, &c->replay_list, &replay_entries_cmp);
333 
334 	list_for_each_entry(r, &c->replay_list, list) {
335 		cond_resched();
336 
337 		err = apply_replay_entry(c, r);
338 		if (err)
339 			return err;
340 	}
341 
342 	return 0;
343 }
344 
345 /**
346  * destroy_replay_list - destroy the replay.
347  * @c: UBIFS file-system description object
348  *
349  * Destroy the replay list.
350  */
destroy_replay_list(struct ubifs_info * c)351 static void destroy_replay_list(struct ubifs_info *c)
352 {
353 	struct replay_entry *r, *tmp;
354 
355 	list_for_each_entry_safe(r, tmp, &c->replay_list, list) {
356 		if (is_hash_key(c, &r->key))
357 			kfree(fname_name(&r->nm));
358 		list_del(&r->list);
359 		kfree(r);
360 	}
361 }
362 
363 /**
364  * insert_node - insert a node to the replay list
365  * @c: UBIFS file-system description object
366  * @lnum: node logical eraseblock number
367  * @offs: node offset
368  * @len: node length
369  * @key: node key
370  * @sqnum: sequence number
371  * @deletion: non-zero if this is a deletion
372  * @used: number of bytes in use in a LEB
373  * @old_size: truncation old size
374  * @new_size: truncation new size
375  *
376  * This function inserts a scanned non-direntry node to the replay list. The
377  * replay list contains @struct replay_entry elements, and we sort this list in
378  * sequence number order before applying it. The replay list is applied at the
379  * very end of the replay process. Since the list is sorted in sequence number
380  * order, the older modifications are applied first. This function returns zero
381  * in case of success and a negative error code in case of failure.
382  */
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)383 static int insert_node(struct ubifs_info *c, int lnum, int offs, int len,
384 		       const u8 *hash, union ubifs_key *key,
385 		       unsigned long long sqnum, int deletion, int *used,
386 		       loff_t old_size, loff_t new_size)
387 {
388 	struct replay_entry *r;
389 
390 	dbg_mntk(key, "add LEB %d:%d, key ", lnum, offs);
391 
392 	if (key_inum(c, key) >= c->highest_inum)
393 		c->highest_inum = key_inum(c, key);
394 
395 	r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL);
396 	if (!r)
397 		return -ENOMEM;
398 
399 	if (!deletion)
400 		*used += ALIGN(len, 8);
401 	r->lnum = lnum;
402 	r->offs = offs;
403 	r->len = len;
404 	ubifs_copy_hash(c, hash, r->hash);
405 	r->deletion = !!deletion;
406 	r->sqnum = sqnum;
407 	key_copy(c, key, &r->key);
408 	r->old_size = old_size;
409 	r->new_size = new_size;
410 
411 	list_add_tail(&r->list, &c->replay_list);
412 	return 0;
413 }
414 
415 /**
416  * insert_dent - insert a directory entry node into the replay list.
417  * @c: UBIFS file-system description object
418  * @lnum: node logical eraseblock number
419  * @offs: node offset
420  * @len: node length
421  * @key: node key
422  * @name: directory entry name
423  * @nlen: directory entry name length
424  * @sqnum: sequence number
425  * @deletion: non-zero if this is a deletion
426  * @used: number of bytes in use in a LEB
427  *
428  * This function inserts a scanned directory entry node or an extended
429  * attribute entry to the replay list. Returns zero in case of success and a
430  * negative error code in case of failure.
431  */
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)432 static int insert_dent(struct ubifs_info *c, int lnum, int offs, int len,
433 		       const u8 *hash, union ubifs_key *key,
434 		       const char *name, int nlen, unsigned long long sqnum,
435 		       int deletion, int *used)
436 {
437 	struct replay_entry *r;
438 	char *nbuf;
439 
440 	dbg_mntk(key, "add LEB %d:%d, key ", lnum, offs);
441 	if (key_inum(c, key) >= c->highest_inum)
442 		c->highest_inum = key_inum(c, key);
443 
444 	r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL);
445 	if (!r)
446 		return -ENOMEM;
447 
448 	nbuf = kmalloc(nlen + 1, GFP_KERNEL);
449 	if (!nbuf) {
450 		kfree(r);
451 		return -ENOMEM;
452 	}
453 
454 	if (!deletion)
455 		*used += ALIGN(len, 8);
456 	r->lnum = lnum;
457 	r->offs = offs;
458 	r->len = len;
459 	ubifs_copy_hash(c, hash, r->hash);
460 	r->deletion = !!deletion;
461 	r->sqnum = sqnum;
462 	key_copy(c, key, &r->key);
463 	fname_len(&r->nm) = nlen;
464 	memcpy(nbuf, name, nlen);
465 	nbuf[nlen] = '\0';
466 	fname_name(&r->nm) = nbuf;
467 
468 	list_add_tail(&r->list, &c->replay_list);
469 	return 0;
470 }
471 
472 /**
473  * ubifs_validate_entry - validate directory or extended attribute entry node.
474  * @c: UBIFS file-system description object
475  * @dent: the node to validate
476  *
477  * This function validates directory or extended attribute entry node @dent.
478  * Returns zero if the node is all right and a %-EINVAL if not.
479  */
ubifs_validate_entry(struct ubifs_info * c,const struct ubifs_dent_node * dent)480 int ubifs_validate_entry(struct ubifs_info *c,
481 			 const struct ubifs_dent_node *dent)
482 {
483 	int key_type = key_type_flash(c, dent->key);
484 	int nlen = le16_to_cpu(dent->nlen);
485 
486 	if (le32_to_cpu(dent->ch.len) != nlen + UBIFS_DENT_NODE_SZ + 1 ||
487 	    dent->type >= UBIFS_ITYPES_CNT ||
488 	    nlen > UBIFS_MAX_NLEN || dent->name[nlen] != 0 ||
489 	    (key_type == UBIFS_XENT_KEY && strnlen(dent->name, nlen) != nlen) ||
490 	    le64_to_cpu(dent->inum) > MAX_INUM) {
491 		ubifs_err(c, "bad %s node", key_type == UBIFS_DENT_KEY ?
492 			  "directory entry" : "extended attribute entry");
493 		return -EINVAL;
494 	}
495 
496 	if (key_type != UBIFS_DENT_KEY && key_type != UBIFS_XENT_KEY) {
497 		ubifs_err(c, "bad key type %d", key_type);
498 		return -EINVAL;
499 	}
500 
501 	return 0;
502 }
503 
504 /**
505  * is_last_bud - check if the bud is the last in the journal head.
506  * @c: UBIFS file-system description object
507  * @bud: bud description object
508  *
509  * This function checks if bud @bud is the last bud in its journal head. This
510  * information is then used by 'replay_bud()' to decide whether the bud can
511  * have corruptions or not. Indeed, only last buds can be corrupted by power
512  * cuts. Returns %1 if this is the last bud, and %0 if not.
513  */
is_last_bud(struct ubifs_info * c,struct ubifs_bud * bud)514 static int is_last_bud(struct ubifs_info *c, struct ubifs_bud *bud)
515 {
516 	struct ubifs_jhead *jh = &c->jheads[bud->jhead];
517 	struct ubifs_bud *next;
518 	uint32_t data;
519 	int err;
520 
521 	if (list_is_last(&bud->list, &jh->buds_list))
522 		return 1;
523 
524 	/*
525 	 * The following is a quirk to make sure we work correctly with UBIFS
526 	 * images used with older UBIFS.
527 	 *
528 	 * Normally, the last bud will be the last in the journal head's list
529 	 * of bud. However, there is one exception if the UBIFS image belongs
530 	 * to older UBIFS. This is fairly unlikely: one would need to use old
531 	 * UBIFS, then have a power cut exactly at the right point, and then
532 	 * try to mount this image with new UBIFS.
533 	 *
534 	 * The exception is: it is possible to have 2 buds A and B, A goes
535 	 * before B, and B is the last, bud B is contains no data, and bud A is
536 	 * corrupted at the end. The reason is that in older versions when the
537 	 * journal code switched the next bud (from A to B), it first added a
538 	 * log reference node for the new bud (B), and only after this it
539 	 * synchronized the write-buffer of current bud (A). But later this was
540 	 * changed and UBIFS started to always synchronize the write-buffer of
541 	 * the bud (A) before writing the log reference for the new bud (B).
542 	 *
543 	 * But because older UBIFS always synchronized A's write-buffer before
544 	 * writing to B, we can recognize this exceptional situation but
545 	 * checking the contents of bud B - if it is empty, then A can be
546 	 * treated as the last and we can recover it.
547 	 *
548 	 * TODO: remove this piece of code in a couple of years (today it is
549 	 * 16.05.2011).
550 	 */
551 	next = list_entry(bud->list.next, struct ubifs_bud, list);
552 	if (!list_is_last(&next->list, &jh->buds_list))
553 		return 0;
554 
555 	err = ubifs_leb_read(c, next->lnum, (char *)&data, next->start, 4, 1);
556 	if (err)
557 		return 0;
558 
559 	return data == 0xFFFFFFFF;
560 }
561 
562 /* authenticate_sleb_hash and authenticate_sleb_hmac are split out for stack usage */
authenticate_sleb_hash(struct ubifs_info * c,struct shash_desc * log_hash,u8 * hash)563 static int authenticate_sleb_hash(struct ubifs_info *c, struct shash_desc *log_hash, u8 *hash)
564 {
565 	SHASH_DESC_ON_STACK(hash_desc, c->hash_tfm);
566 
567 	hash_desc->tfm = c->hash_tfm;
568 
569 	ubifs_shash_copy_state(c, log_hash, hash_desc);
570 	return crypto_shash_final(hash_desc, hash);
571 }
572 
authenticate_sleb_hmac(struct ubifs_info * c,u8 * hash,u8 * hmac)573 static int authenticate_sleb_hmac(struct ubifs_info *c, u8 *hash, u8 *hmac)
574 {
575 	SHASH_DESC_ON_STACK(hmac_desc, c->hmac_tfm);
576 
577 	hmac_desc->tfm = c->hmac_tfm;
578 
579 	return crypto_shash_digest(hmac_desc, hash, c->hash_len, hmac);
580 }
581 
582 /**
583  * authenticate_sleb - authenticate one scan LEB
584  * @c: UBIFS file-system description object
585  * @sleb: the scan LEB to authenticate
586  * @log_hash:
587  * @is_last: if true, this is is the last LEB
588  *
589  * This function iterates over the buds of a single LEB authenticating all buds
590  * with the authentication nodes on this LEB. Authentication nodes are written
591  * after some buds and contain a HMAC covering the authentication node itself
592  * and the buds between the last authentication node and the current
593  * authentication node. It can happen that the last buds cannot be authenticated
594  * because a powercut happened when some nodes were written but not the
595  * corresponding authentication node. This function returns the number of nodes
596  * that could be authenticated or a negative error code.
597  */
authenticate_sleb(struct ubifs_info * c,struct ubifs_scan_leb * sleb,struct shash_desc * log_hash,int is_last)598 static int authenticate_sleb(struct ubifs_info *c, struct ubifs_scan_leb *sleb,
599 			     struct shash_desc *log_hash, int is_last)
600 {
601 	int n_not_auth = 0;
602 	struct ubifs_scan_node *snod;
603 	int n_nodes = 0;
604 	int err;
605 	u8 hash[UBIFS_HASH_ARR_SZ];
606 	u8 hmac[UBIFS_HMAC_ARR_SZ];
607 
608 	if (!ubifs_authenticated(c))
609 		return sleb->nodes_cnt;
610 
611 	list_for_each_entry(snod, &sleb->nodes, list) {
612 
613 		n_nodes++;
614 
615 		if (snod->type == UBIFS_AUTH_NODE) {
616 			struct ubifs_auth_node *auth = snod->node;
617 
618 			err = authenticate_sleb_hash(c, log_hash, hash);
619 			if (err)
620 				goto out;
621 
622 			err = authenticate_sleb_hmac(c, hash, hmac);
623 			if (err)
624 				goto out;
625 
626 			err = ubifs_check_hmac(c, auth->hmac, hmac);
627 			if (err) {
628 				err = -EPERM;
629 				goto out;
630 			}
631 			n_not_auth = 0;
632 		} else {
633 			err = crypto_shash_update(log_hash, snod->node,
634 						  snod->len);
635 			if (err)
636 				goto out;
637 			n_not_auth++;
638 		}
639 	}
640 
641 	/*
642 	 * A powercut can happen when some nodes were written, but not yet
643 	 * the corresponding authentication node. This may only happen on
644 	 * the last bud though.
645 	 */
646 	if (n_not_auth) {
647 		if (is_last) {
648 			dbg_mnt("%d unauthenticated nodes found on LEB %d, Ignoring them",
649 				n_not_auth, sleb->lnum);
650 			err = 0;
651 		} else {
652 			dbg_mnt("%d unauthenticated nodes found on non-last LEB %d",
653 				n_not_auth, sleb->lnum);
654 			err = -EPERM;
655 		}
656 	} else {
657 		err = 0;
658 	}
659 out:
660 	return err ? err : n_nodes - n_not_auth;
661 }
662 
663 /**
664  * replay_bud - replay a bud logical eraseblock.
665  * @c: UBIFS file-system description object
666  * @b: bud entry which describes the bud
667  *
668  * This function replays bud @bud, recovers it if needed, and adds all nodes
669  * from this bud to the replay list. Returns zero in case of success and a
670  * negative error code in case of failure.
671  */
replay_bud(struct ubifs_info * c,struct bud_entry * b)672 static int replay_bud(struct ubifs_info *c, struct bud_entry *b)
673 {
674 	int is_last = is_last_bud(c, b->bud);
675 	int err = 0, used = 0, lnum = b->bud->lnum, offs = b->bud->start;
676 	int n_nodes, n = 0;
677 	struct ubifs_scan_leb *sleb;
678 	struct ubifs_scan_node *snod;
679 
680 	dbg_mnt("replay bud LEB %d, head %d, offs %d, is_last %d",
681 		lnum, b->bud->jhead, offs, is_last);
682 
683 	if (c->need_recovery && is_last)
684 		/*
685 		 * Recover only last LEBs in the journal heads, because power
686 		 * cuts may cause corruptions only in these LEBs, because only
687 		 * these LEBs could possibly be written to at the power cut
688 		 * time.
689 		 */
690 		sleb = ubifs_recover_leb(c, lnum, offs, c->sbuf, b->bud->jhead);
691 	else
692 		sleb = ubifs_scan(c, lnum, offs, c->sbuf, 0);
693 	if (IS_ERR(sleb))
694 		return PTR_ERR(sleb);
695 
696 	n_nodes = authenticate_sleb(c, sleb, b->bud->log_hash, is_last);
697 	if (n_nodes < 0) {
698 		err = n_nodes;
699 		goto out;
700 	}
701 
702 	ubifs_shash_copy_state(c, b->bud->log_hash,
703 			       c->jheads[b->bud->jhead].log_hash);
704 
705 	/*
706 	 * The bud does not have to start from offset zero - the beginning of
707 	 * the 'lnum' LEB may contain previously committed data. One of the
708 	 * things we have to do in replay is to correctly update lprops with
709 	 * newer information about this LEB.
710 	 *
711 	 * At this point lprops thinks that this LEB has 'c->leb_size - offs'
712 	 * bytes of free space because it only contain information about
713 	 * committed data.
714 	 *
715 	 * But we know that real amount of free space is 'c->leb_size -
716 	 * sleb->endpt', and the space in the 'lnum' LEB between 'offs' and
717 	 * 'sleb->endpt' is used by bud data. We have to correctly calculate
718 	 * how much of these data are dirty and update lprops with this
719 	 * information.
720 	 *
721 	 * The dirt in that LEB region is comprised of padding nodes, deletion
722 	 * nodes, truncation nodes and nodes which are obsoleted by subsequent
723 	 * nodes in this LEB. So instead of calculating clean space, we
724 	 * calculate used space ('used' variable).
725 	 */
726 
727 	list_for_each_entry(snod, &sleb->nodes, list) {
728 		u8 hash[UBIFS_HASH_ARR_SZ];
729 		int deletion = 0;
730 
731 		cond_resched();
732 
733 		if (snod->sqnum >= SQNUM_WATERMARK) {
734 			ubifs_err(c, "file system's life ended");
735 			goto out_dump;
736 		}
737 
738 		ubifs_node_calc_hash(c, snod->node, hash);
739 
740 		if (snod->sqnum > c->max_sqnum)
741 			c->max_sqnum = snod->sqnum;
742 
743 		switch (snod->type) {
744 		case UBIFS_INO_NODE:
745 		{
746 			struct ubifs_ino_node *ino = snod->node;
747 			loff_t new_size = le64_to_cpu(ino->size);
748 
749 			if (le32_to_cpu(ino->nlink) == 0)
750 				deletion = 1;
751 			err = insert_node(c, lnum, snod->offs, snod->len, hash,
752 					  &snod->key, snod->sqnum, deletion,
753 					  &used, 0, new_size);
754 			break;
755 		}
756 		case UBIFS_DATA_NODE:
757 		{
758 			struct ubifs_data_node *dn = snod->node;
759 			loff_t new_size = le32_to_cpu(dn->size) +
760 					  key_block(c, &snod->key) *
761 					  UBIFS_BLOCK_SIZE;
762 
763 			err = insert_node(c, lnum, snod->offs, snod->len, hash,
764 					  &snod->key, snod->sqnum, deletion,
765 					  &used, 0, new_size);
766 			break;
767 		}
768 		case UBIFS_DENT_NODE:
769 		case UBIFS_XENT_NODE:
770 		{
771 			struct ubifs_dent_node *dent = snod->node;
772 
773 			err = ubifs_validate_entry(c, dent);
774 			if (err)
775 				goto out_dump;
776 
777 			err = insert_dent(c, lnum, snod->offs, snod->len, hash,
778 					  &snod->key, dent->name,
779 					  le16_to_cpu(dent->nlen), snod->sqnum,
780 					  !le64_to_cpu(dent->inum), &used);
781 			break;
782 		}
783 		case UBIFS_TRUN_NODE:
784 		{
785 			struct ubifs_trun_node *trun = snod->node;
786 			loff_t old_size = le64_to_cpu(trun->old_size);
787 			loff_t new_size = le64_to_cpu(trun->new_size);
788 			union ubifs_key key;
789 
790 			/* Validate truncation node */
791 			if (old_size < 0 || old_size > c->max_inode_sz ||
792 			    new_size < 0 || new_size > c->max_inode_sz ||
793 			    old_size <= new_size) {
794 				ubifs_err(c, "bad truncation node");
795 				goto out_dump;
796 			}
797 
798 			/*
799 			 * Create a fake truncation key just to use the same
800 			 * functions which expect nodes to have keys.
801 			 */
802 			trun_key_init(c, &key, le32_to_cpu(trun->inum));
803 			err = insert_node(c, lnum, snod->offs, snod->len, hash,
804 					  &key, snod->sqnum, 1, &used,
805 					  old_size, new_size);
806 			break;
807 		}
808 		case UBIFS_AUTH_NODE:
809 			break;
810 		default:
811 			ubifs_err(c, "unexpected node type %d in bud LEB %d:%d",
812 				  snod->type, lnum, snod->offs);
813 			err = -EINVAL;
814 			goto out_dump;
815 		}
816 		if (err)
817 			goto out;
818 
819 		n++;
820 		if (n == n_nodes)
821 			break;
822 	}
823 
824 	ubifs_assert(c, ubifs_search_bud(c, lnum));
825 	ubifs_assert(c, sleb->endpt - offs >= used);
826 	ubifs_assert(c, sleb->endpt % c->min_io_size == 0);
827 
828 	b->dirty = sleb->endpt - offs - used;
829 	b->free = c->leb_size - sleb->endpt;
830 	dbg_mnt("bud LEB %d replied: dirty %d, free %d",
831 		lnum, b->dirty, b->free);
832 
833 out:
834 	ubifs_scan_destroy(sleb);
835 	return err;
836 
837 out_dump:
838 	ubifs_err(c, "bad node is at LEB %d:%d", lnum, snod->offs);
839 	ubifs_dump_node(c, snod->node);
840 	ubifs_scan_destroy(sleb);
841 	return -EINVAL;
842 }
843 
844 /**
845  * replay_buds - replay all buds.
846  * @c: UBIFS file-system description object
847  *
848  * This function returns zero in case of success and a negative error code in
849  * case of failure.
850  */
replay_buds(struct ubifs_info * c)851 static int replay_buds(struct ubifs_info *c)
852 {
853 	struct bud_entry *b;
854 	int err;
855 	unsigned long long prev_sqnum = 0;
856 
857 	list_for_each_entry(b, &c->replay_buds, list) {
858 		err = replay_bud(c, b);
859 		if (err)
860 			return err;
861 
862 		ubifs_assert(c, b->sqnum > prev_sqnum);
863 		prev_sqnum = b->sqnum;
864 	}
865 
866 	return 0;
867 }
868 
869 /**
870  * destroy_bud_list - destroy the list of buds to replay.
871  * @c: UBIFS file-system description object
872  */
destroy_bud_list(struct ubifs_info * c)873 static void destroy_bud_list(struct ubifs_info *c)
874 {
875 	struct bud_entry *b;
876 
877 	while (!list_empty(&c->replay_buds)) {
878 		b = list_entry(c->replay_buds.next, struct bud_entry, list);
879 		list_del(&b->list);
880 		kfree(b);
881 	}
882 }
883 
884 /**
885  * add_replay_bud - add a bud to the list of buds to replay.
886  * @c: UBIFS file-system description object
887  * @lnum: bud logical eraseblock number to replay
888  * @offs: bud start offset
889  * @jhead: journal head to which this bud belongs
890  * @sqnum: reference node sequence number
891  *
892  * This function returns zero in case of success and a negative error code in
893  * case of failure.
894  */
add_replay_bud(struct ubifs_info * c,int lnum,int offs,int jhead,unsigned long long sqnum)895 static int add_replay_bud(struct ubifs_info *c, int lnum, int offs, int jhead,
896 			  unsigned long long sqnum)
897 {
898 	struct ubifs_bud *bud;
899 	struct bud_entry *b;
900 	int err;
901 
902 	dbg_mnt("add replay bud LEB %d:%d, head %d", lnum, offs, jhead);
903 
904 	bud = kmalloc(sizeof(struct ubifs_bud), GFP_KERNEL);
905 	if (!bud)
906 		return -ENOMEM;
907 
908 	b = kmalloc(sizeof(struct bud_entry), GFP_KERNEL);
909 	if (!b) {
910 		err = -ENOMEM;
911 		goto out;
912 	}
913 
914 	bud->lnum = lnum;
915 	bud->start = offs;
916 	bud->jhead = jhead;
917 	bud->log_hash = ubifs_hash_get_desc(c);
918 	if (IS_ERR(bud->log_hash)) {
919 		err = PTR_ERR(bud->log_hash);
920 		goto out;
921 	}
922 
923 	ubifs_shash_copy_state(c, c->log_hash, bud->log_hash);
924 
925 	ubifs_add_bud(c, bud);
926 
927 	b->bud = bud;
928 	b->sqnum = sqnum;
929 	list_add_tail(&b->list, &c->replay_buds);
930 
931 	return 0;
932 out:
933 	kfree(bud);
934 	kfree(b);
935 
936 	return err;
937 }
938 
939 /**
940  * validate_ref - validate a reference node.
941  * @c: UBIFS file-system description object
942  * @ref: the reference node to validate
943  * @ref_lnum: LEB number of the reference node
944  * @ref_offs: reference node offset
945  *
946  * This function returns %1 if a bud reference already exists for the LEB. %0 is
947  * returned if the reference node is new, otherwise %-EINVAL is returned if
948  * validation failed.
949  */
validate_ref(struct ubifs_info * c,const struct ubifs_ref_node * ref)950 static int validate_ref(struct ubifs_info *c, const struct ubifs_ref_node *ref)
951 {
952 	struct ubifs_bud *bud;
953 	int lnum = le32_to_cpu(ref->lnum);
954 	unsigned int offs = le32_to_cpu(ref->offs);
955 	unsigned int jhead = le32_to_cpu(ref->jhead);
956 
957 	/*
958 	 * ref->offs may point to the end of LEB when the journal head points
959 	 * to the end of LEB and we write reference node for it during commit.
960 	 * So this is why we require 'offs > c->leb_size'.
961 	 */
962 	if (jhead >= c->jhead_cnt || lnum >= c->leb_cnt ||
963 	    lnum < c->main_first || offs > c->leb_size ||
964 	    offs & (c->min_io_size - 1))
965 		return -EINVAL;
966 
967 	/* Make sure we have not already looked at this bud */
968 	bud = ubifs_search_bud(c, lnum);
969 	if (bud) {
970 		if (bud->jhead == jhead && bud->start <= offs)
971 			return 1;
972 		ubifs_err(c, "bud at LEB %d:%d was already referred", lnum, offs);
973 		return -EINVAL;
974 	}
975 
976 	return 0;
977 }
978 
979 /**
980  * replay_log_leb - replay a log logical eraseblock.
981  * @c: UBIFS file-system description object
982  * @lnum: log logical eraseblock to replay
983  * @offs: offset to start replaying from
984  * @sbuf: scan buffer
985  *
986  * This function replays a log LEB and returns zero in case of success, %1 if
987  * this is the last LEB in the log, and a negative error code in case of
988  * failure.
989  */
replay_log_leb(struct ubifs_info * c,int lnum,int offs,void * sbuf)990 static int replay_log_leb(struct ubifs_info *c, int lnum, int offs, void *sbuf)
991 {
992 	int err;
993 	struct ubifs_scan_leb *sleb;
994 	struct ubifs_scan_node *snod;
995 	const struct ubifs_cs_node *node;
996 
997 	dbg_mnt("replay log LEB %d:%d", lnum, offs);
998 	sleb = ubifs_scan(c, lnum, offs, sbuf, c->need_recovery);
999 	if (IS_ERR(sleb)) {
1000 		if (PTR_ERR(sleb) != -EUCLEAN || !c->need_recovery)
1001 			return PTR_ERR(sleb);
1002 		/*
1003 		 * Note, the below function will recover this log LEB only if
1004 		 * it is the last, because unclean reboots can possibly corrupt
1005 		 * only the tail of the log.
1006 		 */
1007 		sleb = ubifs_recover_log_leb(c, lnum, offs, sbuf);
1008 		if (IS_ERR(sleb))
1009 			return PTR_ERR(sleb);
1010 	}
1011 
1012 	if (sleb->nodes_cnt == 0) {
1013 		err = 1;
1014 		goto out;
1015 	}
1016 
1017 	node = sleb->buf;
1018 	snod = list_entry(sleb->nodes.next, struct ubifs_scan_node, list);
1019 	if (c->cs_sqnum == 0) {
1020 		/*
1021 		 * This is the first log LEB we are looking at, make sure that
1022 		 * the first node is a commit start node. Also record its
1023 		 * sequence number so that UBIFS can determine where the log
1024 		 * ends, because all nodes which were have higher sequence
1025 		 * numbers.
1026 		 */
1027 		if (snod->type != UBIFS_CS_NODE) {
1028 			ubifs_err(c, "first log node at LEB %d:%d is not CS node",
1029 				  lnum, offs);
1030 			goto out_dump;
1031 		}
1032 		if (le64_to_cpu(node->cmt_no) != c->cmt_no) {
1033 			ubifs_err(c, "first CS node at LEB %d:%d has wrong commit number %llu expected %llu",
1034 				  lnum, offs,
1035 				  (unsigned long long)le64_to_cpu(node->cmt_no),
1036 				  c->cmt_no);
1037 			goto out_dump;
1038 		}
1039 
1040 		c->cs_sqnum = le64_to_cpu(node->ch.sqnum);
1041 		dbg_mnt("commit start sqnum %llu", c->cs_sqnum);
1042 
1043 		err = ubifs_shash_init(c, c->log_hash);
1044 		if (err)
1045 			goto out;
1046 
1047 		err = ubifs_shash_update(c, c->log_hash, node, UBIFS_CS_NODE_SZ);
1048 		if (err < 0)
1049 			goto out;
1050 	}
1051 
1052 	if (snod->sqnum < c->cs_sqnum) {
1053 		/*
1054 		 * This means that we reached end of log and now
1055 		 * look to the older log data, which was already
1056 		 * committed but the eraseblock was not erased (UBIFS
1057 		 * only un-maps it). So this basically means we have to
1058 		 * exit with "end of log" code.
1059 		 */
1060 		err = 1;
1061 		goto out;
1062 	}
1063 
1064 	/* Make sure the first node sits at offset zero of the LEB */
1065 	if (snod->offs != 0) {
1066 		ubifs_err(c, "first node is not at zero offset");
1067 		goto out_dump;
1068 	}
1069 
1070 	list_for_each_entry(snod, &sleb->nodes, list) {
1071 		cond_resched();
1072 
1073 		if (snod->sqnum >= SQNUM_WATERMARK) {
1074 			ubifs_err(c, "file system's life ended");
1075 			goto out_dump;
1076 		}
1077 
1078 		if (snod->sqnum < c->cs_sqnum) {
1079 			ubifs_err(c, "bad sqnum %llu, commit sqnum %llu",
1080 				  snod->sqnum, c->cs_sqnum);
1081 			goto out_dump;
1082 		}
1083 
1084 		if (snod->sqnum > c->max_sqnum)
1085 			c->max_sqnum = snod->sqnum;
1086 
1087 		switch (snod->type) {
1088 		case UBIFS_REF_NODE: {
1089 			const struct ubifs_ref_node *ref = snod->node;
1090 
1091 			err = validate_ref(c, ref);
1092 			if (err == 1)
1093 				break; /* Already have this bud */
1094 			if (err)
1095 				goto out_dump;
1096 
1097 			err = ubifs_shash_update(c, c->log_hash, ref,
1098 						 UBIFS_REF_NODE_SZ);
1099 			if (err)
1100 				goto out;
1101 
1102 			err = add_replay_bud(c, le32_to_cpu(ref->lnum),
1103 					     le32_to_cpu(ref->offs),
1104 					     le32_to_cpu(ref->jhead),
1105 					     snod->sqnum);
1106 			if (err)
1107 				goto out;
1108 
1109 			break;
1110 		}
1111 		case UBIFS_CS_NODE:
1112 			/* Make sure it sits at the beginning of LEB */
1113 			if (snod->offs != 0) {
1114 				ubifs_err(c, "unexpected node in log");
1115 				goto out_dump;
1116 			}
1117 			break;
1118 		default:
1119 			ubifs_err(c, "unexpected node in log");
1120 			goto out_dump;
1121 		}
1122 	}
1123 
1124 	if (sleb->endpt || c->lhead_offs >= c->leb_size) {
1125 		c->lhead_lnum = lnum;
1126 		c->lhead_offs = sleb->endpt;
1127 	}
1128 
1129 	err = !sleb->endpt;
1130 out:
1131 	ubifs_scan_destroy(sleb);
1132 	return err;
1133 
1134 out_dump:
1135 	ubifs_err(c, "log error detected while replaying the log at LEB %d:%d",
1136 		  lnum, offs + snod->offs);
1137 	ubifs_dump_node(c, snod->node);
1138 	ubifs_scan_destroy(sleb);
1139 	return -EINVAL;
1140 }
1141 
1142 /**
1143  * take_ihead - update the status of the index head in lprops to 'taken'.
1144  * @c: UBIFS file-system description object
1145  *
1146  * This function returns the amount of free space in the index head LEB or a
1147  * negative error code.
1148  */
take_ihead(struct ubifs_info * c)1149 static int take_ihead(struct ubifs_info *c)
1150 {
1151 	const struct ubifs_lprops *lp;
1152 	int err, free;
1153 
1154 	ubifs_get_lprops(c);
1155 
1156 	lp = ubifs_lpt_lookup_dirty(c, c->ihead_lnum);
1157 	if (IS_ERR(lp)) {
1158 		err = PTR_ERR(lp);
1159 		goto out;
1160 	}
1161 
1162 	free = lp->free;
1163 
1164 	lp = ubifs_change_lp(c, lp, LPROPS_NC, LPROPS_NC,
1165 			     lp->flags | LPROPS_TAKEN, 0);
1166 	if (IS_ERR(lp)) {
1167 		err = PTR_ERR(lp);
1168 		goto out;
1169 	}
1170 
1171 	err = free;
1172 out:
1173 	ubifs_release_lprops(c);
1174 	return err;
1175 }
1176 
1177 /**
1178  * ubifs_replay_journal - replay journal.
1179  * @c: UBIFS file-system description object
1180  *
1181  * This function scans the journal, replays and cleans it up. It makes sure all
1182  * memory data structures related to uncommitted journal are built (dirty TNC
1183  * tree, tree of buds, modified lprops, etc).
1184  */
ubifs_replay_journal(struct ubifs_info * c)1185 int ubifs_replay_journal(struct ubifs_info *c)
1186 {
1187 	int err, lnum, free;
1188 
1189 	BUILD_BUG_ON(UBIFS_TRUN_KEY > 5);
1190 
1191 	/* Update the status of the index head in lprops to 'taken' */
1192 	free = take_ihead(c);
1193 	if (free < 0)
1194 		return free; /* Error code */
1195 
1196 	if (c->ihead_offs != c->leb_size - free) {
1197 		ubifs_err(c, "bad index head LEB %d:%d", c->ihead_lnum,
1198 			  c->ihead_offs);
1199 		return -EINVAL;
1200 	}
1201 
1202 	dbg_mnt("start replaying the journal");
1203 	c->replaying = 1;
1204 	lnum = c->ltail_lnum = c->lhead_lnum;
1205 
1206 	do {
1207 		err = replay_log_leb(c, lnum, 0, c->sbuf);
1208 		if (err == 1) {
1209 			if (lnum != c->lhead_lnum)
1210 				/* We hit the end of the log */
1211 				break;
1212 
1213 			/*
1214 			 * The head of the log must always start with the
1215 			 * "commit start" node on a properly formatted UBIFS.
1216 			 * But we found no nodes at all, which means that
1217 			 * something went wrong and we cannot proceed mounting
1218 			 * the file-system.
1219 			 */
1220 			ubifs_err(c, "no UBIFS nodes found at the log head LEB %d:%d, possibly corrupted",
1221 				  lnum, 0);
1222 			err = -EINVAL;
1223 		}
1224 		if (err)
1225 			goto out;
1226 		lnum = ubifs_next_log_lnum(c, lnum);
1227 	} while (lnum != c->ltail_lnum);
1228 
1229 	err = replay_buds(c);
1230 	if (err)
1231 		goto out;
1232 
1233 	err = apply_replay_list(c);
1234 	if (err)
1235 		goto out;
1236 
1237 	err = set_buds_lprops(c);
1238 	if (err)
1239 		goto out;
1240 
1241 	/*
1242 	 * UBIFS budgeting calculations use @c->bi.uncommitted_idx variable
1243 	 * to roughly estimate index growth. Things like @c->bi.min_idx_lebs
1244 	 * depend on it. This means we have to initialize it to make sure
1245 	 * budgeting works properly.
1246 	 */
1247 	c->bi.uncommitted_idx = atomic_long_read(&c->dirty_zn_cnt);
1248 	c->bi.uncommitted_idx *= c->max_idx_node_sz;
1249 
1250 	ubifs_assert(c, c->bud_bytes <= c->max_bud_bytes || c->need_recovery);
1251 	dbg_mnt("finished, log head LEB %d:%d, max_sqnum %llu, highest_inum %lu",
1252 		c->lhead_lnum, c->lhead_offs, c->max_sqnum,
1253 		(unsigned long)c->highest_inum);
1254 out:
1255 	destroy_replay_list(c);
1256 	destroy_bud_list(c);
1257 	c->replaying = 0;
1258 	return err;
1259 }
1260