blob: a70cd9b27c7f3ebee664f56bc58befc2075753fb [file] [log] [blame]
Darrick J. Wongeec04822017-10-17 21:37:45 -07001/*
2 * Copyright (C) 2017 Oracle. All Rights Reserved.
3 *
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
5 *
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.
10 *
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.
15 *
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.
19 */
20#include "xfs.h"
21#include "xfs_fs.h"
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"
28#include "xfs_bit.h"
29#include "xfs_log_format.h"
30#include "xfs_trans.h"
31#include "xfs_sb.h"
32#include "xfs_inode.h"
33#include "xfs_da_format.h"
34#include "xfs_da_btree.h"
35#include "xfs_dir2.h"
36#include "xfs_attr.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"
43
44#include <linux/posix_acl_xattr.h>
45#include <linux/xattr.h>
46
47/* Set us up to scrub an inode's extended attributes. */
48int
49xfs_scrub_setup_xattr(
50 struct xfs_scrub_context *sc,
51 struct xfs_inode *ip)
52{
53 /* Allocate the buffer without the inode lock held. */
54 sc->buf = kmem_zalloc_large(XATTR_SIZE_MAX, KM_SLEEP);
55 if (!sc->buf)
56 return -ENOMEM;
57
58 return xfs_scrub_setup_inode_contents(sc, ip, 0);
59}
60
61/* Extended Attributes */
62
63struct xfs_scrub_xattr {
64 struct xfs_attr_list_context context;
65 struct xfs_scrub_context *sc;
66};
67
68/*
69 * Check that an extended attribute key can be looked up by hash.
70 *
71 * We use the XFS attribute list iterator (i.e. xfs_attr_list_int_ilocked)
72 * to call this function for every attribute key in an inode. Once
73 * we're here, we load the attribute value to see if any errors happen,
74 * or if we get more or less data than we expected.
75 */
76static void
77xfs_scrub_xattr_listent(
78 struct xfs_attr_list_context *context,
79 int flags,
80 unsigned char *name,
81 int namelen,
82 int valuelen)
83{
84 struct xfs_scrub_xattr *sx;
85 struct xfs_da_args args = {0};
86 int error = 0;
87
88 sx = container_of(context, struct xfs_scrub_xattr, context);
89
90 if (flags & XFS_ATTR_INCOMPLETE) {
91 /* Incomplete attr key, just mark the inode for preening. */
92 xfs_scrub_ino_set_preen(sx->sc, NULL);
93 return;
94 }
95
96 args.flags = ATTR_KERNOTIME;
97 if (flags & XFS_ATTR_ROOT)
98 args.flags |= ATTR_ROOT;
99 else if (flags & XFS_ATTR_SECURE)
100 args.flags |= ATTR_SECURE;
101 args.geo = context->dp->i_mount->m_attr_geo;
102 args.whichfork = XFS_ATTR_FORK;
103 args.dp = context->dp;
104 args.name = name;
105 args.namelen = namelen;
106 args.hashval = xfs_da_hashname(args.name, args.namelen);
107 args.trans = context->tp;
108 args.value = sx->sc->buf;
109 args.valuelen = XATTR_SIZE_MAX;
110
111 error = xfs_attr_get_ilocked(context->dp, &args);
112 if (error == -EEXIST)
113 error = 0;
114 if (!xfs_scrub_fblock_process_error(sx->sc, XFS_ATTR_FORK, args.blkno,
115 &error))
116 goto fail_xref;
117 if (args.valuelen != valuelen)
118 xfs_scrub_fblock_set_corrupt(sx->sc, XFS_ATTR_FORK,
119 args.blkno);
120
121fail_xref:
122 return;
123}
124
125/* Scrub a attribute btree record. */
126STATIC int
127xfs_scrub_xattr_rec(
128 struct xfs_scrub_da_btree *ds,
129 int level,
130 void *rec)
131{
132 struct xfs_mount *mp = ds->state->mp;
133 struct xfs_attr_leaf_entry *ent = rec;
134 struct xfs_da_state_blk *blk;
135 struct xfs_attr_leaf_name_local *lentry;
136 struct xfs_attr_leaf_name_remote *rentry;
137 struct xfs_buf *bp;
138 xfs_dahash_t calc_hash;
139 xfs_dahash_t hash;
140 int nameidx;
141 int hdrsize;
142 unsigned int badflags;
143 int error;
144
145 blk = &ds->state->path.blk[level];
146
147 /* Check the hash of the entry. */
148 error = xfs_scrub_da_btree_hash(ds, level, &ent->hashval);
149 if (error)
150 goto out;
151
152 /* Find the attr entry's location. */
153 bp = blk->bp;
154 hdrsize = xfs_attr3_leaf_hdr_size(bp->b_addr);
155 nameidx = be16_to_cpu(ent->nameidx);
156 if (nameidx < hdrsize || nameidx >= mp->m_attr_geo->blksize) {
157 xfs_scrub_da_set_corrupt(ds, level);
158 goto out;
159 }
160
161 /* Check all the padding. */
162 if (xfs_sb_version_hascrc(&ds->sc->mp->m_sb)) {
163 struct xfs_attr3_leafblock *leaf = bp->b_addr;
164
165 if (leaf->hdr.pad1 != 0 ||
166 leaf->hdr.pad2 != cpu_to_be32(0) ||
167 leaf->hdr.info.hdr.pad != cpu_to_be16(0))
168 xfs_scrub_da_set_corrupt(ds, level);
169 } else {
170 struct xfs_attr_leafblock *leaf = bp->b_addr;
171
172 if (leaf->hdr.pad1 != 0 ||
173 leaf->hdr.info.pad != cpu_to_be16(0))
174 xfs_scrub_da_set_corrupt(ds, level);
175 }
176 if (ent->pad2 != 0)
177 xfs_scrub_da_set_corrupt(ds, level);
178
179 /* Retrieve the entry and check it. */
180 hash = be32_to_cpu(ent->hashval);
181 badflags = ~(XFS_ATTR_LOCAL | XFS_ATTR_ROOT | XFS_ATTR_SECURE |
182 XFS_ATTR_INCOMPLETE);
183 if ((ent->flags & badflags) != 0)
184 xfs_scrub_da_set_corrupt(ds, level);
185 if (ent->flags & XFS_ATTR_LOCAL) {
186 lentry = (struct xfs_attr_leaf_name_local *)
187 (((char *)bp->b_addr) + nameidx);
188 if (lentry->namelen <= 0) {
189 xfs_scrub_da_set_corrupt(ds, level);
190 goto out;
191 }
192 calc_hash = xfs_da_hashname(lentry->nameval, lentry->namelen);
193 } else {
194 rentry = (struct xfs_attr_leaf_name_remote *)
195 (((char *)bp->b_addr) + nameidx);
196 if (rentry->namelen <= 0) {
197 xfs_scrub_da_set_corrupt(ds, level);
198 goto out;
199 }
200 calc_hash = xfs_da_hashname(rentry->name, rentry->namelen);
201 }
202 if (calc_hash != hash)
203 xfs_scrub_da_set_corrupt(ds, level);
204
205out:
206 return error;
207}
208
209/* Scrub the extended attribute metadata. */
210int
211xfs_scrub_xattr(
212 struct xfs_scrub_context *sc)
213{
214 struct xfs_scrub_xattr sx = { 0 };
215 struct attrlist_cursor_kern cursor = { 0 };
216 int error = 0;
217
218 if (!xfs_inode_hasattr(sc->ip))
219 return -ENOENT;
220
221 memset(&sx, 0, sizeof(sx));
222 /* Check attribute tree structure */
223 error = xfs_scrub_da_btree(sc, XFS_ATTR_FORK, xfs_scrub_xattr_rec);
224 if (error)
225 goto out;
226
227 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
228 goto out;
229
230 /* Check that every attr key can also be looked up by hash. */
231 sx.context.dp = sc->ip;
232 sx.context.cursor = &cursor;
233 sx.context.resynch = 1;
234 sx.context.put_listent = xfs_scrub_xattr_listent;
235 sx.context.tp = sc->tp;
236 sx.context.flags = ATTR_INCOMPLETE;
237 sx.sc = sc;
238
239 /*
240 * Look up every xattr in this file by name.
241 *
242 * Use the backend implementation of xfs_attr_list to call
243 * xfs_scrub_xattr_listent on every attribute key in this inode.
244 * In other words, we use the same iterator/callback mechanism
245 * that listattr uses to scrub extended attributes, though in our
246 * _listent function, we check the value of the attribute.
247 *
248 * The VFS only locks i_rwsem when modifying attrs, so keep all
249 * three locks held because that's the only way to ensure we're
250 * the only thread poking into the da btree. We traverse the da
251 * btree while holding a leaf buffer locked for the xattr name
252 * iteration, which doesn't really follow the usual buffer
253 * locking order.
254 */
255 error = xfs_attr_list_int_ilocked(&sx.context);
256 if (!xfs_scrub_fblock_process_error(sc, XFS_ATTR_FORK, 0, &error))
257 goto out;
258out:
259 return error;
260}