ctree.c 101 KB

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