ctree.c 106 KB

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