]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/xfs_dquot_buf.c
xfs: add full xfs_dqblk verifier
[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 * The xfs_dqblk structure /contains/ the xfs_disk_dquot structure;
44 * we verify them separately because at some points we have only the
45 * smaller xfs_disk_dquot structure available.
46 */
47
48 xfs_failaddr_t
49 xfs_dquot_verify(
50 struct xfs_mount *mp,
51 xfs_disk_dquot_t *ddq,
52 xfs_dqid_t id,
53 uint type) /* used only during quotacheck */
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 return __this_address;
72 if (ddq->d_version != XFS_DQUOT_VERSION)
73 return __this_address;
74
75 if (type && ddq->d_flags != type)
76 return __this_address;
77 if (ddq->d_flags != XFS_DQ_USER &&
78 ddq->d_flags != XFS_DQ_PROJ &&
79 ddq->d_flags != XFS_DQ_GROUP)
80 return __this_address;
81
82 if (id != -1 && id != be32_to_cpu(ddq->d_id))
83 return __this_address;
84
85 if (!ddq->d_id)
86 return NULL;
87
88 if (ddq->d_blk_softlimit &&
89 be64_to_cpu(ddq->d_bcount) > be64_to_cpu(ddq->d_blk_softlimit) &&
90 !ddq->d_btimer)
91 return __this_address;
92
93 if (ddq->d_ino_softlimit &&
94 be64_to_cpu(ddq->d_icount) > be64_to_cpu(ddq->d_ino_softlimit) &&
95 !ddq->d_itimer)
96 return __this_address;
97
98 if (ddq->d_rtb_softlimit &&
99 be64_to_cpu(ddq->d_rtbcount) > be64_to_cpu(ddq->d_rtb_softlimit) &&
100 !ddq->d_rtbtimer)
101 return __this_address;
102
103 return NULL;
104 }
105
106 xfs_failaddr_t
107 xfs_dqblk_verify(
108 struct xfs_mount *mp,
109 struct xfs_dqblk *dqb,
110 xfs_dqid_t id,
111 uint type) /* used only during quotacheck */
112 {
113 if (xfs_sb_version_hascrc(&mp->m_sb) &&
114 !uuid_equal(&dqb->dd_uuid, &mp->m_sb.sb_meta_uuid))
115 return __this_address;
116
117 return xfs_dquot_verify(mp, &dqb->dd_diskdq, id, type);
118 }
119
120 /*
121 * Do some primitive error checking on ondisk dquot data structures.
122 */
123 int
124 xfs_dqblk_repair(
125 struct xfs_mount *mp,
126 struct xfs_dqblk *dqb,
127 xfs_dqid_t id,
128 uint type)
129 {
130 /*
131 * Typically, a repair is only requested by quotacheck.
132 */
133 ASSERT(id != -1);
134 memset(dqb, 0, sizeof(xfs_dqblk_t));
135
136 dqb->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
137 dqb->dd_diskdq.d_version = XFS_DQUOT_VERSION;
138 dqb->dd_diskdq.d_flags = type;
139 dqb->dd_diskdq.d_id = cpu_to_be32(id);
140
141 if (xfs_sb_version_hascrc(&mp->m_sb)) {
142 uuid_copy(&dqb->dd_uuid, &mp->m_sb.sb_meta_uuid);
143 xfs_update_cksum((char *)dqb, sizeof(struct xfs_dqblk),
144 XFS_DQUOT_CRC_OFF);
145 }
146
147 return 0;
148 }
149
150 STATIC bool
151 xfs_dquot_buf_verify_crc(
152 struct xfs_mount *mp,
153 struct xfs_buf *bp)
154 {
155 struct xfs_dqblk *d = (struct xfs_dqblk *)bp->b_addr;
156 int ndquots;
157 int i;
158
159 if (!xfs_sb_version_hascrc(&mp->m_sb))
160 return true;
161
162 /*
163 * if we are in log recovery, the quota subsystem has not been
164 * initialised so we have no quotainfo structure. In that case, we need
165 * to manually calculate the number of dquots in the buffer.
166 */
167 if (mp->m_quotainfo)
168 ndquots = mp->m_quotainfo->qi_dqperchunk;
169 else
170 ndquots = xfs_calc_dquots_per_chunk(bp->b_length);
171
172 for (i = 0; i < ndquots; i++, d++) {
173 if (!xfs_verify_cksum((char *)d, sizeof(struct xfs_dqblk),
174 XFS_DQUOT_CRC_OFF))
175 return false;
176 }
177 return true;
178 }
179
180 STATIC xfs_failaddr_t
181 xfs_dquot_buf_verify(
182 struct xfs_mount *mp,
183 struct xfs_buf *bp)
184 {
185 struct xfs_dqblk *dqb = bp->b_addr;
186 xfs_failaddr_t fa;
187 xfs_dqid_t id = 0;
188 int ndquots;
189 int i;
190
191 /*
192 * if we are in log recovery, the quota subsystem has not been
193 * initialised so we have no quotainfo structure. In that case, we need
194 * to manually calculate the number of dquots in the buffer.
195 */
196 if (mp->m_quotainfo)
197 ndquots = mp->m_quotainfo->qi_dqperchunk;
198 else
199 ndquots = xfs_calc_dquots_per_chunk(bp->b_length);
200
201 /*
202 * On the first read of the buffer, verify that each dquot is valid.
203 * We don't know what the id of the dquot is supposed to be, just that
204 * they should be increasing monotonically within the buffer. If the
205 * first id is corrupt, then it will fail on the second dquot in the
206 * buffer so corruptions could point to the wrong dquot in this case.
207 */
208 for (i = 0; i < ndquots; i++) {
209 struct xfs_disk_dquot *ddq;
210
211 ddq = &dqb[i].dd_diskdq;
212
213 if (i == 0)
214 id = be32_to_cpu(ddq->d_id);
215
216 fa = xfs_dqblk_verify(mp, &dqb[i], id + i, 0);
217 if (fa)
218 return fa;
219 }
220
221 return NULL;
222 }
223
224 static xfs_failaddr_t
225 xfs_dquot_buf_verify_struct(
226 struct xfs_buf *bp)
227 {
228 struct xfs_mount *mp = bp->b_target->bt_mount;
229
230 return xfs_dquot_buf_verify(mp, bp);
231 }
232
233 static void
234 xfs_dquot_buf_read_verify(
235 struct xfs_buf *bp)
236 {
237 struct xfs_mount *mp = bp->b_target->bt_mount;
238 xfs_failaddr_t fa;
239
240 if (!xfs_dquot_buf_verify_crc(mp, bp))
241 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
242 else {
243 fa = xfs_dquot_buf_verify(mp, bp);
244 if (fa)
245 xfs_verifier_error(bp, -EFSCORRUPTED, __this_address);
246 }
247 }
248
249 /*
250 * readahead errors are silent and simply leave the buffer as !done so a real
251 * read will then be run with the xfs_dquot_buf_ops verifier. See
252 * xfs_inode_buf_verify() for why we use EIO and ~XBF_DONE here rather than
253 * reporting the failure.
254 */
255 static void
256 xfs_dquot_buf_readahead_verify(
257 struct xfs_buf *bp)
258 {
259 struct xfs_mount *mp = bp->b_target->bt_mount;
260
261 if (!xfs_dquot_buf_verify_crc(mp, bp) ||
262 xfs_dquot_buf_verify(mp, bp) != NULL) {
263 xfs_buf_ioerror(bp, -EIO);
264 bp->b_flags &= ~XBF_DONE;
265 }
266 }
267
268 /*
269 * we don't calculate the CRC here as that is done when the dquot is flushed to
270 * the buffer after the update is done. This ensures that the dquot in the
271 * buffer always has an up-to-date CRC value.
272 */
273 static void
274 xfs_dquot_buf_write_verify(
275 struct xfs_buf *bp)
276 {
277 struct xfs_mount *mp = bp->b_target->bt_mount;
278 xfs_failaddr_t fa;
279
280 fa = xfs_dquot_buf_verify(mp, bp);
281 if (fa)
282 xfs_verifier_error(bp, -EFSCORRUPTED, __this_address);
283 }
284
285 const struct xfs_buf_ops xfs_dquot_buf_ops = {
286 .name = "xfs_dquot",
287 .verify_read = xfs_dquot_buf_read_verify,
288 .verify_write = xfs_dquot_buf_write_verify,
289 .verify_struct = xfs_dquot_buf_verify_struct,
290 };
291
292 const struct xfs_buf_ops xfs_dquot_buf_ra_ops = {
293 .name = "xfs_dquot_ra",
294 .verify_read = xfs_dquot_buf_readahead_verify,
295 .verify_write = xfs_dquot_buf_write_verify,
296 };