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