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