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