dir-test.c 11 KB

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