extent-tree.c 40 KB

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