blob: cce520becee4358f90a60c2ccdd2f417d0db495d [file] [log] [blame]
Dave Chinner9aede1d2013-10-15 09:17:52 +11001/*
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"
Dave Chinner632b89e2013-10-29 22:11:58 +110021#include "xfs_shared.h"
Dave Chinner9aede1d2013-10-15 09:17:52 +110022#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110023#include "xfs_log_format.h"
24#include "xfs_trans_resv.h"
Dave Chinner9aede1d2013-10-15 09:17:52 +110025#include "xfs_mount.h"
Dave Chinner9aede1d2013-10-15 09:17:52 +110026#include "xfs_inode.h"
27#include "xfs_quota.h"
Dave Chinner239880e2013-10-23 10:50:10 +110028#include "xfs_trans.h"
Dave Chinner9aede1d2013-10-15 09:17:52 +110029#include "xfs_qm.h"
30#include "xfs_error.h"
31#include "xfs_cksum.h"
32#include "xfs_trace.h"
33
34int
35xfs_calc_dquots_per_chunk(
Dave Chinner9aede1d2013-10-15 09:17:52 +110036 unsigned int nbblks) /* basic block units */
37{
Dave Chinner9aede1d2013-10-15 09:17:52 +110038 ASSERT(nbblks > 0);
Eric Sandeend956f812017-04-06 16:01:47 -070039 return BBTOB(nbblks) / sizeof(xfs_dqblk_t);
Dave Chinner9aede1d2013-10-15 09:17:52 +110040}
41
42/*
43 * Do some primitive error checking on ondisk dquot data structures.
Eric Sandeen7224fa42018-05-07 09:20:18 -070044 *
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.
Dave Chinner9aede1d2013-10-15 09:17:52 +110048 */
Eric Sandeen7224fa42018-05-07 09:20:18 -070049
Darrick J. Wongeebf3ca2018-01-08 10:51:25 -080050xfs_failaddr_t
51xfs_dquot_verify(
Dave Chinner9aede1d2013-10-15 09:17:52 +110052 struct xfs_mount *mp,
53 xfs_disk_dquot_t *ddq,
54 xfs_dqid_t id,
Eric Sandeen57ab3242018-05-07 09:20:17 -070055 uint type) /* used only during quotacheck */
Dave Chinner9aede1d2013-10-15 09:17:52 +110056{
Dave Chinner9aede1d2013-10-15 09:17:52 +110057 /*
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 */
Darrick J. Wongeebf3ca2018-01-08 10:51:25 -080072 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;
Dave Chinner9aede1d2013-10-15 09:17:52 +110076
Eric Sandeen57ab3242018-05-07 09:20:17 -070077 if (type && ddq->d_flags != type)
78 return __this_address;
Dave Chinner9aede1d2013-10-15 09:17:52 +110079 if (ddq->d_flags != XFS_DQ_USER &&
80 ddq->d_flags != XFS_DQ_PROJ &&
Darrick J. Wongeebf3ca2018-01-08 10:51:25 -080081 ddq->d_flags != XFS_DQ_GROUP)
82 return __this_address;
Dave Chinner9aede1d2013-10-15 09:17:52 +110083
Darrick J. Wongeebf3ca2018-01-08 10:51:25 -080084 if (id != -1 && id != be32_to_cpu(ddq->d_id))
85 return __this_address;
Dave Chinner9aede1d2013-10-15 09:17:52 +110086
Darrick J. Wongeebf3ca2018-01-08 10:51:25 -080087 if (!ddq->d_id)
88 return NULL;
Dave Chinner9aede1d2013-10-15 09:17:52 +110089
Darrick J. Wongeebf3ca2018-01-08 10:51:25 -080090 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;
Darrick J. Wongeeea7982018-01-08 10:51:24 -0800106}
Dave Chinner9aede1d2013-10-15 09:17:52 +1100107
Eric Sandeen7224fa42018-05-07 09:20:18 -0700108xfs_failaddr_t
109xfs_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
Darrick J. Wongeeea7982018-01-08 10:51:24 -0800122/*
123 * Do some primitive error checking on ondisk dquot data structures.
124 */
125int
Eric Sandeen48fa1db2018-05-07 09:20:17 -0700126xfs_dqblk_repair(
Darrick J. Wongeeea7982018-01-08 10:51:24 -0800127 struct xfs_mount *mp,
Eric Sandeen48fa1db2018-05-07 09:20:17 -0700128 struct xfs_dqblk *dqb,
Darrick J. Wongeeea7982018-01-08 10:51:24 -0800129 xfs_dqid_t id,
130 uint type)
131{
Dave Chinner9aede1d2013-10-15 09:17:52 +1100132 /*
133 * Typically, a repair is only requested by quotacheck.
134 */
135 ASSERT(id != -1);
Eric Sandeen48fa1db2018-05-07 09:20:17 -0700136 memset(dqb, 0, sizeof(xfs_dqblk_t));
Dave Chinner9aede1d2013-10-15 09:17:52 +1100137
Eric Sandeen48fa1db2018-05-07 09:20:17 -0700138 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);
Dave Chinner9aede1d2013-10-15 09:17:52 +1100142
143 if (xfs_sb_version_hascrc(&mp->m_sb)) {
Eric Sandeen48fa1db2018-05-07 09:20:17 -0700144 uuid_copy(&dqb->dd_uuid, &mp->m_sb.sb_meta_uuid);
145 xfs_update_cksum((char *)dqb, sizeof(struct xfs_dqblk),
Dave Chinner9aede1d2013-10-15 09:17:52 +1100146 XFS_DQUOT_CRC_OFF);
147 }
148
Darrick J. Wongeeea7982018-01-08 10:51:24 -0800149 return 0;
Dave Chinner9aede1d2013-10-15 09:17:52 +1100150}
151
152STATIC bool
153xfs_dquot_buf_verify_crc(
154 struct xfs_mount *mp,
Eric Sandeen72c5c5f2018-05-07 09:20:47 -0700155 struct xfs_buf *bp,
156 bool readahead)
Dave Chinner9aede1d2013-10-15 09:17:52 +1100157{
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
Darrick J. Wong58d78962016-10-20 15:46:18 +1100173 ndquots = xfs_calc_dquots_per_chunk(bp->b_length);
Dave Chinner9aede1d2013-10-15 09:17:52 +1100174
175 for (i = 0; i < ndquots; i++, d++) {
176 if (!xfs_verify_cksum((char *)d, sizeof(struct xfs_dqblk),
Eric Sandeen72c5c5f2018-05-07 09:20:47 -0700177 XFS_DQUOT_CRC_OFF)) {
178 if (!readahead)
179 xfs_buf_verifier_error(bp, -EFSBADCRC, __func__,
180 d, sizeof(*d), __this_address);
Dave Chinner9aede1d2013-10-15 09:17:52 +1100181 return false;
Eric Sandeen72c5c5f2018-05-07 09:20:47 -0700182 }
Dave Chinner9aede1d2013-10-15 09:17:52 +1100183 }
184 return true;
185}
186
Darrick J. Wongeebf3ca2018-01-08 10:51:25 -0800187STATIC xfs_failaddr_t
Dave Chinner9aede1d2013-10-15 09:17:52 +1100188xfs_dquot_buf_verify(
189 struct xfs_mount *mp,
Eric Sandeen72c5c5f2018-05-07 09:20:47 -0700190 struct xfs_buf *bp,
191 bool readahead)
Dave Chinner9aede1d2013-10-15 09:17:52 +1100192{
Eric Sandeen7224fa42018-05-07 09:20:18 -0700193 struct xfs_dqblk *dqb = bp->b_addr;
Darrick J. Wongeebf3ca2018-01-08 10:51:25 -0800194 xfs_failaddr_t fa;
Dave Chinner9aede1d2013-10-15 09:17:52 +1100195 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
Eric Sandeen6ea94bb2014-04-14 19:03:34 +1000207 ndquots = xfs_calc_dquots_per_chunk(bp->b_length);
Dave Chinner9aede1d2013-10-15 09:17:52 +1100208
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;
Dave Chinner9aede1d2013-10-15 09:17:52 +1100218
Eric Sandeen7224fa42018-05-07 09:20:18 -0700219 ddq = &dqb[i].dd_diskdq;
Dave Chinner9aede1d2013-10-15 09:17:52 +1100220
221 if (i == 0)
222 id = be32_to_cpu(ddq->d_id);
223
Eric Sandeen7224fa42018-05-07 09:20:18 -0700224 fa = xfs_dqblk_verify(mp, &dqb[i], id + i, 0);
Eric Sandeen72c5c5f2018-05-07 09:20:47 -0700225 if (fa) {
226 if (!readahead)
227 xfs_buf_verifier_error(bp, -EFSCORRUPTED,
228 __func__, &dqb[i],
229 sizeof(struct xfs_dqblk), fa);
Darrick J. Wongeebf3ca2018-01-08 10:51:25 -0800230 return fa;
Eric Sandeen72c5c5f2018-05-07 09:20:47 -0700231 }
Dave Chinner9aede1d2013-10-15 09:17:52 +1100232 }
Darrick J. Wongeebf3ca2018-01-08 10:51:25 -0800233
234 return NULL;
Dave Chinner9aede1d2013-10-15 09:17:52 +1100235}
236
Darrick J. Wongb5572592018-01-08 10:51:08 -0800237static xfs_failaddr_t
238xfs_dquot_buf_verify_struct(
Darrick J. Wongeebf3ca2018-01-08 10:51:25 -0800239 struct xfs_buf *bp)
Darrick J. Wongb5572592018-01-08 10:51:08 -0800240{
241 struct xfs_mount *mp = bp->b_target->bt_mount;
242
Eric Sandeen72c5c5f2018-05-07 09:20:47 -0700243 return xfs_dquot_buf_verify(mp, bp, false);
Darrick J. Wongb5572592018-01-08 10:51:08 -0800244}
245
Dave Chinner9aede1d2013-10-15 09:17:52 +1100246static void
247xfs_dquot_buf_read_verify(
Darrick J. Wongeebf3ca2018-01-08 10:51:25 -0800248 struct xfs_buf *bp)
Dave Chinner9aede1d2013-10-15 09:17:52 +1100249{
250 struct xfs_mount *mp = bp->b_target->bt_mount;
251
Eric Sandeen72c5c5f2018-05-07 09:20:47 -0700252 if (!xfs_dquot_buf_verify_crc(mp, bp, false))
253 return;
254 xfs_dquot_buf_verify(mp, bp, false);
Dave Chinner9aede1d2013-10-15 09:17:52 +1100255}
256
257/*
Dave Chinner7d6a13f2016-01-12 07:04:01 +1100258 * 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 */
263static void
264xfs_dquot_buf_readahead_verify(
265 struct xfs_buf *bp)
266{
267 struct xfs_mount *mp = bp->b_target->bt_mount;
268
Eric Sandeen72c5c5f2018-05-07 09:20:47 -0700269 if (!xfs_dquot_buf_verify_crc(mp, bp, true) ||
270 xfs_dquot_buf_verify(mp, bp, true) != NULL) {
Dave Chinner7d6a13f2016-01-12 07:04:01 +1100271 xfs_buf_ioerror(bp, -EIO);
272 bp->b_flags &= ~XBF_DONE;
273 }
274}
275
276/*
Dave Chinner9aede1d2013-10-15 09:17:52 +1100277 * 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 */
Dave Chinner632b89e2013-10-29 22:11:58 +1100281static void
Dave Chinner9aede1d2013-10-15 09:17:52 +1100282xfs_dquot_buf_write_verify(
Darrick J. Wongeebf3ca2018-01-08 10:51:25 -0800283 struct xfs_buf *bp)
Dave Chinner9aede1d2013-10-15 09:17:52 +1100284{
285 struct xfs_mount *mp = bp->b_target->bt_mount;
286
Eric Sandeen72c5c5f2018-05-07 09:20:47 -0700287 xfs_dquot_buf_verify(mp, bp, false);
Dave Chinner9aede1d2013-10-15 09:17:52 +1100288}
289
290const struct xfs_buf_ops xfs_dquot_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +1100291 .name = "xfs_dquot",
Dave Chinner9aede1d2013-10-15 09:17:52 +1100292 .verify_read = xfs_dquot_buf_read_verify,
293 .verify_write = xfs_dquot_buf_write_verify,
Darrick J. Wongb5572592018-01-08 10:51:08 -0800294 .verify_struct = xfs_dquot_buf_verify_struct,
Dave Chinner9aede1d2013-10-15 09:17:52 +1100295};
296
Dave Chinner7d6a13f2016-01-12 07:04:01 +1100297const 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};