extent-tree.c 155 KB

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