UBIFS: synchronize write-buffer before switching to the next bud
[deliverable/linux.git] / fs / ubifs / replay.c
CommitLineData
1e51764a
AB
1/*
2 * This file is part of UBIFS.
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation.
5 *
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.
9 *
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
13 * more details.
14 *
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
18 *
19 * Authors: Adrian Hunter
20 * Artem Bityutskiy (Битюцкий Артём)
21 */
22
23/*
24 * This file contains journal replay code. It runs when the file-system is being
25 * mounted and requires no locking.
26 *
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.
33 */
34
35#include "ubifs.h"
debf12d5 36#include <linux/list_sort.h>
1e51764a 37
1e51764a 38/**
debf12d5 39 * struct replay_entry - replay list entry.
1e51764a
AB
40 * @lnum: logical eraseblock number of the node
41 * @offs: node offset
42 * @len: node length
074bcb9b 43 * @deletion: non-zero if this entry corresponds to a node deletion
1e51764a 44 * @sqnum: node sequence number
debf12d5 45 * @list: links the replay list
1e51764a
AB
46 * @key: node key
47 * @nm: directory entry name
48 * @old_size: truncation old size
49 * @new_size: truncation new size
1e51764a 50 *
debf12d5
AB
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.
1e51764a
AB
54 */
55struct replay_entry {
56 int lnum;
57 int offs;
58 int len;
074bcb9b 59 unsigned int deletion:1;
1e51764a 60 unsigned long long sqnum;
debf12d5 61 struct list_head list;
1e51764a
AB
62 union ubifs_key key;
63 union {
64 struct qstr nm;
65 struct {
66 loff_t old_size;
67 loff_t new_size;
68 };
1e51764a
AB
69 };
70};
71
72/**
73 * struct bud_entry - entry in the list of buds to replay.
74 * @list: next bud in the list
75 * @bud: bud description object
1e51764a 76 * @sqnum: reference node sequence number
af1dd412
AB
77 * @free: free bytes in the bud
78 * @dirty: dirty bytes in the bud
1e51764a
AB
79 */
80struct bud_entry {
81 struct list_head list;
82 struct ubifs_bud *bud;
1e51764a 83 unsigned long long sqnum;
af1dd412
AB
84 int free;
85 int dirty;
1e51764a
AB
86};
87
88/**
89 * set_bud_lprops - set free and dirty space used by a bud.
90 * @c: UBIFS file-system description object
074bcb9b
AB
91 * @b: bud entry which describes the bud
92 *
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
95 * in case of failure.
1e51764a 96 */
074bcb9b 97static int set_bud_lprops(struct ubifs_info *c, struct bud_entry *b)
1e51764a
AB
98{
99 const struct ubifs_lprops *lp;
100 int err = 0, dirty;
101
102 ubifs_get_lprops(c);
103
074bcb9b 104 lp = ubifs_lpt_lookup_dirty(c, b->bud->lnum);
1e51764a
AB
105 if (IS_ERR(lp)) {
106 err = PTR_ERR(lp);
107 goto out;
108 }
109
110 dirty = lp->dirty;
074bcb9b 111 if (b->bud->start == 0 && (lp->free != c->leb_size || lp->dirty != 0)) {
1e51764a
AB
112 /*
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
074bcb9b
AB
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
7a9c3e39
AB
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.
1e51764a
AB
122 *
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
074bcb9b 125 * @b->free.
1e51764a
AB
126 *
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.
130 */
074bcb9b 131 dbg_mnt("bud LEB %d was GC'd (%d free, %d dirty)", b->bud->lnum,
1e51764a 132 lp->free, lp->dirty);
074bcb9b 133 dbg_gc("bud LEB %d was GC'd (%d free, %d dirty)", b->bud->lnum,
1e51764a
AB
134 lp->free, lp->dirty);
135 dirty -= c->leb_size - lp->free;
136 /*
137 * If the replay order was perfect the dirty space would now be
7d4e9ccb 138 * zero. The order is not perfect because the journal heads
6edbfafd 139 * race with each other. This is not a problem but is does mean
1e51764a
AB
140 * that the dirty space may temporarily exceed c->leb_size
141 * during the replay.
142 */
143 if (dirty != 0)
144 dbg_msg("LEB %d lp: %d free %d dirty "
074bcb9b
AB
145 "replay: %d free %d dirty", b->bud->lnum,
146 lp->free, lp->dirty, b->free, b->dirty);
1e51764a 147 }
074bcb9b 148 lp = ubifs_change_lp(c, lp, b->free, dirty + b->dirty,
1e51764a
AB
149 lp->flags | LPROPS_TAKEN, 0);
150 if (IS_ERR(lp)) {
151 err = PTR_ERR(lp);
152 goto out;
153 }
52c6e6f9
AB
154
155 /* Make sure the journal head points to the latest bud */
074bcb9b
AB
156 err = ubifs_wbuf_seek_nolock(&c->jheads[b->bud->jhead].wbuf,
157 b->bud->lnum, c->leb_size - b->free,
158 UBI_SHORTTERM);
52c6e6f9 159
1e51764a
AB
160out:
161 ubifs_release_lprops(c);
162 return err;
163}
164
074bcb9b
AB
165/**
166 * set_buds_lprops - set free and dirty space for all replayed buds.
167 * @c: UBIFS file-system description object
168 *
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.
171 */
172static int set_buds_lprops(struct ubifs_info *c)
173{
174 struct bud_entry *b;
175 int err;
176
177 list_for_each_entry(b, &c->replay_buds, list) {
178 err = set_bud_lprops(c, b);
179 if (err)
180 return err;
181 }
182
183 return 0;
184}
185
1e51764a
AB
186/**
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
190 */
191static int trun_remove_range(struct ubifs_info *c, struct replay_entry *r)
192{
193 unsigned min_blk, max_blk;
194 union ubifs_key min_key, max_key;
195 ino_t ino;
196
197 min_blk = r->new_size / UBIFS_BLOCK_SIZE;
198 if (r->new_size & (UBIFS_BLOCK_SIZE - 1))
199 min_blk += 1;
200
201 max_blk = r->old_size / UBIFS_BLOCK_SIZE;
202 if ((r->old_size & (UBIFS_BLOCK_SIZE - 1)) == 0)
203 max_blk -= 1;
204
205 ino = key_inum(c, &r->key);
206
207 data_key_init(c, &min_key, ino, min_blk);
208 data_key_init(c, &max_key, ino, max_blk);
209
210 return ubifs_tnc_remove_range(c, &min_key, &max_key);
211}
212
213/**
214 * apply_replay_entry - apply a replay entry to the TNC.
215 * @c: UBIFS file-system description object
216 * @r: replay entry to apply
217 *
218 * Apply a replay entry to the TNC.
219 */
220static int apply_replay_entry(struct ubifs_info *c, struct replay_entry *r)
221{
074bcb9b 222 int err;
1e51764a 223
074bcb9b
AB
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));
1e51764a
AB
226
227 /* Set c->replay_sqnum to help deal with dangling branches. */
228 c->replay_sqnum = r->sqnum;
229
074bcb9b
AB
230 if (is_hash_key(c, &r->key)) {
231 if (r->deletion)
1e51764a
AB
232 err = ubifs_tnc_remove_nm(c, &r->key, &r->nm);
233 else
234 err = ubifs_tnc_add_nm(c, &r->key, r->lnum, r->offs,
235 r->len, &r->nm);
236 } else {
074bcb9b 237 if (r->deletion)
1e51764a
AB
238 switch (key_type(c, &r->key)) {
239 case UBIFS_INO_KEY:
240 {
241 ino_t inum = key_inum(c, &r->key);
242
243 err = ubifs_tnc_remove_ino(c, inum);
244 break;
245 }
246 case UBIFS_TRUN_KEY:
247 err = trun_remove_range(c, r);
248 break;
249 default:
250 err = ubifs_tnc_remove(c, &r->key);
251 break;
252 }
253 else
254 err = ubifs_tnc_add(c, &r->key, r->lnum, r->offs,
255 r->len);
256 if (err)
257 return err;
258
259 if (c->need_recovery)
074bcb9b 260 err = ubifs_recover_size_accum(c, &r->key, r->deletion,
1e51764a
AB
261 r->new_size);
262 }
263
264 return err;
265}
266
267/**
debf12d5
AB
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
1e51764a 272 *
debf12d5
AB
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.
1e51764a 276 */
debf12d5
AB
277static int replay_entries_cmp(void *priv, struct list_head *a,
278 struct list_head *b)
1e51764a 279{
debf12d5
AB
280 struct replay_entry *ra, *rb;
281
282 cond_resched();
283 if (a == b)
284 return 0;
285
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)
290 return 1;
291 return -1;
1e51764a
AB
292}
293
294/**
debf12d5 295 * apply_replay_list - apply the replay list to the TNC.
1e51764a
AB
296 * @c: UBIFS file-system description object
297 *
debf12d5
AB
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.
1e51764a 300 */
debf12d5 301static int apply_replay_list(struct ubifs_info *c)
1e51764a 302{
debf12d5
AB
303 struct replay_entry *r;
304 int err;
1e51764a 305
debf12d5 306 list_sort(c, &c->replay_list, &replay_entries_cmp);
1e51764a 307
debf12d5 308 list_for_each_entry(r, &c->replay_list, list) {
1e51764a
AB
309 cond_resched();
310
1e51764a
AB
311 err = apply_replay_entry(c, r);
312 if (err)
313 return err;
1e51764a 314 }
debf12d5 315
1e51764a
AB
316 return 0;
317}
318
319/**
debf12d5
AB
320 * destroy_replay_list - destroy the replay.
321 * @c: UBIFS file-system description object
322 *
323 * Destroy the replay list.
324 */
325static void destroy_replay_list(struct ubifs_info *c)
326{
327 struct replay_entry *r, *tmp;
328
329 list_for_each_entry_safe(r, tmp, &c->replay_list, list) {
330 if (is_hash_key(c, &r->key))
331 kfree(r->nm.name);
332 list_del(&r->list);
333 kfree(r);
334 }
335}
336
337/**
338 * insert_node - insert a node to the replay list
1e51764a
AB
339 * @c: UBIFS file-system description object
340 * @lnum: node logical eraseblock number
341 * @offs: node offset
342 * @len: node length
343 * @key: node key
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
349 *
debf12d5
AB
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.
1e51764a
AB
356 */
357static 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,
360 loff_t new_size)
361{
1e51764a
AB
362 struct replay_entry *r;
363
debf12d5
AB
364 dbg_mnt("add LEB %d:%d, key %s", lnum, offs, DBGKEY(key));
365
1e51764a
AB
366 if (key_inum(c, key) >= c->highest_inum)
367 c->highest_inum = key_inum(c, key);
368
1e51764a
AB
369 r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL);
370 if (!r)
371 return -ENOMEM;
372
373 if (!deletion)
374 *used += ALIGN(len, 8);
375 r->lnum = lnum;
376 r->offs = offs;
377 r->len = len;
074bcb9b 378 r->deletion = !!deletion;
1e51764a 379 r->sqnum = sqnum;
074bcb9b 380 key_copy(c, key, &r->key);
1e51764a
AB
381 r->old_size = old_size;
382 r->new_size = new_size;
1e51764a 383
debf12d5 384 list_add_tail(&r->list, &c->replay_list);
1e51764a
AB
385 return 0;
386}
387
388/**
debf12d5 389 * insert_dent - insert a directory entry node into the replay list.
1e51764a
AB
390 * @c: UBIFS file-system description object
391 * @lnum: node logical eraseblock number
392 * @offs: node offset
393 * @len: node length
394 * @key: node key
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
400 *
debf12d5
AB
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.
1e51764a
AB
404 */
405static 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)
408{
1e51764a
AB
409 struct replay_entry *r;
410 char *nbuf;
411
debf12d5 412 dbg_mnt("add LEB %d:%d, key %s", lnum, offs, DBGKEY(key));
1e51764a
AB
413 if (key_inum(c, key) >= c->highest_inum)
414 c->highest_inum = key_inum(c, key);
415
1e51764a
AB
416 r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL);
417 if (!r)
418 return -ENOMEM;
debf12d5 419
1e51764a
AB
420 nbuf = kmalloc(nlen + 1, GFP_KERNEL);
421 if (!nbuf) {
422 kfree(r);
423 return -ENOMEM;
424 }
425
426 if (!deletion)
427 *used += ALIGN(len, 8);
428 r->lnum = lnum;
429 r->offs = offs;
430 r->len = len;
074bcb9b 431 r->deletion = !!deletion;
1e51764a 432 r->sqnum = sqnum;
074bcb9b 433 key_copy(c, key, &r->key);
1e51764a
AB
434 r->nm.len = nlen;
435 memcpy(nbuf, name, nlen);
436 nbuf[nlen] = '\0';
437 r->nm.name = nbuf;
1e51764a 438
debf12d5 439 list_add_tail(&r->list, &c->replay_list);
1e51764a
AB
440 return 0;
441}
442
443/**
444 * ubifs_validate_entry - validate directory or extended attribute entry node.
445 * @c: UBIFS file-system description object
446 * @dent: the node to validate
447 *
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.
450 */
451int ubifs_validate_entry(struct ubifs_info *c,
452 const struct ubifs_dent_node *dent)
453{
454 int key_type = key_type_flash(c, dent->key);
455 int nlen = le16_to_cpu(dent->nlen);
456
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");
464 return -EINVAL;
465 }
466
467 if (key_type != UBIFS_DENT_KEY && key_type != UBIFS_XENT_KEY) {
468 ubifs_err("bad key type %d", key_type);
469 return -EINVAL;
470 }
471
472 return 0;
473}
474
475/**
476 * replay_bud - replay a bud logical eraseblock.
477 * @c: UBIFS file-system description object
e76a4526 478 * @b: bud entry which describes the bud
1e51764a 479 *
e76a4526
AB
480 * This function replays bud @bud, recovers it if needed, and adds all nodes
481 * from this bud to the replay list. Returns zero in case of success and a
482 * negative error code in case of failure.
1e51764a 483 */
e76a4526 484static int replay_bud(struct ubifs_info *c, struct bud_entry *b)
1e51764a 485{
e76a4526
AB
486 int err = 0, used = 0, lnum = b->bud->lnum, offs = b->bud->start;
487 int jhead = b->bud->jhead;
1e51764a
AB
488 struct ubifs_scan_leb *sleb;
489 struct ubifs_scan_node *snod;
1e51764a 490
c839e297 491 dbg_mnt("replay bud LEB %d, head %d, offs %d", lnum, jhead, offs);
e76a4526 492
1e51764a
AB
493 if (c->need_recovery)
494 sleb = ubifs_recover_leb(c, lnum, offs, c->sbuf, jhead != GCHD);
495 else
348709ba 496 sleb = ubifs_scan(c, lnum, offs, c->sbuf, 0);
1e51764a
AB
497 if (IS_ERR(sleb))
498 return PTR_ERR(sleb);
499
500 /*
501 * The bud does not have to start from offset zero - the beginning of
502 * the 'lnum' LEB may contain previously committed data. One of the
503 * things we have to do in replay is to correctly update lprops with
504 * newer information about this LEB.
505 *
506 * At this point lprops thinks that this LEB has 'c->leb_size - offs'
507 * bytes of free space because it only contain information about
508 * committed data.
509 *
510 * But we know that real amount of free space is 'c->leb_size -
511 * sleb->endpt', and the space in the 'lnum' LEB between 'offs' and
512 * 'sleb->endpt' is used by bud data. We have to correctly calculate
513 * how much of these data are dirty and update lprops with this
514 * information.
515 *
516 * The dirt in that LEB region is comprised of padding nodes, deletion
517 * nodes, truncation nodes and nodes which are obsoleted by subsequent
518 * nodes in this LEB. So instead of calculating clean space, we
519 * calculate used space ('used' variable).
520 */
521
522 list_for_each_entry(snod, &sleb->nodes, list) {
523 int deletion = 0;
524
525 cond_resched();
526
527 if (snod->sqnum >= SQNUM_WATERMARK) {
528 ubifs_err("file system's life ended");
529 goto out_dump;
530 }
531
532 if (snod->sqnum > c->max_sqnum)
533 c->max_sqnum = snod->sqnum;
534
535 switch (snod->type) {
536 case UBIFS_INO_NODE:
537 {
538 struct ubifs_ino_node *ino = snod->node;
539 loff_t new_size = le64_to_cpu(ino->size);
540
541 if (le32_to_cpu(ino->nlink) == 0)
542 deletion = 1;
543 err = insert_node(c, lnum, snod->offs, snod->len,
544 &snod->key, snod->sqnum, deletion,
545 &used, 0, new_size);
546 break;
547 }
548 case UBIFS_DATA_NODE:
549 {
550 struct ubifs_data_node *dn = snod->node;
551 loff_t new_size = le32_to_cpu(dn->size) +
552 key_block(c, &snod->key) *
553 UBIFS_BLOCK_SIZE;
554
555 err = insert_node(c, lnum, snod->offs, snod->len,
556 &snod->key, snod->sqnum, deletion,
557 &used, 0, new_size);
558 break;
559 }
560 case UBIFS_DENT_NODE:
561 case UBIFS_XENT_NODE:
562 {
563 struct ubifs_dent_node *dent = snod->node;
564
565 err = ubifs_validate_entry(c, dent);
566 if (err)
567 goto out_dump;
568
569 err = insert_dent(c, lnum, snod->offs, snod->len,
570 &snod->key, dent->name,
571 le16_to_cpu(dent->nlen), snod->sqnum,
572 !le64_to_cpu(dent->inum), &used);
573 break;
574 }
575 case UBIFS_TRUN_NODE:
576 {
577 struct ubifs_trun_node *trun = snod->node;
578 loff_t old_size = le64_to_cpu(trun->old_size);
579 loff_t new_size = le64_to_cpu(trun->new_size);
580 union ubifs_key key;
581
582 /* Validate truncation node */
583 if (old_size < 0 || old_size > c->max_inode_sz ||
584 new_size < 0 || new_size > c->max_inode_sz ||
585 old_size <= new_size) {
586 ubifs_err("bad truncation node");
587 goto out_dump;
588 }
589
590 /*
591 * Create a fake truncation key just to use the same
592 * functions which expect nodes to have keys.
593 */
594 trun_key_init(c, &key, le32_to_cpu(trun->inum));
595 err = insert_node(c, lnum, snod->offs, snod->len,
596 &key, snod->sqnum, 1, &used,
597 old_size, new_size);
598 break;
599 }
600 default:
601 ubifs_err("unexpected node type %d in bud LEB %d:%d",
602 snod->type, lnum, snod->offs);
603 err = -EINVAL;
604 goto out_dump;
605 }
606 if (err)
607 goto out;
608 }
609
c49139d8 610 ubifs_assert(ubifs_search_bud(c, lnum));
1e51764a
AB
611 ubifs_assert(sleb->endpt - offs >= used);
612 ubifs_assert(sleb->endpt % c->min_io_size == 0);
613
e76a4526
AB
614 b->dirty = sleb->endpt - offs - used;
615 b->free = c->leb_size - sleb->endpt;
616 dbg_mnt("bud LEB %d replied: dirty %d, free %d", lnum, b->dirty, b->free);
1e51764a
AB
617
618out:
619 ubifs_scan_destroy(sleb);
620 return err;
621
622out_dump:
623 ubifs_err("bad node is at LEB %d:%d", lnum, snod->offs);
624 dbg_dump_node(c, snod->node);
625 ubifs_scan_destroy(sleb);
626 return -EINVAL;
627}
628
1e51764a
AB
629/**
630 * replay_buds - replay all buds.
631 * @c: UBIFS file-system description object
632 *
633 * This function returns zero in case of success and a negative error code in
634 * case of failure.
635 */
636static int replay_buds(struct ubifs_info *c)
637{
638 struct bud_entry *b;
074bcb9b 639 int err;
7703f09d 640 unsigned long long prev_sqnum = 0;
1e51764a
AB
641
642 list_for_each_entry(b, &c->replay_buds, list) {
e76a4526 643 err = replay_bud(c, b);
1e51764a
AB
644 if (err)
645 return err;
7703f09d
AB
646
647 ubifs_assert(b->sqnum > prev_sqnum);
648 prev_sqnum = b->sqnum;
1e51764a
AB
649 }
650
651 return 0;
652}
653
654/**
655 * destroy_bud_list - destroy the list of buds to replay.
656 * @c: UBIFS file-system description object
657 */
658static void destroy_bud_list(struct ubifs_info *c)
659{
660 struct bud_entry *b;
661
662 while (!list_empty(&c->replay_buds)) {
663 b = list_entry(c->replay_buds.next, struct bud_entry, list);
664 list_del(&b->list);
665 kfree(b);
666 }
667}
668
669/**
670 * add_replay_bud - add a bud to the list of buds to replay.
671 * @c: UBIFS file-system description object
672 * @lnum: bud logical eraseblock number to replay
673 * @offs: bud start offset
674 * @jhead: journal head to which this bud belongs
675 * @sqnum: reference node sequence number
676 *
677 * This function returns zero in case of success and a negative error code in
678 * case of failure.
679 */
680static int add_replay_bud(struct ubifs_info *c, int lnum, int offs, int jhead,
681 unsigned long long sqnum)
682{
683 struct ubifs_bud *bud;
684 struct bud_entry *b;
685
686 dbg_mnt("add replay bud LEB %d:%d, head %d", lnum, offs, jhead);
687
688 bud = kmalloc(sizeof(struct ubifs_bud), GFP_KERNEL);
689 if (!bud)
690 return -ENOMEM;
691
692 b = kmalloc(sizeof(struct bud_entry), GFP_KERNEL);
693 if (!b) {
694 kfree(bud);
695 return -ENOMEM;
696 }
697
698 bud->lnum = lnum;
699 bud->start = offs;
700 bud->jhead = jhead;
701 ubifs_add_bud(c, bud);
702
703 b->bud = bud;
704 b->sqnum = sqnum;
705 list_add_tail(&b->list, &c->replay_buds);
706
707 return 0;
708}
709
710/**
711 * validate_ref - validate a reference node.
712 * @c: UBIFS file-system description object
713 * @ref: the reference node to validate
714 * @ref_lnum: LEB number of the reference node
715 * @ref_offs: reference node offset
716 *
717 * This function returns %1 if a bud reference already exists for the LEB. %0 is
718 * returned if the reference node is new, otherwise %-EINVAL is returned if
719 * validation failed.
720 */
721static int validate_ref(struct ubifs_info *c, const struct ubifs_ref_node *ref)
722{
723 struct ubifs_bud *bud;
724 int lnum = le32_to_cpu(ref->lnum);
725 unsigned int offs = le32_to_cpu(ref->offs);
726 unsigned int jhead = le32_to_cpu(ref->jhead);
727
728 /*
729 * ref->offs may point to the end of LEB when the journal head points
730 * to the end of LEB and we write reference node for it during commit.
731 * So this is why we require 'offs > c->leb_size'.
732 */
733 if (jhead >= c->jhead_cnt || lnum >= c->leb_cnt ||
734 lnum < c->main_first || offs > c->leb_size ||
735 offs & (c->min_io_size - 1))
736 return -EINVAL;
737
738 /* Make sure we have not already looked at this bud */
739 bud = ubifs_search_bud(c, lnum);
740 if (bud) {
741 if (bud->jhead == jhead && bud->start <= offs)
742 return 1;
743 ubifs_err("bud at LEB %d:%d was already referred", lnum, offs);
744 return -EINVAL;
745 }
746
747 return 0;
748}
749
750/**
751 * replay_log_leb - replay a log logical eraseblock.
752 * @c: UBIFS file-system description object
753 * @lnum: log logical eraseblock to replay
754 * @offs: offset to start replaying from
755 * @sbuf: scan buffer
756 *
757 * This function replays a log LEB and returns zero in case of success, %1 if
758 * this is the last LEB in the log, and a negative error code in case of
759 * failure.
760 */
761static int replay_log_leb(struct ubifs_info *c, int lnum, int offs, void *sbuf)
762{
763 int err;
764 struct ubifs_scan_leb *sleb;
765 struct ubifs_scan_node *snod;
766 const struct ubifs_cs_node *node;
767
768 dbg_mnt("replay log LEB %d:%d", lnum, offs);
348709ba
AB
769 sleb = ubifs_scan(c, lnum, offs, sbuf, c->need_recovery);
770 if (IS_ERR(sleb)) {
ed43f2f0
AB
771 if (PTR_ERR(sleb) != -EUCLEAN || !c->need_recovery)
772 return PTR_ERR(sleb);
7d08ae3c
AB
773 /*
774 * Note, the below function will recover this log LEB only if
775 * it is the last, because unclean reboots can possibly corrupt
776 * only the tail of the log.
777 */
ed43f2f0 778 sleb = ubifs_recover_log_leb(c, lnum, offs, sbuf);
1e51764a
AB
779 if (IS_ERR(sleb))
780 return PTR_ERR(sleb);
781 }
782
783 if (sleb->nodes_cnt == 0) {
784 err = 1;
785 goto out;
786 }
787
788 node = sleb->buf;
1e51764a
AB
789 snod = list_entry(sleb->nodes.next, struct ubifs_scan_node, list);
790 if (c->cs_sqnum == 0) {
791 /*
792 * This is the first log LEB we are looking at, make sure that
793 * the first node is a commit start node. Also record its
794 * sequence number so that UBIFS can determine where the log
795 * ends, because all nodes which were have higher sequence
796 * numbers.
797 */
798 if (snod->type != UBIFS_CS_NODE) {
799 dbg_err("first log node at LEB %d:%d is not CS node",
800 lnum, offs);
801 goto out_dump;
802 }
803 if (le64_to_cpu(node->cmt_no) != c->cmt_no) {
804 dbg_err("first CS node at LEB %d:%d has wrong "
805 "commit number %llu expected %llu",
806 lnum, offs,
807 (unsigned long long)le64_to_cpu(node->cmt_no),
808 c->cmt_no);
809 goto out_dump;
810 }
811
812 c->cs_sqnum = le64_to_cpu(node->ch.sqnum);
813 dbg_mnt("commit start sqnum %llu", c->cs_sqnum);
814 }
815
816 if (snod->sqnum < c->cs_sqnum) {
817 /*
818 * This means that we reached end of log and now
819 * look to the older log data, which was already
820 * committed but the eraseblock was not erased (UBIFS
6edbfafd 821 * only un-maps it). So this basically means we have to
1e51764a
AB
822 * exit with "end of log" code.
823 */
824 err = 1;
825 goto out;
826 }
827
828 /* Make sure the first node sits at offset zero of the LEB */
829 if (snod->offs != 0) {
830 dbg_err("first node is not at zero offset");
831 goto out_dump;
832 }
833
834 list_for_each_entry(snod, &sleb->nodes, list) {
1e51764a
AB
835 cond_resched();
836
837 if (snod->sqnum >= SQNUM_WATERMARK) {
838 ubifs_err("file system's life ended");
839 goto out_dump;
840 }
841
842 if (snod->sqnum < c->cs_sqnum) {
843 dbg_err("bad sqnum %llu, commit sqnum %llu",
844 snod->sqnum, c->cs_sqnum);
845 goto out_dump;
846 }
847
848 if (snod->sqnum > c->max_sqnum)
849 c->max_sqnum = snod->sqnum;
850
851 switch (snod->type) {
852 case UBIFS_REF_NODE: {
853 const struct ubifs_ref_node *ref = snod->node;
854
855 err = validate_ref(c, ref);
856 if (err == 1)
857 break; /* Already have this bud */
858 if (err)
859 goto out_dump;
860
861 err = add_replay_bud(c, le32_to_cpu(ref->lnum),
862 le32_to_cpu(ref->offs),
863 le32_to_cpu(ref->jhead),
864 snod->sqnum);
865 if (err)
866 goto out;
867
868 break;
869 }
870 case UBIFS_CS_NODE:
871 /* Make sure it sits at the beginning of LEB */
872 if (snod->offs != 0) {
873 ubifs_err("unexpected node in log");
874 goto out_dump;
875 }
876 break;
877 default:
878 ubifs_err("unexpected node in log");
879 goto out_dump;
880 }
881 }
882
883 if (sleb->endpt || c->lhead_offs >= c->leb_size) {
884 c->lhead_lnum = lnum;
885 c->lhead_offs = sleb->endpt;
886 }
887
888 err = !sleb->endpt;
889out:
890 ubifs_scan_destroy(sleb);
891 return err;
892
893out_dump:
681947d2 894 ubifs_err("log error detected while replaying the log at LEB %d:%d",
1e51764a
AB
895 lnum, offs + snod->offs);
896 dbg_dump_node(c, snod->node);
897 ubifs_scan_destroy(sleb);
898 return -EINVAL;
899}
900
901/**
902 * take_ihead - update the status of the index head in lprops to 'taken'.
903 * @c: UBIFS file-system description object
904 *
905 * This function returns the amount of free space in the index head LEB or a
906 * negative error code.
907 */
908static int take_ihead(struct ubifs_info *c)
909{
910 const struct ubifs_lprops *lp;
911 int err, free;
912
913 ubifs_get_lprops(c);
914
915 lp = ubifs_lpt_lookup_dirty(c, c->ihead_lnum);
916 if (IS_ERR(lp)) {
917 err = PTR_ERR(lp);
918 goto out;
919 }
920
921 free = lp->free;
922
923 lp = ubifs_change_lp(c, lp, LPROPS_NC, LPROPS_NC,
924 lp->flags | LPROPS_TAKEN, 0);
925 if (IS_ERR(lp)) {
926 err = PTR_ERR(lp);
927 goto out;
928 }
929
930 err = free;
931out:
932 ubifs_release_lprops(c);
933 return err;
934}
935
936/**
937 * ubifs_replay_journal - replay journal.
938 * @c: UBIFS file-system description object
939 *
940 * This function scans the journal, replays and cleans it up. It makes sure all
941 * memory data structures related to uncommitted journal are built (dirty TNC
942 * tree, tree of buds, modified lprops, etc).
943 */
944int ubifs_replay_journal(struct ubifs_info *c)
945{
946 int err, i, lnum, offs, free;
1e51764a
AB
947
948 BUILD_BUG_ON(UBIFS_TRUN_KEY > 5);
949
950 /* Update the status of the index head in lprops to 'taken' */
951 free = take_ihead(c);
952 if (free < 0)
953 return free; /* Error code */
954
955 if (c->ihead_offs != c->leb_size - free) {
956 ubifs_err("bad index head LEB %d:%d", c->ihead_lnum,
957 c->ihead_offs);
958 return -EINVAL;
959 }
960
1e51764a 961 dbg_mnt("start replaying the journal");
1e51764a 962 c->replaying = 1;
1e51764a
AB
963 lnum = c->ltail_lnum = c->lhead_lnum;
964 offs = c->lhead_offs;
965
966 for (i = 0; i < c->log_lebs; i++, lnum++) {
967 if (lnum >= UBIFS_LOG_LNUM + c->log_lebs) {
968 /*
969 * The log is logically circular, we reached the last
970 * LEB, switch to the first one.
971 */
972 lnum = UBIFS_LOG_LNUM;
973 offs = 0;
974 }
6599fcbd 975 err = replay_log_leb(c, lnum, offs, c->sbuf);
1e51764a
AB
976 if (err == 1)
977 /* We hit the end of the log */
978 break;
979 if (err)
980 goto out;
981 offs = 0;
982 }
983
984 err = replay_buds(c);
985 if (err)
986 goto out;
987
debf12d5 988 err = apply_replay_list(c);
1e51764a
AB
989 if (err)
990 goto out;
991
074bcb9b
AB
992 err = set_buds_lprops(c);
993 if (err)
994 goto out;
995
6edbfafd 996 /*
b137545c
AB
997 * UBIFS budgeting calculations use @c->bi.uncommitted_idx variable
998 * to roughly estimate index growth. Things like @c->bi.min_idx_lebs
6edbfafd
AB
999 * depend on it. This means we have to initialize it to make sure
1000 * budgeting works properly.
1001 */
b137545c
AB
1002 c->bi.uncommitted_idx = atomic_long_read(&c->dirty_zn_cnt);
1003 c->bi.uncommitted_idx *= c->max_idx_node_sz;
6edbfafd 1004
1e51764a
AB
1005 ubifs_assert(c->bud_bytes <= c->max_bud_bytes || c->need_recovery);
1006 dbg_mnt("finished, log head LEB %d:%d, max_sqnum %llu, "
1007 "highest_inum %lu", c->lhead_lnum, c->lhead_offs, c->max_sqnum,
e84461ad 1008 (unsigned long)c->highest_inum);
1e51764a 1009out:
debf12d5 1010 destroy_replay_list(c);
1e51764a 1011 destroy_bud_list(c);
1e51764a
AB
1012 c->replaying = 0;
1013 return err;
1014}
This page took 0.264643 seconds and 5 git commands to generate.