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