2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
3 * Copyright (c) 2013 Red Hat, Inc.
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.
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.
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
19 #include "libxfs_priv.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"
33 xfs_calc_dquots_per_chunk(
34 unsigned int nbblks
) /* basic block units */
37 return BBTOB(nbblks
) / sizeof(xfs_dqblk_t
);
41 * Do some primitive error checking on ondisk dquot data structures.
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.
51 xfs_disk_dquot_t
*ddq
,
53 uint type
) /* used only during quotacheck */
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
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.
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.
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
;
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
;
82 if (id
!= -1 && id
!= be32_to_cpu(ddq
->d_id
))
83 return __this_address
;
88 if (ddq
->d_blk_softlimit
&&
89 be64_to_cpu(ddq
->d_bcount
) > be64_to_cpu(ddq
->d_blk_softlimit
) &&
91 return __this_address
;
93 if (ddq
->d_ino_softlimit
&&
94 be64_to_cpu(ddq
->d_icount
) > be64_to_cpu(ddq
->d_ino_softlimit
) &&
96 return __this_address
;
98 if (ddq
->d_rtb_softlimit
&&
99 be64_to_cpu(ddq
->d_rtbcount
) > be64_to_cpu(ddq
->d_rtb_softlimit
) &&
101 return __this_address
;
108 struct xfs_mount
*mp
,
109 struct xfs_dqblk
*dqb
,
111 uint type
) /* used only during quotacheck */
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
;
117 return xfs_dquot_verify(mp
, &dqb
->dd_diskdq
, id
, type
);
121 * Do some primitive error checking on ondisk dquot data structures.
125 struct xfs_mount
*mp
,
126 struct xfs_dqblk
*dqb
,
131 * Typically, a repair is only requested by quotacheck.
134 memset(dqb
, 0, sizeof(xfs_dqblk_t
));
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
);
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
),
151 xfs_dquot_buf_verify_crc(
152 struct xfs_mount
*mp
,
155 struct xfs_dqblk
*d
= (struct xfs_dqblk
*)bp
->b_addr
;
159 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
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.
168 ndquots
= mp
->m_quotainfo
->qi_dqperchunk
;
170 ndquots
= xfs_calc_dquots_per_chunk(bp
->b_length
);
172 for (i
= 0; i
< ndquots
; i
++, d
++) {
173 if (!xfs_verify_cksum((char *)d
, sizeof(struct xfs_dqblk
),
180 STATIC xfs_failaddr_t
181 xfs_dquot_buf_verify(
182 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
, 0);
224 static xfs_failaddr_t
225 xfs_dquot_buf_verify_struct(
228 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
230 return xfs_dquot_buf_verify(mp
, bp
);
234 xfs_dquot_buf_read_verify(
237 struct xfs_mount
*mp
= bp
->b_target
->bt_mount
;
240 if (!xfs_dquot_buf_verify_crc(mp
, bp
))
241 xfs_verifier_error(bp
, -EFSBADCRC
, __this_address
);
243 fa
= xfs_dquot_buf_verify(mp
, bp
);
245 xfs_verifier_error(bp
, -EFSCORRUPTED
, __this_address
);
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_target
->bt_mount
;
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
;
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_target
->bt_mount
;
280 fa
= xfs_dquot_buf_verify(mp
, bp
);
282 xfs_verifier_error(bp
, -EFSCORRUPTED
, __this_address
);
285 const struct xfs_buf_ops xfs_dquot_buf_ops
= {
287 .verify_read
= xfs_dquot_buf_read_verify
,
288 .verify_write
= xfs_dquot_buf_write_verify
,
289 .verify_struct
= xfs_dquot_buf_verify_struct
,
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
,