qgroup-tests.c 13 KB

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