xfs_btree.c 100 KB

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