blob: 747085b4ef4406d387464b40fdea302b83c25c4c [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.
44 */
45int
46xfs_dqcheck(
47 struct xfs_mount *mp,
48 xfs_disk_dquot_t *ddq,
49 xfs_dqid_t id,
50 uint type, /* used only when IO_dorepair is true */
51 uint flags,
Dave Chinner7d6a13f2016-01-12 07:04:01 +110052 const char *str)
Dave Chinner9aede1d2013-10-15 09:17:52 +110053{
54 xfs_dqblk_t *d = (xfs_dqblk_t *)ddq;
55 int errs = 0;
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 if (flags & XFS_QMOPT_DOWARN)
74 xfs_alert(mp,
75 "%s : XFS dquot ID 0x%x, magic 0x%x != 0x%x",
76 str, id, be16_to_cpu(ddq->d_magic), XFS_DQUOT_MAGIC);
77 errs++;
78 }
79 if (ddq->d_version != XFS_DQUOT_VERSION) {
80 if (flags & XFS_QMOPT_DOWARN)
81 xfs_alert(mp,
82 "%s : XFS dquot ID 0x%x, version 0x%x != 0x%x",
83 str, id, ddq->d_version, XFS_DQUOT_VERSION);
84 errs++;
85 }
86
87 if (ddq->d_flags != XFS_DQ_USER &&
88 ddq->d_flags != XFS_DQ_PROJ &&
89 ddq->d_flags != XFS_DQ_GROUP) {
90 if (flags & XFS_QMOPT_DOWARN)
91 xfs_alert(mp,
92 "%s : XFS dquot ID 0x%x, unknown flags 0x%x",
93 str, id, ddq->d_flags);
94 errs++;
95 }
96
97 if (id != -1 && id != be32_to_cpu(ddq->d_id)) {
98 if (flags & XFS_QMOPT_DOWARN)
99 xfs_alert(mp,
100 "%s : ondisk-dquot 0x%p, ID mismatch: "
101 "0x%x expected, found id 0x%x",
102 str, ddq, id, be32_to_cpu(ddq->d_id));
103 errs++;
104 }
105
106 if (!errs && ddq->d_id) {
107 if (ddq->d_blk_softlimit &&
108 be64_to_cpu(ddq->d_bcount) >
109 be64_to_cpu(ddq->d_blk_softlimit)) {
110 if (!ddq->d_btimer) {
111 if (flags & XFS_QMOPT_DOWARN)
112 xfs_alert(mp,
113 "%s : Dquot ID 0x%x (0x%p) BLK TIMER NOT STARTED",
114 str, (int)be32_to_cpu(ddq->d_id), ddq);
115 errs++;
116 }
117 }
118 if (ddq->d_ino_softlimit &&
119 be64_to_cpu(ddq->d_icount) >
120 be64_to_cpu(ddq->d_ino_softlimit)) {
121 if (!ddq->d_itimer) {
122 if (flags & XFS_QMOPT_DOWARN)
123 xfs_alert(mp,
124 "%s : Dquot ID 0x%x (0x%p) INODE TIMER NOT STARTED",
125 str, (int)be32_to_cpu(ddq->d_id), ddq);
126 errs++;
127 }
128 }
129 if (ddq->d_rtb_softlimit &&
130 be64_to_cpu(ddq->d_rtbcount) >
131 be64_to_cpu(ddq->d_rtb_softlimit)) {
132 if (!ddq->d_rtbtimer) {
133 if (flags & XFS_QMOPT_DOWARN)
134 xfs_alert(mp,
135 "%s : Dquot ID 0x%x (0x%p) RTBLK TIMER NOT STARTED",
136 str, (int)be32_to_cpu(ddq->d_id), ddq);
137 errs++;
138 }
139 }
140 }
141
142 if (!errs || !(flags & XFS_QMOPT_DQREPAIR))
143 return errs;
144
145 if (flags & XFS_QMOPT_DOWARN)
146 xfs_notice(mp, "Re-initializing dquot ID 0x%x", id);
147
148 /*
149 * Typically, a repair is only requested by quotacheck.
150 */
151 ASSERT(id != -1);
152 ASSERT(flags & XFS_QMOPT_DQREPAIR);
153 memset(d, 0, sizeof(xfs_dqblk_t));
154
155 d->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
156 d->dd_diskdq.d_version = XFS_DQUOT_VERSION;
157 d->dd_diskdq.d_flags = type;
158 d->dd_diskdq.d_id = cpu_to_be32(id);
159
160 if (xfs_sb_version_hascrc(&mp->m_sb)) {
Eric Sandeence748ea2015-07-29 11:53:31 +1000161 uuid_copy(&d->dd_uuid, &mp->m_sb.sb_meta_uuid);
Dave Chinner9aede1d2013-10-15 09:17:52 +1100162 xfs_update_cksum((char *)d, sizeof(struct xfs_dqblk),
163 XFS_DQUOT_CRC_OFF);
164 }
165
166 return errs;
167}
168
169STATIC bool
170xfs_dquot_buf_verify_crc(
171 struct xfs_mount *mp,
172 struct xfs_buf *bp)
173{
174 struct xfs_dqblk *d = (struct xfs_dqblk *)bp->b_addr;
175 int ndquots;
176 int i;
177
178 if (!xfs_sb_version_hascrc(&mp->m_sb))
179 return true;
180
181 /*
182 * if we are in log recovery, the quota subsystem has not been
183 * initialised so we have no quotainfo structure. In that case, we need
184 * to manually calculate the number of dquots in the buffer.
185 */
186 if (mp->m_quotainfo)
187 ndquots = mp->m_quotainfo->qi_dqperchunk;
188 else
Darrick J. Wong58d78962016-10-20 15:46:18 +1100189 ndquots = xfs_calc_dquots_per_chunk(bp->b_length);
Dave Chinner9aede1d2013-10-15 09:17:52 +1100190
191 for (i = 0; i < ndquots; i++, d++) {
192 if (!xfs_verify_cksum((char *)d, sizeof(struct xfs_dqblk),
193 XFS_DQUOT_CRC_OFF))
194 return false;
Eric Sandeence748ea2015-07-29 11:53:31 +1000195 if (!uuid_equal(&d->dd_uuid, &mp->m_sb.sb_meta_uuid))
Dave Chinner9aede1d2013-10-15 09:17:52 +1100196 return false;
197 }
198 return true;
199}
200
201STATIC bool
202xfs_dquot_buf_verify(
203 struct xfs_mount *mp,
Dave Chinner7d6a13f2016-01-12 07:04:01 +1100204 struct xfs_buf *bp,
205 int warn)
Dave Chinner9aede1d2013-10-15 09:17:52 +1100206{
207 struct xfs_dqblk *d = (struct xfs_dqblk *)bp->b_addr;
208 xfs_dqid_t id = 0;
209 int ndquots;
210 int i;
211
212 /*
213 * if we are in log recovery, the quota subsystem has not been
214 * initialised so we have no quotainfo structure. In that case, we need
215 * to manually calculate the number of dquots in the buffer.
216 */
217 if (mp->m_quotainfo)
218 ndquots = mp->m_quotainfo->qi_dqperchunk;
219 else
Eric Sandeen6ea94bb2014-04-14 19:03:34 +1000220 ndquots = xfs_calc_dquots_per_chunk(bp->b_length);
Dave Chinner9aede1d2013-10-15 09:17:52 +1100221
222 /*
223 * On the first read of the buffer, verify that each dquot is valid.
224 * We don't know what the id of the dquot is supposed to be, just that
225 * they should be increasing monotonically within the buffer. If the
226 * first id is corrupt, then it will fail on the second dquot in the
227 * buffer so corruptions could point to the wrong dquot in this case.
228 */
229 for (i = 0; i < ndquots; i++) {
230 struct xfs_disk_dquot *ddq;
231 int error;
232
233 ddq = &d[i].dd_diskdq;
234
235 if (i == 0)
236 id = be32_to_cpu(ddq->d_id);
237
Dave Chinner7d6a13f2016-01-12 07:04:01 +1100238 error = xfs_dqcheck(mp, ddq, id + i, 0, warn, __func__);
Dave Chinner9aede1d2013-10-15 09:17:52 +1100239 if (error)
240 return false;
241 }
242 return true;
243}
244
245static void
246xfs_dquot_buf_read_verify(
247 struct xfs_buf *bp)
248{
249 struct xfs_mount *mp = bp->b_target->bt_mount;
250
Eric Sandeence5028c2014-02-27 15:23:10 +1100251 if (!xfs_dquot_buf_verify_crc(mp, bp))
Dave Chinner24513372014-06-25 14:58:08 +1000252 xfs_buf_ioerror(bp, -EFSBADCRC);
Dave Chinner7d6a13f2016-01-12 07:04:01 +1100253 else if (!xfs_dquot_buf_verify(mp, bp, XFS_QMOPT_DOWARN))
Dave Chinner24513372014-06-25 14:58:08 +1000254 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +1100255
256 if (bp->b_error)
257 xfs_verifier_error(bp);
Dave Chinner9aede1d2013-10-15 09:17:52 +1100258}
259
260/*
Dave Chinner7d6a13f2016-01-12 07:04:01 +1100261 * readahead errors are silent and simply leave the buffer as !done so a real
262 * read will then be run with the xfs_dquot_buf_ops verifier. See
263 * xfs_inode_buf_verify() for why we use EIO and ~XBF_DONE here rather than
264 * reporting the failure.
265 */
266static void
267xfs_dquot_buf_readahead_verify(
268 struct xfs_buf *bp)
269{
270 struct xfs_mount *mp = bp->b_target->bt_mount;
271
272 if (!xfs_dquot_buf_verify_crc(mp, bp) ||
273 !xfs_dquot_buf_verify(mp, bp, 0)) {
274 xfs_buf_ioerror(bp, -EIO);
275 bp->b_flags &= ~XBF_DONE;
276 }
277}
278
279/*
Dave Chinner9aede1d2013-10-15 09:17:52 +1100280 * we don't calculate the CRC here as that is done when the dquot is flushed to
281 * the buffer after the update is done. This ensures that the dquot in the
282 * buffer always has an up-to-date CRC value.
283 */
Dave Chinner632b89e2013-10-29 22:11:58 +1100284static void
Dave Chinner9aede1d2013-10-15 09:17:52 +1100285xfs_dquot_buf_write_verify(
286 struct xfs_buf *bp)
287{
288 struct xfs_mount *mp = bp->b_target->bt_mount;
289
Dave Chinner7d6a13f2016-01-12 07:04:01 +1100290 if (!xfs_dquot_buf_verify(mp, bp, XFS_QMOPT_DOWARN)) {
Dave Chinner24513372014-06-25 14:58:08 +1000291 xfs_buf_ioerror(bp, -EFSCORRUPTED);
Eric Sandeence5028c2014-02-27 15:23:10 +1100292 xfs_verifier_error(bp);
Dave Chinner9aede1d2013-10-15 09:17:52 +1100293 return;
294 }
295}
296
297const struct xfs_buf_ops xfs_dquot_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +1100298 .name = "xfs_dquot",
Dave Chinner9aede1d2013-10-15 09:17:52 +1100299 .verify_read = xfs_dquot_buf_read_verify,
300 .verify_write = xfs_dquot_buf_write_verify,
301};
302
Dave Chinner7d6a13f2016-01-12 07:04:01 +1100303const struct xfs_buf_ops xfs_dquot_buf_ra_ops = {
304 .name = "xfs_dquot_ra",
305 .verify_read = xfs_dquot_buf_readahead_verify,
306 .verify_write = xfs_dquot_buf_write_verify,
307};