ctree.c 115 KB

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