dir-test.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <signal.h>
  4. #include <unistd.h>
  5. #include "kerncompat.h"
  6. #include "radix-tree.h"
  7. #include "ctree.h"
  8. #include "disk-io.h"
  9. #include "print-tree.h"
  10. #include "hash.h"
  11. #include "transaction.h"
  12. int keep_running = 1;
  13. struct btrfs_super_block super;
  14. static u64 dir_oid = 44556;
  15. static u64 file_oid = 33778;
  16. static int find_num(struct radix_tree_root *root, unsigned long *num_ret,
  17. int exists)
  18. {
  19. unsigned long num = rand();
  20. unsigned long res[2];
  21. int ret;
  22. again:
  23. ret = radix_tree_gang_lookup(root, (void **)res, num, 2);
  24. if (exists) {
  25. if (ret == 0)
  26. return -1;
  27. num = res[0];
  28. } else if (ret != 0 && num == res[0]) {
  29. num++;
  30. if (ret > 1 && num == res[1]) {
  31. num++;
  32. goto again;
  33. }
  34. }
  35. *num_ret = num;
  36. return 0;
  37. }
  38. static int ins_one(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  39. struct radix_tree_root *radix)
  40. {
  41. int ret;
  42. char buf[128];
  43. unsigned long oid;
  44. u64 objectid;
  45. struct btrfs_path path;
  46. struct btrfs_key inode_map;
  47. find_num(radix, &oid, 0);
  48. sprintf(buf, "str-%lu", oid);
  49. ret = btrfs_find_free_objectid(trans, root, dir_oid + 1, &objectid);
  50. if (ret)
  51. goto error;
  52. inode_map.objectid = objectid;
  53. inode_map.flags = 0;
  54. inode_map.offset = 0;
  55. ret = btrfs_insert_inode_map(trans, root, objectid, &inode_map);
  56. if (ret)
  57. goto error;
  58. ret = btrfs_insert_dir_item(trans, root, buf, strlen(buf), dir_oid,
  59. objectid, 1);
  60. if (ret)
  61. goto error;
  62. radix_tree_preload(GFP_KERNEL);
  63. ret = radix_tree_insert(radix, oid, (void *)oid);
  64. radix_tree_preload_end();
  65. if (ret)
  66. goto error;
  67. return ret;
  68. error:
  69. if (ret != -EEXIST)
  70. goto fatal;
  71. /*
  72. * if we got an EEXIST, it may be due to hash collision, double
  73. * check
  74. */
  75. btrfs_init_path(&path);
  76. ret = btrfs_lookup_dir_item(trans, root, &path, dir_oid, buf,
  77. strlen(buf), 0);
  78. if (ret)
  79. goto fatal_release;
  80. if (!btrfs_match_dir_item_name(root, &path, buf, strlen(buf))) {
  81. struct btrfs_dir_item *di;
  82. char *found;
  83. u32 found_len;
  84. u64 myhash;
  85. u64 foundhash;
  86. di = btrfs_item_ptr(&path.nodes[0]->leaf, path.slots[0],
  87. struct btrfs_dir_item);
  88. found = (char *)(di + 1);
  89. found_len = btrfs_dir_name_len(di);
  90. btrfs_name_hash(buf, strlen(buf), &myhash);
  91. btrfs_name_hash(found, found_len, &foundhash);
  92. if (myhash != foundhash)
  93. goto fatal_release;
  94. btrfs_release_path(root, &path);
  95. return 0;
  96. }
  97. fatal_release:
  98. btrfs_release_path(root, &path);
  99. fatal:
  100. printf("failed to insert %lu ret %d\n", oid, ret);
  101. return -1;
  102. }
  103. static int insert_dup(struct btrfs_trans_handle *trans, struct btrfs_root
  104. *root, struct radix_tree_root *radix)
  105. {
  106. int ret;
  107. char buf[128];
  108. unsigned long oid;
  109. ret = find_num(radix, &oid, 1);
  110. if (ret < 0)
  111. return 0;
  112. sprintf(buf, "str-%lu", oid);
  113. ret = btrfs_insert_dir_item(trans, root, buf, strlen(buf), dir_oid,
  114. file_oid, 1);
  115. if (ret != -EEXIST) {
  116. printf("insert on %s gave us %d\n", buf, ret);
  117. return 1;
  118. }
  119. return 0;
  120. }
  121. static int del_dir_item(struct btrfs_trans_handle *trans,
  122. struct btrfs_root *root,
  123. struct radix_tree_root *radix,
  124. unsigned long radix_index,
  125. struct btrfs_path *path)
  126. {
  127. int ret;
  128. unsigned long *ptr;
  129. u64 file_objectid;
  130. struct btrfs_dir_item *di;
  131. struct btrfs_path map_path;
  132. /* find the inode number of the file */
  133. di = btrfs_item_ptr(&path->nodes[0]->leaf, path->slots[0],
  134. struct btrfs_dir_item);
  135. file_objectid = btrfs_dir_objectid(di);
  136. /* delete the directory item */
  137. ret = btrfs_del_item(trans, root, path);
  138. if (ret)
  139. goto out;
  140. /* delete the inode mapping */
  141. btrfs_init_path(&map_path);
  142. ret = btrfs_lookup_inode_map(trans, root, &map_path, file_objectid, -1);
  143. if (ret)
  144. goto out_release;
  145. ret = btrfs_del_item(trans, root->fs_info->inode_root, &map_path);
  146. if (ret)
  147. goto out_release;
  148. if (root->fs_info->last_inode_alloc > file_objectid)
  149. root->fs_info->last_inode_alloc = file_objectid;
  150. btrfs_release_path(root, &map_path);
  151. ptr = radix_tree_delete(radix, radix_index);
  152. if (!ptr) {
  153. ret = -5555;
  154. goto out;
  155. }
  156. return 0;
  157. out_release:
  158. btrfs_release_path(root, &map_path);
  159. out:
  160. printf("failed to delete %lu %d\n", radix_index, ret);
  161. return -1;
  162. }
  163. static int del_one(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  164. struct radix_tree_root *radix)
  165. {
  166. int ret;
  167. char buf[128];
  168. unsigned long oid;
  169. struct btrfs_path path;
  170. ret = find_num(radix, &oid, 1);
  171. if (ret < 0)
  172. return 0;
  173. sprintf(buf, "str-%lu", oid);
  174. btrfs_init_path(&path);
  175. ret = btrfs_lookup_dir_item(trans, root, &path, dir_oid, buf,
  176. strlen(buf), -1);
  177. if (ret)
  178. goto out_release;
  179. ret = del_dir_item(trans, root, radix, oid, &path);
  180. if (ret)
  181. goto out_release;
  182. btrfs_release_path(root, &path);
  183. return ret;
  184. out_release:
  185. btrfs_release_path(root, &path);
  186. printf("failed to delete %lu %d\n", oid, ret);
  187. return -1;
  188. }
  189. static int lookup_item(struct btrfs_trans_handle *trans, struct btrfs_root
  190. *root, struct radix_tree_root *radix)
  191. {
  192. struct btrfs_path path;
  193. char buf[128];
  194. int ret;
  195. unsigned long oid;
  196. u64 objectid;
  197. struct btrfs_dir_item *di;
  198. ret = find_num(radix, &oid, 1);
  199. if (ret < 0)
  200. return 0;
  201. sprintf(buf, "str-%lu", oid);
  202. btrfs_init_path(&path);
  203. ret = btrfs_lookup_dir_item(trans, root, &path, dir_oid, buf,
  204. strlen(buf), 0);
  205. if (!ret) {
  206. di = btrfs_item_ptr(&path.nodes[0]->leaf, path.slots[0],
  207. struct btrfs_dir_item);
  208. objectid = btrfs_dir_objectid(di);
  209. btrfs_release_path(root, &path);
  210. btrfs_init_path(&path);
  211. ret = btrfs_lookup_inode_map(trans, root, &path, objectid, 0);
  212. }
  213. btrfs_release_path(root, &path);
  214. if (ret) {
  215. printf("unable to find key %lu\n", oid);
  216. return -1;
  217. }
  218. return 0;
  219. }
  220. static int lookup_enoent(struct btrfs_trans_handle *trans, struct btrfs_root
  221. *root, struct radix_tree_root *radix)
  222. {
  223. struct btrfs_path path;
  224. char buf[128];
  225. int ret;
  226. unsigned long oid;
  227. ret = find_num(radix, &oid, 0);
  228. if (ret < 0)
  229. return 0;
  230. sprintf(buf, "str-%lu", oid);
  231. btrfs_init_path(&path);
  232. ret = btrfs_lookup_dir_item(trans, root, &path, dir_oid, buf,
  233. strlen(buf), 0);
  234. btrfs_release_path(root, &path);
  235. if (!ret) {
  236. printf("able to find key that should not exist %lu\n", oid);
  237. return -1;
  238. }
  239. return 0;
  240. }
  241. static int empty_tree(struct btrfs_trans_handle *trans, struct btrfs_root
  242. *root, struct radix_tree_root *radix, int nr)
  243. {
  244. struct btrfs_path path;
  245. struct btrfs_key key;
  246. unsigned long found = 0;
  247. u32 found_len;
  248. int ret;
  249. int slot;
  250. int count = 0;
  251. char buf[128];
  252. struct btrfs_dir_item *di;
  253. key.offset = (u64)-1;
  254. key.flags = 0;
  255. btrfs_set_key_type(&key, BTRFS_DIR_ITEM_KEY);
  256. key.objectid = dir_oid;
  257. while(nr-- >= 0) {
  258. btrfs_init_path(&path);
  259. ret = btrfs_search_slot(trans, root, &key, &path, -1, 1);
  260. if (ret < 0) {
  261. btrfs_release_path(root, &path);
  262. return ret;
  263. }
  264. if (ret != 0) {
  265. if (path.slots[0] == 0) {
  266. btrfs_release_path(root, &path);
  267. break;
  268. }
  269. path.slots[0] -= 1;
  270. }
  271. slot = path.slots[0];
  272. di = btrfs_item_ptr(&path.nodes[0]->leaf, slot,
  273. struct btrfs_dir_item);
  274. found_len = btrfs_dir_name_len(di);
  275. memcpy(buf, (char *)(di + 1), found_len);
  276. BUG_ON(found_len > 128);
  277. buf[found_len] = '\0';
  278. found = atoi(buf + 4);
  279. ret = del_dir_item(trans, root, radix, found, &path);
  280. count++;
  281. if (ret) {
  282. fprintf(stderr,
  283. "failed to remove %lu from tree\n",
  284. found);
  285. return -1;
  286. }
  287. btrfs_release_path(root, &path);
  288. if (!keep_running)
  289. break;
  290. }
  291. return 0;
  292. fprintf(stderr, "failed to delete from the radix %lu\n", found);
  293. return -1;
  294. }
  295. static int fill_tree(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  296. struct radix_tree_root *radix, int count)
  297. {
  298. int i;
  299. int ret = 0;
  300. for (i = 0; i < count; i++) {
  301. ret = ins_one(trans, root, radix);
  302. if (ret) {
  303. fprintf(stderr, "fill failed\n");
  304. goto out;
  305. }
  306. if (i % 1000 == 0) {
  307. ret = btrfs_commit_transaction(trans, root, &super);
  308. if (ret) {
  309. fprintf(stderr, "fill commit failed\n");
  310. return ret;
  311. }
  312. }
  313. if (i && i % 10000 == 0) {
  314. printf("bigfill %d\n", i);
  315. }
  316. if (!keep_running)
  317. break;
  318. }
  319. out:
  320. return ret;
  321. }
  322. static int bulk_op(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  323. struct radix_tree_root *radix)
  324. {
  325. int ret;
  326. int nr = rand() % 5000;
  327. static int run_nr = 0;
  328. /* do the bulk op much less frequently */
  329. if (run_nr++ % 100)
  330. return 0;
  331. ret = empty_tree(trans, root, radix, nr);
  332. if (ret)
  333. return ret;
  334. ret = fill_tree(trans, root, radix, nr);
  335. if (ret)
  336. return ret;
  337. return 0;
  338. }
  339. int (*ops[])(struct btrfs_trans_handle *trans, struct btrfs_root *root, struct
  340. radix_tree_root *radix) =
  341. { ins_one, insert_dup, del_one, lookup_item,
  342. lookup_enoent, bulk_op };
  343. void sigstopper(int ignored)
  344. {
  345. keep_running = 0;
  346. fprintf(stderr, "caught exit signal, stopping\n");
  347. }
  348. int print_usage(void)
  349. {
  350. printf("usage: tester [-ih] [-c count] [-f count]\n");
  351. printf("\t -c count -- iteration count after filling\n");
  352. printf("\t -f count -- run this many random inserts before starting\n");
  353. printf("\t -i -- only do initial fill\n");
  354. printf("\t -h -- this help text\n");
  355. exit(1);
  356. }
  357. int main(int ac, char **av)
  358. {
  359. RADIX_TREE(radix, GFP_KERNEL);
  360. struct btrfs_root *root;
  361. int i;
  362. int ret;
  363. int count;
  364. int op;
  365. int iterations = 20000;
  366. int init_fill_count = 800000;
  367. int err = 0;
  368. int initial_only = 0;
  369. struct btrfs_trans_handle *trans;
  370. radix_tree_init();
  371. printf("removing old tree\n");
  372. unlink("dbfile");
  373. root = open_ctree("dbfile", &super);
  374. trans = btrfs_start_transaction(root, 1);
  375. signal(SIGTERM, sigstopper);
  376. signal(SIGINT, sigstopper);
  377. for (i = 1 ; i < ac ; i++) {
  378. if (strcmp(av[i], "-i") == 0) {
  379. initial_only = 1;
  380. } else if (strcmp(av[i], "-c") == 0) {
  381. iterations = atoi(av[i+1]);
  382. i++;
  383. } else if (strcmp(av[i], "-f") == 0) {
  384. init_fill_count = atoi(av[i+1]);
  385. i++;
  386. } else {
  387. print_usage();
  388. }
  389. }
  390. printf("initial fill\n");
  391. ret = fill_tree(trans, root, &radix, init_fill_count);
  392. printf("starting run\n");
  393. if (ret) {
  394. err = ret;
  395. goto out;
  396. }
  397. if (initial_only == 1) {
  398. goto out;
  399. }
  400. for (i = 0; i < iterations; i++) {
  401. op = rand() % ARRAY_SIZE(ops);
  402. count = rand() % 128;
  403. if (i % 2000 == 0) {
  404. printf("%d\n", i);
  405. fflush(stdout);
  406. }
  407. if (i && i % 5000 == 0) {
  408. printf("open & close, root level %d nritems %d\n",
  409. btrfs_header_level(&root->node->node.header),
  410. btrfs_header_nritems(&root->node->node.header));
  411. close_ctree(root, &super);
  412. root = open_ctree("dbfile", &super);
  413. }
  414. while(count--) {
  415. ret = ops[op](trans, root, &radix);
  416. if (ret) {
  417. fprintf(stderr, "op %d failed %d:%d\n",
  418. op, i, iterations);
  419. btrfs_print_tree(root, root->node);
  420. fprintf(stderr, "op %d failed %d:%d\n",
  421. op, i, iterations);
  422. err = ret;
  423. goto out;
  424. }
  425. if (ops[op] == bulk_op)
  426. break;
  427. if (keep_running == 0) {
  428. err = 0;
  429. goto out;
  430. }
  431. }
  432. }
  433. out:
  434. close_ctree(root, &super);
  435. return err;
  436. }