extent-tree.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  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 find_search_start(struct btrfs_root *root, int data)
  14. {
  15. struct btrfs_block_group_cache *cache[8];
  16. struct btrfs_fs_info *info = root->fs_info;
  17. u64 used;
  18. u64 last;
  19. int i;
  20. int ret;
  21. cache[0] = info->block_group_cache;
  22. if (!cache[0])
  23. goto find_new;
  24. used = btrfs_block_group_used(&cache[0]->item);
  25. if (used < (cache[0]->key.offset * 3 / 2))
  26. return 0;
  27. find_new:
  28. last = 0;
  29. while(1) {
  30. ret = radix_tree_gang_lookup_tag(&info->block_group_radix,
  31. (void **)cache,
  32. last, ARRAY_SIZE(cache),
  33. BTRFS_BLOCK_GROUP_DIRTY);
  34. if (!ret)
  35. break;
  36. for (i = 0; i < ret; i++) {
  37. used = btrfs_block_group_used(&cache[i]->item);
  38. if (used < (cache[i]->key.offset * 3 / 2)) {
  39. info->block_group_cache = cache[i];
  40. cache[i]->last_alloc = cache[i]->first_free;
  41. return 0;
  42. }
  43. last = cache[i]->key.objectid +
  44. cache[i]->key.offset - 1;
  45. }
  46. }
  47. last = 0;
  48. while(1) {
  49. ret = radix_tree_gang_lookup(&info->block_group_radix,
  50. (void **)cache,
  51. last, ARRAY_SIZE(cache));
  52. if (!ret)
  53. break;
  54. for (i = 0; i < ret; i++) {
  55. used = btrfs_block_group_used(&cache[i]->item);
  56. if (used < (cache[i]->key.offset * 3 / 2)) {
  57. info->block_group_cache = cache[i];
  58. cache[i]->last_alloc = cache[i]->first_free;
  59. return 0;
  60. }
  61. last = cache[i]->key.objectid +
  62. cache[i]->key.offset - 1;
  63. }
  64. }
  65. info->block_group_cache = NULL;
  66. return 0;
  67. }
  68. int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
  69. struct btrfs_root *root,
  70. u64 blocknr, u64 num_blocks)
  71. {
  72. struct btrfs_path *path;
  73. int ret;
  74. struct btrfs_key key;
  75. struct btrfs_leaf *l;
  76. struct btrfs_extent_item *item;
  77. struct btrfs_key ins;
  78. u32 refs;
  79. find_free_extent(trans, root->fs_info->extent_root, 0, 0, (u64)-1,
  80. &ins);
  81. path = btrfs_alloc_path();
  82. BUG_ON(!path);
  83. btrfs_init_path(path);
  84. key.objectid = blocknr;
  85. key.flags = 0;
  86. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  87. key.offset = num_blocks;
  88. ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
  89. 0, 1);
  90. if (ret != 0) {
  91. printk("can't find block %Lu %Lu\n", blocknr, num_blocks);
  92. BUG();
  93. }
  94. BUG_ON(ret != 0);
  95. l = btrfs_buffer_leaf(path->nodes[0]);
  96. item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
  97. refs = btrfs_extent_refs(item);
  98. btrfs_set_extent_refs(item, refs + 1);
  99. btrfs_mark_buffer_dirty(path->nodes[0]);
  100. btrfs_release_path(root->fs_info->extent_root, path);
  101. btrfs_free_path(path);
  102. finish_current_insert(trans, root->fs_info->extent_root);
  103. del_pending_extents(trans, root->fs_info->extent_root);
  104. return 0;
  105. }
  106. static int lookup_extent_ref(struct btrfs_trans_handle *trans,
  107. struct btrfs_root *root, u64 blocknr,
  108. u64 num_blocks, u32 *refs)
  109. {
  110. struct btrfs_path *path;
  111. int ret;
  112. struct btrfs_key key;
  113. struct btrfs_leaf *l;
  114. struct btrfs_extent_item *item;
  115. path = btrfs_alloc_path();
  116. btrfs_init_path(path);
  117. key.objectid = blocknr;
  118. key.offset = num_blocks;
  119. key.flags = 0;
  120. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  121. ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
  122. 0, 0);
  123. if (ret != 0)
  124. BUG();
  125. l = btrfs_buffer_leaf(path->nodes[0]);
  126. item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
  127. *refs = btrfs_extent_refs(item);
  128. btrfs_release_path(root->fs_info->extent_root, path);
  129. btrfs_free_path(path);
  130. return 0;
  131. }
  132. int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
  133. struct btrfs_root *root)
  134. {
  135. return btrfs_inc_extent_ref(trans, root, bh_blocknr(root->node), 1);
  136. }
  137. int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  138. struct buffer_head *buf)
  139. {
  140. u64 blocknr;
  141. struct btrfs_node *buf_node;
  142. struct btrfs_leaf *buf_leaf;
  143. struct btrfs_disk_key *key;
  144. struct btrfs_file_extent_item *fi;
  145. int i;
  146. int leaf;
  147. int ret;
  148. if (!root->ref_cows)
  149. return 0;
  150. buf_node = btrfs_buffer_node(buf);
  151. leaf = btrfs_is_leaf(buf_node);
  152. buf_leaf = btrfs_buffer_leaf(buf);
  153. for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
  154. if (leaf) {
  155. key = &buf_leaf->items[i].key;
  156. if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
  157. continue;
  158. fi = btrfs_item_ptr(buf_leaf, i,
  159. struct btrfs_file_extent_item);
  160. if (btrfs_file_extent_type(fi) ==
  161. BTRFS_FILE_EXTENT_INLINE)
  162. continue;
  163. ret = btrfs_inc_extent_ref(trans, root,
  164. btrfs_file_extent_disk_blocknr(fi),
  165. btrfs_file_extent_disk_num_blocks(fi));
  166. BUG_ON(ret);
  167. } else {
  168. blocknr = btrfs_node_blockptr(buf_node, i);
  169. ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
  170. BUG_ON(ret);
  171. }
  172. }
  173. return 0;
  174. }
  175. static int write_one_cache_group(struct btrfs_trans_handle *trans,
  176. struct btrfs_root *root,
  177. struct btrfs_path *path,
  178. struct btrfs_block_group_cache *cache)
  179. {
  180. int ret;
  181. int pending_ret;
  182. struct btrfs_root *extent_root = root->fs_info->extent_root;
  183. struct btrfs_block_group_item *bi;
  184. struct btrfs_key ins;
  185. find_free_extent(trans, extent_root, 0, 0, (u64)-1, &ins);
  186. ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
  187. BUG_ON(ret);
  188. bi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
  189. struct btrfs_block_group_item);
  190. memcpy(bi, &cache->item, sizeof(*bi));
  191. mark_buffer_dirty(path->nodes[0]);
  192. btrfs_release_path(extent_root, path);
  193. finish_current_insert(trans, extent_root);
  194. pending_ret = del_pending_extents(trans, extent_root);
  195. if (ret)
  196. return ret;
  197. if (pending_ret)
  198. return pending_ret;
  199. return 0;
  200. }
  201. int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
  202. struct btrfs_root *root)
  203. {
  204. struct btrfs_block_group_cache *cache[8];
  205. int ret;
  206. int err = 0;
  207. int werr = 0;
  208. struct radix_tree_root *radix = &root->fs_info->block_group_radix;
  209. int i;
  210. struct btrfs_path *path;
  211. path = btrfs_alloc_path();
  212. if (!path)
  213. return -ENOMEM;
  214. while(1) {
  215. ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
  216. 0, ARRAY_SIZE(cache),
  217. BTRFS_BLOCK_GROUP_DIRTY);
  218. if (!ret)
  219. break;
  220. for (i = 0; i < ret; i++) {
  221. radix_tree_tag_clear(radix, cache[i]->key.objectid +
  222. cache[i]->key.offset - 1,
  223. BTRFS_BLOCK_GROUP_DIRTY);
  224. err = write_one_cache_group(trans, root,
  225. path, cache[i]);
  226. if (err)
  227. werr = err;
  228. }
  229. }
  230. btrfs_free_path(path);
  231. return werr;
  232. }
  233. static int update_block_group(struct btrfs_trans_handle *trans,
  234. struct btrfs_root *root,
  235. u64 blocknr, u64 num, int alloc)
  236. {
  237. struct btrfs_block_group_cache *cache;
  238. struct btrfs_fs_info *info = root->fs_info;
  239. u64 total = num;
  240. u64 old_val;
  241. u64 block_in_group;
  242. int ret;
  243. while(total) {
  244. ret = radix_tree_gang_lookup(&info->block_group_radix,
  245. (void **)&cache, blocknr, 1);
  246. if (!ret) {
  247. printk(KERN_CRIT "blocknr %Lu lookup failed\n",
  248. blocknr);
  249. return -1;
  250. }
  251. block_in_group = blocknr - cache->key.objectid;
  252. WARN_ON(block_in_group > cache->key.offset);
  253. radix_tree_tag_set(&info->block_group_radix,
  254. cache->key.objectid + cache->key.offset - 1,
  255. BTRFS_BLOCK_GROUP_DIRTY);
  256. old_val = btrfs_block_group_used(&cache->item);
  257. num = min(total, cache->key.offset - block_in_group);
  258. total -= num;
  259. blocknr += num;
  260. if (alloc) {
  261. old_val += num;
  262. if (blocknr > cache->last_alloc)
  263. cache->last_alloc = blocknr;
  264. } else {
  265. old_val -= num;
  266. if (blocknr < cache->first_free)
  267. cache->first_free = blocknr;
  268. }
  269. btrfs_set_block_group_used(&cache->item, old_val);
  270. }
  271. return 0;
  272. }
  273. static int try_remove_page(struct address_space *mapping, unsigned long index)
  274. {
  275. int ret;
  276. ret = invalidate_mapping_pages(mapping, index, index);
  277. return ret;
  278. }
  279. int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
  280. btrfs_root *root)
  281. {
  282. unsigned long gang[8];
  283. struct inode *btree_inode = root->fs_info->btree_inode;
  284. u64 first = 0;
  285. int ret;
  286. int i;
  287. struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
  288. while(1) {
  289. ret = find_first_radix_bit(pinned_radix, gang,
  290. ARRAY_SIZE(gang));
  291. if (!ret)
  292. break;
  293. if (!first)
  294. first = gang[0];
  295. for (i = 0; i < ret; i++) {
  296. clear_radix_bit(pinned_radix, gang[i]);
  297. try_remove_page(btree_inode->i_mapping,
  298. gang[i] << (PAGE_CACHE_SHIFT -
  299. btree_inode->i_blkbits));
  300. }
  301. }
  302. if (root->fs_info->block_group_cache) {
  303. root->fs_info->block_group_cache->last_alloc =
  304. root->fs_info->block_group_cache->first_free;
  305. }
  306. return 0;
  307. }
  308. static int finish_current_insert(struct btrfs_trans_handle *trans, struct
  309. btrfs_root *extent_root)
  310. {
  311. struct btrfs_key ins;
  312. struct btrfs_extent_item extent_item;
  313. int i;
  314. int ret;
  315. u64 super_blocks_used;
  316. struct btrfs_fs_info *info = extent_root->fs_info;
  317. btrfs_set_extent_refs(&extent_item, 1);
  318. ins.offset = 1;
  319. ins.flags = 0;
  320. btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
  321. btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid);
  322. for (i = 0; i < extent_root->fs_info->extent_tree_insert_nr; i++) {
  323. ins.objectid = extent_root->fs_info->extent_tree_insert[i];
  324. super_blocks_used = btrfs_super_blocks_used(info->disk_super);
  325. btrfs_set_super_blocks_used(info->disk_super,
  326. super_blocks_used + 1);
  327. ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
  328. sizeof(extent_item));
  329. BUG_ON(ret);
  330. }
  331. extent_root->fs_info->extent_tree_insert_nr = 0;
  332. extent_root->fs_info->extent_tree_prealloc_nr = 0;
  333. return 0;
  334. }
  335. static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
  336. {
  337. int err;
  338. struct btrfs_header *header;
  339. struct buffer_head *bh;
  340. if (!pending) {
  341. bh = btrfs_find_tree_block(root, blocknr);
  342. if (bh) {
  343. if (buffer_uptodate(bh)) {
  344. u64 transid =
  345. root->fs_info->running_transaction->transid;
  346. header = btrfs_buffer_header(bh);
  347. if (btrfs_header_generation(header) ==
  348. transid) {
  349. btrfs_block_release(root, bh);
  350. return 0;
  351. }
  352. }
  353. btrfs_block_release(root, bh);
  354. }
  355. err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
  356. } else {
  357. err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
  358. }
  359. BUG_ON(err);
  360. return 0;
  361. }
  362. /*
  363. * remove an extent from the root, returns 0 on success
  364. */
  365. static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
  366. *root, u64 blocknr, u64 num_blocks, int pin)
  367. {
  368. struct btrfs_path *path;
  369. struct btrfs_key key;
  370. struct btrfs_fs_info *info = root->fs_info;
  371. struct btrfs_root *extent_root = info->extent_root;
  372. int ret;
  373. struct btrfs_extent_item *ei;
  374. struct btrfs_key ins;
  375. u32 refs;
  376. key.objectid = blocknr;
  377. key.flags = 0;
  378. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  379. key.offset = num_blocks;
  380. find_free_extent(trans, root, 0, 0, (u64)-1, &ins);
  381. path = btrfs_alloc_path();
  382. BUG_ON(!path);
  383. btrfs_init_path(path);
  384. ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
  385. if (ret) {
  386. printk("failed to find %Lu\n", key.objectid);
  387. btrfs_print_tree(extent_root, extent_root->node);
  388. printk("failed to find %Lu\n", key.objectid);
  389. BUG();
  390. }
  391. ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
  392. struct btrfs_extent_item);
  393. BUG_ON(ei->refs == 0);
  394. refs = btrfs_extent_refs(ei) - 1;
  395. btrfs_set_extent_refs(ei, refs);
  396. btrfs_mark_buffer_dirty(path->nodes[0]);
  397. if (refs == 0) {
  398. u64 super_blocks_used;
  399. if (pin) {
  400. ret = pin_down_block(root, blocknr, 0);
  401. BUG_ON(ret);
  402. }
  403. super_blocks_used = btrfs_super_blocks_used(info->disk_super);
  404. btrfs_set_super_blocks_used(info->disk_super,
  405. super_blocks_used - num_blocks);
  406. ret = btrfs_del_item(trans, extent_root, path);
  407. if (ret)
  408. BUG();
  409. ret = update_block_group(trans, root, blocknr, num_blocks, 0);
  410. BUG_ON(ret);
  411. }
  412. btrfs_release_path(extent_root, path);
  413. btrfs_free_path(path);
  414. finish_current_insert(trans, extent_root);
  415. return ret;
  416. }
  417. /*
  418. * find all the blocks marked as pending in the radix tree and remove
  419. * them from the extent map
  420. */
  421. static int del_pending_extents(struct btrfs_trans_handle *trans, struct
  422. btrfs_root *extent_root)
  423. {
  424. int ret;
  425. int wret;
  426. int err = 0;
  427. unsigned long gang[4];
  428. int i;
  429. struct radix_tree_root *pending_radix;
  430. struct radix_tree_root *pinned_radix;
  431. pending_radix = &extent_root->fs_info->pending_del_radix;
  432. pinned_radix = &extent_root->fs_info->pinned_radix;
  433. while(1) {
  434. ret = find_first_radix_bit(pending_radix, gang,
  435. ARRAY_SIZE(gang));
  436. if (!ret)
  437. break;
  438. for (i = 0; i < ret; i++) {
  439. wret = set_radix_bit(pinned_radix, gang[i]);
  440. BUG_ON(wret);
  441. wret = clear_radix_bit(pending_radix, gang[i]);
  442. BUG_ON(wret);
  443. wret = __free_extent(trans, extent_root,
  444. gang[i], 1, 0);
  445. if (wret)
  446. err = wret;
  447. }
  448. }
  449. return err;
  450. }
  451. /*
  452. * remove an extent from the root, returns 0 on success
  453. */
  454. int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
  455. *root, u64 blocknr, u64 num_blocks, int pin)
  456. {
  457. struct btrfs_root *extent_root = root->fs_info->extent_root;
  458. int pending_ret;
  459. int ret;
  460. if (root == extent_root) {
  461. pin_down_block(root, blocknr, 1);
  462. return 0;
  463. }
  464. ret = __free_extent(trans, root, blocknr, num_blocks, pin);
  465. pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
  466. return ret ? ret : pending_ret;
  467. }
  468. /*
  469. * walks the btree of allocated extents and find a hole of a given size.
  470. * The key ins is changed to record the hole:
  471. * ins->objectid == block start
  472. * ins->flags = BTRFS_EXTENT_ITEM_KEY
  473. * ins->offset == number of blocks
  474. * Any available blocks before search_start are skipped.
  475. */
  476. static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
  477. *orig_root, u64 num_blocks, u64 search_start, u64
  478. search_end, struct btrfs_key *ins)
  479. {
  480. struct btrfs_path *path;
  481. struct btrfs_key key;
  482. int ret;
  483. u64 hole_size = 0;
  484. int slot = 0;
  485. u64 last_block = 0;
  486. u64 test_block;
  487. int start_found;
  488. struct btrfs_leaf *l;
  489. struct btrfs_root * root = orig_root->fs_info->extent_root;
  490. struct btrfs_fs_info *info = root->fs_info;
  491. int total_needed = num_blocks;
  492. int total_found = 0;
  493. int fill_prealloc = 0;
  494. int level;
  495. path = btrfs_alloc_path();
  496. ins->flags = 0;
  497. btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
  498. level = btrfs_header_level(btrfs_buffer_header(root->node));
  499. if (num_blocks == 0) {
  500. fill_prealloc = 1;
  501. num_blocks = 1;
  502. total_needed = (min(level + 1, BTRFS_MAX_LEVEL) + 2) * 3;
  503. }
  504. find_search_start(root, 0);
  505. if (info->block_group_cache &&
  506. info->block_group_cache->last_alloc > search_start)
  507. search_start = info->block_group_cache->last_alloc;
  508. check_failed:
  509. btrfs_init_path(path);
  510. ins->objectid = search_start;
  511. ins->offset = 0;
  512. start_found = 0;
  513. ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
  514. if (ret < 0)
  515. goto error;
  516. if (path->slots[0] > 0)
  517. path->slots[0]--;
  518. while (1) {
  519. l = btrfs_buffer_leaf(path->nodes[0]);
  520. slot = path->slots[0];
  521. if (slot >= btrfs_header_nritems(&l->header)) {
  522. if (fill_prealloc) {
  523. info->extent_tree_prealloc_nr = 0;
  524. total_found = 0;
  525. }
  526. ret = btrfs_next_leaf(root, path);
  527. if (ret == 0)
  528. continue;
  529. if (ret < 0)
  530. goto error;
  531. if (!start_found) {
  532. ins->objectid = search_start;
  533. ins->offset = (u64)-1 - search_start;
  534. start_found = 1;
  535. goto check_pending;
  536. }
  537. ins->objectid = last_block > search_start ?
  538. last_block : search_start;
  539. ins->offset = (u64)-1 - ins->objectid;
  540. goto check_pending;
  541. }
  542. btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
  543. if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
  544. goto next;
  545. if (key.objectid >= search_start) {
  546. if (start_found) {
  547. if (last_block < search_start)
  548. last_block = search_start;
  549. hole_size = key.objectid - last_block;
  550. if (hole_size >= num_blocks) {
  551. ins->objectid = last_block;
  552. ins->offset = hole_size;
  553. goto check_pending;
  554. }
  555. }
  556. }
  557. start_found = 1;
  558. last_block = key.objectid + key.offset;
  559. next:
  560. path->slots[0]++;
  561. }
  562. // FIXME -ENOSPC
  563. check_pending:
  564. /* we have to make sure we didn't find an extent that has already
  565. * been allocated by the map tree or the original allocation
  566. */
  567. btrfs_release_path(root, path);
  568. BUG_ON(ins->objectid < search_start);
  569. if (ins->objectid >= btrfs_super_total_blocks(info->disk_super)) {
  570. if (search_start == 0)
  571. return -ENOSPC;
  572. search_start = 0;
  573. goto check_failed;
  574. }
  575. for (test_block = ins->objectid;
  576. test_block < ins->objectid + num_blocks; test_block++) {
  577. if (test_radix_bit(&info->pinned_radix, test_block)) {
  578. search_start = test_block + 1;
  579. goto check_failed;
  580. }
  581. }
  582. if (!fill_prealloc && info->extent_tree_insert_nr) {
  583. u64 last =
  584. info->extent_tree_insert[info->extent_tree_insert_nr - 1];
  585. if (ins->objectid + num_blocks >
  586. info->extent_tree_insert[0] &&
  587. ins->objectid <= last) {
  588. search_start = last + 1;
  589. WARN_ON(1);
  590. goto check_failed;
  591. }
  592. }
  593. if (!fill_prealloc && info->extent_tree_prealloc_nr) {
  594. u64 first =
  595. info->extent_tree_prealloc[info->extent_tree_prealloc_nr - 1];
  596. if (ins->objectid + num_blocks > first &&
  597. ins->objectid <= info->extent_tree_prealloc[0]) {
  598. search_start = info->extent_tree_prealloc[0] + 1;
  599. WARN_ON(1);
  600. goto check_failed;
  601. }
  602. }
  603. if (fill_prealloc) {
  604. int nr;
  605. test_block = ins->objectid;
  606. while(test_block < ins->objectid + ins->offset &&
  607. total_found < total_needed) {
  608. nr = total_needed - total_found - 1;
  609. BUG_ON(nr < 0);
  610. info->extent_tree_prealloc[nr] = test_block;
  611. total_found++;
  612. test_block++;
  613. }
  614. if (total_found < total_needed) {
  615. search_start = test_block;
  616. goto check_failed;
  617. }
  618. info->extent_tree_prealloc_nr = total_found;
  619. }
  620. ret = radix_tree_gang_lookup(&info->block_group_radix,
  621. (void **)&info->block_group_cache,
  622. ins->objectid, 1);
  623. if (ret) {
  624. info->block_group_cache->last_alloc = ins->objectid;
  625. }
  626. ins->offset = num_blocks;
  627. btrfs_free_path(path);
  628. return 0;
  629. error:
  630. btrfs_release_path(root, path);
  631. btrfs_free_path(path);
  632. return ret;
  633. }
  634. /*
  635. * finds a free extent and does all the dirty work required for allocation
  636. * returns the key for the extent through ins, and a tree buffer for
  637. * the first block of the extent through buf.
  638. *
  639. * returns 0 if everything worked, non-zero otherwise.
  640. */
  641. int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
  642. struct btrfs_root *root, u64 owner,
  643. u64 num_blocks, u64 search_start,
  644. u64 search_end, struct btrfs_key *ins)
  645. {
  646. int ret;
  647. int pending_ret;
  648. u64 super_blocks_used;
  649. struct btrfs_fs_info *info = root->fs_info;
  650. struct btrfs_root *extent_root = info->extent_root;
  651. struct btrfs_extent_item extent_item;
  652. struct btrfs_key prealloc_key;
  653. btrfs_set_extent_refs(&extent_item, 1);
  654. btrfs_set_extent_owner(&extent_item, owner);
  655. if (root == extent_root) {
  656. int nr;
  657. BUG_ON(info->extent_tree_prealloc_nr == 0);
  658. BUG_ON(num_blocks != 1);
  659. ins->offset = 1;
  660. info->extent_tree_prealloc_nr--;
  661. nr = info->extent_tree_prealloc_nr;
  662. ins->objectid = info->extent_tree_prealloc[nr];
  663. info->extent_tree_insert[info->extent_tree_insert_nr++] =
  664. ins->objectid;
  665. ret = update_block_group(trans, root,
  666. ins->objectid, ins->offset, 1);
  667. BUG_ON(ret);
  668. return 0;
  669. }
  670. /* do the real allocation */
  671. ret = find_free_extent(trans, root, num_blocks, search_start,
  672. search_end, ins);
  673. if (ret)
  674. return ret;
  675. /* then do prealloc for the extent tree */
  676. ret = find_free_extent(trans, root, 0, ins->objectid + ins->offset,
  677. search_end, &prealloc_key);
  678. if (ret)
  679. return ret;
  680. super_blocks_used = btrfs_super_blocks_used(info->disk_super);
  681. btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
  682. num_blocks);
  683. ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
  684. sizeof(extent_item));
  685. finish_current_insert(trans, extent_root);
  686. pending_ret = del_pending_extents(trans, extent_root);
  687. if (ret)
  688. return ret;
  689. if (pending_ret)
  690. return pending_ret;
  691. ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
  692. return 0;
  693. }
  694. /*
  695. * helper function to allocate a block for a given tree
  696. * returns the tree buffer or NULL.
  697. */
  698. struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
  699. struct btrfs_root *root)
  700. {
  701. struct btrfs_key ins;
  702. int ret;
  703. struct buffer_head *buf;
  704. ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
  705. 1, 0, (unsigned long)-1, &ins);
  706. if (ret) {
  707. BUG();
  708. return NULL;
  709. }
  710. BUG_ON(ret);
  711. buf = btrfs_find_create_tree_block(root, ins.objectid);
  712. set_buffer_uptodate(buf);
  713. set_radix_bit(&trans->transaction->dirty_pages, buf->b_page->index);
  714. return buf;
  715. }
  716. static int drop_leaf_ref(struct btrfs_trans_handle *trans,
  717. struct btrfs_root *root, struct buffer_head *cur)
  718. {
  719. struct btrfs_disk_key *key;
  720. struct btrfs_leaf *leaf;
  721. struct btrfs_file_extent_item *fi;
  722. int i;
  723. int nritems;
  724. int ret;
  725. BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
  726. leaf = btrfs_buffer_leaf(cur);
  727. nritems = btrfs_header_nritems(&leaf->header);
  728. for (i = 0; i < nritems; i++) {
  729. key = &leaf->items[i].key;
  730. if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
  731. continue;
  732. fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
  733. if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE)
  734. continue;
  735. /*
  736. * FIXME make sure to insert a trans record that
  737. * repeats the snapshot del on crash
  738. */
  739. ret = btrfs_free_extent(trans, root,
  740. btrfs_file_extent_disk_blocknr(fi),
  741. btrfs_file_extent_disk_num_blocks(fi),
  742. 0);
  743. BUG_ON(ret);
  744. }
  745. return 0;
  746. }
  747. /*
  748. * helper function for drop_snapshot, this walks down the tree dropping ref
  749. * counts as it goes.
  750. */
  751. static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
  752. *root, struct btrfs_path *path, int *level)
  753. {
  754. struct buffer_head *next;
  755. struct buffer_head *cur;
  756. u64 blocknr;
  757. int ret;
  758. u32 refs;
  759. WARN_ON(*level < 0);
  760. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  761. ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
  762. 1, &refs);
  763. BUG_ON(ret);
  764. if (refs > 1)
  765. goto out;
  766. /*
  767. * walk down to the last node level and free all the leaves
  768. */
  769. while(*level >= 0) {
  770. WARN_ON(*level < 0);
  771. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  772. cur = path->nodes[*level];
  773. if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
  774. WARN_ON(1);
  775. if (path->slots[*level] >=
  776. btrfs_header_nritems(btrfs_buffer_header(cur)))
  777. break;
  778. if (*level == 0) {
  779. ret = drop_leaf_ref(trans, root, cur);
  780. BUG_ON(ret);
  781. break;
  782. }
  783. blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
  784. path->slots[*level]);
  785. ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
  786. BUG_ON(ret);
  787. if (refs != 1) {
  788. path->slots[*level]++;
  789. ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
  790. BUG_ON(ret);
  791. continue;
  792. }
  793. next = read_tree_block(root, blocknr);
  794. WARN_ON(*level <= 0);
  795. if (path->nodes[*level-1])
  796. btrfs_block_release(root, path->nodes[*level-1]);
  797. path->nodes[*level-1] = next;
  798. *level = btrfs_header_level(btrfs_buffer_header(next));
  799. path->slots[*level] = 0;
  800. }
  801. out:
  802. WARN_ON(*level < 0);
  803. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  804. ret = btrfs_free_extent(trans, root,
  805. bh_blocknr(path->nodes[*level]), 1, 1);
  806. btrfs_block_release(root, path->nodes[*level]);
  807. path->nodes[*level] = NULL;
  808. *level += 1;
  809. BUG_ON(ret);
  810. return 0;
  811. }
  812. /*
  813. * helper for dropping snapshots. This walks back up the tree in the path
  814. * to find the first node higher up where we haven't yet gone through
  815. * all the slots
  816. */
  817. static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
  818. *root, struct btrfs_path *path, int *level)
  819. {
  820. int i;
  821. int slot;
  822. int ret;
  823. for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
  824. slot = path->slots[i];
  825. if (slot < btrfs_header_nritems(
  826. btrfs_buffer_header(path->nodes[i])) - 1) {
  827. path->slots[i]++;
  828. *level = i;
  829. return 0;
  830. } else {
  831. ret = btrfs_free_extent(trans, root,
  832. bh_blocknr(path->nodes[*level]),
  833. 1, 1);
  834. BUG_ON(ret);
  835. btrfs_block_release(root, path->nodes[*level]);
  836. path->nodes[*level] = NULL;
  837. *level = i + 1;
  838. }
  839. }
  840. return 1;
  841. }
  842. /*
  843. * drop the reference count on the tree rooted at 'snap'. This traverses
  844. * the tree freeing any blocks that have a ref count of zero after being
  845. * decremented.
  846. */
  847. int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
  848. *root, struct buffer_head *snap)
  849. {
  850. int ret = 0;
  851. int wret;
  852. int level;
  853. struct btrfs_path *path;
  854. int i;
  855. int orig_level;
  856. path = btrfs_alloc_path();
  857. BUG_ON(!path);
  858. btrfs_init_path(path);
  859. level = btrfs_header_level(btrfs_buffer_header(snap));
  860. orig_level = level;
  861. path->nodes[level] = snap;
  862. path->slots[level] = 0;
  863. while(1) {
  864. wret = walk_down_tree(trans, root, path, &level);
  865. if (wret > 0)
  866. break;
  867. if (wret < 0)
  868. ret = wret;
  869. wret = walk_up_tree(trans, root, path, &level);
  870. if (wret > 0)
  871. break;
  872. if (wret < 0)
  873. ret = wret;
  874. }
  875. for (i = 0; i <= orig_level; i++) {
  876. if (path->nodes[i]) {
  877. btrfs_block_release(root, path->nodes[i]);
  878. }
  879. }
  880. btrfs_free_path(path);
  881. return ret;
  882. }
  883. int btrfs_free_block_groups(struct btrfs_fs_info *info)
  884. {
  885. int ret;
  886. struct btrfs_block_group_cache *cache[8];
  887. int i;
  888. while(1) {
  889. ret = radix_tree_gang_lookup(&info->block_group_radix,
  890. (void **)cache, 0,
  891. ARRAY_SIZE(cache));
  892. if (!ret)
  893. break;
  894. for (i = 0; i < ret; i++) {
  895. radix_tree_delete(&info->block_group_radix,
  896. cache[i]->key.objectid +
  897. cache[i]->key.offset - 1);
  898. kfree(cache[i]);
  899. }
  900. }
  901. return 0;
  902. }
  903. int btrfs_read_block_groups(struct btrfs_root *root)
  904. {
  905. struct btrfs_path *path;
  906. int ret;
  907. int err = 0;
  908. struct btrfs_block_group_item *bi;
  909. struct btrfs_block_group_cache *cache;
  910. struct btrfs_key key;
  911. struct btrfs_key found_key;
  912. struct btrfs_leaf *leaf;
  913. u64 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE / root->blocksize;
  914. root = root->fs_info->extent_root;
  915. key.objectid = 0;
  916. key.offset = group_size_blocks;
  917. key.flags = 0;
  918. btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
  919. path = btrfs_alloc_path();
  920. if (!path)
  921. return -ENOMEM;
  922. while(1) {
  923. ret = btrfs_search_slot(NULL, root->fs_info->extent_root,
  924. &key, path, 0, 0);
  925. if (ret != 0) {
  926. err = ret;
  927. break;
  928. }
  929. leaf = btrfs_buffer_leaf(path->nodes[0]);
  930. btrfs_disk_key_to_cpu(&found_key,
  931. &leaf->items[path->slots[0]].key);
  932. cache = kmalloc(sizeof(*cache), GFP_NOFS);
  933. if (!cache) {
  934. err = -1;
  935. break;
  936. }
  937. bi = btrfs_item_ptr(leaf, path->slots[0],
  938. struct btrfs_block_group_item);
  939. memcpy(&cache->item, bi, sizeof(*bi));
  940. memcpy(&cache->key, &found_key, sizeof(found_key));
  941. cache->last_alloc = 0;
  942. cache->first_free = 0;
  943. key.objectid = found_key.objectid + found_key.offset;
  944. btrfs_release_path(root, path);
  945. ret = radix_tree_insert(&root->fs_info->block_group_radix,
  946. found_key.objectid +
  947. found_key.offset - 1,
  948. (void *)cache);
  949. BUG_ON(ret);
  950. if (key.objectid >=
  951. btrfs_super_total_blocks(root->fs_info->disk_super))
  952. break;
  953. }
  954. btrfs_free_path(path);
  955. return 0;
  956. }