streak, bg->start, space_info->flags);
}
+/* A refused operation's retry window is open: see btrfs_exclop_start(). */
+static bool reclaim_yielding(const struct btrfs_fs_info *fs_info)
+{
+ const unsigned long until = READ_ONCE(fs_info->reclaim_yield_until);
+
+ return until && time_before(jiffies, until);
+}
+
+/*
+ * Unmounting, or read-only after an error: every group taken from here on is
+ * dropped (see btrfs_need_cleaner_sleep()), so do not wait out a gap for them.
+ */
+static bool reclaim_stopping(const struct btrfs_fs_info *fs_info)
+{
+ return btrfs_need_cleaner_sleep(fs_info) || BTRFS_FS_ERROR(fs_info);
+}
+
void btrfs_reclaim_block_groups(struct btrfs_fs_info *fs_info, unsigned int limit)
{
struct btrfs_block_group *bg;
if (!btrfs_should_reclaim(fs_info))
return;
+ if (reclaim_yielding(fs_info))
+ return;
+
guard(super_write)(fs_info->sb);
- if (!btrfs_exclop_start(fs_info, BTRFS_EXCLOP_BALANCE))
+ if (!btrfs_exclop_start_reclaim(fs_info))
return;
/*
list_sort(NULL, &fs_info->reclaim_bgs, reclaim_bgs_cmp);
while (!list_empty(&fs_info->reclaim_bgs)) {
u64 need;
+ bool handoff = false;
int ret;
bg = list_first_entry(&fs_info->reclaim_bgs,
ret = btrfs_reclaim_block_group(bg, &reclaimed);
if (ret == -ENOSPC || reclaimed > before)
reclaim_note_result(bg, ret == -ENOSPC, need);
+ /*
+ * It was relocated, or failed to be (an error also
+ * comes from setting it read-only, which is quick).
+ * Once the limit is reached the loop ends below and
+ * releases the operation anyway: no gap.
+ */
+ handoff = (ret || reclaimed > before) && reclaimed < limit;
}
/* An ENOSPC failure is not retried at once; see above. */
btrfs_put_block_group(bg);
mutex_unlock(&fs_info->reclaim_bgs_lock);
+ /*
+ * After a group that was relocated, let a balance, replace or
+ * resize that is waiting for the exclusive operation have it: a
+ * long reclaim list must not lock them out for its whole run.
+ * A group that was skipped or dropped held it only briefly.
+ */
+ if (handoff)
+ btrfs_exclop_finish(fs_info);
/*
* Reclaiming all the block groups in the list can take really
* long. Prioritize cleaning up unused block groups.
*/
btrfs_delete_unused_bgs(fs_info);
+ if (handoff) {
+ if (reclaim_stopping(fs_info))
+ goto out;
+ /*
+ * Leave the exclusive operation free for a moment
+ * before the next group. btrfs-progs reads sysfs
+ * exclusive_operation before it calls the ioctl:
+ * without --enqueue it gives up there, and with it it
+ * waits for the notification that btrfs_exclop_finish()
+ * just sent. Either way the refusal never reaches
+ * btrfs_exclop_start(), so only a gap lets a balance,
+ * replace or resize in while the list is long.
+ */
+ if (!list_empty_careful(&fs_info->reclaim_bgs))
+ schedule_timeout_idle(BTRFS_RECLAIM_GAP);
+ /*
+ * Someone was refused the exclusive operation while
+ * we held it: leave the rest of the list to a run
+ * after their retry; stop as well if the filesystem
+ * started closing or failed meanwhile.
+ */
+ if (reclaim_yielding(fs_info) ||
+ reclaim_stopping(fs_info))
+ goto out;
+ if (!btrfs_exclop_start_reclaim(fs_info))
+ goto out;
+ }
/*
* If we are interrupted by a balance, we can just bail out. The
* cleaner thread restart again if necessary.
spin_unlock(&fs_info->unused_bgs_lock);
mutex_unlock(&fs_info->reclaim_bgs_lock);
end:
+ btrfs_exclop_finish(fs_info);
+out:
spin_lock(&fs_info->unused_bgs_lock);
list_splice_tail(&retry_list, &fs_info->reclaim_bgs);
spin_unlock(&fs_info->unused_bgs_lock);
- btrfs_exclop_finish(fs_info);
}
void btrfs_reclaim_bgs_work(struct work_struct *work)
if (fs_info->exclusive_operation == BTRFS_EXCLOP_NONE) {
fs_info->exclusive_operation = type;
ret = true;
+ } else if (test_bit(BTRFS_FS_RECLAIM_EXCLOP, &fs_info->flags)) {
+ /*
+ * The block group reclaim worker holds it. It is background
+ * work: have it stand aside after its current group, long
+ * enough for the caller to retry.
+ */
+ WRITE_ONCE(fs_info->reclaim_yield_until,
+ (jiffies + BTRFS_RECLAIM_YIELD_WINDOW) ?: 1);
+ }
+ spin_unlock(&fs_info->super_lock);
+
+ return ret;
+}
+
+/*
+ * The block group reclaim worker's balance exclusive operation: marked as
+ * the worker's, so that an operation it refuses makes it yield (see
+ * btrfs_exclop_start()).
+ */
+bool btrfs_exclop_start_reclaim(struct btrfs_fs_info *fs_info)
+{
+ bool ret = false;
+
+ spin_lock(&fs_info->super_lock);
+ if (fs_info->exclusive_operation == BTRFS_EXCLOP_NONE) {
+ fs_info->exclusive_operation = BTRFS_EXCLOP_BALANCE;
+ set_bit(BTRFS_FS_RECLAIM_EXCLOP, &fs_info->flags);
+ ret = true;
}
spin_unlock(&fs_info->super_lock);
{
spin_lock(&fs_info->super_lock);
WRITE_ONCE(fs_info->exclusive_operation, BTRFS_EXCLOP_NONE);
+ clear_bit(BTRFS_FS_RECLAIM_EXCLOP, &fs_info->flags);
spin_unlock(&fs_info->super_lock);
sysfs_notify(&fs_info->fs_devices->fsid_kobj, NULL, "exclusive_operation");
}
*/
BTRFS_FS_STRIPE_ALLOC_ENABLING,
+ /* The block group reclaim worker holds the exclusive operation. */
+ BTRFS_FS_RECLAIM_EXCLOP,
+
#if BITS_PER_LONG == 32
/* Indicate if we have error/warn message printed on 32bit systems */
BTRFS_FS_32BIT_ERROR,
/* Reclaim partially filled block groups in the background */
struct work_struct reclaim_bgs_work;
+ /*
+ * A balance, replace, resize or device add/remove was refused while
+ * the reclaim worker held the exclusive operation: until this jiffies
+ * value the worker neither starts nor takes another group, so that the
+ * caller's retry gets the operation.
+ */
+ unsigned long reclaim_yield_until;
/* Protected by unused_bgs_lock. */
struct list_head reclaim_bgs;
int bg_reclaim_threshold;
}
bool __attribute_const__ btrfs_supported_blocksize(u32 blocksize);
+/* How long the reclaim worker stands aside after a refused operation. */
+#define BTRFS_RECLAIM_YIELD_WINDOW (60 * HZ)
+/* How long it leaves the exclusive operation free between two groups. */
+#define BTRFS_RECLAIM_GAP (12 * HZ)
+
+bool btrfs_exclop_start_reclaim(struct btrfs_fs_info *fs_info);
bool btrfs_exclop_start(struct btrfs_fs_info *fs_info,
enum btrfs_exclusive_operation type);
bool btrfs_exclop_start_try_lock(struct btrfs_fs_info *fs_info,