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