extent-tree.c 154 KB

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