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