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