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