extent-tree.c 41 KB

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