extent-tree.c 45 KB

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