xfs_btree.c 106 KB

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