ctree.c 147 KB

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