xfs: merge xfs_ag.h into xfs_format.h
[deliverable/linux.git] / fs / xfs / libxfs / xfs_dquot_buf.c
CommitLineData
9aede1d8
DC
1/*
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"
632b89e8 21#include "xfs_shared.h"
9aede1d8 22#include "xfs_format.h"
239880ef
DC
23#include "xfs_log_format.h"
24#include "xfs_trans_resv.h"
9aede1d8 25#include "xfs_sb.h"
9aede1d8 26#include "xfs_mount.h"
9aede1d8
DC
27#include "xfs_inode.h"
28#include "xfs_quota.h"
239880ef 29#include "xfs_trans.h"
9aede1d8
DC
30#include "xfs_qm.h"
31#include "xfs_error.h"
32#include "xfs_cksum.h"
33#include "xfs_trace.h"
34
35int
36xfs_calc_dquots_per_chunk(
9aede1d8
DC
37 unsigned int nbblks) /* basic block units */
38{
39 unsigned int ndquots;
40
41 ASSERT(nbblks > 0);
42 ndquots = BBTOB(nbblks);
43 do_div(ndquots, sizeof(xfs_dqblk_t));
44
45 return ndquots;
46}
47
48/*
49 * Do some primitive error checking on ondisk dquot data structures.
50 */
51int
52xfs_dqcheck(
53 struct xfs_mount *mp,
54 xfs_disk_dquot_t *ddq,
55 xfs_dqid_t id,
56 uint type, /* used only when IO_dorepair is true */
57 uint flags,
58 char *str)
59{
60 xfs_dqblk_t *d = (xfs_dqblk_t *)ddq;
61 int errs = 0;
62
63 /*
64 * We can encounter an uninitialized dquot buffer for 2 reasons:
65 * 1. If we crash while deleting the quotainode(s), and those blks got
66 * used for user data. This is because we take the path of regular
67 * file deletion; however, the size field of quotainodes is never
68 * updated, so all the tricks that we play in itruncate_finish
69 * don't quite matter.
70 *
71 * 2. We don't play the quota buffers when there's a quotaoff logitem.
72 * But the allocation will be replayed so we'll end up with an
73 * uninitialized quota block.
74 *
75 * This is all fine; things are still consistent, and we haven't lost
76 * any quota information. Just don't complain about bad dquot blks.
77 */
78 if (ddq->d_magic != cpu_to_be16(XFS_DQUOT_MAGIC)) {
79 if (flags & XFS_QMOPT_DOWARN)
80 xfs_alert(mp,
81 "%s : XFS dquot ID 0x%x, magic 0x%x != 0x%x",
82 str, id, be16_to_cpu(ddq->d_magic), XFS_DQUOT_MAGIC);
83 errs++;
84 }
85 if (ddq->d_version != XFS_DQUOT_VERSION) {
86 if (flags & XFS_QMOPT_DOWARN)
87 xfs_alert(mp,
88 "%s : XFS dquot ID 0x%x, version 0x%x != 0x%x",
89 str, id, ddq->d_version, XFS_DQUOT_VERSION);
90 errs++;
91 }
92
93 if (ddq->d_flags != XFS_DQ_USER &&
94 ddq->d_flags != XFS_DQ_PROJ &&
95 ddq->d_flags != XFS_DQ_GROUP) {
96 if (flags & XFS_QMOPT_DOWARN)
97 xfs_alert(mp,
98 "%s : XFS dquot ID 0x%x, unknown flags 0x%x",
99 str, id, ddq->d_flags);
100 errs++;
101 }
102
103 if (id != -1 && id != be32_to_cpu(ddq->d_id)) {
104 if (flags & XFS_QMOPT_DOWARN)
105 xfs_alert(mp,
106 "%s : ondisk-dquot 0x%p, ID mismatch: "
107 "0x%x expected, found id 0x%x",
108 str, ddq, id, be32_to_cpu(ddq->d_id));
109 errs++;
110 }
111
112 if (!errs && ddq->d_id) {
113 if (ddq->d_blk_softlimit &&
114 be64_to_cpu(ddq->d_bcount) >
115 be64_to_cpu(ddq->d_blk_softlimit)) {
116 if (!ddq->d_btimer) {
117 if (flags & XFS_QMOPT_DOWARN)
118 xfs_alert(mp,
119 "%s : Dquot ID 0x%x (0x%p) BLK TIMER NOT STARTED",
120 str, (int)be32_to_cpu(ddq->d_id), ddq);
121 errs++;
122 }
123 }
124 if (ddq->d_ino_softlimit &&
125 be64_to_cpu(ddq->d_icount) >
126 be64_to_cpu(ddq->d_ino_softlimit)) {
127 if (!ddq->d_itimer) {
128 if (flags & XFS_QMOPT_DOWARN)
129 xfs_alert(mp,
130 "%s : Dquot ID 0x%x (0x%p) INODE TIMER NOT STARTED",
131 str, (int)be32_to_cpu(ddq->d_id), ddq);
132 errs++;
133 }
134 }
135 if (ddq->d_rtb_softlimit &&
136 be64_to_cpu(ddq->d_rtbcount) >
137 be64_to_cpu(ddq->d_rtb_softlimit)) {
138 if (!ddq->d_rtbtimer) {
139 if (flags & XFS_QMOPT_DOWARN)
140 xfs_alert(mp,
141 "%s : Dquot ID 0x%x (0x%p) RTBLK TIMER NOT STARTED",
142 str, (int)be32_to_cpu(ddq->d_id), ddq);
143 errs++;
144 }
145 }
146 }
147
148 if (!errs || !(flags & XFS_QMOPT_DQREPAIR))
149 return errs;
150
151 if (flags & XFS_QMOPT_DOWARN)
152 xfs_notice(mp, "Re-initializing dquot ID 0x%x", id);
153
154 /*
155 * Typically, a repair is only requested by quotacheck.
156 */
157 ASSERT(id != -1);
158 ASSERT(flags & XFS_QMOPT_DQREPAIR);
159 memset(d, 0, sizeof(xfs_dqblk_t));
160
161 d->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
162 d->dd_diskdq.d_version = XFS_DQUOT_VERSION;
163 d->dd_diskdq.d_flags = type;
164 d->dd_diskdq.d_id = cpu_to_be32(id);
165
166 if (xfs_sb_version_hascrc(&mp->m_sb)) {
167 uuid_copy(&d->dd_uuid, &mp->m_sb.sb_uuid);
168 xfs_update_cksum((char *)d, sizeof(struct xfs_dqblk),
169 XFS_DQUOT_CRC_OFF);
170 }
171
172 return errs;
173}
174
175STATIC bool
176xfs_dquot_buf_verify_crc(
177 struct xfs_mount *mp,
178 struct xfs_buf *bp)
179{
180 struct xfs_dqblk *d = (struct xfs_dqblk *)bp->b_addr;
181 int ndquots;
182 int i;
183
184 if (!xfs_sb_version_hascrc(&mp->m_sb))
185 return true;
186
187 /*
188 * if we are in log recovery, the quota subsystem has not been
189 * initialised so we have no quotainfo structure. In that case, we need
190 * to manually calculate the number of dquots in the buffer.
191 */
192 if (mp->m_quotainfo)
193 ndquots = mp->m_quotainfo->qi_dqperchunk;
194 else
6ea94bb5 195 ndquots = xfs_calc_dquots_per_chunk(
9aede1d8
DC
196 XFS_BB_TO_FSB(mp, bp->b_length));
197
198 for (i = 0; i < ndquots; i++, d++) {
199 if (!xfs_verify_cksum((char *)d, sizeof(struct xfs_dqblk),
200 XFS_DQUOT_CRC_OFF))
201 return false;
202 if (!uuid_equal(&d->dd_uuid, &mp->m_sb.sb_uuid))
203 return false;
204 }
205 return true;
206}
207
208STATIC bool
209xfs_dquot_buf_verify(
210 struct xfs_mount *mp,
211 struct xfs_buf *bp)
212{
213 struct xfs_dqblk *d = (struct xfs_dqblk *)bp->b_addr;
214 xfs_dqid_t id = 0;
215 int ndquots;
216 int i;
217
218 /*
219 * if we are in log recovery, the quota subsystem has not been
220 * initialised so we have no quotainfo structure. In that case, we need
221 * to manually calculate the number of dquots in the buffer.
222 */
223 if (mp->m_quotainfo)
224 ndquots = mp->m_quotainfo->qi_dqperchunk;
225 else
6ea94bb5 226 ndquots = xfs_calc_dquots_per_chunk(bp->b_length);
9aede1d8
DC
227
228 /*
229 * On the first read of the buffer, verify that each dquot is valid.
230 * We don't know what the id of the dquot is supposed to be, just that
231 * they should be increasing monotonically within the buffer. If the
232 * first id is corrupt, then it will fail on the second dquot in the
233 * buffer so corruptions could point to the wrong dquot in this case.
234 */
235 for (i = 0; i < ndquots; i++) {
236 struct xfs_disk_dquot *ddq;
237 int error;
238
239 ddq = &d[i].dd_diskdq;
240
241 if (i == 0)
242 id = be32_to_cpu(ddq->d_id);
243
244 error = xfs_dqcheck(mp, ddq, id + i, 0, XFS_QMOPT_DOWARN,
245 "xfs_dquot_buf_verify");
246 if (error)
247 return false;
248 }
249 return true;
250}
251
252static void
253xfs_dquot_buf_read_verify(
254 struct xfs_buf *bp)
255{
256 struct xfs_mount *mp = bp->b_target->bt_mount;
257
ce5028cf 258 if (!xfs_dquot_buf_verify_crc(mp, bp))
2451337d 259 xfs_buf_ioerror(bp, -EFSBADCRC);
ce5028cf 260 else if (!xfs_dquot_buf_verify(mp, bp))
2451337d 261 xfs_buf_ioerror(bp, -EFSCORRUPTED);
ce5028cf
ES
262
263 if (bp->b_error)
264 xfs_verifier_error(bp);
9aede1d8
DC
265}
266
267/*
268 * we don't calculate the CRC here as that is done when the dquot is flushed to
269 * the buffer after the update is done. This ensures that the dquot in the
270 * buffer always has an up-to-date CRC value.
271 */
632b89e8 272static void
9aede1d8
DC
273xfs_dquot_buf_write_verify(
274 struct xfs_buf *bp)
275{
276 struct xfs_mount *mp = bp->b_target->bt_mount;
277
278 if (!xfs_dquot_buf_verify(mp, bp)) {
2451337d 279 xfs_buf_ioerror(bp, -EFSCORRUPTED);
ce5028cf 280 xfs_verifier_error(bp);
9aede1d8
DC
281 return;
282 }
283}
284
285const struct xfs_buf_ops xfs_dquot_buf_ops = {
286 .verify_read = xfs_dquot_buf_read_verify,
287 .verify_write = xfs_dquot_buf_write_verify,
288};
289
This page took 0.198197 seconds and 5 git commands to generate.