ref-verify.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2014 Facebook. All rights reserved.
  4. */
  5. #include <linux/sched.h>
  6. #include <linux/stacktrace.h>
  7. #include "ctree.h"
  8. #include "disk-io.h"
  9. #include "locking.h"
  10. #include "delayed-ref.h"
  11. #include "ref-verify.h"
  12. /*
  13. * Used to keep track the roots and number of refs each root has for a given
  14. * bytenr. This just tracks the number of direct references, no shared
  15. * references.
  16. */
  17. struct root_entry {
  18. u64 root_objectid;
  19. u64 num_refs;
  20. struct rb_node node;
  21. };
  22. /*
  23. * These are meant to represent what should exist in the extent tree, these can
  24. * be used to verify the extent tree is consistent as these should all match
  25. * what the extent tree says.
  26. */
  27. struct ref_entry {
  28. u64 root_objectid;
  29. u64 parent;
  30. u64 owner;
  31. u64 offset;
  32. u64 num_refs;
  33. struct rb_node node;
  34. };
  35. #define MAX_TRACE 16
  36. /*
  37. * Whenever we add/remove a reference we record the action. The action maps
  38. * back to the delayed ref action. We hold the ref we are changing in the
  39. * action so we can account for the history properly, and we record the root we
  40. * were called with since it could be different from ref_root. We also store
  41. * stack traces because thats how I roll.
  42. */
  43. struct ref_action {
  44. int action;
  45. u64 root;
  46. struct ref_entry ref;
  47. struct list_head list;
  48. unsigned long trace[MAX_TRACE];
  49. unsigned int trace_len;
  50. };
  51. /*
  52. * One of these for every block we reference, it holds the roots and references
  53. * to it as well as all of the ref actions that have occured to it. We never
  54. * free it until we unmount the file system in order to make sure re-allocations
  55. * are happening properly.
  56. */
  57. struct block_entry {
  58. u64 bytenr;
  59. u64 len;
  60. u64 num_refs;
  61. int metadata;
  62. int from_disk;
  63. struct rb_root roots;
  64. struct rb_root refs;
  65. struct rb_node node;
  66. struct list_head actions;
  67. };
  68. static struct block_entry *insert_block_entry(struct rb_root *root,
  69. struct block_entry *be)
  70. {
  71. struct rb_node **p = &root->rb_node;
  72. struct rb_node *parent_node = NULL;
  73. struct block_entry *entry;
  74. while (*p) {
  75. parent_node = *p;
  76. entry = rb_entry(parent_node, struct block_entry, node);
  77. if (entry->bytenr > be->bytenr)
  78. p = &(*p)->rb_left;
  79. else if (entry->bytenr < be->bytenr)
  80. p = &(*p)->rb_right;
  81. else
  82. return entry;
  83. }
  84. rb_link_node(&be->node, parent_node, p);
  85. rb_insert_color(&be->node, root);
  86. return NULL;
  87. }
  88. static struct block_entry *lookup_block_entry(struct rb_root *root, u64 bytenr)
  89. {
  90. struct rb_node *n;
  91. struct block_entry *entry = NULL;
  92. n = root->rb_node;
  93. while (n) {
  94. entry = rb_entry(n, struct block_entry, node);
  95. if (entry->bytenr < bytenr)
  96. n = n->rb_right;
  97. else if (entry->bytenr > bytenr)
  98. n = n->rb_left;
  99. else
  100. return entry;
  101. }
  102. return NULL;
  103. }
  104. static struct root_entry *insert_root_entry(struct rb_root *root,
  105. struct root_entry *re)
  106. {
  107. struct rb_node **p = &root->rb_node;
  108. struct rb_node *parent_node = NULL;
  109. struct root_entry *entry;
  110. while (*p) {
  111. parent_node = *p;
  112. entry = rb_entry(parent_node, struct root_entry, node);
  113. if (entry->root_objectid > re->root_objectid)
  114. p = &(*p)->rb_left;
  115. else if (entry->root_objectid < re->root_objectid)
  116. p = &(*p)->rb_right;
  117. else
  118. return entry;
  119. }
  120. rb_link_node(&re->node, parent_node, p);
  121. rb_insert_color(&re->node, root);
  122. return NULL;
  123. }
  124. static int comp_refs(struct ref_entry *ref1, struct ref_entry *ref2)
  125. {
  126. if (ref1->root_objectid < ref2->root_objectid)
  127. return -1;
  128. if (ref1->root_objectid > ref2->root_objectid)
  129. return 1;
  130. if (ref1->parent < ref2->parent)
  131. return -1;
  132. if (ref1->parent > ref2->parent)
  133. return 1;
  134. if (ref1->owner < ref2->owner)
  135. return -1;
  136. if (ref1->owner > ref2->owner)
  137. return 1;
  138. if (ref1->offset < ref2->offset)
  139. return -1;
  140. if (ref1->offset > ref2->offset)
  141. return 1;
  142. return 0;
  143. }
  144. static struct ref_entry *insert_ref_entry(struct rb_root *root,
  145. struct ref_entry *ref)
  146. {
  147. struct rb_node **p = &root->rb_node;
  148. struct rb_node *parent_node = NULL;
  149. struct ref_entry *entry;
  150. int cmp;
  151. while (*p) {
  152. parent_node = *p;
  153. entry = rb_entry(parent_node, struct ref_entry, node);
  154. cmp = comp_refs(entry, ref);
  155. if (cmp > 0)
  156. p = &(*p)->rb_left;
  157. else if (cmp < 0)
  158. p = &(*p)->rb_right;
  159. else
  160. return entry;
  161. }
  162. rb_link_node(&ref->node, parent_node, p);
  163. rb_insert_color(&ref->node, root);
  164. return NULL;
  165. }
  166. static struct root_entry *lookup_root_entry(struct rb_root *root, u64 objectid)
  167. {
  168. struct rb_node *n;
  169. struct root_entry *entry = NULL;
  170. n = root->rb_node;
  171. while (n) {
  172. entry = rb_entry(n, struct root_entry, node);
  173. if (entry->root_objectid < objectid)
  174. n = n->rb_right;
  175. else if (entry->root_objectid > objectid)
  176. n = n->rb_left;
  177. else
  178. return entry;
  179. }
  180. return NULL;
  181. }
  182. #ifdef CONFIG_STACKTRACE
  183. static void __save_stack_trace(struct ref_action *ra)
  184. {
  185. struct stack_trace stack_trace;
  186. stack_trace.max_entries = MAX_TRACE;
  187. stack_trace.nr_entries = 0;
  188. stack_trace.entries = ra->trace;
  189. stack_trace.skip = 2;
  190. save_stack_trace(&stack_trace);
  191. ra->trace_len = stack_trace.nr_entries;
  192. }
  193. static void __print_stack_trace(struct btrfs_fs_info *fs_info,
  194. struct ref_action *ra)
  195. {
  196. struct stack_trace trace;
  197. if (ra->trace_len == 0) {
  198. btrfs_err(fs_info, " ref-verify: no stacktrace");
  199. return;
  200. }
  201. trace.nr_entries = ra->trace_len;
  202. trace.entries = ra->trace;
  203. print_stack_trace(&trace, 2);
  204. }
  205. #else
  206. static void inline __save_stack_trace(struct ref_action *ra)
  207. {
  208. }
  209. static void inline __print_stack_trace(struct btrfs_fs_info *fs_info,
  210. struct ref_action *ra)
  211. {
  212. btrfs_err(fs_info, " ref-verify: no stacktrace support");
  213. }
  214. #endif
  215. static void free_block_entry(struct block_entry *be)
  216. {
  217. struct root_entry *re;
  218. struct ref_entry *ref;
  219. struct ref_action *ra;
  220. struct rb_node *n;
  221. while ((n = rb_first(&be->roots))) {
  222. re = rb_entry(n, struct root_entry, node);
  223. rb_erase(&re->node, &be->roots);
  224. kfree(re);
  225. }
  226. while((n = rb_first(&be->refs))) {
  227. ref = rb_entry(n, struct ref_entry, node);
  228. rb_erase(&ref->node, &be->refs);
  229. kfree(ref);
  230. }
  231. while (!list_empty(&be->actions)) {
  232. ra = list_first_entry(&be->actions, struct ref_action,
  233. list);
  234. list_del(&ra->list);
  235. kfree(ra);
  236. }
  237. kfree(be);
  238. }
  239. static struct block_entry *add_block_entry(struct btrfs_fs_info *fs_info,
  240. u64 bytenr, u64 len,
  241. u64 root_objectid)
  242. {
  243. struct block_entry *be = NULL, *exist;
  244. struct root_entry *re = NULL;
  245. re = kzalloc(sizeof(struct root_entry), GFP_KERNEL);
  246. be = kzalloc(sizeof(struct block_entry), GFP_KERNEL);
  247. if (!be || !re) {
  248. kfree(re);
  249. kfree(be);
  250. return ERR_PTR(-ENOMEM);
  251. }
  252. be->bytenr = bytenr;
  253. be->len = len;
  254. re->root_objectid = root_objectid;
  255. re->num_refs = 0;
  256. spin_lock(&fs_info->ref_verify_lock);
  257. exist = insert_block_entry(&fs_info->block_tree, be);
  258. if (exist) {
  259. if (root_objectid) {
  260. struct root_entry *exist_re;
  261. exist_re = insert_root_entry(&exist->roots, re);
  262. if (exist_re)
  263. kfree(re);
  264. }
  265. kfree(be);
  266. return exist;
  267. }
  268. be->num_refs = 0;
  269. be->metadata = 0;
  270. be->from_disk = 0;
  271. be->roots = RB_ROOT;
  272. be->refs = RB_ROOT;
  273. INIT_LIST_HEAD(&be->actions);
  274. if (root_objectid)
  275. insert_root_entry(&be->roots, re);
  276. else
  277. kfree(re);
  278. return be;
  279. }
  280. static int add_tree_block(struct btrfs_fs_info *fs_info, u64 ref_root,
  281. u64 parent, u64 bytenr, int level)
  282. {
  283. struct block_entry *be;
  284. struct root_entry *re;
  285. struct ref_entry *ref = NULL, *exist;
  286. ref = kmalloc(sizeof(struct ref_entry), GFP_KERNEL);
  287. if (!ref)
  288. return -ENOMEM;
  289. if (parent)
  290. ref->root_objectid = 0;
  291. else
  292. ref->root_objectid = ref_root;
  293. ref->parent = parent;
  294. ref->owner = level;
  295. ref->offset = 0;
  296. ref->num_refs = 1;
  297. be = add_block_entry(fs_info, bytenr, fs_info->nodesize, ref_root);
  298. if (IS_ERR(be)) {
  299. kfree(ref);
  300. return PTR_ERR(be);
  301. }
  302. be->num_refs++;
  303. be->from_disk = 1;
  304. be->metadata = 1;
  305. if (!parent) {
  306. ASSERT(ref_root);
  307. re = lookup_root_entry(&be->roots, ref_root);
  308. ASSERT(re);
  309. re->num_refs++;
  310. }
  311. exist = insert_ref_entry(&be->refs, ref);
  312. if (exist) {
  313. exist->num_refs++;
  314. kfree(ref);
  315. }
  316. spin_unlock(&fs_info->ref_verify_lock);
  317. return 0;
  318. }
  319. static int add_shared_data_ref(struct btrfs_fs_info *fs_info,
  320. u64 parent, u32 num_refs, u64 bytenr,
  321. u64 num_bytes)
  322. {
  323. struct block_entry *be;
  324. struct ref_entry *ref;
  325. ref = kzalloc(sizeof(struct ref_entry), GFP_KERNEL);
  326. if (!ref)
  327. return -ENOMEM;
  328. be = add_block_entry(fs_info, bytenr, num_bytes, 0);
  329. if (IS_ERR(be)) {
  330. kfree(ref);
  331. return PTR_ERR(be);
  332. }
  333. be->num_refs += num_refs;
  334. ref->parent = parent;
  335. ref->num_refs = num_refs;
  336. if (insert_ref_entry(&be->refs, ref)) {
  337. spin_unlock(&fs_info->ref_verify_lock);
  338. btrfs_err(fs_info, "existing shared ref when reading from disk?");
  339. kfree(ref);
  340. return -EINVAL;
  341. }
  342. spin_unlock(&fs_info->ref_verify_lock);
  343. return 0;
  344. }
  345. static int add_extent_data_ref(struct btrfs_fs_info *fs_info,
  346. struct extent_buffer *leaf,
  347. struct btrfs_extent_data_ref *dref,
  348. u64 bytenr, u64 num_bytes)
  349. {
  350. struct block_entry *be;
  351. struct ref_entry *ref;
  352. struct root_entry *re;
  353. u64 ref_root = btrfs_extent_data_ref_root(leaf, dref);
  354. u64 owner = btrfs_extent_data_ref_objectid(leaf, dref);
  355. u64 offset = btrfs_extent_data_ref_offset(leaf, dref);
  356. u32 num_refs = btrfs_extent_data_ref_count(leaf, dref);
  357. ref = kzalloc(sizeof(struct ref_entry), GFP_KERNEL);
  358. if (!ref)
  359. return -ENOMEM;
  360. be = add_block_entry(fs_info, bytenr, num_bytes, ref_root);
  361. if (IS_ERR(be)) {
  362. kfree(ref);
  363. return PTR_ERR(be);
  364. }
  365. be->num_refs += num_refs;
  366. ref->parent = 0;
  367. ref->owner = owner;
  368. ref->root_objectid = ref_root;
  369. ref->offset = offset;
  370. ref->num_refs = num_refs;
  371. if (insert_ref_entry(&be->refs, ref)) {
  372. spin_unlock(&fs_info->ref_verify_lock);
  373. btrfs_err(fs_info, "existing ref when reading from disk?");
  374. kfree(ref);
  375. return -EINVAL;
  376. }
  377. re = lookup_root_entry(&be->roots, ref_root);
  378. if (!re) {
  379. spin_unlock(&fs_info->ref_verify_lock);
  380. btrfs_err(fs_info, "missing root in new block entry?");
  381. return -EINVAL;
  382. }
  383. re->num_refs += num_refs;
  384. spin_unlock(&fs_info->ref_verify_lock);
  385. return 0;
  386. }
  387. static int process_extent_item(struct btrfs_fs_info *fs_info,
  388. struct btrfs_path *path, struct btrfs_key *key,
  389. int slot, int *tree_block_level)
  390. {
  391. struct btrfs_extent_item *ei;
  392. struct btrfs_extent_inline_ref *iref;
  393. struct btrfs_extent_data_ref *dref;
  394. struct btrfs_shared_data_ref *sref;
  395. struct extent_buffer *leaf = path->nodes[0];
  396. u32 item_size = btrfs_item_size_nr(leaf, slot);
  397. unsigned long end, ptr;
  398. u64 offset, flags, count;
  399. int type, ret;
  400. ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
  401. flags = btrfs_extent_flags(leaf, ei);
  402. if ((key->type == BTRFS_EXTENT_ITEM_KEY) &&
  403. flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
  404. struct btrfs_tree_block_info *info;
  405. info = (struct btrfs_tree_block_info *)(ei + 1);
  406. *tree_block_level = btrfs_tree_block_level(leaf, info);
  407. iref = (struct btrfs_extent_inline_ref *)(info + 1);
  408. } else {
  409. if (key->type == BTRFS_METADATA_ITEM_KEY)
  410. *tree_block_level = key->offset;
  411. iref = (struct btrfs_extent_inline_ref *)(ei + 1);
  412. }
  413. ptr = (unsigned long)iref;
  414. end = (unsigned long)ei + item_size;
  415. while (ptr < end) {
  416. iref = (struct btrfs_extent_inline_ref *)ptr;
  417. type = btrfs_extent_inline_ref_type(leaf, iref);
  418. offset = btrfs_extent_inline_ref_offset(leaf, iref);
  419. switch (type) {
  420. case BTRFS_TREE_BLOCK_REF_KEY:
  421. ret = add_tree_block(fs_info, offset, 0, key->objectid,
  422. *tree_block_level);
  423. break;
  424. case BTRFS_SHARED_BLOCK_REF_KEY:
  425. ret = add_tree_block(fs_info, 0, offset, key->objectid,
  426. *tree_block_level);
  427. break;
  428. case BTRFS_EXTENT_DATA_REF_KEY:
  429. dref = (struct btrfs_extent_data_ref *)(&iref->offset);
  430. ret = add_extent_data_ref(fs_info, leaf, dref,
  431. key->objectid, key->offset);
  432. break;
  433. case BTRFS_SHARED_DATA_REF_KEY:
  434. sref = (struct btrfs_shared_data_ref *)(iref + 1);
  435. count = btrfs_shared_data_ref_count(leaf, sref);
  436. ret = add_shared_data_ref(fs_info, offset, count,
  437. key->objectid, key->offset);
  438. break;
  439. default:
  440. btrfs_err(fs_info, "invalid key type in iref");
  441. ret = -EINVAL;
  442. break;
  443. }
  444. if (ret)
  445. break;
  446. ptr += btrfs_extent_inline_ref_size(type);
  447. }
  448. return ret;
  449. }
  450. static int process_leaf(struct btrfs_root *root,
  451. struct btrfs_path *path, u64 *bytenr, u64 *num_bytes)
  452. {
  453. struct btrfs_fs_info *fs_info = root->fs_info;
  454. struct extent_buffer *leaf = path->nodes[0];
  455. struct btrfs_extent_data_ref *dref;
  456. struct btrfs_shared_data_ref *sref;
  457. u32 count;
  458. int i = 0, tree_block_level = 0, ret = 0;
  459. struct btrfs_key key;
  460. int nritems = btrfs_header_nritems(leaf);
  461. for (i = 0; i < nritems; i++) {
  462. btrfs_item_key_to_cpu(leaf, &key, i);
  463. switch (key.type) {
  464. case BTRFS_EXTENT_ITEM_KEY:
  465. *num_bytes = key.offset;
  466. case BTRFS_METADATA_ITEM_KEY:
  467. *bytenr = key.objectid;
  468. ret = process_extent_item(fs_info, path, &key, i,
  469. &tree_block_level);
  470. break;
  471. case BTRFS_TREE_BLOCK_REF_KEY:
  472. ret = add_tree_block(fs_info, key.offset, 0,
  473. key.objectid, tree_block_level);
  474. break;
  475. case BTRFS_SHARED_BLOCK_REF_KEY:
  476. ret = add_tree_block(fs_info, 0, key.offset,
  477. key.objectid, tree_block_level);
  478. break;
  479. case BTRFS_EXTENT_DATA_REF_KEY:
  480. dref = btrfs_item_ptr(leaf, i,
  481. struct btrfs_extent_data_ref);
  482. ret = add_extent_data_ref(fs_info, leaf, dref, *bytenr,
  483. *num_bytes);
  484. break;
  485. case BTRFS_SHARED_DATA_REF_KEY:
  486. sref = btrfs_item_ptr(leaf, i,
  487. struct btrfs_shared_data_ref);
  488. count = btrfs_shared_data_ref_count(leaf, sref);
  489. ret = add_shared_data_ref(fs_info, key.offset, count,
  490. *bytenr, *num_bytes);
  491. break;
  492. default:
  493. break;
  494. }
  495. if (ret)
  496. break;
  497. }
  498. return ret;
  499. }
  500. /* Walk down to the leaf from the given level */
  501. static int walk_down_tree(struct btrfs_root *root, struct btrfs_path *path,
  502. int level, u64 *bytenr, u64 *num_bytes)
  503. {
  504. struct btrfs_fs_info *fs_info = root->fs_info;
  505. struct extent_buffer *eb;
  506. u64 block_bytenr, gen;
  507. int ret = 0;
  508. while (level >= 0) {
  509. if (level) {
  510. struct btrfs_key first_key;
  511. block_bytenr = btrfs_node_blockptr(path->nodes[level],
  512. path->slots[level]);
  513. gen = btrfs_node_ptr_generation(path->nodes[level],
  514. path->slots[level]);
  515. btrfs_node_key_to_cpu(path->nodes[level], &first_key,
  516. path->slots[level]);
  517. eb = read_tree_block(fs_info, block_bytenr, gen,
  518. level - 1, &first_key);
  519. if (IS_ERR(eb))
  520. return PTR_ERR(eb);
  521. if (!extent_buffer_uptodate(eb)) {
  522. free_extent_buffer(eb);
  523. return -EIO;
  524. }
  525. btrfs_tree_read_lock(eb);
  526. btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
  527. path->nodes[level-1] = eb;
  528. path->slots[level-1] = 0;
  529. path->locks[level-1] = BTRFS_READ_LOCK_BLOCKING;
  530. } else {
  531. ret = process_leaf(root, path, bytenr, num_bytes);
  532. if (ret)
  533. break;
  534. }
  535. level--;
  536. }
  537. return ret;
  538. }
  539. /* Walk up to the next node that needs to be processed */
  540. static int walk_up_tree(struct btrfs_path *path, int *level)
  541. {
  542. int l;
  543. for (l = 0; l < BTRFS_MAX_LEVEL; l++) {
  544. if (!path->nodes[l])
  545. continue;
  546. if (l) {
  547. path->slots[l]++;
  548. if (path->slots[l] <
  549. btrfs_header_nritems(path->nodes[l])) {
  550. *level = l;
  551. return 0;
  552. }
  553. }
  554. btrfs_tree_unlock_rw(path->nodes[l], path->locks[l]);
  555. free_extent_buffer(path->nodes[l]);
  556. path->nodes[l] = NULL;
  557. path->slots[l] = 0;
  558. path->locks[l] = 0;
  559. }
  560. return 1;
  561. }
  562. static void dump_ref_action(struct btrfs_fs_info *fs_info,
  563. struct ref_action *ra)
  564. {
  565. btrfs_err(fs_info,
  566. " Ref action %d, root %llu, ref_root %llu, parent %llu, owner %llu, offset %llu, num_refs %llu",
  567. ra->action, ra->root, ra->ref.root_objectid, ra->ref.parent,
  568. ra->ref.owner, ra->ref.offset, ra->ref.num_refs);
  569. __print_stack_trace(fs_info, ra);
  570. }
  571. /*
  572. * Dumps all the information from the block entry to printk, it's going to be
  573. * awesome.
  574. */
  575. static void dump_block_entry(struct btrfs_fs_info *fs_info,
  576. struct block_entry *be)
  577. {
  578. struct ref_entry *ref;
  579. struct root_entry *re;
  580. struct ref_action *ra;
  581. struct rb_node *n;
  582. btrfs_err(fs_info,
  583. "dumping block entry [%llu %llu], num_refs %llu, metadata %d, from disk %d",
  584. be->bytenr, be->len, be->num_refs, be->metadata,
  585. be->from_disk);
  586. for (n = rb_first(&be->refs); n; n = rb_next(n)) {
  587. ref = rb_entry(n, struct ref_entry, node);
  588. btrfs_err(fs_info,
  589. " ref root %llu, parent %llu, owner %llu, offset %llu, num_refs %llu",
  590. ref->root_objectid, ref->parent, ref->owner,
  591. ref->offset, ref->num_refs);
  592. }
  593. for (n = rb_first(&be->roots); n; n = rb_next(n)) {
  594. re = rb_entry(n, struct root_entry, node);
  595. btrfs_err(fs_info, " root entry %llu, num_refs %llu",
  596. re->root_objectid, re->num_refs);
  597. }
  598. list_for_each_entry(ra, &be->actions, list)
  599. dump_ref_action(fs_info, ra);
  600. }
  601. /*
  602. * btrfs_ref_tree_mod: called when we modify a ref for a bytenr
  603. * @root: the root we are making this modification from.
  604. * @bytenr: the bytenr we are modifying.
  605. * @num_bytes: number of bytes.
  606. * @parent: the parent bytenr.
  607. * @ref_root: the original root owner of the bytenr.
  608. * @owner: level in the case of metadata, inode in the case of data.
  609. * @offset: 0 for metadata, file offset for data.
  610. * @action: the action that we are doing, this is the same as the delayed ref
  611. * action.
  612. *
  613. * This will add an action item to the given bytenr and do sanity checks to make
  614. * sure we haven't messed something up. If we are making a new allocation and
  615. * this block entry has history we will delete all previous actions as long as
  616. * our sanity checks pass as they are no longer needed.
  617. */
  618. int btrfs_ref_tree_mod(struct btrfs_root *root, u64 bytenr, u64 num_bytes,
  619. u64 parent, u64 ref_root, u64 owner, u64 offset,
  620. int action)
  621. {
  622. struct btrfs_fs_info *fs_info = root->fs_info;
  623. struct ref_entry *ref = NULL, *exist;
  624. struct ref_action *ra = NULL;
  625. struct block_entry *be = NULL;
  626. struct root_entry *re = NULL;
  627. int ret = 0;
  628. bool metadata = owner < BTRFS_FIRST_FREE_OBJECTID;
  629. if (!btrfs_test_opt(root->fs_info, REF_VERIFY))
  630. return 0;
  631. ref = kzalloc(sizeof(struct ref_entry), GFP_NOFS);
  632. ra = kmalloc(sizeof(struct ref_action), GFP_NOFS);
  633. if (!ra || !ref) {
  634. kfree(ref);
  635. kfree(ra);
  636. ret = -ENOMEM;
  637. goto out;
  638. }
  639. if (parent) {
  640. ref->parent = parent;
  641. } else {
  642. ref->root_objectid = ref_root;
  643. ref->owner = owner;
  644. ref->offset = offset;
  645. }
  646. ref->num_refs = (action == BTRFS_DROP_DELAYED_REF) ? -1 : 1;
  647. memcpy(&ra->ref, ref, sizeof(struct ref_entry));
  648. /*
  649. * Save the extra info from the delayed ref in the ref action to make it
  650. * easier to figure out what is happening. The real ref's we add to the
  651. * ref tree need to reflect what we save on disk so it matches any
  652. * on-disk refs we pre-loaded.
  653. */
  654. ra->ref.owner = owner;
  655. ra->ref.offset = offset;
  656. ra->ref.root_objectid = ref_root;
  657. __save_stack_trace(ra);
  658. INIT_LIST_HEAD(&ra->list);
  659. ra->action = action;
  660. ra->root = root->objectid;
  661. /*
  662. * This is an allocation, preallocate the block_entry in case we haven't
  663. * used it before.
  664. */
  665. ret = -EINVAL;
  666. if (action == BTRFS_ADD_DELAYED_EXTENT) {
  667. /*
  668. * For subvol_create we'll just pass in whatever the parent root
  669. * is and the new root objectid, so let's not treat the passed
  670. * in root as if it really has a ref for this bytenr.
  671. */
  672. be = add_block_entry(root->fs_info, bytenr, num_bytes, ref_root);
  673. if (IS_ERR(be)) {
  674. kfree(ref);
  675. kfree(ra);
  676. ret = PTR_ERR(be);
  677. goto out;
  678. }
  679. be->num_refs++;
  680. if (metadata)
  681. be->metadata = 1;
  682. if (be->num_refs != 1) {
  683. btrfs_err(fs_info,
  684. "re-allocated a block that still has references to it!");
  685. dump_block_entry(fs_info, be);
  686. dump_ref_action(fs_info, ra);
  687. kfree(ref);
  688. kfree(ra);
  689. goto out_unlock;
  690. }
  691. while (!list_empty(&be->actions)) {
  692. struct ref_action *tmp;
  693. tmp = list_first_entry(&be->actions, struct ref_action,
  694. list);
  695. list_del(&tmp->list);
  696. kfree(tmp);
  697. }
  698. } else {
  699. struct root_entry *tmp;
  700. if (!parent) {
  701. re = kmalloc(sizeof(struct root_entry), GFP_NOFS);
  702. if (!re) {
  703. kfree(ref);
  704. kfree(ra);
  705. ret = -ENOMEM;
  706. goto out;
  707. }
  708. /*
  709. * This is the root that is modifying us, so it's the
  710. * one we want to lookup below when we modify the
  711. * re->num_refs.
  712. */
  713. ref_root = root->objectid;
  714. re->root_objectid = root->objectid;
  715. re->num_refs = 0;
  716. }
  717. spin_lock(&root->fs_info->ref_verify_lock);
  718. be = lookup_block_entry(&root->fs_info->block_tree, bytenr);
  719. if (!be) {
  720. btrfs_err(fs_info,
  721. "trying to do action %d to bytenr %llu num_bytes %llu but there is no existing entry!",
  722. action, (unsigned long long)bytenr,
  723. (unsigned long long)num_bytes);
  724. dump_ref_action(fs_info, ra);
  725. kfree(ref);
  726. kfree(ra);
  727. goto out_unlock;
  728. }
  729. if (!parent) {
  730. tmp = insert_root_entry(&be->roots, re);
  731. if (tmp) {
  732. kfree(re);
  733. re = tmp;
  734. }
  735. }
  736. }
  737. exist = insert_ref_entry(&be->refs, ref);
  738. if (exist) {
  739. if (action == BTRFS_DROP_DELAYED_REF) {
  740. if (exist->num_refs == 0) {
  741. btrfs_err(fs_info,
  742. "dropping a ref for a existing root that doesn't have a ref on the block");
  743. dump_block_entry(fs_info, be);
  744. dump_ref_action(fs_info, ra);
  745. kfree(ref);
  746. kfree(ra);
  747. goto out_unlock;
  748. }
  749. exist->num_refs--;
  750. if (exist->num_refs == 0) {
  751. rb_erase(&exist->node, &be->refs);
  752. kfree(exist);
  753. }
  754. } else if (!be->metadata) {
  755. exist->num_refs++;
  756. } else {
  757. btrfs_err(fs_info,
  758. "attempting to add another ref for an existing ref on a tree block");
  759. dump_block_entry(fs_info, be);
  760. dump_ref_action(fs_info, ra);
  761. kfree(ref);
  762. kfree(ra);
  763. goto out_unlock;
  764. }
  765. kfree(ref);
  766. } else {
  767. if (action == BTRFS_DROP_DELAYED_REF) {
  768. btrfs_err(fs_info,
  769. "dropping a ref for a root that doesn't have a ref on the block");
  770. dump_block_entry(fs_info, be);
  771. dump_ref_action(fs_info, ra);
  772. kfree(ra);
  773. goto out_unlock;
  774. }
  775. }
  776. if (!parent && !re) {
  777. re = lookup_root_entry(&be->roots, ref_root);
  778. if (!re) {
  779. /*
  780. * This shouldn't happen because we will add our re
  781. * above when we lookup the be with !parent, but just in
  782. * case catch this case so we don't panic because I
  783. * didn't thik of some other corner case.
  784. */
  785. btrfs_err(fs_info, "failed to find root %llu for %llu",
  786. root->objectid, be->bytenr);
  787. dump_block_entry(fs_info, be);
  788. dump_ref_action(fs_info, ra);
  789. kfree(ra);
  790. goto out_unlock;
  791. }
  792. }
  793. if (action == BTRFS_DROP_DELAYED_REF) {
  794. if (re)
  795. re->num_refs--;
  796. be->num_refs--;
  797. } else if (action == BTRFS_ADD_DELAYED_REF) {
  798. be->num_refs++;
  799. if (re)
  800. re->num_refs++;
  801. }
  802. list_add_tail(&ra->list, &be->actions);
  803. ret = 0;
  804. out_unlock:
  805. spin_unlock(&root->fs_info->ref_verify_lock);
  806. out:
  807. if (ret)
  808. btrfs_clear_opt(fs_info->mount_opt, REF_VERIFY);
  809. return ret;
  810. }
  811. /* Free up the ref cache */
  812. void btrfs_free_ref_cache(struct btrfs_fs_info *fs_info)
  813. {
  814. struct block_entry *be;
  815. struct rb_node *n;
  816. if (!btrfs_test_opt(fs_info, REF_VERIFY))
  817. return;
  818. spin_lock(&fs_info->ref_verify_lock);
  819. while ((n = rb_first(&fs_info->block_tree))) {
  820. be = rb_entry(n, struct block_entry, node);
  821. rb_erase(&be->node, &fs_info->block_tree);
  822. free_block_entry(be);
  823. cond_resched_lock(&fs_info->ref_verify_lock);
  824. }
  825. spin_unlock(&fs_info->ref_verify_lock);
  826. }
  827. void btrfs_free_ref_tree_range(struct btrfs_fs_info *fs_info, u64 start,
  828. u64 len)
  829. {
  830. struct block_entry *be = NULL, *entry;
  831. struct rb_node *n;
  832. if (!btrfs_test_opt(fs_info, REF_VERIFY))
  833. return;
  834. spin_lock(&fs_info->ref_verify_lock);
  835. n = fs_info->block_tree.rb_node;
  836. while (n) {
  837. entry = rb_entry(n, struct block_entry, node);
  838. if (entry->bytenr < start) {
  839. n = n->rb_right;
  840. } else if (entry->bytenr > start) {
  841. n = n->rb_left;
  842. } else {
  843. be = entry;
  844. break;
  845. }
  846. /* We want to get as close to start as possible */
  847. if (be == NULL ||
  848. (entry->bytenr < start && be->bytenr > start) ||
  849. (entry->bytenr < start && entry->bytenr > be->bytenr))
  850. be = entry;
  851. }
  852. /*
  853. * Could have an empty block group, maybe have something to check for
  854. * this case to verify we were actually empty?
  855. */
  856. if (!be) {
  857. spin_unlock(&fs_info->ref_verify_lock);
  858. return;
  859. }
  860. n = &be->node;
  861. while (n) {
  862. be = rb_entry(n, struct block_entry, node);
  863. n = rb_next(n);
  864. if (be->bytenr < start && be->bytenr + be->len > start) {
  865. btrfs_err(fs_info,
  866. "block entry overlaps a block group [%llu,%llu]!",
  867. start, len);
  868. dump_block_entry(fs_info, be);
  869. continue;
  870. }
  871. if (be->bytenr < start)
  872. continue;
  873. if (be->bytenr >= start + len)
  874. break;
  875. if (be->bytenr + be->len > start + len) {
  876. btrfs_err(fs_info,
  877. "block entry overlaps a block group [%llu,%llu]!",
  878. start, len);
  879. dump_block_entry(fs_info, be);
  880. }
  881. rb_erase(&be->node, &fs_info->block_tree);
  882. free_block_entry(be);
  883. }
  884. spin_unlock(&fs_info->ref_verify_lock);
  885. }
  886. /* Walk down all roots and build the ref tree, meant to be called at mount */
  887. int btrfs_build_ref_tree(struct btrfs_fs_info *fs_info)
  888. {
  889. struct btrfs_path *path;
  890. struct extent_buffer *eb;
  891. u64 bytenr = 0, num_bytes = 0;
  892. int ret, level;
  893. if (!btrfs_test_opt(fs_info, REF_VERIFY))
  894. return 0;
  895. path = btrfs_alloc_path();
  896. if (!path)
  897. return -ENOMEM;
  898. eb = btrfs_read_lock_root_node(fs_info->extent_root);
  899. btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
  900. level = btrfs_header_level(eb);
  901. path->nodes[level] = eb;
  902. path->slots[level] = 0;
  903. path->locks[level] = BTRFS_READ_LOCK_BLOCKING;
  904. while (1) {
  905. /*
  906. * We have to keep track of the bytenr/num_bytes we last hit
  907. * because we could have run out of space for an inline ref, and
  908. * would have had to added a ref key item which may appear on a
  909. * different leaf from the original extent item.
  910. */
  911. ret = walk_down_tree(fs_info->extent_root, path, level,
  912. &bytenr, &num_bytes);
  913. if (ret)
  914. break;
  915. ret = walk_up_tree(path, &level);
  916. if (ret < 0)
  917. break;
  918. if (ret > 0) {
  919. ret = 0;
  920. break;
  921. }
  922. }
  923. if (ret) {
  924. btrfs_clear_opt(fs_info->mount_opt, REF_VERIFY);
  925. btrfs_free_ref_cache(fs_info);
  926. }
  927. btrfs_free_path(path);
  928. return ret;
  929. }