ctree.c 148 KB

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