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.
40 * REPLAY_DELETION: node was deleted
41 * REPLAY_REF: node is a reference node
49 * struct replay_entry - replay tree entry.
50 * @lnum: logical eraseblock number of the node
53 * @sqnum: node sequence number
54 * @flags: replay flags
55 * @rb: links the replay tree
57 * @nm: directory entry name
58 * @old_size: truncation old size
59 * @new_size: truncation new size
60 * @free: amount of free space in a bud
61 * @dirty: amount of dirty space in a bud from padding and deletion nodes
62 * @jhead: journal head number of the bud
64 * UBIFS journal replay must compare node sequence numbers, which means it must
65 * build a tree of node information to insert into the TNC.
71 unsigned long long sqnum;
90 * struct bud_entry - entry in the list of buds to replay.
91 * @list: next bud in the list
92 * @bud: bud description object
93 * @free: free bytes in the bud
94 * @sqnum: reference node sequence number
97 struct list_head list;
98 struct ubifs_bud *bud;
100 unsigned long long sqnum;
104 * set_bud_lprops - set free and dirty space used by a bud.
105 * @c: UBIFS file-system description object
106 * @r: replay entry of bud
108 static int set_bud_lprops(struct ubifs_info *c, struct replay_entry *r)
110 const struct ubifs_lprops *lp;
115 lp = ubifs_lpt_lookup_dirty(c, r->lnum);
122 if (r->offs == 0 && (lp->free != c->leb_size || lp->dirty != 0)) {
124 * The LEB was added to the journal with a starting offset of
125 * zero which means the LEB must have been empty. The LEB
126 * property values should be lp->free == c->leb_size and
127 * lp->dirty == 0, but that is not the case. The reason is that
128 * the LEB was garbage collected. The garbage collector resets
129 * the free and dirty space without recording it anywhere except
130 * lprops, so if there is not a commit then lprops does not have
131 * that information next time the file system is mounted.
133 * We do not need to adjust free space because the scan has told
134 * us the exact value which is recorded in the replay entry as
137 * However we do need to subtract from the dirty space the
138 * amount of space that the garbage collector reclaimed, which
139 * is the whole LEB minus the amount of space that was free.
141 dbg_mnt("bud LEB %d was GC'd (%d free, %d dirty)", r->lnum,
142 lp->free, lp->dirty);
143 dbg_gc("bud LEB %d was GC'd (%d free, %d dirty)", r->lnum,
144 lp->free, lp->dirty);
145 dirty -= c->leb_size - lp->free;
147 * If the replay order was perfect the dirty space would now be
148 * zero. The order is not perfect because the journal heads
149 * race with each other. This is not a problem but is does mean
150 * that the dirty space may temporarily exceed c->leb_size
154 dbg_msg("LEB %d lp: %d free %d dirty "
155 "replay: %d free %d dirty", r->lnum, lp->free,
156 lp->dirty, r->free, r->dirty);
158 lp = ubifs_change_lp(c, lp, r->free, dirty + r->dirty,
159 lp->flags | LPROPS_TAKEN, 0);
165 /* Make sure the journal head points to the latest bud */
166 err = ubifs_wbuf_seek_nolock(&c->jheads[r->jhead].wbuf, r->lnum,
167 c->leb_size - r->free, UBI_SHORTTERM);
170 ubifs_release_lprops(c);
175 * trun_remove_range - apply a replay entry for a truncation to the TNC.
176 * @c: UBIFS file-system description object
177 * @r: replay entry of truncation
179 static int trun_remove_range(struct ubifs_info *c, struct replay_entry *r)
181 unsigned min_blk, max_blk;
182 union ubifs_key min_key, max_key;
185 min_blk = r->new_size / UBIFS_BLOCK_SIZE;
186 if (r->new_size & (UBIFS_BLOCK_SIZE - 1))
189 max_blk = r->old_size / UBIFS_BLOCK_SIZE;
190 if ((r->old_size & (UBIFS_BLOCK_SIZE - 1)) == 0)
193 ino = key_inum(c, &r->key);
195 data_key_init(c, &min_key, ino, min_blk);
196 data_key_init(c, &max_key, ino, max_blk);
198 return ubifs_tnc_remove_range(c, &min_key, &max_key);
202 * apply_replay_entry - apply a replay entry to the TNC.
203 * @c: UBIFS file-system description object
204 * @r: replay entry to apply
206 * Apply a replay entry to the TNC.
208 static int apply_replay_entry(struct ubifs_info *c, struct replay_entry *r)
210 int err, deletion = ((r->flags & REPLAY_DELETION) != 0);
212 dbg_mnt("LEB %d:%d len %d flgs %d sqnum %llu %s", r->lnum,
213 r->offs, r->len, r->flags, r->sqnum, DBGKEY(&r->key));
215 /* Set c->replay_sqnum to help deal with dangling branches. */
216 c->replay_sqnum = r->sqnum;
218 if (r->flags & REPLAY_REF)
219 err = set_bud_lprops(c, r);
220 else if (is_hash_key(c, &r->key)) {
222 err = ubifs_tnc_remove_nm(c, &r->key, &r->nm);
224 err = ubifs_tnc_add_nm(c, &r->key, r->lnum, r->offs,
228 switch (key_type(c, &r->key)) {
231 ino_t inum = key_inum(c, &r->key);
233 err = ubifs_tnc_remove_ino(c, inum);
237 err = trun_remove_range(c, r);
240 err = ubifs_tnc_remove(c, &r->key);
244 err = ubifs_tnc_add(c, &r->key, r->lnum, r->offs,
249 if (c->need_recovery)
250 err = ubifs_recover_size_accum(c, &r->key, deletion,
258 * destroy_replay_tree - destroy the replay.
259 * @c: UBIFS file-system description object
261 * Destroy the replay tree.
263 static void destroy_replay_tree(struct ubifs_info *c)
265 struct rb_node *this = c->replay_tree.rb_node;
266 struct replay_entry *r;
270 this = this->rb_left;
272 } else if (this->rb_right) {
273 this = this->rb_right;
276 r = rb_entry(this, struct replay_entry, rb);
277 this = rb_parent(this);
279 if (this->rb_left == &r->rb)
280 this->rb_left = NULL;
282 this->rb_right = NULL;
284 if (is_hash_key(c, &r->key))
288 c->replay_tree = RB_ROOT;
292 * apply_replay_tree - apply the replay tree to the TNC.
293 * @c: UBIFS file-system description object
295 * Apply the replay tree.
296 * Returns zero in case of success and a negative error code in case of
299 static int apply_replay_tree(struct ubifs_info *c)
301 struct rb_node *this = rb_first(&c->replay_tree);
304 struct replay_entry *r;
309 r = rb_entry(this, struct replay_entry, rb);
310 err = apply_replay_entry(c, r);
313 this = rb_next(this);
319 * insert_node - insert a node to the replay tree.
320 * @c: UBIFS file-system description object
321 * @lnum: node logical eraseblock number
325 * @sqnum: sequence number
326 * @deletion: non-zero if this is a deletion
327 * @used: number of bytes in use in a LEB
328 * @old_size: truncation old size
329 * @new_size: truncation new size
331 * This function inserts a scanned non-direntry node to the replay tree. The
332 * replay tree is an RB-tree containing @struct replay_entry elements which are
333 * indexed by the sequence number. The replay tree is applied at the very end
334 * of the replay process. Since the tree is sorted in sequence number order,
335 * the older modifications are applied first. This function returns zero in
336 * case of success and a negative error code in case of failure.
338 static int insert_node(struct ubifs_info *c, int lnum, int offs, int len,
339 union ubifs_key *key, unsigned long long sqnum,
340 int deletion, int *used, loff_t old_size,
343 struct rb_node **p = &c->replay_tree.rb_node, *parent = NULL;
344 struct replay_entry *r;
346 if (key_inum(c, key) >= c->highest_inum)
347 c->highest_inum = key_inum(c, key);
349 dbg_mnt("add LEB %d:%d, key %s", lnum, offs, DBGKEY(key));
352 r = rb_entry(parent, struct replay_entry, rb);
353 if (sqnum < r->sqnum) {
356 } else if (sqnum > r->sqnum) {
360 ubifs_err("duplicate sqnum in replay");
364 r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL);
369 *used += ALIGN(len, 8);
374 r->flags = (deletion ? REPLAY_DELETION : 0);
375 r->old_size = old_size;
376 r->new_size = new_size;
377 key_copy(c, key, &r->key);
379 rb_link_node(&r->rb, parent, p);
380 rb_insert_color(&r->rb, &c->replay_tree);
385 * insert_dent - insert a directory entry node into the replay tree.
386 * @c: UBIFS file-system description object
387 * @lnum: node logical eraseblock number
391 * @name: directory entry name
392 * @nlen: directory entry name length
393 * @sqnum: sequence number
394 * @deletion: non-zero if this is a deletion
395 * @used: number of bytes in use in a LEB
397 * This function inserts a scanned directory entry node to the replay tree.
398 * Returns zero in case of success and a negative error code in case of
401 * This function is also used for extended attribute entries because they are
402 * implemented as directory entry nodes.
404 static int insert_dent(struct ubifs_info *c, int lnum, int offs, int len,
405 union ubifs_key *key, const char *name, int nlen,
406 unsigned long long sqnum, int deletion, int *used)
408 struct rb_node **p = &c->replay_tree.rb_node, *parent = NULL;
409 struct replay_entry *r;
412 if (key_inum(c, key) >= c->highest_inum)
413 c->highest_inum = key_inum(c, key);
415 dbg_mnt("add LEB %d:%d, key %s", lnum, offs, DBGKEY(key));
418 r = rb_entry(parent, struct replay_entry, rb);
419 if (sqnum < r->sqnum) {
423 if (sqnum > r->sqnum) {
427 ubifs_err("duplicate sqnum in replay");
431 r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL);
434 nbuf = kmalloc(nlen + 1, GFP_KERNEL);
441 *used += ALIGN(len, 8);
447 memcpy(nbuf, name, nlen);
450 r->flags = (deletion ? REPLAY_DELETION : 0);
451 key_copy(c, key, &r->key);
454 rb_link_node(&r->rb, parent, p);
455 rb_insert_color(&r->rb, &c->replay_tree);
460 * ubifs_validate_entry - validate directory or extended attribute entry node.
461 * @c: UBIFS file-system description object
462 * @dent: the node to validate
464 * This function validates directory or extended attribute entry node @dent.
465 * Returns zero if the node is all right and a %-EINVAL if not.
467 int ubifs_validate_entry(struct ubifs_info *c,
468 const struct ubifs_dent_node *dent)
470 int key_type = key_type_flash(c, dent->key);
471 int nlen = le16_to_cpu(dent->nlen);
473 if (le32_to_cpu(dent->ch.len) != nlen + UBIFS_DENT_NODE_SZ + 1 ||
474 dent->type >= UBIFS_ITYPES_CNT ||
475 nlen > UBIFS_MAX_NLEN || dent->name[nlen] != 0 ||
476 strnlen(dent->name, nlen) != nlen ||
477 le64_to_cpu(dent->inum) > MAX_INUM) {
478 ubifs_err("bad %s node", key_type == UBIFS_DENT_KEY ?
479 "directory entry" : "extended attribute entry");
483 if (key_type != UBIFS_DENT_KEY && key_type != UBIFS_XENT_KEY) {
484 ubifs_err("bad key type %d", key_type);
492 * replay_bud - replay a bud logical eraseblock.
493 * @c: UBIFS file-system description object
494 * @lnum: bud logical eraseblock number to replay
495 * @offs: bud start offset
496 * @jhead: journal head to which this bud belongs
497 * @free: amount of free space in the bud is returned here
498 * @dirty: amount of dirty space from padding and deletion nodes is returned
501 * This function returns zero in case of success and a negative error code in
504 static int replay_bud(struct ubifs_info *c, int lnum, int offs, int jhead,
505 int *free, int *dirty)
507 int err = 0, used = 0;
508 struct ubifs_scan_leb *sleb;
509 struct ubifs_scan_node *snod;
510 struct ubifs_bud *bud;
512 dbg_mnt("replay bud LEB %d, head %d, offs %d", lnum, jhead, offs);
513 if (c->need_recovery)
514 sleb = ubifs_recover_leb(c, lnum, offs, c->sbuf, jhead != GCHD);
516 sleb = ubifs_scan(c, lnum, offs, c->sbuf, 0);
518 return PTR_ERR(sleb);
521 * The bud does not have to start from offset zero - the beginning of
522 * the 'lnum' LEB may contain previously committed data. One of the
523 * things we have to do in replay is to correctly update lprops with
524 * newer information about this LEB.
526 * At this point lprops thinks that this LEB has 'c->leb_size - offs'
527 * bytes of free space because it only contain information about
530 * But we know that real amount of free space is 'c->leb_size -
531 * sleb->endpt', and the space in the 'lnum' LEB between 'offs' and
532 * 'sleb->endpt' is used by bud data. We have to correctly calculate
533 * how much of these data are dirty and update lprops with this
536 * The dirt in that LEB region is comprised of padding nodes, deletion
537 * nodes, truncation nodes and nodes which are obsoleted by subsequent
538 * nodes in this LEB. So instead of calculating clean space, we
539 * calculate used space ('used' variable).
542 list_for_each_entry(snod, &sleb->nodes, list) {
547 if (snod->sqnum >= SQNUM_WATERMARK) {
548 ubifs_err("file system's life ended");
552 if (snod->sqnum > c->max_sqnum)
553 c->max_sqnum = snod->sqnum;
555 switch (snod->type) {
558 struct ubifs_ino_node *ino = snod->node;
559 loff_t new_size = le64_to_cpu(ino->size);
561 if (le32_to_cpu(ino->nlink) == 0)
563 err = insert_node(c, lnum, snod->offs, snod->len,
564 &snod->key, snod->sqnum, deletion,
568 case UBIFS_DATA_NODE:
570 struct ubifs_data_node *dn = snod->node;
571 loff_t new_size = le32_to_cpu(dn->size) +
572 key_block(c, &snod->key) *
575 err = insert_node(c, lnum, snod->offs, snod->len,
576 &snod->key, snod->sqnum, deletion,
580 case UBIFS_DENT_NODE:
581 case UBIFS_XENT_NODE:
583 struct ubifs_dent_node *dent = snod->node;
585 err = ubifs_validate_entry(c, dent);
589 err = insert_dent(c, lnum, snod->offs, snod->len,
590 &snod->key, dent->name,
591 le16_to_cpu(dent->nlen), snod->sqnum,
592 !le64_to_cpu(dent->inum), &used);
595 case UBIFS_TRUN_NODE:
597 struct ubifs_trun_node *trun = snod->node;
598 loff_t old_size = le64_to_cpu(trun->old_size);
599 loff_t new_size = le64_to_cpu(trun->new_size);
602 /* Validate truncation node */
603 if (old_size < 0 || old_size > c->max_inode_sz ||
604 new_size < 0 || new_size > c->max_inode_sz ||
605 old_size <= new_size) {
606 ubifs_err("bad truncation node");
611 * Create a fake truncation key just to use the same
612 * functions which expect nodes to have keys.
614 trun_key_init(c, &key, le32_to_cpu(trun->inum));
615 err = insert_node(c, lnum, snod->offs, snod->len,
616 &key, snod->sqnum, 1, &used,
621 ubifs_err("unexpected node type %d in bud LEB %d:%d",
622 snod->type, lnum, snod->offs);
630 bud = ubifs_search_bud(c, lnum);
634 ubifs_assert(sleb->endpt - offs >= used);
635 ubifs_assert(sleb->endpt % c->min_io_size == 0);
637 *dirty = sleb->endpt - offs - used;
638 *free = c->leb_size - sleb->endpt;
639 dbg_mnt("bud LEB %d replied: dirty %d, free %d", lnum, *dirty, *free);
642 ubifs_scan_destroy(sleb);
646 ubifs_err("bad node is at LEB %d:%d", lnum, snod->offs);
647 dbg_dump_node(c, snod->node);
648 ubifs_scan_destroy(sleb);
653 * insert_ref_node - insert a reference node to the replay tree.
654 * @c: UBIFS file-system description object
655 * @lnum: node logical eraseblock number
657 * @sqnum: sequence number
658 * @free: amount of free space in bud
659 * @dirty: amount of dirty space from padding and deletion nodes
660 * @jhead: journal head number for the bud
662 * This function inserts a reference node to the replay tree and returns zero
663 * in case of success or a negative error code in case of failure.
665 static int insert_ref_node(struct ubifs_info *c, int lnum, int offs,
666 unsigned long long sqnum, int free, int dirty,
669 struct rb_node **p = &c->replay_tree.rb_node, *parent = NULL;
670 struct replay_entry *r;
672 dbg_mnt("add ref LEB %d:%d", lnum, offs);
675 r = rb_entry(parent, struct replay_entry, rb);
676 if (sqnum < r->sqnum) {
679 } else if (sqnum > r->sqnum) {
683 ubifs_err("duplicate sqnum in replay tree");
687 r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL);
694 r->flags = REPLAY_REF;
699 rb_link_node(&r->rb, parent, p);
700 rb_insert_color(&r->rb, &c->replay_tree);
705 * replay_buds - replay all buds.
706 * @c: UBIFS file-system description object
708 * This function returns zero in case of success and a negative error code in
711 static int replay_buds(struct ubifs_info *c)
714 int err, uninitialized_var(free), uninitialized_var(dirty);
716 list_for_each_entry(b, &c->replay_buds, list) {
717 err = replay_bud(c, b->bud->lnum, b->bud->start, b->bud->jhead,
721 err = insert_ref_node(c, b->bud->lnum, b->bud->start, b->sqnum,
722 free, dirty, b->bud->jhead);
731 * destroy_bud_list - destroy the list of buds to replay.
732 * @c: UBIFS file-system description object
734 static void destroy_bud_list(struct ubifs_info *c)
738 while (!list_empty(&c->replay_buds)) {
739 b = list_entry(c->replay_buds.next, struct bud_entry, list);
746 * add_replay_bud - add a bud to the list of buds to replay.
747 * @c: UBIFS file-system description object
748 * @lnum: bud logical eraseblock number to replay
749 * @offs: bud start offset
750 * @jhead: journal head to which this bud belongs
751 * @sqnum: reference node sequence number
753 * This function returns zero in case of success and a negative error code in
756 static int add_replay_bud(struct ubifs_info *c, int lnum, int offs, int jhead,
757 unsigned long long sqnum)
759 struct ubifs_bud *bud;
762 dbg_mnt("add replay bud LEB %d:%d, head %d", lnum, offs, jhead);
764 bud = kmalloc(sizeof(struct ubifs_bud), GFP_KERNEL);
768 b = kmalloc(sizeof(struct bud_entry), GFP_KERNEL);
777 ubifs_add_bud(c, bud);
781 list_add_tail(&b->list, &c->replay_buds);
787 * validate_ref - validate a reference node.
788 * @c: UBIFS file-system description object
789 * @ref: the reference node to validate
790 * @ref_lnum: LEB number of the reference node
791 * @ref_offs: reference node offset
793 * This function returns %1 if a bud reference already exists for the LEB. %0 is
794 * returned if the reference node is new, otherwise %-EINVAL is returned if
797 static int validate_ref(struct ubifs_info *c, const struct ubifs_ref_node *ref)
799 struct ubifs_bud *bud;
800 int lnum = le32_to_cpu(ref->lnum);
801 unsigned int offs = le32_to_cpu(ref->offs);
802 unsigned int jhead = le32_to_cpu(ref->jhead);
805 * ref->offs may point to the end of LEB when the journal head points
806 * to the end of LEB and we write reference node for it during commit.
807 * So this is why we require 'offs > c->leb_size'.
809 if (jhead >= c->jhead_cnt || lnum >= c->leb_cnt ||
810 lnum < c->main_first || offs > c->leb_size ||
811 offs & (c->min_io_size - 1))
814 /* Make sure we have not already looked at this bud */
815 bud = ubifs_search_bud(c, lnum);
817 if (bud->jhead == jhead && bud->start <= offs)
819 ubifs_err("bud at LEB %d:%d was already referred", lnum, offs);
827 * replay_log_leb - replay a log logical eraseblock.
828 * @c: UBIFS file-system description object
829 * @lnum: log logical eraseblock to replay
830 * @offs: offset to start replaying from
833 * This function replays a log LEB and returns zero in case of success, %1 if
834 * this is the last LEB in the log, and a negative error code in case of
837 static int replay_log_leb(struct ubifs_info *c, int lnum, int offs, void *sbuf)
840 struct ubifs_scan_leb *sleb;
841 struct ubifs_scan_node *snod;
842 const struct ubifs_cs_node *node;
844 dbg_mnt("replay log LEB %d:%d", lnum, offs);
845 sleb = ubifs_scan(c, lnum, offs, sbuf, c->need_recovery);
847 if (PTR_ERR(sleb) != -EUCLEAN || !c->need_recovery)
848 return PTR_ERR(sleb);
850 * Note, the below function will recover this log LEB only if
851 * it is the last, because unclean reboots can possibly corrupt
852 * only the tail of the log.
854 sleb = ubifs_recover_log_leb(c, lnum, offs, sbuf);
856 return PTR_ERR(sleb);
859 if (sleb->nodes_cnt == 0) {
865 snod = list_entry(sleb->nodes.next, struct ubifs_scan_node, list);
866 if (c->cs_sqnum == 0) {
868 * This is the first log LEB we are looking at, make sure that
869 * the first node is a commit start node. Also record its
870 * sequence number so that UBIFS can determine where the log
871 * ends, because all nodes which were have higher sequence
874 if (snod->type != UBIFS_CS_NODE) {
875 dbg_err("first log node at LEB %d:%d is not CS node",
879 if (le64_to_cpu(node->cmt_no) != c->cmt_no) {
880 dbg_err("first CS node at LEB %d:%d has wrong "
881 "commit number %llu expected %llu",
883 (unsigned long long)le64_to_cpu(node->cmt_no),
888 c->cs_sqnum = le64_to_cpu(node->ch.sqnum);
889 dbg_mnt("commit start sqnum %llu", c->cs_sqnum);
892 if (snod->sqnum < c->cs_sqnum) {
894 * This means that we reached end of log and now
895 * look to the older log data, which was already
896 * committed but the eraseblock was not erased (UBIFS
897 * only un-maps it). So this basically means we have to
898 * exit with "end of log" code.
904 /* Make sure the first node sits at offset zero of the LEB */
905 if (snod->offs != 0) {
906 dbg_err("first node is not at zero offset");
910 list_for_each_entry(snod, &sleb->nodes, list) {
913 if (snod->sqnum >= SQNUM_WATERMARK) {
914 ubifs_err("file system's life ended");
918 if (snod->sqnum < c->cs_sqnum) {
919 dbg_err("bad sqnum %llu, commit sqnum %llu",
920 snod->sqnum, c->cs_sqnum);
924 if (snod->sqnum > c->max_sqnum)
925 c->max_sqnum = snod->sqnum;
927 switch (snod->type) {
928 case UBIFS_REF_NODE: {
929 const struct ubifs_ref_node *ref = snod->node;
931 err = validate_ref(c, ref);
933 break; /* Already have this bud */
937 err = add_replay_bud(c, le32_to_cpu(ref->lnum),
938 le32_to_cpu(ref->offs),
939 le32_to_cpu(ref->jhead),
947 /* Make sure it sits at the beginning of LEB */
948 if (snod->offs != 0) {
949 ubifs_err("unexpected node in log");
954 ubifs_err("unexpected node in log");
959 if (sleb->endpt || c->lhead_offs >= c->leb_size) {
960 c->lhead_lnum = lnum;
961 c->lhead_offs = sleb->endpt;
966 ubifs_scan_destroy(sleb);
970 ubifs_err("log error detected while replaying the log at LEB %d:%d",
971 lnum, offs + snod->offs);
972 dbg_dump_node(c, snod->node);
973 ubifs_scan_destroy(sleb);
978 * take_ihead - update the status of the index head in lprops to 'taken'.
979 * @c: UBIFS file-system description object
981 * This function returns the amount of free space in the index head LEB or a
982 * negative error code.
984 static int take_ihead(struct ubifs_info *c)
986 const struct ubifs_lprops *lp;
991 lp = ubifs_lpt_lookup_dirty(c, c->ihead_lnum);
999 lp = ubifs_change_lp(c, lp, LPROPS_NC, LPROPS_NC,
1000 lp->flags | LPROPS_TAKEN, 0);
1008 ubifs_release_lprops(c);
1013 * ubifs_replay_journal - replay journal.
1014 * @c: UBIFS file-system description object
1016 * This function scans the journal, replays and cleans it up. It makes sure all
1017 * memory data structures related to uncommitted journal are built (dirty TNC
1018 * tree, tree of buds, modified lprops, etc).
1020 int ubifs_replay_journal(struct ubifs_info *c)
1022 int err, i, lnum, offs, free;
1024 BUILD_BUG_ON(UBIFS_TRUN_KEY > 5);
1026 /* Update the status of the index head in lprops to 'taken' */
1027 free = take_ihead(c);
1029 return free; /* Error code */
1031 if (c->ihead_offs != c->leb_size - free) {
1032 ubifs_err("bad index head LEB %d:%d", c->ihead_lnum,
1037 dbg_mnt("start replaying the journal");
1039 lnum = c->ltail_lnum = c->lhead_lnum;
1040 offs = c->lhead_offs;
1042 for (i = 0; i < c->log_lebs; i++, lnum++) {
1043 if (lnum >= UBIFS_LOG_LNUM + c->log_lebs) {
1045 * The log is logically circular, we reached the last
1046 * LEB, switch to the first one.
1048 lnum = UBIFS_LOG_LNUM;
1051 err = replay_log_leb(c, lnum, offs, c->sbuf);
1053 /* We hit the end of the log */
1060 err = replay_buds(c);
1064 err = apply_replay_tree(c);
1069 * UBIFS budgeting calculations use @c->bi.uncommitted_idx variable
1070 * to roughly estimate index growth. Things like @c->bi.min_idx_lebs
1071 * depend on it. This means we have to initialize it to make sure
1072 * budgeting works properly.
1074 c->bi.uncommitted_idx = atomic_long_read(&c->dirty_zn_cnt);
1075 c->bi.uncommitted_idx *= c->max_idx_node_sz;
1077 ubifs_assert(c->bud_bytes <= c->max_bud_bytes || c->need_recovery);
1078 dbg_mnt("finished, log head LEB %d:%d, max_sqnum %llu, "
1079 "highest_inum %lu", c->lhead_lnum, c->lhead_offs, c->max_sqnum,
1080 (unsigned long)c->highest_inum);
1082 destroy_replay_tree(c);
1083 destroy_bud_list(c);