1 // SPDX-License-Identifier: GPL-2.0-only
3 * This file is part of UBIFS.
5 * Copyright (C) 2006-2008 Nokia Corporation.
7 * Authors: Adrian Hunter
8 * Artem Bityutskiy (Битюцкий Артём)
12 * This file contains journal replay code. It runs when the file-system is being
13 * mounted and requires no locking.
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.
24 #include <linux/list_sort.h>
25 #include <crypto/hash.h>
26 #include <crypto/algapi.h>
29 * struct replay_entry - replay list entry.
30 * @lnum: logical eraseblock number of the node
33 * @deletion: non-zero if this entry corresponds to a node deletion
34 * @sqnum: node sequence number
35 * @list: links the replay list
37 * @nm: directory entry name
38 * @old_size: truncation old size
39 * @new_size: truncation new size
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.
49 u8 hash
[UBIFS_HASH_ARR_SZ
];
50 unsigned int deletion
:1;
51 unsigned long long sqnum
;
52 struct list_head list
;
55 struct fscrypt_name nm
;
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
72 struct list_head list
;
73 struct ubifs_bud
*bud
;
74 unsigned long long sqnum
;
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
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
88 static int set_bud_lprops(struct ubifs_info
*c
, struct bud_entry
*b
)
90 const struct ubifs_lprops
*lp
;
95 lp
= ubifs_lpt_lookup_dirty(c
, b
->bud
->lnum
);
102 if (b
->bud
->start
== 0 && (lp
->free
!= c
->leb_size
|| lp
->dirty
!= 0)) {
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.
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
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.
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
;
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
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
,
139 lp
= ubifs_change_lp(c
, lp
, b
->free
, dirty
+ b
->dirty
,
140 lp
->flags
| LPROPS_TAKEN
, 0);
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
);
151 ubifs_release_lprops(c
);
156 * set_buds_lprops - set free and dirty space for all replayed buds.
157 * @c: UBIFS file-system description object
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.
162 static int set_buds_lprops(struct ubifs_info
*c
)
167 list_for_each_entry(b
, &c
->replay_buds
, list
) {
168 err
= set_bud_lprops(c
, b
);
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
181 static int trun_remove_range(struct ubifs_info
*c
, struct replay_entry
*r
)
183 unsigned min_blk
, max_blk
;
184 union ubifs_key min_key
, max_key
;
187 min_blk
= r
->new_size
/ UBIFS_BLOCK_SIZE
;
188 if (r
->new_size
& (UBIFS_BLOCK_SIZE
- 1))
191 max_blk
= r
->old_size
/ UBIFS_BLOCK_SIZE
;
192 if ((r
->old_size
& (UBIFS_BLOCK_SIZE
- 1)) == 0)
195 ino
= key_inum(c
, &r
->key
);
197 data_key_init(c
, &min_key
, ino
, min_blk
);
198 data_key_init(c
, &max_key
, ino
, max_blk
);
200 return ubifs_tnc_remove_range(c
, &min_key
, &max_key
);
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
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
213 static bool inode_still_linked(struct ubifs_info
*c
, struct replay_entry
*rino
)
215 struct replay_entry
*r
;
217 ubifs_assert(c
, rino
->deletion
);
218 ubifs_assert(c
, key_type(c
, &rino
->key
) == UBIFS_INO_KEY
);
221 * Find the most recent entry for the inode behind @rino and check
222 * whether it is a deletion.
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 return r
->deletion
== 0;
236 * apply_replay_entry - apply a replay entry to the TNC.
237 * @c: UBIFS file-system description object
238 * @r: replay entry to apply
240 * Apply a replay entry to the TNC.
242 static int apply_replay_entry(struct ubifs_info
*c
, struct replay_entry
*r
)
246 dbg_mntk(&r
->key
, "LEB %d:%d len %d deletion %d sqnum %llu key ",
247 r
->lnum
, r
->offs
, r
->len
, r
->deletion
, r
->sqnum
);
249 if (is_hash_key(c
, &r
->key
)) {
251 err
= ubifs_tnc_remove_nm(c
, &r
->key
, &r
->nm
);
253 err
= ubifs_tnc_add_nm(c
, &r
->key
, r
->lnum
, r
->offs
,
254 r
->len
, r
->hash
, &r
->nm
);
257 switch (key_type(c
, &r
->key
)) {
260 ino_t inum
= key_inum(c
, &r
->key
);
262 if (inode_still_linked(c
, r
)) {
267 err
= ubifs_tnc_remove_ino(c
, inum
);
271 err
= trun_remove_range(c
, r
);
274 err
= ubifs_tnc_remove(c
, &r
->key
);
278 err
= ubifs_tnc_add(c
, &r
->key
, r
->lnum
, r
->offs
,
283 if (c
->need_recovery
)
284 err
= ubifs_recover_size_accum(c
, &r
->key
, r
->deletion
,
292 * replay_entries_cmp - compare 2 replay entries.
293 * @priv: UBIFS file-system description object
294 * @a: first replay entry
295 * @b: second replay entry
297 * This is a comparios function for 'list_sort()' which compares 2 replay
298 * entries @a and @b by comparing their sequence numer. Returns %1 if @a has
299 * greater sequence number and %-1 otherwise.
301 static int replay_entries_cmp(void *priv
, struct list_head
*a
,
304 struct ubifs_info
*c
= priv
;
305 struct replay_entry
*ra
, *rb
;
311 ra
= list_entry(a
, struct replay_entry
, list
);
312 rb
= list_entry(b
, struct replay_entry
, list
);
313 ubifs_assert(c
, ra
->sqnum
!= rb
->sqnum
);
314 if (ra
->sqnum
> rb
->sqnum
)
320 * apply_replay_list - apply the replay list to the TNC.
321 * @c: UBIFS file-system description object
323 * Apply all entries in the replay list to the TNC. Returns zero in case of
324 * success and a negative error code in case of failure.
326 static int apply_replay_list(struct ubifs_info
*c
)
328 struct replay_entry
*r
;
331 list_sort(c
, &c
->replay_list
, &replay_entries_cmp
);
333 list_for_each_entry(r
, &c
->replay_list
, list
) {
336 err
= apply_replay_entry(c
, r
);
345 * destroy_replay_list - destroy the replay.
346 * @c: UBIFS file-system description object
348 * Destroy the replay list.
350 static void destroy_replay_list(struct ubifs_info
*c
)
352 struct replay_entry
*r
, *tmp
;
354 list_for_each_entry_safe(r
, tmp
, &c
->replay_list
, list
) {
355 if (is_hash_key(c
, &r
->key
))
356 kfree(fname_name(&r
->nm
));
363 * insert_node - insert a node to the replay list
364 * @c: UBIFS file-system description object
365 * @lnum: node logical eraseblock number
369 * @sqnum: sequence number
370 * @deletion: non-zero if this is a deletion
371 * @used: number of bytes in use in a LEB
372 * @old_size: truncation old size
373 * @new_size: truncation new size
375 * This function inserts a scanned non-direntry node to the replay list. The
376 * replay list contains @struct replay_entry elements, and we sort this list in
377 * sequence number order before applying it. The replay list is applied at the
378 * very end of the replay process. Since the list is sorted in sequence number
379 * order, the older modifications are applied first. This function returns zero
380 * in case of success and a negative error code in case of failure.
382 static int insert_node(struct ubifs_info
*c
, int lnum
, int offs
, int len
,
383 const u8
*hash
, union ubifs_key
*key
,
384 unsigned long long sqnum
, int deletion
, int *used
,
385 loff_t old_size
, loff_t new_size
)
387 struct replay_entry
*r
;
389 dbg_mntk(key
, "add LEB %d:%d, key ", lnum
, offs
);
391 if (key_inum(c
, key
) >= c
->highest_inum
)
392 c
->highest_inum
= key_inum(c
, key
);
394 r
= kzalloc(sizeof(struct replay_entry
), GFP_KERNEL
);
399 *used
+= ALIGN(len
, 8);
403 ubifs_copy_hash(c
, hash
, r
->hash
);
404 r
->deletion
= !!deletion
;
406 key_copy(c
, key
, &r
->key
);
407 r
->old_size
= old_size
;
408 r
->new_size
= new_size
;
410 list_add_tail(&r
->list
, &c
->replay_list
);
415 * insert_dent - insert a directory entry node into the replay list.
416 * @c: UBIFS file-system description object
417 * @lnum: node logical eraseblock number
421 * @name: directory entry name
422 * @nlen: directory entry name length
423 * @sqnum: sequence number
424 * @deletion: non-zero if this is a deletion
425 * @used: number of bytes in use in a LEB
427 * This function inserts a scanned directory entry node or an extended
428 * attribute entry to the replay list. Returns zero in case of success and a
429 * negative error code in case of failure.
431 static int insert_dent(struct ubifs_info
*c
, int lnum
, int offs
, int len
,
432 const u8
*hash
, union ubifs_key
*key
,
433 const char *name
, int nlen
, unsigned long long sqnum
,
434 int deletion
, int *used
)
436 struct replay_entry
*r
;
439 dbg_mntk(key
, "add LEB %d:%d, key ", lnum
, offs
);
440 if (key_inum(c
, key
) >= c
->highest_inum
)
441 c
->highest_inum
= key_inum(c
, key
);
443 r
= kzalloc(sizeof(struct replay_entry
), GFP_KERNEL
);
447 nbuf
= kmalloc(nlen
+ 1, GFP_KERNEL
);
454 *used
+= ALIGN(len
, 8);
458 ubifs_copy_hash(c
, hash
, r
->hash
);
459 r
->deletion
= !!deletion
;
461 key_copy(c
, key
, &r
->key
);
462 fname_len(&r
->nm
) = nlen
;
463 memcpy(nbuf
, name
, nlen
);
465 fname_name(&r
->nm
) = nbuf
;
467 list_add_tail(&r
->list
, &c
->replay_list
);
472 * ubifs_validate_entry - validate directory or extended attribute entry node.
473 * @c: UBIFS file-system description object
474 * @dent: the node to validate
476 * This function validates directory or extended attribute entry node @dent.
477 * Returns zero if the node is all right and a %-EINVAL if not.
479 int ubifs_validate_entry(struct ubifs_info
*c
,
480 const struct ubifs_dent_node
*dent
)
482 int key_type
= key_type_flash(c
, dent
->key
);
483 int nlen
= le16_to_cpu(dent
->nlen
);
485 if (le32_to_cpu(dent
->ch
.len
) != nlen
+ UBIFS_DENT_NODE_SZ
+ 1 ||
486 dent
->type
>= UBIFS_ITYPES_CNT
||
487 nlen
> UBIFS_MAX_NLEN
|| dent
->name
[nlen
] != 0 ||
488 (key_type
== UBIFS_XENT_KEY
&& strnlen(dent
->name
, nlen
) != nlen
) ||
489 le64_to_cpu(dent
->inum
) > MAX_INUM
) {
490 ubifs_err(c
, "bad %s node", key_type
== UBIFS_DENT_KEY
?
491 "directory entry" : "extended attribute entry");
495 if (key_type
!= UBIFS_DENT_KEY
&& key_type
!= UBIFS_XENT_KEY
) {
496 ubifs_err(c
, "bad key type %d", key_type
);
504 * is_last_bud - check if the bud is the last in the journal head.
505 * @c: UBIFS file-system description object
506 * @bud: bud description object
508 * This function checks if bud @bud is the last bud in its journal head. This
509 * information is then used by 'replay_bud()' to decide whether the bud can
510 * have corruptions or not. Indeed, only last buds can be corrupted by power
511 * cuts. Returns %1 if this is the last bud, and %0 if not.
513 static int is_last_bud(struct ubifs_info
*c
, struct ubifs_bud
*bud
)
515 struct ubifs_jhead
*jh
= &c
->jheads
[bud
->jhead
];
516 struct ubifs_bud
*next
;
520 if (list_is_last(&bud
->list
, &jh
->buds_list
))
524 * The following is a quirk to make sure we work correctly with UBIFS
525 * images used with older UBIFS.
527 * Normally, the last bud will be the last in the journal head's list
528 * of bud. However, there is one exception if the UBIFS image belongs
529 * to older UBIFS. This is fairly unlikely: one would need to use old
530 * UBIFS, then have a power cut exactly at the right point, and then
531 * try to mount this image with new UBIFS.
533 * The exception is: it is possible to have 2 buds A and B, A goes
534 * before B, and B is the last, bud B is contains no data, and bud A is
535 * corrupted at the end. The reason is that in older versions when the
536 * journal code switched the next bud (from A to B), it first added a
537 * log reference node for the new bud (B), and only after this it
538 * synchronized the write-buffer of current bud (A). But later this was
539 * changed and UBIFS started to always synchronize the write-buffer of
540 * the bud (A) before writing the log reference for the new bud (B).
542 * But because older UBIFS always synchronized A's write-buffer before
543 * writing to B, we can recognize this exceptional situation but
544 * checking the contents of bud B - if it is empty, then A can be
545 * treated as the last and we can recover it.
547 * TODO: remove this piece of code in a couple of years (today it is
550 next
= list_entry(bud
->list
.next
, struct ubifs_bud
, list
);
551 if (!list_is_last(&next
->list
, &jh
->buds_list
))
554 err
= ubifs_leb_read(c
, next
->lnum
, (char *)&data
, next
->start
, 4, 1);
558 return data
== 0xFFFFFFFF;
561 /* authenticate_sleb_hash is split out for stack usage */
562 static int authenticate_sleb_hash(struct ubifs_info
*c
, struct shash_desc
*log_hash
, u8
*hash
)
564 SHASH_DESC_ON_STACK(hash_desc
, c
->hash_tfm
);
566 hash_desc
->tfm
= c
->hash_tfm
;
568 ubifs_shash_copy_state(c
, log_hash
, hash_desc
);
569 return crypto_shash_final(hash_desc
, hash
);
573 * authenticate_sleb - authenticate one scan LEB
574 * @c: UBIFS file-system description object
575 * @sleb: the scan LEB to authenticate
577 * @is_last: if true, this is is the last LEB
579 * This function iterates over the buds of a single LEB authenticating all buds
580 * with the authentication nodes on this LEB. Authentication nodes are written
581 * after some buds and contain a HMAC covering the authentication node itself
582 * and the buds between the last authentication node and the current
583 * authentication node. It can happen that the last buds cannot be authenticated
584 * because a powercut happened when some nodes were written but not the
585 * corresponding authentication node. This function returns the number of nodes
586 * that could be authenticated or a negative error code.
588 static int authenticate_sleb(struct ubifs_info
*c
, struct ubifs_scan_leb
*sleb
,
589 struct shash_desc
*log_hash
, int is_last
)
592 struct ubifs_scan_node
*snod
;
595 u8 hash
[UBIFS_HASH_ARR_SZ
];
596 u8 hmac
[UBIFS_HMAC_ARR_SZ
];
598 if (!ubifs_authenticated(c
))
599 return sleb
->nodes_cnt
;
601 list_for_each_entry(snod
, &sleb
->nodes
, list
) {
605 if (snod
->type
== UBIFS_AUTH_NODE
) {
606 struct ubifs_auth_node
*auth
= snod
->node
;
608 err
= authenticate_sleb_hash(c
, log_hash
, hash
);
612 err
= crypto_shash_tfm_digest(c
->hmac_tfm
, hash
,
617 err
= ubifs_check_hmac(c
, auth
->hmac
, hmac
);
624 err
= crypto_shash_update(log_hash
, snod
->node
,
633 * A powercut can happen when some nodes were written, but not yet
634 * the corresponding authentication node. This may only happen on
635 * the last bud though.
639 dbg_mnt("%d unauthenticated nodes found on LEB %d, Ignoring them",
640 n_not_auth
, sleb
->lnum
);
643 dbg_mnt("%d unauthenticated nodes found on non-last LEB %d",
644 n_not_auth
, sleb
->lnum
);
651 return err
? err
: n_nodes
- n_not_auth
;
655 * replay_bud - replay a bud logical eraseblock.
656 * @c: UBIFS file-system description object
657 * @b: bud entry which describes the bud
659 * This function replays bud @bud, recovers it if needed, and adds all nodes
660 * from this bud to the replay list. Returns zero in case of success and a
661 * negative error code in case of failure.
663 static int replay_bud(struct ubifs_info
*c
, struct bud_entry
*b
)
665 int is_last
= is_last_bud(c
, b
->bud
);
666 int err
= 0, used
= 0, lnum
= b
->bud
->lnum
, offs
= b
->bud
->start
;
668 struct ubifs_scan_leb
*sleb
;
669 struct ubifs_scan_node
*snod
;
671 dbg_mnt("replay bud LEB %d, head %d, offs %d, is_last %d",
672 lnum
, b
->bud
->jhead
, offs
, is_last
);
674 if (c
->need_recovery
&& is_last
)
676 * Recover only last LEBs in the journal heads, because power
677 * cuts may cause corruptions only in these LEBs, because only
678 * these LEBs could possibly be written to at the power cut
681 sleb
= ubifs_recover_leb(c
, lnum
, offs
, c
->sbuf
, b
->bud
->jhead
);
683 sleb
= ubifs_scan(c
, lnum
, offs
, c
->sbuf
, 0);
685 return PTR_ERR(sleb
);
687 n_nodes
= authenticate_sleb(c
, sleb
, b
->bud
->log_hash
, is_last
);
693 ubifs_shash_copy_state(c
, b
->bud
->log_hash
,
694 c
->jheads
[b
->bud
->jhead
].log_hash
);
697 * The bud does not have to start from offset zero - the beginning of
698 * the 'lnum' LEB may contain previously committed data. One of the
699 * things we have to do in replay is to correctly update lprops with
700 * newer information about this LEB.
702 * At this point lprops thinks that this LEB has 'c->leb_size - offs'
703 * bytes of free space because it only contain information about
706 * But we know that real amount of free space is 'c->leb_size -
707 * sleb->endpt', and the space in the 'lnum' LEB between 'offs' and
708 * 'sleb->endpt' is used by bud data. We have to correctly calculate
709 * how much of these data are dirty and update lprops with this
712 * The dirt in that LEB region is comprised of padding nodes, deletion
713 * nodes, truncation nodes and nodes which are obsoleted by subsequent
714 * nodes in this LEB. So instead of calculating clean space, we
715 * calculate used space ('used' variable).
718 list_for_each_entry(snod
, &sleb
->nodes
, list
) {
719 u8 hash
[UBIFS_HASH_ARR_SZ
];
724 if (snod
->sqnum
>= SQNUM_WATERMARK
) {
725 ubifs_err(c
, "file system's life ended");
729 ubifs_node_calc_hash(c
, snod
->node
, hash
);
731 if (snod
->sqnum
> c
->max_sqnum
)
732 c
->max_sqnum
= snod
->sqnum
;
734 switch (snod
->type
) {
737 struct ubifs_ino_node
*ino
= snod
->node
;
738 loff_t new_size
= le64_to_cpu(ino
->size
);
740 if (le32_to_cpu(ino
->nlink
) == 0)
742 err
= insert_node(c
, lnum
, snod
->offs
, snod
->len
, hash
,
743 &snod
->key
, snod
->sqnum
, deletion
,
747 case UBIFS_DATA_NODE
:
749 struct ubifs_data_node
*dn
= snod
->node
;
750 loff_t new_size
= le32_to_cpu(dn
->size
) +
751 key_block(c
, &snod
->key
) *
754 err
= insert_node(c
, lnum
, snod
->offs
, snod
->len
, hash
,
755 &snod
->key
, snod
->sqnum
, deletion
,
759 case UBIFS_DENT_NODE
:
760 case UBIFS_XENT_NODE
:
762 struct ubifs_dent_node
*dent
= snod
->node
;
764 err
= ubifs_validate_entry(c
, dent
);
768 err
= insert_dent(c
, lnum
, snod
->offs
, snod
->len
, hash
,
769 &snod
->key
, dent
->name
,
770 le16_to_cpu(dent
->nlen
), snod
->sqnum
,
771 !le64_to_cpu(dent
->inum
), &used
);
774 case UBIFS_TRUN_NODE
:
776 struct ubifs_trun_node
*trun
= snod
->node
;
777 loff_t old_size
= le64_to_cpu(trun
->old_size
);
778 loff_t new_size
= le64_to_cpu(trun
->new_size
);
781 /* Validate truncation node */
782 if (old_size
< 0 || old_size
> c
->max_inode_sz
||
783 new_size
< 0 || new_size
> c
->max_inode_sz
||
784 old_size
<= new_size
) {
785 ubifs_err(c
, "bad truncation node");
790 * Create a fake truncation key just to use the same
791 * functions which expect nodes to have keys.
793 trun_key_init(c
, &key
, le32_to_cpu(trun
->inum
));
794 err
= insert_node(c
, lnum
, snod
->offs
, snod
->len
, hash
,
795 &key
, snod
->sqnum
, 1, &used
,
799 case UBIFS_AUTH_NODE
:
802 ubifs_err(c
, "unexpected node type %d in bud LEB %d:%d",
803 snod
->type
, lnum
, snod
->offs
);
815 ubifs_assert(c
, ubifs_search_bud(c
, lnum
));
816 ubifs_assert(c
, sleb
->endpt
- offs
>= used
);
817 ubifs_assert(c
, sleb
->endpt
% c
->min_io_size
== 0);
819 b
->dirty
= sleb
->endpt
- offs
- used
;
820 b
->free
= c
->leb_size
- sleb
->endpt
;
821 dbg_mnt("bud LEB %d replied: dirty %d, free %d",
822 lnum
, b
->dirty
, b
->free
);
825 ubifs_scan_destroy(sleb
);
829 ubifs_err(c
, "bad node is at LEB %d:%d", lnum
, snod
->offs
);
830 ubifs_dump_node(c
, snod
->node
);
831 ubifs_scan_destroy(sleb
);
836 * replay_buds - replay all buds.
837 * @c: UBIFS file-system description object
839 * This function returns zero in case of success and a negative error code in
842 static int replay_buds(struct ubifs_info
*c
)
846 unsigned long long prev_sqnum
= 0;
848 list_for_each_entry(b
, &c
->replay_buds
, list
) {
849 err
= replay_bud(c
, b
);
853 ubifs_assert(c
, b
->sqnum
> prev_sqnum
);
854 prev_sqnum
= b
->sqnum
;
861 * destroy_bud_list - destroy the list of buds to replay.
862 * @c: UBIFS file-system description object
864 static void destroy_bud_list(struct ubifs_info
*c
)
868 while (!list_empty(&c
->replay_buds
)) {
869 b
= list_entry(c
->replay_buds
.next
, struct bud_entry
, list
);
876 * add_replay_bud - add a bud to the list of buds to replay.
877 * @c: UBIFS file-system description object
878 * @lnum: bud logical eraseblock number to replay
879 * @offs: bud start offset
880 * @jhead: journal head to which this bud belongs
881 * @sqnum: reference node sequence number
883 * This function returns zero in case of success and a negative error code in
886 static int add_replay_bud(struct ubifs_info
*c
, int lnum
, int offs
, int jhead
,
887 unsigned long long sqnum
)
889 struct ubifs_bud
*bud
;
893 dbg_mnt("add replay bud LEB %d:%d, head %d", lnum
, offs
, jhead
);
895 bud
= kmalloc(sizeof(struct ubifs_bud
), GFP_KERNEL
);
899 b
= kmalloc(sizeof(struct bud_entry
), GFP_KERNEL
);
908 bud
->log_hash
= ubifs_hash_get_desc(c
);
909 if (IS_ERR(bud
->log_hash
)) {
910 err
= PTR_ERR(bud
->log_hash
);
914 ubifs_shash_copy_state(c
, c
->log_hash
, bud
->log_hash
);
916 ubifs_add_bud(c
, bud
);
920 list_add_tail(&b
->list
, &c
->replay_buds
);
931 * validate_ref - validate a reference node.
932 * @c: UBIFS file-system description object
933 * @ref: the reference node to validate
934 * @ref_lnum: LEB number of the reference node
935 * @ref_offs: reference node offset
937 * This function returns %1 if a bud reference already exists for the LEB. %0 is
938 * returned if the reference node is new, otherwise %-EINVAL is returned if
941 static int validate_ref(struct ubifs_info
*c
, const struct ubifs_ref_node
*ref
)
943 struct ubifs_bud
*bud
;
944 int lnum
= le32_to_cpu(ref
->lnum
);
945 unsigned int offs
= le32_to_cpu(ref
->offs
);
946 unsigned int jhead
= le32_to_cpu(ref
->jhead
);
949 * ref->offs may point to the end of LEB when the journal head points
950 * to the end of LEB and we write reference node for it during commit.
951 * So this is why we require 'offs > c->leb_size'.
953 if (jhead
>= c
->jhead_cnt
|| lnum
>= c
->leb_cnt
||
954 lnum
< c
->main_first
|| offs
> c
->leb_size
||
955 offs
& (c
->min_io_size
- 1))
958 /* Make sure we have not already looked at this bud */
959 bud
= ubifs_search_bud(c
, lnum
);
961 if (bud
->jhead
== jhead
&& bud
->start
<= offs
)
963 ubifs_err(c
, "bud at LEB %d:%d was already referred", lnum
, offs
);
971 * replay_log_leb - replay a log logical eraseblock.
972 * @c: UBIFS file-system description object
973 * @lnum: log logical eraseblock to replay
974 * @offs: offset to start replaying from
977 * This function replays a log LEB and returns zero in case of success, %1 if
978 * this is the last LEB in the log, and a negative error code in case of
981 static int replay_log_leb(struct ubifs_info
*c
, int lnum
, int offs
, void *sbuf
)
984 struct ubifs_scan_leb
*sleb
;
985 struct ubifs_scan_node
*snod
;
986 const struct ubifs_cs_node
*node
;
988 dbg_mnt("replay log LEB %d:%d", lnum
, offs
);
989 sleb
= ubifs_scan(c
, lnum
, offs
, sbuf
, c
->need_recovery
);
991 if (PTR_ERR(sleb
) != -EUCLEAN
|| !c
->need_recovery
)
992 return PTR_ERR(sleb
);
994 * Note, the below function will recover this log LEB only if
995 * it is the last, because unclean reboots can possibly corrupt
996 * only the tail of the log.
998 sleb
= ubifs_recover_log_leb(c
, lnum
, offs
, sbuf
);
1000 return PTR_ERR(sleb
);
1003 if (sleb
->nodes_cnt
== 0) {
1009 snod
= list_entry(sleb
->nodes
.next
, struct ubifs_scan_node
, list
);
1010 if (c
->cs_sqnum
== 0) {
1012 * This is the first log LEB we are looking at, make sure that
1013 * the first node is a commit start node. Also record its
1014 * sequence number so that UBIFS can determine where the log
1015 * ends, because all nodes which were have higher sequence
1018 if (snod
->type
!= UBIFS_CS_NODE
) {
1019 ubifs_err(c
, "first log node at LEB %d:%d is not CS node",
1023 if (le64_to_cpu(node
->cmt_no
) != c
->cmt_no
) {
1024 ubifs_err(c
, "first CS node at LEB %d:%d has wrong commit number %llu expected %llu",
1026 (unsigned long long)le64_to_cpu(node
->cmt_no
),
1031 c
->cs_sqnum
= le64_to_cpu(node
->ch
.sqnum
);
1032 dbg_mnt("commit start sqnum %llu", c
->cs_sqnum
);
1034 err
= ubifs_shash_init(c
, c
->log_hash
);
1038 err
= ubifs_shash_update(c
, c
->log_hash
, node
, UBIFS_CS_NODE_SZ
);
1043 if (snod
->sqnum
< c
->cs_sqnum
) {
1045 * This means that we reached end of log and now
1046 * look to the older log data, which was already
1047 * committed but the eraseblock was not erased (UBIFS
1048 * only un-maps it). So this basically means we have to
1049 * exit with "end of log" code.
1055 /* Make sure the first node sits at offset zero of the LEB */
1056 if (snod
->offs
!= 0) {
1057 ubifs_err(c
, "first node is not at zero offset");
1061 list_for_each_entry(snod
, &sleb
->nodes
, list
) {
1064 if (snod
->sqnum
>= SQNUM_WATERMARK
) {
1065 ubifs_err(c
, "file system's life ended");
1069 if (snod
->sqnum
< c
->cs_sqnum
) {
1070 ubifs_err(c
, "bad sqnum %llu, commit sqnum %llu",
1071 snod
->sqnum
, c
->cs_sqnum
);
1075 if (snod
->sqnum
> c
->max_sqnum
)
1076 c
->max_sqnum
= snod
->sqnum
;
1078 switch (snod
->type
) {
1079 case UBIFS_REF_NODE
: {
1080 const struct ubifs_ref_node
*ref
= snod
->node
;
1082 err
= validate_ref(c
, ref
);
1084 break; /* Already have this bud */
1088 err
= ubifs_shash_update(c
, c
->log_hash
, ref
,
1093 err
= add_replay_bud(c
, le32_to_cpu(ref
->lnum
),
1094 le32_to_cpu(ref
->offs
),
1095 le32_to_cpu(ref
->jhead
),
1103 /* Make sure it sits at the beginning of LEB */
1104 if (snod
->offs
!= 0) {
1105 ubifs_err(c
, "unexpected node in log");
1110 ubifs_err(c
, "unexpected node in log");
1115 if (sleb
->endpt
|| c
->lhead_offs
>= c
->leb_size
) {
1116 c
->lhead_lnum
= lnum
;
1117 c
->lhead_offs
= sleb
->endpt
;
1122 ubifs_scan_destroy(sleb
);
1126 ubifs_err(c
, "log error detected while replaying the log at LEB %d:%d",
1127 lnum
, offs
+ snod
->offs
);
1128 ubifs_dump_node(c
, snod
->node
);
1129 ubifs_scan_destroy(sleb
);
1134 * take_ihead - update the status of the index head in lprops to 'taken'.
1135 * @c: UBIFS file-system description object
1137 * This function returns the amount of free space in the index head LEB or a
1138 * negative error code.
1140 static int take_ihead(struct ubifs_info
*c
)
1142 const struct ubifs_lprops
*lp
;
1145 ubifs_get_lprops(c
);
1147 lp
= ubifs_lpt_lookup_dirty(c
, c
->ihead_lnum
);
1155 lp
= ubifs_change_lp(c
, lp
, LPROPS_NC
, LPROPS_NC
,
1156 lp
->flags
| LPROPS_TAKEN
, 0);
1164 ubifs_release_lprops(c
);
1169 * ubifs_replay_journal - replay journal.
1170 * @c: UBIFS file-system description object
1172 * This function scans the journal, replays and cleans it up. It makes sure all
1173 * memory data structures related to uncommitted journal are built (dirty TNC
1174 * tree, tree of buds, modified lprops, etc).
1176 int ubifs_replay_journal(struct ubifs_info
*c
)
1178 int err
, lnum
, free
;
1180 BUILD_BUG_ON(UBIFS_TRUN_KEY
> 5);
1182 /* Update the status of the index head in lprops to 'taken' */
1183 free
= take_ihead(c
);
1185 return free
; /* Error code */
1187 if (c
->ihead_offs
!= c
->leb_size
- free
) {
1188 ubifs_err(c
, "bad index head LEB %d:%d", c
->ihead_lnum
,
1193 dbg_mnt("start replaying the journal");
1195 lnum
= c
->ltail_lnum
= c
->lhead_lnum
;
1198 err
= replay_log_leb(c
, lnum
, 0, c
->sbuf
);
1200 if (lnum
!= c
->lhead_lnum
)
1201 /* We hit the end of the log */
1205 * The head of the log must always start with the
1206 * "commit start" node on a properly formatted UBIFS.
1207 * But we found no nodes at all, which means that
1208 * something went wrong and we cannot proceed mounting
1211 ubifs_err(c
, "no UBIFS nodes found at the log head LEB %d:%d, possibly corrupted",
1217 lnum
= ubifs_next_log_lnum(c
, lnum
);
1218 } while (lnum
!= c
->ltail_lnum
);
1220 err
= replay_buds(c
);
1224 err
= apply_replay_list(c
);
1228 err
= set_buds_lprops(c
);
1233 * UBIFS budgeting calculations use @c->bi.uncommitted_idx variable
1234 * to roughly estimate index growth. Things like @c->bi.min_idx_lebs
1235 * depend on it. This means we have to initialize it to make sure
1236 * budgeting works properly.
1238 c
->bi
.uncommitted_idx
= atomic_long_read(&c
->dirty_zn_cnt
);
1239 c
->bi
.uncommitted_idx
*= c
->max_idx_node_sz
;
1241 ubifs_assert(c
, c
->bud_bytes
<= c
->max_bud_bytes
|| c
->need_recovery
);
1242 dbg_mnt("finished, log head LEB %d:%d, max_sqnum %llu, highest_inum %lu",
1243 c
->lhead_lnum
, c
->lhead_offs
, c
->max_sqnum
,
1244 (unsigned long)c
->highest_inum
);
1246 destroy_replay_list(c
);
1247 destroy_bud_list(c
);