ctree.c 147 KB

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