/* * Allocate the free space bitmap if we're trying harder; there are leaf blocks * in the attr fork; or we can't tell if there are leaf blocks.
*/ staticinlinebool
xchk_xattr_want_freemap( struct xfs_scrub *sc)
{ struct xfs_ifork *ifp;
if (sc->flags & XCHK_TRY_HARDER) returntrue;
if (!sc->ip) returntrue;
ifp = xfs_ifork_ptr(sc->ip, XFS_ATTR_FORK); if (!ifp) returnfalse;
return xfs_ifork_has_extents(ifp);
}
/* * Allocate enough memory to hold an attr value and attr block bitmaps, * reallocating the buffer if necessary. Buffer contents are not preserved * across a reallocation.
*/ int
xchk_setup_xattr_buf( struct xfs_scrub *sc,
size_t value_size)
{
size_t bmp_sz; struct xchk_xattr_buf *ab = sc->buf; void *new_val;
/* Set us up to scrub an inode's extended attributes. */ int
xchk_setup_xattr( struct xfs_scrub *sc)
{ int error;
if (xchk_could_repair(sc)) {
error = xrep_setup_xattr(sc); if (error) return error;
}
/* * We failed to get memory while checking attrs, so this time try to * get all the memory we're ever going to need. Allocate the buffer * without the inode lock held, which means we can sleep.
*/ if (sc->flags & XCHK_TRY_HARDER) {
error = xchk_setup_xattr_buf(sc, XATTR_SIZE_MAX); if (error) return error;
}
return xchk_setup_inode_contents(sc, 0);
}
/* Extended Attributes */
/* * Check that an extended attribute key can be looked up by hash. * * We use the extended attribute walk helper to call this function for every * attribute key in an inode. Once we're here, we load the attribute value to * see if any errors happen, or if we get more or less data than we expected.
*/ staticint
xchk_xattr_actor( struct xfs_scrub *sc, struct xfs_inode *ip, unsignedint attr_flags, constunsignedchar *name, unsignedint namelen, constvoid *value, unsignedint valuelen, void *priv)
{ struct xfs_da_args args = {
.attr_filter = attr_flags & XFS_ATTR_NSP_ONDISK_MASK,
.geo = sc->mp->m_attr_geo,
.whichfork = XFS_ATTR_FORK,
.dp = ip,
.name = name,
.namelen = namelen,
.trans = sc->tp,
.valuelen = valuelen,
.owner = ip->i_ino,
}; struct xchk_xattr_buf *ab; int error = 0;
ab = sc->buf;
if (xchk_should_terminate(sc, &error)) return error;
if (attr_flags & XFS_ATTR_INCOMPLETE) { /* Incomplete attr key, just mark the inode for preening. */
xchk_ino_set_preen(sc, ip->i_ino); return 0;
}
/* Does this name make sense? */ if (!xfs_attr_namecheck(attr_flags, name, namelen)) {
xchk_fblock_set_corrupt(sc, XFS_ATTR_FORK, args.blkno); return -ECANCELED;
}
/* * Try to allocate enough memory to extract the attr value. If that * doesn't work, return -EDEADLOCK as a signal to try again with a * maximally sized buffer.
*/
error = xchk_setup_xattr_buf(sc, valuelen); if (error == -ENOMEM)
error = -EDEADLOCK; if (error) return error;
/* * Parent pointers are matched on attr name and value, so we must * supply the xfs_parent_rec here when confirming that the dabtree * indexing works correctly.
*/ if (attr_flags & XFS_ATTR_PARENT)
memcpy(ab->value, value, valuelen);
args.value = ab->value;
/* * Get the attr value to ensure that lookup can find this attribute * through the dabtree indexing and that remote value retrieval also * works correctly.
*/
xfs_attr_sethash(&args);
error = xfs_attr_get_ilocked(&args); /* ENODATA means the hash lookup failed and the attr is bad */ if (error == -ENODATA)
error = -EFSCORRUPTED; if (!xchk_fblock_process_error(sc, XFS_ATTR_FORK, args.blkno,
&error)) return error; if (args.valuelen != valuelen)
xchk_fblock_set_corrupt(sc, XFS_ATTR_FORK, args.blkno);
return 0;
}
/* * Mark a range [start, start+len) in this map. Returns true if the * region was free, and false if there's a conflict or a problem. * * Within a char, the lowest bit of the char represents the byte with * the smallest address
*/ bool
xchk_xattr_set_map( struct xfs_scrub *sc, unsignedlong *map, unsignedint start, unsignedint len)
{ unsignedint mapsize = sc->mp->m_attr_geo->blksize; bool ret = true;
if (start >= mapsize) returnfalse; if (start + len > mapsize) {
len = mapsize - start;
ret = false;
}
if (find_next_bit(map, mapsize, start) < start + len)
ret = false;
bitmap_set(map, start, len);
return ret;
}
/* * Check the leaf freemap from the usage bitmap. Returns false if the * attr freemap has problems or points to used space.
*/ STATICbool
xchk_xattr_check_freemap( struct xfs_scrub *sc, struct xfs_attr3_icleaf_hdr *leafhdr)
{ struct xchk_xattr_buf *ab = sc->buf; unsignedint mapsize = sc->mp->m_attr_geo->blksize; int i;
/* Construct bitmap of freemap contents. */
bitmap_zero(ab->freemap, mapsize); for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) { if (!xchk_xattr_set_map(sc, ab->freemap,
leafhdr->freemap[i].base,
leafhdr->freemap[i].size)) returnfalse;
}
/* Look for bits that are set in freemap and are marked in use. */ return !bitmap_intersects(ab->freemap, ab->usedmap, mapsize);
}
/* * Check this leaf entry's relations to everything else. * Returns the number of bytes used for the name/value data.
*/ STATICvoid
xchk_xattr_entry( struct xchk_da_btree *ds, int level, char *buf_end, struct xfs_attr_leafblock *leaf, struct xfs_attr3_icleaf_hdr *leafhdr, struct xfs_attr_leaf_entry *ent, int idx, unsignedint *usedbytes,
__u32 *last_hashval)
{ struct xfs_mount *mp = ds->state->mp; struct xchk_xattr_buf *ab = ds->sc->buf; char *name_end; struct xfs_attr_leaf_name_local *lentry; struct xfs_attr_leaf_name_remote *rentry; unsignedint nameidx; unsignedint namesize;
if (ent->pad2 != 0)
xchk_da_set_corrupt(ds, level);
/* Hash values in order? */ if (be32_to_cpu(ent->hashval) < *last_hashval)
xchk_da_set_corrupt(ds, level);
*last_hashval = be32_to_cpu(ent->hashval);
/* * Empty xattr leaf blocks mapped at block 0 are probably a byproduct * of a race between setxattr and a log shutdown. Anywhere else in the * attr fork is a corruption.
*/ if (leafhdr.count == 0) { if (blk->blkno == 0)
xchk_da_set_preen(ds, level); else
xchk_da_set_corrupt(ds, level);
} if (leafhdr.usedbytes > mp->m_attr_geo->blksize)
xchk_da_set_corrupt(ds, level); if (leafhdr.firstused > mp->m_attr_geo->blksize)
xchk_da_set_corrupt(ds, level); if (leafhdr.firstused < hdrsize)
xchk_da_set_corrupt(ds, level); if (!xchk_xattr_set_map(ds->sc, ab->usedmap, 0, hdrsize))
xchk_da_set_corrupt(ds, level); if (leafhdr.holes)
xchk_da_set_preen(ds, level);
if (ds->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) goto out;
for (i = 0; i < sf->count; i++) { unsignedchar *name = sfe->nameval; unsignedchar *value = &sfe->nameval[sfe->namelen];
if (xchk_should_terminate(sc, &error)) return error;
next = xfs_attr_sf_nextentry(sfe); if ((unsignedchar *)next > end) {
xchk_fblock_set_corrupt(sc, XFS_ATTR_FORK, 0); break;
}
/* * Shortform entries do not set LOCAL or INCOMPLETE, so the * only valid flag bits here are for namespaces.
*/ if (sfe->flags & ~XFS_ATTR_NSP_ONDISK_MASK) {
xchk_fblock_set_corrupt(sc, XFS_ATTR_FORK, 0); break;
}
/* Scrub the extended attribute metadata. */ int
xchk_xattr( struct xfs_scrub *sc)
{
xfs_dablk_t last_checked = -1U; int error = 0;
if (!xfs_inode_hasattr(sc->ip)) return -ENOENT;
/* Allocate memory for xattr checking. */
error = xchk_setup_xattr_buf(sc, 0); if (error == -ENOMEM) return -EDEADLOCK; if (error) return error;
/* Check the physical structure of the xattr. */ if (sc->ip->i_af.if_format == XFS_DINODE_FMT_LOCAL)
error = xchk_xattr_check_sf(sc); else
error = xchk_da_btree(sc, XFS_ATTR_FORK, xchk_xattr_rec,
&last_checked); if (error) return error;
if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT) return 0;
/* * Look up every xattr in this file by name and hash. * * The VFS only locks i_rwsem when modifying attrs, so keep all * three locks held because that's the only way to ensure we're * the only thread poking into the da btree. We traverse the da * btree while holding a leaf buffer locked for the xattr name * iteration, which doesn't really follow the usual buffer * locking order.
*/
error = xchk_xattr_walk(sc, sc->ip, xchk_xattr_actor, NULL, NULL); if (!xchk_fblock_process_error(sc, XFS_ATTR_FORK, 0, &error)) return error;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.