blob: d0fcd83058fa600228dc5db9111e0872eb8aa2a9 [file] [log] [blame]
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001/*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation.
5 *
Heiko Schocherff94bc42014-06-24 10:10:04 +02006 * SPDX-License-Identifier: GPL-2.0+
Stefan Roese9eefe2a2009-03-19 15:35:05 +01007 *
8 * Authors: Adrian Hunter
9 * Artem Bityutskiy (Битюцкий Артём)
10 */
11
12/*
13 * This file contains journal replay code. It runs when the file-system is being
14 * mounted and requires no locking.
15 *
16 * The larger is the journal, the longer it takes to scan it, so the longer it
17 * takes to mount UBIFS. This is why the journal has limited size which may be
18 * changed depending on the system requirements. But a larger journal gives
19 * faster I/O speed because it writes the index less frequently. So this is a
20 * trade-off. Also, the journal is indexed by the in-memory index (TNC), so the
21 * larger is the journal, the more memory its index may consume.
22 */
23
Heiko Schocherff94bc42014-06-24 10:10:04 +020024#ifdef __UBOOT__
25#include <linux/compat.h>
26#include <linux/err.h>
27#endif
Stefan Roese9eefe2a2009-03-19 15:35:05 +010028#include "ubifs.h"
Heiko Schocherff94bc42014-06-24 10:10:04 +020029#include <linux/list_sort.h>
Stefan Roese9eefe2a2009-03-19 15:35:05 +010030
31/**
Heiko Schocherff94bc42014-06-24 10:10:04 +020032 * struct replay_entry - replay list entry.
Stefan Roese9eefe2a2009-03-19 15:35:05 +010033 * @lnum: logical eraseblock number of the node
34 * @offs: node offset
35 * @len: node length
Heiko Schocherff94bc42014-06-24 10:10:04 +020036 * @deletion: non-zero if this entry corresponds to a node deletion
Stefan Roese9eefe2a2009-03-19 15:35:05 +010037 * @sqnum: node sequence number
Heiko Schocherff94bc42014-06-24 10:10:04 +020038 * @list: links the replay list
Stefan Roese9eefe2a2009-03-19 15:35:05 +010039 * @key: node key
40 * @nm: directory entry name
41 * @old_size: truncation old size
42 * @new_size: truncation new size
Stefan Roese9eefe2a2009-03-19 15:35:05 +010043 *
Heiko Schocherff94bc42014-06-24 10:10:04 +020044 * The replay process first scans all buds and builds the replay list, then
45 * sorts the replay list in nodes sequence number order, and then inserts all
46 * the replay entries to the TNC.
Stefan Roese9eefe2a2009-03-19 15:35:05 +010047 */
48struct replay_entry {
49 int lnum;
50 int offs;
51 int len;
Heiko Schocherff94bc42014-06-24 10:10:04 +020052 unsigned int deletion:1;
Stefan Roese9eefe2a2009-03-19 15:35:05 +010053 unsigned long long sqnum;
Heiko Schocherff94bc42014-06-24 10:10:04 +020054 struct list_head list;
Stefan Roese9eefe2a2009-03-19 15:35:05 +010055 union ubifs_key key;
56 union {
57 struct qstr nm;
58 struct {
59 loff_t old_size;
60 loff_t new_size;
61 };
Stefan Roese9eefe2a2009-03-19 15:35:05 +010062 };
63};
64
65/**
66 * struct bud_entry - entry in the list of buds to replay.
67 * @list: next bud in the list
68 * @bud: bud description object
Stefan Roese9eefe2a2009-03-19 15:35:05 +010069 * @sqnum: reference node sequence number
Heiko Schocherff94bc42014-06-24 10:10:04 +020070 * @free: free bytes in the bud
71 * @dirty: dirty bytes in the bud
Stefan Roese9eefe2a2009-03-19 15:35:05 +010072 */
73struct bud_entry {
74 struct list_head list;
75 struct ubifs_bud *bud;
Stefan Roese9eefe2a2009-03-19 15:35:05 +010076 unsigned long long sqnum;
Heiko Schocherff94bc42014-06-24 10:10:04 +020077 int free;
78 int dirty;
Stefan Roese9eefe2a2009-03-19 15:35:05 +010079};
80
81/**
82 * set_bud_lprops - set free and dirty space used by a bud.
83 * @c: UBIFS file-system description object
Heiko Schocherff94bc42014-06-24 10:10:04 +020084 * @b: bud entry which describes the bud
85 *
86 * This function makes sure the LEB properties of bud @b are set correctly
87 * after the replay. Returns zero in case of success and a negative error code
88 * in case of failure.
Stefan Roese9eefe2a2009-03-19 15:35:05 +010089 */
Heiko Schocherff94bc42014-06-24 10:10:04 +020090static int set_bud_lprops(struct ubifs_info *c, struct bud_entry *b)
Stefan Roese9eefe2a2009-03-19 15:35:05 +010091{
92 const struct ubifs_lprops *lp;
93 int err = 0, dirty;
94
95 ubifs_get_lprops(c);
96
Heiko Schocherff94bc42014-06-24 10:10:04 +020097 lp = ubifs_lpt_lookup_dirty(c, b->bud->lnum);
Stefan Roese9eefe2a2009-03-19 15:35:05 +010098 if (IS_ERR(lp)) {
99 err = PTR_ERR(lp);
100 goto out;
101 }
102
103 dirty = lp->dirty;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200104 if (b->bud->start == 0 && (lp->free != c->leb_size || lp->dirty != 0)) {
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100105 /*
106 * The LEB was added to the journal with a starting offset of
107 * zero which means the LEB must have been empty. The LEB
Heiko Schocherff94bc42014-06-24 10:10:04 +0200108 * property values should be @lp->free == @c->leb_size and
109 * @lp->dirty == 0, but that is not the case. The reason is that
110 * the LEB had been garbage collected before it became the bud,
111 * and there was not commit inbetween. The garbage collector
112 * resets the free and dirty space without recording it
113 * anywhere except lprops, so if there was no commit then
114 * lprops does not have that information.
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100115 *
116 * We do not need to adjust free space because the scan has told
117 * us the exact value which is recorded in the replay entry as
Heiko Schocherff94bc42014-06-24 10:10:04 +0200118 * @b->free.
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100119 *
120 * However we do need to subtract from the dirty space the
121 * amount of space that the garbage collector reclaimed, which
122 * is the whole LEB minus the amount of space that was free.
123 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200124 dbg_mnt("bud LEB %d was GC'd (%d free, %d dirty)", b->bud->lnum,
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100125 lp->free, lp->dirty);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200126 dbg_gc("bud LEB %d was GC'd (%d free, %d dirty)", b->bud->lnum,
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100127 lp->free, lp->dirty);
128 dirty -= c->leb_size - lp->free;
129 /*
130 * If the replay order was perfect the dirty space would now be
Heiko Schocherff94bc42014-06-24 10:10:04 +0200131 * zero. The order is not perfect because the journal heads
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100132 * race with each other. This is not a problem but is does mean
133 * that the dirty space may temporarily exceed c->leb_size
134 * during the replay.
135 */
136 if (dirty != 0)
Heiko Schocherff94bc42014-06-24 10:10:04 +0200137 dbg_mnt("LEB %d lp: %d free %d dirty replay: %d free %d dirty",
138 b->bud->lnum, lp->free, lp->dirty, b->free,
139 b->dirty);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100140 }
Heiko Schocherff94bc42014-06-24 10:10:04 +0200141 lp = ubifs_change_lp(c, lp, b->free, dirty + b->dirty,
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100142 lp->flags | LPROPS_TAKEN, 0);
143 if (IS_ERR(lp)) {
144 err = PTR_ERR(lp);
145 goto out;
146 }
Heiko Schocherff94bc42014-06-24 10:10:04 +0200147
148 /* Make sure the journal head points to the latest bud */
149 err = ubifs_wbuf_seek_nolock(&c->jheads[b->bud->jhead].wbuf,
150 b->bud->lnum, c->leb_size - b->free);
151
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100152out:
153 ubifs_release_lprops(c);
154 return err;
155}
156
157/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200158 * set_buds_lprops - set free and dirty space for all replayed buds.
159 * @c: UBIFS file-system description object
160 *
161 * This function sets LEB properties for all replayed buds. Returns zero in
162 * case of success and a negative error code in case of failure.
163 */
164static int set_buds_lprops(struct ubifs_info *c)
165{
166 struct bud_entry *b;
167 int err;
168
169 list_for_each_entry(b, &c->replay_buds, list) {
170 err = set_bud_lprops(c, b);
171 if (err)
172 return err;
173 }
174
175 return 0;
176}
177
178/**
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100179 * trun_remove_range - apply a replay entry for a truncation to the TNC.
180 * @c: UBIFS file-system description object
181 * @r: replay entry of truncation
182 */
183static int trun_remove_range(struct ubifs_info *c, struct replay_entry *r)
184{
185 unsigned min_blk, max_blk;
186 union ubifs_key min_key, max_key;
187 ino_t ino;
188
189 min_blk = r->new_size / UBIFS_BLOCK_SIZE;
190 if (r->new_size & (UBIFS_BLOCK_SIZE - 1))
191 min_blk += 1;
192
193 max_blk = r->old_size / UBIFS_BLOCK_SIZE;
194 if ((r->old_size & (UBIFS_BLOCK_SIZE - 1)) == 0)
195 max_blk -= 1;
196
197 ino = key_inum(c, &r->key);
198
199 data_key_init(c, &min_key, ino, min_blk);
200 data_key_init(c, &max_key, ino, max_blk);
201
202 return ubifs_tnc_remove_range(c, &min_key, &max_key);
203}
204
205/**
206 * apply_replay_entry - apply a replay entry to the TNC.
207 * @c: UBIFS file-system description object
208 * @r: replay entry to apply
209 *
210 * Apply a replay entry to the TNC.
211 */
212static int apply_replay_entry(struct ubifs_info *c, struct replay_entry *r)
213{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200214 int err;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100215
Heiko Schocherff94bc42014-06-24 10:10:04 +0200216 dbg_mntk(&r->key, "LEB %d:%d len %d deletion %d sqnum %llu key ",
217 r->lnum, r->offs, r->len, r->deletion, r->sqnum);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100218
219 /* Set c->replay_sqnum to help deal with dangling branches. */
220 c->replay_sqnum = r->sqnum;
221
Heiko Schocherff94bc42014-06-24 10:10:04 +0200222 if (is_hash_key(c, &r->key)) {
223 if (r->deletion)
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100224 err = ubifs_tnc_remove_nm(c, &r->key, &r->nm);
225 else
226 err = ubifs_tnc_add_nm(c, &r->key, r->lnum, r->offs,
227 r->len, &r->nm);
228 } else {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200229 if (r->deletion)
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100230 switch (key_type(c, &r->key)) {
231 case UBIFS_INO_KEY:
232 {
233 ino_t inum = key_inum(c, &r->key);
234
235 err = ubifs_tnc_remove_ino(c, inum);
236 break;
237 }
238 case UBIFS_TRUN_KEY:
239 err = trun_remove_range(c, r);
240 break;
241 default:
242 err = ubifs_tnc_remove(c, &r->key);
243 break;
244 }
245 else
246 err = ubifs_tnc_add(c, &r->key, r->lnum, r->offs,
247 r->len);
248 if (err)
249 return err;
250
251 if (c->need_recovery)
Heiko Schocherff94bc42014-06-24 10:10:04 +0200252 err = ubifs_recover_size_accum(c, &r->key, r->deletion,
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100253 r->new_size);
254 }
255
256 return err;
257}
258
259/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200260 * replay_entries_cmp - compare 2 replay entries.
261 * @priv: UBIFS file-system description object
262 * @a: first replay entry
263 * @a: second replay entry
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100264 *
Heiko Schocherff94bc42014-06-24 10:10:04 +0200265 * This is a comparios function for 'list_sort()' which compares 2 replay
266 * entries @a and @b by comparing their sequence numer. Returns %1 if @a has
267 * greater sequence number and %-1 otherwise.
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100268 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200269static int replay_entries_cmp(void *priv, struct list_head *a,
270 struct list_head *b)
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100271{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200272 struct replay_entry *ra, *rb;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100273
Heiko Schocherff94bc42014-06-24 10:10:04 +0200274 cond_resched();
275 if (a == b)
276 return 0;
277
278 ra = list_entry(a, struct replay_entry, list);
279 rb = list_entry(b, struct replay_entry, list);
280 ubifs_assert(ra->sqnum != rb->sqnum);
281 if (ra->sqnum > rb->sqnum)
282 return 1;
283 return -1;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100284}
285
286/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200287 * apply_replay_list - apply the replay list to the TNC.
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100288 * @c: UBIFS file-system description object
289 *
Heiko Schocherff94bc42014-06-24 10:10:04 +0200290 * Apply all entries in the replay list to the TNC. Returns zero in case of
291 * success and a negative error code in case of failure.
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100292 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200293static int apply_replay_list(struct ubifs_info *c)
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100294{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200295 struct replay_entry *r;
296 int err;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100297
Heiko Schocherff94bc42014-06-24 10:10:04 +0200298 list_sort(c, &c->replay_list, &replay_entries_cmp);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100299
Heiko Schocherff94bc42014-06-24 10:10:04 +0200300 list_for_each_entry(r, &c->replay_list, list) {
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100301 cond_resched();
302
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100303 err = apply_replay_entry(c, r);
304 if (err)
305 return err;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100306 }
Heiko Schocherff94bc42014-06-24 10:10:04 +0200307
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100308 return 0;
309}
310
311/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200312 * destroy_replay_list - destroy the replay.
313 * @c: UBIFS file-system description object
314 *
315 * Destroy the replay list.
316 */
317static void destroy_replay_list(struct ubifs_info *c)
318{
319 struct replay_entry *r, *tmp;
320
321 list_for_each_entry_safe(r, tmp, &c->replay_list, list) {
322 if (is_hash_key(c, &r->key))
323 kfree(r->nm.name);
324 list_del(&r->list);
325 kfree(r);
326 }
327}
328
329/**
330 * insert_node - insert a node to the replay list
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100331 * @c: UBIFS file-system description object
332 * @lnum: node logical eraseblock number
333 * @offs: node offset
334 * @len: node length
335 * @key: node key
336 * @sqnum: sequence number
337 * @deletion: non-zero if this is a deletion
338 * @used: number of bytes in use in a LEB
339 * @old_size: truncation old size
340 * @new_size: truncation new size
341 *
Heiko Schocherff94bc42014-06-24 10:10:04 +0200342 * This function inserts a scanned non-direntry node to the replay list. The
343 * replay list contains @struct replay_entry elements, and we sort this list in
344 * sequence number order before applying it. The replay list is applied at the
345 * very end of the replay process. Since the list is sorted in sequence number
346 * order, the older modifications are applied first. This function returns zero
347 * in case of success and a negative error code in case of failure.
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100348 */
349static int insert_node(struct ubifs_info *c, int lnum, int offs, int len,
350 union ubifs_key *key, unsigned long long sqnum,
351 int deletion, int *used, loff_t old_size,
352 loff_t new_size)
353{
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100354 struct replay_entry *r;
355
Heiko Schocherff94bc42014-06-24 10:10:04 +0200356 dbg_mntk(key, "add LEB %d:%d, key ", lnum, offs);
357
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100358 if (key_inum(c, key) >= c->highest_inum)
359 c->highest_inum = key_inum(c, key);
360
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100361 r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL);
362 if (!r)
363 return -ENOMEM;
364
365 if (!deletion)
366 *used += ALIGN(len, 8);
367 r->lnum = lnum;
368 r->offs = offs;
369 r->len = len;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200370 r->deletion = !!deletion;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100371 r->sqnum = sqnum;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200372 key_copy(c, key, &r->key);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100373 r->old_size = old_size;
374 r->new_size = new_size;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100375
Heiko Schocherff94bc42014-06-24 10:10:04 +0200376 list_add_tail(&r->list, &c->replay_list);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100377 return 0;
378}
379
380/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200381 * insert_dent - insert a directory entry node into the replay list.
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100382 * @c: UBIFS file-system description object
383 * @lnum: node logical eraseblock number
384 * @offs: node offset
385 * @len: node length
386 * @key: node key
387 * @name: directory entry name
388 * @nlen: directory entry name length
389 * @sqnum: sequence number
390 * @deletion: non-zero if this is a deletion
391 * @used: number of bytes in use in a LEB
392 *
Heiko Schocherff94bc42014-06-24 10:10:04 +0200393 * This function inserts a scanned directory entry node or an extended
394 * attribute entry to the replay list. Returns zero in case of success and a
395 * negative error code in case of failure.
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100396 */
397static int insert_dent(struct ubifs_info *c, int lnum, int offs, int len,
398 union ubifs_key *key, const char *name, int nlen,
399 unsigned long long sqnum, int deletion, int *used)
400{
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100401 struct replay_entry *r;
402 char *nbuf;
403
Heiko Schocherff94bc42014-06-24 10:10:04 +0200404 dbg_mntk(key, "add LEB %d:%d, key ", lnum, offs);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100405 if (key_inum(c, key) >= c->highest_inum)
406 c->highest_inum = key_inum(c, key);
407
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100408 r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL);
409 if (!r)
410 return -ENOMEM;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200411
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100412 nbuf = kmalloc(nlen + 1, GFP_KERNEL);
413 if (!nbuf) {
414 kfree(r);
415 return -ENOMEM;
416 }
417
418 if (!deletion)
419 *used += ALIGN(len, 8);
420 r->lnum = lnum;
421 r->offs = offs;
422 r->len = len;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200423 r->deletion = !!deletion;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100424 r->sqnum = sqnum;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200425 key_copy(c, key, &r->key);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100426 r->nm.len = nlen;
427 memcpy(nbuf, name, nlen);
428 nbuf[nlen] = '\0';
429 r->nm.name = nbuf;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100430
Heiko Schocherff94bc42014-06-24 10:10:04 +0200431 list_add_tail(&r->list, &c->replay_list);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100432 return 0;
433}
434
435/**
436 * ubifs_validate_entry - validate directory or extended attribute entry node.
437 * @c: UBIFS file-system description object
438 * @dent: the node to validate
439 *
440 * This function validates directory or extended attribute entry node @dent.
441 * Returns zero if the node is all right and a %-EINVAL if not.
442 */
443int ubifs_validate_entry(struct ubifs_info *c,
444 const struct ubifs_dent_node *dent)
445{
446 int key_type = key_type_flash(c, dent->key);
447 int nlen = le16_to_cpu(dent->nlen);
448
449 if (le32_to_cpu(dent->ch.len) != nlen + UBIFS_DENT_NODE_SZ + 1 ||
450 dent->type >= UBIFS_ITYPES_CNT ||
451 nlen > UBIFS_MAX_NLEN || dent->name[nlen] != 0 ||
Heiko Schocherff94bc42014-06-24 10:10:04 +0200452 strnlen(dent->name, nlen) != nlen ||
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100453 le64_to_cpu(dent->inum) > MAX_INUM) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200454 ubifs_err(c, "bad %s node", key_type == UBIFS_DENT_KEY ?
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100455 "directory entry" : "extended attribute entry");
456 return -EINVAL;
457 }
458
459 if (key_type != UBIFS_DENT_KEY && key_type != UBIFS_XENT_KEY) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200460 ubifs_err(c, "bad key type %d", key_type);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100461 return -EINVAL;
462 }
463
464 return 0;
465}
466
Heiko Schocherff94bc42014-06-24 10:10:04 +0200467/**
468 * is_last_bud - check if the bud is the last in the journal head.
469 * @c: UBIFS file-system description object
470 * @bud: bud description object
471 *
472 * This function checks if bud @bud is the last bud in its journal head. This
473 * information is then used by 'replay_bud()' to decide whether the bud can
474 * have corruptions or not. Indeed, only last buds can be corrupted by power
475 * cuts. Returns %1 if this is the last bud, and %0 if not.
476 */
477static int is_last_bud(struct ubifs_info *c, struct ubifs_bud *bud)
478{
479 struct ubifs_jhead *jh = &c->jheads[bud->jhead];
480 struct ubifs_bud *next;
481 uint32_t data;
482 int err;
483
484 if (list_is_last(&bud->list, &jh->buds_list))
485 return 1;
486
487 /*
488 * The following is a quirk to make sure we work correctly with UBIFS
489 * images used with older UBIFS.
490 *
491 * Normally, the last bud will be the last in the journal head's list
492 * of bud. However, there is one exception if the UBIFS image belongs
493 * to older UBIFS. This is fairly unlikely: one would need to use old
494 * UBIFS, then have a power cut exactly at the right point, and then
495 * try to mount this image with new UBIFS.
496 *
497 * The exception is: it is possible to have 2 buds A and B, A goes
498 * before B, and B is the last, bud B is contains no data, and bud A is
499 * corrupted at the end. The reason is that in older versions when the
500 * journal code switched the next bud (from A to B), it first added a
501 * log reference node for the new bud (B), and only after this it
502 * synchronized the write-buffer of current bud (A). But later this was
503 * changed and UBIFS started to always synchronize the write-buffer of
504 * the bud (A) before writing the log reference for the new bud (B).
505 *
506 * But because older UBIFS always synchronized A's write-buffer before
507 * writing to B, we can recognize this exceptional situation but
508 * checking the contents of bud B - if it is empty, then A can be
509 * treated as the last and we can recover it.
510 *
511 * TODO: remove this piece of code in a couple of years (today it is
512 * 16.05.2011).
513 */
514 next = list_entry(bud->list.next, struct ubifs_bud, list);
515 if (!list_is_last(&next->list, &jh->buds_list))
516 return 0;
517
518 err = ubifs_leb_read(c, next->lnum, (char *)&data, next->start, 4, 1);
519 if (err)
520 return 0;
521
522 return data == 0xFFFFFFFF;
523}
524
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100525/**
526 * replay_bud - replay a bud logical eraseblock.
527 * @c: UBIFS file-system description object
Heiko Schocherff94bc42014-06-24 10:10:04 +0200528 * @b: bud entry which describes the bud
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100529 *
Heiko Schocherff94bc42014-06-24 10:10:04 +0200530 * This function replays bud @bud, recovers it if needed, and adds all nodes
531 * from this bud to the replay list. Returns zero in case of success and a
532 * negative error code in case of failure.
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100533 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200534static int replay_bud(struct ubifs_info *c, struct bud_entry *b)
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100535{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200536 int is_last = is_last_bud(c, b->bud);
537 int err = 0, used = 0, lnum = b->bud->lnum, offs = b->bud->start;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100538 struct ubifs_scan_leb *sleb;
539 struct ubifs_scan_node *snod;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100540
Heiko Schocherff94bc42014-06-24 10:10:04 +0200541 dbg_mnt("replay bud LEB %d, head %d, offs %d, is_last %d",
542 lnum, b->bud->jhead, offs, is_last);
543
544 if (c->need_recovery && is_last)
545 /*
546 * Recover only last LEBs in the journal heads, because power
547 * cuts may cause corruptions only in these LEBs, because only
548 * these LEBs could possibly be written to at the power cut
549 * time.
550 */
551 sleb = ubifs_recover_leb(c, lnum, offs, c->sbuf, b->bud->jhead);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100552 else
Heiko Schocherff94bc42014-06-24 10:10:04 +0200553 sleb = ubifs_scan(c, lnum, offs, c->sbuf, 0);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100554 if (IS_ERR(sleb))
555 return PTR_ERR(sleb);
556
557 /*
558 * The bud does not have to start from offset zero - the beginning of
559 * the 'lnum' LEB may contain previously committed data. One of the
560 * things we have to do in replay is to correctly update lprops with
561 * newer information about this LEB.
562 *
563 * At this point lprops thinks that this LEB has 'c->leb_size - offs'
564 * bytes of free space because it only contain information about
565 * committed data.
566 *
567 * But we know that real amount of free space is 'c->leb_size -
568 * sleb->endpt', and the space in the 'lnum' LEB between 'offs' and
569 * 'sleb->endpt' is used by bud data. We have to correctly calculate
570 * how much of these data are dirty and update lprops with this
571 * information.
572 *
573 * The dirt in that LEB region is comprised of padding nodes, deletion
574 * nodes, truncation nodes and nodes which are obsoleted by subsequent
575 * nodes in this LEB. So instead of calculating clean space, we
576 * calculate used space ('used' variable).
577 */
578
579 list_for_each_entry(snod, &sleb->nodes, list) {
580 int deletion = 0;
581
582 cond_resched();
583
584 if (snod->sqnum >= SQNUM_WATERMARK) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200585 ubifs_err(c, "file system's life ended");
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100586 goto out_dump;
587 }
588
589 if (snod->sqnum > c->max_sqnum)
590 c->max_sqnum = snod->sqnum;
591
592 switch (snod->type) {
593 case UBIFS_INO_NODE:
594 {
595 struct ubifs_ino_node *ino = snod->node;
596 loff_t new_size = le64_to_cpu(ino->size);
597
598 if (le32_to_cpu(ino->nlink) == 0)
599 deletion = 1;
600 err = insert_node(c, lnum, snod->offs, snod->len,
601 &snod->key, snod->sqnum, deletion,
602 &used, 0, new_size);
603 break;
604 }
605 case UBIFS_DATA_NODE:
606 {
607 struct ubifs_data_node *dn = snod->node;
608 loff_t new_size = le32_to_cpu(dn->size) +
609 key_block(c, &snod->key) *
610 UBIFS_BLOCK_SIZE;
611
612 err = insert_node(c, lnum, snod->offs, snod->len,
613 &snod->key, snod->sqnum, deletion,
614 &used, 0, new_size);
615 break;
616 }
617 case UBIFS_DENT_NODE:
618 case UBIFS_XENT_NODE:
619 {
620 struct ubifs_dent_node *dent = snod->node;
621
622 err = ubifs_validate_entry(c, dent);
623 if (err)
624 goto out_dump;
625
626 err = insert_dent(c, lnum, snod->offs, snod->len,
Heiko Schocherff94bc42014-06-24 10:10:04 +0200627 &snod->key, dent->name,
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100628 le16_to_cpu(dent->nlen), snod->sqnum,
629 !le64_to_cpu(dent->inum), &used);
630 break;
631 }
632 case UBIFS_TRUN_NODE:
633 {
634 struct ubifs_trun_node *trun = snod->node;
635 loff_t old_size = le64_to_cpu(trun->old_size);
636 loff_t new_size = le64_to_cpu(trun->new_size);
637 union ubifs_key key;
638
639 /* Validate truncation node */
640 if (old_size < 0 || old_size > c->max_inode_sz ||
641 new_size < 0 || new_size > c->max_inode_sz ||
642 old_size <= new_size) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200643 ubifs_err(c, "bad truncation node");
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100644 goto out_dump;
645 }
646
647 /*
648 * Create a fake truncation key just to use the same
649 * functions which expect nodes to have keys.
650 */
651 trun_key_init(c, &key, le32_to_cpu(trun->inum));
652 err = insert_node(c, lnum, snod->offs, snod->len,
653 &key, snod->sqnum, 1, &used,
654 old_size, new_size);
655 break;
656 }
657 default:
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200658 ubifs_err(c, "unexpected node type %d in bud LEB %d:%d",
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100659 snod->type, lnum, snod->offs);
660 err = -EINVAL;
661 goto out_dump;
662 }
663 if (err)
664 goto out;
665 }
666
Heiko Schocherff94bc42014-06-24 10:10:04 +0200667 ubifs_assert(ubifs_search_bud(c, lnum));
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100668 ubifs_assert(sleb->endpt - offs >= used);
669 ubifs_assert(sleb->endpt % c->min_io_size == 0);
670
Heiko Schocherff94bc42014-06-24 10:10:04 +0200671 b->dirty = sleb->endpt - offs - used;
672 b->free = c->leb_size - sleb->endpt;
673 dbg_mnt("bud LEB %d replied: dirty %d, free %d",
674 lnum, b->dirty, b->free);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100675
676out:
677 ubifs_scan_destroy(sleb);
678 return err;
679
680out_dump:
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200681 ubifs_err(c, "bad node is at LEB %d:%d", lnum, snod->offs);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200682 ubifs_dump_node(c, snod->node);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100683 ubifs_scan_destroy(sleb);
684 return -EINVAL;
685}
686
687/**
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100688 * replay_buds - replay all buds.
689 * @c: UBIFS file-system description object
690 *
691 * This function returns zero in case of success and a negative error code in
692 * case of failure.
693 */
694static int replay_buds(struct ubifs_info *c)
695{
696 struct bud_entry *b;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200697 int err;
698 unsigned long long prev_sqnum = 0;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100699
700 list_for_each_entry(b, &c->replay_buds, list) {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200701 err = replay_bud(c, b);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100702 if (err)
703 return err;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200704
705 ubifs_assert(b->sqnum > prev_sqnum);
706 prev_sqnum = b->sqnum;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100707 }
708
709 return 0;
710}
711
712/**
713 * destroy_bud_list - destroy the list of buds to replay.
714 * @c: UBIFS file-system description object
715 */
716static void destroy_bud_list(struct ubifs_info *c)
717{
718 struct bud_entry *b;
719
720 while (!list_empty(&c->replay_buds)) {
721 b = list_entry(c->replay_buds.next, struct bud_entry, list);
722 list_del(&b->list);
723 kfree(b);
724 }
725}
726
727/**
728 * add_replay_bud - add a bud to the list of buds to replay.
729 * @c: UBIFS file-system description object
730 * @lnum: bud logical eraseblock number to replay
731 * @offs: bud start offset
732 * @jhead: journal head to which this bud belongs
733 * @sqnum: reference node sequence number
734 *
735 * This function returns zero in case of success and a negative error code in
736 * case of failure.
737 */
738static int add_replay_bud(struct ubifs_info *c, int lnum, int offs, int jhead,
739 unsigned long long sqnum)
740{
741 struct ubifs_bud *bud;
742 struct bud_entry *b;
743
744 dbg_mnt("add replay bud LEB %d:%d, head %d", lnum, offs, jhead);
745
746 bud = kmalloc(sizeof(struct ubifs_bud), GFP_KERNEL);
747 if (!bud)
748 return -ENOMEM;
749
750 b = kmalloc(sizeof(struct bud_entry), GFP_KERNEL);
751 if (!b) {
752 kfree(bud);
753 return -ENOMEM;
754 }
755
756 bud->lnum = lnum;
757 bud->start = offs;
758 bud->jhead = jhead;
759 ubifs_add_bud(c, bud);
760
761 b->bud = bud;
762 b->sqnum = sqnum;
763 list_add_tail(&b->list, &c->replay_buds);
764
765 return 0;
766}
767
768/**
769 * validate_ref - validate a reference node.
770 * @c: UBIFS file-system description object
771 * @ref: the reference node to validate
772 * @ref_lnum: LEB number of the reference node
773 * @ref_offs: reference node offset
774 *
775 * This function returns %1 if a bud reference already exists for the LEB. %0 is
776 * returned if the reference node is new, otherwise %-EINVAL is returned if
777 * validation failed.
778 */
779static int validate_ref(struct ubifs_info *c, const struct ubifs_ref_node *ref)
780{
781 struct ubifs_bud *bud;
782 int lnum = le32_to_cpu(ref->lnum);
783 unsigned int offs = le32_to_cpu(ref->offs);
784 unsigned int jhead = le32_to_cpu(ref->jhead);
785
786 /*
787 * ref->offs may point to the end of LEB when the journal head points
788 * to the end of LEB and we write reference node for it during commit.
789 * So this is why we require 'offs > c->leb_size'.
790 */
791 if (jhead >= c->jhead_cnt || lnum >= c->leb_cnt ||
792 lnum < c->main_first || offs > c->leb_size ||
793 offs & (c->min_io_size - 1))
794 return -EINVAL;
795
796 /* Make sure we have not already looked at this bud */
797 bud = ubifs_search_bud(c, lnum);
798 if (bud) {
799 if (bud->jhead == jhead && bud->start <= offs)
800 return 1;
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200801 ubifs_err(c, "bud at LEB %d:%d was already referred", lnum, offs);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100802 return -EINVAL;
803 }
804
805 return 0;
806}
807
808/**
809 * replay_log_leb - replay a log logical eraseblock.
810 * @c: UBIFS file-system description object
811 * @lnum: log logical eraseblock to replay
812 * @offs: offset to start replaying from
813 * @sbuf: scan buffer
814 *
815 * This function replays a log LEB and returns zero in case of success, %1 if
816 * this is the last LEB in the log, and a negative error code in case of
817 * failure.
818 */
819static int replay_log_leb(struct ubifs_info *c, int lnum, int offs, void *sbuf)
820{
821 int err;
822 struct ubifs_scan_leb *sleb;
823 struct ubifs_scan_node *snod;
824 const struct ubifs_cs_node *node;
825
826 dbg_mnt("replay log LEB %d:%d", lnum, offs);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200827 sleb = ubifs_scan(c, lnum, offs, sbuf, c->need_recovery);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100828 if (IS_ERR(sleb)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200829 if (PTR_ERR(sleb) != -EUCLEAN || !c->need_recovery)
830 return PTR_ERR(sleb);
831 /*
832 * Note, the below function will recover this log LEB only if
833 * it is the last, because unclean reboots can possibly corrupt
834 * only the tail of the log.
835 */
836 sleb = ubifs_recover_log_leb(c, lnum, offs, sbuf);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100837 if (IS_ERR(sleb))
838 return PTR_ERR(sleb);
839 }
840
841 if (sleb->nodes_cnt == 0) {
842 err = 1;
843 goto out;
844 }
845
846 node = sleb->buf;
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100847 snod = list_entry(sleb->nodes.next, struct ubifs_scan_node, list);
848 if (c->cs_sqnum == 0) {
849 /*
850 * This is the first log LEB we are looking at, make sure that
851 * the first node is a commit start node. Also record its
852 * sequence number so that UBIFS can determine where the log
853 * ends, because all nodes which were have higher sequence
854 * numbers.
855 */
856 if (snod->type != UBIFS_CS_NODE) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200857 ubifs_err(c, "first log node at LEB %d:%d is not CS node",
Heiko Schocherff94bc42014-06-24 10:10:04 +0200858 lnum, offs);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100859 goto out_dump;
860 }
861 if (le64_to_cpu(node->cmt_no) != c->cmt_no) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200862 ubifs_err(c, "first CS node at LEB %d:%d has wrong commit number %llu expected %llu",
Heiko Schocherff94bc42014-06-24 10:10:04 +0200863 lnum, offs,
864 (unsigned long long)le64_to_cpu(node->cmt_no),
865 c->cmt_no);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100866 goto out_dump;
867 }
868
869 c->cs_sqnum = le64_to_cpu(node->ch.sqnum);
870 dbg_mnt("commit start sqnum %llu", c->cs_sqnum);
871 }
872
873 if (snod->sqnum < c->cs_sqnum) {
874 /*
875 * This means that we reached end of log and now
876 * look to the older log data, which was already
877 * committed but the eraseblock was not erased (UBIFS
878 * only un-maps it). So this basically means we have to
879 * exit with "end of log" code.
880 */
881 err = 1;
882 goto out;
883 }
884
885 /* Make sure the first node sits at offset zero of the LEB */
886 if (snod->offs != 0) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200887 ubifs_err(c, "first node is not at zero offset");
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100888 goto out_dump;
889 }
890
891 list_for_each_entry(snod, &sleb->nodes, list) {
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100892 cond_resched();
893
894 if (snod->sqnum >= SQNUM_WATERMARK) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200895 ubifs_err(c, "file system's life ended");
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100896 goto out_dump;
897 }
898
899 if (snod->sqnum < c->cs_sqnum) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200900 ubifs_err(c, "bad sqnum %llu, commit sqnum %llu",
Heiko Schocherff94bc42014-06-24 10:10:04 +0200901 snod->sqnum, c->cs_sqnum);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100902 goto out_dump;
903 }
904
905 if (snod->sqnum > c->max_sqnum)
906 c->max_sqnum = snod->sqnum;
907
908 switch (snod->type) {
909 case UBIFS_REF_NODE: {
910 const struct ubifs_ref_node *ref = snod->node;
911
912 err = validate_ref(c, ref);
913 if (err == 1)
914 break; /* Already have this bud */
915 if (err)
916 goto out_dump;
917
918 err = add_replay_bud(c, le32_to_cpu(ref->lnum),
919 le32_to_cpu(ref->offs),
920 le32_to_cpu(ref->jhead),
921 snod->sqnum);
922 if (err)
923 goto out;
924
925 break;
926 }
927 case UBIFS_CS_NODE:
928 /* Make sure it sits at the beginning of LEB */
929 if (snod->offs != 0) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200930 ubifs_err(c, "unexpected node in log");
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100931 goto out_dump;
932 }
933 break;
934 default:
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200935 ubifs_err(c, "unexpected node in log");
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100936 goto out_dump;
937 }
938 }
939
940 if (sleb->endpt || c->lhead_offs >= c->leb_size) {
941 c->lhead_lnum = lnum;
942 c->lhead_offs = sleb->endpt;
943 }
944
945 err = !sleb->endpt;
946out:
947 ubifs_scan_destroy(sleb);
948 return err;
949
950out_dump:
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200951 ubifs_err(c, "log error detected while replaying the log at LEB %d:%d",
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100952 lnum, offs + snod->offs);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200953 ubifs_dump_node(c, snod->node);
Stefan Roese9eefe2a2009-03-19 15:35:05 +0100954 ubifs_scan_destroy(sleb);
955 return -EINVAL;
956}
957
958/**
959 * take_ihead - update the status of the index head in lprops to 'taken'.
960 * @c: UBIFS file-system description object
961 *
962 * This function returns the amount of free space in the index head LEB or a
963 * negative error code.
964 */
965static int take_ihead(struct ubifs_info *c)
966{
967 const struct ubifs_lprops *lp;
968 int err, free;
969
970 ubifs_get_lprops(c);
971
972 lp = ubifs_lpt_lookup_dirty(c, c->ihead_lnum);
973 if (IS_ERR(lp)) {
974 err = PTR_ERR(lp);
975 goto out;
976 }
977
978 free = lp->free;
979
980 lp = ubifs_change_lp(c, lp, LPROPS_NC, LPROPS_NC,
981 lp->flags | LPROPS_TAKEN, 0);
982 if (IS_ERR(lp)) {
983 err = PTR_ERR(lp);
984 goto out;
985 }
986
987 err = free;
988out:
989 ubifs_release_lprops(c);
990 return err;
991}
992
993/**
994 * ubifs_replay_journal - replay journal.
995 * @c: UBIFS file-system description object
996 *
997 * This function scans the journal, replays and cleans it up. It makes sure all
998 * memory data structures related to uncommitted journal are built (dirty TNC
999 * tree, tree of buds, modified lprops, etc).
1000 */
1001int ubifs_replay_journal(struct ubifs_info *c)
1002{
Heiko Schocherff94bc42014-06-24 10:10:04 +02001003 int err, lnum, free;
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001004
1005 BUILD_BUG_ON(UBIFS_TRUN_KEY > 5);
1006
1007 /* Update the status of the index head in lprops to 'taken' */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001008 free = take_ihead(c);
1009 if (free < 0)
1010 return free; /* Error code */
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001011
Heiko Schocherff94bc42014-06-24 10:10:04 +02001012 if (c->ihead_offs != c->leb_size - free) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001013 ubifs_err(c, "bad index head LEB %d:%d", c->ihead_lnum,
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001014 c->ihead_offs);
1015 return -EINVAL;
1016 }
1017
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001018 dbg_mnt("start replaying the journal");
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001019 c->replaying = 1;
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001020 lnum = c->ltail_lnum = c->lhead_lnum;
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001021
Heiko Schocherff94bc42014-06-24 10:10:04 +02001022 do {
1023 err = replay_log_leb(c, lnum, 0, c->sbuf);
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001024 if (err == 1) {
1025 if (lnum != c->lhead_lnum)
1026 /* We hit the end of the log */
1027 break;
1028
1029 /*
1030 * The head of the log must always start with the
1031 * "commit start" node on a properly formatted UBIFS.
1032 * But we found no nodes at all, which means that
1033 * someting went wrong and we cannot proceed mounting
1034 * the file-system.
1035 */
1036 ubifs_err(c, "no UBIFS nodes found at the log head LEB %d:%d, possibly corrupted",
1037 lnum, 0);
1038 err = -EINVAL;
1039 }
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001040 if (err)
1041 goto out;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001042 lnum = ubifs_next_log_lnum(c, lnum);
1043 } while (lnum != c->ltail_lnum);
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001044
1045 err = replay_buds(c);
1046 if (err)
1047 goto out;
1048
Heiko Schocherff94bc42014-06-24 10:10:04 +02001049 err = apply_replay_list(c);
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001050 if (err)
1051 goto out;
1052
Heiko Schocherff94bc42014-06-24 10:10:04 +02001053 err = set_buds_lprops(c);
1054 if (err)
1055 goto out;
1056
1057 /*
1058 * UBIFS budgeting calculations use @c->bi.uncommitted_idx variable
1059 * to roughly estimate index growth. Things like @c->bi.min_idx_lebs
1060 * depend on it. This means we have to initialize it to make sure
1061 * budgeting works properly.
1062 */
1063 c->bi.uncommitted_idx = atomic_long_read(&c->dirty_zn_cnt);
1064 c->bi.uncommitted_idx *= c->max_idx_node_sz;
1065
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001066 ubifs_assert(c->bud_bytes <= c->max_bud_bytes || c->need_recovery);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001067 dbg_mnt("finished, log head LEB %d:%d, max_sqnum %llu, highest_inum %lu",
1068 c->lhead_lnum, c->lhead_offs, c->max_sqnum,
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001069 (unsigned long)c->highest_inum);
1070out:
Heiko Schocherff94bc42014-06-24 10:10:04 +02001071 destroy_replay_list(c);
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001072 destroy_bud_list(c);
Stefan Roese9eefe2a2009-03-19 15:35:05 +01001073 c->replaying = 0;
1074 return err;
1075}