ctree.c 133 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178
  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. left = read_node_slot(root, parent, pslot - 1);
  1464. if (left) {
  1465. btrfs_tree_lock(left);
  1466. btrfs_set_lock_blocking(left);
  1467. wret = btrfs_cow_block(trans, root, left,
  1468. parent, pslot - 1, &left);
  1469. if (wret) {
  1470. ret = wret;
  1471. goto enospc;
  1472. }
  1473. }
  1474. right = read_node_slot(root, parent, pslot + 1);
  1475. if (right) {
  1476. btrfs_tree_lock(right);
  1477. btrfs_set_lock_blocking(right);
  1478. wret = btrfs_cow_block(trans, root, right,
  1479. parent, pslot + 1, &right);
  1480. if (wret) {
  1481. ret = wret;
  1482. goto enospc;
  1483. }
  1484. }
  1485. /* first, try to make some room in the middle buffer */
  1486. if (left) {
  1487. orig_slot += btrfs_header_nritems(left);
  1488. wret = push_node_left(trans, root, left, mid, 1);
  1489. if (wret < 0)
  1490. ret = wret;
  1491. }
  1492. /*
  1493. * then try to empty the right most buffer into the middle
  1494. */
  1495. if (right) {
  1496. wret = push_node_left(trans, root, mid, right, 1);
  1497. if (wret < 0 && wret != -ENOSPC)
  1498. ret = wret;
  1499. if (btrfs_header_nritems(right) == 0) {
  1500. clean_tree_block(trans, root, right);
  1501. btrfs_tree_unlock(right);
  1502. del_ptr(trans, root, path, level + 1, pslot + 1, 1);
  1503. root_sub_used(root, right->len);
  1504. btrfs_free_tree_block(trans, root, right, 0, 1);
  1505. free_extent_buffer_stale(right);
  1506. right = NULL;
  1507. } else {
  1508. struct btrfs_disk_key right_key;
  1509. btrfs_node_key(right, &right_key, 0);
  1510. tree_mod_log_set_node_key(root->fs_info, parent,
  1511. &right_key, pslot + 1, 0);
  1512. btrfs_set_node_key(parent, &right_key, pslot + 1);
  1513. btrfs_mark_buffer_dirty(parent);
  1514. }
  1515. }
  1516. if (btrfs_header_nritems(mid) == 1) {
  1517. /*
  1518. * we're not allowed to leave a node with one item in the
  1519. * tree during a delete. A deletion from lower in the tree
  1520. * could try to delete the only pointer in this node.
  1521. * So, pull some keys from the left.
  1522. * There has to be a left pointer at this point because
  1523. * otherwise we would have pulled some pointers from the
  1524. * right
  1525. */
  1526. if (!left) {
  1527. ret = -EROFS;
  1528. btrfs_std_error(root->fs_info, ret);
  1529. goto enospc;
  1530. }
  1531. wret = balance_node_right(trans, root, mid, left);
  1532. if (wret < 0) {
  1533. ret = wret;
  1534. goto enospc;
  1535. }
  1536. if (wret == 1) {
  1537. wret = push_node_left(trans, root, left, mid, 1);
  1538. if (wret < 0)
  1539. ret = wret;
  1540. }
  1541. BUG_ON(wret == 1);
  1542. }
  1543. if (btrfs_header_nritems(mid) == 0) {
  1544. clean_tree_block(trans, root, mid);
  1545. btrfs_tree_unlock(mid);
  1546. del_ptr(trans, root, path, level + 1, pslot, 1);
  1547. root_sub_used(root, mid->len);
  1548. btrfs_free_tree_block(trans, root, mid, 0, 1);
  1549. free_extent_buffer_stale(mid);
  1550. mid = NULL;
  1551. } else {
  1552. /* update the parent key to reflect our changes */
  1553. struct btrfs_disk_key mid_key;
  1554. btrfs_node_key(mid, &mid_key, 0);
  1555. tree_mod_log_set_node_key(root->fs_info, parent, &mid_key,
  1556. pslot, 0);
  1557. btrfs_set_node_key(parent, &mid_key, pslot);
  1558. btrfs_mark_buffer_dirty(parent);
  1559. }
  1560. /* update the path */
  1561. if (left) {
  1562. if (btrfs_header_nritems(left) > orig_slot) {
  1563. extent_buffer_get(left);
  1564. /* left was locked after cow */
  1565. path->nodes[level] = left;
  1566. path->slots[level + 1] -= 1;
  1567. path->slots[level] = orig_slot;
  1568. if (mid) {
  1569. btrfs_tree_unlock(mid);
  1570. free_extent_buffer(mid);
  1571. }
  1572. } else {
  1573. orig_slot -= btrfs_header_nritems(left);
  1574. path->slots[level] = orig_slot;
  1575. }
  1576. }
  1577. /* double check we haven't messed things up */
  1578. if (orig_ptr !=
  1579. btrfs_node_blockptr(path->nodes[level], path->slots[level]))
  1580. BUG();
  1581. enospc:
  1582. if (right) {
  1583. btrfs_tree_unlock(right);
  1584. free_extent_buffer(right);
  1585. }
  1586. if (left) {
  1587. if (path->nodes[level] != left)
  1588. btrfs_tree_unlock(left);
  1589. free_extent_buffer(left);
  1590. }
  1591. return ret;
  1592. }
  1593. /* Node balancing for insertion. Here we only split or push nodes around
  1594. * when they are completely full. This is also done top down, so we
  1595. * have to be pessimistic.
  1596. */
  1597. static noinline int push_nodes_for_insert(struct btrfs_trans_handle *trans,
  1598. struct btrfs_root *root,
  1599. struct btrfs_path *path, int level)
  1600. {
  1601. struct extent_buffer *right = NULL;
  1602. struct extent_buffer *mid;
  1603. struct extent_buffer *left = NULL;
  1604. struct extent_buffer *parent = NULL;
  1605. int ret = 0;
  1606. int wret;
  1607. int pslot;
  1608. int orig_slot = path->slots[level];
  1609. if (level == 0)
  1610. return 1;
  1611. mid = path->nodes[level];
  1612. WARN_ON(btrfs_header_generation(mid) != trans->transid);
  1613. if (level < BTRFS_MAX_LEVEL - 1) {
  1614. parent = path->nodes[level + 1];
  1615. pslot = path->slots[level + 1];
  1616. }
  1617. if (!parent)
  1618. return 1;
  1619. left = read_node_slot(root, parent, pslot - 1);
  1620. /* first, try to make some room in the middle buffer */
  1621. if (left) {
  1622. u32 left_nr;
  1623. btrfs_tree_lock(left);
  1624. btrfs_set_lock_blocking(left);
  1625. left_nr = btrfs_header_nritems(left);
  1626. if (left_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
  1627. wret = 1;
  1628. } else {
  1629. ret = btrfs_cow_block(trans, root, left, parent,
  1630. pslot - 1, &left);
  1631. if (ret)
  1632. wret = 1;
  1633. else {
  1634. wret = push_node_left(trans, root,
  1635. left, mid, 0);
  1636. }
  1637. }
  1638. if (wret < 0)
  1639. ret = wret;
  1640. if (wret == 0) {
  1641. struct btrfs_disk_key disk_key;
  1642. orig_slot += left_nr;
  1643. btrfs_node_key(mid, &disk_key, 0);
  1644. tree_mod_log_set_node_key(root->fs_info, parent,
  1645. &disk_key, pslot, 0);
  1646. btrfs_set_node_key(parent, &disk_key, pslot);
  1647. btrfs_mark_buffer_dirty(parent);
  1648. if (btrfs_header_nritems(left) > orig_slot) {
  1649. path->nodes[level] = left;
  1650. path->slots[level + 1] -= 1;
  1651. path->slots[level] = orig_slot;
  1652. btrfs_tree_unlock(mid);
  1653. free_extent_buffer(mid);
  1654. } else {
  1655. orig_slot -=
  1656. btrfs_header_nritems(left);
  1657. path->slots[level] = orig_slot;
  1658. btrfs_tree_unlock(left);
  1659. free_extent_buffer(left);
  1660. }
  1661. return 0;
  1662. }
  1663. btrfs_tree_unlock(left);
  1664. free_extent_buffer(left);
  1665. }
  1666. right = read_node_slot(root, parent, pslot + 1);
  1667. /*
  1668. * then try to empty the right most buffer into the middle
  1669. */
  1670. if (right) {
  1671. u32 right_nr;
  1672. btrfs_tree_lock(right);
  1673. btrfs_set_lock_blocking(right);
  1674. right_nr = btrfs_header_nritems(right);
  1675. if (right_nr >= BTRFS_NODEPTRS_PER_BLOCK(root) - 1) {
  1676. wret = 1;
  1677. } else {
  1678. ret = btrfs_cow_block(trans, root, right,
  1679. parent, pslot + 1,
  1680. &right);
  1681. if (ret)
  1682. wret = 1;
  1683. else {
  1684. wret = balance_node_right(trans, root,
  1685. right, mid);
  1686. }
  1687. }
  1688. if (wret < 0)
  1689. ret = wret;
  1690. if (wret == 0) {
  1691. struct btrfs_disk_key disk_key;
  1692. btrfs_node_key(right, &disk_key, 0);
  1693. tree_mod_log_set_node_key(root->fs_info, parent,
  1694. &disk_key, pslot + 1, 0);
  1695. btrfs_set_node_key(parent, &disk_key, pslot + 1);
  1696. btrfs_mark_buffer_dirty(parent);
  1697. if (btrfs_header_nritems(mid) <= orig_slot) {
  1698. path->nodes[level] = right;
  1699. path->slots[level + 1] += 1;
  1700. path->slots[level] = orig_slot -
  1701. btrfs_header_nritems(mid);
  1702. btrfs_tree_unlock(mid);
  1703. free_extent_buffer(mid);
  1704. } else {
  1705. btrfs_tree_unlock(right);
  1706. free_extent_buffer(right);
  1707. }
  1708. return 0;
  1709. }
  1710. btrfs_tree_unlock(right);
  1711. free_extent_buffer(right);
  1712. }
  1713. return 1;
  1714. }
  1715. /*
  1716. * readahead one full node of leaves, finding things that are close
  1717. * to the block in 'slot', and triggering ra on them.
  1718. */
  1719. static void reada_for_search(struct btrfs_root *root,
  1720. struct btrfs_path *path,
  1721. int level, int slot, u64 objectid)
  1722. {
  1723. struct extent_buffer *node;
  1724. struct btrfs_disk_key disk_key;
  1725. u32 nritems;
  1726. u64 search;
  1727. u64 target;
  1728. u64 nread = 0;
  1729. u64 gen;
  1730. int direction = path->reada;
  1731. struct extent_buffer *eb;
  1732. u32 nr;
  1733. u32 blocksize;
  1734. u32 nscan = 0;
  1735. if (level != 1)
  1736. return;
  1737. if (!path->nodes[level])
  1738. return;
  1739. node = path->nodes[level];
  1740. search = btrfs_node_blockptr(node, slot);
  1741. blocksize = btrfs_level_size(root, level - 1);
  1742. eb = btrfs_find_tree_block(root, search, blocksize);
  1743. if (eb) {
  1744. free_extent_buffer(eb);
  1745. return;
  1746. }
  1747. target = search;
  1748. nritems = btrfs_header_nritems(node);
  1749. nr = slot;
  1750. while (1) {
  1751. if (direction < 0) {
  1752. if (nr == 0)
  1753. break;
  1754. nr--;
  1755. } else if (direction > 0) {
  1756. nr++;
  1757. if (nr >= nritems)
  1758. break;
  1759. }
  1760. if (path->reada < 0 && objectid) {
  1761. btrfs_node_key(node, &disk_key, nr);
  1762. if (btrfs_disk_key_objectid(&disk_key) != objectid)
  1763. break;
  1764. }
  1765. search = btrfs_node_blockptr(node, nr);
  1766. if ((search <= target && target - search <= 65536) ||
  1767. (search > target && search - target <= 65536)) {
  1768. gen = btrfs_node_ptr_generation(node, nr);
  1769. readahead_tree_block(root, search, blocksize, gen);
  1770. nread += blocksize;
  1771. }
  1772. nscan++;
  1773. if ((nread > 65536 || nscan > 32))
  1774. break;
  1775. }
  1776. }
  1777. /*
  1778. * returns -EAGAIN if it had to drop the path, or zero if everything was in
  1779. * cache
  1780. */
  1781. static noinline int reada_for_balance(struct btrfs_root *root,
  1782. struct btrfs_path *path, int level)
  1783. {
  1784. int slot;
  1785. int nritems;
  1786. struct extent_buffer *parent;
  1787. struct extent_buffer *eb;
  1788. u64 gen;
  1789. u64 block1 = 0;
  1790. u64 block2 = 0;
  1791. int ret = 0;
  1792. int blocksize;
  1793. parent = path->nodes[level + 1];
  1794. if (!parent)
  1795. return 0;
  1796. nritems = btrfs_header_nritems(parent);
  1797. slot = path->slots[level + 1];
  1798. blocksize = btrfs_level_size(root, level);
  1799. if (slot > 0) {
  1800. block1 = btrfs_node_blockptr(parent, slot - 1);
  1801. gen = btrfs_node_ptr_generation(parent, slot - 1);
  1802. eb = btrfs_find_tree_block(root, block1, blocksize);
  1803. /*
  1804. * if we get -eagain from btrfs_buffer_uptodate, we
  1805. * don't want to return eagain here. That will loop
  1806. * forever
  1807. */
  1808. if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
  1809. block1 = 0;
  1810. free_extent_buffer(eb);
  1811. }
  1812. if (slot + 1 < nritems) {
  1813. block2 = btrfs_node_blockptr(parent, slot + 1);
  1814. gen = btrfs_node_ptr_generation(parent, slot + 1);
  1815. eb = btrfs_find_tree_block(root, block2, blocksize);
  1816. if (eb && btrfs_buffer_uptodate(eb, gen, 1) != 0)
  1817. block2 = 0;
  1818. free_extent_buffer(eb);
  1819. }
  1820. if (block1 || block2) {
  1821. ret = -EAGAIN;
  1822. /* release the whole path */
  1823. btrfs_release_path(path);
  1824. /* read the blocks */
  1825. if (block1)
  1826. readahead_tree_block(root, block1, blocksize, 0);
  1827. if (block2)
  1828. readahead_tree_block(root, block2, blocksize, 0);
  1829. if (block1) {
  1830. eb = read_tree_block(root, block1, blocksize, 0);
  1831. free_extent_buffer(eb);
  1832. }
  1833. if (block2) {
  1834. eb = read_tree_block(root, block2, blocksize, 0);
  1835. free_extent_buffer(eb);
  1836. }
  1837. }
  1838. return ret;
  1839. }
  1840. /*
  1841. * when we walk down the tree, it is usually safe to unlock the higher layers
  1842. * in the tree. The exceptions are when our path goes through slot 0, because
  1843. * operations on the tree might require changing key pointers higher up in the
  1844. * tree.
  1845. *
  1846. * callers might also have set path->keep_locks, which tells this code to keep
  1847. * the lock if the path points to the last slot in the block. This is part of
  1848. * walking through the tree, and selecting the next slot in the higher block.
  1849. *
  1850. * lowest_unlock sets the lowest level in the tree we're allowed to unlock. so
  1851. * if lowest_unlock is 1, level 0 won't be unlocked
  1852. */
  1853. static noinline void unlock_up(struct btrfs_path *path, int level,
  1854. int lowest_unlock, int min_write_lock_level,
  1855. int *write_lock_level)
  1856. {
  1857. int i;
  1858. int skip_level = level;
  1859. int no_skips = 0;
  1860. struct extent_buffer *t;
  1861. for (i = level; i < BTRFS_MAX_LEVEL; i++) {
  1862. if (!path->nodes[i])
  1863. break;
  1864. if (!path->locks[i])
  1865. break;
  1866. if (!no_skips && path->slots[i] == 0) {
  1867. skip_level = i + 1;
  1868. continue;
  1869. }
  1870. if (!no_skips && path->keep_locks) {
  1871. u32 nritems;
  1872. t = path->nodes[i];
  1873. nritems = btrfs_header_nritems(t);
  1874. if (nritems < 1 || path->slots[i] >= nritems - 1) {
  1875. skip_level = i + 1;
  1876. continue;
  1877. }
  1878. }
  1879. if (skip_level < i && i >= lowest_unlock)
  1880. no_skips = 1;
  1881. t = path->nodes[i];
  1882. if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
  1883. btrfs_tree_unlock_rw(t, path->locks[i]);
  1884. path->locks[i] = 0;
  1885. if (write_lock_level &&
  1886. i > min_write_lock_level &&
  1887. i <= *write_lock_level) {
  1888. *write_lock_level = i - 1;
  1889. }
  1890. }
  1891. }
  1892. }
  1893. /*
  1894. * This releases any locks held in the path starting at level and
  1895. * going all the way up to the root.
  1896. *
  1897. * btrfs_search_slot will keep the lock held on higher nodes in a few
  1898. * corner cases, such as COW of the block at slot zero in the node. This
  1899. * ignores those rules, and it should only be called when there are no
  1900. * more updates to be done higher up in the tree.
  1901. */
  1902. noinline void btrfs_unlock_up_safe(struct btrfs_path *path, int level)
  1903. {
  1904. int i;
  1905. if (path->keep_locks)
  1906. return;
  1907. for (i = level; i < BTRFS_MAX_LEVEL; i++) {
  1908. if (!path->nodes[i])
  1909. continue;
  1910. if (!path->locks[i])
  1911. continue;
  1912. btrfs_tree_unlock_rw(path->nodes[i], path->locks[i]);
  1913. path->locks[i] = 0;
  1914. }
  1915. }
  1916. /*
  1917. * helper function for btrfs_search_slot. The goal is to find a block
  1918. * in cache without setting the path to blocking. If we find the block
  1919. * we return zero and the path is unchanged.
  1920. *
  1921. * If we can't find the block, we set the path blocking and do some
  1922. * reada. -EAGAIN is returned and the search must be repeated.
  1923. */
  1924. static int
  1925. read_block_for_search(struct btrfs_trans_handle *trans,
  1926. struct btrfs_root *root, struct btrfs_path *p,
  1927. struct extent_buffer **eb_ret, int level, int slot,
  1928. struct btrfs_key *key, u64 time_seq)
  1929. {
  1930. u64 blocknr;
  1931. u64 gen;
  1932. u32 blocksize;
  1933. struct extent_buffer *b = *eb_ret;
  1934. struct extent_buffer *tmp;
  1935. int ret;
  1936. blocknr = btrfs_node_blockptr(b, slot);
  1937. gen = btrfs_node_ptr_generation(b, slot);
  1938. blocksize = btrfs_level_size(root, level - 1);
  1939. tmp = btrfs_find_tree_block(root, blocknr, blocksize);
  1940. if (tmp) {
  1941. /* first we do an atomic uptodate check */
  1942. if (btrfs_buffer_uptodate(tmp, 0, 1) > 0) {
  1943. if (btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
  1944. /*
  1945. * we found an up to date block without
  1946. * sleeping, return
  1947. * right away
  1948. */
  1949. *eb_ret = tmp;
  1950. return 0;
  1951. }
  1952. /* the pages were up to date, but we failed
  1953. * the generation number check. Do a full
  1954. * read for the generation number that is correct.
  1955. * We must do this without dropping locks so
  1956. * we can trust our generation number
  1957. */
  1958. free_extent_buffer(tmp);
  1959. btrfs_set_path_blocking(p);
  1960. /* now we're allowed to do a blocking uptodate check */
  1961. tmp = read_tree_block(root, blocknr, blocksize, gen);
  1962. if (tmp && btrfs_buffer_uptodate(tmp, gen, 0) > 0) {
  1963. *eb_ret = tmp;
  1964. return 0;
  1965. }
  1966. free_extent_buffer(tmp);
  1967. btrfs_release_path(p);
  1968. return -EIO;
  1969. }
  1970. }
  1971. /*
  1972. * reduce lock contention at high levels
  1973. * of the btree by dropping locks before
  1974. * we read. Don't release the lock on the current
  1975. * level because we need to walk this node to figure
  1976. * out which blocks to read.
  1977. */
  1978. btrfs_unlock_up_safe(p, level + 1);
  1979. btrfs_set_path_blocking(p);
  1980. free_extent_buffer(tmp);
  1981. if (p->reada)
  1982. reada_for_search(root, p, level, slot, key->objectid);
  1983. btrfs_release_path(p);
  1984. ret = -EAGAIN;
  1985. tmp = read_tree_block(root, blocknr, blocksize, 0);
  1986. if (tmp) {
  1987. /*
  1988. * If the read above didn't mark this buffer up to date,
  1989. * it will never end up being up to date. Set ret to EIO now
  1990. * and give up so that our caller doesn't loop forever
  1991. * on our EAGAINs.
  1992. */
  1993. if (!btrfs_buffer_uptodate(tmp, 0, 0))
  1994. ret = -EIO;
  1995. free_extent_buffer(tmp);
  1996. }
  1997. return ret;
  1998. }
  1999. /*
  2000. * helper function for btrfs_search_slot. This does all of the checks
  2001. * for node-level blocks and does any balancing required based on
  2002. * the ins_len.
  2003. *
  2004. * If no extra work was required, zero is returned. If we had to
  2005. * drop the path, -EAGAIN is returned and btrfs_search_slot must
  2006. * start over
  2007. */
  2008. static int
  2009. setup_nodes_for_search(struct btrfs_trans_handle *trans,
  2010. struct btrfs_root *root, struct btrfs_path *p,
  2011. struct extent_buffer *b, int level, int ins_len,
  2012. int *write_lock_level)
  2013. {
  2014. int ret;
  2015. if ((p->search_for_split || ins_len > 0) && btrfs_header_nritems(b) >=
  2016. BTRFS_NODEPTRS_PER_BLOCK(root) - 3) {
  2017. int sret;
  2018. if (*write_lock_level < level + 1) {
  2019. *write_lock_level = level + 1;
  2020. btrfs_release_path(p);
  2021. goto again;
  2022. }
  2023. sret = reada_for_balance(root, p, level);
  2024. if (sret)
  2025. goto again;
  2026. btrfs_set_path_blocking(p);
  2027. sret = split_node(trans, root, p, level);
  2028. btrfs_clear_path_blocking(p, NULL, 0);
  2029. BUG_ON(sret > 0);
  2030. if (sret) {
  2031. ret = sret;
  2032. goto done;
  2033. }
  2034. b = p->nodes[level];
  2035. } else if (ins_len < 0 && btrfs_header_nritems(b) <
  2036. BTRFS_NODEPTRS_PER_BLOCK(root) / 2) {
  2037. int sret;
  2038. if (*write_lock_level < level + 1) {
  2039. *write_lock_level = level + 1;
  2040. btrfs_release_path(p);
  2041. goto again;
  2042. }
  2043. sret = reada_for_balance(root, p, level);
  2044. if (sret)
  2045. goto again;
  2046. btrfs_set_path_blocking(p);
  2047. sret = balance_level(trans, root, p, level);
  2048. btrfs_clear_path_blocking(p, NULL, 0);
  2049. if (sret) {
  2050. ret = sret;
  2051. goto done;
  2052. }
  2053. b = p->nodes[level];
  2054. if (!b) {
  2055. btrfs_release_path(p);
  2056. goto again;
  2057. }
  2058. BUG_ON(btrfs_header_nritems(b) == 1);
  2059. }
  2060. return 0;
  2061. again:
  2062. ret = -EAGAIN;
  2063. done:
  2064. return ret;
  2065. }
  2066. /*
  2067. * look for key in the tree. path is filled in with nodes along the way
  2068. * if key is found, we return zero and you can find the item in the leaf
  2069. * level of the path (level 0)
  2070. *
  2071. * If the key isn't found, the path points to the slot where it should
  2072. * be inserted, and 1 is returned. If there are other errors during the
  2073. * search a negative error number is returned.
  2074. *
  2075. * if ins_len > 0, nodes and leaves will be split as we walk down the
  2076. * tree. if ins_len < 0, nodes will be merged as we walk down the tree (if
  2077. * possible)
  2078. */
  2079. int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
  2080. *root, struct btrfs_key *key, struct btrfs_path *p, int
  2081. ins_len, int cow)
  2082. {
  2083. struct extent_buffer *b;
  2084. int slot;
  2085. int ret;
  2086. int err;
  2087. int level;
  2088. int lowest_unlock = 1;
  2089. int root_lock;
  2090. /* everything at write_lock_level or lower must be write locked */
  2091. int write_lock_level = 0;
  2092. u8 lowest_level = 0;
  2093. int min_write_lock_level;
  2094. lowest_level = p->lowest_level;
  2095. WARN_ON(lowest_level && ins_len > 0);
  2096. WARN_ON(p->nodes[0] != NULL);
  2097. if (ins_len < 0) {
  2098. lowest_unlock = 2;
  2099. /* when we are removing items, we might have to go up to level
  2100. * two as we update tree pointers Make sure we keep write
  2101. * for those levels as well
  2102. */
  2103. write_lock_level = 2;
  2104. } else if (ins_len > 0) {
  2105. /*
  2106. * for inserting items, make sure we have a write lock on
  2107. * level 1 so we can update keys
  2108. */
  2109. write_lock_level = 1;
  2110. }
  2111. if (!cow)
  2112. write_lock_level = -1;
  2113. if (cow && (p->keep_locks || p->lowest_level))
  2114. write_lock_level = BTRFS_MAX_LEVEL;
  2115. min_write_lock_level = write_lock_level;
  2116. again:
  2117. /*
  2118. * we try very hard to do read locks on the root
  2119. */
  2120. root_lock = BTRFS_READ_LOCK;
  2121. level = 0;
  2122. if (p->search_commit_root) {
  2123. /*
  2124. * the commit roots are read only
  2125. * so we always do read locks
  2126. */
  2127. b = root->commit_root;
  2128. extent_buffer_get(b);
  2129. level = btrfs_header_level(b);
  2130. if (!p->skip_locking)
  2131. btrfs_tree_read_lock(b);
  2132. } else {
  2133. if (p->skip_locking) {
  2134. b = btrfs_root_node(root);
  2135. level = btrfs_header_level(b);
  2136. } else {
  2137. /* we don't know the level of the root node
  2138. * until we actually have it read locked
  2139. */
  2140. b = btrfs_read_lock_root_node(root);
  2141. level = btrfs_header_level(b);
  2142. if (level <= write_lock_level) {
  2143. /* whoops, must trade for write lock */
  2144. btrfs_tree_read_unlock(b);
  2145. free_extent_buffer(b);
  2146. b = btrfs_lock_root_node(root);
  2147. root_lock = BTRFS_WRITE_LOCK;
  2148. /* the level might have changed, check again */
  2149. level = btrfs_header_level(b);
  2150. }
  2151. }
  2152. }
  2153. p->nodes[level] = b;
  2154. if (!p->skip_locking)
  2155. p->locks[level] = root_lock;
  2156. while (b) {
  2157. level = btrfs_header_level(b);
  2158. /*
  2159. * setup the path here so we can release it under lock
  2160. * contention with the cow code
  2161. */
  2162. if (cow) {
  2163. /*
  2164. * if we don't really need to cow this block
  2165. * then we don't want to set the path blocking,
  2166. * so we test it here
  2167. */
  2168. if (!should_cow_block(trans, root, b))
  2169. goto cow_done;
  2170. btrfs_set_path_blocking(p);
  2171. /*
  2172. * must have write locks on this node and the
  2173. * parent
  2174. */
  2175. if (level + 1 > write_lock_level) {
  2176. write_lock_level = level + 1;
  2177. btrfs_release_path(p);
  2178. goto again;
  2179. }
  2180. err = btrfs_cow_block(trans, root, b,
  2181. p->nodes[level + 1],
  2182. p->slots[level + 1], &b);
  2183. if (err) {
  2184. ret = err;
  2185. goto done;
  2186. }
  2187. }
  2188. cow_done:
  2189. BUG_ON(!cow && ins_len);
  2190. p->nodes[level] = b;
  2191. btrfs_clear_path_blocking(p, NULL, 0);
  2192. /*
  2193. * we have a lock on b and as long as we aren't changing
  2194. * the tree, there is no way to for the items in b to change.
  2195. * It is safe to drop the lock on our parent before we
  2196. * go through the expensive btree search on b.
  2197. *
  2198. * If cow is true, then we might be changing slot zero,
  2199. * which may require changing the parent. So, we can't
  2200. * drop the lock until after we know which slot we're
  2201. * operating on.
  2202. */
  2203. if (!cow)
  2204. btrfs_unlock_up_safe(p, level + 1);
  2205. ret = bin_search(b, key, level, &slot);
  2206. if (level != 0) {
  2207. int dec = 0;
  2208. if (ret && slot > 0) {
  2209. dec = 1;
  2210. slot -= 1;
  2211. }
  2212. p->slots[level] = slot;
  2213. err = setup_nodes_for_search(trans, root, p, b, level,
  2214. ins_len, &write_lock_level);
  2215. if (err == -EAGAIN)
  2216. goto again;
  2217. if (err) {
  2218. ret = err;
  2219. goto done;
  2220. }
  2221. b = p->nodes[level];
  2222. slot = p->slots[level];
  2223. /*
  2224. * slot 0 is special, if we change the key
  2225. * we have to update the parent pointer
  2226. * which means we must have a write lock
  2227. * on the parent
  2228. */
  2229. if (slot == 0 && cow &&
  2230. write_lock_level < level + 1) {
  2231. write_lock_level = level + 1;
  2232. btrfs_release_path(p);
  2233. goto again;
  2234. }
  2235. unlock_up(p, level, lowest_unlock,
  2236. min_write_lock_level, &write_lock_level);
  2237. if (level == lowest_level) {
  2238. if (dec)
  2239. p->slots[level]++;
  2240. goto done;
  2241. }
  2242. err = read_block_for_search(trans, root, p,
  2243. &b, level, slot, key, 0);
  2244. if (err == -EAGAIN)
  2245. goto again;
  2246. if (err) {
  2247. ret = err;
  2248. goto done;
  2249. }
  2250. if (!p->skip_locking) {
  2251. level = btrfs_header_level(b);
  2252. if (level <= write_lock_level) {
  2253. err = btrfs_try_tree_write_lock(b);
  2254. if (!err) {
  2255. btrfs_set_path_blocking(p);
  2256. btrfs_tree_lock(b);
  2257. btrfs_clear_path_blocking(p, b,
  2258. BTRFS_WRITE_LOCK);
  2259. }
  2260. p->locks[level] = BTRFS_WRITE_LOCK;
  2261. } else {
  2262. err = btrfs_try_tree_read_lock(b);
  2263. if (!err) {
  2264. btrfs_set_path_blocking(p);
  2265. btrfs_tree_read_lock(b);
  2266. btrfs_clear_path_blocking(p, b,
  2267. BTRFS_READ_LOCK);
  2268. }
  2269. p->locks[level] = BTRFS_READ_LOCK;
  2270. }
  2271. p->nodes[level] = b;
  2272. }
  2273. } else {
  2274. p->slots[level] = slot;
  2275. if (ins_len > 0 &&
  2276. btrfs_leaf_free_space(root, b) < ins_len) {
  2277. if (write_lock_level < 1) {
  2278. write_lock_level = 1;
  2279. btrfs_release_path(p);
  2280. goto again;
  2281. }
  2282. btrfs_set_path_blocking(p);
  2283. err = split_leaf(trans, root, key,
  2284. p, ins_len, ret == 0);
  2285. btrfs_clear_path_blocking(p, NULL, 0);
  2286. BUG_ON(err > 0);
  2287. if (err) {
  2288. ret = err;
  2289. goto done;
  2290. }
  2291. }
  2292. if (!p->search_for_split)
  2293. unlock_up(p, level, lowest_unlock,
  2294. min_write_lock_level, &write_lock_level);
  2295. goto done;
  2296. }
  2297. }
  2298. ret = 1;
  2299. done:
  2300. /*
  2301. * we don't really know what they plan on doing with the path
  2302. * from here on, so for now just mark it as blocking
  2303. */
  2304. if (!p->leave_spinning)
  2305. btrfs_set_path_blocking(p);
  2306. if (ret < 0)
  2307. btrfs_release_path(p);
  2308. return ret;
  2309. }
  2310. /*
  2311. * Like btrfs_search_slot, this looks for a key in the given tree. It uses the
  2312. * current state of the tree together with the operations recorded in the tree
  2313. * modification log to search for the key in a previous version of this tree, as
  2314. * denoted by the time_seq parameter.
  2315. *
  2316. * Naturally, there is no support for insert, delete or cow operations.
  2317. *
  2318. * The resulting path and return value will be set up as if we called
  2319. * btrfs_search_slot at that point in time with ins_len and cow both set to 0.
  2320. */
  2321. int btrfs_search_old_slot(struct btrfs_root *root, struct btrfs_key *key,
  2322. struct btrfs_path *p, u64 time_seq)
  2323. {
  2324. struct extent_buffer *b;
  2325. int slot;
  2326. int ret;
  2327. int err;
  2328. int level;
  2329. int lowest_unlock = 1;
  2330. u8 lowest_level = 0;
  2331. lowest_level = p->lowest_level;
  2332. WARN_ON(p->nodes[0] != NULL);
  2333. if (p->search_commit_root) {
  2334. BUG_ON(time_seq);
  2335. return btrfs_search_slot(NULL, root, key, p, 0, 0);
  2336. }
  2337. again:
  2338. b = get_old_root(root, time_seq);
  2339. extent_buffer_get(b);
  2340. level = btrfs_header_level(b);
  2341. btrfs_tree_read_lock(b);
  2342. p->locks[level] = BTRFS_READ_LOCK;
  2343. while (b) {
  2344. level = btrfs_header_level(b);
  2345. p->nodes[level] = b;
  2346. btrfs_clear_path_blocking(p, NULL, 0);
  2347. /*
  2348. * we have a lock on b and as long as we aren't changing
  2349. * the tree, there is no way to for the items in b to change.
  2350. * It is safe to drop the lock on our parent before we
  2351. * go through the expensive btree search on b.
  2352. */
  2353. btrfs_unlock_up_safe(p, level + 1);
  2354. ret = bin_search(b, key, level, &slot);
  2355. if (level != 0) {
  2356. int dec = 0;
  2357. if (ret && slot > 0) {
  2358. dec = 1;
  2359. slot -= 1;
  2360. }
  2361. p->slots[level] = slot;
  2362. unlock_up(p, level, lowest_unlock, 0, NULL);
  2363. if (level == lowest_level) {
  2364. if (dec)
  2365. p->slots[level]++;
  2366. goto done;
  2367. }
  2368. err = read_block_for_search(NULL, root, p, &b, level,
  2369. slot, key, time_seq);
  2370. if (err == -EAGAIN)
  2371. goto again;
  2372. if (err) {
  2373. ret = err;
  2374. goto done;
  2375. }
  2376. level = btrfs_header_level(b);
  2377. err = btrfs_try_tree_read_lock(b);
  2378. if (!err) {
  2379. btrfs_set_path_blocking(p);
  2380. btrfs_tree_read_lock(b);
  2381. btrfs_clear_path_blocking(p, b,
  2382. BTRFS_READ_LOCK);
  2383. }
  2384. p->locks[level] = BTRFS_READ_LOCK;
  2385. p->nodes[level] = b;
  2386. b = tree_mod_log_rewind(root->fs_info, b, time_seq);
  2387. if (b != p->nodes[level]) {
  2388. btrfs_tree_unlock_rw(p->nodes[level],
  2389. p->locks[level]);
  2390. p->locks[level] = 0;
  2391. p->nodes[level] = b;
  2392. }
  2393. } else {
  2394. p->slots[level] = slot;
  2395. unlock_up(p, level, lowest_unlock, 0, NULL);
  2396. goto done;
  2397. }
  2398. }
  2399. ret = 1;
  2400. done:
  2401. if (!p->leave_spinning)
  2402. btrfs_set_path_blocking(p);
  2403. if (ret < 0)
  2404. btrfs_release_path(p);
  2405. return ret;
  2406. }
  2407. /*
  2408. * adjust the pointers going up the tree, starting at level
  2409. * making sure the right key of each node is points to 'key'.
  2410. * This is used after shifting pointers to the left, so it stops
  2411. * fixing up pointers when a given leaf/node is not in slot 0 of the
  2412. * higher levels
  2413. *
  2414. */
  2415. static void fixup_low_keys(struct btrfs_trans_handle *trans,
  2416. struct btrfs_root *root, struct btrfs_path *path,
  2417. struct btrfs_disk_key *key, int level)
  2418. {
  2419. int i;
  2420. struct extent_buffer *t;
  2421. for (i = level; i < BTRFS_MAX_LEVEL; i++) {
  2422. int tslot = path->slots[i];
  2423. if (!path->nodes[i])
  2424. break;
  2425. t = path->nodes[i];
  2426. tree_mod_log_set_node_key(root->fs_info, t, key, tslot, 1);
  2427. btrfs_set_node_key(t, key, tslot);
  2428. btrfs_mark_buffer_dirty(path->nodes[i]);
  2429. if (tslot != 0)
  2430. break;
  2431. }
  2432. }
  2433. /*
  2434. * update item key.
  2435. *
  2436. * This function isn't completely safe. It's the caller's responsibility
  2437. * that the new key won't break the order
  2438. */
  2439. void btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
  2440. struct btrfs_root *root, struct btrfs_path *path,
  2441. struct btrfs_key *new_key)
  2442. {
  2443. struct btrfs_disk_key disk_key;
  2444. struct extent_buffer *eb;
  2445. int slot;
  2446. eb = path->nodes[0];
  2447. slot = path->slots[0];
  2448. if (slot > 0) {
  2449. btrfs_item_key(eb, &disk_key, slot - 1);
  2450. BUG_ON(comp_keys(&disk_key, new_key) >= 0);
  2451. }
  2452. if (slot < btrfs_header_nritems(eb) - 1) {
  2453. btrfs_item_key(eb, &disk_key, slot + 1);
  2454. BUG_ON(comp_keys(&disk_key, new_key) <= 0);
  2455. }
  2456. btrfs_cpu_key_to_disk(&disk_key, new_key);
  2457. btrfs_set_item_key(eb, &disk_key, slot);
  2458. btrfs_mark_buffer_dirty(eb);
  2459. if (slot == 0)
  2460. fixup_low_keys(trans, root, path, &disk_key, 1);
  2461. }
  2462. /*
  2463. * try to push data from one node into the next node left in the
  2464. * tree.
  2465. *
  2466. * returns 0 if some ptrs were pushed left, < 0 if there was some horrible
  2467. * error, and > 0 if there was no room in the left hand block.
  2468. */
  2469. static int push_node_left(struct btrfs_trans_handle *trans,
  2470. struct btrfs_root *root, struct extent_buffer *dst,
  2471. struct extent_buffer *src, int empty)
  2472. {
  2473. int push_items = 0;
  2474. int src_nritems;
  2475. int dst_nritems;
  2476. int ret = 0;
  2477. src_nritems = btrfs_header_nritems(src);
  2478. dst_nritems = btrfs_header_nritems(dst);
  2479. push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
  2480. WARN_ON(btrfs_header_generation(src) != trans->transid);
  2481. WARN_ON(btrfs_header_generation(dst) != trans->transid);
  2482. if (!empty && src_nritems <= 8)
  2483. return 1;
  2484. if (push_items <= 0)
  2485. return 1;
  2486. if (empty) {
  2487. push_items = min(src_nritems, push_items);
  2488. if (push_items < src_nritems) {
  2489. /* leave at least 8 pointers in the node if
  2490. * we aren't going to empty it
  2491. */
  2492. if (src_nritems - push_items < 8) {
  2493. if (push_items <= 8)
  2494. return 1;
  2495. push_items -= 8;
  2496. }
  2497. }
  2498. } else
  2499. push_items = min(src_nritems - 8, push_items);
  2500. tree_mod_log_eb_copy(root->fs_info, dst, src, dst_nritems, 0,
  2501. push_items);
  2502. copy_extent_buffer(dst, src,
  2503. btrfs_node_key_ptr_offset(dst_nritems),
  2504. btrfs_node_key_ptr_offset(0),
  2505. push_items * sizeof(struct btrfs_key_ptr));
  2506. if (push_items < src_nritems) {
  2507. tree_mod_log_eb_move(root->fs_info, src, 0, push_items,
  2508. src_nritems - push_items);
  2509. memmove_extent_buffer(src, btrfs_node_key_ptr_offset(0),
  2510. btrfs_node_key_ptr_offset(push_items),
  2511. (src_nritems - push_items) *
  2512. sizeof(struct btrfs_key_ptr));
  2513. }
  2514. btrfs_set_header_nritems(src, src_nritems - push_items);
  2515. btrfs_set_header_nritems(dst, dst_nritems + push_items);
  2516. btrfs_mark_buffer_dirty(src);
  2517. btrfs_mark_buffer_dirty(dst);
  2518. return ret;
  2519. }
  2520. /*
  2521. * try to push data from one node into the next node right in the
  2522. * tree.
  2523. *
  2524. * returns 0 if some ptrs were pushed, < 0 if there was some horrible
  2525. * error, and > 0 if there was no room in the right hand block.
  2526. *
  2527. * this will only push up to 1/2 the contents of the left node over
  2528. */
  2529. static int balance_node_right(struct btrfs_trans_handle *trans,
  2530. struct btrfs_root *root,
  2531. struct extent_buffer *dst,
  2532. struct extent_buffer *src)
  2533. {
  2534. int push_items = 0;
  2535. int max_push;
  2536. int src_nritems;
  2537. int dst_nritems;
  2538. int ret = 0;
  2539. WARN_ON(btrfs_header_generation(src) != trans->transid);
  2540. WARN_ON(btrfs_header_generation(dst) != trans->transid);
  2541. src_nritems = btrfs_header_nritems(src);
  2542. dst_nritems = btrfs_header_nritems(dst);
  2543. push_items = BTRFS_NODEPTRS_PER_BLOCK(root) - dst_nritems;
  2544. if (push_items <= 0)
  2545. return 1;
  2546. if (src_nritems < 4)
  2547. return 1;
  2548. max_push = src_nritems / 2 + 1;
  2549. /* don't try to empty the node */
  2550. if (max_push >= src_nritems)
  2551. return 1;
  2552. if (max_push < push_items)
  2553. push_items = max_push;
  2554. tree_mod_log_eb_move(root->fs_info, dst, push_items, 0, dst_nritems);
  2555. memmove_extent_buffer(dst, btrfs_node_key_ptr_offset(push_items),
  2556. btrfs_node_key_ptr_offset(0),
  2557. (dst_nritems) *
  2558. sizeof(struct btrfs_key_ptr));
  2559. tree_mod_log_eb_copy(root->fs_info, dst, src, 0,
  2560. src_nritems - push_items, push_items);
  2561. copy_extent_buffer(dst, src,
  2562. btrfs_node_key_ptr_offset(0),
  2563. btrfs_node_key_ptr_offset(src_nritems - push_items),
  2564. push_items * sizeof(struct btrfs_key_ptr));
  2565. btrfs_set_header_nritems(src, src_nritems - push_items);
  2566. btrfs_set_header_nritems(dst, dst_nritems + push_items);
  2567. btrfs_mark_buffer_dirty(src);
  2568. btrfs_mark_buffer_dirty(dst);
  2569. return ret;
  2570. }
  2571. /*
  2572. * helper function to insert a new root level in the tree.
  2573. * A new node is allocated, and a single item is inserted to
  2574. * point to the existing root
  2575. *
  2576. * returns zero on success or < 0 on failure.
  2577. */
  2578. static noinline int insert_new_root(struct btrfs_trans_handle *trans,
  2579. struct btrfs_root *root,
  2580. struct btrfs_path *path, int level)
  2581. {
  2582. u64 lower_gen;
  2583. struct extent_buffer *lower;
  2584. struct extent_buffer *c;
  2585. struct extent_buffer *old;
  2586. struct btrfs_disk_key lower_key;
  2587. BUG_ON(path->nodes[level]);
  2588. BUG_ON(path->nodes[level-1] != root->node);
  2589. lower = path->nodes[level-1];
  2590. if (level == 1)
  2591. btrfs_item_key(lower, &lower_key, 0);
  2592. else
  2593. btrfs_node_key(lower, &lower_key, 0);
  2594. c = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
  2595. root->root_key.objectid, &lower_key,
  2596. level, root->node->start, 0);
  2597. if (IS_ERR(c))
  2598. return PTR_ERR(c);
  2599. root_add_used(root, root->nodesize);
  2600. memset_extent_buffer(c, 0, 0, sizeof(struct btrfs_header));
  2601. btrfs_set_header_nritems(c, 1);
  2602. btrfs_set_header_level(c, level);
  2603. btrfs_set_header_bytenr(c, c->start);
  2604. btrfs_set_header_generation(c, trans->transid);
  2605. btrfs_set_header_backref_rev(c, BTRFS_MIXED_BACKREF_REV);
  2606. btrfs_set_header_owner(c, root->root_key.objectid);
  2607. write_extent_buffer(c, root->fs_info->fsid,
  2608. (unsigned long)btrfs_header_fsid(c),
  2609. BTRFS_FSID_SIZE);
  2610. write_extent_buffer(c, root->fs_info->chunk_tree_uuid,
  2611. (unsigned long)btrfs_header_chunk_tree_uuid(c),
  2612. BTRFS_UUID_SIZE);
  2613. btrfs_set_node_key(c, &lower_key, 0);
  2614. btrfs_set_node_blockptr(c, 0, lower->start);
  2615. lower_gen = btrfs_header_generation(lower);
  2616. WARN_ON(lower_gen != trans->transid);
  2617. btrfs_set_node_ptr_generation(c, 0, lower_gen);
  2618. btrfs_mark_buffer_dirty(c);
  2619. old = root->node;
  2620. tree_mod_log_set_root_pointer(root, c);
  2621. rcu_assign_pointer(root->node, c);
  2622. /* the super has an extra ref to root->node */
  2623. free_extent_buffer(old);
  2624. add_root_to_dirty_list(root);
  2625. extent_buffer_get(c);
  2626. path->nodes[level] = c;
  2627. path->locks[level] = BTRFS_WRITE_LOCK;
  2628. path->slots[level] = 0;
  2629. return 0;
  2630. }
  2631. /*
  2632. * worker function to insert a single pointer in a node.
  2633. * the node should have enough room for the pointer already
  2634. *
  2635. * slot and level indicate where you want the key to go, and
  2636. * blocknr is the block the key points to.
  2637. */
  2638. static void insert_ptr(struct btrfs_trans_handle *trans,
  2639. struct btrfs_root *root, struct btrfs_path *path,
  2640. struct btrfs_disk_key *key, u64 bytenr,
  2641. int slot, int level, int tree_mod_log)
  2642. {
  2643. struct extent_buffer *lower;
  2644. int nritems;
  2645. int ret;
  2646. BUG_ON(!path->nodes[level]);
  2647. btrfs_assert_tree_locked(path->nodes[level]);
  2648. lower = path->nodes[level];
  2649. nritems = btrfs_header_nritems(lower);
  2650. BUG_ON(slot > nritems);
  2651. BUG_ON(nritems == BTRFS_NODEPTRS_PER_BLOCK(root));
  2652. if (slot != nritems) {
  2653. if (tree_mod_log && level)
  2654. tree_mod_log_eb_move(root->fs_info, lower, slot + 1,
  2655. slot, nritems - slot);
  2656. memmove_extent_buffer(lower,
  2657. btrfs_node_key_ptr_offset(slot + 1),
  2658. btrfs_node_key_ptr_offset(slot),
  2659. (nritems - slot) * sizeof(struct btrfs_key_ptr));
  2660. }
  2661. if (tree_mod_log && level) {
  2662. ret = tree_mod_log_insert_key(root->fs_info, lower, slot,
  2663. MOD_LOG_KEY_ADD);
  2664. BUG_ON(ret < 0);
  2665. }
  2666. btrfs_set_node_key(lower, key, slot);
  2667. btrfs_set_node_blockptr(lower, slot, bytenr);
  2668. WARN_ON(trans->transid == 0);
  2669. btrfs_set_node_ptr_generation(lower, slot, trans->transid);
  2670. btrfs_set_header_nritems(lower, nritems + 1);
  2671. btrfs_mark_buffer_dirty(lower);
  2672. }
  2673. /*
  2674. * split the node at the specified level in path in two.
  2675. * The path is corrected to point to the appropriate node after the split
  2676. *
  2677. * Before splitting this tries to make some room in the node by pushing
  2678. * left and right, if either one works, it returns right away.
  2679. *
  2680. * returns 0 on success and < 0 on failure
  2681. */
  2682. static noinline int split_node(struct btrfs_trans_handle *trans,
  2683. struct btrfs_root *root,
  2684. struct btrfs_path *path, int level)
  2685. {
  2686. struct extent_buffer *c;
  2687. struct extent_buffer *split;
  2688. struct btrfs_disk_key disk_key;
  2689. int mid;
  2690. int ret;
  2691. u32 c_nritems;
  2692. c = path->nodes[level];
  2693. WARN_ON(btrfs_header_generation(c) != trans->transid);
  2694. if (c == root->node) {
  2695. /* trying to split the root, lets make a new one */
  2696. ret = insert_new_root(trans, root, path, level + 1);
  2697. if (ret)
  2698. return ret;
  2699. } else {
  2700. ret = push_nodes_for_insert(trans, root, path, level);
  2701. c = path->nodes[level];
  2702. if (!ret && btrfs_header_nritems(c) <
  2703. BTRFS_NODEPTRS_PER_BLOCK(root) - 3)
  2704. return 0;
  2705. if (ret < 0)
  2706. return ret;
  2707. }
  2708. c_nritems = btrfs_header_nritems(c);
  2709. mid = (c_nritems + 1) / 2;
  2710. btrfs_node_key(c, &disk_key, mid);
  2711. split = btrfs_alloc_free_block(trans, root, root->nodesize, 0,
  2712. root->root_key.objectid,
  2713. &disk_key, level, c->start, 0);
  2714. if (IS_ERR(split))
  2715. return PTR_ERR(split);
  2716. root_add_used(root, root->nodesize);
  2717. memset_extent_buffer(split, 0, 0, sizeof(struct btrfs_header));
  2718. btrfs_set_header_level(split, btrfs_header_level(c));
  2719. btrfs_set_header_bytenr(split, split->start);
  2720. btrfs_set_header_generation(split, trans->transid);
  2721. btrfs_set_header_backref_rev(split, BTRFS_MIXED_BACKREF_REV);
  2722. btrfs_set_header_owner(split, root->root_key.objectid);
  2723. write_extent_buffer(split, root->fs_info->fsid,
  2724. (unsigned long)btrfs_header_fsid(split),
  2725. BTRFS_FSID_SIZE);
  2726. write_extent_buffer(split, root->fs_info->chunk_tree_uuid,
  2727. (unsigned long)btrfs_header_chunk_tree_uuid(split),
  2728. BTRFS_UUID_SIZE);
  2729. tree_mod_log_eb_copy(root->fs_info, split, c, 0, mid, c_nritems - mid);
  2730. copy_extent_buffer(split, c,
  2731. btrfs_node_key_ptr_offset(0),
  2732. btrfs_node_key_ptr_offset(mid),
  2733. (c_nritems - mid) * sizeof(struct btrfs_key_ptr));
  2734. btrfs_set_header_nritems(split, c_nritems - mid);
  2735. btrfs_set_header_nritems(c, mid);
  2736. ret = 0;
  2737. btrfs_mark_buffer_dirty(c);
  2738. btrfs_mark_buffer_dirty(split);
  2739. insert_ptr(trans, root, path, &disk_key, split->start,
  2740. path->slots[level + 1] + 1, level + 1, 1);
  2741. if (path->slots[level] >= mid) {
  2742. path->slots[level] -= mid;
  2743. btrfs_tree_unlock(c);
  2744. free_extent_buffer(c);
  2745. path->nodes[level] = split;
  2746. path->slots[level + 1] += 1;
  2747. } else {
  2748. btrfs_tree_unlock(split);
  2749. free_extent_buffer(split);
  2750. }
  2751. return ret;
  2752. }
  2753. /*
  2754. * how many bytes are required to store the items in a leaf. start
  2755. * and nr indicate which items in the leaf to check. This totals up the
  2756. * space used both by the item structs and the item data
  2757. */
  2758. static int leaf_space_used(struct extent_buffer *l, int start, int nr)
  2759. {
  2760. int data_len;
  2761. int nritems = btrfs_header_nritems(l);
  2762. int end = min(nritems, start + nr) - 1;
  2763. if (!nr)
  2764. return 0;
  2765. data_len = btrfs_item_end_nr(l, start);
  2766. data_len = data_len - btrfs_item_offset_nr(l, end);
  2767. data_len += sizeof(struct btrfs_item) * nr;
  2768. WARN_ON(data_len < 0);
  2769. return data_len;
  2770. }
  2771. /*
  2772. * The space between the end of the leaf items and
  2773. * the start of the leaf data. IOW, how much room
  2774. * the leaf has left for both items and data
  2775. */
  2776. noinline int btrfs_leaf_free_space(struct btrfs_root *root,
  2777. struct extent_buffer *leaf)
  2778. {
  2779. int nritems = btrfs_header_nritems(leaf);
  2780. int ret;
  2781. ret = BTRFS_LEAF_DATA_SIZE(root) - leaf_space_used(leaf, 0, nritems);
  2782. if (ret < 0) {
  2783. printk(KERN_CRIT "leaf free space ret %d, leaf data size %lu, "
  2784. "used %d nritems %d\n",
  2785. ret, (unsigned long) BTRFS_LEAF_DATA_SIZE(root),
  2786. leaf_space_used(leaf, 0, nritems), nritems);
  2787. }
  2788. return ret;
  2789. }
  2790. /*
  2791. * min slot controls the lowest index we're willing to push to the
  2792. * right. We'll push up to and including min_slot, but no lower
  2793. */
  2794. static noinline int __push_leaf_right(struct btrfs_trans_handle *trans,
  2795. struct btrfs_root *root,
  2796. struct btrfs_path *path,
  2797. int data_size, int empty,
  2798. struct extent_buffer *right,
  2799. int free_space, u32 left_nritems,
  2800. u32 min_slot)
  2801. {
  2802. struct extent_buffer *left = path->nodes[0];
  2803. struct extent_buffer *upper = path->nodes[1];
  2804. struct btrfs_map_token token;
  2805. struct btrfs_disk_key disk_key;
  2806. int slot;
  2807. u32 i;
  2808. int push_space = 0;
  2809. int push_items = 0;
  2810. struct btrfs_item *item;
  2811. u32 nr;
  2812. u32 right_nritems;
  2813. u32 data_end;
  2814. u32 this_item_size;
  2815. btrfs_init_map_token(&token);
  2816. if (empty)
  2817. nr = 0;
  2818. else
  2819. nr = max_t(u32, 1, min_slot);
  2820. if (path->slots[0] >= left_nritems)
  2821. push_space += data_size;
  2822. slot = path->slots[1];
  2823. i = left_nritems - 1;
  2824. while (i >= nr) {
  2825. item = btrfs_item_nr(left, i);
  2826. if (!empty && push_items > 0) {
  2827. if (path->slots[0] > i)
  2828. break;
  2829. if (path->slots[0] == i) {
  2830. int space = btrfs_leaf_free_space(root, left);
  2831. if (space + push_space * 2 > free_space)
  2832. break;
  2833. }
  2834. }
  2835. if (path->slots[0] == i)
  2836. push_space += data_size;
  2837. this_item_size = btrfs_item_size(left, item);
  2838. if (this_item_size + sizeof(*item) + push_space > free_space)
  2839. break;
  2840. push_items++;
  2841. push_space += this_item_size + sizeof(*item);
  2842. if (i == 0)
  2843. break;
  2844. i--;
  2845. }
  2846. if (push_items == 0)
  2847. goto out_unlock;
  2848. if (!empty && push_items == left_nritems)
  2849. WARN_ON(1);
  2850. /* push left to right */
  2851. right_nritems = btrfs_header_nritems(right);
  2852. push_space = btrfs_item_end_nr(left, left_nritems - push_items);
  2853. push_space -= leaf_data_end(root, left);
  2854. /* make room in the right data area */
  2855. data_end = leaf_data_end(root, right);
  2856. memmove_extent_buffer(right,
  2857. btrfs_leaf_data(right) + data_end - push_space,
  2858. btrfs_leaf_data(right) + data_end,
  2859. BTRFS_LEAF_DATA_SIZE(root) - data_end);
  2860. /* copy from the left data area */
  2861. copy_extent_buffer(right, left, btrfs_leaf_data(right) +
  2862. BTRFS_LEAF_DATA_SIZE(root) - push_space,
  2863. btrfs_leaf_data(left) + leaf_data_end(root, left),
  2864. push_space);
  2865. memmove_extent_buffer(right, btrfs_item_nr_offset(push_items),
  2866. btrfs_item_nr_offset(0),
  2867. right_nritems * sizeof(struct btrfs_item));
  2868. /* copy the items from left to right */
  2869. copy_extent_buffer(right, left, btrfs_item_nr_offset(0),
  2870. btrfs_item_nr_offset(left_nritems - push_items),
  2871. push_items * sizeof(struct btrfs_item));
  2872. /* update the item pointers */
  2873. right_nritems += push_items;
  2874. btrfs_set_header_nritems(right, right_nritems);
  2875. push_space = BTRFS_LEAF_DATA_SIZE(root);
  2876. for (i = 0; i < right_nritems; i++) {
  2877. item = btrfs_item_nr(right, i);
  2878. push_space -= btrfs_token_item_size(right, item, &token);
  2879. btrfs_set_token_item_offset(right, item, push_space, &token);
  2880. }
  2881. left_nritems -= push_items;
  2882. btrfs_set_header_nritems(left, left_nritems);
  2883. if (left_nritems)
  2884. btrfs_mark_buffer_dirty(left);
  2885. else
  2886. clean_tree_block(trans, root, left);
  2887. btrfs_mark_buffer_dirty(right);
  2888. btrfs_item_key(right, &disk_key, 0);
  2889. btrfs_set_node_key(upper, &disk_key, slot + 1);
  2890. btrfs_mark_buffer_dirty(upper);
  2891. /* then fixup the leaf pointer in the path */
  2892. if (path->slots[0] >= left_nritems) {
  2893. path->slots[0] -= left_nritems;
  2894. if (btrfs_header_nritems(path->nodes[0]) == 0)
  2895. clean_tree_block(trans, root, path->nodes[0]);
  2896. btrfs_tree_unlock(path->nodes[0]);
  2897. free_extent_buffer(path->nodes[0]);
  2898. path->nodes[0] = right;
  2899. path->slots[1] += 1;
  2900. } else {
  2901. btrfs_tree_unlock(right);
  2902. free_extent_buffer(right);
  2903. }
  2904. return 0;
  2905. out_unlock:
  2906. btrfs_tree_unlock(right);
  2907. free_extent_buffer(right);
  2908. return 1;
  2909. }
  2910. /*
  2911. * push some data in the path leaf to the right, trying to free up at
  2912. * least data_size bytes. returns zero if the push worked, nonzero otherwise
  2913. *
  2914. * returns 1 if the push failed because the other node didn't have enough
  2915. * room, 0 if everything worked out and < 0 if there were major errors.
  2916. *
  2917. * this will push starting from min_slot to the end of the leaf. It won't
  2918. * push any slot lower than min_slot
  2919. */
  2920. static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
  2921. *root, struct btrfs_path *path,
  2922. int min_data_size, int data_size,
  2923. int empty, u32 min_slot)
  2924. {
  2925. struct extent_buffer *left = path->nodes[0];
  2926. struct extent_buffer *right;
  2927. struct extent_buffer *upper;
  2928. int slot;
  2929. int free_space;
  2930. u32 left_nritems;
  2931. int ret;
  2932. if (!path->nodes[1])
  2933. return 1;
  2934. slot = path->slots[1];
  2935. upper = path->nodes[1];
  2936. if (slot >= btrfs_header_nritems(upper) - 1)
  2937. return 1;
  2938. btrfs_assert_tree_locked(path->nodes[1]);
  2939. right = read_node_slot(root, upper, slot + 1);
  2940. if (right == NULL)
  2941. return 1;
  2942. btrfs_tree_lock(right);
  2943. btrfs_set_lock_blocking(right);
  2944. free_space = btrfs_leaf_free_space(root, right);
  2945. if (free_space < data_size)
  2946. goto out_unlock;
  2947. /* cow and double check */
  2948. ret = btrfs_cow_block(trans, root, right, upper,
  2949. slot + 1, &right);
  2950. if (ret)
  2951. goto out_unlock;
  2952. free_space = btrfs_leaf_free_space(root, right);
  2953. if (free_space < data_size)
  2954. goto out_unlock;
  2955. left_nritems = btrfs_header_nritems(left);
  2956. if (left_nritems == 0)
  2957. goto out_unlock;
  2958. return __push_leaf_right(trans, root, path, min_data_size, empty,
  2959. right, free_space, left_nritems, min_slot);
  2960. out_unlock:
  2961. btrfs_tree_unlock(right);
  2962. free_extent_buffer(right);
  2963. return 1;
  2964. }
  2965. /*
  2966. * push some data in the path leaf to the left, trying to free up at
  2967. * least data_size bytes. returns zero if the push worked, nonzero otherwise
  2968. *
  2969. * max_slot can put a limit on how far into the leaf we'll push items. The
  2970. * item at 'max_slot' won't be touched. Use (u32)-1 to make us do all the
  2971. * items
  2972. */
  2973. static noinline int __push_leaf_left(struct btrfs_trans_handle *trans,
  2974. struct btrfs_root *root,
  2975. struct btrfs_path *path, int data_size,
  2976. int empty, struct extent_buffer *left,
  2977. int free_space, u32 right_nritems,
  2978. u32 max_slot)
  2979. {
  2980. struct btrfs_disk_key disk_key;
  2981. struct extent_buffer *right = path->nodes[0];
  2982. int i;
  2983. int push_space = 0;
  2984. int push_items = 0;
  2985. struct btrfs_item *item;
  2986. u32 old_left_nritems;
  2987. u32 nr;
  2988. int ret = 0;
  2989. u32 this_item_size;
  2990. u32 old_left_item_size;
  2991. struct btrfs_map_token token;
  2992. btrfs_init_map_token(&token);
  2993. if (empty)
  2994. nr = min(right_nritems, max_slot);
  2995. else
  2996. nr = min(right_nritems - 1, max_slot);
  2997. for (i = 0; i < nr; i++) {
  2998. item = btrfs_item_nr(right, i);
  2999. if (!empty && push_items > 0) {
  3000. if (path->slots[0] < i)
  3001. break;
  3002. if (path->slots[0] == i) {
  3003. int space = btrfs_leaf_free_space(root, right);
  3004. if (space + push_space * 2 > free_space)
  3005. break;
  3006. }
  3007. }
  3008. if (path->slots[0] == i)
  3009. push_space += data_size;
  3010. this_item_size = btrfs_item_size(right, item);
  3011. if (this_item_size + sizeof(*item) + push_space > free_space)
  3012. break;
  3013. push_items++;
  3014. push_space += this_item_size + sizeof(*item);
  3015. }
  3016. if (push_items == 0) {
  3017. ret = 1;
  3018. goto out;
  3019. }
  3020. if (!empty && push_items == btrfs_header_nritems(right))
  3021. WARN_ON(1);
  3022. /* push data from right to left */
  3023. copy_extent_buffer(left, right,
  3024. btrfs_item_nr_offset(btrfs_header_nritems(left)),
  3025. btrfs_item_nr_offset(0),
  3026. push_items * sizeof(struct btrfs_item));
  3027. push_space = BTRFS_LEAF_DATA_SIZE(root) -
  3028. btrfs_item_offset_nr(right, push_items - 1);
  3029. copy_extent_buffer(left, right, btrfs_leaf_data(left) +
  3030. leaf_data_end(root, left) - push_space,
  3031. btrfs_leaf_data(right) +
  3032. btrfs_item_offset_nr(right, push_items - 1),
  3033. push_space);
  3034. old_left_nritems = btrfs_header_nritems(left);
  3035. BUG_ON(old_left_nritems <= 0);
  3036. old_left_item_size = btrfs_item_offset_nr(left, old_left_nritems - 1);
  3037. for (i = old_left_nritems; i < old_left_nritems + push_items; i++) {
  3038. u32 ioff;
  3039. item = btrfs_item_nr(left, i);
  3040. ioff = btrfs_token_item_offset(left, item, &token);
  3041. btrfs_set_token_item_offset(left, item,
  3042. ioff - (BTRFS_LEAF_DATA_SIZE(root) - old_left_item_size),
  3043. &token);
  3044. }
  3045. btrfs_set_header_nritems(left, old_left_nritems + push_items);
  3046. /* fixup right node */
  3047. if (push_items > right_nritems) {
  3048. printk(KERN_CRIT "push items %d nr %u\n", push_items,
  3049. right_nritems);
  3050. WARN_ON(1);
  3051. }
  3052. if (push_items < right_nritems) {
  3053. push_space = btrfs_item_offset_nr(right, push_items - 1) -
  3054. leaf_data_end(root, right);
  3055. memmove_extent_buffer(right, btrfs_leaf_data(right) +
  3056. BTRFS_LEAF_DATA_SIZE(root) - push_space,
  3057. btrfs_leaf_data(right) +
  3058. leaf_data_end(root, right), push_space);
  3059. memmove_extent_buffer(right, btrfs_item_nr_offset(0),
  3060. btrfs_item_nr_offset(push_items),
  3061. (btrfs_header_nritems(right) - push_items) *
  3062. sizeof(struct btrfs_item));
  3063. }
  3064. right_nritems -= push_items;
  3065. btrfs_set_header_nritems(right, right_nritems);
  3066. push_space = BTRFS_LEAF_DATA_SIZE(root);
  3067. for (i = 0; i < right_nritems; i++) {
  3068. item = btrfs_item_nr(right, i);
  3069. push_space = push_space - btrfs_token_item_size(right,
  3070. item, &token);
  3071. btrfs_set_token_item_offset(right, item, push_space, &token);
  3072. }
  3073. btrfs_mark_buffer_dirty(left);
  3074. if (right_nritems)
  3075. btrfs_mark_buffer_dirty(right);
  3076. else
  3077. clean_tree_block(trans, root, right);
  3078. btrfs_item_key(right, &disk_key, 0);
  3079. fixup_low_keys(trans, root, path, &disk_key, 1);
  3080. /* then fixup the leaf pointer in the path */
  3081. if (path->slots[0] < push_items) {
  3082. path->slots[0] += old_left_nritems;
  3083. btrfs_tree_unlock(path->nodes[0]);
  3084. free_extent_buffer(path->nodes[0]);
  3085. path->nodes[0] = left;
  3086. path->slots[1] -= 1;
  3087. } else {
  3088. btrfs_tree_unlock(left);
  3089. free_extent_buffer(left);
  3090. path->slots[0] -= push_items;
  3091. }
  3092. BUG_ON(path->slots[0] < 0);
  3093. return ret;
  3094. out:
  3095. btrfs_tree_unlock(left);
  3096. free_extent_buffer(left);
  3097. return ret;
  3098. }
  3099. /*
  3100. * push some data in the path leaf to the left, trying to free up at
  3101. * least data_size bytes. returns zero if the push worked, nonzero otherwise
  3102. *
  3103. * max_slot can put a limit on how far into the leaf we'll push items. The
  3104. * item at 'max_slot' won't be touched. Use (u32)-1 to make us push all the
  3105. * items
  3106. */
  3107. static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
  3108. *root, struct btrfs_path *path, int min_data_size,
  3109. int data_size, int empty, u32 max_slot)
  3110. {
  3111. struct extent_buffer *right = path->nodes[0];
  3112. struct extent_buffer *left;
  3113. int slot;
  3114. int free_space;
  3115. u32 right_nritems;
  3116. int ret = 0;
  3117. slot = path->slots[1];
  3118. if (slot == 0)
  3119. return 1;
  3120. if (!path->nodes[1])
  3121. return 1;
  3122. right_nritems = btrfs_header_nritems(right);
  3123. if (right_nritems == 0)
  3124. return 1;
  3125. btrfs_assert_tree_locked(path->nodes[1]);
  3126. left = read_node_slot(root, path->nodes[1], slot - 1);
  3127. if (left == NULL)
  3128. return 1;
  3129. btrfs_tree_lock(left);
  3130. btrfs_set_lock_blocking(left);
  3131. free_space = btrfs_leaf_free_space(root, left);
  3132. if (free_space < data_size) {
  3133. ret = 1;
  3134. goto out;
  3135. }
  3136. /* cow and double check */
  3137. ret = btrfs_cow_block(trans, root, left,
  3138. path->nodes[1], slot - 1, &left);
  3139. if (ret) {
  3140. /* we hit -ENOSPC, but it isn't fatal here */
  3141. if (ret == -ENOSPC)
  3142. ret = 1;
  3143. goto out;
  3144. }
  3145. free_space = btrfs_leaf_free_space(root, left);
  3146. if (free_space < data_size) {
  3147. ret = 1;
  3148. goto out;
  3149. }
  3150. return __push_leaf_left(trans, root, path, min_data_size,
  3151. empty, left, free_space, right_nritems,
  3152. max_slot);
  3153. out:
  3154. btrfs_tree_unlock(left);
  3155. free_extent_buffer(left);
  3156. return ret;
  3157. }
  3158. /*
  3159. * split the path's leaf in two, making sure there is at least data_size
  3160. * available for the resulting leaf level of the path.
  3161. */
  3162. static noinline void copy_for_split(struct btrfs_trans_handle *trans,
  3163. struct btrfs_root *root,
  3164. struct btrfs_path *path,
  3165. struct extent_buffer *l,
  3166. struct extent_buffer *right,
  3167. int slot, int mid, int nritems)
  3168. {
  3169. int data_copy_size;
  3170. int rt_data_off;
  3171. int i;
  3172. struct btrfs_disk_key disk_key;
  3173. struct btrfs_map_token token;
  3174. btrfs_init_map_token(&token);
  3175. nritems = nritems - mid;
  3176. btrfs_set_header_nritems(right, nritems);
  3177. data_copy_size = btrfs_item_end_nr(l, mid) - leaf_data_end(root, l);
  3178. copy_extent_buffer(right, l, btrfs_item_nr_offset(0),
  3179. btrfs_item_nr_offset(mid),
  3180. nritems * sizeof(struct btrfs_item));
  3181. copy_extent_buffer(right, l,
  3182. btrfs_leaf_data(right) + BTRFS_LEAF_DATA_SIZE(root) -
  3183. data_copy_size, btrfs_leaf_data(l) +
  3184. leaf_data_end(root, l), data_copy_size);
  3185. rt_data_off = BTRFS_LEAF_DATA_SIZE(root) -
  3186. btrfs_item_end_nr(l, mid);
  3187. for (i = 0; i < nritems; i++) {
  3188. struct btrfs_item *item = btrfs_item_nr(right, i);
  3189. u32 ioff;
  3190. ioff = btrfs_token_item_offset(right, item, &token);
  3191. btrfs_set_token_item_offset(right, item,
  3192. ioff + rt_data_off, &token);
  3193. }
  3194. btrfs_set_header_nritems(l, mid);
  3195. btrfs_item_key(right, &disk_key, 0);
  3196. insert_ptr(trans, root, path, &disk_key, right->start,
  3197. path->slots[1] + 1, 1, 0);
  3198. btrfs_mark_buffer_dirty(right);
  3199. btrfs_mark_buffer_dirty(l);
  3200. BUG_ON(path->slots[0] != slot);
  3201. if (mid <= slot) {
  3202. btrfs_tree_unlock(path->nodes[0]);
  3203. free_extent_buffer(path->nodes[0]);
  3204. path->nodes[0] = right;
  3205. path->slots[0] -= mid;
  3206. path->slots[1] += 1;
  3207. } else {
  3208. btrfs_tree_unlock(right);
  3209. free_extent_buffer(right);
  3210. }
  3211. BUG_ON(path->slots[0] < 0);
  3212. }
  3213. /*
  3214. * double splits happen when we need to insert a big item in the middle
  3215. * of a leaf. A double split can leave us with 3 mostly empty leaves:
  3216. * leaf: [ slots 0 - N] [ our target ] [ N + 1 - total in leaf ]
  3217. * A B C
  3218. *
  3219. * We avoid this by trying to push the items on either side of our target
  3220. * into the adjacent leaves. If all goes well we can avoid the double split
  3221. * completely.
  3222. */
  3223. static noinline int push_for_double_split(struct btrfs_trans_handle *trans,
  3224. struct btrfs_root *root,
  3225. struct btrfs_path *path,
  3226. int data_size)
  3227. {
  3228. int ret;
  3229. int progress = 0;
  3230. int slot;
  3231. u32 nritems;
  3232. slot = path->slots[0];
  3233. /*
  3234. * try to push all the items after our slot into the
  3235. * right leaf
  3236. */
  3237. ret = push_leaf_right(trans, root, path, 1, data_size, 0, slot);
  3238. if (ret < 0)
  3239. return ret;
  3240. if (ret == 0)
  3241. progress++;
  3242. nritems = btrfs_header_nritems(path->nodes[0]);
  3243. /*
  3244. * our goal is to get our slot at the start or end of a leaf. If
  3245. * we've done so we're done
  3246. */
  3247. if (path->slots[0] == 0 || path->slots[0] == nritems)
  3248. return 0;
  3249. if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
  3250. return 0;
  3251. /* try to push all the items before our slot into the next leaf */
  3252. slot = path->slots[0];
  3253. ret = push_leaf_left(trans, root, path, 1, data_size, 0, slot);
  3254. if (ret < 0)
  3255. return ret;
  3256. if (ret == 0)
  3257. progress++;
  3258. if (progress)
  3259. return 0;
  3260. return 1;
  3261. }
  3262. /*
  3263. * split the path's leaf in two, making sure there is at least data_size
  3264. * available for the resulting leaf level of the path.
  3265. *
  3266. * returns 0 if all went well and < 0 on failure.
  3267. */
  3268. static noinline int split_leaf(struct btrfs_trans_handle *trans,
  3269. struct btrfs_root *root,
  3270. struct btrfs_key *ins_key,
  3271. struct btrfs_path *path, int data_size,
  3272. int extend)
  3273. {
  3274. struct btrfs_disk_key disk_key;
  3275. struct extent_buffer *l;
  3276. u32 nritems;
  3277. int mid;
  3278. int slot;
  3279. struct extent_buffer *right;
  3280. int ret = 0;
  3281. int wret;
  3282. int split;
  3283. int num_doubles = 0;
  3284. int tried_avoid_double = 0;
  3285. l = path->nodes[0];
  3286. slot = path->slots[0];
  3287. if (extend && data_size + btrfs_item_size_nr(l, slot) +
  3288. sizeof(struct btrfs_item) > BTRFS_LEAF_DATA_SIZE(root))
  3289. return -EOVERFLOW;
  3290. /* first try to make some room by pushing left and right */
  3291. if (data_size) {
  3292. wret = push_leaf_right(trans, root, path, data_size,
  3293. data_size, 0, 0);
  3294. if (wret < 0)
  3295. return wret;
  3296. if (wret) {
  3297. wret = push_leaf_left(trans, root, path, data_size,
  3298. data_size, 0, (u32)-1);
  3299. if (wret < 0)
  3300. return wret;
  3301. }
  3302. l = path->nodes[0];
  3303. /* did the pushes work? */
  3304. if (btrfs_leaf_free_space(root, l) >= data_size)
  3305. return 0;
  3306. }
  3307. if (!path->nodes[1]) {
  3308. ret = insert_new_root(trans, root, path, 1);
  3309. if (ret)
  3310. return ret;
  3311. }
  3312. again:
  3313. split = 1;
  3314. l = path->nodes[0];
  3315. slot = path->slots[0];
  3316. nritems = btrfs_header_nritems(l);
  3317. mid = (nritems + 1) / 2;
  3318. if (mid <= slot) {
  3319. if (nritems == 1 ||
  3320. leaf_space_used(l, mid, nritems - mid) + data_size >
  3321. BTRFS_LEAF_DATA_SIZE(root)) {
  3322. if (slot >= nritems) {
  3323. split = 0;
  3324. } else {
  3325. mid = slot;
  3326. if (mid != nritems &&
  3327. leaf_space_used(l, mid, nritems - mid) +
  3328. data_size > BTRFS_LEAF_DATA_SIZE(root)) {
  3329. if (data_size && !tried_avoid_double)
  3330. goto push_for_double;
  3331. split = 2;
  3332. }
  3333. }
  3334. }
  3335. } else {
  3336. if (leaf_space_used(l, 0, mid) + data_size >
  3337. BTRFS_LEAF_DATA_SIZE(root)) {
  3338. if (!extend && data_size && slot == 0) {
  3339. split = 0;
  3340. } else if ((extend || !data_size) && slot == 0) {
  3341. mid = 1;
  3342. } else {
  3343. mid = slot;
  3344. if (mid != nritems &&
  3345. leaf_space_used(l, mid, nritems - mid) +
  3346. data_size > BTRFS_LEAF_DATA_SIZE(root)) {
  3347. if (data_size && !tried_avoid_double)
  3348. goto push_for_double;
  3349. split = 2 ;
  3350. }
  3351. }
  3352. }
  3353. }
  3354. if (split == 0)
  3355. btrfs_cpu_key_to_disk(&disk_key, ins_key);
  3356. else
  3357. btrfs_item_key(l, &disk_key, mid);
  3358. right = btrfs_alloc_free_block(trans, root, root->leafsize, 0,
  3359. root->root_key.objectid,
  3360. &disk_key, 0, l->start, 0);
  3361. if (IS_ERR(right))
  3362. return PTR_ERR(right);
  3363. root_add_used(root, root->leafsize);
  3364. memset_extent_buffer(right, 0, 0, sizeof(struct btrfs_header));
  3365. btrfs_set_header_bytenr(right, right->start);
  3366. btrfs_set_header_generation(right, trans->transid);
  3367. btrfs_set_header_backref_rev(right, BTRFS_MIXED_BACKREF_REV);
  3368. btrfs_set_header_owner(right, root->root_key.objectid);
  3369. btrfs_set_header_level(right, 0);
  3370. write_extent_buffer(right, root->fs_info->fsid,
  3371. (unsigned long)btrfs_header_fsid(right),
  3372. BTRFS_FSID_SIZE);
  3373. write_extent_buffer(right, root->fs_info->chunk_tree_uuid,
  3374. (unsigned long)btrfs_header_chunk_tree_uuid(right),
  3375. BTRFS_UUID_SIZE);
  3376. if (split == 0) {
  3377. if (mid <= slot) {
  3378. btrfs_set_header_nritems(right, 0);
  3379. insert_ptr(trans, root, path, &disk_key, right->start,
  3380. path->slots[1] + 1, 1, 0);
  3381. btrfs_tree_unlock(path->nodes[0]);
  3382. free_extent_buffer(path->nodes[0]);
  3383. path->nodes[0] = right;
  3384. path->slots[0] = 0;
  3385. path->slots[1] += 1;
  3386. } else {
  3387. btrfs_set_header_nritems(right, 0);
  3388. insert_ptr(trans, root, path, &disk_key, right->start,
  3389. path->slots[1], 1, 0);
  3390. btrfs_tree_unlock(path->nodes[0]);
  3391. free_extent_buffer(path->nodes[0]);
  3392. path->nodes[0] = right;
  3393. path->slots[0] = 0;
  3394. if (path->slots[1] == 0)
  3395. fixup_low_keys(trans, root, path,
  3396. &disk_key, 1);
  3397. }
  3398. btrfs_mark_buffer_dirty(right);
  3399. return ret;
  3400. }
  3401. copy_for_split(trans, root, path, l, right, slot, mid, nritems);
  3402. if (split == 2) {
  3403. BUG_ON(num_doubles != 0);
  3404. num_doubles++;
  3405. goto again;
  3406. }
  3407. return 0;
  3408. push_for_double:
  3409. push_for_double_split(trans, root, path, data_size);
  3410. tried_avoid_double = 1;
  3411. if (btrfs_leaf_free_space(root, path->nodes[0]) >= data_size)
  3412. return 0;
  3413. goto again;
  3414. }
  3415. static noinline int setup_leaf_for_split(struct btrfs_trans_handle *trans,
  3416. struct btrfs_root *root,
  3417. struct btrfs_path *path, int ins_len)
  3418. {
  3419. struct btrfs_key key;
  3420. struct extent_buffer *leaf;
  3421. struct btrfs_file_extent_item *fi;
  3422. u64 extent_len = 0;
  3423. u32 item_size;
  3424. int ret;
  3425. leaf = path->nodes[0];
  3426. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  3427. BUG_ON(key.type != BTRFS_EXTENT_DATA_KEY &&
  3428. key.type != BTRFS_EXTENT_CSUM_KEY);
  3429. if (btrfs_leaf_free_space(root, leaf) >= ins_len)
  3430. return 0;
  3431. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  3432. if (key.type == BTRFS_EXTENT_DATA_KEY) {
  3433. fi = btrfs_item_ptr(leaf, path->slots[0],
  3434. struct btrfs_file_extent_item);
  3435. extent_len = btrfs_file_extent_num_bytes(leaf, fi);
  3436. }
  3437. btrfs_release_path(path);
  3438. path->keep_locks = 1;
  3439. path->search_for_split = 1;
  3440. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  3441. path->search_for_split = 0;
  3442. if (ret < 0)
  3443. goto err;
  3444. ret = -EAGAIN;
  3445. leaf = path->nodes[0];
  3446. /* if our item isn't there or got smaller, return now */
  3447. if (ret > 0 || item_size != btrfs_item_size_nr(leaf, path->slots[0]))
  3448. goto err;
  3449. /* the leaf has changed, it now has room. return now */
  3450. if (btrfs_leaf_free_space(root, path->nodes[0]) >= ins_len)
  3451. goto err;
  3452. if (key.type == BTRFS_EXTENT_DATA_KEY) {
  3453. fi = btrfs_item_ptr(leaf, path->slots[0],
  3454. struct btrfs_file_extent_item);
  3455. if (extent_len != btrfs_file_extent_num_bytes(leaf, fi))
  3456. goto err;
  3457. }
  3458. btrfs_set_path_blocking(path);
  3459. ret = split_leaf(trans, root, &key, path, ins_len, 1);
  3460. if (ret)
  3461. goto err;
  3462. path->keep_locks = 0;
  3463. btrfs_unlock_up_safe(path, 1);
  3464. return 0;
  3465. err:
  3466. path->keep_locks = 0;
  3467. return ret;
  3468. }
  3469. static noinline int split_item(struct btrfs_trans_handle *trans,
  3470. struct btrfs_root *root,
  3471. struct btrfs_path *path,
  3472. struct btrfs_key *new_key,
  3473. unsigned long split_offset)
  3474. {
  3475. struct extent_buffer *leaf;
  3476. struct btrfs_item *item;
  3477. struct btrfs_item *new_item;
  3478. int slot;
  3479. char *buf;
  3480. u32 nritems;
  3481. u32 item_size;
  3482. u32 orig_offset;
  3483. struct btrfs_disk_key disk_key;
  3484. leaf = path->nodes[0];
  3485. BUG_ON(btrfs_leaf_free_space(root, leaf) < sizeof(struct btrfs_item));
  3486. btrfs_set_path_blocking(path);
  3487. item = btrfs_item_nr(leaf, path->slots[0]);
  3488. orig_offset = btrfs_item_offset(leaf, item);
  3489. item_size = btrfs_item_size(leaf, item);
  3490. buf = kmalloc(item_size, GFP_NOFS);
  3491. if (!buf)
  3492. return -ENOMEM;
  3493. read_extent_buffer(leaf, buf, btrfs_item_ptr_offset(leaf,
  3494. path->slots[0]), item_size);
  3495. slot = path->slots[0] + 1;
  3496. nritems = btrfs_header_nritems(leaf);
  3497. if (slot != nritems) {
  3498. /* shift the items */
  3499. memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + 1),
  3500. btrfs_item_nr_offset(slot),
  3501. (nritems - slot) * sizeof(struct btrfs_item));
  3502. }
  3503. btrfs_cpu_key_to_disk(&disk_key, new_key);
  3504. btrfs_set_item_key(leaf, &disk_key, slot);
  3505. new_item = btrfs_item_nr(leaf, slot);
  3506. btrfs_set_item_offset(leaf, new_item, orig_offset);
  3507. btrfs_set_item_size(leaf, new_item, item_size - split_offset);
  3508. btrfs_set_item_offset(leaf, item,
  3509. orig_offset + item_size - split_offset);
  3510. btrfs_set_item_size(leaf, item, split_offset);
  3511. btrfs_set_header_nritems(leaf, nritems + 1);
  3512. /* write the data for the start of the original item */
  3513. write_extent_buffer(leaf, buf,
  3514. btrfs_item_ptr_offset(leaf, path->slots[0]),
  3515. split_offset);
  3516. /* write the data for the new item */
  3517. write_extent_buffer(leaf, buf + split_offset,
  3518. btrfs_item_ptr_offset(leaf, slot),
  3519. item_size - split_offset);
  3520. btrfs_mark_buffer_dirty(leaf);
  3521. BUG_ON(btrfs_leaf_free_space(root, leaf) < 0);
  3522. kfree(buf);
  3523. return 0;
  3524. }
  3525. /*
  3526. * This function splits a single item into two items,
  3527. * giving 'new_key' to the new item and splitting the
  3528. * old one at split_offset (from the start of the item).
  3529. *
  3530. * The path may be released by this operation. After
  3531. * the split, the path is pointing to the old item. The
  3532. * new item is going to be in the same node as the old one.
  3533. *
  3534. * Note, the item being split must be smaller enough to live alone on
  3535. * a tree block with room for one extra struct btrfs_item
  3536. *
  3537. * This allows us to split the item in place, keeping a lock on the
  3538. * leaf the entire time.
  3539. */
  3540. int btrfs_split_item(struct btrfs_trans_handle *trans,
  3541. struct btrfs_root *root,
  3542. struct btrfs_path *path,
  3543. struct btrfs_key *new_key,
  3544. unsigned long split_offset)
  3545. {
  3546. int ret;
  3547. ret = setup_leaf_for_split(trans, root, path,
  3548. sizeof(struct btrfs_item));
  3549. if (ret)
  3550. return ret;
  3551. ret = split_item(trans, root, path, new_key, split_offset);
  3552. return ret;
  3553. }
  3554. /*
  3555. * This function duplicate a item, giving 'new_key' to the new item.
  3556. * It guarantees both items live in the same tree leaf and the new item
  3557. * is contiguous with the original item.
  3558. *
  3559. * This allows us to split file extent in place, keeping a lock on the
  3560. * leaf the entire time.
  3561. */
  3562. int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
  3563. struct btrfs_root *root,
  3564. struct btrfs_path *path,
  3565. struct btrfs_key *new_key)
  3566. {
  3567. struct extent_buffer *leaf;
  3568. int ret;
  3569. u32 item_size;
  3570. leaf = path->nodes[0];
  3571. item_size = btrfs_item_size_nr(leaf, path->slots[0]);
  3572. ret = setup_leaf_for_split(trans, root, path,
  3573. item_size + sizeof(struct btrfs_item));
  3574. if (ret)
  3575. return ret;
  3576. path->slots[0]++;
  3577. setup_items_for_insert(trans, root, path, new_key, &item_size,
  3578. item_size, item_size +
  3579. sizeof(struct btrfs_item), 1);
  3580. leaf = path->nodes[0];
  3581. memcpy_extent_buffer(leaf,
  3582. btrfs_item_ptr_offset(leaf, path->slots[0]),
  3583. btrfs_item_ptr_offset(leaf, path->slots[0] - 1),
  3584. item_size);
  3585. return 0;
  3586. }
  3587. /*
  3588. * make the item pointed to by the path smaller. new_size indicates
  3589. * how small to make it, and from_end tells us if we just chop bytes
  3590. * off the end of the item or if we shift the item to chop bytes off
  3591. * the front.
  3592. */
  3593. void btrfs_truncate_item(struct btrfs_trans_handle *trans,
  3594. struct btrfs_root *root,
  3595. struct btrfs_path *path,
  3596. u32 new_size, int from_end)
  3597. {
  3598. int slot;
  3599. struct extent_buffer *leaf;
  3600. struct btrfs_item *item;
  3601. u32 nritems;
  3602. unsigned int data_end;
  3603. unsigned int old_data_start;
  3604. unsigned int old_size;
  3605. unsigned int size_diff;
  3606. int i;
  3607. struct btrfs_map_token token;
  3608. btrfs_init_map_token(&token);
  3609. leaf = path->nodes[0];
  3610. slot = path->slots[0];
  3611. old_size = btrfs_item_size_nr(leaf, slot);
  3612. if (old_size == new_size)
  3613. return;
  3614. nritems = btrfs_header_nritems(leaf);
  3615. data_end = leaf_data_end(root, leaf);
  3616. old_data_start = btrfs_item_offset_nr(leaf, slot);
  3617. size_diff = old_size - new_size;
  3618. BUG_ON(slot < 0);
  3619. BUG_ON(slot >= nritems);
  3620. /*
  3621. * item0..itemN ... dataN.offset..dataN.size .. data0.size
  3622. */
  3623. /* first correct the data pointers */
  3624. for (i = slot; i < nritems; i++) {
  3625. u32 ioff;
  3626. item = btrfs_item_nr(leaf, i);
  3627. ioff = btrfs_token_item_offset(leaf, item, &token);
  3628. btrfs_set_token_item_offset(leaf, item,
  3629. ioff + size_diff, &token);
  3630. }
  3631. /* shift the data */
  3632. if (from_end) {
  3633. memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
  3634. data_end + size_diff, btrfs_leaf_data(leaf) +
  3635. data_end, old_data_start + new_size - data_end);
  3636. } else {
  3637. struct btrfs_disk_key disk_key;
  3638. u64 offset;
  3639. btrfs_item_key(leaf, &disk_key, slot);
  3640. if (btrfs_disk_key_type(&disk_key) == BTRFS_EXTENT_DATA_KEY) {
  3641. unsigned long ptr;
  3642. struct btrfs_file_extent_item *fi;
  3643. fi = btrfs_item_ptr(leaf, slot,
  3644. struct btrfs_file_extent_item);
  3645. fi = (struct btrfs_file_extent_item *)(
  3646. (unsigned long)fi - size_diff);
  3647. if (btrfs_file_extent_type(leaf, fi) ==
  3648. BTRFS_FILE_EXTENT_INLINE) {
  3649. ptr = btrfs_item_ptr_offset(leaf, slot);
  3650. memmove_extent_buffer(leaf, ptr,
  3651. (unsigned long)fi,
  3652. offsetof(struct btrfs_file_extent_item,
  3653. disk_bytenr));
  3654. }
  3655. }
  3656. memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
  3657. data_end + size_diff, btrfs_leaf_data(leaf) +
  3658. data_end, old_data_start - data_end);
  3659. offset = btrfs_disk_key_offset(&disk_key);
  3660. btrfs_set_disk_key_offset(&disk_key, offset + size_diff);
  3661. btrfs_set_item_key(leaf, &disk_key, slot);
  3662. if (slot == 0)
  3663. fixup_low_keys(trans, root, path, &disk_key, 1);
  3664. }
  3665. item = btrfs_item_nr(leaf, slot);
  3666. btrfs_set_item_size(leaf, item, new_size);
  3667. btrfs_mark_buffer_dirty(leaf);
  3668. if (btrfs_leaf_free_space(root, leaf) < 0) {
  3669. btrfs_print_leaf(root, leaf);
  3670. BUG();
  3671. }
  3672. }
  3673. /*
  3674. * make the item pointed to by the path bigger, data_size is the new size.
  3675. */
  3676. void btrfs_extend_item(struct btrfs_trans_handle *trans,
  3677. struct btrfs_root *root, struct btrfs_path *path,
  3678. u32 data_size)
  3679. {
  3680. int slot;
  3681. struct extent_buffer *leaf;
  3682. struct btrfs_item *item;
  3683. u32 nritems;
  3684. unsigned int data_end;
  3685. unsigned int old_data;
  3686. unsigned int old_size;
  3687. int i;
  3688. struct btrfs_map_token token;
  3689. btrfs_init_map_token(&token);
  3690. leaf = path->nodes[0];
  3691. nritems = btrfs_header_nritems(leaf);
  3692. data_end = leaf_data_end(root, leaf);
  3693. if (btrfs_leaf_free_space(root, leaf) < data_size) {
  3694. btrfs_print_leaf(root, leaf);
  3695. BUG();
  3696. }
  3697. slot = path->slots[0];
  3698. old_data = btrfs_item_end_nr(leaf, slot);
  3699. BUG_ON(slot < 0);
  3700. if (slot >= nritems) {
  3701. btrfs_print_leaf(root, leaf);
  3702. printk(KERN_CRIT "slot %d too large, nritems %d\n",
  3703. slot, nritems);
  3704. BUG_ON(1);
  3705. }
  3706. /*
  3707. * item0..itemN ... dataN.offset..dataN.size .. data0.size
  3708. */
  3709. /* first correct the data pointers */
  3710. for (i = slot; i < nritems; i++) {
  3711. u32 ioff;
  3712. item = btrfs_item_nr(leaf, i);
  3713. ioff = btrfs_token_item_offset(leaf, item, &token);
  3714. btrfs_set_token_item_offset(leaf, item,
  3715. ioff - data_size, &token);
  3716. }
  3717. /* shift the data */
  3718. memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
  3719. data_end - data_size, btrfs_leaf_data(leaf) +
  3720. data_end, old_data - data_end);
  3721. data_end = old_data;
  3722. old_size = btrfs_item_size_nr(leaf, slot);
  3723. item = btrfs_item_nr(leaf, slot);
  3724. btrfs_set_item_size(leaf, item, old_size + data_size);
  3725. btrfs_mark_buffer_dirty(leaf);
  3726. if (btrfs_leaf_free_space(root, leaf) < 0) {
  3727. btrfs_print_leaf(root, leaf);
  3728. BUG();
  3729. }
  3730. }
  3731. /*
  3732. * Given a key and some data, insert items into the tree.
  3733. * This does all the path init required, making room in the tree if needed.
  3734. * Returns the number of keys that were inserted.
  3735. */
  3736. int btrfs_insert_some_items(struct btrfs_trans_handle *trans,
  3737. struct btrfs_root *root,
  3738. struct btrfs_path *path,
  3739. struct btrfs_key *cpu_key, u32 *data_size,
  3740. int nr)
  3741. {
  3742. struct extent_buffer *leaf;
  3743. struct btrfs_item *item;
  3744. int ret = 0;
  3745. int slot;
  3746. int i;
  3747. u32 nritems;
  3748. u32 total_data = 0;
  3749. u32 total_size = 0;
  3750. unsigned int data_end;
  3751. struct btrfs_disk_key disk_key;
  3752. struct btrfs_key found_key;
  3753. struct btrfs_map_token token;
  3754. btrfs_init_map_token(&token);
  3755. for (i = 0; i < nr; i++) {
  3756. if (total_size + data_size[i] + sizeof(struct btrfs_item) >
  3757. BTRFS_LEAF_DATA_SIZE(root)) {
  3758. break;
  3759. nr = i;
  3760. }
  3761. total_data += data_size[i];
  3762. total_size += data_size[i] + sizeof(struct btrfs_item);
  3763. }
  3764. BUG_ON(nr == 0);
  3765. ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
  3766. if (ret == 0)
  3767. return -EEXIST;
  3768. if (ret < 0)
  3769. goto out;
  3770. leaf = path->nodes[0];
  3771. nritems = btrfs_header_nritems(leaf);
  3772. data_end = leaf_data_end(root, leaf);
  3773. if (btrfs_leaf_free_space(root, leaf) < total_size) {
  3774. for (i = nr; i >= 0; i--) {
  3775. total_data -= data_size[i];
  3776. total_size -= data_size[i] + sizeof(struct btrfs_item);
  3777. if (total_size < btrfs_leaf_free_space(root, leaf))
  3778. break;
  3779. }
  3780. nr = i;
  3781. }
  3782. slot = path->slots[0];
  3783. BUG_ON(slot < 0);
  3784. if (slot != nritems) {
  3785. unsigned int old_data = btrfs_item_end_nr(leaf, slot);
  3786. item = btrfs_item_nr(leaf, slot);
  3787. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  3788. /* figure out how many keys we can insert in here */
  3789. total_data = data_size[0];
  3790. for (i = 1; i < nr; i++) {
  3791. if (btrfs_comp_cpu_keys(&found_key, cpu_key + i) <= 0)
  3792. break;
  3793. total_data += data_size[i];
  3794. }
  3795. nr = i;
  3796. if (old_data < data_end) {
  3797. btrfs_print_leaf(root, leaf);
  3798. printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
  3799. slot, old_data, data_end);
  3800. BUG_ON(1);
  3801. }
  3802. /*
  3803. * item0..itemN ... dataN.offset..dataN.size .. data0.size
  3804. */
  3805. /* first correct the data pointers */
  3806. for (i = slot; i < nritems; i++) {
  3807. u32 ioff;
  3808. item = btrfs_item_nr(leaf, i);
  3809. ioff = btrfs_token_item_offset(leaf, item, &token);
  3810. btrfs_set_token_item_offset(leaf, item,
  3811. ioff - total_data, &token);
  3812. }
  3813. /* shift the items */
  3814. memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
  3815. btrfs_item_nr_offset(slot),
  3816. (nritems - slot) * sizeof(struct btrfs_item));
  3817. /* shift the data */
  3818. memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
  3819. data_end - total_data, btrfs_leaf_data(leaf) +
  3820. data_end, old_data - data_end);
  3821. data_end = old_data;
  3822. } else {
  3823. /*
  3824. * this sucks but it has to be done, if we are inserting at
  3825. * the end of the leaf only insert 1 of the items, since we
  3826. * have no way of knowing whats on the next leaf and we'd have
  3827. * to drop our current locks to figure it out
  3828. */
  3829. nr = 1;
  3830. }
  3831. /* setup the item for the new data */
  3832. for (i = 0; i < nr; i++) {
  3833. btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
  3834. btrfs_set_item_key(leaf, &disk_key, slot + i);
  3835. item = btrfs_item_nr(leaf, slot + i);
  3836. btrfs_set_token_item_offset(leaf, item,
  3837. data_end - data_size[i], &token);
  3838. data_end -= data_size[i];
  3839. btrfs_set_token_item_size(leaf, item, data_size[i], &token);
  3840. }
  3841. btrfs_set_header_nritems(leaf, nritems + nr);
  3842. btrfs_mark_buffer_dirty(leaf);
  3843. ret = 0;
  3844. if (slot == 0) {
  3845. btrfs_cpu_key_to_disk(&disk_key, cpu_key);
  3846. fixup_low_keys(trans, root, path, &disk_key, 1);
  3847. }
  3848. if (btrfs_leaf_free_space(root, leaf) < 0) {
  3849. btrfs_print_leaf(root, leaf);
  3850. BUG();
  3851. }
  3852. out:
  3853. if (!ret)
  3854. ret = nr;
  3855. return ret;
  3856. }
  3857. /*
  3858. * this is a helper for btrfs_insert_empty_items, the main goal here is
  3859. * to save stack depth by doing the bulk of the work in a function
  3860. * that doesn't call btrfs_search_slot
  3861. */
  3862. void setup_items_for_insert(struct btrfs_trans_handle *trans,
  3863. struct btrfs_root *root, struct btrfs_path *path,
  3864. struct btrfs_key *cpu_key, u32 *data_size,
  3865. u32 total_data, u32 total_size, int nr)
  3866. {
  3867. struct btrfs_item *item;
  3868. int i;
  3869. u32 nritems;
  3870. unsigned int data_end;
  3871. struct btrfs_disk_key disk_key;
  3872. struct extent_buffer *leaf;
  3873. int slot;
  3874. struct btrfs_map_token token;
  3875. btrfs_init_map_token(&token);
  3876. leaf = path->nodes[0];
  3877. slot = path->slots[0];
  3878. nritems = btrfs_header_nritems(leaf);
  3879. data_end = leaf_data_end(root, leaf);
  3880. if (btrfs_leaf_free_space(root, leaf) < total_size) {
  3881. btrfs_print_leaf(root, leaf);
  3882. printk(KERN_CRIT "not enough freespace need %u have %d\n",
  3883. total_size, btrfs_leaf_free_space(root, leaf));
  3884. BUG();
  3885. }
  3886. if (slot != nritems) {
  3887. unsigned int old_data = btrfs_item_end_nr(leaf, slot);
  3888. if (old_data < data_end) {
  3889. btrfs_print_leaf(root, leaf);
  3890. printk(KERN_CRIT "slot %d old_data %d data_end %d\n",
  3891. slot, old_data, data_end);
  3892. BUG_ON(1);
  3893. }
  3894. /*
  3895. * item0..itemN ... dataN.offset..dataN.size .. data0.size
  3896. */
  3897. /* first correct the data pointers */
  3898. for (i = slot; i < nritems; i++) {
  3899. u32 ioff;
  3900. item = btrfs_item_nr(leaf, i);
  3901. ioff = btrfs_token_item_offset(leaf, item, &token);
  3902. btrfs_set_token_item_offset(leaf, item,
  3903. ioff - total_data, &token);
  3904. }
  3905. /* shift the items */
  3906. memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot + nr),
  3907. btrfs_item_nr_offset(slot),
  3908. (nritems - slot) * sizeof(struct btrfs_item));
  3909. /* shift the data */
  3910. memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
  3911. data_end - total_data, btrfs_leaf_data(leaf) +
  3912. data_end, old_data - data_end);
  3913. data_end = old_data;
  3914. }
  3915. /* setup the item for the new data */
  3916. for (i = 0; i < nr; i++) {
  3917. btrfs_cpu_key_to_disk(&disk_key, cpu_key + i);
  3918. btrfs_set_item_key(leaf, &disk_key, slot + i);
  3919. item = btrfs_item_nr(leaf, slot + i);
  3920. btrfs_set_token_item_offset(leaf, item,
  3921. data_end - data_size[i], &token);
  3922. data_end -= data_size[i];
  3923. btrfs_set_token_item_size(leaf, item, data_size[i], &token);
  3924. }
  3925. btrfs_set_header_nritems(leaf, nritems + nr);
  3926. if (slot == 0) {
  3927. btrfs_cpu_key_to_disk(&disk_key, cpu_key);
  3928. fixup_low_keys(trans, root, path, &disk_key, 1);
  3929. }
  3930. btrfs_unlock_up_safe(path, 1);
  3931. btrfs_mark_buffer_dirty(leaf);
  3932. if (btrfs_leaf_free_space(root, leaf) < 0) {
  3933. btrfs_print_leaf(root, leaf);
  3934. BUG();
  3935. }
  3936. }
  3937. /*
  3938. * Given a key and some data, insert items into the tree.
  3939. * This does all the path init required, making room in the tree if needed.
  3940. */
  3941. int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
  3942. struct btrfs_root *root,
  3943. struct btrfs_path *path,
  3944. struct btrfs_key *cpu_key, u32 *data_size,
  3945. int nr)
  3946. {
  3947. int ret = 0;
  3948. int slot;
  3949. int i;
  3950. u32 total_size = 0;
  3951. u32 total_data = 0;
  3952. for (i = 0; i < nr; i++)
  3953. total_data += data_size[i];
  3954. total_size = total_data + (nr * sizeof(struct btrfs_item));
  3955. ret = btrfs_search_slot(trans, root, cpu_key, path, total_size, 1);
  3956. if (ret == 0)
  3957. return -EEXIST;
  3958. if (ret < 0)
  3959. return ret;
  3960. slot = path->slots[0];
  3961. BUG_ON(slot < 0);
  3962. setup_items_for_insert(trans, root, path, cpu_key, data_size,
  3963. total_data, total_size, nr);
  3964. return 0;
  3965. }
  3966. /*
  3967. * Given a key and some data, insert an item into the tree.
  3968. * This does all the path init required, making room in the tree if needed.
  3969. */
  3970. int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root
  3971. *root, struct btrfs_key *cpu_key, void *data, u32
  3972. data_size)
  3973. {
  3974. int ret = 0;
  3975. struct btrfs_path *path;
  3976. struct extent_buffer *leaf;
  3977. unsigned long ptr;
  3978. path = btrfs_alloc_path();
  3979. if (!path)
  3980. return -ENOMEM;
  3981. ret = btrfs_insert_empty_item(trans, root, path, cpu_key, data_size);
  3982. if (!ret) {
  3983. leaf = path->nodes[0];
  3984. ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
  3985. write_extent_buffer(leaf, data, ptr, data_size);
  3986. btrfs_mark_buffer_dirty(leaf);
  3987. }
  3988. btrfs_free_path(path);
  3989. return ret;
  3990. }
  3991. /*
  3992. * delete the pointer from a given node.
  3993. *
  3994. * the tree should have been previously balanced so the deletion does not
  3995. * empty a node.
  3996. */
  3997. static void del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  3998. struct btrfs_path *path, int level, int slot,
  3999. int tree_mod_log)
  4000. {
  4001. struct extent_buffer *parent = path->nodes[level];
  4002. u32 nritems;
  4003. int ret;
  4004. nritems = btrfs_header_nritems(parent);
  4005. if (slot != nritems - 1) {
  4006. if (tree_mod_log && level)
  4007. tree_mod_log_eb_move(root->fs_info, parent, slot,
  4008. slot + 1, nritems - slot - 1);
  4009. memmove_extent_buffer(parent,
  4010. btrfs_node_key_ptr_offset(slot),
  4011. btrfs_node_key_ptr_offset(slot + 1),
  4012. sizeof(struct btrfs_key_ptr) *
  4013. (nritems - slot - 1));
  4014. } else if (tree_mod_log && level) {
  4015. ret = tree_mod_log_insert_key(root->fs_info, parent, slot,
  4016. MOD_LOG_KEY_REMOVE);
  4017. BUG_ON(ret < 0);
  4018. }
  4019. nritems--;
  4020. btrfs_set_header_nritems(parent, nritems);
  4021. if (nritems == 0 && parent == root->node) {
  4022. BUG_ON(btrfs_header_level(root->node) != 1);
  4023. /* just turn the root into a leaf and break */
  4024. btrfs_set_header_level(root->node, 0);
  4025. } else if (slot == 0) {
  4026. struct btrfs_disk_key disk_key;
  4027. btrfs_node_key(parent, &disk_key, 0);
  4028. fixup_low_keys(trans, root, path, &disk_key, level + 1);
  4029. }
  4030. btrfs_mark_buffer_dirty(parent);
  4031. }
  4032. /*
  4033. * a helper function to delete the leaf pointed to by path->slots[1] and
  4034. * path->nodes[1].
  4035. *
  4036. * This deletes the pointer in path->nodes[1] and frees the leaf
  4037. * block extent. zero is returned if it all worked out, < 0 otherwise.
  4038. *
  4039. * The path must have already been setup for deleting the leaf, including
  4040. * all the proper balancing. path->nodes[1] must be locked.
  4041. */
  4042. static noinline void btrfs_del_leaf(struct btrfs_trans_handle *trans,
  4043. struct btrfs_root *root,
  4044. struct btrfs_path *path,
  4045. struct extent_buffer *leaf)
  4046. {
  4047. WARN_ON(btrfs_header_generation(leaf) != trans->transid);
  4048. del_ptr(trans, root, path, 1, path->slots[1], 1);
  4049. /*
  4050. * btrfs_free_extent is expensive, we want to make sure we
  4051. * aren't holding any locks when we call it
  4052. */
  4053. btrfs_unlock_up_safe(path, 0);
  4054. root_sub_used(root, leaf->len);
  4055. extent_buffer_get(leaf);
  4056. btrfs_free_tree_block(trans, root, leaf, 0, 1);
  4057. free_extent_buffer_stale(leaf);
  4058. }
  4059. /*
  4060. * delete the item at the leaf level in path. If that empties
  4061. * the leaf, remove it from the tree
  4062. */
  4063. int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  4064. struct btrfs_path *path, int slot, int nr)
  4065. {
  4066. struct extent_buffer *leaf;
  4067. struct btrfs_item *item;
  4068. int last_off;
  4069. int dsize = 0;
  4070. int ret = 0;
  4071. int wret;
  4072. int i;
  4073. u32 nritems;
  4074. struct btrfs_map_token token;
  4075. btrfs_init_map_token(&token);
  4076. leaf = path->nodes[0];
  4077. last_off = btrfs_item_offset_nr(leaf, slot + nr - 1);
  4078. for (i = 0; i < nr; i++)
  4079. dsize += btrfs_item_size_nr(leaf, slot + i);
  4080. nritems = btrfs_header_nritems(leaf);
  4081. if (slot + nr != nritems) {
  4082. int data_end = leaf_data_end(root, leaf);
  4083. memmove_extent_buffer(leaf, btrfs_leaf_data(leaf) +
  4084. data_end + dsize,
  4085. btrfs_leaf_data(leaf) + data_end,
  4086. last_off - data_end);
  4087. for (i = slot + nr; i < nritems; i++) {
  4088. u32 ioff;
  4089. item = btrfs_item_nr(leaf, i);
  4090. ioff = btrfs_token_item_offset(leaf, item, &token);
  4091. btrfs_set_token_item_offset(leaf, item,
  4092. ioff + dsize, &token);
  4093. }
  4094. memmove_extent_buffer(leaf, btrfs_item_nr_offset(slot),
  4095. btrfs_item_nr_offset(slot + nr),
  4096. sizeof(struct btrfs_item) *
  4097. (nritems - slot - nr));
  4098. }
  4099. btrfs_set_header_nritems(leaf, nritems - nr);
  4100. nritems -= nr;
  4101. /* delete the leaf if we've emptied it */
  4102. if (nritems == 0) {
  4103. if (leaf == root->node) {
  4104. btrfs_set_header_level(leaf, 0);
  4105. } else {
  4106. btrfs_set_path_blocking(path);
  4107. clean_tree_block(trans, root, leaf);
  4108. btrfs_del_leaf(trans, root, path, leaf);
  4109. }
  4110. } else {
  4111. int used = leaf_space_used(leaf, 0, nritems);
  4112. if (slot == 0) {
  4113. struct btrfs_disk_key disk_key;
  4114. btrfs_item_key(leaf, &disk_key, 0);
  4115. fixup_low_keys(trans, root, path, &disk_key, 1);
  4116. }
  4117. /* delete the leaf if it is mostly empty */
  4118. if (used < BTRFS_LEAF_DATA_SIZE(root) / 3) {
  4119. /* push_leaf_left fixes the path.
  4120. * make sure the path still points to our leaf
  4121. * for possible call to del_ptr below
  4122. */
  4123. slot = path->slots[1];
  4124. extent_buffer_get(leaf);
  4125. btrfs_set_path_blocking(path);
  4126. wret = push_leaf_left(trans, root, path, 1, 1,
  4127. 1, (u32)-1);
  4128. if (wret < 0 && wret != -ENOSPC)
  4129. ret = wret;
  4130. if (path->nodes[0] == leaf &&
  4131. btrfs_header_nritems(leaf)) {
  4132. wret = push_leaf_right(trans, root, path, 1,
  4133. 1, 1, 0);
  4134. if (wret < 0 && wret != -ENOSPC)
  4135. ret = wret;
  4136. }
  4137. if (btrfs_header_nritems(leaf) == 0) {
  4138. path->slots[1] = slot;
  4139. btrfs_del_leaf(trans, root, path, leaf);
  4140. free_extent_buffer(leaf);
  4141. ret = 0;
  4142. } else {
  4143. /* if we're still in the path, make sure
  4144. * we're dirty. Otherwise, one of the
  4145. * push_leaf functions must have already
  4146. * dirtied this buffer
  4147. */
  4148. if (path->nodes[0] == leaf)
  4149. btrfs_mark_buffer_dirty(leaf);
  4150. free_extent_buffer(leaf);
  4151. }
  4152. } else {
  4153. btrfs_mark_buffer_dirty(leaf);
  4154. }
  4155. }
  4156. return ret;
  4157. }
  4158. /*
  4159. * search the tree again to find a leaf with lesser keys
  4160. * returns 0 if it found something or 1 if there are no lesser leaves.
  4161. * returns < 0 on io errors.
  4162. *
  4163. * This may release the path, and so you may lose any locks held at the
  4164. * time you call it.
  4165. */
  4166. int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path)
  4167. {
  4168. struct btrfs_key key;
  4169. struct btrfs_disk_key found_key;
  4170. int ret;
  4171. btrfs_item_key_to_cpu(path->nodes[0], &key, 0);
  4172. if (key.offset > 0)
  4173. key.offset--;
  4174. else if (key.type > 0)
  4175. key.type--;
  4176. else if (key.objectid > 0)
  4177. key.objectid--;
  4178. else
  4179. return 1;
  4180. btrfs_release_path(path);
  4181. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4182. if (ret < 0)
  4183. return ret;
  4184. btrfs_item_key(path->nodes[0], &found_key, 0);
  4185. ret = comp_keys(&found_key, &key);
  4186. if (ret < 0)
  4187. return 0;
  4188. return 1;
  4189. }
  4190. /*
  4191. * A helper function to walk down the tree starting at min_key, and looking
  4192. * for nodes or leaves that are either in cache or have a minimum
  4193. * transaction id. This is used by the btree defrag code, and tree logging
  4194. *
  4195. * This does not cow, but it does stuff the starting key it finds back
  4196. * into min_key, so you can call btrfs_search_slot with cow=1 on the
  4197. * key and get a writable path.
  4198. *
  4199. * This does lock as it descends, and path->keep_locks should be set
  4200. * to 1 by the caller.
  4201. *
  4202. * This honors path->lowest_level to prevent descent past a given level
  4203. * of the tree.
  4204. *
  4205. * min_trans indicates the oldest transaction that you are interested
  4206. * in walking through. Any nodes or leaves older than min_trans are
  4207. * skipped over (without reading them).
  4208. *
  4209. * returns zero if something useful was found, < 0 on error and 1 if there
  4210. * was nothing in the tree that matched the search criteria.
  4211. */
  4212. int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
  4213. struct btrfs_key *max_key,
  4214. struct btrfs_path *path, int cache_only,
  4215. u64 min_trans)
  4216. {
  4217. struct extent_buffer *cur;
  4218. struct btrfs_key found_key;
  4219. int slot;
  4220. int sret;
  4221. u32 nritems;
  4222. int level;
  4223. int ret = 1;
  4224. WARN_ON(!path->keep_locks);
  4225. again:
  4226. cur = btrfs_read_lock_root_node(root);
  4227. level = btrfs_header_level(cur);
  4228. WARN_ON(path->nodes[level]);
  4229. path->nodes[level] = cur;
  4230. path->locks[level] = BTRFS_READ_LOCK;
  4231. if (btrfs_header_generation(cur) < min_trans) {
  4232. ret = 1;
  4233. goto out;
  4234. }
  4235. while (1) {
  4236. nritems = btrfs_header_nritems(cur);
  4237. level = btrfs_header_level(cur);
  4238. sret = bin_search(cur, min_key, level, &slot);
  4239. /* at the lowest level, we're done, setup the path and exit */
  4240. if (level == path->lowest_level) {
  4241. if (slot >= nritems)
  4242. goto find_next_key;
  4243. ret = 0;
  4244. path->slots[level] = slot;
  4245. btrfs_item_key_to_cpu(cur, &found_key, slot);
  4246. goto out;
  4247. }
  4248. if (sret && slot > 0)
  4249. slot--;
  4250. /*
  4251. * check this node pointer against the cache_only and
  4252. * min_trans parameters. If it isn't in cache or is too
  4253. * old, skip to the next one.
  4254. */
  4255. while (slot < nritems) {
  4256. u64 blockptr;
  4257. u64 gen;
  4258. struct extent_buffer *tmp;
  4259. struct btrfs_disk_key disk_key;
  4260. blockptr = btrfs_node_blockptr(cur, slot);
  4261. gen = btrfs_node_ptr_generation(cur, slot);
  4262. if (gen < min_trans) {
  4263. slot++;
  4264. continue;
  4265. }
  4266. if (!cache_only)
  4267. break;
  4268. if (max_key) {
  4269. btrfs_node_key(cur, &disk_key, slot);
  4270. if (comp_keys(&disk_key, max_key) >= 0) {
  4271. ret = 1;
  4272. goto out;
  4273. }
  4274. }
  4275. tmp = btrfs_find_tree_block(root, blockptr,
  4276. btrfs_level_size(root, level - 1));
  4277. if (tmp && btrfs_buffer_uptodate(tmp, gen, 1) > 0) {
  4278. free_extent_buffer(tmp);
  4279. break;
  4280. }
  4281. if (tmp)
  4282. free_extent_buffer(tmp);
  4283. slot++;
  4284. }
  4285. find_next_key:
  4286. /*
  4287. * we didn't find a candidate key in this node, walk forward
  4288. * and find another one
  4289. */
  4290. if (slot >= nritems) {
  4291. path->slots[level] = slot;
  4292. btrfs_set_path_blocking(path);
  4293. sret = btrfs_find_next_key(root, path, min_key, level,
  4294. cache_only, min_trans);
  4295. if (sret == 0) {
  4296. btrfs_release_path(path);
  4297. goto again;
  4298. } else {
  4299. goto out;
  4300. }
  4301. }
  4302. /* save our key for returning back */
  4303. btrfs_node_key_to_cpu(cur, &found_key, slot);
  4304. path->slots[level] = slot;
  4305. if (level == path->lowest_level) {
  4306. ret = 0;
  4307. unlock_up(path, level, 1, 0, NULL);
  4308. goto out;
  4309. }
  4310. btrfs_set_path_blocking(path);
  4311. cur = read_node_slot(root, cur, slot);
  4312. BUG_ON(!cur); /* -ENOMEM */
  4313. btrfs_tree_read_lock(cur);
  4314. path->locks[level - 1] = BTRFS_READ_LOCK;
  4315. path->nodes[level - 1] = cur;
  4316. unlock_up(path, level, 1, 0, NULL);
  4317. btrfs_clear_path_blocking(path, NULL, 0);
  4318. }
  4319. out:
  4320. if (ret == 0)
  4321. memcpy(min_key, &found_key, sizeof(found_key));
  4322. btrfs_set_path_blocking(path);
  4323. return ret;
  4324. }
  4325. /*
  4326. * this is similar to btrfs_next_leaf, but does not try to preserve
  4327. * and fixup the path. It looks for and returns the next key in the
  4328. * tree based on the current path and the cache_only and min_trans
  4329. * parameters.
  4330. *
  4331. * 0 is returned if another key is found, < 0 if there are any errors
  4332. * and 1 is returned if there are no higher keys in the tree
  4333. *
  4334. * path->keep_locks should be set to 1 on the search made before
  4335. * calling this function.
  4336. */
  4337. int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
  4338. struct btrfs_key *key, int level,
  4339. int cache_only, u64 min_trans)
  4340. {
  4341. int slot;
  4342. struct extent_buffer *c;
  4343. WARN_ON(!path->keep_locks);
  4344. while (level < BTRFS_MAX_LEVEL) {
  4345. if (!path->nodes[level])
  4346. return 1;
  4347. slot = path->slots[level] + 1;
  4348. c = path->nodes[level];
  4349. next:
  4350. if (slot >= btrfs_header_nritems(c)) {
  4351. int ret;
  4352. int orig_lowest;
  4353. struct btrfs_key cur_key;
  4354. if (level + 1 >= BTRFS_MAX_LEVEL ||
  4355. !path->nodes[level + 1])
  4356. return 1;
  4357. if (path->locks[level + 1]) {
  4358. level++;
  4359. continue;
  4360. }
  4361. slot = btrfs_header_nritems(c) - 1;
  4362. if (level == 0)
  4363. btrfs_item_key_to_cpu(c, &cur_key, slot);
  4364. else
  4365. btrfs_node_key_to_cpu(c, &cur_key, slot);
  4366. orig_lowest = path->lowest_level;
  4367. btrfs_release_path(path);
  4368. path->lowest_level = level;
  4369. ret = btrfs_search_slot(NULL, root, &cur_key, path,
  4370. 0, 0);
  4371. path->lowest_level = orig_lowest;
  4372. if (ret < 0)
  4373. return ret;
  4374. c = path->nodes[level];
  4375. slot = path->slots[level];
  4376. if (ret == 0)
  4377. slot++;
  4378. goto next;
  4379. }
  4380. if (level == 0)
  4381. btrfs_item_key_to_cpu(c, key, slot);
  4382. else {
  4383. u64 blockptr = btrfs_node_blockptr(c, slot);
  4384. u64 gen = btrfs_node_ptr_generation(c, slot);
  4385. if (cache_only) {
  4386. struct extent_buffer *cur;
  4387. cur = btrfs_find_tree_block(root, blockptr,
  4388. btrfs_level_size(root, level - 1));
  4389. if (!cur ||
  4390. btrfs_buffer_uptodate(cur, gen, 1) <= 0) {
  4391. slot++;
  4392. if (cur)
  4393. free_extent_buffer(cur);
  4394. goto next;
  4395. }
  4396. free_extent_buffer(cur);
  4397. }
  4398. if (gen < min_trans) {
  4399. slot++;
  4400. goto next;
  4401. }
  4402. btrfs_node_key_to_cpu(c, key, slot);
  4403. }
  4404. return 0;
  4405. }
  4406. return 1;
  4407. }
  4408. /*
  4409. * search the tree again to find a leaf with greater keys
  4410. * returns 0 if it found something or 1 if there are no greater leaves.
  4411. * returns < 0 on io errors.
  4412. */
  4413. int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
  4414. {
  4415. int slot;
  4416. int level;
  4417. struct extent_buffer *c;
  4418. struct extent_buffer *next;
  4419. struct btrfs_key key;
  4420. u32 nritems;
  4421. int ret;
  4422. int old_spinning = path->leave_spinning;
  4423. int next_rw_lock = 0;
  4424. nritems = btrfs_header_nritems(path->nodes[0]);
  4425. if (nritems == 0)
  4426. return 1;
  4427. btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
  4428. again:
  4429. level = 1;
  4430. next = NULL;
  4431. next_rw_lock = 0;
  4432. btrfs_release_path(path);
  4433. path->keep_locks = 1;
  4434. path->leave_spinning = 1;
  4435. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  4436. path->keep_locks = 0;
  4437. if (ret < 0)
  4438. return ret;
  4439. nritems = btrfs_header_nritems(path->nodes[0]);
  4440. /*
  4441. * by releasing the path above we dropped all our locks. A balance
  4442. * could have added more items next to the key that used to be
  4443. * at the very end of the block. So, check again here and
  4444. * advance the path if there are now more items available.
  4445. */
  4446. if (nritems > 0 && path->slots[0] < nritems - 1) {
  4447. if (ret == 0)
  4448. path->slots[0]++;
  4449. ret = 0;
  4450. goto done;
  4451. }
  4452. while (level < BTRFS_MAX_LEVEL) {
  4453. if (!path->nodes[level]) {
  4454. ret = 1;
  4455. goto done;
  4456. }
  4457. slot = path->slots[level] + 1;
  4458. c = path->nodes[level];
  4459. if (slot >= btrfs_header_nritems(c)) {
  4460. level++;
  4461. if (level == BTRFS_MAX_LEVEL) {
  4462. ret = 1;
  4463. goto done;
  4464. }
  4465. continue;
  4466. }
  4467. if (next) {
  4468. btrfs_tree_unlock_rw(next, next_rw_lock);
  4469. free_extent_buffer(next);
  4470. }
  4471. next = c;
  4472. next_rw_lock = path->locks[level];
  4473. ret = read_block_for_search(NULL, root, path, &next, level,
  4474. slot, &key, 0);
  4475. if (ret == -EAGAIN)
  4476. goto again;
  4477. if (ret < 0) {
  4478. btrfs_release_path(path);
  4479. goto done;
  4480. }
  4481. if (!path->skip_locking) {
  4482. ret = btrfs_try_tree_read_lock(next);
  4483. if (!ret) {
  4484. btrfs_set_path_blocking(path);
  4485. btrfs_tree_read_lock(next);
  4486. btrfs_clear_path_blocking(path, next,
  4487. BTRFS_READ_LOCK);
  4488. }
  4489. next_rw_lock = BTRFS_READ_LOCK;
  4490. }
  4491. break;
  4492. }
  4493. path->slots[level] = slot;
  4494. while (1) {
  4495. level--;
  4496. c = path->nodes[level];
  4497. if (path->locks[level])
  4498. btrfs_tree_unlock_rw(c, path->locks[level]);
  4499. free_extent_buffer(c);
  4500. path->nodes[level] = next;
  4501. path->slots[level] = 0;
  4502. if (!path->skip_locking)
  4503. path->locks[level] = next_rw_lock;
  4504. if (!level)
  4505. break;
  4506. ret = read_block_for_search(NULL, root, path, &next, level,
  4507. 0, &key, 0);
  4508. if (ret == -EAGAIN)
  4509. goto again;
  4510. if (ret < 0) {
  4511. btrfs_release_path(path);
  4512. goto done;
  4513. }
  4514. if (!path->skip_locking) {
  4515. ret = btrfs_try_tree_read_lock(next);
  4516. if (!ret) {
  4517. btrfs_set_path_blocking(path);
  4518. btrfs_tree_read_lock(next);
  4519. btrfs_clear_path_blocking(path, next,
  4520. BTRFS_READ_LOCK);
  4521. }
  4522. next_rw_lock = BTRFS_READ_LOCK;
  4523. }
  4524. }
  4525. ret = 0;
  4526. done:
  4527. unlock_up(path, 0, 1, 0, NULL);
  4528. path->leave_spinning = old_spinning;
  4529. if (!old_spinning)
  4530. btrfs_set_path_blocking(path);
  4531. return ret;
  4532. }
  4533. /*
  4534. * this uses btrfs_prev_leaf to walk backwards in the tree, and keeps
  4535. * searching until it gets past min_objectid or finds an item of 'type'
  4536. *
  4537. * returns 0 if something is found, 1 if nothing was found and < 0 on error
  4538. */
  4539. int btrfs_previous_item(struct btrfs_root *root,
  4540. struct btrfs_path *path, u64 min_objectid,
  4541. int type)
  4542. {
  4543. struct btrfs_key found_key;
  4544. struct extent_buffer *leaf;
  4545. u32 nritems;
  4546. int ret;
  4547. while (1) {
  4548. if (path->slots[0] == 0) {
  4549. btrfs_set_path_blocking(path);
  4550. ret = btrfs_prev_leaf(root, path);
  4551. if (ret != 0)
  4552. return ret;
  4553. } else {
  4554. path->slots[0]--;
  4555. }
  4556. leaf = path->nodes[0];
  4557. nritems = btrfs_header_nritems(leaf);
  4558. if (nritems == 0)
  4559. return 1;
  4560. if (path->slots[0] == nritems)
  4561. path->slots[0]--;
  4562. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  4563. if (found_key.objectid < min_objectid)
  4564. break;
  4565. if (found_key.type == type)
  4566. return 0;
  4567. if (found_key.objectid == min_objectid &&
  4568. found_key.type < type)
  4569. break;
  4570. }
  4571. return 1;
  4572. }