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" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include "xfs_log.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 22 | #include "xfs_inum.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include "xfs_trans.h" |
| 24 | #include "xfs_sb.h" |
David Chinner | da353b0 | 2007-08-28 14:00:13 +1000 | [diff] [blame] | 25 | #include "xfs_ag.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include "xfs_dir2.h" |
| 27 | #include "xfs_dmapi.h" |
| 28 | #include "xfs_mount.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 29 | #include "xfs_da_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include "xfs_bmap_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include "xfs_dir2_sf.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 32 | #include "xfs_attr_sf.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include "xfs_dinode.h" |
| 34 | #include "xfs_inode.h" |
| 35 | #include "xfs_bmap.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include "xfs_dir2_data.h" |
| 37 | #include "xfs_dir2_leaf.h" |
| 38 | #include "xfs_dir2_block.h" |
| 39 | #include "xfs_dir2_node.h" |
| 40 | #include "xfs_dir2_trace.h" |
| 41 | #include "xfs_error.h" |
| 42 | |
| 43 | /* |
| 44 | * Function declarations. |
| 45 | */ |
| 46 | static void xfs_dir2_free_log_header(xfs_trans_t *tp, xfs_dabuf_t *bp); |
| 47 | static int xfs_dir2_leafn_add(xfs_dabuf_t *bp, xfs_da_args_t *args, int index); |
| 48 | #ifdef DEBUG |
| 49 | static void xfs_dir2_leafn_check(xfs_inode_t *dp, xfs_dabuf_t *bp); |
| 50 | #else |
| 51 | #define xfs_dir2_leafn_check(dp, bp) |
| 52 | #endif |
| 53 | static void xfs_dir2_leafn_moveents(xfs_da_args_t *args, xfs_dabuf_t *bp_s, |
| 54 | int start_s, xfs_dabuf_t *bp_d, int start_d, |
| 55 | int count); |
| 56 | static void xfs_dir2_leafn_rebalance(xfs_da_state_t *state, |
| 57 | xfs_da_state_blk_t *blk1, |
| 58 | xfs_da_state_blk_t *blk2); |
| 59 | static int xfs_dir2_leafn_remove(xfs_da_args_t *args, xfs_dabuf_t *bp, |
| 60 | int index, xfs_da_state_blk_t *dblk, |
| 61 | int *rval); |
| 62 | static int xfs_dir2_node_addname_int(xfs_da_args_t *args, |
| 63 | xfs_da_state_blk_t *fblk); |
| 64 | |
| 65 | /* |
| 66 | * Log entries from a freespace block. |
| 67 | */ |
| 68 | void |
| 69 | xfs_dir2_free_log_bests( |
| 70 | xfs_trans_t *tp, /* transaction pointer */ |
| 71 | xfs_dabuf_t *bp, /* freespace buffer */ |
| 72 | int first, /* first entry to log */ |
| 73 | int last) /* last entry to log */ |
| 74 | { |
| 75 | xfs_dir2_free_t *free; /* freespace structure */ |
| 76 | |
| 77 | free = bp->data; |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 78 | ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | xfs_da_log_buf(tp, bp, |
| 80 | (uint)((char *)&free->bests[first] - (char *)free), |
| 81 | (uint)((char *)&free->bests[last] - (char *)free + |
| 82 | sizeof(free->bests[0]) - 1)); |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | * Log header from a freespace block. |
| 87 | */ |
| 88 | static void |
| 89 | xfs_dir2_free_log_header( |
| 90 | xfs_trans_t *tp, /* transaction pointer */ |
| 91 | xfs_dabuf_t *bp) /* freespace buffer */ |
| 92 | { |
| 93 | xfs_dir2_free_t *free; /* freespace structure */ |
| 94 | |
| 95 | free = bp->data; |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 96 | ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | xfs_da_log_buf(tp, bp, (uint)((char *)&free->hdr - (char *)free), |
| 98 | (uint)(sizeof(xfs_dir2_free_hdr_t) - 1)); |
| 99 | } |
| 100 | |
| 101 | /* |
| 102 | * Convert a leaf-format directory to a node-format directory. |
| 103 | * We need to change the magic number of the leaf block, and copy |
| 104 | * the freespace table out of the leaf block into its own block. |
| 105 | */ |
| 106 | int /* error */ |
| 107 | xfs_dir2_leaf_to_node( |
| 108 | xfs_da_args_t *args, /* operation arguments */ |
| 109 | xfs_dabuf_t *lbp) /* leaf buffer */ |
| 110 | { |
| 111 | xfs_inode_t *dp; /* incore directory inode */ |
| 112 | int error; /* error return value */ |
| 113 | xfs_dabuf_t *fbp; /* freespace buffer */ |
| 114 | xfs_dir2_db_t fdb; /* freespace block number */ |
| 115 | xfs_dir2_free_t *free; /* freespace structure */ |
Nathan Scott | 68b3a10 | 2006-03-17 17:27:19 +1100 | [diff] [blame] | 116 | __be16 *from; /* pointer to freespace entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | int i; /* leaf freespace index */ |
| 118 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 119 | xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */ |
| 120 | xfs_mount_t *mp; /* filesystem mount point */ |
| 121 | int n; /* count of live freespc ents */ |
| 122 | xfs_dir2_data_off_t off; /* freespace entry value */ |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 123 | __be16 *to; /* pointer to freespace entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | xfs_trans_t *tp; /* transaction pointer */ |
| 125 | |
| 126 | xfs_dir2_trace_args_b("leaf_to_node", args, lbp); |
| 127 | dp = args->dp; |
| 128 | mp = dp->i_mount; |
| 129 | tp = args->trans; |
| 130 | /* |
| 131 | * Add a freespace block to the directory. |
| 132 | */ |
| 133 | if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, &fdb))) { |
| 134 | return error; |
| 135 | } |
| 136 | ASSERT(fdb == XFS_DIR2_FREE_FIRSTDB(mp)); |
| 137 | /* |
| 138 | * Get the buffer for the new freespace block. |
| 139 | */ |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 140 | if ((error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(mp, fdb), -1, &fbp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | XFS_DATA_FORK))) { |
| 142 | return error; |
| 143 | } |
| 144 | ASSERT(fbp != NULL); |
| 145 | free = fbp->data; |
| 146 | leaf = lbp->data; |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 147 | ltp = xfs_dir2_leaf_tail_p(mp, leaf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | /* |
| 149 | * Initialize the freespace block header. |
| 150 | */ |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 151 | free->hdr.magic = cpu_to_be32(XFS_DIR2_FREE_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | free->hdr.firstdb = 0; |
Nathan Scott | afbcb3f | 2006-03-17 17:27:28 +1100 | [diff] [blame] | 153 | ASSERT(be32_to_cpu(ltp->bestcount) <= (uint)dp->i_d.di_size / mp->m_dirblksize); |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 154 | free->hdr.nvalid = ltp->bestcount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | /* |
| 156 | * Copy freespace entries from the leaf block to the new block. |
| 157 | * Count active entries. |
| 158 | */ |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 159 | for (i = n = 0, from = xfs_dir2_leaf_bests_p(ltp), to = free->bests; |
Nathan Scott | afbcb3f | 2006-03-17 17:27:28 +1100 | [diff] [blame] | 160 | i < be32_to_cpu(ltp->bestcount); i++, from++, to++) { |
Nathan Scott | 68b3a10 | 2006-03-17 17:27:19 +1100 | [diff] [blame] | 161 | if ((off = be16_to_cpu(*from)) != NULLDATAOFF) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | n++; |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 163 | *to = cpu_to_be16(off); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | } |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 165 | free->hdr.nused = cpu_to_be32(n); |
Nathan Scott | 89da054 | 2006-03-17 17:28:40 +1100 | [diff] [blame] | 166 | leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAFN_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | /* |
| 168 | * Log everything. |
| 169 | */ |
| 170 | xfs_dir2_leaf_log_header(tp, lbp); |
| 171 | xfs_dir2_free_log_header(tp, fbp); |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 172 | xfs_dir2_free_log_bests(tp, fbp, 0, be32_to_cpu(free->hdr.nvalid) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | xfs_da_buf_done(fbp); |
| 174 | xfs_dir2_leafn_check(dp, lbp); |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | /* |
| 179 | * Add a leaf entry to a leaf block in a node-form directory. |
| 180 | * The other work necessary is done from the caller. |
| 181 | */ |
| 182 | static int /* error */ |
| 183 | xfs_dir2_leafn_add( |
| 184 | xfs_dabuf_t *bp, /* leaf buffer */ |
| 185 | xfs_da_args_t *args, /* operation arguments */ |
| 186 | int index) /* insertion pt for new entry */ |
| 187 | { |
| 188 | int compact; /* compacting stale leaves */ |
| 189 | xfs_inode_t *dp; /* incore directory inode */ |
| 190 | int highstale; /* next stale entry */ |
| 191 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 192 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ |
| 193 | int lfloghigh; /* high leaf entry logging */ |
| 194 | int lfloglow; /* low leaf entry logging */ |
| 195 | int lowstale; /* previous stale entry */ |
| 196 | xfs_mount_t *mp; /* filesystem mount point */ |
| 197 | xfs_trans_t *tp; /* transaction pointer */ |
| 198 | |
| 199 | xfs_dir2_trace_args_sb("leafn_add", args, index, bp); |
| 200 | dp = args->dp; |
| 201 | mp = dp->i_mount; |
| 202 | tp = args->trans; |
| 203 | leaf = bp->data; |
| 204 | |
| 205 | /* |
| 206 | * Quick check just to make sure we are not going to index |
| 207 | * into other peoples memory |
| 208 | */ |
| 209 | if (index < 0) |
| 210 | return XFS_ERROR(EFSCORRUPTED); |
| 211 | |
| 212 | /* |
| 213 | * If there are already the maximum number of leaf entries in |
| 214 | * the block, if there are no stale entries it won't fit. |
| 215 | * Caller will do a split. If there are stale entries we'll do |
| 216 | * a compact. |
| 217 | */ |
| 218 | |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 219 | if (be16_to_cpu(leaf->hdr.count) == xfs_dir2_max_leaf_ents(mp)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | if (!leaf->hdr.stale) |
| 221 | return XFS_ERROR(ENOSPC); |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 222 | compact = be16_to_cpu(leaf->hdr.stale) > 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | } else |
| 224 | compact = 0; |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 225 | ASSERT(index == 0 || be32_to_cpu(leaf->ents[index - 1].hashval) <= args->hashval); |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 226 | ASSERT(index == be16_to_cpu(leaf->hdr.count) || |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 227 | be32_to_cpu(leaf->ents[index].hashval) >= args->hashval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | |
| 229 | if (args->justcheck) |
| 230 | return 0; |
| 231 | |
| 232 | /* |
| 233 | * Compact out all but one stale leaf entry. Leaves behind |
| 234 | * the entry closest to index. |
| 235 | */ |
| 236 | if (compact) { |
| 237 | xfs_dir2_leaf_compact_x1(bp, &index, &lowstale, &highstale, |
| 238 | &lfloglow, &lfloghigh); |
| 239 | } |
| 240 | /* |
| 241 | * Set impossible logging indices for this case. |
| 242 | */ |
| 243 | else if (leaf->hdr.stale) { |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 244 | lfloglow = be16_to_cpu(leaf->hdr.count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | lfloghigh = -1; |
| 246 | } |
| 247 | /* |
| 248 | * No stale entries, just insert a space for the new entry. |
| 249 | */ |
| 250 | if (!leaf->hdr.stale) { |
| 251 | lep = &leaf->ents[index]; |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 252 | if (index < be16_to_cpu(leaf->hdr.count)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | memmove(lep + 1, lep, |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 254 | (be16_to_cpu(leaf->hdr.count) - index) * sizeof(*lep)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | lfloglow = index; |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 256 | lfloghigh = be16_to_cpu(leaf->hdr.count); |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 257 | be16_add_cpu(&leaf->hdr.count, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | } |
| 259 | /* |
| 260 | * There are stale entries. We'll use one for the new entry. |
| 261 | */ |
| 262 | else { |
| 263 | /* |
| 264 | * If we didn't do a compact then we need to figure out |
| 265 | * which stale entry will be used. |
| 266 | */ |
| 267 | if (compact == 0) { |
| 268 | /* |
| 269 | * Find first stale entry before our insertion point. |
| 270 | */ |
| 271 | for (lowstale = index - 1; |
| 272 | lowstale >= 0 && |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 273 | be32_to_cpu(leaf->ents[lowstale].address) != |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | XFS_DIR2_NULL_DATAPTR; |
| 275 | lowstale--) |
| 276 | continue; |
| 277 | /* |
| 278 | * Find next stale entry after insertion point. |
| 279 | * Stop looking if the answer would be worse than |
| 280 | * lowstale already found. |
| 281 | */ |
| 282 | for (highstale = index; |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 283 | highstale < be16_to_cpu(leaf->hdr.count) && |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 284 | be32_to_cpu(leaf->ents[highstale].address) != |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | XFS_DIR2_NULL_DATAPTR && |
| 286 | (lowstale < 0 || |
| 287 | index - lowstale - 1 >= highstale - index); |
| 288 | highstale++) |
| 289 | continue; |
| 290 | } |
| 291 | /* |
| 292 | * Using the low stale entry. |
| 293 | * Shift entries up toward the stale slot. |
| 294 | */ |
| 295 | if (lowstale >= 0 && |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 296 | (highstale == be16_to_cpu(leaf->hdr.count) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | index - lowstale - 1 < highstale - index)) { |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 298 | ASSERT(be32_to_cpu(leaf->ents[lowstale].address) == |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | XFS_DIR2_NULL_DATAPTR); |
| 300 | ASSERT(index - lowstale - 1 >= 0); |
| 301 | if (index - lowstale - 1 > 0) |
| 302 | memmove(&leaf->ents[lowstale], |
| 303 | &leaf->ents[lowstale + 1], |
| 304 | (index - lowstale - 1) * sizeof(*lep)); |
| 305 | lep = &leaf->ents[index - 1]; |
| 306 | lfloglow = MIN(lowstale, lfloglow); |
| 307 | lfloghigh = MAX(index - 1, lfloghigh); |
| 308 | } |
| 309 | /* |
| 310 | * Using the high stale entry. |
| 311 | * Shift entries down toward the stale slot. |
| 312 | */ |
| 313 | else { |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 314 | ASSERT(be32_to_cpu(leaf->ents[highstale].address) == |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | XFS_DIR2_NULL_DATAPTR); |
| 316 | ASSERT(highstale - index >= 0); |
| 317 | if (highstale - index > 0) |
| 318 | memmove(&leaf->ents[index + 1], |
| 319 | &leaf->ents[index], |
| 320 | (highstale - index) * sizeof(*lep)); |
| 321 | lep = &leaf->ents[index]; |
| 322 | lfloglow = MIN(index, lfloglow); |
| 323 | lfloghigh = MAX(highstale, lfloghigh); |
| 324 | } |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 325 | be16_add_cpu(&leaf->hdr.stale, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | } |
| 327 | /* |
| 328 | * Insert the new entry, log everything. |
| 329 | */ |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 330 | lep->hashval = cpu_to_be32(args->hashval); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 331 | lep->address = cpu_to_be32(xfs_dir2_db_off_to_dataptr(mp, |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 332 | args->blkno, args->index)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | xfs_dir2_leaf_log_header(tp, bp); |
| 334 | xfs_dir2_leaf_log_ents(tp, bp, lfloglow, lfloghigh); |
| 335 | xfs_dir2_leafn_check(dp, bp); |
| 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | #ifdef DEBUG |
| 340 | /* |
| 341 | * Check internal consistency of a leafn block. |
| 342 | */ |
| 343 | void |
| 344 | xfs_dir2_leafn_check( |
| 345 | xfs_inode_t *dp, /* incore directory inode */ |
| 346 | xfs_dabuf_t *bp) /* leaf buffer */ |
| 347 | { |
| 348 | int i; /* leaf index */ |
| 349 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 350 | xfs_mount_t *mp; /* filesystem mount point */ |
| 351 | int stale; /* count of stale leaves */ |
| 352 | |
| 353 | leaf = bp->data; |
| 354 | mp = dp->i_mount; |
Nathan Scott | 89da054 | 2006-03-17 17:28:40 +1100 | [diff] [blame] | 355 | ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 356 | ASSERT(be16_to_cpu(leaf->hdr.count) <= xfs_dir2_max_leaf_ents(mp)); |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 357 | for (i = stale = 0; i < be16_to_cpu(leaf->hdr.count); i++) { |
| 358 | if (i + 1 < be16_to_cpu(leaf->hdr.count)) { |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 359 | ASSERT(be32_to_cpu(leaf->ents[i].hashval) <= |
| 360 | be32_to_cpu(leaf->ents[i + 1].hashval)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | } |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 362 | if (be32_to_cpu(leaf->ents[i].address) == XFS_DIR2_NULL_DATAPTR) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | stale++; |
| 364 | } |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 365 | ASSERT(be16_to_cpu(leaf->hdr.stale) == stale); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | } |
| 367 | #endif /* DEBUG */ |
| 368 | |
| 369 | /* |
| 370 | * Return the last hash value in the leaf. |
| 371 | * Stale entries are ok. |
| 372 | */ |
| 373 | xfs_dahash_t /* hash value */ |
| 374 | xfs_dir2_leafn_lasthash( |
| 375 | xfs_dabuf_t *bp, /* leaf buffer */ |
| 376 | int *count) /* count of entries in leaf */ |
| 377 | { |
| 378 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 379 | |
| 380 | leaf = bp->data; |
Nathan Scott | 89da054 | 2006-03-17 17:28:40 +1100 | [diff] [blame] | 381 | ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | if (count) |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 383 | *count = be16_to_cpu(leaf->hdr.count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | if (!leaf->hdr.count) |
| 385 | return 0; |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 386 | return be32_to_cpu(leaf->ents[be16_to_cpu(leaf->hdr.count) - 1].hashval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | /* |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame^] | 390 | * Look up a leaf entry for space to add a name in a node-format leaf block. |
| 391 | * The extrablk in state is a freespace block. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | */ |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame^] | 393 | STATIC int |
| 394 | xfs_dir2_leafn_lookup_for_addname( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | xfs_dabuf_t *bp, /* leaf buffer */ |
| 396 | xfs_da_args_t *args, /* operation arguments */ |
| 397 | int *indexp, /* out: leaf entry index */ |
| 398 | xfs_da_state_t *state) /* state to fill in */ |
| 399 | { |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame^] | 400 | xfs_dabuf_t *curbp = NULL; /* current data/free buffer */ |
| 401 | xfs_dir2_db_t curdb = -1; /* current data block number */ |
| 402 | xfs_dir2_db_t curfdb = -1; /* current free block number */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | xfs_inode_t *dp; /* incore directory inode */ |
| 404 | int error; /* error return value */ |
| 405 | int fi; /* free entry index */ |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame^] | 406 | xfs_dir2_free_t *free = NULL; /* free block structure */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | int index; /* leaf entry index */ |
| 408 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame^] | 409 | int length; /* length of new data entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ |
| 411 | xfs_mount_t *mp; /* filesystem mount point */ |
| 412 | xfs_dir2_db_t newdb; /* new data block number */ |
| 413 | xfs_dir2_db_t newfdb; /* new free block number */ |
| 414 | xfs_trans_t *tp; /* transaction pointer */ |
| 415 | |
| 416 | dp = args->dp; |
| 417 | tp = args->trans; |
| 418 | mp = dp->i_mount; |
| 419 | leaf = bp->data; |
Nathan Scott | 89da054 | 2006-03-17 17:28:40 +1100 | [diff] [blame] | 420 | ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | #ifdef __KERNEL__ |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 422 | ASSERT(be16_to_cpu(leaf->hdr.count) > 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | #endif |
| 424 | xfs_dir2_leafn_check(dp, bp); |
| 425 | /* |
| 426 | * Look up the hash value in the leaf entries. |
| 427 | */ |
| 428 | index = xfs_dir2_leaf_search_hash(args, bp); |
| 429 | /* |
| 430 | * Do we have a buffer coming in? |
| 431 | */ |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame^] | 432 | if (state->extravalid) { |
| 433 | /* If so, it's a free block buffer, get the block number. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | curbp = state->extrablk.bp; |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame^] | 435 | curfdb = state->extrablk.blkno; |
| 436 | free = curbp->data; |
| 437 | ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | } |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame^] | 439 | length = xfs_dir2_data_entsize(args->namelen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | /* |
| 441 | * Loop over leaf entries with the right hash value. |
| 442 | */ |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame^] | 443 | for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) && |
| 444 | be32_to_cpu(lep->hashval) == args->hashval; |
| 445 | lep++, index++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | /* |
| 447 | * Skip stale leaf entries. |
| 448 | */ |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 449 | if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | continue; |
| 451 | /* |
| 452 | * Pull the data block number from the entry. |
| 453 | */ |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 454 | newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | /* |
| 456 | * For addname, we're looking for a place to put the new entry. |
| 457 | * We want to use a data block with an entry of equal |
| 458 | * hash value to ours if there is one with room. |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame^] | 459 | * |
| 460 | * If this block isn't the data block we already have |
| 461 | * in hand, take a look at it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | */ |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame^] | 463 | if (newdb != curdb) { |
| 464 | curdb = newdb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | /* |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame^] | 466 | * Convert the data block to the free block |
| 467 | * holding its freespace information. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | */ |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame^] | 469 | newfdb = xfs_dir2_db_to_fdb(mp, newdb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | /* |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame^] | 471 | * If it's not the one we have in hand, read it in. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | */ |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame^] | 473 | if (newfdb != curfdb) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | /* |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame^] | 475 | * If we had one before, drop it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | */ |
| 477 | if (curbp) |
| 478 | xfs_da_brelse(tp, curbp); |
| 479 | /* |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame^] | 480 | * Read the free block. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | */ |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame^] | 482 | error = xfs_da_read_buf(tp, dp, |
| 483 | xfs_dir2_db_to_da(mp, newfdb), |
| 484 | -1, &curbp, XFS_DATA_FORK); |
| 485 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | return error; |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame^] | 487 | free = curbp->data; |
| 488 | ASSERT(be32_to_cpu(free->hdr.magic) == |
| 489 | XFS_DIR2_FREE_MAGIC); |
| 490 | ASSERT((be32_to_cpu(free->hdr.firstdb) % |
| 491 | XFS_DIR2_MAX_FREE_BESTS(mp)) == 0); |
| 492 | ASSERT(be32_to_cpu(free->hdr.firstdb) <= curdb); |
| 493 | ASSERT(curdb < be32_to_cpu(free->hdr.firstdb) + |
| 494 | be32_to_cpu(free->hdr.nvalid)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | } |
| 496 | /* |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame^] | 497 | * Get the index for our entry. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | */ |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame^] | 499 | fi = xfs_dir2_db_to_fdindex(mp, curdb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | /* |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame^] | 501 | * If it has room, return it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | */ |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame^] | 503 | if (unlikely(be16_to_cpu(free->bests[fi]) == NULLDATAOFF)) { |
| 504 | XFS_ERROR_REPORT("xfs_dir2_leafn_lookup_int", |
| 505 | XFS_ERRLEVEL_LOW, mp); |
| 506 | if (curfdb != newfdb) |
| 507 | xfs_da_brelse(tp, curbp); |
| 508 | return XFS_ERROR(EFSCORRUPTED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | } |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame^] | 510 | curfdb = newfdb; |
| 511 | if (be16_to_cpu(free->bests[fi]) >= length) |
| 512 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | } |
| 514 | } |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame^] | 515 | /* Didn't find any space */ |
| 516 | fi = -1; |
| 517 | out: |
| 518 | ASSERT(args->oknoent); |
| 519 | if (curbp) { |
| 520 | /* Giving back a free block. */ |
| 521 | state->extravalid = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | state->extrablk.bp = curbp; |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame^] | 523 | state->extrablk.index = fi; |
| 524 | state->extrablk.blkno = curfdb; |
| 525 | state->extrablk.magic = XFS_DIR2_FREE_MAGIC; |
| 526 | } else { |
| 527 | state->extravalid = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | } |
| 529 | /* |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame^] | 530 | * Return the index, that will be the insertion point. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | */ |
| 532 | *indexp = index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | return XFS_ERROR(ENOENT); |
| 534 | } |
| 535 | |
| 536 | /* |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame^] | 537 | * Look up a leaf entry in a node-format leaf block. |
| 538 | * The extrablk in state a data block. |
| 539 | */ |
| 540 | STATIC int |
| 541 | xfs_dir2_leafn_lookup_for_entry( |
| 542 | xfs_dabuf_t *bp, /* leaf buffer */ |
| 543 | xfs_da_args_t *args, /* operation arguments */ |
| 544 | int *indexp, /* out: leaf entry index */ |
| 545 | xfs_da_state_t *state) /* state to fill in */ |
| 546 | { |
| 547 | xfs_dabuf_t *curbp = NULL; /* current data/free buffer */ |
| 548 | xfs_dir2_db_t curdb = -1; /* current data block number */ |
| 549 | xfs_dir2_data_entry_t *dep; /* data block entry */ |
| 550 | xfs_inode_t *dp; /* incore directory inode */ |
| 551 | int error; /* error return value */ |
| 552 | int di; /* data entry index */ |
| 553 | int index; /* leaf entry index */ |
| 554 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 555 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ |
| 556 | xfs_mount_t *mp; /* filesystem mount point */ |
| 557 | xfs_dir2_db_t newdb; /* new data block number */ |
| 558 | xfs_trans_t *tp; /* transaction pointer */ |
| 559 | |
| 560 | dp = args->dp; |
| 561 | tp = args->trans; |
| 562 | mp = dp->i_mount; |
| 563 | leaf = bp->data; |
| 564 | ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC); |
| 565 | #ifdef __KERNEL__ |
| 566 | ASSERT(be16_to_cpu(leaf->hdr.count) > 0); |
| 567 | #endif |
| 568 | xfs_dir2_leafn_check(dp, bp); |
| 569 | /* |
| 570 | * Look up the hash value in the leaf entries. |
| 571 | */ |
| 572 | index = xfs_dir2_leaf_search_hash(args, bp); |
| 573 | /* |
| 574 | * Do we have a buffer coming in? |
| 575 | */ |
| 576 | if (state->extravalid) { |
| 577 | curbp = state->extrablk.bp; |
| 578 | curdb = state->extrablk.blkno; |
| 579 | } |
| 580 | /* |
| 581 | * Loop over leaf entries with the right hash value. |
| 582 | */ |
| 583 | for (lep = &leaf->ents[index]; index < be16_to_cpu(leaf->hdr.count) && |
| 584 | be32_to_cpu(lep->hashval) == args->hashval; |
| 585 | lep++, index++) { |
| 586 | /* |
| 587 | * Skip stale leaf entries. |
| 588 | */ |
| 589 | if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR) |
| 590 | continue; |
| 591 | /* |
| 592 | * Pull the data block number from the entry. |
| 593 | */ |
| 594 | newdb = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address)); |
| 595 | /* |
| 596 | * Not adding a new entry, so we really want to find |
| 597 | * the name given to us. |
| 598 | * |
| 599 | * If it's a different data block, go get it. |
| 600 | */ |
| 601 | if (newdb != curdb) { |
| 602 | /* |
| 603 | * If we had a block before, drop it. |
| 604 | */ |
| 605 | if (curbp) |
| 606 | xfs_da_brelse(tp, curbp); |
| 607 | /* |
| 608 | * Read the data block. |
| 609 | */ |
| 610 | error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, |
| 611 | newdb), -1, &curbp, XFS_DATA_FORK); |
| 612 | if (error) |
| 613 | return error; |
| 614 | xfs_dir2_data_check(dp, curbp); |
| 615 | curdb = newdb; |
| 616 | } |
| 617 | /* |
| 618 | * Point to the data entry. |
| 619 | */ |
| 620 | dep = (xfs_dir2_data_entry_t *)((char *)curbp->data + |
| 621 | xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address))); |
| 622 | /* |
| 623 | * Compare the entry, return it if it matches. |
| 624 | */ |
| 625 | if (dep->namelen == args->namelen && memcmp(dep->name, |
| 626 | args->name, args->namelen) == 0) { |
| 627 | args->inumber = be64_to_cpu(dep->inumber); |
| 628 | di = (int)((char *)dep - (char *)curbp->data); |
| 629 | error = EEXIST; |
| 630 | goto out; |
| 631 | } |
| 632 | } |
| 633 | /* Didn't find a match. */ |
| 634 | error = ENOENT; |
| 635 | di = -1; |
| 636 | ASSERT(index == be16_to_cpu(leaf->hdr.count) || args->oknoent); |
| 637 | out: |
| 638 | if (curbp) { |
| 639 | /* Giving back a data block. */ |
| 640 | state->extravalid = 1; |
| 641 | state->extrablk.bp = curbp; |
| 642 | state->extrablk.index = di; |
| 643 | state->extrablk.blkno = curdb; |
| 644 | state->extrablk.magic = XFS_DIR2_DATA_MAGIC; |
| 645 | } else { |
| 646 | state->extravalid = 0; |
| 647 | } |
| 648 | /* |
| 649 | * Return the index, that will be the insertion point. |
| 650 | */ |
| 651 | *indexp = index; |
| 652 | return XFS_ERROR(error); |
| 653 | } |
| 654 | |
| 655 | /* |
| 656 | * Look up a leaf entry in a node-format leaf block. |
| 657 | * If this is an addname then the extrablk in state is a freespace block, |
| 658 | * otherwise it's a data block. |
| 659 | */ |
| 660 | int |
| 661 | xfs_dir2_leafn_lookup_int( |
| 662 | xfs_dabuf_t *bp, /* leaf buffer */ |
| 663 | xfs_da_args_t *args, /* operation arguments */ |
| 664 | int *indexp, /* out: leaf entry index */ |
| 665 | xfs_da_state_t *state) /* state to fill in */ |
| 666 | { |
| 667 | if (args->addname) |
| 668 | return xfs_dir2_leafn_lookup_for_addname(bp, args, indexp, |
| 669 | state); |
| 670 | return xfs_dir2_leafn_lookup_for_entry(bp, args, indexp, state); |
| 671 | } |
| 672 | |
| 673 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | * Move count leaf entries from source to destination leaf. |
| 675 | * Log entries and headers. Stale entries are preserved. |
| 676 | */ |
| 677 | static void |
| 678 | xfs_dir2_leafn_moveents( |
| 679 | xfs_da_args_t *args, /* operation arguments */ |
| 680 | xfs_dabuf_t *bp_s, /* source leaf buffer */ |
| 681 | int start_s, /* source leaf index */ |
| 682 | xfs_dabuf_t *bp_d, /* destination leaf buffer */ |
| 683 | int start_d, /* destination leaf index */ |
| 684 | int count) /* count of leaves to copy */ |
| 685 | { |
| 686 | xfs_dir2_leaf_t *leaf_d; /* destination leaf structure */ |
| 687 | xfs_dir2_leaf_t *leaf_s; /* source leaf structure */ |
| 688 | int stale; /* count stale leaves copied */ |
| 689 | xfs_trans_t *tp; /* transaction pointer */ |
| 690 | |
| 691 | xfs_dir2_trace_args_bibii("leafn_moveents", args, bp_s, start_s, bp_d, |
| 692 | start_d, count); |
| 693 | /* |
| 694 | * Silently return if nothing to do. |
| 695 | */ |
| 696 | if (count == 0) { |
| 697 | return; |
| 698 | } |
| 699 | tp = args->trans; |
| 700 | leaf_s = bp_s->data; |
| 701 | leaf_d = bp_d->data; |
| 702 | /* |
| 703 | * If the destination index is not the end of the current |
| 704 | * destination leaf entries, open up a hole in the destination |
| 705 | * to hold the new entries. |
| 706 | */ |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 707 | if (start_d < be16_to_cpu(leaf_d->hdr.count)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | memmove(&leaf_d->ents[start_d + count], &leaf_d->ents[start_d], |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 709 | (be16_to_cpu(leaf_d->hdr.count) - start_d) * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | sizeof(xfs_dir2_leaf_entry_t)); |
| 711 | xfs_dir2_leaf_log_ents(tp, bp_d, start_d + count, |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 712 | count + be16_to_cpu(leaf_d->hdr.count) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | } |
| 714 | /* |
| 715 | * If the source has stale leaves, count the ones in the copy range |
| 716 | * so we can update the header correctly. |
| 717 | */ |
| 718 | if (leaf_s->hdr.stale) { |
| 719 | int i; /* temp leaf index */ |
| 720 | |
| 721 | for (i = start_s, stale = 0; i < start_s + count; i++) { |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 722 | if (be32_to_cpu(leaf_s->ents[i].address) == XFS_DIR2_NULL_DATAPTR) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | stale++; |
| 724 | } |
| 725 | } else |
| 726 | stale = 0; |
| 727 | /* |
| 728 | * Copy the leaf entries from source to destination. |
| 729 | */ |
| 730 | memcpy(&leaf_d->ents[start_d], &leaf_s->ents[start_s], |
| 731 | count * sizeof(xfs_dir2_leaf_entry_t)); |
| 732 | xfs_dir2_leaf_log_ents(tp, bp_d, start_d, start_d + count - 1); |
| 733 | /* |
| 734 | * If there are source entries after the ones we copied, |
| 735 | * delete the ones we copied by sliding the next ones down. |
| 736 | */ |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 737 | if (start_s + count < be16_to_cpu(leaf_s->hdr.count)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | memmove(&leaf_s->ents[start_s], &leaf_s->ents[start_s + count], |
| 739 | count * sizeof(xfs_dir2_leaf_entry_t)); |
| 740 | xfs_dir2_leaf_log_ents(tp, bp_s, start_s, start_s + count - 1); |
| 741 | } |
| 742 | /* |
| 743 | * Update the headers and log them. |
| 744 | */ |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 745 | be16_add_cpu(&leaf_s->hdr.count, -(count)); |
| 746 | be16_add_cpu(&leaf_s->hdr.stale, -(stale)); |
| 747 | be16_add_cpu(&leaf_d->hdr.count, count); |
| 748 | be16_add_cpu(&leaf_d->hdr.stale, stale); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | xfs_dir2_leaf_log_header(tp, bp_s); |
| 750 | xfs_dir2_leaf_log_header(tp, bp_d); |
| 751 | xfs_dir2_leafn_check(args->dp, bp_s); |
| 752 | xfs_dir2_leafn_check(args->dp, bp_d); |
| 753 | } |
| 754 | |
| 755 | /* |
| 756 | * Determine the sort order of two leaf blocks. |
| 757 | * Returns 1 if both are valid and leaf2 should be before leaf1, else 0. |
| 758 | */ |
| 759 | int /* sort order */ |
| 760 | xfs_dir2_leafn_order( |
| 761 | xfs_dabuf_t *leaf1_bp, /* leaf1 buffer */ |
| 762 | xfs_dabuf_t *leaf2_bp) /* leaf2 buffer */ |
| 763 | { |
| 764 | xfs_dir2_leaf_t *leaf1; /* leaf1 structure */ |
| 765 | xfs_dir2_leaf_t *leaf2; /* leaf2 structure */ |
| 766 | |
| 767 | leaf1 = leaf1_bp->data; |
| 768 | leaf2 = leaf2_bp->data; |
Nathan Scott | 89da054 | 2006-03-17 17:28:40 +1100 | [diff] [blame] | 769 | ASSERT(be16_to_cpu(leaf1->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC); |
| 770 | ASSERT(be16_to_cpu(leaf2->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC); |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 771 | if (be16_to_cpu(leaf1->hdr.count) > 0 && |
| 772 | be16_to_cpu(leaf2->hdr.count) > 0 && |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 773 | (be32_to_cpu(leaf2->ents[0].hashval) < be32_to_cpu(leaf1->ents[0].hashval) || |
| 774 | be32_to_cpu(leaf2->ents[be16_to_cpu(leaf2->hdr.count) - 1].hashval) < |
| 775 | be32_to_cpu(leaf1->ents[be16_to_cpu(leaf1->hdr.count) - 1].hashval))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | return 1; |
| 777 | return 0; |
| 778 | } |
| 779 | |
| 780 | /* |
| 781 | * Rebalance leaf entries between two leaf blocks. |
| 782 | * This is actually only called when the second block is new, |
| 783 | * though the code deals with the general case. |
| 784 | * A new entry will be inserted in one of the blocks, and that |
| 785 | * entry is taken into account when balancing. |
| 786 | */ |
| 787 | static void |
| 788 | xfs_dir2_leafn_rebalance( |
| 789 | xfs_da_state_t *state, /* btree cursor */ |
| 790 | xfs_da_state_blk_t *blk1, /* first btree block */ |
| 791 | xfs_da_state_blk_t *blk2) /* second btree block */ |
| 792 | { |
| 793 | xfs_da_args_t *args; /* operation arguments */ |
| 794 | int count; /* count (& direction) leaves */ |
| 795 | int isleft; /* new goes in left leaf */ |
| 796 | xfs_dir2_leaf_t *leaf1; /* first leaf structure */ |
| 797 | xfs_dir2_leaf_t *leaf2; /* second leaf structure */ |
| 798 | int mid; /* midpoint leaf index */ |
| 799 | #ifdef DEBUG |
| 800 | int oldstale; /* old count of stale leaves */ |
| 801 | #endif |
| 802 | int oldsum; /* old total leaf count */ |
| 803 | int swap; /* swapped leaf blocks */ |
| 804 | |
| 805 | args = state->args; |
| 806 | /* |
| 807 | * If the block order is wrong, swap the arguments. |
| 808 | */ |
| 809 | if ((swap = xfs_dir2_leafn_order(blk1->bp, blk2->bp))) { |
| 810 | xfs_da_state_blk_t *tmp; /* temp for block swap */ |
| 811 | |
| 812 | tmp = blk1; |
| 813 | blk1 = blk2; |
| 814 | blk2 = tmp; |
| 815 | } |
| 816 | leaf1 = blk1->bp->data; |
| 817 | leaf2 = blk2->bp->data; |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 818 | oldsum = be16_to_cpu(leaf1->hdr.count) + be16_to_cpu(leaf2->hdr.count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | #ifdef DEBUG |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 820 | oldstale = be16_to_cpu(leaf1->hdr.stale) + be16_to_cpu(leaf2->hdr.stale); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | #endif |
| 822 | mid = oldsum >> 1; |
| 823 | /* |
| 824 | * If the old leaf count was odd then the new one will be even, |
| 825 | * so we need to divide the new count evenly. |
| 826 | */ |
| 827 | if (oldsum & 1) { |
| 828 | xfs_dahash_t midhash; /* middle entry hash value */ |
| 829 | |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 830 | if (mid >= be16_to_cpu(leaf1->hdr.count)) |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 831 | midhash = be32_to_cpu(leaf2->ents[mid - be16_to_cpu(leaf1->hdr.count)].hashval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | else |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 833 | midhash = be32_to_cpu(leaf1->ents[mid].hashval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | isleft = args->hashval <= midhash; |
| 835 | } |
| 836 | /* |
| 837 | * If the old count is even then the new count is odd, so there's |
| 838 | * no preferred side for the new entry. |
| 839 | * Pick the left one. |
| 840 | */ |
| 841 | else |
| 842 | isleft = 1; |
| 843 | /* |
| 844 | * Calculate moved entry count. Positive means left-to-right, |
| 845 | * negative means right-to-left. Then move the entries. |
| 846 | */ |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 847 | count = be16_to_cpu(leaf1->hdr.count) - mid + (isleft == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | if (count > 0) |
| 849 | xfs_dir2_leafn_moveents(args, blk1->bp, |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 850 | be16_to_cpu(leaf1->hdr.count) - count, blk2->bp, 0, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | else if (count < 0) |
| 852 | xfs_dir2_leafn_moveents(args, blk2->bp, 0, blk1->bp, |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 853 | be16_to_cpu(leaf1->hdr.count), count); |
| 854 | ASSERT(be16_to_cpu(leaf1->hdr.count) + be16_to_cpu(leaf2->hdr.count) == oldsum); |
| 855 | ASSERT(be16_to_cpu(leaf1->hdr.stale) + be16_to_cpu(leaf2->hdr.stale) == oldstale); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | /* |
| 857 | * Mark whether we're inserting into the old or new leaf. |
| 858 | */ |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 859 | if (be16_to_cpu(leaf1->hdr.count) < be16_to_cpu(leaf2->hdr.count)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | state->inleaf = swap; |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 861 | else if (be16_to_cpu(leaf1->hdr.count) > be16_to_cpu(leaf2->hdr.count)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | state->inleaf = !swap; |
| 863 | else |
| 864 | state->inleaf = |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 865 | swap ^ (blk1->index <= be16_to_cpu(leaf1->hdr.count)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | /* |
| 867 | * Adjust the expected index for insertion. |
| 868 | */ |
| 869 | if (!state->inleaf) |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 870 | blk2->index = blk1->index - be16_to_cpu(leaf1->hdr.count); |
Barry Naujok | f9f6dce | 2008-04-17 16:49:43 +1000 | [diff] [blame^] | 871 | |
| 872 | /* |
| 873 | * Finally sanity check just to make sure we are not returning a |
| 874 | * negative index |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | */ |
| 876 | if(blk2->index < 0) { |
| 877 | state->inleaf = 1; |
| 878 | blk2->index = 0; |
| 879 | cmn_err(CE_ALERT, |
Nathan Scott | c41564b | 2006-03-29 08:55:14 +1000 | [diff] [blame] | 880 | "xfs_dir2_leafn_rebalance: picked the wrong leaf? reverting original leaf: " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | "blk1->index %d\n", |
| 882 | blk1->index); |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | /* |
| 887 | * Remove an entry from a node directory. |
| 888 | * This removes the leaf entry and the data entry, |
| 889 | * and updates the free block if necessary. |
| 890 | */ |
| 891 | static int /* error */ |
| 892 | xfs_dir2_leafn_remove( |
| 893 | xfs_da_args_t *args, /* operation arguments */ |
| 894 | xfs_dabuf_t *bp, /* leaf buffer */ |
| 895 | int index, /* leaf entry index */ |
| 896 | xfs_da_state_blk_t *dblk, /* data block */ |
| 897 | int *rval) /* resulting block needs join */ |
| 898 | { |
| 899 | xfs_dir2_data_t *data; /* data block structure */ |
| 900 | xfs_dir2_db_t db; /* data block number */ |
| 901 | xfs_dabuf_t *dbp; /* data block buffer */ |
| 902 | xfs_dir2_data_entry_t *dep; /* data block entry */ |
| 903 | xfs_inode_t *dp; /* incore directory inode */ |
| 904 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 905 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ |
| 906 | int longest; /* longest data free entry */ |
| 907 | int off; /* data block entry offset */ |
| 908 | xfs_mount_t *mp; /* filesystem mount point */ |
| 909 | int needlog; /* need to log data header */ |
| 910 | int needscan; /* need to rescan data frees */ |
| 911 | xfs_trans_t *tp; /* transaction pointer */ |
| 912 | |
| 913 | xfs_dir2_trace_args_sb("leafn_remove", args, index, bp); |
| 914 | dp = args->dp; |
| 915 | tp = args->trans; |
| 916 | mp = dp->i_mount; |
| 917 | leaf = bp->data; |
Nathan Scott | 89da054 | 2006-03-17 17:28:40 +1100 | [diff] [blame] | 918 | ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | /* |
| 920 | * Point to the entry we're removing. |
| 921 | */ |
| 922 | lep = &leaf->ents[index]; |
| 923 | /* |
| 924 | * Extract the data block and offset from the entry. |
| 925 | */ |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 926 | db = xfs_dir2_dataptr_to_db(mp, be32_to_cpu(lep->address)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | ASSERT(dblk->blkno == db); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 928 | off = xfs_dir2_dataptr_to_off(mp, be32_to_cpu(lep->address)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | ASSERT(dblk->index == off); |
| 930 | /* |
| 931 | * Kill the leaf entry by marking it stale. |
| 932 | * Log the leaf block changes. |
| 933 | */ |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 934 | be16_add_cpu(&leaf->hdr.stale, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | xfs_dir2_leaf_log_header(tp, bp); |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 936 | lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | xfs_dir2_leaf_log_ents(tp, bp, index, index); |
| 938 | /* |
| 939 | * Make the data entry free. Keep track of the longest freespace |
| 940 | * in the data block in case it changes. |
| 941 | */ |
| 942 | dbp = dblk->bp; |
| 943 | data = dbp->data; |
| 944 | dep = (xfs_dir2_data_entry_t *)((char *)data + off); |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 945 | longest = be16_to_cpu(data->hdr.bestfree[0].length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | needlog = needscan = 0; |
| 947 | xfs_dir2_data_make_free(tp, dbp, off, |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 948 | xfs_dir2_data_entsize(dep->namelen), &needlog, &needscan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | /* |
| 950 | * Rescan the data block freespaces for bestfree. |
| 951 | * Log the data block header if needed. |
| 952 | */ |
| 953 | if (needscan) |
Eric Sandeen | ef497f8 | 2007-05-08 13:48:49 +1000 | [diff] [blame] | 954 | xfs_dir2_data_freescan(mp, data, &needlog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | if (needlog) |
| 956 | xfs_dir2_data_log_header(tp, dbp); |
| 957 | xfs_dir2_data_check(dp, dbp); |
| 958 | /* |
| 959 | * If the longest data block freespace changes, need to update |
| 960 | * the corresponding freeblock entry. |
| 961 | */ |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 962 | if (longest < be16_to_cpu(data->hdr.bestfree[0].length)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | int error; /* error return value */ |
| 964 | xfs_dabuf_t *fbp; /* freeblock buffer */ |
| 965 | xfs_dir2_db_t fdb; /* freeblock block number */ |
| 966 | int findex; /* index in freeblock entries */ |
| 967 | xfs_dir2_free_t *free; /* freeblock structure */ |
| 968 | int logfree; /* need to log free entry */ |
| 969 | |
| 970 | /* |
| 971 | * Convert the data block number to a free block, |
| 972 | * read in the free block. |
| 973 | */ |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 974 | fdb = xfs_dir2_db_to_fdb(mp, db); |
| 975 | if ((error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, fdb), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | -1, &fbp, XFS_DATA_FORK))) { |
| 977 | return error; |
| 978 | } |
| 979 | free = fbp->data; |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 980 | ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC); |
| 981 | ASSERT(be32_to_cpu(free->hdr.firstdb) == |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | XFS_DIR2_MAX_FREE_BESTS(mp) * |
| 983 | (fdb - XFS_DIR2_FREE_FIRSTDB(mp))); |
| 984 | /* |
| 985 | * Calculate which entry we need to fix. |
| 986 | */ |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 987 | findex = xfs_dir2_db_to_fdindex(mp, db); |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 988 | longest = be16_to_cpu(data->hdr.bestfree[0].length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | /* |
| 990 | * If the data block is now empty we can get rid of it |
| 991 | * (usually). |
| 992 | */ |
| 993 | if (longest == mp->m_dirblksize - (uint)sizeof(data->hdr)) { |
| 994 | /* |
| 995 | * Try to punch out the data block. |
| 996 | */ |
| 997 | error = xfs_dir2_shrink_inode(args, db, dbp); |
| 998 | if (error == 0) { |
| 999 | dblk->bp = NULL; |
| 1000 | data = NULL; |
| 1001 | } |
| 1002 | /* |
| 1003 | * We can get ENOSPC if there's no space reservation. |
| 1004 | * In this case just drop the buffer and some one else |
| 1005 | * will eventually get rid of the empty block. |
| 1006 | */ |
| 1007 | else if (error == ENOSPC && args->total == 0) |
| 1008 | xfs_da_buf_done(dbp); |
| 1009 | else |
| 1010 | return error; |
| 1011 | } |
| 1012 | /* |
| 1013 | * If we got rid of the data block, we can eliminate that entry |
| 1014 | * in the free block. |
| 1015 | */ |
| 1016 | if (data == NULL) { |
| 1017 | /* |
| 1018 | * One less used entry in the free table. |
| 1019 | */ |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 1020 | be32_add_cpu(&free->hdr.nused, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | xfs_dir2_free_log_header(tp, fbp); |
| 1022 | /* |
| 1023 | * If this was the last entry in the table, we can |
| 1024 | * trim the table size back. There might be other |
| 1025 | * entries at the end referring to non-existent |
| 1026 | * data blocks, get those too. |
| 1027 | */ |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 1028 | if (findex == be32_to_cpu(free->hdr.nvalid) - 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | int i; /* free entry index */ |
| 1030 | |
| 1031 | for (i = findex - 1; |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 1032 | i >= 0 && be16_to_cpu(free->bests[i]) == NULLDATAOFF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | i--) |
| 1034 | continue; |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 1035 | free->hdr.nvalid = cpu_to_be32(i + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | logfree = 0; |
| 1037 | } |
| 1038 | /* |
| 1039 | * Not the last entry, just punch it out. |
| 1040 | */ |
| 1041 | else { |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 1042 | free->bests[findex] = cpu_to_be16(NULLDATAOFF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | logfree = 1; |
| 1044 | } |
| 1045 | /* |
| 1046 | * If there are no useful entries left in the block, |
| 1047 | * get rid of the block if we can. |
| 1048 | */ |
| 1049 | if (!free->hdr.nused) { |
| 1050 | error = xfs_dir2_shrink_inode(args, fdb, fbp); |
| 1051 | if (error == 0) { |
| 1052 | fbp = NULL; |
| 1053 | logfree = 0; |
| 1054 | } else if (error != ENOSPC || args->total != 0) |
| 1055 | return error; |
| 1056 | /* |
| 1057 | * It's possible to get ENOSPC if there is no |
| 1058 | * space reservation. In this case some one |
| 1059 | * else will eventually get rid of this block. |
| 1060 | */ |
| 1061 | } |
| 1062 | } |
| 1063 | /* |
| 1064 | * Data block is not empty, just set the free entry to |
| 1065 | * the new value. |
| 1066 | */ |
| 1067 | else { |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 1068 | free->bests[findex] = cpu_to_be16(longest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | logfree = 1; |
| 1070 | } |
| 1071 | /* |
| 1072 | * Log the free entry that changed, unless we got rid of it. |
| 1073 | */ |
| 1074 | if (logfree) |
| 1075 | xfs_dir2_free_log_bests(tp, fbp, findex, findex); |
| 1076 | /* |
| 1077 | * Drop the buffer if we still have it. |
| 1078 | */ |
| 1079 | if (fbp) |
| 1080 | xfs_da_buf_done(fbp); |
| 1081 | } |
| 1082 | xfs_dir2_leafn_check(dp, bp); |
| 1083 | /* |
| 1084 | * Return indication of whether this leaf block is emtpy enough |
| 1085 | * to justify trying to join it with a neighbor. |
| 1086 | */ |
| 1087 | *rval = |
| 1088 | ((uint)sizeof(leaf->hdr) + |
| 1089 | (uint)sizeof(leaf->ents[0]) * |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 1090 | (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale))) < |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | mp->m_dir_magicpct; |
| 1092 | return 0; |
| 1093 | } |
| 1094 | |
| 1095 | /* |
| 1096 | * Split the leaf entries in the old block into old and new blocks. |
| 1097 | */ |
| 1098 | int /* error */ |
| 1099 | xfs_dir2_leafn_split( |
| 1100 | xfs_da_state_t *state, /* btree cursor */ |
| 1101 | xfs_da_state_blk_t *oldblk, /* original block */ |
| 1102 | xfs_da_state_blk_t *newblk) /* newly created block */ |
| 1103 | { |
| 1104 | xfs_da_args_t *args; /* operation arguments */ |
| 1105 | xfs_dablk_t blkno; /* new leaf block number */ |
| 1106 | int error; /* error return value */ |
| 1107 | xfs_mount_t *mp; /* filesystem mount point */ |
| 1108 | |
| 1109 | /* |
| 1110 | * Allocate space for a new leaf node. |
| 1111 | */ |
| 1112 | args = state->args; |
| 1113 | mp = args->dp->i_mount; |
| 1114 | ASSERT(args != NULL); |
| 1115 | ASSERT(oldblk->magic == XFS_DIR2_LEAFN_MAGIC); |
| 1116 | error = xfs_da_grow_inode(args, &blkno); |
| 1117 | if (error) { |
| 1118 | return error; |
| 1119 | } |
| 1120 | /* |
| 1121 | * Initialize the new leaf block. |
| 1122 | */ |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1123 | error = xfs_dir2_leaf_init(args, xfs_dir2_da_to_db(mp, blkno), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | &newblk->bp, XFS_DIR2_LEAFN_MAGIC); |
| 1125 | if (error) { |
| 1126 | return error; |
| 1127 | } |
| 1128 | newblk->blkno = blkno; |
| 1129 | newblk->magic = XFS_DIR2_LEAFN_MAGIC; |
| 1130 | /* |
| 1131 | * Rebalance the entries across the two leaves, link the new |
| 1132 | * block into the leaves. |
| 1133 | */ |
| 1134 | xfs_dir2_leafn_rebalance(state, oldblk, newblk); |
| 1135 | error = xfs_da_blk_link(state, oldblk, newblk); |
| 1136 | if (error) { |
| 1137 | return error; |
| 1138 | } |
| 1139 | /* |
| 1140 | * Insert the new entry in the correct block. |
| 1141 | */ |
| 1142 | if (state->inleaf) |
| 1143 | error = xfs_dir2_leafn_add(oldblk->bp, args, oldblk->index); |
| 1144 | else |
| 1145 | error = xfs_dir2_leafn_add(newblk->bp, args, newblk->index); |
| 1146 | /* |
| 1147 | * Update last hashval in each block since we added the name. |
| 1148 | */ |
| 1149 | oldblk->hashval = xfs_dir2_leafn_lasthash(oldblk->bp, NULL); |
| 1150 | newblk->hashval = xfs_dir2_leafn_lasthash(newblk->bp, NULL); |
| 1151 | xfs_dir2_leafn_check(args->dp, oldblk->bp); |
| 1152 | xfs_dir2_leafn_check(args->dp, newblk->bp); |
| 1153 | return error; |
| 1154 | } |
| 1155 | |
| 1156 | /* |
| 1157 | * Check a leaf block and its neighbors to see if the block should be |
| 1158 | * collapsed into one or the other neighbor. Always keep the block |
| 1159 | * with the smaller block number. |
| 1160 | * If the current block is over 50% full, don't try to join it, return 0. |
| 1161 | * If the block is empty, fill in the state structure and return 2. |
| 1162 | * If it can be collapsed, fill in the state structure and return 1. |
| 1163 | * If nothing can be done, return 0. |
| 1164 | */ |
| 1165 | int /* error */ |
| 1166 | xfs_dir2_leafn_toosmall( |
| 1167 | xfs_da_state_t *state, /* btree cursor */ |
| 1168 | int *action) /* resulting action to take */ |
| 1169 | { |
| 1170 | xfs_da_state_blk_t *blk; /* leaf block */ |
| 1171 | xfs_dablk_t blkno; /* leaf block number */ |
| 1172 | xfs_dabuf_t *bp; /* leaf buffer */ |
| 1173 | int bytes; /* bytes in use */ |
| 1174 | int count; /* leaf live entry count */ |
| 1175 | int error; /* error return value */ |
| 1176 | int forward; /* sibling block direction */ |
| 1177 | int i; /* sibling counter */ |
| 1178 | xfs_da_blkinfo_t *info; /* leaf block header */ |
| 1179 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 1180 | int rval; /* result from path_shift */ |
| 1181 | |
| 1182 | /* |
| 1183 | * Check for the degenerate case of the block being over 50% full. |
| 1184 | * If so, it's not worth even looking to see if we might be able |
| 1185 | * to coalesce with a sibling. |
| 1186 | */ |
| 1187 | blk = &state->path.blk[state->path.active - 1]; |
| 1188 | info = blk->bp->data; |
Nathan Scott | 89da054 | 2006-03-17 17:28:40 +1100 | [diff] [blame] | 1189 | ASSERT(be16_to_cpu(info->magic) == XFS_DIR2_LEAFN_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | leaf = (xfs_dir2_leaf_t *)info; |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 1191 | count = be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | bytes = (uint)sizeof(leaf->hdr) + count * (uint)sizeof(leaf->ents[0]); |
| 1193 | if (bytes > (state->blocksize >> 1)) { |
| 1194 | /* |
| 1195 | * Blk over 50%, don't try to join. |
| 1196 | */ |
| 1197 | *action = 0; |
| 1198 | return 0; |
| 1199 | } |
| 1200 | /* |
| 1201 | * Check for the degenerate case of the block being empty. |
| 1202 | * If the block is empty, we'll simply delete it, no need to |
| 1203 | * coalesce it with a sibling block. We choose (arbitrarily) |
| 1204 | * to merge with the forward block unless it is NULL. |
| 1205 | */ |
| 1206 | if (count == 0) { |
| 1207 | /* |
| 1208 | * Make altpath point to the block we want to keep and |
| 1209 | * path point to the block we want to drop (this one). |
| 1210 | */ |
Nathan Scott | 89da054 | 2006-03-17 17:28:40 +1100 | [diff] [blame] | 1211 | forward = (info->forw != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | memcpy(&state->altpath, &state->path, sizeof(state->path)); |
| 1213 | error = xfs_da_path_shift(state, &state->altpath, forward, 0, |
| 1214 | &rval); |
| 1215 | if (error) |
| 1216 | return error; |
| 1217 | *action = rval ? 2 : 0; |
| 1218 | return 0; |
| 1219 | } |
| 1220 | /* |
| 1221 | * Examine each sibling block to see if we can coalesce with |
| 1222 | * at least 25% free space to spare. We need to figure out |
| 1223 | * whether to merge with the forward or the backward block. |
| 1224 | * We prefer coalescing with the lower numbered sibling so as |
| 1225 | * to shrink a directory over time. |
| 1226 | */ |
Nathan Scott | 89da054 | 2006-03-17 17:28:40 +1100 | [diff] [blame] | 1227 | forward = be32_to_cpu(info->forw) < be32_to_cpu(info->back); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | for (i = 0, bp = NULL; i < 2; forward = !forward, i++) { |
Nathan Scott | 89da054 | 2006-03-17 17:28:40 +1100 | [diff] [blame] | 1229 | blkno = forward ? be32_to_cpu(info->forw) : be32_to_cpu(info->back); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | if (blkno == 0) |
| 1231 | continue; |
| 1232 | /* |
| 1233 | * Read the sibling leaf block. |
| 1234 | */ |
| 1235 | if ((error = |
| 1236 | xfs_da_read_buf(state->args->trans, state->args->dp, blkno, |
| 1237 | -1, &bp, XFS_DATA_FORK))) { |
| 1238 | return error; |
| 1239 | } |
| 1240 | ASSERT(bp != NULL); |
| 1241 | /* |
| 1242 | * Count bytes in the two blocks combined. |
| 1243 | */ |
| 1244 | leaf = (xfs_dir2_leaf_t *)info; |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 1245 | count = be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | bytes = state->blocksize - (state->blocksize >> 2); |
| 1247 | leaf = bp->data; |
Nathan Scott | 89da054 | 2006-03-17 17:28:40 +1100 | [diff] [blame] | 1248 | ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC); |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 1249 | count += be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | bytes -= count * (uint)sizeof(leaf->ents[0]); |
| 1251 | /* |
| 1252 | * Fits with at least 25% to spare. |
| 1253 | */ |
| 1254 | if (bytes >= 0) |
| 1255 | break; |
| 1256 | xfs_da_brelse(state->args->trans, bp); |
| 1257 | } |
| 1258 | /* |
| 1259 | * Didn't like either block, give up. |
| 1260 | */ |
| 1261 | if (i >= 2) { |
| 1262 | *action = 0; |
| 1263 | return 0; |
| 1264 | } |
| 1265 | /* |
| 1266 | * Done with the sibling leaf block here, drop the dabuf |
| 1267 | * so path_shift can get it. |
| 1268 | */ |
| 1269 | xfs_da_buf_done(bp); |
| 1270 | /* |
| 1271 | * Make altpath point to the block we want to keep (the lower |
| 1272 | * numbered block) and path point to the block we want to drop. |
| 1273 | */ |
| 1274 | memcpy(&state->altpath, &state->path, sizeof(state->path)); |
| 1275 | if (blkno < blk->blkno) |
| 1276 | error = xfs_da_path_shift(state, &state->altpath, forward, 0, |
| 1277 | &rval); |
| 1278 | else |
| 1279 | error = xfs_da_path_shift(state, &state->path, forward, 0, |
| 1280 | &rval); |
| 1281 | if (error) { |
| 1282 | return error; |
| 1283 | } |
| 1284 | *action = rval ? 0 : 1; |
| 1285 | return 0; |
| 1286 | } |
| 1287 | |
| 1288 | /* |
| 1289 | * Move all the leaf entries from drop_blk to save_blk. |
| 1290 | * This is done as part of a join operation. |
| 1291 | */ |
| 1292 | void |
| 1293 | xfs_dir2_leafn_unbalance( |
| 1294 | xfs_da_state_t *state, /* cursor */ |
| 1295 | xfs_da_state_blk_t *drop_blk, /* dead block */ |
| 1296 | xfs_da_state_blk_t *save_blk) /* surviving block */ |
| 1297 | { |
| 1298 | xfs_da_args_t *args; /* operation arguments */ |
| 1299 | xfs_dir2_leaf_t *drop_leaf; /* dead leaf structure */ |
| 1300 | xfs_dir2_leaf_t *save_leaf; /* surviving leaf structure */ |
| 1301 | |
| 1302 | args = state->args; |
| 1303 | ASSERT(drop_blk->magic == XFS_DIR2_LEAFN_MAGIC); |
| 1304 | ASSERT(save_blk->magic == XFS_DIR2_LEAFN_MAGIC); |
| 1305 | drop_leaf = drop_blk->bp->data; |
| 1306 | save_leaf = save_blk->bp->data; |
Nathan Scott | 89da054 | 2006-03-17 17:28:40 +1100 | [diff] [blame] | 1307 | ASSERT(be16_to_cpu(drop_leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC); |
| 1308 | ASSERT(be16_to_cpu(save_leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | /* |
| 1310 | * If there are any stale leaf entries, take this opportunity |
| 1311 | * to purge them. |
| 1312 | */ |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 1313 | if (drop_leaf->hdr.stale) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | xfs_dir2_leaf_compact(args, drop_blk->bp); |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 1315 | if (save_leaf->hdr.stale) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | xfs_dir2_leaf_compact(args, save_blk->bp); |
| 1317 | /* |
| 1318 | * Move the entries from drop to the appropriate end of save. |
| 1319 | */ |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 1320 | drop_blk->hashval = be32_to_cpu(drop_leaf->ents[be16_to_cpu(drop_leaf->hdr.count) - 1].hashval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | if (xfs_dir2_leafn_order(save_blk->bp, drop_blk->bp)) |
| 1322 | xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp, 0, |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 1323 | be16_to_cpu(drop_leaf->hdr.count)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | else |
| 1325 | xfs_dir2_leafn_moveents(args, drop_blk->bp, 0, save_blk->bp, |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 1326 | be16_to_cpu(save_leaf->hdr.count), be16_to_cpu(drop_leaf->hdr.count)); |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 1327 | save_blk->hashval = be32_to_cpu(save_leaf->ents[be16_to_cpu(save_leaf->hdr.count) - 1].hashval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1328 | xfs_dir2_leafn_check(args->dp, save_blk->bp); |
| 1329 | } |
| 1330 | |
| 1331 | /* |
| 1332 | * Top-level node form directory addname routine. |
| 1333 | */ |
| 1334 | int /* error */ |
| 1335 | xfs_dir2_node_addname( |
| 1336 | xfs_da_args_t *args) /* operation arguments */ |
| 1337 | { |
| 1338 | xfs_da_state_blk_t *blk; /* leaf block for insert */ |
| 1339 | int error; /* error return value */ |
| 1340 | int rval; /* sub-return value */ |
| 1341 | xfs_da_state_t *state; /* btree cursor */ |
| 1342 | |
| 1343 | xfs_dir2_trace_args("node_addname", args); |
| 1344 | /* |
| 1345 | * Allocate and initialize the state (btree cursor). |
| 1346 | */ |
| 1347 | state = xfs_da_state_alloc(); |
| 1348 | state->args = args; |
| 1349 | state->mp = args->dp->i_mount; |
| 1350 | state->blocksize = state->mp->m_dirblksize; |
| 1351 | state->node_ents = state->mp->m_dir_node_ents; |
| 1352 | /* |
| 1353 | * Look up the name. We're not supposed to find it, but |
| 1354 | * this gives us the insertion point. |
| 1355 | */ |
| 1356 | error = xfs_da_node_lookup_int(state, &rval); |
| 1357 | if (error) |
| 1358 | rval = error; |
| 1359 | if (rval != ENOENT) { |
| 1360 | goto done; |
| 1361 | } |
| 1362 | /* |
| 1363 | * Add the data entry to a data block. |
| 1364 | * Extravalid is set to a freeblock found by lookup. |
| 1365 | */ |
| 1366 | rval = xfs_dir2_node_addname_int(args, |
| 1367 | state->extravalid ? &state->extrablk : NULL); |
| 1368 | if (rval) { |
| 1369 | goto done; |
| 1370 | } |
| 1371 | blk = &state->path.blk[state->path.active - 1]; |
| 1372 | ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC); |
| 1373 | /* |
| 1374 | * Add the new leaf entry. |
| 1375 | */ |
| 1376 | rval = xfs_dir2_leafn_add(blk->bp, args, blk->index); |
| 1377 | if (rval == 0) { |
| 1378 | /* |
| 1379 | * It worked, fix the hash values up the btree. |
| 1380 | */ |
| 1381 | if (!args->justcheck) |
| 1382 | xfs_da_fixhashpath(state, &state->path); |
| 1383 | } else { |
| 1384 | /* |
| 1385 | * It didn't work, we need to split the leaf block. |
| 1386 | */ |
| 1387 | if (args->total == 0) { |
| 1388 | ASSERT(rval == ENOSPC); |
| 1389 | goto done; |
| 1390 | } |
| 1391 | /* |
| 1392 | * Split the leaf block and insert the new entry. |
| 1393 | */ |
| 1394 | rval = xfs_da_split(state); |
| 1395 | } |
| 1396 | done: |
| 1397 | xfs_da_state_free(state); |
| 1398 | return rval; |
| 1399 | } |
| 1400 | |
| 1401 | /* |
| 1402 | * Add the data entry for a node-format directory name addition. |
| 1403 | * The leaf entry is added in xfs_dir2_leafn_add. |
| 1404 | * We may enter with a freespace block that the lookup found. |
| 1405 | */ |
| 1406 | static int /* error */ |
| 1407 | xfs_dir2_node_addname_int( |
| 1408 | xfs_da_args_t *args, /* operation arguments */ |
| 1409 | xfs_da_state_blk_t *fblk) /* optional freespace block */ |
| 1410 | { |
| 1411 | xfs_dir2_data_t *data; /* data block structure */ |
| 1412 | xfs_dir2_db_t dbno; /* data block number */ |
| 1413 | xfs_dabuf_t *dbp; /* data block buffer */ |
| 1414 | xfs_dir2_data_entry_t *dep; /* data entry pointer */ |
| 1415 | xfs_inode_t *dp; /* incore directory inode */ |
| 1416 | xfs_dir2_data_unused_t *dup; /* data unused entry pointer */ |
| 1417 | int error; /* error return value */ |
| 1418 | xfs_dir2_db_t fbno; /* freespace block number */ |
| 1419 | xfs_dabuf_t *fbp; /* freespace buffer */ |
| 1420 | int findex; /* freespace entry index */ |
| 1421 | xfs_dir2_free_t *free=NULL; /* freespace block structure */ |
| 1422 | xfs_dir2_db_t ifbno; /* initial freespace block no */ |
| 1423 | xfs_dir2_db_t lastfbno=0; /* highest freespace block no */ |
| 1424 | int length; /* length of the new entry */ |
| 1425 | int logfree; /* need to log free entry */ |
| 1426 | xfs_mount_t *mp; /* filesystem mount point */ |
| 1427 | int needlog; /* need to log data header */ |
| 1428 | int needscan; /* need to rescan data frees */ |
Nathan Scott | 3d693c6 | 2006-03-17 17:28:27 +1100 | [diff] [blame] | 1429 | __be16 *tagp; /* data entry tag pointer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | xfs_trans_t *tp; /* transaction pointer */ |
| 1431 | |
| 1432 | dp = args->dp; |
| 1433 | mp = dp->i_mount; |
| 1434 | tp = args->trans; |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1435 | length = xfs_dir2_data_entsize(args->namelen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | /* |
| 1437 | * If we came in with a freespace block that means that lookup |
| 1438 | * found an entry with our hash value. This is the freespace |
| 1439 | * block for that data entry. |
| 1440 | */ |
| 1441 | if (fblk) { |
| 1442 | fbp = fblk->bp; |
| 1443 | /* |
| 1444 | * Remember initial freespace block number. |
| 1445 | */ |
| 1446 | ifbno = fblk->blkno; |
| 1447 | free = fbp->data; |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 1448 | ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | findex = fblk->index; |
| 1450 | /* |
| 1451 | * This means the free entry showed that the data block had |
| 1452 | * space for our entry, so we remembered it. |
| 1453 | * Use that data block. |
| 1454 | */ |
| 1455 | if (findex >= 0) { |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 1456 | ASSERT(findex < be32_to_cpu(free->hdr.nvalid)); |
| 1457 | ASSERT(be16_to_cpu(free->bests[findex]) != NULLDATAOFF); |
| 1458 | ASSERT(be16_to_cpu(free->bests[findex]) >= length); |
| 1459 | dbno = be32_to_cpu(free->hdr.firstdb) + findex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1460 | } |
| 1461 | /* |
| 1462 | * The data block looked at didn't have enough room. |
| 1463 | * We'll start at the beginning of the freespace entries. |
| 1464 | */ |
| 1465 | else { |
| 1466 | dbno = -1; |
| 1467 | findex = 0; |
| 1468 | } |
| 1469 | } |
| 1470 | /* |
| 1471 | * Didn't come in with a freespace block, so don't have a data block. |
| 1472 | */ |
| 1473 | else { |
| 1474 | ifbno = dbno = -1; |
| 1475 | fbp = NULL; |
| 1476 | findex = 0; |
| 1477 | } |
| 1478 | /* |
| 1479 | * If we don't have a data block yet, we're going to scan the |
| 1480 | * freespace blocks looking for one. Figure out what the |
| 1481 | * highest freespace block number is. |
| 1482 | */ |
| 1483 | if (dbno == -1) { |
| 1484 | xfs_fileoff_t fo; /* freespace block number */ |
| 1485 | |
| 1486 | if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK))) |
| 1487 | return error; |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1488 | lastfbno = xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1489 | fbno = ifbno; |
| 1490 | } |
| 1491 | /* |
| 1492 | * While we haven't identified a data block, search the freeblock |
| 1493 | * data for a good data block. If we find a null freeblock entry, |
| 1494 | * indicating a hole in the data blocks, remember that. |
| 1495 | */ |
| 1496 | while (dbno == -1) { |
| 1497 | /* |
| 1498 | * If we don't have a freeblock in hand, get the next one. |
| 1499 | */ |
| 1500 | if (fbp == NULL) { |
| 1501 | /* |
| 1502 | * Happens the first time through unless lookup gave |
| 1503 | * us a freespace block to start with. |
| 1504 | */ |
| 1505 | if (++fbno == 0) |
| 1506 | fbno = XFS_DIR2_FREE_FIRSTDB(mp); |
| 1507 | /* |
| 1508 | * If it's ifbno we already looked at it. |
| 1509 | */ |
| 1510 | if (fbno == ifbno) |
| 1511 | fbno++; |
| 1512 | /* |
| 1513 | * If it's off the end we're done. |
| 1514 | */ |
| 1515 | if (fbno >= lastfbno) |
| 1516 | break; |
| 1517 | /* |
| 1518 | * Read the block. There can be holes in the |
| 1519 | * freespace blocks, so this might not succeed. |
| 1520 | * This should be really rare, so there's no reason |
| 1521 | * to avoid it. |
| 1522 | */ |
| 1523 | if ((error = xfs_da_read_buf(tp, dp, |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1524 | xfs_dir2_db_to_da(mp, fbno), -2, &fbp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | XFS_DATA_FORK))) { |
| 1526 | return error; |
| 1527 | } |
| 1528 | if (unlikely(fbp == NULL)) { |
| 1529 | continue; |
| 1530 | } |
| 1531 | free = fbp->data; |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 1532 | ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | findex = 0; |
| 1534 | } |
| 1535 | /* |
| 1536 | * Look at the current free entry. Is it good enough? |
| 1537 | */ |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 1538 | if (be16_to_cpu(free->bests[findex]) != NULLDATAOFF && |
| 1539 | be16_to_cpu(free->bests[findex]) >= length) |
| 1540 | dbno = be32_to_cpu(free->hdr.firstdb) + findex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1541 | else { |
| 1542 | /* |
| 1543 | * Are we done with the freeblock? |
| 1544 | */ |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 1545 | if (++findex == be32_to_cpu(free->hdr.nvalid)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | /* |
| 1547 | * Drop the block. |
| 1548 | */ |
| 1549 | xfs_da_brelse(tp, fbp); |
| 1550 | fbp = NULL; |
| 1551 | if (fblk && fblk->bp) |
| 1552 | fblk->bp = NULL; |
| 1553 | } |
| 1554 | } |
| 1555 | } |
| 1556 | /* |
| 1557 | * If we don't have a data block, we need to allocate one and make |
| 1558 | * the freespace entries refer to it. |
| 1559 | */ |
| 1560 | if (unlikely(dbno == -1)) { |
| 1561 | /* |
| 1562 | * Not allowed to allocate, return failure. |
| 1563 | */ |
| 1564 | if (args->justcheck || args->total == 0) { |
| 1565 | /* |
| 1566 | * Drop the freespace buffer unless it came from our |
| 1567 | * caller. |
| 1568 | */ |
| 1569 | if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL) |
| 1570 | xfs_da_buf_done(fbp); |
| 1571 | return XFS_ERROR(ENOSPC); |
| 1572 | } |
| 1573 | /* |
| 1574 | * Allocate and initialize the new data block. |
| 1575 | */ |
| 1576 | if (unlikely((error = xfs_dir2_grow_inode(args, |
| 1577 | XFS_DIR2_DATA_SPACE, |
| 1578 | &dbno)) || |
| 1579 | (error = xfs_dir2_data_init(args, dbno, &dbp)))) { |
| 1580 | /* |
| 1581 | * Drop the freespace buffer unless it came from our |
| 1582 | * caller. |
| 1583 | */ |
| 1584 | if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL) |
| 1585 | xfs_da_buf_done(fbp); |
| 1586 | return error; |
| 1587 | } |
| 1588 | /* |
| 1589 | * If (somehow) we have a freespace block, get rid of it. |
| 1590 | */ |
| 1591 | if (fbp) |
| 1592 | xfs_da_brelse(tp, fbp); |
| 1593 | if (fblk && fblk->bp) |
| 1594 | fblk->bp = NULL; |
| 1595 | |
| 1596 | /* |
| 1597 | * Get the freespace block corresponding to the data block |
| 1598 | * that was just allocated. |
| 1599 | */ |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1600 | fbno = xfs_dir2_db_to_fdb(mp, dbno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | if (unlikely(error = xfs_da_read_buf(tp, dp, |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1602 | xfs_dir2_db_to_da(mp, fbno), -2, &fbp, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1603 | XFS_DATA_FORK))) { |
| 1604 | xfs_da_buf_done(dbp); |
| 1605 | return error; |
| 1606 | } |
| 1607 | /* |
| 1608 | * If there wasn't a freespace block, the read will |
| 1609 | * return a NULL fbp. Allocate and initialize a new one. |
| 1610 | */ |
| 1611 | if( fbp == NULL ) { |
| 1612 | if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_FREE_SPACE, |
| 1613 | &fbno))) { |
| 1614 | return error; |
| 1615 | } |
| 1616 | |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1617 | if (unlikely(xfs_dir2_db_to_fdb(mp, dbno) != fbno)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1618 | cmn_err(CE_ALERT, |
| 1619 | "xfs_dir2_node_addname_int: dir ino " |
| 1620 | "%llu needed freesp block %lld for\n" |
| 1621 | " data block %lld, got %lld\n" |
| 1622 | " ifbno %llu lastfbno %d\n", |
| 1623 | (unsigned long long)dp->i_ino, |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1624 | (long long)xfs_dir2_db_to_fdb(mp, dbno), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1625 | (long long)dbno, (long long)fbno, |
| 1626 | (unsigned long long)ifbno, lastfbno); |
| 1627 | if (fblk) { |
| 1628 | cmn_err(CE_ALERT, |
| 1629 | " fblk 0x%p blkno %llu " |
| 1630 | "index %d magic 0x%x\n", |
| 1631 | fblk, |
| 1632 | (unsigned long long)fblk->blkno, |
| 1633 | fblk->index, |
| 1634 | fblk->magic); |
| 1635 | } else { |
| 1636 | cmn_err(CE_ALERT, |
| 1637 | " ... fblk is NULL\n"); |
| 1638 | } |
| 1639 | XFS_ERROR_REPORT("xfs_dir2_node_addname_int", |
| 1640 | XFS_ERRLEVEL_LOW, mp); |
| 1641 | return XFS_ERROR(EFSCORRUPTED); |
| 1642 | } |
| 1643 | |
| 1644 | /* |
| 1645 | * Get a buffer for the new block. |
| 1646 | */ |
| 1647 | if ((error = xfs_da_get_buf(tp, dp, |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1648 | xfs_dir2_db_to_da(mp, fbno), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1649 | -1, &fbp, XFS_DATA_FORK))) { |
| 1650 | return error; |
| 1651 | } |
| 1652 | ASSERT(fbp != NULL); |
| 1653 | |
| 1654 | /* |
| 1655 | * Initialize the new block to be empty, and remember |
| 1656 | * its first slot as our empty slot. |
| 1657 | */ |
| 1658 | free = fbp->data; |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 1659 | free->hdr.magic = cpu_to_be32(XFS_DIR2_FREE_MAGIC); |
| 1660 | free->hdr.firstdb = cpu_to_be32( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | (fbno - XFS_DIR2_FREE_FIRSTDB(mp)) * |
| 1662 | XFS_DIR2_MAX_FREE_BESTS(mp)); |
| 1663 | free->hdr.nvalid = 0; |
| 1664 | free->hdr.nused = 0; |
| 1665 | } else { |
| 1666 | free = fbp->data; |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 1667 | ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | } |
| 1669 | |
| 1670 | /* |
| 1671 | * Set the freespace block index from the data block number. |
| 1672 | */ |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1673 | findex = xfs_dir2_db_to_fdindex(mp, dbno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1674 | /* |
| 1675 | * If it's after the end of the current entries in the |
| 1676 | * freespace block, extend that table. |
| 1677 | */ |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 1678 | if (findex >= be32_to_cpu(free->hdr.nvalid)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1679 | ASSERT(findex < XFS_DIR2_MAX_FREE_BESTS(mp)); |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 1680 | free->hdr.nvalid = cpu_to_be32(findex + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | /* |
| 1682 | * Tag new entry so nused will go up. |
| 1683 | */ |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 1684 | free->bests[findex] = cpu_to_be16(NULLDATAOFF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1685 | } |
| 1686 | /* |
| 1687 | * If this entry was for an empty data block |
| 1688 | * (this should always be true) then update the header. |
| 1689 | */ |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 1690 | if (be16_to_cpu(free->bests[findex]) == NULLDATAOFF) { |
Marcin Slusarz | 413d57c | 2008-02-13 15:03:29 -0800 | [diff] [blame] | 1691 | be32_add_cpu(&free->hdr.nused, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1692 | xfs_dir2_free_log_header(tp, fbp); |
| 1693 | } |
| 1694 | /* |
| 1695 | * Update the real value in the table. |
| 1696 | * We haven't allocated the data entry yet so this will |
| 1697 | * change again. |
| 1698 | */ |
| 1699 | data = dbp->data; |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 1700 | free->bests[findex] = data->hdr.bestfree[0].length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | logfree = 1; |
| 1702 | } |
| 1703 | /* |
| 1704 | * We had a data block so we don't have to make a new one. |
| 1705 | */ |
| 1706 | else { |
| 1707 | /* |
| 1708 | * If just checking, we succeeded. |
| 1709 | */ |
| 1710 | if (args->justcheck) { |
| 1711 | if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL) |
| 1712 | xfs_da_buf_done(fbp); |
| 1713 | return 0; |
| 1714 | } |
| 1715 | /* |
| 1716 | * Read the data block in. |
| 1717 | */ |
| 1718 | if (unlikely( |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1719 | error = xfs_da_read_buf(tp, dp, xfs_dir2_db_to_da(mp, dbno), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | -1, &dbp, XFS_DATA_FORK))) { |
| 1721 | if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL) |
| 1722 | xfs_da_buf_done(fbp); |
| 1723 | return error; |
| 1724 | } |
| 1725 | data = dbp->data; |
| 1726 | logfree = 0; |
| 1727 | } |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 1728 | ASSERT(be16_to_cpu(data->hdr.bestfree[0].length) >= length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1729 | /* |
| 1730 | * Point to the existing unused space. |
| 1731 | */ |
| 1732 | dup = (xfs_dir2_data_unused_t *) |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 1733 | ((char *)data + be16_to_cpu(data->hdr.bestfree[0].offset)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | needscan = needlog = 0; |
| 1735 | /* |
| 1736 | * Mark the first part of the unused space, inuse for us. |
| 1737 | */ |
| 1738 | xfs_dir2_data_use_free(tp, dbp, dup, |
| 1739 | (xfs_dir2_data_aoff_t)((char *)dup - (char *)data), length, |
| 1740 | &needlog, &needscan); |
| 1741 | /* |
| 1742 | * Fill in the new entry and log it. |
| 1743 | */ |
| 1744 | dep = (xfs_dir2_data_entry_t *)dup; |
Christoph Hellwig | ff9901c | 2006-06-09 14:48:37 +1000 | [diff] [blame] | 1745 | dep->inumber = cpu_to_be64(args->inumber); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | dep->namelen = args->namelen; |
| 1747 | memcpy(dep->name, args->name, dep->namelen); |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1748 | tagp = xfs_dir2_data_entry_tag_p(dep); |
Nathan Scott | 3d693c6 | 2006-03-17 17:28:27 +1100 | [diff] [blame] | 1749 | *tagp = cpu_to_be16((char *)dep - (char *)data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1750 | xfs_dir2_data_log_entry(tp, dbp, dep); |
| 1751 | /* |
| 1752 | * Rescan the block for bestfree if needed. |
| 1753 | */ |
| 1754 | if (needscan) |
Eric Sandeen | ef497f8 | 2007-05-08 13:48:49 +1000 | [diff] [blame] | 1755 | xfs_dir2_data_freescan(mp, data, &needlog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1756 | /* |
| 1757 | * Log the data block header if needed. |
| 1758 | */ |
| 1759 | if (needlog) |
| 1760 | xfs_dir2_data_log_header(tp, dbp); |
| 1761 | /* |
| 1762 | * If the freespace entry is now wrong, update it. |
| 1763 | */ |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 1764 | if (be16_to_cpu(free->bests[findex]) != be16_to_cpu(data->hdr.bestfree[0].length)) { |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 1765 | free->bests[findex] = data->hdr.bestfree[0].length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | logfree = 1; |
| 1767 | } |
| 1768 | /* |
| 1769 | * Log the freespace entry if needed. |
| 1770 | */ |
| 1771 | if (logfree) |
| 1772 | xfs_dir2_free_log_bests(tp, fbp, findex, findex); |
| 1773 | /* |
| 1774 | * If the caller didn't hand us the freespace block, drop it. |
| 1775 | */ |
| 1776 | if ((fblk == NULL || fblk->bp == NULL) && fbp != NULL) |
| 1777 | xfs_da_buf_done(fbp); |
| 1778 | /* |
| 1779 | * Return the data block and offset in args, then drop the data block. |
| 1780 | */ |
| 1781 | args->blkno = (xfs_dablk_t)dbno; |
Nathan Scott | 3d693c6 | 2006-03-17 17:28:27 +1100 | [diff] [blame] | 1782 | args->index = be16_to_cpu(*tagp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1783 | xfs_da_buf_done(dbp); |
| 1784 | return 0; |
| 1785 | } |
| 1786 | |
| 1787 | /* |
| 1788 | * Lookup an entry in a node-format directory. |
| 1789 | * All the real work happens in xfs_da_node_lookup_int. |
| 1790 | * The only real output is the inode number of the entry. |
| 1791 | */ |
| 1792 | int /* error */ |
| 1793 | xfs_dir2_node_lookup( |
| 1794 | xfs_da_args_t *args) /* operation arguments */ |
| 1795 | { |
| 1796 | int error; /* error return value */ |
| 1797 | int i; /* btree level */ |
| 1798 | int rval; /* operation return value */ |
| 1799 | xfs_da_state_t *state; /* btree cursor */ |
| 1800 | |
| 1801 | xfs_dir2_trace_args("node_lookup", args); |
| 1802 | /* |
| 1803 | * Allocate and initialize the btree cursor. |
| 1804 | */ |
| 1805 | state = xfs_da_state_alloc(); |
| 1806 | state->args = args; |
| 1807 | state->mp = args->dp->i_mount; |
| 1808 | state->blocksize = state->mp->m_dirblksize; |
| 1809 | state->node_ents = state->mp->m_dir_node_ents; |
| 1810 | /* |
| 1811 | * Fill in the path to the entry in the cursor. |
| 1812 | */ |
| 1813 | error = xfs_da_node_lookup_int(state, &rval); |
| 1814 | if (error) |
| 1815 | rval = error; |
| 1816 | /* |
| 1817 | * Release the btree blocks and leaf block. |
| 1818 | */ |
| 1819 | for (i = 0; i < state->path.active; i++) { |
| 1820 | xfs_da_brelse(args->trans, state->path.blk[i].bp); |
| 1821 | state->path.blk[i].bp = NULL; |
| 1822 | } |
| 1823 | /* |
| 1824 | * Release the data block if we have it. |
| 1825 | */ |
| 1826 | if (state->extravalid && state->extrablk.bp) { |
| 1827 | xfs_da_brelse(args->trans, state->extrablk.bp); |
| 1828 | state->extrablk.bp = NULL; |
| 1829 | } |
| 1830 | xfs_da_state_free(state); |
| 1831 | return rval; |
| 1832 | } |
| 1833 | |
| 1834 | /* |
| 1835 | * Remove an entry from a node-format directory. |
| 1836 | */ |
| 1837 | int /* error */ |
| 1838 | xfs_dir2_node_removename( |
| 1839 | xfs_da_args_t *args) /* operation arguments */ |
| 1840 | { |
| 1841 | xfs_da_state_blk_t *blk; /* leaf block */ |
| 1842 | int error; /* error return value */ |
| 1843 | int rval; /* operation return value */ |
| 1844 | xfs_da_state_t *state; /* btree cursor */ |
| 1845 | |
| 1846 | xfs_dir2_trace_args("node_removename", args); |
| 1847 | /* |
| 1848 | * Allocate and initialize the btree cursor. |
| 1849 | */ |
| 1850 | state = xfs_da_state_alloc(); |
| 1851 | state->args = args; |
| 1852 | state->mp = args->dp->i_mount; |
| 1853 | state->blocksize = state->mp->m_dirblksize; |
| 1854 | state->node_ents = state->mp->m_dir_node_ents; |
| 1855 | /* |
| 1856 | * Look up the entry we're deleting, set up the cursor. |
| 1857 | */ |
| 1858 | error = xfs_da_node_lookup_int(state, &rval); |
| 1859 | if (error) { |
| 1860 | rval = error; |
| 1861 | } |
| 1862 | /* |
| 1863 | * Didn't find it, upper layer screwed up. |
| 1864 | */ |
| 1865 | if (rval != EEXIST) { |
| 1866 | xfs_da_state_free(state); |
| 1867 | return rval; |
| 1868 | } |
| 1869 | blk = &state->path.blk[state->path.active - 1]; |
| 1870 | ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC); |
| 1871 | ASSERT(state->extravalid); |
| 1872 | /* |
| 1873 | * Remove the leaf and data entries. |
| 1874 | * Extrablk refers to the data block. |
| 1875 | */ |
| 1876 | error = xfs_dir2_leafn_remove(args, blk->bp, blk->index, |
| 1877 | &state->extrablk, &rval); |
| 1878 | if (error) { |
| 1879 | return error; |
| 1880 | } |
| 1881 | /* |
| 1882 | * Fix the hash values up the btree. |
| 1883 | */ |
| 1884 | xfs_da_fixhashpath(state, &state->path); |
| 1885 | /* |
| 1886 | * If we need to join leaf blocks, do it. |
| 1887 | */ |
| 1888 | if (rval && state->path.active > 1) |
| 1889 | error = xfs_da_join(state); |
| 1890 | /* |
| 1891 | * If no errors so far, try conversion to leaf format. |
| 1892 | */ |
| 1893 | if (!error) |
| 1894 | error = xfs_dir2_node_to_leaf(state); |
| 1895 | xfs_da_state_free(state); |
| 1896 | return error; |
| 1897 | } |
| 1898 | |
| 1899 | /* |
| 1900 | * Replace an entry's inode number in a node-format directory. |
| 1901 | */ |
| 1902 | int /* error */ |
| 1903 | xfs_dir2_node_replace( |
| 1904 | xfs_da_args_t *args) /* operation arguments */ |
| 1905 | { |
| 1906 | xfs_da_state_blk_t *blk; /* leaf block */ |
| 1907 | xfs_dir2_data_t *data; /* data block structure */ |
| 1908 | xfs_dir2_data_entry_t *dep; /* data entry changed */ |
| 1909 | int error; /* error return value */ |
| 1910 | int i; /* btree level */ |
| 1911 | xfs_ino_t inum; /* new inode number */ |
| 1912 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 1913 | xfs_dir2_leaf_entry_t *lep; /* leaf entry being changed */ |
| 1914 | int rval; /* internal return value */ |
| 1915 | xfs_da_state_t *state; /* btree cursor */ |
| 1916 | |
| 1917 | xfs_dir2_trace_args("node_replace", args); |
| 1918 | /* |
| 1919 | * Allocate and initialize the btree cursor. |
| 1920 | */ |
| 1921 | state = xfs_da_state_alloc(); |
| 1922 | state->args = args; |
| 1923 | state->mp = args->dp->i_mount; |
| 1924 | state->blocksize = state->mp->m_dirblksize; |
| 1925 | state->node_ents = state->mp->m_dir_node_ents; |
| 1926 | inum = args->inumber; |
| 1927 | /* |
| 1928 | * Lookup the entry to change in the btree. |
| 1929 | */ |
| 1930 | error = xfs_da_node_lookup_int(state, &rval); |
| 1931 | if (error) { |
| 1932 | rval = error; |
| 1933 | } |
| 1934 | /* |
| 1935 | * It should be found, since the vnodeops layer has looked it up |
| 1936 | * and locked it. But paranoia is good. |
| 1937 | */ |
| 1938 | if (rval == EEXIST) { |
| 1939 | /* |
| 1940 | * Find the leaf entry. |
| 1941 | */ |
| 1942 | blk = &state->path.blk[state->path.active - 1]; |
| 1943 | ASSERT(blk->magic == XFS_DIR2_LEAFN_MAGIC); |
| 1944 | leaf = blk->bp->data; |
| 1945 | lep = &leaf->ents[blk->index]; |
| 1946 | ASSERT(state->extravalid); |
| 1947 | /* |
| 1948 | * Point to the data entry. |
| 1949 | */ |
| 1950 | data = state->extrablk.bp->data; |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 1951 | ASSERT(be32_to_cpu(data->hdr.magic) == XFS_DIR2_DATA_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1952 | dep = (xfs_dir2_data_entry_t *) |
| 1953 | ((char *)data + |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 1954 | xfs_dir2_dataptr_to_off(state->mp, be32_to_cpu(lep->address))); |
Christoph Hellwig | ff9901c | 2006-06-09 14:48:37 +1000 | [diff] [blame] | 1955 | ASSERT(inum != be64_to_cpu(dep->inumber)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1956 | /* |
| 1957 | * Fill in the new inode number and log the entry. |
| 1958 | */ |
Christoph Hellwig | ff9901c | 2006-06-09 14:48:37 +1000 | [diff] [blame] | 1959 | dep->inumber = cpu_to_be64(inum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1960 | xfs_dir2_data_log_entry(args->trans, state->extrablk.bp, dep); |
| 1961 | rval = 0; |
| 1962 | } |
| 1963 | /* |
| 1964 | * Didn't find it, and we're holding a data block. Drop it. |
| 1965 | */ |
| 1966 | else if (state->extravalid) { |
| 1967 | xfs_da_brelse(args->trans, state->extrablk.bp); |
| 1968 | state->extrablk.bp = NULL; |
| 1969 | } |
| 1970 | /* |
| 1971 | * Release all the buffers in the cursor. |
| 1972 | */ |
| 1973 | for (i = 0; i < state->path.active; i++) { |
| 1974 | xfs_da_brelse(args->trans, state->path.blk[i].bp); |
| 1975 | state->path.blk[i].bp = NULL; |
| 1976 | } |
| 1977 | xfs_da_state_free(state); |
| 1978 | return rval; |
| 1979 | } |
| 1980 | |
| 1981 | /* |
| 1982 | * Trim off a trailing empty freespace block. |
| 1983 | * Return (in rvalp) 1 if we did it, 0 if not. |
| 1984 | */ |
| 1985 | int /* error */ |
| 1986 | xfs_dir2_node_trim_free( |
| 1987 | xfs_da_args_t *args, /* operation arguments */ |
| 1988 | xfs_fileoff_t fo, /* free block number */ |
| 1989 | int *rvalp) /* out: did something */ |
| 1990 | { |
| 1991 | xfs_dabuf_t *bp; /* freespace buffer */ |
| 1992 | xfs_inode_t *dp; /* incore directory inode */ |
| 1993 | int error; /* error return code */ |
| 1994 | xfs_dir2_free_t *free; /* freespace structure */ |
| 1995 | xfs_mount_t *mp; /* filesystem mount point */ |
| 1996 | xfs_trans_t *tp; /* transaction pointer */ |
| 1997 | |
| 1998 | dp = args->dp; |
| 1999 | mp = dp->i_mount; |
| 2000 | tp = args->trans; |
| 2001 | /* |
| 2002 | * Read the freespace block. |
| 2003 | */ |
| 2004 | if (unlikely(error = xfs_da_read_buf(tp, dp, (xfs_dablk_t)fo, -2, &bp, |
| 2005 | XFS_DATA_FORK))) { |
| 2006 | return error; |
| 2007 | } |
| 2008 | |
| 2009 | /* |
| 2010 | * There can be holes in freespace. If fo is a hole, there's |
| 2011 | * nothing to do. |
| 2012 | */ |
| 2013 | if (bp == NULL) { |
| 2014 | return 0; |
| 2015 | } |
| 2016 | free = bp->data; |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 2017 | ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2018 | /* |
| 2019 | * If there are used entries, there's nothing to do. |
| 2020 | */ |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 2021 | if (be32_to_cpu(free->hdr.nused) > 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2022 | xfs_da_brelse(tp, bp); |
| 2023 | *rvalp = 0; |
| 2024 | return 0; |
| 2025 | } |
| 2026 | /* |
| 2027 | * Blow the block away. |
| 2028 | */ |
| 2029 | if ((error = |
Christoph Hellwig | bbaaf53 | 2007-06-28 16:43:50 +1000 | [diff] [blame] | 2030 | xfs_dir2_shrink_inode(args, xfs_dir2_da_to_db(mp, (xfs_dablk_t)fo), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2031 | bp))) { |
| 2032 | /* |
| 2033 | * Can't fail with ENOSPC since that only happens with no |
| 2034 | * space reservation, when breaking up an extent into two |
| 2035 | * pieces. This is the last block of an extent. |
| 2036 | */ |
| 2037 | ASSERT(error != ENOSPC); |
| 2038 | xfs_da_brelse(tp, bp); |
| 2039 | return error; |
| 2040 | } |
| 2041 | /* |
| 2042 | * Return that we succeeded. |
| 2043 | */ |
| 2044 | *rvalp = 1; |
| 2045 | return 0; |
| 2046 | } |