extent-tree.c 95 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635
  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 <linux/writeback.h>
  21. #include "hash.h"
  22. #include "crc32c.h"
  23. #include "ctree.h"
  24. #include "disk-io.h"
  25. #include "print-tree.h"
  26. #include "transaction.h"
  27. #include "volumes.h"
  28. #include "locking.h"
  29. #include "ref-cache.h"
  30. #define BLOCK_GROUP_DATA EXTENT_WRITEBACK
  31. #define BLOCK_GROUP_METADATA EXTENT_UPTODATE
  32. #define BLOCK_GROUP_SYSTEM EXTENT_NEW
  33. #define BLOCK_GROUP_DIRTY EXTENT_DIRTY
  34. static int finish_current_insert(struct btrfs_trans_handle *trans, struct
  35. btrfs_root *extent_root);
  36. static int del_pending_extents(struct btrfs_trans_handle *trans, struct
  37. btrfs_root *extent_root);
  38. static struct btrfs_block_group_cache *
  39. __btrfs_find_block_group(struct btrfs_root *root,
  40. struct btrfs_block_group_cache *hint,
  41. u64 search_start, int data, int owner);
  42. void maybe_lock_mutex(struct btrfs_root *root)
  43. {
  44. if (root != root->fs_info->extent_root &&
  45. root != root->fs_info->chunk_root &&
  46. root != root->fs_info->dev_root) {
  47. mutex_lock(&root->fs_info->alloc_mutex);
  48. }
  49. }
  50. void maybe_unlock_mutex(struct btrfs_root *root)
  51. {
  52. if (root != root->fs_info->extent_root &&
  53. root != root->fs_info->chunk_root &&
  54. root != root->fs_info->dev_root) {
  55. mutex_unlock(&root->fs_info->alloc_mutex);
  56. }
  57. }
  58. static int cache_block_group(struct btrfs_root *root,
  59. struct btrfs_block_group_cache *block_group)
  60. {
  61. struct btrfs_path *path;
  62. int ret;
  63. struct btrfs_key key;
  64. struct extent_buffer *leaf;
  65. struct extent_io_tree *free_space_cache;
  66. int slot;
  67. u64 last = 0;
  68. u64 hole_size;
  69. u64 first_free;
  70. int found = 0;
  71. if (!block_group)
  72. return 0;
  73. root = root->fs_info->extent_root;
  74. free_space_cache = &root->fs_info->free_space_cache;
  75. if (block_group->cached)
  76. return 0;
  77. path = btrfs_alloc_path();
  78. if (!path)
  79. return -ENOMEM;
  80. path->reada = 2;
  81. /*
  82. * we get into deadlocks with paths held by callers of this function.
  83. * since the alloc_mutex is protecting things right now, just
  84. * skip the locking here
  85. */
  86. path->skip_locking = 1;
  87. first_free = block_group->key.objectid;
  88. key.objectid = block_group->key.objectid;
  89. key.offset = 0;
  90. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  91. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  92. if (ret < 0)
  93. return ret;
  94. ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
  95. if (ret < 0)
  96. return ret;
  97. if (ret == 0) {
  98. leaf = path->nodes[0];
  99. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  100. if (key.objectid + key.offset > first_free)
  101. first_free = key.objectid + key.offset;
  102. }
  103. while(1) {
  104. leaf = path->nodes[0];
  105. slot = path->slots[0];
  106. if (slot >= btrfs_header_nritems(leaf)) {
  107. ret = btrfs_next_leaf(root, path);
  108. if (ret < 0)
  109. goto err;
  110. if (ret == 0) {
  111. continue;
  112. } else {
  113. break;
  114. }
  115. }
  116. btrfs_item_key_to_cpu(leaf, &key, slot);
  117. if (key.objectid < block_group->key.objectid) {
  118. goto next;
  119. }
  120. if (key.objectid >= block_group->key.objectid +
  121. block_group->key.offset) {
  122. break;
  123. }
  124. if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
  125. if (!found) {
  126. last = first_free;
  127. found = 1;
  128. }
  129. if (key.objectid > last) {
  130. hole_size = key.objectid - last;
  131. set_extent_dirty(free_space_cache, last,
  132. last + hole_size - 1,
  133. GFP_NOFS);
  134. }
  135. last = key.objectid + key.offset;
  136. }
  137. next:
  138. path->slots[0]++;
  139. }
  140. if (!found)
  141. last = first_free;
  142. if (block_group->key.objectid +
  143. block_group->key.offset > last) {
  144. hole_size = block_group->key.objectid +
  145. block_group->key.offset - last;
  146. set_extent_dirty(free_space_cache, last,
  147. last + hole_size - 1, GFP_NOFS);
  148. }
  149. block_group->cached = 1;
  150. err:
  151. btrfs_free_path(path);
  152. return 0;
  153. }
  154. struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct
  155. btrfs_fs_info *info,
  156. u64 bytenr)
  157. {
  158. struct extent_io_tree *block_group_cache;
  159. struct btrfs_block_group_cache *block_group = NULL;
  160. u64 ptr;
  161. u64 start;
  162. u64 end;
  163. int ret;
  164. bytenr = max_t(u64, bytenr,
  165. BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
  166. block_group_cache = &info->block_group_cache;
  167. ret = find_first_extent_bit(block_group_cache,
  168. bytenr, &start, &end,
  169. BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
  170. BLOCK_GROUP_SYSTEM);
  171. if (ret) {
  172. return NULL;
  173. }
  174. ret = get_state_private(block_group_cache, start, &ptr);
  175. if (ret)
  176. return NULL;
  177. block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
  178. return block_group;
  179. }
  180. struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
  181. btrfs_fs_info *info,
  182. u64 bytenr)
  183. {
  184. struct extent_io_tree *block_group_cache;
  185. struct btrfs_block_group_cache *block_group = NULL;
  186. u64 ptr;
  187. u64 start;
  188. u64 end;
  189. int ret;
  190. bytenr = max_t(u64, bytenr,
  191. BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
  192. block_group_cache = &info->block_group_cache;
  193. ret = find_first_extent_bit(block_group_cache,
  194. bytenr, &start, &end,
  195. BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
  196. BLOCK_GROUP_SYSTEM);
  197. if (ret) {
  198. return NULL;
  199. }
  200. ret = get_state_private(block_group_cache, start, &ptr);
  201. if (ret)
  202. return NULL;
  203. block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
  204. if (block_group->key.objectid <= bytenr && bytenr <
  205. block_group->key.objectid + block_group->key.offset)
  206. return block_group;
  207. return NULL;
  208. }
  209. static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
  210. {
  211. return (cache->flags & bits) == bits;
  212. }
  213. static int noinline find_search_start(struct btrfs_root *root,
  214. struct btrfs_block_group_cache **cache_ret,
  215. u64 *start_ret, u64 num, int data)
  216. {
  217. int ret;
  218. struct btrfs_block_group_cache *cache = *cache_ret;
  219. struct extent_io_tree *free_space_cache;
  220. struct extent_state *state;
  221. u64 last;
  222. u64 start = 0;
  223. u64 cache_miss = 0;
  224. u64 total_fs_bytes;
  225. u64 search_start = *start_ret;
  226. int wrapped = 0;
  227. WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
  228. total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
  229. free_space_cache = &root->fs_info->free_space_cache;
  230. if (!cache)
  231. goto out;
  232. again:
  233. ret = cache_block_group(root, cache);
  234. if (ret) {
  235. goto out;
  236. }
  237. last = max(search_start, cache->key.objectid);
  238. if (!block_group_bits(cache, data) || cache->ro)
  239. goto new_group;
  240. spin_lock_irq(&free_space_cache->lock);
  241. state = find_first_extent_bit_state(free_space_cache, last, EXTENT_DIRTY);
  242. while(1) {
  243. if (!state) {
  244. if (!cache_miss)
  245. cache_miss = last;
  246. spin_unlock_irq(&free_space_cache->lock);
  247. goto new_group;
  248. }
  249. start = max(last, state->start);
  250. last = state->end + 1;
  251. if (last - start < num) {
  252. do {
  253. state = extent_state_next(state);
  254. } while(state && !(state->state & EXTENT_DIRTY));
  255. continue;
  256. }
  257. spin_unlock_irq(&free_space_cache->lock);
  258. if (cache->ro) {
  259. goto new_group;
  260. }
  261. if (start + num > cache->key.objectid + cache->key.offset)
  262. goto new_group;
  263. if (!block_group_bits(cache, data)) {
  264. printk("block group bits don't match %Lu %d\n", cache->flags, data);
  265. }
  266. *start_ret = start;
  267. return 0;
  268. }
  269. out:
  270. cache = btrfs_lookup_block_group(root->fs_info, search_start);
  271. if (!cache) {
  272. printk("Unable to find block group for %Lu\n", search_start);
  273. WARN_ON(1);
  274. }
  275. return -ENOSPC;
  276. new_group:
  277. last = cache->key.objectid + cache->key.offset;
  278. wrapped:
  279. cache = btrfs_lookup_first_block_group(root->fs_info, last);
  280. if (!cache || cache->key.objectid >= total_fs_bytes) {
  281. no_cache:
  282. if (!wrapped) {
  283. wrapped = 1;
  284. last = search_start;
  285. goto wrapped;
  286. }
  287. goto out;
  288. }
  289. if (cache_miss && !cache->cached) {
  290. cache_block_group(root, cache);
  291. last = cache_miss;
  292. cache = btrfs_lookup_first_block_group(root->fs_info, last);
  293. }
  294. cache_miss = 0;
  295. cache = btrfs_find_block_group(root, cache, last, data, 0);
  296. if (!cache)
  297. goto no_cache;
  298. *cache_ret = cache;
  299. goto again;
  300. }
  301. static u64 div_factor(u64 num, int factor)
  302. {
  303. if (factor == 10)
  304. return num;
  305. num *= factor;
  306. do_div(num, 10);
  307. return num;
  308. }
  309. static int block_group_state_bits(u64 flags)
  310. {
  311. int bits = 0;
  312. if (flags & BTRFS_BLOCK_GROUP_DATA)
  313. bits |= BLOCK_GROUP_DATA;
  314. if (flags & BTRFS_BLOCK_GROUP_METADATA)
  315. bits |= BLOCK_GROUP_METADATA;
  316. if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  317. bits |= BLOCK_GROUP_SYSTEM;
  318. return bits;
  319. }
  320. static struct btrfs_block_group_cache *
  321. __btrfs_find_block_group(struct btrfs_root *root,
  322. struct btrfs_block_group_cache *hint,
  323. u64 search_start, int data, int owner)
  324. {
  325. struct btrfs_block_group_cache *cache;
  326. struct extent_io_tree *block_group_cache;
  327. struct btrfs_block_group_cache *found_group = NULL;
  328. struct btrfs_fs_info *info = root->fs_info;
  329. u64 used;
  330. u64 last = 0;
  331. u64 start;
  332. u64 end;
  333. u64 free_check;
  334. u64 ptr;
  335. int bit;
  336. int ret;
  337. int full_search = 0;
  338. int factor = 10;
  339. int wrapped = 0;
  340. block_group_cache = &info->block_group_cache;
  341. if (data & BTRFS_BLOCK_GROUP_METADATA)
  342. factor = 9;
  343. bit = block_group_state_bits(data);
  344. if (search_start) {
  345. struct btrfs_block_group_cache *shint;
  346. shint = btrfs_lookup_first_block_group(info, search_start);
  347. if (shint && block_group_bits(shint, data) && !shint->ro) {
  348. spin_lock(&shint->lock);
  349. used = btrfs_block_group_used(&shint->item);
  350. if (used + shint->pinned <
  351. div_factor(shint->key.offset, factor)) {
  352. spin_unlock(&shint->lock);
  353. return shint;
  354. }
  355. spin_unlock(&shint->lock);
  356. }
  357. }
  358. if (hint && !hint->ro && block_group_bits(hint, data)) {
  359. spin_lock(&hint->lock);
  360. used = btrfs_block_group_used(&hint->item);
  361. if (used + hint->pinned <
  362. div_factor(hint->key.offset, factor)) {
  363. spin_unlock(&hint->lock);
  364. return hint;
  365. }
  366. spin_unlock(&hint->lock);
  367. last = hint->key.objectid + hint->key.offset;
  368. } else {
  369. if (hint)
  370. last = max(hint->key.objectid, search_start);
  371. else
  372. last = search_start;
  373. }
  374. again:
  375. while(1) {
  376. ret = find_first_extent_bit(block_group_cache, last,
  377. &start, &end, bit);
  378. if (ret)
  379. break;
  380. ret = get_state_private(block_group_cache, start, &ptr);
  381. if (ret) {
  382. last = end + 1;
  383. continue;
  384. }
  385. cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
  386. spin_lock(&cache->lock);
  387. last = cache->key.objectid + cache->key.offset;
  388. used = btrfs_block_group_used(&cache->item);
  389. if (!cache->ro && block_group_bits(cache, data)) {
  390. free_check = div_factor(cache->key.offset, factor);
  391. if (used + cache->pinned < free_check) {
  392. found_group = cache;
  393. spin_unlock(&cache->lock);
  394. goto found;
  395. }
  396. }
  397. spin_unlock(&cache->lock);
  398. cond_resched();
  399. }
  400. if (!wrapped) {
  401. last = search_start;
  402. wrapped = 1;
  403. goto again;
  404. }
  405. if (!full_search && factor < 10) {
  406. last = search_start;
  407. full_search = 1;
  408. factor = 10;
  409. goto again;
  410. }
  411. found:
  412. return found_group;
  413. }
  414. struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
  415. struct btrfs_block_group_cache
  416. *hint, u64 search_start,
  417. int data, int owner)
  418. {
  419. struct btrfs_block_group_cache *ret;
  420. ret = __btrfs_find_block_group(root, hint, search_start, data, owner);
  421. return ret;
  422. }
  423. static u64 hash_extent_ref(u64 root_objectid, u64 ref_generation,
  424. u64 owner, u64 owner_offset)
  425. {
  426. u32 high_crc = ~(u32)0;
  427. u32 low_crc = ~(u32)0;
  428. __le64 lenum;
  429. lenum = cpu_to_le64(root_objectid);
  430. high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
  431. lenum = cpu_to_le64(ref_generation);
  432. low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
  433. if (owner >= BTRFS_FIRST_FREE_OBJECTID) {
  434. lenum = cpu_to_le64(owner);
  435. low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
  436. lenum = cpu_to_le64(owner_offset);
  437. low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
  438. }
  439. return ((u64)high_crc << 32) | (u64)low_crc;
  440. }
  441. static int match_extent_ref(struct extent_buffer *leaf,
  442. struct btrfs_extent_ref *disk_ref,
  443. struct btrfs_extent_ref *cpu_ref)
  444. {
  445. int ret;
  446. int len;
  447. if (cpu_ref->objectid)
  448. len = sizeof(*cpu_ref);
  449. else
  450. len = 2 * sizeof(u64);
  451. ret = memcmp_extent_buffer(leaf, cpu_ref, (unsigned long)disk_ref,
  452. len);
  453. return ret == 0;
  454. }
  455. static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
  456. struct btrfs_root *root,
  457. struct btrfs_path *path, u64 bytenr,
  458. u64 root_objectid,
  459. u64 ref_generation, u64 owner,
  460. u64 owner_offset, int del)
  461. {
  462. u64 hash;
  463. struct btrfs_key key;
  464. struct btrfs_key found_key;
  465. struct btrfs_extent_ref ref;
  466. struct extent_buffer *leaf;
  467. struct btrfs_extent_ref *disk_ref;
  468. int ret;
  469. int ret2;
  470. btrfs_set_stack_ref_root(&ref, root_objectid);
  471. btrfs_set_stack_ref_generation(&ref, ref_generation);
  472. btrfs_set_stack_ref_objectid(&ref, owner);
  473. btrfs_set_stack_ref_offset(&ref, owner_offset);
  474. hash = hash_extent_ref(root_objectid, ref_generation, owner,
  475. owner_offset);
  476. key.offset = hash;
  477. key.objectid = bytenr;
  478. key.type = BTRFS_EXTENT_REF_KEY;
  479. while (1) {
  480. ret = btrfs_search_slot(trans, root, &key, path,
  481. del ? -1 : 0, del);
  482. if (ret < 0)
  483. goto out;
  484. leaf = path->nodes[0];
  485. if (ret != 0) {
  486. u32 nritems = btrfs_header_nritems(leaf);
  487. if (path->slots[0] >= nritems) {
  488. ret2 = btrfs_next_leaf(root, path);
  489. if (ret2)
  490. goto out;
  491. leaf = path->nodes[0];
  492. }
  493. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  494. if (found_key.objectid != bytenr ||
  495. found_key.type != BTRFS_EXTENT_REF_KEY)
  496. goto out;
  497. key.offset = found_key.offset;
  498. if (del) {
  499. btrfs_release_path(root, path);
  500. continue;
  501. }
  502. }
  503. disk_ref = btrfs_item_ptr(path->nodes[0],
  504. path->slots[0],
  505. struct btrfs_extent_ref);
  506. if (match_extent_ref(path->nodes[0], disk_ref, &ref)) {
  507. ret = 0;
  508. goto out;
  509. }
  510. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  511. key.offset = found_key.offset + 1;
  512. btrfs_release_path(root, path);
  513. }
  514. out:
  515. return ret;
  516. }
  517. /*
  518. * Back reference rules. Back refs have three main goals:
  519. *
  520. * 1) differentiate between all holders of references to an extent so that
  521. * when a reference is dropped we can make sure it was a valid reference
  522. * before freeing the extent.
  523. *
  524. * 2) Provide enough information to quickly find the holders of an extent
  525. * if we notice a given block is corrupted or bad.
  526. *
  527. * 3) Make it easy to migrate blocks for FS shrinking or storage pool
  528. * maintenance. This is actually the same as #2, but with a slightly
  529. * different use case.
  530. *
  531. * File extents can be referenced by:
  532. *
  533. * - multiple snapshots, subvolumes, or different generations in one subvol
  534. * - different files inside a single subvolume (in theory, not implemented yet)
  535. * - different offsets inside a file (bookend extents in file.c)
  536. *
  537. * The extent ref structure has fields for:
  538. *
  539. * - Objectid of the subvolume root
  540. * - Generation number of the tree holding the reference
  541. * - objectid of the file holding the reference
  542. * - offset in the file corresponding to the key holding the reference
  543. *
  544. * When a file extent is allocated the fields are filled in:
  545. * (root_key.objectid, trans->transid, inode objectid, offset in file)
  546. *
  547. * When a leaf is cow'd new references are added for every file extent found
  548. * in the leaf. It looks the same as the create case, but trans->transid
  549. * will be different when the block is cow'd.
  550. *
  551. * (root_key.objectid, trans->transid, inode objectid, offset in file)
  552. *
  553. * When a file extent is removed either during snapshot deletion or file
  554. * truncation, the corresponding back reference is found
  555. * by searching for:
  556. *
  557. * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
  558. * inode objectid, offset in file)
  559. *
  560. * Btree extents can be referenced by:
  561. *
  562. * - Different subvolumes
  563. * - Different generations of the same subvolume
  564. *
  565. * Storing sufficient information for a full reverse mapping of a btree
  566. * block would require storing the lowest key of the block in the backref,
  567. * and it would require updating that lowest key either before write out or
  568. * every time it changed. Instead, the objectid of the lowest key is stored
  569. * along with the level of the tree block. This provides a hint
  570. * about where in the btree the block can be found. Searches through the
  571. * btree only need to look for a pointer to that block, so they stop one
  572. * level higher than the level recorded in the backref.
  573. *
  574. * Some btrees do not do reference counting on their extents. These
  575. * include the extent tree and the tree of tree roots. Backrefs for these
  576. * trees always have a generation of zero.
  577. *
  578. * When a tree block is created, back references are inserted:
  579. *
  580. * (root->root_key.objectid, trans->transid or zero, level, lowest_key_objectid)
  581. *
  582. * When a tree block is cow'd in a reference counted root,
  583. * new back references are added for all the blocks it points to.
  584. * These are of the form (trans->transid will have increased since creation):
  585. *
  586. * (root->root_key.objectid, trans->transid, level, lowest_key_objectid)
  587. *
  588. * Because the lowest_key_objectid and the level are just hints
  589. * they are not used when backrefs are deleted. When a backref is deleted:
  590. *
  591. * if backref was for a tree root:
  592. * root_objectid = root->root_key.objectid
  593. * else
  594. * root_objectid = btrfs_header_owner(parent)
  595. *
  596. * (root_objectid, btrfs_header_generation(parent) or zero, 0, 0)
  597. *
  598. * Back Reference Key hashing:
  599. *
  600. * Back references have four fields, each 64 bits long. Unfortunately,
  601. * This is hashed into a single 64 bit number and placed into the key offset.
  602. * The key objectid corresponds to the first byte in the extent, and the
  603. * key type is set to BTRFS_EXTENT_REF_KEY
  604. */
  605. int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans,
  606. struct btrfs_root *root,
  607. struct btrfs_path *path, u64 bytenr,
  608. u64 root_objectid, u64 ref_generation,
  609. u64 owner, u64 owner_offset)
  610. {
  611. u64 hash;
  612. struct btrfs_key key;
  613. struct btrfs_extent_ref ref;
  614. struct btrfs_extent_ref *disk_ref;
  615. int ret;
  616. btrfs_set_stack_ref_root(&ref, root_objectid);
  617. btrfs_set_stack_ref_generation(&ref, ref_generation);
  618. btrfs_set_stack_ref_objectid(&ref, owner);
  619. btrfs_set_stack_ref_offset(&ref, owner_offset);
  620. hash = hash_extent_ref(root_objectid, ref_generation, owner,
  621. owner_offset);
  622. key.offset = hash;
  623. key.objectid = bytenr;
  624. key.type = BTRFS_EXTENT_REF_KEY;
  625. ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(ref));
  626. while (ret == -EEXIST) {
  627. disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
  628. struct btrfs_extent_ref);
  629. if (match_extent_ref(path->nodes[0], disk_ref, &ref))
  630. goto out;
  631. key.offset++;
  632. btrfs_release_path(root, path);
  633. ret = btrfs_insert_empty_item(trans, root, path, &key,
  634. sizeof(ref));
  635. }
  636. if (ret)
  637. goto out;
  638. disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
  639. struct btrfs_extent_ref);
  640. write_extent_buffer(path->nodes[0], &ref, (unsigned long)disk_ref,
  641. sizeof(ref));
  642. btrfs_mark_buffer_dirty(path->nodes[0]);
  643. out:
  644. btrfs_release_path(root, path);
  645. return ret;
  646. }
  647. static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
  648. struct btrfs_root *root,
  649. u64 bytenr, u64 num_bytes,
  650. u64 root_objectid, u64 ref_generation,
  651. u64 owner, u64 owner_offset)
  652. {
  653. struct btrfs_path *path;
  654. int ret;
  655. struct btrfs_key key;
  656. struct extent_buffer *l;
  657. struct btrfs_extent_item *item;
  658. u32 refs;
  659. WARN_ON(num_bytes < root->sectorsize);
  660. path = btrfs_alloc_path();
  661. if (!path)
  662. return -ENOMEM;
  663. path->reada = 1;
  664. key.objectid = bytenr;
  665. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  666. key.offset = num_bytes;
  667. ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
  668. 0, 1);
  669. if (ret < 0)
  670. return ret;
  671. if (ret != 0) {
  672. BUG();
  673. }
  674. BUG_ON(ret != 0);
  675. l = path->nodes[0];
  676. item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
  677. refs = btrfs_extent_refs(l, item);
  678. btrfs_set_extent_refs(l, item, refs + 1);
  679. btrfs_mark_buffer_dirty(path->nodes[0]);
  680. btrfs_release_path(root->fs_info->extent_root, path);
  681. path->reada = 1;
  682. ret = btrfs_insert_extent_backref(trans, root->fs_info->extent_root,
  683. path, bytenr, root_objectid,
  684. ref_generation, owner, owner_offset);
  685. BUG_ON(ret);
  686. finish_current_insert(trans, root->fs_info->extent_root);
  687. del_pending_extents(trans, root->fs_info->extent_root);
  688. btrfs_free_path(path);
  689. return 0;
  690. }
  691. int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
  692. struct btrfs_root *root,
  693. u64 bytenr, u64 num_bytes,
  694. u64 root_objectid, u64 ref_generation,
  695. u64 owner, u64 owner_offset)
  696. {
  697. int ret;
  698. mutex_lock(&root->fs_info->alloc_mutex);
  699. ret = __btrfs_inc_extent_ref(trans, root, bytenr, num_bytes,
  700. root_objectid, ref_generation,
  701. owner, owner_offset);
  702. mutex_unlock(&root->fs_info->alloc_mutex);
  703. return ret;
  704. }
  705. int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
  706. struct btrfs_root *root)
  707. {
  708. finish_current_insert(trans, root->fs_info->extent_root);
  709. del_pending_extents(trans, root->fs_info->extent_root);
  710. return 0;
  711. }
  712. static int lookup_extent_ref(struct btrfs_trans_handle *trans,
  713. struct btrfs_root *root, u64 bytenr,
  714. u64 num_bytes, u32 *refs)
  715. {
  716. struct btrfs_path *path;
  717. int ret;
  718. struct btrfs_key key;
  719. struct extent_buffer *l;
  720. struct btrfs_extent_item *item;
  721. WARN_ON(num_bytes < root->sectorsize);
  722. path = btrfs_alloc_path();
  723. path->reada = 1;
  724. key.objectid = bytenr;
  725. key.offset = num_bytes;
  726. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  727. ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
  728. 0, 0);
  729. if (ret < 0)
  730. goto out;
  731. if (ret != 0) {
  732. btrfs_print_leaf(root, path->nodes[0]);
  733. printk("failed to find block number %Lu\n", bytenr);
  734. BUG();
  735. }
  736. l = path->nodes[0];
  737. item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
  738. *refs = btrfs_extent_refs(l, item);
  739. out:
  740. btrfs_free_path(path);
  741. return 0;
  742. }
  743. static int get_reference_status(struct btrfs_root *root, u64 bytenr,
  744. u64 parent_gen, u64 ref_objectid,
  745. u64 *min_generation, u32 *ref_count)
  746. {
  747. struct btrfs_root *extent_root = root->fs_info->extent_root;
  748. struct btrfs_path *path;
  749. struct extent_buffer *leaf;
  750. struct btrfs_extent_ref *ref_item;
  751. struct btrfs_key key;
  752. struct btrfs_key found_key;
  753. u64 root_objectid = root->root_key.objectid;
  754. u64 ref_generation;
  755. u32 nritems;
  756. int ret;
  757. key.objectid = bytenr;
  758. key.offset = 0;
  759. key.type = BTRFS_EXTENT_ITEM_KEY;
  760. path = btrfs_alloc_path();
  761. mutex_lock(&root->fs_info->alloc_mutex);
  762. ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
  763. if (ret < 0)
  764. goto out;
  765. BUG_ON(ret == 0);
  766. leaf = path->nodes[0];
  767. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  768. if (found_key.objectid != bytenr ||
  769. found_key.type != BTRFS_EXTENT_ITEM_KEY) {
  770. ret = 1;
  771. goto out;
  772. }
  773. *ref_count = 0;
  774. *min_generation = (u64)-1;
  775. while (1) {
  776. leaf = path->nodes[0];
  777. nritems = btrfs_header_nritems(leaf);
  778. if (path->slots[0] >= nritems) {
  779. ret = btrfs_next_leaf(extent_root, path);
  780. if (ret < 0)
  781. goto out;
  782. if (ret == 0)
  783. continue;
  784. break;
  785. }
  786. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  787. if (found_key.objectid != bytenr)
  788. break;
  789. if (found_key.type != BTRFS_EXTENT_REF_KEY) {
  790. path->slots[0]++;
  791. continue;
  792. }
  793. ref_item = btrfs_item_ptr(leaf, path->slots[0],
  794. struct btrfs_extent_ref);
  795. ref_generation = btrfs_ref_generation(leaf, ref_item);
  796. /*
  797. * For (parent_gen > 0 && parent_gen > ref_gen):
  798. *
  799. * we reach here through the oldest root, therefore
  800. * all other reference from same snapshot should have
  801. * a larger generation.
  802. */
  803. if ((root_objectid != btrfs_ref_root(leaf, ref_item)) ||
  804. (parent_gen > 0 && parent_gen > ref_generation) ||
  805. (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID &&
  806. ref_objectid != btrfs_ref_objectid(leaf, ref_item))) {
  807. if (ref_count)
  808. *ref_count = 2;
  809. break;
  810. }
  811. *ref_count = 1;
  812. if (*min_generation > ref_generation)
  813. *min_generation = ref_generation;
  814. path->slots[0]++;
  815. }
  816. ret = 0;
  817. out:
  818. mutex_unlock(&root->fs_info->alloc_mutex);
  819. btrfs_free_path(path);
  820. return ret;
  821. }
  822. int btrfs_cross_ref_exists(struct btrfs_root *root,
  823. struct btrfs_key *key, u64 bytenr)
  824. {
  825. struct btrfs_trans_handle *trans;
  826. struct btrfs_root *old_root;
  827. struct btrfs_path *path = NULL;
  828. struct extent_buffer *eb;
  829. struct btrfs_file_extent_item *item;
  830. u64 ref_generation;
  831. u64 min_generation;
  832. u64 extent_start;
  833. u32 ref_count;
  834. int level;
  835. int ret;
  836. BUG_ON(key->type != BTRFS_EXTENT_DATA_KEY);
  837. ret = get_reference_status(root, bytenr, 0, key->objectid,
  838. &min_generation, &ref_count);
  839. if (ret)
  840. return ret;
  841. if (ref_count != 1)
  842. return 1;
  843. trans = btrfs_start_transaction(root, 0);
  844. old_root = root->dirty_root->root;
  845. ref_generation = old_root->root_key.offset;
  846. /* all references are created in running transaction */
  847. if (min_generation > ref_generation) {
  848. ret = 0;
  849. goto out;
  850. }
  851. path = btrfs_alloc_path();
  852. if (!path) {
  853. ret = -ENOMEM;
  854. goto out;
  855. }
  856. path->skip_locking = 1;
  857. /* if no item found, the extent is referenced by other snapshot */
  858. ret = btrfs_search_slot(NULL, old_root, key, path, 0, 0);
  859. if (ret)
  860. goto out;
  861. eb = path->nodes[0];
  862. item = btrfs_item_ptr(eb, path->slots[0],
  863. struct btrfs_file_extent_item);
  864. if (btrfs_file_extent_type(eb, item) != BTRFS_FILE_EXTENT_REG ||
  865. btrfs_file_extent_disk_bytenr(eb, item) != bytenr) {
  866. ret = 1;
  867. goto out;
  868. }
  869. for (level = BTRFS_MAX_LEVEL - 1; level >= -1; level--) {
  870. if (level >= 0) {
  871. eb = path->nodes[level];
  872. if (!eb)
  873. continue;
  874. extent_start = eb->start;
  875. } else
  876. extent_start = bytenr;
  877. ret = get_reference_status(root, extent_start, ref_generation,
  878. 0, &min_generation, &ref_count);
  879. if (ret)
  880. goto out;
  881. if (ref_count != 1) {
  882. ret = 1;
  883. goto out;
  884. }
  885. if (level >= 0)
  886. ref_generation = btrfs_header_generation(eb);
  887. }
  888. ret = 0;
  889. out:
  890. if (path)
  891. btrfs_free_path(path);
  892. btrfs_end_transaction(trans, root);
  893. return ret;
  894. }
  895. int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  896. struct extent_buffer *buf, int cache_ref)
  897. {
  898. u64 bytenr;
  899. u32 nritems;
  900. struct btrfs_key key;
  901. struct btrfs_file_extent_item *fi;
  902. int i;
  903. int level;
  904. int ret;
  905. int faili;
  906. int nr_file_extents = 0;
  907. if (!root->ref_cows)
  908. return 0;
  909. level = btrfs_header_level(buf);
  910. nritems = btrfs_header_nritems(buf);
  911. for (i = 0; i < nritems; i++) {
  912. cond_resched();
  913. if (level == 0) {
  914. u64 disk_bytenr;
  915. btrfs_item_key_to_cpu(buf, &key, i);
  916. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  917. continue;
  918. fi = btrfs_item_ptr(buf, i,
  919. struct btrfs_file_extent_item);
  920. if (btrfs_file_extent_type(buf, fi) ==
  921. BTRFS_FILE_EXTENT_INLINE)
  922. continue;
  923. disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  924. if (disk_bytenr == 0)
  925. continue;
  926. if (buf != root->commit_root)
  927. nr_file_extents++;
  928. mutex_lock(&root->fs_info->alloc_mutex);
  929. ret = __btrfs_inc_extent_ref(trans, root, disk_bytenr,
  930. btrfs_file_extent_disk_num_bytes(buf, fi),
  931. root->root_key.objectid, trans->transid,
  932. key.objectid, key.offset);
  933. mutex_unlock(&root->fs_info->alloc_mutex);
  934. if (ret) {
  935. faili = i;
  936. WARN_ON(1);
  937. goto fail;
  938. }
  939. } else {
  940. bytenr = btrfs_node_blockptr(buf, i);
  941. btrfs_node_key_to_cpu(buf, &key, i);
  942. mutex_lock(&root->fs_info->alloc_mutex);
  943. ret = __btrfs_inc_extent_ref(trans, root, bytenr,
  944. btrfs_level_size(root, level - 1),
  945. root->root_key.objectid,
  946. trans->transid,
  947. level - 1, key.objectid);
  948. mutex_unlock(&root->fs_info->alloc_mutex);
  949. if (ret) {
  950. faili = i;
  951. WARN_ON(1);
  952. goto fail;
  953. }
  954. }
  955. }
  956. /* cache orignal leaf block's references */
  957. if (level == 0 && cache_ref && buf != root->commit_root) {
  958. struct btrfs_leaf_ref *ref;
  959. struct btrfs_extent_info *info;
  960. ref = btrfs_alloc_leaf_ref(root, nr_file_extents);
  961. if (!ref) {
  962. WARN_ON(1);
  963. goto out;
  964. }
  965. ref->root_gen = root->root_key.offset;
  966. ref->bytenr = buf->start;
  967. ref->owner = btrfs_header_owner(buf);
  968. ref->generation = btrfs_header_generation(buf);
  969. ref->nritems = nr_file_extents;
  970. info = ref->extents;
  971. for (i = 0; nr_file_extents > 0 && i < nritems; i++) {
  972. u64 disk_bytenr;
  973. btrfs_item_key_to_cpu(buf, &key, i);
  974. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  975. continue;
  976. fi = btrfs_item_ptr(buf, i,
  977. struct btrfs_file_extent_item);
  978. if (btrfs_file_extent_type(buf, fi) ==
  979. BTRFS_FILE_EXTENT_INLINE)
  980. continue;
  981. disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  982. if (disk_bytenr == 0)
  983. continue;
  984. info->bytenr = disk_bytenr;
  985. info->num_bytes =
  986. btrfs_file_extent_disk_num_bytes(buf, fi);
  987. info->objectid = key.objectid;
  988. info->offset = key.offset;
  989. info++;
  990. }
  991. BUG_ON(!root->ref_tree);
  992. ret = btrfs_add_leaf_ref(root, ref);
  993. WARN_ON(ret);
  994. btrfs_free_leaf_ref(root, ref);
  995. }
  996. out:
  997. return 0;
  998. fail:
  999. WARN_ON(1);
  1000. #if 0
  1001. for (i =0; i < faili; i++) {
  1002. if (level == 0) {
  1003. u64 disk_bytenr;
  1004. btrfs_item_key_to_cpu(buf, &key, i);
  1005. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  1006. continue;
  1007. fi = btrfs_item_ptr(buf, i,
  1008. struct btrfs_file_extent_item);
  1009. if (btrfs_file_extent_type(buf, fi) ==
  1010. BTRFS_FILE_EXTENT_INLINE)
  1011. continue;
  1012. disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  1013. if (disk_bytenr == 0)
  1014. continue;
  1015. err = btrfs_free_extent(trans, root, disk_bytenr,
  1016. btrfs_file_extent_disk_num_bytes(buf,
  1017. fi), 0);
  1018. BUG_ON(err);
  1019. } else {
  1020. bytenr = btrfs_node_blockptr(buf, i);
  1021. err = btrfs_free_extent(trans, root, bytenr,
  1022. btrfs_level_size(root, level - 1), 0);
  1023. BUG_ON(err);
  1024. }
  1025. }
  1026. #endif
  1027. return ret;
  1028. }
  1029. static int write_one_cache_group(struct btrfs_trans_handle *trans,
  1030. struct btrfs_root *root,
  1031. struct btrfs_path *path,
  1032. struct btrfs_block_group_cache *cache)
  1033. {
  1034. int ret;
  1035. int pending_ret;
  1036. struct btrfs_root *extent_root = root->fs_info->extent_root;
  1037. unsigned long bi;
  1038. struct extent_buffer *leaf;
  1039. ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
  1040. if (ret < 0)
  1041. goto fail;
  1042. BUG_ON(ret);
  1043. leaf = path->nodes[0];
  1044. bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
  1045. write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
  1046. btrfs_mark_buffer_dirty(leaf);
  1047. btrfs_release_path(extent_root, path);
  1048. fail:
  1049. finish_current_insert(trans, extent_root);
  1050. pending_ret = del_pending_extents(trans, extent_root);
  1051. if (ret)
  1052. return ret;
  1053. if (pending_ret)
  1054. return pending_ret;
  1055. return 0;
  1056. }
  1057. int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
  1058. struct btrfs_root *root)
  1059. {
  1060. struct extent_io_tree *block_group_cache;
  1061. struct btrfs_block_group_cache *cache;
  1062. int ret;
  1063. int err = 0;
  1064. int werr = 0;
  1065. struct btrfs_path *path;
  1066. u64 last = 0;
  1067. u64 start;
  1068. u64 end;
  1069. u64 ptr;
  1070. block_group_cache = &root->fs_info->block_group_cache;
  1071. path = btrfs_alloc_path();
  1072. if (!path)
  1073. return -ENOMEM;
  1074. mutex_lock(&root->fs_info->alloc_mutex);
  1075. while(1) {
  1076. ret = find_first_extent_bit(block_group_cache, last,
  1077. &start, &end, BLOCK_GROUP_DIRTY);
  1078. if (ret)
  1079. break;
  1080. last = end + 1;
  1081. ret = get_state_private(block_group_cache, start, &ptr);
  1082. if (ret)
  1083. break;
  1084. cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
  1085. err = write_one_cache_group(trans, root,
  1086. path, cache);
  1087. /*
  1088. * if we fail to write the cache group, we want
  1089. * to keep it marked dirty in hopes that a later
  1090. * write will work
  1091. */
  1092. if (err) {
  1093. werr = err;
  1094. continue;
  1095. }
  1096. clear_extent_bits(block_group_cache, start, end,
  1097. BLOCK_GROUP_DIRTY, GFP_NOFS);
  1098. }
  1099. btrfs_free_path(path);
  1100. mutex_unlock(&root->fs_info->alloc_mutex);
  1101. return werr;
  1102. }
  1103. static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
  1104. u64 flags)
  1105. {
  1106. struct list_head *head = &info->space_info;
  1107. struct list_head *cur;
  1108. struct btrfs_space_info *found;
  1109. list_for_each(cur, head) {
  1110. found = list_entry(cur, struct btrfs_space_info, list);
  1111. if (found->flags == flags)
  1112. return found;
  1113. }
  1114. return NULL;
  1115. }
  1116. static int update_space_info(struct btrfs_fs_info *info, u64 flags,
  1117. u64 total_bytes, u64 bytes_used,
  1118. struct btrfs_space_info **space_info)
  1119. {
  1120. struct btrfs_space_info *found;
  1121. found = __find_space_info(info, flags);
  1122. if (found) {
  1123. found->total_bytes += total_bytes;
  1124. found->bytes_used += bytes_used;
  1125. found->full = 0;
  1126. WARN_ON(found->total_bytes < found->bytes_used);
  1127. *space_info = found;
  1128. return 0;
  1129. }
  1130. found = kmalloc(sizeof(*found), GFP_NOFS);
  1131. if (!found)
  1132. return -ENOMEM;
  1133. list_add(&found->list, &info->space_info);
  1134. found->flags = flags;
  1135. found->total_bytes = total_bytes;
  1136. found->bytes_used = bytes_used;
  1137. found->bytes_pinned = 0;
  1138. found->full = 0;
  1139. found->force_alloc = 0;
  1140. *space_info = found;
  1141. return 0;
  1142. }
  1143. static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
  1144. {
  1145. u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
  1146. BTRFS_BLOCK_GROUP_RAID1 |
  1147. BTRFS_BLOCK_GROUP_RAID10 |
  1148. BTRFS_BLOCK_GROUP_DUP);
  1149. if (extra_flags) {
  1150. if (flags & BTRFS_BLOCK_GROUP_DATA)
  1151. fs_info->avail_data_alloc_bits |= extra_flags;
  1152. if (flags & BTRFS_BLOCK_GROUP_METADATA)
  1153. fs_info->avail_metadata_alloc_bits |= extra_flags;
  1154. if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  1155. fs_info->avail_system_alloc_bits |= extra_flags;
  1156. }
  1157. }
  1158. static u64 reduce_alloc_profile(struct btrfs_root *root, u64 flags)
  1159. {
  1160. u64 num_devices = root->fs_info->fs_devices->num_devices;
  1161. if (num_devices == 1)
  1162. flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
  1163. if (num_devices < 4)
  1164. flags &= ~BTRFS_BLOCK_GROUP_RAID10;
  1165. if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
  1166. (flags & (BTRFS_BLOCK_GROUP_RAID1 |
  1167. BTRFS_BLOCK_GROUP_RAID10))) {
  1168. flags &= ~BTRFS_BLOCK_GROUP_DUP;
  1169. }
  1170. if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
  1171. (flags & BTRFS_BLOCK_GROUP_RAID10)) {
  1172. flags &= ~BTRFS_BLOCK_GROUP_RAID1;
  1173. }
  1174. if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
  1175. ((flags & BTRFS_BLOCK_GROUP_RAID1) |
  1176. (flags & BTRFS_BLOCK_GROUP_RAID10) |
  1177. (flags & BTRFS_BLOCK_GROUP_DUP)))
  1178. flags &= ~BTRFS_BLOCK_GROUP_RAID0;
  1179. return flags;
  1180. }
  1181. static int do_chunk_alloc(struct btrfs_trans_handle *trans,
  1182. struct btrfs_root *extent_root, u64 alloc_bytes,
  1183. u64 flags, int force)
  1184. {
  1185. struct btrfs_space_info *space_info;
  1186. u64 thresh;
  1187. u64 start;
  1188. u64 num_bytes;
  1189. int ret;
  1190. flags = reduce_alloc_profile(extent_root, flags);
  1191. space_info = __find_space_info(extent_root->fs_info, flags);
  1192. if (!space_info) {
  1193. ret = update_space_info(extent_root->fs_info, flags,
  1194. 0, 0, &space_info);
  1195. BUG_ON(ret);
  1196. }
  1197. BUG_ON(!space_info);
  1198. if (space_info->force_alloc) {
  1199. force = 1;
  1200. space_info->force_alloc = 0;
  1201. }
  1202. if (space_info->full)
  1203. goto out;
  1204. thresh = div_factor(space_info->total_bytes, 6);
  1205. if (!force &&
  1206. (space_info->bytes_used + space_info->bytes_pinned + alloc_bytes) <
  1207. thresh)
  1208. goto out;
  1209. mutex_lock(&extent_root->fs_info->chunk_mutex);
  1210. ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes, flags);
  1211. if (ret == -ENOSPC) {
  1212. printk("space info full %Lu\n", flags);
  1213. space_info->full = 1;
  1214. goto out_unlock;
  1215. }
  1216. BUG_ON(ret);
  1217. ret = btrfs_make_block_group(trans, extent_root, 0, flags,
  1218. BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
  1219. BUG_ON(ret);
  1220. out_unlock:
  1221. mutex_unlock(&extent_root->fs_info->chunk_mutex);
  1222. out:
  1223. return 0;
  1224. }
  1225. static int update_block_group(struct btrfs_trans_handle *trans,
  1226. struct btrfs_root *root,
  1227. u64 bytenr, u64 num_bytes, int alloc,
  1228. int mark_free)
  1229. {
  1230. struct btrfs_block_group_cache *cache;
  1231. struct btrfs_fs_info *info = root->fs_info;
  1232. u64 total = num_bytes;
  1233. u64 old_val;
  1234. u64 byte_in_group;
  1235. u64 start;
  1236. u64 end;
  1237. WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
  1238. while(total) {
  1239. cache = btrfs_lookup_block_group(info, bytenr);
  1240. if (!cache) {
  1241. return -1;
  1242. }
  1243. byte_in_group = bytenr - cache->key.objectid;
  1244. WARN_ON(byte_in_group > cache->key.offset);
  1245. start = cache->key.objectid;
  1246. end = start + cache->key.offset - 1;
  1247. set_extent_bits(&info->block_group_cache, start, end,
  1248. BLOCK_GROUP_DIRTY, GFP_NOFS);
  1249. spin_lock(&cache->lock);
  1250. old_val = btrfs_block_group_used(&cache->item);
  1251. num_bytes = min(total, cache->key.offset - byte_in_group);
  1252. if (alloc) {
  1253. old_val += num_bytes;
  1254. cache->space_info->bytes_used += num_bytes;
  1255. btrfs_set_block_group_used(&cache->item, old_val);
  1256. spin_unlock(&cache->lock);
  1257. } else {
  1258. old_val -= num_bytes;
  1259. cache->space_info->bytes_used -= num_bytes;
  1260. btrfs_set_block_group_used(&cache->item, old_val);
  1261. spin_unlock(&cache->lock);
  1262. if (mark_free) {
  1263. set_extent_dirty(&info->free_space_cache,
  1264. bytenr, bytenr + num_bytes - 1,
  1265. GFP_NOFS);
  1266. }
  1267. }
  1268. total -= num_bytes;
  1269. bytenr += num_bytes;
  1270. }
  1271. return 0;
  1272. }
  1273. static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
  1274. {
  1275. u64 start;
  1276. u64 end;
  1277. int ret;
  1278. ret = find_first_extent_bit(&root->fs_info->block_group_cache,
  1279. search_start, &start, &end,
  1280. BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
  1281. BLOCK_GROUP_SYSTEM);
  1282. if (ret)
  1283. return 0;
  1284. return start;
  1285. }
  1286. static int update_pinned_extents(struct btrfs_root *root,
  1287. u64 bytenr, u64 num, int pin)
  1288. {
  1289. u64 len;
  1290. struct btrfs_block_group_cache *cache;
  1291. struct btrfs_fs_info *fs_info = root->fs_info;
  1292. WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
  1293. if (pin) {
  1294. set_extent_dirty(&fs_info->pinned_extents,
  1295. bytenr, bytenr + num - 1, GFP_NOFS);
  1296. } else {
  1297. clear_extent_dirty(&fs_info->pinned_extents,
  1298. bytenr, bytenr + num - 1, GFP_NOFS);
  1299. }
  1300. while (num > 0) {
  1301. cache = btrfs_lookup_block_group(fs_info, bytenr);
  1302. if (!cache) {
  1303. u64 first = first_logical_byte(root, bytenr);
  1304. WARN_ON(first < bytenr);
  1305. len = min(first - bytenr, num);
  1306. } else {
  1307. len = min(num, cache->key.offset -
  1308. (bytenr - cache->key.objectid));
  1309. }
  1310. if (pin) {
  1311. if (cache) {
  1312. spin_lock(&cache->lock);
  1313. cache->pinned += len;
  1314. cache->space_info->bytes_pinned += len;
  1315. spin_unlock(&cache->lock);
  1316. }
  1317. fs_info->total_pinned += len;
  1318. } else {
  1319. if (cache) {
  1320. spin_lock(&cache->lock);
  1321. cache->pinned -= len;
  1322. cache->space_info->bytes_pinned -= len;
  1323. spin_unlock(&cache->lock);
  1324. }
  1325. fs_info->total_pinned -= len;
  1326. }
  1327. bytenr += len;
  1328. num -= len;
  1329. }
  1330. return 0;
  1331. }
  1332. int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
  1333. {
  1334. u64 last = 0;
  1335. u64 start;
  1336. u64 end;
  1337. struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
  1338. int ret;
  1339. while(1) {
  1340. ret = find_first_extent_bit(pinned_extents, last,
  1341. &start, &end, EXTENT_DIRTY);
  1342. if (ret)
  1343. break;
  1344. set_extent_dirty(copy, start, end, GFP_NOFS);
  1345. last = end + 1;
  1346. }
  1347. return 0;
  1348. }
  1349. int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
  1350. struct btrfs_root *root,
  1351. struct extent_io_tree *unpin)
  1352. {
  1353. u64 start;
  1354. u64 end;
  1355. int ret;
  1356. struct extent_io_tree *free_space_cache;
  1357. free_space_cache = &root->fs_info->free_space_cache;
  1358. mutex_lock(&root->fs_info->alloc_mutex);
  1359. while(1) {
  1360. ret = find_first_extent_bit(unpin, 0, &start, &end,
  1361. EXTENT_DIRTY);
  1362. if (ret)
  1363. break;
  1364. update_pinned_extents(root, start, end + 1 - start, 0);
  1365. clear_extent_dirty(unpin, start, end, GFP_NOFS);
  1366. set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
  1367. if (need_resched()) {
  1368. mutex_unlock(&root->fs_info->alloc_mutex);
  1369. cond_resched();
  1370. mutex_lock(&root->fs_info->alloc_mutex);
  1371. }
  1372. }
  1373. mutex_unlock(&root->fs_info->alloc_mutex);
  1374. return 0;
  1375. }
  1376. static int finish_current_insert(struct btrfs_trans_handle *trans,
  1377. struct btrfs_root *extent_root)
  1378. {
  1379. u64 start;
  1380. u64 end;
  1381. struct btrfs_fs_info *info = extent_root->fs_info;
  1382. struct extent_buffer *eb;
  1383. struct btrfs_path *path;
  1384. struct btrfs_key ins;
  1385. struct btrfs_disk_key first;
  1386. struct btrfs_extent_item extent_item;
  1387. int ret;
  1388. int level;
  1389. int err = 0;
  1390. WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex));
  1391. btrfs_set_stack_extent_refs(&extent_item, 1);
  1392. btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
  1393. path = btrfs_alloc_path();
  1394. while(1) {
  1395. ret = find_first_extent_bit(&info->extent_ins, 0, &start,
  1396. &end, EXTENT_LOCKED);
  1397. if (ret)
  1398. break;
  1399. ins.objectid = start;
  1400. ins.offset = end + 1 - start;
  1401. err = btrfs_insert_item(trans, extent_root, &ins,
  1402. &extent_item, sizeof(extent_item));
  1403. clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
  1404. GFP_NOFS);
  1405. eb = btrfs_find_tree_block(extent_root, ins.objectid,
  1406. ins.offset);
  1407. if (!btrfs_buffer_uptodate(eb, trans->transid)) {
  1408. mutex_unlock(&extent_root->fs_info->alloc_mutex);
  1409. btrfs_read_buffer(eb, trans->transid);
  1410. mutex_lock(&extent_root->fs_info->alloc_mutex);
  1411. }
  1412. btrfs_tree_lock(eb);
  1413. level = btrfs_header_level(eb);
  1414. if (level == 0) {
  1415. btrfs_item_key(eb, &first, 0);
  1416. } else {
  1417. btrfs_node_key(eb, &first, 0);
  1418. }
  1419. btrfs_tree_unlock(eb);
  1420. free_extent_buffer(eb);
  1421. /*
  1422. * the first key is just a hint, so the race we've created
  1423. * against reading it is fine
  1424. */
  1425. err = btrfs_insert_extent_backref(trans, extent_root, path,
  1426. start, extent_root->root_key.objectid,
  1427. 0, level,
  1428. btrfs_disk_key_objectid(&first));
  1429. BUG_ON(err);
  1430. if (need_resched()) {
  1431. mutex_unlock(&extent_root->fs_info->alloc_mutex);
  1432. cond_resched();
  1433. mutex_lock(&extent_root->fs_info->alloc_mutex);
  1434. }
  1435. }
  1436. btrfs_free_path(path);
  1437. return 0;
  1438. }
  1439. static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
  1440. int pending)
  1441. {
  1442. int err = 0;
  1443. WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
  1444. if (!pending) {
  1445. struct extent_buffer *buf;
  1446. buf = btrfs_find_tree_block(root, bytenr, num_bytes);
  1447. if (buf) {
  1448. if (btrfs_buffer_uptodate(buf, 0) &&
  1449. btrfs_try_tree_lock(buf)) {
  1450. u64 transid =
  1451. root->fs_info->running_transaction->transid;
  1452. u64 header_transid =
  1453. btrfs_header_generation(buf);
  1454. if (header_transid == transid &&
  1455. !btrfs_header_flag(buf,
  1456. BTRFS_HEADER_FLAG_WRITTEN)) {
  1457. clean_tree_block(NULL, root, buf);
  1458. btrfs_tree_unlock(buf);
  1459. free_extent_buffer(buf);
  1460. return 1;
  1461. }
  1462. btrfs_tree_unlock(buf);
  1463. }
  1464. free_extent_buffer(buf);
  1465. }
  1466. update_pinned_extents(root, bytenr, num_bytes, 1);
  1467. } else {
  1468. set_extent_bits(&root->fs_info->pending_del,
  1469. bytenr, bytenr + num_bytes - 1,
  1470. EXTENT_LOCKED, GFP_NOFS);
  1471. }
  1472. BUG_ON(err < 0);
  1473. return 0;
  1474. }
  1475. /*
  1476. * remove an extent from the root, returns 0 on success
  1477. */
  1478. static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
  1479. *root, u64 bytenr, u64 num_bytes,
  1480. u64 root_objectid, u64 ref_generation,
  1481. u64 owner_objectid, u64 owner_offset, int pin,
  1482. int mark_free)
  1483. {
  1484. struct btrfs_path *path;
  1485. struct btrfs_key key;
  1486. struct btrfs_fs_info *info = root->fs_info;
  1487. struct btrfs_root *extent_root = info->extent_root;
  1488. struct extent_buffer *leaf;
  1489. int ret;
  1490. int extent_slot = 0;
  1491. int found_extent = 0;
  1492. int num_to_del = 1;
  1493. struct btrfs_extent_item *ei;
  1494. u32 refs;
  1495. WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
  1496. key.objectid = bytenr;
  1497. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  1498. key.offset = num_bytes;
  1499. path = btrfs_alloc_path();
  1500. if (!path)
  1501. return -ENOMEM;
  1502. path->reada = 1;
  1503. ret = lookup_extent_backref(trans, extent_root, path,
  1504. bytenr, root_objectid,
  1505. ref_generation,
  1506. owner_objectid, owner_offset, 1);
  1507. if (ret == 0) {
  1508. struct btrfs_key found_key;
  1509. extent_slot = path->slots[0];
  1510. while(extent_slot > 0) {
  1511. extent_slot--;
  1512. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  1513. extent_slot);
  1514. if (found_key.objectid != bytenr)
  1515. break;
  1516. if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
  1517. found_key.offset == num_bytes) {
  1518. found_extent = 1;
  1519. break;
  1520. }
  1521. if (path->slots[0] - extent_slot > 5)
  1522. break;
  1523. }
  1524. if (!found_extent)
  1525. ret = btrfs_del_item(trans, extent_root, path);
  1526. } else {
  1527. btrfs_print_leaf(extent_root, path->nodes[0]);
  1528. WARN_ON(1);
  1529. printk("Unable to find ref byte nr %Lu root %Lu "
  1530. " gen %Lu owner %Lu offset %Lu\n", bytenr,
  1531. root_objectid, ref_generation, owner_objectid,
  1532. owner_offset);
  1533. }
  1534. if (!found_extent) {
  1535. btrfs_release_path(extent_root, path);
  1536. ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
  1537. if (ret < 0)
  1538. return ret;
  1539. BUG_ON(ret);
  1540. extent_slot = path->slots[0];
  1541. }
  1542. leaf = path->nodes[0];
  1543. ei = btrfs_item_ptr(leaf, extent_slot,
  1544. struct btrfs_extent_item);
  1545. refs = btrfs_extent_refs(leaf, ei);
  1546. BUG_ON(refs == 0);
  1547. refs -= 1;
  1548. btrfs_set_extent_refs(leaf, ei, refs);
  1549. btrfs_mark_buffer_dirty(leaf);
  1550. if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
  1551. /* if the back ref and the extent are next to each other
  1552. * they get deleted below in one shot
  1553. */
  1554. path->slots[0] = extent_slot;
  1555. num_to_del = 2;
  1556. } else if (found_extent) {
  1557. /* otherwise delete the extent back ref */
  1558. ret = btrfs_del_item(trans, extent_root, path);
  1559. BUG_ON(ret);
  1560. /* if refs are 0, we need to setup the path for deletion */
  1561. if (refs == 0) {
  1562. btrfs_release_path(extent_root, path);
  1563. ret = btrfs_search_slot(trans, extent_root, &key, path,
  1564. -1, 1);
  1565. if (ret < 0)
  1566. return ret;
  1567. BUG_ON(ret);
  1568. }
  1569. }
  1570. if (refs == 0) {
  1571. u64 super_used;
  1572. u64 root_used;
  1573. if (pin) {
  1574. ret = pin_down_bytes(root, bytenr, num_bytes, 0);
  1575. if (ret > 0)
  1576. mark_free = 1;
  1577. BUG_ON(ret < 0);
  1578. }
  1579. /* block accounting for super block */
  1580. spin_lock_irq(&info->delalloc_lock);
  1581. super_used = btrfs_super_bytes_used(&info->super_copy);
  1582. btrfs_set_super_bytes_used(&info->super_copy,
  1583. super_used - num_bytes);
  1584. spin_unlock_irq(&info->delalloc_lock);
  1585. /* block accounting for root item */
  1586. root_used = btrfs_root_used(&root->root_item);
  1587. btrfs_set_root_used(&root->root_item,
  1588. root_used - num_bytes);
  1589. ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
  1590. num_to_del);
  1591. if (ret) {
  1592. return ret;
  1593. }
  1594. ret = update_block_group(trans, root, bytenr, num_bytes, 0,
  1595. mark_free);
  1596. BUG_ON(ret);
  1597. }
  1598. btrfs_free_path(path);
  1599. finish_current_insert(trans, extent_root);
  1600. return ret;
  1601. }
  1602. /*
  1603. * find all the blocks marked as pending in the radix tree and remove
  1604. * them from the extent map
  1605. */
  1606. static int del_pending_extents(struct btrfs_trans_handle *trans, struct
  1607. btrfs_root *extent_root)
  1608. {
  1609. int ret;
  1610. int err = 0;
  1611. u64 start;
  1612. u64 end;
  1613. struct extent_io_tree *pending_del;
  1614. struct extent_io_tree *pinned_extents;
  1615. WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex));
  1616. pending_del = &extent_root->fs_info->pending_del;
  1617. pinned_extents = &extent_root->fs_info->pinned_extents;
  1618. while(1) {
  1619. ret = find_first_extent_bit(pending_del, 0, &start, &end,
  1620. EXTENT_LOCKED);
  1621. if (ret)
  1622. break;
  1623. clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
  1624. GFP_NOFS);
  1625. if (!test_range_bit(&extent_root->fs_info->extent_ins,
  1626. start, end, EXTENT_LOCKED, 0)) {
  1627. update_pinned_extents(extent_root, start,
  1628. end + 1 - start, 1);
  1629. ret = __free_extent(trans, extent_root,
  1630. start, end + 1 - start,
  1631. extent_root->root_key.objectid,
  1632. 0, 0, 0, 0, 0);
  1633. } else {
  1634. clear_extent_bits(&extent_root->fs_info->extent_ins,
  1635. start, end, EXTENT_LOCKED, GFP_NOFS);
  1636. }
  1637. if (ret)
  1638. err = ret;
  1639. if (need_resched()) {
  1640. mutex_unlock(&extent_root->fs_info->alloc_mutex);
  1641. cond_resched();
  1642. mutex_lock(&extent_root->fs_info->alloc_mutex);
  1643. }
  1644. }
  1645. return err;
  1646. }
  1647. /*
  1648. * remove an extent from the root, returns 0 on success
  1649. */
  1650. static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
  1651. struct btrfs_root *root, u64 bytenr,
  1652. u64 num_bytes, u64 root_objectid,
  1653. u64 ref_generation, u64 owner_objectid,
  1654. u64 owner_offset, int pin)
  1655. {
  1656. struct btrfs_root *extent_root = root->fs_info->extent_root;
  1657. int pending_ret;
  1658. int ret;
  1659. WARN_ON(num_bytes < root->sectorsize);
  1660. if (!root->ref_cows)
  1661. ref_generation = 0;
  1662. if (root == extent_root) {
  1663. pin_down_bytes(root, bytenr, num_bytes, 1);
  1664. return 0;
  1665. }
  1666. ret = __free_extent(trans, root, bytenr, num_bytes, root_objectid,
  1667. ref_generation, owner_objectid, owner_offset,
  1668. pin, pin == 0);
  1669. finish_current_insert(trans, root->fs_info->extent_root);
  1670. pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
  1671. return ret ? ret : pending_ret;
  1672. }
  1673. int btrfs_free_extent(struct btrfs_trans_handle *trans,
  1674. struct btrfs_root *root, u64 bytenr,
  1675. u64 num_bytes, u64 root_objectid,
  1676. u64 ref_generation, u64 owner_objectid,
  1677. u64 owner_offset, int pin)
  1678. {
  1679. int ret;
  1680. maybe_lock_mutex(root);
  1681. ret = __btrfs_free_extent(trans, root, bytenr, num_bytes,
  1682. root_objectid, ref_generation,
  1683. owner_objectid, owner_offset, pin);
  1684. maybe_unlock_mutex(root);
  1685. return ret;
  1686. }
  1687. static u64 stripe_align(struct btrfs_root *root, u64 val)
  1688. {
  1689. u64 mask = ((u64)root->stripesize - 1);
  1690. u64 ret = (val + mask) & ~mask;
  1691. return ret;
  1692. }
  1693. /*
  1694. * walks the btree of allocated extents and find a hole of a given size.
  1695. * The key ins is changed to record the hole:
  1696. * ins->objectid == block start
  1697. * ins->flags = BTRFS_EXTENT_ITEM_KEY
  1698. * ins->offset == number of blocks
  1699. * Any available blocks before search_start are skipped.
  1700. */
  1701. static int noinline find_free_extent(struct btrfs_trans_handle *trans,
  1702. struct btrfs_root *orig_root,
  1703. u64 num_bytes, u64 empty_size,
  1704. u64 search_start, u64 search_end,
  1705. u64 hint_byte, struct btrfs_key *ins,
  1706. u64 exclude_start, u64 exclude_nr,
  1707. int data)
  1708. {
  1709. int ret;
  1710. u64 orig_search_start;
  1711. struct btrfs_root * root = orig_root->fs_info->extent_root;
  1712. struct btrfs_fs_info *info = root->fs_info;
  1713. u64 total_needed = num_bytes;
  1714. u64 *last_ptr = NULL;
  1715. struct btrfs_block_group_cache *block_group;
  1716. int full_scan = 0;
  1717. int wrapped = 0;
  1718. int chunk_alloc_done = 0;
  1719. int empty_cluster = 2 * 1024 * 1024;
  1720. int allowed_chunk_alloc = 0;
  1721. WARN_ON(num_bytes < root->sectorsize);
  1722. btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
  1723. if (orig_root->ref_cows || empty_size)
  1724. allowed_chunk_alloc = 1;
  1725. if (data & BTRFS_BLOCK_GROUP_METADATA) {
  1726. last_ptr = &root->fs_info->last_alloc;
  1727. empty_cluster = 256 * 1024;
  1728. }
  1729. if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) {
  1730. last_ptr = &root->fs_info->last_data_alloc;
  1731. }
  1732. if (last_ptr) {
  1733. if (*last_ptr)
  1734. hint_byte = *last_ptr;
  1735. else {
  1736. empty_size += empty_cluster;
  1737. }
  1738. }
  1739. search_start = max(search_start, first_logical_byte(root, 0));
  1740. orig_search_start = search_start;
  1741. if (search_end == (u64)-1)
  1742. search_end = btrfs_super_total_bytes(&info->super_copy);
  1743. if (hint_byte) {
  1744. block_group = btrfs_lookup_first_block_group(info, hint_byte);
  1745. if (!block_group)
  1746. hint_byte = search_start;
  1747. block_group = btrfs_find_block_group(root, block_group,
  1748. hint_byte, data, 1);
  1749. if (last_ptr && *last_ptr == 0 && block_group)
  1750. hint_byte = block_group->key.objectid;
  1751. } else {
  1752. block_group = btrfs_find_block_group(root,
  1753. trans->block_group,
  1754. search_start, data, 1);
  1755. }
  1756. search_start = max(search_start, hint_byte);
  1757. total_needed += empty_size;
  1758. check_failed:
  1759. if (!block_group) {
  1760. block_group = btrfs_lookup_first_block_group(info,
  1761. search_start);
  1762. if (!block_group)
  1763. block_group = btrfs_lookup_first_block_group(info,
  1764. orig_search_start);
  1765. }
  1766. if (full_scan && !chunk_alloc_done) {
  1767. if (allowed_chunk_alloc) {
  1768. do_chunk_alloc(trans, root,
  1769. num_bytes + 2 * 1024 * 1024, data, 1);
  1770. allowed_chunk_alloc = 0;
  1771. } else if (block_group && block_group_bits(block_group, data)) {
  1772. block_group->space_info->force_alloc = 1;
  1773. }
  1774. chunk_alloc_done = 1;
  1775. }
  1776. ret = find_search_start(root, &block_group, &search_start,
  1777. total_needed, data);
  1778. if (ret == -ENOSPC && last_ptr && *last_ptr) {
  1779. *last_ptr = 0;
  1780. block_group = btrfs_lookup_first_block_group(info,
  1781. orig_search_start);
  1782. search_start = orig_search_start;
  1783. ret = find_search_start(root, &block_group, &search_start,
  1784. total_needed, data);
  1785. }
  1786. if (ret == -ENOSPC)
  1787. goto enospc;
  1788. if (ret)
  1789. goto error;
  1790. if (last_ptr && *last_ptr && search_start != *last_ptr) {
  1791. *last_ptr = 0;
  1792. if (!empty_size) {
  1793. empty_size += empty_cluster;
  1794. total_needed += empty_size;
  1795. }
  1796. block_group = btrfs_lookup_first_block_group(info,
  1797. orig_search_start);
  1798. search_start = orig_search_start;
  1799. ret = find_search_start(root, &block_group,
  1800. &search_start, total_needed, data);
  1801. if (ret == -ENOSPC)
  1802. goto enospc;
  1803. if (ret)
  1804. goto error;
  1805. }
  1806. search_start = stripe_align(root, search_start);
  1807. ins->objectid = search_start;
  1808. ins->offset = num_bytes;
  1809. if (ins->objectid + num_bytes >= search_end)
  1810. goto enospc;
  1811. if (ins->objectid + num_bytes >
  1812. block_group->key.objectid + block_group->key.offset) {
  1813. search_start = block_group->key.objectid +
  1814. block_group->key.offset;
  1815. goto new_group;
  1816. }
  1817. if (test_range_bit(&info->extent_ins, ins->objectid,
  1818. ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
  1819. search_start = ins->objectid + num_bytes;
  1820. goto new_group;
  1821. }
  1822. if (test_range_bit(&info->pinned_extents, ins->objectid,
  1823. ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
  1824. search_start = ins->objectid + num_bytes;
  1825. goto new_group;
  1826. }
  1827. if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
  1828. ins->objectid < exclude_start + exclude_nr)) {
  1829. search_start = exclude_start + exclude_nr;
  1830. goto new_group;
  1831. }
  1832. if (!(data & BTRFS_BLOCK_GROUP_DATA)) {
  1833. block_group = btrfs_lookup_block_group(info, ins->objectid);
  1834. if (block_group)
  1835. trans->block_group = block_group;
  1836. }
  1837. ins->offset = num_bytes;
  1838. if (last_ptr) {
  1839. *last_ptr = ins->objectid + ins->offset;
  1840. if (*last_ptr ==
  1841. btrfs_super_total_bytes(&root->fs_info->super_copy)) {
  1842. *last_ptr = 0;
  1843. }
  1844. }
  1845. return 0;
  1846. new_group:
  1847. if (search_start + num_bytes >= search_end) {
  1848. enospc:
  1849. search_start = orig_search_start;
  1850. if (full_scan) {
  1851. ret = -ENOSPC;
  1852. goto error;
  1853. }
  1854. if (wrapped) {
  1855. if (!full_scan)
  1856. total_needed -= empty_size;
  1857. full_scan = 1;
  1858. } else
  1859. wrapped = 1;
  1860. }
  1861. block_group = btrfs_lookup_first_block_group(info, search_start);
  1862. cond_resched();
  1863. block_group = btrfs_find_block_group(root, block_group,
  1864. search_start, data, 0);
  1865. goto check_failed;
  1866. error:
  1867. return ret;
  1868. }
  1869. static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
  1870. struct btrfs_root *root,
  1871. u64 num_bytes, u64 min_alloc_size,
  1872. u64 empty_size, u64 hint_byte,
  1873. u64 search_end, struct btrfs_key *ins,
  1874. u64 data)
  1875. {
  1876. int ret;
  1877. u64 search_start = 0;
  1878. u64 alloc_profile;
  1879. struct btrfs_fs_info *info = root->fs_info;
  1880. if (data) {
  1881. alloc_profile = info->avail_data_alloc_bits &
  1882. info->data_alloc_profile;
  1883. data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
  1884. } else if (root == root->fs_info->chunk_root) {
  1885. alloc_profile = info->avail_system_alloc_bits &
  1886. info->system_alloc_profile;
  1887. data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
  1888. } else {
  1889. alloc_profile = info->avail_metadata_alloc_bits &
  1890. info->metadata_alloc_profile;
  1891. data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
  1892. }
  1893. again:
  1894. data = reduce_alloc_profile(root, data);
  1895. /*
  1896. * the only place that sets empty_size is btrfs_realloc_node, which
  1897. * is not called recursively on allocations
  1898. */
  1899. if (empty_size || root->ref_cows) {
  1900. if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
  1901. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  1902. 2 * 1024 * 1024,
  1903. BTRFS_BLOCK_GROUP_METADATA |
  1904. (info->metadata_alloc_profile &
  1905. info->avail_metadata_alloc_bits), 0);
  1906. BUG_ON(ret);
  1907. }
  1908. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  1909. num_bytes + 2 * 1024 * 1024, data, 0);
  1910. BUG_ON(ret);
  1911. }
  1912. WARN_ON(num_bytes < root->sectorsize);
  1913. ret = find_free_extent(trans, root, num_bytes, empty_size,
  1914. search_start, search_end, hint_byte, ins,
  1915. trans->alloc_exclude_start,
  1916. trans->alloc_exclude_nr, data);
  1917. if (ret == -ENOSPC && num_bytes > min_alloc_size) {
  1918. num_bytes = num_bytes >> 1;
  1919. num_bytes = max(num_bytes, min_alloc_size);
  1920. do_chunk_alloc(trans, root->fs_info->extent_root,
  1921. num_bytes, data, 1);
  1922. goto again;
  1923. }
  1924. if (ret) {
  1925. printk("allocation failed flags %Lu\n", data);
  1926. BUG();
  1927. }
  1928. clear_extent_dirty(&root->fs_info->free_space_cache,
  1929. ins->objectid, ins->objectid + ins->offset - 1,
  1930. GFP_NOFS);
  1931. return 0;
  1932. }
  1933. int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
  1934. {
  1935. maybe_lock_mutex(root);
  1936. set_extent_dirty(&root->fs_info->free_space_cache,
  1937. start, start + len - 1, GFP_NOFS);
  1938. maybe_unlock_mutex(root);
  1939. return 0;
  1940. }
  1941. int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
  1942. struct btrfs_root *root,
  1943. u64 num_bytes, u64 min_alloc_size,
  1944. u64 empty_size, u64 hint_byte,
  1945. u64 search_end, struct btrfs_key *ins,
  1946. u64 data)
  1947. {
  1948. int ret;
  1949. maybe_lock_mutex(root);
  1950. ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
  1951. empty_size, hint_byte, search_end, ins,
  1952. data);
  1953. maybe_unlock_mutex(root);
  1954. return ret;
  1955. }
  1956. static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
  1957. struct btrfs_root *root,
  1958. u64 root_objectid, u64 ref_generation,
  1959. u64 owner, u64 owner_offset,
  1960. struct btrfs_key *ins)
  1961. {
  1962. int ret;
  1963. int pending_ret;
  1964. u64 super_used;
  1965. u64 root_used;
  1966. u64 num_bytes = ins->offset;
  1967. u32 sizes[2];
  1968. struct btrfs_fs_info *info = root->fs_info;
  1969. struct btrfs_root *extent_root = info->extent_root;
  1970. struct btrfs_extent_item *extent_item;
  1971. struct btrfs_extent_ref *ref;
  1972. struct btrfs_path *path;
  1973. struct btrfs_key keys[2];
  1974. /* block accounting for super block */
  1975. spin_lock_irq(&info->delalloc_lock);
  1976. super_used = btrfs_super_bytes_used(&info->super_copy);
  1977. btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
  1978. spin_unlock_irq(&info->delalloc_lock);
  1979. /* block accounting for root item */
  1980. root_used = btrfs_root_used(&root->root_item);
  1981. btrfs_set_root_used(&root->root_item, root_used + num_bytes);
  1982. if (root == extent_root) {
  1983. set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
  1984. ins->objectid + ins->offset - 1,
  1985. EXTENT_LOCKED, GFP_NOFS);
  1986. goto update_block;
  1987. }
  1988. memcpy(&keys[0], ins, sizeof(*ins));
  1989. keys[1].offset = hash_extent_ref(root_objectid, ref_generation,
  1990. owner, owner_offset);
  1991. keys[1].objectid = ins->objectid;
  1992. keys[1].type = BTRFS_EXTENT_REF_KEY;
  1993. sizes[0] = sizeof(*extent_item);
  1994. sizes[1] = sizeof(*ref);
  1995. path = btrfs_alloc_path();
  1996. BUG_ON(!path);
  1997. ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
  1998. sizes, 2);
  1999. BUG_ON(ret);
  2000. extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2001. struct btrfs_extent_item);
  2002. btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
  2003. ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
  2004. struct btrfs_extent_ref);
  2005. btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
  2006. btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
  2007. btrfs_set_ref_objectid(path->nodes[0], ref, owner);
  2008. btrfs_set_ref_offset(path->nodes[0], ref, owner_offset);
  2009. btrfs_mark_buffer_dirty(path->nodes[0]);
  2010. trans->alloc_exclude_start = 0;
  2011. trans->alloc_exclude_nr = 0;
  2012. btrfs_free_path(path);
  2013. finish_current_insert(trans, extent_root);
  2014. pending_ret = del_pending_extents(trans, extent_root);
  2015. if (ret)
  2016. goto out;
  2017. if (pending_ret) {
  2018. ret = pending_ret;
  2019. goto out;
  2020. }
  2021. update_block:
  2022. ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
  2023. if (ret) {
  2024. printk("update block group failed for %Lu %Lu\n",
  2025. ins->objectid, ins->offset);
  2026. BUG();
  2027. }
  2028. out:
  2029. return ret;
  2030. }
  2031. int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
  2032. struct btrfs_root *root,
  2033. u64 root_objectid, u64 ref_generation,
  2034. u64 owner, u64 owner_offset,
  2035. struct btrfs_key *ins)
  2036. {
  2037. int ret;
  2038. maybe_lock_mutex(root);
  2039. ret = __btrfs_alloc_reserved_extent(trans, root, root_objectid,
  2040. ref_generation, owner,
  2041. owner_offset, ins);
  2042. maybe_unlock_mutex(root);
  2043. return ret;
  2044. }
  2045. /*
  2046. * finds a free extent and does all the dirty work required for allocation
  2047. * returns the key for the extent through ins, and a tree buffer for
  2048. * the first block of the extent through buf.
  2049. *
  2050. * returns 0 if everything worked, non-zero otherwise.
  2051. */
  2052. int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
  2053. struct btrfs_root *root,
  2054. u64 num_bytes, u64 min_alloc_size,
  2055. u64 root_objectid, u64 ref_generation,
  2056. u64 owner, u64 owner_offset,
  2057. u64 empty_size, u64 hint_byte,
  2058. u64 search_end, struct btrfs_key *ins, u64 data)
  2059. {
  2060. int ret;
  2061. maybe_lock_mutex(root);
  2062. ret = __btrfs_reserve_extent(trans, root, num_bytes,
  2063. min_alloc_size, empty_size, hint_byte,
  2064. search_end, ins, data);
  2065. BUG_ON(ret);
  2066. ret = __btrfs_alloc_reserved_extent(trans, root, root_objectid,
  2067. ref_generation, owner,
  2068. owner_offset, ins);
  2069. BUG_ON(ret);
  2070. maybe_unlock_mutex(root);
  2071. return ret;
  2072. }
  2073. struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
  2074. struct btrfs_root *root,
  2075. u64 bytenr, u32 blocksize)
  2076. {
  2077. struct extent_buffer *buf;
  2078. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  2079. if (!buf)
  2080. return ERR_PTR(-ENOMEM);
  2081. btrfs_set_header_generation(buf, trans->transid);
  2082. btrfs_tree_lock(buf);
  2083. clean_tree_block(trans, root, buf);
  2084. btrfs_set_buffer_uptodate(buf);
  2085. set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
  2086. buf->start + buf->len - 1, GFP_NOFS);
  2087. trans->blocks_used++;
  2088. return buf;
  2089. }
  2090. /*
  2091. * helper function to allocate a block for a given tree
  2092. * returns the tree buffer or NULL.
  2093. */
  2094. struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
  2095. struct btrfs_root *root,
  2096. u32 blocksize,
  2097. u64 root_objectid,
  2098. u64 ref_generation,
  2099. u64 first_objectid,
  2100. int level,
  2101. u64 hint,
  2102. u64 empty_size)
  2103. {
  2104. struct btrfs_key ins;
  2105. int ret;
  2106. struct extent_buffer *buf;
  2107. ret = btrfs_alloc_extent(trans, root, blocksize, blocksize,
  2108. root_objectid, ref_generation,
  2109. level, first_objectid, empty_size, hint,
  2110. (u64)-1, &ins, 0);
  2111. if (ret) {
  2112. BUG_ON(ret > 0);
  2113. return ERR_PTR(ret);
  2114. }
  2115. buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize);
  2116. return buf;
  2117. }
  2118. static int noinline drop_leaf_ref_no_cache(struct btrfs_trans_handle *trans,
  2119. struct btrfs_root *root,
  2120. struct extent_buffer *leaf)
  2121. {
  2122. u64 leaf_owner;
  2123. u64 leaf_generation;
  2124. struct btrfs_key key;
  2125. struct btrfs_file_extent_item *fi;
  2126. int i;
  2127. int nritems;
  2128. int ret;
  2129. BUG_ON(!btrfs_is_leaf(leaf));
  2130. nritems = btrfs_header_nritems(leaf);
  2131. leaf_owner = btrfs_header_owner(leaf);
  2132. leaf_generation = btrfs_header_generation(leaf);
  2133. for (i = 0; i < nritems; i++) {
  2134. u64 disk_bytenr;
  2135. cond_resched();
  2136. btrfs_item_key_to_cpu(leaf, &key, i);
  2137. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  2138. continue;
  2139. fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
  2140. if (btrfs_file_extent_type(leaf, fi) ==
  2141. BTRFS_FILE_EXTENT_INLINE)
  2142. continue;
  2143. /*
  2144. * FIXME make sure to insert a trans record that
  2145. * repeats the snapshot del on crash
  2146. */
  2147. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  2148. if (disk_bytenr == 0)
  2149. continue;
  2150. mutex_lock(&root->fs_info->alloc_mutex);
  2151. ret = __btrfs_free_extent(trans, root, disk_bytenr,
  2152. btrfs_file_extent_disk_num_bytes(leaf, fi),
  2153. leaf_owner, leaf_generation,
  2154. key.objectid, key.offset, 0);
  2155. mutex_unlock(&root->fs_info->alloc_mutex);
  2156. atomic_inc(&root->fs_info->throttle_gen);
  2157. wake_up(&root->fs_info->transaction_throttle);
  2158. cond_resched();
  2159. BUG_ON(ret);
  2160. }
  2161. return 0;
  2162. }
  2163. static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans,
  2164. struct btrfs_root *root,
  2165. struct btrfs_leaf_ref *ref)
  2166. {
  2167. int i;
  2168. int ret;
  2169. struct btrfs_extent_info *info = ref->extents;
  2170. for (i = 0; i < ref->nritems; i++) {
  2171. mutex_lock(&root->fs_info->alloc_mutex);
  2172. ret = __btrfs_free_extent(trans, root,
  2173. info->bytenr, info->num_bytes,
  2174. ref->owner, ref->generation,
  2175. info->objectid, info->offset, 0);
  2176. mutex_unlock(&root->fs_info->alloc_mutex);
  2177. atomic_inc(&root->fs_info->throttle_gen);
  2178. wake_up(&root->fs_info->transaction_throttle);
  2179. cond_resched();
  2180. BUG_ON(ret);
  2181. info++;
  2182. }
  2183. return 0;
  2184. }
  2185. int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start, u64 len,
  2186. u32 *refs)
  2187. {
  2188. int ret;
  2189. ret = lookup_extent_ref(NULL, root, start, len, refs);
  2190. BUG_ON(ret);
  2191. #if 0 // some debugging code in case we see problems here
  2192. /* if the refs count is one, it won't get increased again. But
  2193. * if the ref count is > 1, someone may be decreasing it at
  2194. * the same time we are.
  2195. */
  2196. if (*refs != 1) {
  2197. struct extent_buffer *eb = NULL;
  2198. eb = btrfs_find_create_tree_block(root, start, len);
  2199. if (eb)
  2200. btrfs_tree_lock(eb);
  2201. mutex_lock(&root->fs_info->alloc_mutex);
  2202. ret = lookup_extent_ref(NULL, root, start, len, refs);
  2203. BUG_ON(ret);
  2204. mutex_unlock(&root->fs_info->alloc_mutex);
  2205. if (eb) {
  2206. btrfs_tree_unlock(eb);
  2207. free_extent_buffer(eb);
  2208. }
  2209. if (*refs == 1) {
  2210. printk("block %llu went down to one during drop_snap\n",
  2211. (unsigned long long)start);
  2212. }
  2213. }
  2214. #endif
  2215. cond_resched();
  2216. return ret;
  2217. }
  2218. /*
  2219. * helper function for drop_snapshot, this walks down the tree dropping ref
  2220. * counts as it goes.
  2221. */
  2222. static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
  2223. struct btrfs_root *root,
  2224. struct btrfs_path *path, int *level)
  2225. {
  2226. u64 root_owner;
  2227. u64 root_gen;
  2228. u64 bytenr;
  2229. u64 ptr_gen;
  2230. struct extent_buffer *next;
  2231. struct extent_buffer *cur;
  2232. struct extent_buffer *parent;
  2233. struct btrfs_leaf_ref *ref;
  2234. u32 blocksize;
  2235. int ret;
  2236. u32 refs;
  2237. WARN_ON(*level < 0);
  2238. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  2239. ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start,
  2240. path->nodes[*level]->len, &refs);
  2241. BUG_ON(ret);
  2242. if (refs > 1)
  2243. goto out;
  2244. /*
  2245. * walk down to the last node level and free all the leaves
  2246. */
  2247. while(*level >= 0) {
  2248. WARN_ON(*level < 0);
  2249. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  2250. cur = path->nodes[*level];
  2251. if (btrfs_header_level(cur) != *level)
  2252. WARN_ON(1);
  2253. if (path->slots[*level] >=
  2254. btrfs_header_nritems(cur))
  2255. break;
  2256. if (*level == 0) {
  2257. ret = drop_leaf_ref_no_cache(trans, root, cur);
  2258. BUG_ON(ret);
  2259. break;
  2260. }
  2261. bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
  2262. ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
  2263. blocksize = btrfs_level_size(root, *level - 1);
  2264. ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs);
  2265. BUG_ON(ret);
  2266. if (refs != 1) {
  2267. parent = path->nodes[*level];
  2268. root_owner = btrfs_header_owner(parent);
  2269. root_gen = btrfs_header_generation(parent);
  2270. path->slots[*level]++;
  2271. mutex_lock(&root->fs_info->alloc_mutex);
  2272. ret = __btrfs_free_extent(trans, root, bytenr,
  2273. blocksize, root_owner,
  2274. root_gen, 0, 0, 1);
  2275. BUG_ON(ret);
  2276. mutex_unlock(&root->fs_info->alloc_mutex);
  2277. atomic_inc(&root->fs_info->throttle_gen);
  2278. wake_up(&root->fs_info->transaction_throttle);
  2279. cond_resched();
  2280. continue;
  2281. }
  2282. /*
  2283. * at this point, we have a single ref, and since the
  2284. * only place referencing this extent is a dead root
  2285. * the reference count should never go higher.
  2286. * So, we don't need to check it again
  2287. */
  2288. if (*level == 1) {
  2289. struct btrfs_key key;
  2290. btrfs_node_key_to_cpu(cur, &key, path->slots[*level]);
  2291. ref = btrfs_lookup_leaf_ref(root, bytenr);
  2292. if (ref) {
  2293. ret = drop_leaf_ref(trans, root, ref);
  2294. BUG_ON(ret);
  2295. btrfs_remove_leaf_ref(root, ref);
  2296. btrfs_free_leaf_ref(root, ref);
  2297. *level = 0;
  2298. break;
  2299. }
  2300. if (printk_ratelimit())
  2301. printk("leaf ref miss for bytenr %llu\n",
  2302. (unsigned long long)bytenr);
  2303. }
  2304. next = btrfs_find_tree_block(root, bytenr, blocksize);
  2305. if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
  2306. free_extent_buffer(next);
  2307. next = read_tree_block(root, bytenr, blocksize,
  2308. ptr_gen);
  2309. cond_resched();
  2310. #if 0
  2311. /*
  2312. * this is a debugging check and can go away
  2313. * the ref should never go all the way down to 1
  2314. * at this point
  2315. */
  2316. ret = lookup_extent_ref(NULL, root, bytenr, blocksize,
  2317. &refs);
  2318. BUG_ON(ret);
  2319. WARN_ON(refs != 1);
  2320. #endif
  2321. }
  2322. WARN_ON(*level <= 0);
  2323. if (path->nodes[*level-1])
  2324. free_extent_buffer(path->nodes[*level-1]);
  2325. path->nodes[*level-1] = next;
  2326. *level = btrfs_header_level(next);
  2327. path->slots[*level] = 0;
  2328. cond_resched();
  2329. }
  2330. out:
  2331. WARN_ON(*level < 0);
  2332. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  2333. if (path->nodes[*level] == root->node) {
  2334. parent = path->nodes[*level];
  2335. bytenr = path->nodes[*level]->start;
  2336. } else {
  2337. parent = path->nodes[*level + 1];
  2338. bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
  2339. }
  2340. blocksize = btrfs_level_size(root, *level);
  2341. root_owner = btrfs_header_owner(parent);
  2342. root_gen = btrfs_header_generation(parent);
  2343. mutex_lock(&root->fs_info->alloc_mutex);
  2344. ret = __btrfs_free_extent(trans, root, bytenr, blocksize,
  2345. root_owner, root_gen, 0, 0, 1);
  2346. free_extent_buffer(path->nodes[*level]);
  2347. path->nodes[*level] = NULL;
  2348. *level += 1;
  2349. BUG_ON(ret);
  2350. mutex_unlock(&root->fs_info->alloc_mutex);
  2351. cond_resched();
  2352. return 0;
  2353. }
  2354. /*
  2355. * helper for dropping snapshots. This walks back up the tree in the path
  2356. * to find the first node higher up where we haven't yet gone through
  2357. * all the slots
  2358. */
  2359. static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
  2360. struct btrfs_root *root,
  2361. struct btrfs_path *path, int *level)
  2362. {
  2363. u64 root_owner;
  2364. u64 root_gen;
  2365. struct btrfs_root_item *root_item = &root->root_item;
  2366. int i;
  2367. int slot;
  2368. int ret;
  2369. for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
  2370. slot = path->slots[i];
  2371. if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
  2372. struct extent_buffer *node;
  2373. struct btrfs_disk_key disk_key;
  2374. node = path->nodes[i];
  2375. path->slots[i]++;
  2376. *level = i;
  2377. WARN_ON(*level == 0);
  2378. btrfs_node_key(node, &disk_key, path->slots[i]);
  2379. memcpy(&root_item->drop_progress,
  2380. &disk_key, sizeof(disk_key));
  2381. root_item->drop_level = i;
  2382. return 0;
  2383. } else {
  2384. if (path->nodes[*level] == root->node) {
  2385. root_owner = root->root_key.objectid;
  2386. root_gen =
  2387. btrfs_header_generation(path->nodes[*level]);
  2388. } else {
  2389. struct extent_buffer *node;
  2390. node = path->nodes[*level + 1];
  2391. root_owner = btrfs_header_owner(node);
  2392. root_gen = btrfs_header_generation(node);
  2393. }
  2394. ret = btrfs_free_extent(trans, root,
  2395. path->nodes[*level]->start,
  2396. path->nodes[*level]->len,
  2397. root_owner, root_gen, 0, 0, 1);
  2398. BUG_ON(ret);
  2399. free_extent_buffer(path->nodes[*level]);
  2400. path->nodes[*level] = NULL;
  2401. *level = i + 1;
  2402. }
  2403. }
  2404. return 1;
  2405. }
  2406. /*
  2407. * drop the reference count on the tree rooted at 'snap'. This traverses
  2408. * the tree freeing any blocks that have a ref count of zero after being
  2409. * decremented.
  2410. */
  2411. int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
  2412. *root)
  2413. {
  2414. int ret = 0;
  2415. int wret;
  2416. int level;
  2417. struct btrfs_path *path;
  2418. int i;
  2419. int orig_level;
  2420. struct btrfs_root_item *root_item = &root->root_item;
  2421. WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
  2422. path = btrfs_alloc_path();
  2423. BUG_ON(!path);
  2424. level = btrfs_header_level(root->node);
  2425. orig_level = level;
  2426. if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
  2427. path->nodes[level] = root->node;
  2428. extent_buffer_get(root->node);
  2429. path->slots[level] = 0;
  2430. } else {
  2431. struct btrfs_key key;
  2432. struct btrfs_disk_key found_key;
  2433. struct extent_buffer *node;
  2434. btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
  2435. level = root_item->drop_level;
  2436. path->lowest_level = level;
  2437. wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2438. if (wret < 0) {
  2439. ret = wret;
  2440. goto out;
  2441. }
  2442. node = path->nodes[level];
  2443. btrfs_node_key(node, &found_key, path->slots[level]);
  2444. WARN_ON(memcmp(&found_key, &root_item->drop_progress,
  2445. sizeof(found_key)));
  2446. /*
  2447. * unlock our path, this is safe because only this
  2448. * function is allowed to delete this snapshot
  2449. */
  2450. for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
  2451. if (path->nodes[i] && path->locks[i]) {
  2452. path->locks[i] = 0;
  2453. btrfs_tree_unlock(path->nodes[i]);
  2454. }
  2455. }
  2456. }
  2457. while(1) {
  2458. wret = walk_down_tree(trans, root, path, &level);
  2459. if (wret > 0)
  2460. break;
  2461. if (wret < 0)
  2462. ret = wret;
  2463. wret = walk_up_tree(trans, root, path, &level);
  2464. if (wret > 0)
  2465. break;
  2466. if (wret < 0)
  2467. ret = wret;
  2468. if (trans->transaction->in_commit) {
  2469. ret = -EAGAIN;
  2470. break;
  2471. }
  2472. atomic_inc(&root->fs_info->throttle_gen);
  2473. wake_up(&root->fs_info->transaction_throttle);
  2474. }
  2475. for (i = 0; i <= orig_level; i++) {
  2476. if (path->nodes[i]) {
  2477. free_extent_buffer(path->nodes[i]);
  2478. path->nodes[i] = NULL;
  2479. }
  2480. }
  2481. out:
  2482. btrfs_free_path(path);
  2483. return ret;
  2484. }
  2485. int btrfs_free_block_groups(struct btrfs_fs_info *info)
  2486. {
  2487. u64 start;
  2488. u64 end;
  2489. u64 ptr;
  2490. int ret;
  2491. mutex_lock(&info->alloc_mutex);
  2492. while(1) {
  2493. ret = find_first_extent_bit(&info->block_group_cache, 0,
  2494. &start, &end, (unsigned int)-1);
  2495. if (ret)
  2496. break;
  2497. ret = get_state_private(&info->block_group_cache, start, &ptr);
  2498. if (!ret)
  2499. kfree((void *)(unsigned long)ptr);
  2500. clear_extent_bits(&info->block_group_cache, start,
  2501. end, (unsigned int)-1, GFP_NOFS);
  2502. }
  2503. while(1) {
  2504. ret = find_first_extent_bit(&info->free_space_cache, 0,
  2505. &start, &end, EXTENT_DIRTY);
  2506. if (ret)
  2507. break;
  2508. clear_extent_dirty(&info->free_space_cache, start,
  2509. end, GFP_NOFS);
  2510. }
  2511. mutex_unlock(&info->alloc_mutex);
  2512. return 0;
  2513. }
  2514. static unsigned long calc_ra(unsigned long start, unsigned long last,
  2515. unsigned long nr)
  2516. {
  2517. return min(last, start + nr - 1);
  2518. }
  2519. static int noinline relocate_inode_pages(struct inode *inode, u64 start,
  2520. u64 len)
  2521. {
  2522. u64 page_start;
  2523. u64 page_end;
  2524. unsigned long last_index;
  2525. unsigned long i;
  2526. struct page *page;
  2527. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  2528. struct file_ra_state *ra;
  2529. unsigned long total_read = 0;
  2530. unsigned long ra_pages;
  2531. struct btrfs_ordered_extent *ordered;
  2532. struct btrfs_trans_handle *trans;
  2533. ra = kzalloc(sizeof(*ra), GFP_NOFS);
  2534. mutex_lock(&inode->i_mutex);
  2535. i = start >> PAGE_CACHE_SHIFT;
  2536. last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
  2537. ra_pages = BTRFS_I(inode)->root->fs_info->bdi.ra_pages;
  2538. file_ra_state_init(ra, inode->i_mapping);
  2539. for (; i <= last_index; i++) {
  2540. if (total_read % ra_pages == 0) {
  2541. btrfs_force_ra(inode->i_mapping, ra, NULL, i,
  2542. calc_ra(i, last_index, ra_pages));
  2543. }
  2544. total_read++;
  2545. again:
  2546. if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
  2547. goto truncate_racing;
  2548. page = grab_cache_page(inode->i_mapping, i);
  2549. if (!page) {
  2550. goto out_unlock;
  2551. }
  2552. if (!PageUptodate(page)) {
  2553. btrfs_readpage(NULL, page);
  2554. lock_page(page);
  2555. if (!PageUptodate(page)) {
  2556. unlock_page(page);
  2557. page_cache_release(page);
  2558. goto out_unlock;
  2559. }
  2560. }
  2561. wait_on_page_writeback(page);
  2562. page_start = (u64)page->index << PAGE_CACHE_SHIFT;
  2563. page_end = page_start + PAGE_CACHE_SIZE - 1;
  2564. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  2565. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  2566. if (ordered) {
  2567. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  2568. unlock_page(page);
  2569. page_cache_release(page);
  2570. btrfs_start_ordered_extent(inode, ordered, 1);
  2571. btrfs_put_ordered_extent(ordered);
  2572. goto again;
  2573. }
  2574. set_page_extent_mapped(page);
  2575. /*
  2576. * make sure page_mkwrite is called for this page if userland
  2577. * wants to change it from mmap
  2578. */
  2579. clear_page_dirty_for_io(page);
  2580. set_extent_delalloc(io_tree, page_start,
  2581. page_end, GFP_NOFS);
  2582. set_page_dirty(page);
  2583. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  2584. unlock_page(page);
  2585. page_cache_release(page);
  2586. }
  2587. out_unlock:
  2588. /* we have to start the IO in order to get the ordered extents
  2589. * instantiated. This allows the relocation to code to wait
  2590. * for all the ordered extents to hit the disk.
  2591. *
  2592. * Otherwise, it would constantly loop over the same extents
  2593. * because the old ones don't get deleted until the IO is
  2594. * started
  2595. */
  2596. btrfs_fdatawrite_range(inode->i_mapping, start, start + len - 1,
  2597. WB_SYNC_NONE);
  2598. kfree(ra);
  2599. trans = btrfs_start_transaction(BTRFS_I(inode)->root, 1);
  2600. if (trans) {
  2601. btrfs_end_transaction(trans, BTRFS_I(inode)->root);
  2602. mark_inode_dirty(inode);
  2603. }
  2604. mutex_unlock(&inode->i_mutex);
  2605. return 0;
  2606. truncate_racing:
  2607. vmtruncate(inode, inode->i_size);
  2608. balance_dirty_pages_ratelimited_nr(inode->i_mapping,
  2609. total_read);
  2610. goto out_unlock;
  2611. }
  2612. /*
  2613. * The back references tell us which tree holds a ref on a block,
  2614. * but it is possible for the tree root field in the reference to
  2615. * reflect the original root before a snapshot was made. In this
  2616. * case we should search through all the children of a given root
  2617. * to find potential holders of references on a block.
  2618. *
  2619. * Instead, we do something a little less fancy and just search
  2620. * all the roots for a given key/block combination.
  2621. */
  2622. static int find_root_for_ref(struct btrfs_root *root,
  2623. struct btrfs_path *path,
  2624. struct btrfs_key *key0,
  2625. int level,
  2626. int file_key,
  2627. struct btrfs_root **found_root,
  2628. u64 bytenr)
  2629. {
  2630. struct btrfs_key root_location;
  2631. struct btrfs_root *cur_root = *found_root;
  2632. struct btrfs_file_extent_item *file_extent;
  2633. u64 root_search_start = BTRFS_FS_TREE_OBJECTID;
  2634. u64 found_bytenr;
  2635. int ret;
  2636. root_location.offset = (u64)-1;
  2637. root_location.type = BTRFS_ROOT_ITEM_KEY;
  2638. path->lowest_level = level;
  2639. path->reada = 0;
  2640. while(1) {
  2641. ret = btrfs_search_slot(NULL, cur_root, key0, path, 0, 0);
  2642. found_bytenr = 0;
  2643. if (ret == 0 && file_key) {
  2644. struct extent_buffer *leaf = path->nodes[0];
  2645. file_extent = btrfs_item_ptr(leaf, path->slots[0],
  2646. struct btrfs_file_extent_item);
  2647. if (btrfs_file_extent_type(leaf, file_extent) ==
  2648. BTRFS_FILE_EXTENT_REG) {
  2649. found_bytenr =
  2650. btrfs_file_extent_disk_bytenr(leaf,
  2651. file_extent);
  2652. }
  2653. } else if (!file_key) {
  2654. if (path->nodes[level])
  2655. found_bytenr = path->nodes[level]->start;
  2656. }
  2657. btrfs_release_path(cur_root, path);
  2658. if (found_bytenr == bytenr) {
  2659. *found_root = cur_root;
  2660. ret = 0;
  2661. goto out;
  2662. }
  2663. ret = btrfs_search_root(root->fs_info->tree_root,
  2664. root_search_start, &root_search_start);
  2665. if (ret)
  2666. break;
  2667. root_location.objectid = root_search_start;
  2668. cur_root = btrfs_read_fs_root_no_name(root->fs_info,
  2669. &root_location);
  2670. if (!cur_root) {
  2671. ret = 1;
  2672. break;
  2673. }
  2674. }
  2675. out:
  2676. path->lowest_level = 0;
  2677. return ret;
  2678. }
  2679. /*
  2680. * note, this releases the path
  2681. */
  2682. static int noinline relocate_one_reference(struct btrfs_root *extent_root,
  2683. struct btrfs_path *path,
  2684. struct btrfs_key *extent_key,
  2685. u64 *last_file_objectid,
  2686. u64 *last_file_offset,
  2687. u64 *last_file_root,
  2688. u64 last_extent)
  2689. {
  2690. struct inode *inode;
  2691. struct btrfs_root *found_root;
  2692. struct btrfs_key root_location;
  2693. struct btrfs_key found_key;
  2694. struct btrfs_extent_ref *ref;
  2695. u64 ref_root;
  2696. u64 ref_gen;
  2697. u64 ref_objectid;
  2698. u64 ref_offset;
  2699. int ret;
  2700. int level;
  2701. WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex));
  2702. ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2703. struct btrfs_extent_ref);
  2704. ref_root = btrfs_ref_root(path->nodes[0], ref);
  2705. ref_gen = btrfs_ref_generation(path->nodes[0], ref);
  2706. ref_objectid = btrfs_ref_objectid(path->nodes[0], ref);
  2707. ref_offset = btrfs_ref_offset(path->nodes[0], ref);
  2708. btrfs_release_path(extent_root, path);
  2709. root_location.objectid = ref_root;
  2710. if (ref_gen == 0)
  2711. root_location.offset = 0;
  2712. else
  2713. root_location.offset = (u64)-1;
  2714. root_location.type = BTRFS_ROOT_ITEM_KEY;
  2715. found_root = btrfs_read_fs_root_no_name(extent_root->fs_info,
  2716. &root_location);
  2717. BUG_ON(!found_root);
  2718. mutex_unlock(&extent_root->fs_info->alloc_mutex);
  2719. if (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
  2720. found_key.objectid = ref_objectid;
  2721. found_key.type = BTRFS_EXTENT_DATA_KEY;
  2722. found_key.offset = ref_offset;
  2723. level = 0;
  2724. if (last_extent == extent_key->objectid &&
  2725. *last_file_objectid == ref_objectid &&
  2726. *last_file_offset == ref_offset &&
  2727. *last_file_root == ref_root)
  2728. goto out;
  2729. ret = find_root_for_ref(extent_root, path, &found_key,
  2730. level, 1, &found_root,
  2731. extent_key->objectid);
  2732. if (ret)
  2733. goto out;
  2734. if (last_extent == extent_key->objectid &&
  2735. *last_file_objectid == ref_objectid &&
  2736. *last_file_offset == ref_offset &&
  2737. *last_file_root == ref_root)
  2738. goto out;
  2739. inode = btrfs_iget_locked(extent_root->fs_info->sb,
  2740. ref_objectid, found_root);
  2741. if (inode->i_state & I_NEW) {
  2742. /* the inode and parent dir are two different roots */
  2743. BTRFS_I(inode)->root = found_root;
  2744. BTRFS_I(inode)->location.objectid = ref_objectid;
  2745. BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
  2746. BTRFS_I(inode)->location.offset = 0;
  2747. btrfs_read_locked_inode(inode);
  2748. unlock_new_inode(inode);
  2749. }
  2750. /* this can happen if the reference is not against
  2751. * the latest version of the tree root
  2752. */
  2753. if (is_bad_inode(inode))
  2754. goto out;
  2755. *last_file_objectid = inode->i_ino;
  2756. *last_file_root = found_root->root_key.objectid;
  2757. *last_file_offset = ref_offset;
  2758. relocate_inode_pages(inode, ref_offset, extent_key->offset);
  2759. iput(inode);
  2760. } else {
  2761. struct btrfs_trans_handle *trans;
  2762. struct extent_buffer *eb;
  2763. int needs_lock = 0;
  2764. eb = read_tree_block(found_root, extent_key->objectid,
  2765. extent_key->offset, 0);
  2766. btrfs_tree_lock(eb);
  2767. level = btrfs_header_level(eb);
  2768. if (level == 0)
  2769. btrfs_item_key_to_cpu(eb, &found_key, 0);
  2770. else
  2771. btrfs_node_key_to_cpu(eb, &found_key, 0);
  2772. btrfs_tree_unlock(eb);
  2773. free_extent_buffer(eb);
  2774. ret = find_root_for_ref(extent_root, path, &found_key,
  2775. level, 0, &found_root,
  2776. extent_key->objectid);
  2777. if (ret)
  2778. goto out;
  2779. /*
  2780. * right here almost anything could happen to our key,
  2781. * but that's ok. The cow below will either relocate it
  2782. * or someone else will have relocated it. Either way,
  2783. * it is in a different spot than it was before and
  2784. * we're happy.
  2785. */
  2786. trans = btrfs_start_transaction(found_root, 1);
  2787. if (found_root == extent_root->fs_info->extent_root ||
  2788. found_root == extent_root->fs_info->chunk_root ||
  2789. found_root == extent_root->fs_info->dev_root) {
  2790. needs_lock = 1;
  2791. mutex_lock(&extent_root->fs_info->alloc_mutex);
  2792. }
  2793. path->lowest_level = level;
  2794. path->reada = 2;
  2795. ret = btrfs_search_slot(trans, found_root, &found_key, path,
  2796. 0, 1);
  2797. path->lowest_level = 0;
  2798. btrfs_release_path(found_root, path);
  2799. if (found_root == found_root->fs_info->extent_root)
  2800. btrfs_extent_post_op(trans, found_root);
  2801. if (needs_lock)
  2802. mutex_unlock(&extent_root->fs_info->alloc_mutex);
  2803. btrfs_end_transaction(trans, found_root);
  2804. }
  2805. out:
  2806. mutex_lock(&extent_root->fs_info->alloc_mutex);
  2807. return 0;
  2808. }
  2809. static int noinline del_extent_zero(struct btrfs_root *extent_root,
  2810. struct btrfs_path *path,
  2811. struct btrfs_key *extent_key)
  2812. {
  2813. int ret;
  2814. struct btrfs_trans_handle *trans;
  2815. trans = btrfs_start_transaction(extent_root, 1);
  2816. ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
  2817. if (ret > 0) {
  2818. ret = -EIO;
  2819. goto out;
  2820. }
  2821. if (ret < 0)
  2822. goto out;
  2823. ret = btrfs_del_item(trans, extent_root, path);
  2824. out:
  2825. btrfs_end_transaction(trans, extent_root);
  2826. return ret;
  2827. }
  2828. static int noinline relocate_one_extent(struct btrfs_root *extent_root,
  2829. struct btrfs_path *path,
  2830. struct btrfs_key *extent_key)
  2831. {
  2832. struct btrfs_key key;
  2833. struct btrfs_key found_key;
  2834. struct extent_buffer *leaf;
  2835. u64 last_file_objectid = 0;
  2836. u64 last_file_root = 0;
  2837. u64 last_file_offset = (u64)-1;
  2838. u64 last_extent = 0;
  2839. u32 nritems;
  2840. u32 item_size;
  2841. int ret = 0;
  2842. if (extent_key->objectid == 0) {
  2843. ret = del_extent_zero(extent_root, path, extent_key);
  2844. goto out;
  2845. }
  2846. key.objectid = extent_key->objectid;
  2847. key.type = BTRFS_EXTENT_REF_KEY;
  2848. key.offset = 0;
  2849. while(1) {
  2850. ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
  2851. if (ret < 0)
  2852. goto out;
  2853. ret = 0;
  2854. leaf = path->nodes[0];
  2855. nritems = btrfs_header_nritems(leaf);
  2856. if (path->slots[0] == nritems) {
  2857. ret = btrfs_next_leaf(extent_root, path);
  2858. if (ret > 0) {
  2859. ret = 0;
  2860. goto out;
  2861. }
  2862. if (ret < 0)
  2863. goto out;
  2864. leaf = path->nodes[0];
  2865. }
  2866. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2867. if (found_key.objectid != extent_key->objectid) {
  2868. break;
  2869. }
  2870. if (found_key.type != BTRFS_EXTENT_REF_KEY) {
  2871. break;
  2872. }
  2873. key.offset = found_key.offset + 1;
  2874. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  2875. ret = relocate_one_reference(extent_root, path, extent_key,
  2876. &last_file_objectid,
  2877. &last_file_offset,
  2878. &last_file_root, last_extent);
  2879. if (ret)
  2880. goto out;
  2881. last_extent = extent_key->objectid;
  2882. }
  2883. ret = 0;
  2884. out:
  2885. btrfs_release_path(extent_root, path);
  2886. return ret;
  2887. }
  2888. static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
  2889. {
  2890. u64 num_devices;
  2891. u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
  2892. BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
  2893. num_devices = root->fs_info->fs_devices->num_devices;
  2894. if (num_devices == 1) {
  2895. stripped |= BTRFS_BLOCK_GROUP_DUP;
  2896. stripped = flags & ~stripped;
  2897. /* turn raid0 into single device chunks */
  2898. if (flags & BTRFS_BLOCK_GROUP_RAID0)
  2899. return stripped;
  2900. /* turn mirroring into duplication */
  2901. if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
  2902. BTRFS_BLOCK_GROUP_RAID10))
  2903. return stripped | BTRFS_BLOCK_GROUP_DUP;
  2904. return flags;
  2905. } else {
  2906. /* they already had raid on here, just return */
  2907. if (flags & stripped)
  2908. return flags;
  2909. stripped |= BTRFS_BLOCK_GROUP_DUP;
  2910. stripped = flags & ~stripped;
  2911. /* switch duplicated blocks with raid1 */
  2912. if (flags & BTRFS_BLOCK_GROUP_DUP)
  2913. return stripped | BTRFS_BLOCK_GROUP_RAID1;
  2914. /* turn single device chunks into raid0 */
  2915. return stripped | BTRFS_BLOCK_GROUP_RAID0;
  2916. }
  2917. return flags;
  2918. }
  2919. int __alloc_chunk_for_shrink(struct btrfs_root *root,
  2920. struct btrfs_block_group_cache *shrink_block_group,
  2921. int force)
  2922. {
  2923. struct btrfs_trans_handle *trans;
  2924. u64 new_alloc_flags;
  2925. u64 calc;
  2926. spin_lock(&shrink_block_group->lock);
  2927. if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
  2928. spin_unlock(&shrink_block_group->lock);
  2929. mutex_unlock(&root->fs_info->alloc_mutex);
  2930. trans = btrfs_start_transaction(root, 1);
  2931. mutex_lock(&root->fs_info->alloc_mutex);
  2932. spin_lock(&shrink_block_group->lock);
  2933. new_alloc_flags = update_block_group_flags(root,
  2934. shrink_block_group->flags);
  2935. if (new_alloc_flags != shrink_block_group->flags) {
  2936. calc =
  2937. btrfs_block_group_used(&shrink_block_group->item);
  2938. } else {
  2939. calc = shrink_block_group->key.offset;
  2940. }
  2941. spin_unlock(&shrink_block_group->lock);
  2942. do_chunk_alloc(trans, root->fs_info->extent_root,
  2943. calc + 2 * 1024 * 1024, new_alloc_flags, force);
  2944. mutex_unlock(&root->fs_info->alloc_mutex);
  2945. btrfs_end_transaction(trans, root);
  2946. mutex_lock(&root->fs_info->alloc_mutex);
  2947. } else
  2948. spin_unlock(&shrink_block_group->lock);
  2949. return 0;
  2950. }
  2951. int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 shrink_start)
  2952. {
  2953. struct btrfs_trans_handle *trans;
  2954. struct btrfs_root *tree_root = root->fs_info->tree_root;
  2955. struct btrfs_path *path;
  2956. u64 cur_byte;
  2957. u64 total_found;
  2958. u64 shrink_last_byte;
  2959. struct btrfs_block_group_cache *shrink_block_group;
  2960. struct btrfs_fs_info *info = root->fs_info;
  2961. struct btrfs_key key;
  2962. struct btrfs_key found_key;
  2963. struct extent_buffer *leaf;
  2964. u32 nritems;
  2965. int ret;
  2966. int progress;
  2967. mutex_lock(&root->fs_info->alloc_mutex);
  2968. shrink_block_group = btrfs_lookup_block_group(root->fs_info,
  2969. shrink_start);
  2970. BUG_ON(!shrink_block_group);
  2971. shrink_last_byte = shrink_block_group->key.objectid +
  2972. shrink_block_group->key.offset;
  2973. shrink_block_group->space_info->total_bytes -=
  2974. shrink_block_group->key.offset;
  2975. path = btrfs_alloc_path();
  2976. root = root->fs_info->extent_root;
  2977. path->reada = 2;
  2978. printk("btrfs relocating block group %llu flags %llu\n",
  2979. (unsigned long long)shrink_start,
  2980. (unsigned long long)shrink_block_group->flags);
  2981. __alloc_chunk_for_shrink(root, shrink_block_group, 1);
  2982. again:
  2983. shrink_block_group->ro = 1;
  2984. total_found = 0;
  2985. progress = 0;
  2986. key.objectid = shrink_start;
  2987. key.offset = 0;
  2988. key.type = 0;
  2989. cur_byte = key.objectid;
  2990. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2991. if (ret < 0)
  2992. goto out;
  2993. ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
  2994. if (ret < 0)
  2995. goto out;
  2996. if (ret == 0) {
  2997. leaf = path->nodes[0];
  2998. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  2999. if (found_key.objectid + found_key.offset > shrink_start &&
  3000. found_key.objectid < shrink_last_byte) {
  3001. cur_byte = found_key.objectid;
  3002. key.objectid = cur_byte;
  3003. }
  3004. }
  3005. btrfs_release_path(root, path);
  3006. while(1) {
  3007. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3008. if (ret < 0)
  3009. goto out;
  3010. next:
  3011. leaf = path->nodes[0];
  3012. nritems = btrfs_header_nritems(leaf);
  3013. if (path->slots[0] >= nritems) {
  3014. ret = btrfs_next_leaf(root, path);
  3015. if (ret < 0)
  3016. goto out;
  3017. if (ret == 1) {
  3018. ret = 0;
  3019. break;
  3020. }
  3021. leaf = path->nodes[0];
  3022. nritems = btrfs_header_nritems(leaf);
  3023. }
  3024. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3025. if (found_key.objectid >= shrink_last_byte)
  3026. break;
  3027. if (progress && need_resched()) {
  3028. memcpy(&key, &found_key, sizeof(key));
  3029. cond_resched();
  3030. btrfs_release_path(root, path);
  3031. btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3032. progress = 0;
  3033. goto next;
  3034. }
  3035. progress = 1;
  3036. if (btrfs_key_type(&found_key) != BTRFS_EXTENT_ITEM_KEY ||
  3037. found_key.objectid + found_key.offset <= cur_byte) {
  3038. memcpy(&key, &found_key, sizeof(key));
  3039. key.offset++;
  3040. path->slots[0]++;
  3041. goto next;
  3042. }
  3043. total_found++;
  3044. cur_byte = found_key.objectid + found_key.offset;
  3045. key.objectid = cur_byte;
  3046. btrfs_release_path(root, path);
  3047. ret = relocate_one_extent(root, path, &found_key);
  3048. __alloc_chunk_for_shrink(root, shrink_block_group, 0);
  3049. }
  3050. btrfs_release_path(root, path);
  3051. if (total_found > 0) {
  3052. printk("btrfs relocate found %llu last extent was %llu\n",
  3053. (unsigned long long)total_found,
  3054. (unsigned long long)found_key.objectid);
  3055. mutex_unlock(&root->fs_info->alloc_mutex);
  3056. trans = btrfs_start_transaction(tree_root, 1);
  3057. btrfs_commit_transaction(trans, tree_root);
  3058. btrfs_clean_old_snapshots(tree_root);
  3059. btrfs_wait_ordered_extents(tree_root);
  3060. trans = btrfs_start_transaction(tree_root, 1);
  3061. btrfs_commit_transaction(trans, tree_root);
  3062. mutex_lock(&root->fs_info->alloc_mutex);
  3063. goto again;
  3064. }
  3065. /*
  3066. * we've freed all the extents, now remove the block
  3067. * group item from the tree
  3068. */
  3069. mutex_unlock(&root->fs_info->alloc_mutex);
  3070. trans = btrfs_start_transaction(root, 1);
  3071. mutex_lock(&root->fs_info->alloc_mutex);
  3072. memcpy(&key, &shrink_block_group->key, sizeof(key));
  3073. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  3074. if (ret > 0)
  3075. ret = -EIO;
  3076. if (ret < 0) {
  3077. btrfs_end_transaction(trans, root);
  3078. goto out;
  3079. }
  3080. clear_extent_bits(&info->block_group_cache, key.objectid,
  3081. key.objectid + key.offset - 1,
  3082. (unsigned int)-1, GFP_NOFS);
  3083. clear_extent_bits(&info->free_space_cache,
  3084. key.objectid, key.objectid + key.offset - 1,
  3085. (unsigned int)-1, GFP_NOFS);
  3086. memset(shrink_block_group, 0, sizeof(*shrink_block_group));
  3087. kfree(shrink_block_group);
  3088. btrfs_del_item(trans, root, path);
  3089. btrfs_release_path(root, path);
  3090. mutex_unlock(&root->fs_info->alloc_mutex);
  3091. btrfs_commit_transaction(trans, root);
  3092. mutex_lock(&root->fs_info->alloc_mutex);
  3093. /* the code to unpin extents might set a few bits in the free
  3094. * space cache for this range again
  3095. */
  3096. clear_extent_bits(&info->free_space_cache,
  3097. key.objectid, key.objectid + key.offset - 1,
  3098. (unsigned int)-1, GFP_NOFS);
  3099. out:
  3100. btrfs_free_path(path);
  3101. mutex_unlock(&root->fs_info->alloc_mutex);
  3102. return ret;
  3103. }
  3104. int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
  3105. struct btrfs_key *key)
  3106. {
  3107. int ret = 0;
  3108. struct btrfs_key found_key;
  3109. struct extent_buffer *leaf;
  3110. int slot;
  3111. ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
  3112. if (ret < 0)
  3113. goto out;
  3114. while(1) {
  3115. slot = path->slots[0];
  3116. leaf = path->nodes[0];
  3117. if (slot >= btrfs_header_nritems(leaf)) {
  3118. ret = btrfs_next_leaf(root, path);
  3119. if (ret == 0)
  3120. continue;
  3121. if (ret < 0)
  3122. goto out;
  3123. break;
  3124. }
  3125. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  3126. if (found_key.objectid >= key->objectid &&
  3127. found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
  3128. ret = 0;
  3129. goto out;
  3130. }
  3131. path->slots[0]++;
  3132. }
  3133. ret = -ENOENT;
  3134. out:
  3135. return ret;
  3136. }
  3137. int btrfs_read_block_groups(struct btrfs_root *root)
  3138. {
  3139. struct btrfs_path *path;
  3140. int ret;
  3141. int bit;
  3142. struct btrfs_block_group_cache *cache;
  3143. struct btrfs_fs_info *info = root->fs_info;
  3144. struct btrfs_space_info *space_info;
  3145. struct extent_io_tree *block_group_cache;
  3146. struct btrfs_key key;
  3147. struct btrfs_key found_key;
  3148. struct extent_buffer *leaf;
  3149. block_group_cache = &info->block_group_cache;
  3150. root = info->extent_root;
  3151. key.objectid = 0;
  3152. key.offset = 0;
  3153. btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
  3154. path = btrfs_alloc_path();
  3155. if (!path)
  3156. return -ENOMEM;
  3157. mutex_lock(&root->fs_info->alloc_mutex);
  3158. while(1) {
  3159. ret = find_first_block_group(root, path, &key);
  3160. if (ret > 0) {
  3161. ret = 0;
  3162. goto error;
  3163. }
  3164. if (ret != 0)
  3165. goto error;
  3166. leaf = path->nodes[0];
  3167. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3168. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  3169. if (!cache) {
  3170. ret = -ENOMEM;
  3171. break;
  3172. }
  3173. spin_lock_init(&cache->lock);
  3174. read_extent_buffer(leaf, &cache->item,
  3175. btrfs_item_ptr_offset(leaf, path->slots[0]),
  3176. sizeof(cache->item));
  3177. memcpy(&cache->key, &found_key, sizeof(found_key));
  3178. key.objectid = found_key.objectid + found_key.offset;
  3179. btrfs_release_path(root, path);
  3180. cache->flags = btrfs_block_group_flags(&cache->item);
  3181. bit = 0;
  3182. if (cache->flags & BTRFS_BLOCK_GROUP_DATA) {
  3183. bit = BLOCK_GROUP_DATA;
  3184. } else if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
  3185. bit = BLOCK_GROUP_SYSTEM;
  3186. } else if (cache->flags & BTRFS_BLOCK_GROUP_METADATA) {
  3187. bit = BLOCK_GROUP_METADATA;
  3188. }
  3189. set_avail_alloc_bits(info, cache->flags);
  3190. ret = update_space_info(info, cache->flags, found_key.offset,
  3191. btrfs_block_group_used(&cache->item),
  3192. &space_info);
  3193. BUG_ON(ret);
  3194. cache->space_info = space_info;
  3195. /* use EXTENT_LOCKED to prevent merging */
  3196. set_extent_bits(block_group_cache, found_key.objectid,
  3197. found_key.objectid + found_key.offset - 1,
  3198. EXTENT_LOCKED, GFP_NOFS);
  3199. set_state_private(block_group_cache, found_key.objectid,
  3200. (unsigned long)cache);
  3201. set_extent_bits(block_group_cache, found_key.objectid,
  3202. found_key.objectid + found_key.offset - 1,
  3203. bit | EXTENT_LOCKED, GFP_NOFS);
  3204. if (key.objectid >=
  3205. btrfs_super_total_bytes(&info->super_copy))
  3206. break;
  3207. }
  3208. ret = 0;
  3209. error:
  3210. btrfs_free_path(path);
  3211. mutex_unlock(&root->fs_info->alloc_mutex);
  3212. return ret;
  3213. }
  3214. int btrfs_make_block_group(struct btrfs_trans_handle *trans,
  3215. struct btrfs_root *root, u64 bytes_used,
  3216. u64 type, u64 chunk_objectid, u64 chunk_offset,
  3217. u64 size)
  3218. {
  3219. int ret;
  3220. int bit = 0;
  3221. struct btrfs_root *extent_root;
  3222. struct btrfs_block_group_cache *cache;
  3223. struct extent_io_tree *block_group_cache;
  3224. WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
  3225. extent_root = root->fs_info->extent_root;
  3226. block_group_cache = &root->fs_info->block_group_cache;
  3227. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  3228. BUG_ON(!cache);
  3229. cache->key.objectid = chunk_offset;
  3230. cache->key.offset = size;
  3231. spin_lock_init(&cache->lock);
  3232. btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
  3233. btrfs_set_block_group_used(&cache->item, bytes_used);
  3234. btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
  3235. cache->flags = type;
  3236. btrfs_set_block_group_flags(&cache->item, type);
  3237. ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
  3238. &cache->space_info);
  3239. BUG_ON(ret);
  3240. bit = block_group_state_bits(type);
  3241. set_extent_bits(block_group_cache, chunk_offset,
  3242. chunk_offset + size - 1,
  3243. EXTENT_LOCKED, GFP_NOFS);
  3244. set_state_private(block_group_cache, chunk_offset,
  3245. (unsigned long)cache);
  3246. set_extent_bits(block_group_cache, chunk_offset,
  3247. chunk_offset + size - 1,
  3248. bit | EXTENT_LOCKED, GFP_NOFS);
  3249. ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
  3250. sizeof(cache->item));
  3251. BUG_ON(ret);
  3252. finish_current_insert(trans, extent_root);
  3253. ret = del_pending_extents(trans, extent_root);
  3254. BUG_ON(ret);
  3255. set_avail_alloc_bits(extent_root->fs_info, type);
  3256. return 0;
  3257. }