1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include "ubifs.h"
24#include <linux/list_sort.h>
25#include <crypto/hash.h>
26#include <crypto/algapi.h>
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45struct replay_entry {
46 int lnum;
47 int offs;
48 int len;
49 u8 hash[UBIFS_HASH_ARR_SZ];
50 unsigned int deletion:1;
51 unsigned long long sqnum;
52 struct list_head list;
53 union ubifs_key key;
54 union {
55 struct fscrypt_name nm;
56 struct {
57 loff_t old_size;
58 loff_t new_size;
59 };
60 };
61};
62
63
64
65
66
67
68
69
70
71struct bud_entry {
72 struct list_head list;
73 struct ubifs_bud *bud;
74 unsigned long long sqnum;
75 int free;
76 int dirty;
77};
78
79
80
81
82
83
84
85
86
87
88static int set_bud_lprops(struct ubifs_info *c, struct bud_entry *b)
89{
90 const struct ubifs_lprops *lp;
91 int err = 0, dirty;
92
93 ubifs_get_lprops(c);
94
95 lp = ubifs_lpt_lookup_dirty(c, b->bud->lnum);
96 if (IS_ERR(lp)) {
97 err = PTR_ERR(lp);
98 goto out;
99 }
100
101 dirty = lp->dirty;
102 if (b->bud->start == 0 && (lp->free != c->leb_size || lp->dirty != 0)) {
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122 dbg_mnt("bud LEB %d was GC'd (%d free, %d dirty)", b->bud->lnum,
123 lp->free, lp->dirty);
124 dbg_gc("bud LEB %d was GC'd (%d free, %d dirty)", b->bud->lnum,
125 lp->free, lp->dirty);
126 dirty -= c->leb_size - lp->free;
127
128
129
130
131
132
133
134 if (dirty != 0)
135 dbg_mnt("LEB %d lp: %d free %d dirty replay: %d free %d dirty",
136 b->bud->lnum, lp->free, lp->dirty, b->free,
137 b->dirty);
138 }
139 lp = ubifs_change_lp(c, lp, b->free, dirty + b->dirty,
140 lp->flags | LPROPS_TAKEN, 0);
141 if (IS_ERR(lp)) {
142 err = PTR_ERR(lp);
143 goto out;
144 }
145
146
147 err = ubifs_wbuf_seek_nolock(&c->jheads[b->bud->jhead].wbuf,
148 b->bud->lnum, c->leb_size - b->free);
149
150out:
151 ubifs_release_lprops(c);
152 return err;
153}
154
155
156
157
158
159
160
161
162static int set_buds_lprops(struct ubifs_info *c)
163{
164 struct bud_entry *b;
165 int err;
166
167 list_for_each_entry(b, &c->replay_buds, list) {
168 err = set_bud_lprops(c, b);
169 if (err)
170 return err;
171 }
172
173 return 0;
174}
175
176
177
178
179
180
181static int trun_remove_range(struct ubifs_info *c, struct replay_entry *r)
182{
183 unsigned min_blk, max_blk;
184 union ubifs_key min_key, max_key;
185 ino_t ino;
186
187 min_blk = r->new_size / UBIFS_BLOCK_SIZE;
188 if (r->new_size & (UBIFS_BLOCK_SIZE - 1))
189 min_blk += 1;
190
191 max_blk = r->old_size / UBIFS_BLOCK_SIZE;
192 if ((r->old_size & (UBIFS_BLOCK_SIZE - 1)) == 0)
193 max_blk -= 1;
194
195 ino = key_inum(c, &r->key);
196
197 data_key_init(c, &min_key, ino, min_blk);
198 data_key_init(c, &max_key, ino, max_blk);
199
200 return ubifs_tnc_remove_range(c, &min_key, &max_key);
201}
202
203
204
205
206
207
208
209
210
211
212
213static bool inode_still_linked(struct ubifs_info *c, struct replay_entry *rino)
214{
215 struct replay_entry *r;
216
217 ubifs_assert(c, rino->deletion);
218 ubifs_assert(c, key_type(c, &rino->key) == UBIFS_INO_KEY);
219
220
221
222
223
224 list_for_each_entry_reverse(r, &c->replay_list, list) {
225 ubifs_assert(c, r->sqnum >= rino->sqnum);
226 if (key_inum(c, &r->key) == key_inum(c, &rino->key))
227 return r->deletion == 0;
228
229 }
230
231 ubifs_assert(c, 0);
232 return false;
233}
234
235
236
237
238
239
240
241
242static int apply_replay_entry(struct ubifs_info *c, struct replay_entry *r)
243{
244 int err;
245
246 dbg_mntk(&r->key, "LEB %d:%d len %d deletion %d sqnum %llu key ",
247 r->lnum, r->offs, r->len, r->deletion, r->sqnum);
248
249 if (is_hash_key(c, &r->key)) {
250 if (r->deletion)
251 err = ubifs_tnc_remove_nm(c, &r->key, &r->nm);
252 else
253 err = ubifs_tnc_add_nm(c, &r->key, r->lnum, r->offs,
254 r->len, r->hash, &r->nm);
255 } else {
256 if (r->deletion)
257 switch (key_type(c, &r->key)) {
258 case UBIFS_INO_KEY:
259 {
260 ino_t inum = key_inum(c, &r->key);
261
262 if (inode_still_linked(c, r)) {
263 err = 0;
264 break;
265 }
266
267 err = ubifs_tnc_remove_ino(c, inum);
268 break;
269 }
270 case UBIFS_TRUN_KEY:
271 err = trun_remove_range(c, r);
272 break;
273 default:
274 err = ubifs_tnc_remove(c, &r->key);
275 break;
276 }
277 else
278 err = ubifs_tnc_add(c, &r->key, r->lnum, r->offs,
279 r->len, r->hash);
280 if (err)
281 return err;
282
283 if (c->need_recovery)
284 err = ubifs_recover_size_accum(c, &r->key, r->deletion,
285 r->new_size);
286 }
287
288 return err;
289}
290
291
292
293
294
295
296
297
298
299
300
301static int replay_entries_cmp(void *priv, struct list_head *a,
302 struct list_head *b)
303{
304 struct ubifs_info *c = priv;
305 struct replay_entry *ra, *rb;
306
307 cond_resched();
308 if (a == b)
309 return 0;
310
311 ra = list_entry(a, struct replay_entry, list);
312 rb = list_entry(b, struct replay_entry, list);
313 ubifs_assert(c, ra->sqnum != rb->sqnum);
314 if (ra->sqnum > rb->sqnum)
315 return 1;
316 return -1;
317}
318
319
320
321
322
323
324
325
326static int apply_replay_list(struct ubifs_info *c)
327{
328 struct replay_entry *r;
329 int err;
330
331 list_sort(c, &c->replay_list, &replay_entries_cmp);
332
333 list_for_each_entry(r, &c->replay_list, list) {
334 cond_resched();
335
336 err = apply_replay_entry(c, r);
337 if (err)
338 return err;
339 }
340
341 return 0;
342}
343
344
345
346
347
348
349
350static void destroy_replay_list(struct ubifs_info *c)
351{
352 struct replay_entry *r, *tmp;
353
354 list_for_each_entry_safe(r, tmp, &c->replay_list, list) {
355 if (is_hash_key(c, &r->key))
356 kfree(fname_name(&r->nm));
357 list_del(&r->list);
358 kfree(r);
359 }
360}
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382static int insert_node(struct ubifs_info *c, int lnum, int offs, int len,
383 const u8 *hash, union ubifs_key *key,
384 unsigned long long sqnum, int deletion, int *used,
385 loff_t old_size, loff_t new_size)
386{
387 struct replay_entry *r;
388
389 dbg_mntk(key, "add LEB %d:%d, key ", lnum, offs);
390
391 if (key_inum(c, key) >= c->highest_inum)
392 c->highest_inum = key_inum(c, key);
393
394 r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL);
395 if (!r)
396 return -ENOMEM;
397
398 if (!deletion)
399 *used += ALIGN(len, 8);
400 r->lnum = lnum;
401 r->offs = offs;
402 r->len = len;
403 ubifs_copy_hash(c, hash, r->hash);
404 r->deletion = !!deletion;
405 r->sqnum = sqnum;
406 key_copy(c, key, &r->key);
407 r->old_size = old_size;
408 r->new_size = new_size;
409
410 list_add_tail(&r->list, &c->replay_list);
411 return 0;
412}
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431static int insert_dent(struct ubifs_info *c, int lnum, int offs, int len,
432 const u8 *hash, union ubifs_key *key,
433 const char *name, int nlen, unsigned long long sqnum,
434 int deletion, int *used)
435{
436 struct replay_entry *r;
437 char *nbuf;
438
439 dbg_mntk(key, "add LEB %d:%d, key ", lnum, offs);
440 if (key_inum(c, key) >= c->highest_inum)
441 c->highest_inum = key_inum(c, key);
442
443 r = kzalloc(sizeof(struct replay_entry), GFP_KERNEL);
444 if (!r)
445 return -ENOMEM;
446
447 nbuf = kmalloc(nlen + 1, GFP_KERNEL);
448 if (!nbuf) {
449 kfree(r);
450 return -ENOMEM;
451 }
452
453 if (!deletion)
454 *used += ALIGN(len, 8);
455 r->lnum = lnum;
456 r->offs = offs;
457 r->len = len;
458 ubifs_copy_hash(c, hash, r->hash);
459 r->deletion = !!deletion;
460 r->sqnum = sqnum;
461 key_copy(c, key, &r->key);
462 fname_len(&r->nm) = nlen;
463 memcpy(nbuf, name, nlen);
464 nbuf[nlen] = '\0';
465 fname_name(&r->nm) = nbuf;
466
467 list_add_tail(&r->list, &c->replay_list);
468 return 0;
469}
470
471
472
473
474
475
476
477
478
479int ubifs_validate_entry(struct ubifs_info *c,
480 const struct ubifs_dent_node *dent)
481{
482 int key_type = key_type_flash(c, dent->key);
483 int nlen = le16_to_cpu(dent->nlen);
484
485 if (le32_to_cpu(dent->ch.len) != nlen + UBIFS_DENT_NODE_SZ + 1 ||
486 dent->type >= UBIFS_ITYPES_CNT ||
487 nlen > UBIFS_MAX_NLEN || dent->name[nlen] != 0 ||
488 (key_type == UBIFS_XENT_KEY && strnlen(dent->name, nlen) != nlen) ||
489 le64_to_cpu(dent->inum) > MAX_INUM) {
490 ubifs_err(c, "bad %s node", key_type == UBIFS_DENT_KEY ?
491 "directory entry" : "extended attribute entry");
492 return -EINVAL;
493 }
494
495 if (key_type != UBIFS_DENT_KEY && key_type != UBIFS_XENT_KEY) {
496 ubifs_err(c, "bad key type %d", key_type);
497 return -EINVAL;
498 }
499
500 return 0;
501}
502
503
504
505
506
507
508
509
510
511
512
513static int is_last_bud(struct ubifs_info *c, struct ubifs_bud *bud)
514{
515 struct ubifs_jhead *jh = &c->jheads[bud->jhead];
516 struct ubifs_bud *next;
517 uint32_t data;
518 int err;
519
520 if (list_is_last(&bud->list, &jh->buds_list))
521 return 1;
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550 next = list_entry(bud->list.next, struct ubifs_bud, list);
551 if (!list_is_last(&next->list, &jh->buds_list))
552 return 0;
553
554 err = ubifs_leb_read(c, next->lnum, (char *)&data, next->start, 4, 1);
555 if (err)
556 return 0;
557
558 return data == 0xFFFFFFFF;
559}
560
561
562static int authenticate_sleb_hash(struct ubifs_info *c, struct shash_desc *log_hash, u8 *hash)
563{
564 SHASH_DESC_ON_STACK(hash_desc, c->hash_tfm);
565
566 hash_desc->tfm = c->hash_tfm;
567
568 ubifs_shash_copy_state(c, log_hash, hash_desc);
569 return crypto_shash_final(hash_desc, hash);
570}
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588static int authenticate_sleb(struct ubifs_info *c, struct ubifs_scan_leb *sleb,
589 struct shash_desc *log_hash, int is_last)
590{
591 int n_not_auth = 0;
592 struct ubifs_scan_node *snod;
593 int n_nodes = 0;
594 int err;
595 u8 hash[UBIFS_HASH_ARR_SZ];
596 u8 hmac[UBIFS_HMAC_ARR_SZ];
597
598 if (!ubifs_authenticated(c))
599 return sleb->nodes_cnt;
600
601 list_for_each_entry(snod, &sleb->nodes, list) {
602
603 n_nodes++;
604
605 if (snod->type == UBIFS_AUTH_NODE) {
606 struct ubifs_auth_node *auth = snod->node;
607
608 err = authenticate_sleb_hash(c, log_hash, hash);
609 if (err)
610 goto out;
611
612 err = crypto_shash_tfm_digest(c->hmac_tfm, hash,
613 c->hash_len, hmac);
614 if (err)
615 goto out;
616
617 err = ubifs_check_hmac(c, auth->hmac, hmac);
618 if (err) {
619 err = -EPERM;
620 goto out;
621 }
622 n_not_auth = 0;
623 } else {
624 err = crypto_shash_update(log_hash, snod->node,
625 snod->len);
626 if (err)
627 goto out;
628 n_not_auth++;
629 }
630 }
631
632
633
634
635
636
637 if (n_not_auth) {
638 if (is_last) {
639 dbg_mnt("%d unauthenticated nodes found on LEB %d, Ignoring them",
640 n_not_auth, sleb->lnum);
641 err = 0;
642 } else {
643 dbg_mnt("%d unauthenticated nodes found on non-last LEB %d",
644 n_not_auth, sleb->lnum);
645 err = -EPERM;
646 }
647 } else {
648 err = 0;
649 }
650out:
651 return err ? err : n_nodes - n_not_auth;
652}
653
654
655
656
657
658
659
660
661
662
663static int replay_bud(struct ubifs_info *c, struct bud_entry *b)
664{
665 int is_last = is_last_bud(c, b->bud);
666 int err = 0, used = 0, lnum = b->bud->lnum, offs = b->bud->start;
667 int n_nodes, n = 0;
668 struct ubifs_scan_leb *sleb;
669 struct ubifs_scan_node *snod;
670
671 dbg_mnt("replay bud LEB %d, head %d, offs %d, is_last %d",
672 lnum, b->bud->jhead, offs, is_last);
673
674 if (c->need_recovery && is_last)
675
676
677
678
679
680
681 sleb = ubifs_recover_leb(c, lnum, offs, c->sbuf, b->bud->jhead);
682 else
683 sleb = ubifs_scan(c, lnum, offs, c->sbuf, 0);
684 if (IS_ERR(sleb))
685 return PTR_ERR(sleb);
686
687 n_nodes = authenticate_sleb(c, sleb, b->bud->log_hash, is_last);
688 if (n_nodes < 0) {
689 err = n_nodes;
690 goto out;
691 }
692
693 ubifs_shash_copy_state(c, b->bud->log_hash,
694 c->jheads[b->bud->jhead].log_hash);
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718 list_for_each_entry(snod, &sleb->nodes, list) {
719 u8 hash[UBIFS_HASH_ARR_SZ];
720 int deletion = 0;
721
722 cond_resched();
723
724 if (snod->sqnum >= SQNUM_WATERMARK) {
725 ubifs_err(c, "file system's life ended");
726 goto out_dump;
727 }
728
729 ubifs_node_calc_hash(c, snod->node, hash);
730
731 if (snod->sqnum > c->max_sqnum)
732 c->max_sqnum = snod->sqnum;
733
734 switch (snod->type) {
735 case UBIFS_INO_NODE:
736 {
737 struct ubifs_ino_node *ino = snod->node;
738 loff_t new_size = le64_to_cpu(ino->size);
739
740 if (le32_to_cpu(ino->nlink) == 0)
741 deletion = 1;
742 err = insert_node(c, lnum, snod->offs, snod->len, hash,
743 &snod->key, snod->sqnum, deletion,
744 &used, 0, new_size);
745 break;
746 }
747 case UBIFS_DATA_NODE:
748 {
749 struct ubifs_data_node *dn = snod->node;
750 loff_t new_size = le32_to_cpu(dn->size) +
751 key_block(c, &snod->key) *
752 UBIFS_BLOCK_SIZE;
753
754 err = insert_node(c, lnum, snod->offs, snod->len, hash,
755 &snod->key, snod->sqnum, deletion,
756 &used, 0, new_size);
757 break;
758 }
759 case UBIFS_DENT_NODE:
760 case UBIFS_XENT_NODE:
761 {
762 struct ubifs_dent_node *dent = snod->node;
763
764 err = ubifs_validate_entry(c, dent);
765 if (err)
766 goto out_dump;
767
768 err = insert_dent(c, lnum, snod->offs, snod->len, hash,
769 &snod->key, dent->name,
770 le16_to_cpu(dent->nlen), snod->sqnum,
771 !le64_to_cpu(dent->inum), &used);
772 break;
773 }
774 case UBIFS_TRUN_NODE:
775 {
776 struct ubifs_trun_node *trun = snod->node;
777 loff_t old_size = le64_to_cpu(trun->old_size);
778 loff_t new_size = le64_to_cpu(trun->new_size);
779 union ubifs_key key;
780
781
782 if (old_size < 0 || old_size > c->max_inode_sz ||
783 new_size < 0 || new_size > c->max_inode_sz ||
784 old_size <= new_size) {
785 ubifs_err(c, "bad truncation node");
786 goto out_dump;
787 }
788
789
790
791
792
793 trun_key_init(c, &key, le32_to_cpu(trun->inum));
794 err = insert_node(c, lnum, snod->offs, snod->len, hash,
795 &key, snod->sqnum, 1, &used,
796 old_size, new_size);
797 break;
798 }
799 case UBIFS_AUTH_NODE:
800 break;
801 default:
802 ubifs_err(c, "unexpected node type %d in bud LEB %d:%d",
803 snod->type, lnum, snod->offs);
804 err = -EINVAL;
805 goto out_dump;
806 }
807 if (err)
808 goto out;
809
810 n++;
811 if (n == n_nodes)
812 break;
813 }
814
815 ubifs_assert(c, ubifs_search_bud(c, lnum));
816 ubifs_assert(c, sleb->endpt - offs >= used);
817 ubifs_assert(c, sleb->endpt % c->min_io_size == 0);
818
819 b->dirty = sleb->endpt - offs - used;
820 b->free = c->leb_size - sleb->endpt;
821 dbg_mnt("bud LEB %d replied: dirty %d, free %d",
822 lnum, b->dirty, b->free);
823
824out:
825 ubifs_scan_destroy(sleb);
826 return err;
827
828out_dump:
829 ubifs_err(c, "bad node is at LEB %d:%d", lnum, snod->offs);
830 ubifs_dump_node(c, snod->node);
831 ubifs_scan_destroy(sleb);
832 return -EINVAL;
833}
834
835
836
837
838
839
840
841
842static int replay_buds(struct ubifs_info *c)
843{
844 struct bud_entry *b;
845 int err;
846 unsigned long long prev_sqnum = 0;
847
848 list_for_each_entry(b, &c->replay_buds, list) {
849 err = replay_bud(c, b);
850 if (err)
851 return err;
852
853 ubifs_assert(c, b->sqnum > prev_sqnum);
854 prev_sqnum = b->sqnum;
855 }
856
857 return 0;
858}
859
860
861
862
863
864static void destroy_bud_list(struct ubifs_info *c)
865{
866 struct bud_entry *b;
867
868 while (!list_empty(&c->replay_buds)) {
869 b = list_entry(c->replay_buds.next, struct bud_entry, list);
870 list_del(&b->list);
871 kfree(b);
872 }
873}
874
875
876
877
878
879
880
881
882
883
884
885
886static int add_replay_bud(struct ubifs_info *c, int lnum, int offs, int jhead,
887 unsigned long long sqnum)
888{
889 struct ubifs_bud *bud;
890 struct bud_entry *b;
891 int err;
892
893 dbg_mnt("add replay bud LEB %d:%d, head %d", lnum, offs, jhead);
894
895 bud = kmalloc(sizeof(struct ubifs_bud), GFP_KERNEL);
896 if (!bud)
897 return -ENOMEM;
898
899 b = kmalloc(sizeof(struct bud_entry), GFP_KERNEL);
900 if (!b) {
901 err = -ENOMEM;
902 goto out;
903 }
904
905 bud->lnum = lnum;
906 bud->start = offs;
907 bud->jhead = jhead;
908 bud->log_hash = ubifs_hash_get_desc(c);
909 if (IS_ERR(bud->log_hash)) {
910 err = PTR_ERR(bud->log_hash);
911 goto out;
912 }
913
914 ubifs_shash_copy_state(c, c->log_hash, bud->log_hash);
915
916 ubifs_add_bud(c, bud);
917
918 b->bud = bud;
919 b->sqnum = sqnum;
920 list_add_tail(&b->list, &c->replay_buds);
921
922 return 0;
923out:
924 kfree(bud);
925 kfree(b);
926
927 return err;
928}
929
930
931
932
933
934
935
936
937
938
939
940
941static int validate_ref(struct ubifs_info *c, const struct ubifs_ref_node *ref)
942{
943 struct ubifs_bud *bud;
944 int lnum = le32_to_cpu(ref->lnum);
945 unsigned int offs = le32_to_cpu(ref->offs);
946 unsigned int jhead = le32_to_cpu(ref->jhead);
947
948
949
950
951
952
953 if (jhead >= c->jhead_cnt || lnum >= c->leb_cnt ||
954 lnum < c->main_first || offs > c->leb_size ||
955 offs & (c->min_io_size - 1))
956 return -EINVAL;
957
958
959 bud = ubifs_search_bud(c, lnum);
960 if (bud) {
961 if (bud->jhead == jhead && bud->start <= offs)
962 return 1;
963 ubifs_err(c, "bud at LEB %d:%d was already referred", lnum, offs);
964 return -EINVAL;
965 }
966
967 return 0;
968}
969
970
971
972
973
974
975
976
977
978
979
980
981static int replay_log_leb(struct ubifs_info *c, int lnum, int offs, void *sbuf)
982{
983 int err;
984 struct ubifs_scan_leb *sleb;
985 struct ubifs_scan_node *snod;
986 const struct ubifs_cs_node *node;
987
988 dbg_mnt("replay log LEB %d:%d", lnum, offs);
989 sleb = ubifs_scan(c, lnum, offs, sbuf, c->need_recovery);
990 if (IS_ERR(sleb)) {
991 if (PTR_ERR(sleb) != -EUCLEAN || !c->need_recovery)
992 return PTR_ERR(sleb);
993
994
995
996
997
998 sleb = ubifs_recover_log_leb(c, lnum, offs, sbuf);
999 if (IS_ERR(sleb))
1000 return PTR_ERR(sleb);
1001 }
1002
1003 if (sleb->nodes_cnt == 0) {
1004 err = 1;
1005 goto out;
1006 }
1007
1008 node = sleb->buf;
1009 snod = list_entry(sleb->nodes.next, struct ubifs_scan_node, list);
1010 if (c->cs_sqnum == 0) {
1011
1012
1013
1014
1015
1016
1017
1018 if (snod->type != UBIFS_CS_NODE) {
1019 ubifs_err(c, "first log node at LEB %d:%d is not CS node",
1020 lnum, offs);
1021 goto out_dump;
1022 }
1023 if (le64_to_cpu(node->cmt_no) != c->cmt_no) {
1024 ubifs_err(c, "first CS node at LEB %d:%d has wrong commit number %llu expected %llu",
1025 lnum, offs,
1026 (unsigned long long)le64_to_cpu(node->cmt_no),
1027 c->cmt_no);
1028 goto out_dump;
1029 }
1030
1031 c->cs_sqnum = le64_to_cpu(node->ch.sqnum);
1032 dbg_mnt("commit start sqnum %llu", c->cs_sqnum);
1033
1034 err = ubifs_shash_init(c, c->log_hash);
1035 if (err)
1036 goto out;
1037
1038 err = ubifs_shash_update(c, c->log_hash, node, UBIFS_CS_NODE_SZ);
1039 if (err < 0)
1040 goto out;
1041 }
1042
1043 if (snod->sqnum < c->cs_sqnum) {
1044
1045
1046
1047
1048
1049
1050
1051 err = 1;
1052 goto out;
1053 }
1054
1055
1056 if (snod->offs != 0) {
1057 ubifs_err(c, "first node is not at zero offset");
1058 goto out_dump;
1059 }
1060
1061 list_for_each_entry(snod, &sleb->nodes, list) {
1062 cond_resched();
1063
1064 if (snod->sqnum >= SQNUM_WATERMARK) {
1065 ubifs_err(c, "file system's life ended");
1066 goto out_dump;
1067 }
1068
1069 if (snod->sqnum < c->cs_sqnum) {
1070 ubifs_err(c, "bad sqnum %llu, commit sqnum %llu",
1071 snod->sqnum, c->cs_sqnum);
1072 goto out_dump;
1073 }
1074
1075 if (snod->sqnum > c->max_sqnum)
1076 c->max_sqnum = snod->sqnum;
1077
1078 switch (snod->type) {
1079 case UBIFS_REF_NODE: {
1080 const struct ubifs_ref_node *ref = snod->node;
1081
1082 err = validate_ref(c, ref);
1083 if (err == 1)
1084 break;
1085 if (err)
1086 goto out_dump;
1087
1088 err = ubifs_shash_update(c, c->log_hash, ref,
1089 UBIFS_REF_NODE_SZ);
1090 if (err)
1091 goto out;
1092
1093 err = add_replay_bud(c, le32_to_cpu(ref->lnum),
1094 le32_to_cpu(ref->offs),
1095 le32_to_cpu(ref->jhead),
1096 snod->sqnum);
1097 if (err)
1098 goto out;
1099
1100 break;
1101 }
1102 case UBIFS_CS_NODE:
1103
1104 if (snod->offs != 0) {
1105 ubifs_err(c, "unexpected node in log");
1106 goto out_dump;
1107 }
1108 break;
1109 default:
1110 ubifs_err(c, "unexpected node in log");
1111 goto out_dump;
1112 }
1113 }
1114
1115 if (sleb->endpt || c->lhead_offs >= c->leb_size) {
1116 c->lhead_lnum = lnum;
1117 c->lhead_offs = sleb->endpt;
1118 }
1119
1120 err = !sleb->endpt;
1121out:
1122 ubifs_scan_destroy(sleb);
1123 return err;
1124
1125out_dump:
1126 ubifs_err(c, "log error detected while replaying the log at LEB %d:%d",
1127 lnum, offs + snod->offs);
1128 ubifs_dump_node(c, snod->node);
1129 ubifs_scan_destroy(sleb);
1130 return -EINVAL;
1131}
1132
1133
1134
1135
1136
1137
1138
1139
1140static int take_ihead(struct ubifs_info *c)
1141{
1142 const struct ubifs_lprops *lp;
1143 int err, free;
1144
1145 ubifs_get_lprops(c);
1146
1147 lp = ubifs_lpt_lookup_dirty(c, c->ihead_lnum);
1148 if (IS_ERR(lp)) {
1149 err = PTR_ERR(lp);
1150 goto out;
1151 }
1152
1153 free = lp->free;
1154
1155 lp = ubifs_change_lp(c, lp, LPROPS_NC, LPROPS_NC,
1156 lp->flags | LPROPS_TAKEN, 0);
1157 if (IS_ERR(lp)) {
1158 err = PTR_ERR(lp);
1159 goto out;
1160 }
1161
1162 err = free;
1163out:
1164 ubifs_release_lprops(c);
1165 return err;
1166}
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176int ubifs_replay_journal(struct ubifs_info *c)
1177{
1178 int err, lnum, free;
1179
1180 BUILD_BUG_ON(UBIFS_TRUN_KEY > 5);
1181
1182
1183 free = take_ihead(c);
1184 if (free < 0)
1185 return free;
1186
1187 if (c->ihead_offs != c->leb_size - free) {
1188 ubifs_err(c, "bad index head LEB %d:%d", c->ihead_lnum,
1189 c->ihead_offs);
1190 return -EINVAL;
1191 }
1192
1193 dbg_mnt("start replaying the journal");
1194 c->replaying = 1;
1195 lnum = c->ltail_lnum = c->lhead_lnum;
1196
1197 do {
1198 err = replay_log_leb(c, lnum, 0, c->sbuf);
1199 if (err == 1) {
1200 if (lnum != c->lhead_lnum)
1201
1202 break;
1203
1204
1205
1206
1207
1208
1209
1210
1211 ubifs_err(c, "no UBIFS nodes found at the log head LEB %d:%d, possibly corrupted",
1212 lnum, 0);
1213 err = -EINVAL;
1214 }
1215 if (err)
1216 goto out;
1217 lnum = ubifs_next_log_lnum(c, lnum);
1218 } while (lnum != c->ltail_lnum);
1219
1220 err = replay_buds(c);
1221 if (err)
1222 goto out;
1223
1224 err = apply_replay_list(c);
1225 if (err)
1226 goto out;
1227
1228 err = set_buds_lprops(c);
1229 if (err)
1230 goto out;
1231
1232
1233
1234
1235
1236
1237
1238 c->bi.uncommitted_idx = atomic_long_read(&c->dirty_zn_cnt);
1239 c->bi.uncommitted_idx *= c->max_idx_node_sz;
1240
1241 ubifs_assert(c, c->bud_bytes <= c->max_bud_bytes || c->need_recovery);
1242 dbg_mnt("finished, log head LEB %d:%d, max_sqnum %llu, highest_inum %lu",
1243 c->lhead_lnum, c->lhead_offs, c->max_sqnum,
1244 (unsigned long)c->highest_inum);
1245out:
1246 destroy_replay_list(c);
1247 destroy_bud_list(c);
1248 c->replaying = 0;
1249 return err;
1250}
1251