qgroup-tests.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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. if (ret) {
  213. ulist_free(old_roots);
  214. test_msg("Couldn't find old roots: %d\n", ret);
  215. return ret;
  216. }
  217. ret = insert_normal_tree_ref(root, nodesize, nodesize, 0,
  218. BTRFS_FS_TREE_OBJECTID);
  219. if (ret)
  220. return ret;
  221. ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots);
  222. if (ret) {
  223. ulist_free(old_roots);
  224. ulist_free(new_roots);
  225. test_msg("Couldn't find old roots: %d\n", ret);
  226. return ret;
  227. }
  228. ret = btrfs_qgroup_account_extent(&trans, fs_info, nodesize,
  229. nodesize, old_roots, new_roots);
  230. if (ret) {
  231. test_msg("Couldn't account space for a qgroup %d\n", ret);
  232. return ret;
  233. }
  234. if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID,
  235. nodesize, nodesize)) {
  236. test_msg("Qgroup counts didn't match expected values\n");
  237. return -EINVAL;
  238. }
  239. old_roots = NULL;
  240. new_roots = NULL;
  241. ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots);
  242. if (ret) {
  243. ulist_free(old_roots);
  244. test_msg("Couldn't find old roots: %d\n", ret);
  245. return ret;
  246. }
  247. ret = remove_extent_item(root, nodesize, nodesize);
  248. if (ret)
  249. return -EINVAL;
  250. ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots);
  251. if (ret) {
  252. ulist_free(old_roots);
  253. ulist_free(new_roots);
  254. test_msg("Couldn't find old roots: %d\n", ret);
  255. return ret;
  256. }
  257. ret = btrfs_qgroup_account_extent(&trans, fs_info, nodesize,
  258. nodesize, old_roots, new_roots);
  259. if (ret) {
  260. test_msg("Couldn't account space for a qgroup %d\n", ret);
  261. return -EINVAL;
  262. }
  263. if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID, 0, 0)) {
  264. test_msg("Qgroup counts didn't match expected values\n");
  265. return -EINVAL;
  266. }
  267. return 0;
  268. }
  269. /*
  270. * Add a ref for two different roots to make sure the shared value comes out
  271. * right, also remove one of the roots and make sure the exclusive count is
  272. * adjusted properly.
  273. */
  274. static int test_multiple_refs(struct btrfs_root *root,
  275. u32 sectorsize, u32 nodesize)
  276. {
  277. struct btrfs_trans_handle trans;
  278. struct btrfs_fs_info *fs_info = root->fs_info;
  279. struct ulist *old_roots = NULL;
  280. struct ulist *new_roots = NULL;
  281. int ret;
  282. btrfs_init_dummy_trans(&trans);
  283. test_msg("Qgroup multiple refs test\n");
  284. /*
  285. * We have BTRFS_FS_TREE_OBJECTID created already from the
  286. * previous test.
  287. */
  288. ret = btrfs_create_qgroup(NULL, fs_info, BTRFS_FIRST_FREE_OBJECTID);
  289. if (ret) {
  290. test_msg("Couldn't create a qgroup %d\n", ret);
  291. return ret;
  292. }
  293. ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots);
  294. if (ret) {
  295. ulist_free(old_roots);
  296. test_msg("Couldn't find old roots: %d\n", ret);
  297. return ret;
  298. }
  299. ret = insert_normal_tree_ref(root, nodesize, nodesize, 0,
  300. BTRFS_FS_TREE_OBJECTID);
  301. if (ret)
  302. return ret;
  303. ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots);
  304. if (ret) {
  305. ulist_free(old_roots);
  306. ulist_free(new_roots);
  307. test_msg("Couldn't find old roots: %d\n", ret);
  308. return ret;
  309. }
  310. ret = btrfs_qgroup_account_extent(&trans, fs_info, nodesize,
  311. nodesize, old_roots, new_roots);
  312. if (ret) {
  313. test_msg("Couldn't account space for a qgroup %d\n", ret);
  314. return ret;
  315. }
  316. if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID,
  317. nodesize, nodesize)) {
  318. test_msg("Qgroup counts didn't match expected values\n");
  319. return -EINVAL;
  320. }
  321. ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots);
  322. if (ret) {
  323. ulist_free(old_roots);
  324. test_msg("Couldn't find old roots: %d\n", ret);
  325. return ret;
  326. }
  327. ret = add_tree_ref(root, nodesize, nodesize, 0,
  328. BTRFS_FIRST_FREE_OBJECTID);
  329. if (ret)
  330. return ret;
  331. ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots);
  332. if (ret) {
  333. ulist_free(old_roots);
  334. ulist_free(new_roots);
  335. test_msg("Couldn't find old roots: %d\n", ret);
  336. return ret;
  337. }
  338. ret = btrfs_qgroup_account_extent(&trans, fs_info, nodesize,
  339. nodesize, old_roots, new_roots);
  340. if (ret) {
  341. test_msg("Couldn't account space for a qgroup %d\n", ret);
  342. return ret;
  343. }
  344. if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID,
  345. nodesize, 0)) {
  346. test_msg("Qgroup counts didn't match expected values\n");
  347. return -EINVAL;
  348. }
  349. if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FIRST_FREE_OBJECTID,
  350. nodesize, 0)) {
  351. test_msg("Qgroup counts didn't match expected values\n");
  352. return -EINVAL;
  353. }
  354. ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &old_roots);
  355. if (ret) {
  356. ulist_free(old_roots);
  357. test_msg("Couldn't find old roots: %d\n", ret);
  358. return ret;
  359. }
  360. ret = remove_extent_ref(root, nodesize, nodesize, 0,
  361. BTRFS_FIRST_FREE_OBJECTID);
  362. if (ret)
  363. return ret;
  364. ret = btrfs_find_all_roots(&trans, fs_info, nodesize, 0, &new_roots);
  365. if (ret) {
  366. ulist_free(old_roots);
  367. ulist_free(new_roots);
  368. test_msg("Couldn't find old roots: %d\n", ret);
  369. return ret;
  370. }
  371. ret = btrfs_qgroup_account_extent(&trans, fs_info, nodesize,
  372. nodesize, old_roots, new_roots);
  373. if (ret) {
  374. test_msg("Couldn't account space for a qgroup %d\n", ret);
  375. return ret;
  376. }
  377. if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FIRST_FREE_OBJECTID,
  378. 0, 0)) {
  379. test_msg("Qgroup counts didn't match expected values\n");
  380. return -EINVAL;
  381. }
  382. if (btrfs_verify_qgroup_counts(fs_info, BTRFS_FS_TREE_OBJECTID,
  383. nodesize, nodesize)) {
  384. test_msg("Qgroup counts didn't match expected values\n");
  385. return -EINVAL;
  386. }
  387. return 0;
  388. }
  389. int btrfs_test_qgroups(u32 sectorsize, u32 nodesize)
  390. {
  391. struct btrfs_fs_info *fs_info = NULL;
  392. struct btrfs_root *root;
  393. struct btrfs_root *tmp_root;
  394. int ret = 0;
  395. fs_info = btrfs_alloc_dummy_fs_info();
  396. if (!fs_info) {
  397. test_msg("Couldn't allocate dummy fs info\n");
  398. return -ENOMEM;
  399. }
  400. root = btrfs_alloc_dummy_root(fs_info, sectorsize, nodesize);
  401. if (IS_ERR(root)) {
  402. test_msg("Couldn't allocate root\n");
  403. ret = PTR_ERR(root);
  404. goto out;
  405. }
  406. /* We are using this root as our extent root */
  407. root->fs_info->extent_root = root;
  408. /*
  409. * Some of the paths we test assume we have a filled out fs_info, so we
  410. * just need to add the root in there so we don't panic.
  411. */
  412. root->fs_info->tree_root = root;
  413. root->fs_info->quota_root = root;
  414. set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
  415. /*
  416. * Can't use bytenr 0, some things freak out
  417. * *cough*backref walking code*cough*
  418. */
  419. root->node = alloc_test_extent_buffer(root->fs_info, nodesize,
  420. nodesize);
  421. if (!root->node) {
  422. test_msg("Couldn't allocate dummy buffer\n");
  423. ret = -ENOMEM;
  424. goto out;
  425. }
  426. btrfs_set_header_level(root->node, 0);
  427. btrfs_set_header_nritems(root->node, 0);
  428. root->alloc_bytenr += 2 * nodesize;
  429. tmp_root = btrfs_alloc_dummy_root(fs_info, sectorsize, nodesize);
  430. if (IS_ERR(tmp_root)) {
  431. test_msg("Couldn't allocate a fs root\n");
  432. ret = PTR_ERR(tmp_root);
  433. goto out;
  434. }
  435. tmp_root->root_key.objectid = BTRFS_FS_TREE_OBJECTID;
  436. root->fs_info->fs_root = tmp_root;
  437. ret = btrfs_insert_fs_root(root->fs_info, tmp_root);
  438. if (ret) {
  439. test_msg("Couldn't insert fs root %d\n", ret);
  440. goto out;
  441. }
  442. tmp_root = btrfs_alloc_dummy_root(fs_info, sectorsize, nodesize);
  443. if (IS_ERR(tmp_root)) {
  444. test_msg("Couldn't allocate a fs root\n");
  445. ret = PTR_ERR(tmp_root);
  446. goto out;
  447. }
  448. tmp_root->root_key.objectid = BTRFS_FIRST_FREE_OBJECTID;
  449. ret = btrfs_insert_fs_root(root->fs_info, tmp_root);
  450. if (ret) {
  451. test_msg("Couldn't insert fs root %d\n", ret);
  452. goto out;
  453. }
  454. test_msg("Running qgroup tests\n");
  455. ret = test_no_shared_qgroup(root, sectorsize, nodesize);
  456. if (ret)
  457. goto out;
  458. ret = test_multiple_refs(root, sectorsize, nodesize);
  459. out:
  460. btrfs_free_dummy_root(root);
  461. btrfs_free_dummy_fs_info(fs_info);
  462. return ret;
  463. }