1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#ifdef __UBOOT__
20#include <log.h>
21#include <malloc.h>
22#include <linux/err.h>
23#endif
24#include "ubifs.h"
25
26
27
28
29
30
31static int get_heap_comp_val(struct ubifs_lprops *lprops, int cat)
32{
33 switch (cat) {
34 case LPROPS_FREE:
35 return lprops->free;
36 case LPROPS_DIRTY_IDX:
37 return lprops->free + lprops->dirty;
38 default:
39 return lprops->dirty;
40 }
41}
42
43
44
45
46
47
48
49
50
51
52
53
54
55static void move_up_lpt_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap,
56 struct ubifs_lprops *lprops, int cat)
57{
58 int val1, val2, hpos;
59
60 hpos = lprops->hpos;
61 if (!hpos)
62 return;
63 val1 = get_heap_comp_val(lprops, cat);
64
65 do {
66 int ppos = (hpos - 1) / 2;
67
68 val2 = get_heap_comp_val(heap->arr[ppos], cat);
69 if (val2 >= val1)
70 return;
71
72 heap->arr[ppos]->hpos = hpos;
73 heap->arr[hpos] = heap->arr[ppos];
74 heap->arr[ppos] = lprops;
75 lprops->hpos = ppos;
76 hpos = ppos;
77 } while (hpos);
78}
79
80
81
82
83
84
85
86
87
88
89
90
91
92static void adjust_lpt_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap,
93 struct ubifs_lprops *lprops, int hpos, int cat)
94{
95 int val1, val2, val3, cpos;
96
97 val1 = get_heap_comp_val(lprops, cat);
98
99 if (hpos) {
100 int ppos = (hpos - 1) / 2;
101
102 val2 = get_heap_comp_val(heap->arr[ppos], cat);
103 if (val1 > val2) {
104
105 while (1) {
106 heap->arr[ppos]->hpos = hpos;
107 heap->arr[hpos] = heap->arr[ppos];
108 heap->arr[ppos] = lprops;
109 lprops->hpos = ppos;
110 hpos = ppos;
111 if (!hpos)
112 return;
113 ppos = (hpos - 1) / 2;
114 val2 = get_heap_comp_val(heap->arr[ppos], cat);
115 if (val1 <= val2)
116 return;
117
118 }
119 }
120 }
121
122
123 while (1) {
124
125 cpos = hpos * 2 + 1;
126 if (cpos >= heap->cnt)
127 return;
128 val2 = get_heap_comp_val(heap->arr[cpos], cat);
129 if (val1 < val2) {
130
131 if (cpos + 1 < heap->cnt) {
132 val3 = get_heap_comp_val(heap->arr[cpos + 1],
133 cat);
134 if (val3 > val2)
135 cpos += 1;
136 }
137 heap->arr[cpos]->hpos = hpos;
138 heap->arr[hpos] = heap->arr[cpos];
139 heap->arr[cpos] = lprops;
140 lprops->hpos = cpos;
141 hpos = cpos;
142 continue;
143 }
144
145 cpos += 1;
146 if (cpos >= heap->cnt)
147 return;
148 val3 = get_heap_comp_val(heap->arr[cpos], cat);
149 if (val1 < val3) {
150
151 heap->arr[cpos]->hpos = hpos;
152 heap->arr[hpos] = heap->arr[cpos];
153 heap->arr[cpos] = lprops;
154 lprops->hpos = cpos;
155 hpos = cpos;
156 continue;
157 }
158 return;
159 }
160}
161
162
163
164
165
166
167
168
169
170
171static int add_to_lpt_heap(struct ubifs_info *c, struct ubifs_lprops *lprops,
172 int cat)
173{
174 struct ubifs_lpt_heap *heap = &c->lpt_heap[cat - 1];
175
176 if (heap->cnt >= heap->max_cnt) {
177 const int b = LPT_HEAP_SZ / 2 - 1;
178 int cpos, val1, val2;
179
180
181
182 cpos = (((size_t)lprops >> 4) & b) + b;
183 ubifs_assert(cpos >= b);
184 ubifs_assert(cpos < LPT_HEAP_SZ);
185 ubifs_assert(cpos < heap->cnt);
186
187 val1 = get_heap_comp_val(lprops, cat);
188 val2 = get_heap_comp_val(heap->arr[cpos], cat);
189 if (val1 > val2) {
190 struct ubifs_lprops *lp;
191
192 lp = heap->arr[cpos];
193 lp->flags &= ~LPROPS_CAT_MASK;
194 lp->flags |= LPROPS_UNCAT;
195 list_add(&lp->list, &c->uncat_list);
196 lprops->hpos = cpos;
197 heap->arr[cpos] = lprops;
198 move_up_lpt_heap(c, heap, lprops, cat);
199 dbg_check_heap(c, heap, cat, lprops->hpos);
200 return 1;
201 }
202 dbg_check_heap(c, heap, cat, -1);
203 return 0;
204 } else {
205 lprops->hpos = heap->cnt++;
206 heap->arr[lprops->hpos] = lprops;
207 move_up_lpt_heap(c, heap, lprops, cat);
208 dbg_check_heap(c, heap, cat, lprops->hpos);
209 return 1;
210 }
211}
212
213
214
215
216
217
218
219static void remove_from_lpt_heap(struct ubifs_info *c,
220 struct ubifs_lprops *lprops, int cat)
221{
222 struct ubifs_lpt_heap *heap;
223 int hpos = lprops->hpos;
224
225 heap = &c->lpt_heap[cat - 1];
226 ubifs_assert(hpos >= 0 && hpos < heap->cnt);
227 ubifs_assert(heap->arr[hpos] == lprops);
228 heap->cnt -= 1;
229 if (hpos < heap->cnt) {
230 heap->arr[hpos] = heap->arr[heap->cnt];
231 heap->arr[hpos]->hpos = hpos;
232 adjust_lpt_heap(c, heap, heap->arr[hpos], hpos, cat);
233 }
234 dbg_check_heap(c, heap, cat, -1);
235}
236
237
238
239
240
241
242
243
244
245
246
247
248
249static void lpt_heap_replace(struct ubifs_info *c,
250 struct ubifs_lprops *old_lprops,
251 struct ubifs_lprops *new_lprops, int cat)
252{
253 struct ubifs_lpt_heap *heap;
254 int hpos = new_lprops->hpos;
255
256 heap = &c->lpt_heap[cat - 1];
257 heap->arr[hpos] = new_lprops;
258}
259
260
261
262
263
264
265
266
267
268void ubifs_add_to_cat(struct ubifs_info *c, struct ubifs_lprops *lprops,
269 int cat)
270{
271 switch (cat) {
272 case LPROPS_DIRTY:
273 case LPROPS_DIRTY_IDX:
274 case LPROPS_FREE:
275 if (add_to_lpt_heap(c, lprops, cat))
276 break;
277
278 cat = LPROPS_UNCAT;
279
280 case LPROPS_UNCAT:
281 list_add(&lprops->list, &c->uncat_list);
282 break;
283 case LPROPS_EMPTY:
284 list_add(&lprops->list, &c->empty_list);
285 break;
286 case LPROPS_FREEABLE:
287 list_add(&lprops->list, &c->freeable_list);
288 c->freeable_cnt += 1;
289 break;
290 case LPROPS_FRDI_IDX:
291 list_add(&lprops->list, &c->frdi_idx_list);
292 break;
293 default:
294 ubifs_assert(0);
295 }
296
297 lprops->flags &= ~LPROPS_CAT_MASK;
298 lprops->flags |= cat;
299 c->in_a_category_cnt += 1;
300 ubifs_assert(c->in_a_category_cnt <= c->main_lebs);
301}
302
303
304
305
306
307
308
309
310
311static void ubifs_remove_from_cat(struct ubifs_info *c,
312 struct ubifs_lprops *lprops, int cat)
313{
314 switch (cat) {
315 case LPROPS_DIRTY:
316 case LPROPS_DIRTY_IDX:
317 case LPROPS_FREE:
318 remove_from_lpt_heap(c, lprops, cat);
319 break;
320 case LPROPS_FREEABLE:
321 c->freeable_cnt -= 1;
322 ubifs_assert(c->freeable_cnt >= 0);
323
324 case LPROPS_UNCAT:
325 case LPROPS_EMPTY:
326 case LPROPS_FRDI_IDX:
327 ubifs_assert(!list_empty(&lprops->list));
328 list_del(&lprops->list);
329 break;
330 default:
331 ubifs_assert(0);
332 }
333
334 c->in_a_category_cnt -= 1;
335 ubifs_assert(c->in_a_category_cnt >= 0);
336}
337
338
339
340
341
342
343
344
345
346
347
348void ubifs_replace_cat(struct ubifs_info *c, struct ubifs_lprops *old_lprops,
349 struct ubifs_lprops *new_lprops)
350{
351 int cat;
352
353 cat = new_lprops->flags & LPROPS_CAT_MASK;
354 switch (cat) {
355 case LPROPS_DIRTY:
356 case LPROPS_DIRTY_IDX:
357 case LPROPS_FREE:
358 lpt_heap_replace(c, old_lprops, new_lprops, cat);
359 break;
360 case LPROPS_UNCAT:
361 case LPROPS_EMPTY:
362 case LPROPS_FREEABLE:
363 case LPROPS_FRDI_IDX:
364 list_replace(&old_lprops->list, &new_lprops->list);
365 break;
366 default:
367 ubifs_assert(0);
368 }
369}
370
371
372
373
374
375
376
377
378
379
380void ubifs_ensure_cat(struct ubifs_info *c, struct ubifs_lprops *lprops)
381{
382 int cat = lprops->flags & LPROPS_CAT_MASK;
383
384 if (cat != LPROPS_UNCAT)
385 return;
386 cat = ubifs_categorize_lprops(c, lprops);
387 if (cat == LPROPS_UNCAT)
388 return;
389 ubifs_remove_from_cat(c, lprops, LPROPS_UNCAT);
390 ubifs_add_to_cat(c, lprops, cat);
391}
392
393
394
395
396
397
398
399
400
401
402
403int ubifs_categorize_lprops(const struct ubifs_info *c,
404 const struct ubifs_lprops *lprops)
405{
406 if (lprops->flags & LPROPS_TAKEN)
407 return LPROPS_UNCAT;
408
409 if (lprops->free == c->leb_size) {
410 ubifs_assert(!(lprops->flags & LPROPS_INDEX));
411 return LPROPS_EMPTY;
412 }
413
414 if (lprops->free + lprops->dirty == c->leb_size) {
415 if (lprops->flags & LPROPS_INDEX)
416 return LPROPS_FRDI_IDX;
417 else
418 return LPROPS_FREEABLE;
419 }
420
421 if (lprops->flags & LPROPS_INDEX) {
422 if (lprops->dirty + lprops->free >= c->min_idx_node_sz)
423 return LPROPS_DIRTY_IDX;
424 } else {
425 if (lprops->dirty >= c->dead_wm &&
426 lprops->dirty > lprops->free)
427 return LPROPS_DIRTY;
428 if (lprops->free > 0)
429 return LPROPS_FREE;
430 }
431
432 return LPROPS_UNCAT;
433}
434
435
436
437
438
439
440
441
442
443static void change_category(struct ubifs_info *c, struct ubifs_lprops *lprops)
444{
445 int old_cat = lprops->flags & LPROPS_CAT_MASK;
446 int new_cat = ubifs_categorize_lprops(c, lprops);
447
448 if (old_cat == new_cat) {
449 struct ubifs_lpt_heap *heap;
450
451
452 if (new_cat < 1 || new_cat > LPROPS_HEAP_CNT)
453 return;
454 heap = &c->lpt_heap[new_cat - 1];
455 adjust_lpt_heap(c, heap, lprops, lprops->hpos, new_cat);
456 } else {
457 ubifs_remove_from_cat(c, lprops, old_cat);
458 ubifs_add_to_cat(c, lprops, new_cat);
459 }
460}
461
462
463
464
465
466
467
468
469
470
471
472
473
474int ubifs_calc_dark(const struct ubifs_info *c, int spc)
475{
476 ubifs_assert(!(spc & 7));
477
478 if (spc < c->dark_wm)
479 return spc;
480
481
482
483
484
485
486 if (spc - c->dark_wm < MIN_WRITE_SZ)
487 return spc - MIN_WRITE_SZ;
488
489 return c->dark_wm;
490}
491
492
493
494
495
496
497static int is_lprops_dirty(struct ubifs_info *c, struct ubifs_lprops *lprops)
498{
499 struct ubifs_pnode *pnode;
500 int pos;
501
502 pos = (lprops->lnum - c->main_first) & (UBIFS_LPT_FANOUT - 1);
503 pnode = (struct ubifs_pnode *)container_of(lprops - pos,
504 struct ubifs_pnode,
505 lprops[0]);
506 return !test_bit(COW_CNODE, &pnode->flags) &&
507 test_bit(DIRTY_CNODE, &pnode->flags);
508}
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527const struct ubifs_lprops *ubifs_change_lp(struct ubifs_info *c,
528 const struct ubifs_lprops *lp,
529 int free, int dirty, int flags,
530 int idx_gc_cnt)
531{
532
533
534
535
536 struct ubifs_lprops *lprops = (struct ubifs_lprops *)lp;
537
538 dbg_lp("LEB %d, free %d, dirty %d, flags %d",
539 lprops->lnum, free, dirty, flags);
540
541 ubifs_assert(mutex_is_locked(&c->lp_mutex));
542 ubifs_assert(c->lst.empty_lebs >= 0 &&
543 c->lst.empty_lebs <= c->main_lebs);
544 ubifs_assert(c->freeable_cnt >= 0);
545 ubifs_assert(c->freeable_cnt <= c->main_lebs);
546 ubifs_assert(c->lst.taken_empty_lebs >= 0);
547 ubifs_assert(c->lst.taken_empty_lebs <= c->lst.empty_lebs);
548 ubifs_assert(!(c->lst.total_free & 7) && !(c->lst.total_dirty & 7));
549 ubifs_assert(!(c->lst.total_dead & 7) && !(c->lst.total_dark & 7));
550 ubifs_assert(!(c->lst.total_used & 7));
551 ubifs_assert(free == LPROPS_NC || free >= 0);
552 ubifs_assert(dirty == LPROPS_NC || dirty >= 0);
553
554 if (!is_lprops_dirty(c, lprops)) {
555 lprops = ubifs_lpt_lookup_dirty(c, lprops->lnum);
556 if (IS_ERR(lprops))
557 return lprops;
558 } else
559 ubifs_assert(lprops == ubifs_lpt_lookup_dirty(c, lprops->lnum));
560
561 ubifs_assert(!(lprops->free & 7) && !(lprops->dirty & 7));
562
563 spin_lock(&c->space_lock);
564 if ((lprops->flags & LPROPS_TAKEN) && lprops->free == c->leb_size)
565 c->lst.taken_empty_lebs -= 1;
566
567 if (!(lprops->flags & LPROPS_INDEX)) {
568 int old_spc;
569
570 old_spc = lprops->free + lprops->dirty;
571 if (old_spc < c->dead_wm)
572 c->lst.total_dead -= old_spc;
573 else
574 c->lst.total_dark -= ubifs_calc_dark(c, old_spc);
575
576 c->lst.total_used -= c->leb_size - old_spc;
577 }
578
579 if (free != LPROPS_NC) {
580 free = ALIGN(free, 8);
581 c->lst.total_free += free - lprops->free;
582
583
584 if (free == c->leb_size) {
585 if (lprops->free != c->leb_size)
586 c->lst.empty_lebs += 1;
587 } else if (lprops->free == c->leb_size)
588 c->lst.empty_lebs -= 1;
589 lprops->free = free;
590 }
591
592 if (dirty != LPROPS_NC) {
593 dirty = ALIGN(dirty, 8);
594 c->lst.total_dirty += dirty - lprops->dirty;
595 lprops->dirty = dirty;
596 }
597
598 if (flags != LPROPS_NC) {
599
600 if ((lprops->flags & LPROPS_INDEX)) {
601 if (!(flags & LPROPS_INDEX))
602 c->lst.idx_lebs -= 1;
603 } else if (flags & LPROPS_INDEX)
604 c->lst.idx_lebs += 1;
605 lprops->flags = flags;
606 }
607
608 if (!(lprops->flags & LPROPS_INDEX)) {
609 int new_spc;
610
611 new_spc = lprops->free + lprops->dirty;
612 if (new_spc < c->dead_wm)
613 c->lst.total_dead += new_spc;
614 else
615 c->lst.total_dark += ubifs_calc_dark(c, new_spc);
616
617 c->lst.total_used += c->leb_size - new_spc;
618 }
619
620 if ((lprops->flags & LPROPS_TAKEN) && lprops->free == c->leb_size)
621 c->lst.taken_empty_lebs += 1;
622
623 change_category(c, lprops);
624 c->idx_gc_cnt += idx_gc_cnt;
625 spin_unlock(&c->space_lock);
626 return lprops;
627}
628
629
630
631
632
633
634void ubifs_get_lp_stats(struct ubifs_info *c, struct ubifs_lp_stats *lst)
635{
636 spin_lock(&c->space_lock);
637 memcpy(lst, &c->lst, sizeof(struct ubifs_lp_stats));
638 spin_unlock(&c->space_lock);
639}
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656int ubifs_change_one_lp(struct ubifs_info *c, int lnum, int free, int dirty,
657 int flags_set, int flags_clean, int idx_gc_cnt)
658{
659 int err = 0, flags;
660 const struct ubifs_lprops *lp;
661
662 ubifs_get_lprops(c);
663
664 lp = ubifs_lpt_lookup_dirty(c, lnum);
665 if (IS_ERR(lp)) {
666 err = PTR_ERR(lp);
667 goto out;
668 }
669
670 flags = (lp->flags | flags_set) & ~flags_clean;
671 lp = ubifs_change_lp(c, lp, free, dirty, flags, idx_gc_cnt);
672 if (IS_ERR(lp))
673 err = PTR_ERR(lp);
674
675out:
676 ubifs_release_lprops(c);
677 if (err)
678 ubifs_err(c, "cannot change properties of LEB %d, error %d",
679 lnum, err);
680 return err;
681}
682
683
684
685
686
687
688
689
690
691
692
693
694
695int ubifs_update_one_lp(struct ubifs_info *c, int lnum, int free, int dirty,
696 int flags_set, int flags_clean)
697{
698 int err = 0, flags;
699 const struct ubifs_lprops *lp;
700
701 ubifs_get_lprops(c);
702
703 lp = ubifs_lpt_lookup_dirty(c, lnum);
704 if (IS_ERR(lp)) {
705 err = PTR_ERR(lp);
706 goto out;
707 }
708
709 flags = (lp->flags | flags_set) & ~flags_clean;
710 lp = ubifs_change_lp(c, lp, free, lp->dirty + dirty, flags, 0);
711 if (IS_ERR(lp))
712 err = PTR_ERR(lp);
713
714out:
715 ubifs_release_lprops(c);
716 if (err)
717 ubifs_err(c, "cannot update properties of LEB %d, error %d",
718 lnum, err);
719 return err;
720}
721
722
723
724
725
726
727
728
729
730
731
732int ubifs_read_one_lp(struct ubifs_info *c, int lnum, struct ubifs_lprops *lp)
733{
734 int err = 0;
735 const struct ubifs_lprops *lpp;
736
737 ubifs_get_lprops(c);
738
739 lpp = ubifs_lpt_lookup(c, lnum);
740 if (IS_ERR(lpp)) {
741 err = PTR_ERR(lpp);
742 ubifs_err(c, "cannot read properties of LEB %d, error %d",
743 lnum, err);
744 goto out;
745 }
746
747 memcpy(lp, lpp, sizeof(struct ubifs_lprops));
748
749out:
750 ubifs_release_lprops(c);
751 return err;
752}
753
754
755
756
757
758
759
760
761const struct ubifs_lprops *ubifs_fast_find_free(struct ubifs_info *c)
762{
763 struct ubifs_lprops *lprops;
764 struct ubifs_lpt_heap *heap;
765
766 ubifs_assert(mutex_is_locked(&c->lp_mutex));
767
768 heap = &c->lpt_heap[LPROPS_FREE - 1];
769 if (heap->cnt == 0)
770 return NULL;
771
772 lprops = heap->arr[0];
773 ubifs_assert(!(lprops->flags & LPROPS_TAKEN));
774 ubifs_assert(!(lprops->flags & LPROPS_INDEX));
775 return lprops;
776}
777
778
779
780
781
782
783
784
785const struct ubifs_lprops *ubifs_fast_find_empty(struct ubifs_info *c)
786{
787 struct ubifs_lprops *lprops;
788
789 ubifs_assert(mutex_is_locked(&c->lp_mutex));
790
791 if (list_empty(&c->empty_list))
792 return NULL;
793
794 lprops = list_entry(c->empty_list.next, struct ubifs_lprops, list);
795 ubifs_assert(!(lprops->flags & LPROPS_TAKEN));
796 ubifs_assert(!(lprops->flags & LPROPS_INDEX));
797 ubifs_assert(lprops->free == c->leb_size);
798 return lprops;
799}
800
801
802
803
804
805
806
807
808const struct ubifs_lprops *ubifs_fast_find_freeable(struct ubifs_info *c)
809{
810 struct ubifs_lprops *lprops;
811
812 ubifs_assert(mutex_is_locked(&c->lp_mutex));
813
814 if (list_empty(&c->freeable_list))
815 return NULL;
816
817 lprops = list_entry(c->freeable_list.next, struct ubifs_lprops, list);
818 ubifs_assert(!(lprops->flags & LPROPS_TAKEN));
819 ubifs_assert(!(lprops->flags & LPROPS_INDEX));
820 ubifs_assert(lprops->free + lprops->dirty == c->leb_size);
821 ubifs_assert(c->freeable_cnt > 0);
822 return lprops;
823}
824
825
826
827
828
829
830
831
832const struct ubifs_lprops *ubifs_fast_find_frdi_idx(struct ubifs_info *c)
833{
834 struct ubifs_lprops *lprops;
835
836 ubifs_assert(mutex_is_locked(&c->lp_mutex));
837
838 if (list_empty(&c->frdi_idx_list))
839 return NULL;
840
841 lprops = list_entry(c->frdi_idx_list.next, struct ubifs_lprops, list);
842 ubifs_assert(!(lprops->flags & LPROPS_TAKEN));
843 ubifs_assert((lprops->flags & LPROPS_INDEX));
844 ubifs_assert(lprops->free + lprops->dirty == c->leb_size);
845 return lprops;
846}
847
848
849
850
851
852
853
854
855
856
857
858int dbg_check_cats(struct ubifs_info *c)
859{
860 struct ubifs_lprops *lprops;
861 struct list_head *pos;
862 int i, cat;
863
864 if (!dbg_is_chk_gen(c) && !dbg_is_chk_lprops(c))
865 return 0;
866
867 list_for_each_entry(lprops, &c->empty_list, list) {
868 if (lprops->free != c->leb_size) {
869 ubifs_err(c, "non-empty LEB %d on empty list (free %d dirty %d flags %d)",
870 lprops->lnum, lprops->free, lprops->dirty,
871 lprops->flags);
872 return -EINVAL;
873 }
874 if (lprops->flags & LPROPS_TAKEN) {
875 ubifs_err(c, "taken LEB %d on empty list (free %d dirty %d flags %d)",
876 lprops->lnum, lprops->free, lprops->dirty,
877 lprops->flags);
878 return -EINVAL;
879 }
880 }
881
882 i = 0;
883 list_for_each_entry(lprops, &c->freeable_list, list) {
884 if (lprops->free + lprops->dirty != c->leb_size) {
885 ubifs_err(c, "non-freeable LEB %d on freeable list (free %d dirty %d flags %d)",
886 lprops->lnum, lprops->free, lprops->dirty,
887 lprops->flags);
888 return -EINVAL;
889 }
890 if (lprops->flags & LPROPS_TAKEN) {
891 ubifs_err(c, "taken LEB %d on freeable list (free %d dirty %d flags %d)",
892 lprops->lnum, lprops->free, lprops->dirty,
893 lprops->flags);
894 return -EINVAL;
895 }
896 i += 1;
897 }
898 if (i != c->freeable_cnt) {
899 ubifs_err(c, "freeable list count %d expected %d", i,
900 c->freeable_cnt);
901 return -EINVAL;
902 }
903
904 i = 0;
905 list_for_each(pos, &c->idx_gc)
906 i += 1;
907 if (i != c->idx_gc_cnt) {
908 ubifs_err(c, "idx_gc list count %d expected %d", i,
909 c->idx_gc_cnt);
910 return -EINVAL;
911 }
912
913 list_for_each_entry(lprops, &c->frdi_idx_list, list) {
914 if (lprops->free + lprops->dirty != c->leb_size) {
915 ubifs_err(c, "non-freeable LEB %d on frdi_idx list (free %d dirty %d flags %d)",
916 lprops->lnum, lprops->free, lprops->dirty,
917 lprops->flags);
918 return -EINVAL;
919 }
920 if (lprops->flags & LPROPS_TAKEN) {
921 ubifs_err(c, "taken LEB %d on frdi_idx list (free %d dirty %d flags %d)",
922 lprops->lnum, lprops->free, lprops->dirty,
923 lprops->flags);
924 return -EINVAL;
925 }
926 if (!(lprops->flags & LPROPS_INDEX)) {
927 ubifs_err(c, "non-index LEB %d on frdi_idx list (free %d dirty %d flags %d)",
928 lprops->lnum, lprops->free, lprops->dirty,
929 lprops->flags);
930 return -EINVAL;
931 }
932 }
933
934 for (cat = 1; cat <= LPROPS_HEAP_CNT; cat++) {
935 struct ubifs_lpt_heap *heap = &c->lpt_heap[cat - 1];
936
937 for (i = 0; i < heap->cnt; i++) {
938 lprops = heap->arr[i];
939 if (!lprops) {
940 ubifs_err(c, "null ptr in LPT heap cat %d", cat);
941 return -EINVAL;
942 }
943 if (lprops->hpos != i) {
944 ubifs_err(c, "bad ptr in LPT heap cat %d", cat);
945 return -EINVAL;
946 }
947 if (lprops->flags & LPROPS_TAKEN) {
948 ubifs_err(c, "taken LEB in LPT heap cat %d", cat);
949 return -EINVAL;
950 }
951 }
952 }
953
954 return 0;
955}
956
957void dbg_check_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat,
958 int add_pos)
959{
960 int i = 0, j, err = 0;
961
962 if (!dbg_is_chk_gen(c) && !dbg_is_chk_lprops(c))
963 return;
964
965 for (i = 0; i < heap->cnt; i++) {
966 struct ubifs_lprops *lprops = heap->arr[i];
967 struct ubifs_lprops *lp;
968
969 if (i != add_pos)
970 if ((lprops->flags & LPROPS_CAT_MASK) != cat) {
971 err = 1;
972 goto out;
973 }
974 if (lprops->hpos != i) {
975 err = 2;
976 goto out;
977 }
978 lp = ubifs_lpt_lookup(c, lprops->lnum);
979 if (IS_ERR(lp)) {
980 err = 3;
981 goto out;
982 }
983 if (lprops != lp) {
984 ubifs_err(c, "lprops %zx lp %zx lprops->lnum %d lp->lnum %d",
985 (size_t)lprops, (size_t)lp, lprops->lnum,
986 lp->lnum);
987 err = 4;
988 goto out;
989 }
990 for (j = 0; j < i; j++) {
991 lp = heap->arr[j];
992 if (lp == lprops) {
993 err = 5;
994 goto out;
995 }
996 if (lp->lnum == lprops->lnum) {
997 err = 6;
998 goto out;
999 }
1000 }
1001 }
1002out:
1003 if (err) {
1004 ubifs_err(c, "failed cat %d hpos %d err %d", cat, i, err);
1005 dump_stack();
1006 ubifs_dump_heap(c, heap, cat);
1007 }
1008}
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022static int scan_check_cb(struct ubifs_info *c,
1023 const struct ubifs_lprops *lp, int in_tree,
1024 struct ubifs_lp_stats *lst)
1025{
1026 struct ubifs_scan_leb *sleb;
1027 struct ubifs_scan_node *snod;
1028 int cat, lnum = lp->lnum, is_idx = 0, used = 0, free, dirty, ret;
1029 void *buf = NULL;
1030
1031 cat = lp->flags & LPROPS_CAT_MASK;
1032 if (cat != LPROPS_UNCAT) {
1033 cat = ubifs_categorize_lprops(c, lp);
1034 if (cat != (lp->flags & LPROPS_CAT_MASK)) {
1035 ubifs_err(c, "bad LEB category %d expected %d",
1036 (lp->flags & LPROPS_CAT_MASK), cat);
1037 return -EINVAL;
1038 }
1039 }
1040
1041
1042 if (in_tree) {
1043 struct list_head *list = NULL;
1044
1045 switch (cat) {
1046 case LPROPS_EMPTY:
1047 list = &c->empty_list;
1048 break;
1049 case LPROPS_FREEABLE:
1050 list = &c->freeable_list;
1051 break;
1052 case LPROPS_FRDI_IDX:
1053 list = &c->frdi_idx_list;
1054 break;
1055 case LPROPS_UNCAT:
1056 list = &c->uncat_list;
1057 break;
1058 }
1059 if (list) {
1060 struct ubifs_lprops *lprops;
1061 int found = 0;
1062
1063 list_for_each_entry(lprops, list, list) {
1064 if (lprops == lp) {
1065 found = 1;
1066 break;
1067 }
1068 }
1069 if (!found) {
1070 ubifs_err(c, "bad LPT list (category %d)", cat);
1071 return -EINVAL;
1072 }
1073 }
1074 }
1075
1076
1077 if (in_tree && cat > 0 && cat <= LPROPS_HEAP_CNT) {
1078 struct ubifs_lpt_heap *heap = &c->lpt_heap[cat - 1];
1079
1080 if ((lp->hpos != -1 && heap->arr[lp->hpos]->lnum != lnum) ||
1081 lp != heap->arr[lp->hpos]) {
1082 ubifs_err(c, "bad LPT heap (category %d)", cat);
1083 return -EINVAL;
1084 }
1085 }
1086
1087 buf = __vmalloc(c->leb_size, GFP_NOFS, PAGE_KERNEL);
1088 if (!buf)
1089 return -ENOMEM;
1090
1091
1092
1093
1094
1095 if (lp->free == c->leb_size) {
1096 lst->empty_lebs += 1;
1097 lst->total_free += c->leb_size;
1098 lst->total_dark += ubifs_calc_dark(c, c->leb_size);
1099 return LPT_SCAN_CONTINUE;
1100 }
1101 if (lp->free + lp->dirty == c->leb_size &&
1102 !(lp->flags & LPROPS_INDEX)) {
1103 lst->total_free += lp->free;
1104 lst->total_dirty += lp->dirty;
1105 lst->total_dark += ubifs_calc_dark(c, c->leb_size);
1106 return LPT_SCAN_CONTINUE;
1107 }
1108
1109 sleb = ubifs_scan(c, lnum, 0, buf, 0);
1110 if (IS_ERR(sleb)) {
1111 ret = PTR_ERR(sleb);
1112 if (ret == -EUCLEAN) {
1113 ubifs_dump_lprops(c);
1114 ubifs_dump_budg(c, &c->bi);
1115 }
1116 goto out;
1117 }
1118
1119 is_idx = -1;
1120 list_for_each_entry(snod, &sleb->nodes, list) {
1121 int found, level = 0;
1122
1123 cond_resched();
1124
1125 if (is_idx == -1)
1126 is_idx = (snod->type == UBIFS_IDX_NODE) ? 1 : 0;
1127
1128 if (is_idx && snod->type != UBIFS_IDX_NODE) {
1129 ubifs_err(c, "indexing node in data LEB %d:%d",
1130 lnum, snod->offs);
1131 goto out_destroy;
1132 }
1133
1134 if (snod->type == UBIFS_IDX_NODE) {
1135 struct ubifs_idx_node *idx = snod->node;
1136
1137 key_read(c, ubifs_idx_key(c, idx), &snod->key);
1138 level = le16_to_cpu(idx->level);
1139 }
1140
1141 found = ubifs_tnc_has_node(c, &snod->key, level, lnum,
1142 snod->offs, is_idx);
1143 if (found) {
1144 if (found < 0)
1145 goto out_destroy;
1146 used += ALIGN(snod->len, 8);
1147 }
1148 }
1149
1150 free = c->leb_size - sleb->endpt;
1151 dirty = sleb->endpt - used;
1152
1153 if (free > c->leb_size || free < 0 || dirty > c->leb_size ||
1154 dirty < 0) {
1155 ubifs_err(c, "bad calculated accounting for LEB %d: free %d, dirty %d",
1156 lnum, free, dirty);
1157 goto out_destroy;
1158 }
1159
1160 if (lp->free + lp->dirty == c->leb_size &&
1161 free + dirty == c->leb_size)
1162 if ((is_idx && !(lp->flags & LPROPS_INDEX)) ||
1163 (!is_idx && free == c->leb_size) ||
1164 lp->free == c->leb_size) {
1165
1166
1167
1168
1169
1170
1171
1172 free = lp->free;
1173 dirty = lp->dirty;
1174 is_idx = 0;
1175 }
1176
1177 if (is_idx && lp->free + lp->dirty == free + dirty &&
1178 lnum != c->ihead_lnum) {
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190 free = lp->free;
1191 dirty = lp->dirty;
1192 }
1193
1194 if (lp->free != free || lp->dirty != dirty)
1195 goto out_print;
1196
1197 if (is_idx && !(lp->flags & LPROPS_INDEX)) {
1198 if (free == c->leb_size)
1199
1200 is_idx = 0;
1201 else {
1202 ubifs_err(c, "indexing node without indexing flag");
1203 goto out_print;
1204 }
1205 }
1206
1207 if (!is_idx && (lp->flags & LPROPS_INDEX)) {
1208 ubifs_err(c, "data node with indexing flag");
1209 goto out_print;
1210 }
1211
1212 if (free == c->leb_size)
1213 lst->empty_lebs += 1;
1214
1215 if (is_idx)
1216 lst->idx_lebs += 1;
1217
1218 if (!(lp->flags & LPROPS_INDEX))
1219 lst->total_used += c->leb_size - free - dirty;
1220 lst->total_free += free;
1221 lst->total_dirty += dirty;
1222
1223 if (!(lp->flags & LPROPS_INDEX)) {
1224 int spc = free + dirty;
1225
1226 if (spc < c->dead_wm)
1227 lst->total_dead += spc;
1228 else
1229 lst->total_dark += ubifs_calc_dark(c, spc);
1230 }
1231
1232 ubifs_scan_destroy(sleb);
1233 vfree(buf);
1234 return LPT_SCAN_CONTINUE;
1235
1236out_print:
1237 ubifs_err(c, "bad accounting of LEB %d: free %d, dirty %d flags %#x, should be free %d, dirty %d",
1238 lnum, lp->free, lp->dirty, lp->flags, free, dirty);
1239 ubifs_dump_leb(c, lnum);
1240out_destroy:
1241 ubifs_scan_destroy(sleb);
1242 ret = -EINVAL;
1243out:
1244 vfree(buf);
1245 return ret;
1246}
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259int dbg_check_lprops(struct ubifs_info *c)
1260{
1261 int i, err;
1262 struct ubifs_lp_stats lst;
1263
1264 if (!dbg_is_chk_lprops(c))
1265 return 0;
1266
1267
1268
1269
1270
1271 for (i = 0; i < c->jhead_cnt; i++) {
1272 err = ubifs_wbuf_sync(&c->jheads[i].wbuf);
1273 if (err)
1274 return err;
1275 }
1276
1277 memset(&lst, 0, sizeof(struct ubifs_lp_stats));
1278 err = ubifs_lpt_scan_nolock(c, c->main_first, c->leb_cnt - 1,
1279 (ubifs_lpt_scan_callback)scan_check_cb,
1280 &lst);
1281 if (err && err != -ENOSPC)
1282 goto out;
1283
1284 if (lst.empty_lebs != c->lst.empty_lebs ||
1285 lst.idx_lebs != c->lst.idx_lebs ||
1286 lst.total_free != c->lst.total_free ||
1287 lst.total_dirty != c->lst.total_dirty ||
1288 lst.total_used != c->lst.total_used) {
1289 ubifs_err(c, "bad overall accounting");
1290 ubifs_err(c, "calculated: empty_lebs %d, idx_lebs %d, total_free %lld, total_dirty %lld, total_used %lld",
1291 lst.empty_lebs, lst.idx_lebs, lst.total_free,
1292 lst.total_dirty, lst.total_used);
1293 ubifs_err(c, "read from lprops: empty_lebs %d, idx_lebs %d, total_free %lld, total_dirty %lld, total_used %lld",
1294 c->lst.empty_lebs, c->lst.idx_lebs, c->lst.total_free,
1295 c->lst.total_dirty, c->lst.total_used);
1296 err = -EINVAL;
1297 goto out;
1298 }
1299
1300 if (lst.total_dead != c->lst.total_dead ||
1301 lst.total_dark != c->lst.total_dark) {
1302 ubifs_err(c, "bad dead/dark space accounting");
1303 ubifs_err(c, "calculated: total_dead %lld, total_dark %lld",
1304 lst.total_dead, lst.total_dark);
1305 ubifs_err(c, "read from lprops: total_dead %lld, total_dark %lld",
1306 c->lst.total_dead, c->lst.total_dark);
1307 err = -EINVAL;
1308 goto out;
1309 }
1310
1311 err = dbg_check_cats(c);
1312out:
1313 return err;
1314}
1315