xfs_da_btree.c 73 KB

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