qgroup-tests.c 11 KB

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