xfs_da_btree.c 66 KB

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