xfs_btree.c 94 KB

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