extent-tree.c 157 KB

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