ctree.c 115 KB

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