extent-tree.c 134 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225
  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, int all);
  46. static int del_pending_extents(struct btrfs_trans_handle *trans, struct
  47. btrfs_root *extent_root, int all);
  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. static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
  53. {
  54. return (cache->flags & bits) == bits;
  55. }
  56. /*
  57. * this adds the block group to the fs_info rb tree for the block group
  58. * cache
  59. */
  60. int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
  61. struct btrfs_block_group_cache *block_group)
  62. {
  63. struct rb_node **p;
  64. struct rb_node *parent = NULL;
  65. struct btrfs_block_group_cache *cache;
  66. spin_lock(&info->block_group_cache_lock);
  67. p = &info->block_group_cache_tree.rb_node;
  68. while (*p) {
  69. parent = *p;
  70. cache = rb_entry(parent, struct btrfs_block_group_cache,
  71. cache_node);
  72. if (block_group->key.objectid < cache->key.objectid) {
  73. p = &(*p)->rb_left;
  74. } else if (block_group->key.objectid > cache->key.objectid) {
  75. p = &(*p)->rb_right;
  76. } else {
  77. spin_unlock(&info->block_group_cache_lock);
  78. return -EEXIST;
  79. }
  80. }
  81. rb_link_node(&block_group->cache_node, parent, p);
  82. rb_insert_color(&block_group->cache_node,
  83. &info->block_group_cache_tree);
  84. spin_unlock(&info->block_group_cache_lock);
  85. return 0;
  86. }
  87. /*
  88. * This will return the block group at or after bytenr if contains is 0, else
  89. * it will return the block group that contains the bytenr
  90. */
  91. static struct btrfs_block_group_cache *
  92. block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
  93. int contains)
  94. {
  95. struct btrfs_block_group_cache *cache, *ret = NULL;
  96. struct rb_node *n;
  97. u64 end, start;
  98. spin_lock(&info->block_group_cache_lock);
  99. n = info->block_group_cache_tree.rb_node;
  100. while (n) {
  101. cache = rb_entry(n, struct btrfs_block_group_cache,
  102. cache_node);
  103. end = cache->key.objectid + cache->key.offset - 1;
  104. start = cache->key.objectid;
  105. if (bytenr < start) {
  106. if (!contains && (!ret || start < ret->key.objectid))
  107. ret = cache;
  108. n = n->rb_left;
  109. } else if (bytenr > start) {
  110. if (contains && bytenr <= end) {
  111. ret = cache;
  112. break;
  113. }
  114. n = n->rb_right;
  115. } else {
  116. ret = cache;
  117. break;
  118. }
  119. }
  120. spin_unlock(&info->block_group_cache_lock);
  121. return ret;
  122. }
  123. /*
  124. * this is only called by cache_block_group, since we could have freed extents
  125. * we need to check the pinned_extents for any extents that can't be used yet
  126. * since their free space will be released as soon as the transaction commits.
  127. */
  128. static int add_new_free_space(struct btrfs_block_group_cache *block_group,
  129. struct btrfs_fs_info *info, u64 start, u64 end)
  130. {
  131. u64 extent_start, extent_end, size;
  132. int ret;
  133. mutex_lock(&info->pinned_mutex);
  134. while (start < end) {
  135. ret = find_first_extent_bit(&info->pinned_extents, start,
  136. &extent_start, &extent_end,
  137. EXTENT_DIRTY);
  138. if (ret)
  139. break;
  140. if (extent_start == start) {
  141. start = extent_end + 1;
  142. } else if (extent_start > start && extent_start < end) {
  143. size = extent_start - start;
  144. ret = btrfs_add_free_space_lock(block_group, start,
  145. size);
  146. BUG_ON(ret);
  147. start = extent_end + 1;
  148. } else {
  149. break;
  150. }
  151. }
  152. if (start < end) {
  153. size = end - start;
  154. ret = btrfs_add_free_space_lock(block_group, start, size);
  155. BUG_ON(ret);
  156. }
  157. mutex_unlock(&info->pinned_mutex);
  158. return 0;
  159. }
  160. static int cache_block_group(struct btrfs_root *root,
  161. struct btrfs_block_group_cache *block_group)
  162. {
  163. struct btrfs_path *path;
  164. int ret = 0;
  165. struct btrfs_key key;
  166. struct extent_buffer *leaf;
  167. int slot;
  168. u64 last = 0;
  169. u64 first_free;
  170. int found = 0;
  171. if (!block_group)
  172. return 0;
  173. root = root->fs_info->extent_root;
  174. if (block_group->cached)
  175. return 0;
  176. path = btrfs_alloc_path();
  177. if (!path)
  178. return -ENOMEM;
  179. path->reada = 2;
  180. /*
  181. * we get into deadlocks with paths held by callers of this function.
  182. * since the alloc_mutex is protecting things right now, just
  183. * skip the locking here
  184. */
  185. path->skip_locking = 1;
  186. first_free = max_t(u64, block_group->key.objectid,
  187. BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
  188. key.objectid = block_group->key.objectid;
  189. key.offset = 0;
  190. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  191. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  192. if (ret < 0)
  193. goto err;
  194. ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
  195. if (ret < 0)
  196. goto err;
  197. if (ret == 0) {
  198. leaf = path->nodes[0];
  199. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  200. if (key.objectid + key.offset > first_free)
  201. first_free = key.objectid + key.offset;
  202. }
  203. while(1) {
  204. leaf = path->nodes[0];
  205. slot = path->slots[0];
  206. if (slot >= btrfs_header_nritems(leaf)) {
  207. ret = btrfs_next_leaf(root, path);
  208. if (ret < 0)
  209. goto err;
  210. if (ret == 0)
  211. continue;
  212. else
  213. break;
  214. }
  215. btrfs_item_key_to_cpu(leaf, &key, slot);
  216. if (key.objectid < block_group->key.objectid)
  217. goto next;
  218. if (key.objectid >= block_group->key.objectid +
  219. block_group->key.offset)
  220. break;
  221. if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
  222. if (!found) {
  223. last = first_free;
  224. found = 1;
  225. }
  226. add_new_free_space(block_group, root->fs_info, last,
  227. key.objectid);
  228. last = key.objectid + key.offset;
  229. }
  230. next:
  231. path->slots[0]++;
  232. }
  233. if (!found)
  234. last = first_free;
  235. add_new_free_space(block_group, root->fs_info, last,
  236. block_group->key.objectid +
  237. block_group->key.offset);
  238. block_group->cached = 1;
  239. ret = 0;
  240. err:
  241. btrfs_free_path(path);
  242. return ret;
  243. }
  244. /*
  245. * return the block group that starts at or after bytenr
  246. */
  247. struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct
  248. btrfs_fs_info *info,
  249. u64 bytenr)
  250. {
  251. struct btrfs_block_group_cache *cache;
  252. cache = block_group_cache_tree_search(info, bytenr, 0);
  253. return cache;
  254. }
  255. /*
  256. * return the block group that contains teh given bytenr
  257. */
  258. struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
  259. btrfs_fs_info *info,
  260. u64 bytenr)
  261. {
  262. struct btrfs_block_group_cache *cache;
  263. cache = block_group_cache_tree_search(info, bytenr, 1);
  264. return cache;
  265. }
  266. static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
  267. u64 flags)
  268. {
  269. struct list_head *head = &info->space_info;
  270. struct list_head *cur;
  271. struct btrfs_space_info *found;
  272. list_for_each(cur, head) {
  273. found = list_entry(cur, struct btrfs_space_info, list);
  274. if (found->flags == flags)
  275. return found;
  276. }
  277. return NULL;
  278. }
  279. static u64 div_factor(u64 num, int factor)
  280. {
  281. if (factor == 10)
  282. return num;
  283. num *= factor;
  284. do_div(num, 10);
  285. return num;
  286. }
  287. static struct btrfs_block_group_cache *
  288. __btrfs_find_block_group(struct btrfs_root *root,
  289. struct btrfs_block_group_cache *hint,
  290. u64 search_start, int data, int owner)
  291. {
  292. struct btrfs_block_group_cache *cache;
  293. struct btrfs_block_group_cache *found_group = NULL;
  294. struct btrfs_fs_info *info = root->fs_info;
  295. u64 used;
  296. u64 last = 0;
  297. u64 free_check;
  298. int full_search = 0;
  299. int factor = 10;
  300. int wrapped = 0;
  301. if (data & BTRFS_BLOCK_GROUP_METADATA)
  302. factor = 9;
  303. if (search_start) {
  304. struct btrfs_block_group_cache *shint;
  305. shint = btrfs_lookup_first_block_group(info, search_start);
  306. if (shint && block_group_bits(shint, data) && !shint->ro) {
  307. spin_lock(&shint->lock);
  308. used = btrfs_block_group_used(&shint->item);
  309. if (used + shint->pinned + shint->reserved <
  310. div_factor(shint->key.offset, factor)) {
  311. spin_unlock(&shint->lock);
  312. return shint;
  313. }
  314. spin_unlock(&shint->lock);
  315. }
  316. }
  317. if (hint && !hint->ro && block_group_bits(hint, data)) {
  318. spin_lock(&hint->lock);
  319. used = btrfs_block_group_used(&hint->item);
  320. if (used + hint->pinned + hint->reserved <
  321. div_factor(hint->key.offset, factor)) {
  322. spin_unlock(&hint->lock);
  323. return hint;
  324. }
  325. spin_unlock(&hint->lock);
  326. last = hint->key.objectid + hint->key.offset;
  327. } else {
  328. if (hint)
  329. last = max(hint->key.objectid, search_start);
  330. else
  331. last = search_start;
  332. }
  333. again:
  334. while (1) {
  335. cache = btrfs_lookup_first_block_group(root->fs_info, last);
  336. if (!cache)
  337. break;
  338. spin_lock(&cache->lock);
  339. last = cache->key.objectid + cache->key.offset;
  340. used = btrfs_block_group_used(&cache->item);
  341. if (!cache->ro && block_group_bits(cache, data)) {
  342. free_check = div_factor(cache->key.offset, factor);
  343. if (used + cache->pinned + cache->reserved <
  344. free_check) {
  345. found_group = cache;
  346. spin_unlock(&cache->lock);
  347. goto found;
  348. }
  349. }
  350. spin_unlock(&cache->lock);
  351. cond_resched();
  352. }
  353. if (!wrapped) {
  354. last = search_start;
  355. wrapped = 1;
  356. goto again;
  357. }
  358. if (!full_search && factor < 10) {
  359. last = search_start;
  360. full_search = 1;
  361. factor = 10;
  362. goto again;
  363. }
  364. found:
  365. return found_group;
  366. }
  367. struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
  368. struct btrfs_block_group_cache
  369. *hint, u64 search_start,
  370. int data, int owner)
  371. {
  372. struct btrfs_block_group_cache *ret;
  373. ret = __btrfs_find_block_group(root, hint, search_start, data, owner);
  374. return ret;
  375. }
  376. /* simple helper to search for an existing extent at a given offset */
  377. int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
  378. {
  379. int ret;
  380. struct btrfs_key key;
  381. struct btrfs_path *path;
  382. path = btrfs_alloc_path();
  383. BUG_ON(!path);
  384. key.objectid = start;
  385. key.offset = len;
  386. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  387. ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
  388. 0, 0);
  389. btrfs_free_path(path);
  390. return ret;
  391. }
  392. /*
  393. * Back reference rules. Back refs have three main goals:
  394. *
  395. * 1) differentiate between all holders of references to an extent so that
  396. * when a reference is dropped we can make sure it was a valid reference
  397. * before freeing the extent.
  398. *
  399. * 2) Provide enough information to quickly find the holders of an extent
  400. * if we notice a given block is corrupted or bad.
  401. *
  402. * 3) Make it easy to migrate blocks for FS shrinking or storage pool
  403. * maintenance. This is actually the same as #2, but with a slightly
  404. * different use case.
  405. *
  406. * File extents can be referenced by:
  407. *
  408. * - multiple snapshots, subvolumes, or different generations in one subvol
  409. * - different files inside a single subvolume
  410. * - different offsets inside a file (bookend extents in file.c)
  411. *
  412. * The extent ref structure has fields for:
  413. *
  414. * - Objectid of the subvolume root
  415. * - Generation number of the tree holding the reference
  416. * - objectid of the file holding the reference
  417. * - number of references holding by parent node (alway 1 for tree blocks)
  418. *
  419. * Btree leaf may hold multiple references to a file extent. In most cases,
  420. * these references are from same file and the corresponding offsets inside
  421. * the file are close together.
  422. *
  423. * When a file extent is allocated the fields are filled in:
  424. * (root_key.objectid, trans->transid, inode objectid, 1)
  425. *
  426. * When a leaf is cow'd new references are added for every file extent found
  427. * in the leaf. It looks similar to the create case, but trans->transid will
  428. * be different when the block is cow'd.
  429. *
  430. * (root_key.objectid, trans->transid, inode objectid,
  431. * number of references in the leaf)
  432. *
  433. * When a file extent is removed either during snapshot deletion or
  434. * file truncation, we find the corresponding back reference and check
  435. * the following fields:
  436. *
  437. * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
  438. * inode objectid)
  439. *
  440. * Btree extents can be referenced by:
  441. *
  442. * - Different subvolumes
  443. * - Different generations of the same subvolume
  444. *
  445. * When a tree block is created, back references are inserted:
  446. *
  447. * (root->root_key.objectid, trans->transid, level, 1)
  448. *
  449. * When a tree block is cow'd, new back references are added for all the
  450. * blocks it points to. If the tree block isn't in reference counted root,
  451. * the old back references are removed. These new back references are of
  452. * the form (trans->transid will have increased since creation):
  453. *
  454. * (root->root_key.objectid, trans->transid, level, 1)
  455. *
  456. * When a backref is in deleting, the following fields are checked:
  457. *
  458. * if backref was for a tree root:
  459. * (btrfs_header_owner(itself), btrfs_header_generation(itself), level)
  460. * else
  461. * (btrfs_header_owner(parent), btrfs_header_generation(parent), level)
  462. *
  463. * Back Reference Key composing:
  464. *
  465. * The key objectid corresponds to the first byte in the extent, the key
  466. * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first
  467. * byte of parent extent. If a extent is tree root, the key offset is set
  468. * to the key objectid.
  469. */
  470. static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
  471. struct btrfs_root *root,
  472. struct btrfs_path *path,
  473. u64 bytenr, u64 parent,
  474. u64 ref_root, u64 ref_generation,
  475. u64 owner_objectid, int del)
  476. {
  477. struct btrfs_key key;
  478. struct btrfs_extent_ref *ref;
  479. struct extent_buffer *leaf;
  480. u64 ref_objectid;
  481. int ret;
  482. key.objectid = bytenr;
  483. key.type = BTRFS_EXTENT_REF_KEY;
  484. key.offset = parent;
  485. ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1);
  486. if (ret < 0)
  487. goto out;
  488. if (ret > 0) {
  489. ret = -ENOENT;
  490. goto out;
  491. }
  492. leaf = path->nodes[0];
  493. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
  494. ref_objectid = btrfs_ref_objectid(leaf, ref);
  495. if (btrfs_ref_root(leaf, ref) != ref_root ||
  496. btrfs_ref_generation(leaf, ref) != ref_generation ||
  497. (ref_objectid != owner_objectid &&
  498. ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
  499. ret = -EIO;
  500. WARN_ON(1);
  501. goto out;
  502. }
  503. ret = 0;
  504. out:
  505. return ret;
  506. }
  507. static int noinline insert_extent_backref(struct btrfs_trans_handle *trans,
  508. struct btrfs_root *root,
  509. struct btrfs_path *path,
  510. u64 bytenr, u64 parent,
  511. u64 ref_root, u64 ref_generation,
  512. u64 owner_objectid)
  513. {
  514. struct btrfs_key key;
  515. struct extent_buffer *leaf;
  516. struct btrfs_extent_ref *ref;
  517. u32 num_refs;
  518. int ret;
  519. key.objectid = bytenr;
  520. key.type = BTRFS_EXTENT_REF_KEY;
  521. key.offset = parent;
  522. ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref));
  523. if (ret == 0) {
  524. leaf = path->nodes[0];
  525. ref = btrfs_item_ptr(leaf, path->slots[0],
  526. struct btrfs_extent_ref);
  527. btrfs_set_ref_root(leaf, ref, ref_root);
  528. btrfs_set_ref_generation(leaf, ref, ref_generation);
  529. btrfs_set_ref_objectid(leaf, ref, owner_objectid);
  530. btrfs_set_ref_num_refs(leaf, ref, 1);
  531. } else if (ret == -EEXIST) {
  532. u64 existing_owner;
  533. BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID);
  534. leaf = path->nodes[0];
  535. ref = btrfs_item_ptr(leaf, path->slots[0],
  536. struct btrfs_extent_ref);
  537. if (btrfs_ref_root(leaf, ref) != ref_root ||
  538. btrfs_ref_generation(leaf, ref) != ref_generation) {
  539. ret = -EIO;
  540. WARN_ON(1);
  541. goto out;
  542. }
  543. num_refs = btrfs_ref_num_refs(leaf, ref);
  544. BUG_ON(num_refs == 0);
  545. btrfs_set_ref_num_refs(leaf, ref, num_refs + 1);
  546. existing_owner = btrfs_ref_objectid(leaf, ref);
  547. if (existing_owner != owner_objectid &&
  548. existing_owner != BTRFS_MULTIPLE_OBJECTIDS) {
  549. btrfs_set_ref_objectid(leaf, ref,
  550. BTRFS_MULTIPLE_OBJECTIDS);
  551. }
  552. ret = 0;
  553. } else {
  554. goto out;
  555. }
  556. btrfs_mark_buffer_dirty(path->nodes[0]);
  557. out:
  558. btrfs_release_path(root, path);
  559. return ret;
  560. }
  561. static int noinline remove_extent_backref(struct btrfs_trans_handle *trans,
  562. struct btrfs_root *root,
  563. struct btrfs_path *path)
  564. {
  565. struct extent_buffer *leaf;
  566. struct btrfs_extent_ref *ref;
  567. u32 num_refs;
  568. int ret = 0;
  569. leaf = path->nodes[0];
  570. ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
  571. num_refs = btrfs_ref_num_refs(leaf, ref);
  572. BUG_ON(num_refs == 0);
  573. num_refs -= 1;
  574. if (num_refs == 0) {
  575. ret = btrfs_del_item(trans, root, path);
  576. } else {
  577. btrfs_set_ref_num_refs(leaf, ref, num_refs);
  578. btrfs_mark_buffer_dirty(leaf);
  579. }
  580. btrfs_release_path(root, path);
  581. return ret;
  582. }
  583. static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
  584. struct btrfs_root *root, u64 bytenr,
  585. u64 orig_parent, u64 parent,
  586. u64 orig_root, u64 ref_root,
  587. u64 orig_generation, u64 ref_generation,
  588. u64 owner_objectid)
  589. {
  590. int ret;
  591. struct btrfs_root *extent_root = root->fs_info->extent_root;
  592. struct btrfs_path *path;
  593. if (root == root->fs_info->extent_root) {
  594. struct pending_extent_op *extent_op;
  595. u64 num_bytes;
  596. BUG_ON(owner_objectid >= BTRFS_MAX_LEVEL);
  597. num_bytes = btrfs_level_size(root, (int)owner_objectid);
  598. mutex_lock(&root->fs_info->extent_ins_mutex);
  599. if (test_range_bit(&root->fs_info->extent_ins, bytenr,
  600. bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
  601. u64 priv;
  602. ret = get_state_private(&root->fs_info->extent_ins,
  603. bytenr, &priv);
  604. BUG_ON(ret);
  605. extent_op = (struct pending_extent_op *)
  606. (unsigned long)priv;
  607. BUG_ON(extent_op->parent != orig_parent);
  608. BUG_ON(extent_op->generation != orig_generation);
  609. extent_op->parent = parent;
  610. extent_op->generation = ref_generation;
  611. } else {
  612. extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
  613. BUG_ON(!extent_op);
  614. extent_op->type = PENDING_BACKREF_UPDATE;
  615. extent_op->bytenr = bytenr;
  616. extent_op->num_bytes = num_bytes;
  617. extent_op->parent = parent;
  618. extent_op->orig_parent = orig_parent;
  619. extent_op->generation = ref_generation;
  620. extent_op->orig_generation = orig_generation;
  621. extent_op->level = (int)owner_objectid;
  622. set_extent_bits(&root->fs_info->extent_ins,
  623. bytenr, bytenr + num_bytes - 1,
  624. EXTENT_WRITEBACK, GFP_NOFS);
  625. set_state_private(&root->fs_info->extent_ins,
  626. bytenr, (unsigned long)extent_op);
  627. }
  628. mutex_unlock(&root->fs_info->extent_ins_mutex);
  629. return 0;
  630. }
  631. path = btrfs_alloc_path();
  632. if (!path)
  633. return -ENOMEM;
  634. ret = lookup_extent_backref(trans, extent_root, path,
  635. bytenr, orig_parent, orig_root,
  636. orig_generation, owner_objectid, 1);
  637. if (ret)
  638. goto out;
  639. ret = remove_extent_backref(trans, extent_root, path);
  640. if (ret)
  641. goto out;
  642. ret = insert_extent_backref(trans, extent_root, path, bytenr,
  643. parent, ref_root, ref_generation,
  644. owner_objectid);
  645. BUG_ON(ret);
  646. finish_current_insert(trans, extent_root, 0);
  647. del_pending_extents(trans, extent_root, 0);
  648. out:
  649. btrfs_free_path(path);
  650. return ret;
  651. }
  652. 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 ref_root, u64 ref_generation,
  656. u64 owner_objectid)
  657. {
  658. int ret;
  659. if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
  660. owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
  661. return 0;
  662. ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent,
  663. parent, ref_root, ref_root,
  664. ref_generation, ref_generation,
  665. owner_objectid);
  666. return ret;
  667. }
  668. static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
  669. struct btrfs_root *root, u64 bytenr,
  670. u64 orig_parent, u64 parent,
  671. u64 orig_root, u64 ref_root,
  672. u64 orig_generation, u64 ref_generation,
  673. u64 owner_objectid)
  674. {
  675. struct btrfs_path *path;
  676. int ret;
  677. struct btrfs_key key;
  678. struct extent_buffer *l;
  679. struct btrfs_extent_item *item;
  680. u32 refs;
  681. path = btrfs_alloc_path();
  682. if (!path)
  683. return -ENOMEM;
  684. path->reada = 1;
  685. key.objectid = bytenr;
  686. key.type = BTRFS_EXTENT_ITEM_KEY;
  687. key.offset = (u64)-1;
  688. ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
  689. 0, 1);
  690. if (ret < 0)
  691. return ret;
  692. BUG_ON(ret == 0 || path->slots[0] == 0);
  693. path->slots[0]--;
  694. l = path->nodes[0];
  695. btrfs_item_key_to_cpu(l, &key, path->slots[0]);
  696. if (key.objectid != bytenr) {
  697. btrfs_print_leaf(root->fs_info->extent_root, path->nodes[0]);
  698. printk("wanted %Lu found %Lu\n", bytenr, key.objectid);
  699. BUG();
  700. }
  701. BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
  702. item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
  703. refs = btrfs_extent_refs(l, item);
  704. btrfs_set_extent_refs(l, item, refs + 1);
  705. btrfs_mark_buffer_dirty(path->nodes[0]);
  706. btrfs_release_path(root->fs_info->extent_root, path);
  707. path->reada = 1;
  708. ret = insert_extent_backref(trans, root->fs_info->extent_root,
  709. path, bytenr, parent,
  710. ref_root, ref_generation,
  711. owner_objectid);
  712. BUG_ON(ret);
  713. finish_current_insert(trans, root->fs_info->extent_root, 0);
  714. del_pending_extents(trans, root->fs_info->extent_root, 0);
  715. btrfs_free_path(path);
  716. return 0;
  717. }
  718. int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
  719. struct btrfs_root *root,
  720. u64 bytenr, u64 num_bytes, u64 parent,
  721. u64 ref_root, u64 ref_generation,
  722. u64 owner_objectid)
  723. {
  724. int ret;
  725. if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
  726. owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
  727. return 0;
  728. ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent,
  729. 0, ref_root, 0, ref_generation,
  730. owner_objectid);
  731. return ret;
  732. }
  733. int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
  734. struct btrfs_root *root)
  735. {
  736. finish_current_insert(trans, root->fs_info->extent_root, 1);
  737. del_pending_extents(trans, root->fs_info->extent_root, 1);
  738. return 0;
  739. }
  740. int btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans,
  741. struct btrfs_root *root, u64 bytenr,
  742. u64 num_bytes, u32 *refs)
  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. WARN_ON(num_bytes < root->sectorsize);
  750. path = btrfs_alloc_path();
  751. path->reada = 1;
  752. key.objectid = bytenr;
  753. key.offset = num_bytes;
  754. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  755. ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
  756. 0, 0);
  757. if (ret < 0)
  758. goto out;
  759. if (ret != 0) {
  760. btrfs_print_leaf(root, path->nodes[0]);
  761. printk("failed to find block number %Lu\n", bytenr);
  762. BUG();
  763. }
  764. l = path->nodes[0];
  765. item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
  766. *refs = btrfs_extent_refs(l, item);
  767. out:
  768. btrfs_free_path(path);
  769. return 0;
  770. }
  771. int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
  772. struct btrfs_root *root, u64 bytenr)
  773. {
  774. struct btrfs_root *extent_root = root->fs_info->extent_root;
  775. struct btrfs_path *path;
  776. struct extent_buffer *leaf;
  777. struct btrfs_extent_ref *ref_item;
  778. struct btrfs_key key;
  779. struct btrfs_key found_key;
  780. u64 ref_root;
  781. u64 last_snapshot;
  782. u32 nritems;
  783. int ret;
  784. key.objectid = bytenr;
  785. key.offset = (u64)-1;
  786. key.type = BTRFS_EXTENT_ITEM_KEY;
  787. path = btrfs_alloc_path();
  788. ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
  789. if (ret < 0)
  790. goto out;
  791. BUG_ON(ret == 0);
  792. ret = -ENOENT;
  793. if (path->slots[0] == 0)
  794. goto out;
  795. path->slots[0]--;
  796. leaf = path->nodes[0];
  797. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  798. if (found_key.objectid != bytenr ||
  799. found_key.type != BTRFS_EXTENT_ITEM_KEY)
  800. goto out;
  801. last_snapshot = btrfs_root_last_snapshot(&root->root_item);
  802. while (1) {
  803. leaf = path->nodes[0];
  804. nritems = btrfs_header_nritems(leaf);
  805. if (path->slots[0] >= nritems) {
  806. ret = btrfs_next_leaf(extent_root, path);
  807. if (ret < 0)
  808. goto out;
  809. if (ret == 0)
  810. continue;
  811. break;
  812. }
  813. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  814. if (found_key.objectid != bytenr)
  815. break;
  816. if (found_key.type != BTRFS_EXTENT_REF_KEY) {
  817. path->slots[0]++;
  818. continue;
  819. }
  820. ref_item = btrfs_item_ptr(leaf, path->slots[0],
  821. struct btrfs_extent_ref);
  822. ref_root = btrfs_ref_root(leaf, ref_item);
  823. if (ref_root != root->root_key.objectid &&
  824. ref_root != BTRFS_TREE_LOG_OBJECTID) {
  825. ret = 1;
  826. goto out;
  827. }
  828. if (btrfs_ref_generation(leaf, ref_item) <= last_snapshot) {
  829. ret = 1;
  830. goto out;
  831. }
  832. path->slots[0]++;
  833. }
  834. ret = 0;
  835. out:
  836. btrfs_free_path(path);
  837. return ret;
  838. }
  839. int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  840. struct extent_buffer *buf, u32 nr_extents)
  841. {
  842. struct btrfs_key key;
  843. struct btrfs_file_extent_item *fi;
  844. u64 root_gen;
  845. u32 nritems;
  846. int i;
  847. int level;
  848. int ret = 0;
  849. int shared = 0;
  850. if (!root->ref_cows)
  851. return 0;
  852. if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
  853. shared = 0;
  854. root_gen = root->root_key.offset;
  855. } else {
  856. shared = 1;
  857. root_gen = trans->transid - 1;
  858. }
  859. level = btrfs_header_level(buf);
  860. nritems = btrfs_header_nritems(buf);
  861. if (level == 0) {
  862. struct btrfs_leaf_ref *ref;
  863. struct btrfs_extent_info *info;
  864. ref = btrfs_alloc_leaf_ref(root, nr_extents);
  865. if (!ref) {
  866. ret = -ENOMEM;
  867. goto out;
  868. }
  869. ref->root_gen = root_gen;
  870. ref->bytenr = buf->start;
  871. ref->owner = btrfs_header_owner(buf);
  872. ref->generation = btrfs_header_generation(buf);
  873. ref->nritems = nr_extents;
  874. info = ref->extents;
  875. for (i = 0; nr_extents > 0 && i < nritems; i++) {
  876. u64 disk_bytenr;
  877. btrfs_item_key_to_cpu(buf, &key, i);
  878. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  879. continue;
  880. fi = btrfs_item_ptr(buf, i,
  881. struct btrfs_file_extent_item);
  882. if (btrfs_file_extent_type(buf, fi) ==
  883. BTRFS_FILE_EXTENT_INLINE)
  884. continue;
  885. disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  886. if (disk_bytenr == 0)
  887. continue;
  888. info->bytenr = disk_bytenr;
  889. info->num_bytes =
  890. btrfs_file_extent_disk_num_bytes(buf, fi);
  891. info->objectid = key.objectid;
  892. info->offset = key.offset;
  893. info++;
  894. }
  895. ret = btrfs_add_leaf_ref(root, ref, shared);
  896. if (ret == -EEXIST && shared) {
  897. struct btrfs_leaf_ref *old;
  898. old = btrfs_lookup_leaf_ref(root, ref->bytenr);
  899. BUG_ON(!old);
  900. btrfs_remove_leaf_ref(root, old);
  901. btrfs_free_leaf_ref(root, old);
  902. ret = btrfs_add_leaf_ref(root, ref, shared);
  903. }
  904. WARN_ON(ret);
  905. btrfs_free_leaf_ref(root, ref);
  906. }
  907. out:
  908. return ret;
  909. }
  910. int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  911. struct extent_buffer *orig_buf, struct extent_buffer *buf,
  912. u32 *nr_extents)
  913. {
  914. u64 bytenr;
  915. u64 ref_root;
  916. u64 orig_root;
  917. u64 ref_generation;
  918. u64 orig_generation;
  919. u32 nritems;
  920. u32 nr_file_extents = 0;
  921. struct btrfs_key key;
  922. struct btrfs_file_extent_item *fi;
  923. int i;
  924. int level;
  925. int ret = 0;
  926. int faili = 0;
  927. int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
  928. u64, u64, u64, u64, u64, u64, u64, u64);
  929. ref_root = btrfs_header_owner(buf);
  930. ref_generation = btrfs_header_generation(buf);
  931. orig_root = btrfs_header_owner(orig_buf);
  932. orig_generation = btrfs_header_generation(orig_buf);
  933. nritems = btrfs_header_nritems(buf);
  934. level = btrfs_header_level(buf);
  935. if (root->ref_cows) {
  936. process_func = __btrfs_inc_extent_ref;
  937. } else {
  938. if (level == 0 &&
  939. root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
  940. goto out;
  941. if (level != 0 &&
  942. root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
  943. goto out;
  944. process_func = __btrfs_update_extent_ref;
  945. }
  946. for (i = 0; i < nritems; i++) {
  947. cond_resched();
  948. if (level == 0) {
  949. btrfs_item_key_to_cpu(buf, &key, i);
  950. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  951. continue;
  952. fi = btrfs_item_ptr(buf, i,
  953. struct btrfs_file_extent_item);
  954. if (btrfs_file_extent_type(buf, fi) ==
  955. BTRFS_FILE_EXTENT_INLINE)
  956. continue;
  957. bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  958. if (bytenr == 0)
  959. continue;
  960. nr_file_extents++;
  961. ret = process_func(trans, root, bytenr,
  962. orig_buf->start, buf->start,
  963. orig_root, ref_root,
  964. orig_generation, ref_generation,
  965. key.objectid);
  966. if (ret) {
  967. faili = i;
  968. WARN_ON(1);
  969. goto fail;
  970. }
  971. } else {
  972. bytenr = btrfs_node_blockptr(buf, i);
  973. ret = process_func(trans, root, bytenr,
  974. orig_buf->start, buf->start,
  975. orig_root, ref_root,
  976. orig_generation, ref_generation,
  977. level - 1);
  978. if (ret) {
  979. faili = i;
  980. WARN_ON(1);
  981. goto fail;
  982. }
  983. }
  984. }
  985. out:
  986. if (nr_extents) {
  987. if (level == 0)
  988. *nr_extents = nr_file_extents;
  989. else
  990. *nr_extents = nritems;
  991. }
  992. return 0;
  993. fail:
  994. WARN_ON(1);
  995. return ret;
  996. }
  997. int btrfs_update_ref(struct btrfs_trans_handle *trans,
  998. struct btrfs_root *root, struct extent_buffer *orig_buf,
  999. struct extent_buffer *buf, int start_slot, int nr)
  1000. {
  1001. u64 bytenr;
  1002. u64 ref_root;
  1003. u64 orig_root;
  1004. u64 ref_generation;
  1005. u64 orig_generation;
  1006. struct btrfs_key key;
  1007. struct btrfs_file_extent_item *fi;
  1008. int i;
  1009. int ret;
  1010. int slot;
  1011. int level;
  1012. BUG_ON(start_slot < 0);
  1013. BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
  1014. ref_root = btrfs_header_owner(buf);
  1015. ref_generation = btrfs_header_generation(buf);
  1016. orig_root = btrfs_header_owner(orig_buf);
  1017. orig_generation = btrfs_header_generation(orig_buf);
  1018. level = btrfs_header_level(buf);
  1019. if (!root->ref_cows) {
  1020. if (level == 0 &&
  1021. root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
  1022. return 0;
  1023. if (level != 0 &&
  1024. root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
  1025. return 0;
  1026. }
  1027. for (i = 0, slot = start_slot; i < nr; i++, slot++) {
  1028. cond_resched();
  1029. if (level == 0) {
  1030. btrfs_item_key_to_cpu(buf, &key, slot);
  1031. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  1032. continue;
  1033. fi = btrfs_item_ptr(buf, slot,
  1034. struct btrfs_file_extent_item);
  1035. if (btrfs_file_extent_type(buf, fi) ==
  1036. BTRFS_FILE_EXTENT_INLINE)
  1037. continue;
  1038. bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
  1039. if (bytenr == 0)
  1040. continue;
  1041. ret = __btrfs_update_extent_ref(trans, root, bytenr,
  1042. orig_buf->start, buf->start,
  1043. orig_root, ref_root,
  1044. orig_generation, ref_generation,
  1045. key.objectid);
  1046. if (ret)
  1047. goto fail;
  1048. } else {
  1049. bytenr = btrfs_node_blockptr(buf, slot);
  1050. ret = __btrfs_update_extent_ref(trans, root, bytenr,
  1051. orig_buf->start, buf->start,
  1052. orig_root, ref_root,
  1053. orig_generation, ref_generation,
  1054. level - 1);
  1055. if (ret)
  1056. goto fail;
  1057. }
  1058. }
  1059. return 0;
  1060. fail:
  1061. WARN_ON(1);
  1062. return -1;
  1063. }
  1064. static int write_one_cache_group(struct btrfs_trans_handle *trans,
  1065. struct btrfs_root *root,
  1066. struct btrfs_path *path,
  1067. struct btrfs_block_group_cache *cache)
  1068. {
  1069. int ret;
  1070. int pending_ret;
  1071. struct btrfs_root *extent_root = root->fs_info->extent_root;
  1072. unsigned long bi;
  1073. struct extent_buffer *leaf;
  1074. ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
  1075. if (ret < 0)
  1076. goto fail;
  1077. BUG_ON(ret);
  1078. leaf = path->nodes[0];
  1079. bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
  1080. write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
  1081. btrfs_mark_buffer_dirty(leaf);
  1082. btrfs_release_path(extent_root, path);
  1083. fail:
  1084. finish_current_insert(trans, extent_root, 0);
  1085. pending_ret = del_pending_extents(trans, extent_root, 0);
  1086. if (ret)
  1087. return ret;
  1088. if (pending_ret)
  1089. return pending_ret;
  1090. return 0;
  1091. }
  1092. int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
  1093. struct btrfs_root *root)
  1094. {
  1095. struct btrfs_block_group_cache *cache, *entry;
  1096. struct rb_node *n;
  1097. int err = 0;
  1098. int werr = 0;
  1099. struct btrfs_path *path;
  1100. u64 last = 0;
  1101. path = btrfs_alloc_path();
  1102. if (!path)
  1103. return -ENOMEM;
  1104. while(1) {
  1105. cache = NULL;
  1106. spin_lock(&root->fs_info->block_group_cache_lock);
  1107. for (n = rb_first(&root->fs_info->block_group_cache_tree);
  1108. n; n = rb_next(n)) {
  1109. entry = rb_entry(n, struct btrfs_block_group_cache,
  1110. cache_node);
  1111. if (entry->dirty) {
  1112. cache = entry;
  1113. break;
  1114. }
  1115. }
  1116. spin_unlock(&root->fs_info->block_group_cache_lock);
  1117. if (!cache)
  1118. break;
  1119. cache->dirty = 0;
  1120. last += cache->key.offset;
  1121. err = write_one_cache_group(trans, root,
  1122. path, cache);
  1123. /*
  1124. * if we fail to write the cache group, we want
  1125. * to keep it marked dirty in hopes that a later
  1126. * write will work
  1127. */
  1128. if (err) {
  1129. werr = err;
  1130. continue;
  1131. }
  1132. }
  1133. btrfs_free_path(path);
  1134. return werr;
  1135. }
  1136. static int update_space_info(struct btrfs_fs_info *info, u64 flags,
  1137. u64 total_bytes, u64 bytes_used,
  1138. struct btrfs_space_info **space_info)
  1139. {
  1140. struct btrfs_space_info *found;
  1141. found = __find_space_info(info, flags);
  1142. if (found) {
  1143. spin_lock(&found->lock);
  1144. found->total_bytes += total_bytes;
  1145. found->bytes_used += bytes_used;
  1146. found->full = 0;
  1147. spin_unlock(&found->lock);
  1148. *space_info = found;
  1149. return 0;
  1150. }
  1151. found = kmalloc(sizeof(*found), GFP_NOFS);
  1152. if (!found)
  1153. return -ENOMEM;
  1154. list_add(&found->list, &info->space_info);
  1155. INIT_LIST_HEAD(&found->block_groups);
  1156. init_rwsem(&found->groups_sem);
  1157. spin_lock_init(&found->lock);
  1158. found->flags = flags;
  1159. found->total_bytes = total_bytes;
  1160. found->bytes_used = bytes_used;
  1161. found->bytes_pinned = 0;
  1162. found->bytes_reserved = 0;
  1163. found->full = 0;
  1164. found->force_alloc = 0;
  1165. *space_info = found;
  1166. return 0;
  1167. }
  1168. static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
  1169. {
  1170. u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
  1171. BTRFS_BLOCK_GROUP_RAID1 |
  1172. BTRFS_BLOCK_GROUP_RAID10 |
  1173. BTRFS_BLOCK_GROUP_DUP);
  1174. if (extra_flags) {
  1175. if (flags & BTRFS_BLOCK_GROUP_DATA)
  1176. fs_info->avail_data_alloc_bits |= extra_flags;
  1177. if (flags & BTRFS_BLOCK_GROUP_METADATA)
  1178. fs_info->avail_metadata_alloc_bits |= extra_flags;
  1179. if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
  1180. fs_info->avail_system_alloc_bits |= extra_flags;
  1181. }
  1182. }
  1183. static u64 reduce_alloc_profile(struct btrfs_root *root, u64 flags)
  1184. {
  1185. u64 num_devices = root->fs_info->fs_devices->num_devices;
  1186. if (num_devices == 1)
  1187. flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
  1188. if (num_devices < 4)
  1189. flags &= ~BTRFS_BLOCK_GROUP_RAID10;
  1190. if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
  1191. (flags & (BTRFS_BLOCK_GROUP_RAID1 |
  1192. BTRFS_BLOCK_GROUP_RAID10))) {
  1193. flags &= ~BTRFS_BLOCK_GROUP_DUP;
  1194. }
  1195. if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
  1196. (flags & BTRFS_BLOCK_GROUP_RAID10)) {
  1197. flags &= ~BTRFS_BLOCK_GROUP_RAID1;
  1198. }
  1199. if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
  1200. ((flags & BTRFS_BLOCK_GROUP_RAID1) |
  1201. (flags & BTRFS_BLOCK_GROUP_RAID10) |
  1202. (flags & BTRFS_BLOCK_GROUP_DUP)))
  1203. flags &= ~BTRFS_BLOCK_GROUP_RAID0;
  1204. return flags;
  1205. }
  1206. static int do_chunk_alloc(struct btrfs_trans_handle *trans,
  1207. struct btrfs_root *extent_root, u64 alloc_bytes,
  1208. u64 flags, int force)
  1209. {
  1210. struct btrfs_space_info *space_info;
  1211. u64 thresh;
  1212. u64 start;
  1213. u64 num_bytes;
  1214. int ret = 0, waited = 0;
  1215. flags = reduce_alloc_profile(extent_root, flags);
  1216. space_info = __find_space_info(extent_root->fs_info, flags);
  1217. if (!space_info) {
  1218. ret = update_space_info(extent_root->fs_info, flags,
  1219. 0, 0, &space_info);
  1220. BUG_ON(ret);
  1221. }
  1222. BUG_ON(!space_info);
  1223. spin_lock(&space_info->lock);
  1224. if (space_info->force_alloc) {
  1225. force = 1;
  1226. space_info->force_alloc = 0;
  1227. }
  1228. if (space_info->full) {
  1229. spin_unlock(&space_info->lock);
  1230. goto out;
  1231. }
  1232. thresh = div_factor(space_info->total_bytes, 6);
  1233. if (!force &&
  1234. (space_info->bytes_used + space_info->bytes_pinned +
  1235. space_info->bytes_reserved + alloc_bytes) < thresh) {
  1236. spin_unlock(&space_info->lock);
  1237. goto out;
  1238. }
  1239. spin_unlock(&space_info->lock);
  1240. ret = mutex_trylock(&extent_root->fs_info->chunk_mutex);
  1241. if (!ret && !force) {
  1242. goto out;
  1243. } else if (!ret) {
  1244. mutex_lock(&extent_root->fs_info->chunk_mutex);
  1245. waited = 1;
  1246. }
  1247. if (waited) {
  1248. spin_lock(&space_info->lock);
  1249. if (space_info->full) {
  1250. spin_unlock(&space_info->lock);
  1251. goto out_unlock;
  1252. }
  1253. spin_unlock(&space_info->lock);
  1254. }
  1255. ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes, flags);
  1256. if (ret) {
  1257. printk("space info full %Lu\n", flags);
  1258. space_info->full = 1;
  1259. goto out_unlock;
  1260. }
  1261. ret = btrfs_make_block_group(trans, extent_root, 0, flags,
  1262. BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
  1263. BUG_ON(ret);
  1264. out_unlock:
  1265. mutex_unlock(&extent_root->fs_info->chunk_mutex);
  1266. out:
  1267. return ret;
  1268. }
  1269. static int update_block_group(struct btrfs_trans_handle *trans,
  1270. struct btrfs_root *root,
  1271. u64 bytenr, u64 num_bytes, int alloc,
  1272. int mark_free)
  1273. {
  1274. struct btrfs_block_group_cache *cache;
  1275. struct btrfs_fs_info *info = root->fs_info;
  1276. u64 total = num_bytes;
  1277. u64 old_val;
  1278. u64 byte_in_group;
  1279. while(total) {
  1280. cache = btrfs_lookup_block_group(info, bytenr);
  1281. if (!cache) {
  1282. return -1;
  1283. }
  1284. byte_in_group = bytenr - cache->key.objectid;
  1285. WARN_ON(byte_in_group > cache->key.offset);
  1286. spin_lock(&cache->space_info->lock);
  1287. spin_lock(&cache->lock);
  1288. cache->dirty = 1;
  1289. old_val = btrfs_block_group_used(&cache->item);
  1290. num_bytes = min(total, cache->key.offset - byte_in_group);
  1291. if (alloc) {
  1292. old_val += num_bytes;
  1293. cache->space_info->bytes_used += num_bytes;
  1294. btrfs_set_block_group_used(&cache->item, old_val);
  1295. spin_unlock(&cache->lock);
  1296. spin_unlock(&cache->space_info->lock);
  1297. } else {
  1298. old_val -= num_bytes;
  1299. cache->space_info->bytes_used -= num_bytes;
  1300. btrfs_set_block_group_used(&cache->item, old_val);
  1301. spin_unlock(&cache->lock);
  1302. spin_unlock(&cache->space_info->lock);
  1303. if (mark_free) {
  1304. int ret;
  1305. ret = btrfs_add_free_space(cache, bytenr,
  1306. num_bytes);
  1307. if (ret)
  1308. return -1;
  1309. }
  1310. }
  1311. total -= num_bytes;
  1312. bytenr += num_bytes;
  1313. }
  1314. return 0;
  1315. }
  1316. static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
  1317. {
  1318. struct btrfs_block_group_cache *cache;
  1319. cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
  1320. if (!cache)
  1321. return 0;
  1322. return cache->key.objectid;
  1323. }
  1324. int btrfs_update_pinned_extents(struct btrfs_root *root,
  1325. u64 bytenr, u64 num, int pin)
  1326. {
  1327. u64 len;
  1328. struct btrfs_block_group_cache *cache;
  1329. struct btrfs_fs_info *fs_info = root->fs_info;
  1330. WARN_ON(!mutex_is_locked(&root->fs_info->pinned_mutex));
  1331. if (pin) {
  1332. set_extent_dirty(&fs_info->pinned_extents,
  1333. bytenr, bytenr + num - 1, GFP_NOFS);
  1334. } else {
  1335. clear_extent_dirty(&fs_info->pinned_extents,
  1336. bytenr, bytenr + num - 1, GFP_NOFS);
  1337. }
  1338. while (num > 0) {
  1339. cache = btrfs_lookup_block_group(fs_info, bytenr);
  1340. BUG_ON(!cache);
  1341. len = min(num, cache->key.offset -
  1342. (bytenr - cache->key.objectid));
  1343. if (pin) {
  1344. spin_lock(&cache->space_info->lock);
  1345. spin_lock(&cache->lock);
  1346. cache->pinned += len;
  1347. cache->space_info->bytes_pinned += len;
  1348. spin_unlock(&cache->lock);
  1349. spin_unlock(&cache->space_info->lock);
  1350. fs_info->total_pinned += len;
  1351. } else {
  1352. spin_lock(&cache->space_info->lock);
  1353. spin_lock(&cache->lock);
  1354. cache->pinned -= len;
  1355. cache->space_info->bytes_pinned -= len;
  1356. spin_unlock(&cache->lock);
  1357. spin_unlock(&cache->space_info->lock);
  1358. fs_info->total_pinned -= len;
  1359. }
  1360. bytenr += len;
  1361. num -= len;
  1362. }
  1363. return 0;
  1364. }
  1365. static int update_reserved_extents(struct btrfs_root *root,
  1366. u64 bytenr, u64 num, int reserve)
  1367. {
  1368. u64 len;
  1369. struct btrfs_block_group_cache *cache;
  1370. struct btrfs_fs_info *fs_info = root->fs_info;
  1371. while (num > 0) {
  1372. cache = btrfs_lookup_block_group(fs_info, bytenr);
  1373. BUG_ON(!cache);
  1374. len = min(num, cache->key.offset -
  1375. (bytenr - cache->key.objectid));
  1376. spin_lock(&cache->space_info->lock);
  1377. spin_lock(&cache->lock);
  1378. if (reserve) {
  1379. cache->reserved += len;
  1380. cache->space_info->bytes_reserved += len;
  1381. } else {
  1382. cache->reserved -= len;
  1383. cache->space_info->bytes_reserved -= len;
  1384. }
  1385. spin_unlock(&cache->lock);
  1386. spin_unlock(&cache->space_info->lock);
  1387. bytenr += len;
  1388. num -= len;
  1389. }
  1390. return 0;
  1391. }
  1392. int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
  1393. {
  1394. u64 last = 0;
  1395. u64 start;
  1396. u64 end;
  1397. struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
  1398. int ret;
  1399. mutex_lock(&root->fs_info->pinned_mutex);
  1400. while(1) {
  1401. ret = find_first_extent_bit(pinned_extents, last,
  1402. &start, &end, EXTENT_DIRTY);
  1403. if (ret)
  1404. break;
  1405. set_extent_dirty(copy, start, end, GFP_NOFS);
  1406. last = end + 1;
  1407. }
  1408. mutex_unlock(&root->fs_info->pinned_mutex);
  1409. return 0;
  1410. }
  1411. int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
  1412. struct btrfs_root *root,
  1413. struct extent_io_tree *unpin)
  1414. {
  1415. u64 start;
  1416. u64 end;
  1417. int ret;
  1418. struct btrfs_block_group_cache *cache;
  1419. mutex_lock(&root->fs_info->pinned_mutex);
  1420. while(1) {
  1421. ret = find_first_extent_bit(unpin, 0, &start, &end,
  1422. EXTENT_DIRTY);
  1423. if (ret)
  1424. break;
  1425. btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
  1426. clear_extent_dirty(unpin, start, end, GFP_NOFS);
  1427. cache = btrfs_lookup_block_group(root->fs_info, start);
  1428. if (cache->cached)
  1429. btrfs_add_free_space(cache, start, end - start + 1);
  1430. if (need_resched()) {
  1431. mutex_unlock(&root->fs_info->pinned_mutex);
  1432. cond_resched();
  1433. mutex_lock(&root->fs_info->pinned_mutex);
  1434. }
  1435. }
  1436. mutex_unlock(&root->fs_info->pinned_mutex);
  1437. return 0;
  1438. }
  1439. static int finish_current_insert(struct btrfs_trans_handle *trans,
  1440. struct btrfs_root *extent_root, int all)
  1441. {
  1442. u64 start;
  1443. u64 end;
  1444. u64 priv;
  1445. u64 search = 0;
  1446. struct btrfs_fs_info *info = extent_root->fs_info;
  1447. struct btrfs_path *path;
  1448. struct btrfs_extent_ref *ref;
  1449. struct pending_extent_op *extent_op;
  1450. struct btrfs_key key;
  1451. struct btrfs_extent_item extent_item;
  1452. int ret;
  1453. int err = 0;
  1454. btrfs_set_stack_extent_refs(&extent_item, 1);
  1455. path = btrfs_alloc_path();
  1456. while(1) {
  1457. mutex_lock(&info->extent_ins_mutex);
  1458. ret = find_first_extent_bit(&info->extent_ins, search, &start,
  1459. &end, EXTENT_WRITEBACK);
  1460. if (ret) {
  1461. mutex_unlock(&info->extent_ins_mutex);
  1462. if (search && all) {
  1463. search = 0;
  1464. continue;
  1465. }
  1466. break;
  1467. }
  1468. ret = try_lock_extent(&info->extent_ins, start, end, GFP_NOFS);
  1469. if (!ret) {
  1470. search = end + 1;
  1471. mutex_unlock(&info->extent_ins_mutex);
  1472. cond_resched();
  1473. continue;
  1474. }
  1475. BUG_ON(ret < 0);
  1476. ret = get_state_private(&info->extent_ins, start, &priv);
  1477. BUG_ON(ret);
  1478. extent_op = (struct pending_extent_op *)(unsigned long)priv;
  1479. mutex_unlock(&info->extent_ins_mutex);
  1480. if (extent_op->type == PENDING_EXTENT_INSERT) {
  1481. key.objectid = start;
  1482. key.offset = end + 1 - start;
  1483. key.type = BTRFS_EXTENT_ITEM_KEY;
  1484. err = btrfs_insert_item(trans, extent_root, &key,
  1485. &extent_item, sizeof(extent_item));
  1486. BUG_ON(err);
  1487. mutex_lock(&info->extent_ins_mutex);
  1488. clear_extent_bits(&info->extent_ins, start, end,
  1489. EXTENT_WRITEBACK, GFP_NOFS);
  1490. mutex_unlock(&info->extent_ins_mutex);
  1491. err = insert_extent_backref(trans, extent_root, path,
  1492. start, extent_op->parent,
  1493. extent_root->root_key.objectid,
  1494. extent_op->generation,
  1495. extent_op->level);
  1496. BUG_ON(err);
  1497. } else if (extent_op->type == PENDING_BACKREF_UPDATE) {
  1498. err = lookup_extent_backref(trans, extent_root, path,
  1499. start, extent_op->orig_parent,
  1500. extent_root->root_key.objectid,
  1501. extent_op->orig_generation,
  1502. extent_op->level, 0);
  1503. BUG_ON(err);
  1504. mutex_lock(&info->extent_ins_mutex);
  1505. clear_extent_bits(&info->extent_ins, start, end,
  1506. EXTENT_WRITEBACK, GFP_NOFS);
  1507. mutex_unlock(&info->extent_ins_mutex);
  1508. key.objectid = start;
  1509. key.offset = extent_op->parent;
  1510. key.type = BTRFS_EXTENT_REF_KEY;
  1511. err = btrfs_set_item_key_safe(trans, extent_root, path,
  1512. &key);
  1513. BUG_ON(err);
  1514. ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
  1515. struct btrfs_extent_ref);
  1516. btrfs_set_ref_generation(path->nodes[0], ref,
  1517. extent_op->generation);
  1518. btrfs_mark_buffer_dirty(path->nodes[0]);
  1519. btrfs_release_path(extent_root, path);
  1520. } else {
  1521. BUG_ON(1);
  1522. }
  1523. kfree(extent_op);
  1524. unlock_extent(&info->extent_ins, start, end, GFP_NOFS);
  1525. if (all)
  1526. search = 0;
  1527. else
  1528. search = end + 1;
  1529. cond_resched();
  1530. }
  1531. btrfs_free_path(path);
  1532. return 0;
  1533. }
  1534. static int pin_down_bytes(struct btrfs_trans_handle *trans,
  1535. struct btrfs_root *root,
  1536. u64 bytenr, u64 num_bytes, int is_data)
  1537. {
  1538. int err = 0;
  1539. struct extent_buffer *buf;
  1540. if (is_data)
  1541. goto pinit;
  1542. buf = btrfs_find_tree_block(root, bytenr, num_bytes);
  1543. if (!buf)
  1544. goto pinit;
  1545. /* we can reuse a block if it hasn't been written
  1546. * and it is from this transaction. We can't
  1547. * reuse anything from the tree log root because
  1548. * it has tiny sub-transactions.
  1549. */
  1550. if (btrfs_buffer_uptodate(buf, 0) &&
  1551. btrfs_try_tree_lock(buf)) {
  1552. u64 header_owner = btrfs_header_owner(buf);
  1553. u64 header_transid = btrfs_header_generation(buf);
  1554. if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
  1555. header_owner != BTRFS_TREE_RELOC_OBJECTID &&
  1556. header_transid == trans->transid &&
  1557. !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
  1558. clean_tree_block(NULL, root, buf);
  1559. btrfs_tree_unlock(buf);
  1560. free_extent_buffer(buf);
  1561. return 1;
  1562. }
  1563. btrfs_tree_unlock(buf);
  1564. }
  1565. free_extent_buffer(buf);
  1566. pinit:
  1567. btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
  1568. BUG_ON(err < 0);
  1569. return 0;
  1570. }
  1571. /*
  1572. * remove an extent from the root, returns 0 on success
  1573. */
  1574. static int __free_extent(struct btrfs_trans_handle *trans,
  1575. struct btrfs_root *root,
  1576. u64 bytenr, u64 num_bytes, u64 parent,
  1577. u64 root_objectid, u64 ref_generation,
  1578. u64 owner_objectid, int pin, int mark_free)
  1579. {
  1580. struct btrfs_path *path;
  1581. struct btrfs_key key;
  1582. struct btrfs_fs_info *info = root->fs_info;
  1583. struct btrfs_root *extent_root = info->extent_root;
  1584. struct extent_buffer *leaf;
  1585. int ret;
  1586. int extent_slot = 0;
  1587. int found_extent = 0;
  1588. int num_to_del = 1;
  1589. struct btrfs_extent_item *ei;
  1590. u32 refs;
  1591. key.objectid = bytenr;
  1592. btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
  1593. key.offset = num_bytes;
  1594. path = btrfs_alloc_path();
  1595. if (!path)
  1596. return -ENOMEM;
  1597. path->reada = 1;
  1598. ret = lookup_extent_backref(trans, extent_root, path,
  1599. bytenr, parent, root_objectid,
  1600. ref_generation, owner_objectid, 1);
  1601. if (ret == 0) {
  1602. struct btrfs_key found_key;
  1603. extent_slot = path->slots[0];
  1604. while(extent_slot > 0) {
  1605. extent_slot--;
  1606. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  1607. extent_slot);
  1608. if (found_key.objectid != bytenr)
  1609. break;
  1610. if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
  1611. found_key.offset == num_bytes) {
  1612. found_extent = 1;
  1613. break;
  1614. }
  1615. if (path->slots[0] - extent_slot > 5)
  1616. break;
  1617. }
  1618. if (!found_extent) {
  1619. ret = remove_extent_backref(trans, extent_root, path);
  1620. BUG_ON(ret);
  1621. btrfs_release_path(extent_root, path);
  1622. ret = btrfs_search_slot(trans, extent_root,
  1623. &key, path, -1, 1);
  1624. BUG_ON(ret);
  1625. extent_slot = path->slots[0];
  1626. }
  1627. } else {
  1628. btrfs_print_leaf(extent_root, path->nodes[0]);
  1629. WARN_ON(1);
  1630. printk("Unable to find ref byte nr %Lu root %Lu "
  1631. "gen %Lu owner %Lu\n", bytenr,
  1632. root_objectid, ref_generation, owner_objectid);
  1633. }
  1634. leaf = path->nodes[0];
  1635. ei = btrfs_item_ptr(leaf, extent_slot,
  1636. struct btrfs_extent_item);
  1637. refs = btrfs_extent_refs(leaf, ei);
  1638. BUG_ON(refs == 0);
  1639. refs -= 1;
  1640. btrfs_set_extent_refs(leaf, ei, refs);
  1641. btrfs_mark_buffer_dirty(leaf);
  1642. if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
  1643. struct btrfs_extent_ref *ref;
  1644. ref = btrfs_item_ptr(leaf, path->slots[0],
  1645. struct btrfs_extent_ref);
  1646. BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1);
  1647. /* if the back ref and the extent are next to each other
  1648. * they get deleted below in one shot
  1649. */
  1650. path->slots[0] = extent_slot;
  1651. num_to_del = 2;
  1652. } else if (found_extent) {
  1653. /* otherwise delete the extent back ref */
  1654. ret = remove_extent_backref(trans, extent_root, path);
  1655. BUG_ON(ret);
  1656. /* if refs are 0, we need to setup the path for deletion */
  1657. if (refs == 0) {
  1658. btrfs_release_path(extent_root, path);
  1659. ret = btrfs_search_slot(trans, extent_root, &key, path,
  1660. -1, 1);
  1661. BUG_ON(ret);
  1662. }
  1663. }
  1664. if (refs == 0) {
  1665. u64 super_used;
  1666. u64 root_used;
  1667. #ifdef BIO_RW_DISCARD
  1668. u64 map_length = num_bytes;
  1669. struct btrfs_multi_bio *multi = NULL;
  1670. #endif
  1671. if (pin) {
  1672. mutex_lock(&root->fs_info->pinned_mutex);
  1673. ret = pin_down_bytes(trans, root, bytenr, num_bytes,
  1674. owner_objectid >= BTRFS_FIRST_FREE_OBJECTID);
  1675. mutex_unlock(&root->fs_info->pinned_mutex);
  1676. if (ret > 0)
  1677. mark_free = 1;
  1678. BUG_ON(ret < 0);
  1679. }
  1680. /* block accounting for super block */
  1681. spin_lock_irq(&info->delalloc_lock);
  1682. super_used = btrfs_super_bytes_used(&info->super_copy);
  1683. btrfs_set_super_bytes_used(&info->super_copy,
  1684. super_used - num_bytes);
  1685. spin_unlock_irq(&info->delalloc_lock);
  1686. /* block accounting for root item */
  1687. root_used = btrfs_root_used(&root->root_item);
  1688. btrfs_set_root_used(&root->root_item,
  1689. root_used - num_bytes);
  1690. ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
  1691. num_to_del);
  1692. BUG_ON(ret);
  1693. btrfs_release_path(extent_root, path);
  1694. ret = update_block_group(trans, root, bytenr, num_bytes, 0,
  1695. mark_free);
  1696. BUG_ON(ret);
  1697. #ifdef BIO_RW_DISCARD
  1698. /* Tell the block device(s) that the sectors can be discarded */
  1699. ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
  1700. bytenr, &map_length, &multi, 0);
  1701. if (!ret) {
  1702. struct btrfs_bio_stripe *stripe = multi->stripes;
  1703. int i;
  1704. if (map_length > num_bytes)
  1705. map_length = num_bytes;
  1706. for (i = 0; i < multi->num_stripes; i++, stripe++) {
  1707. blkdev_issue_discard(stripe->dev->bdev,
  1708. stripe->physical >> 9,
  1709. map_length >> 9);
  1710. }
  1711. kfree(multi);
  1712. }
  1713. #endif
  1714. }
  1715. btrfs_free_path(path);
  1716. finish_current_insert(trans, extent_root, 0);
  1717. return ret;
  1718. }
  1719. /*
  1720. * find all the blocks marked as pending in the radix tree and remove
  1721. * them from the extent map
  1722. */
  1723. static int del_pending_extents(struct btrfs_trans_handle *trans, struct
  1724. btrfs_root *extent_root, int all)
  1725. {
  1726. int ret;
  1727. int err = 0;
  1728. u64 start;
  1729. u64 end;
  1730. u64 priv;
  1731. u64 search = 0;
  1732. struct extent_io_tree *pending_del;
  1733. struct extent_io_tree *extent_ins;
  1734. struct pending_extent_op *extent_op;
  1735. struct btrfs_fs_info *info = extent_root->fs_info;
  1736. extent_ins = &extent_root->fs_info->extent_ins;
  1737. pending_del = &extent_root->fs_info->pending_del;
  1738. while(1) {
  1739. mutex_lock(&info->extent_ins_mutex);
  1740. ret = find_first_extent_bit(pending_del, search, &start, &end,
  1741. EXTENT_WRITEBACK);
  1742. if (ret) {
  1743. mutex_unlock(&info->extent_ins_mutex);
  1744. if (all && search) {
  1745. search = 0;
  1746. continue;
  1747. }
  1748. break;
  1749. }
  1750. ret = try_lock_extent(extent_ins, start, end, GFP_NOFS);
  1751. if (!ret) {
  1752. search = end+1;
  1753. mutex_unlock(&info->extent_ins_mutex);
  1754. cond_resched();
  1755. continue;
  1756. }
  1757. BUG_ON(ret < 0);
  1758. ret = get_state_private(pending_del, start, &priv);
  1759. BUG_ON(ret);
  1760. extent_op = (struct pending_extent_op *)(unsigned long)priv;
  1761. clear_extent_bits(pending_del, start, end, EXTENT_WRITEBACK,
  1762. GFP_NOFS);
  1763. if (!test_range_bit(extent_ins, start, end,
  1764. EXTENT_WRITEBACK, 0)) {
  1765. mutex_unlock(&info->extent_ins_mutex);
  1766. free_extent:
  1767. ret = __free_extent(trans, extent_root,
  1768. start, end + 1 - start,
  1769. extent_op->orig_parent,
  1770. extent_root->root_key.objectid,
  1771. extent_op->orig_generation,
  1772. extent_op->level, 1, 0);
  1773. kfree(extent_op);
  1774. } else {
  1775. kfree(extent_op);
  1776. ret = get_state_private(&info->extent_ins, start,
  1777. &priv);
  1778. BUG_ON(ret);
  1779. extent_op = (struct pending_extent_op *)
  1780. (unsigned long)priv;
  1781. clear_extent_bits(&info->extent_ins, start, end,
  1782. EXTENT_WRITEBACK, GFP_NOFS);
  1783. mutex_unlock(&info->extent_ins_mutex);
  1784. if (extent_op->type == PENDING_BACKREF_UPDATE)
  1785. goto free_extent;
  1786. mutex_lock(&extent_root->fs_info->pinned_mutex);
  1787. ret = pin_down_bytes(trans, extent_root, start,
  1788. end + 1 - start, 0);
  1789. mutex_unlock(&extent_root->fs_info->pinned_mutex);
  1790. ret = update_block_group(trans, extent_root, start,
  1791. end + 1 - start, 0, ret > 0);
  1792. BUG_ON(ret);
  1793. kfree(extent_op);
  1794. }
  1795. if (ret)
  1796. err = ret;
  1797. unlock_extent(extent_ins, start, end, GFP_NOFS);
  1798. if (all)
  1799. search = 0;
  1800. else
  1801. search = end + 1;
  1802. cond_resched();
  1803. }
  1804. return err;
  1805. }
  1806. /*
  1807. * remove an extent from the root, returns 0 on success
  1808. */
  1809. static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
  1810. struct btrfs_root *root,
  1811. u64 bytenr, u64 num_bytes, u64 parent,
  1812. u64 root_objectid, u64 ref_generation,
  1813. u64 owner_objectid, int pin)
  1814. {
  1815. struct btrfs_root *extent_root = root->fs_info->extent_root;
  1816. int pending_ret;
  1817. int ret;
  1818. WARN_ON(num_bytes < root->sectorsize);
  1819. if (root == extent_root) {
  1820. struct pending_extent_op *extent_op;
  1821. extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
  1822. BUG_ON(!extent_op);
  1823. extent_op->type = PENDING_EXTENT_DELETE;
  1824. extent_op->bytenr = bytenr;
  1825. extent_op->num_bytes = num_bytes;
  1826. extent_op->parent = parent;
  1827. extent_op->orig_parent = parent;
  1828. extent_op->generation = ref_generation;
  1829. extent_op->orig_generation = ref_generation;
  1830. extent_op->level = (int)owner_objectid;
  1831. mutex_lock(&root->fs_info->extent_ins_mutex);
  1832. set_extent_bits(&root->fs_info->pending_del,
  1833. bytenr, bytenr + num_bytes - 1,
  1834. EXTENT_WRITEBACK, GFP_NOFS);
  1835. set_state_private(&root->fs_info->pending_del,
  1836. bytenr, (unsigned long)extent_op);
  1837. mutex_unlock(&root->fs_info->extent_ins_mutex);
  1838. return 0;
  1839. }
  1840. /* if metadata always pin */
  1841. if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
  1842. if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
  1843. struct btrfs_block_group_cache *cache;
  1844. /* btrfs_free_reserved_extent */
  1845. cache = btrfs_lookup_block_group(root->fs_info, bytenr);
  1846. BUG_ON(!cache);
  1847. btrfs_add_free_space(cache, bytenr, num_bytes);
  1848. update_reserved_extents(root, bytenr, num_bytes, 0);
  1849. return 0;
  1850. }
  1851. pin = 1;
  1852. }
  1853. /* if data pin when any transaction has committed this */
  1854. if (ref_generation != trans->transid)
  1855. pin = 1;
  1856. ret = __free_extent(trans, root, bytenr, num_bytes, parent,
  1857. root_objectid, ref_generation,
  1858. owner_objectid, pin, pin == 0);
  1859. finish_current_insert(trans, root->fs_info->extent_root, 0);
  1860. pending_ret = del_pending_extents(trans, root->fs_info->extent_root, 0);
  1861. return ret ? ret : pending_ret;
  1862. }
  1863. 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, int pin)
  1868. {
  1869. int ret;
  1870. ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, parent,
  1871. root_objectid, ref_generation,
  1872. owner_objectid, pin);
  1873. return ret;
  1874. }
  1875. static u64 stripe_align(struct btrfs_root *root, u64 val)
  1876. {
  1877. u64 mask = ((u64)root->stripesize - 1);
  1878. u64 ret = (val + mask) & ~mask;
  1879. return ret;
  1880. }
  1881. /*
  1882. * walks the btree of allocated extents and find a hole of a given size.
  1883. * The key ins is changed to record the hole:
  1884. * ins->objectid == block start
  1885. * ins->flags = BTRFS_EXTENT_ITEM_KEY
  1886. * ins->offset == number of blocks
  1887. * Any available blocks before search_start are skipped.
  1888. */
  1889. static int noinline find_free_extent(struct btrfs_trans_handle *trans,
  1890. struct btrfs_root *orig_root,
  1891. u64 num_bytes, u64 empty_size,
  1892. u64 search_start, u64 search_end,
  1893. u64 hint_byte, struct btrfs_key *ins,
  1894. u64 exclude_start, u64 exclude_nr,
  1895. int data)
  1896. {
  1897. int ret = 0;
  1898. struct btrfs_root * root = orig_root->fs_info->extent_root;
  1899. u64 total_needed = num_bytes;
  1900. u64 *last_ptr = NULL;
  1901. u64 last_wanted = 0;
  1902. struct btrfs_block_group_cache *block_group = NULL;
  1903. int chunk_alloc_done = 0;
  1904. int empty_cluster = 2 * 1024 * 1024;
  1905. int allowed_chunk_alloc = 0;
  1906. struct list_head *head = NULL, *cur = NULL;
  1907. int loop = 0;
  1908. struct btrfs_space_info *space_info;
  1909. WARN_ON(num_bytes < root->sectorsize);
  1910. btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
  1911. ins->objectid = 0;
  1912. ins->offset = 0;
  1913. if (orig_root->ref_cows || empty_size)
  1914. allowed_chunk_alloc = 1;
  1915. if (data & BTRFS_BLOCK_GROUP_METADATA) {
  1916. last_ptr = &root->fs_info->last_alloc;
  1917. empty_cluster = 64 * 1024;
  1918. }
  1919. if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD))
  1920. last_ptr = &root->fs_info->last_data_alloc;
  1921. if (last_ptr) {
  1922. if (*last_ptr) {
  1923. hint_byte = *last_ptr;
  1924. last_wanted = *last_ptr;
  1925. } else
  1926. empty_size += empty_cluster;
  1927. } else {
  1928. empty_cluster = 0;
  1929. }
  1930. search_start = max(search_start, first_logical_byte(root, 0));
  1931. search_start = max(search_start, hint_byte);
  1932. total_needed += empty_size;
  1933. if (search_start != last_wanted)
  1934. last_wanted = 0;
  1935. block_group = btrfs_lookup_block_group(root->fs_info, search_start);
  1936. if (!block_group)
  1937. block_group = btrfs_lookup_first_block_group(root->fs_info,
  1938. search_start);
  1939. space_info = __find_space_info(root->fs_info, data);
  1940. down_read(&space_info->groups_sem);
  1941. while (1) {
  1942. struct btrfs_free_space *free_space;
  1943. /*
  1944. * the only way this happens if our hint points to a block
  1945. * group thats not of the proper type, while looping this
  1946. * should never happen
  1947. */
  1948. WARN_ON(!block_group);
  1949. mutex_lock(&block_group->alloc_mutex);
  1950. if (unlikely(!block_group_bits(block_group, data)))
  1951. goto new_group;
  1952. ret = cache_block_group(root, block_group);
  1953. if (ret) {
  1954. mutex_unlock(&block_group->alloc_mutex);
  1955. break;
  1956. }
  1957. if (block_group->ro)
  1958. goto new_group;
  1959. free_space = btrfs_find_free_space(block_group, search_start,
  1960. total_needed);
  1961. if (free_space) {
  1962. u64 start = block_group->key.objectid;
  1963. u64 end = block_group->key.objectid +
  1964. block_group->key.offset;
  1965. search_start = stripe_align(root, free_space->offset);
  1966. /* move on to the next group */
  1967. if (search_start + num_bytes >= search_end)
  1968. goto new_group;
  1969. /* move on to the next group */
  1970. if (search_start + num_bytes > end)
  1971. goto new_group;
  1972. if (last_wanted && search_start != last_wanted) {
  1973. total_needed += empty_cluster;
  1974. last_wanted = 0;
  1975. /*
  1976. * if search_start is still in this block group
  1977. * then we just re-search this block group
  1978. */
  1979. if (search_start >= start &&
  1980. search_start < end) {
  1981. mutex_unlock(&block_group->alloc_mutex);
  1982. continue;
  1983. }
  1984. /* else we go to the next block group */
  1985. goto new_group;
  1986. }
  1987. if (exclude_nr > 0 &&
  1988. (search_start + num_bytes > exclude_start &&
  1989. search_start < exclude_start + exclude_nr)) {
  1990. search_start = exclude_start + exclude_nr;
  1991. /*
  1992. * if search_start is still in this block group
  1993. * then we just re-search this block group
  1994. */
  1995. if (search_start >= start &&
  1996. search_start < end) {
  1997. mutex_unlock(&block_group->alloc_mutex);
  1998. last_wanted = 0;
  1999. continue;
  2000. }
  2001. /* else we go to the next block group */
  2002. goto new_group;
  2003. }
  2004. ins->objectid = search_start;
  2005. ins->offset = num_bytes;
  2006. btrfs_remove_free_space_lock(block_group, search_start,
  2007. num_bytes);
  2008. /* we are all good, lets return */
  2009. mutex_unlock(&block_group->alloc_mutex);
  2010. break;
  2011. }
  2012. new_group:
  2013. last_wanted = 0;
  2014. if (loop > 0) {
  2015. total_needed -= empty_cluster;
  2016. empty_cluster = 0;
  2017. }
  2018. mutex_unlock(&block_group->alloc_mutex);
  2019. /*
  2020. * Here's how this works.
  2021. * loop == 0: we were searching a block group via a hint
  2022. * and didn't find anything, so we start at
  2023. * the head of the block groups and keep searching
  2024. * loop == 1: we're searching through all of the block groups
  2025. * if we hit the head again we have searched
  2026. * all of the block groups for this space and we
  2027. * need to try and allocate, if we cant error out.
  2028. * loop == 2: we allocated more space and are looping through
  2029. * all of the block groups again.
  2030. */
  2031. if (loop == 0) {
  2032. head = &space_info->block_groups;
  2033. cur = head->next;
  2034. loop++;
  2035. } else if (loop == 1 && cur == head) {
  2036. if (allowed_chunk_alloc && !chunk_alloc_done) {
  2037. up_read(&space_info->groups_sem);
  2038. ret = do_chunk_alloc(trans, root, num_bytes +
  2039. 2 * 1024 * 1024, data, 1);
  2040. if (ret < 0)
  2041. break;
  2042. down_read(&space_info->groups_sem);
  2043. loop++;
  2044. head = &space_info->block_groups;
  2045. cur = head->next;
  2046. chunk_alloc_done = 1;
  2047. } else if (!allowed_chunk_alloc) {
  2048. space_info->force_alloc = 1;
  2049. break;
  2050. } else {
  2051. break;
  2052. }
  2053. } else if (cur == head) {
  2054. break;
  2055. }
  2056. block_group = list_entry(cur, struct btrfs_block_group_cache,
  2057. list);
  2058. search_start = block_group->key.objectid;
  2059. cur = cur->next;
  2060. }
  2061. /* we found what we needed */
  2062. if (ins->objectid) {
  2063. if (!(data & BTRFS_BLOCK_GROUP_DATA))
  2064. trans->block_group = block_group;
  2065. if (last_ptr)
  2066. *last_ptr = ins->objectid + ins->offset;
  2067. ret = 0;
  2068. } else if (!ret) {
  2069. ret = -ENOSPC;
  2070. }
  2071. up_read(&space_info->groups_sem);
  2072. return ret;
  2073. }
  2074. static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
  2075. {
  2076. struct btrfs_block_group_cache *cache;
  2077. struct list_head *l;
  2078. printk(KERN_INFO "space_info has %Lu free, is %sfull\n",
  2079. info->total_bytes - info->bytes_used - info->bytes_pinned -
  2080. info->bytes_reserved, (info->full) ? "" : "not ");
  2081. down_read(&info->groups_sem);
  2082. list_for_each(l, &info->block_groups) {
  2083. cache = list_entry(l, struct btrfs_block_group_cache, list);
  2084. spin_lock(&cache->lock);
  2085. printk(KERN_INFO "block group %Lu has %Lu bytes, %Lu used "
  2086. "%Lu pinned %Lu reserved\n",
  2087. cache->key.objectid, cache->key.offset,
  2088. btrfs_block_group_used(&cache->item),
  2089. cache->pinned, cache->reserved);
  2090. btrfs_dump_free_space(cache, bytes);
  2091. spin_unlock(&cache->lock);
  2092. }
  2093. up_read(&info->groups_sem);
  2094. }
  2095. static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
  2096. struct btrfs_root *root,
  2097. u64 num_bytes, u64 min_alloc_size,
  2098. u64 empty_size, u64 hint_byte,
  2099. u64 search_end, struct btrfs_key *ins,
  2100. u64 data)
  2101. {
  2102. int ret;
  2103. u64 search_start = 0;
  2104. u64 alloc_profile;
  2105. struct btrfs_fs_info *info = root->fs_info;
  2106. if (data) {
  2107. alloc_profile = info->avail_data_alloc_bits &
  2108. info->data_alloc_profile;
  2109. data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
  2110. } else if (root == root->fs_info->chunk_root) {
  2111. alloc_profile = info->avail_system_alloc_bits &
  2112. info->system_alloc_profile;
  2113. data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
  2114. } else {
  2115. alloc_profile = info->avail_metadata_alloc_bits &
  2116. info->metadata_alloc_profile;
  2117. data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
  2118. }
  2119. again:
  2120. data = reduce_alloc_profile(root, data);
  2121. /*
  2122. * the only place that sets empty_size is btrfs_realloc_node, which
  2123. * is not called recursively on allocations
  2124. */
  2125. if (empty_size || root->ref_cows) {
  2126. if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
  2127. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  2128. 2 * 1024 * 1024,
  2129. BTRFS_BLOCK_GROUP_METADATA |
  2130. (info->metadata_alloc_profile &
  2131. info->avail_metadata_alloc_bits), 0);
  2132. }
  2133. ret = do_chunk_alloc(trans, root->fs_info->extent_root,
  2134. num_bytes + 2 * 1024 * 1024, data, 0);
  2135. }
  2136. WARN_ON(num_bytes < root->sectorsize);
  2137. ret = find_free_extent(trans, root, num_bytes, empty_size,
  2138. search_start, search_end, hint_byte, ins,
  2139. trans->alloc_exclude_start,
  2140. trans->alloc_exclude_nr, data);
  2141. if (ret == -ENOSPC && num_bytes > min_alloc_size) {
  2142. num_bytes = num_bytes >> 1;
  2143. num_bytes = num_bytes & ~(root->sectorsize - 1);
  2144. num_bytes = max(num_bytes, min_alloc_size);
  2145. do_chunk_alloc(trans, root->fs_info->extent_root,
  2146. num_bytes, data, 1);
  2147. goto again;
  2148. }
  2149. if (ret) {
  2150. struct btrfs_space_info *sinfo;
  2151. sinfo = __find_space_info(root->fs_info, data);
  2152. printk("allocation failed flags %Lu, wanted %Lu\n",
  2153. data, num_bytes);
  2154. dump_space_info(sinfo, num_bytes);
  2155. BUG();
  2156. }
  2157. return ret;
  2158. }
  2159. int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
  2160. {
  2161. struct btrfs_block_group_cache *cache;
  2162. cache = btrfs_lookup_block_group(root->fs_info, start);
  2163. if (!cache) {
  2164. printk(KERN_ERR "Unable to find block group for %Lu\n", start);
  2165. return -ENOSPC;
  2166. }
  2167. btrfs_add_free_space(cache, start, len);
  2168. update_reserved_extents(root, start, len, 0);
  2169. return 0;
  2170. }
  2171. int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
  2172. struct btrfs_root *root,
  2173. u64 num_bytes, u64 min_alloc_size,
  2174. u64 empty_size, u64 hint_byte,
  2175. u64 search_end, struct btrfs_key *ins,
  2176. u64 data)
  2177. {
  2178. int ret;
  2179. ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
  2180. empty_size, hint_byte, search_end, ins,
  2181. data);
  2182. update_reserved_extents(root, ins->objectid, ins->offset, 1);
  2183. return ret;
  2184. }
  2185. static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
  2186. struct btrfs_root *root, u64 parent,
  2187. u64 root_objectid, u64 ref_generation,
  2188. u64 owner, struct btrfs_key *ins)
  2189. {
  2190. int ret;
  2191. int pending_ret;
  2192. u64 super_used;
  2193. u64 root_used;
  2194. u64 num_bytes = ins->offset;
  2195. u32 sizes[2];
  2196. struct btrfs_fs_info *info = root->fs_info;
  2197. struct btrfs_root *extent_root = info->extent_root;
  2198. struct btrfs_extent_item *extent_item;
  2199. struct btrfs_extent_ref *ref;
  2200. struct btrfs_path *path;
  2201. struct btrfs_key keys[2];
  2202. if (parent == 0)
  2203. parent = ins->objectid;
  2204. /* block accounting for super block */
  2205. spin_lock_irq(&info->delalloc_lock);
  2206. super_used = btrfs_super_bytes_used(&info->super_copy);
  2207. btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
  2208. spin_unlock_irq(&info->delalloc_lock);
  2209. /* block accounting for root item */
  2210. root_used = btrfs_root_used(&root->root_item);
  2211. btrfs_set_root_used(&root->root_item, root_used + num_bytes);
  2212. if (root == extent_root) {
  2213. struct pending_extent_op *extent_op;
  2214. extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
  2215. BUG_ON(!extent_op);
  2216. extent_op->type = PENDING_EXTENT_INSERT;
  2217. extent_op->bytenr = ins->objectid;
  2218. extent_op->num_bytes = ins->offset;
  2219. extent_op->parent = parent;
  2220. extent_op->orig_parent = 0;
  2221. extent_op->generation = ref_generation;
  2222. extent_op->orig_generation = 0;
  2223. extent_op->level = (int)owner;
  2224. mutex_lock(&root->fs_info->extent_ins_mutex);
  2225. set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
  2226. ins->objectid + ins->offset - 1,
  2227. EXTENT_WRITEBACK, GFP_NOFS);
  2228. set_state_private(&root->fs_info->extent_ins,
  2229. ins->objectid, (unsigned long)extent_op);
  2230. mutex_unlock(&root->fs_info->extent_ins_mutex);
  2231. goto update_block;
  2232. }
  2233. memcpy(&keys[0], ins, sizeof(*ins));
  2234. keys[1].objectid = ins->objectid;
  2235. keys[1].type = BTRFS_EXTENT_REF_KEY;
  2236. keys[1].offset = parent;
  2237. sizes[0] = sizeof(*extent_item);
  2238. sizes[1] = sizeof(*ref);
  2239. path = btrfs_alloc_path();
  2240. BUG_ON(!path);
  2241. ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
  2242. sizes, 2);
  2243. BUG_ON(ret);
  2244. extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
  2245. struct btrfs_extent_item);
  2246. btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
  2247. ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
  2248. struct btrfs_extent_ref);
  2249. btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
  2250. btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
  2251. btrfs_set_ref_objectid(path->nodes[0], ref, owner);
  2252. btrfs_set_ref_num_refs(path->nodes[0], ref, 1);
  2253. btrfs_mark_buffer_dirty(path->nodes[0]);
  2254. trans->alloc_exclude_start = 0;
  2255. trans->alloc_exclude_nr = 0;
  2256. btrfs_free_path(path);
  2257. finish_current_insert(trans, extent_root, 0);
  2258. pending_ret = del_pending_extents(trans, extent_root, 0);
  2259. if (ret)
  2260. goto out;
  2261. if (pending_ret) {
  2262. ret = pending_ret;
  2263. goto out;
  2264. }
  2265. update_block:
  2266. ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
  2267. if (ret) {
  2268. printk("update block group failed for %Lu %Lu\n",
  2269. ins->objectid, ins->offset);
  2270. BUG();
  2271. }
  2272. out:
  2273. return ret;
  2274. }
  2275. int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
  2276. struct btrfs_root *root, u64 parent,
  2277. u64 root_objectid, u64 ref_generation,
  2278. u64 owner, struct btrfs_key *ins)
  2279. {
  2280. int ret;
  2281. if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
  2282. return 0;
  2283. ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
  2284. ref_generation, owner, ins);
  2285. update_reserved_extents(root, ins->objectid, ins->offset, 0);
  2286. return ret;
  2287. }
  2288. /*
  2289. * this is used by the tree logging recovery code. It records that
  2290. * an extent has been allocated and makes sure to clear the free
  2291. * space cache bits as well
  2292. */
  2293. int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
  2294. struct btrfs_root *root, u64 parent,
  2295. u64 root_objectid, u64 ref_generation,
  2296. u64 owner, struct btrfs_key *ins)
  2297. {
  2298. int ret;
  2299. struct btrfs_block_group_cache *block_group;
  2300. block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
  2301. mutex_lock(&block_group->alloc_mutex);
  2302. cache_block_group(root, block_group);
  2303. ret = btrfs_remove_free_space_lock(block_group, ins->objectid,
  2304. ins->offset);
  2305. mutex_unlock(&block_group->alloc_mutex);
  2306. BUG_ON(ret);
  2307. ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
  2308. ref_generation, owner, ins);
  2309. return ret;
  2310. }
  2311. /*
  2312. * finds a free extent and does all the dirty work required for allocation
  2313. * returns the key for the extent through ins, and a tree buffer for
  2314. * the first block of the extent through buf.
  2315. *
  2316. * returns 0 if everything worked, non-zero otherwise.
  2317. */
  2318. int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
  2319. struct btrfs_root *root,
  2320. u64 num_bytes, u64 parent, u64 min_alloc_size,
  2321. u64 root_objectid, u64 ref_generation,
  2322. u64 owner_objectid, u64 empty_size, u64 hint_byte,
  2323. u64 search_end, struct btrfs_key *ins, u64 data)
  2324. {
  2325. int ret;
  2326. ret = __btrfs_reserve_extent(trans, root, num_bytes,
  2327. min_alloc_size, empty_size, hint_byte,
  2328. search_end, ins, data);
  2329. BUG_ON(ret);
  2330. if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
  2331. ret = __btrfs_alloc_reserved_extent(trans, root, parent,
  2332. root_objectid, ref_generation,
  2333. owner_objectid, ins);
  2334. BUG_ON(ret);
  2335. } else {
  2336. update_reserved_extents(root, ins->objectid, ins->offset, 1);
  2337. }
  2338. return ret;
  2339. }
  2340. struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
  2341. struct btrfs_root *root,
  2342. u64 bytenr, u32 blocksize)
  2343. {
  2344. struct extent_buffer *buf;
  2345. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  2346. if (!buf)
  2347. return ERR_PTR(-ENOMEM);
  2348. btrfs_set_header_generation(buf, trans->transid);
  2349. btrfs_tree_lock(buf);
  2350. clean_tree_block(trans, root, buf);
  2351. btrfs_set_buffer_uptodate(buf);
  2352. if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
  2353. set_extent_dirty(&root->dirty_log_pages, buf->start,
  2354. buf->start + buf->len - 1, GFP_NOFS);
  2355. } else {
  2356. set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
  2357. buf->start + buf->len - 1, GFP_NOFS);
  2358. }
  2359. trans->blocks_used++;
  2360. return buf;
  2361. }
  2362. /*
  2363. * helper function to allocate a block for a given tree
  2364. * returns the tree buffer or NULL.
  2365. */
  2366. struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
  2367. struct btrfs_root *root,
  2368. u32 blocksize, u64 parent,
  2369. u64 root_objectid,
  2370. u64 ref_generation,
  2371. int level,
  2372. u64 hint,
  2373. u64 empty_size)
  2374. {
  2375. struct btrfs_key ins;
  2376. int ret;
  2377. struct extent_buffer *buf;
  2378. ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize,
  2379. root_objectid, ref_generation, level,
  2380. empty_size, hint, (u64)-1, &ins, 0);
  2381. if (ret) {
  2382. BUG_ON(ret > 0);
  2383. return ERR_PTR(ret);
  2384. }
  2385. buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize);
  2386. return buf;
  2387. }
  2388. int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
  2389. struct btrfs_root *root, struct extent_buffer *leaf)
  2390. {
  2391. u64 leaf_owner;
  2392. u64 leaf_generation;
  2393. struct btrfs_key key;
  2394. struct btrfs_file_extent_item *fi;
  2395. int i;
  2396. int nritems;
  2397. int ret;
  2398. BUG_ON(!btrfs_is_leaf(leaf));
  2399. nritems = btrfs_header_nritems(leaf);
  2400. leaf_owner = btrfs_header_owner(leaf);
  2401. leaf_generation = btrfs_header_generation(leaf);
  2402. for (i = 0; i < nritems; i++) {
  2403. u64 disk_bytenr;
  2404. cond_resched();
  2405. btrfs_item_key_to_cpu(leaf, &key, i);
  2406. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  2407. continue;
  2408. fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
  2409. if (btrfs_file_extent_type(leaf, fi) ==
  2410. BTRFS_FILE_EXTENT_INLINE)
  2411. continue;
  2412. /*
  2413. * FIXME make sure to insert a trans record that
  2414. * repeats the snapshot del on crash
  2415. */
  2416. disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  2417. if (disk_bytenr == 0)
  2418. continue;
  2419. ret = __btrfs_free_extent(trans, root, disk_bytenr,
  2420. btrfs_file_extent_disk_num_bytes(leaf, fi),
  2421. leaf->start, leaf_owner, leaf_generation,
  2422. key.objectid, 0);
  2423. BUG_ON(ret);
  2424. atomic_inc(&root->fs_info->throttle_gen);
  2425. wake_up(&root->fs_info->transaction_throttle);
  2426. cond_resched();
  2427. }
  2428. return 0;
  2429. }
  2430. static int noinline cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
  2431. struct btrfs_root *root,
  2432. struct btrfs_leaf_ref *ref)
  2433. {
  2434. int i;
  2435. int ret;
  2436. struct btrfs_extent_info *info = ref->extents;
  2437. for (i = 0; i < ref->nritems; i++) {
  2438. ret = __btrfs_free_extent(trans, root, info->bytenr,
  2439. info->num_bytes, ref->bytenr,
  2440. ref->owner, ref->generation,
  2441. info->objectid, 0);
  2442. atomic_inc(&root->fs_info->throttle_gen);
  2443. wake_up(&root->fs_info->transaction_throttle);
  2444. cond_resched();
  2445. BUG_ON(ret);
  2446. info++;
  2447. }
  2448. return 0;
  2449. }
  2450. int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start, u64 len,
  2451. u32 *refs)
  2452. {
  2453. int ret;
  2454. ret = btrfs_lookup_extent_ref(NULL, root, start, len, refs);
  2455. BUG_ON(ret);
  2456. #if 0 // some debugging code in case we see problems here
  2457. /* if the refs count is one, it won't get increased again. But
  2458. * if the ref count is > 1, someone may be decreasing it at
  2459. * the same time we are.
  2460. */
  2461. if (*refs != 1) {
  2462. struct extent_buffer *eb = NULL;
  2463. eb = btrfs_find_create_tree_block(root, start, len);
  2464. if (eb)
  2465. btrfs_tree_lock(eb);
  2466. mutex_lock(&root->fs_info->alloc_mutex);
  2467. ret = lookup_extent_ref(NULL, root, start, len, refs);
  2468. BUG_ON(ret);
  2469. mutex_unlock(&root->fs_info->alloc_mutex);
  2470. if (eb) {
  2471. btrfs_tree_unlock(eb);
  2472. free_extent_buffer(eb);
  2473. }
  2474. if (*refs == 1) {
  2475. printk("block %llu went down to one during drop_snap\n",
  2476. (unsigned long long)start);
  2477. }
  2478. }
  2479. #endif
  2480. cond_resched();
  2481. return ret;
  2482. }
  2483. /*
  2484. * helper function for drop_snapshot, this walks down the tree dropping ref
  2485. * counts as it goes.
  2486. */
  2487. static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
  2488. struct btrfs_root *root,
  2489. struct btrfs_path *path, int *level)
  2490. {
  2491. u64 root_owner;
  2492. u64 root_gen;
  2493. u64 bytenr;
  2494. u64 ptr_gen;
  2495. struct extent_buffer *next;
  2496. struct extent_buffer *cur;
  2497. struct extent_buffer *parent;
  2498. struct btrfs_leaf_ref *ref;
  2499. u32 blocksize;
  2500. int ret;
  2501. u32 refs;
  2502. WARN_ON(*level < 0);
  2503. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  2504. ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start,
  2505. path->nodes[*level]->len, &refs);
  2506. BUG_ON(ret);
  2507. if (refs > 1)
  2508. goto out;
  2509. /*
  2510. * walk down to the last node level and free all the leaves
  2511. */
  2512. while(*level >= 0) {
  2513. WARN_ON(*level < 0);
  2514. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  2515. cur = path->nodes[*level];
  2516. if (btrfs_header_level(cur) != *level)
  2517. WARN_ON(1);
  2518. if (path->slots[*level] >=
  2519. btrfs_header_nritems(cur))
  2520. break;
  2521. if (*level == 0) {
  2522. ret = btrfs_drop_leaf_ref(trans, root, cur);
  2523. BUG_ON(ret);
  2524. break;
  2525. }
  2526. bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
  2527. ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
  2528. blocksize = btrfs_level_size(root, *level - 1);
  2529. ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs);
  2530. BUG_ON(ret);
  2531. if (refs != 1) {
  2532. parent = path->nodes[*level];
  2533. root_owner = btrfs_header_owner(parent);
  2534. root_gen = btrfs_header_generation(parent);
  2535. path->slots[*level]++;
  2536. ret = __btrfs_free_extent(trans, root, bytenr,
  2537. blocksize, parent->start,
  2538. root_owner, root_gen,
  2539. *level - 1, 1);
  2540. BUG_ON(ret);
  2541. atomic_inc(&root->fs_info->throttle_gen);
  2542. wake_up(&root->fs_info->transaction_throttle);
  2543. cond_resched();
  2544. continue;
  2545. }
  2546. /*
  2547. * at this point, we have a single ref, and since the
  2548. * only place referencing this extent is a dead root
  2549. * the reference count should never go higher.
  2550. * So, we don't need to check it again
  2551. */
  2552. if (*level == 1) {
  2553. ref = btrfs_lookup_leaf_ref(root, bytenr);
  2554. if (ref && ref->generation != ptr_gen) {
  2555. btrfs_free_leaf_ref(root, ref);
  2556. ref = NULL;
  2557. }
  2558. if (ref) {
  2559. ret = cache_drop_leaf_ref(trans, root, ref);
  2560. BUG_ON(ret);
  2561. btrfs_remove_leaf_ref(root, ref);
  2562. btrfs_free_leaf_ref(root, ref);
  2563. *level = 0;
  2564. break;
  2565. }
  2566. if (printk_ratelimit()) {
  2567. printk("leaf ref miss for bytenr %llu\n",
  2568. (unsigned long long)bytenr);
  2569. }
  2570. }
  2571. next = btrfs_find_tree_block(root, bytenr, blocksize);
  2572. if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
  2573. free_extent_buffer(next);
  2574. next = read_tree_block(root, bytenr, blocksize,
  2575. ptr_gen);
  2576. cond_resched();
  2577. #if 0
  2578. /*
  2579. * this is a debugging check and can go away
  2580. * the ref should never go all the way down to 1
  2581. * at this point
  2582. */
  2583. ret = lookup_extent_ref(NULL, root, bytenr, blocksize,
  2584. &refs);
  2585. BUG_ON(ret);
  2586. WARN_ON(refs != 1);
  2587. #endif
  2588. }
  2589. WARN_ON(*level <= 0);
  2590. if (path->nodes[*level-1])
  2591. free_extent_buffer(path->nodes[*level-1]);
  2592. path->nodes[*level-1] = next;
  2593. *level = btrfs_header_level(next);
  2594. path->slots[*level] = 0;
  2595. cond_resched();
  2596. }
  2597. out:
  2598. WARN_ON(*level < 0);
  2599. WARN_ON(*level >= BTRFS_MAX_LEVEL);
  2600. if (path->nodes[*level] == root->node) {
  2601. parent = path->nodes[*level];
  2602. bytenr = path->nodes[*level]->start;
  2603. } else {
  2604. parent = path->nodes[*level + 1];
  2605. bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
  2606. }
  2607. blocksize = btrfs_level_size(root, *level);
  2608. root_owner = btrfs_header_owner(parent);
  2609. root_gen = btrfs_header_generation(parent);
  2610. ret = __btrfs_free_extent(trans, root, bytenr, blocksize,
  2611. parent->start, root_owner, root_gen,
  2612. *level, 1);
  2613. free_extent_buffer(path->nodes[*level]);
  2614. path->nodes[*level] = NULL;
  2615. *level += 1;
  2616. BUG_ON(ret);
  2617. cond_resched();
  2618. return 0;
  2619. }
  2620. /*
  2621. * helper function for drop_subtree, this function is similar to
  2622. * walk_down_tree. The main difference is that it checks reference
  2623. * counts while tree blocks are locked.
  2624. */
  2625. static int noinline walk_down_subtree(struct btrfs_trans_handle *trans,
  2626. struct btrfs_root *root,
  2627. struct btrfs_path *path, int *level)
  2628. {
  2629. struct extent_buffer *next;
  2630. struct extent_buffer *cur;
  2631. struct extent_buffer *parent;
  2632. u64 bytenr;
  2633. u64 ptr_gen;
  2634. u32 blocksize;
  2635. u32 refs;
  2636. int ret;
  2637. cur = path->nodes[*level];
  2638. ret = btrfs_lookup_extent_ref(trans, root, cur->start, cur->len,
  2639. &refs);
  2640. BUG_ON(ret);
  2641. if (refs > 1)
  2642. goto out;
  2643. while (*level >= 0) {
  2644. cur = path->nodes[*level];
  2645. if (*level == 0) {
  2646. ret = btrfs_drop_leaf_ref(trans, root, cur);
  2647. BUG_ON(ret);
  2648. clean_tree_block(trans, root, cur);
  2649. break;
  2650. }
  2651. if (path->slots[*level] >= btrfs_header_nritems(cur)) {
  2652. clean_tree_block(trans, root, cur);
  2653. break;
  2654. }
  2655. bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
  2656. blocksize = btrfs_level_size(root, *level - 1);
  2657. ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
  2658. next = read_tree_block(root, bytenr, blocksize, ptr_gen);
  2659. btrfs_tree_lock(next);
  2660. ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
  2661. &refs);
  2662. BUG_ON(ret);
  2663. if (refs > 1) {
  2664. parent = path->nodes[*level];
  2665. ret = btrfs_free_extent(trans, root, bytenr,
  2666. blocksize, parent->start,
  2667. btrfs_header_owner(parent),
  2668. btrfs_header_generation(parent),
  2669. *level - 1, 1);
  2670. BUG_ON(ret);
  2671. path->slots[*level]++;
  2672. btrfs_tree_unlock(next);
  2673. free_extent_buffer(next);
  2674. continue;
  2675. }
  2676. *level = btrfs_header_level(next);
  2677. path->nodes[*level] = next;
  2678. path->slots[*level] = 0;
  2679. path->locks[*level] = 1;
  2680. cond_resched();
  2681. }
  2682. out:
  2683. parent = path->nodes[*level + 1];
  2684. bytenr = path->nodes[*level]->start;
  2685. blocksize = path->nodes[*level]->len;
  2686. ret = btrfs_free_extent(trans, root, bytenr, blocksize,
  2687. parent->start, btrfs_header_owner(parent),
  2688. btrfs_header_generation(parent), *level, 1);
  2689. BUG_ON(ret);
  2690. if (path->locks[*level]) {
  2691. btrfs_tree_unlock(path->nodes[*level]);
  2692. path->locks[*level] = 0;
  2693. }
  2694. free_extent_buffer(path->nodes[*level]);
  2695. path->nodes[*level] = NULL;
  2696. *level += 1;
  2697. cond_resched();
  2698. return 0;
  2699. }
  2700. /*
  2701. * helper for dropping snapshots. This walks back up the tree in the path
  2702. * to find the first node higher up where we haven't yet gone through
  2703. * all the slots
  2704. */
  2705. static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
  2706. struct btrfs_root *root,
  2707. struct btrfs_path *path,
  2708. int *level, int max_level)
  2709. {
  2710. u64 root_owner;
  2711. u64 root_gen;
  2712. struct btrfs_root_item *root_item = &root->root_item;
  2713. int i;
  2714. int slot;
  2715. int ret;
  2716. for (i = *level; i < max_level && path->nodes[i]; i++) {
  2717. slot = path->slots[i];
  2718. if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
  2719. struct extent_buffer *node;
  2720. struct btrfs_disk_key disk_key;
  2721. node = path->nodes[i];
  2722. path->slots[i]++;
  2723. *level = i;
  2724. WARN_ON(*level == 0);
  2725. btrfs_node_key(node, &disk_key, path->slots[i]);
  2726. memcpy(&root_item->drop_progress,
  2727. &disk_key, sizeof(disk_key));
  2728. root_item->drop_level = i;
  2729. return 0;
  2730. } else {
  2731. struct extent_buffer *parent;
  2732. if (path->nodes[*level] == root->node)
  2733. parent = path->nodes[*level];
  2734. else
  2735. parent = path->nodes[*level + 1];
  2736. root_owner = btrfs_header_owner(parent);
  2737. root_gen = btrfs_header_generation(parent);
  2738. clean_tree_block(trans, root, path->nodes[*level]);
  2739. ret = btrfs_free_extent(trans, root,
  2740. path->nodes[*level]->start,
  2741. path->nodes[*level]->len,
  2742. parent->start, root_owner,
  2743. root_gen, *level, 1);
  2744. BUG_ON(ret);
  2745. if (path->locks[*level]) {
  2746. btrfs_tree_unlock(path->nodes[*level]);
  2747. path->locks[*level] = 0;
  2748. }
  2749. free_extent_buffer(path->nodes[*level]);
  2750. path->nodes[*level] = NULL;
  2751. *level = i + 1;
  2752. }
  2753. }
  2754. return 1;
  2755. }
  2756. /*
  2757. * drop the reference count on the tree rooted at 'snap'. This traverses
  2758. * the tree freeing any blocks that have a ref count of zero after being
  2759. * decremented.
  2760. */
  2761. int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
  2762. *root)
  2763. {
  2764. int ret = 0;
  2765. int wret;
  2766. int level;
  2767. struct btrfs_path *path;
  2768. int i;
  2769. int orig_level;
  2770. struct btrfs_root_item *root_item = &root->root_item;
  2771. WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
  2772. path = btrfs_alloc_path();
  2773. BUG_ON(!path);
  2774. level = btrfs_header_level(root->node);
  2775. orig_level = level;
  2776. if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
  2777. path->nodes[level] = root->node;
  2778. extent_buffer_get(root->node);
  2779. path->slots[level] = 0;
  2780. } else {
  2781. struct btrfs_key key;
  2782. struct btrfs_disk_key found_key;
  2783. struct extent_buffer *node;
  2784. btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
  2785. level = root_item->drop_level;
  2786. path->lowest_level = level;
  2787. wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2788. if (wret < 0) {
  2789. ret = wret;
  2790. goto out;
  2791. }
  2792. node = path->nodes[level];
  2793. btrfs_node_key(node, &found_key, path->slots[level]);
  2794. WARN_ON(memcmp(&found_key, &root_item->drop_progress,
  2795. sizeof(found_key)));
  2796. /*
  2797. * unlock our path, this is safe because only this
  2798. * function is allowed to delete this snapshot
  2799. */
  2800. for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
  2801. if (path->nodes[i] && path->locks[i]) {
  2802. path->locks[i] = 0;
  2803. btrfs_tree_unlock(path->nodes[i]);
  2804. }
  2805. }
  2806. }
  2807. while(1) {
  2808. wret = walk_down_tree(trans, root, path, &level);
  2809. if (wret > 0)
  2810. break;
  2811. if (wret < 0)
  2812. ret = wret;
  2813. wret = walk_up_tree(trans, root, path, &level,
  2814. BTRFS_MAX_LEVEL);
  2815. if (wret > 0)
  2816. break;
  2817. if (wret < 0)
  2818. ret = wret;
  2819. if (trans->transaction->in_commit) {
  2820. ret = -EAGAIN;
  2821. break;
  2822. }
  2823. atomic_inc(&root->fs_info->throttle_gen);
  2824. wake_up(&root->fs_info->transaction_throttle);
  2825. }
  2826. for (i = 0; i <= orig_level; i++) {
  2827. if (path->nodes[i]) {
  2828. free_extent_buffer(path->nodes[i]);
  2829. path->nodes[i] = NULL;
  2830. }
  2831. }
  2832. out:
  2833. btrfs_free_path(path);
  2834. return ret;
  2835. }
  2836. int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
  2837. struct btrfs_root *root,
  2838. struct extent_buffer *node,
  2839. struct extent_buffer *parent)
  2840. {
  2841. struct btrfs_path *path;
  2842. int level;
  2843. int parent_level;
  2844. int ret = 0;
  2845. int wret;
  2846. path = btrfs_alloc_path();
  2847. BUG_ON(!path);
  2848. BUG_ON(!btrfs_tree_locked(parent));
  2849. parent_level = btrfs_header_level(parent);
  2850. extent_buffer_get(parent);
  2851. path->nodes[parent_level] = parent;
  2852. path->slots[parent_level] = btrfs_header_nritems(parent);
  2853. BUG_ON(!btrfs_tree_locked(node));
  2854. level = btrfs_header_level(node);
  2855. extent_buffer_get(node);
  2856. path->nodes[level] = node;
  2857. path->slots[level] = 0;
  2858. while (1) {
  2859. wret = walk_down_subtree(trans, root, path, &level);
  2860. if (wret < 0)
  2861. ret = wret;
  2862. if (wret != 0)
  2863. break;
  2864. wret = walk_up_tree(trans, root, path, &level, parent_level);
  2865. if (wret < 0)
  2866. ret = wret;
  2867. if (wret != 0)
  2868. break;
  2869. }
  2870. btrfs_free_path(path);
  2871. return ret;
  2872. }
  2873. static unsigned long calc_ra(unsigned long start, unsigned long last,
  2874. unsigned long nr)
  2875. {
  2876. return min(last, start + nr - 1);
  2877. }
  2878. static int noinline relocate_inode_pages(struct inode *inode, u64 start,
  2879. u64 len)
  2880. {
  2881. u64 page_start;
  2882. u64 page_end;
  2883. unsigned long first_index;
  2884. unsigned long last_index;
  2885. unsigned long i;
  2886. struct page *page;
  2887. struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
  2888. struct file_ra_state *ra;
  2889. struct btrfs_ordered_extent *ordered;
  2890. unsigned int total_read = 0;
  2891. unsigned int total_dirty = 0;
  2892. int ret = 0;
  2893. ra = kzalloc(sizeof(*ra), GFP_NOFS);
  2894. mutex_lock(&inode->i_mutex);
  2895. first_index = start >> PAGE_CACHE_SHIFT;
  2896. last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
  2897. /* make sure the dirty trick played by the caller work */
  2898. ret = invalidate_inode_pages2_range(inode->i_mapping,
  2899. first_index, last_index);
  2900. if (ret)
  2901. goto out_unlock;
  2902. file_ra_state_init(ra, inode->i_mapping);
  2903. for (i = first_index ; i <= last_index; i++) {
  2904. if (total_read % ra->ra_pages == 0) {
  2905. btrfs_force_ra(inode->i_mapping, ra, NULL, i,
  2906. calc_ra(i, last_index, ra->ra_pages));
  2907. }
  2908. total_read++;
  2909. again:
  2910. if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
  2911. BUG_ON(1);
  2912. page = grab_cache_page(inode->i_mapping, i);
  2913. if (!page) {
  2914. ret = -ENOMEM;
  2915. goto out_unlock;
  2916. }
  2917. if (!PageUptodate(page)) {
  2918. btrfs_readpage(NULL, page);
  2919. lock_page(page);
  2920. if (!PageUptodate(page)) {
  2921. unlock_page(page);
  2922. page_cache_release(page);
  2923. ret = -EIO;
  2924. goto out_unlock;
  2925. }
  2926. }
  2927. wait_on_page_writeback(page);
  2928. page_start = (u64)page->index << PAGE_CACHE_SHIFT;
  2929. page_end = page_start + PAGE_CACHE_SIZE - 1;
  2930. lock_extent(io_tree, page_start, page_end, GFP_NOFS);
  2931. ordered = btrfs_lookup_ordered_extent(inode, page_start);
  2932. if (ordered) {
  2933. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  2934. unlock_page(page);
  2935. page_cache_release(page);
  2936. btrfs_start_ordered_extent(inode, ordered, 1);
  2937. btrfs_put_ordered_extent(ordered);
  2938. goto again;
  2939. }
  2940. set_page_extent_mapped(page);
  2941. btrfs_set_extent_delalloc(inode, page_start, page_end);
  2942. if (i == first_index)
  2943. set_extent_bits(io_tree, page_start, page_end,
  2944. EXTENT_BOUNDARY, GFP_NOFS);
  2945. set_page_dirty(page);
  2946. total_dirty++;
  2947. unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
  2948. unlock_page(page);
  2949. page_cache_release(page);
  2950. }
  2951. out_unlock:
  2952. kfree(ra);
  2953. mutex_unlock(&inode->i_mutex);
  2954. balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
  2955. return ret;
  2956. }
  2957. static int noinline relocate_data_extent(struct inode *reloc_inode,
  2958. struct btrfs_key *extent_key,
  2959. u64 offset)
  2960. {
  2961. struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
  2962. struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
  2963. struct extent_map *em;
  2964. u64 start = extent_key->objectid - offset;
  2965. u64 end = start + extent_key->offset - 1;
  2966. em = alloc_extent_map(GFP_NOFS);
  2967. BUG_ON(!em || IS_ERR(em));
  2968. em->start = start;
  2969. em->len = extent_key->offset;
  2970. em->block_len = extent_key->offset;
  2971. em->block_start = extent_key->objectid;
  2972. em->bdev = root->fs_info->fs_devices->latest_bdev;
  2973. set_bit(EXTENT_FLAG_PINNED, &em->flags);
  2974. /* setup extent map to cheat btrfs_readpage */
  2975. lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
  2976. while (1) {
  2977. int ret;
  2978. spin_lock(&em_tree->lock);
  2979. ret = add_extent_mapping(em_tree, em);
  2980. spin_unlock(&em_tree->lock);
  2981. if (ret != -EEXIST) {
  2982. free_extent_map(em);
  2983. break;
  2984. }
  2985. btrfs_drop_extent_cache(reloc_inode, start, end, 0);
  2986. }
  2987. unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
  2988. return relocate_inode_pages(reloc_inode, start, extent_key->offset);
  2989. }
  2990. struct btrfs_ref_path {
  2991. u64 extent_start;
  2992. u64 nodes[BTRFS_MAX_LEVEL];
  2993. u64 root_objectid;
  2994. u64 root_generation;
  2995. u64 owner_objectid;
  2996. u32 num_refs;
  2997. int lowest_level;
  2998. int current_level;
  2999. int shared_level;
  3000. struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
  3001. u64 new_nodes[BTRFS_MAX_LEVEL];
  3002. };
  3003. struct disk_extent {
  3004. u64 ram_bytes;
  3005. u64 disk_bytenr;
  3006. u64 disk_num_bytes;
  3007. u64 offset;
  3008. u64 num_bytes;
  3009. u8 compression;
  3010. u8 encryption;
  3011. u16 other_encoding;
  3012. };
  3013. static int is_cowonly_root(u64 root_objectid)
  3014. {
  3015. if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
  3016. root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
  3017. root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
  3018. root_objectid == BTRFS_DEV_TREE_OBJECTID ||
  3019. root_objectid == BTRFS_TREE_LOG_OBJECTID)
  3020. return 1;
  3021. return 0;
  3022. }
  3023. static int noinline __next_ref_path(struct btrfs_trans_handle *trans,
  3024. struct btrfs_root *extent_root,
  3025. struct btrfs_ref_path *ref_path,
  3026. int first_time)
  3027. {
  3028. struct extent_buffer *leaf;
  3029. struct btrfs_path *path;
  3030. struct btrfs_extent_ref *ref;
  3031. struct btrfs_key key;
  3032. struct btrfs_key found_key;
  3033. u64 bytenr;
  3034. u32 nritems;
  3035. int level;
  3036. int ret = 1;
  3037. path = btrfs_alloc_path();
  3038. if (!path)
  3039. return -ENOMEM;
  3040. if (first_time) {
  3041. ref_path->lowest_level = -1;
  3042. ref_path->current_level = -1;
  3043. ref_path->shared_level = -1;
  3044. goto walk_up;
  3045. }
  3046. walk_down:
  3047. level = ref_path->current_level - 1;
  3048. while (level >= -1) {
  3049. u64 parent;
  3050. if (level < ref_path->lowest_level)
  3051. break;
  3052. if (level >= 0) {
  3053. bytenr = ref_path->nodes[level];
  3054. } else {
  3055. bytenr = ref_path->extent_start;
  3056. }
  3057. BUG_ON(bytenr == 0);
  3058. parent = ref_path->nodes[level + 1];
  3059. ref_path->nodes[level + 1] = 0;
  3060. ref_path->current_level = level;
  3061. BUG_ON(parent == 0);
  3062. key.objectid = bytenr;
  3063. key.offset = parent + 1;
  3064. key.type = BTRFS_EXTENT_REF_KEY;
  3065. ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
  3066. if (ret < 0)
  3067. goto out;
  3068. BUG_ON(ret == 0);
  3069. leaf = path->nodes[0];
  3070. nritems = btrfs_header_nritems(leaf);
  3071. if (path->slots[0] >= nritems) {
  3072. ret = btrfs_next_leaf(extent_root, path);
  3073. if (ret < 0)
  3074. goto out;
  3075. if (ret > 0)
  3076. goto next;
  3077. leaf = path->nodes[0];
  3078. }
  3079. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3080. if (found_key.objectid == bytenr &&
  3081. found_key.type == BTRFS_EXTENT_REF_KEY) {
  3082. if (level < ref_path->shared_level)
  3083. ref_path->shared_level = level;
  3084. goto found;
  3085. }
  3086. next:
  3087. level--;
  3088. btrfs_release_path(extent_root, path);
  3089. cond_resched();
  3090. }
  3091. /* reached lowest level */
  3092. ret = 1;
  3093. goto out;
  3094. walk_up:
  3095. level = ref_path->current_level;
  3096. while (level < BTRFS_MAX_LEVEL - 1) {
  3097. u64 ref_objectid;
  3098. if (level >= 0) {
  3099. bytenr = ref_path->nodes[level];
  3100. } else {
  3101. bytenr = ref_path->extent_start;
  3102. }
  3103. BUG_ON(bytenr == 0);
  3104. key.objectid = bytenr;
  3105. key.offset = 0;
  3106. key.type = BTRFS_EXTENT_REF_KEY;
  3107. ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
  3108. if (ret < 0)
  3109. goto out;
  3110. leaf = path->nodes[0];
  3111. nritems = btrfs_header_nritems(leaf);
  3112. if (path->slots[0] >= nritems) {
  3113. ret = btrfs_next_leaf(extent_root, path);
  3114. if (ret < 0)
  3115. goto out;
  3116. if (ret > 0) {
  3117. /* the extent was freed by someone */
  3118. if (ref_path->lowest_level == level)
  3119. goto out;
  3120. btrfs_release_path(extent_root, path);
  3121. goto walk_down;
  3122. }
  3123. leaf = path->nodes[0];
  3124. }
  3125. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3126. if (found_key.objectid != bytenr ||
  3127. found_key.type != BTRFS_EXTENT_REF_KEY) {
  3128. /* the extent was freed by someone */
  3129. if (ref_path->lowest_level == level) {
  3130. ret = 1;
  3131. goto out;
  3132. }
  3133. btrfs_release_path(extent_root, path);
  3134. goto walk_down;
  3135. }
  3136. found:
  3137. ref = btrfs_item_ptr(leaf, path->slots[0],
  3138. struct btrfs_extent_ref);
  3139. ref_objectid = btrfs_ref_objectid(leaf, ref);
  3140. if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
  3141. if (first_time) {
  3142. level = (int)ref_objectid;
  3143. BUG_ON(level >= BTRFS_MAX_LEVEL);
  3144. ref_path->lowest_level = level;
  3145. ref_path->current_level = level;
  3146. ref_path->nodes[level] = bytenr;
  3147. } else {
  3148. WARN_ON(ref_objectid != level);
  3149. }
  3150. } else {
  3151. WARN_ON(level != -1);
  3152. }
  3153. first_time = 0;
  3154. if (ref_path->lowest_level == level) {
  3155. ref_path->owner_objectid = ref_objectid;
  3156. ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
  3157. }
  3158. /*
  3159. * the block is tree root or the block isn't in reference
  3160. * counted tree.
  3161. */
  3162. if (found_key.objectid == found_key.offset ||
  3163. is_cowonly_root(btrfs_ref_root(leaf, ref))) {
  3164. ref_path->root_objectid = btrfs_ref_root(leaf, ref);
  3165. ref_path->root_generation =
  3166. btrfs_ref_generation(leaf, ref);
  3167. if (level < 0) {
  3168. /* special reference from the tree log */
  3169. ref_path->nodes[0] = found_key.offset;
  3170. ref_path->current_level = 0;
  3171. }
  3172. ret = 0;
  3173. goto out;
  3174. }
  3175. level++;
  3176. BUG_ON(ref_path->nodes[level] != 0);
  3177. ref_path->nodes[level] = found_key.offset;
  3178. ref_path->current_level = level;
  3179. /*
  3180. * the reference was created in the running transaction,
  3181. * no need to continue walking up.
  3182. */
  3183. if (btrfs_ref_generation(leaf, ref) == trans->transid) {
  3184. ref_path->root_objectid = btrfs_ref_root(leaf, ref);
  3185. ref_path->root_generation =
  3186. btrfs_ref_generation(leaf, ref);
  3187. ret = 0;
  3188. goto out;
  3189. }
  3190. btrfs_release_path(extent_root, path);
  3191. cond_resched();
  3192. }
  3193. /* reached max tree level, but no tree root found. */
  3194. BUG();
  3195. out:
  3196. btrfs_free_path(path);
  3197. return ret;
  3198. }
  3199. static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
  3200. struct btrfs_root *extent_root,
  3201. struct btrfs_ref_path *ref_path,
  3202. u64 extent_start)
  3203. {
  3204. memset(ref_path, 0, sizeof(*ref_path));
  3205. ref_path->extent_start = extent_start;
  3206. return __next_ref_path(trans, extent_root, ref_path, 1);
  3207. }
  3208. static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
  3209. struct btrfs_root *extent_root,
  3210. struct btrfs_ref_path *ref_path)
  3211. {
  3212. return __next_ref_path(trans, extent_root, ref_path, 0);
  3213. }
  3214. static int noinline get_new_locations(struct inode *reloc_inode,
  3215. struct btrfs_key *extent_key,
  3216. u64 offset, int no_fragment,
  3217. struct disk_extent **extents,
  3218. int *nr_extents)
  3219. {
  3220. struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
  3221. struct btrfs_path *path;
  3222. struct btrfs_file_extent_item *fi;
  3223. struct extent_buffer *leaf;
  3224. struct disk_extent *exts = *extents;
  3225. struct btrfs_key found_key;
  3226. u64 cur_pos;
  3227. u64 last_byte;
  3228. u32 nritems;
  3229. int nr = 0;
  3230. int max = *nr_extents;
  3231. int ret;
  3232. WARN_ON(!no_fragment && *extents);
  3233. if (!exts) {
  3234. max = 1;
  3235. exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
  3236. if (!exts)
  3237. return -ENOMEM;
  3238. }
  3239. path = btrfs_alloc_path();
  3240. BUG_ON(!path);
  3241. cur_pos = extent_key->objectid - offset;
  3242. last_byte = extent_key->objectid + extent_key->offset;
  3243. ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
  3244. cur_pos, 0);
  3245. if (ret < 0)
  3246. goto out;
  3247. if (ret > 0) {
  3248. ret = -ENOENT;
  3249. goto out;
  3250. }
  3251. while (1) {
  3252. leaf = path->nodes[0];
  3253. nritems = btrfs_header_nritems(leaf);
  3254. if (path->slots[0] >= nritems) {
  3255. ret = btrfs_next_leaf(root, path);
  3256. if (ret < 0)
  3257. goto out;
  3258. if (ret > 0)
  3259. break;
  3260. leaf = path->nodes[0];
  3261. }
  3262. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  3263. if (found_key.offset != cur_pos ||
  3264. found_key.type != BTRFS_EXTENT_DATA_KEY ||
  3265. found_key.objectid != reloc_inode->i_ino)
  3266. break;
  3267. fi = btrfs_item_ptr(leaf, path->slots[0],
  3268. struct btrfs_file_extent_item);
  3269. if (btrfs_file_extent_type(leaf, fi) !=
  3270. BTRFS_FILE_EXTENT_REG ||
  3271. btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
  3272. break;
  3273. if (nr == max) {
  3274. struct disk_extent *old = exts;
  3275. max *= 2;
  3276. exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
  3277. memcpy(exts, old, sizeof(*exts) * nr);
  3278. if (old != *extents)
  3279. kfree(old);
  3280. }
  3281. exts[nr].disk_bytenr =
  3282. btrfs_file_extent_disk_bytenr(leaf, fi);
  3283. exts[nr].disk_num_bytes =
  3284. btrfs_file_extent_disk_num_bytes(leaf, fi);
  3285. exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
  3286. exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
  3287. exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
  3288. exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
  3289. exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
  3290. exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
  3291. fi);
  3292. BUG_ON(exts[nr].offset > 0);
  3293. BUG_ON(exts[nr].compression || exts[nr].encryption);
  3294. BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
  3295. cur_pos += exts[nr].num_bytes;
  3296. nr++;
  3297. if (cur_pos + offset >= last_byte)
  3298. break;
  3299. if (no_fragment) {
  3300. ret = 1;
  3301. goto out;
  3302. }
  3303. path->slots[0]++;
  3304. }
  3305. WARN_ON(cur_pos + offset > last_byte);
  3306. if (cur_pos + offset < last_byte) {
  3307. ret = -ENOENT;
  3308. goto out;
  3309. }
  3310. ret = 0;
  3311. out:
  3312. btrfs_free_path(path);
  3313. if (ret) {
  3314. if (exts != *extents)
  3315. kfree(exts);
  3316. } else {
  3317. *extents = exts;
  3318. *nr_extents = nr;
  3319. }
  3320. return ret;
  3321. }
  3322. static int noinline replace_one_extent(struct btrfs_trans_handle *trans,
  3323. struct btrfs_root *root,
  3324. struct btrfs_path *path,
  3325. struct btrfs_key *extent_key,
  3326. struct btrfs_key *leaf_key,
  3327. struct btrfs_ref_path *ref_path,
  3328. struct disk_extent *new_extents,
  3329. int nr_extents)
  3330. {
  3331. struct extent_buffer *leaf;
  3332. struct btrfs_file_extent_item *fi;
  3333. struct inode *inode = NULL;
  3334. struct btrfs_key key;
  3335. u64 lock_start = 0;
  3336. u64 lock_end = 0;
  3337. u64 num_bytes;
  3338. u64 ext_offset;
  3339. u64 first_pos;
  3340. u32 nritems;
  3341. int nr_scaned = 0;
  3342. int extent_locked = 0;
  3343. int extent_type;
  3344. int ret;
  3345. memcpy(&key, leaf_key, sizeof(key));
  3346. first_pos = INT_LIMIT(loff_t) - extent_key->offset;
  3347. if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
  3348. if (key.objectid < ref_path->owner_objectid ||
  3349. (key.objectid == ref_path->owner_objectid &&
  3350. key.type < BTRFS_EXTENT_DATA_KEY)) {
  3351. key.objectid = ref_path->owner_objectid;
  3352. key.type = BTRFS_EXTENT_DATA_KEY;
  3353. key.offset = 0;
  3354. }
  3355. }
  3356. while (1) {
  3357. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  3358. if (ret < 0)
  3359. goto out;
  3360. leaf = path->nodes[0];
  3361. nritems = btrfs_header_nritems(leaf);
  3362. next:
  3363. if (extent_locked && ret > 0) {
  3364. /*
  3365. * the file extent item was modified by someone
  3366. * before the extent got locked.
  3367. */
  3368. unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
  3369. lock_end, GFP_NOFS);
  3370. extent_locked = 0;
  3371. }
  3372. if (path->slots[0] >= nritems) {
  3373. if (++nr_scaned > 2)
  3374. break;
  3375. BUG_ON(extent_locked);
  3376. ret = btrfs_next_leaf(root, path);
  3377. if (ret < 0)
  3378. goto out;
  3379. if (ret > 0)
  3380. break;
  3381. leaf = path->nodes[0];
  3382. nritems = btrfs_header_nritems(leaf);
  3383. }
  3384. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  3385. if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
  3386. if ((key.objectid > ref_path->owner_objectid) ||
  3387. (key.objectid == ref_path->owner_objectid &&
  3388. key.type > BTRFS_EXTENT_DATA_KEY) ||
  3389. (key.offset >= first_pos + extent_key->offset))
  3390. break;
  3391. }
  3392. if (inode && key.objectid != inode->i_ino) {
  3393. BUG_ON(extent_locked);
  3394. btrfs_release_path(root, path);
  3395. mutex_unlock(&inode->i_mutex);
  3396. iput(inode);
  3397. inode = NULL;
  3398. continue;
  3399. }
  3400. if (key.type != BTRFS_EXTENT_DATA_KEY) {
  3401. path->slots[0]++;
  3402. ret = 1;
  3403. goto next;
  3404. }
  3405. fi = btrfs_item_ptr(leaf, path->slots[0],
  3406. struct btrfs_file_extent_item);
  3407. extent_type = btrfs_file_extent_type(leaf, fi);
  3408. if ((extent_type != BTRFS_FILE_EXTENT_REG &&
  3409. extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
  3410. (btrfs_file_extent_disk_bytenr(leaf, fi) !=
  3411. extent_key->objectid)) {
  3412. path->slots[0]++;
  3413. ret = 1;
  3414. goto next;
  3415. }
  3416. num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
  3417. ext_offset = btrfs_file_extent_offset(leaf, fi);
  3418. if (first_pos > key.offset - ext_offset)
  3419. first_pos = key.offset - ext_offset;
  3420. if (!extent_locked) {
  3421. lock_start = key.offset;
  3422. lock_end = lock_start + num_bytes - 1;
  3423. } else {
  3424. if (lock_start > key.offset ||
  3425. lock_end + 1 < key.offset + num_bytes) {
  3426. unlock_extent(&BTRFS_I(inode)->io_tree,
  3427. lock_start, lock_end, GFP_NOFS);
  3428. extent_locked = 0;
  3429. }
  3430. }
  3431. if (!inode) {
  3432. btrfs_release_path(root, path);
  3433. inode = btrfs_iget_locked(root->fs_info->sb,
  3434. key.objectid, root);
  3435. if (inode->i_state & I_NEW) {
  3436. BTRFS_I(inode)->root = root;
  3437. BTRFS_I(inode)->location.objectid =
  3438. key.objectid;
  3439. BTRFS_I(inode)->location.type =
  3440. BTRFS_INODE_ITEM_KEY;
  3441. BTRFS_I(inode)->location.offset = 0;
  3442. btrfs_read_locked_inode(inode);
  3443. unlock_new_inode(inode);
  3444. }
  3445. /*
  3446. * some code call btrfs_commit_transaction while
  3447. * holding the i_mutex, so we can't use mutex_lock
  3448. * here.
  3449. */
  3450. if (is_bad_inode(inode) ||
  3451. !mutex_trylock(&inode->i_mutex)) {
  3452. iput(inode);
  3453. inode = NULL;
  3454. key.offset = (u64)-1;
  3455. goto skip;
  3456. }
  3457. }
  3458. if (!extent_locked) {
  3459. struct btrfs_ordered_extent *ordered;
  3460. btrfs_release_path(root, path);
  3461. lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
  3462. lock_end, GFP_NOFS);
  3463. ordered = btrfs_lookup_first_ordered_extent(inode,
  3464. lock_end);
  3465. if (ordered &&
  3466. ordered->file_offset <= lock_end &&
  3467. ordered->file_offset + ordered->len > lock_start) {
  3468. unlock_extent(&BTRFS_I(inode)->io_tree,
  3469. lock_start, lock_end, GFP_NOFS);
  3470. btrfs_start_ordered_extent(inode, ordered, 1);
  3471. btrfs_put_ordered_extent(ordered);
  3472. key.offset += num_bytes;
  3473. goto skip;
  3474. }
  3475. if (ordered)
  3476. btrfs_put_ordered_extent(ordered);
  3477. extent_locked = 1;
  3478. continue;
  3479. }
  3480. if (nr_extents == 1) {
  3481. /* update extent pointer in place */
  3482. btrfs_set_file_extent_disk_bytenr(leaf, fi,
  3483. new_extents[0].disk_bytenr);
  3484. btrfs_set_file_extent_disk_num_bytes(leaf, fi,
  3485. new_extents[0].disk_num_bytes);
  3486. btrfs_mark_buffer_dirty(leaf);
  3487. btrfs_drop_extent_cache(inode, key.offset,
  3488. key.offset + num_bytes - 1, 0);
  3489. ret = btrfs_inc_extent_ref(trans, root,
  3490. new_extents[0].disk_bytenr,
  3491. new_extents[0].disk_num_bytes,
  3492. leaf->start,
  3493. root->root_key.objectid,
  3494. trans->transid,
  3495. key.objectid);
  3496. BUG_ON(ret);
  3497. ret = btrfs_free_extent(trans, root,
  3498. extent_key->objectid,
  3499. extent_key->offset,
  3500. leaf->start,
  3501. btrfs_header_owner(leaf),
  3502. btrfs_header_generation(leaf),
  3503. key.objectid, 0);
  3504. BUG_ON(ret);
  3505. btrfs_release_path(root, path);
  3506. key.offset += num_bytes;
  3507. } else {
  3508. BUG_ON(1);
  3509. #if 0
  3510. u64 alloc_hint;
  3511. u64 extent_len;
  3512. int i;
  3513. /*
  3514. * drop old extent pointer at first, then insert the
  3515. * new pointers one bye one
  3516. */
  3517. btrfs_release_path(root, path);
  3518. ret = btrfs_drop_extents(trans, root, inode, key.offset,
  3519. key.offset + num_bytes,
  3520. key.offset, &alloc_hint);
  3521. BUG_ON(ret);
  3522. for (i = 0; i < nr_extents; i++) {
  3523. if (ext_offset >= new_extents[i].num_bytes) {
  3524. ext_offset -= new_extents[i].num_bytes;
  3525. continue;
  3526. }
  3527. extent_len = min(new_extents[i].num_bytes -
  3528. ext_offset, num_bytes);
  3529. ret = btrfs_insert_empty_item(trans, root,
  3530. path, &key,
  3531. sizeof(*fi));
  3532. BUG_ON(ret);
  3533. leaf = path->nodes[0];
  3534. fi = btrfs_item_ptr(leaf, path->slots[0],
  3535. struct btrfs_file_extent_item);
  3536. btrfs_set_file_extent_generation(leaf, fi,
  3537. trans->transid);
  3538. btrfs_set_file_extent_type(leaf, fi,
  3539. BTRFS_FILE_EXTENT_REG);
  3540. btrfs_set_file_extent_disk_bytenr(leaf, fi,
  3541. new_extents[i].disk_bytenr);
  3542. btrfs_set_file_extent_disk_num_bytes(leaf, fi,
  3543. new_extents[i].disk_num_bytes);
  3544. btrfs_set_file_extent_ram_bytes(leaf, fi,
  3545. new_extents[i].ram_bytes);
  3546. btrfs_set_file_extent_compression(leaf, fi,
  3547. new_extents[i].compression);
  3548. btrfs_set_file_extent_encryption(leaf, fi,
  3549. new_extents[i].encryption);
  3550. btrfs_set_file_extent_other_encoding(leaf, fi,
  3551. new_extents[i].other_encoding);
  3552. btrfs_set_file_extent_num_bytes(leaf, fi,
  3553. extent_len);
  3554. ext_offset += new_extents[i].offset;
  3555. btrfs_set_file_extent_offset(leaf, fi,
  3556. ext_offset);
  3557. btrfs_mark_buffer_dirty(leaf);
  3558. btrfs_drop_extent_cache(inode, key.offset,
  3559. key.offset + extent_len - 1, 0);
  3560. ret = btrfs_inc_extent_ref(trans, root,
  3561. new_extents[i].disk_bytenr,
  3562. new_extents[i].disk_num_bytes,
  3563. leaf->start,
  3564. root->root_key.objectid,
  3565. trans->transid, key.objectid);
  3566. BUG_ON(ret);
  3567. btrfs_release_path(root, path);
  3568. inode_add_bytes(inode, extent_len);
  3569. ext_offset = 0;
  3570. num_bytes -= extent_len;
  3571. key.offset += extent_len;
  3572. if (num_bytes == 0)
  3573. break;
  3574. }
  3575. BUG_ON(i >= nr_extents);
  3576. #endif
  3577. }
  3578. if (extent_locked) {
  3579. unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
  3580. lock_end, GFP_NOFS);
  3581. extent_locked = 0;
  3582. }
  3583. skip:
  3584. if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
  3585. key.offset >= first_pos + extent_key->offset)
  3586. break;
  3587. cond_resched();
  3588. }
  3589. ret = 0;
  3590. out:
  3591. btrfs_release_path(root, path);
  3592. if (inode) {
  3593. mutex_unlock(&inode->i_mutex);
  3594. if (extent_locked) {
  3595. unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
  3596. lock_end, GFP_NOFS);
  3597. }
  3598. iput(inode);
  3599. }
  3600. return ret;
  3601. }
  3602. int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
  3603. struct btrfs_root *root,
  3604. struct extent_buffer *buf, u64 orig_start)
  3605. {
  3606. int level;
  3607. int ret;
  3608. BUG_ON(btrfs_header_generation(buf) != trans->transid);
  3609. BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
  3610. level = btrfs_header_level(buf);
  3611. if (level == 0) {
  3612. struct btrfs_leaf_ref *ref;
  3613. struct btrfs_leaf_ref *orig_ref;
  3614. orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
  3615. if (!orig_ref)
  3616. return -ENOENT;
  3617. ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
  3618. if (!ref) {
  3619. btrfs_free_leaf_ref(root, orig_ref);
  3620. return -ENOMEM;
  3621. }
  3622. ref->nritems = orig_ref->nritems;
  3623. memcpy(ref->extents, orig_ref->extents,
  3624. sizeof(ref->extents[0]) * ref->nritems);
  3625. btrfs_free_leaf_ref(root, orig_ref);
  3626. ref->root_gen = trans->transid;
  3627. ref->bytenr = buf->start;
  3628. ref->owner = btrfs_header_owner(buf);
  3629. ref->generation = btrfs_header_generation(buf);
  3630. ret = btrfs_add_leaf_ref(root, ref, 0);
  3631. WARN_ON(ret);
  3632. btrfs_free_leaf_ref(root, ref);
  3633. }
  3634. return 0;
  3635. }
  3636. static int noinline invalidate_extent_cache(struct btrfs_root *root,
  3637. struct extent_buffer *leaf,
  3638. struct btrfs_block_group_cache *group,
  3639. struct btrfs_root *target_root)
  3640. {
  3641. struct btrfs_key key;
  3642. struct inode *inode = NULL;
  3643. struct btrfs_file_extent_item *fi;
  3644. u64 num_bytes;
  3645. u64 skip_objectid = 0;
  3646. u32 nritems;
  3647. u32 i;
  3648. nritems = btrfs_header_nritems(leaf);
  3649. for (i = 0; i < nritems; i++) {
  3650. btrfs_item_key_to_cpu(leaf, &key, i);
  3651. if (key.objectid == skip_objectid ||
  3652. key.type != BTRFS_EXTENT_DATA_KEY)
  3653. continue;
  3654. fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
  3655. if (btrfs_file_extent_type(leaf, fi) ==
  3656. BTRFS_FILE_EXTENT_INLINE)
  3657. continue;
  3658. if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
  3659. continue;
  3660. if (!inode || inode->i_ino != key.objectid) {
  3661. iput(inode);
  3662. inode = btrfs_ilookup(target_root->fs_info->sb,
  3663. key.objectid, target_root, 1);
  3664. }
  3665. if (!inode) {
  3666. skip_objectid = key.objectid;
  3667. continue;
  3668. }
  3669. num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
  3670. lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
  3671. key.offset + num_bytes - 1, GFP_NOFS);
  3672. btrfs_drop_extent_cache(inode, key.offset,
  3673. key.offset + num_bytes - 1, 1);
  3674. unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
  3675. key.offset + num_bytes - 1, GFP_NOFS);
  3676. cond_resched();
  3677. }
  3678. iput(inode);
  3679. return 0;
  3680. }
  3681. static int noinline replace_extents_in_leaf(struct btrfs_trans_handle *trans,
  3682. struct btrfs_root *root,
  3683. struct extent_buffer *leaf,
  3684. struct btrfs_block_group_cache *group,
  3685. struct inode *reloc_inode)
  3686. {
  3687. struct btrfs_key key;
  3688. struct btrfs_key extent_key;
  3689. struct btrfs_file_extent_item *fi;
  3690. struct btrfs_leaf_ref *ref;
  3691. struct disk_extent *new_extent;
  3692. u64 bytenr;
  3693. u64 num_bytes;
  3694. u32 nritems;
  3695. u32 i;
  3696. int ext_index;
  3697. int nr_extent;
  3698. int ret;
  3699. new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
  3700. BUG_ON(!new_extent);
  3701. ref = btrfs_lookup_leaf_ref(root, leaf->start);
  3702. BUG_ON(!ref);
  3703. ext_index = -1;
  3704. nritems = btrfs_header_nritems(leaf);
  3705. for (i = 0; i < nritems; i++) {
  3706. btrfs_item_key_to_cpu(leaf, &key, i);
  3707. if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
  3708. continue;
  3709. fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
  3710. if (btrfs_file_extent_type(leaf, fi) ==
  3711. BTRFS_FILE_EXTENT_INLINE)
  3712. continue;
  3713. bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
  3714. num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
  3715. if (bytenr == 0)
  3716. continue;
  3717. ext_index++;
  3718. if (bytenr >= group->key.objectid + group->key.offset ||
  3719. bytenr + num_bytes <= group->key.objectid)
  3720. continue;
  3721. extent_key.objectid = bytenr;
  3722. extent_key.offset = num_bytes;
  3723. extent_key.type = BTRFS_EXTENT_ITEM_KEY;
  3724. nr_extent = 1;
  3725. ret = get_new_locations(reloc_inode, &extent_key,
  3726. group->key.objectid, 1,
  3727. &new_extent, &nr_extent);
  3728. if (ret > 0)
  3729. continue;
  3730. BUG_ON(ret < 0);
  3731. BUG_ON(ref->extents[ext_index].bytenr != bytenr);
  3732. BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
  3733. ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
  3734. ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
  3735. btrfs_set_file_extent_disk_bytenr(leaf, fi,
  3736. new_extent->disk_bytenr);
  3737. btrfs_set_file_extent_disk_num_bytes(leaf, fi,
  3738. new_extent->disk_num_bytes);
  3739. btrfs_mark_buffer_dirty(leaf);
  3740. ret = btrfs_inc_extent_ref(trans, root,
  3741. new_extent->disk_bytenr,
  3742. new_extent->disk_num_bytes,
  3743. leaf->start,
  3744. root->root_key.objectid,
  3745. trans->transid, key.objectid);
  3746. BUG_ON(ret);
  3747. ret = btrfs_free_extent(trans, root,
  3748. bytenr, num_bytes, leaf->start,
  3749. btrfs_header_owner(leaf),
  3750. btrfs_header_generation(leaf),
  3751. key.objectid, 0);
  3752. BUG_ON(ret);
  3753. cond_resched();
  3754. }
  3755. kfree(new_extent);
  3756. BUG_ON(ext_index + 1 != ref->nritems);
  3757. btrfs_free_leaf_ref(root, ref);
  3758. return 0;
  3759. }
  3760. int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
  3761. struct btrfs_root *root)
  3762. {
  3763. struct btrfs_root *reloc_root;
  3764. int ret;
  3765. if (root->reloc_root) {
  3766. reloc_root = root->reloc_root;
  3767. root->reloc_root = NULL;
  3768. list_add(&reloc_root->dead_list,
  3769. &root->fs_info->dead_reloc_roots);
  3770. btrfs_set_root_bytenr(&reloc_root->root_item,
  3771. reloc_root->node->start);
  3772. btrfs_set_root_level(&root->root_item,
  3773. btrfs_header_level(reloc_root->node));
  3774. memset(&reloc_root->root_item.drop_progress, 0,
  3775. sizeof(struct btrfs_disk_key));
  3776. reloc_root->root_item.drop_level = 0;
  3777. ret = btrfs_update_root(trans, root->fs_info->tree_root,
  3778. &reloc_root->root_key,
  3779. &reloc_root->root_item);
  3780. BUG_ON(ret);
  3781. }
  3782. return 0;
  3783. }
  3784. int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
  3785. {
  3786. struct btrfs_trans_handle *trans;
  3787. struct btrfs_root *reloc_root;
  3788. struct btrfs_root *prev_root = NULL;
  3789. struct list_head dead_roots;
  3790. int ret;
  3791. unsigned long nr;
  3792. INIT_LIST_HEAD(&dead_roots);
  3793. list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
  3794. while (!list_empty(&dead_roots)) {
  3795. reloc_root = list_entry(dead_roots.prev,
  3796. struct btrfs_root, dead_list);
  3797. list_del_init(&reloc_root->dead_list);
  3798. BUG_ON(reloc_root->commit_root != NULL);
  3799. while (1) {
  3800. trans = btrfs_join_transaction(root, 1);
  3801. BUG_ON(!trans);
  3802. mutex_lock(&root->fs_info->drop_mutex);
  3803. ret = btrfs_drop_snapshot(trans, reloc_root);
  3804. if (ret != -EAGAIN)
  3805. break;
  3806. mutex_unlock(&root->fs_info->drop_mutex);
  3807. nr = trans->blocks_used;
  3808. ret = btrfs_end_transaction(trans, root);
  3809. BUG_ON(ret);
  3810. btrfs_btree_balance_dirty(root, nr);
  3811. }
  3812. free_extent_buffer(reloc_root->node);
  3813. ret = btrfs_del_root(trans, root->fs_info->tree_root,
  3814. &reloc_root->root_key);
  3815. BUG_ON(ret);
  3816. mutex_unlock(&root->fs_info->drop_mutex);
  3817. nr = trans->blocks_used;
  3818. ret = btrfs_end_transaction(trans, root);
  3819. BUG_ON(ret);
  3820. btrfs_btree_balance_dirty(root, nr);
  3821. kfree(prev_root);
  3822. prev_root = reloc_root;
  3823. }
  3824. if (prev_root) {
  3825. btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
  3826. kfree(prev_root);
  3827. }
  3828. return 0;
  3829. }
  3830. int btrfs_add_dead_reloc_root(struct btrfs_root *root)
  3831. {
  3832. list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
  3833. return 0;
  3834. }
  3835. int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
  3836. {
  3837. struct btrfs_root *reloc_root;
  3838. struct btrfs_trans_handle *trans;
  3839. struct btrfs_key location;
  3840. int found;
  3841. int ret;
  3842. mutex_lock(&root->fs_info->tree_reloc_mutex);
  3843. ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
  3844. BUG_ON(ret);
  3845. found = !list_empty(&root->fs_info->dead_reloc_roots);
  3846. mutex_unlock(&root->fs_info->tree_reloc_mutex);
  3847. if (found) {
  3848. trans = btrfs_start_transaction(root, 1);
  3849. BUG_ON(!trans);
  3850. ret = btrfs_commit_transaction(trans, root);
  3851. BUG_ON(ret);
  3852. }
  3853. location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
  3854. location.offset = (u64)-1;
  3855. location.type = BTRFS_ROOT_ITEM_KEY;
  3856. reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
  3857. BUG_ON(!reloc_root);
  3858. btrfs_orphan_cleanup(reloc_root);
  3859. return 0;
  3860. }
  3861. static int noinline init_reloc_tree(struct btrfs_trans_handle *trans,
  3862. struct btrfs_root *root)
  3863. {
  3864. struct btrfs_root *reloc_root;
  3865. struct extent_buffer *eb;
  3866. struct btrfs_root_item *root_item;
  3867. struct btrfs_key root_key;
  3868. int ret;
  3869. BUG_ON(!root->ref_cows);
  3870. if (root->reloc_root)
  3871. return 0;
  3872. root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
  3873. BUG_ON(!root_item);
  3874. ret = btrfs_copy_root(trans, root, root->commit_root,
  3875. &eb, BTRFS_TREE_RELOC_OBJECTID);
  3876. BUG_ON(ret);
  3877. root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
  3878. root_key.offset = root->root_key.objectid;
  3879. root_key.type = BTRFS_ROOT_ITEM_KEY;
  3880. memcpy(root_item, &root->root_item, sizeof(root_item));
  3881. btrfs_set_root_refs(root_item, 0);
  3882. btrfs_set_root_bytenr(root_item, eb->start);
  3883. btrfs_set_root_level(root_item, btrfs_header_level(eb));
  3884. btrfs_set_root_generation(root_item, trans->transid);
  3885. btrfs_tree_unlock(eb);
  3886. free_extent_buffer(eb);
  3887. ret = btrfs_insert_root(trans, root->fs_info->tree_root,
  3888. &root_key, root_item);
  3889. BUG_ON(ret);
  3890. kfree(root_item);
  3891. reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
  3892. &root_key);
  3893. BUG_ON(!reloc_root);
  3894. reloc_root->last_trans = trans->transid;
  3895. reloc_root->commit_root = NULL;
  3896. reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
  3897. root->reloc_root = reloc_root;
  3898. return 0;
  3899. }
  3900. /*
  3901. * Core function of space balance.
  3902. *
  3903. * The idea is using reloc trees to relocate tree blocks in reference
  3904. * counted roots. There is one reloc tree for each subvol, and all
  3905. * reloc trees share same root key objectid. Reloc trees are snapshots
  3906. * of the latest committed roots of subvols (root->commit_root).
  3907. *
  3908. * To relocate a tree block referenced by a subvol, there are two steps.
  3909. * COW the block through subvol's reloc tree, then update block pointer
  3910. * in the subvol to point to the new block. Since all reloc trees share
  3911. * same root key objectid, doing special handing for tree blocks owned
  3912. * by them is easy. Once a tree block has been COWed in one reloc tree,
  3913. * we can use the resulting new block directly when the same block is
  3914. * required to COW again through other reloc trees. By this way, relocated
  3915. * tree blocks are shared between reloc trees, so they are also shared
  3916. * between subvols.
  3917. */
  3918. static int noinline relocate_one_path(struct btrfs_trans_handle *trans,
  3919. struct btrfs_root *root,
  3920. struct btrfs_path *path,
  3921. struct btrfs_key *first_key,
  3922. struct btrfs_ref_path *ref_path,
  3923. struct btrfs_block_group_cache *group,
  3924. struct inode *reloc_inode)
  3925. {
  3926. struct btrfs_root *reloc_root;
  3927. struct extent_buffer *eb = NULL;
  3928. struct btrfs_key *keys;
  3929. u64 *nodes;
  3930. int level;
  3931. int shared_level;
  3932. int lowest_level = 0;
  3933. int ret;
  3934. if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
  3935. lowest_level = ref_path->owner_objectid;
  3936. if (!root->ref_cows) {
  3937. path->lowest_level = lowest_level;
  3938. ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
  3939. BUG_ON(ret < 0);
  3940. path->lowest_level = 0;
  3941. btrfs_release_path(root, path);
  3942. return 0;
  3943. }
  3944. mutex_lock(&root->fs_info->tree_reloc_mutex);
  3945. ret = init_reloc_tree(trans, root);
  3946. BUG_ON(ret);
  3947. reloc_root = root->reloc_root;
  3948. shared_level = ref_path->shared_level;
  3949. ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
  3950. keys = ref_path->node_keys;
  3951. nodes = ref_path->new_nodes;
  3952. memset(&keys[shared_level + 1], 0,
  3953. sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
  3954. memset(&nodes[shared_level + 1], 0,
  3955. sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
  3956. if (nodes[lowest_level] == 0) {
  3957. path->lowest_level = lowest_level;
  3958. ret = btrfs_search_slot(trans, reloc_root, first_key, path,
  3959. 0, 1);
  3960. BUG_ON(ret);
  3961. for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
  3962. eb = path->nodes[level];
  3963. if (!eb || eb == reloc_root->node)
  3964. break;
  3965. nodes[level] = eb->start;
  3966. if (level == 0)
  3967. btrfs_item_key_to_cpu(eb, &keys[level], 0);
  3968. else
  3969. btrfs_node_key_to_cpu(eb, &keys[level], 0);
  3970. }
  3971. if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
  3972. eb = path->nodes[0];
  3973. ret = replace_extents_in_leaf(trans, reloc_root, eb,
  3974. group, reloc_inode);
  3975. BUG_ON(ret);
  3976. }
  3977. btrfs_release_path(reloc_root, path);
  3978. } else {
  3979. ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
  3980. lowest_level);
  3981. BUG_ON(ret);
  3982. }
  3983. /*
  3984. * replace tree blocks in the fs tree with tree blocks in
  3985. * the reloc tree.
  3986. */
  3987. ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
  3988. BUG_ON(ret < 0);
  3989. if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
  3990. ret = btrfs_search_slot(trans, reloc_root, first_key, path,
  3991. 0, 0);
  3992. BUG_ON(ret);
  3993. extent_buffer_get(path->nodes[0]);
  3994. eb = path->nodes[0];
  3995. btrfs_release_path(reloc_root, path);
  3996. ret = invalidate_extent_cache(reloc_root, eb, group, root);
  3997. BUG_ON(ret);
  3998. free_extent_buffer(eb);
  3999. }
  4000. mutex_unlock(&root->fs_info->tree_reloc_mutex);
  4001. path->lowest_level = 0;
  4002. return 0;
  4003. }
  4004. static int noinline relocate_tree_block(struct btrfs_trans_handle *trans,
  4005. struct btrfs_root *root,
  4006. struct btrfs_path *path,
  4007. struct btrfs_key *first_key,
  4008. struct btrfs_ref_path *ref_path)
  4009. {
  4010. int ret;
  4011. ret = relocate_one_path(trans, root, path, first_key,
  4012. ref_path, NULL, NULL);
  4013. BUG_ON(ret);
  4014. if (root == root->fs_info->extent_root)
  4015. btrfs_extent_post_op(trans, root);
  4016. return 0;
  4017. }
  4018. static int noinline del_extent_zero(struct btrfs_trans_handle *trans,
  4019. struct btrfs_root *extent_root,
  4020. struct btrfs_path *path,
  4021. struct btrfs_key *extent_key)
  4022. {
  4023. int ret;
  4024. ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
  4025. if (ret)
  4026. goto out;
  4027. ret = btrfs_del_item(trans, extent_root, path);
  4028. out:
  4029. btrfs_release_path(extent_root, path);
  4030. return ret;
  4031. }
  4032. static struct btrfs_root noinline *read_ref_root(struct btrfs_fs_info *fs_info,
  4033. struct btrfs_ref_path *ref_path)
  4034. {
  4035. struct btrfs_key root_key;
  4036. root_key.objectid = ref_path->root_objectid;
  4037. root_key.type = BTRFS_ROOT_ITEM_KEY;
  4038. if (is_cowonly_root(ref_path->root_objectid))
  4039. root_key.offset = 0;
  4040. else
  4041. root_key.offset = (u64)-1;
  4042. return btrfs_read_fs_root_no_name(fs_info, &root_key);
  4043. }
  4044. static int noinline relocate_one_extent(struct btrfs_root *extent_root,
  4045. struct btrfs_path *path,
  4046. struct btrfs_key *extent_key,
  4047. struct btrfs_block_group_cache *group,
  4048. struct inode *reloc_inode, int pass)
  4049. {
  4050. struct btrfs_trans_handle *trans;
  4051. struct btrfs_root *found_root;
  4052. struct btrfs_ref_path *ref_path = NULL;
  4053. struct disk_extent *new_extents = NULL;
  4054. int nr_extents = 0;
  4055. int loops;
  4056. int ret;
  4057. int level;
  4058. struct btrfs_key first_key;
  4059. u64 prev_block = 0;
  4060. trans = btrfs_start_transaction(extent_root, 1);
  4061. BUG_ON(!trans);
  4062. if (extent_key->objectid == 0) {
  4063. ret = del_extent_zero(trans, extent_root, path, extent_key);
  4064. goto out;
  4065. }
  4066. ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
  4067. if (!ref_path) {
  4068. ret = -ENOMEM;
  4069. goto out;
  4070. }
  4071. for (loops = 0; ; loops++) {
  4072. if (loops == 0) {
  4073. ret = btrfs_first_ref_path(trans, extent_root, ref_path,
  4074. extent_key->objectid);
  4075. } else {
  4076. ret = btrfs_next_ref_path(trans, extent_root, ref_path);
  4077. }
  4078. if (ret < 0)
  4079. goto out;
  4080. if (ret > 0)
  4081. break;
  4082. if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
  4083. ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
  4084. continue;
  4085. found_root = read_ref_root(extent_root->fs_info, ref_path);
  4086. BUG_ON(!found_root);
  4087. /*
  4088. * for reference counted tree, only process reference paths
  4089. * rooted at the latest committed root.
  4090. */
  4091. if (found_root->ref_cows &&
  4092. ref_path->root_generation != found_root->root_key.offset)
  4093. continue;
  4094. if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
  4095. if (pass == 0) {
  4096. /*
  4097. * copy data extents to new locations
  4098. */
  4099. u64 group_start = group->key.objectid;
  4100. ret = relocate_data_extent(reloc_inode,
  4101. extent_key,
  4102. group_start);
  4103. if (ret < 0)
  4104. goto out;
  4105. break;
  4106. }
  4107. level = 0;
  4108. } else {
  4109. level = ref_path->owner_objectid;
  4110. }
  4111. if (prev_block != ref_path->nodes[level]) {
  4112. struct extent_buffer *eb;
  4113. u64 block_start = ref_path->nodes[level];
  4114. u64 block_size = btrfs_level_size(found_root, level);
  4115. eb = read_tree_block(found_root, block_start,
  4116. block_size, 0);
  4117. btrfs_tree_lock(eb);
  4118. BUG_ON(level != btrfs_header_level(eb));
  4119. if (level == 0)
  4120. btrfs_item_key_to_cpu(eb, &first_key, 0);
  4121. else
  4122. btrfs_node_key_to_cpu(eb, &first_key, 0);
  4123. btrfs_tree_unlock(eb);
  4124. free_extent_buffer(eb);
  4125. prev_block = block_start;
  4126. }
  4127. if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID &&
  4128. pass >= 2) {
  4129. /*
  4130. * use fallback method to process the remaining
  4131. * references.
  4132. */
  4133. if (!new_extents) {
  4134. u64 group_start = group->key.objectid;
  4135. new_extents = kmalloc(sizeof(*new_extents),
  4136. GFP_NOFS);
  4137. nr_extents = 1;
  4138. ret = get_new_locations(reloc_inode,
  4139. extent_key,
  4140. group_start, 1,
  4141. &new_extents,
  4142. &nr_extents);
  4143. if (ret)
  4144. goto out;
  4145. }
  4146. btrfs_record_root_in_trans(found_root);
  4147. ret = replace_one_extent(trans, found_root,
  4148. path, extent_key,
  4149. &first_key, ref_path,
  4150. new_extents, nr_extents);
  4151. if (ret < 0)
  4152. goto out;
  4153. continue;
  4154. }
  4155. btrfs_record_root_in_trans(found_root);
  4156. if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
  4157. ret = relocate_tree_block(trans, found_root, path,
  4158. &first_key, ref_path);
  4159. } else {
  4160. /*
  4161. * try to update data extent references while
  4162. * keeping metadata shared between snapshots.
  4163. */
  4164. ret = relocate_one_path(trans, found_root, path,
  4165. &first_key, ref_path,
  4166. group, reloc_inode);
  4167. }
  4168. if (ret < 0)
  4169. goto out;
  4170. }
  4171. ret = 0;
  4172. out:
  4173. btrfs_end_transaction(trans, extent_root);
  4174. kfree(new_extents);
  4175. kfree(ref_path);
  4176. return ret;
  4177. }
  4178. static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
  4179. {
  4180. u64 num_devices;
  4181. u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
  4182. BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
  4183. num_devices = root->fs_info->fs_devices->num_devices;
  4184. if (num_devices == 1) {
  4185. stripped |= BTRFS_BLOCK_GROUP_DUP;
  4186. stripped = flags & ~stripped;
  4187. /* turn raid0 into single device chunks */
  4188. if (flags & BTRFS_BLOCK_GROUP_RAID0)
  4189. return stripped;
  4190. /* turn mirroring into duplication */
  4191. if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
  4192. BTRFS_BLOCK_GROUP_RAID10))
  4193. return stripped | BTRFS_BLOCK_GROUP_DUP;
  4194. return flags;
  4195. } else {
  4196. /* they already had raid on here, just return */
  4197. if (flags & stripped)
  4198. return flags;
  4199. stripped |= BTRFS_BLOCK_GROUP_DUP;
  4200. stripped = flags & ~stripped;
  4201. /* switch duplicated blocks with raid1 */
  4202. if (flags & BTRFS_BLOCK_GROUP_DUP)
  4203. return stripped | BTRFS_BLOCK_GROUP_RAID1;
  4204. /* turn single device chunks into raid0 */
  4205. return stripped | BTRFS_BLOCK_GROUP_RAID0;
  4206. }
  4207. return flags;
  4208. }
  4209. int __alloc_chunk_for_shrink(struct btrfs_root *root,
  4210. struct btrfs_block_group_cache *shrink_block_group,
  4211. int force)
  4212. {
  4213. struct btrfs_trans_handle *trans;
  4214. u64 new_alloc_flags;
  4215. u64 calc;
  4216. spin_lock(&shrink_block_group->lock);
  4217. if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
  4218. spin_unlock(&shrink_block_group->lock);
  4219. trans = btrfs_start_transaction(root, 1);
  4220. spin_lock(&shrink_block_group->lock);
  4221. new_alloc_flags = update_block_group_flags(root,
  4222. shrink_block_group->flags);
  4223. if (new_alloc_flags != shrink_block_group->flags) {
  4224. calc =
  4225. btrfs_block_group_used(&shrink_block_group->item);
  4226. } else {
  4227. calc = shrink_block_group->key.offset;
  4228. }
  4229. spin_unlock(&shrink_block_group->lock);
  4230. do_chunk_alloc(trans, root->fs_info->extent_root,
  4231. calc + 2 * 1024 * 1024, new_alloc_flags, force);
  4232. btrfs_end_transaction(trans, root);
  4233. } else
  4234. spin_unlock(&shrink_block_group->lock);
  4235. return 0;
  4236. }
  4237. static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
  4238. struct btrfs_root *root,
  4239. u64 objectid, u64 size)
  4240. {
  4241. struct btrfs_path *path;
  4242. struct btrfs_inode_item *item;
  4243. struct extent_buffer *leaf;
  4244. int ret;
  4245. path = btrfs_alloc_path();
  4246. if (!path)
  4247. return -ENOMEM;
  4248. ret = btrfs_insert_empty_inode(trans, root, path, objectid);
  4249. if (ret)
  4250. goto out;
  4251. leaf = path->nodes[0];
  4252. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
  4253. memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
  4254. btrfs_set_inode_generation(leaf, item, 1);
  4255. btrfs_set_inode_size(leaf, item, size);
  4256. btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
  4257. btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NODATASUM |
  4258. BTRFS_INODE_NOCOMPRESS);
  4259. btrfs_mark_buffer_dirty(leaf);
  4260. btrfs_release_path(root, path);
  4261. out:
  4262. btrfs_free_path(path);
  4263. return ret;
  4264. }
  4265. static struct inode noinline *create_reloc_inode(struct btrfs_fs_info *fs_info,
  4266. struct btrfs_block_group_cache *group)
  4267. {
  4268. struct inode *inode = NULL;
  4269. struct btrfs_trans_handle *trans;
  4270. struct btrfs_root *root;
  4271. struct btrfs_key root_key;
  4272. u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
  4273. int err = 0;
  4274. root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
  4275. root_key.type = BTRFS_ROOT_ITEM_KEY;
  4276. root_key.offset = (u64)-1;
  4277. root = btrfs_read_fs_root_no_name(fs_info, &root_key);
  4278. if (IS_ERR(root))
  4279. return ERR_CAST(root);
  4280. trans = btrfs_start_transaction(root, 1);
  4281. BUG_ON(!trans);
  4282. err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
  4283. if (err)
  4284. goto out;
  4285. err = __insert_orphan_inode(trans, root, objectid, group->key.offset);
  4286. BUG_ON(err);
  4287. err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0,
  4288. group->key.offset, 0, group->key.offset,
  4289. 0, 0, 0);
  4290. BUG_ON(err);
  4291. inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
  4292. if (inode->i_state & I_NEW) {
  4293. BTRFS_I(inode)->root = root;
  4294. BTRFS_I(inode)->location.objectid = objectid;
  4295. BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
  4296. BTRFS_I(inode)->location.offset = 0;
  4297. btrfs_read_locked_inode(inode);
  4298. unlock_new_inode(inode);
  4299. BUG_ON(is_bad_inode(inode));
  4300. } else {
  4301. BUG_ON(1);
  4302. }
  4303. err = btrfs_orphan_add(trans, inode);
  4304. out:
  4305. btrfs_end_transaction(trans, root);
  4306. if (err) {
  4307. if (inode)
  4308. iput(inode);
  4309. inode = ERR_PTR(err);
  4310. }
  4311. return inode;
  4312. }
  4313. int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
  4314. {
  4315. struct btrfs_trans_handle *trans;
  4316. struct btrfs_path *path;
  4317. struct btrfs_fs_info *info = root->fs_info;
  4318. struct extent_buffer *leaf;
  4319. struct inode *reloc_inode;
  4320. struct btrfs_block_group_cache *block_group;
  4321. struct btrfs_key key;
  4322. u64 skipped;
  4323. u64 cur_byte;
  4324. u64 total_found;
  4325. u32 nritems;
  4326. int ret;
  4327. int progress;
  4328. int pass = 0;
  4329. root = root->fs_info->extent_root;
  4330. block_group = btrfs_lookup_block_group(info, group_start);
  4331. BUG_ON(!block_group);
  4332. printk("btrfs relocating block group %llu flags %llu\n",
  4333. (unsigned long long)block_group->key.objectid,
  4334. (unsigned long long)block_group->flags);
  4335. path = btrfs_alloc_path();
  4336. BUG_ON(!path);
  4337. reloc_inode = create_reloc_inode(info, block_group);
  4338. BUG_ON(IS_ERR(reloc_inode));
  4339. __alloc_chunk_for_shrink(root, block_group, 1);
  4340. block_group->ro = 1;
  4341. block_group->space_info->total_bytes -= block_group->key.offset;
  4342. btrfs_start_delalloc_inodes(info->tree_root);
  4343. btrfs_wait_ordered_extents(info->tree_root, 0);
  4344. again:
  4345. skipped = 0;
  4346. total_found = 0;
  4347. progress = 0;
  4348. key.objectid = block_group->key.objectid;
  4349. key.offset = 0;
  4350. key.type = 0;
  4351. cur_byte = key.objectid;
  4352. trans = btrfs_start_transaction(info->tree_root, 1);
  4353. btrfs_commit_transaction(trans, info->tree_root);
  4354. mutex_lock(&root->fs_info->cleaner_mutex);
  4355. btrfs_clean_old_snapshots(info->tree_root);
  4356. btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
  4357. mutex_unlock(&root->fs_info->cleaner_mutex);
  4358. while(1) {
  4359. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4360. if (ret < 0)
  4361. goto out;
  4362. next:
  4363. leaf = path->nodes[0];
  4364. nritems = btrfs_header_nritems(leaf);
  4365. if (path->slots[0] >= nritems) {
  4366. ret = btrfs_next_leaf(root, path);
  4367. if (ret < 0)
  4368. goto out;
  4369. if (ret == 1) {
  4370. ret = 0;
  4371. break;
  4372. }
  4373. leaf = path->nodes[0];
  4374. nritems = btrfs_header_nritems(leaf);
  4375. }
  4376. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  4377. if (key.objectid >= block_group->key.objectid +
  4378. block_group->key.offset)
  4379. break;
  4380. if (progress && need_resched()) {
  4381. btrfs_release_path(root, path);
  4382. cond_resched();
  4383. progress = 0;
  4384. continue;
  4385. }
  4386. progress = 1;
  4387. if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY ||
  4388. key.objectid + key.offset <= cur_byte) {
  4389. path->slots[0]++;
  4390. goto next;
  4391. }
  4392. total_found++;
  4393. cur_byte = key.objectid + key.offset;
  4394. btrfs_release_path(root, path);
  4395. __alloc_chunk_for_shrink(root, block_group, 0);
  4396. ret = relocate_one_extent(root, path, &key, block_group,
  4397. reloc_inode, pass);
  4398. BUG_ON(ret < 0);
  4399. if (ret > 0)
  4400. skipped++;
  4401. key.objectid = cur_byte;
  4402. key.type = 0;
  4403. key.offset = 0;
  4404. }
  4405. btrfs_release_path(root, path);
  4406. if (pass == 0) {
  4407. btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1);
  4408. invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1);
  4409. WARN_ON(reloc_inode->i_mapping->nrpages);
  4410. }
  4411. if (total_found > 0) {
  4412. printk("btrfs found %llu extents in pass %d\n",
  4413. (unsigned long long)total_found, pass);
  4414. pass++;
  4415. if (total_found == skipped && pass > 2) {
  4416. iput(reloc_inode);
  4417. reloc_inode = create_reloc_inode(info, block_group);
  4418. pass = 0;
  4419. }
  4420. goto again;
  4421. }
  4422. /* delete reloc_inode */
  4423. iput(reloc_inode);
  4424. /* unpin extents in this range */
  4425. trans = btrfs_start_transaction(info->tree_root, 1);
  4426. btrfs_commit_transaction(trans, info->tree_root);
  4427. spin_lock(&block_group->lock);
  4428. WARN_ON(block_group->pinned > 0);
  4429. WARN_ON(block_group->reserved > 0);
  4430. WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
  4431. spin_unlock(&block_group->lock);
  4432. ret = 0;
  4433. out:
  4434. btrfs_free_path(path);
  4435. return ret;
  4436. }
  4437. int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
  4438. struct btrfs_key *key)
  4439. {
  4440. int ret = 0;
  4441. struct btrfs_key found_key;
  4442. struct extent_buffer *leaf;
  4443. int slot;
  4444. ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
  4445. if (ret < 0)
  4446. goto out;
  4447. while(1) {
  4448. slot = path->slots[0];
  4449. leaf = path->nodes[0];
  4450. if (slot >= btrfs_header_nritems(leaf)) {
  4451. ret = btrfs_next_leaf(root, path);
  4452. if (ret == 0)
  4453. continue;
  4454. if (ret < 0)
  4455. goto out;
  4456. break;
  4457. }
  4458. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  4459. if (found_key.objectid >= key->objectid &&
  4460. found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
  4461. ret = 0;
  4462. goto out;
  4463. }
  4464. path->slots[0]++;
  4465. }
  4466. ret = -ENOENT;
  4467. out:
  4468. return ret;
  4469. }
  4470. int btrfs_free_block_groups(struct btrfs_fs_info *info)
  4471. {
  4472. struct btrfs_block_group_cache *block_group;
  4473. struct rb_node *n;
  4474. spin_lock(&info->block_group_cache_lock);
  4475. while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
  4476. block_group = rb_entry(n, struct btrfs_block_group_cache,
  4477. cache_node);
  4478. rb_erase(&block_group->cache_node,
  4479. &info->block_group_cache_tree);
  4480. spin_unlock(&info->block_group_cache_lock);
  4481. btrfs_remove_free_space_cache(block_group);
  4482. down_write(&block_group->space_info->groups_sem);
  4483. list_del(&block_group->list);
  4484. up_write(&block_group->space_info->groups_sem);
  4485. kfree(block_group);
  4486. spin_lock(&info->block_group_cache_lock);
  4487. }
  4488. spin_unlock(&info->block_group_cache_lock);
  4489. return 0;
  4490. }
  4491. int btrfs_read_block_groups(struct btrfs_root *root)
  4492. {
  4493. struct btrfs_path *path;
  4494. int ret;
  4495. struct btrfs_block_group_cache *cache;
  4496. struct btrfs_fs_info *info = root->fs_info;
  4497. struct btrfs_space_info *space_info;
  4498. struct btrfs_key key;
  4499. struct btrfs_key found_key;
  4500. struct extent_buffer *leaf;
  4501. root = info->extent_root;
  4502. key.objectid = 0;
  4503. key.offset = 0;
  4504. btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
  4505. path = btrfs_alloc_path();
  4506. if (!path)
  4507. return -ENOMEM;
  4508. while(1) {
  4509. ret = find_first_block_group(root, path, &key);
  4510. if (ret > 0) {
  4511. ret = 0;
  4512. goto error;
  4513. }
  4514. if (ret != 0)
  4515. goto error;
  4516. leaf = path->nodes[0];
  4517. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  4518. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  4519. if (!cache) {
  4520. ret = -ENOMEM;
  4521. break;
  4522. }
  4523. spin_lock_init(&cache->lock);
  4524. mutex_init(&cache->alloc_mutex);
  4525. INIT_LIST_HEAD(&cache->list);
  4526. read_extent_buffer(leaf, &cache->item,
  4527. btrfs_item_ptr_offset(leaf, path->slots[0]),
  4528. sizeof(cache->item));
  4529. memcpy(&cache->key, &found_key, sizeof(found_key));
  4530. key.objectid = found_key.objectid + found_key.offset;
  4531. btrfs_release_path(root, path);
  4532. cache->flags = btrfs_block_group_flags(&cache->item);
  4533. ret = update_space_info(info, cache->flags, found_key.offset,
  4534. btrfs_block_group_used(&cache->item),
  4535. &space_info);
  4536. BUG_ON(ret);
  4537. cache->space_info = space_info;
  4538. down_write(&space_info->groups_sem);
  4539. list_add_tail(&cache->list, &space_info->block_groups);
  4540. up_write(&space_info->groups_sem);
  4541. ret = btrfs_add_block_group_cache(root->fs_info, cache);
  4542. BUG_ON(ret);
  4543. set_avail_alloc_bits(root->fs_info, cache->flags);
  4544. }
  4545. ret = 0;
  4546. error:
  4547. btrfs_free_path(path);
  4548. return ret;
  4549. }
  4550. int btrfs_make_block_group(struct btrfs_trans_handle *trans,
  4551. struct btrfs_root *root, u64 bytes_used,
  4552. u64 type, u64 chunk_objectid, u64 chunk_offset,
  4553. u64 size)
  4554. {
  4555. int ret;
  4556. struct btrfs_root *extent_root;
  4557. struct btrfs_block_group_cache *cache;
  4558. extent_root = root->fs_info->extent_root;
  4559. root->fs_info->last_trans_new_blockgroup = trans->transid;
  4560. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  4561. if (!cache)
  4562. return -ENOMEM;
  4563. cache->key.objectid = chunk_offset;
  4564. cache->key.offset = size;
  4565. spin_lock_init(&cache->lock);
  4566. mutex_init(&cache->alloc_mutex);
  4567. INIT_LIST_HEAD(&cache->list);
  4568. btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
  4569. btrfs_set_block_group_used(&cache->item, bytes_used);
  4570. btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
  4571. cache->flags = type;
  4572. btrfs_set_block_group_flags(&cache->item, type);
  4573. ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
  4574. &cache->space_info);
  4575. BUG_ON(ret);
  4576. down_write(&cache->space_info->groups_sem);
  4577. list_add_tail(&cache->list, &cache->space_info->block_groups);
  4578. up_write(&cache->space_info->groups_sem);
  4579. ret = btrfs_add_block_group_cache(root->fs_info, cache);
  4580. BUG_ON(ret);
  4581. ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
  4582. sizeof(cache->item));
  4583. BUG_ON(ret);
  4584. finish_current_insert(trans, extent_root, 0);
  4585. ret = del_pending_extents(trans, extent_root, 0);
  4586. BUG_ON(ret);
  4587. set_avail_alloc_bits(extent_root->fs_info, type);
  4588. return 0;
  4589. }
  4590. int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
  4591. struct btrfs_root *root, u64 group_start)
  4592. {
  4593. struct btrfs_path *path;
  4594. struct btrfs_block_group_cache *block_group;
  4595. struct btrfs_key key;
  4596. int ret;
  4597. root = root->fs_info->extent_root;
  4598. block_group = btrfs_lookup_block_group(root->fs_info, group_start);
  4599. BUG_ON(!block_group);
  4600. memcpy(&key, &block_group->key, sizeof(key));
  4601. path = btrfs_alloc_path();
  4602. BUG_ON(!path);
  4603. btrfs_remove_free_space_cache(block_group);
  4604. rb_erase(&block_group->cache_node,
  4605. &root->fs_info->block_group_cache_tree);
  4606. down_write(&block_group->space_info->groups_sem);
  4607. list_del(&block_group->list);
  4608. up_write(&block_group->space_info->groups_sem);
  4609. /*
  4610. memset(shrink_block_group, 0, sizeof(*shrink_block_group));
  4611. kfree(shrink_block_group);
  4612. */
  4613. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  4614. if (ret > 0)
  4615. ret = -EIO;
  4616. if (ret < 0)
  4617. goto out;
  4618. ret = btrfs_del_item(trans, root, path);
  4619. out:
  4620. btrfs_free_path(path);
  4621. return ret;
  4622. }