ctree.c 148 KB

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