| 1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 |
/* |
| 3 |
* Copyright (C) 2017-2023 Oracle. All Rights Reserved. |
| 4 |
* Author: Darrick J. Wong <[email protected]> |
| 5 |
*/ |
| 6 |
#include "xfs_platform.h" |
| 7 |
#include "xfs_fs.h" |
| 8 |
#include "xfs_shared.h" |
| 9 |
#include "xfs_format.h" |
| 10 |
#include "xfs_trans_resv.h" |
| 11 |
#include "xfs_mount.h" |
| 12 |
#include "xfs_log_format.h" |
| 13 |
#include "xfs_trans.h" |
| 14 |
#include "xfs_inode.h" |
| 15 |
#include "xfs_da_format.h" |
| 16 |
#include "xfs_da_btree.h" |
| 17 |
#include "xfs_attr.h" |
| 18 |
#include "xfs_attr_leaf.h" |
| 19 |
#include "xfs_attr_sf.h" |
| 20 |
#include "xfs_parent.h" |
| 21 |
#include "scrub/scrub.h" |
| 22 |
#include "scrub/common.h" |
| 23 |
#include "scrub/dabtree.h" |
| 24 |
#include "scrub/attr.h" |
| 25 |
#include "scrub/listxattr.h" |
| 26 |
#include "scrub/repair.h" |
| 27 |
|
| 28 |
/* Free the buffers linked from the xattr buffer. */ |
| 29 |
static void |
| 30 |
xchk_xattr_buf_cleanup( |
| 31 |
void *priv) |
| 32 |
{ |
| 33 |
struct xchk_xattr_buf *ab = priv; |
| 34 |
|
| 35 |
kvfree(ab->freemap); |
| 36 |
ab->freemap = NULL; |
| 37 |
kvfree(ab->usedmap); |
| 38 |
ab->usedmap = NULL; |
| 39 |
kvfree(ab->value); |
| 40 |
ab->value = NULL; |
| 41 |
ab->value_sz = 0; |
| 42 |
kvfree(ab->name); |
| 43 |
ab->name = NULL; |
| 44 |
} |
| 45 |
|
| 46 |
/* |
| 47 |
* Allocate the free space bitmap if we're trying harder; there are leaf blocks |
| 48 |
* in the attr fork; or we can't tell if there are leaf blocks. |
| 49 |
*/ |
| 50 |
static inline bool |
| 51 |
xchk_xattr_want_freemap( |
| 52 |
struct xfs_scrub *sc) |
| 53 |
{ |
| 54 |
struct xfs_ifork *ifp; |
| 55 |
|
| 56 |
if (sc->flags & XCHK_TRY_HARDER) |
| 57 |
return true; |
| 58 |
|
| 59 |
if (!sc->ip) |
| 60 |
return true; |
| 61 |
|
| 62 |
ifp = xfs_ifork_ptr(sc->ip, XFS_ATTR_FORK); |
| 63 |
if (!ifp) |
| 64 |
return false; |
| 65 |
|
| 66 |
return xfs_ifork_has_extents(ifp); |
| 67 |
} |
| 68 |
|
| 69 |
/* |
| 70 |
* Allocate enough memory to hold an attr value and attr block bitmaps, |
| 71 |
* reallocating the buffer if necessary. Buffer contents are not preserved |
| 72 |
* across a reallocation. |
| 73 |
*/ |
| 74 |
int |
| 75 |
xchk_setup_xattr_buf( |
| 76 |
struct xfs_scrub *sc, |
| 77 |
size_t value_size) |
| 78 |
{ |
| 79 |
size_t bmp_sz; |
| 80 |
struct xchk_xattr_buf *ab = sc->buf; |
| 81 |
void *new_val; |
| 82 |
|
| 83 |
bmp_sz = sizeof(long) * BITS_TO_LONGS(sc->mp->m_attr_geo->blksize); |
| 84 |
|
| 85 |
if (ab) |
| 86 |
goto resize_value; |
| 87 |
|
| 88 |
ab = kvzalloc_obj(struct xchk_xattr_buf, XCHK_GFP_FLAGS); |
| 89 |
if (!ab) |
| 90 |
return -ENOMEM; |
| 91 |
sc->buf = ab; |
| 92 |
sc->buf_cleanup = xchk_xattr_buf_cleanup; |
| 93 |
|
| 94 |
ab->usedmap = kvmalloc(bmp_sz, XCHK_GFP_FLAGS); |
| 95 |
if (!ab->usedmap) |
| 96 |
return -ENOMEM; |
| 97 |
|
| 98 |
if (xchk_xattr_want_freemap(sc)) { |
| 99 |
ab->freemap = kvmalloc(bmp_sz, XCHK_GFP_FLAGS); |
| 100 |
if (!ab->freemap) |
| 101 |
return -ENOMEM; |
| 102 |
} |
| 103 |
|
| 104 |
if (xchk_could_repair(sc)) { |
| 105 |
ab->name = kvmalloc(XATTR_NAME_MAX + 1, XCHK_GFP_FLAGS); |
| 106 |
if (!ab->name) |
| 107 |
return -ENOMEM; |
| 108 |
} |
| 109 |
|
| 110 |
resize_value: |
| 111 |
if (ab->value_sz >= value_size) |
| 112 |
return 0; |
| 113 |
|
| 114 |
if (ab->value) { |
| 115 |
kvfree(ab->value); |
| 116 |
ab->value = NULL; |
| 117 |
ab->value_sz = 0; |
| 118 |
} |
| 119 |
|
| 120 |
new_val = kvmalloc(value_size, XCHK_GFP_FLAGS); |
| 121 |
if (!new_val) |
| 122 |
return -ENOMEM; |
| 123 |
|
| 124 |
ab->value = new_val; |
| 125 |
ab->value_sz = value_size; |
| 126 |
return 0; |
| 127 |
} |
| 128 |
|
| 129 |
/* Set us up to scrub an inode's extended attributes. */ |
| 130 |
int |
| 131 |
xchk_setup_xattr( |
| 132 |
struct xfs_scrub *sc) |
| 133 |
{ |
| 134 |
int error; |
| 135 |
|
| 136 |
if (xchk_could_repair(sc)) { |
| 137 |
error = xrep_setup_xattr(sc); |
| 138 |
if (error) |
| 139 |
return error; |
| 140 |
} |
| 141 |
|
| 142 |
/* |
| 143 |
* We failed to get memory while checking attrs, so this time try to |
| 144 |
* get all the memory we're ever going to need. Allocate the buffer |
| 145 |
* without the inode lock held, which means we can sleep. |
| 146 |
*/ |
| 147 |
if (sc->flags & XCHK_TRY_HARDER) { |
| 148 |
error = xchk_setup_xattr_buf(sc, XATTR_SIZE_MAX); |
| 149 |
if (error) |
| 150 |
return error; |
| 151 |
} |
| 152 |
|
| 153 |
return xchk_setup_inode_contents(sc, 0); |
| 154 |
} |
| 155 |
|
| 156 |
/* Extended Attributes */ |
| 157 |
|
| 158 |
/* |
| 159 |
* Check that an extended attribute key can be looked up by hash. |
| 160 |
* |
| 161 |
* We use the extended attribute walk helper to call this function for every |
| 162 |
* attribute key in an inode. Once we're here, we load the attribute value to |
| 163 |
* see if any errors happen, or if we get more or less data than we expected. |
| 164 |
*/ |
| 165 |
static int |
| 166 |
xchk_xattr_actor( |
| 167 |
struct xfs_scrub *sc, |
| 168 |
struct xfs_inode *ip, |
| 169 |
unsigned int attr_flags, |
| 170 |
const unsigned char *name, |
| 171 |
unsigned int namelen, |
| 172 |
const void *value, |
| 173 |
unsigned int valuelen, |
| 174 |
void *priv) |
| 175 |
{ |
| 176 |
struct xfs_da_args args = { |
| 177 |
.attr_filter = attr_flags & XFS_ATTR_NSP_ONDISK_MASK, |
| 178 |
.geo = sc->mp->m_attr_geo, |
| 179 |
.whichfork = XFS_ATTR_FORK, |
| 180 |
.dp = ip, |
| 181 |
.name = name, |
| 182 |
.namelen = namelen, |
| 183 |
.trans = sc->tp, |
| 184 |
.valuelen = valuelen, |
| 185 |
.owner = ip->i_ino, |
| 186 |
}; |
| 187 |
struct xchk_xattr_buf *ab; |
| 188 |
int error = 0; |
| 189 |
|
| 190 |
ab = sc->buf; |
| 191 |
|
| 192 |
if (xchk_should_terminate(sc, &error)) |
| 193 |
return error; |
| 194 |
|
| 195 |
if (attr_flags & ~XFS_ATTR_ONDISK_MASK) { |
| 196 |
xchk_fblock_set_corrupt(sc, XFS_ATTR_FORK, args.blkno); |
| 197 |
return -ECANCELED; |
| 198 |
} |
| 199 |
|
| 200 |
if (attr_flags & XFS_ATTR_INCOMPLETE) { |
| 201 |
/* Incomplete attr key, just mark the inode for preening. */ |
| 202 |
xchk_ino_set_preen(sc, ip->i_ino); |
| 203 |
return 0; |
| 204 |
} |
| 205 |
|
| 206 |
/* Does this name make sense? */ |
| 207 |
if (!xfs_attr_namecheck(attr_flags, name, namelen)) { |
| 208 |
xchk_fblock_set_corrupt(sc, XFS_ATTR_FORK, args.blkno); |
| 209 |
return -ECANCELED; |
| 210 |
} |
| 211 |
|
| 212 |
/* Check parent pointer record. */ |
| 213 |
if ((attr_flags & XFS_ATTR_PARENT) && |
| 214 |
!xfs_parent_valuecheck(sc->mp, value, valuelen)) { |
| 215 |
xchk_fblock_set_corrupt(sc, XFS_ATTR_FORK, args.blkno); |
| 216 |
return -ECANCELED; |
| 217 |
} |
| 218 |
|
| 219 |
/* |
| 220 |
* Try to allocate enough memory to extract the attr value. If that |
| 221 |
* doesn't work, return -EDEADLOCK as a signal to try again with a |
| 222 |
* maximally sized buffer. |
| 223 |
*/ |
| 224 |
error = xchk_setup_xattr_buf(sc, valuelen); |
| 225 |
if (error == -ENOMEM) |
| 226 |
error = -EDEADLOCK; |
| 227 |
if (error) |
| 228 |
return error; |
| 229 |
|
| 230 |
/* |
| 231 |
* Parent pointers are matched on attr name and value, so we must |
| 232 |
* supply the xfs_parent_rec here when confirming that the dabtree |
| 233 |
* indexing works correctly. |
| 234 |
*/ |
| 235 |
if (attr_flags & XFS_ATTR_PARENT) |
| 236 |
memcpy(ab->value, value, valuelen); |
| 237 |
|
| 238 |
args.value = ab->value; |
| 239 |
|
| 240 |
/* |
| 241 |
* Get the attr value to ensure that lookup can find this attribute |
| 242 |
* through the dabtree indexing and that remote value retrieval also |
| 243 |
* works correctly. |
| 244 |
*/ |
| 245 |
xfs_attr_sethash(&args); |
| 246 |
error = xfs_attr_get_ilocked(&args); |
| 247 |
/* ENODATA means the hash lookup failed and the attr is bad */ |
| 248 |
if (error == -ENODATA) |
| 249 |
error = -EFSCORRUPTED; |
| 250 |
if (!xchk_fblock_process_error(sc, XFS_ATTR_FORK, args.blkno, |
| 251 |
&error)) |
| 252 |
return error; |
| 253 |
if (args.valuelen != valuelen) |
| 254 |
xchk_fblock_set_corrupt(sc, XFS_ATTR_FORK, args.blkno); |
| 255 |
|
| 256 |
return 0; |
| 257 |
} |
| 258 |
|
| 259 |
/* |
| 260 |
* Mark a range [start, start+len) in this map. Returns true if the |
| 261 |
* region was free, and false if there's a conflict or a problem. |
| 262 |
* |
| 263 |
* Within a char, the lowest bit of the char represents the byte with |
| 264 |
* the smallest address |
| 265 |
*/ |
| 266 |
bool |
| 267 |
xchk_xattr_set_map( |
| 268 |
struct xfs_scrub *sc, |
| 269 |
unsigned long *map, |
| 270 |
unsigned int start, |
| 271 |
unsigned int len) |
| 272 |
{ |
| 273 |
unsigned int mapsize = sc->mp->m_attr_geo->blksize; |
| 274 |
bool ret = true; |
| 275 |
|
| 276 |
if (start >= mapsize) |
| 277 |
return false; |
| 278 |
if (start + len > mapsize) { |
| 279 |
len = mapsize - start; |
| 280 |
ret = false; |
| 281 |
} |
| 282 |
|
| 283 |
if (find_next_bit(map, mapsize, start) < start + len) |
| 284 |
ret = false; |
| 285 |
bitmap_set(map, start, len); |
| 286 |
|
| 287 |
return ret; |
| 288 |
} |
| 289 |
|
| 290 |
/* |
| 291 |
* Check this leaf entry's relations to everything else. |
| 292 |
* Returns the number of bytes used for the name/value data. |
| 293 |
*/ |
| 294 |
STATIC void |
| 295 |
xchk_xattr_entry( |
| 296 |
struct xchk_da_btree *ds, |
| 297 |
int level, |
| 298 |
char *buf_end, |
| 299 |
struct xfs_attr_leafblock *leaf, |
| 300 |
struct xfs_attr3_icleaf_hdr *leafhdr, |
| 301 |
struct xfs_attr_leaf_entry *ent, |
| 302 |
int idx, |
| 303 |
unsigned int *usedbytes, |
| 304 |
__u32 *last_hashval) |
| 305 |
{ |
| 306 |
struct xfs_mount *mp = ds->state->mp; |
| 307 |
struct xchk_xattr_buf *ab = ds->sc->buf; |
| 308 |
char *name_end; |
| 309 |
struct xfs_attr_leaf_name_local *lentry; |
| 310 |
struct xfs_attr_leaf_name_remote *rentry; |
| 311 |
unsigned int nameidx; |
| 312 |
unsigned int namesize; |
| 313 |
|
| 314 |
if (ent->pad2 != 0) |
| 315 |
xchk_da_set_corrupt(ds, level); |
| 316 |
|
| 317 |
/* Hash values in order? */ |
| 318 |
if (be32_to_cpu(ent->hashval) < *last_hashval) |
| 319 |
xchk_da_set_corrupt(ds, level); |
| 320 |
*last_hashval = be32_to_cpu(ent->hashval); |
| 321 |
|
| 322 |
nameidx = be16_to_cpu(ent->nameidx); |
| 323 |
if (nameidx < leafhdr->firstused || |
| 324 |
nameidx >= mp->m_attr_geo->blksize) { |
| 325 |
xchk_da_set_corrupt(ds, level); |
| 326 |
return; |
| 327 |
} |
| 328 |
|
| 329 |
/* Check the name information. */ |
| 330 |
if (ent->flags & XFS_ATTR_LOCAL) { |
| 331 |
lentry = xfs_attr3_leaf_name_local(leaf, idx); |
| 332 |
namesize = xfs_attr_leaf_entsize_local(lentry->namelen, |
| 333 |
be16_to_cpu(lentry->valuelen)); |
| 334 |
name_end = (char *)lentry + namesize; |
| 335 |
if (lentry->namelen == 0) |
| 336 |
xchk_da_set_corrupt(ds, level); |
| 337 |
} else { |
| 338 |
rentry = xfs_attr3_leaf_name_remote(leaf, idx); |
| 339 |
namesize = xfs_attr_leaf_entsize_remote(rentry->namelen); |
| 340 |
name_end = (char *)rentry + namesize; |
| 341 |
if (rentry->namelen == 0) |
| 342 |
xchk_da_set_corrupt(ds, level); |
| 343 |
if (rentry->valueblk == 0 && |
| 344 |
!(ent->flags & XFS_ATTR_INCOMPLETE)) |
| 345 |
xchk_da_set_corrupt(ds, level); |
| 346 |
} |
| 347 |
if (name_end > buf_end) |
| 348 |
xchk_da_set_corrupt(ds, level); |
| 349 |
|
| 350 |
if (!xchk_xattr_set_map(ds->sc, ab->usedmap, nameidx, namesize)) |
| 351 |
xchk_da_set_corrupt(ds, level); |
| 352 |
if (!(ds->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)) |
| 353 |
*usedbytes += namesize; |
| 354 |
} |
| 355 |
|
| 356 |
/* Scrub an attribute leaf. */ |
| 357 |
STATIC int |
| 358 |
xchk_xattr_block( |
| 359 |
struct xchk_da_btree *ds, |
| 360 |
int level) |
| 361 |
{ |
| 362 |
struct xfs_attr3_icleaf_hdr leafhdr; |
| 363 |
struct xfs_mount *mp = ds->state->mp; |
| 364 |
struct xfs_da_state_blk *blk = &ds->state->path.blk[level]; |
| 365 |
struct xfs_buf *bp = blk->bp; |
| 366 |
xfs_dablk_t *last_checked = ds->private; |
| 367 |
struct xfs_attr_leafblock *leaf = bp->b_addr; |
| 368 |
struct xfs_attr_leaf_entry *ent; |
| 369 |
struct xfs_attr_leaf_entry *entries; |
| 370 |
struct xchk_xattr_buf *ab = ds->sc->buf; |
| 371 |
char *buf_end; |
| 372 |
size_t off; |
| 373 |
__u32 last_hashval = 0; |
| 374 |
unsigned int usedbytes = 0; |
| 375 |
unsigned int hdrsize; |
| 376 |
int i; |
| 377 |
|
| 378 |
if (*last_checked == blk->blkno) |
| 379 |
return 0; |
| 380 |
|
| 381 |
*last_checked = blk->blkno; |
| 382 |
bitmap_zero(ab->usedmap, mp->m_attr_geo->blksize); |
| 383 |
bitmap_zero(ab->freemap, mp->m_attr_geo->blksize); |
| 384 |
|
| 385 |
/* Check all the padding. */ |
| 386 |
if (xfs_has_crc(ds->sc->mp)) { |
| 387 |
struct xfs_attr3_leafblock *leaf3 = bp->b_addr; |
| 388 |
|
| 389 |
if (leaf3->hdr.pad1 != 0 || leaf3->hdr.pad2 != 0 || |
| 390 |
leaf3->hdr.info.hdr.pad != 0) |
| 391 |
xchk_da_set_corrupt(ds, level); |
| 392 |
} else { |
| 393 |
if (leaf->hdr.pad1 != 0 || leaf->hdr.info.pad != 0) |
| 394 |
xchk_da_set_corrupt(ds, level); |
| 395 |
} |
| 396 |
|
| 397 |
/* Check the leaf header */ |
| 398 |
xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &leafhdr, leaf); |
| 399 |
hdrsize = xfs_attr3_leaf_hdr_size(leaf); |
| 400 |
|
| 401 |
/* |
| 402 |
* Empty xattr leaf blocks mapped at block 0 are probably a byproduct |
| 403 |
* of a race between setxattr and a log shutdown. Anywhere else in the |
| 404 |
* attr fork is a corruption. |
| 405 |
*/ |
| 406 |
if (leafhdr.count == 0) { |
| 407 |
if (blk->blkno == 0) |
| 408 |
xchk_da_set_preen(ds, level); |
| 409 |
else |
| 410 |
xchk_da_set_corrupt(ds, level); |
| 411 |
} |
| 412 |
if (leafhdr.usedbytes > mp->m_attr_geo->blksize) |
| 413 |
xchk_da_set_corrupt(ds, level); |
| 414 |
if (leafhdr.firstused > mp->m_attr_geo->blksize) |
| 415 |
xchk_da_set_corrupt(ds, level); |
| 416 |
if (leafhdr.firstused < hdrsize) |
| 417 |
xchk_da_set_corrupt(ds, level); |
| 418 |
if (!xchk_xattr_set_map(ds->sc, ab->usedmap, 0, hdrsize)) |
| 419 |
xchk_da_set_corrupt(ds, level); |
| 420 |
if (leafhdr.holes) |
| 421 |
xchk_da_set_preen(ds, level); |
| 422 |
|
| 423 |
if (ds->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) |
| 424 |
goto out; |
| 425 |
|
| 426 |
entries = xfs_attr3_leaf_entryp(leaf); |
| 427 |
if ((char *)&entries[leafhdr.count] > (char *)leaf + leafhdr.firstused) |
| 428 |
xchk_da_set_corrupt(ds, level); |
| 429 |
|
| 430 |
if (ds->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) |
| 431 |
goto out; |
| 432 |
|
| 433 |
buf_end = (char *)bp->b_addr + mp->m_attr_geo->blksize; |
| 434 |
for (i = 0, ent = entries; i < leafhdr.count; ent++, i++) { |
| 435 |
/* Mark the leaf entry itself. */ |
| 436 |
off = (char *)ent - (char *)leaf; |
| 437 |
if (!xchk_xattr_set_map(ds->sc, ab->usedmap, off, |
| 438 |
sizeof(xfs_attr_leaf_entry_t))) { |
| 439 |
xchk_da_set_corrupt(ds, level); |
| 440 |
goto out; |
| 441 |
} |
| 442 |
|
| 443 |
/* Check the entry and nameval. */ |
| 444 |
xchk_xattr_entry(ds, level, buf_end, leaf, &leafhdr, |
| 445 |
ent, i, &usedbytes, &last_hashval); |
| 446 |
|
| 447 |
if (ds->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) |
| 448 |
goto out; |
| 449 |
} |
| 450 |
|
| 451 |
/* Construct bitmap of freemap contents. */ |
| 452 |
for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) { |
| 453 |
if (!xchk_xattr_set_map(ds->sc, ab->freemap, |
| 454 |
leafhdr.freemap[i].base, |
| 455 |
leafhdr.freemap[i].size)) |
| 456 |
xchk_da_set_corrupt(ds, level); |
| 457 |
|
| 458 |
/* |
| 459 |
* freemap entries with zero length and nonzero base can cause |
| 460 |
* problems with older kernels, so we mark these for preening |
| 461 |
* even though there's no inconsistency. |
| 462 |
*/ |
| 463 |
if (leafhdr.freemap[i].size == 0 && |
| 464 |
leafhdr.freemap[i].base > 0) |
| 465 |
xchk_da_set_preen(ds, level); |
| 466 |
|
| 467 |
if (ds->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) |
| 468 |
goto out; |
| 469 |
} |
| 470 |
|
| 471 |
/* Look for bits that are set in freemap and are marked in use. */ |
| 472 |
if (bitmap_intersects(ab->freemap, ab->usedmap, |
| 473 |
mp->m_attr_geo->blksize)) |
| 474 |
xchk_da_set_corrupt(ds, level); |
| 475 |
|
| 476 |
if (leafhdr.usedbytes != usedbytes) |
| 477 |
xchk_da_set_corrupt(ds, level); |
| 478 |
|
| 479 |
out: |
| 480 |
return 0; |
| 481 |
} |
| 482 |
|
| 483 |
/* Scrub a attribute btree record. */ |
| 484 |
STATIC int |
| 485 |
xchk_xattr_rec( |
| 486 |
struct xchk_da_btree *ds, |
| 487 |
int level) |
| 488 |
{ |
| 489 |
struct xfs_mount *mp = ds->state->mp; |
| 490 |
struct xfs_da_state_blk *blk = &ds->state->path.blk[level]; |
| 491 |
struct xfs_attr_leaf_name_local *lentry; |
| 492 |
struct xfs_attr_leaf_name_remote *rentry; |
| 493 |
struct xfs_buf *bp; |
| 494 |
struct xfs_attr_leaf_entry *ent; |
| 495 |
xfs_dahash_t calc_hash; |
| 496 |
xfs_dahash_t hash; |
| 497 |
int nameidx; |
| 498 |
int hdrsize; |
| 499 |
int error; |
| 500 |
|
| 501 |
ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC); |
| 502 |
|
| 503 |
ent = xfs_attr3_leaf_entryp(blk->bp->b_addr) + blk->index; |
| 504 |
|
| 505 |
/* Check the whole block, if necessary. */ |
| 506 |
error = xchk_xattr_block(ds, level); |
| 507 |
if (error) |
| 508 |
goto out; |
| 509 |
if (ds->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) |
| 510 |
goto out; |
| 511 |
|
| 512 |
/* Check the hash of the entry. */ |
| 513 |
error = xchk_da_btree_hash(ds, level, &ent->hashval); |
| 514 |
if (error) |
| 515 |
goto out; |
| 516 |
|
| 517 |
/* Find the attr entry's location. */ |
| 518 |
bp = blk->bp; |
| 519 |
hdrsize = xfs_attr3_leaf_hdr_size(bp->b_addr); |
| 520 |
nameidx = be16_to_cpu(ent->nameidx); |
| 521 |
if (nameidx < hdrsize || nameidx >= mp->m_attr_geo->blksize) { |
| 522 |
xchk_da_set_corrupt(ds, level); |
| 523 |
goto out; |
| 524 |
} |
| 525 |
|
| 526 |
/* Retrieve the entry and check it. */ |
| 527 |
hash = be32_to_cpu(ent->hashval); |
| 528 |
if (ent->flags & ~XFS_ATTR_ONDISK_MASK) { |
| 529 |
xchk_da_set_corrupt(ds, level); |
| 530 |
return 0; |
| 531 |
} |
| 532 |
if (!xfs_attr_check_namespace(ent->flags)) { |
| 533 |
xchk_da_set_corrupt(ds, level); |
| 534 |
return 0; |
| 535 |
} |
| 536 |
|
| 537 |
if (ent->flags & XFS_ATTR_LOCAL) { |
| 538 |
lentry = (struct xfs_attr_leaf_name_local *) |
| 539 |
(((char *)bp->b_addr) + nameidx); |
| 540 |
if (lentry->namelen <= 0) { |
| 541 |
xchk_da_set_corrupt(ds, level); |
| 542 |
goto out; |
| 543 |
} |
| 544 |
calc_hash = xfs_attr_hashval(mp, ent->flags, lentry->nameval, |
| 545 |
lentry->namelen, |
| 546 |
lentry->nameval + lentry->namelen, |
| 547 |
be16_to_cpu(lentry->valuelen)); |
| 548 |
} else { |
| 549 |
rentry = (struct xfs_attr_leaf_name_remote *) |
| 550 |
(((char *)bp->b_addr) + nameidx); |
| 551 |
if (rentry->namelen <= 0) { |
| 552 |
xchk_da_set_corrupt(ds, level); |
| 553 |
goto out; |
| 554 |
} |
| 555 |
if (ent->flags & XFS_ATTR_PARENT) { |
| 556 |
xchk_da_set_corrupt(ds, level); |
| 557 |
goto out; |
| 558 |
} |
| 559 |
calc_hash = xfs_attr_hashval(mp, ent->flags, rentry->name, |
| 560 |
rentry->namelen, NULL, |
| 561 |
be32_to_cpu(rentry->valuelen)); |
| 562 |
} |
| 563 |
if (calc_hash != hash) |
| 564 |
xchk_da_set_corrupt(ds, level); |
| 565 |
|
| 566 |
out: |
| 567 |
return error; |
| 568 |
} |
| 569 |
|
| 570 |
/* Check space usage of shortform attrs. */ |
| 571 |
STATIC int |
| 572 |
xchk_xattr_check_sf( |
| 573 |
struct xfs_scrub *sc) |
| 574 |
{ |
| 575 |
struct xchk_xattr_buf *ab = sc->buf; |
| 576 |
struct xfs_ifork *ifp = &sc->ip->i_af; |
| 577 |
struct xfs_attr_sf_hdr *sf = ifp->if_data; |
| 578 |
struct xfs_attr_sf_entry *sfe = xfs_attr_sf_firstentry(sf); |
| 579 |
struct xfs_attr_sf_entry *next; |
| 580 |
unsigned char *end = ifp->if_data + ifp->if_bytes; |
| 581 |
int i; |
| 582 |
int error = 0; |
| 583 |
|
| 584 |
bitmap_zero(ab->usedmap, ifp->if_bytes); |
| 585 |
xchk_xattr_set_map(sc, ab->usedmap, 0, sizeof(*sf)); |
| 586 |
|
| 587 |
if ((unsigned char *)sfe > end) { |
| 588 |
xchk_fblock_set_corrupt(sc, XFS_ATTR_FORK, 0); |
| 589 |
return 0; |
| 590 |
} |
| 591 |
|
| 592 |
for (i = 0; i < sf->count; i++) { |
| 593 |
unsigned char *name = sfe->nameval; |
| 594 |
unsigned char *value = &sfe->nameval[sfe->namelen]; |
| 595 |
|
| 596 |
if (xchk_should_terminate(sc, &error)) |
| 597 |
return error; |
| 598 |
|
| 599 |
next = xfs_attr_sf_nextentry(sfe); |
| 600 |
if ((unsigned char *)next > end) { |
| 601 |
xchk_fblock_set_corrupt(sc, XFS_ATTR_FORK, 0); |
| 602 |
break; |
| 603 |
} |
| 604 |
|
| 605 |
/* |
| 606 |
* Shortform entries do not set LOCAL or INCOMPLETE, so the |
| 607 |
* only valid flag bits here are for namespaces. |
| 608 |
*/ |
| 609 |
if (sfe->flags & ~XFS_ATTR_NSP_ONDISK_MASK) { |
| 610 |
xchk_fblock_set_corrupt(sc, XFS_ATTR_FORK, 0); |
| 611 |
break; |
| 612 |
} |
| 613 |
|
| 614 |
if (!xchk_xattr_set_map(sc, ab->usedmap, |
| 615 |
(char *)sfe - (char *)sf, |
| 616 |
sizeof(struct xfs_attr_sf_entry))) { |
| 617 |
xchk_fblock_set_corrupt(sc, XFS_ATTR_FORK, 0); |
| 618 |
break; |
| 619 |
} |
| 620 |
|
| 621 |
if (!xchk_xattr_set_map(sc, ab->usedmap, |
| 622 |
(char *)name - (char *)sf, |
| 623 |
sfe->namelen)) { |
| 624 |
xchk_fblock_set_corrupt(sc, XFS_ATTR_FORK, 0); |
| 625 |
break; |
| 626 |
} |
| 627 |
|
| 628 |
if (!xchk_xattr_set_map(sc, ab->usedmap, |
| 629 |
(char *)value - (char *)sf, |
| 630 |
sfe->valuelen)) { |
| 631 |
xchk_fblock_set_corrupt(sc, XFS_ATTR_FORK, 0); |
| 632 |
break; |
| 633 |
} |
| 634 |
|
| 635 |
sfe = next; |
| 636 |
} |
| 637 |
|
| 638 |
return 0; |
| 639 |
} |
| 640 |
|
| 641 |
/* Scrub the extended attribute metadata. */ |
| 642 |
int |
| 643 |
xchk_xattr( |
| 644 |
struct xfs_scrub *sc) |
| 645 |
{ |
| 646 |
xfs_dablk_t last_checked = -1U; |
| 647 |
int error = 0; |
| 648 |
|
| 649 |
if (!xfs_inode_hasattr(sc->ip)) |
| 650 |
return -ENOENT; |
| 651 |
|
| 652 |
/* Allocate memory for xattr checking. */ |
| 653 |
error = xchk_setup_xattr_buf(sc, 0); |
| 654 |
if (error == -ENOMEM) |
| 655 |
return -EDEADLOCK; |
| 656 |
if (error) |
| 657 |
return error; |
| 658 |
|
| 659 |
/* Check the physical structure of the xattr. */ |
| 660 |
if (sc->ip->i_af.if_format == XFS_DINODE_FMT_LOCAL) |
| 661 |
error = xchk_xattr_check_sf(sc); |
| 662 |
else |
| 663 |
error = xchk_da_btree(sc, XFS_ATTR_FORK, xchk_xattr_rec, |
| 664 |
&last_checked); |
| 665 |
if (error) |
| 666 |
return error; |
| 667 |
|
| 668 |
if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) |
| 669 |
return 0; |
| 670 |
|
| 671 |
/* |
| 672 |
* Look up every xattr in this file by name and hash. |
| 673 |
* |
| 674 |
* The VFS only locks i_rwsem when modifying attrs, so keep all |
| 675 |
* three locks held because that's the only way to ensure we're |
| 676 |
* the only thread poking into the da btree. We traverse the da |
| 677 |
* btree while holding a leaf buffer locked for the xattr name |
| 678 |
* iteration, which doesn't really follow the usual buffer |
| 679 |
* locking order. |
| 680 |
*/ |
| 681 |
error = xchk_xattr_walk(sc, sc->ip, xchk_xattr_actor, NULL, NULL); |
| 682 |
if (!xchk_fblock_process_error(sc, XFS_ATTR_FORK, 0, &error)) |
| 683 |
return error; |
| 684 |
|
| 685 |
return 0; |
| 686 |
} |