Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (C) 2017 Oracle.  All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Author: Darrick J. Wong <darrick.wong@oracle.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include "xfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include "xfs_fs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include "xfs_shared.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include "xfs_format.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include "xfs_trans_resv.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include "xfs_mount.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include "xfs_log_format.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include "xfs_inode.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include "xfs_da_format.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include "xfs_da_btree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include "xfs_attr.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include "xfs_attr_leaf.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include "scrub/scrub.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include "scrub/common.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include "scrub/dabtree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include "scrub/attr.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * Allocate enough memory to hold an attr value and attr block bitmaps,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  * reallocating the buffer if necessary.  Buffer contents are not preserved
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * across a reallocation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) xchk_setup_xattr_buf(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	struct xfs_scrub	*sc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	size_t			value_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	xfs_km_flags_t		flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	size_t			sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	struct xchk_xattr_buf	*ab = sc->buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	 * We need enough space to read an xattr value from the file or enough
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	 * space to hold three copies of the xattr free space bitmap.  We don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	 * need the buffer space for both purposes at the same time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	sz = 3 * sizeof(long) * BITS_TO_LONGS(sc->mp->m_attr_geo->blksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	sz = max_t(size_t, sz, value_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	 * If there's already a buffer, figure out if we need to reallocate it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	 * to accommodate a larger size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	if (ab) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		if (sz <= ab->sz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 		kmem_free(ab);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		sc->buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	 * Don't zero the buffer upon allocation to avoid runtime overhead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	 * All users must be careful never to read uninitialized contents.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	ab = kmem_alloc_large(sizeof(*ab) + sz, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	if (!ab)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	ab->sz = sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	sc->buf = ab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) /* Set us up to scrub an inode's extended attributes. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) xchk_setup_xattr(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	struct xfs_scrub	*sc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	struct xfs_inode	*ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	 * We failed to get memory while checking attrs, so this time try to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	 * get all the memory we're ever going to need.  Allocate the buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	 * without the inode lock held, which means we can sleep.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	if (sc->flags & XCHK_TRY_HARDER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		error = xchk_setup_xattr_buf(sc, XATTR_SIZE_MAX, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	return xchk_setup_inode_contents(sc, ip, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) /* Extended Attributes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) struct xchk_xattr {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	struct xfs_attr_list_context	context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	struct xfs_scrub		*sc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99)  * Check that an extended attribute key can be looked up by hash.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)  * We use the XFS attribute list iterator (i.e. xfs_attr_list_ilocked)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)  * to call this function for every attribute key in an inode.  Once
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)  * we're here, we load the attribute value to see if any errors happen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)  * or if we get more or less data than we expected.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) xchk_xattr_listent(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	struct xfs_attr_list_context	*context,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	int				flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	unsigned char			*name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	int				namelen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	int				valuelen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	struct xchk_xattr		*sx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	struct xfs_da_args		args = { NULL };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	int				error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	sx = container_of(context, struct xchk_xattr, context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	if (xchk_should_terminate(sx->sc, &error)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		context->seen_enough = error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	if (flags & XFS_ATTR_INCOMPLETE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		/* Incomplete attr key, just mark the inode for preening. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		xchk_ino_set_preen(sx->sc, context->dp->i_ino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	/* Does this name make sense? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	if (!xfs_attr_namecheck(name, namelen)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 		xchk_fblock_set_corrupt(sx->sc, XFS_ATTR_FORK, args.blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	 * Try to allocate enough memory to extrat the attr value.  If that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	 * doesn't work, we overload the seen_enough variable to convey
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	 * the error message back to the main scrub function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	error = xchk_setup_xattr_buf(sx->sc, valuelen, KM_MAYFAIL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	if (error == -ENOMEM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		error = -EDEADLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		context->seen_enough = error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	args.op_flags = XFS_DA_OP_NOTIME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	args.attr_filter = flags & XFS_ATTR_NSP_ONDISK_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	args.geo = context->dp->i_mount->m_attr_geo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	args.whichfork = XFS_ATTR_FORK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	args.dp = context->dp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	args.name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	args.namelen = namelen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	args.hashval = xfs_da_hashname(args.name, args.namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	args.trans = context->tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	args.value = xchk_xattr_valuebuf(sx->sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	args.valuelen = valuelen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	error = xfs_attr_get_ilocked(&args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	/* ENODATA means the hash lookup failed and the attr is bad */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	if (error == -ENODATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	if (!xchk_fblock_process_error(sx->sc, XFS_ATTR_FORK, args.blkno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 			&error))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		goto fail_xref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 	if (args.valuelen != valuelen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		xchk_fblock_set_corrupt(sx->sc, XFS_ATTR_FORK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 					     args.blkno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) fail_xref:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	if (sx->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		context->seen_enough = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)  * Mark a range [start, start+len) in this map.  Returns true if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)  * region was free, and false if there's a conflict or a problem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)  * Within a char, the lowest bit of the char represents the byte with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)  * the smallest address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) STATIC bool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) xchk_xattr_set_map(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	struct xfs_scrub	*sc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	unsigned long		*map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	unsigned int		start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	unsigned int		len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	unsigned int		mapsize = sc->mp->m_attr_geo->blksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	bool			ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	if (start >= mapsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	if (start + len > mapsize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		len = mapsize - start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		ret = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	if (find_next_bit(map, mapsize, start) < start + len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 		ret = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	bitmap_set(map, start, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)  * Check the leaf freemap from the usage bitmap.  Returns false if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)  * attr freemap has problems or points to used space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) STATIC bool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) xchk_xattr_check_freemap(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	struct xfs_scrub		*sc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	unsigned long			*map,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	struct xfs_attr3_icleaf_hdr	*leafhdr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	unsigned long			*freemap = xchk_xattr_freemap(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	unsigned long			*dstmap = xchk_xattr_dstmap(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	unsigned int			mapsize = sc->mp->m_attr_geo->blksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	int				i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	/* Construct bitmap of freemap contents. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	bitmap_zero(freemap, mapsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 		if (!xchk_xattr_set_map(sc, freemap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 				leafhdr->freemap[i].base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 				leafhdr->freemap[i].size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 			return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 	/* Look for bits that are set in freemap and are marked in use. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	return bitmap_and(dstmap, freemap, map, mapsize) == 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)  * Check this leaf entry's relations to everything else.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)  * Returns the number of bytes used for the name/value data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) STATIC void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) xchk_xattr_entry(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	struct xchk_da_btree		*ds,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	int				level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	char				*buf_end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	struct xfs_attr_leafblock	*leaf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	struct xfs_attr3_icleaf_hdr	*leafhdr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	struct xfs_attr_leaf_entry	*ent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	int				idx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	unsigned int			*usedbytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	__u32				*last_hashval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	struct xfs_mount		*mp = ds->state->mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	unsigned long			*usedmap = xchk_xattr_usedmap(ds->sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	char				*name_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	struct xfs_attr_leaf_name_local	*lentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	struct xfs_attr_leaf_name_remote *rentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	unsigned int			nameidx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	unsigned int			namesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 	if (ent->pad2 != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		xchk_da_set_corrupt(ds, level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	/* Hash values in order? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	if (be32_to_cpu(ent->hashval) < *last_hashval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		xchk_da_set_corrupt(ds, level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	*last_hashval = be32_to_cpu(ent->hashval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	nameidx = be16_to_cpu(ent->nameidx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	if (nameidx < leafhdr->firstused ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	    nameidx >= mp->m_attr_geo->blksize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		xchk_da_set_corrupt(ds, level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	/* Check the name information. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	if (ent->flags & XFS_ATTR_LOCAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		lentry = xfs_attr3_leaf_name_local(leaf, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		namesize = xfs_attr_leaf_entsize_local(lentry->namelen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 				be16_to_cpu(lentry->valuelen));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		name_end = (char *)lentry + namesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		if (lentry->namelen == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 			xchk_da_set_corrupt(ds, level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 		rentry = xfs_attr3_leaf_name_remote(leaf, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 		namesize = xfs_attr_leaf_entsize_remote(rentry->namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		name_end = (char *)rentry + namesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		if (rentry->namelen == 0 || rentry->valueblk == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 			xchk_da_set_corrupt(ds, level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	if (name_end > buf_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		xchk_da_set_corrupt(ds, level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	if (!xchk_xattr_set_map(ds->sc, usedmap, nameidx, namesize))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		xchk_da_set_corrupt(ds, level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	if (!(ds->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		*usedbytes += namesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) /* Scrub an attribute leaf. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) xchk_xattr_block(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	struct xchk_da_btree		*ds,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	int				level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	struct xfs_attr3_icleaf_hdr	leafhdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 	struct xfs_mount		*mp = ds->state->mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	struct xfs_da_state_blk		*blk = &ds->state->path.blk[level];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	struct xfs_buf			*bp = blk->bp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	xfs_dablk_t			*last_checked = ds->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	struct xfs_attr_leafblock	*leaf = bp->b_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	struct xfs_attr_leaf_entry	*ent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	struct xfs_attr_leaf_entry	*entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	unsigned long			*usedmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	char				*buf_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	size_t				off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	__u32				last_hashval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	unsigned int			usedbytes = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	unsigned int			hdrsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	int				i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	int				error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	if (*last_checked == blk->blkno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	/* Allocate memory for block usage checking. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	error = xchk_setup_xattr_buf(ds->sc, 0, KM_MAYFAIL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	if (error == -ENOMEM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		return -EDEADLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	usedmap = xchk_xattr_usedmap(ds->sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	*last_checked = blk->blkno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 	bitmap_zero(usedmap, mp->m_attr_geo->blksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	/* Check all the padding. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	if (xfs_sb_version_hascrc(&ds->sc->mp->m_sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 		struct xfs_attr3_leafblock	*leaf = bp->b_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 		if (leaf->hdr.pad1 != 0 || leaf->hdr.pad2 != 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 		    leaf->hdr.info.hdr.pad != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 			xchk_da_set_corrupt(ds, level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 		if (leaf->hdr.pad1 != 0 || leaf->hdr.info.pad != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 			xchk_da_set_corrupt(ds, level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	/* Check the leaf header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &leafhdr, leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	hdrsize = xfs_attr3_leaf_hdr_size(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	if (leafhdr.usedbytes > mp->m_attr_geo->blksize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 		xchk_da_set_corrupt(ds, level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	if (leafhdr.firstused > mp->m_attr_geo->blksize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 		xchk_da_set_corrupt(ds, level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	if (leafhdr.firstused < hdrsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 		xchk_da_set_corrupt(ds, level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	if (!xchk_xattr_set_map(ds->sc, usedmap, 0, hdrsize))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 		xchk_da_set_corrupt(ds, level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	if (ds->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	entries = xfs_attr3_leaf_entryp(leaf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	if ((char *)&entries[leafhdr.count] > (char *)leaf + leafhdr.firstused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 		xchk_da_set_corrupt(ds, level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	buf_end = (char *)bp->b_addr + mp->m_attr_geo->blksize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	for (i = 0, ent = entries; i < leafhdr.count; ent++, i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		/* Mark the leaf entry itself. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 		off = (char *)ent - (char *)leaf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 		if (!xchk_xattr_set_map(ds->sc, usedmap, off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 				sizeof(xfs_attr_leaf_entry_t))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 			xchk_da_set_corrupt(ds, level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 		/* Check the entry and nameval. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		xchk_xattr_entry(ds, level, buf_end, leaf, &leafhdr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 				ent, i, &usedbytes, &last_hashval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 		if (ds->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	if (!xchk_xattr_check_freemap(ds->sc, usedmap, &leafhdr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 		xchk_da_set_corrupt(ds, level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	if (leafhdr.usedbytes != usedbytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 		xchk_da_set_corrupt(ds, level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) /* Scrub a attribute btree record. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) xchk_xattr_rec(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	struct xchk_da_btree		*ds,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	int				level)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 	struct xfs_mount		*mp = ds->state->mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	struct xfs_da_state_blk		*blk = &ds->state->path.blk[level];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	struct xfs_attr_leaf_name_local	*lentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 	struct xfs_attr_leaf_name_remote	*rentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 	struct xfs_buf			*bp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	struct xfs_attr_leaf_entry	*ent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	xfs_dahash_t			calc_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	xfs_dahash_t			hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	int				nameidx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 	int				hdrsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	unsigned int			badflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	int				error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 	ent = xfs_attr3_leaf_entryp(blk->bp->b_addr) + blk->index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	/* Check the whole block, if necessary. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 	error = xchk_xattr_block(ds, level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 	if (ds->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	/* Check the hash of the entry. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	error = xchk_da_btree_hash(ds, level, &ent->hashval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	/* Find the attr entry's location. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	bp = blk->bp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	hdrsize = xfs_attr3_leaf_hdr_size(bp->b_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	nameidx = be16_to_cpu(ent->nameidx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	if (nameidx < hdrsize || nameidx >= mp->m_attr_geo->blksize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 		xchk_da_set_corrupt(ds, level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	/* Retrieve the entry and check it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	hash = be32_to_cpu(ent->hashval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	badflags = ~(XFS_ATTR_LOCAL | XFS_ATTR_ROOT | XFS_ATTR_SECURE |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 			XFS_ATTR_INCOMPLETE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	if ((ent->flags & badflags) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 		xchk_da_set_corrupt(ds, level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	if (ent->flags & XFS_ATTR_LOCAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 		lentry = (struct xfs_attr_leaf_name_local *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 				(((char *)bp->b_addr) + nameidx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 		if (lentry->namelen <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 			xchk_da_set_corrupt(ds, level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 		calc_hash = xfs_da_hashname(lentry->nameval, lentry->namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 		rentry = (struct xfs_attr_leaf_name_remote *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 				(((char *)bp->b_addr) + nameidx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 		if (rentry->namelen <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 			xchk_da_set_corrupt(ds, level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 		calc_hash = xfs_da_hashname(rentry->name, rentry->namelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	if (calc_hash != hash)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 		xchk_da_set_corrupt(ds, level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) /* Scrub the extended attribute metadata. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) xchk_xattr(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	struct xfs_scrub		*sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	struct xchk_xattr		sx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 	xfs_dablk_t			last_checked = -1U;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	int				error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	if (!xfs_inode_hasattr(sc->ip))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	memset(&sx, 0, sizeof(sx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	/* Check attribute tree structure */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	error = xchk_da_btree(sc, XFS_ATTR_FORK, xchk_xattr_rec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 			&last_checked);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	/* Check that every attr key can also be looked up by hash. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	sx.context.dp = sc->ip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	sx.context.resynch = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	sx.context.put_listent = xchk_xattr_listent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	sx.context.tp = sc->tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	sx.context.allow_incomplete = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	sx.sc = sc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	 * Look up every xattr in this file by name.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	 * Use the backend implementation of xfs_attr_list to call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	 * xchk_xattr_listent on every attribute key in this inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	 * In other words, we use the same iterator/callback mechanism
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	 * that listattr uses to scrub extended attributes, though in our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	 * _listent function, we check the value of the attribute.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	 * The VFS only locks i_rwsem when modifying attrs, so keep all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	 * three locks held because that's the only way to ensure we're
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	 * the only thread poking into the da btree.  We traverse the da
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	 * btree while holding a leaf buffer locked for the xattr name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	 * iteration, which doesn't really follow the usual buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	 * locking order.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	error = xfs_attr_list_ilocked(&sx.context);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	if (!xchk_fblock_process_error(sc, XFS_ATTR_FORK, 0, &error))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	/* Did our listent function try to return any errors? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	if (sx.context.seen_enough < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 		error = sx.context.seen_enough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) }