ctree.c 133 KB

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