blob: c6f9b2225387f630c55e622b6e0eb90246e53a96 [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001/*
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"
36#include <linux/list_sort.h>
37
38/**
39 * struct replay_entry - replay list entry.
40 * @lnum: logical eraseblock number of the node
41 * @offs: node offset
42 * @len: node length
43 * @deletion: non-zero if this entry corresponds to a node deletion
44 * @sqnum: node sequence number
45 * @list: links the replay list
46 * @key: node key
47 * @nm: directory entry name
48 * @old_size: truncation old size
49 * @new_size: truncation new size
50 *
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.
54 */
55struct replay_entry {
56 int lnum;
57 int offs;
58 int len;
59 unsigned int deletion:1;
60 unsigned long long sqnum;
61 struct list_head list;
62 union ubifs_key key;
63 union {
64 struct fscrypt_name nm;
65 struct {
66 loff_t old_size;
67 loff_t new_size;
68 };
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
76 * @sqnum: reference node sequence number
77 * @free: free bytes in the bud
78 * @dirty: dirty bytes in the bud
79 */
80struct bud_entry {
81 struct list_head list;
82 struct ubifs_bud *bud;
83 unsigned long long sqnum;
84 int free;
85 int dirty;
86};
87
88/**
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
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.
96 */
97static int set_bud_lprops(struct ubifs_info *c, struct bud_entry *b)
98{
99 const struct ubifs_lprops *lp;
100 int err = 0, dirty;
101
102 ubifs_get_lprops(c);
103
104 lp = ubifs_lpt_lookup_dirty(c, b->bud->lnum);
105 if (IS_ERR(lp)) {
106 err = PTR_ERR(lp);
107 goto out;
108 }
109
110 dirty = lp->dirty;
111 if (b->bud->start == 0 && (lp->free != c->leb_size || lp->dirty != 0)) {
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
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.
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
125 * @b->free.
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 */
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;
136 /*
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
141 * during the replay.
142 */
143 if (dirty != 0)
144 dbg_mnt("LEB %d lp: %d free %d dirty replay: %d free %d dirty",
145 b->bud->lnum, lp->free, lp->dirty, b->free,
146 b->dirty);
147 }
148 lp = ubifs_change_lp(c, lp, b->free, dirty + b->dirty,
149 lp->flags | LPROPS_TAKEN, 0);
150 if (IS_ERR(lp)) {
151 err = PTR_ERR(lp);
152 goto out;
153 }
154
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);
158
159out:
160 ubifs_release_lprops(c);
161 return err;
162}
163
164/**
165 * set_buds_lprops - set free and dirty space for all replayed buds.
166 * @c: UBIFS file-system description object
167 *
168 * This function sets LEB properties for all replayed buds. Returns zero in
169 * case of success and a negative error code in case of failure.
170 */
171static int set_buds_lprops(struct ubifs_info *c)
172{
173 struct bud_entry *b;
174 int err;
175
176 list_for_each_entry(b, &c->replay_buds, list) {
177 err = set_bud_lprops(c, b);
178 if (err)
179 return err;
180 }
181
182 return 0;
183}
184
185/**
186 * trun_remove_range - apply a replay entry for a truncation to the TNC.
187 * @c: UBIFS file-system description object
188 * @r: replay entry of truncation
189 */
190static int trun_remove_range(struct ubifs_info *c, struct replay_entry *r)
191{
192 unsigned min_blk, max_blk;
193 union ubifs_key min_key, max_key;
194 ino_t ino;
195
196 min_blk = r->new_size / UBIFS_BLOCK_SIZE;
197 if (r->new_size & (UBIFS_BLOCK_SIZE - 1))
198 min_blk += 1;
199
200 max_blk = r->old_size / UBIFS_BLOCK_SIZE;
201 if ((r->old_size & (UBIFS_BLOCK_SIZE - 1)) == 0)
202 max_blk -= 1;
203
204 ino = key_inum(c, &r->key);
205
206 data_key_init(c, &min_key, ino, min_blk);
207 data_key_init(c, &max_key, ino, max_blk);
208
209 return ubifs_tnc_remove_range(c, &min_key, &max_key);
210}
211
212/**
213 * inode_still_linked - check whether inode in question will be re-linked.
214 * @c: UBIFS file-system description object
215 * @rino: replay entry to test
216 *
217 * O_TMPFILE files can be re-linked, this means link count goes from 0 to 1.
218 * This case needs special care, otherwise all references to the inode will
219 * be removed upon the first replay entry of an inode with link count 0
220 * is found.
221 */
222static bool inode_still_linked(struct ubifs_info *c, struct replay_entry *rino)
223{
224 struct replay_entry *r;
225
226 ubifs_assert(c, rino->deletion);
227 ubifs_assert(c, key_type(c, &rino->key) == UBIFS_INO_KEY);
228
229 /*
230 * Find the most recent entry for the inode behind @rino and check
231 * whether it is a deletion.
232 */
233 list_for_each_entry_reverse(r, &c->replay_list, list) {
234 ubifs_assert(c, r->sqnum >= rino->sqnum);
235 if (key_inum(c, &r->key) == key_inum(c, &rino->key))
236 return r->deletion == 0;
237
238 }
239
240 ubifs_assert(c, 0);
241 return false;
242}
243
244/**
245 * apply_replay_entry - apply a replay entry to the TNC.
246 * @c: UBIFS file-system description object
247 * @r: replay entry to apply
248 *
249 * Apply a replay entry to the TNC.
250 */
251static int apply_replay_entry(struct ubifs_info *c, struct replay_entry *r)
252{
253 int err;
254
255 dbg_mntk(&r->key, "LEB %d:%d len %d deletion %d sqnum %llu key ",
256 r->lnum, r->offs, r->len, r->deletion, r->sqnum);
257
258 if (is_hash_key(c, &r->key)) {
259 if (r->deletion)
260 err = ubifs_tnc_remove_nm(c, &r->key, &r->nm);
261 else
262 err = ubifs_tnc_add_nm(c, &r->key, r->lnum, r->offs,
263 r->len, &r->nm);
264 } else {
265 if (r->deletion)
266 switch (key_type(c, &r->key)) {
267 case UBIFS_INO_KEY:
268 {
269 ino_t inum = key_inum(c, &r->key);
270
271 if (inode_still_linked(c, r)) {
272 err = 0;
273 break;
274 }
275
276 err = ubifs_tnc_remove_ino(c, inum);
277 break;
278 }
279 case UBIFS_TRUN_KEY:
280 err = trun_remove_range(c, r);
281 break;
282 default:
283 err = ubifs_tnc_remove(c, &r->key);
284 break;
285 }
286 else
287 err = ubifs_tnc_add(c, &r->key, r->lnum, r->offs,
288 r->len);
289 if (err)
290 return err;
291
292 if (c->need_recovery)
293 err = ubifs_recover_size_accum(c, &r->key, r->deletion,
294 r->new_size);
295 }
296
297 return err;
298}
299
300/**
301 * replay_entries_cmp - compare 2 replay entries.
302 * @priv: UBIFS file-system description object
303 * @a: first replay entry
304 * @b: second replay entry
305 *
306 * This is a comparios function for 'list_sort()' which compares 2 replay
307 * entries @a and @b by comparing their sequence numer. Returns %1 if @a has
308 * greater sequence number and %-1 otherwise.
309 */
310static int replay_entries_cmp(void *priv, struct list_head *a,
311 struct list_head *b)
312{
313 struct ubifs_info *c = priv;
314 struct replay_entry *ra, *rb;
315
316 cond_resched();
317 if (a == b)
318 return 0;
319
320 ra = list_entry(a, struct replay_entry, list);
321 rb = list_entry(b, struct replay_entry, list);
322 ubifs_assert(c, ra->sqnum != rb->sqnum);
323 if (ra->sqnum > rb->sqnum)
324 return 1;
325 return -1;
326}
327
328/**
329 * apply_replay_list - apply the replay list to the TNC.
330 * @c: UBIFS file-system description object
331 *
332 * Apply all entries in the replay list to the TNC. Returns zero in case of
333 * success and a negative error code in case of failure.
334 */
335static int apply_replay_list(struct ubifs_info *c)
336{
337 struct replay_entry *r;
338 int err;
339
340 list_sort(c, &c->replay_list, &replay_entries_cmp);
341
342 list_for_each_entry(r, &c->replay_list, list) {
343 cond_resched();
344
345 err = apply_replay_entry(c, r);
346 if (err)
347 return err;
348 }
349
350 return 0;
351}
352
353/**
354 * destroy_replay_list - destroy the replay.
355 * @c: UBIFS file-system description object
356 *
357 * Destroy the replay list.
358 */
359static void destroy_replay_list(struct ubifs_info *c)
360{
361 struct replay_entry *r, *tmp;
362
363 list_for_each_entry_safe(r, tmp, &c->replay_list, list) {
364 if (is_hash_key(c, &r->key))
365 kfree(fname_name(&r->nm));
366 list_del(&r->list);
367 kfree(r);
368 }
369}
370
371/**
372 * insert_node - insert a node to the replay list
373 * @c: UBIFS file-system description object
374 * @lnum: node logical eraseblock number
375 * @offs: node offset
376 * @len: node length
377 * @key: node key
378 * @sqnum: sequence number
379 * @deletion: non-zero if this is a deletion
380 * @used: number of bytes in use in a LEB
381 * @old_size: truncation old size
382 * @new_size: truncation new size
383 *
384 * This function inserts a scanned non-direntry node to the replay list. The
385 * replay list contains @struct replay_entry elements, and we sort this list in
386 * sequence number order before applying it. The replay list is applied at the
387 * very end of the replay process. Since the list is sorted in sequence number
388 * order, the older modifications are applied first. This function returns zero
389 * in case of success and a negative error code in case of failure.
390 */
391static int insert_node(struct ubifs_info *c, int lnum, int offs, int len,
392 union ubifs_key *key, unsigned long long sqnum,
393 int deletion, int *used, loff_t old_size,
394 loff_t new_size)
395{
396 struct replay_entry *r;
397
398 dbg_mntk(key, "add LEB %d:%d, key ", lnum, offs);
399
400 if (key_inum(c, key) >= c->highest_inum)
401 c->highest_inum = key_inum(c, key);
402
403 r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL);
404 if (!r)
405 return -ENOMEM;
406
407 if (!deletion)
408 *used += ALIGN(len, 8);
409 r->lnum = lnum;
410 r->offs = offs;
411 r->len = len;
412 r->deletion = !!deletion;
413 r->sqnum = sqnum;
414 key_copy(c, key, &r->key);
415 r->old_size = old_size;
416 r->new_size = new_size;
417
418 list_add_tail(&r->list, &c->replay_list);
419 return 0;
420}
421
422/**
423 * insert_dent - insert a directory entry node into the replay list.
424 * @c: UBIFS file-system description object
425 * @lnum: node logical eraseblock number
426 * @offs: node offset
427 * @len: node length
428 * @key: node key
429 * @name: directory entry name
430 * @nlen: directory entry name length
431 * @sqnum: sequence number
432 * @deletion: non-zero if this is a deletion
433 * @used: number of bytes in use in a LEB
434 *
435 * This function inserts a scanned directory entry node or an extended
436 * attribute entry to the replay list. Returns zero in case of success and a
437 * negative error code in case of failure.
438 */
439static int insert_dent(struct ubifs_info *c, int lnum, int offs, int len,
440 union ubifs_key *key, const char *name, int nlen,
441 unsigned long long sqnum, int deletion, int *used)
442{
443 struct replay_entry *r;
444 char *nbuf;
445
446 dbg_mntk(key, "add LEB %d:%d, key ", lnum, offs);
447 if (key_inum(c, key) >= c->highest_inum)
448 c->highest_inum = key_inum(c, key);
449
450 r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL);
451 if (!r)
452 return -ENOMEM;
453
454 nbuf = kmalloc(nlen + 1, GFP_KERNEL);
455 if (!nbuf) {
456 kfree(r);
457 return -ENOMEM;
458 }
459
460 if (!deletion)
461 *used += ALIGN(len, 8);
462 r->lnum = lnum;
463 r->offs = offs;
464 r->len = len;
465 r->deletion = !!deletion;
466 r->sqnum = sqnum;
467 key_copy(c, key, &r->key);
468 fname_len(&r->nm) = nlen;
469 memcpy(nbuf, name, nlen);
470 nbuf[nlen] = '\0';
471 fname_name(&r->nm) = nbuf;
472
473 list_add_tail(&r->list, &c->replay_list);
474 return 0;
475}
476
477/**
478 * ubifs_validate_entry - validate directory or extended attribute entry node.
479 * @c: UBIFS file-system description object
480 * @dent: the node to validate
481 *
482 * This function validates directory or extended attribute entry node @dent.
483 * Returns zero if the node is all right and a %-EINVAL if not.
484 */
485int ubifs_validate_entry(struct ubifs_info *c,
486 const struct ubifs_dent_node *dent)
487{
488 int key_type = key_type_flash(c, dent->key);
489 int nlen = le16_to_cpu(dent->nlen);
490
491 if (le32_to_cpu(dent->ch.len) != nlen + UBIFS_DENT_NODE_SZ + 1 ||
492 dent->type >= UBIFS_ITYPES_CNT ||
493 nlen > UBIFS_MAX_NLEN || dent->name[nlen] != 0 ||
494 (key_type == UBIFS_XENT_KEY && strnlen(dent->name, nlen) != nlen) ||
495 le64_to_cpu(dent->inum) > MAX_INUM) {
496 ubifs_err(c, "bad %s node", key_type == UBIFS_DENT_KEY ?
497 "directory entry" : "extended attribute entry");
498 return -EINVAL;
499 }
500
501 if (key_type != UBIFS_DENT_KEY && key_type != UBIFS_XENT_KEY) {
502 ubifs_err(c, "bad key type %d", key_type);
503 return -EINVAL;
504 }
505
506 return 0;
507}
508
509/**
510 * is_last_bud - check if the bud is the last in the journal head.
511 * @c: UBIFS file-system description object
512 * @bud: bud description object
513 *
514 * This function checks if bud @bud is the last bud in its journal head. This
515 * information is then used by 'replay_bud()' to decide whether the bud can
516 * have corruptions or not. Indeed, only last buds can be corrupted by power
517 * cuts. Returns %1 if this is the last bud, and %0 if not.
518 */
519static int is_last_bud(struct ubifs_info *c, struct ubifs_bud *bud)
520{
521 struct ubifs_jhead *jh = &c->jheads[bud->jhead];
522 struct ubifs_bud *next;
523 uint32_t data;
524 int err;
525
526 if (list_is_last(&bud->list, &jh->buds_list))
527 return 1;
528
529 /*
530 * The following is a quirk to make sure we work correctly with UBIFS
531 * images used with older UBIFS.
532 *
533 * Normally, the last bud will be the last in the journal head's list
534 * of bud. However, there is one exception if the UBIFS image belongs
535 * to older UBIFS. This is fairly unlikely: one would need to use old
536 * UBIFS, then have a power cut exactly at the right point, and then
537 * try to mount this image with new UBIFS.
538 *
539 * The exception is: it is possible to have 2 buds A and B, A goes
540 * before B, and B is the last, bud B is contains no data, and bud A is
541 * corrupted at the end. The reason is that in older versions when the
542 * journal code switched the next bud (from A to B), it first added a
543 * log reference node for the new bud (B), and only after this it
544 * synchronized the write-buffer of current bud (A). But later this was
545 * changed and UBIFS started to always synchronize the write-buffer of
546 * the bud (A) before writing the log reference for the new bud (B).
547 *
548 * But because older UBIFS always synchronized A's write-buffer before
549 * writing to B, we can recognize this exceptional situation but
550 * checking the contents of bud B - if it is empty, then A can be
551 * treated as the last and we can recover it.
552 *
553 * TODO: remove this piece of code in a couple of years (today it is
554 * 16.05.2011).
555 */
556 next = list_entry(bud->list.next, struct ubifs_bud, list);
557 if (!list_is_last(&next->list, &jh->buds_list))
558 return 0;
559
560 err = ubifs_leb_read(c, next->lnum, (char *)&data, next->start, 4, 1);
561 if (err)
562 return 0;
563
564 return data == 0xFFFFFFFF;
565}
566
567/**
568 * replay_bud - replay a bud logical eraseblock.
569 * @c: UBIFS file-system description object
570 * @b: bud entry which describes the bud
571 *
572 * This function replays bud @bud, recovers it if needed, and adds all nodes
573 * from this bud to the replay list. Returns zero in case of success and a
574 * negative error code in case of failure.
575 */
576static int replay_bud(struct ubifs_info *c, struct bud_entry *b)
577{
578 int is_last = is_last_bud(c, b->bud);
579 int err = 0, used = 0, lnum = b->bud->lnum, offs = b->bud->start;
580 struct ubifs_scan_leb *sleb;
581 struct ubifs_scan_node *snod;
582
583 dbg_mnt("replay bud LEB %d, head %d, offs %d, is_last %d",
584 lnum, b->bud->jhead, offs, is_last);
585
586 if (c->need_recovery && is_last)
587 /*
588 * Recover only last LEBs in the journal heads, because power
589 * cuts may cause corruptions only in these LEBs, because only
590 * these LEBs could possibly be written to at the power cut
591 * time.
592 */
593 sleb = ubifs_recover_leb(c, lnum, offs, c->sbuf, b->bud->jhead);
594 else
595 sleb = ubifs_scan(c, lnum, offs, c->sbuf, 0);
596 if (IS_ERR(sleb))
597 return PTR_ERR(sleb);
598
599 /*
600 * The bud does not have to start from offset zero - the beginning of
601 * the 'lnum' LEB may contain previously committed data. One of the
602 * things we have to do in replay is to correctly update lprops with
603 * newer information about this LEB.
604 *
605 * At this point lprops thinks that this LEB has 'c->leb_size - offs'
606 * bytes of free space because it only contain information about
607 * committed data.
608 *
609 * But we know that real amount of free space is 'c->leb_size -
610 * sleb->endpt', and the space in the 'lnum' LEB between 'offs' and
611 * 'sleb->endpt' is used by bud data. We have to correctly calculate
612 * how much of these data are dirty and update lprops with this
613 * information.
614 *
615 * The dirt in that LEB region is comprised of padding nodes, deletion
616 * nodes, truncation nodes and nodes which are obsoleted by subsequent
617 * nodes in this LEB. So instead of calculating clean space, we
618 * calculate used space ('used' variable).
619 */
620
621 list_for_each_entry(snod, &sleb->nodes, list) {
622 int deletion = 0;
623
624 cond_resched();
625
626 if (snod->sqnum >= SQNUM_WATERMARK) {
627 ubifs_err(c, "file system's life ended");
628 goto out_dump;
629 }
630
631 if (snod->sqnum > c->max_sqnum)
632 c->max_sqnum = snod->sqnum;
633
634 switch (snod->type) {
635 case UBIFS_INO_NODE:
636 {
637 struct ubifs_ino_node *ino = snod->node;
638 loff_t new_size = le64_to_cpu(ino->size);
639
640 if (le32_to_cpu(ino->nlink) == 0)
641 deletion = 1;
642 err = insert_node(c, lnum, snod->offs, snod->len,
643 &snod->key, snod->sqnum, deletion,
644 &used, 0, new_size);
645 break;
646 }
647 case UBIFS_DATA_NODE:
648 {
649 struct ubifs_data_node *dn = snod->node;
650 loff_t new_size = le32_to_cpu(dn->size) +
651 key_block(c, &snod->key) *
652 UBIFS_BLOCK_SIZE;
653
654 err = insert_node(c, lnum, snod->offs, snod->len,
655 &snod->key, snod->sqnum, deletion,
656 &used, 0, new_size);
657 break;
658 }
659 case UBIFS_DENT_NODE:
660 case UBIFS_XENT_NODE:
661 {
662 struct ubifs_dent_node *dent = snod->node;
663
664 err = ubifs_validate_entry(c, dent);
665 if (err)
666 goto out_dump;
667
668 err = insert_dent(c, lnum, snod->offs, snod->len,
669 &snod->key, dent->name,
670 le16_to_cpu(dent->nlen), snod->sqnum,
671 !le64_to_cpu(dent->inum), &used);
672 break;
673 }
674 case UBIFS_TRUN_NODE:
675 {
676 struct ubifs_trun_node *trun = snod->node;
677 loff_t old_size = le64_to_cpu(trun->old_size);
678 loff_t new_size = le64_to_cpu(trun->new_size);
679 union ubifs_key key;
680
681 /* Validate truncation node */
682 if (old_size < 0 || old_size > c->max_inode_sz ||
683 new_size < 0 || new_size > c->max_inode_sz ||
684 old_size <= new_size) {
685 ubifs_err(c, "bad truncation node");
686 goto out_dump;
687 }
688
689 /*
690 * Create a fake truncation key just to use the same
691 * functions which expect nodes to have keys.
692 */
693 trun_key_init(c, &key, le32_to_cpu(trun->inum));
694 err = insert_node(c, lnum, snod->offs, snod->len,
695 &key, snod->sqnum, 1, &used,
696 old_size, new_size);
697 break;
698 }
699 default:
700 ubifs_err(c, "unexpected node type %d in bud LEB %d:%d",
701 snod->type, lnum, snod->offs);
702 err = -EINVAL;
703 goto out_dump;
704 }
705 if (err)
706 goto out;
707 }
708
709 ubifs_assert(c, ubifs_search_bud(c, lnum));
710 ubifs_assert(c, sleb->endpt - offs >= used);
711 ubifs_assert(c, sleb->endpt % c->min_io_size == 0);
712
713 b->dirty = sleb->endpt - offs - used;
714 b->free = c->leb_size - sleb->endpt;
715 dbg_mnt("bud LEB %d replied: dirty %d, free %d",
716 lnum, b->dirty, b->free);
717
718out:
719 ubifs_scan_destroy(sleb);
720 return err;
721
722out_dump:
723 ubifs_err(c, "bad node is at LEB %d:%d", lnum, snod->offs);
724 ubifs_dump_node(c, snod->node);
725 ubifs_scan_destroy(sleb);
726 return -EINVAL;
727}
728
729/**
730 * replay_buds - replay all buds.
731 * @c: UBIFS file-system description object
732 *
733 * This function returns zero in case of success and a negative error code in
734 * case of failure.
735 */
736static int replay_buds(struct ubifs_info *c)
737{
738 struct bud_entry *b;
739 int err;
740 unsigned long long prev_sqnum = 0;
741
742 list_for_each_entry(b, &c->replay_buds, list) {
743 err = replay_bud(c, b);
744 if (err)
745 return err;
746
747 ubifs_assert(c, b->sqnum > prev_sqnum);
748 prev_sqnum = b->sqnum;
749 }
750
751 return 0;
752}
753
754/**
755 * destroy_bud_list - destroy the list of buds to replay.
756 * @c: UBIFS file-system description object
757 */
758static void destroy_bud_list(struct ubifs_info *c)
759{
760 struct bud_entry *b;
761
762 while (!list_empty(&c->replay_buds)) {
763 b = list_entry(c->replay_buds.next, struct bud_entry, list);
764 list_del(&b->list);
765 kfree(b);
766 }
767}
768
769/**
770 * add_replay_bud - add a bud to the list of buds to replay.
771 * @c: UBIFS file-system description object
772 * @lnum: bud logical eraseblock number to replay
773 * @offs: bud start offset
774 * @jhead: journal head to which this bud belongs
775 * @sqnum: reference node sequence number
776 *
777 * This function returns zero in case of success and a negative error code in
778 * case of failure.
779 */
780static int add_replay_bud(struct ubifs_info *c, int lnum, int offs, int jhead,
781 unsigned long long sqnum)
782{
783 struct ubifs_bud *bud;
784 struct bud_entry *b;
785
786 dbg_mnt("add replay bud LEB %d:%d, head %d", lnum, offs, jhead);
787
788 bud = kmalloc(sizeof(struct ubifs_bud), GFP_KERNEL);
789 if (!bud)
790 return -ENOMEM;
791
792 b = kmalloc(sizeof(struct bud_entry), GFP_KERNEL);
793 if (!b) {
794 kfree(bud);
795 return -ENOMEM;
796 }
797
798 bud->lnum = lnum;
799 bud->start = offs;
800 bud->jhead = jhead;
801 ubifs_add_bud(c, bud);
802
803 b->bud = bud;
804 b->sqnum = sqnum;
805 list_add_tail(&b->list, &c->replay_buds);
806
807 return 0;
808}
809
810/**
811 * validate_ref - validate a reference node.
812 * @c: UBIFS file-system description object
813 * @ref: the reference node to validate
814 * @ref_lnum: LEB number of the reference node
815 * @ref_offs: reference node offset
816 *
817 * This function returns %1 if a bud reference already exists for the LEB. %0 is
818 * returned if the reference node is new, otherwise %-EINVAL is returned if
819 * validation failed.
820 */
821static int validate_ref(struct ubifs_info *c, const struct ubifs_ref_node *ref)
822{
823 struct ubifs_bud *bud;
824 int lnum = le32_to_cpu(ref->lnum);
825 unsigned int offs = le32_to_cpu(ref->offs);
826 unsigned int jhead = le32_to_cpu(ref->jhead);
827
828 /*
829 * ref->offs may point to the end of LEB when the journal head points
830 * to the end of LEB and we write reference node for it during commit.
831 * So this is why we require 'offs > c->leb_size'.
832 */
833 if (jhead >= c->jhead_cnt || lnum >= c->leb_cnt ||
834 lnum < c->main_first || offs > c->leb_size ||
835 offs & (c->min_io_size - 1))
836 return -EINVAL;
837
838 /* Make sure we have not already looked at this bud */
839 bud = ubifs_search_bud(c, lnum);
840 if (bud) {
841 if (bud->jhead == jhead && bud->start <= offs)
842 return 1;
843 ubifs_err(c, "bud at LEB %d:%d was already referred", lnum, offs);
844 return -EINVAL;
845 }
846
847 return 0;
848}
849
850/**
851 * replay_log_leb - replay a log logical eraseblock.
852 * @c: UBIFS file-system description object
853 * @lnum: log logical eraseblock to replay
854 * @offs: offset to start replaying from
855 * @sbuf: scan buffer
856 *
857 * This function replays a log LEB and returns zero in case of success, %1 if
858 * this is the last LEB in the log, and a negative error code in case of
859 * failure.
860 */
861static int replay_log_leb(struct ubifs_info *c, int lnum, int offs, void *sbuf)
862{
863 int err;
864 struct ubifs_scan_leb *sleb;
865 struct ubifs_scan_node *snod;
866 const struct ubifs_cs_node *node;
867
868 dbg_mnt("replay log LEB %d:%d", lnum, offs);
869 sleb = ubifs_scan(c, lnum, offs, sbuf, c->need_recovery);
870 if (IS_ERR(sleb)) {
871 if (PTR_ERR(sleb) != -EUCLEAN || !c->need_recovery)
872 return PTR_ERR(sleb);
873 /*
874 * Note, the below function will recover this log LEB only if
875 * it is the last, because unclean reboots can possibly corrupt
876 * only the tail of the log.
877 */
878 sleb = ubifs_recover_log_leb(c, lnum, offs, sbuf);
879 if (IS_ERR(sleb))
880 return PTR_ERR(sleb);
881 }
882
883 if (sleb->nodes_cnt == 0) {
884 err = 1;
885 goto out;
886 }
887
888 node = sleb->buf;
889 snod = list_entry(sleb->nodes.next, struct ubifs_scan_node, list);
890 if (c->cs_sqnum == 0) {
891 /*
892 * This is the first log LEB we are looking at, make sure that
893 * the first node is a commit start node. Also record its
894 * sequence number so that UBIFS can determine where the log
895 * ends, because all nodes which were have higher sequence
896 * numbers.
897 */
898 if (snod->type != UBIFS_CS_NODE) {
899 ubifs_err(c, "first log node at LEB %d:%d is not CS node",
900 lnum, offs);
901 goto out_dump;
902 }
903 if (le64_to_cpu(node->cmt_no) != c->cmt_no) {
904 ubifs_err(c, "first CS node at LEB %d:%d has wrong commit number %llu expected %llu",
905 lnum, offs,
906 (unsigned long long)le64_to_cpu(node->cmt_no),
907 c->cmt_no);
908 goto out_dump;
909 }
910
911 c->cs_sqnum = le64_to_cpu(node->ch.sqnum);
912 dbg_mnt("commit start sqnum %llu", c->cs_sqnum);
913 }
914
915 if (snod->sqnum < c->cs_sqnum) {
916 /*
917 * This means that we reached end of log and now
918 * look to the older log data, which was already
919 * committed but the eraseblock was not erased (UBIFS
920 * only un-maps it). So this basically means we have to
921 * exit with "end of log" code.
922 */
923 err = 1;
924 goto out;
925 }
926
927 /* Make sure the first node sits at offset zero of the LEB */
928 if (snod->offs != 0) {
929 ubifs_err(c, "first node is not at zero offset");
930 goto out_dump;
931 }
932
933 list_for_each_entry(snod, &sleb->nodes, list) {
934 cond_resched();
935
936 if (snod->sqnum >= SQNUM_WATERMARK) {
937 ubifs_err(c, "file system's life ended");
938 goto out_dump;
939 }
940
941 if (snod->sqnum < c->cs_sqnum) {
942 ubifs_err(c, "bad sqnum %llu, commit sqnum %llu",
943 snod->sqnum, c->cs_sqnum);
944 goto out_dump;
945 }
946
947 if (snod->sqnum > c->max_sqnum)
948 c->max_sqnum = snod->sqnum;
949
950 switch (snod->type) {
951 case UBIFS_REF_NODE: {
952 const struct ubifs_ref_node *ref = snod->node;
953
954 err = validate_ref(c, ref);
955 if (err == 1)
956 break; /* Already have this bud */
957 if (err)
958 goto out_dump;
959
960 err = add_replay_bud(c, le32_to_cpu(ref->lnum),
961 le32_to_cpu(ref->offs),
962 le32_to_cpu(ref->jhead),
963 snod->sqnum);
964 if (err)
965 goto out;
966
967 break;
968 }
969 case UBIFS_CS_NODE:
970 /* Make sure it sits at the beginning of LEB */
971 if (snod->offs != 0) {
972 ubifs_err(c, "unexpected node in log");
973 goto out_dump;
974 }
975 break;
976 default:
977 ubifs_err(c, "unexpected node in log");
978 goto out_dump;
979 }
980 }
981
982 if (sleb->endpt || c->lhead_offs >= c->leb_size) {
983 c->lhead_lnum = lnum;
984 c->lhead_offs = sleb->endpt;
985 }
986
987 err = !sleb->endpt;
988out:
989 ubifs_scan_destroy(sleb);
990 return err;
991
992out_dump:
993 ubifs_err(c, "log error detected while replaying the log at LEB %d:%d",
994 lnum, offs + snod->offs);
995 ubifs_dump_node(c, snod->node);
996 ubifs_scan_destroy(sleb);
997 return -EINVAL;
998}
999
1000/**
1001 * take_ihead - update the status of the index head in lprops to 'taken'.
1002 * @c: UBIFS file-system description object
1003 *
1004 * This function returns the amount of free space in the index head LEB or a
1005 * negative error code.
1006 */
1007static int take_ihead(struct ubifs_info *c)
1008{
1009 const struct ubifs_lprops *lp;
1010 int err, free;
1011
1012 ubifs_get_lprops(c);
1013
1014 lp = ubifs_lpt_lookup_dirty(c, c->ihead_lnum);
1015 if (IS_ERR(lp)) {
1016 err = PTR_ERR(lp);
1017 goto out;
1018 }
1019
1020 free = lp->free;
1021
1022 lp = ubifs_change_lp(c, lp, LPROPS_NC, LPROPS_NC,
1023 lp->flags | LPROPS_TAKEN, 0);
1024 if (IS_ERR(lp)) {
1025 err = PTR_ERR(lp);
1026 goto out;
1027 }
1028
1029 err = free;
1030out:
1031 ubifs_release_lprops(c);
1032 return err;
1033}
1034
1035/**
1036 * ubifs_replay_journal - replay journal.
1037 * @c: UBIFS file-system description object
1038 *
1039 * This function scans the journal, replays and cleans it up. It makes sure all
1040 * memory data structures related to uncommitted journal are built (dirty TNC
1041 * tree, tree of buds, modified lprops, etc).
1042 */
1043int ubifs_replay_journal(struct ubifs_info *c)
1044{
1045 int err, lnum, free;
1046
1047 BUILD_BUG_ON(UBIFS_TRUN_KEY > 5);
1048
1049 /* Update the status of the index head in lprops to 'taken' */
1050 free = take_ihead(c);
1051 if (free < 0)
1052 return free; /* Error code */
1053
1054 if (c->ihead_offs != c->leb_size - free) {
1055 ubifs_err(c, "bad index head LEB %d:%d", c->ihead_lnum,
1056 c->ihead_offs);
1057 return -EINVAL;
1058 }
1059
1060 dbg_mnt("start replaying the journal");
1061 c->replaying = 1;
1062 lnum = c->ltail_lnum = c->lhead_lnum;
1063
1064 do {
1065 err = replay_log_leb(c, lnum, 0, c->sbuf);
1066 if (err == 1) {
1067 if (lnum != c->lhead_lnum)
1068 /* We hit the end of the log */
1069 break;
1070
1071 /*
1072 * The head of the log must always start with the
1073 * "commit start" node on a properly formatted UBIFS.
1074 * But we found no nodes at all, which means that
1075 * something went wrong and we cannot proceed mounting
1076 * the file-system.
1077 */
1078 ubifs_err(c, "no UBIFS nodes found at the log head LEB %d:%d, possibly corrupted",
1079 lnum, 0);
1080 err = -EINVAL;
1081 }
1082 if (err)
1083 goto out;
1084 lnum = ubifs_next_log_lnum(c, lnum);
1085 } while (lnum != c->ltail_lnum);
1086
1087 err = replay_buds(c);
1088 if (err)
1089 goto out;
1090
1091 err = apply_replay_list(c);
1092 if (err)
1093 goto out;
1094
1095 err = set_buds_lprops(c);
1096 if (err)
1097 goto out;
1098
1099 /*
1100 * UBIFS budgeting calculations use @c->bi.uncommitted_idx variable
1101 * to roughly estimate index growth. Things like @c->bi.min_idx_lebs
1102 * depend on it. This means we have to initialize it to make sure
1103 * budgeting works properly.
1104 */
1105 c->bi.uncommitted_idx = atomic_long_read(&c->dirty_zn_cnt);
1106 c->bi.uncommitted_idx *= c->max_idx_node_sz;
1107
1108 ubifs_assert(c, c->bud_bytes <= c->max_bud_bytes || c->need_recovery);
1109 dbg_mnt("finished, log head LEB %d:%d, max_sqnum %llu, highest_inum %lu",
1110 c->lhead_lnum, c->lhead_offs, c->max_sqnum,
1111 (unsigned long)c->highest_inum);
1112out:
1113 destroy_replay_list(c);
1114 destroy_bud_list(c);
1115 c->replaying = 0;
1116 return err;
1117}