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