ctree.c 112 KB

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