ctree.c 152 KB

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