]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
bcachefs: opts.rebalance_on_ac_only
authorKent Overstreet <kent.overstreet@linux.dev>
Tue, 6 May 2025 00:35:36 +0000 (20:35 -0400)
committerKent Overstreet <kent.overstreet@linux.dev>
Thu, 22 May 2025 00:14:48 +0000 (20:14 -0400)
Add an option for setting rebalance to only run when connected to mains
power.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
fs/bcachefs/bcachefs_format.h
fs/bcachefs/opts.h
fs/bcachefs/rebalance.c
fs/bcachefs/rebalance.h
fs/bcachefs/rebalance_types.h
fs/bcachefs/super.c

index 0beff6af7ecf28a074fc46d89c9f0882a016164f..061fa2666f35b495ff828ee6dd8089a46d12ea67 100644 (file)
@@ -870,6 +870,7 @@ LE64_BITMASK(BCH_SB_WRITE_ERROR_TIMEOUT,struct bch_sb, flags[6],  4, 14);
 LE64_BITMASK(BCH_SB_CSUM_ERR_RETRY_NR, struct bch_sb, flags[6], 14, 20);
 LE64_BITMASK(BCH_SB_DEGRADED_ACTION,   struct bch_sb, flags[6], 20, 22);
 LE64_BITMASK(BCH_SB_CASEFOLD,          struct bch_sb, flags[6], 22, 23);
+LE64_BITMASK(BCH_SB_REBALANCE_AC_ONLY, struct bch_sb, flags[6], 23, 24);
 
 static inline __u64 BCH_SB_COMPRESSION_TYPE(const struct bch_sb *sb)
 {
index b8cd0b04e62aee169e01f15424fa5026c363d050..f4c014ad43c159acaf121a54477296e87cd9fef1 100644 (file)
@@ -490,6 +490,11 @@ enum fsck_err_opts {
          BCH2_NO_SB_OPT,                       true,                   \
          NULL,         "Enable rebalance: disable for debugging, or to\n"\
                        "quiet the system when doing performance testing\n")\
+       x(rebalance_on_ac_only,         u8,                             \
+         OPT_FS|OPT_MOUNT|OPT_RUNTIME,                                 \
+         OPT_BOOL(),                                                   \
+         BCH_SB_REBALANCE_AC_ONLY,             false,                  \
+         NULL,         "Enable rebalance while on mains power only\n") \
        x(no_data_io,                   u8,                             \
          OPT_MOUNT,                                                    \
          OPT_BOOL(),                                                   \
index 7bcebcac2e1ad1f825443f86a59f423aecd88ce3..8fefe2b174c2af0d89c826f0abaad56e73c7473c 100644 (file)
@@ -518,6 +518,13 @@ static void rebalance_wait(struct bch_fs *c)
        bch2_kthread_io_clock_wait(clock, r->wait_iotime_end, MAX_SCHEDULE_TIMEOUT);
 }
 
+static bool bch2_rebalance_enabled(struct bch_fs *c)
+{
+       return c->opts.rebalance_enabled &&
+               !(c->opts.rebalance_on_ac_only &&
+                 c->rebalance.on_battery);
+}
+
 static int do_rebalance(struct moving_context *ctxt)
 {
        struct btree_trans *trans = ctxt->trans;
@@ -537,9 +544,9 @@ static int do_rebalance(struct moving_context *ctxt)
                             BTREE_ITER_all_snapshots);
 
        while (!bch2_move_ratelimit(ctxt)) {
-               if (!c->opts.rebalance_enabled) {
+               if (!bch2_rebalance_enabled(c)) {
                        bch2_moving_ctxt_flush_all(ctxt);
-                       kthread_wait_freezable(c->opts.rebalance_enabled ||
+                       kthread_wait_freezable(bch2_rebalance_enabled(c) ||
                                               kthread_should_stop());
                }
 
@@ -714,9 +721,42 @@ int bch2_rebalance_start(struct bch_fs *c)
        return 0;
 }
 
-void bch2_fs_rebalance_init(struct bch_fs *c)
+#ifdef CONFIG_POWER_SUPPLY
+#include <linux/power_supply.h>
+
+static int bch2_rebalance_power_notifier(struct notifier_block *nb,
+                                        unsigned long event, void *data)
+{
+       struct bch_fs *c = container_of(nb, struct bch_fs, rebalance.power_notifier);
+
+       c->rebalance.on_battery = !power_supply_is_system_supplied();
+       bch2_rebalance_wakeup(c);
+       return NOTIFY_OK;
+}
+#endif
+
+void bch2_fs_rebalance_exit(struct bch_fs *c)
 {
-       bch2_pd_controller_init(&c->rebalance.pd);
+#ifdef CONFIG_POWER_SUPPLY
+       power_supply_unreg_notifier(&c->rebalance.power_notifier);
+#endif
+}
+
+int bch2_fs_rebalance_init(struct bch_fs *c)
+{
+       struct bch_fs_rebalance *r = &c->rebalance;
+
+       bch2_pd_controller_init(&r->pd);
+
+#ifdef CONFIG_POWER_SUPPLY
+       r->power_notifier.notifier_call = bch2_rebalance_power_notifier;
+       int ret = power_supply_reg_notifier(&r->power_notifier);
+       if (ret)
+               return ret;
+
+       r->on_battery = !power_supply_is_system_supplied();
+#endif
+       return 0;
 }
 
 static int check_rebalance_work_one(struct btree_trans *trans,
index b7c8c0652ad62a4b7ae29eb43a9b6f67095099b8..5d9214fe1a22b5534d54b0379f09cd37f605f506 100644 (file)
@@ -52,7 +52,9 @@ void bch2_rebalance_status_to_text(struct printbuf *, struct bch_fs *);
 
 void bch2_rebalance_stop(struct bch_fs *);
 int bch2_rebalance_start(struct bch_fs *);
-void bch2_fs_rebalance_init(struct bch_fs *);
+
+void bch2_fs_rebalance_exit(struct bch_fs *);
+int bch2_fs_rebalance_init(struct bch_fs *);
 
 int bch2_check_rebalance_work(struct bch_fs *);
 
index fe5098c17dfc0a4b9a6d8d142314c86163367d05..33d77286f1d565c7a1f811e94805ff1670055c51 100644 (file)
@@ -30,6 +30,11 @@ struct bch_fs_rebalance {
        struct bbpos                    scan_start;
        struct bbpos                    scan_end;
        struct bch_move_stats           scan_stats;
+
+       bool                            on_battery;
+#ifdef CONFIG_POWER_SUPPLY
+       struct notifier_block           power_notifier;
+#endif
 };
 
 #endif /* _BCACHEFS_REBALANCE_TYPES_H */
index 7c6ea43b43471825c814934aef536e503a6deefc..bd0565b7a9ba0cb5ee737c6f9a7cd2c89e2ab3dd 100644 (file)
@@ -583,6 +583,7 @@ static void __bch2_fs_free(struct bch_fs *c)
        bch2_fs_snapshots_exit(c);
        bch2_fs_sb_errors_exit(c);
        bch2_fs_replicas_exit(c);
+       bch2_fs_rebalance_exit(c);
        bch2_fs_quota_exit(c);
        bch2_fs_nocow_locking_exit(c);
        bch2_fs_journal_exit(&c->journal);
@@ -867,7 +868,6 @@ static struct bch_fs *bch2_fs_alloc(struct bch_sb *sb, struct bch_opts *opts,
        bch2_fs_move_init(c);
        bch2_fs_nocow_locking_init_early(c);
        bch2_fs_quota_init(c);
-       bch2_fs_rebalance_init(c);
        bch2_fs_sb_errors_init_early(c);
        bch2_fs_snapshots_init_early(c);
        bch2_fs_subvolumes_init_early(c);
@@ -989,6 +989,7 @@ static struct bch_fs *bch2_fs_alloc(struct bch_sb *sb, struct bch_opts *opts,
            bch2_fs_fsio_init(c) ?:
            bch2_fs_fs_io_direct_init(c) ?:
            bch2_fs_io_read_init(c) ?:
+           bch2_fs_rebalance_init(c) ?:
            bch2_fs_sb_errors_init(c) ?:
            bch2_fs_vfs_init(c);
        if (ret)