xfs_btree.c 108 KB

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