ctree.c 155 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2007,2008 Oracle. All rights reserved.
  4. */
  5. #include <linux/sched.h>
  6. #include <linux/slab.h>
  7. #include <linux/rbtree.h>
  8. #include <linux/mm.h>
  9. #include "ctree.h"
  10. #include "disk-io.h"
  11. #include "transaction.h"
  12. #include "print-tree.h"
  13. #include "locking.h"
  14. static int split_node(struct btrfs_trans_handle *trans, struct btrfs_root
  15. *root, struct btrfs_path *path, int level);
  16. static int split_leaf(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  17. const struct btrfs_key *ins_key, struct btrfs_path *path,
  18. int data_size, int extend);
  19. static int push_node_left(struct btrfs_trans_handle *trans,
  20. struct btrfs_fs_info *fs_info,
  21. struct extent_buffer *dst,
  22. struct extent_buffer *src, int empty);
  23. static int balance_node_right(struct btrfs_trans_handle *trans,
  24. struct btrfs_fs_info *fs_info,
  25. struct extent_buffer *dst_buf,
  26. struct extent_buffer *src_buf);
  27. static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
  28. int level, int slot);
  29. struct btrfs_path *btrfs_alloc_path(void)
  30. {
  31. return kmem_cache_zalloc(btrfs_path_cachep, GFP_NOFS);
  32. }
  33. /*
  34. * set all locked nodes in the path to blocking locks. This should
  35. * be done before scheduling
  36. */
  37. noinline void btrfs_set_path_blocking(struct btrfs_path *p)
  38. {
  39. int i;
  40. for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
  41. if (!p->nodes[i] || !p->locks[i])
  42. continue;
  43. btrfs_set_lock_blocking_rw(p->nodes[i], p->locks[i]);
  44. if (p->locks[i] == BTRFS_READ_LOCK)
  45. p->locks[i] = BTRFS_READ_LOCK_BLOCKING;
  46. else if (p->locks[i] == BTRFS_WRITE_LOCK)
  47. p->locks[i] = BTRFS_WRITE_LOCK_BLOCKING;
  48. }
  49. }
  50. /*
  51. * reset all the locked nodes in the patch to spinning locks.
  52. *
  53. * held is used to keep lockdep happy, when lockdep is enabled
  54. * we set held to a blocking lock before we go around and
  55. * retake all the spinlocks in the path. You can safely use NULL
  56. * for held
  57. */
  58. noinline void btrfs_clear_path_blocking(struct btrfs_path *p,
  59. struct extent_buffer *held, int held_rw)
  60. {
  61. int i;
  62. if (held) {
  63. btrfs_set_lock_blocking_rw(held, held_rw);
  64. if (held_rw == BTRFS_WRITE_LOCK)
  65. held_rw = BTRFS_WRITE_LOCK_BLOCKING;
  66. else if (held_rw == BTRFS_READ_LOCK)
  67. held_rw = BTRFS_READ_LOCK_BLOCKING;
  68. }
  69. btrfs_set_path_blocking(p);
  70. for (i = BTRFS_MAX_LEVEL - 1; i >= 0; i--) {
  71. if (p->nodes[i] && p->locks[i]) {
  72. btrfs_clear_lock_blocking_rw(p->nodes[i], p->locks[i]);
  73. if (p->locks[i] == BTRFS_WRITE_LOCK_BLOCKING)
  74. p->locks[i] = BTRFS_WRITE_LOCK;
  75. else if (p->locks[i] == BTRFS_READ_LOCK_BLOCKING)
  76. p->locks[i] = BTRFS_READ_LOCK;
  77. }
  78. }
  79. if (held)
  80. btrfs_clear_lock_blocking_rw(held, held_rw);
  81. }
  82. /* this also releases the path */
  83. void btrfs_free_path(struct btrfs_path *p)
  84. {
  85. if (!p)
  86. return;
  87. btrfs_release_path(p);
  88. kmem_cache_free(btrfs_path_cachep, p);
  89. }
  90. /*
  91. * path release drops references on the extent buffers in the path
  92. * and it drops any locks held by this path
  93. *
  94. * It is safe to call this on paths that no locks or extent buffers held.
  95. */
  96. noinline void btrfs_release_path(struct btrfs_path *p)
  97. {
  98. int i;
  99. for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
  100. p->slots[i] = 0;
  101. if (!p->nodes[i])
  102. continue;
  103. if (p->locks[i]) {
  104. btrfs_tree_unlock_rw(p->nodes[i], p->locks[i]);
  105. p->locks[i] = 0;
  106. }
  107. free_extent_buffer(p->nodes[i]);
  108. p->nodes[i] = NULL;
  109. }
  110. }
  111. /*
  112. * safely gets a reference on the root node of a tree. A lock
  113. * is not taken, so a concurrent writer may put a different node
  114. * at the root of the tree. See btrfs_lock_root_node for the
  115. * looping required.
  116. *
  117. * The extent buffer returned by this has a reference taken, so
  118. * it won't disappear. It may stop being the root of the tree
  119. * at any time because there are no locks held.
  120. */
  121. struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
  122. {
  123. struct extent_buffer *eb;
  124. while (1) {
  125. rcu_read_lock();
  126. eb = rcu_dereference(root->node);
  127. /*
  128. * RCU really hurts here, we could free up the root node because
  129. * it was COWed but we may not get the new root node yet so do
  130. * the inc_not_zero dance and if it doesn't work then
  131. * synchronize_rcu and try again.
  132. */
  133. if (atomic_inc_not_zero(&eb->refs)) {
  134. rcu_read_unlock();
  135. break;
  136. }
  137. rcu_read_unlock();
  138. synchronize_rcu();
  139. }
  140. return eb;
  141. }
  142. /* loop around taking references on and locking the root node of the
  143. * tree until you end up with a lock on the root. A locked buffer
  144. * is returned, with a reference held.
  145. */
  146. struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root)
  147. {
  148. struct extent_buffer *eb;
  149. while (1) {
  150. eb = btrfs_root_node(root);
  151. btrfs_tree_lock(eb);
  152. if (eb == root->node)
  153. break;
  154. btrfs_tree_unlock(eb);
  155. free_extent_buffer(eb);
  156. }
  157. return eb;
  158. }
  159. /* loop around taking references on and locking the root node of the
  160. * tree until you end up with a lock on the root. A locked buffer
  161. * is returned, with a reference held.
  162. */
  163. struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root)
  164. {
  165. struct extent_buffer *eb;
  166. while (1) {
  167. eb = btrfs_root_node(root);
  168. btrfs_tree_read_lock(eb);
  169. if (eb == root->node)
  170. break;
  171. btrfs_tree_read_unlock(eb);
  172. free_extent_buffer(eb);
  173. }
  174. return eb;
  175. }
  176. /* cowonly root (everything not a reference counted cow subvolume), just get
  177. * put onto a simple dirty list. transaction.c walks this to make sure they
  178. * get properly updated on disk.
  179. */
  180. static void add_root_to_dirty_list(struct btrfs_root *root)
  181. {
  182. struct btrfs_fs_info *fs_info = root->fs_info;
  183. if (test_bit(BTRFS_ROOT_DIRTY, &root->state) ||
  184. !test_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state))
  185. return;
  186. spin_lock(&fs_info->trans_lock);
  187. if (!test_and_set_bit(BTRFS_ROOT_DIRTY, &root->state)) {
  188. /* Want the extent tree to be the last on the list */
  189. if (root->objectid == BTRFS_EXTENT_TREE_OBJECTID)
  190. list_move_tail(&root->dirty_list,
  191. &fs_info->dirty_cowonly_roots);
  192. else
  193. list_move(&root->dirty_list,
  194. &fs_info->dirty_cowonly_roots);
  195. }
  196. spin_unlock(&fs_info->trans_lock);
  197. }
  198. /*
  199. * used by snapshot creation to make a copy of a root for a tree with
  200. * a given objectid. The buffer with the new root node is returned in
  201. * cow_ret, and this func returns zero on success or a negative error code.
  202. */
  203. int btrfs_copy_root(struct btrfs_trans_handle *trans,
  204. struct btrfs_root *root,
  205. struct extent_buffer *buf,
  206. struct extent_buffer **cow_ret, u64 new_root_objectid)
  207. {
  208. struct btrfs_fs_info *fs_info = root->fs_info;
  209. struct extent_buffer *cow;
  210. int ret = 0;
  211. int level;
  212. struct btrfs_disk_key disk_key;
  213. WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
  214. trans->transid != fs_info->running_transaction->transid);
  215. WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
  216. trans->transid != root->last_trans);
  217. level = btrfs_header_level(buf);
  218. if (level == 0)
  219. btrfs_item_key(buf, &disk_key, 0);
  220. else
  221. btrfs_node_key(buf, &disk_key, 0);
  222. cow = btrfs_alloc_tree_block(trans, root, 0, new_root_objectid,
  223. &disk_key, level, buf->start, 0);
  224. if (IS_ERR(cow))
  225. return PTR_ERR(cow);
  226. copy_extent_buffer_full(cow, buf);
  227. btrfs_set_header_bytenr(cow, cow->start);
  228. btrfs_set_header_generation(cow, trans->transid);
  229. btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
  230. btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
  231. BTRFS_HEADER_FLAG_RELOC);
  232. if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
  233. btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
  234. else
  235. btrfs_set_header_owner(cow, new_root_objectid);
  236. write_extent_buffer_fsid(cow, fs_info->fsid);
  237. WARN_ON(btrfs_header_generation(buf) > trans->transid);
  238. if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
  239. ret = btrfs_inc_ref(trans, root, cow, 1);
  240. else
  241. ret = btrfs_inc_ref(trans, root, cow, 0);
  242. if (ret) {
  243. btrfs_tree_unlock(cow);
  244. free_extent_buffer(cow);
  245. btrfs_abort_transaction(trans, ret);
  246. return ret;
  247. }
  248. btrfs_mark_buffer_dirty(cow);
  249. *cow_ret = cow;
  250. return 0;
  251. }
  252. enum mod_log_op {
  253. MOD_LOG_KEY_REPLACE,
  254. MOD_LOG_KEY_ADD,
  255. MOD_LOG_KEY_REMOVE,
  256. MOD_LOG_KEY_REMOVE_WHILE_FREEING,
  257. MOD_LOG_KEY_REMOVE_WHILE_MOVING,
  258. MOD_LOG_MOVE_KEYS,
  259. MOD_LOG_ROOT_REPLACE,
  260. };
  261. struct tree_mod_root {
  262. u64 logical;
  263. u8 level;
  264. };
  265. struct tree_mod_elem {
  266. struct rb_node node;
  267. u64 logical;
  268. u64 seq;
  269. enum mod_log_op op;
  270. /* this is used for MOD_LOG_KEY_* and MOD_LOG_MOVE_KEYS operations */
  271. int slot;
  272. /* this is used for MOD_LOG_KEY* and MOD_LOG_ROOT_REPLACE */
  273. u64 generation;
  274. /* those are used for op == MOD_LOG_KEY_{REPLACE,REMOVE} */
  275. struct btrfs_disk_key key;
  276. u64 blockptr;
  277. /* this is used for op == MOD_LOG_MOVE_KEYS */
  278. struct {
  279. int dst_slot;
  280. int nr_items;
  281. } move;
  282. /* this is used for op == MOD_LOG_ROOT_REPLACE */
  283. struct tree_mod_root old_root;
  284. };
  285. /*
  286. * Pull a new tree mod seq number for our operation.
  287. */
  288. static inline u64 btrfs_inc_tree_mod_seq(struct btrfs_fs_info *fs_info)
  289. {
  290. return atomic64_inc_return(&fs_info->tree_mod_seq);
  291. }
  292. /*
  293. * This adds a new blocker to the tree mod log's blocker list if the @elem
  294. * passed does not already have a sequence number set. So when a caller expects
  295. * to record tree modifications, it should ensure to set elem->seq to zero
  296. * before calling btrfs_get_tree_mod_seq.
  297. * Returns a fresh, unused tree log modification sequence number, even if no new
  298. * blocker was added.
  299. */
  300. u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
  301. struct seq_list *elem)
  302. {
  303. write_lock(&fs_info->tree_mod_log_lock);
  304. if (!elem->seq) {
  305. elem->seq = btrfs_inc_tree_mod_seq(fs_info);
  306. list_add_tail(&elem->list, &fs_info->tree_mod_seq_list);
  307. }
  308. write_unlock(&fs_info->tree_mod_log_lock);
  309. return elem->seq;
  310. }
  311. void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
  312. struct seq_list *elem)
  313. {
  314. struct rb_root *tm_root;
  315. struct rb_node *node;
  316. struct rb_node *next;
  317. struct seq_list *cur_elem;
  318. struct tree_mod_elem *tm;
  319. u64 min_seq = (u64)-1;
  320. u64 seq_putting = elem->seq;
  321. if (!seq_putting)
  322. return;
  323. write_lock(&fs_info->tree_mod_log_lock);
  324. list_del(&elem->list);
  325. elem->seq = 0;
  326. list_for_each_entry(cur_elem, &fs_info->tree_mod_seq_list, list) {
  327. if (cur_elem->seq < min_seq) {
  328. if (seq_putting > cur_elem->seq) {
  329. /*
  330. * blocker with lower sequence number exists, we
  331. * cannot remove anything from the log
  332. */
  333. write_unlock(&fs_info->tree_mod_log_lock);
  334. return;
  335. }
  336. min_seq = cur_elem->seq;
  337. }
  338. }
  339. /*
  340. * anything that's lower than the lowest existing (read: blocked)
  341. * sequence number can be removed from the tree.
  342. */
  343. tm_root = &fs_info->tree_mod_log;
  344. for (node = rb_first(tm_root); node; node = next) {
  345. next = rb_next(node);
  346. tm = rb_entry(node, struct tree_mod_elem, node);
  347. if (tm->seq >= min_seq)
  348. continue;
  349. rb_erase(node, tm_root);
  350. kfree(tm);
  351. }
  352. write_unlock(&fs_info->tree_mod_log_lock);
  353. }
  354. /*
  355. * key order of the log:
  356. * node/leaf start address -> sequence
  357. *
  358. * The 'start address' is the logical address of the *new* root node
  359. * for root replace operations, or the logical address of the affected
  360. * block for all other operations.
  361. *
  362. * Note: must be called with write lock for fs_info::tree_mod_log_lock.
  363. */
  364. static noinline int
  365. __tree_mod_log_insert(struct btrfs_fs_info *fs_info, struct tree_mod_elem *tm)
  366. {
  367. struct rb_root *tm_root;
  368. struct rb_node **new;
  369. struct rb_node *parent = NULL;
  370. struct tree_mod_elem *cur;
  371. tm->seq = btrfs_inc_tree_mod_seq(fs_info);
  372. tm_root = &fs_info->tree_mod_log;
  373. new = &tm_root->rb_node;
  374. while (*new) {
  375. cur = rb_entry(*new, struct tree_mod_elem, node);
  376. parent = *new;
  377. if (cur->logical < tm->logical)
  378. new = &((*new)->rb_left);
  379. else if (cur->logical > tm->logical)
  380. new = &((*new)->rb_right);
  381. else if (cur->seq < tm->seq)
  382. new = &((*new)->rb_left);
  383. else if (cur->seq > tm->seq)
  384. new = &((*new)->rb_right);
  385. else
  386. return -EEXIST;
  387. }
  388. rb_link_node(&tm->node, parent, new);
  389. rb_insert_color(&tm->node, tm_root);
  390. return 0;
  391. }
  392. /*
  393. * Determines if logging can be omitted. Returns 1 if it can. Otherwise, it
  394. * returns zero with the tree_mod_log_lock acquired. The caller must hold
  395. * this until all tree mod log insertions are recorded in the rb tree and then
  396. * write unlock fs_info::tree_mod_log_lock.
  397. */
  398. static inline int tree_mod_dont_log(struct btrfs_fs_info *fs_info,
  399. struct extent_buffer *eb) {
  400. smp_mb();
  401. if (list_empty(&(fs_info)->tree_mod_seq_list))
  402. return 1;
  403. if (eb && btrfs_header_level(eb) == 0)
  404. return 1;
  405. write_lock(&fs_info->tree_mod_log_lock);
  406. if (list_empty(&(fs_info)->tree_mod_seq_list)) {
  407. write_unlock(&fs_info->tree_mod_log_lock);
  408. return 1;
  409. }
  410. return 0;
  411. }
  412. /* Similar to tree_mod_dont_log, but doesn't acquire any locks. */
  413. static inline int tree_mod_need_log(const struct btrfs_fs_info *fs_info,
  414. struct extent_buffer *eb)
  415. {
  416. smp_mb();
  417. if (list_empty(&(fs_info)->tree_mod_seq_list))
  418. return 0;
  419. if (eb && btrfs_header_level(eb) == 0)
  420. return 0;
  421. return 1;
  422. }
  423. static struct tree_mod_elem *
  424. alloc_tree_mod_elem(struct extent_buffer *eb, int slot,
  425. enum mod_log_op op, gfp_t flags)
  426. {
  427. struct tree_mod_elem *tm;
  428. tm = kzalloc(sizeof(*tm), flags);
  429. if (!tm)
  430. return NULL;
  431. tm->logical = eb->start;
  432. if (op != MOD_LOG_KEY_ADD) {
  433. btrfs_node_key(eb, &tm->key, slot);
  434. tm->blockptr = btrfs_node_blockptr(eb, slot);
  435. }
  436. tm->op = op;
  437. tm->slot = slot;
  438. tm->generation = btrfs_node_ptr_generation(eb, slot);
  439. RB_CLEAR_NODE(&tm->node);
  440. return tm;
  441. }
  442. static noinline int tree_mod_log_insert_key(struct extent_buffer *eb, int slot,
  443. enum mod_log_op op, gfp_t flags)
  444. {
  445. struct tree_mod_elem *tm;
  446. int ret;
  447. if (!tree_mod_need_log(eb->fs_info, eb))
  448. return 0;
  449. tm = alloc_tree_mod_elem(eb, slot, op, flags);
  450. if (!tm)
  451. return -ENOMEM;
  452. if (tree_mod_dont_log(eb->fs_info, eb)) {
  453. kfree(tm);
  454. return 0;
  455. }
  456. ret = __tree_mod_log_insert(eb->fs_info, tm);
  457. write_unlock(&eb->fs_info->tree_mod_log_lock);
  458. if (ret)
  459. kfree(tm);
  460. return ret;
  461. }
  462. static noinline int tree_mod_log_insert_move(struct extent_buffer *eb,
  463. int dst_slot, int src_slot, int nr_items)
  464. {
  465. struct tree_mod_elem *tm = NULL;
  466. struct tree_mod_elem **tm_list = NULL;
  467. int ret = 0;
  468. int i;
  469. int locked = 0;
  470. if (!tree_mod_need_log(eb->fs_info, eb))
  471. return 0;
  472. tm_list = kcalloc(nr_items, sizeof(struct tree_mod_elem *), GFP_NOFS);
  473. if (!tm_list)
  474. return -ENOMEM;
  475. tm = kzalloc(sizeof(*tm), GFP_NOFS);
  476. if (!tm) {
  477. ret = -ENOMEM;
  478. goto free_tms;
  479. }
  480. tm->logical = eb->start;
  481. tm->slot = src_slot;
  482. tm->move.dst_slot = dst_slot;
  483. tm->move.nr_items = nr_items;
  484. tm->op = MOD_LOG_MOVE_KEYS;
  485. for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
  486. tm_list[i] = alloc_tree_mod_elem(eb, i + dst_slot,
  487. MOD_LOG_KEY_REMOVE_WHILE_MOVING, GFP_NOFS);
  488. if (!tm_list[i]) {
  489. ret = -ENOMEM;
  490. goto free_tms;
  491. }
  492. }
  493. if (tree_mod_dont_log(eb->fs_info, eb))
  494. goto free_tms;
  495. locked = 1;
  496. /*
  497. * When we override something during the move, we log these removals.
  498. * This can only happen when we move towards the beginning of the
  499. * buffer, i.e. dst_slot < src_slot.
  500. */
  501. for (i = 0; i + dst_slot < src_slot && i < nr_items; i++) {
  502. ret = __tree_mod_log_insert(eb->fs_info, tm_list[i]);
  503. if (ret)
  504. goto free_tms;
  505. }
  506. ret = __tree_mod_log_insert(eb->fs_info, tm);
  507. if (ret)
  508. goto free_tms;
  509. write_unlock(&eb->fs_info->tree_mod_log_lock);
  510. kfree(tm_list);
  511. return 0;
  512. free_tms:
  513. for (i = 0; i < nr_items; i++) {
  514. if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
  515. rb_erase(&tm_list[i]->node, &eb->fs_info->tree_mod_log);
  516. kfree(tm_list[i]);
  517. }
  518. if (locked)
  519. write_unlock(&eb->fs_info->tree_mod_log_lock);
  520. kfree(tm_list);
  521. kfree(tm);
  522. return ret;
  523. }
  524. static inline int
  525. __tree_mod_log_free_eb(struct btrfs_fs_info *fs_info,
  526. struct tree_mod_elem **tm_list,
  527. int nritems)
  528. {
  529. int i, j;
  530. int ret;
  531. for (i = nritems - 1; i >= 0; i--) {
  532. ret = __tree_mod_log_insert(fs_info, tm_list[i]);
  533. if (ret) {
  534. for (j = nritems - 1; j > i; j--)
  535. rb_erase(&tm_list[j]->node,
  536. &fs_info->tree_mod_log);
  537. return ret;
  538. }
  539. }
  540. return 0;
  541. }
  542. static noinline int tree_mod_log_insert_root(struct extent_buffer *old_root,
  543. struct extent_buffer *new_root, int log_removal)
  544. {
  545. struct btrfs_fs_info *fs_info = old_root->fs_info;
  546. struct tree_mod_elem *tm = NULL;
  547. struct tree_mod_elem **tm_list = NULL;
  548. int nritems = 0;
  549. int ret = 0;
  550. int i;
  551. if (!tree_mod_need_log(fs_info, NULL))
  552. return 0;
  553. if (log_removal && btrfs_header_level(old_root) > 0) {
  554. nritems = btrfs_header_nritems(old_root);
  555. tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *),
  556. GFP_NOFS);
  557. if (!tm_list) {
  558. ret = -ENOMEM;
  559. goto free_tms;
  560. }
  561. for (i = 0; i < nritems; i++) {
  562. tm_list[i] = alloc_tree_mod_elem(old_root, i,
  563. MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
  564. if (!tm_list[i]) {
  565. ret = -ENOMEM;
  566. goto free_tms;
  567. }
  568. }
  569. }
  570. tm = kzalloc(sizeof(*tm), GFP_NOFS);
  571. if (!tm) {
  572. ret = -ENOMEM;
  573. goto free_tms;
  574. }
  575. tm->logical = new_root->start;
  576. tm->old_root.logical = old_root->start;
  577. tm->old_root.level = btrfs_header_level(old_root);
  578. tm->generation = btrfs_header_generation(old_root);
  579. tm->op = MOD_LOG_ROOT_REPLACE;
  580. if (tree_mod_dont_log(fs_info, NULL))
  581. goto free_tms;
  582. if (tm_list)
  583. ret = __tree_mod_log_free_eb(fs_info, tm_list, nritems);
  584. if (!ret)
  585. ret = __tree_mod_log_insert(fs_info, tm);
  586. write_unlock(&fs_info->tree_mod_log_lock);
  587. if (ret)
  588. goto free_tms;
  589. kfree(tm_list);
  590. return ret;
  591. free_tms:
  592. if (tm_list) {
  593. for (i = 0; i < nritems; i++)
  594. kfree(tm_list[i]);
  595. kfree(tm_list);
  596. }
  597. kfree(tm);
  598. return ret;
  599. }
  600. static struct tree_mod_elem *
  601. __tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq,
  602. int smallest)
  603. {
  604. struct rb_root *tm_root;
  605. struct rb_node *node;
  606. struct tree_mod_elem *cur = NULL;
  607. struct tree_mod_elem *found = NULL;
  608. read_lock(&fs_info->tree_mod_log_lock);
  609. tm_root = &fs_info->tree_mod_log;
  610. node = tm_root->rb_node;
  611. while (node) {
  612. cur = rb_entry(node, struct tree_mod_elem, node);
  613. if (cur->logical < start) {
  614. node = node->rb_left;
  615. } else if (cur->logical > start) {
  616. node = node->rb_right;
  617. } else if (cur->seq < min_seq) {
  618. node = node->rb_left;
  619. } else if (!smallest) {
  620. /* we want the node with the highest seq */
  621. if (found)
  622. BUG_ON(found->seq > cur->seq);
  623. found = cur;
  624. node = node->rb_left;
  625. } else if (cur->seq > min_seq) {
  626. /* we want the node with the smallest seq */
  627. if (found)
  628. BUG_ON(found->seq < cur->seq);
  629. found = cur;
  630. node = node->rb_right;
  631. } else {
  632. found = cur;
  633. break;
  634. }
  635. }
  636. read_unlock(&fs_info->tree_mod_log_lock);
  637. return found;
  638. }
  639. /*
  640. * this returns the element from the log with the smallest time sequence
  641. * value that's in the log (the oldest log item). any element with a time
  642. * sequence lower than min_seq will be ignored.
  643. */
  644. static struct tree_mod_elem *
  645. tree_mod_log_search_oldest(struct btrfs_fs_info *fs_info, u64 start,
  646. u64 min_seq)
  647. {
  648. return __tree_mod_log_search(fs_info, start, min_seq, 1);
  649. }
  650. /*
  651. * this returns the element from the log with the largest time sequence
  652. * value that's in the log (the most recent log item). any element with
  653. * a time sequence lower than min_seq will be ignored.
  654. */
  655. static struct tree_mod_elem *
  656. tree_mod_log_search(struct btrfs_fs_info *fs_info, u64 start, u64 min_seq)
  657. {
  658. return __tree_mod_log_search(fs_info, start, min_seq, 0);
  659. }
  660. static noinline int
  661. tree_mod_log_eb_copy(struct btrfs_fs_info *fs_info, struct extent_buffer *dst,
  662. struct extent_buffer *src, unsigned long dst_offset,
  663. unsigned long src_offset, int nr_items)
  664. {
  665. int ret = 0;
  666. struct tree_mod_elem **tm_list = NULL;
  667. struct tree_mod_elem **tm_list_add, **tm_list_rem;
  668. int i;
  669. int locked = 0;
  670. if (!tree_mod_need_log(fs_info, NULL))
  671. return 0;
  672. if (btrfs_header_level(dst) == 0 && btrfs_header_level(src) == 0)
  673. return 0;
  674. tm_list = kcalloc(nr_items * 2, sizeof(struct tree_mod_elem *),
  675. GFP_NOFS);
  676. if (!tm_list)
  677. return -ENOMEM;
  678. tm_list_add = tm_list;
  679. tm_list_rem = tm_list + nr_items;
  680. for (i = 0; i < nr_items; i++) {
  681. tm_list_rem[i] = alloc_tree_mod_elem(src, i + src_offset,
  682. MOD_LOG_KEY_REMOVE, GFP_NOFS);
  683. if (!tm_list_rem[i]) {
  684. ret = -ENOMEM;
  685. goto free_tms;
  686. }
  687. tm_list_add[i] = alloc_tree_mod_elem(dst, i + dst_offset,
  688. MOD_LOG_KEY_ADD, GFP_NOFS);
  689. if (!tm_list_add[i]) {
  690. ret = -ENOMEM;
  691. goto free_tms;
  692. }
  693. }
  694. if (tree_mod_dont_log(fs_info, NULL))
  695. goto free_tms;
  696. locked = 1;
  697. for (i = 0; i < nr_items; i++) {
  698. ret = __tree_mod_log_insert(fs_info, tm_list_rem[i]);
  699. if (ret)
  700. goto free_tms;
  701. ret = __tree_mod_log_insert(fs_info, tm_list_add[i]);
  702. if (ret)
  703. goto free_tms;
  704. }
  705. write_unlock(&fs_info->tree_mod_log_lock);
  706. kfree(tm_list);
  707. return 0;
  708. free_tms:
  709. for (i = 0; i < nr_items * 2; i++) {
  710. if (tm_list[i] && !RB_EMPTY_NODE(&tm_list[i]->node))
  711. rb_erase(&tm_list[i]->node, &fs_info->tree_mod_log);
  712. kfree(tm_list[i]);
  713. }
  714. if (locked)
  715. write_unlock(&fs_info->tree_mod_log_lock);
  716. kfree(tm_list);
  717. return ret;
  718. }
  719. static noinline int tree_mod_log_free_eb(struct extent_buffer *eb)
  720. {
  721. struct tree_mod_elem **tm_list = NULL;
  722. int nritems = 0;
  723. int i;
  724. int ret = 0;
  725. if (btrfs_header_level(eb) == 0)
  726. return 0;
  727. if (!tree_mod_need_log(eb->fs_info, NULL))
  728. return 0;
  729. nritems = btrfs_header_nritems(eb);
  730. tm_list = kcalloc(nritems, sizeof(struct tree_mod_elem *), GFP_NOFS);
  731. if (!tm_list)
  732. return -ENOMEM;
  733. for (i = 0; i < nritems; i++) {
  734. tm_list[i] = alloc_tree_mod_elem(eb, i,
  735. MOD_LOG_KEY_REMOVE_WHILE_FREEING, GFP_NOFS);
  736. if (!tm_list[i]) {
  737. ret = -ENOMEM;
  738. goto free_tms;
  739. }
  740. }
  741. if (tree_mod_dont_log(eb->fs_info, eb))
  742. goto free_tms;
  743. ret = __tree_mod_log_free_eb(eb->fs_info, tm_list, nritems);
  744. write_unlock(&eb->fs_info->tree_mod_log_lock);
  745. if (ret)
  746. goto free_tms;
  747. kfree(tm_list);
  748. return 0;
  749. free_tms:
  750. for (i = 0; i < nritems; i++)
  751. kfree(tm_list[i]);
  752. kfree(tm_list);
  753. return ret;
  754. }
  755. /*
  756. * check if the tree block can be shared by multiple trees
  757. */
  758. int btrfs_block_can_be_shared(struct btrfs_root *root,
  759. struct extent_buffer *buf)
  760. {
  761. /*
  762. * Tree blocks not in reference counted trees and tree roots
  763. * are never shared. If a block was allocated after the last
  764. * snapshot and the block was not allocated by tree relocation,
  765. * we know the block is not shared.
  766. */
  767. if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
  768. buf != root->node && buf != root->commit_root &&
  769. (btrfs_header_generation(buf) <=
  770. btrfs_root_last_snapshot(&root->root_item) ||
  771. btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)))
  772. return 1;
  773. return 0;
  774. }
  775. static noinline int update_ref_for_cow(struct btrfs_trans_handle *trans,
  776. struct btrfs_root *root,
  777. struct extent_buffer *buf,
  778. struct extent_buffer *cow,
  779. int *last_ref)
  780. {
  781. struct btrfs_fs_info *fs_info = root->fs_info;
  782. u64 refs;
  783. u64 owner;
  784. u64 flags;
  785. u64 new_flags = 0;
  786. int ret;
  787. /*
  788. * Backrefs update rules:
  789. *
  790. * Always use full backrefs for extent pointers in tree block
  791. * allocated by tree relocation.
  792. *
  793. * If a shared tree block is no longer referenced by its owner
  794. * tree (btrfs_header_owner(buf) == root->root_key.objectid),
  795. * use full backrefs for extent pointers in tree block.
  796. *
  797. * If a tree block is been relocating
  798. * (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID),
  799. * use full backrefs for extent pointers in tree block.
  800. * The reason for this is some operations (such as drop tree)
  801. * are only allowed for blocks use full backrefs.
  802. */
  803. if (btrfs_block_can_be_shared(root, buf)) {
  804. ret = btrfs_lookup_extent_info(trans, fs_info, buf->start,
  805. btrfs_header_level(buf), 1,
  806. &refs, &flags);
  807. if (ret)
  808. return ret;
  809. if (refs == 0) {
  810. ret = -EROFS;
  811. btrfs_handle_fs_error(fs_info, ret, NULL);
  812. return ret;
  813. }
  814. } else {
  815. refs = 1;
  816. if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
  817. btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
  818. flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
  819. else
  820. flags = 0;
  821. }
  822. owner = btrfs_header_owner(buf);
  823. BUG_ON(owner == BTRFS_TREE_RELOC_OBJECTID &&
  824. !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
  825. if (refs > 1) {
  826. if ((owner == root->root_key.objectid ||
  827. root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) &&
  828. !(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
  829. ret = btrfs_inc_ref(trans, root, buf, 1);
  830. if (ret)
  831. return ret;
  832. if (root->root_key.objectid ==
  833. BTRFS_TREE_RELOC_OBJECTID) {
  834. ret = btrfs_dec_ref(trans, root, buf, 0);
  835. if (ret)
  836. return ret;
  837. ret = btrfs_inc_ref(trans, root, cow, 1);
  838. if (ret)
  839. return ret;
  840. }
  841. new_flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
  842. } else {
  843. if (root->root_key.objectid ==
  844. BTRFS_TREE_RELOC_OBJECTID)
  845. ret = btrfs_inc_ref(trans, root, cow, 1);
  846. else
  847. ret = btrfs_inc_ref(trans, root, cow, 0);
  848. if (ret)
  849. return ret;
  850. }
  851. if (new_flags != 0) {
  852. int level = btrfs_header_level(buf);
  853. ret = btrfs_set_disk_extent_flags(trans, fs_info,
  854. buf->start,
  855. buf->len,
  856. new_flags, level, 0);
  857. if (ret)
  858. return ret;
  859. }
  860. } else {
  861. if (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
  862. if (root->root_key.objectid ==
  863. BTRFS_TREE_RELOC_OBJECTID)
  864. ret = btrfs_inc_ref(trans, root, cow, 1);
  865. else
  866. ret = btrfs_inc_ref(trans, root, cow, 0);
  867. if (ret)
  868. return ret;
  869. ret = btrfs_dec_ref(trans, root, buf, 1);
  870. if (ret)
  871. return ret;
  872. }
  873. clean_tree_block(fs_info, buf);
  874. *last_ref = 1;
  875. }
  876. return 0;
  877. }
  878. static struct extent_buffer *alloc_tree_block_no_bg_flush(
  879. struct btrfs_trans_handle *trans,
  880. struct btrfs_root *root,
  881. u64 parent_start,
  882. const struct btrfs_disk_key *disk_key,
  883. int level,
  884. u64 hint,
  885. u64 empty_size)
  886. {
  887. struct btrfs_fs_info *fs_info = root->fs_info;
  888. struct extent_buffer *ret;
  889. /*
  890. * If we are COWing a node/leaf from the extent, chunk, device or free
  891. * space trees, make sure that we do not finish block group creation of
  892. * pending block groups. We do this to avoid a deadlock.
  893. * COWing can result in allocation of a new chunk, and flushing pending
  894. * block groups (btrfs_create_pending_block_groups()) can be triggered
  895. * when finishing allocation of a new chunk. Creation of a pending block
  896. * group modifies the extent, chunk, device and free space trees,
  897. * therefore we could deadlock with ourselves since we are holding a
  898. * lock on an extent buffer that btrfs_create_pending_block_groups() may
  899. * try to COW later.
  900. * For similar reasons, we also need to delay flushing pending block
  901. * groups when splitting a leaf or node, from one of those trees, since
  902. * we are holding a write lock on it and its parent or when inserting a
  903. * new root node for one of those trees.
  904. */
  905. if (root == fs_info->extent_root ||
  906. root == fs_info->chunk_root ||
  907. root == fs_info->dev_root ||
  908. root == fs_info->free_space_root)
  909. trans->can_flush_pending_bgs = false;
  910. ret = btrfs_alloc_tree_block(trans, root, parent_start,
  911. root->root_key.objectid, disk_key, level,
  912. hint, empty_size);
  913. trans->can_flush_pending_bgs = true;
  914. return ret;
  915. }
  916. /*
  917. * does the dirty work in cow of a single block. The parent block (if
  918. * supplied) is updated to point to the new cow copy. The new buffer is marked
  919. * dirty and returned locked. If you modify the block it needs to be marked
  920. * dirty again.
  921. *
  922. * search_start -- an allocation hint for the new block
  923. *
  924. * empty_size -- a hint that you plan on doing more cow. This is the size in
  925. * bytes the allocator should try to find free next to the block it returns.
  926. * This is just a hint and may be ignored by the allocator.
  927. */
  928. static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
  929. struct btrfs_root *root,
  930. struct extent_buffer *buf,
  931. struct extent_buffer *parent, int parent_slot,
  932. struct extent_buffer **cow_ret,
  933. u64 search_start, u64 empty_size)
  934. {
  935. struct btrfs_fs_info *fs_info = root->fs_info;
  936. struct btrfs_disk_key disk_key;
  937. struct extent_buffer *cow;
  938. int level, ret;
  939. int last_ref = 0;
  940. int unlock_orig = 0;
  941. u64 parent_start = 0;
  942. if (*cow_ret == buf)
  943. unlock_orig = 1;
  944. btrfs_assert_tree_locked(buf);
  945. WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
  946. trans->transid != fs_info->running_transaction->transid);
  947. WARN_ON(test_bit(BTRFS_ROOT_REF_COWS, &root->state) &&
  948. trans->transid != root->last_trans);
  949. level = btrfs_header_level(buf);
  950. if (level == 0)
  951. btrfs_item_key(buf, &disk_key, 0);
  952. else
  953. btrfs_node_key(buf, &disk_key, 0);
  954. if ((root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID) && parent)
  955. parent_start = parent->start;
  956. cow = alloc_tree_block_no_bg_flush(trans, root, parent_start, &disk_key,
  957. level, search_start, empty_size);
  958. if (IS_ERR(cow))
  959. return PTR_ERR(cow);
  960. /* cow is set to blocking by btrfs_init_new_buffer */
  961. copy_extent_buffer_full(cow, buf);
  962. btrfs_set_header_bytenr(cow, cow->start);
  963. btrfs_set_header_generation(cow, trans->transid);
  964. btrfs_set_header_backref_rev(cow, BTRFS_MIXED_BACKREF_REV);
  965. btrfs_clear_header_flag(cow, BTRFS_HEADER_FLAG_WRITTEN |
  966. BTRFS_HEADER_FLAG_RELOC);
  967. if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID)
  968. btrfs_set_header_flag(cow, BTRFS_HEADER_FLAG_RELOC);
  969. else
  970. btrfs_set_header_owner(cow, root->root_key.objectid);
  971. write_extent_buffer_fsid(cow, fs_info->fsid);
  972. ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
  973. if (ret) {
  974. btrfs_tree_unlock(cow);
  975. free_extent_buffer(cow);
  976. btrfs_abort_transaction(trans, ret);
  977. return ret;
  978. }
  979. if (test_bit(BTRFS_ROOT_REF_COWS, &root->state)) {
  980. ret = btrfs_reloc_cow_block(trans, root, buf, cow);
  981. if (ret) {
  982. btrfs_tree_unlock(cow);
  983. free_extent_buffer(cow);
  984. btrfs_abort_transaction(trans, ret);
  985. return ret;
  986. }
  987. }
  988. if (buf == root->node) {
  989. WARN_ON(parent && parent != buf);
  990. if (root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID ||
  991. btrfs_header_backref_rev(buf) < BTRFS_MIXED_BACKREF_REV)
  992. parent_start = buf->start;
  993. extent_buffer_get(cow);
  994. ret = tree_mod_log_insert_root(root->node, cow, 1);
  995. BUG_ON(ret < 0);
  996. rcu_assign_pointer(root->node, cow);
  997. btrfs_free_tree_block(trans, root, buf, parent_start,
  998. last_ref);
  999. free_extent_buffer(buf);
  1000. add_root_to_dirty_list(root);
  1001. } else {
  1002. WARN_ON(trans->transid != btrfs_header_generation(parent));
  1003. tree_mod_log_insert_key(parent, parent_slot,
  1004. MOD_LOG_KEY_REPLACE, GFP_NOFS);
  1005. btrfs_set_node_blockptr(parent, parent_slot,
  1006. cow->start);
  1007. btrfs_set_node_ptr_generation(parent, parent_slot,
  1008. trans->transid);
  1009. btrfs_mark_buffer_dirty(parent);
  1010. if (last_ref) {
  1011. ret = tree_mod_log_free_eb(buf);
  1012. if (ret) {
  1013. btrfs_tree_unlock(cow);
  1014. free_extent_buffer(cow);
  1015. btrfs_abort_transaction(trans, ret);
  1016. return ret;
  1017. }
  1018. }
  1019. btrfs_free_tree_block(trans, root, buf, parent_start,
  1020. last_ref);
  1021. }
  1022. if (unlock_orig)
  1023. btrfs_tree_unlock(buf);
  1024. free_extent_buffer_stale(buf);
  1025. btrfs_mark_buffer_dirty(cow);
  1026. *cow_ret = cow;
  1027. return 0;
  1028. }
  1029. /*
  1030. * returns the logical address of the oldest predecessor of the given root.
  1031. * entries older than time_seq are ignored.
  1032. */
  1033. static struct tree_mod_elem *__tree_mod_log_oldest_root(
  1034. struct extent_buffer *eb_root, u64 time_seq)
  1035. {
  1036. struct tree_mod_elem *tm;
  1037. struct tree_mod_elem *found = NULL;
  1038. u64 root_logical = eb_root->start;
  1039. int looped = 0;
  1040. if (!time_seq)
  1041. return NULL;
  1042. /*
  1043. * the very last operation that's logged for a root is the
  1044. * replacement operation (if it is replaced at all). this has
  1045. * the logical address of the *new* root, making it the very
  1046. * first operation that's logged for this root.
  1047. */
  1048. while (1) {
  1049. tm = tree_mod_log_search_oldest(eb_root->fs_info, root_logical,
  1050. time_seq);
  1051. if (!looped && !tm)
  1052. return NULL;
  1053. /*
  1054. * if there are no tree operation for the oldest root, we simply
  1055. * return it. this should only happen if that (old) root is at
  1056. * level 0.
  1057. */
  1058. if (!tm)
  1059. break;
  1060. /*
  1061. * if there's an operation that's not a root replacement, we
  1062. * found the oldest version of our root. normally, we'll find a
  1063. * MOD_LOG_KEY_REMOVE_WHILE_FREEING operation here.
  1064. */
  1065. if (tm->op != MOD_LOG_ROOT_REPLACE)
  1066. break;
  1067. found = tm;
  1068. root_logical = tm->old_root.logical;
  1069. looped = 1;
  1070. }
  1071. /* if there's no old root to return, return what we found instead */
  1072. if (!found)
  1073. found = tm;
  1074. return found;
  1075. }
  1076. /*
  1077. * tm is a pointer to the first operation to rewind within eb. then, all
  1078. * previous operations will be rewound (until we reach something older than
  1079. * time_seq).
  1080. */
  1081. static void
  1082. __tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct extent_buffer *eb,
  1083. u64 time_seq, struct tree_mod_elem *first_tm)
  1084. {
  1085. u32 n;
  1086. struct rb_node *next;
  1087. struct tree_mod_elem *tm = first_tm;
  1088. unsigned long o_dst;
  1089. unsigned long o_src;
  1090. unsigned long p_size = sizeof(struct btrfs_key_ptr);
  1091. n = btrfs_header_nritems(eb);
  1092. read_lock(&fs_info->tree_mod_log_lock);
  1093. while (tm && tm->seq >= time_seq) {
  1094. /*
  1095. * all the operations are recorded with the operator used for
  1096. * the modification. as we're going backwards, we do the
  1097. * opposite of each operation here.
  1098. */
  1099. switch (tm->op) {
  1100. case MOD_LOG_KEY_REMOVE_WHILE_FREEING:
  1101. BUG_ON(tm->slot < n);
  1102. /* Fallthrough */
  1103. case MOD_LOG_KEY_REMOVE_WHILE_MOVING:
  1104. case MOD_LOG_KEY_REMOVE:
  1105. btrfs_set_node_key(eb, &tm->key, tm->slot);
  1106. btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
  1107. btrfs_set_node_ptr_generation(eb, tm->slot,
  1108. tm->generation);
  1109. n++;
  1110. break;
  1111. case MOD_LOG_KEY_REPLACE:
  1112. BUG_ON(tm->slot >= n);
  1113. btrfs_set_node_key(eb, &tm->key, tm->slot);
  1114. btrfs_set_node_blockptr(eb, tm->slot, tm->blockptr);
  1115. btrfs_set_node_ptr_generation(eb, tm->slot,
  1116. tm->generation);
  1117. break;
  1118. case MOD_LOG_KEY_ADD:
  1119. /* if a move operation is needed it's in the log */
  1120. n--;
  1121. break;
  1122. case MOD_LOG_MOVE_KEYS:
  1123. o_dst = btrfs_node_key_ptr_offset(tm->slot);
  1124. o_src = btrfs_node_key_ptr_offset(tm->move.dst_slot);
  1125. memmove_extent_buffer(eb, o_dst, o_src,
  1126. tm->move.nr_items * p_size);
  1127. break;
  1128. case MOD_LOG_ROOT_REPLACE:
  1129. /*
  1130. * this operation is special. for roots, this must be
  1131. * handled explicitly before rewinding.
  1132. * for non-roots, this operation may exist if the node
  1133. * was a root: root A -> child B; then A gets empty and
  1134. * B is promoted to the new root. in the mod log, we'll
  1135. * have a root-replace operation for B, a tree block
  1136. * that is no root. we simply ignore that operation.
  1137. */
  1138. break;
  1139. }
  1140. next = rb_next(&tm->node);
  1141. if (!next)
  1142. break;
  1143. tm = rb_entry(next, struct tree_mod_elem, node);
  1144. if (tm->logical != first_tm->logical)
  1145. break;
  1146. }
  1147. read_unlock(&fs_info->tree_mod_log_lock);
  1148. btrfs_set_header_nritems(eb, n);
  1149. }
  1150. /*
  1151. * Called with eb read locked. If the buffer cannot be rewound, the same buffer
  1152. * is returned. If rewind operations happen, a fresh buffer is returned. The
  1153. * returned buffer is always read-locked. If the returned buffer is not the
  1154. * input buffer, the lock on the input buffer is released and the input buffer
  1155. * is freed (its refcount is decremented).
  1156. */
  1157. static struct extent_buffer *
  1158. tree_mod_log_rewind(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
  1159. struct extent_buffer *eb, u64 time_seq)
  1160. {
  1161. struct extent_buffer *eb_rewin;
  1162. struct tree_mod_elem *tm;
  1163. if (!time_seq)
  1164. return eb;
  1165. if (btrfs_header_level(eb) == 0)
  1166. return eb;
  1167. tm = tree_mod_log_search(fs_info, eb->start, time_seq);
  1168. if (!tm)
  1169. return eb;
  1170. btrfs_set_path_blocking(path);
  1171. btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
  1172. if (tm->op == MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
  1173. BUG_ON(tm->slot != 0);
  1174. eb_rewin = alloc_dummy_extent_buffer(fs_info, eb->start);
  1175. if (!eb_rewin) {
  1176. btrfs_tree_read_unlock_blocking(eb);
  1177. free_extent_buffer(eb);
  1178. return NULL;
  1179. }
  1180. btrfs_set_header_bytenr(eb_rewin, eb->start);
  1181. btrfs_set_header_backref_rev(eb_rewin,
  1182. btrfs_header_backref_rev(eb));
  1183. btrfs_set_header_owner(eb_rewin, btrfs_header_owner(eb));
  1184. btrfs_set_header_level(eb_rewin, btrfs_header_level(eb));
  1185. } else {
  1186. eb_rewin = btrfs_clone_extent_buffer(eb);
  1187. if (!eb_rewin) {
  1188. btrfs_tree_read_unlock_blocking(eb);
  1189. free_extent_buffer(eb);
  1190. return NULL;
  1191. }
  1192. }
  1193. btrfs_clear_path_blocking(path, NULL, BTRFS_READ_LOCK);
  1194. btrfs_tree_read_unlock_blocking(eb);
  1195. free_extent_buffer(eb);
  1196. btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb_rewin),
  1197. eb_rewin, btrfs_header_level(eb_rewin));
  1198. btrfs_tree_read_lock(eb_rewin);
  1199. __tree_mod_log_rewind(fs_info, eb_rewin, time_seq, tm);
  1200. WARN_ON(btrfs_header_nritems(eb_rewin) >
  1201. BTRFS_NODEPTRS_PER_BLOCK(fs_info));
  1202. return eb_rewin;
  1203. }
  1204. /*
  1205. * get_old_root() rewinds the state of @root's root node to the given @time_seq
  1206. * value. If there are no changes, the current root->root_node is returned. If
  1207. * anything changed in between, there's a fresh buffer allocated on which the
  1208. * rewind operations are done. In any case, the returned buffer is read locked.
  1209. * Returns NULL on error (with no locks held).
  1210. */
  1211. static inline struct extent_buffer *
  1212. get_old_root(struct btrfs_root *root, u64 time_seq)
  1213. {
  1214. struct btrfs_fs_info *fs_info = root->fs_info;
  1215. struct tree_mod_elem *tm;
  1216. struct extent_buffer *eb = NULL;
  1217. struct extent_buffer *eb_root;
  1218. u64 eb_root_owner = 0;
  1219. struct extent_buffer *old;
  1220. struct tree_mod_root *old_root = NULL;
  1221. u64 old_generation = 0;
  1222. u64 logical;
  1223. int level;
  1224. eb_root = btrfs_read_lock_root_node(root);
  1225. tm = __tree_mod_log_oldest_root(eb_root, time_seq);
  1226. if (!tm)
  1227. return eb_root;
  1228. if (tm->op == MOD_LOG_ROOT_REPLACE) {
  1229. old_root = &tm->old_root;
  1230. old_generation = tm->generation;
  1231. logical = old_root->logical;
  1232. level = old_root->level;
  1233. } else {
  1234. logical = eb_root->start;
  1235. level = btrfs_header_level(eb_root);
  1236. }
  1237. tm = tree_mod_log_search(fs_info, logical, time_seq);
  1238. if (old_root && tm && tm->op != MOD_LOG_KEY_REMOVE_WHILE_FREEING) {
  1239. btrfs_tree_read_unlock(eb_root);
  1240. free_extent_buffer(eb_root);
  1241. old = read_tree_block(fs_info, logical, 0, level, NULL);
  1242. if (WARN_ON(IS_ERR(old) || !extent_buffer_uptodate(old))) {
  1243. if (!IS_ERR(old))
  1244. free_extent_buffer(old);
  1245. btrfs_warn(fs_info,
  1246. "failed to read tree block %llu from get_old_root",
  1247. logical);
  1248. } else {
  1249. struct tree_mod_elem *tm2;
  1250. btrfs_tree_read_lock(old);
  1251. eb = btrfs_clone_extent_buffer(old);
  1252. /*
  1253. * After the lookup for the most recent tree mod operation
  1254. * above and before we locked and cloned the extent buffer
  1255. * 'old', a new tree mod log operation may have been added.
  1256. * So lookup for a more recent one to make sure the number
  1257. * of mod log operations we replay is consistent with the
  1258. * number of items we have in the cloned extent buffer,
  1259. * otherwise we can hit a BUG_ON when rewinding the extent
  1260. * buffer.
  1261. */
  1262. tm2 = tree_mod_log_search(fs_info, logical, time_seq);
  1263. btrfs_tree_read_unlock(old);
  1264. free_extent_buffer(old);
  1265. ASSERT(tm2);
  1266. ASSERT(tm2 == tm || tm2->seq > tm->seq);
  1267. if (!tm2 || tm2->seq < tm->seq) {
  1268. free_extent_buffer(eb);
  1269. return NULL;
  1270. }
  1271. tm = tm2;
  1272. }
  1273. } else if (old_root) {
  1274. eb_root_owner = btrfs_header_owner(eb_root);
  1275. btrfs_tree_read_unlock(eb_root);
  1276. free_extent_buffer(eb_root);
  1277. eb = alloc_dummy_extent_buffer(fs_info, logical);
  1278. } else {
  1279. btrfs_set_lock_blocking_rw(eb_root, BTRFS_READ_LOCK);
  1280. eb = btrfs_clone_extent_buffer(eb_root);
  1281. btrfs_tree_read_unlock_blocking(eb_root);
  1282. free_extent_buffer(eb_root);
  1283. }
  1284. if (!eb)
  1285. return NULL;
  1286. if (old_root) {
  1287. btrfs_set_header_bytenr(eb, eb->start);
  1288. btrfs_set_header_backref_rev(eb, BTRFS_MIXED_BACKREF_REV);
  1289. btrfs_set_header_owner(eb, eb_root_owner);
  1290. btrfs_set_header_level(eb, old_root->level);
  1291. btrfs_set_header_generation(eb, old_generation);
  1292. }
  1293. btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb), eb,
  1294. btrfs_header_level(eb));
  1295. btrfs_tree_read_lock(eb);
  1296. if (tm)
  1297. __tree_mod_log_rewind(fs_info, eb, time_seq, tm);
  1298. else
  1299. WARN_ON(btrfs_header_level(eb) != 0);
  1300. WARN_ON(btrfs_header_nritems(eb) > BTRFS_NODEPTRS_PER_BLOCK(fs_info));
  1301. return eb;
  1302. }
  1303. int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq)
  1304. {
  1305. struct tree_mod_elem *tm;
  1306. int level;
  1307. struct extent_buffer *eb_root = btrfs_root_node(root);
  1308. tm = __tree_mod_log_oldest_root(eb_root, time_seq);
  1309. if (tm && tm->op == MOD_LOG_ROOT_REPLACE) {
  1310. level = tm->old_root.level;
  1311. } else {
  1312. level = btrfs_header_level(eb_root);
  1313. }
  1314. free_extent_buffer(eb_root);
  1315. return level;
  1316. }
  1317. static inline int should_cow_block(struct btrfs_trans_handle *trans,
  1318. struct btrfs_root *root,
  1319. struct extent_buffer *buf)
  1320. {
  1321. if (btrfs_is_testing(root->fs_info))
  1322. return 0;
  1323. /* Ensure we can see the FORCE_COW bit */
  1324. smp_mb__before_atomic();
  1325. /*
  1326. * We do not need to cow a block if
  1327. * 1) this block is not created or changed in this transaction;
  1328. * 2) this block does not belong to TREE_RELOC tree;
  1329. * 3) the root is not forced COW.
  1330. *
  1331. * What is forced COW:
  1332. * when we create snapshot during committing the transaction,
  1333. * after we've finished coping src root, we must COW the shared
  1334. * block to ensure the metadata consistency.
  1335. */
  1336. if (btrfs_header_generation(buf) == trans->transid &&
  1337. !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN) &&
  1338. !(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
  1339. btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC)) &&
  1340. !test_bit(BTRFS_ROOT_FORCE_COW, &root->state))
  1341. return 0;
  1342. return 1;
  1343. }
  1344. /*
  1345. * cows a single block, see __btrfs_cow_block for the real work.
  1346. * This version of it has extra checks so that a block isn't COWed more than
  1347. * once per transaction, as long as it hasn't been written yet
  1348. */
  1349. noinline int btrfs_cow_block(struct btrfs_trans_handle *trans,
  1350. struct btrfs_root *root, struct extent_buffer *buf,
  1351. struct extent_buffer *parent, int parent_slot,
  1352. struct extent_buffer **cow_ret)
  1353. {
  1354. struct btrfs_fs_info *fs_info = root->fs_info;
  1355. u64 search_start;
  1356. int ret;
  1357. if (trans->transaction != fs_info->running_transaction)
  1358. WARN(1, KERN_CRIT "trans %llu running %llu\n",
  1359. trans->transid,
  1360. fs_info->running_transaction->transid);
  1361. if (trans->transid != fs_info->generation)
  1362. WARN(1, KERN_CRIT "trans %llu running %llu\n",
  1363. trans->transid, fs_info->generation);
  1364. if (!should_cow_block(trans, root, buf)) {
  1365. trans->dirty = true;
  1366. *cow_ret = buf;
  1367. return 0;
  1368. }
  1369. search_start = buf->start & ~((u64)SZ_1G - 1);
  1370. if (parent)
  1371. btrfs_set_lock_blocking(parent);
  1372. btrfs_set_lock_blocking(buf);
  1373. ret = __btrfs_cow_block(trans, root, buf, parent,
  1374. parent_slot, cow_ret, search_start, 0);
  1375. trace_btrfs_cow_block(root, buf, *cow_ret);
  1376. return ret;
  1377. }
  1378. /*
  1379. * helper function for defrag to decide if two blocks pointed to by a
  1380. * node are actually close by
  1381. */
  1382. static int close_blocks(u64 blocknr, u64 other, u32 blocksize)
  1383. {
  1384. if (blocknr < other && other - (blocknr + blocksize) < 32768)
  1385. return 1;
  1386. if (blocknr > other && blocknr - (other + blocksize) < 32768)
  1387. return 1;
  1388. return 0;
  1389. }
  1390. /*
  1391. * compare two keys in a memcmp fashion
  1392. */
  1393. static int comp_keys(const struct btrfs_disk_key *disk,
  1394. const struct btrfs_key *k2)
  1395. {
  1396. struct btrfs_key k1;
  1397. btrfs_disk_key_to_cpu(&k1, disk);
  1398. return btrfs_comp_cpu_keys(&k1, k2);
  1399. }
  1400. /*
  1401. * same as comp_keys only with two btrfs_key's
  1402. */
  1403. int btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2)
  1404. {
  1405. if (k1->objectid > k2->objectid)
  1406. return 1;
  1407. if (k1->objectid < k2->objectid)
  1408. return -1;
  1409. if (k1->type > k2->type)
  1410. return 1;
  1411. if (k1->type < k2->type)
  1412. return -1;
  1413. if (k1->offset > k2->offset)
  1414. return 1;
  1415. if (k1->offset < k2->offset)
  1416. return -1;
  1417. return 0;
  1418. }
  1419. /*
  1420. * this is used by the defrag code to go through all the
  1421. * leaves pointed to by a node and reallocate them so that
  1422. * disk order is close to key order
  1423. */
  1424. int btrfs_realloc_node(struct btrfs_trans_handle *trans,
  1425. struct btrfs_root *root, struct extent_buffer *parent,
  1426. int start_slot, u64 *last_ret,
  1427. struct btrfs_key *progress)
  1428. {
  1429. struct btrfs_fs_info *fs_info = root->fs_info;
  1430. struct extent_buffer *cur;
  1431. u64 blocknr;
  1432. u64 gen;
  1433. u64 search_start = *last_ret;
  1434. u64 last_block = 0;
  1435. u64 other;
  1436. u32 parent_nritems;
  1437. int end_slot;
  1438. int i;
  1439. int err = 0;
  1440. int parent_level;
  1441. int uptodate;
  1442. u32 blocksize;
  1443. int progress_passed = 0;
  1444. struct btrfs_disk_key disk_key;
  1445. parent_level = btrfs_header_level(parent);
  1446. WARN_ON(trans->transaction != fs_info->running_transaction);
  1447. WARN_ON(trans->transid != fs_info->generation);
  1448. parent_nritems = btrfs_header_nritems(parent);
  1449. blocksize = fs_info->nodesize;
  1450. end_slot = parent_nritems - 1;
  1451. if (parent_nritems <= 1)
  1452. return 0;
  1453. btrfs_set_lock_blocking(parent);
  1454. for (i = start_slot; i <= end_slot; i++) {
  1455. struct btrfs_key first_key;
  1456. int close = 1;
  1457. btrfs_node_key(parent, &disk_key, i);
  1458. if (!progress_passed && comp_keys(&disk_key, progress) < 0)
  1459. continue;
  1460. progress_passed = 1;
  1461. blocknr = btrfs_node_blockptr(parent, i);
  1462. gen = btrfs_node_ptr_generation(parent, i);
  1463. btrfs_node_key_to_cpu(parent, &first_key, i);
  1464. if (last_block == 0)
  1465. last_block = blocknr;
  1466. if (i > 0) {
  1467. other = btrfs_node_blockptr(parent, i - 1);
  1468. close = close_blocks(blocknr, other, blocksize);
  1469. }
  1470. if (!close && i < end_slot) {
  1471. other = btrfs_node_blockptr(parent, i + 1);
  1472. close = close_blocks(blocknr, other, blocksize);
  1473. }
  1474. if (close) {
  1475. last_block = blocknr;
  1476. continue;
  1477. }
  1478. cur = find_extent_buffer(fs_info, blocknr);
  1479. if (cur)
  1480. uptodate = btrfs_buffer_uptodate(cur, gen, 0);
  1481. else
  1482. uptodate = 0;
  1483. if (!cur || !uptodate) {
  1484. if (!cur) {
  1485. cur = read_tree_block(fs_info, blocknr, gen,
  1486. parent_level - 1,
  1487. &first_key);
  1488. if (IS_ERR(cur)) {
  1489. return PTR_ERR(cur);
  1490. } else if (!extent_buffer_uptodate(cur)) {
  1491. free_extent_buffer(cur);
  1492. return -EIO;
  1493. }
  1494. } else if (!uptodate) {
  1495. err = btrfs_read_buffer(cur, gen,
  1496. parent_level - 1,&first_key);
  1497. if (err) {
  1498. free_extent_buffer(cur);
  1499. return err;
  1500. }
  1501. }
  1502. }
  1503. if (search_start == 0)
  1504. search_start = last_block;
  1505. btrfs_tree_lock(cur);
  1506. btrfs_set_lock_blocking(cur);
  1507. err = __btrfs_cow_block(trans, root, cur, parent, i,
  1508. &cur, search_start,
  1509. min(16 * blocksize,
  1510. (end_slot - i) * blocksize));
  1511. if (err) {
  1512. btrfs_tree_unlock(cur);
  1513. free_extent_buffer(cur);
  1514. break;
  1515. }
  1516. search_start = cur->start;
  1517. last_block = cur->start;
  1518. *last_ret = search_start;
  1519. btrfs_tree_unlock(cur);
  1520. free_extent_buffer(cur);
  1521. }
  1522. return err;
  1523. }
  1524. /*
  1525. * search for key in the extent_buffer. The items start at offset p,
  1526. * and they are item_size apart. There are 'max' items in p.
  1527. *
  1528. * the slot in the array is returned via slot, and it points to
  1529. * the place where you would insert key if it is not found in
  1530. * the array.
  1531. *
  1532. * slot may point to max if the key is bigger than all of the keys
  1533. */
  1534. static noinline int generic_bin_search(struct extent_buffer *eb,
  1535. unsigned long p, int item_size,
  1536. const struct btrfs_key *key,
  1537. int max, int *slot)
  1538. {
  1539. int low = 0;
  1540. int high = max;
  1541. int mid;
  1542. int ret;
  1543. struct btrfs_disk_key *tmp = NULL;
  1544. struct btrfs_disk_key unaligned;
  1545. unsigned long offset;
  1546. char *kaddr = NULL;
  1547. unsigned long map_start = 0;
  1548. unsigned long map_len = 0;
  1549. int err;
  1550. if (low > high) {
  1551. btrfs_err(eb->fs_info,
  1552. "%s: low (%d) > high (%d) eb %llu owner %llu level %d",
  1553. __func__, low, high, eb->start,
  1554. btrfs_header_owner(eb), btrfs_header_level(eb));
  1555. return -EINVAL;
  1556. }
  1557. while (low < high) {
  1558. mid = (low + high) / 2;
  1559. offset = p + mid * item_size;
  1560. if (!kaddr || offset < map_start ||
  1561. (offset + sizeof(struct btrfs_disk_key)) >
  1562. map_start + map_len) {
  1563. err = map_private_extent_buffer(eb, offset,
  1564. sizeof(struct btrfs_disk_key),
  1565. &kaddr, &map_start, &map_len);
  1566. if (!err) {
  1567. tmp = (struct btrfs_disk_key *)(kaddr + offset -
  1568. map_start);
  1569. } else if (err == 1) {
  1570. read_extent_buffer(eb, &unaligned,
  1571. offset, sizeof(unaligned));
  1572. tmp = &unaligned;
  1573. } else {
  1574. return err;
  1575. }
  1576. } else {
  1577. tmp = (struct btrfs_disk_key *)(kaddr + offset -
  1578. map_start);
  1579. }
  1580. ret = comp_keys(tmp, key);
  1581. if (ret < 0)
  1582. low = mid + 1;
  1583. else if (ret > 0)
  1584. high = mid;
  1585. else {
  1586. *slot = mid;
  1587. return 0;
  1588. }
  1589. }
  1590. *slot = low;
  1591. return 1;
  1592. }
  1593. /*
  1594. * simple bin_search frontend that does the right thing for
  1595. * leaves vs nodes
  1596. */
  1597. int btrfs_bin_search(struct extent_buffer *eb, const struct btrfs_key *key,
  1598. int level, int *slot)
  1599. {
  1600. if (level == 0)
  1601. return generic_bin_search(eb,
  1602. offsetof(struct btrfs_leaf, items),
  1603. sizeof(struct btrfs_item),
  1604. key, btrfs_header_nritems(eb),
  1605. slot);
  1606. else
  1607. return generic_bin_search(eb,
  1608. offsetof(struct btrfs_node, ptrs),
  1609. sizeof(struct btrfs_key_ptr),
  1610. key, btrfs_header_nritems(eb),
  1611. slot);
  1612. }
  1613. static void root_add_used(struct btrfs_root *root, u32 size)
  1614. {
  1615. spin_lock(&root->accounting_lock);
  1616. btrfs_set_root_used(&root->root_item,
  1617. btrfs_root_used(&root->root_item) + size);
  1618. spin_unlock(&root->accounting_lock);
  1619. }
  1620. static void root_sub_used(struct btrfs_root *root, u32 size)
  1621. {
  1622. spin_lock(&root->accounting_lock);
  1623. btrfs_set_root_used(&root->root_item,
  1624. btrfs_root_used(&root->root_item) - size);
  1625. spin_unlock(&root->accounting_lock);
  1626. }
  1627. /* given a node and slot number, this reads the blocks it points to. The
  1628. * extent buffer is returned with a reference taken (but unlocked).
  1629. */
  1630. static noinline struct extent_buffer *
  1631. read_node_slot(struct btrfs_fs_info *fs_info, struct extent_buffer *parent,
  1632. int slot)
  1633. {
  1634. int level = btrfs_header_level(parent);
  1635. struct extent_buffer *eb;
  1636. struct btrfs_key first_key;
  1637. if (slot < 0 || slot >= btrfs_header_nritems(parent))
  1638. return ERR_PTR(-ENOENT);
  1639. BUG_ON(level == 0);
  1640. btrfs_node_key_to_cpu(parent, &first_key, slot);
  1641. eb = read_tree_block(fs_info, btrfs_node_blockptr(parent, slot),
  1642. btrfs_node_ptr_generation(parent, slot),
  1643. level - 1, &first_key);
  1644. if (!IS_ERR(eb) && !extent_buffer_uptodate(eb)) {
  1645. free_extent_buffer(eb);
  1646. eb = ERR_PTR(-EIO);
  1647. }
  1648. return eb;
  1649. }
  1650. /*
  1651. * node level balancing, used to make sure nodes are in proper order for
  1652. * item deletion. We balance from the top down, so we have to make sure
  1653. * that a deletion won't leave an node completely empty later on.
  1654. */
  1655. static noinline int balance_level(struct btrfs_trans_handle *trans,
  1656. struct btrfs_root *root,
  1657. struct btrfs_path *path, int level)
  1658. {
  1659. struct btrfs_fs_info *fs_info = root->fs_info;
  1660. struct extent_buffer *right = NULL;
  1661. struct extent_buffer *mid;
  1662. struct extent_buffer *left = NULL;
  1663. struct extent_buffer *parent = NULL;
  1664. int ret = 0;
  1665. int wret;
  1666. int pslot;
  1667. int orig_slot = path->slots[level];
  1668. u64 orig_ptr;
  1669. if (level == 0)
  1670. return 0;
  1671. mid = path->nodes[level];
  1672. WARN_ON(path->locks[level] != BTRFS_WRITE_LOCK &&
  1673. path->locks[level] != BTRFS_WRITE_LOCK_BLOCKING);
  1674. WARN_ON(btrfs_header_generation(mid) != trans->transid);
  1675. orig_ptr = btrfs_node_blockptr(mid, orig_slot);
  1676. if (level < BTRFS_MAX_LEVEL - 1) {
  1677. parent = path->nodes[level + 1];
  1678. pslot = path->slots[level + 1];
  1679. }
  1680. /*
  1681. * deal with the case where there is only one pointer in the root
  1682. * by promoting the node below to a root
  1683. */
  1684. if (!parent) {
  1685. struct extent_buffer *child;
  1686. if (btrfs_header_nritems(mid) != 1)
  1687. return 0;
  1688. /* promote the child to a root */
  1689. child = read_node_slot(fs_info, mid, 0);
  1690. if (IS_ERR(child)) {
  1691. ret = PTR_ERR(child);
  1692. btrfs_handle_fs_error(fs_info, ret, NULL);
  1693. goto enospc;
  1694. }
  1695. btrfs_tree_lock(child);
  1696. btrfs_set_lock_blocking(child);
  1697. ret = btrfs_cow_block(trans, root, child, mid, 0, &child);
  1698. if (ret) {
  1699. btrfs_tree_unlock(child);
  1700. free_extent_buffer(child);
  1701. goto enospc;
  1702. }
  1703. ret = tree_mod_log_insert_root(root->node, child, 1);
  1704. BUG_ON(ret < 0);
  1705. rcu_assign_pointer(root->node, child);
  1706. add_root_to_dirty_list(root);
  1707. btrfs_tree_unlock(child);
  1708. path->locks[level] = 0;
  1709. path->nodes[level] = NULL;
  1710. clean_tree_block(fs_info, mid);
  1711. btrfs_tree_unlock(mid);
  1712. /* once for the path */
  1713. free_extent_buffer(mid);
  1714. root_sub_used(root, mid->len);
  1715. btrfs_free_tree_block(trans, root, mid, 0, 1);
  1716. /* once for the root ptr */
  1717. free_extent_buffer_stale(mid);
  1718. return 0;
  1719. }
  1720. if (btrfs_header_nritems(mid) >
  1721. BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 4)
  1722. return 0;
  1723. left = read_node_slot(fs_info, parent, pslot - 1);
  1724. if (IS_ERR(left))
  1725. left = NULL;
  1726. if (left) {
  1727. btrfs_tree_lock(left);
  1728. btrfs_set_lock_blocking(left);
  1729. wret = btrfs_cow_block(trans, root, left,
  1730. parent, pslot - 1, &left);
  1731. if (wret) {
  1732. ret = wret;
  1733. goto enospc;
  1734. }
  1735. }
  1736. right = read_node_slot(fs_info, parent, pslot + 1);
  1737. if (IS_ERR(right))
  1738. right = NULL;
  1739. if (right) {
  1740. btrfs_tree_lock(right);
  1741. btrfs_set_lock_blocking(right);
  1742. wret = btrfs_cow_block(trans, root, right,
  1743. parent, pslot + 1, &right);
  1744. if (wret) {
  1745. ret = wret;
  1746. goto enospc;
  1747. }
  1748. }
  1749. /* first, try to make some room in the middle buffer */
  1750. if (left) {
  1751. orig_slot += btrfs_header_nritems(left);
  1752. wret = push_node_left(trans, fs_info, left, mid, 1);
  1753. if (wret < 0)
  1754. ret = wret;
  1755. }
  1756. /*
  1757. * then try to empty the right most buffer into the middle
  1758. */
  1759. if (right) {
  1760. wret = push_node_left(trans, fs_info, mid, right, 1);
  1761. if (wret < 0 && wret != -ENOSPC)
  1762. ret = wret;
  1763. if (btrfs_header_nritems(right) == 0) {
  1764. clean_tree_block(fs_info, right);
  1765. btrfs_tree_unlock(right);
  1766. del_ptr(root, path, level + 1, pslot + 1);
  1767. root_sub_used(root, right->len);
  1768. btrfs_free_tree_block(trans, root, right, 0, 1);
  1769. free_extent_buffer_stale(right);
  1770. right = NULL;
  1771. } else {
  1772. struct btrfs_disk_key right_key;
  1773. btrfs_node_key(right, &right_key, 0);
  1774. ret = tree_mod_log_insert_key(parent, pslot + 1,
  1775. MOD_LOG_KEY_REPLACE, GFP_NOFS);
  1776. BUG_ON(ret < 0);
  1777. btrfs_set_node_key(parent, &right_key, pslot + 1);
  1778. btrfs_mark_buffer_dirty(parent);
  1779. }
  1780. }
  1781. if (btrfs_header_nritems(mid) == 1) {
  1782. /*
  1783. * we're not allowed to leave a node with one item in the
  1784. * tree during a delete. A deletion from lower in the tree
  1785. * could try to delete the only pointer in this node.
  1786. * So, pull some keys from the left.
  1787. * There has to be a left pointer at this point because
  1788. * otherwise we would have pulled some pointers from the
  1789. * right
  1790. */
  1791. if (!left) {
  1792. ret = -EROFS;
  1793. btrfs_handle_fs_error(fs_info, ret, NULL);
  1794. goto enospc;
  1795. }
  1796. wret = balance_node_right(trans, fs_info, mid, left);
  1797. if (wret < 0) {
  1798. ret = wret;
  1799. goto enospc;
  1800. }
  1801. if (wret == 1) {
  1802. wret = push_node_left(trans, fs_info, left, mid, 1);
  1803. if (wret < 0)
  1804. ret = wret;
  1805. }
  1806. BUG_ON(wret == 1);
  1807. }
  1808. if (btrfs_header_nritems(mid) == 0) {
  1809. clean_tree_block(fs_info, mid);
  1810. btrfs_tree_unlock(mid);
  1811. del_ptr(root, path, level + 1, pslot);
  1812. root_sub_used(root, mid->len);
  1813. btrfs_free_tree_block(trans, root, mid, 0, 1);
  1814. free_extent_buffer_stale(mid);
  1815. mid = NULL;
  1816. } else {
  1817. /* update the parent key to reflect our changes */
  1818. struct btrfs_disk_key mid_key;
  1819. btrfs_node_key(mid, &mid_key, 0);
  1820. ret = tree_mod_log_insert_key(parent, pslot,
  1821. MOD_LOG_KEY_REPLACE, GFP_NOFS);
  1822. BUG_ON(ret < 0);
  1823. btrfs_set_node_key(parent, &mid_key, pslot);
  1824. btrfs_mark_buffer_dirty(parent);
  1825. }
  1826. /* update the path */
  1827. if (left) {
  1828. if (btrfs_header_nritems(left) > orig_slot) {
  1829. extent_buffer_get(left);
  1830. /* left was locked after cow */
  1831. path->nodes[level] = left;
  1832. path->slots[level + 1] -= 1;
  1833. path->slots[level] = orig_slot;
  1834. if (mid) {
  1835. btrfs_tree_unlock(mid);
  1836. free_extent_buffer(mid);
  1837. }
  1838. } else {
  1839. orig_slot -= btrfs_header_nritems(left);
  1840. path->slots[level] = orig_slot;
  1841. }
  1842. }
  1843. /* double check we haven't messed things up */
  1844. if (orig_ptr !=
  1845. btrfs_node_blockptr(path->nodes[level], path->slots[level]))
  1846. BUG();
  1847. enospc:
  1848. if (right) {
  1849. btrfs_tree_unlock(right);
  1850. free_extent_buffer(right);
  1851. }
  1852. if (left) {
  1853. if (path->nodes[level] != left)
  1854. btrfs_tree_unlock(left);
  1855. free_extent_buffer(left);
  1856. }
  1857. return ret;
  1858. }
  1859. /* Node balancing for insertion. Here we only split or push nodes around
  1860. * when they are completely full. This is also done top down, so we
  1861. * have to be pessimistic.
  1862. */
  1863. static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
  1864. struct btrfs_root *root,
  1865. struct btrfs_path *path, int level)
  1866. {
  1867. struct btrfs_fs_info *fs_info = root->fs_info;
  1868. struct extent_buffer *right = NULL;
  1869. struct extent_buffer *mid;
  1870. struct extent_buffer *left = NULL;
  1871. struct extent_buffer *parent = NULL;
  1872. int ret = 0;
  1873. int wret;
  1874. int pslot;
  1875. int orig_slot = path->slots[level];
  1876. if (level == 0)
  1877. return 1;
  1878. mid = path->nodes[level];
  1879. WARN_ON(btrfs_header_generation(mid) != trans->transid);
  1880. if (level < BTRFS_MAX_LEVEL - 1) {
  1881. parent = path->nodes[level + 1];
  1882. pslot = path->slots[level + 1];
  1883. }
  1884. if (!parent)
  1885. return 1;
  1886. left = read_node_slot(fs_info, parent, pslot - 1);
  1887. if (IS_ERR(left))
  1888. left = NULL;
  1889. /* first, try to make some room in the middle buffer */
  1890. if (left) {
  1891. u32 left_nr;
  1892. btrfs_tree_lock(left);
  1893. btrfs_set_lock_blocking(left);
  1894. left_nr = btrfs_header_nritems(left);
  1895. if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
  1896. wret = 1;
  1897. } else {
  1898. ret = btrfs_cow_block(trans, root, left, parent,
  1899. pslot - 1, &left);
  1900. if (ret)
  1901. wret = 1;
  1902. else {
  1903. wret = push_node_left(trans, fs_info,
  1904. left, mid, 0);
  1905. }
  1906. }
  1907. if (wret < 0)
  1908. ret = wret;
  1909. if (wret == 0) {
  1910. struct btrfs_disk_key disk_key;
  1911. orig_slot += left_nr;
  1912. btrfs_node_key(mid, &disk_key, 0);
  1913. ret = tree_mod_log_insert_key(parent, pslot,
  1914. MOD_LOG_KEY_REPLACE, GFP_NOFS);
  1915. BUG_ON(ret < 0);
  1916. btrfs_set_node_key(parent, &disk_key, pslot);
  1917. btrfs_mark_buffer_dirty(parent);
  1918. if (btrfs_header_nritems(left) > orig_slot) {
  1919. path->nodes[level] = left;
  1920. path->slots[level + 1] -= 1;
  1921. path->slots[level] = orig_slot;
  1922. btrfs_tree_unlock(mid);
  1923. free_extent_buffer(mid);
  1924. } else {
  1925. orig_slot -=
  1926. btrfs_header_nritems(left);
  1927. path->slots[level] = orig_slot;
  1928. btrfs_tree_unlock(left);
  1929. free_extent_buffer(left);
  1930. }
  1931. return 0;
  1932. }
  1933. btrfs_tree_unlock(left);
  1934. free_extent_buffer(left);
  1935. }
  1936. right = read_node_slot(fs_info, parent, pslot + 1);
  1937. if (IS_ERR(right))
  1938. right = NULL;
  1939. /*
  1940. * then try to empty the right most buffer into the middle
  1941. */
  1942. if (right) {
  1943. u32 right_nr;
  1944. btrfs_tree_lock(right);
  1945. btrfs_set_lock_blocking(right);
  1946. right_nr = btrfs_header_nritems(right);
  1947. if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 1) {
  1948. wret = 1;
  1949. } else {
  1950. ret = btrfs_cow_block(trans, root, right,
  1951. parent, pslot + 1,
  1952. &right);
  1953. if (ret)
  1954. wret = 1;
  1955. else {
  1956. wret = balance_node_right(trans, fs_info,
  1957. right, mid);
  1958. }
  1959. }
  1960. if (wret < 0)
  1961. ret = wret;
  1962. if (wret == 0) {
  1963. struct btrfs_disk_key disk_key;
  1964. btrfs_node_key(right, &disk_key, 0);
  1965. ret = tree_mod_log_insert_key(parent, pslot + 1,
  1966. MOD_LOG_KEY_REPLACE, GFP_NOFS);
  1967. BUG_ON(ret < 0);
  1968. btrfs_set_node_key(parent, &disk_key, pslot + 1);
  1969. btrfs_mark_buffer_dirty(parent);
  1970. if (btrfs_header_nritems(mid) <= orig_slot) {
  1971. path->nodes[level] = right;
  1972. path->slots[level + 1] += 1;
  1973. path->slots[level] = orig_slot -
  1974. btrfs_header_nritems(mid);
  1975. btrfs_tree_unlock(mid);
  1976. free_extent_buffer(mid);
  1977. } else {
  1978. btrfs_tree_unlock(right);
  1979. free_extent_buffer(right);
  1980. }
  1981. return 0;
  1982. }
  1983. btrfs_tree_unlock(right);
  1984. free_extent_buffer(right);
  1985. }
  1986. return 1;
  1987. }
  1988. /*
  1989. * readahead one full node of leaves, finding things that are close
  1990. * to the block in 'slot', and triggering ra on them.
  1991. */
  1992. static void reada_for_search(struct btrfs_fs_info *fs_info,
  1993. struct btrfs_path *path,
  1994. int level, int slot, u64 objectid)
  1995. {
  1996. struct extent_buffer *node;
  1997. struct btrfs_disk_key disk_key;
  1998. u32 nritems;
  1999. u64 search;
  2000. u64 target;
  2001. u64 nread = 0;
  2002. struct extent_buffer *eb;
  2003. u32 nr;
  2004. u32 blocksize;
  2005. u32 nscan = 0;
  2006. if (level != 1)
  2007. return;
  2008. if (!path->nodes[level])
  2009. return;
  2010. node = path->nodes[level];
  2011. search = btrfs_node_blockptr(node, slot);
  2012. blocksize = fs_info->nodesize;
  2013. eb = find_extent_buffer(fs_info, search);
  2014. if (eb) {
  2015. free_extent_buffer(eb);
  2016. return;
  2017. }
  2018. target = search;
  2019. nritems = btrfs_header_nritems(node);
  2020. nr = slot;
  2021. while (1) {
  2022. if (path->reada == READA_BACK) {
  2023. if (nr == 0)
  2024. break;
  2025. nr--;
  2026. } else if (path->reada == READA_FORWARD) {
  2027. nr++;
  2028. if (nr >= nritems)
  2029. break;
  2030. }
  2031. if (path->reada == READA_BACK && objectid) {
  2032. btrfs_node_key(node, &disk_key, nr);
  2033. if (btrfs_disk_key_objectid(&disk_key) != objectid)
  2034. break;
  2035. }
  2036. search = btrfs_node_blockptr(node, nr);
  2037. if ((search <= target && target - search <= 65536) ||
  2038. (search > target && search - target <= 65536)) {
  2039. readahead_tree_block(fs_info, search);
  2040. nread += blocksize;
  2041. }
  2042. nscan++;
  2043. if ((nread > 65536 || nscan > 32))
  2044. break;
  2045. }
  2046. }
  2047. static noinline void reada_for_balance(struct btrfs_fs_info *fs_info,
  2048. struct btrfs_path *path, int level)
  2049. {
  2050. int slot;
  2051. int nritems;
  2052. struct extent_buffer *parent;
  2053. struct extent_buffer *eb;
  2054. u64 gen;
  2055. u64 block1 = 0;
  2056. u64 block2 = 0;
  2057. parent = path->nodes[level + 1];
  2058. if (!parent)
  2059. return;
  2060. nritems = btrfs_header_nritems(parent);
  2061. slot = path->slots[level + 1];
  2062. if (slot > 0) {
  2063. block1 = btrfs_node_blockptr(parent, slot - 1);
  2064. gen = btrfs_node_ptr_generation(parent, slot - 1);
  2065. eb = find_extent_buffer(fs_info, block1);
  2066. /*
  2067. * if we get -eagain from btrfs_buffer_uptodate, we
  2068. * don't want to return eagain here. That will loop
  2069. * forever
  2070. */
  2071. if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
  2072. block1 = 0;
  2073. free_extent_buffer(eb);
  2074. }
  2075. if (slot + 1 < nritems) {
  2076. block2 = btrfs_node_blockptr(parent, slot + 1);
  2077. gen = btrfs_node_ptr_generation(parent, slot + 1);
  2078. eb = find_extent_buffer(fs_info, block2);
  2079. if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
  2080. block2 = 0;
  2081. free_extent_buffer(eb);
  2082. }
  2083. if (block1)
  2084. readahead_tree_block(fs_info, block1);
  2085. if (block2)
  2086. readahead_tree_block(fs_info, block2);
  2087. }
  2088. /*
  2089. * when we walk down the tree, it is usually safe to unlock the higher layers
  2090. * in the tree. The exceptions are when our path goes through slot 0, because
  2091. * operations on the tree might require changing key pointers higher up in the
  2092. * tree.
  2093. *
  2094. * callers might also have set path->keep_locks, which tells this code to keep
  2095. * the lock if the path points to the last slot in the block. This is part of
  2096. * walking through the tree, and selecting the next slot in the higher block.
  2097. *
  2098. * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
  2099. * if lowest_unlock is 1, level 0 won't be unlocked
  2100. */
  2101. static noinline void unlock_up(struct btrfs_path *path, int level,
  2102. int lowest_unlock, int min_write_lock_level,
  2103. int *write_lock_level)
  2104. {
  2105. int i;
  2106. int skip_level = level;
  2107. int no_skips = 0;
  2108. struct extent_buffer *t;
  2109. for (i = level; i < BTRFS_MAX_LEVEL; i++) {
  2110. if (!path->nodes[i])
  2111. break;
  2112. if (!path->locks[i])
  2113. break;
  2114. if (!no_skips && path->slots[i] == 0) {
  2115. skip_level = i + 1;
  2116. continue;
  2117. }
  2118. if (!no_skips && path->keep_locks) {
  2119. u32 nritems;
  2120. t = path->nodes[i];
  2121. nritems = btrfs_header_nritems(t);
  2122. if (nritems < 1 || path->slots[i] >= nritems - 1) {
  2123. skip_level = i + 1;
  2124. continue;
  2125. }
  2126. }
  2127. if (skip_level < i && i >= lowest_unlock)
  2128. no_skips = 1;
  2129. t = path->nodes[i];
  2130. if (i >= lowest_unlock && i > skip_level) {
  2131. btrfs_tree_unlock_rw(t, path->locks[i]);
  2132. path->locks[i] = 0;
  2133. if (write_lock_level &&
  2134. i > min_write_lock_level &&
  2135. i <= *write_lock_level) {
  2136. *write_lock_level = i - 1;
  2137. }
  2138. }
  2139. }
  2140. }
  2141. /*
  2142. * This releases any locks held in the path starting at level and
  2143. * going all the way up to the root.
  2144. *
  2145. * btrfs_search_slot will keep the lock held on higher nodes in a few
  2146. * corner cases, such as COW of the block at slot zero in the node. This
  2147. * ignores those rules, and it should only be called when there are no
  2148. * more updates to be done higher up in the tree.
  2149. */
  2150. noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
  2151. {
  2152. int i;
  2153. if (path->keep_locks)
  2154. return;
  2155. for (i = level; i < BTRFS_MAX_LEVEL; i++) {
  2156. if (!path->nodes[i])
  2157. continue;
  2158. if (!path->locks[i])
  2159. continue;
  2160. btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
  2161. path->locks[i] = 0;
  2162. }
  2163. }
  2164. /*
  2165. * helper function for btrfs_search_slot. The goal is to find a block
  2166. * in cache without setting the path to blocking. If we find the block
  2167. * we return zero and the path is unchanged.
  2168. *
  2169. * If we can't find the block, we set the path blocking and do some
  2170. * reada. -EAGAIN is returned and the search must be repeated.
  2171. */
  2172. static int
  2173. read_block_for_search(struct btrfs_root *root, struct btrfs_path *p,
  2174. struct extent_buffer **eb_ret, int level, int slot,
  2175. const struct btrfs_key *key)
  2176. {
  2177. struct btrfs_fs_info *fs_info = root->fs_info;
  2178. u64 blocknr;
  2179. u64 gen;
  2180. struct extent_buffer *b = *eb_ret;
  2181. struct extent_buffer *tmp;
  2182. struct btrfs_key first_key;
  2183. int ret;
  2184. int parent_level;
  2185. blocknr = btrfs_node_blockptr(b, slot);
  2186. gen = btrfs_node_ptr_generation(b, slot);
  2187. parent_level = btrfs_header_level(b);
  2188. btrfs_node_key_to_cpu(b, &first_key, slot);
  2189. tmp = find_extent_buffer(fs_info, blocknr);
  2190. if (tmp) {
  2191. /* first we do an atomic uptodate check */
  2192. if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
  2193. /*
  2194. * Do extra check for first_key, eb can be stale due to
  2195. * being cached, read from scrub, or have multiple
  2196. * parents (shared tree blocks).
  2197. */
  2198. if (btrfs_verify_level_key(fs_info, tmp,
  2199. parent_level - 1, &first_key, gen)) {
  2200. free_extent_buffer(tmp);
  2201. return -EUCLEAN;
  2202. }
  2203. *eb_ret = tmp;
  2204. return 0;
  2205. }
  2206. /* the pages were up to date, but we failed
  2207. * the generation number check. Do a full
  2208. * read for the generation number that is correct.
  2209. * We must do this without dropping locks so
  2210. * we can trust our generation number
  2211. */
  2212. btrfs_set_path_blocking(p);
  2213. /* now we're allowed to do a blocking uptodate check */
  2214. ret = btrfs_read_buffer(tmp, gen, parent_level - 1, &first_key);
  2215. if (!ret) {
  2216. *eb_ret = tmp;
  2217. return 0;
  2218. }
  2219. free_extent_buffer(tmp);
  2220. btrfs_release_path(p);
  2221. return -EIO;
  2222. }
  2223. /*
  2224. * reduce lock contention at high levels
  2225. * of the btree by dropping locks before
  2226. * we read. Don't release the lock on the current
  2227. * level because we need to walk this node to figure
  2228. * out which blocks to read.
  2229. */
  2230. btrfs_unlock_up_safe(p, level + 1);
  2231. btrfs_set_path_blocking(p);
  2232. if (p->reada != READA_NONE)
  2233. reada_for_search(fs_info, p, level, slot, key->objectid);
  2234. ret = -EAGAIN;
  2235. tmp = read_tree_block(fs_info, blocknr, gen, parent_level - 1,
  2236. &first_key);
  2237. if (!IS_ERR(tmp)) {
  2238. /*
  2239. * If the read above didn't mark this buffer up to date,
  2240. * it will never end up being up to date. Set ret to EIO now
  2241. * and give up so that our caller doesn't loop forever
  2242. * on our EAGAINs.
  2243. */
  2244. if (!extent_buffer_uptodate(tmp))
  2245. ret = -EIO;
  2246. free_extent_buffer(tmp);
  2247. } else {
  2248. ret = PTR_ERR(tmp);
  2249. }
  2250. btrfs_release_path(p);
  2251. return ret;
  2252. }
  2253. /*
  2254. * helper function for btrfs_search_slot. This does all of the checks
  2255. * for node-level blocks and does any balancing required based on
  2256. * the ins_len.
  2257. *
  2258. * If no extra work was required, zero is returned. If we had to
  2259. * drop the path, -EAGAIN is returned and btrfs_search_slot must
  2260. * start over
  2261. */
  2262. static int
  2263. setup_nodes_for_search(struct btrfs_trans_handle *trans,
  2264. struct btrfs_root *root, struct btrfs_path *p,
  2265. struct extent_buffer *b, int level, int ins_len,
  2266. int *write_lock_level)
  2267. {
  2268. struct btrfs_fs_info *fs_info = root->fs_info;
  2269. int ret;
  2270. if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
  2271. BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3) {
  2272. int sret;
  2273. if (*write_lock_level < level + 1) {
  2274. *write_lock_level = level + 1;
  2275. btrfs_release_path(p);
  2276. goto again;
  2277. }
  2278. btrfs_set_path_blocking(p);
  2279. reada_for_balance(fs_info, p, level);
  2280. sret = split_node(trans, root, p, level);
  2281. btrfs_clear_path_blocking(p, NULL, 0);
  2282. BUG_ON(sret > 0);
  2283. if (sret) {
  2284. ret = sret;
  2285. goto done;
  2286. }
  2287. b = p->nodes[level];
  2288. } else if (ins_len < 0 && btrfs_header_nritems(b) <
  2289. BTRFS_NODEPTRS_PER_BLOCK(fs_info) / 2) {
  2290. int sret;
  2291. if (*write_lock_level < level + 1) {
  2292. *write_lock_level = level + 1;
  2293. btrfs_release_path(p);
  2294. goto again;
  2295. }
  2296. btrfs_set_path_blocking(p);
  2297. reada_for_balance(fs_info, p, level);
  2298. sret = balance_level(trans, root, p, level);
  2299. btrfs_clear_path_blocking(p, NULL, 0);
  2300. if (sret) {
  2301. ret = sret;
  2302. goto done;
  2303. }
  2304. b = p->nodes[level];
  2305. if (!b) {
  2306. btrfs_release_path(p);
  2307. goto again;
  2308. }
  2309. BUG_ON(btrfs_header_nritems(b) == 1);
  2310. }
  2311. return 0;
  2312. again:
  2313. ret = -EAGAIN;
  2314. done:
  2315. return ret;
  2316. }
  2317. static void key_search_validate(struct extent_buffer *b,
  2318. const struct btrfs_key *key,
  2319. int level)
  2320. {
  2321. #ifdef CONFIG_BTRFS_ASSERT
  2322. struct btrfs_disk_key disk_key;
  2323. btrfs_cpu_key_to_disk(&disk_key, key);
  2324. if (level == 0)
  2325. ASSERT(!memcmp_extent_buffer(b, &disk_key,
  2326. offsetof(struct btrfs_leaf, items[0].key),
  2327. sizeof(disk_key)));
  2328. else
  2329. ASSERT(!memcmp_extent_buffer(b, &disk_key,
  2330. offsetof(struct btrfs_node, ptrs[0].key),
  2331. sizeof(disk_key)));
  2332. #endif
  2333. }
  2334. static int key_search(struct extent_buffer *b, const struct btrfs_key *key,
  2335. int level, int *prev_cmp, int *slot)
  2336. {
  2337. if (*prev_cmp != 0) {
  2338. *prev_cmp = btrfs_bin_search(b, key, level, slot);
  2339. return *prev_cmp;
  2340. }
  2341. key_search_validate(b, key, level);
  2342. *slot = 0;
  2343. return 0;
  2344. }
  2345. int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path,
  2346. u64 iobjectid, u64 ioff, u8 key_type,
  2347. struct btrfs_key *found_key)
  2348. {
  2349. int ret;
  2350. struct btrfs_key key;
  2351. struct extent_buffer *eb;
  2352. ASSERT(path);
  2353. ASSERT(found_key);
  2354. key.type = key_type;
  2355. key.objectid = iobjectid;
  2356. key.offset = ioff;
  2357. ret = btrfs_search_slot(NULL, fs_root, &key, path, 0, 0);
  2358. if (ret < 0)
  2359. return ret;
  2360. eb = path->nodes[0];
  2361. if (ret && path->slots[0] >= btrfs_header_nritems(eb)) {
  2362. ret = btrfs_next_leaf(fs_root, path);
  2363. if (ret)
  2364. return ret;
  2365. eb = path->nodes[0];
  2366. }
  2367. btrfs_item_key_to_cpu(eb, found_key, path->slots[0]);
  2368. if (found_key->type != key.type ||
  2369. found_key->objectid != key.objectid)
  2370. return 1;
  2371. return 0;
  2372. }
  2373. static struct extent_buffer *btrfs_search_slot_get_root(struct btrfs_root *root,
  2374. struct btrfs_path *p,
  2375. int write_lock_level)
  2376. {
  2377. struct btrfs_fs_info *fs_info = root->fs_info;
  2378. struct extent_buffer *b;
  2379. int root_lock;
  2380. int level = 0;
  2381. /* We try very hard to do read locks on the root */
  2382. root_lock = BTRFS_READ_LOCK;
  2383. if (p->search_commit_root) {
  2384. /*
  2385. * The commit roots are read only so we always do read locks,
  2386. * and we always must hold the commit_root_sem when doing
  2387. * searches on them, the only exception is send where we don't
  2388. * want to block transaction commits for a long time, so
  2389. * we need to clone the commit root in order to avoid races
  2390. * with transaction commits that create a snapshot of one of
  2391. * the roots used by a send operation.
  2392. */
  2393. if (p->need_commit_sem) {
  2394. down_read(&fs_info->commit_root_sem);
  2395. b = btrfs_clone_extent_buffer(root->commit_root);
  2396. up_read(&fs_info->commit_root_sem);
  2397. if (!b)
  2398. return ERR_PTR(-ENOMEM);
  2399. } else {
  2400. b = root->commit_root;
  2401. extent_buffer_get(b);
  2402. }
  2403. level = btrfs_header_level(b);
  2404. /*
  2405. * Ensure that all callers have set skip_locking when
  2406. * p->search_commit_root = 1.
  2407. */
  2408. ASSERT(p->skip_locking == 1);
  2409. goto out;
  2410. }
  2411. if (p->skip_locking) {
  2412. b = btrfs_root_node(root);
  2413. level = btrfs_header_level(b);
  2414. goto out;
  2415. }
  2416. /*
  2417. * If the level is set to maximum, we can skip trying to get the read
  2418. * lock.
  2419. */
  2420. if (write_lock_level < BTRFS_MAX_LEVEL) {
  2421. /*
  2422. * We don't know the level of the root node until we actually
  2423. * have it read locked
  2424. */
  2425. b = btrfs_read_lock_root_node(root);
  2426. level = btrfs_header_level(b);
  2427. if (level > write_lock_level)
  2428. goto out;
  2429. /* Whoops, must trade for write lock */
  2430. btrfs_tree_read_unlock(b);
  2431. free_extent_buffer(b);
  2432. }
  2433. b = btrfs_lock_root_node(root);
  2434. root_lock = BTRFS_WRITE_LOCK;
  2435. /* The level might have changed, check again */
  2436. level = btrfs_header_level(b);
  2437. out:
  2438. p->nodes[level] = b;
  2439. if (!p->skip_locking)
  2440. p->locks[level] = root_lock;
  2441. /*
  2442. * Callers are responsible for dropping b's references.
  2443. */
  2444. return b;
  2445. }
  2446. /*
  2447. * btrfs_search_slot - look for a key in a tree and perform necessary
  2448. * modifications to preserve tree invariants.
  2449. *
  2450. * @trans: Handle of transaction, used when modifying the tree
  2451. * @p: Holds all btree nodes along the search path
  2452. * @root: The root node of the tree
  2453. * @key: The key we are looking for
  2454. * @ins_len: Indicates purpose of search, for inserts it is 1, for
  2455. * deletions it's -1. 0 for plain searches
  2456. * @cow: boolean should CoW operations be performed. Must always be 1
  2457. * when modifying the tree.
  2458. *
  2459. * If @ins_len > 0, nodes and leaves will be split as we walk down the tree.
  2460. * If @ins_len < 0, nodes will be merged as we walk down the tree (if possible)
  2461. *
  2462. * If @key is found, 0 is returned and you can find the item in the leaf level
  2463. * of the path (level 0)
  2464. *
  2465. * If @key isn't found, 1 is returned and the leaf level of the path (level 0)
  2466. * points to the slot where it should be inserted
  2467. *
  2468. * If an error is encountered while searching the tree a negative error number
  2469. * is returned
  2470. */
  2471. int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  2472. const struct btrfs_key *key, struct btrfs_path *p,
  2473. int ins_len, int cow)
  2474. {
  2475. struct btrfs_fs_info *fs_info = root->fs_info;
  2476. struct extent_buffer *b;
  2477. int slot;
  2478. int ret;
  2479. int err;
  2480. int level;
  2481. int lowest_unlock = 1;
  2482. /* everything at write_lock_level or lower must be write locked */
  2483. int write_lock_level = 0;
  2484. u8 lowest_level = 0;
  2485. int min_write_lock_level;
  2486. int prev_cmp;
  2487. lowest_level = p->lowest_level;
  2488. WARN_ON(lowest_level && ins_len > 0);
  2489. WARN_ON(p->nodes[0] != NULL);
  2490. BUG_ON(!cow && ins_len);
  2491. if (ins_len < 0) {
  2492. lowest_unlock = 2;
  2493. /* when we are removing items, we might have to go up to level
  2494. * two as we update tree pointers Make sure we keep write
  2495. * for those levels as well
  2496. */
  2497. write_lock_level = 2;
  2498. } else if (ins_len > 0) {
  2499. /*
  2500. * for inserting items, make sure we have a write lock on
  2501. * level 1 so we can update keys
  2502. */
  2503. write_lock_level = 1;
  2504. }
  2505. if (!cow)
  2506. write_lock_level = -1;
  2507. if (cow && (p->keep_locks || p->lowest_level))
  2508. write_lock_level = BTRFS_MAX_LEVEL;
  2509. min_write_lock_level = write_lock_level;
  2510. again:
  2511. prev_cmp = -1;
  2512. b = btrfs_search_slot_get_root(root, p, write_lock_level);
  2513. if (IS_ERR(b)) {
  2514. ret = PTR_ERR(b);
  2515. goto done;
  2516. }
  2517. while (b) {
  2518. level = btrfs_header_level(b);
  2519. /*
  2520. * setup the path here so we can release it under lock
  2521. * contention with the cow code
  2522. */
  2523. if (cow) {
  2524. bool last_level = (level == (BTRFS_MAX_LEVEL - 1));
  2525. /*
  2526. * if we don't really need to cow this block
  2527. * then we don't want to set the path blocking,
  2528. * so we test it here
  2529. */
  2530. if (!should_cow_block(trans, root, b)) {
  2531. trans->dirty = true;
  2532. goto cow_done;
  2533. }
  2534. /*
  2535. * must have write locks on this node and the
  2536. * parent
  2537. */
  2538. if (level > write_lock_level ||
  2539. (level + 1 > write_lock_level &&
  2540. level + 1 < BTRFS_MAX_LEVEL &&
  2541. p->nodes[level + 1])) {
  2542. write_lock_level = level + 1;
  2543. btrfs_release_path(p);
  2544. goto again;
  2545. }
  2546. btrfs_set_path_blocking(p);
  2547. if (last_level)
  2548. err = btrfs_cow_block(trans, root, b, NULL, 0,
  2549. &b);
  2550. else
  2551. err = btrfs_cow_block(trans, root, b,
  2552. p->nodes[level + 1],
  2553. p->slots[level + 1], &b);
  2554. if (err) {
  2555. ret = err;
  2556. goto done;
  2557. }
  2558. }
  2559. cow_done:
  2560. p->nodes[level] = b;
  2561. btrfs_clear_path_blocking(p, NULL, 0);
  2562. /*
  2563. * we have a lock on b and as long as we aren't changing
  2564. * the tree, there is no way to for the items in b to change.
  2565. * It is safe to drop the lock on our parent before we
  2566. * go through the expensive btree search on b.
  2567. *
  2568. * If we're inserting or deleting (ins_len != 0), then we might
  2569. * be changing slot zero, which may require changing the parent.
  2570. * So, we can't drop the lock until after we know which slot
  2571. * we're operating on.
  2572. */
  2573. if (!ins_len && !p->keep_locks) {
  2574. int u = level + 1;
  2575. if (u < BTRFS_MAX_LEVEL && p->locks[u]) {
  2576. btrfs_tree_unlock_rw(p->nodes[u], p->locks[u]);
  2577. p->locks[u] = 0;
  2578. }
  2579. }
  2580. ret = key_search(b, key, level, &prev_cmp, &slot);
  2581. if (ret < 0)
  2582. goto done;
  2583. if (level != 0) {
  2584. int dec = 0;
  2585. if (ret && slot > 0) {
  2586. dec = 1;
  2587. slot -= 1;
  2588. }
  2589. p->slots[level] = slot;
  2590. err = setup_nodes_for_search(trans, root, p, b, level,
  2591. ins_len, &write_lock_level);
  2592. if (err == -EAGAIN)
  2593. goto again;
  2594. if (err) {
  2595. ret = err;
  2596. goto done;
  2597. }
  2598. b = p->nodes[level];
  2599. slot = p->slots[level];
  2600. /*
  2601. * slot 0 is special, if we change the key
  2602. * we have to update the parent pointer
  2603. * which means we must have a write lock
  2604. * on the parent
  2605. */
  2606. if (slot == 0 && ins_len &&
  2607. write_lock_level < level + 1) {
  2608. write_lock_level = level + 1;
  2609. btrfs_release_path(p);
  2610. goto again;
  2611. }
  2612. unlock_up(p, level, lowest_unlock,
  2613. min_write_lock_level, &write_lock_level);
  2614. if (level == lowest_level) {
  2615. if (dec)
  2616. p->slots[level]++;
  2617. goto done;
  2618. }
  2619. err = read_block_for_search(root, p, &b, level,
  2620. slot, key);
  2621. if (err == -EAGAIN)
  2622. goto again;
  2623. if (err) {
  2624. ret = err;
  2625. goto done;
  2626. }
  2627. if (!p->skip_locking) {
  2628. level = btrfs_header_level(b);
  2629. if (level <= write_lock_level) {
  2630. err = btrfs_try_tree_write_lock(b);
  2631. if (!err) {
  2632. btrfs_set_path_blocking(p);
  2633. btrfs_tree_lock(b);
  2634. btrfs_clear_path_blocking(p, b,
  2635. BTRFS_WRITE_LOCK);
  2636. }
  2637. p->locks[level] = BTRFS_WRITE_LOCK;
  2638. } else {
  2639. err = btrfs_tree_read_lock_atomic(b);
  2640. if (!err) {
  2641. btrfs_set_path_blocking(p);
  2642. btrfs_tree_read_lock(b);
  2643. btrfs_clear_path_blocking(p, b,
  2644. BTRFS_READ_LOCK);
  2645. }
  2646. p->locks[level] = BTRFS_READ_LOCK;
  2647. }
  2648. p->nodes[level] = b;
  2649. }
  2650. } else {
  2651. p->slots[level] = slot;
  2652. if (ins_len > 0 &&
  2653. btrfs_leaf_free_space(fs_info, b) < ins_len) {
  2654. if (write_lock_level < 1) {
  2655. write_lock_level = 1;
  2656. btrfs_release_path(p);
  2657. goto again;
  2658. }
  2659. btrfs_set_path_blocking(p);
  2660. err = split_leaf(trans, root, key,
  2661. p, ins_len, ret == 0);
  2662. btrfs_clear_path_blocking(p, NULL, 0);
  2663. BUG_ON(err > 0);
  2664. if (err) {
  2665. ret = err;
  2666. goto done;
  2667. }
  2668. }
  2669. if (!p->search_for_split)
  2670. unlock_up(p, level, lowest_unlock,
  2671. min_write_lock_level, &write_lock_level);
  2672. goto done;
  2673. }
  2674. }
  2675. ret = 1;
  2676. done:
  2677. /*
  2678. * we don't really know what they plan on doing with the path
  2679. * from here on, so for now just mark it as blocking
  2680. */
  2681. if (!p->leave_spinning)
  2682. btrfs_set_path_blocking(p);
  2683. if (ret < 0 && !p->skip_release_on_error)
  2684. btrfs_release_path(p);
  2685. return ret;
  2686. }
  2687. /*
  2688. * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
  2689. * current state of the tree together with the operations recorded in the tree
  2690. * modification log to search for the key in a previous version of this tree, as
  2691. * denoted by the time_seq parameter.
  2692. *
  2693. * Naturally, there is no support for insert, delete or cow operations.
  2694. *
  2695. * The resulting path and return value will be set up as if we called
  2696. * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
  2697. */
  2698. int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key,
  2699. struct btrfs_path *p, u64 time_seq)
  2700. {
  2701. struct btrfs_fs_info *fs_info = root->fs_info;
  2702. struct extent_buffer *b;
  2703. int slot;
  2704. int ret;
  2705. int err;
  2706. int level;
  2707. int lowest_unlock = 1;
  2708. u8 lowest_level = 0;
  2709. int prev_cmp = -1;
  2710. lowest_level = p->lowest_level;
  2711. WARN_ON(p->nodes[0] != NULL);
  2712. if (p->search_commit_root) {
  2713. BUG_ON(time_seq);
  2714. return btrfs_search_slot(NULL, root, key, p, 0, 0);
  2715. }
  2716. again:
  2717. b = get_old_root(root, time_seq);
  2718. if (!b) {
  2719. ret = -EIO;
  2720. goto done;
  2721. }
  2722. level = btrfs_header_level(b);
  2723. p->locks[level] = BTRFS_READ_LOCK;
  2724. while (b) {
  2725. level = btrfs_header_level(b);
  2726. p->nodes[level] = b;
  2727. btrfs_clear_path_blocking(p, NULL, 0);
  2728. /*
  2729. * we have a lock on b and as long as we aren't changing
  2730. * the tree, there is no way to for the items in b to change.
  2731. * It is safe to drop the lock on our parent before we
  2732. * go through the expensive btree search on b.
  2733. */
  2734. btrfs_unlock_up_safe(p, level + 1);
  2735. /*
  2736. * Since we can unwind ebs we want to do a real search every
  2737. * time.
  2738. */
  2739. prev_cmp = -1;
  2740. ret = key_search(b, key, level, &prev_cmp, &slot);
  2741. if (level != 0) {
  2742. int dec = 0;
  2743. if (ret && slot > 0) {
  2744. dec = 1;
  2745. slot -= 1;
  2746. }
  2747. p->slots[level] = slot;
  2748. unlock_up(p, level, lowest_unlock, 0, NULL);
  2749. if (level == lowest_level) {
  2750. if (dec)
  2751. p->slots[level]++;
  2752. goto done;
  2753. }
  2754. err = read_block_for_search(root, p, &b, level,
  2755. slot, key);
  2756. if (err == -EAGAIN)
  2757. goto again;
  2758. if (err) {
  2759. ret = err;
  2760. goto done;
  2761. }
  2762. level = btrfs_header_level(b);
  2763. err = btrfs_tree_read_lock_atomic(b);
  2764. if (!err) {
  2765. btrfs_set_path_blocking(p);
  2766. btrfs_tree_read_lock(b);
  2767. btrfs_clear_path_blocking(p, b,
  2768. BTRFS_READ_LOCK);
  2769. }
  2770. b = tree_mod_log_rewind(fs_info, p, b, time_seq);
  2771. if (!b) {
  2772. ret = -ENOMEM;
  2773. goto done;
  2774. }
  2775. p->locks[level] = BTRFS_READ_LOCK;
  2776. p->nodes[level] = b;
  2777. } else {
  2778. p->slots[level] = slot;
  2779. unlock_up(p, level, lowest_unlock, 0, NULL);
  2780. goto done;
  2781. }
  2782. }
  2783. ret = 1;
  2784. done:
  2785. if (!p->leave_spinning)
  2786. btrfs_set_path_blocking(p);
  2787. if (ret < 0)
  2788. btrfs_release_path(p);
  2789. return ret;
  2790. }
  2791. /*
  2792. * helper to use instead of search slot if no exact match is needed but
  2793. * instead the next or previous item should be returned.
  2794. * When find_higher is true, the next higher item is returned, the next lower
  2795. * otherwise.
  2796. * When return_any and find_higher are both true, and no higher item is found,
  2797. * return the next lower instead.
  2798. * When return_any is true and find_higher is false, and no lower item is found,
  2799. * return the next higher instead.
  2800. * It returns 0 if any item is found, 1 if none is found (tree empty), and
  2801. * < 0 on error
  2802. */
  2803. int btrfs_search_slot_for_read(struct btrfs_root *root,
  2804. const struct btrfs_key *key,
  2805. struct btrfs_path *p, int find_higher,
  2806. int return_any)
  2807. {
  2808. int ret;
  2809. struct extent_buffer *leaf;
  2810. again:
  2811. ret = btrfs_search_slot(NULL, root, key, p, 0, 0);
  2812. if (ret <= 0)
  2813. return ret;
  2814. /*
  2815. * a return value of 1 means the path is at the position where the
  2816. * item should be inserted. Normally this is the next bigger item,
  2817. * but in case the previous item is the last in a leaf, path points
  2818. * to the first free slot in the previous leaf, i.e. at an invalid
  2819. * item.
  2820. */
  2821. leaf = p->nodes[0];
  2822. if (find_higher) {
  2823. if (p->slots[0] >= btrfs_header_nritems(leaf)) {
  2824. ret = btrfs_next_leaf(root, p);
  2825. if (ret <= 0)
  2826. return ret;
  2827. if (!return_any)
  2828. return 1;
  2829. /*
  2830. * no higher item found, return the next
  2831. * lower instead
  2832. */
  2833. return_any = 0;
  2834. find_higher = 0;
  2835. btrfs_release_path(p);
  2836. goto again;
  2837. }
  2838. } else {
  2839. if (p->slots[0] == 0) {
  2840. ret = btrfs_prev_leaf(root, p);
  2841. if (ret < 0)
  2842. return ret;
  2843. if (!ret) {
  2844. leaf = p->nodes[0];
  2845. if (p->slots[0] == btrfs_header_nritems(leaf))
  2846. p->slots[0]--;
  2847. return 0;
  2848. }
  2849. if (!return_any)
  2850. return 1;
  2851. /*
  2852. * no lower item found, return the next
  2853. * higher instead
  2854. */
  2855. return_any = 0;
  2856. find_higher = 1;
  2857. btrfs_release_path(p);
  2858. goto again;
  2859. } else {
  2860. --p->slots[0];
  2861. }
  2862. }
  2863. return 0;
  2864. }
  2865. /*
  2866. * adjust the pointers going up the tree, starting at level
  2867. * making sure the right key of each node is points to 'key'.
  2868. * This is used after shifting pointers to the left, so it stops
  2869. * fixing up pointers when a given leaf/node is not in slot 0 of the
  2870. * higher levels
  2871. *
  2872. */
  2873. static void fixup_low_keys(struct btrfs_path *path,
  2874. struct btrfs_disk_key *key, int level)
  2875. {
  2876. int i;
  2877. struct extent_buffer *t;
  2878. int ret;
  2879. for (i = level; i < BTRFS_MAX_LEVEL; i++) {
  2880. int tslot = path->slots[i];
  2881. if (!path->nodes[i])
  2882. break;
  2883. t = path->nodes[i];
  2884. ret = tree_mod_log_insert_key(t, tslot, MOD_LOG_KEY_REPLACE,
  2885. GFP_ATOMIC);
  2886. BUG_ON(ret < 0);
  2887. btrfs_set_node_key(t, key, tslot);
  2888. btrfs_mark_buffer_dirty(path->nodes[i]);
  2889. if (tslot != 0)
  2890. break;
  2891. }
  2892. }
  2893. /*
  2894. * update item key.
  2895. *
  2896. * This function isn't completely safe. It's the caller's responsibility
  2897. * that the new key won't break the order
  2898. */
  2899. void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info,
  2900. struct btrfs_path *path,
  2901. const struct btrfs_key *new_key)
  2902. {
  2903. struct btrfs_disk_key disk_key;
  2904. struct extent_buffer *eb;
  2905. int slot;
  2906. eb = path->nodes[0];
  2907. slot = path->slots[0];
  2908. if (slot > 0) {
  2909. btrfs_item_key(eb, &disk_key, slot - 1);
  2910. BUG_ON(comp_keys(&disk_key, new_key) >= 0);
  2911. }
  2912. if (slot < btrfs_header_nritems(eb) - 1) {
  2913. btrfs_item_key(eb, &disk_key, slot + 1);
  2914. BUG_ON(comp_keys(&disk_key, new_key) <= 0);
  2915. }
  2916. btrfs_cpu_key_to_disk(&disk_key, new_key);
  2917. btrfs_set_item_key(eb, &disk_key, slot);
  2918. btrfs_mark_buffer_dirty(eb);
  2919. if (slot == 0)
  2920. fixup_low_keys(path, &disk_key, 1);
  2921. }
  2922. /*
  2923. * try to push data from one node into the next node left in the
  2924. * tree.
  2925. *
  2926. * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
  2927. * error, and > 0 if there was no room in the left hand block.
  2928. */
  2929. static int push_node_left(struct btrfs_trans_handle *trans,
  2930. struct btrfs_fs_info *fs_info,
  2931. struct extent_buffer *dst,
  2932. struct extent_buffer *src, int empty)
  2933. {
  2934. int push_items = 0;
  2935. int src_nritems;
  2936. int dst_nritems;
  2937. int ret = 0;
  2938. src_nritems = btrfs_header_nritems(src);
  2939. dst_nritems = btrfs_header_nritems(dst);
  2940. push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
  2941. WARN_ON(btrfs_header_generation(src) != trans->transid);
  2942. WARN_ON(btrfs_header_generation(dst) != trans->transid);
  2943. if (!empty && src_nritems <= 8)
  2944. return 1;
  2945. if (push_items <= 0)
  2946. return 1;
  2947. if (empty) {
  2948. push_items = min(src_nritems, push_items);
  2949. if (push_items < src_nritems) {
  2950. /* leave at least 8 pointers in the node if
  2951. * we aren't going to empty it
  2952. */
  2953. if (src_nritems - push_items < 8) {
  2954. if (push_items <= 8)
  2955. return 1;
  2956. push_items -= 8;
  2957. }
  2958. }
  2959. } else
  2960. push_items = min(src_nritems - 8, push_items);
  2961. ret = tree_mod_log_eb_copy(fs_info, dst, src, dst_nritems, 0,
  2962. push_items);
  2963. if (ret) {
  2964. btrfs_abort_transaction(trans, ret);
  2965. return ret;
  2966. }
  2967. copy_extent_buffer(dst, src,
  2968. btrfs_node_key_ptr_offset(dst_nritems),
  2969. btrfs_node_key_ptr_offset(0),
  2970. push_items * sizeof(struct btrfs_key_ptr));
  2971. if (push_items < src_nritems) {
  2972. /*
  2973. * Don't call tree_mod_log_insert_move here, key removal was
  2974. * already fully logged by tree_mod_log_eb_copy above.
  2975. */
  2976. memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
  2977. btrfs_node_key_ptr_offset(push_items),
  2978. (src_nritems - push_items) *
  2979. sizeof(struct btrfs_key_ptr));
  2980. }
  2981. btrfs_set_header_nritems(src, src_nritems - push_items);
  2982. btrfs_set_header_nritems(dst, dst_nritems + push_items);
  2983. btrfs_mark_buffer_dirty(src);
  2984. btrfs_mark_buffer_dirty(dst);
  2985. return ret;
  2986. }
  2987. /*
  2988. * try to push data from one node into the next node right in the
  2989. * tree.
  2990. *
  2991. * returns 0 if some ptrs were pushed, < 0 if there was some horrible
  2992. * error, and > 0 if there was no room in the right hand block.
  2993. *
  2994. * this will only push up to 1/2 the contents of the left node over
  2995. */
  2996. static int balance_node_right(struct btrfs_trans_handle *trans,
  2997. struct btrfs_fs_info *fs_info,
  2998. struct extent_buffer *dst,
  2999. struct extent_buffer *src)
  3000. {
  3001. int push_items = 0;
  3002. int max_push;
  3003. int src_nritems;
  3004. int dst_nritems;
  3005. int ret = 0;
  3006. WARN_ON(btrfs_header_generation(src) != trans->transid);
  3007. WARN_ON(btrfs_header_generation(dst) != trans->transid);
  3008. src_nritems = btrfs_header_nritems(src);
  3009. dst_nritems = btrfs_header_nritems(dst);
  3010. push_items = BTRFS_NODEPTRS_PER_BLOCK(fs_info) - dst_nritems;
  3011. if (push_items <= 0)
  3012. return 1;
  3013. if (src_nritems < 4)
  3014. return 1;
  3015. max_push = src_nritems / 2 + 1;
  3016. /* don't try to empty the node */
  3017. if (max_push >= src_nritems)
  3018. return 1;
  3019. if (max_push < push_items)
  3020. push_items = max_push;
  3021. ret = tree_mod_log_insert_move(dst, push_items, 0, dst_nritems);
  3022. BUG_ON(ret < 0);
  3023. memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
  3024. btrfs_node_key_ptr_offset(0),
  3025. (dst_nritems) *
  3026. sizeof(struct btrfs_key_ptr));
  3027. ret = tree_mod_log_eb_copy(fs_info, dst, src, 0,
  3028. src_nritems - push_items, push_items);
  3029. if (ret) {
  3030. btrfs_abort_transaction(trans, ret);
  3031. return ret;
  3032. }
  3033. copy_extent_buffer(dst, src,
  3034. btrfs_node_key_ptr_offset(0),
  3035. btrfs_node_key_ptr_offset(src_nritems - push_items),
  3036. push_items * sizeof(struct btrfs_key_ptr));
  3037. btrfs_set_header_nritems(src, src_nritems - push_items);
  3038. btrfs_set_header_nritems(dst, dst_nritems + push_items);
  3039. btrfs_mark_buffer_dirty(src);
  3040. btrfs_mark_buffer_dirty(dst);
  3041. return ret;
  3042. }
  3043. /*
  3044. * helper function to insert a new root level in the tree.
  3045. * A new node is allocated, and a single item is inserted to
  3046. * point to the existing root
  3047. *
  3048. * returns zero on success or < 0 on failure.
  3049. */
  3050. static noinline int insert_new_root(struct btrfs_trans_handle *trans,
  3051. struct btrfs_root *root,
  3052. struct btrfs_path *path, int level)
  3053. {
  3054. struct btrfs_fs_info *fs_info = root->fs_info;
  3055. u64 lower_gen;
  3056. struct extent_buffer *lower;
  3057. struct extent_buffer *c;
  3058. struct extent_buffer *old;
  3059. struct btrfs_disk_key lower_key;
  3060. int ret;
  3061. BUG_ON(path->nodes[level]);
  3062. BUG_ON(path->nodes[level-1] != root->node);
  3063. lower = path->nodes[level-1];
  3064. if (level == 1)
  3065. btrfs_item_key(lower, &lower_key, 0);
  3066. else
  3067. btrfs_node_key(lower, &lower_key, 0);
  3068. c = alloc_tree_block_no_bg_flush(trans, root, 0, &lower_key, level,
  3069. root->node->start, 0);
  3070. if (IS_ERR(c))
  3071. return PTR_ERR(c);
  3072. root_add_used(root, fs_info->nodesize);
  3073. btrfs_set_header_nritems(c, 1);
  3074. btrfs_set_node_key(c, &lower_key, 0);
  3075. btrfs_set_node_blockptr(c, 0, lower->start);
  3076. lower_gen = btrfs_header_generation(lower);
  3077. WARN_ON(lower_gen != trans->transid);
  3078. btrfs_set_node_ptr_generation(c, 0, lower_gen);
  3079. btrfs_mark_buffer_dirty(c);
  3080. old = root->node;
  3081. ret = tree_mod_log_insert_root(root->node, c, 0);
  3082. BUG_ON(ret < 0);
  3083. rcu_assign_pointer(root->node, c);
  3084. /* the super has an extra ref to root->node */
  3085. free_extent_buffer(old);
  3086. add_root_to_dirty_list(root);
  3087. extent_buffer_get(c);
  3088. path->nodes[level] = c;
  3089. path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
  3090. path->slots[level] = 0;
  3091. return 0;
  3092. }
  3093. /*
  3094. * worker function to insert a single pointer in a node.
  3095. * the node should have enough room for the pointer already
  3096. *
  3097. * slot and level indicate where you want the key to go, and
  3098. * blocknr is the block the key points to.
  3099. */
  3100. static void insert_ptr(struct btrfs_trans_handle *trans,
  3101. struct btrfs_fs_info *fs_info, struct btrfs_path *path,
  3102. struct btrfs_disk_key *key, u64 bytenr,
  3103. int slot, int level)
  3104. {
  3105. struct extent_buffer *lower;
  3106. int nritems;
  3107. int ret;
  3108. BUG_ON(!path->nodes[level]);
  3109. btrfs_assert_tree_locked(path->nodes[level]);
  3110. lower = path->nodes[level];
  3111. nritems = btrfs_header_nritems(lower);
  3112. BUG_ON(slot > nritems);
  3113. BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(fs_info));
  3114. if (slot != nritems) {
  3115. if (level) {
  3116. ret = tree_mod_log_insert_move(lower, slot + 1, slot,
  3117. nritems - slot);
  3118. BUG_ON(ret < 0);
  3119. }
  3120. memmove_extent_buffer(lower,
  3121. btrfs_node_key_ptr_offset(slot + 1),
  3122. btrfs_node_key_ptr_offset(slot),
  3123. (nritems - slot) * sizeof(struct btrfs_key_ptr));
  3124. }
  3125. if (level) {
  3126. ret = tree_mod_log_insert_key(lower, slot, MOD_LOG_KEY_ADD,
  3127. GFP_NOFS);
  3128. BUG_ON(ret < 0);
  3129. }
  3130. btrfs_set_node_key(lower, key, slot);
  3131. btrfs_set_node_blockptr(lower, slot, bytenr);
  3132. WARN_ON(trans->transid == 0);
  3133. btrfs_set_node_ptr_generation(lower, slot, trans->transid);
  3134. btrfs_set_header_nritems(lower, nritems + 1);
  3135. btrfs_mark_buffer_dirty(lower);
  3136. }
  3137. /*
  3138. * split the node at the specified level in path in two.
  3139. * The path is corrected to point to the appropriate node after the split
  3140. *
  3141. * Before splitting this tries to make some room in the node by pushing
  3142. * left and right, if either one works, it returns right away.
  3143. *
  3144. * returns 0 on success and < 0 on failure
  3145. */
  3146. static noinline int split_node(struct btrfs_trans_handle *trans,
  3147. struct btrfs_root *root,
  3148. struct btrfs_path *path, int level)
  3149. {
  3150. struct btrfs_fs_info *fs_info = root->fs_info;
  3151. struct extent_buffer *c;
  3152. struct extent_buffer *split;
  3153. struct btrfs_disk_key disk_key;
  3154. int mid;
  3155. int ret;
  3156. u32 c_nritems;
  3157. c = path->nodes[level];
  3158. WARN_ON(btrfs_header_generation(c) != trans->transid);
  3159. if (c == root->node) {
  3160. /*
  3161. * trying to split the root, lets make a new one
  3162. *
  3163. * tree mod log: We don't log_removal old root in
  3164. * insert_new_root, because that root buffer will be kept as a
  3165. * normal node. We are going to log removal of half of the
  3166. * elements below with tree_mod_log_eb_copy. We're holding a
  3167. * tree lock on the buffer, which is why we cannot race with
  3168. * other tree_mod_log users.
  3169. */
  3170. ret = insert_new_root(trans, root, path, level + 1);
  3171. if (ret)
  3172. return ret;
  3173. } else {
  3174. ret = push_nodes_for_insert(trans, root, path, level);
  3175. c = path->nodes[level];
  3176. if (!ret && btrfs_header_nritems(c) <
  3177. BTRFS_NODEPTRS_PER_BLOCK(fs_info) - 3)
  3178. return 0;
  3179. if (ret < 0)
  3180. return ret;
  3181. }
  3182. c_nritems = btrfs_header_nritems(c);
  3183. mid = (c_nritems + 1) / 2;
  3184. btrfs_node_key(c, &disk_key, mid);
  3185. split = alloc_tree_block_no_bg_flush(trans, root, 0, &disk_key, level,
  3186. c->start, 0);
  3187. if (IS_ERR(split))
  3188. return PTR_ERR(split);
  3189. root_add_used(root, fs_info->nodesize);
  3190. ASSERT(btrfs_header_level(c) == level);
  3191. ret = tree_mod_log_eb_copy(fs_info, split, c, 0, mid, c_nritems - mid);
  3192. if (ret) {
  3193. btrfs_abort_transaction(trans, ret);
  3194. return ret;
  3195. }
  3196. copy_extent_buffer(split, c,
  3197. btrfs_node_key_ptr_offset(0),
  3198. btrfs_node_key_ptr_offset(mid),
  3199. (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
  3200. btrfs_set_header_nritems(split, c_nritems - mid);
  3201. btrfs_set_header_nritems(c, mid);
  3202. ret = 0;
  3203. btrfs_mark_buffer_dirty(c);
  3204. btrfs_mark_buffer_dirty(split);
  3205. insert_ptr(trans, fs_info, path, &disk_key, split->start,
  3206. path->slots[level + 1] + 1, level + 1);
  3207. if (path->slots[level] >= mid) {
  3208. path->slots[level] -= mid;
  3209. btrfs_tree_unlock(c);
  3210. free_extent_buffer(c);
  3211. path->nodes[level] = split;
  3212. path->slots[level + 1] += 1;
  3213. } else {
  3214. btrfs_tree_unlock(split);
  3215. free_extent_buffer(split);
  3216. }
  3217. return ret;
  3218. }
  3219. /*
  3220. * how many bytes are required to store the items in a leaf. start
  3221. * and nr indicate which items in the leaf to check. This totals up the
  3222. * space used both by the item structs and the item data
  3223. */
  3224. static int leaf_space_used(struct extent_buffer *l, int start, int nr)
  3225. {
  3226. struct btrfs_item *start_item;
  3227. struct btrfs_item *end_item;
  3228. struct btrfs_map_token token;
  3229. int data_len;
  3230. int nritems = btrfs_header_nritems(l);
  3231. int end = min(nritems, start + nr) - 1;
  3232. if (!nr)
  3233. return 0;
  3234. btrfs_init_map_token(&token);
  3235. start_item = btrfs_item_nr(start);
  3236. end_item = btrfs_item_nr(end);
  3237. data_len = btrfs_token_item_offset(l, start_item, &token) +
  3238. btrfs_token_item_size(l, start_item, &token);
  3239. data_len = data_len - btrfs_token_item_offset(l, end_item, &token);
  3240. data_len += sizeof(struct btrfs_item) * nr;
  3241. WARN_ON(data_len < 0);
  3242. return data_len;
  3243. }
  3244. /*
  3245. * The space between the end of the leaf items and
  3246. * the start of the leaf data. IOW, how much room
  3247. * the leaf has left for both items and data
  3248. */
  3249. noinline int btrfs_leaf_free_space(struct btrfs_fs_info *fs_info,
  3250. struct extent_buffer *leaf)
  3251. {
  3252. int nritems = btrfs_header_nritems(leaf);
  3253. int ret;
  3254. ret = BTRFS_LEAF_DATA_SIZE(fs_info) - leaf_space_used(leaf, 0, nritems);
  3255. if (ret < 0) {
  3256. btrfs_crit(fs_info,
  3257. "leaf free space ret %d, leaf data size %lu, used %d nritems %d",
  3258. ret,
  3259. (unsigned long) BTRFS_LEAF_DATA_SIZE(fs_info),
  3260. leaf_space_used(leaf, 0, nritems), nritems);
  3261. }
  3262. return ret;
  3263. }
  3264. /*
  3265. * min slot controls the lowest index we're willing to push to the
  3266. * right. We'll push up to and including min_slot, but no lower
  3267. */
  3268. static noinline int __push_leaf_right(struct btrfs_fs_info *fs_info,
  3269. struct btrfs_path *path,
  3270. int data_size, int empty,
  3271. struct extent_buffer *right,
  3272. int free_space, u32 left_nritems,
  3273. u32 min_slot)
  3274. {
  3275. struct extent_buffer *left = path->nodes[0];
  3276. struct extent_buffer *upper = path->nodes[1];
  3277. struct btrfs_map_token token;
  3278. struct btrfs_disk_key disk_key;
  3279. int slot;
  3280. u32 i;
  3281. int push_space = 0;
  3282. int push_items = 0;
  3283. struct btrfs_item *item;
  3284. u32 nr;
  3285. u32 right_nritems;
  3286. u32 data_end;
  3287. u32 this_item_size;
  3288. btrfs_init_map_token(&token);
  3289. if (empty)
  3290. nr = 0;
  3291. else
  3292. nr = max_t(u32, 1, min_slot);
  3293. if (path->slots[0] >= left_nritems)
  3294. push_space += data_size;
  3295. slot = path->slots[1];
  3296. i = left_nritems - 1;
  3297. while (i >= nr) {
  3298. item = btrfs_item_nr(i);
  3299. if (!empty && push_items > 0) {
  3300. if (path->slots[0] > i)
  3301. break;
  3302. if (path->slots[0] == i) {
  3303. int space = btrfs_leaf_free_space(fs_info, left);
  3304. if (space + push_space * 2 > free_space)
  3305. break;
  3306. }
  3307. }
  3308. if (path->slots[0] == i)
  3309. push_space += data_size;
  3310. this_item_size = btrfs_item_size(left, item);
  3311. if (this_item_size + sizeof(*item) + push_space > free_space)
  3312. break;
  3313. push_items++;
  3314. push_space += this_item_size + sizeof(*item);
  3315. if (i == 0)
  3316. break;
  3317. i--;
  3318. }
  3319. if (push_items == 0)
  3320. goto out_unlock;
  3321. WARN_ON(!empty && push_items == left_nritems);
  3322. /* push left to right */
  3323. right_nritems = btrfs_header_nritems(right);
  3324. push_space = btrfs_item_end_nr(left, left_nritems - push_items);
  3325. push_space -= leaf_data_end(fs_info, left);
  3326. /* make room in the right data area */
  3327. data_end = leaf_data_end(fs_info, right);
  3328. memmove_extent_buffer(right,
  3329. BTRFS_LEAF_DATA_OFFSET + data_end - push_space,
  3330. BTRFS_LEAF_DATA_OFFSET + data_end,
  3331. BTRFS_LEAF_DATA_SIZE(fs_info) - data_end);
  3332. /* copy from the left data area */
  3333. copy_extent_buffer(right, left, BTRFS_LEAF_DATA_OFFSET +
  3334. BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
  3335. BTRFS_LEAF_DATA_OFFSET + leaf_data_end(fs_info, left),
  3336. push_space);
  3337. memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
  3338. btrfs_item_nr_offset(0),
  3339. right_nritems * sizeof(struct btrfs_item));
  3340. /* copy the items from left to right */
  3341. copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
  3342. btrfs_item_nr_offset(left_nritems - push_items),
  3343. push_items * sizeof(struct btrfs_item));
  3344. /* update the item pointers */
  3345. right_nritems += push_items;
  3346. btrfs_set_header_nritems(right, right_nritems);
  3347. push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
  3348. for (i = 0; i < right_nritems; i++) {
  3349. item = btrfs_item_nr(i);
  3350. push_space -= btrfs_token_item_size(right, item, &token);
  3351. btrfs_set_token_item_offset(right, item, push_space, &token);
  3352. }
  3353. left_nritems -= push_items;
  3354. btrfs_set_header_nritems(left, left_nritems);
  3355. if (left_nritems)
  3356. btrfs_mark_buffer_dirty(left);
  3357. else
  3358. clean_tree_block(fs_info, left);
  3359. btrfs_mark_buffer_dirty(right);
  3360. btrfs_item_key(right, &disk_key, 0);
  3361. btrfs_set_node_key(upper, &disk_key, slot + 1);
  3362. btrfs_mark_buffer_dirty(upper);
  3363. /* then fixup the leaf pointer in the path */
  3364. if (path->slots[0] >= left_nritems) {
  3365. path->slots[0] -= left_nritems;
  3366. if (btrfs_header_nritems(path->nodes[0]) == 0)
  3367. clean_tree_block(fs_info, path->nodes[0]);
  3368. btrfs_tree_unlock(path->nodes[0]);
  3369. free_extent_buffer(path->nodes[0]);
  3370. path->nodes[0] = right;
  3371. path->slots[1] += 1;
  3372. } else {
  3373. btrfs_tree_unlock(right);
  3374. free_extent_buffer(right);
  3375. }
  3376. return 0;
  3377. out_unlock:
  3378. btrfs_tree_unlock(right);
  3379. free_extent_buffer(right);
  3380. return 1;
  3381. }
  3382. /*
  3383. * push some data in the path leaf to the right, trying to free up at
  3384. * least data_size bytes. returns zero if the push worked, nonzero otherwise
  3385. *
  3386. * returns 1 if the push failed because the other node didn't have enough
  3387. * room, 0 if everything worked out and < 0 if there were major errors.
  3388. *
  3389. * this will push starting from min_slot to the end of the leaf. It won't
  3390. * push any slot lower than min_slot
  3391. */
  3392. static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
  3393. *root, struct btrfs_path *path,
  3394. int min_data_size, int data_size,
  3395. int empty, u32 min_slot)
  3396. {
  3397. struct btrfs_fs_info *fs_info = root->fs_info;
  3398. struct extent_buffer *left = path->nodes[0];
  3399. struct extent_buffer *right;
  3400. struct extent_buffer *upper;
  3401. int slot;
  3402. int free_space;
  3403. u32 left_nritems;
  3404. int ret;
  3405. if (!path->nodes[1])
  3406. return 1;
  3407. slot = path->slots[1];
  3408. upper = path->nodes[1];
  3409. if (slot >= btrfs_header_nritems(upper) - 1)
  3410. return 1;
  3411. btrfs_assert_tree_locked(path->nodes[1]);
  3412. right = read_node_slot(fs_info, upper, slot + 1);
  3413. /*
  3414. * slot + 1 is not valid or we fail to read the right node,
  3415. * no big deal, just return.
  3416. */
  3417. if (IS_ERR(right))
  3418. return 1;
  3419. btrfs_tree_lock(right);
  3420. btrfs_set_lock_blocking(right);
  3421. free_space = btrfs_leaf_free_space(fs_info, right);
  3422. if (free_space < data_size)
  3423. goto out_unlock;
  3424. /* cow and double check */
  3425. ret = btrfs_cow_block(trans, root, right, upper,
  3426. slot + 1, &right);
  3427. if (ret)
  3428. goto out_unlock;
  3429. free_space = btrfs_leaf_free_space(fs_info, right);
  3430. if (free_space < data_size)
  3431. goto out_unlock;
  3432. left_nritems = btrfs_header_nritems(left);
  3433. if (left_nritems == 0)
  3434. goto out_unlock;
  3435. if (path->slots[0] == left_nritems && !empty) {
  3436. /* Key greater than all keys in the leaf, right neighbor has
  3437. * enough room for it and we're not emptying our leaf to delete
  3438. * it, therefore use right neighbor to insert the new item and
  3439. * no need to touch/dirty our left leaft. */
  3440. btrfs_tree_unlock(left);
  3441. free_extent_buffer(left);
  3442. path->nodes[0] = right;
  3443. path->slots[0] = 0;
  3444. path->slots[1]++;
  3445. return 0;
  3446. }
  3447. return __push_leaf_right(fs_info, path, min_data_size, empty,
  3448. right, free_space, left_nritems, min_slot);
  3449. out_unlock:
  3450. btrfs_tree_unlock(right);
  3451. free_extent_buffer(right);
  3452. return 1;
  3453. }
  3454. /*
  3455. * push some data in the path leaf to the left, trying to free up at
  3456. * least data_size bytes. returns zero if the push worked, nonzero otherwise
  3457. *
  3458. * max_slot can put a limit on how far into the leaf we'll push items. The
  3459. * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
  3460. * items
  3461. */
  3462. static noinline int __push_leaf_left(struct btrfs_fs_info *fs_info,
  3463. struct btrfs_path *path, int data_size,
  3464. int empty, struct extent_buffer *left,
  3465. int free_space, u32 right_nritems,
  3466. u32 max_slot)
  3467. {
  3468. struct btrfs_disk_key disk_key;
  3469. struct extent_buffer *right = path->nodes[0];
  3470. int i;
  3471. int push_space = 0;
  3472. int push_items = 0;
  3473. struct btrfs_item *item;
  3474. u32 old_left_nritems;
  3475. u32 nr;
  3476. int ret = 0;
  3477. u32 this_item_size;
  3478. u32 old_left_item_size;
  3479. struct btrfs_map_token token;
  3480. btrfs_init_map_token(&token);
  3481. if (empty)
  3482. nr = min(right_nritems, max_slot);
  3483. else
  3484. nr = min(right_nritems - 1, max_slot);
  3485. for (i = 0; i < nr; i++) {
  3486. item = btrfs_item_nr(i);
  3487. if (!empty && push_items > 0) {
  3488. if (path->slots[0] < i)
  3489. break;
  3490. if (path->slots[0] == i) {
  3491. int space = btrfs_leaf_free_space(fs_info, right);
  3492. if (space + push_space * 2 > free_space)
  3493. break;
  3494. }
  3495. }
  3496. if (path->slots[0] == i)
  3497. push_space += data_size;
  3498. this_item_size = btrfs_item_size(right, item);
  3499. if (this_item_size + sizeof(*item) + push_space > free_space)
  3500. break;
  3501. push_items++;
  3502. push_space += this_item_size + sizeof(*item);
  3503. }
  3504. if (push_items == 0) {
  3505. ret = 1;
  3506. goto out;
  3507. }
  3508. WARN_ON(!empty && push_items == btrfs_header_nritems(right));
  3509. /* push data from right to left */
  3510. copy_extent_buffer(left, right,
  3511. btrfs_item_nr_offset(btrfs_header_nritems(left)),
  3512. btrfs_item_nr_offset(0),
  3513. push_items * sizeof(struct btrfs_item));
  3514. push_space = BTRFS_LEAF_DATA_SIZE(fs_info) -
  3515. btrfs_item_offset_nr(right, push_items - 1);
  3516. copy_extent_buffer(left, right, BTRFS_LEAF_DATA_OFFSET +
  3517. leaf_data_end(fs_info, left) - push_space,
  3518. BTRFS_LEAF_DATA_OFFSET +
  3519. btrfs_item_offset_nr(right, push_items - 1),
  3520. push_space);
  3521. old_left_nritems = btrfs_header_nritems(left);
  3522. BUG_ON(old_left_nritems <= 0);
  3523. old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
  3524. for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
  3525. u32 ioff;
  3526. item = btrfs_item_nr(i);
  3527. ioff = btrfs_token_item_offset(left, item, &token);
  3528. btrfs_set_token_item_offset(left, item,
  3529. ioff - (BTRFS_LEAF_DATA_SIZE(fs_info) - old_left_item_size),
  3530. &token);
  3531. }
  3532. btrfs_set_header_nritems(left, old_left_nritems + push_items);
  3533. /* fixup right node */
  3534. if (push_items > right_nritems)
  3535. WARN(1, KERN_CRIT "push items %d nr %u\n", push_items,
  3536. right_nritems);
  3537. if (push_items < right_nritems) {
  3538. push_space = btrfs_item_offset_nr(right, push_items - 1) -
  3539. leaf_data_end(fs_info, right);
  3540. memmove_extent_buffer(right, BTRFS_LEAF_DATA_OFFSET +
  3541. BTRFS_LEAF_DATA_SIZE(fs_info) - push_space,
  3542. BTRFS_LEAF_DATA_OFFSET +
  3543. leaf_data_end(fs_info, right), push_space);
  3544. memmove_extent_buffer(right, btrfs_item_nr_offset(0),
  3545. btrfs_item_nr_offset(push_items),
  3546. (btrfs_header_nritems(right) - push_items) *
  3547. sizeof(struct btrfs_item));
  3548. }
  3549. right_nritems -= push_items;
  3550. btrfs_set_header_nritems(right, right_nritems);
  3551. push_space = BTRFS_LEAF_DATA_SIZE(fs_info);
  3552. for (i = 0; i < right_nritems; i++) {
  3553. item = btrfs_item_nr(i);
  3554. push_space = push_space - btrfs_token_item_size(right,
  3555. item, &token);
  3556. btrfs_set_token_item_offset(right, item, push_space, &token);
  3557. }
  3558. btrfs_mark_buffer_dirty(left);
  3559. if (right_nritems)
  3560. btrfs_mark_buffer_dirty(right);
  3561. else
  3562. clean_tree_block(fs_info, right);
  3563. btrfs_item_key(right, &disk_key, 0);
  3564. fixup_low_keys(path, &disk_key, 1);
  3565. /* then fixup the leaf pointer in the path */
  3566. if (path->slots[0] < push_items) {
  3567. path->slots[0] += old_left_nritems;
  3568. btrfs_tree_unlock(path->nodes[0]);
  3569. free_extent_buffer(path->nodes[0]);
  3570. path->nodes[0] = left;
  3571. path->slots[1] -= 1;
  3572. } else {
  3573. btrfs_tree_unlock(left);
  3574. free_extent_buffer(left);
  3575. path->slots[0] -= push_items;
  3576. }
  3577. BUG_ON(path->slots[0] < 0);
  3578. return ret;
  3579. out:
  3580. btrfs_tree_unlock(left);
  3581. free_extent_buffer(left);
  3582. return ret;
  3583. }
  3584. /*
  3585. * push some data in the path leaf to the left, trying to free up at
  3586. * least data_size bytes. returns zero if the push worked, nonzero otherwise
  3587. *
  3588. * max_slot can put a limit on how far into the leaf we'll push items. The
  3589. * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
  3590. * items
  3591. */
  3592. static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
  3593. *root, struct btrfs_path *path, int min_data_size,
  3594. int data_size, int empty, u32 max_slot)
  3595. {
  3596. struct btrfs_fs_info *fs_info = root->fs_info;
  3597. struct extent_buffer *right = path->nodes[0];
  3598. struct extent_buffer *left;
  3599. int slot;
  3600. int free_space;
  3601. u32 right_nritems;
  3602. int ret = 0;
  3603. slot = path->slots[1];
  3604. if (slot == 0)
  3605. return 1;
  3606. if (!path->nodes[1])
  3607. return 1;
  3608. right_nritems = btrfs_header_nritems(right);
  3609. if (right_nritems == 0)
  3610. return 1;
  3611. btrfs_assert_tree_locked(path->nodes[1]);
  3612. left = read_node_slot(fs_info, path->nodes[1], slot - 1);
  3613. /*
  3614. * slot - 1 is not valid or we fail to read the left node,
  3615. * no big deal, just return.
  3616. */
  3617. if (IS_ERR(left))
  3618. return 1;
  3619. btrfs_tree_lock(left);
  3620. btrfs_set_lock_blocking(left);
  3621. free_space = btrfs_leaf_free_space(fs_info, left);
  3622. if (free_space < data_size) {
  3623. ret = 1;
  3624. goto out;
  3625. }
  3626. /* cow and double check */
  3627. ret = btrfs_cow_block(trans, root, left,
  3628. path->nodes[1], slot - 1, &left);
  3629. if (ret) {
  3630. /* we hit -ENOSPC, but it isn't fatal here */
  3631. if (ret == -ENOSPC)
  3632. ret = 1;
  3633. goto out;
  3634. }
  3635. free_space = btrfs_leaf_free_space(fs_info, left);
  3636. if (free_space < data_size) {
  3637. ret = 1;
  3638. goto out;
  3639. }
  3640. return __push_leaf_left(fs_info, path, min_data_size,
  3641. empty, left, free_space, right_nritems,
  3642. max_slot);
  3643. out:
  3644. btrfs_tree_unlock(left);
  3645. free_extent_buffer(left);
  3646. return ret;
  3647. }
  3648. /*
  3649. * split the path's leaf in two, making sure there is at least data_size
  3650. * available for the resulting leaf level of the path.
  3651. */
  3652. static noinline void copy_for_split(struct btrfs_trans_handle *trans,
  3653. struct btrfs_fs_info *fs_info,
  3654. struct btrfs_path *path,
  3655. struct extent_buffer *l,
  3656. struct extent_buffer *right,
  3657. int slot, int mid, int nritems)
  3658. {
  3659. int data_copy_size;
  3660. int rt_data_off;
  3661. int i;
  3662. struct btrfs_disk_key disk_key;
  3663. struct btrfs_map_token token;
  3664. btrfs_init_map_token(&token);
  3665. nritems = nritems - mid;
  3666. btrfs_set_header_nritems(right, nritems);
  3667. data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(fs_info, l);
  3668. copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
  3669. btrfs_item_nr_offset(mid),
  3670. nritems * sizeof(struct btrfs_item));
  3671. copy_extent_buffer(right, l,
  3672. BTRFS_LEAF_DATA_OFFSET + BTRFS_LEAF_DATA_SIZE(fs_info) -
  3673. data_copy_size, BTRFS_LEAF_DATA_OFFSET +
  3674. leaf_data_end(fs_info, l), data_copy_size);
  3675. rt_data_off = BTRFS_LEAF_DATA_SIZE(fs_info) - btrfs_item_end_nr(l, mid);
  3676. for (i = 0; i < nritems; i++) {
  3677. struct btrfs_item *item = btrfs_item_nr(i);
  3678. u32 ioff;
  3679. ioff = btrfs_token_item_offset(right, item, &token);
  3680. btrfs_set_token_item_offset(right, item,
  3681. ioff + rt_data_off, &token);
  3682. }
  3683. btrfs_set_header_nritems(l, mid);
  3684. btrfs_item_key(right, &disk_key, 0);
  3685. insert_ptr(trans, fs_info, path, &disk_key, right->start,
  3686. path->slots[1] + 1, 1);
  3687. btrfs_mark_buffer_dirty(right);
  3688. btrfs_mark_buffer_dirty(l);
  3689. BUG_ON(path->slots[0] != slot);
  3690. if (mid <= slot) {
  3691. btrfs_tree_unlock(path->nodes[0]);
  3692. free_extent_buffer(path->nodes[0]);
  3693. path->nodes[0] = right;
  3694. path->slots[0] -= mid;
  3695. path->slots[1] += 1;
  3696. } else {
  3697. btrfs_tree_unlock(right);
  3698. free_extent_buffer(right);
  3699. }
  3700. BUG_ON(path->slots[0] < 0);
  3701. }
  3702. /*
  3703. * double splits happen when we need to insert a big item in the middle
  3704. * of a leaf. A double split can leave us with 3 mostly empty leaves:
  3705. * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
  3706. * A B C
  3707. *
  3708. * We avoid this by trying to push the items on either side of our target
  3709. * into the adjacent leaves. If all goes well we can avoid the double split
  3710. * completely.
  3711. */
  3712. static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
  3713. struct btrfs_root *root,
  3714. struct btrfs_path *path,
  3715. int data_size)
  3716. {
  3717. struct btrfs_fs_info *fs_info = root->fs_info;
  3718. int ret;
  3719. int progress = 0;
  3720. int slot;
  3721. u32 nritems;
  3722. int space_needed = data_size;
  3723. slot = path->slots[0];
  3724. if (slot < btrfs_header_nritems(path->nodes[0]))
  3725. space_needed -= btrfs_leaf_free_space(fs_info, path->nodes[0]);
  3726. /*
  3727. * try to push all the items after our slot into the
  3728. * right leaf
  3729. */
  3730. ret = push_leaf_right(trans, root, path, 1, space_needed, 0, slot);
  3731. if (ret < 0)
  3732. return ret;
  3733. if (ret == 0)
  3734. progress++;
  3735. nritems = btrfs_header_nritems(path->nodes[0]);
  3736. /*
  3737. * our goal is to get our slot at the start or end of a leaf. If
  3738. * we've done so we're done
  3739. */
  3740. if (path->slots[0] == 0 || path->slots[0] == nritems)
  3741. return 0;
  3742. if (btrfs_leaf_free_space(fs_info, path->nodes[0]) >= data_size)
  3743. return 0;
  3744. /* try to push all the items before our slot into the next leaf */
  3745. slot = path->slots[0];
  3746. space_needed = data_size;
  3747. if (slot > 0)
  3748. space_needed -= btrfs_leaf_free_space(fs_info, path->nodes[0]);
  3749. ret = push_leaf_left(trans, root, path, 1, space_needed, 0, slot);
  3750. if (ret < 0)
  3751. return ret;
  3752. if (ret == 0)
  3753. progress++;
  3754. if (progress)
  3755. return 0;
  3756. return 1;
  3757. }
  3758. /*
  3759. * split the path's leaf in two, making sure there is at least data_size
  3760. * available for the resulting leaf level of the path.
  3761. *
  3762. * returns 0 if all went well and < 0 on failure.
  3763. */
  3764. static noinline int split_leaf(struct btrfs_trans_handle *trans,
  3765. struct btrfs_root *root,
  3766. const struct btrfs_key *ins_key,
  3767. struct btrfs_path *path, int data_size,
  3768. int extend)
  3769. {
  3770. struct btrfs_disk_key disk_key;
  3771. struct extent_buffer *l;
  3772. u32 nritems;
  3773. int mid;
  3774. int slot;
  3775. struct extent_buffer *right;
  3776. struct btrfs_fs_info *fs_info = root->fs_info;
  3777. int ret = 0;
  3778. int wret;
  3779. int split;
  3780. int num_doubles = 0;
  3781. int tried_avoid_double = 0;
  3782. l = path->nodes[0];
  3783. slot = path->slots[0];
  3784. if (extend && data_size + btrfs_item_size_nr(l, slot) +
  3785. sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(fs_info))
  3786. return -EOVERFLOW;
  3787. /* first try to make some room by pushing left and right */
  3788. if (data_size && path->nodes[1]) {
  3789. int space_needed = data_size;
  3790. if (slot < btrfs_header_nritems(l))
  3791. space_needed -= btrfs_leaf_free_space(fs_info, l);
  3792. wret = push_leaf_right(trans, root, path, space_needed,
  3793. space_needed, 0, 0);
  3794. if (wret < 0)
  3795. return wret;
  3796. if (wret) {
  3797. space_needed = data_size;
  3798. if (slot > 0)
  3799. space_needed -= btrfs_leaf_free_space(fs_info,
  3800. l);
  3801. wret = push_leaf_left(trans, root, path, space_needed,
  3802. space_needed, 0, (u32)-1);
  3803. if (wret < 0)
  3804. return wret;
  3805. }
  3806. l = path->nodes[0];
  3807. /* did the pushes work? */
  3808. if (btrfs_leaf_free_space(fs_info, l) >= data_size)
  3809. return 0;
  3810. }
  3811. if (!path->nodes[1]) {
  3812. ret = insert_new_root(trans, root, path, 1);
  3813. if (ret)
  3814. return ret;
  3815. }
  3816. again:
  3817. split = 1;
  3818. l = path->nodes[0];
  3819. slot = path->slots[0];
  3820. nritems = btrfs_header_nritems(l);
  3821. mid = (nritems + 1) / 2;
  3822. if (mid <= slot) {
  3823. if (nritems == 1 ||
  3824. leaf_space_used(l, mid, nritems - mid) + data_size >
  3825. BTRFS_LEAF_DATA_SIZE(fs_info)) {
  3826. if (slot >= nritems) {
  3827. split = 0;
  3828. } else {
  3829. mid = slot;
  3830. if (mid != nritems &&
  3831. leaf_space_used(l, mid, nritems - mid) +
  3832. data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
  3833. if (data_size && !tried_avoid_double)
  3834. goto push_for_double;
  3835. split = 2;
  3836. }
  3837. }
  3838. }
  3839. } else {
  3840. if (leaf_space_used(l, 0, mid) + data_size >
  3841. BTRFS_LEAF_DATA_SIZE(fs_info)) {
  3842. if (!extend && data_size && slot == 0) {
  3843. split = 0;
  3844. } else if ((extend || !data_size) && slot == 0) {
  3845. mid = 1;
  3846. } else {
  3847. mid = slot;
  3848. if (mid != nritems &&
  3849. leaf_space_used(l, mid, nritems - mid) +
  3850. data_size > BTRFS_LEAF_DATA_SIZE(fs_info)) {
  3851. if (data_size && !tried_avoid_double)
  3852. goto push_for_double;
  3853. split = 2;
  3854. }
  3855. }
  3856. }
  3857. }
  3858. if (split == 0)
  3859. btrfs_cpu_key_to_disk(&disk_key, ins_key);
  3860. else
  3861. btrfs_item_key(l, &disk_key, mid);
  3862. right = alloc_tree_block_no_bg_flush(trans, root, 0, &disk_key, 0,
  3863. l->start, 0);
  3864. if (IS_ERR(right))
  3865. return PTR_ERR(right);
  3866. root_add_used(root, fs_info->nodesize);
  3867. if (split == 0) {
  3868. if (mid <= slot) {
  3869. btrfs_set_header_nritems(right, 0);
  3870. insert_ptr(trans, fs_info, path, &disk_key,
  3871. right->start, path->slots[1] + 1, 1);
  3872. btrfs_tree_unlock(path->nodes[0]);
  3873. free_extent_buffer(path->nodes[0]);
  3874. path->nodes[0] = right;
  3875. path->slots[0] = 0;
  3876. path->slots[1] += 1;
  3877. } else {
  3878. btrfs_set_header_nritems(right, 0);
  3879. insert_ptr(trans, fs_info, path, &disk_key,
  3880. right->start, path->slots[1], 1);
  3881. btrfs_tree_unlock(path->nodes[0]);
  3882. free_extent_buffer(path->nodes[0]);
  3883. path->nodes[0] = right;
  3884. path->slots[0] = 0;
  3885. if (path->slots[1] == 0)
  3886. fixup_low_keys(path, &disk_key, 1);
  3887. }
  3888. /*
  3889. * We create a new leaf 'right' for the required ins_len and
  3890. * we'll do btrfs_mark_buffer_dirty() on this leaf after copying
  3891. * the content of ins_len to 'right'.
  3892. */
  3893. return ret;
  3894. }
  3895. copy_for_split(trans, fs_info, path, l, right, slot, mid, nritems);
  3896. if (split == 2) {
  3897. BUG_ON(num_doubles != 0);
  3898. num_doubles++;
  3899. goto again;
  3900. }
  3901. return 0;
  3902. push_for_double:
  3903. push_for_double_split(trans, root, path, data_size);
  3904. tried_avoid_double = 1;
  3905. if (btrfs_leaf_free_space(fs_info, path->nodes[0]) >= data_size)
  3906. return 0;
  3907. goto again;
  3908. }
  3909. static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
  3910. struct btrfs_root *root,
  3911. struct btrfs_path *path, int ins_len)
  3912. {
  3913. struct btrfs_fs_info *fs_info = root->fs_info;
  3914. struct btrfs_key key;
  3915. struct extent_buffer *leaf;
  3916. struct btrfs_file_extent_item *fi;
  3917. u64 extent_len = 0;
  3918. u32 item_size;
  3919. int ret;
  3920. leaf = path->nodes[0];
  3921. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  3922. BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
  3923. key.type != BTRFS_EXTENT_CSUM_KEY);
  3924. if (btrfs_leaf_free_space(fs_info, leaf) >= ins_len)
  3925. return 0;
  3926. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  3927. if (key.type == BTRFS_EXTENT_DATA_KEY) {
  3928. fi = btrfs_item_ptr(leaf, path->slots[0],
  3929. struct btrfs_file_extent_item);
  3930. extent_len = btrfs_file_extent_num_bytes(leaf, fi);
  3931. }
  3932. btrfs_release_path(path);
  3933. path->keep_locks = 1;
  3934. path->search_for_split = 1;
  3935. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  3936. path->search_for_split = 0;
  3937. if (ret > 0)
  3938. ret = -EAGAIN;
  3939. if (ret < 0)
  3940. goto err;
  3941. ret = -EAGAIN;
  3942. leaf = path->nodes[0];
  3943. /* if our item isn't there, return now */
  3944. if (item_size != btrfs_item_size_nr(leaf, path->slots[0]))
  3945. goto err;
  3946. /* the leaf has changed, it now has room. return now */
  3947. if (btrfs_leaf_free_space(fs_info, path->nodes[0]) >= ins_len)
  3948. goto err;
  3949. if (key.type == BTRFS_EXTENT_DATA_KEY) {
  3950. fi = btrfs_item_ptr(leaf, path->slots[0],
  3951. struct btrfs_file_extent_item);
  3952. if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
  3953. goto err;
  3954. }
  3955. btrfs_set_path_blocking(path);
  3956. ret = split_leaf(trans, root, &key, path, ins_len, 1);
  3957. if (ret)
  3958. goto err;
  3959. path->keep_locks = 0;
  3960. btrfs_unlock_up_safe(path, 1);
  3961. return 0;
  3962. err:
  3963. path->keep_locks = 0;
  3964. return ret;
  3965. }
  3966. static noinline int split_item(struct btrfs_fs_info *fs_info,
  3967. struct btrfs_path *path,
  3968. const struct btrfs_key *new_key,
  3969. unsigned long split_offset)
  3970. {
  3971. struct extent_buffer *leaf;
  3972. struct btrfs_item *item;
  3973. struct btrfs_item *new_item;
  3974. int slot;
  3975. char *buf;
  3976. u32 nritems;
  3977. u32 item_size;
  3978. u32 orig_offset;
  3979. struct btrfs_disk_key disk_key;
  3980. leaf = path->nodes[0];
  3981. BUG_ON(btrfs_leaf_free_space(fs_info, leaf) < sizeof(struct btrfs_item));
  3982. btrfs_set_path_blocking(path);
  3983. item = btrfs_item_nr(path->slots[0]);
  3984. orig_offset = btrfs_item_offset(leaf, item);
  3985. item_size = btrfs_item_size(leaf, item);
  3986. buf = kmalloc(item_size, GFP_NOFS);
  3987. if (!buf)
  3988. return -ENOMEM;
  3989. read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
  3990. path->slots[0]), item_size);
  3991. slot = path->slots[0] + 1;
  3992. nritems = btrfs_header_nritems(leaf);
  3993. if (slot != nritems) {
  3994. /* shift the items */
  3995. memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
  3996. btrfs_item_nr_offset(slot),
  3997. (nritems - slot) * sizeof(struct btrfs_item));
  3998. }
  3999. btrfs_cpu_key_to_disk(&disk_key, new_key);
  4000. btrfs_set_item_key(leaf, &disk_key, slot);
  4001. new_item = btrfs_item_nr(slot);
  4002. btrfs_set_item_offset(leaf, new_item, orig_offset);
  4003. btrfs_set_item_size(leaf, new_item, item_size - split_offset);
  4004. btrfs_set_item_offset(leaf, item,
  4005. orig_offset + item_size - split_offset);
  4006. btrfs_set_item_size(leaf, item, split_offset);
  4007. btrfs_set_header_nritems(leaf, nritems + 1);
  4008. /* write the data for the start of the original item */
  4009. write_extent_buffer(leaf, buf,
  4010. btrfs_item_ptr_offset(leaf, path->slots[0]),
  4011. split_offset);
  4012. /* write the data for the new item */
  4013. write_extent_buffer(leaf, buf + split_offset,
  4014. btrfs_item_ptr_offset(leaf, slot),
  4015. item_size - split_offset);
  4016. btrfs_mark_buffer_dirty(leaf);
  4017. BUG_ON(btrfs_leaf_free_space(fs_info, leaf) < 0);
  4018. kfree(buf);
  4019. return 0;
  4020. }
  4021. /*
  4022. * This function splits a single item into two items,
  4023. * giving 'new_key' to the new item and splitting the
  4024. * old one at split_offset (from the start of the item).
  4025. *
  4026. * The path may be released by this operation. After
  4027. * the split, the path is pointing to the old item. The
  4028. * new item is going to be in the same node as the old one.
  4029. *
  4030. * Note, the item being split must be smaller enough to live alone on
  4031. * a tree block with room for one extra struct btrfs_item
  4032. *
  4033. * This allows us to split the item in place, keeping a lock on the
  4034. * leaf the entire time.
  4035. */
  4036. int btrfs_split_item(struct btrfs_trans_handle *trans,
  4037. struct btrfs_root *root,
  4038. struct btrfs_path *path,
  4039. const struct btrfs_key *new_key,
  4040. unsigned long split_offset)
  4041. {
  4042. int ret;
  4043. ret = setup_leaf_for_split(trans, root, path,
  4044. sizeof(struct btrfs_item));
  4045. if (ret)
  4046. return ret;
  4047. ret = split_item(root->fs_info, path, new_key, split_offset);
  4048. return ret;
  4049. }
  4050. /*
  4051. * This function duplicate a item, giving 'new_key' to the new item.
  4052. * It guarantees both items live in the same tree leaf and the new item
  4053. * is contiguous with the original item.
  4054. *
  4055. * This allows us to split file extent in place, keeping a lock on the
  4056. * leaf the entire time.
  4057. */
  4058. int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
  4059. struct btrfs_root *root,
  4060. struct btrfs_path *path,
  4061. const struct btrfs_key *new_key)
  4062. {
  4063. struct extent_buffer *leaf;
  4064. int ret;
  4065. u32 item_size;
  4066. leaf = path->nodes[0];
  4067. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  4068. ret = setup_leaf_for_split(trans, root, path,
  4069. item_size + sizeof(struct btrfs_item));
  4070. if (ret)
  4071. return ret;
  4072. path->slots[0]++;
  4073. setup_items_for_insert(root, path, new_key, &item_size,
  4074. item_size, item_size +
  4075. sizeof(struct btrfs_item), 1);
  4076. leaf = path->nodes[0];
  4077. memcpy_extent_buffer(leaf,
  4078. btrfs_item_ptr_offset(leaf, path->slots[0]),
  4079. btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
  4080. item_size);
  4081. return 0;
  4082. }
  4083. /*
  4084. * make the item pointed to by the path smaller. new_size indicates
  4085. * how small to make it, and from_end tells us if we just chop bytes
  4086. * off the end of the item or if we shift the item to chop bytes off
  4087. * the front.
  4088. */
  4089. void btrfs_truncate_item(struct btrfs_fs_info *fs_info,
  4090. struct btrfs_path *path, u32 new_size, int from_end)
  4091. {
  4092. int slot;
  4093. struct extent_buffer *leaf;
  4094. struct btrfs_item *item;
  4095. u32 nritems;
  4096. unsigned int data_end;
  4097. unsigned int old_data_start;
  4098. unsigned int old_size;
  4099. unsigned int size_diff;
  4100. int i;
  4101. struct btrfs_map_token token;
  4102. btrfs_init_map_token(&token);
  4103. leaf = path->nodes[0];
  4104. slot = path->slots[0];
  4105. old_size = btrfs_item_size_nr(leaf, slot);
  4106. if (old_size == new_size)
  4107. return;
  4108. nritems = btrfs_header_nritems(leaf);
  4109. data_end = leaf_data_end(fs_info, leaf);
  4110. old_data_start = btrfs_item_offset_nr(leaf, slot);
  4111. size_diff = old_size - new_size;
  4112. BUG_ON(slot < 0);
  4113. BUG_ON(slot >= nritems);
  4114. /*
  4115. * item0..itemN ... dataN.offset..dataN.size .. data0.size
  4116. */
  4117. /* first correct the data pointers */
  4118. for (i = slot; i < nritems; i++) {
  4119. u32 ioff;
  4120. item = btrfs_item_nr(i);
  4121. ioff = btrfs_token_item_offset(leaf, item, &token);
  4122. btrfs_set_token_item_offset(leaf, item,
  4123. ioff + size_diff, &token);
  4124. }
  4125. /* shift the data */
  4126. if (from_end) {
  4127. memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
  4128. data_end + size_diff, BTRFS_LEAF_DATA_OFFSET +
  4129. data_end, old_data_start + new_size - data_end);
  4130. } else {
  4131. struct btrfs_disk_key disk_key;
  4132. u64 offset;
  4133. btrfs_item_key(leaf, &disk_key, slot);
  4134. if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
  4135. unsigned long ptr;
  4136. struct btrfs_file_extent_item *fi;
  4137. fi = btrfs_item_ptr(leaf, slot,
  4138. struct btrfs_file_extent_item);
  4139. fi = (struct btrfs_file_extent_item *)(
  4140. (unsigned long)fi - size_diff);
  4141. if (btrfs_file_extent_type(leaf, fi) ==
  4142. BTRFS_FILE_EXTENT_INLINE) {
  4143. ptr = btrfs_item_ptr_offset(leaf, slot);
  4144. memmove_extent_buffer(leaf, ptr,
  4145. (unsigned long)fi,
  4146. BTRFS_FILE_EXTENT_INLINE_DATA_START);
  4147. }
  4148. }
  4149. memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
  4150. data_end + size_diff, BTRFS_LEAF_DATA_OFFSET +
  4151. data_end, old_data_start - data_end);
  4152. offset = btrfs_disk_key_offset(&disk_key);
  4153. btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
  4154. btrfs_set_item_key(leaf, &disk_key, slot);
  4155. if (slot == 0)
  4156. fixup_low_keys(path, &disk_key, 1);
  4157. }
  4158. item = btrfs_item_nr(slot);
  4159. btrfs_set_item_size(leaf, item, new_size);
  4160. btrfs_mark_buffer_dirty(leaf);
  4161. if (btrfs_leaf_free_space(fs_info, leaf) < 0) {
  4162. btrfs_print_leaf(leaf);
  4163. BUG();
  4164. }
  4165. }
  4166. /*
  4167. * make the item pointed to by the path bigger, data_size is the added size.
  4168. */
  4169. void btrfs_extend_item(struct btrfs_fs_info *fs_info, struct btrfs_path *path,
  4170. u32 data_size)
  4171. {
  4172. int slot;
  4173. struct extent_buffer *leaf;
  4174. struct btrfs_item *item;
  4175. u32 nritems;
  4176. unsigned int data_end;
  4177. unsigned int old_data;
  4178. unsigned int old_size;
  4179. int i;
  4180. struct btrfs_map_token token;
  4181. btrfs_init_map_token(&token);
  4182. leaf = path->nodes[0];
  4183. nritems = btrfs_header_nritems(leaf);
  4184. data_end = leaf_data_end(fs_info, leaf);
  4185. if (btrfs_leaf_free_space(fs_info, leaf) < data_size) {
  4186. btrfs_print_leaf(leaf);
  4187. BUG();
  4188. }
  4189. slot = path->slots[0];
  4190. old_data = btrfs_item_end_nr(leaf, slot);
  4191. BUG_ON(slot < 0);
  4192. if (slot >= nritems) {
  4193. btrfs_print_leaf(leaf);
  4194. btrfs_crit(fs_info, "slot %d too large, nritems %d",
  4195. slot, nritems);
  4196. BUG_ON(1);
  4197. }
  4198. /*
  4199. * item0..itemN ... dataN.offset..dataN.size .. data0.size
  4200. */
  4201. /* first correct the data pointers */
  4202. for (i = slot; i < nritems; i++) {
  4203. u32 ioff;
  4204. item = btrfs_item_nr(i);
  4205. ioff = btrfs_token_item_offset(leaf, item, &token);
  4206. btrfs_set_token_item_offset(leaf, item,
  4207. ioff - data_size, &token);
  4208. }
  4209. /* shift the data */
  4210. memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
  4211. data_end - data_size, BTRFS_LEAF_DATA_OFFSET +
  4212. data_end, old_data - data_end);
  4213. data_end = old_data;
  4214. old_size = btrfs_item_size_nr(leaf, slot);
  4215. item = btrfs_item_nr(slot);
  4216. btrfs_set_item_size(leaf, item, old_size + data_size);
  4217. btrfs_mark_buffer_dirty(leaf);
  4218. if (btrfs_leaf_free_space(fs_info, leaf) < 0) {
  4219. btrfs_print_leaf(leaf);
  4220. BUG();
  4221. }
  4222. }
  4223. /*
  4224. * this is a helper for btrfs_insert_empty_items, the main goal here is
  4225. * to save stack depth by doing the bulk of the work in a function
  4226. * that doesn't call btrfs_search_slot
  4227. */
  4228. void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
  4229. const struct btrfs_key *cpu_key, u32 *data_size,
  4230. u32 total_data, u32 total_size, int nr)
  4231. {
  4232. struct btrfs_fs_info *fs_info = root->fs_info;
  4233. struct btrfs_item *item;
  4234. int i;
  4235. u32 nritems;
  4236. unsigned int data_end;
  4237. struct btrfs_disk_key disk_key;
  4238. struct extent_buffer *leaf;
  4239. int slot;
  4240. struct btrfs_map_token token;
  4241. if (path->slots[0] == 0) {
  4242. btrfs_cpu_key_to_disk(&disk_key, cpu_key);
  4243. fixup_low_keys(path, &disk_key, 1);
  4244. }
  4245. btrfs_unlock_up_safe(path, 1);
  4246. btrfs_init_map_token(&token);
  4247. leaf = path->nodes[0];
  4248. slot = path->slots[0];
  4249. nritems = btrfs_header_nritems(leaf);
  4250. data_end = leaf_data_end(fs_info, leaf);
  4251. if (btrfs_leaf_free_space(fs_info, leaf) < total_size) {
  4252. btrfs_print_leaf(leaf);
  4253. btrfs_crit(fs_info, "not enough freespace need %u have %d",
  4254. total_size, btrfs_leaf_free_space(fs_info, leaf));
  4255. BUG();
  4256. }
  4257. if (slot != nritems) {
  4258. unsigned int old_data = btrfs_item_end_nr(leaf, slot);
  4259. if (old_data < data_end) {
  4260. btrfs_print_leaf(leaf);
  4261. btrfs_crit(fs_info, "slot %d old_data %d data_end %d",
  4262. slot, old_data, data_end);
  4263. BUG_ON(1);
  4264. }
  4265. /*
  4266. * item0..itemN ... dataN.offset..dataN.size .. data0.size
  4267. */
  4268. /* first correct the data pointers */
  4269. for (i = slot; i < nritems; i++) {
  4270. u32 ioff;
  4271. item = btrfs_item_nr(i);
  4272. ioff = btrfs_token_item_offset(leaf, item, &token);
  4273. btrfs_set_token_item_offset(leaf, item,
  4274. ioff - total_data, &token);
  4275. }
  4276. /* shift the items */
  4277. memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
  4278. btrfs_item_nr_offset(slot),
  4279. (nritems - slot) * sizeof(struct btrfs_item));
  4280. /* shift the data */
  4281. memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
  4282. data_end - total_data, BTRFS_LEAF_DATA_OFFSET +
  4283. data_end, old_data - data_end);
  4284. data_end = old_data;
  4285. }
  4286. /* setup the item for the new data */
  4287. for (i = 0; i < nr; i++) {
  4288. btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
  4289. btrfs_set_item_key(leaf, &disk_key, slot + i);
  4290. item = btrfs_item_nr(slot + i);
  4291. btrfs_set_token_item_offset(leaf, item,
  4292. data_end - data_size[i], &token);
  4293. data_end -= data_size[i];
  4294. btrfs_set_token_item_size(leaf, item, data_size[i], &token);
  4295. }
  4296. btrfs_set_header_nritems(leaf, nritems + nr);
  4297. btrfs_mark_buffer_dirty(leaf);
  4298. if (btrfs_leaf_free_space(fs_info, leaf) < 0) {
  4299. btrfs_print_leaf(leaf);
  4300. BUG();
  4301. }
  4302. }
  4303. /*
  4304. * Given a key and some data, insert items into the tree.
  4305. * This does all the path init required, making room in the tree if needed.
  4306. */
  4307. int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
  4308. struct btrfs_root *root,
  4309. struct btrfs_path *path,
  4310. const struct btrfs_key *cpu_key, u32 *data_size,
  4311. int nr)
  4312. {
  4313. int ret = 0;
  4314. int slot;
  4315. int i;
  4316. u32 total_size = 0;
  4317. u32 total_data = 0;
  4318. for (i = 0; i < nr; i++)
  4319. total_data += data_size[i];
  4320. total_size = total_data + (nr * sizeof(struct btrfs_item));
  4321. ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
  4322. if (ret == 0)
  4323. return -EEXIST;
  4324. if (ret < 0)
  4325. return ret;
  4326. slot = path->slots[0];
  4327. BUG_ON(slot < 0);
  4328. setup_items_for_insert(root, path, cpu_key, data_size,
  4329. total_data, total_size, nr);
  4330. return 0;
  4331. }
  4332. /*
  4333. * Given a key and some data, insert an item into the tree.
  4334. * This does all the path init required, making room in the tree if needed.
  4335. */
  4336. int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  4337. const struct btrfs_key *cpu_key, void *data,
  4338. u32 data_size)
  4339. {
  4340. int ret = 0;
  4341. struct btrfs_path *path;
  4342. struct extent_buffer *leaf;
  4343. unsigned long ptr;
  4344. path = btrfs_alloc_path();
  4345. if (!path)
  4346. return -ENOMEM;
  4347. ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
  4348. if (!ret) {
  4349. leaf = path->nodes[0];
  4350. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  4351. write_extent_buffer(leaf, data, ptr, data_size);
  4352. btrfs_mark_buffer_dirty(leaf);
  4353. }
  4354. btrfs_free_path(path);
  4355. return ret;
  4356. }
  4357. /*
  4358. * delete the pointer from a given node.
  4359. *
  4360. * the tree should have been previously balanced so the deletion does not
  4361. * empty a node.
  4362. */
  4363. static void del_ptr(struct btrfs_root *root, struct btrfs_path *path,
  4364. int level, int slot)
  4365. {
  4366. struct extent_buffer *parent = path->nodes[level];
  4367. u32 nritems;
  4368. int ret;
  4369. nritems = btrfs_header_nritems(parent);
  4370. if (slot != nritems - 1) {
  4371. if (level) {
  4372. ret = tree_mod_log_insert_move(parent, slot, slot + 1,
  4373. nritems - slot - 1);
  4374. BUG_ON(ret < 0);
  4375. }
  4376. memmove_extent_buffer(parent,
  4377. btrfs_node_key_ptr_offset(slot),
  4378. btrfs_node_key_ptr_offset(slot + 1),
  4379. sizeof(struct btrfs_key_ptr) *
  4380. (nritems - slot - 1));
  4381. } else if (level) {
  4382. ret = tree_mod_log_insert_key(parent, slot, MOD_LOG_KEY_REMOVE,
  4383. GFP_NOFS);
  4384. BUG_ON(ret < 0);
  4385. }
  4386. nritems--;
  4387. btrfs_set_header_nritems(parent, nritems);
  4388. if (nritems == 0 && parent == root->node) {
  4389. BUG_ON(btrfs_header_level(root->node) != 1);
  4390. /* just turn the root into a leaf and break */
  4391. btrfs_set_header_level(root->node, 0);
  4392. } else if (slot == 0) {
  4393. struct btrfs_disk_key disk_key;
  4394. btrfs_node_key(parent, &disk_key, 0);
  4395. fixup_low_keys(path, &disk_key, level + 1);
  4396. }
  4397. btrfs_mark_buffer_dirty(parent);
  4398. }
  4399. /*
  4400. * a helper function to delete the leaf pointed to by path->slots[1] and
  4401. * path->nodes[1].
  4402. *
  4403. * This deletes the pointer in path->nodes[1] and frees the leaf
  4404. * block extent. zero is returned if it all worked out, < 0 otherwise.
  4405. *
  4406. * The path must have already been setup for deleting the leaf, including
  4407. * all the proper balancing. path->nodes[1] must be locked.
  4408. */
  4409. static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
  4410. struct btrfs_root *root,
  4411. struct btrfs_path *path,
  4412. struct extent_buffer *leaf)
  4413. {
  4414. WARN_ON(btrfs_header_generation(leaf) != trans->transid);
  4415. del_ptr(root, path, 1, path->slots[1]);
  4416. /*
  4417. * btrfs_free_extent is expensive, we want to make sure we
  4418. * aren't holding any locks when we call it
  4419. */
  4420. btrfs_unlock_up_safe(path, 0);
  4421. root_sub_used(root, leaf->len);
  4422. extent_buffer_get(leaf);
  4423. btrfs_free_tree_block(trans, root, leaf, 0, 1);
  4424. free_extent_buffer_stale(leaf);
  4425. }
  4426. /*
  4427. * delete the item at the leaf level in path. If that empties
  4428. * the leaf, remove it from the tree
  4429. */
  4430. int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  4431. struct btrfs_path *path, int slot, int nr)
  4432. {
  4433. struct btrfs_fs_info *fs_info = root->fs_info;
  4434. struct extent_buffer *leaf;
  4435. struct btrfs_item *item;
  4436. u32 last_off;
  4437. u32 dsize = 0;
  4438. int ret = 0;
  4439. int wret;
  4440. int i;
  4441. u32 nritems;
  4442. struct btrfs_map_token token;
  4443. btrfs_init_map_token(&token);
  4444. leaf = path->nodes[0];
  4445. last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
  4446. for (i = 0; i < nr; i++)
  4447. dsize += btrfs_item_size_nr(leaf, slot + i);
  4448. nritems = btrfs_header_nritems(leaf);
  4449. if (slot + nr != nritems) {
  4450. int data_end = leaf_data_end(fs_info, leaf);
  4451. memmove_extent_buffer(leaf, BTRFS_LEAF_DATA_OFFSET +
  4452. data_end + dsize,
  4453. BTRFS_LEAF_DATA_OFFSET + data_end,
  4454. last_off - data_end);
  4455. for (i = slot + nr; i < nritems; i++) {
  4456. u32 ioff;
  4457. item = btrfs_item_nr(i);
  4458. ioff = btrfs_token_item_offset(leaf, item, &token);
  4459. btrfs_set_token_item_offset(leaf, item,
  4460. ioff + dsize, &token);
  4461. }
  4462. memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
  4463. btrfs_item_nr_offset(slot + nr),
  4464. sizeof(struct btrfs_item) *
  4465. (nritems - slot - nr));
  4466. }
  4467. btrfs_set_header_nritems(leaf, nritems - nr);
  4468. nritems -= nr;
  4469. /* delete the leaf if we've emptied it */
  4470. if (nritems == 0) {
  4471. if (leaf == root->node) {
  4472. btrfs_set_header_level(leaf, 0);
  4473. } else {
  4474. btrfs_set_path_blocking(path);
  4475. clean_tree_block(fs_info, leaf);
  4476. btrfs_del_leaf(trans, root, path, leaf);
  4477. }
  4478. } else {
  4479. int used = leaf_space_used(leaf, 0, nritems);
  4480. if (slot == 0) {
  4481. struct btrfs_disk_key disk_key;
  4482. btrfs_item_key(leaf, &disk_key, 0);
  4483. fixup_low_keys(path, &disk_key, 1);
  4484. }
  4485. /* delete the leaf if it is mostly empty */
  4486. if (used < BTRFS_LEAF_DATA_SIZE(fs_info) / 3) {
  4487. /* push_leaf_left fixes the path.
  4488. * make sure the path still points to our leaf
  4489. * for possible call to del_ptr below
  4490. */
  4491. slot = path->slots[1];
  4492. extent_buffer_get(leaf);
  4493. btrfs_set_path_blocking(path);
  4494. wret = push_leaf_left(trans, root, path, 1, 1,
  4495. 1, (u32)-1);
  4496. if (wret < 0 && wret != -ENOSPC)
  4497. ret = wret;
  4498. if (path->nodes[0] == leaf &&
  4499. btrfs_header_nritems(leaf)) {
  4500. wret = push_leaf_right(trans, root, path, 1,
  4501. 1, 1, 0);
  4502. if (wret < 0 && wret != -ENOSPC)
  4503. ret = wret;
  4504. }
  4505. if (btrfs_header_nritems(leaf) == 0) {
  4506. path->slots[1] = slot;
  4507. btrfs_del_leaf(trans, root, path, leaf);
  4508. free_extent_buffer(leaf);
  4509. ret = 0;
  4510. } else {
  4511. /* if we're still in the path, make sure
  4512. * we're dirty. Otherwise, one of the
  4513. * push_leaf functions must have already
  4514. * dirtied this buffer
  4515. */
  4516. if (path->nodes[0] == leaf)
  4517. btrfs_mark_buffer_dirty(leaf);
  4518. free_extent_buffer(leaf);
  4519. }
  4520. } else {
  4521. btrfs_mark_buffer_dirty(leaf);
  4522. }
  4523. }
  4524. return ret;
  4525. }
  4526. /*
  4527. * search the tree again to find a leaf with lesser keys
  4528. * returns 0 if it found something or 1 if there are no lesser leaves.
  4529. * returns < 0 on io errors.
  4530. *
  4531. * This may release the path, and so you may lose any locks held at the
  4532. * time you call it.
  4533. */
  4534. int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
  4535. {
  4536. struct btrfs_key key;
  4537. struct btrfs_disk_key found_key;
  4538. int ret;
  4539. btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
  4540. if (key.offset > 0) {
  4541. key.offset--;
  4542. } else if (key.type > 0) {
  4543. key.type--;
  4544. key.offset = (u64)-1;
  4545. } else if (key.objectid > 0) {
  4546. key.objectid--;
  4547. key.type = (u8)-1;
  4548. key.offset = (u64)-1;
  4549. } else {
  4550. return 1;
  4551. }
  4552. btrfs_release_path(path);
  4553. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4554. if (ret < 0)
  4555. return ret;
  4556. btrfs_item_key(path->nodes[0], &found_key, 0);
  4557. ret = comp_keys(&found_key, &key);
  4558. /*
  4559. * We might have had an item with the previous key in the tree right
  4560. * before we released our path. And after we released our path, that
  4561. * item might have been pushed to the first slot (0) of the leaf we
  4562. * were holding due to a tree balance. Alternatively, an item with the
  4563. * previous key can exist as the only element of a leaf (big fat item).
  4564. * Therefore account for these 2 cases, so that our callers (like
  4565. * btrfs_previous_item) don't miss an existing item with a key matching
  4566. * the previous key we computed above.
  4567. */
  4568. if (ret <= 0)
  4569. return 0;
  4570. return 1;
  4571. }
  4572. /*
  4573. * A helper function to walk down the tree starting at min_key, and looking
  4574. * for nodes or leaves that are have a minimum transaction id.
  4575. * This is used by the btree defrag code, and tree logging
  4576. *
  4577. * This does not cow, but it does stuff the starting key it finds back
  4578. * into min_key, so you can call btrfs_search_slot with cow=1 on the
  4579. * key and get a writable path.
  4580. *
  4581. * This honors path->lowest_level to prevent descent past a given level
  4582. * of the tree.
  4583. *
  4584. * min_trans indicates the oldest transaction that you are interested
  4585. * in walking through. Any nodes or leaves older than min_trans are
  4586. * skipped over (without reading them).
  4587. *
  4588. * returns zero if something useful was found, < 0 on error and 1 if there
  4589. * was nothing in the tree that matched the search criteria.
  4590. */
  4591. int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
  4592. struct btrfs_path *path,
  4593. u64 min_trans)
  4594. {
  4595. struct btrfs_fs_info *fs_info = root->fs_info;
  4596. struct extent_buffer *cur;
  4597. struct btrfs_key found_key;
  4598. int slot;
  4599. int sret;
  4600. u32 nritems;
  4601. int level;
  4602. int ret = 1;
  4603. int keep_locks = path->keep_locks;
  4604. path->keep_locks = 1;
  4605. again:
  4606. cur = btrfs_read_lock_root_node(root);
  4607. level = btrfs_header_level(cur);
  4608. WARN_ON(path->nodes[level]);
  4609. path->nodes[level] = cur;
  4610. path->locks[level] = BTRFS_READ_LOCK;
  4611. if (btrfs_header_generation(cur) < min_trans) {
  4612. ret = 1;
  4613. goto out;
  4614. }
  4615. while (1) {
  4616. nritems = btrfs_header_nritems(cur);
  4617. level = btrfs_header_level(cur);
  4618. sret = btrfs_bin_search(cur, min_key, level, &slot);
  4619. /* at the lowest level, we're done, setup the path and exit */
  4620. if (level == path->lowest_level) {
  4621. if (slot >= nritems)
  4622. goto find_next_key;
  4623. ret = 0;
  4624. path->slots[level] = slot;
  4625. btrfs_item_key_to_cpu(cur, &found_key, slot);
  4626. goto out;
  4627. }
  4628. if (sret && slot > 0)
  4629. slot--;
  4630. /*
  4631. * check this node pointer against the min_trans parameters.
  4632. * If it is too old, old, skip to the next one.
  4633. */
  4634. while (slot < nritems) {
  4635. u64 gen;
  4636. gen = btrfs_node_ptr_generation(cur, slot);
  4637. if (gen < min_trans) {
  4638. slot++;
  4639. continue;
  4640. }
  4641. break;
  4642. }
  4643. find_next_key:
  4644. /*
  4645. * we didn't find a candidate key in this node, walk forward
  4646. * and find another one
  4647. */
  4648. if (slot >= nritems) {
  4649. path->slots[level] = slot;
  4650. btrfs_set_path_blocking(path);
  4651. sret = btrfs_find_next_key(root, path, min_key, level,
  4652. min_trans);
  4653. if (sret == 0) {
  4654. btrfs_release_path(path);
  4655. goto again;
  4656. } else {
  4657. goto out;
  4658. }
  4659. }
  4660. /* save our key for returning back */
  4661. btrfs_node_key_to_cpu(cur, &found_key, slot);
  4662. path->slots[level] = slot;
  4663. if (level == path->lowest_level) {
  4664. ret = 0;
  4665. goto out;
  4666. }
  4667. btrfs_set_path_blocking(path);
  4668. cur = read_node_slot(fs_info, cur, slot);
  4669. if (IS_ERR(cur)) {
  4670. ret = PTR_ERR(cur);
  4671. goto out;
  4672. }
  4673. btrfs_tree_read_lock(cur);
  4674. path->locks[level - 1] = BTRFS_READ_LOCK;
  4675. path->nodes[level - 1] = cur;
  4676. unlock_up(path, level, 1, 0, NULL);
  4677. btrfs_clear_path_blocking(path, NULL, 0);
  4678. }
  4679. out:
  4680. path->keep_locks = keep_locks;
  4681. if (ret == 0) {
  4682. btrfs_unlock_up_safe(path, path->lowest_level + 1);
  4683. btrfs_set_path_blocking(path);
  4684. memcpy(min_key, &found_key, sizeof(found_key));
  4685. }
  4686. return ret;
  4687. }
  4688. static int tree_move_down(struct btrfs_fs_info *fs_info,
  4689. struct btrfs_path *path,
  4690. int *level)
  4691. {
  4692. struct extent_buffer *eb;
  4693. BUG_ON(*level == 0);
  4694. eb = read_node_slot(fs_info, path->nodes[*level], path->slots[*level]);
  4695. if (IS_ERR(eb))
  4696. return PTR_ERR(eb);
  4697. path->nodes[*level - 1] = eb;
  4698. path->slots[*level - 1] = 0;
  4699. (*level)--;
  4700. return 0;
  4701. }
  4702. static int tree_move_next_or_upnext(struct btrfs_path *path,
  4703. int *level, int root_level)
  4704. {
  4705. int ret = 0;
  4706. int nritems;
  4707. nritems = btrfs_header_nritems(path->nodes[*level]);
  4708. path->slots[*level]++;
  4709. while (path->slots[*level] >= nritems) {
  4710. if (*level == root_level)
  4711. return -1;
  4712. /* move upnext */
  4713. path->slots[*level] = 0;
  4714. free_extent_buffer(path->nodes[*level]);
  4715. path->nodes[*level] = NULL;
  4716. (*level)++;
  4717. path->slots[*level]++;
  4718. nritems = btrfs_header_nritems(path->nodes[*level]);
  4719. ret = 1;
  4720. }
  4721. return ret;
  4722. }
  4723. /*
  4724. * Returns 1 if it had to move up and next. 0 is returned if it moved only next
  4725. * or down.
  4726. */
  4727. static int tree_advance(struct btrfs_fs_info *fs_info,
  4728. struct btrfs_path *path,
  4729. int *level, int root_level,
  4730. int allow_down,
  4731. struct btrfs_key *key)
  4732. {
  4733. int ret;
  4734. if (*level == 0 || !allow_down) {
  4735. ret = tree_move_next_or_upnext(path, level, root_level);
  4736. } else {
  4737. ret = tree_move_down(fs_info, path, level);
  4738. }
  4739. if (ret >= 0) {
  4740. if (*level == 0)
  4741. btrfs_item_key_to_cpu(path->nodes[*level], key,
  4742. path->slots[*level]);
  4743. else
  4744. btrfs_node_key_to_cpu(path->nodes[*level], key,
  4745. path->slots[*level]);
  4746. }
  4747. return ret;
  4748. }
  4749. static int tree_compare_item(struct btrfs_path *left_path,
  4750. struct btrfs_path *right_path,
  4751. char *tmp_buf)
  4752. {
  4753. int cmp;
  4754. int len1, len2;
  4755. unsigned long off1, off2;
  4756. len1 = btrfs_item_size_nr(left_path->nodes[0], left_path->slots[0]);
  4757. len2 = btrfs_item_size_nr(right_path->nodes[0], right_path->slots[0]);
  4758. if (len1 != len2)
  4759. return 1;
  4760. off1 = btrfs_item_ptr_offset(left_path->nodes[0], left_path->slots[0]);
  4761. off2 = btrfs_item_ptr_offset(right_path->nodes[0],
  4762. right_path->slots[0]);
  4763. read_extent_buffer(left_path->nodes[0], tmp_buf, off1, len1);
  4764. cmp = memcmp_extent_buffer(right_path->nodes[0], tmp_buf, off2, len1);
  4765. if (cmp)
  4766. return 1;
  4767. return 0;
  4768. }
  4769. #define ADVANCE 1
  4770. #define ADVANCE_ONLY_NEXT -1
  4771. /*
  4772. * This function compares two trees and calls the provided callback for
  4773. * every changed/new/deleted item it finds.
  4774. * If shared tree blocks are encountered, whole subtrees are skipped, making
  4775. * the compare pretty fast on snapshotted subvolumes.
  4776. *
  4777. * This currently works on commit roots only. As commit roots are read only,
  4778. * we don't do any locking. The commit roots are protected with transactions.
  4779. * Transactions are ended and rejoined when a commit is tried in between.
  4780. *
  4781. * This function checks for modifications done to the trees while comparing.
  4782. * If it detects a change, it aborts immediately.
  4783. */
  4784. int btrfs_compare_trees(struct btrfs_root *left_root,
  4785. struct btrfs_root *right_root,
  4786. btrfs_changed_cb_t changed_cb, void *ctx)
  4787. {
  4788. struct btrfs_fs_info *fs_info = left_root->fs_info;
  4789. int ret;
  4790. int cmp;
  4791. struct btrfs_path *left_path = NULL;
  4792. struct btrfs_path *right_path = NULL;
  4793. struct btrfs_key left_key;
  4794. struct btrfs_key right_key;
  4795. char *tmp_buf = NULL;
  4796. int left_root_level;
  4797. int right_root_level;
  4798. int left_level;
  4799. int right_level;
  4800. int left_end_reached;
  4801. int right_end_reached;
  4802. int advance_left;
  4803. int advance_right;
  4804. u64 left_blockptr;
  4805. u64 right_blockptr;
  4806. u64 left_gen;
  4807. u64 right_gen;
  4808. left_path = btrfs_alloc_path();
  4809. if (!left_path) {
  4810. ret = -ENOMEM;
  4811. goto out;
  4812. }
  4813. right_path = btrfs_alloc_path();
  4814. if (!right_path) {
  4815. ret = -ENOMEM;
  4816. goto out;
  4817. }
  4818. tmp_buf = kvmalloc(fs_info->nodesize, GFP_KERNEL);
  4819. if (!tmp_buf) {
  4820. ret = -ENOMEM;
  4821. goto out;
  4822. }
  4823. left_path->search_commit_root = 1;
  4824. left_path->skip_locking = 1;
  4825. right_path->search_commit_root = 1;
  4826. right_path->skip_locking = 1;
  4827. /*
  4828. * Strategy: Go to the first items of both trees. Then do
  4829. *
  4830. * If both trees are at level 0
  4831. * Compare keys of current items
  4832. * If left < right treat left item as new, advance left tree
  4833. * and repeat
  4834. * If left > right treat right item as deleted, advance right tree
  4835. * and repeat
  4836. * If left == right do deep compare of items, treat as changed if
  4837. * needed, advance both trees and repeat
  4838. * If both trees are at the same level but not at level 0
  4839. * Compare keys of current nodes/leafs
  4840. * If left < right advance left tree and repeat
  4841. * If left > right advance right tree and repeat
  4842. * If left == right compare blockptrs of the next nodes/leafs
  4843. * If they match advance both trees but stay at the same level
  4844. * and repeat
  4845. * If they don't match advance both trees while allowing to go
  4846. * deeper and repeat
  4847. * If tree levels are different
  4848. * Advance the tree that needs it and repeat
  4849. *
  4850. * Advancing a tree means:
  4851. * If we are at level 0, try to go to the next slot. If that's not
  4852. * possible, go one level up and repeat. Stop when we found a level
  4853. * where we could go to the next slot. We may at this point be on a
  4854. * node or a leaf.
  4855. *
  4856. * If we are not at level 0 and not on shared tree blocks, go one
  4857. * level deeper.
  4858. *
  4859. * If we are not at level 0 and on shared tree blocks, go one slot to
  4860. * the right if possible or go up and right.
  4861. */
  4862. down_read(&fs_info->commit_root_sem);
  4863. left_level = btrfs_header_level(left_root->commit_root);
  4864. left_root_level = left_level;
  4865. left_path->nodes[left_level] =
  4866. btrfs_clone_extent_buffer(left_root->commit_root);
  4867. if (!left_path->nodes[left_level]) {
  4868. up_read(&fs_info->commit_root_sem);
  4869. ret = -ENOMEM;
  4870. goto out;
  4871. }
  4872. extent_buffer_get(left_path->nodes[left_level]);
  4873. right_level = btrfs_header_level(right_root->commit_root);
  4874. right_root_level = right_level;
  4875. right_path->nodes[right_level] =
  4876. btrfs_clone_extent_buffer(right_root->commit_root);
  4877. if (!right_path->nodes[right_level]) {
  4878. up_read(&fs_info->commit_root_sem);
  4879. ret = -ENOMEM;
  4880. goto out;
  4881. }
  4882. extent_buffer_get(right_path->nodes[right_level]);
  4883. up_read(&fs_info->commit_root_sem);
  4884. if (left_level == 0)
  4885. btrfs_item_key_to_cpu(left_path->nodes[left_level],
  4886. &left_key, left_path->slots[left_level]);
  4887. else
  4888. btrfs_node_key_to_cpu(left_path->nodes[left_level],
  4889. &left_key, left_path->slots[left_level]);
  4890. if (right_level == 0)
  4891. btrfs_item_key_to_cpu(right_path->nodes[right_level],
  4892. &right_key, right_path->slots[right_level]);
  4893. else
  4894. btrfs_node_key_to_cpu(right_path->nodes[right_level],
  4895. &right_key, right_path->slots[right_level]);
  4896. left_end_reached = right_end_reached = 0;
  4897. advance_left = advance_right = 0;
  4898. while (1) {
  4899. cond_resched();
  4900. if (advance_left && !left_end_reached) {
  4901. ret = tree_advance(fs_info, left_path, &left_level,
  4902. left_root_level,
  4903. advance_left != ADVANCE_ONLY_NEXT,
  4904. &left_key);
  4905. if (ret == -1)
  4906. left_end_reached = ADVANCE;
  4907. else if (ret < 0)
  4908. goto out;
  4909. advance_left = 0;
  4910. }
  4911. if (advance_right && !right_end_reached) {
  4912. ret = tree_advance(fs_info, right_path, &right_level,
  4913. right_root_level,
  4914. advance_right != ADVANCE_ONLY_NEXT,
  4915. &right_key);
  4916. if (ret == -1)
  4917. right_end_reached = ADVANCE;
  4918. else if (ret < 0)
  4919. goto out;
  4920. advance_right = 0;
  4921. }
  4922. if (left_end_reached && right_end_reached) {
  4923. ret = 0;
  4924. goto out;
  4925. } else if (left_end_reached) {
  4926. if (right_level == 0) {
  4927. ret = changed_cb(left_path, right_path,
  4928. &right_key,
  4929. BTRFS_COMPARE_TREE_DELETED,
  4930. ctx);
  4931. if (ret < 0)
  4932. goto out;
  4933. }
  4934. advance_right = ADVANCE;
  4935. continue;
  4936. } else if (right_end_reached) {
  4937. if (left_level == 0) {
  4938. ret = changed_cb(left_path, right_path,
  4939. &left_key,
  4940. BTRFS_COMPARE_TREE_NEW,
  4941. ctx);
  4942. if (ret < 0)
  4943. goto out;
  4944. }
  4945. advance_left = ADVANCE;
  4946. continue;
  4947. }
  4948. if (left_level == 0 && right_level == 0) {
  4949. cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
  4950. if (cmp < 0) {
  4951. ret = changed_cb(left_path, right_path,
  4952. &left_key,
  4953. BTRFS_COMPARE_TREE_NEW,
  4954. ctx);
  4955. if (ret < 0)
  4956. goto out;
  4957. advance_left = ADVANCE;
  4958. } else if (cmp > 0) {
  4959. ret = changed_cb(left_path, right_path,
  4960. &right_key,
  4961. BTRFS_COMPARE_TREE_DELETED,
  4962. ctx);
  4963. if (ret < 0)
  4964. goto out;
  4965. advance_right = ADVANCE;
  4966. } else {
  4967. enum btrfs_compare_tree_result result;
  4968. WARN_ON(!extent_buffer_uptodate(left_path->nodes[0]));
  4969. ret = tree_compare_item(left_path, right_path,
  4970. tmp_buf);
  4971. if (ret)
  4972. result = BTRFS_COMPARE_TREE_CHANGED;
  4973. else
  4974. result = BTRFS_COMPARE_TREE_SAME;
  4975. ret = changed_cb(left_path, right_path,
  4976. &left_key, result, ctx);
  4977. if (ret < 0)
  4978. goto out;
  4979. advance_left = ADVANCE;
  4980. advance_right = ADVANCE;
  4981. }
  4982. } else if (left_level == right_level) {
  4983. cmp = btrfs_comp_cpu_keys(&left_key, &right_key);
  4984. if (cmp < 0) {
  4985. advance_left = ADVANCE;
  4986. } else if (cmp > 0) {
  4987. advance_right = ADVANCE;
  4988. } else {
  4989. left_blockptr = btrfs_node_blockptr(
  4990. left_path->nodes[left_level],
  4991. left_path->slots[left_level]);
  4992. right_blockptr = btrfs_node_blockptr(
  4993. right_path->nodes[right_level],
  4994. right_path->slots[right_level]);
  4995. left_gen = btrfs_node_ptr_generation(
  4996. left_path->nodes[left_level],
  4997. left_path->slots[left_level]);
  4998. right_gen = btrfs_node_ptr_generation(
  4999. right_path->nodes[right_level],
  5000. right_path->slots[right_level]);
  5001. if (left_blockptr == right_blockptr &&
  5002. left_gen == right_gen) {
  5003. /*
  5004. * As we're on a shared block, don't
  5005. * allow to go deeper.
  5006. */
  5007. advance_left = ADVANCE_ONLY_NEXT;
  5008. advance_right = ADVANCE_ONLY_NEXT;
  5009. } else {
  5010. advance_left = ADVANCE;
  5011. advance_right = ADVANCE;
  5012. }
  5013. }
  5014. } else if (left_level < right_level) {
  5015. advance_right = ADVANCE;
  5016. } else {
  5017. advance_left = ADVANCE;
  5018. }
  5019. }
  5020. out:
  5021. btrfs_free_path(left_path);
  5022. btrfs_free_path(right_path);
  5023. kvfree(tmp_buf);
  5024. return ret;
  5025. }
  5026. /*
  5027. * this is similar to btrfs_next_leaf, but does not try to preserve
  5028. * and fixup the path. It looks for and returns the next key in the
  5029. * tree based on the current path and the min_trans parameters.
  5030. *
  5031. * 0 is returned if another key is found, < 0 if there are any errors
  5032. * and 1 is returned if there are no higher keys in the tree
  5033. *
  5034. * path->keep_locks should be set to 1 on the search made before
  5035. * calling this function.
  5036. */
  5037. int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
  5038. struct btrfs_key *key, int level, u64 min_trans)
  5039. {
  5040. int slot;
  5041. struct extent_buffer *c;
  5042. WARN_ON(!path->keep_locks);
  5043. while (level < BTRFS_MAX_LEVEL) {
  5044. if (!path->nodes[level])
  5045. return 1;
  5046. slot = path->slots[level] + 1;
  5047. c = path->nodes[level];
  5048. next:
  5049. if (slot >= btrfs_header_nritems(c)) {
  5050. int ret;
  5051. int orig_lowest;
  5052. struct btrfs_key cur_key;
  5053. if (level + 1 >= BTRFS_MAX_LEVEL ||
  5054. !path->nodes[level + 1])
  5055. return 1;
  5056. if (path->locks[level + 1]) {
  5057. level++;
  5058. continue;
  5059. }
  5060. slot = btrfs_header_nritems(c) - 1;
  5061. if (level == 0)
  5062. btrfs_item_key_to_cpu(c, &cur_key, slot);
  5063. else
  5064. btrfs_node_key_to_cpu(c, &cur_key, slot);
  5065. orig_lowest = path->lowest_level;
  5066. btrfs_release_path(path);
  5067. path->lowest_level = level;
  5068. ret = btrfs_search_slot(NULL, root, &cur_key, path,
  5069. 0, 0);
  5070. path->lowest_level = orig_lowest;
  5071. if (ret < 0)
  5072. return ret;
  5073. c = path->nodes[level];
  5074. slot = path->slots[level];
  5075. if (ret == 0)
  5076. slot++;
  5077. goto next;
  5078. }
  5079. if (level == 0)
  5080. btrfs_item_key_to_cpu(c, key, slot);
  5081. else {
  5082. u64 gen = btrfs_node_ptr_generation(c, slot);
  5083. if (gen < min_trans) {
  5084. slot++;
  5085. goto next;
  5086. }
  5087. btrfs_node_key_to_cpu(c, key, slot);
  5088. }
  5089. return 0;
  5090. }
  5091. return 1;
  5092. }
  5093. /*
  5094. * search the tree again to find a leaf with greater keys
  5095. * returns 0 if it found something or 1 if there are no greater leaves.
  5096. * returns < 0 on io errors.
  5097. */
  5098. int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
  5099. {
  5100. return btrfs_next_old_leaf(root, path, 0);
  5101. }
  5102. int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
  5103. u64 time_seq)
  5104. {
  5105. int slot;
  5106. int level;
  5107. struct extent_buffer *c;
  5108. struct extent_buffer *next;
  5109. struct btrfs_key key;
  5110. u32 nritems;
  5111. int ret;
  5112. int old_spinning = path->leave_spinning;
  5113. int next_rw_lock = 0;
  5114. nritems = btrfs_header_nritems(path->nodes[0]);
  5115. if (nritems == 0)
  5116. return 1;
  5117. btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
  5118. again:
  5119. level = 1;
  5120. next = NULL;
  5121. next_rw_lock = 0;
  5122. btrfs_release_path(path);
  5123. path->keep_locks = 1;
  5124. path->leave_spinning = 1;
  5125. if (time_seq)
  5126. ret = btrfs_search_old_slot(root, &key, path, time_seq);
  5127. else
  5128. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  5129. path->keep_locks = 0;
  5130. if (ret < 0)
  5131. return ret;
  5132. nritems = btrfs_header_nritems(path->nodes[0]);
  5133. /*
  5134. * by releasing the path above we dropped all our locks. A balance
  5135. * could have added more items next to the key that used to be
  5136. * at the very end of the block. So, check again here and
  5137. * advance the path if there are now more items available.
  5138. */
  5139. if (nritems > 0 && path->slots[0] < nritems - 1) {
  5140. if (ret == 0)
  5141. path->slots[0]++;
  5142. ret = 0;
  5143. goto done;
  5144. }
  5145. /*
  5146. * So the above check misses one case:
  5147. * - after releasing the path above, someone has removed the item that
  5148. * used to be at the very end of the block, and balance between leafs
  5149. * gets another one with bigger key.offset to replace it.
  5150. *
  5151. * This one should be returned as well, or we can get leaf corruption
  5152. * later(esp. in __btrfs_drop_extents()).
  5153. *
  5154. * And a bit more explanation about this check,
  5155. * with ret > 0, the key isn't found, the path points to the slot
  5156. * where it should be inserted, so the path->slots[0] item must be the
  5157. * bigger one.
  5158. */
  5159. if (nritems > 0 && ret > 0 && path->slots[0] == nritems - 1) {
  5160. ret = 0;
  5161. goto done;
  5162. }
  5163. while (level < BTRFS_MAX_LEVEL) {
  5164. if (!path->nodes[level]) {
  5165. ret = 1;
  5166. goto done;
  5167. }
  5168. slot = path->slots[level] + 1;
  5169. c = path->nodes[level];
  5170. if (slot >= btrfs_header_nritems(c)) {
  5171. level++;
  5172. if (level == BTRFS_MAX_LEVEL) {
  5173. ret = 1;
  5174. goto done;
  5175. }
  5176. continue;
  5177. }
  5178. if (next) {
  5179. btrfs_tree_unlock_rw(next, next_rw_lock);
  5180. free_extent_buffer(next);
  5181. }
  5182. next = c;
  5183. next_rw_lock = path->locks[level];
  5184. ret = read_block_for_search(root, path, &next, level,
  5185. slot, &key);
  5186. if (ret == -EAGAIN)
  5187. goto again;
  5188. if (ret < 0) {
  5189. btrfs_release_path(path);
  5190. goto done;
  5191. }
  5192. if (!path->skip_locking) {
  5193. ret = btrfs_try_tree_read_lock(next);
  5194. if (!ret && time_seq) {
  5195. /*
  5196. * If we don't get the lock, we may be racing
  5197. * with push_leaf_left, holding that lock while
  5198. * itself waiting for the leaf we've currently
  5199. * locked. To solve this situation, we give up
  5200. * on our lock and cycle.
  5201. */
  5202. free_extent_buffer(next);
  5203. btrfs_release_path(path);
  5204. cond_resched();
  5205. goto again;
  5206. }
  5207. if (!ret) {
  5208. btrfs_set_path_blocking(path);
  5209. btrfs_tree_read_lock(next);
  5210. btrfs_clear_path_blocking(path, next,
  5211. BTRFS_READ_LOCK);
  5212. }
  5213. next_rw_lock = BTRFS_READ_LOCK;
  5214. }
  5215. break;
  5216. }
  5217. path->slots[level] = slot;
  5218. while (1) {
  5219. level--;
  5220. c = path->nodes[level];
  5221. if (path->locks[level])
  5222. btrfs_tree_unlock_rw(c, path->locks[level]);
  5223. free_extent_buffer(c);
  5224. path->nodes[level] = next;
  5225. path->slots[level] = 0;
  5226. if (!path->skip_locking)
  5227. path->locks[level] = next_rw_lock;
  5228. if (!level)
  5229. break;
  5230. ret = read_block_for_search(root, path, &next, level,
  5231. 0, &key);
  5232. if (ret == -EAGAIN)
  5233. goto again;
  5234. if (ret < 0) {
  5235. btrfs_release_path(path);
  5236. goto done;
  5237. }
  5238. if (!path->skip_locking) {
  5239. ret = btrfs_try_tree_read_lock(next);
  5240. if (!ret) {
  5241. btrfs_set_path_blocking(path);
  5242. btrfs_tree_read_lock(next);
  5243. btrfs_clear_path_blocking(path, next,
  5244. BTRFS_READ_LOCK);
  5245. }
  5246. next_rw_lock = BTRFS_READ_LOCK;
  5247. }
  5248. }
  5249. ret = 0;
  5250. done:
  5251. unlock_up(path, 0, 1, 0, NULL);
  5252. path->leave_spinning = old_spinning;
  5253. if (!old_spinning)
  5254. btrfs_set_path_blocking(path);
  5255. return ret;
  5256. }
  5257. /*
  5258. * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
  5259. * searching until it gets past min_objectid or finds an item of 'type'
  5260. *
  5261. * returns 0 if something is found, 1 if nothing was found and < 0 on error
  5262. */
  5263. int btrfs_previous_item(struct btrfs_root *root,
  5264. struct btrfs_path *path, u64 min_objectid,
  5265. int type)
  5266. {
  5267. struct btrfs_key found_key;
  5268. struct extent_buffer *leaf;
  5269. u32 nritems;
  5270. int ret;
  5271. while (1) {
  5272. if (path->slots[0] == 0) {
  5273. btrfs_set_path_blocking(path);
  5274. ret = btrfs_prev_leaf(root, path);
  5275. if (ret != 0)
  5276. return ret;
  5277. } else {
  5278. path->slots[0]--;
  5279. }
  5280. leaf = path->nodes[0];
  5281. nritems = btrfs_header_nritems(leaf);
  5282. if (nritems == 0)
  5283. return 1;
  5284. if (path->slots[0] == nritems)
  5285. path->slots[0]--;
  5286. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  5287. if (found_key.objectid < min_objectid)
  5288. break;
  5289. if (found_key.type == type)
  5290. return 0;
  5291. if (found_key.objectid == min_objectid &&
  5292. found_key.type < type)
  5293. break;
  5294. }
  5295. return 1;
  5296. }
  5297. /*
  5298. * search in extent tree to find a previous Metadata/Data extent item with
  5299. * min objecitd.
  5300. *
  5301. * returns 0 if something is found, 1 if nothing was found and < 0 on error
  5302. */
  5303. int btrfs_previous_extent_item(struct btrfs_root *root,
  5304. struct btrfs_path *path, u64 min_objectid)
  5305. {
  5306. struct btrfs_key found_key;
  5307. struct extent_buffer *leaf;
  5308. u32 nritems;
  5309. int ret;
  5310. while (1) {
  5311. if (path->slots[0] == 0) {
  5312. btrfs_set_path_blocking(path);
  5313. ret = btrfs_prev_leaf(root, path);
  5314. if (ret != 0)
  5315. return ret;
  5316. } else {
  5317. path->slots[0]--;
  5318. }
  5319. leaf = path->nodes[0];
  5320. nritems = btrfs_header_nritems(leaf);
  5321. if (nritems == 0)
  5322. return 1;
  5323. if (path->slots[0] == nritems)
  5324. path->slots[0]--;
  5325. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  5326. if (found_key.objectid < min_objectid)
  5327. break;
  5328. if (found_key.type == BTRFS_EXTENT_ITEM_KEY ||
  5329. found_key.type == BTRFS_METADATA_ITEM_KEY)
  5330. return 0;
  5331. if (found_key.objectid == min_objectid &&
  5332. found_key.type < BTRFS_EXTENT_ITEM_KEY)
  5333. break;
  5334. }
  5335. return 1;
  5336. }