extent-tree.c 153 KB

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