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