extent-tree.c 17 KB

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