ctree.c 147 KB

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