0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019 #include "ubifs.h"
0020
0021
0022
0023
0024
0025
0026 static int get_heap_comp_val(struct ubifs_lprops *lprops, int cat)
0027 {
0028 switch (cat) {
0029 case LPROPS_FREE:
0030 return lprops->free;
0031 case LPROPS_DIRTY_IDX:
0032 return lprops->free + lprops->dirty;
0033 default:
0034 return lprops->dirty;
0035 }
0036 }
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050 static void move_up_lpt_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap,
0051 struct ubifs_lprops *lprops, int cat)
0052 {
0053 int val1, val2, hpos;
0054
0055 hpos = lprops->hpos;
0056 if (!hpos)
0057 return;
0058 val1 = get_heap_comp_val(lprops, cat);
0059
0060 do {
0061 int ppos = (hpos - 1) / 2;
0062
0063 val2 = get_heap_comp_val(heap->arr[ppos], cat);
0064 if (val2 >= val1)
0065 return;
0066
0067 heap->arr[ppos]->hpos = hpos;
0068 heap->arr[hpos] = heap->arr[ppos];
0069 heap->arr[ppos] = lprops;
0070 lprops->hpos = ppos;
0071 hpos = ppos;
0072 } while (hpos);
0073 }
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087 static void adjust_lpt_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap,
0088 struct ubifs_lprops *lprops, int hpos, int cat)
0089 {
0090 int val1, val2, val3, cpos;
0091
0092 val1 = get_heap_comp_val(lprops, cat);
0093
0094 if (hpos) {
0095 int ppos = (hpos - 1) / 2;
0096
0097 val2 = get_heap_comp_val(heap->arr[ppos], cat);
0098 if (val1 > val2) {
0099
0100 while (1) {
0101 heap->arr[ppos]->hpos = hpos;
0102 heap->arr[hpos] = heap->arr[ppos];
0103 heap->arr[ppos] = lprops;
0104 lprops->hpos = ppos;
0105 hpos = ppos;
0106 if (!hpos)
0107 return;
0108 ppos = (hpos - 1) / 2;
0109 val2 = get_heap_comp_val(heap->arr[ppos], cat);
0110 if (val1 <= val2)
0111 return;
0112
0113 }
0114 }
0115 }
0116
0117
0118 while (1) {
0119
0120 cpos = hpos * 2 + 1;
0121 if (cpos >= heap->cnt)
0122 return;
0123 val2 = get_heap_comp_val(heap->arr[cpos], cat);
0124 if (val1 < val2) {
0125
0126 if (cpos + 1 < heap->cnt) {
0127 val3 = get_heap_comp_val(heap->arr[cpos + 1],
0128 cat);
0129 if (val3 > val2)
0130 cpos += 1;
0131 }
0132 heap->arr[cpos]->hpos = hpos;
0133 heap->arr[hpos] = heap->arr[cpos];
0134 heap->arr[cpos] = lprops;
0135 lprops->hpos = cpos;
0136 hpos = cpos;
0137 continue;
0138 }
0139
0140 cpos += 1;
0141 if (cpos >= heap->cnt)
0142 return;
0143 val3 = get_heap_comp_val(heap->arr[cpos], cat);
0144 if (val1 < val3) {
0145
0146 heap->arr[cpos]->hpos = hpos;
0147 heap->arr[hpos] = heap->arr[cpos];
0148 heap->arr[cpos] = lprops;
0149 lprops->hpos = cpos;
0150 hpos = cpos;
0151 continue;
0152 }
0153 return;
0154 }
0155 }
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166 static int add_to_lpt_heap(struct ubifs_info *c, struct ubifs_lprops *lprops,
0167 int cat)
0168 {
0169 struct ubifs_lpt_heap *heap = &c->lpt_heap[cat - 1];
0170
0171 if (heap->cnt >= heap->max_cnt) {
0172 const int b = LPT_HEAP_SZ / 2 - 1;
0173 int cpos, val1, val2;
0174
0175
0176
0177 cpos = (((size_t)lprops >> 4) & b) + b;
0178 ubifs_assert(c, cpos >= b);
0179 ubifs_assert(c, cpos < LPT_HEAP_SZ);
0180 ubifs_assert(c, cpos < heap->cnt);
0181
0182 val1 = get_heap_comp_val(lprops, cat);
0183 val2 = get_heap_comp_val(heap->arr[cpos], cat);
0184 if (val1 > val2) {
0185 struct ubifs_lprops *lp;
0186
0187 lp = heap->arr[cpos];
0188 lp->flags &= ~LPROPS_CAT_MASK;
0189 lp->flags |= LPROPS_UNCAT;
0190 list_add(&lp->list, &c->uncat_list);
0191 lprops->hpos = cpos;
0192 heap->arr[cpos] = lprops;
0193 move_up_lpt_heap(c, heap, lprops, cat);
0194 dbg_check_heap(c, heap, cat, lprops->hpos);
0195 return 1;
0196 }
0197 dbg_check_heap(c, heap, cat, -1);
0198 return 0;
0199 } else {
0200 lprops->hpos = heap->cnt++;
0201 heap->arr[lprops->hpos] = lprops;
0202 move_up_lpt_heap(c, heap, lprops, cat);
0203 dbg_check_heap(c, heap, cat, lprops->hpos);
0204 return 1;
0205 }
0206 }
0207
0208
0209
0210
0211
0212
0213
0214 static void remove_from_lpt_heap(struct ubifs_info *c,
0215 struct ubifs_lprops *lprops, int cat)
0216 {
0217 struct ubifs_lpt_heap *heap;
0218 int hpos = lprops->hpos;
0219
0220 heap = &c->lpt_heap[cat - 1];
0221 ubifs_assert(c, hpos >= 0 && hpos < heap->cnt);
0222 ubifs_assert(c, heap->arr[hpos] == lprops);
0223 heap->cnt -= 1;
0224 if (hpos < heap->cnt) {
0225 heap->arr[hpos] = heap->arr[heap->cnt];
0226 heap->arr[hpos]->hpos = hpos;
0227 adjust_lpt_heap(c, heap, heap->arr[hpos], hpos, cat);
0228 }
0229 dbg_check_heap(c, heap, cat, -1);
0230 }
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243 static void lpt_heap_replace(struct ubifs_info *c,
0244 struct ubifs_lprops *new_lprops, int cat)
0245 {
0246 struct ubifs_lpt_heap *heap;
0247 int hpos = new_lprops->hpos;
0248
0249 heap = &c->lpt_heap[cat - 1];
0250 heap->arr[hpos] = new_lprops;
0251 }
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261 void ubifs_add_to_cat(struct ubifs_info *c, struct ubifs_lprops *lprops,
0262 int cat)
0263 {
0264 switch (cat) {
0265 case LPROPS_DIRTY:
0266 case LPROPS_DIRTY_IDX:
0267 case LPROPS_FREE:
0268 if (add_to_lpt_heap(c, lprops, cat))
0269 break;
0270
0271 cat = LPROPS_UNCAT;
0272 fallthrough;
0273 case LPROPS_UNCAT:
0274 list_add(&lprops->list, &c->uncat_list);
0275 break;
0276 case LPROPS_EMPTY:
0277 list_add(&lprops->list, &c->empty_list);
0278 break;
0279 case LPROPS_FREEABLE:
0280 list_add(&lprops->list, &c->freeable_list);
0281 c->freeable_cnt += 1;
0282 break;
0283 case LPROPS_FRDI_IDX:
0284 list_add(&lprops->list, &c->frdi_idx_list);
0285 break;
0286 default:
0287 ubifs_assert(c, 0);
0288 }
0289
0290 lprops->flags &= ~LPROPS_CAT_MASK;
0291 lprops->flags |= cat;
0292 c->in_a_category_cnt += 1;
0293 ubifs_assert(c, c->in_a_category_cnt <= c->main_lebs);
0294 }
0295
0296
0297
0298
0299
0300
0301
0302
0303
0304 static void ubifs_remove_from_cat(struct ubifs_info *c,
0305 struct ubifs_lprops *lprops, int cat)
0306 {
0307 switch (cat) {
0308 case LPROPS_DIRTY:
0309 case LPROPS_DIRTY_IDX:
0310 case LPROPS_FREE:
0311 remove_from_lpt_heap(c, lprops, cat);
0312 break;
0313 case LPROPS_FREEABLE:
0314 c->freeable_cnt -= 1;
0315 ubifs_assert(c, c->freeable_cnt >= 0);
0316 fallthrough;
0317 case LPROPS_UNCAT:
0318 case LPROPS_EMPTY:
0319 case LPROPS_FRDI_IDX:
0320 ubifs_assert(c, !list_empty(&lprops->list));
0321 list_del(&lprops->list);
0322 break;
0323 default:
0324 ubifs_assert(c, 0);
0325 }
0326
0327 c->in_a_category_cnt -= 1;
0328 ubifs_assert(c, c->in_a_category_cnt >= 0);
0329 }
0330
0331
0332
0333
0334
0335
0336
0337
0338
0339
0340
0341 void ubifs_replace_cat(struct ubifs_info *c, struct ubifs_lprops *old_lprops,
0342 struct ubifs_lprops *new_lprops)
0343 {
0344 int cat;
0345
0346 cat = new_lprops->flags & LPROPS_CAT_MASK;
0347 switch (cat) {
0348 case LPROPS_DIRTY:
0349 case LPROPS_DIRTY_IDX:
0350 case LPROPS_FREE:
0351 lpt_heap_replace(c, new_lprops, cat);
0352 break;
0353 case LPROPS_UNCAT:
0354 case LPROPS_EMPTY:
0355 case LPROPS_FREEABLE:
0356 case LPROPS_FRDI_IDX:
0357 list_replace(&old_lprops->list, &new_lprops->list);
0358 break;
0359 default:
0360 ubifs_assert(c, 0);
0361 }
0362 }
0363
0364
0365
0366
0367
0368
0369
0370
0371
0372
0373 void ubifs_ensure_cat(struct ubifs_info *c, struct ubifs_lprops *lprops)
0374 {
0375 int cat = lprops->flags & LPROPS_CAT_MASK;
0376
0377 if (cat != LPROPS_UNCAT)
0378 return;
0379 cat = ubifs_categorize_lprops(c, lprops);
0380 if (cat == LPROPS_UNCAT)
0381 return;
0382 ubifs_remove_from_cat(c, lprops, LPROPS_UNCAT);
0383 ubifs_add_to_cat(c, lprops, cat);
0384 }
0385
0386
0387
0388
0389
0390
0391
0392
0393
0394
0395
0396 int ubifs_categorize_lprops(const struct ubifs_info *c,
0397 const struct ubifs_lprops *lprops)
0398 {
0399 if (lprops->flags & LPROPS_TAKEN)
0400 return LPROPS_UNCAT;
0401
0402 if (lprops->free == c->leb_size) {
0403 ubifs_assert(c, !(lprops->flags & LPROPS_INDEX));
0404 return LPROPS_EMPTY;
0405 }
0406
0407 if (lprops->free + lprops->dirty == c->leb_size) {
0408 if (lprops->flags & LPROPS_INDEX)
0409 return LPROPS_FRDI_IDX;
0410 else
0411 return LPROPS_FREEABLE;
0412 }
0413
0414 if (lprops->flags & LPROPS_INDEX) {
0415 if (lprops->dirty + lprops->free >= c->min_idx_node_sz)
0416 return LPROPS_DIRTY_IDX;
0417 } else {
0418 if (lprops->dirty >= c->dead_wm &&
0419 lprops->dirty > lprops->free)
0420 return LPROPS_DIRTY;
0421 if (lprops->free > 0)
0422 return LPROPS_FREE;
0423 }
0424
0425 return LPROPS_UNCAT;
0426 }
0427
0428
0429
0430
0431
0432
0433
0434
0435
0436 static void change_category(struct ubifs_info *c, struct ubifs_lprops *lprops)
0437 {
0438 int old_cat = lprops->flags & LPROPS_CAT_MASK;
0439 int new_cat = ubifs_categorize_lprops(c, lprops);
0440
0441 if (old_cat == new_cat) {
0442 struct ubifs_lpt_heap *heap;
0443
0444
0445 if (new_cat < 1 || new_cat > LPROPS_HEAP_CNT)
0446 return;
0447 heap = &c->lpt_heap[new_cat - 1];
0448 adjust_lpt_heap(c, heap, lprops, lprops->hpos, new_cat);
0449 } else {
0450 ubifs_remove_from_cat(c, lprops, old_cat);
0451 ubifs_add_to_cat(c, lprops, new_cat);
0452 }
0453 }
0454
0455
0456
0457
0458
0459
0460
0461
0462
0463
0464
0465
0466
0467 int ubifs_calc_dark(const struct ubifs_info *c, int spc)
0468 {
0469 ubifs_assert(c, !(spc & 7));
0470
0471 if (spc < c->dark_wm)
0472 return spc;
0473
0474
0475
0476
0477
0478
0479 if (spc - c->dark_wm < MIN_WRITE_SZ)
0480 return spc - MIN_WRITE_SZ;
0481
0482 return c->dark_wm;
0483 }
0484
0485
0486
0487
0488
0489
0490 static int is_lprops_dirty(struct ubifs_info *c, struct ubifs_lprops *lprops)
0491 {
0492 struct ubifs_pnode *pnode;
0493 int pos;
0494
0495 pos = (lprops->lnum - c->main_first) & (UBIFS_LPT_FANOUT - 1);
0496 pnode = (struct ubifs_pnode *)container_of(lprops - pos,
0497 struct ubifs_pnode,
0498 lprops[0]);
0499 return !test_bit(COW_CNODE, &pnode->flags) &&
0500 test_bit(DIRTY_CNODE, &pnode->flags);
0501 }
0502
0503
0504
0505
0506
0507
0508
0509
0510
0511
0512
0513
0514
0515
0516
0517
0518
0519
0520 const struct ubifs_lprops *ubifs_change_lp(struct ubifs_info *c,
0521 const struct ubifs_lprops *lp,
0522 int free, int dirty, int flags,
0523 int idx_gc_cnt)
0524 {
0525
0526
0527
0528
0529 struct ubifs_lprops *lprops = (struct ubifs_lprops *)lp;
0530
0531 dbg_lp("LEB %d, free %d, dirty %d, flags %d",
0532 lprops->lnum, free, dirty, flags);
0533
0534 ubifs_assert(c, mutex_is_locked(&c->lp_mutex));
0535 ubifs_assert(c, c->lst.empty_lebs >= 0 &&
0536 c->lst.empty_lebs <= c->main_lebs);
0537 ubifs_assert(c, c->freeable_cnt >= 0);
0538 ubifs_assert(c, c->freeable_cnt <= c->main_lebs);
0539 ubifs_assert(c, c->lst.taken_empty_lebs >= 0);
0540 ubifs_assert(c, c->lst.taken_empty_lebs <= c->lst.empty_lebs);
0541 ubifs_assert(c, !(c->lst.total_free & 7) && !(c->lst.total_dirty & 7));
0542 ubifs_assert(c, !(c->lst.total_dead & 7) && !(c->lst.total_dark & 7));
0543 ubifs_assert(c, !(c->lst.total_used & 7));
0544 ubifs_assert(c, free == LPROPS_NC || free >= 0);
0545 ubifs_assert(c, dirty == LPROPS_NC || dirty >= 0);
0546
0547 if (!is_lprops_dirty(c, lprops)) {
0548 lprops = ubifs_lpt_lookup_dirty(c, lprops->lnum);
0549 if (IS_ERR(lprops))
0550 return lprops;
0551 } else
0552 ubifs_assert(c, lprops == ubifs_lpt_lookup_dirty(c, lprops->lnum));
0553
0554 ubifs_assert(c, !(lprops->free & 7) && !(lprops->dirty & 7));
0555
0556 spin_lock(&c->space_lock);
0557 if ((lprops->flags & LPROPS_TAKEN) && lprops->free == c->leb_size)
0558 c->lst.taken_empty_lebs -= 1;
0559
0560 if (!(lprops->flags & LPROPS_INDEX)) {
0561 int old_spc;
0562
0563 old_spc = lprops->free + lprops->dirty;
0564 if (old_spc < c->dead_wm)
0565 c->lst.total_dead -= old_spc;
0566 else
0567 c->lst.total_dark -= ubifs_calc_dark(c, old_spc);
0568
0569 c->lst.total_used -= c->leb_size - old_spc;
0570 }
0571
0572 if (free != LPROPS_NC) {
0573 free = ALIGN(free, 8);
0574 c->lst.total_free += free - lprops->free;
0575
0576
0577 if (free == c->leb_size) {
0578 if (lprops->free != c->leb_size)
0579 c->lst.empty_lebs += 1;
0580 } else if (lprops->free == c->leb_size)
0581 c->lst.empty_lebs -= 1;
0582 lprops->free = free;
0583 }
0584
0585 if (dirty != LPROPS_NC) {
0586 dirty = ALIGN(dirty, 8);
0587 c->lst.total_dirty += dirty - lprops->dirty;
0588 lprops->dirty = dirty;
0589 }
0590
0591 if (flags != LPROPS_NC) {
0592
0593 if ((lprops->flags & LPROPS_INDEX)) {
0594 if (!(flags & LPROPS_INDEX))
0595 c->lst.idx_lebs -= 1;
0596 } else if (flags & LPROPS_INDEX)
0597 c->lst.idx_lebs += 1;
0598 lprops->flags = flags;
0599 }
0600
0601 if (!(lprops->flags & LPROPS_INDEX)) {
0602 int new_spc;
0603
0604 new_spc = lprops->free + lprops->dirty;
0605 if (new_spc < c->dead_wm)
0606 c->lst.total_dead += new_spc;
0607 else
0608 c->lst.total_dark += ubifs_calc_dark(c, new_spc);
0609
0610 c->lst.total_used += c->leb_size - new_spc;
0611 }
0612
0613 if ((lprops->flags & LPROPS_TAKEN) && lprops->free == c->leb_size)
0614 c->lst.taken_empty_lebs += 1;
0615
0616 change_category(c, lprops);
0617 c->idx_gc_cnt += idx_gc_cnt;
0618 spin_unlock(&c->space_lock);
0619 return lprops;
0620 }
0621
0622
0623
0624
0625
0626
0627 void ubifs_get_lp_stats(struct ubifs_info *c, struct ubifs_lp_stats *lst)
0628 {
0629 spin_lock(&c->space_lock);
0630 memcpy(lst, &c->lst, sizeof(struct ubifs_lp_stats));
0631 spin_unlock(&c->space_lock);
0632 }
0633
0634
0635
0636
0637
0638
0639
0640
0641
0642
0643
0644
0645
0646
0647
0648
0649 int ubifs_change_one_lp(struct ubifs_info *c, int lnum, int free, int dirty,
0650 int flags_set, int flags_clean, int idx_gc_cnt)
0651 {
0652 int err = 0, flags;
0653 const struct ubifs_lprops *lp;
0654
0655 ubifs_get_lprops(c);
0656
0657 lp = ubifs_lpt_lookup_dirty(c, lnum);
0658 if (IS_ERR(lp)) {
0659 err = PTR_ERR(lp);
0660 goto out;
0661 }
0662
0663 flags = (lp->flags | flags_set) & ~flags_clean;
0664 lp = ubifs_change_lp(c, lp, free, dirty, flags, idx_gc_cnt);
0665 if (IS_ERR(lp))
0666 err = PTR_ERR(lp);
0667
0668 out:
0669 ubifs_release_lprops(c);
0670 if (err)
0671 ubifs_err(c, "cannot change properties of LEB %d, error %d",
0672 lnum, err);
0673 return err;
0674 }
0675
0676
0677
0678
0679
0680
0681
0682
0683
0684
0685
0686
0687
0688 int ubifs_update_one_lp(struct ubifs_info *c, int lnum, int free, int dirty,
0689 int flags_set, int flags_clean)
0690 {
0691 int err = 0, flags;
0692 const struct ubifs_lprops *lp;
0693
0694 ubifs_get_lprops(c);
0695
0696 lp = ubifs_lpt_lookup_dirty(c, lnum);
0697 if (IS_ERR(lp)) {
0698 err = PTR_ERR(lp);
0699 goto out;
0700 }
0701
0702 flags = (lp->flags | flags_set) & ~flags_clean;
0703 lp = ubifs_change_lp(c, lp, free, lp->dirty + dirty, flags, 0);
0704 if (IS_ERR(lp))
0705 err = PTR_ERR(lp);
0706
0707 out:
0708 ubifs_release_lprops(c);
0709 if (err)
0710 ubifs_err(c, "cannot update properties of LEB %d, error %d",
0711 lnum, err);
0712 return err;
0713 }
0714
0715
0716
0717
0718
0719
0720
0721
0722
0723
0724
0725 int ubifs_read_one_lp(struct ubifs_info *c, int lnum, struct ubifs_lprops *lp)
0726 {
0727 int err = 0;
0728 const struct ubifs_lprops *lpp;
0729
0730 ubifs_get_lprops(c);
0731
0732 lpp = ubifs_lpt_lookup(c, lnum);
0733 if (IS_ERR(lpp)) {
0734 err = PTR_ERR(lpp);
0735 ubifs_err(c, "cannot read properties of LEB %d, error %d",
0736 lnum, err);
0737 goto out;
0738 }
0739
0740 memcpy(lp, lpp, sizeof(struct ubifs_lprops));
0741
0742 out:
0743 ubifs_release_lprops(c);
0744 return err;
0745 }
0746
0747
0748
0749
0750
0751
0752
0753
0754 const struct ubifs_lprops *ubifs_fast_find_free(struct ubifs_info *c)
0755 {
0756 struct ubifs_lprops *lprops;
0757 struct ubifs_lpt_heap *heap;
0758
0759 ubifs_assert(c, mutex_is_locked(&c->lp_mutex));
0760
0761 heap = &c->lpt_heap[LPROPS_FREE - 1];
0762 if (heap->cnt == 0)
0763 return NULL;
0764
0765 lprops = heap->arr[0];
0766 ubifs_assert(c, !(lprops->flags & LPROPS_TAKEN));
0767 ubifs_assert(c, !(lprops->flags & LPROPS_INDEX));
0768 return lprops;
0769 }
0770
0771
0772
0773
0774
0775
0776
0777
0778 const struct ubifs_lprops *ubifs_fast_find_empty(struct ubifs_info *c)
0779 {
0780 struct ubifs_lprops *lprops;
0781
0782 ubifs_assert(c, mutex_is_locked(&c->lp_mutex));
0783
0784 if (list_empty(&c->empty_list))
0785 return NULL;
0786
0787 lprops = list_entry(c->empty_list.next, struct ubifs_lprops, list);
0788 ubifs_assert(c, !(lprops->flags & LPROPS_TAKEN));
0789 ubifs_assert(c, !(lprops->flags & LPROPS_INDEX));
0790 ubifs_assert(c, lprops->free == c->leb_size);
0791 return lprops;
0792 }
0793
0794
0795
0796
0797
0798
0799
0800
0801 const struct ubifs_lprops *ubifs_fast_find_freeable(struct ubifs_info *c)
0802 {
0803 struct ubifs_lprops *lprops;
0804
0805 ubifs_assert(c, mutex_is_locked(&c->lp_mutex));
0806
0807 if (list_empty(&c->freeable_list))
0808 return NULL;
0809
0810 lprops = list_entry(c->freeable_list.next, struct ubifs_lprops, list);
0811 ubifs_assert(c, !(lprops->flags & LPROPS_TAKEN));
0812 ubifs_assert(c, !(lprops->flags & LPROPS_INDEX));
0813 ubifs_assert(c, lprops->free + lprops->dirty == c->leb_size);
0814 ubifs_assert(c, c->freeable_cnt > 0);
0815 return lprops;
0816 }
0817
0818
0819
0820
0821
0822
0823
0824
0825 const struct ubifs_lprops *ubifs_fast_find_frdi_idx(struct ubifs_info *c)
0826 {
0827 struct ubifs_lprops *lprops;
0828
0829 ubifs_assert(c, mutex_is_locked(&c->lp_mutex));
0830
0831 if (list_empty(&c->frdi_idx_list))
0832 return NULL;
0833
0834 lprops = list_entry(c->frdi_idx_list.next, struct ubifs_lprops, list);
0835 ubifs_assert(c, !(lprops->flags & LPROPS_TAKEN));
0836 ubifs_assert(c, (lprops->flags & LPROPS_INDEX));
0837 ubifs_assert(c, lprops->free + lprops->dirty == c->leb_size);
0838 return lprops;
0839 }
0840
0841
0842
0843
0844
0845
0846
0847
0848
0849
0850
0851 int dbg_check_cats(struct ubifs_info *c)
0852 {
0853 struct ubifs_lprops *lprops;
0854 struct list_head *pos;
0855 int i, cat;
0856
0857 if (!dbg_is_chk_gen(c) && !dbg_is_chk_lprops(c))
0858 return 0;
0859
0860 list_for_each_entry(lprops, &c->empty_list, list) {
0861 if (lprops->free != c->leb_size) {
0862 ubifs_err(c, "non-empty LEB %d on empty list (free %d dirty %d flags %d)",
0863 lprops->lnum, lprops->free, lprops->dirty,
0864 lprops->flags);
0865 return -EINVAL;
0866 }
0867 if (lprops->flags & LPROPS_TAKEN) {
0868 ubifs_err(c, "taken LEB %d on empty list (free %d dirty %d flags %d)",
0869 lprops->lnum, lprops->free, lprops->dirty,
0870 lprops->flags);
0871 return -EINVAL;
0872 }
0873 }
0874
0875 i = 0;
0876 list_for_each_entry(lprops, &c->freeable_list, list) {
0877 if (lprops->free + lprops->dirty != c->leb_size) {
0878 ubifs_err(c, "non-freeable LEB %d on freeable list (free %d dirty %d flags %d)",
0879 lprops->lnum, lprops->free, lprops->dirty,
0880 lprops->flags);
0881 return -EINVAL;
0882 }
0883 if (lprops->flags & LPROPS_TAKEN) {
0884 ubifs_err(c, "taken LEB %d on freeable list (free %d dirty %d flags %d)",
0885 lprops->lnum, lprops->free, lprops->dirty,
0886 lprops->flags);
0887 return -EINVAL;
0888 }
0889 i += 1;
0890 }
0891 if (i != c->freeable_cnt) {
0892 ubifs_err(c, "freeable list count %d expected %d", i,
0893 c->freeable_cnt);
0894 return -EINVAL;
0895 }
0896
0897 i = 0;
0898 list_for_each(pos, &c->idx_gc)
0899 i += 1;
0900 if (i != c->idx_gc_cnt) {
0901 ubifs_err(c, "idx_gc list count %d expected %d", i,
0902 c->idx_gc_cnt);
0903 return -EINVAL;
0904 }
0905
0906 list_for_each_entry(lprops, &c->frdi_idx_list, list) {
0907 if (lprops->free + lprops->dirty != c->leb_size) {
0908 ubifs_err(c, "non-freeable LEB %d on frdi_idx list (free %d dirty %d flags %d)",
0909 lprops->lnum, lprops->free, lprops->dirty,
0910 lprops->flags);
0911 return -EINVAL;
0912 }
0913 if (lprops->flags & LPROPS_TAKEN) {
0914 ubifs_err(c, "taken LEB %d on frdi_idx list (free %d dirty %d flags %d)",
0915 lprops->lnum, lprops->free, lprops->dirty,
0916 lprops->flags);
0917 return -EINVAL;
0918 }
0919 if (!(lprops->flags & LPROPS_INDEX)) {
0920 ubifs_err(c, "non-index LEB %d on frdi_idx list (free %d dirty %d flags %d)",
0921 lprops->lnum, lprops->free, lprops->dirty,
0922 lprops->flags);
0923 return -EINVAL;
0924 }
0925 }
0926
0927 for (cat = 1; cat <= LPROPS_HEAP_CNT; cat++) {
0928 struct ubifs_lpt_heap *heap = &c->lpt_heap[cat - 1];
0929
0930 for (i = 0; i < heap->cnt; i++) {
0931 lprops = heap->arr[i];
0932 if (!lprops) {
0933 ubifs_err(c, "null ptr in LPT heap cat %d", cat);
0934 return -EINVAL;
0935 }
0936 if (lprops->hpos != i) {
0937 ubifs_err(c, "bad ptr in LPT heap cat %d", cat);
0938 return -EINVAL;
0939 }
0940 if (lprops->flags & LPROPS_TAKEN) {
0941 ubifs_err(c, "taken LEB in LPT heap cat %d", cat);
0942 return -EINVAL;
0943 }
0944 }
0945 }
0946
0947 return 0;
0948 }
0949
0950 void dbg_check_heap(struct ubifs_info *c, struct ubifs_lpt_heap *heap, int cat,
0951 int add_pos)
0952 {
0953 int i = 0, j, err = 0;
0954
0955 if (!dbg_is_chk_gen(c) && !dbg_is_chk_lprops(c))
0956 return;
0957
0958 for (i = 0; i < heap->cnt; i++) {
0959 struct ubifs_lprops *lprops = heap->arr[i];
0960 struct ubifs_lprops *lp;
0961
0962 if (i != add_pos)
0963 if ((lprops->flags & LPROPS_CAT_MASK) != cat) {
0964 err = 1;
0965 goto out;
0966 }
0967 if (lprops->hpos != i) {
0968 err = 2;
0969 goto out;
0970 }
0971 lp = ubifs_lpt_lookup(c, lprops->lnum);
0972 if (IS_ERR(lp)) {
0973 err = 3;
0974 goto out;
0975 }
0976 if (lprops != lp) {
0977 ubifs_err(c, "lprops %zx lp %zx lprops->lnum %d lp->lnum %d",
0978 (size_t)lprops, (size_t)lp, lprops->lnum,
0979 lp->lnum);
0980 err = 4;
0981 goto out;
0982 }
0983 for (j = 0; j < i; j++) {
0984 lp = heap->arr[j];
0985 if (lp == lprops) {
0986 err = 5;
0987 goto out;
0988 }
0989 if (lp->lnum == lprops->lnum) {
0990 err = 6;
0991 goto out;
0992 }
0993 }
0994 }
0995 out:
0996 if (err) {
0997 ubifs_err(c, "failed cat %d hpos %d err %d", cat, i, err);
0998 dump_stack();
0999 ubifs_dump_heap(c, heap, cat);
1000 }
1001 }
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015 static int scan_check_cb(struct ubifs_info *c,
1016 const struct ubifs_lprops *lp, int in_tree,
1017 struct ubifs_lp_stats *lst)
1018 {
1019 struct ubifs_scan_leb *sleb;
1020 struct ubifs_scan_node *snod;
1021 int cat, lnum = lp->lnum, is_idx = 0, used = 0, free, dirty, ret;
1022 void *buf = NULL;
1023
1024 cat = lp->flags & LPROPS_CAT_MASK;
1025 if (cat != LPROPS_UNCAT) {
1026 cat = ubifs_categorize_lprops(c, lp);
1027 if (cat != (lp->flags & LPROPS_CAT_MASK)) {
1028 ubifs_err(c, "bad LEB category %d expected %d",
1029 (lp->flags & LPROPS_CAT_MASK), cat);
1030 return -EINVAL;
1031 }
1032 }
1033
1034
1035 if (in_tree) {
1036 struct list_head *list = NULL;
1037
1038 switch (cat) {
1039 case LPROPS_EMPTY:
1040 list = &c->empty_list;
1041 break;
1042 case LPROPS_FREEABLE:
1043 list = &c->freeable_list;
1044 break;
1045 case LPROPS_FRDI_IDX:
1046 list = &c->frdi_idx_list;
1047 break;
1048 case LPROPS_UNCAT:
1049 list = &c->uncat_list;
1050 break;
1051 }
1052 if (list) {
1053 struct ubifs_lprops *lprops;
1054 int found = 0;
1055
1056 list_for_each_entry(lprops, list, list) {
1057 if (lprops == lp) {
1058 found = 1;
1059 break;
1060 }
1061 }
1062 if (!found) {
1063 ubifs_err(c, "bad LPT list (category %d)", cat);
1064 return -EINVAL;
1065 }
1066 }
1067 }
1068
1069
1070 if (in_tree && cat > 0 && cat <= LPROPS_HEAP_CNT) {
1071 struct ubifs_lpt_heap *heap = &c->lpt_heap[cat - 1];
1072
1073 if ((lp->hpos != -1 && heap->arr[lp->hpos]->lnum != lnum) ||
1074 lp != heap->arr[lp->hpos]) {
1075 ubifs_err(c, "bad LPT heap (category %d)", cat);
1076 return -EINVAL;
1077 }
1078 }
1079
1080
1081
1082
1083
1084 if (lp->free == c->leb_size) {
1085 lst->empty_lebs += 1;
1086 lst->total_free += c->leb_size;
1087 lst->total_dark += ubifs_calc_dark(c, c->leb_size);
1088 return LPT_SCAN_CONTINUE;
1089 }
1090 if (lp->free + lp->dirty == c->leb_size &&
1091 !(lp->flags & LPROPS_INDEX)) {
1092 lst->total_free += lp->free;
1093 lst->total_dirty += lp->dirty;
1094 lst->total_dark += ubifs_calc_dark(c, c->leb_size);
1095 return LPT_SCAN_CONTINUE;
1096 }
1097
1098 buf = __vmalloc(c->leb_size, GFP_NOFS);
1099 if (!buf)
1100 return -ENOMEM;
1101
1102 sleb = ubifs_scan(c, lnum, 0, buf, 0);
1103 if (IS_ERR(sleb)) {
1104 ret = PTR_ERR(sleb);
1105 if (ret == -EUCLEAN) {
1106 ubifs_dump_lprops(c);
1107 ubifs_dump_budg(c, &c->bi);
1108 }
1109 goto out;
1110 }
1111
1112 is_idx = -1;
1113 list_for_each_entry(snod, &sleb->nodes, list) {
1114 int found, level = 0;
1115
1116 cond_resched();
1117
1118 if (is_idx == -1)
1119 is_idx = (snod->type == UBIFS_IDX_NODE) ? 1 : 0;
1120
1121 if (is_idx && snod->type != UBIFS_IDX_NODE) {
1122 ubifs_err(c, "indexing node in data LEB %d:%d",
1123 lnum, snod->offs);
1124 goto out_destroy;
1125 }
1126
1127 if (snod->type == UBIFS_IDX_NODE) {
1128 struct ubifs_idx_node *idx = snod->node;
1129
1130 key_read(c, ubifs_idx_key(c, idx), &snod->key);
1131 level = le16_to_cpu(idx->level);
1132 }
1133
1134 found = ubifs_tnc_has_node(c, &snod->key, level, lnum,
1135 snod->offs, is_idx);
1136 if (found) {
1137 if (found < 0)
1138 goto out_destroy;
1139 used += ALIGN(snod->len, 8);
1140 }
1141 }
1142
1143 free = c->leb_size - sleb->endpt;
1144 dirty = sleb->endpt - used;
1145
1146 if (free > c->leb_size || free < 0 || dirty > c->leb_size ||
1147 dirty < 0) {
1148 ubifs_err(c, "bad calculated accounting for LEB %d: free %d, dirty %d",
1149 lnum, free, dirty);
1150 goto out_destroy;
1151 }
1152
1153 if (lp->free + lp->dirty == c->leb_size &&
1154 free + dirty == c->leb_size)
1155 if ((is_idx && !(lp->flags & LPROPS_INDEX)) ||
1156 (!is_idx && free == c->leb_size) ||
1157 lp->free == c->leb_size) {
1158
1159
1160
1161
1162
1163
1164
1165 free = lp->free;
1166 dirty = lp->dirty;
1167 is_idx = 0;
1168 }
1169
1170 if (is_idx && lp->free + lp->dirty == free + dirty &&
1171 lnum != c->ihead_lnum) {
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183 free = lp->free;
1184 dirty = lp->dirty;
1185 }
1186
1187 if (lp->free != free || lp->dirty != dirty)
1188 goto out_print;
1189
1190 if (is_idx && !(lp->flags & LPROPS_INDEX)) {
1191 if (free == c->leb_size)
1192
1193 is_idx = 0;
1194 else {
1195 ubifs_err(c, "indexing node without indexing flag");
1196 goto out_print;
1197 }
1198 }
1199
1200 if (!is_idx && (lp->flags & LPROPS_INDEX)) {
1201 ubifs_err(c, "data node with indexing flag");
1202 goto out_print;
1203 }
1204
1205 if (free == c->leb_size)
1206 lst->empty_lebs += 1;
1207
1208 if (is_idx)
1209 lst->idx_lebs += 1;
1210
1211 if (!(lp->flags & LPROPS_INDEX))
1212 lst->total_used += c->leb_size - free - dirty;
1213 lst->total_free += free;
1214 lst->total_dirty += dirty;
1215
1216 if (!(lp->flags & LPROPS_INDEX)) {
1217 int spc = free + dirty;
1218
1219 if (spc < c->dead_wm)
1220 lst->total_dead += spc;
1221 else
1222 lst->total_dark += ubifs_calc_dark(c, spc);
1223 }
1224
1225 ubifs_scan_destroy(sleb);
1226 vfree(buf);
1227 return LPT_SCAN_CONTINUE;
1228
1229 out_print:
1230 ubifs_err(c, "bad accounting of LEB %d: free %d, dirty %d flags %#x, should be free %d, dirty %d",
1231 lnum, lp->free, lp->dirty, lp->flags, free, dirty);
1232 ubifs_dump_leb(c, lnum);
1233 out_destroy:
1234 ubifs_scan_destroy(sleb);
1235 ret = -EINVAL;
1236 out:
1237 vfree(buf);
1238 return ret;
1239 }
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252 int dbg_check_lprops(struct ubifs_info *c)
1253 {
1254 int i, err;
1255 struct ubifs_lp_stats lst;
1256
1257 if (!dbg_is_chk_lprops(c))
1258 return 0;
1259
1260
1261
1262
1263
1264 for (i = 0; i < c->jhead_cnt; i++) {
1265 err = ubifs_wbuf_sync(&c->jheads[i].wbuf);
1266 if (err)
1267 return err;
1268 }
1269
1270 memset(&lst, 0, sizeof(struct ubifs_lp_stats));
1271 err = ubifs_lpt_scan_nolock(c, c->main_first, c->leb_cnt - 1,
1272 (ubifs_lpt_scan_callback)scan_check_cb,
1273 &lst);
1274 if (err && err != -ENOSPC)
1275 goto out;
1276
1277 if (lst.empty_lebs != c->lst.empty_lebs ||
1278 lst.idx_lebs != c->lst.idx_lebs ||
1279 lst.total_free != c->lst.total_free ||
1280 lst.total_dirty != c->lst.total_dirty ||
1281 lst.total_used != c->lst.total_used) {
1282 ubifs_err(c, "bad overall accounting");
1283 ubifs_err(c, "calculated: empty_lebs %d, idx_lebs %d, total_free %lld, total_dirty %lld, total_used %lld",
1284 lst.empty_lebs, lst.idx_lebs, lst.total_free,
1285 lst.total_dirty, lst.total_used);
1286 ubifs_err(c, "read from lprops: empty_lebs %d, idx_lebs %d, total_free %lld, total_dirty %lld, total_used %lld",
1287 c->lst.empty_lebs, c->lst.idx_lebs, c->lst.total_free,
1288 c->lst.total_dirty, c->lst.total_used);
1289 err = -EINVAL;
1290 goto out;
1291 }
1292
1293 if (lst.total_dead != c->lst.total_dead ||
1294 lst.total_dark != c->lst.total_dark) {
1295 ubifs_err(c, "bad dead/dark space accounting");
1296 ubifs_err(c, "calculated: total_dead %lld, total_dark %lld",
1297 lst.total_dead, lst.total_dark);
1298 ubifs_err(c, "read from lprops: total_dead %lld, total_dark %lld",
1299 c->lst.total_dead, c->lst.total_dark);
1300 err = -EINVAL;
1301 goto out;
1302 }
1303
1304 err = dbg_check_cats(c);
1305 out:
1306 return err;
1307 }