xfs_btree.c 94 KB

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