1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
4 * Copyright (c) 2013 Red Hat, Inc.
5 * All Rights Reserved.
6 */
7 #include "xfs_platform.h"
8 #include "xfs_fs.h"
9 #include "xfs_shared.h"
10 #include "xfs_format.h"
11 #include "xfs_log_format.h"
12 #include "xfs_trans_resv.h"
13 #include "xfs_mount.h"
14 #include "xfs_inode.h"
15 #include "xfs_quota.h"
16 #include "xfs_trans.h"
17 #include "xfs_qm.h"
18 #include "xfs_error.h"
19 #include "xfs_health.h"
20 #include "xfs_metadir.h"
21 #include "xfs_metafile.h"
22
23 int
xfs_calc_dquots_per_chunk(unsigned int nbblks)24 xfs_calc_dquots_per_chunk(
25 unsigned int nbblks) /* basic block units */
26 {
27 ASSERT(nbblks > 0);
28 return BBTOB(nbblks) / sizeof(struct xfs_dqblk);
29 }
30
31 /*
32 * Do some primitive error checking on ondisk dquot data structures.
33 *
34 * The xfs_dqblk structure /contains/ the xfs_disk_dquot structure;
35 * we verify them separately because at some points we have only the
36 * smaller xfs_disk_dquot structure available.
37 */
38
39 xfs_failaddr_t
xfs_dquot_verify(struct xfs_mount * mp,struct xfs_disk_dquot * ddq,xfs_dqid_t id)40 xfs_dquot_verify(
41 struct xfs_mount *mp,
42 struct xfs_disk_dquot *ddq,
43 xfs_dqid_t id) /* used only during quotacheck */
44 {
45 __u8 ddq_type;
46
47 /*
48 * We can encounter an uninitialized dquot buffer for 2 reasons:
49 * 1. If we crash while deleting the quotainode(s), and those blks got
50 * used for user data. This is because we take the path of regular
51 * file deletion; however, the size field of quotainodes is never
52 * updated, so all the tricks that we play in itruncate_finish
53 * don't quite matter.
54 *
55 * 2. We don't play the quota buffers when there's a quotaoff logitem.
56 * But the allocation will be replayed so we'll end up with an
57 * uninitialized quota block.
58 *
59 * This is all fine; things are still consistent, and we haven't lost
60 * any quota information. Just don't complain about bad dquot blks.
61 */
62 if (ddq->d_magic != cpu_to_be16(XFS_DQUOT_MAGIC))
63 return __this_address;
64 if (ddq->d_version != XFS_DQUOT_VERSION)
65 return __this_address;
66
67 if (ddq->d_type & ~XFS_DQTYPE_ANY)
68 return __this_address;
69 ddq_type = ddq->d_type & XFS_DQTYPE_REC_MASK;
70 if (ddq_type != XFS_DQTYPE_USER &&
71 ddq_type != XFS_DQTYPE_PROJ &&
72 ddq_type != XFS_DQTYPE_GROUP)
73 return __this_address;
74
75 if ((ddq->d_type & XFS_DQTYPE_BIGTIME) &&
76 !xfs_has_bigtime(mp))
77 return __this_address;
78
79 if ((ddq->d_type & XFS_DQTYPE_BIGTIME) && !ddq->d_id)
80 return __this_address;
81
82 if (id != -1 && id != be32_to_cpu(ddq->d_id))
83 return __this_address;
84
85 if (!ddq->d_id)
86 return NULL;
87
88 if (ddq->d_blk_softlimit &&
89 be64_to_cpu(ddq->d_bcount) > be64_to_cpu(ddq->d_blk_softlimit) &&
90 !ddq->d_btimer)
91 return __this_address;
92
93 if (ddq->d_ino_softlimit &&
94 be64_to_cpu(ddq->d_icount) > be64_to_cpu(ddq->d_ino_softlimit) &&
95 !ddq->d_itimer)
96 return __this_address;
97
98 if (ddq->d_rtb_softlimit &&
99 be64_to_cpu(ddq->d_rtbcount) > be64_to_cpu(ddq->d_rtb_softlimit) &&
100 !ddq->d_rtbtimer)
101 return __this_address;
102
103 return NULL;
104 }
105
106 xfs_failaddr_t
xfs_dqblk_verify(struct xfs_mount * mp,struct xfs_dqblk * dqb,xfs_dqid_t id)107 xfs_dqblk_verify(
108 struct xfs_mount *mp,
109 struct xfs_dqblk *dqb,
110 xfs_dqid_t id) /* used only during quotacheck */
111 {
112 if (xfs_has_crc(mp) &&
113 !uuid_equal(&dqb->dd_uuid, &mp->m_sb.sb_meta_uuid))
114 return __this_address;
115
116 return xfs_dquot_verify(mp, &dqb->dd_diskdq, id);
117 }
118
119 /*
120 * Do some primitive error checking on ondisk dquot data structures.
121 */
122 void
xfs_dqblk_repair(struct xfs_mount * mp,struct xfs_dqblk * dqb,xfs_dqid_t id,xfs_dqtype_t type)123 xfs_dqblk_repair(
124 struct xfs_mount *mp,
125 struct xfs_dqblk *dqb,
126 xfs_dqid_t id,
127 xfs_dqtype_t type)
128 {
129 /*
130 * Typically, a repair is only requested by quotacheck.
131 */
132 ASSERT(id != -1);
133 memset(dqb, 0, sizeof(struct xfs_dqblk));
134
135 dqb->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
136 dqb->dd_diskdq.d_version = XFS_DQUOT_VERSION;
137 dqb->dd_diskdq.d_type = type;
138 dqb->dd_diskdq.d_id = cpu_to_be32(id);
139
140 if (xfs_has_crc(mp)) {
141 uuid_copy(&dqb->dd_uuid, &mp->m_sb.sb_meta_uuid);
142 xfs_update_cksum((char *)dqb, sizeof(struct xfs_dqblk),
143 XFS_DQUOT_CRC_OFF);
144 }
145 }
146
147 STATIC bool
xfs_dquot_buf_verify_crc(struct xfs_mount * mp,struct xfs_buf * bp,bool readahead)148 xfs_dquot_buf_verify_crc(
149 struct xfs_mount *mp,
150 struct xfs_buf *bp,
151 bool readahead)
152 {
153 struct xfs_dqblk *d = (struct xfs_dqblk *)bp->b_addr;
154 int ndquots;
155 int i;
156
157 if (!xfs_has_crc(mp))
158 return true;
159
160 /*
161 * if we are in log recovery, the quota subsystem has not been
162 * initialised so we have no quotainfo structure. In that case, we need
163 * to manually calculate the number of dquots in the buffer.
164 */
165 if (mp->m_quotainfo)
166 ndquots = mp->m_quotainfo->qi_dqperchunk;
167 else
168 ndquots = xfs_calc_dquots_per_chunk(bp->b_length);
169
170 for (i = 0; i < ndquots; i++, d++) {
171 if (!xfs_verify_cksum((char *)d, sizeof(struct xfs_dqblk),
172 XFS_DQUOT_CRC_OFF)) {
173 if (!readahead)
174 xfs_buf_verifier_error(bp, -EFSBADCRC, __func__,
175 d, sizeof(*d), __this_address);
176 return false;
177 }
178 }
179 return true;
180 }
181
182 STATIC xfs_failaddr_t
xfs_dquot_buf_verify(struct xfs_mount * mp,struct xfs_buf * bp,bool readahead)183 xfs_dquot_buf_verify(
184 struct xfs_mount *mp,
185 struct xfs_buf *bp,
186 bool readahead)
187 {
188 struct xfs_dqblk *dqb = bp->b_addr;
189 xfs_failaddr_t fa;
190 xfs_dqid_t id = 0;
191 int ndquots;
192 int i;
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
204 /*
205 * On the first read of the buffer, verify that each dquot is valid.
206 * We don't know what the id of the dquot is supposed to be, just that
207 * they should be increasing monotonically within the buffer. If the
208 * first id is corrupt, then it will fail on the second dquot in the
209 * buffer so corruptions could point to the wrong dquot in this case.
210 */
211 for (i = 0; i < ndquots; i++) {
212 struct xfs_disk_dquot *ddq;
213
214 ddq = &dqb[i].dd_diskdq;
215
216 if (i == 0)
217 id = be32_to_cpu(ddq->d_id);
218
219 fa = xfs_dqblk_verify(mp, &dqb[i], id + i);
220 if (fa) {
221 if (!readahead)
222 xfs_buf_verifier_error(bp, -EFSCORRUPTED,
223 __func__, &dqb[i],
224 sizeof(struct xfs_dqblk), fa);
225 return fa;
226 }
227 }
228
229 return NULL;
230 }
231
232 static xfs_failaddr_t
xfs_dquot_buf_verify_struct(struct xfs_buf * bp)233 xfs_dquot_buf_verify_struct(
234 struct xfs_buf *bp)
235 {
236 struct xfs_mount *mp = bp->b_mount;
237
238 return xfs_dquot_buf_verify(mp, bp, false);
239 }
240
241 static void
xfs_dquot_buf_read_verify(struct xfs_buf * bp)242 xfs_dquot_buf_read_verify(
243 struct xfs_buf *bp)
244 {
245 struct xfs_mount *mp = bp->b_mount;
246
247 if (!xfs_dquot_buf_verify_crc(mp, bp, false))
248 return;
249 xfs_dquot_buf_verify(mp, bp, false);
250 }
251
252 /*
253 * readahead errors are silent and simply leave the buffer as !done so a real
254 * read will then be run with the xfs_dquot_buf_ops verifier. See
255 * xfs_inode_buf_verify() for why we use EIO here rather than reporting the
256 * failure.
257 */
258 static void
xfs_dquot_buf_readahead_verify(struct xfs_buf * bp)259 xfs_dquot_buf_readahead_verify(
260 struct xfs_buf *bp)
261 {
262 struct xfs_mount *mp = bp->b_mount;
263
264 if (!xfs_dquot_buf_verify_crc(mp, bp, true) ||
265 xfs_dquot_buf_verify(mp, bp, true) != NULL)
266 xfs_buf_ioerror(bp, -EIO);
267 }
268
269 /*
270 * we don't calculate the CRC here as that is done when the dquot is flushed to
271 * the buffer after the update is done. This ensures that the dquot in the
272 * buffer always has an up-to-date CRC value.
273 */
274 static void
xfs_dquot_buf_write_verify(struct xfs_buf * bp)275 xfs_dquot_buf_write_verify(
276 struct xfs_buf *bp)
277 {
278 struct xfs_mount *mp = bp->b_mount;
279
280 xfs_dquot_buf_verify(mp, bp, false);
281 }
282
283 const struct xfs_buf_ops xfs_dquot_buf_ops = {
284 .name = "xfs_dquot",
285 .magic16 = { cpu_to_be16(XFS_DQUOT_MAGIC),
286 cpu_to_be16(XFS_DQUOT_MAGIC) },
287 .verify_read = xfs_dquot_buf_read_verify,
288 .verify_write = xfs_dquot_buf_write_verify,
289 .verify_struct = xfs_dquot_buf_verify_struct,
290 };
291
292 const struct xfs_buf_ops xfs_dquot_buf_ra_ops = {
293 .name = "xfs_dquot_ra",
294 .magic16 = { cpu_to_be16(XFS_DQUOT_MAGIC),
295 cpu_to_be16(XFS_DQUOT_MAGIC) },
296 .verify_read = xfs_dquot_buf_readahead_verify,
297 .verify_write = xfs_dquot_buf_write_verify,
298 };
299
300 /* Convert an on-disk timer value into an incore timer value. */
301 time64_t
xfs_dquot_from_disk_ts(struct xfs_disk_dquot * ddq,__be32 dtimer)302 xfs_dquot_from_disk_ts(
303 struct xfs_disk_dquot *ddq,
304 __be32 dtimer)
305 {
306 uint32_t t = be32_to_cpu(dtimer);
307
308 if (t != 0 && (ddq->d_type & XFS_DQTYPE_BIGTIME))
309 return xfs_dq_bigtime_to_unix(t);
310
311 return t;
312 }
313
314 /* Convert an incore timer value into an on-disk timer value. */
315 __be32
xfs_dquot_to_disk_ts(struct xfs_dquot * dqp,time64_t timer)316 xfs_dquot_to_disk_ts(
317 struct xfs_dquot *dqp,
318 time64_t timer)
319 {
320 uint32_t t = timer;
321
322 if (timer != 0 && (dqp->q_type & XFS_DQTYPE_BIGTIME))
323 t = xfs_dq_unix_to_bigtime(timer);
324
325 return cpu_to_be32(t);
326 }
327
328 inline unsigned int
xfs_dqinode_sick_mask(xfs_dqtype_t type)329 xfs_dqinode_sick_mask(xfs_dqtype_t type)
330 {
331 switch (type) {
332 case XFS_DQTYPE_USER:
333 return XFS_SICK_FS_UQUOTA;
334 case XFS_DQTYPE_GROUP:
335 return XFS_SICK_FS_GQUOTA;
336 case XFS_DQTYPE_PROJ:
337 return XFS_SICK_FS_PQUOTA;
338 }
339
340 ASSERT(0);
341 return 0;
342 }
343
344 /*
345 * Load the inode for a given type of quota, assuming that the sb fields have
346 * been sorted out. This is not true when switching quota types on a V4
347 * filesystem, so do not use this function for that. If metadir is enabled,
348 * @dp must be the /quota metadir.
349 *
350 * Returns -ENOENT if the quota inode field is NULLFSINO; 0 and an inode on
351 * success; or a negative errno.
352 */
353 int
xfs_dqinode_load(struct xfs_trans * tp,struct xfs_inode * dp,xfs_dqtype_t type,struct xfs_inode ** ipp)354 xfs_dqinode_load(
355 struct xfs_trans *tp,
356 struct xfs_inode *dp,
357 xfs_dqtype_t type,
358 struct xfs_inode **ipp)
359 {
360 struct xfs_mount *mp = tp->t_mountp;
361 struct xfs_inode *ip;
362 enum xfs_metafile_type metafile_type = xfs_dqinode_metafile_type(type);
363 int error;
364
365 if (!xfs_has_metadir(mp)) {
366 xfs_ino_t ino;
367
368 switch (type) {
369 case XFS_DQTYPE_USER:
370 ino = mp->m_sb.sb_uquotino;
371 break;
372 case XFS_DQTYPE_GROUP:
373 ino = mp->m_sb.sb_gquotino;
374 break;
375 case XFS_DQTYPE_PROJ:
376 ino = mp->m_sb.sb_pquotino;
377 break;
378 default:
379 ASSERT(0);
380 return -EFSCORRUPTED;
381 }
382
383 /* Should have set 0 to NULLFSINO when loading superblock */
384 if (ino == NULLFSINO)
385 return -ENOENT;
386
387 error = xfs_trans_metafile_iget(tp, ino, metafile_type, &ip);
388 } else {
389 error = xfs_metadir_load(tp, dp, xfs_dqinode_path(type),
390 metafile_type, &ip);
391 if (error == -ENOENT)
392 return error;
393 }
394 if (error) {
395 if (xfs_metadata_is_sick(error))
396 xfs_fs_mark_sick(mp, xfs_dqinode_sick_mask(type));
397 return error;
398 }
399
400 if (XFS_IS_CORRUPT(mp, ip->i_df.if_format != XFS_DINODE_FMT_EXTENTS &&
401 ip->i_df.if_format != XFS_DINODE_FMT_BTREE)) {
402 xfs_irele(ip);
403 xfs_fs_mark_sick(mp, xfs_dqinode_sick_mask(type));
404 return -EFSCORRUPTED;
405 }
406
407 if (XFS_IS_CORRUPT(mp, ip->i_projid != 0)) {
408 xfs_irele(ip);
409 xfs_fs_mark_sick(mp, xfs_dqinode_sick_mask(type));
410 return -EFSCORRUPTED;
411 }
412
413 *ipp = ip;
414 return 0;
415 }
416
417 static int
xfs_dqinode_init(struct xfs_metadir_update * upd,void * priv)418 xfs_dqinode_init(
419 struct xfs_metadir_update *upd,
420 void *priv)
421 {
422 xfs_trans_log_inode(upd->tp, upd->ip, XFS_ILOG_CORE);
423 return 0;
424 }
425
426 /* Create a metadata directory quota inode. */
427 int
xfs_dqinode_metadir_create(struct xfs_inode * dp,xfs_dqtype_t type,struct xfs_inode ** ipp)428 xfs_dqinode_metadir_create(
429 struct xfs_inode *dp,
430 xfs_dqtype_t type,
431 struct xfs_inode **ipp)
432 {
433 struct xfs_metadir_update upd = {
434 .dp = dp,
435 .metafile_type = xfs_dqinode_metafile_type(type),
436 .path = xfs_dqinode_path(type),
437 };
438
439 return xfs_metadir_create_file(&upd, S_IFREG, xfs_dqinode_init, NULL,
440 ipp);
441 }
442
443 #ifndef __KERNEL__
444 /* Link a metadata directory quota inode. */
445 int
xfs_dqinode_metadir_link(struct xfs_inode * dp,xfs_dqtype_t type,struct xfs_inode * ip)446 xfs_dqinode_metadir_link(
447 struct xfs_inode *dp,
448 xfs_dqtype_t type,
449 struct xfs_inode *ip)
450 {
451 struct xfs_metadir_update upd = {
452 .dp = dp,
453 .metafile_type = xfs_dqinode_metafile_type(type),
454 .path = xfs_dqinode_path(type),
455 .ip = ip,
456 };
457 int error;
458
459 error = xfs_metadir_start_link(&upd);
460 if (error)
461 return error;
462
463 error = xfs_metadir_link(&upd);
464 if (error)
465 return error;
466
467 xfs_trans_log_inode(upd.tp, upd.ip, XFS_ILOG_CORE);
468
469 return xfs_metadir_commit(&upd);
470 }
471 #endif /* __KERNEL__ */
472
473 /* Create the parent directory for all quota inodes and load it. */
474 int
xfs_dqinode_mkdir_parent(struct xfs_mount * mp,struct xfs_inode ** dpp)475 xfs_dqinode_mkdir_parent(
476 struct xfs_mount *mp,
477 struct xfs_inode **dpp)
478 {
479 if (!mp->m_metadirip) {
480 xfs_fs_mark_sick(mp, XFS_SICK_FS_METADIR);
481 return -EFSCORRUPTED;
482 }
483
484 return xfs_metadir_mkdir(mp->m_metadirip, "quota", dpp);
485 }
486
487 /*
488 * Load the parent directory of all quota inodes. Pass the inode to the caller
489 * because quota functions (e.g. QUOTARM) can be called on the quota files even
490 * if quotas are not enabled.
491 */
492 int
xfs_dqinode_load_parent(struct xfs_trans * tp,struct xfs_inode ** dpp)493 xfs_dqinode_load_parent(
494 struct xfs_trans *tp,
495 struct xfs_inode **dpp)
496 {
497 struct xfs_mount *mp = tp->t_mountp;
498
499 if (!mp->m_metadirip) {
500 xfs_fs_mark_sick(mp, XFS_SICK_FS_METADIR);
501 return -EFSCORRUPTED;
502 }
503
504 return xfs_metadir_load(tp, mp->m_metadirip, "quota", XFS_METAFILE_DIR,
505 dpp);
506 }
507