]> git.karo-electronics.de Git - karo-tx-linux.git/blobdiff - drivers/block/drbd/drbd_nl.c
drbd: Fixed processing of disk-barrier, disk-flushes and disk-drain
[karo-tx-linux.git] / drivers / block / drbd / drbd_nl.c
index d4b29fd603f4d0a57e8ebdfcf5642a1bfe0cb906..ce9f4ca55ce20f391951022d9402c3efa1ff60f6 100644 (file)
@@ -47,8 +47,8 @@
 int drbd_adm_add_minor(struct sk_buff *skb, struct genl_info *info);
 int drbd_adm_delete_minor(struct sk_buff *skb, struct genl_info *info);
 
-int drbd_adm_create_connection(struct sk_buff *skb, struct genl_info *info);
-int drbd_adm_delete_connection(struct sk_buff *skb, struct genl_info *info);
+int drbd_adm_new_resource(struct sk_buff *skb, struct genl_info *info);
+int drbd_adm_del_resource(struct sk_buff *skb, struct genl_info *info);
 int drbd_adm_down(struct sk_buff *skb, struct genl_info *info);
 
 int drbd_adm_set_role(struct sk_buff *skb, struct genl_info *info);
@@ -75,6 +75,7 @@ int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info);
 int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb);
 
 #include <linux/drbd_genl_api.h>
+#include "drbd_nla.h"
 #include <linux/genl_magic_func.h>
 
 /* used blkdev_get_by_path, to claim our meta data device(s) */
@@ -92,7 +93,9 @@ static struct drbd_config_context {
 #define VOLUME_UNSPECIFIED             (-1U)
        /* pointer into the request skb,
         * limited lifetime! */
-       char *conn_name;
+       char *resource_name;
+       struct nlattr *my_addr;
+       struct nlattr *peer_addr;
 
        /* reply buffer */
        struct sk_buff *reply_skb;
@@ -140,7 +143,8 @@ int drbd_msg_put_info(const char *info)
  * If it returns successfully, adm_ctx members are valid.
  */
 #define DRBD_ADM_NEED_MINOR    1
-#define DRBD_ADM_NEED_CONN     2
+#define DRBD_ADM_NEED_RESOURCE 2
+#define DRBD_ADM_NEED_CONNECTION 4
 static int drbd_adm_prepare(struct sk_buff *skb, struct genl_info *info,
                unsigned flags)
 {
@@ -156,19 +160,24 @@ static int drbd_adm_prepare(struct sk_buff *skb, struct genl_info *info,
               return -EPERM;
 
        adm_ctx.reply_skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
-       if (!adm_ctx.reply_skb)
+       if (!adm_ctx.reply_skb) {
+               err = -ENOMEM;
                goto fail;
+       }
 
        adm_ctx.reply_dh = genlmsg_put_reply(adm_ctx.reply_skb,
                                        info, &drbd_genl_family, 0, cmd);
        /* put of a few bytes into a fresh skb of >= 4k will always succeed.
         * but anyways */
-       if (!adm_ctx.reply_dh)
+       if (!adm_ctx.reply_dh) {
+               err = -ENOMEM;
                goto fail;
+       }
 
        adm_ctx.reply_dh->minor = d_in->minor;
        adm_ctx.reply_dh->ret_code = NO_ERROR;
 
+       adm_ctx.volume = VOLUME_UNSPECIFIED;
        if (info->attrs[DRBD_NLA_CFG_CONTEXT]) {
                struct nlattr *nla;
                /* parse and validate only */
@@ -186,32 +195,62 @@ static int drbd_adm_prepare(struct sk_buff *skb, struct genl_info *info,
 
                /* and assign stuff to the global adm_ctx */
                nla = nested_attr_tb[__nla_type(T_ctx_volume)];
-               adm_ctx.volume = nla ? nla_get_u32(nla) : VOLUME_UNSPECIFIED;
-               nla = nested_attr_tb[__nla_type(T_ctx_conn_name)];
                if (nla)
-                       adm_ctx.conn_name = nla_data(nla);
-       } else
-               adm_ctx.volume = VOLUME_UNSPECIFIED;
+                       adm_ctx.volume = nla_get_u32(nla);
+               nla = nested_attr_tb[__nla_type(T_ctx_resource_name)];
+               if (nla)
+                       adm_ctx.resource_name = nla_data(nla);
+               adm_ctx.my_addr = nested_attr_tb[__nla_type(T_ctx_my_addr)];
+               adm_ctx.peer_addr = nested_attr_tb[__nla_type(T_ctx_peer_addr)];
+               if ((adm_ctx.my_addr &&
+                    nla_len(adm_ctx.my_addr) > sizeof(adm_ctx.tconn->my_addr)) ||
+                   (adm_ctx.peer_addr &&
+                    nla_len(adm_ctx.peer_addr) > sizeof(adm_ctx.tconn->peer_addr))) {
+                       err = -EINVAL;
+                       goto fail;
+               }
+       }
 
        adm_ctx.minor = d_in->minor;
        adm_ctx.mdev = minor_to_mdev(d_in->minor);
-       adm_ctx.tconn = conn_by_name(adm_ctx.conn_name);
+       adm_ctx.tconn = conn_get_by_name(adm_ctx.resource_name);
 
        if (!adm_ctx.mdev && (flags & DRBD_ADM_NEED_MINOR)) {
                drbd_msg_put_info("unknown minor");
                return ERR_MINOR_INVALID;
        }
-       if (!adm_ctx.tconn && (flags & DRBD_ADM_NEED_CONN)) {
-               drbd_msg_put_info("unknown connection");
+       if (!adm_ctx.tconn && (flags & DRBD_ADM_NEED_RESOURCE)) {
+               drbd_msg_put_info("unknown resource");
                return ERR_INVALID_REQUEST;
        }
 
+       if (flags & DRBD_ADM_NEED_CONNECTION) {
+               if (adm_ctx.tconn && !(flags & DRBD_ADM_NEED_RESOURCE)) {
+                       drbd_msg_put_info("no resource name expected");
+                       return ERR_INVALID_REQUEST;
+               }
+               if (adm_ctx.mdev) {
+                       drbd_msg_put_info("no minor number expected");
+                       return ERR_INVALID_REQUEST;
+               }
+               if (adm_ctx.my_addr && adm_ctx.peer_addr)
+                       adm_ctx.tconn = conn_get_by_addrs(nla_data(adm_ctx.my_addr),
+                                                         nla_len(adm_ctx.my_addr),
+                                                         nla_data(adm_ctx.peer_addr),
+                                                         nla_len(adm_ctx.peer_addr));
+               if (!adm_ctx.tconn) {
+                       drbd_msg_put_info("unknown connection");
+                       return ERR_INVALID_REQUEST;
+               }
+       }
+
        /* some more paranoia, if the request was over-determined */
        if (adm_ctx.mdev && adm_ctx.tconn &&
            adm_ctx.mdev->tconn != adm_ctx.tconn) {
-               pr_warning("request: minor=%u, conn=%s; but that minor belongs to connection %s\n",
-                               adm_ctx.minor, adm_ctx.conn_name, adm_ctx.mdev->tconn->name);
-               drbd_msg_put_info("minor exists in different connection");
+               pr_warning("request: minor=%u, resource=%s; but that minor belongs to connection %s\n",
+                               adm_ctx.minor, adm_ctx.resource_name,
+                               adm_ctx.mdev->tconn->name);
+               drbd_msg_put_info("minor exists in different resource");
                return ERR_INVALID_REQUEST;
        }
        if (adm_ctx.mdev &&
@@ -223,33 +262,26 @@ static int drbd_adm_prepare(struct sk_buff *skb, struct genl_info *info,
                drbd_msg_put_info("minor exists as different volume");
                return ERR_INVALID_REQUEST;
        }
-       if (adm_ctx.mdev && !adm_ctx.tconn)
-               adm_ctx.tconn = adm_ctx.mdev->tconn;
+
        return NO_ERROR;
 
 fail:
        nlmsg_free(adm_ctx.reply_skb);
        adm_ctx.reply_skb = NULL;
-       return -ENOMEM;
+       return err;
 }
 
 static int drbd_adm_finish(struct genl_info *info, int retcode)
 {
-       struct nlattr *nla;
-       const char *conn_name = NULL;
+       if (adm_ctx.tconn) {
+               kref_put(&adm_ctx.tconn->kref, &conn_destroy);
+               adm_ctx.tconn = NULL;
+       }
 
        if (!adm_ctx.reply_skb)
                return -ENOMEM;
 
        adm_ctx.reply_dh->ret_code = retcode;
-
-       nla = info->attrs[DRBD_NLA_CFG_CONTEXT];
-       if (nla) {
-               nla = nla_find_nested(nla, __nla_type(T_ctx_conn_name));
-               if (nla)
-                       conn_name = nla_data(nla);
-       }
-
        drbd_adm_send_reply(adm_ctx.reply_skb, info);
        return 0;
 }
@@ -258,26 +290,27 @@ static void setup_khelper_env(struct drbd_tconn *tconn, char **envp)
 {
        char *afs;
 
-       if (get_net_conf(tconn)) {
-               switch (((struct sockaddr *)tconn->net_conf->peer_addr)->sa_family) {
-               case AF_INET6:
-                       afs = "ipv6";
-                       snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI6",
-                                &((struct sockaddr_in6 *)tconn->net_conf->peer_addr)->sin6_addr);
-                       break;
-               case AF_INET:
-                       afs = "ipv4";
-                       snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
-                                &((struct sockaddr_in *)tconn->net_conf->peer_addr)->sin_addr);
-                       break;
-               default:
-                       afs = "ssocks";
-                       snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
-                                &((struct sockaddr_in *)tconn->net_conf->peer_addr)->sin_addr);
-               }
-               snprintf(envp[3], 20, "DRBD_PEER_AF=%s", afs);
-               put_net_conf(tconn);
+       /* FIXME: A future version will not allow this case. */
+       if (tconn->my_addr_len == 0 || tconn->peer_addr_len == 0)
+               return;
+
+       switch (((struct sockaddr *)&tconn->peer_addr)->sa_family) {
+       case AF_INET6:
+               afs = "ipv6";
+               snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI6",
+                        &((struct sockaddr_in6 *)&tconn->peer_addr)->sin6_addr);
+               break;
+       case AF_INET:
+               afs = "ipv4";
+               snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
+                        &((struct sockaddr_in *)&tconn->peer_addr)->sin_addr);
+               break;
+       default:
+               afs = "ssocks";
+               snprintf(envp[4], 60, "DRBD_PEER_ADDRESS=%pI4",
+                        &((struct sockaddr_in *)&tconn->peer_addr)->sin_addr);
        }
+       snprintf(envp[3], 20, "DRBD_PEER_AF=%s", afs);
 }
 
 int drbd_khelper(struct drbd_conf *mdev, char *cmd)
@@ -328,10 +361,15 @@ static void conn_md_sync(struct drbd_tconn *tconn)
        struct drbd_conf *mdev;
        int vnr;
 
-       down_read(&drbd_cfg_rwsem);
-       idr_for_each_entry(&tconn->volumes, mdev, vnr)
+       rcu_read_lock();
+       idr_for_each_entry(&tconn->volumes, mdev, vnr) {
+               kref_get(&mdev->kref);
+               rcu_read_unlock();
                drbd_md_sync(mdev);
-       up_read(&drbd_cfg_rwsem);
+               kref_put(&mdev->kref, &drbd_minor_destroy);
+               rcu_read_lock();
+       }
+       rcu_read_unlock();
 }
 
 int conn_khelper(struct drbd_tconn *tconn, char *cmd)
@@ -377,7 +415,8 @@ static enum drbd_fencing_p highest_fencing_policy(struct drbd_tconn *tconn)
        rcu_read_lock();
        idr_for_each_entry(&tconn->volumes, mdev, vnr) {
                if (get_ldev_if_state(mdev, D_CONSISTENT)) {
-                       fp = max_t(enum drbd_fencing_p, fp, mdev->ldev->dc.fencing);
+                       fp = max_t(enum drbd_fencing_p, fp,
+                                  rcu_dereference(mdev->ldev->disk_conf)->fencing);
                        put_ldev(mdev);
                }
        }
@@ -476,6 +515,7 @@ static int _try_outdate_peer_async(void *data)
 
        conn_try_outdate_peer(tconn);
 
+       kref_put(&tconn->kref, &conn_destroy);
        return 0;
 }
 
@@ -483,9 +523,12 @@ void conn_try_outdate_peer_async(struct drbd_tconn *tconn)
 {
        struct task_struct *opa;
 
+       kref_get(&tconn->kref);
        opa = kthread_run(_try_outdate_peer_async, tconn, "drbd_async_h");
-       if (IS_ERR(opa))
+       if (IS_ERR(opa)) {
                conn_err(tconn, "out of mem, failed to invoke fence-peer helper\n");
+               kref_put(&tconn->kref, &conn_destroy);
+       }
 }
 
 enum drbd_state_rv
@@ -493,6 +536,7 @@ drbd_set_role(struct drbd_conf *mdev, enum drbd_role new_role, int force)
 {
        const int max_tries = 4;
        enum drbd_state_rv rv = SS_UNKNOWN_ERROR;
+       struct net_conf *nc;
        int try = 0;
        int forced = 0;
        union drbd_state mask, val;
@@ -550,7 +594,12 @@ drbd_set_role(struct drbd_conf *mdev, enum drbd_role new_role, int force)
                if (rv == SS_TWO_PRIMARIES) {
                        /* Maybe the peer is detected as dead very soon...
                           retry at most once more in this case. */
-                       schedule_timeout_interruptible((mdev->tconn->net_conf->ping_timeo+1)*HZ/10);
+                       int timeo;
+                       rcu_read_lock();
+                       nc = rcu_dereference(mdev->tconn->net_conf);
+                       timeo = nc ? (nc->ping_timeo + 1) * HZ / 10 : 1;
+                       rcu_read_unlock();
+                       schedule_timeout_interruptible(timeo);
                        if (try < max_tries)
                                try = max_tries - 1;
                        continue;
@@ -580,10 +629,12 @@ drbd_set_role(struct drbd_conf *mdev, enum drbd_role new_role, int force)
                        put_ldev(mdev);
                }
        } else {
-               if (get_net_conf(mdev->tconn)) {
-                       mdev->tconn->net_conf->want_lose = 0;
-                       put_net_conf(mdev->tconn);
-               }
+               mutex_lock(&mdev->tconn->conf_update);
+               nc = mdev->tconn->net_conf;
+               if (nc)
+                       nc->discard_my_data = 0; /* without copy; single bit op is atomic */
+               mutex_unlock(&mdev->tconn->conf_update);
+
                set_disk_ro(mdev->vdisk, false);
                if (get_ldev(mdev)) {
                        if (((mdev->state.conn < C_CONNECTED ||
@@ -603,7 +654,7 @@ drbd_set_role(struct drbd_conf *mdev, enum drbd_role new_role, int force)
                /* if this was forced, we should consider sync */
                if (forced)
                        drbd_send_uuids(mdev);
-               drbd_send_state(mdev);
+               drbd_send_current_state(mdev);
        }
 
        drbd_md_sync(mdev);
@@ -659,7 +710,12 @@ static void drbd_md_set_sector_offsets(struct drbd_conf *mdev,
                                       struct drbd_backing_dev *bdev)
 {
        sector_t md_size_sect = 0;
-       switch (bdev->dc.meta_dev_idx) {
+       int meta_dev_idx;
+
+       rcu_read_lock();
+       meta_dev_idx = rcu_dereference(bdev->disk_conf)->meta_dev_idx;
+
+       switch (meta_dev_idx) {
        default:
                /* v07 style fixed size indexed meta data */
                bdev->md.md_size_sect = MD_RESERVED_SECT;
@@ -694,6 +750,7 @@ static void drbd_md_set_sector_offsets(struct drbd_conf *mdev,
                bdev->md.bm_offset   = -md_size_sect + MD_AL_OFFSET;
                break;
        }
+       rcu_read_unlock();
 }
 
 /* input size is expected to be in KB */
@@ -756,7 +813,7 @@ void drbd_resume_io(struct drbd_conf *mdev)
 enum determine_dev_size drbd_determine_dev_size(struct drbd_conf *mdev, enum dds_flags flags) __must_hold(local)
 {
        sector_t prev_first_sect, prev_size; /* previous meta location */
-       sector_t la_size;
+       sector_t la_size, u_size;
        sector_t size;
        char ppb[10];
 
@@ -784,7 +841,10 @@ enum determine_dev_size drbd_determine_dev_size(struct drbd_conf *mdev, enum dds
        /* TODO: should only be some assert here, not (re)init... */
        drbd_md_set_sector_offsets(mdev, mdev->ldev);
 
-       size = drbd_new_dev_size(mdev, mdev->ldev, flags & DDSF_FORCED);
+       rcu_read_lock();
+       u_size = rcu_dereference(mdev->ldev->disk_conf)->disk_size;
+       rcu_read_unlock();
+       size = drbd_new_dev_size(mdev, mdev->ldev, u_size, flags & DDSF_FORCED);
 
        if (drbd_get_capacity(mdev->this_bdev) != size ||
            drbd_bm_capacity(mdev) != size) {
@@ -847,12 +907,12 @@ out:
 }
 
 sector_t
-drbd_new_dev_size(struct drbd_conf *mdev, struct drbd_backing_dev *bdev, int assume_peer_has_space)
+drbd_new_dev_size(struct drbd_conf *mdev, struct drbd_backing_dev *bdev,
+                 sector_t u_size, int assume_peer_has_space)
 {
        sector_t p_size = mdev->p_size;   /* partner's disk size. */
        sector_t la_size = bdev->md.la_size_sect; /* last agreed size. */
        sector_t m_size; /* my size */
-       sector_t u_size = bdev->dc.disk_size; /* size requested by user. */
        sector_t size = 0;
 
        m_size = drbd_get_max_capacity(bdev);
@@ -908,9 +968,6 @@ static int drbd_check_al_size(struct drbd_conf *mdev, struct disk_conf *dc)
        unsigned int in_use;
        int i;
 
-       if (!expect(dc->al_extents >= DRBD_AL_EXTENTS_MIN))
-               dc->al_extents = DRBD_AL_EXTENTS_MIN;
-
        if (mdev->act_log &&
            mdev->act_log->nr_elements == dc->al_extents)
                return 0;
@@ -959,7 +1016,9 @@ static void drbd_setup_queue_param(struct drbd_conf *mdev, unsigned int max_bio_
                struct request_queue * const b = mdev->ldev->backing_bdev->bd_disk->queue;
 
                max_hw_sectors = min(queue_max_hw_sectors(b), max_bio_size >> 9);
-               max_segments = mdev->ldev->dc.max_bio_bvecs;
+               rcu_read_lock();
+               max_segments = rcu_dereference(mdev->ldev->disk_conf)->max_bio_bvecs;
+               rcu_read_unlock();
                put_ldev(mdev);
        }
 
@@ -1003,10 +1062,13 @@ void drbd_reconsider_max_bio_size(struct drbd_conf *mdev)
           BIOs for a single peer_request */
        if (mdev->state.conn >= C_CONNECTED) {
                if (mdev->tconn->agreed_pro_version < 94)
-                       peer = mdev->peer_max_bio_size;
+                       peer = min_t(int, mdev->peer_max_bio_size, DRBD_MAX_SIZE_H80_PACKET);
+                       /* Correct old drbd (up to 8.3.7) if it believes it can do more than 32KiB */
                else if (mdev->tconn->agreed_pro_version == 94)
                        peer = DRBD_MAX_SIZE_H80_PACKET;
-               else /* drbd 8.3.8 onwards */
+               else if (mdev->tconn->agreed_pro_version < 100)
+                       peer = DRBD_MAX_BIO_SIZE_P95;  /* drbd 8.3.8 onwards, before 8.4.0 */
+               else
                        peer = DRBD_MAX_BIO_SIZE;
        }
 
@@ -1021,34 +1083,27 @@ void drbd_reconsider_max_bio_size(struct drbd_conf *mdev)
        drbd_setup_queue_param(mdev, new);
 }
 
-/* serialize deconfig (worker exiting, doing cleanup)
- * and reconfig (drbdsetup disk, drbdsetup net)
- *
- * Wait for a potentially exiting worker, then restart it,
- * or start a new one.  Flush any pending work, there may still be an
- * after_state_change queued.
- */
+/* Starts the worker thread */
 static void conn_reconfig_start(struct drbd_tconn *tconn)
 {
-       wait_event(tconn->ping_wait, !test_and_set_bit(CONFIG_PENDING, &tconn->flags));
-       wait_event(tconn->ping_wait, !test_bit(OBJECT_DYING, &tconn->flags));
        drbd_thread_start(&tconn->worker);
        conn_flush_workqueue(tconn);
 }
 
-/* if still unconfigured, stops worker again.
- * if configured now, clears CONFIG_PENDING.
- * wakes potential waiters */
+/* if still unconfigured, stops worker again. */
 static void conn_reconfig_done(struct drbd_tconn *tconn)
 {
+       bool stop_threads;
        spin_lock_irq(&tconn->req_lock);
-       if (conn_all_vols_unconf(tconn)) {
-               set_bit(OBJECT_DYING, &tconn->flags);
-               drbd_thread_stop_nowait(&tconn->worker);
-       } else
-               clear_bit(CONFIG_PENDING, &tconn->flags);
+       stop_threads = conn_all_vols_unconf(tconn) &&
+               tconn->cstate == C_STANDALONE;
        spin_unlock_irq(&tconn->req_lock);
-       wake_up(&tconn->ping_wait);
+       if (stop_threads) {
+               /* asender is implicitly stopped by receiver
+                * in conn_disconnect() */
+               drbd_thread_stop(&tconn->receiver);
+               drbd_thread_stop(&tconn->worker);
+       }
 }
 
 /* Make sure IO is suspended before calling this function(). */
@@ -1072,13 +1127,31 @@ static void drbd_suspend_al(struct drbd_conf *mdev)
                dev_info(DEV, "Suspended AL updates\n");
 }
 
+
+static bool should_set_defaults(struct genl_info *info)
+{
+       unsigned flags = ((struct drbd_genlmsghdr*)info->userhdr)->flags;
+       return 0 != (flags & DRBD_GENL_F_SET_DEFAULTS);
+}
+
+static void enforce_disk_conf_limits(struct disk_conf *dc)
+{
+       if (dc->al_extents < DRBD_AL_EXTENTS_MIN)
+               dc->al_extents = DRBD_AL_EXTENTS_MIN;
+       if (dc->al_extents > DRBD_AL_EXTENTS_MAX)
+               dc->al_extents = DRBD_AL_EXTENTS_MAX;
+
+       if (dc->c_plan_ahead > DRBD_C_PLAN_AHEAD_MAX)
+               dc->c_plan_ahead = DRBD_C_PLAN_AHEAD_MAX;
+}
+
 int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info)
 {
        enum drbd_ret_code retcode;
        struct drbd_conf *mdev;
-       struct disk_conf *ndc; /* new disk conf */
+       struct disk_conf *new_disk_conf, *old_disk_conf;
+       struct fifo_buffer *old_plan = NULL, *new_plan = NULL;
        int err, fifo_size;
-       int *rs_plan_s = NULL;
 
        retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
        if (!adm_ctx.reply_skb)
@@ -1095,86 +1168,88 @@ int drbd_adm_disk_opts(struct sk_buff *skb, struct genl_info *info)
                goto out;
        }
 
-/* FIXME freeze IO, cluster wide.
- *
- * We should make sure no-one uses
- * some half-updated struct when we
- * assign it later. */
-
-       ndc = kmalloc(sizeof(*ndc), GFP_KERNEL);
-       if (!ndc) {
+       new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
+       if (!new_disk_conf) {
                retcode = ERR_NOMEM;
                goto fail;
        }
 
-       memcpy(ndc, &mdev->ldev->dc, sizeof(*ndc));
-       err = disk_conf_from_attrs_for_change(ndc, info);
-       if (err) {
+       mutex_lock(&mdev->tconn->conf_update);
+       old_disk_conf = mdev->ldev->disk_conf;
+       *new_disk_conf = *old_disk_conf;
+       if (should_set_defaults(info))
+               set_disk_conf_defaults(new_disk_conf);
+
+       err = disk_conf_from_attrs_for_change(new_disk_conf, info);
+       if (err && err != -ENOMSG) {
                retcode = ERR_MANDATORY_TAG;
                drbd_msg_put_info(from_attrs_err_to_txt(err));
        }
 
-       if (!expect(ndc->resync_rate >= 1))
-               ndc->resync_rate = 1;
-
-       /* clip to allowed range */
-       if (!expect(ndc->al_extents >= DRBD_AL_EXTENTS_MIN))
-               ndc->al_extents = DRBD_AL_EXTENTS_MIN;
-       if (!expect(ndc->al_extents <= DRBD_AL_EXTENTS_MAX))
-               ndc->al_extents = DRBD_AL_EXTENTS_MAX;
+       if (!expect(new_disk_conf->resync_rate >= 1))
+               new_disk_conf->resync_rate = 1;
 
-       /* most sanity checks done, try to assign the new sync-after
-        * dependency.  need to hold the global lock in there,
-        * to avoid a race in the dependency loop check. */
-       retcode = drbd_alter_sa(mdev, ndc->resync_after);
-       if (retcode != NO_ERROR)
-               goto fail;
+       enforce_disk_conf_limits(new_disk_conf);
 
-       fifo_size = (ndc->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
-       if (fifo_size != mdev->rs_plan_s.size && fifo_size > 0) {
-               rs_plan_s   = kzalloc(sizeof(int) * fifo_size, GFP_KERNEL);
-               if (!rs_plan_s) {
+       fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
+       if (fifo_size != mdev->rs_plan_s->size) {
+               new_plan = fifo_alloc(fifo_size);
+               if (!new_plan) {
                        dev_err(DEV, "kmalloc of fifo_buffer failed");
                        retcode = ERR_NOMEM;
-                       goto fail;
+                       goto fail_unlock;
                }
        }
 
-       if (fifo_size != mdev->rs_plan_s.size) {
-               kfree(mdev->rs_plan_s.values);
-               mdev->rs_plan_s.values = rs_plan_s;
-               mdev->rs_plan_s.size   = fifo_size;
-               mdev->rs_planed = 0;
-               rs_plan_s = NULL;
-       }
-
        wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
        drbd_al_shrink(mdev);
-       err = drbd_check_al_size(mdev, ndc);
+       err = drbd_check_al_size(mdev, new_disk_conf);
        lc_unlock(mdev->act_log);
        wake_up(&mdev->al_wait);
 
        if (err) {
                retcode = ERR_NOMEM;
-               goto fail;
+               goto fail_unlock;
        }
 
-       /* FIXME
-        * To avoid someone looking at a half-updated struct, we probably
-        * should have a rw-semaphor on net_conf and disk_conf.
-        */
-       mdev->ldev->dc = *ndc;
+       write_lock_irq(&global_state_lock);
+       retcode = drbd_resync_after_valid(mdev, new_disk_conf->resync_after);
+       if (retcode == NO_ERROR) {
+               rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
+               drbd_resync_after_changed(mdev);
+       }
+       write_unlock_irq(&global_state_lock);
 
-       drbd_md_sync(mdev);
+       if (retcode != NO_ERROR)
+               goto fail_unlock;
 
+       if (new_plan) {
+               old_plan = mdev->rs_plan_s;
+               rcu_assign_pointer(mdev->rs_plan_s, new_plan);
+       }
+
+       mutex_unlock(&mdev->tconn->conf_update);
+
+       drbd_bump_write_ordering(mdev->tconn, WO_bdev_flush);
+
+       drbd_md_sync(mdev);
 
        if (mdev->state.conn >= C_CONNECTED)
                drbd_send_sync_param(mdev);
 
+       synchronize_rcu();
+       kfree(old_disk_conf);
+       kfree(old_plan);
+       mod_timer(&mdev->request_timer, jiffies + HZ);
+       goto success;
+
+fail_unlock:
+       mutex_unlock(&mdev->tconn->conf_update);
  fail:
+       kfree(new_disk_conf);
+       kfree(new_plan);
+success:
        put_ldev(mdev);
-       kfree(ndc);
-       kfree(rs_plan_s);
  out:
        drbd_adm_finish(info, retcode);
        return 0;
@@ -1189,11 +1264,13 @@ int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
        sector_t max_possible_sectors;
        sector_t min_md_device_sectors;
        struct drbd_backing_dev *nbc = NULL; /* new_backing_conf */
+       struct disk_conf *new_disk_conf = NULL;
        struct block_device *bdev;
        struct lru_cache *resync_lru = NULL;
+       struct fifo_buffer *new_plan = NULL;
        union drbd_state ns, os;
        enum drbd_state_rv rv;
-       int cp_discovered = 0;
+       struct net_conf *nc;
 
        retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
        if (!adm_ctx.reply_skb)
@@ -1221,54 +1298,49 @@ int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
                retcode = ERR_NOMEM;
                goto fail;
        }
+       new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
+       if (!new_disk_conf) {
+               retcode = ERR_NOMEM;
+               goto fail;
+       }
+       nbc->disk_conf = new_disk_conf;
 
-       nbc->dc = (struct disk_conf) {
-               {}, 0, /* backing_dev */
-               {}, 0, /* meta_dev */
-               0, /* meta_dev_idx */
-               DRBD_DISK_SIZE_SECT_DEF, /* disk_size */
-               DRBD_MAX_BIO_BVECS_DEF, /* max_bio_bvecs */
-               DRBD_ON_IO_ERROR_DEF, /* on_io_error */
-               DRBD_FENCING_DEF, /* fencing */
-               DRBD_RATE_DEF, /* resync_rate */
-               DRBD_AFTER_DEF, /* resync_after */
-               DRBD_AL_EXTENTS_DEF, /* al_extents */
-               DRBD_C_PLAN_AHEAD_DEF, /* c_plan_ahead */
-               DRBD_C_DELAY_TARGET_DEF, /* c_delay_target */
-               DRBD_C_FILL_TARGET_DEF, /* c_fill_target */
-               DRBD_C_MAX_RATE_DEF, /* c_max_rate */
-               DRBD_C_MIN_RATE_DEF, /* c_min_rate */
-               0, /* no_disk_barrier */
-               0, /* no_disk_flush */
-               0, /* no_disk_drain */
-               0, /* no_md_flush */
-       };
-
-       err = disk_conf_from_attrs(&nbc->dc, info);
+       set_disk_conf_defaults(new_disk_conf);
+       err = disk_conf_from_attrs(new_disk_conf, info);
        if (err) {
                retcode = ERR_MANDATORY_TAG;
                drbd_msg_put_info(from_attrs_err_to_txt(err));
                goto fail;
        }
 
-       if ((int)nbc->dc.meta_dev_idx < DRBD_MD_INDEX_FLEX_INT) {
+       enforce_disk_conf_limits(new_disk_conf);
+
+       new_plan = fifo_alloc((new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ);
+       if (!new_plan) {
+               retcode = ERR_NOMEM;
+               goto fail;
+       }
+
+       if (new_disk_conf->meta_dev_idx < DRBD_MD_INDEX_FLEX_INT) {
                retcode = ERR_MD_IDX_INVALID;
                goto fail;
        }
 
-       if (get_net_conf(mdev->tconn)) {
-               int prot = mdev->tconn->net_conf->wire_protocol;
-               put_net_conf(mdev->tconn);
-               if (nbc->dc.fencing == FP_STONITH && prot == DRBD_PROT_A) {
+       rcu_read_lock();
+       nc = rcu_dereference(mdev->tconn->net_conf);
+       if (nc) {
+               if (new_disk_conf->fencing == FP_STONITH && nc->wire_protocol == DRBD_PROT_A) {
+                       rcu_read_unlock();
                        retcode = ERR_STONITH_AND_PROT_A;
                        goto fail;
                }
        }
+       rcu_read_unlock();
 
-       bdev = blkdev_get_by_path(nbc->dc.backing_dev,
+       bdev = blkdev_get_by_path(new_disk_conf->backing_dev,
                                  FMODE_READ | FMODE_WRITE | FMODE_EXCL, mdev);
        if (IS_ERR(bdev)) {
-               dev_err(DEV, "open(\"%s\") failed with %ld\n", nbc->dc.backing_dev,
+               dev_err(DEV, "open(\"%s\") failed with %ld\n", new_disk_conf->backing_dev,
                        PTR_ERR(bdev));
                retcode = ERR_OPEN_DISK;
                goto fail;
@@ -1283,12 +1355,12 @@ int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
         * should check it for you already; but if you don't, or
         * someone fooled it, we need to double check here)
         */
-       bdev = blkdev_get_by_path(nbc->dc.meta_dev,
+       bdev = blkdev_get_by_path(new_disk_conf->meta_dev,
                                  FMODE_READ | FMODE_WRITE | FMODE_EXCL,
-                                 ((int)nbc->dc.meta_dev_idx < 0) ?
+                                 (new_disk_conf->meta_dev_idx < 0) ?
                                  (void *)mdev : (void *)drbd_m_holder);
        if (IS_ERR(bdev)) {
-               dev_err(DEV, "open(\"%s\") failed with %ld\n", nbc->dc.meta_dev,
+               dev_err(DEV, "open(\"%s\") failed with %ld\n", new_disk_conf->meta_dev,
                        PTR_ERR(bdev));
                retcode = ERR_OPEN_MD_DISK;
                goto fail;
@@ -1296,8 +1368,8 @@ int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
        nbc->md_bdev = bdev;
 
        if ((nbc->backing_bdev == nbc->md_bdev) !=
-           (nbc->dc.meta_dev_idx == DRBD_MD_INDEX_INTERNAL ||
-            nbc->dc.meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)) {
+           (new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_INTERNAL ||
+            new_disk_conf->meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)) {
                retcode = ERR_MD_IDX_INVALID;
                goto fail;
        }
@@ -1313,25 +1385,25 @@ int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
        /* RT - for drbd_get_max_capacity() DRBD_MD_INDEX_FLEX_INT */
        drbd_md_set_sector_offsets(mdev, nbc);
 
-       if (drbd_get_max_capacity(nbc) < nbc->dc.disk_size) {
+       if (drbd_get_max_capacity(nbc) < new_disk_conf->disk_size) {
                dev_err(DEV, "max capacity %llu smaller than disk size %llu\n",
                        (unsigned long long) drbd_get_max_capacity(nbc),
-                       (unsigned long long) nbc->dc.disk_size);
-               retcode = ERR_DISK_TO_SMALL;
+                       (unsigned long long) new_disk_conf->disk_size);
+               retcode = ERR_DISK_TOO_SMALL;
                goto fail;
        }
 
-       if ((int)nbc->dc.meta_dev_idx < 0) {
+       if (new_disk_conf->meta_dev_idx < 0) {
                max_possible_sectors = DRBD_MAX_SECTORS_FLEX;
                /* at least one MB, otherwise it does not make sense */
                min_md_device_sectors = (2<<10);
        } else {
                max_possible_sectors = DRBD_MAX_SECTORS;
-               min_md_device_sectors = MD_RESERVED_SECT * (nbc->dc.meta_dev_idx + 1);
+               min_md_device_sectors = MD_RESERVED_SECT * (new_disk_conf->meta_dev_idx + 1);
        }
 
        if (drbd_get_capacity(nbc->md_bdev) < min_md_device_sectors) {
-               retcode = ERR_MD_DISK_TO_SMALL;
+               retcode = ERR_MD_DISK_TOO_SMALL;
                dev_warn(DEV, "refusing attach: md-device too small, "
                     "at least %llu sectors needed for this meta-disk type\n",
                     (unsigned long long) min_md_device_sectors);
@@ -1342,7 +1414,7 @@ int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
         * (we may currently be R_PRIMARY with no local disk...) */
        if (drbd_get_max_capacity(nbc) <
            drbd_get_capacity(mdev->this_bdev)) {
-               retcode = ERR_DISK_TO_SMALL;
+               retcode = ERR_DISK_TOO_SMALL;
                goto fail;
        }
 
@@ -1352,7 +1424,7 @@ int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
                dev_warn(DEV, "==> truncating very big lower level device "
                        "to currently maximum possible %llu sectors <==\n",
                        (unsigned long long) max_possible_sectors);
-               if ((int)nbc->dc.meta_dev_idx >= 0)
+               if (new_disk_conf->meta_dev_idx >= 0)
                        dev_warn(DEV, "==>> using internal or flexible "
                                      "meta data may help <<==\n");
        }
@@ -1395,30 +1467,25 @@ int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
        }
 
        /* Since we are diskless, fix the activity log first... */
-       if (drbd_check_al_size(mdev, &nbc->dc)) {
+       if (drbd_check_al_size(mdev, new_disk_conf)) {
                retcode = ERR_NOMEM;
                goto force_diskless_dec;
        }
 
        /* Prevent shrinking of consistent devices ! */
        if (drbd_md_test_flag(nbc, MDF_CONSISTENT) &&
-           drbd_new_dev_size(mdev, nbc, 0) < nbc->md.la_size_sect) {
+           drbd_new_dev_size(mdev, nbc, nbc->disk_conf->disk_size, 0) < nbc->md.la_size_sect) {
                dev_warn(DEV, "refusing to truncate a consistent device\n");
-               retcode = ERR_DISK_TO_SMALL;
-               goto force_diskless_dec;
-       }
-
-       if (!drbd_al_read_log(mdev, nbc)) {
-               retcode = ERR_IO_MD_DISK;
+               retcode = ERR_DISK_TOO_SMALL;
                goto force_diskless_dec;
        }
 
        /* Reset the "barriers don't work" bits here, then force meta data to
         * be written, to ensure we determine if barriers are supported. */
-       if (nbc->dc.no_md_flush)
-               set_bit(MD_NO_FUA, &mdev->flags);
-       else
+       if (new_disk_conf->md_flushes)
                clear_bit(MD_NO_FUA, &mdev->flags);
+       else
+               set_bit(MD_NO_FUA, &mdev->flags);
 
        /* Point of no return reached.
         * Devices and memory are no longer released by error cleanup below.
@@ -1427,11 +1494,13 @@ int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
        D_ASSERT(mdev->ldev == NULL);
        mdev->ldev = nbc;
        mdev->resync = resync_lru;
+       mdev->rs_plan_s = new_plan;
        nbc = NULL;
        resync_lru = NULL;
+       new_disk_conf = NULL;
+       new_plan = NULL;
 
-       mdev->write_ordering = WO_bdev_flush;
-       drbd_bump_write_ordering(mdev, WO_bdev_flush);
+       drbd_bump_write_ordering(mdev->tconn, WO_bdev_flush);
 
        if (drbd_md_test_flag(mdev->ldev, MDF_CRASHED_PRIMARY))
                set_bit(CRASHED_PRIMARY, &mdev->flags);
@@ -1439,10 +1508,8 @@ int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
                clear_bit(CRASHED_PRIMARY, &mdev->flags);
 
        if (drbd_md_test_flag(mdev->ldev, MDF_PRIMARY_IND) &&
-           !(mdev->state.role == R_PRIMARY && mdev->tconn->susp_nod)) {
+           !(mdev->state.role == R_PRIMARY && mdev->tconn->susp_nod))
                set_bit(CRASHED_PRIMARY, &mdev->flags);
-               cp_discovered = 1;
-       }
 
        mdev->send_cnt = 0;
        mdev->recv_cnt = 0;
@@ -1494,15 +1561,6 @@ int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
                }
        }
 
-       if (cp_discovered) {
-               drbd_al_apply_to_bm(mdev);
-               if (drbd_bitmap_io(mdev, &drbd_bm_write,
-                       "crashed primary apply AL", BM_LOCKED_MASK)) {
-                       retcode = ERR_IO_MD_DISK;
-                       goto force_diskless_dec;
-               }
-       }
-
        if (_drbd_bm_total_weight(mdev) == drbd_bm_bits(mdev))
                drbd_suspend_al(mdev); /* IO is still suspended here... */
 
@@ -1526,9 +1584,11 @@ int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
        if (drbd_md_test_flag(mdev->ldev, MDF_PEER_OUT_DATED))
                ns.pdsk = D_OUTDATED;
 
-       if ( ns.disk == D_CONSISTENT &&
-           (ns.pdsk == D_OUTDATED || mdev->ldev->dc.fencing == FP_DONT_CARE))
+       rcu_read_lock();
+       if (ns.disk == D_CONSISTENT &&
+           (ns.pdsk == D_OUTDATED || rcu_dereference(mdev->ldev->disk_conf)->fencing == FP_DONT_CARE))
                ns.disk = D_UP_TO_DATE;
+       rcu_read_unlock();
 
        /* All tests on MDF_PRIMARY_IND, MDF_CONNECTED_IND,
           MDF_CONSISTENT and MDF_WAS_UP_TO_DATE must happen before
@@ -1555,6 +1615,8 @@ int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
        if (rv < SS_SUCCESS)
                goto force_diskless_dec;
 
+       mod_timer(&mdev->request_timer, jiffies + HZ);
+
        if (mdev->state.role == R_PRIMARY)
                mdev->ldev->md.uuid[UI_CURRENT] |=  (u64)1;
        else
@@ -1572,7 +1634,7 @@ int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
  force_diskless_dec:
        put_ldev(mdev);
  force_diskless:
-       drbd_force_state(mdev, NS(disk, D_FAILED));
+       drbd_force_state(mdev, NS(disk, D_DISKLESS));
        drbd_md_sync(mdev);
  fail:
        conn_reconfig_done(mdev->tconn);
@@ -1585,22 +1647,39 @@ int drbd_adm_attach(struct sk_buff *skb, struct genl_info *info)
                                   FMODE_READ | FMODE_WRITE | FMODE_EXCL);
                kfree(nbc);
        }
+       kfree(new_disk_conf);
        lc_destroy(resync_lru);
+       kfree(new_plan);
 
  finish:
        drbd_adm_finish(info, retcode);
        return 0;
 }
 
-static int adm_detach(struct drbd_conf *mdev)
+static int adm_detach(struct drbd_conf *mdev, int force)
 {
        enum drbd_state_rv retcode;
+       int ret;
+
+       if (force) {
+               drbd_force_state(mdev, NS(disk, D_FAILED));
+               retcode = SS_SUCCESS;
+               goto out;
+       }
+
        drbd_suspend_io(mdev); /* so no-one is stuck in drbd_al_begin_io */
-       retcode = drbd_request_state(mdev, NS(disk, D_DISKLESS));
-       wait_event(mdev->misc_wait,
-                       mdev->state.disk != D_DISKLESS ||
-                       !atomic_read(&mdev->local_cnt));
+       drbd_md_get_buffer(mdev); /* make sure there is no in-flight meta-data IO */
+       retcode = drbd_request_state(mdev, NS(disk, D_FAILED));
+       drbd_md_put_buffer(mdev);
+       /* D_FAILED will transition to DISKLESS. */
+       ret = wait_event_interruptible(mdev->misc_wait,
+                       mdev->state.disk != D_FAILED);
        drbd_resume_io(mdev);
+       if ((int)retcode == (int)SS_IS_DISKLESS)
+               retcode = SS_NOTHING_TO_DO;
+       if (ret)
+               retcode = ERR_INTR;
+out:
        return retcode;
 }
 
@@ -1612,6 +1691,8 @@ static int adm_detach(struct drbd_conf *mdev)
 int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info)
 {
        enum drbd_ret_code retcode;
+       struct detach_parms parms = { };
+       int err;
 
        retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
        if (!adm_ctx.reply_skb)
@@ -1619,7 +1700,16 @@ int drbd_adm_detach(struct sk_buff *skb, struct genl_info *info)
        if (retcode != NO_ERROR)
                goto out;
 
-       retcode = adm_detach(adm_ctx.mdev);
+       if (info->attrs[DRBD_NLA_DETACH_PARMS]) {
+               err = detach_parms_from_attrs(&parms, info);
+               if (err) {
+                       retcode = ERR_MANDATORY_TAG;
+                       drbd_msg_put_info(from_attrs_err_to_txt(err));
+                       goto out;
+               }
+       }
+
+       retcode = adm_detach(adm_ctx.mdev, parms.force_detach);
 out:
        drbd_adm_finish(info, retcode);
        return 0;
@@ -1666,62 +1756,143 @@ static bool conn_ov_running(struct drbd_tconn *tconn)
 }
 
 static enum drbd_ret_code
-check_net_options(struct drbd_tconn *tconn, struct net_conf *new_conf)
+_check_net_options(struct drbd_tconn *tconn, struct net_conf *old_conf, struct net_conf *new_conf)
 {
        struct drbd_conf *mdev;
        int i;
 
-       if (tconn->net_conf && tconn->agreed_pro_version < 100 &&
-           tconn->cstate == C_WF_REPORT_PARAMS &&
-           new_conf->wire_protocol != tconn->net_conf->wire_protocol)
-               return ERR_NEED_APV_100;
+       if (old_conf && tconn->cstate == C_WF_REPORT_PARAMS && tconn->agreed_pro_version < 100) {
+               if (new_conf->wire_protocol != old_conf->wire_protocol)
+                       return ERR_NEED_APV_100;
+
+               if (new_conf->two_primaries != old_conf->two_primaries)
+                       return ERR_NEED_APV_100;
+
+               if (!new_conf->integrity_alg != !old_conf->integrity_alg)
+                       return ERR_NEED_APV_100;
+
+               if (strcmp(new_conf->integrity_alg, old_conf->integrity_alg))
+                       return ERR_NEED_APV_100;
+       }
+
+       if (!new_conf->two_primaries &&
+           conn_highest_role(tconn) == R_PRIMARY &&
+           conn_highest_peer(tconn) == R_PRIMARY)
+               return ERR_NEED_ALLOW_TWO_PRI;
 
        if (new_conf->two_primaries &&
            (new_conf->wire_protocol != DRBD_PROT_C))
                return ERR_NOT_PROTO_C;
 
-       rcu_read_lock();
        idr_for_each_entry(&tconn->volumes, mdev, i) {
                if (get_ldev(mdev)) {
-                       enum drbd_fencing_p fp = mdev->ldev->dc.fencing;
+                       enum drbd_fencing_p fp = rcu_dereference(mdev->ldev->disk_conf)->fencing;
                        put_ldev(mdev);
-                       if (new_conf->wire_protocol == DRBD_PROT_A && fp == FP_STONITH) {
-                               rcu_read_unlock();
+                       if (new_conf->wire_protocol == DRBD_PROT_A && fp == FP_STONITH)
                                return ERR_STONITH_AND_PROT_A;
-                       }
                }
-               if (mdev->state.role == R_PRIMARY && new_conf->want_lose) {
-                       rcu_read_unlock();
+               if (mdev->state.role == R_PRIMARY && new_conf->discard_my_data)
                        return ERR_DISCARD;
-               }
+       }
+
+       if (new_conf->on_congestion != OC_BLOCK && new_conf->wire_protocol != DRBD_PROT_A)
+               return ERR_CONG_NOT_PROTO_A;
+
+       return NO_ERROR;
+}
+
+static enum drbd_ret_code
+check_net_options(struct drbd_tconn *tconn, struct net_conf *new_conf)
+{
+       static enum drbd_ret_code rv;
+       struct drbd_conf *mdev;
+       int i;
+
+       rcu_read_lock();
+       rv = _check_net_options(tconn, rcu_dereference(tconn->net_conf), new_conf);
+       rcu_read_unlock();
+
+       /* tconn->volumes protected by genl_lock() here */
+       idr_for_each_entry(&tconn->volumes, mdev, i) {
                if (!mdev->bitmap) {
-                       if(drbd_bm_init(mdev)) {
-                               rcu_read_unlock();
+                       if(drbd_bm_init(mdev))
                                return ERR_NOMEM;
-                       }
                }
        }
-       rcu_read_unlock();
 
-       if (new_conf->on_congestion != OC_BLOCK && new_conf->wire_protocol != DRBD_PROT_A)
-               return ERR_CONG_NOT_PROTO_A;
+       return rv;
+}
+
+struct crypto {
+       struct crypto_hash *verify_tfm;
+       struct crypto_hash *csums_tfm;
+       struct crypto_hash *cram_hmac_tfm;
+       struct crypto_hash *integrity_tfm;
+};
+
+static int
+alloc_hash(struct crypto_hash **tfm, char *tfm_name, int err_alg)
+{
+       if (!tfm_name[0])
+               return NO_ERROR;
+
+       *tfm = crypto_alloc_hash(tfm_name, 0, CRYPTO_ALG_ASYNC);
+       if (IS_ERR(*tfm)) {
+               *tfm = NULL;
+               return err_alg;
+       }
 
        return NO_ERROR;
 }
 
+static enum drbd_ret_code
+alloc_crypto(struct crypto *crypto, struct net_conf *new_conf)
+{
+       char hmac_name[CRYPTO_MAX_ALG_NAME];
+       enum drbd_ret_code rv;
+
+       rv = alloc_hash(&crypto->csums_tfm, new_conf->csums_alg,
+                      ERR_CSUMS_ALG);
+       if (rv != NO_ERROR)
+               return rv;
+       rv = alloc_hash(&crypto->verify_tfm, new_conf->verify_alg,
+                      ERR_VERIFY_ALG);
+       if (rv != NO_ERROR)
+               return rv;
+       rv = alloc_hash(&crypto->integrity_tfm, new_conf->integrity_alg,
+                      ERR_INTEGRITY_ALG);
+       if (rv != NO_ERROR)
+               return rv;
+       if (new_conf->cram_hmac_alg[0] != 0) {
+               snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)",
+                        new_conf->cram_hmac_alg);
+
+               rv = alloc_hash(&crypto->cram_hmac_tfm, hmac_name,
+                              ERR_AUTH_ALG);
+       }
+
+       return rv;
+}
+
+static void free_crypto(struct crypto *crypto)
+{
+       crypto_free_hash(crypto->cram_hmac_tfm);
+       crypto_free_hash(crypto->integrity_tfm);
+       crypto_free_hash(crypto->csums_tfm);
+       crypto_free_hash(crypto->verify_tfm);
+}
+
 int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info)
 {
        enum drbd_ret_code retcode;
        struct drbd_tconn *tconn;
-       struct net_conf *new_conf = NULL;
+       struct net_conf *old_conf, *new_conf = NULL;
        int err;
        int ovr; /* online verify running */
        int rsr; /* re-sync running */
-       struct crypto_hash *verify_tfm = NULL;
-       struct crypto_hash *csums_tfm = NULL;
-
+       struct crypto crypto = { };
 
-       retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONN);
+       retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONNECTION);
        if (!adm_ctx.reply_skb)
                return retcode;
        if (retcode != NO_ERROR)
@@ -1735,19 +1906,24 @@ int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info)
                goto out;
        }
 
-       /* we also need a net config
-        * to change the options on */
-       if (!get_net_conf(tconn)) {
+       conn_reconfig_start(tconn);
+
+       mutex_lock(&tconn->data.mutex);
+       mutex_lock(&tconn->conf_update);
+       old_conf = tconn->net_conf;
+
+       if (!old_conf) {
                drbd_msg_put_info("net conf missing, try connect");
                retcode = ERR_INVALID_REQUEST;
-               goto out;
+               goto fail;
        }
 
-       conn_reconfig_start(tconn);
+       *new_conf = *old_conf;
+       if (should_set_defaults(info))
+               set_net_conf_defaults(new_conf);
 
-       memcpy(new_conf, tconn->net_conf, sizeof(*new_conf));
        err = net_conf_from_attrs_for_change(new_conf, info);
-       if (err) {
+       if (err && err != -ENOMSG) {
                retcode = ERR_MANDATORY_TAG;
                drbd_msg_put_info(from_attrs_err_to_txt(err));
                goto fail;
@@ -1759,78 +1935,60 @@ int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info)
 
        /* re-sync running */
        rsr = conn_resync_running(tconn);
-       if (rsr && strcmp(new_conf->csums_alg, tconn->net_conf->csums_alg)) {
+       if (rsr && strcmp(new_conf->csums_alg, old_conf->csums_alg)) {
                retcode = ERR_CSUMS_RESYNC_RUNNING;
                goto fail;
        }
 
-       if (!rsr && new_conf->csums_alg[0]) {
-               csums_tfm = crypto_alloc_hash(new_conf->csums_alg, 0, CRYPTO_ALG_ASYNC);
-               if (IS_ERR(csums_tfm)) {
-                       csums_tfm = NULL;
-                       retcode = ERR_CSUMS_ALG;
-                       goto fail;
-               }
-
-               if (!drbd_crypto_is_hash(crypto_hash_tfm(csums_tfm))) {
-                       retcode = ERR_CSUMS_ALG_ND;
-                       goto fail;
-               }
-       }
-
        /* online verify running */
        ovr = conn_ov_running(tconn);
-       if (ovr) {
-               if (strcmp(new_conf->verify_alg, tconn->net_conf->verify_alg)) {
-                       retcode = ERR_VERIFY_RUNNING;
-                       goto fail;
-               }
-       }
-
-       if (!ovr && new_conf->verify_alg[0]) {
-               verify_tfm = crypto_alloc_hash(new_conf->verify_alg, 0, CRYPTO_ALG_ASYNC);
-               if (IS_ERR(verify_tfm)) {
-                       verify_tfm = NULL;
-                       retcode = ERR_VERIFY_ALG;
-                       goto fail;
-               }
-
-               if (!drbd_crypto_is_hash(crypto_hash_tfm(verify_tfm))) {
-                       retcode = ERR_VERIFY_ALG_ND;
-                       goto fail;
-               }
+       if (ovr && strcmp(new_conf->verify_alg, old_conf->verify_alg)) {
+               retcode = ERR_VERIFY_RUNNING;
+               goto fail;
        }
 
+       retcode = alloc_crypto(&crypto, new_conf);
+       if (retcode != NO_ERROR)
+               goto fail;
 
-       /* For now, use struct assignment, not pointer assignment.
-        * We don't have any means to determine who might still
-        * keep a local alias into the struct,
-        * so we cannot just free it and hope for the best :(
-        * FIXME
-        * To avoid someone looking at a half-updated struct, we probably
-        * should have a rw-semaphor on net_conf and disk_conf.
-        */
-       *tconn->net_conf = *new_conf;
+       rcu_assign_pointer(tconn->net_conf, new_conf);
 
        if (!rsr) {
                crypto_free_hash(tconn->csums_tfm);
-               tconn->csums_tfm = csums_tfm;
-               csums_tfm = NULL;
+               tconn->csums_tfm = crypto.csums_tfm;
+               crypto.csums_tfm = NULL;
        }
        if (!ovr) {
                crypto_free_hash(tconn->verify_tfm);
-               tconn->verify_tfm = verify_tfm;
-               verify_tfm = NULL;
+               tconn->verify_tfm = crypto.verify_tfm;
+               crypto.verify_tfm = NULL;
        }
 
+       crypto_free_hash(tconn->integrity_tfm);
+       tconn->integrity_tfm = crypto.integrity_tfm;
+       if (tconn->cstate >= C_WF_REPORT_PARAMS && tconn->agreed_pro_version >= 100)
+               /* Do this without trying to take tconn->data.mutex again.  */
+               __drbd_send_protocol(tconn, P_PROTOCOL_UPDATE);
+
+       crypto_free_hash(tconn->cram_hmac_tfm);
+       tconn->cram_hmac_tfm = crypto.cram_hmac_tfm;
+
+       mutex_unlock(&tconn->conf_update);
+       mutex_unlock(&tconn->data.mutex);
+       synchronize_rcu();
+       kfree(old_conf);
+
        if (tconn->cstate >= C_WF_REPORT_PARAMS)
                drbd_send_sync_param(minor_to_mdev(conn_lowest_minor(tconn)));
 
+       goto done;
+
  fail:
-       crypto_free_hash(csums_tfm);
-       crypto_free_hash(verify_tfm);
+       mutex_unlock(&tconn->conf_update);
+       mutex_unlock(&tconn->data.mutex);
+       free_crypto(&crypto);
        kfree(new_conf);
-       put_net_conf(tconn);
+ done:
        conn_reconfig_done(tconn);
  out:
        drbd_adm_finish(info, retcode);
@@ -1839,26 +1997,42 @@ int drbd_adm_net_opts(struct sk_buff *skb, struct genl_info *info)
 
 int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info)
 {
-       char hmac_name[CRYPTO_MAX_ALG_NAME];
        struct drbd_conf *mdev;
-       struct net_conf *new_conf = NULL;
-       struct crypto_hash *tfm = NULL;
-       struct crypto_hash *integrity_w_tfm = NULL;
-       struct crypto_hash *integrity_r_tfm = NULL;
-       void *int_dig_in = NULL;
-       void *int_dig_vv = NULL;
-       struct drbd_tconn *oconn;
+       struct net_conf *old_conf, *new_conf = NULL;
+       struct crypto crypto = { };
        struct drbd_tconn *tconn;
-       struct sockaddr *new_my_addr, *new_peer_addr, *taken_addr;
        enum drbd_ret_code retcode;
        int i;
        int err;
 
-       retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONN);
+       retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
+
        if (!adm_ctx.reply_skb)
                return retcode;
        if (retcode != NO_ERROR)
                goto out;
+       if (!(adm_ctx.my_addr && adm_ctx.peer_addr)) {
+               drbd_msg_put_info("connection endpoint(s) missing");
+               retcode = ERR_INVALID_REQUEST;
+               goto out;
+       }
+
+       /* No need for _rcu here. All reconfiguration is
+        * strictly serialized on genl_lock(). We are protected against
+        * concurrent reconfiguration/addition/deletion */
+       list_for_each_entry(tconn, &drbd_tconns, all_tconn) {
+               if (nla_len(adm_ctx.my_addr) == tconn->my_addr_len &&
+                   !memcmp(nla_data(adm_ctx.my_addr), &tconn->my_addr, tconn->my_addr_len)) {
+                       retcode = ERR_LOCAL_ADDR;
+                       goto out;
+               }
+
+               if (nla_len(adm_ctx.peer_addr) == tconn->peer_addr_len &&
+                   !memcmp(nla_data(adm_ctx.peer_addr), &tconn->peer_addr, tconn->peer_addr_len)) {
+                       retcode = ERR_PEER_ADDR;
+                       goto out;
+               }
+       }
 
        tconn = adm_ctx.tconn;
        conn_reconfig_start(tconn);
@@ -1868,49 +2042,17 @@ int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info)
                goto fail;
        }
 
-       /* allocation not in the IO path, cqueue thread context */
-       new_conf = kmalloc(sizeof(struct net_conf), GFP_KERNEL);
+       /* allocation not in the IO path, drbdsetup / netlink process context */
+       new_conf = kzalloc(sizeof(*new_conf), GFP_KERNEL);
        if (!new_conf) {
                retcode = ERR_NOMEM;
                goto fail;
        }
 
-       *new_conf = (struct net_conf) {
-               {}, 0, /* my_addr */
-               {}, 0, /* peer_addr */
-               {}, 0, /* shared_secret */
-               {}, 0, /* cram_hmac_alg */
-               {}, 0, /* integrity_alg */
-               {}, 0, /* verify_alg */
-               {}, 0, /* csums_alg */
-               DRBD_PROTOCOL_DEF, /* wire_protocol */
-               DRBD_CONNECT_INT_DEF, /* try_connect_int */
-               DRBD_TIMEOUT_DEF, /* timeout */
-               DRBD_PING_INT_DEF, /* ping_int */
-               DRBD_PING_TIMEO_DEF, /* ping_timeo */
-               DRBD_SNDBUF_SIZE_DEF, /* sndbuf_size */
-               DRBD_RCVBUF_SIZE_DEF, /* rcvbuf_size */
-               DRBD_KO_COUNT_DEF, /* ko_count */
-               DRBD_MAX_BUFFERS_DEF, /* max_buffers */
-               DRBD_MAX_EPOCH_SIZE_DEF, /* max_epoch_size */
-               DRBD_UNPLUG_WATERMARK_DEF, /* unplug_watermark */
-               DRBD_AFTER_SB_0P_DEF, /* after_sb_0p */
-               DRBD_AFTER_SB_1P_DEF, /* after_sb_1p */
-               DRBD_AFTER_SB_2P_DEF, /* after_sb_2p */
-               DRBD_RR_CONFLICT_DEF, /* rr_conflict */
-               DRBD_ON_CONGESTION_DEF, /* on_congestion */
-               DRBD_CONG_FILL_DEF, /* cong_fill */
-               DRBD_CONG_EXTENTS_DEF, /* cong_extents */
-               0, /* two_primaries */
-               0, /* want_lose */
-               0, /* no_cork */
-               0, /* always_asbp */
-               0, /* dry_run */
-               0, /* use_rle */
-       };
+       set_net_conf_defaults(new_conf);
 
        err = net_conf_from_attrs(new_conf, info);
-       if (err) {
+       if (err && err != -ENOMSG) {
                retcode = ERR_MANDATORY_TAG;
                drbd_msg_put_info(from_attrs_err_to_txt(err));
                goto fail;
@@ -1920,130 +2062,51 @@ int drbd_adm_connect(struct sk_buff *skb, struct genl_info *info)
        if (retcode != NO_ERROR)
                goto fail;
 
-       retcode = NO_ERROR;
-
-       new_my_addr = (struct sockaddr *)&new_conf->my_addr;
-       new_peer_addr = (struct sockaddr *)&new_conf->peer_addr;
-
-       /* No need to take drbd_cfg_rwsem here.  All reconfiguration is
-        * strictly serialized on genl_lock(). We are protected against
-        * concurrent reconfiguration/addition/deletion */
-       list_for_each_entry(oconn, &drbd_tconns, all_tconn) {
-               if (oconn == tconn)
-                       continue;
-               if (get_net_conf(oconn)) {
-                       taken_addr = (struct sockaddr *)&oconn->net_conf->my_addr;
-                       if (new_conf->my_addr_len == oconn->net_conf->my_addr_len &&
-                           !memcmp(new_my_addr, taken_addr, new_conf->my_addr_len))
-                               retcode = ERR_LOCAL_ADDR;
-
-                       taken_addr = (struct sockaddr *)&oconn->net_conf->peer_addr;
-                       if (new_conf->peer_addr_len == oconn->net_conf->peer_addr_len &&
-                           !memcmp(new_peer_addr, taken_addr, new_conf->peer_addr_len))
-                               retcode = ERR_PEER_ADDR;
-
-                       put_net_conf(oconn);
-                       if (retcode != NO_ERROR)
-                               goto fail;
-               }
-       }
-
-       if (new_conf->cram_hmac_alg[0] != 0) {
-               snprintf(hmac_name, CRYPTO_MAX_ALG_NAME, "hmac(%s)",
-                       new_conf->cram_hmac_alg);
-               tfm = crypto_alloc_hash(hmac_name, 0, CRYPTO_ALG_ASYNC);
-               if (IS_ERR(tfm)) {
-                       tfm = NULL;
-                       retcode = ERR_AUTH_ALG;
-                       goto fail;
-               }
-
-               if (!drbd_crypto_is_hash(crypto_hash_tfm(tfm))) {
-                       retcode = ERR_AUTH_ALG_ND;
-                       goto fail;
-               }
-       }
-
-       if (new_conf->integrity_alg[0]) {
-               integrity_w_tfm = crypto_alloc_hash(new_conf->integrity_alg, 0, CRYPTO_ALG_ASYNC);
-               if (IS_ERR(integrity_w_tfm)) {
-                       integrity_w_tfm = NULL;
-                       retcode=ERR_INTEGRITY_ALG;
-                       goto fail;
-               }
-
-               if (!drbd_crypto_is_hash(crypto_hash_tfm(integrity_w_tfm))) {
-                       retcode=ERR_INTEGRITY_ALG_ND;
-                       goto fail;
-               }
-
-               integrity_r_tfm = crypto_alloc_hash(new_conf->integrity_alg, 0, CRYPTO_ALG_ASYNC);
-               if (IS_ERR(integrity_r_tfm)) {
-                       integrity_r_tfm = NULL;
-                       retcode=ERR_INTEGRITY_ALG;
-                       goto fail;
-               }
-       }
+       retcode = alloc_crypto(&crypto, new_conf);
+       if (retcode != NO_ERROR)
+               goto fail;
 
        ((char *)new_conf->shared_secret)[SHARED_SECRET_MAX-1] = 0;
 
-       /* allocation not in the IO path, cqueue thread context */
-       if (integrity_w_tfm) {
-               i = crypto_hash_digestsize(integrity_w_tfm);
-               int_dig_in = kmalloc(i, GFP_KERNEL);
-               if (!int_dig_in) {
-                       retcode = ERR_NOMEM;
-                       goto fail;
-               }
-               int_dig_vv = kmalloc(i, GFP_KERNEL);
-               if (!int_dig_vv) {
-                       retcode = ERR_NOMEM;
-                       goto fail;
-               }
-       }
-
        conn_flush_workqueue(tconn);
-       spin_lock_irq(&tconn->req_lock);
-       if (tconn->net_conf != NULL) {
+
+       mutex_lock(&tconn->conf_update);
+       old_conf = tconn->net_conf;
+       if (old_conf) {
                retcode = ERR_NET_CONFIGURED;
-               spin_unlock_irq(&tconn->req_lock);
+               mutex_unlock(&tconn->conf_update);
                goto fail;
        }
-       tconn->net_conf = new_conf;
-
-       crypto_free_hash(tconn->cram_hmac_tfm);
-       tconn->cram_hmac_tfm = tfm;
+       rcu_assign_pointer(tconn->net_conf, new_conf);
 
-       crypto_free_hash(tconn->integrity_w_tfm);
-       tconn->integrity_w_tfm = integrity_w_tfm;
+       conn_free_crypto(tconn);
+       tconn->cram_hmac_tfm = crypto.cram_hmac_tfm;
+       tconn->integrity_tfm = crypto.integrity_tfm;
+       tconn->csums_tfm = crypto.csums_tfm;
+       tconn->verify_tfm = crypto.verify_tfm;
 
-       crypto_free_hash(tconn->integrity_r_tfm);
-       tconn->integrity_r_tfm = integrity_r_tfm;
+       tconn->my_addr_len = nla_len(adm_ctx.my_addr);
+       memcpy(&tconn->my_addr, nla_data(adm_ctx.my_addr), tconn->my_addr_len);
+       tconn->peer_addr_len = nla_len(adm_ctx.peer_addr);
+       memcpy(&tconn->peer_addr, nla_data(adm_ctx.peer_addr), tconn->peer_addr_len);
 
-       kfree(tconn->int_dig_in);
-       kfree(tconn->int_dig_vv);
-       tconn->int_dig_in=int_dig_in;
-       tconn->int_dig_vv=int_dig_vv;
-       retcode = _conn_request_state(tconn, NS(conn, C_UNCONNECTED), CS_VERBOSE);
-       spin_unlock_irq(&tconn->req_lock);
+       mutex_unlock(&tconn->conf_update);
 
        rcu_read_lock();
        idr_for_each_entry(&tconn->volumes, mdev, i) {
                mdev->send_cnt = 0;
                mdev->recv_cnt = 0;
-               kobject_uevent(&disk_to_dev(mdev->vdisk)->kobj, KOBJ_CHANGE);
        }
        rcu_read_unlock();
+
+       retcode = conn_request_state(tconn, NS(conn, C_UNCONNECTED), CS_VERBOSE);
+
        conn_reconfig_done(tconn);
        drbd_adm_finish(info, retcode);
        return 0;
 
 fail:
-       kfree(int_dig_in);
-       kfree(int_dig_vv);
-       crypto_free_hash(tfm);
-       crypto_free_hash(integrity_w_tfm);
-       crypto_free_hash(integrity_r_tfm);
+       free_crypto(&crypto);
        kfree(new_conf);
 
        conn_reconfig_done(tconn);
@@ -2055,38 +2118,54 @@ out:
 static enum drbd_state_rv conn_try_disconnect(struct drbd_tconn *tconn, bool force)
 {
        enum drbd_state_rv rv;
-       if (force) {
-               spin_lock_irq(&tconn->req_lock);
-               if (tconn->cstate >= C_WF_CONNECTION)
-                       _conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
-               spin_unlock_irq(&tconn->req_lock);
-               return SS_SUCCESS;
-       }
 
-       rv = conn_request_state(tconn, NS(conn, C_DISCONNECTING), 0);
+       rv = conn_request_state(tconn, NS(conn, C_DISCONNECTING),
+                       force ? CS_HARD : 0);
 
        switch (rv) {
        case SS_NOTHING_TO_DO:
+               break;
        case SS_ALREADY_STANDALONE:
                return SS_SUCCESS;
        case SS_PRIMARY_NOP:
                /* Our state checking code wants to see the peer outdated. */
                rv = conn_request_state(tconn, NS2(conn, C_DISCONNECTING,
-                                                       pdsk, D_OUTDATED), CS_VERBOSE);
+                                               pdsk, D_OUTDATED), CS_VERBOSE);
                break;
        case SS_CW_FAILED_BY_PEER:
                /* The peer probably wants to see us outdated. */
                rv = conn_request_state(tconn, NS2(conn, C_DISCONNECTING,
                                                        disk, D_OUTDATED), 0);
                if (rv == SS_IS_DISKLESS || rv == SS_LOWER_THAN_OUTDATED) {
-                       conn_request_state(tconn, NS(conn, C_DISCONNECTING), CS_HARD);
-                       rv = SS_SUCCESS;
+                       rv = conn_request_state(tconn, NS(conn, C_DISCONNECTING),
+                                       CS_HARD);
                }
                break;
        default:;
                /* no special handling necessary */
        }
 
+       if (rv >= SS_SUCCESS) {
+               enum drbd_state_rv rv2;
+               /* No one else can reconfigure the network while I am here.
+                * The state handling only uses drbd_thread_stop_nowait(),
+                * we want to really wait here until the receiver is no more.
+                */
+               drbd_thread_stop(&adm_ctx.tconn->receiver);
+
+               /* Race breaker.  This additional state change request may be
+                * necessary, if this was a forced disconnect during a receiver
+                * restart.  We may have "killed" the receiver thread just
+                * after drbdd_init() returned.  Typically, we should be
+                * C_STANDALONE already, now, and this becomes a no-op.
+                */
+               rv2 = conn_request_state(tconn, NS(conn, C_STANDALONE),
+                               CS_VERBOSE | CS_HARD);
+               if (rv2 < SS_SUCCESS)
+                       conn_err(tconn,
+                               "unexpected rv2=%d in conn_try_disconnect()\n",
+                               rv2);
+       }
        return rv;
 }
 
@@ -2098,7 +2177,7 @@ int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info)
        enum drbd_ret_code retcode;
        int err;
 
-       retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONN);
+       retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONNECTION);
        if (!adm_ctx.reply_skb)
                return retcode;
        if (retcode != NO_ERROR)
@@ -2117,17 +2196,9 @@ int drbd_adm_disconnect(struct sk_buff *skb, struct genl_info *info)
 
        rv = conn_try_disconnect(tconn, parms.force_disconnect);
        if (rv < SS_SUCCESS)
-               goto fail;
-
-       if (wait_event_interruptible(tconn->ping_wait,
-                                    tconn->cstate != C_DISCONNECTING)) {
-               /* Do not test for mdev->state.conn == C_STANDALONE, since
-                  someone else might connect us in the mean time! */
-               retcode = ERR_INTR;
-               goto fail;
-       }
-
-       retcode = NO_ERROR;
+               retcode = rv;  /* FIXME: Type mismatch. */
+       else
+               retcode = NO_ERROR;
  fail:
        drbd_adm_finish(info, retcode);
        return 0;
@@ -2151,11 +2222,13 @@ void resync_after_online_grow(struct drbd_conf *mdev)
 
 int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info)
 {
+       struct disk_conf *old_disk_conf, *new_disk_conf = NULL;
        struct resize_parms rs;
        struct drbd_conf *mdev;
        enum drbd_ret_code retcode;
        enum determine_dev_size dd;
        enum dds_flags ddsf;
+       sector_t u_size;
        int err;
 
        retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
@@ -2193,13 +2266,34 @@ int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info)
 
        if (rs.no_resync && mdev->tconn->agreed_pro_version < 93) {
                retcode = ERR_NEED_APV_93;
-               goto fail;
+               goto fail_ldev;
+       }
+
+       rcu_read_lock();
+       u_size = rcu_dereference(mdev->ldev->disk_conf)->disk_size;
+       rcu_read_unlock();
+       if (u_size != (sector_t)rs.resize_size) {
+               new_disk_conf = kmalloc(sizeof(struct disk_conf), GFP_KERNEL);
+               if (!new_disk_conf) {
+                       retcode = ERR_NOMEM;
+                       goto fail_ldev;
+               }
        }
 
        if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev))
                mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
 
-       mdev->ldev->dc.disk_size = (sector_t)rs.resize_size;
+       if (new_disk_conf) {
+               mutex_lock(&mdev->tconn->conf_update);
+               old_disk_conf = mdev->ldev->disk_conf;
+               *new_disk_conf = *old_disk_conf;
+               new_disk_conf->disk_size = (sector_t)rs.resize_size;
+               rcu_assign_pointer(mdev->ldev->disk_conf, new_disk_conf);
+               mutex_unlock(&mdev->tconn->conf_update);
+               synchronize_rcu();
+               kfree(old_disk_conf);
+       }
+
        ddsf = (rs.resize_force ? DDSF_FORCED : 0) | (rs.no_resync ? DDSF_NO_RESYNC : 0);
        dd = drbd_determine_dev_size(mdev, ddsf);
        drbd_md_sync(mdev);
@@ -2220,70 +2314,45 @@ int drbd_adm_resize(struct sk_buff *skb, struct genl_info *info)
  fail:
        drbd_adm_finish(info, retcode);
        return 0;
+
+ fail_ldev:
+       put_ldev(mdev);
+       goto fail;
 }
 
 int drbd_adm_resource_opts(struct sk_buff *skb, struct genl_info *info)
 {
        enum drbd_ret_code retcode;
-       cpumask_var_t new_cpu_mask;
        struct drbd_tconn *tconn;
-       int *rs_plan_s = NULL;
-       struct res_opts sc;
+       struct res_opts res_opts;
        int err;
 
-       retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONN);
+       retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
        if (!adm_ctx.reply_skb)
                return retcode;
        if (retcode != NO_ERROR)
                goto fail;
        tconn = adm_ctx.tconn;
 
-       if (!zalloc_cpumask_var(&new_cpu_mask, GFP_KERNEL)) {
-               retcode = ERR_NOMEM;
-               drbd_msg_put_info("unable to allocate cpumask");
-               goto fail;
-       }
+       res_opts = tconn->res_opts;
+       if (should_set_defaults(info))
+               set_res_opts_defaults(&res_opts);
 
-       if (((struct drbd_genlmsghdr*)info->userhdr)->flags
-                       & DRBD_GENL_F_SET_DEFAULTS) {
-               memset(&sc, 0, sizeof(struct res_opts));
-               sc.on_no_data  = DRBD_ON_NO_DATA_DEF;
-       } else
-               sc = tconn->res_opts;
-
-       err = res_opts_from_attrs(&sc, info);
-       if (err) {
+       err = res_opts_from_attrs(&res_opts, info);
+       if (err && err != -ENOMSG) {
                retcode = ERR_MANDATORY_TAG;
                drbd_msg_put_info(from_attrs_err_to_txt(err));
                goto fail;
        }
 
-       /* silently ignore cpu mask on UP kernel */
-       if (nr_cpu_ids > 1 && sc.cpu_mask[0] != 0) {
-               err = __bitmap_parse(sc.cpu_mask, 32, 0,
-                               cpumask_bits(new_cpu_mask), nr_cpu_ids);
-               if (err) {
-                       conn_warn(tconn, "__bitmap_parse() failed with %d\n", err);
-                       retcode = ERR_CPU_MASK_PARSE;
-                       goto fail;
-               }
-       }
-
-
-       tconn->res_opts = sc;
-
-       if (!cpumask_equal(tconn->cpu_mask, new_cpu_mask)) {
-               cpumask_copy(tconn->cpu_mask, new_cpu_mask);
-               drbd_calc_cpu_mask(tconn);
-               tconn->receiver.reset_cpu_mask = 1;
-               tconn->asender.reset_cpu_mask = 1;
-               tconn->worker.reset_cpu_mask = 1;
+       err = set_resource_options(tconn, &res_opts);
+       if (err) {
+               retcode = ERR_INVALID_REQUEST;
+               if (err == -ENOMEM)
+                       retcode = ERR_NOMEM;
        }
 
 fail:
-       kfree(rs_plan_s);
-       free_cpumask_var(new_cpu_mask);
-
        drbd_adm_finish(info, retcode);
        return 0;
 }
@@ -2327,6 +2396,23 @@ out:
        return 0;
 }
 
+static int drbd_adm_simple_request_state(struct sk_buff *skb, struct genl_info *info,
+               union drbd_state mask, union drbd_state val)
+{
+       enum drbd_ret_code retcode;
+
+       retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
+       if (!adm_ctx.reply_skb)
+               return retcode;
+       if (retcode != NO_ERROR)
+               goto out;
+
+       retcode = drbd_request_state(adm_ctx.mdev, mask, val);
+out:
+       drbd_adm_finish(info, retcode);
+       return 0;
+}
+
 static int drbd_bmio_set_susp_al(struct drbd_conf *mdev)
 {
        int rv;
@@ -2336,10 +2422,10 @@ static int drbd_bmio_set_susp_al(struct drbd_conf *mdev)
        return rv;
 }
 
-static int drbd_adm_simple_request_state(struct sk_buff *skb, struct genl_info *info,
-               union drbd_state mask, union drbd_state val)
+int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info)
 {
-       enum drbd_ret_code retcode;
+       int retcode; /* drbd_ret_code, drbd_state_rv */
+       struct drbd_conf *mdev;
 
        retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_MINOR);
        if (!adm_ctx.reply_skb)
@@ -2347,17 +2433,29 @@ static int drbd_adm_simple_request_state(struct sk_buff *skb, struct genl_info *
        if (retcode != NO_ERROR)
                goto out;
 
-       retcode = drbd_request_state(adm_ctx.mdev, mask, val);
+       mdev = adm_ctx.mdev;
+
+       retcode = _drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_S), CS_ORDERED);
+       if (retcode < SS_SUCCESS) {
+               if (retcode == SS_NEED_CONNECTION && mdev->state.role == R_PRIMARY) {
+                       /* The peer will get a resync upon connect anyways.
+                        * Just make that into a full resync. */
+                       retcode = drbd_request_state(mdev, NS(pdsk, D_INCONSISTENT));
+                       if (retcode >= SS_SUCCESS) {
+                               if (drbd_bitmap_io(mdev, &drbd_bmio_set_susp_al,
+                                                  "set_n_write from invalidate_peer",
+                                                  BM_LOCKED_SET_ALLOWED))
+                                       retcode = ERR_IO_MD_DISK;
+                       }
+               } else
+                       retcode = drbd_request_state(mdev, NS(conn, C_STARTING_SYNC_S));
+       }
+
 out:
        drbd_adm_finish(info, retcode);
        return 0;
 }
 
-int drbd_adm_invalidate_peer(struct sk_buff *skb, struct genl_info *info)
-{
-       return drbd_adm_simple_request_state(skb, info, NS(conn, C_STARTING_SYNC_S));
-}
-
 int drbd_adm_pause_sync(struct sk_buff *skb, struct genl_info *info)
 {
        enum drbd_ret_code retcode;
@@ -2442,7 +2540,7 @@ int drbd_adm_outdate(struct sk_buff *skb, struct genl_info *info)
        return drbd_adm_simple_request_state(skb, info, NS(disk, D_OUTDATED));
 }
 
-int nla_put_drbd_cfg_context(struct sk_buff *skb, const char *conn_name, unsigned vnr)
+int nla_put_drbd_cfg_context(struct sk_buff *skb, struct drbd_tconn *tconn, unsigned vnr)
 {
        struct nlattr *nla;
        nla = nla_nest_start(skb, DRBD_NLA_CFG_CONTEXT);
@@ -2450,7 +2548,11 @@ int nla_put_drbd_cfg_context(struct sk_buff *skb, const char *conn_name, unsigne
                goto nla_put_failure;
        if (vnr != VOLUME_UNSPECIFIED)
                NLA_PUT_U32(skb, T_ctx_volume, vnr);
-       NLA_PUT_STRING(skb, T_ctx_conn_name, conn_name);
+       NLA_PUT_STRING(skb, T_ctx_resource_name, tconn->name);
+       if (tconn->my_addr_len)
+               NLA_PUT(skb, T_ctx_my_addr, tconn->my_addr_len, &tconn->my_addr);
+       if (tconn->peer_addr_len)
+               NLA_PUT(skb, T_ctx_peer_addr, tconn->peer_addr_len, &tconn->peer_addr);
        nla_nest_end(skb, nla);
        return 0;
 
@@ -2464,9 +2566,9 @@ int nla_put_status_info(struct sk_buff *skb, struct drbd_conf *mdev,
                const struct sib_info *sib)
 {
        struct state_info *si = NULL; /* for sizeof(si->member); */
+       struct net_conf *nc;
        struct nlattr *nla;
        int got_ldev;
-       int got_net;
        int err = 0;
        int exclude_sensitive;
 
@@ -2484,23 +2586,27 @@ int nla_put_status_info(struct sk_buff *skb, struct drbd_conf *mdev,
        exclude_sensitive = sib || !capable(CAP_SYS_ADMIN);
 
        got_ldev = get_ldev(mdev);
-       got_net = get_net_conf(mdev->tconn);
 
        /* We need to add connection name and volume number information still.
         * Minor number is in drbd_genlmsghdr. */
-       if (nla_put_drbd_cfg_context(skb, mdev->tconn->name, mdev->vnr))
+       if (nla_put_drbd_cfg_context(skb, mdev->tconn, mdev->vnr))
                goto nla_put_failure;
 
        if (res_opts_to_skb(skb, &mdev->tconn->res_opts, exclude_sensitive))
                goto nla_put_failure;
 
+       rcu_read_lock();
        if (got_ldev)
-               if (disk_conf_to_skb(skb, &mdev->ldev->dc, exclude_sensitive))
-                       goto nla_put_failure;
-       if (got_net)
-               if (net_conf_to_skb(skb, mdev->tconn->net_conf, exclude_sensitive))
+               if (disk_conf_to_skb(skb, rcu_dereference(mdev->ldev->disk_conf), exclude_sensitive))
                        goto nla_put_failure;
 
+       nc = rcu_dereference(mdev->tconn->net_conf);
+       if (nc)
+               err = net_conf_to_skb(skb, nc, exclude_sensitive);
+       rcu_read_unlock();
+       if (err)
+               goto nla_put_failure;
+
        nla = nla_nest_start(skb, DRBD_NLA_STATE_INFO);
        if (!nla)
                goto nla_put_failure;
@@ -2546,8 +2652,6 @@ nla_put_failure:
                err = -EMSGSIZE;
        if (got_ldev)
                put_ldev(mdev);
-       if (got_net)
-               put_net_conf(mdev->tconn);
        return err;
 }
 
@@ -2572,7 +2676,7 @@ out:
        return 0;
 }
 
-int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb)
+int get_one_status(struct sk_buff *skb, struct netlink_callback *cb)
 {
        struct drbd_conf *mdev;
        struct drbd_genlmsghdr *dh;
@@ -2590,6 +2694,9 @@ int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb)
         * where tconn is cb->args[0];
         * and i is cb->args[1];
         *
+        * cb->args[2] indicates if we shall loop over all resources,
+        * or just dump all volumes of a single resource.
+        *
         * This may miss entries inserted after this dump started,
         * or entries deleted before they are reached.
         *
@@ -2598,11 +2705,10 @@ int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb)
         * on each iteration.
         */
 
-       /* synchronize with drbd_new_tconn/drbd_free_tconn */
-       down_read(&drbd_cfg_rwsem);
-next_tconn:
+       /* synchronize with conn_create()/conn_destroy() */
+       rcu_read_lock();
        /* revalidate iterator position */
-       list_for_each_entry(tmp, &drbd_tconns, all_tconn) {
+       list_for_each_entry_rcu(tmp, &drbd_tconns, all_tconn) {
                if (pos == NULL) {
                        /* first iteration */
                        pos = tmp;
@@ -2615,16 +2721,22 @@ next_tconn:
                }
        }
        if (tconn) {
+next_tconn:
                mdev = idr_get_next(&tconn->volumes, &volume);
                if (!mdev) {
                        /* No more volumes to dump on this tconn.
                         * Advance tconn iterator. */
-                       pos = list_entry(tconn->all_tconn.next,
-                                       struct drbd_tconn, all_tconn);
-                       /* But, did we dump any volume on this tconn yet? */
+                       pos = list_entry_rcu(tconn->all_tconn.next,
+                                            struct drbd_tconn, all_tconn);
+                       /* Did we dump any volume on this tconn yet? */
                        if (volume != 0) {
-                               tconn = NULL;
+                               /* If we reached the end of the list,
+                                * or only a single resource dump was requested,
+                                * we are done. */
+                               if (&pos->all_tconn == &drbd_tconns || cb->args[2])
+                                       goto out;
                                volume = 0;
+                               tconn = pos;
                                goto next_tconn;
                        }
                }
@@ -2636,14 +2748,18 @@ next_tconn:
                        goto out;
 
                if (!mdev) {
-                       /* this is a tconn without a single volume */
+                       /* This is a tconn without a single volume.
+                        * Suprisingly enough, it may have a network
+                        * configuration. */
+                       struct net_conf *nc;
                        dh->minor = -1U;
                        dh->ret_code = NO_ERROR;
-                       if (nla_put_drbd_cfg_context(skb, tconn->name, VOLUME_UNSPECIFIED))
-                               genlmsg_cancel(skb, dh);
-                       else
-                               genlmsg_end(skb, dh);
-                       goto out;
+                       if (nla_put_drbd_cfg_context(skb, tconn, VOLUME_UNSPECIFIED))
+                               goto cancel;
+                       nc = rcu_dereference(tconn->net_conf);
+                       if (nc && net_conf_to_skb(skb, nc, 1) != 0)
+                               goto cancel;
+                       goto done;
                }
 
                D_ASSERT(mdev->vnr == volume);
@@ -2653,14 +2769,16 @@ next_tconn:
                dh->ret_code = NO_ERROR;
 
                if (nla_put_status_info(skb, mdev, NULL)) {
+cancel:
                        genlmsg_cancel(skb, dh);
                        goto out;
                }
+done:
                genlmsg_end(skb, dh);
         }
 
 out:
-       up_read(&drbd_cfg_rwsem);
+       rcu_read_unlock();
        /* where to start the next iteration */
         cb->args[0] = (long)pos;
         cb->args[1] = (pos == tconn) ? volume + 1 : 0;
@@ -2670,6 +2788,67 @@ out:
         return skb->len;
 }
 
+/*
+ * Request status of all resources, or of all volumes within a single resource.
+ *
+ * This is a dump, as the answer may not fit in a single reply skb otherwise.
+ * Which means we cannot use the family->attrbuf or other such members, because
+ * dump is NOT protected by the genl_lock().  During dump, we only have access
+ * to the incoming skb, and need to opencode "parsing" of the nlattr payload.
+ *
+ * Once things are setup properly, we call into get_one_status().
+ */
+int drbd_adm_get_status_all(struct sk_buff *skb, struct netlink_callback *cb)
+{
+       const unsigned hdrlen = GENL_HDRLEN + GENL_MAGIC_FAMILY_HDRSZ;
+       struct nlattr *nla;
+       const char *resource_name;
+       struct drbd_tconn *tconn;
+       int maxtype;
+
+       /* Is this a followup call? */
+       if (cb->args[0]) {
+               /* ... of a single resource dump,
+                * and the resource iterator has been advanced already? */
+               if (cb->args[2] && cb->args[2] != cb->args[0])
+                       return 0; /* DONE. */
+               goto dump;
+       }
+
+       /* First call (from netlink_dump_start).  We need to figure out
+        * which resource(s) the user wants us to dump. */
+       nla = nla_find(nlmsg_attrdata(cb->nlh, hdrlen),
+                       nlmsg_attrlen(cb->nlh, hdrlen),
+                       DRBD_NLA_CFG_CONTEXT);
+
+       /* No explicit context given.  Dump all. */
+       if (!nla)
+               goto dump;
+       maxtype = ARRAY_SIZE(drbd_cfg_context_nl_policy) - 1;
+       nla = drbd_nla_find_nested(maxtype, nla, __nla_type(T_ctx_resource_name));
+       if (IS_ERR(nla))
+               return PTR_ERR(nla);
+       /* context given, but no name present? */
+       if (!nla)
+               return -EINVAL;
+       resource_name = nla_data(nla);
+       tconn = conn_get_by_name(resource_name);
+
+       if (!tconn)
+               return -ENODEV;
+
+       kref_put(&tconn->kref, &conn_destroy); /* get_one_status() (re)validates tconn by itself */
+
+       /* prime iterators, and set "filter" mode mark:
+        * only dump this tconn. */
+       cb->args[0] = (long)tconn;
+       /* cb->args[1] = 0; passed in this way. */
+       cb->args[2] = (long)tconn;
+
+dump:
+       return get_one_status(skb, cb);
+}
+
 int drbd_adm_get_timeout_type(struct sk_buff *skb, struct genl_info *info)
 {
        enum drbd_ret_code retcode;
@@ -2806,24 +2985,26 @@ out_nolock:
 }
 
 static enum drbd_ret_code
-drbd_check_conn_name(const char *name)
+drbd_check_resource_name(const char *name)
 {
        if (!name || !name[0]) {
-               drbd_msg_put_info("connection name missing");
+               drbd_msg_put_info("resource name missing");
                return ERR_MANDATORY_TAG;
        }
        /* if we want to use these in sysfs/configfs/debugfs some day,
         * we must not allow slashes */
        if (strchr(name, '/')) {
-               drbd_msg_put_info("invalid connection name");
+               drbd_msg_put_info("invalid resource name");
                return ERR_INVALID_REQUEST;
        }
        return NO_ERROR;
 }
 
-int drbd_adm_create_connection(struct sk_buff *skb, struct genl_info *info)
+int drbd_adm_new_resource(struct sk_buff *skb, struct genl_info *info)
 {
        enum drbd_ret_code retcode;
+       struct res_opts res_opts;
+       int err;
 
        retcode = drbd_adm_prepare(skb, info, 0);
        if (!adm_ctx.reply_skb)
@@ -2831,20 +3012,28 @@ int drbd_adm_create_connection(struct sk_buff *skb, struct genl_info *info)
        if (retcode != NO_ERROR)
                goto out;
 
-       retcode = drbd_check_conn_name(adm_ctx.conn_name);
+       set_res_opts_defaults(&res_opts);
+       err = res_opts_from_attrs(&res_opts, info);
+       if (err && err != -ENOMSG) {
+               retcode = ERR_MANDATORY_TAG;
+               drbd_msg_put_info(from_attrs_err_to_txt(err));
+               goto out;
+       }
+
+       retcode = drbd_check_resource_name(adm_ctx.resource_name);
        if (retcode != NO_ERROR)
                goto out;
 
        if (adm_ctx.tconn) {
                if (info->nlhdr->nlmsg_flags & NLM_F_EXCL) {
                        retcode = ERR_INVALID_REQUEST;
-                       drbd_msg_put_info("connection exists");
+                       drbd_msg_put_info("resource exists");
                }
                /* else: still NO_ERROR */
                goto out;
        }
 
-       if (!drbd_new_tconn(adm_ctx.conn_name))
+       if (!conn_create(adm_ctx.resource_name, &res_opts))
                retcode = ERR_NOMEM;
 out:
        drbd_adm_finish(info, retcode);
@@ -2856,14 +3045,13 @@ int drbd_adm_add_minor(struct sk_buff *skb, struct genl_info *info)
        struct drbd_genlmsghdr *dh = info->userhdr;
        enum drbd_ret_code retcode;
 
-       retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONN);
+       retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
        if (!adm_ctx.reply_skb)
                return retcode;
        if (retcode != NO_ERROR)
                goto out;
 
-       /* FIXME drop minor_count parameter, limit to MINORMASK */
-       if (dh->minor >= minor_count) {
+       if (dh->minor > MINORMASK) {
                drbd_msg_put_info("requested minor out of range");
                retcode = ERR_INVALID_REQUEST;
                goto out;
@@ -2883,9 +3071,7 @@ int drbd_adm_add_minor(struct sk_buff *skb, struct genl_info *info)
                goto out;
        }
 
-       down_write(&drbd_cfg_rwsem);
        retcode = conn_new_minor(adm_ctx.tconn, dh->minor, adm_ctx.volume);
-       up_write(&drbd_cfg_rwsem);
 out:
        drbd_adm_finish(info, retcode);
        return 0;
@@ -2898,7 +3084,13 @@ static enum drbd_ret_code adm_delete_minor(struct drbd_conf *mdev)
             * we may want to delete a minor from a live replication group.
             */
            mdev->state.role == R_SECONDARY) {
-               drbd_delete_device(mdev);
+               _drbd_request_state(mdev, NS(conn, C_WF_REPORT_PARAMS),
+                                   CS_VERBOSE + CS_WAIT_COMPLETE);
+               idr_remove(&mdev->tconn->volumes, mdev->vnr);
+               idr_remove(&minors, mdev_to_minor(mdev));
+               del_gendisk(mdev->vdisk);
+               synchronize_rcu();
+               kref_put(&mdev->kref, &drbd_minor_destroy);
                return NO_ERROR;
        } else
                return ERR_MINOR_CONFIGURED;
@@ -2914,13 +3106,7 @@ int drbd_adm_delete_minor(struct sk_buff *skb, struct genl_info *info)
        if (retcode != NO_ERROR)
                goto out;
 
-       down_write(&drbd_cfg_rwsem);
        retcode = adm_delete_minor(adm_ctx.mdev);
-       up_write(&drbd_cfg_rwsem);
-       /* if this was the last volume of this connection,
-        * this will terminate all threads */
-       if (retcode == NO_ERROR)
-               conn_reconfig_done(adm_ctx.tconn);
 out:
        drbd_adm_finish(info, retcode);
        return 0;
@@ -2928,8 +3114,7 @@ out:
 
 int drbd_adm_down(struct sk_buff *skb, struct genl_info *info)
 {
-       enum drbd_ret_code retcode;
-       enum drbd_state_rv rv;
+       int retcode; /* enum drbd_ret_code rsp. enum drbd_state_rv */
        struct drbd_conf *mdev;
        unsigned i;
 
@@ -2940,92 +3125,91 @@ int drbd_adm_down(struct sk_buff *skb, struct genl_info *info)
                goto out;
 
        if (!adm_ctx.tconn) {
-               retcode = ERR_CONN_NOT_KNOWN;
+               retcode = ERR_RES_NOT_KNOWN;
                goto out;
        }
 
-       down_read(&drbd_cfg_rwsem);
        /* demote */
        idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
                retcode = drbd_set_role(mdev, R_SECONDARY, 0);
                if (retcode < SS_SUCCESS) {
                        drbd_msg_put_info("failed to demote");
-                       goto out_unlock;
+                       goto out;
                }
        }
 
-       /* disconnect */
-       rv = conn_try_disconnect(adm_ctx.tconn, 0);
-       if (rv < SS_SUCCESS) {
-               retcode = rv; /* enum type mismatch! */
+       retcode = conn_try_disconnect(adm_ctx.tconn, 0);
+       if (retcode < SS_SUCCESS) {
                drbd_msg_put_info("failed to disconnect");
-               goto out_unlock;
+               goto out;
        }
 
        /* detach */
        idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
-               rv = adm_detach(mdev);
-               if (rv < SS_SUCCESS) {
-                       retcode = rv; /* enum type mismatch! */
+               retcode = adm_detach(mdev, 0);
+               if (retcode < SS_SUCCESS) {
                        drbd_msg_put_info("failed to detach");
-                       goto out_unlock;
+                       goto out;
                }
        }
-       up_read(&drbd_cfg_rwsem);
+
+       /* If we reach this, all volumes (of this tconn) are Secondary,
+        * Disconnected, Diskless, aka Unconfigured. Make sure all threads have
+        * actually stopped, state handling only does drbd_thread_stop_nowait(). */
+       drbd_thread_stop(&adm_ctx.tconn->worker);
+
+       /* Now, nothing can fail anymore */
 
        /* delete volumes */
-       down_write(&drbd_cfg_rwsem);
        idr_for_each_entry(&adm_ctx.tconn->volumes, mdev, i) {
                retcode = adm_delete_minor(mdev);
                if (retcode != NO_ERROR) {
                        /* "can not happen" */
                        drbd_msg_put_info("failed to delete volume");
-                       up_write(&drbd_cfg_rwsem);
                        goto out;
                }
        }
 
-       /* stop all threads */
-       conn_reconfig_done(adm_ctx.tconn);
-
        /* delete connection */
        if (conn_lowest_minor(adm_ctx.tconn) < 0) {
-               drbd_free_tconn(adm_ctx.tconn);
+               list_del_rcu(&adm_ctx.tconn->all_tconn);
+               synchronize_rcu();
+               kref_put(&adm_ctx.tconn->kref, &conn_destroy);
+
                retcode = NO_ERROR;
        } else {
                /* "can not happen" */
-               retcode = ERR_CONN_IN_USE;
+               retcode = ERR_RES_IN_USE;
                drbd_msg_put_info("failed to delete connection");
        }
-
-       up_write(&drbd_cfg_rwsem);
        goto out;
-out_unlock:
-       up_read(&drbd_cfg_rwsem);
 out:
        drbd_adm_finish(info, retcode);
        return 0;
 }
 
-int drbd_adm_delete_connection(struct sk_buff *skb, struct genl_info *info)
+int drbd_adm_del_resource(struct sk_buff *skb, struct genl_info *info)
 {
        enum drbd_ret_code retcode;
 
-       retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_CONN);
+       retcode = drbd_adm_prepare(skb, info, DRBD_ADM_NEED_RESOURCE);
        if (!adm_ctx.reply_skb)
                return retcode;
        if (retcode != NO_ERROR)
                goto out;
 
-       down_write(&drbd_cfg_rwsem);
        if (conn_lowest_minor(adm_ctx.tconn) < 0) {
-               drbd_free_tconn(adm_ctx.tconn);
+               list_del_rcu(&adm_ctx.tconn->all_tconn);
+               synchronize_rcu();
+               kref_put(&adm_ctx.tconn->kref, &conn_destroy);
+
                retcode = NO_ERROR;
        } else {
-               retcode = ERR_CONN_IN_USE;
+               retcode = ERR_RES_IN_USE;
        }
-       up_write(&drbd_cfg_rwsem);
 
+       if (retcode == NO_ERROR)
+               drbd_thread_stop(&adm_ctx.tconn->worker);
 out:
        drbd_adm_finish(info, retcode);
        return 0;
@@ -3049,7 +3233,7 @@ void drbd_bcast_event(struct drbd_conf *mdev, const struct sib_info *sib)
        if (!d_out) /* cannot happen, but anyways. */
                goto nla_put_failure;
        d_out->minor = mdev_to_minor(mdev);
-       d_out->ret_code = 0;
+       d_out->ret_code = NO_ERROR;
 
        if (nla_put_status_info(msg, mdev, sib))
                goto nla_put_failure;