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