extent-tree.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081
  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 <linux/crc32c.h>
  20. #include "hash.h"
  21. #include "ctree.h"
  22. #include "disk-io.h"
  23. #include "print-tree.h"
  24. #include "transaction.h"
  25. #define BLOCK_GROUP_DATA EXTENT_WRITEBACK
  26. #define BLOCK_GROUP_METADATA EXTENT_UPTODATE
  27. #define BLOCK_GROUP_DIRTY EXTENT_DIRTY
  28. static int finish_current_insert(struct btrfs_trans_handle *trans, struct
  29. btrfs_root *extent_root);
  30. static int del_pending_extents(struct btrfs_trans_handle *trans, struct
  31. btrfs_root *extent_root);
  32. static int cache_block_group(struct btrfs_root *root,
  33. struct btrfs_block_group_cache *block_group)
  34. {
  35. struct btrfs_path *path;
  36. int ret;
  37. struct btrfs_key key;
  38. struct extent_buffer *leaf;
  39. struct extent_map_tree *free_space_cache;
  40. int slot;
  41. u64 last = 0;
  42. u64 hole_size;
  43. u64 first_free;
  44. int found = 0;
  45. if (!block_group)
  46. return 0;
  47. root = root->fs_info->extent_root;
  48. free_space_cache = &root->fs_info->free_space_cache;
  49. if (block_group->cached)
  50. return 0;
  51. path = btrfs_alloc_path();
  52. if (!path)
  53. return -ENOMEM;
  54. path->reada = 2;
  55. first_free = block_group->key.objectid;
  56. key.objectid = block_group->key.objectid;
  57. key.offset = 0;
  58. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  59. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  60. if (ret < 0)
  61. return ret;
  62. if (ret && path->slots[0] > 0)
  63. path->slots[0]--;
  64. while(1) {
  65. leaf = path->nodes[0];
  66. slot = path->slots[0];
  67. if (slot >= btrfs_header_nritems(leaf)) {
  68. ret = btrfs_next_leaf(root, path);
  69. if (ret < 0)
  70. goto err;
  71. if (ret == 0) {
  72. continue;
  73. } else {
  74. break;
  75. }
  76. }
  77. btrfs_item_key_to_cpu(leaf, &key, slot);
  78. if (key.objectid < block_group->key.objectid) {
  79. if (btrfs_key_type(&key) != BTRFS_EXTENT_REF_KEY &&
  80. key.objectid + key.offset > first_free)
  81. first_free = key.objectid + key.offset;
  82. goto next;
  83. }
  84. if (key.objectid >= block_group->key.objectid +
  85. block_group->key.offset) {
  86. break;
  87. }
  88. if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
  89. if (!found) {
  90. last = first_free;
  91. found = 1;
  92. }
  93. if (key.objectid > last) {
  94. hole_size = key.objectid - last;
  95. set_extent_dirty(free_space_cache, last,
  96. last + hole_size - 1,
  97. GFP_NOFS);
  98. }
  99. last = key.objectid + key.offset;
  100. }
  101. next:
  102. path->slots[0]++;
  103. }
  104. if (!found)
  105. last = first_free;
  106. if (block_group->key.objectid +
  107. block_group->key.offset > last) {
  108. hole_size = block_group->key.objectid +
  109. block_group->key.offset - last;
  110. set_extent_dirty(free_space_cache, last,
  111. last + hole_size - 1, GFP_NOFS);
  112. }
  113. block_group->cached = 1;
  114. err:
  115. btrfs_free_path(path);
  116. return 0;
  117. }
  118. struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
  119. btrfs_fs_info *info,
  120. u64 bytenr)
  121. {
  122. struct extent_map_tree *block_group_cache;
  123. struct btrfs_block_group_cache *block_group = NULL;
  124. u64 ptr;
  125. u64 start;
  126. u64 end;
  127. int ret;
  128. block_group_cache = &info->block_group_cache;
  129. ret = find_first_extent_bit(block_group_cache,
  130. bytenr, &start, &end,
  131. BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA);
  132. if (ret) {
  133. return NULL;
  134. }
  135. ret = get_state_private(block_group_cache, start, &ptr);
  136. if (ret)
  137. return NULL;
  138. block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
  139. if (block_group->key.objectid <= bytenr && bytenr <
  140. block_group->key.objectid + block_group->key.offset)
  141. return block_group;
  142. return NULL;
  143. }
  144. static u64 find_search_start(struct btrfs_root *root,
  145. struct btrfs_block_group_cache **cache_ret,
  146. u64 search_start, int num,
  147. int data, int full_scan)
  148. {
  149. int ret;
  150. struct btrfs_block_group_cache *cache = *cache_ret;
  151. u64 last;
  152. u64 start = 0;
  153. u64 end = 0;
  154. u64 cache_miss = 0;
  155. int wrapped = 0;
  156. if (!cache) {
  157. goto out;
  158. }
  159. again:
  160. ret = cache_block_group(root, cache);
  161. if (ret)
  162. goto out;
  163. last = max(search_start, cache->key.objectid);
  164. while(1) {
  165. ret = find_first_extent_bit(&root->fs_info->free_space_cache,
  166. last, &start, &end, EXTENT_DIRTY);
  167. if (ret) {
  168. if (!cache_miss)
  169. cache_miss = last;
  170. goto new_group;
  171. }
  172. start = max(last, start);
  173. last = end + 1;
  174. if (last - start < num) {
  175. if (last == cache->key.objectid + cache->key.offset)
  176. cache_miss = start;
  177. continue;
  178. }
  179. if (data != BTRFS_BLOCK_GROUP_MIXED &&
  180. start + num > cache->key.objectid + cache->key.offset)
  181. goto new_group;
  182. return start;
  183. }
  184. out:
  185. cache = btrfs_lookup_block_group(root->fs_info, search_start);
  186. if (!cache) {
  187. printk("Unable to find block group for %Lu\n",
  188. search_start);
  189. WARN_ON(1);
  190. return search_start;
  191. }
  192. return search_start;
  193. new_group:
  194. last = cache->key.objectid + cache->key.offset;
  195. wrapped:
  196. cache = btrfs_lookup_block_group(root->fs_info, last);
  197. if (!cache) {
  198. no_cache:
  199. if (!wrapped) {
  200. wrapped = 1;
  201. last = search_start;
  202. data = BTRFS_BLOCK_GROUP_MIXED;
  203. goto wrapped;
  204. }
  205. goto out;
  206. }
  207. if (cache_miss && !cache->cached) {
  208. cache_block_group(root, cache);
  209. last = cache_miss;
  210. cache = btrfs_lookup_block_group(root->fs_info, last);
  211. }
  212. cache = btrfs_find_block_group(root, cache, last, data, 0);
  213. if (!cache)
  214. goto no_cache;
  215. *cache_ret = cache;
  216. cache_miss = 0;
  217. goto again;
  218. }
  219. static u64 div_factor(u64 num, int factor)
  220. {
  221. if (factor == 10)
  222. return num;
  223. num *= factor;
  224. do_div(num, 10);
  225. return num;
  226. }
  227. struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
  228. struct btrfs_block_group_cache
  229. *hint, u64 search_start,
  230. int data, int owner)
  231. {
  232. struct btrfs_block_group_cache *cache;
  233. struct extent_map_tree *block_group_cache;
  234. struct btrfs_block_group_cache *found_group = NULL;
  235. struct btrfs_fs_info *info = root->fs_info;
  236. u64 used;
  237. u64 last = 0;
  238. u64 hint_last;
  239. u64 start;
  240. u64 end;
  241. u64 free_check;
  242. u64 ptr;
  243. int bit;
  244. int ret;
  245. int full_search = 0;
  246. int factor = 8;
  247. int data_swap = 0;
  248. block_group_cache = &info->block_group_cache;
  249. if (!owner)
  250. factor = 8;
  251. if (data == BTRFS_BLOCK_GROUP_MIXED) {
  252. bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
  253. factor = 10;
  254. } else if (data)
  255. bit = BLOCK_GROUP_DATA;
  256. else
  257. bit = BLOCK_GROUP_METADATA;
  258. if (search_start) {
  259. struct btrfs_block_group_cache *shint;
  260. shint = btrfs_lookup_block_group(info, search_start);
  261. if (shint && (shint->data == data ||
  262. shint->data == BTRFS_BLOCK_GROUP_MIXED)) {
  263. used = btrfs_block_group_used(&shint->item);
  264. if (used + shint->pinned <
  265. div_factor(shint->key.offset, factor)) {
  266. return shint;
  267. }
  268. }
  269. }
  270. if (hint && (hint->data == data ||
  271. hint->data == BTRFS_BLOCK_GROUP_MIXED)) {
  272. used = btrfs_block_group_used(&hint->item);
  273. if (used + hint->pinned <
  274. div_factor(hint->key.offset, factor)) {
  275. return hint;
  276. }
  277. last = hint->key.objectid + hint->key.offset;
  278. hint_last = last;
  279. } else {
  280. if (hint)
  281. hint_last = max(hint->key.objectid, search_start);
  282. else
  283. hint_last = search_start;
  284. last = hint_last;
  285. }
  286. again:
  287. while(1) {
  288. ret = find_first_extent_bit(block_group_cache, last,
  289. &start, &end, bit);
  290. if (ret)
  291. break;
  292. ret = get_state_private(block_group_cache, start, &ptr);
  293. if (ret)
  294. break;
  295. cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
  296. last = cache->key.objectid + cache->key.offset;
  297. used = btrfs_block_group_used(&cache->item);
  298. if (full_search)
  299. free_check = cache->key.offset;
  300. else
  301. free_check = div_factor(cache->key.offset, factor);
  302. if (used + cache->pinned < free_check) {
  303. found_group = cache;
  304. goto found;
  305. }
  306. cond_resched();
  307. }
  308. if (!full_search) {
  309. last = search_start;
  310. full_search = 1;
  311. goto again;
  312. }
  313. if (!data_swap) {
  314. data_swap = 1;
  315. bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
  316. last = search_start;
  317. goto again;
  318. }
  319. found:
  320. return found_group;
  321. }
  322. static u64 hash_extent_ref(u64 root_objectid, u64 ref_generation,
  323. u64 owner, u64 owner_offset)
  324. {
  325. u32 high_crc = ~(u32)0;
  326. u32 low_crc = ~(u32)0;
  327. __le64 lenum;
  328. lenum = cpu_to_le64(root_objectid);
  329. high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
  330. lenum = cpu_to_le64(ref_generation);
  331. low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
  332. #if 0
  333. lenum = cpu_to_le64(owner);
  334. low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
  335. lenum = cpu_to_le64(owner_offset);
  336. low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
  337. #endif
  338. return ((u64)high_crc << 32) | (u64)low_crc;
  339. }
  340. static int match_extent_ref(struct extent_buffer *leaf,
  341. struct btrfs_extent_ref *disk_ref,
  342. struct btrfs_extent_ref *cpu_ref)
  343. {
  344. int ret;
  345. int len;
  346. if (cpu_ref->objectid)
  347. len = sizeof(*cpu_ref);
  348. else
  349. len = 2 * sizeof(u64);
  350. ret = memcmp_extent_buffer(leaf, cpu_ref, (unsigned long)disk_ref,
  351. len);
  352. return ret == 0;
  353. }
  354. static int lookup_extent_backref(struct btrfs_trans_handle *trans,
  355. struct btrfs_root *root,
  356. struct btrfs_path *path, u64 bytenr,
  357. u64 root_objectid, u64 ref_generation,
  358. u64 owner, u64 owner_offset, int del)
  359. {
  360. u64 hash;
  361. struct btrfs_key key;
  362. struct btrfs_key found_key;
  363. struct btrfs_extent_ref ref;
  364. struct extent_buffer *leaf;
  365. struct btrfs_extent_ref *disk_ref;
  366. int ret;
  367. int ret2;
  368. btrfs_set_stack_ref_root(&ref, root_objectid);
  369. btrfs_set_stack_ref_generation(&ref, ref_generation);
  370. btrfs_set_stack_ref_objectid(&ref, owner);
  371. btrfs_set_stack_ref_offset(&ref, owner_offset);
  372. hash = hash_extent_ref(root_objectid, ref_generation, owner,
  373. owner_offset);
  374. key.offset = hash;
  375. key.objectid = bytenr;
  376. key.type = BTRFS_EXTENT_REF_KEY;
  377. while (1) {
  378. ret = btrfs_search_slot(trans, root, &key, path,
  379. del ? -1 : 0, del);
  380. if (ret < 0)
  381. goto out;
  382. leaf = path->nodes[0];
  383. if (ret != 0) {
  384. u32 nritems = btrfs_header_nritems(leaf);
  385. if (path->slots[0] >= nritems) {
  386. ret2 = btrfs_next_leaf(root, path);
  387. if (ret2)
  388. goto out;
  389. leaf = path->nodes[0];
  390. }
  391. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  392. if (found_key.objectid != bytenr ||
  393. found_key.type != BTRFS_EXTENT_REF_KEY)
  394. goto out;
  395. key.offset = found_key.offset;
  396. if (del) {
  397. btrfs_release_path(root, path);
  398. continue;
  399. }
  400. }
  401. disk_ref = btrfs_item_ptr(path->nodes[0],
  402. path->slots[0],
  403. struct btrfs_extent_ref);
  404. if (match_extent_ref(path->nodes[0], disk_ref, &ref)) {
  405. ret = 0;
  406. goto out;
  407. }
  408. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  409. key.offset = found_key.offset + 1;
  410. btrfs_release_path(root, path);
  411. }
  412. out:
  413. return ret;
  414. }
  415. /*
  416. * Back reference rules. Back refs have three main goals:
  417. *
  418. * 1) differentiate between all holders of references to an extent so that
  419. * when a reference is dropped we can make sure it was a valid reference
  420. * before freeing the extent.
  421. *
  422. * 2) Provide enough information to quickly find the holders of an extent
  423. * if we notice a given block is corrupted or bad.
  424. *
  425. * 3) Make it easy to migrate blocks for FS shrinking or storage pool
  426. * maintenance. This is actually the same as #2, but with a slightly
  427. * different use case.
  428. *
  429. * File extents can be referenced by:
  430. *
  431. * - multiple snapshots, subvolumes, or different generations in one subvol
  432. * - different files inside a single subvolume (in theory, not implemented yet)
  433. * - different offsets inside a file (bookend extents in file.c)
  434. *
  435. * The extent ref structure has fields for:
  436. *
  437. * - Objectid of the subvolume root
  438. * - Generation number of the tree holding the reference
  439. * - objectid of the file holding the reference
  440. * - offset in the file corresponding to the key holding the reference
  441. *
  442. * When a file extent is allocated the fields are filled in:
  443. * (root_key.objectid, trans->transid, inode objectid, offset in file)
  444. *
  445. * When a leaf is cow'd new references are added for every file extent found
  446. * in the leaf. It looks the same as the create case, but trans->transid
  447. * will be different when the block is cow'd.
  448. *
  449. * (root_key.objectid, trans->transid, inode objectid, offset in file)
  450. *
  451. * When a file extent is removed either during snapshot deletion or file
  452. * truncation, the corresponding back reference is found
  453. * by searching for:
  454. *
  455. * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
  456. * inode objectid, offset in file)
  457. *
  458. * Btree extents can be referenced by:
  459. *
  460. * - Different subvolumes
  461. * - Different generations of the same subvolume
  462. *
  463. * Storing sufficient information for a full reverse mapping of a btree
  464. * block would require storing the lowest key of the block in the backref,
  465. * and it would require updating that lowest key either before write out or
  466. * every time it changed. Instead, the objectid of the lowest key is stored
  467. * along with the level of the tree block. This provides a hint
  468. * about where in the btree the block can be found. Searches through the
  469. * btree only need to look for a pointer to that block, so they stop one
  470. * level higher than the level recorded in the backref.
  471. *
  472. * Some btrees do not do reference counting on their extents. These
  473. * include the extent tree and the tree of tree roots. Backrefs for these
  474. * trees always have a generation of zero.
  475. *
  476. * When a tree block is created, back references are inserted:
  477. *
  478. * (root->root_key.objectid, trans->transid or zero, level, lowest_key_objectid)
  479. *
  480. * When a tree block is cow'd in a reference counted root,
  481. * new back references are added for all the blocks it points to.
  482. * These are of the form (trans->transid will have increased since creation):
  483. *
  484. * (root->root_key.objectid, trans->transid, level, lowest_key_objectid)
  485. *
  486. * Because the lowest_key_objectid and the level are just hints
  487. * they are not used when backrefs are deleted. When a backref is deleted:
  488. *
  489. * if backref was for a tree root:
  490. * root_objectid = root->root_key.objectid
  491. * else
  492. * root_objectid = btrfs_header_owner(parent)
  493. *
  494. * (root_objectid, btrfs_header_generation(parent) or zero, 0, 0)
  495. *
  496. * Back Reference Key hashing:
  497. *
  498. * Back references have four fields, each 64 bits long. Unfortunately,
  499. * This is hashed into a single 64 bit number and placed into the key offset.
  500. * The key objectid corresponds to the first byte in the extent, and the
  501. * key type is set to BTRFS_EXTENT_REF_KEY
  502. */
  503. int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans,
  504. struct btrfs_root *root,
  505. struct btrfs_path *path, u64 bytenr,
  506. u64 root_objectid, u64 ref_generation,
  507. u64 owner, u64 owner_offset)
  508. {
  509. u64 hash;
  510. struct btrfs_key key;
  511. struct btrfs_extent_ref ref;
  512. struct btrfs_extent_ref *disk_ref;
  513. int ret;
  514. btrfs_set_stack_ref_root(&ref, root_objectid);
  515. btrfs_set_stack_ref_generation(&ref, ref_generation);
  516. btrfs_set_stack_ref_objectid(&ref, owner);
  517. btrfs_set_stack_ref_offset(&ref, owner_offset);
  518. hash = hash_extent_ref(root_objectid, ref_generation, owner,
  519. owner_offset);
  520. key.offset = hash;
  521. key.objectid = bytenr;
  522. key.type = BTRFS_EXTENT_REF_KEY;
  523. ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(ref));
  524. while (ret == -EEXIST) {
  525. disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
  526. struct btrfs_extent_ref);
  527. if (match_extent_ref(path->nodes[0], disk_ref, &ref))
  528. goto out;
  529. key.offset++;
  530. btrfs_release_path(root, path);
  531. ret = btrfs_insert_empty_item(trans, root, path, &key,
  532. sizeof(ref));
  533. }
  534. if (ret)
  535. goto out;
  536. disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
  537. struct btrfs_extent_ref);
  538. write_extent_buffer(path->nodes[0], &ref, (unsigned long)disk_ref,
  539. sizeof(ref));
  540. btrfs_mark_buffer_dirty(path->nodes[0]);
  541. out:
  542. btrfs_release_path(root, path);
  543. return ret;
  544. }
  545. int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
  546. struct btrfs_root *root,
  547. u64 bytenr, u64 num_bytes,
  548. u64 root_objectid, u64 ref_generation,
  549. u64 owner, u64 owner_offset)
  550. {
  551. struct btrfs_path *path;
  552. int ret;
  553. struct btrfs_key key;
  554. struct extent_buffer *l;
  555. struct btrfs_extent_item *item;
  556. u32 refs;
  557. WARN_ON(num_bytes < root->sectorsize);
  558. path = btrfs_alloc_path();
  559. if (!path)
  560. return -ENOMEM;
  561. key.objectid = bytenr;
  562. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  563. key.offset = num_bytes;
  564. ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
  565. 0, 1);
  566. if (ret < 0)
  567. return ret;
  568. if (ret != 0) {
  569. BUG();
  570. }
  571. BUG_ON(ret != 0);
  572. l = path->nodes[0];
  573. item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
  574. refs = btrfs_extent_refs(l, item);
  575. btrfs_set_extent_refs(l, item, refs + 1);
  576. btrfs_mark_buffer_dirty(path->nodes[0]);
  577. btrfs_release_path(root->fs_info->extent_root, path);
  578. ret = btrfs_insert_extent_backref(trans, root->fs_info->extent_root,
  579. path, bytenr, root_objectid,
  580. ref_generation, owner, owner_offset);
  581. BUG_ON(ret);
  582. finish_current_insert(trans, root->fs_info->extent_root);
  583. del_pending_extents(trans, root->fs_info->extent_root);
  584. btrfs_free_path(path);
  585. return 0;
  586. }
  587. int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
  588. struct btrfs_root *root)
  589. {
  590. finish_current_insert(trans, root->fs_info->extent_root);
  591. del_pending_extents(trans, root->fs_info->extent_root);
  592. return 0;
  593. }
  594. static int lookup_extent_ref(struct btrfs_trans_handle *trans,
  595. struct btrfs_root *root, u64 bytenr,
  596. u64 num_bytes, u32 *refs)
  597. {
  598. struct btrfs_path *path;
  599. int ret;
  600. struct btrfs_key key;
  601. struct extent_buffer *l;
  602. struct btrfs_extent_item *item;
  603. WARN_ON(num_bytes < root->sectorsize);
  604. path = btrfs_alloc_path();
  605. key.objectid = bytenr;
  606. key.offset = num_bytes;
  607. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  608. ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
  609. 0, 0);
  610. if (ret < 0)
  611. goto out;
  612. if (ret != 0) {
  613. btrfs_print_leaf(root, path->nodes[0]);
  614. printk("failed to find block number %Lu\n", bytenr);
  615. BUG();
  616. }
  617. l = path->nodes[0];
  618. item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
  619. *refs = btrfs_extent_refs(l, item);
  620. out:
  621. btrfs_free_path(path);
  622. return 0;
  623. }
  624. int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
  625. struct btrfs_root *root, u64 owner_objectid)
  626. {
  627. u64 generation;
  628. u64 key_objectid;
  629. u64 level;
  630. u32 nritems;
  631. struct btrfs_disk_key disk_key;
  632. level = btrfs_header_level(root->node);
  633. generation = trans->transid;
  634. nritems = btrfs_header_nritems(root->node);
  635. if (nritems > 0) {
  636. if (level == 0)
  637. btrfs_item_key(root->node, &disk_key, 0);
  638. else
  639. btrfs_node_key(root->node, &disk_key, 0);
  640. key_objectid = btrfs_disk_key_objectid(&disk_key);
  641. } else {
  642. key_objectid = 0;
  643. }
  644. return btrfs_inc_extent_ref(trans, root, root->node->start,
  645. root->node->len, owner_objectid,
  646. generation, level, key_objectid);
  647. }
  648. int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  649. struct extent_buffer *buf)
  650. {
  651. u64 bytenr;
  652. u32 nritems;
  653. struct btrfs_key key;
  654. struct btrfs_file_extent_item *fi;
  655. int i;
  656. int level;
  657. int ret;
  658. int faili;
  659. if (!root->ref_cows)
  660. return 0;
  661. level = btrfs_header_level(buf);
  662. nritems = btrfs_header_nritems(buf);
  663. for (i = 0; i < nritems; i++) {
  664. if (level == 0) {
  665. u64 disk_bytenr;
  666. btrfs_item_key_to_cpu(buf, &key, i);
  667. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  668. continue;
  669. fi = btrfs_item_ptr(buf, i,
  670. struct btrfs_file_extent_item);
  671. if (btrfs_file_extent_type(buf, fi) ==
  672. BTRFS_FILE_EXTENT_INLINE)
  673. continue;
  674. disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  675. if (disk_bytenr == 0)
  676. continue;
  677. ret = btrfs_inc_extent_ref(trans, root, disk_bytenr,
  678. btrfs_file_extent_disk_num_bytes(buf, fi),
  679. root->root_key.objectid, trans->transid,
  680. key.objectid, key.offset);
  681. if (ret) {
  682. faili = i;
  683. goto fail;
  684. }
  685. } else {
  686. bytenr = btrfs_node_blockptr(buf, i);
  687. btrfs_node_key_to_cpu(buf, &key, i);
  688. ret = btrfs_inc_extent_ref(trans, root, bytenr,
  689. btrfs_level_size(root, level - 1),
  690. root->root_key.objectid,
  691. trans->transid,
  692. level - 1, key.objectid);
  693. if (ret) {
  694. faili = i;
  695. goto fail;
  696. }
  697. }
  698. }
  699. return 0;
  700. fail:
  701. WARN_ON(1);
  702. #if 0
  703. for (i =0; i < faili; i++) {
  704. if (level == 0) {
  705. u64 disk_bytenr;
  706. btrfs_item_key_to_cpu(buf, &key, i);
  707. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  708. continue;
  709. fi = btrfs_item_ptr(buf, i,
  710. struct btrfs_file_extent_item);
  711. if (btrfs_file_extent_type(buf, fi) ==
  712. BTRFS_FILE_EXTENT_INLINE)
  713. continue;
  714. disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  715. if (disk_bytenr == 0)
  716. continue;
  717. err = btrfs_free_extent(trans, root, disk_bytenr,
  718. btrfs_file_extent_disk_num_bytes(buf,
  719. fi), 0);
  720. BUG_ON(err);
  721. } else {
  722. bytenr = btrfs_node_blockptr(buf, i);
  723. err = btrfs_free_extent(trans, root, bytenr,
  724. btrfs_level_size(root, level - 1), 0);
  725. BUG_ON(err);
  726. }
  727. }
  728. #endif
  729. return ret;
  730. }
  731. static int write_one_cache_group(struct btrfs_trans_handle *trans,
  732. struct btrfs_root *root,
  733. struct btrfs_path *path,
  734. struct btrfs_block_group_cache *cache)
  735. {
  736. int ret;
  737. int pending_ret;
  738. struct btrfs_root *extent_root = root->fs_info->extent_root;
  739. unsigned long bi;
  740. struct extent_buffer *leaf;
  741. ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
  742. if (ret < 0)
  743. goto fail;
  744. BUG_ON(ret);
  745. leaf = path->nodes[0];
  746. bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
  747. write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
  748. btrfs_mark_buffer_dirty(leaf);
  749. btrfs_release_path(extent_root, path);
  750. fail:
  751. finish_current_insert(trans, extent_root);
  752. pending_ret = del_pending_extents(trans, extent_root);
  753. if (ret)
  754. return ret;
  755. if (pending_ret)
  756. return pending_ret;
  757. return 0;
  758. }
  759. int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
  760. struct btrfs_root *root)
  761. {
  762. struct extent_map_tree *block_group_cache;
  763. struct btrfs_block_group_cache *cache;
  764. int ret;
  765. int err = 0;
  766. int werr = 0;
  767. struct btrfs_path *path;
  768. u64 last = 0;
  769. u64 start;
  770. u64 end;
  771. u64 ptr;
  772. block_group_cache = &root->fs_info->block_group_cache;
  773. path = btrfs_alloc_path();
  774. if (!path)
  775. return -ENOMEM;
  776. while(1) {
  777. ret = find_first_extent_bit(block_group_cache, last,
  778. &start, &end, BLOCK_GROUP_DIRTY);
  779. if (ret)
  780. break;
  781. last = end + 1;
  782. ret = get_state_private(block_group_cache, start, &ptr);
  783. if (ret)
  784. break;
  785. cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
  786. err = write_one_cache_group(trans, root,
  787. path, cache);
  788. /*
  789. * if we fail to write the cache group, we want
  790. * to keep it marked dirty in hopes that a later
  791. * write will work
  792. */
  793. if (err) {
  794. werr = err;
  795. continue;
  796. }
  797. clear_extent_bits(block_group_cache, start, end,
  798. BLOCK_GROUP_DIRTY, GFP_NOFS);
  799. }
  800. btrfs_free_path(path);
  801. return werr;
  802. }
  803. static int update_block_group(struct btrfs_trans_handle *trans,
  804. struct btrfs_root *root,
  805. u64 bytenr, u64 num_bytes, int alloc,
  806. int mark_free, int data)
  807. {
  808. struct btrfs_block_group_cache *cache;
  809. struct btrfs_fs_info *info = root->fs_info;
  810. u64 total = num_bytes;
  811. u64 old_val;
  812. u64 byte_in_group;
  813. u64 start;
  814. u64 end;
  815. while(total) {
  816. cache = btrfs_lookup_block_group(info, bytenr);
  817. if (!cache) {
  818. return -1;
  819. }
  820. byte_in_group = bytenr - cache->key.objectid;
  821. WARN_ON(byte_in_group > cache->key.offset);
  822. start = cache->key.objectid;
  823. end = start + cache->key.offset - 1;
  824. set_extent_bits(&info->block_group_cache, start, end,
  825. BLOCK_GROUP_DIRTY, GFP_NOFS);
  826. old_val = btrfs_block_group_used(&cache->item);
  827. num_bytes = min(total, cache->key.offset - byte_in_group);
  828. if (alloc) {
  829. if (cache->data != data &&
  830. old_val < (cache->key.offset >> 1)) {
  831. int bit_to_clear;
  832. int bit_to_set;
  833. cache->data = data;
  834. if (data) {
  835. bit_to_clear = BLOCK_GROUP_METADATA;
  836. bit_to_set = BLOCK_GROUP_DATA;
  837. cache->item.flags &=
  838. ~BTRFS_BLOCK_GROUP_MIXED;
  839. cache->item.flags |=
  840. BTRFS_BLOCK_GROUP_DATA;
  841. } else {
  842. bit_to_clear = BLOCK_GROUP_DATA;
  843. bit_to_set = BLOCK_GROUP_METADATA;
  844. cache->item.flags &=
  845. ~BTRFS_BLOCK_GROUP_MIXED;
  846. cache->item.flags &=
  847. ~BTRFS_BLOCK_GROUP_DATA;
  848. }
  849. clear_extent_bits(&info->block_group_cache,
  850. start, end, bit_to_clear,
  851. GFP_NOFS);
  852. set_extent_bits(&info->block_group_cache,
  853. start, end, bit_to_set,
  854. GFP_NOFS);
  855. } else if (cache->data != data &&
  856. cache->data != BTRFS_BLOCK_GROUP_MIXED) {
  857. cache->data = BTRFS_BLOCK_GROUP_MIXED;
  858. set_extent_bits(&info->block_group_cache,
  859. start, end,
  860. BLOCK_GROUP_DATA |
  861. BLOCK_GROUP_METADATA,
  862. GFP_NOFS);
  863. }
  864. old_val += num_bytes;
  865. } else {
  866. old_val -= num_bytes;
  867. if (mark_free) {
  868. set_extent_dirty(&info->free_space_cache,
  869. bytenr, bytenr + num_bytes - 1,
  870. GFP_NOFS);
  871. }
  872. }
  873. btrfs_set_block_group_used(&cache->item, old_val);
  874. total -= num_bytes;
  875. bytenr += num_bytes;
  876. }
  877. return 0;
  878. }
  879. static int update_pinned_extents(struct btrfs_root *root,
  880. u64 bytenr, u64 num, int pin)
  881. {
  882. u64 len;
  883. struct btrfs_block_group_cache *cache;
  884. struct btrfs_fs_info *fs_info = root->fs_info;
  885. if (pin) {
  886. set_extent_dirty(&fs_info->pinned_extents,
  887. bytenr, bytenr + num - 1, GFP_NOFS);
  888. } else {
  889. clear_extent_dirty(&fs_info->pinned_extents,
  890. bytenr, bytenr + num - 1, GFP_NOFS);
  891. }
  892. while (num > 0) {
  893. cache = btrfs_lookup_block_group(fs_info, bytenr);
  894. WARN_ON(!cache);
  895. len = min(num, cache->key.offset -
  896. (bytenr - cache->key.objectid));
  897. if (pin) {
  898. cache->pinned += len;
  899. fs_info->total_pinned += len;
  900. } else {
  901. cache->pinned -= len;
  902. fs_info->total_pinned -= len;
  903. }
  904. bytenr += len;
  905. num -= len;
  906. }
  907. return 0;
  908. }
  909. int btrfs_copy_pinned(struct btrfs_root *root, struct extent_map_tree *copy)
  910. {
  911. u64 last = 0;
  912. u64 start;
  913. u64 end;
  914. struct extent_map_tree *pinned_extents = &root->fs_info->pinned_extents;
  915. int ret;
  916. while(1) {
  917. ret = find_first_extent_bit(pinned_extents, last,
  918. &start, &end, EXTENT_DIRTY);
  919. if (ret)
  920. break;
  921. set_extent_dirty(copy, start, end, GFP_NOFS);
  922. last = end + 1;
  923. }
  924. return 0;
  925. }
  926. int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
  927. struct btrfs_root *root,
  928. struct extent_map_tree *unpin)
  929. {
  930. u64 start;
  931. u64 end;
  932. int ret;
  933. struct extent_map_tree *free_space_cache;
  934. free_space_cache = &root->fs_info->free_space_cache;
  935. while(1) {
  936. ret = find_first_extent_bit(unpin, 0, &start, &end,
  937. EXTENT_DIRTY);
  938. if (ret)
  939. break;
  940. update_pinned_extents(root, start, end + 1 - start, 0);
  941. clear_extent_dirty(unpin, start, end, GFP_NOFS);
  942. set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
  943. }
  944. return 0;
  945. }
  946. static int finish_current_insert(struct btrfs_trans_handle *trans, struct
  947. btrfs_root *extent_root)
  948. {
  949. u64 start;
  950. u64 end;
  951. struct btrfs_fs_info *info = extent_root->fs_info;
  952. struct extent_buffer *eb;
  953. struct btrfs_path *path;
  954. struct btrfs_key ins;
  955. struct btrfs_disk_key first;
  956. struct btrfs_extent_item extent_item;
  957. int ret;
  958. int level;
  959. int err = 0;
  960. btrfs_set_stack_extent_refs(&extent_item, 1);
  961. btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
  962. path = btrfs_alloc_path();
  963. while(1) {
  964. ret = find_first_extent_bit(&info->extent_ins, 0, &start,
  965. &end, EXTENT_LOCKED);
  966. if (ret)
  967. break;
  968. ins.objectid = start;
  969. ins.offset = end + 1 - start;
  970. err = btrfs_insert_item(trans, extent_root, &ins,
  971. &extent_item, sizeof(extent_item));
  972. clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
  973. GFP_NOFS);
  974. eb = read_tree_block(extent_root, ins.objectid, ins.offset);
  975. level = btrfs_header_level(eb);
  976. if (level == 0) {
  977. btrfs_item_key(eb, &first, 0);
  978. } else {
  979. btrfs_node_key(eb, &first, 0);
  980. }
  981. err = btrfs_insert_extent_backref(trans, extent_root, path,
  982. start, extent_root->root_key.objectid,
  983. 0, level,
  984. btrfs_disk_key_objectid(&first));
  985. BUG_ON(err);
  986. free_extent_buffer(eb);
  987. }
  988. btrfs_free_path(path);
  989. return 0;
  990. }
  991. static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
  992. int pending)
  993. {
  994. int err = 0;
  995. struct extent_buffer *buf;
  996. if (!pending) {
  997. buf = btrfs_find_tree_block(root, bytenr, num_bytes);
  998. if (buf) {
  999. if (btrfs_buffer_uptodate(buf)) {
  1000. u64 transid =
  1001. root->fs_info->running_transaction->transid;
  1002. if (btrfs_header_generation(buf) == transid) {
  1003. free_extent_buffer(buf);
  1004. return 1;
  1005. }
  1006. }
  1007. free_extent_buffer(buf);
  1008. }
  1009. update_pinned_extents(root, bytenr, num_bytes, 1);
  1010. } else {
  1011. set_extent_bits(&root->fs_info->pending_del,
  1012. bytenr, bytenr + num_bytes - 1,
  1013. EXTENT_LOCKED, GFP_NOFS);
  1014. }
  1015. BUG_ON(err < 0);
  1016. return 0;
  1017. }
  1018. /*
  1019. * remove an extent from the root, returns 0 on success
  1020. */
  1021. static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
  1022. *root, u64 bytenr, u64 num_bytes,
  1023. u64 root_objectid, u64 ref_generation,
  1024. u64 owner_objectid, u64 owner_offset, int pin,
  1025. int mark_free)
  1026. {
  1027. struct btrfs_path *path;
  1028. struct btrfs_key key;
  1029. struct btrfs_fs_info *info = root->fs_info;
  1030. struct btrfs_root *extent_root = info->extent_root;
  1031. struct extent_buffer *leaf;
  1032. int ret;
  1033. struct btrfs_extent_item *ei;
  1034. u32 refs;
  1035. key.objectid = bytenr;
  1036. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  1037. key.offset = num_bytes;
  1038. path = btrfs_alloc_path();
  1039. if (!path)
  1040. return -ENOMEM;
  1041. if (ref_generation && owner_objectid == 0 && root_objectid == 3) {
  1042. //printk("drop backref root %Lu gen %Lu byte %Lu\n", root_objectid, ref_generation, bytenr );
  1043. }
  1044. ret = lookup_extent_backref(trans, extent_root, path,
  1045. bytenr, root_objectid,
  1046. ref_generation,
  1047. owner_objectid, owner_offset, 1);
  1048. if (ret == 0) {
  1049. ret = btrfs_del_item(trans, extent_root, path);
  1050. } else {
  1051. btrfs_print_leaf(extent_root, path->nodes[0]);
  1052. WARN_ON(1);
  1053. printk("Unable to find ref byte nr %Lu root %Lu "
  1054. " gen %Lu owner %Lu offset %Lu\n", bytenr,
  1055. root_objectid, ref_generation, owner_objectid,
  1056. owner_offset);
  1057. }
  1058. btrfs_release_path(extent_root, path);
  1059. ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
  1060. if (ret < 0)
  1061. return ret;
  1062. BUG_ON(ret);
  1063. leaf = path->nodes[0];
  1064. ei = btrfs_item_ptr(leaf, path->slots[0],
  1065. struct btrfs_extent_item);
  1066. refs = btrfs_extent_refs(leaf, ei);
  1067. BUG_ON(refs == 0);
  1068. refs -= 1;
  1069. btrfs_set_extent_refs(leaf, ei, refs);
  1070. btrfs_mark_buffer_dirty(leaf);
  1071. if (refs == 0) {
  1072. u64 super_used;
  1073. u64 root_used;
  1074. if (pin) {
  1075. ret = pin_down_bytes(root, bytenr, num_bytes, 0);
  1076. if (ret > 0)
  1077. mark_free = 1;
  1078. BUG_ON(ret < 0);
  1079. }
  1080. /* block accounting for super block */
  1081. super_used = btrfs_super_bytes_used(&info->super_copy);
  1082. btrfs_set_super_bytes_used(&info->super_copy,
  1083. super_used - num_bytes);
  1084. /* block accounting for root item */
  1085. root_used = btrfs_root_used(&root->root_item);
  1086. btrfs_set_root_used(&root->root_item,
  1087. root_used - num_bytes);
  1088. ret = btrfs_del_item(trans, extent_root, path);
  1089. if (ret) {
  1090. return ret;
  1091. }
  1092. ret = update_block_group(trans, root, bytenr, num_bytes, 0,
  1093. mark_free, 0);
  1094. BUG_ON(ret);
  1095. }
  1096. btrfs_free_path(path);
  1097. finish_current_insert(trans, extent_root);
  1098. return ret;
  1099. }
  1100. /*
  1101. * find all the blocks marked as pending in the radix tree and remove
  1102. * them from the extent map
  1103. */
  1104. static int del_pending_extents(struct btrfs_trans_handle *trans, struct
  1105. btrfs_root *extent_root)
  1106. {
  1107. int ret;
  1108. int err = 0;
  1109. u64 start;
  1110. u64 end;
  1111. struct extent_map_tree *pending_del;
  1112. struct extent_map_tree *pinned_extents;
  1113. pending_del = &extent_root->fs_info->pending_del;
  1114. pinned_extents = &extent_root->fs_info->pinned_extents;
  1115. while(1) {
  1116. ret = find_first_extent_bit(pending_del, 0, &start, &end,
  1117. EXTENT_LOCKED);
  1118. if (ret)
  1119. break;
  1120. update_pinned_extents(extent_root, start, end + 1 - start, 1);
  1121. clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
  1122. GFP_NOFS);
  1123. ret = __free_extent(trans, extent_root,
  1124. start, end + 1 - start,
  1125. extent_root->root_key.objectid,
  1126. 0, 0, 0, 0, 0);
  1127. if (ret)
  1128. err = ret;
  1129. }
  1130. return err;
  1131. }
  1132. /*
  1133. * remove an extent from the root, returns 0 on success
  1134. */
  1135. int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
  1136. *root, u64 bytenr, u64 num_bytes,
  1137. u64 root_objectid, u64 ref_generation,
  1138. u64 owner_objectid, u64 owner_offset, int pin)
  1139. {
  1140. struct btrfs_root *extent_root = root->fs_info->extent_root;
  1141. int pending_ret;
  1142. int ret;
  1143. WARN_ON(num_bytes < root->sectorsize);
  1144. if (!root->ref_cows)
  1145. ref_generation = 0;
  1146. if (root == extent_root) {
  1147. pin_down_bytes(root, bytenr, num_bytes, 1);
  1148. return 0;
  1149. }
  1150. ret = __free_extent(trans, root, bytenr, num_bytes, root_objectid,
  1151. ref_generation, owner_objectid, owner_offset,
  1152. pin, pin == 0);
  1153. pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
  1154. return ret ? ret : pending_ret;
  1155. }
  1156. static u64 stripe_align(struct btrfs_root *root, u64 val)
  1157. {
  1158. u64 mask = ((u64)root->stripesize - 1);
  1159. u64 ret = (val + mask) & ~mask;
  1160. return ret;
  1161. }
  1162. /*
  1163. * walks the btree of allocated extents and find a hole of a given size.
  1164. * The key ins is changed to record the hole:
  1165. * ins->objectid == block start
  1166. * ins->flags = BTRFS_EXTENT_ITEM_KEY
  1167. * ins->offset == number of blocks
  1168. * Any available blocks before search_start are skipped.
  1169. */
  1170. static int find_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
  1171. *orig_root, u64 num_bytes, u64 empty_size,
  1172. u64 search_start, u64 search_end, u64 hint_byte,
  1173. struct btrfs_key *ins, u64 exclude_start,
  1174. u64 exclude_nr, int data)
  1175. {
  1176. struct btrfs_path *path;
  1177. struct btrfs_key key;
  1178. u64 hole_size = 0;
  1179. u64 aligned;
  1180. int ret;
  1181. int slot = 0;
  1182. u64 last_byte = 0;
  1183. u64 orig_search_start = search_start;
  1184. int start_found;
  1185. struct extent_buffer *l;
  1186. struct btrfs_root * root = orig_root->fs_info->extent_root;
  1187. struct btrfs_fs_info *info = root->fs_info;
  1188. u64 total_needed = num_bytes;
  1189. int level;
  1190. struct btrfs_block_group_cache *block_group;
  1191. int full_scan = 0;
  1192. int wrapped = 0;
  1193. u64 cached_start;
  1194. WARN_ON(num_bytes < root->sectorsize);
  1195. btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
  1196. level = btrfs_header_level(root->node);
  1197. if (num_bytes >= 32 * 1024 * 1024 && hint_byte) {
  1198. data = BTRFS_BLOCK_GROUP_MIXED;
  1199. }
  1200. if (search_end == (u64)-1)
  1201. search_end = btrfs_super_total_bytes(&info->super_copy);
  1202. if (hint_byte) {
  1203. block_group = btrfs_lookup_block_group(info, hint_byte);
  1204. if (!block_group)
  1205. hint_byte = search_start;
  1206. block_group = btrfs_find_block_group(root, block_group,
  1207. hint_byte, data, 1);
  1208. } else {
  1209. block_group = btrfs_find_block_group(root,
  1210. trans->block_group,
  1211. search_start, data, 1);
  1212. }
  1213. total_needed += empty_size;
  1214. path = btrfs_alloc_path();
  1215. check_failed:
  1216. if (!block_group) {
  1217. block_group = btrfs_lookup_block_group(info, search_start);
  1218. if (!block_group)
  1219. block_group = btrfs_lookup_block_group(info,
  1220. orig_search_start);
  1221. }
  1222. search_start = find_search_start(root, &block_group, search_start,
  1223. total_needed, data, full_scan);
  1224. search_start = stripe_align(root, search_start);
  1225. cached_start = search_start;
  1226. btrfs_init_path(path);
  1227. ins->objectid = search_start;
  1228. ins->offset = 0;
  1229. start_found = 0;
  1230. path->reada = 2;
  1231. ret = btrfs_search_slot(trans, root, ins, path, 0, 0);
  1232. if (ret < 0)
  1233. goto error;
  1234. if (path->slots[0] > 0) {
  1235. path->slots[0]--;
  1236. }
  1237. l = path->nodes[0];
  1238. btrfs_item_key_to_cpu(l, &key, path->slots[0]);
  1239. /*
  1240. * walk backwards to find the first extent item key
  1241. */
  1242. while(btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
  1243. if (path->slots[0] == 0) {
  1244. ret = btrfs_prev_leaf(root, path);
  1245. if (ret != 0) {
  1246. ret = btrfs_search_slot(trans, root, ins,
  1247. path, 0, 0);
  1248. if (ret < 0)
  1249. goto error;
  1250. if (path->slots[0] > 0)
  1251. path->slots[0]--;
  1252. break;
  1253. }
  1254. } else {
  1255. path->slots[0]--;
  1256. }
  1257. l = path->nodes[0];
  1258. btrfs_item_key_to_cpu(l, &key, path->slots[0]);
  1259. }
  1260. while (1) {
  1261. l = path->nodes[0];
  1262. slot = path->slots[0];
  1263. if (slot >= btrfs_header_nritems(l)) {
  1264. ret = btrfs_next_leaf(root, path);
  1265. if (ret == 0)
  1266. continue;
  1267. if (ret < 0)
  1268. goto error;
  1269. search_start = max(search_start,
  1270. block_group->key.objectid);
  1271. if (!start_found) {
  1272. aligned = stripe_align(root, search_start);
  1273. ins->objectid = aligned;
  1274. if (aligned >= search_end) {
  1275. ret = -ENOSPC;
  1276. goto error;
  1277. }
  1278. ins->offset = search_end - aligned;
  1279. start_found = 1;
  1280. goto check_pending;
  1281. }
  1282. ins->objectid = stripe_align(root,
  1283. last_byte > search_start ?
  1284. last_byte : search_start);
  1285. if (search_end <= ins->objectid) {
  1286. ret = -ENOSPC;
  1287. goto error;
  1288. }
  1289. ins->offset = search_end - ins->objectid;
  1290. BUG_ON(ins->objectid >= search_end);
  1291. goto check_pending;
  1292. }
  1293. btrfs_item_key_to_cpu(l, &key, slot);
  1294. if (key.objectid >= search_start && key.objectid > last_byte &&
  1295. start_found) {
  1296. if (last_byte < search_start)
  1297. last_byte = search_start;
  1298. aligned = stripe_align(root, last_byte);
  1299. hole_size = key.objectid - aligned;
  1300. if (key.objectid > aligned && hole_size >= num_bytes) {
  1301. ins->objectid = aligned;
  1302. ins->offset = hole_size;
  1303. goto check_pending;
  1304. }
  1305. }
  1306. if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY) {
  1307. if (!start_found && btrfs_key_type(&key) ==
  1308. BTRFS_BLOCK_GROUP_ITEM_KEY) {
  1309. last_byte = key.objectid;
  1310. start_found = 1;
  1311. }
  1312. goto next;
  1313. }
  1314. start_found = 1;
  1315. last_byte = key.objectid + key.offset;
  1316. if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
  1317. last_byte >= block_group->key.objectid +
  1318. block_group->key.offset) {
  1319. btrfs_release_path(root, path);
  1320. search_start = block_group->key.objectid +
  1321. block_group->key.offset;
  1322. goto new_group;
  1323. }
  1324. next:
  1325. path->slots[0]++;
  1326. cond_resched();
  1327. }
  1328. check_pending:
  1329. /* we have to make sure we didn't find an extent that has already
  1330. * been allocated by the map tree or the original allocation
  1331. */
  1332. btrfs_release_path(root, path);
  1333. BUG_ON(ins->objectid < search_start);
  1334. if (ins->objectid + num_bytes >= search_end)
  1335. goto enospc;
  1336. if (!full_scan && data != BTRFS_BLOCK_GROUP_MIXED &&
  1337. ins->objectid + num_bytes > block_group->
  1338. key.objectid + block_group->key.offset) {
  1339. search_start = block_group->key.objectid +
  1340. block_group->key.offset;
  1341. goto new_group;
  1342. }
  1343. if (test_range_bit(&info->extent_ins, ins->objectid,
  1344. ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
  1345. search_start = ins->objectid + num_bytes;
  1346. goto new_group;
  1347. }
  1348. if (test_range_bit(&info->pinned_extents, ins->objectid,
  1349. ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
  1350. search_start = ins->objectid + num_bytes;
  1351. goto new_group;
  1352. }
  1353. if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
  1354. ins->objectid < exclude_start + exclude_nr)) {
  1355. search_start = exclude_start + exclude_nr;
  1356. goto new_group;
  1357. }
  1358. if (!data) {
  1359. block_group = btrfs_lookup_block_group(info, ins->objectid);
  1360. if (block_group)
  1361. trans->block_group = block_group;
  1362. }
  1363. ins->offset = num_bytes;
  1364. btrfs_free_path(path);
  1365. return 0;
  1366. new_group:
  1367. if (search_start + num_bytes >= search_end) {
  1368. enospc:
  1369. search_start = orig_search_start;
  1370. if (full_scan) {
  1371. ret = -ENOSPC;
  1372. goto error;
  1373. }
  1374. if (wrapped) {
  1375. if (!full_scan)
  1376. total_needed -= empty_size;
  1377. full_scan = 1;
  1378. data = BTRFS_BLOCK_GROUP_MIXED;
  1379. } else
  1380. wrapped = 1;
  1381. }
  1382. block_group = btrfs_lookup_block_group(info, search_start);
  1383. cond_resched();
  1384. block_group = btrfs_find_block_group(root, block_group,
  1385. search_start, data, 0);
  1386. goto check_failed;
  1387. error:
  1388. btrfs_release_path(root, path);
  1389. btrfs_free_path(path);
  1390. return ret;
  1391. }
  1392. /*
  1393. * finds a free extent and does all the dirty work required for allocation
  1394. * returns the key for the extent through ins, and a tree buffer for
  1395. * the first block of the extent through buf.
  1396. *
  1397. * returns 0 if everything worked, non-zero otherwise.
  1398. */
  1399. int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
  1400. struct btrfs_root *root,
  1401. u64 num_bytes, u64 root_objectid, u64 ref_generation,
  1402. u64 owner, u64 owner_offset,
  1403. u64 empty_size, u64 hint_byte,
  1404. u64 search_end, struct btrfs_key *ins, int data)
  1405. {
  1406. int ret;
  1407. int pending_ret;
  1408. u64 super_used, root_used;
  1409. u64 search_start = 0;
  1410. struct btrfs_fs_info *info = root->fs_info;
  1411. struct btrfs_root *extent_root = info->extent_root;
  1412. struct btrfs_extent_item extent_item;
  1413. struct btrfs_path *path;
  1414. btrfs_set_stack_extent_refs(&extent_item, 1);
  1415. WARN_ON(num_bytes < root->sectorsize);
  1416. ret = find_free_extent(trans, root, num_bytes, empty_size,
  1417. search_start, search_end, hint_byte, ins,
  1418. trans->alloc_exclude_start,
  1419. trans->alloc_exclude_nr, data);
  1420. BUG_ON(ret);
  1421. if (ret)
  1422. return ret;
  1423. /* block accounting for super block */
  1424. super_used = btrfs_super_bytes_used(&info->super_copy);
  1425. btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
  1426. /* block accounting for root item */
  1427. root_used = btrfs_root_used(&root->root_item);
  1428. btrfs_set_root_used(&root->root_item, root_used + num_bytes);
  1429. clear_extent_dirty(&root->fs_info->free_space_cache,
  1430. ins->objectid, ins->objectid + ins->offset - 1,
  1431. GFP_NOFS);
  1432. if (root == extent_root) {
  1433. set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
  1434. ins->objectid + ins->offset - 1,
  1435. EXTENT_LOCKED, GFP_NOFS);
  1436. WARN_ON(data == 1);
  1437. goto update_block;
  1438. }
  1439. WARN_ON(trans->alloc_exclude_nr);
  1440. trans->alloc_exclude_start = ins->objectid;
  1441. trans->alloc_exclude_nr = ins->offset;
  1442. ret = btrfs_insert_item(trans, extent_root, ins, &extent_item,
  1443. sizeof(extent_item));
  1444. trans->alloc_exclude_start = 0;
  1445. trans->alloc_exclude_nr = 0;
  1446. BUG_ON(ret);
  1447. path = btrfs_alloc_path();
  1448. BUG_ON(!path);
  1449. ret = btrfs_insert_extent_backref(trans, extent_root, path,
  1450. ins->objectid, root_objectid,
  1451. ref_generation, owner, owner_offset);
  1452. BUG_ON(ret);
  1453. btrfs_free_path(path);
  1454. finish_current_insert(trans, extent_root);
  1455. pending_ret = del_pending_extents(trans, extent_root);
  1456. if (ret) {
  1457. return ret;
  1458. }
  1459. if (pending_ret) {
  1460. return pending_ret;
  1461. }
  1462. update_block:
  1463. ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0,
  1464. data);
  1465. BUG_ON(ret);
  1466. return 0;
  1467. }
  1468. /*
  1469. * helper function to allocate a block for a given tree
  1470. * returns the tree buffer or NULL.
  1471. */
  1472. struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
  1473. struct btrfs_root *root,
  1474. u32 blocksize,
  1475. u64 root_objectid, u64 hint,
  1476. u64 empty_size)
  1477. {
  1478. u64 ref_generation;
  1479. if (root->ref_cows)
  1480. ref_generation = trans->transid;
  1481. else
  1482. ref_generation = 0;
  1483. return __btrfs_alloc_free_block(trans, root, blocksize, root_objectid,
  1484. ref_generation, 0, 0, hint, empty_size);
  1485. }
  1486. /*
  1487. * helper function to allocate a block for a given tree
  1488. * returns the tree buffer or NULL.
  1489. */
  1490. struct extent_buffer *__btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
  1491. struct btrfs_root *root,
  1492. u32 blocksize,
  1493. u64 root_objectid,
  1494. u64 ref_generation,
  1495. u64 first_objectid,
  1496. int level,
  1497. u64 hint,
  1498. u64 empty_size)
  1499. {
  1500. struct btrfs_key ins;
  1501. int ret;
  1502. struct extent_buffer *buf;
  1503. ret = btrfs_alloc_extent(trans, root, blocksize,
  1504. root_objectid, ref_generation,
  1505. level, first_objectid, empty_size, hint,
  1506. (u64)-1, &ins, 0);
  1507. if (ret) {
  1508. BUG_ON(ret > 0);
  1509. return ERR_PTR(ret);
  1510. }
  1511. buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
  1512. if (!buf) {
  1513. btrfs_free_extent(trans, root, ins.objectid, blocksize,
  1514. root->root_key.objectid, ref_generation,
  1515. 0, 0, 0);
  1516. return ERR_PTR(-ENOMEM);
  1517. }
  1518. btrfs_set_buffer_uptodate(buf);
  1519. set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
  1520. buf->start + buf->len - 1, GFP_NOFS);
  1521. set_extent_bits(&BTRFS_I(root->fs_info->btree_inode)->extent_tree,
  1522. buf->start, buf->start + buf->len - 1,
  1523. EXTENT_CSUM, GFP_NOFS);
  1524. buf->flags |= EXTENT_CSUM;
  1525. btrfs_set_buffer_defrag(buf);
  1526. trans->blocks_used++;
  1527. return buf;
  1528. }
  1529. static int drop_leaf_ref(struct btrfs_trans_handle *trans,
  1530. struct btrfs_root *root, struct extent_buffer *leaf)
  1531. {
  1532. u64 leaf_owner;
  1533. u64 leaf_generation;
  1534. struct btrfs_key key;
  1535. struct btrfs_file_extent_item *fi;
  1536. int i;
  1537. int nritems;
  1538. int ret;
  1539. BUG_ON(!btrfs_is_leaf(leaf));
  1540. nritems = btrfs_header_nritems(leaf);
  1541. leaf_owner = btrfs_header_owner(leaf);
  1542. leaf_generation = btrfs_header_generation(leaf);
  1543. for (i = 0; i < nritems; i++) {
  1544. u64 disk_bytenr;
  1545. btrfs_item_key_to_cpu(leaf, &key, i);
  1546. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  1547. continue;
  1548. fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
  1549. if (btrfs_file_extent_type(leaf, fi) ==
  1550. BTRFS_FILE_EXTENT_INLINE)
  1551. continue;
  1552. /*
  1553. * FIXME make sure to insert a trans record that
  1554. * repeats the snapshot del on crash
  1555. */
  1556. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  1557. if (disk_bytenr == 0)
  1558. continue;
  1559. ret = btrfs_free_extent(trans, root, disk_bytenr,
  1560. btrfs_file_extent_disk_num_bytes(leaf, fi),
  1561. leaf_owner, leaf_generation,
  1562. key.objectid, key.offset, 0);
  1563. BUG_ON(ret);
  1564. }
  1565. return 0;
  1566. }
  1567. static void reada_walk_down(struct btrfs_root *root,
  1568. struct extent_buffer *node)
  1569. {
  1570. int i;
  1571. u32 nritems;
  1572. u64 bytenr;
  1573. int ret;
  1574. u32 refs;
  1575. int level;
  1576. u32 blocksize;
  1577. nritems = btrfs_header_nritems(node);
  1578. level = btrfs_header_level(node);
  1579. for (i = 0; i < nritems; i++) {
  1580. bytenr = btrfs_node_blockptr(node, i);
  1581. blocksize = btrfs_level_size(root, level - 1);
  1582. ret = lookup_extent_ref(NULL, root, bytenr, blocksize, &refs);
  1583. BUG_ON(ret);
  1584. if (refs != 1)
  1585. continue;
  1586. mutex_unlock(&root->fs_info->fs_mutex);
  1587. ret = readahead_tree_block(root, bytenr, blocksize);
  1588. cond_resched();
  1589. mutex_lock(&root->fs_info->fs_mutex);
  1590. if (ret)
  1591. break;
  1592. }
  1593. }
  1594. /*
  1595. * helper function for drop_snapshot, this walks down the tree dropping ref
  1596. * counts as it goes.
  1597. */
  1598. static int walk_down_tree(struct btrfs_trans_handle *trans, struct btrfs_root
  1599. *root, struct btrfs_path *path, int *level)
  1600. {
  1601. u64 root_owner;
  1602. u64 root_gen;
  1603. u64 bytenr;
  1604. struct extent_buffer *next;
  1605. struct extent_buffer *cur;
  1606. struct extent_buffer *parent;
  1607. u32 blocksize;
  1608. int ret;
  1609. u32 refs;
  1610. WARN_ON(*level < 0);
  1611. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  1612. ret = lookup_extent_ref(trans, root,
  1613. path->nodes[*level]->start,
  1614. path->nodes[*level]->len, &refs);
  1615. BUG_ON(ret);
  1616. if (refs > 1)
  1617. goto out;
  1618. /*
  1619. * walk down to the last node level and free all the leaves
  1620. */
  1621. while(*level >= 0) {
  1622. WARN_ON(*level < 0);
  1623. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  1624. cur = path->nodes[*level];
  1625. if (*level > 0 && path->slots[*level] == 0)
  1626. reada_walk_down(root, cur);
  1627. if (btrfs_header_level(cur) != *level)
  1628. WARN_ON(1);
  1629. if (path->slots[*level] >=
  1630. btrfs_header_nritems(cur))
  1631. break;
  1632. if (*level == 0) {
  1633. ret = drop_leaf_ref(trans, root, cur);
  1634. BUG_ON(ret);
  1635. break;
  1636. }
  1637. bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
  1638. blocksize = btrfs_level_size(root, *level - 1);
  1639. ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
  1640. BUG_ON(ret);
  1641. if (refs != 1) {
  1642. parent = path->nodes[*level];
  1643. root_owner = btrfs_header_owner(parent);
  1644. root_gen = btrfs_header_generation(parent);
  1645. path->slots[*level]++;
  1646. ret = btrfs_free_extent(trans, root, bytenr,
  1647. blocksize, root_owner,
  1648. root_gen, 0, 0, 1);
  1649. BUG_ON(ret);
  1650. continue;
  1651. }
  1652. next = btrfs_find_tree_block(root, bytenr, blocksize);
  1653. if (!next || !btrfs_buffer_uptodate(next)) {
  1654. free_extent_buffer(next);
  1655. mutex_unlock(&root->fs_info->fs_mutex);
  1656. next = read_tree_block(root, bytenr, blocksize);
  1657. mutex_lock(&root->fs_info->fs_mutex);
  1658. /* we dropped the lock, check one more time */
  1659. ret = lookup_extent_ref(trans, root, bytenr,
  1660. blocksize, &refs);
  1661. BUG_ON(ret);
  1662. if (refs != 1) {
  1663. parent = path->nodes[*level];
  1664. root_owner = btrfs_header_owner(parent);
  1665. root_gen = btrfs_header_generation(parent);
  1666. path->slots[*level]++;
  1667. free_extent_buffer(next);
  1668. ret = btrfs_free_extent(trans, root, bytenr,
  1669. blocksize,
  1670. root_owner,
  1671. root_gen, 0, 0, 1);
  1672. BUG_ON(ret);
  1673. continue;
  1674. }
  1675. }
  1676. WARN_ON(*level <= 0);
  1677. if (path->nodes[*level-1])
  1678. free_extent_buffer(path->nodes[*level-1]);
  1679. path->nodes[*level-1] = next;
  1680. *level = btrfs_header_level(next);
  1681. path->slots[*level] = 0;
  1682. }
  1683. out:
  1684. WARN_ON(*level < 0);
  1685. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  1686. if (path->nodes[*level] == root->node) {
  1687. root_owner = root->root_key.objectid;
  1688. parent = path->nodes[*level];
  1689. } else {
  1690. parent = path->nodes[*level + 1];
  1691. root_owner = btrfs_header_owner(parent);
  1692. }
  1693. root_gen = btrfs_header_generation(parent);
  1694. ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
  1695. path->nodes[*level]->len,
  1696. root_owner, root_gen, 0, 0, 1);
  1697. free_extent_buffer(path->nodes[*level]);
  1698. path->nodes[*level] = NULL;
  1699. *level += 1;
  1700. BUG_ON(ret);
  1701. return 0;
  1702. }
  1703. /*
  1704. * helper for dropping snapshots. This walks back up the tree in the path
  1705. * to find the first node higher up where we haven't yet gone through
  1706. * all the slots
  1707. */
  1708. static int walk_up_tree(struct btrfs_trans_handle *trans, struct btrfs_root
  1709. *root, struct btrfs_path *path, int *level)
  1710. {
  1711. u64 root_owner;
  1712. u64 root_gen;
  1713. struct btrfs_root_item *root_item = &root->root_item;
  1714. int i;
  1715. int slot;
  1716. int ret;
  1717. for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
  1718. slot = path->slots[i];
  1719. if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
  1720. struct extent_buffer *node;
  1721. struct btrfs_disk_key disk_key;
  1722. node = path->nodes[i];
  1723. path->slots[i]++;
  1724. *level = i;
  1725. WARN_ON(*level == 0);
  1726. btrfs_node_key(node, &disk_key, path->slots[i]);
  1727. memcpy(&root_item->drop_progress,
  1728. &disk_key, sizeof(disk_key));
  1729. root_item->drop_level = i;
  1730. return 0;
  1731. } else {
  1732. if (path->nodes[*level] == root->node) {
  1733. root_owner = root->root_key.objectid;
  1734. root_gen =
  1735. btrfs_header_generation(path->nodes[*level]);
  1736. } else {
  1737. struct extent_buffer *node;
  1738. node = path->nodes[*level + 1];
  1739. root_owner = btrfs_header_owner(node);
  1740. root_gen = btrfs_header_generation(node);
  1741. }
  1742. ret = btrfs_free_extent(trans, root,
  1743. path->nodes[*level]->start,
  1744. path->nodes[*level]->len,
  1745. root_owner, root_gen, 0, 0, 1);
  1746. BUG_ON(ret);
  1747. free_extent_buffer(path->nodes[*level]);
  1748. path->nodes[*level] = NULL;
  1749. *level = i + 1;
  1750. }
  1751. }
  1752. return 1;
  1753. }
  1754. /*
  1755. * drop the reference count on the tree rooted at 'snap'. This traverses
  1756. * the tree freeing any blocks that have a ref count of zero after being
  1757. * decremented.
  1758. */
  1759. int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
  1760. *root)
  1761. {
  1762. int ret = 0;
  1763. int wret;
  1764. int level;
  1765. struct btrfs_path *path;
  1766. int i;
  1767. int orig_level;
  1768. struct btrfs_root_item *root_item = &root->root_item;
  1769. path = btrfs_alloc_path();
  1770. BUG_ON(!path);
  1771. level = btrfs_header_level(root->node);
  1772. orig_level = level;
  1773. if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
  1774. path->nodes[level] = root->node;
  1775. extent_buffer_get(root->node);
  1776. path->slots[level] = 0;
  1777. } else {
  1778. struct btrfs_key key;
  1779. struct btrfs_disk_key found_key;
  1780. struct extent_buffer *node;
  1781. btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
  1782. level = root_item->drop_level;
  1783. path->lowest_level = level;
  1784. wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1785. if (wret < 0) {
  1786. ret = wret;
  1787. goto out;
  1788. }
  1789. node = path->nodes[level];
  1790. btrfs_node_key(node, &found_key, path->slots[level]);
  1791. WARN_ON(memcmp(&found_key, &root_item->drop_progress,
  1792. sizeof(found_key)));
  1793. }
  1794. while(1) {
  1795. wret = walk_down_tree(trans, root, path, &level);
  1796. if (wret > 0)
  1797. break;
  1798. if (wret < 0)
  1799. ret = wret;
  1800. wret = walk_up_tree(trans, root, path, &level);
  1801. if (wret > 0)
  1802. break;
  1803. if (wret < 0)
  1804. ret = wret;
  1805. ret = -EAGAIN;
  1806. break;
  1807. }
  1808. for (i = 0; i <= orig_level; i++) {
  1809. if (path->nodes[i]) {
  1810. free_extent_buffer(path->nodes[i]);
  1811. path->nodes[i] = NULL;
  1812. }
  1813. }
  1814. out:
  1815. btrfs_free_path(path);
  1816. return ret;
  1817. }
  1818. int btrfs_free_block_groups(struct btrfs_fs_info *info)
  1819. {
  1820. u64 start;
  1821. u64 end;
  1822. u64 ptr;
  1823. int ret;
  1824. while(1) {
  1825. ret = find_first_extent_bit(&info->block_group_cache, 0,
  1826. &start, &end, (unsigned int)-1);
  1827. if (ret)
  1828. break;
  1829. ret = get_state_private(&info->block_group_cache, start, &ptr);
  1830. if (!ret)
  1831. kfree((void *)(unsigned long)ptr);
  1832. clear_extent_bits(&info->block_group_cache, start,
  1833. end, (unsigned int)-1, GFP_NOFS);
  1834. }
  1835. while(1) {
  1836. ret = find_first_extent_bit(&info->free_space_cache, 0,
  1837. &start, &end, EXTENT_DIRTY);
  1838. if (ret)
  1839. break;
  1840. clear_extent_dirty(&info->free_space_cache, start,
  1841. end, GFP_NOFS);
  1842. }
  1843. return 0;
  1844. }
  1845. int btrfs_read_block_groups(struct btrfs_root *root)
  1846. {
  1847. struct btrfs_path *path;
  1848. int ret;
  1849. int err = 0;
  1850. int bit;
  1851. struct btrfs_block_group_cache *cache;
  1852. struct btrfs_fs_info *info = root->fs_info;
  1853. struct extent_map_tree *block_group_cache;
  1854. struct btrfs_key key;
  1855. struct btrfs_key found_key;
  1856. struct extent_buffer *leaf;
  1857. block_group_cache = &info->block_group_cache;
  1858. root = info->extent_root;
  1859. key.objectid = 0;
  1860. key.offset = BTRFS_BLOCK_GROUP_SIZE;
  1861. btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
  1862. path = btrfs_alloc_path();
  1863. if (!path)
  1864. return -ENOMEM;
  1865. while(1) {
  1866. ret = btrfs_search_slot(NULL, info->extent_root,
  1867. &key, path, 0, 0);
  1868. if (ret != 0) {
  1869. err = ret;
  1870. break;
  1871. }
  1872. leaf = path->nodes[0];
  1873. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  1874. cache = kmalloc(sizeof(*cache), GFP_NOFS);
  1875. if (!cache) {
  1876. err = -1;
  1877. break;
  1878. }
  1879. read_extent_buffer(leaf, &cache->item,
  1880. btrfs_item_ptr_offset(leaf, path->slots[0]),
  1881. sizeof(cache->item));
  1882. memcpy(&cache->key, &found_key, sizeof(found_key));
  1883. cache->cached = 0;
  1884. cache->pinned = 0;
  1885. key.objectid = found_key.objectid + found_key.offset;
  1886. btrfs_release_path(root, path);
  1887. if (cache->item.flags & BTRFS_BLOCK_GROUP_MIXED) {
  1888. bit = BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA;
  1889. cache->data = BTRFS_BLOCK_GROUP_MIXED;
  1890. } else if (cache->item.flags & BTRFS_BLOCK_GROUP_DATA) {
  1891. bit = BLOCK_GROUP_DATA;
  1892. cache->data = BTRFS_BLOCK_GROUP_DATA;
  1893. } else {
  1894. bit = BLOCK_GROUP_METADATA;
  1895. cache->data = 0;
  1896. }
  1897. /* use EXTENT_LOCKED to prevent merging */
  1898. set_extent_bits(block_group_cache, found_key.objectid,
  1899. found_key.objectid + found_key.offset - 1,
  1900. bit | EXTENT_LOCKED, GFP_NOFS);
  1901. set_state_private(block_group_cache, found_key.objectid,
  1902. (unsigned long)cache);
  1903. if (key.objectid >=
  1904. btrfs_super_total_bytes(&info->super_copy))
  1905. break;
  1906. }
  1907. btrfs_free_path(path);
  1908. return 0;
  1909. }