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