xfs_btree.c 95 KB

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