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