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