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", lnum
, jhead
);
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
;
641 ubifs_scan_destroy(sleb
);
645 ubifs_err("bad node is at LEB %d:%d", lnum
, snod
->offs
);
646 dbg_dump_node(c
, snod
->node
);
647 ubifs_scan_destroy(sleb
);
652 * insert_ref_node - insert a reference node to the replay tree.
653 * @c: UBIFS file-system description object
654 * @lnum: node logical eraseblock number
656 * @sqnum: sequence number
657 * @free: amount of free space in bud
658 * @dirty: amount of dirty space from padding and deletion nodes
659 * @jhead: journal head number for the bud
661 * This function inserts a reference node to the replay tree and returns zero
662 * in case of success or a negative error code in case of failure.
664 static int insert_ref_node(struct ubifs_info
*c
, int lnum
, int offs
,
665 unsigned long long sqnum
, int free
, int dirty
,
668 struct rb_node
**p
= &c
->replay_tree
.rb_node
, *parent
= NULL
;
669 struct replay_entry
*r
;
671 dbg_mnt("add ref LEB %d:%d", lnum
, offs
);
674 r
= rb_entry(parent
, struct replay_entry
, rb
);
675 if (sqnum
< r
->sqnum
) {
678 } else if (sqnum
> r
->sqnum
) {
682 ubifs_err("duplicate sqnum in replay tree");
686 r
= kzalloc(sizeof(struct replay_entry
), GFP_KERNEL
);
693 r
->flags
= REPLAY_REF
;
698 rb_link_node(&r
->rb
, parent
, p
);
699 rb_insert_color(&r
->rb
, &c
->replay_tree
);
704 * replay_buds - replay all buds.
705 * @c: UBIFS file-system description object
707 * This function returns zero in case of success and a negative error code in
710 static int replay_buds(struct ubifs_info
*c
)
713 int err
, uninitialized_var(free
), uninitialized_var(dirty
);
715 list_for_each_entry(b
, &c
->replay_buds
, list
) {
716 err
= replay_bud(c
, b
->bud
->lnum
, b
->bud
->start
, b
->bud
->jhead
,
720 err
= insert_ref_node(c
, b
->bud
->lnum
, b
->bud
->start
, b
->sqnum
,
721 free
, dirty
, b
->bud
->jhead
);
730 * destroy_bud_list - destroy the list of buds to replay.
731 * @c: UBIFS file-system description object
733 static void destroy_bud_list(struct ubifs_info
*c
)
737 while (!list_empty(&c
->replay_buds
)) {
738 b
= list_entry(c
->replay_buds
.next
, struct bud_entry
, list
);
745 * add_replay_bud - add a bud to the list of buds to replay.
746 * @c: UBIFS file-system description object
747 * @lnum: bud logical eraseblock number to replay
748 * @offs: bud start offset
749 * @jhead: journal head to which this bud belongs
750 * @sqnum: reference node sequence number
752 * This function returns zero in case of success and a negative error code in
755 static int add_replay_bud(struct ubifs_info
*c
, int lnum
, int offs
, int jhead
,
756 unsigned long long sqnum
)
758 struct ubifs_bud
*bud
;
761 dbg_mnt("add replay bud LEB %d:%d, head %d", lnum
, offs
, jhead
);
763 bud
= kmalloc(sizeof(struct ubifs_bud
), GFP_KERNEL
);
767 b
= kmalloc(sizeof(struct bud_entry
), GFP_KERNEL
);
776 ubifs_add_bud(c
, bud
);
780 list_add_tail(&b
->list
, &c
->replay_buds
);
786 * validate_ref - validate a reference node.
787 * @c: UBIFS file-system description object
788 * @ref: the reference node to validate
789 * @ref_lnum: LEB number of the reference node
790 * @ref_offs: reference node offset
792 * This function returns %1 if a bud reference already exists for the LEB. %0 is
793 * returned if the reference node is new, otherwise %-EINVAL is returned if
796 static int validate_ref(struct ubifs_info
*c
, const struct ubifs_ref_node
*ref
)
798 struct ubifs_bud
*bud
;
799 int lnum
= le32_to_cpu(ref
->lnum
);
800 unsigned int offs
= le32_to_cpu(ref
->offs
);
801 unsigned int jhead
= le32_to_cpu(ref
->jhead
);
804 * ref->offs may point to the end of LEB when the journal head points
805 * to the end of LEB and we write reference node for it during commit.
806 * So this is why we require 'offs > c->leb_size'.
808 if (jhead
>= c
->jhead_cnt
|| lnum
>= c
->leb_cnt
||
809 lnum
< c
->main_first
|| offs
> c
->leb_size
||
810 offs
& (c
->min_io_size
- 1))
813 /* Make sure we have not already looked at this bud */
814 bud
= ubifs_search_bud(c
, lnum
);
816 if (bud
->jhead
== jhead
&& bud
->start
<= offs
)
818 ubifs_err("bud at LEB %d:%d was already referred", lnum
, offs
);
826 * replay_log_leb - replay a log logical eraseblock.
827 * @c: UBIFS file-system description object
828 * @lnum: log logical eraseblock to replay
829 * @offs: offset to start replaying from
832 * This function replays a log LEB and returns zero in case of success, %1 if
833 * this is the last LEB in the log, and a negative error code in case of
836 static int replay_log_leb(struct ubifs_info
*c
, int lnum
, int offs
, void *sbuf
)
839 struct ubifs_scan_leb
*sleb
;
840 struct ubifs_scan_node
*snod
;
841 const struct ubifs_cs_node
*node
;
843 dbg_mnt("replay log LEB %d:%d", lnum
, offs
);
844 sleb
= ubifs_scan(c
, lnum
, offs
, sbuf
, c
->need_recovery
);
846 if (PTR_ERR(sleb
) != -EUCLEAN
|| !c
->need_recovery
)
847 return PTR_ERR(sleb
);
849 * Note, the below function will recover this log LEB only if
850 * it is the last, because unclean reboots can possibly corrupt
851 * only the tail of the log.
853 sleb
= ubifs_recover_log_leb(c
, lnum
, offs
, sbuf
);
855 return PTR_ERR(sleb
);
858 if (sleb
->nodes_cnt
== 0) {
864 snod
= list_entry(sleb
->nodes
.next
, struct ubifs_scan_node
, list
);
865 if (c
->cs_sqnum
== 0) {
867 * This is the first log LEB we are looking at, make sure that
868 * the first node is a commit start node. Also record its
869 * sequence number so that UBIFS can determine where the log
870 * ends, because all nodes which were have higher sequence
873 if (snod
->type
!= UBIFS_CS_NODE
) {
874 dbg_err("first log node at LEB %d:%d is not CS node",
878 if (le64_to_cpu(node
->cmt_no
) != c
->cmt_no
) {
879 dbg_err("first CS node at LEB %d:%d has wrong "
880 "commit number %llu expected %llu",
882 (unsigned long long)le64_to_cpu(node
->cmt_no
),
887 c
->cs_sqnum
= le64_to_cpu(node
->ch
.sqnum
);
888 dbg_mnt("commit start sqnum %llu", c
->cs_sqnum
);
891 if (snod
->sqnum
< c
->cs_sqnum
) {
893 * This means that we reached end of log and now
894 * look to the older log data, which was already
895 * committed but the eraseblock was not erased (UBIFS
896 * only un-maps it). So this basically means we have to
897 * exit with "end of log" code.
903 /* Make sure the first node sits at offset zero of the LEB */
904 if (snod
->offs
!= 0) {
905 dbg_err("first node is not at zero offset");
909 list_for_each_entry(snod
, &sleb
->nodes
, list
) {
912 if (snod
->sqnum
>= SQNUM_WATERMARK
) {
913 ubifs_err("file system's life ended");
917 if (snod
->sqnum
< c
->cs_sqnum
) {
918 dbg_err("bad sqnum %llu, commit sqnum %llu",
919 snod
->sqnum
, c
->cs_sqnum
);
923 if (snod
->sqnum
> c
->max_sqnum
)
924 c
->max_sqnum
= snod
->sqnum
;
926 switch (snod
->type
) {
927 case UBIFS_REF_NODE
: {
928 const struct ubifs_ref_node
*ref
= snod
->node
;
930 err
= validate_ref(c
, ref
);
932 break; /* Already have this bud */
936 err
= add_replay_bud(c
, le32_to_cpu(ref
->lnum
),
937 le32_to_cpu(ref
->offs
),
938 le32_to_cpu(ref
->jhead
),
946 /* Make sure it sits at the beginning of LEB */
947 if (snod
->offs
!= 0) {
948 ubifs_err("unexpected node in log");
953 ubifs_err("unexpected node in log");
958 if (sleb
->endpt
|| c
->lhead_offs
>= c
->leb_size
) {
959 c
->lhead_lnum
= lnum
;
960 c
->lhead_offs
= sleb
->endpt
;
965 ubifs_scan_destroy(sleb
);
969 ubifs_err("log error detected while replaying the log at LEB %d:%d",
970 lnum
, offs
+ snod
->offs
);
971 dbg_dump_node(c
, snod
->node
);
972 ubifs_scan_destroy(sleb
);
977 * take_ihead - update the status of the index head in lprops to 'taken'.
978 * @c: UBIFS file-system description object
980 * This function returns the amount of free space in the index head LEB or a
981 * negative error code.
983 static int take_ihead(struct ubifs_info
*c
)
985 const struct ubifs_lprops
*lp
;
990 lp
= ubifs_lpt_lookup_dirty(c
, c
->ihead_lnum
);
998 lp
= ubifs_change_lp(c
, lp
, LPROPS_NC
, LPROPS_NC
,
999 lp
->flags
| LPROPS_TAKEN
, 0);
1007 ubifs_release_lprops(c
);
1012 * ubifs_replay_journal - replay journal.
1013 * @c: UBIFS file-system description object
1015 * This function scans the journal, replays and cleans it up. It makes sure all
1016 * memory data structures related to uncommitted journal are built (dirty TNC
1017 * tree, tree of buds, modified lprops, etc).
1019 int ubifs_replay_journal(struct ubifs_info
*c
)
1021 int err
, i
, lnum
, offs
, free
;
1023 BUILD_BUG_ON(UBIFS_TRUN_KEY
> 5);
1025 /* Update the status of the index head in lprops to 'taken' */
1026 free
= take_ihead(c
);
1028 return free
; /* Error code */
1030 if (c
->ihead_offs
!= c
->leb_size
- free
) {
1031 ubifs_err("bad index head LEB %d:%d", c
->ihead_lnum
,
1036 dbg_mnt("start replaying the journal");
1038 lnum
= c
->ltail_lnum
= c
->lhead_lnum
;
1039 offs
= c
->lhead_offs
;
1041 for (i
= 0; i
< c
->log_lebs
; i
++, lnum
++) {
1042 if (lnum
>= UBIFS_LOG_LNUM
+ c
->log_lebs
) {
1044 * The log is logically circular, we reached the last
1045 * LEB, switch to the first one.
1047 lnum
= UBIFS_LOG_LNUM
;
1050 err
= replay_log_leb(c
, lnum
, offs
, c
->sbuf
);
1052 /* We hit the end of the log */
1059 err
= replay_buds(c
);
1063 err
= apply_replay_tree(c
);
1068 * UBIFS budgeting calculations use @c->budg_uncommitted_idx variable
1069 * to roughly estimate index growth. Things like @c->min_idx_lebs
1070 * depend on it. This means we have to initialize it to make sure
1071 * budgeting works properly.
1073 c
->budg_uncommitted_idx
= atomic_long_read(&c
->dirty_zn_cnt
);
1074 c
->budg_uncommitted_idx
*= c
->max_idx_node_sz
;
1076 ubifs_assert(c
->bud_bytes
<= c
->max_bud_bytes
|| c
->need_recovery
);
1077 dbg_mnt("finished, log head LEB %d:%d, max_sqnum %llu, "
1078 "highest_inum %lu", c
->lhead_lnum
, c
->lhead_offs
, c
->max_sqnum
,
1079 (unsigned long)c
->highest_inum
);
1081 destroy_replay_tree(c
);
1082 destroy_bud_list(c
);