extent-tree.c 38 KB

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