]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/xfs_dquot_buf.c
xfs: create a new buf_ops pointer to verify structure metadata
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_dquot_buf.c
1 /*
2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
3 * Copyright (c) 2013 Red Hat, Inc.
4 * All Rights Reserved.
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 as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it would be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 #include "libxfs_priv.h"
20 #include "xfs_fs.h"
21 #include "xfs_shared.h"
22 #include "xfs_format.h"
23 #include "xfs_log_format.h"
24 #include "xfs_trans_resv.h"
25 #include "xfs_mount.h"
26 #include "xfs_inode.h"
27 #include "xfs_trans.h"
28 #include "xfs_cksum.h"
29 #include "xfs_trace.h"
30 #include "xfs_quota_defs.h"
31
32 int
33 xfs_calc_dquots_per_chunk(
34 unsigned int nbblks) /* basic block units */
35 {
36 ASSERT(nbblks > 0);
37 return BBTOB(nbblks) / sizeof(xfs_dqblk_t);
38 }
39
40 /*
41 * Do some primitive error checking on ondisk dquot data structures.
42 */
43 int
44 xfs_dqcheck(
45 struct xfs_mount *mp,
46 xfs_disk_dquot_t *ddq,
47 xfs_dqid_t id,
48 uint type, /* used only when IO_dorepair is true */
49 uint flags,
50 const char *str)
51 {
52 xfs_dqblk_t *d = (xfs_dqblk_t *)ddq;
53 int errs = 0;
54
55 /*
56 * We can encounter an uninitialized dquot buffer for 2 reasons:
57 * 1. If we crash while deleting the quotainode(s), and those blks got
58 * used for user data. This is because we take the path of regular
59 * file deletion; however, the size field of quotainodes is never
60 * updated, so all the tricks that we play in itruncate_finish
61 * don't quite matter.
62 *
63 * 2. We don't play the quota buffers when there's a quotaoff logitem.
64 * But the allocation will be replayed so we'll end up with an
65 * uninitialized quota block.
66 *
67 * This is all fine; things are still consistent, and we haven't lost
68 * any quota information. Just don't complain about bad dquot blks.
69 */
70 if (ddq->d_magic != cpu_to_be16(XFS_DQUOT_MAGIC)) {
71 if (flags & XFS_QMOPT_DOWARN)
72 xfs_alert(mp,
73 "%s : XFS dquot ID 0x%x, magic 0x%x != 0x%x",
74 str, id, be16_to_cpu(ddq->d_magic), XFS_DQUOT_MAGIC);
75 errs++;
76 }
77 if (ddq->d_version != XFS_DQUOT_VERSION) {
78 if (flags & XFS_QMOPT_DOWARN)
79 xfs_alert(mp,
80 "%s : XFS dquot ID 0x%x, version 0x%x != 0x%x",
81 str, id, ddq->d_version, XFS_DQUOT_VERSION);
82 errs++;
83 }
84
85 if (ddq->d_flags != XFS_DQ_USER &&
86 ddq->d_flags != XFS_DQ_PROJ &&
87 ddq->d_flags != XFS_DQ_GROUP) {
88 if (flags & XFS_QMOPT_DOWARN)
89 xfs_alert(mp,
90 "%s : XFS dquot ID 0x%x, unknown flags 0x%x",
91 str, id, ddq->d_flags);
92 errs++;
93 }
94
95 if (id != -1 && id != be32_to_cpu(ddq->d_id)) {
96 if (flags & XFS_QMOPT_DOWARN)
97 xfs_alert(mp,
98 "%s : ondisk-dquot 0x%p, ID mismatch: "
99 "0x%x expected, found id 0x%x",
100 str, ddq, id, be32_to_cpu(ddq->d_id));
101 errs++;
102 }
103
104 if (!errs && ddq->d_id) {
105 if (ddq->d_blk_softlimit &&
106 be64_to_cpu(ddq->d_bcount) >
107 be64_to_cpu(ddq->d_blk_softlimit)) {
108 if (!ddq->d_btimer) {
109 if (flags & XFS_QMOPT_DOWARN)
110 xfs_alert(mp,
111 "%s : Dquot ID 0x%x (0x%p) BLK TIMER NOT STARTED",
112 str, (int)be32_to_cpu(ddq->d_id), ddq);
113 errs++;
114 }
115 }
116 if (ddq->d_ino_softlimit &&
117 be64_to_cpu(ddq->d_icount) >
118 be64_to_cpu(ddq->d_ino_softlimit)) {
119 if (!ddq->d_itimer) {
120 if (flags & XFS_QMOPT_DOWARN)
121 xfs_alert(mp,
122 "%s : Dquot ID 0x%x (0x%p) INODE TIMER NOT STARTED",
123 str, (int)be32_to_cpu(ddq->d_id), ddq);
124 errs++;
125 }
126 }
127 if (ddq->d_rtb_softlimit &&
128 be64_to_cpu(ddq->d_rtbcount) >
129 be64_to_cpu(ddq->d_rtb_softlimit)) {
130 if (!ddq->d_rtbtimer) {
131 if (flags & XFS_QMOPT_DOWARN)
132 xfs_alert(mp,
133 "%s : Dquot ID 0x%x (0x%p) RTBLK TIMER NOT STARTED",
134 str, (int)be32_to_cpu(ddq->d_id), ddq);
135 errs++;
136 }
137 }
138 }
139
140 if (!errs || !(flags & XFS_QMOPT_DQREPAIR))
141 return errs;
142
143 if (flags & XFS_QMOPT_DOWARN)
144 xfs_notice(mp, "Re-initializing dquot ID 0x%x", id);
145
146 /*
147 * Typically, a repair is only requested by quotacheck.
148 */
149 ASSERT(id != -1);
150 ASSERT(flags & XFS_QMOPT_DQREPAIR);
151 memset(d, 0, sizeof(xfs_dqblk_t));
152
153 d->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
154 d->dd_diskdq.d_version = XFS_DQUOT_VERSION;
155 d->dd_diskdq.d_flags = type;
156 d->dd_diskdq.d_id = cpu_to_be32(id);
157
158 if (xfs_sb_version_hascrc(&mp->m_sb)) {
159 uuid_copy(&d->dd_uuid, &mp->m_sb.sb_meta_uuid);
160 xfs_update_cksum((char *)d, sizeof(struct xfs_dqblk),
161 XFS_DQUOT_CRC_OFF);
162 }
163
164 return errs;
165 }
166
167 STATIC bool
168 xfs_dquot_buf_verify_crc(
169 struct xfs_mount *mp,
170 struct xfs_buf *bp)
171 {
172 struct xfs_dqblk *d = (struct xfs_dqblk *)bp->b_addr;
173 int ndquots;
174 int i;
175
176 if (!xfs_sb_version_hascrc(&mp->m_sb))
177 return true;
178
179 /*
180 * if we are in log recovery, the quota subsystem has not been
181 * initialised so we have no quotainfo structure. In that case, we need
182 * to manually calculate the number of dquots in the buffer.
183 */
184 if (mp->m_quotainfo)
185 ndquots = mp->m_quotainfo->qi_dqperchunk;
186 else
187 ndquots = xfs_calc_dquots_per_chunk(bp->b_length);
188
189 for (i = 0; i < ndquots; i++, d++) {
190 if (!xfs_verify_cksum((char *)d, sizeof(struct xfs_dqblk),
191 XFS_DQUOT_CRC_OFF))
192 return false;
193 if (!uuid_equal(&d->dd_uuid, &mp->m_sb.sb_meta_uuid))
194 return false;
195 }
196 return true;
197 }
198
199 STATIC bool
200 xfs_dquot_buf_verify(
201 struct xfs_mount *mp,
202 struct xfs_buf *bp,
203 int warn)
204 {
205 struct xfs_dqblk *d = (struct xfs_dqblk *)bp->b_addr;
206 xfs_dqid_t id = 0;
207 int ndquots;
208 int i;
209
210 /*
211 * if we are in log recovery, the quota subsystem has not been
212 * initialised so we have no quotainfo structure. In that case, we need
213 * to manually calculate the number of dquots in the buffer.
214 */
215 if (mp->m_quotainfo)
216 ndquots = mp->m_quotainfo->qi_dqperchunk;
217 else
218 ndquots = xfs_calc_dquots_per_chunk(bp->b_length);
219
220 /*
221 * On the first read of the buffer, verify that each dquot is valid.
222 * We don't know what the id of the dquot is supposed to be, just that
223 * they should be increasing monotonically within the buffer. If the
224 * first id is corrupt, then it will fail on the second dquot in the
225 * buffer so corruptions could point to the wrong dquot in this case.
226 */
227 for (i = 0; i < ndquots; i++) {
228 struct xfs_disk_dquot *ddq;
229 int error;
230
231 ddq = &d[i].dd_diskdq;
232
233 if (i == 0)
234 id = be32_to_cpu(ddq->d_id);
235
236 error = xfs_dqcheck(mp, ddq, id + i, 0, warn, __func__);
237 if (error)
238 return false;
239 }
240 return true;
241 }
242
243 static xfs_failaddr_t
244 xfs_dquot_buf_verify_struct(
245 struct xfs_buf *bp)
246 {
247 struct xfs_mount *mp = bp->b_target->bt_mount;
248
249 if (!xfs_dquot_buf_verify(mp, bp, 0))
250 return __this_address;
251 return NULL;
252 }
253
254 static void
255 xfs_dquot_buf_read_verify(
256 struct xfs_buf *bp)
257 {
258 struct xfs_mount *mp = bp->b_target->bt_mount;
259
260 if (!xfs_dquot_buf_verify_crc(mp, bp))
261 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
262 else if (!xfs_dquot_buf_verify(mp, bp, XFS_QMOPT_DOWARN))
263 xfs_verifier_error(bp, -EFSCORRUPTED, __this_address);
264 }
265
266 /*
267 * readahead errors are silent and simply leave the buffer as !done so a real
268 * read will then be run with the xfs_dquot_buf_ops verifier. See
269 * xfs_inode_buf_verify() for why we use EIO and ~XBF_DONE here rather than
270 * reporting the failure.
271 */
272 static void
273 xfs_dquot_buf_readahead_verify(
274 struct xfs_buf *bp)
275 {
276 struct xfs_mount *mp = bp->b_target->bt_mount;
277
278 if (!xfs_dquot_buf_verify_crc(mp, bp) ||
279 !xfs_dquot_buf_verify(mp, bp, 0)) {
280 xfs_buf_ioerror(bp, -EIO);
281 bp->b_flags &= ~XBF_DONE;
282 }
283 }
284
285 /*
286 * we don't calculate the CRC here as that is done when the dquot is flushed to
287 * the buffer after the update is done. This ensures that the dquot in the
288 * buffer always has an up-to-date CRC value.
289 */
290 static void
291 xfs_dquot_buf_write_verify(
292 struct xfs_buf *bp)
293 {
294 struct xfs_mount *mp = bp->b_target->bt_mount;
295
296 if (!xfs_dquot_buf_verify(mp, bp, XFS_QMOPT_DOWARN)) {
297 xfs_verifier_error(bp, -EFSCORRUPTED, __this_address);
298 return;
299 }
300 }
301
302 const struct xfs_buf_ops xfs_dquot_buf_ops = {
303 .name = "xfs_dquot",
304 .verify_read = xfs_dquot_buf_read_verify,
305 .verify_write = xfs_dquot_buf_write_verify,
306 .verify_struct = xfs_dquot_buf_verify_struct,
307 };
308
309 const struct xfs_buf_ops xfs_dquot_buf_ra_ops = {
310 .name = "xfs_dquot_ra",
311 .verify_read = xfs_dquot_buf_readahead_verify,
312 .verify_write = xfs_dquot_buf_write_verify,
313 };