xref: /openbmc/linux/fs/xfs/libxfs/xfs_dquot_buf.c (revision 11a83f4c)
10b61f8a4SDave Chinner // SPDX-License-Identifier: GPL-2.0
230f712c9SDave Chinner /*
330f712c9SDave Chinner  * Copyright (c) 2000-2006 Silicon Graphics, Inc.
430f712c9SDave Chinner  * Copyright (c) 2013 Red Hat, Inc.
530f712c9SDave Chinner  * All Rights Reserved.
630f712c9SDave Chinner  */
730f712c9SDave Chinner #include "xfs.h"
830f712c9SDave Chinner #include "xfs_fs.h"
930f712c9SDave Chinner #include "xfs_shared.h"
1030f712c9SDave Chinner #include "xfs_format.h"
1130f712c9SDave Chinner #include "xfs_log_format.h"
1230f712c9SDave Chinner #include "xfs_trans_resv.h"
1330f712c9SDave Chinner #include "xfs_mount.h"
1430f712c9SDave Chinner #include "xfs_inode.h"
1530f712c9SDave Chinner #include "xfs_quota.h"
1630f712c9SDave Chinner #include "xfs_trans.h"
1730f712c9SDave Chinner #include "xfs_qm.h"
1830f712c9SDave Chinner #include "xfs_error.h"
1930f712c9SDave Chinner 
2030f712c9SDave Chinner int
xfs_calc_dquots_per_chunk(unsigned int nbblks)2130f712c9SDave Chinner xfs_calc_dquots_per_chunk(
2230f712c9SDave Chinner 	unsigned int		nbblks)	/* basic block units */
2330f712c9SDave Chinner {
2430f712c9SDave Chinner 	ASSERT(nbblks > 0);
25*11a83f4cSChristoph Hellwig 	return BBTOB(nbblks) / sizeof(struct xfs_dqblk);
2630f712c9SDave Chinner }
2730f712c9SDave Chinner 
2830f712c9SDave Chinner /*
2930f712c9SDave Chinner  * Do some primitive error checking on ondisk dquot data structures.
307224fa48SEric Sandeen  *
317224fa48SEric Sandeen  * The xfs_dqblk structure /contains/ the xfs_disk_dquot structure;
327224fa48SEric Sandeen  * we verify them separately because at some points we have only the
337224fa48SEric Sandeen  * smaller xfs_disk_dquot structure available.
3430f712c9SDave Chinner  */
357224fa48SEric Sandeen 
36eebf3cabSDarrick J. Wong xfs_failaddr_t
xfs_dquot_verify(struct xfs_mount * mp,struct xfs_disk_dquot * ddq,xfs_dqid_t id)37eebf3cabSDarrick J. Wong xfs_dquot_verify(
3830f712c9SDave Chinner 	struct xfs_mount	*mp,
39aefe69a4SPavel Reichl 	struct xfs_disk_dquot	*ddq,
40f9751c4aSDarrick J. Wong 	xfs_dqid_t		id)	/* used only during quotacheck */
4130f712c9SDave Chinner {
42a990f7a8SDarrick J. Wong 	__u8			ddq_type;
43a990f7a8SDarrick J. Wong 
4430f712c9SDave Chinner 	/*
4530f712c9SDave Chinner 	 * We can encounter an uninitialized dquot buffer for 2 reasons:
4630f712c9SDave Chinner 	 * 1. If we crash while deleting the quotainode(s), and those blks got
4730f712c9SDave Chinner 	 *    used for user data. This is because we take the path of regular
4830f712c9SDave Chinner 	 *    file deletion; however, the size field of quotainodes is never
4930f712c9SDave Chinner 	 *    updated, so all the tricks that we play in itruncate_finish
5030f712c9SDave Chinner 	 *    don't quite matter.
5130f712c9SDave Chinner 	 *
5230f712c9SDave Chinner 	 * 2. We don't play the quota buffers when there's a quotaoff logitem.
5330f712c9SDave Chinner 	 *    But the allocation will be replayed so we'll end up with an
5430f712c9SDave Chinner 	 *    uninitialized quota block.
5530f712c9SDave Chinner 	 *
5630f712c9SDave Chinner 	 * This is all fine; things are still consistent, and we haven't lost
5730f712c9SDave Chinner 	 * any quota information. Just don't complain about bad dquot blks.
5830f712c9SDave Chinner 	 */
59eebf3cabSDarrick J. Wong 	if (ddq->d_magic != cpu_to_be16(XFS_DQUOT_MAGIC))
60eebf3cabSDarrick J. Wong 		return __this_address;
61eebf3cabSDarrick J. Wong 	if (ddq->d_version != XFS_DQUOT_VERSION)
62eebf3cabSDarrick J. Wong 		return __this_address;
6330f712c9SDave Chinner 
64d8c1af0dSDarrick J. Wong 	if (ddq->d_type & ~XFS_DQTYPE_ANY)
65a990f7a8SDarrick J. Wong 		return __this_address;
66d8c1af0dSDarrick J. Wong 	ddq_type = ddq->d_type & XFS_DQTYPE_REC_MASK;
67a990f7a8SDarrick J. Wong 	if (ddq_type != XFS_DQTYPE_USER &&
68a990f7a8SDarrick J. Wong 	    ddq_type != XFS_DQTYPE_PROJ &&
69a990f7a8SDarrick J. Wong 	    ddq_type != XFS_DQTYPE_GROUP)
70eebf3cabSDarrick J. Wong 		return __this_address;
7130f712c9SDave Chinner 
724ea1ff3bSDarrick J. Wong 	if ((ddq->d_type & XFS_DQTYPE_BIGTIME) &&
73ebd9027dSDave Chinner 	    !xfs_has_bigtime(mp))
744ea1ff3bSDarrick J. Wong 		return __this_address;
754ea1ff3bSDarrick J. Wong 
764ea1ff3bSDarrick J. Wong 	if ((ddq->d_type & XFS_DQTYPE_BIGTIME) && !ddq->d_id)
774ea1ff3bSDarrick J. Wong 		return __this_address;
784ea1ff3bSDarrick J. Wong 
79eebf3cabSDarrick J. Wong 	if (id != -1 && id != be32_to_cpu(ddq->d_id))
80eebf3cabSDarrick J. Wong 		return __this_address;
8130f712c9SDave Chinner 
82eebf3cabSDarrick J. Wong 	if (!ddq->d_id)
83eebf3cabSDarrick J. Wong 		return NULL;
84eebf3cabSDarrick J. Wong 
8530f712c9SDave Chinner 	if (ddq->d_blk_softlimit &&
86eebf3cabSDarrick J. Wong 	    be64_to_cpu(ddq->d_bcount) > be64_to_cpu(ddq->d_blk_softlimit) &&
87eebf3cabSDarrick J. Wong 	    !ddq->d_btimer)
88eebf3cabSDarrick J. Wong 		return __this_address;
8930f712c9SDave Chinner 
90eebf3cabSDarrick J. Wong 	if (ddq->d_ino_softlimit &&
91eebf3cabSDarrick J. Wong 	    be64_to_cpu(ddq->d_icount) > be64_to_cpu(ddq->d_ino_softlimit) &&
92eebf3cabSDarrick J. Wong 	    !ddq->d_itimer)
93eebf3cabSDarrick J. Wong 		return __this_address;
94eebf3cabSDarrick J. Wong 
95eebf3cabSDarrick J. Wong 	if (ddq->d_rtb_softlimit &&
96eebf3cabSDarrick J. Wong 	    be64_to_cpu(ddq->d_rtbcount) > be64_to_cpu(ddq->d_rtb_softlimit) &&
97eebf3cabSDarrick J. Wong 	    !ddq->d_rtbtimer)
98eebf3cabSDarrick J. Wong 		return __this_address;
99eebf3cabSDarrick J. Wong 
100eebf3cabSDarrick J. Wong 	return NULL;
101eeea7980SDarrick J. Wong }
10230f712c9SDave Chinner 
1037224fa48SEric Sandeen xfs_failaddr_t
xfs_dqblk_verify(struct xfs_mount * mp,struct xfs_dqblk * dqb,xfs_dqid_t id)1047224fa48SEric Sandeen xfs_dqblk_verify(
1057224fa48SEric Sandeen 	struct xfs_mount	*mp,
1067224fa48SEric Sandeen 	struct xfs_dqblk	*dqb,
107f9751c4aSDarrick J. Wong 	xfs_dqid_t		id)	/* used only during quotacheck */
1087224fa48SEric Sandeen {
10938c26bfdSDave Chinner 	if (xfs_has_crc(mp) &&
1107224fa48SEric Sandeen 	    !uuid_equal(&dqb->dd_uuid, &mp->m_sb.sb_meta_uuid))
1117224fa48SEric Sandeen 		return __this_address;
1127224fa48SEric Sandeen 
113f9751c4aSDarrick J. Wong 	return xfs_dquot_verify(mp, &dqb->dd_diskdq, id);
1147224fa48SEric Sandeen }
1157224fa48SEric Sandeen 
116eeea7980SDarrick J. Wong /*
117eeea7980SDarrick J. Wong  * Do some primitive error checking on ondisk dquot data structures.
118eeea7980SDarrick J. Wong  */
11991083269SEric Sandeen void
xfs_dqblk_repair(struct xfs_mount * mp,struct xfs_dqblk * dqb,xfs_dqid_t id,xfs_dqtype_t type)12048fa1db8SEric Sandeen xfs_dqblk_repair(
121eeea7980SDarrick J. Wong 	struct xfs_mount	*mp,
12248fa1db8SEric Sandeen 	struct xfs_dqblk	*dqb,
123eeea7980SDarrick J. Wong 	xfs_dqid_t		id,
1241a7ed271SDarrick J. Wong 	xfs_dqtype_t		type)
125eeea7980SDarrick J. Wong {
12630f712c9SDave Chinner 	/*
12730f712c9SDave Chinner 	 * Typically, a repair is only requested by quotacheck.
12830f712c9SDave Chinner 	 */
12930f712c9SDave Chinner 	ASSERT(id != -1);
130*11a83f4cSChristoph Hellwig 	memset(dqb, 0, sizeof(struct xfs_dqblk));
13130f712c9SDave Chinner 
13248fa1db8SEric Sandeen 	dqb->dd_diskdq.d_magic = cpu_to_be16(XFS_DQUOT_MAGIC);
13348fa1db8SEric Sandeen 	dqb->dd_diskdq.d_version = XFS_DQUOT_VERSION;
134d8c1af0dSDarrick J. Wong 	dqb->dd_diskdq.d_type = type;
13548fa1db8SEric Sandeen 	dqb->dd_diskdq.d_id = cpu_to_be32(id);
13630f712c9SDave Chinner 
13738c26bfdSDave Chinner 	if (xfs_has_crc(mp)) {
13848fa1db8SEric Sandeen 		uuid_copy(&dqb->dd_uuid, &mp->m_sb.sb_meta_uuid);
13948fa1db8SEric Sandeen 		xfs_update_cksum((char *)dqb, sizeof(struct xfs_dqblk),
14030f712c9SDave Chinner 				 XFS_DQUOT_CRC_OFF);
14130f712c9SDave Chinner 	}
14230f712c9SDave Chinner }
14330f712c9SDave Chinner 
14430f712c9SDave Chinner STATIC bool
xfs_dquot_buf_verify_crc(struct xfs_mount * mp,struct xfs_buf * bp,bool readahead)14530f712c9SDave Chinner xfs_dquot_buf_verify_crc(
14630f712c9SDave Chinner 	struct xfs_mount	*mp,
14772c5c5f6SEric Sandeen 	struct xfs_buf		*bp,
14872c5c5f6SEric Sandeen 	bool			readahead)
14930f712c9SDave Chinner {
15030f712c9SDave Chinner 	struct xfs_dqblk	*d = (struct xfs_dqblk *)bp->b_addr;
15130f712c9SDave Chinner 	int			ndquots;
15230f712c9SDave Chinner 	int			i;
15330f712c9SDave Chinner 
15438c26bfdSDave Chinner 	if (!xfs_has_crc(mp))
15530f712c9SDave Chinner 		return true;
15630f712c9SDave Chinner 
15730f712c9SDave Chinner 	/*
15830f712c9SDave Chinner 	 * if we are in log recovery, the quota subsystem has not been
15930f712c9SDave Chinner 	 * initialised so we have no quotainfo structure. In that case, we need
16030f712c9SDave Chinner 	 * to manually calculate the number of dquots in the buffer.
16130f712c9SDave Chinner 	 */
16230f712c9SDave Chinner 	if (mp->m_quotainfo)
16330f712c9SDave Chinner 		ndquots = mp->m_quotainfo->qi_dqperchunk;
16430f712c9SDave Chinner 	else
16558d78967SDarrick J. Wong 		ndquots = xfs_calc_dquots_per_chunk(bp->b_length);
16630f712c9SDave Chinner 
16730f712c9SDave Chinner 	for (i = 0; i < ndquots; i++, d++) {
16830f712c9SDave Chinner 		if (!xfs_verify_cksum((char *)d, sizeof(struct xfs_dqblk),
16972c5c5f6SEric Sandeen 				 XFS_DQUOT_CRC_OFF)) {
17072c5c5f6SEric Sandeen 			if (!readahead)
17172c5c5f6SEric Sandeen 				xfs_buf_verifier_error(bp, -EFSBADCRC, __func__,
17272c5c5f6SEric Sandeen 					d, sizeof(*d), __this_address);
17330f712c9SDave Chinner 			return false;
17430f712c9SDave Chinner 		}
17572c5c5f6SEric Sandeen 	}
17630f712c9SDave Chinner 	return true;
17730f712c9SDave Chinner }
17830f712c9SDave Chinner 
179eebf3cabSDarrick J. Wong STATIC xfs_failaddr_t
xfs_dquot_buf_verify(struct xfs_mount * mp,struct xfs_buf * bp,bool readahead)18030f712c9SDave Chinner xfs_dquot_buf_verify(
18130f712c9SDave Chinner 	struct xfs_mount	*mp,
18272c5c5f6SEric Sandeen 	struct xfs_buf		*bp,
18372c5c5f6SEric Sandeen 	bool			readahead)
18430f712c9SDave Chinner {
1857224fa48SEric Sandeen 	struct xfs_dqblk	*dqb = bp->b_addr;
186eebf3cabSDarrick J. Wong 	xfs_failaddr_t		fa;
18730f712c9SDave Chinner 	xfs_dqid_t		id = 0;
18830f712c9SDave Chinner 	int			ndquots;
18930f712c9SDave Chinner 	int			i;
19030f712c9SDave Chinner 
19130f712c9SDave Chinner 	/*
19230f712c9SDave Chinner 	 * if we are in log recovery, the quota subsystem has not been
19330f712c9SDave Chinner 	 * initialised so we have no quotainfo structure. In that case, we need
19430f712c9SDave Chinner 	 * to manually calculate the number of dquots in the buffer.
19530f712c9SDave Chinner 	 */
19630f712c9SDave Chinner 	if (mp->m_quotainfo)
19730f712c9SDave Chinner 		ndquots = mp->m_quotainfo->qi_dqperchunk;
19830f712c9SDave Chinner 	else
19930f712c9SDave Chinner 		ndquots = xfs_calc_dquots_per_chunk(bp->b_length);
20030f712c9SDave Chinner 
20130f712c9SDave Chinner 	/*
20230f712c9SDave Chinner 	 * On the first read of the buffer, verify that each dquot is valid.
20330f712c9SDave Chinner 	 * We don't know what the id of the dquot is supposed to be, just that
20430f712c9SDave Chinner 	 * they should be increasing monotonically within the buffer. If the
20530f712c9SDave Chinner 	 * first id is corrupt, then it will fail on the second dquot in the
20630f712c9SDave Chinner 	 * buffer so corruptions could point to the wrong dquot in this case.
20730f712c9SDave Chinner 	 */
20830f712c9SDave Chinner 	for (i = 0; i < ndquots; i++) {
20930f712c9SDave Chinner 		struct xfs_disk_dquot	*ddq;
21030f712c9SDave Chinner 
2117224fa48SEric Sandeen 		ddq = &dqb[i].dd_diskdq;
21230f712c9SDave Chinner 
21330f712c9SDave Chinner 		if (i == 0)
21430f712c9SDave Chinner 			id = be32_to_cpu(ddq->d_id);
21530f712c9SDave Chinner 
216f9751c4aSDarrick J. Wong 		fa = xfs_dqblk_verify(mp, &dqb[i], id + i);
21772c5c5f6SEric Sandeen 		if (fa) {
21872c5c5f6SEric Sandeen 			if (!readahead)
21972c5c5f6SEric Sandeen 				xfs_buf_verifier_error(bp, -EFSCORRUPTED,
22072c5c5f6SEric Sandeen 					__func__, &dqb[i],
22172c5c5f6SEric Sandeen 					sizeof(struct xfs_dqblk), fa);
222eebf3cabSDarrick J. Wong 			return fa;
22330f712c9SDave Chinner 		}
22472c5c5f6SEric Sandeen 	}
225eebf3cabSDarrick J. Wong 
226eebf3cabSDarrick J. Wong 	return NULL;
22730f712c9SDave Chinner }
22830f712c9SDave Chinner 
229b5572597SDarrick J. Wong static xfs_failaddr_t
xfs_dquot_buf_verify_struct(struct xfs_buf * bp)230b5572597SDarrick J. Wong xfs_dquot_buf_verify_struct(
231b5572597SDarrick J. Wong 	struct xfs_buf		*bp)
232b5572597SDarrick J. Wong {
233dbd329f1SChristoph Hellwig 	struct xfs_mount	*mp = bp->b_mount;
234b5572597SDarrick J. Wong 
23572c5c5f6SEric Sandeen 	return xfs_dquot_buf_verify(mp, bp, false);
236b5572597SDarrick J. Wong }
237b5572597SDarrick J. Wong 
23830f712c9SDave Chinner static void
xfs_dquot_buf_read_verify(struct xfs_buf * bp)23930f712c9SDave Chinner xfs_dquot_buf_read_verify(
24030f712c9SDave Chinner 	struct xfs_buf		*bp)
24130f712c9SDave Chinner {
242dbd329f1SChristoph Hellwig 	struct xfs_mount	*mp = bp->b_mount;
24330f712c9SDave Chinner 
24472c5c5f6SEric Sandeen 	if (!xfs_dquot_buf_verify_crc(mp, bp, false))
24572c5c5f6SEric Sandeen 		return;
24672c5c5f6SEric Sandeen 	xfs_dquot_buf_verify(mp, bp, false);
247eebf3cabSDarrick J. Wong }
24830f712c9SDave Chinner 
24930f712c9SDave Chinner /*
2507d6a13f0SDave Chinner  * readahead errors are silent and simply leave the buffer as !done so a real
2517d6a13f0SDave Chinner  * read will then be run with the xfs_dquot_buf_ops verifier. See
2527d6a13f0SDave Chinner  * xfs_inode_buf_verify() for why we use EIO and ~XBF_DONE here rather than
2537d6a13f0SDave Chinner  * reporting the failure.
2547d6a13f0SDave Chinner  */
2557d6a13f0SDave Chinner static void
xfs_dquot_buf_readahead_verify(struct xfs_buf * bp)2567d6a13f0SDave Chinner xfs_dquot_buf_readahead_verify(
2577d6a13f0SDave Chinner 	struct xfs_buf	*bp)
2587d6a13f0SDave Chinner {
259dbd329f1SChristoph Hellwig 	struct xfs_mount	*mp = bp->b_mount;
2607d6a13f0SDave Chinner 
26172c5c5f6SEric Sandeen 	if (!xfs_dquot_buf_verify_crc(mp, bp, true) ||
26272c5c5f6SEric Sandeen 	    xfs_dquot_buf_verify(mp, bp, true) != NULL) {
2637d6a13f0SDave Chinner 		xfs_buf_ioerror(bp, -EIO);
2647d6a13f0SDave Chinner 		bp->b_flags &= ~XBF_DONE;
2657d6a13f0SDave Chinner 	}
2667d6a13f0SDave Chinner }
2677d6a13f0SDave Chinner 
2687d6a13f0SDave Chinner /*
26930f712c9SDave Chinner  * we don't calculate the CRC here as that is done when the dquot is flushed to
27030f712c9SDave Chinner  * the buffer after the update is done. This ensures that the dquot in the
27130f712c9SDave Chinner  * buffer always has an up-to-date CRC value.
27230f712c9SDave Chinner  */
27330f712c9SDave Chinner static void
xfs_dquot_buf_write_verify(struct xfs_buf * bp)27430f712c9SDave Chinner xfs_dquot_buf_write_verify(
27530f712c9SDave Chinner 	struct xfs_buf		*bp)
27630f712c9SDave Chinner {
277dbd329f1SChristoph Hellwig 	struct xfs_mount	*mp = bp->b_mount;
27830f712c9SDave Chinner 
27972c5c5f6SEric Sandeen 	xfs_dquot_buf_verify(mp, bp, false);
28030f712c9SDave Chinner }
28130f712c9SDave Chinner 
28230f712c9SDave Chinner const struct xfs_buf_ops xfs_dquot_buf_ops = {
283233135b7SEric Sandeen 	.name = "xfs_dquot",
28415baadf7SDarrick J. Wong 	.magic16 = { cpu_to_be16(XFS_DQUOT_MAGIC),
2854260baacSDarrick J. Wong 		     cpu_to_be16(XFS_DQUOT_MAGIC) },
28630f712c9SDave Chinner 	.verify_read = xfs_dquot_buf_read_verify,
28730f712c9SDave Chinner 	.verify_write = xfs_dquot_buf_write_verify,
288b5572597SDarrick J. Wong 	.verify_struct = xfs_dquot_buf_verify_struct,
28930f712c9SDave Chinner };
29030f712c9SDave Chinner 
2917d6a13f0SDave Chinner const struct xfs_buf_ops xfs_dquot_buf_ra_ops = {
2927d6a13f0SDave Chinner 	.name = "xfs_dquot_ra",
29315baadf7SDarrick J. Wong 	.magic16 = { cpu_to_be16(XFS_DQUOT_MAGIC),
2944260baacSDarrick J. Wong 		     cpu_to_be16(XFS_DQUOT_MAGIC) },
2957d6a13f0SDave Chinner 	.verify_read = xfs_dquot_buf_readahead_verify,
2967d6a13f0SDave Chinner 	.verify_write = xfs_dquot_buf_write_verify,
2977d6a13f0SDave Chinner };
2989f99c8feSDarrick J. Wong 
2999f99c8feSDarrick J. Wong /* Convert an on-disk timer value into an incore timer value. */
3009f99c8feSDarrick J. Wong time64_t
xfs_dquot_from_disk_ts(struct xfs_disk_dquot * ddq,__be32 dtimer)3019f99c8feSDarrick J. Wong xfs_dquot_from_disk_ts(
3029f99c8feSDarrick J. Wong 	struct xfs_disk_dquot	*ddq,
3039f99c8feSDarrick J. Wong 	__be32			dtimer)
3049f99c8feSDarrick J. Wong {
3054ea1ff3bSDarrick J. Wong 	uint32_t		t = be32_to_cpu(dtimer);
3064ea1ff3bSDarrick J. Wong 
3074ea1ff3bSDarrick J. Wong 	if (t != 0 && (ddq->d_type & XFS_DQTYPE_BIGTIME))
3084ea1ff3bSDarrick J. Wong 		return xfs_dq_bigtime_to_unix(t);
3094ea1ff3bSDarrick J. Wong 
3104ea1ff3bSDarrick J. Wong 	return t;
3119f99c8feSDarrick J. Wong }
3129f99c8feSDarrick J. Wong 
3139f99c8feSDarrick J. Wong /* Convert an incore timer value into an on-disk timer value. */
3149f99c8feSDarrick J. Wong __be32
xfs_dquot_to_disk_ts(struct xfs_dquot * dqp,time64_t timer)3159f99c8feSDarrick J. Wong xfs_dquot_to_disk_ts(
3169f99c8feSDarrick J. Wong 	struct xfs_dquot	*dqp,
3179f99c8feSDarrick J. Wong 	time64_t		timer)
3189f99c8feSDarrick J. Wong {
3194ea1ff3bSDarrick J. Wong 	uint32_t		t = timer;
3204ea1ff3bSDarrick J. Wong 
3214ea1ff3bSDarrick J. Wong 	if (timer != 0 && (dqp->q_type & XFS_DQTYPE_BIGTIME))
3224ea1ff3bSDarrick J. Wong 		t = xfs_dq_unix_to_bigtime(timer);
3234ea1ff3bSDarrick J. Wong 
3244ea1ff3bSDarrick J. Wong 	return cpu_to_be32(t);
3259f99c8feSDarrick J. Wong }
326