xfs_btree.c 104 KB

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