]> git.proxmox.com Git - mirror_zfs.git/commitdiff
ZIL: Call brt_pending_add() replaying TX_CLONE_RANGE
authorAlexander Motin <mav@FreeBSD.org>
Wed, 29 Nov 2023 18:51:34 +0000 (13:51 -0500)
committerTony Hutter <hutter2@llnl.gov>
Wed, 29 Nov 2023 21:08:25 +0000 (13:08 -0800)
zil_claim_clone_range() takes references on cloned blocks before ZIL
replay.  Later zil_free_clone_range() drops them after replay or on
dataset destroy.  The total balance is neutral.  It means on actual
replay we must take additional references, which would stay in BRT.

Without this blocks could be freed prematurely when either original
file or its clone are destroyed.  I've observed BRT being emptied
and the feature being deactivated after ZIL replay completion, which
should not have happened.  With the patch I see expected stats.

Reviewed-by: Kay Pedersen <mail@mkwg.de>
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Reviewed-by: Rob Norris <robn@despairlabs.com>
Signed-off-by: Alexander Motin <mav@FreeBSD.org>
Sponsored by: iXsystems, Inc.
Closes #15603

include/sys/dmu.h
module/zfs/brt.c
module/zfs/dmu.c
module/zfs/zfs_vnops.c

index 615ba8fe7496d6bf062c5530879d4fc3ef98bec8..06b4dc27dfea30bf82b783e83ba733753fb4640b 100644 (file)
@@ -1072,8 +1072,7 @@ int dmu_offset_next(objset_t *os, uint64_t object, boolean_t hole,
 int dmu_read_l0_bps(objset_t *os, uint64_t object, uint64_t offset,
     uint64_t length, struct blkptr *bps, size_t *nbpsp);
 int dmu_brt_clone(objset_t *os, uint64_t object, uint64_t offset,
-    uint64_t length, dmu_tx_t *tx, const struct blkptr *bps, size_t nbps,
-    boolean_t replay);
+    uint64_t length, dmu_tx_t *tx, const struct blkptr *bps, size_t nbps);
 
 /*
  * Initial setup and final teardown.
index b0529521ec7633b0edd2c54d7e67e43fbe12afbf..759bc8d2e2b85a2428e7d32fb86fa9deefdbc90f 100644 (file)
  * destination dataset is mounted and its ZIL replayed.
  * To address this situation we leverage zil_claim() mechanism where ZFS will
  * parse all the ZILs on pool import. When we come across TX_CLONE_RANGE
- * entries, we will bump reference counters for their BPs in the BRT and then
- * on mount and ZIL replay we will just attach BPs to the file without
- * bumping reference counters.
- * Note it is still possible that after zil_claim() we never mount the
- * destination, so we never replay its ZIL and we destroy it. This way we would
- * end up with leaked references in BRT. We address that too as ZFS gives us
- * a chance to clean this up on dataset destroy (see zil_free_clone_range()).
+ * entries, we will bump reference counters for their BPs in the BRT.  Then
+ * on mount and ZIL replay we bump the reference counters once more, while the
+ * first references are dropped during ZIL destroy by zil_free_clone_range().
+ * It is possible that after zil_claim() we never mount the destination, so
+ * we never replay its ZIL and just destroy it.  In this case the only taken
+ * references will be dropped by zil_free_clone_range(), since the cloning is
+ * not going to ever take place.
  */
 
 static kmem_cache_t *brt_entry_cache;
index ddb29020b09b434bc6b3f3a78fd0ea00fab82850..3f626031de52be4caaa21d605033ec200e723880 100644 (file)
@@ -2267,7 +2267,7 @@ out:
 
 int
 dmu_brt_clone(objset_t *os, uint64_t object, uint64_t offset, uint64_t length,
-    dmu_tx_t *tx, const blkptr_t *bps, size_t nbps, boolean_t replay)
+    dmu_tx_t *tx, const blkptr_t *bps, size_t nbps)
 {
        spa_t *spa;
        dmu_buf_t **dbp, *dbuf;
@@ -2341,10 +2341,8 @@ dmu_brt_clone(objset_t *os, uint64_t object, uint64_t offset, uint64_t length,
                 * When data in embedded into BP there is no need to create
                 * BRT entry as there is no data block. Just copy the BP as
                 * it contains the data.
-                * Also, when replaying ZIL we don't want to bump references
-                * in the BRT as it was already done during ZIL claim.
                 */
-               if (!replay && !BP_IS_HOLE(bp) && !BP_IS_EMBEDDED(bp)) {
+               if (!BP_IS_HOLE(bp) && !BP_IS_EMBEDDED(bp)) {
                        brt_pending_add(spa, bp, tx);
                }
        }
index 84e6b10ef37c3c1a8a6ebcd1e6d0f8eb76574ad3..3a5fa75df2ea21538498c81e9f791f4711508ea8 100644 (file)
@@ -1333,7 +1333,7 @@ zfs_clone_range(znode_t *inzp, uint64_t *inoffp, znode_t *outzp,
                }
 
                error = dmu_brt_clone(outos, outzp->z_id, outoff, size, tx,
-                   bps, nbps, B_FALSE);
+                   bps, nbps);
                if (error != 0) {
                        dmu_tx_commit(tx);
                        break;
@@ -1467,7 +1467,7 @@ zfs_clone_range_replay(znode_t *zp, uint64_t off, uint64_t len, uint64_t blksz,
        if (zp->z_blksz < blksz)
                zfs_grow_blocksize(zp, blksz, tx);
 
-       dmu_brt_clone(zfsvfs->z_os, zp->z_id, off, len, tx, bps, nbps, B_TRUE);
+       dmu_brt_clone(zfsvfs->z_os, zp->z_id, off, len, tx, bps, nbps);
 
        zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime);