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>
39 * struct replay_entry - replay list entry.
40 * @lnum: logical eraseblock number of the node
43 * @deletion: non-zero if this entry corresponds to a node deletion
44 * @sqnum: node sequence number
45 * @list: links the replay list
47 * @nm: directory entry name
48 * @old_size: truncation old size
49 * @new_size: truncation new size
51 * The replay process first scans all buds and builds the replay list, then
52 * sorts the replay list in nodes sequence number order, and then inserts all
53 * the replay entries to the TNC.
59 unsigned int deletion:1;
60 unsigned long long sqnum;
61 struct list_head list;
64 struct fscrypt_name nm;
73 * struct bud_entry - entry in the list of buds to replay.
74 * @list: next bud in the list
75 * @bud: bud description object
76 * @sqnum: reference node sequence number
77 * @free: free bytes in the bud
78 * @dirty: dirty bytes in the bud
81 struct list_head list;
82 struct ubifs_bud *bud;
83 unsigned long long sqnum;
89 * set_bud_lprops - set free and dirty space used by a bud.
90 * @c: UBIFS file-system description object
91 * @b: bud entry which describes the bud
93 * This function makes sure the LEB properties of bud @b are set correctly
94 * after the replay. Returns zero in case of success and a negative error code
97 static int set_bud_lprops(struct ubifs_info *c, struct bud_entry *b)
99 const struct ubifs_lprops *lp;
104 lp = ubifs_lpt_lookup_dirty(c, b->bud->lnum);
111 if (b->bud->start == 0 && (lp->free != c->leb_size || lp->dirty != 0)) {
113 * The LEB was added to the journal with a starting offset of
114 * zero which means the LEB must have been empty. The LEB
115 * property values should be @lp->free == @c->leb_size and
116 * @lp->dirty == 0, but that is not the case. The reason is that
117 * the LEB had been garbage collected before it became the bud,
118 * and there was not commit inbetween. The garbage collector
119 * resets the free and dirty space without recording it
120 * anywhere except lprops, so if there was no commit then
121 * lprops does not have that information.
123 * We do not need to adjust free space because the scan has told
124 * us the exact value which is recorded in the replay entry as
127 * However we do need to subtract from the dirty space the
128 * amount of space that the garbage collector reclaimed, which
129 * is the whole LEB minus the amount of space that was free.
131 dbg_mnt("bud LEB %d was GC'd (%d free, %d dirty)", b->bud->lnum,
132 lp->free, lp->dirty);
133 dbg_gc("bud LEB %d was GC'd (%d free, %d dirty)", b->bud->lnum,
134 lp->free, lp->dirty);
135 dirty -= c->leb_size - lp->free;
137 * If the replay order was perfect the dirty space would now be
138 * zero. The order is not perfect because the journal heads
139 * race with each other. This is not a problem but is does mean
140 * that the dirty space may temporarily exceed c->leb_size
144 dbg_mnt("LEB %d lp: %d free %d dirty replay: %d free %d dirty",
145 b->bud->lnum, lp->free, lp->dirty, b->free,
148 lp = ubifs_change_lp(c, lp, b->free, dirty + b->dirty,
149 lp->flags | LPROPS_TAKEN, 0);
155 /* Make sure the journal head points to the latest bud */
156 err = ubifs_wbuf_seek_nolock(&c->jheads[b->bud->jhead].wbuf,
157 b->bud->lnum, c->leb_size - b->free);
160 ubifs_release_lprops(c);
165 * set_buds_lprops - set free and dirty space for all replayed buds.
166 * @c: UBIFS file-system description object
168 * This function sets LEB properties for all replayed buds. Returns zero in
169 * case of success and a negative error code in case of failure.
171 static int set_buds_lprops(struct ubifs_info *c)
176 list_for_each_entry(b, &c->replay_buds, list) {
177 err = set_bud_lprops(c, b);
186 * trun_remove_range - apply a replay entry for a truncation to the TNC.
187 * @c: UBIFS file-system description object
188 * @r: replay entry of truncation
190 static int trun_remove_range(struct ubifs_info *c, struct replay_entry *r)
192 unsigned min_blk, max_blk;
193 union ubifs_key min_key, max_key;
196 min_blk = r->new_size / UBIFS_BLOCK_SIZE;
197 if (r->new_size & (UBIFS_BLOCK_SIZE - 1))
200 max_blk = r->old_size / UBIFS_BLOCK_SIZE;
201 if ((r->old_size & (UBIFS_BLOCK_SIZE - 1)) == 0)
204 ino = key_inum(c, &r->key);
206 data_key_init(c, &min_key, ino, min_blk);
207 data_key_init(c, &max_key, ino, max_blk);
209 return ubifs_tnc_remove_range(c, &min_key, &max_key);
213 * inode_still_linked - check whether inode in question will be re-linked.
214 * @c: UBIFS file-system description object
215 * @rino: replay entry to test
217 * O_TMPFILE files can be re-linked, this means link count goes from 0 to 1.
218 * This case needs special care, otherwise all references to the inode will
219 * be removed upon the first replay entry of an inode with link count 0
222 static bool inode_still_linked(struct ubifs_info *c, struct replay_entry *rino)
224 struct replay_entry *r;
226 ubifs_assert(c, rino->deletion);
227 ubifs_assert(c, key_type(c, &rino->key) == UBIFS_INO_KEY);
230 * Find the most recent entry for the inode behind @rino and check
231 * whether it is a deletion.
233 list_for_each_entry_reverse(r, &c->replay_list, list) {
234 ubifs_assert(c, r->sqnum >= rino->sqnum);
235 if (key_inum(c, &r->key) == key_inum(c, &rino->key) &&
236 key_type(c, &r->key) == UBIFS_INO_KEY)
237 return r->deletion == 0;
246 * apply_replay_entry - apply a replay entry to the TNC.
247 * @c: UBIFS file-system description object
248 * @r: replay entry to apply
250 * Apply a replay entry to the TNC.
252 static int apply_replay_entry(struct ubifs_info *c, struct replay_entry *r)
256 dbg_mntk(&r->key, "LEB %d:%d len %d deletion %d sqnum %llu key ",
257 r->lnum, r->offs, r->len, r->deletion, r->sqnum);
259 if (is_hash_key(c, &r->key)) {
261 err = ubifs_tnc_remove_nm(c, &r->key, &r->nm);
263 err = ubifs_tnc_add_nm(c, &r->key, r->lnum, r->offs,
267 switch (key_type(c, &r->key)) {
270 ino_t inum = key_inum(c, &r->key);
272 if (inode_still_linked(c, r)) {
277 err = ubifs_tnc_remove_ino(c, inum);
281 err = trun_remove_range(c, r);
284 err = ubifs_tnc_remove(c, &r->key);
288 err = ubifs_tnc_add(c, &r->key, r->lnum, r->offs,
293 if (c->need_recovery)
294 err = ubifs_recover_size_accum(c, &r->key, r->deletion,
302 * replay_entries_cmp - compare 2 replay entries.
303 * @priv: UBIFS file-system description object
304 * @a: first replay entry
305 * @b: second replay entry
307 * This is a comparios function for 'list_sort()' which compares 2 replay
308 * entries @a and @b by comparing their sequence numer. Returns %1 if @a has
309 * greater sequence number and %-1 otherwise.
311 static int replay_entries_cmp(void *priv, struct list_head *a,
314 struct ubifs_info *c = priv;
315 struct replay_entry *ra, *rb;
321 ra = list_entry(a, struct replay_entry, list);
322 rb = list_entry(b, struct replay_entry, list);
323 ubifs_assert(c, ra->sqnum != rb->sqnum);
324 if (ra->sqnum > rb->sqnum)
330 * apply_replay_list - apply the replay list to the TNC.
331 * @c: UBIFS file-system description object
333 * Apply all entries in the replay list to the TNC. Returns zero in case of
334 * success and a negative error code in case of failure.
336 static int apply_replay_list(struct ubifs_info *c)
338 struct replay_entry *r;
341 list_sort(c, &c->replay_list, &replay_entries_cmp);
343 list_for_each_entry(r, &c->replay_list, list) {
346 err = apply_replay_entry(c, r);
355 * destroy_replay_list - destroy the replay.
356 * @c: UBIFS file-system description object
358 * Destroy the replay list.
360 static void destroy_replay_list(struct ubifs_info *c)
362 struct replay_entry *r, *tmp;
364 list_for_each_entry_safe(r, tmp, &c->replay_list, list) {
365 if (is_hash_key(c, &r->key))
366 kfree(fname_name(&r->nm));
373 * insert_node - insert a node to the replay list
374 * @c: UBIFS file-system description object
375 * @lnum: node logical eraseblock number
379 * @sqnum: sequence number
380 * @deletion: non-zero if this is a deletion
381 * @used: number of bytes in use in a LEB
382 * @old_size: truncation old size
383 * @new_size: truncation new size
385 * This function inserts a scanned non-direntry node to the replay list. The
386 * replay list contains @struct replay_entry elements, and we sort this list in
387 * sequence number order before applying it. The replay list is applied at the
388 * very end of the replay process. Since the list is sorted in sequence number
389 * order, the older modifications are applied first. This function returns zero
390 * in case of success and a negative error code in case of failure.
392 static int insert_node(struct ubifs_info *c, int lnum, int offs, int len,
393 union ubifs_key *key, unsigned long long sqnum,
394 int deletion, int *used, loff_t old_size,
397 struct replay_entry *r;
399 dbg_mntk(key, "add LEB %d:%d, key ", lnum, offs);
401 if (key_inum(c, key) >= c->highest_inum)
402 c->highest_inum = key_inum(c, key);
404 r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL);
409 *used += ALIGN(len, 8);
413 r->deletion = !!deletion;
415 key_copy(c, key, &r->key);
416 r->old_size = old_size;
417 r->new_size = new_size;
419 list_add_tail(&r->list, &c->replay_list);
424 * insert_dent - insert a directory entry node into the replay list.
425 * @c: UBIFS file-system description object
426 * @lnum: node logical eraseblock number
430 * @name: directory entry name
431 * @nlen: directory entry name length
432 * @sqnum: sequence number
433 * @deletion: non-zero if this is a deletion
434 * @used: number of bytes in use in a LEB
436 * This function inserts a scanned directory entry node or an extended
437 * attribute entry to the replay list. Returns zero in case of success and a
438 * negative error code in case of failure.
440 static int insert_dent(struct ubifs_info *c, int lnum, int offs, int len,
441 union ubifs_key *key, const char *name, int nlen,
442 unsigned long long sqnum, int deletion, int *used)
444 struct replay_entry *r;
447 dbg_mntk(key, "add LEB %d:%d, key ", lnum, offs);
448 if (key_inum(c, key) >= c->highest_inum)
449 c->highest_inum = key_inum(c, key);
451 r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL);
455 nbuf = kmalloc(nlen + 1, GFP_KERNEL);
462 *used += ALIGN(len, 8);
466 r->deletion = !!deletion;
468 key_copy(c, key, &r->key);
469 fname_len(&r->nm) = nlen;
470 memcpy(nbuf, name, nlen);
472 fname_name(&r->nm) = nbuf;
474 list_add_tail(&r->list, &c->replay_list);
479 * ubifs_validate_entry - validate directory or extended attribute entry node.
480 * @c: UBIFS file-system description object
481 * @dent: the node to validate
483 * This function validates directory or extended attribute entry node @dent.
484 * Returns zero if the node is all right and a %-EINVAL if not.
486 int ubifs_validate_entry(struct ubifs_info *c,
487 const struct ubifs_dent_node *dent)
489 int key_type = key_type_flash(c, dent->key);
490 int nlen = le16_to_cpu(dent->nlen);
492 if (le32_to_cpu(dent->ch.len) != nlen + UBIFS_DENT_NODE_SZ + 1 ||
493 dent->type >= UBIFS_ITYPES_CNT ||
494 nlen > UBIFS_MAX_NLEN || dent->name[nlen] != 0 ||
495 (key_type == UBIFS_XENT_KEY && strnlen(dent->name, nlen) != nlen) ||
496 le64_to_cpu(dent->inum) > MAX_INUM) {
497 ubifs_err(c, "bad %s node", key_type == UBIFS_DENT_KEY ?
498 "directory entry" : "extended attribute entry");
502 if (key_type != UBIFS_DENT_KEY && key_type != UBIFS_XENT_KEY) {
503 ubifs_err(c, "bad key type %d", key_type);
511 * is_last_bud - check if the bud is the last in the journal head.
512 * @c: UBIFS file-system description object
513 * @bud: bud description object
515 * This function checks if bud @bud is the last bud in its journal head. This
516 * information is then used by 'replay_bud()' to decide whether the bud can
517 * have corruptions or not. Indeed, only last buds can be corrupted by power
518 * cuts. Returns %1 if this is the last bud, and %0 if not.
520 static int is_last_bud(struct ubifs_info *c, struct ubifs_bud *bud)
522 struct ubifs_jhead *jh = &c->jheads[bud->jhead];
523 struct ubifs_bud *next;
527 if (list_is_last(&bud->list, &jh->buds_list))
531 * The following is a quirk to make sure we work correctly with UBIFS
532 * images used with older UBIFS.
534 * Normally, the last bud will be the last in the journal head's list
535 * of bud. However, there is one exception if the UBIFS image belongs
536 * to older UBIFS. This is fairly unlikely: one would need to use old
537 * UBIFS, then have a power cut exactly at the right point, and then
538 * try to mount this image with new UBIFS.
540 * The exception is: it is possible to have 2 buds A and B, A goes
541 * before B, and B is the last, bud B is contains no data, and bud A is
542 * corrupted at the end. The reason is that in older versions when the
543 * journal code switched the next bud (from A to B), it first added a
544 * log reference node for the new bud (B), and only after this it
545 * synchronized the write-buffer of current bud (A). But later this was
546 * changed and UBIFS started to always synchronize the write-buffer of
547 * the bud (A) before writing the log reference for the new bud (B).
549 * But because older UBIFS always synchronized A's write-buffer before
550 * writing to B, we can recognize this exceptional situation but
551 * checking the contents of bud B - if it is empty, then A can be
552 * treated as the last and we can recover it.
554 * TODO: remove this piece of code in a couple of years (today it is
557 next = list_entry(bud->list.next, struct ubifs_bud, list);
558 if (!list_is_last(&next->list, &jh->buds_list))
561 err = ubifs_leb_read(c, next->lnum, (char *)&data, next->start, 4, 1);
565 return data == 0xFFFFFFFF;
569 * replay_bud - replay a bud logical eraseblock.
570 * @c: UBIFS file-system description object
571 * @b: bud entry which describes the bud
573 * This function replays bud @bud, recovers it if needed, and adds all nodes
574 * from this bud to the replay list. Returns zero in case of success and a
575 * negative error code in case of failure.
577 static int replay_bud(struct ubifs_info *c, struct bud_entry *b)
579 int is_last = is_last_bud(c, b->bud);
580 int err = 0, used = 0, lnum = b->bud->lnum, offs = b->bud->start;
581 struct ubifs_scan_leb *sleb;
582 struct ubifs_scan_node *snod;
584 dbg_mnt("replay bud LEB %d, head %d, offs %d, is_last %d",
585 lnum, b->bud->jhead, offs, is_last);
587 if (c->need_recovery && is_last)
589 * Recover only last LEBs in the journal heads, because power
590 * cuts may cause corruptions only in these LEBs, because only
591 * these LEBs could possibly be written to at the power cut
594 sleb = ubifs_recover_leb(c, lnum, offs, c->sbuf, b->bud->jhead);
596 sleb = ubifs_scan(c, lnum, offs, c->sbuf, 0);
598 return PTR_ERR(sleb);
601 * The bud does not have to start from offset zero - the beginning of
602 * the 'lnum' LEB may contain previously committed data. One of the
603 * things we have to do in replay is to correctly update lprops with
604 * newer information about this LEB.
606 * At this point lprops thinks that this LEB has 'c->leb_size - offs'
607 * bytes of free space because it only contain information about
610 * But we know that real amount of free space is 'c->leb_size -
611 * sleb->endpt', and the space in the 'lnum' LEB between 'offs' and
612 * 'sleb->endpt' is used by bud data. We have to correctly calculate
613 * how much of these data are dirty and update lprops with this
616 * The dirt in that LEB region is comprised of padding nodes, deletion
617 * nodes, truncation nodes and nodes which are obsoleted by subsequent
618 * nodes in this LEB. So instead of calculating clean space, we
619 * calculate used space ('used' variable).
622 list_for_each_entry(snod, &sleb->nodes, list) {
627 if (snod->sqnum >= SQNUM_WATERMARK) {
628 ubifs_err(c, "file system's life ended");
632 if (snod->sqnum > c->max_sqnum)
633 c->max_sqnum = snod->sqnum;
635 switch (snod->type) {
638 struct ubifs_ino_node *ino = snod->node;
639 loff_t new_size = le64_to_cpu(ino->size);
641 if (le32_to_cpu(ino->nlink) == 0)
643 err = insert_node(c, lnum, snod->offs, snod->len,
644 &snod->key, snod->sqnum, deletion,
648 case UBIFS_DATA_NODE:
650 struct ubifs_data_node *dn = snod->node;
651 loff_t new_size = le32_to_cpu(dn->size) +
652 key_block(c, &snod->key) *
655 err = insert_node(c, lnum, snod->offs, snod->len,
656 &snod->key, snod->sqnum, deletion,
660 case UBIFS_DENT_NODE:
661 case UBIFS_XENT_NODE:
663 struct ubifs_dent_node *dent = snod->node;
665 err = ubifs_validate_entry(c, dent);
669 err = insert_dent(c, lnum, snod->offs, snod->len,
670 &snod->key, dent->name,
671 le16_to_cpu(dent->nlen), snod->sqnum,
672 !le64_to_cpu(dent->inum), &used);
675 case UBIFS_TRUN_NODE:
677 struct ubifs_trun_node *trun = snod->node;
678 loff_t old_size = le64_to_cpu(trun->old_size);
679 loff_t new_size = le64_to_cpu(trun->new_size);
682 /* Validate truncation node */
683 if (old_size < 0 || old_size > c->max_inode_sz ||
684 new_size < 0 || new_size > c->max_inode_sz ||
685 old_size <= new_size) {
686 ubifs_err(c, "bad truncation node");
691 * Create a fake truncation key just to use the same
692 * functions which expect nodes to have keys.
694 trun_key_init(c, &key, le32_to_cpu(trun->inum));
695 err = insert_node(c, lnum, snod->offs, snod->len,
696 &key, snod->sqnum, 1, &used,
701 ubifs_err(c, "unexpected node type %d in bud LEB %d:%d",
702 snod->type, lnum, snod->offs);
710 ubifs_assert(c, ubifs_search_bud(c, lnum));
711 ubifs_assert(c, sleb->endpt - offs >= used);
712 ubifs_assert(c, sleb->endpt % c->min_io_size == 0);
714 b->dirty = sleb->endpt - offs - used;
715 b->free = c->leb_size - sleb->endpt;
716 dbg_mnt("bud LEB %d replied: dirty %d, free %d",
717 lnum, b->dirty, b->free);
720 ubifs_scan_destroy(sleb);
724 ubifs_err(c, "bad node is at LEB %d:%d", lnum, snod->offs);
725 ubifs_dump_node(c, snod->node);
726 ubifs_scan_destroy(sleb);
731 * replay_buds - replay all buds.
732 * @c: UBIFS file-system description object
734 * This function returns zero in case of success and a negative error code in
737 static int replay_buds(struct ubifs_info *c)
741 unsigned long long prev_sqnum = 0;
743 list_for_each_entry(b, &c->replay_buds, list) {
744 err = replay_bud(c, b);
748 ubifs_assert(c, b->sqnum > prev_sqnum);
749 prev_sqnum = b->sqnum;
756 * destroy_bud_list - destroy the list of buds to replay.
757 * @c: UBIFS file-system description object
759 static void destroy_bud_list(struct ubifs_info *c)
763 while (!list_empty(&c->replay_buds)) {
764 b = list_entry(c->replay_buds.next, struct bud_entry, list);
771 * add_replay_bud - add a bud to the list of buds to replay.
772 * @c: UBIFS file-system description object
773 * @lnum: bud logical eraseblock number to replay
774 * @offs: bud start offset
775 * @jhead: journal head to which this bud belongs
776 * @sqnum: reference node sequence number
778 * This function returns zero in case of success and a negative error code in
781 static int add_replay_bud(struct ubifs_info *c, int lnum, int offs, int jhead,
782 unsigned long long sqnum)
784 struct ubifs_bud *bud;
787 dbg_mnt("add replay bud LEB %d:%d, head %d", lnum, offs, jhead);
789 bud = kmalloc(sizeof(struct ubifs_bud), GFP_KERNEL);
793 b = kmalloc(sizeof(struct bud_entry), GFP_KERNEL);
802 ubifs_add_bud(c, bud);
806 list_add_tail(&b->list, &c->replay_buds);
812 * validate_ref - validate a reference node.
813 * @c: UBIFS file-system description object
814 * @ref: the reference node to validate
815 * @ref_lnum: LEB number of the reference node
816 * @ref_offs: reference node offset
818 * This function returns %1 if a bud reference already exists for the LEB. %0 is
819 * returned if the reference node is new, otherwise %-EINVAL is returned if
822 static int validate_ref(struct ubifs_info *c, const struct ubifs_ref_node *ref)
824 struct ubifs_bud *bud;
825 int lnum = le32_to_cpu(ref->lnum);
826 unsigned int offs = le32_to_cpu(ref->offs);
827 unsigned int jhead = le32_to_cpu(ref->jhead);
830 * ref->offs may point to the end of LEB when the journal head points
831 * to the end of LEB and we write reference node for it during commit.
832 * So this is why we require 'offs > c->leb_size'.
834 if (jhead >= c->jhead_cnt || lnum >= c->leb_cnt ||
835 lnum < c->main_first || offs > c->leb_size ||
836 offs & (c->min_io_size - 1))
839 /* Make sure we have not already looked at this bud */
840 bud = ubifs_search_bud(c, lnum);
842 if (bud->jhead == jhead && bud->start <= offs)
844 ubifs_err(c, "bud at LEB %d:%d was already referred", lnum, offs);
852 * replay_log_leb - replay a log logical eraseblock.
853 * @c: UBIFS file-system description object
854 * @lnum: log logical eraseblock to replay
855 * @offs: offset to start replaying from
858 * This function replays a log LEB and returns zero in case of success, %1 if
859 * this is the last LEB in the log, and a negative error code in case of
862 static int replay_log_leb(struct ubifs_info *c, int lnum, int offs, void *sbuf)
865 struct ubifs_scan_leb *sleb;
866 struct ubifs_scan_node *snod;
867 const struct ubifs_cs_node *node;
869 dbg_mnt("replay log LEB %d:%d", lnum, offs);
870 sleb = ubifs_scan(c, lnum, offs, sbuf, c->need_recovery);
872 if (PTR_ERR(sleb) != -EUCLEAN || !c->need_recovery)
873 return PTR_ERR(sleb);
875 * Note, the below function will recover this log LEB only if
876 * it is the last, because unclean reboots can possibly corrupt
877 * only the tail of the log.
879 sleb = ubifs_recover_log_leb(c, lnum, offs, sbuf);
881 return PTR_ERR(sleb);
884 if (sleb->nodes_cnt == 0) {
890 snod = list_entry(sleb->nodes.next, struct ubifs_scan_node, list);
891 if (c->cs_sqnum == 0) {
893 * This is the first log LEB we are looking at, make sure that
894 * the first node is a commit start node. Also record its
895 * sequence number so that UBIFS can determine where the log
896 * ends, because all nodes which were have higher sequence
899 if (snod->type != UBIFS_CS_NODE) {
900 ubifs_err(c, "first log node at LEB %d:%d is not CS node",
904 if (le64_to_cpu(node->cmt_no) != c->cmt_no) {
905 ubifs_err(c, "first CS node at LEB %d:%d has wrong commit number %llu expected %llu",
907 (unsigned long long)le64_to_cpu(node->cmt_no),
912 c->cs_sqnum = le64_to_cpu(node->ch.sqnum);
913 dbg_mnt("commit start sqnum %llu", c->cs_sqnum);
916 if (snod->sqnum < c->cs_sqnum) {
918 * This means that we reached end of log and now
919 * look to the older log data, which was already
920 * committed but the eraseblock was not erased (UBIFS
921 * only un-maps it). So this basically means we have to
922 * exit with "end of log" code.
928 /* Make sure the first node sits at offset zero of the LEB */
929 if (snod->offs != 0) {
930 ubifs_err(c, "first node is not at zero offset");
934 list_for_each_entry(snod, &sleb->nodes, list) {
937 if (snod->sqnum >= SQNUM_WATERMARK) {
938 ubifs_err(c, "file system's life ended");
942 if (snod->sqnum < c->cs_sqnum) {
943 ubifs_err(c, "bad sqnum %llu, commit sqnum %llu",
944 snod->sqnum, c->cs_sqnum);
948 if (snod->sqnum > c->max_sqnum)
949 c->max_sqnum = snod->sqnum;
951 switch (snod->type) {
952 case UBIFS_REF_NODE: {
953 const struct ubifs_ref_node *ref = snod->node;
955 err = validate_ref(c, ref);
957 break; /* Already have this bud */
961 err = add_replay_bud(c, le32_to_cpu(ref->lnum),
962 le32_to_cpu(ref->offs),
963 le32_to_cpu(ref->jhead),
971 /* Make sure it sits at the beginning of LEB */
972 if (snod->offs != 0) {
973 ubifs_err(c, "unexpected node in log");
978 ubifs_err(c, "unexpected node in log");
983 if (sleb->endpt || c->lhead_offs >= c->leb_size) {
984 c->lhead_lnum = lnum;
985 c->lhead_offs = sleb->endpt;
990 ubifs_scan_destroy(sleb);
994 ubifs_err(c, "log error detected while replaying the log at LEB %d:%d",
995 lnum, offs + snod->offs);
996 ubifs_dump_node(c, snod->node);
997 ubifs_scan_destroy(sleb);
1002 * take_ihead - update the status of the index head in lprops to 'taken'.
1003 * @c: UBIFS file-system description object
1005 * This function returns the amount of free space in the index head LEB or a
1006 * negative error code.
1008 static int take_ihead(struct ubifs_info *c)
1010 const struct ubifs_lprops *lp;
1013 ubifs_get_lprops(c);
1015 lp = ubifs_lpt_lookup_dirty(c, c->ihead_lnum);
1023 lp = ubifs_change_lp(c, lp, LPROPS_NC, LPROPS_NC,
1024 lp->flags | LPROPS_TAKEN, 0);
1032 ubifs_release_lprops(c);
1037 * ubifs_replay_journal - replay journal.
1038 * @c: UBIFS file-system description object
1040 * This function scans the journal, replays and cleans it up. It makes sure all
1041 * memory data structures related to uncommitted journal are built (dirty TNC
1042 * tree, tree of buds, modified lprops, etc).
1044 int ubifs_replay_journal(struct ubifs_info *c)
1046 int err, lnum, free;
1048 BUILD_BUG_ON(UBIFS_TRUN_KEY > 5);
1050 /* Update the status of the index head in lprops to 'taken' */
1051 free = take_ihead(c);
1053 return free; /* Error code */
1055 if (c->ihead_offs != c->leb_size - free) {
1056 ubifs_err(c, "bad index head LEB %d:%d", c->ihead_lnum,
1061 dbg_mnt("start replaying the journal");
1063 lnum = c->ltail_lnum = c->lhead_lnum;
1066 err = replay_log_leb(c, lnum, 0, c->sbuf);
1068 if (lnum != c->lhead_lnum)
1069 /* We hit the end of the log */
1073 * The head of the log must always start with the
1074 * "commit start" node on a properly formatted UBIFS.
1075 * But we found no nodes at all, which means that
1076 * something went wrong and we cannot proceed mounting
1079 ubifs_err(c, "no UBIFS nodes found at the log head LEB %d:%d, possibly corrupted",
1085 lnum = ubifs_next_log_lnum(c, lnum);
1086 } while (lnum != c->ltail_lnum);
1088 err = replay_buds(c);
1092 err = apply_replay_list(c);
1096 err = set_buds_lprops(c);
1101 * UBIFS budgeting calculations use @c->bi.uncommitted_idx variable
1102 * to roughly estimate index growth. Things like @c->bi.min_idx_lebs
1103 * depend on it. This means we have to initialize it to make sure
1104 * budgeting works properly.
1106 c->bi.uncommitted_idx = atomic_long_read(&c->dirty_zn_cnt);
1107 c->bi.uncommitted_idx *= c->max_idx_node_sz;
1109 ubifs_assert(c, c->bud_bytes <= c->max_bud_bytes || c->need_recovery);
1110 dbg_mnt("finished, log head LEB %d:%d, max_sqnum %llu, highest_inum %lu",
1111 c->lhead_lnum, c->lhead_offs, c->max_sqnum,
1112 (unsigned long)c->highest_inum);
1114 destroy_replay_list(c);
1115 destroy_bud_list(c);