xfs_btree.c 81 KB

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