ctree.c 147 KB

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