b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc. |
| 4 | * Copyright (c) 2013 Red Hat, Inc. |
| 5 | * All Rights Reserved. |
| 6 | */ |
| 7 | #include "xfs.h" |
| 8 | #include "xfs_fs.h" |
| 9 | #include "xfs_shared.h" |
| 10 | #include "xfs_format.h" |
| 11 | #include "xfs_log_format.h" |
| 12 | #include "xfs_trans_resv.h" |
| 13 | #include "xfs_mount.h" |
| 14 | #include "xfs_inode.h" |
| 15 | #include "xfs_dir2.h" |
| 16 | #include "xfs_error.h" |
| 17 | #include "xfs_trans.h" |
| 18 | #include "xfs_buf_item.h" |
| 19 | #include "xfs_log.h" |
| 20 | |
| 21 | static xfs_failaddr_t xfs_dir2_data_freefind_verify( |
| 22 | struct xfs_dir2_data_hdr *hdr, struct xfs_dir2_data_free *bf, |
| 23 | struct xfs_dir2_data_unused *dup, |
| 24 | struct xfs_dir2_data_free **bf_ent); |
| 25 | |
| 26 | /* |
| 27 | * Check the consistency of the data block. |
| 28 | * The input can also be a block-format directory. |
| 29 | * Return NULL if the buffer is good, otherwise the address of the error. |
| 30 | */ |
| 31 | xfs_failaddr_t |
| 32 | __xfs_dir3_data_check( |
| 33 | struct xfs_inode *dp, /* incore inode pointer */ |
| 34 | struct xfs_buf *bp) /* data block's buffer */ |
| 35 | { |
| 36 | xfs_dir2_dataptr_t addr; /* addr for leaf lookup */ |
| 37 | xfs_dir2_data_free_t *bf; /* bestfree table */ |
| 38 | xfs_dir2_block_tail_t *btp=NULL; /* block tail */ |
| 39 | int count; /* count of entries found */ |
| 40 | xfs_dir2_data_hdr_t *hdr; /* data block header */ |
| 41 | xfs_dir2_data_entry_t *dep; /* data entry */ |
| 42 | xfs_dir2_data_free_t *dfp; /* bestfree entry */ |
| 43 | xfs_dir2_data_unused_t *dup; /* unused entry */ |
| 44 | char *endp; /* end of useful data */ |
| 45 | int freeseen; /* mask of bestfrees seen */ |
| 46 | xfs_dahash_t hash; /* hash of current name */ |
| 47 | int i; /* leaf index */ |
| 48 | int lastfree; /* last entry was unused */ |
| 49 | xfs_dir2_leaf_entry_t *lep=NULL; /* block leaf entries */ |
| 50 | struct xfs_mount *mp = bp->b_mount; |
| 51 | char *p; /* current data position */ |
| 52 | int stale; /* count of stale leaves */ |
| 53 | struct xfs_name name; |
| 54 | const struct xfs_dir_ops *ops; |
| 55 | struct xfs_da_geometry *geo; |
| 56 | |
| 57 | geo = mp->m_dir_geo; |
| 58 | |
| 59 | /* |
| 60 | * We can be passed a null dp here from a verifier, so we need to go the |
| 61 | * hard way to get them. |
| 62 | */ |
| 63 | ops = xfs_dir_get_ops(mp, dp); |
| 64 | |
| 65 | /* |
| 66 | * If this isn't a directory, or we don't get handed the dir ops, |
| 67 | * something is seriously wrong. Bail out. |
| 68 | */ |
| 69 | if ((dp && !S_ISDIR(VFS_I(dp)->i_mode)) || |
| 70 | ops != xfs_dir_get_ops(mp, NULL)) |
| 71 | return __this_address; |
| 72 | |
| 73 | hdr = bp->b_addr; |
| 74 | p = (char *)ops->data_entry_p(hdr); |
| 75 | |
| 76 | switch (hdr->magic) { |
| 77 | case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC): |
| 78 | case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC): |
| 79 | btp = xfs_dir2_block_tail_p(geo, hdr); |
| 80 | lep = xfs_dir2_block_leaf_p(btp); |
| 81 | |
| 82 | /* |
| 83 | * The number of leaf entries is limited by the size of the |
| 84 | * block and the amount of space used by the data entries. |
| 85 | * We don't know how much space is used by the data entries yet, |
| 86 | * so just ensure that the count falls somewhere inside the |
| 87 | * block right now. |
| 88 | */ |
| 89 | if (be32_to_cpu(btp->count) >= |
| 90 | ((char *)btp - p) / sizeof(struct xfs_dir2_leaf_entry)) |
| 91 | return __this_address; |
| 92 | break; |
| 93 | case cpu_to_be32(XFS_DIR3_DATA_MAGIC): |
| 94 | case cpu_to_be32(XFS_DIR2_DATA_MAGIC): |
| 95 | break; |
| 96 | default: |
| 97 | return __this_address; |
| 98 | } |
| 99 | endp = xfs_dir3_data_endp(geo, hdr); |
| 100 | if (!endp) |
| 101 | return __this_address; |
| 102 | |
| 103 | /* |
| 104 | * Account for zero bestfree entries. |
| 105 | */ |
| 106 | bf = ops->data_bestfree_p(hdr); |
| 107 | count = lastfree = freeseen = 0; |
| 108 | if (!bf[0].length) { |
| 109 | if (bf[0].offset) |
| 110 | return __this_address; |
| 111 | freeseen |= 1 << 0; |
| 112 | } |
| 113 | if (!bf[1].length) { |
| 114 | if (bf[1].offset) |
| 115 | return __this_address; |
| 116 | freeseen |= 1 << 1; |
| 117 | } |
| 118 | if (!bf[2].length) { |
| 119 | if (bf[2].offset) |
| 120 | return __this_address; |
| 121 | freeseen |= 1 << 2; |
| 122 | } |
| 123 | |
| 124 | if (be16_to_cpu(bf[0].length) < be16_to_cpu(bf[1].length)) |
| 125 | return __this_address; |
| 126 | if (be16_to_cpu(bf[1].length) < be16_to_cpu(bf[2].length)) |
| 127 | return __this_address; |
| 128 | /* |
| 129 | * Loop over the data/unused entries. |
| 130 | */ |
| 131 | while (p < endp) { |
| 132 | dup = (xfs_dir2_data_unused_t *)p; |
| 133 | /* |
| 134 | * If it's unused, look for the space in the bestfree table. |
| 135 | * If we find it, account for that, else make sure it |
| 136 | * doesn't need to be there. |
| 137 | */ |
| 138 | if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) { |
| 139 | xfs_failaddr_t fa; |
| 140 | |
| 141 | if (lastfree != 0) |
| 142 | return __this_address; |
| 143 | if (endp < p + be16_to_cpu(dup->length)) |
| 144 | return __this_address; |
| 145 | if (be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup)) != |
| 146 | (char *)dup - (char *)hdr) |
| 147 | return __this_address; |
| 148 | fa = xfs_dir2_data_freefind_verify(hdr, bf, dup, &dfp); |
| 149 | if (fa) |
| 150 | return fa; |
| 151 | if (dfp) { |
| 152 | i = (int)(dfp - bf); |
| 153 | if ((freeseen & (1 << i)) != 0) |
| 154 | return __this_address; |
| 155 | freeseen |= 1 << i; |
| 156 | } else { |
| 157 | if (be16_to_cpu(dup->length) > |
| 158 | be16_to_cpu(bf[2].length)) |
| 159 | return __this_address; |
| 160 | } |
| 161 | p += be16_to_cpu(dup->length); |
| 162 | lastfree = 1; |
| 163 | continue; |
| 164 | } |
| 165 | /* |
| 166 | * It's a real entry. Validate the fields. |
| 167 | * If this is a block directory then make sure it's |
| 168 | * in the leaf section of the block. |
| 169 | * The linear search is crude but this is DEBUG code. |
| 170 | */ |
| 171 | dep = (xfs_dir2_data_entry_t *)p; |
| 172 | if (dep->namelen == 0) |
| 173 | return __this_address; |
| 174 | if (xfs_dir_ino_validate(mp, be64_to_cpu(dep->inumber))) |
| 175 | return __this_address; |
| 176 | if (endp < p + ops->data_entsize(dep->namelen)) |
| 177 | return __this_address; |
| 178 | if (be16_to_cpu(*ops->data_entry_tag_p(dep)) != |
| 179 | (char *)dep - (char *)hdr) |
| 180 | return __this_address; |
| 181 | if (ops->data_get_ftype(dep) >= XFS_DIR3_FT_MAX) |
| 182 | return __this_address; |
| 183 | count++; |
| 184 | lastfree = 0; |
| 185 | if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) || |
| 186 | hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) { |
| 187 | addr = xfs_dir2_db_off_to_dataptr(geo, geo->datablk, |
| 188 | (xfs_dir2_data_aoff_t) |
| 189 | ((char *)dep - (char *)hdr)); |
| 190 | name.name = dep->name; |
| 191 | name.len = dep->namelen; |
| 192 | hash = mp->m_dirnameops->hashname(&name); |
| 193 | for (i = 0; i < be32_to_cpu(btp->count); i++) { |
| 194 | if (be32_to_cpu(lep[i].address) == addr && |
| 195 | be32_to_cpu(lep[i].hashval) == hash) |
| 196 | break; |
| 197 | } |
| 198 | if (i >= be32_to_cpu(btp->count)) |
| 199 | return __this_address; |
| 200 | } |
| 201 | p += ops->data_entsize(dep->namelen); |
| 202 | } |
| 203 | /* |
| 204 | * Need to have seen all the entries and all the bestfree slots. |
| 205 | */ |
| 206 | if (freeseen != 7) |
| 207 | return __this_address; |
| 208 | if (hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) || |
| 209 | hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) { |
| 210 | for (i = stale = 0; i < be32_to_cpu(btp->count); i++) { |
| 211 | if (lep[i].address == |
| 212 | cpu_to_be32(XFS_DIR2_NULL_DATAPTR)) |
| 213 | stale++; |
| 214 | if (i > 0 && be32_to_cpu(lep[i].hashval) < |
| 215 | be32_to_cpu(lep[i - 1].hashval)) |
| 216 | return __this_address; |
| 217 | } |
| 218 | if (count != be32_to_cpu(btp->count) - be32_to_cpu(btp->stale)) |
| 219 | return __this_address; |
| 220 | if (stale != be32_to_cpu(btp->stale)) |
| 221 | return __this_address; |
| 222 | } |
| 223 | return NULL; |
| 224 | } |
| 225 | |
| 226 | #ifdef DEBUG |
| 227 | void |
| 228 | xfs_dir3_data_check( |
| 229 | struct xfs_inode *dp, |
| 230 | struct xfs_buf *bp) |
| 231 | { |
| 232 | xfs_failaddr_t fa; |
| 233 | |
| 234 | fa = __xfs_dir3_data_check(dp, bp); |
| 235 | if (!fa) |
| 236 | return; |
| 237 | xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, dp->i_mount, |
| 238 | bp->b_addr, BBTOB(bp->b_length), __FILE__, __LINE__, |
| 239 | fa); |
| 240 | ASSERT(0); |
| 241 | } |
| 242 | #endif |
| 243 | |
| 244 | static xfs_failaddr_t |
| 245 | xfs_dir3_data_verify( |
| 246 | struct xfs_buf *bp) |
| 247 | { |
| 248 | struct xfs_mount *mp = bp->b_mount; |
| 249 | struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr; |
| 250 | |
| 251 | if (!xfs_verify_magic(bp, hdr3->magic)) |
| 252 | return __this_address; |
| 253 | |
| 254 | if (xfs_sb_version_hascrc(&mp->m_sb)) { |
| 255 | if (!uuid_equal(&hdr3->uuid, &mp->m_sb.sb_meta_uuid)) |
| 256 | return __this_address; |
| 257 | if (be64_to_cpu(hdr3->blkno) != bp->b_bn) |
| 258 | return __this_address; |
| 259 | if (!xfs_log_check_lsn(mp, be64_to_cpu(hdr3->lsn))) |
| 260 | return __this_address; |
| 261 | } |
| 262 | return __xfs_dir3_data_check(NULL, bp); |
| 263 | } |
| 264 | |
| 265 | /* |
| 266 | * Readahead of the first block of the directory when it is opened is completely |
| 267 | * oblivious to the format of the directory. Hence we can either get a block |
| 268 | * format buffer or a data format buffer on readahead. |
| 269 | */ |
| 270 | static void |
| 271 | xfs_dir3_data_reada_verify( |
| 272 | struct xfs_buf *bp) |
| 273 | { |
| 274 | struct xfs_dir2_data_hdr *hdr = bp->b_addr; |
| 275 | |
| 276 | switch (hdr->magic) { |
| 277 | case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC): |
| 278 | case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC): |
| 279 | bp->b_ops = &xfs_dir3_block_buf_ops; |
| 280 | bp->b_ops->verify_read(bp); |
| 281 | return; |
| 282 | case cpu_to_be32(XFS_DIR2_DATA_MAGIC): |
| 283 | case cpu_to_be32(XFS_DIR3_DATA_MAGIC): |
| 284 | bp->b_ops = &xfs_dir3_data_buf_ops; |
| 285 | bp->b_ops->verify_read(bp); |
| 286 | return; |
| 287 | default: |
| 288 | xfs_verifier_error(bp, -EFSCORRUPTED, __this_address); |
| 289 | break; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | static void |
| 294 | xfs_dir3_data_read_verify( |
| 295 | struct xfs_buf *bp) |
| 296 | { |
| 297 | struct xfs_mount *mp = bp->b_mount; |
| 298 | xfs_failaddr_t fa; |
| 299 | |
| 300 | if (xfs_sb_version_hascrc(&mp->m_sb) && |
| 301 | !xfs_buf_verify_cksum(bp, XFS_DIR3_DATA_CRC_OFF)) |
| 302 | xfs_verifier_error(bp, -EFSBADCRC, __this_address); |
| 303 | else { |
| 304 | fa = xfs_dir3_data_verify(bp); |
| 305 | if (fa) |
| 306 | xfs_verifier_error(bp, -EFSCORRUPTED, fa); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | static void |
| 311 | xfs_dir3_data_write_verify( |
| 312 | struct xfs_buf *bp) |
| 313 | { |
| 314 | struct xfs_mount *mp = bp->b_mount; |
| 315 | struct xfs_buf_log_item *bip = bp->b_log_item; |
| 316 | struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr; |
| 317 | xfs_failaddr_t fa; |
| 318 | |
| 319 | fa = xfs_dir3_data_verify(bp); |
| 320 | if (fa) { |
| 321 | xfs_verifier_error(bp, -EFSCORRUPTED, fa); |
| 322 | return; |
| 323 | } |
| 324 | |
| 325 | if (!xfs_sb_version_hascrc(&mp->m_sb)) |
| 326 | return; |
| 327 | |
| 328 | if (bip) |
| 329 | hdr3->lsn = cpu_to_be64(bip->bli_item.li_lsn); |
| 330 | |
| 331 | xfs_buf_update_cksum(bp, XFS_DIR3_DATA_CRC_OFF); |
| 332 | } |
| 333 | |
| 334 | const struct xfs_buf_ops xfs_dir3_data_buf_ops = { |
| 335 | .name = "xfs_dir3_data", |
| 336 | .magic = { cpu_to_be32(XFS_DIR2_DATA_MAGIC), |
| 337 | cpu_to_be32(XFS_DIR3_DATA_MAGIC) }, |
| 338 | .verify_read = xfs_dir3_data_read_verify, |
| 339 | .verify_write = xfs_dir3_data_write_verify, |
| 340 | .verify_struct = xfs_dir3_data_verify, |
| 341 | }; |
| 342 | |
| 343 | static const struct xfs_buf_ops xfs_dir3_data_reada_buf_ops = { |
| 344 | .name = "xfs_dir3_data_reada", |
| 345 | .magic = { cpu_to_be32(XFS_DIR2_DATA_MAGIC), |
| 346 | cpu_to_be32(XFS_DIR3_DATA_MAGIC) }, |
| 347 | .verify_read = xfs_dir3_data_reada_verify, |
| 348 | .verify_write = xfs_dir3_data_write_verify, |
| 349 | }; |
| 350 | |
| 351 | static xfs_failaddr_t |
| 352 | xfs_dir3_data_header_check( |
| 353 | struct xfs_inode *dp, |
| 354 | struct xfs_buf *bp) |
| 355 | { |
| 356 | struct xfs_mount *mp = dp->i_mount; |
| 357 | |
| 358 | if (xfs_sb_version_hascrc(&mp->m_sb)) { |
| 359 | struct xfs_dir3_data_hdr *hdr3 = bp->b_addr; |
| 360 | |
| 361 | if (be64_to_cpu(hdr3->hdr.owner) != dp->i_ino) |
| 362 | return __this_address; |
| 363 | } |
| 364 | |
| 365 | return NULL; |
| 366 | } |
| 367 | |
| 368 | int |
| 369 | xfs_dir3_data_read( |
| 370 | struct xfs_trans *tp, |
| 371 | struct xfs_inode *dp, |
| 372 | xfs_dablk_t bno, |
| 373 | xfs_daddr_t mapped_bno, |
| 374 | struct xfs_buf **bpp) |
| 375 | { |
| 376 | xfs_failaddr_t fa; |
| 377 | int err; |
| 378 | |
| 379 | err = xfs_da_read_buf(tp, dp, bno, mapped_bno, bpp, |
| 380 | XFS_DATA_FORK, &xfs_dir3_data_buf_ops); |
| 381 | if (err || !*bpp) |
| 382 | return err; |
| 383 | |
| 384 | /* Check things that we can't do in the verifier. */ |
| 385 | fa = xfs_dir3_data_header_check(dp, *bpp); |
| 386 | if (fa) { |
| 387 | __xfs_buf_mark_corrupt(*bpp, fa); |
| 388 | xfs_trans_brelse(tp, *bpp); |
| 389 | *bpp = NULL; |
| 390 | return -EFSCORRUPTED; |
| 391 | } |
| 392 | |
| 393 | xfs_trans_buf_set_type(tp, *bpp, XFS_BLFT_DIR_DATA_BUF); |
| 394 | return err; |
| 395 | } |
| 396 | |
| 397 | int |
| 398 | xfs_dir3_data_readahead( |
| 399 | struct xfs_inode *dp, |
| 400 | xfs_dablk_t bno, |
| 401 | xfs_daddr_t mapped_bno) |
| 402 | { |
| 403 | return xfs_da_reada_buf(dp, bno, mapped_bno, |
| 404 | XFS_DATA_FORK, &xfs_dir3_data_reada_buf_ops); |
| 405 | } |
| 406 | |
| 407 | /* |
| 408 | * Find the bestfree entry that exactly coincides with unused directory space |
| 409 | * or a verifier error because the bestfree data are bad. |
| 410 | */ |
| 411 | static xfs_failaddr_t |
| 412 | xfs_dir2_data_freefind_verify( |
| 413 | struct xfs_dir2_data_hdr *hdr, |
| 414 | struct xfs_dir2_data_free *bf, |
| 415 | struct xfs_dir2_data_unused *dup, |
| 416 | struct xfs_dir2_data_free **bf_ent) |
| 417 | { |
| 418 | struct xfs_dir2_data_free *dfp; |
| 419 | xfs_dir2_data_aoff_t off; |
| 420 | bool matched = false; |
| 421 | bool seenzero = false; |
| 422 | |
| 423 | *bf_ent = NULL; |
| 424 | off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr); |
| 425 | |
| 426 | /* |
| 427 | * Validate some consistency in the bestfree table. |
| 428 | * Check order, non-overlapping entries, and if we find the |
| 429 | * one we're looking for it has to be exact. |
| 430 | */ |
| 431 | for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) { |
| 432 | if (!dfp->offset) { |
| 433 | if (dfp->length) |
| 434 | return __this_address; |
| 435 | seenzero = true; |
| 436 | continue; |
| 437 | } |
| 438 | if (seenzero) |
| 439 | return __this_address; |
| 440 | if (be16_to_cpu(dfp->offset) == off) { |
| 441 | matched = true; |
| 442 | if (dfp->length != dup->length) |
| 443 | return __this_address; |
| 444 | } else if (be16_to_cpu(dfp->offset) > off) { |
| 445 | if (off + be16_to_cpu(dup->length) > |
| 446 | be16_to_cpu(dfp->offset)) |
| 447 | return __this_address; |
| 448 | } else { |
| 449 | if (be16_to_cpu(dfp->offset) + |
| 450 | be16_to_cpu(dfp->length) > off) |
| 451 | return __this_address; |
| 452 | } |
| 453 | if (!matched && |
| 454 | be16_to_cpu(dfp->length) < be16_to_cpu(dup->length)) |
| 455 | return __this_address; |
| 456 | if (dfp > &bf[0] && |
| 457 | be16_to_cpu(dfp[-1].length) < be16_to_cpu(dfp[0].length)) |
| 458 | return __this_address; |
| 459 | } |
| 460 | |
| 461 | /* Looks ok so far; now try to match up with a bestfree entry. */ |
| 462 | *bf_ent = xfs_dir2_data_freefind(hdr, bf, dup); |
| 463 | return NULL; |
| 464 | } |
| 465 | |
| 466 | /* |
| 467 | * Given a data block and an unused entry from that block, |
| 468 | * return the bestfree entry if any that corresponds to it. |
| 469 | */ |
| 470 | xfs_dir2_data_free_t * |
| 471 | xfs_dir2_data_freefind( |
| 472 | struct xfs_dir2_data_hdr *hdr, /* data block header */ |
| 473 | struct xfs_dir2_data_free *bf, /* bestfree table pointer */ |
| 474 | struct xfs_dir2_data_unused *dup) /* unused space */ |
| 475 | { |
| 476 | xfs_dir2_data_free_t *dfp; /* bestfree entry */ |
| 477 | xfs_dir2_data_aoff_t off; /* offset value needed */ |
| 478 | |
| 479 | off = (xfs_dir2_data_aoff_t)((char *)dup - (char *)hdr); |
| 480 | |
| 481 | /* |
| 482 | * If this is smaller than the smallest bestfree entry, |
| 483 | * it can't be there since they're sorted. |
| 484 | */ |
| 485 | if (be16_to_cpu(dup->length) < |
| 486 | be16_to_cpu(bf[XFS_DIR2_DATA_FD_COUNT - 1].length)) |
| 487 | return NULL; |
| 488 | /* |
| 489 | * Look at the three bestfree entries for our guy. |
| 490 | */ |
| 491 | for (dfp = &bf[0]; dfp < &bf[XFS_DIR2_DATA_FD_COUNT]; dfp++) { |
| 492 | if (!dfp->offset) |
| 493 | return NULL; |
| 494 | if (be16_to_cpu(dfp->offset) == off) |
| 495 | return dfp; |
| 496 | } |
| 497 | /* |
| 498 | * Didn't find it. This only happens if there are duplicate lengths. |
| 499 | */ |
| 500 | return NULL; |
| 501 | } |
| 502 | |
| 503 | /* |
| 504 | * Insert an unused-space entry into the bestfree table. |
| 505 | */ |
| 506 | xfs_dir2_data_free_t * /* entry inserted */ |
| 507 | xfs_dir2_data_freeinsert( |
| 508 | struct xfs_dir2_data_hdr *hdr, /* data block pointer */ |
| 509 | struct xfs_dir2_data_free *dfp, /* bestfree table pointer */ |
| 510 | struct xfs_dir2_data_unused *dup, /* unused space */ |
| 511 | int *loghead) /* log the data header (out) */ |
| 512 | { |
| 513 | xfs_dir2_data_free_t new; /* new bestfree entry */ |
| 514 | |
| 515 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || |
| 516 | hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) || |
| 517 | hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) || |
| 518 | hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)); |
| 519 | |
| 520 | new.length = dup->length; |
| 521 | new.offset = cpu_to_be16((char *)dup - (char *)hdr); |
| 522 | |
| 523 | /* |
| 524 | * Insert at position 0, 1, or 2; or not at all. |
| 525 | */ |
| 526 | if (be16_to_cpu(new.length) > be16_to_cpu(dfp[0].length)) { |
| 527 | dfp[2] = dfp[1]; |
| 528 | dfp[1] = dfp[0]; |
| 529 | dfp[0] = new; |
| 530 | *loghead = 1; |
| 531 | return &dfp[0]; |
| 532 | } |
| 533 | if (be16_to_cpu(new.length) > be16_to_cpu(dfp[1].length)) { |
| 534 | dfp[2] = dfp[1]; |
| 535 | dfp[1] = new; |
| 536 | *loghead = 1; |
| 537 | return &dfp[1]; |
| 538 | } |
| 539 | if (be16_to_cpu(new.length) > be16_to_cpu(dfp[2].length)) { |
| 540 | dfp[2] = new; |
| 541 | *loghead = 1; |
| 542 | return &dfp[2]; |
| 543 | } |
| 544 | return NULL; |
| 545 | } |
| 546 | |
| 547 | /* |
| 548 | * Remove a bestfree entry from the table. |
| 549 | */ |
| 550 | STATIC void |
| 551 | xfs_dir2_data_freeremove( |
| 552 | struct xfs_dir2_data_hdr *hdr, /* data block header */ |
| 553 | struct xfs_dir2_data_free *bf, /* bestfree table pointer */ |
| 554 | struct xfs_dir2_data_free *dfp, /* bestfree entry pointer */ |
| 555 | int *loghead) /* out: log data header */ |
| 556 | { |
| 557 | |
| 558 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || |
| 559 | hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) || |
| 560 | hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) || |
| 561 | hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)); |
| 562 | |
| 563 | /* |
| 564 | * It's the first entry, slide the next 2 up. |
| 565 | */ |
| 566 | if (dfp == &bf[0]) { |
| 567 | bf[0] = bf[1]; |
| 568 | bf[1] = bf[2]; |
| 569 | } |
| 570 | /* |
| 571 | * It's the second entry, slide the 3rd entry up. |
| 572 | */ |
| 573 | else if (dfp == &bf[1]) |
| 574 | bf[1] = bf[2]; |
| 575 | /* |
| 576 | * Must be the last entry. |
| 577 | */ |
| 578 | else |
| 579 | ASSERT(dfp == &bf[2]); |
| 580 | /* |
| 581 | * Clear the 3rd entry, must be zero now. |
| 582 | */ |
| 583 | bf[2].length = 0; |
| 584 | bf[2].offset = 0; |
| 585 | *loghead = 1; |
| 586 | } |
| 587 | |
| 588 | /* |
| 589 | * Given a data block, reconstruct its bestfree map. |
| 590 | */ |
| 591 | void |
| 592 | xfs_dir2_data_freescan_int( |
| 593 | struct xfs_da_geometry *geo, |
| 594 | const struct xfs_dir_ops *ops, |
| 595 | struct xfs_dir2_data_hdr *hdr, |
| 596 | int *loghead) |
| 597 | { |
| 598 | xfs_dir2_data_entry_t *dep; /* active data entry */ |
| 599 | xfs_dir2_data_unused_t *dup; /* unused data entry */ |
| 600 | struct xfs_dir2_data_free *bf; |
| 601 | char *endp; /* end of block's data */ |
| 602 | char *p; /* current entry pointer */ |
| 603 | |
| 604 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || |
| 605 | hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) || |
| 606 | hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) || |
| 607 | hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)); |
| 608 | |
| 609 | /* |
| 610 | * Start by clearing the table. |
| 611 | */ |
| 612 | bf = ops->data_bestfree_p(hdr); |
| 613 | memset(bf, 0, sizeof(*bf) * XFS_DIR2_DATA_FD_COUNT); |
| 614 | *loghead = 1; |
| 615 | /* |
| 616 | * Set up pointers. |
| 617 | */ |
| 618 | p = (char *)ops->data_entry_p(hdr); |
| 619 | endp = xfs_dir3_data_endp(geo, hdr); |
| 620 | /* |
| 621 | * Loop over the block's entries. |
| 622 | */ |
| 623 | while (p < endp) { |
| 624 | dup = (xfs_dir2_data_unused_t *)p; |
| 625 | /* |
| 626 | * If it's a free entry, insert it. |
| 627 | */ |
| 628 | if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) { |
| 629 | ASSERT((char *)dup - (char *)hdr == |
| 630 | be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup))); |
| 631 | xfs_dir2_data_freeinsert(hdr, bf, dup, loghead); |
| 632 | p += be16_to_cpu(dup->length); |
| 633 | } |
| 634 | /* |
| 635 | * For active entries, check their tags and skip them. |
| 636 | */ |
| 637 | else { |
| 638 | dep = (xfs_dir2_data_entry_t *)p; |
| 639 | ASSERT((char *)dep - (char *)hdr == |
| 640 | be16_to_cpu(*ops->data_entry_tag_p(dep))); |
| 641 | p += ops->data_entsize(dep->namelen); |
| 642 | } |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | void |
| 647 | xfs_dir2_data_freescan( |
| 648 | struct xfs_inode *dp, |
| 649 | struct xfs_dir2_data_hdr *hdr, |
| 650 | int *loghead) |
| 651 | { |
| 652 | return xfs_dir2_data_freescan_int(dp->i_mount->m_dir_geo, dp->d_ops, |
| 653 | hdr, loghead); |
| 654 | } |
| 655 | |
| 656 | /* |
| 657 | * Initialize a data block at the given block number in the directory. |
| 658 | * Give back the buffer for the created block. |
| 659 | */ |
| 660 | int /* error */ |
| 661 | xfs_dir3_data_init( |
| 662 | xfs_da_args_t *args, /* directory operation args */ |
| 663 | xfs_dir2_db_t blkno, /* logical dir block number */ |
| 664 | struct xfs_buf **bpp) /* output block buffer */ |
| 665 | { |
| 666 | struct xfs_buf *bp; /* block buffer */ |
| 667 | xfs_dir2_data_hdr_t *hdr; /* data block header */ |
| 668 | xfs_inode_t *dp; /* incore directory inode */ |
| 669 | xfs_dir2_data_unused_t *dup; /* unused entry pointer */ |
| 670 | struct xfs_dir2_data_free *bf; |
| 671 | int error; /* error return value */ |
| 672 | int i; /* bestfree index */ |
| 673 | xfs_mount_t *mp; /* filesystem mount point */ |
| 674 | xfs_trans_t *tp; /* transaction pointer */ |
| 675 | int t; /* temp */ |
| 676 | |
| 677 | dp = args->dp; |
| 678 | mp = dp->i_mount; |
| 679 | tp = args->trans; |
| 680 | /* |
| 681 | * Get the buffer set up for the block. |
| 682 | */ |
| 683 | error = xfs_da_get_buf(tp, dp, xfs_dir2_db_to_da(args->geo, blkno), |
| 684 | -1, &bp, XFS_DATA_FORK); |
| 685 | if (error) |
| 686 | return error; |
| 687 | bp->b_ops = &xfs_dir3_data_buf_ops; |
| 688 | xfs_trans_buf_set_type(tp, bp, XFS_BLFT_DIR_DATA_BUF); |
| 689 | |
| 690 | /* |
| 691 | * Initialize the header. |
| 692 | */ |
| 693 | hdr = bp->b_addr; |
| 694 | if (xfs_sb_version_hascrc(&mp->m_sb)) { |
| 695 | struct xfs_dir3_blk_hdr *hdr3 = bp->b_addr; |
| 696 | |
| 697 | memset(hdr3, 0, sizeof(*hdr3)); |
| 698 | hdr3->magic = cpu_to_be32(XFS_DIR3_DATA_MAGIC); |
| 699 | hdr3->blkno = cpu_to_be64(bp->b_bn); |
| 700 | hdr3->owner = cpu_to_be64(dp->i_ino); |
| 701 | uuid_copy(&hdr3->uuid, &mp->m_sb.sb_meta_uuid); |
| 702 | |
| 703 | } else |
| 704 | hdr->magic = cpu_to_be32(XFS_DIR2_DATA_MAGIC); |
| 705 | |
| 706 | bf = dp->d_ops->data_bestfree_p(hdr); |
| 707 | bf[0].offset = cpu_to_be16(dp->d_ops->data_entry_offset); |
| 708 | for (i = 1; i < XFS_DIR2_DATA_FD_COUNT; i++) { |
| 709 | bf[i].length = 0; |
| 710 | bf[i].offset = 0; |
| 711 | } |
| 712 | |
| 713 | /* |
| 714 | * Set up an unused entry for the block's body. |
| 715 | */ |
| 716 | dup = dp->d_ops->data_unused_p(hdr); |
| 717 | dup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG); |
| 718 | |
| 719 | t = args->geo->blksize - (uint)dp->d_ops->data_entry_offset; |
| 720 | bf[0].length = cpu_to_be16(t); |
| 721 | dup->length = cpu_to_be16(t); |
| 722 | *xfs_dir2_data_unused_tag_p(dup) = cpu_to_be16((char *)dup - (char *)hdr); |
| 723 | /* |
| 724 | * Log it and return it. |
| 725 | */ |
| 726 | xfs_dir2_data_log_header(args, bp); |
| 727 | xfs_dir2_data_log_unused(args, bp, dup); |
| 728 | *bpp = bp; |
| 729 | return 0; |
| 730 | } |
| 731 | |
| 732 | /* |
| 733 | * Log an active data entry from the block. |
| 734 | */ |
| 735 | void |
| 736 | xfs_dir2_data_log_entry( |
| 737 | struct xfs_da_args *args, |
| 738 | struct xfs_buf *bp, |
| 739 | xfs_dir2_data_entry_t *dep) /* data entry pointer */ |
| 740 | { |
| 741 | struct xfs_dir2_data_hdr *hdr = bp->b_addr; |
| 742 | |
| 743 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || |
| 744 | hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) || |
| 745 | hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) || |
| 746 | hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)); |
| 747 | |
| 748 | xfs_trans_log_buf(args->trans, bp, (uint)((char *)dep - (char *)hdr), |
| 749 | (uint)((char *)(args->dp->d_ops->data_entry_tag_p(dep) + 1) - |
| 750 | (char *)hdr - 1)); |
| 751 | } |
| 752 | |
| 753 | /* |
| 754 | * Log a data block header. |
| 755 | */ |
| 756 | void |
| 757 | xfs_dir2_data_log_header( |
| 758 | struct xfs_da_args *args, |
| 759 | struct xfs_buf *bp) |
| 760 | { |
| 761 | #ifdef DEBUG |
| 762 | struct xfs_dir2_data_hdr *hdr = bp->b_addr; |
| 763 | |
| 764 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || |
| 765 | hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) || |
| 766 | hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) || |
| 767 | hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)); |
| 768 | #endif |
| 769 | |
| 770 | xfs_trans_log_buf(args->trans, bp, 0, |
| 771 | args->dp->d_ops->data_entry_offset - 1); |
| 772 | } |
| 773 | |
| 774 | /* |
| 775 | * Log a data unused entry. |
| 776 | */ |
| 777 | void |
| 778 | xfs_dir2_data_log_unused( |
| 779 | struct xfs_da_args *args, |
| 780 | struct xfs_buf *bp, |
| 781 | xfs_dir2_data_unused_t *dup) /* data unused pointer */ |
| 782 | { |
| 783 | xfs_dir2_data_hdr_t *hdr = bp->b_addr; |
| 784 | |
| 785 | ASSERT(hdr->magic == cpu_to_be32(XFS_DIR2_DATA_MAGIC) || |
| 786 | hdr->magic == cpu_to_be32(XFS_DIR3_DATA_MAGIC) || |
| 787 | hdr->magic == cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) || |
| 788 | hdr->magic == cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)); |
| 789 | |
| 790 | /* |
| 791 | * Log the first part of the unused entry. |
| 792 | */ |
| 793 | xfs_trans_log_buf(args->trans, bp, (uint)((char *)dup - (char *)hdr), |
| 794 | (uint)((char *)&dup->length + sizeof(dup->length) - |
| 795 | 1 - (char *)hdr)); |
| 796 | /* |
| 797 | * Log the end (tag) of the unused entry. |
| 798 | */ |
| 799 | xfs_trans_log_buf(args->trans, bp, |
| 800 | (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr), |
| 801 | (uint)((char *)xfs_dir2_data_unused_tag_p(dup) - (char *)hdr + |
| 802 | sizeof(xfs_dir2_data_off_t) - 1)); |
| 803 | } |
| 804 | |
| 805 | /* |
| 806 | * Make a byte range in the data block unused. |
| 807 | * Its current contents are unimportant. |
| 808 | */ |
| 809 | void |
| 810 | xfs_dir2_data_make_free( |
| 811 | struct xfs_da_args *args, |
| 812 | struct xfs_buf *bp, |
| 813 | xfs_dir2_data_aoff_t offset, /* starting byte offset */ |
| 814 | xfs_dir2_data_aoff_t len, /* length in bytes */ |
| 815 | int *needlogp, /* out: log header */ |
| 816 | int *needscanp) /* out: regen bestfree */ |
| 817 | { |
| 818 | xfs_dir2_data_hdr_t *hdr; /* data block pointer */ |
| 819 | xfs_dir2_data_free_t *dfp; /* bestfree pointer */ |
| 820 | char *endptr; /* end of data area */ |
| 821 | int needscan; /* need to regen bestfree */ |
| 822 | xfs_dir2_data_unused_t *newdup; /* new unused entry */ |
| 823 | xfs_dir2_data_unused_t *postdup; /* unused entry after us */ |
| 824 | xfs_dir2_data_unused_t *prevdup; /* unused entry before us */ |
| 825 | struct xfs_dir2_data_free *bf; |
| 826 | |
| 827 | hdr = bp->b_addr; |
| 828 | |
| 829 | /* |
| 830 | * Figure out where the end of the data area is. |
| 831 | */ |
| 832 | endptr = xfs_dir3_data_endp(args->geo, hdr); |
| 833 | ASSERT(endptr != NULL); |
| 834 | |
| 835 | /* |
| 836 | * If this isn't the start of the block, then back up to |
| 837 | * the previous entry and see if it's free. |
| 838 | */ |
| 839 | if (offset > args->dp->d_ops->data_entry_offset) { |
| 840 | __be16 *tagp; /* tag just before us */ |
| 841 | |
| 842 | tagp = (__be16 *)((char *)hdr + offset) - 1; |
| 843 | prevdup = (xfs_dir2_data_unused_t *)((char *)hdr + be16_to_cpu(*tagp)); |
| 844 | if (be16_to_cpu(prevdup->freetag) != XFS_DIR2_DATA_FREE_TAG) |
| 845 | prevdup = NULL; |
| 846 | } else |
| 847 | prevdup = NULL; |
| 848 | /* |
| 849 | * If this isn't the end of the block, see if the entry after |
| 850 | * us is free. |
| 851 | */ |
| 852 | if ((char *)hdr + offset + len < endptr) { |
| 853 | postdup = |
| 854 | (xfs_dir2_data_unused_t *)((char *)hdr + offset + len); |
| 855 | if (be16_to_cpu(postdup->freetag) != XFS_DIR2_DATA_FREE_TAG) |
| 856 | postdup = NULL; |
| 857 | } else |
| 858 | postdup = NULL; |
| 859 | ASSERT(*needscanp == 0); |
| 860 | needscan = 0; |
| 861 | /* |
| 862 | * Previous and following entries are both free, |
| 863 | * merge everything into a single free entry. |
| 864 | */ |
| 865 | bf = args->dp->d_ops->data_bestfree_p(hdr); |
| 866 | if (prevdup && postdup) { |
| 867 | xfs_dir2_data_free_t *dfp2; /* another bestfree pointer */ |
| 868 | |
| 869 | /* |
| 870 | * See if prevdup and/or postdup are in bestfree table. |
| 871 | */ |
| 872 | dfp = xfs_dir2_data_freefind(hdr, bf, prevdup); |
| 873 | dfp2 = xfs_dir2_data_freefind(hdr, bf, postdup); |
| 874 | /* |
| 875 | * We need a rescan unless there are exactly 2 free entries |
| 876 | * namely our two. Then we know what's happening, otherwise |
| 877 | * since the third bestfree is there, there might be more |
| 878 | * entries. |
| 879 | */ |
| 880 | needscan = (bf[2].length != 0); |
| 881 | /* |
| 882 | * Fix up the new big freespace. |
| 883 | */ |
| 884 | be16_add_cpu(&prevdup->length, len + be16_to_cpu(postdup->length)); |
| 885 | *xfs_dir2_data_unused_tag_p(prevdup) = |
| 886 | cpu_to_be16((char *)prevdup - (char *)hdr); |
| 887 | xfs_dir2_data_log_unused(args, bp, prevdup); |
| 888 | if (!needscan) { |
| 889 | /* |
| 890 | * Has to be the case that entries 0 and 1 are |
| 891 | * dfp and dfp2 (don't know which is which), and |
| 892 | * entry 2 is empty. |
| 893 | * Remove entry 1 first then entry 0. |
| 894 | */ |
| 895 | ASSERT(dfp && dfp2); |
| 896 | if (dfp == &bf[1]) { |
| 897 | dfp = &bf[0]; |
| 898 | ASSERT(dfp2 == dfp); |
| 899 | dfp2 = &bf[1]; |
| 900 | } |
| 901 | xfs_dir2_data_freeremove(hdr, bf, dfp2, needlogp); |
| 902 | xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp); |
| 903 | /* |
| 904 | * Now insert the new entry. |
| 905 | */ |
| 906 | dfp = xfs_dir2_data_freeinsert(hdr, bf, prevdup, |
| 907 | needlogp); |
| 908 | ASSERT(dfp == &bf[0]); |
| 909 | ASSERT(dfp->length == prevdup->length); |
| 910 | ASSERT(!dfp[1].length); |
| 911 | ASSERT(!dfp[2].length); |
| 912 | } |
| 913 | } |
| 914 | /* |
| 915 | * The entry before us is free, merge with it. |
| 916 | */ |
| 917 | else if (prevdup) { |
| 918 | dfp = xfs_dir2_data_freefind(hdr, bf, prevdup); |
| 919 | be16_add_cpu(&prevdup->length, len); |
| 920 | *xfs_dir2_data_unused_tag_p(prevdup) = |
| 921 | cpu_to_be16((char *)prevdup - (char *)hdr); |
| 922 | xfs_dir2_data_log_unused(args, bp, prevdup); |
| 923 | /* |
| 924 | * If the previous entry was in the table, the new entry |
| 925 | * is longer, so it will be in the table too. Remove |
| 926 | * the old one and add the new one. |
| 927 | */ |
| 928 | if (dfp) { |
| 929 | xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp); |
| 930 | xfs_dir2_data_freeinsert(hdr, bf, prevdup, needlogp); |
| 931 | } |
| 932 | /* |
| 933 | * Otherwise we need a scan if the new entry is big enough. |
| 934 | */ |
| 935 | else { |
| 936 | needscan = be16_to_cpu(prevdup->length) > |
| 937 | be16_to_cpu(bf[2].length); |
| 938 | } |
| 939 | } |
| 940 | /* |
| 941 | * The following entry is free, merge with it. |
| 942 | */ |
| 943 | else if (postdup) { |
| 944 | dfp = xfs_dir2_data_freefind(hdr, bf, postdup); |
| 945 | newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset); |
| 946 | newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG); |
| 947 | newdup->length = cpu_to_be16(len + be16_to_cpu(postdup->length)); |
| 948 | *xfs_dir2_data_unused_tag_p(newdup) = |
| 949 | cpu_to_be16((char *)newdup - (char *)hdr); |
| 950 | xfs_dir2_data_log_unused(args, bp, newdup); |
| 951 | /* |
| 952 | * If the following entry was in the table, the new entry |
| 953 | * is longer, so it will be in the table too. Remove |
| 954 | * the old one and add the new one. |
| 955 | */ |
| 956 | if (dfp) { |
| 957 | xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp); |
| 958 | xfs_dir2_data_freeinsert(hdr, bf, newdup, needlogp); |
| 959 | } |
| 960 | /* |
| 961 | * Otherwise we need a scan if the new entry is big enough. |
| 962 | */ |
| 963 | else { |
| 964 | needscan = be16_to_cpu(newdup->length) > |
| 965 | be16_to_cpu(bf[2].length); |
| 966 | } |
| 967 | } |
| 968 | /* |
| 969 | * Neither neighbor is free. Make a new entry. |
| 970 | */ |
| 971 | else { |
| 972 | newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset); |
| 973 | newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG); |
| 974 | newdup->length = cpu_to_be16(len); |
| 975 | *xfs_dir2_data_unused_tag_p(newdup) = |
| 976 | cpu_to_be16((char *)newdup - (char *)hdr); |
| 977 | xfs_dir2_data_log_unused(args, bp, newdup); |
| 978 | xfs_dir2_data_freeinsert(hdr, bf, newdup, needlogp); |
| 979 | } |
| 980 | *needscanp = needscan; |
| 981 | } |
| 982 | |
| 983 | /* Check our free data for obvious signs of corruption. */ |
| 984 | static inline xfs_failaddr_t |
| 985 | xfs_dir2_data_check_free( |
| 986 | struct xfs_dir2_data_hdr *hdr, |
| 987 | struct xfs_dir2_data_unused *dup, |
| 988 | xfs_dir2_data_aoff_t offset, |
| 989 | xfs_dir2_data_aoff_t len) |
| 990 | { |
| 991 | if (hdr->magic != cpu_to_be32(XFS_DIR2_DATA_MAGIC) && |
| 992 | hdr->magic != cpu_to_be32(XFS_DIR3_DATA_MAGIC) && |
| 993 | hdr->magic != cpu_to_be32(XFS_DIR2_BLOCK_MAGIC) && |
| 994 | hdr->magic != cpu_to_be32(XFS_DIR3_BLOCK_MAGIC)) |
| 995 | return __this_address; |
| 996 | if (be16_to_cpu(dup->freetag) != XFS_DIR2_DATA_FREE_TAG) |
| 997 | return __this_address; |
| 998 | if (offset < (char *)dup - (char *)hdr) |
| 999 | return __this_address; |
| 1000 | if (offset + len > (char *)dup + be16_to_cpu(dup->length) - (char *)hdr) |
| 1001 | return __this_address; |
| 1002 | if ((char *)dup - (char *)hdr != |
| 1003 | be16_to_cpu(*xfs_dir2_data_unused_tag_p(dup))) |
| 1004 | return __this_address; |
| 1005 | return NULL; |
| 1006 | } |
| 1007 | |
| 1008 | /* Sanity-check a new bestfree entry. */ |
| 1009 | static inline xfs_failaddr_t |
| 1010 | xfs_dir2_data_check_new_free( |
| 1011 | struct xfs_dir2_data_hdr *hdr, |
| 1012 | struct xfs_dir2_data_free *dfp, |
| 1013 | struct xfs_dir2_data_unused *newdup) |
| 1014 | { |
| 1015 | if (dfp == NULL) |
| 1016 | return __this_address; |
| 1017 | if (dfp->length != newdup->length) |
| 1018 | return __this_address; |
| 1019 | if (be16_to_cpu(dfp->offset) != (char *)newdup - (char *)hdr) |
| 1020 | return __this_address; |
| 1021 | return NULL; |
| 1022 | } |
| 1023 | |
| 1024 | /* |
| 1025 | * Take a byte range out of an existing unused space and make it un-free. |
| 1026 | */ |
| 1027 | int |
| 1028 | xfs_dir2_data_use_free( |
| 1029 | struct xfs_da_args *args, |
| 1030 | struct xfs_buf *bp, |
| 1031 | xfs_dir2_data_unused_t *dup, /* unused entry */ |
| 1032 | xfs_dir2_data_aoff_t offset, /* starting offset to use */ |
| 1033 | xfs_dir2_data_aoff_t len, /* length to use */ |
| 1034 | int *needlogp, /* out: need to log header */ |
| 1035 | int *needscanp) /* out: need regen bestfree */ |
| 1036 | { |
| 1037 | xfs_dir2_data_hdr_t *hdr; /* data block header */ |
| 1038 | xfs_dir2_data_free_t *dfp; /* bestfree pointer */ |
| 1039 | xfs_dir2_data_unused_t *newdup; /* new unused entry */ |
| 1040 | xfs_dir2_data_unused_t *newdup2; /* another new unused entry */ |
| 1041 | struct xfs_dir2_data_free *bf; |
| 1042 | xfs_failaddr_t fa; |
| 1043 | int matchback; /* matches end of freespace */ |
| 1044 | int matchfront; /* matches start of freespace */ |
| 1045 | int needscan; /* need to regen bestfree */ |
| 1046 | int oldlen; /* old unused entry's length */ |
| 1047 | |
| 1048 | hdr = bp->b_addr; |
| 1049 | fa = xfs_dir2_data_check_free(hdr, dup, offset, len); |
| 1050 | if (fa) |
| 1051 | goto corrupt; |
| 1052 | /* |
| 1053 | * Look up the entry in the bestfree table. |
| 1054 | */ |
| 1055 | oldlen = be16_to_cpu(dup->length); |
| 1056 | bf = args->dp->d_ops->data_bestfree_p(hdr); |
| 1057 | dfp = xfs_dir2_data_freefind(hdr, bf, dup); |
| 1058 | ASSERT(dfp || oldlen <= be16_to_cpu(bf[2].length)); |
| 1059 | /* |
| 1060 | * Check for alignment with front and back of the entry. |
| 1061 | */ |
| 1062 | matchfront = (char *)dup - (char *)hdr == offset; |
| 1063 | matchback = (char *)dup + oldlen - (char *)hdr == offset + len; |
| 1064 | ASSERT(*needscanp == 0); |
| 1065 | needscan = 0; |
| 1066 | /* |
| 1067 | * If we matched it exactly we just need to get rid of it from |
| 1068 | * the bestfree table. |
| 1069 | */ |
| 1070 | if (matchfront && matchback) { |
| 1071 | if (dfp) { |
| 1072 | needscan = (bf[2].offset != 0); |
| 1073 | if (!needscan) |
| 1074 | xfs_dir2_data_freeremove(hdr, bf, dfp, |
| 1075 | needlogp); |
| 1076 | } |
| 1077 | } |
| 1078 | /* |
| 1079 | * We match the first part of the entry. |
| 1080 | * Make a new entry with the remaining freespace. |
| 1081 | */ |
| 1082 | else if (matchfront) { |
| 1083 | newdup = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len); |
| 1084 | newdup->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG); |
| 1085 | newdup->length = cpu_to_be16(oldlen - len); |
| 1086 | *xfs_dir2_data_unused_tag_p(newdup) = |
| 1087 | cpu_to_be16((char *)newdup - (char *)hdr); |
| 1088 | xfs_dir2_data_log_unused(args, bp, newdup); |
| 1089 | /* |
| 1090 | * If it was in the table, remove it and add the new one. |
| 1091 | */ |
| 1092 | if (dfp) { |
| 1093 | xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp); |
| 1094 | dfp = xfs_dir2_data_freeinsert(hdr, bf, newdup, |
| 1095 | needlogp); |
| 1096 | fa = xfs_dir2_data_check_new_free(hdr, dfp, newdup); |
| 1097 | if (fa) |
| 1098 | goto corrupt; |
| 1099 | /* |
| 1100 | * If we got inserted at the last slot, |
| 1101 | * that means we don't know if there was a better |
| 1102 | * choice for the last slot, or not. Rescan. |
| 1103 | */ |
| 1104 | needscan = dfp == &bf[2]; |
| 1105 | } |
| 1106 | } |
| 1107 | /* |
| 1108 | * We match the last part of the entry. |
| 1109 | * Trim the allocated space off the tail of the entry. |
| 1110 | */ |
| 1111 | else if (matchback) { |
| 1112 | newdup = dup; |
| 1113 | newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup); |
| 1114 | *xfs_dir2_data_unused_tag_p(newdup) = |
| 1115 | cpu_to_be16((char *)newdup - (char *)hdr); |
| 1116 | xfs_dir2_data_log_unused(args, bp, newdup); |
| 1117 | /* |
| 1118 | * If it was in the table, remove it and add the new one. |
| 1119 | */ |
| 1120 | if (dfp) { |
| 1121 | xfs_dir2_data_freeremove(hdr, bf, dfp, needlogp); |
| 1122 | dfp = xfs_dir2_data_freeinsert(hdr, bf, newdup, |
| 1123 | needlogp); |
| 1124 | fa = xfs_dir2_data_check_new_free(hdr, dfp, newdup); |
| 1125 | if (fa) |
| 1126 | goto corrupt; |
| 1127 | /* |
| 1128 | * If we got inserted at the last slot, |
| 1129 | * that means we don't know if there was a better |
| 1130 | * choice for the last slot, or not. Rescan. |
| 1131 | */ |
| 1132 | needscan = dfp == &bf[2]; |
| 1133 | } |
| 1134 | } |
| 1135 | /* |
| 1136 | * Poking out the middle of an entry. |
| 1137 | * Make two new entries. |
| 1138 | */ |
| 1139 | else { |
| 1140 | newdup = dup; |
| 1141 | newdup->length = cpu_to_be16(((char *)hdr + offset) - (char *)newdup); |
| 1142 | *xfs_dir2_data_unused_tag_p(newdup) = |
| 1143 | cpu_to_be16((char *)newdup - (char *)hdr); |
| 1144 | xfs_dir2_data_log_unused(args, bp, newdup); |
| 1145 | newdup2 = (xfs_dir2_data_unused_t *)((char *)hdr + offset + len); |
| 1146 | newdup2->freetag = cpu_to_be16(XFS_DIR2_DATA_FREE_TAG); |
| 1147 | newdup2->length = cpu_to_be16(oldlen - len - be16_to_cpu(newdup->length)); |
| 1148 | *xfs_dir2_data_unused_tag_p(newdup2) = |
| 1149 | cpu_to_be16((char *)newdup2 - (char *)hdr); |
| 1150 | xfs_dir2_data_log_unused(args, bp, newdup2); |
| 1151 | /* |
| 1152 | * If the old entry was in the table, we need to scan |
| 1153 | * if the 3rd entry was valid, since these entries |
| 1154 | * are smaller than the old one. |
| 1155 | * If we don't need to scan that means there were 1 or 2 |
| 1156 | * entries in the table, and removing the old and adding |
| 1157 | * the 2 new will work. |
| 1158 | */ |
| 1159 | if (dfp) { |
| 1160 | needscan = (bf[2].length != 0); |
| 1161 | if (!needscan) { |
| 1162 | xfs_dir2_data_freeremove(hdr, bf, dfp, |
| 1163 | needlogp); |
| 1164 | xfs_dir2_data_freeinsert(hdr, bf, newdup, |
| 1165 | needlogp); |
| 1166 | xfs_dir2_data_freeinsert(hdr, bf, newdup2, |
| 1167 | needlogp); |
| 1168 | } |
| 1169 | } |
| 1170 | } |
| 1171 | *needscanp = needscan; |
| 1172 | return 0; |
| 1173 | corrupt: |
| 1174 | xfs_corruption_error(__func__, XFS_ERRLEVEL_LOW, args->dp->i_mount, |
| 1175 | hdr, sizeof(*hdr), __FILE__, __LINE__, fa); |
| 1176 | return -EFSCORRUPTED; |
| 1177 | } |
| 1178 | |
| 1179 | /* Find the end of the entry data in a data/block format dir block. */ |
| 1180 | void * |
| 1181 | xfs_dir3_data_endp( |
| 1182 | struct xfs_da_geometry *geo, |
| 1183 | struct xfs_dir2_data_hdr *hdr) |
| 1184 | { |
| 1185 | switch (hdr->magic) { |
| 1186 | case cpu_to_be32(XFS_DIR3_BLOCK_MAGIC): |
| 1187 | case cpu_to_be32(XFS_DIR2_BLOCK_MAGIC): |
| 1188 | return xfs_dir2_block_leaf_p(xfs_dir2_block_tail_p(geo, hdr)); |
| 1189 | case cpu_to_be32(XFS_DIR3_DATA_MAGIC): |
| 1190 | case cpu_to_be32(XFS_DIR2_DATA_MAGIC): |
| 1191 | return (char *)hdr + geo->blksize; |
| 1192 | default: |
| 1193 | return NULL; |
| 1194 | } |
| 1195 | } |