extent-tree.c 43 KB

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