2 * This file is part of UBIFS.
4 * Copyright (C) 2006-2008 Nokia Corporation.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published by
8 * the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Authors: Adrian Hunter
20 * Artem Bityutskiy (Битюцкий Артём)
24 * This file contains journal replay code. It runs when the file-system is being
25 * mounted and requires no locking.
27 * The larger is the journal, the longer it takes to scan it, so the longer it
28 * takes to mount UBIFS. This is why the journal has limited size which may be
29 * changed depending on the system requirements. But a larger journal gives
30 * faster I/O speed because it writes the index less frequently. So this is a
31 * trade-off. Also, the journal is indexed by the in-memory index (TNC), so the
32 * larger is the journal, the more memory its index may consume.
36 #include <linux/list_sort.h>
37 #include <crypto/hash.h>
38 #include <crypto/algapi.h>
41 * struct replay_entry - replay list entry.
42 * @lnum: logical eraseblock number of the node
45 * @deletion: non-zero if this entry corresponds to a node deletion
46 * @sqnum: node sequence number
47 * @list: links the replay list
49 * @nm: directory entry name
50 * @old_size: truncation old size
51 * @new_size: truncation new size
53 * The replay process first scans all buds and builds the replay list, then
54 * sorts the replay list in nodes sequence number order, and then inserts all
55 * the replay entries to the TNC.
61 u8 hash[UBIFS_HASH_ARR_SZ];
62 unsigned int deletion:1;
63 unsigned long long sqnum;
64 struct list_head list;
67 struct fscrypt_name nm;
76 * struct bud_entry - entry in the list of buds to replay.
77 * @list: next bud in the list
78 * @bud: bud description object
79 * @sqnum: reference node sequence number
80 * @free: free bytes in the bud
81 * @dirty: dirty bytes in the bud
84 struct list_head list;
85 struct ubifs_bud *bud;
86 unsigned long long sqnum;
92 * set_bud_lprops - set free and dirty space used by a bud.
93 * @c: UBIFS file-system description object
94 * @b: bud entry which describes the bud
96 * This function makes sure the LEB properties of bud @b are set correctly
97 * after the replay. Returns zero in case of success and a negative error code
100 static int set_bud_lprops(struct ubifs_info *c, struct bud_entry *b)
102 const struct ubifs_lprops *lp;
107 lp = ubifs_lpt_lookup_dirty(c, b->bud->lnum);
114 if (b->bud->start == 0 && (lp->free != c->leb_size || lp->dirty != 0)) {
116 * The LEB was added to the journal with a starting offset of
117 * zero which means the LEB must have been empty. The LEB
118 * property values should be @lp->free == @c->leb_size and
119 * @lp->dirty == 0, but that is not the case. The reason is that
120 * the LEB had been garbage collected before it became the bud,
121 * and there was not commit inbetween. The garbage collector
122 * resets the free and dirty space without recording it
123 * anywhere except lprops, so if there was no commit then
124 * lprops does not have that information.
126 * We do not need to adjust free space because the scan has told
127 * us the exact value which is recorded in the replay entry as
130 * However we do need to subtract from the dirty space the
131 * amount of space that the garbage collector reclaimed, which
132 * is the whole LEB minus the amount of space that was free.
134 dbg_mnt("bud LEB %d was GC'd (%d free, %d dirty)", b->bud->lnum,
135 lp->free, lp->dirty);
136 dbg_gc("bud LEB %d was GC'd (%d free, %d dirty)", b->bud->lnum,
137 lp->free, lp->dirty);
138 dirty -= c->leb_size - lp->free;
140 * If the replay order was perfect the dirty space would now be
141 * zero. The order is not perfect because the journal heads
142 * race with each other. This is not a problem but is does mean
143 * that the dirty space may temporarily exceed c->leb_size
147 dbg_mnt("LEB %d lp: %d free %d dirty replay: %d free %d dirty",
148 b->bud->lnum, lp->free, lp->dirty, b->free,
151 lp = ubifs_change_lp(c, lp, b->free, dirty + b->dirty,
152 lp->flags | LPROPS_TAKEN, 0);
158 /* Make sure the journal head points to the latest bud */
159 err = ubifs_wbuf_seek_nolock(&c->jheads[b->bud->jhead].wbuf,
160 b->bud->lnum, c->leb_size - b->free);
163 ubifs_release_lprops(c);
168 * set_buds_lprops - set free and dirty space for all replayed buds.
169 * @c: UBIFS file-system description object
171 * This function sets LEB properties for all replayed buds. Returns zero in
172 * case of success and a negative error code in case of failure.
174 static int set_buds_lprops(struct ubifs_info *c)
179 list_for_each_entry(b, &c->replay_buds, list) {
180 err = set_bud_lprops(c, b);
189 * trun_remove_range - apply a replay entry for a truncation to the TNC.
190 * @c: UBIFS file-system description object
191 * @r: replay entry of truncation
193 static int trun_remove_range(struct ubifs_info *c, struct replay_entry *r)
195 unsigned min_blk, max_blk;
196 union ubifs_key min_key, max_key;
199 min_blk = r->new_size / UBIFS_BLOCK_SIZE;
200 if (r->new_size & (UBIFS_BLOCK_SIZE - 1))
203 max_blk = r->old_size / UBIFS_BLOCK_SIZE;
204 if ((r->old_size & (UBIFS_BLOCK_SIZE - 1)) == 0)
207 ino = key_inum(c, &r->key);
209 data_key_init(c, &min_key, ino, min_blk);
210 data_key_init(c, &max_key, ino, max_blk);
212 return ubifs_tnc_remove_range(c, &min_key, &max_key);
216 * inode_still_linked - check whether inode in question will be re-linked.
217 * @c: UBIFS file-system description object
218 * @rino: replay entry to test
220 * O_TMPFILE files can be re-linked, this means link count goes from 0 to 1.
221 * This case needs special care, otherwise all references to the inode will
222 * be removed upon the first replay entry of an inode with link count 0
225 static bool inode_still_linked(struct ubifs_info *c, struct replay_entry *rino)
227 struct replay_entry *r;
229 ubifs_assert(c, rino->deletion);
230 ubifs_assert(c, key_type(c, &rino->key) == UBIFS_INO_KEY);
233 * Find the most recent entry for the inode behind @rino and check
234 * whether it is a deletion.
236 list_for_each_entry_reverse(r, &c->replay_list, list) {
237 ubifs_assert(c, r->sqnum >= rino->sqnum);
238 if (key_inum(c, &r->key) == key_inum(c, &rino->key))
239 return r->deletion == 0;
248 * apply_replay_entry - apply a replay entry to the TNC.
249 * @c: UBIFS file-system description object
250 * @r: replay entry to apply
252 * Apply a replay entry to the TNC.
254 static int apply_replay_entry(struct ubifs_info *c, struct replay_entry *r)
258 dbg_mntk(&r->key, "LEB %d:%d len %d deletion %d sqnum %llu key ",
259 r->lnum, r->offs, r->len, r->deletion, r->sqnum);
261 if (is_hash_key(c, &r->key)) {
263 err = ubifs_tnc_remove_nm(c, &r->key, &r->nm);
265 err = ubifs_tnc_add_nm(c, &r->key, r->lnum, r->offs,
266 r->len, r->hash, &r->nm);
269 switch (key_type(c, &r->key)) {
272 ino_t inum = key_inum(c, &r->key);
274 if (inode_still_linked(c, r)) {
279 err = ubifs_tnc_remove_ino(c, inum);
283 err = trun_remove_range(c, r);
286 err = ubifs_tnc_remove(c, &r->key);
290 err = ubifs_tnc_add(c, &r->key, r->lnum, r->offs,
295 if (c->need_recovery)
296 err = ubifs_recover_size_accum(c, &r->key, r->deletion,
304 * replay_entries_cmp - compare 2 replay entries.
305 * @priv: UBIFS file-system description object
306 * @a: first replay entry
307 * @b: second replay entry
309 * This is a comparios function for 'list_sort()' which compares 2 replay
310 * entries @a and @b by comparing their sequence numer. Returns %1 if @a has
311 * greater sequence number and %-1 otherwise.
313 static int replay_entries_cmp(void *priv, struct list_head *a,
316 struct ubifs_info *c = priv;
317 struct replay_entry *ra, *rb;
323 ra = list_entry(a, struct replay_entry, list);
324 rb = list_entry(b, struct replay_entry, list);
325 ubifs_assert(c, ra->sqnum != rb->sqnum);
326 if (ra->sqnum > rb->sqnum)
332 * apply_replay_list - apply the replay list to the TNC.
333 * @c: UBIFS file-system description object
335 * Apply all entries in the replay list to the TNC. Returns zero in case of
336 * success and a negative error code in case of failure.
338 static int apply_replay_list(struct ubifs_info *c)
340 struct replay_entry *r;
343 list_sort(c, &c->replay_list, &replay_entries_cmp);
345 list_for_each_entry(r, &c->replay_list, list) {
348 err = apply_replay_entry(c, r);
357 * destroy_replay_list - destroy the replay.
358 * @c: UBIFS file-system description object
360 * Destroy the replay list.
362 static void destroy_replay_list(struct ubifs_info *c)
364 struct replay_entry *r, *tmp;
366 list_for_each_entry_safe(r, tmp, &c->replay_list, list) {
367 if (is_hash_key(c, &r->key))
368 kfree(fname_name(&r->nm));
375 * insert_node - insert a node to the replay list
376 * @c: UBIFS file-system description object
377 * @lnum: node logical eraseblock number
381 * @sqnum: sequence number
382 * @deletion: non-zero if this is a deletion
383 * @used: number of bytes in use in a LEB
384 * @old_size: truncation old size
385 * @new_size: truncation new size
387 * This function inserts a scanned non-direntry node to the replay list. The
388 * replay list contains @struct replay_entry elements, and we sort this list in
389 * sequence number order before applying it. The replay list is applied at the
390 * very end of the replay process. Since the list is sorted in sequence number
391 * order, the older modifications are applied first. This function returns zero
392 * in case of success and a negative error code in case of failure.
394 static int insert_node(struct ubifs_info *c, int lnum, int offs, int len,
395 const u8 *hash, union ubifs_key *key,
396 unsigned long long sqnum, int deletion, int *used,
397 loff_t old_size, loff_t new_size)
399 struct replay_entry *r;
401 dbg_mntk(key, "add LEB %d:%d, key ", lnum, offs);
403 if (key_inum(c, key) >= c->highest_inum)
404 c->highest_inum = key_inum(c, key);
406 r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL);
411 *used += ALIGN(len, 8);
415 ubifs_copy_hash(c, hash, r->hash);
416 r->deletion = !!deletion;
418 key_copy(c, key, &r->key);
419 r->old_size = old_size;
420 r->new_size = new_size;
422 list_add_tail(&r->list, &c->replay_list);
427 * insert_dent - insert a directory entry node into the replay list.
428 * @c: UBIFS file-system description object
429 * @lnum: node logical eraseblock number
433 * @name: directory entry name
434 * @nlen: directory entry name length
435 * @sqnum: sequence number
436 * @deletion: non-zero if this is a deletion
437 * @used: number of bytes in use in a LEB
439 * This function inserts a scanned directory entry node or an extended
440 * attribute entry to the replay list. Returns zero in case of success and a
441 * negative error code in case of failure.
443 static int insert_dent(struct ubifs_info *c, int lnum, int offs, int len,
444 const u8 *hash, union ubifs_key *key,
445 const char *name, int nlen, unsigned long long sqnum,
446 int deletion, int *used)
448 struct replay_entry *r;
451 dbg_mntk(key, "add LEB %d:%d, key ", lnum, offs);
452 if (key_inum(c, key) >= c->highest_inum)
453 c->highest_inum = key_inum(c, key);
455 r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL);
459 nbuf = kmalloc(nlen + 1, GFP_KERNEL);
466 *used += ALIGN(len, 8);
470 ubifs_copy_hash(c, hash, r->hash);
471 r->deletion = !!deletion;
473 key_copy(c, key, &r->key);
474 fname_len(&r->nm) = nlen;
475 memcpy(nbuf, name, nlen);
477 fname_name(&r->nm) = nbuf;
479 list_add_tail(&r->list, &c->replay_list);
484 * ubifs_validate_entry - validate directory or extended attribute entry node.
485 * @c: UBIFS file-system description object
486 * @dent: the node to validate
488 * This function validates directory or extended attribute entry node @dent.
489 * Returns zero if the node is all right and a %-EINVAL if not.
491 int ubifs_validate_entry(struct ubifs_info *c,
492 const struct ubifs_dent_node *dent)
494 int key_type = key_type_flash(c, dent->key);
495 int nlen = le16_to_cpu(dent->nlen);
497 if (le32_to_cpu(dent->ch.len) != nlen + UBIFS_DENT_NODE_SZ + 1 ||
498 dent->type >= UBIFS_ITYPES_CNT ||
499 nlen > UBIFS_MAX_NLEN || dent->name[nlen] != 0 ||
500 (key_type == UBIFS_XENT_KEY && strnlen(dent->name, nlen) != nlen) ||
501 le64_to_cpu(dent->inum) > MAX_INUM) {
502 ubifs_err(c, "bad %s node", key_type == UBIFS_DENT_KEY ?
503 "directory entry" : "extended attribute entry");
507 if (key_type != UBIFS_DENT_KEY && key_type != UBIFS_XENT_KEY) {
508 ubifs_err(c, "bad key type %d", key_type);
516 * is_last_bud - check if the bud is the last in the journal head.
517 * @c: UBIFS file-system description object
518 * @bud: bud description object
520 * This function checks if bud @bud is the last bud in its journal head. This
521 * information is then used by 'replay_bud()' to decide whether the bud can
522 * have corruptions or not. Indeed, only last buds can be corrupted by power
523 * cuts. Returns %1 if this is the last bud, and %0 if not.
525 static int is_last_bud(struct ubifs_info *c, struct ubifs_bud *bud)
527 struct ubifs_jhead *jh = &c->jheads[bud->jhead];
528 struct ubifs_bud *next;
532 if (list_is_last(&bud->list, &jh->buds_list))
536 * The following is a quirk to make sure we work correctly with UBIFS
537 * images used with older UBIFS.
539 * Normally, the last bud will be the last in the journal head's list
540 * of bud. However, there is one exception if the UBIFS image belongs
541 * to older UBIFS. This is fairly unlikely: one would need to use old
542 * UBIFS, then have a power cut exactly at the right point, and then
543 * try to mount this image with new UBIFS.
545 * The exception is: it is possible to have 2 buds A and B, A goes
546 * before B, and B is the last, bud B is contains no data, and bud A is
547 * corrupted at the end. The reason is that in older versions when the
548 * journal code switched the next bud (from A to B), it first added a
549 * log reference node for the new bud (B), and only after this it
550 * synchronized the write-buffer of current bud (A). But later this was
551 * changed and UBIFS started to always synchronize the write-buffer of
552 * the bud (A) before writing the log reference for the new bud (B).
554 * But because older UBIFS always synchronized A's write-buffer before
555 * writing to B, we can recognize this exceptional situation but
556 * checking the contents of bud B - if it is empty, then A can be
557 * treated as the last and we can recover it.
559 * TODO: remove this piece of code in a couple of years (today it is
562 next = list_entry(bud->list.next, struct ubifs_bud, list);
563 if (!list_is_last(&next->list, &jh->buds_list))
566 err = ubifs_leb_read(c, next->lnum, (char *)&data, next->start, 4, 1);
570 return data == 0xFFFFFFFF;
573 /* authenticate_sleb_hash and authenticate_sleb_hmac are split out for stack usage */
574 static int authenticate_sleb_hash(struct ubifs_info *c, struct shash_desc *log_hash, u8 *hash)
576 SHASH_DESC_ON_STACK(hash_desc, c->hash_tfm);
578 hash_desc->tfm = c->hash_tfm;
580 ubifs_shash_copy_state(c, log_hash, hash_desc);
581 return crypto_shash_final(hash_desc, hash);
584 static int authenticate_sleb_hmac(struct ubifs_info *c, u8 *hash, u8 *hmac)
586 SHASH_DESC_ON_STACK(hmac_desc, c->hmac_tfm);
588 hmac_desc->tfm = c->hmac_tfm;
590 return crypto_shash_digest(hmac_desc, hash, c->hash_len, hmac);
594 * authenticate_sleb - authenticate one scan LEB
595 * @c: UBIFS file-system description object
596 * @sleb: the scan LEB to authenticate
598 * @is_last: if true, this is is the last LEB
600 * This function iterates over the buds of a single LEB authenticating all buds
601 * with the authentication nodes on this LEB. Authentication nodes are written
602 * after some buds and contain a HMAC covering the authentication node itself
603 * and the buds between the last authentication node and the current
604 * authentication node. It can happen that the last buds cannot be authenticated
605 * because a powercut happened when some nodes were written but not the
606 * corresponding authentication node. This function returns the number of nodes
607 * that could be authenticated or a negative error code.
609 static int authenticate_sleb(struct ubifs_info *c, struct ubifs_scan_leb *sleb,
610 struct shash_desc *log_hash, int is_last)
613 struct ubifs_scan_node *snod;
618 if (!ubifs_authenticated(c))
619 return sleb->nodes_cnt;
621 hash = kmalloc(crypto_shash_descsize(c->hash_tfm), GFP_NOFS);
622 hmac = kmalloc(c->hmac_desc_len, GFP_NOFS);
623 if (!hash || !hmac) {
628 list_for_each_entry(snod, &sleb->nodes, list) {
632 if (snod->type == UBIFS_AUTH_NODE) {
633 struct ubifs_auth_node *auth = snod->node;
635 err = authenticate_sleb_hash(c, log_hash, hash);
639 err = authenticate_sleb_hmac(c, hash, hmac);
643 err = ubifs_check_hmac(c, auth->hmac, hmac);
650 err = crypto_shash_update(log_hash, snod->node,
659 * A powercut can happen when some nodes were written, but not yet
660 * the corresponding authentication node. This may only happen on
661 * the last bud though.
665 dbg_mnt("%d unauthenticated nodes found on LEB %d, Ignoring them",
666 n_not_auth, sleb->lnum);
669 dbg_mnt("%d unauthenticated nodes found on non-last LEB %d",
670 n_not_auth, sleb->lnum);
680 return err ? err : n_nodes - n_not_auth;
684 * replay_bud - replay a bud logical eraseblock.
685 * @c: UBIFS file-system description object
686 * @b: bud entry which describes the bud
688 * This function replays bud @bud, recovers it if needed, and adds all nodes
689 * from this bud to the replay list. Returns zero in case of success and a
690 * negative error code in case of failure.
692 static int replay_bud(struct ubifs_info *c, struct bud_entry *b)
694 int is_last = is_last_bud(c, b->bud);
695 int err = 0, used = 0, lnum = b->bud->lnum, offs = b->bud->start;
697 struct ubifs_scan_leb *sleb;
698 struct ubifs_scan_node *snod;
700 dbg_mnt("replay bud LEB %d, head %d, offs %d, is_last %d",
701 lnum, b->bud->jhead, offs, is_last);
703 if (c->need_recovery && is_last)
705 * Recover only last LEBs in the journal heads, because power
706 * cuts may cause corruptions only in these LEBs, because only
707 * these LEBs could possibly be written to at the power cut
710 sleb = ubifs_recover_leb(c, lnum, offs, c->sbuf, b->bud->jhead);
712 sleb = ubifs_scan(c, lnum, offs, c->sbuf, 0);
714 return PTR_ERR(sleb);
716 n_nodes = authenticate_sleb(c, sleb, b->bud->log_hash, is_last);
722 ubifs_shash_copy_state(c, b->bud->log_hash,
723 c->jheads[b->bud->jhead].log_hash);
726 * The bud does not have to start from offset zero - the beginning of
727 * the 'lnum' LEB may contain previously committed data. One of the
728 * things we have to do in replay is to correctly update lprops with
729 * newer information about this LEB.
731 * At this point lprops thinks that this LEB has 'c->leb_size - offs'
732 * bytes of free space because it only contain information about
735 * But we know that real amount of free space is 'c->leb_size -
736 * sleb->endpt', and the space in the 'lnum' LEB between 'offs' and
737 * 'sleb->endpt' is used by bud data. We have to correctly calculate
738 * how much of these data are dirty and update lprops with this
741 * The dirt in that LEB region is comprised of padding nodes, deletion
742 * nodes, truncation nodes and nodes which are obsoleted by subsequent
743 * nodes in this LEB. So instead of calculating clean space, we
744 * calculate used space ('used' variable).
747 list_for_each_entry(snod, &sleb->nodes, list) {
748 u8 hash[UBIFS_HASH_ARR_SZ];
753 if (snod->sqnum >= SQNUM_WATERMARK) {
754 ubifs_err(c, "file system's life ended");
758 ubifs_node_calc_hash(c, snod->node, hash);
760 if (snod->sqnum > c->max_sqnum)
761 c->max_sqnum = snod->sqnum;
763 switch (snod->type) {
766 struct ubifs_ino_node *ino = snod->node;
767 loff_t new_size = le64_to_cpu(ino->size);
769 if (le32_to_cpu(ino->nlink) == 0)
771 err = insert_node(c, lnum, snod->offs, snod->len, hash,
772 &snod->key, snod->sqnum, deletion,
776 case UBIFS_DATA_NODE:
778 struct ubifs_data_node *dn = snod->node;
779 loff_t new_size = le32_to_cpu(dn->size) +
780 key_block(c, &snod->key) *
783 err = insert_node(c, lnum, snod->offs, snod->len, hash,
784 &snod->key, snod->sqnum, deletion,
788 case UBIFS_DENT_NODE:
789 case UBIFS_XENT_NODE:
791 struct ubifs_dent_node *dent = snod->node;
793 err = ubifs_validate_entry(c, dent);
797 err = insert_dent(c, lnum, snod->offs, snod->len, hash,
798 &snod->key, dent->name,
799 le16_to_cpu(dent->nlen), snod->sqnum,
800 !le64_to_cpu(dent->inum), &used);
803 case UBIFS_TRUN_NODE:
805 struct ubifs_trun_node *trun = snod->node;
806 loff_t old_size = le64_to_cpu(trun->old_size);
807 loff_t new_size = le64_to_cpu(trun->new_size);
810 /* Validate truncation node */
811 if (old_size < 0 || old_size > c->max_inode_sz ||
812 new_size < 0 || new_size > c->max_inode_sz ||
813 old_size <= new_size) {
814 ubifs_err(c, "bad truncation node");
819 * Create a fake truncation key just to use the same
820 * functions which expect nodes to have keys.
822 trun_key_init(c, &key, le32_to_cpu(trun->inum));
823 err = insert_node(c, lnum, snod->offs, snod->len, hash,
824 &key, snod->sqnum, 1, &used,
828 case UBIFS_AUTH_NODE:
831 ubifs_err(c, "unexpected node type %d in bud LEB %d:%d",
832 snod->type, lnum, snod->offs);
844 ubifs_assert(c, ubifs_search_bud(c, lnum));
845 ubifs_assert(c, sleb->endpt - offs >= used);
846 ubifs_assert(c, sleb->endpt % c->min_io_size == 0);
848 b->dirty = sleb->endpt - offs - used;
849 b->free = c->leb_size - sleb->endpt;
850 dbg_mnt("bud LEB %d replied: dirty %d, free %d",
851 lnum, b->dirty, b->free);
854 ubifs_scan_destroy(sleb);
858 ubifs_err(c, "bad node is at LEB %d:%d", lnum, snod->offs);
859 ubifs_dump_node(c, snod->node);
860 ubifs_scan_destroy(sleb);
865 * replay_buds - replay all buds.
866 * @c: UBIFS file-system description object
868 * This function returns zero in case of success and a negative error code in
871 static int replay_buds(struct ubifs_info *c)
875 unsigned long long prev_sqnum = 0;
877 list_for_each_entry(b, &c->replay_buds, list) {
878 err = replay_bud(c, b);
882 ubifs_assert(c, b->sqnum > prev_sqnum);
883 prev_sqnum = b->sqnum;
890 * destroy_bud_list - destroy the list of buds to replay.
891 * @c: UBIFS file-system description object
893 static void destroy_bud_list(struct ubifs_info *c)
897 while (!list_empty(&c->replay_buds)) {
898 b = list_entry(c->replay_buds.next, struct bud_entry, list);
905 * add_replay_bud - add a bud to the list of buds to replay.
906 * @c: UBIFS file-system description object
907 * @lnum: bud logical eraseblock number to replay
908 * @offs: bud start offset
909 * @jhead: journal head to which this bud belongs
910 * @sqnum: reference node sequence number
912 * This function returns zero in case of success and a negative error code in
915 static int add_replay_bud(struct ubifs_info *c, int lnum, int offs, int jhead,
916 unsigned long long sqnum)
918 struct ubifs_bud *bud;
922 dbg_mnt("add replay bud LEB %d:%d, head %d", lnum, offs, jhead);
924 bud = kmalloc(sizeof(struct ubifs_bud), GFP_KERNEL);
928 b = kmalloc(sizeof(struct bud_entry), GFP_KERNEL);
937 bud->log_hash = ubifs_hash_get_desc(c);
938 if (IS_ERR(bud->log_hash)) {
939 err = PTR_ERR(bud->log_hash);
943 ubifs_shash_copy_state(c, c->log_hash, bud->log_hash);
945 ubifs_add_bud(c, bud);
949 list_add_tail(&b->list, &c->replay_buds);
960 * validate_ref - validate a reference node.
961 * @c: UBIFS file-system description object
962 * @ref: the reference node to validate
963 * @ref_lnum: LEB number of the reference node
964 * @ref_offs: reference node offset
966 * This function returns %1 if a bud reference already exists for the LEB. %0 is
967 * returned if the reference node is new, otherwise %-EINVAL is returned if
970 static int validate_ref(struct ubifs_info *c, const struct ubifs_ref_node *ref)
972 struct ubifs_bud *bud;
973 int lnum = le32_to_cpu(ref->lnum);
974 unsigned int offs = le32_to_cpu(ref->offs);
975 unsigned int jhead = le32_to_cpu(ref->jhead);
978 * ref->offs may point to the end of LEB when the journal head points
979 * to the end of LEB and we write reference node for it during commit.
980 * So this is why we require 'offs > c->leb_size'.
982 if (jhead >= c->jhead_cnt || lnum >= c->leb_cnt ||
983 lnum < c->main_first || offs > c->leb_size ||
984 offs & (c->min_io_size - 1))
987 /* Make sure we have not already looked at this bud */
988 bud = ubifs_search_bud(c, lnum);
990 if (bud->jhead == jhead && bud->start <= offs)
992 ubifs_err(c, "bud at LEB %d:%d was already referred", lnum, offs);
1000 * replay_log_leb - replay a log logical eraseblock.
1001 * @c: UBIFS file-system description object
1002 * @lnum: log logical eraseblock to replay
1003 * @offs: offset to start replaying from
1004 * @sbuf: scan buffer
1006 * This function replays a log LEB and returns zero in case of success, %1 if
1007 * this is the last LEB in the log, and a negative error code in case of
1010 static int replay_log_leb(struct ubifs_info *c, int lnum, int offs, void *sbuf)
1013 struct ubifs_scan_leb *sleb;
1014 struct ubifs_scan_node *snod;
1015 const struct ubifs_cs_node *node;
1017 dbg_mnt("replay log LEB %d:%d", lnum, offs);
1018 sleb = ubifs_scan(c, lnum, offs, sbuf, c->need_recovery);
1020 if (PTR_ERR(sleb) != -EUCLEAN || !c->need_recovery)
1021 return PTR_ERR(sleb);
1023 * Note, the below function will recover this log LEB only if
1024 * it is the last, because unclean reboots can possibly corrupt
1025 * only the tail of the log.
1027 sleb = ubifs_recover_log_leb(c, lnum, offs, sbuf);
1029 return PTR_ERR(sleb);
1032 if (sleb->nodes_cnt == 0) {
1038 snod = list_entry(sleb->nodes.next, struct ubifs_scan_node, list);
1039 if (c->cs_sqnum == 0) {
1041 * This is the first log LEB we are looking at, make sure that
1042 * the first node is a commit start node. Also record its
1043 * sequence number so that UBIFS can determine where the log
1044 * ends, because all nodes which were have higher sequence
1047 if (snod->type != UBIFS_CS_NODE) {
1048 ubifs_err(c, "first log node at LEB %d:%d is not CS node",
1052 if (le64_to_cpu(node->cmt_no) != c->cmt_no) {
1053 ubifs_err(c, "first CS node at LEB %d:%d has wrong commit number %llu expected %llu",
1055 (unsigned long long)le64_to_cpu(node->cmt_no),
1060 c->cs_sqnum = le64_to_cpu(node->ch.sqnum);
1061 dbg_mnt("commit start sqnum %llu", c->cs_sqnum);
1063 err = ubifs_shash_init(c, c->log_hash);
1067 err = ubifs_shash_update(c, c->log_hash, node, UBIFS_CS_NODE_SZ);
1072 if (snod->sqnum < c->cs_sqnum) {
1074 * This means that we reached end of log and now
1075 * look to the older log data, which was already
1076 * committed but the eraseblock was not erased (UBIFS
1077 * only un-maps it). So this basically means we have to
1078 * exit with "end of log" code.
1084 /* Make sure the first node sits at offset zero of the LEB */
1085 if (snod->offs != 0) {
1086 ubifs_err(c, "first node is not at zero offset");
1090 list_for_each_entry(snod, &sleb->nodes, list) {
1093 if (snod->sqnum >= SQNUM_WATERMARK) {
1094 ubifs_err(c, "file system's life ended");
1098 if (snod->sqnum < c->cs_sqnum) {
1099 ubifs_err(c, "bad sqnum %llu, commit sqnum %llu",
1100 snod->sqnum, c->cs_sqnum);
1104 if (snod->sqnum > c->max_sqnum)
1105 c->max_sqnum = snod->sqnum;
1107 switch (snod->type) {
1108 case UBIFS_REF_NODE: {
1109 const struct ubifs_ref_node *ref = snod->node;
1111 err = validate_ref(c, ref);
1113 break; /* Already have this bud */
1117 err = ubifs_shash_update(c, c->log_hash, ref,
1122 err = add_replay_bud(c, le32_to_cpu(ref->lnum),
1123 le32_to_cpu(ref->offs),
1124 le32_to_cpu(ref->jhead),
1132 /* Make sure it sits at the beginning of LEB */
1133 if (snod->offs != 0) {
1134 ubifs_err(c, "unexpected node in log");
1139 ubifs_err(c, "unexpected node in log");
1144 if (sleb->endpt || c->lhead_offs >= c->leb_size) {
1145 c->lhead_lnum = lnum;
1146 c->lhead_offs = sleb->endpt;
1151 ubifs_scan_destroy(sleb);
1155 ubifs_err(c, "log error detected while replaying the log at LEB %d:%d",
1156 lnum, offs + snod->offs);
1157 ubifs_dump_node(c, snod->node);
1158 ubifs_scan_destroy(sleb);
1163 * take_ihead - update the status of the index head in lprops to 'taken'.
1164 * @c: UBIFS file-system description object
1166 * This function returns the amount of free space in the index head LEB or a
1167 * negative error code.
1169 static int take_ihead(struct ubifs_info *c)
1171 const struct ubifs_lprops *lp;
1174 ubifs_get_lprops(c);
1176 lp = ubifs_lpt_lookup_dirty(c, c->ihead_lnum);
1184 lp = ubifs_change_lp(c, lp, LPROPS_NC, LPROPS_NC,
1185 lp->flags | LPROPS_TAKEN, 0);
1193 ubifs_release_lprops(c);
1198 * ubifs_replay_journal - replay journal.
1199 * @c: UBIFS file-system description object
1201 * This function scans the journal, replays and cleans it up. It makes sure all
1202 * memory data structures related to uncommitted journal are built (dirty TNC
1203 * tree, tree of buds, modified lprops, etc).
1205 int ubifs_replay_journal(struct ubifs_info *c)
1207 int err, lnum, free;
1209 BUILD_BUG_ON(UBIFS_TRUN_KEY > 5);
1211 /* Update the status of the index head in lprops to 'taken' */
1212 free = take_ihead(c);
1214 return free; /* Error code */
1216 if (c->ihead_offs != c->leb_size - free) {
1217 ubifs_err(c, "bad index head LEB %d:%d", c->ihead_lnum,
1222 dbg_mnt("start replaying the journal");
1224 lnum = c->ltail_lnum = c->lhead_lnum;
1227 err = replay_log_leb(c, lnum, 0, c->sbuf);
1229 if (lnum != c->lhead_lnum)
1230 /* We hit the end of the log */
1234 * The head of the log must always start with the
1235 * "commit start" node on a properly formatted UBIFS.
1236 * But we found no nodes at all, which means that
1237 * something went wrong and we cannot proceed mounting
1240 ubifs_err(c, "no UBIFS nodes found at the log head LEB %d:%d, possibly corrupted",
1246 lnum = ubifs_next_log_lnum(c, lnum);
1247 } while (lnum != c->ltail_lnum);
1249 err = replay_buds(c);
1253 err = apply_replay_list(c);
1257 err = set_buds_lprops(c);
1262 * UBIFS budgeting calculations use @c->bi.uncommitted_idx variable
1263 * to roughly estimate index growth. Things like @c->bi.min_idx_lebs
1264 * depend on it. This means we have to initialize it to make sure
1265 * budgeting works properly.
1267 c->bi.uncommitted_idx = atomic_long_read(&c->dirty_zn_cnt);
1268 c->bi.uncommitted_idx *= c->max_idx_node_sz;
1270 ubifs_assert(c, c->bud_bytes <= c->max_bud_bytes || c->need_recovery);
1271 dbg_mnt("finished, log head LEB %d:%d, max_sqnum %llu, highest_inum %lu",
1272 c->lhead_lnum, c->lhead_offs, c->max_sqnum,
1273 (unsigned long)c->highest_inum);
1275 destroy_replay_list(c);
1276 destroy_bud_list(c);