ctree.c 96 KB

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