Merge branch 'for-3.8/drivers' of git://git.kernel.dk/linux-block
authorLinus Torvalds <torvalds@linux-foundation.org>
Mon, 17 Dec 2012 21:39:11 +0000 (13:39 -0800)
committerLinus Torvalds <torvalds@linux-foundation.org>
Mon, 17 Dec 2012 21:39:11 +0000 (13:39 -0800)
Pull block driver update from Jens Axboe:
 "Now that the core bits are in, here are the driver bits for 3.8.  The
  branch contains:

   - A huge pile of drbd bits that were dumped from the 3.7 merge
     window.  Following that, it was both made perfectly clear that
     there is going to be no more over-the-wall pulls and how the
     situation on individual pulls can be improved.

   - A few cleanups from Akinobu Mita for drbd and cciss.

   - Queue improvement for loop from Lukas.  This grew into adding a
     generic interface for waiting/checking an even with a specific
     lock, allowing this to be pulled out of md and now loop and drbd is
     also using it.

   - A few fixes for xen back/front block driver from Roger Pau Monne.

   - Partition improvements from Stephen Warren, allowing partiion UUID
     to be used as an identifier."

* 'for-3.8/drivers' of git://git.kernel.dk/linux-block: (609 commits)
  drbd: update Kconfig to match current dependencies
  drbd: Fix drbdsetup wait-connect, wait-sync etc... commands
  drbd: close race between drbd_set_role and drbd_connect
  drbd: respect no-md-barriers setting also when changed online via disk-options
  drbd: Remove obsolete check
  drbd: fixup after wait_even_lock_irq() addition to generic code
  loop: Limit the number of requests in the bio list
  wait: add wait_event_lock_irq() interface
  xen-blkfront: free allocated page
  xen-blkback: move free persistent grants code
  block: partition: msdos: provide UUIDs for partitions
  init: reduce PARTUUID min length to 1 from 36
  block: store partition_meta_info.uuid as a string
  cciss: use check_signature()
  cciss: cleanup bitops usage
  drbd: use copy_highpage
  drbd: if the replication link breaks during handshake, keep retrying
  drbd: check return of kmalloc in receive_uuids
  drbd: Broadcast sync progress no more often than once per second
  drbd: don't try to clear bits once the disk has failed
  ...

49 files changed:
block/genhd.c
block/partitions/efi.c
block/partitions/msdos.c
drivers/block/cciss.c
drivers/block/drbd/Kconfig
drivers/block/drbd/Makefile
drivers/block/drbd/drbd_actlog.c
drivers/block/drbd/drbd_bitmap.c
drivers/block/drbd/drbd_int.h
drivers/block/drbd/drbd_interval.c [new file with mode: 0644]
drivers/block/drbd/drbd_interval.h [new file with mode: 0644]
drivers/block/drbd/drbd_main.c
drivers/block/drbd/drbd_nl.c
drivers/block/drbd/drbd_nla.c [new file with mode: 0644]
drivers/block/drbd/drbd_nla.h [new file with mode: 0644]
drivers/block/drbd/drbd_proc.c
drivers/block/drbd/drbd_receiver.c
drivers/block/drbd/drbd_req.c
drivers/block/drbd/drbd_req.h
drivers/block/drbd/drbd_state.c [new file with mode: 0644]
drivers/block/drbd/drbd_state.h [new file with mode: 0644]
drivers/block/drbd/drbd_strings.c
drivers/block/drbd/drbd_worker.c
drivers/block/drbd/drbd_wrappers.h
drivers/block/loop.c
drivers/block/xen-blkback/blkback.c
drivers/block/xen-blkback/common.h
drivers/block/xen-blkback/xenbus.c
drivers/block/xen-blkfront.c
drivers/md/md.c
drivers/md/md.h
drivers/md/raid1.c
drivers/md/raid10.c
drivers/md/raid5.c
include/linux/drbd.h
include/linux/drbd_genl.h [new file with mode: 0644]
include/linux/drbd_genl_api.h [new file with mode: 0644]
include/linux/drbd_limits.h
include/linux/drbd_nl.h [deleted file]
include/linux/drbd_tag_magic.h [deleted file]
include/linux/genhd.h
include/linux/genl_magic_func.h [new file with mode: 0644]
include/linux/genl_magic_struct.h [new file with mode: 0644]
include/linux/idr.h
include/linux/loop.h
include/linux/lru_cache.h
include/linux/wait.h
init/do_mounts.c
lib/lru_cache.c

index 2a6fdf539a6969d952cddf865f00d7a225d5ea23..9a289d7c84bbedc6ca2e17cf74c201acc43fe7cd 100644 (file)
@@ -743,7 +743,6 @@ void __init printk_all_partitions(void)
                struct hd_struct *part;
                char name_buf[BDEVNAME_SIZE];
                char devt_buf[BDEVT_SIZE];
-               char uuid_buf[PARTITION_META_INFO_UUIDLTH * 2 + 5];
 
                /*
                 * Don't show empty devices or things that have been
@@ -762,16 +761,11 @@ void __init printk_all_partitions(void)
                while ((part = disk_part_iter_next(&piter))) {
                        bool is_part0 = part == &disk->part0;
 
-                       uuid_buf[0] = '\0';
-                       if (part->info)
-                               snprintf(uuid_buf, sizeof(uuid_buf), "%pU",
-                                        part->info->uuid);
-
                        printk("%s%s %10llu %s %s", is_part0 ? "" : "  ",
                               bdevt_str(part_devt(part), devt_buf),
                               (unsigned long long)part_nr_sects_read(part) >> 1
                               , disk_name(disk, part->partno, name_buf),
-                              uuid_buf);
+                              part->info ? part->info->uuid : "");
                        if (is_part0) {
                                if (disk->driverfs_dev != NULL &&
                                    disk->driverfs_dev->driver != NULL)
index 6296b403c67a3d5ca512b05e048ecf3525330153..b62fb88b87118956e81a30884c086b30956ac61d 100644 (file)
@@ -620,7 +620,6 @@ int efi_partition(struct parsed_partitions *state)
        gpt_entry *ptes = NULL;
        u32 i;
        unsigned ssz = bdev_logical_block_size(state->bdev) / 512;
-       u8 unparsed_guid[37];
 
        if (!find_valid_gpt(state, &gpt, &ptes) || !gpt || !ptes) {
                kfree(gpt);
@@ -649,11 +648,7 @@ int efi_partition(struct parsed_partitions *state)
                        state->parts[i + 1].flags = ADDPART_FLAG_RAID;
 
                info = &state->parts[i + 1].info;
-               /* Instead of doing a manual swap to big endian, reuse the
-                * common ASCII hex format as the interim.
-                */
-               efi_guid_unparse(&ptes[i].unique_partition_guid, unparsed_guid);
-               part_pack_uuid(unparsed_guid, info->uuid);
+               efi_guid_unparse(&ptes[i].unique_partition_guid, info->uuid);
 
                /* Naively convert UTF16-LE to 7 bits. */
                label_max = min(sizeof(info->volname) - 1,
index 5f79a6677c69706cee48844f2babd988fb5e26a8..8752a5d265653a5a743c333c9c99bb259aca554b 100644 (file)
@@ -94,6 +94,17 @@ static int aix_magic_present(struct parsed_partitions *state, unsigned char *p)
        return ret;
 }
 
+static void set_info(struct parsed_partitions *state, int slot,
+                    u32 disksig)
+{
+       struct partition_meta_info *info = &state->parts[slot].info;
+
+       snprintf(info->uuid, sizeof(info->uuid), "%08x-%02x", disksig,
+                slot);
+       info->volname[0] = 0;
+       state->parts[slot].has_info = true;
+}
+
 /*
  * Create devices for each logical partition in an extended partition.
  * The logical partitions form a linked list, with each entry being
@@ -106,7 +117,8 @@ static int aix_magic_present(struct parsed_partitions *state, unsigned char *p)
  */
 
 static void parse_extended(struct parsed_partitions *state,
-                          sector_t first_sector, sector_t first_size)
+                          sector_t first_sector, sector_t first_size,
+                          u32 disksig)
 {
        struct partition *p;
        Sector sect;
@@ -166,6 +178,7 @@ static void parse_extended(struct parsed_partitions *state,
                        }
 
                        put_partition(state, state->next, next, size);
+                       set_info(state, state->next, disksig);
                        if (SYS_IND(p) == LINUX_RAID_PARTITION)
                                state->parts[state->next].flags = ADDPART_FLAG_RAID;
                        loopct = 0;
@@ -437,6 +450,7 @@ int msdos_partition(struct parsed_partitions *state)
        struct partition *p;
        struct fat_boot_sector *fb;
        int slot;
+       u32 disksig;
 
        data = read_part_sector(state, 0, &sect);
        if (!data)
@@ -491,6 +505,8 @@ int msdos_partition(struct parsed_partitions *state)
 #endif
        p = (struct partition *) (data + 0x1be);
 
+       disksig = le32_to_cpup((__le32 *)(data + 0x1b8));
+
        /*
         * Look for partitions in two passes:
         * First find the primary and DOS-type extended partitions.
@@ -515,11 +531,12 @@ int msdos_partition(struct parsed_partitions *state)
                        put_partition(state, slot, start, n);
 
                        strlcat(state->pp_buf, " <", PAGE_SIZE);
-                       parse_extended(state, start, size);
+                       parse_extended(state, start, size, disksig);
                        strlcat(state->pp_buf, " >", PAGE_SIZE);
                        continue;
                }
                put_partition(state, slot, start, size);
+               set_info(state, slot, disksig);
                if (SYS_IND(p) == LINUX_RAID_PARTITION)
                        state->parts[slot].flags = ADDPART_FLAG_RAID;
                if (SYS_IND(p) == DM6_PARTITION)
index ca83f96756ad86b2a339971050b7378f9a9752d9..6526157edafc151e2761adf740a6404a6c046f26 100644 (file)
@@ -41,8 +41,9 @@
 #include <linux/spinlock.h>
 #include <linux/compat.h>
 #include <linux/mutex.h>
+#include <linux/bitmap.h>
+#include <linux/io.h>
 #include <asm/uaccess.h>
-#include <asm/io.h>
 
 #include <linux/dma-mapping.h>
 #include <linux/blkdev.h>
@@ -978,8 +979,7 @@ static CommandList_struct *cmd_alloc(ctlr_info_t *h)
                i = find_first_zero_bit(h->cmd_pool_bits, h->nr_cmds);
                if (i == h->nr_cmds)
                        return NULL;
-       } while (test_and_set_bit(i & (BITS_PER_LONG - 1),
-                 h->cmd_pool_bits + (i / BITS_PER_LONG)) != 0);
+       } while (test_and_set_bit(i, h->cmd_pool_bits) != 0);
        c = h->cmd_pool + i;
        memset(c, 0, sizeof(CommandList_struct));
        cmd_dma_handle = h->cmd_pool_dhandle + i * sizeof(CommandList_struct);
@@ -1046,8 +1046,7 @@ static void cmd_free(ctlr_info_t *h, CommandList_struct *c)
        int i;
 
        i = c - h->cmd_pool;
-       clear_bit(i & (BITS_PER_LONG - 1),
-                 h->cmd_pool_bits + (i / BITS_PER_LONG));
+       clear_bit(i, h->cmd_pool_bits);
        h->nr_frees++;
 }
 
@@ -4268,10 +4267,7 @@ static void __devinit cciss_find_board_params(ctlr_info_t *h)
 
 static inline bool CISS_signature_present(ctlr_info_t *h)
 {
-       if ((readb(&h->cfgtable->Signature[0]) != 'C') ||
-           (readb(&h->cfgtable->Signature[1]) != 'I') ||
-           (readb(&h->cfgtable->Signature[2]) != 'S') ||
-           (readb(&h->cfgtable->Signature[3]) != 'S')) {
+       if (!check_signature(h->cfgtable->Signature, "CISS", 4)) {
                dev_warn(&h->pdev->dev, "not a valid CISS config table\n");
                return false;
        }
@@ -4812,8 +4808,7 @@ static __devinit int cciss_init_reset_devices(struct pci_dev *pdev)
 
 static __devinit int cciss_allocate_cmd_pool(ctlr_info_t *h)
 {
-       h->cmd_pool_bits = kmalloc(
-               DIV_ROUND_UP(h->nr_cmds, BITS_PER_LONG) *
+       h->cmd_pool_bits = kmalloc(BITS_TO_LONGS(h->nr_cmds) *
                sizeof(unsigned long), GFP_KERNEL);
        h->cmd_pool = pci_alloc_consistent(h->pdev,
                h->nr_cmds * sizeof(CommandList_struct),
@@ -5068,9 +5063,7 @@ reinit_after_soft_reset:
        pci_set_drvdata(pdev, h);
        /* command and error info recs zeroed out before
           they are used */
-       memset(h->cmd_pool_bits, 0,
-              DIV_ROUND_UP(h->nr_cmds, BITS_PER_LONG)
-                       * sizeof(unsigned long));
+       bitmap_zero(h->cmd_pool_bits, h->nr_cmds);
 
        h->num_luns = 0;
        h->highest_lun = -1;
index df0983787390ca1c4b0d0f2c6a99c2c575bbc8d8..7845bd6ee414715463a81d4975d493a1a7327464 100644 (file)
@@ -2,13 +2,14 @@
 # DRBD device driver configuration
 #
 
-comment "DRBD disabled because PROC_FS, INET or CONNECTOR not selected"
-       depends on PROC_FS='n' || INET='n' || CONNECTOR='n'
+comment "DRBD disabled because PROC_FS or INET not selected"
+       depends on PROC_FS='n' || INET='n'
 
 config BLK_DEV_DRBD
        tristate "DRBD Distributed Replicated Block Device support"
-       depends on PROC_FS && INET && CONNECTOR
+       depends on PROC_FS && INET
        select LRU_CACHE
+       select LIBCRC32C
        default n
        help
 
@@ -58,7 +59,8 @@ config DRBD_FAULT_INJECTION
          32    data read
          64    read ahead
          128   kmalloc of bitmap
-         256   allocation of EE (epoch_entries)
+         256   allocation of peer_requests
+         512   insert data corruption on receiving side
 
          fault_devs: bitmask of minor numbers
          fault_rate: frequency in percent
index 0d3f337ff5ffbc52e16db5e6962087dfad17f96a..8b450338075eca905b91c04e16ce3567e54f73d9 100644 (file)
@@ -1,5 +1,7 @@
 drbd-y := drbd_bitmap.o drbd_proc.o
 drbd-y += drbd_worker.o drbd_receiver.o drbd_req.o drbd_actlog.o
 drbd-y += drbd_main.o drbd_strings.o drbd_nl.o
+drbd-y += drbd_interval.o drbd_state.o
+drbd-y += drbd_nla.o
 
 obj-$(CONFIG_BLK_DEV_DRBD)     += drbd.o
index 3fbef018ce555fe47a716a0de01c2df837a2cc9e..92510f8ad0131f480aac0fddc48d8e889f8d690f 100644 (file)
  */
 
 #include <linux/slab.h>
+#include <linux/crc32c.h>
 #include <linux/drbd.h>
+#include <linux/drbd_limits.h>
+#include <linux/dynamic_debug.h>
 #include "drbd_int.h"
 #include "drbd_wrappers.h"
 
-/* We maintain a trivial checksum in our on disk activity log.
- * With that we can ensure correct operation even when the storage
- * device might do a partial (last) sector write while losing power.
- */
-struct __packed al_transaction {
-       u32       magic;
-       u32       tr_number;
-       struct __packed {
-               u32 pos;
-               u32 extent; } updates[1 + AL_EXTENTS_PT];
-       u32       xor_sum;
+
+enum al_transaction_types {
+       AL_TR_UPDATE = 0,
+       AL_TR_INITIALIZED = 0xffff
+};
+/* all fields on disc in big endian */
+struct __packed al_transaction_on_disk {
+       /* don't we all like magic */
+       __be32  magic;
+
+       /* to identify the most recent transaction block
+        * in the on disk ring buffer */
+       __be32  tr_number;
+
+       /* checksum on the full 4k block, with this field set to 0. */
+       __be32  crc32c;
+
+       /* type of transaction, special transaction types like:
+        * purge-all, set-all-idle, set-all-active, ... to-be-defined
+        * see also enum al_transaction_types */
+       __be16  transaction_type;
+
+       /* we currently allow only a few thousand extents,
+        * so 16bit will be enough for the slot number. */
+
+       /* how many updates in this transaction */
+       __be16  n_updates;
+
+       /* maximum slot number, "al-extents" in drbd.conf speak.
+        * Having this in each transaction should make reconfiguration
+        * of that parameter easier. */
+       __be16  context_size;
+
+       /* slot number the context starts with */
+       __be16  context_start_slot_nr;
+
+       /* Some reserved bytes.  Expected usage is a 64bit counter of
+        * sectors-written since device creation, and other data generation tag
+        * supporting usage */
+       __be32  __reserved[4];
+
+       /* --- 36 byte used --- */
+
+       /* Reserve space for up to AL_UPDATES_PER_TRANSACTION changes
+        * in one transaction, then use the remaining byte in the 4k block for
+        * context information.  "Flexible" number of updates per transaction
+        * does not help, as we have to account for the case when all update
+        * slots are used anyways, so it would only complicate code without
+        * additional benefit.
+        */
+       __be16  update_slot_nr[AL_UPDATES_PER_TRANSACTION];
+
+       /* but the extent number is 32bit, which at an extent size of 4 MiB
+        * allows to cover device sizes of up to 2**54 Byte (16 PiB) */
+       __be32  update_extent_nr[AL_UPDATES_PER_TRANSACTION];
+
+       /* --- 420 bytes used (36 + 64*6) --- */
+
+       /* 4096 - 420 = 3676 = 919 * 4 */
+       __be32  context[AL_CONTEXT_PER_TRANSACTION];
 };
 
 struct update_odbm_work {
@@ -48,22 +100,11 @@ struct update_odbm_work {
 
 struct update_al_work {
        struct drbd_work w;
-       struct lc_element *al_ext;
        struct completion event;
-       unsigned int enr;
-       /* if old_enr != LC_FREE, write corresponding bitmap sector, too */
-       unsigned int old_enr;
-};
-
-struct drbd_atodb_wait {
-       atomic_t           count;
-       struct completion  io_done;
-       struct drbd_conf   *mdev;
-       int                error;
+       int err;
 };
 
-
-int w_al_write_transaction(struct drbd_conf *, struct drbd_work *, int);
+static int al_write_transaction(struct drbd_conf *mdev);
 
 void *drbd_md_get_buffer(struct drbd_conf *mdev)
 {
@@ -82,22 +123,24 @@ void drbd_md_put_buffer(struct drbd_conf *mdev)
                wake_up(&mdev->misc_wait);
 }
 
-static bool md_io_allowed(struct drbd_conf *mdev)
-{
-       enum drbd_disk_state ds = mdev->state.disk;
-       return ds >= D_NEGOTIATING || ds == D_ATTACHING;
-}
-
-void wait_until_done_or_disk_failure(struct drbd_conf *mdev, struct drbd_backing_dev *bdev,
+void wait_until_done_or_force_detached(struct drbd_conf *mdev, struct drbd_backing_dev *bdev,
                                     unsigned int *done)
 {
-       long dt = bdev->dc.disk_timeout * HZ / 10;
+       long dt;
+
+       rcu_read_lock();
+       dt = rcu_dereference(bdev->disk_conf)->disk_timeout;
+       rcu_read_unlock();
+       dt = dt * HZ / 10;
        if (dt == 0)
                dt = MAX_SCHEDULE_TIMEOUT;
 
-       dt = wait_event_timeout(mdev->misc_wait, *done || !md_io_allowed(mdev), dt);
-       if (dt == 0)
+       dt = wait_event_timeout(mdev->misc_wait,
+                       *done || test_bit(FORCE_DETACH, &mdev->flags), dt);
+       if (dt == 0) {
                dev_err(DEV, "meta-data IO operation timed out\n");
+               drbd_chk_io_error(mdev, 1, DRBD_FORCE_DETACH);
+       }
 }
 
 static int _drbd_md_sync_page_io(struct drbd_conf *mdev,
@@ -106,7 +149,7 @@ static int _drbd_md_sync_page_io(struct drbd_conf *mdev,
                                 int rw, int size)
 {
        struct bio *bio;
-       int ok;
+       int err;
 
        mdev->md_io.done = 0;
        mdev->md_io.error = -ENODEV;
@@ -118,8 +161,8 @@ static int _drbd_md_sync_page_io(struct drbd_conf *mdev,
        bio = bio_alloc_drbd(GFP_NOIO);
        bio->bi_bdev = bdev->md_bdev;
        bio->bi_sector = sector;
-       ok = (bio_add_page(bio, page, size, 0) == size);
-       if (!ok)
+       err = -EIO;
+       if (bio_add_page(bio, page, size, 0) != size)
                goto out;
        bio->bi_private = &mdev->md_io;
        bio->bi_end_io = drbd_md_io_complete;
@@ -127,7 +170,7 @@ static int _drbd_md_sync_page_io(struct drbd_conf *mdev,
 
        if (!get_ldev_if_state(mdev, D_ATTACHING)) {  /* Corresponding put_ldev in drbd_md_io_complete() */
                dev_err(DEV, "ASSERT FAILED: get_ldev_if_state() == 1 in _drbd_md_sync_page_io()\n");
-               ok = 0;
+               err = -ENODEV;
                goto out;
        }
 
@@ -137,86 +180,47 @@ static int _drbd_md_sync_page_io(struct drbd_conf *mdev,
                bio_endio(bio, -EIO);
        else
                submit_bio(rw, bio);
-       wait_until_done_or_disk_failure(mdev, bdev, &mdev->md_io.done);
-       ok = bio_flagged(bio, BIO_UPTODATE) && mdev->md_io.error == 0;
+       wait_until_done_or_force_detached(mdev, bdev, &mdev->md_io.done);
+       if (bio_flagged(bio, BIO_UPTODATE))
+               err = mdev->md_io.error;
 
  out:
        bio_put(bio);
-       return ok;
+       return err;
 }
 
 int drbd_md_sync_page_io(struct drbd_conf *mdev, struct drbd_backing_dev *bdev,
                         sector_t sector, int rw)
 {
-       int logical_block_size, mask, ok;
-       int offset = 0;
+       int err;
        struct page *iop = mdev->md_io_page;
 
        D_ASSERT(atomic_read(&mdev->md_io_in_use) == 1);
 
        BUG_ON(!bdev->md_bdev);
 
-       logical_block_size = bdev_logical_block_size(bdev->md_bdev);
-       if (logical_block_size == 0)
-               logical_block_size = MD_SECTOR_SIZE;
-
-       /* in case logical_block_size != 512 [ s390 only? ] */
-       if (logical_block_size != MD_SECTOR_SIZE) {
-               mask = (logical_block_size / MD_SECTOR_SIZE) - 1;
-               D_ASSERT(mask == 1 || mask == 3 || mask == 7);
-               D_ASSERT(logical_block_size == (mask+1) * MD_SECTOR_SIZE);
-               offset = sector & mask;
-               sector = sector & ~mask;
-               iop = mdev->md_io_tmpp;
-
-               if (rw & WRITE) {
-                       /* these are GFP_KERNEL pages, pre-allocated
-                        * on device initialization */
-                       void *p = page_address(mdev->md_io_page);
-                       void *hp = page_address(mdev->md_io_tmpp);
-
-                       ok = _drbd_md_sync_page_io(mdev, bdev, iop, sector,
-                                       READ, logical_block_size);
-
-                       if (unlikely(!ok)) {
-                               dev_err(DEV, "drbd_md_sync_page_io(,%llus,"
-                                   "READ [logical_block_size!=512]) failed!\n",
-                                   (unsigned long long)sector);
-                               return 0;
-                       }
-
-                       memcpy(hp + offset*MD_SECTOR_SIZE, p, MD_SECTOR_SIZE);
-               }
-       }
+       dev_dbg(DEV, "meta_data io: %s [%d]:%s(,%llus,%s)\n",
+            current->comm, current->pid, __func__,
+            (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ");
 
        if (sector < drbd_md_first_sector(bdev) ||
-           sector > drbd_md_last_sector(bdev))
+           sector + 7 > drbd_md_last_sector(bdev))
                dev_alert(DEV, "%s [%d]:%s(,%llus,%s) out of range md access!\n",
                     current->comm, current->pid, __func__,
                     (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ");
 
-       ok = _drbd_md_sync_page_io(mdev, bdev, iop, sector, rw, logical_block_size);
-       if (unlikely(!ok)) {
-               dev_err(DEV, "drbd_md_sync_page_io(,%llus,%s) failed!\n",
-                   (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ");
-               return 0;
-       }
-
-       if (logical_block_size != MD_SECTOR_SIZE && !(rw & WRITE)) {
-               void *p = page_address(mdev->md_io_page);
-               void *hp = page_address(mdev->md_io_tmpp);
-
-               memcpy(p, hp + offset*MD_SECTOR_SIZE, MD_SECTOR_SIZE);
+       err = _drbd_md_sync_page_io(mdev, bdev, iop, sector, rw, MD_BLOCK_SIZE);
+       if (err) {
+               dev_err(DEV, "drbd_md_sync_page_io(,%llus,%s) failed with error %d\n",
+                   (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ", err);
        }
-
-       return ok;
+       return err;
 }
 
 static struct lc_element *_al_get(struct drbd_conf *mdev, unsigned int enr)
 {
        struct lc_element *al_ext;
        struct lc_element *tmp;
-       unsigned long     al_flags = 0;
        int wake;
 
        spin_lock_irq(&mdev->al_lock);
@@ -231,76 +235,92 @@ static struct lc_element *_al_get(struct drbd_conf *mdev, unsigned int enr)
                        return NULL;
                }
        }
-       al_ext   = lc_get(mdev->act_log, enr);
-       al_flags = mdev->act_log->flags;
+       al_ext = lc_get(mdev->act_log, enr);
        spin_unlock_irq(&mdev->al_lock);
-
-       /*
-       if (!al_ext) {
-               if (al_flags & LC_STARVING)
-                       dev_warn(DEV, "Have to wait for LRU element (AL too small?)\n");
-               if (al_flags & LC_DIRTY)
-                       dev_warn(DEV, "Ongoing AL update (AL device too slow?)\n");
-       }
-       */
-
        return al_ext;
 }
 
-void drbd_al_begin_io(struct drbd_conf *mdev, sector_t sector)
+void drbd_al_begin_io(struct drbd_conf *mdev, struct drbd_interval *i)
 {
-       unsigned int enr = (sector >> (AL_EXTENT_SHIFT-9));
-       struct lc_element *al_ext;
-       struct update_al_work al_work;
+       /* for bios crossing activity log extent boundaries,
+        * we may need to activate two extents in one go */
+       unsigned first = i->sector >> (AL_EXTENT_SHIFT-9);
+       unsigned last = i->size == 0 ? first : (i->sector + (i->size >> 9) - 1) >> (AL_EXTENT_SHIFT-9);
+       unsigned enr;
+       bool locked = false;
 
+
+       D_ASSERT(first <= last);
        D_ASSERT(atomic_read(&mdev->local_cnt) > 0);
 
-       wait_event(mdev->al_wait, (al_ext = _al_get(mdev, enr)));
+       for (enr = first; enr <= last; enr++)
+               wait_event(mdev->al_wait, _al_get(mdev, enr) != NULL);
+
+       /* Serialize multiple transactions.
+        * This uses test_and_set_bit, memory barrier is implicit.
+        */
+       wait_event(mdev->al_wait,
+                       mdev->act_log->pending_changes == 0 ||
+                       (locked = lc_try_lock_for_transaction(mdev->act_log)));
 
-       if (al_ext->lc_number != enr) {
+       if (locked) {
                /* drbd_al_write_transaction(mdev,al_ext,enr);
                 * recurses into generic_make_request(), which
                 * disallows recursion, bios being serialized on the
                 * current->bio_tail list now.
                 * we have to delegate updates to the activity log
                 * to the worker thread. */
-               init_completion(&al_work.event);
-               al_work.al_ext = al_ext;
-               al_work.enr = enr;
-               al_work.old_enr = al_ext->lc_number;
-               al_work.w.cb = w_al_write_transaction;
-               drbd_queue_work_front(&mdev->data.work, &al_work.w);
-               wait_for_completion(&al_work.event);
-
-               mdev->al_writ_cnt++;
-
-               spin_lock_irq(&mdev->al_lock);
-               lc_changed(mdev->act_log, al_ext);
-               spin_unlock_irq(&mdev->al_lock);
+
+               /* Double check: it may have been committed by someone else,
+                * while we have been waiting for the lock. */
+               if (mdev->act_log->pending_changes) {
+                       bool write_al_updates;
+
+                       rcu_read_lock();
+                       write_al_updates = rcu_dereference(mdev->ldev->disk_conf)->al_updates;
+                       rcu_read_unlock();
+
+                       if (write_al_updates) {
+                               al_write_transaction(mdev);
+                               mdev->al_writ_cnt++;
+                       }
+
+                       spin_lock_irq(&mdev->al_lock);
+                       /* FIXME
+                       if (err)
+                               we need an "lc_cancel" here;
+                       */
+                       lc_committed(mdev->act_log);
+                       spin_unlock_irq(&mdev->al_lock);
+               }
+               lc_unlock(mdev->act_log);
                wake_up(&mdev->al_wait);
        }
 }
 
-void drbd_al_complete_io(struct drbd_conf *mdev, sector_t sector)
+void drbd_al_complete_io(struct drbd_conf *mdev, struct drbd_interval *i)
 {
-       unsigned int enr = (sector >> (AL_EXTENT_SHIFT-9));
+       /* for bios crossing activity log extent boundaries,
+        * we may need to activate two extents in one go */
+       unsigned first = i->sector >> (AL_EXTENT_SHIFT-9);
+       unsigned last = i->size == 0 ? first : (i->sector + (i->size >> 9) - 1) >> (AL_EXTENT_SHIFT-9);
+       unsigned enr;
        struct lc_element *extent;
        unsigned long flags;
 
+       D_ASSERT(first <= last);
        spin_lock_irqsave(&mdev->al_lock, flags);
 
-       extent = lc_find(mdev->act_log, enr);
-
-       if (!extent) {
-               spin_unlock_irqrestore(&mdev->al_lock, flags);
-               dev_err(DEV, "al_complete_io() called on inactive extent %u\n", enr);
-               return;
+       for (enr = first; enr <= last; enr++) {
+               extent = lc_find(mdev->act_log, enr);
+               if (!extent) {
+                       dev_err(DEV, "al_complete_io() called on inactive extent %u\n", enr);
+                       continue;
+               }
+               lc_put(mdev->act_log, extent);
        }
-
-       if (lc_put(mdev->act_log, extent) == 0)
-               wake_up(&mdev->al_wait);
-
        spin_unlock_irqrestore(&mdev->al_lock, flags);
+       wake_up(&mdev->al_wait);
 }
 
 #if (PAGE_SHIFT + 3) < (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT)
@@ -326,296 +346,148 @@ static unsigned int rs_extent_to_bm_page(unsigned int rs_enr)
        return rs_enr >>
                /* bit to page */
                ((PAGE_SHIFT + 3) -
-               /* al extent number to bit */
+               /* resync extent number to bit */
                 (BM_EXT_SHIFT - BM_BLOCK_SHIFT));
 }
 
-int
-w_al_write_transaction(struct drbd_conf *mdev, struct drbd_work *w, int unused)
+static int
+_al_write_transaction(struct drbd_conf *mdev)
 {
-       struct update_al_work *aw = container_of(w, struct update_al_work, w);
-       struct lc_element *updated = aw->al_ext;
-       const unsigned int new_enr = aw->enr;
-       const unsigned int evicted = aw->old_enr;
-       struct al_transaction *buffer;
+       struct al_transaction_on_disk *buffer;
+       struct lc_element *e;
        sector_t sector;
-       int i, n, mx;
-       unsigned int extent_nr;
-       u32 xor_sum = 0;
+       int i, mx;
+       unsigned extent_nr;
+       unsigned crc = 0;
+       int err = 0;
 
        if (!get_ldev(mdev)) {
-               dev_err(DEV,
-                       "disk is %s, cannot start al transaction (-%d +%d)\n",
-                       drbd_disk_str(mdev->state.disk), evicted, new_enr);
-               complete(&((struct update_al_work *)w)->event);
-               return 1;
+               dev_err(DEV, "disk is %s, cannot start al transaction\n",
+                       drbd_disk_str(mdev->state.disk));
+               return -EIO;
        }
-       /* do we have to do a bitmap write, first?
-        * TODO reduce maximum latency:
-        * submit both bios, then wait for both,
-        * instead of doing two synchronous sector writes.
-        * For now, we must not write the transaction,
-        * if we cannot write out the bitmap of the evicted extent. */
-       if (mdev->state.conn < C_CONNECTED && evicted != LC_FREE)
-               drbd_bm_write_page(mdev, al_extent_to_bm_page(evicted));
 
        /* The bitmap write may have failed, causing a state change. */
        if (mdev->state.disk < D_INCONSISTENT) {
                dev_err(DEV,
-                       "disk is %s, cannot write al transaction (-%d +%d)\n",
-                       drbd_disk_str(mdev->state.disk), evicted, new_enr);
-               complete(&((struct update_al_work *)w)->event);
+                       "disk is %s, cannot write al transaction\n",
+                       drbd_disk_str(mdev->state.disk));
                put_ldev(mdev);
-               return 1;
+               return -EIO;
        }
 
        buffer = drbd_md_get_buffer(mdev); /* protects md_io_buffer, al_tr_cycle, ... */
        if (!buffer) {
                dev_err(DEV, "disk failed while waiting for md_io buffer\n");
-               complete(&((struct update_al_work *)w)->event);
                put_ldev(mdev);
-               return 1;
+               return -ENODEV;
        }
 
-       buffer->magic = __constant_cpu_to_be32(DRBD_MAGIC);
+       memset(buffer, 0, sizeof(*buffer));
+       buffer->magic = cpu_to_be32(DRBD_AL_MAGIC);
        buffer->tr_number = cpu_to_be32(mdev->al_tr_number);
 
-       n = lc_index_of(mdev->act_log, updated);
+       i = 0;
+
+       /* Even though no one can start to change this list
+        * once we set the LC_LOCKED -- from drbd_al_begin_io(),
+        * lc_try_lock_for_transaction() --, someone may still
+        * be in the process of changing it. */
+       spin_lock_irq(&mdev->al_lock);
+       list_for_each_entry(e, &mdev->act_log->to_be_changed, list) {
+               if (i == AL_UPDATES_PER_TRANSACTION) {
+                       i++;
+                       break;
+               }
+               buffer->update_slot_nr[i] = cpu_to_be16(e->lc_index);
+               buffer->update_extent_nr[i] = cpu_to_be32(e->lc_new_number);
+               if (e->lc_number != LC_FREE)
+                       drbd_bm_mark_for_writeout(mdev,
+                                       al_extent_to_bm_page(e->lc_number));
+               i++;
+       }
+       spin_unlock_irq(&mdev->al_lock);
+       BUG_ON(i > AL_UPDATES_PER_TRANSACTION);
 
-       buffer->updates[0].pos = cpu_to_be32(n);
-       buffer->updates[0].extent = cpu_to_be32(new_enr);
+       buffer->n_updates = cpu_to_be16(i);
+       for ( ; i < AL_UPDATES_PER_TRANSACTION; i++) {
+               buffer->update_slot_nr[i] = cpu_to_be16(-1);
+               buffer->update_extent_nr[i] = cpu_to_be32(LC_FREE);
+       }
 
-       xor_sum ^= new_enr;
+       buffer->context_size = cpu_to_be16(mdev->act_log->nr_elements);
+       buffer->context_start_slot_nr = cpu_to_be16(mdev->al_tr_cycle);
 
-       mx = min_t(int, AL_EXTENTS_PT,
+       mx = min_t(int, AL_CONTEXT_PER_TRANSACTION,
                   mdev->act_log->nr_elements - mdev->al_tr_cycle);
        for (i = 0; i < mx; i++) {
                unsigned idx = mdev->al_tr_cycle + i;
                extent_nr = lc_element_by_index(mdev->act_log, idx)->lc_number;
-               buffer->updates[i+1].pos = cpu_to_be32(idx);
-               buffer->updates[i+1].extent = cpu_to_be32(extent_nr);
-               xor_sum ^= extent_nr;
-       }
-       for (; i < AL_EXTENTS_PT; i++) {
-               buffer->updates[i+1].pos = __constant_cpu_to_be32(-1);
-               buffer->updates[i+1].extent = __constant_cpu_to_be32(LC_FREE);
-               xor_sum ^= LC_FREE;
+               buffer->context[i] = cpu_to_be32(extent_nr);
        }
-       mdev->al_tr_cycle += AL_EXTENTS_PT;
+       for (; i < AL_CONTEXT_PER_TRANSACTION; i++)
+               buffer->context[i] = cpu_to_be32(LC_FREE);
+
+       mdev->al_tr_cycle += AL_CONTEXT_PER_TRANSACTION;
        if (mdev->al_tr_cycle >= mdev->act_log->nr_elements)
                mdev->al_tr_cycle = 0;
 
-       buffer->xor_sum = cpu_to_be32(xor_sum);
-
        sector =  mdev->ldev->md.md_offset
-               + mdev->ldev->md.al_offset + mdev->al_tr_pos;
-
-       if (!drbd_md_sync_page_io(mdev, mdev->ldev, sector, WRITE))
-               drbd_chk_io_error(mdev, 1, DRBD_META_IO_ERROR);
+               + mdev->ldev->md.al_offset
+               + mdev->al_tr_pos * (MD_BLOCK_SIZE>>9);
 
-       if (++mdev->al_tr_pos >
-           div_ceil(mdev->act_log->nr_elements, AL_EXTENTS_PT))
-               mdev->al_tr_pos = 0;
+       crc = crc32c(0, buffer, 4096);
+       buffer->crc32c = cpu_to_be32(crc);
 
-       D_ASSERT(mdev->al_tr_pos < MD_AL_MAX_SIZE);
-       mdev->al_tr_number++;
+       if (drbd_bm_write_hinted(mdev))
+               err = -EIO;
+               /* drbd_chk_io_error done already */
+       else if (drbd_md_sync_page_io(mdev, mdev->ldev, sector, WRITE)) {
+               err = -EIO;
+               drbd_chk_io_error(mdev, 1, DRBD_META_IO_ERROR);
+       } else {
+               /* advance ringbuffer position and transaction counter */
+               mdev->al_tr_pos = (mdev->al_tr_pos + 1) % (MD_AL_SECTORS*512/MD_BLOCK_SIZE);
+               mdev->al_tr_number++;
+       }
 
        drbd_md_put_buffer(mdev);
-
-       complete(&((struct update_al_work *)w)->event);
        put_ldev(mdev);
 
-       return 1;
+       return err;
 }
 
-/**
- * drbd_al_read_tr() - Read a single transaction from the on disk activity log
- * @mdev:      DRBD device.
- * @bdev:      Block device to read form.
- * @b:         pointer to an al_transaction.
- * @index:     On disk slot of the transaction to read.
- *
- * Returns -1 on IO error, 0 on checksum error and 1 upon success.
- */
-static int drbd_al_read_tr(struct drbd_conf *mdev,
-                          struct drbd_backing_dev *bdev,
-                          struct al_transaction *b,
-                          int index)
-{
-       sector_t sector;
-       int rv, i;
-       u32 xor_sum = 0;
-
-       sector = bdev->md.md_offset + bdev->md.al_offset + index;
-
-       /* Dont process error normally,
-        * as this is done before disk is attached! */
-       if (!drbd_md_sync_page_io(mdev, bdev, sector, READ))
-               return -1;
-
-       rv = (be32_to_cpu(b->magic) == DRBD_MAGIC);
-
-       for (i = 0; i < AL_EXTENTS_PT + 1; i++)
-               xor_sum ^= be32_to_cpu(b->updates[i].extent);
-       rv &= (xor_sum == be32_to_cpu(b->xor_sum));
 
-       return rv;
-}
-
-/**
- * drbd_al_read_log() - Restores the activity log from its on disk representation.
- * @mdev:      DRBD device.
- * @bdev:      Block device to read form.
- *
- * Returns 1 on success, returns 0 when reading the log failed due to IO errors.
- */
-int drbd_al_read_log(struct drbd_conf *mdev, struct drbd_backing_dev *bdev)
+static int w_al_write_transaction(struct drbd_work *w, int unused)
 {
-       struct al_transaction *buffer;
-       int i;
-       int rv;
-       int mx;
-       int active_extents = 0;
-       int transactions = 0;
-       int found_valid = 0;
-       int from = 0;
-       int to = 0;
-       u32 from_tnr = 0;
-       u32 to_tnr = 0;
-       u32 cnr;
-
-       mx = div_ceil(mdev->act_log->nr_elements, AL_EXTENTS_PT);
-
-       /* lock out all other meta data io for now,
-        * and make sure the page is mapped.
-        */
-       buffer = drbd_md_get_buffer(mdev);
-       if (!buffer)
-               return 0;
-
-       /* Find the valid transaction in the log */
-       for (i = 0; i <= mx; i++) {
-               rv = drbd_al_read_tr(mdev, bdev, buffer, i);
-               if (rv == 0)
-                       continue;
-               if (rv == -1) {
-                       drbd_md_put_buffer(mdev);
-                       return 0;
-               }
-               cnr = be32_to_cpu(buffer->tr_number);
-
-               if (++found_valid == 1) {
-                       from = i;
-                       to = i;
-                       from_tnr = cnr;
-                       to_tnr = cnr;
-                       continue;
-               }
-               if ((int)cnr - (int)from_tnr < 0) {
-                       D_ASSERT(from_tnr - cnr + i - from == mx+1);
-                       from = i;
-                       from_tnr = cnr;
-               }
-               if ((int)cnr - (int)to_tnr > 0) {
-                       D_ASSERT(cnr - to_tnr == i - to);
-                       to = i;
-                       to_tnr = cnr;
-               }
-       }
-
-       if (!found_valid) {
-               dev_warn(DEV, "No usable activity log found.\n");
-               drbd_md_put_buffer(mdev);
-               return 1;
-       }
-
-       /* Read the valid transactions.
-        * dev_info(DEV, "Reading from %d to %d.\n",from,to); */
-       i = from;
-       while (1) {
-               int j, pos;
-               unsigned int extent_nr;
-               unsigned int trn;
-
-               rv = drbd_al_read_tr(mdev, bdev, buffer, i);
-               ERR_IF(rv == 0) goto cancel;
-               if (rv == -1) {
-                       drbd_md_put_buffer(mdev);
-                       return 0;
-               }
-
-               trn = be32_to_cpu(buffer->tr_number);
-
-               spin_lock_irq(&mdev->al_lock);
-
-               /* This loop runs backwards because in the cyclic
-                  elements there might be an old version of the
-                  updated element (in slot 0). So the element in slot 0
-                  can overwrite old versions. */
-               for (j = AL_EXTENTS_PT; j >= 0; j--) {
-                       pos = be32_to_cpu(buffer->updates[j].pos);
-                       extent_nr = be32_to_cpu(buffer->updates[j].extent);
-
-                       if (extent_nr == LC_FREE)
-                               continue;
-
-                       lc_set(mdev->act_log, extent_nr, pos);
-                       active_extents++;
-               }
-               spin_unlock_irq(&mdev->al_lock);
-
-               transactions++;
-
-cancel:
-               if (i == to)
-                       break;
-               i++;
-               if (i > mx)
-                       i = 0;
-       }
-
-       mdev->al_tr_number = to_tnr+1;
-       mdev->al_tr_pos = to;
-       if (++mdev->al_tr_pos >
-           div_ceil(mdev->act_log->nr_elements, AL_EXTENTS_PT))
-               mdev->al_tr_pos = 0;
-
-       /* ok, we are done with it */
-       drbd_md_put_buffer(mdev);
+       struct update_al_work *aw = container_of(w, struct update_al_work, w);
+       struct drbd_conf *mdev = w->mdev;
+       int err;
 
-       dev_info(DEV, "Found %d transactions (%d active extents) in activity log.\n",
-            transactions, active_extents);
+       err = _al_write_transaction(mdev);
+       aw->err = err;
+       complete(&aw->event);
 
-       return 1;
+       return err != -EIO ? err : 0;
 }
 
-/**
- * drbd_al_apply_to_bm() - Sets the bitmap to diry(1) where covered ba active AL extents
- * @mdev:      DRBD device.
- */
-void drbd_al_apply_to_bm(struct drbd_conf *mdev)
+/* Calls from worker context (see w_restart_disk_io()) need to write the
+   transaction directly. Others came through generic_make_request(),
+   those need to delegate it to the worker. */
+static int al_write_transaction(struct drbd_conf *mdev)
 {
-       unsigned int enr;
-       unsigned long add = 0;
-       char ppb[10];
-       int i, tmp;
-
-       wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
+       struct update_al_work al_work;
 
-       for (i = 0; i < mdev->act_log->nr_elements; i++) {
-               enr = lc_element_by_index(mdev->act_log, i)->lc_number;
-               if (enr == LC_FREE)
-                       continue;
-               tmp = drbd_bm_ALe_set_all(mdev, enr);
-               dynamic_dev_dbg(DEV, "AL: set %d bits in extent %u\n", tmp, enr);
-               add += tmp;
-       }
+       if (current == mdev->tconn->worker.task)
+               return _al_write_transaction(mdev);
 
-       lc_unlock(mdev->act_log);
-       wake_up(&mdev->al_wait);
+       init_completion(&al_work.event);
+       al_work.w.cb = w_al_write_transaction;
+       al_work.w.mdev = mdev;
+       drbd_queue_work_front(&mdev->tconn->sender_work, &al_work.w);
+       wait_for_completion(&al_work.event);
 
-       dev_info(DEV, "Marked additional %s as out-of-sync based on AL.\n",
-            ppsize(ppb, Bit2KB(add)));
+       return al_work.err;
 }
 
 static int _try_lc_del(struct drbd_conf *mdev, struct lc_element *al_ext)
@@ -645,7 +517,7 @@ void drbd_al_shrink(struct drbd_conf *mdev)
        struct lc_element *al_ext;
        int i;
 
-       D_ASSERT(test_bit(__LC_DIRTY, &mdev->act_log->flags));
+       D_ASSERT(test_bit(__LC_LOCKED, &mdev->act_log->flags));
 
        for (i = 0; i < mdev->act_log->nr_elements; i++) {
                al_ext = lc_element_by_index(mdev->act_log, i);
@@ -657,15 +529,17 @@ void drbd_al_shrink(struct drbd_conf *mdev)
        wake_up(&mdev->al_wait);
 }
 
-static int w_update_odbm(struct drbd_conf *mdev, struct drbd_work *w, int unused)
+static int w_update_odbm(struct drbd_work *w, int unused)
 {
        struct update_odbm_work *udw = container_of(w, struct update_odbm_work, w);
+       struct drbd_conf *mdev = w->mdev;
+       struct sib_info sib = { .sib_reason = SIB_SYNC_PROGRESS, };
 
        if (!get_ldev(mdev)) {
                if (__ratelimit(&drbd_ratelimit_state))
                        dev_warn(DEV, "Can not update on disk bitmap, local IO disabled.\n");
                kfree(udw);
-               return 1;
+               return 0;
        }
 
        drbd_bm_write_page(mdev, rs_extent_to_bm_page(udw->enr));
@@ -683,9 +557,9 @@ static int w_update_odbm(struct drbd_conf *mdev, struct drbd_work *w, int unused
                        break;
                }
        }
-       drbd_bcast_sync_progress(mdev);
+       drbd_bcast_event(mdev, &sib);
 
-       return 1;
+       return 0;
 }
 
 
@@ -755,7 +629,9 @@ static void drbd_try_clear_on_disk_bm(struct drbd_conf *mdev, sector_t sector,
                        }
                        ext->rs_left = rs_left;
                        ext->rs_failed = success ? 0 : count;
-                       lc_changed(mdev->resync, &ext->lce);
+                       /* we don't keep a persistent log of the resync lru,
+                        * we can commit any change right away. */
+                       lc_committed(mdev->resync);
                }
                lc_put(mdev->resync, &ext->lce);
                /* no race, we are within the al_lock! */
@@ -767,7 +643,8 @@ static void drbd_try_clear_on_disk_bm(struct drbd_conf *mdev, sector_t sector,
                        if (udw) {
                                udw->enr = ext->lce.lc_number;
                                udw->w.cb = w_update_odbm;
-                               drbd_queue_work_front(&mdev->data.work, &udw->w);
+                               udw->w.mdev = mdev;
+                               drbd_queue_work_front(&mdev->tconn->sender_work, &udw->w);
                        } else {
                                dev_warn(DEV, "Could not kmalloc an udw\n");
                        }
@@ -813,16 +690,22 @@ void __drbd_set_in_sync(struct drbd_conf *mdev, sector_t sector, int size,
        int wake_up = 0;
        unsigned long flags;
 
-       if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_BIO_SIZE) {
+       if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
                dev_err(DEV, "drbd_set_in_sync: sector=%llus size=%d nonsense!\n",
                                (unsigned long long)sector, size);
                return;
        }
+
+       if (!get_ldev(mdev))
+               return; /* no disk, no metadata, no bitmap to clear bits in */
+
        nr_sectors = drbd_get_capacity(mdev->this_bdev);
        esector = sector + (size >> 9) - 1;
 
-       ERR_IF(sector >= nr_sectors) return;
-       ERR_IF(esector >= nr_sectors) esector = (nr_sectors-1);
+       if (!expect(sector < nr_sectors))
+               goto out;
+       if (!expect(esector < nr_sectors))
+               esector = nr_sectors - 1;
 
        lbnr = BM_SECT_TO_BIT(nr_sectors-1);
 
@@ -830,7 +713,7 @@ void __drbd_set_in_sync(struct drbd_conf *mdev, sector_t sector, int size,
         * round up start sector, round down end sector.  we make sure we only
         * clear full, aligned, BM_BLOCK_SIZE (4K) blocks */
        if (unlikely(esector < BM_SECT_PER_BIT-1))
-               return;
+               goto out;
        if (unlikely(esector == (nr_sectors-1)))
                ebnr = lbnr;
        else
@@ -838,14 +721,14 @@ void __drbd_set_in_sync(struct drbd_conf *mdev, sector_t sector, int size,
        sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
 
        if (sbnr > ebnr)
-               return;
+               goto out;
 
        /*
         * ok, (capacity & 7) != 0 sometimes, but who cares...
         * we count rs_{total,left} in bits, not sectors.
         */
        count = drbd_bm_clear_bits(mdev, sbnr, ebnr);
-       if (count && get_ldev(mdev)) {
+       if (count) {
                drbd_advance_rs_marks(mdev, drbd_bm_total_weight(mdev));
                spin_lock_irqsave(&mdev->al_lock, flags);
                drbd_try_clear_on_disk_bm(mdev, sector, count, true);
@@ -854,8 +737,9 @@ void __drbd_set_in_sync(struct drbd_conf *mdev, sector_t sector, int size,
                /* just wake_up unconditional now, various lc_chaged(),
                 * lc_put() in drbd_try_clear_on_disk_bm(). */
                wake_up = 1;
-               put_ldev(mdev);
        }
+out:
+       put_ldev(mdev);
        if (wake_up)
                wake_up(&mdev->al_wait);
 }
@@ -871,7 +755,7 @@ void __drbd_set_in_sync(struct drbd_conf *mdev, sector_t sector, int size,
 int __drbd_set_out_of_sync(struct drbd_conf *mdev, sector_t sector, int size,
                            const char *file, const unsigned int line)
 {
-       unsigned long sbnr, ebnr, lbnr, flags;
+       unsigned long sbnr, ebnr, flags;
        sector_t esector, nr_sectors;
        unsigned int enr, count = 0;
        struct lc_element *e;
@@ -880,7 +764,7 @@ int __drbd_set_out_of_sync(struct drbd_conf *mdev, sector_t sector, int size,
        if (size == 0)
                return 0;
 
-       if (size < 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_BIO_SIZE) {
+       if (size < 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
                dev_err(DEV, "sector: %llus, size: %d\n",
                        (unsigned long long)sector, size);
                return 0;
@@ -892,12 +776,10 @@ int __drbd_set_out_of_sync(struct drbd_conf *mdev, sector_t sector, int size,
        nr_sectors = drbd_get_capacity(mdev->this_bdev);
        esector = sector + (size >> 9) - 1;
 
-       ERR_IF(sector >= nr_sectors)
+       if (!expect(sector < nr_sectors))
                goto out;
-       ERR_IF(esector >= nr_sectors)
-               esector = (nr_sectors-1);
-
-       lbnr = BM_SECT_TO_BIT(nr_sectors-1);
+       if (!expect(esector < nr_sectors))
+               esector = nr_sectors - 1;
 
        /* we set it out of sync,
         * we do not need to round anything here */
@@ -940,7 +822,7 @@ struct bm_extent *_bme_get(struct drbd_conf *mdev, unsigned int enr)
                if (bm_ext->lce.lc_number != enr) {
                        bm_ext->rs_left = drbd_bm_e_weight(mdev, enr);
                        bm_ext->rs_failed = 0;
-                       lc_changed(mdev->resync, &bm_ext->lce);
+                       lc_committed(mdev->resync);
                        wakeup = 1;
                }
                if (bm_ext->lce.refcnt == 1)
@@ -956,7 +838,7 @@ struct bm_extent *_bme_get(struct drbd_conf *mdev, unsigned int enr)
                if (rs_flags & LC_STARVING)
                        dev_warn(DEV, "Have to wait for element"
                             " (resync LRU too small?)\n");
-               BUG_ON(rs_flags & LC_DIRTY);
+               BUG_ON(rs_flags & LC_LOCKED);
        }
 
        return bm_ext;
@@ -964,26 +846,12 @@ struct bm_extent *_bme_get(struct drbd_conf *mdev, unsigned int enr)
 
 static int _is_in_al(struct drbd_conf *mdev, unsigned int enr)
 {
-       struct lc_element *al_ext;
-       int rv = 0;
+       int rv;
 
        spin_lock_irq(&mdev->al_lock);
-       if (unlikely(enr == mdev->act_log->new_number))
-               rv = 1;
-       else {
-               al_ext = lc_find(mdev->act_log, enr);
-               if (al_ext) {
-                       if (al_ext->refcnt)
-                               rv = 1;
-               }
-       }
+       rv = lc_is_used(mdev->act_log, enr);
        spin_unlock_irq(&mdev->al_lock);
 
-       /*
-       if (unlikely(rv)) {
-               dev_info(DEV, "Delaying sync read until app's write is done\n");
-       }
-       */
        return rv;
 }
 
@@ -1113,13 +981,13 @@ int drbd_try_rs_begin_io(struct drbd_conf *mdev, sector_t sector)
                        if (rs_flags & LC_STARVING)
                                dev_warn(DEV, "Have to wait for element"
                                     " (resync LRU too small?)\n");
-                       BUG_ON(rs_flags & LC_DIRTY);
+                       BUG_ON(rs_flags & LC_LOCKED);
                        goto try_again;
                }
                if (bm_ext->lce.lc_number != enr) {
                        bm_ext->rs_left = drbd_bm_e_weight(mdev, enr);
                        bm_ext->rs_failed = 0;
-                       lc_changed(mdev->resync, &bm_ext->lce);
+                       lc_committed(mdev->resync);
                        wake_up(&mdev->al_wait);
                        D_ASSERT(test_bit(BME_LOCKED, &bm_ext->flags) == 0);
                }
@@ -1130,8 +998,6 @@ int drbd_try_rs_begin_io(struct drbd_conf *mdev, sector_t sector)
        }
 check_al:
        for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
-               if (unlikely(al_enr+i == mdev->act_log->new_number))
-                       goto try_again;
                if (lc_is_used(mdev->act_log, al_enr+i))
                        goto try_again;
        }
@@ -1266,7 +1132,7 @@ void drbd_rs_failed_io(struct drbd_conf *mdev, sector_t sector, int size)
        sector_t esector, nr_sectors;
        int wake_up = 0;
 
-       if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_BIO_SIZE) {
+       if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
                dev_err(DEV, "drbd_rs_failed_io: sector=%llus size=%d nonsense!\n",
                                (unsigned long long)sector, size);
                return;
@@ -1274,8 +1140,10 @@ void drbd_rs_failed_io(struct drbd_conf *mdev, sector_t sector, int size)
        nr_sectors = drbd_get_capacity(mdev->this_bdev);
        esector = sector + (size >> 9) - 1;
 
-       ERR_IF(sector >= nr_sectors) return;
-       ERR_IF(esector >= nr_sectors) esector = (nr_sectors-1);
+       if (!expect(sector < nr_sectors))
+               return;
+       if (!expect(esector < nr_sectors))
+               esector = nr_sectors - 1;
 
        lbnr = BM_SECT_TO_BIT(nr_sectors-1);
 
index d84566496746aef57baf19dae86528bc202038cb..8dc29502dc08c172ce8d693e78a1578c72bc4562 100644 (file)
@@ -119,13 +119,9 @@ static void __bm_print_lock_info(struct drbd_conf *mdev, const char *func)
        if (!__ratelimit(&drbd_ratelimit_state))
                return;
        dev_err(DEV, "FIXME %s in %s, bitmap locked for '%s' by %s\n",
-           current == mdev->receiver.task ? "receiver" :
-           current == mdev->asender.task  ? "asender"  :
-           current == mdev->worker.task   ? "worker"   : current->comm,
-           func, b->bm_why ?: "?",
-           b->bm_task == mdev->receiver.task ? "receiver" :
-           b->bm_task == mdev->asender.task  ? "asender"  :
-           b->bm_task == mdev->worker.task   ? "worker"   : "?");
+               drbd_task_to_thread_name(mdev->tconn, current),
+               func, b->bm_why ?: "?",
+               drbd_task_to_thread_name(mdev->tconn, b->bm_task));
 }
 
 void drbd_bm_lock(struct drbd_conf *mdev, char *why, enum bm_flag flags)
@@ -142,13 +138,9 @@ void drbd_bm_lock(struct drbd_conf *mdev, char *why, enum bm_flag flags)
 
        if (trylock_failed) {
                dev_warn(DEV, "%s going to '%s' but bitmap already locked for '%s' by %s\n",
-                   current == mdev->receiver.task ? "receiver" :
-                   current == mdev->asender.task  ? "asender"  :
-                   current == mdev->worker.task   ? "worker"   : current->comm,
-                   why, b->bm_why ?: "?",
-                   b->bm_task == mdev->receiver.task ? "receiver" :
-                   b->bm_task == mdev->asender.task  ? "asender"  :
-                   b->bm_task == mdev->worker.task   ? "worker"   : "?");
+                        drbd_task_to_thread_name(mdev->tconn, current),
+                        why, b->bm_why ?: "?",
+                        drbd_task_to_thread_name(mdev->tconn, b->bm_task));
                mutex_lock(&b->bm_change);
        }
        if (BM_LOCKED_MASK & b->bm_flags)
@@ -196,6 +188,9 @@ void drbd_bm_unlock(struct drbd_conf *mdev)
 /* to mark for lazy writeout once syncer cleared all clearable bits,
  * we if bits have been cleared since last IO. */
 #define BM_PAGE_LAZY_WRITEOUT  28
+/* pages marked with this "HINT" will be considered for writeout
+ * on activity log transactions */
+#define BM_PAGE_HINT_WRITEOUT  27
 
 /* store_page_idx uses non-atomic assignment. It is only used directly after
  * allocating the page.  All other bm_set_page_* and bm_clear_page_* need to
@@ -227,8 +222,7 @@ static void bm_page_unlock_io(struct drbd_conf *mdev, int page_nr)
 {
        struct drbd_bitmap *b = mdev->bitmap;
        void *addr = &page_private(b->bm_pages[page_nr]);
-       clear_bit(BM_PAGE_IO_LOCK, addr);
-       smp_mb__after_clear_bit();
+       clear_bit_unlock(BM_PAGE_IO_LOCK, addr);
        wake_up(&mdev->bitmap->bm_io_wait);
 }
 
@@ -246,6 +240,27 @@ static void bm_set_page_need_writeout(struct page *page)
        set_bit(BM_PAGE_NEED_WRITEOUT, &page_private(page));
 }
 
+/**
+ * drbd_bm_mark_for_writeout() - mark a page with a "hint" to be considered for writeout
+ * @mdev:      DRBD device.
+ * @page_nr:   the bitmap page to mark with the "hint" flag
+ *
+ * From within an activity log transaction, we mark a few pages with these
+ * hints, then call drbd_bm_write_hinted(), which will only write out changed
+ * pages which are flagged with this mark.
+ */
+void drbd_bm_mark_for_writeout(struct drbd_conf *mdev, int page_nr)
+{
+       struct page *page;
+       if (page_nr >= mdev->bitmap->bm_number_of_pages) {
+               dev_warn(DEV, "BAD: page_nr: %u, number_of_pages: %u\n",
+                        page_nr, (int)mdev->bitmap->bm_number_of_pages);
+               return;
+       }
+       page = mdev->bitmap->bm_pages[page_nr];
+       set_bit(BM_PAGE_HINT_WRITEOUT, &page_private(page));
+}
+
 static int bm_test_page_unchanged(struct page *page)
 {
        volatile const unsigned long *addr = &page_private(page);
@@ -373,14 +388,16 @@ static struct page **bm_realloc_pages(struct drbd_bitmap *b, unsigned long want)
                return old_pages;
 
        /* Trying kmalloc first, falling back to vmalloc.
-        * GFP_KERNEL is ok, as this is done when a lower level disk is
-        * "attached" to the drbd.  Context is receiver thread or cqueue
-        * thread.  As we have no disk yet, we are not in the IO path,
-        * not even the IO path of the peer. */
+        * GFP_NOIO, as this is called while drbd IO is "suspended",
+        * and during resize or attach on diskless Primary,
+        * we must not block on IO to ourselves.
+        * Context is receiver thread or dmsetup. */
        bytes = sizeof(struct page *)*want;
-       new_pages = kzalloc(bytes, GFP_KERNEL);
+       new_pages = kzalloc(bytes, GFP_NOIO);
        if (!new_pages) {
-               new_pages = vzalloc(bytes);
+               new_pages = __vmalloc(bytes,
+                               GFP_NOIO | __GFP_HIGHMEM | __GFP_ZERO,
+                               PAGE_KERNEL);
                if (!new_pages)
                        return NULL;
                vmalloced = 1;
@@ -390,7 +407,7 @@ static struct page **bm_realloc_pages(struct drbd_bitmap *b, unsigned long want)
                for (i = 0; i < have; i++)
                        new_pages[i] = old_pages[i];
                for (; i < want; i++) {
-                       page = alloc_page(GFP_HIGHUSER);
+                       page = alloc_page(GFP_NOIO | __GFP_HIGHMEM);
                        if (!page) {
                                bm_free_pages(new_pages + have, i - have);
                                bm_vk_free(new_pages, vmalloced);
@@ -439,7 +456,8 @@ int drbd_bm_init(struct drbd_conf *mdev)
 
 sector_t drbd_bm_capacity(struct drbd_conf *mdev)
 {
-       ERR_IF(!mdev->bitmap) return 0;
+       if (!expect(mdev->bitmap))
+               return 0;
        return mdev->bitmap->bm_dev_capacity;
 }
 
@@ -447,7 +465,8 @@ sector_t drbd_bm_capacity(struct drbd_conf *mdev)
  */
 void drbd_bm_cleanup(struct drbd_conf *mdev)
 {
-       ERR_IF (!mdev->bitmap) return;
+       if (!expect(mdev->bitmap))
+               return;
        bm_free_pages(mdev->bitmap->bm_pages, mdev->bitmap->bm_number_of_pages);
        bm_vk_free(mdev->bitmap->bm_pages, (BM_P_VMALLOCED & mdev->bitmap->bm_flags));
        kfree(mdev->bitmap);
@@ -610,7 +629,8 @@ int drbd_bm_resize(struct drbd_conf *mdev, sector_t capacity, int set_new_bits)
        int err = 0, growing;
        int opages_vmalloced;
 
-       ERR_IF(!b) return -ENOMEM;
+       if (!expect(b))
+               return -ENOMEM;
 
        drbd_bm_lock(mdev, "resize", BM_LOCKED_MASK);
 
@@ -732,8 +752,10 @@ unsigned long _drbd_bm_total_weight(struct drbd_conf *mdev)
        unsigned long s;
        unsigned long flags;
 
-       ERR_IF(!b) return 0;
-       ERR_IF(!b->bm_pages) return 0;
+       if (!expect(b))
+               return 0;
+       if (!expect(b->bm_pages))
+               return 0;
 
        spin_lock_irqsave(&b->bm_lock, flags);
        s = b->bm_set;
@@ -756,8 +778,10 @@ unsigned long drbd_bm_total_weight(struct drbd_conf *mdev)
 size_t drbd_bm_words(struct drbd_conf *mdev)
 {
        struct drbd_bitmap *b = mdev->bitmap;
-       ERR_IF(!b) return 0;
-       ERR_IF(!b->bm_pages) return 0;
+       if (!expect(b))
+               return 0;
+       if (!expect(b->bm_pages))
+               return 0;
 
        return b->bm_words;
 }
@@ -765,7 +789,8 @@ size_t drbd_bm_words(struct drbd_conf *mdev)
 unsigned long drbd_bm_bits(struct drbd_conf *mdev)
 {
        struct drbd_bitmap *b = mdev->bitmap;
-       ERR_IF(!b) return 0;
+       if (!expect(b))
+               return 0;
 
        return b->bm_bits;
 }
@@ -786,8 +811,10 @@ void drbd_bm_merge_lel(struct drbd_conf *mdev, size_t offset, size_t number,
 
        end = offset + number;
 
-       ERR_IF(!b) return;
-       ERR_IF(!b->bm_pages) return;
+       if (!expect(b))
+               return;
+       if (!expect(b->bm_pages))
+               return;
        if (number == 0)
                return;
        WARN_ON(offset >= b->bm_words);
@@ -831,8 +858,10 @@ void drbd_bm_get_lel(struct drbd_conf *mdev, size_t offset, size_t number,
 
        end = offset + number;
 
-       ERR_IF(!b) return;
-       ERR_IF(!b->bm_pages) return;
+       if (!expect(b))
+               return;
+       if (!expect(b->bm_pages))
+               return;
 
        spin_lock_irq(&b->bm_lock);
        if ((offset >= b->bm_words) ||
@@ -860,8 +889,10 @@ void drbd_bm_get_lel(struct drbd_conf *mdev, size_t offset, size_t number,
 void drbd_bm_set_all(struct drbd_conf *mdev)
 {
        struct drbd_bitmap *b = mdev->bitmap;
-       ERR_IF(!b) return;
-       ERR_IF(!b->bm_pages) return;
+       if (!expect(b))
+               return;
+       if (!expect(b->bm_pages))
+               return;
 
        spin_lock_irq(&b->bm_lock);
        bm_memset(b, 0, 0xff, b->bm_words);
@@ -874,8 +905,10 @@ void drbd_bm_set_all(struct drbd_conf *mdev)
 void drbd_bm_clear_all(struct drbd_conf *mdev)
 {
        struct drbd_bitmap *b = mdev->bitmap;
-       ERR_IF(!b) return;
-       ERR_IF(!b->bm_pages) return;
+       if (!expect(b))
+               return;
+       if (!expect(b->bm_pages))
+               return;
 
        spin_lock_irq(&b->bm_lock);
        bm_memset(b, 0, 0, b->bm_words);
@@ -889,7 +922,8 @@ struct bm_aio_ctx {
        unsigned int done;
        unsigned flags;
 #define BM_AIO_COPY_PAGES      1
-#define BM_WRITE_ALL_PAGES     2
+#define BM_AIO_WRITE_HINTED    2
+#define BM_WRITE_ALL_PAGES     4
        int error;
        struct kref kref;
 };
@@ -977,17 +1011,11 @@ static void bm_page_io_async(struct bm_aio_ctx *ctx, int page_nr, int rw) __must
        bm_set_page_unchanged(b->bm_pages[page_nr]);
 
        if (ctx->flags & BM_AIO_COPY_PAGES) {
-               void *src, *dest;
                page = mempool_alloc(drbd_md_io_page_pool, __GFP_HIGHMEM|__GFP_WAIT);
-               dest = kmap_atomic(page);
-               src = kmap_atomic(b->bm_pages[page_nr]);
-               memcpy(dest, src, PAGE_SIZE);
-               kunmap_atomic(src);
-               kunmap_atomic(dest);
+               copy_highpage(page, b->bm_pages[page_nr]);
                bm_store_page_idx(page, page_nr);
        } else
                page = b->bm_pages[page_nr];
-
        bio->bi_bdev = mdev->ldev->md_bdev;
        bio->bi_sector = on_disk_sector;
        /* bio_add_page of a single page to an empty bio will always succeed,
@@ -1060,6 +1088,11 @@ static int bm_rw(struct drbd_conf *mdev, int rw, unsigned flags, unsigned lazy_w
                if (lazy_writeout_upper_idx && i == lazy_writeout_upper_idx)
                        break;
                if (rw & WRITE) {
+                       if ((flags & BM_AIO_WRITE_HINTED) &&
+                           !test_and_clear_bit(BM_PAGE_HINT_WRITEOUT,
+                                   &page_private(b->bm_pages[i])))
+                               continue;
+
                        if (!(flags & BM_WRITE_ALL_PAGES) &&
                            bm_test_page_unchanged(b->bm_pages[i])) {
                                dynamic_dev_dbg(DEV, "skipped bm write for idx %u\n", i);
@@ -1088,13 +1121,15 @@ static int bm_rw(struct drbd_conf *mdev, int rw, unsigned flags, unsigned lazy_w
         * "in_flight reached zero, all done" event.
         */
        if (!atomic_dec_and_test(&ctx->in_flight))
-               wait_until_done_or_disk_failure(mdev, mdev->ldev, &ctx->done);
+               wait_until_done_or_force_detached(mdev, mdev->ldev, &ctx->done);
        else
                kref_put(&ctx->kref, &bm_aio_ctx_destroy);
 
-       dev_info(DEV, "bitmap %s of %u pages took %lu jiffies\n",
-                       rw == WRITE ? "WRITE" : "READ",
-                       count, jiffies - now);
+       /* summary for global bitmap IO */
+       if (flags == 0)
+               dev_info(DEV, "bitmap %s of %u pages took %lu jiffies\n",
+                        rw == WRITE ? "WRITE" : "READ",
+                        count, jiffies - now);
 
        if (ctx->error) {
                dev_alert(DEV, "we had at least one MD IO ERROR during bitmap IO\n");
@@ -1103,7 +1138,7 @@ static int bm_rw(struct drbd_conf *mdev, int rw, unsigned flags, unsigned lazy_w
        }
 
        if (atomic_read(&ctx->in_flight))
-               err = -EIO; /* Disk failed during IO... */
+               err = -EIO; /* Disk timeout/force-detach during IO... */
 
        now = jiffies;
        if (rw == WRITE) {
@@ -1115,8 +1150,9 @@ static int bm_rw(struct drbd_conf *mdev, int rw, unsigned flags, unsigned lazy_w
        }
        now = b->bm_set;
 
-       dev_info(DEV, "%s (%lu bits) marked out-of-sync by on disk bit-map.\n",
-            ppsize(ppb, now << (BM_BLOCK_SHIFT-10)), now);
+       if (flags == 0)
+               dev_info(DEV, "%s (%lu bits) marked out-of-sync by on disk bit-map.\n",
+                    ppsize(ppb, now << (BM_BLOCK_SHIFT-10)), now);
 
        kref_put(&ctx->kref, &bm_aio_ctx_destroy);
        return err;
@@ -1179,9 +1215,17 @@ int drbd_bm_write_copy_pages(struct drbd_conf *mdev) __must_hold(local)
        return bm_rw(mdev, WRITE, BM_AIO_COPY_PAGES, 0);
 }
 
+/**
+ * drbd_bm_write_hinted() - Write bitmap pages with "hint" marks, if they have changed.
+ * @mdev:      DRBD device.
+ */
+int drbd_bm_write_hinted(struct drbd_conf *mdev) __must_hold(local)
+{
+       return bm_rw(mdev, WRITE, BM_AIO_WRITE_HINTED | BM_AIO_COPY_PAGES, 0);
+}
 
 /**
- * drbd_bm_write_page: Writes a PAGE_SIZE aligned piece of bitmap
+ * drbd_bm_write_page() - Writes a PAGE_SIZE aligned piece of bitmap
  * @mdev:      DRBD device.
  * @idx:       bitmap page index
  *
@@ -1222,11 +1266,11 @@ int drbd_bm_write_page(struct drbd_conf *mdev, unsigned int idx) __must_hold(loc
        }
 
        bm_page_io_async(ctx, idx, WRITE_SYNC);
-       wait_until_done_or_disk_failure(mdev, mdev->ldev, &ctx->done);
+       wait_until_done_or_force_detached(mdev, mdev->ldev, &ctx->done);
 
        if (ctx->error)
                drbd_chk_io_error(mdev, 1, DRBD_META_IO_ERROR);
-               /* that should force detach, so the in memory bitmap will be
+               /* that causes us to detach, so the in memory bitmap will be
                 * gone in a moment as well. */
 
        mdev->bm_writ_cnt++;
@@ -1289,8 +1333,10 @@ static unsigned long bm_find_next(struct drbd_conf *mdev,
        struct drbd_bitmap *b = mdev->bitmap;
        unsigned long i = DRBD_END_OF_BITMAP;
 
-       ERR_IF(!b) return i;
-       ERR_IF(!b->bm_pages) return i;
+       if (!expect(b))
+               return i;
+       if (!expect(b->bm_pages))
+               return i;
 
        spin_lock_irq(&b->bm_lock);
        if (BM_DONT_TEST & b->bm_flags)
@@ -1391,8 +1437,10 @@ static int bm_change_bits_to(struct drbd_conf *mdev, const unsigned long s,
        struct drbd_bitmap *b = mdev->bitmap;
        int c = 0;
 
-       ERR_IF(!b) return 1;
-       ERR_IF(!b->bm_pages) return 0;
+       if (!expect(b))
+               return 1;
+       if (!expect(b->bm_pages))
+               return 0;
 
        spin_lock_irqsave(&b->bm_lock, flags);
        if ((val ? BM_DONT_SET : BM_DONT_CLEAR) & b->bm_flags)
@@ -1423,13 +1471,21 @@ static inline void bm_set_full_words_within_one_page(struct drbd_bitmap *b,
 {
        int i;
        int bits;
+       int changed = 0;
        unsigned long *paddr = kmap_atomic(b->bm_pages[page_nr]);
        for (i = first_word; i < last_word; i++) {
                bits = hweight_long(paddr[i]);
                paddr[i] = ~0UL;
-               b->bm_set += BITS_PER_LONG - bits;
+               changed += BITS_PER_LONG - bits;
        }
        kunmap_atomic(paddr);
+       if (changed) {
+               /* We only need lazy writeout, the information is still in the
+                * remote bitmap as well, and is reconstructed during the next
+                * bitmap exchange, if lost locally due to a crash. */
+               bm_set_page_lazy_writeout(b->bm_pages[page_nr]);
+               b->bm_set += changed;
+       }
 }
 
 /* Same thing as drbd_bm_set_bits,
@@ -1524,8 +1580,10 @@ int drbd_bm_test_bit(struct drbd_conf *mdev, const unsigned long bitnr)
        unsigned long *p_addr;
        int i;
 
-       ERR_IF(!b) return 0;
-       ERR_IF(!b->bm_pages) return 0;
+       if (!expect(b))
+               return 0;
+       if (!expect(b->bm_pages))
+               return 0;
 
        spin_lock_irqsave(&b->bm_lock, flags);
        if (BM_DONT_TEST & b->bm_flags)
@@ -1559,8 +1617,10 @@ int drbd_bm_count_bits(struct drbd_conf *mdev, const unsigned long s, const unsi
         * robust in case we screwed up elsewhere, in that case pretend there
         * was one dirty bit in the requested area, so we won't try to do a
         * local read there (no bitmap probably implies no disk) */
-       ERR_IF(!b) return 1;
-       ERR_IF(!b->bm_pages) return 1;
+       if (!expect(b))
+               return 1;
+       if (!expect(b->bm_pages))
+               return 1;
 
        spin_lock_irqsave(&b->bm_lock, flags);
        if (BM_DONT_TEST & b->bm_flags)
@@ -1573,11 +1633,10 @@ int drbd_bm_count_bits(struct drbd_conf *mdev, const unsigned long s, const unsi
                                bm_unmap(p_addr);
                        p_addr = bm_map_pidx(b, idx);
                }
-               ERR_IF (bitnr >= b->bm_bits) {
-                       dev_err(DEV, "bitnr=%lu bm_bits=%lu\n", bitnr, b->bm_bits);
-               } else {
+               if (expect(bitnr < b->bm_bits))
                        c += (0 != test_bit_le(bitnr - (page_nr << (PAGE_SHIFT+3)), p_addr));
-               }
+               else
+                       dev_err(DEV, "bitnr=%lu bm_bits=%lu\n", bitnr, b->bm_bits);
        }
        if (p_addr)
                bm_unmap(p_addr);
@@ -1607,8 +1666,10 @@ int drbd_bm_e_weight(struct drbd_conf *mdev, unsigned long enr)
        unsigned long flags;
        unsigned long *p_addr, *bm;
 
-       ERR_IF(!b) return 0;
-       ERR_IF(!b->bm_pages) return 0;
+       if (!expect(b))
+               return 0;
+       if (!expect(b->bm_pages))
+               return 0;
 
        spin_lock_irqsave(&b->bm_lock, flags);
        if (BM_DONT_TEST & b->bm_flags)
@@ -1630,47 +1691,3 @@ int drbd_bm_e_weight(struct drbd_conf *mdev, unsigned long enr)
        spin_unlock_irqrestore(&b->bm_lock, flags);
        return count;
 }
-
-/* Set all bits covered by the AL-extent al_enr.
- * Returns number of bits changed. */
-unsigned long drbd_bm_ALe_set_all(struct drbd_conf *mdev, unsigned long al_enr)
-{
-       struct drbd_bitmap *b = mdev->bitmap;
-       unsigned long *p_addr, *bm;
-       unsigned long weight;
-       unsigned long s, e;
-       int count, i, do_now;
-       ERR_IF(!b) return 0;
-       ERR_IF(!b->bm_pages) return 0;
-
-       spin_lock_irq(&b->bm_lock);
-       if (BM_DONT_SET & b->bm_flags)
-               bm_print_lock_info(mdev);
-       weight = b->bm_set;
-
-       s = al_enr * BM_WORDS_PER_AL_EXT;
-       e = min_t(size_t, s + BM_WORDS_PER_AL_EXT, b->bm_words);
-       /* assert that s and e are on the same page */
-       D_ASSERT((e-1) >> (PAGE_SHIFT - LN2_BPL + 3)
-             ==  s    >> (PAGE_SHIFT - LN2_BPL + 3));
-       count = 0;
-       if (s < b->bm_words) {
-               i = do_now = e-s;
-               p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, s));
-               bm = p_addr + MLPP(s);
-               while (i--) {
-                       count += hweight_long(*bm);
-                       *bm = -1UL;
-                       bm++;
-               }
-               bm_unmap(p_addr);
-               b->bm_set += do_now*BITS_PER_LONG - count;
-               if (e == b->bm_words)
-                       b->bm_set -= bm_clear_surplus(b);
-       } else {
-               dev_err(DEV, "start offset (%lu) too large in drbd_bm_ALe_set_all\n", s);
-       }
-       weight = b->bm_set - weight;
-       spin_unlock_irq(&b->bm_lock);
-       return weight;
-}
index b953cc7c9c00ce4fb13885f2631211c6046ed58d..6b51afa1aae19070d3ec26cf53d784ed71fab3d7 100644 (file)
 #include <linux/major.h>
 #include <linux/blkdev.h>
 #include <linux/genhd.h>
+#include <linux/idr.h>
 #include <net/tcp.h>
 #include <linux/lru_cache.h>
 #include <linux/prefetch.h>
+#include <linux/drbd_genl_api.h>
+#include <linux/drbd.h>
+#include "drbd_state.h"
 
 #ifdef __CHECKER__
 # define __protected_by(x)       __attribute__((require_context(x,1,999,"rdwr")))
@@ -61,7 +65,6 @@
 extern unsigned int minor_count;
 extern bool disable_sendpage;
 extern bool allow_oos;
-extern unsigned int cn_idx;
 
 #ifdef CONFIG_DRBD_FAULT_INJECTION
 extern int enable_faults;
@@ -86,34 +89,44 @@ extern char usermode_helper[];
  */
 #define DRBD_SIGKILL SIGHUP
 
-/* All EEs on the free list should have ID_VACANT (== 0)
- * freshly allocated EEs get !ID_VACANT (== 1)
- * so if it says "cannot dereference null pointer at address 0x00000001",
- * it is most likely one of these :( */
-
 #define ID_IN_SYNC      (4711ULL)
 #define ID_OUT_OF_SYNC  (4712ULL)
-
 #define ID_SYNCER (-1ULL)
-#define ID_VACANT 0
-#define is_syncer_block_id(id) ((id) == ID_SYNCER)
+
 #define UUID_NEW_BM_OFFSET ((u64)0x0001000000000000ULL)
 
 struct drbd_conf;
+struct drbd_tconn;
 
 
 /* to shorten dev_warn(DEV, "msg"); and relatives statements */
 #define DEV (disk_to_dev(mdev->vdisk))
 
+#define conn_printk(LEVEL, TCONN, FMT, ARGS...) \
+       printk(LEVEL "d-con %s: " FMT, TCONN->name , ## ARGS)
+#define conn_alert(TCONN, FMT, ARGS...)  conn_printk(KERN_ALERT, TCONN, FMT, ## ARGS)
+#define conn_crit(TCONN, FMT, ARGS...)   conn_printk(KERN_CRIT, TCONN, FMT, ## ARGS)
+#define conn_err(TCONN, FMT, ARGS...)    conn_printk(KERN_ERR, TCONN, FMT, ## ARGS)
+#define conn_warn(TCONN, FMT, ARGS...)   conn_printk(KERN_WARNING, TCONN, FMT, ## ARGS)
+#define conn_notice(TCONN, FMT, ARGS...) conn_printk(KERN_NOTICE, TCONN, FMT, ## ARGS)
+#define conn_info(TCONN, FMT, ARGS...)   conn_printk(KERN_INFO, TCONN, FMT, ## ARGS)
+#define conn_dbg(TCONN, FMT, ARGS...)    conn_printk(KERN_DEBUG, TCONN, FMT, ## ARGS)
+
 #define D_ASSERT(exp)  if (!(exp)) \
         dev_err(DEV, "ASSERT( " #exp " ) in %s:%d\n", __FILE__, __LINE__)
 
-#define ERR_IF(exp) if (({                                             \
-       int _b = (exp) != 0;                                            \
-       if (_b) dev_err(DEV, "ASSERT FAILED: %s: (%s) in %s:%d\n",      \
-                       __func__, #exp, __FILE__, __LINE__);            \
-       _b;                                                             \
-       }))
+/**
+ * expect  -  Make an assertion
+ *
+ * Unlike the assert macro, this macro returns a boolean result.
+ */
+#define expect(exp) ({                                                         \
+               bool _bool = (exp);                                             \
+               if (!_bool)                                                     \
+                       dev_err(DEV, "ASSERTION %s FAILED in %s\n",             \
+                               #exp, __func__);                                \
+               _bool;                                                          \
+               })
 
 /* Defines to control fault insertion */
 enum {
@@ -150,15 +163,12 @@ drbd_insert_fault(struct drbd_conf *mdev, unsigned int type) {
 /* usual integer division */
 #define div_floor(A, B) ((A)/(B))
 
-/* drbd_meta-data.c (still in drbd_main.c) */
-/* 4th incarnation of the disk layout. */
-#define DRBD_MD_MAGIC (DRBD_MAGIC+4)
-
-extern struct drbd_conf **minor_table;
 extern struct ratelimit_state drbd_ratelimit_state;
+extern struct idr minors; /* RCU, updates: genl_lock() */
+extern struct list_head drbd_tconns; /* RCU, updates: genl_lock() */
 
 /* on the wire */
-enum drbd_packets {
+enum drbd_packet {
        /* receiver (data socket) */
        P_DATA                = 0x00,
        P_DATA_REPLY          = 0x01, /* Response to P_DATA_REQUEST */
@@ -186,7 +196,7 @@ enum drbd_packets {
        P_RECV_ACK            = 0x15, /* Used in protocol B */
        P_WRITE_ACK           = 0x16, /* Used in protocol C */
        P_RS_WRITE_ACK        = 0x17, /* Is a P_WRITE_ACK, additionally call set_in_sync(). */
-       P_DISCARD_ACK         = 0x18, /* Used in proto C, two-primaries conflict detection */
+       P_SUPERSEDED          = 0x18, /* Used in proto C, two-primaries conflict detection */
        P_NEG_ACK             = 0x19, /* Sent if local disk is unusable */
        P_NEG_DREPLY          = 0x1a, /* Local disk is broken... */
        P_NEG_RS_DREPLY       = 0x1b, /* Local disk is broken... */
@@ -207,77 +217,23 @@ enum drbd_packets {
        P_DELAY_PROBE         = 0x27, /* is used on BOTH sockets */
        P_OUT_OF_SYNC         = 0x28, /* Mark as out of sync (Outrunning), data socket */
        P_RS_CANCEL           = 0x29, /* meta: Used to cancel RS_DATA_REQUEST packet by SyncSource */
+       P_CONN_ST_CHG_REQ     = 0x2a, /* data sock: Connection wide state request */
+       P_CONN_ST_CHG_REPLY   = 0x2b, /* meta sock: Connection side state req reply */
+       P_RETRY_WRITE         = 0x2c, /* Protocol C: retry conflicting write request */
+       P_PROTOCOL_UPDATE     = 0x2d, /* data sock: is used in established connections */
 
-       P_MAX_CMD             = 0x2A,
        P_MAY_IGNORE          = 0x100, /* Flag to test if (cmd > P_MAY_IGNORE) ... */
        P_MAX_OPT_CMD         = 0x101,
 
        /* special command ids for handshake */
 
-       P_HAND_SHAKE_M        = 0xfff1, /* First Packet on the MetaSock */
-       P_HAND_SHAKE_S        = 0xfff2, /* First Packet on the Socket */
+       P_INITIAL_META        = 0xfff1, /* First Packet on the MetaSock */
+       P_INITIAL_DATA        = 0xfff2, /* First Packet on the Socket */
 
-       P_HAND_SHAKE          = 0xfffe  /* FIXED for the next century! */
+       P_CONNECTION_FEATURES = 0xfffe  /* FIXED for the next century! */
 };
 
-static inline const char *cmdname(enum drbd_packets cmd)
-{
-       /* THINK may need to become several global tables
-        * when we want to support more than
-        * one PRO_VERSION */
-       static const char *cmdnames[] = {
-               [P_DATA]                = "Data",
-               [P_DATA_REPLY]          = "DataReply",
-               [P_RS_DATA_REPLY]       = "RSDataReply",
-               [P_BARRIER]             = "Barrier",
-               [P_BITMAP]              = "ReportBitMap",
-               [P_BECOME_SYNC_TARGET]  = "BecomeSyncTarget",
-               [P_BECOME_SYNC_SOURCE]  = "BecomeSyncSource",
-               [P_UNPLUG_REMOTE]       = "UnplugRemote",
-               [P_DATA_REQUEST]        = "DataRequest",
-               [P_RS_DATA_REQUEST]     = "RSDataRequest",
-               [P_SYNC_PARAM]          = "SyncParam",
-               [P_SYNC_PARAM89]        = "SyncParam89",
-               [P_PROTOCOL]            = "ReportProtocol",
-               [P_UUIDS]               = "ReportUUIDs",
-               [P_SIZES]               = "ReportSizes",
-               [P_STATE]               = "ReportState",
-               [P_SYNC_UUID]           = "ReportSyncUUID",
-               [P_AUTH_CHALLENGE]      = "AuthChallenge",
-               [P_AUTH_RESPONSE]       = "AuthResponse",
-               [P_PING]                = "Ping",
-               [P_PING_ACK]            = "PingAck",
-               [P_RECV_ACK]            = "RecvAck",
-               [P_WRITE_ACK]           = "WriteAck",
-               [P_RS_WRITE_ACK]        = "RSWriteAck",
-               [P_DISCARD_ACK]         = "DiscardAck",
-               [P_NEG_ACK]             = "NegAck",
-               [P_NEG_DREPLY]          = "NegDReply",
-               [P_NEG_RS_DREPLY]       = "NegRSDReply",
-               [P_BARRIER_ACK]         = "BarrierAck",
-               [P_STATE_CHG_REQ]       = "StateChgRequest",
-               [P_STATE_CHG_REPLY]     = "StateChgReply",
-               [P_OV_REQUEST]          = "OVRequest",
-               [P_OV_REPLY]            = "OVReply",
-               [P_OV_RESULT]           = "OVResult",
-               [P_CSUM_RS_REQUEST]     = "CsumRSRequest",
-               [P_RS_IS_IN_SYNC]       = "CsumRSIsInSync",
-               [P_COMPRESSED_BITMAP]   = "CBitmap",
-               [P_DELAY_PROBE]         = "DelayProbe",
-               [P_OUT_OF_SYNC]         = "OutOfSync",
-               [P_MAX_CMD]             = NULL,
-       };
-
-       if (cmd == P_HAND_SHAKE_M)
-               return "HandShakeM";
-       if (cmd == P_HAND_SHAKE_S)
-               return "HandShakeS";
-       if (cmd == P_HAND_SHAKE)
-               return "HandShake";
-       if (cmd >= P_MAX_CMD)
-               return "Unknown";
-       return cmdnames[cmd];
-}
+extern const char *cmdname(enum drbd_packet cmd);
 
 /* for sending/receiving the bitmap,
  * possibly in some encoding scheme */
@@ -337,37 +293,24 @@ struct p_header80 {
        u32       magic;
        u16       command;
        u16       length;       /* bytes of data after this header */
-       u8        payload[0];
 } __packed;
 
 /* Header for big packets, Used for data packets exceeding 64kB */
 struct p_header95 {
        u16       magic;        /* use DRBD_MAGIC_BIG here */
        u16       command;
-       u32       length;       /* Use only 24 bits of that. Ignore the highest 8 bit. */
-       u8        payload[0];
+       u32       length;
 } __packed;
 
-union p_header {
-       struct p_header80 h80;
-       struct p_header95 h95;
-};
-
-/*
- * short commands, packets without payload, plain p_header:
- *   P_PING
- *   P_PING_ACK
- *   P_BECOME_SYNC_TARGET
- *   P_BECOME_SYNC_SOURCE
- *   P_UNPLUG_REMOTE
- */
+struct p_header100 {
+       u32       magic;
+       u16       volume;
+       u16       command;
+       u32       length;
+       u32       pad;
+} __packed;
 
-/*
- * commands with out-of-struct payload:
- *   P_BITMAP    (no additional fields)
- *   P_DATA, P_DATA_REPLY (see p_data)
- *   P_COMPRESSED_BITMAP (see receive_compressed_bitmap)
- */
+extern unsigned int drbd_header_size(struct drbd_tconn *tconn);
 
 /* these defines must not be changed without changing the protocol version */
 #define DP_HARDBARRIER       1 /* depricated */
@@ -377,9 +320,10 @@ union p_header {
 #define DP_FUA               16 /* equals REQ_FUA     */
 #define DP_FLUSH             32 /* equals REQ_FLUSH   */
 #define DP_DISCARD           64 /* equals REQ_DISCARD */
+#define DP_SEND_RECEIVE_ACK 128 /* This is a proto B write request */
+#define DP_SEND_WRITE_ACK   256 /* This is a proto C write request */
 
 struct p_data {
-       union p_header head;
        u64         sector;    /* 64 bits sector number */
        u64         block_id;  /* to identify the request in protocol B&C */
        u32         seq_num;
@@ -390,21 +334,18 @@ struct p_data {
  * commands which share a struct:
  *  p_block_ack:
  *   P_RECV_ACK (proto B), P_WRITE_ACK (proto C),
- *   P_DISCARD_ACK (proto C, two-primaries conflict detection)
+ *   P_SUPERSEDED (proto C, two-primaries conflict detection)
  *  p_block_req:
  *   P_DATA_REQUEST, P_RS_DATA_REQUEST
  */
 struct p_block_ack {
-       struct p_header80 head;
        u64         sector;
        u64         block_id;
        u32         blksize;
        u32         seq_num;
 } __packed;
 
-
 struct p_block_req {
-       struct p_header80 head;
        u64 sector;
        u64 block_id;
        u32 blksize;
@@ -413,59 +354,52 @@ struct p_block_req {
 
 /*
  * commands with their own struct for additional fields:
- *   P_HAND_SHAKE
+ *   P_CONNECTION_FEATURES
  *   P_BARRIER
  *   P_BARRIER_ACK
  *   P_SYNC_PARAM
  *   ReportParams
  */
 
-struct p_handshake {
-       struct p_header80 head; /* 8 bytes */
+struct p_connection_features {
        u32 protocol_min;
        u32 feature_flags;
        u32 protocol_max;
 
        /* should be more than enough for future enhancements
-        * for now, feature_flags and the reserverd array shall be zero.
+        * for now, feature_flags and the reserved array shall be zero.
         */
 
        u32 _pad;
-       u64 reserverd[7];
+       u64 reserved[7];
 } __packed;
-/* 80 bytes, FIXED for the next century */
 
 struct p_barrier {
-       struct p_header80 head;
        u32 barrier;    /* barrier number _handle_ only */
        u32 pad;        /* to multiple of 8 Byte */
 } __packed;
 
 struct p_barrier_ack {
-       struct p_header80 head;
        u32 barrier;
        u32 set_size;
 } __packed;
 
 struct p_rs_param {
-       struct p_header80 head;
-       u32 rate;
+       u32 resync_rate;
 
              /* Since protocol version 88 and higher. */
        char verify_alg[0];
 } __packed;
 
 struct p_rs_param_89 {
-       struct p_header80 head;
-       u32 rate;
+       u32 resync_rate;
         /* protocol version 89: */
        char verify_alg[SHARED_SECRET_MAX];
        char csums_alg[SHARED_SECRET_MAX];
 } __packed;
 
 struct p_rs_param_95 {
-       struct p_header80 head;
-       u32 rate;
+       u32 resync_rate;
        char verify_alg[SHARED_SECRET_MAX];
        char csums_alg[SHARED_SECRET_MAX];
        u32 c_plan_ahead;
@@ -475,12 +409,11 @@ struct p_rs_param_95 {
 } __packed;
 
 enum drbd_conn_flags {
-       CF_WANT_LOSE = 1,
+       CF_DISCARD_MY_DATA = 1,
        CF_DRY_RUN = 2,
 };
 
 struct p_protocol {
-       struct p_header80 head;
        u32 protocol;
        u32 after_sb_0p;
        u32 after_sb_1p;
@@ -494,17 +427,14 @@ struct p_protocol {
 } __packed;
 
 struct p_uuids {
-       struct p_header80 head;
        u64 uuid[UI_EXTENDED_SIZE];
 } __packed;
 
 struct p_rs_uuid {
-       struct p_header80 head;
        u64         uuid;
 } __packed;
 
 struct p_sizes {
-       struct p_header80 head;
        u64         d_size;  /* size of disk */
        u64         u_size;  /* user requested size */
        u64         c_size;  /* current exported size */
@@ -514,18 +444,15 @@ struct p_sizes {
 } __packed;
 
 struct p_state {
-       struct p_header80 head;
        u32         state;
 } __packed;
 
 struct p_req_state {
-       struct p_header80 head;
        u32         mask;
        u32         val;
 } __packed;
 
 struct p_req_state_reply {
-       struct p_header80 head;
        u32         retcode;
 } __packed;
 
@@ -539,15 +466,7 @@ struct p_drbd06_param {
        u32       bit_map_gen[5];
 } __packed;
 
-struct p_discard {
-       struct p_header80 head;
-       u64         block_id;
-       u32         seq_num;
-       u32         pad;
-} __packed;
-
 struct p_block_desc {
-       struct p_header80 head;
        u64 sector;
        u32 blksize;
        u32 pad;        /* to multiple of 8 Byte */
@@ -563,7 +482,6 @@ enum drbd_bitmap_code {
 };
 
 struct p_compressed_bm {
-       struct p_header80 head;
        /* (encoding & 0x0f): actual encoding, see enum drbd_bitmap_code
         * (encoding & 0x80): polarity (set/unset) of first runlength
         * ((encoding >> 4) & 0x07): pad_bits, number of trailing zero bits
@@ -575,90 +493,22 @@ struct p_compressed_bm {
 } __packed;
 
 struct p_delay_probe93 {
-       struct p_header80 head;
        u32     seq_num; /* sequence number to match the two probe packets */
        u32     offset;  /* usecs the probe got sent after the reference time point */
 } __packed;
 
-/* DCBP: Drbd Compressed Bitmap Packet ... */
-static inline enum drbd_bitmap_code
-DCBP_get_code(struct p_compressed_bm *p)
-{
-       return (enum drbd_bitmap_code)(p->encoding & 0x0f);
-}
-
-static inline void
-DCBP_set_code(struct p_compressed_bm *p, enum drbd_bitmap_code code)
-{
-       BUG_ON(code & ~0xf);
-       p->encoding = (p->encoding & ~0xf) | code;
-}
-
-static inline int
-DCBP_get_start(struct p_compressed_bm *p)
-{
-       return (p->encoding & 0x80) != 0;
-}
-
-static inline void
-DCBP_set_start(struct p_compressed_bm *p, int set)
-{
-       p->encoding = (p->encoding & ~0x80) | (set ? 0x80 : 0);
-}
-
-static inline int
-DCBP_get_pad_bits(struct p_compressed_bm *p)
-{
-       return (p->encoding >> 4) & 0x7;
-}
-
-static inline void
-DCBP_set_pad_bits(struct p_compressed_bm *p, int n)
-{
-       BUG_ON(n & ~0x7);
-       p->encoding = (p->encoding & (~0x7 << 4)) | (n << 4);
-}
-
-/* one bitmap packet, including the p_header,
- * should fit within one _architecture independend_ page.
- * so we need to use the fixed size 4KiB page size
- * most architectures have used for a long time.
+/*
+ * Bitmap packets need to fit within a single page on the sender and receiver,
+ * so we are limited to 4 KiB (and not to PAGE_SIZE, which can be bigger).
  */
-#define BM_PACKET_PAYLOAD_BYTES (4096 - sizeof(struct p_header80))
-#define BM_PACKET_WORDS (BM_PACKET_PAYLOAD_BYTES/sizeof(long))
-#define BM_PACKET_VLI_BYTES_MAX (4096 - sizeof(struct p_compressed_bm))
-#if (PAGE_SIZE < 4096)
-/* drbd_send_bitmap / receive_bitmap would break horribly */
-#error "PAGE_SIZE too small"
-#endif
-
-union p_polymorph {
-        union p_header           header;
-        struct p_handshake       handshake;
-        struct p_data            data;
-        struct p_block_ack       block_ack;
-        struct p_barrier         barrier;
-        struct p_barrier_ack     barrier_ack;
-        struct p_rs_param_89     rs_param_89;
-        struct p_rs_param_95     rs_param_95;
-        struct p_protocol        protocol;
-        struct p_sizes           sizes;
-        struct p_uuids           uuids;
-        struct p_state           state;
-        struct p_req_state       req_state;
-        struct p_req_state_reply req_state_reply;
-        struct p_block_req       block_req;
-       struct p_delay_probe93   delay_probe93;
-       struct p_rs_uuid         rs_uuid;
-       struct p_block_desc      block_desc;
-} __packed;
+#define DRBD_SOCKET_BUFFER_SIZE 4096
 
 /**********************************************************************/
 enum drbd_thread_state {
-       None,
-       Running,
-       Exiting,
-       Restarting
+       NONE,
+       RUNNING,
+       EXITING,
+       RESTARTING
 };
 
 struct drbd_thread {
@@ -667,8 +517,9 @@ struct drbd_thread {
        struct completion stop;
        enum drbd_thread_state t_state;
        int (*function) (struct drbd_thread *);
-       struct drbd_conf *mdev;
+       struct drbd_tconn *tconn;
        int reset_cpu_mask;
+       char name[9];
 };
 
 static inline enum drbd_thread_state get_t_state(struct drbd_thread *thi)
@@ -681,58 +532,54 @@ static inline enum drbd_thread_state get_t_state(struct drbd_thread *thi)
        return thi->t_state;
 }
 
-struct drbd_work;
-typedef int (*drbd_work_cb)(struct drbd_conf *, struct drbd_work *, int cancel);
 struct drbd_work {
        struct list_head list;
-       drbd_work_cb cb;
+       int (*cb)(struct drbd_work *, int cancel);
+       union {
+               struct drbd_conf *mdev;
+               struct drbd_tconn *tconn;
+       };
 };
 
-struct drbd_tl_epoch;
+#include "drbd_interval.h"
+
+extern int drbd_wait_misc(struct drbd_conf *, struct drbd_interval *);
+
 struct drbd_request {
        struct drbd_work w;
-       struct drbd_conf *mdev;
 
        /* if local IO is not allowed, will be NULL.
         * if local IO _is_ allowed, holds the locally submitted bio clone,
         * or, after local IO completion, the ERR_PTR(error).
-        * see drbd_endio_pri(). */
+        * see drbd_request_endio(). */
        struct bio *private_bio;
 
-       struct hlist_node collision;
-       sector_t sector;
-       unsigned int size;
-       unsigned int epoch; /* barrier_nr */
+       struct drbd_interval i;
 
-       /* barrier_nr: used to check on "completion" whether this req was in
+       /* epoch: used to check on "completion" whether this req was in
         * the current epoch, and we therefore have to close it,
-        * starting a new epoch...
+        * causing a p_barrier packet to be send, starting a new epoch.
+        *
+        * This corresponds to "barrier" in struct p_barrier[_ack],
+        * and to "barrier_nr" in struct drbd_epoch (and various
+        * comments/function parameters/local variable names).
         */
+       unsigned int epoch;
 
        struct list_head tl_requests; /* ring list in the transfer log */
        struct bio *master_bio;       /* master bio pointer */
-       unsigned long rq_state; /* see comments above _req_mod() */
        unsigned long start_time;
-};
-
-struct drbd_tl_epoch {
-       struct drbd_work w;
-       struct list_head requests; /* requests before */
-       struct drbd_tl_epoch *next; /* pointer to the next barrier */
-       unsigned int br_number;  /* the barriers identifier. */
-       int n_writes;   /* number of requests attached before this barrier */
-};
 
-struct drbd_request;
+       /* once it hits 0, we may complete the master_bio */
+       atomic_t completion_ref;
+       /* once it hits 0, we may destroy this drbd_request object */
+       struct kref kref;
 
-/* These Tl_epoch_entries may be in one of 6 lists:
-   active_ee .. data packet being written
-   sync_ee   .. syncer block being written
-   done_ee   .. block written, need to send P_WRITE_ACK
-   read_ee   .. [RS]P_DATA_REQUEST being read
-*/
+       unsigned rq_state; /* see comments above _req_mod() */
+};
 
 struct drbd_epoch {
+       struct drbd_tconn *tconn;
        struct list_head list;
        unsigned int barrier_nr;
        atomic_t epoch_size; /* increased on every request added. */
@@ -762,17 +609,14 @@ struct digest_info {
        void *digest;
 };
 
-struct drbd_epoch_entry {
+struct drbd_peer_request {
        struct drbd_work w;
-       struct hlist_node collision;
        struct drbd_epoch *epoch; /* for writes */
-       struct drbd_conf *mdev;
        struct page *pages;
        atomic_t pending_bios;
-       unsigned int size;
+       struct drbd_interval i;
        /* see comments on ee flag bits below */
        unsigned long flags;
-       sector_t sector;
        union {
                u64 block_id;
                struct digest_info *digest;
@@ -793,31 +637,37 @@ enum {
         * we need to resubmit without the barrier flag. */
        __EE_RESUBMITTED,
 
-       /* we may have several bios per epoch entry.
+       /* we may have several bios per peer request.
         * if any of those fail, we set this flag atomically
         * from the endio callback */
        __EE_WAS_ERROR,
 
        /* This ee has a pointer to a digest instead of a block id */
        __EE_HAS_DIGEST,
+
+       /* Conflicting local requests need to be restarted after this request */
+       __EE_RESTART_REQUESTS,
+
+       /* The peer wants a write ACK for this (wire proto C) */
+       __EE_SEND_WRITE_ACK,
+
+       /* Is set when net_conf had two_primaries set while creating this peer_req */
+       __EE_IN_INTERVAL_TREE,
 };
 #define EE_CALL_AL_COMPLETE_IO (1<<__EE_CALL_AL_COMPLETE_IO)
 #define EE_MAY_SET_IN_SYNC     (1<<__EE_MAY_SET_IN_SYNC)
 #define        EE_RESUBMITTED         (1<<__EE_RESUBMITTED)
 #define EE_WAS_ERROR           (1<<__EE_WAS_ERROR)
 #define EE_HAS_DIGEST          (1<<__EE_HAS_DIGEST)
+#define EE_RESTART_REQUESTS    (1<<__EE_RESTART_REQUESTS)
+#define EE_SEND_WRITE_ACK      (1<<__EE_SEND_WRITE_ACK)
+#define EE_IN_INTERVAL_TREE    (1<<__EE_IN_INTERVAL_TREE)
 
-/* global flag bits */
+/* flag bits per mdev */
 enum {
-       CREATE_BARRIER,         /* next P_DATA is preceded by a P_BARRIER */
-       SIGNAL_ASENDER,         /* whether asender wants to be interrupted */
-       SEND_PING,              /* whether asender should send a ping asap */
-
        UNPLUG_REMOTE,          /* sending a "UnplugRemote" could help */
        MD_DIRTY,               /* current uuids and flags not yet on disk */
-       DISCARD_CONCURRENT,     /* Set on one node, cleared on the peer! */
        USE_DEGR_WFC_T,         /* degr-wfc-timeout instead of wfc-timeout. */
-       CLUSTER_ST_CHANGE,      /* Cluster wide state change going on... */
        CL_ST_CHG_SUCCESS,
        CL_ST_CHG_FAIL,
        CRASHED_PRIMARY,        /* This node was a crashed primary.
@@ -831,32 +681,18 @@ enum {
                                   once no more io in flight, start bitmap io */
        BITMAP_IO_QUEUED,       /* Started bitmap IO */
        GO_DISKLESS,            /* Disk is being detached, on io-error or admin request. */
-       WAS_IO_ERROR,           /* Local disk failed returned IO error */
+       WAS_IO_ERROR,           /* Local disk failed, returned IO error */
+       WAS_READ_ERROR,         /* Local disk READ failed (set additionally to the above) */
        FORCE_DETACH,           /* Force-detach from local disk, aborting any pending local IO */
        RESYNC_AFTER_NEG,       /* Resync after online grow after the attach&negotiate finished. */
-       NET_CONGESTED,          /* The data socket is congested */
-
-       CONFIG_PENDING,         /* serialization of (re)configuration requests.
-                                * if set, also prevents the device from dying */
-       DEVICE_DYING,           /* device became unconfigured,
-                                * but worker thread is still handling the cleanup.
-                                * reconfiguring (nl_disk_conf, nl_net_conf) is dissalowed,
-                                * while this is set. */
        RESIZE_PENDING,         /* Size change detected locally, waiting for the response from
                                 * the peer, if it changed there as well. */
-       CONN_DRY_RUN,           /* Expect disconnect after resync handshake. */
-       GOT_PING_ACK,           /* set when we receive a ping_ack packet, misc wait gets woken */
        NEW_CUR_UUID,           /* Create new current UUID when thawing IO */
        AL_SUSPENDED,           /* Activity logging is currently suspended. */
        AHEAD_TO_SYNC_SOURCE,   /* Ahead -> SyncSource queued */
-       STATE_SENT,             /* Do not change state/UUIDs while this is set */
-
-       CALLBACK_PENDING,       /* Whether we have a call_usermodehelper(, UMH_WAIT_PROC)
-                                * pending, from drbd worker context.
-                                * If set, bdi_write_congested() returns true,
-                                * so shrink_page_list() would not recurse into,
-                                * and potentially deadlock on, this drbd worker.
-                                */
+       B_RS_H_DONE,            /* Before resync handler done (already executed) */
+       DISCARD_MY_DATA,        /* discard_my_data flag per volume */
+       READ_BALANCE_RR,
 };
 
 struct drbd_bitmap; /* opaque for drbd_conf */
@@ -894,24 +730,24 @@ enum bm_flag {
 
 struct drbd_work_queue {
        struct list_head q;
-       struct semaphore s; /* producers up it, worker down()s it */
        spinlock_t q_lock;  /* to protect the list. */
+       wait_queue_head_t q_wait;
 };
 
 struct drbd_socket {
-       struct drbd_work_queue work;
        struct mutex mutex;
        struct socket    *socket;
        /* this way we get our
         * send/receive buffers off the stack */
-       union p_polymorph sbuf;
-       union p_polymorph rbuf;
+       void *sbuf;
+       void *rbuf;
 };
 
 struct drbd_md {
        u64 md_offset;          /* sector offset to 'super' block */
 
        u64 la_size_sect;       /* last agreed size, unit sectors */
+       spinlock_t uuid_lock;
        u64 uuid[UI_SIZE];
        u64 device_uuid;
        u32 flags;
@@ -921,24 +757,16 @@ struct drbd_md {
        s32 bm_offset;  /* signed relative sector offset to bitmap */
 
        /* u32 al_nr_extents;      important for restoring the AL
-        * is stored into  sync_conf.al_extents, which in turn
+        * is stored into  ldev->dc.al_extents, which in turn
         * gets applied to act_log->nr_elements
         */
 };
 
-/* for sync_conf and other types... */
-#define NL_PACKET(name, number, fields) struct name { fields };
-#define NL_INTEGER(pn,pr,member) int member;
-#define NL_INT64(pn,pr,member) __u64 member;
-#define NL_BIT(pn,pr,member)   unsigned member:1;
-#define NL_STRING(pn,pr,member,len) unsigned char member[len]; int member ## _len;
-#include <linux/drbd_nl.h>
-
 struct drbd_backing_dev {
        struct block_device *backing_bdev;
        struct block_device *md_bdev;
        struct drbd_md md;
-       struct disk_conf dc; /* The user provided config... */
+       struct disk_conf *disk_conf; /* RCU, for updates: mdev->tconn->conf_update */
        sector_t known_size; /* last known size of that backing device */
 };
 
@@ -962,18 +790,116 @@ enum write_ordering_e {
 };
 
 struct fifo_buffer {
-       int *values;
        unsigned int head_index;
        unsigned int size;
+       int total; /* sum of all values */
+       int values[0];
+};
+extern struct fifo_buffer *fifo_alloc(int fifo_size);
+
+/* flag bits per tconn */
+enum {
+       NET_CONGESTED,          /* The data socket is congested */
+       RESOLVE_CONFLICTS,      /* Set on one node, cleared on the peer! */
+       SEND_PING,              /* whether asender should send a ping asap */
+       SIGNAL_ASENDER,         /* whether asender wants to be interrupted */
+       GOT_PING_ACK,           /* set when we receive a ping_ack packet, ping_wait gets woken */
+       CONN_WD_ST_CHG_REQ,     /* A cluster wide state change on the connection is active */
+       CONN_WD_ST_CHG_OKAY,
+       CONN_WD_ST_CHG_FAIL,
+       CONN_DRY_RUN,           /* Expect disconnect after resync handshake. */
+       CREATE_BARRIER,         /* next P_DATA is preceded by a P_BARRIER */
+       STATE_SENT,             /* Do not change state/UUIDs while this is set */
+       CALLBACK_PENDING,       /* Whether we have a call_usermodehelper(, UMH_WAIT_PROC)
+                                * pending, from drbd worker context.
+                                * If set, bdi_write_congested() returns true,
+                                * so shrink_page_list() would not recurse into,
+                                * and potentially deadlock on, this drbd worker.
+                                */
+       DISCONNECT_SENT,
+};
+
+struct drbd_tconn {                    /* is a resource from the config file */
+       char *name;                     /* Resource name */
+       struct list_head all_tconn;     /* linked on global drbd_tconns */
+       struct kref kref;
+       struct idr volumes;             /* <tconn, vnr> to mdev mapping */
+       enum drbd_conns cstate;         /* Only C_STANDALONE to C_WF_REPORT_PARAMS */
+       unsigned susp:1;                /* IO suspended by user */
+       unsigned susp_nod:1;            /* IO suspended because no data */
+       unsigned susp_fen:1;            /* IO suspended because fence peer handler runs */
+       struct mutex cstate_mutex;      /* Protects graceful disconnects */
+
+       unsigned long flags;
+       struct net_conf *net_conf;      /* content protected by rcu */
+       struct mutex conf_update;       /* mutex for ready-copy-update of net_conf and disk_conf */
+       wait_queue_head_t ping_wait;    /* Woken upon reception of a ping, and a state change */
+       struct res_opts res_opts;
+
+       struct sockaddr_storage my_addr;
+       int my_addr_len;
+       struct sockaddr_storage peer_addr;
+       int peer_addr_len;
+
+       struct drbd_socket data;        /* data/barrier/cstate/parameter packets */
+       struct drbd_socket meta;        /* ping/ack (metadata) packets */
+       int agreed_pro_version;         /* actually used protocol version */
+       unsigned long last_received;    /* in jiffies, either socket */
+       unsigned int ko_count;
+
+       spinlock_t req_lock;
+
+       struct list_head transfer_log;  /* all requests not yet fully processed */
+
+       struct crypto_hash *cram_hmac_tfm;
+       struct crypto_hash *integrity_tfm;  /* checksums we compute, updates protected by tconn->data->mutex */
+       struct crypto_hash *peer_integrity_tfm;  /* checksums we verify, only accessed from receiver thread  */
+       struct crypto_hash *csums_tfm;
+       struct crypto_hash *verify_tfm;
+       void *int_dig_in;
+       void *int_dig_vv;
+
+       /* receiver side */
+       struct drbd_epoch *current_epoch;
+       spinlock_t epoch_lock;
+       unsigned int epochs;
+       enum write_ordering_e write_ordering;
+       atomic_t current_tle_nr;        /* transfer log epoch number */
+       unsigned current_tle_writes;    /* writes seen within this tl epoch */
+
+       unsigned long last_reconnect_jif;
+       struct drbd_thread receiver;
+       struct drbd_thread worker;
+       struct drbd_thread asender;
+       cpumask_var_t cpu_mask;
+
+       /* sender side */
+       struct drbd_work_queue sender_work;
+
+       struct {
+               /* whether this sender thread
+                * has processed a single write yet. */
+               bool seen_any_write_yet;
+
+               /* Which barrier number to send with the next P_BARRIER */
+               int current_epoch_nr;
+
+               /* how many write requests have been sent
+                * with req->epoch == current_epoch_nr.
+                * If none, no P_BARRIER will be sent. */
+               unsigned current_epoch_writes;
+       } send;
 };
 
 struct drbd_conf {
+       struct drbd_tconn *tconn;
+       int vnr;                        /* volume number within the connection */
+       struct kref kref;
+
        /* things that are stored as / read from meta data on disk */
        unsigned long flags;
 
        /* configured by drbdsetup */
-       struct net_conf *net_conf; /* protected by get_net_conf() and put_net_conf() */
-       struct syncer_conf sync_conf;
        struct drbd_backing_dev *ldev __protected_by(local);
 
        sector_t p_size;     /* partner's disk size */
@@ -981,11 +907,7 @@ struct drbd_conf {
        struct block_device *this_bdev;
        struct gendisk      *vdisk;
 
-       struct drbd_socket data; /* data/barrier/cstate/parameter packets */
-       struct drbd_socket meta; /* ping/ack (metadata) packets */
-       int agreed_pro_version;  /* actually used protocol version */
-       unsigned long last_received; /* in jiffies, either socket */
-       unsigned int ko_count;
+       unsigned long last_reattach_jif;
        struct drbd_work  resync_work,
                          unplug_work,
                          go_diskless,
@@ -1005,10 +927,9 @@ struct drbd_conf {
        /* Used after attach while negotiating new disk state. */
        union drbd_state new_state_tmp;
 
-       union drbd_state state;
+       union drbd_dev_state state;
        wait_queue_head_t misc_wait;
        wait_queue_head_t state_wait;  /* upon each state change. */
-       wait_queue_head_t net_cnt_wait;
        unsigned int send_cnt;
        unsigned int recv_cnt;
        unsigned int read_cnt;
@@ -1018,17 +939,12 @@ struct drbd_conf {
        atomic_t ap_bio_cnt;     /* Requests we need to complete */
        atomic_t ap_pending_cnt; /* AP data packets on the wire, ack expected */
        atomic_t rs_pending_cnt; /* RS request/data packets on the wire */
-       atomic_t unacked_cnt;    /* Need to send replys for */
+       atomic_t unacked_cnt;    /* Need to send replies for */
        atomic_t local_cnt;      /* Waiting for local completion */
-       atomic_t net_cnt;        /* Users of net_conf */
-       spinlock_t req_lock;
-       struct drbd_tl_epoch *unused_spare_tle; /* for pre-allocation */
-       struct drbd_tl_epoch *newest_tle;
-       struct drbd_tl_epoch *oldest_tle;
-       struct list_head out_of_sequence_requests;
-       struct list_head barrier_acked_requests;
-       struct hlist_head *tl_hash;
-       unsigned int tl_hash_s;
+
+       /* Interval tree of pending local requests */
+       struct rb_root read_requests;
+       struct rb_root write_requests;
 
        /* blocks to resync in this run [unit BM_BLOCK_SIZE] */
        unsigned long rs_total;
@@ -1048,9 +964,11 @@ struct drbd_conf {
        unsigned long rs_mark_time[DRBD_SYNC_MARKS];
        /* current index into rs_mark_{left,time} */
        int rs_last_mark;
+       unsigned long rs_last_bcast; /* [unit jiffies] */
 
        /* where does the admin want us to start? (sector) */
        sector_t ov_start_sector;
+       sector_t ov_stop_sector;
        /* where are we now? (sector) */
        sector_t ov_position;
        /* Start sector of out of sync range (to merge printk reporting). */
@@ -1058,14 +976,7 @@ struct drbd_conf {
        /* size of out-of-sync range in sectors. */
        sector_t ov_last_oos_size;
        unsigned long ov_left; /* in bits */
-       struct crypto_hash *csums_tfm;
-       struct crypto_hash *verify_tfm;
 
-       unsigned long last_reattach_jif;
-       unsigned long last_reconnect_jif;
-       struct drbd_thread receiver;
-       struct drbd_thread worker;
-       struct drbd_thread asender;
        struct drbd_bitmap *bitmap;
        unsigned long bm_resync_fo; /* bit offset for drbd_bm_find_next */
 
@@ -1078,29 +989,19 @@ struct drbd_conf {
 
        int open_cnt;
        u64 *p_uuid;
-       struct drbd_epoch *current_epoch;
-       spinlock_t epoch_lock;
-       unsigned int epochs;
-       enum write_ordering_e write_ordering;
+
        struct list_head active_ee; /* IO in progress (P_DATA gets written to disk) */
        struct list_head sync_ee;   /* IO in progress (P_RS_DATA_REPLY gets written to disk) */
-       struct list_head done_ee;   /* send ack */
-       struct list_head read_ee;   /* IO in progress (any read) */
+       struct list_head done_ee;   /* need to send P_WRITE_ACK */
+       struct list_head read_ee;   /* [RS]P_DATA_REQUEST being read */
        struct list_head net_ee;    /* zero-copy network send in progress */
-       struct hlist_head *ee_hash; /* is proteced by req_lock! */
-       unsigned int ee_hash_s;
-
-       /* this one is protected by ee_lock, single thread */
-       struct drbd_epoch_entry *last_write_w_barrier;
 
        int next_barrier_nr;
-       struct hlist_head *app_reads_hash; /* is proteced by req_lock */
        struct list_head resync_reads;
        atomic_t pp_in_use;             /* allocated from page pool */
        atomic_t pp_in_use_by_net;      /* sendpage()d, still referenced by tcp */
        wait_queue_head_t ee_wait;
        struct page *md_io_page;        /* one page buffer for md_io */
-       struct page *md_io_tmpp;        /* for logical_block_size != 512 */
        struct drbd_md_io md_io;
        atomic_t md_io_in_use;          /* protects the md_io, md_io_page and md_io_tmpp */
        spinlock_t al_lock;
@@ -1109,22 +1010,16 @@ struct drbd_conf {
        unsigned int al_tr_number;
        int al_tr_cycle;
        int al_tr_pos;   /* position of the next transaction in the journal */
-       struct crypto_hash *cram_hmac_tfm;
-       struct crypto_hash *integrity_w_tfm; /* to be used by the worker thread */
-       struct crypto_hash *integrity_r_tfm; /* to be used by the receiver thread */
-       void *int_dig_out;
-       void *int_dig_in;
-       void *int_dig_vv;
        wait_queue_head_t seq_wait;
        atomic_t packet_seq;
        unsigned int peer_seq;
        spinlock_t peer_seq_lock;
        unsigned int minor;
        unsigned long comm_bm_set; /* communicated number of set bits. */
-       cpumask_var_t cpu_mask;
        struct bm_io_work bm_io_work;
        u64 ed_uuid; /* UUID of the exposed data */
-       struct mutex state_mutex;
+       struct mutex own_state_mutex;
+       struct mutex *state_mutex; /* either own_state_mutex or mdev->tconn->cstate_mutex */
        char congestion_reason;  /* Why we where congested... */
        atomic_t rs_sect_in; /* for incoming resync data rate, SyncTarget */
        atomic_t rs_sect_ev; /* for submitted resync data rate, both */
@@ -1132,9 +1027,8 @@ struct drbd_conf {
        int rs_last_events;  /* counter of read or write "events" (unit sectors)
                              * on the lower level device when we last looked. */
        int c_sync_rate; /* current resync rate after syncer throttle magic */
-       struct fifo_buffer rs_plan_s; /* correction values of resync planer */
+       struct fifo_buffer *rs_plan_s; /* correction values of resync planer (RCU, tconn->conn_update) */
        int rs_in_flight; /* resync sectors in flight (to proxy, in proxy and from proxy) */
-       int rs_planed;    /* resync sectors already planned */
        atomic_t ap_in_flight; /* App sectors in flight (waiting for ack) */
        unsigned int peer_max_bio_size;
        unsigned int local_max_bio_size;
@@ -1142,11 +1036,7 @@ struct drbd_conf {
 
 static inline struct drbd_conf *minor_to_mdev(unsigned int minor)
 {
-       struct drbd_conf *mdev;
-
-       mdev = minor < minor_count ? minor_table[minor] : NULL;
-
-       return mdev;
+       return (struct drbd_conf *)idr_find(&minors, minor);
 }
 
 static inline unsigned int mdev_to_minor(struct drbd_conf *mdev)
@@ -1154,29 +1044,9 @@ static inline unsigned int mdev_to_minor(struct drbd_conf *mdev)
        return mdev->minor;
 }
 
-/* returns 1 if it was successful,
- * returns 0 if there was no data socket.
- * so wherever you are going to use the data.socket, e.g. do
- * if (!drbd_get_data_sock(mdev))
- *     return 0;
- *     CODE();
- * drbd_put_data_sock(mdev);
- */
-static inline int drbd_get_data_sock(struct drbd_conf *mdev)
-{
-       mutex_lock(&mdev->data.mutex);
-       /* drbd_disconnect() could have called drbd_free_sock()
-        * while we were waiting in down()... */
-       if (unlikely(mdev->data.socket == NULL)) {
-               mutex_unlock(&mdev->data.mutex);
-               return 0;
-       }
-       return 1;
-}
-
-static inline void drbd_put_data_sock(struct drbd_conf *mdev)
+static inline struct drbd_conf *vnr_to_mdev(struct drbd_tconn *tconn, int vnr)
 {
-       mutex_unlock(&mdev->data.mutex);
+       return (struct drbd_conf *)idr_find(&tconn->volumes, vnr);
 }
 
 /*
@@ -1185,106 +1055,77 @@ static inline void drbd_put_data_sock(struct drbd_conf *mdev)
 
 /* drbd_main.c */
 
-enum chg_state_flags {
-       CS_HARD = 1,
-       CS_VERBOSE = 2,
-       CS_WAIT_COMPLETE = 4,
-       CS_SERIALIZE    = 8,
-       CS_ORDERED      = CS_WAIT_COMPLETE + CS_SERIALIZE,
-};
-
 enum dds_flags {
        DDSF_FORCED    = 1,
        DDSF_NO_RESYNC = 2, /* Do not run a resync for the new space */
 };
 
 extern void drbd_init_set_defaults(struct drbd_conf *mdev);
-extern enum drbd_state_rv drbd_change_state(struct drbd_conf *mdev,
-                                           enum chg_state_flags f,
-                                           union drbd_state mask,
-                                           union drbd_state val);
-extern void drbd_force_state(struct drbd_conf *, union drbd_state,
-                       union drbd_state);
-extern enum drbd_state_rv _drbd_request_state(struct drbd_conf *,
-                                             union drbd_state,
-                                             union drbd_state,
-                                             enum chg_state_flags);
-extern enum drbd_state_rv __drbd_set_state(struct drbd_conf *, union drbd_state,
-                                          enum chg_state_flags,
-                                          struct completion *done);
-extern void print_st_err(struct drbd_conf *, union drbd_state,
-                       union drbd_state, int);
 extern int  drbd_thread_start(struct drbd_thread *thi);
 extern void _drbd_thread_stop(struct drbd_thread *thi, int restart, int wait);
+extern char *drbd_task_to_thread_name(struct drbd_tconn *tconn, struct task_struct *task);
 #ifdef CONFIG_SMP
-extern void drbd_thread_current_set_cpu(struct drbd_conf *mdev);
-extern void drbd_calc_cpu_mask(struct drbd_conf *mdev);
+extern void drbd_thread_current_set_cpu(struct drbd_thread *thi);
+extern void drbd_calc_cpu_mask(struct drbd_tconn *tconn);
 #else
 #define drbd_thread_current_set_cpu(A) ({})
 #define drbd_calc_cpu_mask(A) ({})
 #endif
-extern void drbd_free_resources(struct drbd_conf *mdev);
-extern void tl_release(struct drbd_conf *mdev, unsigned int barrier_nr,
+extern void tl_release(struct drbd_tconn *, unsigned int barrier_nr,
                       unsigned int set_size);
-extern void tl_clear(struct drbd_conf *mdev);
-extern void _tl_add_barrier(struct drbd_conf *, struct drbd_tl_epoch *);
-extern void drbd_free_sock(struct drbd_conf *mdev);
-extern int drbd_send(struct drbd_conf *mdev, struct socket *sock,
-                       void *buf, size_t size, unsigned msg_flags);
-extern int drbd_send_protocol(struct drbd_conf *mdev);
+extern void tl_clear(struct drbd_tconn *);
+extern void drbd_free_sock(struct drbd_tconn *tconn);
+extern int drbd_send(struct drbd_tconn *tconn, struct socket *sock,
+                    void *buf, size_t size, unsigned msg_flags);
+extern int drbd_send_all(struct drbd_tconn *, struct socket *, void *, size_t,
+                        unsigned);
+
+extern int __drbd_send_protocol(struct drbd_tconn *tconn, enum drbd_packet cmd);
+extern int drbd_send_protocol(struct drbd_tconn *tconn);
 extern int drbd_send_uuids(struct drbd_conf *mdev);
 extern int drbd_send_uuids_skip_initial_sync(struct drbd_conf *mdev);
-extern int drbd_gen_and_send_sync_uuid(struct drbd_conf *mdev);
+extern void drbd_gen_and_send_sync_uuid(struct drbd_conf *mdev);
 extern int drbd_send_sizes(struct drbd_conf *mdev, int trigger_reply, enum dds_flags flags);
 extern int drbd_send_state(struct drbd_conf *mdev, union drbd_state s);
 extern int drbd_send_current_state(struct drbd_conf *mdev);
-extern int _drbd_send_cmd(struct drbd_conf *mdev, struct socket *sock,
-                       enum drbd_packets cmd, struct p_header80 *h,
-                       size_t size, unsigned msg_flags);
-#define USE_DATA_SOCKET 1
-#define USE_META_SOCKET 0
-extern int drbd_send_cmd(struct drbd_conf *mdev, int use_data_socket,
-                       enum drbd_packets cmd, struct p_header80 *h,
-                       size_t size);
-extern int drbd_send_cmd2(struct drbd_conf *mdev, enum drbd_packets cmd,
-                       char *data, size_t size);
-extern int drbd_send_sync_param(struct drbd_conf *mdev, struct syncer_conf *sc);
-extern int drbd_send_b_ack(struct drbd_conf *mdev, u32 barrier_nr,
-                       u32 set_size);
-extern int drbd_send_ack(struct drbd_conf *mdev, enum drbd_packets cmd,
-                       struct drbd_epoch_entry *e);
-extern int drbd_send_ack_rp(struct drbd_conf *mdev, enum drbd_packets cmd,
-                       struct p_block_req *rp);
-extern int drbd_send_ack_dp(struct drbd_conf *mdev, enum drbd_packets cmd,
-                       struct p_data *dp, int data_size);
-extern int drbd_send_ack_ex(struct drbd_conf *mdev, enum drbd_packets cmd,
+extern int drbd_send_sync_param(struct drbd_conf *mdev);
+extern void drbd_send_b_ack(struct drbd_tconn *tconn, u32 barrier_nr,
+                           u32 set_size);
+extern int drbd_send_ack(struct drbd_conf *, enum drbd_packet,
+                        struct drbd_peer_request *);
+extern void drbd_send_ack_rp(struct drbd_conf *mdev, enum drbd_packet cmd,
+                            struct p_block_req *rp);
+extern void drbd_send_ack_dp(struct drbd_conf *mdev, enum drbd_packet cmd,
+                            struct p_data *dp, int data_size);
+extern int drbd_send_ack_ex(struct drbd_conf *mdev, enum drbd_packet cmd,
                            sector_t sector, int blksize, u64 block_id);
-extern int drbd_send_oos(struct drbd_conf *mdev, struct drbd_request *req);
-extern int drbd_send_block(struct drbd_conf *mdev, enum drbd_packets cmd,
-                          struct drbd_epoch_entry *e);
+extern int drbd_send_out_of_sync(struct drbd_conf *, struct drbd_request *);
+extern int drbd_send_block(struct drbd_conf *, enum drbd_packet,
+                          struct drbd_peer_request *);
 extern int drbd_send_dblock(struct drbd_conf *mdev, struct drbd_request *req);
 extern int drbd_send_drequest(struct drbd_conf *mdev, int cmd,
                              sector_t sector, int size, u64 block_id);
-extern int drbd_send_drequest_csum(struct drbd_conf *mdev,
-                                  sector_t sector,int size,
-                                  void *digest, int digest_size,
-                                  enum drbd_packets cmd);
+extern int drbd_send_drequest_csum(struct drbd_conf *mdev, sector_t sector,
+                                  int size, void *digest, int digest_size,
+                                  enum drbd_packet cmd);
 extern int drbd_send_ov_request(struct drbd_conf *mdev,sector_t sector,int size);
 
 extern int drbd_send_bitmap(struct drbd_conf *mdev);
-extern int _drbd_send_bitmap(struct drbd_conf *mdev);
-extern int drbd_send_sr_reply(struct drbd_conf *mdev, enum drbd_state_rv retcode);
+extern void drbd_send_sr_reply(struct drbd_conf *mdev, enum drbd_state_rv retcode);
+extern void conn_send_sr_reply(struct drbd_tconn *tconn, enum drbd_state_rv retcode);
 extern void drbd_free_bc(struct drbd_backing_dev *ldev);
 extern void drbd_mdev_cleanup(struct drbd_conf *mdev);
 void drbd_print_uuids(struct drbd_conf *mdev, const char *text);
 
+extern void conn_md_sync(struct drbd_tconn *tconn);
 extern void drbd_md_sync(struct drbd_conf *mdev);
 extern int  drbd_md_read(struct drbd_conf *mdev, struct drbd_backing_dev *bdev);
 extern void drbd_uuid_set(struct drbd_conf *mdev, int idx, u64 val) __must_hold(local);
 extern void _drbd_uuid_set(struct drbd_conf *mdev, int idx, u64 val) __must_hold(local);
 extern void drbd_uuid_new_current(struct drbd_conf *mdev) __must_hold(local);
-extern void _drbd_uuid_new_current(struct drbd_conf *mdev) __must_hold(local);
 extern void drbd_uuid_set_bm(struct drbd_conf *mdev, u64 val) __must_hold(local);
+extern void drbd_uuid_move_history(struct drbd_conf *mdev) __must_hold(local);
+extern void __drbd_uuid_set(struct drbd_conf *mdev, int idx, u64 val) __must_hold(local);
 extern void drbd_md_set_flag(struct drbd_conf *mdev, int flags) __must_hold(local);
 extern void drbd_md_clear_flag(struct drbd_conf *mdev, int flags)__must_hold(local);
 extern int drbd_md_test_flag(struct drbd_backing_dev *, int);
@@ -1302,33 +1143,52 @@ extern void drbd_queue_bitmap_io(struct drbd_conf *mdev,
 extern int drbd_bitmap_io(struct drbd_conf *mdev,
                int (*io_fn)(struct drbd_conf *),
                char *why, enum bm_flag flags);
+extern int drbd_bitmap_io_from_worker(struct drbd_conf *mdev,
+               int (*io_fn)(struct drbd_conf *),
+               char *why, enum bm_flag flags);
 extern int drbd_bmio_set_n_write(struct drbd_conf *mdev);
 extern int drbd_bmio_clear_n_write(struct drbd_conf *mdev);
 extern void drbd_go_diskless(struct drbd_conf *mdev);
 extern void drbd_ldev_destroy(struct drbd_conf *mdev);
 
-
 /* Meta data layout
    We reserve a 128MB Block (4k aligned)
    * either at the end of the backing device
    * or on a separate meta data device. */
 
-#define MD_RESERVED_SECT (128LU << 11)  /* 128 MB, unit sectors */
 /* The following numbers are sectors */
-#define MD_AL_OFFSET 8     /* 8 Sectors after start of meta area */
-#define MD_AL_MAX_SIZE 64   /* = 32 kb LOG  ~ 3776 extents ~ 14 GB Storage */
-/* Allows up to about 3.8TB */
-#define MD_BM_OFFSET (MD_AL_OFFSET + MD_AL_MAX_SIZE)
-
-/* Since the smalles IO unit is usually 512 byte */
-#define MD_SECTOR_SHIFT         9
-#define MD_SECTOR_SIZE  (1<<MD_SECTOR_SHIFT)
-
-/* activity log */
-#define AL_EXTENTS_PT ((MD_SECTOR_SIZE-12)/8-1) /* 61 ; Extents per 512B sector */
-#define AL_EXTENT_SHIFT 22              /* One extent represents 4M Storage */
+/* Allows up to about 3.8TB, so if you want more,
+ * you need to use the "flexible" meta data format. */
+#define MD_RESERVED_SECT (128LU << 11)  /* 128 MB, unit sectors */
+#define MD_AL_OFFSET   8    /* 8 Sectors after start of meta area */
+#define MD_AL_SECTORS  64   /* = 32 kB on disk activity log ring buffer */
+#define MD_BM_OFFSET (MD_AL_OFFSET + MD_AL_SECTORS)
+
+/* we do all meta data IO in 4k blocks */
+#define MD_BLOCK_SHIFT 12
+#define MD_BLOCK_SIZE  (1<<MD_BLOCK_SHIFT)
+
+/* One activity log extent represents 4M of storage */
+#define AL_EXTENT_SHIFT 22
 #define AL_EXTENT_SIZE (1<<AL_EXTENT_SHIFT)
 
+/* We could make these currently hardcoded constants configurable
+ * variables at create-md time (or even re-configurable at runtime?).
+ * Which will require some more changes to the DRBD "super block"
+ * and attach code.
+ *
+ * updates per transaction:
+ *   This many changes to the active set can be logged with one transaction.
+ *   This number is arbitrary.
+ * context per transaction:
+ *   This many context extent numbers are logged with each transaction.
+ *   This number is resulting from the transaction block size (4k), the layout
+ *   of the transaction header, and the number of updates per transaction.
+ *   See drbd_actlog.c:struct al_transaction_on_disk
+ * */
+#define AL_UPDATES_PER_TRANSACTION      64     // arbitrary
+#define AL_CONTEXT_PER_TRANSACTION     919     // (4096 - 36 - 6*64)/4
+
 #if BITS_PER_LONG == 32
 #define LN2_BPL 5
 #define cpu_to_lel(A) cpu_to_le32(A)
@@ -1364,11 +1224,14 @@ struct bm_extent {
 
 #define SLEEP_TIME (HZ/10)
 
-#define BM_BLOCK_SHIFT  12                      /* 4k per bit */
+/* We do bitmap IO in units of 4k blocks.
+ * We also still have a hardcoded 4k per bit relation. */
+#define BM_BLOCK_SHIFT 12                       /* 4k per bit */
 #define BM_BLOCK_SIZE   (1<<BM_BLOCK_SHIFT)
-/* (9+3) : 512 bytes @ 8 bits; representing 16M storage
- * per sector of on disk bitmap */
-#define BM_EXT_SHIFT    (BM_BLOCK_SHIFT + MD_SECTOR_SHIFT + 3)  /* = 24 */
+/* mostly arbitrarily set the represented size of one bitmap extent,
+ * aka resync extent, to 16 MiB (which is also 512 Byte worth of bitmap
+ * at 4k per bit resolution) */
+#define BM_EXT_SHIFT    24     /* 16 MiB per resync extent */
 #define BM_EXT_SIZE     (1<<BM_EXT_SHIFT)
 
 #if (BM_EXT_SHIFT != 24) || (BM_BLOCK_SHIFT != 12)
@@ -1436,17 +1299,20 @@ struct bm_extent {
 #endif
 #endif
 
-/* Sector shift value for the "hash" functions of tl_hash and ee_hash tables.
- * With a value of 8 all IO in one 128K block make it to the same slot of the
- * hash table. */
-#define HT_SHIFT 8
-#define DRBD_MAX_BIO_SIZE (1U<<(9+HT_SHIFT))
+/* BIO_MAX_SIZE is 256 * PAGE_CACHE_SIZE,
+ * so for typical PAGE_CACHE_SIZE of 4k, that is (1<<20) Byte.
+ * Since we may live in a mixed-platform cluster,
+ * we limit us to a platform agnostic constant here for now.
+ * A followup commit may allow even bigger BIO sizes,
+ * once we thought that through. */
+#define DRBD_MAX_BIO_SIZE (1U << 20)
+#if DRBD_MAX_BIO_SIZE > BIO_MAX_SIZE
+#error Architecture not supported: DRBD_MAX_BIO_SIZE > BIO_MAX_SIZE
+#endif
 #define DRBD_MAX_BIO_SIZE_SAFE (1U << 12)       /* Works always = 4k */
 
-#define DRBD_MAX_SIZE_H80_PACKET (1U << 15) /* The old header only allows packets up to 32Kib data */
-
-/* Number of elements in the app_reads_hash */
-#define APP_R_HSIZE 15
+#define DRBD_MAX_SIZE_H80_PACKET (1U << 15) /* Header 80 only allows packets up to 32KiB data */
+#define DRBD_MAX_BIO_SIZE_P95    (1U << 17) /* Protocol 95 to 99 allows bios up to 128KiB */
 
 extern int  drbd_bm_init(struct drbd_conf *mdev);
 extern int  drbd_bm_resize(struct drbd_conf *mdev, sector_t sectors, int set_new_bits);
@@ -1468,11 +1334,11 @@ extern int  drbd_bm_test_bit(struct drbd_conf *mdev, unsigned long bitnr);
 extern int  drbd_bm_e_weight(struct drbd_conf *mdev, unsigned long enr);
 extern int  drbd_bm_write_page(struct drbd_conf *mdev, unsigned int idx) __must_hold(local);
 extern int  drbd_bm_read(struct drbd_conf *mdev) __must_hold(local);
+extern void drbd_bm_mark_for_writeout(struct drbd_conf *mdev, int page_nr);
 extern int  drbd_bm_write(struct drbd_conf *mdev) __must_hold(local);
+extern int  drbd_bm_write_hinted(struct drbd_conf *mdev) __must_hold(local);
 extern int drbd_bm_write_all(struct drbd_conf *mdev) __must_hold(local);
 extern int  drbd_bm_write_copy_pages(struct drbd_conf *mdev) __must_hold(local);
-extern unsigned long drbd_bm_ALe_set_all(struct drbd_conf *mdev,
-               unsigned long al_enr);
 extern size_t       drbd_bm_words(struct drbd_conf *mdev);
 extern unsigned long drbd_bm_bits(struct drbd_conf *mdev);
 extern sector_t      drbd_bm_capacity(struct drbd_conf *mdev);
@@ -1497,7 +1363,7 @@ extern void drbd_bm_unlock(struct drbd_conf *mdev);
 /* drbd_main.c */
 
 extern struct kmem_cache *drbd_request_cache;
-extern struct kmem_cache *drbd_ee_cache;       /* epoch entries */
+extern struct kmem_cache *drbd_ee_cache;       /* peer requests */
 extern struct kmem_cache *drbd_bm_ext_cache;   /* bitmap extents */
 extern struct kmem_cache *drbd_al_ext_cache;   /* activity log extents */
 extern mempool_t *drbd_request_mempool;
@@ -1537,12 +1403,22 @@ extern struct bio *bio_alloc_drbd(gfp_t gfp_mask);
 
 extern rwlock_t global_state_lock;
 
-extern struct drbd_conf *drbd_new_device(unsigned int minor);
-extern void drbd_free_mdev(struct drbd_conf *mdev);
+extern int conn_lowest_minor(struct drbd_tconn *tconn);
+enum drbd_ret_code conn_new_minor(struct drbd_tconn *tconn, unsigned int minor, int vnr);
+extern void drbd_minor_destroy(struct kref *kref);
+
+extern int set_resource_options(struct drbd_tconn *tconn, struct res_opts *res_opts);
+extern struct drbd_tconn *conn_create(const char *name, struct res_opts *res_opts);
+extern void conn_destroy(struct kref *kref);
+struct drbd_tconn *conn_get_by_name(const char *name);
+extern struct drbd_tconn *conn_get_by_addrs(void *my_addr, int my_addr_len,
+                                           void *peer_addr, int peer_addr_len);
+extern void conn_free_crypto(struct drbd_tconn *tconn);
 
 extern int proc_details;
 
 /* drbd_req */
+extern void __drbd_make_request(struct drbd_conf *, struct bio *, unsigned long);
 extern void drbd_make_request(struct request_queue *q, struct bio *bio);
 extern int drbd_read_remote(struct drbd_conf *mdev, struct drbd_request *req);
 extern int drbd_merge_bvec(struct request_queue *q, struct bvec_merge_data *bvm, struct bio_vec *bvec);
@@ -1550,10 +1426,11 @@ extern int is_valid_ar_handle(struct drbd_request *, sector_t);
 
 
 /* drbd_nl.c */
+extern int drbd_msg_put_info(const char *info);
 extern void drbd_suspend_io(struct drbd_conf *mdev);
 extern void drbd_resume_io(struct drbd_conf *mdev);
 extern char *ppsize(char *buf, unsigned long long size);
-extern sector_t drbd_new_dev_size(struct drbd_conf *, struct drbd_backing_dev *, int);
+extern sector_t drbd_new_dev_size(struct drbd_conf *, struct drbd_backing_dev *, sector_t, int);
 enum determine_dev_size { dev_size_error = -1, unchanged = 0, shrunk = 1, grew = 2 };
 extern enum determine_dev_size drbd_determine_dev_size(struct drbd_conf *, enum dds_flags) __must_hold(local);
 extern void resync_after_online_grow(struct drbd_conf *);
@@ -1561,13 +1438,14 @@ extern void drbd_reconsider_max_bio_size(struct drbd_conf *mdev);
 extern enum drbd_state_rv drbd_set_role(struct drbd_conf *mdev,
                                        enum drbd_role new_role,
                                        int force);
-extern enum drbd_disk_state drbd_try_outdate_peer(struct drbd_conf *mdev);
-extern void drbd_try_outdate_peer_async(struct drbd_conf *mdev);
+extern bool conn_try_outdate_peer(struct drbd_tconn *tconn);
+extern void conn_try_outdate_peer_async(struct drbd_tconn *tconn);
 extern int drbd_khelper(struct drbd_conf *mdev, char *cmd);
 
 /* drbd_worker.c */
 extern int drbd_worker(struct drbd_thread *thi);
-extern int drbd_alter_sa(struct drbd_conf *mdev, int na);
+enum drbd_ret_code drbd_resync_after_valid(struct drbd_conf *mdev, int o_minor);
+void drbd_resync_after_changed(struct drbd_conf *mdev);
 extern void drbd_start_resync(struct drbd_conf *mdev, enum drbd_conns side);
 extern void resume_next_sg(struct drbd_conf *mdev);
 extern void suspend_other_sg(struct drbd_conf *mdev);
@@ -1576,13 +1454,13 @@ extern int drbd_resync_finished(struct drbd_conf *mdev);
 extern void *drbd_md_get_buffer(struct drbd_conf *mdev);
 extern void drbd_md_put_buffer(struct drbd_conf *mdev);
 extern int drbd_md_sync_page_io(struct drbd_conf *mdev,
-                               struct drbd_backing_dev *bdev, sector_t sector, int rw);
-extern void wait_until_done_or_disk_failure(struct drbd_conf *mdev, struct drbd_backing_dev *bdev,
-                                           unsigned int *done);
-extern void drbd_ov_oos_found(struct drbd_conf*, sector_t, int);
+               struct drbd_backing_dev *bdev, sector_t sector, int rw);
+extern void drbd_ov_out_of_sync_found(struct drbd_conf *, sector_t, int);
+extern void wait_until_done_or_force_detached(struct drbd_conf *mdev,
+               struct drbd_backing_dev *bdev, unsigned int *done);
 extern void drbd_rs_controller_reset(struct drbd_conf *mdev);
 
-static inline void ov_oos_print(struct drbd_conf *mdev)
+static inline void ov_out_of_sync_print(struct drbd_conf *mdev)
 {
        if (mdev->ov_last_oos_size) {
                dev_err(DEV, "Out of sync: start=%llu, size=%lu (sectors)\n",
@@ -1594,97 +1472,102 @@ static inline void ov_oos_print(struct drbd_conf *mdev)
 
 
 extern void drbd_csum_bio(struct drbd_conf *, struct crypto_hash *, struct bio *, void *);
-extern void drbd_csum_ee(struct drbd_conf *, struct crypto_hash *, struct drbd_epoch_entry *, void *);
+extern void drbd_csum_ee(struct drbd_conf *, struct crypto_hash *,
+                        struct drbd_peer_request *, void *);
 /* worker callbacks */
-extern int w_req_cancel_conflict(struct drbd_conf *, struct drbd_work *, int);
-extern int w_read_retry_remote(struct drbd_conf *, struct drbd_work *, int);
-extern int w_e_end_data_req(struct drbd_conf *, struct drbd_work *, int);
-extern int w_e_end_rsdata_req(struct drbd_conf *, struct drbd_work *, int);
-extern int w_e_end_csum_rs_req(struct drbd_conf *, struct drbd_work *, int);
-extern int w_e_end_ov_reply(struct drbd_conf *, struct drbd_work *, int);
-extern int w_e_end_ov_req(struct drbd_conf *, struct drbd_work *, int);
-extern int w_ov_finished(struct drbd_conf *, struct drbd_work *, int);
-extern int w_resync_timer(struct drbd_conf *, struct drbd_work *, int);
-extern int w_resume_next_sg(struct drbd_conf *, struct drbd_work *, int);
-extern int w_send_write_hint(struct drbd_conf *, struct drbd_work *, int);
-extern int w_send_dblock(struct drbd_conf *, struct drbd_work *, int);
-extern int w_send_barrier(struct drbd_conf *, struct drbd_work *, int);
-extern int w_send_read_req(struct drbd_conf *, struct drbd_work *, int);
-extern int w_prev_work_done(struct drbd_conf *, struct drbd_work *, int);
-extern int w_e_reissue(struct drbd_conf *, struct drbd_work *, int);
-extern int w_restart_disk_io(struct drbd_conf *, struct drbd_work *, int);
-extern int w_send_oos(struct drbd_conf *, struct drbd_work *, int);
-extern int w_start_resync(struct drbd_conf *, struct drbd_work *, int);
+extern int w_e_end_data_req(struct drbd_work *, int);
+extern int w_e_end_rsdata_req(struct drbd_work *, int);
+extern int w_e_end_csum_rs_req(struct drbd_work *, int);
+extern int w_e_end_ov_reply(struct drbd_work *, int);
+extern int w_e_end_ov_req(struct drbd_work *, int);
+extern int w_ov_finished(struct drbd_work *, int);
+extern int w_resync_timer(struct drbd_work *, int);
+extern int w_send_write_hint(struct drbd_work *, int);
+extern int w_make_resync_request(struct drbd_work *, int);
+extern int w_send_dblock(struct drbd_work *, int);
+extern int w_send_read_req(struct drbd_work *, int);
+extern int w_prev_work_done(struct drbd_work *, int);
+extern int w_e_reissue(struct drbd_work *, int);
+extern int w_restart_disk_io(struct drbd_work *, int);
+extern int w_send_out_of_sync(struct drbd_work *, int);
+extern int w_start_resync(struct drbd_work *, int);
 
 extern void resync_timer_fn(unsigned long data);
 extern void start_resync_timer_fn(unsigned long data);
 
 /* drbd_receiver.c */
 extern int drbd_rs_should_slow_down(struct drbd_conf *mdev, sector_t sector);
-extern int drbd_submit_ee(struct drbd_conf *mdev, struct drbd_epoch_entry *e,
-               const unsigned rw, const int fault_type);
-extern int drbd_release_ee(struct drbd_conf *mdev, struct list_head *list);
-extern struct drbd_epoch_entry *drbd_alloc_ee(struct drbd_conf *mdev,
-                                           u64 id,
-                                           sector_t sector,
-                                           unsigned int data_size,
-                                           gfp_t gfp_mask) __must_hold(local);
-extern void drbd_free_some_ee(struct drbd_conf *mdev, struct drbd_epoch_entry *e,
-               int is_net);
-#define drbd_free_ee(m,e)      drbd_free_some_ee(m, e, 0)
-#define drbd_free_net_ee(m,e)  drbd_free_some_ee(m, e, 1)
-extern void drbd_wait_ee_list_empty(struct drbd_conf *mdev,
-               struct list_head *head);
-extern void _drbd_wait_ee_list_empty(struct drbd_conf *mdev,
-               struct list_head *head);
+extern int drbd_submit_peer_request(struct drbd_conf *,
+                                   struct drbd_peer_request *, const unsigned,
+                                   const int);
+extern int drbd_free_peer_reqs(struct drbd_conf *, struct list_head *);
+extern struct drbd_peer_request *drbd_alloc_peer_req(struct drbd_conf *, u64,
+                                                    sector_t, unsigned int,
+                                                    gfp_t) __must_hold(local);
+extern void __drbd_free_peer_req(struct drbd_conf *, struct drbd_peer_request *,
+                                int);
+#define drbd_free_peer_req(m,e) __drbd_free_peer_req(m, e, 0)
+#define drbd_free_net_peer_req(m,e) __drbd_free_peer_req(m, e, 1)
+extern struct page *drbd_alloc_pages(struct drbd_conf *, unsigned int, bool);
 extern void drbd_set_recv_tcq(struct drbd_conf *mdev, int tcq_enabled);
 extern void _drbd_clear_done_ee(struct drbd_conf *mdev, struct list_head *to_be_freed);
-extern void drbd_flush_workqueue(struct drbd_conf *mdev);
-extern void drbd_free_tl_hash(struct drbd_conf *mdev);
+extern void conn_flush_workqueue(struct drbd_tconn *tconn);
+extern int drbd_connected(struct drbd_conf *mdev);
+static inline void drbd_flush_workqueue(struct drbd_conf *mdev)
+{
+       conn_flush_workqueue(mdev->tconn);
+}
 
-/* yes, there is kernel_setsockopt, but only since 2.6.18. we don't need to
- * mess with get_fs/set_fs, we know we are KERNEL_DS always. */
+/* Yes, there is kernel_setsockopt, but only since 2.6.18.
+ * So we have our own copy of it here. */
 static inline int drbd_setsockopt(struct socket *sock, int level, int optname,
-                       char __user *optval, int optlen)
+                                 char *optval, int optlen)
 {
+       mm_segment_t oldfs = get_fs();
+       char __user *uoptval;
        int err;
+
+       uoptval = (char __user __force *)optval;
+
+       set_fs(KERNEL_DS);
        if (level == SOL_SOCKET)
-               err = sock_setsockopt(sock, level, optname, optval, optlen);
+               err = sock_setsockopt(sock, level, optname, uoptval, optlen);
        else
-               err = sock->ops->setsockopt(sock, level, optname, optval,
+               err = sock->ops->setsockopt(sock, level, optname, uoptval,
                                            optlen);
+       set_fs(oldfs);
        return err;
 }
 
 static inline void drbd_tcp_cork(struct socket *sock)
 {
-       int __user val = 1;
+       int val = 1;
        (void) drbd_setsockopt(sock, SOL_TCP, TCP_CORK,
-                       (char __user *)&val, sizeof(val));
+                       (char*)&val, sizeof(val));
 }
 
 static inline void drbd_tcp_uncork(struct socket *sock)
 {
-       int __user val = 0;
+       int val = 0;
        (void) drbd_setsockopt(sock, SOL_TCP, TCP_CORK,
-                       (char __user *)&val, sizeof(val));
+                       (char*)&val, sizeof(val));
 }
 
 static inline void drbd_tcp_nodelay(struct socket *sock)
 {
-       int __user val = 1;
+       int val = 1;
        (void) drbd_setsockopt(sock, SOL_TCP, TCP_NODELAY,
-                       (char __user *)&val, sizeof(val));
+                       (char*)&val, sizeof(val));
 }
 
 static inline void drbd_tcp_quickack(struct socket *sock)
 {
-       int __user val = 2;
+       int val = 2;
        (void) drbd_setsockopt(sock, SOL_TCP, TCP_QUICKACK,
-                       (char __user *)&val, sizeof(val));
+                       (char*)&val, sizeof(val));
 }
 
-void drbd_bump_write_ordering(struct drbd_conf *mdev, enum write_ordering_e wo);
+void drbd_bump_write_ordering(struct drbd_tconn *tconn, enum write_ordering_e wo);
 
 /* drbd_proc.c */
 extern struct proc_dir_entry *drbd_proc;
@@ -1693,8 +1576,8 @@ extern const char *drbd_conn_str(enum drbd_conns s);
 extern const char *drbd_role_str(enum drbd_role s);
 
 /* drbd_actlog.c */
-extern void drbd_al_begin_io(struct drbd_conf *mdev, sector_t sector);
-extern void drbd_al_complete_io(struct drbd_conf *mdev, sector_t sector);
+extern void drbd_al_begin_io(struct drbd_conf *mdev, struct drbd_interval *i);
+extern void drbd_al_complete_io(struct drbd_conf *mdev, struct drbd_interval *i);
 extern void drbd_rs_complete_io(struct drbd_conf *mdev, sector_t sector);
 extern int drbd_rs_begin_io(struct drbd_conf *mdev, sector_t sector);
 extern int drbd_try_rs_begin_io(struct drbd_conf *mdev, sector_t sector);
@@ -1702,7 +1585,6 @@ extern void drbd_rs_cancel_all(struct drbd_conf *mdev);
 extern int drbd_rs_del_all(struct drbd_conf *mdev);
 extern void drbd_rs_failed_io(struct drbd_conf *mdev,
                sector_t sector, int size);
-extern int drbd_al_read_log(struct drbd_conf *mdev, struct drbd_backing_dev *);
 extern void drbd_advance_rs_marks(struct drbd_conf *mdev, unsigned long still_to_go);
 extern void __drbd_set_in_sync(struct drbd_conf *mdev, sector_t sector,
                int size, const char *file, const unsigned int line);
@@ -1712,73 +1594,24 @@ extern int __drbd_set_out_of_sync(struct drbd_conf *mdev, sector_t sector,
                int size, const char *file, const unsigned int line);
 #define drbd_set_out_of_sync(mdev, sector, size) \
        __drbd_set_out_of_sync(mdev, sector, size, __FILE__, __LINE__)
-extern void drbd_al_apply_to_bm(struct drbd_conf *mdev);
 extern void drbd_al_shrink(struct drbd_conf *mdev);
 
-
 /* drbd_nl.c */
-
-void drbd_nl_cleanup(void);
-int __init drbd_nl_init(void);
-void drbd_bcast_state(struct drbd_conf *mdev, union drbd_state);
-void drbd_bcast_sync_progress(struct drbd_conf *mdev);
-void drbd_bcast_ee(struct drbd_conf *mdev,
-               const char *reason, const int dgs,
-               const char* seen_hash, const char* calc_hash,
-               const struct drbd_epoch_entry* e);
-
-
-/**
- * DOC: DRBD State macros
- *
- * These macros are used to express state changes in easily readable form.
- *
- * The NS macros expand to a mask and a value, that can be bit ored onto the
- * current state as soon as the spinlock (req_lock) was taken.
- *
- * The _NS macros are used for state functions that get called with the
- * spinlock. These macros expand directly to the new state value.
- *
- * Besides the basic forms NS() and _NS() additional _?NS[23] are defined
- * to express state changes that affect more than one aspect of the state.
- *
- * E.g. NS2(conn, C_CONNECTED, peer, R_SECONDARY)
- * Means that the network connection was established and that the peer
- * is in secondary role.
- */
-#define role_MASK R_MASK
-#define peer_MASK R_MASK
-#define disk_MASK D_MASK
-#define pdsk_MASK D_MASK
-#define conn_MASK C_MASK
-#define susp_MASK 1
-#define user_isp_MASK 1
-#define aftr_isp_MASK 1
-#define susp_nod_MASK 1
-#define susp_fen_MASK 1
-
-#define NS(T, S) \
-       ({ union drbd_state mask; mask.i = 0; mask.T = T##_MASK; mask; }), \
-       ({ union drbd_state val; val.i = 0; val.T = (S); val; })
-#define NS2(T1, S1, T2, S2) \
-       ({ union drbd_state mask; mask.i = 0; mask.T1 = T1##_MASK; \
-         mask.T2 = T2##_MASK; mask; }), \
-       ({ union drbd_state val; val.i = 0; val.T1 = (S1); \
-         val.T2 = (S2); val; })
-#define NS3(T1, S1, T2, S2, T3, S3) \
-       ({ union drbd_state mask; mask.i = 0; mask.T1 = T1##_MASK; \
-         mask.T2 = T2##_MASK; mask.T3 = T3##_MASK; mask; }), \
-       ({ union drbd_state val;  val.i = 0; val.T1 = (S1); \
-         val.T2 = (S2); val.T3 = (S3); val; })
-
-#define _NS(D, T, S) \
-       D, ({ union drbd_state __ns; __ns.i = D->state.i; __ns.T = (S); __ns; })
-#define _NS2(D, T1, S1, T2, S2) \
-       D, ({ union drbd_state __ns; __ns.i = D->state.i; __ns.T1 = (S1); \
-       __ns.T2 = (S2); __ns; })
-#define _NS3(D, T1, S1, T2, S2, T3, S3) \
-       D, ({ union drbd_state __ns; __ns.i = D->state.i; __ns.T1 = (S1); \
-       __ns.T2 = (S2); __ns.T3 = (S3); __ns; })
+/* state info broadcast */
+struct sib_info {
+       enum drbd_state_info_bcast_reason sib_reason;
+       union {
+               struct {
+                       char *helper_name;
+                       unsigned helper_exit_code;
+               };
+               struct {
+                       union drbd_state os;
+                       union drbd_state ns;
+               };
+       };
+};
+void drbd_bcast_event(struct drbd_conf *mdev, const struct sib_info *sib);
 
 /*
  * inline helper functions
@@ -1795,9 +1628,10 @@ static inline struct page *page_chain_next(struct page *page)
 #define page_chain_for_each_safe(page, n) \
        for (; page && ({ n = page_chain_next(page); 1; }); page = n)
 
-static inline int drbd_ee_has_active_page(struct drbd_epoch_entry *e)
+
+static inline int drbd_peer_req_has_active_page(struct drbd_peer_request *peer_req)
 {
-       struct page *page = e->pages;
+       struct page *page = peer_req->pages;
        page_chain_for_each(page) {
                if (page_count(page) > 1)
                        return 1;
@@ -1805,18 +1639,6 @@ static inline int drbd_ee_has_active_page(struct drbd_epoch_entry *e)
        return 0;
 }
 
-static inline void drbd_state_lock(struct drbd_conf *mdev)
-{
-       wait_event(mdev->misc_wait,
-                  !test_and_set_bit(CLUSTER_ST_CHANGE, &mdev->flags));
-}
-
-static inline void drbd_state_unlock(struct drbd_conf *mdev)
-{
-       clear_bit(CLUSTER_ST_CHANGE, &mdev->flags);
-       wake_up(&mdev->misc_wait);
-}
-
 static inline enum drbd_state_rv
 _drbd_set_state(struct drbd_conf *mdev, union drbd_state ns,
                enum chg_state_flags flags, struct completion *done)
@@ -1830,48 +1652,71 @@ _drbd_set_state(struct drbd_conf *mdev, union drbd_state ns,
        return rv;
 }
 
-/**
- * drbd_request_state() - Reqest a state change
- * @mdev:      DRBD device.
- * @mask:      mask of state bits to change.
- * @val:       value of new state bits.
- *
- * This is the most graceful way of requesting a state change. It is verbose
- * quite verbose in case the state change is not possible, and all those
- * state changes are globally serialized.
- */
-static inline int drbd_request_state(struct drbd_conf *mdev,
-                                    union drbd_state mask,
-                                    union drbd_state val)
+static inline union drbd_state drbd_read_state(struct drbd_conf *mdev)
 {
-       return _drbd_request_state(mdev, mask, val, CS_VERBOSE + CS_ORDERED);
+       union drbd_state rv;
+
+       rv.i = mdev->state.i;
+       rv.susp = mdev->tconn->susp;
+       rv.susp_nod = mdev->tconn->susp_nod;
+       rv.susp_fen = mdev->tconn->susp_fen;
+
+       return rv;
 }
 
 enum drbd_force_detach_flags {
-       DRBD_IO_ERROR,
+       DRBD_READ_ERROR,
+       DRBD_WRITE_ERROR,
        DRBD_META_IO_ERROR,
        DRBD_FORCE_DETACH,
 };
 
 #define __drbd_chk_io_error(m,f) __drbd_chk_io_error_(m,f, __func__)
 static inline void __drbd_chk_io_error_(struct drbd_conf *mdev,
-               enum drbd_force_detach_flags forcedetach,
+               enum drbd_force_detach_flags df,
                const char *where)
 {
-       switch (mdev->ldev->dc.on_io_error) {
-       case EP_PASS_ON:
-               if (forcedetach == DRBD_IO_ERROR) {
+       enum drbd_io_error_p ep;
+
+       rcu_read_lock();
+       ep = rcu_dereference(mdev->ldev->disk_conf)->on_io_error;
+       rcu_read_unlock();
+       switch (ep) {
+       case EP_PASS_ON: /* FIXME would this be better named "Ignore"? */
+               if (df == DRBD_READ_ERROR || df == DRBD_WRITE_ERROR) {
                        if (__ratelimit(&drbd_ratelimit_state))
                                dev_err(DEV, "Local IO failed in %s.\n", where);
                        if (mdev->state.disk > D_INCONSISTENT)
                                _drbd_set_state(_NS(mdev, disk, D_INCONSISTENT), CS_HARD, NULL);
                        break;
                }
-               /* NOTE fall through to detach case if forcedetach set */
+               /* NOTE fall through for DRBD_META_IO_ERROR or DRBD_FORCE_DETACH */
        case EP_DETACH:
        case EP_CALL_HELPER:
+               /* Remember whether we saw a READ or WRITE error.
+                *
+                * Recovery of the affected area for WRITE failure is covered
+                * by the activity log.
+                * READ errors may fall outside that area though. Certain READ
+                * errors can be "healed" by writing good data to the affected
+                * blocks, which triggers block re-allocation in lower layers.
+                *
+                * If we can not write the bitmap after a READ error,
+                * we may need to trigger a full sync (see w_go_diskless()).
+                *
+                * Force-detach is not really an IO error, but rather a
+                * desperate measure to try to deal with a completely
+                * unresponsive lower level IO stack.
+                * Still it should be treated as a WRITE error.
+                *
+                * Meta IO error is always WRITE error:
+                * we read meta data only once during attach,
+                * which will fail in case of errors.
+                */
                set_bit(WAS_IO_ERROR, &mdev->flags);
-               if (forcedetach == DRBD_FORCE_DETACH)
+               if (df == DRBD_READ_ERROR)
+                       set_bit(WAS_READ_ERROR, &mdev->flags);
+               if (df == DRBD_FORCE_DETACH)
                        set_bit(FORCE_DETACH, &mdev->flags);
                if (mdev->state.disk > D_FAILED) {
                        _drbd_set_state(_NS(mdev, disk, D_FAILED), CS_HARD, NULL);
@@ -1896,9 +1741,9 @@ static inline void drbd_chk_io_error_(struct drbd_conf *mdev,
 {
        if (error) {
                unsigned long flags;
-               spin_lock_irqsave(&mdev->req_lock, flags);
+               spin_lock_irqsave(&mdev->tconn->req_lock, flags);
                __drbd_chk_io_error_(mdev, forcedetach, where);
-               spin_unlock_irqrestore(&mdev->req_lock, flags);
+               spin_unlock_irqrestore(&mdev->tconn->req_lock, flags);
        }
 }
 
@@ -1910,9 +1755,9 @@ static inline void drbd_chk_io_error_(struct drbd_conf *mdev,
  * BTW, for internal meta data, this happens to be the maximum capacity
  * we could agree upon with our peer node.
  */
-static inline sector_t drbd_md_first_sector(struct drbd_backing_dev *bdev)
+static inline sector_t _drbd_md_first_sector(int meta_dev_idx, struct drbd_backing_dev *bdev)
 {
-       switch (bdev->dc.meta_dev_idx) {
+       switch (meta_dev_idx) {
        case DRBD_MD_INDEX_INTERNAL:
        case DRBD_MD_INDEX_FLEX_INT:
                return bdev->md.md_offset + bdev->md.bm_offset;
@@ -1922,13 +1767,30 @@ static inline sector_t drbd_md_first_sector(struct drbd_backing_dev *bdev)
        }
 }
 
+static inline sector_t drbd_md_first_sector(struct drbd_backing_dev *bdev)
+{
+       int meta_dev_idx;
+
+       rcu_read_lock();
+       meta_dev_idx = rcu_dereference(bdev->disk_conf)->meta_dev_idx;
+       rcu_read_unlock();
+
+       return _drbd_md_first_sector(meta_dev_idx, bdev);
+}
+
 /**
  * drbd_md_last_sector() - Return the last sector number of the meta data area
  * @bdev:      Meta data block device.
  */
 static inline sector_t drbd_md_last_sector(struct drbd_backing_dev *bdev)
 {
-       switch (bdev->dc.meta_dev_idx) {
+       int meta_dev_idx;
+
+       rcu_read_lock();
+       meta_dev_idx = rcu_dereference(bdev->disk_conf)->meta_dev_idx;
+       rcu_read_unlock();
+
+       switch (meta_dev_idx) {
        case DRBD_MD_INDEX_INTERNAL:
        case DRBD_MD_INDEX_FLEX_INT:
                return bdev->md.md_offset + MD_AL_OFFSET - 1;
@@ -1956,12 +1818,18 @@ static inline sector_t drbd_get_capacity(struct block_device *bdev)
 static inline sector_t drbd_get_max_capacity(struct drbd_backing_dev *bdev)
 {
        sector_t s;
-       switch (bdev->dc.meta_dev_idx) {
+       int meta_dev_idx;
+
+       rcu_read_lock();
+       meta_dev_idx = rcu_dereference(bdev->disk_conf)->meta_dev_idx;
+       rcu_read_unlock();
+
+       switch (meta_dev_idx) {
        case DRBD_MD_INDEX_INTERNAL:
        case DRBD_MD_INDEX_FLEX_INT:
                s = drbd_get_capacity(bdev->backing_bdev)
                        ? min_t(sector_t, DRBD_MAX_SECTORS_FLEX,
-                                       drbd_md_first_sector(bdev))
+                               _drbd_md_first_sector(meta_dev_idx, bdev))
                        : 0;
                break;
        case DRBD_MD_INDEX_FLEX_EXT:
@@ -1987,9 +1855,15 @@ static inline sector_t drbd_get_max_capacity(struct drbd_backing_dev *bdev)
 static inline sector_t drbd_md_ss__(struct drbd_conf *mdev,
                                    struct drbd_backing_dev *bdev)
 {
-       switch (bdev->dc.meta_dev_idx) {
+       int meta_dev_idx;
+
+       rcu_read_lock();
+       meta_dev_idx = rcu_dereference(bdev->disk_conf)->meta_dev_idx;
+       rcu_read_unlock();
+
+       switch (meta_dev_idx) {
        default: /* external, some index */
-               return MD_RESERVED_SECT * bdev->dc.meta_dev_idx;
+               return MD_RESERVED_SECT * meta_dev_idx;
        case DRBD_MD_INDEX_INTERNAL:
                /* with drbd08, internal meta data is always "flexible" */
        case DRBD_MD_INDEX_FLEX_INT:
@@ -2015,9 +1889,8 @@ drbd_queue_work_front(struct drbd_work_queue *q, struct drbd_work *w)
        unsigned long flags;
        spin_lock_irqsave(&q->q_lock, flags);
        list_add(&w->list, &q->q);
-       up(&q->s); /* within the spinlock,
-                     see comment near end of drbd_worker() */
        spin_unlock_irqrestore(&q->q_lock, flags);
+       wake_up(&q->q_wait);
 }
 
 static inline void
@@ -2026,41 +1899,35 @@ drbd_queue_work(struct drbd_work_queue *q, struct drbd_work *w)
        unsigned long flags;
        spin_lock_irqsave(&q->q_lock, flags);
        list_add_tail(&w->list, &q->q);
-       up(&q->s); /* within the spinlock,
-                     see comment near end of drbd_worker() */
        spin_unlock_irqrestore(&q->q_lock, flags);
+       wake_up(&q->q_wait);
 }
 
-static inline void wake_asender(struct drbd_conf *mdev)
-{
-       if (test_bit(SIGNAL_ASENDER, &mdev->flags))
-               force_sig(DRBD_SIG, mdev->asender.task);
-}
-
-static inline void request_ping(struct drbd_conf *mdev)
+static inline void wake_asender(struct drbd_tconn *tconn)
 {
-       set_bit(SEND_PING, &mdev->flags);
-       wake_asender(mdev);
+       if (test_bit(SIGNAL_ASENDER, &tconn->flags))
+               force_sig(DRBD_SIG, tconn->asender.task);
 }
 
-static inline int drbd_send_short_cmd(struct drbd_conf *mdev,
-       enum drbd_packets cmd)
+static inline void request_ping(struct drbd_tconn *tconn)
 {
-       struct p_header80 h;
-       return drbd_send_cmd(mdev, USE_DATA_SOCKET, cmd, &h, sizeof(h));
+       set_bit(SEND_PING, &tconn->flags);
+       wake_asender(tconn);
 }
 
-static inline int drbd_send_ping(struct drbd_conf *mdev)
-{
-       struct p_header80 h;
-       return drbd_send_cmd(mdev, USE_META_SOCKET, P_PING, &h, sizeof(h));
-}
+extern void *conn_prepare_command(struct drbd_tconn *, struct drbd_socket *);
+extern void *drbd_prepare_command(struct drbd_conf *, struct drbd_socket *);
+extern int conn_send_command(struct drbd_tconn *, struct drbd_socket *,
+                            enum drbd_packet, unsigned int, void *,
+                            unsigned int);
+extern int drbd_send_command(struct drbd_conf *, struct drbd_socket *,
+                            enum drbd_packet, unsigned int, void *,
+                            unsigned int);
 
-static inline int drbd_send_ping_ack(struct drbd_conf *mdev)
-{
-       struct p_header80 h;
-       return drbd_send_cmd(mdev, USE_META_SOCKET, P_PING_ACK, &h, sizeof(h));
-}
+extern int drbd_send_ping(struct drbd_tconn *tconn);
+extern int drbd_send_ping_ack(struct drbd_tconn *tconn);
+extern int drbd_send_state_req(struct drbd_conf *, union drbd_state, union drbd_state);
+extern int conn_send_state_req(struct drbd_tconn *, union drbd_state, union drbd_state);
 
 static inline void drbd_thread_stop(struct drbd_thread *thi)
 {
@@ -2082,21 +1949,21 @@ static inline void drbd_thread_restart_nowait(struct drbd_thread *thi)
  * or implicit barrier packets as necessary.
  * increased:
  *  w_send_barrier
- *  _req_mod(req, queue_for_net_write or queue_for_net_read);
+ *  _req_mod(req, QUEUE_FOR_NET_WRITE or QUEUE_FOR_NET_READ);
  *    it is much easier and equally valid to count what we queue for the
  *    worker, even before it actually was queued or send.
  *    (drbd_make_request_common; recovery path on read io-error)
  * decreased:
  *  got_BarrierAck (respective tl_clear, tl_clear_barrier)
- *  _req_mod(req, data_received)
+ *  _req_mod(req, DATA_RECEIVED)
  *     [from receive_DataReply]
- *  _req_mod(req, write_acked_by_peer or recv_acked_by_peer or neg_acked)
+ *  _req_mod(req, WRITE_ACKED_BY_PEER or RECV_ACKED_BY_PEER or NEG_ACKED)
  *     [from got_BlockAck (P_WRITE_ACK, P_RECV_ACK)]
  *     for some reason it is NOT decreased in got_NegAck,
  *     but in the resulting cleanup code from report_params.
  *     we should try to remember the reason for that...
- *  _req_mod(req, send_failed or send_canceled)
- *  _req_mod(req, connection_lost_while_pending)
+ *  _req_mod(req, SEND_FAILED or SEND_CANCELED)
+ *  _req_mod(req, CONNECTION_LOST_WHILE_PENDING)
  *     [from tl_clear_barrier]
  */
 static inline void inc_ap_pending(struct drbd_conf *mdev)
@@ -2104,17 +1971,19 @@ static inline void inc_ap_pending(struct drbd_conf *mdev)
        atomic_inc(&mdev->ap_pending_cnt);
 }
 
-#define ERR_IF_CNT_IS_NEGATIVE(which)                          \
-       if (atomic_read(&mdev->which) < 0)                      \
+#define ERR_IF_CNT_IS_NEGATIVE(which, func, line)                      \
+       if (atomic_read(&mdev->which) < 0)                              \
                dev_err(DEV, "in %s:%d: " #which " = %d < 0 !\n",       \
-                   __func__ , __LINE__ ,                       \
-                   atomic_read(&mdev->which))
+                       func, line,                                     \
+                       atomic_read(&mdev->which))
 
-#define dec_ap_pending(mdev)   do {                            \
-       typecheck(struct drbd_conf *, mdev);                    \
-       if (atomic_dec_and_test(&mdev->ap_pending_cnt))         \
-               wake_up(&mdev->misc_wait);                      \
-       ERR_IF_CNT_IS_NEGATIVE(ap_pending_cnt); } while (0)
+#define dec_ap_pending(mdev) _dec_ap_pending(mdev, __FUNCTION__, __LINE__)
+static inline void _dec_ap_pending(struct drbd_conf *mdev, const char *func, int line)
+{
+       if (atomic_dec_and_test(&mdev->ap_pending_cnt))
+               wake_up(&mdev->misc_wait);
+       ERR_IF_CNT_IS_NEGATIVE(ap_pending_cnt, func, line);
+}
 
 /* counts how many resync-related answers we still expect from the peer
  *                  increase                   decrease
@@ -2127,10 +1996,12 @@ static inline void inc_rs_pending(struct drbd_conf *mdev)
        atomic_inc(&mdev->rs_pending_cnt);
 }
 
-#define dec_rs_pending(mdev)   do {                            \
-       typecheck(struct drbd_conf *, mdev);                    \
-       atomic_dec(&mdev->rs_pending_cnt);                      \
-       ERR_IF_CNT_IS_NEGATIVE(rs_pending_cnt); } while (0)
+#define dec_rs_pending(mdev) _dec_rs_pending(mdev, __FUNCTION__, __LINE__)
+static inline void _dec_rs_pending(struct drbd_conf *mdev, const char *func, int line)
+{
+       atomic_dec(&mdev->rs_pending_cnt);
+       ERR_IF_CNT_IS_NEGATIVE(rs_pending_cnt, func, line);
+}
 
 /* counts how many answers we still need to send to the peer.
  * increased on
@@ -2146,38 +2017,18 @@ static inline void inc_unacked(struct drbd_conf *mdev)
        atomic_inc(&mdev->unacked_cnt);
 }
 
-#define dec_unacked(mdev)      do {                            \
-       typecheck(struct drbd_conf *, mdev);                    \
-       atomic_dec(&mdev->unacked_cnt);                         \
-       ERR_IF_CNT_IS_NEGATIVE(unacked_cnt); } while (0)
-
-#define sub_unacked(mdev, n)   do {                            \
-       typecheck(struct drbd_conf *, mdev);                    \
-       atomic_sub(n, &mdev->unacked_cnt);                      \
-       ERR_IF_CNT_IS_NEGATIVE(unacked_cnt); } while (0)
-
-
-static inline void put_net_conf(struct drbd_conf *mdev)
+#define dec_unacked(mdev) _dec_unacked(mdev, __FUNCTION__, __LINE__)
+static inline void _dec_unacked(struct drbd_conf *mdev, const char *func, int line)
 {
-       if (atomic_dec_and_test(&mdev->net_cnt))
-               wake_up(&mdev->net_cnt_wait);
+       atomic_dec(&mdev->unacked_cnt);
+       ERR_IF_CNT_IS_NEGATIVE(unacked_cnt, func, line);
 }
 
-/**
- * get_net_conf() - Increase ref count on mdev->net_conf; Returns 0 if nothing there
- * @mdev:      DRBD device.
- *
- * You have to call put_net_conf() when finished working with mdev->net_conf.
- */
-static inline int get_net_conf(struct drbd_conf *mdev)
+#define sub_unacked(mdev, n) _sub_unacked(mdev, n, __FUNCTION__, __LINE__)
+static inline void _sub_unacked(struct drbd_conf *mdev, int n, const char *func, int line)
 {
-       int have_net_conf;
-
-       atomic_inc(&mdev->net_cnt);
-       have_net_conf = mdev->state.conn >= C_UNCONNECTED;
-       if (!have_net_conf)
-               put_net_conf(mdev);
-       return have_net_conf;
+       atomic_sub(n, &mdev->unacked_cnt);
+       ERR_IF_CNT_IS_NEGATIVE(unacked_cnt, func, line);
 }
 
 /**
@@ -2281,17 +2132,20 @@ static inline void drbd_get_syncer_progress(struct drbd_conf *mdev,
  * maybe re-implement using semaphores? */
 static inline int drbd_get_max_buffers(struct drbd_conf *mdev)
 {
-       int mxb = 1000000; /* arbitrary limit on open requests */
-       if (get_net_conf(mdev)) {
-               mxb = mdev->net_conf->max_buffers;
-               put_net_conf(mdev);
-       }
+       struct net_conf *nc;
+       int mxb;
+
+       rcu_read_lock();
+       nc = rcu_dereference(mdev->tconn->net_conf);
+       mxb = nc ? nc->max_buffers : 1000000;  /* arbitrary limit on open requests */
+       rcu_read_unlock();
+
        return mxb;
 }
 
 static inline int drbd_state_is_stable(struct drbd_conf *mdev)
 {
-       union drbd_state s = mdev->state;
+       union drbd_dev_state s = mdev->state;
 
        /* DO NOT add a default clause, we want the compiler to warn us
         * for any newly introduced state we may have forgotten to add here */
@@ -2325,7 +2179,7 @@ static inline int drbd_state_is_stable(struct drbd_conf *mdev)
 
                /* Allow IO in BM exchange states with new protocols */
        case C_WF_BITMAP_S:
-               if (mdev->agreed_pro_version < 96)
+               if (mdev->tconn->agreed_pro_version < 96)
                        return 0;
                break;
 
@@ -2347,7 +2201,7 @@ static inline int drbd_state_is_stable(struct drbd_conf *mdev)
                /* disk state is stable as well. */
                break;
 
-       /* no new io accepted during tansitional states */
+       /* no new io accepted during transitional states */
        case D_ATTACHING:
        case D_NEGOTIATING:
        case D_UNKNOWN:
@@ -2359,16 +2213,18 @@ static inline int drbd_state_is_stable(struct drbd_conf *mdev)
        return 1;
 }
 
-static inline int is_susp(union drbd_state s)
+static inline int drbd_suspended(struct drbd_conf *mdev)
 {
-       return s.susp || s.susp_nod || s.susp_fen;
+       struct drbd_tconn *tconn = mdev->tconn;
+
+       return tconn->susp || tconn->susp_fen || tconn->susp_nod;
 }
 
 static inline bool may_inc_ap_bio(struct drbd_conf *mdev)
 {
        int mxb = drbd_get_max_buffers(mdev);
 
-       if (is_susp(mdev->state))
+       if (drbd_suspended(mdev))
                return false;
        if (test_bit(SUSPEND_IO, &mdev->flags))
                return false;
@@ -2390,30 +2246,30 @@ static inline bool may_inc_ap_bio(struct drbd_conf *mdev)
        return true;
 }
 
-static inline bool inc_ap_bio_cond(struct drbd_conf *mdev, int count)
+static inline bool inc_ap_bio_cond(struct drbd_conf *mdev)
 {
        bool rv = false;
 
-       spin_lock_irq(&mdev->req_lock);
+       spin_lock_irq(&mdev->tconn->req_lock);
        rv = may_inc_ap_bio(mdev);
        if (rv)
-               atomic_add(count, &mdev->ap_bio_cnt);
-       spin_unlock_irq(&mdev->req_lock);
+               atomic_inc(&mdev->ap_bio_cnt);
+       spin_unlock_irq(&mdev->tconn->req_lock);
 
        return rv;
 }
 
-static inline void inc_ap_bio(struct drbd_conf *mdev, int count)
+static inline void inc_ap_bio(struct drbd_conf *mdev)
 {
        /* we wait here
         *    as long as the device is suspended
         *    until the bitmap is no longer on the fly during connection
-        *    handshake as long as we would exeed the max_buffer limit.
+        *    handshake as long as we would exceed the max_buffer limit.
         *
         * to avoid races with the reconnect code,
         * we need to atomic_inc within the spinlock. */
 
-       wait_event(mdev->misc_wait, inc_ap_bio_cond(mdev, count));
+       wait_event(mdev->misc_wait, inc_ap_bio_cond(mdev));
 }
 
 static inline void dec_ap_bio(struct drbd_conf *mdev)
@@ -2425,7 +2281,7 @@ static inline void dec_ap_bio(struct drbd_conf *mdev)
 
        if (ap_bio == 0 && test_bit(BITMAP_IO, &mdev->flags)) {
                if (!test_and_set_bit(BITMAP_IO_QUEUED, &mdev->flags))
-                       drbd_queue_work(&mdev->data.work, &mdev->bm_io_work.w);
+                       drbd_queue_work(&mdev->tconn->sender_work, &mdev->bm_io_work.w);
        }
 
        /* this currently does wake_up for every dec_ap_bio!
@@ -2435,6 +2291,12 @@ static inline void dec_ap_bio(struct drbd_conf *mdev)
                wake_up(&mdev->misc_wait);
 }
 
+static inline bool verify_can_do_stop_sector(struct drbd_conf *mdev)
+{
+       return mdev->tconn->agreed_pro_version >= 97 &&
+               mdev->tconn->agreed_pro_version != 100;
+}
+
 static inline int drbd_set_ed_uuid(struct drbd_conf *mdev, u64 val)
 {
        int changed = mdev->ed_uuid != val;
@@ -2442,40 +2304,6 @@ static inline int drbd_set_ed_uuid(struct drbd_conf *mdev, u64 val)
        return changed;
 }
 
-static inline int seq_cmp(u32 a, u32 b)
-{
-       /* we assume wrap around at 32bit.
-        * for wrap around at 24bit (old atomic_t),
-        * we'd have to
-        *  a <<= 8; b <<= 8;
-        */
-       return (s32)(a) - (s32)(b);
-}
-#define seq_lt(a, b) (seq_cmp((a), (b)) < 0)
-#define seq_gt(a, b) (seq_cmp((a), (b)) > 0)
-#define seq_ge(a, b) (seq_cmp((a), (b)) >= 0)
-#define seq_le(a, b) (seq_cmp((a), (b)) <= 0)
-/* CAUTION: please no side effects in arguments! */
-#define seq_max(a, b) ((u32)(seq_gt((a), (b)) ? (a) : (b)))
-
-static inline void update_peer_seq(struct drbd_conf *mdev, unsigned int new_seq)
-{
-       unsigned int m;
-       spin_lock(&mdev->peer_seq_lock);
-       m = seq_max(mdev->peer_seq, new_seq);
-       mdev->peer_seq = m;
-       spin_unlock(&mdev->peer_seq_lock);
-       if (m == new_seq)
-               wake_up(&mdev->seq_wait);
-}
-
-static inline void drbd_update_congested(struct drbd_conf *mdev)
-{
-       struct sock *sk = mdev->data.socket->sk;
-       if (sk->sk_wmem_queued > sk->sk_sndbuf * 4 / 5)
-               set_bit(NET_CONGESTED, &mdev->flags);
-}
-
 static inline int drbd_queue_order_type(struct drbd_conf *mdev)
 {
        /* sorry, we currently have no working implementation
@@ -2490,10 +2318,15 @@ static inline void drbd_md_flush(struct drbd_conf *mdev)
 {
        int r;
 
+       if (mdev->ldev == NULL) {
+               dev_warn(DEV, "mdev->ldev == NULL in drbd_md_flush\n");
+               return;
+       }
+
        if (test_bit(MD_NO_FUA, &mdev->flags))
                return;
 
-       r = blkdev_issue_flush(mdev->ldev->md_bdev, GFP_KERNEL, NULL);
+       r = blkdev_issue_flush(mdev->ldev->md_bdev, GFP_NOIO, NULL);
        if (r) {
                set_bit(MD_NO_FUA, &mdev->flags);
                dev_err(DEV, "meta data flush failed with status %d, disabling md-flushes\n", r);
diff --git a/drivers/block/drbd/drbd_interval.c b/drivers/block/drbd/drbd_interval.c
new file mode 100644 (file)
index 0000000..89c497c
--- /dev/null
@@ -0,0 +1,207 @@
+#include <asm/bug.h>
+#include <linux/rbtree_augmented.h>
+#include "drbd_interval.h"
+
+/**
+ * interval_end  -  return end of @node
+ */
+static inline
+sector_t interval_end(struct rb_node *node)
+{
+       struct drbd_interval *this = rb_entry(node, struct drbd_interval, rb);
+       return this->end;
+}
+
+/**
+ * compute_subtree_last  -  compute end of @node
+ *
+ * The end of an interval is the highest (start + (size >> 9)) value of this
+ * node and of its children.  Called for @node and its parents whenever the end
+ * may have changed.
+ */
+static inline sector_t
+compute_subtree_last(struct drbd_interval *node)
+{
+       sector_t max = node->sector + (node->size >> 9);
+
+       if (node->rb.rb_left) {
+               sector_t left = interval_end(node->rb.rb_left);
+               if (left > max)
+                       max = left;
+       }
+       if (node->rb.rb_right) {
+               sector_t right = interval_end(node->rb.rb_right);
+               if (right > max)
+                       max = right;
+       }
+       return max;
+}
+
+static void augment_propagate(struct rb_node *rb, struct rb_node *stop)
+{
+       while (rb != stop) {
+               struct drbd_interval *node = rb_entry(rb, struct drbd_interval, rb);
+               sector_t subtree_last = compute_subtree_last(node);
+               if (node->end == subtree_last)
+                       break;
+               node->end = subtree_last;
+               rb = rb_parent(&node->rb);
+       }
+}
+
+static void augment_copy(struct rb_node *rb_old, struct rb_node *rb_new)
+{
+       struct drbd_interval *old = rb_entry(rb_old, struct drbd_interval, rb);
+       struct drbd_interval *new = rb_entry(rb_new, struct drbd_interval, rb);
+
+       new->end = old->end;
+}
+
+static void augment_rotate(struct rb_node *rb_old, struct rb_node *rb_new)
+{
+       struct drbd_interval *old = rb_entry(rb_old, struct drbd_interval, rb);
+       struct drbd_interval *new = rb_entry(rb_new, struct drbd_interval, rb);
+
+       new->end = old->end;
+       old->end = compute_subtree_last(old);
+}
+
+static const struct rb_augment_callbacks augment_callbacks = {
+       augment_propagate,
+       augment_copy,
+       augment_rotate,
+};
+
+/**
+ * drbd_insert_interval  -  insert a new interval into a tree
+ */
+bool
+drbd_insert_interval(struct rb_root *root, struct drbd_interval *this)
+{
+       struct rb_node **new = &root->rb_node, *parent = NULL;
+
+       BUG_ON(!IS_ALIGNED(this->size, 512));
+
+       while (*new) {
+               struct drbd_interval *here =
+                       rb_entry(*new, struct drbd_interval, rb);
+
+               parent = *new;
+               if (this->sector < here->sector)
+                       new = &(*new)->rb_left;
+               else if (this->sector > here->sector)
+                       new = &(*new)->rb_right;
+               else if (this < here)
+                       new = &(*new)->rb_left;
+               else if (this > here)
+                       new = &(*new)->rb_right;
+               else
+                       return false;
+       }
+
+       rb_link_node(&this->rb, parent, new);
+       rb_insert_augmented(&this->rb, root, &augment_callbacks);
+       return true;
+}
+
+/**
+ * drbd_contains_interval  -  check if a tree contains a given interval
+ * @sector:    start sector of @interval
+ * @interval:  may not be a valid pointer
+ *
+ * Returns if the tree contains the node @interval with start sector @start.
+ * Does not dereference @interval until @interval is known to be a valid object
+ * in @tree.  Returns %false if @interval is in the tree but with a different
+ * sector number.
+ */
+bool
+drbd_contains_interval(struct rb_root *root, sector_t sector,
+                      struct drbd_interval *interval)
+{
+       struct rb_node *node = root->rb_node;
+
+       while (node) {
+               struct drbd_interval *here =
+                       rb_entry(node, struct drbd_interval, rb);
+
+               if (sector < here->sector)
+                       node = node->rb_left;
+               else if (sector > here->sector)
+                       node = node->rb_right;
+               else if (interval < here)
+                       node = node->rb_left;
+               else if (interval > here)
+                       node = node->rb_right;
+               else
+                       return true;
+       }
+       return false;
+}
+
+/**
+ * drbd_remove_interval  -  remove an interval from a tree
+ */
+void
+drbd_remove_interval(struct rb_root *root, struct drbd_interval *this)
+{
+       rb_erase_augmented(&this->rb, root, &augment_callbacks);
+}
+
+/**
+ * drbd_find_overlap  - search for an interval overlapping with [sector, sector + size)
+ * @sector:    start sector
+ * @size:      size, aligned to 512 bytes
+ *
+ * Returns an interval overlapping with [sector, sector + size), or NULL if
+ * there is none.  When there is more than one overlapping interval in the
+ * tree, the interval with the lowest start sector is returned, and all other
+ * overlapping intervals will be on the right side of the tree, reachable with
+ * rb_next().
+ */
+struct drbd_interval *
+drbd_find_overlap(struct rb_root *root, sector_t sector, unsigned int size)
+{
+       struct rb_node *node = root->rb_node;
+       struct drbd_interval *overlap = NULL;
+       sector_t end = sector + (size >> 9);
+
+       BUG_ON(!IS_ALIGNED(size, 512));
+
+       while (node) {
+               struct drbd_interval *here =
+                       rb_entry(node, struct drbd_interval, rb);
+
+               if (node->rb_left &&
+                   sector < interval_end(node->rb_left)) {
+                       /* Overlap if any must be on left side */
+                       node = node->rb_left;
+               } else if (here->sector < end &&
+                          sector < here->sector + (here->size >> 9)) {
+                       overlap = here;
+                       break;
+               } else if (sector >= here->sector) {
+                       /* Overlap if any must be on right side */
+                       node = node->rb_right;
+               } else
+                       break;
+       }
+       return overlap;
+}
+
+struct drbd_interval *
+drbd_next_overlap(struct drbd_interval *i, sector_t sector, unsigned int size)
+{
+       sector_t end = sector + (size >> 9);
+       struct rb_node *node;
+
+       for (;;) {
+               node = rb_next(&i->rb);
+               if (!node)
+                       return NULL;
+               i = rb_entry(node, struct drbd_interval, rb);
+               if (i->sector >= end)
+                       return NULL;
+               if (sector < i->sector + (i->size >> 9))
+                       return i;
+       }
+}
diff --git a/drivers/block/drbd/drbd_interval.h b/drivers/block/drbd/drbd_interval.h
new file mode 100644 (file)
index 0000000..f38fcb0
--- /dev/null
@@ -0,0 +1,40 @@
+#ifndef __DRBD_INTERVAL_H
+#define __DRBD_INTERVAL_H
+
+#include <linux/types.h>
+#include <linux/rbtree.h>
+
+struct drbd_interval {
+       struct rb_node rb;
+       sector_t sector;        /* start sector of the interval */
+       unsigned int size;      /* size in bytes */
+       sector_t end;           /* highest interval end in subtree */
+       int local:1             /* local or remote request? */;
+       int waiting:1;
+};
+
+static inline void drbd_clear_interval(struct drbd_interval *i)
+{
+       RB_CLEAR_NODE(&i->rb);
+}
+
+static inline bool drbd_interval_empty(struct drbd_interval *i)
+{
+       return RB_EMPTY_NODE(&i->rb);
+}
+
+extern bool drbd_insert_interval(struct rb_root *, struct drbd_interval *);
+extern bool drbd_contains_interval(struct rb_root *, sector_t,
+                                  struct drbd_interval *);
+extern void drbd_remove_interval(struct rb_root *, struct drbd_interval *);
+extern struct drbd_interval *drbd_find_overlap(struct rb_root *, sector_t,
+                                       unsigned int);
+extern struct drbd_interval *drbd_next_overlap(struct drbd_interval *, sector_t,
+                                       unsigned int);
+
+#define drbd_for_each_overlap(i, root, sector, size)           \
+       for (i = drbd_find_overlap(root, sector, size);         \
+            i;                                                 \
+            i = drbd_next_overlap(i, sector, size))
+
+#endif  /* __DRBD_INTERVAL_H */
index f55683ad4ffad67063b0825a844c3214c07d0601..8c13eeb83c53f2634182721898b6822863b93ac3 100644 (file)
 
 #include "drbd_vli.h"
 
-struct after_state_chg_work {
-       struct drbd_work w;
-       union drbd_state os;
-       union drbd_state ns;
-       enum chg_state_flags flags;
-       struct completion *done;
-};
-
 static DEFINE_MUTEX(drbd_main_mutex);
 int drbdd_init(struct drbd_thread *);
 int drbd_worker(struct drbd_thread *);
@@ -72,21 +64,17 @@ int drbd_asender(struct drbd_thread *);
 int drbd_init(void);
 static int drbd_open(struct block_device *bdev, fmode_t mode);
 static int drbd_release(struct gendisk *gd, fmode_t mode);
-static int w_after_state_ch(struct drbd_conf *mdev, struct drbd_work *w, int unused);
-static void after_state_ch(struct drbd_conf *mdev, union drbd_state os,
-                          union drbd_state ns, enum chg_state_flags flags);
-static int w_md_sync(struct drbd_conf *mdev, struct drbd_work *w, int unused);
+static int w_md_sync(struct drbd_work *w, int unused);
 static void md_sync_timer_fn(unsigned long data);
-static int w_bitmap_io(struct drbd_conf *mdev, struct drbd_work *w, int unused);
-static int w_go_diskless(struct drbd_conf *mdev, struct drbd_work *w, int unused);
-static void _tl_clear(struct drbd_conf *mdev);
+static int w_bitmap_io(struct drbd_work *w, int unused);
+static int w_go_diskless(struct drbd_work *w, int unused);
 
 MODULE_AUTHOR("Philipp Reisner <phil@linbit.com>, "
              "Lars Ellenberg <lars@linbit.com>");
 MODULE_DESCRIPTION("drbd - Distributed Replicated Block Device v" REL_VERSION);
 MODULE_VERSION(REL_VERSION);
 MODULE_LICENSE("GPL");
-MODULE_PARM_DESC(minor_count, "Maximum number of drbd devices ("
+MODULE_PARM_DESC(minor_count, "Approximate number of drbd devices ("
                 __stringify(DRBD_MINOR_COUNT_MIN) "-" __stringify(DRBD_MINOR_COUNT_MAX) ")");
 MODULE_ALIAS_BLOCKDEV_MAJOR(DRBD_MAJOR);
 
@@ -98,7 +86,6 @@ MODULE_PARM_DESC(allow_oos, "DONT USE!");
 module_param(minor_count, uint, 0444);
 module_param(disable_sendpage, bool, 0644);
 module_param(allow_oos, bool, 0);
-module_param(cn_idx, uint, 0444);
 module_param(proc_details, int, 0644);
 
 #ifdef CONFIG_DRBD_FAULT_INJECTION
@@ -120,7 +107,6 @@ module_param(fault_devs, int, 0644);
 unsigned int minor_count = DRBD_MINOR_COUNT_DEF;
 bool disable_sendpage;
 bool allow_oos;
-unsigned int cn_idx = CN_IDX_DRBD;
 int proc_details;       /* Detail level in proc drbd*/
 
 /* Module parameter for setting the user mode helper program
@@ -132,10 +118,11 @@ module_param_string(usermode_helper, usermode_helper, sizeof(usermode_helper), 0
 /* in 2.6.x, our device mapping and config info contains our virtual gendisks
  * as member "struct gendisk *vdisk;"
  */
-struct drbd_conf **minor_table;
+struct idr minors;
+struct list_head drbd_tconns;  /* list of struct drbd_tconn */
 
 struct kmem_cache *drbd_request_cache;
-struct kmem_cache *drbd_ee_cache;      /* epoch entries */
+struct kmem_cache *drbd_ee_cache;      /* peer requests */
 struct kmem_cache *drbd_bm_ext_cache;  /* bitmap extents */
 struct kmem_cache *drbd_al_ext_cache;  /* activity log extents */
 mempool_t *drbd_request_mempool;
@@ -164,10 +151,15 @@ static const struct block_device_operations drbd_ops = {
 
 struct bio *bio_alloc_drbd(gfp_t gfp_mask)
 {
+       struct bio *bio;
+
        if (!drbd_md_io_bio_set)
                return bio_alloc(gfp_mask, 1);
 
-       return bio_alloc_bioset(gfp_mask, 1, drbd_md_io_bio_set);
+       bio = bio_alloc_bioset(gfp_mask, 1, drbd_md_io_bio_set);
+       if (!bio)
+               return NULL;
+       return bio;
 }
 
 #ifdef __CHECKER__
@@ -184,1676 +176,264 @@ int _get_ldev_if_state(struct drbd_conf *mdev, enum drbd_disk_state mins)
                if (atomic_dec_and_test(&mdev->local_cnt))
                        wake_up(&mdev->misc_wait);
        }
-       return io_allowed;
-}
-
-#endif
-
-/**
- * DOC: The transfer log
- *
- * The transfer log is a single linked list of &struct drbd_tl_epoch objects.
- * mdev->newest_tle points to the head, mdev->oldest_tle points to the tail
- * of the list. There is always at least one &struct drbd_tl_epoch object.
- *
- * Each &struct drbd_tl_epoch has a circular double linked list of requests
- * attached.
- */
-static int tl_init(struct drbd_conf *mdev)
-{
-       struct drbd_tl_epoch *b;
-
-       /* during device minor initialization, we may well use GFP_KERNEL */
-       b = kmalloc(sizeof(struct drbd_tl_epoch), GFP_KERNEL);
-       if (!b)
-               return 0;
-       INIT_LIST_HEAD(&b->requests);
-       INIT_LIST_HEAD(&b->w.list);
-       b->next = NULL;
-       b->br_number = 4711;
-       b->n_writes = 0;
-       b->w.cb = NULL; /* if this is != NULL, we need to dec_ap_pending in tl_clear */
-
-       mdev->oldest_tle = b;
-       mdev->newest_tle = b;
-       INIT_LIST_HEAD(&mdev->out_of_sequence_requests);
-       INIT_LIST_HEAD(&mdev->barrier_acked_requests);
-
-       mdev->tl_hash = NULL;
-       mdev->tl_hash_s = 0;
-
-       return 1;
-}
-
-static void tl_cleanup(struct drbd_conf *mdev)
-{
-       D_ASSERT(mdev->oldest_tle == mdev->newest_tle);
-       D_ASSERT(list_empty(&mdev->out_of_sequence_requests));
-       kfree(mdev->oldest_tle);
-       mdev->oldest_tle = NULL;
-       kfree(mdev->unused_spare_tle);
-       mdev->unused_spare_tle = NULL;
-       kfree(mdev->tl_hash);
-       mdev->tl_hash = NULL;
-       mdev->tl_hash_s = 0;
-}
-
-/**
- * _tl_add_barrier() - Adds a barrier to the transfer log
- * @mdev:      DRBD device.
- * @new:       Barrier to be added before the current head of the TL.
- *
- * The caller must hold the req_lock.
- */
-void _tl_add_barrier(struct drbd_conf *mdev, struct drbd_tl_epoch *new)
-{
-       struct drbd_tl_epoch *newest_before;
-
-       INIT_LIST_HEAD(&new->requests);
-       INIT_LIST_HEAD(&new->w.list);
-       new->w.cb = NULL; /* if this is != NULL, we need to dec_ap_pending in tl_clear */
-       new->next = NULL;
-       new->n_writes = 0;
-
-       newest_before = mdev->newest_tle;
-       new->br_number = newest_before->br_number+1;
-       if (mdev->newest_tle != new) {
-               mdev->newest_tle->next = new;
-               mdev->newest_tle = new;
-       }
-}
-
-/**
- * tl_release() - Free or recycle the oldest &struct drbd_tl_epoch object of the TL
- * @mdev:      DRBD device.
- * @barrier_nr:        Expected identifier of the DRBD write barrier packet.
- * @set_size:  Expected number of requests before that barrier.
- *
- * In case the passed barrier_nr or set_size does not match the oldest
- * &struct drbd_tl_epoch objects this function will cause a termination
- * of the connection.
- */
-void tl_release(struct drbd_conf *mdev, unsigned int barrier_nr,
-                      unsigned int set_size)
-{
-       struct drbd_tl_epoch *b, *nob; /* next old barrier */
-       struct list_head *le, *tle;
-       struct drbd_request *r;
-
-       spin_lock_irq(&mdev->req_lock);
-
-       b = mdev->oldest_tle;
-
-       /* first some paranoia code */
-       if (b == NULL) {
-               dev_err(DEV, "BAD! BarrierAck #%u received, but no epoch in tl!?\n",
-                       barrier_nr);
-               goto bail;
-       }
-       if (b->br_number != barrier_nr) {
-               dev_err(DEV, "BAD! BarrierAck #%u received, expected #%u!\n",
-                       barrier_nr, b->br_number);
-               goto bail;
-       }
-       if (b->n_writes != set_size) {
-               dev_err(DEV, "BAD! BarrierAck #%u received with n_writes=%u, expected n_writes=%u!\n",
-                       barrier_nr, set_size, b->n_writes);
-               goto bail;
-       }
-
-       /* Clean up list of requests processed during current epoch */
-       list_for_each_safe(le, tle, &b->requests) {
-               r = list_entry(le, struct drbd_request, tl_requests);
-               _req_mod(r, barrier_acked);
-       }
-       /* There could be requests on the list waiting for completion
-          of the write to the local disk. To avoid corruptions of
-          slab's data structures we have to remove the lists head.
-
-          Also there could have been a barrier ack out of sequence, overtaking
-          the write acks - which would be a bug and violating write ordering.
-          To not deadlock in case we lose connection while such requests are
-          still pending, we need some way to find them for the
-          _req_mode(connection_lost_while_pending).
-
-          These have been list_move'd to the out_of_sequence_requests list in
-          _req_mod(, barrier_acked) above.
-          */
-       list_splice_init(&b->requests, &mdev->barrier_acked_requests);
-
-       nob = b->next;
-       if (test_and_clear_bit(CREATE_BARRIER, &mdev->flags)) {
-               _tl_add_barrier(mdev, b);
-               if (nob)
-                       mdev->oldest_tle = nob;
-               /* if nob == NULL b was the only barrier, and becomes the new
-                  barrier. Therefore mdev->oldest_tle points already to b */
-       } else {
-               D_ASSERT(nob != NULL);
-               mdev->oldest_tle = nob;
-               kfree(b);
-       }
-
-       spin_unlock_irq(&mdev->req_lock);
-       dec_ap_pending(mdev);
-
-       return;
-
-bail:
-       spin_unlock_irq(&mdev->req_lock);
-       drbd_force_state(mdev, NS(conn, C_PROTOCOL_ERROR));
-}
-
-
-/**
- * _tl_restart() - Walks the transfer log, and applies an action to all requests
- * @mdev:      DRBD device.
- * @what:       The action/event to perform with all request objects
- *
- * @what might be one of connection_lost_while_pending, resend, fail_frozen_disk_io,
- * restart_frozen_disk_io.
- */
-static void _tl_restart(struct drbd_conf *mdev, enum drbd_req_event what)
-{
-       struct drbd_tl_epoch *b, *tmp, **pn;
-       struct list_head *le, *tle, carry_reads;
-       struct drbd_request *req;
-       int rv, n_writes, n_reads;
-
-       b = mdev->oldest_tle;
-       pn = &mdev->oldest_tle;
-       while (b) {
-               n_writes = 0;
-               n_reads = 0;
-               INIT_LIST_HEAD(&carry_reads);
-               list_for_each_safe(le, tle, &b->requests) {
-                       req = list_entry(le, struct drbd_request, tl_requests);
-                       rv = _req_mod(req, what);
-
-                       n_writes += (rv & MR_WRITE) >> MR_WRITE_SHIFT;
-                       n_reads  += (rv & MR_READ) >> MR_READ_SHIFT;
-               }
-               tmp = b->next;
-
-               if (n_writes) {
-                       if (what == resend) {
-                               b->n_writes = n_writes;
-                               if (b->w.cb == NULL) {
-                                       b->w.cb = w_send_barrier;
-                                       inc_ap_pending(mdev);
-                                       set_bit(CREATE_BARRIER, &mdev->flags);
-                               }
-
-                               drbd_queue_work(&mdev->data.work, &b->w);
-                       }
-                       pn = &b->next;
-               } else {
-                       if (n_reads)
-                               list_add(&carry_reads, &b->requests);
-                       /* there could still be requests on that ring list,
-                        * in case local io is still pending */
-                       list_del(&b->requests);
-
-                       /* dec_ap_pending corresponding to queue_barrier.
-                        * the newest barrier may not have been queued yet,
-                        * in which case w.cb is still NULL. */
-                       if (b->w.cb != NULL)
-                               dec_ap_pending(mdev);
-
-                       if (b == mdev->newest_tle) {
-                               /* recycle, but reinit! */
-                               D_ASSERT(tmp == NULL);
-                               INIT_LIST_HEAD(&b->requests);
-                               list_splice(&carry_reads, &b->requests);
-                               INIT_LIST_HEAD(&b->w.list);
-                               b->w.cb = NULL;
-                               b->br_number = net_random();
-                               b->n_writes = 0;
-
-                               *pn = b;
-                               break;
-                       }
-                       *pn = tmp;
-                       kfree(b);
-               }
-               b = tmp;
-               list_splice(&carry_reads, &b->requests);
-       }
-
-       /* Actions operating on the disk state, also want to work on
-          requests that got barrier acked. */
-
-       list_for_each_safe(le, tle, &mdev->barrier_acked_requests) {
-               req = list_entry(le, struct drbd_request, tl_requests);
-               _req_mod(req, what);
-       }
-}
-
-
-/**
- * tl_clear() - Clears all requests and &struct drbd_tl_epoch objects out of the TL
- * @mdev:      DRBD device.
- *
- * This is called after the connection to the peer was lost. The storage covered
- * by the requests on the transfer gets marked as our of sync. Called from the
- * receiver thread and the worker thread.
- */
-void tl_clear(struct drbd_conf *mdev)
-{
-       spin_lock_irq(&mdev->req_lock);
-       _tl_clear(mdev);
-       spin_unlock_irq(&mdev->req_lock);
-}
-
-static void _tl_clear(struct drbd_conf *mdev)
-{
-       struct list_head *le, *tle;
-       struct drbd_request *r;
-
-       _tl_restart(mdev, connection_lost_while_pending);
-
-       /* we expect this list to be empty. */
-       D_ASSERT(list_empty(&mdev->out_of_sequence_requests));
-
-       /* but just in case, clean it up anyways! */
-       list_for_each_safe(le, tle, &mdev->out_of_sequence_requests) {
-               r = list_entry(le, struct drbd_request, tl_requests);
-               /* It would be nice to complete outside of spinlock.
-                * But this is easier for now. */
-               _req_mod(r, connection_lost_while_pending);
-       }
-
-       /* ensure bit indicating barrier is required is clear */
-       clear_bit(CREATE_BARRIER, &mdev->flags);
-
-       memset(mdev->app_reads_hash, 0, APP_R_HSIZE*sizeof(void *));
-
-}
-
-void tl_restart(struct drbd_conf *mdev, enum drbd_req_event what)
-{
-       spin_lock_irq(&mdev->req_lock);
-       _tl_restart(mdev, what);
-       spin_unlock_irq(&mdev->req_lock);
-}
-
-/**
- * tl_abort_disk_io() - Abort disk I/O for all requests for a certain mdev in the TL
- * @mdev:      DRBD device.
- */
-void tl_abort_disk_io(struct drbd_conf *mdev)
-{
-       struct drbd_tl_epoch *b;
-       struct list_head *le, *tle;
-       struct drbd_request *req;
-
-       spin_lock_irq(&mdev->req_lock);
-       b = mdev->oldest_tle;
-       while (b) {
-               list_for_each_safe(le, tle, &b->requests) {
-                       req = list_entry(le, struct drbd_request, tl_requests);
-                       if (!(req->rq_state & RQ_LOCAL_PENDING))
-                               continue;
-                       _req_mod(req, abort_disk_io);
-               }
-               b = b->next;
-       }
-
-       list_for_each_safe(le, tle, &mdev->barrier_acked_requests) {
-               req = list_entry(le, struct drbd_request, tl_requests);
-               if (!(req->rq_state & RQ_LOCAL_PENDING))
-                       continue;
-               _req_mod(req, abort_disk_io);
-       }
-
-       spin_unlock_irq(&mdev->req_lock);
-}
-
-/**
- * cl_wide_st_chg() - true if the state change is a cluster wide one
- * @mdev:      DRBD device.
- * @os:                old (current) state.
- * @ns:                new (wanted) state.
- */
-static int cl_wide_st_chg(struct drbd_conf *mdev,
-                         union drbd_state os, union drbd_state ns)
-{
-       return (os.conn >= C_CONNECTED && ns.conn >= C_CONNECTED &&
-                ((os.role != R_PRIMARY && ns.role == R_PRIMARY) ||
-                 (os.conn != C_STARTING_SYNC_T && ns.conn == C_STARTING_SYNC_T) ||
-                 (os.conn != C_STARTING_SYNC_S && ns.conn == C_STARTING_SYNC_S) ||
-                 (os.disk != D_FAILED && ns.disk == D_FAILED))) ||
-               (os.conn >= C_CONNECTED && ns.conn == C_DISCONNECTING) ||
-               (os.conn == C_CONNECTED && ns.conn == C_VERIFY_S);
-}
-
-enum drbd_state_rv
-drbd_change_state(struct drbd_conf *mdev, enum chg_state_flags f,
-                 union drbd_state mask, union drbd_state val)
-{
-       unsigned long flags;
-       union drbd_state os, ns;
-       enum drbd_state_rv rv;
-
-       spin_lock_irqsave(&mdev->req_lock, flags);
-       os = mdev->state;
-       ns.i = (os.i & ~mask.i) | val.i;
-       rv = _drbd_set_state(mdev, ns, f, NULL);
-       ns = mdev->state;
-       spin_unlock_irqrestore(&mdev->req_lock, flags);
-
-       return rv;
-}
-
-/**
- * drbd_force_state() - Impose a change which happens outside our control on our state
- * @mdev:      DRBD device.
- * @mask:      mask of state bits to change.
- * @val:       value of new state bits.
- */
-void drbd_force_state(struct drbd_conf *mdev,
-       union drbd_state mask, union drbd_state val)
-{
-       drbd_change_state(mdev, CS_HARD, mask, val);
-}
-
-static enum drbd_state_rv is_valid_state(struct drbd_conf *, union drbd_state);
-static enum drbd_state_rv is_valid_state_transition(struct drbd_conf *,
-                                                   union drbd_state,
-                                                   union drbd_state);
-enum sanitize_state_warnings {
-       NO_WARNING,
-       ABORTED_ONLINE_VERIFY,
-       ABORTED_RESYNC,
-       CONNECTION_LOST_NEGOTIATING,
-       IMPLICITLY_UPGRADED_DISK,
-       IMPLICITLY_UPGRADED_PDSK,
-};
-static union drbd_state sanitize_state(struct drbd_conf *mdev, union drbd_state os,
-                                      union drbd_state ns, enum sanitize_state_warnings *warn);
-int drbd_send_state_req(struct drbd_conf *,
-                       union drbd_state, union drbd_state);
-
-static enum drbd_state_rv
-_req_st_cond(struct drbd_conf *mdev, union drbd_state mask,
-            union drbd_state val)
-{
-       union drbd_state os, ns;
-       unsigned long flags;
-       enum drbd_state_rv rv;
-
-       if (test_and_clear_bit(CL_ST_CHG_SUCCESS, &mdev->flags))
-               return SS_CW_SUCCESS;
-
-       if (test_and_clear_bit(CL_ST_CHG_FAIL, &mdev->flags))
-               return SS_CW_FAILED_BY_PEER;
-
-       rv = 0;
-       spin_lock_irqsave(&mdev->req_lock, flags);
-       os = mdev->state;
-       ns.i = (os.i & ~mask.i) | val.i;
-       ns = sanitize_state(mdev, os, ns, NULL);
-
-       if (!cl_wide_st_chg(mdev, os, ns))
-               rv = SS_CW_NO_NEED;
-       if (!rv) {
-               rv = is_valid_state(mdev, ns);
-               if (rv == SS_SUCCESS) {
-                       rv = is_valid_state_transition(mdev, ns, os);
-                       if (rv == SS_SUCCESS)
-                               rv = SS_UNKNOWN_ERROR; /* cont waiting, otherwise fail. */
-               }
-       }
-       spin_unlock_irqrestore(&mdev->req_lock, flags);
-
-       return rv;
-}
-
-/**
- * drbd_req_state() - Perform an eventually cluster wide state change
- * @mdev:      DRBD device.
- * @mask:      mask of state bits to change.
- * @val:       value of new state bits.
- * @f:         flags
- *
- * Should not be called directly, use drbd_request_state() or
- * _drbd_request_state().
- */
-static enum drbd_state_rv
-drbd_req_state(struct drbd_conf *mdev, union drbd_state mask,
-              union drbd_state val, enum chg_state_flags f)
-{
-       struct completion done;
-       unsigned long flags;
-       union drbd_state os, ns;
-       enum drbd_state_rv rv;
-
-       init_completion(&done);
-
-       if (f & CS_SERIALIZE)
-               mutex_lock(&mdev->state_mutex);
-
-       spin_lock_irqsave(&mdev->req_lock, flags);
-       os = mdev->state;
-       ns.i = (os.i & ~mask.i) | val.i;
-       ns = sanitize_state(mdev, os, ns, NULL);
-
-       if (cl_wide_st_chg(mdev, os, ns)) {
-               rv = is_valid_state(mdev, ns);
-               if (rv == SS_SUCCESS)
-                       rv = is_valid_state_transition(mdev, ns, os);
-               spin_unlock_irqrestore(&mdev->req_lock, flags);
-
-               if (rv < SS_SUCCESS) {
-                       if (f & CS_VERBOSE)
-                               print_st_err(mdev, os, ns, rv);
-                       goto abort;
-               }
-
-               drbd_state_lock(mdev);
-               if (!drbd_send_state_req(mdev, mask, val)) {
-                       drbd_state_unlock(mdev);
-                       rv = SS_CW_FAILED_BY_PEER;
-                       if (f & CS_VERBOSE)
-                               print_st_err(mdev, os, ns, rv);
-                       goto abort;
-               }
-
-               wait_event(mdev->state_wait,
-                       (rv = _req_st_cond(mdev, mask, val)));
-
-               if (rv < SS_SUCCESS) {
-                       drbd_state_unlock(mdev);
-                       if (f & CS_VERBOSE)
-                               print_st_err(mdev, os, ns, rv);
-                       goto abort;
-               }
-               spin_lock_irqsave(&mdev->req_lock, flags);
-               os = mdev->state;
-               ns.i = (os.i & ~mask.i) | val.i;
-               rv = _drbd_set_state(mdev, ns, f, &done);
-               drbd_state_unlock(mdev);
-       } else {
-               rv = _drbd_set_state(mdev, ns, f, &done);
-       }
-
-       spin_unlock_irqrestore(&mdev->req_lock, flags);
-
-       if (f & CS_WAIT_COMPLETE && rv == SS_SUCCESS) {
-               D_ASSERT(current != mdev->worker.task);
-               wait_for_completion(&done);
-       }
-
-abort:
-       if (f & CS_SERIALIZE)
-               mutex_unlock(&mdev->state_mutex);
-
-       return rv;
-}
-
-/**
- * _drbd_request_state() - Request a state change (with flags)
- * @mdev:      DRBD device.
- * @mask:      mask of state bits to change.
- * @val:       value of new state bits.
- * @f:         flags
- *
- * Cousin of drbd_request_state(), useful with the CS_WAIT_COMPLETE
- * flag, or when logging of failed state change requests is not desired.
- */
-enum drbd_state_rv
-_drbd_request_state(struct drbd_conf *mdev, union drbd_state mask,
-                   union drbd_state val, enum chg_state_flags f)
-{
-       enum drbd_state_rv rv;
-
-       wait_event(mdev->state_wait,
-                  (rv = drbd_req_state(mdev, mask, val, f)) != SS_IN_TRANSIENT_STATE);
-
-       return rv;
-}
-
-static void print_st(struct drbd_conf *mdev, char *name, union drbd_state ns)
-{
-       dev_err(DEV, " %s = { cs:%s ro:%s/%s ds:%s/%s %c%c%c%c }\n",
-           name,
-           drbd_conn_str(ns.conn),
-           drbd_role_str(ns.role),
-           drbd_role_str(ns.peer),
-           drbd_disk_str(ns.disk),
-           drbd_disk_str(ns.pdsk),
-           is_susp(ns) ? 's' : 'r',
-           ns.aftr_isp ? 'a' : '-',
-           ns.peer_isp ? 'p' : '-',
-           ns.user_isp ? 'u' : '-'
-           );
-}
-
-void print_st_err(struct drbd_conf *mdev, union drbd_state os,
-                 union drbd_state ns, enum drbd_state_rv err)
-{
-       if (err == SS_IN_TRANSIENT_STATE)
-               return;
-       dev_err(DEV, "State change failed: %s\n", drbd_set_st_err_str(err));
-       print_st(mdev, " state", os);
-       print_st(mdev, "wanted", ns);
-}
-
-
-/**
- * is_valid_state() - Returns an SS_ error code if ns is not valid
- * @mdev:      DRBD device.
- * @ns:                State to consider.
- */
-static enum drbd_state_rv
-is_valid_state(struct drbd_conf *mdev, union drbd_state ns)
-{
-       /* See drbd_state_sw_errors in drbd_strings.c */
-
-       enum drbd_fencing_p fp;
-       enum drbd_state_rv rv = SS_SUCCESS;
-
-       fp = FP_DONT_CARE;
-       if (get_ldev(mdev)) {
-               fp = mdev->ldev->dc.fencing;
-               put_ldev(mdev);
-       }
-
-       if (get_net_conf(mdev)) {
-               if (!mdev->net_conf->two_primaries &&
-                   ns.role == R_PRIMARY && ns.peer == R_PRIMARY)
-                       rv = SS_TWO_PRIMARIES;
-               put_net_conf(mdev);
-       }
-
-       if (rv <= 0)
-               /* already found a reason to abort */;
-       else if (ns.role == R_SECONDARY && mdev->open_cnt)
-               rv = SS_DEVICE_IN_USE;
-
-       else if (ns.role == R_PRIMARY && ns.conn < C_CONNECTED && ns.disk < D_UP_TO_DATE)
-               rv = SS_NO_UP_TO_DATE_DISK;
-
-       else if (fp >= FP_RESOURCE &&
-                ns.role == R_PRIMARY && ns.conn < C_CONNECTED && ns.pdsk >= D_UNKNOWN)
-               rv = SS_PRIMARY_NOP;
-
-       else if (ns.role == R_PRIMARY && ns.disk <= D_INCONSISTENT && ns.pdsk <= D_INCONSISTENT)
-               rv = SS_NO_UP_TO_DATE_DISK;
-
-       else if (ns.conn > C_CONNECTED && ns.disk < D_INCONSISTENT)
-               rv = SS_NO_LOCAL_DISK;
-
-       else if (ns.conn > C_CONNECTED && ns.pdsk < D_INCONSISTENT)
-               rv = SS_NO_REMOTE_DISK;
-
-       else if (ns.conn > C_CONNECTED && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE)
-               rv = SS_NO_UP_TO_DATE_DISK;
-
-       else if ((ns.conn == C_CONNECTED ||
-                 ns.conn == C_WF_BITMAP_S ||
-                 ns.conn == C_SYNC_SOURCE ||
-                 ns.conn == C_PAUSED_SYNC_S) &&
-                 ns.disk == D_OUTDATED)
-               rv = SS_CONNECTED_OUTDATES;
-
-       else if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) &&
-                (mdev->sync_conf.verify_alg[0] == 0))
-               rv = SS_NO_VERIFY_ALG;
-
-       else if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) &&
-                 mdev->agreed_pro_version < 88)
-               rv = SS_NOT_SUPPORTED;
-
-       else if (ns.conn >= C_CONNECTED && ns.pdsk == D_UNKNOWN)
-               rv = SS_CONNECTED_OUTDATES;
-
-       return rv;
-}
-
-/**
- * is_valid_state_transition() - Returns an SS_ error code if the state transition is not possible
- * @mdev:      DRBD device.
- * @ns:                new state.
- * @os:                old state.
- */
-static enum drbd_state_rv
-is_valid_state_transition(struct drbd_conf *mdev, union drbd_state ns,
-                         union drbd_state os)
-{
-       enum drbd_state_rv rv = SS_SUCCESS;
-
-       if ((ns.conn == C_STARTING_SYNC_T || ns.conn == C_STARTING_SYNC_S) &&
-           os.conn > C_CONNECTED)
-               rv = SS_RESYNC_RUNNING;
-
-       if (ns.conn == C_DISCONNECTING && os.conn == C_STANDALONE)
-               rv = SS_ALREADY_STANDALONE;
-
-       if (ns.disk > D_ATTACHING && os.disk == D_DISKLESS)
-               rv = SS_IS_DISKLESS;
-
-       if (ns.conn == C_WF_CONNECTION && os.conn < C_UNCONNECTED)
-               rv = SS_NO_NET_CONFIG;
-
-       if (ns.disk == D_OUTDATED && os.disk < D_OUTDATED && os.disk != D_ATTACHING)
-               rv = SS_LOWER_THAN_OUTDATED;
-
-       if (ns.conn == C_DISCONNECTING && os.conn == C_UNCONNECTED)
-               rv = SS_IN_TRANSIENT_STATE;
-
-       if (ns.conn == os.conn && ns.conn == C_WF_REPORT_PARAMS)
-               rv = SS_IN_TRANSIENT_STATE;
-
-       /* While establishing a connection only allow cstate to change.
-          Delay/refuse role changes, detach attach etc... */
-       if (test_bit(STATE_SENT, &mdev->flags) &&
-           !(os.conn == C_WF_REPORT_PARAMS ||
-             (ns.conn == C_WF_REPORT_PARAMS && os.conn == C_WF_CONNECTION)))
-               rv = SS_IN_TRANSIENT_STATE;
-
-       if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) && os.conn < C_CONNECTED)
-               rv = SS_NEED_CONNECTION;
-
-       if ((ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T) &&
-           ns.conn != os.conn && os.conn > C_CONNECTED)
-               rv = SS_RESYNC_RUNNING;
-
-       if ((ns.conn == C_STARTING_SYNC_S || ns.conn == C_STARTING_SYNC_T) &&
-           os.conn < C_CONNECTED)
-               rv = SS_NEED_CONNECTION;
-
-       if ((ns.conn == C_SYNC_TARGET || ns.conn == C_SYNC_SOURCE)
-           && os.conn < C_WF_REPORT_PARAMS)
-               rv = SS_NEED_CONNECTION; /* No NetworkFailure -> SyncTarget etc... */
-
-       return rv;
-}
-
-static void print_sanitize_warnings(struct drbd_conf *mdev, enum sanitize_state_warnings warn)
-{
-       static const char *msg_table[] = {
-               [NO_WARNING] = "",
-               [ABORTED_ONLINE_VERIFY] = "Online-verify aborted.",
-               [ABORTED_RESYNC] = "Resync aborted.",
-               [CONNECTION_LOST_NEGOTIATING] = "Connection lost while negotiating, no data!",
-               [IMPLICITLY_UPGRADED_DISK] = "Implicitly upgraded disk",
-               [IMPLICITLY_UPGRADED_PDSK] = "Implicitly upgraded pdsk",
-       };
-
-       if (warn != NO_WARNING)
-               dev_warn(DEV, "%s\n", msg_table[warn]);
-}
-
-/**
- * sanitize_state() - Resolves implicitly necessary additional changes to a state transition
- * @mdev:      DRBD device.
- * @os:                old state.
- * @ns:                new state.
- * @warn_sync_abort:
- *
- * When we loose connection, we have to set the state of the peers disk (pdsk)
- * to D_UNKNOWN. This rule and many more along those lines are in this function.
- */
-static union drbd_state sanitize_state(struct drbd_conf *mdev, union drbd_state os,
-                                      union drbd_state ns, enum sanitize_state_warnings *warn)
-{
-       enum drbd_fencing_p fp;
-       enum drbd_disk_state disk_min, disk_max, pdsk_min, pdsk_max;
-
-       if (warn)
-               *warn = NO_WARNING;
-
-       fp = FP_DONT_CARE;
-       if (get_ldev(mdev)) {
-               fp = mdev->ldev->dc.fencing;
-               put_ldev(mdev);
-       }
-
-       /* Disallow Network errors to configure a device's network part */
-       if ((ns.conn >= C_TIMEOUT && ns.conn <= C_TEAR_DOWN) &&
-           os.conn <= C_DISCONNECTING)
-               ns.conn = os.conn;
-
-       /* After a network error (+C_TEAR_DOWN) only C_UNCONNECTED or C_DISCONNECTING can follow.
-        * If you try to go into some Sync* state, that shall fail (elsewhere). */
-       if (os.conn >= C_TIMEOUT && os.conn <= C_TEAR_DOWN &&
-           ns.conn != C_UNCONNECTED && ns.conn != C_DISCONNECTING && ns.conn <= C_CONNECTED)
-               ns.conn = os.conn;
-
-       /* we cannot fail (again) if we already detached */
-       if (ns.disk == D_FAILED && os.disk == D_DISKLESS)
-               ns.disk = D_DISKLESS;
-
-       /* After C_DISCONNECTING only C_STANDALONE may follow */
-       if (os.conn == C_DISCONNECTING && ns.conn != C_STANDALONE)
-               ns.conn = os.conn;
-
-       if (ns.conn < C_CONNECTED) {
-               ns.peer_isp = 0;
-               ns.peer = R_UNKNOWN;
-               if (ns.pdsk > D_UNKNOWN || ns.pdsk < D_INCONSISTENT)
-                       ns.pdsk = D_UNKNOWN;
-       }
-
-       /* Clear the aftr_isp when becoming unconfigured */
-       if (ns.conn == C_STANDALONE && ns.disk == D_DISKLESS && ns.role == R_SECONDARY)
-               ns.aftr_isp = 0;
-
-       /* Abort resync if a disk fails/detaches */
-       if (os.conn > C_CONNECTED && ns.conn > C_CONNECTED &&
-           (ns.disk <= D_FAILED || ns.pdsk <= D_FAILED)) {
-               if (warn)
-                       *warn = os.conn == C_VERIFY_S || os.conn == C_VERIFY_T ?
-                               ABORTED_ONLINE_VERIFY : ABORTED_RESYNC;
-               ns.conn = C_CONNECTED;
-       }
-
-       /* Connection breaks down before we finished "Negotiating" */
-       if (ns.conn < C_CONNECTED && ns.disk == D_NEGOTIATING &&
-           get_ldev_if_state(mdev, D_NEGOTIATING)) {
-               if (mdev->ed_uuid == mdev->ldev->md.uuid[UI_CURRENT]) {
-                       ns.disk = mdev->new_state_tmp.disk;
-                       ns.pdsk = mdev->new_state_tmp.pdsk;
-               } else {
-                       if (warn)
-                               *warn = CONNECTION_LOST_NEGOTIATING;
-                       ns.disk = D_DISKLESS;
-                       ns.pdsk = D_UNKNOWN;
-               }
-               put_ldev(mdev);
-       }
-
-       /* D_CONSISTENT and D_OUTDATED vanish when we get connected */
-       if (ns.conn >= C_CONNECTED && ns.conn < C_AHEAD) {
-               if (ns.disk == D_CONSISTENT || ns.disk == D_OUTDATED)
-                       ns.disk = D_UP_TO_DATE;
-               if (ns.pdsk == D_CONSISTENT || ns.pdsk == D_OUTDATED)
-                       ns.pdsk = D_UP_TO_DATE;
-       }
-
-       /* Implications of the connection stat on the disk states */
-       disk_min = D_DISKLESS;
-       disk_max = D_UP_TO_DATE;
-       pdsk_min = D_INCONSISTENT;
-       pdsk_max = D_UNKNOWN;
-       switch ((enum drbd_conns)ns.conn) {
-       case C_WF_BITMAP_T:
-       case C_PAUSED_SYNC_T:
-       case C_STARTING_SYNC_T:
-       case C_WF_SYNC_UUID:
-       case C_BEHIND:
-               disk_min = D_INCONSISTENT;
-               disk_max = D_OUTDATED;
-               pdsk_min = D_UP_TO_DATE;
-               pdsk_max = D_UP_TO_DATE;
-               break;
-       case C_VERIFY_S:
-       case C_VERIFY_T:
-               disk_min = D_UP_TO_DATE;
-               disk_max = D_UP_TO_DATE;
-               pdsk_min = D_UP_TO_DATE;
-               pdsk_max = D_UP_TO_DATE;
-               break;
-       case C_CONNECTED:
-               disk_min = D_DISKLESS;
-               disk_max = D_UP_TO_DATE;
-               pdsk_min = D_DISKLESS;
-               pdsk_max = D_UP_TO_DATE;
-               break;
-       case C_WF_BITMAP_S:
-       case C_PAUSED_SYNC_S:
-       case C_STARTING_SYNC_S:
-       case C_AHEAD:
-               disk_min = D_UP_TO_DATE;
-               disk_max = D_UP_TO_DATE;
-               pdsk_min = D_INCONSISTENT;
-               pdsk_max = D_CONSISTENT; /* D_OUTDATED would be nice. But explicit outdate necessary*/
-               break;
-       case C_SYNC_TARGET:
-               disk_min = D_INCONSISTENT;
-               disk_max = D_INCONSISTENT;
-               pdsk_min = D_UP_TO_DATE;
-               pdsk_max = D_UP_TO_DATE;
-               break;
-       case C_SYNC_SOURCE:
-               disk_min = D_UP_TO_DATE;
-               disk_max = D_UP_TO_DATE;
-               pdsk_min = D_INCONSISTENT;
-               pdsk_max = D_INCONSISTENT;
-               break;
-       case C_STANDALONE:
-       case C_DISCONNECTING:
-       case C_UNCONNECTED:
-       case C_TIMEOUT:
-       case C_BROKEN_PIPE:
-       case C_NETWORK_FAILURE:
-       case C_PROTOCOL_ERROR:
-       case C_TEAR_DOWN:
-       case C_WF_CONNECTION:
-       case C_WF_REPORT_PARAMS:
-       case C_MASK:
-               break;
-       }
-       if (ns.disk > disk_max)
-               ns.disk = disk_max;
-
-       if (ns.disk < disk_min) {
-               if (warn)
-                       *warn = IMPLICITLY_UPGRADED_DISK;
-               ns.disk = disk_min;
-       }
-       if (ns.pdsk > pdsk_max)
-               ns.pdsk = pdsk_max;
-
-       if (ns.pdsk < pdsk_min) {
-               if (warn)
-                       *warn = IMPLICITLY_UPGRADED_PDSK;
-               ns.pdsk = pdsk_min;
-       }
-
-       if (fp == FP_STONITH &&
-           (ns.role == R_PRIMARY && ns.conn < C_CONNECTED && ns.pdsk > D_OUTDATED) &&
-           !(os.role == R_PRIMARY && os.conn < C_CONNECTED && os.pdsk > D_OUTDATED))
-               ns.susp_fen = 1; /* Suspend IO while fence-peer handler runs (peer lost) */
-
-       if (mdev->sync_conf.on_no_data == OND_SUSPEND_IO &&
-           (ns.role == R_PRIMARY && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE) &&
-           !(os.role == R_PRIMARY && os.disk < D_UP_TO_DATE && os.pdsk < D_UP_TO_DATE))
-               ns.susp_nod = 1; /* Suspend IO while no data available (no accessible data available) */
-
-       if (ns.aftr_isp || ns.peer_isp || ns.user_isp) {
-               if (ns.conn == C_SYNC_SOURCE)
-                       ns.conn = C_PAUSED_SYNC_S;
-               if (ns.conn == C_SYNC_TARGET)
-                       ns.conn = C_PAUSED_SYNC_T;
-       } else {
-               if (ns.conn == C_PAUSED_SYNC_S)
-                       ns.conn = C_SYNC_SOURCE;
-               if (ns.conn == C_PAUSED_SYNC_T)
-                       ns.conn = C_SYNC_TARGET;
-       }
-
-       return ns;
-}
-
-/* helper for __drbd_set_state */
-static void set_ov_position(struct drbd_conf *mdev, enum drbd_conns cs)
-{
-       if (mdev->agreed_pro_version < 90)
-               mdev->ov_start_sector = 0;
-       mdev->rs_total = drbd_bm_bits(mdev);
-       mdev->ov_position = 0;
-       if (cs == C_VERIFY_T) {
-               /* starting online verify from an arbitrary position
-                * does not fit well into the existing protocol.
-                * on C_VERIFY_T, we initialize ov_left and friends
-                * implicitly in receive_DataRequest once the
-                * first P_OV_REQUEST is received */
-               mdev->ov_start_sector = ~(sector_t)0;
-       } else {
-               unsigned long bit = BM_SECT_TO_BIT(mdev->ov_start_sector);
-               if (bit >= mdev->rs_total) {
-                       mdev->ov_start_sector =
-                               BM_BIT_TO_SECT(mdev->rs_total - 1);
-                       mdev->rs_total = 1;
-               } else
-                       mdev->rs_total -= bit;
-               mdev->ov_position = mdev->ov_start_sector;
-       }
-       mdev->ov_left = mdev->rs_total;
-}
-
-static void drbd_resume_al(struct drbd_conf *mdev)
-{
-       if (test_and_clear_bit(AL_SUSPENDED, &mdev->flags))
-               dev_info(DEV, "Resumed AL updates\n");
-}
-
-/**
- * __drbd_set_state() - Set a new DRBD state
- * @mdev:      DRBD device.
- * @ns:                new state.
- * @flags:     Flags
- * @done:      Optional completion, that will get completed after the after_state_ch() finished
- *
- * Caller needs to hold req_lock, and global_state_lock. Do not call directly.
- */
-enum drbd_state_rv
-__drbd_set_state(struct drbd_conf *mdev, union drbd_state ns,
-                enum chg_state_flags flags, struct completion *done)
-{
-       union drbd_state os;
-       enum drbd_state_rv rv = SS_SUCCESS;
-       enum sanitize_state_warnings ssw;
-       struct after_state_chg_work *ascw;
-
-       os = mdev->state;
-
-       ns = sanitize_state(mdev, os, ns, &ssw);
-
-       if (ns.i == os.i)
-               return SS_NOTHING_TO_DO;
-
-       if (!(flags & CS_HARD)) {
-               /*  pre-state-change checks ; only look at ns  */
-               /* See drbd_state_sw_errors in drbd_strings.c */
-
-               rv = is_valid_state(mdev, ns);
-               if (rv < SS_SUCCESS) {
-                       /* If the old state was illegal as well, then let
-                          this happen...*/
-
-                       if (is_valid_state(mdev, os) == rv)
-                               rv = is_valid_state_transition(mdev, ns, os);
-               } else
-                       rv = is_valid_state_transition(mdev, ns, os);
-       }
-
-       if (rv < SS_SUCCESS) {
-               if (flags & CS_VERBOSE)
-                       print_st_err(mdev, os, ns, rv);
-               return rv;
-       }
-
-       print_sanitize_warnings(mdev, ssw);
-
-       {
-       char *pbp, pb[300];
-       pbp = pb;
-       *pbp = 0;
-       if (ns.role != os.role)
-               pbp += sprintf(pbp, "role( %s -> %s ) ",
-                              drbd_role_str(os.role),
-                              drbd_role_str(ns.role));
-       if (ns.peer != os.peer)
-               pbp += sprintf(pbp, "peer( %s -> %s ) ",
-                              drbd_role_str(os.peer),
-                              drbd_role_str(ns.peer));
-       if (ns.conn != os.conn)
-               pbp += sprintf(pbp, "conn( %s -> %s ) ",
-                              drbd_conn_str(os.conn),
-                              drbd_conn_str(ns.conn));
-       if (ns.disk != os.disk)
-               pbp += sprintf(pbp, "disk( %s -> %s ) ",
-                              drbd_disk_str(os.disk),
-                              drbd_disk_str(ns.disk));
-       if (ns.pdsk != os.pdsk)
-               pbp += sprintf(pbp, "pdsk( %s -> %s ) ",
-                              drbd_disk_str(os.pdsk),
-                              drbd_disk_str(ns.pdsk));
-       if (is_susp(ns) != is_susp(os))
-               pbp += sprintf(pbp, "susp( %d -> %d ) ",
-                              is_susp(os),
-                              is_susp(ns));
-       if (ns.aftr_isp != os.aftr_isp)
-               pbp += sprintf(pbp, "aftr_isp( %d -> %d ) ",
-                              os.aftr_isp,
-                              ns.aftr_isp);
-       if (ns.peer_isp != os.peer_isp)
-               pbp += sprintf(pbp, "peer_isp( %d -> %d ) ",
-                              os.peer_isp,
-                              ns.peer_isp);
-       if (ns.user_isp != os.user_isp)
-               pbp += sprintf(pbp, "user_isp( %d -> %d ) ",
-                              os.user_isp,
-                              ns.user_isp);
-       dev_info(DEV, "%s\n", pb);
-       }
-
-       /* solve the race between becoming unconfigured,
-        * worker doing the cleanup, and
-        * admin reconfiguring us:
-        * on (re)configure, first set CONFIG_PENDING,
-        * then wait for a potentially exiting worker,
-        * start the worker, and schedule one no_op.
-        * then proceed with configuration.
-        */
-       if (ns.disk == D_DISKLESS &&
-           ns.conn == C_STANDALONE &&
-           ns.role == R_SECONDARY &&
-           !test_and_set_bit(CONFIG_PENDING, &mdev->flags))
-               set_bit(DEVICE_DYING, &mdev->flags);
-
-       /* if we are going -> D_FAILED or D_DISKLESS, grab one extra reference
-        * on the ldev here, to be sure the transition -> D_DISKLESS resp.
-        * drbd_ldev_destroy() won't happen before our corresponding
-        * after_state_ch works run, where we put_ldev again. */
-       if ((os.disk != D_FAILED && ns.disk == D_FAILED) ||
-           (os.disk != D_DISKLESS && ns.disk == D_DISKLESS))
-               atomic_inc(&mdev->local_cnt);
-
-       mdev->state = ns;
-
-       if (os.disk == D_ATTACHING && ns.disk >= D_NEGOTIATING)
-               drbd_print_uuids(mdev, "attached to UUIDs");
-
-       wake_up(&mdev->misc_wait);
-       wake_up(&mdev->state_wait);
-
-       /* aborted verify run. log the last position */
-       if ((os.conn == C_VERIFY_S || os.conn == C_VERIFY_T) &&
-           ns.conn < C_CONNECTED) {
-               mdev->ov_start_sector =
-                       BM_BIT_TO_SECT(drbd_bm_bits(mdev) - mdev->ov_left);
-               dev_info(DEV, "Online Verify reached sector %llu\n",
-                       (unsigned long long)mdev->ov_start_sector);
-       }
-
-       if ((os.conn == C_PAUSED_SYNC_T || os.conn == C_PAUSED_SYNC_S) &&
-           (ns.conn == C_SYNC_TARGET  || ns.conn == C_SYNC_SOURCE)) {
-               dev_info(DEV, "Syncer continues.\n");
-               mdev->rs_paused += (long)jiffies
-                                 -(long)mdev->rs_mark_time[mdev->rs_last_mark];
-               if (ns.conn == C_SYNC_TARGET)
-                       mod_timer(&mdev->resync_timer, jiffies);
-       }
-
-       if ((os.conn == C_SYNC_TARGET  || os.conn == C_SYNC_SOURCE) &&
-           (ns.conn == C_PAUSED_SYNC_T || ns.conn == C_PAUSED_SYNC_S)) {
-               dev_info(DEV, "Resync suspended\n");
-               mdev->rs_mark_time[mdev->rs_last_mark] = jiffies;
-       }
-
-       if (os.conn == C_CONNECTED &&
-           (ns.conn == C_VERIFY_S || ns.conn == C_VERIFY_T)) {
-               unsigned long now = jiffies;
-               int i;
-
-               set_ov_position(mdev, ns.conn);
-               mdev->rs_start = now;
-               mdev->rs_last_events = 0;
-               mdev->rs_last_sect_ev = 0;
-               mdev->ov_last_oos_size = 0;
-               mdev->ov_last_oos_start = 0;
-
-               for (i = 0; i < DRBD_SYNC_MARKS; i++) {
-                       mdev->rs_mark_left[i] = mdev->ov_left;
-                       mdev->rs_mark_time[i] = now;
-               }
-
-               drbd_rs_controller_reset(mdev);
-
-               if (ns.conn == C_VERIFY_S) {
-                       dev_info(DEV, "Starting Online Verify from sector %llu\n",
-                                       (unsigned long long)mdev->ov_position);
-                       mod_timer(&mdev->resync_timer, jiffies);
-               }
-       }
-
-       if (get_ldev(mdev)) {
-               u32 mdf = mdev->ldev->md.flags & ~(MDF_CONSISTENT|MDF_PRIMARY_IND|
-                                                MDF_CONNECTED_IND|MDF_WAS_UP_TO_DATE|
-                                                MDF_PEER_OUT_DATED|MDF_CRASHED_PRIMARY);
-
-               if (test_bit(CRASHED_PRIMARY, &mdev->flags))
-                       mdf |= MDF_CRASHED_PRIMARY;
-               if (mdev->state.role == R_PRIMARY ||
-                   (mdev->state.pdsk < D_INCONSISTENT && mdev->state.peer == R_PRIMARY))
-                       mdf |= MDF_PRIMARY_IND;
-               if (mdev->state.conn > C_WF_REPORT_PARAMS)
-                       mdf |= MDF_CONNECTED_IND;
-               if (mdev->state.disk > D_INCONSISTENT)
-                       mdf |= MDF_CONSISTENT;
-               if (mdev->state.disk > D_OUTDATED)
-                       mdf |= MDF_WAS_UP_TO_DATE;
-               if (mdev->state.pdsk <= D_OUTDATED && mdev->state.pdsk >= D_INCONSISTENT)
-                       mdf |= MDF_PEER_OUT_DATED;
-               if (mdf != mdev->ldev->md.flags) {
-                       mdev->ldev->md.flags = mdf;
-                       drbd_md_mark_dirty(mdev);
-               }
-               if (os.disk < D_CONSISTENT && ns.disk >= D_CONSISTENT)
-                       drbd_set_ed_uuid(mdev, mdev->ldev->md.uuid[UI_CURRENT]);
-               put_ldev(mdev);
-       }
-
-       /* Peer was forced D_UP_TO_DATE & R_PRIMARY, consider to resync */
-       if (os.disk == D_INCONSISTENT && os.pdsk == D_INCONSISTENT &&
-           os.peer == R_SECONDARY && ns.peer == R_PRIMARY)
-               set_bit(CONSIDER_RESYNC, &mdev->flags);
-
-       /* Receiver should clean up itself */
-       if (os.conn != C_DISCONNECTING && ns.conn == C_DISCONNECTING)
-               drbd_thread_stop_nowait(&mdev->receiver);
-
-       /* Now the receiver finished cleaning up itself, it should die */
-       if (os.conn != C_STANDALONE && ns.conn == C_STANDALONE)
-               drbd_thread_stop_nowait(&mdev->receiver);
-
-       /* Upon network failure, we need to restart the receiver. */
-       if (os.conn > C_WF_CONNECTION &&
-           ns.conn <= C_TEAR_DOWN && ns.conn >= C_TIMEOUT)
-               drbd_thread_restart_nowait(&mdev->receiver);
-
-       /* Resume AL writing if we get a connection */
-       if (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED)
-               drbd_resume_al(mdev);
-
-       /* remember last connect and attach times so request_timer_fn() won't
-        * kill newly established sessions while we are still trying to thaw
-        * previously frozen IO */
-       if (os.conn != C_WF_REPORT_PARAMS && ns.conn == C_WF_REPORT_PARAMS)
-               mdev->last_reconnect_jif = jiffies;
-       if ((os.disk == D_ATTACHING || os.disk == D_NEGOTIATING) &&
-           ns.disk > D_NEGOTIATING)
-               mdev->last_reattach_jif = jiffies;
-
-       ascw = kmalloc(sizeof(*ascw), GFP_ATOMIC);
-       if (ascw) {
-               ascw->os = os;
-               ascw->ns = ns;
-               ascw->flags = flags;
-               ascw->w.cb = w_after_state_ch;
-               ascw->done = done;
-               drbd_queue_work(&mdev->data.work, &ascw->w);
-       } else {
-               dev_warn(DEV, "Could not kmalloc an ascw\n");
-       }
-
-       return rv;
-}
-
-static int w_after_state_ch(struct drbd_conf *mdev, struct drbd_work *w, int unused)
-{
-       struct after_state_chg_work *ascw =
-               container_of(w, struct after_state_chg_work, w);
-       after_state_ch(mdev, ascw->os, ascw->ns, ascw->flags);
-       if (ascw->flags & CS_WAIT_COMPLETE) {
-               D_ASSERT(ascw->done != NULL);
-               complete(ascw->done);
-       }
-       kfree(ascw);
-
-       return 1;
-}
-
-static void abw_start_sync(struct drbd_conf *mdev, int rv)
-{
-       if (rv) {
-               dev_err(DEV, "Writing the bitmap failed not starting resync.\n");
-               _drbd_request_state(mdev, NS(conn, C_CONNECTED), CS_VERBOSE);
-               return;
-       }
-
-       switch (mdev->state.conn) {
-       case C_STARTING_SYNC_T:
-               _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE);
-               break;
-       case C_STARTING_SYNC_S:
-               drbd_start_resync(mdev, C_SYNC_SOURCE);
-               break;
-       }
-}
-
-int drbd_bitmap_io_from_worker(struct drbd_conf *mdev,
-               int (*io_fn)(struct drbd_conf *),
-               char *why, enum bm_flag flags)
-{
-       int rv;
-
-       D_ASSERT(current == mdev->worker.task);
-
-       /* open coded non-blocking drbd_suspend_io(mdev); */
-       set_bit(SUSPEND_IO, &mdev->flags);
-
-       drbd_bm_lock(mdev, why, flags);
-       rv = io_fn(mdev);
-       drbd_bm_unlock(mdev);
-
-       drbd_resume_io(mdev);
-
-       return rv;
+       return io_allowed;
 }
 
+#endif
+
 /**
- * after_state_ch() - Perform after state change actions that may sleep
- * @mdev:      DRBD device.
- * @os:                old state.
- * @ns:                new state.
- * @flags:     Flags
+ * tl_release() - mark as BARRIER_ACKED all requests in the corresponding transfer log epoch
+ * @tconn:     DRBD connection.
+ * @barrier_nr:        Expected identifier of the DRBD write barrier packet.
+ * @set_size:  Expected number of requests before that barrier.
+ *
+ * In case the passed barrier_nr or set_size does not match the oldest
+ * epoch of not yet barrier-acked requests, this function will cause a
+ * termination of the connection.
  */
-static void after_state_ch(struct drbd_conf *mdev, union drbd_state os,
-                          union drbd_state ns, enum chg_state_flags flags)
+void tl_release(struct drbd_tconn *tconn, unsigned int barrier_nr,
+               unsigned int set_size)
 {
-       enum drbd_fencing_p fp;
-       enum drbd_req_event what = nothing;
-       union drbd_state nsm = (union drbd_state){ .i = -1 };
-
-       if (os.conn != C_CONNECTED && ns.conn == C_CONNECTED) {
-               clear_bit(CRASHED_PRIMARY, &mdev->flags);
-               if (mdev->p_uuid)
-                       mdev->p_uuid[UI_FLAGS] &= ~((u64)2);
-       }
-
-       fp = FP_DONT_CARE;
-       if (get_ldev(mdev)) {
-               fp = mdev->ldev->dc.fencing;
-               put_ldev(mdev);
-       }
-
-       /* Inform userspace about the change... */
-       drbd_bcast_state(mdev, ns);
-
-       if (!(os.role == R_PRIMARY && os.disk < D_UP_TO_DATE && os.pdsk < D_UP_TO_DATE) &&
-           (ns.role == R_PRIMARY && ns.disk < D_UP_TO_DATE && ns.pdsk < D_UP_TO_DATE))
-               drbd_khelper(mdev, "pri-on-incon-degr");
-
-       /* Here we have the actions that are performed after a
-          state change. This function might sleep */
-
-       if (os.disk <= D_NEGOTIATING && ns.disk > D_NEGOTIATING)
-               mod_timer(&mdev->request_timer, jiffies + HZ);
-
-       nsm.i = -1;
-       if (ns.susp_nod) {
-               if (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED)
-                       what = resend;
-
-               if ((os.disk == D_ATTACHING || os.disk == D_NEGOTIATING) &&
-                   ns.disk > D_NEGOTIATING)
-                       what = restart_frozen_disk_io;
-
-               if (what != nothing)
-                       nsm.susp_nod = 0;
-       }
-
-       if (ns.susp_fen) {
-               /* case1: The outdate peer handler is successful: */
-               if (os.pdsk > D_OUTDATED  && ns.pdsk <= D_OUTDATED) {
-                       if (test_bit(NEW_CUR_UUID, &mdev->flags)) {
-                               drbd_uuid_new_current(mdev);
-                               clear_bit(NEW_CUR_UUID, &mdev->flags);
-                       }
-                       spin_lock_irq(&mdev->req_lock);
-                       _tl_clear(mdev);
-                       _drbd_set_state(_NS(mdev, susp_fen, 0), CS_VERBOSE, NULL);
-                       spin_unlock_irq(&mdev->req_lock);
-               }
-               /* case2: The connection was established again: */
-               if (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED) {
-                       clear_bit(NEW_CUR_UUID, &mdev->flags);
-                       what = resend;
-                       nsm.susp_fen = 0;
+       struct drbd_request *r;
+       struct drbd_request *req = NULL;
+       int expect_epoch = 0;
+       int expect_size = 0;
+
+       spin_lock_irq(&tconn->req_lock);
+
+       /* find oldest not yet barrier-acked write request,
+        * count writes in its epoch. */
+       list_for_each_entry(r, &tconn->transfer_log, tl_requests) {
+               const unsigned s = r->rq_state;
+               if (!req) {
+                       if (!(s & RQ_WRITE))
+                               continue;
+                       if (!(s & RQ_NET_MASK))
+                               continue;
+                       if (s & RQ_NET_DONE)
+                               continue;
+                       req = r;
+                       expect_epoch = req->epoch;
+                       expect_size ++;
+               } else {
+                       if (r->epoch != expect_epoch)
+                               break;
+                       if (!(s & RQ_WRITE))
+                               continue;
+                       /* if (s & RQ_DONE): not expected */
+                       /* if (!(s & RQ_NET_MASK)): not expected */
+                       expect_size++;
                }
        }
 
-       if (what != nothing) {
-               spin_lock_irq(&mdev->req_lock);
-               _tl_restart(mdev, what);
-               nsm.i &= mdev->state.i;
-               _drbd_set_state(mdev, nsm, CS_VERBOSE, NULL);
-               spin_unlock_irq(&mdev->req_lock);
+       /* first some paranoia code */
+       if (req == NULL) {
+               conn_err(tconn, "BAD! BarrierAck #%u received, but no epoch in tl!?\n",
+                        barrier_nr);
+               goto bail;
        }
-
-       /* Became sync source.  With protocol >= 96, we still need to send out
-        * the sync uuid now. Need to do that before any drbd_send_state, or
-        * the other side may go "paused sync" before receiving the sync uuids,
-        * which is unexpected. */
-       if ((os.conn != C_SYNC_SOURCE && os.conn != C_PAUSED_SYNC_S) &&
-           (ns.conn == C_SYNC_SOURCE || ns.conn == C_PAUSED_SYNC_S) &&
-           mdev->agreed_pro_version >= 96 && get_ldev(mdev)) {
-               drbd_gen_and_send_sync_uuid(mdev);
-               put_ldev(mdev);
+       if (expect_epoch != barrier_nr) {
+               conn_err(tconn, "BAD! BarrierAck #%u received, expected #%u!\n",
+                        barrier_nr, expect_epoch);
+               goto bail;
        }
 
-       /* Do not change the order of the if above and the two below... */
-       if (os.pdsk == D_DISKLESS && ns.pdsk > D_DISKLESS) {      /* attach on the peer */
-               /* we probably will start a resync soon.
-                * make sure those things are properly reset. */
-               mdev->rs_total = 0;
-               mdev->rs_failed = 0;
-               atomic_set(&mdev->rs_pending_cnt, 0);
-               drbd_rs_cancel_all(mdev);
-
-               drbd_send_uuids(mdev);
-               drbd_send_state(mdev, ns);
-       }
-       /* No point in queuing send_bitmap if we don't have a connection
-        * anymore, so check also the _current_ state, not only the new state
-        * at the time this work was queued. */
-       if (os.conn != C_WF_BITMAP_S && ns.conn == C_WF_BITMAP_S &&
-           mdev->state.conn == C_WF_BITMAP_S)
-               drbd_queue_bitmap_io(mdev, &drbd_send_bitmap, NULL,
-                               "send_bitmap (WFBitMapS)",
-                               BM_LOCKED_TEST_ALLOWED);
-
-       /* Lost contact to peer's copy of the data */
-       if ((os.pdsk >= D_INCONSISTENT &&
-            os.pdsk != D_UNKNOWN &&
-            os.pdsk != D_OUTDATED)
-       &&  (ns.pdsk < D_INCONSISTENT ||
-            ns.pdsk == D_UNKNOWN ||
-            ns.pdsk == D_OUTDATED)) {
-               if (get_ldev(mdev)) {
-                       if ((ns.role == R_PRIMARY || ns.peer == R_PRIMARY) &&
-                           mdev->ldev->md.uuid[UI_BITMAP] == 0 && ns.disk >= D_UP_TO_DATE) {
-                               if (is_susp(mdev->state)) {
-                                       set_bit(NEW_CUR_UUID, &mdev->flags);
-                               } else {
-                                       drbd_uuid_new_current(mdev);
-                                       drbd_send_uuids(mdev);
-                               }
-                       }
-                       put_ldev(mdev);
-               }
+       if (expect_size != set_size) {
+               conn_err(tconn, "BAD! BarrierAck #%u received with n_writes=%u, expected n_writes=%u!\n",
+                        barrier_nr, set_size, expect_size);
+               goto bail;
        }
 
-       if (ns.pdsk < D_INCONSISTENT && get_ldev(mdev)) {
-               if (os.peer == R_SECONDARY && ns.peer == R_PRIMARY &&
-                   mdev->ldev->md.uuid[UI_BITMAP] == 0 && ns.disk >= D_UP_TO_DATE) {
-                       drbd_uuid_new_current(mdev);
-                       drbd_send_uuids(mdev);
-               }
-               /* D_DISKLESS Peer becomes secondary */
-               if (os.peer == R_PRIMARY && ns.peer == R_SECONDARY)
-                       /* We may still be Primary ourselves.
-                        * No harm done if the bitmap still changes,
-                        * redirtied pages will follow later. */
-                       drbd_bitmap_io_from_worker(mdev, &drbd_bm_write,
-                               "demote diskless peer", BM_LOCKED_SET_ALLOWED);
-               put_ldev(mdev);
+       /* Clean up list of requests processed during current epoch. */
+       /* this extra list walk restart is paranoia,
+        * to catch requests being barrier-acked "unexpectedly".
+        * It usually should find the same req again, or some READ preceding it. */
+       list_for_each_entry(req, &tconn->transfer_log, tl_requests)
+               if (req->epoch == expect_epoch)
+                       break;
+       list_for_each_entry_safe_from(req, r, &tconn->transfer_log, tl_requests) {
+               if (req->epoch != expect_epoch)
+                       break;
+               _req_mod(req, BARRIER_ACKED);
        }
+       spin_unlock_irq(&tconn->req_lock);
 
-       /* Write out all changed bits on demote.
-        * Though, no need to da that just yet
-        * if there is a resync going on still */
-       if (os.role == R_PRIMARY && ns.role == R_SECONDARY &&
-               mdev->state.conn <= C_CONNECTED && get_ldev(mdev)) {
-               /* No changes to the bitmap expected this time, so assert that,
-                * even though no harm was done if it did change. */
-               drbd_bitmap_io_from_worker(mdev, &drbd_bm_write,
-                               "demote", BM_LOCKED_TEST_ALLOWED);
-               put_ldev(mdev);
-       }
+       return;
 
-       /* Last part of the attaching process ... */
-       if (ns.conn >= C_CONNECTED &&
-           os.disk == D_ATTACHING && ns.disk == D_NEGOTIATING) {
-               drbd_send_sizes(mdev, 0, 0);  /* to start sync... */
-               drbd_send_uuids(mdev);
-               drbd_send_state(mdev, ns);
-       }
+bail:
+       spin_unlock_irq(&tconn->req_lock);
+       conn_request_state(tconn, NS(conn, C_PROTOCOL_ERROR), CS_HARD);
+}
 
-       /* We want to pause/continue resync, tell peer. */
-       if (ns.conn >= C_CONNECTED &&
-            ((os.aftr_isp != ns.aftr_isp) ||
-             (os.user_isp != ns.user_isp)))
-               drbd_send_state(mdev, ns);
-
-       /* In case one of the isp bits got set, suspend other devices. */
-       if ((!os.aftr_isp && !os.peer_isp && !os.user_isp) &&
-           (ns.aftr_isp || ns.peer_isp || ns.user_isp))
-               suspend_other_sg(mdev);
-
-       /* Make sure the peer gets informed about eventual state
-          changes (ISP bits) while we were in WFReportParams. */
-       if (os.conn == C_WF_REPORT_PARAMS && ns.conn >= C_CONNECTED)
-               drbd_send_state(mdev, ns);
-
-       if (os.conn != C_AHEAD && ns.conn == C_AHEAD)
-               drbd_send_state(mdev, ns);
-
-       /* We are in the progress to start a full sync... */
-       if ((os.conn != C_STARTING_SYNC_T && ns.conn == C_STARTING_SYNC_T) ||
-           (os.conn != C_STARTING_SYNC_S && ns.conn == C_STARTING_SYNC_S))
-               /* no other bitmap changes expected during this phase */
-               drbd_queue_bitmap_io(mdev,
-                       &drbd_bmio_set_n_write, &abw_start_sync,
-                       "set_n_write from StartingSync", BM_LOCKED_TEST_ALLOWED);
-
-       /* We are invalidating our self... */
-       if (os.conn < C_CONNECTED && ns.conn < C_CONNECTED &&
-           os.disk > D_INCONSISTENT && ns.disk == D_INCONSISTENT)
-               /* other bitmap operation expected during this phase */
-               drbd_queue_bitmap_io(mdev, &drbd_bmio_set_n_write, NULL,
-                       "set_n_write from invalidate", BM_LOCKED_MASK);
-
-       /* first half of local IO error, failure to attach,
-        * or administrative detach */
-       if (os.disk != D_FAILED && ns.disk == D_FAILED) {
-               enum drbd_io_error_p eh = EP_PASS_ON;
-               int was_io_error = 0;
-               /* corresponding get_ldev was in __drbd_set_state, to serialize
-                * our cleanup here with the transition to D_DISKLESS.
-                * But is is still not save to dreference ldev here, since
-                * we might come from an failed Attach before ldev was set. */
-               if (mdev->ldev) {
-                       eh = mdev->ldev->dc.on_io_error;
-                       was_io_error = test_and_clear_bit(WAS_IO_ERROR, &mdev->flags);
-
-                       if (was_io_error && eh == EP_CALL_HELPER)
-                               drbd_khelper(mdev, "local-io-error");
-
-                       /* Immediately allow completion of all application IO,
-                        * that waits for completion from the local disk,
-                        * if this was a force-detach due to disk_timeout
-                        * or administrator request (drbdsetup detach --force).
-                        * Do NOT abort otherwise.
-                        * Aborting local requests may cause serious problems,
-                        * if requests are completed to upper layers already,
-                        * and then later the already submitted local bio completes.
-                        * This can cause DMA into former bio pages that meanwhile
-                        * have been re-used for other things.
-                        * So aborting local requests may cause crashes,
-                        * or even worse, silent data corruption.
-                        */
-                       if (test_and_clear_bit(FORCE_DETACH, &mdev->flags))
-                               tl_abort_disk_io(mdev);
-
-                       /* current state still has to be D_FAILED,
-                        * there is only one way out: to D_DISKLESS,
-                        * and that may only happen after our put_ldev below. */
-                       if (mdev->state.disk != D_FAILED)
-                               dev_err(DEV,
-                                       "ASSERT FAILED: disk is %s during detach\n",
-                                       drbd_disk_str(mdev->state.disk));
-
-                       if (ns.conn >= C_CONNECTED)
-                               drbd_send_state(mdev, ns);
-
-                       drbd_rs_cancel_all(mdev);
-
-                       /* In case we want to get something to stable storage still,
-                        * this may be the last chance.
-                        * Following put_ldev may transition to D_DISKLESS. */
-                       drbd_md_sync(mdev);
-               }
-               put_ldev(mdev);
-       }
 
-        /* second half of local IO error, failure to attach,
-         * or administrative detach,
-         * after local_cnt references have reached zero again */
-        if (os.disk != D_DISKLESS && ns.disk == D_DISKLESS) {
-                /* We must still be diskless,
-                 * re-attach has to be serialized with this! */
-                if (mdev->state.disk != D_DISKLESS)
-                        dev_err(DEV,
-                                "ASSERT FAILED: disk is %s while going diskless\n",
-                                drbd_disk_str(mdev->state.disk));
-
-               if (ns.conn >= C_CONNECTED)
-                       drbd_send_state(mdev, ns);
-
-               /* corresponding get_ldev in __drbd_set_state
-                * this may finally trigger drbd_ldev_destroy. */
-               put_ldev(mdev);
-       }
+/**
+ * _tl_restart() - Walks the transfer log, and applies an action to all requests
+ * @mdev:      DRBD device.
+ * @what:       The action/event to perform with all request objects
+ *
+ * @what might be one of CONNECTION_LOST_WHILE_PENDING, RESEND, FAIL_FROZEN_DISK_IO,
+ * RESTART_FROZEN_DISK_IO.
+ */
+/* must hold resource->req_lock */
+void _tl_restart(struct drbd_tconn *tconn, enum drbd_req_event what)
+{
+       struct drbd_request *req, *r;
 
-       /* Notify peer that I had a local IO error, and did not detached.. */
-       if (os.disk == D_UP_TO_DATE && ns.disk == D_INCONSISTENT && ns.conn >= C_CONNECTED)
-               drbd_send_state(mdev, ns);
+       list_for_each_entry_safe(req, r, &tconn->transfer_log, tl_requests)
+               _req_mod(req, what);
+}
 
-       /* Disks got bigger while they were detached */
-       if (ns.disk > D_NEGOTIATING && ns.pdsk > D_NEGOTIATING &&
-           test_and_clear_bit(RESYNC_AFTER_NEG, &mdev->flags)) {
-               if (ns.conn == C_CONNECTED)
-                       resync_after_online_grow(mdev);
-       }
+void tl_restart(struct drbd_tconn *tconn, enum drbd_req_event what)
+{
+       spin_lock_irq(&tconn->req_lock);
+       _tl_restart(tconn, what);
+       spin_unlock_irq(&tconn->req_lock);
+}
 
-       /* A resync finished or aborted, wake paused devices... */
-       if ((os.conn > C_CONNECTED && ns.conn <= C_CONNECTED) ||
-           (os.peer_isp && !ns.peer_isp) ||
-           (os.user_isp && !ns.user_isp))
-               resume_next_sg(mdev);
-
-       /* sync target done with resync.  Explicitly notify peer, even though
-        * it should (at least for non-empty resyncs) already know itself. */
-       if (os.disk < D_UP_TO_DATE && os.conn >= C_SYNC_SOURCE && ns.conn == C_CONNECTED)
-               drbd_send_state(mdev, ns);
-
-       /* Wake up role changes, that were delayed because of connection establishing */
-       if (os.conn == C_WF_REPORT_PARAMS && ns.conn != C_WF_REPORT_PARAMS) {
-               clear_bit(STATE_SENT, &mdev->flags);
-               wake_up(&mdev->state_wait);
-       }
+/**
+ * tl_clear() - Clears all requests and &struct drbd_tl_epoch objects out of the TL
+ * @mdev:      DRBD device.
+ *
+ * This is called after the connection to the peer was lost. The storage covered
+ * by the requests on the transfer gets marked as our of sync. Called from the
+ * receiver thread and the worker thread.
+ */
+void tl_clear(struct drbd_tconn *tconn)
+{
+       tl_restart(tconn, CONNECTION_LOST_WHILE_PENDING);
+}
 
-       /* This triggers bitmap writeout of potentially still unwritten pages
-        * if the resync finished cleanly, or aborted because of peer disk
-        * failure, or because of connection loss.
-        * For resync aborted because of local disk failure, we cannot do
-        * any bitmap writeout anymore.
-        * No harm done if some bits change during this phase.
-        */
-       if (os.conn > C_CONNECTED && ns.conn <= C_CONNECTED && get_ldev(mdev)) {
-               drbd_queue_bitmap_io(mdev, &drbd_bm_write_copy_pages, NULL,
-                       "write from resync_finished", BM_LOCKED_CHANGE_ALLOWED);
-               put_ldev(mdev);
-       }
+/**
+ * tl_abort_disk_io() - Abort disk I/O for all requests for a certain mdev in the TL
+ * @mdev:      DRBD device.
+ */
+void tl_abort_disk_io(struct drbd_conf *mdev)
+{
+       struct drbd_tconn *tconn = mdev->tconn;
+       struct drbd_request *req, *r;
 
-       /* free tl_hash if we Got thawed and are C_STANDALONE */
-       if (ns.conn == C_STANDALONE && !is_susp(ns) && mdev->tl_hash)
-               drbd_free_tl_hash(mdev);
-
-       /* Upon network connection, we need to start the receiver */
-       if (os.conn == C_STANDALONE && ns.conn == C_UNCONNECTED)
-               drbd_thread_start(&mdev->receiver);
-
-       /* Terminate worker thread if we are unconfigured - it will be
-          restarted as needed... */
-       if (ns.disk == D_DISKLESS &&
-           ns.conn == C_STANDALONE &&
-           ns.role == R_SECONDARY) {
-               if (os.aftr_isp != ns.aftr_isp)
-                       resume_next_sg(mdev);
-               /* set in __drbd_set_state, unless CONFIG_PENDING was set */
-               if (test_bit(DEVICE_DYING, &mdev->flags))
-                       drbd_thread_stop_nowait(&mdev->worker);
+       spin_lock_irq(&tconn->req_lock);
+       list_for_each_entry_safe(req, r, &tconn->transfer_log, tl_requests) {
+               if (!(req->rq_state & RQ_LOCAL_PENDING))
+                       continue;
+               if (req->w.mdev != mdev)
+                       continue;
+               _req_mod(req, ABORT_DISK_IO);
        }
-
-       drbd_md_sync(mdev);
+       spin_unlock_irq(&tconn->req_lock);
 }
 
-
 static int drbd_thread_setup(void *arg)
 {
        struct drbd_thread *thi = (struct drbd_thread *) arg;
-       struct drbd_conf *mdev = thi->mdev;
+       struct drbd_tconn *tconn = thi->tconn;
        unsigned long flags;
        int retval;
 
+       snprintf(current->comm, sizeof(current->comm), "drbd_%c_%s",
+                thi->name[0], thi->tconn->name);
+
 restart:
        retval = thi->function(thi);
 
        spin_lock_irqsave(&thi->t_lock, flags);
 
-       /* if the receiver has been "Exiting", the last thing it did
+       /* if the receiver has been "EXITING", the last thing it did
         * was set the conn state to "StandAlone",
         * if now a re-connect request comes in, conn state goes C_UNCONNECTED,
         * and receiver thread will be "started".
-        * drbd_thread_start needs to set "Restarting" in that case.
+        * drbd_thread_start needs to set "RESTARTING" in that case.
         * t_state check and assignment needs to be within the same spinlock,
-        * so either thread_start sees Exiting, and can remap to Restarting,
-        * or thread_start see None, and can proceed as normal.
+        * so either thread_start sees EXITING, and can remap to RESTARTING,
+        * or thread_start see NONE, and can proceed as normal.
         */
 
-       if (thi->t_state == Restarting) {
-               dev_info(DEV, "Restarting %s\n", current->comm);
-               thi->t_state = Running;
+       if (thi->t_state == RESTARTING) {
+               conn_info(tconn, "Restarting %s thread\n", thi->name);
+               thi->t_state = RUNNING;
                spin_unlock_irqrestore(&thi->t_lock, flags);
                goto restart;
        }
 
        thi->task = NULL;
-       thi->t_state = None;
+       thi->t_state = NONE;
        smp_mb();
-       complete(&thi->stop);
+       complete_all(&thi->stop);
        spin_unlock_irqrestore(&thi->t_lock, flags);
 
-       dev_info(DEV, "Terminating %s\n", current->comm);
+       conn_info(tconn, "Terminating %s\n", current->comm);
 
        /* Release mod reference taken when thread was started */
+
+       kref_put(&tconn->kref, &conn_destroy);
        module_put(THIS_MODULE);
        return retval;
 }
 
-static void drbd_thread_init(struct drbd_conf *mdev, struct drbd_thread *thi,
-                     int (*func) (struct drbd_thread *))
+static void drbd_thread_init(struct drbd_tconn *tconn, struct drbd_thread *thi,
+                            int (*func) (struct drbd_thread *), char *name)
 {
        spin_lock_init(&thi->t_lock);
        thi->task    = NULL;
-       thi->t_state = None;
+       thi->t_state = NONE;
        thi->function = func;
-       thi->mdev = mdev;
+       thi->tconn = tconn;
+       strncpy(thi->name, name, ARRAY_SIZE(thi->name));
 }
 
 int drbd_thread_start(struct drbd_thread *thi)
 {
-       struct drbd_conf *mdev = thi->mdev;
+       struct drbd_tconn *tconn = thi->tconn;
        struct task_struct *nt;
        unsigned long flags;
 
-       const char *me =
-               thi == &mdev->receiver ? "receiver" :
-               thi == &mdev->asender  ? "asender"  :
-               thi == &mdev->worker   ? "worker"   : "NONSENSE";
-
        /* is used from state engine doing drbd_thread_stop_nowait,
         * while holding the req lock irqsave */
        spin_lock_irqsave(&thi->t_lock, flags);
 
        switch (thi->t_state) {
-       case None:
-               dev_info(DEV, "Starting %s thread (from %s [%d])\n",
-                               me, current->comm, current->pid);
+       case NONE:
+               conn_info(tconn, "Starting %s thread (from %s [%d])\n",
+                        thi->name, current->comm, current->pid);
 
                /* Get ref on module for thread - this is released when thread exits */
                if (!try_module_get(THIS_MODULE)) {
-                       dev_err(DEV, "Failed to get module reference in drbd_thread_start\n");
+                       conn_err(tconn, "Failed to get module reference in drbd_thread_start\n");
                        spin_unlock_irqrestore(&thi->t_lock, flags);
                        return false;
                }
 
+               kref_get(&thi->tconn->kref);
+
                init_completion(&thi->stop);
-               D_ASSERT(thi->task == NULL);
                thi->reset_cpu_mask = 1;
-               thi->t_state = Running;
+               thi->t_state = RUNNING;
                spin_unlock_irqrestore(&thi->t_lock, flags);
                flush_signals(current); /* otherw. may get -ERESTARTNOINTR */
 
                nt = kthread_create(drbd_thread_setup, (void *) thi,
-                                   "drbd%d_%s", mdev_to_minor(mdev), me);
+                                   "drbd_%c_%s", thi->name[0], thi->tconn->name);
 
                if (IS_ERR(nt)) {
-                       dev_err(DEV, "Couldn't start thread\n");
+                       conn_err(tconn, "Couldn't start thread\n");
 
+                       kref_put(&tconn->kref, &conn_destroy);
                        module_put(THIS_MODULE);
                        return false;
                }
                spin_lock_irqsave(&thi->t_lock, flags);
                thi->task = nt;
-               thi->t_state = Running;
+               thi->t_state = RUNNING;
                spin_unlock_irqrestore(&thi->t_lock, flags);
                wake_up_process(nt);
                break;
-       case Exiting:
-               thi->t_state = Restarting;
-               dev_info(DEV, "Restarting %s thread (from %s [%d])\n",
-                               me, current->comm, current->pid);
+       case EXITING:
+               thi->t_state = RESTARTING;
+               conn_info(tconn, "Restarting %s thread (from %s [%d])\n",
+                               thi->name, current->comm, current->pid);
                /* fall through */
-       case Running:
-       case Restarting:
+       case RUNNING:
+       case RESTARTING:
        default:
                spin_unlock_irqrestore(&thi->t_lock, flags);
                break;
@@ -1867,12 +447,12 @@ void _drbd_thread_stop(struct drbd_thread *thi, int restart, int wait)
 {
        unsigned long flags;
 
-       enum drbd_thread_state ns = restart ? Restarting : Exiting;
+       enum drbd_thread_state ns = restart ? RESTARTING : EXITING;
 
        /* may be called from state engine, holding the req lock irqsave */
        spin_lock_irqsave(&thi->t_lock, flags);
 
-       if (thi->t_state == None) {
+       if (thi->t_state == NONE) {
                spin_unlock_irqrestore(&thi->t_lock, flags);
                if (restart)
                        drbd_thread_start(thi);
@@ -1890,7 +470,6 @@ void _drbd_thread_stop(struct drbd_thread *thi, int restart, int wait)
                init_completion(&thi->stop);
                if (thi->task != current)
                        force_sig(DRBD_SIGKILL, thi->task);
-
        }
 
        spin_unlock_irqrestore(&thi->t_lock, flags);
@@ -1899,6 +478,35 @@ void _drbd_thread_stop(struct drbd_thread *thi, int restart, int wait)
                wait_for_completion(&thi->stop);
 }
 
+static struct drbd_thread *drbd_task_to_thread(struct drbd_tconn *tconn, struct task_struct *task)
+{
+       struct drbd_thread *thi =
+               task == tconn->receiver.task ? &tconn->receiver :
+               task == tconn->asender.task  ? &tconn->asender :
+               task == tconn->worker.task   ? &tconn->worker : NULL;
+
+       return thi;
+}
+
+char *drbd_task_to_thread_name(struct drbd_tconn *tconn, struct task_struct *task)
+{
+       struct drbd_thread *thi = drbd_task_to_thread(tconn, task);
+       return thi ? thi->name : task->comm;
+}
+
+int conn_lowest_minor(struct drbd_tconn *tconn)
+{
+       struct drbd_conf *mdev;
+       int vnr = 0, m;
+
+       rcu_read_lock();
+       mdev = idr_get_next(&tconn->volumes, &vnr);
+       m = mdev ? mdev_to_minor(mdev) : -1;
+       rcu_read_unlock();
+
+       return m;
+}
+
 #ifdef CONFIG_SMP
 /**
  * drbd_calc_cpu_mask() - Generate CPU masks, spread over all CPUs
@@ -1907,238 +515,345 @@ void _drbd_thread_stop(struct drbd_thread *thi, int restart, int wait)
  * Forces all threads of a device onto the same CPU. This is beneficial for
  * DRBD's performance. May be overwritten by user's configuration.
  */
-void drbd_calc_cpu_mask(struct drbd_conf *mdev)
+void drbd_calc_cpu_mask(struct drbd_tconn *tconn)
 {
        int ord, cpu;
 
        /* user override. */
-       if (cpumask_weight(mdev->cpu_mask))
+       if (cpumask_weight(tconn->cpu_mask))
                return;
 
-       ord = mdev_to_minor(mdev) % cpumask_weight(cpu_online_mask);
+       ord = conn_lowest_minor(tconn) % cpumask_weight(cpu_online_mask);
        for_each_online_cpu(cpu) {
                if (ord-- == 0) {
-                       cpumask_set_cpu(cpu, mdev->cpu_mask);
+                       cpumask_set_cpu(cpu, tconn->cpu_mask);
                        return;
                }
        }
        /* should not be reached */
-       cpumask_setall(mdev->cpu_mask);
+       cpumask_setall(tconn->cpu_mask);
 }
 
 /**
  * drbd_thread_current_set_cpu() - modifies the cpu mask of the _current_ thread
  * @mdev:      DRBD device.
+ * @thi:       drbd_thread object
  *
  * call in the "main loop" of _all_ threads, no need for any mutex, current won't die
  * prematurely.
  */
-void drbd_thread_current_set_cpu(struct drbd_conf *mdev)
+void drbd_thread_current_set_cpu(struct drbd_thread *thi)
 {
        struct task_struct *p = current;
-       struct drbd_thread *thi =
-               p == mdev->asender.task  ? &mdev->asender  :
-               p == mdev->receiver.task ? &mdev->receiver :
-               p == mdev->worker.task   ? &mdev->worker   :
-               NULL;
-       ERR_IF(thi == NULL)
-               return;
+
        if (!thi->reset_cpu_mask)
                return;
        thi->reset_cpu_mask = 0;
-       set_cpus_allowed_ptr(p, mdev->cpu_mask);
+       set_cpus_allowed_ptr(p, thi->tconn->cpu_mask);
 }
 #endif
 
-/* the appropriate socket mutex must be held already */
-int _drbd_send_cmd(struct drbd_conf *mdev, struct socket *sock,
-                         enum drbd_packets cmd, struct p_header80 *h,
-                         size_t size, unsigned msg_flags)
+/**
+ * drbd_header_size  -  size of a packet header
+ *
+ * The header size is a multiple of 8, so any payload following the header is
+ * word aligned on 64-bit architectures.  (The bitmap send and receive code
+ * relies on this.)
+ */
+unsigned int drbd_header_size(struct drbd_tconn *tconn)
+{
+       if (tconn->agreed_pro_version >= 100) {
+               BUILD_BUG_ON(!IS_ALIGNED(sizeof(struct p_header100), 8));
+               return sizeof(struct p_header100);
+       } else {
+               BUILD_BUG_ON(sizeof(struct p_header80) !=
+                            sizeof(struct p_header95));
+               BUILD_BUG_ON(!IS_ALIGNED(sizeof(struct p_header80), 8));
+               return sizeof(struct p_header80);
+       }
+}
+
+static unsigned int prepare_header80(struct p_header80 *h, enum drbd_packet cmd, int size)
 {
-       int sent, ok;
+       h->magic   = cpu_to_be32(DRBD_MAGIC);
+       h->command = cpu_to_be16(cmd);
+       h->length  = cpu_to_be16(size);
+       return sizeof(struct p_header80);
+}
 
-       ERR_IF(!h) return false;
-       ERR_IF(!size) return false;
+static unsigned int prepare_header95(struct p_header95 *h, enum drbd_packet cmd, int size)
+{
+       h->magic   = cpu_to_be16(DRBD_MAGIC_BIG);
+       h->command = cpu_to_be16(cmd);
+       h->length = cpu_to_be32(size);
+       return sizeof(struct p_header95);
+}
 
-       h->magic   = BE_DRBD_MAGIC;
+static unsigned int prepare_header100(struct p_header100 *h, enum drbd_packet cmd,
+                                     int size, int vnr)
+{
+       h->magic = cpu_to_be32(DRBD_MAGIC_100);
+       h->volume = cpu_to_be16(vnr);
        h->command = cpu_to_be16(cmd);
-       h->length  = cpu_to_be16(size-sizeof(struct p_header80));
+       h->length = cpu_to_be32(size);
+       h->pad = 0;
+       return sizeof(struct p_header100);
+}
 
-       sent = drbd_send(mdev, sock, h, size, msg_flags);
+static unsigned int prepare_header(struct drbd_tconn *tconn, int vnr,
+                                  void *buffer, enum drbd_packet cmd, int size)
+{
+       if (tconn->agreed_pro_version >= 100)
+               return prepare_header100(buffer, cmd, size, vnr);
+       else if (tconn->agreed_pro_version >= 95 &&
+                size > DRBD_MAX_SIZE_H80_PACKET)
+               return prepare_header95(buffer, cmd, size);
+       else
+               return prepare_header80(buffer, cmd, size);
+}
 
-       ok = (sent == size);
-       if (!ok && !signal_pending(current))
-               dev_warn(DEV, "short sent %s size=%d sent=%d\n",
-                   cmdname(cmd), (int)size, sent);
-       return ok;
+static void *__conn_prepare_command(struct drbd_tconn *tconn,
+                                   struct drbd_socket *sock)
+{
+       if (!sock->socket)
+               return NULL;
+       return sock->sbuf + drbd_header_size(tconn);
 }
 
-/* don't pass the socket. we may only look at it
- * when we hold the appropriate socket mutex.
- */
-int drbd_send_cmd(struct drbd_conf *mdev, int use_data_socket,
-                 enum drbd_packets cmd, struct p_header80 *h, size_t size)
+void *conn_prepare_command(struct drbd_tconn *tconn, struct drbd_socket *sock)
 {
-       int ok = 0;
-       struct socket *sock;
+       void *p;
 
-       if (use_data_socket) {
-               mutex_lock(&mdev->data.mutex);
-               sock = mdev->data.socket;
-       } else {
-               mutex_lock(&mdev->meta.mutex);
-               sock = mdev->meta.socket;
-       }
+       mutex_lock(&sock->mutex);
+       p = __conn_prepare_command(tconn, sock);
+       if (!p)
+               mutex_unlock(&sock->mutex);
 
-       /* drbd_disconnect() could have called drbd_free_sock()
-        * while we were waiting in down()... */
-       if (likely(sock != NULL))
-               ok = _drbd_send_cmd(mdev, sock, cmd, h, size, 0);
+       return p;
+}
 
-       if (use_data_socket)
-               mutex_unlock(&mdev->data.mutex);
-       else
-               mutex_unlock(&mdev->meta.mutex);
-       return ok;
+void *drbd_prepare_command(struct drbd_conf *mdev, struct drbd_socket *sock)
+{
+       return conn_prepare_command(mdev->tconn, sock);
 }
 
-int drbd_send_cmd2(struct drbd_conf *mdev, enum drbd_packets cmd, char *data,
-                  size_t size)
+static int __send_command(struct drbd_tconn *tconn, int vnr,
+                         struct drbd_socket *sock, enum drbd_packet cmd,
+                         unsigned int header_size, void *data,
+                         unsigned int size)
 {
-       struct p_header80 h;
-       int ok;
+       int msg_flags;
+       int err;
 
-       h.magic   = BE_DRBD_MAGIC;
-       h.command = cpu_to_be16(cmd);
-       h.length  = cpu_to_be16(size);
+       /*
+        * Called with @data == NULL and the size of the data blocks in @size
+        * for commands that send data blocks.  For those commands, omit the
+        * MSG_MORE flag: this will increase the likelihood that data blocks
+        * which are page aligned on the sender will end up page aligned on the
+        * receiver.
+        */
+       msg_flags = data ? MSG_MORE : 0;
+
+       header_size += prepare_header(tconn, vnr, sock->sbuf, cmd,
+                                     header_size + size);
+       err = drbd_send_all(tconn, sock->socket, sock->sbuf, header_size,
+                           msg_flags);
+       if (data && !err)
+               err = drbd_send_all(tconn, sock->socket, data, size, 0);
+       return err;
+}
 
-       if (!drbd_get_data_sock(mdev))
-               return 0;
+static int __conn_send_command(struct drbd_tconn *tconn, struct drbd_socket *sock,
+                              enum drbd_packet cmd, unsigned int header_size,
+                              void *data, unsigned int size)
+{
+       return __send_command(tconn, 0, sock, cmd, header_size, data, size);
+}
+
+int conn_send_command(struct drbd_tconn *tconn, struct drbd_socket *sock,
+                     enum drbd_packet cmd, unsigned int header_size,
+                     void *data, unsigned int size)
+{
+       int err;
+
+       err = __conn_send_command(tconn, sock, cmd, header_size, data, size);
+       mutex_unlock(&sock->mutex);
+       return err;
+}
+
+int drbd_send_command(struct drbd_conf *mdev, struct drbd_socket *sock,
+                     enum drbd_packet cmd, unsigned int header_size,
+                     void *data, unsigned int size)
+{
+       int err;
+
+       err = __send_command(mdev->tconn, mdev->vnr, sock, cmd, header_size,
+                            data, size);
+       mutex_unlock(&sock->mutex);
+       return err;
+}
 
-       ok = (sizeof(h) ==
-               drbd_send(mdev, mdev->data.socket, &h, sizeof(h), 0));
-       ok = ok && (size ==
-               drbd_send(mdev, mdev->data.socket, data, size, 0));
+int drbd_send_ping(struct drbd_tconn *tconn)
+{
+       struct drbd_socket *sock;
+
+       sock = &tconn->meta;
+       if (!conn_prepare_command(tconn, sock))
+               return -EIO;
+       return conn_send_command(tconn, sock, P_PING, 0, NULL, 0);
+}
 
-       drbd_put_data_sock(mdev);
+int drbd_send_ping_ack(struct drbd_tconn *tconn)
+{
+       struct drbd_socket *sock;
 
-       return ok;
+       sock = &tconn->meta;
+       if (!conn_prepare_command(tconn, sock))
+               return -EIO;
+       return conn_send_command(tconn, sock, P_PING_ACK, 0, NULL, 0);
 }
 
-int drbd_send_sync_param(struct drbd_conf *mdev, struct syncer_conf *sc)
+int drbd_send_sync_param(struct drbd_conf *mdev)
 {
+       struct drbd_socket *sock;
        struct p_rs_param_95 *p;
-       struct socket *sock;
-       int size, rv;
-       const int apv = mdev->agreed_pro_version;
+       int size;
+       const int apv = mdev->tconn->agreed_pro_version;
+       enum drbd_packet cmd;
+       struct net_conf *nc;
+       struct disk_conf *dc;
+
+       sock = &mdev->tconn->data;
+       p = drbd_prepare_command(mdev, sock);
+       if (!p)
+               return -EIO;
+
+       rcu_read_lock();
+       nc = rcu_dereference(mdev->tconn->net_conf);
 
        size = apv <= 87 ? sizeof(struct p_rs_param)
                : apv == 88 ? sizeof(struct p_rs_param)
-                       + strlen(mdev->sync_conf.verify_alg) + 1
+                       + strlen(nc->verify_alg) + 1
                : apv <= 94 ? sizeof(struct p_rs_param_89)
                : /* apv >= 95 */ sizeof(struct p_rs_param_95);
 
-       /* used from admin command context and receiver/worker context.
-        * to avoid kmalloc, grab the socket right here,
-        * then use the pre-allocated sbuf there */
-       mutex_lock(&mdev->data.mutex);
-       sock = mdev->data.socket;
-
-       if (likely(sock != NULL)) {
-               enum drbd_packets cmd = apv >= 89 ? P_SYNC_PARAM89 : P_SYNC_PARAM;
-
-               p = &mdev->data.sbuf.rs_param_95;
-
-               /* initialize verify_alg and csums_alg */
-               memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX);
-
-               p->rate = cpu_to_be32(sc->rate);
-               p->c_plan_ahead = cpu_to_be32(sc->c_plan_ahead);
-               p->c_delay_target = cpu_to_be32(sc->c_delay_target);
-               p->c_fill_target = cpu_to_be32(sc->c_fill_target);
-               p->c_max_rate = cpu_to_be32(sc->c_max_rate);
+       cmd = apv >= 89 ? P_SYNC_PARAM89 : P_SYNC_PARAM;
 
-               if (apv >= 88)
-                       strcpy(p->verify_alg, mdev->sync_conf.verify_alg);
-               if (apv >= 89)
-                       strcpy(p->csums_alg, mdev->sync_conf.csums_alg);
+       /* initialize verify_alg and csums_alg */
+       memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX);
 
-               rv = _drbd_send_cmd(mdev, sock, cmd, &p->head, size, 0);
-       } else
-               rv = 0; /* not ok */
+       if (get_ldev(mdev)) {
+               dc = rcu_dereference(mdev->ldev->disk_conf);
+               p->resync_rate = cpu_to_be32(dc->resync_rate);
+               p->c_plan_ahead = cpu_to_be32(dc->c_plan_ahead);
+               p->c_delay_target = cpu_to_be32(dc->c_delay_target);
+               p->c_fill_target = cpu_to_be32(dc->c_fill_target);
+               p->c_max_rate = cpu_to_be32(dc->c_max_rate);
+               put_ldev(mdev);
+       } else {
+               p->resync_rate = cpu_to_be32(DRBD_RESYNC_RATE_DEF);
+               p->c_plan_ahead = cpu_to_be32(DRBD_C_PLAN_AHEAD_DEF);
+               p->c_delay_target = cpu_to_be32(DRBD_C_DELAY_TARGET_DEF);
+               p->c_fill_target = cpu_to_be32(DRBD_C_FILL_TARGET_DEF);
+               p->c_max_rate = cpu_to_be32(DRBD_C_MAX_RATE_DEF);
+       }
 
-       mutex_unlock(&mdev->data.mutex);
+       if (apv >= 88)
+               strcpy(p->verify_alg, nc->verify_alg);
+       if (apv >= 89)
+               strcpy(p->csums_alg, nc->csums_alg);
+       rcu_read_unlock();
 
-       return rv;
+       return drbd_send_command(mdev, sock, cmd, size, NULL, 0);
 }
 
-int drbd_send_protocol(struct drbd_conf *mdev)
+int __drbd_send_protocol(struct drbd_tconn *tconn, enum drbd_packet cmd)
 {
+       struct drbd_socket *sock;
        struct p_protocol *p;
-       int size, cf, rv;
+       struct net_conf *nc;
+       int size, cf;
 
-       size = sizeof(struct p_protocol);
+       sock = &tconn->data;
+       p = __conn_prepare_command(tconn, sock);
+       if (!p)
+               return -EIO;
 
-       if (mdev->agreed_pro_version >= 87)
-               size += strlen(mdev->net_conf->integrity_alg) + 1;
+       rcu_read_lock();
+       nc = rcu_dereference(tconn->net_conf);
 
-       /* we must not recurse into our own queue,
-        * as that is blocked during handshake */
-       p = kmalloc(size, GFP_NOIO);
-       if (p == NULL)
-               return 0;
+       if (nc->tentative && tconn->agreed_pro_version < 92) {
+               rcu_read_unlock();
+               mutex_unlock(&sock->mutex);
+               conn_err(tconn, "--dry-run is not supported by peer");
+               return -EOPNOTSUPP;
+       }
 
-       p->protocol      = cpu_to_be32(mdev->net_conf->wire_protocol);
-       p->after_sb_0p   = cpu_to_be32(mdev->net_conf->after_sb_0p);
-       p->after_sb_1p   = cpu_to_be32(mdev->net_conf->after_sb_1p);
-       p->after_sb_2p   = cpu_to_be32(mdev->net_conf->after_sb_2p);
-       p->two_primaries = cpu_to_be32(mdev->net_conf->two_primaries);
+       size = sizeof(*p);
+       if (tconn->agreed_pro_version >= 87)
+               size += strlen(nc->integrity_alg) + 1;
 
+       p->protocol      = cpu_to_be32(nc->wire_protocol);
+       p->after_sb_0p   = cpu_to_be32(nc->after_sb_0p);
+       p->after_sb_1p   = cpu_to_be32(nc->after_sb_1p);
+       p->after_sb_2p   = cpu_to_be32(nc->after_sb_2p);
+       p->two_primaries = cpu_to_be32(nc->two_primaries);
        cf = 0;
-       if (mdev->net_conf->want_lose)
-               cf |= CF_WANT_LOSE;
-       if (mdev->net_conf->dry_run) {
-               if (mdev->agreed_pro_version >= 92)
-                       cf |= CF_DRY_RUN;
-               else {
-                       dev_err(DEV, "--dry-run is not supported by peer");
-                       kfree(p);
-                       return -1;
-               }
-       }
+       if (nc->discard_my_data)
+               cf |= CF_DISCARD_MY_DATA;
+       if (nc->tentative)
+               cf |= CF_DRY_RUN;
        p->conn_flags    = cpu_to_be32(cf);
 
-       if (mdev->agreed_pro_version >= 87)
-               strcpy(p->integrity_alg, mdev->net_conf->integrity_alg);
+       if (tconn->agreed_pro_version >= 87)
+               strcpy(p->integrity_alg, nc->integrity_alg);
+       rcu_read_unlock();
 
-       rv = drbd_send_cmd(mdev, USE_DATA_SOCKET, P_PROTOCOL,
-                          (struct p_header80 *)p, size);
-       kfree(p);
-       return rv;
+       return __conn_send_command(tconn, sock, cmd, size, NULL, 0);
+}
+
+int drbd_send_protocol(struct drbd_tconn *tconn)
+{
+       int err;
+
+       mutex_lock(&tconn->data.mutex);
+       err = __drbd_send_protocol(tconn, P_PROTOCOL);
+       mutex_unlock(&tconn->data.mutex);
+
+       return err;
 }
 
 int _drbd_send_uuids(struct drbd_conf *mdev, u64 uuid_flags)
 {
-       struct p_uuids p;
+       struct drbd_socket *sock;
+       struct p_uuids *p;
        int i;
 
        if (!get_ldev_if_state(mdev, D_NEGOTIATING))
-               return 1;
+               return 0;
 
+       sock = &mdev->tconn->data;
+       p = drbd_prepare_command(mdev, sock);
+       if (!p) {
+               put_ldev(mdev);
+               return -EIO;
+       }
+       spin_lock_irq(&mdev->ldev->md.uuid_lock);
        for (i = UI_CURRENT; i < UI_SIZE; i++)
-               p.uuid[i] = mdev->ldev ? cpu_to_be64(mdev->ldev->md.uuid[i]) : 0;
+               p->uuid[i] = cpu_to_be64(mdev->ldev->md.uuid[i]);
+       spin_unlock_irq(&mdev->ldev->md.uuid_lock);
 
        mdev->comm_bm_set = drbd_bm_total_weight(mdev);
-       p.uuid[UI_SIZE] = cpu_to_be64(mdev->comm_bm_set);
-       uuid_flags |= mdev->net_conf->want_lose ? 1 : 0;
+       p->uuid[UI_SIZE] = cpu_to_be64(mdev->comm_bm_set);
+       rcu_read_lock();
+       uuid_flags |= rcu_dereference(mdev->tconn->net_conf)->discard_my_data ? 1 : 0;
+       rcu_read_unlock();
        uuid_flags |= test_bit(CRASHED_PRIMARY, &mdev->flags) ? 2 : 0;
        uuid_flags |= mdev->new_state_tmp.disk == D_INCONSISTENT ? 4 : 0;
-       p.uuid[UI_FLAGS] = cpu_to_be64(uuid_flags);
+       p->uuid[UI_FLAGS] = cpu_to_be64(uuid_flags);
 
        put_ldev(mdev);
-
-       return drbd_send_cmd(mdev, USE_DATA_SOCKET, P_UUIDS,
-                            (struct p_header80 *)&p, sizeof(p));
+       return drbd_send_command(mdev, sock, P_UUIDS, sizeof(*p), NULL, 0);
 }
 
 int drbd_send_uuids(struct drbd_conf *mdev)
@@ -2169,9 +884,10 @@ void drbd_print_uuids(struct drbd_conf *mdev, const char *text)
        }
 }
 
-int drbd_gen_and_send_sync_uuid(struct drbd_conf *mdev)
+void drbd_gen_and_send_sync_uuid(struct drbd_conf *mdev)
 {
-       struct p_rs_uuid p;
+       struct drbd_socket *sock;
+       struct p_rs_uuid *p;
        u64 uuid;
 
        D_ASSERT(mdev->state.disk == D_UP_TO_DATE);
@@ -2184,24 +900,29 @@ int drbd_gen_and_send_sync_uuid(struct drbd_conf *mdev)
        drbd_uuid_set(mdev, UI_BITMAP, uuid);
        drbd_print_uuids(mdev, "updated sync UUID");
        drbd_md_sync(mdev);
-       p.uuid = cpu_to_be64(uuid);
 
-       return drbd_send_cmd(mdev, USE_DATA_SOCKET, P_SYNC_UUID,
-                            (struct p_header80 *)&p, sizeof(p));
+       sock = &mdev->tconn->data;
+       p = drbd_prepare_command(mdev, sock);
+       if (p) {
+               p->uuid = cpu_to_be64(uuid);
+               drbd_send_command(mdev, sock, P_SYNC_UUID, sizeof(*p), NULL, 0);
+       }
 }
 
 int drbd_send_sizes(struct drbd_conf *mdev, int trigger_reply, enum dds_flags flags)
 {
-       struct p_sizes p;
+       struct drbd_socket *sock;
+       struct p_sizes *p;
        sector_t d_size, u_size;
        int q_order_type;
        unsigned int max_bio_size;
-       int ok;
 
        if (get_ldev_if_state(mdev, D_NEGOTIATING)) {
                D_ASSERT(mdev->ldev->backing_bdev);
                d_size = drbd_get_max_capacity(mdev->ldev);
-               u_size = mdev->ldev->dc.disk_size;
+               rcu_read_lock();
+               u_size = rcu_dereference(mdev->ldev->disk_conf)->disk_size;
+               rcu_read_unlock();
                q_order_type = drbd_queue_order_type(mdev);
                max_bio_size = queue_max_hw_sectors(mdev->ldev->backing_bdev->bd_disk->queue) << 9;
                max_bio_size = min(max_bio_size, DRBD_MAX_BIO_SIZE);
@@ -2213,20 +934,23 @@ int drbd_send_sizes(struct drbd_conf *mdev, int trigger_reply, enum dds_flags fl
                max_bio_size = DRBD_MAX_BIO_SIZE; /* ... multiple BIOs per peer_request */
        }
 
-       /* Never allow old drbd (up to 8.3.7) to see more than 32KiB */
-       if (mdev->agreed_pro_version <= 94)
-               max_bio_size = min(max_bio_size, DRBD_MAX_SIZE_H80_PACKET);
+       sock = &mdev->tconn->data;
+       p = drbd_prepare_command(mdev, sock);
+       if (!p)
+               return -EIO;
 
-       p.d_size = cpu_to_be64(d_size);
-       p.u_size = cpu_to_be64(u_size);
-       p.c_size = cpu_to_be64(trigger_reply ? 0 : drbd_get_capacity(mdev->this_bdev));
-       p.max_bio_size = cpu_to_be32(max_bio_size);
-       p.queue_order_type = cpu_to_be16(q_order_type);
-       p.dds_flags = cpu_to_be16(flags);
+       if (mdev->tconn->agreed_pro_version <= 94)
+               max_bio_size = min(max_bio_size, DRBD_MAX_SIZE_H80_PACKET);
+       else if (mdev->tconn->agreed_pro_version < 100)
+               max_bio_size = min(max_bio_size, DRBD_MAX_BIO_SIZE_P95);
 
-       ok = drbd_send_cmd(mdev, USE_DATA_SOCKET, P_SIZES,
-                          (struct p_header80 *)&p, sizeof(p));
-       return ok;
+       p->d_size = cpu_to_be64(d_size);
+       p->u_size = cpu_to_be64(u_size);
+       p->c_size = cpu_to_be64(trigger_reply ? 0 : drbd_get_capacity(mdev->this_bdev));
+       p->max_bio_size = cpu_to_be32(max_bio_size);
+       p->queue_order_type = cpu_to_be16(q_order_type);
+       p->dds_flags = cpu_to_be16(flags);
+       return drbd_send_command(mdev, sock, P_SIZES, sizeof(*p), NULL, 0);
 }
 
 /**
@@ -2235,34 +959,21 @@ int drbd_send_sizes(struct drbd_conf *mdev, int trigger_reply, enum dds_flags fl
  */
 int drbd_send_current_state(struct drbd_conf *mdev)
 {
-       struct socket *sock;
-       struct p_state p;
-       int ok = 0;
-
-       /* Grab state lock so we wont send state if we're in the middle
-        * of a cluster wide state change on another thread */
-       drbd_state_lock(mdev);
-
-       mutex_lock(&mdev->data.mutex);
-
-       p.state = cpu_to_be32(mdev->state.i); /* Within the send mutex */
-       sock = mdev->data.socket;
+       struct drbd_socket *sock;
+       struct p_state *p;
 
-       if (likely(sock != NULL)) {
-               ok = _drbd_send_cmd(mdev, sock, P_STATE,
-                                   (struct p_header80 *)&p, sizeof(p), 0);
-       }
-
-       mutex_unlock(&mdev->data.mutex);
-
-       drbd_state_unlock(mdev);
-       return ok;
+       sock = &mdev->tconn->data;
+       p = drbd_prepare_command(mdev, sock);
+       if (!p)
+               return -EIO;
+       p->state = cpu_to_be32(mdev->state.i); /* Within the send mutex */
+       return drbd_send_command(mdev, sock, P_STATE, sizeof(*p), NULL, 0);
 }
 
 /**
  * drbd_send_state() - After a state change, sends the new state to the peer
- * @mdev:      DRBD device.
- * @state:     the state to send, not necessarily the current state.
+ * @mdev:      DRBD device.
+ * @state:     the state to send, not necessarily the current state.
  *
  * Each state change queues an "after_state_ch" work, which will eventually
  * send the resulting new state to the peer. If more state changes happen
@@ -2271,50 +982,95 @@ int drbd_send_current_state(struct drbd_conf *mdev)
  */
 int drbd_send_state(struct drbd_conf *mdev, union drbd_state state)
 {
-       struct socket *sock;
-       struct p_state p;
-       int ok = 0;
+       struct drbd_socket *sock;
+       struct p_state *p;
 
-       mutex_lock(&mdev->data.mutex);
+       sock = &mdev->tconn->data;
+       p = drbd_prepare_command(mdev, sock);
+       if (!p)
+               return -EIO;
+       p->state = cpu_to_be32(state.i); /* Within the send mutex */
+       return drbd_send_command(mdev, sock, P_STATE, sizeof(*p), NULL, 0);
+}
 
-       p.state = cpu_to_be32(state.i);
-       sock = mdev->data.socket;
+int drbd_send_state_req(struct drbd_conf *mdev, union drbd_state mask, union drbd_state val)
+{
+       struct drbd_socket *sock;
+       struct p_req_state *p;
 
-       if (likely(sock != NULL)) {
-               ok = _drbd_send_cmd(mdev, sock, P_STATE,
-                                   (struct p_header80 *)&p, sizeof(p), 0);
-       }
+       sock = &mdev->tconn->data;
+       p = drbd_prepare_command(mdev, sock);
+       if (!p)
+               return -EIO;
+       p->mask = cpu_to_be32(mask.i);
+       p->val = cpu_to_be32(val.i);
+       return drbd_send_command(mdev, sock, P_STATE_CHG_REQ, sizeof(*p), NULL, 0);
+}
 
-       mutex_unlock(&mdev->data.mutex);
+int conn_send_state_req(struct drbd_tconn *tconn, union drbd_state mask, union drbd_state val)
+{
+       enum drbd_packet cmd;
+       struct drbd_socket *sock;
+       struct p_req_state *p;
 
-       return ok;
+       cmd = tconn->agreed_pro_version < 100 ? P_STATE_CHG_REQ : P_CONN_ST_CHG_REQ;
+       sock = &tconn->data;
+       p = conn_prepare_command(tconn, sock);
+       if (!p)
+               return -EIO;
+       p->mask = cpu_to_be32(mask.i);
+       p->val = cpu_to_be32(val.i);
+       return conn_send_command(tconn, sock, cmd, sizeof(*p), NULL, 0);
 }
 
-int drbd_send_state_req(struct drbd_conf *mdev,
-       union drbd_state mask, union drbd_state val)
+void drbd_send_sr_reply(struct drbd_conf *mdev, enum drbd_state_rv retcode)
 {
-       struct p_req_state p;
+       struct drbd_socket *sock;
+       struct p_req_state_reply *p;
+
+       sock = &mdev->tconn->meta;
+       p = drbd_prepare_command(mdev, sock);
+       if (p) {
+               p->retcode = cpu_to_be32(retcode);
+               drbd_send_command(mdev, sock, P_STATE_CHG_REPLY, sizeof(*p), NULL, 0);
+       }
+}
 
-       p.mask    = cpu_to_be32(mask.i);
-       p.val     = cpu_to_be32(val.i);
+void conn_send_sr_reply(struct drbd_tconn *tconn, enum drbd_state_rv retcode)
+{
+       struct drbd_socket *sock;
+       struct p_req_state_reply *p;
+       enum drbd_packet cmd = tconn->agreed_pro_version < 100 ? P_STATE_CHG_REPLY : P_CONN_ST_CHG_REPLY;
 
-       return drbd_send_cmd(mdev, USE_DATA_SOCKET, P_STATE_CHG_REQ,
-                            (struct p_header80 *)&p, sizeof(p));
+       sock = &tconn->meta;
+       p = conn_prepare_command(tconn, sock);
+       if (p) {
+               p->retcode = cpu_to_be32(retcode);
+               conn_send_command(tconn, sock, cmd, sizeof(*p), NULL, 0);
+       }
 }
 
-int drbd_send_sr_reply(struct drbd_conf *mdev, enum drbd_state_rv retcode)
+static void dcbp_set_code(struct p_compressed_bm *p, enum drbd_bitmap_code code)
 {
-       struct p_req_state_reply p;
+       BUG_ON(code & ~0xf);
+       p->encoding = (p->encoding & ~0xf) | code;
+}
 
-       p.retcode    = cpu_to_be32(retcode);
+static void dcbp_set_start(struct p_compressed_bm *p, int set)
+{
+       p->encoding = (p->encoding & ~0x80) | (set ? 0x80 : 0);
+}
 
-       return drbd_send_cmd(mdev, USE_META_SOCKET, P_STATE_CHG_REPLY,
-                            (struct p_header80 *)&p, sizeof(p));
+static void dcbp_set_pad_bits(struct p_compressed_bm *p, int n)
+{
+       BUG_ON(n & ~0x7);
+       p->encoding = (p->encoding & (~0x7 << 4)) | (n << 4);
 }
 
 int fill_bitmap_rle_bits(struct drbd_conf *mdev,
-       struct p_compressed_bm *p,
-       struct bm_xfer_ctx *c)
+                        struct p_compressed_bm *p,
+                        unsigned int size,
+                        struct bm_xfer_ctx *c)
 {
        struct bitstream bs;
        unsigned long plain_bits;
@@ -2322,19 +1078,21 @@ int fill_bitmap_rle_bits(struct drbd_conf *mdev,
        unsigned long rl;
        unsigned len;
        unsigned toggle;
-       int bits;
+       int bits, use_rle;
 
        /* may we use this feature? */
-       if ((mdev->sync_conf.use_rle == 0) ||
-               (mdev->agreed_pro_version < 90))
-                       return 0;
+       rcu_read_lock();
+       use_rle = rcu_dereference(mdev->tconn->net_conf)->use_rle;
+       rcu_read_unlock();
+       if (!use_rle || mdev->tconn->agreed_pro_version < 90)
+               return 0;
 
        if (c->bit_offset >= c->bm_bits)
                return 0; /* nothing to do. */
 
        /* use at most thus many bytes */
-       bitstream_init(&bs, p->code, BM_PACKET_VLI_BYTES_MAX, 0);
-       memset(p->code, 0, BM_PACKET_VLI_BYTES_MAX);
+       bitstream_init(&bs, p->code, size, 0);
+       memset(p->code, 0, size);
        /* plain bits covered in this code string */
        plain_bits = 0;
 
@@ -2356,12 +1114,12 @@ int fill_bitmap_rle_bits(struct drbd_conf *mdev,
                        if (rl == 0) {
                                /* the first checked bit was set,
                                 * store start value, */
-                               DCBP_set_start(p, 1);
+                               dcbp_set_start(p, 1);
                                /* but skip encoding of zero run length */
                                toggle = !toggle;
                                continue;
                        }
-                       DCBP_set_start(p, 0);
+                       dcbp_set_start(p, 0);
                }
 
                /* paranoia: catch zero runlength.
@@ -2401,7 +1159,7 @@ int fill_bitmap_rle_bits(struct drbd_conf *mdev,
        bm_xfer_ctx_bit_to_word_offset(c);
 
        /* store pad_bits */
-       DCBP_set_pad_bits(p, (8 - bs.cur.bit) & 0x7);
+       dcbp_set_pad_bits(p, (8 - bs.cur.bit) & 0x7);
 
        return len;
 }
@@ -2413,48 +1171,52 @@ int fill_bitmap_rle_bits(struct drbd_conf *mdev,
  * code upon failure.
  */
 static int
-send_bitmap_rle_or_plain(struct drbd_conf *mdev,
-                        struct p_header80 *h, struct bm_xfer_ctx *c)
+send_bitmap_rle_or_plain(struct drbd_conf *mdev, struct bm_xfer_ctx *c)
 {
-       struct p_compressed_bm *p = (void*)h;
-       unsigned long num_words;
-       int len;
-       int ok;
-
-       len = fill_bitmap_rle_bits(mdev, p, c);
+       struct drbd_socket *sock = &mdev->tconn->data;
+       unsigned int header_size = drbd_header_size(mdev->tconn);
+       struct p_compressed_bm *p = sock->sbuf + header_size;
+       int len, err;
 
+       len = fill_bitmap_rle_bits(mdev, p,
+                       DRBD_SOCKET_BUFFER_SIZE - header_size - sizeof(*p), c);
        if (len < 0)
                return -EIO;
 
        if (len) {
-               DCBP_set_code(p, RLE_VLI_Bits);
-               ok = _drbd_send_cmd(mdev, mdev->data.socket, P_COMPRESSED_BITMAP, h,
-                       sizeof(*p) + len, 0);
-
+               dcbp_set_code(p, RLE_VLI_Bits);
+               err = __send_command(mdev->tconn, mdev->vnr, sock,
+                                    P_COMPRESSED_BITMAP, sizeof(*p) + len,
+                                    NULL, 0);
                c->packets[0]++;
-               c->bytes[0] += sizeof(*p) + len;
+               c->bytes[0] += header_size + sizeof(*p) + len;
 
                if (c->bit_offset >= c->bm_bits)
                        len = 0; /* DONE */
        } else {
                /* was not compressible.
                 * send a buffer full of plain text bits instead. */
-               num_words = min_t(size_t, BM_PACKET_WORDS, c->bm_words - c->word_offset);
-               len = num_words * sizeof(long);
+               unsigned int data_size;
+               unsigned long num_words;
+               unsigned long *p = sock->sbuf + header_size;
+
+               data_size = DRBD_SOCKET_BUFFER_SIZE - header_size;
+               num_words = min_t(size_t, data_size / sizeof(*p),
+                                 c->bm_words - c->word_offset);
+               len = num_words * sizeof(*p);
                if (len)
-                       drbd_bm_get_lel(mdev, c->word_offset, num_words, (unsigned long*)h->payload);
-               ok = _drbd_send_cmd(mdev, mdev->data.socket, P_BITMAP,
-                                  h, sizeof(struct p_header80) + len, 0);
+                       drbd_bm_get_lel(mdev, c->word_offset, num_words, p);
+               err = __send_command(mdev->tconn, mdev->vnr, sock, P_BITMAP, len, NULL, 0);
                c->word_offset += num_words;
                c->bit_offset = c->word_offset * BITS_PER_LONG;
 
                c->packets[1]++;
-               c->bytes[1] += sizeof(struct p_header80) + len;
+               c->bytes[1] += header_size + len;
 
                if (c->bit_offset > c->bm_bits)
                        c->bit_offset = c->bm_bits;
        }
-       if (ok) {
+       if (!err) {
                if (len == 0) {
                        INFO_bm_xfer_stats(mdev, "send", c);
                        return 0;
@@ -2465,21 +1227,13 @@ send_bitmap_rle_or_plain(struct drbd_conf *mdev,
 }
 
 /* See the comment at receive_bitmap() */
-int _drbd_send_bitmap(struct drbd_conf *mdev)
+static int _drbd_send_bitmap(struct drbd_conf *mdev)
 {
        struct bm_xfer_ctx c;
-       struct p_header80 *p;
        int err;
 
-       ERR_IF(!mdev->bitmap) return false;
-
-       /* maybe we should use some per thread scratch page,
-        * and allocate that during initial device creation? */
-       p = (struct p_header80 *) __get_free_page(GFP_NOIO);
-       if (!p) {
-               dev_err(DEV, "failed to allocate one page buffer in %s\n", __func__);
+       if (!expect(mdev->bitmap))
                return false;
-       }
 
        if (get_ldev(mdev)) {
                if (drbd_md_test_flag(mdev->ldev, MDF_FULL_SYNC)) {
@@