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