extent-tree.c 153 KB

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