xfs_btree.c 100 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870
  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 a 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. /*
  780. * Set the buffer for level "lev" in the cursor to bp, releasing
  781. * any previous buffer.
  782. */
  783. STATIC void
  784. xfs_btree_setbuf(
  785. xfs_btree_cur_t *cur, /* btree cursor */
  786. int lev, /* level in btree */
  787. xfs_buf_t *bp) /* new buffer to set */
  788. {
  789. struct xfs_btree_block *b; /* btree block */
  790. if (cur->bc_bufs[lev])
  791. xfs_trans_brelse(cur->bc_tp, cur->bc_bufs[lev]);
  792. cur->bc_bufs[lev] = bp;
  793. cur->bc_ra[lev] = 0;
  794. b = XFS_BUF_TO_BLOCK(bp);
  795. if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
  796. if (b->bb_u.l.bb_leftsib == cpu_to_be64(NULLDFSBNO))
  797. cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
  798. if (b->bb_u.l.bb_rightsib == cpu_to_be64(NULLDFSBNO))
  799. cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
  800. } else {
  801. if (b->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK))
  802. cur->bc_ra[lev] |= XFS_BTCUR_LEFTRA;
  803. if (b->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK))
  804. cur->bc_ra[lev] |= XFS_BTCUR_RIGHTRA;
  805. }
  806. }
  807. STATIC int
  808. xfs_btree_ptr_is_null(
  809. struct xfs_btree_cur *cur,
  810. union xfs_btree_ptr *ptr)
  811. {
  812. if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
  813. return ptr->l == cpu_to_be64(NULLDFSBNO);
  814. else
  815. return ptr->s == cpu_to_be32(NULLAGBLOCK);
  816. }
  817. STATIC void
  818. xfs_btree_set_ptr_null(
  819. struct xfs_btree_cur *cur,
  820. union xfs_btree_ptr *ptr)
  821. {
  822. if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
  823. ptr->l = cpu_to_be64(NULLDFSBNO);
  824. else
  825. ptr->s = cpu_to_be32(NULLAGBLOCK);
  826. }
  827. /*
  828. * Get/set/init sibling pointers
  829. */
  830. STATIC void
  831. xfs_btree_get_sibling(
  832. struct xfs_btree_cur *cur,
  833. struct xfs_btree_block *block,
  834. union xfs_btree_ptr *ptr,
  835. int lr)
  836. {
  837. ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
  838. if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
  839. if (lr == XFS_BB_RIGHTSIB)
  840. ptr->l = block->bb_u.l.bb_rightsib;
  841. else
  842. ptr->l = block->bb_u.l.bb_leftsib;
  843. } else {
  844. if (lr == XFS_BB_RIGHTSIB)
  845. ptr->s = block->bb_u.s.bb_rightsib;
  846. else
  847. ptr->s = block->bb_u.s.bb_leftsib;
  848. }
  849. }
  850. STATIC void
  851. xfs_btree_set_sibling(
  852. struct xfs_btree_cur *cur,
  853. struct xfs_btree_block *block,
  854. union xfs_btree_ptr *ptr,
  855. int lr)
  856. {
  857. ASSERT(lr == XFS_BB_LEFTSIB || lr == XFS_BB_RIGHTSIB);
  858. if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
  859. if (lr == XFS_BB_RIGHTSIB)
  860. block->bb_u.l.bb_rightsib = ptr->l;
  861. else
  862. block->bb_u.l.bb_leftsib = ptr->l;
  863. } else {
  864. if (lr == XFS_BB_RIGHTSIB)
  865. block->bb_u.s.bb_rightsib = ptr->s;
  866. else
  867. block->bb_u.s.bb_leftsib = ptr->s;
  868. }
  869. }
  870. void
  871. xfs_btree_init_block_int(
  872. struct xfs_mount *mp,
  873. struct xfs_btree_block *buf,
  874. xfs_daddr_t blkno,
  875. __u32 magic,
  876. __u16 level,
  877. __u16 numrecs,
  878. __u64 owner,
  879. unsigned int flags)
  880. {
  881. buf->bb_magic = cpu_to_be32(magic);
  882. buf->bb_level = cpu_to_be16(level);
  883. buf->bb_numrecs = cpu_to_be16(numrecs);
  884. if (flags & XFS_BTREE_LONG_PTRS) {
  885. buf->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
  886. buf->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
  887. if (flags & XFS_BTREE_CRC_BLOCKS) {
  888. buf->bb_u.l.bb_blkno = cpu_to_be64(blkno);
  889. buf->bb_u.l.bb_owner = cpu_to_be64(owner);
  890. uuid_copy(&buf->bb_u.l.bb_uuid, &mp->m_sb.sb_uuid);
  891. buf->bb_u.l.bb_pad = 0;
  892. }
  893. } else {
  894. /* owner is a 32 bit value on short blocks */
  895. __u32 __owner = (__u32)owner;
  896. buf->bb_u.s.bb_leftsib = cpu_to_be32(NULLAGBLOCK);
  897. buf->bb_u.s.bb_rightsib = cpu_to_be32(NULLAGBLOCK);
  898. if (flags & XFS_BTREE_CRC_BLOCKS) {
  899. buf->bb_u.s.bb_blkno = cpu_to_be64(blkno);
  900. buf->bb_u.s.bb_owner = cpu_to_be32(__owner);
  901. uuid_copy(&buf->bb_u.s.bb_uuid, &mp->m_sb.sb_uuid);
  902. }
  903. }
  904. }
  905. void
  906. xfs_btree_init_block(
  907. struct xfs_mount *mp,
  908. struct xfs_buf *bp,
  909. __u32 magic,
  910. __u16 level,
  911. __u16 numrecs,
  912. __u64 owner,
  913. unsigned int flags)
  914. {
  915. xfs_btree_init_block_int(mp, XFS_BUF_TO_BLOCK(bp), bp->b_bn,
  916. magic, level, numrecs, owner, flags);
  917. }
  918. STATIC void
  919. xfs_btree_init_block_cur(
  920. struct xfs_btree_cur *cur,
  921. struct xfs_buf *bp,
  922. int level,
  923. int numrecs)
  924. {
  925. __u64 owner;
  926. /*
  927. * we can pull the owner from the cursor right now as the different
  928. * owners align directly with the pointer size of the btree. This may
  929. * change in future, but is safe for current users of the generic btree
  930. * code.
  931. */
  932. if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
  933. owner = cur->bc_private.b.ip->i_ino;
  934. else
  935. owner = cur->bc_private.a.agno;
  936. xfs_btree_init_block_int(cur->bc_mp, XFS_BUF_TO_BLOCK(bp), bp->b_bn,
  937. xfs_btree_magic(cur), level, numrecs,
  938. owner, cur->bc_flags);
  939. }
  940. /*
  941. * Return true if ptr is the last record in the btree and
  942. * we need to track updates to this record. The decision
  943. * will be further refined in the update_lastrec method.
  944. */
  945. STATIC int
  946. xfs_btree_is_lastrec(
  947. struct xfs_btree_cur *cur,
  948. struct xfs_btree_block *block,
  949. int level)
  950. {
  951. union xfs_btree_ptr ptr;
  952. if (level > 0)
  953. return 0;
  954. if (!(cur->bc_flags & XFS_BTREE_LASTREC_UPDATE))
  955. return 0;
  956. xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
  957. if (!xfs_btree_ptr_is_null(cur, &ptr))
  958. return 0;
  959. return 1;
  960. }
  961. STATIC void
  962. xfs_btree_buf_to_ptr(
  963. struct xfs_btree_cur *cur,
  964. struct xfs_buf *bp,
  965. union xfs_btree_ptr *ptr)
  966. {
  967. if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
  968. ptr->l = cpu_to_be64(XFS_DADDR_TO_FSB(cur->bc_mp,
  969. XFS_BUF_ADDR(bp)));
  970. else {
  971. ptr->s = cpu_to_be32(xfs_daddr_to_agbno(cur->bc_mp,
  972. XFS_BUF_ADDR(bp)));
  973. }
  974. }
  975. STATIC xfs_daddr_t
  976. xfs_btree_ptr_to_daddr(
  977. struct xfs_btree_cur *cur,
  978. union xfs_btree_ptr *ptr)
  979. {
  980. if (cur->bc_flags & XFS_BTREE_LONG_PTRS) {
  981. ASSERT(ptr->l != cpu_to_be64(NULLDFSBNO));
  982. return XFS_FSB_TO_DADDR(cur->bc_mp, be64_to_cpu(ptr->l));
  983. } else {
  984. ASSERT(cur->bc_private.a.agno != NULLAGNUMBER);
  985. ASSERT(ptr->s != cpu_to_be32(NULLAGBLOCK));
  986. return XFS_AGB_TO_DADDR(cur->bc_mp, cur->bc_private.a.agno,
  987. be32_to_cpu(ptr->s));
  988. }
  989. }
  990. STATIC void
  991. xfs_btree_set_refs(
  992. struct xfs_btree_cur *cur,
  993. struct xfs_buf *bp)
  994. {
  995. switch (cur->bc_btnum) {
  996. case XFS_BTNUM_BNO:
  997. case XFS_BTNUM_CNT:
  998. xfs_buf_set_ref(bp, XFS_ALLOC_BTREE_REF);
  999. break;
  1000. case XFS_BTNUM_INO:
  1001. xfs_buf_set_ref(bp, XFS_INO_BTREE_REF);
  1002. break;
  1003. case XFS_BTNUM_BMAP:
  1004. xfs_buf_set_ref(bp, XFS_BMAP_BTREE_REF);
  1005. break;
  1006. default:
  1007. ASSERT(0);
  1008. }
  1009. }
  1010. STATIC int
  1011. xfs_btree_get_buf_block(
  1012. struct xfs_btree_cur *cur,
  1013. union xfs_btree_ptr *ptr,
  1014. int flags,
  1015. struct xfs_btree_block **block,
  1016. struct xfs_buf **bpp)
  1017. {
  1018. struct xfs_mount *mp = cur->bc_mp;
  1019. xfs_daddr_t d;
  1020. /* need to sort out how callers deal with failures first */
  1021. ASSERT(!(flags & XBF_TRYLOCK));
  1022. d = xfs_btree_ptr_to_daddr(cur, ptr);
  1023. *bpp = xfs_trans_get_buf(cur->bc_tp, mp->m_ddev_targp, d,
  1024. mp->m_bsize, flags);
  1025. if (!*bpp)
  1026. return ENOMEM;
  1027. (*bpp)->b_ops = cur->bc_ops->buf_ops;
  1028. *block = XFS_BUF_TO_BLOCK(*bpp);
  1029. return 0;
  1030. }
  1031. /*
  1032. * Read in the buffer at the given ptr and return the buffer and
  1033. * the block pointer within the buffer.
  1034. */
  1035. STATIC int
  1036. xfs_btree_read_buf_block(
  1037. struct xfs_btree_cur *cur,
  1038. union xfs_btree_ptr *ptr,
  1039. int level,
  1040. int flags,
  1041. struct xfs_btree_block **block,
  1042. struct xfs_buf **bpp)
  1043. {
  1044. struct xfs_mount *mp = cur->bc_mp;
  1045. xfs_daddr_t d;
  1046. int error;
  1047. /* need to sort out how callers deal with failures first */
  1048. ASSERT(!(flags & XBF_TRYLOCK));
  1049. d = xfs_btree_ptr_to_daddr(cur, ptr);
  1050. error = xfs_trans_read_buf(mp, cur->bc_tp, mp->m_ddev_targp, d,
  1051. mp->m_bsize, flags, bpp,
  1052. cur->bc_ops->buf_ops);
  1053. if (error)
  1054. return error;
  1055. ASSERT(!xfs_buf_geterror(*bpp));
  1056. xfs_btree_set_refs(cur, *bpp);
  1057. *block = XFS_BUF_TO_BLOCK(*bpp);
  1058. return 0;
  1059. }
  1060. /*
  1061. * Copy keys from one btree block to another.
  1062. */
  1063. STATIC void
  1064. xfs_btree_copy_keys(
  1065. struct xfs_btree_cur *cur,
  1066. union xfs_btree_key *dst_key,
  1067. union xfs_btree_key *src_key,
  1068. int numkeys)
  1069. {
  1070. ASSERT(numkeys >= 0);
  1071. memcpy(dst_key, src_key, numkeys * cur->bc_ops->key_len);
  1072. }
  1073. /*
  1074. * Copy records from one btree block to another.
  1075. */
  1076. STATIC void
  1077. xfs_btree_copy_recs(
  1078. struct xfs_btree_cur *cur,
  1079. union xfs_btree_rec *dst_rec,
  1080. union xfs_btree_rec *src_rec,
  1081. int numrecs)
  1082. {
  1083. ASSERT(numrecs >= 0);
  1084. memcpy(dst_rec, src_rec, numrecs * cur->bc_ops->rec_len);
  1085. }
  1086. /*
  1087. * Copy block pointers from one btree block to another.
  1088. */
  1089. STATIC void
  1090. xfs_btree_copy_ptrs(
  1091. struct xfs_btree_cur *cur,
  1092. union xfs_btree_ptr *dst_ptr,
  1093. union xfs_btree_ptr *src_ptr,
  1094. int numptrs)
  1095. {
  1096. ASSERT(numptrs >= 0);
  1097. memcpy(dst_ptr, src_ptr, numptrs * xfs_btree_ptr_len(cur));
  1098. }
  1099. /*
  1100. * Shift keys one index left/right inside a single btree block.
  1101. */
  1102. STATIC void
  1103. xfs_btree_shift_keys(
  1104. struct xfs_btree_cur *cur,
  1105. union xfs_btree_key *key,
  1106. int dir,
  1107. int numkeys)
  1108. {
  1109. char *dst_key;
  1110. ASSERT(numkeys >= 0);
  1111. ASSERT(dir == 1 || dir == -1);
  1112. dst_key = (char *)key + (dir * cur->bc_ops->key_len);
  1113. memmove(dst_key, key, numkeys * cur->bc_ops->key_len);
  1114. }
  1115. /*
  1116. * Shift records one index left/right inside a single btree block.
  1117. */
  1118. STATIC void
  1119. xfs_btree_shift_recs(
  1120. struct xfs_btree_cur *cur,
  1121. union xfs_btree_rec *rec,
  1122. int dir,
  1123. int numrecs)
  1124. {
  1125. char *dst_rec;
  1126. ASSERT(numrecs >= 0);
  1127. ASSERT(dir == 1 || dir == -1);
  1128. dst_rec = (char *)rec + (dir * cur->bc_ops->rec_len);
  1129. memmove(dst_rec, rec, numrecs * cur->bc_ops->rec_len);
  1130. }
  1131. /*
  1132. * Shift block pointers one index left/right inside a single btree block.
  1133. */
  1134. STATIC void
  1135. xfs_btree_shift_ptrs(
  1136. struct xfs_btree_cur *cur,
  1137. union xfs_btree_ptr *ptr,
  1138. int dir,
  1139. int numptrs)
  1140. {
  1141. char *dst_ptr;
  1142. ASSERT(numptrs >= 0);
  1143. ASSERT(dir == 1 || dir == -1);
  1144. dst_ptr = (char *)ptr + (dir * xfs_btree_ptr_len(cur));
  1145. memmove(dst_ptr, ptr, numptrs * xfs_btree_ptr_len(cur));
  1146. }
  1147. /*
  1148. * Log key values from the btree block.
  1149. */
  1150. STATIC void
  1151. xfs_btree_log_keys(
  1152. struct xfs_btree_cur *cur,
  1153. struct xfs_buf *bp,
  1154. int first,
  1155. int last)
  1156. {
  1157. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  1158. XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
  1159. if (bp) {
  1160. xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
  1161. xfs_trans_log_buf(cur->bc_tp, bp,
  1162. xfs_btree_key_offset(cur, first),
  1163. xfs_btree_key_offset(cur, last + 1) - 1);
  1164. } else {
  1165. xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
  1166. xfs_ilog_fbroot(cur->bc_private.b.whichfork));
  1167. }
  1168. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1169. }
  1170. /*
  1171. * Log record values from the btree block.
  1172. */
  1173. void
  1174. xfs_btree_log_recs(
  1175. struct xfs_btree_cur *cur,
  1176. struct xfs_buf *bp,
  1177. int first,
  1178. int last)
  1179. {
  1180. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  1181. XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
  1182. xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
  1183. xfs_trans_log_buf(cur->bc_tp, bp,
  1184. xfs_btree_rec_offset(cur, first),
  1185. xfs_btree_rec_offset(cur, last + 1) - 1);
  1186. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1187. }
  1188. /*
  1189. * Log block pointer fields from a btree block (nonleaf).
  1190. */
  1191. STATIC void
  1192. xfs_btree_log_ptrs(
  1193. struct xfs_btree_cur *cur, /* btree cursor */
  1194. struct xfs_buf *bp, /* buffer containing btree block */
  1195. int first, /* index of first pointer to log */
  1196. int last) /* index of last pointer to log */
  1197. {
  1198. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  1199. XFS_BTREE_TRACE_ARGBII(cur, bp, first, last);
  1200. if (bp) {
  1201. struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
  1202. int level = xfs_btree_get_level(block);
  1203. xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
  1204. xfs_trans_log_buf(cur->bc_tp, bp,
  1205. xfs_btree_ptr_offset(cur, first, level),
  1206. xfs_btree_ptr_offset(cur, last + 1, level) - 1);
  1207. } else {
  1208. xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
  1209. xfs_ilog_fbroot(cur->bc_private.b.whichfork));
  1210. }
  1211. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1212. }
  1213. /*
  1214. * Log fields from a btree block header.
  1215. */
  1216. void
  1217. xfs_btree_log_block(
  1218. struct xfs_btree_cur *cur, /* btree cursor */
  1219. struct xfs_buf *bp, /* buffer containing btree block */
  1220. int fields) /* mask of fields: XFS_BB_... */
  1221. {
  1222. int first; /* first byte offset logged */
  1223. int last; /* last byte offset logged */
  1224. static const short soffsets[] = { /* table of offsets (short) */
  1225. offsetof(struct xfs_btree_block, bb_magic),
  1226. offsetof(struct xfs_btree_block, bb_level),
  1227. offsetof(struct xfs_btree_block, bb_numrecs),
  1228. offsetof(struct xfs_btree_block, bb_u.s.bb_leftsib),
  1229. offsetof(struct xfs_btree_block, bb_u.s.bb_rightsib),
  1230. offsetof(struct xfs_btree_block, bb_u.s.bb_blkno),
  1231. offsetof(struct xfs_btree_block, bb_u.s.bb_lsn),
  1232. offsetof(struct xfs_btree_block, bb_u.s.bb_uuid),
  1233. offsetof(struct xfs_btree_block, bb_u.s.bb_owner),
  1234. offsetof(struct xfs_btree_block, bb_u.s.bb_crc),
  1235. XFS_BTREE_SBLOCK_CRC_LEN
  1236. };
  1237. static const short loffsets[] = { /* table of offsets (long) */
  1238. offsetof(struct xfs_btree_block, bb_magic),
  1239. offsetof(struct xfs_btree_block, bb_level),
  1240. offsetof(struct xfs_btree_block, bb_numrecs),
  1241. offsetof(struct xfs_btree_block, bb_u.l.bb_leftsib),
  1242. offsetof(struct xfs_btree_block, bb_u.l.bb_rightsib),
  1243. offsetof(struct xfs_btree_block, bb_u.l.bb_blkno),
  1244. offsetof(struct xfs_btree_block, bb_u.l.bb_lsn),
  1245. offsetof(struct xfs_btree_block, bb_u.l.bb_uuid),
  1246. offsetof(struct xfs_btree_block, bb_u.l.bb_owner),
  1247. offsetof(struct xfs_btree_block, bb_u.l.bb_crc),
  1248. offsetof(struct xfs_btree_block, bb_u.l.bb_pad),
  1249. XFS_BTREE_LBLOCK_CRC_LEN
  1250. };
  1251. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  1252. XFS_BTREE_TRACE_ARGBI(cur, bp, fields);
  1253. if (bp) {
  1254. int nbits;
  1255. if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) {
  1256. /*
  1257. * We don't log the CRC when updating a btree
  1258. * block but instead recreate it during log
  1259. * recovery. As the log buffers have checksums
  1260. * of their own this is safe and avoids logging a crc
  1261. * update in a lot of places.
  1262. */
  1263. if (fields == XFS_BB_ALL_BITS)
  1264. fields = XFS_BB_ALL_BITS_CRC;
  1265. nbits = XFS_BB_NUM_BITS_CRC;
  1266. } else {
  1267. nbits = XFS_BB_NUM_BITS;
  1268. }
  1269. xfs_btree_offsets(fields,
  1270. (cur->bc_flags & XFS_BTREE_LONG_PTRS) ?
  1271. loffsets : soffsets,
  1272. nbits, &first, &last);
  1273. xfs_trans_buf_set_type(cur->bc_tp, bp, XFS_BLFT_BTREE_BUF);
  1274. xfs_trans_log_buf(cur->bc_tp, bp, first, last);
  1275. } else {
  1276. xfs_trans_log_inode(cur->bc_tp, cur->bc_private.b.ip,
  1277. xfs_ilog_fbroot(cur->bc_private.b.whichfork));
  1278. }
  1279. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1280. }
  1281. /*
  1282. * Increment cursor by one record at the level.
  1283. * For nonzero levels the leaf-ward information is untouched.
  1284. */
  1285. int /* error */
  1286. xfs_btree_increment(
  1287. struct xfs_btree_cur *cur,
  1288. int level,
  1289. int *stat) /* success/failure */
  1290. {
  1291. struct xfs_btree_block *block;
  1292. union xfs_btree_ptr ptr;
  1293. struct xfs_buf *bp;
  1294. int error; /* error return value */
  1295. int lev;
  1296. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  1297. XFS_BTREE_TRACE_ARGI(cur, level);
  1298. ASSERT(level < cur->bc_nlevels);
  1299. /* Read-ahead to the right at this level. */
  1300. xfs_btree_readahead(cur, level, XFS_BTCUR_RIGHTRA);
  1301. /* Get a pointer to the btree block. */
  1302. block = xfs_btree_get_block(cur, level, &bp);
  1303. #ifdef DEBUG
  1304. error = xfs_btree_check_block(cur, block, level, bp);
  1305. if (error)
  1306. goto error0;
  1307. #endif
  1308. /* We're done if we remain in the block after the increment. */
  1309. if (++cur->bc_ptrs[level] <= xfs_btree_get_numrecs(block))
  1310. goto out1;
  1311. /* Fail if we just went off the right edge of the tree. */
  1312. xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
  1313. if (xfs_btree_ptr_is_null(cur, &ptr))
  1314. goto out0;
  1315. XFS_BTREE_STATS_INC(cur, increment);
  1316. /*
  1317. * March up the tree incrementing pointers.
  1318. * Stop when we don't go off the right edge of a block.
  1319. */
  1320. for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
  1321. block = xfs_btree_get_block(cur, lev, &bp);
  1322. #ifdef DEBUG
  1323. error = xfs_btree_check_block(cur, block, lev, bp);
  1324. if (error)
  1325. goto error0;
  1326. #endif
  1327. if (++cur->bc_ptrs[lev] <= xfs_btree_get_numrecs(block))
  1328. break;
  1329. /* Read-ahead the right block for the next loop. */
  1330. xfs_btree_readahead(cur, lev, XFS_BTCUR_RIGHTRA);
  1331. }
  1332. /*
  1333. * If we went off the root then we are either seriously
  1334. * confused or have the tree root in an inode.
  1335. */
  1336. if (lev == cur->bc_nlevels) {
  1337. if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
  1338. goto out0;
  1339. ASSERT(0);
  1340. error = EFSCORRUPTED;
  1341. goto error0;
  1342. }
  1343. ASSERT(lev < cur->bc_nlevels);
  1344. /*
  1345. * Now walk back down the tree, fixing up the cursor's buffer
  1346. * pointers and key numbers.
  1347. */
  1348. for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
  1349. union xfs_btree_ptr *ptrp;
  1350. ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
  1351. error = xfs_btree_read_buf_block(cur, ptrp, --lev,
  1352. 0, &block, &bp);
  1353. if (error)
  1354. goto error0;
  1355. xfs_btree_setbuf(cur, lev, bp);
  1356. cur->bc_ptrs[lev] = 1;
  1357. }
  1358. out1:
  1359. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1360. *stat = 1;
  1361. return 0;
  1362. out0:
  1363. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1364. *stat = 0;
  1365. return 0;
  1366. error0:
  1367. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  1368. return error;
  1369. }
  1370. /*
  1371. * Decrement cursor by one record at the level.
  1372. * For nonzero levels the leaf-ward information is untouched.
  1373. */
  1374. int /* error */
  1375. xfs_btree_decrement(
  1376. struct xfs_btree_cur *cur,
  1377. int level,
  1378. int *stat) /* success/failure */
  1379. {
  1380. struct xfs_btree_block *block;
  1381. xfs_buf_t *bp;
  1382. int error; /* error return value */
  1383. int lev;
  1384. union xfs_btree_ptr ptr;
  1385. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  1386. XFS_BTREE_TRACE_ARGI(cur, level);
  1387. ASSERT(level < cur->bc_nlevels);
  1388. /* Read-ahead to the left at this level. */
  1389. xfs_btree_readahead(cur, level, XFS_BTCUR_LEFTRA);
  1390. /* We're done if we remain in the block after the decrement. */
  1391. if (--cur->bc_ptrs[level] > 0)
  1392. goto out1;
  1393. /* Get a pointer to the btree block. */
  1394. block = xfs_btree_get_block(cur, level, &bp);
  1395. #ifdef DEBUG
  1396. error = xfs_btree_check_block(cur, block, level, bp);
  1397. if (error)
  1398. goto error0;
  1399. #endif
  1400. /* Fail if we just went off the left edge of the tree. */
  1401. xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
  1402. if (xfs_btree_ptr_is_null(cur, &ptr))
  1403. goto out0;
  1404. XFS_BTREE_STATS_INC(cur, decrement);
  1405. /*
  1406. * March up the tree decrementing pointers.
  1407. * Stop when we don't go off the left edge of a block.
  1408. */
  1409. for (lev = level + 1; lev < cur->bc_nlevels; lev++) {
  1410. if (--cur->bc_ptrs[lev] > 0)
  1411. break;
  1412. /* Read-ahead the left block for the next loop. */
  1413. xfs_btree_readahead(cur, lev, XFS_BTCUR_LEFTRA);
  1414. }
  1415. /*
  1416. * If we went off the root then we are seriously confused.
  1417. * or the root of the tree is in an inode.
  1418. */
  1419. if (lev == cur->bc_nlevels) {
  1420. if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE)
  1421. goto out0;
  1422. ASSERT(0);
  1423. error = EFSCORRUPTED;
  1424. goto error0;
  1425. }
  1426. ASSERT(lev < cur->bc_nlevels);
  1427. /*
  1428. * Now walk back down the tree, fixing up the cursor's buffer
  1429. * pointers and key numbers.
  1430. */
  1431. for (block = xfs_btree_get_block(cur, lev, &bp); lev > level; ) {
  1432. union xfs_btree_ptr *ptrp;
  1433. ptrp = xfs_btree_ptr_addr(cur, cur->bc_ptrs[lev], block);
  1434. error = xfs_btree_read_buf_block(cur, ptrp, --lev,
  1435. 0, &block, &bp);
  1436. if (error)
  1437. goto error0;
  1438. xfs_btree_setbuf(cur, lev, bp);
  1439. cur->bc_ptrs[lev] = xfs_btree_get_numrecs(block);
  1440. }
  1441. out1:
  1442. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1443. *stat = 1;
  1444. return 0;
  1445. out0:
  1446. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1447. *stat = 0;
  1448. return 0;
  1449. error0:
  1450. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  1451. return error;
  1452. }
  1453. STATIC int
  1454. xfs_btree_lookup_get_block(
  1455. struct xfs_btree_cur *cur, /* btree cursor */
  1456. int level, /* level in the btree */
  1457. union xfs_btree_ptr *pp, /* ptr to btree block */
  1458. struct xfs_btree_block **blkp) /* return btree block */
  1459. {
  1460. struct xfs_buf *bp; /* buffer pointer for btree block */
  1461. int error = 0;
  1462. /* special case the root block if in an inode */
  1463. if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
  1464. (level == cur->bc_nlevels - 1)) {
  1465. *blkp = xfs_btree_get_iroot(cur);
  1466. return 0;
  1467. }
  1468. /*
  1469. * If the old buffer at this level for the disk address we are
  1470. * looking for re-use it.
  1471. *
  1472. * Otherwise throw it away and get a new one.
  1473. */
  1474. bp = cur->bc_bufs[level];
  1475. if (bp && XFS_BUF_ADDR(bp) == xfs_btree_ptr_to_daddr(cur, pp)) {
  1476. *blkp = XFS_BUF_TO_BLOCK(bp);
  1477. return 0;
  1478. }
  1479. error = xfs_btree_read_buf_block(cur, pp, level, 0, blkp, &bp);
  1480. if (error)
  1481. return error;
  1482. xfs_btree_setbuf(cur, level, bp);
  1483. return 0;
  1484. }
  1485. /*
  1486. * Get current search key. For level 0 we don't actually have a key
  1487. * structure so we make one up from the record. For all other levels
  1488. * we just return the right key.
  1489. */
  1490. STATIC union xfs_btree_key *
  1491. xfs_lookup_get_search_key(
  1492. struct xfs_btree_cur *cur,
  1493. int level,
  1494. int keyno,
  1495. struct xfs_btree_block *block,
  1496. union xfs_btree_key *kp)
  1497. {
  1498. if (level == 0) {
  1499. cur->bc_ops->init_key_from_rec(kp,
  1500. xfs_btree_rec_addr(cur, keyno, block));
  1501. return kp;
  1502. }
  1503. return xfs_btree_key_addr(cur, keyno, block);
  1504. }
  1505. /*
  1506. * Lookup the record. The cursor is made to point to it, based on dir.
  1507. * Return 0 if can't find any such record, 1 for success.
  1508. */
  1509. int /* error */
  1510. xfs_btree_lookup(
  1511. struct xfs_btree_cur *cur, /* btree cursor */
  1512. xfs_lookup_t dir, /* <=, ==, or >= */
  1513. int *stat) /* success/failure */
  1514. {
  1515. struct xfs_btree_block *block; /* current btree block */
  1516. __int64_t diff; /* difference for the current key */
  1517. int error; /* error return value */
  1518. int keyno; /* current key number */
  1519. int level; /* level in the btree */
  1520. union xfs_btree_ptr *pp; /* ptr to btree block */
  1521. union xfs_btree_ptr ptr; /* ptr to btree block */
  1522. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  1523. XFS_BTREE_TRACE_ARGI(cur, dir);
  1524. XFS_BTREE_STATS_INC(cur, lookup);
  1525. block = NULL;
  1526. keyno = 0;
  1527. /* initialise start pointer from cursor */
  1528. cur->bc_ops->init_ptr_from_cur(cur, &ptr);
  1529. pp = &ptr;
  1530. /*
  1531. * Iterate over each level in the btree, starting at the root.
  1532. * For each level above the leaves, find the key we need, based
  1533. * on the lookup record, then follow the corresponding block
  1534. * pointer down to the next level.
  1535. */
  1536. for (level = cur->bc_nlevels - 1, diff = 1; level >= 0; level--) {
  1537. /* Get the block we need to do the lookup on. */
  1538. error = xfs_btree_lookup_get_block(cur, level, pp, &block);
  1539. if (error)
  1540. goto error0;
  1541. if (diff == 0) {
  1542. /*
  1543. * If we already had a key match at a higher level, we
  1544. * know we need to use the first entry in this block.
  1545. */
  1546. keyno = 1;
  1547. } else {
  1548. /* Otherwise search this block. Do a binary search. */
  1549. int high; /* high entry number */
  1550. int low; /* low entry number */
  1551. /* Set low and high entry numbers, 1-based. */
  1552. low = 1;
  1553. high = xfs_btree_get_numrecs(block);
  1554. if (!high) {
  1555. /* Block is empty, must be an empty leaf. */
  1556. ASSERT(level == 0 && cur->bc_nlevels == 1);
  1557. cur->bc_ptrs[0] = dir != XFS_LOOKUP_LE;
  1558. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1559. *stat = 0;
  1560. return 0;
  1561. }
  1562. /* Binary search the block. */
  1563. while (low <= high) {
  1564. union xfs_btree_key key;
  1565. union xfs_btree_key *kp;
  1566. XFS_BTREE_STATS_INC(cur, compare);
  1567. /* keyno is average of low and high. */
  1568. keyno = (low + high) >> 1;
  1569. /* Get current search key */
  1570. kp = xfs_lookup_get_search_key(cur, level,
  1571. keyno, block, &key);
  1572. /*
  1573. * Compute difference to get next direction:
  1574. * - less than, move right
  1575. * - greater than, move left
  1576. * - equal, we're done
  1577. */
  1578. diff = cur->bc_ops->key_diff(cur, kp);
  1579. if (diff < 0)
  1580. low = keyno + 1;
  1581. else if (diff > 0)
  1582. high = keyno - 1;
  1583. else
  1584. break;
  1585. }
  1586. }
  1587. /*
  1588. * If there are more levels, set up for the next level
  1589. * by getting the block number and filling in the cursor.
  1590. */
  1591. if (level > 0) {
  1592. /*
  1593. * If we moved left, need the previous key number,
  1594. * unless there isn't one.
  1595. */
  1596. if (diff > 0 && --keyno < 1)
  1597. keyno = 1;
  1598. pp = xfs_btree_ptr_addr(cur, keyno, block);
  1599. #ifdef DEBUG
  1600. error = xfs_btree_check_ptr(cur, pp, 0, level);
  1601. if (error)
  1602. goto error0;
  1603. #endif
  1604. cur->bc_ptrs[level] = keyno;
  1605. }
  1606. }
  1607. /* Done with the search. See if we need to adjust the results. */
  1608. if (dir != XFS_LOOKUP_LE && diff < 0) {
  1609. keyno++;
  1610. /*
  1611. * If ge search and we went off the end of the block, but it's
  1612. * not the last block, we're in the wrong block.
  1613. */
  1614. xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
  1615. if (dir == XFS_LOOKUP_GE &&
  1616. keyno > xfs_btree_get_numrecs(block) &&
  1617. !xfs_btree_ptr_is_null(cur, &ptr)) {
  1618. int i;
  1619. cur->bc_ptrs[0] = keyno;
  1620. error = xfs_btree_increment(cur, 0, &i);
  1621. if (error)
  1622. goto error0;
  1623. XFS_WANT_CORRUPTED_RETURN(i == 1);
  1624. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1625. *stat = 1;
  1626. return 0;
  1627. }
  1628. } else if (dir == XFS_LOOKUP_LE && diff > 0)
  1629. keyno--;
  1630. cur->bc_ptrs[0] = keyno;
  1631. /* Return if we succeeded or not. */
  1632. if (keyno == 0 || keyno > xfs_btree_get_numrecs(block))
  1633. *stat = 0;
  1634. else if (dir != XFS_LOOKUP_EQ || diff == 0)
  1635. *stat = 1;
  1636. else
  1637. *stat = 0;
  1638. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1639. return 0;
  1640. error0:
  1641. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  1642. return error;
  1643. }
  1644. /*
  1645. * Update keys at all levels from here to the root along the cursor's path.
  1646. */
  1647. STATIC int
  1648. xfs_btree_updkey(
  1649. struct xfs_btree_cur *cur,
  1650. union xfs_btree_key *keyp,
  1651. int level)
  1652. {
  1653. struct xfs_btree_block *block;
  1654. struct xfs_buf *bp;
  1655. union xfs_btree_key *kp;
  1656. int ptr;
  1657. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  1658. XFS_BTREE_TRACE_ARGIK(cur, level, keyp);
  1659. ASSERT(!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) || level >= 1);
  1660. /*
  1661. * Go up the tree from this level toward the root.
  1662. * At each level, update the key value to the value input.
  1663. * Stop when we reach a level where the cursor isn't pointing
  1664. * at the first entry in the block.
  1665. */
  1666. for (ptr = 1; ptr == 1 && level < cur->bc_nlevels; level++) {
  1667. #ifdef DEBUG
  1668. int error;
  1669. #endif
  1670. block = xfs_btree_get_block(cur, level, &bp);
  1671. #ifdef DEBUG
  1672. error = xfs_btree_check_block(cur, block, level, bp);
  1673. if (error) {
  1674. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  1675. return error;
  1676. }
  1677. #endif
  1678. ptr = cur->bc_ptrs[level];
  1679. kp = xfs_btree_key_addr(cur, ptr, block);
  1680. xfs_btree_copy_keys(cur, kp, keyp, 1);
  1681. xfs_btree_log_keys(cur, bp, ptr, ptr);
  1682. }
  1683. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1684. return 0;
  1685. }
  1686. /*
  1687. * Update the record referred to by cur to the value in the
  1688. * given record. This either works (return 0) or gets an
  1689. * EFSCORRUPTED error.
  1690. */
  1691. int
  1692. xfs_btree_update(
  1693. struct xfs_btree_cur *cur,
  1694. union xfs_btree_rec *rec)
  1695. {
  1696. struct xfs_btree_block *block;
  1697. struct xfs_buf *bp;
  1698. int error;
  1699. int ptr;
  1700. union xfs_btree_rec *rp;
  1701. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  1702. XFS_BTREE_TRACE_ARGR(cur, rec);
  1703. /* Pick up the current block. */
  1704. block = xfs_btree_get_block(cur, 0, &bp);
  1705. #ifdef DEBUG
  1706. error = xfs_btree_check_block(cur, block, 0, bp);
  1707. if (error)
  1708. goto error0;
  1709. #endif
  1710. /* Get the address of the rec to be updated. */
  1711. ptr = cur->bc_ptrs[0];
  1712. rp = xfs_btree_rec_addr(cur, ptr, block);
  1713. /* Fill in the new contents and log them. */
  1714. xfs_btree_copy_recs(cur, rp, rec, 1);
  1715. xfs_btree_log_recs(cur, bp, ptr, ptr);
  1716. /*
  1717. * If we are tracking the last record in the tree and
  1718. * we are at the far right edge of the tree, update it.
  1719. */
  1720. if (xfs_btree_is_lastrec(cur, block, 0)) {
  1721. cur->bc_ops->update_lastrec(cur, block, rec,
  1722. ptr, LASTREC_UPDATE);
  1723. }
  1724. /* Updating first rec in leaf. Pass new key value up to our parent. */
  1725. if (ptr == 1) {
  1726. union xfs_btree_key key;
  1727. cur->bc_ops->init_key_from_rec(&key, rec);
  1728. error = xfs_btree_updkey(cur, &key, 1);
  1729. if (error)
  1730. goto error0;
  1731. }
  1732. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1733. return 0;
  1734. error0:
  1735. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  1736. return error;
  1737. }
  1738. /*
  1739. * Move 1 record left from cur/level if possible.
  1740. * Update cur to reflect the new path.
  1741. */
  1742. STATIC int /* error */
  1743. xfs_btree_lshift(
  1744. struct xfs_btree_cur *cur,
  1745. int level,
  1746. int *stat) /* success/failure */
  1747. {
  1748. union xfs_btree_key key; /* btree key */
  1749. struct xfs_buf *lbp; /* left buffer pointer */
  1750. struct xfs_btree_block *left; /* left btree block */
  1751. int lrecs; /* left record count */
  1752. struct xfs_buf *rbp; /* right buffer pointer */
  1753. struct xfs_btree_block *right; /* right btree block */
  1754. int rrecs; /* right record count */
  1755. union xfs_btree_ptr lptr; /* left btree pointer */
  1756. union xfs_btree_key *rkp = NULL; /* right btree key */
  1757. union xfs_btree_ptr *rpp = NULL; /* right address pointer */
  1758. union xfs_btree_rec *rrp = NULL; /* right record pointer */
  1759. int error; /* error return value */
  1760. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  1761. XFS_BTREE_TRACE_ARGI(cur, level);
  1762. if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
  1763. level == cur->bc_nlevels - 1)
  1764. goto out0;
  1765. /* Set up variables for this block as "right". */
  1766. right = xfs_btree_get_block(cur, level, &rbp);
  1767. #ifdef DEBUG
  1768. error = xfs_btree_check_block(cur, right, level, rbp);
  1769. if (error)
  1770. goto error0;
  1771. #endif
  1772. /* If we've got no left sibling then we can't shift an entry left. */
  1773. xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
  1774. if (xfs_btree_ptr_is_null(cur, &lptr))
  1775. goto out0;
  1776. /*
  1777. * If the cursor entry is the one that would be moved, don't
  1778. * do it... it's too complicated.
  1779. */
  1780. if (cur->bc_ptrs[level] <= 1)
  1781. goto out0;
  1782. /* Set up the left neighbor as "left". */
  1783. error = xfs_btree_read_buf_block(cur, &lptr, level, 0, &left, &lbp);
  1784. if (error)
  1785. goto error0;
  1786. /* If it's full, it can't take another entry. */
  1787. lrecs = xfs_btree_get_numrecs(left);
  1788. if (lrecs == cur->bc_ops->get_maxrecs(cur, level))
  1789. goto out0;
  1790. rrecs = xfs_btree_get_numrecs(right);
  1791. /*
  1792. * We add one entry to the left side and remove one for the right side.
  1793. * Account for it here, the changes will be updated on disk and logged
  1794. * later.
  1795. */
  1796. lrecs++;
  1797. rrecs--;
  1798. XFS_BTREE_STATS_INC(cur, lshift);
  1799. XFS_BTREE_STATS_ADD(cur, moves, 1);
  1800. /*
  1801. * If non-leaf, copy a key and a ptr to the left block.
  1802. * Log the changes to the left block.
  1803. */
  1804. if (level > 0) {
  1805. /* It's a non-leaf. Move keys and pointers. */
  1806. union xfs_btree_key *lkp; /* left btree key */
  1807. union xfs_btree_ptr *lpp; /* left address pointer */
  1808. lkp = xfs_btree_key_addr(cur, lrecs, left);
  1809. rkp = xfs_btree_key_addr(cur, 1, right);
  1810. lpp = xfs_btree_ptr_addr(cur, lrecs, left);
  1811. rpp = xfs_btree_ptr_addr(cur, 1, right);
  1812. #ifdef DEBUG
  1813. error = xfs_btree_check_ptr(cur, rpp, 0, level);
  1814. if (error)
  1815. goto error0;
  1816. #endif
  1817. xfs_btree_copy_keys(cur, lkp, rkp, 1);
  1818. xfs_btree_copy_ptrs(cur, lpp, rpp, 1);
  1819. xfs_btree_log_keys(cur, lbp, lrecs, lrecs);
  1820. xfs_btree_log_ptrs(cur, lbp, lrecs, lrecs);
  1821. ASSERT(cur->bc_ops->keys_inorder(cur,
  1822. xfs_btree_key_addr(cur, lrecs - 1, left), lkp));
  1823. } else {
  1824. /* It's a leaf. Move records. */
  1825. union xfs_btree_rec *lrp; /* left record pointer */
  1826. lrp = xfs_btree_rec_addr(cur, lrecs, left);
  1827. rrp = xfs_btree_rec_addr(cur, 1, right);
  1828. xfs_btree_copy_recs(cur, lrp, rrp, 1);
  1829. xfs_btree_log_recs(cur, lbp, lrecs, lrecs);
  1830. ASSERT(cur->bc_ops->recs_inorder(cur,
  1831. xfs_btree_rec_addr(cur, lrecs - 1, left), lrp));
  1832. }
  1833. xfs_btree_set_numrecs(left, lrecs);
  1834. xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
  1835. xfs_btree_set_numrecs(right, rrecs);
  1836. xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
  1837. /*
  1838. * Slide the contents of right down one entry.
  1839. */
  1840. XFS_BTREE_STATS_ADD(cur, moves, rrecs - 1);
  1841. if (level > 0) {
  1842. /* It's a nonleaf. operate on keys and ptrs */
  1843. #ifdef DEBUG
  1844. int i; /* loop index */
  1845. for (i = 0; i < rrecs; i++) {
  1846. error = xfs_btree_check_ptr(cur, rpp, i + 1, level);
  1847. if (error)
  1848. goto error0;
  1849. }
  1850. #endif
  1851. xfs_btree_shift_keys(cur,
  1852. xfs_btree_key_addr(cur, 2, right),
  1853. -1, rrecs);
  1854. xfs_btree_shift_ptrs(cur,
  1855. xfs_btree_ptr_addr(cur, 2, right),
  1856. -1, rrecs);
  1857. xfs_btree_log_keys(cur, rbp, 1, rrecs);
  1858. xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
  1859. } else {
  1860. /* It's a leaf. operate on records */
  1861. xfs_btree_shift_recs(cur,
  1862. xfs_btree_rec_addr(cur, 2, right),
  1863. -1, rrecs);
  1864. xfs_btree_log_recs(cur, rbp, 1, rrecs);
  1865. /*
  1866. * If it's the first record in the block, we'll need a key
  1867. * structure to pass up to the next level (updkey).
  1868. */
  1869. cur->bc_ops->init_key_from_rec(&key,
  1870. xfs_btree_rec_addr(cur, 1, right));
  1871. rkp = &key;
  1872. }
  1873. /* Update the parent key values of right. */
  1874. error = xfs_btree_updkey(cur, rkp, level + 1);
  1875. if (error)
  1876. goto error0;
  1877. /* Slide the cursor value left one. */
  1878. cur->bc_ptrs[level]--;
  1879. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1880. *stat = 1;
  1881. return 0;
  1882. out0:
  1883. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  1884. *stat = 0;
  1885. return 0;
  1886. error0:
  1887. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  1888. return error;
  1889. }
  1890. /*
  1891. * Move 1 record right from cur/level if possible.
  1892. * Update cur to reflect the new path.
  1893. */
  1894. STATIC int /* error */
  1895. xfs_btree_rshift(
  1896. struct xfs_btree_cur *cur,
  1897. int level,
  1898. int *stat) /* success/failure */
  1899. {
  1900. union xfs_btree_key key; /* btree key */
  1901. struct xfs_buf *lbp; /* left buffer pointer */
  1902. struct xfs_btree_block *left; /* left btree block */
  1903. struct xfs_buf *rbp; /* right buffer pointer */
  1904. struct xfs_btree_block *right; /* right btree block */
  1905. struct xfs_btree_cur *tcur; /* temporary btree cursor */
  1906. union xfs_btree_ptr rptr; /* right block pointer */
  1907. union xfs_btree_key *rkp; /* right btree key */
  1908. int rrecs; /* right record count */
  1909. int lrecs; /* left record count */
  1910. int error; /* error return value */
  1911. int i; /* loop counter */
  1912. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  1913. XFS_BTREE_TRACE_ARGI(cur, level);
  1914. if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
  1915. (level == cur->bc_nlevels - 1))
  1916. goto out0;
  1917. /* Set up variables for this block as "left". */
  1918. left = xfs_btree_get_block(cur, level, &lbp);
  1919. #ifdef DEBUG
  1920. error = xfs_btree_check_block(cur, left, level, lbp);
  1921. if (error)
  1922. goto error0;
  1923. #endif
  1924. /* If we've got no right sibling then we can't shift an entry right. */
  1925. xfs_btree_get_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
  1926. if (xfs_btree_ptr_is_null(cur, &rptr))
  1927. goto out0;
  1928. /*
  1929. * If the cursor entry is the one that would be moved, don't
  1930. * do it... it's too complicated.
  1931. */
  1932. lrecs = xfs_btree_get_numrecs(left);
  1933. if (cur->bc_ptrs[level] >= lrecs)
  1934. goto out0;
  1935. /* Set up the right neighbor as "right". */
  1936. error = xfs_btree_read_buf_block(cur, &rptr, level, 0, &right, &rbp);
  1937. if (error)
  1938. goto error0;
  1939. /* If it's full, it can't take another entry. */
  1940. rrecs = xfs_btree_get_numrecs(right);
  1941. if (rrecs == cur->bc_ops->get_maxrecs(cur, level))
  1942. goto out0;
  1943. XFS_BTREE_STATS_INC(cur, rshift);
  1944. XFS_BTREE_STATS_ADD(cur, moves, rrecs);
  1945. /*
  1946. * Make a hole at the start of the right neighbor block, then
  1947. * copy the last left block entry to the hole.
  1948. */
  1949. if (level > 0) {
  1950. /* It's a nonleaf. make a hole in the keys and ptrs */
  1951. union xfs_btree_key *lkp;
  1952. union xfs_btree_ptr *lpp;
  1953. union xfs_btree_ptr *rpp;
  1954. lkp = xfs_btree_key_addr(cur, lrecs, left);
  1955. lpp = xfs_btree_ptr_addr(cur, lrecs, left);
  1956. rkp = xfs_btree_key_addr(cur, 1, right);
  1957. rpp = xfs_btree_ptr_addr(cur, 1, right);
  1958. #ifdef DEBUG
  1959. for (i = rrecs - 1; i >= 0; i--) {
  1960. error = xfs_btree_check_ptr(cur, rpp, i, level);
  1961. if (error)
  1962. goto error0;
  1963. }
  1964. #endif
  1965. xfs_btree_shift_keys(cur, rkp, 1, rrecs);
  1966. xfs_btree_shift_ptrs(cur, rpp, 1, rrecs);
  1967. #ifdef DEBUG
  1968. error = xfs_btree_check_ptr(cur, lpp, 0, level);
  1969. if (error)
  1970. goto error0;
  1971. #endif
  1972. /* Now put the new data in, and log it. */
  1973. xfs_btree_copy_keys(cur, rkp, lkp, 1);
  1974. xfs_btree_copy_ptrs(cur, rpp, lpp, 1);
  1975. xfs_btree_log_keys(cur, rbp, 1, rrecs + 1);
  1976. xfs_btree_log_ptrs(cur, rbp, 1, rrecs + 1);
  1977. ASSERT(cur->bc_ops->keys_inorder(cur, rkp,
  1978. xfs_btree_key_addr(cur, 2, right)));
  1979. } else {
  1980. /* It's a leaf. make a hole in the records */
  1981. union xfs_btree_rec *lrp;
  1982. union xfs_btree_rec *rrp;
  1983. lrp = xfs_btree_rec_addr(cur, lrecs, left);
  1984. rrp = xfs_btree_rec_addr(cur, 1, right);
  1985. xfs_btree_shift_recs(cur, rrp, 1, rrecs);
  1986. /* Now put the new data in, and log it. */
  1987. xfs_btree_copy_recs(cur, rrp, lrp, 1);
  1988. xfs_btree_log_recs(cur, rbp, 1, rrecs + 1);
  1989. cur->bc_ops->init_key_from_rec(&key, rrp);
  1990. rkp = &key;
  1991. ASSERT(cur->bc_ops->recs_inorder(cur, rrp,
  1992. xfs_btree_rec_addr(cur, 2, right)));
  1993. }
  1994. /*
  1995. * Decrement and log left's numrecs, bump and log right's numrecs.
  1996. */
  1997. xfs_btree_set_numrecs(left, --lrecs);
  1998. xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS);
  1999. xfs_btree_set_numrecs(right, ++rrecs);
  2000. xfs_btree_log_block(cur, rbp, XFS_BB_NUMRECS);
  2001. /*
  2002. * Using a temporary cursor, update the parent key values of the
  2003. * block on the right.
  2004. */
  2005. error = xfs_btree_dup_cursor(cur, &tcur);
  2006. if (error)
  2007. goto error0;
  2008. i = xfs_btree_lastrec(tcur, level);
  2009. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  2010. error = xfs_btree_increment(tcur, level, &i);
  2011. if (error)
  2012. goto error1;
  2013. error = xfs_btree_updkey(tcur, rkp, level + 1);
  2014. if (error)
  2015. goto error1;
  2016. xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
  2017. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2018. *stat = 1;
  2019. return 0;
  2020. out0:
  2021. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2022. *stat = 0;
  2023. return 0;
  2024. error0:
  2025. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  2026. return error;
  2027. error1:
  2028. XFS_BTREE_TRACE_CURSOR(tcur, XBT_ERROR);
  2029. xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
  2030. return error;
  2031. }
  2032. /*
  2033. * Split cur/level block in half.
  2034. * Return new block number and the key to its first
  2035. * record (to be inserted into parent).
  2036. */
  2037. STATIC int /* error */
  2038. xfs_btree_split(
  2039. struct xfs_btree_cur *cur,
  2040. int level,
  2041. union xfs_btree_ptr *ptrp,
  2042. union xfs_btree_key *key,
  2043. struct xfs_btree_cur **curp,
  2044. int *stat) /* success/failure */
  2045. {
  2046. union xfs_btree_ptr lptr; /* left sibling block ptr */
  2047. struct xfs_buf *lbp; /* left buffer pointer */
  2048. struct xfs_btree_block *left; /* left btree block */
  2049. union xfs_btree_ptr rptr; /* right sibling block ptr */
  2050. struct xfs_buf *rbp; /* right buffer pointer */
  2051. struct xfs_btree_block *right; /* right btree block */
  2052. union xfs_btree_ptr rrptr; /* right-right sibling ptr */
  2053. struct xfs_buf *rrbp; /* right-right buffer pointer */
  2054. struct xfs_btree_block *rrblock; /* right-right btree block */
  2055. int lrecs;
  2056. int rrecs;
  2057. int src_index;
  2058. int error; /* error return value */
  2059. #ifdef DEBUG
  2060. int i;
  2061. #endif
  2062. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  2063. XFS_BTREE_TRACE_ARGIPK(cur, level, *ptrp, key);
  2064. XFS_BTREE_STATS_INC(cur, split);
  2065. /* Set up left block (current one). */
  2066. left = xfs_btree_get_block(cur, level, &lbp);
  2067. #ifdef DEBUG
  2068. error = xfs_btree_check_block(cur, left, level, lbp);
  2069. if (error)
  2070. goto error0;
  2071. #endif
  2072. xfs_btree_buf_to_ptr(cur, lbp, &lptr);
  2073. /* Allocate the new block. If we can't do it, we're toast. Give up. */
  2074. error = cur->bc_ops->alloc_block(cur, &lptr, &rptr, 1, stat);
  2075. if (error)
  2076. goto error0;
  2077. if (*stat == 0)
  2078. goto out0;
  2079. XFS_BTREE_STATS_INC(cur, alloc);
  2080. /* Set up the new block as "right". */
  2081. error = xfs_btree_get_buf_block(cur, &rptr, 0, &right, &rbp);
  2082. if (error)
  2083. goto error0;
  2084. /* Fill in the btree header for the new right block. */
  2085. xfs_btree_init_block_cur(cur, rbp, xfs_btree_get_level(left), 0);
  2086. /*
  2087. * Split the entries between the old and the new block evenly.
  2088. * Make sure that if there's an odd number of entries now, that
  2089. * each new block will have the same number of entries.
  2090. */
  2091. lrecs = xfs_btree_get_numrecs(left);
  2092. rrecs = lrecs / 2;
  2093. if ((lrecs & 1) && cur->bc_ptrs[level] <= rrecs + 1)
  2094. rrecs++;
  2095. src_index = (lrecs - rrecs + 1);
  2096. XFS_BTREE_STATS_ADD(cur, moves, rrecs);
  2097. /*
  2098. * Copy btree block entries from the left block over to the
  2099. * new block, the right. Update the right block and log the
  2100. * changes.
  2101. */
  2102. if (level > 0) {
  2103. /* It's a non-leaf. Move keys and pointers. */
  2104. union xfs_btree_key *lkp; /* left btree key */
  2105. union xfs_btree_ptr *lpp; /* left address pointer */
  2106. union xfs_btree_key *rkp; /* right btree key */
  2107. union xfs_btree_ptr *rpp; /* right address pointer */
  2108. lkp = xfs_btree_key_addr(cur, src_index, left);
  2109. lpp = xfs_btree_ptr_addr(cur, src_index, left);
  2110. rkp = xfs_btree_key_addr(cur, 1, right);
  2111. rpp = xfs_btree_ptr_addr(cur, 1, right);
  2112. #ifdef DEBUG
  2113. for (i = src_index; i < rrecs; i++) {
  2114. error = xfs_btree_check_ptr(cur, lpp, i, level);
  2115. if (error)
  2116. goto error0;
  2117. }
  2118. #endif
  2119. xfs_btree_copy_keys(cur, rkp, lkp, rrecs);
  2120. xfs_btree_copy_ptrs(cur, rpp, lpp, rrecs);
  2121. xfs_btree_log_keys(cur, rbp, 1, rrecs);
  2122. xfs_btree_log_ptrs(cur, rbp, 1, rrecs);
  2123. /* Grab the keys to the entries moved to the right block */
  2124. xfs_btree_copy_keys(cur, key, rkp, 1);
  2125. } else {
  2126. /* It's a leaf. Move records. */
  2127. union xfs_btree_rec *lrp; /* left record pointer */
  2128. union xfs_btree_rec *rrp; /* right record pointer */
  2129. lrp = xfs_btree_rec_addr(cur, src_index, left);
  2130. rrp = xfs_btree_rec_addr(cur, 1, right);
  2131. xfs_btree_copy_recs(cur, rrp, lrp, rrecs);
  2132. xfs_btree_log_recs(cur, rbp, 1, rrecs);
  2133. cur->bc_ops->init_key_from_rec(key,
  2134. xfs_btree_rec_addr(cur, 1, right));
  2135. }
  2136. /*
  2137. * Find the left block number by looking in the buffer.
  2138. * Adjust numrecs, sibling pointers.
  2139. */
  2140. xfs_btree_get_sibling(cur, left, &rrptr, XFS_BB_RIGHTSIB);
  2141. xfs_btree_set_sibling(cur, right, &rrptr, XFS_BB_RIGHTSIB);
  2142. xfs_btree_set_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
  2143. xfs_btree_set_sibling(cur, left, &rptr, XFS_BB_RIGHTSIB);
  2144. lrecs -= rrecs;
  2145. xfs_btree_set_numrecs(left, lrecs);
  2146. xfs_btree_set_numrecs(right, xfs_btree_get_numrecs(right) + rrecs);
  2147. xfs_btree_log_block(cur, rbp, XFS_BB_ALL_BITS);
  2148. xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
  2149. /*
  2150. * If there's a block to the new block's right, make that block
  2151. * point back to right instead of to left.
  2152. */
  2153. if (!xfs_btree_ptr_is_null(cur, &rrptr)) {
  2154. error = xfs_btree_read_buf_block(cur, &rrptr, level,
  2155. 0, &rrblock, &rrbp);
  2156. if (error)
  2157. goto error0;
  2158. xfs_btree_set_sibling(cur, rrblock, &rptr, XFS_BB_LEFTSIB);
  2159. xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
  2160. }
  2161. /*
  2162. * If the cursor is really in the right block, move it there.
  2163. * If it's just pointing past the last entry in left, then we'll
  2164. * insert there, so don't change anything in that case.
  2165. */
  2166. if (cur->bc_ptrs[level] > lrecs + 1) {
  2167. xfs_btree_setbuf(cur, level, rbp);
  2168. cur->bc_ptrs[level] -= lrecs;
  2169. }
  2170. /*
  2171. * If there are more levels, we'll need another cursor which refers
  2172. * the right block, no matter where this cursor was.
  2173. */
  2174. if (level + 1 < cur->bc_nlevels) {
  2175. error = xfs_btree_dup_cursor(cur, curp);
  2176. if (error)
  2177. goto error0;
  2178. (*curp)->bc_ptrs[level + 1]++;
  2179. }
  2180. *ptrp = rptr;
  2181. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2182. *stat = 1;
  2183. return 0;
  2184. out0:
  2185. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2186. *stat = 0;
  2187. return 0;
  2188. error0:
  2189. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  2190. return error;
  2191. }
  2192. /*
  2193. * Copy the old inode root contents into a real block and make the
  2194. * broot point to it.
  2195. */
  2196. int /* error */
  2197. xfs_btree_new_iroot(
  2198. struct xfs_btree_cur *cur, /* btree cursor */
  2199. int *logflags, /* logging flags for inode */
  2200. int *stat) /* return status - 0 fail */
  2201. {
  2202. struct xfs_buf *cbp; /* buffer for cblock */
  2203. struct xfs_btree_block *block; /* btree block */
  2204. struct xfs_btree_block *cblock; /* child btree block */
  2205. union xfs_btree_key *ckp; /* child key pointer */
  2206. union xfs_btree_ptr *cpp; /* child ptr pointer */
  2207. union xfs_btree_key *kp; /* pointer to btree key */
  2208. union xfs_btree_ptr *pp; /* pointer to block addr */
  2209. union xfs_btree_ptr nptr; /* new block addr */
  2210. int level; /* btree level */
  2211. int error; /* error return code */
  2212. #ifdef DEBUG
  2213. int i; /* loop counter */
  2214. #endif
  2215. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  2216. XFS_BTREE_STATS_INC(cur, newroot);
  2217. ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
  2218. level = cur->bc_nlevels - 1;
  2219. block = xfs_btree_get_iroot(cur);
  2220. pp = xfs_btree_ptr_addr(cur, 1, block);
  2221. /* Allocate the new block. If we can't do it, we're toast. Give up. */
  2222. error = cur->bc_ops->alloc_block(cur, pp, &nptr, 1, stat);
  2223. if (error)
  2224. goto error0;
  2225. if (*stat == 0) {
  2226. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2227. return 0;
  2228. }
  2229. XFS_BTREE_STATS_INC(cur, alloc);
  2230. /* Copy the root into a real block. */
  2231. error = xfs_btree_get_buf_block(cur, &nptr, 0, &cblock, &cbp);
  2232. if (error)
  2233. goto error0;
  2234. /*
  2235. * we can't just memcpy() the root in for CRC enabled btree blocks.
  2236. * In that case have to also ensure the blkno remains correct
  2237. */
  2238. memcpy(cblock, block, xfs_btree_block_len(cur));
  2239. if (cur->bc_flags & XFS_BTREE_CRC_BLOCKS) {
  2240. if (cur->bc_flags & XFS_BTREE_LONG_PTRS)
  2241. cblock->bb_u.l.bb_blkno = cpu_to_be64(cbp->b_bn);
  2242. else
  2243. cblock->bb_u.s.bb_blkno = cpu_to_be64(cbp->b_bn);
  2244. }
  2245. be16_add_cpu(&block->bb_level, 1);
  2246. xfs_btree_set_numrecs(block, 1);
  2247. cur->bc_nlevels++;
  2248. cur->bc_ptrs[level + 1] = 1;
  2249. kp = xfs_btree_key_addr(cur, 1, block);
  2250. ckp = xfs_btree_key_addr(cur, 1, cblock);
  2251. xfs_btree_copy_keys(cur, ckp, kp, xfs_btree_get_numrecs(cblock));
  2252. cpp = xfs_btree_ptr_addr(cur, 1, cblock);
  2253. #ifdef DEBUG
  2254. for (i = 0; i < be16_to_cpu(cblock->bb_numrecs); i++) {
  2255. error = xfs_btree_check_ptr(cur, pp, i, level);
  2256. if (error)
  2257. goto error0;
  2258. }
  2259. #endif
  2260. xfs_btree_copy_ptrs(cur, cpp, pp, xfs_btree_get_numrecs(cblock));
  2261. #ifdef DEBUG
  2262. error = xfs_btree_check_ptr(cur, &nptr, 0, level);
  2263. if (error)
  2264. goto error0;
  2265. #endif
  2266. xfs_btree_copy_ptrs(cur, pp, &nptr, 1);
  2267. xfs_iroot_realloc(cur->bc_private.b.ip,
  2268. 1 - xfs_btree_get_numrecs(cblock),
  2269. cur->bc_private.b.whichfork);
  2270. xfs_btree_setbuf(cur, level, cbp);
  2271. /*
  2272. * Do all this logging at the end so that
  2273. * the root is at the right level.
  2274. */
  2275. xfs_btree_log_block(cur, cbp, XFS_BB_ALL_BITS);
  2276. xfs_btree_log_keys(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
  2277. xfs_btree_log_ptrs(cur, cbp, 1, be16_to_cpu(cblock->bb_numrecs));
  2278. *logflags |=
  2279. XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork);
  2280. *stat = 1;
  2281. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2282. return 0;
  2283. error0:
  2284. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  2285. return error;
  2286. }
  2287. /*
  2288. * Allocate a new root block, fill it in.
  2289. */
  2290. STATIC int /* error */
  2291. xfs_btree_new_root(
  2292. struct xfs_btree_cur *cur, /* btree cursor */
  2293. int *stat) /* success/failure */
  2294. {
  2295. struct xfs_btree_block *block; /* one half of the old root block */
  2296. struct xfs_buf *bp; /* buffer containing block */
  2297. int error; /* error return value */
  2298. struct xfs_buf *lbp; /* left buffer pointer */
  2299. struct xfs_btree_block *left; /* left btree block */
  2300. struct xfs_buf *nbp; /* new (root) buffer */
  2301. struct xfs_btree_block *new; /* new (root) btree block */
  2302. int nptr; /* new value for key index, 1 or 2 */
  2303. struct xfs_buf *rbp; /* right buffer pointer */
  2304. struct xfs_btree_block *right; /* right btree block */
  2305. union xfs_btree_ptr rptr;
  2306. union xfs_btree_ptr lptr;
  2307. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  2308. XFS_BTREE_STATS_INC(cur, newroot);
  2309. /* initialise our start point from the cursor */
  2310. cur->bc_ops->init_ptr_from_cur(cur, &rptr);
  2311. /* Allocate the new block. If we can't do it, we're toast. Give up. */
  2312. error = cur->bc_ops->alloc_block(cur, &rptr, &lptr, 1, stat);
  2313. if (error)
  2314. goto error0;
  2315. if (*stat == 0)
  2316. goto out0;
  2317. XFS_BTREE_STATS_INC(cur, alloc);
  2318. /* Set up the new block. */
  2319. error = xfs_btree_get_buf_block(cur, &lptr, 0, &new, &nbp);
  2320. if (error)
  2321. goto error0;
  2322. /* Set the root in the holding structure increasing the level by 1. */
  2323. cur->bc_ops->set_root(cur, &lptr, 1);
  2324. /*
  2325. * At the previous root level there are now two blocks: the old root,
  2326. * and the new block generated when it was split. We don't know which
  2327. * one the cursor is pointing at, so we set up variables "left" and
  2328. * "right" for each case.
  2329. */
  2330. block = xfs_btree_get_block(cur, cur->bc_nlevels - 1, &bp);
  2331. #ifdef DEBUG
  2332. error = xfs_btree_check_block(cur, block, cur->bc_nlevels - 1, bp);
  2333. if (error)
  2334. goto error0;
  2335. #endif
  2336. xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
  2337. if (!xfs_btree_ptr_is_null(cur, &rptr)) {
  2338. /* Our block is left, pick up the right block. */
  2339. lbp = bp;
  2340. xfs_btree_buf_to_ptr(cur, lbp, &lptr);
  2341. left = block;
  2342. error = xfs_btree_read_buf_block(cur, &rptr,
  2343. cur->bc_nlevels - 1, 0, &right, &rbp);
  2344. if (error)
  2345. goto error0;
  2346. bp = rbp;
  2347. nptr = 1;
  2348. } else {
  2349. /* Our block is right, pick up the left block. */
  2350. rbp = bp;
  2351. xfs_btree_buf_to_ptr(cur, rbp, &rptr);
  2352. right = block;
  2353. xfs_btree_get_sibling(cur, right, &lptr, XFS_BB_LEFTSIB);
  2354. error = xfs_btree_read_buf_block(cur, &lptr,
  2355. cur->bc_nlevels - 1, 0, &left, &lbp);
  2356. if (error)
  2357. goto error0;
  2358. bp = lbp;
  2359. nptr = 2;
  2360. }
  2361. /* Fill in the new block's btree header and log it. */
  2362. xfs_btree_init_block_cur(cur, nbp, cur->bc_nlevels, 2);
  2363. xfs_btree_log_block(cur, nbp, XFS_BB_ALL_BITS);
  2364. ASSERT(!xfs_btree_ptr_is_null(cur, &lptr) &&
  2365. !xfs_btree_ptr_is_null(cur, &rptr));
  2366. /* Fill in the key data in the new root. */
  2367. if (xfs_btree_get_level(left) > 0) {
  2368. xfs_btree_copy_keys(cur,
  2369. xfs_btree_key_addr(cur, 1, new),
  2370. xfs_btree_key_addr(cur, 1, left), 1);
  2371. xfs_btree_copy_keys(cur,
  2372. xfs_btree_key_addr(cur, 2, new),
  2373. xfs_btree_key_addr(cur, 1, right), 1);
  2374. } else {
  2375. cur->bc_ops->init_key_from_rec(
  2376. xfs_btree_key_addr(cur, 1, new),
  2377. xfs_btree_rec_addr(cur, 1, left));
  2378. cur->bc_ops->init_key_from_rec(
  2379. xfs_btree_key_addr(cur, 2, new),
  2380. xfs_btree_rec_addr(cur, 1, right));
  2381. }
  2382. xfs_btree_log_keys(cur, nbp, 1, 2);
  2383. /* Fill in the pointer data in the new root. */
  2384. xfs_btree_copy_ptrs(cur,
  2385. xfs_btree_ptr_addr(cur, 1, new), &lptr, 1);
  2386. xfs_btree_copy_ptrs(cur,
  2387. xfs_btree_ptr_addr(cur, 2, new), &rptr, 1);
  2388. xfs_btree_log_ptrs(cur, nbp, 1, 2);
  2389. /* Fix up the cursor. */
  2390. xfs_btree_setbuf(cur, cur->bc_nlevels, nbp);
  2391. cur->bc_ptrs[cur->bc_nlevels] = nptr;
  2392. cur->bc_nlevels++;
  2393. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2394. *stat = 1;
  2395. return 0;
  2396. error0:
  2397. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  2398. return error;
  2399. out0:
  2400. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2401. *stat = 0;
  2402. return 0;
  2403. }
  2404. STATIC int
  2405. xfs_btree_make_block_unfull(
  2406. struct xfs_btree_cur *cur, /* btree cursor */
  2407. int level, /* btree level */
  2408. int numrecs,/* # of recs in block */
  2409. int *oindex,/* old tree index */
  2410. int *index, /* new tree index */
  2411. union xfs_btree_ptr *nptr, /* new btree ptr */
  2412. struct xfs_btree_cur **ncur, /* new btree cursor */
  2413. union xfs_btree_rec *nrec, /* new record */
  2414. int *stat)
  2415. {
  2416. union xfs_btree_key key; /* new btree key value */
  2417. int error = 0;
  2418. if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
  2419. level == cur->bc_nlevels - 1) {
  2420. struct xfs_inode *ip = cur->bc_private.b.ip;
  2421. if (numrecs < cur->bc_ops->get_dmaxrecs(cur, level)) {
  2422. /* A root block that can be made bigger. */
  2423. xfs_iroot_realloc(ip, 1, cur->bc_private.b.whichfork);
  2424. } else {
  2425. /* A root block that needs replacing */
  2426. int logflags = 0;
  2427. error = xfs_btree_new_iroot(cur, &logflags, stat);
  2428. if (error || *stat == 0)
  2429. return error;
  2430. xfs_trans_log_inode(cur->bc_tp, ip, logflags);
  2431. }
  2432. return 0;
  2433. }
  2434. /* First, try shifting an entry to the right neighbor. */
  2435. error = xfs_btree_rshift(cur, level, stat);
  2436. if (error || *stat)
  2437. return error;
  2438. /* Next, try shifting an entry to the left neighbor. */
  2439. error = xfs_btree_lshift(cur, level, stat);
  2440. if (error)
  2441. return error;
  2442. if (*stat) {
  2443. *oindex = *index = cur->bc_ptrs[level];
  2444. return 0;
  2445. }
  2446. /*
  2447. * Next, try splitting the current block in half.
  2448. *
  2449. * If this works we have to re-set our variables because we
  2450. * could be in a different block now.
  2451. */
  2452. error = xfs_btree_split(cur, level, nptr, &key, ncur, stat);
  2453. if (error || *stat == 0)
  2454. return error;
  2455. *index = cur->bc_ptrs[level];
  2456. cur->bc_ops->init_rec_from_key(&key, nrec);
  2457. return 0;
  2458. }
  2459. /*
  2460. * Insert one record/level. Return information to the caller
  2461. * allowing the next level up to proceed if necessary.
  2462. */
  2463. STATIC int
  2464. xfs_btree_insrec(
  2465. struct xfs_btree_cur *cur, /* btree cursor */
  2466. int level, /* level to insert record at */
  2467. union xfs_btree_ptr *ptrp, /* i/o: block number inserted */
  2468. union xfs_btree_rec *recp, /* i/o: record data inserted */
  2469. struct xfs_btree_cur **curp, /* output: new cursor replacing cur */
  2470. int *stat) /* success/failure */
  2471. {
  2472. struct xfs_btree_block *block; /* btree block */
  2473. struct xfs_buf *bp; /* buffer for block */
  2474. union xfs_btree_key key; /* btree key */
  2475. union xfs_btree_ptr nptr; /* new block ptr */
  2476. struct xfs_btree_cur *ncur; /* new btree cursor */
  2477. union xfs_btree_rec nrec; /* new record count */
  2478. int optr; /* old key/record index */
  2479. int ptr; /* key/record index */
  2480. int numrecs;/* number of records */
  2481. int error; /* error return value */
  2482. #ifdef DEBUG
  2483. int i;
  2484. #endif
  2485. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  2486. XFS_BTREE_TRACE_ARGIPR(cur, level, *ptrp, recp);
  2487. ncur = NULL;
  2488. /*
  2489. * If we have an external root pointer, and we've made it to the
  2490. * root level, allocate a new root block and we're done.
  2491. */
  2492. if (!(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) &&
  2493. (level >= cur->bc_nlevels)) {
  2494. error = xfs_btree_new_root(cur, stat);
  2495. xfs_btree_set_ptr_null(cur, ptrp);
  2496. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2497. return error;
  2498. }
  2499. /* If we're off the left edge, return failure. */
  2500. ptr = cur->bc_ptrs[level];
  2501. if (ptr == 0) {
  2502. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2503. *stat = 0;
  2504. return 0;
  2505. }
  2506. /* Make a key out of the record data to be inserted, and save it. */
  2507. cur->bc_ops->init_key_from_rec(&key, recp);
  2508. optr = ptr;
  2509. XFS_BTREE_STATS_INC(cur, insrec);
  2510. /* Get pointers to the btree buffer and block. */
  2511. block = xfs_btree_get_block(cur, level, &bp);
  2512. numrecs = xfs_btree_get_numrecs(block);
  2513. #ifdef DEBUG
  2514. error = xfs_btree_check_block(cur, block, level, bp);
  2515. if (error)
  2516. goto error0;
  2517. /* Check that the new entry is being inserted in the right place. */
  2518. if (ptr <= numrecs) {
  2519. if (level == 0) {
  2520. ASSERT(cur->bc_ops->recs_inorder(cur, recp,
  2521. xfs_btree_rec_addr(cur, ptr, block)));
  2522. } else {
  2523. ASSERT(cur->bc_ops->keys_inorder(cur, &key,
  2524. xfs_btree_key_addr(cur, ptr, block)));
  2525. }
  2526. }
  2527. #endif
  2528. /*
  2529. * If the block is full, we can't insert the new entry until we
  2530. * make the block un-full.
  2531. */
  2532. xfs_btree_set_ptr_null(cur, &nptr);
  2533. if (numrecs == cur->bc_ops->get_maxrecs(cur, level)) {
  2534. error = xfs_btree_make_block_unfull(cur, level, numrecs,
  2535. &optr, &ptr, &nptr, &ncur, &nrec, stat);
  2536. if (error || *stat == 0)
  2537. goto error0;
  2538. }
  2539. /*
  2540. * The current block may have changed if the block was
  2541. * previously full and we have just made space in it.
  2542. */
  2543. block = xfs_btree_get_block(cur, level, &bp);
  2544. numrecs = xfs_btree_get_numrecs(block);
  2545. #ifdef DEBUG
  2546. error = xfs_btree_check_block(cur, block, level, bp);
  2547. if (error)
  2548. return error;
  2549. #endif
  2550. /*
  2551. * At this point we know there's room for our new entry in the block
  2552. * we're pointing at.
  2553. */
  2554. XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr + 1);
  2555. if (level > 0) {
  2556. /* It's a nonleaf. make a hole in the keys and ptrs */
  2557. union xfs_btree_key *kp;
  2558. union xfs_btree_ptr *pp;
  2559. kp = xfs_btree_key_addr(cur, ptr, block);
  2560. pp = xfs_btree_ptr_addr(cur, ptr, block);
  2561. #ifdef DEBUG
  2562. for (i = numrecs - ptr; i >= 0; i--) {
  2563. error = xfs_btree_check_ptr(cur, pp, i, level);
  2564. if (error)
  2565. return error;
  2566. }
  2567. #endif
  2568. xfs_btree_shift_keys(cur, kp, 1, numrecs - ptr + 1);
  2569. xfs_btree_shift_ptrs(cur, pp, 1, numrecs - ptr + 1);
  2570. #ifdef DEBUG
  2571. error = xfs_btree_check_ptr(cur, ptrp, 0, level);
  2572. if (error)
  2573. goto error0;
  2574. #endif
  2575. /* Now put the new data in, bump numrecs and log it. */
  2576. xfs_btree_copy_keys(cur, kp, &key, 1);
  2577. xfs_btree_copy_ptrs(cur, pp, ptrp, 1);
  2578. numrecs++;
  2579. xfs_btree_set_numrecs(block, numrecs);
  2580. xfs_btree_log_ptrs(cur, bp, ptr, numrecs);
  2581. xfs_btree_log_keys(cur, bp, ptr, numrecs);
  2582. #ifdef DEBUG
  2583. if (ptr < numrecs) {
  2584. ASSERT(cur->bc_ops->keys_inorder(cur, kp,
  2585. xfs_btree_key_addr(cur, ptr + 1, block)));
  2586. }
  2587. #endif
  2588. } else {
  2589. /* It's a leaf. make a hole in the records */
  2590. union xfs_btree_rec *rp;
  2591. rp = xfs_btree_rec_addr(cur, ptr, block);
  2592. xfs_btree_shift_recs(cur, rp, 1, numrecs - ptr + 1);
  2593. /* Now put the new data in, bump numrecs and log it. */
  2594. xfs_btree_copy_recs(cur, rp, recp, 1);
  2595. xfs_btree_set_numrecs(block, ++numrecs);
  2596. xfs_btree_log_recs(cur, bp, ptr, numrecs);
  2597. #ifdef DEBUG
  2598. if (ptr < numrecs) {
  2599. ASSERT(cur->bc_ops->recs_inorder(cur, rp,
  2600. xfs_btree_rec_addr(cur, ptr + 1, block)));
  2601. }
  2602. #endif
  2603. }
  2604. /* Log the new number of records in the btree header. */
  2605. xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
  2606. /* If we inserted at the start of a block, update the parents' keys. */
  2607. if (optr == 1) {
  2608. error = xfs_btree_updkey(cur, &key, level + 1);
  2609. if (error)
  2610. goto error0;
  2611. }
  2612. /*
  2613. * If we are tracking the last record in the tree and
  2614. * we are at the far right edge of the tree, update it.
  2615. */
  2616. if (xfs_btree_is_lastrec(cur, block, level)) {
  2617. cur->bc_ops->update_lastrec(cur, block, recp,
  2618. ptr, LASTREC_INSREC);
  2619. }
  2620. /*
  2621. * Return the new block number, if any.
  2622. * If there is one, give back a record value and a cursor too.
  2623. */
  2624. *ptrp = nptr;
  2625. if (!xfs_btree_ptr_is_null(cur, &nptr)) {
  2626. *recp = nrec;
  2627. *curp = ncur;
  2628. }
  2629. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2630. *stat = 1;
  2631. return 0;
  2632. error0:
  2633. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  2634. return error;
  2635. }
  2636. /*
  2637. * Insert the record at the point referenced by cur.
  2638. *
  2639. * A multi-level split of the tree on insert will invalidate the original
  2640. * cursor. All callers of this function should assume that the cursor is
  2641. * no longer valid and revalidate it.
  2642. */
  2643. int
  2644. xfs_btree_insert(
  2645. struct xfs_btree_cur *cur,
  2646. int *stat)
  2647. {
  2648. int error; /* error return value */
  2649. int i; /* result value, 0 for failure */
  2650. int level; /* current level number in btree */
  2651. union xfs_btree_ptr nptr; /* new block number (split result) */
  2652. struct xfs_btree_cur *ncur; /* new cursor (split result) */
  2653. struct xfs_btree_cur *pcur; /* previous level's cursor */
  2654. union xfs_btree_rec rec; /* record to insert */
  2655. level = 0;
  2656. ncur = NULL;
  2657. pcur = cur;
  2658. xfs_btree_set_ptr_null(cur, &nptr);
  2659. cur->bc_ops->init_rec_from_cur(cur, &rec);
  2660. /*
  2661. * Loop going up the tree, starting at the leaf level.
  2662. * Stop when we don't get a split block, that must mean that
  2663. * the insert is finished with this level.
  2664. */
  2665. do {
  2666. /*
  2667. * Insert nrec/nptr into this level of the tree.
  2668. * Note if we fail, nptr will be null.
  2669. */
  2670. error = xfs_btree_insrec(pcur, level, &nptr, &rec, &ncur, &i);
  2671. if (error) {
  2672. if (pcur != cur)
  2673. xfs_btree_del_cursor(pcur, XFS_BTREE_ERROR);
  2674. goto error0;
  2675. }
  2676. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  2677. level++;
  2678. /*
  2679. * See if the cursor we just used is trash.
  2680. * Can't trash the caller's cursor, but otherwise we should
  2681. * if ncur is a new cursor or we're about to be done.
  2682. */
  2683. if (pcur != cur &&
  2684. (ncur || xfs_btree_ptr_is_null(cur, &nptr))) {
  2685. /* Save the state from the cursor before we trash it */
  2686. if (cur->bc_ops->update_cursor)
  2687. cur->bc_ops->update_cursor(pcur, cur);
  2688. cur->bc_nlevels = pcur->bc_nlevels;
  2689. xfs_btree_del_cursor(pcur, XFS_BTREE_NOERROR);
  2690. }
  2691. /* If we got a new cursor, switch to it. */
  2692. if (ncur) {
  2693. pcur = ncur;
  2694. ncur = NULL;
  2695. }
  2696. } while (!xfs_btree_ptr_is_null(cur, &nptr));
  2697. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2698. *stat = i;
  2699. return 0;
  2700. error0:
  2701. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  2702. return error;
  2703. }
  2704. /*
  2705. * Try to merge a non-leaf block back into the inode root.
  2706. *
  2707. * Note: the killroot names comes from the fact that we're effectively
  2708. * killing the old root block. But because we can't just delete the
  2709. * inode we have to copy the single block it was pointing to into the
  2710. * inode.
  2711. */
  2712. STATIC int
  2713. xfs_btree_kill_iroot(
  2714. struct xfs_btree_cur *cur)
  2715. {
  2716. int whichfork = cur->bc_private.b.whichfork;
  2717. struct xfs_inode *ip = cur->bc_private.b.ip;
  2718. struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
  2719. struct xfs_btree_block *block;
  2720. struct xfs_btree_block *cblock;
  2721. union xfs_btree_key *kp;
  2722. union xfs_btree_key *ckp;
  2723. union xfs_btree_ptr *pp;
  2724. union xfs_btree_ptr *cpp;
  2725. struct xfs_buf *cbp;
  2726. int level;
  2727. int index;
  2728. int numrecs;
  2729. #ifdef DEBUG
  2730. union xfs_btree_ptr ptr;
  2731. int i;
  2732. #endif
  2733. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  2734. ASSERT(cur->bc_flags & XFS_BTREE_ROOT_IN_INODE);
  2735. ASSERT(cur->bc_nlevels > 1);
  2736. /*
  2737. * Don't deal with the root block needs to be a leaf case.
  2738. * We're just going to turn the thing back into extents anyway.
  2739. */
  2740. level = cur->bc_nlevels - 1;
  2741. if (level == 1)
  2742. goto out0;
  2743. /*
  2744. * Give up if the root has multiple children.
  2745. */
  2746. block = xfs_btree_get_iroot(cur);
  2747. if (xfs_btree_get_numrecs(block) != 1)
  2748. goto out0;
  2749. cblock = xfs_btree_get_block(cur, level - 1, &cbp);
  2750. numrecs = xfs_btree_get_numrecs(cblock);
  2751. /*
  2752. * Only do this if the next level will fit.
  2753. * Then the data must be copied up to the inode,
  2754. * instead of freeing the root you free the next level.
  2755. */
  2756. if (numrecs > cur->bc_ops->get_dmaxrecs(cur, level))
  2757. goto out0;
  2758. XFS_BTREE_STATS_INC(cur, killroot);
  2759. #ifdef DEBUG
  2760. xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_LEFTSIB);
  2761. ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
  2762. xfs_btree_get_sibling(cur, block, &ptr, XFS_BB_RIGHTSIB);
  2763. ASSERT(xfs_btree_ptr_is_null(cur, &ptr));
  2764. #endif
  2765. index = numrecs - cur->bc_ops->get_maxrecs(cur, level);
  2766. if (index) {
  2767. xfs_iroot_realloc(cur->bc_private.b.ip, index,
  2768. cur->bc_private.b.whichfork);
  2769. block = ifp->if_broot;
  2770. }
  2771. be16_add_cpu(&block->bb_numrecs, index);
  2772. ASSERT(block->bb_numrecs == cblock->bb_numrecs);
  2773. kp = xfs_btree_key_addr(cur, 1, block);
  2774. ckp = xfs_btree_key_addr(cur, 1, cblock);
  2775. xfs_btree_copy_keys(cur, kp, ckp, numrecs);
  2776. pp = xfs_btree_ptr_addr(cur, 1, block);
  2777. cpp = xfs_btree_ptr_addr(cur, 1, cblock);
  2778. #ifdef DEBUG
  2779. for (i = 0; i < numrecs; i++) {
  2780. int error;
  2781. error = xfs_btree_check_ptr(cur, cpp, i, level - 1);
  2782. if (error) {
  2783. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  2784. return error;
  2785. }
  2786. }
  2787. #endif
  2788. xfs_btree_copy_ptrs(cur, pp, cpp, numrecs);
  2789. cur->bc_ops->free_block(cur, cbp);
  2790. XFS_BTREE_STATS_INC(cur, free);
  2791. cur->bc_bufs[level - 1] = NULL;
  2792. be16_add_cpu(&block->bb_level, -1);
  2793. xfs_trans_log_inode(cur->bc_tp, ip,
  2794. XFS_ILOG_CORE | xfs_ilog_fbroot(cur->bc_private.b.whichfork));
  2795. cur->bc_nlevels--;
  2796. out0:
  2797. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2798. return 0;
  2799. }
  2800. /*
  2801. * Kill the current root node, and replace it with it's only child node.
  2802. */
  2803. STATIC int
  2804. xfs_btree_kill_root(
  2805. struct xfs_btree_cur *cur,
  2806. struct xfs_buf *bp,
  2807. int level,
  2808. union xfs_btree_ptr *newroot)
  2809. {
  2810. int error;
  2811. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  2812. XFS_BTREE_STATS_INC(cur, killroot);
  2813. /*
  2814. * Update the root pointer, decreasing the level by 1 and then
  2815. * free the old root.
  2816. */
  2817. cur->bc_ops->set_root(cur, newroot, -1);
  2818. error = cur->bc_ops->free_block(cur, bp);
  2819. if (error) {
  2820. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  2821. return error;
  2822. }
  2823. XFS_BTREE_STATS_INC(cur, free);
  2824. cur->bc_bufs[level] = NULL;
  2825. cur->bc_ra[level] = 0;
  2826. cur->bc_nlevels--;
  2827. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2828. return 0;
  2829. }
  2830. STATIC int
  2831. xfs_btree_dec_cursor(
  2832. struct xfs_btree_cur *cur,
  2833. int level,
  2834. int *stat)
  2835. {
  2836. int error;
  2837. int i;
  2838. if (level > 0) {
  2839. error = xfs_btree_decrement(cur, level, &i);
  2840. if (error)
  2841. return error;
  2842. }
  2843. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2844. *stat = 1;
  2845. return 0;
  2846. }
  2847. /*
  2848. * Single level of the btree record deletion routine.
  2849. * Delete record pointed to by cur/level.
  2850. * Remove the record from its block then rebalance the tree.
  2851. * Return 0 for error, 1 for done, 2 to go on to the next level.
  2852. */
  2853. STATIC int /* error */
  2854. xfs_btree_delrec(
  2855. struct xfs_btree_cur *cur, /* btree cursor */
  2856. int level, /* level removing record from */
  2857. int *stat) /* fail/done/go-on */
  2858. {
  2859. struct xfs_btree_block *block; /* btree block */
  2860. union xfs_btree_ptr cptr; /* current block ptr */
  2861. struct xfs_buf *bp; /* buffer for block */
  2862. int error; /* error return value */
  2863. int i; /* loop counter */
  2864. union xfs_btree_key key; /* storage for keyp */
  2865. union xfs_btree_key *keyp = &key; /* passed to the next level */
  2866. union xfs_btree_ptr lptr; /* left sibling block ptr */
  2867. struct xfs_buf *lbp; /* left buffer pointer */
  2868. struct xfs_btree_block *left; /* left btree block */
  2869. int lrecs = 0; /* left record count */
  2870. int ptr; /* key/record index */
  2871. union xfs_btree_ptr rptr; /* right sibling block ptr */
  2872. struct xfs_buf *rbp; /* right buffer pointer */
  2873. struct xfs_btree_block *right; /* right btree block */
  2874. struct xfs_btree_block *rrblock; /* right-right btree block */
  2875. struct xfs_buf *rrbp; /* right-right buffer pointer */
  2876. int rrecs = 0; /* right record count */
  2877. struct xfs_btree_cur *tcur; /* temporary btree cursor */
  2878. int numrecs; /* temporary numrec count */
  2879. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  2880. XFS_BTREE_TRACE_ARGI(cur, level);
  2881. tcur = NULL;
  2882. /* Get the index of the entry being deleted, check for nothing there. */
  2883. ptr = cur->bc_ptrs[level];
  2884. if (ptr == 0) {
  2885. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2886. *stat = 0;
  2887. return 0;
  2888. }
  2889. /* Get the buffer & block containing the record or key/ptr. */
  2890. block = xfs_btree_get_block(cur, level, &bp);
  2891. numrecs = xfs_btree_get_numrecs(block);
  2892. #ifdef DEBUG
  2893. error = xfs_btree_check_block(cur, block, level, bp);
  2894. if (error)
  2895. goto error0;
  2896. #endif
  2897. /* Fail if we're off the end of the block. */
  2898. if (ptr > numrecs) {
  2899. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  2900. *stat = 0;
  2901. return 0;
  2902. }
  2903. XFS_BTREE_STATS_INC(cur, delrec);
  2904. XFS_BTREE_STATS_ADD(cur, moves, numrecs - ptr);
  2905. /* Excise the entries being deleted. */
  2906. if (level > 0) {
  2907. /* It's a nonleaf. operate on keys and ptrs */
  2908. union xfs_btree_key *lkp;
  2909. union xfs_btree_ptr *lpp;
  2910. lkp = xfs_btree_key_addr(cur, ptr + 1, block);
  2911. lpp = xfs_btree_ptr_addr(cur, ptr + 1, block);
  2912. #ifdef DEBUG
  2913. for (i = 0; i < numrecs - ptr; i++) {
  2914. error = xfs_btree_check_ptr(cur, lpp, i, level);
  2915. if (error)
  2916. goto error0;
  2917. }
  2918. #endif
  2919. if (ptr < numrecs) {
  2920. xfs_btree_shift_keys(cur, lkp, -1, numrecs - ptr);
  2921. xfs_btree_shift_ptrs(cur, lpp, -1, numrecs - ptr);
  2922. xfs_btree_log_keys(cur, bp, ptr, numrecs - 1);
  2923. xfs_btree_log_ptrs(cur, bp, ptr, numrecs - 1);
  2924. }
  2925. /*
  2926. * If it's the first record in the block, we'll need to pass a
  2927. * key up to the next level (updkey).
  2928. */
  2929. if (ptr == 1)
  2930. keyp = xfs_btree_key_addr(cur, 1, block);
  2931. } else {
  2932. /* It's a leaf. operate on records */
  2933. if (ptr < numrecs) {
  2934. xfs_btree_shift_recs(cur,
  2935. xfs_btree_rec_addr(cur, ptr + 1, block),
  2936. -1, numrecs - ptr);
  2937. xfs_btree_log_recs(cur, bp, ptr, numrecs - 1);
  2938. }
  2939. /*
  2940. * If it's the first record in the block, we'll need a key
  2941. * structure to pass up to the next level (updkey).
  2942. */
  2943. if (ptr == 1) {
  2944. cur->bc_ops->init_key_from_rec(&key,
  2945. xfs_btree_rec_addr(cur, 1, block));
  2946. keyp = &key;
  2947. }
  2948. }
  2949. /*
  2950. * Decrement and log the number of entries in the block.
  2951. */
  2952. xfs_btree_set_numrecs(block, --numrecs);
  2953. xfs_btree_log_block(cur, bp, XFS_BB_NUMRECS);
  2954. /*
  2955. * If we are tracking the last record in the tree and
  2956. * we are at the far right edge of the tree, update it.
  2957. */
  2958. if (xfs_btree_is_lastrec(cur, block, level)) {
  2959. cur->bc_ops->update_lastrec(cur, block, NULL,
  2960. ptr, LASTREC_DELREC);
  2961. }
  2962. /*
  2963. * We're at the root level. First, shrink the root block in-memory.
  2964. * Try to get rid of the next level down. If we can't then there's
  2965. * nothing left to do.
  2966. */
  2967. if (level == cur->bc_nlevels - 1) {
  2968. if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
  2969. xfs_iroot_realloc(cur->bc_private.b.ip, -1,
  2970. cur->bc_private.b.whichfork);
  2971. error = xfs_btree_kill_iroot(cur);
  2972. if (error)
  2973. goto error0;
  2974. error = xfs_btree_dec_cursor(cur, level, stat);
  2975. if (error)
  2976. goto error0;
  2977. *stat = 1;
  2978. return 0;
  2979. }
  2980. /*
  2981. * If this is the root level, and there's only one entry left,
  2982. * and it's NOT the leaf level, then we can get rid of this
  2983. * level.
  2984. */
  2985. if (numrecs == 1 && level > 0) {
  2986. union xfs_btree_ptr *pp;
  2987. /*
  2988. * pp is still set to the first pointer in the block.
  2989. * Make it the new root of the btree.
  2990. */
  2991. pp = xfs_btree_ptr_addr(cur, 1, block);
  2992. error = xfs_btree_kill_root(cur, bp, level, pp);
  2993. if (error)
  2994. goto error0;
  2995. } else if (level > 0) {
  2996. error = xfs_btree_dec_cursor(cur, level, stat);
  2997. if (error)
  2998. goto error0;
  2999. }
  3000. *stat = 1;
  3001. return 0;
  3002. }
  3003. /*
  3004. * If we deleted the leftmost entry in the block, update the
  3005. * key values above us in the tree.
  3006. */
  3007. if (ptr == 1) {
  3008. error = xfs_btree_updkey(cur, keyp, level + 1);
  3009. if (error)
  3010. goto error0;
  3011. }
  3012. /*
  3013. * If the number of records remaining in the block is at least
  3014. * the minimum, we're done.
  3015. */
  3016. if (numrecs >= cur->bc_ops->get_minrecs(cur, level)) {
  3017. error = xfs_btree_dec_cursor(cur, level, stat);
  3018. if (error)
  3019. goto error0;
  3020. return 0;
  3021. }
  3022. /*
  3023. * Otherwise, we have to move some records around to keep the
  3024. * tree balanced. Look at the left and right sibling blocks to
  3025. * see if we can re-balance by moving only one record.
  3026. */
  3027. xfs_btree_get_sibling(cur, block, &rptr, XFS_BB_RIGHTSIB);
  3028. xfs_btree_get_sibling(cur, block, &lptr, XFS_BB_LEFTSIB);
  3029. if (cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) {
  3030. /*
  3031. * One child of root, need to get a chance to copy its contents
  3032. * into the root and delete it. Can't go up to next level,
  3033. * there's nothing to delete there.
  3034. */
  3035. if (xfs_btree_ptr_is_null(cur, &rptr) &&
  3036. xfs_btree_ptr_is_null(cur, &lptr) &&
  3037. level == cur->bc_nlevels - 2) {
  3038. error = xfs_btree_kill_iroot(cur);
  3039. if (!error)
  3040. error = xfs_btree_dec_cursor(cur, level, stat);
  3041. if (error)
  3042. goto error0;
  3043. return 0;
  3044. }
  3045. }
  3046. ASSERT(!xfs_btree_ptr_is_null(cur, &rptr) ||
  3047. !xfs_btree_ptr_is_null(cur, &lptr));
  3048. /*
  3049. * Duplicate the cursor so our btree manipulations here won't
  3050. * disrupt the next level up.
  3051. */
  3052. error = xfs_btree_dup_cursor(cur, &tcur);
  3053. if (error)
  3054. goto error0;
  3055. /*
  3056. * If there's a right sibling, see if it's ok to shift an entry
  3057. * out of it.
  3058. */
  3059. if (!xfs_btree_ptr_is_null(cur, &rptr)) {
  3060. /*
  3061. * Move the temp cursor to the last entry in the next block.
  3062. * Actually any entry but the first would suffice.
  3063. */
  3064. i = xfs_btree_lastrec(tcur, level);
  3065. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  3066. error = xfs_btree_increment(tcur, level, &i);
  3067. if (error)
  3068. goto error0;
  3069. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  3070. i = xfs_btree_lastrec(tcur, level);
  3071. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  3072. /* Grab a pointer to the block. */
  3073. right = xfs_btree_get_block(tcur, level, &rbp);
  3074. #ifdef DEBUG
  3075. error = xfs_btree_check_block(tcur, right, level, rbp);
  3076. if (error)
  3077. goto error0;
  3078. #endif
  3079. /* Grab the current block number, for future use. */
  3080. xfs_btree_get_sibling(tcur, right, &cptr, XFS_BB_LEFTSIB);
  3081. /*
  3082. * If right block is full enough so that removing one entry
  3083. * won't make it too empty, and left-shifting an entry out
  3084. * of right to us works, we're done.
  3085. */
  3086. if (xfs_btree_get_numrecs(right) - 1 >=
  3087. cur->bc_ops->get_minrecs(tcur, level)) {
  3088. error = xfs_btree_lshift(tcur, level, &i);
  3089. if (error)
  3090. goto error0;
  3091. if (i) {
  3092. ASSERT(xfs_btree_get_numrecs(block) >=
  3093. cur->bc_ops->get_minrecs(tcur, level));
  3094. xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
  3095. tcur = NULL;
  3096. error = xfs_btree_dec_cursor(cur, level, stat);
  3097. if (error)
  3098. goto error0;
  3099. return 0;
  3100. }
  3101. }
  3102. /*
  3103. * Otherwise, grab the number of records in right for
  3104. * future reference, and fix up the temp cursor to point
  3105. * to our block again (last record).
  3106. */
  3107. rrecs = xfs_btree_get_numrecs(right);
  3108. if (!xfs_btree_ptr_is_null(cur, &lptr)) {
  3109. i = xfs_btree_firstrec(tcur, level);
  3110. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  3111. error = xfs_btree_decrement(tcur, level, &i);
  3112. if (error)
  3113. goto error0;
  3114. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  3115. }
  3116. }
  3117. /*
  3118. * If there's a left sibling, see if it's ok to shift an entry
  3119. * out of it.
  3120. */
  3121. if (!xfs_btree_ptr_is_null(cur, &lptr)) {
  3122. /*
  3123. * Move the temp cursor to the first entry in the
  3124. * previous block.
  3125. */
  3126. i = xfs_btree_firstrec(tcur, level);
  3127. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  3128. error = xfs_btree_decrement(tcur, level, &i);
  3129. if (error)
  3130. goto error0;
  3131. i = xfs_btree_firstrec(tcur, level);
  3132. XFS_WANT_CORRUPTED_GOTO(i == 1, error0);
  3133. /* Grab a pointer to the block. */
  3134. left = xfs_btree_get_block(tcur, level, &lbp);
  3135. #ifdef DEBUG
  3136. error = xfs_btree_check_block(cur, left, level, lbp);
  3137. if (error)
  3138. goto error0;
  3139. #endif
  3140. /* Grab the current block number, for future use. */
  3141. xfs_btree_get_sibling(tcur, left, &cptr, XFS_BB_RIGHTSIB);
  3142. /*
  3143. * If left block is full enough so that removing one entry
  3144. * won't make it too empty, and right-shifting an entry out
  3145. * of left to us works, we're done.
  3146. */
  3147. if (xfs_btree_get_numrecs(left) - 1 >=
  3148. cur->bc_ops->get_minrecs(tcur, level)) {
  3149. error = xfs_btree_rshift(tcur, level, &i);
  3150. if (error)
  3151. goto error0;
  3152. if (i) {
  3153. ASSERT(xfs_btree_get_numrecs(block) >=
  3154. cur->bc_ops->get_minrecs(tcur, level));
  3155. xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
  3156. tcur = NULL;
  3157. if (level == 0)
  3158. cur->bc_ptrs[0]++;
  3159. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  3160. *stat = 1;
  3161. return 0;
  3162. }
  3163. }
  3164. /*
  3165. * Otherwise, grab the number of records in right for
  3166. * future reference.
  3167. */
  3168. lrecs = xfs_btree_get_numrecs(left);
  3169. }
  3170. /* Delete the temp cursor, we're done with it. */
  3171. xfs_btree_del_cursor(tcur, XFS_BTREE_NOERROR);
  3172. tcur = NULL;
  3173. /* If here, we need to do a join to keep the tree balanced. */
  3174. ASSERT(!xfs_btree_ptr_is_null(cur, &cptr));
  3175. if (!xfs_btree_ptr_is_null(cur, &lptr) &&
  3176. lrecs + xfs_btree_get_numrecs(block) <=
  3177. cur->bc_ops->get_maxrecs(cur, level)) {
  3178. /*
  3179. * Set "right" to be the starting block,
  3180. * "left" to be the left neighbor.
  3181. */
  3182. rptr = cptr;
  3183. right = block;
  3184. rbp = bp;
  3185. error = xfs_btree_read_buf_block(cur, &lptr, level,
  3186. 0, &left, &lbp);
  3187. if (error)
  3188. goto error0;
  3189. /*
  3190. * If that won't work, see if we can join with the right neighbor block.
  3191. */
  3192. } else if (!xfs_btree_ptr_is_null(cur, &rptr) &&
  3193. rrecs + xfs_btree_get_numrecs(block) <=
  3194. cur->bc_ops->get_maxrecs(cur, level)) {
  3195. /*
  3196. * Set "left" to be the starting block,
  3197. * "right" to be the right neighbor.
  3198. */
  3199. lptr = cptr;
  3200. left = block;
  3201. lbp = bp;
  3202. error = xfs_btree_read_buf_block(cur, &rptr, level,
  3203. 0, &right, &rbp);
  3204. if (error)
  3205. goto error0;
  3206. /*
  3207. * Otherwise, we can't fix the imbalance.
  3208. * Just return. This is probably a logic error, but it's not fatal.
  3209. */
  3210. } else {
  3211. error = xfs_btree_dec_cursor(cur, level, stat);
  3212. if (error)
  3213. goto error0;
  3214. return 0;
  3215. }
  3216. rrecs = xfs_btree_get_numrecs(right);
  3217. lrecs = xfs_btree_get_numrecs(left);
  3218. /*
  3219. * We're now going to join "left" and "right" by moving all the stuff
  3220. * in "right" to "left" and deleting "right".
  3221. */
  3222. XFS_BTREE_STATS_ADD(cur, moves, rrecs);
  3223. if (level > 0) {
  3224. /* It's a non-leaf. Move keys and pointers. */
  3225. union xfs_btree_key *lkp; /* left btree key */
  3226. union xfs_btree_ptr *lpp; /* left address pointer */
  3227. union xfs_btree_key *rkp; /* right btree key */
  3228. union xfs_btree_ptr *rpp; /* right address pointer */
  3229. lkp = xfs_btree_key_addr(cur, lrecs + 1, left);
  3230. lpp = xfs_btree_ptr_addr(cur, lrecs + 1, left);
  3231. rkp = xfs_btree_key_addr(cur, 1, right);
  3232. rpp = xfs_btree_ptr_addr(cur, 1, right);
  3233. #ifdef DEBUG
  3234. for (i = 1; i < rrecs; i++) {
  3235. error = xfs_btree_check_ptr(cur, rpp, i, level);
  3236. if (error)
  3237. goto error0;
  3238. }
  3239. #endif
  3240. xfs_btree_copy_keys(cur, lkp, rkp, rrecs);
  3241. xfs_btree_copy_ptrs(cur, lpp, rpp, rrecs);
  3242. xfs_btree_log_keys(cur, lbp, lrecs + 1, lrecs + rrecs);
  3243. xfs_btree_log_ptrs(cur, lbp, lrecs + 1, lrecs + rrecs);
  3244. } else {
  3245. /* It's a leaf. Move records. */
  3246. union xfs_btree_rec *lrp; /* left record pointer */
  3247. union xfs_btree_rec *rrp; /* right record pointer */
  3248. lrp = xfs_btree_rec_addr(cur, lrecs + 1, left);
  3249. rrp = xfs_btree_rec_addr(cur, 1, right);
  3250. xfs_btree_copy_recs(cur, lrp, rrp, rrecs);
  3251. xfs_btree_log_recs(cur, lbp, lrecs + 1, lrecs + rrecs);
  3252. }
  3253. XFS_BTREE_STATS_INC(cur, join);
  3254. /*
  3255. * Fix up the number of records and right block pointer in the
  3256. * surviving block, and log it.
  3257. */
  3258. xfs_btree_set_numrecs(left, lrecs + rrecs);
  3259. xfs_btree_get_sibling(cur, right, &cptr, XFS_BB_RIGHTSIB),
  3260. xfs_btree_set_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
  3261. xfs_btree_log_block(cur, lbp, XFS_BB_NUMRECS | XFS_BB_RIGHTSIB);
  3262. /* If there is a right sibling, point it to the remaining block. */
  3263. xfs_btree_get_sibling(cur, left, &cptr, XFS_BB_RIGHTSIB);
  3264. if (!xfs_btree_ptr_is_null(cur, &cptr)) {
  3265. error = xfs_btree_read_buf_block(cur, &cptr, level,
  3266. 0, &rrblock, &rrbp);
  3267. if (error)
  3268. goto error0;
  3269. xfs_btree_set_sibling(cur, rrblock, &lptr, XFS_BB_LEFTSIB);
  3270. xfs_btree_log_block(cur, rrbp, XFS_BB_LEFTSIB);
  3271. }
  3272. /* Free the deleted block. */
  3273. error = cur->bc_ops->free_block(cur, rbp);
  3274. if (error)
  3275. goto error0;
  3276. XFS_BTREE_STATS_INC(cur, free);
  3277. /*
  3278. * If we joined with the left neighbor, set the buffer in the
  3279. * cursor to the left block, and fix up the index.
  3280. */
  3281. if (bp != lbp) {
  3282. cur->bc_bufs[level] = lbp;
  3283. cur->bc_ptrs[level] += lrecs;
  3284. cur->bc_ra[level] = 0;
  3285. }
  3286. /*
  3287. * If we joined with the right neighbor and there's a level above
  3288. * us, increment the cursor at that level.
  3289. */
  3290. else if ((cur->bc_flags & XFS_BTREE_ROOT_IN_INODE) ||
  3291. (level + 1 < cur->bc_nlevels)) {
  3292. error = xfs_btree_increment(cur, level + 1, &i);
  3293. if (error)
  3294. goto error0;
  3295. }
  3296. /*
  3297. * Readjust the ptr at this level if it's not a leaf, since it's
  3298. * still pointing at the deletion point, which makes the cursor
  3299. * inconsistent. If this makes the ptr 0, the caller fixes it up.
  3300. * We can't use decrement because it would change the next level up.
  3301. */
  3302. if (level > 0)
  3303. cur->bc_ptrs[level]--;
  3304. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  3305. /* Return value means the next level up has something to do. */
  3306. *stat = 2;
  3307. return 0;
  3308. error0:
  3309. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  3310. if (tcur)
  3311. xfs_btree_del_cursor(tcur, XFS_BTREE_ERROR);
  3312. return error;
  3313. }
  3314. /*
  3315. * Delete the record pointed to by cur.
  3316. * The cursor refers to the place where the record was (could be inserted)
  3317. * when the operation returns.
  3318. */
  3319. int /* error */
  3320. xfs_btree_delete(
  3321. struct xfs_btree_cur *cur,
  3322. int *stat) /* success/failure */
  3323. {
  3324. int error; /* error return value */
  3325. int level;
  3326. int i;
  3327. XFS_BTREE_TRACE_CURSOR(cur, XBT_ENTRY);
  3328. /*
  3329. * Go up the tree, starting at leaf level.
  3330. *
  3331. * If 2 is returned then a join was done; go to the next level.
  3332. * Otherwise we are done.
  3333. */
  3334. for (level = 0, i = 2; i == 2; level++) {
  3335. error = xfs_btree_delrec(cur, level, &i);
  3336. if (error)
  3337. goto error0;
  3338. }
  3339. if (i == 0) {
  3340. for (level = 1; level < cur->bc_nlevels; level++) {
  3341. if (cur->bc_ptrs[level] == 0) {
  3342. error = xfs_btree_decrement(cur, level, &i);
  3343. if (error)
  3344. goto error0;
  3345. break;
  3346. }
  3347. }
  3348. }
  3349. XFS_BTREE_TRACE_CURSOR(cur, XBT_EXIT);
  3350. *stat = i;
  3351. return 0;
  3352. error0:
  3353. XFS_BTREE_TRACE_CURSOR(cur, XBT_ERROR);
  3354. return error;
  3355. }
  3356. /*
  3357. * Get the data from the pointed-to record.
  3358. */
  3359. int /* error */
  3360. xfs_btree_get_rec(
  3361. struct xfs_btree_cur *cur, /* btree cursor */
  3362. union xfs_btree_rec **recp, /* output: btree record */
  3363. int *stat) /* output: success/failure */
  3364. {
  3365. struct xfs_btree_block *block; /* btree block */
  3366. struct xfs_buf *bp; /* buffer pointer */
  3367. int ptr; /* record number */
  3368. #ifdef DEBUG
  3369. int error; /* error return value */
  3370. #endif
  3371. ptr = cur->bc_ptrs[0];
  3372. block = xfs_btree_get_block(cur, 0, &bp);
  3373. #ifdef DEBUG
  3374. error = xfs_btree_check_block(cur, block, 0, bp);
  3375. if (error)
  3376. return error;
  3377. #endif
  3378. /*
  3379. * Off the right end or left end, return failure.
  3380. */
  3381. if (ptr > xfs_btree_get_numrecs(block) || ptr <= 0) {
  3382. *stat = 0;
  3383. return 0;
  3384. }
  3385. /*
  3386. * Point to the record and extract its data.
  3387. */
  3388. *recp = xfs_btree_rec_addr(cur, ptr, block);
  3389. *stat = 1;
  3390. return 0;
  3391. }