blob: 6b15c50052668e71ddff0f8f136cf426dfa168bc [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{
Dave Chinner9aede1d2013-10-15 09:17:52 +110054 int errs = 0;
55
56 /*
57 * We can encounter an uninitialized dquot buffer for 2 reasons:
58 * 1. If we crash while deleting the quotainode(s), and those blks got
59 * used for user data. This is because we take the path of regular
60 * file deletion; however, the size field of quotainodes is never
61 * updated, so all the tricks that we play in itruncate_finish
62 * don't quite matter.
63 *
64 * 2. We don't play the quota buffers when there's a quotaoff logitem.
65 * But the allocation will be replayed so we'll end up with an
66 * uninitialized quota block.
67 *
68 * This is all fine; things are still consistent, and we haven't lost
69 * any quota information. Just don't complain about bad dquot blks.
70 */
71 if (ddq->d_magic != cpu_to_be16(XFS_DQUOT_MAGIC)) {
72 if (flags & XFS_QMOPT_DOWARN)
73 xfs_alert(mp,
74 "%s : XFS dquot ID 0x%x, magic 0x%x != 0x%x",
75 str, id, be16_to_cpu(ddq->d_magic), XFS_DQUOT_MAGIC);
76 errs++;
77 }
78 if (ddq->d_version != XFS_DQUOT_VERSION) {
79 if (flags & XFS_QMOPT_DOWARN)
80 xfs_alert(mp,
81 "%s : XFS dquot ID 0x%x, version 0x%x != 0x%x",
82 str, id, ddq->d_version, XFS_DQUOT_VERSION);
83 errs++;
84 }
85
86 if (ddq->d_flags != XFS_DQ_USER &&
87 ddq->d_flags != XFS_DQ_PROJ &&
88 ddq->d_flags != XFS_DQ_GROUP) {
89 if (flags & XFS_QMOPT_DOWARN)
90 xfs_alert(mp,
91 "%s : XFS dquot ID 0x%x, unknown flags 0x%x",
92 str, id, ddq->d_flags);
93 errs++;
94 }
95
96 if (id != -1 && id != be32_to_cpu(ddq->d_id)) {
97 if (flags & XFS_QMOPT_DOWARN)
98 xfs_alert(mp,
99 "%s : ondisk-dquot 0x%p, ID mismatch: "
100 "0x%x expected, found id 0x%x",
101 str, ddq, id, be32_to_cpu(ddq->d_id));
102 errs++;
103 }
104
105 if (!errs && ddq->d_id) {
106 if (ddq->d_blk_softlimit &&
107 be64_to_cpu(ddq->d_bcount) >
108 be64_to_cpu(ddq->d_blk_softlimit)) {
109 if (!ddq->d_btimer) {
110 if (flags & XFS_QMOPT_DOWARN)
111 xfs_alert(mp,
112 "%s : Dquot ID 0x%x (0x%p) BLK TIMER NOT STARTED",
113 str, (int)be32_to_cpu(ddq->d_id), ddq);
114 errs++;
115 }
116 }
117 if (ddq->d_ino_softlimit &&
118 be64_to_cpu(ddq->d_icount) >
119 be64_to_cpu(ddq->d_ino_softlimit)) {
120 if (!ddq->d_itimer) {
121 if (flags & XFS_QMOPT_DOWARN)
122 xfs_alert(mp,
123 "%s : Dquot ID 0x%x (0x%p) INODE TIMER NOT STARTED",
124 str, (int)be32_to_cpu(ddq->d_id), ddq);
125 errs++;
126 }
127 }
128 if (ddq->d_rtb_softlimit &&
129 be64_to_cpu(ddq->d_rtbcount) >
130 be64_to_cpu(ddq->d_rtb_softlimit)) {
131 if (!ddq->d_rtbtimer) {
132 if (flags & XFS_QMOPT_DOWARN)
133 xfs_alert(mp,
134 "%s : Dquot ID 0x%x (0x%p) RTBLK TIMER NOT STARTED",
135 str, (int)be32_to_cpu(ddq->d_id), ddq);
136 errs++;
137 }
138 }
139 }
140
Darrick J. Wongeeea7982018-01-08 10:51:24 -0800141 return errs;
142}
Dave Chinner9aede1d2013-10-15 09:17:52 +1100143
Darrick J. Wongeeea7982018-01-08 10:51:24 -0800144/*
145 * Do some primitive error checking on ondisk dquot data structures.
146 */
147int
148xfs_dquot_repair(
149 struct xfs_mount *mp,
150 struct xfs_disk_dquot *ddq,
151 xfs_dqid_t id,
152 uint type)
153{
154 struct xfs_dqblk *d = (struct xfs_dqblk *)ddq;
155
Dave Chinner9aede1d2013-10-15 09:17:52 +1100156
157 /*
158 * Typically, a repair is only requested by quotacheck.
159 */
160 ASSERT(id != -1);
Dave Chinner9aede1d2013-10-15 09:17:52 +1100161 memset(d, 0, sizeof(xfs_dqblk_t));
162
163 d->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
164 d->dd_diskdq.d_version = XFS_DQUOT_VERSION;
165 d->dd_diskdq.d_flags = type;
166 d->dd_diskdq.d_id = cpu_to_be32(id);
167
168 if (xfs_sb_version_hascrc(&mp->m_sb)) {
Eric Sandeence748ea2015-07-29 11:53:31 +1000169 uuid_copy(&d->dd_uuid, &mp->m_sb.sb_meta_uuid);
Dave Chinner9aede1d2013-10-15 09:17:52 +1100170 xfs_update_cksum((char *)d, sizeof(struct xfs_dqblk),
171 XFS_DQUOT_CRC_OFF);
172 }
173
Darrick J. Wongeeea7982018-01-08 10:51:24 -0800174 return 0;
Dave Chinner9aede1d2013-10-15 09:17:52 +1100175}
176
177STATIC bool
178xfs_dquot_buf_verify_crc(
179 struct xfs_mount *mp,
180 struct xfs_buf *bp)
181{
182 struct xfs_dqblk *d = (struct xfs_dqblk *)bp->b_addr;
183 int ndquots;
184 int i;
185
186 if (!xfs_sb_version_hascrc(&mp->m_sb))
187 return true;
188
189 /*
190 * if we are in log recovery, the quota subsystem has not been
191 * initialised so we have no quotainfo structure. In that case, we need
192 * to manually calculate the number of dquots in the buffer.
193 */
194 if (mp->m_quotainfo)
195 ndquots = mp->m_quotainfo->qi_dqperchunk;
196 else
Darrick J. Wong58d78962016-10-20 15:46:18 +1100197 ndquots = xfs_calc_dquots_per_chunk(bp->b_length);
Dave Chinner9aede1d2013-10-15 09:17:52 +1100198
199 for (i = 0; i < ndquots; i++, d++) {
200 if (!xfs_verify_cksum((char *)d, sizeof(struct xfs_dqblk),
201 XFS_DQUOT_CRC_OFF))
202 return false;
Eric Sandeence748ea2015-07-29 11:53:31 +1000203 if (!uuid_equal(&d->dd_uuid, &mp->m_sb.sb_meta_uuid))
Dave Chinner9aede1d2013-10-15 09:17:52 +1100204 return false;
205 }
206 return true;
207}
208
209STATIC bool
210xfs_dquot_buf_verify(
211 struct xfs_mount *mp,
Dave Chinner7d6a13f2016-01-12 07:04:01 +1100212 struct xfs_buf *bp,
213 int warn)
Dave Chinner9aede1d2013-10-15 09:17:52 +1100214{
215 struct xfs_dqblk *d = (struct xfs_dqblk *)bp->b_addr;
216 xfs_dqid_t id = 0;
217 int ndquots;
218 int i;
219
220 /*
221 * if we are in log recovery, the quota subsystem has not been
222 * initialised so we have no quotainfo structure. In that case, we need
223 * to manually calculate the number of dquots in the buffer.
224 */
225 if (mp->m_quotainfo)
226 ndquots = mp->m_quotainfo->qi_dqperchunk;
227 else
Eric Sandeen6ea94bb2014-04-14 19:03:34 +1000228 ndquots = xfs_calc_dquots_per_chunk(bp->b_length);
Dave Chinner9aede1d2013-10-15 09:17:52 +1100229
230 /*
231 * On the first read of the buffer, verify that each dquot is valid.
232 * We don't know what the id of the dquot is supposed to be, just that
233 * they should be increasing monotonically within the buffer. If the
234 * first id is corrupt, then it will fail on the second dquot in the
235 * buffer so corruptions could point to the wrong dquot in this case.
236 */
237 for (i = 0; i < ndquots; i++) {
238 struct xfs_disk_dquot *ddq;
239 int error;
240
241 ddq = &d[i].dd_diskdq;
242
243 if (i == 0)
244 id = be32_to_cpu(ddq->d_id);
245
Dave Chinner7d6a13f2016-01-12 07:04:01 +1100246 error = xfs_dqcheck(mp, ddq, id + i, 0, warn, __func__);
Dave Chinner9aede1d2013-10-15 09:17:52 +1100247 if (error)
248 return false;
249 }
250 return true;
251}
252
Darrick J. Wongb5572592018-01-08 10:51:08 -0800253static xfs_failaddr_t
254xfs_dquot_buf_verify_struct(
255 struct xfs_buf *bp)
256{
257 struct xfs_mount *mp = bp->b_target->bt_mount;
258
259 if (!xfs_dquot_buf_verify(mp, bp, 0))
260 return __this_address;
261 return NULL;
262}
263
Dave Chinner9aede1d2013-10-15 09:17:52 +1100264static void
265xfs_dquot_buf_read_verify(
266 struct xfs_buf *bp)
267{
268 struct xfs_mount *mp = bp->b_target->bt_mount;
269
Eric Sandeence5028c2014-02-27 15:23:10 +1100270 if (!xfs_dquot_buf_verify_crc(mp, bp))
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800271 xfs_verifier_error(bp, -EFSBADCRC, __this_address);
Dave Chinner7d6a13f2016-01-12 07:04:01 +1100272 else if (!xfs_dquot_buf_verify(mp, bp, XFS_QMOPT_DOWARN))
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800273 xfs_verifier_error(bp, -EFSCORRUPTED, __this_address);
Dave Chinner9aede1d2013-10-15 09:17:52 +1100274}
275
276/*
Dave Chinner7d6a13f2016-01-12 07:04:01 +1100277 * readahead errors are silent and simply leave the buffer as !done so a real
278 * read will then be run with the xfs_dquot_buf_ops verifier. See
279 * xfs_inode_buf_verify() for why we use EIO and ~XBF_DONE here rather than
280 * reporting the failure.
281 */
282static void
283xfs_dquot_buf_readahead_verify(
284 struct xfs_buf *bp)
285{
286 struct xfs_mount *mp = bp->b_target->bt_mount;
287
288 if (!xfs_dquot_buf_verify_crc(mp, bp) ||
289 !xfs_dquot_buf_verify(mp, bp, 0)) {
290 xfs_buf_ioerror(bp, -EIO);
291 bp->b_flags &= ~XBF_DONE;
292 }
293}
294
295/*
Dave Chinner9aede1d2013-10-15 09:17:52 +1100296 * we don't calculate the CRC here as that is done when the dquot is flushed to
297 * the buffer after the update is done. This ensures that the dquot in the
298 * buffer always has an up-to-date CRC value.
299 */
Dave Chinner632b89e2013-10-29 22:11:58 +1100300static void
Dave Chinner9aede1d2013-10-15 09:17:52 +1100301xfs_dquot_buf_write_verify(
302 struct xfs_buf *bp)
303{
304 struct xfs_mount *mp = bp->b_target->bt_mount;
305
Dave Chinner7d6a13f2016-01-12 07:04:01 +1100306 if (!xfs_dquot_buf_verify(mp, bp, XFS_QMOPT_DOWARN)) {
Darrick J. Wongbc1a09b2018-01-08 10:51:03 -0800307 xfs_verifier_error(bp, -EFSCORRUPTED, __this_address);
Dave Chinner9aede1d2013-10-15 09:17:52 +1100308 return;
309 }
310}
311
312const struct xfs_buf_ops xfs_dquot_buf_ops = {
Eric Sandeen233135b2016-01-04 16:10:19 +1100313 .name = "xfs_dquot",
Dave Chinner9aede1d2013-10-15 09:17:52 +1100314 .verify_read = xfs_dquot_buf_read_verify,
315 .verify_write = xfs_dquot_buf_write_verify,
Darrick J. Wongb5572592018-01-08 10:51:08 -0800316 .verify_struct = xfs_dquot_buf_verify_struct,
Dave Chinner9aede1d2013-10-15 09:17:52 +1100317};
318
Dave Chinner7d6a13f2016-01-12 07:04:01 +1100319const struct xfs_buf_ops xfs_dquot_buf_ra_ops = {
320 .name = "xfs_dquot_ra",
321 .verify_read = xfs_dquot_buf_readahead_verify,
322 .verify_write = xfs_dquot_buf_write_verify,
323};