#include "misc.h"
#include "ctree.h"
#include "block-group.h"
+#include "ordered-data.h"
+#include "btrfs_inode.h"
#include "space-info.h"
#include "disk-io.h"
#include "free-space-cache.h"
"stripe_alloc does not cover raid56 metadata: the write hole remains for metadata, consider converting it to raid1c3/raid1c4");
}
+/*
+ * Drain the writes the legacy allocator placed before stripe_alloc was
+ * enabled on a live filesystem, then arm the write-hole invariant check.
+ * Runs in a worker: the property that enables the policy is applied inside
+ * a transaction, where flushing delalloc cannot wait.
+ */
+void btrfs_stripe_alloc_enable_work(struct work_struct *work)
+{
+ struct btrfs_fs_info *fs_info = container_of(work, struct btrfs_fs_info,
+ stripe_alloc_enable_work);
+
+ btrfs_start_delalloc_roots(fs_info, LONG_MAX, false);
+ btrfs_wait_ordered_roots(fs_info, U64_MAX, NULL);
+ clear_bit(BTRFS_FS_STRIPE_ALLOC_ENABLING, &fs_info->flags);
+ btrfs_info(fs_info,
+ "stripe_alloc: writes placed before the policy was enabled have landed");
+}
+
+/*
+ * Turn on stripe-exclusive allocation, from the "stripe_alloc" filesystem
+ * property on the top-level subvolume's root directory. Runs during mount
+ * when the root directory inode loads its properties -- before any user IO
+ * -- or at runtime from btrfs_set_prop().
+ */
int btrfs_enable_stripe_alloc(struct btrfs_fs_info *fs_info)
{
+ bool live = test_bit(BTRFS_FS_OPEN, &fs_info->flags);
int ret;
if (btrfs_test_opt(fs_info, STRIPE_ALLOC))
ret = btrfs_stripe_alloc_check_support(fs_info);
if (ret)
return ret;
+ /*
+ * On a live filesystem the legacy allocator may still have writes in
+ * flight. Their extents sit in partly used stripes that no stripe run
+ * covers, so the write-hole invariant check would report them although
+ * they were placed before the policy existed. Keep the check off until
+ * every ordered extent that could have been allocated before the flip
+ * has landed; extents allocated after it have stripe runs. A mount-time
+ * enable has nothing in flight and skips the drain.
+ */
+ if (live)
+ set_bit(BTRFS_FS_STRIPE_ALLOC_ENABLING, &fs_info->flags);
btrfs_set_opt(fs_info->mount_opt, STRIPE_ALLOC);
warn_stripe_alloc_uncovered(fs_info);
/*
stripe_alloc_sweep_groups(fs_info, true);
btrfs_info(fs_info,
"using stripe-exclusive allocation for raid56 data");
+ if (live)
+ queue_work(system_unbound_wq, &fs_info->stripe_alloc_enable_work);
return 0;
}
return ret;
}
+#ifdef CONFIG_BTRFS_DEBUG
+/*
+ * Write-hole invariant checker, called for every raid56 write (full-stripe
+ * and sub-stripe alike). Under stripe_alloc and stripe_meta a stripe may
+ * only be written while an open or draining run covers it; a write outside
+ * every run means a retired stripe is being rewritten or an allocation
+ * bypassed the policy.
+ *
+ * Groups that hosted RELOC or NOCOW runs are skipped: those extents are
+ * legitimately overwritten in place after their runs drain.
+ */
+void btrfs_stripe_check_write(struct btrfs_fs_info *fs_info,
+ u64 full_stripe_start, bool sub_stripe)
+{
+ struct btrfs_open_stripe_run *run;
+ struct btrfs_block_group *bg;
+ unsigned long flags;
+ bool live = false;
+ u64 fsl;
+
+ if (!btrfs_test_opt(fs_info, STRIPE_ALLOC))
+ return;
+ /* the legacy allocator's writes are still landing after a runtime enable */
+ if (test_bit(BTRFS_FS_STRIPE_ALLOC_ENABLING, &fs_info->flags))
+ return;
+ bg = btrfs_lookup_block_group(fs_info, full_stripe_start);
+ if (!bg)
+ return;
+ if (!btrfs_is_stripe_alloc_bg(bg) ||
+ test_bit(BLOCK_GROUP_FLAG_STRIPE_RELOC_USED, &bg->runtime_flags))
+ goto out;
+ fsl = bg->full_stripe_len;
+ spin_lock_irqsave(&bg->stripe_run_lock, flags);
+ list_for_each_entry(run, &bg->open_stripe_runs, list) {
+ if (full_stripe_start < run->end &&
+ full_stripe_start + fsl > run->start) {
+ live = true;
+ break;
+ }
+ }
+ spin_unlock_irqrestore(&bg->stripe_run_lock, flags);
+ WARN_ONCE(!live,
+"btrfs: %s write to stripe %llu (block group %llu) outside any live stripe run: write hole window violated",
+ sub_stripe ? "sub-stripe" : "full-stripe",
+ full_stripe_start, bg->start);
+out:
+ btrfs_put_block_group(bg);
+}
+#endif
+
/*
* Drop the dedication of a block group to data relocation. Shared by the
* zoned allocator and the stripe allocation policy; both dedicate one
BLOCK_GROUP_FLAG_NEW,
BLOCK_GROUP_FLAG_FULLY_REMAPPED,
BLOCK_GROUP_FLAG_STRIPE_REMOVAL_PENDING,
+ /*
+ * The group has hosted relocation-class stripe runs. Relocation
+ * overwrites its preallocated extents in place after their runs
+ * drain, so the write-hole debug check cannot tell those writes
+ * from violations and skips such groups (sticky, debug only).
+ */
+ BLOCK_GROUP_FLAG_STRIPE_RELOC_USED,
/*
* Free space changed this transaction, so the group's stripe_unusable
* counter must be rescanned at commit (stripe_alloc only).
bool btrfs_stripe_alloc_forces_cow(struct btrfs_fs_info *fs_info, u64 bytenr);
bool btrfs_is_stripe_alloc_bg(const struct btrfs_block_group *bg);
void btrfs_scan_stripe_unusable(struct btrfs_fs_info *fs_info, bool force);
+void btrfs_stripe_alloc_enable_work(struct work_struct *work);
+#ifdef CONFIG_BTRFS_DEBUG
+void btrfs_stripe_check_write(struct btrfs_fs_info *fs_info,
+ u64 full_stripe_start, bool sub_stripe);
+#else
+static inline void btrfs_stripe_check_write(struct btrfs_fs_info *fs_info,
+ u64 full_stripe_start,
+ bool sub_stripe) { }
+#endif
struct btrfs_block_group *btrfs_inc_nocow_writers(struct btrfs_fs_info *fs_info,
u64 bytenr);
void btrfs_dec_nocow_writers(struct btrfs_block_group *bg);