ctree.c 150 KB

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