extent-tree.c 68 KB

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