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