ctree.c 133 KB

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