hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/fs/xfs/scrub/attr.c
....@@ -9,26 +9,62 @@
99 #include "xfs_format.h"
1010 #include "xfs_trans_resv.h"
1111 #include "xfs_mount.h"
12
-#include "xfs_defer.h"
13
-#include "xfs_btree.h"
14
-#include "xfs_bit.h"
1512 #include "xfs_log_format.h"
16
-#include "xfs_trans.h"
17
-#include "xfs_sb.h"
1813 #include "xfs_inode.h"
1914 #include "xfs_da_format.h"
2015 #include "xfs_da_btree.h"
21
-#include "xfs_dir2.h"
2216 #include "xfs_attr.h"
2317 #include "xfs_attr_leaf.h"
24
-#include "scrub/xfs_scrub.h"
2518 #include "scrub/scrub.h"
2619 #include "scrub/common.h"
2720 #include "scrub/dabtree.h"
28
-#include "scrub/trace.h"
21
+#include "scrub/attr.h"
2922
30
-#include <linux/posix_acl_xattr.h>
31
-#include <linux/xattr.h>
23
+/*
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.
27
+ */
28
+int
29
+xchk_setup_xattr_buf(
30
+ struct xfs_scrub *sc,
31
+ size_t value_size,
32
+ xfs_km_flags_t flags)
33
+{
34
+ size_t sz;
35
+ struct xchk_xattr_buf *ab = sc->buf;
36
+
37
+ /*
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.
41
+ */
42
+ sz = 3 * sizeof(long) * BITS_TO_LONGS(sc->mp->m_attr_geo->blksize);
43
+ sz = max_t(size_t, sz, value_size);
44
+
45
+ /*
46
+ * If there's already a buffer, figure out if we need to reallocate it
47
+ * to accommodate a larger size.
48
+ */
49
+ if (ab) {
50
+ if (sz <= ab->sz)
51
+ return 0;
52
+ kmem_free(ab);
53
+ sc->buf = NULL;
54
+ }
55
+
56
+ /*
57
+ * Don't zero the buffer upon allocation to avoid runtime overhead.
58
+ * All users must be careful never to read uninitialized contents.
59
+ */
60
+ ab = kmem_alloc_large(sizeof(*ab) + sz, flags);
61
+ if (!ab)
62
+ return -ENOMEM;
63
+
64
+ ab->sz = sz;
65
+ sc->buf = ab;
66
+ return 0;
67
+}
3268
3369 /* Set us up to scrub an inode's extended attributes. */
3470 int
....@@ -36,19 +72,18 @@
3672 struct xfs_scrub *sc,
3773 struct xfs_inode *ip)
3874 {
39
- size_t sz;
75
+ int error;
4076
4177 /*
42
- * Allocate the buffer without the inode lock held. We need enough
43
- * space to read every xattr value in the file or enough space to
44
- * hold three copies of the xattr free space bitmap. (Not both at
45
- * the same time.)
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.
4681 */
47
- sz = max_t(size_t, XATTR_SIZE_MAX, 3 * sizeof(long) *
48
- BITS_TO_LONGS(sc->mp->m_attr_geo->blksize));
49
- sc->buf = kmem_zalloc_large(sz, KM_SLEEP);
50
- if (!sc->buf)
51
- return -ENOMEM;
82
+ if (sc->flags & XCHK_TRY_HARDER) {
83
+ error = xchk_setup_xattr_buf(sc, XATTR_SIZE_MAX, 0);
84
+ if (error)
85
+ return error;
86
+ }
5287
5388 return xchk_setup_inode_contents(sc, ip, 0);
5489 }
....@@ -63,7 +98,7 @@
6398 /*
6499 * Check that an extended attribute key can be looked up by hash.
65100 *
66
- * We use the XFS attribute list iterator (i.e. xfs_attr_list_int_ilocked)
101
+ * We use the XFS attribute list iterator (i.e. xfs_attr_list_ilocked)
67102 * to call this function for every attribute key in an inode. Once
68103 * we're here, we load the attribute value to see if any errors happen,
69104 * or if we get more or less data than we expected.
....@@ -82,17 +117,38 @@
82117
83118 sx = container_of(context, struct xchk_xattr, context);
84119
120
+ if (xchk_should_terminate(sx->sc, &error)) {
121
+ context->seen_enough = error;
122
+ return;
123
+ }
124
+
85125 if (flags & XFS_ATTR_INCOMPLETE) {
86126 /* Incomplete attr key, just mark the inode for preening. */
87127 xchk_ino_set_preen(sx->sc, context->dp->i_ino);
88128 return;
89129 }
90130
91
- args.flags = ATTR_KERNOTIME;
92
- if (flags & XFS_ATTR_ROOT)
93
- args.flags |= ATTR_ROOT;
94
- else if (flags & XFS_ATTR_SECURE)
95
- args.flags |= ATTR_SECURE;
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);
134
+ return;
135
+ }
136
+
137
+ /*
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.
141
+ */
142
+ error = xchk_setup_xattr_buf(sx->sc, valuelen, KM_MAYFAIL);
143
+ if (error == -ENOMEM)
144
+ error = -EDEADLOCK;
145
+ if (error) {
146
+ context->seen_enough = error;
147
+ return;
148
+ }
149
+
150
+ args.op_flags = XFS_DA_OP_NOTIME;
151
+ args.attr_filter = flags & XFS_ATTR_NSP_ONDISK_MASK;
96152 args.geo = context->dp->i_mount->m_attr_geo;
97153 args.whichfork = XFS_ATTR_FORK;
98154 args.dp = context->dp;
....@@ -100,12 +156,13 @@
100156 args.namelen = namelen;
101157 args.hashval = xfs_da_hashname(args.name, args.namelen);
102158 args.trans = context->tp;
103
- args.value = sx->sc->buf;
104
- args.valuelen = XATTR_SIZE_MAX;
159
+ args.value = xchk_xattr_valuebuf(sx->sc);
160
+ args.valuelen = valuelen;
105161
106
- error = xfs_attr_get_ilocked(context->dp, &args);
107
- if (error == -EEXIST)
108
- error = 0;
162
+ error = xfs_attr_get_ilocked(&args);
163
+ /* ENODATA means the hash lookup failed and the attr is bad */
164
+ if (error == -ENODATA)
165
+ error = -EFSCORRUPTED;
109166 if (!xchk_fblock_process_error(sx->sc, XFS_ATTR_FORK, args.blkno,
110167 &error))
111168 goto fail_xref;
....@@ -159,13 +216,12 @@
159216 unsigned long *map,
160217 struct xfs_attr3_icleaf_hdr *leafhdr)
161218 {
162
- unsigned long *freemap;
163
- unsigned long *dstmap;
219
+ unsigned long *freemap = xchk_xattr_freemap(sc);
220
+ unsigned long *dstmap = xchk_xattr_dstmap(sc);
164221 unsigned int mapsize = sc->mp->m_attr_geo->blksize;
165222 int i;
166223
167224 /* Construct bitmap of freemap contents. */
168
- freemap = (unsigned long *)sc->buf + BITS_TO_LONGS(mapsize);
169225 bitmap_zero(freemap, mapsize);
170226 for (i = 0; i < XFS_ATTR_LEAF_MAPSIZE; i++) {
171227 if (!xchk_xattr_set_map(sc, freemap,
....@@ -175,7 +231,6 @@
175231 }
176232
177233 /* Look for bits that are set in freemap and are marked in use. */
178
- dstmap = freemap + BITS_TO_LONGS(mapsize);
179234 return bitmap_and(dstmap, freemap, map, mapsize) == 0;
180235 }
181236
....@@ -190,13 +245,13 @@
190245 char *buf_end,
191246 struct xfs_attr_leafblock *leaf,
192247 struct xfs_attr3_icleaf_hdr *leafhdr,
193
- unsigned long *usedmap,
194248 struct xfs_attr_leaf_entry *ent,
195249 int idx,
196250 unsigned int *usedbytes,
197251 __u32 *last_hashval)
198252 {
199253 struct xfs_mount *mp = ds->state->mp;
254
+ unsigned long *usedmap = xchk_xattr_usedmap(ds->sc);
200255 char *name_end;
201256 struct xfs_attr_leaf_name_local *lentry;
202257 struct xfs_attr_leaf_name_remote *rentry;
....@@ -256,16 +311,26 @@
256311 struct xfs_attr_leafblock *leaf = bp->b_addr;
257312 struct xfs_attr_leaf_entry *ent;
258313 struct xfs_attr_leaf_entry *entries;
259
- unsigned long *usedmap = ds->sc->buf;
314
+ unsigned long *usedmap;
260315 char *buf_end;
261316 size_t off;
262317 __u32 last_hashval = 0;
263318 unsigned int usedbytes = 0;
264319 unsigned int hdrsize;
265320 int i;
321
+ int error;
266322
267323 if (*last_checked == blk->blkno)
268324 return 0;
325
+
326
+ /* Allocate memory for block usage checking. */
327
+ error = xchk_setup_xattr_buf(ds->sc, 0, KM_MAYFAIL);
328
+ if (error == -ENOMEM)
329
+ return -EDEADLOCK;
330
+ if (error)
331
+ return error;
332
+ usedmap = xchk_xattr_usedmap(ds->sc);
333
+
269334 *last_checked = blk->blkno;
270335 bitmap_zero(usedmap, mp->m_attr_geo->blksize);
271336
....@@ -313,7 +378,7 @@
313378
314379 /* Check the entry and nameval. */
315380 xchk_xattr_entry(ds, level, buf_end, leaf, &leafhdr,
316
- usedmap, ent, i, &usedbytes, &last_hashval);
381
+ ent, i, &usedbytes, &last_hashval);
317382
318383 if (ds->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
319384 goto out;
....@@ -333,15 +398,14 @@
333398 STATIC int
334399 xchk_xattr_rec(
335400 struct xchk_da_btree *ds,
336
- int level,
337
- void *rec)
401
+ int level)
338402 {
339403 struct xfs_mount *mp = ds->state->mp;
340
- struct xfs_attr_leaf_entry *ent = rec;
341
- struct xfs_da_state_blk *blk;
404
+ struct xfs_da_state_blk *blk = &ds->state->path.blk[level];
342405 struct xfs_attr_leaf_name_local *lentry;
343406 struct xfs_attr_leaf_name_remote *rentry;
344407 struct xfs_buf *bp;
408
+ struct xfs_attr_leaf_entry *ent;
345409 xfs_dahash_t calc_hash;
346410 xfs_dahash_t hash;
347411 int nameidx;
....@@ -349,7 +413,9 @@
349413 unsigned int badflags;
350414 int error;
351415
352
- blk = &ds->state->path.blk[level];
416
+ ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
417
+
418
+ ent = xfs_attr3_leaf_entryp(blk->bp->b_addr) + blk->index;
353419
354420 /* Check the whole block, if necessary. */
355421 error = xchk_xattr_block(ds, level);
....@@ -408,7 +474,6 @@
408474 struct xfs_scrub *sc)
409475 {
410476 struct xchk_xattr sx;
411
- struct attrlist_cursor_kern cursor = { 0 };
412477 xfs_dablk_t last_checked = -1U;
413478 int error = 0;
414479
....@@ -427,11 +492,10 @@
427492
428493 /* Check that every attr key can also be looked up by hash. */
429494 sx.context.dp = sc->ip;
430
- sx.context.cursor = &cursor;
431495 sx.context.resynch = 1;
432496 sx.context.put_listent = xchk_xattr_listent;
433497 sx.context.tp = sc->tp;
434
- sx.context.flags = ATTR_INCOMPLETE;
498
+ sx.context.allow_incomplete = true;
435499 sx.sc = sc;
436500
437501 /*
....@@ -450,9 +514,13 @@
450514 * iteration, which doesn't really follow the usual buffer
451515 * locking order.
452516 */
453
- error = xfs_attr_list_int_ilocked(&sx.context);
517
+ error = xfs_attr_list_ilocked(&sx.context);
454518 if (!xchk_fblock_process_error(sc, XFS_ATTR_FORK, 0, &error))
455519 goto out;
520
+
521
+ /* Did our listent function try to return any errors? */
522
+ if (sx.context.seen_enough < 0)
523
+ error = sx.context.seen_enough;
456524 out:
457525 return error;
458526 }