xfs_da_btree.c 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597
  1. /*
  2. * Copyright (c) 2000-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_dir.h"
  28. #include "xfs_dir2.h"
  29. #include "xfs_dmapi.h"
  30. #include "xfs_mount.h"
  31. #include "xfs_da_btree.h"
  32. #include "xfs_bmap_btree.h"
  33. #include "xfs_alloc_btree.h"
  34. #include "xfs_ialloc_btree.h"
  35. #include "xfs_dir_sf.h"
  36. #include "xfs_dir2_sf.h"
  37. #include "xfs_attr_sf.h"
  38. #include "xfs_dinode.h"
  39. #include "xfs_inode.h"
  40. #include "xfs_inode_item.h"
  41. #include "xfs_alloc.h"
  42. #include "xfs_btree.h"
  43. #include "xfs_bmap.h"
  44. #include "xfs_attr.h"
  45. #include "xfs_attr_leaf.h"
  46. #include "xfs_dir_leaf.h"
  47. #include "xfs_dir2_data.h"
  48. #include "xfs_dir2_leaf.h"
  49. #include "xfs_dir2_block.h"
  50. #include "xfs_dir2_node.h"
  51. #include "xfs_error.h"
  52. /*
  53. * xfs_da_btree.c
  54. *
  55. * Routines to implement directories as Btrees of hashed names.
  56. */
  57. /*========================================================================
  58. * Function prototypes for the kernel.
  59. *========================================================================*/
  60. /*
  61. * Routines used for growing the Btree.
  62. */
  63. STATIC int xfs_da_root_split(xfs_da_state_t *state,
  64. xfs_da_state_blk_t *existing_root,
  65. xfs_da_state_blk_t *new_child);
  66. STATIC int xfs_da_node_split(xfs_da_state_t *state,
  67. xfs_da_state_blk_t *existing_blk,
  68. xfs_da_state_blk_t *split_blk,
  69. xfs_da_state_blk_t *blk_to_add,
  70. int treelevel,
  71. int *result);
  72. STATIC void xfs_da_node_rebalance(xfs_da_state_t *state,
  73. xfs_da_state_blk_t *node_blk_1,
  74. xfs_da_state_blk_t *node_blk_2);
  75. STATIC void xfs_da_node_add(xfs_da_state_t *state,
  76. xfs_da_state_blk_t *old_node_blk,
  77. xfs_da_state_blk_t *new_node_blk);
  78. /*
  79. * Routines used for shrinking the Btree.
  80. */
  81. STATIC int xfs_da_root_join(xfs_da_state_t *state,
  82. xfs_da_state_blk_t *root_blk);
  83. STATIC int xfs_da_node_toosmall(xfs_da_state_t *state, int *retval);
  84. STATIC void xfs_da_node_remove(xfs_da_state_t *state,
  85. xfs_da_state_blk_t *drop_blk);
  86. STATIC void xfs_da_node_unbalance(xfs_da_state_t *state,
  87. xfs_da_state_blk_t *src_node_blk,
  88. xfs_da_state_blk_t *dst_node_blk);
  89. /*
  90. * Utility routines.
  91. */
  92. STATIC uint xfs_da_node_lasthash(xfs_dabuf_t *bp, int *count);
  93. STATIC int xfs_da_node_order(xfs_dabuf_t *node1_bp, xfs_dabuf_t *node2_bp);
  94. STATIC xfs_dabuf_t *xfs_da_buf_make(int nbuf, xfs_buf_t **bps, inst_t *ra);
  95. STATIC int xfs_da_blk_unlink(xfs_da_state_t *state,
  96. xfs_da_state_blk_t *drop_blk,
  97. xfs_da_state_blk_t *save_blk);
  98. STATIC void xfs_da_state_kill_altpath(xfs_da_state_t *state);
  99. /*========================================================================
  100. * Routines used for growing the Btree.
  101. *========================================================================*/
  102. /*
  103. * Create the initial contents of an intermediate node.
  104. */
  105. int
  106. xfs_da_node_create(xfs_da_args_t *args, xfs_dablk_t blkno, int level,
  107. xfs_dabuf_t **bpp, int whichfork)
  108. {
  109. xfs_da_intnode_t *node;
  110. xfs_dabuf_t *bp;
  111. int error;
  112. xfs_trans_t *tp;
  113. tp = args->trans;
  114. error = xfs_da_get_buf(tp, args->dp, blkno, -1, &bp, whichfork);
  115. if (error)
  116. return(error);
  117. ASSERT(bp != NULL);
  118. node = bp->data;
  119. node->hdr.info.forw = 0;
  120. node->hdr.info.back = 0;
  121. INT_SET(node->hdr.info.magic, ARCH_CONVERT, XFS_DA_NODE_MAGIC);
  122. node->hdr.info.pad = 0;
  123. node->hdr.count = 0;
  124. INT_SET(node->hdr.level, ARCH_CONVERT, level);
  125. xfs_da_log_buf(tp, bp,
  126. XFS_DA_LOGRANGE(node, &node->hdr, sizeof(node->hdr)));
  127. *bpp = bp;
  128. return(0);
  129. }
  130. /*
  131. * Split a leaf node, rebalance, then possibly split
  132. * intermediate nodes, rebalance, etc.
  133. */
  134. int /* error */
  135. xfs_da_split(xfs_da_state_t *state)
  136. {
  137. xfs_da_state_blk_t *oldblk, *newblk, *addblk;
  138. xfs_da_intnode_t *node;
  139. xfs_dabuf_t *bp;
  140. int max, action, error, i;
  141. /*
  142. * Walk back up the tree splitting/inserting/adjusting as necessary.
  143. * If we need to insert and there isn't room, split the node, then
  144. * decide which fragment to insert the new block from below into.
  145. * Note that we may split the root this way, but we need more fixup.
  146. */
  147. max = state->path.active - 1;
  148. ASSERT((max >= 0) && (max < XFS_DA_NODE_MAXDEPTH));
  149. ASSERT(state->path.blk[max].magic == XFS_ATTR_LEAF_MAGIC ||
  150. state->path.blk[max].magic == XFS_DIRX_LEAF_MAGIC(state->mp));
  151. addblk = &state->path.blk[max]; /* initial dummy value */
  152. for (i = max; (i >= 0) && addblk; state->path.active--, i--) {
  153. oldblk = &state->path.blk[i];
  154. newblk = &state->altpath.blk[i];
  155. /*
  156. * If a leaf node then
  157. * Allocate a new leaf node, then rebalance across them.
  158. * else if an intermediate node then
  159. * We split on the last layer, must we split the node?
  160. */
  161. switch (oldblk->magic) {
  162. case XFS_ATTR_LEAF_MAGIC:
  163. error = xfs_attr_leaf_split(state, oldblk, newblk);
  164. if ((error != 0) && (error != ENOSPC)) {
  165. return(error); /* GROT: attr is inconsistent */
  166. }
  167. if (!error) {
  168. addblk = newblk;
  169. break;
  170. }
  171. /*
  172. * Entry wouldn't fit, split the leaf again.
  173. */
  174. state->extravalid = 1;
  175. if (state->inleaf) {
  176. state->extraafter = 0; /* before newblk */
  177. error = xfs_attr_leaf_split(state, oldblk,
  178. &state->extrablk);
  179. } else {
  180. state->extraafter = 1; /* after newblk */
  181. error = xfs_attr_leaf_split(state, newblk,
  182. &state->extrablk);
  183. }
  184. if (error)
  185. return(error); /* GROT: attr inconsistent */
  186. addblk = newblk;
  187. break;
  188. case XFS_DIR_LEAF_MAGIC:
  189. ASSERT(XFS_DIR_IS_V1(state->mp));
  190. error = xfs_dir_leaf_split(state, oldblk, newblk);
  191. if ((error != 0) && (error != ENOSPC)) {
  192. return(error); /* GROT: dir is inconsistent */
  193. }
  194. if (!error) {
  195. addblk = newblk;
  196. break;
  197. }
  198. /*
  199. * Entry wouldn't fit, split the leaf again.
  200. */
  201. state->extravalid = 1;
  202. if (state->inleaf) {
  203. state->extraafter = 0; /* before newblk */
  204. error = xfs_dir_leaf_split(state, oldblk,
  205. &state->extrablk);
  206. if (error)
  207. return(error); /* GROT: dir incon. */
  208. addblk = newblk;
  209. } else {
  210. state->extraafter = 1; /* after newblk */
  211. error = xfs_dir_leaf_split(state, newblk,
  212. &state->extrablk);
  213. if (error)
  214. return(error); /* GROT: dir incon. */
  215. addblk = newblk;
  216. }
  217. break;
  218. case XFS_DIR2_LEAFN_MAGIC:
  219. ASSERT(XFS_DIR_IS_V2(state->mp));
  220. error = xfs_dir2_leafn_split(state, oldblk, newblk);
  221. if (error)
  222. return error;
  223. addblk = newblk;
  224. break;
  225. case XFS_DA_NODE_MAGIC:
  226. error = xfs_da_node_split(state, oldblk, newblk, addblk,
  227. max - i, &action);
  228. xfs_da_buf_done(addblk->bp);
  229. addblk->bp = NULL;
  230. if (error)
  231. return(error); /* GROT: dir is inconsistent */
  232. /*
  233. * Record the newly split block for the next time thru?
  234. */
  235. if (action)
  236. addblk = newblk;
  237. else
  238. addblk = NULL;
  239. break;
  240. }
  241. /*
  242. * Update the btree to show the new hashval for this child.
  243. */
  244. xfs_da_fixhashpath(state, &state->path);
  245. /*
  246. * If we won't need this block again, it's getting dropped
  247. * from the active path by the loop control, so we need
  248. * to mark it done now.
  249. */
  250. if (i > 0 || !addblk)
  251. xfs_da_buf_done(oldblk->bp);
  252. }
  253. if (!addblk)
  254. return(0);
  255. /*
  256. * Split the root node.
  257. */
  258. ASSERT(state->path.active == 0);
  259. oldblk = &state->path.blk[0];
  260. error = xfs_da_root_split(state, oldblk, addblk);
  261. if (error) {
  262. xfs_da_buf_done(oldblk->bp);
  263. xfs_da_buf_done(addblk->bp);
  264. addblk->bp = NULL;
  265. return(error); /* GROT: dir is inconsistent */
  266. }
  267. /*
  268. * Update pointers to the node which used to be block 0 and
  269. * just got bumped because of the addition of a new root node.
  270. * There might be three blocks involved if a double split occurred,
  271. * and the original block 0 could be at any position in the list.
  272. */
  273. node = oldblk->bp->data;
  274. if (node->hdr.info.forw) {
  275. if (INT_GET(node->hdr.info.forw, ARCH_CONVERT) == addblk->blkno) {
  276. bp = addblk->bp;
  277. } else {
  278. ASSERT(state->extravalid);
  279. bp = state->extrablk.bp;
  280. }
  281. node = bp->data;
  282. INT_SET(node->hdr.info.back, ARCH_CONVERT, oldblk->blkno);
  283. xfs_da_log_buf(state->args->trans, bp,
  284. XFS_DA_LOGRANGE(node, &node->hdr.info,
  285. sizeof(node->hdr.info)));
  286. }
  287. node = oldblk->bp->data;
  288. if (INT_GET(node->hdr.info.back, ARCH_CONVERT)) {
  289. if (INT_GET(node->hdr.info.back, ARCH_CONVERT) == addblk->blkno) {
  290. bp = addblk->bp;
  291. } else {
  292. ASSERT(state->extravalid);
  293. bp = state->extrablk.bp;
  294. }
  295. node = bp->data;
  296. INT_SET(node->hdr.info.forw, ARCH_CONVERT, oldblk->blkno);
  297. xfs_da_log_buf(state->args->trans, bp,
  298. XFS_DA_LOGRANGE(node, &node->hdr.info,
  299. sizeof(node->hdr.info)));
  300. }
  301. xfs_da_buf_done(oldblk->bp);
  302. xfs_da_buf_done(addblk->bp);
  303. addblk->bp = NULL;
  304. return(0);
  305. }
  306. /*
  307. * Split the root. We have to create a new root and point to the two
  308. * parts (the split old root) that we just created. Copy block zero to
  309. * the EOF, extending the inode in process.
  310. */
  311. STATIC int /* error */
  312. xfs_da_root_split(xfs_da_state_t *state, xfs_da_state_blk_t *blk1,
  313. xfs_da_state_blk_t *blk2)
  314. {
  315. xfs_da_intnode_t *node, *oldroot;
  316. xfs_da_args_t *args;
  317. xfs_dablk_t blkno;
  318. xfs_dabuf_t *bp;
  319. int error, size;
  320. xfs_inode_t *dp;
  321. xfs_trans_t *tp;
  322. xfs_mount_t *mp;
  323. xfs_dir2_leaf_t *leaf;
  324. /*
  325. * Copy the existing (incorrect) block from the root node position
  326. * to a free space somewhere.
  327. */
  328. args = state->args;
  329. ASSERT(args != NULL);
  330. error = xfs_da_grow_inode(args, &blkno);
  331. if (error)
  332. return(error);
  333. dp = args->dp;
  334. tp = args->trans;
  335. mp = state->mp;
  336. error = xfs_da_get_buf(tp, dp, blkno, -1, &bp, args->whichfork);
  337. if (error)
  338. return(error);
  339. ASSERT(bp != NULL);
  340. node = bp->data;
  341. oldroot = blk1->bp->data;
  342. if (INT_GET(oldroot->hdr.info.magic, ARCH_CONVERT) == XFS_DA_NODE_MAGIC) {
  343. size = (int)((char *)&oldroot->btree[INT_GET(oldroot->hdr.count, ARCH_CONVERT)] -
  344. (char *)oldroot);
  345. } else {
  346. ASSERT(XFS_DIR_IS_V2(mp));
  347. ASSERT(INT_GET(oldroot->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC);
  348. leaf = (xfs_dir2_leaf_t *)oldroot;
  349. size = (int)((char *)&leaf->ents[INT_GET(leaf->hdr.count, ARCH_CONVERT)] -
  350. (char *)leaf);
  351. }
  352. memcpy(node, oldroot, size);
  353. xfs_da_log_buf(tp, bp, 0, size - 1);
  354. xfs_da_buf_done(blk1->bp);
  355. blk1->bp = bp;
  356. blk1->blkno = blkno;
  357. /*
  358. * Set up the new root node.
  359. */
  360. error = xfs_da_node_create(args,
  361. args->whichfork == XFS_DATA_FORK &&
  362. XFS_DIR_IS_V2(mp) ? mp->m_dirleafblk : 0,
  363. INT_GET(node->hdr.level, ARCH_CONVERT) + 1, &bp, args->whichfork);
  364. if (error)
  365. return(error);
  366. node = bp->data;
  367. INT_SET(node->btree[0].hashval, ARCH_CONVERT, blk1->hashval);
  368. INT_SET(node->btree[0].before, ARCH_CONVERT, blk1->blkno);
  369. INT_SET(node->btree[1].hashval, ARCH_CONVERT, blk2->hashval);
  370. INT_SET(node->btree[1].before, ARCH_CONVERT, blk2->blkno);
  371. INT_SET(node->hdr.count, ARCH_CONVERT, 2);
  372. #ifdef DEBUG
  373. if (INT_GET(oldroot->hdr.info.magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC) {
  374. ASSERT(blk1->blkno >= mp->m_dirleafblk &&
  375. blk1->blkno < mp->m_dirfreeblk);
  376. ASSERT(blk2->blkno >= mp->m_dirleafblk &&
  377. blk2->blkno < mp->m_dirfreeblk);
  378. }
  379. #endif
  380. /* Header is already logged by xfs_da_node_create */
  381. xfs_da_log_buf(tp, bp,
  382. XFS_DA_LOGRANGE(node, node->btree,
  383. sizeof(xfs_da_node_entry_t) * 2));
  384. xfs_da_buf_done(bp);
  385. return(0);
  386. }
  387. /*
  388. * Split the node, rebalance, then add the new entry.
  389. */
  390. STATIC int /* error */
  391. xfs_da_node_split(xfs_da_state_t *state, xfs_da_state_blk_t *oldblk,
  392. xfs_da_state_blk_t *newblk,
  393. xfs_da_state_blk_t *addblk,
  394. int treelevel, int *result)
  395. {
  396. xfs_da_intnode_t *node;
  397. xfs_dablk_t blkno;
  398. int newcount, error;
  399. int useextra;
  400. node = oldblk->bp->data;
  401. ASSERT(INT_GET(node->hdr.info.magic, ARCH_CONVERT) == XFS_DA_NODE_MAGIC);
  402. /*
  403. * With V2 the extra block is data or freespace.
  404. */
  405. useextra = state->extravalid && XFS_DIR_IS_V1(state->mp);
  406. newcount = 1 + useextra;
  407. /*
  408. * Do we have to split the node?
  409. */
  410. if ((INT_GET(node->hdr.count, ARCH_CONVERT) + newcount) > state->node_ents) {
  411. /*
  412. * Allocate a new node, add to the doubly linked chain of
  413. * nodes, then move some of our excess entries into it.
  414. */
  415. error = xfs_da_grow_inode(state->args, &blkno);
  416. if (error)
  417. return(error); /* GROT: dir is inconsistent */
  418. error = xfs_da_node_create(state->args, blkno, treelevel,
  419. &newblk->bp, state->args->whichfork);
  420. if (error)
  421. return(error); /* GROT: dir is inconsistent */
  422. newblk->blkno = blkno;
  423. newblk->magic = XFS_DA_NODE_MAGIC;
  424. xfs_da_node_rebalance(state, oldblk, newblk);
  425. error = xfs_da_blk_link(state, oldblk, newblk);
  426. if (error)
  427. return(error);
  428. *result = 1;
  429. } else {
  430. *result = 0;
  431. }
  432. /*
  433. * Insert the new entry(s) into the correct block
  434. * (updating last hashval in the process).
  435. *
  436. * xfs_da_node_add() inserts BEFORE the given index,
  437. * and as a result of using node_lookup_int() we always
  438. * point to a valid entry (not after one), but a split
  439. * operation always results in a new block whose hashvals
  440. * FOLLOW the current block.
  441. *
  442. * If we had double-split op below us, then add the extra block too.
  443. */
  444. node = oldblk->bp->data;
  445. if (oldblk->index <= INT_GET(node->hdr.count, ARCH_CONVERT)) {
  446. oldblk->index++;
  447. xfs_da_node_add(state, oldblk, addblk);
  448. if (useextra) {
  449. if (state->extraafter)
  450. oldblk->index++;
  451. xfs_da_node_add(state, oldblk, &state->extrablk);
  452. state->extravalid = 0;
  453. }
  454. } else {
  455. newblk->index++;
  456. xfs_da_node_add(state, newblk, addblk);
  457. if (useextra) {
  458. if (state->extraafter)
  459. newblk->index++;
  460. xfs_da_node_add(state, newblk, &state->extrablk);
  461. state->extravalid = 0;
  462. }
  463. }
  464. return(0);
  465. }
  466. /*
  467. * Balance the btree elements between two intermediate nodes,
  468. * usually one full and one empty.
  469. *
  470. * NOTE: if blk2 is empty, then it will get the upper half of blk1.
  471. */
  472. STATIC void
  473. xfs_da_node_rebalance(xfs_da_state_t *state, xfs_da_state_blk_t *blk1,
  474. xfs_da_state_blk_t *blk2)
  475. {
  476. xfs_da_intnode_t *node1, *node2, *tmpnode;
  477. xfs_da_node_entry_t *btree_s, *btree_d;
  478. int count, tmp;
  479. xfs_trans_t *tp;
  480. node1 = blk1->bp->data;
  481. node2 = blk2->bp->data;
  482. /*
  483. * Figure out how many entries need to move, and in which direction.
  484. * Swap the nodes around if that makes it simpler.
  485. */
  486. if ((INT_GET(node1->hdr.count, ARCH_CONVERT) > 0) && (INT_GET(node2->hdr.count, ARCH_CONVERT) > 0) &&
  487. ((INT_GET(node2->btree[ 0 ].hashval, ARCH_CONVERT) < INT_GET(node1->btree[ 0 ].hashval, ARCH_CONVERT)) ||
  488. (INT_GET(node2->btree[ INT_GET(node2->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT) <
  489. INT_GET(node1->btree[ INT_GET(node1->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT)))) {
  490. tmpnode = node1;
  491. node1 = node2;
  492. node2 = tmpnode;
  493. }
  494. ASSERT(INT_GET(node1->hdr.info.magic, ARCH_CONVERT) == XFS_DA_NODE_MAGIC);
  495. ASSERT(INT_GET(node2->hdr.info.magic, ARCH_CONVERT) == XFS_DA_NODE_MAGIC);
  496. count = (INT_GET(node1->hdr.count, ARCH_CONVERT) - INT_GET(node2->hdr.count, ARCH_CONVERT)) / 2;
  497. if (count == 0)
  498. return;
  499. tp = state->args->trans;
  500. /*
  501. * Two cases: high-to-low and low-to-high.
  502. */
  503. if (count > 0) {
  504. /*
  505. * Move elements in node2 up to make a hole.
  506. */
  507. if ((tmp = INT_GET(node2->hdr.count, ARCH_CONVERT)) > 0) {
  508. tmp *= (uint)sizeof(xfs_da_node_entry_t);
  509. btree_s = &node2->btree[0];
  510. btree_d = &node2->btree[count];
  511. memmove(btree_d, btree_s, tmp);
  512. }
  513. /*
  514. * Move the req'd B-tree elements from high in node1 to
  515. * low in node2.
  516. */
  517. INT_MOD(node2->hdr.count, ARCH_CONVERT, count);
  518. tmp = count * (uint)sizeof(xfs_da_node_entry_t);
  519. btree_s = &node1->btree[INT_GET(node1->hdr.count, ARCH_CONVERT) - count];
  520. btree_d = &node2->btree[0];
  521. memcpy(btree_d, btree_s, tmp);
  522. INT_MOD(node1->hdr.count, ARCH_CONVERT, -(count));
  523. } else {
  524. /*
  525. * Move the req'd B-tree elements from low in node2 to
  526. * high in node1.
  527. */
  528. count = -count;
  529. tmp = count * (uint)sizeof(xfs_da_node_entry_t);
  530. btree_s = &node2->btree[0];
  531. btree_d = &node1->btree[INT_GET(node1->hdr.count, ARCH_CONVERT)];
  532. memcpy(btree_d, btree_s, tmp);
  533. INT_MOD(node1->hdr.count, ARCH_CONVERT, count);
  534. xfs_da_log_buf(tp, blk1->bp,
  535. XFS_DA_LOGRANGE(node1, btree_d, tmp));
  536. /*
  537. * Move elements in node2 down to fill the hole.
  538. */
  539. tmp = INT_GET(node2->hdr.count, ARCH_CONVERT) - count;
  540. tmp *= (uint)sizeof(xfs_da_node_entry_t);
  541. btree_s = &node2->btree[count];
  542. btree_d = &node2->btree[0];
  543. memmove(btree_d, btree_s, tmp);
  544. INT_MOD(node2->hdr.count, ARCH_CONVERT, -(count));
  545. }
  546. /*
  547. * Log header of node 1 and all current bits of node 2.
  548. */
  549. xfs_da_log_buf(tp, blk1->bp,
  550. XFS_DA_LOGRANGE(node1, &node1->hdr, sizeof(node1->hdr)));
  551. xfs_da_log_buf(tp, blk2->bp,
  552. XFS_DA_LOGRANGE(node2, &node2->hdr,
  553. sizeof(node2->hdr) +
  554. sizeof(node2->btree[0]) * INT_GET(node2->hdr.count, ARCH_CONVERT)));
  555. /*
  556. * Record the last hashval from each block for upward propagation.
  557. * (note: don't use the swapped node pointers)
  558. */
  559. node1 = blk1->bp->data;
  560. node2 = blk2->bp->data;
  561. blk1->hashval = INT_GET(node1->btree[ INT_GET(node1->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT);
  562. blk2->hashval = INT_GET(node2->btree[ INT_GET(node2->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT);
  563. /*
  564. * Adjust the expected index for insertion.
  565. */
  566. if (blk1->index >= INT_GET(node1->hdr.count, ARCH_CONVERT)) {
  567. blk2->index = blk1->index - INT_GET(node1->hdr.count, ARCH_CONVERT);
  568. blk1->index = INT_GET(node1->hdr.count, ARCH_CONVERT) + 1; /* make it invalid */
  569. }
  570. }
  571. /*
  572. * Add a new entry to an intermediate node.
  573. */
  574. STATIC void
  575. xfs_da_node_add(xfs_da_state_t *state, xfs_da_state_blk_t *oldblk,
  576. xfs_da_state_blk_t *newblk)
  577. {
  578. xfs_da_intnode_t *node;
  579. xfs_da_node_entry_t *btree;
  580. int tmp;
  581. xfs_mount_t *mp;
  582. node = oldblk->bp->data;
  583. mp = state->mp;
  584. ASSERT(INT_GET(node->hdr.info.magic, ARCH_CONVERT) == XFS_DA_NODE_MAGIC);
  585. ASSERT((oldblk->index >= 0) && (oldblk->index <= INT_GET(node->hdr.count, ARCH_CONVERT)));
  586. ASSERT(newblk->blkno != 0);
  587. if (state->args->whichfork == XFS_DATA_FORK && XFS_DIR_IS_V2(mp))
  588. ASSERT(newblk->blkno >= mp->m_dirleafblk &&
  589. newblk->blkno < mp->m_dirfreeblk);
  590. /*
  591. * We may need to make some room before we insert the new node.
  592. */
  593. tmp = 0;
  594. btree = &node->btree[ oldblk->index ];
  595. if (oldblk->index < INT_GET(node->hdr.count, ARCH_CONVERT)) {
  596. tmp = (INT_GET(node->hdr.count, ARCH_CONVERT) - oldblk->index) * (uint)sizeof(*btree);
  597. memmove(btree + 1, btree, tmp);
  598. }
  599. INT_SET(btree->hashval, ARCH_CONVERT, newblk->hashval);
  600. INT_SET(btree->before, ARCH_CONVERT, newblk->blkno);
  601. xfs_da_log_buf(state->args->trans, oldblk->bp,
  602. XFS_DA_LOGRANGE(node, btree, tmp + sizeof(*btree)));
  603. INT_MOD(node->hdr.count, ARCH_CONVERT, +1);
  604. xfs_da_log_buf(state->args->trans, oldblk->bp,
  605. XFS_DA_LOGRANGE(node, &node->hdr, sizeof(node->hdr)));
  606. /*
  607. * Copy the last hash value from the oldblk to propagate upwards.
  608. */
  609. oldblk->hashval = INT_GET(node->btree[ INT_GET(node->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT);
  610. }
  611. /*========================================================================
  612. * Routines used for shrinking the Btree.
  613. *========================================================================*/
  614. /*
  615. * Deallocate an empty leaf node, remove it from its parent,
  616. * possibly deallocating that block, etc...
  617. */
  618. int
  619. xfs_da_join(xfs_da_state_t *state)
  620. {
  621. xfs_da_state_blk_t *drop_blk, *save_blk;
  622. int action, error;
  623. action = 0;
  624. drop_blk = &state->path.blk[ state->path.active-1 ];
  625. save_blk = &state->altpath.blk[ state->path.active-1 ];
  626. ASSERT(state->path.blk[0].magic == XFS_DA_NODE_MAGIC);
  627. ASSERT(drop_blk->magic == XFS_ATTR_LEAF_MAGIC ||
  628. drop_blk->magic == XFS_DIRX_LEAF_MAGIC(state->mp));
  629. /*
  630. * Walk back up the tree joining/deallocating as necessary.
  631. * When we stop dropping blocks, break out.
  632. */
  633. for ( ; state->path.active >= 2; drop_blk--, save_blk--,
  634. state->path.active--) {
  635. /*
  636. * See if we can combine the block with a neighbor.
  637. * (action == 0) => no options, just leave
  638. * (action == 1) => coalesce, then unlink
  639. * (action == 2) => block empty, unlink it
  640. */
  641. switch (drop_blk->magic) {
  642. case XFS_ATTR_LEAF_MAGIC:
  643. error = xfs_attr_leaf_toosmall(state, &action);
  644. if (error)
  645. return(error);
  646. if (action == 0)
  647. return(0);
  648. xfs_attr_leaf_unbalance(state, drop_blk, save_blk);
  649. break;
  650. case XFS_DIR_LEAF_MAGIC:
  651. ASSERT(XFS_DIR_IS_V1(state->mp));
  652. error = xfs_dir_leaf_toosmall(state, &action);
  653. if (error)
  654. return(error);
  655. if (action == 0)
  656. return(0);
  657. xfs_dir_leaf_unbalance(state, drop_blk, save_blk);
  658. break;
  659. case XFS_DIR2_LEAFN_MAGIC:
  660. ASSERT(XFS_DIR_IS_V2(state->mp));
  661. error = xfs_dir2_leafn_toosmall(state, &action);
  662. if (error)
  663. return error;
  664. if (action == 0)
  665. return 0;
  666. xfs_dir2_leafn_unbalance(state, drop_blk, save_blk);
  667. break;
  668. case XFS_DA_NODE_MAGIC:
  669. /*
  670. * Remove the offending node, fixup hashvals,
  671. * check for a toosmall neighbor.
  672. */
  673. xfs_da_node_remove(state, drop_blk);
  674. xfs_da_fixhashpath(state, &state->path);
  675. error = xfs_da_node_toosmall(state, &action);
  676. if (error)
  677. return(error);
  678. if (action == 0)
  679. return 0;
  680. xfs_da_node_unbalance(state, drop_blk, save_blk);
  681. break;
  682. }
  683. xfs_da_fixhashpath(state, &state->altpath);
  684. error = xfs_da_blk_unlink(state, drop_blk, save_blk);
  685. xfs_da_state_kill_altpath(state);
  686. if (error)
  687. return(error);
  688. error = xfs_da_shrink_inode(state->args, drop_blk->blkno,
  689. drop_blk->bp);
  690. drop_blk->bp = NULL;
  691. if (error)
  692. return(error);
  693. }
  694. /*
  695. * We joined all the way to the top. If it turns out that
  696. * we only have one entry in the root, make the child block
  697. * the new root.
  698. */
  699. xfs_da_node_remove(state, drop_blk);
  700. xfs_da_fixhashpath(state, &state->path);
  701. error = xfs_da_root_join(state, &state->path.blk[0]);
  702. return(error);
  703. }
  704. /*
  705. * We have only one entry in the root. Copy the only remaining child of
  706. * the old root to block 0 as the new root node.
  707. */
  708. STATIC int
  709. xfs_da_root_join(xfs_da_state_t *state, xfs_da_state_blk_t *root_blk)
  710. {
  711. xfs_da_intnode_t *oldroot;
  712. /* REFERENCED */
  713. xfs_da_blkinfo_t *blkinfo;
  714. xfs_da_args_t *args;
  715. xfs_dablk_t child;
  716. xfs_dabuf_t *bp;
  717. int error;
  718. args = state->args;
  719. ASSERT(args != NULL);
  720. ASSERT(root_blk->magic == XFS_DA_NODE_MAGIC);
  721. oldroot = root_blk->bp->data;
  722. ASSERT(INT_GET(oldroot->hdr.info.magic, ARCH_CONVERT) == XFS_DA_NODE_MAGIC);
  723. ASSERT(!oldroot->hdr.info.forw);
  724. ASSERT(!oldroot->hdr.info.back);
  725. /*
  726. * If the root has more than one child, then don't do anything.
  727. */
  728. if (INT_GET(oldroot->hdr.count, ARCH_CONVERT) > 1)
  729. return(0);
  730. /*
  731. * Read in the (only) child block, then copy those bytes into
  732. * the root block's buffer and free the original child block.
  733. */
  734. child = INT_GET(oldroot->btree[ 0 ].before, ARCH_CONVERT);
  735. ASSERT(child != 0);
  736. error = xfs_da_read_buf(args->trans, args->dp, child, -1, &bp,
  737. args->whichfork);
  738. if (error)
  739. return(error);
  740. ASSERT(bp != NULL);
  741. blkinfo = bp->data;
  742. if (INT_GET(oldroot->hdr.level, ARCH_CONVERT) == 1) {
  743. ASSERT(INT_GET(blkinfo->magic, ARCH_CONVERT) == XFS_DIRX_LEAF_MAGIC(state->mp) ||
  744. INT_GET(blkinfo->magic, ARCH_CONVERT) == XFS_ATTR_LEAF_MAGIC);
  745. } else {
  746. ASSERT(INT_GET(blkinfo->magic, ARCH_CONVERT) == XFS_DA_NODE_MAGIC);
  747. }
  748. ASSERT(!blkinfo->forw);
  749. ASSERT(!blkinfo->back);
  750. memcpy(root_blk->bp->data, bp->data, state->blocksize);
  751. xfs_da_log_buf(args->trans, root_blk->bp, 0, state->blocksize - 1);
  752. error = xfs_da_shrink_inode(args, child, bp);
  753. return(error);
  754. }
  755. /*
  756. * Check a node block and its neighbors to see if the block should be
  757. * collapsed into one or the other neighbor. Always keep the block
  758. * with the smaller block number.
  759. * If the current block is over 50% full, don't try to join it, return 0.
  760. * If the block is empty, fill in the state structure and return 2.
  761. * If it can be collapsed, fill in the state structure and return 1.
  762. * If nothing can be done, return 0.
  763. */
  764. STATIC int
  765. xfs_da_node_toosmall(xfs_da_state_t *state, int *action)
  766. {
  767. xfs_da_intnode_t *node;
  768. xfs_da_state_blk_t *blk;
  769. xfs_da_blkinfo_t *info;
  770. int count, forward, error, retval, i;
  771. xfs_dablk_t blkno;
  772. xfs_dabuf_t *bp;
  773. /*
  774. * Check for the degenerate case of the block being over 50% full.
  775. * If so, it's not worth even looking to see if we might be able
  776. * to coalesce with a sibling.
  777. */
  778. blk = &state->path.blk[ state->path.active-1 ];
  779. info = blk->bp->data;
  780. ASSERT(INT_GET(info->magic, ARCH_CONVERT) == XFS_DA_NODE_MAGIC);
  781. node = (xfs_da_intnode_t *)info;
  782. count = INT_GET(node->hdr.count, ARCH_CONVERT);
  783. if (count > (state->node_ents >> 1)) {
  784. *action = 0; /* blk over 50%, don't try to join */
  785. return(0); /* blk over 50%, don't try to join */
  786. }
  787. /*
  788. * Check for the degenerate case of the block being empty.
  789. * If the block is empty, we'll simply delete it, no need to
  790. * coalesce it with a sibling block. We choose (aribtrarily)
  791. * to merge with the forward block unless it is NULL.
  792. */
  793. if (count == 0) {
  794. /*
  795. * Make altpath point to the block we want to keep and
  796. * path point to the block we want to drop (this one).
  797. */
  798. forward = info->forw;
  799. memcpy(&state->altpath, &state->path, sizeof(state->path));
  800. error = xfs_da_path_shift(state, &state->altpath, forward,
  801. 0, &retval);
  802. if (error)
  803. return(error);
  804. if (retval) {
  805. *action = 0;
  806. } else {
  807. *action = 2;
  808. }
  809. return(0);
  810. }
  811. /*
  812. * Examine each sibling block to see if we can coalesce with
  813. * at least 25% free space to spare. We need to figure out
  814. * whether to merge with the forward or the backward block.
  815. * We prefer coalescing with the lower numbered sibling so as
  816. * to shrink a directory over time.
  817. */
  818. /* start with smaller blk num */
  819. forward = (INT_GET(info->forw, ARCH_CONVERT)
  820. < INT_GET(info->back, ARCH_CONVERT));
  821. for (i = 0; i < 2; forward = !forward, i++) {
  822. if (forward)
  823. blkno = INT_GET(info->forw, ARCH_CONVERT);
  824. else
  825. blkno = INT_GET(info->back, ARCH_CONVERT);
  826. if (blkno == 0)
  827. continue;
  828. error = xfs_da_read_buf(state->args->trans, state->args->dp,
  829. blkno, -1, &bp, state->args->whichfork);
  830. if (error)
  831. return(error);
  832. ASSERT(bp != NULL);
  833. node = (xfs_da_intnode_t *)info;
  834. count = state->node_ents;
  835. count -= state->node_ents >> 2;
  836. count -= INT_GET(node->hdr.count, ARCH_CONVERT);
  837. node = bp->data;
  838. ASSERT(INT_GET(node->hdr.info.magic, ARCH_CONVERT) == XFS_DA_NODE_MAGIC);
  839. count -= INT_GET(node->hdr.count, ARCH_CONVERT);
  840. xfs_da_brelse(state->args->trans, bp);
  841. if (count >= 0)
  842. break; /* fits with at least 25% to spare */
  843. }
  844. if (i >= 2) {
  845. *action = 0;
  846. return(0);
  847. }
  848. /*
  849. * Make altpath point to the block we want to keep (the lower
  850. * numbered block) and path point to the block we want to drop.
  851. */
  852. memcpy(&state->altpath, &state->path, sizeof(state->path));
  853. if (blkno < blk->blkno) {
  854. error = xfs_da_path_shift(state, &state->altpath, forward,
  855. 0, &retval);
  856. if (error) {
  857. return(error);
  858. }
  859. if (retval) {
  860. *action = 0;
  861. return(0);
  862. }
  863. } else {
  864. error = xfs_da_path_shift(state, &state->path, forward,
  865. 0, &retval);
  866. if (error) {
  867. return(error);
  868. }
  869. if (retval) {
  870. *action = 0;
  871. return(0);
  872. }
  873. }
  874. *action = 1;
  875. return(0);
  876. }
  877. /*
  878. * Walk back up the tree adjusting hash values as necessary,
  879. * when we stop making changes, return.
  880. */
  881. void
  882. xfs_da_fixhashpath(xfs_da_state_t *state, xfs_da_state_path_t *path)
  883. {
  884. xfs_da_state_blk_t *blk;
  885. xfs_da_intnode_t *node;
  886. xfs_da_node_entry_t *btree;
  887. xfs_dahash_t lasthash=0;
  888. int level, count;
  889. level = path->active-1;
  890. blk = &path->blk[ level ];
  891. switch (blk->magic) {
  892. case XFS_ATTR_LEAF_MAGIC:
  893. lasthash = xfs_attr_leaf_lasthash(blk->bp, &count);
  894. if (count == 0)
  895. return;
  896. break;
  897. case XFS_DIR_LEAF_MAGIC:
  898. ASSERT(XFS_DIR_IS_V1(state->mp));
  899. lasthash = xfs_dir_leaf_lasthash(blk->bp, &count);
  900. if (count == 0)
  901. return;
  902. break;
  903. case XFS_DIR2_LEAFN_MAGIC:
  904. ASSERT(XFS_DIR_IS_V2(state->mp));
  905. lasthash = xfs_dir2_leafn_lasthash(blk->bp, &count);
  906. if (count == 0)
  907. return;
  908. break;
  909. case XFS_DA_NODE_MAGIC:
  910. lasthash = xfs_da_node_lasthash(blk->bp, &count);
  911. if (count == 0)
  912. return;
  913. break;
  914. }
  915. for (blk--, level--; level >= 0; blk--, level--) {
  916. node = blk->bp->data;
  917. ASSERT(INT_GET(node->hdr.info.magic, ARCH_CONVERT) == XFS_DA_NODE_MAGIC);
  918. btree = &node->btree[ blk->index ];
  919. if (INT_GET(btree->hashval, ARCH_CONVERT) == lasthash)
  920. break;
  921. blk->hashval = lasthash;
  922. INT_SET(btree->hashval, ARCH_CONVERT, lasthash);
  923. xfs_da_log_buf(state->args->trans, blk->bp,
  924. XFS_DA_LOGRANGE(node, btree, sizeof(*btree)));
  925. lasthash = INT_GET(node->btree[ INT_GET(node->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT);
  926. }
  927. }
  928. /*
  929. * Remove an entry from an intermediate node.
  930. */
  931. STATIC void
  932. xfs_da_node_remove(xfs_da_state_t *state, xfs_da_state_blk_t *drop_blk)
  933. {
  934. xfs_da_intnode_t *node;
  935. xfs_da_node_entry_t *btree;
  936. int tmp;
  937. node = drop_blk->bp->data;
  938. ASSERT(drop_blk->index < INT_GET(node->hdr.count, ARCH_CONVERT));
  939. ASSERT(drop_blk->index >= 0);
  940. /*
  941. * Copy over the offending entry, or just zero it out.
  942. */
  943. btree = &node->btree[drop_blk->index];
  944. if (drop_blk->index < (INT_GET(node->hdr.count, ARCH_CONVERT)-1)) {
  945. tmp = INT_GET(node->hdr.count, ARCH_CONVERT) - drop_blk->index - 1;
  946. tmp *= (uint)sizeof(xfs_da_node_entry_t);
  947. memmove(btree, btree + 1, tmp);
  948. xfs_da_log_buf(state->args->trans, drop_blk->bp,
  949. XFS_DA_LOGRANGE(node, btree, tmp));
  950. btree = &node->btree[ INT_GET(node->hdr.count, ARCH_CONVERT)-1 ];
  951. }
  952. memset((char *)btree, 0, sizeof(xfs_da_node_entry_t));
  953. xfs_da_log_buf(state->args->trans, drop_blk->bp,
  954. XFS_DA_LOGRANGE(node, btree, sizeof(*btree)));
  955. INT_MOD(node->hdr.count, ARCH_CONVERT, -1);
  956. xfs_da_log_buf(state->args->trans, drop_blk->bp,
  957. XFS_DA_LOGRANGE(node, &node->hdr, sizeof(node->hdr)));
  958. /*
  959. * Copy the last hash value from the block to propagate upwards.
  960. */
  961. btree--;
  962. drop_blk->hashval = INT_GET(btree->hashval, ARCH_CONVERT);
  963. }
  964. /*
  965. * Unbalance the btree elements between two intermediate nodes,
  966. * move all Btree elements from one node into another.
  967. */
  968. STATIC void
  969. xfs_da_node_unbalance(xfs_da_state_t *state, xfs_da_state_blk_t *drop_blk,
  970. xfs_da_state_blk_t *save_blk)
  971. {
  972. xfs_da_intnode_t *drop_node, *save_node;
  973. xfs_da_node_entry_t *btree;
  974. int tmp;
  975. xfs_trans_t *tp;
  976. drop_node = drop_blk->bp->data;
  977. save_node = save_blk->bp->data;
  978. ASSERT(INT_GET(drop_node->hdr.info.magic, ARCH_CONVERT) == XFS_DA_NODE_MAGIC);
  979. ASSERT(INT_GET(save_node->hdr.info.magic, ARCH_CONVERT) == XFS_DA_NODE_MAGIC);
  980. tp = state->args->trans;
  981. /*
  982. * If the dying block has lower hashvals, then move all the
  983. * elements in the remaining block up to make a hole.
  984. */
  985. if ((INT_GET(drop_node->btree[ 0 ].hashval, ARCH_CONVERT) < INT_GET(save_node->btree[ 0 ].hashval, ARCH_CONVERT)) ||
  986. (INT_GET(drop_node->btree[ INT_GET(drop_node->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT) <
  987. INT_GET(save_node->btree[ INT_GET(save_node->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT)))
  988. {
  989. btree = &save_node->btree[ INT_GET(drop_node->hdr.count, ARCH_CONVERT) ];
  990. tmp = INT_GET(save_node->hdr.count, ARCH_CONVERT) * (uint)sizeof(xfs_da_node_entry_t);
  991. memmove(btree, &save_node->btree[0], tmp);
  992. btree = &save_node->btree[0];
  993. xfs_da_log_buf(tp, save_blk->bp,
  994. XFS_DA_LOGRANGE(save_node, btree,
  995. (INT_GET(save_node->hdr.count, ARCH_CONVERT) + INT_GET(drop_node->hdr.count, ARCH_CONVERT)) *
  996. sizeof(xfs_da_node_entry_t)));
  997. } else {
  998. btree = &save_node->btree[ INT_GET(save_node->hdr.count, ARCH_CONVERT) ];
  999. xfs_da_log_buf(tp, save_blk->bp,
  1000. XFS_DA_LOGRANGE(save_node, btree,
  1001. INT_GET(drop_node->hdr.count, ARCH_CONVERT) *
  1002. sizeof(xfs_da_node_entry_t)));
  1003. }
  1004. /*
  1005. * Move all the B-tree elements from drop_blk to save_blk.
  1006. */
  1007. tmp = INT_GET(drop_node->hdr.count, ARCH_CONVERT) * (uint)sizeof(xfs_da_node_entry_t);
  1008. memcpy(btree, &drop_node->btree[0], tmp);
  1009. INT_MOD(save_node->hdr.count, ARCH_CONVERT, INT_GET(drop_node->hdr.count, ARCH_CONVERT));
  1010. xfs_da_log_buf(tp, save_blk->bp,
  1011. XFS_DA_LOGRANGE(save_node, &save_node->hdr,
  1012. sizeof(save_node->hdr)));
  1013. /*
  1014. * Save the last hashval in the remaining block for upward propagation.
  1015. */
  1016. save_blk->hashval = INT_GET(save_node->btree[ INT_GET(save_node->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT);
  1017. }
  1018. /*========================================================================
  1019. * Routines used for finding things in the Btree.
  1020. *========================================================================*/
  1021. /*
  1022. * Walk down the Btree looking for a particular filename, filling
  1023. * in the state structure as we go.
  1024. *
  1025. * We will set the state structure to point to each of the elements
  1026. * in each of the nodes where either the hashval is or should be.
  1027. *
  1028. * We support duplicate hashval's so for each entry in the current
  1029. * node that could contain the desired hashval, descend. This is a
  1030. * pruned depth-first tree search.
  1031. */
  1032. int /* error */
  1033. xfs_da_node_lookup_int(xfs_da_state_t *state, int *result)
  1034. {
  1035. xfs_da_state_blk_t *blk;
  1036. xfs_da_blkinfo_t *curr;
  1037. xfs_da_intnode_t *node;
  1038. xfs_da_node_entry_t *btree;
  1039. xfs_dablk_t blkno;
  1040. int probe, span, max, error, retval;
  1041. xfs_dahash_t hashval;
  1042. xfs_da_args_t *args;
  1043. args = state->args;
  1044. /*
  1045. * Descend thru the B-tree searching each level for the right
  1046. * node to use, until the right hashval is found.
  1047. */
  1048. if (args->whichfork == XFS_DATA_FORK && XFS_DIR_IS_V2(state->mp))
  1049. blkno = state->mp->m_dirleafblk;
  1050. else
  1051. blkno = 0;
  1052. for (blk = &state->path.blk[0], state->path.active = 1;
  1053. state->path.active <= XFS_DA_NODE_MAXDEPTH;
  1054. blk++, state->path.active++) {
  1055. /*
  1056. * Read the next node down in the tree.
  1057. */
  1058. blk->blkno = blkno;
  1059. error = xfs_da_read_buf(args->trans, args->dp, blkno,
  1060. -1, &blk->bp, args->whichfork);
  1061. if (error) {
  1062. blk->blkno = 0;
  1063. state->path.active--;
  1064. return(error);
  1065. }
  1066. curr = blk->bp->data;
  1067. ASSERT(INT_GET(curr->magic, ARCH_CONVERT) == XFS_DA_NODE_MAGIC ||
  1068. INT_GET(curr->magic, ARCH_CONVERT) == XFS_DIRX_LEAF_MAGIC(state->mp) ||
  1069. INT_GET(curr->magic, ARCH_CONVERT) == XFS_ATTR_LEAF_MAGIC);
  1070. /*
  1071. * Search an intermediate node for a match.
  1072. */
  1073. blk->magic = INT_GET(curr->magic, ARCH_CONVERT);
  1074. if (INT_GET(curr->magic, ARCH_CONVERT) == XFS_DA_NODE_MAGIC) {
  1075. node = blk->bp->data;
  1076. blk->hashval = INT_GET(node->btree[ INT_GET(node->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT);
  1077. /*
  1078. * Binary search. (note: small blocks will skip loop)
  1079. */
  1080. max = INT_GET(node->hdr.count, ARCH_CONVERT);
  1081. probe = span = max / 2;
  1082. hashval = args->hashval;
  1083. for (btree = &node->btree[probe]; span > 4;
  1084. btree = &node->btree[probe]) {
  1085. span /= 2;
  1086. if (INT_GET(btree->hashval, ARCH_CONVERT) < hashval)
  1087. probe += span;
  1088. else if (INT_GET(btree->hashval, ARCH_CONVERT) > hashval)
  1089. probe -= span;
  1090. else
  1091. break;
  1092. }
  1093. ASSERT((probe >= 0) && (probe < max));
  1094. ASSERT((span <= 4) || (INT_GET(btree->hashval, ARCH_CONVERT) == hashval));
  1095. /*
  1096. * Since we may have duplicate hashval's, find the first
  1097. * matching hashval in the node.
  1098. */
  1099. while ((probe > 0) && (INT_GET(btree->hashval, ARCH_CONVERT) >= hashval)) {
  1100. btree--;
  1101. probe--;
  1102. }
  1103. while ((probe < max) && (INT_GET(btree->hashval, ARCH_CONVERT) < hashval)) {
  1104. btree++;
  1105. probe++;
  1106. }
  1107. /*
  1108. * Pick the right block to descend on.
  1109. */
  1110. if (probe == max) {
  1111. blk->index = max-1;
  1112. blkno = INT_GET(node->btree[ max-1 ].before, ARCH_CONVERT);
  1113. } else {
  1114. blk->index = probe;
  1115. blkno = INT_GET(btree->before, ARCH_CONVERT);
  1116. }
  1117. }
  1118. else if (INT_GET(curr->magic, ARCH_CONVERT) == XFS_ATTR_LEAF_MAGIC) {
  1119. blk->hashval = xfs_attr_leaf_lasthash(blk->bp, NULL);
  1120. break;
  1121. }
  1122. else if (INT_GET(curr->magic, ARCH_CONVERT) == XFS_DIR_LEAF_MAGIC) {
  1123. blk->hashval = xfs_dir_leaf_lasthash(blk->bp, NULL);
  1124. break;
  1125. }
  1126. else if (INT_GET(curr->magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC) {
  1127. blk->hashval = xfs_dir2_leafn_lasthash(blk->bp, NULL);
  1128. break;
  1129. }
  1130. }
  1131. /*
  1132. * A leaf block that ends in the hashval that we are interested in
  1133. * (final hashval == search hashval) means that the next block may
  1134. * contain more entries with the same hashval, shift upward to the
  1135. * next leaf and keep searching.
  1136. */
  1137. for (;;) {
  1138. if (blk->magic == XFS_DIR_LEAF_MAGIC) {
  1139. ASSERT(XFS_DIR_IS_V1(state->mp));
  1140. retval = xfs_dir_leaf_lookup_int(blk->bp, args,
  1141. &blk->index);
  1142. } else if (blk->magic == XFS_DIR2_LEAFN_MAGIC) {
  1143. ASSERT(XFS_DIR_IS_V2(state->mp));
  1144. retval = xfs_dir2_leafn_lookup_int(blk->bp, args,
  1145. &blk->index, state);
  1146. }
  1147. else if (blk->magic == XFS_ATTR_LEAF_MAGIC) {
  1148. retval = xfs_attr_leaf_lookup_int(blk->bp, args);
  1149. blk->index = args->index;
  1150. args->blkno = blk->blkno;
  1151. }
  1152. if (((retval == ENOENT) || (retval == ENOATTR)) &&
  1153. (blk->hashval == args->hashval)) {
  1154. error = xfs_da_path_shift(state, &state->path, 1, 1,
  1155. &retval);
  1156. if (error)
  1157. return(error);
  1158. if (retval == 0) {
  1159. continue;
  1160. }
  1161. else if (blk->magic == XFS_ATTR_LEAF_MAGIC) {
  1162. /* path_shift() gives ENOENT */
  1163. retval = XFS_ERROR(ENOATTR);
  1164. }
  1165. }
  1166. break;
  1167. }
  1168. *result = retval;
  1169. return(0);
  1170. }
  1171. /*========================================================================
  1172. * Utility routines.
  1173. *========================================================================*/
  1174. /*
  1175. * Link a new block into a doubly linked list of blocks (of whatever type).
  1176. */
  1177. int /* error */
  1178. xfs_da_blk_link(xfs_da_state_t *state, xfs_da_state_blk_t *old_blk,
  1179. xfs_da_state_blk_t *new_blk)
  1180. {
  1181. xfs_da_blkinfo_t *old_info, *new_info, *tmp_info;
  1182. xfs_da_args_t *args;
  1183. int before=0, error;
  1184. xfs_dabuf_t *bp;
  1185. /*
  1186. * Set up environment.
  1187. */
  1188. args = state->args;
  1189. ASSERT(args != NULL);
  1190. old_info = old_blk->bp->data;
  1191. new_info = new_blk->bp->data;
  1192. ASSERT(old_blk->magic == XFS_DA_NODE_MAGIC ||
  1193. old_blk->magic == XFS_DIRX_LEAF_MAGIC(state->mp) ||
  1194. old_blk->magic == XFS_ATTR_LEAF_MAGIC);
  1195. ASSERT(old_blk->magic == INT_GET(old_info->magic, ARCH_CONVERT));
  1196. ASSERT(new_blk->magic == INT_GET(new_info->magic, ARCH_CONVERT));
  1197. ASSERT(old_blk->magic == new_blk->magic);
  1198. switch (old_blk->magic) {
  1199. case XFS_ATTR_LEAF_MAGIC:
  1200. before = xfs_attr_leaf_order(old_blk->bp, new_blk->bp);
  1201. break;
  1202. case XFS_DIR_LEAF_MAGIC:
  1203. ASSERT(XFS_DIR_IS_V1(state->mp));
  1204. before = xfs_dir_leaf_order(old_blk->bp, new_blk->bp);
  1205. break;
  1206. case XFS_DIR2_LEAFN_MAGIC:
  1207. ASSERT(XFS_DIR_IS_V2(state->mp));
  1208. before = xfs_dir2_leafn_order(old_blk->bp, new_blk->bp);
  1209. break;
  1210. case XFS_DA_NODE_MAGIC:
  1211. before = xfs_da_node_order(old_blk->bp, new_blk->bp);
  1212. break;
  1213. }
  1214. /*
  1215. * Link blocks in appropriate order.
  1216. */
  1217. if (before) {
  1218. /*
  1219. * Link new block in before existing block.
  1220. */
  1221. INT_SET(new_info->forw, ARCH_CONVERT, old_blk->blkno);
  1222. new_info->back = old_info->back; /* INT_: direct copy */
  1223. if (INT_GET(old_info->back, ARCH_CONVERT)) {
  1224. error = xfs_da_read_buf(args->trans, args->dp,
  1225. INT_GET(old_info->back,
  1226. ARCH_CONVERT), -1, &bp,
  1227. args->whichfork);
  1228. if (error)
  1229. return(error);
  1230. ASSERT(bp != NULL);
  1231. tmp_info = bp->data;
  1232. ASSERT(INT_GET(tmp_info->magic, ARCH_CONVERT) == INT_GET(old_info->magic, ARCH_CONVERT));
  1233. ASSERT(INT_GET(tmp_info->forw, ARCH_CONVERT) == old_blk->blkno);
  1234. INT_SET(tmp_info->forw, ARCH_CONVERT, new_blk->blkno);
  1235. xfs_da_log_buf(args->trans, bp, 0, sizeof(*tmp_info)-1);
  1236. xfs_da_buf_done(bp);
  1237. }
  1238. INT_SET(old_info->back, ARCH_CONVERT, new_blk->blkno);
  1239. } else {
  1240. /*
  1241. * Link new block in after existing block.
  1242. */
  1243. new_info->forw = old_info->forw; /* INT_: direct copy */
  1244. INT_SET(new_info->back, ARCH_CONVERT, old_blk->blkno);
  1245. if (INT_GET(old_info->forw, ARCH_CONVERT)) {
  1246. error = xfs_da_read_buf(args->trans, args->dp,
  1247. INT_GET(old_info->forw, ARCH_CONVERT), -1, &bp,
  1248. args->whichfork);
  1249. if (error)
  1250. return(error);
  1251. ASSERT(bp != NULL);
  1252. tmp_info = bp->data;
  1253. ASSERT(INT_GET(tmp_info->magic, ARCH_CONVERT)
  1254. == INT_GET(old_info->magic, ARCH_CONVERT));
  1255. ASSERT(INT_GET(tmp_info->back, ARCH_CONVERT)
  1256. == old_blk->blkno);
  1257. INT_SET(tmp_info->back, ARCH_CONVERT, new_blk->blkno);
  1258. xfs_da_log_buf(args->trans, bp, 0, sizeof(*tmp_info)-1);
  1259. xfs_da_buf_done(bp);
  1260. }
  1261. INT_SET(old_info->forw, ARCH_CONVERT, new_blk->blkno);
  1262. }
  1263. xfs_da_log_buf(args->trans, old_blk->bp, 0, sizeof(*tmp_info) - 1);
  1264. xfs_da_log_buf(args->trans, new_blk->bp, 0, sizeof(*tmp_info) - 1);
  1265. return(0);
  1266. }
  1267. /*
  1268. * Compare two intermediate nodes for "order".
  1269. */
  1270. STATIC int
  1271. xfs_da_node_order(xfs_dabuf_t *node1_bp, xfs_dabuf_t *node2_bp)
  1272. {
  1273. xfs_da_intnode_t *node1, *node2;
  1274. node1 = node1_bp->data;
  1275. node2 = node2_bp->data;
  1276. ASSERT((INT_GET(node1->hdr.info.magic, ARCH_CONVERT) == XFS_DA_NODE_MAGIC) &&
  1277. (INT_GET(node2->hdr.info.magic, ARCH_CONVERT) == XFS_DA_NODE_MAGIC));
  1278. if ((INT_GET(node1->hdr.count, ARCH_CONVERT) > 0) && (INT_GET(node2->hdr.count, ARCH_CONVERT) > 0) &&
  1279. ((INT_GET(node2->btree[ 0 ].hashval, ARCH_CONVERT) <
  1280. INT_GET(node1->btree[ 0 ].hashval, ARCH_CONVERT)) ||
  1281. (INT_GET(node2->btree[ INT_GET(node2->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT) <
  1282. INT_GET(node1->btree[ INT_GET(node1->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT)))) {
  1283. return(1);
  1284. }
  1285. return(0);
  1286. }
  1287. /*
  1288. * Pick up the last hashvalue from an intermediate node.
  1289. */
  1290. STATIC uint
  1291. xfs_da_node_lasthash(xfs_dabuf_t *bp, int *count)
  1292. {
  1293. xfs_da_intnode_t *node;
  1294. node = bp->data;
  1295. ASSERT(INT_GET(node->hdr.info.magic, ARCH_CONVERT) == XFS_DA_NODE_MAGIC);
  1296. if (count)
  1297. *count = INT_GET(node->hdr.count, ARCH_CONVERT);
  1298. if (!node->hdr.count)
  1299. return(0);
  1300. return(INT_GET(node->btree[ INT_GET(node->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT));
  1301. }
  1302. /*
  1303. * Unlink a block from a doubly linked list of blocks.
  1304. */
  1305. STATIC int /* error */
  1306. xfs_da_blk_unlink(xfs_da_state_t *state, xfs_da_state_blk_t *drop_blk,
  1307. xfs_da_state_blk_t *save_blk)
  1308. {
  1309. xfs_da_blkinfo_t *drop_info, *save_info, *tmp_info;
  1310. xfs_da_args_t *args;
  1311. xfs_dabuf_t *bp;
  1312. int error;
  1313. /*
  1314. * Set up environment.
  1315. */
  1316. args = state->args;
  1317. ASSERT(args != NULL);
  1318. save_info = save_blk->bp->data;
  1319. drop_info = drop_blk->bp->data;
  1320. ASSERT(save_blk->magic == XFS_DA_NODE_MAGIC ||
  1321. save_blk->magic == XFS_DIRX_LEAF_MAGIC(state->mp) ||
  1322. save_blk->magic == XFS_ATTR_LEAF_MAGIC);
  1323. ASSERT(save_blk->magic == INT_GET(save_info->magic, ARCH_CONVERT));
  1324. ASSERT(drop_blk->magic == INT_GET(drop_info->magic, ARCH_CONVERT));
  1325. ASSERT(save_blk->magic == drop_blk->magic);
  1326. ASSERT((INT_GET(save_info->forw, ARCH_CONVERT) == drop_blk->blkno) ||
  1327. (INT_GET(save_info->back, ARCH_CONVERT) == drop_blk->blkno));
  1328. ASSERT((INT_GET(drop_info->forw, ARCH_CONVERT) == save_blk->blkno) ||
  1329. (INT_GET(drop_info->back, ARCH_CONVERT) == save_blk->blkno));
  1330. /*
  1331. * Unlink the leaf block from the doubly linked chain of leaves.
  1332. */
  1333. if (INT_GET(save_info->back, ARCH_CONVERT) == drop_blk->blkno) {
  1334. save_info->back = drop_info->back; /* INT_: direct copy */
  1335. if (INT_GET(drop_info->back, ARCH_CONVERT)) {
  1336. error = xfs_da_read_buf(args->trans, args->dp,
  1337. INT_GET(drop_info->back,
  1338. ARCH_CONVERT), -1, &bp,
  1339. args->whichfork);
  1340. if (error)
  1341. return(error);
  1342. ASSERT(bp != NULL);
  1343. tmp_info = bp->data;
  1344. ASSERT(INT_GET(tmp_info->magic, ARCH_CONVERT) == INT_GET(save_info->magic, ARCH_CONVERT));
  1345. ASSERT(INT_GET(tmp_info->forw, ARCH_CONVERT) == drop_blk->blkno);
  1346. INT_SET(tmp_info->forw, ARCH_CONVERT, save_blk->blkno);
  1347. xfs_da_log_buf(args->trans, bp, 0,
  1348. sizeof(*tmp_info) - 1);
  1349. xfs_da_buf_done(bp);
  1350. }
  1351. } else {
  1352. save_info->forw = drop_info->forw; /* INT_: direct copy */
  1353. if (INT_GET(drop_info->forw, ARCH_CONVERT)) {
  1354. error = xfs_da_read_buf(args->trans, args->dp,
  1355. INT_GET(drop_info->forw, ARCH_CONVERT), -1, &bp,
  1356. args->whichfork);
  1357. if (error)
  1358. return(error);
  1359. ASSERT(bp != NULL);
  1360. tmp_info = bp->data;
  1361. ASSERT(INT_GET(tmp_info->magic, ARCH_CONVERT)
  1362. == INT_GET(save_info->magic, ARCH_CONVERT));
  1363. ASSERT(INT_GET(tmp_info->back, ARCH_CONVERT)
  1364. == drop_blk->blkno);
  1365. INT_SET(tmp_info->back, ARCH_CONVERT, save_blk->blkno);
  1366. xfs_da_log_buf(args->trans, bp, 0,
  1367. sizeof(*tmp_info) - 1);
  1368. xfs_da_buf_done(bp);
  1369. }
  1370. }
  1371. xfs_da_log_buf(args->trans, save_blk->bp, 0, sizeof(*save_info) - 1);
  1372. return(0);
  1373. }
  1374. /*
  1375. * Move a path "forward" or "!forward" one block at the current level.
  1376. *
  1377. * This routine will adjust a "path" to point to the next block
  1378. * "forward" (higher hashvalues) or "!forward" (lower hashvals) in the
  1379. * Btree, including updating pointers to the intermediate nodes between
  1380. * the new bottom and the root.
  1381. */
  1382. int /* error */
  1383. xfs_da_path_shift(xfs_da_state_t *state, xfs_da_state_path_t *path,
  1384. int forward, int release, int *result)
  1385. {
  1386. xfs_da_state_blk_t *blk;
  1387. xfs_da_blkinfo_t *info;
  1388. xfs_da_intnode_t *node;
  1389. xfs_da_args_t *args;
  1390. xfs_dablk_t blkno=0;
  1391. int level, error;
  1392. /*
  1393. * Roll up the Btree looking for the first block where our
  1394. * current index is not at the edge of the block. Note that
  1395. * we skip the bottom layer because we want the sibling block.
  1396. */
  1397. args = state->args;
  1398. ASSERT(args != NULL);
  1399. ASSERT(path != NULL);
  1400. ASSERT((path->active > 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1401. level = (path->active-1) - 1; /* skip bottom layer in path */
  1402. for (blk = &path->blk[level]; level >= 0; blk--, level--) {
  1403. ASSERT(blk->bp != NULL);
  1404. node = blk->bp->data;
  1405. ASSERT(INT_GET(node->hdr.info.magic, ARCH_CONVERT) == XFS_DA_NODE_MAGIC);
  1406. if (forward && (blk->index < INT_GET(node->hdr.count, ARCH_CONVERT)-1)) {
  1407. blk->index++;
  1408. blkno = INT_GET(node->btree[ blk->index ].before, ARCH_CONVERT);
  1409. break;
  1410. } else if (!forward && (blk->index > 0)) {
  1411. blk->index--;
  1412. blkno = INT_GET(node->btree[ blk->index ].before, ARCH_CONVERT);
  1413. break;
  1414. }
  1415. }
  1416. if (level < 0) {
  1417. *result = XFS_ERROR(ENOENT); /* we're out of our tree */
  1418. ASSERT(args->oknoent);
  1419. return(0);
  1420. }
  1421. /*
  1422. * Roll down the edge of the subtree until we reach the
  1423. * same depth we were at originally.
  1424. */
  1425. for (blk++, level++; level < path->active; blk++, level++) {
  1426. /*
  1427. * Release the old block.
  1428. * (if it's dirty, trans won't actually let go)
  1429. */
  1430. if (release)
  1431. xfs_da_brelse(args->trans, blk->bp);
  1432. /*
  1433. * Read the next child block.
  1434. */
  1435. blk->blkno = blkno;
  1436. error = xfs_da_read_buf(args->trans, args->dp, blkno, -1,
  1437. &blk->bp, args->whichfork);
  1438. if (error)
  1439. return(error);
  1440. ASSERT(blk->bp != NULL);
  1441. info = blk->bp->data;
  1442. ASSERT(INT_GET(info->magic, ARCH_CONVERT) == XFS_DA_NODE_MAGIC ||
  1443. INT_GET(info->magic, ARCH_CONVERT) == XFS_DIRX_LEAF_MAGIC(state->mp) ||
  1444. INT_GET(info->magic, ARCH_CONVERT) == XFS_ATTR_LEAF_MAGIC);
  1445. blk->magic = INT_GET(info->magic, ARCH_CONVERT);
  1446. if (INT_GET(info->magic, ARCH_CONVERT) == XFS_DA_NODE_MAGIC) {
  1447. node = (xfs_da_intnode_t *)info;
  1448. blk->hashval = INT_GET(node->btree[ INT_GET(node->hdr.count, ARCH_CONVERT)-1 ].hashval, ARCH_CONVERT);
  1449. if (forward)
  1450. blk->index = 0;
  1451. else
  1452. blk->index = INT_GET(node->hdr.count, ARCH_CONVERT)-1;
  1453. blkno = INT_GET(node->btree[ blk->index ].before, ARCH_CONVERT);
  1454. } else {
  1455. ASSERT(level == path->active-1);
  1456. blk->index = 0;
  1457. switch(blk->magic) {
  1458. case XFS_ATTR_LEAF_MAGIC:
  1459. blk->hashval = xfs_attr_leaf_lasthash(blk->bp,
  1460. NULL);
  1461. break;
  1462. case XFS_DIR_LEAF_MAGIC:
  1463. ASSERT(XFS_DIR_IS_V1(state->mp));
  1464. blk->hashval = xfs_dir_leaf_lasthash(blk->bp,
  1465. NULL);
  1466. break;
  1467. case XFS_DIR2_LEAFN_MAGIC:
  1468. ASSERT(XFS_DIR_IS_V2(state->mp));
  1469. blk->hashval = xfs_dir2_leafn_lasthash(blk->bp,
  1470. NULL);
  1471. break;
  1472. default:
  1473. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC ||
  1474. blk->magic ==
  1475. XFS_DIRX_LEAF_MAGIC(state->mp));
  1476. break;
  1477. }
  1478. }
  1479. }
  1480. *result = 0;
  1481. return(0);
  1482. }
  1483. /*========================================================================
  1484. * Utility routines.
  1485. *========================================================================*/
  1486. /*
  1487. * Implement a simple hash on a character string.
  1488. * Rotate the hash value by 7 bits, then XOR each character in.
  1489. * This is implemented with some source-level loop unrolling.
  1490. */
  1491. xfs_dahash_t
  1492. xfs_da_hashname(const uchar_t *name, int namelen)
  1493. {
  1494. xfs_dahash_t hash;
  1495. /*
  1496. * Do four characters at a time as long as we can.
  1497. */
  1498. for (hash = 0; namelen >= 4; namelen -= 4, name += 4)
  1499. hash = (name[0] << 21) ^ (name[1] << 14) ^ (name[2] << 7) ^
  1500. (name[3] << 0) ^ rol32(hash, 7 * 4);
  1501. /*
  1502. * Now do the rest of the characters.
  1503. */
  1504. switch (namelen) {
  1505. case 3:
  1506. return (name[0] << 14) ^ (name[1] << 7) ^ (name[2] << 0) ^
  1507. rol32(hash, 7 * 3);
  1508. case 2:
  1509. return (name[0] << 7) ^ (name[1] << 0) ^ rol32(hash, 7 * 2);
  1510. case 1:
  1511. return (name[0] << 0) ^ rol32(hash, 7 * 1);
  1512. default: /* case 0: */
  1513. return hash;
  1514. }
  1515. }
  1516. /*
  1517. * Add a block to the btree ahead of the file.
  1518. * Return the new block number to the caller.
  1519. */
  1520. int
  1521. xfs_da_grow_inode(xfs_da_args_t *args, xfs_dablk_t *new_blkno)
  1522. {
  1523. xfs_fileoff_t bno, b;
  1524. xfs_bmbt_irec_t map;
  1525. xfs_bmbt_irec_t *mapp;
  1526. xfs_inode_t *dp;
  1527. int nmap, error, w, count, c, got, i, mapi;
  1528. xfs_fsize_t size;
  1529. xfs_trans_t *tp;
  1530. xfs_mount_t *mp;
  1531. dp = args->dp;
  1532. mp = dp->i_mount;
  1533. w = args->whichfork;
  1534. tp = args->trans;
  1535. /*
  1536. * For new directories adjust the file offset and block count.
  1537. */
  1538. if (w == XFS_DATA_FORK && XFS_DIR_IS_V2(mp)) {
  1539. bno = mp->m_dirleafblk;
  1540. count = mp->m_dirblkfsbs;
  1541. } else {
  1542. bno = 0;
  1543. count = 1;
  1544. }
  1545. /*
  1546. * Find a spot in the file space to put the new block.
  1547. */
  1548. if ((error = xfs_bmap_first_unused(tp, dp, count, &bno, w))) {
  1549. return error;
  1550. }
  1551. if (w == XFS_DATA_FORK && XFS_DIR_IS_V2(mp))
  1552. ASSERT(bno >= mp->m_dirleafblk && bno < mp->m_dirfreeblk);
  1553. /*
  1554. * Try mapping it in one filesystem block.
  1555. */
  1556. nmap = 1;
  1557. ASSERT(args->firstblock != NULL);
  1558. if ((error = xfs_bmapi(tp, dp, bno, count,
  1559. XFS_BMAPI_AFLAG(w)|XFS_BMAPI_WRITE|XFS_BMAPI_METADATA|
  1560. XFS_BMAPI_CONTIG,
  1561. args->firstblock, args->total, &map, &nmap,
  1562. args->flist))) {
  1563. return error;
  1564. }
  1565. ASSERT(nmap <= 1);
  1566. if (nmap == 1) {
  1567. mapp = &map;
  1568. mapi = 1;
  1569. }
  1570. /*
  1571. * If we didn't get it and the block might work if fragmented,
  1572. * try without the CONTIG flag. Loop until we get it all.
  1573. */
  1574. else if (nmap == 0 && count > 1) {
  1575. mapp = kmem_alloc(sizeof(*mapp) * count, KM_SLEEP);
  1576. for (b = bno, mapi = 0; b < bno + count; ) {
  1577. nmap = MIN(XFS_BMAP_MAX_NMAP, count);
  1578. c = (int)(bno + count - b);
  1579. if ((error = xfs_bmapi(tp, dp, b, c,
  1580. XFS_BMAPI_AFLAG(w)|XFS_BMAPI_WRITE|
  1581. XFS_BMAPI_METADATA,
  1582. args->firstblock, args->total,
  1583. &mapp[mapi], &nmap, args->flist))) {
  1584. kmem_free(mapp, sizeof(*mapp) * count);
  1585. return error;
  1586. }
  1587. if (nmap < 1)
  1588. break;
  1589. mapi += nmap;
  1590. b = mapp[mapi - 1].br_startoff +
  1591. mapp[mapi - 1].br_blockcount;
  1592. }
  1593. } else {
  1594. mapi = 0;
  1595. mapp = NULL;
  1596. }
  1597. /*
  1598. * Count the blocks we got, make sure it matches the total.
  1599. */
  1600. for (i = 0, got = 0; i < mapi; i++)
  1601. got += mapp[i].br_blockcount;
  1602. if (got != count || mapp[0].br_startoff != bno ||
  1603. mapp[mapi - 1].br_startoff + mapp[mapi - 1].br_blockcount !=
  1604. bno + count) {
  1605. if (mapp != &map)
  1606. kmem_free(mapp, sizeof(*mapp) * count);
  1607. return XFS_ERROR(ENOSPC);
  1608. }
  1609. if (mapp != &map)
  1610. kmem_free(mapp, sizeof(*mapp) * count);
  1611. *new_blkno = (xfs_dablk_t)bno;
  1612. /*
  1613. * For version 1 directories, adjust the file size if it changed.
  1614. */
  1615. if (w == XFS_DATA_FORK && XFS_DIR_IS_V1(mp)) {
  1616. ASSERT(mapi == 1);
  1617. if ((error = xfs_bmap_last_offset(tp, dp, &bno, w)))
  1618. return error;
  1619. size = XFS_FSB_TO_B(mp, bno);
  1620. if (size != dp->i_d.di_size) {
  1621. dp->i_d.di_size = size;
  1622. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  1623. }
  1624. }
  1625. return 0;
  1626. }
  1627. /*
  1628. * Ick. We need to always be able to remove a btree block, even
  1629. * if there's no space reservation because the filesystem is full.
  1630. * This is called if xfs_bunmapi on a btree block fails due to ENOSPC.
  1631. * It swaps the target block with the last block in the file. The
  1632. * last block in the file can always be removed since it can't cause
  1633. * a bmap btree split to do that.
  1634. */
  1635. STATIC int
  1636. xfs_da_swap_lastblock(xfs_da_args_t *args, xfs_dablk_t *dead_blknop,
  1637. xfs_dabuf_t **dead_bufp)
  1638. {
  1639. xfs_dablk_t dead_blkno, last_blkno, sib_blkno, par_blkno;
  1640. xfs_dabuf_t *dead_buf, *last_buf, *sib_buf, *par_buf;
  1641. xfs_fileoff_t lastoff;
  1642. xfs_inode_t *ip;
  1643. xfs_trans_t *tp;
  1644. xfs_mount_t *mp;
  1645. int error, w, entno, level, dead_level;
  1646. xfs_da_blkinfo_t *dead_info, *sib_info;
  1647. xfs_da_intnode_t *par_node, *dead_node;
  1648. xfs_dir_leafblock_t *dead_leaf;
  1649. xfs_dir2_leaf_t *dead_leaf2;
  1650. xfs_dahash_t dead_hash;
  1651. dead_buf = *dead_bufp;
  1652. dead_blkno = *dead_blknop;
  1653. tp = args->trans;
  1654. ip = args->dp;
  1655. w = args->whichfork;
  1656. ASSERT(w == XFS_DATA_FORK);
  1657. mp = ip->i_mount;
  1658. if (XFS_DIR_IS_V2(mp)) {
  1659. lastoff = mp->m_dirfreeblk;
  1660. error = xfs_bmap_last_before(tp, ip, &lastoff, w);
  1661. } else
  1662. error = xfs_bmap_last_offset(tp, ip, &lastoff, w);
  1663. if (error)
  1664. return error;
  1665. if (unlikely(lastoff == 0)) {
  1666. XFS_ERROR_REPORT("xfs_da_swap_lastblock(1)", XFS_ERRLEVEL_LOW,
  1667. mp);
  1668. return XFS_ERROR(EFSCORRUPTED);
  1669. }
  1670. /*
  1671. * Read the last block in the btree space.
  1672. */
  1673. last_blkno = (xfs_dablk_t)lastoff - mp->m_dirblkfsbs;
  1674. if ((error = xfs_da_read_buf(tp, ip, last_blkno, -1, &last_buf, w)))
  1675. return error;
  1676. /*
  1677. * Copy the last block into the dead buffer and log it.
  1678. */
  1679. memcpy(dead_buf->data, last_buf->data, mp->m_dirblksize);
  1680. xfs_da_log_buf(tp, dead_buf, 0, mp->m_dirblksize - 1);
  1681. dead_info = dead_buf->data;
  1682. /*
  1683. * Get values from the moved block.
  1684. */
  1685. if (INT_GET(dead_info->magic, ARCH_CONVERT) == XFS_DIR_LEAF_MAGIC) {
  1686. ASSERT(XFS_DIR_IS_V1(mp));
  1687. dead_leaf = (xfs_dir_leafblock_t *)dead_info;
  1688. dead_level = 0;
  1689. dead_hash =
  1690. INT_GET(dead_leaf->entries[INT_GET(dead_leaf->hdr.count, ARCH_CONVERT) - 1].hashval, ARCH_CONVERT);
  1691. } else if (INT_GET(dead_info->magic, ARCH_CONVERT) == XFS_DIR2_LEAFN_MAGIC) {
  1692. ASSERT(XFS_DIR_IS_V2(mp));
  1693. dead_leaf2 = (xfs_dir2_leaf_t *)dead_info;
  1694. dead_level = 0;
  1695. dead_hash = INT_GET(dead_leaf2->ents[INT_GET(dead_leaf2->hdr.count, ARCH_CONVERT) - 1].hashval, ARCH_CONVERT);
  1696. } else {
  1697. ASSERT(INT_GET(dead_info->magic, ARCH_CONVERT) == XFS_DA_NODE_MAGIC);
  1698. dead_node = (xfs_da_intnode_t *)dead_info;
  1699. dead_level = INT_GET(dead_node->hdr.level, ARCH_CONVERT);
  1700. dead_hash = INT_GET(dead_node->btree[INT_GET(dead_node->hdr.count, ARCH_CONVERT) - 1].hashval, ARCH_CONVERT);
  1701. }
  1702. sib_buf = par_buf = NULL;
  1703. /*
  1704. * If the moved block has a left sibling, fix up the pointers.
  1705. */
  1706. if ((sib_blkno = INT_GET(dead_info->back, ARCH_CONVERT))) {
  1707. if ((error = xfs_da_read_buf(tp, ip, sib_blkno, -1, &sib_buf, w)))
  1708. goto done;
  1709. sib_info = sib_buf->data;
  1710. if (unlikely(
  1711. INT_GET(sib_info->forw, ARCH_CONVERT) != last_blkno ||
  1712. INT_GET(sib_info->magic, ARCH_CONVERT) != INT_GET(dead_info->magic, ARCH_CONVERT))) {
  1713. XFS_ERROR_REPORT("xfs_da_swap_lastblock(2)",
  1714. XFS_ERRLEVEL_LOW, mp);
  1715. error = XFS_ERROR(EFSCORRUPTED);
  1716. goto done;
  1717. }
  1718. INT_SET(sib_info->forw, ARCH_CONVERT, dead_blkno);
  1719. xfs_da_log_buf(tp, sib_buf,
  1720. XFS_DA_LOGRANGE(sib_info, &sib_info->forw,
  1721. sizeof(sib_info->forw)));
  1722. xfs_da_buf_done(sib_buf);
  1723. sib_buf = NULL;
  1724. }
  1725. /*
  1726. * If the moved block has a right sibling, fix up the pointers.
  1727. */
  1728. if ((sib_blkno = INT_GET(dead_info->forw, ARCH_CONVERT))) {
  1729. if ((error = xfs_da_read_buf(tp, ip, sib_blkno, -1, &sib_buf, w)))
  1730. goto done;
  1731. sib_info = sib_buf->data;
  1732. if (unlikely(
  1733. INT_GET(sib_info->back, ARCH_CONVERT) != last_blkno
  1734. || INT_GET(sib_info->magic, ARCH_CONVERT)
  1735. != INT_GET(dead_info->magic, ARCH_CONVERT))) {
  1736. XFS_ERROR_REPORT("xfs_da_swap_lastblock(3)",
  1737. XFS_ERRLEVEL_LOW, mp);
  1738. error = XFS_ERROR(EFSCORRUPTED);
  1739. goto done;
  1740. }
  1741. INT_SET(sib_info->back, ARCH_CONVERT, dead_blkno);
  1742. xfs_da_log_buf(tp, sib_buf,
  1743. XFS_DA_LOGRANGE(sib_info, &sib_info->back,
  1744. sizeof(sib_info->back)));
  1745. xfs_da_buf_done(sib_buf);
  1746. sib_buf = NULL;
  1747. }
  1748. par_blkno = XFS_DIR_IS_V1(mp) ? 0 : mp->m_dirleafblk;
  1749. level = -1;
  1750. /*
  1751. * Walk down the tree looking for the parent of the moved block.
  1752. */
  1753. for (;;) {
  1754. if ((error = xfs_da_read_buf(tp, ip, par_blkno, -1, &par_buf, w)))
  1755. goto done;
  1756. par_node = par_buf->data;
  1757. if (unlikely(
  1758. INT_GET(par_node->hdr.info.magic, ARCH_CONVERT) != XFS_DA_NODE_MAGIC ||
  1759. (level >= 0 && level != INT_GET(par_node->hdr.level, ARCH_CONVERT) + 1))) {
  1760. XFS_ERROR_REPORT("xfs_da_swap_lastblock(4)",
  1761. XFS_ERRLEVEL_LOW, mp);
  1762. error = XFS_ERROR(EFSCORRUPTED);
  1763. goto done;
  1764. }
  1765. level = INT_GET(par_node->hdr.level, ARCH_CONVERT);
  1766. for (entno = 0;
  1767. entno < INT_GET(par_node->hdr.count, ARCH_CONVERT) &&
  1768. INT_GET(par_node->btree[entno].hashval, ARCH_CONVERT) < dead_hash;
  1769. entno++)
  1770. continue;
  1771. if (unlikely(entno == INT_GET(par_node->hdr.count, ARCH_CONVERT))) {
  1772. XFS_ERROR_REPORT("xfs_da_swap_lastblock(5)",
  1773. XFS_ERRLEVEL_LOW, mp);
  1774. error = XFS_ERROR(EFSCORRUPTED);
  1775. goto done;
  1776. }
  1777. par_blkno = INT_GET(par_node->btree[entno].before, ARCH_CONVERT);
  1778. if (level == dead_level + 1)
  1779. break;
  1780. xfs_da_brelse(tp, par_buf);
  1781. par_buf = NULL;
  1782. }
  1783. /*
  1784. * We're in the right parent block.
  1785. * Look for the right entry.
  1786. */
  1787. for (;;) {
  1788. for (;
  1789. entno < INT_GET(par_node->hdr.count, ARCH_CONVERT) &&
  1790. INT_GET(par_node->btree[entno].before, ARCH_CONVERT) != last_blkno;
  1791. entno++)
  1792. continue;
  1793. if (entno < INT_GET(par_node->hdr.count, ARCH_CONVERT))
  1794. break;
  1795. par_blkno = INT_GET(par_node->hdr.info.forw, ARCH_CONVERT);
  1796. xfs_da_brelse(tp, par_buf);
  1797. par_buf = NULL;
  1798. if (unlikely(par_blkno == 0)) {
  1799. XFS_ERROR_REPORT("xfs_da_swap_lastblock(6)",
  1800. XFS_ERRLEVEL_LOW, mp);
  1801. error = XFS_ERROR(EFSCORRUPTED);
  1802. goto done;
  1803. }
  1804. if ((error = xfs_da_read_buf(tp, ip, par_blkno, -1, &par_buf, w)))
  1805. goto done;
  1806. par_node = par_buf->data;
  1807. if (unlikely(
  1808. INT_GET(par_node->hdr.level, ARCH_CONVERT) != level ||
  1809. INT_GET(par_node->hdr.info.magic, ARCH_CONVERT) != XFS_DA_NODE_MAGIC)) {
  1810. XFS_ERROR_REPORT("xfs_da_swap_lastblock(7)",
  1811. XFS_ERRLEVEL_LOW, mp);
  1812. error = XFS_ERROR(EFSCORRUPTED);
  1813. goto done;
  1814. }
  1815. entno = 0;
  1816. }
  1817. /*
  1818. * Update the parent entry pointing to the moved block.
  1819. */
  1820. INT_SET(par_node->btree[entno].before, ARCH_CONVERT, dead_blkno);
  1821. xfs_da_log_buf(tp, par_buf,
  1822. XFS_DA_LOGRANGE(par_node, &par_node->btree[entno].before,
  1823. sizeof(par_node->btree[entno].before)));
  1824. xfs_da_buf_done(par_buf);
  1825. xfs_da_buf_done(dead_buf);
  1826. *dead_blknop = last_blkno;
  1827. *dead_bufp = last_buf;
  1828. return 0;
  1829. done:
  1830. if (par_buf)
  1831. xfs_da_brelse(tp, par_buf);
  1832. if (sib_buf)
  1833. xfs_da_brelse(tp, sib_buf);
  1834. xfs_da_brelse(tp, last_buf);
  1835. return error;
  1836. }
  1837. /*
  1838. * Remove a btree block from a directory or attribute.
  1839. */
  1840. int
  1841. xfs_da_shrink_inode(xfs_da_args_t *args, xfs_dablk_t dead_blkno,
  1842. xfs_dabuf_t *dead_buf)
  1843. {
  1844. xfs_inode_t *dp;
  1845. int done, error, w, count;
  1846. xfs_fileoff_t bno;
  1847. xfs_fsize_t size;
  1848. xfs_trans_t *tp;
  1849. xfs_mount_t *mp;
  1850. dp = args->dp;
  1851. w = args->whichfork;
  1852. tp = args->trans;
  1853. mp = dp->i_mount;
  1854. if (w == XFS_DATA_FORK && XFS_DIR_IS_V2(mp))
  1855. count = mp->m_dirblkfsbs;
  1856. else
  1857. count = 1;
  1858. for (;;) {
  1859. /*
  1860. * Remove extents. If we get ENOSPC for a dir we have to move
  1861. * the last block to the place we want to kill.
  1862. */
  1863. if ((error = xfs_bunmapi(tp, dp, dead_blkno, count,
  1864. XFS_BMAPI_AFLAG(w)|XFS_BMAPI_METADATA,
  1865. 0, args->firstblock, args->flist,
  1866. &done)) == ENOSPC) {
  1867. if (w != XFS_DATA_FORK)
  1868. goto done;
  1869. if ((error = xfs_da_swap_lastblock(args, &dead_blkno,
  1870. &dead_buf)))
  1871. goto done;
  1872. } else if (error)
  1873. goto done;
  1874. else
  1875. break;
  1876. }
  1877. ASSERT(done);
  1878. xfs_da_binval(tp, dead_buf);
  1879. /*
  1880. * Adjust the directory size for version 1.
  1881. */
  1882. if (w == XFS_DATA_FORK && XFS_DIR_IS_V1(mp)) {
  1883. if ((error = xfs_bmap_last_offset(tp, dp, &bno, w)))
  1884. return error;
  1885. size = XFS_FSB_TO_B(dp->i_mount, bno);
  1886. if (size != dp->i_d.di_size) {
  1887. dp->i_d.di_size = size;
  1888. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  1889. }
  1890. }
  1891. return 0;
  1892. done:
  1893. xfs_da_binval(tp, dead_buf);
  1894. return error;
  1895. }
  1896. /*
  1897. * See if the mapping(s) for this btree block are valid, i.e.
  1898. * don't contain holes, are logically contiguous, and cover the whole range.
  1899. */
  1900. STATIC int
  1901. xfs_da_map_covers_blocks(
  1902. int nmap,
  1903. xfs_bmbt_irec_t *mapp,
  1904. xfs_dablk_t bno,
  1905. int count)
  1906. {
  1907. int i;
  1908. xfs_fileoff_t off;
  1909. for (i = 0, off = bno; i < nmap; i++) {
  1910. if (mapp[i].br_startblock == HOLESTARTBLOCK ||
  1911. mapp[i].br_startblock == DELAYSTARTBLOCK) {
  1912. return 0;
  1913. }
  1914. if (off != mapp[i].br_startoff) {
  1915. return 0;
  1916. }
  1917. off += mapp[i].br_blockcount;
  1918. }
  1919. return off == bno + count;
  1920. }
  1921. /*
  1922. * Make a dabuf.
  1923. * Used for get_buf, read_buf, read_bufr, and reada_buf.
  1924. */
  1925. STATIC int
  1926. xfs_da_do_buf(
  1927. xfs_trans_t *trans,
  1928. xfs_inode_t *dp,
  1929. xfs_dablk_t bno,
  1930. xfs_daddr_t *mappedbnop,
  1931. xfs_dabuf_t **bpp,
  1932. int whichfork,
  1933. int caller,
  1934. inst_t *ra)
  1935. {
  1936. xfs_buf_t *bp = NULL;
  1937. xfs_buf_t **bplist;
  1938. int error=0;
  1939. int i;
  1940. xfs_bmbt_irec_t map;
  1941. xfs_bmbt_irec_t *mapp;
  1942. xfs_daddr_t mappedbno;
  1943. xfs_mount_t *mp;
  1944. int nbplist=0;
  1945. int nfsb;
  1946. int nmap;
  1947. xfs_dabuf_t *rbp;
  1948. mp = dp->i_mount;
  1949. if (whichfork == XFS_DATA_FORK && XFS_DIR_IS_V2(mp))
  1950. nfsb = mp->m_dirblkfsbs;
  1951. else
  1952. nfsb = 1;
  1953. mappedbno = *mappedbnop;
  1954. /*
  1955. * Caller doesn't have a mapping. -2 means don't complain
  1956. * if we land in a hole.
  1957. */
  1958. if (mappedbno == -1 || mappedbno == -2) {
  1959. /*
  1960. * Optimize the one-block case.
  1961. */
  1962. if (nfsb == 1) {
  1963. xfs_fsblock_t fsb;
  1964. if ((error =
  1965. xfs_bmapi_single(trans, dp, whichfork, &fsb,
  1966. (xfs_fileoff_t)bno))) {
  1967. return error;
  1968. }
  1969. mapp = &map;
  1970. if (fsb == NULLFSBLOCK) {
  1971. nmap = 0;
  1972. } else {
  1973. map.br_startblock = fsb;
  1974. map.br_startoff = (xfs_fileoff_t)bno;
  1975. map.br_blockcount = 1;
  1976. nmap = 1;
  1977. }
  1978. } else {
  1979. mapp = kmem_alloc(sizeof(*mapp) * nfsb, KM_SLEEP);
  1980. nmap = nfsb;
  1981. if ((error = xfs_bmapi(trans, dp, (xfs_fileoff_t)bno,
  1982. nfsb,
  1983. XFS_BMAPI_METADATA |
  1984. XFS_BMAPI_AFLAG(whichfork),
  1985. NULL, 0, mapp, &nmap, NULL)))
  1986. goto exit0;
  1987. }
  1988. } else {
  1989. map.br_startblock = XFS_DADDR_TO_FSB(mp, mappedbno);
  1990. map.br_startoff = (xfs_fileoff_t)bno;
  1991. map.br_blockcount = nfsb;
  1992. mapp = &map;
  1993. nmap = 1;
  1994. }
  1995. if (!xfs_da_map_covers_blocks(nmap, mapp, bno, nfsb)) {
  1996. error = mappedbno == -2 ? 0 : XFS_ERROR(EFSCORRUPTED);
  1997. if (unlikely(error == EFSCORRUPTED)) {
  1998. if (xfs_error_level >= XFS_ERRLEVEL_LOW) {
  1999. int i;
  2000. cmn_err(CE_ALERT, "xfs_da_do_buf: bno %lld\n",
  2001. (long long)bno);
  2002. cmn_err(CE_ALERT, "dir: inode %lld\n",
  2003. (long long)dp->i_ino);
  2004. for (i = 0; i < nmap; i++) {
  2005. cmn_err(CE_ALERT,
  2006. "[%02d] br_startoff %lld br_startblock %lld br_blockcount %lld br_state %d\n",
  2007. i,
  2008. (long long)mapp[i].br_startoff,
  2009. (long long)mapp[i].br_startblock,
  2010. (long long)mapp[i].br_blockcount,
  2011. mapp[i].br_state);
  2012. }
  2013. }
  2014. XFS_ERROR_REPORT("xfs_da_do_buf(1)",
  2015. XFS_ERRLEVEL_LOW, mp);
  2016. }
  2017. goto exit0;
  2018. }
  2019. if (caller != 3 && nmap > 1) {
  2020. bplist = kmem_alloc(sizeof(*bplist) * nmap, KM_SLEEP);
  2021. nbplist = 0;
  2022. } else
  2023. bplist = NULL;
  2024. /*
  2025. * Turn the mapping(s) into buffer(s).
  2026. */
  2027. for (i = 0; i < nmap; i++) {
  2028. int nmapped;
  2029. mappedbno = XFS_FSB_TO_DADDR(mp, mapp[i].br_startblock);
  2030. if (i == 0)
  2031. *mappedbnop = mappedbno;
  2032. nmapped = (int)XFS_FSB_TO_BB(mp, mapp[i].br_blockcount);
  2033. switch (caller) {
  2034. case 0:
  2035. bp = xfs_trans_get_buf(trans, mp->m_ddev_targp,
  2036. mappedbno, nmapped, 0);
  2037. error = bp ? XFS_BUF_GETERROR(bp) : XFS_ERROR(EIO);
  2038. break;
  2039. case 1:
  2040. case 2:
  2041. bp = NULL;
  2042. error = xfs_trans_read_buf(mp, trans, mp->m_ddev_targp,
  2043. mappedbno, nmapped, 0, &bp);
  2044. break;
  2045. case 3:
  2046. xfs_baread(mp->m_ddev_targp, mappedbno, nmapped);
  2047. error = 0;
  2048. bp = NULL;
  2049. break;
  2050. }
  2051. if (error) {
  2052. if (bp)
  2053. xfs_trans_brelse(trans, bp);
  2054. goto exit1;
  2055. }
  2056. if (!bp)
  2057. continue;
  2058. if (caller == 1) {
  2059. if (whichfork == XFS_ATTR_FORK) {
  2060. XFS_BUF_SET_VTYPE_REF(bp, B_FS_ATTR_BTREE,
  2061. XFS_ATTR_BTREE_REF);
  2062. } else {
  2063. XFS_BUF_SET_VTYPE_REF(bp, B_FS_DIR_BTREE,
  2064. XFS_DIR_BTREE_REF);
  2065. }
  2066. }
  2067. if (bplist) {
  2068. bplist[nbplist++] = bp;
  2069. }
  2070. }
  2071. /*
  2072. * Build a dabuf structure.
  2073. */
  2074. if (bplist) {
  2075. rbp = xfs_da_buf_make(nbplist, bplist, ra);
  2076. } else if (bp)
  2077. rbp = xfs_da_buf_make(1, &bp, ra);
  2078. else
  2079. rbp = NULL;
  2080. /*
  2081. * For read_buf, check the magic number.
  2082. */
  2083. if (caller == 1) {
  2084. xfs_dir2_data_t *data;
  2085. xfs_dir2_free_t *free;
  2086. xfs_da_blkinfo_t *info;
  2087. uint magic, magic1;
  2088. info = rbp->data;
  2089. data = rbp->data;
  2090. free = rbp->data;
  2091. magic = INT_GET(info->magic, ARCH_CONVERT);
  2092. magic1 = INT_GET(data->hdr.magic, ARCH_CONVERT);
  2093. if (unlikely(
  2094. XFS_TEST_ERROR((magic != XFS_DA_NODE_MAGIC) &&
  2095. (magic != XFS_DIR_LEAF_MAGIC) &&
  2096. (magic != XFS_ATTR_LEAF_MAGIC) &&
  2097. (magic != XFS_DIR2_LEAF1_MAGIC) &&
  2098. (magic != XFS_DIR2_LEAFN_MAGIC) &&
  2099. (magic1 != XFS_DIR2_BLOCK_MAGIC) &&
  2100. (magic1 != XFS_DIR2_DATA_MAGIC) &&
  2101. (INT_GET(free->hdr.magic, ARCH_CONVERT) != XFS_DIR2_FREE_MAGIC),
  2102. mp, XFS_ERRTAG_DA_READ_BUF,
  2103. XFS_RANDOM_DA_READ_BUF))) {
  2104. xfs_buftrace("DA READ ERROR", rbp->bps[0]);
  2105. XFS_CORRUPTION_ERROR("xfs_da_do_buf(2)",
  2106. XFS_ERRLEVEL_LOW, mp, info);
  2107. error = XFS_ERROR(EFSCORRUPTED);
  2108. xfs_da_brelse(trans, rbp);
  2109. nbplist = 0;
  2110. goto exit1;
  2111. }
  2112. }
  2113. if (bplist) {
  2114. kmem_free(bplist, sizeof(*bplist) * nmap);
  2115. }
  2116. if (mapp != &map) {
  2117. kmem_free(mapp, sizeof(*mapp) * nfsb);
  2118. }
  2119. if (bpp)
  2120. *bpp = rbp;
  2121. return 0;
  2122. exit1:
  2123. if (bplist) {
  2124. for (i = 0; i < nbplist; i++)
  2125. xfs_trans_brelse(trans, bplist[i]);
  2126. kmem_free(bplist, sizeof(*bplist) * nmap);
  2127. }
  2128. exit0:
  2129. if (mapp != &map)
  2130. kmem_free(mapp, sizeof(*mapp) * nfsb);
  2131. if (bpp)
  2132. *bpp = NULL;
  2133. return error;
  2134. }
  2135. /*
  2136. * Get a buffer for the dir/attr block.
  2137. */
  2138. int
  2139. xfs_da_get_buf(
  2140. xfs_trans_t *trans,
  2141. xfs_inode_t *dp,
  2142. xfs_dablk_t bno,
  2143. xfs_daddr_t mappedbno,
  2144. xfs_dabuf_t **bpp,
  2145. int whichfork)
  2146. {
  2147. return xfs_da_do_buf(trans, dp, bno, &mappedbno, bpp, whichfork, 0,
  2148. (inst_t *)__return_address);
  2149. }
  2150. /*
  2151. * Get a buffer for the dir/attr block, fill in the contents.
  2152. */
  2153. int
  2154. xfs_da_read_buf(
  2155. xfs_trans_t *trans,
  2156. xfs_inode_t *dp,
  2157. xfs_dablk_t bno,
  2158. xfs_daddr_t mappedbno,
  2159. xfs_dabuf_t **bpp,
  2160. int whichfork)
  2161. {
  2162. return xfs_da_do_buf(trans, dp, bno, &mappedbno, bpp, whichfork, 1,
  2163. (inst_t *)__return_address);
  2164. }
  2165. /*
  2166. * Readahead the dir/attr block.
  2167. */
  2168. xfs_daddr_t
  2169. xfs_da_reada_buf(
  2170. xfs_trans_t *trans,
  2171. xfs_inode_t *dp,
  2172. xfs_dablk_t bno,
  2173. int whichfork)
  2174. {
  2175. xfs_daddr_t rval;
  2176. rval = -1;
  2177. if (xfs_da_do_buf(trans, dp, bno, &rval, NULL, whichfork, 3,
  2178. (inst_t *)__return_address))
  2179. return -1;
  2180. else
  2181. return rval;
  2182. }
  2183. /*
  2184. * Calculate the number of bits needed to hold i different values.
  2185. */
  2186. uint
  2187. xfs_da_log2_roundup(uint i)
  2188. {
  2189. uint rval;
  2190. for (rval = 0; rval < NBBY * sizeof(i); rval++) {
  2191. if ((1 << rval) >= i)
  2192. break;
  2193. }
  2194. return(rval);
  2195. }
  2196. kmem_zone_t *xfs_da_state_zone; /* anchor for state struct zone */
  2197. kmem_zone_t *xfs_dabuf_zone; /* dabuf zone */
  2198. /*
  2199. * Allocate a dir-state structure.
  2200. * We don't put them on the stack since they're large.
  2201. */
  2202. xfs_da_state_t *
  2203. xfs_da_state_alloc(void)
  2204. {
  2205. return kmem_zone_zalloc(xfs_da_state_zone, KM_SLEEP);
  2206. }
  2207. /*
  2208. * Kill the altpath contents of a da-state structure.
  2209. */
  2210. STATIC void
  2211. xfs_da_state_kill_altpath(xfs_da_state_t *state)
  2212. {
  2213. int i;
  2214. for (i = 0; i < state->altpath.active; i++) {
  2215. if (state->altpath.blk[i].bp) {
  2216. if (state->altpath.blk[i].bp != state->path.blk[i].bp)
  2217. xfs_da_buf_done(state->altpath.blk[i].bp);
  2218. state->altpath.blk[i].bp = NULL;
  2219. }
  2220. }
  2221. state->altpath.active = 0;
  2222. }
  2223. /*
  2224. * Free a da-state structure.
  2225. */
  2226. void
  2227. xfs_da_state_free(xfs_da_state_t *state)
  2228. {
  2229. int i;
  2230. xfs_da_state_kill_altpath(state);
  2231. for (i = 0; i < state->path.active; i++) {
  2232. if (state->path.blk[i].bp)
  2233. xfs_da_buf_done(state->path.blk[i].bp);
  2234. }
  2235. if (state->extravalid && state->extrablk.bp)
  2236. xfs_da_buf_done(state->extrablk.bp);
  2237. #ifdef DEBUG
  2238. memset((char *)state, 0, sizeof(*state));
  2239. #endif /* DEBUG */
  2240. kmem_zone_free(xfs_da_state_zone, state);
  2241. }
  2242. #ifdef XFS_DABUF_DEBUG
  2243. xfs_dabuf_t *xfs_dabuf_global_list;
  2244. lock_t xfs_dabuf_global_lock;
  2245. #endif
  2246. /*
  2247. * Create a dabuf.
  2248. */
  2249. /* ARGSUSED */
  2250. STATIC xfs_dabuf_t *
  2251. xfs_da_buf_make(int nbuf, xfs_buf_t **bps, inst_t *ra)
  2252. {
  2253. xfs_buf_t *bp;
  2254. xfs_dabuf_t *dabuf;
  2255. int i;
  2256. int off;
  2257. if (nbuf == 1)
  2258. dabuf = kmem_zone_alloc(xfs_dabuf_zone, KM_SLEEP);
  2259. else
  2260. dabuf = kmem_alloc(XFS_DA_BUF_SIZE(nbuf), KM_SLEEP);
  2261. dabuf->dirty = 0;
  2262. #ifdef XFS_DABUF_DEBUG
  2263. dabuf->ra = ra;
  2264. dabuf->target = XFS_BUF_TARGET(bps[0]);
  2265. dabuf->blkno = XFS_BUF_ADDR(bps[0]);
  2266. #endif
  2267. if (nbuf == 1) {
  2268. dabuf->nbuf = 1;
  2269. bp = bps[0];
  2270. dabuf->bbcount = (short)BTOBB(XFS_BUF_COUNT(bp));
  2271. dabuf->data = XFS_BUF_PTR(bp);
  2272. dabuf->bps[0] = bp;
  2273. } else {
  2274. dabuf->nbuf = nbuf;
  2275. for (i = 0, dabuf->bbcount = 0; i < nbuf; i++) {
  2276. dabuf->bps[i] = bp = bps[i];
  2277. dabuf->bbcount += BTOBB(XFS_BUF_COUNT(bp));
  2278. }
  2279. dabuf->data = kmem_alloc(BBTOB(dabuf->bbcount), KM_SLEEP);
  2280. for (i = off = 0; i < nbuf; i++, off += XFS_BUF_COUNT(bp)) {
  2281. bp = bps[i];
  2282. memcpy((char *)dabuf->data + off, XFS_BUF_PTR(bp),
  2283. XFS_BUF_COUNT(bp));
  2284. }
  2285. }
  2286. #ifdef XFS_DABUF_DEBUG
  2287. {
  2288. SPLDECL(s);
  2289. xfs_dabuf_t *p;
  2290. s = mutex_spinlock(&xfs_dabuf_global_lock);
  2291. for (p = xfs_dabuf_global_list; p; p = p->next) {
  2292. ASSERT(p->blkno != dabuf->blkno ||
  2293. p->target != dabuf->target);
  2294. }
  2295. dabuf->prev = NULL;
  2296. if (xfs_dabuf_global_list)
  2297. xfs_dabuf_global_list->prev = dabuf;
  2298. dabuf->next = xfs_dabuf_global_list;
  2299. xfs_dabuf_global_list = dabuf;
  2300. mutex_spinunlock(&xfs_dabuf_global_lock, s);
  2301. }
  2302. #endif
  2303. return dabuf;
  2304. }
  2305. /*
  2306. * Un-dirty a dabuf.
  2307. */
  2308. STATIC void
  2309. xfs_da_buf_clean(xfs_dabuf_t *dabuf)
  2310. {
  2311. xfs_buf_t *bp;
  2312. int i;
  2313. int off;
  2314. if (dabuf->dirty) {
  2315. ASSERT(dabuf->nbuf > 1);
  2316. dabuf->dirty = 0;
  2317. for (i = off = 0; i < dabuf->nbuf;
  2318. i++, off += XFS_BUF_COUNT(bp)) {
  2319. bp = dabuf->bps[i];
  2320. memcpy(XFS_BUF_PTR(bp), (char *)dabuf->data + off,
  2321. XFS_BUF_COUNT(bp));
  2322. }
  2323. }
  2324. }
  2325. /*
  2326. * Release a dabuf.
  2327. */
  2328. void
  2329. xfs_da_buf_done(xfs_dabuf_t *dabuf)
  2330. {
  2331. ASSERT(dabuf);
  2332. ASSERT(dabuf->nbuf && dabuf->data && dabuf->bbcount && dabuf->bps[0]);
  2333. if (dabuf->dirty)
  2334. xfs_da_buf_clean(dabuf);
  2335. if (dabuf->nbuf > 1)
  2336. kmem_free(dabuf->data, BBTOB(dabuf->bbcount));
  2337. #ifdef XFS_DABUF_DEBUG
  2338. {
  2339. SPLDECL(s);
  2340. s = mutex_spinlock(&xfs_dabuf_global_lock);
  2341. if (dabuf->prev)
  2342. dabuf->prev->next = dabuf->next;
  2343. else
  2344. xfs_dabuf_global_list = dabuf->next;
  2345. if (dabuf->next)
  2346. dabuf->next->prev = dabuf->prev;
  2347. mutex_spinunlock(&xfs_dabuf_global_lock, s);
  2348. }
  2349. memset(dabuf, 0, XFS_DA_BUF_SIZE(dabuf->nbuf));
  2350. #endif
  2351. if (dabuf->nbuf == 1)
  2352. kmem_zone_free(xfs_dabuf_zone, dabuf);
  2353. else
  2354. kmem_free(dabuf, XFS_DA_BUF_SIZE(dabuf->nbuf));
  2355. }
  2356. /*
  2357. * Log transaction from a dabuf.
  2358. */
  2359. void
  2360. xfs_da_log_buf(xfs_trans_t *tp, xfs_dabuf_t *dabuf, uint first, uint last)
  2361. {
  2362. xfs_buf_t *bp;
  2363. uint f;
  2364. int i;
  2365. uint l;
  2366. int off;
  2367. ASSERT(dabuf->nbuf && dabuf->data && dabuf->bbcount && dabuf->bps[0]);
  2368. if (dabuf->nbuf == 1) {
  2369. ASSERT(dabuf->data == (void *)XFS_BUF_PTR(dabuf->bps[0]));
  2370. xfs_trans_log_buf(tp, dabuf->bps[0], first, last);
  2371. return;
  2372. }
  2373. dabuf->dirty = 1;
  2374. ASSERT(first <= last);
  2375. for (i = off = 0; i < dabuf->nbuf; i++, off += XFS_BUF_COUNT(bp)) {
  2376. bp = dabuf->bps[i];
  2377. f = off;
  2378. l = f + XFS_BUF_COUNT(bp) - 1;
  2379. if (f < first)
  2380. f = first;
  2381. if (l > last)
  2382. l = last;
  2383. if (f <= l)
  2384. xfs_trans_log_buf(tp, bp, f - off, l - off);
  2385. /*
  2386. * B_DONE is set by xfs_trans_log buf.
  2387. * If we don't set it on a new buffer (get not read)
  2388. * then if we don't put anything in the buffer it won't
  2389. * be set, and at commit it it released into the cache,
  2390. * and then a read will fail.
  2391. */
  2392. else if (!(XFS_BUF_ISDONE(bp)))
  2393. XFS_BUF_DONE(bp);
  2394. }
  2395. ASSERT(last < off);
  2396. }
  2397. /*
  2398. * Release dabuf from a transaction.
  2399. * Have to free up the dabuf before the buffers are released,
  2400. * since the synchronization on the dabuf is really the lock on the buffer.
  2401. */
  2402. void
  2403. xfs_da_brelse(xfs_trans_t *tp, xfs_dabuf_t *dabuf)
  2404. {
  2405. xfs_buf_t *bp;
  2406. xfs_buf_t **bplist;
  2407. int i;
  2408. int nbuf;
  2409. ASSERT(dabuf->nbuf && dabuf->data && dabuf->bbcount && dabuf->bps[0]);
  2410. if ((nbuf = dabuf->nbuf) == 1) {
  2411. bplist = &bp;
  2412. bp = dabuf->bps[0];
  2413. } else {
  2414. bplist = kmem_alloc(nbuf * sizeof(*bplist), KM_SLEEP);
  2415. memcpy(bplist, dabuf->bps, nbuf * sizeof(*bplist));
  2416. }
  2417. xfs_da_buf_done(dabuf);
  2418. for (i = 0; i < nbuf; i++)
  2419. xfs_trans_brelse(tp, bplist[i]);
  2420. if (bplist != &bp)
  2421. kmem_free(bplist, nbuf * sizeof(*bplist));
  2422. }
  2423. /*
  2424. * Invalidate dabuf from a transaction.
  2425. */
  2426. void
  2427. xfs_da_binval(xfs_trans_t *tp, xfs_dabuf_t *dabuf)
  2428. {
  2429. xfs_buf_t *bp;
  2430. xfs_buf_t **bplist;
  2431. int i;
  2432. int nbuf;
  2433. ASSERT(dabuf->nbuf && dabuf->data && dabuf->bbcount && dabuf->bps[0]);
  2434. if ((nbuf = dabuf->nbuf) == 1) {
  2435. bplist = &bp;
  2436. bp = dabuf->bps[0];
  2437. } else {
  2438. bplist = kmem_alloc(nbuf * sizeof(*bplist), KM_SLEEP);
  2439. memcpy(bplist, dabuf->bps, nbuf * sizeof(*bplist));
  2440. }
  2441. xfs_da_buf_done(dabuf);
  2442. for (i = 0; i < nbuf; i++)
  2443. xfs_trans_binval(tp, bplist[i]);
  2444. if (bplist != &bp)
  2445. kmem_free(bplist, nbuf * sizeof(*bplist));
  2446. }
  2447. /*
  2448. * Get the first daddr from a dabuf.
  2449. */
  2450. xfs_daddr_t
  2451. xfs_da_blkno(xfs_dabuf_t *dabuf)
  2452. {
  2453. ASSERT(dabuf->nbuf);
  2454. ASSERT(dabuf->data);
  2455. return XFS_BUF_ADDR(dabuf->bps[0]);
  2456. }