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(xfs_dqblk_t);
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 xfs_disk_dquot_t *ddq,
41 uint type) /* used only during quotacheck */
44 * We can encounter an uninitialized dquot buffer for 2 reasons:
45 * 1. If we crash while deleting the quotainode(s), and those blks got
46 * used for user data. This is because we take the path of regular
47 * file deletion; however, the size field of quotainodes is never
48 * updated, so all the tricks that we play in itruncate_finish
51 * 2. We don't play the quota buffers when there's a quotaoff logitem.
52 * But the allocation will be replayed so we'll end up with an
53 * uninitialized quota block.
55 * This is all fine; things are still consistent, and we haven't lost
56 * any quota information. Just don't complain about bad dquot blks.
58 if (ddq->d_magic != cpu_to_be16(XFS_DQUOT_MAGIC))
59 return __this_address;
60 if (ddq->d_version != XFS_DQUOT_VERSION)
61 return __this_address;
63 if (type && ddq->d_flags != type)
64 return __this_address;
65 if (ddq->d_flags != XFS_DQ_USER &&
66 ddq->d_flags != XFS_DQ_PROJ &&
67 ddq->d_flags != XFS_DQ_GROUP)
68 return __this_address;
70 if (id != -1 && id != be32_to_cpu(ddq->d_id))
71 return __this_address;
76 if (ddq->d_blk_softlimit &&
77 be64_to_cpu(ddq->d_bcount) > be64_to_cpu(ddq->d_blk_softlimit) &&
79 return __this_address;
81 if (ddq->d_ino_softlimit &&
82 be64_to_cpu(ddq->d_icount) > be64_to_cpu(ddq->d_ino_softlimit) &&
84 return __this_address;
86 if (ddq->d_rtb_softlimit &&
87 be64_to_cpu(ddq->d_rtbcount) > be64_to_cpu(ddq->d_rtb_softlimit) &&
89 return __this_address;
97 struct xfs_dqblk *dqb,
99 uint type) /* used only during quotacheck */
101 if (xfs_sb_version_hascrc(&mp->m_sb) &&
102 !uuid_equal(&dqb->dd_uuid, &mp->m_sb.sb_meta_uuid))
103 return __this_address;
105 return xfs_dquot_verify(mp, &dqb->dd_diskdq, id, type);
109 * Do some primitive error checking on ondisk dquot data structures.
113 struct xfs_mount *mp,
114 struct xfs_dqblk *dqb,
119 * Typically, a repair is only requested by quotacheck.
122 memset(dqb, 0, sizeof(xfs_dqblk_t));
124 dqb->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
125 dqb->dd_diskdq.d_version = XFS_DQUOT_VERSION;
126 dqb->dd_diskdq.d_flags = type;
127 dqb->dd_diskdq.d_id = cpu_to_be32(id);
129 if (xfs_sb_version_hascrc(&mp->m_sb)) {
130 uuid_copy(&dqb->dd_uuid, &mp->m_sb.sb_meta_uuid);
131 xfs_update_cksum((char *)dqb, sizeof(struct xfs_dqblk),
137 xfs_dquot_buf_verify_crc(
138 struct xfs_mount *mp,
142 struct xfs_dqblk *d = (struct xfs_dqblk *)bp->b_addr;
146 if (!xfs_sb_version_hascrc(&mp->m_sb))
150 * if we are in log recovery, the quota subsystem has not been
151 * initialised so we have no quotainfo structure. In that case, we need
152 * to manually calculate the number of dquots in the buffer.
155 ndquots = mp->m_quotainfo->qi_dqperchunk;
157 ndquots = xfs_calc_dquots_per_chunk(bp->b_length);
159 for (i = 0; i < ndquots; i++, d++) {
160 if (!xfs_verify_cksum((char *)d, sizeof(struct xfs_dqblk),
161 XFS_DQUOT_CRC_OFF)) {
163 xfs_buf_verifier_error(bp, -EFSBADCRC, __func__,
164 d, sizeof(*d), __this_address);
171 STATIC xfs_failaddr_t
172 xfs_dquot_buf_verify(
173 struct xfs_mount *mp,
177 struct xfs_dqblk *dqb = bp->b_addr;
184 * if we are in log recovery, the quota subsystem has not been
185 * initialised so we have no quotainfo structure. In that case, we need
186 * to manually calculate the number of dquots in the buffer.
189 ndquots = mp->m_quotainfo->qi_dqperchunk;
191 ndquots = xfs_calc_dquots_per_chunk(bp->b_length);
194 * On the first read of the buffer, verify that each dquot is valid.
195 * We don't know what the id of the dquot is supposed to be, just that
196 * they should be increasing monotonically within the buffer. If the
197 * first id is corrupt, then it will fail on the second dquot in the
198 * buffer so corruptions could point to the wrong dquot in this case.
200 for (i = 0; i < ndquots; i++) {
201 struct xfs_disk_dquot *ddq;
203 ddq = &dqb[i].dd_diskdq;
206 id = be32_to_cpu(ddq->d_id);
208 fa = xfs_dqblk_verify(mp, &dqb[i], id + i, 0);
211 xfs_buf_verifier_error(bp, -EFSCORRUPTED,
213 sizeof(struct xfs_dqblk), fa);
221 static xfs_failaddr_t
222 xfs_dquot_buf_verify_struct(
225 struct xfs_mount *mp = bp->b_mount;
227 return xfs_dquot_buf_verify(mp, bp, false);
231 xfs_dquot_buf_read_verify(
234 struct xfs_mount *mp = bp->b_mount;
236 if (!xfs_dquot_buf_verify_crc(mp, bp, false))
238 xfs_dquot_buf_verify(mp, bp, false);
242 * readahead errors are silent and simply leave the buffer as !done so a real
243 * read will then be run with the xfs_dquot_buf_ops verifier. See
244 * xfs_inode_buf_verify() for why we use EIO and ~XBF_DONE here rather than
245 * reporting the failure.
248 xfs_dquot_buf_readahead_verify(
251 struct xfs_mount *mp = bp->b_mount;
253 if (!xfs_dquot_buf_verify_crc(mp, bp, true) ||
254 xfs_dquot_buf_verify(mp, bp, true) != NULL) {
255 xfs_buf_ioerror(bp, -EIO);
256 bp->b_flags &= ~XBF_DONE;
261 * we don't calculate the CRC here as that is done when the dquot is flushed to
262 * the buffer after the update is done. This ensures that the dquot in the
263 * buffer always has an up-to-date CRC value.
266 xfs_dquot_buf_write_verify(
269 struct xfs_mount *mp = bp->b_mount;
271 xfs_dquot_buf_verify(mp, bp, false);
274 const struct xfs_buf_ops xfs_dquot_buf_ops = {
276 .magic16 = { cpu_to_be16(XFS_DQUOT_MAGIC),
277 cpu_to_be16(XFS_DQUOT_MAGIC) },
278 .verify_read = xfs_dquot_buf_read_verify,
279 .verify_write = xfs_dquot_buf_write_verify,
280 .verify_struct = xfs_dquot_buf_verify_struct,
283 const struct xfs_buf_ops xfs_dquot_buf_ra_ops = {
284 .name = "xfs_dquot_ra",
285 .magic16 = { cpu_to_be16(XFS_DQUOT_MAGIC),
286 cpu_to_be16(XFS_DQUOT_MAGIC) },
287 .verify_read = xfs_dquot_buf_readahead_verify,
288 .verify_write = xfs_dquot_buf_write_verify,