extent-tree.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  1. #include <linux/module.h>
  2. #include "ctree.h"
  3. #include "disk-io.h"
  4. #include "print-tree.h"
  5. #include "transaction.h"
  6. static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
  7. *orig_root, u64 num_blocks, u64 search_start, u64
  8. search_end, struct btrfs_key *ins);
  9. static int finish_current_insert(struct btrfs_trans_handle *trans, struct
  10. btrfs_root *extent_root);
  11. static int del_pending_extents(struct btrfs_trans_handle *trans, struct
  12. btrfs_root *extent_root);
  13. int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
  14. struct btrfs_root *root,
  15. u64 blocknr, u64 num_blocks)
  16. {
  17. struct btrfs_path *path;
  18. int ret;
  19. struct btrfs_key key;
  20. struct btrfs_leaf *l;
  21. struct btrfs_extent_item *item;
  22. struct btrfs_key ins;
  23. u32 refs;
  24. find_free_extent(trans, root->fs_info->extent_root, 0, 0, (u64)-1,
  25. &ins);
  26. path = btrfs_alloc_path();
  27. BUG_ON(!path);
  28. btrfs_init_path(path);
  29. key.objectid = blocknr;
  30. key.flags = 0;
  31. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  32. key.offset = num_blocks;
  33. ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
  34. 0, 1);
  35. if (ret != 0) {
  36. printk("can't find block %Lu %Lu\n", blocknr, num_blocks);
  37. BUG();
  38. }
  39. BUG_ON(ret != 0);
  40. l = btrfs_buffer_leaf(path->nodes[0]);
  41. item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
  42. refs = btrfs_extent_refs(item);
  43. btrfs_set_extent_refs(item, refs + 1);
  44. btrfs_mark_buffer_dirty(path->nodes[0]);
  45. btrfs_release_path(root->fs_info->extent_root, path);
  46. btrfs_free_path(path);
  47. finish_current_insert(trans, root->fs_info->extent_root);
  48. del_pending_extents(trans, root->fs_info->extent_root);
  49. return 0;
  50. }
  51. static int lookup_extent_ref(struct btrfs_trans_handle *trans,
  52. struct btrfs_root *root, u64 blocknr,
  53. u64 num_blocks, u32 *refs)
  54. {
  55. struct btrfs_path *path;
  56. int ret;
  57. struct btrfs_key key;
  58. struct btrfs_leaf *l;
  59. struct btrfs_extent_item *item;
  60. path = btrfs_alloc_path();
  61. btrfs_init_path(path);
  62. key.objectid = blocknr;
  63. key.offset = num_blocks;
  64. key.flags = 0;
  65. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  66. ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
  67. 0, 0);
  68. if (ret != 0)
  69. BUG();
  70. l = btrfs_buffer_leaf(path->nodes[0]);
  71. item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
  72. *refs = btrfs_extent_refs(item);
  73. btrfs_release_path(root->fs_info->extent_root, path);
  74. btrfs_free_path(path);
  75. return 0;
  76. }
  77. int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
  78. struct btrfs_root *root)
  79. {
  80. return btrfs_inc_extent_ref(trans, root, bh_blocknr(root->node), 1);
  81. }
  82. int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  83. struct buffer_head *buf)
  84. {
  85. u64 blocknr;
  86. struct btrfs_node *buf_node;
  87. struct btrfs_leaf *buf_leaf;
  88. struct btrfs_disk_key *key;
  89. struct btrfs_file_extent_item *fi;
  90. int i;
  91. int leaf;
  92. int ret;
  93. if (!root->ref_cows)
  94. return 0;
  95. buf_node = btrfs_buffer_node(buf);
  96. leaf = btrfs_is_leaf(buf_node);
  97. buf_leaf = btrfs_buffer_leaf(buf);
  98. for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
  99. if (leaf) {
  100. key = &buf_leaf->items[i].key;
  101. if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
  102. continue;
  103. fi = btrfs_item_ptr(buf_leaf, i,
  104. struct btrfs_file_extent_item);
  105. if (btrfs_file_extent_type(fi) ==
  106. BTRFS_FILE_EXTENT_INLINE)
  107. continue;
  108. ret = btrfs_inc_extent_ref(trans, root,
  109. btrfs_file_extent_disk_blocknr(fi),
  110. btrfs_file_extent_disk_num_blocks(fi));
  111. BUG_ON(ret);
  112. } else {
  113. blocknr = btrfs_node_blockptr(buf_node, i);
  114. ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
  115. BUG_ON(ret);
  116. }
  117. }
  118. return 0;
  119. }
  120. int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
  121. btrfs_root *root)
  122. {
  123. unsigned long gang[8];
  124. u64 first = 0;
  125. int ret;
  126. int i;
  127. struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
  128. while(1) {
  129. ret = find_first_radix_bit(pinned_radix, gang,
  130. ARRAY_SIZE(gang));
  131. if (!ret)
  132. break;
  133. if (!first)
  134. first = gang[0];
  135. for (i = 0; i < ret; i++) {
  136. clear_radix_bit(pinned_radix, gang[i]);
  137. }
  138. }
  139. if (root->fs_info->last_insert.objectid > first)
  140. root->fs_info->last_insert.objectid = first;
  141. root->fs_info->last_insert.offset = 0;
  142. return 0;
  143. }
  144. static int finish_current_insert(struct btrfs_trans_handle *trans, struct
  145. btrfs_root *extent_root)
  146. {
  147. struct btrfs_key ins;
  148. struct btrfs_extent_item extent_item;
  149. int i;
  150. int ret;
  151. u64 super_blocks_used;
  152. struct btrfs_fs_info *info = extent_root->fs_info;
  153. btrfs_set_extent_refs(&extent_item, 1);
  154. ins.offset = 1;
  155. ins.flags = 0;
  156. btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
  157. btrfs_set_extent_type(&extent_item, BTRFS_EXTENT_TREE);
  158. btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid);
  159. for (i = 0; i < extent_root->fs_info->current_insert.flags; i++) {
  160. ins.objectid = extent_root->fs_info->current_insert.objectid +
  161. i;
  162. super_blocks_used = btrfs_super_blocks_used(info->disk_super);
  163. btrfs_set_super_blocks_used(info->disk_super,
  164. super_blocks_used + 1);
  165. ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
  166. sizeof(extent_item));
  167. BUG_ON(ret);
  168. }
  169. extent_root->fs_info->current_insert.offset = 0;
  170. return 0;
  171. }
  172. static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
  173. {
  174. int err;
  175. struct btrfs_header *header;
  176. struct buffer_head *bh;
  177. if (!pending) {
  178. bh = btrfs_find_tree_block(root, blocknr);
  179. if (bh) {
  180. if (buffer_uptodate(bh)) {
  181. u64 transid =
  182. root->fs_info->running_transaction->transid;
  183. header = btrfs_buffer_header(bh);
  184. if (btrfs_header_generation(header) ==
  185. transid) {
  186. btrfs_block_release(root, bh);
  187. return 0;
  188. }
  189. }
  190. btrfs_block_release(root, bh);
  191. }
  192. err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
  193. } else {
  194. err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
  195. }
  196. BUG_ON(err);
  197. return 0;
  198. }
  199. /*
  200. * remove an extent from the root, returns 0 on success
  201. */
  202. static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
  203. *root, u64 blocknr, u64 num_blocks, int pin)
  204. {
  205. struct btrfs_path *path;
  206. struct btrfs_key key;
  207. struct btrfs_fs_info *info = root->fs_info;
  208. struct btrfs_root *extent_root = info->extent_root;
  209. int ret;
  210. struct btrfs_extent_item *ei;
  211. struct btrfs_key ins;
  212. u32 refs;
  213. key.objectid = blocknr;
  214. key.flags = 0;
  215. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  216. key.offset = num_blocks;
  217. find_free_extent(trans, root, 0, 0, (u64)-1, &ins);
  218. path = btrfs_alloc_path();
  219. BUG_ON(!path);
  220. btrfs_init_path(path);
  221. ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
  222. if (ret) {
  223. printk("failed to find %Lu\n", key.objectid);
  224. btrfs_print_tree(extent_root, extent_root->node);
  225. printk("failed to find %Lu\n", key.objectid);
  226. BUG();
  227. }
  228. ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
  229. struct btrfs_extent_item);
  230. BUG_ON(ei->refs == 0);
  231. refs = btrfs_extent_refs(ei) - 1;
  232. btrfs_set_extent_refs(ei, refs);
  233. btrfs_mark_buffer_dirty(path->nodes[0]);
  234. if (refs == 0) {
  235. u64 super_blocks_used;
  236. if (pin) {
  237. ret = pin_down_block(root, blocknr, 0);
  238. BUG_ON(ret);
  239. }
  240. super_blocks_used = btrfs_super_blocks_used(info->disk_super);
  241. btrfs_set_super_blocks_used(info->disk_super,
  242. super_blocks_used - num_blocks);
  243. ret = btrfs_del_item(trans, extent_root, path);
  244. if (ret)
  245. BUG();
  246. }
  247. btrfs_release_path(extent_root, path);
  248. btrfs_free_path(path);
  249. finish_current_insert(trans, extent_root);
  250. return ret;
  251. }
  252. /*
  253. * find all the blocks marked as pending in the radix tree and remove
  254. * them from the extent map
  255. */
  256. static int del_pending_extents(struct btrfs_trans_handle *trans, struct
  257. btrfs_root *extent_root)
  258. {
  259. int ret;
  260. int wret;
  261. int err = 0;
  262. unsigned long gang[4];
  263. int i;
  264. struct radix_tree_root *pending_radix;
  265. struct radix_tree_root *pinned_radix;
  266. pending_radix = &extent_root->fs_info->pending_del_radix;
  267. pinned_radix = &extent_root->fs_info->pinned_radix;
  268. while(1) {
  269. ret = find_first_radix_bit(pending_radix, gang,
  270. ARRAY_SIZE(gang));
  271. if (!ret)
  272. break;
  273. for (i = 0; i < ret; i++) {
  274. wret = set_radix_bit(pinned_radix, gang[i]);
  275. BUG_ON(wret);
  276. wret = clear_radix_bit(pending_radix, gang[i]);
  277. BUG_ON(wret);
  278. wret = __free_extent(trans, extent_root,
  279. gang[i], 1, 0);
  280. if (wret)
  281. err = wret;
  282. }
  283. }
  284. return err;
  285. }
  286. /*
  287. * remove an extent from the root, returns 0 on success
  288. */
  289. int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
  290. *root, u64 blocknr, u64 num_blocks, int pin)
  291. {
  292. struct btrfs_root *extent_root = root->fs_info->extent_root;
  293. int pending_ret;
  294. int ret;
  295. if (root == extent_root) {
  296. pin_down_block(root, blocknr, 1);
  297. return 0;
  298. }
  299. ret = __free_extent(trans, root, blocknr, num_blocks, pin);
  300. pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
  301. return ret ? ret : pending_ret;
  302. }
  303. /*
  304. * walks the btree of allocated extents and find a hole of a given size.
  305. * The key ins is changed to record the hole:
  306. * ins->objectid == block start
  307. * ins->flags = BTRFS_EXTENT_ITEM_KEY
  308. * ins->offset == number of blocks
  309. * Any available blocks before search_start are skipped.
  310. */
  311. static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
  312. *orig_root, u64 num_blocks, u64 search_start, u64
  313. search_end, struct btrfs_key *ins)
  314. {
  315. struct btrfs_path *path;
  316. struct btrfs_key key;
  317. int ret;
  318. u64 hole_size = 0;
  319. int slot = 0;
  320. u64 last_block = 0;
  321. u64 test_block;
  322. int start_found;
  323. struct btrfs_leaf *l;
  324. struct btrfs_root * root = orig_root->fs_info->extent_root;
  325. int total_needed = num_blocks;
  326. int level;
  327. path = btrfs_alloc_path();
  328. ins->flags = 0;
  329. btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
  330. level = btrfs_header_level(btrfs_buffer_header(root->node));
  331. total_needed += (level + 2) * 3;
  332. if (root->fs_info->last_insert.objectid == 0 && search_end == (u64)-1) {
  333. struct btrfs_disk_key *last_key;
  334. btrfs_init_path(path);
  335. ins->objectid = (u64)-1;
  336. ins->offset = (u64)-1;
  337. ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
  338. if (ret < 0)
  339. goto error;
  340. BUG_ON(ret == 0);
  341. if (path->slots[0] > 0)
  342. path->slots[0]--;
  343. l = btrfs_buffer_leaf(path->nodes[0]);
  344. last_key = &l->items[path->slots[0]].key;
  345. search_start = btrfs_disk_key_objectid(last_key);
  346. }
  347. if (root->fs_info->last_insert.objectid > search_start)
  348. search_start = root->fs_info->last_insert.objectid;
  349. check_failed:
  350. btrfs_init_path(path);
  351. ins->objectid = search_start;
  352. ins->offset = 0;
  353. start_found = 0;
  354. ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
  355. if (ret < 0)
  356. goto error;
  357. if (path->slots[0] > 0)
  358. path->slots[0]--;
  359. while (1) {
  360. l = btrfs_buffer_leaf(path->nodes[0]);
  361. slot = path->slots[0];
  362. if (slot >= btrfs_header_nritems(&l->header)) {
  363. ret = btrfs_next_leaf(root, path);
  364. if (ret == 0)
  365. continue;
  366. if (ret < 0)
  367. goto error;
  368. if (!start_found) {
  369. ins->objectid = search_start;
  370. ins->offset = (u64)-1;
  371. start_found = 1;
  372. goto check_pending;
  373. }
  374. ins->objectid = last_block > search_start ?
  375. last_block : search_start;
  376. ins->offset = (u64)-1;
  377. goto check_pending;
  378. }
  379. btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
  380. if (key.objectid >= search_start) {
  381. if (start_found) {
  382. if (last_block < search_start)
  383. last_block = search_start;
  384. hole_size = key.objectid - last_block;
  385. if (hole_size > total_needed) {
  386. ins->objectid = last_block;
  387. ins->offset = hole_size;
  388. goto check_pending;
  389. }
  390. }
  391. }
  392. start_found = 1;
  393. last_block = key.objectid + key.offset;
  394. path->slots[0]++;
  395. }
  396. // FIXME -ENOSPC
  397. check_pending:
  398. /* we have to make sure we didn't find an extent that has already
  399. * been allocated by the map tree or the original allocation
  400. */
  401. btrfs_release_path(root, path);
  402. BUG_ON(ins->objectid < search_start);
  403. for (test_block = ins->objectid;
  404. test_block < ins->objectid + total_needed; test_block++) {
  405. if (test_radix_bit(&root->fs_info->pinned_radix,
  406. test_block)) {
  407. search_start = test_block + 1;
  408. goto check_failed;
  409. }
  410. }
  411. BUG_ON(root->fs_info->current_insert.offset);
  412. root->fs_info->current_insert.offset = total_needed - num_blocks;
  413. root->fs_info->current_insert.objectid = ins->objectid + num_blocks;
  414. root->fs_info->current_insert.flags = 0;
  415. root->fs_info->last_insert.objectid = ins->objectid;
  416. ins->offset = num_blocks;
  417. btrfs_free_path(path);
  418. return 0;
  419. error:
  420. btrfs_release_path(root, path);
  421. btrfs_free_path(path);
  422. return ret;
  423. }
  424. /*
  425. * finds a free extent and does all the dirty work required for allocation
  426. * returns the key for the extent through ins, and a tree buffer for
  427. * the first block of the extent through buf.
  428. *
  429. * returns 0 if everything worked, non-zero otherwise.
  430. */
  431. int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
  432. struct btrfs_root *root, u64 owner,
  433. u8 type, u64 num_blocks, u64 search_start,
  434. u64 search_end, struct btrfs_key *ins)
  435. {
  436. int ret;
  437. int pending_ret;
  438. u64 super_blocks_used;
  439. struct btrfs_fs_info *info = root->fs_info;
  440. struct btrfs_root *extent_root = info->extent_root;
  441. struct btrfs_extent_item extent_item;
  442. btrfs_set_extent_refs(&extent_item, 1);
  443. btrfs_set_extent_owner(&extent_item, owner);
  444. btrfs_set_extent_type(&extent_item, type);
  445. if (root == extent_root) {
  446. BUG_ON(extent_root->fs_info->current_insert.offset == 0);
  447. BUG_ON(num_blocks != 1);
  448. BUG_ON(extent_root->fs_info->current_insert.flags ==
  449. extent_root->fs_info->current_insert.offset);
  450. ins->offset = 1;
  451. ins->objectid = extent_root->fs_info->current_insert.objectid +
  452. extent_root->fs_info->current_insert.flags++;
  453. return 0;
  454. }
  455. ret = find_free_extent(trans, root, num_blocks, search_start,
  456. search_end, ins);
  457. if (ret)
  458. return ret;
  459. super_blocks_used = btrfs_super_blocks_used(info->disk_super);
  460. btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
  461. num_blocks);
  462. ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
  463. sizeof(extent_item));
  464. finish_current_insert(trans, extent_root);
  465. pending_ret = del_pending_extents(trans, extent_root);
  466. if (ret)
  467. return ret;
  468. if (pending_ret)
  469. return pending_ret;
  470. return 0;
  471. }
  472. /*
  473. * helper function to allocate a block for a given tree
  474. * returns the tree buffer or NULL.
  475. */
  476. struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
  477. struct btrfs_root *root)
  478. {
  479. struct btrfs_key ins;
  480. int ret;
  481. struct buffer_head *buf;
  482. ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
  483. BTRFS_EXTENT_TREE,
  484. 1, 0, (unsigned long)-1, &ins);
  485. if (ret) {
  486. BUG();
  487. return NULL;
  488. }
  489. buf = btrfs_find_create_tree_block(root, ins.objectid);
  490. set_buffer_uptodate(buf);
  491. return buf;
  492. }
  493. static int drop_leaf_ref(struct btrfs_trans_handle *trans,
  494. struct btrfs_root *root, struct buffer_head *cur)
  495. {
  496. struct btrfs_disk_key *key;
  497. struct btrfs_leaf *leaf;
  498. struct btrfs_file_extent_item *fi;
  499. int i;
  500. int nritems;
  501. int ret;
  502. BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
  503. leaf = btrfs_buffer_leaf(cur);
  504. nritems = btrfs_header_nritems(&leaf->header);
  505. for (i = 0; i < nritems; i++) {
  506. key = &leaf->items[i].key;
  507. if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
  508. continue;
  509. fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
  510. if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE)
  511. continue;
  512. /*
  513. * FIXME make sure to insert a trans record that
  514. * repeats the snapshot del on crash
  515. */
  516. ret = btrfs_free_extent(trans, root,
  517. btrfs_file_extent_disk_blocknr(fi),
  518. btrfs_file_extent_disk_num_blocks(fi),
  519. 0);
  520. BUG_ON(ret);
  521. }
  522. return 0;
  523. }
  524. /*
  525. * helper function for drop_snapshot, this walks down the tree dropping ref
  526. * counts as it goes.
  527. */
  528. static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
  529. *root, struct btrfs_path *path, int *level)
  530. {
  531. struct buffer_head *next;
  532. struct buffer_head *cur;
  533. u64 blocknr;
  534. int ret;
  535. u32 refs;
  536. WARN_ON(*level < 0);
  537. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  538. ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
  539. 1, &refs);
  540. BUG_ON(ret);
  541. if (refs > 1)
  542. goto out;
  543. /*
  544. * walk down to the last node level and free all the leaves
  545. */
  546. while(*level >= 0) {
  547. WARN_ON(*level < 0);
  548. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  549. cur = path->nodes[*level];
  550. if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
  551. WARN_ON(1);
  552. if (path->slots[*level] >=
  553. btrfs_header_nritems(btrfs_buffer_header(cur)))
  554. break;
  555. if (*level == 0) {
  556. ret = drop_leaf_ref(trans, root, cur);
  557. BUG_ON(ret);
  558. break;
  559. }
  560. blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
  561. path->slots[*level]);
  562. ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
  563. BUG_ON(ret);
  564. if (refs != 1) {
  565. path->slots[*level]++;
  566. ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
  567. BUG_ON(ret);
  568. continue;
  569. }
  570. next = read_tree_block(root, blocknr);
  571. WARN_ON(*level <= 0);
  572. if (path->nodes[*level-1])
  573. btrfs_block_release(root, path->nodes[*level-1]);
  574. path->nodes[*level-1] = next;
  575. *level = btrfs_header_level(btrfs_buffer_header(next));
  576. path->slots[*level] = 0;
  577. }
  578. out:
  579. WARN_ON(*level < 0);
  580. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  581. ret = btrfs_free_extent(trans, root,
  582. bh_blocknr(path->nodes[*level]), 1, 1);
  583. btrfs_block_release(root, path->nodes[*level]);
  584. path->nodes[*level] = NULL;
  585. *level += 1;
  586. BUG_ON(ret);
  587. return 0;
  588. }
  589. /*
  590. * helper for dropping snapshots. This walks back up the tree in the path
  591. * to find the first node higher up where we haven't yet gone through
  592. * all the slots
  593. */
  594. static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
  595. *root, struct btrfs_path *path, int *level)
  596. {
  597. int i;
  598. int slot;
  599. int ret;
  600. for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
  601. slot = path->slots[i];
  602. if (slot < btrfs_header_nritems(
  603. btrfs_buffer_header(path->nodes[i])) - 1) {
  604. path->slots[i]++;
  605. *level = i;
  606. return 0;
  607. } else {
  608. ret = btrfs_free_extent(trans, root,
  609. bh_blocknr(path->nodes[*level]),
  610. 1, 1);
  611. BUG_ON(ret);
  612. btrfs_block_release(root, path->nodes[*level]);
  613. path->nodes[*level] = NULL;
  614. *level = i + 1;
  615. }
  616. }
  617. return 1;
  618. }
  619. /*
  620. * drop the reference count on the tree rooted at 'snap'. This traverses
  621. * the tree freeing any blocks that have a ref count of zero after being
  622. * decremented.
  623. */
  624. int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
  625. *root, struct buffer_head *snap)
  626. {
  627. int ret = 0;
  628. int wret;
  629. int level;
  630. struct btrfs_path *path;
  631. int i;
  632. int orig_level;
  633. path = btrfs_alloc_path();
  634. BUG_ON(!path);
  635. btrfs_init_path(path);
  636. level = btrfs_header_level(btrfs_buffer_header(snap));
  637. orig_level = level;
  638. path->nodes[level] = snap;
  639. path->slots[level] = 0;
  640. while(1) {
  641. wret = walk_down_tree(trans, root, path, &level);
  642. if (wret > 0)
  643. break;
  644. if (wret < 0)
  645. ret = wret;
  646. wret = walk_up_tree(trans, root, path, &level);
  647. if (wret > 0)
  648. break;
  649. if (wret < 0)
  650. ret = wret;
  651. }
  652. for (i = 0; i <= orig_level; i++) {
  653. if (path->nodes[i]) {
  654. btrfs_block_release(root, path->nodes[i]);
  655. }
  656. }
  657. btrfs_free_path(path);
  658. return ret;
  659. }