u64 owner; /* LOG class: btrfs_ino of the owner */
enum btrfs_stripe_run_class class;
bool open; /* accepting allocations */
+ /*
+ * Relocation's run: kept after it closes and drains, until relocation
+ * releases it (btrfs_release_stripe_reloc_runs()). See
+ * run_freed_on_drain().
+ */
+ bool retained;
/*
* One bit per sector of the run, set while the sector holds an
* allocation someone still intends to write. Padding uses it to find
tail_len = run->end - run->offset;
run->end = run->offset;
bg->stripe_open_remainder -= tail_len;
- if (open_stripe_run_drained(run))
+ if (open_stripe_run_drained(run) && !run->retained)
free_open_stripe_run(bg, run);
return tail_len;
}
}
new_run->bg = bg;
new_run->class = class;
+ new_run->retained = class == BTRFS_STRIPE_RUN_RELOC;
+ if (new_run->retained)
+ atomic64_inc(&fs_info->stripe_run_stats.reloc_run_retained);
new_run->owner = 0;
new_run->start = start;
new_run->end = start + len;
ASSERT(run->inflight_bytes >= num_bytes);
run_live_set(run, start, num_bytes, false);
run->inflight_bytes -= num_bytes;
- if (open_stripe_run_drained(run))
+ if (open_stripe_run_drained(run) && !run->retained)
free_open_stripe_run(bg, run);
break;
}
ASSERT(start + num_bytes <= run->offset);
ASSERT(run->inflight_bytes >= num_bytes);
run->inflight_bytes -= num_bytes;
- if (open_stripe_run_drained(run))
+ if (open_stripe_run_drained(run) && !run->retained)
free_open_stripe_run(bg, run);
break;
}
spin_lock_irqsave(&bg->stripe_run_lock, flags);
ASSERT(run->inflight_bytes >= num_bytes);
run->inflight_bytes -= num_bytes;
- if (open_stripe_run_drained(run))
+ if (open_stripe_run_drained(run) && !run->retained)
free_open_stripe_run(bg, run);
spin_unlock_irqrestore(&bg->stripe_run_lock, flags);
}
spin_lock_irqsave(&bg->stripe_run_lock, flags);
list_for_each_entry(run, &bg->open_stripe_runs, list) {
+ /* Nor are retained relocation runs, once nothing is in flight. */
+ if (run->retained && open_stripe_run_drained(run) &&
+ seq != U64_MAX)
+ continue;
if (run->open_seq < seq) {
ret = false;
break;
return ret;
}
+/*
+ * Stop retaining @bg's relocation runs: free the ones already drained, and
+ * let any still open or in flight be freed when they drain, like any other.
+ */
+static void release_retained_stripe_runs(struct btrfs_block_group *bg)
+{
+ struct btrfs_open_stripe_run *run, *tmp;
+ unsigned long flags;
+
+ spin_lock_irqsave(&bg->stripe_run_lock, flags);
+ list_for_each_entry_safe(run, tmp, &bg->open_stripe_runs, list) {
+ if (!run->retained)
+ continue;
+ run->retained = false;
+ atomic64_inc(&bg->fs_info->stripe_run_stats.reloc_run_released);
+ if (open_stripe_run_drained(run))
+ free_open_stripe_run(bg, run);
+ }
+ spin_unlock_irqrestore(&bg->stripe_run_lock, flags);
+}
+
+/*
+ * Relocation is done with its destination stripes: release its retained
+ * runs in every block group (the dedication of btrfs_release_data_reloc_bg()
+ * can have moved between groups during the relocation).
+ */
+static void btrfs_release_stripe_reloc_runs(struct btrfs_fs_info *fs_info)
+{
+ struct btrfs_block_group *bg;
+
+ spin_lock(&fs_info->open_stripe_lock);
+ list_for_each_entry(bg, &fs_info->open_stripe_bgs, open_stripe_bg_list)
+ release_retained_stripe_runs(bg);
+ spin_unlock(&fs_info->open_stripe_lock);
+}
+
/*
* Close and drain all of a block group's stripe runs. The caller must
* prevent new allocations first (e.g. the block group is read-only),
{
struct btrfs_fs_info *fs_info = bg->fs_info;
+ /* Nothing may outlive this, relocation's retained runs included. */
+ release_retained_stripe_runs(bg);
close_block_group_stripe_runs(bg, U64_MAX);
/* Parked partial-stripe rbios hold the very bios the wait drains. */
btrfs_flush_parked_rbios(fs_info, bg->start, bg->length, false);
spin_unlock(&fs_info->relocation_bg_lock);
if (!bytenr)
- return;
+ goto release;
bg = btrfs_lookup_block_group(fs_info, bytenr);
if (!bg)
- return;
+ goto release;
if (bg->space_info) {
spin_lock(&bg->space_info->lock);
bg->space_info->bytes_stripe_claimable_reloc = 0;
}
btrfs_close_bg_open_stripes(bg);
btrfs_put_block_group(bg);
+release:
+ btrfs_release_stripe_reloc_runs(fs_info);
}
struct btrfs_caching_control *btrfs_get_caching_control(