0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include "ubifs.h"
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043 static int dbg_check_orphans(struct ubifs_info *c);
0044
0045 static struct ubifs_orphan *orphan_add(struct ubifs_info *c, ino_t inum,
0046 struct ubifs_orphan *parent_orphan)
0047 {
0048 struct ubifs_orphan *orphan, *o;
0049 struct rb_node **p, *parent = NULL;
0050
0051 orphan = kzalloc(sizeof(struct ubifs_orphan), GFP_NOFS);
0052 if (!orphan)
0053 return ERR_PTR(-ENOMEM);
0054 orphan->inum = inum;
0055 orphan->new = 1;
0056 INIT_LIST_HEAD(&orphan->child_list);
0057
0058 spin_lock(&c->orphan_lock);
0059 if (c->tot_orphans >= c->max_orphans) {
0060 spin_unlock(&c->orphan_lock);
0061 kfree(orphan);
0062 return ERR_PTR(-ENFILE);
0063 }
0064 p = &c->orph_tree.rb_node;
0065 while (*p) {
0066 parent = *p;
0067 o = rb_entry(parent, struct ubifs_orphan, rb);
0068 if (inum < o->inum)
0069 p = &(*p)->rb_left;
0070 else if (inum > o->inum)
0071 p = &(*p)->rb_right;
0072 else {
0073 ubifs_err(c, "orphaned twice");
0074 spin_unlock(&c->orphan_lock);
0075 kfree(orphan);
0076 return ERR_PTR(-EINVAL);
0077 }
0078 }
0079 c->tot_orphans += 1;
0080 c->new_orphans += 1;
0081 rb_link_node(&orphan->rb, parent, p);
0082 rb_insert_color(&orphan->rb, &c->orph_tree);
0083 list_add_tail(&orphan->list, &c->orph_list);
0084 list_add_tail(&orphan->new_list, &c->orph_new);
0085
0086 if (parent_orphan) {
0087 list_add_tail(&orphan->child_list,
0088 &parent_orphan->child_list);
0089 }
0090
0091 spin_unlock(&c->orphan_lock);
0092 dbg_gen("ino %lu", (unsigned long)inum);
0093 return orphan;
0094 }
0095
0096 static struct ubifs_orphan *lookup_orphan(struct ubifs_info *c, ino_t inum)
0097 {
0098 struct ubifs_orphan *o;
0099 struct rb_node *p;
0100
0101 p = c->orph_tree.rb_node;
0102 while (p) {
0103 o = rb_entry(p, struct ubifs_orphan, rb);
0104 if (inum < o->inum)
0105 p = p->rb_left;
0106 else if (inum > o->inum)
0107 p = p->rb_right;
0108 else {
0109 return o;
0110 }
0111 }
0112 return NULL;
0113 }
0114
0115 static void __orphan_drop(struct ubifs_info *c, struct ubifs_orphan *o)
0116 {
0117 rb_erase(&o->rb, &c->orph_tree);
0118 list_del(&o->list);
0119 c->tot_orphans -= 1;
0120
0121 if (o->new) {
0122 list_del(&o->new_list);
0123 c->new_orphans -= 1;
0124 }
0125
0126 kfree(o);
0127 }
0128
0129 static void orphan_delete(struct ubifs_info *c, struct ubifs_orphan *orph)
0130 {
0131 if (orph->del) {
0132 dbg_gen("deleted twice ino %lu", (unsigned long)orph->inum);
0133 return;
0134 }
0135
0136 if (orph->cmt) {
0137 orph->del = 1;
0138 orph->dnext = c->orph_dnext;
0139 c->orph_dnext = orph;
0140 dbg_gen("delete later ino %lu", (unsigned long)orph->inum);
0141 return;
0142 }
0143
0144 __orphan_drop(c, orph);
0145 }
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155 int ubifs_add_orphan(struct ubifs_info *c, ino_t inum)
0156 {
0157 int err = 0;
0158 ino_t xattr_inum;
0159 union ubifs_key key;
0160 struct ubifs_dent_node *xent, *pxent = NULL;
0161 struct fscrypt_name nm = {0};
0162 struct ubifs_orphan *xattr_orphan;
0163 struct ubifs_orphan *orphan;
0164
0165 orphan = orphan_add(c, inum, NULL);
0166 if (IS_ERR(orphan))
0167 return PTR_ERR(orphan);
0168
0169 lowest_xent_key(c, &key, inum);
0170 while (1) {
0171 xent = ubifs_tnc_next_ent(c, &key, &nm);
0172 if (IS_ERR(xent)) {
0173 err = PTR_ERR(xent);
0174 if (err == -ENOENT)
0175 break;
0176 kfree(pxent);
0177 return err;
0178 }
0179
0180 fname_name(&nm) = xent->name;
0181 fname_len(&nm) = le16_to_cpu(xent->nlen);
0182 xattr_inum = le64_to_cpu(xent->inum);
0183
0184 xattr_orphan = orphan_add(c, xattr_inum, orphan);
0185 if (IS_ERR(xattr_orphan)) {
0186 kfree(pxent);
0187 kfree(xent);
0188 return PTR_ERR(xattr_orphan);
0189 }
0190
0191 kfree(pxent);
0192 pxent = xent;
0193 key_read(c, &xent->key, &key);
0194 }
0195 kfree(pxent);
0196
0197 return 0;
0198 }
0199
0200
0201
0202
0203
0204
0205
0206
0207 void ubifs_delete_orphan(struct ubifs_info *c, ino_t inum)
0208 {
0209 struct ubifs_orphan *orph, *child_orph, *tmp_o;
0210
0211 spin_lock(&c->orphan_lock);
0212
0213 orph = lookup_orphan(c, inum);
0214 if (!orph) {
0215 spin_unlock(&c->orphan_lock);
0216 ubifs_err(c, "missing orphan ino %lu", (unsigned long)inum);
0217 dump_stack();
0218
0219 return;
0220 }
0221
0222 list_for_each_entry_safe(child_orph, tmp_o, &orph->child_list, child_list) {
0223 list_del(&child_orph->child_list);
0224 orphan_delete(c, child_orph);
0225 }
0226
0227 orphan_delete(c, orph);
0228
0229 spin_unlock(&c->orphan_lock);
0230 }
0231
0232
0233
0234
0235
0236
0237
0238 int ubifs_orphan_start_commit(struct ubifs_info *c)
0239 {
0240 struct ubifs_orphan *orphan, **last;
0241
0242 spin_lock(&c->orphan_lock);
0243 last = &c->orph_cnext;
0244 list_for_each_entry(orphan, &c->orph_new, new_list) {
0245 ubifs_assert(c, orphan->new);
0246 ubifs_assert(c, !orphan->cmt);
0247 orphan->new = 0;
0248 orphan->cmt = 1;
0249 *last = orphan;
0250 last = &orphan->cnext;
0251 }
0252 *last = NULL;
0253 c->cmt_orphans = c->new_orphans;
0254 c->new_orphans = 0;
0255 dbg_cmt("%d orphans to commit", c->cmt_orphans);
0256 INIT_LIST_HEAD(&c->orph_new);
0257 if (c->tot_orphans == 0)
0258 c->no_orphs = 1;
0259 else
0260 c->no_orphs = 0;
0261 spin_unlock(&c->orphan_lock);
0262 return 0;
0263 }
0264
0265
0266
0267
0268
0269
0270
0271
0272 static int avail_orphs(struct ubifs_info *c)
0273 {
0274 int avail_lebs, avail, gap;
0275
0276 avail_lebs = c->orph_lebs - (c->ohead_lnum - c->orph_first) - 1;
0277 avail = avail_lebs *
0278 ((c->leb_size - UBIFS_ORPH_NODE_SZ) / sizeof(__le64));
0279 gap = c->leb_size - c->ohead_offs;
0280 if (gap >= UBIFS_ORPH_NODE_SZ + sizeof(__le64))
0281 avail += (gap - UBIFS_ORPH_NODE_SZ) / sizeof(__le64);
0282 return avail;
0283 }
0284
0285
0286
0287
0288
0289
0290
0291
0292 static int tot_avail_orphs(struct ubifs_info *c)
0293 {
0294 int avail_lebs, avail;
0295
0296 avail_lebs = c->orph_lebs;
0297 avail = avail_lebs *
0298 ((c->leb_size - UBIFS_ORPH_NODE_SZ) / sizeof(__le64));
0299 return avail / 2;
0300 }
0301
0302
0303
0304
0305
0306
0307
0308
0309
0310
0311
0312 static int do_write_orph_node(struct ubifs_info *c, int len, int atomic)
0313 {
0314 int err = 0;
0315
0316 if (atomic) {
0317 ubifs_assert(c, c->ohead_offs == 0);
0318 ubifs_prepare_node(c, c->orph_buf, len, 1);
0319 len = ALIGN(len, c->min_io_size);
0320 err = ubifs_leb_change(c, c->ohead_lnum, c->orph_buf, len);
0321 } else {
0322 if (c->ohead_offs == 0) {
0323
0324 err = ubifs_leb_unmap(c, c->ohead_lnum);
0325 if (err)
0326 return err;
0327 }
0328 err = ubifs_write_node(c, c->orph_buf, len, c->ohead_lnum,
0329 c->ohead_offs);
0330 }
0331 return err;
0332 }
0333
0334
0335
0336
0337
0338
0339
0340
0341
0342
0343 static int write_orph_node(struct ubifs_info *c, int atomic)
0344 {
0345 struct ubifs_orphan *orphan, *cnext;
0346 struct ubifs_orph_node *orph;
0347 int gap, err, len, cnt, i;
0348
0349 ubifs_assert(c, c->cmt_orphans > 0);
0350 gap = c->leb_size - c->ohead_offs;
0351 if (gap < UBIFS_ORPH_NODE_SZ + sizeof(__le64)) {
0352 c->ohead_lnum += 1;
0353 c->ohead_offs = 0;
0354 gap = c->leb_size;
0355 if (c->ohead_lnum > c->orph_last) {
0356
0357
0358
0359
0360 ubifs_err(c, "out of space in orphan area");
0361 return -EINVAL;
0362 }
0363 }
0364 cnt = (gap - UBIFS_ORPH_NODE_SZ) / sizeof(__le64);
0365 if (cnt > c->cmt_orphans)
0366 cnt = c->cmt_orphans;
0367 len = UBIFS_ORPH_NODE_SZ + cnt * sizeof(__le64);
0368 ubifs_assert(c, c->orph_buf);
0369 orph = c->orph_buf;
0370 orph->ch.node_type = UBIFS_ORPH_NODE;
0371 spin_lock(&c->orphan_lock);
0372 cnext = c->orph_cnext;
0373 for (i = 0; i < cnt; i++) {
0374 orphan = cnext;
0375 ubifs_assert(c, orphan->cmt);
0376 orph->inos[i] = cpu_to_le64(orphan->inum);
0377 orphan->cmt = 0;
0378 cnext = orphan->cnext;
0379 orphan->cnext = NULL;
0380 }
0381 c->orph_cnext = cnext;
0382 c->cmt_orphans -= cnt;
0383 spin_unlock(&c->orphan_lock);
0384 if (c->cmt_orphans)
0385 orph->cmt_no = cpu_to_le64(c->cmt_no);
0386 else
0387
0388 orph->cmt_no = cpu_to_le64((c->cmt_no) | (1ULL << 63));
0389 ubifs_assert(c, c->ohead_offs + len <= c->leb_size);
0390 ubifs_assert(c, c->ohead_lnum >= c->orph_first);
0391 ubifs_assert(c, c->ohead_lnum <= c->orph_last);
0392 err = do_write_orph_node(c, len, atomic);
0393 c->ohead_offs += ALIGN(len, c->min_io_size);
0394 c->ohead_offs = ALIGN(c->ohead_offs, 8);
0395 return err;
0396 }
0397
0398
0399
0400
0401
0402
0403
0404
0405
0406 static int write_orph_nodes(struct ubifs_info *c, int atomic)
0407 {
0408 int err;
0409
0410 while (c->cmt_orphans > 0) {
0411 err = write_orph_node(c, atomic);
0412 if (err)
0413 return err;
0414 }
0415 if (atomic) {
0416 int lnum;
0417
0418
0419 for (lnum = c->ohead_lnum + 1; lnum <= c->orph_last; lnum++) {
0420 err = ubifs_leb_unmap(c, lnum);
0421 if (err)
0422 return err;
0423 }
0424 }
0425 return 0;
0426 }
0427
0428
0429
0430
0431
0432
0433
0434
0435
0436
0437
0438
0439 static int consolidate(struct ubifs_info *c)
0440 {
0441 int tot_avail = tot_avail_orphs(c), err = 0;
0442
0443 spin_lock(&c->orphan_lock);
0444 dbg_cmt("there is space for %d orphans and there are %d",
0445 tot_avail, c->tot_orphans);
0446 if (c->tot_orphans - c->new_orphans <= tot_avail) {
0447 struct ubifs_orphan *orphan, **last;
0448 int cnt = 0;
0449
0450
0451 last = &c->orph_cnext;
0452 list_for_each_entry(orphan, &c->orph_list, list) {
0453 if (orphan->new)
0454 continue;
0455 orphan->cmt = 1;
0456 *last = orphan;
0457 last = &orphan->cnext;
0458 cnt += 1;
0459 }
0460 *last = NULL;
0461 ubifs_assert(c, cnt == c->tot_orphans - c->new_orphans);
0462 c->cmt_orphans = cnt;
0463 c->ohead_lnum = c->orph_first;
0464 c->ohead_offs = 0;
0465 } else {
0466
0467
0468
0469
0470 ubifs_err(c, "out of space in orphan area");
0471 err = -EINVAL;
0472 }
0473 spin_unlock(&c->orphan_lock);
0474 return err;
0475 }
0476
0477
0478
0479
0480
0481
0482
0483
0484 static int commit_orphans(struct ubifs_info *c)
0485 {
0486 int avail, atomic = 0, err;
0487
0488 ubifs_assert(c, c->cmt_orphans > 0);
0489 avail = avail_orphs(c);
0490 if (avail < c->cmt_orphans) {
0491
0492 err = consolidate(c);
0493 if (err)
0494 return err;
0495 atomic = 1;
0496 }
0497 err = write_orph_nodes(c, atomic);
0498 return err;
0499 }
0500
0501
0502
0503
0504
0505
0506
0507
0508
0509
0510 static void erase_deleted(struct ubifs_info *c)
0511 {
0512 struct ubifs_orphan *orphan, *dnext;
0513
0514 spin_lock(&c->orphan_lock);
0515 dnext = c->orph_dnext;
0516 while (dnext) {
0517 orphan = dnext;
0518 dnext = orphan->dnext;
0519 ubifs_assert(c, !orphan->new);
0520 ubifs_assert(c, orphan->del);
0521 rb_erase(&orphan->rb, &c->orph_tree);
0522 list_del(&orphan->list);
0523 c->tot_orphans -= 1;
0524 dbg_gen("deleting orphan ino %lu", (unsigned long)orphan->inum);
0525 kfree(orphan);
0526 }
0527 c->orph_dnext = NULL;
0528 spin_unlock(&c->orphan_lock);
0529 }
0530
0531
0532
0533
0534
0535
0536
0537 int ubifs_orphan_end_commit(struct ubifs_info *c)
0538 {
0539 int err;
0540
0541 if (c->cmt_orphans != 0) {
0542 err = commit_orphans(c);
0543 if (err)
0544 return err;
0545 }
0546 erase_deleted(c);
0547 err = dbg_check_orphans(c);
0548 return err;
0549 }
0550
0551
0552
0553
0554
0555
0556
0557
0558
0559 int ubifs_clear_orphans(struct ubifs_info *c)
0560 {
0561 int lnum, err;
0562
0563 for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
0564 err = ubifs_leb_unmap(c, lnum);
0565 if (err)
0566 return err;
0567 }
0568 c->ohead_lnum = c->orph_first;
0569 c->ohead_offs = 0;
0570 return 0;
0571 }
0572
0573
0574
0575
0576
0577
0578
0579
0580
0581
0582 static int insert_dead_orphan(struct ubifs_info *c, ino_t inum)
0583 {
0584 struct ubifs_orphan *orphan, *o;
0585 struct rb_node **p, *parent = NULL;
0586
0587 orphan = kzalloc(sizeof(struct ubifs_orphan), GFP_KERNEL);
0588 if (!orphan)
0589 return -ENOMEM;
0590 orphan->inum = inum;
0591
0592 p = &c->orph_tree.rb_node;
0593 while (*p) {
0594 parent = *p;
0595 o = rb_entry(parent, struct ubifs_orphan, rb);
0596 if (inum < o->inum)
0597 p = &(*p)->rb_left;
0598 else if (inum > o->inum)
0599 p = &(*p)->rb_right;
0600 else {
0601
0602 kfree(orphan);
0603 return 0;
0604 }
0605 }
0606 c->tot_orphans += 1;
0607 rb_link_node(&orphan->rb, parent, p);
0608 rb_insert_color(&orphan->rb, &c->orph_tree);
0609 list_add_tail(&orphan->list, &c->orph_list);
0610 orphan->del = 1;
0611 orphan->dnext = c->orph_dnext;
0612 c->orph_dnext = orphan;
0613 dbg_mnt("ino %lu, new %d, tot %d", (unsigned long)inum,
0614 c->new_orphans, c->tot_orphans);
0615 return 0;
0616 }
0617
0618
0619
0620
0621
0622
0623
0624
0625
0626
0627
0628
0629
0630 static int do_kill_orphans(struct ubifs_info *c, struct ubifs_scan_leb *sleb,
0631 unsigned long long *last_cmt_no, int *outofdate,
0632 int *last_flagged)
0633 {
0634 struct ubifs_scan_node *snod;
0635 struct ubifs_orph_node *orph;
0636 struct ubifs_ino_node *ino = NULL;
0637 unsigned long long cmt_no;
0638 ino_t inum;
0639 int i, n, err, first = 1;
0640
0641 ino = kmalloc(UBIFS_MAX_INO_NODE_SZ, GFP_NOFS);
0642 if (!ino)
0643 return -ENOMEM;
0644
0645 list_for_each_entry(snod, &sleb->nodes, list) {
0646 if (snod->type != UBIFS_ORPH_NODE) {
0647 ubifs_err(c, "invalid node type %d in orphan area at %d:%d",
0648 snod->type, sleb->lnum, snod->offs);
0649 ubifs_dump_node(c, snod->node,
0650 c->leb_size - snod->offs);
0651 err = -EINVAL;
0652 goto out_free;
0653 }
0654
0655 orph = snod->node;
0656
0657
0658 cmt_no = le64_to_cpu(orph->cmt_no) & LLONG_MAX;
0659
0660
0661
0662
0663
0664
0665
0666
0667 if (cmt_no > c->cmt_no)
0668 c->cmt_no = cmt_no;
0669 if (cmt_no < *last_cmt_no && *last_flagged) {
0670
0671
0672
0673
0674
0675 if (!first) {
0676 ubifs_err(c, "out of order commit number %llu in orphan node at %d:%d",
0677 cmt_no, sleb->lnum, snod->offs);
0678 ubifs_dump_node(c, snod->node,
0679 c->leb_size - snod->offs);
0680 err = -EINVAL;
0681 goto out_free;
0682 }
0683 dbg_rcvry("out of date LEB %d", sleb->lnum);
0684 *outofdate = 1;
0685 err = 0;
0686 goto out_free;
0687 }
0688
0689 if (first)
0690 first = 0;
0691
0692 n = (le32_to_cpu(orph->ch.len) - UBIFS_ORPH_NODE_SZ) >> 3;
0693 for (i = 0; i < n; i++) {
0694 union ubifs_key key1, key2;
0695
0696 inum = le64_to_cpu(orph->inos[i]);
0697
0698 ino_key_init(c, &key1, inum);
0699 err = ubifs_tnc_lookup(c, &key1, ino);
0700 if (err && err != -ENOENT)
0701 goto out_free;
0702
0703
0704
0705
0706
0707 if (err == 0 && ino->nlink == 0) {
0708 dbg_rcvry("deleting orphaned inode %lu",
0709 (unsigned long)inum);
0710
0711 lowest_ino_key(c, &key1, inum);
0712 highest_ino_key(c, &key2, inum);
0713
0714 err = ubifs_tnc_remove_range(c, &key1, &key2);
0715 if (err)
0716 goto out_ro;
0717 }
0718
0719 err = insert_dead_orphan(c, inum);
0720 if (err)
0721 goto out_free;
0722 }
0723
0724 *last_cmt_no = cmt_no;
0725 if (le64_to_cpu(orph->cmt_no) & (1ULL << 63)) {
0726 dbg_rcvry("last orph node for commit %llu at %d:%d",
0727 cmt_no, sleb->lnum, snod->offs);
0728 *last_flagged = 1;
0729 } else
0730 *last_flagged = 0;
0731 }
0732
0733 err = 0;
0734 out_free:
0735 kfree(ino);
0736 return err;
0737
0738 out_ro:
0739 ubifs_ro_mode(c, err);
0740 kfree(ino);
0741 return err;
0742 }
0743
0744
0745
0746
0747
0748
0749
0750
0751
0752
0753
0754 static int kill_orphans(struct ubifs_info *c)
0755 {
0756 unsigned long long last_cmt_no = 0;
0757 int lnum, err = 0, outofdate = 0, last_flagged = 0;
0758
0759 c->ohead_lnum = c->orph_first;
0760 c->ohead_offs = 0;
0761
0762 if (c->no_orphs) {
0763 dbg_rcvry("no orphans");
0764 return 0;
0765 }
0766
0767
0768
0769
0770
0771
0772
0773
0774
0775
0776
0777 for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
0778 struct ubifs_scan_leb *sleb;
0779
0780 dbg_rcvry("LEB %d", lnum);
0781 sleb = ubifs_scan(c, lnum, 0, c->sbuf, 1);
0782 if (IS_ERR(sleb)) {
0783 if (PTR_ERR(sleb) == -EUCLEAN)
0784 sleb = ubifs_recover_leb(c, lnum, 0,
0785 c->sbuf, -1);
0786 if (IS_ERR(sleb)) {
0787 err = PTR_ERR(sleb);
0788 break;
0789 }
0790 }
0791 err = do_kill_orphans(c, sleb, &last_cmt_no, &outofdate,
0792 &last_flagged);
0793 if (err || outofdate) {
0794 ubifs_scan_destroy(sleb);
0795 break;
0796 }
0797 if (sleb->endpt) {
0798 c->ohead_lnum = lnum;
0799 c->ohead_offs = sleb->endpt;
0800 }
0801 ubifs_scan_destroy(sleb);
0802 }
0803 return err;
0804 }
0805
0806
0807
0808
0809
0810
0811
0812
0813
0814
0815
0816 int ubifs_mount_orphans(struct ubifs_info *c, int unclean, int read_only)
0817 {
0818 int err = 0;
0819
0820 c->max_orphans = tot_avail_orphs(c);
0821
0822 if (!read_only) {
0823 c->orph_buf = vmalloc(c->leb_size);
0824 if (!c->orph_buf)
0825 return -ENOMEM;
0826 }
0827
0828 if (unclean)
0829 err = kill_orphans(c);
0830 else if (!read_only)
0831 err = ubifs_clear_orphans(c);
0832
0833 return err;
0834 }
0835
0836
0837
0838
0839
0840 struct check_orphan {
0841 struct rb_node rb;
0842 ino_t inum;
0843 };
0844
0845 struct check_info {
0846 unsigned long last_ino;
0847 unsigned long tot_inos;
0848 unsigned long missing;
0849 unsigned long long leaf_cnt;
0850 struct ubifs_ino_node *node;
0851 struct rb_root root;
0852 };
0853
0854 static bool dbg_find_orphan(struct ubifs_info *c, ino_t inum)
0855 {
0856 bool found = false;
0857
0858 spin_lock(&c->orphan_lock);
0859 found = !!lookup_orphan(c, inum);
0860 spin_unlock(&c->orphan_lock);
0861
0862 return found;
0863 }
0864
0865 static int dbg_ins_check_orphan(struct rb_root *root, ino_t inum)
0866 {
0867 struct check_orphan *orphan, *o;
0868 struct rb_node **p, *parent = NULL;
0869
0870 orphan = kzalloc(sizeof(struct check_orphan), GFP_NOFS);
0871 if (!orphan)
0872 return -ENOMEM;
0873 orphan->inum = inum;
0874
0875 p = &root->rb_node;
0876 while (*p) {
0877 parent = *p;
0878 o = rb_entry(parent, struct check_orphan, rb);
0879 if (inum < o->inum)
0880 p = &(*p)->rb_left;
0881 else if (inum > o->inum)
0882 p = &(*p)->rb_right;
0883 else {
0884 kfree(orphan);
0885 return 0;
0886 }
0887 }
0888 rb_link_node(&orphan->rb, parent, p);
0889 rb_insert_color(&orphan->rb, root);
0890 return 0;
0891 }
0892
0893 static int dbg_find_check_orphan(struct rb_root *root, ino_t inum)
0894 {
0895 struct check_orphan *o;
0896 struct rb_node *p;
0897
0898 p = root->rb_node;
0899 while (p) {
0900 o = rb_entry(p, struct check_orphan, rb);
0901 if (inum < o->inum)
0902 p = p->rb_left;
0903 else if (inum > o->inum)
0904 p = p->rb_right;
0905 else
0906 return 1;
0907 }
0908 return 0;
0909 }
0910
0911 static void dbg_free_check_tree(struct rb_root *root)
0912 {
0913 struct check_orphan *o, *n;
0914
0915 rbtree_postorder_for_each_entry_safe(o, n, root, rb)
0916 kfree(o);
0917 }
0918
0919 static int dbg_orphan_check(struct ubifs_info *c, struct ubifs_zbranch *zbr,
0920 void *priv)
0921 {
0922 struct check_info *ci = priv;
0923 ino_t inum;
0924 int err;
0925
0926 inum = key_inum(c, &zbr->key);
0927 if (inum != ci->last_ino) {
0928
0929 if (key_type(c, &zbr->key) != UBIFS_INO_KEY)
0930 ubifs_err(c, "found orphan node ino %lu, type %d",
0931 (unsigned long)inum, key_type(c, &zbr->key));
0932 ci->last_ino = inum;
0933 ci->tot_inos += 1;
0934 err = ubifs_tnc_read_node(c, zbr, ci->node);
0935 if (err) {
0936 ubifs_err(c, "node read failed, error %d", err);
0937 return err;
0938 }
0939 if (ci->node->nlink == 0)
0940
0941 if (!dbg_find_check_orphan(&ci->root, inum) &&
0942 !dbg_find_orphan(c, inum)) {
0943 ubifs_err(c, "missing orphan, ino %lu",
0944 (unsigned long)inum);
0945 ci->missing += 1;
0946 }
0947 }
0948 ci->leaf_cnt += 1;
0949 return 0;
0950 }
0951
0952 static int dbg_read_orphans(struct check_info *ci, struct ubifs_scan_leb *sleb)
0953 {
0954 struct ubifs_scan_node *snod;
0955 struct ubifs_orph_node *orph;
0956 ino_t inum;
0957 int i, n, err;
0958
0959 list_for_each_entry(snod, &sleb->nodes, list) {
0960 cond_resched();
0961 if (snod->type != UBIFS_ORPH_NODE)
0962 continue;
0963 orph = snod->node;
0964 n = (le32_to_cpu(orph->ch.len) - UBIFS_ORPH_NODE_SZ) >> 3;
0965 for (i = 0; i < n; i++) {
0966 inum = le64_to_cpu(orph->inos[i]);
0967 err = dbg_ins_check_orphan(&ci->root, inum);
0968 if (err)
0969 return err;
0970 }
0971 }
0972 return 0;
0973 }
0974
0975 static int dbg_scan_orphans(struct ubifs_info *c, struct check_info *ci)
0976 {
0977 int lnum, err = 0;
0978 void *buf;
0979
0980
0981 if (c->no_orphs)
0982 return 0;
0983
0984 buf = __vmalloc(c->leb_size, GFP_NOFS);
0985 if (!buf) {
0986 ubifs_err(c, "cannot allocate memory to check orphans");
0987 return 0;
0988 }
0989
0990 for (lnum = c->orph_first; lnum <= c->orph_last; lnum++) {
0991 struct ubifs_scan_leb *sleb;
0992
0993 sleb = ubifs_scan(c, lnum, 0, buf, 0);
0994 if (IS_ERR(sleb)) {
0995 err = PTR_ERR(sleb);
0996 break;
0997 }
0998
0999 err = dbg_read_orphans(ci, sleb);
1000 ubifs_scan_destroy(sleb);
1001 if (err)
1002 break;
1003 }
1004
1005 vfree(buf);
1006 return err;
1007 }
1008
1009 static int dbg_check_orphans(struct ubifs_info *c)
1010 {
1011 struct check_info ci;
1012 int err;
1013
1014 if (!dbg_is_chk_orph(c))
1015 return 0;
1016
1017 ci.last_ino = 0;
1018 ci.tot_inos = 0;
1019 ci.missing = 0;
1020 ci.leaf_cnt = 0;
1021 ci.root = RB_ROOT;
1022 ci.node = kmalloc(UBIFS_MAX_INO_NODE_SZ, GFP_NOFS);
1023 if (!ci.node) {
1024 ubifs_err(c, "out of memory");
1025 return -ENOMEM;
1026 }
1027
1028 err = dbg_scan_orphans(c, &ci);
1029 if (err)
1030 goto out;
1031
1032 err = dbg_walk_index(c, &dbg_orphan_check, NULL, &ci);
1033 if (err) {
1034 ubifs_err(c, "cannot scan TNC, error %d", err);
1035 goto out;
1036 }
1037
1038 if (ci.missing) {
1039 ubifs_err(c, "%lu missing orphan(s)", ci.missing);
1040 err = -EINVAL;
1041 goto out;
1042 }
1043
1044 dbg_cmt("last inode number is %lu", ci.last_ino);
1045 dbg_cmt("total number of inodes is %lu", ci.tot_inos);
1046 dbg_cmt("total number of leaf nodes is %llu", ci.leaf_cnt);
1047
1048 out:
1049 dbg_free_check_tree(&ci.root);
1050 kfree(ci.node);
1051 return err;
1052 }