xfs_btree.c 94 KB

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