PageRenderTime 30ms CodeModel.GetById 20ms 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

Large files files are truncated, but you can click here to view the full file

  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,

Large files files are truncated, but you can click here to view the full file