extent-tree.c 72 KB

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