qgroup-tests.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. /*
  2. * Copyright (C) 2013 Facebook. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/types.h>
  19. #include "btrfs-tests.h"
  20. #include "../ctree.h"
  21. #include "../transaction.h"
  22. #include "../disk-io.h"
  23. #include "../qgroup.h"
  24. #include "../backref.h"
  25. static int insert_normal_tree_ref(struct btrfs_root *root, u64 bytenr,
  26. u64 num_bytes, u64 parent, u64 root_objectid)
  27. {
  28. struct btrfs_trans_handle trans;
  29. struct btrfs_extent_item *item;
  30. struct btrfs_extent_inline_ref *iref;
  31. struct btrfs_tree_block_info *block_info;
  32. struct btrfs_path *path;
  33. struct extent_buffer *leaf;
  34. struct btrfs_key ins;
  35. u32 size = sizeof(*item) + sizeof(*iref) + sizeof(*block_info);
  36. int ret;
  37. btrfs_init_dummy_trans(&trans);
  38. ins.objectid = bytenr;
  39. ins.type = BTRFS_EXTENT_ITEM_KEY;
  40. ins.offset = num_bytes;
  41. path = btrfs_alloc_path();
  42. if (!path) {
  43. test_msg("Couldn't allocate path\n");
  44. return -ENOMEM;
  45. }
  46. path->leave_spinning = 1;
  47. ret = btrfs_insert_empty_item(&trans, root, path, &ins, size);
  48. if (ret) {
  49. test_msg("Couldn't insert ref %d\n", ret);
  50. btrfs_free_path(path);
  51. return ret;
  52. }
  53. leaf = path->nodes[0];
  54. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
  55. btrfs_set_extent_refs(leaf, item, 1);
  56. btrfs_set_extent_generation(leaf, item, 1);
  57. btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_TREE_BLOCK);
  58. block_info = (struct btrfs_tree_block_info *)(item + 1);
  59. btrfs_set_tree_block_level(leaf, block_info, 0);
  60. iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
  61. if (parent > 0) {
  62. btrfs_set_extent_inline_ref_type(leaf, iref,
  63. BTRFS_SHARED_BLOCK_REF_KEY);
  64. btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
  65. } else {
  66. btrfs_set_extent_inline_ref_type(leaf, iref, BTRFS_TREE_BLOCK_REF_KEY);
  67. btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
  68. }
  69. btrfs_free_path(path);
  70. return 0;
  71. }
  72. static int add_tree_ref(struct btrfs_root *root, u64 bytenr, u64 num_bytes,
  73. u64 parent, u64 root_objectid)
  74. {
  75. struct btrfs_trans_handle trans;
  76. struct btrfs_extent_item *item;
  77. struct btrfs_path *path;
  78. struct btrfs_key key;
  79. u64 refs;
  80. int ret;
  81. btrfs_init_dummy_trans(&trans);
  82. key.objectid = bytenr;
  83. key.type = BTRFS_EXTENT_ITEM_KEY;
  84. key.offset = num_bytes;
  85. path = btrfs_alloc_path();
  86. if (!path) {
  87. test_msg("Couldn't allocate path\n");
  88. return -ENOMEM;
  89. }
  90. path->leave_spinning = 1;
  91. ret = btrfs_search_slot(&trans, root, &key, path, 0, 1);
  92. if (ret) {
  93. test_msg("Couldn't find extent ref\n");
  94. btrfs_free_path(path);
  95. return ret;
  96. }
  97. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  98. struct btrfs_extent_item);
  99. refs = btrfs_extent_refs(path->nodes[0], item);
  100. btrfs_set_extent_refs(path->nodes[0], item, refs + 1);
  101. btrfs_release_path(path);
  102. key.objectid = bytenr;
  103. if (parent) {
  104. key.type = BTRFS_SHARED_BLOCK_REF_KEY;
  105. key.offset = parent;
  106. } else {
  107. key.type = BTRFS_TREE_BLOCK_REF_KEY;
  108. key.offset = root_objectid;
  109. }
  110. ret = btrfs_insert_empty_item(&trans, root, path, &key, 0);
  111. if (ret)
  112. test_msg("Failed to insert backref\n");
  113. btrfs_free_path(path);
  114. return ret;
  115. }
  116. static int remove_extent_item(struct btrfs_root *root, u64 bytenr,
  117. u64 num_bytes)
  118. {
  119. struct btrfs_trans_handle trans;
  120. struct btrfs_key key;
  121. struct btrfs_path *path;
  122. int ret;
  123. btrfs_init_dummy_trans(&trans);
  124. key.objectid = bytenr;
  125. key.type = BTRFS_EXTENT_ITEM_KEY;
  126. key.offset = num_bytes;
  127. path = btrfs_alloc_path();
  128. if (!path) {
  129. test_msg("Couldn't allocate path\n");
  130. return -ENOMEM;
  131. }
  132. path->leave_spinning = 1;
  133. ret = btrfs_search_slot(&trans, root, &key, path, -1, 1);
  134. if (ret) {
  135. test_msg("Didn't find our key %d\n", ret);
  136. btrfs_free_path(path);
  137. return ret;
  138. }
  139. btrfs_del_item(&trans, root, path);
  140. btrfs_free_path(path);
  141. return 0;
  142. }
  143. static int remove_extent_ref(struct btrfs_root *root, u64 bytenr,
  144. u64 num_bytes, u64 parent, u64 root_objectid)
  145. {
  146. struct btrfs_trans_handle trans;
  147. struct btrfs_extent_item *item;
  148. struct btrfs_path *path;
  149. struct btrfs_key key;
  150. u64 refs;
  151. int ret;
  152. btrfs_init_dummy_trans(&trans);
  153. key.objectid = bytenr;
  154. key.type = BTRFS_EXTENT_ITEM_KEY;
  155. key.offset = num_bytes;
  156. path = btrfs_alloc_path();
  157. if (!path) {
  158. test_msg("Couldn't allocate path\n");
  159. return -ENOMEM;
  160. }
  161. path->leave_spinning = 1;
  162. ret = btrfs_search_slot(&trans, root, &key, path, 0, 1);
  163. if (ret) {
  164. test_msg("Couldn't find extent ref\n");
  165. btrfs_free_path(path);
  166. return ret;
  167. }
  168. item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  169. struct btrfs_extent_item);
  170. refs = btrfs_extent_refs(path->nodes[0], item);
  171. btrfs_set_extent_refs(path->nodes[0], item, refs - 1);
  172. btrfs_release_path(path);
  173. key.objectid = bytenr;
  174. if (parent) {
  175. key.type = BTRFS_SHARED_BLOCK_REF_KEY;
  176. key.offset = parent;
  177. } else {
  178. key.type = BTRFS_TREE_BLOCK_REF_KEY;
  179. key.offset = root_objectid;
  180. }
  181. ret = btrfs_search_slot(&trans, root, &key, path, -1, 1);
  182. if (ret) {
  183. test_msg("Couldn't find backref %d\n", ret);
  184. btrfs_free_path(path);
  185. return ret;
  186. }
  187. btrfs_del_item(&trans, root, path);
  188. btrfs_free_path(path);
  189. return ret;
  190. }
  191. static int test_no_shared_qgroup(struct btrfs_root *root,
  192. u32 sectorsize, u32 nodesize)
  193. {
  194. struct btrfs_trans_handle trans;
  195. struct btrfs_fs_info *fs_info = root->fs_info;
  196. struct ulist *old_roots = NULL;
  197. struct ulist *new_roots = NULL;
  198. int ret;
  199. btrfs_init_dummy_trans(&trans);
  200. test_msg("Qgroup basic add\n");
  201. ret = btrfs_create_qgroup(NULL, fs_info, BTRFS_FS_TREE_OBJECTID);
  202. if (ret) {
  203. test_msg("Couldn't create a qgroup %d\n", ret);
  204. return ret;
  205. }
  206. /*
  207. * Since the test trans doesn't have the complicated delayed refs,
  208. * we can only call btrfs_qgroup_account_extent() directly to test
  209. * quota.
  210. */
  211. ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots,
  212. false);
  213. if (ret) {
  214. ulist_free(old_roots);
  215. test_msg("Couldn't find old roots: %d\n", ret);
  216. return ret;
  217. }
  218. ret = insert_normal_tree_ref(root, nodesize, nodesize, 0,
  219. BTRFS_FS_TREE_OBJECTID);
  220. if (ret)
  221. return ret;
  222. ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots,
  223. false);
  224. if (ret) {
  225. ulist_free(old_roots);
  226. ulist_free(new_roots);
  227. test_msg("Couldn't find old roots: %d\n", ret);
  228. return ret;
  229. }
  230. ret = btrfs_qgroup_account_extent(&trans, fs_info, nodesize,
  231. nodesize, old_roots, new_roots);
  232. if (ret) {
  233. test_msg("Couldn't account space for a qgroup %d\n", ret);
  234. return ret;
  235. }
  236. if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID,
  237. nodesize, nodesize)) {
  238. test_msg("Qgroup counts didn't match expected values\n");
  239. return -EINVAL;
  240. }
  241. old_roots = NULL;
  242. new_roots = NULL;
  243. ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots,
  244. false);
  245. if (ret) {
  246. ulist_free(old_roots);
  247. test_msg("Couldn't find old roots: %d\n", ret);
  248. return ret;
  249. }
  250. ret = remove_extent_item(root, nodesize, nodesize);
  251. if (ret)
  252. return -EINVAL;
  253. ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots,
  254. false);
  255. if (ret) {
  256. ulist_free(old_roots);
  257. ulist_free(new_roots);
  258. test_msg("Couldn't find old roots: %d\n", ret);
  259. return ret;
  260. }
  261. ret = btrfs_qgroup_account_extent(&trans, fs_info, nodesize,
  262. nodesize, old_roots, new_roots);
  263. if (ret) {
  264. test_msg("Couldn't account space for a qgroup %d\n", ret);
  265. return -EINVAL;
  266. }
  267. if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID, 0, 0)) {
  268. test_msg("Qgroup counts didn't match expected values\n");
  269. return -EINVAL;
  270. }
  271. return 0;
  272. }
  273. /*
  274. * Add a ref for two different roots to make sure the shared value comes out
  275. * right, also remove one of the roots and make sure the exclusive count is
  276. * adjusted properly.
  277. */
  278. static int test_multiple_refs(struct btrfs_root *root,
  279. u32 sectorsize, u32 nodesize)
  280. {
  281. struct btrfs_trans_handle trans;
  282. struct btrfs_fs_info *fs_info = root->fs_info;
  283. struct ulist *old_roots = NULL;
  284. struct ulist *new_roots = NULL;
  285. int ret;
  286. btrfs_init_dummy_trans(&trans);
  287. test_msg("Qgroup multiple refs test\n");
  288. /*
  289. * We have BTRFS_FS_TREE_OBJECTID created already from the
  290. * previous test.
  291. */
  292. ret = btrfs_create_qgroup(NULL, fs_info, BTRFS_FIRST_FREE_OBJECTID);
  293. if (ret) {
  294. test_msg("Couldn't create a qgroup %d\n", ret);
  295. return ret;
  296. }
  297. ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots,
  298. false);
  299. if (ret) {
  300. ulist_free(old_roots);
  301. test_msg("Couldn't find old roots: %d\n", ret);
  302. return ret;
  303. }
  304. ret = insert_normal_tree_ref(root, nodesize, nodesize, 0,
  305. BTRFS_FS_TREE_OBJECTID);
  306. if (ret)
  307. return ret;
  308. ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots,
  309. false);
  310. if (ret) {
  311. ulist_free(old_roots);
  312. ulist_free(new_roots);
  313. test_msg("Couldn't find old roots: %d\n", ret);
  314. return ret;
  315. }
  316. ret = btrfs_qgroup_account_extent(&trans, fs_info, nodesize,
  317. nodesize, old_roots, new_roots);
  318. if (ret) {
  319. test_msg("Couldn't account space for a qgroup %d\n", ret);
  320. return ret;
  321. }
  322. if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID,
  323. nodesize, nodesize)) {
  324. test_msg("Qgroup counts didn't match expected values\n");
  325. return -EINVAL;
  326. }
  327. ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots,
  328. false);
  329. if (ret) {
  330. ulist_free(old_roots);
  331. test_msg("Couldn't find old roots: %d\n", ret);
  332. return ret;
  333. }
  334. ret = add_tree_ref(root, nodesize, nodesize, 0,
  335. BTRFS_FIRST_FREE_OBJECTID);
  336. if (ret)
  337. return ret;
  338. ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots,
  339. false);
  340. if (ret) {
  341. ulist_free(old_roots);
  342. ulist_free(new_roots);
  343. test_msg("Couldn't find old roots: %d\n", ret);
  344. return ret;
  345. }
  346. ret = btrfs_qgroup_account_extent(&trans, fs_info, nodesize,
  347. nodesize, old_roots, new_roots);
  348. if (ret) {
  349. test_msg("Couldn't account space for a qgroup %d\n", ret);
  350. return ret;
  351. }
  352. if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID,
  353. nodesize, 0)) {
  354. test_msg("Qgroup counts didn't match expected values\n");
  355. return -EINVAL;
  356. }
  357. if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FIRST_FREE_OBJECTID,
  358. nodesize, 0)) {
  359. test_msg("Qgroup counts didn't match expected values\n");
  360. return -EINVAL;
  361. }
  362. ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots,
  363. false);
  364. if (ret) {
  365. ulist_free(old_roots);
  366. test_msg("Couldn't find old roots: %d\n", ret);
  367. return ret;
  368. }
  369. ret = remove_extent_ref(root, nodesize, nodesize, 0,
  370. BTRFS_FIRST_FREE_OBJECTID);
  371. if (ret)
  372. return ret;
  373. ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots,
  374. false);
  375. if (ret) {
  376. ulist_free(old_roots);
  377. ulist_free(new_roots);
  378. test_msg("Couldn't find old roots: %d\n", ret);
  379. return ret;
  380. }
  381. ret = btrfs_qgroup_account_extent(&trans, fs_info, nodesize,
  382. nodesize, old_roots, new_roots);
  383. if (ret) {
  384. test_msg("Couldn't account space for a qgroup %d\n", ret);
  385. return ret;
  386. }
  387. if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FIRST_FREE_OBJECTID,
  388. 0, 0)) {
  389. test_msg("Qgroup counts didn't match expected values\n");
  390. return -EINVAL;
  391. }
  392. if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID,
  393. nodesize, nodesize)) {
  394. test_msg("Qgroup counts didn't match expected values\n");
  395. return -EINVAL;
  396. }
  397. return 0;
  398. }
  399. int btrfs_test_qgroups(u32 sectorsize, u32 nodesize)
  400. {
  401. struct btrfs_fs_info *fs_info = NULL;
  402. struct btrfs_root *root;
  403. struct btrfs_root *tmp_root;
  404. int ret = 0;
  405. fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
  406. if (!fs_info) {
  407. test_msg("Couldn't allocate dummy fs info\n");
  408. return -ENOMEM;
  409. }
  410. root = btrfs_alloc_dummy_root(fs_info);
  411. if (IS_ERR(root)) {
  412. test_msg("Couldn't allocate root\n");
  413. ret = PTR_ERR(root);
  414. goto out;
  415. }
  416. /* We are using this root as our extent root */
  417. root->fs_info->extent_root = root;
  418. /*
  419. * Some of the paths we test assume we have a filled out fs_info, so we
  420. * just need to add the root in there so we don't panic.
  421. */
  422. root->fs_info->tree_root = root;
  423. root->fs_info->quota_root = root;
  424. set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
  425. /*
  426. * Can't use bytenr 0, some things freak out
  427. * *cough*backref walking code*cough*
  428. */
  429. root->node = alloc_test_extent_buffer(root->fs_info, nodesize);
  430. if (!root->node) {
  431. test_msg("Couldn't allocate dummy buffer\n");
  432. ret = -ENOMEM;
  433. goto out;
  434. }
  435. btrfs_set_header_level(root->node, 0);
  436. btrfs_set_header_nritems(root->node, 0);
  437. root->alloc_bytenr += 2 * nodesize;
  438. tmp_root = btrfs_alloc_dummy_root(fs_info);
  439. if (IS_ERR(tmp_root)) {
  440. test_msg("Couldn't allocate a fs root\n");
  441. ret = PTR_ERR(tmp_root);
  442. goto out;
  443. }
  444. tmp_root->root_key.objectid = BTRFS_FS_TREE_OBJECTID;
  445. root->fs_info->fs_root = tmp_root;
  446. ret = btrfs_insert_fs_root(root->fs_info, tmp_root);
  447. if (ret) {
  448. test_msg("Couldn't insert fs root %d\n", ret);
  449. goto out;
  450. }
  451. tmp_root = btrfs_alloc_dummy_root(fs_info);
  452. if (IS_ERR(tmp_root)) {
  453. test_msg("Couldn't allocate a fs root\n");
  454. ret = PTR_ERR(tmp_root);
  455. goto out;
  456. }
  457. tmp_root->root_key.objectid = BTRFS_FIRST_FREE_OBJECTID;
  458. ret = btrfs_insert_fs_root(root->fs_info, tmp_root);
  459. if (ret) {
  460. test_msg("Couldn't insert fs root %d\n", ret);
  461. goto out;
  462. }
  463. test_msg("Running qgroup tests\n");
  464. ret = test_no_shared_qgroup(root, sectorsize, nodesize);
  465. if (ret)
  466. goto out;
  467. ret = test_multiple_refs(root, sectorsize, nodesize);
  468. out:
  469. btrfs_free_dummy_root(root);
  470. btrfs_free_dummy_fs_info(fs_info);
  471. return ret;
  472. }