xfs_btree.c 104 KB

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