xfs_da_btree.c 62 KB

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