Skip to contentSkip to conversation

support for RT data checksums v2

PATCH In review Christoph Hellwig 2 participants 34 messages Nov 4 – Nov 11

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

Hi all,

data checksums provide an additional safeguard against silent data loss.

In classic XFS they were hard to support because they need to be atomically updated with the written data. The zoned allocator solves that problem because it always writes out of place, and the checksums can be committed at the same as the metadata linking the newly written file data into place. In theory, a conventional allocator could be used in combination with the always_cow option, but there are few upsides of this compared to using the zoned allocator.

Data checksums are stored in per-realtime group files in the metadir, similar to other modern RT metadata. Unlike the checksum design in btrfs or some other file system, the checksums are associated with the physical blocks, and not with logical data in files. This reduces the mapping overhead, and significantly reduces the write amplification, and also avoids duplicate checksums for reflinked files (although those are not yet supported with the zoned allocator anyway).

The initial version provides two checksums algorithms: crc32c and crc64. Both of those are cyclic redundancy check algorithms which provide known good detection of bit flips that is better than general purpose hash functions. Both are not cryptographic hashes and thus do not provide any kind of protection against intentional tampering with the data. The crc32c parameters exactly match those use for xfs metadata checksums, and also those used by the default btrfs checksum, and the NVMe PI formats using crc32c. The crc64 parameters exactly match those using the NVMe PI formats using crc64. crc32c provides reasonable assurance for today's hardware, but might prove limiting for extremely large data sets, crc64 fills that void, but probably warrants using > 4k file system block sizes to amortize the overhead.

In this series the checksums are only used to check data integrity and report issues with it. That on it's own is a bit of a lame story, but it is important as a building block for two additional features under development:

- exposing the checksum to userspace through io_uring with IORING_RW_ATTR_FLAG_PI. A prototype of this exists, but it needs a bit more rework of common code than I'd like for the initial review. As a side effect this support will also support salvaging data with bad checksums for analysis in userspace.
- retrying reads through a different replica from RAID devices that provide it. This has been proposed before and requires some hairy block layer infrastructure. I have a prototype for MD-based mirrors that can be extended to other use cases. The same mechanism can also be used to retry reads for the already checksum protected XFS metadata.

The performance drop when using data checksums is between not measurable to about 2% for most workloads on HDD and SSD. On fast enough SSDs single threaded large reads can see up to 10% slow down as the checksum validation runs on a strict per-cpu workqueue through the block layer in-task bio completion. If needed, different completion methods that distribute the I/O completions could be added.

This series is repost with all the code review comments from last round hopefully addressed to resync with -rc1, but without full benchmarking of the new one-shot log item based approach, and without a design document. If you need that to digest the code, this version is not for you.

git://git.infradead.org/users/hch/xfs.git xfs-crc

Gitweb:

https://git.infradead.org/?p=users/hch/xfs.git;a=shortlog;h=refs/heads/xfs-crc

Note that the series will need a rebase on top of the fsverity work, but the code points have been chosen to hopefully not conflict.

The userspace repositories are here:

git://git.infradead.org/users/hch/xfsprorgs.git xfs-crc
git://git.infradead.org/users/hch/xfstests-dev.git xfs-crc

Changes since v1:
- improve various comments and commit logs in terms of spelling and structure
- minor indentation fixes
- add a big comment explaining the io_parent/io_csum_alloc/io_csum_alloc union and the relation to the io_csum field in struct iomap_ioend.
- add a log reservation assert in xfs_rtfile_initialize_block
- use xfs_rgblock_t instead of xfs_rtblock_t in two places that clearly deal with rgblock numbers
- clear oz_csum_bufs on zone open failure for extra belts and suspenders
- add a comment on the bi_idx assert in xfs_rtcsum_prepare_read
- add an assert and improve the comment for the no-bp case in xfs_rtcsum_verify_ioend
- add a comment (provided by Darrick) explaining the checksum log vs extent remap ordering
- remove the unused csum_buf field from struct xfs_gc_bio
- add a comment explaining the xfs_submit_verify_bio_csum return value
- make the end calculation in xfs_submit_verify_bio_csum more rounding-safe with a code snipplet from Darrick, and give up if the range is empty afterwards
- use plural "data checksums" in the experimental warning
- s/xfs_rgb_to_/rgbno_to_/
- improve superblock field validation
- add health reporting (XFS_RTGROUP_GEOM_SICK_CSUM)
- reduce the log reservation that added a bit too much slack in some cases
- use xfs_buf_set_uptodate
- fix error handling in xfs_rtcsum_open_zone
- don't enable xfs_iomap_read_ops for all device that require stable pages
- remove an extra xfs_rtcsum.h include in xfs_zone_alloc.c
- fix a lost error in xfs_zone_gc_finish_chunk
- set bi_end_io earlier to fix completions when xfs_rtcsum_prepare_read fails
- add a xfs_min_diosize helper to share code between the aops and statx reporting
- add a XFS_FEAT_* value for rtcsum
- switch to an INCOMPAT flag as old kernels don't like the padding reuse in xfs_dsb
- fix a buffer double buffer put in the read async error path
- fix arithmerics on the media verify ioctl
- refactor get_cache_zone to avoid a sleep under RCU condition
- hold the IOLOCK over zone reset to synchronize with media verification, and check the write pointer and not just for an empty zone

Diffstat:

 block/bio-integrity-fs.c        |    1 
 fs/iomap/bio.c                  |    2 
 fs/iomap/direct-io.c            |    2 
 fs/iomap/internal.h             |   27 ++-
 fs/iomap/ioend.c                |   78 +++++++++
 fs/xfs/Kconfig                  |    1 
 fs/xfs/Makefile                 |    3 
 fs/xfs/libxfs/xfs_cksum.h       |    7 
 fs/xfs/libxfs/xfs_format.h      |   46 +++++
 fs/xfs/libxfs/xfs_fs.h          |    7 
 fs/xfs/libxfs/xfs_health.h      |    4 
 fs/xfs/libxfs/xfs_log_format.h  |   13 +
 fs/xfs/libxfs/xfs_log_recover.h |    1 
 fs/xfs/libxfs/xfs_ondisk.h      |    5 
 fs/xfs/libxfs/xfs_rtbitmap.c    |   57 ++++---
 fs/xfs/libxfs/xfs_rtbitmap.h    |    3 
 fs/xfs/libxfs/xfs_rtcsumfile.c  |   95 ++++++++++++
 fs/xfs/libxfs/xfs_rtcsumfile.h  |  150 +++++++++++++++++++
 fs/xfs/libxfs/xfs_rtgroup.c     |   16 ++
 fs/xfs/libxfs/xfs_rtgroup.h     |   26 +++
 fs/xfs/libxfs/xfs_sb.c          |   91 +++++++++++
 fs/xfs/libxfs/xfs_sb.h          |    1 
 fs/xfs/libxfs/xfs_shared.h      |    1 
 fs/xfs/libxfs/xfs_trans_resv.c  |   26 +++
 fs/xfs/libxfs/xfs_trans_resv.h  |    4 
 fs/xfs/scrub/agheader.c         |    5 
 fs/xfs/xfs_aops.c               |    3 
 fs/xfs/xfs_buf.c                |   96 ++++++++++++
 fs/xfs/xfs_buf.h                |    4 
 fs/xfs/xfs_buf_item_recover.c   |   10 +
 fs/xfs/xfs_file.c               |   21 ++
 fs/xfs/xfs_health.c             |    1 
 fs/xfs/xfs_inode.h              |   22 ++
 fs/xfs/xfs_ioctl.c              |    2 
 fs/xfs/xfs_ioend.c              |  175 ++++++++++++++++++++--
 fs/xfs/xfs_ioend.h              |    2 
 fs/xfs/xfs_iomap.c              |   72 ++++++++-
 fs/xfs/xfs_iomap.h              |    4 
 fs/xfs/xfs_iops.c               |    8 -
 fs/xfs/xfs_log.h                |    3 
 fs/xfs/xfs_log_recover.c        |    3 
 fs/xfs/xfs_message.c            |    4 
 fs/xfs/xfs_message.h            |    1 
 fs/xfs/xfs_mount.h              |    6 
 fs/xfs/xfs_platform.h           |    1 
 fs/xfs/xfs_reflink.c            |    2 
 fs/xfs/xfs_rtalloc.c            |   11 +
 fs/xfs/xfs_rtcsum.c             |  315 ++++++++++++++++++++++++++++++++++++++++
 fs/xfs/xfs_rtcsum.h             |   30 +++
 fs/xfs/xfs_rtcsum_item.c        |  218 +++++++++++++++++++++++++++
 fs/xfs/xfs_super.c              |   21 ++
 fs/xfs/xfs_sysfs.c              |    2 
 fs/xfs/xfs_trace.h              |    3 
 fs/xfs/xfs_verify_media.c       |  194 +++++++++++++++++++-----
 fs/xfs/xfs_zone_alloc.c         |   43 ++++-
 fs/xfs/xfs_zone_gc.c            |   73 +++++++--
 fs/xfs/xfs_zone_priv.h          |    8 +
 include/linux/iomap.h           |   42 +++++
 58 files changed, 1932 insertions(+), 140 deletions(-)
#1 lore raw

PATCH 1/25 block: export fs_bio_integrity_verify

From: Damien Le Moal <[email protected]>

XFS will need to call this in the iomap_write_ops.read_folio_range implementation.

---
 block/bio-integrity-fs.c | 1 +
 1 file changed, 1 insertion(+)
block/bio-integrity-fs.c +1 -0
index c8e91ada8ca6..20d0a3d52932 100644
@@ -74,6 +74,7 @@ int fs_bio_integrity_verify(struct bio *bio, struct bvec_iter *data_iter)
 		bio_integrity_bytes(bi, data_iter->bi_size >> SECTOR_SHIFT);
 	return blk_status_to_errno(bio_integrity_verify(bio, data_iter));
 }
+EXPORT_SYMBOL_GPL(fs_bio_integrity_verify);
 
 static int __init fs_bio_integrity_init(void)
 {
Signature · 1 line
2.53.0
#2 lore raw

PATCH 2/25 iomap: add support for data checksumming

In reply to Christoph Hellwig

Add a new refcounted structure for a data checksum attached to an iomap_ioend, and helpers to allocate, split and access it.

---
 fs/iomap/bio.c        |  2 +-
 fs/iomap/direct-io.c  |  2 +-
 fs/iomap/internal.h   | 27 ++++++++++-----
 fs/iomap/ioend.c      | 78 ++++++++++++++++++++++++++++++++++++++++---
 include/linux/iomap.h | 42 ++++++++++++++++++++++-
 5 files changed, 136 insertions(+), 15 deletions(-)
fs/iomap/bio.c +1 -1
index 256564ebe238..3c13d85997a6 100644
@@ -109,7 +109,7 @@ int iomap_bio_read_folio_range(const struct iomap_iter *iter,
 
 	if (!bio ||
 	    bio_end_sector(bio) != iomap_sector(&iter->iomap, iter->pos) ||
-	    !iomap_bio_has_room(&iter->iomap, bio, plen) ||
+	    !iomap_bio_has_room(iter->inode, &iter->iomap, bio, plen) ||
 	    !bio_add_folio(bio, folio, plen, offset_in_folio(folio, iter->pos)))
 		iomap_read_alloc_bio(iter, ctx, plen);
 	return 0;
fs/iomap/direct-io.c +1 -1
index ad041ac3252a..36fb2e733b0f 100644
@@ -342,7 +342,7 @@ static ssize_t iomap_dio_bio_iter_one(struct iomap_iter *iter,
 		struct iomap_dio *dio, loff_t pos, unsigned int alignment,
 		blk_opf_t op)
 {
-	unsigned int maxsize = iomap_max_bio_size(&iter->iomap);
+	unsigned int maxsize = iomap_max_bio_size(iter->inode, &iter->iomap);
 	unsigned int nr_vecs;
 	struct bio *bio;
 	ssize_t ret;
fs/iomap/internal.h +19 -8
index e69b3ac02cc2..b52a49345d68 100644
@@ -4,24 +4,35 @@
 
 #define IOEND_BATCH_SIZE	4096
 
+static inline unsigned int max_csum_io_size(const struct inode *inode,
+		const struct iomap *iomap)
+{
+	return IOMAP_CSUM_MAX_SIZE << (inode->i_blkbits - iomap->csum_shift);
+}
+
 /*
  * Normally we can build bios as big as the data structure supports.
  *
- * But for integrity protected I/O we need to respect the maximum size of the
- * single contiguous allocation for the integrity buffer.
+ * But for checksum or integrity protected I/O we need to respect the maximum
+ * size of the single contiguous allocation for the checksum/integrity buffer.
  */
-static inline size_t iomap_max_bio_size(const struct iomap *iomap)
+static inline size_t iomap_max_bio_size(const struct inode *inode,
+		const struct iomap *iomap)
 {
+	size_t max = BIO_MAX_SIZE;
+
 	if (iomap->flags & IOMAP_F_INTEGRITY)
-		return max_integrity_io_size(bdev_limits(iomap->bdev));
-	return BIO_MAX_SIZE;
+		max = min(max, max_integrity_io_size(bdev_limits(iomap->bdev)));
+	if (iomap->csum_shift)
+		max = min(max, max_csum_io_size(inode, iomap));
+	return max;
 }
 
 /* Can @len more bytes go into @bio? */
-static inline bool iomap_bio_has_room(const struct iomap *iomap,
-		const struct bio *bio, size_t len)
+static inline bool iomap_bio_has_room(const struct inode *inode,
+		const struct iomap *iomap, const struct bio *bio, size_t len)
 {
-	size_t max_size = iomap_max_bio_size(iomap);
+	size_t max_size = iomap_max_bio_size(inode, iomap);
 
 	return len <= max_size && bio->bi_iter.bi_size <= max_size - len;
 }
fs/iomap/ioend.c +74 -4

157 lines not on this page · raw message

include/linux/iomap.h +41 -1

73 lines not on this page · raw message

Signature · 1 line
2.53.0
#3 lore raw

Re: [PATCH 02/25] iomap: add support for data checksumming

Quoted 1 line · Christoph Hellwig

Add a new refcounted structure for a data checksum attached to

I don't see any new refcounted structure. Probably the commit message is stale?

Quoted 11 lines

an iomap_ioend, and helpers to allocate, split and access it.

/*
* Flags that if set on either ioend prevent the merge of two ioends.

include/linux/iomap.h +1 -0
@@ -522,14 +529,31 @@ static inline u16 iomap_ioend_flags(const struct iomap *iomap)
 struct iomap_ioend {
 	struct list_head	io_list;	/* next ioend in chain */
 	u16			io_flags;	/* IOMAP_IOEND_* */
+	u8			io_csum_shift;	/* ilog(2) of csum size */

include/linux/iomap.h static inline u16 iomap_ioend_flags(const struct iomap *iomap) · line 541

Quoted 10 lines
include/linux/iomap.h +4 -1
 	u32			io_bvec_offset;	/* offset into first bvec */
 	struct inode		*io_inode;	/* file being written to */
 	size_t			io_size;	/* size of the extent */
 	atomic_t		io_remaining;	/* completetion defer count */
 	int			io_error;	/* stashed away status */
-	struct iomap_ioend	*io_parent;	/* parent for completions */
+
+	/*
+	 * There are two kinds of ioend, the original ones submitted by iomap,
+	 * and thus split of for I/O size limitations.  For the latter,

s/thus split of/those split off

Quoted 22 lines
include/linux/iomap.h +13 -0
+	 * io_parent points to the original ioend.  For the original ioends,
+	 * the space used by the parent pointer is reused by the checksum
+	 * pointer when data checksum are used.  Split ioends always point
+	 * into the original allocation with their io_csum pointer.  As a
+	 * special case tiny checksum use the pointer as actual checksum
+	 * storage if that fits.
+	 */
+	union {
+		struct iomap_ioend *io_parent;	/* parent for completions */
+		void		*io_csum_alloc; /* original csum allocation. */
+		u8		io_csum_inline[sizeof(void *)];
+	};
 	loff_t			io_offset;	/* offset in the file */
 	sector_t		io_sector;	/* start sector of ioend */
+	void			*io_csum;	/* data checksum */
 	void			*io_private;	/* file system private data */
 	struct fsverity_info	*io_vi;		/* fsverity info */
 	struct bio		io_bio;		/* MUST BE LAST! */
@@ -547,6 +571,22 @@ static inline struct iomap_ioend *iomap_ioend_from_bio(struct bio *bio)
 	.bi_offset	= (_ioend)->io_bvec_offset,	\
 }
 
Signature · 1 line
Pankaj
#4 lore raw

Re: [PATCH 02/25] iomap: add support for data checksumming

Quoted 5 lines · Pankaj Raghav (Samsung)
Quoted 1 line · Christoph Hellwig

Add a new refcounted structure for a data checksum attached to

I don't see any new refcounted structure. Probably the commit message is stale?

Yes, I'll fix it up.

#5 lore raw

PATCH 3/25 xfs: always call xfs_update_stable_writes in xfs_ioctl_setattr_xflags

In reply to Christoph Hellwig

Currently xfs_update_stable_writes() is never called when the RT flag is flipped because xfs_ioctl_setattr_xflags() updates ip->i_diflags before comparing the rtflag variable to XFS_IS_REALTIME_INODE(ip), and thus the stable flag can be set incorretly when flipping the realtime bit on an existing file.

Fix this by unconditionally calling xfs_update_stable_writes for regular files.

Fixes d528a6c1d4b6 ("xfs: data checksums require stable writes")
---
 fs/xfs/xfs_ioctl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/xfs/xfs_ioctl.c +1 -1
index f81b6e52ac40..36ba609a2593 100644
@@ -595,7 +595,7 @@ xfs_ioctl_setattr_xflags(
 	 * Make the stable writes flag match that of the device the inode
 	 * resides on when flipping the RT flag.
 	 */
-	if (rtflag != XFS_IS_REALTIME_INODE(ip) && S_ISREG(VFS_I(ip)->i_mode))
+	if (S_ISREG(VFS_I(ip)->i_mode))
 		xfs_update_stable_writes(ip);
 
 	xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
Signature · 1 line
2.53.0
#6 lore raw

Re: [PATCH 03/25] xfs: always call xfs_update_stable_writes in xfs_ioctl_setattr_xflags

Quoted 10 lines · Christoph Hellwig

Currently xfs_update_stable_writes() is never called when the RT flag is flipped because xfs_ioctl_setattr_xflags() updates ip->i_diflags before comparing the rtflag variable to XFS_IS_REALTIME_INODE(ip), and thus the stable flag can be set incorretly when flipping the realtime bit on an existing file.

Fix this by unconditionally calling xfs_update_stable_writes for regular files.

Fixes d528a6c1d4b6 ("xfs: data checksums require stable writes")

Shouldn't this patch be folded in to the stable writes patches?

Quoted 22 lines
---
 fs/xfs/xfs_ioctl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/xfs/xfs_ioctl.c +1 -1
index f81b6e52ac40..36ba609a2593 100644
@@ -595,7 +595,7 @@ xfs_ioctl_setattr_xflags(
 	 * Make the stable writes flag match that of the device the inode
 	 * resides on when flipping the RT flag.
 	 */
-	if (rtflag != XFS_IS_REALTIME_INODE(ip) && S_ISREG(VFS_I(ip)->i_mode))
+	if (S_ISREG(VFS_I(ip)->i_mode))
 		xfs_update_stable_writes(ip);
 
 	xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
Signature · 1 line
2.53.0
Signature · 1 line
Pankaj
#7 lore raw

Re: [PATCH 03/25] xfs: always call xfs_update_stable_writes in xfs_ioctl_setattr_xflags

Quoted 7 lines · Pankaj Raghav (Samsung)
Quoted 6 lines · Christoph Hellwig

Currently xfs_update_stable_writes() is never called when the RT flag is flipped because xfs_ioctl_setattr_xflags() updates ip->i_diflags before comparing the rtflag variable to XFS_IS_REALTIME_INODE(ip), and thus the stable flag can be set incorretly when flipping the realtime bit on an existing file.

Quoted 6 lines
Quoted 4 lines

Fix this by unconditionally calling xfs_update_stable_writes for regular files.

Fixes d528a6c1d4b6 ("xfs: data checksums require stable writes")

Shouldn't this patch be folded in to the stable writes patches?

Not really, this is a fix for patches already in mainline. Carlos has queued this one up already, so it will be gone from the next round.

#8 lore raw

PATCH 4/25 xfs: use a separate lockdep class for all rmap inode locks

In reply to Christoph Hellwig

We'll soon use the rmap inode iolock for additional zone reset synchronization, so assign a differnet lock class to it. And while we're at it do the same for the invalidate lock.

---
 fs/xfs/libxfs/xfs_rtgroup.c | 6 ++++++
 1 file changed, 6 insertions(+)
fs/xfs/libxfs/xfs_rtgroup.c +6 -0
index c6981f566860..91961fb85aff 100644
@@ -282,6 +282,8 @@ xfs_rtgroup_get_geometry(
 
 #ifdef CONFIG_PROVE_LOCKING
 static struct lock_class_key xfs_rtginode_lock_class;
+static struct lock_class_key xfs_rtginode_iolock_class;
+static struct lock_class_key xfs_rtginode_invalidatelock_class;
 
 static int
 xfs_rtginode_ilock_cmp_fn(
@@ -328,6 +330,10 @@ xfs_rtginode_lockdep_setup(
 			type);
 	lock_set_cmp_fn(&ip->i_lock, xfs_rtginode_ilock_cmp_fn,
 			xfs_rtginode_ilock_print_fn);
+	lockdep_set_class_and_subclass(&VFS_I(ip)->i_rwsem,
+			&xfs_rtginode_iolock_class, type);
+	lockdep_set_class_and_subclass(&VFS_I(ip)->i_mapping->invalidate_lock,
+			&xfs_rtginode_invalidatelock_class, type);
 }
 #else
 #define xfs_rtginode_lockdep_setup(ip, rgno, type)	do { } while (0)
Signature · 1 line
2.53.0
#9 lore raw

PATCH 5/25 xfs: add a xfs_buf_read_async buffer cache API

In reply to Christoph Hellwig

Add a new helper that reads a buffer asynchronously, and one that waits for this read. The xfs_buf_read_async call is somehwat similar to readahead, but returns a buffered reference to the caller and does not become a no-op under memory or I/O congestion. This means the callers can use xfs_buf_read_async_wait instead of doing another read call and will never issue I/O.

The intended use is to kick off a read of data checksum buffers at roughly the same time as the data read so that they are available in the I/O completion handler.

---
 fs/xfs/xfs_buf.c   | 96 ++++++++++++++++++++++++++++++++++++++++++++++
 fs/xfs/xfs_buf.h   |  4 ++
 fs/xfs/xfs_trace.h |  3 ++
 3 files changed, 103 insertions(+)
fs/xfs/xfs_buf.c +96 -0
index 6c93b4f5629c..39dd3650b425 100644
@@ -734,6 +734,102 @@ xfs_buf_read_map(
 	return error;
 }
 
+static int
+xfs_buf_read_async_wait_lock(
+	struct xfs_buf		*bp)
+{
+	int			error = 0;
+
+	trace_xfs_buf_read_async_lock(bp, 0, _RET_IP_);
+
+	/* xfs_buf_find_lock can't return an error with 0 flags */
+	xfs_buf_find_lock(bp, 0);
+	if (bp->b_error) {
+		if (!xlog_is_shutdown(bp->b_mount->m_log))
+			xfs_buf_ioerror_alert(bp, __builtin_return_address(0));
+		xfs_buf_clear_flags(bp, XBF_DONE);
+		xfs_buf_stale(bp);
+		error = bp->b_error;
+		/* bad CRC means corrupted metadata */
+		if (error == -EFSBADCRC)
+			error = -EFSCORRUPTED;
+	} else {
+		ASSERT(bp->b_ops);
+		xfs_buf_clear_flags(bp, XBF_READ);
+	}
+	xfs_buf_unlock(bp);
+	return error;
+}
+
+/*
+ * Wait for a buffer read kicked off by xfs_buf_read_async().
+ *
+ * Do a full lock cycle to wait for unfinished I/O and for buffers with errors
+ * or which are stale.  For successfully read buffers nothing needs to be done.
+ */
+int
+xfs_buf_read_async_wait(
+	struct xfs_buf		*bp)
+{
+	trace_xfs_buf_read_async_wait(bp, 0, _RET_IP_);
+	if (bp->b_error ||
+	    (READ_ONCE(bp->b_flags) & (XBF_DONE | XBF_STALE)) != XBF_DONE)
+		return xfs_buf_read_async_wait_lock(bp);
+	return 0;
+}
+
+/*
+ * Kick off an asynchronous read.  Unlike xfs_buf_readahead, this returns a
+ * reference to the buffer, and reliably reads the data instead of skipping the
+ * read on memory pressure.
+ *
+ * The buffer may be locked when I/O is kicked off, but the I/O completion
+ * handler will unlock it.  The caller needs to lock itself if need to prevent
+ * concurrent access or to synchronize with I/O completion.
+ */
+int
+xfs_buf_read_async(
+	struct xfs_buftarg	*btp,
+	xfs_daddr_t		daddr,
+	size_t			numblks,
+	const struct xfs_buf_ops *ops,
+	struct xfs_buf		**bpp)
+{
+	DEFINE_SINGLE_BUF_MAP(map, daddr, numblks);
+	struct xfs_buf		*bp;
+	int			error;
+
+	ASSERT(!xfs_buftarg_is_mem(btp));
+
+	error = xfs_find_get_buf(btp, &map, 1, XBF_READ, &bp);
+	if (error)
+		return error;
+
+	/*
+	 * Do a lockless fast path check for a valid uptodate buffer and avoid
+	 * locking entirely in this case.
+	 */
+	if ((READ_ONCE(bp->b_flags) & (XBF_DONE | XBF_STALE)) == XBF_DONE)
+		goto done;
+
+	/* xfs_buf_find_lock can't return an error with 0 flags */
+	xfs_buf_find_lock(bp, 0);
+	if (bp->b_flags & XBF_DONE) {
+		xfs_buf_unlock(bp);
+		goto done;
+	}
+	trace_xfs_buf_read_async(bp, 0, _RET_IP_);
+	XFS_STATS_INC(btp->bt_mount, xb_get_read);
+	xfs_buf_hold(bp);
+	bp->b_ops = ops;
+	xfs_buf_clear_flags(bp, XBF_WRITE);
+	xfs_buf_set_flags(bp, XBF_READ | XBF_ASYNC);
+	xfs_buf_submit(bp);
+done:
+	*bpp = bp;
+	return 0;
+}
+
 /*
  *	If we are not low on memory then do the readahead in a deadlock
  *	safe manner.
fs/xfs/xfs_buf.h +4 -0
index a4729253b56f..1b352ec91aa6 100644
@@ -255,6 +255,10 @@ xfs_buf_readahead(
 	return xfs_buf_readahead_map(target, &map, 1, ops);
 }
 
+int xfs_buf_read_async(struct xfs_buftarg *btp, xfs_daddr_t daddr,
+		size_t numblks, const struct xfs_buf_ops *ops,
+		struct xfs_buf **bpp);
+int xfs_buf_read_async_wait(struct xfs_buf *bp);
 int xfs_buf_get_uncached(struct xfs_buftarg *target, size_t numblks,
 		struct xfs_buf **bpp);
 int xfs_buf_read_uncached(struct xfs_buftarg *target, xfs_daddr_t daddr,
fs/xfs/xfs_trace.h +3 -0
index 28d49f158b22..aca4e7b03052 100644
@@ -840,6 +840,9 @@ DEFINE_EVENT(xfs_buf_flags_class, name, \
 	TP_ARGS(bp, flags, caller_ip))
 DEFINE_BUF_FLAGS_EVENT(xfs_buf_get);
 DEFINE_BUF_FLAGS_EVENT(xfs_buf_read);
+DEFINE_BUF_FLAGS_EVENT(xfs_buf_read_async);
+DEFINE_BUF_FLAGS_EVENT(xfs_buf_read_async_wait);
+DEFINE_BUF_FLAGS_EVENT(xfs_buf_read_async_lock);
 DEFINE_BUF_FLAGS_EVENT(xfs_buf_readahead);
 
 TRACE_EVENT(xfs_buf_ioerror,
Signature · 1 line
2.53.0
#10 lore raw

PATCH 6/25 xfs: add xfs_daddr_to_rgno and xfs_daddr_to_rgbno helpers

In reply to Christoph Hellwig

Translate from a disk address to the realtime group and group-relative block numbers. This will be needed by the data checksumming code.

---
 fs/xfs/libxfs/xfs_rtgroup.h | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
fs/xfs/libxfs/xfs_rtgroup.h +20 -0
index fca2eb74908c..f26e324f0de3 100644
@@ -390,4 +390,24 @@ xfs_rtgroup_raw_size(
 	return g->blocks;
 }
 
+static inline xfs_rgnumber_t
+xfs_daddr_to_rgno(struct xfs_mount *mp, xfs_daddr_t d)
+{
+	struct xfs_groups	*g = &mp->m_groups[XG_TYPE_RTG];
+	xfs_rfsblock_t		rbno = XFS_BB_TO_FSBT(mp, d) - g->start_fsb;
+
+	ASSERT(xfs_has_rtgroups(mp));
+	return div_u64(rbno, xfs_rtgroup_raw_size(mp));
+}
+
+static inline xfs_rgblock_t
+xfs_daddr_to_rgbno(struct xfs_mount *mp, xfs_daddr_t d)
+{
+	struct xfs_groups	*g = &mp->m_groups[XG_TYPE_RTG];
+	xfs_rfsblock_t		rbno = XFS_BB_TO_FSBT(mp, d) - g->start_fsb;
+
+	ASSERT(xfs_has_rtgroups(mp));
+	return do_div(rbno, xfs_rtgroup_raw_size(mp));
+}
+
 #endif /* __LIBXFS_RTGROUP_H */
Signature · 1 line
2.53.0
#11 lore raw

PATCH 7/25 xfs: prepare xfs_rtfile_initialize_blocks for larger than FSB blocks

In reply to Christoph Hellwig

The upcoming RT data checksum feature will use larger than FSB blocks. Prepare xfs_rtfile_initialize_blocks to pass the number of FSBs per RT blocks, and to pass bmapi_flags to ask for contiguous allocation.

---
 fs/xfs/libxfs/xfs_rtbitmap.c | 47 ++++++++++++++++++++++--------------
 fs/xfs/libxfs/xfs_rtbitmap.h |  3 ++-
 fs/xfs/xfs_rtalloc.c         |  4 +--
 3 files changed, 33 insertions(+), 21 deletions(-)
fs/xfs/libxfs/xfs_rtbitmap.c +29 -18
index 7fbfca2a5edd..c83d7f961f40 100644
@@ -1345,6 +1345,7 @@ xfs_rtfile_alloc_blocks(
 	struct xfs_inode	*ip,
 	xfs_fileoff_t		offset_fsb,
 	xfs_filblks_t		count_fsb,
+	uint32_t		bmapi_flags,
 	struct xfs_bmbt_irec	*map)
 {
 	struct xfs_mount	*mp = ip->i_mount;
@@ -1366,7 +1367,7 @@ xfs_rtfile_alloc_blocks(
 		goto out_trans_cancel;
 
 	error = xfs_bmapi_write(tp, ip, offset_fsb, count_fsb,
-			XFS_BMAPI_METADATA, 0, map, &nmap);
+			XFS_BMAPI_METADATA | bmapi_flags, 0, map, &nmap);
 	if (error)
 		goto out_trans_cancel;
 
@@ -1404,34 +1405,46 @@ xfs_rtfile_initialize_block(
 	struct xfs_rtgroup	*rtg,
 	enum xfs_rtg_inodes	type,
 	xfs_fsblock_t		fsbno,
-	void			*data)
+	xfs_filblks_t		nblks,
+	void			**data)
 {
 	struct xfs_mount	*mp = rtg_mount(rtg);
 	struct xfs_inode	*ip = rtg->rtg_inodes[type];
+	size_t			len = XFS_FSB_TO_B(mp, nblks);
+	size_t			copylen = len;
+	struct xfs_trans_res	tres = M_RES(mp)->tr_growrtzero;
 	struct xfs_trans	*tp;
 	struct xfs_buf		*bp;
-	const size_t		copylen = mp->m_blockwsize << XFS_WORDLOG;
 	int			error;
 
-	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growrtzero, 0, 0, 0, &tp);
+	/* The log should always be able to handle 64k of logged buffers */
+	ASSERT(nblks <= XFS_B_TO_FSB(mp, SZ_64K));
+
+	tres.tr_logres *= nblks;
+	error = xfs_trans_alloc(mp, &tres, 0, 0, 0, &tp);
 	if (error)
 		return error;
 	xfs_ilock(ip, XFS_ILOCK_EXCL);
 	xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
 
 	error = xfs_trans_get_buf(tp, mp->m_ddev_targp,
-			XFS_FSB_TO_DADDR(mp, fsbno), mp->m_bsize, 0, &bp);
+			XFS_FSB_TO_DADDR(mp, fsbno), BTOBB(len), 0, &bp);
 	if (error) {
 		xfs_trans_cancel(tp);
 		return error;
 	}
 
+	if (xfs_has_rtgroups(mp))
+		copylen -= sizeof(struct xfs_rtbuf_blkinfo);
+
 	xfs_rtfile_initialize_buf(rtg, type, bp, tp);
-	if (data)
-		memcpy(xfs_rtblock_payload(bp), data, copylen);
-	else
+	if (*data) {
+		memcpy(xfs_rtblock_payload(bp), *data, copylen);
+		*data += copylen;
+	} else {
 		memset(xfs_rtblock_payload(bp), 0, copylen);
-	xfs_trans_log_buf(tp, bp, 0, mp->m_sb.sb_blocksize - 1);
+	}
+	xfs_trans_log_buf(tp, bp, 0, len - 1);
 	return xfs_trans_commit(tp);
 }
 
@@ -1446,33 +1459,31 @@ xfs_rtfile_initialize_blocks(
 	enum xfs_rtg_inodes	type,
 	xfs_fileoff_t		offset_fsb,	/* offset to start from */
 	xfs_fileoff_t		end_fsb,	/* offset to allocate to */
+	xfs_filblks_t		bsize,
+	uint32_t		bmapi_flags,
 	void			*data)		/* data to fill the blocks */
 {
-	struct xfs_mount	*mp = rtg_mount(rtg);
-	const size_t		copylen = mp->m_blockwsize << XFS_WORDLOG;
-
 	while (offset_fsb < end_fsb) {
 		struct xfs_bmbt_irec	map;
 		xfs_filblks_t		i;
 		int			error;
 
 		error = xfs_rtfile_alloc_blocks(rtg->rtg_inodes[type],
-				offset_fsb, end_fsb - offset_fsb, &map);
+				offset_fsb, end_fsb - offset_fsb, bmapi_flags,
+				&map);
 		if (error)
 			return error;
 
 		/*
-		 * Now we need to clear the allocated blocks.
+		 * Now we need to clear or initialize the allocated blocks.
 		 *
 		 * Do this one block per transaction, to keep it simple.
 		 */
-		for (i = 0; i < map.br_blockcount; i++) {
+		for (i = 0; i < map.br_blockcount; i += bsize) {
 			error = xfs_rtfile_initialize_block(rtg, type,
-					map.br_startblock + i, data);
+					map.br_startblock + i, bsize, &data);
 			if (error)
 				return error;
-			if (data)
-				data += copylen;
 		}
 
 		offset_fsb = map.br_startoff + map.br_blockcount;
fs/xfs/libxfs/xfs_rtbitmap.h +2 -1
index 750d74fbf4ed..e9e3378d15aa 100644
@@ -410,7 +410,8 @@ void xfs_rtfile_initialize_buf(struct xfs_rtgroup *rtg,
 		struct xfs_trans *tp);
 int xfs_rtfile_initialize_blocks(struct xfs_rtgroup *rtg,
 		enum xfs_rtg_inodes type, xfs_fileoff_t offset_fsb,
-		xfs_fileoff_t end_fsb, void *data);
+		xfs_fileoff_t end_fsb, xfs_filblks_t bsize,
+		uint32_t bmapi_flags, void *data);
 int xfs_rtbitmap_create(struct xfs_rtgroup *rtg, struct xfs_inode *ip,
 		struct xfs_trans *tp, bool init);
 int xfs_rtsummary_create(struct xfs_rtgroup *rtg, struct xfs_inode *ip,
fs/xfs/xfs_rtalloc.c +2 -2
index 84efe5a8fb11..78a1c066c7eb 100644
@@ -1191,11 +1191,11 @@ xfs_growfs_rt_alloc_blocks(
 	}
 
 	error = xfs_rtfile_initialize_blocks(rtg, XFS_RTGI_BITMAP, orbmblocks,
-			nmp->m_sb.sb_rbmblocks, NULL);
+			nmp->m_sb.sb_rbmblocks, 1, 0, NULL);
 	if (error)
 		goto out_free;
 	error = xfs_rtfile_initialize_blocks(rtg, XFS_RTGI_SUMMARY, orsumblocks,
-			nmp->m_rsumblocks, NULL);
+			nmp->m_rsumblocks, 1, 0, NULL);
 out_free:
 	kfree(nmp);
 	return error;
Signature · 1 line
2.53.0
#12 lore raw

PATCH 8/25 xfs: relase zi_open_zones_lock over xfs_open_zone_put on unmount

In reply to Christoph Hellwig

xfs_open_zone_put will soon grow code that could sleep. The only caller that currently doesn't allow that is xfs_free_open_zones, but that can easily drop zi_open_zones_lock because at this point nothing else is looking at the open zone list.

---
 fs/xfs/xfs_zone_alloc.c | 3 +++
 1 file changed, 3 insertions(+)
fs/xfs/xfs_zone_alloc.c +3 -0
index f827ea3d899a..8909c8d0f5fc 100644
@@ -1037,7 +1037,10 @@ xfs_free_open_zones(
 	while ((oz = list_first_entry_or_null(&zi->zi_open_zones,
 			struct xfs_open_zone, oz_entry))) {
 		list_del(&oz->oz_entry);
+		spin_unlock(&zi->zi_open_zones_lock);
+
 		xfs_open_zone_put(oz);
+		spin_lock(&zi->zi_open_zones_lock);
 	}
 	spin_unlock(&zi->zi_open_zones_lock);
 
Signature · 1 line
2.53.0
#13 lore raw

PATCH 9/25 xfs: put the open zone outside RCU protection in xfs_get_cached_zone

In reply to Christoph Hellwig

With the RT data checksum support, xfs_open_zone_put will need a sleepable context, so move it out of the RCU critical section.

---
 fs/xfs/xfs_zone_alloc.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/xfs/xfs_zone_alloc.c +2 -2
index 8909c8d0f5fc..509420c63e49 100644
@@ -842,9 +842,9 @@ xfs_get_cached_zone(
 		if (oz && oz->oz_allocated == rtg_blocks(oz->oz_rtg)) {
 			VFS_I(ip)->i_private = NULL;
 			spin_unlock(&ip->i_flags_lock);
+			rcu_read_unlock();
 			xfs_open_zone_put(oz);
-			oz = NULL;
-			goto out_unlock;
+			return NULL;
 		}
 		spin_unlock(&ip->i_flags_lock);
 	}
Signature · 1 line
2.53.0
#14 lore raw

PATCH 10/25 xfs: define the RT data checksum on-disk format

In reply to Christoph Hellwig

Add the on-disk format for the new RT data checksum format.

Keyed off a new read-only compat feature flag, this adds new fields to the superblock to indicate the checksum algorithm used and the size of the blocks containing the checksums. These new fields reuse the previously reserved padding to make efficient use of the space in the on-disk superblock.

Data checksums are only supported on zoned RT devices, because they require out of places writes to safely update the checksums for file overwrites and a data/metadata split to be able to store the checksums for a group in a file without causing recursion. This means they can't be supported directly on the data device at all, and only when using the always_cow mode on regular RT devices, but that has no benefit over the zoned allocator which is designed for out of place writes.

The initially supported data checksum algorithms are crc32c and crc64 as specified by NVMe. Both have extremely fast kernel implementations and the strong data protection guarantees offered by CRC-style algorithms. Both also happen to be support by NVMe for protection information so that the userspace PI passthrough support (once extended to files on file systems) can be reused to expose the checksums to applications and thus provide true end-to-end data integrity.

---
 fs/xfs/libxfs/xfs_format.h     | 43 +++++++++++++++--
 fs/xfs/libxfs/xfs_log_format.h |  1 +
 fs/xfs/libxfs/xfs_ondisk.h     |  4 +-
 fs/xfs/libxfs/xfs_sb.c         | 86 ++++++++++++++++++++++++++++++++++
 fs/xfs/libxfs/xfs_sb.h         |  1 +
 fs/xfs/scrub/agheader.c        |  5 ++
 fs/xfs/xfs_mount.h             |  4 ++
 7 files changed, 140 insertions(+), 4 deletions(-)
fs/xfs/libxfs/xfs_format.h +40 -3
index 469e879149ac..3d794a2eb497 100644
@@ -179,7 +179,9 @@ typedef struct xfs_sb {
 	xfs_rgnumber_t	sb_rgcount;	/* number of realtime groups */
 	xfs_rtxlen_t	sb_rgextents;	/* size of a realtime group in rtx */
 	uint8_t		sb_rgblklog;    /* rt group number shift */
-	uint8_t		sb_pad[7];	/* zeroes */
+	uint8_t		sb_rtcsum_type;	/* RT device data checksum type */
+	uint8_t		sb_rtcsum_blklog; /* log2 of rtcsum bsize */
+	uint8_t		sb_pad[5];	/* zero */
 	xfs_rfsblock_t	sb_rtstart;	/* start of internal RT section (FSB) */
 	xfs_filblks_t	sb_rtreserved;	/* reserved (zoned) RT blocks */
 
@@ -272,7 +274,9 @@ struct xfs_dsb {
 	__be32		sb_rgcount;	/* # of realtime groups */
 	__be32		sb_rgextents;	/* size of rtgroup in rtx */
 	__u8		sb_rgblklog;    /* rt group number shift */
-	__u8		sb_pad[7];	/* zeroes */
+	__u8		sb_rtcsum_type;	/* RT device data checksum type */
+	__u8		sb_rtcsum_blklog; /* log2 of rtcsum bsize */
+	__u8		sb_pad[5];	/* zero */
 	__be64		sb_rtstart;	/* start of internal RT section (FSB) */
 	__be64		sb_rtreserved;	/* reserved (zoned) RT blocks */
 
@@ -374,6 +378,7 @@ xfs_sb_has_compat_feature(
 #define XFS_SB_FEAT_RO_COMPAT_RMAPBT   (1 << 1)		/* reverse map btree */
 #define XFS_SB_FEAT_RO_COMPAT_REFLINK  (1 << 2)		/* reflinked files */
 #define XFS_SB_FEAT_RO_COMPAT_INOBTCNT (1 << 3)		/* inobt block counts */
+
 #define XFS_SB_FEAT_RO_COMPAT_ALL \
 		(XFS_SB_FEAT_RO_COMPAT_FINOBT | \
 		 XFS_SB_FEAT_RO_COMPAT_RMAPBT | \
@@ -399,6 +404,7 @@ xfs_sb_has_ro_compat_feature(
 #define XFS_SB_FEAT_INCOMPAT_METADIR	(1 << 8)  /* metadata dir tree */
 #define XFS_SB_FEAT_INCOMPAT_ZONED	(1 << 9)  /* zoned RT allocator */
 #define XFS_SB_FEAT_INCOMPAT_ZONE_GAPS	(1 << 10) /* RTGs have LBA gaps */
+#define XFS_SB_FEAT_INCOMPAT_RTCSUM     (1 << 11) /* RT data checksums */
 
 #define XFS_SB_FEAT_INCOMPAT_ALL \
 		(XFS_SB_FEAT_INCOMPAT_FTYPE | \
@@ -866,6 +872,7 @@ enum xfs_metafile_type {
 	XFS_METAFILE_RTSUMMARY,		/* rt summary */
 	XFS_METAFILE_RTRMAP,		/* rt rmap */
 	XFS_METAFILE_RTREFCOUNT,	/* rt refcount */
+	XFS_METAFILE_RTCSUM,		/* rt data checksums */
 
 	XFS_METAFILE_MAX
 } __packed;
@@ -879,7 +886,8 @@ enum xfs_metafile_type {
 	{ XFS_METAFILE_RTBITMAP,	"rtbitmap" }, \
 	{ XFS_METAFILE_RTSUMMARY,	"rtsummary" }, \
 	{ XFS_METAFILE_RTRMAP,		"rtrmap" }, \
-	{ XFS_METAFILE_RTREFCOUNT,	"rtrefcount" }
+	{ XFS_METAFILE_RTREFCOUNT,	"rtrefcount" }, \
+	{ XFS_METAFILE_RTCSUM,		"rtcsum", }
 
 /*
  * On-disk inode structure.
@@ -1322,6 +1330,7 @@ static inline bool xfs_dinode_is_metadir(const struct xfs_dinode *dip)
  */
 #define XFS_RTBITMAP_MAGIC	0x424D505A	/* BMPZ */
 #define XFS_RTSUMMARY_MAGIC	0x53554D59	/* SUMY */
+#define XFS_RTCSUM_MAGIC	0x4353554D	/* CSUM */
 
 struct xfs_rtbuf_blkinfo {
 	__be32		rt_magic;	/* validity check on block */
@@ -2031,4 +2040,32 @@ struct xfs_acl {
 #define SGI_ACL_FILE_SIZE	(sizeof(SGI_ACL_FILE)-1)
 #define SGI_ACL_DEFAULT_SIZE	(sizeof(SGI_ACL_DEFAULT)-1)
 
+/*
+ * Size of a RT data checksum block.  Data reads must be contained in a single
+ * block, so this should be fairly large.
+ *
+ * The default is 32k, matching the default inode cluster size and the maximum
+ * memory allocation the Linux MM can handle in the fast path.  64k is primarily
+ * there so that this value never needs to be below the FSB size, even for 64k
+ * blocks.
+ */
+#define XFS_RTCSUM_BSIZE_LOG_MIN	15
+#define XFS_RTCSUM_BSIZE_LOG_MAX	16
+
+/*
+ * Data checksum types.
+ */
+#define XFS_CSUM_TYPE_NONE	0u
+#define XFS_CSUM_TYPE_CRC32C	1u
+#define XFS_CSUM_TYPE_CRC64	2u
+#define XFS_CSUM_TYPE_MAX	3u
+
+/*
+ * On-disk data checksums.
+ */
+union xfs_disk_csum {
+	__le32			crc32c;
+	__le64			crc64;
+};
+
 #endif /* __XFS_FORMAT_H__ */
fs/xfs/libxfs/xfs_log_format.h +1 -0
index 6e620380d5dc..c6f0edb4bf0a 100644
@@ -581,6 +581,7 @@ enum xfs_blft {
 	XFS_BLFT_SB_BUF,
 	XFS_BLFT_RTBITMAP_BUF,
 	XFS_BLFT_RTSUMMARY_BUF,
+	XFS_BLFT_RTCSUM_BUF,
 	XFS_BLFT_MAX_BUF = (1 << XFS_BLFT_BITS),
 };
 
fs/xfs/libxfs/xfs_ondisk.h +3 -1
index 23cde1248f01..17ab9366b3b9 100644
@@ -284,7 +284,9 @@ xfs_check_ondisk_structs(void)
 	XFS_CHECK_SB_OFFSET(sb_rgcount,			272);
 	XFS_CHECK_SB_OFFSET(sb_rgextents,		276);
 	XFS_CHECK_SB_OFFSET(sb_rgblklog,		280);
-	XFS_CHECK_SB_OFFSET(sb_pad,			281);
+	XFS_CHECK_SB_OFFSET(sb_rtcsum_type,		281);
+	XFS_CHECK_SB_OFFSET(sb_rtcsum_blklog,		282);
+	XFS_CHECK_SB_OFFSET(sb_pad,			283);
 	XFS_CHECK_SB_OFFSET(sb_rtstart,			288);
 	XFS_CHECK_SB_OFFSET(sb_rtreserved,		296);
 
fs/xfs/libxfs/xfs_sb.c +86 -0

132 lines not on this page · raw message

fs/xfs/libxfs/xfs_sb.h +1 -0
index 77de65922213..298d70f2a72e 100644
@@ -19,6 +19,7 @@ extern void	xfs_sb_mount_common(struct xfs_mount *mp, struct xfs_sb *sbp);
 void		xfs_sb_mount_rextsize(struct xfs_mount *mp, struct xfs_sb *sbp);
 void		xfs_mount_sb_set_rextsize(struct xfs_mount *mp,
 			struct xfs_sb *sbp, xfs_agblock_t rextsize);
+uint8_t		xfs_data_csum_shift(uint8_t csum);
 extern void	xfs_sb_from_disk(struct xfs_sb *to, struct xfs_dsb *from);
 extern void	xfs_sb_to_disk(struct xfs_dsb *to, struct xfs_sb *from);
 extern void	xfs_sb_quota_from_disk(struct xfs_sb *sbp);
fs/xfs/scrub/agheader.c +5 -0
index be59b0b49547..b074212fbfff 100644
@@ -445,6 +445,11 @@ xchk_superblock(
 
 		if (memchr_inv(sb->sb_pad, 0, sizeof(sb->sb_pad)))
 			xchk_block_set_corrupt(sc, bp);
+
+		if (sb->sb_rtcsum_type != mp->m_sb.sb_rtcsum_type)
+			xchk_block_set_corrupt(sc, bp);
+		if (sb->sb_rtcsum_blklog != mp->m_sb.sb_rtcsum_blklog)
+			xchk_block_set_corrupt(sc, bp);
 	}
 
 	if (xfs_has_zoned(mp)) {
fs/xfs/xfs_mount.h +4 -0
index cbcfb89c67a4..0e60d5c29c9d 100644
@@ -191,6 +191,8 @@ typedef struct xfs_mount {
 	uint8_t			m_agno_log;	/* log #ag's */
 	uint8_t			m_sectbb_log;	/* sectlog - BBSHIFT */
 	int8_t			m_rtxblklog;	/* log2 of rextsize, if possible */
+	uint8_t			m_rtcsum_shift;	/* log2 of RT data csum size */
+	uint32_t		m_rtcsum_bsize;	/* rtcsum block size in bytes */
 
 	uint			m_blockmask;	/* sb_blocksize-1 */
 	uint			m_blockwsize;	/* sb_blocksize in words */
@@ -403,6 +405,7 @@ typedef struct xfs_mount {
 #define XFS_FEAT_EXCHANGE_RANGE	(1ULL << 27)	/* exchange range */
 #define XFS_FEAT_METADIR	(1ULL << 28)	/* metadata directory tree */
 #define XFS_FEAT_ZONED		(1ULL << 29)	/* zoned RT device */
+#define XFS_FEAT_RTCSUM		(1ULL << 30)	/* RT data checksums */
 
 /* Mount features */
 #define XFS_FEAT_NOLIFETIME	(1ULL << 47)	/* disable lifetime hints */
@@ -460,6 +463,7 @@ __XFS_HAS_FEAT(exchange_range, EXCHANGE_RANGE)
 __XFS_HAS_FEAT(metadir, METADIR)
 __XFS_HAS_FEAT(zoned, ZONED)
 __XFS_HAS_FEAT(nolifetime, NOLIFETIME)
+__XFS_HAS_FEAT(rtcsum, RTCSUM)
 
 static inline bool xfs_has_rtgroups(const struct xfs_mount *mp)
 {
Signature · 1 line
2.53.0
#15 lore raw

PATCH 11/25 xfs: add support for per-RTG csum files

In reply to Christoph Hellwig

Add the definitions for another per-RTG file that stores data checksums for the RTG. The file is fully preallocated at mkfs/growfs time, and thus bmap lookups for it can be performed without taking locks.

Checksums are organized in fixed size large (initially 32Kib or 64KiB) blocks to reduce the lookup and read overhead compare to using the smaller file system block size.

Each block uses the standard RT file header for self-describing metadata and can thus reuse the buf_ops including the verifier.

---
 fs/xfs/Makefile                |   1 +
 fs/xfs/libxfs/xfs_cksum.h      |   7 +-
 fs/xfs/libxfs/xfs_fs.h         |   1 +
 fs/xfs/libxfs/xfs_health.h     |   4 +-
 fs/xfs/libxfs/xfs_rtbitmap.c   |  10 +++
 fs/xfs/libxfs/xfs_rtcsumfile.c |  95 +++++++++++++++++++++
 fs/xfs/libxfs/xfs_rtcsumfile.h | 150 +++++++++++++++++++++++++++++++++
 fs/xfs/libxfs/xfs_rtgroup.c    |  10 +++
 fs/xfs/libxfs/xfs_rtgroup.h    |   6 ++
 fs/xfs/libxfs/xfs_shared.h     |   1 +
 fs/xfs/xfs_buf_item_recover.c  |  10 ++-
 fs/xfs/xfs_health.c            |   1 +
 fs/xfs/xfs_platform.h          |   1 +
 fs/xfs/xfs_rtalloc.c           |   7 ++
 14 files changed, 301 insertions(+), 3 deletions(-)
 create mode 100644 fs/xfs/libxfs/xfs_rtcsumfile.c
 create mode 100644 fs/xfs/libxfs/xfs_rtcsumfile.h
fs/xfs/Makefile +1 -0
index 399a207f2d0e..79ea4136fbba 100644
@@ -64,6 +64,7 @@ xfs-y				+= $(addprefix libxfs/, \
 xfs-$(CONFIG_XFS_RT)		+= $(addprefix libxfs/, \
 				   xfs_rtbitmap.o \
 				   xfs_rtgroup.o \
+				   xfs_rtcsumfile.o \
 				   xfs_zones.o \
 				   )
 
fs/xfs/libxfs/xfs_cksum.h +6 -1
index 999a290cfd72..315b85ff78ae 100644
@@ -2,7 +2,12 @@
 #ifndef _XFS_CKSUM_H
 #define _XFS_CKSUM_H 1
 
-#define XFS_CRC_SEED	(~(uint32_t)0)
+/*
+ * crc32c() does not include the inversion at the beginning and end, while
+ * crc64_nvme() does.
+ */
+#define XFS_CRC_SEED		(~(uint32_t)0)
+#define XFS_CRC64_SEED		0
 
 /*
  * Calculate the intermediate checksum for a buffer that has the CRC field
fs/xfs/libxfs/xfs_fs.h +1 -0
index 185f09f327c0..226cb96dfae8 100644
@@ -1003,6 +1003,7 @@ struct xfs_rtgroup_geometry {
 #define XFS_RTGROUP_GEOM_SICK_SUMMARY	(1U << 2)  /* rtsummary */
 #define XFS_RTGROUP_GEOM_SICK_RMAPBT	(1U << 3)  /* reverse mappings */
 #define XFS_RTGROUP_GEOM_SICK_REFCNTBT	(1U << 4)  /* reference counts */
+#define XFS_RTGROUP_GEOM_SICK_CSUM	(1U << 5)  /* data csums */
 
 #define XFS_RTGROUP_GEOM_WRITEPOINTER  (1U << 0)  /* write pointer */
 
fs/xfs/libxfs/xfs_health.h +3 -1
index 1d45cf5789e8..349093e75268 100644
@@ -72,6 +72,7 @@ struct xfs_rtgroup;
 #define XFS_SICK_RG_SUMMARY	(1 << 2)  /* rt groups summary */
 #define XFS_SICK_RG_RMAPBT	(1 << 3)  /* reverse mappings */
 #define XFS_SICK_RG_REFCNTBT	(1 << 4)  /* reference counts */
+#define XFS_SICK_RG_CSUM	(1 << 5)  /* data checksums */
 
 /* Observable health issues for AG metadata. */
 #define XFS_SICK_AG_SB		(1 << 0)  /* superblock */
@@ -119,7 +120,8 @@ struct xfs_rtgroup;
 				 XFS_SICK_RG_BITMAP | \
 				 XFS_SICK_RG_SUMMARY | \
 				 XFS_SICK_RG_RMAPBT | \
-				 XFS_SICK_RG_REFCNTBT)
+				 XFS_SICK_RG_REFCNTBT | \
+				 XFS_SICK_RG_CSUM)
 
 #define XFS_SICK_AG_PRIMARY	(XFS_SICK_AG_SB | \
 				 XFS_SICK_AG_AGF | \
fs/xfs/libxfs/xfs_rtbitmap.c +10 -0
index c83d7f961f40..edf7b6e88c7c 100644
@@ -124,9 +124,18 @@ const struct xfs_buf_ops xfs_rtsummary_buf_ops = {
 	.verify_struct	= xfs_rtbuf_verify,
 };
 
+const struct xfs_buf_ops xfs_rtcsum_buf_ops = {
+	.name		= "xfs_rtcsum",
+	.magic		= { 0, cpu_to_be32(XFS_RTCSUM_MAGIC) },
+	.verify_read	= xfs_rtbuf_verify_read,
+	.verify_write	= xfs_rtbuf_verify_write,
+	.verify_struct	= xfs_rtbuf_verify,
+};
+
 static const struct xfs_buf_ops *xfs_rtblock_buf_ops[XFS_RTGI_MAX] = {
 	[XFS_RTGI_SUMMARY]	= &xfs_rtsummary_buf_ops,
 	[XFS_RTGI_BITMAP]	= &xfs_rtbitmap_buf_ops,
+	[XFS_RTGI_CSUM]		= &xfs_rtcsum_buf_ops,
 };
 
 const struct xfs_buf_ops *
@@ -142,6 +151,7 @@ xfs_rtblock_ops(
 static enum xfs_blft xfs_rtblock_buf_types[XFS_RTGI_MAX] = {
 	[XFS_RTGI_SUMMARY]	= XFS_BLFT_RTSUMMARY_BUF,
 	[XFS_RTGI_BITMAP]	= XFS_BLFT_RTBITMAP_BUF,
+	[XFS_RTGI_CSUM]		= XFS_BLFT_RTCSUM_BUF,
 };
 
 /* Release cached rt bitmap and summary buffers. */
fs/xfs/libxfs/xfs_rtcsumfile.c new file +95 -0

95 lines not on this page · raw message

fs/xfs/libxfs/xfs_rtcsumfile.h new file +150 -0

150 lines not on this page · raw message

fs/xfs/libxfs/xfs_rtgroup.c +10 -0
index 91961fb85aff..4864adf6f59e 100644
@@ -34,6 +34,7 @@
 #include "xfs_metadir.h"
 #include "xfs_rtrmap_btree.h"
 #include "xfs_rtrefcount_btree.h"
+#include "xfs_rtcsumfile.h"
 
 /* Find the first usable fsblock in this rtgroup. */
 static inline uint32_t
@@ -399,6 +400,15 @@ static const struct xfs_rtginode_ops xfs_rtginode_ops[XFS_RTGI_MAX] = {
 		.enabled	= xfs_has_reflink,
 		.create		= xfs_rtrefcountbt_create,
 	},
+	[XFS_RTGI_CSUM] = {
+		.name		= "csum",
+		.metafile_type	= XFS_METAFILE_RTCSUM,
+		.sick		= XFS_SICK_RG_CSUM,
+		.fmt_mask	= (1U << XFS_DINODE_FMT_EXTENTS) |
+				  (1U << XFS_DINODE_FMT_BTREE),
+		.enabled	= xfs_has_rtcsum,
+		.create		= xfs_rtcsum_create,
+	},
 };
 
 /* Return the shortname of this rtgroup inode. */
fs/xfs/libxfs/xfs_rtgroup.h +6 -0
index f26e324f0de3..5cca0f4dd25a 100644
@@ -16,6 +16,7 @@ enum xfs_rtg_inodes {
 	XFS_RTGI_SUMMARY,	/* allocation summary */
 	XFS_RTGI_RMAP,		/* rmap btree inode */
 	XFS_RTGI_REFCOUNT,	/* refcount btree inode */
+	XFS_RTGI_CSUM,		/* data checksum inode */
 
 	XFS_RTGI_MAX,
 };
@@ -109,6 +110,11 @@ static inline struct xfs_inode *rtg_refcount(const struct xfs_rtgroup *rtg)
 	return rtg->rtg_inodes[XFS_RTGI_REFCOUNT];
 }
 
+static inline struct xfs_inode *rtg_csum(const struct xfs_rtgroup *rtg)
+{
+	return rtg->rtg_inodes[XFS_RTGI_CSUM];
+}
+
 /* Passive rtgroup references */
 static inline struct xfs_rtgroup *
 xfs_rtgroup_get(
fs/xfs/libxfs/xfs_shared.h +1 -0
index b1e0d9bc1f7d..8a60f2c56fe7 100644
@@ -40,6 +40,7 @@ extern const struct xfs_buf_ops xfs_refcountbt_buf_ops;
 extern const struct xfs_buf_ops xfs_rmapbt_buf_ops;
 extern const struct xfs_buf_ops xfs_rtbitmap_buf_ops;
 extern const struct xfs_buf_ops xfs_rtsummary_buf_ops;
+extern const struct xfs_buf_ops xfs_rtcsum_buf_ops;
 extern const struct xfs_buf_ops xfs_rtbuf_ops;
 extern const struct xfs_buf_ops xfs_rtsb_buf_ops;
 extern const struct xfs_buf_ops xfs_rtrefcountbt_buf_ops;
fs/xfs/xfs_buf_item_recover.c +9 -1
index 57929f115055..28fd1f358ba2 100644
@@ -414,6 +414,13 @@ xlog_recover_validate_buf_type(
 		}
 		bp->b_ops = xfs_rtblock_ops(mp, XFS_RTGI_SUMMARY);
 		break;
+	case XFS_BLFT_RTCSUM_BUF:
+		if (xfs_has_rtgroups(mp) && magic32 != XFS_RTCSUM_MAGIC) {
+			warnmsg = "Bad rtcsum magic!";
+			break;
+		}
+		bp->b_ops = &xfs_rtcsum_buf_ops;
+		break;
 #endif /* CONFIG_XFS_RT */
 	default:
 		xfs_warn(mp, "Unknown buffer type %d!",
@@ -869,7 +876,8 @@ xlog_recover_get_buf_lsn(
 	magic32 = be32_to_cpu(*(__be32 *)blk);
 	switch (magic32) {
 	case XFS_RTSUMMARY_MAGIC:
-	case XFS_RTBITMAP_MAGIC: {
+	case XFS_RTBITMAP_MAGIC:
+	case XFS_RTCSUM_MAGIC: {
 		struct xfs_rtbuf_blkinfo	*hdr = blk;
 
 		lsn = be64_to_cpu(hdr->rt_lsn);
fs/xfs/xfs_health.c +1 -0
index 239b843e83d4..dec452b5b15f 100644
@@ -571,6 +571,7 @@ static const struct ioctl_sick_map rtgroup_map[] = {
 	{ XFS_SICK_RG_SUMMARY,	XFS_RTGROUP_GEOM_SICK_SUMMARY },
 	{ XFS_SICK_RG_RMAPBT,	XFS_RTGROUP_GEOM_SICK_RMAPBT },
 	{ XFS_SICK_RG_REFCNTBT,	XFS_RTGROUP_GEOM_SICK_REFCNTBT },
+	{ XFS_SICK_RG_CSUM,	XFS_RTGROUP_GEOM_SICK_CSUM },
 };
 
 /* Fill out rtgroup geometry health info. */
fs/xfs/xfs_platform.h +1 -0
index 981488778ccc..2c52b004e82b 100644
@@ -16,6 +16,7 @@
 #include <linux/slab.h>
 #include <linux/vmalloc.h>
 #include <linux/crc32c.h>
+#include <linux/crc64.h>
 #include <linux/module.h>
 #include <linux/mutex.h>
 #include <linux/file.h>
fs/xfs/xfs_rtalloc.c +7 -0
index 78a1c066c7eb..e2b6113772b6 100644
@@ -32,6 +32,7 @@
 #include "xfs_error.h"
 #include "xfs_trace.h"
 #include "xfs_rtrefcount_btree.h"
+#include "xfs_rtcsumfile.h"
 #include "xfs_reflink.h"
 #include "xfs_zone_alloc.h"
 
@@ -898,6 +899,12 @@ xfs_growfs_rt_zoned(
 	xfs_rtbxlen_t		freed_rtx;
 	int			error;
 
+	if (xfs_has_rtcsum(mp)) {
+		error = xfs_rtcsum_alloc_blocks(rtg);
+		if (error)
+			return error;
+	}
+
 	/*
 	 * Calculate new sb and mount fields for this round.  Also ensure the
 	 * rtg_extents value is uptodate as the rtbitmap code relies on it.
Signature · 1 line
2.53.0
#16 lore raw

PATCH 12/25 xfs: calculate the log reservation for logging data checksum buffers

In reply to Christoph Hellwig

The data checksum is logged in its own transaction, which only logs the checksum buffers. While the maximum size of a checksummed data write is the same as that of a single checksum buffer, the Zone Append based zoned write path can't guarantee alignment, so a reservation for an extra slack buffer is needed.

---
 fs/xfs/libxfs/xfs_trans_resv.c | 23 +++++++++++++++++++++++
 fs/xfs/libxfs/xfs_trans_resv.h |  4 ++++
 2 files changed, 27 insertions(+)
fs/xfs/libxfs/xfs_trans_resv.c +23 -0
index b09c88aec185..25f7581ea90c 100644
@@ -11,6 +11,7 @@
 #include "xfs_log_format.h"
 #include "xfs_trans_resv.h"
 #include "xfs_mount.h"
+#include "xfs_rtcsumfile.h"
 #include "xfs_da_format.h"
 #include "xfs_da_btree.h"
 #include "xfs_inode.h"
@@ -1235,6 +1236,24 @@ xfs_calc_qm_dqalloc_reservation_minlogsize(
 	return xfs_calc_qm_dqalloc_reservation(mp, true);
 }
 
+/*
+ * Log data checksums for a write.
+ *
+ * Writes do not have to be aligned to checksum file blocks and thus can
+ * spill into an extra buffer that needs to be accounted for.
+ */
+unsigned int
+xfs_calc_csum_reservation(
+	struct xfs_mount	*mp,
+	unsigned int		csum_len)
+{
+	unsigned int		payload_size = xfs_rtcsum_payload_size(mp);
+
+	return xfs_calc_buf_res(
+			howmany(csum_len + payload_size, payload_size),
+			mp->m_rtcsum_bsize);
+}
+
 /*
  * Syncing the incore super block changes to disk.
  *     the super block to reflect the changes: sector size
@@ -1355,6 +1374,10 @@ xfs_trans_resv_calc(
 
 	xfs_calc_namespace_reservations(mp, resp);
 
+	resp->tr_csum.tr_logres =
+		xfs_calc_csum_reservation(mp, XFS_RTCSUM_MAX_WRITE);
+	resp->tr_csum.tr_logcount = XFS_DEFAULT_LOG_COUNT;
+
 	/*
 	 * The following transactions are logged in logical format with
 	 * a default log count.
fs/xfs/libxfs/xfs_trans_resv.h +4 -0
index 336279e0fc61..fbd978a91cc8 100644
@@ -49,6 +49,7 @@ struct xfs_trans_resv {
 	struct xfs_trans_res	tr_sb;		/* modify superblock */
 	struct xfs_trans_res	tr_fsyncts;	/* update timestamps on fsync */
 	struct xfs_trans_res	tr_atomic_ioend; /* untorn write completion */
+	struct xfs_trans_res	tr_csum;	/* data checksums in metafile */
 };
 
 /* shorthand way of accessing reservation structure */
@@ -121,6 +122,9 @@ unsigned int xfs_calc_itruncate_reservation_minlogsize(struct xfs_mount *mp);
 unsigned int xfs_calc_write_reservation_minlogsize(struct xfs_mount *mp);
 unsigned int xfs_calc_qm_dqalloc_reservation_minlogsize(struct xfs_mount *mp);
 
+unsigned int xfs_calc_csum_reservation(struct xfs_mount *mp,
+		unsigned int csum_len);
+
 xfs_extlen_t xfs_calc_max_atomic_write_fsblocks(struct xfs_mount *mp);
 xfs_extlen_t xfs_calc_atomic_write_log_geometry(struct xfs_mount *mp,
 		xfs_extlen_t blockcount, unsigned int *new_logres);
Signature · 1 line
2.53.0
#17 lore raw

PATCH 13/25 xfs: core RT data checksum support

In reply to Christoph Hellwig

Support reading and writing of data checksum buffers, and generating and verifying the checksum.

All data checksum buffers for an open zone are pre-allocated at zone open time, so that we never have to read in a partially written buffer as part of a data write, which would otherwise impose very expensive seeks and stall writes.

---
 fs/xfs/Kconfig          |   1 +
 fs/xfs/Makefile         |   1 +
 fs/xfs/xfs_inode.h      |   5 +
 fs/xfs/xfs_rtcsum.c     | 314 ++++++++++++++++++++++++++++++++++++++++
 fs/xfs/xfs_rtcsum.h     |  23 +++
 fs/xfs/xfs_super.c      |  14 ++
 fs/xfs/xfs_sysfs.c      |   2 +
 fs/xfs/xfs_zone_alloc.c |  32 +++-
 fs/xfs/xfs_zone_priv.h  |   8 +
 9 files changed, 396 insertions(+), 4 deletions(-)
 create mode 100644 fs/xfs/xfs_rtcsum.c
 create mode 100644 fs/xfs/xfs_rtcsum.h
fs/xfs/Kconfig +1 -0
index b99da294e9a3..424a04b507a6 100644
@@ -106,6 +106,7 @@ config XFS_RT
 	bool "XFS Realtime subvolume support"
 	depends on XFS_FS
 	default BLK_DEV_ZONED
+	select CRC64
 	help
 	  If you say Y here you will be able to mount and use XFS filesystems
 	  which contain a realtime subvolume.  The realtime subvolume is a
fs/xfs/Makefile +1 -0
index 79ea4136fbba..0d57bf0701ec 100644
@@ -142,6 +142,7 @@ xfs-$(CONFIG_XFS_QUOTA)		+= xfs_dquot.o \
 
 # xfs_rtbitmap is shared with libxfs
 xfs-$(CONFIG_XFS_RT)		+= xfs_rtalloc.o \
+				   xfs_rtcsum.o \
 				   xfs_zone_alloc.o \
 				   xfs_zone_gc.o \
 				   xfs_zone_info.o \
fs/xfs/xfs_inode.h +5 -0
index b16eee99b567..56d5a670ba75 100644
@@ -385,6 +385,11 @@ static inline bool xfs_inode_can_sw_atomic_write(const struct xfs_inode *ip)
 	return xfs_can_sw_atomic_write(ip->i_mount);
 }
 
+static inline bool xfs_is_rtcsum_inode(const struct xfs_inode *ip)
+{
+	return xfs_has_rtcsum(ip->i_mount) && XFS_IS_REALTIME_INODE(ip);
+}
+
 /*
  * In-core inode flags.
  */
fs/xfs/xfs_rtcsum.c new file +314 -0

314 lines not on this page · raw message

fs/xfs/xfs_rtcsum.h new file +23 -0
new file mode 100644
index 000000000000..bd16f5a99451
@@ -0,0 +1,23 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _XFS_RTCSUM_H
+#define _XFS_RTCSUM_H
+
+#include "xfs_rtcsumfile.h"
+
+void xfs_csum_generate(struct xfs_mount *mp, struct bio *bio,
+		void *csum_buf);
+int xfs_csum_verify(struct xfs_mount *mp, struct bio *bio,
+		struct bvec_iter *iter, void *csum_buf,
+		xfs_fsblock_t bno, bool verbose);
+
+int xfs_rtcsum_read_async(struct xfs_mount *mp, xfs_rtblock_t fsbno,
+		struct xfs_buf **bpp);
+int xfs_rtcsum_log(struct xfs_open_zone *oz, xfs_daddr_t daddr,
+		unsigned int data_len, const void *csum);
+
+int xfs_rtcsum_open_zone(struct xfs_open_zone *oz);
+void xfs_rtcsum_free_zone(struct xfs_open_zone *oz);
+
+int xfs_rtcsum_mount(struct xfs_mount *mp);
+
+#endif /* _XFS_RTCSUM_H */
fs/xfs/xfs_super.c +14 -0
index 5a06132aa384..fc99167938e5 100644
@@ -50,6 +50,7 @@
 #include "xfs_rtalloc.h"
 #include "xfs_zone_alloc.h"
 #include "xfs_healthmon.h"
+#include "xfs_rtcsum.h"
 #include "scrub/stats.h"
 #include "scrub/rcbag_btree.h"
 
@@ -1953,6 +1954,19 @@ xfs_fs_fill_super(
 		}
 	}
 
+	if (xfs_has_rtcsum(mp)) {
+		if (!IS_ENABLED(CONFIG_XFS_RT) || !xfs_has_zoned(mp)) {
+			xfs_alert(mp,
+	"zoned allocator required for RT data checksums");
+			error = -EINVAL;
+			goto out_filestream_unmount;
+		}
+
+		error = xfs_rtcsum_mount(mp);
+		if (error)
+			goto out_filestream_unmount;
+	}
+
 	if (xfs_has_reflink(mp)) {
 		if (xfs_has_realtime(mp) &&
 		    !xfs_reflink_supports_rextsize(mp, mp->m_sb.sb_rextsize)) {
fs/xfs/xfs_sysfs.c +2 -0
index 4a6a8c92c3a1..4a451a414312 100644
@@ -401,6 +401,8 @@ static bool
 xfs_has_read_bounce(
 	struct xfs_mount	*mp)
 {
+	if (xfs_has_rtcsum(mp))
+		return true;
 	if (bdev_has_integrity_csum(mp->m_ddev_targp->bt_bdev))
 		return true;
 	if (mp->m_rtdev_targp &&
fs/xfs/xfs_zone_alloc.c +28 -4
index 509420c63e49..0dbc08c58817 100644
@@ -26,6 +26,7 @@
 #include "xfs_zones.h"
 #include "xfs_trace.h"
 #include "xfs_mru_cache.h"
+#include "xfs_rtcsum.h"
 #include <linux/bio-integrity.h>
 
 static void
@@ -42,8 +43,10 @@ void
 xfs_open_zone_put(
 	struct xfs_open_zone	*oz)
 {
-	if (atomic_dec_and_test(&oz->oz_ref))
+	if (atomic_dec_and_test(&oz->oz_ref)) {
+		xfs_rtcsum_free_zone(oz);
 		call_rcu(&oz->oz_rcu, xfs_open_zone_free_rcu);
+	}
 }
 
 static inline uint32_t
@@ -416,9 +419,12 @@ xfs_init_open_zone(
 	enum rw_hint		write_hint,
 	bool			is_gc)
 {
+	unsigned int		nr_csum_bufs = xfs_rtcsum_bufs_per_rtg(rtg);
 	struct xfs_open_zone	*oz;
+	int			error;
 
-	oz = kzalloc_obj(*oz, GFP_NOFS | __GFP_NOFAIL);
+	oz = kzalloc_flex(*oz, oz_csum_bufs, nr_csum_bufs,
+			GFP_NOFS | __GFP_NOFAIL);
 	spin_lock_init(&oz->oz_alloc_lock);
 	atomic_set(&oz->oz_ref, 1);
 	oz->oz_rtg = rtg;
@@ -426,6 +432,14 @@ xfs_init_open_zone(
 	oz->oz_written = write_pointer;
 	oz->oz_write_hint = write_hint;
 	oz->oz_is_gc = is_gc;
+	oz->oz_nr_csum_bufs = nr_csum_bufs;
+	if (nr_csum_bufs) {
+		error = xfs_rtcsum_open_zone(oz);
+		if (error) {
+			kfree(oz);
+			return ERR_PTR(error);
+		}
+	}
 
 	/*
 	 * All dereferences of rtg->rtg_open_zone hold the ILOCK for the rmap
@@ -469,6 +483,7 @@ xfs_open_zone(
 {
 	struct xfs_zone_info	*zi = mp->m_zone_info;
 	XA_STATE		(xas, &mp->m_groups[XG_TYPE_RTG].xa, 0);
+	struct xfs_open_zone	*oz;
 	struct xfs_group	*xg;
 
 	if (!xfs_claim_free_zone(zi, is_gc))
@@ -500,7 +515,14 @@ xfs_open_zone(
 	xas_unlock(&xas);
 
 	set_current_state(TASK_RUNNING);
-	return xfs_init_open_zone(to_rtg(xg), 0, write_hint, is_gc);
+	oz = xfs_init_open_zone(to_rtg(xg), 0, write_hint, is_gc);
+	if (IS_ERR(oz)) {
+		xfs_rtgroup_rele(to_rtg(xg));
+		xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_ONDISK);
+		return NULL;
+	}
+
+	return oz;
 }
 
 static struct xfs_open_zone *
@@ -1174,9 +1196,11 @@ xfs_init_zone(
 		/* zone is open */
 		struct xfs_open_zone *oz;
 
-		atomic_inc(&rtg_group(rtg)->xg_active_ref);
 		oz = xfs_init_open_zone(rtg, write_pointer, WRITE_LIFE_NOT_SET,
 				false);
+		if (IS_ERR(oz))
+			return PTR_ERR(oz);
+		atomic_inc(&rtg_group(rtg)->xg_active_ref);
 		list_add_tail(&oz->oz_entry, &zi->zi_open_zones);
 		zi->zi_nr_open_zones++;
 
fs/xfs/xfs_zone_priv.h +8 -0
index fcb57506d8e6..322a031e6ba0 100644
@@ -42,6 +42,14 @@ struct xfs_open_zone {
 	struct xfs_rtgroup	*oz_rtg;
 
 	struct rcu_head		oz_rcu;
+
+	/*
+	 * Checksum buffers.  All checksum buffers for an open zone are pinned
+	 * into memory so that we never need to read them in to log checksums
+	 * for a write.
+	 */
+	unsigned int		oz_nr_csum_bufs;
+	struct xfs_buf		*oz_csum_bufs[] __counted_by(oz_nr_csum_bufs);
 };
 
 /*
Signature · 1 line
2.53.0
#18 lore raw

PATCH 14/25 xfs: data checksums require stable writes

In reply to Christoph Hellwig

Call mapping_set_stable_writes based on the data checksum flag so that the page cache doesn't change data in-flight as that could corrupt the checksum.

---
 fs/xfs/xfs_inode.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/xfs/xfs_inode.h +2 -1
index 56d5a670ba75..169208e1c7da 100644
@@ -628,7 +628,8 @@ int	xfs_break_layouts(struct inode *inode, uint *iolock,
 
 static inline void xfs_update_stable_writes(struct xfs_inode *ip)
 {
-	if (bdev_stable_writes(xfs_inode_buftarg(ip)->bt_bdev))
+	if (xfs_is_rtcsum_inode(ip) ||
+	    bdev_stable_writes(xfs_inode_buftarg(ip)->bt_bdev))
 		mapping_set_stable_writes(VFS_I(ip)->i_mapping);
 	else
 		mapping_clear_stable_writes(VFS_I(ip)->i_mapping);
Signature · 1 line
2.53.0
#19 lore raw

PATCH 15/25 xfs: require file system block size alignment when using data checksums

In reply to Christoph Hellwig

The checksums cover a whole block, so we can't read or update parts of a block. Report the requirement and enforce it for direct I/O reads. Direct I/O writes already require file system block size alignment when using the zoned allocator, and buffered I/O never does sub-block I/O.

---
 fs/xfs/xfs_file.c  | 14 +++++++++++++-
 fs/xfs/xfs_inode.h | 14 ++++++++++++++
 fs/xfs/xfs_ioend.c |  4 ++--
 fs/xfs/xfs_iops.c  |  8 +++++++-
 4 files changed, 36 insertions(+), 4 deletions(-)
fs/xfs/xfs_file.c +13 -1
index 396aca8c66a9..97b0a976058a 100644
@@ -29,6 +29,7 @@
 #include "xfs_zone_alloc.h"
 #include "xfs_error.h"
 #include "xfs_errortag.h"
+#include "xfs_rtcsum.h"
 
 #include <linux/dax.h>
 #include <linux/falloc.h>
@@ -208,9 +209,20 @@ xfs_file_dio_read(
 	if (ret)
 		return ret;
 	if (mapping_stable_writes(iocb->ki_filp->f_mapping)) {
+		unsigned int		dio_flags = 0;
+
+		/*
+		 * Each checksums covers a whole file system block, and thus
+		 * sub-fsblock reads are not supported for file systems using
+		 * data checksums.
+		 */
+		if (xfs_is_rtcsum_inode(ip))
+			dio_flags |= IOMAP_DIO_FSBLOCK_ALIGNED;
 		ret = iomap_dio_rw(iocb, to, &xfs_read_iomap_ops,
-				&xfs_dio_read_bounce_ops, 0, NULL, 0);
+				&xfs_dio_read_bounce_ops, dio_flags, NULL, 0);
 	} else {
+		ASSERT(!xfs_is_rtcsum_inode(ip));
+
 		ret = iomap_dio_read_simple(iocb, to, xfs_read_iomap_begin);
 		if (ret == -ENOTBLK)
 			ret = iomap_dio_rw(iocb, to, &xfs_read_iomap_ops, NULL,
fs/xfs/xfs_inode.h +14 -0
index 169208e1c7da..7b75483bb43d 100644
@@ -390,6 +390,20 @@ static inline bool xfs_is_rtcsum_inode(const struct xfs_inode *ip)
 	return xfs_has_rtcsum(ip->i_mount) && XFS_IS_REALTIME_INODE(ip);
 }
 
+/*
+ * Each checksums covers a whole file system block, and thus sub-fsblock
+ * reads are not supported for file systems using data checksums.
+ */
+static inline unsigned int
+xfs_min_diosize(
+	struct xfs_inode	*ip,
+	struct block_device	*bdev)
+{
+	if (xfs_is_rtcsum_inode(ip))
+		return i_blocksize(VFS_I(ip));
+	return bdev_logical_block_size(bdev);
+}
+
 /*
  * In-core inode flags.
  */
fs/xfs/xfs_ioend.c +2 -2
index 7f4a4a95afe5..05ab91dd4343 100644
@@ -86,7 +86,7 @@ xfs_read_bounce_and_resubmit(
 		.bi_offset	= ioend->io_bvec_offset,
 	};
 	bio->bi_end_io = xfs_end_bio_bounced;
-	iomap_bounce_read(ioend, bdev_logical_block_size(bio->bi_bdev),
+	iomap_bounce_read(ioend, xfs_min_diosize(ip, bio->bi_bdev),
 			xfs_bounce_submit_ioend);
 	memalloc_nofs_restore(nofs_flag);
 }
@@ -134,7 +134,7 @@ xfs_ioend_submit_read(
 	ioend = iomap_init_ioend(inode, bio, file_offset, ioend_flags);
 	if ((ioend_flags & IOMAP_IOEND_DIRECT) &&
 	    READ_ONCE(mp->m_read_bounce) == XFS_READ_BOUNCE_ALWAYS) {
-		iomap_bounce_read(ioend, bdev_logical_block_size(bio->bi_bdev),
+		iomap_bounce_read(ioend, xfs_min_diosize(ip, bio->bi_bdev),
 				xfs_bounce_submit_ioend);
 		return;
 	}
fs/xfs/xfs_iops.c +7 -1
index d307ec97fac8..11952caad5e6 100644
@@ -581,6 +581,13 @@ xfs_report_dioalign(
 	stat->result_mask |= STATX_DIOALIGN | STATX_DIO_READ_ALIGN;
 	stat->dio_mem_align = bdev_dma_alignment(bdev) + 1;
 
+	/*
+	 * The minimum read size is generally the sector size of the underlying
+	 * device, but when using checksums that whole FSB is needed to verify
+	 * the checksum.
+	 */
+	stat->dio_read_offset_align = xfs_min_diosize(ip, bdev);
+
 	/*
 	 * For COW inodes, we can only perform out of place writes of entire
 	 * allocation units (blocks or RT extents).
@@ -591,7 +598,6 @@ xfs_report_dioalign(
 	 * alignment in dio_offset_align, and the smaller read alignment in
 	 * dio_read_offset_align.
 	 */
-	stat->dio_read_offset_align = bdev_logical_block_size(bdev);
 	if (xfs_is_cow_inode(ip))
 		stat->dio_offset_align = xfs_inode_alloc_unitsize(ip);
 	else
Signature · 1 line
2.53.0
#20 lore raw

PATCH 16/25 xfs: add support for reading with data checksums

In reply to Christoph Hellwig

All reads from files with data checksums have the returned iomaps for data blocks limited to be inside a single RT csum file block, so that each data read only needs to deal with a single checksum buffer.

All reads on checksummed files need to use ioends so that the checksum can be verified from process context. The ioend submission path looks up the checksum buffer and kicks of an asynchronous read of it. The completion path waits for the buffer if needed and verifies the checksum.

---
 fs/xfs/xfs_aops.c  |   3 +-
 fs/xfs/xfs_ioend.c | 120 +++++++++++++++++++++++++++++++++++++++------
 fs/xfs/xfs_iomap.c |  23 +++++++--
 3 files changed, 126 insertions(+), 20 deletions(-)
fs/xfs/xfs_aops.c +2 -1
index c30e688cfc9f..db834e5e60c1 100644
@@ -601,7 +601,8 @@ xfs_get_iomap_read_ops(
 {
 	struct xfs_inode		*ip = XFS_I(mapping->host);
 
-	if (bdev_has_integrity_csum(xfs_inode_buftarg(ip)->bt_bdev))
+	if (xfs_has_rtcsum(ip->i_mount) ||
+	    bdev_has_integrity_csum(xfs_inode_buftarg(ip)->bt_bdev))
 		return &xfs_iomap_read_ops;
 	return &iomap_bio_read_ops;
 }
fs/xfs/xfs_ioend.c +105 -15

164 lines not on this page · raw message

fs/xfs/xfs_iomap.c +19 -4
index c3465204b8d5..80b7c9df7c14 100644
@@ -32,6 +32,7 @@
 #include "xfs_rtbitmap.h"
 #include "xfs_icache.h"
 #include "xfs_zone_alloc.h"
+#include "xfs_rtcsum.h"
 
 #define XFS_ALLOC_ALIGN(mp, off) \
 	(((off) >> mp->m_allocsize_log) << mp->m_allocsize_log)
@@ -166,6 +167,8 @@ xfs_bmbt_to_iomap(
 	}
 
 	iomap->validity_cookie = sequence_cookie;
+	if (xfs_is_rtcsum_inode(ip))
+		iomap->csum_shift = mp->m_rtcsum_shift;
 	return 0;
 }
 
@@ -2249,16 +2252,28 @@ xfs_read_iomap_begin(
 		return error;
 	error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, &imap,
 			       &nimaps, 0);
-	if (!error && ((flags & IOMAP_REPORT) || IS_DAX(inode)))
+	if (error)
+		goto out_unlock;
+
+	if ((flags & IOMAP_REPORT) || IS_DAX(inode)) {
 		error = xfs_reflink_trim_around_shared(ip, &imap, &shared);
+		if (error)
+			goto out_unlock;
+	} else if (!isnullstartblock(imap.br_startblock) &&
+		   xfs_is_rtcsum_inode(ip)) {
+		imap.br_blockcount = min(imap.br_blockcount,
+				xfs_rtcsum_max_len(mp, imap.br_startblock));
+	}
+
 	seq = xfs_iomap_inode_sequence(ip, shared ? IOMAP_F_SHARED : 0);
 	xfs_iunlock(ip, lockmode);
-
-	if (error)
-		return error;
 	trace_xfs_iomap_found(ip, offset, length, XFS_DATA_FORK, &imap);
 	return xfs_bmbt_to_iomap(ip, iomap, &imap, flags,
 				 shared ? IOMAP_F_SHARED : 0, seq);
+
+out_unlock:
+	xfs_iunlock(ip, lockmode);
+	return error;
 }
 
 static DEFINE_IOMAP_ITER_NEXT(xfs_read_iomap_next, xfs_read_iomap_begin);
Signature · 1 line
2.53.0
#21 lore raw

Re: [PATCH 16/25] xfs: add support for reading with data checksums

Quoted 21 lines · Christoph Hellwig

All reads from files with data checksums have the returned iomaps for data blocks limited to be inside a single RT csum file block, so that each data read only needs to deal with a single checksum buffer.

All reads on checksummed files need to use ioends so that the checksum can be verified from process context. The ioend submission path looks up the checksum buffer and kicks of an asynchronous read of it. The completion path waits for the buffer if needed and verifies the checksum.

---
 fs/xfs/xfs_aops.c  |   3 +-
 fs/xfs/xfs_ioend.c | 120 +++++++++++++++++++++++++++++++++++++++------
 fs/xfs/xfs_iomap.c |  23 +++++++--
 3 files changed, 126 insertions(+), 20 deletions(-)
fs/xfs/xfs_aops.c +0 -0
index c30e688cfc9f..db834e5e60c1 100644

fs/xfs/xfs_aops.c xfs_get_iomap_read_ops( · line 604

Quoted 5 lines
fs/xfs/xfs_aops.c +1 -1
@@ -601,7 +601,8 @@ xfs_get_iomap_read_ops(
 {
 	struct xfs_inode		*ip = XFS_I(mapping->host);
 
-	if (bdev_has_integrity_csum(xfs_inode_buftarg(ip)->bt_bdev))
+	if (xfs_has_rtcsum(ip->i_mount) ||

Shouldn't this be xfs_is_rtcsum_inode(ip) instead of a per mount option?

Quoted 4 lines
fs/xfs/xfs_aops.c +1 -0
+	    bdev_has_integrity_csum(xfs_inode_buftarg(ip)->bt_bdev))
 		return &xfs_iomap_read_ops;
 	return &iomap_bio_read_ops;
 }
Signature · 1 line
Pankaj
#22 lore raw

Re: [PATCH 16/25] xfs: add support for reading with data checksums

Quoted 8 lines · Pankaj Raghav (Samsung)
Quoted 8 lines
fs/xfs/xfs_aops.c +0 -0
index c30e688cfc9f..db834e5e60c1 100644
@@ -601,7 +601,8 @@ xfs_get_iomap_read_ops(
 {
 	struct xfs_inode		*ip = XFS_I(mapping->host);
 
Quoted 4 lines

fs/xfs/xfs_aops.c xfs_get_iomap_read_ops( · line 604

Quoted 2 lines
fs/xfs/xfs_aops.c +1 -1
-	if (bdev_has_integrity_csum(xfs_inode_buftarg(ip)->bt_bdev))
+	if (xfs_has_rtcsum(ip->i_mount) ||

Shouldn't this be xfs_is_rtcsum_inode(ip) instead of a per mount option?

Yes.

#23 lore raw

PATCH 17/25 xfs: add support for writing with data checksums

In reply to Christoph Hellwig

All write to checksummed files need to use ioends so that the checksum can be verified from process context.

The ioend submission path allocates the csum buffer and attaches it to the ioend before generating the checksum from the file data. The I/O completion then logs the checksums into the buffers for the LBAs that were written.

---
 fs/xfs/xfs_file.c       |  7 +++---
 fs/xfs/xfs_ioend.c      | 51 +++++++++++++++++++++++++++++++++++++++++
 fs/xfs/xfs_ioend.h      |  2 ++
 fs/xfs/xfs_iomap.c      | 49 +++++++++++++++++++++++++++++++++++----
 fs/xfs/xfs_iomap.h      |  4 +++-
 fs/xfs/xfs_reflink.c    |  2 +-
 fs/xfs/xfs_zone_alloc.c |  4 ++++
 7 files changed, 109 insertions(+), 10 deletions(-)
fs/xfs/xfs_file.c +4 -3
index 97b0a976058a..c522913309c3 100644
@@ -1012,7 +1012,8 @@ xfs_file_buffered_write(
 
 	trace_xfs_file_buffered_write(iocb, from);
 	ret = iomap_file_buffered_write(iocb, from,
-			&xfs_buffered_write_iomap_ops, &xfs_iomap_write_ops,
+			&xfs_buffered_write_iomap_ops,
+			xfs_get_iomap_write_ops(ip),
 			NULL);
 
 	/*
@@ -1093,8 +1094,8 @@ xfs_file_buffered_write_zoned(
 retry:
 	trace_xfs_file_buffered_write(iocb, from);
 	ret = iomap_file_buffered_write(iocb, from,
-			&xfs_buffered_write_iomap_ops, &xfs_iomap_write_ops,
-			&ac);
+			&xfs_buffered_write_iomap_ops,
+			xfs_get_iomap_write_ops(ip), &ac);
 	if (ret == -ENOSPC && !cleared_space) {
 		/*
 		 * Kick off writeback to convert delalloc space and release the
fs/xfs/xfs_ioend.c +51 -0

63 lines not on this page · raw message

fs/xfs/xfs_ioend.h +2 -0
index 7c2a1ea3e6ed..f01aa208c61f 100644
@@ -14,5 +14,7 @@ static inline bool xfs_ioend_is_append(struct iomap_ioend *ioend)
 void xfs_end_bio(struct bio *bio);
 void xfs_ioend_submit_read(struct inode *inode, struct bio *bio,
 		loff_t file_offset, u16 ioend_flags);
+int xfs_ioend_submit_read_sync(struct bio *bio, struct inode *inode,
+		loff_t file_offset, u16 ioend_flags);
 
 #endif /* __XFS_IOEND_H */
fs/xfs/xfs_iomap.c +44 -5

80 lines not on this page · raw message

fs/xfs/xfs_iomap.h +3 -1
index 8fc0c6fc8bea..81fff7cad59b 100644
@@ -44,6 +44,7 @@ xfs_iomap_set_anon_write(
 	iomap->flags = IOMAP_F_ANON_WRITE | IOMAP_F_DIRTY;
 	if (bdev_has_integrity_csum(iomap->bdev))
 		iomap->flags |= IOMAP_F_INTEGRITY;
+	iomap->csum_shift = ip->i_mount->m_rtcsum_shift;
 }
 
 static inline xfs_filblks_t
@@ -70,6 +71,8 @@ int xfs_read_iomap_begin(struct inode *inode, loff_t offset,
 		loff_t length, unsigned flags, struct iomap *iomap,
 		struct iomap *srcmap);
 
+const struct iomap_write_ops *xfs_get_iomap_write_ops(struct xfs_inode *ip);
+
 extern const struct iomap_ops xfs_buffered_write_iomap_ops;
 extern const struct iomap_ops xfs_direct_write_iomap_ops;
 extern const struct iomap_ops xfs_zoned_direct_write_iomap_ops;
@@ -78,6 +81,5 @@ extern const struct iomap_ops xfs_seek_iomap_ops;
 extern const struct iomap_ops xfs_xattr_iomap_ops;
 extern const struct iomap_ops xfs_dax_write_iomap_ops;
 extern const struct iomap_ops xfs_atomic_write_cow_iomap_ops;
-extern const struct iomap_write_ops xfs_iomap_write_ops;
 
 #endif /* __XFS_IOMAP_H__*/
fs/xfs/xfs_reflink.c +1 -1
index 104542b10d9e..5653aa08ce1c 100644
@@ -1923,7 +1923,7 @@ xfs_reflink_unshare(
 	else
 		error = iomap_file_unshare(inode, offset, len,
 				&xfs_buffered_write_iomap_ops,
-				&xfs_iomap_write_ops);
+				xfs_get_iomap_write_ops(ip));
 	if (error)
 		goto out;
 
fs/xfs/xfs_zone_alloc.c +4 -0
index 0dbc08c58817..5c5f42f3262c 100644
@@ -964,6 +964,10 @@ xfs_zone_alloc_and_submit(
 
 	if (ioend->io_flags & IOMAP_IOEND_INTEGRITY)
 		fs_bio_integrity_generate(&ioend->io_bio);
+	if (xfs_is_rtcsum_inode(ip)) {
+		xfs_csum_generate(mp, &ioend->io_bio,
+				iomap_csum_alloc(ioend, mp->m_rtcsum_shift));
+	}
 
 	/*
 	 * If we don't have a locally cached zone in this write context, see if
Signature · 1 line
2.53.0
#24 lore raw

PATCH 18/25 xfs: add data checksum support to zoned garbage collection

In reply to Christoph Hellwig

Transfer the checksum from the old location to the new one. The implementation mirrors that of user data reads and writes.

---
 fs/xfs/xfs_zone_gc.c | 66 ++++++++++++++++++++++++++++++++++++--------
 1 file changed, 54 insertions(+), 12 deletions(-)
fs/xfs/xfs_zone_gc.c +54 -12
index cea5447a886d..9296f584282a 100644
@@ -21,6 +21,7 @@
 #include "xfs_zone_alloc.h"
 #include "xfs_zone_priv.h"
 #include "xfs_zones.h"
+#include "xfs_rtcsum.h"
 #include "xfs_trace.h"
 
 /*
@@ -106,6 +107,9 @@ struct xfs_gc_bio {
 	/* Realtime group currently being reclaimed */
 	struct xfs_rtgroup		*victim_rtg;
 
+	/* Buffer for data checksums */
+	struct xfs_buf			*csum_bp;
+
 	/* Bio used for reads and writes, including the bvec used by it */
 	struct bio			bio;	/* must be last */
 };
@@ -660,6 +664,20 @@ xfs_zone_gc_alloc_blocks(
 	return true;
 }
 
+static void
+xfs_zone_gc_free_chunk(
+	struct xfs_gc_bio	*chunk)
+{
+	atomic_dec(&chunk->victim_rtg->rtg_gccount);
+	xfs_rtgroup_rele(chunk->victim_rtg);
+	list_del(&chunk->entry);
+	xfs_open_zone_put(chunk->oz);
+	if (chunk->csum_bp)
+		xfs_buf_rele(chunk->csum_bp);
+	xfs_irele(chunk->ip);
+	bio_put(&chunk->bio);
+}
+
 static void
 xfs_zone_gc_add_data(
 	struct xfs_gc_bio	*chunk)
@@ -725,6 +743,11 @@ xfs_zone_gc_start_chunk(
 	if (!xfs_zone_gc_iter_irec(mp, iter, &irec, &ip))
 		return false;
 
+	if (xfs_has_rtcsum(mp)) {
+		irec.rm_blockcount = min(irec.rm_blockcount,
+			xfs_rtcsum_max_len(mp, irec.rm_startblock));
+	}
+
 	if (!xfs_zone_gc_alloc_blocks(data, &irec.rm_blockcount, &daddr,
 			&is_seq)) {
 		xfs_irele(ip);
@@ -748,6 +771,7 @@ xfs_zone_gc_start_chunk(
 	chunk->data = data;
 	chunk->oz = data->oz;
 	chunk->victim_rtg = iter->victim_rtg;
+	chunk->csum_bp = NULL;
 	atomic_inc(&rtg_group(chunk->victim_rtg)->xg_active_ref);
 	atomic_inc(&chunk->victim_rtg->rtg_gccount);
 
@@ -764,22 +788,22 @@ xfs_zone_gc_start_chunk(
 	list_add_tail(&chunk->entry, &data->reading);
 	xfs_zone_gc_iter_advance(iter, irec.rm_blockcount);
 
+	if (xfs_is_rtcsum_inode(ip)) {
+		int error;
+
+		error = xfs_rtcsum_read_async(mp, chunk->old_startblock,
+				&chunk->csum_bp);
+		if (error) {
+			xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
+			xfs_zone_gc_free_chunk(chunk);
+			return false;
+		}
+	}
+
 	submit_bio(bio);
 	return true;
 }
 
-static void
-xfs_zone_gc_free_chunk(
-	struct xfs_gc_bio	*chunk)
-{
-	atomic_dec(&chunk->victim_rtg->rtg_gccount);
-	xfs_rtgroup_rele(chunk->victim_rtg);
-	list_del(&chunk->entry);
-	xfs_open_zone_put(chunk->oz);
-	xfs_irele(chunk->ip);
-	bio_put(&chunk->bio);
-}
-
 static void
 xfs_zone_gc_submit_write(
 	struct xfs_zone_gc_data	*data,
@@ -832,6 +856,9 @@ xfs_zone_gc_split_write(
 	split_chunk->old_startblock = chunk->old_startblock;
 	split_chunk->new_daddr = chunk->new_daddr;
 	split_chunk->oz = chunk->oz;
+	split_chunk->csum_bp = chunk->csum_bp;
+	if (split_chunk->csum_bp)
+		xfs_buf_hold(split_chunk->csum_bp);
 	atomic_inc(&chunk->oz->oz_ref);
 
 	split_chunk->victim_rtg = chunk->victim_rtg;
@@ -919,6 +946,21 @@ xfs_zone_gc_finish_chunk(
 
 	if (chunk->is_seq)
 		chunk->new_daddr = chunk->bio.bi_iter.bi_sector;
+
+	if (xfs_is_rtcsum_inode(ip)) {
+		unsigned int	boff;
+
+		error = xfs_buf_read_async_wait(chunk->csum_bp);
+		if (error)
+			goto free;
+
+		boff = xfs_rtb_to_rtcsumoff(mp, chunk->old_startblock);
+		error = xfs_rtcsum_log(chunk->oz, chunk->new_daddr, chunk->len,
+				chunk->csum_bp->b_addr + boff);
+		if (error)
+			goto free;
+	}
+
 	error = xfs_zoned_end_io(ip, chunk->offset, chunk->len,
 			chunk->new_daddr, chunk->oz, chunk->old_startblock);
 free:
Signature · 1 line
2.53.0
#25 lore raw

PATCH 19/25 xfs: simplify range tracking in xfs_verify_media

In reply to Christoph Hellwig

Remove the bbcount variable tracking the remainining basic blocks in the current verification request, and instead always recalculated it from the daddr and the end, reducing the number of moving pieces.

---
 fs/xfs/xfs_verify_media.c | 42 +++++++++++----------------------------
 1 file changed, 12 insertions(+), 30 deletions(-)
fs/xfs/xfs_verify_media.c +12 -30
index 516763219d3f..382c46cdfb6a 100644
@@ -267,12 +267,11 @@ xfs_verify_media(
 	struct xfs_mount	*mp,
 	struct xfs_verify_media	*me)
 {
+	xfs_daddr_t		daddr = me->me_start_daddr;
 	struct xfs_buftarg	*btp = NULL;
 	struct folio		*folio;
 	xfs_daddr_t		dev_start = 0;
 	xfs_daddr_t		dev_end = 0;
-	xfs_daddr_t		daddr;
-	uint64_t		bbcount;
 	int			error = 0;
 
 	me->me_ioerror = 0;
@@ -305,11 +304,11 @@ xfs_verify_media(
 	 */
 	if (me->me_end_daddr > dev_end)
 		me->me_end_daddr = dev_end;
-	if (me->me_start_daddr < dev_start)
-		me->me_start_daddr = dev_start;
+	if (daddr < dev_start)
+		daddr = dev_start;
 
 	/* start and end have to be aligned to the lba size */
-	if (!IS_ALIGNED(BBTOB(me->me_start_daddr | me->me_end_daddr),
+	if (!IS_ALIGNED(BBTOB(daddr | me->me_end_daddr),
 			btp->bt_logical_sectorsize))
 		return -EINVAL;
 
@@ -317,36 +316,22 @@ xfs_verify_media(
 	 * end_daddr is the exclusive end of the range, so if start_daddr
 	 * reaches there (or beyond), there's no work to be done.
 	 */
-	if (me->me_start_daddr >= me->me_end_daddr)
+	if (daddr >= me->me_end_daddr)
 		return 0;
 
-	/*
-	 * There are three ranges involved here:
-	 *
-	 *  - [me->me_start_daddr, me->me_end_daddr) is the range that the
-	 *    user wants to verify.  end_daddr can be beyond the end of the
-	 *    disk; we'll constrain it to the end if necessary.
-	 *
-	 *  - [daddr, me->me_end_daddr) is the range that we have not yet
-	 *    verified.  We update daddr after each successful read.
-	 *    me->me_start_daddr is set to daddr before returning.
-	 *
-	 *  - [daddr, daddr + bio_bbcount) is the range that we're currently
-	 *    verifying.
-	 */
-	daddr = me->me_start_daddr;
-	bbcount = me->me_end_daddr - me->me_start_daddr;
-
-	folio = xfs_verify_alloc_folio(xfs_verify_iosize(me, btp, bbcount));
+	folio = xfs_verify_alloc_folio(xfs_verify_iosize(me, btp,
+			me->me_end_daddr - daddr));
 	if (!folio)
 		return -ENOMEM;
 
-	trace_xfs_verify_media(mp, me, btp->bt_dev, daddr, bbcount, folio);
+	trace_xfs_verify_media(mp, me, btp->bt_dev, daddr,
+			me->me_end_daddr - daddr, folio);
 
 	for (;;) {
 		unsigned int	bio_bbcount;
 
-		bio_bbcount = min(bbcount, folio_size(folio) >> SECTOR_SHIFT);
+		bio_bbcount = min(me->me_end_daddr - daddr,
+				folio_size(folio) >> SECTOR_SHIFT);
 		error = bdev_rw_virt(btp->bt_bdev, daddr, folio_address(folio),
 				bio_bbcount << SECTOR_SHIFT,
 				REQ_OP_READ);
@@ -356,11 +341,8 @@ xfs_verify_media(
 			error = 0;
 			break;
 		}
-
 		daddr += bio_bbcount;
-		bbcount -= bio_bbcount;
-
-		if (bbcount == 0)
+		if (daddr >= me->me_end_daddr)
 			break;
 
 		if (me->me_rest_us) {
Signature · 1 line
2.53.0
#26 lore raw

PATCH 20/25 xfs: verify data checksums during media verification

In reply to Christoph Hellwig

Wire up reading and verifying data checksums during media verification. This is very similar to the file read path in that it kicks of an async read for the checksum buffer before reading the data, and then validating once both are read in.

To support this, split the per-read logic in xfs_verify_media into a separate helpers for the data checksums vs no checksum cases.

---
 fs/xfs/xfs_verify_media.c | 116 ++++++++++++++++++++++++++++++++++----
 1 file changed, 105 insertions(+), 11 deletions(-)
fs/xfs/xfs_verify_media.c +105 -11
index 382c46cdfb6a..bafaac2f3ea8 100644
@@ -22,6 +22,7 @@
 #include "xfs_rtrmap_btree.h"
 #include "xfs_health.h"
 #include "xfs_healthmon.h"
+#include "xfs_rtcsum.h"
 #include "xfs_trace.h"
 #include "xfs_verify_media.h"
 
@@ -261,6 +262,99 @@ xfs_verify_media_error(
 	}
 }
 
+static int
+xfs_submit_verify_bio(
+	struct xfs_mount	*mp,
+	struct xfs_verify_media	*me,
+	struct xfs_buftarg	*btp,
+	struct folio		*folio,
+	xfs_daddr_t		*daddr)
+{
+	unsigned int		bbcount;
+	int			error;
+
+	bbcount = min(me->me_end_daddr - *daddr,
+			folio_size(folio) >> SECTOR_SHIFT);
+	error = bdev_rw_virt(btp->bt_bdev, *daddr, folio_address(folio),
+			bbcount << SECTOR_SHIFT, REQ_OP_READ);
+	if (error) {
+		xfs_verify_media_error(mp, me, btp, *daddr, bbcount, error);
+		return 1;
+	}
+
+	*daddr += bbcount;
+	return 0;
+}
+
+/*
+ * Data checksums are per-FSB, so we can only validate FSB-aligned regions.
+ *
+ * When the alignment leads to an empty range, give up.
+ */
+static int
+xfs_submit_verify_bio_csum(
+	struct xfs_mount	*mp,
+	struct xfs_verify_media	*me,
+	struct xfs_buftarg	*btp,
+	struct folio		*folio,
+	xfs_daddr_t		*daddr)
+{
+	xfs_fsblock_t		bno = xfs_daddr_to_rtb(mp, *daddr);
+	xfs_fsblock_t		end = xfs_daddr_to_rtb(mp, me->me_end_daddr);
+	struct xfs_buf		*csum_bp = NULL;
+	struct bvec_iter	saved_iter;
+	unsigned int		bbcount;
+	xfs_filblks_t		len;
+	struct bio		bio;
+	struct bio_vec		bv;
+	int			error;
+
+	len = min3(end - bno, xfs_rtcsum_max_len(mp, bno),
+			XFS_B_TO_FSBT(mp, folio_size(folio)));
+	if (!len) {
+		*daddr = me->me_end_daddr;
+		return 0;
+	}
+
+	error = xfs_rtcsum_read_async(mp, bno, &csum_bp);
+	if (error)
+		return error;
+
+	*daddr = xfs_rtb_to_daddr(mp, bno);
+	bbcount = XFS_FSB_TO_BB(mp, len);
+
+	bio_init(&bio, btp->bt_bdev, &bv, 1, REQ_OP_READ);
+	bio.bi_iter.bi_sector = *daddr;
+	bio_add_folio_nofail(&bio, folio, bbcount << SECTOR_SHIFT, 0);
+
+	saved_iter = bio.bi_iter;
+	error = submit_bio_wait(&bio);
+	if (error) {
+		xfs_verify_media_error(mp, me, btp, *daddr, bbcount, error);
+		error = 1;
+		goto out_buf_rele;
+	}
+
+	error = xfs_buf_read_async_wait(csum_bp);
+	if (error)
+		goto out_buf_rele;
+
+	error = xfs_csum_verify(mp, &bio, &saved_iter,
+			csum_bp->b_addr + xfs_rtb_to_rtcsumoff(mp, bno), bno,
+			false);
+	if (error) {
+		xfs_verify_media_error(mp, me, btp, *daddr, bbcount, error);
+		error = 1;
+		goto out_buf_rele;
+	}
+
+	*daddr = xfs_rtb_to_daddr(mp, bno + len);
+out_buf_rele:
+	xfs_buf_rele(csum_bp);
+	bio_uninit(&bio);
+	return error;
+}
+
 /* Verify the media of an xfs device by submitting read requests to the disk. */
 static int
 xfs_verify_media(
@@ -328,20 +422,20 @@ xfs_verify_media(
 			me->me_end_daddr - daddr, folio);
 
 	for (;;) {
-		unsigned int	bio_bbcount;
-
-		bio_bbcount = min(me->me_end_daddr - daddr,
-				folio_size(folio) >> SECTOR_SHIFT);
-		error = bdev_rw_virt(btp->bt_bdev, daddr, folio_address(folio),
-				bio_bbcount << SECTOR_SHIFT,
-				REQ_OP_READ);
+		if (IS_ENABLED(CONFIG_XFS_RT) &&
+		    me->me_dev == XFS_DEV_RT &&
+		    xfs_has_rtcsum(mp)) {
+			error = xfs_submit_verify_bio_csum(mp, me, btp, folio,
+					&daddr);
+		} else {
+			error = xfs_submit_verify_bio(mp, me, btp, folio,
+					&daddr);
+		}
 		if (error) {
-			xfs_verify_media_error(mp, me, btp, daddr, bio_bbcount,
-					error);
-			error = 0;
+			if (error == 1)
+				error = 0;
 			break;
 		}
-		daddr += bio_bbcount;
 		if (daddr >= me->me_end_daddr)
 			break;
 
Signature · 1 line
2.53.0
#27 lore raw

Re: [PATCH 20/25] xfs: verify data checksums during media verification

Quoted 8 lines · Christoph Hellwig

Wire up reading and verifying data checksums during media verification. This is very similar to the file read path in that it kicks of an async read for the checksum buffer before reading the data, and then validating once both are read in.

To support this, split the per-read logic in xfs_verify_media into a separate helpers for the data checksums vs no checksum cases.

fs/xfs/xfs_verify_media.c xfs_verify_media_error( · line 293

Quoted 6 lines
fs/xfs/xfs_verify_media.c +6 -0
+
+/*
+ * Data checksums are per-FSB, so we can only validate FSB-aligned regions.
+ *
+ * When the alignment leads to an empty range, give up.
+ */

A comment here would be helpful about the return values. 0 for success,
1 for media errors
errno for other errors.

Quoted 10 lines
fs/xfs/xfs_verify_media.c +10 -0
+static int
+xfs_submit_verify_bio_csum(
+	struct xfs_mount	*mp,
+	struct xfs_verify_media	*me,
+	struct xfs_buftarg	*btp,
+	struct folio		*folio,
+	xfs_daddr_t		*daddr)
+{
+	xfs_fsblock_t		bno = xfs_daddr_to_rtb(mp, *daddr);
+	xfs_fsblock_t		end = xfs_daddr_to_rtb(mp, me->me_end_daddr);

fs/xfs/xfs_verify_media.c xfs_verify_media_error( · line 313

Quoted 10 lines
fs/xfs/xfs_verify_media.c +10 -0
+	struct xfs_buf		*csum_bp = NULL;
+	struct bvec_iter	saved_iter;
+	unsigned int		bbcount;
+	xfs_filblks_t		len;
+	struct bio		bio;
+	struct bio_vec		bv;
+	int			error;
+
+	len = min3(end - bno, xfs_rtcsum_max_len(mp, bno),
+			XFS_B_TO_FSBT(mp, folio_size(folio)));

For large block size systems, if we allocate a single page in the folio due to fragmentation, len will always be zero (4096 >> (blocklog > 12)).

fs/xfs/xfs_verify_media.c xfs_verify_media_error( · line 317

Quoted 4 lines
fs/xfs/xfs_verify_media.c +4 -0
+	if (!len) {
+		*daddr = me->me_end_daddr;
+		return 0;
+	}

And we skip those ranges without verifying anything with a success.

I think we should raise the iosize to a single FSB in xfs_verify_alloc_folio and should return ENOMEM if we cannot allocate one?

Quoted 40 lines
fs/xfs/xfs_verify_media.c +40 -0
+
+	error = xfs_rtcsum_read_async(mp, bno, &csum_bp);
+	if (error)
+		return error;
+
+	*daddr = xfs_rtb_to_daddr(mp, bno);
+	bbcount = XFS_FSB_TO_BB(mp, len);
+
+	bio_init(&bio, btp->bt_bdev, &bv, 1, REQ_OP_READ);
+	bio.bi_iter.bi_sector = *daddr;
+	bio_add_folio_nofail(&bio, folio, bbcount << SECTOR_SHIFT, 0);
+
+	saved_iter = bio.bi_iter;
+	error = submit_bio_wait(&bio);
+	if (error) {
+		xfs_verify_media_error(mp, me, btp, *daddr, bbcount, error);
+		error = 1;
+		goto out_buf_rele;
+	}
+
+	error = xfs_buf_read_async_wait(csum_bp);
+	if (error)
+		goto out_buf_rele;
+
+	error = xfs_csum_verify(mp, &bio, &saved_iter,
+			csum_bp->b_addr + xfs_rtb_to_rtcsumoff(mp, bno), bno,
+			false);
+	if (error) {
+		xfs_verify_media_error(mp, me, btp, *daddr, bbcount, error);
+		error = 1;
+		goto out_buf_rele;
+	}
+
+	*daddr = xfs_rtb_to_daddr(mp, bno + len);
+out_buf_rele:
+	xfs_buf_rele(csum_bp);
+	bio_uninit(&bio);
+	return error;
+}
+
Signature · 1 line
Pankaj
#28 lore raw

Re: [PATCH 20/25] xfs: verify data checksums during media verification

Quoted 10 lines · Pankaj Raghav (Samsung)
Quoted 5 lines

+/*
+ * Data checksums are per-FSB, so we can only validate FSB-aligned regions.
+ *
+ * When the alignment leads to an empty range, give up.
+ */

A comment here would be helpful about the return values. 0 for success,
1 for media errors
errno for other errors.

Ok, I'll add it.

Quoted 19 lines

fs/xfs/xfs_verify_media.c xfs_verify_media_error( · line 313

Quoted 8 lines
fs/xfs/xfs_verify_media.c +8 -0
+	unsigned int		bbcount;
+	xfs_filblks_t		len;
+	struct bio		bio;
+	struct bio_vec		bv;
+	int			error;
+
+	len = min3(end - bno, xfs_rtcsum_max_len(mp, bno),
+			XFS_B_TO_FSBT(mp, folio_size(folio)));

For large block size systems, if we allocate a single page in the folio due to fragmentation, len will always be zero (4096 >> (blocklog > 12)).

fs/xfs/xfs_verify_media.c xfs_verify_media_error( · line 317

Quoted 4 lines
fs/xfs/xfs_verify_media.c +4 -0
+	if (!len) {
+		*daddr = me->me_end_daddr;
+		return 0;
+	}

And we skip those ranges without verifying anything with a success.

Quoted 3 lines

I think we should raise the iosize to a single FSB in xfs_verify_alloc_folio and should return ENOMEM if we cannot allocate one?

Yes, I'll do that.

#29 lore raw

PATCH 21/25 xfs: don't try to verify checksums on empty zones

In reply to Christoph Hellwig

xfs_scrub can sometimes send XFS_IOC_VERIFY_MEDIA ioctls for ranges that have never been written since the last zone reset, which will lead to checksum verification failures.

Protect against this by checking that the range is valid. To make the range-check non-racy hold the IOLOCK of the rmap inode over the entire media verification and cycle through it before resetting a zone, as that is the only way to rewrite data in a zone.

---
 fs/xfs/xfs_verify_media.c | 46 ++++++++++++++++++++++++++++++++++++---
 fs/xfs/xfs_zone_gc.c      |  7 ++++++
 2 files changed, 50 insertions(+), 3 deletions(-)
fs/xfs/xfs_verify_media.c +43 -3
index bafaac2f3ea8..d94bee0b9b6b 100644
@@ -25,6 +25,7 @@
 #include "xfs_rtcsum.h"
 #include "xfs_trace.h"
 #include "xfs_verify_media.h"
+#include "xfs_zone_alloc.h"
 
 #include <linux/fserror.h>
 
@@ -291,6 +292,46 @@ xfs_submit_verify_bio(
  *
  * When the alignment leads to an empty range, give up.
  */
+static int
+xfs_csum_verify_metafile(
+	struct xfs_mount	*mp,
+	struct bio		*bio,
+	struct bvec_iter	*saved_iter,
+	void			*csum_buf,
+	xfs_fsblock_t		bno)
+{
+	struct xfs_rtgroup	*rtg;
+	bool			is_valid;
+	int			error = 0;
+
+	rtg = xfs_rtgroup_get(mp, xfs_rtb_to_rgno(mp, bno));
+	if (!rtg)
+		return -EFSCORRUPTED;
+
+	/*
+	 * Only validate the checksums for valid data, as data never written
+	 * will not have valid checksums.  We need to hold the iolock on the
+	 * rmap inode over the actual verification to prevent freeing of blocks
+	 * and thus a zone reset to happen underneath us.
+	 *
+	 * Note that this still relies on cooperating userspace, as there also
+	 * can be blocks that were written but never recorded after an unclean
+	 * shutdown, which this check does not catch.  It purely tries to deal
+	 * with races vs the previous FSMAP output used by xfs_scrub.
+	 */
+	xfs_ilock(rtg_rmap(rtg), XFS_IOLOCK_SHARED);
+	xfs_ilock(rtg_rmap(rtg), XFS_ILOCK_SHARED);
+	is_valid = xfs_zone_rgbno_is_valid(rtg, xfs_rtb_to_rgbno(mp, bno));
+	xfs_iunlock(rtg_rmap(rtg), XFS_ILOCK_SHARED);
+
+	if (is_valid)
+		error = xfs_csum_verify(mp, bio, saved_iter, csum_buf, bno,
+				false);
+	xfs_iunlock(rtg_rmap(rtg), XFS_IOLOCK_SHARED);
+	xfs_rtgroup_put(rtg);
+	return error;
+}
+
 static int
 xfs_submit_verify_bio_csum(
 	struct xfs_mount	*mp,
@@ -339,9 +380,8 @@ xfs_submit_verify_bio_csum(
 	if (error)
 		goto out_buf_rele;
 
-	error = xfs_csum_verify(mp, &bio, &saved_iter,
-			csum_bp->b_addr + xfs_rtb_to_rtcsumoff(mp, bno), bno,
-			false);
+	error = xfs_csum_verify_metafile(mp, &bio, &saved_iter,
+			csum_bp->b_addr + xfs_rtb_to_rtcsumoff(mp, bno), bno);
 	if (error) {
 		xfs_verify_media_error(mp, me, btp, *daddr, bbcount, error);
 		error = 1;
fs/xfs/xfs_zone_gc.c +7 -0
index 9296f584282a..7e04693fd573 100644
@@ -1001,6 +1001,13 @@ xfs_submit_zone_reset_bio(
 
 	trace_xfs_zone_reset(rtg);
 
+	/*
+	 * Cycle through the rmap inode iolock to synchronize against
+	 * xfs_csum_verify_metafile.
+	 */
+	xfs_ilock(rtg_rmap(rtg), XFS_IOLOCK_SHARED);
+	xfs_iunlock(rtg_rmap(rtg), XFS_IOLOCK_SHARED);
+
 	ASSERT(rtg_rmap(rtg)->i_used_blocks == 0);
 
 	if (XFS_TEST_ERROR(mp, XFS_ERRTAG_ZONE_RESET)) {
Signature · 1 line
2.53.0
#30 lore raw

PATCH 22/25 xfs: report RT data checksum information via XFS_FSOP_GEOM

In reply to Christoph Hellwig

Report the RT data checksum flag and the checksum algorithm to userspace.

---
 fs/xfs/libxfs/xfs_fs.h | 6 +++++-
 fs/xfs/libxfs/xfs_sb.c | 5 +++++
 2 files changed, 10 insertions(+), 1 deletion(-)
fs/xfs/libxfs/xfs_fs.h +5 -1
index 226cb96dfae8..ef644cfe26d6 100644
@@ -191,7 +191,10 @@ struct xfs_fsop_geom {
 	__u32		rgcount;	/* number of realtime groups	*/
 	__u64		rtstart;	/* start of internal rt section */
 	__u64		rtreserved;	/* RT (zoned) reserved blocks	*/
-	__u64		reserved[14];	/* reserved space		*/
+	__u8		rtcsum_type;	/* RT data checksum type	*/
+	__u8		rtcsum_blklog;	/* log2 of rtcsum bsize		*/
+	__u8		reserved_pad[6];/* reserved space		*/
+	__u64		reserved[13];	/* reserved space		*/
 };
 
 #define XFS_FSOP_GEOM_SICK_COUNTERS	(1 << 0)  /* summary counters */
@@ -250,6 +253,7 @@ typedef struct xfs_fsop_resblks {
 #define XFS_FSOP_GEOM_FLAGS_PARENT	(1 << 25) /* linux parent pointers */
 #define XFS_FSOP_GEOM_FLAGS_METADIR	(1 << 26) /* metadata directories */
 #define XFS_FSOP_GEOM_FLAGS_ZONED	(1 << 27) /* zoned rt device */
+#define XFS_FSOP_GEOM_FLAGS_DATA_CSUM	(1 << 28) /* data checksums */
 
 /*
  * Minimum and maximum sizes need for growth checks.
fs/xfs/libxfs/xfs_sb.c +5 -0
index 8096706ed78a..a1aebf398423 100644
@@ -1630,6 +1630,11 @@ xfs_fs_geometry(
 		geo->flags |= XFS_FSOP_GEOM_FLAGS_METADIR;
 	if (xfs_has_zoned(mp))
 		geo->flags |= XFS_FSOP_GEOM_FLAGS_ZONED;
+	if (xfs_has_rtcsum(mp)) {
+		geo->flags |= XFS_FSOP_GEOM_FLAGS_DATA_CSUM;
+		geo->rtcsum_type = mp->m_sb.sb_rtcsum_type;
+		geo->rtcsum_blklog = mp->m_sb.sb_rtcsum_blklog;
+	}
 	geo->rtsectsize = sbp->sb_blocksize;
 	geo->dirblocksize = xfs_dir2_dirblock_bytes(sbp);
 
Signature · 1 line
2.53.0
#31 lore raw

PATCH 23/25 xfs: add an experimental feature warning for RT data checksums

In reply to Christoph Hellwig
---
 fs/xfs/xfs_message.c | 4 ++++
 fs/xfs/xfs_message.h | 1 +
 fs/xfs/xfs_mount.h   | 2 ++
 fs/xfs/xfs_rtcsum.c  | 1 +
 4 files changed, 8 insertions(+)
fs/xfs/xfs_message.c +4 -0
index 0243e509a468..2924e5c2b8fc 100644
@@ -149,6 +149,10 @@ xfs_warn_experimental(
 			.opstate	= XFS_OPSTATE_WARNED_LARP,
 			.name		= "logged extended attributes",
 		},
+		[XFS_EXPERIMENTAL_CSUM] = {
+			.opstate	= XFS_OPSTATE_WARNED_CSUM,
+			.name		= "data checksums",
+		},
 	};
 	ASSERT(feat >= 0 && feat < XFS_EXPERIMENTAL_MAX);
 	BUILD_BUG_ON(ARRAY_SIZE(features) != XFS_EXPERIMENTAL_MAX);
fs/xfs/xfs_message.h +1 -0
index bfae8dddc9a6..23b178ea4976 100644
@@ -94,6 +94,7 @@ void xfs_buf_alert_ratelimited(struct xfs_buf *bp, const char *rlmsg,
 enum xfs_experimental_feat {
 	XFS_EXPERIMENTAL_SHRINK,
 	XFS_EXPERIMENTAL_LARP,
+	XFS_EXPERIMENTAL_CSUM,
 
 	XFS_EXPERIMENTAL_MAX,
 };
fs/xfs/xfs_mount.h +2 -0
index 0e60d5c29c9d..70f7563cde61 100644
@@ -583,6 +583,8 @@ __XFS_HAS_FEAT(nouuid, NOUUID)
  */
 #define XFS_OPSTATE_BLOCKGC_ENABLED	6
 
+/* Kernel has logged a warning about checksums */
+#define XFS_OPSTATE_WARNED_CSUM		8
 /* Kernel has logged a warning about shrink being used on this fs. */
 #define XFS_OPSTATE_WARNED_SHRINK	9
 /* Kernel has logged a warning about logged xattr updates being used. */
fs/xfs/xfs_rtcsum.c +1 -0
index 64fbf6886f8f..93e9792207c6 100644
@@ -308,6 +308,7 @@ xfs_rtcsum_mount(
 	}
 	mp->m_features |= XFS_FEAT_DAX_NEVER;
 
+	xfs_warn_experimental(mp, XFS_EXPERIMENTAL_CSUM);
 	xfs_info(mp, "using %s for RT device data checksums",
 		xfs_data_csum_names[mp->m_sb.sb_rtcsum_type]);
 	return 0;
Signature · 1 line
2.53.0
#32 lore raw

PATCH 24/25 xfs: enable RT data checksums

In reply to Christoph Hellwig

All mandatory pieces are in place now, allow mounting.

---
 fs/xfs/libxfs/xfs_format.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/xfs/libxfs/xfs_format.h +2 -1
index 3d794a2eb497..4ed938d710ac 100644
@@ -417,7 +417,8 @@ xfs_sb_has_ro_compat_feature(
 		 XFS_SB_FEAT_INCOMPAT_PARENT | \
 		 XFS_SB_FEAT_INCOMPAT_METADIR | \
 		 XFS_SB_FEAT_INCOMPAT_ZONED | \
-		 XFS_SB_FEAT_INCOMPAT_ZONE_GAPS)
+		 XFS_SB_FEAT_INCOMPAT_ZONE_GAPS | \
+		 XFS_SB_FEAT_INCOMPAT_RTCSUM)
 
 #define XFS_SB_FEAT_INCOMPAT_UNKNOWN	~XFS_SB_FEAT_INCOMPAT_ALL
 static inline bool
Signature · 1 line
2.53.0
#33 lore raw

PATCH 25/25 xfs: add a RT checksum log item

In reply to Christoph Hellwig

Use a one-shot log item to log RT checksum log items instead of the buffer log item, and just update the buffer as an ordered buffer.

This reduces the amount of reallocs of the shadow buffers compared to the buffer item, with the downside of needing more code and creating more overhead for lots of small non-data integrity writes.

Still needs full benchmarking.

---
 fs/xfs/Makefile                 |   1 +
 fs/xfs/libxfs/xfs_log_format.h  |  12 +-
 fs/xfs/libxfs/xfs_log_recover.h |   1 +
 fs/xfs/libxfs/xfs_ondisk.h      |   1 +
 fs/xfs/libxfs/xfs_trans_resv.c  |   9 +-
 fs/xfs/xfs_log.h                |   3 +-
 fs/xfs/xfs_log_recover.c        |   3 +
 fs/xfs/xfs_rtcsum.c             |   4 +-
 fs/xfs/xfs_rtcsum.h             |   7 +
 fs/xfs/xfs_rtcsum_item.c        | 218 ++++++++++++++++++++++++++++++++
 fs/xfs/xfs_super.c              |   7 +
 11 files changed, 259 insertions(+), 7 deletions(-)
 create mode 100644 fs/xfs/xfs_rtcsum_item.c
fs/xfs/Makefile +1 -0
index 0d57bf0701ec..1891fc3edf39 100644
@@ -143,6 +143,7 @@ xfs-$(CONFIG_XFS_QUOTA)		+= xfs_dquot.o \
 # xfs_rtbitmap is shared with libxfs
 xfs-$(CONFIG_XFS_RT)		+= xfs_rtalloc.o \
 				   xfs_rtcsum.o \
+				   xfs_rtcsum_item.o \
 				   xfs_zone_alloc.o \
 				   xfs_zone_gc.o \
 				   xfs_zone_info.o \
fs/xfs/libxfs/xfs_log_format.h +11 -1
index c6f0edb4bf0a..ae6476a01b11 100644
@@ -249,6 +249,7 @@ struct xfs_trans_header {
 #define	XFS_LI_RUD_RT		0x124d	/* realtime rmap update done */
 #define	XFS_LI_CUI_RT		0x124e	/* realtime refcount update intent */
 #define	XFS_LI_CUD_RT		0x124f	/* realtime refcount update done */
+#define	XFS_LI_RTCSUM		0x1250	/* realtime checksum update */
 
 #define XFS_LI_TYPE_DESC \
 	{ XFS_LI_EFI,		"XFS_LI_EFI" }, \
@@ -274,7 +275,8 @@ struct xfs_trans_header {
 	{ XFS_LI_RUI_RT,	"XFS_LI_RUI_RT" }, \
 	{ XFS_LI_RUD_RT,	"XFS_LI_RUD_RT" }, \
 	{ XFS_LI_CUI_RT,	"XFS_LI_CUI_RT" }, \
-	{ XFS_LI_CUD_RT,	"XFS_LI_CUD_RT" }
+	{ XFS_LI_CUD_RT,	"XFS_LI_CUD_RT" }, \
+	{ XFS_LI_RTCSUM,	"XFS_LI_RTCSUM" }
 
 /*
  * Inode Log Item Format definitions.
@@ -1082,4 +1084,12 @@ struct xfs_attrd_log_format {
 	uint64_t	alfd_alf_id;	/* id of corresponding attri */
 };
 
+#define XFS_RTCSUM_NR_VECS 1
+struct xfs_rtcsum_log_format {
+	uint16_t	rlf_type;	/* type of log format structure */
+	uint16_t	rlf_size;	/* size of log format structure */
+	uint32_t	rlf_buf_offset;	/* offset into the rtcsum buf */
+	uint64_t	rlf_buf_daddr;	/* daddr of the rtcsum buf */
+};
+
 #endif /* __XFS_LOG_FORMAT_H__ */
fs/xfs/libxfs/xfs_log_recover.h +1 -0
index 9e712e62369c..67ec495f2ff3 100644
@@ -83,6 +83,7 @@ extern const struct xlog_recover_item_ops xlog_rtrui_item_ops;
 extern const struct xlog_recover_item_ops xlog_rtrud_item_ops;
 extern const struct xlog_recover_item_ops xlog_rtcui_item_ops;
 extern const struct xlog_recover_item_ops xlog_rtcud_item_ops;
+extern const struct xlog_recover_item_ops xlog_rtcsum_item_ops;
 
 /*
  * Macros, structures, prototypes for internal log manager use.
fs/xfs/libxfs/xfs_ondisk.h +1 -0
index 17ab9366b3b9..cbfdd865a00b 100644
@@ -163,6 +163,7 @@ xfs_check_ondisk_structs(void)
 	XFS_CHECK_STRUCT_SIZE(struct xfs_cud_log_format,	16);
 	XFS_CHECK_STRUCT_SIZE(struct xfs_rui_log_format,	16);
 	XFS_CHECK_STRUCT_SIZE(struct xfs_rud_log_format,	16);
+	XFS_CHECK_STRUCT_SIZE(struct xfs_rtcsum_log_format,	16);
 	XFS_CHECK_STRUCT_SIZE(struct xfs_map_extent,		32);
 	XFS_CHECK_STRUCT_SIZE(struct xfs_phys_extent,		16);
 	XFS_CHECK_STRUCT_SIZE(struct xlog_rec_header,		512);
fs/xfs/libxfs/xfs_trans_resv.c +6 -3
index 25f7581ea90c..f9c811db0761 100644
@@ -1248,10 +1248,13 @@ xfs_calc_csum_reservation(
 	unsigned int		csum_len)
 {
 	unsigned int		payload_size = xfs_rtcsum_payload_size(mp);
+	unsigned int		nbufs =
+		howmany(csum_len + payload_size, payload_size);
 
-	return xfs_calc_buf_res(
-			howmany(csum_len + payload_size, payload_size),
-			mp->m_rtcsum_bsize);
+	return nbufs * (sizeof(struct xlog_op_header) +
+			sizeof(struct xfs_rtcsum_log_format) +
+			sizeof(struct xlog_op_header) +
+			payload_size);
 }
 
 /*
fs/xfs/xfs_log.h +2 -1
index f715695e8fcb..ff6df9d51046 100644
@@ -44,7 +44,8 @@ struct xfs_cil_ctx;
 #define XLOG_REG_TYPE_XMD_FORMAT	32
 #define XLOG_REG_TYPE_ATTR_NEWNAME	33
 #define XLOG_REG_TYPE_ATTR_NEWVALUE	34
-#define XLOG_REG_TYPE_MAX		34
+#define XLOG_REG_TYPE_RTCSUM_FORMAT	35
+#define XLOG_REG_TYPE_MAX		36
 
 #define XFS_LOG_VEC_ORDERED	(-1)
 
fs/xfs/xfs_log_recover.c +3 -0
index 457617979668..d812ee5e6752 100644
@@ -1825,6 +1825,9 @@ static const struct xlog_recover_item_ops *xlog_recover_item_ops[] = {
 	&xlog_rtrud_item_ops,
 	&xlog_rtcui_item_ops,
 	&xlog_rtcud_item_ops,
+#ifdef CONFIG_XFS_RT
+	&xlog_rtcsum_item_ops,
+#endif
 };
 
 static const struct xlog_recover_item_ops *
fs/xfs/xfs_rtcsum.c +2 -2
index 93e9792207c6..c6daa32c71c7 100644
@@ -237,8 +237,8 @@ xfs_rtcsum_log_one(
 		xfs_rtcsum_len_to_extlen(mp, mp->m_rtcsum_bsize - boff));
 	len = xfs_extlen_to_rtcsum_len(mp, *nr);
 	memcpy(bp->b_addr + boff, csum, len);
-
-	xfs_trans_log_buf(tp, bp, boff, boff + len - 1);
+	xfs_trans_ordered_buf(tp, bp);
+	xfs_rtcsum_item_log(tp, xfs_buf_daddr(bp), boff, *nr, csum);
 	return 0;
 }
 
fs/xfs/xfs_rtcsum.h +7 -0
index bd16f5a99451..6cab812c9d9b 100644
@@ -15,9 +15,16 @@ int xfs_rtcsum_read_async(struct xfs_mount *mp, xfs_rtblock_t fsbno,
 int xfs_rtcsum_log(struct xfs_open_zone *oz, xfs_daddr_t daddr,
 		unsigned int data_len, const void *csum);
 
+void xfs_rtcsum_item_log(struct xfs_trans *tp, xfs_daddr_t buf_daddr,
+		unsigned int buf_offset, xfs_extlen_t count,
+		const void *csum_buf);
+
 int xfs_rtcsum_open_zone(struct xfs_open_zone *oz);
 void xfs_rtcsum_free_zone(struct xfs_open_zone *oz);
 
 int xfs_rtcsum_mount(struct xfs_mount *mp);
 
+int __init xfs_rtcsum_init(void);
+void xfs_rtcsum_exit(void);
+
 #endif /* _XFS_RTCSUM_H */
fs/xfs/xfs_rtcsum_item.c new file +218 -0

218 lines not on this page · raw message

fs/xfs/xfs_super.c +7 -0
index fc99167938e5..960f1e9693f6 100644
@@ -2530,8 +2530,13 @@ xfs_init_caches(void)
 	if (!xfs_parent_args_cache)
 		goto out_destroy_xmi_cache;
 
+	if (IS_ENABLED(CONFIG_XFS_RT) && xfs_rtcsum_init() < 0)
+		goto out_destroy_parent_args_cache;
+
 	return 0;
 
+ out_destroy_parent_args_cache:
+	kmem_cache_destroy(xfs_parent_args_cache);
  out_destroy_xmi_cache:
 	kmem_cache_destroy(xfs_xmi_cache);
  out_destroy_xmd_cache:
@@ -2594,6 +2599,8 @@ xfs_destroy_caches(void)
 	 * destroy caches.
 	 */
 	rcu_barrier();
+	if (IS_ENABLED(CONFIG_XFS_RT))
+		xfs_rtcsum_exit();
 	kmem_cache_destroy(xfs_parent_args_cache);
 	kmem_cache_destroy(xfs_xmd_cache);
 	kmem_cache_destroy(xfs_xmi_cache);
Signature · 1 line
2.53.0