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