xfs_btree.c 94 KB

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