xref: /OK3568_Linux_fs/kernel/fs/xfs/xfs_dquot_item_recover.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (c) 2000-2006 Silicon Graphics, Inc.
4*4882a593Smuzhiyun  * All Rights Reserved.
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun #include "xfs.h"
7*4882a593Smuzhiyun #include "xfs_fs.h"
8*4882a593Smuzhiyun #include "xfs_shared.h"
9*4882a593Smuzhiyun #include "xfs_format.h"
10*4882a593Smuzhiyun #include "xfs_log_format.h"
11*4882a593Smuzhiyun #include "xfs_trans_resv.h"
12*4882a593Smuzhiyun #include "xfs_mount.h"
13*4882a593Smuzhiyun #include "xfs_inode.h"
14*4882a593Smuzhiyun #include "xfs_quota.h"
15*4882a593Smuzhiyun #include "xfs_trans.h"
16*4882a593Smuzhiyun #include "xfs_buf_item.h"
17*4882a593Smuzhiyun #include "xfs_trans_priv.h"
18*4882a593Smuzhiyun #include "xfs_qm.h"
19*4882a593Smuzhiyun #include "xfs_log.h"
20*4882a593Smuzhiyun #include "xfs_log_priv.h"
21*4882a593Smuzhiyun #include "xfs_log_recover.h"
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun STATIC void
xlog_recover_dquot_ra_pass2(struct xlog * log,struct xlog_recover_item * item)24*4882a593Smuzhiyun xlog_recover_dquot_ra_pass2(
25*4882a593Smuzhiyun 	struct xlog			*log,
26*4882a593Smuzhiyun 	struct xlog_recover_item	*item)
27*4882a593Smuzhiyun {
28*4882a593Smuzhiyun 	struct xfs_mount	*mp = log->l_mp;
29*4882a593Smuzhiyun 	struct xfs_disk_dquot	*recddq;
30*4882a593Smuzhiyun 	struct xfs_dq_logformat	*dq_f;
31*4882a593Smuzhiyun 	uint			type;
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun 	if (mp->m_qflags == 0)
34*4882a593Smuzhiyun 		return;
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun 	recddq = item->ri_buf[1].i_addr;
37*4882a593Smuzhiyun 	if (recddq == NULL)
38*4882a593Smuzhiyun 		return;
39*4882a593Smuzhiyun 	if (item->ri_buf[1].i_len < sizeof(struct xfs_disk_dquot))
40*4882a593Smuzhiyun 		return;
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun 	type = recddq->d_type & XFS_DQTYPE_REC_MASK;
43*4882a593Smuzhiyun 	ASSERT(type);
44*4882a593Smuzhiyun 	if (log->l_quotaoffs_flag & type)
45*4882a593Smuzhiyun 		return;
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun 	dq_f = item->ri_buf[0].i_addr;
48*4882a593Smuzhiyun 	ASSERT(dq_f);
49*4882a593Smuzhiyun 	ASSERT(dq_f->qlf_len == 1);
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun 	xlog_buf_readahead(log, dq_f->qlf_blkno,
52*4882a593Smuzhiyun 			XFS_FSB_TO_BB(mp, dq_f->qlf_len),
53*4882a593Smuzhiyun 			&xfs_dquot_buf_ra_ops);
54*4882a593Smuzhiyun }
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun /*
57*4882a593Smuzhiyun  * Recover a dquot record
58*4882a593Smuzhiyun  */
59*4882a593Smuzhiyun STATIC int
xlog_recover_dquot_commit_pass2(struct xlog * log,struct list_head * buffer_list,struct xlog_recover_item * item,xfs_lsn_t current_lsn)60*4882a593Smuzhiyun xlog_recover_dquot_commit_pass2(
61*4882a593Smuzhiyun 	struct xlog			*log,
62*4882a593Smuzhiyun 	struct list_head		*buffer_list,
63*4882a593Smuzhiyun 	struct xlog_recover_item	*item,
64*4882a593Smuzhiyun 	xfs_lsn_t			current_lsn)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun 	struct xfs_mount		*mp = log->l_mp;
67*4882a593Smuzhiyun 	struct xfs_buf			*bp;
68*4882a593Smuzhiyun 	struct xfs_disk_dquot		*ddq, *recddq;
69*4882a593Smuzhiyun 	struct xfs_dq_logformat		*dq_f;
70*4882a593Smuzhiyun 	xfs_failaddr_t			fa;
71*4882a593Smuzhiyun 	int				error;
72*4882a593Smuzhiyun 	uint				type;
73*4882a593Smuzhiyun 
74*4882a593Smuzhiyun 	/*
75*4882a593Smuzhiyun 	 * Filesystems are required to send in quota flags at mount time.
76*4882a593Smuzhiyun 	 */
77*4882a593Smuzhiyun 	if (mp->m_qflags == 0)
78*4882a593Smuzhiyun 		return 0;
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 	recddq = item->ri_buf[1].i_addr;
81*4882a593Smuzhiyun 	if (recddq == NULL) {
82*4882a593Smuzhiyun 		xfs_alert(log->l_mp, "NULL dquot in %s.", __func__);
83*4882a593Smuzhiyun 		return -EFSCORRUPTED;
84*4882a593Smuzhiyun 	}
85*4882a593Smuzhiyun 	if (item->ri_buf[1].i_len < sizeof(struct xfs_disk_dquot)) {
86*4882a593Smuzhiyun 		xfs_alert(log->l_mp, "dquot too small (%d) in %s.",
87*4882a593Smuzhiyun 			item->ri_buf[1].i_len, __func__);
88*4882a593Smuzhiyun 		return -EFSCORRUPTED;
89*4882a593Smuzhiyun 	}
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 	/*
92*4882a593Smuzhiyun 	 * This type of quotas was turned off, so ignore this record.
93*4882a593Smuzhiyun 	 */
94*4882a593Smuzhiyun 	type = recddq->d_type & XFS_DQTYPE_REC_MASK;
95*4882a593Smuzhiyun 	ASSERT(type);
96*4882a593Smuzhiyun 	if (log->l_quotaoffs_flag & type)
97*4882a593Smuzhiyun 		return 0;
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 	/*
100*4882a593Smuzhiyun 	 * At this point we know that quota was _not_ turned off.
101*4882a593Smuzhiyun 	 * Since the mount flags are not indicating to us otherwise, this
102*4882a593Smuzhiyun 	 * must mean that quota is on, and the dquot needs to be replayed.
103*4882a593Smuzhiyun 	 * Remember that we may not have fully recovered the superblock yet,
104*4882a593Smuzhiyun 	 * so we can't do the usual trick of looking at the SB quota bits.
105*4882a593Smuzhiyun 	 *
106*4882a593Smuzhiyun 	 * The other possibility, of course, is that the quota subsystem was
107*4882a593Smuzhiyun 	 * removed since the last mount - ENOSYS.
108*4882a593Smuzhiyun 	 */
109*4882a593Smuzhiyun 	dq_f = item->ri_buf[0].i_addr;
110*4882a593Smuzhiyun 	ASSERT(dq_f);
111*4882a593Smuzhiyun 	fa = xfs_dquot_verify(mp, recddq, dq_f->qlf_id);
112*4882a593Smuzhiyun 	if (fa) {
113*4882a593Smuzhiyun 		xfs_alert(mp, "corrupt dquot ID 0x%x in log at %pS",
114*4882a593Smuzhiyun 				dq_f->qlf_id, fa);
115*4882a593Smuzhiyun 		return -EFSCORRUPTED;
116*4882a593Smuzhiyun 	}
117*4882a593Smuzhiyun 	ASSERT(dq_f->qlf_len == 1);
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 	/*
120*4882a593Smuzhiyun 	 * At this point we are assuming that the dquots have been allocated
121*4882a593Smuzhiyun 	 * and hence the buffer has valid dquots stamped in it. It should,
122*4882a593Smuzhiyun 	 * therefore, pass verifier validation. If the dquot is bad, then the
123*4882a593Smuzhiyun 	 * we'll return an error here, so we don't need to specifically check
124*4882a593Smuzhiyun 	 * the dquot in the buffer after the verifier has run.
125*4882a593Smuzhiyun 	 */
126*4882a593Smuzhiyun 	error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dq_f->qlf_blkno,
127*4882a593Smuzhiyun 				   XFS_FSB_TO_BB(mp, dq_f->qlf_len), 0, &bp,
128*4882a593Smuzhiyun 				   &xfs_dquot_buf_ops);
129*4882a593Smuzhiyun 	if (error)
130*4882a593Smuzhiyun 		return error;
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	ASSERT(bp);
133*4882a593Smuzhiyun 	ddq = xfs_buf_offset(bp, dq_f->qlf_boffset);
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	/*
136*4882a593Smuzhiyun 	 * If the dquot has an LSN in it, recover the dquot only if it's less
137*4882a593Smuzhiyun 	 * than the lsn of the transaction we are replaying.
138*4882a593Smuzhiyun 	 */
139*4882a593Smuzhiyun 	if (xfs_sb_version_hascrc(&mp->m_sb)) {
140*4882a593Smuzhiyun 		struct xfs_dqblk *dqb = (struct xfs_dqblk *)ddq;
141*4882a593Smuzhiyun 		xfs_lsn_t	lsn = be64_to_cpu(dqb->dd_lsn);
142*4882a593Smuzhiyun 
143*4882a593Smuzhiyun 		if (lsn && lsn != -1 && XFS_LSN_CMP(lsn, current_lsn) >= 0) {
144*4882a593Smuzhiyun 			goto out_release;
145*4882a593Smuzhiyun 		}
146*4882a593Smuzhiyun 	}
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 	memcpy(ddq, recddq, item->ri_buf[1].i_len);
149*4882a593Smuzhiyun 	if (xfs_sb_version_hascrc(&mp->m_sb)) {
150*4882a593Smuzhiyun 		xfs_update_cksum((char *)ddq, sizeof(struct xfs_dqblk),
151*4882a593Smuzhiyun 				 XFS_DQUOT_CRC_OFF);
152*4882a593Smuzhiyun 	}
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	ASSERT(dq_f->qlf_size == 2);
155*4882a593Smuzhiyun 	ASSERT(bp->b_mount == mp);
156*4882a593Smuzhiyun 	bp->b_flags |= _XBF_LOGRECOVERY;
157*4882a593Smuzhiyun 	xfs_buf_delwri_queue(bp, buffer_list);
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun out_release:
160*4882a593Smuzhiyun 	xfs_buf_relse(bp);
161*4882a593Smuzhiyun 	return 0;
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun const struct xlog_recover_item_ops xlog_dquot_item_ops = {
165*4882a593Smuzhiyun 	.item_type		= XFS_LI_DQUOT,
166*4882a593Smuzhiyun 	.ra_pass2		= xlog_recover_dquot_ra_pass2,
167*4882a593Smuzhiyun 	.commit_pass2		= xlog_recover_dquot_commit_pass2,
168*4882a593Smuzhiyun };
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun /*
171*4882a593Smuzhiyun  * Recover QUOTAOFF records. We simply make a note of it in the xlog
172*4882a593Smuzhiyun  * structure, so that we know not to do any dquot item or dquot buffer recovery,
173*4882a593Smuzhiyun  * of that type.
174*4882a593Smuzhiyun  */
175*4882a593Smuzhiyun STATIC int
xlog_recover_quotaoff_commit_pass1(struct xlog * log,struct xlog_recover_item * item)176*4882a593Smuzhiyun xlog_recover_quotaoff_commit_pass1(
177*4882a593Smuzhiyun 	struct xlog			*log,
178*4882a593Smuzhiyun 	struct xlog_recover_item	*item)
179*4882a593Smuzhiyun {
180*4882a593Smuzhiyun 	struct xfs_qoff_logformat	*qoff_f = item->ri_buf[0].i_addr;
181*4882a593Smuzhiyun 	ASSERT(qoff_f);
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun 	/*
184*4882a593Smuzhiyun 	 * The logitem format's flag tells us if this was user quotaoff,
185*4882a593Smuzhiyun 	 * group/project quotaoff or both.
186*4882a593Smuzhiyun 	 */
187*4882a593Smuzhiyun 	if (qoff_f->qf_flags & XFS_UQUOTA_ACCT)
188*4882a593Smuzhiyun 		log->l_quotaoffs_flag |= XFS_DQTYPE_USER;
189*4882a593Smuzhiyun 	if (qoff_f->qf_flags & XFS_PQUOTA_ACCT)
190*4882a593Smuzhiyun 		log->l_quotaoffs_flag |= XFS_DQTYPE_PROJ;
191*4882a593Smuzhiyun 	if (qoff_f->qf_flags & XFS_GQUOTA_ACCT)
192*4882a593Smuzhiyun 		log->l_quotaoffs_flag |= XFS_DQTYPE_GROUP;
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun 	return 0;
195*4882a593Smuzhiyun }
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun const struct xlog_recover_item_ops xlog_quotaoff_item_ops = {
198*4882a593Smuzhiyun 	.item_type		= XFS_LI_QUOTAOFF,
199*4882a593Smuzhiyun 	.commit_pass1		= xlog_recover_quotaoff_commit_pass1,
200*4882a593Smuzhiyun 	/* nothing to commit in pass2 */
201*4882a593Smuzhiyun };
202