xfs_btree.c 105 KB

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