ctree.c 110 KB

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