extent-tree.c 105 KB

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