ctree.c 149 KB

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