xfs_btree.c 94 KB

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