ctree.c 134 KB

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