extent-tree.c 155 KB

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