xfs_btree.c 97 KB

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