1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2017 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
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_inode.h"
14 #include "xfs_da_format.h"
15 #include "xfs_da_btree.h"
17 #include "xfs_attr_leaf.h"
18 #include "scrub/scrub.h"
19 #include "scrub/common.h"
20 #include "scrub/dabtree.h"
21 #include "scrub/attr.h"
24 * Allocate enough memory to hold an attr value and attr block bitmaps,
25 * reallocating the buffer if necessary. Buffer contents are not preserved
26 * across a reallocation.
35 struct xchk_xattr_buf *ab = sc->buf;
38 * We need enough space to read an xattr value from the file or enough
39 * space to hold three copies of the xattr free space bitmap. We don't
40 * need the buffer space for both purposes at the same time.
42 sz = 3 * sizeof(long) * BITS_TO_LONGS(sc->mp->m_attr_geo->blksize);
43 sz = max_t(size_t, sz, value_size);
46 * If there's already a buffer, figure out if we need to reallocate it
47 * to accommodate a larger size.
57 * Don't zero the buffer upon allocation to avoid runtime overhead.
58 * All users must be careful never to read uninitialized contents.
60 ab = kmem_alloc_large(sizeof(*ab) + sz, flags);
69 /* Set us up to scrub an inode's extended attributes. */
78 * We failed to get memory while checking attrs, so this time try to
79 * get all the memory we're ever going to need. Allocate the buffer
80 * without the inode lock held, which means we can sleep.
82 if (sc->flags & XCHK_TRY_HARDER) {
83 error = xchk_setup_xattr_buf(sc, XATTR_SIZE_MAX, 0);
88 return xchk_setup_inode_contents(sc, ip, 0);
91 /* Extended Attributes */
94 struct xfs_attr_list_context context;
99 * Check that an extended attribute key can be looked up by hash.
101 * We use the XFS attribute list iterator (i.e. xfs_attr_list_ilocked)
102 * to call this function for every attribute key in an inode. Once
103 * we're here, we load the attribute value to see if any errors happen,
104 * or if we get more or less data than we expected.
108 struct xfs_attr_list_context *context,
114 struct xchk_xattr *sx;
115 struct xfs_da_args args = { NULL };
118 sx = container_of(context, struct xchk_xattr, context);
120 if (xchk_should_terminate(sx->sc, &error)) {
121 context->seen_enough = error;
125 if (flags & XFS_ATTR_INCOMPLETE) {
126 /* Incomplete attr key, just mark the inode for preening. */
127 xchk_ino_set_preen(sx->sc, context->dp->i_ino);
131 /* Does this name make sense? */
132 if (!xfs_attr_namecheck(name, namelen)) {
133 xchk_fblock_set_corrupt(sx->sc, XFS_ATTR_FORK, args.blkno);
138 * Try to allocate enough memory to extrat the attr value. If that
139 * doesn't work, we overload the seen_enough variable to convey
140 * the error message back to the main scrub function.
142 error = xchk_setup_xattr_buf(sx->sc, valuelen, KM_MAYFAIL);
143 if (error == -ENOMEM)
146 context->seen_enough = error;
150 args.op_flags = XFS_DA_OP_NOTIME;
151 args.attr_filter = flags & XFS_ATTR_NSP_ONDISK_MASK;
152 args.geo = context->dp->i_mount->m_attr_geo;
153 args.whichfork = XFS_ATTR_FORK;
154 args.dp = context->dp;
156 args.namelen = namelen;
157 args.hashval = xfs_da_hashname(args.name, args.namelen);
158 args.trans = context->tp;
159 args.value = xchk_xattr_valuebuf(sx->sc);
160 args.valuelen = valuelen;
162 error = xfs_attr_get_ilocked(&args);
163 if (!xchk_fblock_process_error(sx->sc, XFS_ATTR_FORK, args.blkno,
166 if (args.valuelen != valuelen)
167 xchk_fblock_set_corrupt(sx->sc, XFS_ATTR_FORK,
170 if (sx->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
171 context->seen_enough = 1;
176 * Mark a range [start, start+len) in this map. Returns true if the
177 * region was free, and false if there's a conflict or a problem.
179 * Within a char, the lowest bit of the char represents the byte with
180 * the smallest address
184 struct xfs_scrub *sc,
189 unsigned int mapsize = sc->mp->m_attr_geo->blksize;
192 if (start >= mapsize)
194 if (start + len > mapsize) {
195 len = mapsize - start;
199 if (find_next_bit(map, mapsize, start) < start + len)
201 bitmap_set(map, start, len);
207 * Check the leaf freemap from the usage bitmap. Returns false if the
208 * attr freemap has problems or points to used space.
211 xchk_xattr_check_freemap(
212 struct xfs_scrub *sc,
214 struct xfs_attr3_icleaf_hdr *leafhdr)
216 unsigned long *freemap = xchk_xattr_freemap(sc);
217 unsigned long *dstmap = xchk_xattr_dstmap(sc);
218 unsigned int mapsize = sc->mp->m_attr_geo->blksize;
221 /* Construct bitmap of freemap contents. */
222 bitmap_zero(freemap, mapsize);
223 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
224 if (!xchk_xattr_set_map(sc, freemap,
225 leafhdr->freemap[i].base,
226 leafhdr->freemap[i].size))
230 /* Look for bits that are set in freemap and are marked in use. */
231 return bitmap_and(dstmap, freemap, map, mapsize) == 0;
235 * Check this leaf entry's relations to everything else.
236 * Returns the number of bytes used for the name/value data.
240 struct xchk_da_btree *ds,
243 struct xfs_attr_leafblock *leaf,
244 struct xfs_attr3_icleaf_hdr *leafhdr,
245 struct xfs_attr_leaf_entry *ent,
247 unsigned int *usedbytes,
250 struct xfs_mount *mp = ds->state->mp;
251 unsigned long *usedmap = xchk_xattr_usedmap(ds->sc);
253 struct xfs_attr_leaf_name_local *lentry;
254 struct xfs_attr_leaf_name_remote *rentry;
255 unsigned int nameidx;
256 unsigned int namesize;
259 xchk_da_set_corrupt(ds, level);
261 /* Hash values in order? */
262 if (be32_to_cpu(ent->hashval) < *last_hashval)
263 xchk_da_set_corrupt(ds, level);
264 *last_hashval = be32_to_cpu(ent->hashval);
266 nameidx = be16_to_cpu(ent->nameidx);
267 if (nameidx < leafhdr->firstused ||
268 nameidx >= mp->m_attr_geo->blksize) {
269 xchk_da_set_corrupt(ds, level);
273 /* Check the name information. */
274 if (ent->flags & XFS_ATTR_LOCAL) {
275 lentry = xfs_attr3_leaf_name_local(leaf, idx);
276 namesize = xfs_attr_leaf_entsize_local(lentry->namelen,
277 be16_to_cpu(lentry->valuelen));
278 name_end = (char *)lentry + namesize;
279 if (lentry->namelen == 0)
280 xchk_da_set_corrupt(ds, level);
282 rentry = xfs_attr3_leaf_name_remote(leaf, idx);
283 namesize = xfs_attr_leaf_entsize_remote(rentry->namelen);
284 name_end = (char *)rentry + namesize;
285 if (rentry->namelen == 0 || rentry->valueblk == 0)
286 xchk_da_set_corrupt(ds, level);
288 if (name_end > buf_end)
289 xchk_da_set_corrupt(ds, level);
291 if (!xchk_xattr_set_map(ds->sc, usedmap, nameidx, namesize))
292 xchk_da_set_corrupt(ds, level);
293 if (!(ds->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
294 *usedbytes += namesize;
297 /* Scrub an attribute leaf. */
300 struct xchk_da_btree *ds,
303 struct xfs_attr3_icleaf_hdr leafhdr;
304 struct xfs_mount *mp = ds->state->mp;
305 struct xfs_da_state_blk *blk = &ds->state->path.blk[level];
306 struct xfs_buf *bp = blk->bp;
307 xfs_dablk_t *last_checked = ds->private;
308 struct xfs_attr_leafblock *leaf = bp->b_addr;
309 struct xfs_attr_leaf_entry *ent;
310 struct xfs_attr_leaf_entry *entries;
311 unsigned long *usedmap;
314 __u32 last_hashval = 0;
315 unsigned int usedbytes = 0;
316 unsigned int hdrsize;
320 if (*last_checked == blk->blkno)
323 /* Allocate memory for block usage checking. */
324 error = xchk_setup_xattr_buf(ds->sc, 0, KM_MAYFAIL);
325 if (error == -ENOMEM)
329 usedmap = xchk_xattr_usedmap(ds->sc);
331 *last_checked = blk->blkno;
332 bitmap_zero(usedmap, mp->m_attr_geo->blksize);
334 /* Check all the padding. */
335 if (xfs_sb_version_hascrc(&ds->sc->mp->m_sb)) {
336 struct xfs_attr3_leafblock *leaf = bp->b_addr;
338 if (leaf->hdr.pad1 != 0 || leaf->hdr.pad2 != 0 ||
339 leaf->hdr.info.hdr.pad != 0)
340 xchk_da_set_corrupt(ds, level);
342 if (leaf->hdr.pad1 != 0 || leaf->hdr.info.pad != 0)
343 xchk_da_set_corrupt(ds, level);
346 /* Check the leaf header */
347 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &leafhdr, leaf);
348 hdrsize = xfs_attr3_leaf_hdr_size(leaf);
350 if (leafhdr.usedbytes > mp->m_attr_geo->blksize)
351 xchk_da_set_corrupt(ds, level);
352 if (leafhdr.firstused > mp->m_attr_geo->blksize)
353 xchk_da_set_corrupt(ds, level);
354 if (leafhdr.firstused < hdrsize)
355 xchk_da_set_corrupt(ds, level);
356 if (!xchk_xattr_set_map(ds->sc, usedmap, 0, hdrsize))
357 xchk_da_set_corrupt(ds, level);
359 if (ds->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
362 entries = xfs_attr3_leaf_entryp(leaf);
363 if ((char *)&entries[leafhdr.count] > (char *)leaf + leafhdr.firstused)
364 xchk_da_set_corrupt(ds, level);
366 buf_end = (char *)bp->b_addr + mp->m_attr_geo->blksize;
367 for (i = 0, ent = entries; i < leafhdr.count; ent++, i++) {
368 /* Mark the leaf entry itself. */
369 off = (char *)ent - (char *)leaf;
370 if (!xchk_xattr_set_map(ds->sc, usedmap, off,
371 sizeof(xfs_attr_leaf_entry_t))) {
372 xchk_da_set_corrupt(ds, level);
376 /* Check the entry and nameval. */
377 xchk_xattr_entry(ds, level, buf_end, leaf, &leafhdr,
378 ent, i, &usedbytes, &last_hashval);
380 if (ds->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
384 if (!xchk_xattr_check_freemap(ds->sc, usedmap, &leafhdr))
385 xchk_da_set_corrupt(ds, level);
387 if (leafhdr.usedbytes != usedbytes)
388 xchk_da_set_corrupt(ds, level);
394 /* Scrub a attribute btree record. */
397 struct xchk_da_btree *ds,
400 struct xfs_mount *mp = ds->state->mp;
401 struct xfs_da_state_blk *blk = &ds->state->path.blk[level];
402 struct xfs_attr_leaf_name_local *lentry;
403 struct xfs_attr_leaf_name_remote *rentry;
405 struct xfs_attr_leaf_entry *ent;
406 xfs_dahash_t calc_hash;
410 unsigned int badflags;
413 ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
415 ent = xfs_attr3_leaf_entryp(blk->bp->b_addr) + blk->index;
417 /* Check the whole block, if necessary. */
418 error = xchk_xattr_block(ds, level);
421 if (ds->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
424 /* Check the hash of the entry. */
425 error = xchk_da_btree_hash(ds, level, &ent->hashval);
429 /* Find the attr entry's location. */
431 hdrsize = xfs_attr3_leaf_hdr_size(bp->b_addr);
432 nameidx = be16_to_cpu(ent->nameidx);
433 if (nameidx < hdrsize || nameidx >= mp->m_attr_geo->blksize) {
434 xchk_da_set_corrupt(ds, level);
438 /* Retrieve the entry and check it. */
439 hash = be32_to_cpu(ent->hashval);
440 badflags = ~(XFS_ATTR_LOCAL | XFS_ATTR_ROOT | XFS_ATTR_SECURE |
441 XFS_ATTR_INCOMPLETE);
442 if ((ent->flags & badflags) != 0)
443 xchk_da_set_corrupt(ds, level);
444 if (ent->flags & XFS_ATTR_LOCAL) {
445 lentry = (struct xfs_attr_leaf_name_local *)
446 (((char *)bp->b_addr) + nameidx);
447 if (lentry->namelen <= 0) {
448 xchk_da_set_corrupt(ds, level);
451 calc_hash = xfs_da_hashname(lentry->nameval, lentry->namelen);
453 rentry = (struct xfs_attr_leaf_name_remote *)
454 (((char *)bp->b_addr) + nameidx);
455 if (rentry->namelen <= 0) {
456 xchk_da_set_corrupt(ds, level);
459 calc_hash = xfs_da_hashname(rentry->name, rentry->namelen);
461 if (calc_hash != hash)
462 xchk_da_set_corrupt(ds, level);
468 /* Scrub the extended attribute metadata. */
471 struct xfs_scrub *sc)
473 struct xchk_xattr sx;
474 struct attrlist_cursor_kern cursor = { 0 };
475 xfs_dablk_t last_checked = -1U;
478 if (!xfs_inode_hasattr(sc->ip))
481 memset(&sx, 0, sizeof(sx));
482 /* Check attribute tree structure */
483 error = xchk_da_btree(sc, XFS_ATTR_FORK, xchk_xattr_rec,
488 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
491 /* Check that every attr key can also be looked up by hash. */
492 sx.context.dp = sc->ip;
493 sx.context.cursor = &cursor;
494 sx.context.resynch = 1;
495 sx.context.put_listent = xchk_xattr_listent;
496 sx.context.tp = sc->tp;
497 sx.context.allow_incomplete = true;
501 * Look up every xattr in this file by name.
503 * Use the backend implementation of xfs_attr_list to call
504 * xchk_xattr_listent on every attribute key in this inode.
505 * In other words, we use the same iterator/callback mechanism
506 * that listattr uses to scrub extended attributes, though in our
507 * _listent function, we check the value of the attribute.
509 * The VFS only locks i_rwsem when modifying attrs, so keep all
510 * three locks held because that's the only way to ensure we're
511 * the only thread poking into the da btree. We traverse the da
512 * btree while holding a leaf buffer locked for the xattr name
513 * iteration, which doesn't really follow the usual buffer
516 error = xfs_attr_list_ilocked(&sx.context);
517 if (!xchk_fblock_process_error(sc, XFS_ATTR_FORK, 0, &error))
520 /* Did our listent function try to return any errors? */
521 if (sx.context.seen_enough < 0)
522 error = sx.context.seen_enough;