extent-tree.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549
  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, int data);
  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 void reada_extent_leaves(struct btrfs_root *root,
  14. struct btrfs_path *path, u64 limit)
  15. {
  16. struct btrfs_node *node;
  17. int i;
  18. int nritems;
  19. u64 item_objectid;
  20. u64 blocknr;
  21. int slot;
  22. int ret;
  23. if (!path->nodes[1])
  24. return;
  25. node = btrfs_buffer_node(path->nodes[1]);
  26. slot = path->slots[1] + 1;
  27. nritems = btrfs_header_nritems(&node->header);
  28. for (i = slot; i < nritems && i < slot + 8; i++) {
  29. item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key);
  30. if (item_objectid > limit)
  31. break;
  32. blocknr = btrfs_node_blockptr(node, i);
  33. ret = readahead_tree_block(root, blocknr);
  34. if (ret)
  35. break;
  36. }
  37. }
  38. static int cache_block_group(struct btrfs_root *root,
  39. struct btrfs_block_group_cache *block_group)
  40. {
  41. struct btrfs_path *path;
  42. int ret;
  43. struct btrfs_key key;
  44. struct btrfs_leaf *leaf;
  45. struct radix_tree_root *extent_radix;
  46. int slot;
  47. u64 i;
  48. u64 last = 0;
  49. u64 hole_size;
  50. u64 limit;
  51. int found = 0;
  52. root = root->fs_info->extent_root;
  53. extent_radix = &root->fs_info->extent_map_radix;
  54. if (block_group->cached)
  55. return 0;
  56. if (block_group->data)
  57. return 0;
  58. path = btrfs_alloc_path();
  59. if (!path)
  60. return -ENOMEM;
  61. printk("cache block group %Lu\n", block_group->key.objectid);
  62. key.objectid = block_group->key.objectid;
  63. key.flags = 0;
  64. key.offset = 0;
  65. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  66. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  67. if (ret < 0)
  68. return ret;
  69. if (ret && path->slots[0] > 0)
  70. path->slots[0]--;
  71. limit = block_group->key.objectid + block_group->key.offset;
  72. reada_extent_leaves(root, path, limit);
  73. while(1) {
  74. leaf = btrfs_buffer_leaf(path->nodes[0]);
  75. slot = path->slots[0];
  76. if (slot >= btrfs_header_nritems(&leaf->header)) {
  77. reada_extent_leaves(root, path, limit);
  78. ret = btrfs_next_leaf(root, path);
  79. if (ret == 0) {
  80. continue;
  81. } else {
  82. if (found) {
  83. hole_size = block_group->key.objectid +
  84. block_group->key.offset - last;
  85. } else {
  86. last = block_group->key.objectid;
  87. hole_size = block_group->key.offset;
  88. }
  89. for (i = 0; i < hole_size; i++) {
  90. set_radix_bit(extent_radix,
  91. last + i);
  92. }
  93. break;
  94. }
  95. }
  96. btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
  97. if (key.objectid >= block_group->key.objectid +
  98. block_group->key.offset) {
  99. if (found) {
  100. hole_size = block_group->key.objectid +
  101. block_group->key.offset - last;
  102. } else {
  103. last = block_group->key.objectid;
  104. hole_size = block_group->key.offset;
  105. }
  106. for (i = 0; i < hole_size; i++) {
  107. set_radix_bit(extent_radix, last + i);
  108. }
  109. break;
  110. }
  111. if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
  112. if (!found) {
  113. last = key.objectid + key.offset;
  114. found = 1;
  115. } else {
  116. hole_size = key.objectid - last;
  117. for (i = 0; i < hole_size; i++) {
  118. set_radix_bit(extent_radix, last + i);
  119. }
  120. last = key.objectid + key.offset;
  121. }
  122. }
  123. path->slots[0]++;
  124. }
  125. block_group->cached = 1;
  126. btrfs_free_path(path);
  127. return 0;
  128. }
  129. static struct btrfs_block_group_cache *lookup_block_group(struct
  130. btrfs_fs_info *info,
  131. u64 blocknr)
  132. {
  133. struct btrfs_block_group_cache *block_group;
  134. int ret;
  135. ret = radix_tree_gang_lookup(&info->block_group_radix,
  136. (void **)&block_group,
  137. blocknr, 1);
  138. if (ret) {
  139. if (block_group->key.objectid <= blocknr && blocknr <=
  140. block_group->key.objectid + block_group->key.offset)
  141. return block_group;
  142. }
  143. ret = radix_tree_gang_lookup(&info->block_group_data_radix,
  144. (void **)&block_group,
  145. blocknr, 1);
  146. if (ret) {
  147. if (block_group->key.objectid <= blocknr && blocknr <=
  148. block_group->key.objectid + block_group->key.offset)
  149. return block_group;
  150. }
  151. WARN_ON(1);
  152. printk("lookup_block_group fails for blocknr %Lu\n", blocknr);
  153. printk("last ret was %d\n", ret);
  154. if (ret) {
  155. printk("last block group was %Lu %Lu\n", block_group->key.objectid, block_group->key.offset);
  156. }
  157. return NULL;
  158. }
  159. static u64 leaf_range(struct btrfs_root *root)
  160. {
  161. u64 size = BTRFS_LEAF_DATA_SIZE(root);
  162. size = size / (sizeof(struct btrfs_extent_item) +
  163. sizeof(struct btrfs_item));
  164. return size;
  165. }
  166. static u64 find_search_start(struct btrfs_root *root,
  167. struct btrfs_block_group_cache **cache_ret,
  168. u64 search_start, int num)
  169. {
  170. unsigned long gang[8];
  171. int ret;
  172. struct btrfs_block_group_cache *cache = *cache_ret;
  173. u64 last = max(search_start, cache->key.objectid);
  174. if (cache->data)
  175. goto out;
  176. if (num > 1) {
  177. last = max(last, cache->last_prealloc);
  178. }
  179. again:
  180. cache_block_group(root, cache);
  181. while(1) {
  182. ret = find_first_radix_bit(&root->fs_info->extent_map_radix,
  183. gang, last, ARRAY_SIZE(gang));
  184. if (!ret)
  185. goto out;
  186. last = gang[ret-1] + 1;
  187. if (num > 1) {
  188. if (ret != ARRAY_SIZE(gang)) {
  189. goto new_group;
  190. }
  191. if (gang[ret-1] - gang[0] > leaf_range(root)) {
  192. continue;
  193. }
  194. }
  195. if (gang[0] >= cache->key.objectid + cache->key.offset) {
  196. goto new_group;
  197. }
  198. return gang[0];
  199. }
  200. out:
  201. return max(cache->last_alloc, search_start);
  202. new_group:
  203. cache = lookup_block_group(root->fs_info, last + cache->key.offset - 1);
  204. if (!cache) {
  205. return max((*cache_ret)->last_alloc, search_start);
  206. }
  207. cache = btrfs_find_block_group(root, cache,
  208. last + cache->key.offset - 1, 0, 0);
  209. *cache_ret = cache;
  210. goto again;
  211. }
  212. struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
  213. struct btrfs_block_group_cache
  214. *hint, u64 search_start,
  215. int data, int owner)
  216. {
  217. struct btrfs_block_group_cache *cache[8];
  218. struct btrfs_block_group_cache *found_group = NULL;
  219. struct btrfs_fs_info *info = root->fs_info;
  220. struct radix_tree_root *radix;
  221. u64 used;
  222. u64 last = 0;
  223. u64 hint_last;
  224. int i;
  225. int ret;
  226. int full_search = 0;
  227. int factor = 8;
  228. if (!owner)
  229. factor = 5;
  230. if (data)
  231. radix = &info->block_group_data_radix;
  232. else
  233. radix = &info->block_group_radix;
  234. if (search_start) {
  235. struct btrfs_block_group_cache *shint;
  236. shint = lookup_block_group(info, search_start);
  237. if (shint->data == data) {
  238. used = btrfs_block_group_used(&shint->item);
  239. if (used + shint->pinned <
  240. (shint->key.offset * factor) / 10) {
  241. return shint;
  242. }
  243. }
  244. }
  245. if (hint && hint->data == data) {
  246. used = btrfs_block_group_used(&hint->item);
  247. if (used + hint->pinned < (hint->key.offset * factor) / 10) {
  248. return hint;
  249. }
  250. if (used >= (hint->key.offset * 8) / 10) {
  251. radix_tree_tag_clear(radix,
  252. hint->key.objectid +
  253. hint->key.offset - 1,
  254. BTRFS_BLOCK_GROUP_AVAIL);
  255. }
  256. last = hint->key.offset * 3;
  257. if (hint->key.objectid >= last)
  258. last = max(search_start + hint->key.offset - 1,
  259. hint->key.objectid - last);
  260. else
  261. last = hint->key.objectid + hint->key.offset;
  262. hint_last = last;
  263. } else {
  264. if (hint)
  265. hint_last = max(hint->key.objectid, search_start);
  266. else
  267. hint_last = search_start;
  268. last = hint_last;
  269. }
  270. while(1) {
  271. ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
  272. last, ARRAY_SIZE(cache),
  273. BTRFS_BLOCK_GROUP_AVAIL);
  274. if (!ret)
  275. break;
  276. for (i = 0; i < ret; i++) {
  277. last = cache[i]->key.objectid +
  278. cache[i]->key.offset;
  279. used = btrfs_block_group_used(&cache[i]->item);
  280. if (used + cache[i]->pinned <
  281. (cache[i]->key.offset * factor) / 10) {
  282. found_group = cache[i];
  283. goto found;
  284. }
  285. if (used >= (cache[i]->key.offset * 8) / 10) {
  286. radix_tree_tag_clear(radix,
  287. cache[i]->key.objectid +
  288. cache[i]->key.offset - 1,
  289. BTRFS_BLOCK_GROUP_AVAIL);
  290. }
  291. }
  292. cond_resched();
  293. }
  294. last = hint_last;
  295. again:
  296. while(1) {
  297. ret = radix_tree_gang_lookup(radix, (void **)cache,
  298. last, ARRAY_SIZE(cache));
  299. if (!ret)
  300. break;
  301. for (i = 0; i < ret; i++) {
  302. last = cache[i]->key.objectid +
  303. cache[i]->key.offset;
  304. used = btrfs_block_group_used(&cache[i]->item);
  305. if (used + cache[i]->pinned < cache[i]->key.offset) {
  306. found_group = cache[i];
  307. goto found;
  308. }
  309. if (used >= cache[i]->key.offset) {
  310. radix_tree_tag_clear(radix,
  311. cache[i]->key.objectid +
  312. cache[i]->key.offset - 1,
  313. BTRFS_BLOCK_GROUP_AVAIL);
  314. }
  315. }
  316. cond_resched();
  317. }
  318. if (!full_search) {
  319. printk("find block group doing full search data %d start %Lu\n", data, search_start);
  320. last = search_start;
  321. full_search = 1;
  322. goto again;
  323. }
  324. if (!found_group) {
  325. printk("find block group bailing to zero data %d\n", data);
  326. ret = radix_tree_gang_lookup(radix,
  327. (void **)&found_group, 0, 1);
  328. BUG_ON(ret != 1);
  329. }
  330. found:
  331. return found_group;
  332. }
  333. int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
  334. struct btrfs_root *root,
  335. u64 blocknr, u64 num_blocks)
  336. {
  337. struct btrfs_path *path;
  338. int ret;
  339. struct btrfs_key key;
  340. struct btrfs_leaf *l;
  341. struct btrfs_extent_item *item;
  342. struct btrfs_key ins;
  343. u32 refs;
  344. find_free_extent(trans, root->fs_info->extent_root, 0, 0, (u64)-1,
  345. &ins, 0);
  346. path = btrfs_alloc_path();
  347. BUG_ON(!path);
  348. btrfs_init_path(path);
  349. key.objectid = blocknr;
  350. key.flags = 0;
  351. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  352. key.offset = num_blocks;
  353. ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
  354. 0, 1);
  355. if (ret != 0) {
  356. printk("can't find block %Lu %Lu\n", blocknr, num_blocks);
  357. BUG();
  358. }
  359. BUG_ON(ret != 0);
  360. l = btrfs_buffer_leaf(path->nodes[0]);
  361. item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
  362. refs = btrfs_extent_refs(item);
  363. btrfs_set_extent_refs(item, refs + 1);
  364. btrfs_mark_buffer_dirty(path->nodes[0]);
  365. btrfs_release_path(root->fs_info->extent_root, path);
  366. btrfs_free_path(path);
  367. finish_current_insert(trans, root->fs_info->extent_root);
  368. del_pending_extents(trans, root->fs_info->extent_root);
  369. return 0;
  370. }
  371. static int lookup_extent_ref(struct btrfs_trans_handle *trans,
  372. struct btrfs_root *root, u64 blocknr,
  373. u64 num_blocks, u32 *refs)
  374. {
  375. struct btrfs_path *path;
  376. int ret;
  377. struct btrfs_key key;
  378. struct btrfs_leaf *l;
  379. struct btrfs_extent_item *item;
  380. path = btrfs_alloc_path();
  381. btrfs_init_path(path);
  382. key.objectid = blocknr;
  383. key.offset = num_blocks;
  384. key.flags = 0;
  385. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  386. ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
  387. 0, 0);
  388. if (ret != 0)
  389. BUG();
  390. l = btrfs_buffer_leaf(path->nodes[0]);
  391. item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
  392. *refs = btrfs_extent_refs(item);
  393. btrfs_release_path(root->fs_info->extent_root, path);
  394. btrfs_free_path(path);
  395. return 0;
  396. }
  397. int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
  398. struct btrfs_root *root)
  399. {
  400. return btrfs_inc_extent_ref(trans, root, bh_blocknr(root->node), 1);
  401. }
  402. int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  403. struct buffer_head *buf)
  404. {
  405. u64 blocknr;
  406. struct btrfs_node *buf_node;
  407. struct btrfs_leaf *buf_leaf;
  408. struct btrfs_disk_key *key;
  409. struct btrfs_file_extent_item *fi;
  410. int i;
  411. int leaf;
  412. int ret;
  413. if (!root->ref_cows)
  414. return 0;
  415. buf_node = btrfs_buffer_node(buf);
  416. leaf = btrfs_is_leaf(buf_node);
  417. buf_leaf = btrfs_buffer_leaf(buf);
  418. for (i = 0; i < btrfs_header_nritems(&buf_node->header); i++) {
  419. if (leaf) {
  420. u64 disk_blocknr;
  421. key = &buf_leaf->items[i].key;
  422. if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
  423. continue;
  424. fi = btrfs_item_ptr(buf_leaf, i,
  425. struct btrfs_file_extent_item);
  426. if (btrfs_file_extent_type(fi) ==
  427. BTRFS_FILE_EXTENT_INLINE)
  428. continue;
  429. disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
  430. if (disk_blocknr == 0)
  431. continue;
  432. ret = btrfs_inc_extent_ref(trans, root, disk_blocknr,
  433. btrfs_file_extent_disk_num_blocks(fi));
  434. BUG_ON(ret);
  435. } else {
  436. blocknr = btrfs_node_blockptr(buf_node, i);
  437. ret = btrfs_inc_extent_ref(trans, root, blocknr, 1);
  438. BUG_ON(ret);
  439. }
  440. }
  441. return 0;
  442. }
  443. static int write_one_cache_group(struct btrfs_trans_handle *trans,
  444. struct btrfs_root *root,
  445. struct btrfs_path *path,
  446. struct btrfs_block_group_cache *cache)
  447. {
  448. int ret;
  449. int pending_ret;
  450. struct btrfs_root *extent_root = root->fs_info->extent_root;
  451. struct btrfs_block_group_item *bi;
  452. struct btrfs_key ins;
  453. find_free_extent(trans, extent_root, 0, 0, (u64)-1, &ins, 0);
  454. ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
  455. BUG_ON(ret);
  456. bi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
  457. struct btrfs_block_group_item);
  458. memcpy(bi, &cache->item, sizeof(*bi));
  459. mark_buffer_dirty(path->nodes[0]);
  460. btrfs_release_path(extent_root, path);
  461. finish_current_insert(trans, extent_root);
  462. pending_ret = del_pending_extents(trans, extent_root);
  463. if (ret)
  464. return ret;
  465. if (pending_ret)
  466. return pending_ret;
  467. if (cache->data)
  468. cache->last_alloc = cache->first_free;
  469. return 0;
  470. }
  471. static int write_dirty_block_radix(struct btrfs_trans_handle *trans,
  472. struct btrfs_root *root,
  473. struct radix_tree_root *radix)
  474. {
  475. struct btrfs_block_group_cache *cache[8];
  476. int ret;
  477. int err = 0;
  478. int werr = 0;
  479. int i;
  480. struct btrfs_path *path;
  481. path = btrfs_alloc_path();
  482. if (!path)
  483. return -ENOMEM;
  484. while(1) {
  485. ret = radix_tree_gang_lookup_tag(radix, (void **)cache,
  486. 0, ARRAY_SIZE(cache),
  487. BTRFS_BLOCK_GROUP_DIRTY);
  488. if (!ret)
  489. break;
  490. for (i = 0; i < ret; i++) {
  491. radix_tree_tag_clear(radix, cache[i]->key.objectid +
  492. cache[i]->key.offset - 1,
  493. BTRFS_BLOCK_GROUP_DIRTY);
  494. err = write_one_cache_group(trans, root,
  495. path, cache[i]);
  496. if (err)
  497. werr = err;
  498. }
  499. }
  500. btrfs_free_path(path);
  501. return werr;
  502. }
  503. int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
  504. struct btrfs_root *root)
  505. {
  506. int ret;
  507. int ret2;
  508. ret = write_dirty_block_radix(trans, root,
  509. &root->fs_info->block_group_radix);
  510. ret2 = write_dirty_block_radix(trans, root,
  511. &root->fs_info->block_group_data_radix);
  512. if (ret)
  513. return ret;
  514. if (ret2)
  515. return ret2;
  516. return 0;
  517. }
  518. static int update_block_group(struct btrfs_trans_handle *trans,
  519. struct btrfs_root *root,
  520. u64 blocknr, u64 num, int alloc, int mark_free)
  521. {
  522. struct btrfs_block_group_cache *cache;
  523. struct btrfs_fs_info *info = root->fs_info;
  524. u64 total = num;
  525. u64 old_val;
  526. u64 block_in_group;
  527. u64 i;
  528. while(total) {
  529. cache = lookup_block_group(info, blocknr);
  530. if (!cache) {
  531. printk(KERN_CRIT "blocknr %Lu lookup failed\n",
  532. blocknr);
  533. return -1;
  534. }
  535. block_in_group = blocknr - cache->key.objectid;
  536. WARN_ON(block_in_group > cache->key.offset);
  537. radix_tree_tag_set(cache->radix, cache->key.objectid +
  538. cache->key.offset - 1,
  539. BTRFS_BLOCK_GROUP_DIRTY);
  540. old_val = btrfs_block_group_used(&cache->item);
  541. num = min(total, cache->key.offset - block_in_group);
  542. if (alloc) {
  543. old_val += num;
  544. if (blocknr > cache->last_alloc)
  545. cache->last_alloc = blocknr;
  546. if (!cache->data) {
  547. for (i = 0; i < num; i++) {
  548. clear_radix_bit(&info->extent_map_radix,
  549. blocknr + i);
  550. }
  551. }
  552. } else {
  553. old_val -= num;
  554. if (blocknr < cache->first_free)
  555. cache->first_free = blocknr;
  556. if (!cache->data && mark_free) {
  557. for (i = 0; i < num; i++) {
  558. set_radix_bit(&info->extent_map_radix,
  559. blocknr + i);
  560. }
  561. }
  562. if (old_val < (cache->key.offset * 5) / 10 &&
  563. old_val + num >= (cache->key.offset * 5) / 10) {
  564. printk("group %Lu now available\n", cache->key.objectid);
  565. radix_tree_tag_set(cache->radix,
  566. cache->key.objectid +
  567. cache->key.offset - 1,
  568. BTRFS_BLOCK_GROUP_AVAIL);
  569. }
  570. }
  571. btrfs_set_block_group_used(&cache->item, old_val);
  572. total -= num;
  573. blocknr += num;
  574. }
  575. return 0;
  576. }
  577. static int try_remove_page(struct address_space *mapping, unsigned long index)
  578. {
  579. int ret;
  580. ret = invalidate_mapping_pages(mapping, index, index);
  581. return ret;
  582. }
  583. int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, struct
  584. btrfs_root *root)
  585. {
  586. unsigned long gang[8];
  587. struct inode *btree_inode = root->fs_info->btree_inode;
  588. struct btrfs_block_group_cache *block_group;
  589. u64 first = 0;
  590. int ret;
  591. int i;
  592. struct radix_tree_root *pinned_radix = &root->fs_info->pinned_radix;
  593. struct radix_tree_root *extent_radix = &root->fs_info->extent_map_radix;
  594. while(1) {
  595. ret = find_first_radix_bit(pinned_radix, gang, 0,
  596. ARRAY_SIZE(gang));
  597. if (!ret)
  598. break;
  599. if (!first)
  600. first = gang[0];
  601. for (i = 0; i < ret; i++) {
  602. clear_radix_bit(pinned_radix, gang[i]);
  603. block_group = lookup_block_group(root->fs_info,
  604. gang[i]);
  605. if (block_group) {
  606. WARN_ON(block_group->pinned == 0);
  607. block_group->pinned--;
  608. if (gang[i] < block_group->last_alloc)
  609. block_group->last_alloc = gang[i];
  610. if (gang[i] < block_group->last_prealloc)
  611. block_group->last_prealloc = gang[i];
  612. if (!block_group->data)
  613. set_radix_bit(extent_radix, gang[i]);
  614. }
  615. try_remove_page(btree_inode->i_mapping,
  616. gang[i] << (PAGE_CACHE_SHIFT -
  617. btree_inode->i_blkbits));
  618. }
  619. }
  620. return 0;
  621. }
  622. static int finish_current_insert(struct btrfs_trans_handle *trans, struct
  623. btrfs_root *extent_root)
  624. {
  625. struct btrfs_key ins;
  626. struct btrfs_extent_item extent_item;
  627. int i;
  628. int ret;
  629. u64 super_blocks_used;
  630. struct btrfs_fs_info *info = extent_root->fs_info;
  631. btrfs_set_extent_refs(&extent_item, 1);
  632. ins.offset = 1;
  633. ins.flags = 0;
  634. btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
  635. btrfs_set_extent_owner(&extent_item, extent_root->root_key.objectid);
  636. for (i = 0; i < extent_root->fs_info->extent_tree_insert_nr; i++) {
  637. ins.objectid = extent_root->fs_info->extent_tree_insert[i];
  638. super_blocks_used = btrfs_super_blocks_used(info->disk_super);
  639. btrfs_set_super_blocks_used(info->disk_super,
  640. super_blocks_used + 1);
  641. ret = btrfs_insert_item(trans, extent_root, &ins, &extent_item,
  642. sizeof(extent_item));
  643. BUG_ON(ret);
  644. }
  645. extent_root->fs_info->extent_tree_insert_nr = 0;
  646. extent_root->fs_info->extent_tree_prealloc_nr = 0;
  647. return 0;
  648. }
  649. static int pin_down_block(struct btrfs_root *root, u64 blocknr, int pending)
  650. {
  651. int err;
  652. struct btrfs_header *header;
  653. struct buffer_head *bh;
  654. if (!pending) {
  655. bh = btrfs_find_tree_block(root, blocknr);
  656. if (bh) {
  657. if (buffer_uptodate(bh)) {
  658. u64 transid =
  659. root->fs_info->running_transaction->transid;
  660. header = btrfs_buffer_header(bh);
  661. if (btrfs_header_generation(header) ==
  662. transid) {
  663. btrfs_block_release(root, bh);
  664. return 0;
  665. }
  666. }
  667. btrfs_block_release(root, bh);
  668. }
  669. err = set_radix_bit(&root->fs_info->pinned_radix, blocknr);
  670. if (!err) {
  671. struct btrfs_block_group_cache *cache;
  672. cache = lookup_block_group(root->fs_info, blocknr);
  673. if (cache)
  674. cache->pinned++;
  675. }
  676. } else {
  677. err = set_radix_bit(&root->fs_info->pending_del_radix, blocknr);
  678. }
  679. BUG_ON(err < 0);
  680. return 0;
  681. }
  682. /*
  683. * remove an extent from the root, returns 0 on success
  684. */
  685. static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
  686. *root, u64 blocknr, u64 num_blocks, int pin,
  687. int mark_free)
  688. {
  689. struct btrfs_path *path;
  690. struct btrfs_key key;
  691. struct btrfs_fs_info *info = root->fs_info;
  692. struct btrfs_root *extent_root = info->extent_root;
  693. int ret;
  694. struct btrfs_extent_item *ei;
  695. struct btrfs_key ins;
  696. u32 refs;
  697. key.objectid = blocknr;
  698. key.flags = 0;
  699. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  700. key.offset = num_blocks;
  701. find_free_extent(trans, root, 0, 0, (u64)-1, &ins, 0);
  702. path = btrfs_alloc_path();
  703. BUG_ON(!path);
  704. btrfs_init_path(path);
  705. ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
  706. if (ret) {
  707. printk("failed to find %Lu\n", key.objectid);
  708. btrfs_print_tree(extent_root, extent_root->node);
  709. printk("failed to find %Lu\n", key.objectid);
  710. BUG();
  711. }
  712. ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0],
  713. struct btrfs_extent_item);
  714. BUG_ON(ei->refs == 0);
  715. refs = btrfs_extent_refs(ei) - 1;
  716. btrfs_set_extent_refs(ei, refs);
  717. btrfs_mark_buffer_dirty(path->nodes[0]);
  718. if (refs == 0) {
  719. u64 super_blocks_used;
  720. if (pin) {
  721. ret = pin_down_block(root, blocknr, 0);
  722. BUG_ON(ret);
  723. }
  724. super_blocks_used = btrfs_super_blocks_used(info->disk_super);
  725. btrfs_set_super_blocks_used(info->disk_super,
  726. super_blocks_used - num_blocks);
  727. ret = btrfs_del_item(trans, extent_root, path);
  728. if (ret)
  729. BUG();
  730. ret = update_block_group(trans, root, blocknr, num_blocks, 0,
  731. mark_free);
  732. BUG_ON(ret);
  733. }
  734. btrfs_free_path(path);
  735. finish_current_insert(trans, extent_root);
  736. return ret;
  737. }
  738. /*
  739. * find all the blocks marked as pending in the radix tree and remove
  740. * them from the extent map
  741. */
  742. static int del_pending_extents(struct btrfs_trans_handle *trans, struct
  743. btrfs_root *extent_root)
  744. {
  745. int ret;
  746. int wret;
  747. int err = 0;
  748. unsigned long gang[4];
  749. int i;
  750. struct radix_tree_root *pending_radix;
  751. struct radix_tree_root *pinned_radix;
  752. struct btrfs_block_group_cache *cache;
  753. pending_radix = &extent_root->fs_info->pending_del_radix;
  754. pinned_radix = &extent_root->fs_info->pinned_radix;
  755. while(1) {
  756. ret = find_first_radix_bit(pending_radix, gang, 0,
  757. ARRAY_SIZE(gang));
  758. if (!ret)
  759. break;
  760. for (i = 0; i < ret; i++) {
  761. wret = set_radix_bit(pinned_radix, gang[i]);
  762. if (wret == 0) {
  763. cache = lookup_block_group(extent_root->fs_info,
  764. gang[i]);
  765. if (cache)
  766. cache->pinned++;
  767. }
  768. if (wret < 0) {
  769. printk(KERN_CRIT "set_radix_bit, err %d\n",
  770. wret);
  771. BUG_ON(wret < 0);
  772. }
  773. wret = clear_radix_bit(pending_radix, gang[i]);
  774. BUG_ON(wret);
  775. wret = __free_extent(trans, extent_root,
  776. gang[i], 1, 0, 0);
  777. if (wret)
  778. err = wret;
  779. }
  780. }
  781. return err;
  782. }
  783. /*
  784. * remove an extent from the root, returns 0 on success
  785. */
  786. int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
  787. *root, u64 blocknr, u64 num_blocks, int pin)
  788. {
  789. struct btrfs_root *extent_root = root->fs_info->extent_root;
  790. int pending_ret;
  791. int ret;
  792. if (root == extent_root) {
  793. pin_down_block(root, blocknr, 1);
  794. return 0;
  795. }
  796. ret = __free_extent(trans, root, blocknr, num_blocks, pin, pin == 0);
  797. pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
  798. return ret ? ret : pending_ret;
  799. }
  800. /*
  801. * walks the btree of allocated extents and find a hole of a given size.
  802. * The key ins is changed to record the hole:
  803. * ins->objectid == block start
  804. * ins->flags = BTRFS_EXTENT_ITEM_KEY
  805. * ins->offset == number of blocks
  806. * Any available blocks before search_start are skipped.
  807. */
  808. static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
  809. *orig_root, u64 num_blocks, u64 search_start, u64
  810. search_end, struct btrfs_key *ins, int data)
  811. {
  812. struct btrfs_path *path;
  813. struct btrfs_key key;
  814. int ret;
  815. u64 hole_size = 0;
  816. int slot = 0;
  817. u64 last_block = 0;
  818. u64 test_block;
  819. u64 orig_search_start = search_start;
  820. int start_found;
  821. struct btrfs_leaf *l;
  822. struct btrfs_root * root = orig_root->fs_info->extent_root;
  823. struct btrfs_fs_info *info = root->fs_info;
  824. int total_needed = num_blocks;
  825. int total_found = 0;
  826. int fill_prealloc = 0;
  827. int level;
  828. struct btrfs_block_group_cache *block_group;
  829. int full_scan = 0;
  830. u64 limit;
  831. path = btrfs_alloc_path();
  832. ins->flags = 0;
  833. btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
  834. level = btrfs_header_level(btrfs_buffer_header(root->node));
  835. if (num_blocks == 0) {
  836. fill_prealloc = 1;
  837. num_blocks = 1;
  838. total_needed = (min(level + 1, BTRFS_MAX_LEVEL) + 2) * 3;
  839. }
  840. if (search_end == (u64)-1)
  841. search_end = btrfs_super_total_blocks(info->disk_super);
  842. if (search_start) {
  843. block_group = lookup_block_group(info, search_start);
  844. block_group = btrfs_find_block_group(root, block_group,
  845. search_start, data, 1);
  846. } else {
  847. block_group = btrfs_find_block_group(root,
  848. trans->block_group, 0,
  849. data, 1);
  850. }
  851. check_failed:
  852. if (!full_scan && block_group->data != data)
  853. WARN_ON(1);
  854. if (!data)
  855. search_start = find_search_start(root, &block_group,
  856. search_start, total_needed);
  857. else
  858. search_start = max(block_group->last_alloc, search_start);
  859. btrfs_init_path(path);
  860. ins->objectid = search_start;
  861. ins->offset = 0;
  862. start_found = 0;
  863. ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
  864. if (ret < 0)
  865. goto error;
  866. if (path->slots[0] > 0) {
  867. path->slots[0]--;
  868. }
  869. l = btrfs_buffer_leaf(path->nodes[0]);
  870. btrfs_disk_key_to_cpu(&key, &l->items[path->slots[0]].key);
  871. /*
  872. * a rare case, go back one key if we hit a block group item
  873. * instead of an extent item
  874. */
  875. if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY &&
  876. key.objectid + key.offset >= search_start) {
  877. ins->objectid = key.objectid;
  878. ins->offset = key.offset - 1;
  879. btrfs_release_path(root, path);
  880. ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
  881. if (ret < 0)
  882. goto error;
  883. if (path->slots[0] > 0) {
  884. path->slots[0]--;
  885. }
  886. }
  887. while (1) {
  888. l = btrfs_buffer_leaf(path->nodes[0]);
  889. slot = path->slots[0];
  890. if (slot >= btrfs_header_nritems(&l->header)) {
  891. if (fill_prealloc) {
  892. info->extent_tree_prealloc_nr = 0;
  893. total_found = 0;
  894. }
  895. if (start_found)
  896. limit = last_block +
  897. block_group->key.offset / 2;
  898. else
  899. limit = search_start +
  900. block_group->key.offset / 2;
  901. ret = btrfs_next_leaf(root, path);
  902. if (ret == 0)
  903. continue;
  904. if (ret < 0)
  905. goto error;
  906. if (!start_found) {
  907. ins->objectid = search_start;
  908. ins->offset = search_end - search_start;
  909. start_found = 1;
  910. goto check_pending;
  911. }
  912. ins->objectid = last_block > search_start ?
  913. last_block : search_start;
  914. ins->offset = search_end - ins->objectid;
  915. goto check_pending;
  916. }
  917. btrfs_disk_key_to_cpu(&key, &l->items[slot].key);
  918. if (key.objectid >= search_start && key.objectid > last_block &&
  919. start_found) {
  920. if (last_block < search_start)
  921. last_block = search_start;
  922. hole_size = key.objectid - last_block;
  923. if (hole_size >= num_blocks) {
  924. ins->objectid = last_block;
  925. ins->offset = hole_size;
  926. goto check_pending;
  927. }
  928. }
  929. if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY)
  930. goto next;
  931. start_found = 1;
  932. last_block = key.objectid + key.offset;
  933. if (last_block >= block_group->key.objectid +
  934. block_group->key.offset) {
  935. btrfs_release_path(root, path);
  936. search_start = block_group->key.objectid +
  937. block_group->key.offset * 2;
  938. goto new_group;
  939. }
  940. next:
  941. path->slots[0]++;
  942. cond_resched();
  943. }
  944. // FIXME -ENOSPC
  945. check_pending:
  946. /* we have to make sure we didn't find an extent that has already
  947. * been allocated by the map tree or the original allocation
  948. */
  949. btrfs_release_path(root, path);
  950. BUG_ON(ins->objectid < search_start);
  951. if (ins->objectid + num_blocks >= search_end) {
  952. if (full_scan)
  953. return -ENOSPC;
  954. search_start = orig_search_start;
  955. full_scan = 1;
  956. goto new_group;
  957. }
  958. for (test_block = ins->objectid;
  959. test_block < ins->objectid + num_blocks; test_block++) {
  960. if (test_radix_bit(&info->pinned_radix, test_block)) {
  961. search_start = test_block + 1;
  962. goto new_group;
  963. }
  964. }
  965. if (!fill_prealloc && info->extent_tree_insert_nr) {
  966. u64 last =
  967. info->extent_tree_insert[info->extent_tree_insert_nr - 1];
  968. if (ins->objectid + num_blocks >
  969. info->extent_tree_insert[0] &&
  970. ins->objectid <= last) {
  971. search_start = last + 1;
  972. WARN_ON(!full_scan);
  973. goto new_group;
  974. }
  975. }
  976. if (!fill_prealloc && info->extent_tree_prealloc_nr) {
  977. u64 first =
  978. info->extent_tree_prealloc[info->extent_tree_prealloc_nr - 1];
  979. if (ins->objectid + num_blocks > first &&
  980. ins->objectid <= info->extent_tree_prealloc[0]) {
  981. search_start = info->extent_tree_prealloc[0] + 1;
  982. WARN_ON(!full_scan);
  983. goto new_group;
  984. }
  985. }
  986. if (fill_prealloc) {
  987. int nr;
  988. test_block = ins->objectid;
  989. if (test_block - info->extent_tree_prealloc[total_needed - 1] >=
  990. leaf_range(root)) {
  991. total_found = 0;
  992. info->extent_tree_prealloc_nr = total_found;
  993. }
  994. while(test_block < ins->objectid + ins->offset &&
  995. total_found < total_needed) {
  996. nr = total_needed - total_found - 1;
  997. BUG_ON(nr < 0);
  998. info->extent_tree_prealloc[nr] = test_block;
  999. total_found++;
  1000. test_block++;
  1001. }
  1002. if (total_found < total_needed) {
  1003. search_start = test_block;
  1004. goto new_group;
  1005. }
  1006. info->extent_tree_prealloc_nr = total_found;
  1007. }
  1008. if (!data) {
  1009. block_group = lookup_block_group(info, ins->objectid);
  1010. if (block_group) {
  1011. if (fill_prealloc)
  1012. block_group->last_prealloc =
  1013. info->extent_tree_prealloc[total_needed-1];
  1014. else
  1015. trans->block_group = block_group;
  1016. }
  1017. }
  1018. ins->offset = num_blocks;
  1019. btrfs_free_path(path);
  1020. return 0;
  1021. new_group:
  1022. if (search_start + num_blocks >= search_end) {
  1023. search_start = orig_search_start;
  1024. printk("doing full scan!\n");
  1025. full_scan = 1;
  1026. }
  1027. block_group = lookup_block_group(info, search_start);
  1028. if (!full_scan)
  1029. block_group = btrfs_find_block_group(root, block_group,
  1030. search_start, data, 0);
  1031. cond_resched();
  1032. goto check_failed;
  1033. error:
  1034. btrfs_release_path(root, path);
  1035. btrfs_free_path(path);
  1036. return ret;
  1037. }
  1038. /*
  1039. * finds a free extent and does all the dirty work required for allocation
  1040. * returns the key for the extent through ins, and a tree buffer for
  1041. * the first block of the extent through buf.
  1042. *
  1043. * returns 0 if everything worked, non-zero otherwise.
  1044. */
  1045. int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
  1046. struct btrfs_root *root, u64 owner,
  1047. u64 num_blocks, u64 search_start,
  1048. u64 search_end, struct btrfs_key *ins, int data)
  1049. {
  1050. int ret;
  1051. int pending_ret;
  1052. u64 super_blocks_used;
  1053. struct btrfs_fs_info *info = root->fs_info;
  1054. struct btrfs_root *extent_root = info->extent_root;
  1055. struct btrfs_extent_item extent_item;
  1056. struct btrfs_key prealloc_key;
  1057. btrfs_set_extent_refs(&extent_item, 1);
  1058. btrfs_set_extent_owner(&extent_item, owner);
  1059. if (root == extent_root) {
  1060. int nr;
  1061. BUG_ON(info->extent_tree_prealloc_nr == 0);
  1062. BUG_ON(num_blocks != 1);
  1063. ins->offset = 1;
  1064. info->extent_tree_prealloc_nr--;
  1065. nr = info->extent_tree_prealloc_nr;
  1066. ins->objectid = info->extent_tree_prealloc[nr];
  1067. info->extent_tree_insert[info->extent_tree_insert_nr++] =
  1068. ins->objectid;
  1069. ret = update_block_group(trans, root,
  1070. ins->objectid, ins->offset, 1, 0);
  1071. BUG_ON(ret);
  1072. return 0;
  1073. }
  1074. /*
  1075. * if we're doing a data allocation, preallocate room in the
  1076. * extent tree first. This way the extent tree blocks end up
  1077. * in the correct block group.
  1078. */
  1079. if (data) {
  1080. ret = find_free_extent(trans, root, 0, 0,
  1081. search_end, &prealloc_key, 0);
  1082. if (ret) {
  1083. return ret;
  1084. }
  1085. if (prealloc_key.objectid + prealloc_key.offset >= search_end) {
  1086. int nr = info->extent_tree_prealloc_nr;
  1087. search_end = info->extent_tree_prealloc[nr - 1] - 1;
  1088. } else {
  1089. search_start = info->extent_tree_prealloc[0] + 1;
  1090. }
  1091. }
  1092. /* do the real allocation */
  1093. ret = find_free_extent(trans, root, num_blocks, search_start,
  1094. search_end, ins, data);
  1095. if (ret) {
  1096. return ret;
  1097. }
  1098. /*
  1099. * if we're doing a metadata allocation, preallocate space in the
  1100. * extent tree second. This way, we don't create a tiny hole
  1101. * in the allocation map between any unused preallocation blocks
  1102. * and the metadata block we're actually allocating. On disk,
  1103. * it'll go:
  1104. * [block we've allocated], [used prealloc 1], [ unused prealloc ]
  1105. * The unused prealloc will get reused the next time around.
  1106. */
  1107. if (!data) {
  1108. if (ins->objectid + ins->offset >= search_end)
  1109. search_end = ins->objectid - 1;
  1110. else
  1111. search_start = ins->objectid + ins->offset;
  1112. ret = find_free_extent(trans, root, 0, search_start,
  1113. search_end, &prealloc_key, 0);
  1114. if (ret) {
  1115. return ret;
  1116. }
  1117. }
  1118. super_blocks_used = btrfs_super_blocks_used(info->disk_super);
  1119. btrfs_set_super_blocks_used(info->disk_super, super_blocks_used +
  1120. num_blocks);
  1121. ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
  1122. sizeof(extent_item));
  1123. finish_current_insert(trans, extent_root);
  1124. pending_ret = del_pending_extents(trans, extent_root);
  1125. if (ret) {
  1126. return ret;
  1127. }
  1128. if (pending_ret) {
  1129. return pending_ret;
  1130. }
  1131. ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
  1132. return 0;
  1133. }
  1134. /*
  1135. * helper function to allocate a block for a given tree
  1136. * returns the tree buffer or NULL.
  1137. */
  1138. struct buffer_head *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
  1139. struct btrfs_root *root, u64 hint)
  1140. {
  1141. struct btrfs_key ins;
  1142. int ret;
  1143. struct buffer_head *buf;
  1144. ret = btrfs_alloc_extent(trans, root, root->root_key.objectid,
  1145. 1, hint, (unsigned long)-1, &ins, 0);
  1146. if (ret) {
  1147. BUG();
  1148. return NULL;
  1149. }
  1150. BUG_ON(ret);
  1151. buf = btrfs_find_create_tree_block(root, ins.objectid);
  1152. set_buffer_uptodate(buf);
  1153. set_buffer_checked(buf);
  1154. set_radix_bit(&trans->transaction->dirty_pages, buf->b_page->index);
  1155. return buf;
  1156. }
  1157. static int drop_leaf_ref(struct btrfs_trans_handle *trans,
  1158. struct btrfs_root *root, struct buffer_head *cur)
  1159. {
  1160. struct btrfs_disk_key *key;
  1161. struct btrfs_leaf *leaf;
  1162. struct btrfs_file_extent_item *fi;
  1163. int i;
  1164. int nritems;
  1165. int ret;
  1166. BUG_ON(!btrfs_is_leaf(btrfs_buffer_node(cur)));
  1167. leaf = btrfs_buffer_leaf(cur);
  1168. nritems = btrfs_header_nritems(&leaf->header);
  1169. for (i = 0; i < nritems; i++) {
  1170. u64 disk_blocknr;
  1171. key = &leaf->items[i].key;
  1172. if (btrfs_disk_key_type(key) != BTRFS_EXTENT_DATA_KEY)
  1173. continue;
  1174. fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
  1175. if (btrfs_file_extent_type(fi) == BTRFS_FILE_EXTENT_INLINE)
  1176. continue;
  1177. /*
  1178. * FIXME make sure to insert a trans record that
  1179. * repeats the snapshot del on crash
  1180. */
  1181. disk_blocknr = btrfs_file_extent_disk_blocknr(fi);
  1182. if (disk_blocknr == 0)
  1183. continue;
  1184. ret = btrfs_free_extent(trans, root, disk_blocknr,
  1185. btrfs_file_extent_disk_num_blocks(fi),
  1186. 0);
  1187. BUG_ON(ret);
  1188. }
  1189. return 0;
  1190. }
  1191. /*
  1192. * helper function for drop_snapshot, this walks down the tree dropping ref
  1193. * counts as it goes.
  1194. */
  1195. static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
  1196. *root, struct btrfs_path *path, int *level)
  1197. {
  1198. struct buffer_head *next;
  1199. struct buffer_head *cur;
  1200. u64 blocknr;
  1201. int ret;
  1202. u32 refs;
  1203. WARN_ON(*level < 0);
  1204. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  1205. ret = lookup_extent_ref(trans, root, bh_blocknr(path->nodes[*level]),
  1206. 1, &refs);
  1207. BUG_ON(ret);
  1208. if (refs > 1)
  1209. goto out;
  1210. /*
  1211. * walk down to the last node level and free all the leaves
  1212. */
  1213. while(*level >= 0) {
  1214. WARN_ON(*level < 0);
  1215. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  1216. cur = path->nodes[*level];
  1217. if (btrfs_header_level(btrfs_buffer_header(cur)) != *level)
  1218. WARN_ON(1);
  1219. if (path->slots[*level] >=
  1220. btrfs_header_nritems(btrfs_buffer_header(cur)))
  1221. break;
  1222. if (*level == 0) {
  1223. ret = drop_leaf_ref(trans, root, cur);
  1224. BUG_ON(ret);
  1225. break;
  1226. }
  1227. blocknr = btrfs_node_blockptr(btrfs_buffer_node(cur),
  1228. path->slots[*level]);
  1229. ret = lookup_extent_ref(trans, root, blocknr, 1, &refs);
  1230. BUG_ON(ret);
  1231. if (refs != 1) {
  1232. path->slots[*level]++;
  1233. ret = btrfs_free_extent(trans, root, blocknr, 1, 1);
  1234. BUG_ON(ret);
  1235. continue;
  1236. }
  1237. next = read_tree_block(root, blocknr);
  1238. WARN_ON(*level <= 0);
  1239. if (path->nodes[*level-1])
  1240. btrfs_block_release(root, path->nodes[*level-1]);
  1241. path->nodes[*level-1] = next;
  1242. *level = btrfs_header_level(btrfs_buffer_header(next));
  1243. path->slots[*level] = 0;
  1244. }
  1245. out:
  1246. WARN_ON(*level < 0);
  1247. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  1248. ret = btrfs_free_extent(trans, root,
  1249. bh_blocknr(path->nodes[*level]), 1, 1);
  1250. btrfs_block_release(root, path->nodes[*level]);
  1251. path->nodes[*level] = NULL;
  1252. *level += 1;
  1253. BUG_ON(ret);
  1254. return 0;
  1255. }
  1256. /*
  1257. * helper for dropping snapshots. This walks back up the tree in the path
  1258. * to find the first node higher up where we haven't yet gone through
  1259. * all the slots
  1260. */
  1261. static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
  1262. *root, struct btrfs_path *path, int *level)
  1263. {
  1264. int i;
  1265. int slot;
  1266. int ret;
  1267. for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
  1268. slot = path->slots[i];
  1269. if (slot < btrfs_header_nritems(
  1270. btrfs_buffer_header(path->nodes[i])) - 1) {
  1271. path->slots[i]++;
  1272. *level = i;
  1273. return 0;
  1274. } else {
  1275. ret = btrfs_free_extent(trans, root,
  1276. bh_blocknr(path->nodes[*level]),
  1277. 1, 1);
  1278. BUG_ON(ret);
  1279. btrfs_block_release(root, path->nodes[*level]);
  1280. path->nodes[*level] = NULL;
  1281. *level = i + 1;
  1282. }
  1283. }
  1284. return 1;
  1285. }
  1286. /*
  1287. * drop the reference count on the tree rooted at 'snap'. This traverses
  1288. * the tree freeing any blocks that have a ref count of zero after being
  1289. * decremented.
  1290. */
  1291. int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
  1292. *root, struct buffer_head *snap)
  1293. {
  1294. int ret = 0;
  1295. int wret;
  1296. int level;
  1297. struct btrfs_path *path;
  1298. int i;
  1299. int orig_level;
  1300. path = btrfs_alloc_path();
  1301. BUG_ON(!path);
  1302. btrfs_init_path(path);
  1303. level = btrfs_header_level(btrfs_buffer_header(snap));
  1304. orig_level = level;
  1305. path->nodes[level] = snap;
  1306. path->slots[level] = 0;
  1307. while(1) {
  1308. wret = walk_down_tree(trans, root, path, &level);
  1309. if (wret > 0)
  1310. break;
  1311. if (wret < 0)
  1312. ret = wret;
  1313. wret = walk_up_tree(trans, root, path, &level);
  1314. if (wret > 0)
  1315. break;
  1316. if (wret < 0)
  1317. ret = wret;
  1318. btrfs_btree_balance_dirty(root);
  1319. }
  1320. for (i = 0; i <= orig_level; i++) {
  1321. if (path->nodes[i]) {
  1322. btrfs_block_release(root, path->nodes[i]);
  1323. }
  1324. }
  1325. btrfs_free_path(path);
  1326. return ret;
  1327. }
  1328. static int free_block_group_radix(struct radix_tree_root *radix)
  1329. {
  1330. int ret;
  1331. struct btrfs_block_group_cache *cache[8];
  1332. int i;
  1333. while(1) {
  1334. ret = radix_tree_gang_lookup(radix, (void **)cache, 0,
  1335. ARRAY_SIZE(cache));
  1336. if (!ret)
  1337. break;
  1338. for (i = 0; i < ret; i++) {
  1339. radix_tree_delete(radix, cache[i]->key.objectid +
  1340. cache[i]->key.offset - 1);
  1341. kfree(cache[i]);
  1342. }
  1343. }
  1344. return 0;
  1345. }
  1346. int btrfs_free_block_groups(struct btrfs_fs_info *info)
  1347. {
  1348. int ret;
  1349. int ret2;
  1350. unsigned long gang[16];
  1351. int i;
  1352. ret = free_block_group_radix(&info->block_group_radix);
  1353. ret2 = free_block_group_radix(&info->block_group_data_radix);
  1354. if (ret)
  1355. return ret;
  1356. if (ret2)
  1357. return ret2;
  1358. while(1) {
  1359. ret = find_first_radix_bit(&info->extent_map_radix,
  1360. gang, 0, ARRAY_SIZE(gang));
  1361. if (!ret)
  1362. break;
  1363. for (i = 0; i < ret; i++) {
  1364. clear_radix_bit(&info->extent_map_radix, gang[i]);
  1365. }
  1366. }
  1367. return 0;
  1368. }
  1369. int btrfs_read_block_groups(struct btrfs_root *root)
  1370. {
  1371. struct btrfs_path *path;
  1372. int ret;
  1373. int err = 0;
  1374. struct btrfs_block_group_item *bi;
  1375. struct btrfs_block_group_cache *cache;
  1376. struct btrfs_fs_info *info = root->fs_info;
  1377. struct radix_tree_root *radix;
  1378. struct btrfs_key key;
  1379. struct btrfs_key found_key;
  1380. struct btrfs_leaf *leaf;
  1381. u64 group_size_blocks = BTRFS_BLOCK_GROUP_SIZE / root->blocksize;
  1382. u64 used;
  1383. u64 nr = 0;
  1384. root = info->extent_root;
  1385. key.objectid = 0;
  1386. key.offset = group_size_blocks;
  1387. key.flags = 0;
  1388. btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
  1389. path = btrfs_alloc_path();
  1390. if (!path)
  1391. return -ENOMEM;
  1392. while(1) {
  1393. ret = btrfs_search_slot(NULL, info->extent_root,
  1394. &key, path, 0, 0);
  1395. if (ret != 0) {
  1396. err = ret;
  1397. break;
  1398. }
  1399. leaf = btrfs_buffer_leaf(path->nodes[0]);
  1400. btrfs_disk_key_to_cpu(&found_key,
  1401. &leaf->items[path->slots[0]].key);
  1402. cache = kmalloc(sizeof(*cache), GFP_NOFS);
  1403. if (!cache) {
  1404. err = -1;
  1405. break;
  1406. }
  1407. if (nr % 3)
  1408. radix = &info->block_group_data_radix;
  1409. else
  1410. radix = &info->block_group_radix;
  1411. bi = btrfs_item_ptr(leaf, path->slots[0],
  1412. struct btrfs_block_group_item);
  1413. memcpy(&cache->item, bi, sizeof(*bi));
  1414. memcpy(&cache->key, &found_key, sizeof(found_key));
  1415. cache->last_alloc = cache->key.objectid;
  1416. cache->first_free = cache->key.objectid;
  1417. cache->last_prealloc = cache->key.objectid;
  1418. cache->pinned = 0;
  1419. cache->cached = 0;
  1420. if (nr % 3)
  1421. cache->data = 1;
  1422. else
  1423. cache->data = 0;
  1424. cache->radix = radix;
  1425. key.objectid = found_key.objectid + found_key.offset;
  1426. btrfs_release_path(root, path);
  1427. ret = radix_tree_insert(radix, found_key.objectid +
  1428. found_key.offset - 1,
  1429. (void *)cache);
  1430. BUG_ON(ret);
  1431. used = btrfs_block_group_used(bi);
  1432. if (used < (key.offset * 8) / 10) {
  1433. radix_tree_tag_set(radix, found_key.objectid +
  1434. found_key.offset - 1,
  1435. BTRFS_BLOCK_GROUP_AVAIL);
  1436. }
  1437. if (key.objectid >=
  1438. btrfs_super_total_blocks(info->disk_super))
  1439. break;
  1440. nr++;
  1441. }
  1442. btrfs_free_path(path);
  1443. return 0;
  1444. }