ctree.c 110 KB

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