Blame SOURCES/xfsprogs-5.10.0-xfs-widen-ondisk-quota-expiration-timestamps-to-hand.patch

5d5cbe
From 06963ef0d4ff9a6736f699d1ca8ef5a0c194799b Mon Sep 17 00:00:00 2001
5d5cbe
From: "Darrick J. Wong" <darrick.wong@oracle.com>
5d5cbe
Date: Thu, 12 Nov 2020 16:49:42 -0500
5d5cbe
Subject: [PATCH] xfs: widen ondisk quota expiration timestamps to handle
5d5cbe
 y2038+
5d5cbe
5d5cbe
Source kernel commit: 4ea1ff3b49681af45a4a8c14baf7f0b3d11aa74a
5d5cbe
5d5cbe
Enable the bigtime feature for quota timers.  We decrease the accuracy
5d5cbe
of the timers to ~4s in exchange for being able to set timers up to the
5d5cbe
bigtime maximum.
5d5cbe
5d5cbe
Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
5d5cbe
Reviewed-by: Christoph Hellwig <hch@lst.de>
5d5cbe
Reviewed-by: Allison Collins <allison.henderson@oracle.com>
5d5cbe
Reviewed-by: Dave Chinner <dchinner@redhat.com>
5d5cbe
Signed-off-by: Eric Sandeen <sandeen@sandeen.net>
5d5cbe
---
5d5cbe
5d5cbe
NOTE: Again, slightly different macro names due to lack of the 
5d5cbe
quota type/flags split.
5d5cbe
5d5cbe
diff --git a/include/xfs_mount.h b/include/xfs_mount.h
5d5cbe
index 8651d6a..beb2328 100644
5d5cbe
--- a/include/xfs_mount.h
5d5cbe
+++ b/include/xfs_mount.h
5d5cbe
@@ -202,4 +202,8 @@ extern xfs_mount_t	*libxfs_mount (xfs_mount_t *, xfs_sb_t *,
5d5cbe
 extern void	libxfs_umount (xfs_mount_t *);
5d5cbe
 extern void	libxfs_rtmount_destroy (xfs_mount_t *);
5d5cbe
 
5d5cbe
+/* Dummy xfs_dquot so that libxfs compiles. */
5d5cbe
+struct xfs_dquot {
5d5cbe
+	int		q_type;
5d5cbe
+};
5d5cbe
 #endif	/* __XFS_MOUNT_H__ */
5d5cbe
diff --git a/libxfs/xfs_dquot_buf.c b/libxfs/xfs_dquot_buf.c
5d5cbe
index 963f8ea..db93dda 100644
5d5cbe
--- a/libxfs/xfs_dquot_buf.c
5d5cbe
+++ b/libxfs/xfs_dquot_buf.c
5d5cbe
@@ -69,6 +69,13 @@ xfs_dquot_verify(
5d5cbe
 	    ddq_type != XFS_DQ_GROUP)
5d5cbe
 		return __this_address;
5d5cbe
 
5d5cbe
+	if ((ddq->d_flags & XFS_DQ_BIGTIME) &&
5d5cbe
+	    !xfs_sb_version_hasbigtime(&mp->m_sb))
5d5cbe
+		return __this_address;
5d5cbe
+
5d5cbe
+	if ((ddq->d_flags & XFS_DQ_BIGTIME) && !ddq->d_id)
5d5cbe
+		return __this_address;
5d5cbe
+
5d5cbe
 	if (id != -1 && id != be32_to_cpu(ddq->d_id))
5d5cbe
 		return __this_address;
5d5cbe
 
5d5cbe
@@ -293,7 +300,12 @@ xfs_dquot_from_disk_ts(
5d5cbe
 	struct xfs_disk_dquot	*ddq,
5d5cbe
 	__be32			dtimer)
5d5cbe
 {
5d5cbe
-	return be32_to_cpu(dtimer);
5d5cbe
+	uint32_t		t = be32_to_cpu(dtimer);
5d5cbe
+
5d5cbe
+	if (t != 0 && (ddq->d_flags & XFS_DQ_BIGTIME))
5d5cbe
+		return xfs_dq_bigtime_to_unix(t);
5d5cbe
+
5d5cbe
+	return t;
5d5cbe
 }
5d5cbe
 
5d5cbe
 /* Convert an incore timer value into an on-disk timer value. */
5d5cbe
@@ -302,5 +314,10 @@ xfs_dquot_to_disk_ts(
5d5cbe
 	struct xfs_dquot	*dqp,
5d5cbe
 	time64_t		timer)
5d5cbe
 {
5d5cbe
-	return cpu_to_be32(timer);
5d5cbe
+	uint32_t		t = timer;
5d5cbe
+
5d5cbe
+	if (timer != 0 && (dqp->q_type & XFS_DQ_BIGTIME))
5d5cbe
+		t = xfs_dq_unix_to_bigtime(timer);
5d5cbe
+
5d5cbe
+	return cpu_to_be32(t);
5d5cbe
 }
5d5cbe
diff --git a/libxfs/xfs_format.h b/libxfs/xfs_format.h
5d5cbe
index b1f6219..9ba65e5 100644
5d5cbe
--- a/libxfs/xfs_format.h
5d5cbe
+++ b/libxfs/xfs_format.h
5d5cbe
@@ -1263,6 +1263,10 @@ static inline bool xfs_dinode_has_bigtime(const struct xfs_dinode *dip)
5d5cbe
  * ondisk min and max defined here can be used directly to constrain the incore
5d5cbe
  * quota expiration timestamps on a Unix system.
5d5cbe
  *
5d5cbe
+ * When bigtime is enabled, we trade two bits of precision to expand the
5d5cbe
+ * expiration timeout range to match that of big inode timestamps.  The min and
5d5cbe
+ * max recorded here are the on-disk limits, not a Unix timestamp.
5d5cbe
+ *
5d5cbe
  * The grace period for each quota type is stored in the root dquot (id = 0)
5d5cbe
  * and is applied to a non-root dquot when it exceeds the soft or hard limits.
5d5cbe
  * The length of quota grace periods are unsigned 32-bit quantities measured in
5d5cbe
@@ -1281,6 +1285,48 @@ static inline bool xfs_dinode_has_bigtime(const struct xfs_dinode *dip)
5d5cbe
  */
5d5cbe
 #define XFS_DQ_LEGACY_EXPIRY_MAX	((int64_t)U32_MAX)
5d5cbe
 
5d5cbe
+/*
5d5cbe
+ * Smallest possible ondisk quota expiration value with bigtime timestamps.
5d5cbe
+ * This corresponds (after conversion to a Unix timestamp) with the incore
5d5cbe
+ * expiration of Jan  1 00:00:04 UTC 1970.
5d5cbe
+ */
5d5cbe
+#define XFS_DQ_BIGTIME_EXPIRY_MIN	(XFS_DQ_LEGACY_EXPIRY_MIN)
5d5cbe
+
5d5cbe
+/*
5d5cbe
+ * Largest supported ondisk quota expiration value with bigtime timestamps.
5d5cbe
+ * This corresponds (after conversion to a Unix timestamp) with an incore
5d5cbe
+ * expiration of Jul  2 20:20:24 UTC 2486.
5d5cbe
+ *
5d5cbe
+ * The ondisk field supports values up to -1U, which corresponds to an incore
5d5cbe
+ * expiration in 2514.  This is beyond the maximum the bigtime inode timestamp,
5d5cbe
+ * so we cap the maximum bigtime quota expiration to the max inode timestamp.
5d5cbe
+ */
5d5cbe
+#define XFS_DQ_BIGTIME_EXPIRY_MAX	((int64_t)4074815106U)
5d5cbe
+
5d5cbe
+/*
5d5cbe
+ * The following conversion factors assist in converting a quota expiration
5d5cbe
+ * timestamp between the incore and ondisk formats.
5d5cbe
+ */
5d5cbe
+#define XFS_DQ_BIGTIME_SHIFT	(2)
5d5cbe
+#define XFS_DQ_BIGTIME_SLACK	((int64_t)(1ULL << XFS_DQ_BIGTIME_SHIFT) - 1)
5d5cbe
+
5d5cbe
+/* Convert an incore quota expiration timestamp to an ondisk bigtime value. */
5d5cbe
+static inline uint32_t xfs_dq_unix_to_bigtime(time64_t unix_seconds)
5d5cbe
+{
5d5cbe
+	/*
5d5cbe
+	 * Round the expiration timestamp up to the nearest bigtime timestamp
5d5cbe
+	 * that we can store, to give users the most time to fix problems.
5d5cbe
+	 */
5d5cbe
+	return ((uint64_t)unix_seconds + XFS_DQ_BIGTIME_SLACK) >>
5d5cbe
+			XFS_DQ_BIGTIME_SHIFT;
5d5cbe
+}
5d5cbe
+
5d5cbe
+/* Convert an ondisk bigtime quota expiration value to an incore timestamp. */
5d5cbe
+static inline time64_t xfs_dq_bigtime_to_unix(uint32_t ondisk_seconds)
5d5cbe
+{
5d5cbe
+	return (time64_t)ondisk_seconds << XFS_DQ_BIGTIME_SHIFT;
5d5cbe
+}
5d5cbe
+
5d5cbe
 /*
5d5cbe
  * Default quota grace periods, ranging from zero (use the compiled defaults)
5d5cbe
  * to ~136 years.  These are applied to a non-root dquot that has exceeded
5d5cbe
diff --git a/libxfs/xfs_quota_defs.h b/libxfs/xfs_quota_defs.h
5d5cbe
index 2f61cd3..70d89cc 100644
5d5cbe
--- a/libxfs/xfs_quota_defs.h
5d5cbe
+++ b/libxfs/xfs_quota_defs.h
5d5cbe
@@ -28,17 +28,20 @@ typedef uint16_t	xfs_qwarncnt_t;
5d5cbe
 #define XFS_DQ_GROUP		0x0004		/* a group quota */
5d5cbe
 #define XFS_DQ_DIRTY		0x0008		/* dquot is dirty */
5d5cbe
 #define XFS_DQ_FREEING		0x0010		/* dquot is being torn down */
5d5cbe
+#define XFS_DQ_BIGTIME		0x0080		/* large expiry timestamps */
5d5cbe
 
5d5cbe
 #define XFS_DQ_ALLTYPES		(XFS_DQ_USER|XFS_DQ_PROJ|XFS_DQ_GROUP)
5d5cbe
 
5d5cbe
-#define XFS_DQTYPE_ANY		(XFS_DQ_ALLTYPES)
5d5cbe
+#define XFS_DQTYPE_ANY		(XFS_DQ_ALLTYPES | \
5d5cbe
+				 XFS_DQ_BIGTIME)
5d5cbe
 
5d5cbe
 #define XFS_DQ_FLAGS \
5d5cbe
 	{ XFS_DQ_USER,		"USER" }, \
5d5cbe
 	{ XFS_DQ_PROJ,		"PROJ" }, \
5d5cbe
 	{ XFS_DQ_GROUP,		"GROUP" }, \
5d5cbe
 	{ XFS_DQ_DIRTY,		"DIRTY" }, \
5d5cbe
-	{ XFS_DQ_FREEING,	"FREEING" }
5d5cbe
+	{ XFS_DQ_FREEING,	"FREEING" }, \
5d5cbe
+	{ XFS_DQ_BIGTIME,	"BIGTIME" }
5d5cbe
 
5d5cbe
 /*
5d5cbe
  * We have the possibility of all three quota types being active at once, and