]> git.proxmox.com Git - mirror_zfs.git/blobdiff - module/zfs/vdev_disk.c
Fix send/recv lost spill block
[mirror_zfs.git] / module / zfs / vdev_disk.c
index 1d8bf3f8cc518d24fc346f766596d40e96145e1c..1419ae6ad54aaab3918d23f8774c364e56e1f8d5 100644 (file)
  * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
  * Rewritten for Linux by Brian Behlendorf <behlendorf1@llnl.gov>.
  * LLNL-CODE-403049.
- * Copyright (c) 2013 by Delphix. All rights reserved.
+ * Copyright (c) 2012, 2019 by Delphix. All rights reserved.
  */
 
 #include <sys/zfs_context.h>
-#include <sys/spa.h>
+#include <sys/spa_impl.h>
 #include <sys/vdev_disk.h>
 #include <sys/vdev_impl.h>
+#include <sys/vdev_trim.h>
+#include <sys/abd.h>
 #include <sys/fs/zfs.h>
 #include <sys/zio.h>
-#include <sys/sunldi.h>
+#include <linux/mod_compat.h>
+#include <linux/msdos_fs.h>
+#include <linux/vfs_compat.h>
 
 char *zfs_vdev_scheduler = VDEV_SCHEDULER;
 static void *zfs_vdev_holder = VDEV_HOLDER;
 
+/* size of the "reserved" partition, in blocks */
+#define        EFI_MIN_RESV_SIZE       (16 * 1024)
+
 /*
  * Virtual device vector for disks.
  */
 typedef struct dio_request {
-       struct completion       dr_comp;        /* Completion for sync IO */
-       atomic_t                dr_ref;         /* References */
        zio_t                   *dr_zio;        /* Parent ZIO */
-       int                     dr_rw;          /* Read/Write */
+       atomic_t                dr_ref;         /* References */
        int                     dr_error;       /* Bio error */
        int                     dr_bio_count;   /* Count of bio's */
        struct bio              *dr_bio[0];     /* Attached bio's */
 } dio_request_t;
 
 
-#ifdef HAVE_OPEN_BDEV_EXCLUSIVE
+#if defined(HAVE_OPEN_BDEV_EXCLUSIVE) || defined(HAVE_BLKDEV_GET_BY_PATH)
 static fmode_t
 vdev_bdev_mode(int smode)
 {
@@ -76,34 +81,83 @@ vdev_bdev_mode(int smode)
        ASSERT3S(smode & (FREAD | FWRITE), !=, 0);
 
        if ((smode & FREAD) && !(smode & FWRITE))
-               mode = MS_RDONLY;
+               mode = SB_RDONLY;
 
        return (mode);
 }
 #endif /* HAVE_OPEN_BDEV_EXCLUSIVE */
 
+/*
+ * Returns the usable capacity (in bytes) for the partition or disk.
+ */
 static uint64_t
 bdev_capacity(struct block_device *bdev)
 {
-       struct hd_struct *part = bdev->bd_part;
+       return (i_size_read(bdev->bd_inode));
+}
+
+/*
+ * Returns the maximum expansion capacity of the block device (in bytes).
+ *
+ * It is possible to expand a vdev when it has been created as a wholedisk
+ * and the containing block device has increased in capacity.  Or when the
+ * partition containing the pool has been manually increased in size.
+ *
+ * This function is only responsible for calculating the potential expansion
+ * size so it can be reported by 'zpool list'.  The efi_use_whole_disk() is
+ * responsible for verifying the expected partition layout in the wholedisk
+ * case, and updating the partition table if appropriate.  Once the partition
+ * size has been increased the additional capacity will be visible using
+ * bdev_capacity().
+ *
+ * The returned maximum expansion capacity is always expected to be larger, or
+ * at the very least equal, to its usable capacity to prevent overestimating
+ * the pool expandsize.
+ */
+static uint64_t
+bdev_max_capacity(struct block_device *bdev, uint64_t wholedisk)
+{
+       uint64_t psize;
+       int64_t available;
 
-       /* The partition capacity referenced by the block device */
-       if (part)
-               return (part->nr_sects << 9);
+       if (wholedisk && bdev->bd_part != NULL && bdev != bdev->bd_contains) {
+               /*
+                * When reporting maximum expansion capacity for a wholedisk
+                * deduct any capacity which is expected to be lost due to
+                * alignment restrictions.  Over reporting this value isn't
+                * harmful and would only result in slightly less capacity
+                * than expected post expansion.
+                * The estimated available space may be slightly smaller than
+                * bdev_capacity() for devices where the number of sectors is
+                * not a multiple of the alignment size and the partition layout
+                * is keeping less than PARTITION_END_ALIGNMENT bytes after the
+                * "reserved" EFI partition: in such cases return the device
+                * usable capacity.
+                */
+               available = i_size_read(bdev->bd_contains->bd_inode) -
+                   ((EFI_MIN_RESV_SIZE + NEW_START_BLOCK +
+                   PARTITION_END_ALIGNMENT) << SECTOR_BITS);
+               psize = MAX(available, bdev_capacity(bdev));
+       } else {
+               psize = bdev_capacity(bdev);
+       }
 
-       /* Otherwise assume the full device capacity */
-       return (get_capacity(bdev->bd_disk) << 9);
+       return (psize);
 }
 
 static void
 vdev_disk_error(zio_t *zio)
 {
-#ifdef ZFS_DEBUG
-       printk("ZFS: zio error=%d type=%d offset=%llu size=%llu "
-           "flags=%x delay=%llu\n", zio->io_error, zio->io_type,
+       /*
+        * This function can be called in interrupt context, for instance while
+        * handling IRQs coming from a misbehaving disk device; use printk()
+        * which is safe from any context.
+        */
+       printk(KERN_WARNING "zio pool=%s vdev=%s error=%d type=%d "
+           "offset=%llu size=%llu flags=%x\n", spa_name(zio->io_spa),
+           zio->io_vd->vdev_path, zio->io_error, zio->io_type,
            (u_longlong_t)zio->io_offset, (u_longlong_t)zio->io_size,
-           zio->io_flags, (u_longlong_t)zio->io_delay);
-#endif
+           zio->io_flags);
 }
 
 /*
@@ -114,15 +168,23 @@ vdev_disk_error(zio_t *zio)
  * physical device.  This yields the largest possible requests for
  * the device with the lowest total overhead.
  */
-static int
+static void
 vdev_elevator_switch(vdev_t *v, char *elevator)
 {
        vdev_disk_t *vd = v->vdev_tsd;
-       struct block_device *bdev = vd->vd_bdev;
-       struct request_queue *q = bdev_get_queue(bdev);
-       char *device = bdev->bd_disk->disk_name;
+       struct request_queue *q;
+       char *device;
        int error;
 
+       for (int c = 0; c < v->vdev_children; c++)
+               vdev_elevator_switch(v->vdev_child[c], elevator);
+
+       if (!v->vdev_ops->vdev_op_leaf || vd->vd_bdev == NULL)
+               return;
+
+       q = bdev_get_queue(vd->vd_bdev);
+       device = vd->vd_bdev->bd_disk->disk_name;
+
        /*
         * Skip devices which are not whole disks (partitions).
         * Device-mapper devices are excepted since they may be whole
@@ -132,183 +194,171 @@ vdev_elevator_switch(vdev_t *v, char *elevator)
         * "Skip devices without schedulers" check below will fail.
         */
        if (!v->vdev_wholedisk && strncmp(device, "dm-", 3) != 0)
-               return (0);
-
-       /* Skip devices without schedulers (loop, ram, dm, etc) */
-       if (!q->elevator || !blk_queue_stackable(q))
-               return (0);
+               return;
 
        /* Leave existing scheduler when set to "none" */
-       if (strncmp(elevator, "none", 4) && (strlen(elevator) == 4) == 0)
-               return (0);
+       if ((strncmp(elevator, "none", 4) == 0) && (strlen(elevator) == 4))
+               return;
 
+       /*
+        * The elevator_change() function was available in kernels from
+        * 2.6.36 to 4.11.  When not available fall back to using the user
+        * mode helper functionality to set the elevator via sysfs.  This
+        * requires /bin/echo and sysfs to be mounted which may not be true
+        * early in the boot process.
+        */
 #ifdef HAVE_ELEVATOR_CHANGE
        error = elevator_change(q, elevator);
 #else
-       /*
-        * For pre-2.6.36 kernels elevator_change() is not available.
-        * Therefore we fall back to using a usermodehelper to echo the
-        * elevator into sysfs;  This requires /bin/echo and sysfs to be
-        * mounted which may not be true early in the boot process.
-        */
 #define        SET_SCHEDULER_CMD \
        "exec 0</dev/null " \
        "     1>/sys/block/%s/queue/scheduler " \
        "     2>/dev/null; " \
        "echo %s"
 
-       {
-               char *argv[] = { "/bin/sh", "-c", NULL, NULL };
-               char *envp[] = { NULL };
+       char *argv[] = { "/bin/sh", "-c", NULL, NULL };
+       char *envp[] = { NULL };
 
-               argv[2] = kmem_asprintf(SET_SCHEDULER_CMD, device, elevator);
-               error = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
-               strfree(argv[2]);
-       }
+       argv[2] = kmem_asprintf(SET_SCHEDULER_CMD, device, elevator);
+       error = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
+       strfree(argv[2]);
 #endif /* HAVE_ELEVATOR_CHANGE */
-       if (error)
-               printk("ZFS: Unable to set \"%s\" scheduler for %s (%s): %d\n",
+       if (error) {
+               zfs_dbgmsg("Unable to set \"%s\" scheduler for %s (%s): %d",
                    elevator, v->vdev_path, device, error);
-
-       return (error);
-}
-
-/*
- * Expanding a whole disk vdev involves invoking BLKRRPART on the
- * whole disk device. This poses a problem, because BLKRRPART will
- * return EBUSY if one of the disk's partitions is open. That's why
- * we have to do it here, just before opening the data partition.
- * Unfortunately, BLKRRPART works by dropping all partitions and
- * recreating them, which means that for a short time window, all
- * /dev/sdxN device files disappear (until udev recreates them).
- * This means two things:
- *  - When we open the data partition just after a BLKRRPART, we
- *    can't do it using the normal device file path because of the
- *    obvious race condition with udev. Instead, we use reliable
- *    kernel APIs to get a handle to the new partition device from
- *    the whole disk device.
- *  - Because vdev_disk_open() initially needs to find the device
- *    using its path, multiple vdev_disk_open() invocations in
- *    short succession on the same disk with BLKRRPARTs in the
- *    middle have a high probability of failure (because of the
- *    race condition with udev). A typical situation where this
- *    might happen is when the zpool userspace tool does a
- *    TRYIMPORT immediately followed by an IMPORT. For this
- *    reason, we only invoke BLKRRPART in the module when strictly
- *    necessary (zpool online -e case), and rely on userspace to
- *    do it when possible.
- */
-static struct block_device *
-vdev_disk_rrpart(const char *path, int mode, vdev_disk_t *vd)
-{
-#if defined(HAVE_3ARG_BLKDEV_GET) && defined(HAVE_GET_GENDISK)
-       struct block_device *bdev, *result = ERR_PTR(-ENXIO);
-       struct gendisk *disk;
-       int error, partno;
-
-       bdev = vdev_bdev_open(path, vdev_bdev_mode(mode), zfs_vdev_holder);
-       if (IS_ERR(bdev))
-               return (bdev);
-
-       disk = get_gendisk(bdev->bd_dev, &partno);
-       vdev_bdev_close(bdev, vdev_bdev_mode(mode));
-
-       if (disk) {
-               bdev = bdget(disk_devt(disk));
-               if (bdev) {
-                       error = blkdev_get(bdev, vdev_bdev_mode(mode), vd);
-                       if (error == 0)
-                               error = ioctl_by_bdev(bdev, BLKRRPART, 0);
-                       vdev_bdev_close(bdev, vdev_bdev_mode(mode));
-               }
-
-               bdev = bdget_disk(disk, partno);
-               if (bdev) {
-                       error = blkdev_get(bdev,
-                           vdev_bdev_mode(mode) | FMODE_EXCL, vd);
-                       if (error == 0)
-                               result = bdev;
-               }
-               put_disk(disk);
        }
-
-       return (result);
-#else
-       return (ERR_PTR(-EOPNOTSUPP));
-#endif /* defined(HAVE_3ARG_BLKDEV_GET) && defined(HAVE_GET_GENDISK) */
 }
 
 static int
 vdev_disk_open(vdev_t *v, uint64_t *psize, uint64_t *max_psize,
     uint64_t *ashift)
 {
-       struct block_device *bdev = ERR_PTR(-ENXIO);
+       struct block_device *bdev;
+       fmode_t mode = vdev_bdev_mode(spa_mode(v->vdev_spa));
+       int count = 0, block_size;
+       int bdev_retry_count = 50;
        vdev_disk_t *vd;
-       int mode, block_size;
 
        /* Must have a pathname and it must be absolute. */
        if (v->vdev_path == NULL || v->vdev_path[0] != '/') {
                v->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
-               return (EINVAL);
+               vdev_dbgmsg(v, "invalid vdev_path");
+               return (SET_ERROR(EINVAL));
        }
 
        /*
-        * Reopen the device if it's not currently open. Otherwise,
-        * just update the physical size of the device.
+        * Reopen the device if it is currently open.  When expanding a
+        * partition force re-scanning the partition table while closed
+        * in order to get an accurate updated block device size.  Then
+        * since udev may need to recreate the device links increase the
+        * open retry count before reporting the device as unavailable.
         */
-       if (v->vdev_tsd != NULL) {
-               ASSERT(v->vdev_reopening);
-               vd = v->vdev_tsd;
-               goto skip_open;
-       }
+       vd = v->vdev_tsd;
+       if (vd) {
+               char disk_name[BDEVNAME_SIZE + 6] = "/dev/";
+               boolean_t reread_part = B_FALSE;
+
+               rw_enter(&vd->vd_lock, RW_WRITER);
+               bdev = vd->vd_bdev;
+               vd->vd_bdev = NULL;
+
+               if (bdev) {
+                       if (v->vdev_expanding && bdev != bdev->bd_contains) {
+                               bdevname(bdev->bd_contains, disk_name + 5);
+                               reread_part = B_TRUE;
+                       }
 
-       vd = kmem_zalloc(sizeof (vdev_disk_t), KM_PUSHPAGE);
-       if (vd == NULL)
-               return (ENOMEM);
+                       vdev_bdev_close(bdev, mode);
+               }
+
+               if (reread_part) {
+                       bdev = vdev_bdev_open(disk_name, mode, zfs_vdev_holder);
+                       if (!IS_ERR(bdev)) {
+                               int error = vdev_bdev_reread_part(bdev);
+                               vdev_bdev_close(bdev, mode);
+                               if (error == 0)
+                                       bdev_retry_count = 100;
+                       }
+               }
+       } else {
+               vd = kmem_zalloc(sizeof (vdev_disk_t), KM_SLEEP);
+
+               rw_init(&vd->vd_lock, NULL, RW_DEFAULT, NULL);
+               rw_enter(&vd->vd_lock, RW_WRITER);
+       }
 
        /*
         * Devices are always opened by the path provided at configuration
         * time.  This means that if the provided path is a udev by-id path
-        * then drives may be recabled without an issue.  If the provided
+        * then drives may be re-cabled without an issue.  If the provided
         * path is a udev by-path path, then the physical location information
         * will be preserved.  This can be critical for more complicated
         * configurations where drives are located in specific physical
-        * locations to maximize the systems tolerence to component failure.
+        * locations to maximize the systems tolerance to component failure.
+        *
         * Alternatively, you can provide your own udev rule to flexibly map
         * the drives as you see fit.  It is not advised that you use the
         * /dev/[hd]d devices which may be reordered due to probing order.
         * Devices in the wrong locations will be detected by the higher
         * level vdev validation.
+        *
+        * The specified paths may be briefly removed and recreated in
+        * response to udev events.  This should be exceptionally unlikely
+        * because the zpool command makes every effort to verify these paths
+        * have already settled prior to reaching this point.  Therefore,
+        * a ENOENT failure at this point is highly likely to be transient
+        * and it is reasonable to sleep and retry before giving up.  In
+        * practice delays have been observed to be on the order of 100ms.
         */
-       mode = spa_mode(v->vdev_spa);
-       if (v->vdev_wholedisk && v->vdev_expanding)
-               bdev = vdev_disk_rrpart(v->vdev_path, mode, vd);
-       if (IS_ERR(bdev))
-               bdev = vdev_bdev_open(v->vdev_path,
-                   vdev_bdev_mode(mode), zfs_vdev_holder);
+       bdev = ERR_PTR(-ENXIO);
+       while (IS_ERR(bdev) && count < bdev_retry_count) {
+               bdev = vdev_bdev_open(v->vdev_path, mode, zfs_vdev_holder);
+               if (unlikely(PTR_ERR(bdev) == -ENOENT)) {
+                       schedule_timeout(MSEC_TO_TICK(10));
+                       count++;
+               } else if (IS_ERR(bdev)) {
+                       break;
+               }
+       }
+
        if (IS_ERR(bdev)) {
-               kmem_free(vd, sizeof (vdev_disk_t));
-               return (-PTR_ERR(bdev));
+               int error = -PTR_ERR(bdev);
+               vdev_dbgmsg(v, "open error=%d count=%d", error, count);
+               vd->vd_bdev = NULL;
+               v->vdev_tsd = vd;
+               rw_exit(&vd->vd_lock);
+               return (SET_ERROR(error));
+       } else {
+               vd->vd_bdev = bdev;
+               v->vdev_tsd = vd;
+               rw_exit(&vd->vd_lock);
        }
 
-       v->vdev_tsd = vd;
-       vd->vd_bdev = bdev;
+       struct request_queue *q = bdev_get_queue(vd->vd_bdev);
 
-skip_open:
        /*  Determine the physical block size */
        block_size = vdev_bdev_block_size(vd->vd_bdev);
 
        /* Clear the nowritecache bit, causes vdev_reopen() to try again. */
        v->vdev_nowritecache = B_FALSE;
 
-       /* Physical volume size in bytes */
+       /* Set when device reports it supports TRIM. */
+       v->vdev_has_trim = !!blk_queue_discard(q);
+
+       /* Set when device reports it supports secure TRIM. */
+       v->vdev_has_securetrim = !!blk_queue_discard_secure(q);
+
+       /* Inform the ZIO pipeline that we are non-rotational */
+       v->vdev_nonrot = blk_queue_nonrot(q);
+
+       /* Physical volume size in bytes for the partition */
        *psize = bdev_capacity(vd->vd_bdev);
 
-       /* TODO: report possible expansion size */
-       *max_psize = *psize;
+       /* Physical volume size in bytes including possible expansion space */
+       *max_psize = bdev_max_capacity(vd->vd_bdev, v->vdev_wholedisk);
 
        /* Based on the minimum sector size set the block size */
-       *ashift = highbit(MAX(block_size, SPA_MINBLOCKSIZE)) - 1;
+       *ashift = highbit64(MAX(block_size, SPA_MINBLOCKSIZE)) - 1;
 
        /* Try to set the io scheduler elevator algorithm */
        (void) vdev_elevator_switch(v, zfs_vdev_scheduler);
@@ -324,10 +374,12 @@ vdev_disk_close(vdev_t *v)
        if (v->vdev_reopening || vd == NULL)
                return;
 
-       if (vd->vd_bdev != NULL)
+       if (vd->vd_bdev != NULL) {
                vdev_bdev_close(vd->vd_bdev,
                    vdev_bdev_mode(spa_mode(v->vdev_spa)));
+       }
 
+       rw_destroy(&vd->vd_lock);
        kmem_free(vd, sizeof (vdev_disk_t));
        v->vdev_tsd = NULL;
 }
@@ -339,9 +391,8 @@ vdev_disk_dio_alloc(int bio_count)
        int i;
 
        dr = kmem_zalloc(sizeof (dio_request_t) +
-           sizeof (struct bio *) * bio_count, KM_PUSHPAGE);
+           sizeof (struct bio *) * bio_count, KM_SLEEP);
        if (dr) {
-               init_completion(&dr->dr_comp);
                atomic_set(&dr->dr_ref, 0);
                dr->dr_bio_count = bio_count;
                dr->dr_error = 0;
@@ -366,27 +417,6 @@ vdev_disk_dio_free(dio_request_t *dr)
            sizeof (struct bio *) * dr->dr_bio_count);
 }
 
-static int
-vdev_disk_dio_is_sync(dio_request_t *dr)
-{
-#ifdef HAVE_BIO_RW_SYNC
-       /* BIO_RW_SYNC preferred interface from 2.6.12-2.6.29 */
-       return (dr->dr_rw & (1 << BIO_RW_SYNC));
-#else
-#ifdef HAVE_BIO_RW_SYNCIO
-       /* BIO_RW_SYNCIO preferred interface from 2.6.30-2.6.35 */
-       return (dr->dr_rw & (1 << BIO_RW_SYNCIO));
-#else
-#ifdef HAVE_REQ_SYNC
-       /* REQ_SYNC preferred interface from 2.6.36-2.6.xx */
-       return (dr->dr_rw & REQ_SYNC);
-#else
-#error "Unable to determine bio sync flag"
-#endif /* HAVE_REQ_SYNC */
-#endif /* HAVE_BIO_RW_SYNC */
-#endif /* HAVE_BIO_RW_SYNCIO */
-}
-
 static void
 vdev_disk_dio_get(dio_request_t *dr)
 {
@@ -409,58 +439,36 @@ vdev_disk_dio_put(dio_request_t *dr)
                vdev_disk_dio_free(dr);
 
                if (zio) {
-                       zio->io_delay = jiffies_64 - zio->io_delay;
                        zio->io_error = error;
                        ASSERT3S(zio->io_error, >=, 0);
                        if (zio->io_error)
                                vdev_disk_error(zio);
-                       zio_interrupt(zio);
+
+                       zio_delay_interrupt(zio);
                }
        }
 
        return (rc);
 }
 
-BIO_END_IO_PROTO(vdev_disk_physio_completion, bio, size, error)
+BIO_END_IO_PROTO(vdev_disk_physio_completion, bio, error)
 {
        dio_request_t *dr = bio->bi_private;
        int rc;
 
-       /* Fatal error but print some useful debugging before asserting */
-       if (dr == NULL)
-               PANIC("dr == NULL, bio->bi_private == NULL\n"
-                   "bi_next: %p, bi_flags: %lx, bi_rw: %lu, bi_vcnt: %d\n"
-                   "bi_idx: %d, bi_size: %d, bi_end_io: %p, bi_cnt: %d\n",
-                   bio->bi_next, bio->bi_flags, bio->bi_rw, bio->bi_vcnt,
-                   bio->bi_idx, bio->bi_size, bio->bi_end_io,
-                   atomic_read(&bio->bi_cnt));
-
-#ifndef HAVE_2ARGS_BIO_END_IO_T
-       if (bio->bi_size)
-               return (1);
-#endif /* HAVE_2ARGS_BIO_END_IO_T */
-
-       if (error == 0 && !test_bit(BIO_UPTODATE, &bio->bi_flags))
-               error = (-EIO);
-
-       if (dr->dr_error == 0)
-               dr->dr_error = -error;
+       if (dr->dr_error == 0) {
+#ifdef HAVE_1ARG_BIO_END_IO_T
+               dr->dr_error = BIO_END_IO_ERROR(bio);
+#else
+               if (error)
+                       dr->dr_error = -(error);
+               else if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
+                       dr->dr_error = EIO;
+#endif
+       }
 
-       /* Drop reference aquired by __vdev_disk_physio */
+       /* Drop reference acquired by __vdev_disk_physio */
        rc = vdev_disk_dio_put(dr);
-
-       /* Wake up synchronous waiter this is the last outstanding bio */
-       if ((rc == 1) && vdev_disk_dio_is_sync(dr))
-               complete(&dr->dr_comp);
-
-       BIO_END_IO_RETURN(0);
-}
-
-static inline unsigned long
-bio_nr_pages(void *bio_ptr, unsigned int bio_size)
-{
-       return ((((unsigned long)bio_ptr + bio_size + PAGE_SIZE - 1) >>
-           PAGE_SHIFT) - ((unsigned long)bio_ptr >> PAGE_SHIFT));
 }
 
 static unsigned int
@@ -479,11 +487,18 @@ bio_map(struct bio *bio, void *bio_ptr, unsigned int bio_size)
                if (size > bio_size)
                        size = bio_size;
 
-               if (kmem_virt(bio_ptr))
+               if (is_vmalloc_addr(bio_ptr))
                        page = vmalloc_to_page(bio_ptr);
                else
                        page = virt_to_page(bio_ptr);
 
+               /*
+                * Some network related block device uses tcp_sendpage, which
+                * doesn't behave well when using 0-count page, this is a
+                * safety net to catch them.
+                */
+               ASSERT3S(page_count(page), >, 0);
+
                if (bio_add_page(bio, page, size, offset) != size)
                        break;
 
@@ -495,28 +510,104 @@ bio_map(struct bio *bio, void *bio_ptr, unsigned int bio_size)
        return (bio_size);
 }
 
+static unsigned int
+bio_map_abd_off(struct bio *bio, abd_t *abd, unsigned int size, size_t off)
+{
+       if (abd_is_linear(abd))
+               return (bio_map(bio, ((char *)abd_to_buf(abd)) + off, size));
+
+       return (abd_scatter_bio_map_off(bio, abd, size, off));
+}
+
+static inline void
+vdev_submit_bio_impl(struct bio *bio)
+{
+#ifdef HAVE_1ARG_SUBMIT_BIO
+       submit_bio(bio);
+#else
+       submit_bio(0, bio);
+#endif
+}
+
+#ifdef HAVE_BIO_SET_DEV
+#if defined(CONFIG_BLK_CGROUP) && defined(HAVE_BIO_SET_DEV_GPL_ONLY)
+/*
+ * The Linux 5.0 kernel updated the bio_set_dev() macro so it calls the
+ * GPL-only bio_associate_blkg() symbol thus inadvertently converting
+ * the entire macro.  Provide a minimal version which always assigns the
+ * request queue's root_blkg to the bio.
+ */
+static inline void
+vdev_bio_associate_blkg(struct bio *bio)
+{
+       struct request_queue *q = bio->bi_disk->queue;
+
+       ASSERT3P(q, !=, NULL);
+       ASSERT3P(bio->bi_blkg, ==, NULL);
+
+       if (blkg_tryget(q->root_blkg))
+               bio->bi_blkg = q->root_blkg;
+}
+#define        bio_associate_blkg vdev_bio_associate_blkg
+#endif
+#else
+/*
+ * Provide a bio_set_dev() helper macro for pre-Linux 4.14 kernels.
+ */
+static inline void
+bio_set_dev(struct bio *bio, struct block_device *bdev)
+{
+       bio->bi_bdev = bdev;
+}
+#endif /* HAVE_BIO_SET_DEV */
+
+static inline void
+vdev_submit_bio(struct bio *bio)
+{
+#ifdef HAVE_CURRENT_BIO_TAIL
+       struct bio **bio_tail = current->bio_tail;
+       current->bio_tail = NULL;
+       vdev_submit_bio_impl(bio);
+       current->bio_tail = bio_tail;
+#else
+       struct bio_list *bio_list = current->bio_list;
+       current->bio_list = NULL;
+       vdev_submit_bio_impl(bio);
+       current->bio_list = bio_list;
+#endif
+}
+
 static int
-__vdev_disk_physio(struct block_device *bdev, zio_t *zio, caddr_t kbuf_ptr,
-    size_t kbuf_size, uint64_t kbuf_offset, int flags)
+__vdev_disk_physio(struct block_device *bdev, zio_t *zio,
+    size_t io_size, uint64_t io_offset, int rw, int flags)
 {
        dio_request_t *dr;
-       caddr_t bio_ptr;
+       uint64_t abd_offset;
        uint64_t bio_offset;
        int bio_size, bio_count = 16;
        int i = 0, error = 0;
-
-       ASSERT3U(kbuf_offset + kbuf_size, <=, bdev->bd_inode->i_size);
+#if defined(HAVE_BLK_QUEUE_HAVE_BLK_PLUG)
+       struct blk_plug plug;
+#endif
+       /*
+        * Accessing outside the block device is never allowed.
+        */
+       if (io_offset + io_size > bdev->bd_inode->i_size) {
+               vdev_dbgmsg(zio->io_vd,
+                   "Illegal access %llu size %llu, device size %llu",
+                   io_offset, io_size, i_size_read(bdev->bd_inode));
+               return (SET_ERROR(EIO));
+       }
 
 retry:
        dr = vdev_disk_dio_alloc(bio_count);
        if (dr == NULL)
-               return (ENOMEM);
+               return (SET_ERROR(ENOMEM));
 
        if (zio && !(zio->io_flags & (ZIO_FLAG_IO_RETRY | ZIO_FLAG_TRYHARD)))
-                       bio_set_flags_failfast(bdev, &flags);
+               bio_set_flags_failfast(bdev, &flags);
 
        dr->dr_zio = zio;
-       dr->dr_rw = flags;
 
        /*
         * When the IO size exceeds the maximum bio size for the request
@@ -525,9 +616,10 @@ retry:
         * their volume block size to match the maximum request size and
         * the common case will be one bio per vdev IO request.
         */
-       bio_ptr    = kbuf_ptr;
-       bio_offset = kbuf_offset;
-       bio_size   = kbuf_size;
+
+       abd_offset = 0;
+       bio_offset = io_offset;
+       bio_size   = io_size;
        for (i = 0; i <= dr->dr_bio_count; i++) {
 
                /* Finished constructing bio's for given buffer */
@@ -545,74 +637,66 @@ retry:
                        goto retry;
                }
 
+               /* bio_alloc() with __GFP_WAIT never returns NULL */
                dr->dr_bio[i] = bio_alloc(GFP_NOIO,
-                   bio_nr_pages(bio_ptr, bio_size));
-               if (dr->dr_bio[i] == NULL) {
+                   MIN(abd_nr_pages_off(zio->io_abd, bio_size, abd_offset),
+                   BIO_MAX_PAGES));
+               if (unlikely(dr->dr_bio[i] == NULL)) {
                        vdev_disk_dio_free(dr);
-                       return (ENOMEM);
+                       return (SET_ERROR(ENOMEM));
                }
 
                /* Matching put called by vdev_disk_physio_completion */
                vdev_disk_dio_get(dr);
 
-               dr->dr_bio[i]->bi_bdev = bdev;
-               dr->dr_bio[i]->bi_sector = bio_offset >> 9;
-               dr->dr_bio[i]->bi_rw = dr->dr_rw;
+               bio_set_dev(dr->dr_bio[i], bdev);
+               BIO_BI_SECTOR(dr->dr_bio[i]) = bio_offset >> 9;
                dr->dr_bio[i]->bi_end_io = vdev_disk_physio_completion;
                dr->dr_bio[i]->bi_private = dr;
+               bio_set_op_attrs(dr->dr_bio[i], rw, flags);
 
                /* Remaining size is returned to become the new size */
-               bio_size = bio_map(dr->dr_bio[i], bio_ptr, bio_size);
+               bio_size = bio_map_abd_off(dr->dr_bio[i], zio->io_abd,
+                   bio_size, abd_offset);
 
                /* Advance in buffer and construct another bio if needed */
-               bio_ptr    += dr->dr_bio[i]->bi_size;
-               bio_offset += dr->dr_bio[i]->bi_size;
+               abd_offset += BIO_BI_SIZE(dr->dr_bio[i]);
+               bio_offset += BIO_BI_SIZE(dr->dr_bio[i]);
        }
 
-       /* Extra reference to protect dio_request during submit_bio */
+       /* Extra reference to protect dio_request during vdev_submit_bio */
        vdev_disk_dio_get(dr);
-       if (zio)
-               zio->io_delay = jiffies_64;
+
+#if defined(HAVE_BLK_QUEUE_HAVE_BLK_PLUG)
+       if (dr->dr_bio_count > 1)
+               blk_start_plug(&plug);
+#endif
 
        /* Submit all bio's associated with this dio */
        for (i = 0; i < dr->dr_bio_count; i++)
                if (dr->dr_bio[i])
-                       submit_bio(dr->dr_rw, dr->dr_bio[i]);
+                       vdev_submit_bio(dr->dr_bio[i]);
 
-       /*
-        * On synchronous blocking requests we wait for all bio the completion
-        * callbacks to run.  We will be woken when the last callback runs
-        * for this dio.  We are responsible for putting the last dio_request
-        * reference will in turn put back the last bio references.  The
-        * only synchronous consumer is vdev_disk_read_rootlabel() all other
-        * IO originating from vdev_disk_io_start() is asynchronous.
-        */
-       if (vdev_disk_dio_is_sync(dr)) {
-               wait_for_completion(&dr->dr_comp);
-               error = dr->dr_error;
-               ASSERT3S(atomic_read(&dr->dr_ref), ==, 1);
-       }
+#if defined(HAVE_BLK_QUEUE_HAVE_BLK_PLUG)
+       if (dr->dr_bio_count > 1)
+               blk_finish_plug(&plug);
+#endif
 
        (void) vdev_disk_dio_put(dr);
 
        return (error);
 }
 
-int
-vdev_disk_physio(struct block_device *bdev, caddr_t kbuf,
-    size_t size, uint64_t offset, int flags)
-{
-       bio_set_flags_failfast(bdev, &flags);
-       return (__vdev_disk_physio(bdev, NULL, kbuf, size, offset, flags));
-}
-
-BIO_END_IO_PROTO(vdev_disk_io_flush_completion, bio, size, rc)
+BIO_END_IO_PROTO(vdev_disk_io_flush_completion, bio, error)
 {
        zio_t *zio = bio->bi_private;
+#ifdef HAVE_1ARG_BIO_END_IO_T
+       zio->io_error = BIO_END_IO_ERROR(bio);
+#else
+       zio->io_error = -error;
+#endif
 
-       zio->io_delay = jiffies_64 - zio->io_delay;
-       zio->io_error = -rc;
-       if (rc && (rc == -EOPNOTSUPP))
+       if (zio->io_error && (zio->io_error == EOPNOTSUPP))
                zio->io_vd->vdev_nowritecache = B_TRUE;
 
        bio_put(bio);
@@ -620,8 +704,6 @@ BIO_END_IO_PROTO(vdev_disk_io_flush_completion, bio, size, rc)
        if (zio->io_error)
                vdev_disk_error(zio);
        zio_interrupt(zio);
-
-       BIO_END_IO_RETURN(0);
 }
 
 static int
@@ -632,34 +714,62 @@ vdev_disk_io_flush(struct block_device *bdev, zio_t *zio)
 
        q = bdev_get_queue(bdev);
        if (!q)
-               return (ENXIO);
+               return (SET_ERROR(ENXIO));
 
        bio = bio_alloc(GFP_NOIO, 0);
-       if (!bio)
-               return (ENOMEM);
+       /* bio_alloc() with __GFP_WAIT never returns NULL */
+       if (unlikely(bio == NULL))
+               return (SET_ERROR(ENOMEM));
 
        bio->bi_end_io = vdev_disk_io_flush_completion;
        bio->bi_private = zio;
-       bio->bi_bdev = bdev;
-       zio->io_delay = jiffies_64;
-       submit_bio(VDEV_WRITE_FLUSH_FUA, bio);
+       bio_set_dev(bio, bdev);
+       bio_set_flush(bio);
+       vdev_submit_bio(bio);
+       invalidate_bdev(bdev);
 
        return (0);
 }
 
-static int
+static void
 vdev_disk_io_start(zio_t *zio)
 {
        vdev_t *v = zio->io_vd;
        vdev_disk_t *vd = v->vdev_tsd;
-       int flags, error;
+       unsigned long trim_flags = 0;
+       int rw, flags, error;
+
+       /*
+        * If the vdev is closed, it's likely in the REMOVED or FAULTED state.
+        * Nothing to be done here but return failure.
+        */
+       if (vd == NULL) {
+               zio->io_error = ENXIO;
+               zio_interrupt(zio);
+               return;
+       }
+
+       rw_enter(&vd->vd_lock, RW_READER);
+
+       /*
+        * If the vdev is closed, it's likely due to a failed reopen and is
+        * in the UNAVAIL state.  Nothing to be done here but return failure.
+        */
+       if (vd->vd_bdev == NULL) {
+               rw_exit(&vd->vd_lock);
+               zio->io_error = ENXIO;
+               zio_interrupt(zio);
+               return;
+       }
 
        switch (zio->io_type) {
        case ZIO_TYPE_IOCTL:
 
                if (!vdev_readable(v)) {
+                       rw_exit(&vd->vd_lock);
                        zio->io_error = SET_ERROR(ENXIO);
-                       return (ZIO_PIPELINE_CONTINUE);
+                       zio_interrupt(zio);
+                       return;
                }
 
                switch (zio->io_cmd) {
@@ -674,12 +784,12 @@ vdev_disk_io_start(zio_t *zio)
                        }
 
                        error = vdev_disk_io_flush(vd->vd_bdev, zio);
-                       if (error == 0)
-                               return (ZIO_PIPELINE_STOP);
+                       if (error == 0) {
+                               rw_exit(&vd->vd_lock);
+                               return;
+                       }
 
                        zio->io_error = error;
-                       if (error == ENOTSUP)
-                               v->vdev_nowritecache = B_TRUE;
 
                        break;
 
@@ -687,29 +797,61 @@ vdev_disk_io_start(zio_t *zio)
                        zio->io_error = SET_ERROR(ENOTSUP);
                }
 
-               return (ZIO_PIPELINE_CONTINUE);
-
+               rw_exit(&vd->vd_lock);
+               zio_execute(zio);
+               return;
        case ZIO_TYPE_WRITE:
-               flags = WRITE;
+               rw = WRITE;
+#if defined(HAVE_BLK_QUEUE_HAVE_BIO_RW_UNPLUG)
+               flags = (1 << BIO_RW_UNPLUG);
+#elif defined(REQ_UNPLUG)
+               flags = REQ_UNPLUG;
+#else
+               flags = 0;
+#endif
                break;
 
        case ZIO_TYPE_READ:
-               flags = READ;
+               rw = READ;
+#if defined(HAVE_BLK_QUEUE_HAVE_BIO_RW_UNPLUG)
+               flags = (1 << BIO_RW_UNPLUG);
+#elif defined(REQ_UNPLUG)
+               flags = REQ_UNPLUG;
+#else
+               flags = 0;
+#endif
                break;
 
+       case ZIO_TYPE_TRIM:
+#if defined(BLKDEV_DISCARD_SECURE)
+               if (zio->io_trim_flags & ZIO_TRIM_SECURE)
+                       trim_flags |= BLKDEV_DISCARD_SECURE;
+#endif
+               zio->io_error = -blkdev_issue_discard(vd->vd_bdev,
+                   zio->io_offset >> 9, zio->io_size >> 9, GFP_NOFS,
+                   trim_flags);
+
+               rw_exit(&vd->vd_lock);
+               zio_interrupt(zio);
+               return;
+
        default:
+               rw_exit(&vd->vd_lock);
                zio->io_error = SET_ERROR(ENOTSUP);
-               return (ZIO_PIPELINE_CONTINUE);
+               zio_interrupt(zio);
+               return;
        }
 
-       error = __vdev_disk_physio(vd->vd_bdev, zio, zio->io_data,
-           zio->io_size, zio->io_offset, flags);
+       zio->io_target_timestamp = zio_handle_io_delay(zio);
+       error = __vdev_disk_physio(vd->vd_bdev, zio,
+           zio->io_size, zio->io_offset, rw, flags);
+       rw_exit(&vd->vd_lock);
+
        if (error) {
                zio->io_error = error;
-               return (ZIO_PIPELINE_CONTINUE);
+               zio_interrupt(zio);
+               return;
        }
-
-       return (ZIO_PIPELINE_STOP);
 }
 
 static void
@@ -761,6 +903,37 @@ vdev_disk_rele(vdev_t *vd)
        /* XXX: Implement me as a vnode rele for the device */
 }
 
+static int
+param_set_vdev_scheduler(const char *val, zfs_kernel_param_t *kp)
+{
+       spa_t *spa = NULL;
+       char *p;
+
+       if (val == NULL)
+               return (SET_ERROR(-EINVAL));
+
+       if ((p = strchr(val, '\n')) != NULL)
+               *p = '\0';
+
+       if (spa_mode_global != 0) {
+               mutex_enter(&spa_namespace_lock);
+               while ((spa = spa_next(spa)) != NULL) {
+                       if (spa_state(spa) != POOL_STATE_ACTIVE ||
+                           !spa_writeable(spa) || spa_suspended(spa))
+                               continue;
+
+                       spa_open_ref(spa, FTAG);
+                       mutex_exit(&spa_namespace_lock);
+                       vdev_elevator_switch(spa->spa_root_vdev, (char *)val);
+                       mutex_enter(&spa_namespace_lock);
+                       spa_close(spa, FTAG);
+               }
+               mutex_exit(&spa_namespace_lock);
+       }
+
+       return (param_set_charp(val, kp));
+}
+
 vdev_ops_t vdev_disk_ops = {
        vdev_disk_open,
        vdev_disk_close,
@@ -768,74 +941,15 @@ vdev_ops_t vdev_disk_ops = {
        vdev_disk_io_start,
        vdev_disk_io_done,
        NULL,
+       NULL,
        vdev_disk_hold,
        vdev_disk_rele,
+       NULL,
+       vdev_default_xlate,
        VDEV_TYPE_DISK,         /* name of this vdev type */
        B_TRUE                  /* leaf vdev */
 };
 
-/*
- * Given the root disk device devid or pathname, read the label from
- * the device, and construct a configuration nvlist.
- */
-int
-vdev_disk_read_rootlabel(char *devpath, char *devid, nvlist_t **config)
-{
-       struct block_device *bdev;
-       vdev_label_t *label;
-       uint64_t s, size;
-       int i;
-
-       bdev = vdev_bdev_open(devpath, vdev_bdev_mode(FREAD), zfs_vdev_holder);
-       if (IS_ERR(bdev))
-               return (-PTR_ERR(bdev));
-
-       s = bdev_capacity(bdev);
-       if (s == 0) {
-               vdev_bdev_close(bdev, vdev_bdev_mode(FREAD));
-               return (EIO);
-       }
-
-       size = P2ALIGN_TYPED(s, sizeof (vdev_label_t), uint64_t);
-       label = vmem_alloc(sizeof (vdev_label_t), KM_PUSHPAGE);
-
-       for (i = 0; i < VDEV_LABELS; i++) {
-               uint64_t offset, state, txg = 0;
-
-               /* read vdev label */
-               offset = vdev_label_offset(size, i, 0);
-               if (vdev_disk_physio(bdev, (caddr_t)label,
-                   VDEV_SKIP_SIZE + VDEV_PHYS_SIZE, offset, READ_SYNC) != 0)
-                       continue;
-
-               if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist,
-                   sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0) {
-                       *config = NULL;
-                       continue;
-               }
-
-               if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
-                   &state) != 0 || state >= POOL_STATE_DESTROYED) {
-                       nvlist_free(*config);
-                       *config = NULL;
-                       continue;
-               }
-
-               if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
-                   &txg) != 0 || txg == 0) {
-                       nvlist_free(*config);
-                       *config = NULL;
-                       continue;
-               }
-
-               break;
-       }
-
-       vmem_free(label, sizeof (vdev_label_t));
-       vdev_bdev_close(bdev, vdev_bdev_mode(FREAD));
-
-       return (0);
-}
-
-module_param(zfs_vdev_scheduler, charp, 0644);
+module_param_call(zfs_vdev_scheduler, param_set_vdev_scheduler,
+    param_get_charp, &zfs_vdev_scheduler, 0644);
 MODULE_PARM_DESC(zfs_vdev_scheduler, "I/O scheduler");