1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
4 * Copyright (c) 2013 Red Hat, Inc.
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"
18 #include "xfs_error.h"
21 xfs_calc_dquots_per_chunk(
22 unsigned int nbblks) /* basic block units */
25 return BBTOB(nbblks) / sizeof(struct xfs_dqblk);
29 * Do some primitive error checking on ondisk dquot data structures.
31 * The xfs_dqblk structure /contains/ the xfs_disk_dquot structure;
32 * we verify them separately because at some points we have only the
33 * smaller xfs_disk_dquot structure available.
39 struct xfs_disk_dquot *ddq,
40 xfs_dqid_t id) /* used only during quotacheck */
45 * We can encounter an uninitialized dquot buffer for 2 reasons:
46 * 1. If we crash while deleting the quotainode(s), and those blks got
47 * used for user data. This is because we take the path of regular
48 * file deletion; however, the size field of quotainodes is never
49 * updated, so all the tricks that we play in itruncate_finish
52 * 2. We don't play the quota buffers when there's a quotaoff logitem.
53 * But the allocation will be replayed so we'll end up with an
54 * uninitialized quota block.
56 * This is all fine; things are still consistent, and we haven't lost
57 * any quota information. Just don't complain about bad dquot blks.
59 if (ddq->d_magic != cpu_to_be16(XFS_DQUOT_MAGIC))
60 return __this_address;
61 if (ddq->d_version != XFS_DQUOT_VERSION)
62 return __this_address;
64 if (ddq->d_type & ~XFS_DQTYPE_ANY)
65 return __this_address;
66 ddq_type = ddq->d_type & XFS_DQTYPE_REC_MASK;
67 if (ddq_type != XFS_DQTYPE_USER &&
68 ddq_type != XFS_DQTYPE_PROJ &&
69 ddq_type != XFS_DQTYPE_GROUP)
70 return __this_address;
72 if ((ddq->d_type & XFS_DQTYPE_BIGTIME) &&
74 return __this_address;
76 if ((ddq->d_type & XFS_DQTYPE_BIGTIME) && !ddq->d_id)
77 return __this_address;
79 if (id != -1 && id != be32_to_cpu(ddq->d_id))
80 return __this_address;
85 if (ddq->d_blk_softlimit &&
86 be64_to_cpu(ddq->d_bcount) > be64_to_cpu(ddq->d_blk_softlimit) &&
88 return __this_address;
90 if (ddq->d_ino_softlimit &&
91 be64_to_cpu(ddq->d_icount) > be64_to_cpu(ddq->d_ino_softlimit) &&
93 return __this_address;
95 if (ddq->d_rtb_softlimit &&
96 be64_to_cpu(ddq->d_rtbcount) > be64_to_cpu(ddq->d_rtb_softlimit) &&
98 return __this_address;
105 struct xfs_mount *mp,
106 struct xfs_dqblk *dqb,
107 xfs_dqid_t id) /* used only during quotacheck */
109 if (xfs_has_crc(mp) &&
110 !uuid_equal(&dqb->dd_uuid, &mp->m_sb.sb_meta_uuid))
111 return __this_address;
113 return xfs_dquot_verify(mp, &dqb->dd_diskdq, id);
117 * Do some primitive error checking on ondisk dquot data structures.
121 struct xfs_mount *mp,
122 struct xfs_dqblk *dqb,
127 * Typically, a repair is only requested by quotacheck.
130 memset(dqb, 0, sizeof(struct xfs_dqblk));
132 dqb->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
133 dqb->dd_diskdq.d_version = XFS_DQUOT_VERSION;
134 dqb->dd_diskdq.d_type = type;
135 dqb->dd_diskdq.d_id = cpu_to_be32(id);
137 if (xfs_has_crc(mp)) {
138 uuid_copy(&dqb->dd_uuid, &mp->m_sb.sb_meta_uuid);
139 xfs_update_cksum((char *)dqb, sizeof(struct xfs_dqblk),
145 xfs_dquot_buf_verify_crc(
146 struct xfs_mount *mp,
150 struct xfs_dqblk *d = (struct xfs_dqblk *)bp->b_addr;
154 if (!xfs_has_crc(mp))
158 * if we are in log recovery, the quota subsystem has not been
159 * initialised so we have no quotainfo structure. In that case, we need
160 * to manually calculate the number of dquots in the buffer.
163 ndquots = mp->m_quotainfo->qi_dqperchunk;
165 ndquots = xfs_calc_dquots_per_chunk(bp->b_length);
167 for (i = 0; i < ndquots; i++, d++) {
168 if (!xfs_verify_cksum((char *)d, sizeof(struct xfs_dqblk),
169 XFS_DQUOT_CRC_OFF)) {
171 xfs_buf_verifier_error(bp, -EFSBADCRC, __func__,
172 d, sizeof(*d), __this_address);
179 STATIC xfs_failaddr_t
180 xfs_dquot_buf_verify(
181 struct xfs_mount *mp,
185 struct xfs_dqblk *dqb = bp->b_addr;
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.
197 ndquots = mp->m_quotainfo->qi_dqperchunk;
199 ndquots = xfs_calc_dquots_per_chunk(bp->b_length);
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.
208 for (i = 0; i < ndquots; i++) {
209 struct xfs_disk_dquot *ddq;
211 ddq = &dqb[i].dd_diskdq;
214 id = be32_to_cpu(ddq->d_id);
216 fa = xfs_dqblk_verify(mp, &dqb[i], id + i);
219 xfs_buf_verifier_error(bp, -EFSCORRUPTED,
221 sizeof(struct xfs_dqblk), fa);
229 static xfs_failaddr_t
230 xfs_dquot_buf_verify_struct(
233 struct xfs_mount *mp = bp->b_mount;
235 return xfs_dquot_buf_verify(mp, bp, false);
239 xfs_dquot_buf_read_verify(
242 struct xfs_mount *mp = bp->b_mount;
244 if (!xfs_dquot_buf_verify_crc(mp, bp, false))
246 xfs_dquot_buf_verify(mp, bp, false);
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.
256 xfs_dquot_buf_readahead_verify(
259 struct xfs_mount *mp = bp->b_mount;
261 if (!xfs_dquot_buf_verify_crc(mp, bp, true) ||
262 xfs_dquot_buf_verify(mp, bp, true) != NULL) {
263 xfs_buf_ioerror(bp, -EIO);
264 bp->b_flags &= ~XBF_DONE;
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.
274 xfs_dquot_buf_write_verify(
277 struct xfs_mount *mp = bp->b_mount;
279 xfs_dquot_buf_verify(mp, bp, false);
282 const struct xfs_buf_ops xfs_dquot_buf_ops = {
284 .magic16 = { cpu_to_be16(XFS_DQUOT_MAGIC),
285 cpu_to_be16(XFS_DQUOT_MAGIC) },
286 .verify_read = xfs_dquot_buf_read_verify,
287 .verify_write = xfs_dquot_buf_write_verify,
288 .verify_struct = xfs_dquot_buf_verify_struct,
291 const struct xfs_buf_ops xfs_dquot_buf_ra_ops = {
292 .name = "xfs_dquot_ra",
293 .magic16 = { cpu_to_be16(XFS_DQUOT_MAGIC),
294 cpu_to_be16(XFS_DQUOT_MAGIC) },
295 .verify_read = xfs_dquot_buf_readahead_verify,
296 .verify_write = xfs_dquot_buf_write_verify,
299 /* Convert an on-disk timer value into an incore timer value. */
301 xfs_dquot_from_disk_ts(
302 struct xfs_disk_dquot *ddq,
305 uint32_t t = be32_to_cpu(dtimer);
307 if (t != 0 && (ddq->d_type & XFS_DQTYPE_BIGTIME))
308 return xfs_dq_bigtime_to_unix(t);
313 /* Convert an incore timer value into an on-disk timer value. */
315 xfs_dquot_to_disk_ts(
316 struct xfs_dquot *dqp,
321 if (timer != 0 && (dqp->q_type & XFS_DQTYPE_BIGTIME))
322 t = xfs_dq_unix_to_bigtime(timer);
324 return cpu_to_be32(t);