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