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