xfs_btree.c 94 KB

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