bcachefs: Add option for completely disabling nocow
authorKent Overstreet <kent.overstreet@linux.dev>
Sat, 25 Feb 2023 00:07:21 +0000 (19:07 -0500)
committerKent Overstreet <kent.overstreet@linux.dev>
Sun, 22 Oct 2023 21:09:54 +0000 (17:09 -0400)
This adds an option for completely disabling nocow mode, including the
locking in the data move path.

Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
fs/bcachefs/data_update.c
fs/bcachefs/io.c
fs/bcachefs/opts.h

index c98a393f49168ddd9ed6216ad97f94bec50d1a80..dacea5e040003a142f06bcbcac261a825ee7ca34 100644 (file)
@@ -327,8 +327,9 @@ void bch2_data_update_exit(struct data_update *update)
        const struct bch_extent_ptr *ptr;
 
        bkey_for_each_ptr(ptrs, ptr) {
-               bch2_bucket_nocow_unlock(&c->nocow_locks,
-                                        PTR_BUCKET_POS(c, ptr), 0);
+               if (c->opts.nocow_enabled)
+                       bch2_bucket_nocow_unlock(&c->nocow_locks,
+                                                PTR_BUCKET_POS(c, ptr), 0);
                percpu_ref_put(&bch_dev_bkey_exists(c, ptr->dev)->ref);
        }
 
@@ -488,23 +489,26 @@ int bch2_data_update_init(struct btree_trans *trans,
                if (p.crc.compression_type == BCH_COMPRESSION_TYPE_incompressible)
                        m->op.incompressible = true;
 
-               if (ctxt) {
-                       move_ctxt_wait_event(ctxt, trans,
-                                       (locked = bch2_bucket_nocow_trylock(&c->nocow_locks,
-                                                                 PTR_BUCKET_POS(c, &p.ptr), 0)) ||
-                                       !atomic_read(&ctxt->read_sectors));
-
-                       if (!locked)
-                               bch2_bucket_nocow_lock(&c->nocow_locks,
-                                                      PTR_BUCKET_POS(c, &p.ptr), 0);
-               } else {
-                       if (!bch2_bucket_nocow_trylock(&c->nocow_locks,
-                                                      PTR_BUCKET_POS(c, &p.ptr), 0)) {
-                               ret = -BCH_ERR_nocow_lock_blocked;
-                               goto err;
+               if (c->opts.nocow_enabled) {
+                       if (ctxt) {
+                               move_ctxt_wait_event(ctxt, trans,
+                                               (locked = bch2_bucket_nocow_trylock(&c->nocow_locks,
+                                                                         PTR_BUCKET_POS(c, &p.ptr), 0)) ||
+                                               !atomic_read(&ctxt->read_sectors));
+
+                               if (!locked)
+                                       bch2_bucket_nocow_lock(&c->nocow_locks,
+                                                              PTR_BUCKET_POS(c, &p.ptr), 0);
+                       } else {
+                               if (!bch2_bucket_nocow_trylock(&c->nocow_locks,
+                                                              PTR_BUCKET_POS(c, &p.ptr), 0)) {
+                                       ret = -BCH_ERR_nocow_lock_blocked;
+                                       goto err;
+                               }
                        }
+                       ptrs_locked |= (1U << i);
                }
-               ptrs_locked |= (1U << i);
+
                i++;
        }
 
index 6f7e4dac4268fcab811145f2736c9308c6f77227..ede2f3116935b8cf63e2f62a535be0c9b9c4760d 100644 (file)
@@ -1649,7 +1649,7 @@ static void __bch2_write(struct bch_write_op *op)
 
        nofs_flags = memalloc_nofs_save();
 
-       if (unlikely(op->opts.nocow)) {
+       if (unlikely(op->opts.nocow && c->opts.nocow_enabled)) {
                bch2_nocow_write(op);
                if (op->flags & BCH_WRITE_DONE)
                        goto out_nofs_restore;
index fc444c68025c8fa6efa321b5837cc486ae079d52..afbf82d629779c4bd1013a12072e7c9761552da8 100644 (file)
@@ -404,6 +404,12 @@ enum opt_type {
          NULL,         "Nocow mode: Writes will be done in place when possible.\n"\
                        "Snapshots and reflink will still caused writes to be COW\n"\
                        "Implicitly disables data checksumming, compression and encryption")\
+       x(nocow_enabled,                u8,                             \
+         OPT_FS|OPT_MOUNT,                                             \
+         OPT_BOOL(),                                                   \
+         BCH2_NO_SB_OPT,                       true,                   \
+         NULL,         "Enable nocow mode: enables runtime locking in\n"\
+                       "data move path needed if nocow will ever be in use\n")\
        x(no_data_io,                   u8,                             \
          OPT_MOUNT,                                                    \
          OPT_BOOL(),                                                   \