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
;
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_msg("LEB %d lp: %d free %d dirty "
145 "replay: %d free %d dirty", b
->bud
->lnum
,
146 lp
->free
, lp
->dirty
, b
->free
, b
->dirty
);
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
,
161 ubifs_release_lprops(c
);
166 * set_buds_lprops - set free and dirty space for all replayed buds.
167 * @c: UBIFS file-system description object
169 * This function sets LEB properties for all replayed buds. Returns zero in
170 * case of success and a negative error code in case of failure.
172 static int set_buds_lprops(struct ubifs_info
*c
)
177 list_for_each_entry(b
, &c
->replay_buds
, list
) {
178 err
= set_bud_lprops(c
, b
);
187 * trun_remove_range - apply a replay entry for a truncation to the TNC.
188 * @c: UBIFS file-system description object
189 * @r: replay entry of truncation
191 static int trun_remove_range(struct ubifs_info
*c
, struct replay_entry
*r
)
193 unsigned min_blk
, max_blk
;
194 union ubifs_key min_key
, max_key
;
197 min_blk
= r
->new_size
/ UBIFS_BLOCK_SIZE
;
198 if (r
->new_size
& (UBIFS_BLOCK_SIZE
- 1))
201 max_blk
= r
->old_size
/ UBIFS_BLOCK_SIZE
;
202 if ((r
->old_size
& (UBIFS_BLOCK_SIZE
- 1)) == 0)
205 ino
= key_inum(c
, &r
->key
);
207 data_key_init(c
, &min_key
, ino
, min_blk
);
208 data_key_init(c
, &max_key
, ino
, max_blk
);
210 return ubifs_tnc_remove_range(c
, &min_key
, &max_key
);
214 * apply_replay_entry - apply a replay entry to the TNC.
215 * @c: UBIFS file-system description object
216 * @r: replay entry to apply
218 * Apply a replay entry to the TNC.
220 static int apply_replay_entry(struct ubifs_info
*c
, struct replay_entry
*r
)
224 dbg_mnt("LEB %d:%d len %d deletion %d sqnum %llu %s", r
->lnum
,
225 r
->offs
, r
->len
, r
->deletion
, r
->sqnum
, DBGKEY(&r
->key
));
227 /* Set c->replay_sqnum to help deal with dangling branches. */
228 c
->replay_sqnum
= r
->sqnum
;
230 if (is_hash_key(c
, &r
->key
)) {
232 err
= ubifs_tnc_remove_nm(c
, &r
->key
, &r
->nm
);
234 err
= ubifs_tnc_add_nm(c
, &r
->key
, r
->lnum
, r
->offs
,
238 switch (key_type(c
, &r
->key
)) {
241 ino_t inum
= key_inum(c
, &r
->key
);
243 err
= ubifs_tnc_remove_ino(c
, inum
);
247 err
= trun_remove_range(c
, r
);
250 err
= ubifs_tnc_remove(c
, &r
->key
);
254 err
= ubifs_tnc_add(c
, &r
->key
, r
->lnum
, r
->offs
,
259 if (c
->need_recovery
)
260 err
= ubifs_recover_size_accum(c
, &r
->key
, r
->deletion
,
268 * replay_entries_cmp - compare 2 replay entries.
269 * @priv: UBIFS file-system description object
270 * @a: first replay entry
271 * @a: second replay entry
273 * This is a comparios function for 'list_sort()' which compares 2 replay
274 * entries @a and @b by comparing their sequence numer. Returns %1 if @a has
275 * greater sequence number and %-1 otherwise.
277 static int replay_entries_cmp(void *priv
, struct list_head
*a
,
280 struct replay_entry
*ra
, *rb
;
286 ra
= list_entry(a
, struct replay_entry
, list
);
287 rb
= list_entry(b
, struct replay_entry
, list
);
288 ubifs_assert(ra
->sqnum
!= rb
->sqnum
);
289 if (ra
->sqnum
> rb
->sqnum
)
295 * apply_replay_list - apply the replay list to the TNC.
296 * @c: UBIFS file-system description object
298 * Apply all entries in the replay list to the TNC. Returns zero in case of
299 * success and a negative error code in case of failure.
301 static int apply_replay_list(struct ubifs_info
*c
)
303 struct replay_entry
*r
;
306 list_sort(c
, &c
->replay_list
, &replay_entries_cmp
);
308 list_for_each_entry(r
, &c
->replay_list
, list
) {
311 err
= apply_replay_entry(c
, r
);
320 * destroy_replay_list - destroy the replay.
321 * @c: UBIFS file-system description object
323 * Destroy the replay list.
325 static void destroy_replay_list(struct ubifs_info
*c
)
327 struct replay_entry
*r
, *tmp
;
329 list_for_each_entry_safe(r
, tmp
, &c
->replay_list
, list
) {
330 if (is_hash_key(c
, &r
->key
))
338 * insert_node - insert a node to the replay list
339 * @c: UBIFS file-system description object
340 * @lnum: node logical eraseblock number
344 * @sqnum: sequence number
345 * @deletion: non-zero if this is a deletion
346 * @used: number of bytes in use in a LEB
347 * @old_size: truncation old size
348 * @new_size: truncation new size
350 * This function inserts a scanned non-direntry node to the replay list. The
351 * replay list contains @struct replay_entry elements, and we sort this list in
352 * sequence number order before applying it. The replay list is applied at the
353 * very end of the replay process. Since the list is sorted in sequence number
354 * order, the older modifications are applied first. This function returns zero
355 * in case of success and a negative error code in case of failure.
357 static int insert_node(struct ubifs_info
*c
, int lnum
, int offs
, int len
,
358 union ubifs_key
*key
, unsigned long long sqnum
,
359 int deletion
, int *used
, loff_t old_size
,
362 struct replay_entry
*r
;
364 dbg_mnt("add LEB %d:%d, key %s", lnum
, offs
, DBGKEY(key
));
366 if (key_inum(c
, key
) >= c
->highest_inum
)
367 c
->highest_inum
= key_inum(c
, key
);
369 r
= kzalloc(sizeof(struct replay_entry
), GFP_KERNEL
);
374 *used
+= ALIGN(len
, 8);
378 r
->deletion
= !!deletion
;
380 key_copy(c
, key
, &r
->key
);
381 r
->old_size
= old_size
;
382 r
->new_size
= new_size
;
384 list_add_tail(&r
->list
, &c
->replay_list
);
389 * insert_dent - insert a directory entry node into the replay list.
390 * @c: UBIFS file-system description object
391 * @lnum: node logical eraseblock number
395 * @name: directory entry name
396 * @nlen: directory entry name length
397 * @sqnum: sequence number
398 * @deletion: non-zero if this is a deletion
399 * @used: number of bytes in use in a LEB
401 * This function inserts a scanned directory entry node or an extended
402 * attribute entry to the replay list. Returns zero in case of success and a
403 * negative error code in case of failure.
405 static int insert_dent(struct ubifs_info
*c
, int lnum
, int offs
, int len
,
406 union ubifs_key
*key
, const char *name
, int nlen
,
407 unsigned long long sqnum
, int deletion
, int *used
)
409 struct replay_entry
*r
;
412 dbg_mnt("add LEB %d:%d, key %s", lnum
, offs
, DBGKEY(key
));
413 if (key_inum(c
, key
) >= c
->highest_inum
)
414 c
->highest_inum
= key_inum(c
, key
);
416 r
= kzalloc(sizeof(struct replay_entry
), GFP_KERNEL
);
420 nbuf
= kmalloc(nlen
+ 1, GFP_KERNEL
);
427 *used
+= ALIGN(len
, 8);
431 r
->deletion
= !!deletion
;
433 key_copy(c
, key
, &r
->key
);
435 memcpy(nbuf
, name
, nlen
);
439 list_add_tail(&r
->list
, &c
->replay_list
);
444 * ubifs_validate_entry - validate directory or extended attribute entry node.
445 * @c: UBIFS file-system description object
446 * @dent: the node to validate
448 * This function validates directory or extended attribute entry node @dent.
449 * Returns zero if the node is all right and a %-EINVAL if not.
451 int ubifs_validate_entry(struct ubifs_info
*c
,
452 const struct ubifs_dent_node
*dent
)
454 int key_type
= key_type_flash(c
, dent
->key
);
455 int nlen
= le16_to_cpu(dent
->nlen
);
457 if (le32_to_cpu(dent
->ch
.len
) != nlen
+ UBIFS_DENT_NODE_SZ
+ 1 ||
458 dent
->type
>= UBIFS_ITYPES_CNT
||
459 nlen
> UBIFS_MAX_NLEN
|| dent
->name
[nlen
] != 0 ||
460 strnlen(dent
->name
, nlen
) != nlen
||
461 le64_to_cpu(dent
->inum
) > MAX_INUM
) {
462 ubifs_err("bad %s node", key_type
== UBIFS_DENT_KEY
?
463 "directory entry" : "extended attribute entry");
467 if (key_type
!= UBIFS_DENT_KEY
&& key_type
!= UBIFS_XENT_KEY
) {
468 ubifs_err("bad key type %d", key_type
);
476 * is_last_bud - check if the bud is the last in the journal head.
477 * @c: UBIFS file-system description object
478 * @bud: bud description object
480 * This function checks if bud @bud is the last bud in its journal head. This
481 * information is then used by 'replay_bud()' to decide whether the bud can
482 * have corruptions or not. Indeed, only last buds can be corrupted by power
483 * cuts. Returns %1 if this is the last bud, and %0 if not.
485 static int is_last_bud(struct ubifs_info
*c
, struct ubifs_bud
*bud
)
487 struct ubifs_jhead
*jh
= &c
->jheads
[bud
->jhead
];
488 struct ubifs_bud
*next
;
492 if (list_is_last(&bud
->list
, &jh
->buds_list
))
496 * The following is a quirk to make sure we work correctly with UBIFS
497 * images used with older UBIFS.
499 * Normally, the last bud will be the last in the journal head's list
500 * of bud. However, there is one exception if the UBIFS image belongs
501 * to older UBIFS. This is fairly unlikely: one would need to use old
502 * UBIFS, then have a power cut exactly at the right point, and then
503 * try to mount this image with new UBIFS.
505 * The exception is: it is possible to have 2 buds A and B, A goes
506 * before B, and B is the last, bud B is contains no data, and bud A is
507 * corrupted at the end. The reason is that in older versions when the
508 * journal code switched the next bud (from A to B), it first added a
509 * log reference node for the new bud (B), and only after this it
510 * synchronized the write-buffer of current bud (A). But later this was
511 * changed and UBIFS started to always synchronize the write-buffer of
512 * the bud (A) before writing the log reference for the new bud (B).
514 * But because older UBIFS always synchronized A's write-buffer before
515 * writing to B, we can recognize this exceptional situation but
516 * checking the contents of bud B - if it is empty, then A can be
517 * treated as the last and we can recover it.
519 * TODO: remove this piece of code in a couple of years (today it is
522 next
= list_entry(bud
->list
.next
, struct ubifs_bud
, list
);
523 if (!list_is_last(&next
->list
, &jh
->buds_list
))
526 err
= ubi_read(c
->ubi
, next
->lnum
, (char *)&data
,
531 return data
== 0xFFFFFFFF;
535 * replay_bud - replay a bud logical eraseblock.
536 * @c: UBIFS file-system description object
537 * @b: bud entry which describes the bud
539 * This function replays bud @bud, recovers it if needed, and adds all nodes
540 * from this bud to the replay list. Returns zero in case of success and a
541 * negative error code in case of failure.
543 static int replay_bud(struct ubifs_info
*c
, struct bud_entry
*b
)
545 int is_last
= is_last_bud(c
, b
->bud
);
546 int err
= 0, used
= 0, lnum
= b
->bud
->lnum
, offs
= b
->bud
->start
;
547 struct ubifs_scan_leb
*sleb
;
548 struct ubifs_scan_node
*snod
;
550 dbg_mnt("replay bud LEB %d, head %d, offs %d, is_last %d",
551 lnum
, b
->bud
->jhead
, offs
, is_last
);
553 if (c
->need_recovery
&& is_last
)
555 * Recover only last LEBs in the journal heads, because power
556 * cuts may cause corruptions only in these LEBs, because only
557 * these LEBs could possibly be written to at the power cut
560 sleb
= ubifs_recover_leb(c
, lnum
, offs
, c
->sbuf
,
561 b
->bud
->jhead
!= GCHD
);
563 sleb
= ubifs_scan(c
, lnum
, offs
, c
->sbuf
, 0);
565 return PTR_ERR(sleb
);
568 * The bud does not have to start from offset zero - the beginning of
569 * the 'lnum' LEB may contain previously committed data. One of the
570 * things we have to do in replay is to correctly update lprops with
571 * newer information about this LEB.
573 * At this point lprops thinks that this LEB has 'c->leb_size - offs'
574 * bytes of free space because it only contain information about
577 * But we know that real amount of free space is 'c->leb_size -
578 * sleb->endpt', and the space in the 'lnum' LEB between 'offs' and
579 * 'sleb->endpt' is used by bud data. We have to correctly calculate
580 * how much of these data are dirty and update lprops with this
583 * The dirt in that LEB region is comprised of padding nodes, deletion
584 * nodes, truncation nodes and nodes which are obsoleted by subsequent
585 * nodes in this LEB. So instead of calculating clean space, we
586 * calculate used space ('used' variable).
589 list_for_each_entry(snod
, &sleb
->nodes
, list
) {
594 if (snod
->sqnum
>= SQNUM_WATERMARK
) {
595 ubifs_err("file system's life ended");
599 if (snod
->sqnum
> c
->max_sqnum
)
600 c
->max_sqnum
= snod
->sqnum
;
602 switch (snod
->type
) {
605 struct ubifs_ino_node
*ino
= snod
->node
;
606 loff_t new_size
= le64_to_cpu(ino
->size
);
608 if (le32_to_cpu(ino
->nlink
) == 0)
610 err
= insert_node(c
, lnum
, snod
->offs
, snod
->len
,
611 &snod
->key
, snod
->sqnum
, deletion
,
615 case UBIFS_DATA_NODE
:
617 struct ubifs_data_node
*dn
= snod
->node
;
618 loff_t new_size
= le32_to_cpu(dn
->size
) +
619 key_block(c
, &snod
->key
) *
622 err
= insert_node(c
, lnum
, snod
->offs
, snod
->len
,
623 &snod
->key
, snod
->sqnum
, deletion
,
627 case UBIFS_DENT_NODE
:
628 case UBIFS_XENT_NODE
:
630 struct ubifs_dent_node
*dent
= snod
->node
;
632 err
= ubifs_validate_entry(c
, dent
);
636 err
= insert_dent(c
, lnum
, snod
->offs
, snod
->len
,
637 &snod
->key
, dent
->name
,
638 le16_to_cpu(dent
->nlen
), snod
->sqnum
,
639 !le64_to_cpu(dent
->inum
), &used
);
642 case UBIFS_TRUN_NODE
:
644 struct ubifs_trun_node
*trun
= snod
->node
;
645 loff_t old_size
= le64_to_cpu(trun
->old_size
);
646 loff_t new_size
= le64_to_cpu(trun
->new_size
);
649 /* Validate truncation node */
650 if (old_size
< 0 || old_size
> c
->max_inode_sz
||
651 new_size
< 0 || new_size
> c
->max_inode_sz
||
652 old_size
<= new_size
) {
653 ubifs_err("bad truncation node");
658 * Create a fake truncation key just to use the same
659 * functions which expect nodes to have keys.
661 trun_key_init(c
, &key
, le32_to_cpu(trun
->inum
));
662 err
= insert_node(c
, lnum
, snod
->offs
, snod
->len
,
663 &key
, snod
->sqnum
, 1, &used
,
668 ubifs_err("unexpected node type %d in bud LEB %d:%d",
669 snod
->type
, lnum
, snod
->offs
);
677 ubifs_assert(ubifs_search_bud(c
, lnum
));
678 ubifs_assert(sleb
->endpt
- offs
>= used
);
679 ubifs_assert(sleb
->endpt
% c
->min_io_size
== 0);
681 b
->dirty
= sleb
->endpt
- offs
- used
;
682 b
->free
= c
->leb_size
- sleb
->endpt
;
683 dbg_mnt("bud LEB %d replied: dirty %d, free %d", lnum
, b
->dirty
, b
->free
);
686 ubifs_scan_destroy(sleb
);
690 ubifs_err("bad node is at LEB %d:%d", lnum
, snod
->offs
);
691 dbg_dump_node(c
, snod
->node
);
692 ubifs_scan_destroy(sleb
);
697 * replay_buds - replay all buds.
698 * @c: UBIFS file-system description object
700 * This function returns zero in case of success and a negative error code in
703 static int replay_buds(struct ubifs_info
*c
)
707 unsigned long long prev_sqnum
= 0;
709 list_for_each_entry(b
, &c
->replay_buds
, list
) {
710 err
= replay_bud(c
, b
);
714 ubifs_assert(b
->sqnum
> prev_sqnum
);
715 prev_sqnum
= b
->sqnum
;
722 * destroy_bud_list - destroy the list of buds to replay.
723 * @c: UBIFS file-system description object
725 static void destroy_bud_list(struct ubifs_info
*c
)
729 while (!list_empty(&c
->replay_buds
)) {
730 b
= list_entry(c
->replay_buds
.next
, struct bud_entry
, list
);
737 * add_replay_bud - add a bud to the list of buds to replay.
738 * @c: UBIFS file-system description object
739 * @lnum: bud logical eraseblock number to replay
740 * @offs: bud start offset
741 * @jhead: journal head to which this bud belongs
742 * @sqnum: reference node sequence number
744 * This function returns zero in case of success and a negative error code in
747 static int add_replay_bud(struct ubifs_info
*c
, int lnum
, int offs
, int jhead
,
748 unsigned long long sqnum
)
750 struct ubifs_bud
*bud
;
753 dbg_mnt("add replay bud LEB %d:%d, head %d", lnum
, offs
, jhead
);
755 bud
= kmalloc(sizeof(struct ubifs_bud
), GFP_KERNEL
);
759 b
= kmalloc(sizeof(struct bud_entry
), GFP_KERNEL
);
768 ubifs_add_bud(c
, bud
);
772 list_add_tail(&b
->list
, &c
->replay_buds
);
778 * validate_ref - validate a reference node.
779 * @c: UBIFS file-system description object
780 * @ref: the reference node to validate
781 * @ref_lnum: LEB number of the reference node
782 * @ref_offs: reference node offset
784 * This function returns %1 if a bud reference already exists for the LEB. %0 is
785 * returned if the reference node is new, otherwise %-EINVAL is returned if
788 static int validate_ref(struct ubifs_info
*c
, const struct ubifs_ref_node
*ref
)
790 struct ubifs_bud
*bud
;
791 int lnum
= le32_to_cpu(ref
->lnum
);
792 unsigned int offs
= le32_to_cpu(ref
->offs
);
793 unsigned int jhead
= le32_to_cpu(ref
->jhead
);
796 * ref->offs may point to the end of LEB when the journal head points
797 * to the end of LEB and we write reference node for it during commit.
798 * So this is why we require 'offs > c->leb_size'.
800 if (jhead
>= c
->jhead_cnt
|| lnum
>= c
->leb_cnt
||
801 lnum
< c
->main_first
|| offs
> c
->leb_size
||
802 offs
& (c
->min_io_size
- 1))
805 /* Make sure we have not already looked at this bud */
806 bud
= ubifs_search_bud(c
, lnum
);
808 if (bud
->jhead
== jhead
&& bud
->start
<= offs
)
810 ubifs_err("bud at LEB %d:%d was already referred", lnum
, offs
);
818 * replay_log_leb - replay a log logical eraseblock.
819 * @c: UBIFS file-system description object
820 * @lnum: log logical eraseblock to replay
821 * @offs: offset to start replaying from
824 * This function replays a log LEB and returns zero in case of success, %1 if
825 * this is the last LEB in the log, and a negative error code in case of
828 static int replay_log_leb(struct ubifs_info
*c
, int lnum
, int offs
, void *sbuf
)
831 struct ubifs_scan_leb
*sleb
;
832 struct ubifs_scan_node
*snod
;
833 const struct ubifs_cs_node
*node
;
835 dbg_mnt("replay log LEB %d:%d", lnum
, offs
);
836 sleb
= ubifs_scan(c
, lnum
, offs
, sbuf
, c
->need_recovery
);
838 if (PTR_ERR(sleb
) != -EUCLEAN
|| !c
->need_recovery
)
839 return PTR_ERR(sleb
);
841 * Note, the below function will recover this log LEB only if
842 * it is the last, because unclean reboots can possibly corrupt
843 * only the tail of the log.
845 sleb
= ubifs_recover_log_leb(c
, lnum
, offs
, sbuf
);
847 return PTR_ERR(sleb
);
850 if (sleb
->nodes_cnt
== 0) {
856 snod
= list_entry(sleb
->nodes
.next
, struct ubifs_scan_node
, list
);
857 if (c
->cs_sqnum
== 0) {
859 * This is the first log LEB we are looking at, make sure that
860 * the first node is a commit start node. Also record its
861 * sequence number so that UBIFS can determine where the log
862 * ends, because all nodes which were have higher sequence
865 if (snod
->type
!= UBIFS_CS_NODE
) {
866 dbg_err("first log node at LEB %d:%d is not CS node",
870 if (le64_to_cpu(node
->cmt_no
) != c
->cmt_no
) {
871 dbg_err("first CS node at LEB %d:%d has wrong "
872 "commit number %llu expected %llu",
874 (unsigned long long)le64_to_cpu(node
->cmt_no
),
879 c
->cs_sqnum
= le64_to_cpu(node
->ch
.sqnum
);
880 dbg_mnt("commit start sqnum %llu", c
->cs_sqnum
);
883 if (snod
->sqnum
< c
->cs_sqnum
) {
885 * This means that we reached end of log and now
886 * look to the older log data, which was already
887 * committed but the eraseblock was not erased (UBIFS
888 * only un-maps it). So this basically means we have to
889 * exit with "end of log" code.
895 /* Make sure the first node sits at offset zero of the LEB */
896 if (snod
->offs
!= 0) {
897 dbg_err("first node is not at zero offset");
901 list_for_each_entry(snod
, &sleb
->nodes
, list
) {
904 if (snod
->sqnum
>= SQNUM_WATERMARK
) {
905 ubifs_err("file system's life ended");
909 if (snod
->sqnum
< c
->cs_sqnum
) {
910 dbg_err("bad sqnum %llu, commit sqnum %llu",
911 snod
->sqnum
, c
->cs_sqnum
);
915 if (snod
->sqnum
> c
->max_sqnum
)
916 c
->max_sqnum
= snod
->sqnum
;
918 switch (snod
->type
) {
919 case UBIFS_REF_NODE
: {
920 const struct ubifs_ref_node
*ref
= snod
->node
;
922 err
= validate_ref(c
, ref
);
924 break; /* Already have this bud */
928 err
= add_replay_bud(c
, le32_to_cpu(ref
->lnum
),
929 le32_to_cpu(ref
->offs
),
930 le32_to_cpu(ref
->jhead
),
938 /* Make sure it sits at the beginning of LEB */
939 if (snod
->offs
!= 0) {
940 ubifs_err("unexpected node in log");
945 ubifs_err("unexpected node in log");
950 if (sleb
->endpt
|| c
->lhead_offs
>= c
->leb_size
) {
951 c
->lhead_lnum
= lnum
;
952 c
->lhead_offs
= sleb
->endpt
;
957 ubifs_scan_destroy(sleb
);
961 ubifs_err("log error detected while replaying the log at LEB %d:%d",
962 lnum
, offs
+ snod
->offs
);
963 dbg_dump_node(c
, snod
->node
);
964 ubifs_scan_destroy(sleb
);
969 * take_ihead - update the status of the index head in lprops to 'taken'.
970 * @c: UBIFS file-system description object
972 * This function returns the amount of free space in the index head LEB or a
973 * negative error code.
975 static int take_ihead(struct ubifs_info
*c
)
977 const struct ubifs_lprops
*lp
;
982 lp
= ubifs_lpt_lookup_dirty(c
, c
->ihead_lnum
);
990 lp
= ubifs_change_lp(c
, lp
, LPROPS_NC
, LPROPS_NC
,
991 lp
->flags
| LPROPS_TAKEN
, 0);
999 ubifs_release_lprops(c
);
1004 * ubifs_replay_journal - replay journal.
1005 * @c: UBIFS file-system description object
1007 * This function scans the journal, replays and cleans it up. It makes sure all
1008 * memory data structures related to uncommitted journal are built (dirty TNC
1009 * tree, tree of buds, modified lprops, etc).
1011 int ubifs_replay_journal(struct ubifs_info
*c
)
1013 int err
, i
, lnum
, offs
, free
;
1015 BUILD_BUG_ON(UBIFS_TRUN_KEY
> 5);
1017 /* Update the status of the index head in lprops to 'taken' */
1018 free
= take_ihead(c
);
1020 return free
; /* Error code */
1022 if (c
->ihead_offs
!= c
->leb_size
- free
) {
1023 ubifs_err("bad index head LEB %d:%d", c
->ihead_lnum
,
1028 dbg_mnt("start replaying the journal");
1030 lnum
= c
->ltail_lnum
= c
->lhead_lnum
;
1031 offs
= c
->lhead_offs
;
1033 for (i
= 0; i
< c
->log_lebs
; i
++, lnum
++) {
1034 if (lnum
>= UBIFS_LOG_LNUM
+ c
->log_lebs
) {
1036 * The log is logically circular, we reached the last
1037 * LEB, switch to the first one.
1039 lnum
= UBIFS_LOG_LNUM
;
1042 err
= replay_log_leb(c
, lnum
, offs
, c
->sbuf
);
1044 /* We hit the end of the log */
1051 err
= replay_buds(c
);
1055 err
= apply_replay_list(c
);
1059 err
= set_buds_lprops(c
);
1064 * UBIFS budgeting calculations use @c->bi.uncommitted_idx variable
1065 * to roughly estimate index growth. Things like @c->bi.min_idx_lebs
1066 * depend on it. This means we have to initialize it to make sure
1067 * budgeting works properly.
1069 c
->bi
.uncommitted_idx
= atomic_long_read(&c
->dirty_zn_cnt
);
1070 c
->bi
.uncommitted_idx
*= c
->max_idx_node_sz
;
1072 ubifs_assert(c
->bud_bytes
<= c
->max_bud_bytes
|| c
->need_recovery
);
1073 dbg_mnt("finished, log head LEB %d:%d, max_sqnum %llu, "
1074 "highest_inum %lu", c
->lhead_lnum
, c
->lhead_offs
, c
->max_sqnum
,
1075 (unsigned long)c
->highest_inum
);
1077 destroy_replay_list(c
);
1078 destroy_bud_list(c
);