diff mbox series

[04/18] xfs: stop using q_core.d_flags in the quota code

Message ID 159353173676.2864738.5361850443664572160.stgit@magnolia (mailing list archive)
State Superseded
Headers show
Series xfs: remove xfs_disk_quot from incore dquot | expand

Commit Message

Darrick J. Wong June 30, 2020, 3:42 p.m. UTC
From: Darrick J. Wong <darrick.wong@oracle.com>

Use the incore dq_flags to figure out the dquot type.  This is the first
step towards removing xfs_disk_dquot from the incore dquot.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
---
 fs/xfs/libxfs/xfs_quota_defs.h |    2 ++
 fs/xfs/scrub/quota.c           |    4 ----
 fs/xfs/xfs_dquot.c             |   33 +++++++++++++++++++++++++++++++--
 fs/xfs/xfs_dquot.h             |    2 ++
 fs/xfs/xfs_dquot_item.c        |    6 ++++--
 fs/xfs/xfs_qm.c                |    4 ++--
 fs/xfs/xfs_qm.h                |    2 +-
 fs/xfs/xfs_qm_syscalls.c       |    9 +++------
 8 files changed, 45 insertions(+), 17 deletions(-)

Comments

Chandan Babu R July 1, 2020, 8:34 a.m. UTC | #1
On Tuesday 30 June 2020 9:12:16 PM IST Darrick J. Wong wrote:
> From: Darrick J. Wong <darrick.wong@oracle.com>
> 
> Use the incore dq_flags to figure out the dquot type.  This is the first
> step towards removing xfs_disk_dquot from the incore dquot.
>

The changes are logically correct.

Reviewed-by: Chandan Babu R <chandanrlinux@gmail.com>

> Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
> ---
>  fs/xfs/libxfs/xfs_quota_defs.h |    2 ++
>  fs/xfs/scrub/quota.c           |    4 ----
>  fs/xfs/xfs_dquot.c             |   33 +++++++++++++++++++++++++++++++--
>  fs/xfs/xfs_dquot.h             |    2 ++
>  fs/xfs/xfs_dquot_item.c        |    6 ++++--
>  fs/xfs/xfs_qm.c                |    4 ++--
>  fs/xfs/xfs_qm.h                |    2 +-
>  fs/xfs/xfs_qm_syscalls.c       |    9 +++------
>  8 files changed, 45 insertions(+), 17 deletions(-)
> 
> 
> diff --git a/fs/xfs/libxfs/xfs_quota_defs.h b/fs/xfs/libxfs/xfs_quota_defs.h
> index 56d9dd787e7b..459023b0a304 100644
> --- a/fs/xfs/libxfs/xfs_quota_defs.h
> +++ b/fs/xfs/libxfs/xfs_quota_defs.h
> @@ -29,6 +29,8 @@ typedef uint16_t	xfs_qwarncnt_t;
>  
>  #define XFS_DQ_ALLTYPES		(XFS_DQ_USER|XFS_DQ_PROJ|XFS_DQ_GROUP)
>  
> +#define XFS_DQ_ONDISK		(XFS_DQ_ALLTYPES)
> +
>  #define XFS_DQ_FLAGS \
>  	{ XFS_DQ_USER,		"USER" }, \
>  	{ XFS_DQ_PROJ,		"PROJ" }, \
> diff --git a/fs/xfs/scrub/quota.c b/fs/xfs/scrub/quota.c
> index 905a34558361..710659d3fa28 100644
> --- a/fs/xfs/scrub/quota.c
> +++ b/fs/xfs/scrub/quota.c
> @@ -108,10 +108,6 @@ xchk_quota_item(
>  
>  	sqi->last_id = id;
>  
> -	/* Did we get the dquot type we wanted? */
> -	if (dqtype != (d->d_flags & XFS_DQ_ALLTYPES))
> -		xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
> -
>  	if (d->d_pad0 != cpu_to_be32(0) || d->d_pad != cpu_to_be16(0))
>  		xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
>  
> diff --git a/fs/xfs/xfs_dquot.c b/fs/xfs/xfs_dquot.c
> index 46c8ca83c04d..59d1bce34a98 100644
> --- a/fs/xfs/xfs_dquot.c
> +++ b/fs/xfs/xfs_dquot.c
> @@ -561,6 +561,16 @@ xfs_dquot_from_disk(
>  	return 0;
>  }
>  
> +/* Copy the in-core quota fields into the on-disk buffer. */
> +void
> +xfs_dquot_to_disk(
> +	struct xfs_disk_dquot	*ddqp,
> +	struct xfs_dquot	*dqp)
> +{
> +	memcpy(ddqp, &dqp->q_core, sizeof(struct xfs_disk_dquot));
> +	ddqp->d_flags = dqp->dq_flags & XFS_DQ_ONDISK;
> +}
> +
>  /* Allocate and initialize the dquot buffer for this in-core dquot. */
>  static int
>  xfs_qm_dqread_alloc(
> @@ -1108,6 +1118,17 @@ xfs_qm_dqflush_done(
>  	xfs_dqfunlock(dqp);
>  }
>  
> +/* Check incore dquot for errors before we flush. */
> +static xfs_failaddr_t
> +xfs_qm_dqflush_check(
> +	struct xfs_dquot	*dqp)
> +{
> +	if (hweight8(dqp->dq_flags & XFS_DQ_ALLTYPES) != 1)
> +		return __this_address;
> +
> +	return NULL;
> +}
> +
>  /*
>   * Write a modified dquot to disk.
>   * The dquot must be locked and the flush lock too taken by caller.
> @@ -1166,8 +1187,16 @@ xfs_qm_dqflush(
>  		goto out_abort;
>  	}
>  
> -	/* This is the only portion of data that needs to persist */
> -	memcpy(ddqp, &dqp->q_core, sizeof(struct xfs_disk_dquot));
> +	fa = xfs_qm_dqflush_check(dqp);
> +	if (fa) {
> +		xfs_alert(mp, "corrupt dquot ID 0x%x in memory at %pS",
> +				be32_to_cpu(dqp->q_core.d_id), fa);
> +		xfs_buf_relse(bp);
> +		error = -EFSCORRUPTED;
> +		goto out_abort;
> +	}
> +
> +	xfs_dquot_to_disk(ddqp, dqp);
>  
>  	/*
>  	 * Clear the dirty field and remember the flush lsn for later use.
> diff --git a/fs/xfs/xfs_dquot.h b/fs/xfs/xfs_dquot.h
> index 71e36c85e20b..1b1a4261a580 100644
> --- a/fs/xfs/xfs_dquot.h
> +++ b/fs/xfs/xfs_dquot.h
> @@ -144,6 +144,8 @@ static inline bool xfs_dquot_lowsp(struct xfs_dquot *dqp)
>  	return false;
>  }
>  
> +void xfs_dquot_to_disk(struct xfs_disk_dquot *ddqp, struct xfs_dquot *dqp);
> +
>  #define XFS_DQ_IS_LOCKED(dqp)	(mutex_is_locked(&((dqp)->q_qlock)))
>  #define XFS_DQ_IS_DIRTY(dqp)	((dqp)->dq_flags & XFS_DQ_DIRTY)
>  #define XFS_QM_ISUDQ(dqp)	((dqp)->dq_flags & XFS_DQ_USER)
> diff --git a/fs/xfs/xfs_dquot_item.c b/fs/xfs/xfs_dquot_item.c
> index 349c92d26570..ff0ab65cf413 100644
> --- a/fs/xfs/xfs_dquot_item.c
> +++ b/fs/xfs/xfs_dquot_item.c
> @@ -45,6 +45,7 @@ xfs_qm_dquot_logitem_format(
>  	struct xfs_log_item	*lip,
>  	struct xfs_log_vec	*lv)
>  {
> +	struct xfs_disk_dquot	ddq;
>  	struct xfs_dq_logitem	*qlip = DQUOT_ITEM(lip);
>  	struct xfs_log_iovec	*vecp = NULL;
>  	struct xfs_dq_logformat	*qlf;
> @@ -58,8 +59,9 @@ xfs_qm_dquot_logitem_format(
>  	qlf->qlf_boffset = qlip->qli_dquot->q_bufoffset;
>  	xlog_finish_iovec(lv, vecp, sizeof(struct xfs_dq_logformat));
>  
> -	xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_DQUOT,
> -			&qlip->qli_dquot->q_core,
> +	xfs_dquot_to_disk(&ddq, qlip->qli_dquot);
> +
> +	xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_DQUOT, &ddq,
>  			sizeof(struct xfs_disk_dquot));
>  }
>  
> diff --git a/fs/xfs/xfs_qm.c b/fs/xfs/xfs_qm.c
> index 938023dd8ce5..632025c2f00b 100644
> --- a/fs/xfs/xfs_qm.c
> +++ b/fs/xfs/xfs_qm.c
> @@ -161,7 +161,7 @@ xfs_qm_dqpurge(
>  	xfs_dqfunlock(dqp);
>  	xfs_dqunlock(dqp);
>  
> -	radix_tree_delete(xfs_dquot_tree(qi, dqp->q_core.d_flags),
> +	radix_tree_delete(xfs_dquot_tree(qi, dqp->dq_flags),
>  			  be32_to_cpu(dqp->q_core.d_id));
>  	qi->qi_dquots--;
>  
> @@ -1598,7 +1598,7 @@ xfs_qm_dqfree_one(
>  	struct xfs_quotainfo	*qi = mp->m_quotainfo;
>  
>  	mutex_lock(&qi->qi_tree_lock);
> -	radix_tree_delete(xfs_dquot_tree(qi, dqp->q_core.d_flags),
> +	radix_tree_delete(xfs_dquot_tree(qi, dqp->dq_flags),
>  			  be32_to_cpu(dqp->q_core.d_id));
>  
>  	qi->qi_dquots--;
> diff --git a/fs/xfs/xfs_qm.h b/fs/xfs/xfs_qm.h
> index 7b0e771fcbce..43b4650cdcdf 100644
> --- a/fs/xfs/xfs_qm.h
> +++ b/fs/xfs/xfs_qm.h
> @@ -85,7 +85,7 @@ xfs_dquot_tree(
>  	struct xfs_quotainfo	*qi,
>  	int			type)
>  {
> -	switch (type) {
> +	switch (type & XFS_DQ_ALLTYPES) {
>  	case XFS_DQ_USER:
>  		return &qi->qi_uquota_tree;
>  	case XFS_DQ_GROUP:
> diff --git a/fs/xfs/xfs_qm_syscalls.c b/fs/xfs/xfs_qm_syscalls.c
> index 7effd7a28136..8cbb65f01bf1 100644
> --- a/fs/xfs/xfs_qm_syscalls.c
> +++ b/fs/xfs/xfs_qm_syscalls.c
> @@ -644,12 +644,9 @@ xfs_qm_scall_getquota_fill_qc(
>  	 * gets turned off. No need to confuse the user level code,
>  	 * so return zeroes in that case.
>  	 */
> -	if ((!XFS_IS_UQUOTA_ENFORCED(mp) &&
> -	     dqp->q_core.d_flags == XFS_DQ_USER) ||
> -	    (!XFS_IS_GQUOTA_ENFORCED(mp) &&
> -	     dqp->q_core.d_flags == XFS_DQ_GROUP) ||
> -	    (!XFS_IS_PQUOTA_ENFORCED(mp) &&
> -	     dqp->q_core.d_flags == XFS_DQ_PROJ)) {
> +	if ((!XFS_IS_UQUOTA_ENFORCED(mp) && (dqp->dq_flags & XFS_DQ_USER)) ||
> +	    (!XFS_IS_GQUOTA_ENFORCED(mp) && (dqp->dq_flags & XFS_DQ_GROUP)) ||
> +	    (!XFS_IS_PQUOTA_ENFORCED(mp) && (dqp->dq_flags & XFS_DQ_PROJ))) {
>  		dst->d_spc_timer = 0;
>  		dst->d_ino_timer = 0;
>  		dst->d_rt_spc_timer = 0;
> 
>
Christoph Hellwig July 1, 2020, 8:47 a.m. UTC | #2
>  #define XFS_DQ_ALLTYPES		(XFS_DQ_USER|XFS_DQ_PROJ|XFS_DQ_GROUP)
>  
> +#define XFS_DQ_ONDISK		(XFS_DQ_ALLTYPES)
> +

I really wonder if we should split the on-disk type and the in-core
flags properly instead.

That is propagate the u8 flags from the on-disk field directly,
and use a separate field for the in-memory flags dirty and freeing
flags, as that this kind of mixing up is bound to eventually create
problems.
Darrick J. Wong July 1, 2020, 7:02 p.m. UTC | #3
On Wed, Jul 01, 2020 at 09:47:14AM +0100, Christoph Hellwig wrote:
> >  #define XFS_DQ_ALLTYPES		(XFS_DQ_USER|XFS_DQ_PROJ|XFS_DQ_GROUP)
> >  
> > +#define XFS_DQ_ONDISK		(XFS_DQ_ALLTYPES)
> > +
> 
> I really wonder if we should split the on-disk type and the in-core
> flags properly instead.
> 
> That is propagate the u8 flags from the on-disk field directly,
> and use a separate field for the in-memory flags dirty and freeing
> flags, as that this kind of mixing up is bound to eventually create
> problems.

I was already half-inclined to try to separate them anyway, guess I'll
add that to this series.

--D
Allison Henderson July 1, 2020, 8:15 p.m. UTC | #4
On 6/30/20 8:42 AM, Darrick J. Wong wrote:
> From: Darrick J. Wong <darrick.wong@oracle.com>
> 
> Use the incore dq_flags to figure out the dquot type.  This is the first
> step towards removing xfs_disk_dquot from the incore dquot.
> 
> Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>

Ok, seems reasonable
Reviewed-by: Allison Collins <allison.henderson@oracle.com>

> ---
>   fs/xfs/libxfs/xfs_quota_defs.h |    2 ++
>   fs/xfs/scrub/quota.c           |    4 ----
>   fs/xfs/xfs_dquot.c             |   33 +++++++++++++++++++++++++++++++--
>   fs/xfs/xfs_dquot.h             |    2 ++
>   fs/xfs/xfs_dquot_item.c        |    6 ++++--
>   fs/xfs/xfs_qm.c                |    4 ++--
>   fs/xfs/xfs_qm.h                |    2 +-
>   fs/xfs/xfs_qm_syscalls.c       |    9 +++------
>   8 files changed, 45 insertions(+), 17 deletions(-)
> 
> 
> diff --git a/fs/xfs/libxfs/xfs_quota_defs.h b/fs/xfs/libxfs/xfs_quota_defs.h
> index 56d9dd787e7b..459023b0a304 100644
> --- a/fs/xfs/libxfs/xfs_quota_defs.h
> +++ b/fs/xfs/libxfs/xfs_quota_defs.h
> @@ -29,6 +29,8 @@ typedef uint16_t	xfs_qwarncnt_t;
>   
>   #define XFS_DQ_ALLTYPES		(XFS_DQ_USER|XFS_DQ_PROJ|XFS_DQ_GROUP)
>   
> +#define XFS_DQ_ONDISK		(XFS_DQ_ALLTYPES)
> +
>   #define XFS_DQ_FLAGS \
>   	{ XFS_DQ_USER,		"USER" }, \
>   	{ XFS_DQ_PROJ,		"PROJ" }, \
> diff --git a/fs/xfs/scrub/quota.c b/fs/xfs/scrub/quota.c
> index 905a34558361..710659d3fa28 100644
> --- a/fs/xfs/scrub/quota.c
> +++ b/fs/xfs/scrub/quota.c
> @@ -108,10 +108,6 @@ xchk_quota_item(
>   
>   	sqi->last_id = id;
>   
> -	/* Did we get the dquot type we wanted? */
> -	if (dqtype != (d->d_flags & XFS_DQ_ALLTYPES))
> -		xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
> -
>   	if (d->d_pad0 != cpu_to_be32(0) || d->d_pad != cpu_to_be16(0))
>   		xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
>   
> diff --git a/fs/xfs/xfs_dquot.c b/fs/xfs/xfs_dquot.c
> index 46c8ca83c04d..59d1bce34a98 100644
> --- a/fs/xfs/xfs_dquot.c
> +++ b/fs/xfs/xfs_dquot.c
> @@ -561,6 +561,16 @@ xfs_dquot_from_disk(
>   	return 0;
>   }
>   
> +/* Copy the in-core quota fields into the on-disk buffer. */
> +void
> +xfs_dquot_to_disk(
> +	struct xfs_disk_dquot	*ddqp,
> +	struct xfs_dquot	*dqp)
> +{
> +	memcpy(ddqp, &dqp->q_core, sizeof(struct xfs_disk_dquot));
> +	ddqp->d_flags = dqp->dq_flags & XFS_DQ_ONDISK;
> +}
> +
>   /* Allocate and initialize the dquot buffer for this in-core dquot. */
>   static int
>   xfs_qm_dqread_alloc(
> @@ -1108,6 +1118,17 @@ xfs_qm_dqflush_done(
>   	xfs_dqfunlock(dqp);
>   }
>   
> +/* Check incore dquot for errors before we flush. */
> +static xfs_failaddr_t
> +xfs_qm_dqflush_check(
> +	struct xfs_dquot	*dqp)
> +{
> +	if (hweight8(dqp->dq_flags & XFS_DQ_ALLTYPES) != 1)
> +		return __this_address;
> +
> +	return NULL;
> +}
> +
>   /*
>    * Write a modified dquot to disk.
>    * The dquot must be locked and the flush lock too taken by caller.
> @@ -1166,8 +1187,16 @@ xfs_qm_dqflush(
>   		goto out_abort;
>   	}
>   
> -	/* This is the only portion of data that needs to persist */
> -	memcpy(ddqp, &dqp->q_core, sizeof(struct xfs_disk_dquot));
> +	fa = xfs_qm_dqflush_check(dqp);
> +	if (fa) {
> +		xfs_alert(mp, "corrupt dquot ID 0x%x in memory at %pS",
> +				be32_to_cpu(dqp->q_core.d_id), fa);
> +		xfs_buf_relse(bp);
> +		error = -EFSCORRUPTED;
> +		goto out_abort;
> +	}
> +
> +	xfs_dquot_to_disk(ddqp, dqp);
>   
>   	/*
>   	 * Clear the dirty field and remember the flush lsn for later use.
> diff --git a/fs/xfs/xfs_dquot.h b/fs/xfs/xfs_dquot.h
> index 71e36c85e20b..1b1a4261a580 100644
> --- a/fs/xfs/xfs_dquot.h
> +++ b/fs/xfs/xfs_dquot.h
> @@ -144,6 +144,8 @@ static inline bool xfs_dquot_lowsp(struct xfs_dquot *dqp)
>   	return false;
>   }
>   
> +void xfs_dquot_to_disk(struct xfs_disk_dquot *ddqp, struct xfs_dquot *dqp);
> +
>   #define XFS_DQ_IS_LOCKED(dqp)	(mutex_is_locked(&((dqp)->q_qlock)))
>   #define XFS_DQ_IS_DIRTY(dqp)	((dqp)->dq_flags & XFS_DQ_DIRTY)
>   #define XFS_QM_ISUDQ(dqp)	((dqp)->dq_flags & XFS_DQ_USER)
> diff --git a/fs/xfs/xfs_dquot_item.c b/fs/xfs/xfs_dquot_item.c
> index 349c92d26570..ff0ab65cf413 100644
> --- a/fs/xfs/xfs_dquot_item.c
> +++ b/fs/xfs/xfs_dquot_item.c
> @@ -45,6 +45,7 @@ xfs_qm_dquot_logitem_format(
>   	struct xfs_log_item	*lip,
>   	struct xfs_log_vec	*lv)
>   {
> +	struct xfs_disk_dquot	ddq;
>   	struct xfs_dq_logitem	*qlip = DQUOT_ITEM(lip);
>   	struct xfs_log_iovec	*vecp = NULL;
>   	struct xfs_dq_logformat	*qlf;
> @@ -58,8 +59,9 @@ xfs_qm_dquot_logitem_format(
>   	qlf->qlf_boffset = qlip->qli_dquot->q_bufoffset;
>   	xlog_finish_iovec(lv, vecp, sizeof(struct xfs_dq_logformat));
>   
> -	xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_DQUOT,
> -			&qlip->qli_dquot->q_core,
> +	xfs_dquot_to_disk(&ddq, qlip->qli_dquot);
> +
> +	xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_DQUOT, &ddq,
>   			sizeof(struct xfs_disk_dquot));
>   }
>   
> diff --git a/fs/xfs/xfs_qm.c b/fs/xfs/xfs_qm.c
> index 938023dd8ce5..632025c2f00b 100644
> --- a/fs/xfs/xfs_qm.c
> +++ b/fs/xfs/xfs_qm.c
> @@ -161,7 +161,7 @@ xfs_qm_dqpurge(
>   	xfs_dqfunlock(dqp);
>   	xfs_dqunlock(dqp);
>   
> -	radix_tree_delete(xfs_dquot_tree(qi, dqp->q_core.d_flags),
> +	radix_tree_delete(xfs_dquot_tree(qi, dqp->dq_flags),
>   			  be32_to_cpu(dqp->q_core.d_id));
>   	qi->qi_dquots--;
>   
> @@ -1598,7 +1598,7 @@ xfs_qm_dqfree_one(
>   	struct xfs_quotainfo	*qi = mp->m_quotainfo;
>   
>   	mutex_lock(&qi->qi_tree_lock);
> -	radix_tree_delete(xfs_dquot_tree(qi, dqp->q_core.d_flags),
> +	radix_tree_delete(xfs_dquot_tree(qi, dqp->dq_flags),
>   			  be32_to_cpu(dqp->q_core.d_id));
>   
>   	qi->qi_dquots--;
> diff --git a/fs/xfs/xfs_qm.h b/fs/xfs/xfs_qm.h
> index 7b0e771fcbce..43b4650cdcdf 100644
> --- a/fs/xfs/xfs_qm.h
> +++ b/fs/xfs/xfs_qm.h
> @@ -85,7 +85,7 @@ xfs_dquot_tree(
>   	struct xfs_quotainfo	*qi,
>   	int			type)
>   {
> -	switch (type) {
> +	switch (type & XFS_DQ_ALLTYPES) {
>   	case XFS_DQ_USER:
>   		return &qi->qi_uquota_tree;
>   	case XFS_DQ_GROUP:
> diff --git a/fs/xfs/xfs_qm_syscalls.c b/fs/xfs/xfs_qm_syscalls.c
> index 7effd7a28136..8cbb65f01bf1 100644
> --- a/fs/xfs/xfs_qm_syscalls.c
> +++ b/fs/xfs/xfs_qm_syscalls.c
> @@ -644,12 +644,9 @@ xfs_qm_scall_getquota_fill_qc(
>   	 * gets turned off. No need to confuse the user level code,
>   	 * so return zeroes in that case.
>   	 */
> -	if ((!XFS_IS_UQUOTA_ENFORCED(mp) &&
> -	     dqp->q_core.d_flags == XFS_DQ_USER) ||
> -	    (!XFS_IS_GQUOTA_ENFORCED(mp) &&
> -	     dqp->q_core.d_flags == XFS_DQ_GROUP) ||
> -	    (!XFS_IS_PQUOTA_ENFORCED(mp) &&
> -	     dqp->q_core.d_flags == XFS_DQ_PROJ)) {
> +	if ((!XFS_IS_UQUOTA_ENFORCED(mp) && (dqp->dq_flags & XFS_DQ_USER)) ||
> +	    (!XFS_IS_GQUOTA_ENFORCED(mp) && (dqp->dq_flags & XFS_DQ_GROUP)) ||
> +	    (!XFS_IS_PQUOTA_ENFORCED(mp) && (dqp->dq_flags & XFS_DQ_PROJ))) {
>   		dst->d_spc_timer = 0;
>   		dst->d_ino_timer = 0;
>   		dst->d_rt_spc_timer = 0;
>
Dave Chinner July 1, 2020, 10:50 p.m. UTC | #5
On Tue, Jun 30, 2020 at 08:42:16AM -0700, Darrick J. Wong wrote:
> From: Darrick J. Wong <darrick.wong@oracle.com>
> 
> Use the incore dq_flags to figure out the dquot type.  This is the first
> step towards removing xfs_disk_dquot from the incore dquot.
> 
> Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
> ---
>  fs/xfs/libxfs/xfs_quota_defs.h |    2 ++
>  fs/xfs/scrub/quota.c           |    4 ----
>  fs/xfs/xfs_dquot.c             |   33 +++++++++++++++++++++++++++++++--
>  fs/xfs/xfs_dquot.h             |    2 ++
>  fs/xfs/xfs_dquot_item.c        |    6 ++++--
>  fs/xfs/xfs_qm.c                |    4 ++--
>  fs/xfs/xfs_qm.h                |    2 +-
>  fs/xfs/xfs_qm_syscalls.c       |    9 +++------
>  8 files changed, 45 insertions(+), 17 deletions(-)
> 
> 
> diff --git a/fs/xfs/libxfs/xfs_quota_defs.h b/fs/xfs/libxfs/xfs_quota_defs.h
> index 56d9dd787e7b..459023b0a304 100644
> --- a/fs/xfs/libxfs/xfs_quota_defs.h
> +++ b/fs/xfs/libxfs/xfs_quota_defs.h
> @@ -29,6 +29,8 @@ typedef uint16_t	xfs_qwarncnt_t;
>  
>  #define XFS_DQ_ALLTYPES		(XFS_DQ_USER|XFS_DQ_PROJ|XFS_DQ_GROUP)
>  
> +#define XFS_DQ_ONDISK		(XFS_DQ_ALLTYPES)

That's used as an on-disk flags mask. Perhaps XFS_DQF_ONDISK_MASK?

> +
>  #define XFS_DQ_FLAGS \
>  	{ XFS_DQ_USER,		"USER" }, \
>  	{ XFS_DQ_PROJ,		"PROJ" }, \
> diff --git a/fs/xfs/scrub/quota.c b/fs/xfs/scrub/quota.c
> index 905a34558361..710659d3fa28 100644
> --- a/fs/xfs/scrub/quota.c
> +++ b/fs/xfs/scrub/quota.c
> @@ -108,10 +108,6 @@ xchk_quota_item(
>  
>  	sqi->last_id = id;
>  
> -	/* Did we get the dquot type we wanted? */
> -	if (dqtype != (d->d_flags & XFS_DQ_ALLTYPES))
> -		xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
> -
>  	if (d->d_pad0 != cpu_to_be32(0) || d->d_pad != cpu_to_be16(0))
>  		xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
>  
> diff --git a/fs/xfs/xfs_dquot.c b/fs/xfs/xfs_dquot.c
> index 46c8ca83c04d..59d1bce34a98 100644
> --- a/fs/xfs/xfs_dquot.c
> +++ b/fs/xfs/xfs_dquot.c
> @@ -561,6 +561,16 @@ xfs_dquot_from_disk(
>  	return 0;
>  }
>  
> +/* Copy the in-core quota fields into the on-disk buffer. */
> +void
> +xfs_dquot_to_disk(
> +	struct xfs_disk_dquot	*ddqp,
> +	struct xfs_dquot	*dqp)
> +{
> +	memcpy(ddqp, &dqp->q_core, sizeof(struct xfs_disk_dquot));
> +	ddqp->d_flags = dqp->dq_flags & XFS_DQ_ONDISK;
> +}
> +
>  /* Allocate and initialize the dquot buffer for this in-core dquot. */
>  static int
>  xfs_qm_dqread_alloc(
> @@ -1108,6 +1118,17 @@ xfs_qm_dqflush_done(
>  	xfs_dqfunlock(dqp);
>  }
>  
> +/* Check incore dquot for errors before we flush. */
> +static xfs_failaddr_t
> +xfs_qm_dqflush_check(
> +	struct xfs_dquot	*dqp)
> +{
> +	if (hweight8(dqp->dq_flags & XFS_DQ_ALLTYPES) != 1)
> +		return __this_address;

This only checks the low 8 bits in dq_flags, which is a 32 bit
field. If we ever renumber the dq flags and the dquot types end up
outside the LSB, this code will break.

I don't really see a need to micro-optimise the code so much it
leaves landmines like this in the code...

Cheers,

Dave.
Darrick J. Wong July 1, 2020, 11:19 p.m. UTC | #6
On Thu, Jul 02, 2020 at 08:50:53AM +1000, Dave Chinner wrote:
> On Tue, Jun 30, 2020 at 08:42:16AM -0700, Darrick J. Wong wrote:
> > From: Darrick J. Wong <darrick.wong@oracle.com>
> > 
> > Use the incore dq_flags to figure out the dquot type.  This is the first
> > step towards removing xfs_disk_dquot from the incore dquot.
> > 
> > Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
> > ---
> >  fs/xfs/libxfs/xfs_quota_defs.h |    2 ++
> >  fs/xfs/scrub/quota.c           |    4 ----
> >  fs/xfs/xfs_dquot.c             |   33 +++++++++++++++++++++++++++++++--
> >  fs/xfs/xfs_dquot.h             |    2 ++
> >  fs/xfs/xfs_dquot_item.c        |    6 ++++--
> >  fs/xfs/xfs_qm.c                |    4 ++--
> >  fs/xfs/xfs_qm.h                |    2 +-
> >  fs/xfs/xfs_qm_syscalls.c       |    9 +++------
> >  8 files changed, 45 insertions(+), 17 deletions(-)
> > 
> > 
> > diff --git a/fs/xfs/libxfs/xfs_quota_defs.h b/fs/xfs/libxfs/xfs_quota_defs.h
> > index 56d9dd787e7b..459023b0a304 100644
> > --- a/fs/xfs/libxfs/xfs_quota_defs.h
> > +++ b/fs/xfs/libxfs/xfs_quota_defs.h
> > @@ -29,6 +29,8 @@ typedef uint16_t	xfs_qwarncnt_t;
> >  
> >  #define XFS_DQ_ALLTYPES		(XFS_DQ_USER|XFS_DQ_PROJ|XFS_DQ_GROUP)
> >  
> > +#define XFS_DQ_ONDISK		(XFS_DQ_ALLTYPES)
> 
> That's used as an on-disk flags mask. Perhaps XFS_DQF_ONDISK_MASK?

Well, based on Christoph's suggestions I broke the incore dquot flags
(XFS_DQ_*) apart from the ondisk dquot flags (XFS_DQFLAG_*).  Not sure
if that's really better, but at least the namespaces are separate now.

> > +
> >  #define XFS_DQ_FLAGS \
> >  	{ XFS_DQ_USER,		"USER" }, \
> >  	{ XFS_DQ_PROJ,		"PROJ" }, \
> > diff --git a/fs/xfs/scrub/quota.c b/fs/xfs/scrub/quota.c
> > index 905a34558361..710659d3fa28 100644
> > --- a/fs/xfs/scrub/quota.c
> > +++ b/fs/xfs/scrub/quota.c
> > @@ -108,10 +108,6 @@ xchk_quota_item(
> >  
> >  	sqi->last_id = id;
> >  
> > -	/* Did we get the dquot type we wanted? */
> > -	if (dqtype != (d->d_flags & XFS_DQ_ALLTYPES))
> > -		xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
> > -
> >  	if (d->d_pad0 != cpu_to_be32(0) || d->d_pad != cpu_to_be16(0))
> >  		xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
> >  
> > diff --git a/fs/xfs/xfs_dquot.c b/fs/xfs/xfs_dquot.c
> > index 46c8ca83c04d..59d1bce34a98 100644
> > --- a/fs/xfs/xfs_dquot.c
> > +++ b/fs/xfs/xfs_dquot.c
> > @@ -561,6 +561,16 @@ xfs_dquot_from_disk(
> >  	return 0;
> >  }
> >  
> > +/* Copy the in-core quota fields into the on-disk buffer. */
> > +void
> > +xfs_dquot_to_disk(
> > +	struct xfs_disk_dquot	*ddqp,
> > +	struct xfs_dquot	*dqp)
> > +{
> > +	memcpy(ddqp, &dqp->q_core, sizeof(struct xfs_disk_dquot));
> > +	ddqp->d_flags = dqp->dq_flags & XFS_DQ_ONDISK;
> > +}
> > +
> >  /* Allocate and initialize the dquot buffer for this in-core dquot. */
> >  static int
> >  xfs_qm_dqread_alloc(
> > @@ -1108,6 +1118,17 @@ xfs_qm_dqflush_done(
> >  	xfs_dqfunlock(dqp);
> >  }
> >  
> > +/* Check incore dquot for errors before we flush. */
> > +static xfs_failaddr_t
> > +xfs_qm_dqflush_check(
> > +	struct xfs_dquot	*dqp)
> > +{
> > +	if (hweight8(dqp->dq_flags & XFS_DQ_ALLTYPES) != 1)
> > +		return __this_address;
> 
> This only checks the low 8 bits in dq_flags, which is a 32 bit
> field. If we ever renumber the dq flags and the dquot types end up
> outside the LSB, this code will break.
> 
> I don't really see a need to micro-optimise the code so much it
> leaves landmines like this in the code...

Ok. Fixed.

--D

> Cheers,
> 
> Dave.
> -- 
> Dave Chinner
> david@fromorbit.com
Dave Chinner July 1, 2020, 11:44 p.m. UTC | #7
On Wed, Jul 01, 2020 at 04:19:10PM -0700, Darrick J. Wong wrote:
> On Thu, Jul 02, 2020 at 08:50:53AM +1000, Dave Chinner wrote:
> > On Tue, Jun 30, 2020 at 08:42:16AM -0700, Darrick J. Wong wrote:
> > > From: Darrick J. Wong <darrick.wong@oracle.com>
> > > 
> > > Use the incore dq_flags to figure out the dquot type.  This is the first
> > > step towards removing xfs_disk_dquot from the incore dquot.
> > > 
> > > Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
> > > ---
> > >  fs/xfs/libxfs/xfs_quota_defs.h |    2 ++
> > >  fs/xfs/scrub/quota.c           |    4 ----
> > >  fs/xfs/xfs_dquot.c             |   33 +++++++++++++++++++++++++++++++--
> > >  fs/xfs/xfs_dquot.h             |    2 ++
> > >  fs/xfs/xfs_dquot_item.c        |    6 ++++--
> > >  fs/xfs/xfs_qm.c                |    4 ++--
> > >  fs/xfs/xfs_qm.h                |    2 +-
> > >  fs/xfs/xfs_qm_syscalls.c       |    9 +++------
> > >  8 files changed, 45 insertions(+), 17 deletions(-)
> > > 
> > > 
> > > diff --git a/fs/xfs/libxfs/xfs_quota_defs.h b/fs/xfs/libxfs/xfs_quota_defs.h
> > > index 56d9dd787e7b..459023b0a304 100644
> > > --- a/fs/xfs/libxfs/xfs_quota_defs.h
> > > +++ b/fs/xfs/libxfs/xfs_quota_defs.h
> > > @@ -29,6 +29,8 @@ typedef uint16_t	xfs_qwarncnt_t;
> > >  
> > >  #define XFS_DQ_ALLTYPES		(XFS_DQ_USER|XFS_DQ_PROJ|XFS_DQ_GROUP)
> > >  
> > > +#define XFS_DQ_ONDISK		(XFS_DQ_ALLTYPES)
> > 
> > That's used as an on-disk flags mask. Perhaps XFS_DQF_ONDISK_MASK?
> 
> Well, based on Christoph's suggestions I broke the incore dquot flags
> (XFS_DQ_*) apart from the ondisk dquot flags (XFS_DQFLAG_*).  Not sure
> if that's really better, but at least the namespaces are separate now.

Sure, but the point I was trying to make is that "XFS_DQ_ONDISK"
doesn't actually indicate what part of the on-disk dquot it refers
to. We use the phrase "on-disk dquot" to refer to the entire on-disk
dquot, not a subset of flags in a flags field in the on-disk
dquot. Hence the name of this variable needs to be more specific as
to what it applies to in the on-disk dquot...

Cheers,

Dave.
Darrick J. Wong July 1, 2020, 11:50 p.m. UTC | #8
On Thu, Jul 02, 2020 at 09:44:35AM +1000, Dave Chinner wrote:
> On Wed, Jul 01, 2020 at 04:19:10PM -0700, Darrick J. Wong wrote:
> > On Thu, Jul 02, 2020 at 08:50:53AM +1000, Dave Chinner wrote:
> > > On Tue, Jun 30, 2020 at 08:42:16AM -0700, Darrick J. Wong wrote:
> > > > From: Darrick J. Wong <darrick.wong@oracle.com>
> > > > 
> > > > Use the incore dq_flags to figure out the dquot type.  This is the first
> > > > step towards removing xfs_disk_dquot from the incore dquot.
> > > > 
> > > > Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
> > > > ---
> > > >  fs/xfs/libxfs/xfs_quota_defs.h |    2 ++
> > > >  fs/xfs/scrub/quota.c           |    4 ----
> > > >  fs/xfs/xfs_dquot.c             |   33 +++++++++++++++++++++++++++++++--
> > > >  fs/xfs/xfs_dquot.h             |    2 ++
> > > >  fs/xfs/xfs_dquot_item.c        |    6 ++++--
> > > >  fs/xfs/xfs_qm.c                |    4 ++--
> > > >  fs/xfs/xfs_qm.h                |    2 +-
> > > >  fs/xfs/xfs_qm_syscalls.c       |    9 +++------
> > > >  8 files changed, 45 insertions(+), 17 deletions(-)
> > > > 
> > > > 
> > > > diff --git a/fs/xfs/libxfs/xfs_quota_defs.h b/fs/xfs/libxfs/xfs_quota_defs.h
> > > > index 56d9dd787e7b..459023b0a304 100644
> > > > --- a/fs/xfs/libxfs/xfs_quota_defs.h
> > > > +++ b/fs/xfs/libxfs/xfs_quota_defs.h
> > > > @@ -29,6 +29,8 @@ typedef uint16_t	xfs_qwarncnt_t;
> > > >  
> > > >  #define XFS_DQ_ALLTYPES		(XFS_DQ_USER|XFS_DQ_PROJ|XFS_DQ_GROUP)
> > > >  
> > > > +#define XFS_DQ_ONDISK		(XFS_DQ_ALLTYPES)
> > > 
> > > That's used as an on-disk flags mask. Perhaps XFS_DQF_ONDISK_MASK?
> > 
> > Well, based on Christoph's suggestions I broke the incore dquot flags
> > (XFS_DQ_*) apart from the ondisk dquot flags (XFS_DQFLAG_*).  Not sure
> > if that's really better, but at least the namespaces are separate now.
> 
> Sure, but the point I was trying to make is that "XFS_DQ_ONDISK"
> doesn't actually indicate what part of the on-disk dquot it refers
> to. We use the phrase "on-disk dquot" to refer to the entire on-disk
> dquot, not a subset of flags in a flags field in the on-disk
> dquot. Hence the name of this variable needs to be more specific as
> to what it applies to in the on-disk dquot...

Sorry, I was typing too fast.  xfs_format.h now has:

#define XFS_DQFLAG_USER		0x01		/* user dquot record */
#define XFS_DQFLAG_PROJ		0x02		/* project dquot record */
#define XFS_DQFLAG_GROUP	0x04		/* group dquot record */

#define XFS_DQFLAG_TYPE_MASK	(XFS_DQFLAG_USER | \
				 XFS_DQFLAG_PROJ | \
				 XFS_DQFLAG_GROUP)

#define XFS_DQFLAG_ALL		(XFS_DQFLAG_TYPE_MASK)

/*
 * This is the main portion of the on-disk representation of quota
 * information for a user. This is the q_core of the struct xfs_dquot
 * that is kept in kernel memory. We pad this with some more expansion
 * room to construct the on disk structure.
 */
struct xfs_disk_dquot {
	__be16		d_magic;	/* dquot magic = XFS_DQUOT_MAGIC */
	__u8		d_version;	/* dquot version */
	__u8		d_flags;	/* XFS_DQFLAG_* */

I'm not particularly thrilled about the DQFLAG/DQ thing though.  DDFLAG?

(Also note that the future bigtime series will add a new ondisk flag
XFS_DQFLAG_BIGTIME, which ofc will get added to XFS_DQFLAG_ALL.)

--D

> Cheers,
> 
> Dave.
> -- 
> Dave Chinner
> david@fromorbit.com
Dave Chinner July 3, 2020, 12:58 a.m. UTC | #9
On Wed, Jul 01, 2020 at 04:50:31PM -0700, Darrick J. Wong wrote:
> On Thu, Jul 02, 2020 at 09:44:35AM +1000, Dave Chinner wrote:
> > On Wed, Jul 01, 2020 at 04:19:10PM -0700, Darrick J. Wong wrote:
> > > On Thu, Jul 02, 2020 at 08:50:53AM +1000, Dave Chinner wrote:
> > > > On Tue, Jun 30, 2020 at 08:42:16AM -0700, Darrick J. Wong wrote:
> > > > > From: Darrick J. Wong <darrick.wong@oracle.com>
> > > > > 
> > > > > Use the incore dq_flags to figure out the dquot type.  This is the first
> > > > > step towards removing xfs_disk_dquot from the incore dquot.
> > > > > 
> > > > > Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
> > > > > ---
> > > > >  fs/xfs/libxfs/xfs_quota_defs.h |    2 ++
> > > > >  fs/xfs/scrub/quota.c           |    4 ----
> > > > >  fs/xfs/xfs_dquot.c             |   33 +++++++++++++++++++++++++++++++--
> > > > >  fs/xfs/xfs_dquot.h             |    2 ++
> > > > >  fs/xfs/xfs_dquot_item.c        |    6 ++++--
> > > > >  fs/xfs/xfs_qm.c                |    4 ++--
> > > > >  fs/xfs/xfs_qm.h                |    2 +-
> > > > >  fs/xfs/xfs_qm_syscalls.c       |    9 +++------
> > > > >  8 files changed, 45 insertions(+), 17 deletions(-)
> > > > > 
> > > > > 
> > > > > diff --git a/fs/xfs/libxfs/xfs_quota_defs.h b/fs/xfs/libxfs/xfs_quota_defs.h
> > > > > index 56d9dd787e7b..459023b0a304 100644
> > > > > --- a/fs/xfs/libxfs/xfs_quota_defs.h
> > > > > +++ b/fs/xfs/libxfs/xfs_quota_defs.h
> > > > > @@ -29,6 +29,8 @@ typedef uint16_t	xfs_qwarncnt_t;
> > > > >  
> > > > >  #define XFS_DQ_ALLTYPES		(XFS_DQ_USER|XFS_DQ_PROJ|XFS_DQ_GROUP)
> > > > >  
> > > > > +#define XFS_DQ_ONDISK		(XFS_DQ_ALLTYPES)
> > > > 
> > > > That's used as an on-disk flags mask. Perhaps XFS_DQF_ONDISK_MASK?
> > > 
> > > Well, based on Christoph's suggestions I broke the incore dquot flags
> > > (XFS_DQ_*) apart from the ondisk dquot flags (XFS_DQFLAG_*).  Not sure
> > > if that's really better, but at least the namespaces are separate now.
> > 
> > Sure, but the point I was trying to make is that "XFS_DQ_ONDISK"
> > doesn't actually indicate what part of the on-disk dquot it refers
> > to. We use the phrase "on-disk dquot" to refer to the entire on-disk
> > dquot, not a subset of flags in a flags field in the on-disk
> > dquot. Hence the name of this variable needs to be more specific as
> > to what it applies to in the on-disk dquot...
> 
> Sorry, I was typing too fast.  xfs_format.h now has:
> 
> #define XFS_DQFLAG_USER		0x01		/* user dquot record */
> #define XFS_DQFLAG_PROJ		0x02		/* project dquot record */
> #define XFS_DQFLAG_GROUP	0x04		/* group dquot record */
> 
> #define XFS_DQFLAG_TYPE_MASK	(XFS_DQFLAG_USER | \
> 				 XFS_DQFLAG_PROJ | \
> 				 XFS_DQFLAG_GROUP)
> 
> #define XFS_DQFLAG_ALL		(XFS_DQFLAG_TYPE_MASK)
> 
> /*
>  * This is the main portion of the on-disk representation of quota
>  * information for a user. This is the q_core of the struct xfs_dquot
>  * that is kept in kernel memory. We pad this with some more expansion
>  * room to construct the on disk structure.
>  */
> struct xfs_disk_dquot {
> 	__be16		d_magic;	/* dquot magic = XFS_DQUOT_MAGIC */
> 	__u8		d_version;	/* dquot version */
> 	__u8		d_flags;	/* XFS_DQFLAG_* */
> 
> I'm not particularly thrilled about the DQFLAG/DQ thing though.  DDFLAG?
> (Also note that the future bigtime series will add a new ondisk flag
> XFS_DQFLAG_BIGTIME, which ofc will get added to XFS_DQFLAG_ALL.)

/me shrugs

I don't have any good ideas, but I think that DQFLAG is a bad choice
for an on-disk flag namespace because it's way too generic. It's
more a type/feature indicator so perhaps we should rename d_flags to
d_type or d_features and use:

#define XFS_DDQTYPE_USER
#define XFS_DDQTYPE_PROJ
#define XFS_DDQTYPE_GROUP
#define XFS_DDQTYPE_BIGTIME

#define XFS_DDQTYPE_QUOTA_MASK	(XFS_DDQTYPE_USER | ...

#define XFS_DDQTYPE_ALL		(XFS_DDQTYPE_QUOTA_MASK | XFS_DDQTYPE_BIGTIME)

Or something like that...

Cheers,

Dave.
Darrick J. Wong July 3, 2020, 6:01 p.m. UTC | #10
On Fri, Jul 03, 2020 at 10:58:50AM +1000, Dave Chinner wrote:
> On Wed, Jul 01, 2020 at 04:50:31PM -0700, Darrick J. Wong wrote:
> > On Thu, Jul 02, 2020 at 09:44:35AM +1000, Dave Chinner wrote:
> > > On Wed, Jul 01, 2020 at 04:19:10PM -0700, Darrick J. Wong wrote:
> > > > On Thu, Jul 02, 2020 at 08:50:53AM +1000, Dave Chinner wrote:
> > > > > On Tue, Jun 30, 2020 at 08:42:16AM -0700, Darrick J. Wong wrote:
> > > > > > From: Darrick J. Wong <darrick.wong@oracle.com>
> > > > > > 
> > > > > > Use the incore dq_flags to figure out the dquot type.  This is the first
> > > > > > step towards removing xfs_disk_dquot from the incore dquot.
> > > > > > 
> > > > > > Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
> > > > > > ---
> > > > > >  fs/xfs/libxfs/xfs_quota_defs.h |    2 ++
> > > > > >  fs/xfs/scrub/quota.c           |    4 ----
> > > > > >  fs/xfs/xfs_dquot.c             |   33 +++++++++++++++++++++++++++++++--
> > > > > >  fs/xfs/xfs_dquot.h             |    2 ++
> > > > > >  fs/xfs/xfs_dquot_item.c        |    6 ++++--
> > > > > >  fs/xfs/xfs_qm.c                |    4 ++--
> > > > > >  fs/xfs/xfs_qm.h                |    2 +-
> > > > > >  fs/xfs/xfs_qm_syscalls.c       |    9 +++------
> > > > > >  8 files changed, 45 insertions(+), 17 deletions(-)
> > > > > > 
> > > > > > 
> > > > > > diff --git a/fs/xfs/libxfs/xfs_quota_defs.h b/fs/xfs/libxfs/xfs_quota_defs.h
> > > > > > index 56d9dd787e7b..459023b0a304 100644
> > > > > > --- a/fs/xfs/libxfs/xfs_quota_defs.h
> > > > > > +++ b/fs/xfs/libxfs/xfs_quota_defs.h
> > > > > > @@ -29,6 +29,8 @@ typedef uint16_t	xfs_qwarncnt_t;
> > > > > >  
> > > > > >  #define XFS_DQ_ALLTYPES		(XFS_DQ_USER|XFS_DQ_PROJ|XFS_DQ_GROUP)
> > > > > >  
> > > > > > +#define XFS_DQ_ONDISK		(XFS_DQ_ALLTYPES)
> > > > > 
> > > > > That's used as an on-disk flags mask. Perhaps XFS_DQF_ONDISK_MASK?
> > > > 
> > > > Well, based on Christoph's suggestions I broke the incore dquot flags
> > > > (XFS_DQ_*) apart from the ondisk dquot flags (XFS_DQFLAG_*).  Not sure
> > > > if that's really better, but at least the namespaces are separate now.
> > > 
> > > Sure, but the point I was trying to make is that "XFS_DQ_ONDISK"
> > > doesn't actually indicate what part of the on-disk dquot it refers
> > > to. We use the phrase "on-disk dquot" to refer to the entire on-disk
> > > dquot, not a subset of flags in a flags field in the on-disk
> > > dquot. Hence the name of this variable needs to be more specific as
> > > to what it applies to in the on-disk dquot...
> > 
> > Sorry, I was typing too fast.  xfs_format.h now has:
> > 
> > #define XFS_DQFLAG_USER		0x01		/* user dquot record */
> > #define XFS_DQFLAG_PROJ		0x02		/* project dquot record */
> > #define XFS_DQFLAG_GROUP	0x04		/* group dquot record */
> > 
> > #define XFS_DQFLAG_TYPE_MASK	(XFS_DQFLAG_USER | \
> > 				 XFS_DQFLAG_PROJ | \
> > 				 XFS_DQFLAG_GROUP)
> > 
> > #define XFS_DQFLAG_ALL		(XFS_DQFLAG_TYPE_MASK)
> > 
> > /*
> >  * This is the main portion of the on-disk representation of quota
> >  * information for a user. This is the q_core of the struct xfs_dquot
> >  * that is kept in kernel memory. We pad this with some more expansion
> >  * room to construct the on disk structure.
> >  */
> > struct xfs_disk_dquot {
> > 	__be16		d_magic;	/* dquot magic = XFS_DQUOT_MAGIC */
> > 	__u8		d_version;	/* dquot version */
> > 	__u8		d_flags;	/* XFS_DQFLAG_* */
> > 
> > I'm not particularly thrilled about the DQFLAG/DQ thing though.  DDFLAG?
> > (Also note that the future bigtime series will add a new ondisk flag
> > XFS_DQFLAG_BIGTIME, which ofc will get added to XFS_DQFLAG_ALL.)
> 
> /me shrugs
> 
> I don't have any good ideas, but I think that DQFLAG is a bad choice
> for an on-disk flag namespace because it's way too generic. It's
> more a type/feature indicator so perhaps we should rename d_flags to
> d_type or d_features and use:
> 
> #define XFS_DDQTYPE_USER
> #define XFS_DDQTYPE_PROJ
> #define XFS_DDQTYPE_GROUP
> #define XFS_DDQTYPE_BIGTIME
> 
> #define XFS_DDQTYPE_QUOTA_MASK	(XFS_DDQTYPE_USER | ...
> 
> #define XFS_DDQTYPE_ALL		(XFS_DDQTYPE_QUOTA_MASK | XFS_DDQTYPE_BIGTIME)
> 
> Or something like that...

I prefer d_features/DDQFEAT over d_type/DDQTYPE because I don't want
people to mix up "ondisk dquot type (user/group/proj/bigtime)" with
"quota type (user/group/proj)".

--D

> Cheers,
> 
> Dave.
> -- 
> Dave Chinner
> david@fromorbit.com
diff mbox series

Patch

diff --git a/fs/xfs/libxfs/xfs_quota_defs.h b/fs/xfs/libxfs/xfs_quota_defs.h
index 56d9dd787e7b..459023b0a304 100644
--- a/fs/xfs/libxfs/xfs_quota_defs.h
+++ b/fs/xfs/libxfs/xfs_quota_defs.h
@@ -29,6 +29,8 @@  typedef uint16_t	xfs_qwarncnt_t;
 
 #define XFS_DQ_ALLTYPES		(XFS_DQ_USER|XFS_DQ_PROJ|XFS_DQ_GROUP)
 
+#define XFS_DQ_ONDISK		(XFS_DQ_ALLTYPES)
+
 #define XFS_DQ_FLAGS \
 	{ XFS_DQ_USER,		"USER" }, \
 	{ XFS_DQ_PROJ,		"PROJ" }, \
diff --git a/fs/xfs/scrub/quota.c b/fs/xfs/scrub/quota.c
index 905a34558361..710659d3fa28 100644
--- a/fs/xfs/scrub/quota.c
+++ b/fs/xfs/scrub/quota.c
@@ -108,10 +108,6 @@  xchk_quota_item(
 
 	sqi->last_id = id;
 
-	/* Did we get the dquot type we wanted? */
-	if (dqtype != (d->d_flags & XFS_DQ_ALLTYPES))
-		xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
-
 	if (d->d_pad0 != cpu_to_be32(0) || d->d_pad != cpu_to_be16(0))
 		xchk_fblock_set_corrupt(sc, XFS_DATA_FORK, offset);
 
diff --git a/fs/xfs/xfs_dquot.c b/fs/xfs/xfs_dquot.c
index 46c8ca83c04d..59d1bce34a98 100644
--- a/fs/xfs/xfs_dquot.c
+++ b/fs/xfs/xfs_dquot.c
@@ -561,6 +561,16 @@  xfs_dquot_from_disk(
 	return 0;
 }
 
+/* Copy the in-core quota fields into the on-disk buffer. */
+void
+xfs_dquot_to_disk(
+	struct xfs_disk_dquot	*ddqp,
+	struct xfs_dquot	*dqp)
+{
+	memcpy(ddqp, &dqp->q_core, sizeof(struct xfs_disk_dquot));
+	ddqp->d_flags = dqp->dq_flags & XFS_DQ_ONDISK;
+}
+
 /* Allocate and initialize the dquot buffer for this in-core dquot. */
 static int
 xfs_qm_dqread_alloc(
@@ -1108,6 +1118,17 @@  xfs_qm_dqflush_done(
 	xfs_dqfunlock(dqp);
 }
 
+/* Check incore dquot for errors before we flush. */
+static xfs_failaddr_t
+xfs_qm_dqflush_check(
+	struct xfs_dquot	*dqp)
+{
+	if (hweight8(dqp->dq_flags & XFS_DQ_ALLTYPES) != 1)
+		return __this_address;
+
+	return NULL;
+}
+
 /*
  * Write a modified dquot to disk.
  * The dquot must be locked and the flush lock too taken by caller.
@@ -1166,8 +1187,16 @@  xfs_qm_dqflush(
 		goto out_abort;
 	}
 
-	/* This is the only portion of data that needs to persist */
-	memcpy(ddqp, &dqp->q_core, sizeof(struct xfs_disk_dquot));
+	fa = xfs_qm_dqflush_check(dqp);
+	if (fa) {
+		xfs_alert(mp, "corrupt dquot ID 0x%x in memory at %pS",
+				be32_to_cpu(dqp->q_core.d_id), fa);
+		xfs_buf_relse(bp);
+		error = -EFSCORRUPTED;
+		goto out_abort;
+	}
+
+	xfs_dquot_to_disk(ddqp, dqp);
 
 	/*
 	 * Clear the dirty field and remember the flush lsn for later use.
diff --git a/fs/xfs/xfs_dquot.h b/fs/xfs/xfs_dquot.h
index 71e36c85e20b..1b1a4261a580 100644
--- a/fs/xfs/xfs_dquot.h
+++ b/fs/xfs/xfs_dquot.h
@@ -144,6 +144,8 @@  static inline bool xfs_dquot_lowsp(struct xfs_dquot *dqp)
 	return false;
 }
 
+void xfs_dquot_to_disk(struct xfs_disk_dquot *ddqp, struct xfs_dquot *dqp);
+
 #define XFS_DQ_IS_LOCKED(dqp)	(mutex_is_locked(&((dqp)->q_qlock)))
 #define XFS_DQ_IS_DIRTY(dqp)	((dqp)->dq_flags & XFS_DQ_DIRTY)
 #define XFS_QM_ISUDQ(dqp)	((dqp)->dq_flags & XFS_DQ_USER)
diff --git a/fs/xfs/xfs_dquot_item.c b/fs/xfs/xfs_dquot_item.c
index 349c92d26570..ff0ab65cf413 100644
--- a/fs/xfs/xfs_dquot_item.c
+++ b/fs/xfs/xfs_dquot_item.c
@@ -45,6 +45,7 @@  xfs_qm_dquot_logitem_format(
 	struct xfs_log_item	*lip,
 	struct xfs_log_vec	*lv)
 {
+	struct xfs_disk_dquot	ddq;
 	struct xfs_dq_logitem	*qlip = DQUOT_ITEM(lip);
 	struct xfs_log_iovec	*vecp = NULL;
 	struct xfs_dq_logformat	*qlf;
@@ -58,8 +59,9 @@  xfs_qm_dquot_logitem_format(
 	qlf->qlf_boffset = qlip->qli_dquot->q_bufoffset;
 	xlog_finish_iovec(lv, vecp, sizeof(struct xfs_dq_logformat));
 
-	xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_DQUOT,
-			&qlip->qli_dquot->q_core,
+	xfs_dquot_to_disk(&ddq, qlip->qli_dquot);
+
+	xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_DQUOT, &ddq,
 			sizeof(struct xfs_disk_dquot));
 }
 
diff --git a/fs/xfs/xfs_qm.c b/fs/xfs/xfs_qm.c
index 938023dd8ce5..632025c2f00b 100644
--- a/fs/xfs/xfs_qm.c
+++ b/fs/xfs/xfs_qm.c
@@ -161,7 +161,7 @@  xfs_qm_dqpurge(
 	xfs_dqfunlock(dqp);
 	xfs_dqunlock(dqp);
 
-	radix_tree_delete(xfs_dquot_tree(qi, dqp->q_core.d_flags),
+	radix_tree_delete(xfs_dquot_tree(qi, dqp->dq_flags),
 			  be32_to_cpu(dqp->q_core.d_id));
 	qi->qi_dquots--;
 
@@ -1598,7 +1598,7 @@  xfs_qm_dqfree_one(
 	struct xfs_quotainfo	*qi = mp->m_quotainfo;
 
 	mutex_lock(&qi->qi_tree_lock);
-	radix_tree_delete(xfs_dquot_tree(qi, dqp->q_core.d_flags),
+	radix_tree_delete(xfs_dquot_tree(qi, dqp->dq_flags),
 			  be32_to_cpu(dqp->q_core.d_id));
 
 	qi->qi_dquots--;
diff --git a/fs/xfs/xfs_qm.h b/fs/xfs/xfs_qm.h
index 7b0e771fcbce..43b4650cdcdf 100644
--- a/fs/xfs/xfs_qm.h
+++ b/fs/xfs/xfs_qm.h
@@ -85,7 +85,7 @@  xfs_dquot_tree(
 	struct xfs_quotainfo	*qi,
 	int			type)
 {
-	switch (type) {
+	switch (type & XFS_DQ_ALLTYPES) {
 	case XFS_DQ_USER:
 		return &qi->qi_uquota_tree;
 	case XFS_DQ_GROUP:
diff --git a/fs/xfs/xfs_qm_syscalls.c b/fs/xfs/xfs_qm_syscalls.c
index 7effd7a28136..8cbb65f01bf1 100644
--- a/fs/xfs/xfs_qm_syscalls.c
+++ b/fs/xfs/xfs_qm_syscalls.c
@@ -644,12 +644,9 @@  xfs_qm_scall_getquota_fill_qc(
 	 * gets turned off. No need to confuse the user level code,
 	 * so return zeroes in that case.
 	 */
-	if ((!XFS_IS_UQUOTA_ENFORCED(mp) &&
-	     dqp->q_core.d_flags == XFS_DQ_USER) ||
-	    (!XFS_IS_GQUOTA_ENFORCED(mp) &&
-	     dqp->q_core.d_flags == XFS_DQ_GROUP) ||
-	    (!XFS_IS_PQUOTA_ENFORCED(mp) &&
-	     dqp->q_core.d_flags == XFS_DQ_PROJ)) {
+	if ((!XFS_IS_UQUOTA_ENFORCED(mp) && (dqp->dq_flags & XFS_DQ_USER)) ||
+	    (!XFS_IS_GQUOTA_ENFORCED(mp) && (dqp->dq_flags & XFS_DQ_GROUP)) ||
+	    (!XFS_IS_PQUOTA_ENFORCED(mp) && (dqp->dq_flags & XFS_DQ_PROJ))) {
 		dst->d_spc_timer = 0;
 		dst->d_ino_timer = 0;
 		dst->d_rt_spc_timer = 0;