extent-tree.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  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. int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
  274. btrfs_root *root)
  275. {
  276. unsigned long gang[8];
  277. u64 first = 0;
  278. int ret;
  279. int i;
  280. struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
  281. while(1) {
  282. ret = find_first_radix_bit(pinned_radix, gang,
  283. ARRAY_SIZE(gang));
  284. if (!ret)
  285. break;
  286. if (!first)
  287. first = gang[0];
  288. for (i = 0; i < ret; i++) {
  289. clear_radix_bit(pinned_radix, gang[i]);
  290. }
  291. }
  292. if (root->fs_info->block_group_cache) {
  293. root->fs_info->block_group_cache->last_alloc =
  294. root->fs_info->block_group_cache->first_free;
  295. }
  296. return 0;
  297. }
  298. static int finish_current_insert(struct btrfs_trans_handle *trans, struct
  299. btrfs_root *extent_root)
  300. {
  301. struct btrfs_key ins;
  302. struct btrfs_extent_item extent_item;
  303. int i;
  304. int ret;
  305. u64 super_blocks_used;
  306. struct btrfs_fs_info *info = extent_root->fs_info;
  307. btrfs_set_extent_refs(&extent_item, 1);
  308. ins.offset = 1;
  309. ins.flags = 0;
  310. btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
  311. btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid);
  312. for (i = 0; i < extent_root->fs_info->extent_tree_insert_nr; i++) {
  313. ins.objectid = extent_root->fs_info->extent_tree_insert[i];
  314. super_blocks_used = btrfs_super_blocks_used(info->disk_super);
  315. btrfs_set_super_blocks_used(info->disk_super,
  316. super_blocks_used + 1);
  317. ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
  318. sizeof(extent_item));
  319. BUG_ON(ret);
  320. }
  321. extent_root->fs_info->extent_tree_insert_nr = 0;
  322. extent_root->fs_info->extent_tree_prealloc_nr = 0;
  323. return 0;
  324. }
  325. static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
  326. {
  327. int err;
  328. struct btrfs_header *header;
  329. struct buffer_head *bh;
  330. if (!pending) {
  331. bh = btrfs_find_tree_block(root, blocknr);
  332. if (bh) {
  333. if (buffer_uptodate(bh)) {
  334. u64 transid =
  335. root->fs_info->running_transaction->transid;
  336. header = btrfs_buffer_header(bh);
  337. if (btrfs_header_generation(header) ==
  338. transid) {
  339. btrfs_block_release(root, bh);
  340. return 0;
  341. }
  342. }
  343. btrfs_block_release(root, bh);
  344. }
  345. err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
  346. } else {
  347. err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
  348. }
  349. BUG_ON(err);
  350. return 0;
  351. }
  352. /*
  353. * remove an extent from the root, returns 0 on success
  354. */
  355. static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
  356. *root, u64 blocknr, u64 num_blocks, int pin)
  357. {
  358. struct btrfs_path *path;
  359. struct btrfs_key key;
  360. struct btrfs_fs_info *info = root->fs_info;
  361. struct btrfs_root *extent_root = info->extent_root;
  362. int ret;
  363. struct btrfs_extent_item *ei;
  364. struct btrfs_key ins;
  365. u32 refs;
  366. key.objectid = blocknr;
  367. key.flags = 0;
  368. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  369. key.offset = num_blocks;
  370. find_free_extent(trans, root, 0, 0, (u64)-1, &ins);
  371. path = btrfs_alloc_path();
  372. BUG_ON(!path);
  373. btrfs_init_path(path);
  374. ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
  375. if (ret) {
  376. printk("failed to find %Lu\n", key.objectid);
  377. btrfs_print_tree(extent_root, extent_root->node);
  378. printk("failed to find %Lu\n", key.objectid);
  379. BUG();
  380. }
  381. ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
  382. struct btrfs_extent_item);
  383. BUG_ON(ei->refs == 0);
  384. refs = btrfs_extent_refs(ei) - 1;
  385. btrfs_set_extent_refs(ei, refs);
  386. btrfs_mark_buffer_dirty(path->nodes[0]);
  387. if (refs == 0) {
  388. u64 super_blocks_used;
  389. if (pin) {
  390. ret = pin_down_block(root, blocknr, 0);
  391. BUG_ON(ret);
  392. }
  393. super_blocks_used = btrfs_super_blocks_used(info->disk_super);
  394. btrfs_set_super_blocks_used(info->disk_super,
  395. super_blocks_used - num_blocks);
  396. ret = btrfs_del_item(trans, extent_root, path);
  397. if (ret)
  398. BUG();
  399. ret = update_block_group(trans, root, blocknr, num_blocks, 0);
  400. BUG_ON(ret);
  401. }
  402. btrfs_release_path(extent_root, path);
  403. btrfs_free_path(path);
  404. finish_current_insert(trans, extent_root);
  405. return ret;
  406. }
  407. /*
  408. * find all the blocks marked as pending in the radix tree and remove
  409. * them from the extent map
  410. */
  411. static int del_pending_extents(struct btrfs_trans_handle *trans, struct
  412. btrfs_root *extent_root)
  413. {
  414. int ret;
  415. int wret;
  416. int err = 0;
  417. unsigned long gang[4];
  418. int i;
  419. struct radix_tree_root *pending_radix;
  420. struct radix_tree_root *pinned_radix;
  421. pending_radix = &extent_root->fs_info->pending_del_radix;
  422. pinned_radix = &extent_root->fs_info->pinned_radix;
  423. while(1) {
  424. ret = find_first_radix_bit(pending_radix, gang,
  425. ARRAY_SIZE(gang));
  426. if (!ret)
  427. break;
  428. for (i = 0; i < ret; i++) {
  429. wret = set_radix_bit(pinned_radix, gang[i]);
  430. BUG_ON(wret);
  431. wret = clear_radix_bit(pending_radix, gang[i]);
  432. BUG_ON(wret);
  433. wret = __free_extent(trans, extent_root,
  434. gang[i], 1, 0);
  435. if (wret)
  436. err = wret;
  437. }
  438. }
  439. return err;
  440. }
  441. /*
  442. * remove an extent from the root, returns 0 on success
  443. */
  444. int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
  445. *root, u64 blocknr, u64 num_blocks, int pin)
  446. {
  447. struct btrfs_root *extent_root = root->fs_info->extent_root;
  448. int pending_ret;
  449. int ret;
  450. if (root == extent_root) {
  451. pin_down_block(root, blocknr, 1);
  452. return 0;
  453. }
  454. ret = __free_extent(trans, root, blocknr, num_blocks, pin);
  455. pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
  456. return ret ? ret : pending_ret;
  457. }
  458. /*
  459. * walks the btree of allocated extents and find a hole of a given size.
  460. * The key ins is changed to record the hole:
  461. * ins->objectid == block start
  462. * ins->flags = BTRFS_EXTENT_ITEM_KEY
  463. * ins->offset == number of blocks
  464. * Any available blocks before search_start are skipped.
  465. */
  466. static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
  467. *orig_root, u64 num_blocks, u64 search_start, u64
  468. search_end, struct btrfs_key *ins)
  469. {
  470. struct btrfs_path *path;
  471. struct btrfs_key key;
  472. int ret;
  473. u64 hole_size = 0;
  474. int slot = 0;
  475. u64 last_block = 0;
  476. u64 test_block;
  477. int start_found;
  478. struct btrfs_leaf *l;
  479. struct btrfs_root * root = orig_root->fs_info->extent_root;
  480. struct btrfs_fs_info *info = root->fs_info;
  481. int total_needed = num_blocks;
  482. int total_found = 0;
  483. int fill_prealloc = 0;
  484. int level;
  485. path = btrfs_alloc_path();
  486. ins->flags = 0;
  487. btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
  488. level = btrfs_header_level(btrfs_buffer_header(root->node));
  489. if (num_blocks == 0) {
  490. fill_prealloc = 1;
  491. num_blocks = 1;
  492. total_needed = min(level + 2, BTRFS_MAX_LEVEL) * 3;
  493. }
  494. find_search_start(root, 0);
  495. if (info->block_group_cache &&
  496. info->block_group_cache->last_alloc > search_start)
  497. search_start = info->block_group_cache->last_alloc;
  498. check_failed:
  499. btrfs_init_path(path);
  500. ins->objectid = search_start;
  501. ins->offset = 0;
  502. start_found = 0;
  503. ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
  504. if (ret < 0)
  505. goto error;
  506. if (path->slots[0] > 0)
  507. path->slots[0]--;
  508. while (1) {
  509. l = btrfs_buffer_leaf(path->nodes[0]);
  510. slot = path->slots[0];
  511. if (slot >= btrfs_header_nritems(&l->header)) {
  512. if (fill_prealloc) {
  513. info->extent_tree_prealloc_nr = 0;
  514. total_found = 0;
  515. }
  516. ret = btrfs_next_leaf(root, path);
  517. if (ret == 0)
  518. continue;
  519. if (ret < 0)
  520. goto error;
  521. if (!start_found) {
  522. ins->objectid = search_start;
  523. ins->offset = (u64)-1 - search_start;
  524. start_found = 1;
  525. goto check_pending;
  526. }
  527. ins->objectid = last_block > search_start ?
  528. last_block : search_start;
  529. ins->offset = (u64)-1 - ins->objectid;
  530. goto check_pending;
  531. }
  532. btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
  533. if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
  534. goto next;
  535. if (key.objectid >= search_start) {
  536. if (start_found) {
  537. if (last_block < search_start)
  538. last_block = search_start;
  539. hole_size = key.objectid - last_block;
  540. if (hole_size >= num_blocks) {
  541. ins->objectid = last_block;
  542. ins->offset = hole_size;
  543. goto check_pending;
  544. }
  545. }
  546. }
  547. start_found = 1;
  548. last_block = key.objectid + key.offset;
  549. next:
  550. path->slots[0]++;
  551. }
  552. // FIXME -ENOSPC
  553. check_pending:
  554. /* we have to make sure we didn't find an extent that has already
  555. * been allocated by the map tree or the original allocation
  556. */
  557. btrfs_release_path(root, path);
  558. BUG_ON(ins->objectid < search_start);
  559. for (test_block = ins->objectid;
  560. test_block < ins->objectid + num_blocks; test_block++) {
  561. if (test_radix_bit(&info->pinned_radix, test_block)) {
  562. search_start = test_block + 1;
  563. goto check_failed;
  564. }
  565. }
  566. if (!fill_prealloc && info->extent_tree_insert_nr) {
  567. u64 last =
  568. info->extent_tree_insert[info->extent_tree_insert_nr - 1];
  569. if (ins->objectid + num_blocks >
  570. info->extent_tree_insert[0] &&
  571. ins->objectid <= last) {
  572. search_start = last + 1;
  573. WARN_ON(1);
  574. goto check_failed;
  575. }
  576. }
  577. if (!fill_prealloc && info->extent_tree_prealloc_nr) {
  578. u64 first =
  579. info->extent_tree_prealloc[info->extent_tree_prealloc_nr - 1];
  580. if (ins->objectid + num_blocks > first &&
  581. ins->objectid <= info->extent_tree_prealloc[0]) {
  582. search_start = info->extent_tree_prealloc[0] + 1;
  583. WARN_ON(1);
  584. goto check_failed;
  585. }
  586. }
  587. if (fill_prealloc) {
  588. int nr;
  589. test_block = ins->objectid;
  590. while(test_block < ins->objectid + ins->offset &&
  591. total_found < total_needed) {
  592. nr = total_needed - total_found - 1;
  593. BUG_ON(nr < 0);
  594. info->extent_tree_prealloc[nr] = test_block;
  595. total_found++;
  596. test_block++;
  597. }
  598. if (total_found < total_needed) {
  599. search_start = test_block;
  600. goto check_failed;
  601. }
  602. info->extent_tree_prealloc_nr = total_found;
  603. }
  604. ret = radix_tree_gang_lookup(&info->block_group_radix,
  605. (void **)&info->block_group_cache,
  606. ins->objectid, 1);
  607. if (ret) {
  608. info->block_group_cache->last_alloc = ins->objectid;
  609. }
  610. ins->offset = num_blocks;
  611. btrfs_free_path(path);
  612. return 0;
  613. error:
  614. btrfs_release_path(root, path);
  615. btrfs_free_path(path);
  616. return ret;
  617. }
  618. /*
  619. * finds a free extent and does all the dirty work required for allocation
  620. * returns the key for the extent through ins, and a tree buffer for
  621. * the first block of the extent through buf.
  622. *
  623. * returns 0 if everything worked, non-zero otherwise.
  624. */
  625. int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
  626. struct btrfs_root *root, u64 owner,
  627. u64 num_blocks, u64 search_start,
  628. u64 search_end, struct btrfs_key *ins)
  629. {
  630. int ret;
  631. int pending_ret;
  632. u64 super_blocks_used;
  633. struct btrfs_fs_info *info = root->fs_info;
  634. struct btrfs_root *extent_root = info->extent_root;
  635. struct btrfs_extent_item extent_item;
  636. struct btrfs_key prealloc_key;
  637. btrfs_set_extent_refs(&extent_item, 1);
  638. btrfs_set_extent_owner(&extent_item, owner);
  639. if (root == extent_root) {
  640. int nr;
  641. BUG_ON(info->extent_tree_prealloc_nr == 0);
  642. BUG_ON(num_blocks != 1);
  643. ins->offset = 1;
  644. info->extent_tree_prealloc_nr--;
  645. nr = info->extent_tree_prealloc_nr;
  646. ins->objectid = info->extent_tree_prealloc[nr];
  647. info->extent_tree_insert[info->extent_tree_insert_nr++] =
  648. ins->objectid;
  649. ret = update_block_group(trans, root,
  650. ins->objectid, ins->offset, 1);
  651. BUG_ON(ret);
  652. return 0;
  653. }
  654. /* do the real allocation */
  655. ret = find_free_extent(trans, root, num_blocks, search_start,
  656. search_end, ins);
  657. if (ret)
  658. return ret;
  659. /* then do prealloc for the extent tree */
  660. ret = find_free_extent(trans, root, 0, ins->objectid + ins->offset,
  661. search_end, &prealloc_key);
  662. if (ret)
  663. return ret;
  664. super_blocks_used = btrfs_super_blocks_used(info->disk_super);
  665. btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
  666. num_blocks);
  667. ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
  668. sizeof(extent_item));
  669. finish_current_insert(trans, extent_root);
  670. pending_ret = del_pending_extents(trans, extent_root);
  671. if (ret)
  672. return ret;
  673. if (pending_ret)
  674. return pending_ret;
  675. ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
  676. return 0;
  677. }
  678. /*
  679. * helper function to allocate a block for a given tree
  680. * returns the tree buffer or NULL.
  681. */
  682. struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
  683. struct btrfs_root *root)
  684. {
  685. struct btrfs_key ins;
  686. int ret;
  687. struct buffer_head *buf;
  688. ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
  689. 1, 0, (unsigned long)-1, &ins);
  690. if (ret) {
  691. BUG();
  692. return NULL;
  693. }
  694. BUG_ON(ret);
  695. buf = btrfs_find_create_tree_block(root, ins.objectid);
  696. set_buffer_uptodate(buf);
  697. return buf;
  698. }
  699. static int drop_leaf_ref(struct btrfs_trans_handle *trans,
  700. struct btrfs_root *root, struct buffer_head *cur)
  701. {
  702. struct btrfs_disk_key *key;
  703. struct btrfs_leaf *leaf;
  704. struct btrfs_file_extent_item *fi;
  705. int i;
  706. int nritems;
  707. int ret;
  708. BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
  709. leaf = btrfs_buffer_leaf(cur);
  710. nritems = btrfs_header_nritems(&leaf->header);
  711. for (i = 0; i < nritems; i++) {
  712. key = &leaf->items[i].key;
  713. if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
  714. continue;
  715. fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
  716. if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE)
  717. continue;
  718. /*
  719. * FIXME make sure to insert a trans record that
  720. * repeats the snapshot del on crash
  721. */
  722. ret = btrfs_free_extent(trans, root,
  723. btrfs_file_extent_disk_blocknr(fi),
  724. btrfs_file_extent_disk_num_blocks(fi),
  725. 0);
  726. BUG_ON(ret);
  727. }
  728. return 0;
  729. }
  730. /*
  731. * helper function for drop_snapshot, this walks down the tree dropping ref
  732. * counts as it goes.
  733. */
  734. static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
  735. *root, struct btrfs_path *path, int *level)
  736. {
  737. struct buffer_head *next;
  738. struct buffer_head *cur;
  739. u64 blocknr;
  740. int ret;
  741. u32 refs;
  742. WARN_ON(*level < 0);
  743. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  744. ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
  745. 1, &refs);
  746. BUG_ON(ret);
  747. if (refs > 1)
  748. goto out;
  749. /*
  750. * walk down to the last node level and free all the leaves
  751. */
  752. while(*level >= 0) {
  753. WARN_ON(*level < 0);
  754. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  755. cur = path->nodes[*level];
  756. if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
  757. WARN_ON(1);
  758. if (path->slots[*level] >=
  759. btrfs_header_nritems(btrfs_buffer_header(cur)))
  760. break;
  761. if (*level == 0) {
  762. ret = drop_leaf_ref(trans, root, cur);
  763. BUG_ON(ret);
  764. break;
  765. }
  766. blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
  767. path->slots[*level]);
  768. ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
  769. BUG_ON(ret);
  770. if (refs != 1) {
  771. path->slots[*level]++;
  772. ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
  773. BUG_ON(ret);
  774. continue;
  775. }
  776. next = read_tree_block(root, blocknr);
  777. WARN_ON(*level <= 0);
  778. if (path->nodes[*level-1])
  779. btrfs_block_release(root, path->nodes[*level-1]);
  780. path->nodes[*level-1] = next;
  781. *level = btrfs_header_level(btrfs_buffer_header(next));
  782. path->slots[*level] = 0;
  783. }
  784. out:
  785. WARN_ON(*level < 0);
  786. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  787. ret = btrfs_free_extent(trans, root,
  788. bh_blocknr(path->nodes[*level]), 1, 1);
  789. btrfs_block_release(root, path->nodes[*level]);
  790. path->nodes[*level] = NULL;
  791. *level += 1;
  792. BUG_ON(ret);
  793. return 0;
  794. }
  795. /*
  796. * helper for dropping snapshots. This walks back up the tree in the path
  797. * to find the first node higher up where we haven't yet gone through
  798. * all the slots
  799. */
  800. static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
  801. *root, struct btrfs_path *path, int *level)
  802. {
  803. int i;
  804. int slot;
  805. int ret;
  806. for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
  807. slot = path->slots[i];
  808. if (slot < btrfs_header_nritems(
  809. btrfs_buffer_header(path->nodes[i])) - 1) {
  810. path->slots[i]++;
  811. *level = i;
  812. return 0;
  813. } else {
  814. ret = btrfs_free_extent(trans, root,
  815. bh_blocknr(path->nodes[*level]),
  816. 1, 1);
  817. BUG_ON(ret);
  818. btrfs_block_release(root, path->nodes[*level]);
  819. path->nodes[*level] = NULL;
  820. *level = i + 1;
  821. }
  822. }
  823. return 1;
  824. }
  825. /*
  826. * drop the reference count on the tree rooted at 'snap'. This traverses
  827. * the tree freeing any blocks that have a ref count of zero after being
  828. * decremented.
  829. */
  830. int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
  831. *root, struct buffer_head *snap)
  832. {
  833. int ret = 0;
  834. int wret;
  835. int level;
  836. struct btrfs_path *path;
  837. int i;
  838. int orig_level;
  839. path = btrfs_alloc_path();
  840. BUG_ON(!path);
  841. btrfs_init_path(path);
  842. level = btrfs_header_level(btrfs_buffer_header(snap));
  843. orig_level = level;
  844. path->nodes[level] = snap;
  845. path->slots[level] = 0;
  846. while(1) {
  847. wret = walk_down_tree(trans, root, path, &level);
  848. if (wret > 0)
  849. break;
  850. if (wret < 0)
  851. ret = wret;
  852. wret = walk_up_tree(trans, root, path, &level);
  853. if (wret > 0)
  854. break;
  855. if (wret < 0)
  856. ret = wret;
  857. }
  858. for (i = 0; i <= orig_level; i++) {
  859. if (path->nodes[i]) {
  860. btrfs_block_release(root, path->nodes[i]);
  861. }
  862. }
  863. btrfs_free_path(path);
  864. return ret;
  865. }
  866. int btrfs_free_block_groups(struct btrfs_fs_info *info)
  867. {
  868. int ret;
  869. struct btrfs_block_group_cache *cache[8];
  870. int i;
  871. while(1) {
  872. ret = radix_tree_gang_lookup(&info->block_group_radix,
  873. (void **)cache, 0,
  874. ARRAY_SIZE(cache));
  875. if (!ret)
  876. break;
  877. for (i = 0; i < ret; i++) {
  878. radix_tree_delete(&info->block_group_radix,
  879. cache[i]->key.objectid +
  880. cache[i]->key.offset - 1);
  881. kfree(cache[i]);
  882. }
  883. }
  884. return 0;
  885. }
  886. int btrfs_read_block_groups(struct btrfs_root *root)
  887. {
  888. struct btrfs_path *path;
  889. int ret;
  890. int err = 0;
  891. struct btrfs_block_group_item *bi;
  892. struct btrfs_block_group_cache *cache;
  893. struct btrfs_key key;
  894. struct btrfs_key found_key;
  895. struct btrfs_leaf *leaf;
  896. u64 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE / root->blocksize;
  897. root = root->fs_info->extent_root;
  898. key.objectid = 0;
  899. key.offset = group_size_blocks;
  900. key.flags = 0;
  901. btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
  902. path = btrfs_alloc_path();
  903. if (!path)
  904. return -ENOMEM;
  905. while(1) {
  906. ret = btrfs_search_slot(NULL, root->fs_info->extent_root,
  907. &key, path, 0, 0);
  908. if (ret != 0) {
  909. err = ret;
  910. break;
  911. }
  912. leaf = btrfs_buffer_leaf(path->nodes[0]);
  913. btrfs_disk_key_to_cpu(&found_key,
  914. &leaf->items[path->slots[0]].key);
  915. cache = kmalloc(sizeof(*cache), GFP_NOFS);
  916. if (!cache) {
  917. err = -1;
  918. break;
  919. }
  920. bi = btrfs_item_ptr(leaf, path->slots[0],
  921. struct btrfs_block_group_item);
  922. memcpy(&cache->item, bi, sizeof(*bi));
  923. memcpy(&cache->key, &found_key, sizeof(found_key));
  924. cache->last_alloc = 0;
  925. cache->first_free = 0;
  926. key.objectid = found_key.objectid + found_key.offset;
  927. btrfs_release_path(root, path);
  928. ret = radix_tree_insert(&root->fs_info->block_group_radix,
  929. found_key.objectid +
  930. found_key.offset - 1,
  931. (void *)cache);
  932. BUG_ON(ret);
  933. if (key.objectid >=
  934. btrfs_super_total_blocks(root->fs_info->disk_super))
  935. break;
  936. }
  937. btrfs_free_path(path);
  938. return 0;
  939. }