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