xfs_btree.c 103 KB

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