xfs_btree.c 94 KB

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