ctree.c 146 KB

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