PageRenderTime 74ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/fs/xfs/xfs_da_btree.c

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