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