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-2003,2005 Silicon Graphics, Inc. |
| 3 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License as |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * published by the Free Software Foundation. |
| 8 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 9 | * This program is distributed in the hope that it would be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write the Free Software Foundation, |
| 16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "xfs.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 19 | #include "xfs_fs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include "xfs_types.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 21 | #include "xfs_bit.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include "xfs_log.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 23 | #include "xfs_inum.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "xfs_trans.h" |
| 25 | #include "xfs_sb.h" |
| 26 | #include "xfs_ag.h" |
| 27 | #include "xfs_dir.h" |
| 28 | #include "xfs_dir2.h" |
| 29 | #include "xfs_dmapi.h" |
| 30 | #include "xfs_mount.h" |
Nathan Scott | a844f45 | 2005-11-02 14:38:42 +1100 | [diff] [blame] | 31 | #include "xfs_da_btree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include "xfs_bmap_btree.h" |
| 33 | #include "xfs_attr_sf.h" |
| 34 | #include "xfs_dir_sf.h" |
| 35 | #include "xfs_dir2_sf.h" |
| 36 | #include "xfs_dinode.h" |
| 37 | #include "xfs_inode.h" |
| 38 | #include "xfs_bmap.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include "xfs_dir2_data.h" |
| 40 | #include "xfs_dir2_leaf.h" |
| 41 | #include "xfs_dir2_block.h" |
| 42 | #include "xfs_dir2_node.h" |
| 43 | #include "xfs_dir2_trace.h" |
| 44 | #include "xfs_error.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
| 46 | /* |
| 47 | * Local function declarations. |
| 48 | */ |
| 49 | #ifdef DEBUG |
| 50 | static void xfs_dir2_leaf_check(xfs_inode_t *dp, xfs_dabuf_t *bp); |
| 51 | #else |
| 52 | #define xfs_dir2_leaf_check(dp, bp) |
| 53 | #endif |
| 54 | static int xfs_dir2_leaf_lookup_int(xfs_da_args_t *args, xfs_dabuf_t **lbpp, |
| 55 | int *indexp, xfs_dabuf_t **dbpp); |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 56 | static void xfs_dir2_leaf_log_bests(struct xfs_trans *tp, struct xfs_dabuf *bp, |
| 57 | int first, int last); |
Eric Sandeen | 6add2c4 | 2005-06-21 15:39:44 +1000 | [diff] [blame] | 58 | static void xfs_dir2_leaf_log_tail(struct xfs_trans *tp, struct xfs_dabuf *bp); |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 59 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
| 61 | /* |
| 62 | * Convert a block form directory to a leaf form directory. |
| 63 | */ |
| 64 | int /* error */ |
| 65 | xfs_dir2_block_to_leaf( |
| 66 | xfs_da_args_t *args, /* operation arguments */ |
| 67 | xfs_dabuf_t *dbp) /* input block's buffer */ |
| 68 | { |
Nathan Scott | 68b3a10 | 2006-03-17 17:27:19 +1100 | [diff] [blame] | 69 | __be16 *bestsp; /* leaf's bestsp entries */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | xfs_dablk_t blkno; /* leaf block's bno */ |
| 71 | xfs_dir2_block_t *block; /* block structure */ |
| 72 | xfs_dir2_leaf_entry_t *blp; /* block's leaf entries */ |
| 73 | xfs_dir2_block_tail_t *btp; /* block's tail */ |
| 74 | xfs_inode_t *dp; /* incore directory inode */ |
| 75 | int error; /* error return code */ |
| 76 | xfs_dabuf_t *lbp; /* leaf block's buffer */ |
| 77 | xfs_dir2_db_t ldb; /* leaf block's bno */ |
| 78 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 79 | xfs_dir2_leaf_tail_t *ltp; /* leaf's tail */ |
| 80 | xfs_mount_t *mp; /* filesystem mount point */ |
| 81 | int needlog; /* need to log block header */ |
| 82 | int needscan; /* need to rescan bestfree */ |
| 83 | xfs_trans_t *tp; /* transaction pointer */ |
| 84 | |
| 85 | xfs_dir2_trace_args_b("block_to_leaf", args, dbp); |
| 86 | dp = args->dp; |
| 87 | mp = dp->i_mount; |
| 88 | tp = args->trans; |
| 89 | /* |
| 90 | * Add the leaf block to the inode. |
| 91 | * This interface will only put blocks in the leaf/node range. |
| 92 | * Since that's empty now, we'll get the root (block 0 in range). |
| 93 | */ |
| 94 | if ((error = xfs_da_grow_inode(args, &blkno))) { |
| 95 | return error; |
| 96 | } |
| 97 | ldb = XFS_DIR2_DA_TO_DB(mp, blkno); |
| 98 | ASSERT(ldb == XFS_DIR2_LEAF_FIRSTDB(mp)); |
| 99 | /* |
| 100 | * Initialize the leaf block, get a buffer for it. |
| 101 | */ |
| 102 | if ((error = xfs_dir2_leaf_init(args, ldb, &lbp, XFS_DIR2_LEAF1_MAGIC))) { |
| 103 | return error; |
| 104 | } |
| 105 | ASSERT(lbp != NULL); |
| 106 | leaf = lbp->data; |
| 107 | block = dbp->data; |
| 108 | xfs_dir2_data_check(dp, dbp); |
| 109 | btp = XFS_DIR2_BLOCK_TAIL_P(mp, block); |
| 110 | blp = XFS_DIR2_BLOCK_LEAF_P(btp); |
| 111 | /* |
| 112 | * Set the counts in the leaf header. |
| 113 | */ |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 114 | leaf->hdr.count = cpu_to_be16(be32_to_cpu(btp->count)); |
| 115 | leaf->hdr.stale = cpu_to_be16(be32_to_cpu(btp->stale)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | /* |
| 117 | * Could compact these but I think we always do the conversion |
| 118 | * after squeezing out stale entries. |
| 119 | */ |
Nathan Scott | e922fff | 2006-03-17 17:27:56 +1100 | [diff] [blame] | 120 | memcpy(leaf->ents, blp, be32_to_cpu(btp->count) * sizeof(xfs_dir2_leaf_entry_t)); |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 121 | xfs_dir2_leaf_log_ents(tp, lbp, 0, be16_to_cpu(leaf->hdr.count) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | needscan = 0; |
| 123 | needlog = 1; |
| 124 | /* |
| 125 | * Make the space formerly occupied by the leaf entries and block |
| 126 | * tail be free. |
| 127 | */ |
| 128 | xfs_dir2_data_make_free(tp, dbp, |
| 129 | (xfs_dir2_data_aoff_t)((char *)blp - (char *)block), |
| 130 | (xfs_dir2_data_aoff_t)((char *)block + mp->m_dirblksize - |
| 131 | (char *)blp), |
| 132 | &needlog, &needscan); |
| 133 | /* |
| 134 | * Fix up the block header, make it a data block. |
| 135 | */ |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 136 | block->hdr.magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | if (needscan) |
| 138 | xfs_dir2_data_freescan(mp, (xfs_dir2_data_t *)block, &needlog, |
| 139 | NULL); |
| 140 | /* |
| 141 | * Set up leaf tail and bests table. |
| 142 | */ |
| 143 | ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf); |
Nathan Scott | afbcb3f | 2006-03-17 17:27:28 +1100 | [diff] [blame] | 144 | ltp->bestcount = cpu_to_be32(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | bestsp = XFS_DIR2_LEAF_BESTS_P(ltp); |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 146 | bestsp[0] = block->hdr.bestfree[0].length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | /* |
| 148 | * Log the data header and leaf bests table. |
| 149 | */ |
| 150 | if (needlog) |
| 151 | xfs_dir2_data_log_header(tp, dbp); |
| 152 | xfs_dir2_leaf_check(dp, lbp); |
| 153 | xfs_dir2_data_check(dp, dbp); |
| 154 | xfs_dir2_leaf_log_bests(tp, lbp, 0, 0); |
| 155 | xfs_da_buf_done(lbp); |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | * Add an entry to a leaf form directory. |
| 161 | */ |
| 162 | int /* error */ |
| 163 | xfs_dir2_leaf_addname( |
| 164 | xfs_da_args_t *args) /* operation arguments */ |
| 165 | { |
Nathan Scott | 68b3a10 | 2006-03-17 17:27:19 +1100 | [diff] [blame] | 166 | __be16 *bestsp; /* freespace table in leaf */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | int compact; /* need to compact leaves */ |
| 168 | xfs_dir2_data_t *data; /* data block structure */ |
| 169 | xfs_dabuf_t *dbp; /* data block buffer */ |
| 170 | xfs_dir2_data_entry_t *dep; /* data block entry */ |
| 171 | xfs_inode_t *dp; /* incore directory inode */ |
| 172 | xfs_dir2_data_unused_t *dup; /* data unused entry */ |
| 173 | int error; /* error return value */ |
| 174 | int grown; /* allocated new data block */ |
| 175 | int highstale; /* index of next stale leaf */ |
| 176 | int i; /* temporary, index */ |
| 177 | int index; /* leaf table position */ |
| 178 | xfs_dabuf_t *lbp; /* leaf's buffer */ |
| 179 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 180 | int length; /* length of new entry */ |
| 181 | xfs_dir2_leaf_entry_t *lep; /* leaf entry table pointer */ |
| 182 | int lfloglow; /* low leaf logging index */ |
| 183 | int lfloghigh; /* high leaf logging index */ |
| 184 | int lowstale; /* index of prev stale leaf */ |
| 185 | xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */ |
| 186 | xfs_mount_t *mp; /* filesystem mount point */ |
| 187 | int needbytes; /* leaf block bytes needed */ |
| 188 | int needlog; /* need to log data header */ |
| 189 | int needscan; /* need to rescan data free */ |
Nathan Scott | 3d693c6 | 2006-03-17 17:28:27 +1100 | [diff] [blame] | 190 | __be16 *tagp; /* end of data entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | xfs_trans_t *tp; /* transaction pointer */ |
| 192 | xfs_dir2_db_t use_block; /* data block number */ |
| 193 | |
| 194 | xfs_dir2_trace_args("leaf_addname", args); |
| 195 | dp = args->dp; |
| 196 | tp = args->trans; |
| 197 | mp = dp->i_mount; |
| 198 | /* |
| 199 | * Read the leaf block. |
| 200 | */ |
| 201 | error = xfs_da_read_buf(tp, dp, mp->m_dirleafblk, -1, &lbp, |
| 202 | XFS_DATA_FORK); |
| 203 | if (error) { |
| 204 | return error; |
| 205 | } |
| 206 | ASSERT(lbp != NULL); |
| 207 | /* |
| 208 | * Look up the entry by hash value and name. |
| 209 | * We know it's not there, our caller has already done a lookup. |
| 210 | * So the index is of the entry to insert in front of. |
| 211 | * But if there are dup hash values the index is of the first of those. |
| 212 | */ |
| 213 | index = xfs_dir2_leaf_search_hash(args, lbp); |
| 214 | leaf = lbp->data; |
| 215 | ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf); |
| 216 | bestsp = XFS_DIR2_LEAF_BESTS_P(ltp); |
| 217 | length = XFS_DIR2_DATA_ENTSIZE(args->namelen); |
| 218 | /* |
| 219 | * See if there are any entries with the same hash value |
| 220 | * and space in their block for the new entry. |
| 221 | * This is good because it puts multiple same-hash value entries |
| 222 | * in a data block, improving the lookup of those entries. |
| 223 | */ |
| 224 | for (use_block = -1, lep = &leaf->ents[index]; |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 225 | index < be16_to_cpu(leaf->hdr.count) && be32_to_cpu(lep->hashval) == args->hashval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | index++, lep++) { |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 227 | if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | continue; |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 229 | i = XFS_DIR2_DATAPTR_TO_DB(mp, be32_to_cpu(lep->address)); |
Nathan Scott | afbcb3f | 2006-03-17 17:27:28 +1100 | [diff] [blame] | 230 | ASSERT(i < be32_to_cpu(ltp->bestcount)); |
Nathan Scott | 68b3a10 | 2006-03-17 17:27:19 +1100 | [diff] [blame] | 231 | ASSERT(be16_to_cpu(bestsp[i]) != NULLDATAOFF); |
| 232 | if (be16_to_cpu(bestsp[i]) >= length) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | use_block = i; |
| 234 | break; |
| 235 | } |
| 236 | } |
| 237 | /* |
| 238 | * Didn't find a block yet, linear search all the data blocks. |
| 239 | */ |
| 240 | if (use_block == -1) { |
Nathan Scott | afbcb3f | 2006-03-17 17:27:28 +1100 | [diff] [blame] | 241 | for (i = 0; i < be32_to_cpu(ltp->bestcount); i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | /* |
| 243 | * Remember a block we see that's missing. |
| 244 | */ |
Nathan Scott | 68b3a10 | 2006-03-17 17:27:19 +1100 | [diff] [blame] | 245 | if (be16_to_cpu(bestsp[i]) == NULLDATAOFF && use_block == -1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | use_block = i; |
Nathan Scott | 68b3a10 | 2006-03-17 17:27:19 +1100 | [diff] [blame] | 247 | else if (be16_to_cpu(bestsp[i]) >= length) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | use_block = i; |
| 249 | break; |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | /* |
| 254 | * How many bytes do we need in the leaf block? |
| 255 | */ |
| 256 | needbytes = |
| 257 | (leaf->hdr.stale ? 0 : (uint)sizeof(leaf->ents[0])) + |
| 258 | (use_block != -1 ? 0 : (uint)sizeof(leaf->bests[0])); |
| 259 | /* |
| 260 | * Now kill use_block if it refers to a missing block, so we |
| 261 | * can use it as an indication of allocation needed. |
| 262 | */ |
Nathan Scott | 68b3a10 | 2006-03-17 17:27:19 +1100 | [diff] [blame] | 263 | if (use_block != -1 && be16_to_cpu(bestsp[use_block]) == NULLDATAOFF) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | use_block = -1; |
| 265 | /* |
| 266 | * If we don't have enough free bytes but we can make enough |
| 267 | * by compacting out stale entries, we'll do that. |
| 268 | */ |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 269 | if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] < needbytes && |
| 270 | be16_to_cpu(leaf->hdr.stale) > 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | compact = 1; |
| 272 | } |
| 273 | /* |
| 274 | * Otherwise if we don't have enough free bytes we need to |
| 275 | * convert to node form. |
| 276 | */ |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 277 | else if ((char *)bestsp - (char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] < |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | needbytes) { |
| 279 | /* |
| 280 | * Just checking or no space reservation, give up. |
| 281 | */ |
| 282 | if (args->justcheck || args->total == 0) { |
| 283 | xfs_da_brelse(tp, lbp); |
| 284 | return XFS_ERROR(ENOSPC); |
| 285 | } |
| 286 | /* |
| 287 | * Convert to node form. |
| 288 | */ |
| 289 | error = xfs_dir2_leaf_to_node(args, lbp); |
| 290 | xfs_da_buf_done(lbp); |
| 291 | if (error) |
| 292 | return error; |
| 293 | /* |
| 294 | * Then add the new entry. |
| 295 | */ |
| 296 | return xfs_dir2_node_addname(args); |
| 297 | } |
| 298 | /* |
| 299 | * Otherwise it will fit without compaction. |
| 300 | */ |
| 301 | else |
| 302 | compact = 0; |
| 303 | /* |
| 304 | * If just checking, then it will fit unless we needed to allocate |
| 305 | * a new data block. |
| 306 | */ |
| 307 | if (args->justcheck) { |
| 308 | xfs_da_brelse(tp, lbp); |
| 309 | return use_block == -1 ? XFS_ERROR(ENOSPC) : 0; |
| 310 | } |
| 311 | /* |
| 312 | * If no allocations are allowed, return now before we've |
| 313 | * changed anything. |
| 314 | */ |
| 315 | if (args->total == 0 && use_block == -1) { |
| 316 | xfs_da_brelse(tp, lbp); |
| 317 | return XFS_ERROR(ENOSPC); |
| 318 | } |
| 319 | /* |
| 320 | * Need to compact the leaf entries, removing stale ones. |
| 321 | * Leave one stale entry behind - the one closest to our |
| 322 | * insertion index - and we'll shift that one to our insertion |
| 323 | * point later. |
| 324 | */ |
| 325 | if (compact) { |
| 326 | xfs_dir2_leaf_compact_x1(lbp, &index, &lowstale, &highstale, |
| 327 | &lfloglow, &lfloghigh); |
| 328 | } |
| 329 | /* |
| 330 | * There are stale entries, so we'll need log-low and log-high |
| 331 | * impossibly bad values later. |
| 332 | */ |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 333 | else if (be16_to_cpu(leaf->hdr.stale)) { |
| 334 | lfloglow = be16_to_cpu(leaf->hdr.count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | lfloghigh = -1; |
| 336 | } |
| 337 | /* |
| 338 | * If there was no data block space found, we need to allocate |
| 339 | * a new one. |
| 340 | */ |
| 341 | if (use_block == -1) { |
| 342 | /* |
| 343 | * Add the new data block. |
| 344 | */ |
| 345 | if ((error = xfs_dir2_grow_inode(args, XFS_DIR2_DATA_SPACE, |
| 346 | &use_block))) { |
| 347 | xfs_da_brelse(tp, lbp); |
| 348 | return error; |
| 349 | } |
| 350 | /* |
| 351 | * Initialize the block. |
| 352 | */ |
| 353 | if ((error = xfs_dir2_data_init(args, use_block, &dbp))) { |
| 354 | xfs_da_brelse(tp, lbp); |
| 355 | return error; |
| 356 | } |
| 357 | /* |
| 358 | * If we're adding a new data block on the end we need to |
| 359 | * extend the bests table. Copy it up one entry. |
| 360 | */ |
Nathan Scott | afbcb3f | 2006-03-17 17:27:28 +1100 | [diff] [blame] | 361 | if (use_block >= be32_to_cpu(ltp->bestcount)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | bestsp--; |
| 363 | memmove(&bestsp[0], &bestsp[1], |
Nathan Scott | afbcb3f | 2006-03-17 17:27:28 +1100 | [diff] [blame] | 364 | be32_to_cpu(ltp->bestcount) * sizeof(bestsp[0])); |
| 365 | be32_add(<p->bestcount, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | xfs_dir2_leaf_log_tail(tp, lbp); |
Nathan Scott | afbcb3f | 2006-03-17 17:27:28 +1100 | [diff] [blame] | 367 | xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | } |
| 369 | /* |
| 370 | * If we're filling in a previously empty block just log it. |
| 371 | */ |
| 372 | else |
| 373 | xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block); |
| 374 | data = dbp->data; |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 375 | bestsp[use_block] = data->hdr.bestfree[0].length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | grown = 1; |
| 377 | } |
| 378 | /* |
| 379 | * Already had space in some data block. |
| 380 | * Just read that one in. |
| 381 | */ |
| 382 | else { |
| 383 | if ((error = |
| 384 | xfs_da_read_buf(tp, dp, XFS_DIR2_DB_TO_DA(mp, use_block), |
| 385 | -1, &dbp, XFS_DATA_FORK))) { |
| 386 | xfs_da_brelse(tp, lbp); |
| 387 | return error; |
| 388 | } |
| 389 | data = dbp->data; |
| 390 | grown = 0; |
| 391 | } |
| 392 | xfs_dir2_data_check(dp, dbp); |
| 393 | /* |
| 394 | * Point to the biggest freespace in our data block. |
| 395 | */ |
| 396 | dup = (xfs_dir2_data_unused_t *) |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 397 | ((char *)data + be16_to_cpu(data->hdr.bestfree[0].offset)); |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 398 | ASSERT(be16_to_cpu(dup->length) >= length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | needscan = needlog = 0; |
| 400 | /* |
| 401 | * Mark the initial part of our freespace in use for the new entry. |
| 402 | */ |
| 403 | xfs_dir2_data_use_free(tp, dbp, dup, |
| 404 | (xfs_dir2_data_aoff_t)((char *)dup - (char *)data), length, |
| 405 | &needlog, &needscan); |
| 406 | /* |
| 407 | * Initialize our new entry (at last). |
| 408 | */ |
| 409 | dep = (xfs_dir2_data_entry_t *)dup; |
| 410 | INT_SET(dep->inumber, ARCH_CONVERT, args->inumber); |
| 411 | dep->namelen = args->namelen; |
| 412 | memcpy(dep->name, args->name, dep->namelen); |
| 413 | tagp = XFS_DIR2_DATA_ENTRY_TAG_P(dep); |
Nathan Scott | 3d693c6 | 2006-03-17 17:28:27 +1100 | [diff] [blame] | 414 | *tagp = cpu_to_be16((char *)dep - (char *)data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | /* |
| 416 | * Need to scan fix up the bestfree table. |
| 417 | */ |
| 418 | if (needscan) |
| 419 | xfs_dir2_data_freescan(mp, data, &needlog, NULL); |
| 420 | /* |
| 421 | * Need to log the data block's header. |
| 422 | */ |
| 423 | if (needlog) |
| 424 | xfs_dir2_data_log_header(tp, dbp); |
| 425 | xfs_dir2_data_log_entry(tp, dbp, dep); |
| 426 | /* |
| 427 | * If the bests table needs to be changed, do it. |
| 428 | * Log the change unless we've already done that. |
| 429 | */ |
Nathan Scott | 68b3a10 | 2006-03-17 17:27:19 +1100 | [diff] [blame] | 430 | if (be16_to_cpu(bestsp[use_block]) != be16_to_cpu(data->hdr.bestfree[0].length)) { |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 431 | bestsp[use_block] = data->hdr.bestfree[0].length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | if (!grown) |
| 433 | xfs_dir2_leaf_log_bests(tp, lbp, use_block, use_block); |
| 434 | } |
| 435 | /* |
| 436 | * Now we need to make room to insert the leaf entry. |
| 437 | * If there are no stale entries, we just insert a hole at index. |
| 438 | */ |
| 439 | if (!leaf->hdr.stale) { |
| 440 | /* |
| 441 | * lep is still good as the index leaf entry. |
| 442 | */ |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 443 | if (index < be16_to_cpu(leaf->hdr.count)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | memmove(lep + 1, lep, |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 445 | (be16_to_cpu(leaf->hdr.count) - index) * sizeof(*lep)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | /* |
| 447 | * Record low and high logging indices for the leaf. |
| 448 | */ |
| 449 | lfloglow = index; |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 450 | lfloghigh = be16_to_cpu(leaf->hdr.count); |
| 451 | be16_add(&leaf->hdr.count, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | } |
| 453 | /* |
| 454 | * There are stale entries. |
| 455 | * We will use one of them for the new entry. |
| 456 | * It's probably not at the right location, so we'll have to |
| 457 | * shift some up or down first. |
| 458 | */ |
| 459 | else { |
| 460 | /* |
| 461 | * If we didn't compact before, we need to find the nearest |
| 462 | * stale entries before and after our insertion point. |
| 463 | */ |
| 464 | if (compact == 0) { |
| 465 | /* |
| 466 | * Find the first stale entry before the insertion |
| 467 | * point, if any. |
| 468 | */ |
| 469 | for (lowstale = index - 1; |
| 470 | lowstale >= 0 && |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 471 | be32_to_cpu(leaf->ents[lowstale].address) != |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | XFS_DIR2_NULL_DATAPTR; |
| 473 | lowstale--) |
| 474 | continue; |
| 475 | /* |
| 476 | * Find the next stale entry at or after the insertion |
| 477 | * point, if any. Stop if we go so far that the |
| 478 | * lowstale entry would be better. |
| 479 | */ |
| 480 | for (highstale = index; |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 481 | highstale < be16_to_cpu(leaf->hdr.count) && |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 482 | be32_to_cpu(leaf->ents[highstale].address) != |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | XFS_DIR2_NULL_DATAPTR && |
| 484 | (lowstale < 0 || |
| 485 | index - lowstale - 1 >= highstale - index); |
| 486 | highstale++) |
| 487 | continue; |
| 488 | } |
| 489 | /* |
| 490 | * If the low one is better, use it. |
| 491 | */ |
| 492 | if (lowstale >= 0 && |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 493 | (highstale == be16_to_cpu(leaf->hdr.count) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | index - lowstale - 1 < highstale - index)) { |
| 495 | ASSERT(index - lowstale - 1 >= 0); |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 496 | ASSERT(be32_to_cpu(leaf->ents[lowstale].address) == |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | XFS_DIR2_NULL_DATAPTR); |
| 498 | /* |
| 499 | * Copy entries up to cover the stale entry |
| 500 | * and make room for the new entry. |
| 501 | */ |
| 502 | if (index - lowstale - 1 > 0) |
| 503 | memmove(&leaf->ents[lowstale], |
| 504 | &leaf->ents[lowstale + 1], |
| 505 | (index - lowstale - 1) * sizeof(*lep)); |
| 506 | lep = &leaf->ents[index - 1]; |
| 507 | lfloglow = MIN(lowstale, lfloglow); |
| 508 | lfloghigh = MAX(index - 1, lfloghigh); |
| 509 | } |
| 510 | /* |
| 511 | * The high one is better, so use that one. |
| 512 | */ |
| 513 | else { |
| 514 | ASSERT(highstale - index >= 0); |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 515 | ASSERT(be32_to_cpu(leaf->ents[highstale].address) == |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | XFS_DIR2_NULL_DATAPTR); |
| 517 | /* |
| 518 | * Copy entries down to copver the stale entry |
| 519 | * and make room for the new entry. |
| 520 | */ |
| 521 | if (highstale - index > 0) |
| 522 | memmove(&leaf->ents[index + 1], |
| 523 | &leaf->ents[index], |
| 524 | (highstale - index) * sizeof(*lep)); |
| 525 | lep = &leaf->ents[index]; |
| 526 | lfloglow = MIN(index, lfloglow); |
| 527 | lfloghigh = MAX(highstale, lfloghigh); |
| 528 | } |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 529 | be16_add(&leaf->hdr.stale, -1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | } |
| 531 | /* |
| 532 | * Fill in the new leaf entry. |
| 533 | */ |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 534 | lep->hashval = cpu_to_be32(args->hashval); |
| 535 | lep->address = cpu_to_be32(XFS_DIR2_DB_OFF_TO_DATAPTR(mp, use_block, |
Nathan Scott | 3d693c6 | 2006-03-17 17:28:27 +1100 | [diff] [blame] | 536 | be16_to_cpu(*tagp))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | /* |
| 538 | * Log the leaf fields and give up the buffers. |
| 539 | */ |
| 540 | xfs_dir2_leaf_log_header(tp, lbp); |
| 541 | xfs_dir2_leaf_log_ents(tp, lbp, lfloglow, lfloghigh); |
| 542 | xfs_dir2_leaf_check(dp, lbp); |
| 543 | xfs_da_buf_done(lbp); |
| 544 | xfs_dir2_data_check(dp, dbp); |
| 545 | xfs_da_buf_done(dbp); |
| 546 | return 0; |
| 547 | } |
| 548 | |
| 549 | #ifdef DEBUG |
| 550 | /* |
| 551 | * Check the internal consistency of a leaf1 block. |
| 552 | * Pop an assert if something is wrong. |
| 553 | */ |
| 554 | void |
| 555 | xfs_dir2_leaf_check( |
| 556 | xfs_inode_t *dp, /* incore directory inode */ |
| 557 | xfs_dabuf_t *bp) /* leaf's buffer */ |
| 558 | { |
| 559 | int i; /* leaf index */ |
| 560 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 561 | xfs_dir2_leaf_tail_t *ltp; /* leaf tail pointer */ |
| 562 | xfs_mount_t *mp; /* filesystem mount point */ |
| 563 | int stale; /* count of stale leaves */ |
| 564 | |
| 565 | leaf = bp->data; |
| 566 | mp = dp->i_mount; |
Nathan Scott | 89da054 | 2006-03-17 17:28:40 +1100 | [diff] [blame] | 567 | ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | /* |
| 569 | * This value is not restrictive enough. |
| 570 | * Should factor in the size of the bests table as well. |
| 571 | * We can deduce a value for that from di_size. |
| 572 | */ |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 573 | ASSERT(be16_to_cpu(leaf->hdr.count) <= XFS_DIR2_MAX_LEAF_ENTS(mp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf); |
| 575 | /* |
| 576 | * Leaves and bests don't overlap. |
| 577 | */ |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 578 | ASSERT((char *)&leaf->ents[be16_to_cpu(leaf->hdr.count)] <= |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | (char *)XFS_DIR2_LEAF_BESTS_P(ltp)); |
| 580 | /* |
| 581 | * Check hash value order, count stale entries. |
| 582 | */ |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 583 | for (i = stale = 0; i < be16_to_cpu(leaf->hdr.count); i++) { |
| 584 | if (i + 1 < be16_to_cpu(leaf->hdr.count)) |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 585 | ASSERT(be32_to_cpu(leaf->ents[i].hashval) <= |
| 586 | be32_to_cpu(leaf->ents[i + 1].hashval)); |
| 587 | if (be32_to_cpu(leaf->ents[i].address) == XFS_DIR2_NULL_DATAPTR) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | stale++; |
| 589 | } |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 590 | ASSERT(be16_to_cpu(leaf->hdr.stale) == stale); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | } |
| 592 | #endif /* DEBUG */ |
| 593 | |
| 594 | /* |
| 595 | * Compact out any stale entries in the leaf. |
| 596 | * Log the header and changed leaf entries, if any. |
| 597 | */ |
| 598 | void |
| 599 | xfs_dir2_leaf_compact( |
| 600 | xfs_da_args_t *args, /* operation arguments */ |
| 601 | xfs_dabuf_t *bp) /* leaf buffer */ |
| 602 | { |
| 603 | int from; /* source leaf index */ |
| 604 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 605 | int loglow; /* first leaf entry to log */ |
| 606 | int to; /* target leaf index */ |
| 607 | |
| 608 | leaf = bp->data; |
| 609 | if (!leaf->hdr.stale) { |
| 610 | return; |
| 611 | } |
| 612 | /* |
| 613 | * Compress out the stale entries in place. |
| 614 | */ |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 615 | for (from = to = 0, loglow = -1; from < be16_to_cpu(leaf->hdr.count); from++) { |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 616 | if (be32_to_cpu(leaf->ents[from].address) == XFS_DIR2_NULL_DATAPTR) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | continue; |
| 618 | /* |
| 619 | * Only actually copy the entries that are different. |
| 620 | */ |
| 621 | if (from > to) { |
| 622 | if (loglow == -1) |
| 623 | loglow = to; |
| 624 | leaf->ents[to] = leaf->ents[from]; |
| 625 | } |
| 626 | to++; |
| 627 | } |
| 628 | /* |
| 629 | * Update and log the header, log the leaf entries. |
| 630 | */ |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 631 | ASSERT(be16_to_cpu(leaf->hdr.stale) == from - to); |
| 632 | be16_add(&leaf->hdr.count, -(be16_to_cpu(leaf->hdr.stale))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | leaf->hdr.stale = 0; |
| 634 | xfs_dir2_leaf_log_header(args->trans, bp); |
| 635 | if (loglow != -1) |
| 636 | xfs_dir2_leaf_log_ents(args->trans, bp, loglow, to - 1); |
| 637 | } |
| 638 | |
| 639 | /* |
| 640 | * Compact the leaf entries, removing stale ones. |
| 641 | * Leave one stale entry behind - the one closest to our |
| 642 | * insertion index - and the caller will shift that one to our insertion |
| 643 | * point later. |
| 644 | * Return new insertion index, where the remaining stale entry is, |
| 645 | * and leaf logging indices. |
| 646 | */ |
| 647 | void |
| 648 | xfs_dir2_leaf_compact_x1( |
| 649 | xfs_dabuf_t *bp, /* leaf buffer */ |
| 650 | int *indexp, /* insertion index */ |
| 651 | int *lowstalep, /* out: stale entry before us */ |
| 652 | int *highstalep, /* out: stale entry after us */ |
| 653 | int *lowlogp, /* out: low log index */ |
| 654 | int *highlogp) /* out: high log index */ |
| 655 | { |
| 656 | int from; /* source copy index */ |
| 657 | int highstale; /* stale entry at/after index */ |
| 658 | int index; /* insertion index */ |
| 659 | int keepstale; /* source index of kept stale */ |
| 660 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 661 | int lowstale; /* stale entry before index */ |
| 662 | int newindex=0; /* new insertion index */ |
| 663 | int to; /* destination copy index */ |
| 664 | |
| 665 | leaf = bp->data; |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 666 | ASSERT(be16_to_cpu(leaf->hdr.stale) > 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | index = *indexp; |
| 668 | /* |
| 669 | * Find the first stale entry before our index, if any. |
| 670 | */ |
| 671 | for (lowstale = index - 1; |
| 672 | lowstale >= 0 && |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 673 | be32_to_cpu(leaf->ents[lowstale].address) != XFS_DIR2_NULL_DATAPTR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | lowstale--) |
| 675 | continue; |
| 676 | /* |
| 677 | * Find the first stale entry at or after our index, if any. |
| 678 | * Stop if the answer would be worse than lowstale. |
| 679 | */ |
| 680 | for (highstale = index; |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 681 | highstale < be16_to_cpu(leaf->hdr.count) && |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 682 | be32_to_cpu(leaf->ents[highstale].address) != XFS_DIR2_NULL_DATAPTR && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | (lowstale < 0 || index - lowstale > highstale - index); |
| 684 | highstale++) |
| 685 | continue; |
| 686 | /* |
| 687 | * Pick the better of lowstale and highstale. |
| 688 | */ |
| 689 | if (lowstale >= 0 && |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 690 | (highstale == be16_to_cpu(leaf->hdr.count) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | index - lowstale <= highstale - index)) |
| 692 | keepstale = lowstale; |
| 693 | else |
| 694 | keepstale = highstale; |
| 695 | /* |
| 696 | * Copy the entries in place, removing all the stale entries |
| 697 | * except keepstale. |
| 698 | */ |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 699 | for (from = to = 0; from < be16_to_cpu(leaf->hdr.count); from++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | /* |
| 701 | * Notice the new value of index. |
| 702 | */ |
| 703 | if (index == from) |
| 704 | newindex = to; |
| 705 | if (from != keepstale && |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 706 | be32_to_cpu(leaf->ents[from].address) == XFS_DIR2_NULL_DATAPTR) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | if (from == to) |
| 708 | *lowlogp = to; |
| 709 | continue; |
| 710 | } |
| 711 | /* |
| 712 | * Record the new keepstale value for the insertion. |
| 713 | */ |
| 714 | if (from == keepstale) |
| 715 | lowstale = highstale = to; |
| 716 | /* |
| 717 | * Copy only the entries that have moved. |
| 718 | */ |
| 719 | if (from > to) |
| 720 | leaf->ents[to] = leaf->ents[from]; |
| 721 | to++; |
| 722 | } |
| 723 | ASSERT(from > to); |
| 724 | /* |
| 725 | * If the insertion point was past the last entry, |
| 726 | * set the new insertion point accordingly. |
| 727 | */ |
| 728 | if (index == from) |
| 729 | newindex = to; |
| 730 | *indexp = newindex; |
| 731 | /* |
| 732 | * Adjust the leaf header values. |
| 733 | */ |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 734 | be16_add(&leaf->hdr.count, -(from - to)); |
| 735 | leaf->hdr.stale = cpu_to_be16(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | /* |
| 737 | * Remember the low/high stale value only in the "right" |
| 738 | * direction. |
| 739 | */ |
| 740 | if (lowstale >= newindex) |
| 741 | lowstale = -1; |
| 742 | else |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 743 | highstale = be16_to_cpu(leaf->hdr.count); |
| 744 | *highlogp = be16_to_cpu(leaf->hdr.count) - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | *lowstalep = lowstale; |
| 746 | *highstalep = highstale; |
| 747 | } |
| 748 | |
| 749 | /* |
| 750 | * Getdents (readdir) for leaf and node directories. |
| 751 | * This reads the data blocks only, so is the same for both forms. |
| 752 | */ |
| 753 | int /* error */ |
| 754 | xfs_dir2_leaf_getdents( |
| 755 | xfs_trans_t *tp, /* transaction pointer */ |
| 756 | xfs_inode_t *dp, /* incore directory inode */ |
| 757 | uio_t *uio, /* I/O control & vectors */ |
| 758 | int *eofp, /* out: reached end of dir */ |
| 759 | xfs_dirent_t *dbp, /* caller's buffer */ |
| 760 | xfs_dir2_put_t put) /* ABI formatting routine */ |
| 761 | { |
| 762 | xfs_dabuf_t *bp; /* data block buffer */ |
| 763 | int byteoff; /* offset in current block */ |
| 764 | xfs_dir2_db_t curdb; /* db for current block */ |
| 765 | xfs_dir2_off_t curoff; /* current overall offset */ |
| 766 | xfs_dir2_data_t *data; /* data block structure */ |
| 767 | xfs_dir2_data_entry_t *dep; /* data entry */ |
| 768 | xfs_dir2_data_unused_t *dup; /* unused entry */ |
| 769 | int eof; /* reached end of directory */ |
Nathan Scott | f6d75cb | 2006-03-14 13:32:24 +1100 | [diff] [blame] | 770 | int error = 0; /* error return value */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | int i; /* temporary loop index */ |
| 772 | int j; /* temporary loop index */ |
| 773 | int length; /* temporary length value */ |
| 774 | xfs_bmbt_irec_t *map; /* map vector for blocks */ |
| 775 | xfs_extlen_t map_blocks; /* number of fsbs in map */ |
| 776 | xfs_dablk_t map_off; /* last mapped file offset */ |
| 777 | int map_size; /* total entries in *map */ |
| 778 | int map_valid; /* valid entries in *map */ |
| 779 | xfs_mount_t *mp; /* filesystem mount point */ |
| 780 | xfs_dir2_off_t newoff; /* new curoff after new blk */ |
| 781 | int nmap; /* mappings to ask xfs_bmapi */ |
Nathan Scott | f6d75cb | 2006-03-14 13:32:24 +1100 | [diff] [blame] | 782 | xfs_dir2_put_args_t *p; /* formatting arg bundle */ |
| 783 | char *ptr = NULL; /* pointer to current data */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | int ra_current; /* number of read-ahead blks */ |
| 785 | int ra_index; /* *map index for read-ahead */ |
| 786 | int ra_offset; /* map entry offset for ra */ |
| 787 | int ra_want; /* readahead count wanted */ |
| 788 | |
| 789 | /* |
| 790 | * If the offset is at or past the largest allowed value, |
| 791 | * give up right away, return eof. |
| 792 | */ |
| 793 | if (uio->uio_offset >= XFS_DIR2_MAX_DATAPTR) { |
| 794 | *eofp = 1; |
| 795 | return 0; |
| 796 | } |
| 797 | mp = dp->i_mount; |
| 798 | /* |
| 799 | * Setup formatting arguments. |
| 800 | */ |
Nathan Scott | f6d75cb | 2006-03-14 13:32:24 +1100 | [diff] [blame] | 801 | p = kmem_alloc(sizeof(*p), KM_SLEEP); |
| 802 | p->dbp = dbp; |
| 803 | p->put = put; |
| 804 | p->uio = uio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | /* |
| 806 | * Set up to bmap a number of blocks based on the caller's |
| 807 | * buffer size, the directory block size, and the filesystem |
| 808 | * block size. |
| 809 | */ |
| 810 | map_size = |
| 811 | howmany(uio->uio_resid + mp->m_dirblksize, |
| 812 | mp->m_sb.sb_blocksize); |
| 813 | map = kmem_alloc(map_size * sizeof(*map), KM_SLEEP); |
| 814 | map_valid = ra_index = ra_offset = ra_current = map_blocks = 0; |
| 815 | bp = NULL; |
| 816 | eof = 1; |
| 817 | /* |
| 818 | * Inside the loop we keep the main offset value as a byte offset |
| 819 | * in the directory file. |
| 820 | */ |
| 821 | curoff = XFS_DIR2_DATAPTR_TO_BYTE(mp, uio->uio_offset); |
| 822 | /* |
| 823 | * Force this conversion through db so we truncate the offset |
| 824 | * down to get the start of the data block. |
| 825 | */ |
| 826 | map_off = XFS_DIR2_DB_TO_DA(mp, XFS_DIR2_BYTE_TO_DB(mp, curoff)); |
| 827 | /* |
| 828 | * Loop over directory entries until we reach the end offset. |
| 829 | * Get more blocks and readahead as necessary. |
| 830 | */ |
| 831 | while (curoff < XFS_DIR2_LEAF_OFFSET) { |
| 832 | /* |
| 833 | * If we have no buffer, or we're off the end of the |
| 834 | * current buffer, need to get another one. |
| 835 | */ |
| 836 | if (!bp || ptr >= (char *)bp->data + mp->m_dirblksize) { |
| 837 | /* |
| 838 | * If we have a buffer, we need to release it and |
| 839 | * take it out of the mapping. |
| 840 | */ |
| 841 | if (bp) { |
| 842 | xfs_da_brelse(tp, bp); |
| 843 | bp = NULL; |
| 844 | map_blocks -= mp->m_dirblkfsbs; |
| 845 | /* |
| 846 | * Loop to get rid of the extents for the |
| 847 | * directory block. |
| 848 | */ |
| 849 | for (i = mp->m_dirblkfsbs; i > 0; ) { |
| 850 | j = MIN((int)map->br_blockcount, i); |
| 851 | map->br_blockcount -= j; |
| 852 | map->br_startblock += j; |
| 853 | map->br_startoff += j; |
| 854 | /* |
| 855 | * If mapping is done, pitch it from |
| 856 | * the table. |
| 857 | */ |
| 858 | if (!map->br_blockcount && --map_valid) |
| 859 | memmove(&map[0], &map[1], |
| 860 | sizeof(map[0]) * |
| 861 | map_valid); |
| 862 | i -= j; |
| 863 | } |
| 864 | } |
| 865 | /* |
| 866 | * Recalculate the readahead blocks wanted. |
| 867 | */ |
| 868 | ra_want = howmany(uio->uio_resid + mp->m_dirblksize, |
| 869 | mp->m_sb.sb_blocksize) - 1; |
| 870 | /* |
| 871 | * If we don't have as many as we want, and we haven't |
| 872 | * run out of data blocks, get some more mappings. |
| 873 | */ |
| 874 | if (1 + ra_want > map_blocks && |
| 875 | map_off < |
| 876 | XFS_DIR2_BYTE_TO_DA(mp, XFS_DIR2_LEAF_OFFSET)) { |
| 877 | /* |
| 878 | * Get more bmaps, fill in after the ones |
| 879 | * we already have in the table. |
| 880 | */ |
| 881 | nmap = map_size - map_valid; |
| 882 | error = xfs_bmapi(tp, dp, |
| 883 | map_off, |
| 884 | XFS_DIR2_BYTE_TO_DA(mp, |
| 885 | XFS_DIR2_LEAF_OFFSET) - map_off, |
| 886 | XFS_BMAPI_METADATA, NULL, 0, |
| 887 | &map[map_valid], &nmap, NULL); |
| 888 | /* |
| 889 | * Don't know if we should ignore this or |
| 890 | * try to return an error. |
| 891 | * The trouble with returning errors |
| 892 | * is that readdir will just stop without |
| 893 | * actually passing the error through. |
| 894 | */ |
| 895 | if (error) |
| 896 | break; /* XXX */ |
| 897 | /* |
| 898 | * If we got all the mappings we asked for, |
| 899 | * set the final map offset based on the |
| 900 | * last bmap value received. |
| 901 | * Otherwise, we've reached the end. |
| 902 | */ |
| 903 | if (nmap == map_size - map_valid) |
| 904 | map_off = |
| 905 | map[map_valid + nmap - 1].br_startoff + |
| 906 | map[map_valid + nmap - 1].br_blockcount; |
| 907 | else |
| 908 | map_off = |
| 909 | XFS_DIR2_BYTE_TO_DA(mp, |
| 910 | XFS_DIR2_LEAF_OFFSET); |
| 911 | /* |
| 912 | * Look for holes in the mapping, and |
| 913 | * eliminate them. Count up the valid blocks. |
| 914 | */ |
| 915 | for (i = map_valid; i < map_valid + nmap; ) { |
| 916 | if (map[i].br_startblock == |
| 917 | HOLESTARTBLOCK) { |
| 918 | nmap--; |
| 919 | length = map_valid + nmap - i; |
| 920 | if (length) |
| 921 | memmove(&map[i], |
| 922 | &map[i + 1], |
| 923 | sizeof(map[i]) * |
| 924 | length); |
| 925 | } else { |
| 926 | map_blocks += |
| 927 | map[i].br_blockcount; |
| 928 | i++; |
| 929 | } |
| 930 | } |
| 931 | map_valid += nmap; |
| 932 | } |
| 933 | /* |
| 934 | * No valid mappings, so no more data blocks. |
| 935 | */ |
| 936 | if (!map_valid) { |
| 937 | curoff = XFS_DIR2_DA_TO_BYTE(mp, map_off); |
| 938 | break; |
| 939 | } |
| 940 | /* |
| 941 | * Read the directory block starting at the first |
| 942 | * mapping. |
| 943 | */ |
| 944 | curdb = XFS_DIR2_DA_TO_DB(mp, map->br_startoff); |
| 945 | error = xfs_da_read_buf(tp, dp, map->br_startoff, |
| 946 | map->br_blockcount >= mp->m_dirblkfsbs ? |
| 947 | XFS_FSB_TO_DADDR(mp, map->br_startblock) : |
| 948 | -1, |
| 949 | &bp, XFS_DATA_FORK); |
| 950 | /* |
| 951 | * Should just skip over the data block instead |
| 952 | * of giving up. |
| 953 | */ |
| 954 | if (error) |
| 955 | break; /* XXX */ |
| 956 | /* |
| 957 | * Adjust the current amount of read-ahead: we just |
| 958 | * read a block that was previously ra. |
| 959 | */ |
| 960 | if (ra_current) |
| 961 | ra_current -= mp->m_dirblkfsbs; |
| 962 | /* |
| 963 | * Do we need more readahead? |
| 964 | */ |
| 965 | for (ra_index = ra_offset = i = 0; |
| 966 | ra_want > ra_current && i < map_blocks; |
| 967 | i += mp->m_dirblkfsbs) { |
| 968 | ASSERT(ra_index < map_valid); |
| 969 | /* |
| 970 | * Read-ahead a contiguous directory block. |
| 971 | */ |
| 972 | if (i > ra_current && |
| 973 | map[ra_index].br_blockcount >= |
| 974 | mp->m_dirblkfsbs) { |
| 975 | xfs_baread(mp->m_ddev_targp, |
| 976 | XFS_FSB_TO_DADDR(mp, |
| 977 | map[ra_index].br_startblock + |
| 978 | ra_offset), |
| 979 | (int)BTOBB(mp->m_dirblksize)); |
| 980 | ra_current = i; |
| 981 | } |
| 982 | /* |
| 983 | * Read-ahead a non-contiguous directory block. |
| 984 | * This doesn't use our mapping, but this |
| 985 | * is a very rare case. |
| 986 | */ |
| 987 | else if (i > ra_current) { |
| 988 | (void)xfs_da_reada_buf(tp, dp, |
| 989 | map[ra_index].br_startoff + |
| 990 | ra_offset, XFS_DATA_FORK); |
| 991 | ra_current = i; |
| 992 | } |
| 993 | /* |
| 994 | * Advance offset through the mapping table. |
| 995 | */ |
| 996 | for (j = 0; j < mp->m_dirblkfsbs; j++) { |
| 997 | /* |
| 998 | * The rest of this extent but not |
| 999 | * more than a dir block. |
| 1000 | */ |
| 1001 | length = MIN(mp->m_dirblkfsbs, |
| 1002 | (int)(map[ra_index].br_blockcount - |
| 1003 | ra_offset)); |
| 1004 | j += length; |
| 1005 | ra_offset += length; |
| 1006 | /* |
| 1007 | * Advance to the next mapping if |
| 1008 | * this one is used up. |
| 1009 | */ |
| 1010 | if (ra_offset == |
| 1011 | map[ra_index].br_blockcount) { |
| 1012 | ra_offset = 0; |
| 1013 | ra_index++; |
| 1014 | } |
| 1015 | } |
| 1016 | } |
| 1017 | /* |
| 1018 | * Having done a read, we need to set a new offset. |
| 1019 | */ |
| 1020 | newoff = XFS_DIR2_DB_OFF_TO_BYTE(mp, curdb, 0); |
| 1021 | /* |
| 1022 | * Start of the current block. |
| 1023 | */ |
| 1024 | if (curoff < newoff) |
| 1025 | curoff = newoff; |
| 1026 | /* |
| 1027 | * Make sure we're in the right block. |
| 1028 | */ |
| 1029 | else if (curoff > newoff) |
| 1030 | ASSERT(XFS_DIR2_BYTE_TO_DB(mp, curoff) == |
| 1031 | curdb); |
| 1032 | data = bp->data; |
| 1033 | xfs_dir2_data_check(dp, bp); |
| 1034 | /* |
| 1035 | * Find our position in the block. |
| 1036 | */ |
| 1037 | ptr = (char *)&data->u; |
| 1038 | byteoff = XFS_DIR2_BYTE_TO_OFF(mp, curoff); |
| 1039 | /* |
| 1040 | * Skip past the header. |
| 1041 | */ |
| 1042 | if (byteoff == 0) |
| 1043 | curoff += (uint)sizeof(data->hdr); |
| 1044 | /* |
| 1045 | * Skip past entries until we reach our offset. |
| 1046 | */ |
| 1047 | else { |
| 1048 | while ((char *)ptr - (char *)data < byteoff) { |
| 1049 | dup = (xfs_dir2_data_unused_t *)ptr; |
| 1050 | |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 1051 | if (be16_to_cpu(dup->freetag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | == XFS_DIR2_DATA_FREE_TAG) { |
| 1053 | |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 1054 | length = be16_to_cpu(dup->length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | ptr += length; |
| 1056 | continue; |
| 1057 | } |
| 1058 | dep = (xfs_dir2_data_entry_t *)ptr; |
| 1059 | length = |
| 1060 | XFS_DIR2_DATA_ENTSIZE(dep->namelen); |
| 1061 | ptr += length; |
| 1062 | } |
| 1063 | /* |
| 1064 | * Now set our real offset. |
| 1065 | */ |
| 1066 | curoff = |
| 1067 | XFS_DIR2_DB_OFF_TO_BYTE(mp, |
| 1068 | XFS_DIR2_BYTE_TO_DB(mp, curoff), |
| 1069 | (char *)ptr - (char *)data); |
| 1070 | if (ptr >= (char *)data + mp->m_dirblksize) { |
| 1071 | continue; |
| 1072 | } |
| 1073 | } |
| 1074 | } |
| 1075 | /* |
| 1076 | * We have a pointer to an entry. |
| 1077 | * Is it a live one? |
| 1078 | */ |
| 1079 | dup = (xfs_dir2_data_unused_t *)ptr; |
| 1080 | /* |
| 1081 | * No, it's unused, skip over it. |
| 1082 | */ |
Nathan Scott | ad354eb | 2006-03-17 17:27:37 +1100 | [diff] [blame] | 1083 | if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) { |
| 1084 | length = be16_to_cpu(dup->length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | ptr += length; |
| 1086 | curoff += length; |
| 1087 | continue; |
| 1088 | } |
| 1089 | |
| 1090 | /* |
| 1091 | * Copy the entry into the putargs, and try formatting it. |
| 1092 | */ |
| 1093 | dep = (xfs_dir2_data_entry_t *)ptr; |
| 1094 | |
Nathan Scott | f6d75cb | 2006-03-14 13:32:24 +1100 | [diff] [blame] | 1095 | p->namelen = dep->namelen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | |
Nathan Scott | f6d75cb | 2006-03-14 13:32:24 +1100 | [diff] [blame] | 1097 | length = XFS_DIR2_DATA_ENTSIZE(p->namelen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | |
Nathan Scott | f6d75cb | 2006-03-14 13:32:24 +1100 | [diff] [blame] | 1099 | p->cook = XFS_DIR2_BYTE_TO_DATAPTR(mp, curoff + length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | |
Nathan Scott | f6d75cb | 2006-03-14 13:32:24 +1100 | [diff] [blame] | 1101 | p->ino = INT_GET(dep->inumber, ARCH_CONVERT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | #if XFS_BIG_INUMS |
Nathan Scott | f6d75cb | 2006-03-14 13:32:24 +1100 | [diff] [blame] | 1103 | p->ino += mp->m_inoadd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | #endif |
Nathan Scott | f6d75cb | 2006-03-14 13:32:24 +1100 | [diff] [blame] | 1105 | p->name = (char *)dep->name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | |
Nathan Scott | f6d75cb | 2006-03-14 13:32:24 +1100 | [diff] [blame] | 1107 | error = p->put(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | |
| 1109 | /* |
| 1110 | * Won't fit. Return to caller. |
| 1111 | */ |
Nathan Scott | f6d75cb | 2006-03-14 13:32:24 +1100 | [diff] [blame] | 1112 | if (!p->done) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | eof = 0; |
| 1114 | break; |
| 1115 | } |
| 1116 | /* |
| 1117 | * Advance to next entry in the block. |
| 1118 | */ |
| 1119 | ptr += length; |
| 1120 | curoff += length; |
| 1121 | } |
| 1122 | |
| 1123 | /* |
| 1124 | * All done. Set output offset value to current offset. |
| 1125 | */ |
| 1126 | *eofp = eof; |
| 1127 | if (curoff > XFS_DIR2_DATAPTR_TO_BYTE(mp, XFS_DIR2_MAX_DATAPTR)) |
| 1128 | uio->uio_offset = XFS_DIR2_MAX_DATAPTR; |
| 1129 | else |
| 1130 | uio->uio_offset = XFS_DIR2_BYTE_TO_DATAPTR(mp, curoff); |
| 1131 | kmem_free(map, map_size * sizeof(*map)); |
Nathan Scott | f6d75cb | 2006-03-14 13:32:24 +1100 | [diff] [blame] | 1132 | kmem_free(p, sizeof(*p)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | if (bp) |
| 1134 | xfs_da_brelse(tp, bp); |
| 1135 | return error; |
| 1136 | } |
| 1137 | |
| 1138 | /* |
| 1139 | * Initialize a new leaf block, leaf1 or leafn magic accepted. |
| 1140 | */ |
| 1141 | int |
| 1142 | xfs_dir2_leaf_init( |
| 1143 | xfs_da_args_t *args, /* operation arguments */ |
| 1144 | xfs_dir2_db_t bno, /* directory block number */ |
| 1145 | xfs_dabuf_t **bpp, /* out: leaf buffer */ |
| 1146 | int magic) /* magic number for block */ |
| 1147 | { |
| 1148 | xfs_dabuf_t *bp; /* leaf buffer */ |
| 1149 | xfs_inode_t *dp; /* incore directory inode */ |
| 1150 | int error; /* error return code */ |
| 1151 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 1152 | xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */ |
| 1153 | xfs_mount_t *mp; /* filesystem mount point */ |
| 1154 | xfs_trans_t *tp; /* transaction pointer */ |
| 1155 | |
| 1156 | dp = args->dp; |
| 1157 | ASSERT(dp != NULL); |
| 1158 | tp = args->trans; |
| 1159 | mp = dp->i_mount; |
| 1160 | ASSERT(bno >= XFS_DIR2_LEAF_FIRSTDB(mp) && |
| 1161 | bno < XFS_DIR2_FREE_FIRSTDB(mp)); |
| 1162 | /* |
| 1163 | * Get the buffer for the block. |
| 1164 | */ |
| 1165 | error = xfs_da_get_buf(tp, dp, XFS_DIR2_DB_TO_DA(mp, bno), -1, &bp, |
| 1166 | XFS_DATA_FORK); |
| 1167 | if (error) { |
| 1168 | return error; |
| 1169 | } |
| 1170 | ASSERT(bp != NULL); |
| 1171 | leaf = bp->data; |
| 1172 | /* |
| 1173 | * Initialize the header. |
| 1174 | */ |
Nathan Scott | 89da054 | 2006-03-17 17:28:40 +1100 | [diff] [blame] | 1175 | leaf->hdr.info.magic = cpu_to_be16(magic); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | leaf->hdr.info.forw = 0; |
| 1177 | leaf->hdr.info.back = 0; |
| 1178 | leaf->hdr.count = 0; |
| 1179 | leaf->hdr.stale = 0; |
| 1180 | xfs_dir2_leaf_log_header(tp, bp); |
| 1181 | /* |
| 1182 | * If it's a leaf-format directory initialize the tail. |
| 1183 | * In this case our caller has the real bests table to copy into |
| 1184 | * the block. |
| 1185 | */ |
| 1186 | if (magic == XFS_DIR2_LEAF1_MAGIC) { |
| 1187 | ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf); |
| 1188 | ltp->bestcount = 0; |
| 1189 | xfs_dir2_leaf_log_tail(tp, bp); |
| 1190 | } |
| 1191 | *bpp = bp; |
| 1192 | return 0; |
| 1193 | } |
| 1194 | |
| 1195 | /* |
| 1196 | * Log the bests entries indicated from a leaf1 block. |
| 1197 | */ |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 1198 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | xfs_dir2_leaf_log_bests( |
| 1200 | xfs_trans_t *tp, /* transaction pointer */ |
| 1201 | xfs_dabuf_t *bp, /* leaf buffer */ |
| 1202 | int first, /* first entry to log */ |
| 1203 | int last) /* last entry to log */ |
| 1204 | { |
Nathan Scott | 68b3a10 | 2006-03-17 17:27:19 +1100 | [diff] [blame] | 1205 | __be16 *firstb; /* pointer to first entry */ |
| 1206 | __be16 *lastb; /* pointer to last entry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 1208 | xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */ |
| 1209 | |
| 1210 | leaf = bp->data; |
Nathan Scott | 89da054 | 2006-03-17 17:28:40 +1100 | [diff] [blame] | 1211 | ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | ltp = XFS_DIR2_LEAF_TAIL_P(tp->t_mountp, leaf); |
| 1213 | firstb = XFS_DIR2_LEAF_BESTS_P(ltp) + first; |
| 1214 | lastb = XFS_DIR2_LEAF_BESTS_P(ltp) + last; |
| 1215 | xfs_da_log_buf(tp, bp, (uint)((char *)firstb - (char *)leaf), |
| 1216 | (uint)((char *)lastb - (char *)leaf + sizeof(*lastb) - 1)); |
| 1217 | } |
| 1218 | |
| 1219 | /* |
| 1220 | * Log the leaf entries indicated from a leaf1 or leafn block. |
| 1221 | */ |
| 1222 | void |
| 1223 | xfs_dir2_leaf_log_ents( |
| 1224 | xfs_trans_t *tp, /* transaction pointer */ |
| 1225 | xfs_dabuf_t *bp, /* leaf buffer */ |
| 1226 | int first, /* first entry to log */ |
| 1227 | int last) /* last entry to log */ |
| 1228 | { |
| 1229 | xfs_dir2_leaf_entry_t *firstlep; /* pointer to first entry */ |
| 1230 | xfs_dir2_leaf_entry_t *lastlep; /* pointer to last entry */ |
| 1231 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 1232 | |
| 1233 | leaf = bp->data; |
Nathan Scott | 89da054 | 2006-03-17 17:28:40 +1100 | [diff] [blame] | 1234 | ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC || |
| 1235 | be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | firstlep = &leaf->ents[first]; |
| 1237 | lastlep = &leaf->ents[last]; |
| 1238 | xfs_da_log_buf(tp, bp, (uint)((char *)firstlep - (char *)leaf), |
| 1239 | (uint)((char *)lastlep - (char *)leaf + sizeof(*lastlep) - 1)); |
| 1240 | } |
| 1241 | |
| 1242 | /* |
| 1243 | * Log the header of the leaf1 or leafn block. |
| 1244 | */ |
| 1245 | void |
| 1246 | xfs_dir2_leaf_log_header( |
| 1247 | xfs_trans_t *tp, /* transaction pointer */ |
| 1248 | xfs_dabuf_t *bp) /* leaf buffer */ |
| 1249 | { |
| 1250 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 1251 | |
| 1252 | leaf = bp->data; |
Nathan Scott | 89da054 | 2006-03-17 17:28:40 +1100 | [diff] [blame] | 1253 | ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC || |
| 1254 | be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1255 | xfs_da_log_buf(tp, bp, (uint)((char *)&leaf->hdr - (char *)leaf), |
| 1256 | (uint)(sizeof(leaf->hdr) - 1)); |
| 1257 | } |
| 1258 | |
| 1259 | /* |
| 1260 | * Log the tail of the leaf1 block. |
| 1261 | */ |
Christoph Hellwig | ba0f32d | 2005-06-21 15:36:52 +1000 | [diff] [blame] | 1262 | STATIC void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | xfs_dir2_leaf_log_tail( |
| 1264 | xfs_trans_t *tp, /* transaction pointer */ |
| 1265 | xfs_dabuf_t *bp) /* leaf buffer */ |
| 1266 | { |
| 1267 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 1268 | xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */ |
| 1269 | xfs_mount_t *mp; /* filesystem mount point */ |
| 1270 | |
| 1271 | mp = tp->t_mountp; |
| 1272 | leaf = bp->data; |
Nathan Scott | 89da054 | 2006-03-17 17:28:40 +1100 | [diff] [blame] | 1273 | ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAF1_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf); |
| 1275 | xfs_da_log_buf(tp, bp, (uint)((char *)ltp - (char *)leaf), |
| 1276 | (uint)(mp->m_dirblksize - 1)); |
| 1277 | } |
| 1278 | |
| 1279 | /* |
| 1280 | * Look up the entry referred to by args in the leaf format directory. |
| 1281 | * Most of the work is done by the xfs_dir2_leaf_lookup_int routine which |
| 1282 | * is also used by the node-format code. |
| 1283 | */ |
| 1284 | int |
| 1285 | xfs_dir2_leaf_lookup( |
| 1286 | xfs_da_args_t *args) /* operation arguments */ |
| 1287 | { |
| 1288 | xfs_dabuf_t *dbp; /* data block buffer */ |
| 1289 | xfs_dir2_data_entry_t *dep; /* data block entry */ |
| 1290 | xfs_inode_t *dp; /* incore directory inode */ |
| 1291 | int error; /* error return code */ |
| 1292 | int index; /* found entry index */ |
| 1293 | xfs_dabuf_t *lbp; /* leaf buffer */ |
| 1294 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 1295 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ |
| 1296 | xfs_trans_t *tp; /* transaction pointer */ |
| 1297 | |
| 1298 | xfs_dir2_trace_args("leaf_lookup", args); |
| 1299 | /* |
| 1300 | * Look up name in the leaf block, returning both buffers and index. |
| 1301 | */ |
| 1302 | if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) { |
| 1303 | return error; |
| 1304 | } |
| 1305 | tp = args->trans; |
| 1306 | dp = args->dp; |
| 1307 | xfs_dir2_leaf_check(dp, lbp); |
| 1308 | leaf = lbp->data; |
| 1309 | /* |
| 1310 | * Get to the leaf entry and contained data entry address. |
| 1311 | */ |
| 1312 | lep = &leaf->ents[index]; |
| 1313 | /* |
| 1314 | * Point to the data entry. |
| 1315 | */ |
| 1316 | dep = (xfs_dir2_data_entry_t *) |
| 1317 | ((char *)dbp->data + |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 1318 | XFS_DIR2_DATAPTR_TO_OFF(dp->i_mount, be32_to_cpu(lep->address))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | /* |
| 1320 | * Return the found inode number. |
| 1321 | */ |
| 1322 | args->inumber = INT_GET(dep->inumber, ARCH_CONVERT); |
| 1323 | xfs_da_brelse(tp, dbp); |
| 1324 | xfs_da_brelse(tp, lbp); |
| 1325 | return XFS_ERROR(EEXIST); |
| 1326 | } |
| 1327 | |
| 1328 | /* |
| 1329 | * Look up name/hash in the leaf block. |
| 1330 | * Fill in indexp with the found index, and dbpp with the data buffer. |
| 1331 | * If not found dbpp will be NULL, and ENOENT comes back. |
| 1332 | * lbpp will always be filled in with the leaf buffer unless there's an error. |
| 1333 | */ |
| 1334 | static int /* error */ |
| 1335 | xfs_dir2_leaf_lookup_int( |
| 1336 | xfs_da_args_t *args, /* operation arguments */ |
| 1337 | xfs_dabuf_t **lbpp, /* out: leaf buffer */ |
| 1338 | int *indexp, /* out: index in leaf block */ |
| 1339 | xfs_dabuf_t **dbpp) /* out: data buffer */ |
| 1340 | { |
| 1341 | xfs_dir2_db_t curdb; /* current data block number */ |
| 1342 | xfs_dabuf_t *dbp; /* data buffer */ |
| 1343 | xfs_dir2_data_entry_t *dep; /* data entry */ |
| 1344 | xfs_inode_t *dp; /* incore directory inode */ |
| 1345 | int error; /* error return code */ |
| 1346 | int index; /* index in leaf block */ |
| 1347 | xfs_dabuf_t *lbp; /* leaf buffer */ |
| 1348 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ |
| 1349 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 1350 | xfs_mount_t *mp; /* filesystem mount point */ |
| 1351 | xfs_dir2_db_t newdb; /* new data block number */ |
| 1352 | xfs_trans_t *tp; /* transaction pointer */ |
| 1353 | |
| 1354 | dp = args->dp; |
| 1355 | tp = args->trans; |
| 1356 | mp = dp->i_mount; |
| 1357 | /* |
| 1358 | * Read the leaf block into the buffer. |
| 1359 | */ |
| 1360 | if ((error = |
| 1361 | xfs_da_read_buf(tp, dp, mp->m_dirleafblk, -1, &lbp, |
| 1362 | XFS_DATA_FORK))) { |
| 1363 | return error; |
| 1364 | } |
| 1365 | *lbpp = lbp; |
| 1366 | leaf = lbp->data; |
| 1367 | xfs_dir2_leaf_check(dp, lbp); |
| 1368 | /* |
| 1369 | * Look for the first leaf entry with our hash value. |
| 1370 | */ |
| 1371 | index = xfs_dir2_leaf_search_hash(args, lbp); |
| 1372 | /* |
| 1373 | * Loop over all the entries with the right hash value |
| 1374 | * looking to match the name. |
| 1375 | */ |
| 1376 | for (lep = &leaf->ents[index], dbp = NULL, curdb = -1; |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 1377 | index < be16_to_cpu(leaf->hdr.count) && be32_to_cpu(lep->hashval) == args->hashval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | lep++, index++) { |
| 1379 | /* |
| 1380 | * Skip over stale leaf entries. |
| 1381 | */ |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 1382 | if (be32_to_cpu(lep->address) == XFS_DIR2_NULL_DATAPTR) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1383 | continue; |
| 1384 | /* |
| 1385 | * Get the new data block number. |
| 1386 | */ |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 1387 | newdb = XFS_DIR2_DATAPTR_TO_DB(mp, be32_to_cpu(lep->address)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1388 | /* |
| 1389 | * If it's not the same as the old data block number, |
| 1390 | * need to pitch the old one and read the new one. |
| 1391 | */ |
| 1392 | if (newdb != curdb) { |
| 1393 | if (dbp) |
| 1394 | xfs_da_brelse(tp, dbp); |
| 1395 | if ((error = |
| 1396 | xfs_da_read_buf(tp, dp, |
| 1397 | XFS_DIR2_DB_TO_DA(mp, newdb), -1, &dbp, |
| 1398 | XFS_DATA_FORK))) { |
| 1399 | xfs_da_brelse(tp, lbp); |
| 1400 | return error; |
| 1401 | } |
| 1402 | xfs_dir2_data_check(dp, dbp); |
| 1403 | curdb = newdb; |
| 1404 | } |
| 1405 | /* |
| 1406 | * Point to the data entry. |
| 1407 | */ |
| 1408 | dep = (xfs_dir2_data_entry_t *) |
| 1409 | ((char *)dbp->data + |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 1410 | XFS_DIR2_DATAPTR_TO_OFF(mp, be32_to_cpu(lep->address))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1411 | /* |
| 1412 | * If it matches then return it. |
| 1413 | */ |
| 1414 | if (dep->namelen == args->namelen && |
| 1415 | dep->name[0] == args->name[0] && |
| 1416 | memcmp(dep->name, args->name, args->namelen) == 0) { |
| 1417 | *dbpp = dbp; |
| 1418 | *indexp = index; |
| 1419 | return 0; |
| 1420 | } |
| 1421 | } |
| 1422 | /* |
| 1423 | * No match found, return ENOENT. |
| 1424 | */ |
| 1425 | ASSERT(args->oknoent); |
| 1426 | if (dbp) |
| 1427 | xfs_da_brelse(tp, dbp); |
| 1428 | xfs_da_brelse(tp, lbp); |
| 1429 | return XFS_ERROR(ENOENT); |
| 1430 | } |
| 1431 | |
| 1432 | /* |
| 1433 | * Remove an entry from a leaf format directory. |
| 1434 | */ |
| 1435 | int /* error */ |
| 1436 | xfs_dir2_leaf_removename( |
| 1437 | xfs_da_args_t *args) /* operation arguments */ |
| 1438 | { |
Nathan Scott | 68b3a10 | 2006-03-17 17:27:19 +1100 | [diff] [blame] | 1439 | __be16 *bestsp; /* leaf block best freespace */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1440 | xfs_dir2_data_t *data; /* data block structure */ |
| 1441 | xfs_dir2_db_t db; /* data block number */ |
| 1442 | xfs_dabuf_t *dbp; /* data block buffer */ |
| 1443 | xfs_dir2_data_entry_t *dep; /* data entry structure */ |
| 1444 | xfs_inode_t *dp; /* incore directory inode */ |
| 1445 | int error; /* error return code */ |
| 1446 | xfs_dir2_db_t i; /* temporary data block # */ |
| 1447 | int index; /* index into leaf entries */ |
| 1448 | xfs_dabuf_t *lbp; /* leaf buffer */ |
| 1449 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 1450 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ |
| 1451 | xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */ |
| 1452 | xfs_mount_t *mp; /* filesystem mount point */ |
| 1453 | int needlog; /* need to log data header */ |
| 1454 | int needscan; /* need to rescan data frees */ |
| 1455 | xfs_dir2_data_off_t oldbest; /* old value of best free */ |
| 1456 | xfs_trans_t *tp; /* transaction pointer */ |
| 1457 | |
| 1458 | xfs_dir2_trace_args("leaf_removename", args); |
| 1459 | /* |
| 1460 | * Lookup the leaf entry, get the leaf and data blocks read in. |
| 1461 | */ |
| 1462 | if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) { |
| 1463 | return error; |
| 1464 | } |
| 1465 | dp = args->dp; |
| 1466 | tp = args->trans; |
| 1467 | mp = dp->i_mount; |
| 1468 | leaf = lbp->data; |
| 1469 | data = dbp->data; |
| 1470 | xfs_dir2_data_check(dp, dbp); |
| 1471 | /* |
| 1472 | * Point to the leaf entry, use that to point to the data entry. |
| 1473 | */ |
| 1474 | lep = &leaf->ents[index]; |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 1475 | db = XFS_DIR2_DATAPTR_TO_DB(mp, be32_to_cpu(lep->address)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | dep = (xfs_dir2_data_entry_t *) |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 1477 | ((char *)data + XFS_DIR2_DATAPTR_TO_OFF(mp, be32_to_cpu(lep->address))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | needscan = needlog = 0; |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 1479 | oldbest = be16_to_cpu(data->hdr.bestfree[0].length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf); |
| 1481 | bestsp = XFS_DIR2_LEAF_BESTS_P(ltp); |
Nathan Scott | 68b3a10 | 2006-03-17 17:27:19 +1100 | [diff] [blame] | 1482 | ASSERT(be16_to_cpu(bestsp[db]) == oldbest); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | /* |
| 1484 | * Mark the former data entry unused. |
| 1485 | */ |
| 1486 | xfs_dir2_data_make_free(tp, dbp, |
| 1487 | (xfs_dir2_data_aoff_t)((char *)dep - (char *)data), |
| 1488 | XFS_DIR2_DATA_ENTSIZE(dep->namelen), &needlog, &needscan); |
| 1489 | /* |
| 1490 | * We just mark the leaf entry stale by putting a null in it. |
| 1491 | */ |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 1492 | be16_add(&leaf->hdr.stale, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | xfs_dir2_leaf_log_header(tp, lbp); |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 1494 | lep->address = cpu_to_be32(XFS_DIR2_NULL_DATAPTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1495 | xfs_dir2_leaf_log_ents(tp, lbp, index, index); |
| 1496 | /* |
| 1497 | * Scan the freespace in the data block again if necessary, |
| 1498 | * log the data block header if necessary. |
| 1499 | */ |
| 1500 | if (needscan) |
| 1501 | xfs_dir2_data_freescan(mp, data, &needlog, NULL); |
| 1502 | if (needlog) |
| 1503 | xfs_dir2_data_log_header(tp, dbp); |
| 1504 | /* |
| 1505 | * If the longest freespace in the data block has changed, |
| 1506 | * put the new value in the bests table and log that. |
| 1507 | */ |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 1508 | if (be16_to_cpu(data->hdr.bestfree[0].length) != oldbest) { |
| 1509 | bestsp[db] = data->hdr.bestfree[0].length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1510 | xfs_dir2_leaf_log_bests(tp, lbp, db, db); |
| 1511 | } |
| 1512 | xfs_dir2_data_check(dp, dbp); |
| 1513 | /* |
| 1514 | * If the data block is now empty then get rid of the data block. |
| 1515 | */ |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 1516 | if (be16_to_cpu(data->hdr.bestfree[0].length) == |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | mp->m_dirblksize - (uint)sizeof(data->hdr)) { |
| 1518 | ASSERT(db != mp->m_dirdatablk); |
| 1519 | if ((error = xfs_dir2_shrink_inode(args, db, dbp))) { |
| 1520 | /* |
| 1521 | * Nope, can't get rid of it because it caused |
| 1522 | * allocation of a bmap btree block to do so. |
| 1523 | * Just go on, returning success, leaving the |
| 1524 | * empty block in place. |
| 1525 | */ |
| 1526 | if (error == ENOSPC && args->total == 0) { |
| 1527 | xfs_da_buf_done(dbp); |
| 1528 | error = 0; |
| 1529 | } |
| 1530 | xfs_dir2_leaf_check(dp, lbp); |
| 1531 | xfs_da_buf_done(lbp); |
| 1532 | return error; |
| 1533 | } |
| 1534 | dbp = NULL; |
| 1535 | /* |
| 1536 | * If this is the last data block then compact the |
| 1537 | * bests table by getting rid of entries. |
| 1538 | */ |
Nathan Scott | afbcb3f | 2006-03-17 17:27:28 +1100 | [diff] [blame] | 1539 | if (db == be32_to_cpu(ltp->bestcount) - 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | /* |
| 1541 | * Look for the last active entry (i). |
| 1542 | */ |
| 1543 | for (i = db - 1; i > 0; i--) { |
Nathan Scott | 68b3a10 | 2006-03-17 17:27:19 +1100 | [diff] [blame] | 1544 | if (be16_to_cpu(bestsp[i]) != NULLDATAOFF) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1545 | break; |
| 1546 | } |
| 1547 | /* |
| 1548 | * Copy the table down so inactive entries at the |
| 1549 | * end are removed. |
| 1550 | */ |
| 1551 | memmove(&bestsp[db - i], bestsp, |
Nathan Scott | afbcb3f | 2006-03-17 17:27:28 +1100 | [diff] [blame] | 1552 | (be32_to_cpu(ltp->bestcount) - (db - i)) * sizeof(*bestsp)); |
| 1553 | be32_add(<p->bestcount, -(db - i)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | xfs_dir2_leaf_log_tail(tp, lbp); |
Nathan Scott | afbcb3f | 2006-03-17 17:27:28 +1100 | [diff] [blame] | 1555 | xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1556 | } else |
Nathan Scott | 68b3a10 | 2006-03-17 17:27:19 +1100 | [diff] [blame] | 1557 | bestsp[db] = cpu_to_be16(NULLDATAOFF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | } |
| 1559 | /* |
| 1560 | * If the data block was not the first one, drop it. |
| 1561 | */ |
| 1562 | else if (db != mp->m_dirdatablk && dbp != NULL) { |
| 1563 | xfs_da_buf_done(dbp); |
| 1564 | dbp = NULL; |
| 1565 | } |
| 1566 | xfs_dir2_leaf_check(dp, lbp); |
| 1567 | /* |
| 1568 | * See if we can convert to block form. |
| 1569 | */ |
| 1570 | return xfs_dir2_leaf_to_block(args, lbp, dbp); |
| 1571 | } |
| 1572 | |
| 1573 | /* |
| 1574 | * Replace the inode number in a leaf format directory entry. |
| 1575 | */ |
| 1576 | int /* error */ |
| 1577 | xfs_dir2_leaf_replace( |
| 1578 | xfs_da_args_t *args) /* operation arguments */ |
| 1579 | { |
| 1580 | xfs_dabuf_t *dbp; /* data block buffer */ |
| 1581 | xfs_dir2_data_entry_t *dep; /* data block entry */ |
| 1582 | xfs_inode_t *dp; /* incore directory inode */ |
| 1583 | int error; /* error return code */ |
| 1584 | int index; /* index of leaf entry */ |
| 1585 | xfs_dabuf_t *lbp; /* leaf buffer */ |
| 1586 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 1587 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ |
| 1588 | xfs_trans_t *tp; /* transaction pointer */ |
| 1589 | |
| 1590 | xfs_dir2_trace_args("leaf_replace", args); |
| 1591 | /* |
| 1592 | * Look up the entry. |
| 1593 | */ |
| 1594 | if ((error = xfs_dir2_leaf_lookup_int(args, &lbp, &index, &dbp))) { |
| 1595 | return error; |
| 1596 | } |
| 1597 | dp = args->dp; |
| 1598 | leaf = lbp->data; |
| 1599 | /* |
| 1600 | * Point to the leaf entry, get data address from it. |
| 1601 | */ |
| 1602 | lep = &leaf->ents[index]; |
| 1603 | /* |
| 1604 | * Point to the data entry. |
| 1605 | */ |
| 1606 | dep = (xfs_dir2_data_entry_t *) |
| 1607 | ((char *)dbp->data + |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 1608 | XFS_DIR2_DATAPTR_TO_OFF(dp->i_mount, be32_to_cpu(lep->address))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1609 | ASSERT(args->inumber != INT_GET(dep->inumber, ARCH_CONVERT)); |
| 1610 | /* |
| 1611 | * Put the new inode number in, log it. |
| 1612 | */ |
| 1613 | INT_SET(dep->inumber, ARCH_CONVERT, args->inumber); |
| 1614 | tp = args->trans; |
| 1615 | xfs_dir2_data_log_entry(tp, dbp, dep); |
| 1616 | xfs_da_buf_done(dbp); |
| 1617 | xfs_dir2_leaf_check(dp, lbp); |
| 1618 | xfs_da_brelse(tp, lbp); |
| 1619 | return 0; |
| 1620 | } |
| 1621 | |
| 1622 | /* |
| 1623 | * Return index in the leaf block (lbp) which is either the first |
| 1624 | * one with this hash value, or if there are none, the insert point |
| 1625 | * for that hash value. |
| 1626 | */ |
| 1627 | int /* index value */ |
| 1628 | xfs_dir2_leaf_search_hash( |
| 1629 | xfs_da_args_t *args, /* operation arguments */ |
| 1630 | xfs_dabuf_t *lbp) /* leaf buffer */ |
| 1631 | { |
| 1632 | xfs_dahash_t hash=0; /* hash from this entry */ |
| 1633 | xfs_dahash_t hashwant; /* hash value looking for */ |
| 1634 | int high; /* high leaf index */ |
| 1635 | int low; /* low leaf index */ |
| 1636 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 1637 | xfs_dir2_leaf_entry_t *lep; /* leaf entry */ |
| 1638 | int mid=0; /* current leaf index */ |
| 1639 | |
| 1640 | leaf = lbp->data; |
| 1641 | #ifndef __KERNEL__ |
| 1642 | if (!leaf->hdr.count) |
| 1643 | return 0; |
| 1644 | #endif |
| 1645 | /* |
| 1646 | * Note, the table cannot be empty, so we have to go through the loop. |
| 1647 | * Binary search the leaf entries looking for our hash value. |
| 1648 | */ |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 1649 | for (lep = leaf->ents, low = 0, high = be16_to_cpu(leaf->hdr.count) - 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1650 | hashwant = args->hashval; |
| 1651 | low <= high; ) { |
| 1652 | mid = (low + high) >> 1; |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 1653 | if ((hash = be32_to_cpu(lep[mid].hashval)) == hashwant) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1654 | break; |
| 1655 | if (hash < hashwant) |
| 1656 | low = mid + 1; |
| 1657 | else |
| 1658 | high = mid - 1; |
| 1659 | } |
| 1660 | /* |
| 1661 | * Found one, back up through all the equal hash values. |
| 1662 | */ |
| 1663 | if (hash == hashwant) { |
Nathan Scott | 3c1f9c1 | 2006-03-17 17:28:18 +1100 | [diff] [blame] | 1664 | while (mid > 0 && be32_to_cpu(lep[mid - 1].hashval) == hashwant) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1665 | mid--; |
| 1666 | } |
| 1667 | } |
| 1668 | /* |
| 1669 | * Need to point to an entry higher than ours. |
| 1670 | */ |
| 1671 | else if (hash < hashwant) |
| 1672 | mid++; |
| 1673 | return mid; |
| 1674 | } |
| 1675 | |
| 1676 | /* |
| 1677 | * Trim off a trailing data block. We know it's empty since the leaf |
| 1678 | * freespace table says so. |
| 1679 | */ |
| 1680 | int /* error */ |
| 1681 | xfs_dir2_leaf_trim_data( |
| 1682 | xfs_da_args_t *args, /* operation arguments */ |
| 1683 | xfs_dabuf_t *lbp, /* leaf buffer */ |
| 1684 | xfs_dir2_db_t db) /* data block number */ |
| 1685 | { |
Nathan Scott | 68b3a10 | 2006-03-17 17:27:19 +1100 | [diff] [blame] | 1686 | __be16 *bestsp; /* leaf bests table */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1687 | #ifdef DEBUG |
| 1688 | xfs_dir2_data_t *data; /* data block structure */ |
| 1689 | #endif |
| 1690 | xfs_dabuf_t *dbp; /* data block buffer */ |
| 1691 | xfs_inode_t *dp; /* incore directory inode */ |
| 1692 | int error; /* error return value */ |
| 1693 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 1694 | xfs_dir2_leaf_tail_t *ltp; /* leaf tail structure */ |
| 1695 | xfs_mount_t *mp; /* filesystem mount point */ |
| 1696 | xfs_trans_t *tp; /* transaction pointer */ |
| 1697 | |
| 1698 | dp = args->dp; |
| 1699 | mp = dp->i_mount; |
| 1700 | tp = args->trans; |
| 1701 | /* |
| 1702 | * Read the offending data block. We need its buffer. |
| 1703 | */ |
| 1704 | if ((error = xfs_da_read_buf(tp, dp, XFS_DIR2_DB_TO_DA(mp, db), -1, &dbp, |
| 1705 | XFS_DATA_FORK))) { |
| 1706 | return error; |
| 1707 | } |
| 1708 | #ifdef DEBUG |
| 1709 | data = dbp->data; |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 1710 | ASSERT(be32_to_cpu(data->hdr.magic) == XFS_DIR2_DATA_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1711 | #endif |
| 1712 | /* this seems to be an error |
| 1713 | * data is only valid if DEBUG is defined? |
| 1714 | * RMC 09/08/1999 |
| 1715 | */ |
| 1716 | |
| 1717 | leaf = lbp->data; |
| 1718 | ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf); |
Nathan Scott | 70e73f5 | 2006-03-17 17:26:52 +1100 | [diff] [blame] | 1719 | ASSERT(be16_to_cpu(data->hdr.bestfree[0].length) == |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | mp->m_dirblksize - (uint)sizeof(data->hdr)); |
Nathan Scott | afbcb3f | 2006-03-17 17:27:28 +1100 | [diff] [blame] | 1721 | ASSERT(db == be32_to_cpu(ltp->bestcount) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1722 | /* |
| 1723 | * Get rid of the data block. |
| 1724 | */ |
| 1725 | if ((error = xfs_dir2_shrink_inode(args, db, dbp))) { |
| 1726 | ASSERT(error != ENOSPC); |
| 1727 | xfs_da_brelse(tp, dbp); |
| 1728 | return error; |
| 1729 | } |
| 1730 | /* |
| 1731 | * Eliminate the last bests entry from the table. |
| 1732 | */ |
| 1733 | bestsp = XFS_DIR2_LEAF_BESTS_P(ltp); |
Nathan Scott | afbcb3f | 2006-03-17 17:27:28 +1100 | [diff] [blame] | 1734 | be32_add(<p->bestcount, -1); |
| 1735 | memmove(&bestsp[1], &bestsp[0], be32_to_cpu(ltp->bestcount) * sizeof(*bestsp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1736 | xfs_dir2_leaf_log_tail(tp, lbp); |
Nathan Scott | afbcb3f | 2006-03-17 17:27:28 +1100 | [diff] [blame] | 1737 | xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1738 | return 0; |
| 1739 | } |
| 1740 | |
| 1741 | /* |
| 1742 | * Convert node form directory to leaf form directory. |
| 1743 | * The root of the node form dir needs to already be a LEAFN block. |
| 1744 | * Just return if we can't do anything. |
| 1745 | */ |
| 1746 | int /* error */ |
| 1747 | xfs_dir2_node_to_leaf( |
| 1748 | xfs_da_state_t *state) /* directory operation state */ |
| 1749 | { |
| 1750 | xfs_da_args_t *args; /* operation arguments */ |
| 1751 | xfs_inode_t *dp; /* incore directory inode */ |
| 1752 | int error; /* error return code */ |
| 1753 | xfs_dabuf_t *fbp; /* buffer for freespace block */ |
| 1754 | xfs_fileoff_t fo; /* freespace file offset */ |
| 1755 | xfs_dir2_free_t *free; /* freespace structure */ |
| 1756 | xfs_dabuf_t *lbp; /* buffer for leaf block */ |
| 1757 | xfs_dir2_leaf_tail_t *ltp; /* tail of leaf structure */ |
| 1758 | xfs_dir2_leaf_t *leaf; /* leaf structure */ |
| 1759 | xfs_mount_t *mp; /* filesystem mount point */ |
| 1760 | int rval; /* successful free trim? */ |
| 1761 | xfs_trans_t *tp; /* transaction pointer */ |
| 1762 | |
| 1763 | /* |
| 1764 | * There's more than a leaf level in the btree, so there must |
| 1765 | * be multiple leafn blocks. Give up. |
| 1766 | */ |
| 1767 | if (state->path.active > 1) |
| 1768 | return 0; |
| 1769 | args = state->args; |
| 1770 | xfs_dir2_trace_args("node_to_leaf", args); |
| 1771 | mp = state->mp; |
| 1772 | dp = args->dp; |
| 1773 | tp = args->trans; |
| 1774 | /* |
| 1775 | * Get the last offset in the file. |
| 1776 | */ |
| 1777 | if ((error = xfs_bmap_last_offset(tp, dp, &fo, XFS_DATA_FORK))) { |
| 1778 | return error; |
| 1779 | } |
| 1780 | fo -= mp->m_dirblkfsbs; |
| 1781 | /* |
| 1782 | * If there are freespace blocks other than the first one, |
| 1783 | * take this opportunity to remove trailing empty freespace blocks |
| 1784 | * that may have been left behind during no-space-reservation |
| 1785 | * operations. |
| 1786 | */ |
| 1787 | while (fo > mp->m_dirfreeblk) { |
| 1788 | if ((error = xfs_dir2_node_trim_free(args, fo, &rval))) { |
| 1789 | return error; |
| 1790 | } |
| 1791 | if (rval) |
| 1792 | fo -= mp->m_dirblkfsbs; |
| 1793 | else |
| 1794 | return 0; |
| 1795 | } |
| 1796 | /* |
| 1797 | * Now find the block just before the freespace block. |
| 1798 | */ |
| 1799 | if ((error = xfs_bmap_last_before(tp, dp, &fo, XFS_DATA_FORK))) { |
| 1800 | return error; |
| 1801 | } |
| 1802 | /* |
| 1803 | * If it's not the single leaf block, give up. |
| 1804 | */ |
| 1805 | if (XFS_FSB_TO_B(mp, fo) > XFS_DIR2_LEAF_OFFSET + mp->m_dirblksize) |
| 1806 | return 0; |
| 1807 | lbp = state->path.blk[0].bp; |
| 1808 | leaf = lbp->data; |
Nathan Scott | 89da054 | 2006-03-17 17:28:40 +1100 | [diff] [blame] | 1809 | ASSERT(be16_to_cpu(leaf->hdr.info.magic) == XFS_DIR2_LEAFN_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1810 | /* |
| 1811 | * Read the freespace block. |
| 1812 | */ |
| 1813 | if ((error = xfs_da_read_buf(tp, dp, mp->m_dirfreeblk, -1, &fbp, |
| 1814 | XFS_DATA_FORK))) { |
| 1815 | return error; |
| 1816 | } |
| 1817 | free = fbp->data; |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 1818 | ASSERT(be32_to_cpu(free->hdr.magic) == XFS_DIR2_FREE_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | ASSERT(!free->hdr.firstdb); |
| 1820 | /* |
| 1821 | * Now see if the leafn and free data will fit in a leaf1. |
| 1822 | * If not, release the buffer and give up. |
| 1823 | */ |
| 1824 | if ((uint)sizeof(leaf->hdr) + |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 1825 | (be16_to_cpu(leaf->hdr.count) - be16_to_cpu(leaf->hdr.stale)) * (uint)sizeof(leaf->ents[0]) + |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 1826 | be32_to_cpu(free->hdr.nvalid) * (uint)sizeof(leaf->bests[0]) + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1827 | (uint)sizeof(leaf->tail) > |
| 1828 | mp->m_dirblksize) { |
| 1829 | xfs_da_brelse(tp, fbp); |
| 1830 | return 0; |
| 1831 | } |
| 1832 | /* |
| 1833 | * If the leaf has any stale entries in it, compress them out. |
| 1834 | * The compact routine will log the header. |
| 1835 | */ |
Nathan Scott | a818e5d | 2006-03-17 17:28:07 +1100 | [diff] [blame] | 1836 | if (be16_to_cpu(leaf->hdr.stale)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1837 | xfs_dir2_leaf_compact(args, lbp); |
| 1838 | else |
| 1839 | xfs_dir2_leaf_log_header(tp, lbp); |
Nathan Scott | 89da054 | 2006-03-17 17:28:40 +1100 | [diff] [blame] | 1840 | leaf->hdr.info.magic = cpu_to_be16(XFS_DIR2_LEAF1_MAGIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | /* |
| 1842 | * Set up the leaf tail from the freespace block. |
| 1843 | */ |
| 1844 | ltp = XFS_DIR2_LEAF_TAIL_P(mp, leaf); |
Nathan Scott | 0ba962e | 2006-03-17 17:27:07 +1100 | [diff] [blame] | 1845 | ltp->bestcount = free->hdr.nvalid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1846 | /* |
| 1847 | * Set up the leaf bests table. |
| 1848 | */ |
| 1849 | memcpy(XFS_DIR2_LEAF_BESTS_P(ltp), free->bests, |
Nathan Scott | afbcb3f | 2006-03-17 17:27:28 +1100 | [diff] [blame] | 1850 | be32_to_cpu(ltp->bestcount) * sizeof(leaf->bests[0])); |
| 1851 | xfs_dir2_leaf_log_bests(tp, lbp, 0, be32_to_cpu(ltp->bestcount) - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1852 | xfs_dir2_leaf_log_tail(tp, lbp); |
| 1853 | xfs_dir2_leaf_check(dp, lbp); |
| 1854 | /* |
| 1855 | * Get rid of the freespace block. |
| 1856 | */ |
| 1857 | error = xfs_dir2_shrink_inode(args, XFS_DIR2_FREE_FIRSTDB(mp), fbp); |
| 1858 | if (error) { |
| 1859 | /* |
| 1860 | * This can't fail here because it can only happen when |
| 1861 | * punching out the middle of an extent, and this is an |
| 1862 | * isolated block. |
| 1863 | */ |
| 1864 | ASSERT(error != ENOSPC); |
| 1865 | return error; |
| 1866 | } |
| 1867 | fbp = NULL; |
| 1868 | /* |
| 1869 | * Now see if we can convert the single-leaf directory |
| 1870 | * down to a block form directory. |
| 1871 | * This routine always kills the dabuf for the leaf, so |
| 1872 | * eliminate it from the path. |
| 1873 | */ |
| 1874 | error = xfs_dir2_leaf_to_block(args, lbp, NULL); |
| 1875 | state->path.blk[0].bp = NULL; |
| 1876 | return error; |
| 1877 | } |