xfs_btree.c 106 KB

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