extent-tree.c 28 KB

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