xfs_btree.c 99 KB

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