xfs_btree.c 95 KB

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