xfs_btree.c 94 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680
  1. /*
  2. * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_types.h"
  21. #include "xfs_bit.h"
  22. #include "xfs_log.h"
  23. #include "xfs_inum.h"
  24. #include "xfs_trans.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_mount.h"
  28. #include "xfs_bmap_btree.h"
  29. #include "xfs_alloc_btree.h"
  30. #include "xfs_ialloc_btree.h"
  31. #include "xfs_dinode.h"
  32. #include "xfs_inode.h"
  33. #include "xfs_inode_item.h"
  34. #include "xfs_btree.h"
  35. #include "xfs_btree_trace.h"
  36. #include "xfs_error.h"
  37. #include "xfs_trace.h"
  38. /*
  39. * Cursor allocation zone.
  40. */
  41. kmem_zone_t *xfs_btree_cur_zone;
  42. /*
  43. * Btree magic numbers.
  44. */
  45. const __uint32_t xfs_magics[XFS_BTNUM_MAX] = {
  46. XFS_ABTB_MAGIC, XFS_ABTC_MAGIC, XFS_BMAP_MAGIC, XFS_IBT_MAGIC
  47. };
  48. STATIC int /* error (0 or EFSCORRUPTED) */
  49. xfs_btree_check_lblock(
  50. struct xfs_btree_cur *cur, /* btree cursor */
  51. struct xfs_btree_block *block, /* btree long form block pointer */
  52. int level, /* level of the btree block */
  53. struct xfs_buf *bp) /* buffer for block, if any */
  54. {
  55. int lblock_ok; /* block passes checks */
  56. struct xfs_mount *mp; /* file system mount point */
  57. mp = cur->bc_mp;
  58. lblock_ok =
  59. be32_to_cpu(block->bb_magic) == xfs_magics[cur->bc_btnum] &&
  60. be16_to_cpu(block->bb_level) == level &&
  61. be16_to_cpu(block->bb_numrecs) <=
  62. cur->bc_ops->get_maxrecs(cur, level) &&
  63. block->bb_u.l.bb_leftsib &&
  64. (be64_to_cpu(block->bb_u.l.bb_leftsib) == NULLDFSBNO ||
  65. XFS_FSB_SANITY_CHECK(mp,
  66. be64_to_cpu(block->bb_u.l.bb_leftsib))) &&
  67. block->bb_u.l.bb_rightsib &&
  68. (be64_to_cpu(block->bb_u.l.bb_rightsib) == NULLDFSBNO ||
  69. XFS_FSB_SANITY_CHECK(mp,
  70. be64_to_cpu(block->bb_u.l.bb_rightsib)));
  71. if (unlikely(XFS_TEST_ERROR(!lblock_ok, mp,
  72. XFS_ERRTAG_BTREE_CHECK_LBLOCK,
  73. XFS_RANDOM_BTREE_CHECK_LBLOCK))) {
  74. if (bp)
  75. trace_xfs_btree_corrupt(bp, _RET_IP_);
  76. XFS_ERROR_REPORT("xfs_btree_check_lblock", XFS_ERRLEVEL_LOW,
  77. mp);
  78. return XFS_ERROR(EFSCORRUPTED);
  79. }
  80. return 0;
  81. }
  82. STATIC int /* error (0 or EFSCORRUPTED) */
  83. xfs_btree_check_sblock(
  84. struct xfs_btree_cur *cur, /* btree cursor */
  85. struct xfs_btree_block *block, /* btree short form block pointer */
  86. int level, /* level of the btree block */
  87. struct xfs_buf *bp) /* buffer containing block */
  88. {
  89. struct xfs_buf *agbp; /* buffer for ag. freespace struct */
  90. struct xfs_agf *agf; /* ag. freespace structure */
  91. xfs_agblock_t agflen; /* native ag. freespace length */
  92. int sblock_ok; /* block passes checks */
  93. agbp = cur->bc_private.a.agbp;
  94. agf = XFS_BUF_TO_AGF(agbp);
  95. agflen = be32_to_cpu(agf->agf_length);
  96. sblock_ok =
  97. be32_to_cpu(block->bb_magic) == xfs_magics[cur->bc_btnum] &&
  98. be16_to_cpu(block->bb_level) == level &&
  99. be16_to_cpu(block->bb_numrecs) <=
  100. cur->bc_ops->get_maxrecs(cur, level) &&
  101. (be32_to_cpu(block->bb_u.s.bb_leftsib) == NULLAGBLOCK ||
  102. be32_to_cpu(block->bb_u.s.bb_leftsib) < agflen) &&
  103. block->bb_u.s.bb_leftsib &&
  104. (be32_to_cpu(block->bb_u.s.bb_rightsib) == NULLAGBLOCK ||
  105. be32_to_cpu(block->bb_u.s.bb_rightsib) < agflen) &&
  106. block->bb_u.s.bb_rightsib;
  107. if (unlikely(XFS_TEST_ERROR(!sblock_ok, cur->bc_mp,
  108. XFS_ERRTAG_BTREE_CHECK_SBLOCK,
  109. XFS_RANDOM_BTREE_CHECK_SBLOCK))) {
  110. if (bp)
  111. trace_xfs_btree_corrupt(bp, _RET_IP_);
  112. XFS_CORRUPTION_ERROR("xfs_btree_check_sblock",
  113. XFS_ERRLEVEL_LOW, cur->bc_mp, block);
  114. return XFS_ERROR(EFSCORRUPTED);
  115. }
  116. return 0;
  117. }
  118. /*
  119. * Debug routine: check that block header is ok.
  120. */
  121. int
  122. xfs_btree_check_block(
  123. struct xfs_btree_cur *cur, /* btree cursor */
  124. struct xfs_btree_block *block, /* generic btree block pointer */
  125. int level, /* level of the btree block */
  126. struct xfs_buf *bp) /* buffer containing block, if any */
  127. {
  128. if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
  129. return xfs_btree_check_lblock(cur, block, level, bp);
  130. else
  131. return xfs_btree_check_sblock(cur, block, level, bp);
  132. }
  133. /*
  134. * Check that (long) pointer is ok.
  135. */
  136. int /* error (0 or EFSCORRUPTED) */
  137. xfs_btree_check_lptr(
  138. struct xfs_btree_cur *cur, /* btree cursor */
  139. xfs_dfsbno_t bno, /* btree block disk address */
  140. int level) /* btree block level */
  141. {
  142. XFS_WANT_CORRUPTED_RETURN(
  143. level > 0 &&
  144. bno != NULLDFSBNO &&
  145. XFS_FSB_SANITY_CHECK(cur->bc_mp, bno));
  146. return 0;
  147. }
  148. #ifdef DEBUG
  149. /*
  150. * Check that (short) pointer is ok.
  151. */
  152. STATIC int /* error (0 or EFSCORRUPTED) */
  153. xfs_btree_check_sptr(
  154. struct xfs_btree_cur *cur, /* btree cursor */
  155. xfs_agblock_t bno, /* btree block disk address */
  156. int level) /* btree block level */
  157. {
  158. xfs_agblock_t agblocks = cur->bc_mp->m_sb.sb_agblocks;
  159. XFS_WANT_CORRUPTED_RETURN(
  160. level > 0 &&
  161. bno != NULLAGBLOCK &&
  162. bno != 0 &&
  163. bno < agblocks);
  164. return 0;
  165. }
  166. /*
  167. * Check that block ptr is ok.
  168. */
  169. STATIC int /* error (0 or EFSCORRUPTED) */
  170. xfs_btree_check_ptr(
  171. struct xfs_btree_cur *cur, /* btree cursor */
  172. union xfs_btree_ptr *ptr, /* btree block disk address */
  173. int index, /* offset from ptr to check */
  174. int level) /* btree block level */
  175. {
  176. if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
  177. return xfs_btree_check_lptr(cur,
  178. be64_to_cpu((&ptr->l)[index]), level);
  179. } else {
  180. return xfs_btree_check_sptr(cur,
  181. be32_to_cpu((&ptr->s)[index]), level);
  182. }
  183. }
  184. #endif
  185. /*
  186. * Delete the btree cursor.
  187. */
  188. void
  189. xfs_btree_del_cursor(
  190. xfs_btree_cur_t *cur, /* btree cursor */
  191. int error) /* del because of error */
  192. {
  193. int i; /* btree level */
  194. /*
  195. * Clear the buffer pointers, and release the buffers.
  196. * If we're doing this in the face of an error, we
  197. * need to make sure to inspect all of the entries
  198. * in the bc_bufs array for buffers to be unlocked.
  199. * This is because some of the btree code works from
  200. * level n down to 0, and if we get an error along
  201. * the way we won't have initialized all the entries
  202. * down to 0.
  203. */
  204. for (i = 0; i < cur->bc_nlevels; i++) {
  205. if (cur->bc_bufs[i])
  206. xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[i]);
  207. else if (!error)
  208. break;
  209. }
  210. /*
  211. * Can't free a bmap cursor without having dealt with the
  212. * allocated indirect blocks' accounting.
  213. */
  214. ASSERT(cur->bc_btnum != XFS_BTNUM_BMAP ||
  215. cur->bc_private.b.allocated == 0);
  216. /*
  217. * Free the cursor.
  218. */
  219. kmem_zone_free(xfs_btree_cur_zone, cur);
  220. }
  221. /*
  222. * Duplicate the btree cursor.
  223. * Allocate a new one, copy the record, re-get the buffers.
  224. */
  225. int /* error */
  226. xfs_btree_dup_cursor(
  227. xfs_btree_cur_t *cur, /* input cursor */
  228. xfs_btree_cur_t **ncur) /* output cursor */
  229. {
  230. xfs_buf_t *bp; /* btree block's buffer pointer */
  231. int error; /* error return value */
  232. int i; /* level number of btree block */
  233. xfs_mount_t *mp; /* mount structure for filesystem */
  234. xfs_btree_cur_t *new; /* new cursor value */
  235. xfs_trans_t *tp; /* transaction pointer, can be NULL */
  236. tp = cur->bc_tp;
  237. mp = cur->bc_mp;
  238. /*
  239. * Allocate a new cursor like the old one.
  240. */
  241. new = cur->bc_ops->dup_cursor(cur);
  242. /*
  243. * Copy the record currently in the cursor.
  244. */
  245. new->bc_rec = cur->bc_rec;
  246. /*
  247. * For each level current, re-get the buffer and copy the ptr value.
  248. */
  249. for (i = 0; i < new->bc_nlevels; i++) {
  250. new->bc_ptrs[i] = cur->bc_ptrs[i];
  251. new->bc_ra[i] = cur->bc_ra[i];
  252. if ((bp = cur->bc_bufs[i])) {
  253. if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
  254. XFS_BUF_ADDR(bp), mp->m_bsize, 0, &bp))) {
  255. xfs_btree_del_cursor(new, error);
  256. *ncur = NULL;
  257. return error;
  258. }
  259. new->bc_bufs[i] = bp;
  260. ASSERT(bp);
  261. ASSERT(!XFS_BUF_GETERROR(bp));
  262. } else
  263. new->bc_bufs[i] = NULL;
  264. }
  265. *ncur = new;
  266. return 0;
  267. }
  268. /*
  269. * XFS btree block layout and addressing:
  270. *
  271. * There are two types of blocks in the btree: leaf and non-leaf blocks.
  272. *
  273. * The leaf record start with a header then followed by records containing
  274. * the values. A non-leaf block also starts with the same header, and
  275. * then first contains lookup keys followed by an equal number of pointers
  276. * to the btree blocks at the previous level.
  277. *
  278. * +--------+-------+-------+-------+-------+-------+-------+
  279. * Leaf: | header | rec 1 | rec 2 | rec 3 | rec 4 | rec 5 | rec N |
  280. * +--------+-------+-------+-------+-------+-------+-------+
  281. *
  282. * +--------+-------+-------+-------+-------+-------+-------+
  283. * Non-Leaf: | header | key 1 | key 2 | key N | ptr 1 | ptr 2 | ptr N |
  284. * +--------+-------+-------+-------+-------+-------+-------+
  285. *
  286. * The header is called struct xfs_btree_block for reasons better left unknown
  287. * and comes in different versions for short (32bit) and long (64bit) block
  288. * pointers. The record and key structures are defined by the btree instances
  289. * and opaque to the btree core. The block pointers are simple disk endian
  290. * integers, available in a short (32bit) and long (64bit) variant.
  291. *
  292. * The helpers below calculate the offset of a given record, key or pointer
  293. * into a btree block (xfs_btree_*_offset) or return a pointer to the given
  294. * record, key or pointer (xfs_btree_*_addr). Note that all addressing
  295. * inside the btree block is done using indices starting at one, not zero!
  296. */
  297. /*
  298. * Return size of the btree block header for this btree instance.
  299. */
  300. static inline size_t xfs_btree_block_len(struct xfs_btree_cur *cur)
  301. {
  302. return (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
  303. XFS_BTREE_LBLOCK_LEN :
  304. XFS_BTREE_SBLOCK_LEN;
  305. }
  306. /*
  307. * Return size of btree block pointers for this btree instance.
  308. */
  309. static inline size_t xfs_btree_ptr_len(struct xfs_btree_cur *cur)
  310. {
  311. return (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
  312. sizeof(__be64) : sizeof(__be32);
  313. }
  314. /*
  315. * Calculate offset of the n-th record in a btree block.
  316. */
  317. STATIC size_t
  318. xfs_btree_rec_offset(
  319. struct xfs_btree_cur *cur,
  320. int n)
  321. {
  322. return xfs_btree_block_len(cur) +
  323. (n - 1) * cur->bc_ops->rec_len;
  324. }
  325. /*
  326. * Calculate offset of the n-th key in a btree block.
  327. */
  328. STATIC size_t
  329. xfs_btree_key_offset(
  330. struct xfs_btree_cur *cur,
  331. int n)
  332. {
  333. return xfs_btree_block_len(cur) +
  334. (n - 1) * cur->bc_ops->key_len;
  335. }
  336. /*
  337. * Calculate offset of the n-th block pointer in a btree block.
  338. */
  339. STATIC size_t
  340. xfs_btree_ptr_offset(
  341. struct xfs_btree_cur *cur,
  342. int n,
  343. int level)
  344. {
  345. return xfs_btree_block_len(cur) +
  346. cur->bc_ops->get_maxrecs(cur, level) * cur->bc_ops->key_len +
  347. (n - 1) * xfs_btree_ptr_len(cur);
  348. }
  349. /*
  350. * Return a pointer to the n-th record in the btree block.
  351. */
  352. STATIC union xfs_btree_rec *
  353. xfs_btree_rec_addr(
  354. struct xfs_btree_cur *cur,
  355. int n,
  356. struct xfs_btree_block *block)
  357. {
  358. return (union xfs_btree_rec *)
  359. ((char *)block + xfs_btree_rec_offset(cur, n));
  360. }
  361. /*
  362. * Return a pointer to the n-th key in the btree block.
  363. */
  364. STATIC union xfs_btree_key *
  365. xfs_btree_key_addr(
  366. struct xfs_btree_cur *cur,
  367. int n,
  368. struct xfs_btree_block *block)
  369. {
  370. return (union xfs_btree_key *)
  371. ((char *)block + xfs_btree_key_offset(cur, n));
  372. }
  373. /*
  374. * Return a pointer to the n-th block pointer in the btree block.
  375. */
  376. STATIC union xfs_btree_ptr *
  377. xfs_btree_ptr_addr(
  378. struct xfs_btree_cur *cur,
  379. int n,
  380. struct xfs_btree_block *block)
  381. {
  382. int level = xfs_btree_get_level(block);
  383. ASSERT(block->bb_level != 0);
  384. return (union xfs_btree_ptr *)
  385. ((char *)block + xfs_btree_ptr_offset(cur, n, level));
  386. }
  387. /*
  388. * Get a the root block which is stored in the inode.
  389. *
  390. * For now this btree implementation assumes the btree root is always
  391. * stored in the if_broot field of an inode fork.
  392. */
  393. STATIC struct xfs_btree_block *
  394. xfs_btree_get_iroot(
  395. struct xfs_btree_cur *cur)
  396. {
  397. struct xfs_ifork *ifp;
  398. ifp = XFS_IFORK_PTR(cur->bc_private.b.ip, cur->bc_private.b.whichfork);
  399. return (struct xfs_btree_block *)ifp->if_broot;
  400. }
  401. /*
  402. * Retrieve the block pointer from the cursor at the given level.
  403. * This may be an inode btree root or from a buffer.
  404. */
  405. STATIC struct xfs_btree_block * /* generic btree block pointer */
  406. xfs_btree_get_block(
  407. struct xfs_btree_cur *cur, /* btree cursor */
  408. int level, /* level in btree */
  409. struct xfs_buf **bpp) /* buffer containing the block */
  410. {
  411. if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
  412. (level == cur->bc_nlevels - 1)) {
  413. *bpp = NULL;
  414. return xfs_btree_get_iroot(cur);
  415. }
  416. *bpp = cur->bc_bufs[level];
  417. return XFS_BUF_TO_BLOCK(*bpp);
  418. }
  419. /*
  420. * Get a buffer for the block, return it with no data read.
  421. * Long-form addressing.
  422. */
  423. xfs_buf_t * /* buffer for fsbno */
  424. xfs_btree_get_bufl(
  425. xfs_mount_t *mp, /* file system mount point */
  426. xfs_trans_t *tp, /* transaction pointer */
  427. xfs_fsblock_t fsbno, /* file system block number */
  428. uint lock) /* lock flags for get_buf */
  429. {
  430. xfs_buf_t *bp; /* buffer pointer (return value) */
  431. xfs_daddr_t d; /* real disk block address */
  432. ASSERT(fsbno != NULLFSBLOCK);
  433. d = XFS_FSB_TO_DADDR(mp, fsbno);
  434. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
  435. ASSERT(bp);
  436. ASSERT(!XFS_BUF_GETERROR(bp));
  437. return bp;
  438. }
  439. /*
  440. * Get a buffer for the block, return it with no data read.
  441. * Short-form addressing.
  442. */
  443. xfs_buf_t * /* buffer for agno/agbno */
  444. xfs_btree_get_bufs(
  445. xfs_mount_t *mp, /* file system mount point */
  446. xfs_trans_t *tp, /* transaction pointer */
  447. xfs_agnumber_t agno, /* allocation group number */
  448. xfs_agblock_t agbno, /* allocation group block number */
  449. uint lock) /* lock flags for get_buf */
  450. {
  451. xfs_buf_t *bp; /* buffer pointer (return value) */
  452. xfs_daddr_t d; /* real disk block address */
  453. ASSERT(agno != NULLAGNUMBER);
  454. ASSERT(agbno != NULLAGBLOCK);
  455. d = XFS_AGB_TO_DADDR(mp, agno, agbno);
  456. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d, mp->m_bsize, lock);
  457. ASSERT(bp);
  458. ASSERT(!XFS_BUF_GETERROR(bp));
  459. return bp;
  460. }
  461. /*
  462. * Check for the cursor referring to the last block at the given level.
  463. */
  464. int /* 1=is last block, 0=not last block */
  465. xfs_btree_islastblock(
  466. xfs_btree_cur_t *cur, /* btree cursor */
  467. int level) /* level to check */
  468. {
  469. struct xfs_btree_block *block; /* generic btree block pointer */
  470. xfs_buf_t *bp; /* buffer containing block */
  471. block = xfs_btree_get_block(cur, level, &bp);
  472. xfs_btree_check_block(cur, block, level, bp);
  473. if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
  474. return be64_to_cpu(block->bb_u.l.bb_rightsib) == NULLDFSBNO;
  475. else
  476. return be32_to_cpu(block->bb_u.s.bb_rightsib) == NULLAGBLOCK;
  477. }
  478. /*
  479. * Change the cursor to point to the first record at the given level.
  480. * Other levels are unaffected.
  481. */
  482. STATIC int /* success=1, failure=0 */
  483. xfs_btree_firstrec(
  484. xfs_btree_cur_t *cur, /* btree cursor */
  485. int level) /* level to change */
  486. {
  487. struct xfs_btree_block *block; /* generic btree block pointer */
  488. xfs_buf_t *bp; /* buffer containing block */
  489. /*
  490. * Get the block pointer for this level.
  491. */
  492. block = xfs_btree_get_block(cur, level, &bp);
  493. xfs_btree_check_block(cur, block, level, bp);
  494. /*
  495. * It's empty, there is no such record.
  496. */
  497. if (!block->bb_numrecs)
  498. return 0;
  499. /*
  500. * Set the ptr value to 1, that's the first record/key.
  501. */
  502. cur->bc_ptrs[level] = 1;
  503. return 1;
  504. }
  505. /*
  506. * Change the cursor to point to the last record in the current block
  507. * at the given level. Other levels are unaffected.
  508. */
  509. STATIC int /* success=1, failure=0 */
  510. xfs_btree_lastrec(
  511. xfs_btree_cur_t *cur, /* btree cursor */
  512. int level) /* level to change */
  513. {
  514. struct xfs_btree_block *block; /* generic btree block pointer */
  515. xfs_buf_t *bp; /* buffer containing block */
  516. /*
  517. * Get the block pointer for this level.
  518. */
  519. block = xfs_btree_get_block(cur, level, &bp);
  520. xfs_btree_check_block(cur, block, level, bp);
  521. /*
  522. * It's empty, there is no such record.
  523. */
  524. if (!block->bb_numrecs)
  525. return 0;
  526. /*
  527. * Set the ptr value to numrecs, that's the last record/key.
  528. */
  529. cur->bc_ptrs[level] = be16_to_cpu(block->bb_numrecs);
  530. return 1;
  531. }
  532. /*
  533. * Compute first and last byte offsets for the fields given.
  534. * Interprets the offsets table, which contains struct field offsets.
  535. */
  536. void
  537. xfs_btree_offsets(
  538. __int64_t fields, /* bitmask of fields */
  539. const short *offsets, /* table of field offsets */
  540. int nbits, /* number of bits to inspect */
  541. int *first, /* output: first byte offset */
  542. int *last) /* output: last byte offset */
  543. {
  544. int i; /* current bit number */
  545. __int64_t imask; /* mask for current bit number */
  546. ASSERT(fields != 0);
  547. /*
  548. * Find the lowest bit, so the first byte offset.
  549. */
  550. for (i = 0, imask = 1LL; ; i++, imask <<= 1) {
  551. if (imask & fields) {
  552. *first = offsets[i];
  553. break;
  554. }
  555. }
  556. /*
  557. * Find the highest bit, so the last byte offset.
  558. */
  559. for (i = nbits - 1, imask = 1LL << i; ; i--, imask >>= 1) {
  560. if (imask & fields) {
  561. *last = offsets[i + 1] - 1;
  562. break;
  563. }
  564. }
  565. }
  566. /*
  567. * Get a buffer for the block, return it read in.
  568. * Long-form addressing.
  569. */
  570. int /* error */
  571. xfs_btree_read_bufl(
  572. xfs_mount_t *mp, /* file system mount point */
  573. xfs_trans_t *tp, /* transaction pointer */
  574. xfs_fsblock_t fsbno, /* file system block number */
  575. uint lock, /* lock flags for read_buf */
  576. xfs_buf_t **bpp, /* buffer for fsbno */
  577. int refval) /* ref count value for buffer */
  578. {
  579. xfs_buf_t *bp; /* return value */
  580. xfs_daddr_t d; /* real disk block address */
  581. int error;
  582. ASSERT(fsbno != NULLFSBLOCK);
  583. d = XFS_FSB_TO_DADDR(mp, fsbno);
  584. if ((error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, d,
  585. mp->m_bsize, lock, &bp))) {
  586. return error;
  587. }
  588. ASSERT(!bp || !XFS_BUF_GETERROR(bp));
  589. if (bp != NULL) {
  590. XFS_BUF_SET_VTYPE_REF(bp, B_FS_MAP, refval);
  591. }
  592. *bpp = bp;
  593. return 0;
  594. }
  595. /*
  596. * Read-ahead the block, don't wait for it, don't return a buffer.
  597. * Long-form addressing.
  598. */
  599. /* ARGSUSED */
  600. void
  601. xfs_btree_reada_bufl(
  602. xfs_mount_t *mp, /* file system mount point */
  603. xfs_fsblock_t fsbno, /* file system block number */
  604. xfs_extlen_t count) /* count of filesystem blocks */
  605. {
  606. xfs_daddr_t d;
  607. ASSERT(fsbno != NULLFSBLOCK);
  608. d = XFS_FSB_TO_DADDR(mp, fsbno);
  609. xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count);
  610. }
  611. /*
  612. * Read-ahead the block, don't wait for it, don't return a buffer.
  613. * Short-form addressing.
  614. */
  615. /* ARGSUSED */
  616. void
  617. xfs_btree_reada_bufs(
  618. xfs_mount_t *mp, /* file system mount point */
  619. xfs_agnumber_t agno, /* allocation group number */
  620. xfs_agblock_t agbno, /* allocation group block number */
  621. xfs_extlen_t count) /* count of filesystem blocks */
  622. {
  623. xfs_daddr_t d;
  624. ASSERT(agno != NULLAGNUMBER);
  625. ASSERT(agbno != NULLAGBLOCK);
  626. d = XFS_AGB_TO_DADDR(mp, agno, agbno);
  627. xfs_buf_readahead(mp->m_ddev_targp, d, mp->m_bsize * count);
  628. }
  629. STATIC int
  630. xfs_btree_readahead_lblock(
  631. struct xfs_btree_cur *cur,
  632. int lr,
  633. struct xfs_btree_block *block)
  634. {
  635. int rval = 0;
  636. xfs_dfsbno_t left = be64_to_cpu(block->bb_u.l.bb_leftsib);
  637. xfs_dfsbno_t right = be64_to_cpu(block->bb_u.l.bb_rightsib);
  638. if ((lr & XFS_BTCUR_LEFTRA) && left != NULLDFSBNO) {
  639. xfs_btree_reada_bufl(cur->bc_mp, left, 1);
  640. rval++;
  641. }
  642. if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLDFSBNO) {
  643. xfs_btree_reada_bufl(cur->bc_mp, right, 1);
  644. rval++;
  645. }
  646. return rval;
  647. }
  648. STATIC int
  649. xfs_btree_readahead_sblock(
  650. struct xfs_btree_cur *cur,
  651. int lr,
  652. struct xfs_btree_block *block)
  653. {
  654. int rval = 0;
  655. xfs_agblock_t left = be32_to_cpu(block->bb_u.s.bb_leftsib);
  656. xfs_agblock_t right = be32_to_cpu(block->bb_u.s.bb_rightsib);
  657. if ((lr & XFS_BTCUR_LEFTRA) && left != NULLAGBLOCK) {
  658. xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
  659. left, 1);
  660. rval++;
  661. }
  662. if ((lr & XFS_BTCUR_RIGHTRA) && right != NULLAGBLOCK) {
  663. xfs_btree_reada_bufs(cur->bc_mp, cur->bc_private.a.agno,
  664. right, 1);
  665. rval++;
  666. }
  667. return rval;
  668. }
  669. /*
  670. * Read-ahead btree blocks, at the given level.
  671. * Bits in lr are set from XFS_BTCUR_{LEFT,RIGHT}RA.
  672. */
  673. STATIC int
  674. xfs_btree_readahead(
  675. struct xfs_btree_cur *cur, /* btree cursor */
  676. int lev, /* level in btree */
  677. int lr) /* left/right bits */
  678. {
  679. struct xfs_btree_block *block;
  680. /*
  681. * No readahead needed if we are at the root level and the
  682. * btree root is stored in the inode.
  683. */
  684. if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
  685. (lev == cur->bc_nlevels - 1))
  686. return 0;
  687. if ((cur->bc_ra[lev] | lr) == cur->bc_ra[lev])
  688. return 0;
  689. cur->bc_ra[lev] |= lr;
  690. block = XFS_BUF_TO_BLOCK(cur->bc_bufs[lev]);
  691. if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
  692. return xfs_btree_readahead_lblock(cur, lr, block);
  693. return xfs_btree_readahead_sblock(cur, lr, block);
  694. }
  695. /*
  696. * Set the buffer for level "lev" in the cursor to bp, releasing
  697. * any previous buffer.
  698. */
  699. STATIC void
  700. xfs_btree_setbuf(
  701. xfs_btree_cur_t *cur, /* btree cursor */
  702. int lev, /* level in btree */
  703. xfs_buf_t *bp) /* new buffer to set */
  704. {
  705. struct xfs_btree_block *b; /* btree block */
  706. if (cur->bc_bufs[lev])
  707. xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[lev]);
  708. cur->bc_bufs[lev] = bp;
  709. cur->bc_ra[lev] = 0;
  710. b = XFS_BUF_TO_BLOCK(bp);
  711. if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
  712. if (be64_to_cpu(b->bb_u.l.bb_leftsib) == NULLDFSBNO)
  713. cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
  714. if (be64_to_cpu(b->bb_u.l.bb_rightsib) == NULLDFSBNO)
  715. cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
  716. } else {
  717. if (be32_to_cpu(b->bb_u.s.bb_leftsib) == NULLAGBLOCK)
  718. cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
  719. if (be32_to_cpu(b->bb_u.s.bb_rightsib) == NULLAGBLOCK)
  720. cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
  721. }
  722. }
  723. STATIC int
  724. xfs_btree_ptr_is_null(
  725. struct xfs_btree_cur *cur,
  726. union xfs_btree_ptr *ptr)
  727. {
  728. if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
  729. return be64_to_cpu(ptr->l) == NULLDFSBNO;
  730. else
  731. return be32_to_cpu(ptr->s) == NULLAGBLOCK;
  732. }
  733. STATIC void
  734. xfs_btree_set_ptr_null(
  735. struct xfs_btree_cur *cur,
  736. union xfs_btree_ptr *ptr)
  737. {
  738. if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
  739. ptr->l = cpu_to_be64(NULLDFSBNO);
  740. else
  741. ptr->s = cpu_to_be32(NULLAGBLOCK);
  742. }
  743. /*
  744. * Get/set/init sibling pointers
  745. */
  746. STATIC void
  747. xfs_btree_get_sibling(
  748. struct xfs_btree_cur *cur,
  749. struct xfs_btree_block *block,
  750. union xfs_btree_ptr *ptr,
  751. int lr)
  752. {
  753. ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
  754. if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
  755. if (lr == XFS_BB_RIGHTSIB)
  756. ptr->l = block->bb_u.l.bb_rightsib;
  757. else
  758. ptr->l = block->bb_u.l.bb_leftsib;
  759. } else {
  760. if (lr == XFS_BB_RIGHTSIB)
  761. ptr->s = block->bb_u.s.bb_rightsib;
  762. else
  763. ptr->s = block->bb_u.s.bb_leftsib;
  764. }
  765. }
  766. STATIC void
  767. xfs_btree_set_sibling(
  768. struct xfs_btree_cur *cur,
  769. struct xfs_btree_block *block,
  770. union xfs_btree_ptr *ptr,
  771. int lr)
  772. {
  773. ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
  774. if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
  775. if (lr == XFS_BB_RIGHTSIB)
  776. block->bb_u.l.bb_rightsib = ptr->l;
  777. else
  778. block->bb_u.l.bb_leftsib = ptr->l;
  779. } else {
  780. if (lr == XFS_BB_RIGHTSIB)
  781. block->bb_u.s.bb_rightsib = ptr->s;
  782. else
  783. block->bb_u.s.bb_leftsib = ptr->s;
  784. }
  785. }
  786. STATIC void
  787. xfs_btree_init_block(
  788. struct xfs_btree_cur *cur,
  789. int level,
  790. int numrecs,
  791. struct xfs_btree_block *new) /* new block */
  792. {
  793. new->bb_magic = cpu_to_be32(xfs_magics[cur->bc_btnum]);
  794. new->bb_level = cpu_to_be16(level);
  795. new->bb_numrecs = cpu_to_be16(numrecs);
  796. if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
  797. new->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
  798. new->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
  799. } else {
  800. new->bb_u.s.bb_leftsib = cpu_to_be32(NULLAGBLOCK);
  801. new->bb_u.s.bb_rightsib = cpu_to_be32(NULLAGBLOCK);
  802. }
  803. }
  804. /*
  805. * Return true if ptr is the last record in the btree and
  806. * we need to track updateѕ to this record. The decision
  807. * will be further refined in the update_lastrec method.
  808. */
  809. STATIC int
  810. xfs_btree_is_lastrec(
  811. struct xfs_btree_cur *cur,
  812. struct xfs_btree_block *block,
  813. int level)
  814. {
  815. union xfs_btree_ptr ptr;
  816. if (level > 0)
  817. return 0;
  818. if (!(cur->bc_flags & XFS_BTREE_LASTREC_UPDATE))
  819. return 0;
  820. xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
  821. if (!xfs_btree_ptr_is_null(cur, &ptr))
  822. return 0;
  823. return 1;
  824. }
  825. STATIC void
  826. xfs_btree_buf_to_ptr(
  827. struct xfs_btree_cur *cur,
  828. struct xfs_buf *bp,
  829. union xfs_btree_ptr *ptr)
  830. {
  831. if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
  832. ptr->l = cpu_to_be64(XFS_DADDR_TO_FSB(cur->bc_mp,
  833. XFS_BUF_ADDR(bp)));
  834. else {
  835. ptr->s = cpu_to_be32(xfs_daddr_to_agbno(cur->bc_mp,
  836. XFS_BUF_ADDR(bp)));
  837. }
  838. }
  839. STATIC xfs_daddr_t
  840. xfs_btree_ptr_to_daddr(
  841. struct xfs_btree_cur *cur,
  842. union xfs_btree_ptr *ptr)
  843. {
  844. if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
  845. ASSERT(be64_to_cpu(ptr->l) != NULLDFSBNO);
  846. return XFS_FSB_TO_DADDR(cur->bc_mp, be64_to_cpu(ptr->l));
  847. } else {
  848. ASSERT(cur->bc_private.a.agno != NULLAGNUMBER);
  849. ASSERT(be32_to_cpu(ptr->s) != NULLAGBLOCK);
  850. return XFS_AGB_TO_DADDR(cur->bc_mp, cur->bc_private.a.agno,
  851. be32_to_cpu(ptr->s));
  852. }
  853. }
  854. STATIC void
  855. xfs_btree_set_refs(
  856. struct xfs_btree_cur *cur,
  857. struct xfs_buf *bp)
  858. {
  859. switch (cur->bc_btnum) {
  860. case XFS_BTNUM_BNO:
  861. case XFS_BTNUM_CNT:
  862. XFS_BUF_SET_VTYPE_REF(*bpp, B_FS_MAP, XFS_ALLOC_BTREE_REF);
  863. break;
  864. case XFS_BTNUM_INO:
  865. XFS_BUF_SET_VTYPE_REF(*bpp, B_FS_INOMAP, XFS_INO_BTREE_REF);
  866. break;
  867. case XFS_BTNUM_BMAP:
  868. XFS_BUF_SET_VTYPE_REF(*bpp, B_FS_MAP, XFS_BMAP_BTREE_REF);
  869. break;
  870. default:
  871. ASSERT(0);
  872. }
  873. }
  874. STATIC int
  875. xfs_btree_get_buf_block(
  876. struct xfs_btree_cur *cur,
  877. union xfs_btree_ptr *ptr,
  878. int flags,
  879. struct xfs_btree_block **block,
  880. struct xfs_buf **bpp)
  881. {
  882. struct xfs_mount *mp = cur->bc_mp;
  883. xfs_daddr_t d;
  884. /* need to sort out how callers deal with failures first */
  885. ASSERT(!(flags & XBF_TRYLOCK));
  886. d = xfs_btree_ptr_to_daddr(cur, ptr);
  887. *bpp = xfs_trans_get_buf(cur->bc_tp, mp->m_ddev_targp, d,
  888. mp->m_bsize, flags);
  889. ASSERT(*bpp);
  890. ASSERT(!XFS_BUF_GETERROR(*bpp));
  891. *block = XFS_BUF_TO_BLOCK(*bpp);
  892. return 0;
  893. }
  894. /*
  895. * Read in the buffer at the given ptr and return the buffer and
  896. * the block pointer within the buffer.
  897. */
  898. STATIC int
  899. xfs_btree_read_buf_block(
  900. struct xfs_btree_cur *cur,
  901. union xfs_btree_ptr *ptr,
  902. int level,
  903. int flags,
  904. struct xfs_btree_block **block,
  905. struct xfs_buf **bpp)
  906. {
  907. struct xfs_mount *mp = cur->bc_mp;
  908. xfs_daddr_t d;
  909. int error;
  910. /* need to sort out how callers deal with failures first */
  911. ASSERT(!(flags & XBF_TRYLOCK));
  912. d = xfs_btree_ptr_to_daddr(cur, ptr);
  913. error = xfs_trans_read_buf(mp, cur->bc_tp, mp->m_ddev_targp, d,
  914. mp->m_bsize, flags, bpp);
  915. if (error)
  916. return error;
  917. ASSERT(*bpp != NULL);
  918. ASSERT(!XFS_BUF_GETERROR(*bpp));
  919. xfs_btree_set_refs(cur, *bpp);
  920. *block = XFS_BUF_TO_BLOCK(*bpp);
  921. error = xfs_btree_check_block(cur, *block, level, *bpp);
  922. if (error)
  923. xfs_trans_brelse(cur->bc_tp, *bpp);
  924. return error;
  925. }
  926. /*
  927. * Copy keys from one btree block to another.
  928. */
  929. STATIC void
  930. xfs_btree_copy_keys(
  931. struct xfs_btree_cur *cur,
  932. union xfs_btree_key *dst_key,
  933. union xfs_btree_key *src_key,
  934. int numkeys)
  935. {
  936. ASSERT(numkeys >= 0);
  937. memcpy(dst_key, src_key, numkeys * cur->bc_ops->key_len);
  938. }
  939. /*
  940. * Copy records from one btree block to another.
  941. */
  942. STATIC void
  943. xfs_btree_copy_recs(
  944. struct xfs_btree_cur *cur,
  945. union xfs_btree_rec *dst_rec,
  946. union xfs_btree_rec *src_rec,
  947. int numrecs)
  948. {
  949. ASSERT(numrecs >= 0);
  950. memcpy(dst_rec, src_rec, numrecs * cur->bc_ops->rec_len);
  951. }
  952. /*
  953. * Copy block pointers from one btree block to another.
  954. */
  955. STATIC void
  956. xfs_btree_copy_ptrs(
  957. struct xfs_btree_cur *cur,
  958. union xfs_btree_ptr *dst_ptr,
  959. union xfs_btree_ptr *src_ptr,
  960. int numptrs)
  961. {
  962. ASSERT(numptrs >= 0);
  963. memcpy(dst_ptr, src_ptr, numptrs * xfs_btree_ptr_len(cur));
  964. }
  965. /*
  966. * Shift keys one index left/right inside a single btree block.
  967. */
  968. STATIC void
  969. xfs_btree_shift_keys(
  970. struct xfs_btree_cur *cur,
  971. union xfs_btree_key *key,
  972. int dir,
  973. int numkeys)
  974. {
  975. char *dst_key;
  976. ASSERT(numkeys >= 0);
  977. ASSERT(dir == 1 || dir == -1);
  978. dst_key = (char *)key + (dir * cur->bc_ops->key_len);
  979. memmove(dst_key, key, numkeys * cur->bc_ops->key_len);
  980. }
  981. /*
  982. * Shift records one index left/right inside a single btree block.
  983. */
  984. STATIC void
  985. xfs_btree_shift_recs(
  986. struct xfs_btree_cur *cur,
  987. union xfs_btree_rec *rec,
  988. int dir,
  989. int numrecs)
  990. {
  991. char *dst_rec;
  992. ASSERT(numrecs >= 0);
  993. ASSERT(dir == 1 || dir == -1);
  994. dst_rec = (char *)rec + (dir * cur->bc_ops->rec_len);
  995. memmove(dst_rec, rec, numrecs * cur->bc_ops->rec_len);
  996. }
  997. /*
  998. * Shift block pointers one index left/right inside a single btree block.
  999. */
  1000. STATIC void
  1001. xfs_btree_shift_ptrs(
  1002. struct xfs_btree_cur *cur,
  1003. union xfs_btree_ptr *ptr,
  1004. int dir,
  1005. int numptrs)
  1006. {
  1007. char *dst_ptr;
  1008. ASSERT(numptrs >= 0);
  1009. ASSERT(dir == 1 || dir == -1);
  1010. dst_ptr = (char *)ptr + (dir * xfs_btree_ptr_len(cur));
  1011. memmove(dst_ptr, ptr, numptrs * xfs_btree_ptr_len(cur));
  1012. }
  1013. /*
  1014. * Log key values from the btree block.
  1015. */
  1016. STATIC void
  1017. xfs_btree_log_keys(
  1018. struct xfs_btree_cur *cur,
  1019. struct xfs_buf *bp,
  1020. int first,
  1021. int last)
  1022. {
  1023. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  1024. XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
  1025. if (bp) {
  1026. xfs_trans_log_buf(cur->bc_tp, bp,
  1027. xfs_btree_key_offset(cur, first),
  1028. xfs_btree_key_offset(cur, last + 1) - 1);
  1029. } else {
  1030. xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
  1031. xfs_ilog_fbroot(cur->bc_private.b.whichfork));
  1032. }
  1033. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1034. }
  1035. /*
  1036. * Log record values from the btree block.
  1037. */
  1038. void
  1039. xfs_btree_log_recs(
  1040. struct xfs_btree_cur *cur,
  1041. struct xfs_buf *bp,
  1042. int first,
  1043. int last)
  1044. {
  1045. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  1046. XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
  1047. xfs_trans_log_buf(cur->bc_tp, bp,
  1048. xfs_btree_rec_offset(cur, first),
  1049. xfs_btree_rec_offset(cur, last + 1) - 1);
  1050. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1051. }
  1052. /*
  1053. * Log block pointer fields from a btree block (nonleaf).
  1054. */
  1055. STATIC void
  1056. xfs_btree_log_ptrs(
  1057. struct xfs_btree_cur *cur, /* btree cursor */
  1058. struct xfs_buf *bp, /* buffer containing btree block */
  1059. int first, /* index of first pointer to log */
  1060. int last) /* index of last pointer to log */
  1061. {
  1062. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  1063. XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
  1064. if (bp) {
  1065. struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
  1066. int level = xfs_btree_get_level(block);
  1067. xfs_trans_log_buf(cur->bc_tp, bp,
  1068. xfs_btree_ptr_offset(cur, first, level),
  1069. xfs_btree_ptr_offset(cur, last + 1, level) - 1);
  1070. } else {
  1071. xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
  1072. xfs_ilog_fbroot(cur->bc_private.b.whichfork));
  1073. }
  1074. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1075. }
  1076. /*
  1077. * Log fields from a btree block header.
  1078. */
  1079. void
  1080. xfs_btree_log_block(
  1081. struct xfs_btree_cur *cur, /* btree cursor */
  1082. struct xfs_buf *bp, /* buffer containing btree block */
  1083. int fields) /* mask of fields: XFS_BB_... */
  1084. {
  1085. int first; /* first byte offset logged */
  1086. int last; /* last byte offset logged */
  1087. static const short soffsets[] = { /* table of offsets (short) */
  1088. offsetof(struct xfs_btree_block, bb_magic),
  1089. offsetof(struct xfs_btree_block, bb_level),
  1090. offsetof(struct xfs_btree_block, bb_numrecs),
  1091. offsetof(struct xfs_btree_block, bb_u.s.bb_leftsib),
  1092. offsetof(struct xfs_btree_block, bb_u.s.bb_rightsib),
  1093. XFS_BTREE_SBLOCK_LEN
  1094. };
  1095. static const short loffsets[] = { /* table of offsets (long) */
  1096. offsetof(struct xfs_btree_block, bb_magic),
  1097. offsetof(struct xfs_btree_block, bb_level),
  1098. offsetof(struct xfs_btree_block, bb_numrecs),
  1099. offsetof(struct xfs_btree_block, bb_u.l.bb_leftsib),
  1100. offsetof(struct xfs_btree_block, bb_u.l.bb_rightsib),
  1101. XFS_BTREE_LBLOCK_LEN
  1102. };
  1103. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  1104. XFS_BTREE_TRACE_ARGBI(cur, bp, fields);
  1105. if (bp) {
  1106. xfs_btree_offsets(fields,
  1107. (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
  1108. loffsets : soffsets,
  1109. XFS_BB_NUM_BITS, &first, &last);
  1110. xfs_trans_log_buf(cur->bc_tp, bp, first, last);
  1111. } else {
  1112. xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
  1113. xfs_ilog_fbroot(cur->bc_private.b.whichfork));
  1114. }
  1115. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1116. }
  1117. /*
  1118. * Increment cursor by one record at the level.
  1119. * For nonzero levels the leaf-ward information is untouched.
  1120. */
  1121. int /* error */
  1122. xfs_btree_increment(
  1123. struct xfs_btree_cur *cur,
  1124. int level,
  1125. int *stat) /* success/failure */
  1126. {
  1127. struct xfs_btree_block *block;
  1128. union xfs_btree_ptr ptr;
  1129. struct xfs_buf *bp;
  1130. int error; /* error return value */
  1131. int lev;
  1132. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  1133. XFS_BTREE_TRACE_ARGI(cur, level);
  1134. ASSERT(level < cur->bc_nlevels);
  1135. /* Read-ahead to the right at this level. */
  1136. xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
  1137. /* Get a pointer to the btree block. */
  1138. block = xfs_btree_get_block(cur, level, &bp);
  1139. #ifdef DEBUG
  1140. error = xfs_btree_check_block(cur, block, level, bp);
  1141. if (error)
  1142. goto error0;
  1143. #endif
  1144. /* We're done if we remain in the block after the increment. */
  1145. if (++cur->bc_ptrs[level] <= xfs_btree_get_numrecs(block))
  1146. goto out1;
  1147. /* Fail if we just went off the right edge of the tree. */
  1148. xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
  1149. if (xfs_btree_ptr_is_null(cur, &ptr))
  1150. goto out0;
  1151. XFS_BTREE_STATS_INC(cur, increment);
  1152. /*
  1153. * March up the tree incrementing pointers.
  1154. * Stop when we don't go off the right edge of a block.
  1155. */
  1156. for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
  1157. block = xfs_btree_get_block(cur, lev, &bp);
  1158. #ifdef DEBUG
  1159. error = xfs_btree_check_block(cur, block, lev, bp);
  1160. if (error)
  1161. goto error0;
  1162. #endif
  1163. if (++cur->bc_ptrs[lev] <= xfs_btree_get_numrecs(block))
  1164. break;
  1165. /* Read-ahead the right block for the next loop. */
  1166. xfs_btree_readahead(cur, lev, XFS_BTCUR_RIGHTRA);
  1167. }
  1168. /*
  1169. * If we went off the root then we are either seriously
  1170. * confused or have the tree root in an inode.
  1171. */
  1172. if (lev == cur->bc_nlevels) {
  1173. if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
  1174. goto out0;
  1175. ASSERT(0);
  1176. error = EFSCORRUPTED;
  1177. goto error0;
  1178. }
  1179. ASSERT(lev < cur->bc_nlevels);
  1180. /*
  1181. * Now walk back down the tree, fixing up the cursor's buffer
  1182. * pointers and key numbers.
  1183. */
  1184. for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
  1185. union xfs_btree_ptr *ptrp;
  1186. ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
  1187. error = xfs_btree_read_buf_block(cur, ptrp, --lev,
  1188. 0, &block, &bp);
  1189. if (error)
  1190. goto error0;
  1191. xfs_btree_setbuf(cur, lev, bp);
  1192. cur->bc_ptrs[lev] = 1;
  1193. }
  1194. out1:
  1195. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1196. *stat = 1;
  1197. return 0;
  1198. out0:
  1199. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1200. *stat = 0;
  1201. return 0;
  1202. error0:
  1203. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  1204. return error;
  1205. }
  1206. /*
  1207. * Decrement cursor by one record at the level.
  1208. * For nonzero levels the leaf-ward information is untouched.
  1209. */
  1210. int /* error */
  1211. xfs_btree_decrement(
  1212. struct xfs_btree_cur *cur,
  1213. int level,
  1214. int *stat) /* success/failure */
  1215. {
  1216. struct xfs_btree_block *block;
  1217. xfs_buf_t *bp;
  1218. int error; /* error return value */
  1219. int lev;
  1220. union xfs_btree_ptr ptr;
  1221. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  1222. XFS_BTREE_TRACE_ARGI(cur, level);
  1223. ASSERT(level < cur->bc_nlevels);
  1224. /* Read-ahead to the left at this level. */
  1225. xfs_btree_readahead(cur, level, XFS_BTCUR_LEFTRA);
  1226. /* We're done if we remain in the block after the decrement. */
  1227. if (--cur->bc_ptrs[level] > 0)
  1228. goto out1;
  1229. /* Get a pointer to the btree block. */
  1230. block = xfs_btree_get_block(cur, level, &bp);
  1231. #ifdef DEBUG
  1232. error = xfs_btree_check_block(cur, block, level, bp);
  1233. if (error)
  1234. goto error0;
  1235. #endif
  1236. /* Fail if we just went off the left edge of the tree. */
  1237. xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
  1238. if (xfs_btree_ptr_is_null(cur, &ptr))
  1239. goto out0;
  1240. XFS_BTREE_STATS_INC(cur, decrement);
  1241. /*
  1242. * March up the tree decrementing pointers.
  1243. * Stop when we don't go off the left edge of a block.
  1244. */
  1245. for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
  1246. if (--cur->bc_ptrs[lev] > 0)
  1247. break;
  1248. /* Read-ahead the left block for the next loop. */
  1249. xfs_btree_readahead(cur, lev, XFS_BTCUR_LEFTRA);
  1250. }
  1251. /*
  1252. * If we went off the root then we are seriously confused.
  1253. * or the root of the tree is in an inode.
  1254. */
  1255. if (lev == cur->bc_nlevels) {
  1256. if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
  1257. goto out0;
  1258. ASSERT(0);
  1259. error = EFSCORRUPTED;
  1260. goto error0;
  1261. }
  1262. ASSERT(lev < cur->bc_nlevels);
  1263. /*
  1264. * Now walk back down the tree, fixing up the cursor's buffer
  1265. * pointers and key numbers.
  1266. */
  1267. for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
  1268. union xfs_btree_ptr *ptrp;
  1269. ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
  1270. error = xfs_btree_read_buf_block(cur, ptrp, --lev,
  1271. 0, &block, &bp);
  1272. if (error)
  1273. goto error0;
  1274. xfs_btree_setbuf(cur, lev, bp);
  1275. cur->bc_ptrs[lev] = xfs_btree_get_numrecs(block);
  1276. }
  1277. out1:
  1278. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1279. *stat = 1;
  1280. return 0;
  1281. out0:
  1282. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1283. *stat = 0;
  1284. return 0;
  1285. error0:
  1286. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  1287. return error;
  1288. }
  1289. STATIC int
  1290. xfs_btree_lookup_get_block(
  1291. struct xfs_btree_cur *cur, /* btree cursor */
  1292. int level, /* level in the btree */
  1293. union xfs_btree_ptr *pp, /* ptr to btree block */
  1294. struct xfs_btree_block **blkp) /* return btree block */
  1295. {
  1296. struct xfs_buf *bp; /* buffer pointer for btree block */
  1297. int error = 0;
  1298. /* special case the root block if in an inode */
  1299. if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
  1300. (level == cur->bc_nlevels - 1)) {
  1301. *blkp = xfs_btree_get_iroot(cur);
  1302. return 0;
  1303. }
  1304. /*
  1305. * If the old buffer at this level for the disk address we are
  1306. * looking for re-use it.
  1307. *
  1308. * Otherwise throw it away and get a new one.
  1309. */
  1310. bp = cur->bc_bufs[level];
  1311. if (bp && XFS_BUF_ADDR(bp) == xfs_btree_ptr_to_daddr(cur, pp)) {
  1312. *blkp = XFS_BUF_TO_BLOCK(bp);
  1313. return 0;
  1314. }
  1315. error = xfs_btree_read_buf_block(cur, pp, level, 0, blkp, &bp);
  1316. if (error)
  1317. return error;
  1318. xfs_btree_setbuf(cur, level, bp);
  1319. return 0;
  1320. }
  1321. /*
  1322. * Get current search key. For level 0 we don't actually have a key
  1323. * structure so we make one up from the record. For all other levels
  1324. * we just return the right key.
  1325. */
  1326. STATIC union xfs_btree_key *
  1327. xfs_lookup_get_search_key(
  1328. struct xfs_btree_cur *cur,
  1329. int level,
  1330. int keyno,
  1331. struct xfs_btree_block *block,
  1332. union xfs_btree_key *kp)
  1333. {
  1334. if (level == 0) {
  1335. cur->bc_ops->init_key_from_rec(kp,
  1336. xfs_btree_rec_addr(cur, keyno, block));
  1337. return kp;
  1338. }
  1339. return xfs_btree_key_addr(cur, keyno, block);
  1340. }
  1341. /*
  1342. * Lookup the record. The cursor is made to point to it, based on dir.
  1343. * Return 0 if can't find any such record, 1 for success.
  1344. */
  1345. int /* error */
  1346. xfs_btree_lookup(
  1347. struct xfs_btree_cur *cur, /* btree cursor */
  1348. xfs_lookup_t dir, /* <=, ==, or >= */
  1349. int *stat) /* success/failure */
  1350. {
  1351. struct xfs_btree_block *block; /* current btree block */
  1352. __int64_t diff; /* difference for the current key */
  1353. int error; /* error return value */
  1354. int keyno; /* current key number */
  1355. int level; /* level in the btree */
  1356. union xfs_btree_ptr *pp; /* ptr to btree block */
  1357. union xfs_btree_ptr ptr; /* ptr to btree block */
  1358. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  1359. XFS_BTREE_TRACE_ARGI(cur, dir);
  1360. XFS_BTREE_STATS_INC(cur, lookup);
  1361. block = NULL;
  1362. keyno = 0;
  1363. /* initialise start pointer from cursor */
  1364. cur->bc_ops->init_ptr_from_cur(cur, &ptr);
  1365. pp = &ptr;
  1366. /*
  1367. * Iterate over each level in the btree, starting at the root.
  1368. * For each level above the leaves, find the key we need, based
  1369. * on the lookup record, then follow the corresponding block
  1370. * pointer down to the next level.
  1371. */
  1372. for (level = cur->bc_nlevels - 1, diff = 1; level >= 0; level--) {
  1373. /* Get the block we need to do the lookup on. */
  1374. error = xfs_btree_lookup_get_block(cur, level, pp, &block);
  1375. if (error)
  1376. goto error0;
  1377. if (diff == 0) {
  1378. /*
  1379. * If we already had a key match at a higher level, we
  1380. * know we need to use the first entry in this block.
  1381. */
  1382. keyno = 1;
  1383. } else {
  1384. /* Otherwise search this block. Do a binary search. */
  1385. int high; /* high entry number */
  1386. int low; /* low entry number */
  1387. /* Set low and high entry numbers, 1-based. */
  1388. low = 1;
  1389. high = xfs_btree_get_numrecs(block);
  1390. if (!high) {
  1391. /* Block is empty, must be an empty leaf. */
  1392. ASSERT(level == 0 && cur->bc_nlevels == 1);
  1393. cur->bc_ptrs[0] = dir != XFS_LOOKUP_LE;
  1394. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1395. *stat = 0;
  1396. return 0;
  1397. }
  1398. /* Binary search the block. */
  1399. while (low <= high) {
  1400. union xfs_btree_key key;
  1401. union xfs_btree_key *kp;
  1402. XFS_BTREE_STATS_INC(cur, compare);
  1403. /* keyno is average of low and high. */
  1404. keyno = (low + high) >> 1;
  1405. /* Get current search key */
  1406. kp = xfs_lookup_get_search_key(cur, level,
  1407. keyno, block, &key);
  1408. /*
  1409. * Compute difference to get next direction:
  1410. * - less than, move right
  1411. * - greater than, move left
  1412. * - equal, we're done
  1413. */
  1414. diff = cur->bc_ops->key_diff(cur, kp);
  1415. if (diff < 0)
  1416. low = keyno + 1;
  1417. else if (diff > 0)
  1418. high = keyno - 1;
  1419. else
  1420. break;
  1421. }
  1422. }
  1423. /*
  1424. * If there are more levels, set up for the next level
  1425. * by getting the block number and filling in the cursor.
  1426. */
  1427. if (level > 0) {
  1428. /*
  1429. * If we moved left, need the previous key number,
  1430. * unless there isn't one.
  1431. */
  1432. if (diff > 0 && --keyno < 1)
  1433. keyno = 1;
  1434. pp = xfs_btree_ptr_addr(cur, keyno, block);
  1435. #ifdef DEBUG
  1436. error = xfs_btree_check_ptr(cur, pp, 0, level);
  1437. if (error)
  1438. goto error0;
  1439. #endif
  1440. cur->bc_ptrs[level] = keyno;
  1441. }
  1442. }
  1443. /* Done with the search. See if we need to adjust the results. */
  1444. if (dir != XFS_LOOKUP_LE && diff < 0) {
  1445. keyno++;
  1446. /*
  1447. * If ge search and we went off the end of the block, but it's
  1448. * not the last block, we're in the wrong block.
  1449. */
  1450. xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
  1451. if (dir == XFS_LOOKUP_GE &&
  1452. keyno > xfs_btree_get_numrecs(block) &&
  1453. !xfs_btree_ptr_is_null(cur, &ptr)) {
  1454. int i;
  1455. cur->bc_ptrs[0] = keyno;
  1456. error = xfs_btree_increment(cur, 0, &i);
  1457. if (error)
  1458. goto error0;
  1459. XFS_WANT_CORRUPTED_RETURN(i == 1);
  1460. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1461. *stat = 1;
  1462. return 0;
  1463. }
  1464. } else if (dir == XFS_LOOKUP_LE && diff > 0)
  1465. keyno--;
  1466. cur->bc_ptrs[0] = keyno;
  1467. /* Return if we succeeded or not. */
  1468. if (keyno == 0 || keyno > xfs_btree_get_numrecs(block))
  1469. *stat = 0;
  1470. else if (dir != XFS_LOOKUP_EQ || diff == 0)
  1471. *stat = 1;
  1472. else
  1473. *stat = 0;
  1474. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1475. return 0;
  1476. error0:
  1477. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  1478. return error;
  1479. }
  1480. /*
  1481. * Update keys at all levels from here to the root along the cursor's path.
  1482. */
  1483. STATIC int
  1484. xfs_btree_updkey(
  1485. struct xfs_btree_cur *cur,
  1486. union xfs_btree_key *keyp,
  1487. int level)
  1488. {
  1489. struct xfs_btree_block *block;
  1490. struct xfs_buf *bp;
  1491. union xfs_btree_key *kp;
  1492. int ptr;
  1493. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  1494. XFS_BTREE_TRACE_ARGIK(cur, level, keyp);
  1495. ASSERT(!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) || level >= 1);
  1496. /*
  1497. * Go up the tree from this level toward the root.
  1498. * At each level, update the key value to the value input.
  1499. * Stop when we reach a level where the cursor isn't pointing
  1500. * at the first entry in the block.
  1501. */
  1502. for (ptr = 1; ptr == 1 && level < cur->bc_nlevels; level++) {
  1503. #ifdef DEBUG
  1504. int error;
  1505. #endif
  1506. block = xfs_btree_get_block(cur, level, &bp);
  1507. #ifdef DEBUG
  1508. error = xfs_btree_check_block(cur, block, level, bp);
  1509. if (error) {
  1510. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  1511. return error;
  1512. }
  1513. #endif
  1514. ptr = cur->bc_ptrs[level];
  1515. kp = xfs_btree_key_addr(cur, ptr, block);
  1516. xfs_btree_copy_keys(cur, kp, keyp, 1);
  1517. xfs_btree_log_keys(cur, bp, ptr, ptr);
  1518. }
  1519. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1520. return 0;
  1521. }
  1522. /*
  1523. * Update the record referred to by cur to the value in the
  1524. * given record. This either works (return 0) or gets an
  1525. * EFSCORRUPTED error.
  1526. */
  1527. int
  1528. xfs_btree_update(
  1529. struct xfs_btree_cur *cur,
  1530. union xfs_btree_rec *rec)
  1531. {
  1532. struct xfs_btree_block *block;
  1533. struct xfs_buf *bp;
  1534. int error;
  1535. int ptr;
  1536. union xfs_btree_rec *rp;
  1537. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  1538. XFS_BTREE_TRACE_ARGR(cur, rec);
  1539. /* Pick up the current block. */
  1540. block = xfs_btree_get_block(cur, 0, &bp);
  1541. #ifdef DEBUG
  1542. error = xfs_btree_check_block(cur, block, 0, bp);
  1543. if (error)
  1544. goto error0;
  1545. #endif
  1546. /* Get the address of the rec to be updated. */
  1547. ptr = cur->bc_ptrs[0];
  1548. rp = xfs_btree_rec_addr(cur, ptr, block);
  1549. /* Fill in the new contents and log them. */
  1550. xfs_btree_copy_recs(cur, rp, rec, 1);
  1551. xfs_btree_log_recs(cur, bp, ptr, ptr);
  1552. /*
  1553. * If we are tracking the last record in the tree and
  1554. * we are at the far right edge of the tree, update it.
  1555. */
  1556. if (xfs_btree_is_lastrec(cur, block, 0)) {
  1557. cur->bc_ops->update_lastrec(cur, block, rec,
  1558. ptr, LASTREC_UPDATE);
  1559. }
  1560. /* Updating first rec in leaf. Pass new key value up to our parent. */
  1561. if (ptr == 1) {
  1562. union xfs_btree_key key;
  1563. cur->bc_ops->init_key_from_rec(&key, rec);
  1564. error = xfs_btree_updkey(cur, &key, 1);
  1565. if (error)
  1566. goto error0;
  1567. }
  1568. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1569. return 0;
  1570. error0:
  1571. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  1572. return error;
  1573. }
  1574. /*
  1575. * Move 1 record left from cur/level if possible.
  1576. * Update cur to reflect the new path.
  1577. */
  1578. STATIC int /* error */
  1579. xfs_btree_lshift(
  1580. struct xfs_btree_cur *cur,
  1581. int level,
  1582. int *stat) /* success/failure */
  1583. {
  1584. union xfs_btree_key key; /* btree key */
  1585. struct xfs_buf *lbp; /* left buffer pointer */
  1586. struct xfs_btree_block *left; /* left btree block */
  1587. int lrecs; /* left record count */
  1588. struct xfs_buf *rbp; /* right buffer pointer */
  1589. struct xfs_btree_block *right; /* right btree block */
  1590. int rrecs; /* right record count */
  1591. union xfs_btree_ptr lptr; /* left btree pointer */
  1592. union xfs_btree_key *rkp = NULL; /* right btree key */
  1593. union xfs_btree_ptr *rpp = NULL; /* right address pointer */
  1594. union xfs_btree_rec *rrp = NULL; /* right record pointer */
  1595. int error; /* error return value */
  1596. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  1597. XFS_BTREE_TRACE_ARGI(cur, level);
  1598. if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
  1599. level == cur->bc_nlevels - 1)
  1600. goto out0;
  1601. /* Set up variables for this block as "right". */
  1602. right = xfs_btree_get_block(cur, level, &rbp);
  1603. #ifdef DEBUG
  1604. error = xfs_btree_check_block(cur, right, level, rbp);
  1605. if (error)
  1606. goto error0;
  1607. #endif
  1608. /* If we've got no left sibling then we can't shift an entry left. */
  1609. xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
  1610. if (xfs_btree_ptr_is_null(cur, &lptr))
  1611. goto out0;
  1612. /*
  1613. * If the cursor entry is the one that would be moved, don't
  1614. * do it... it's too complicated.
  1615. */
  1616. if (cur->bc_ptrs[level] <= 1)
  1617. goto out0;
  1618. /* Set up the left neighbor as "left". */
  1619. error = xfs_btree_read_buf_block(cur, &lptr, level, 0, &left, &lbp);
  1620. if (error)
  1621. goto error0;
  1622. /* If it's full, it can't take another entry. */
  1623. lrecs = xfs_btree_get_numrecs(left);
  1624. if (lrecs == cur->bc_ops->get_maxrecs(cur, level))
  1625. goto out0;
  1626. rrecs = xfs_btree_get_numrecs(right);
  1627. /*
  1628. * We add one entry to the left side and remove one for the right side.
  1629. * Account for it here, the changes will be updated on disk and logged
  1630. * later.
  1631. */
  1632. lrecs++;
  1633. rrecs--;
  1634. XFS_BTREE_STATS_INC(cur, lshift);
  1635. XFS_BTREE_STATS_ADD(cur, moves, 1);
  1636. /*
  1637. * If non-leaf, copy a key and a ptr to the left block.
  1638. * Log the changes to the left block.
  1639. */
  1640. if (level > 0) {
  1641. /* It's a non-leaf. Move keys and pointers. */
  1642. union xfs_btree_key *lkp; /* left btree key */
  1643. union xfs_btree_ptr *lpp; /* left address pointer */
  1644. lkp = xfs_btree_key_addr(cur, lrecs, left);
  1645. rkp = xfs_btree_key_addr(cur, 1, right);
  1646. lpp = xfs_btree_ptr_addr(cur, lrecs, left);
  1647. rpp = xfs_btree_ptr_addr(cur, 1, right);
  1648. #ifdef DEBUG
  1649. error = xfs_btree_check_ptr(cur, rpp, 0, level);
  1650. if (error)
  1651. goto error0;
  1652. #endif
  1653. xfs_btree_copy_keys(cur, lkp, rkp, 1);
  1654. xfs_btree_copy_ptrs(cur, lpp, rpp, 1);
  1655. xfs_btree_log_keys(cur, lbp, lrecs, lrecs);
  1656. xfs_btree_log_ptrs(cur, lbp, lrecs, lrecs);
  1657. ASSERT(cur->bc_ops->keys_inorder(cur,
  1658. xfs_btree_key_addr(cur, lrecs - 1, left), lkp));
  1659. } else {
  1660. /* It's a leaf. Move records. */
  1661. union xfs_btree_rec *lrp; /* left record pointer */
  1662. lrp = xfs_btree_rec_addr(cur, lrecs, left);
  1663. rrp = xfs_btree_rec_addr(cur, 1, right);
  1664. xfs_btree_copy_recs(cur, lrp, rrp, 1);
  1665. xfs_btree_log_recs(cur, lbp, lrecs, lrecs);
  1666. ASSERT(cur->bc_ops->recs_inorder(cur,
  1667. xfs_btree_rec_addr(cur, lrecs - 1, left), lrp));
  1668. }
  1669. xfs_btree_set_numrecs(left, lrecs);
  1670. xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
  1671. xfs_btree_set_numrecs(right, rrecs);
  1672. xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
  1673. /*
  1674. * Slide the contents of right down one entry.
  1675. */
  1676. XFS_BTREE_STATS_ADD(cur, moves, rrecs - 1);
  1677. if (level > 0) {
  1678. /* It's a nonleaf. operate on keys and ptrs */
  1679. #ifdef DEBUG
  1680. int i; /* loop index */
  1681. for (i = 0; i < rrecs; i++) {
  1682. error = xfs_btree_check_ptr(cur, rpp, i + 1, level);
  1683. if (error)
  1684. goto error0;
  1685. }
  1686. #endif
  1687. xfs_btree_shift_keys(cur,
  1688. xfs_btree_key_addr(cur, 2, right),
  1689. -1, rrecs);
  1690. xfs_btree_shift_ptrs(cur,
  1691. xfs_btree_ptr_addr(cur, 2, right),
  1692. -1, rrecs);
  1693. xfs_btree_log_keys(cur, rbp, 1, rrecs);
  1694. xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
  1695. } else {
  1696. /* It's a leaf. operate on records */
  1697. xfs_btree_shift_recs(cur,
  1698. xfs_btree_rec_addr(cur, 2, right),
  1699. -1, rrecs);
  1700. xfs_btree_log_recs(cur, rbp, 1, rrecs);
  1701. /*
  1702. * If it's the first record in the block, we'll need a key
  1703. * structure to pass up to the next level (updkey).
  1704. */
  1705. cur->bc_ops->init_key_from_rec(&key,
  1706. xfs_btree_rec_addr(cur, 1, right));
  1707. rkp = &key;
  1708. }
  1709. /* Update the parent key values of right. */
  1710. error = xfs_btree_updkey(cur, rkp, level + 1);
  1711. if (error)
  1712. goto error0;
  1713. /* Slide the cursor value left one. */
  1714. cur->bc_ptrs[level]--;
  1715. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1716. *stat = 1;
  1717. return 0;
  1718. out0:
  1719. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1720. *stat = 0;
  1721. return 0;
  1722. error0:
  1723. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  1724. return error;
  1725. }
  1726. /*
  1727. * Move 1 record right from cur/level if possible.
  1728. * Update cur to reflect the new path.
  1729. */
  1730. STATIC int /* error */
  1731. xfs_btree_rshift(
  1732. struct xfs_btree_cur *cur,
  1733. int level,
  1734. int *stat) /* success/failure */
  1735. {
  1736. union xfs_btree_key key; /* btree key */
  1737. struct xfs_buf *lbp; /* left buffer pointer */
  1738. struct xfs_btree_block *left; /* left btree block */
  1739. struct xfs_buf *rbp; /* right buffer pointer */
  1740. struct xfs_btree_block *right; /* right btree block */
  1741. struct xfs_btree_cur *tcur; /* temporary btree cursor */
  1742. union xfs_btree_ptr rptr; /* right block pointer */
  1743. union xfs_btree_key *rkp; /* right btree key */
  1744. int rrecs; /* right record count */
  1745. int lrecs; /* left record count */
  1746. int error; /* error return value */
  1747. int i; /* loop counter */
  1748. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  1749. XFS_BTREE_TRACE_ARGI(cur, level);
  1750. if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
  1751. (level == cur->bc_nlevels - 1))
  1752. goto out0;
  1753. /* Set up variables for this block as "left". */
  1754. left = xfs_btree_get_block(cur, level, &lbp);
  1755. #ifdef DEBUG
  1756. error = xfs_btree_check_block(cur, left, level, lbp);
  1757. if (error)
  1758. goto error0;
  1759. #endif
  1760. /* If we've got no right sibling then we can't shift an entry right. */
  1761. xfs_btree_get_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
  1762. if (xfs_btree_ptr_is_null(cur, &rptr))
  1763. goto out0;
  1764. /*
  1765. * If the cursor entry is the one that would be moved, don't
  1766. * do it... it's too complicated.
  1767. */
  1768. lrecs = xfs_btree_get_numrecs(left);
  1769. if (cur->bc_ptrs[level] >= lrecs)
  1770. goto out0;
  1771. /* Set up the right neighbor as "right". */
  1772. error = xfs_btree_read_buf_block(cur, &rptr, level, 0, &right, &rbp);
  1773. if (error)
  1774. goto error0;
  1775. /* If it's full, it can't take another entry. */
  1776. rrecs = xfs_btree_get_numrecs(right);
  1777. if (rrecs == cur->bc_ops->get_maxrecs(cur, level))
  1778. goto out0;
  1779. XFS_BTREE_STATS_INC(cur, rshift);
  1780. XFS_BTREE_STATS_ADD(cur, moves, rrecs);
  1781. /*
  1782. * Make a hole at the start of the right neighbor block, then
  1783. * copy the last left block entry to the hole.
  1784. */
  1785. if (level > 0) {
  1786. /* It's a nonleaf. make a hole in the keys and ptrs */
  1787. union xfs_btree_key *lkp;
  1788. union xfs_btree_ptr *lpp;
  1789. union xfs_btree_ptr *rpp;
  1790. lkp = xfs_btree_key_addr(cur, lrecs, left);
  1791. lpp = xfs_btree_ptr_addr(cur, lrecs, left);
  1792. rkp = xfs_btree_key_addr(cur, 1, right);
  1793. rpp = xfs_btree_ptr_addr(cur, 1, right);
  1794. #ifdef DEBUG
  1795. for (i = rrecs - 1; i >= 0; i--) {
  1796. error = xfs_btree_check_ptr(cur, rpp, i, level);
  1797. if (error)
  1798. goto error0;
  1799. }
  1800. #endif
  1801. xfs_btree_shift_keys(cur, rkp, 1, rrecs);
  1802. xfs_btree_shift_ptrs(cur, rpp, 1, rrecs);
  1803. #ifdef DEBUG
  1804. error = xfs_btree_check_ptr(cur, lpp, 0, level);
  1805. if (error)
  1806. goto error0;
  1807. #endif
  1808. /* Now put the new data in, and log it. */
  1809. xfs_btree_copy_keys(cur, rkp, lkp, 1);
  1810. xfs_btree_copy_ptrs(cur, rpp, lpp, 1);
  1811. xfs_btree_log_keys(cur, rbp, 1, rrecs + 1);
  1812. xfs_btree_log_ptrs(cur, rbp, 1, rrecs + 1);
  1813. ASSERT(cur->bc_ops->keys_inorder(cur, rkp,
  1814. xfs_btree_key_addr(cur, 2, right)));
  1815. } else {
  1816. /* It's a leaf. make a hole in the records */
  1817. union xfs_btree_rec *lrp;
  1818. union xfs_btree_rec *rrp;
  1819. lrp = xfs_btree_rec_addr(cur, lrecs, left);
  1820. rrp = xfs_btree_rec_addr(cur, 1, right);
  1821. xfs_btree_shift_recs(cur, rrp, 1, rrecs);
  1822. /* Now put the new data in, and log it. */
  1823. xfs_btree_copy_recs(cur, rrp, lrp, 1);
  1824. xfs_btree_log_recs(cur, rbp, 1, rrecs + 1);
  1825. cur->bc_ops->init_key_from_rec(&key, rrp);
  1826. rkp = &key;
  1827. ASSERT(cur->bc_ops->recs_inorder(cur, rrp,
  1828. xfs_btree_rec_addr(cur, 2, right)));
  1829. }
  1830. /*
  1831. * Decrement and log left's numrecs, bump and log right's numrecs.
  1832. */
  1833. xfs_btree_set_numrecs(left, --lrecs);
  1834. xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
  1835. xfs_btree_set_numrecs(right, ++rrecs);
  1836. xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
  1837. /*
  1838. * Using a temporary cursor, update the parent key values of the
  1839. * block on the right.
  1840. */
  1841. error = xfs_btree_dup_cursor(cur, &tcur);
  1842. if (error)
  1843. goto error0;
  1844. i = xfs_btree_lastrec(tcur, level);
  1845. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  1846. error = xfs_btree_increment(tcur, level, &i);
  1847. if (error)
  1848. goto error1;
  1849. error = xfs_btree_updkey(tcur, rkp, level + 1);
  1850. if (error)
  1851. goto error1;
  1852. xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
  1853. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1854. *stat = 1;
  1855. return 0;
  1856. out0:
  1857. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1858. *stat = 0;
  1859. return 0;
  1860. error0:
  1861. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  1862. return error;
  1863. error1:
  1864. XFS_BTREE_TRACE_CURSOR(tcur, XBT_ERROR);
  1865. xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
  1866. return error;
  1867. }
  1868. /*
  1869. * Split cur/level block in half.
  1870. * Return new block number and the key to its first
  1871. * record (to be inserted into parent).
  1872. */
  1873. STATIC int /* error */
  1874. xfs_btree_split(
  1875. struct xfs_btree_cur *cur,
  1876. int level,
  1877. union xfs_btree_ptr *ptrp,
  1878. union xfs_btree_key *key,
  1879. struct xfs_btree_cur **curp,
  1880. int *stat) /* success/failure */
  1881. {
  1882. union xfs_btree_ptr lptr; /* left sibling block ptr */
  1883. struct xfs_buf *lbp; /* left buffer pointer */
  1884. struct xfs_btree_block *left; /* left btree block */
  1885. union xfs_btree_ptr rptr; /* right sibling block ptr */
  1886. struct xfs_buf *rbp; /* right buffer pointer */
  1887. struct xfs_btree_block *right; /* right btree block */
  1888. union xfs_btree_ptr rrptr; /* right-right sibling ptr */
  1889. struct xfs_buf *rrbp; /* right-right buffer pointer */
  1890. struct xfs_btree_block *rrblock; /* right-right btree block */
  1891. int lrecs;
  1892. int rrecs;
  1893. int src_index;
  1894. int error; /* error return value */
  1895. #ifdef DEBUG
  1896. int i;
  1897. #endif
  1898. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  1899. XFS_BTREE_TRACE_ARGIPK(cur, level, *ptrp, key);
  1900. XFS_BTREE_STATS_INC(cur, split);
  1901. /* Set up left block (current one). */
  1902. left = xfs_btree_get_block(cur, level, &lbp);
  1903. #ifdef DEBUG
  1904. error = xfs_btree_check_block(cur, left, level, lbp);
  1905. if (error)
  1906. goto error0;
  1907. #endif
  1908. xfs_btree_buf_to_ptr(cur, lbp, &lptr);
  1909. /* Allocate the new block. If we can't do it, we're toast. Give up. */
  1910. error = cur->bc_ops->alloc_block(cur, &lptr, &rptr, 1, stat);
  1911. if (error)
  1912. goto error0;
  1913. if (*stat == 0)
  1914. goto out0;
  1915. XFS_BTREE_STATS_INC(cur, alloc);
  1916. /* Set up the new block as "right". */
  1917. error = xfs_btree_get_buf_block(cur, &rptr, 0, &right, &rbp);
  1918. if (error)
  1919. goto error0;
  1920. /* Fill in the btree header for the new right block. */
  1921. xfs_btree_init_block(cur, xfs_btree_get_level(left), 0, right);
  1922. /*
  1923. * Split the entries between the old and the new block evenly.
  1924. * Make sure that if there's an odd number of entries now, that
  1925. * each new block will have the same number of entries.
  1926. */
  1927. lrecs = xfs_btree_get_numrecs(left);
  1928. rrecs = lrecs / 2;
  1929. if ((lrecs & 1) && cur->bc_ptrs[level] <= rrecs + 1)
  1930. rrecs++;
  1931. src_index = (lrecs - rrecs + 1);
  1932. XFS_BTREE_STATS_ADD(cur, moves, rrecs);
  1933. /*
  1934. * Copy btree block entries from the left block over to the
  1935. * new block, the right. Update the right block and log the
  1936. * changes.
  1937. */
  1938. if (level > 0) {
  1939. /* It's a non-leaf. Move keys and pointers. */
  1940. union xfs_btree_key *lkp; /* left btree key */
  1941. union xfs_btree_ptr *lpp; /* left address pointer */
  1942. union xfs_btree_key *rkp; /* right btree key */
  1943. union xfs_btree_ptr *rpp; /* right address pointer */
  1944. lkp = xfs_btree_key_addr(cur, src_index, left);
  1945. lpp = xfs_btree_ptr_addr(cur, src_index, left);
  1946. rkp = xfs_btree_key_addr(cur, 1, right);
  1947. rpp = xfs_btree_ptr_addr(cur, 1, right);
  1948. #ifdef DEBUG
  1949. for (i = src_index; i < rrecs; i++) {
  1950. error = xfs_btree_check_ptr(cur, lpp, i, level);
  1951. if (error)
  1952. goto error0;
  1953. }
  1954. #endif
  1955. xfs_btree_copy_keys(cur, rkp, lkp, rrecs);
  1956. xfs_btree_copy_ptrs(cur, rpp, lpp, rrecs);
  1957. xfs_btree_log_keys(cur, rbp, 1, rrecs);
  1958. xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
  1959. /* Grab the keys to the entries moved to the right block */
  1960. xfs_btree_copy_keys(cur, key, rkp, 1);
  1961. } else {
  1962. /* It's a leaf. Move records. */
  1963. union xfs_btree_rec *lrp; /* left record pointer */
  1964. union xfs_btree_rec *rrp; /* right record pointer */
  1965. lrp = xfs_btree_rec_addr(cur, src_index, left);
  1966. rrp = xfs_btree_rec_addr(cur, 1, right);
  1967. xfs_btree_copy_recs(cur, rrp, lrp, rrecs);
  1968. xfs_btree_log_recs(cur, rbp, 1, rrecs);
  1969. cur->bc_ops->init_key_from_rec(key,
  1970. xfs_btree_rec_addr(cur, 1, right));
  1971. }
  1972. /*
  1973. * Find the left block number by looking in the buffer.
  1974. * Adjust numrecs, sibling pointers.
  1975. */
  1976. xfs_btree_get_sibling(cur, left, &rrptr, XFS_BB_RIGHTSIB);
  1977. xfs_btree_set_sibling(cur, right, &rrptr, XFS_BB_RIGHTSIB);
  1978. xfs_btree_set_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
  1979. xfs_btree_set_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
  1980. lrecs -= rrecs;
  1981. xfs_btree_set_numrecs(left, lrecs);
  1982. xfs_btree_set_numrecs(right, xfs_btree_get_numrecs(right) + rrecs);
  1983. xfs_btree_log_block(cur, rbp, XFS_BB_ALL_BITS);
  1984. xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
  1985. /*
  1986. * If there's a block to the new block's right, make that block
  1987. * point back to right instead of to left.
  1988. */
  1989. if (!xfs_btree_ptr_is_null(cur, &rrptr)) {
  1990. error = xfs_btree_read_buf_block(cur, &rrptr, level,
  1991. 0, &rrblock, &rrbp);
  1992. if (error)
  1993. goto error0;
  1994. xfs_btree_set_sibling(cur, rrblock, &rptr, XFS_BB_LEFTSIB);
  1995. xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
  1996. }
  1997. /*
  1998. * If the cursor is really in the right block, move it there.
  1999. * If it's just pointing past the last entry in left, then we'll
  2000. * insert there, so don't change anything in that case.
  2001. */
  2002. if (cur->bc_ptrs[level] > lrecs + 1) {
  2003. xfs_btree_setbuf(cur, level, rbp);
  2004. cur->bc_ptrs[level] -= lrecs;
  2005. }
  2006. /*
  2007. * If there are more levels, we'll need another cursor which refers
  2008. * the right block, no matter where this cursor was.
  2009. */
  2010. if (level + 1 < cur->bc_nlevels) {
  2011. error = xfs_btree_dup_cursor(cur, curp);
  2012. if (error)
  2013. goto error0;
  2014. (*curp)->bc_ptrs[level + 1]++;
  2015. }
  2016. *ptrp = rptr;
  2017. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2018. *stat = 1;
  2019. return 0;
  2020. out0:
  2021. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2022. *stat = 0;
  2023. return 0;
  2024. error0:
  2025. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  2026. return error;
  2027. }
  2028. /*
  2029. * Copy the old inode root contents into a real block and make the
  2030. * broot point to it.
  2031. */
  2032. int /* error */
  2033. xfs_btree_new_iroot(
  2034. struct xfs_btree_cur *cur, /* btree cursor */
  2035. int *logflags, /* logging flags for inode */
  2036. int *stat) /* return status - 0 fail */
  2037. {
  2038. struct xfs_buf *cbp; /* buffer for cblock */
  2039. struct xfs_btree_block *block; /* btree block */
  2040. struct xfs_btree_block *cblock; /* child btree block */
  2041. union xfs_btree_key *ckp; /* child key pointer */
  2042. union xfs_btree_ptr *cpp; /* child ptr pointer */
  2043. union xfs_btree_key *kp; /* pointer to btree key */
  2044. union xfs_btree_ptr *pp; /* pointer to block addr */
  2045. union xfs_btree_ptr nptr; /* new block addr */
  2046. int level; /* btree level */
  2047. int error; /* error return code */
  2048. #ifdef DEBUG
  2049. int i; /* loop counter */
  2050. #endif
  2051. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  2052. XFS_BTREE_STATS_INC(cur, newroot);
  2053. ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
  2054. level = cur->bc_nlevels - 1;
  2055. block = xfs_btree_get_iroot(cur);
  2056. pp = xfs_btree_ptr_addr(cur, 1, block);
  2057. /* Allocate the new block. If we can't do it, we're toast. Give up. */
  2058. error = cur->bc_ops->alloc_block(cur, pp, &nptr, 1, stat);
  2059. if (error)
  2060. goto error0;
  2061. if (*stat == 0) {
  2062. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2063. return 0;
  2064. }
  2065. XFS_BTREE_STATS_INC(cur, alloc);
  2066. /* Copy the root into a real block. */
  2067. error = xfs_btree_get_buf_block(cur, &nptr, 0, &cblock, &cbp);
  2068. if (error)
  2069. goto error0;
  2070. memcpy(cblock, block, xfs_btree_block_len(cur));
  2071. be16_add_cpu(&block->bb_level, 1);
  2072. xfs_btree_set_numrecs(block, 1);
  2073. cur->bc_nlevels++;
  2074. cur->bc_ptrs[level + 1] = 1;
  2075. kp = xfs_btree_key_addr(cur, 1, block);
  2076. ckp = xfs_btree_key_addr(cur, 1, cblock);
  2077. xfs_btree_copy_keys(cur, ckp, kp, xfs_btree_get_numrecs(cblock));
  2078. cpp = xfs_btree_ptr_addr(cur, 1, cblock);
  2079. #ifdef DEBUG
  2080. for (i = 0; i < be16_to_cpu(cblock->bb_numrecs); i++) {
  2081. error = xfs_btree_check_ptr(cur, pp, i, level);
  2082. if (error)
  2083. goto error0;
  2084. }
  2085. #endif
  2086. xfs_btree_copy_ptrs(cur, cpp, pp, xfs_btree_get_numrecs(cblock));
  2087. #ifdef DEBUG
  2088. error = xfs_btree_check_ptr(cur, &nptr, 0, level);
  2089. if (error)
  2090. goto error0;
  2091. #endif
  2092. xfs_btree_copy_ptrs(cur, pp, &nptr, 1);
  2093. xfs_iroot_realloc(cur->bc_private.b.ip,
  2094. 1 - xfs_btree_get_numrecs(cblock),
  2095. cur->bc_private.b.whichfork);
  2096. xfs_btree_setbuf(cur, level, cbp);
  2097. /*
  2098. * Do all this logging at the end so that
  2099. * the root is at the right level.
  2100. */
  2101. xfs_btree_log_block(cur, cbp, XFS_BB_ALL_BITS);
  2102. xfs_btree_log_keys(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
  2103. xfs_btree_log_ptrs(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
  2104. *logflags |=
  2105. XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork);
  2106. *stat = 1;
  2107. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2108. return 0;
  2109. error0:
  2110. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  2111. return error;
  2112. }
  2113. /*
  2114. * Allocate a new root block, fill it in.
  2115. */
  2116. STATIC int /* error */
  2117. xfs_btree_new_root(
  2118. struct xfs_btree_cur *cur, /* btree cursor */
  2119. int *stat) /* success/failure */
  2120. {
  2121. struct xfs_btree_block *block; /* one half of the old root block */
  2122. struct xfs_buf *bp; /* buffer containing block */
  2123. int error; /* error return value */
  2124. struct xfs_buf *lbp; /* left buffer pointer */
  2125. struct xfs_btree_block *left; /* left btree block */
  2126. struct xfs_buf *nbp; /* new (root) buffer */
  2127. struct xfs_btree_block *new; /* new (root) btree block */
  2128. int nptr; /* new value for key index, 1 or 2 */
  2129. struct xfs_buf *rbp; /* right buffer pointer */
  2130. struct xfs_btree_block *right; /* right btree block */
  2131. union xfs_btree_ptr rptr;
  2132. union xfs_btree_ptr lptr;
  2133. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  2134. XFS_BTREE_STATS_INC(cur, newroot);
  2135. /* initialise our start point from the cursor */
  2136. cur->bc_ops->init_ptr_from_cur(cur, &rptr);
  2137. /* Allocate the new block. If we can't do it, we're toast. Give up. */
  2138. error = cur->bc_ops->alloc_block(cur, &rptr, &lptr, 1, stat);
  2139. if (error)
  2140. goto error0;
  2141. if (*stat == 0)
  2142. goto out0;
  2143. XFS_BTREE_STATS_INC(cur, alloc);
  2144. /* Set up the new block. */
  2145. error = xfs_btree_get_buf_block(cur, &lptr, 0, &new, &nbp);
  2146. if (error)
  2147. goto error0;
  2148. /* Set the root in the holding structure increasing the level by 1. */
  2149. cur->bc_ops->set_root(cur, &lptr, 1);
  2150. /*
  2151. * At the previous root level there are now two blocks: the old root,
  2152. * and the new block generated when it was split. We don't know which
  2153. * one the cursor is pointing at, so we set up variables "left" and
  2154. * "right" for each case.
  2155. */
  2156. block = xfs_btree_get_block(cur, cur->bc_nlevels - 1, &bp);
  2157. #ifdef DEBUG
  2158. error = xfs_btree_check_block(cur, block, cur->bc_nlevels - 1, bp);
  2159. if (error)
  2160. goto error0;
  2161. #endif
  2162. xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
  2163. if (!xfs_btree_ptr_is_null(cur, &rptr)) {
  2164. /* Our block is left, pick up the right block. */
  2165. lbp = bp;
  2166. xfs_btree_buf_to_ptr(cur, lbp, &lptr);
  2167. left = block;
  2168. error = xfs_btree_read_buf_block(cur, &rptr,
  2169. cur->bc_nlevels - 1, 0, &right, &rbp);
  2170. if (error)
  2171. goto error0;
  2172. bp = rbp;
  2173. nptr = 1;
  2174. } else {
  2175. /* Our block is right, pick up the left block. */
  2176. rbp = bp;
  2177. xfs_btree_buf_to_ptr(cur, rbp, &rptr);
  2178. right = block;
  2179. xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
  2180. error = xfs_btree_read_buf_block(cur, &lptr,
  2181. cur->bc_nlevels - 1, 0, &left, &lbp);
  2182. if (error)
  2183. goto error0;
  2184. bp = lbp;
  2185. nptr = 2;
  2186. }
  2187. /* Fill in the new block's btree header and log it. */
  2188. xfs_btree_init_block(cur, cur->bc_nlevels, 2, new);
  2189. xfs_btree_log_block(cur, nbp, XFS_BB_ALL_BITS);
  2190. ASSERT(!xfs_btree_ptr_is_null(cur, &lptr) &&
  2191. !xfs_btree_ptr_is_null(cur, &rptr));
  2192. /* Fill in the key data in the new root. */
  2193. if (xfs_btree_get_level(left) > 0) {
  2194. xfs_btree_copy_keys(cur,
  2195. xfs_btree_key_addr(cur, 1, new),
  2196. xfs_btree_key_addr(cur, 1, left), 1);
  2197. xfs_btree_copy_keys(cur,
  2198. xfs_btree_key_addr(cur, 2, new),
  2199. xfs_btree_key_addr(cur, 1, right), 1);
  2200. } else {
  2201. cur->bc_ops->init_key_from_rec(
  2202. xfs_btree_key_addr(cur, 1, new),
  2203. xfs_btree_rec_addr(cur, 1, left));
  2204. cur->bc_ops->init_key_from_rec(
  2205. xfs_btree_key_addr(cur, 2, new),
  2206. xfs_btree_rec_addr(cur, 1, right));
  2207. }
  2208. xfs_btree_log_keys(cur, nbp, 1, 2);
  2209. /* Fill in the pointer data in the new root. */
  2210. xfs_btree_copy_ptrs(cur,
  2211. xfs_btree_ptr_addr(cur, 1, new), &lptr, 1);
  2212. xfs_btree_copy_ptrs(cur,
  2213. xfs_btree_ptr_addr(cur, 2, new), &rptr, 1);
  2214. xfs_btree_log_ptrs(cur, nbp, 1, 2);
  2215. /* Fix up the cursor. */
  2216. xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
  2217. cur->bc_ptrs[cur->bc_nlevels] = nptr;
  2218. cur->bc_nlevels++;
  2219. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2220. *stat = 1;
  2221. return 0;
  2222. error0:
  2223. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  2224. return error;
  2225. out0:
  2226. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2227. *stat = 0;
  2228. return 0;
  2229. }
  2230. STATIC int
  2231. xfs_btree_make_block_unfull(
  2232. struct xfs_btree_cur *cur, /* btree cursor */
  2233. int level, /* btree level */
  2234. int numrecs,/* # of recs in block */
  2235. int *oindex,/* old tree index */
  2236. int *index, /* new tree index */
  2237. union xfs_btree_ptr *nptr, /* new btree ptr */
  2238. struct xfs_btree_cur **ncur, /* new btree cursor */
  2239. union xfs_btree_rec *nrec, /* new record */
  2240. int *stat)
  2241. {
  2242. union xfs_btree_key key; /* new btree key value */
  2243. int error = 0;
  2244. if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
  2245. level == cur->bc_nlevels - 1) {
  2246. struct xfs_inode *ip = cur->bc_private.b.ip;
  2247. if (numrecs < cur->bc_ops->get_dmaxrecs(cur, level)) {
  2248. /* A root block that can be made bigger. */
  2249. xfs_iroot_realloc(ip, 1, cur->bc_private.b.whichfork);
  2250. } else {
  2251. /* A root block that needs replacing */
  2252. int logflags = 0;
  2253. error = xfs_btree_new_iroot(cur, &logflags, stat);
  2254. if (error || *stat == 0)
  2255. return error;
  2256. xfs_trans_log_inode(cur->bc_tp, ip, logflags);
  2257. }
  2258. return 0;
  2259. }
  2260. /* First, try shifting an entry to the right neighbor. */
  2261. error = xfs_btree_rshift(cur, level, stat);
  2262. if (error || *stat)
  2263. return error;
  2264. /* Next, try shifting an entry to the left neighbor. */
  2265. error = xfs_btree_lshift(cur, level, stat);
  2266. if (error)
  2267. return error;
  2268. if (*stat) {
  2269. *oindex = *index = cur->bc_ptrs[level];
  2270. return 0;
  2271. }
  2272. /*
  2273. * Next, try splitting the current block in half.
  2274. *
  2275. * If this works we have to re-set our variables because we
  2276. * could be in a different block now.
  2277. */
  2278. error = xfs_btree_split(cur, level, nptr, &key, ncur, stat);
  2279. if (error || *stat == 0)
  2280. return error;
  2281. *index = cur->bc_ptrs[level];
  2282. cur->bc_ops->init_rec_from_key(&key, nrec);
  2283. return 0;
  2284. }
  2285. /*
  2286. * Insert one record/level. Return information to the caller
  2287. * allowing the next level up to proceed if necessary.
  2288. */
  2289. STATIC int
  2290. xfs_btree_insrec(
  2291. struct xfs_btree_cur *cur, /* btree cursor */
  2292. int level, /* level to insert record at */
  2293. union xfs_btree_ptr *ptrp, /* i/o: block number inserted */
  2294. union xfs_btree_rec *recp, /* i/o: record data inserted */
  2295. struct xfs_btree_cur **curp, /* output: new cursor replacing cur */
  2296. int *stat) /* success/failure */
  2297. {
  2298. struct xfs_btree_block *block; /* btree block */
  2299. struct xfs_buf *bp; /* buffer for block */
  2300. union xfs_btree_key key; /* btree key */
  2301. union xfs_btree_ptr nptr; /* new block ptr */
  2302. struct xfs_btree_cur *ncur; /* new btree cursor */
  2303. union xfs_btree_rec nrec; /* new record count */
  2304. int optr; /* old key/record index */
  2305. int ptr; /* key/record index */
  2306. int numrecs;/* number of records */
  2307. int error; /* error return value */
  2308. #ifdef DEBUG
  2309. int i;
  2310. #endif
  2311. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  2312. XFS_BTREE_TRACE_ARGIPR(cur, level, *ptrp, recp);
  2313. ncur = NULL;
  2314. /*
  2315. * If we have an external root pointer, and we've made it to the
  2316. * root level, allocate a new root block and we're done.
  2317. */
  2318. if (!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
  2319. (level >= cur->bc_nlevels)) {
  2320. error = xfs_btree_new_root(cur, stat);
  2321. xfs_btree_set_ptr_null(cur, ptrp);
  2322. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2323. return error;
  2324. }
  2325. /* If we're off the left edge, return failure. */
  2326. ptr = cur->bc_ptrs[level];
  2327. if (ptr == 0) {
  2328. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2329. *stat = 0;
  2330. return 0;
  2331. }
  2332. /* Make a key out of the record data to be inserted, and save it. */
  2333. cur->bc_ops->init_key_from_rec(&key, recp);
  2334. optr = ptr;
  2335. XFS_BTREE_STATS_INC(cur, insrec);
  2336. /* Get pointers to the btree buffer and block. */
  2337. block = xfs_btree_get_block(cur, level, &bp);
  2338. numrecs = xfs_btree_get_numrecs(block);
  2339. #ifdef DEBUG
  2340. error = xfs_btree_check_block(cur, block, level, bp);
  2341. if (error)
  2342. goto error0;
  2343. /* Check that the new entry is being inserted in the right place. */
  2344. if (ptr <= numrecs) {
  2345. if (level == 0) {
  2346. ASSERT(cur->bc_ops->recs_inorder(cur, recp,
  2347. xfs_btree_rec_addr(cur, ptr, block)));
  2348. } else {
  2349. ASSERT(cur->bc_ops->keys_inorder(cur, &key,
  2350. xfs_btree_key_addr(cur, ptr, block)));
  2351. }
  2352. }
  2353. #endif
  2354. /*
  2355. * If the block is full, we can't insert the new entry until we
  2356. * make the block un-full.
  2357. */
  2358. xfs_btree_set_ptr_null(cur, &nptr);
  2359. if (numrecs == cur->bc_ops->get_maxrecs(cur, level)) {
  2360. error = xfs_btree_make_block_unfull(cur, level, numrecs,
  2361. &optr, &ptr, &nptr, &ncur, &nrec, stat);
  2362. if (error || *stat == 0)
  2363. goto error0;
  2364. }
  2365. /*
  2366. * The current block may have changed if the block was
  2367. * previously full and we have just made space in it.
  2368. */
  2369. block = xfs_btree_get_block(cur, level, &bp);
  2370. numrecs = xfs_btree_get_numrecs(block);
  2371. #ifdef DEBUG
  2372. error = xfs_btree_check_block(cur, block, level, bp);
  2373. if (error)
  2374. return error;
  2375. #endif
  2376. /*
  2377. * At this point we know there's room for our new entry in the block
  2378. * we're pointing at.
  2379. */
  2380. XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr + 1);
  2381. if (level > 0) {
  2382. /* It's a nonleaf. make a hole in the keys and ptrs */
  2383. union xfs_btree_key *kp;
  2384. union xfs_btree_ptr *pp;
  2385. kp = xfs_btree_key_addr(cur, ptr, block);
  2386. pp = xfs_btree_ptr_addr(cur, ptr, block);
  2387. #ifdef DEBUG
  2388. for (i = numrecs - ptr; i >= 0; i--) {
  2389. error = xfs_btree_check_ptr(cur, pp, i, level);
  2390. if (error)
  2391. return error;
  2392. }
  2393. #endif
  2394. xfs_btree_shift_keys(cur, kp, 1, numrecs - ptr + 1);
  2395. xfs_btree_shift_ptrs(cur, pp, 1, numrecs - ptr + 1);
  2396. #ifdef DEBUG
  2397. error = xfs_btree_check_ptr(cur, ptrp, 0, level);
  2398. if (error)
  2399. goto error0;
  2400. #endif
  2401. /* Now put the new data in, bump numrecs and log it. */
  2402. xfs_btree_copy_keys(cur, kp, &key, 1);
  2403. xfs_btree_copy_ptrs(cur, pp, ptrp, 1);
  2404. numrecs++;
  2405. xfs_btree_set_numrecs(block, numrecs);
  2406. xfs_btree_log_ptrs(cur, bp, ptr, numrecs);
  2407. xfs_btree_log_keys(cur, bp, ptr, numrecs);
  2408. #ifdef DEBUG
  2409. if (ptr < numrecs) {
  2410. ASSERT(cur->bc_ops->keys_inorder(cur, kp,
  2411. xfs_btree_key_addr(cur, ptr + 1, block)));
  2412. }
  2413. #endif
  2414. } else {
  2415. /* It's a leaf. make a hole in the records */
  2416. union xfs_btree_rec *rp;
  2417. rp = xfs_btree_rec_addr(cur, ptr, block);
  2418. xfs_btree_shift_recs(cur, rp, 1, numrecs - ptr + 1);
  2419. /* Now put the new data in, bump numrecs and log it. */
  2420. xfs_btree_copy_recs(cur, rp, recp, 1);
  2421. xfs_btree_set_numrecs(block, ++numrecs);
  2422. xfs_btree_log_recs(cur, bp, ptr, numrecs);
  2423. #ifdef DEBUG
  2424. if (ptr < numrecs) {
  2425. ASSERT(cur->bc_ops->recs_inorder(cur, rp,
  2426. xfs_btree_rec_addr(cur, ptr + 1, block)));
  2427. }
  2428. #endif
  2429. }
  2430. /* Log the new number of records in the btree header. */
  2431. xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
  2432. /* If we inserted at the start of a block, update the parents' keys. */
  2433. if (optr == 1) {
  2434. error = xfs_btree_updkey(cur, &key, level + 1);
  2435. if (error)
  2436. goto error0;
  2437. }
  2438. /*
  2439. * If we are tracking the last record in the tree and
  2440. * we are at the far right edge of the tree, update it.
  2441. */
  2442. if (xfs_btree_is_lastrec(cur, block, level)) {
  2443. cur->bc_ops->update_lastrec(cur, block, recp,
  2444. ptr, LASTREC_INSREC);
  2445. }
  2446. /*
  2447. * Return the new block number, if any.
  2448. * If there is one, give back a record value and a cursor too.
  2449. */
  2450. *ptrp = nptr;
  2451. if (!xfs_btree_ptr_is_null(cur, &nptr)) {
  2452. *recp = nrec;
  2453. *curp = ncur;
  2454. }
  2455. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2456. *stat = 1;
  2457. return 0;
  2458. error0:
  2459. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  2460. return error;
  2461. }
  2462. /*
  2463. * Insert the record at the point referenced by cur.
  2464. *
  2465. * A multi-level split of the tree on insert will invalidate the original
  2466. * cursor. All callers of this function should assume that the cursor is
  2467. * no longer valid and revalidate it.
  2468. */
  2469. int
  2470. xfs_btree_insert(
  2471. struct xfs_btree_cur *cur,
  2472. int *stat)
  2473. {
  2474. int error; /* error return value */
  2475. int i; /* result value, 0 for failure */
  2476. int level; /* current level number in btree */
  2477. union xfs_btree_ptr nptr; /* new block number (split result) */
  2478. struct xfs_btree_cur *ncur; /* new cursor (split result) */
  2479. struct xfs_btree_cur *pcur; /* previous level's cursor */
  2480. union xfs_btree_rec rec; /* record to insert */
  2481. level = 0;
  2482. ncur = NULL;
  2483. pcur = cur;
  2484. xfs_btree_set_ptr_null(cur, &nptr);
  2485. cur->bc_ops->init_rec_from_cur(cur, &rec);
  2486. /*
  2487. * Loop going up the tree, starting at the leaf level.
  2488. * Stop when we don't get a split block, that must mean that
  2489. * the insert is finished with this level.
  2490. */
  2491. do {
  2492. /*
  2493. * Insert nrec/nptr into this level of the tree.
  2494. * Note if we fail, nptr will be null.
  2495. */
  2496. error = xfs_btree_insrec(pcur, level, &nptr, &rec, &ncur, &i);
  2497. if (error) {
  2498. if (pcur != cur)
  2499. xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
  2500. goto error0;
  2501. }
  2502. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  2503. level++;
  2504. /*
  2505. * See if the cursor we just used is trash.
  2506. * Can't trash the caller's cursor, but otherwise we should
  2507. * if ncur is a new cursor or we're about to be done.
  2508. */
  2509. if (pcur != cur &&
  2510. (ncur || xfs_btree_ptr_is_null(cur, &nptr))) {
  2511. /* Save the state from the cursor before we trash it */
  2512. if (cur->bc_ops->update_cursor)
  2513. cur->bc_ops->update_cursor(pcur, cur);
  2514. cur->bc_nlevels = pcur->bc_nlevels;
  2515. xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
  2516. }
  2517. /* If we got a new cursor, switch to it. */
  2518. if (ncur) {
  2519. pcur = ncur;
  2520. ncur = NULL;
  2521. }
  2522. } while (!xfs_btree_ptr_is_null(cur, &nptr));
  2523. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2524. *stat = i;
  2525. return 0;
  2526. error0:
  2527. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  2528. return error;
  2529. }
  2530. /*
  2531. * Try to merge a non-leaf block back into the inode root.
  2532. *
  2533. * Note: the killroot names comes from the fact that we're effectively
  2534. * killing the old root block. But because we can't just delete the
  2535. * inode we have to copy the single block it was pointing to into the
  2536. * inode.
  2537. */
  2538. STATIC int
  2539. xfs_btree_kill_iroot(
  2540. struct xfs_btree_cur *cur)
  2541. {
  2542. int whichfork = cur->bc_private.b.whichfork;
  2543. struct xfs_inode *ip = cur->bc_private.b.ip;
  2544. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
  2545. struct xfs_btree_block *block;
  2546. struct xfs_btree_block *cblock;
  2547. union xfs_btree_key *kp;
  2548. union xfs_btree_key *ckp;
  2549. union xfs_btree_ptr *pp;
  2550. union xfs_btree_ptr *cpp;
  2551. struct xfs_buf *cbp;
  2552. int level;
  2553. int index;
  2554. int numrecs;
  2555. #ifdef DEBUG
  2556. union xfs_btree_ptr ptr;
  2557. int i;
  2558. #endif
  2559. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  2560. ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
  2561. ASSERT(cur->bc_nlevels > 1);
  2562. /*
  2563. * Don't deal with the root block needs to be a leaf case.
  2564. * We're just going to turn the thing back into extents anyway.
  2565. */
  2566. level = cur->bc_nlevels - 1;
  2567. if (level == 1)
  2568. goto out0;
  2569. /*
  2570. * Give up if the root has multiple children.
  2571. */
  2572. block = xfs_btree_get_iroot(cur);
  2573. if (xfs_btree_get_numrecs(block) != 1)
  2574. goto out0;
  2575. cblock = xfs_btree_get_block(cur, level - 1, &cbp);
  2576. numrecs = xfs_btree_get_numrecs(cblock);
  2577. /*
  2578. * Only do this if the next level will fit.
  2579. * Then the data must be copied up to the inode,
  2580. * instead of freeing the root you free the next level.
  2581. */
  2582. if (numrecs > cur->bc_ops->get_dmaxrecs(cur, level))
  2583. goto out0;
  2584. XFS_BTREE_STATS_INC(cur, killroot);
  2585. #ifdef DEBUG
  2586. xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
  2587. ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
  2588. xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
  2589. ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
  2590. #endif
  2591. index = numrecs - cur->bc_ops->get_maxrecs(cur, level);
  2592. if (index) {
  2593. xfs_iroot_realloc(cur->bc_private.b.ip, index,
  2594. cur->bc_private.b.whichfork);
  2595. block = ifp->if_broot;
  2596. }
  2597. be16_add_cpu(&block->bb_numrecs, index);
  2598. ASSERT(block->bb_numrecs == cblock->bb_numrecs);
  2599. kp = xfs_btree_key_addr(cur, 1, block);
  2600. ckp = xfs_btree_key_addr(cur, 1, cblock);
  2601. xfs_btree_copy_keys(cur, kp, ckp, numrecs);
  2602. pp = xfs_btree_ptr_addr(cur, 1, block);
  2603. cpp = xfs_btree_ptr_addr(cur, 1, cblock);
  2604. #ifdef DEBUG
  2605. for (i = 0; i < numrecs; i++) {
  2606. int error;
  2607. error = xfs_btree_check_ptr(cur, cpp, i, level - 1);
  2608. if (error) {
  2609. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  2610. return error;
  2611. }
  2612. }
  2613. #endif
  2614. xfs_btree_copy_ptrs(cur, pp, cpp, numrecs);
  2615. cur->bc_ops->free_block(cur, cbp);
  2616. XFS_BTREE_STATS_INC(cur, free);
  2617. cur->bc_bufs[level - 1] = NULL;
  2618. be16_add_cpu(&block->bb_level, -1);
  2619. xfs_trans_log_inode(cur->bc_tp, ip,
  2620. XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork));
  2621. cur->bc_nlevels--;
  2622. out0:
  2623. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2624. return 0;
  2625. }
  2626. /*
  2627. * Kill the current root node, and replace it with it's only child node.
  2628. */
  2629. STATIC int
  2630. xfs_btree_kill_root(
  2631. struct xfs_btree_cur *cur,
  2632. struct xfs_buf *bp,
  2633. int level,
  2634. union xfs_btree_ptr *newroot)
  2635. {
  2636. int error;
  2637. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  2638. XFS_BTREE_STATS_INC(cur, killroot);
  2639. /*
  2640. * Update the root pointer, decreasing the level by 1 and then
  2641. * free the old root.
  2642. */
  2643. cur->bc_ops->set_root(cur, newroot, -1);
  2644. error = cur->bc_ops->free_block(cur, bp);
  2645. if (error) {
  2646. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  2647. return error;
  2648. }
  2649. XFS_BTREE_STATS_INC(cur, free);
  2650. cur->bc_bufs[level] = NULL;
  2651. cur->bc_ra[level] = 0;
  2652. cur->bc_nlevels--;
  2653. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2654. return 0;
  2655. }
  2656. STATIC int
  2657. xfs_btree_dec_cursor(
  2658. struct xfs_btree_cur *cur,
  2659. int level,
  2660. int *stat)
  2661. {
  2662. int error;
  2663. int i;
  2664. if (level > 0) {
  2665. error = xfs_btree_decrement(cur, level, &i);
  2666. if (error)
  2667. return error;
  2668. }
  2669. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2670. *stat = 1;
  2671. return 0;
  2672. }
  2673. /*
  2674. * Single level of the btree record deletion routine.
  2675. * Delete record pointed to by cur/level.
  2676. * Remove the record from its block then rebalance the tree.
  2677. * Return 0 for error, 1 for done, 2 to go on to the next level.
  2678. */
  2679. STATIC int /* error */
  2680. xfs_btree_delrec(
  2681. struct xfs_btree_cur *cur, /* btree cursor */
  2682. int level, /* level removing record from */
  2683. int *stat) /* fail/done/go-on */
  2684. {
  2685. struct xfs_btree_block *block; /* btree block */
  2686. union xfs_btree_ptr cptr; /* current block ptr */
  2687. struct xfs_buf *bp; /* buffer for block */
  2688. int error; /* error return value */
  2689. int i; /* loop counter */
  2690. union xfs_btree_key key; /* storage for keyp */
  2691. union xfs_btree_key *keyp = &key; /* passed to the next level */
  2692. union xfs_btree_ptr lptr; /* left sibling block ptr */
  2693. struct xfs_buf *lbp; /* left buffer pointer */
  2694. struct xfs_btree_block *left; /* left btree block */
  2695. int lrecs = 0; /* left record count */
  2696. int ptr; /* key/record index */
  2697. union xfs_btree_ptr rptr; /* right sibling block ptr */
  2698. struct xfs_buf *rbp; /* right buffer pointer */
  2699. struct xfs_btree_block *right; /* right btree block */
  2700. struct xfs_btree_block *rrblock; /* right-right btree block */
  2701. struct xfs_buf *rrbp; /* right-right buffer pointer */
  2702. int rrecs = 0; /* right record count */
  2703. struct xfs_btree_cur *tcur; /* temporary btree cursor */
  2704. int numrecs; /* temporary numrec count */
  2705. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  2706. XFS_BTREE_TRACE_ARGI(cur, level);
  2707. tcur = NULL;
  2708. /* Get the index of the entry being deleted, check for nothing there. */
  2709. ptr = cur->bc_ptrs[level];
  2710. if (ptr == 0) {
  2711. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2712. *stat = 0;
  2713. return 0;
  2714. }
  2715. /* Get the buffer & block containing the record or key/ptr. */
  2716. block = xfs_btree_get_block(cur, level, &bp);
  2717. numrecs = xfs_btree_get_numrecs(block);
  2718. #ifdef DEBUG
  2719. error = xfs_btree_check_block(cur, block, level, bp);
  2720. if (error)
  2721. goto error0;
  2722. #endif
  2723. /* Fail if we're off the end of the block. */
  2724. if (ptr > numrecs) {
  2725. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2726. *stat = 0;
  2727. return 0;
  2728. }
  2729. XFS_BTREE_STATS_INC(cur, delrec);
  2730. XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr);
  2731. /* Excise the entries being deleted. */
  2732. if (level > 0) {
  2733. /* It's a nonleaf. operate on keys and ptrs */
  2734. union xfs_btree_key *lkp;
  2735. union xfs_btree_ptr *lpp;
  2736. lkp = xfs_btree_key_addr(cur, ptr + 1, block);
  2737. lpp = xfs_btree_ptr_addr(cur, ptr + 1, block);
  2738. #ifdef DEBUG
  2739. for (i = 0; i < numrecs - ptr; i++) {
  2740. error = xfs_btree_check_ptr(cur, lpp, i, level);
  2741. if (error)
  2742. goto error0;
  2743. }
  2744. #endif
  2745. if (ptr < numrecs) {
  2746. xfs_btree_shift_keys(cur, lkp, -1, numrecs - ptr);
  2747. xfs_btree_shift_ptrs(cur, lpp, -1, numrecs - ptr);
  2748. xfs_btree_log_keys(cur, bp, ptr, numrecs - 1);
  2749. xfs_btree_log_ptrs(cur, bp, ptr, numrecs - 1);
  2750. }
  2751. /*
  2752. * If it's the first record in the block, we'll need to pass a
  2753. * key up to the next level (updkey).
  2754. */
  2755. if (ptr == 1)
  2756. keyp = xfs_btree_key_addr(cur, 1, block);
  2757. } else {
  2758. /* It's a leaf. operate on records */
  2759. if (ptr < numrecs) {
  2760. xfs_btree_shift_recs(cur,
  2761. xfs_btree_rec_addr(cur, ptr + 1, block),
  2762. -1, numrecs - ptr);
  2763. xfs_btree_log_recs(cur, bp, ptr, numrecs - 1);
  2764. }
  2765. /*
  2766. * If it's the first record in the block, we'll need a key
  2767. * structure to pass up to the next level (updkey).
  2768. */
  2769. if (ptr == 1) {
  2770. cur->bc_ops->init_key_from_rec(&key,
  2771. xfs_btree_rec_addr(cur, 1, block));
  2772. keyp = &key;
  2773. }
  2774. }
  2775. /*
  2776. * Decrement and log the number of entries in the block.
  2777. */
  2778. xfs_btree_set_numrecs(block, --numrecs);
  2779. xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
  2780. /*
  2781. * If we are tracking the last record in the tree and
  2782. * we are at the far right edge of the tree, update it.
  2783. */
  2784. if (xfs_btree_is_lastrec(cur, block, level)) {
  2785. cur->bc_ops->update_lastrec(cur, block, NULL,
  2786. ptr, LASTREC_DELREC);
  2787. }
  2788. /*
  2789. * We're at the root level. First, shrink the root block in-memory.
  2790. * Try to get rid of the next level down. If we can't then there's
  2791. * nothing left to do.
  2792. */
  2793. if (level == cur->bc_nlevels - 1) {
  2794. if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
  2795. xfs_iroot_realloc(cur->bc_private.b.ip, -1,
  2796. cur->bc_private.b.whichfork);
  2797. error = xfs_btree_kill_iroot(cur);
  2798. if (error)
  2799. goto error0;
  2800. error = xfs_btree_dec_cursor(cur, level, stat);
  2801. if (error)
  2802. goto error0;
  2803. *stat = 1;
  2804. return 0;
  2805. }
  2806. /*
  2807. * If this is the root level, and there's only one entry left,
  2808. * and it's NOT the leaf level, then we can get rid of this
  2809. * level.
  2810. */
  2811. if (numrecs == 1 && level > 0) {
  2812. union xfs_btree_ptr *pp;
  2813. /*
  2814. * pp is still set to the first pointer in the block.
  2815. * Make it the new root of the btree.
  2816. */
  2817. pp = xfs_btree_ptr_addr(cur, 1, block);
  2818. error = xfs_btree_kill_root(cur, bp, level, pp);
  2819. if (error)
  2820. goto error0;
  2821. } else if (level > 0) {
  2822. error = xfs_btree_dec_cursor(cur, level, stat);
  2823. if (error)
  2824. goto error0;
  2825. }
  2826. *stat = 1;
  2827. return 0;
  2828. }
  2829. /*
  2830. * If we deleted the leftmost entry in the block, update the
  2831. * key values above us in the tree.
  2832. */
  2833. if (ptr == 1) {
  2834. error = xfs_btree_updkey(cur, keyp, level + 1);
  2835. if (error)
  2836. goto error0;
  2837. }
  2838. /*
  2839. * If the number of records remaining in the block is at least
  2840. * the minimum, we're done.
  2841. */
  2842. if (numrecs >= cur->bc_ops->get_minrecs(cur, level)) {
  2843. error = xfs_btree_dec_cursor(cur, level, stat);
  2844. if (error)
  2845. goto error0;
  2846. return 0;
  2847. }
  2848. /*
  2849. * Otherwise, we have to move some records around to keep the
  2850. * tree balanced. Look at the left and right sibling blocks to
  2851. * see if we can re-balance by moving only one record.
  2852. */
  2853. xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
  2854. xfs_btree_get_sibling(cur, block, &lptr, XFS_BB_LEFTSIB);
  2855. if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
  2856. /*
  2857. * One child of root, need to get a chance to copy its contents
  2858. * into the root and delete it. Can't go up to next level,
  2859. * there's nothing to delete there.
  2860. */
  2861. if (xfs_btree_ptr_is_null(cur, &rptr) &&
  2862. xfs_btree_ptr_is_null(cur, &lptr) &&
  2863. level == cur->bc_nlevels - 2) {
  2864. error = xfs_btree_kill_iroot(cur);
  2865. if (!error)
  2866. error = xfs_btree_dec_cursor(cur, level, stat);
  2867. if (error)
  2868. goto error0;
  2869. return 0;
  2870. }
  2871. }
  2872. ASSERT(!xfs_btree_ptr_is_null(cur, &rptr) ||
  2873. !xfs_btree_ptr_is_null(cur, &lptr));
  2874. /*
  2875. * Duplicate the cursor so our btree manipulations here won't
  2876. * disrupt the next level up.
  2877. */
  2878. error = xfs_btree_dup_cursor(cur, &tcur);
  2879. if (error)
  2880. goto error0;
  2881. /*
  2882. * If there's a right sibling, see if it's ok to shift an entry
  2883. * out of it.
  2884. */
  2885. if (!xfs_btree_ptr_is_null(cur, &rptr)) {
  2886. /*
  2887. * Move the temp cursor to the last entry in the next block.
  2888. * Actually any entry but the first would suffice.
  2889. */
  2890. i = xfs_btree_lastrec(tcur, level);
  2891. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  2892. error = xfs_btree_increment(tcur, level, &i);
  2893. if (error)
  2894. goto error0;
  2895. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  2896. i = xfs_btree_lastrec(tcur, level);
  2897. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  2898. /* Grab a pointer to the block. */
  2899. right = xfs_btree_get_block(tcur, level, &rbp);
  2900. #ifdef DEBUG
  2901. error = xfs_btree_check_block(tcur, right, level, rbp);
  2902. if (error)
  2903. goto error0;
  2904. #endif
  2905. /* Grab the current block number, for future use. */
  2906. xfs_btree_get_sibling(tcur, right, &cptr, XFS_BB_LEFTSIB);
  2907. /*
  2908. * If right block is full enough so that removing one entry
  2909. * won't make it too empty, and left-shifting an entry out
  2910. * of right to us works, we're done.
  2911. */
  2912. if (xfs_btree_get_numrecs(right) - 1 >=
  2913. cur->bc_ops->get_minrecs(tcur, level)) {
  2914. error = xfs_btree_lshift(tcur, level, &i);
  2915. if (error)
  2916. goto error0;
  2917. if (i) {
  2918. ASSERT(xfs_btree_get_numrecs(block) >=
  2919. cur->bc_ops->get_minrecs(tcur, level));
  2920. xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
  2921. tcur = NULL;
  2922. error = xfs_btree_dec_cursor(cur, level, stat);
  2923. if (error)
  2924. goto error0;
  2925. return 0;
  2926. }
  2927. }
  2928. /*
  2929. * Otherwise, grab the number of records in right for
  2930. * future reference, and fix up the temp cursor to point
  2931. * to our block again (last record).
  2932. */
  2933. rrecs = xfs_btree_get_numrecs(right);
  2934. if (!xfs_btree_ptr_is_null(cur, &lptr)) {
  2935. i = xfs_btree_firstrec(tcur, level);
  2936. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  2937. error = xfs_btree_decrement(tcur, level, &i);
  2938. if (error)
  2939. goto error0;
  2940. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  2941. }
  2942. }
  2943. /*
  2944. * If there's a left sibling, see if it's ok to shift an entry
  2945. * out of it.
  2946. */
  2947. if (!xfs_btree_ptr_is_null(cur, &lptr)) {
  2948. /*
  2949. * Move the temp cursor to the first entry in the
  2950. * previous block.
  2951. */
  2952. i = xfs_btree_firstrec(tcur, level);
  2953. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  2954. error = xfs_btree_decrement(tcur, level, &i);
  2955. if (error)
  2956. goto error0;
  2957. i = xfs_btree_firstrec(tcur, level);
  2958. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  2959. /* Grab a pointer to the block. */
  2960. left = xfs_btree_get_block(tcur, level, &lbp);
  2961. #ifdef DEBUG
  2962. error = xfs_btree_check_block(cur, left, level, lbp);
  2963. if (error)
  2964. goto error0;
  2965. #endif
  2966. /* Grab the current block number, for future use. */
  2967. xfs_btree_get_sibling(tcur, left, &cptr, XFS_BB_RIGHTSIB);
  2968. /*
  2969. * If left block is full enough so that removing one entry
  2970. * won't make it too empty, and right-shifting an entry out
  2971. * of left to us works, we're done.
  2972. */
  2973. if (xfs_btree_get_numrecs(left) - 1 >=
  2974. cur->bc_ops->get_minrecs(tcur, level)) {
  2975. error = xfs_btree_rshift(tcur, level, &i);
  2976. if (error)
  2977. goto error0;
  2978. if (i) {
  2979. ASSERT(xfs_btree_get_numrecs(block) >=
  2980. cur->bc_ops->get_minrecs(tcur, level));
  2981. xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
  2982. tcur = NULL;
  2983. if (level == 0)
  2984. cur->bc_ptrs[0]++;
  2985. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2986. *stat = 1;
  2987. return 0;
  2988. }
  2989. }
  2990. /*
  2991. * Otherwise, grab the number of records in right for
  2992. * future reference.
  2993. */
  2994. lrecs = xfs_btree_get_numrecs(left);
  2995. }
  2996. /* Delete the temp cursor, we're done with it. */
  2997. xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
  2998. tcur = NULL;
  2999. /* If here, we need to do a join to keep the tree balanced. */
  3000. ASSERT(!xfs_btree_ptr_is_null(cur, &cptr));
  3001. if (!xfs_btree_ptr_is_null(cur, &lptr) &&
  3002. lrecs + xfs_btree_get_numrecs(block) <=
  3003. cur->bc_ops->get_maxrecs(cur, level)) {
  3004. /*
  3005. * Set "right" to be the starting block,
  3006. * "left" to be the left neighbor.
  3007. */
  3008. rptr = cptr;
  3009. right = block;
  3010. rbp = bp;
  3011. error = xfs_btree_read_buf_block(cur, &lptr, level,
  3012. 0, &left, &lbp);
  3013. if (error)
  3014. goto error0;
  3015. /*
  3016. * If that won't work, see if we can join with the right neighbor block.
  3017. */
  3018. } else if (!xfs_btree_ptr_is_null(cur, &rptr) &&
  3019. rrecs + xfs_btree_get_numrecs(block) <=
  3020. cur->bc_ops->get_maxrecs(cur, level)) {
  3021. /*
  3022. * Set "left" to be the starting block,
  3023. * "right" to be the right neighbor.
  3024. */
  3025. lptr = cptr;
  3026. left = block;
  3027. lbp = bp;
  3028. error = xfs_btree_read_buf_block(cur, &rptr, level,
  3029. 0, &right, &rbp);
  3030. if (error)
  3031. goto error0;
  3032. /*
  3033. * Otherwise, we can't fix the imbalance.
  3034. * Just return. This is probably a logic error, but it's not fatal.
  3035. */
  3036. } else {
  3037. error = xfs_btree_dec_cursor(cur, level, stat);
  3038. if (error)
  3039. goto error0;
  3040. return 0;
  3041. }
  3042. rrecs = xfs_btree_get_numrecs(right);
  3043. lrecs = xfs_btree_get_numrecs(left);
  3044. /*
  3045. * We're now going to join "left" and "right" by moving all the stuff
  3046. * in "right" to "left" and deleting "right".
  3047. */
  3048. XFS_BTREE_STATS_ADD(cur, moves, rrecs);
  3049. if (level > 0) {
  3050. /* It's a non-leaf. Move keys and pointers. */
  3051. union xfs_btree_key *lkp; /* left btree key */
  3052. union xfs_btree_ptr *lpp; /* left address pointer */
  3053. union xfs_btree_key *rkp; /* right btree key */
  3054. union xfs_btree_ptr *rpp; /* right address pointer */
  3055. lkp = xfs_btree_key_addr(cur, lrecs + 1, left);
  3056. lpp = xfs_btree_ptr_addr(cur, lrecs + 1, left);
  3057. rkp = xfs_btree_key_addr(cur, 1, right);
  3058. rpp = xfs_btree_ptr_addr(cur, 1, right);
  3059. #ifdef DEBUG
  3060. for (i = 1; i < rrecs; i++) {
  3061. error = xfs_btree_check_ptr(cur, rpp, i, level);
  3062. if (error)
  3063. goto error0;
  3064. }
  3065. #endif
  3066. xfs_btree_copy_keys(cur, lkp, rkp, rrecs);
  3067. xfs_btree_copy_ptrs(cur, lpp, rpp, rrecs);
  3068. xfs_btree_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs);
  3069. xfs_btree_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs);
  3070. } else {
  3071. /* It's a leaf. Move records. */
  3072. union xfs_btree_rec *lrp; /* left record pointer */
  3073. union xfs_btree_rec *rrp; /* right record pointer */
  3074. lrp = xfs_btree_rec_addr(cur, lrecs + 1, left);
  3075. rrp = xfs_btree_rec_addr(cur, 1, right);
  3076. xfs_btree_copy_recs(cur, lrp, rrp, rrecs);
  3077. xfs_btree_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs);
  3078. }
  3079. XFS_BTREE_STATS_INC(cur, join);
  3080. /*
  3081. * Fix up the number of records and right block pointer in the
  3082. * surviving block, and log it.
  3083. */
  3084. xfs_btree_set_numrecs(left, lrecs + rrecs);
  3085. xfs_btree_get_sibling(cur, right, &cptr, XFS_BB_RIGHTSIB),
  3086. xfs_btree_set_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
  3087. xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
  3088. /* If there is a right sibling, point it to the remaining block. */
  3089. xfs_btree_get_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
  3090. if (!xfs_btree_ptr_is_null(cur, &cptr)) {
  3091. error = xfs_btree_read_buf_block(cur, &cptr, level,
  3092. 0, &rrblock, &rrbp);
  3093. if (error)
  3094. goto error0;
  3095. xfs_btree_set_sibling(cur, rrblock, &lptr, XFS_BB_LEFTSIB);
  3096. xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
  3097. }
  3098. /* Free the deleted block. */
  3099. error = cur->bc_ops->free_block(cur, rbp);
  3100. if (error)
  3101. goto error0;
  3102. XFS_BTREE_STATS_INC(cur, free);
  3103. /*
  3104. * If we joined with the left neighbor, set the buffer in the
  3105. * cursor to the left block, and fix up the index.
  3106. */
  3107. if (bp != lbp) {
  3108. cur->bc_bufs[level] = lbp;
  3109. cur->bc_ptrs[level] += lrecs;
  3110. cur->bc_ra[level] = 0;
  3111. }
  3112. /*
  3113. * If we joined with the right neighbor and there's a level above
  3114. * us, increment the cursor at that level.
  3115. */
  3116. else if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) ||
  3117. (level + 1 < cur->bc_nlevels)) {
  3118. error = xfs_btree_increment(cur, level + 1, &i);
  3119. if (error)
  3120. goto error0;
  3121. }
  3122. /*
  3123. * Readjust the ptr at this level if it's not a leaf, since it's
  3124. * still pointing at the deletion point, which makes the cursor
  3125. * inconsistent. If this makes the ptr 0, the caller fixes it up.
  3126. * We can't use decrement because it would change the next level up.
  3127. */
  3128. if (level > 0)
  3129. cur->bc_ptrs[level]--;
  3130. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  3131. /* Return value means the next level up has something to do. */
  3132. *stat = 2;
  3133. return 0;
  3134. error0:
  3135. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  3136. if (tcur)
  3137. xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
  3138. return error;
  3139. }
  3140. /*
  3141. * Delete the record pointed to by cur.
  3142. * The cursor refers to the place where the record was (could be inserted)
  3143. * when the operation returns.
  3144. */
  3145. int /* error */
  3146. xfs_btree_delete(
  3147. struct xfs_btree_cur *cur,
  3148. int *stat) /* success/failure */
  3149. {
  3150. int error; /* error return value */
  3151. int level;
  3152. int i;
  3153. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  3154. /*
  3155. * Go up the tree, starting at leaf level.
  3156. *
  3157. * If 2 is returned then a join was done; go to the next level.
  3158. * Otherwise we are done.
  3159. */
  3160. for (level = 0, i = 2; i == 2; level++) {
  3161. error = xfs_btree_delrec(cur, level, &i);
  3162. if (error)
  3163. goto error0;
  3164. }
  3165. if (i == 0) {
  3166. for (level = 1; level < cur->bc_nlevels; level++) {
  3167. if (cur->bc_ptrs[level] == 0) {
  3168. error = xfs_btree_decrement(cur, level, &i);
  3169. if (error)
  3170. goto error0;
  3171. break;
  3172. }
  3173. }
  3174. }
  3175. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  3176. *stat = i;
  3177. return 0;
  3178. error0:
  3179. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  3180. return error;
  3181. }
  3182. /*
  3183. * Get the data from the pointed-to record.
  3184. */
  3185. int /* error */
  3186. xfs_btree_get_rec(
  3187. struct xfs_btree_cur *cur, /* btree cursor */
  3188. union xfs_btree_rec **recp, /* output: btree record */
  3189. int *stat) /* output: success/failure */
  3190. {
  3191. struct xfs_btree_block *block; /* btree block */
  3192. struct xfs_buf *bp; /* buffer pointer */
  3193. int ptr; /* record number */
  3194. #ifdef DEBUG
  3195. int error; /* error return value */
  3196. #endif
  3197. ptr = cur->bc_ptrs[0];
  3198. block = xfs_btree_get_block(cur, 0, &bp);
  3199. #ifdef DEBUG
  3200. error = xfs_btree_check_block(cur, block, 0, bp);
  3201. if (error)
  3202. return error;
  3203. #endif
  3204. /*
  3205. * Off the right end or left end, return failure.
  3206. */
  3207. if (ptr > xfs_btree_get_numrecs(block) || ptr <= 0) {
  3208. *stat = 0;
  3209. return 0;
  3210. }
  3211. /*
  3212. * Point to the record and extract its data.
  3213. */
  3214. *recp = xfs_btree_rec_addr(cur, ptr, block);
  3215. *stat = 1;
  3216. return 0;
  3217. }