2 * Copyright (C) 2017 Oracle. All Rights Reserved.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it would be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "xfs_shared.h"
23 #include "xfs_format.h"
24 #include "xfs_trans_resv.h"
25 #include "xfs_mount.h"
26 #include "xfs_defer.h"
27 #include "xfs_btree.h"
29 #include "xfs_log_format.h"
30 #include "xfs_trans.h"
32 #include "xfs_inode.h"
33 #include "xfs_da_format.h"
34 #include "xfs_da_btree.h"
37 #include "xfs_attr_leaf.h"
38 #include "scrub/xfs_scrub.h"
39 #include "scrub/scrub.h"
40 #include "scrub/common.h"
41 #include "scrub/dabtree.h"
42 #include "scrub/trace.h"
44 #include <linux/posix_acl_xattr.h>
45 #include <linux/xattr.h>
47 /* Set us up to scrub an inode's extended attributes. */
49 xfs_scrub_setup_xattr(
50 struct xfs_scrub_context *sc,
56 * Allocate the buffer without the inode lock held. We need enough
57 * space to read every xattr value in the file or enough space to
58 * hold three copies of the xattr free space bitmap. (Not both at
61 sz = max_t(size_t, XATTR_SIZE_MAX, 3 * sizeof(long) *
62 BITS_TO_LONGS(sc->mp->m_attr_geo->blksize));
63 sc->buf = kmem_zalloc_large(sz, KM_SLEEP);
67 return xfs_scrub_setup_inode_contents(sc, ip, 0);
70 /* Extended Attributes */
72 struct xfs_scrub_xattr {
73 struct xfs_attr_list_context context;
74 struct xfs_scrub_context *sc;
78 * Check that an extended attribute key can be looked up by hash.
80 * We use the XFS attribute list iterator (i.e. xfs_attr_list_int_ilocked)
81 * to call this function for every attribute key in an inode. Once
82 * we're here, we load the attribute value to see if any errors happen,
83 * or if we get more or less data than we expected.
86 xfs_scrub_xattr_listent(
87 struct xfs_attr_list_context *context,
93 struct xfs_scrub_xattr *sx;
94 struct xfs_da_args args = { NULL };
97 sx = container_of(context, struct xfs_scrub_xattr, context);
99 if (flags & XFS_ATTR_INCOMPLETE) {
100 /* Incomplete attr key, just mark the inode for preening. */
101 xfs_scrub_ino_set_preen(sx->sc, context->dp->i_ino);
105 args.flags = ATTR_KERNOTIME;
106 if (flags & XFS_ATTR_ROOT)
107 args.flags |= ATTR_ROOT;
108 else if (flags & XFS_ATTR_SECURE)
109 args.flags |= ATTR_SECURE;
110 args.geo = context->dp->i_mount->m_attr_geo;
111 args.whichfork = XFS_ATTR_FORK;
112 args.dp = context->dp;
114 args.namelen = namelen;
115 args.hashval = xfs_da_hashname(args.name, args.namelen);
116 args.trans = context->tp;
117 args.value = sx->sc->buf;
118 args.valuelen = XATTR_SIZE_MAX;
120 error = xfs_attr_get_ilocked(context->dp, &args);
121 if (error == -EEXIST)
123 if (!xfs_scrub_fblock_process_error(sx->sc, XFS_ATTR_FORK, args.blkno,
126 if (args.valuelen != valuelen)
127 xfs_scrub_fblock_set_corrupt(sx->sc, XFS_ATTR_FORK,
130 if (sx->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
131 context->seen_enough = 1;
136 * Mark a range [start, start+len) in this map. Returns true if the
137 * region was free, and false if there's a conflict or a problem.
139 * Within a char, the lowest bit of the char represents the byte with
140 * the smallest address
143 xfs_scrub_xattr_set_map(
144 struct xfs_scrub_context *sc,
149 unsigned int mapsize = sc->mp->m_attr_geo->blksize;
152 if (start >= mapsize)
154 if (start + len > mapsize) {
155 len = mapsize - start;
159 if (find_next_bit(map, mapsize, start) < start + len)
161 bitmap_set(map, start, len);
167 * Check the leaf freemap from the usage bitmap. Returns false if the
168 * attr freemap has problems or points to used space.
171 xfs_scrub_xattr_check_freemap(
172 struct xfs_scrub_context *sc,
174 struct xfs_attr3_icleaf_hdr *leafhdr)
176 unsigned long *freemap;
177 unsigned long *dstmap;
178 unsigned int mapsize = sc->mp->m_attr_geo->blksize;
181 /* Construct bitmap of freemap contents. */
182 freemap = (unsigned long *)sc->buf + BITS_TO_LONGS(mapsize);
183 bitmap_zero(freemap, mapsize);
184 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
185 if (!xfs_scrub_xattr_set_map(sc, freemap,
186 leafhdr->freemap[i].base,
187 leafhdr->freemap[i].size))
191 /* Look for bits that are set in freemap and are marked in use. */
192 dstmap = freemap + BITS_TO_LONGS(mapsize);
193 return bitmap_and(dstmap, freemap, map, mapsize) == 0;
197 * Check this leaf entry's relations to everything else.
198 * Returns the number of bytes used for the name/value data.
201 xfs_scrub_xattr_entry(
202 struct xfs_scrub_da_btree *ds,
205 struct xfs_attr_leafblock *leaf,
206 struct xfs_attr3_icleaf_hdr *leafhdr,
207 unsigned long *usedmap,
208 struct xfs_attr_leaf_entry *ent,
210 unsigned int *usedbytes,
213 struct xfs_mount *mp = ds->state->mp;
215 struct xfs_attr_leaf_name_local *lentry;
216 struct xfs_attr_leaf_name_remote *rentry;
217 unsigned int nameidx;
218 unsigned int namesize;
221 xfs_scrub_da_set_corrupt(ds, level);
223 /* Hash values in order? */
224 if (be32_to_cpu(ent->hashval) < *last_hashval)
225 xfs_scrub_da_set_corrupt(ds, level);
226 *last_hashval = be32_to_cpu(ent->hashval);
228 nameidx = be16_to_cpu(ent->nameidx);
229 if (nameidx < leafhdr->firstused ||
230 nameidx >= mp->m_attr_geo->blksize) {
231 xfs_scrub_da_set_corrupt(ds, level);
235 /* Check the name information. */
236 if (ent->flags & XFS_ATTR_LOCAL) {
237 lentry = xfs_attr3_leaf_name_local(leaf, idx);
238 namesize = xfs_attr_leaf_entsize_local(lentry->namelen,
239 be16_to_cpu(lentry->valuelen));
240 name_end = (char *)lentry + namesize;
241 if (lentry->namelen == 0)
242 xfs_scrub_da_set_corrupt(ds, level);
244 rentry = xfs_attr3_leaf_name_remote(leaf, idx);
245 namesize = xfs_attr_leaf_entsize_remote(rentry->namelen);
246 name_end = (char *)rentry + namesize;
247 if (rentry->namelen == 0 || rentry->valueblk == 0)
248 xfs_scrub_da_set_corrupt(ds, level);
250 if (name_end > buf_end)
251 xfs_scrub_da_set_corrupt(ds, level);
253 if (!xfs_scrub_xattr_set_map(ds->sc, usedmap, nameidx, namesize))
254 xfs_scrub_da_set_corrupt(ds, level);
255 if (!(ds->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT))
256 *usedbytes += namesize;
259 /* Scrub an attribute leaf. */
261 xfs_scrub_xattr_block(
262 struct xfs_scrub_da_btree *ds,
265 struct xfs_attr3_icleaf_hdr leafhdr;
266 struct xfs_mount *mp = ds->state->mp;
267 struct xfs_da_state_blk *blk = &ds->state->path.blk[level];
268 struct xfs_buf *bp = blk->bp;
269 xfs_dablk_t *last_checked = ds->private;
270 struct xfs_attr_leafblock *leaf = bp->b_addr;
271 struct xfs_attr_leaf_entry *ent;
272 struct xfs_attr_leaf_entry *entries;
273 unsigned long *usedmap = ds->sc->buf;
276 __u32 last_hashval = 0;
277 unsigned int usedbytes = 0;
278 unsigned int hdrsize;
281 if (*last_checked == blk->blkno)
283 *last_checked = blk->blkno;
284 bitmap_zero(usedmap, mp->m_attr_geo->blksize);
286 /* Check all the padding. */
287 if (xfs_sb_version_hascrc(&ds->sc->mp->m_sb)) {
288 struct xfs_attr3_leafblock *leaf = bp->b_addr;
290 if (leaf->hdr.pad1 != 0 || leaf->hdr.pad2 != 0 ||
291 leaf->hdr.info.hdr.pad != 0)
292 xfs_scrub_da_set_corrupt(ds, level);
294 if (leaf->hdr.pad1 != 0 || leaf->hdr.info.pad != 0)
295 xfs_scrub_da_set_corrupt(ds, level);
298 /* Check the leaf header */
299 xfs_attr3_leaf_hdr_from_disk(mp->m_attr_geo, &leafhdr, leaf);
300 hdrsize = xfs_attr3_leaf_hdr_size(leaf);
302 if (leafhdr.usedbytes > mp->m_attr_geo->blksize)
303 xfs_scrub_da_set_corrupt(ds, level);
304 if (leafhdr.firstused > mp->m_attr_geo->blksize)
305 xfs_scrub_da_set_corrupt(ds, level);
306 if (leafhdr.firstused < hdrsize)
307 xfs_scrub_da_set_corrupt(ds, level);
308 if (!xfs_scrub_xattr_set_map(ds->sc, usedmap, 0, hdrsize))
309 xfs_scrub_da_set_corrupt(ds, level);
311 if (ds->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
314 entries = xfs_attr3_leaf_entryp(leaf);
315 if ((char *)&entries[leafhdr.count] > (char *)leaf + leafhdr.firstused)
316 xfs_scrub_da_set_corrupt(ds, level);
318 buf_end = (char *)bp->b_addr + mp->m_attr_geo->blksize;
319 for (i = 0, ent = entries; i < leafhdr.count; ent++, i++) {
320 /* Mark the leaf entry itself. */
321 off = (char *)ent - (char *)leaf;
322 if (!xfs_scrub_xattr_set_map(ds->sc, usedmap, off,
323 sizeof(xfs_attr_leaf_entry_t))) {
324 xfs_scrub_da_set_corrupt(ds, level);
328 /* Check the entry and nameval. */
329 xfs_scrub_xattr_entry(ds, level, buf_end, leaf, &leafhdr,
330 usedmap, ent, i, &usedbytes, &last_hashval);
332 if (ds->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
336 if (!xfs_scrub_xattr_check_freemap(ds->sc, usedmap, &leafhdr))
337 xfs_scrub_da_set_corrupt(ds, level);
339 if (leafhdr.usedbytes != usedbytes)
340 xfs_scrub_da_set_corrupt(ds, level);
346 /* Scrub a attribute btree record. */
349 struct xfs_scrub_da_btree *ds,
353 struct xfs_mount *mp = ds->state->mp;
354 struct xfs_attr_leaf_entry *ent = rec;
355 struct xfs_da_state_blk *blk;
356 struct xfs_attr_leaf_name_local *lentry;
357 struct xfs_attr_leaf_name_remote *rentry;
359 xfs_dahash_t calc_hash;
363 unsigned int badflags;
366 blk = &ds->state->path.blk[level];
368 /* Check the whole block, if necessary. */
369 error = xfs_scrub_xattr_block(ds, level);
372 if (ds->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
375 /* Check the hash of the entry. */
376 error = xfs_scrub_da_btree_hash(ds, level, &ent->hashval);
380 /* Find the attr entry's location. */
382 hdrsize = xfs_attr3_leaf_hdr_size(bp->b_addr);
383 nameidx = be16_to_cpu(ent->nameidx);
384 if (nameidx < hdrsize || nameidx >= mp->m_attr_geo->blksize) {
385 xfs_scrub_da_set_corrupt(ds, level);
389 /* Retrieve the entry and check it. */
390 hash = be32_to_cpu(ent->hashval);
391 badflags = ~(XFS_ATTR_LOCAL | XFS_ATTR_ROOT | XFS_ATTR_SECURE |
392 XFS_ATTR_INCOMPLETE);
393 if ((ent->flags & badflags) != 0)
394 xfs_scrub_da_set_corrupt(ds, level);
395 if (ent->flags & XFS_ATTR_LOCAL) {
396 lentry = (struct xfs_attr_leaf_name_local *)
397 (((char *)bp->b_addr) + nameidx);
398 if (lentry->namelen <= 0) {
399 xfs_scrub_da_set_corrupt(ds, level);
402 calc_hash = xfs_da_hashname(lentry->nameval, lentry->namelen);
404 rentry = (struct xfs_attr_leaf_name_remote *)
405 (((char *)bp->b_addr) + nameidx);
406 if (rentry->namelen <= 0) {
407 xfs_scrub_da_set_corrupt(ds, level);
410 calc_hash = xfs_da_hashname(rentry->name, rentry->namelen);
412 if (calc_hash != hash)
413 xfs_scrub_da_set_corrupt(ds, level);
419 /* Scrub the extended attribute metadata. */
422 struct xfs_scrub_context *sc)
424 struct xfs_scrub_xattr sx;
425 struct attrlist_cursor_kern cursor = { 0 };
426 xfs_dablk_t last_checked = -1U;
429 if (!xfs_inode_hasattr(sc->ip))
432 memset(&sx, 0, sizeof(sx));
433 /* Check attribute tree structure */
434 error = xfs_scrub_da_btree(sc, XFS_ATTR_FORK, xfs_scrub_xattr_rec,
439 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
442 /* Check that every attr key can also be looked up by hash. */
443 sx.context.dp = sc->ip;
444 sx.context.cursor = &cursor;
445 sx.context.resynch = 1;
446 sx.context.put_listent = xfs_scrub_xattr_listent;
447 sx.context.tp = sc->tp;
448 sx.context.flags = ATTR_INCOMPLETE;
452 * Look up every xattr in this file by name.
454 * Use the backend implementation of xfs_attr_list to call
455 * xfs_scrub_xattr_listent on every attribute key in this inode.
456 * In other words, we use the same iterator/callback mechanism
457 * that listattr uses to scrub extended attributes, though in our
458 * _listent function, we check the value of the attribute.
460 * The VFS only locks i_rwsem when modifying attrs, so keep all
461 * three locks held because that's the only way to ensure we're
462 * the only thread poking into the da btree. We traverse the da
463 * btree while holding a leaf buffer locked for the xattr name
464 * iteration, which doesn't really follow the usual buffer
467 error = xfs_attr_list_int_ilocked(&sx.context);
468 if (!xfs_scrub_fblock_process_error(sc, XFS_ATTR_FORK, 0, &error))