ctree.c 152 KB

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