ctree.c 147 KB

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