1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51489Swebaker * Common Development and Distribution License (the "License"). 61489Swebaker * You may not use this file except in compliance with the License. 7789Sahrens * 8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9789Sahrens * or http://www.opensolaris.org/os/licensing. 10789Sahrens * See the License for the specific language governing permissions 11789Sahrens * and limitations under the License. 12789Sahrens * 13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15789Sahrens * If applicable, add the following below this CDDL HEADER, with the 16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18789Sahrens * 19789Sahrens * CDDL HEADER END 20789Sahrens */ 21789Sahrens /* 221489Swebaker * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 26789Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 27789Sahrens 28789Sahrens #include <sys/zfs_context.h> 29789Sahrens #include <sys/spa.h> 30789Sahrens #include <sys/vdev_disk.h> 31789Sahrens #include <sys/vdev_impl.h> 32789Sahrens #include <sys/fs/zfs.h> 33789Sahrens #include <sys/zio.h> 341171Seschrock #include <sys/sunldi.h> 35789Sahrens 36789Sahrens /* 37789Sahrens * Virtual device vector for disks. 38789Sahrens */ 39789Sahrens 40789Sahrens extern ldi_ident_t zfs_li; 41789Sahrens 42789Sahrens typedef struct vdev_disk_buf { 43789Sahrens buf_t vdb_buf; 44789Sahrens zio_t *vdb_io; 45789Sahrens } vdev_disk_buf_t; 46789Sahrens 47789Sahrens static int 48789Sahrens vdev_disk_open(vdev_t *vd, uint64_t *psize, uint64_t *ashift) 49789Sahrens { 50789Sahrens vdev_disk_t *dvd; 511732Sbonwick struct dk_minfo dkm; 52789Sahrens int error; 53789Sahrens 54789Sahrens /* 55789Sahrens * We must have a pathname, and it must be absolute. 56789Sahrens */ 57789Sahrens if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') { 58789Sahrens vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL; 59789Sahrens return (EINVAL); 60789Sahrens } 61789Sahrens 62789Sahrens dvd = vd->vdev_tsd = kmem_zalloc(sizeof (vdev_disk_t), KM_SLEEP); 63789Sahrens 64789Sahrens /* 65789Sahrens * When opening a disk device, we want to preserve the user's original 66789Sahrens * intent. We always want to open the device by the path the user gave 67789Sahrens * us, even if it is one of multiple paths to the save device. But we 68789Sahrens * also want to be able to survive disks being removed/recabled. 69789Sahrens * Therefore the sequence of opening devices is: 70789Sahrens * 711171Seschrock * 1. Try opening the device by path. For legacy pools without the 721171Seschrock * 'whole_disk' property, attempt to fix the path by appending 's0'. 73789Sahrens * 74789Sahrens * 2. If the devid of the device matches the stored value, return 75789Sahrens * success. 76789Sahrens * 77789Sahrens * 3. Otherwise, the device may have moved. Try opening the device 78789Sahrens * by the devid instead. 79789Sahrens * 80789Sahrens */ 81789Sahrens if (vd->vdev_devid != NULL) { 82789Sahrens if (ddi_devid_str_decode(vd->vdev_devid, &dvd->vd_devid, 83789Sahrens &dvd->vd_minor) != 0) { 84789Sahrens vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL; 85789Sahrens return (EINVAL); 86789Sahrens } 87789Sahrens } 88789Sahrens 89789Sahrens error = EINVAL; /* presume failure */ 90789Sahrens 91789Sahrens if (vd->vdev_path != NULL) { 92789Sahrens ddi_devid_t devid; 93789Sahrens 941171Seschrock if (vd->vdev_wholedisk == -1ULL) { 951171Seschrock size_t len = strlen(vd->vdev_path) + 3; 961171Seschrock char *buf = kmem_alloc(len, KM_SLEEP); 971171Seschrock ldi_handle_t lh; 981171Seschrock 991171Seschrock (void) snprintf(buf, len, "%ss0", vd->vdev_path); 100789Sahrens 1011171Seschrock if (ldi_open_by_name(buf, spa_mode, kcred, 1021171Seschrock &lh, zfs_li) == 0) { 1031171Seschrock spa_strfree(vd->vdev_path); 1041171Seschrock vd->vdev_path = buf; 1051171Seschrock vd->vdev_wholedisk = 1ULL; 1061171Seschrock (void) ldi_close(lh, spa_mode, kcred); 1071171Seschrock } else { 1081171Seschrock kmem_free(buf, len); 1091171Seschrock } 1101171Seschrock } 111789Sahrens 1121171Seschrock error = ldi_open_by_name(vd->vdev_path, spa_mode, kcred, 1131171Seschrock &dvd->vd_lh, zfs_li); 114789Sahrens 115789Sahrens /* 116789Sahrens * Compare the devid to the stored value. 117789Sahrens */ 118789Sahrens if (error == 0 && vd->vdev_devid != NULL && 119789Sahrens ldi_get_devid(dvd->vd_lh, &devid) == 0) { 120789Sahrens if (ddi_devid_compare(devid, dvd->vd_devid) != 0) { 121789Sahrens error = EINVAL; 122789Sahrens (void) ldi_close(dvd->vd_lh, spa_mode, kcred); 123789Sahrens dvd->vd_lh = NULL; 124789Sahrens } 125789Sahrens ddi_devid_free(devid); 126789Sahrens } 1271171Seschrock 1281171Seschrock /* 1291171Seschrock * If we succeeded in opening the device, but 'vdev_wholedisk' 1301171Seschrock * is not yet set, then this must be a slice. 1311171Seschrock */ 1321171Seschrock if (error == 0 && vd->vdev_wholedisk == -1ULL) 1331171Seschrock vd->vdev_wholedisk = 0; 134789Sahrens } 135789Sahrens 136789Sahrens /* 137789Sahrens * If we were unable to open by path, or the devid check fails, open by 138789Sahrens * devid instead. 139789Sahrens */ 140789Sahrens if (error != 0 && vd->vdev_devid != NULL) 141789Sahrens error = ldi_open_by_devid(dvd->vd_devid, dvd->vd_minor, 142789Sahrens spa_mode, kcred, &dvd->vd_lh, zfs_li); 143789Sahrens 144789Sahrens if (error) { 145789Sahrens vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED; 146789Sahrens return (error); 147789Sahrens } 148789Sahrens 149789Sahrens /* 150789Sahrens * Determine the actual size of the device. 151789Sahrens */ 152789Sahrens if (ldi_get_size(dvd->vd_lh, psize) != 0) { 153789Sahrens vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED; 154789Sahrens return (EINVAL); 155789Sahrens } 156789Sahrens 1571732Sbonwick /* 1581732Sbonwick * If we own the whole disk, try to enable disk write caching. 1591732Sbonwick * We ignore errors because it's OK if we can't do it. 1601732Sbonwick */ 1611489Swebaker if (vd->vdev_wholedisk == 1) { 1621732Sbonwick int wce = 1; 1631732Sbonwick (void) ldi_ioctl(dvd->vd_lh, DKIOCSETWCE, (intptr_t)&wce, 1641732Sbonwick FKIOCTL, kcred, NULL); 1651732Sbonwick } 1661489Swebaker 1671732Sbonwick /* 1681732Sbonwick * Determine the device's minimum transfer size. 1691732Sbonwick * If the ioctl isn't supported, assume DEV_BSIZE. 1701732Sbonwick */ 1711732Sbonwick if (ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFO, (intptr_t)&dkm, 1721732Sbonwick FKIOCTL, kcred, NULL) != 0) 1731732Sbonwick dkm.dki_lbsize = DEV_BSIZE; 1741489Swebaker 1751732Sbonwick *ashift = highbit(MAX(dkm.dki_lbsize, SPA_MINBLOCKSIZE)) - 1; 1761489Swebaker 177*1773Seschrock /* 178*1773Seschrock * Clear the nowritecache bit, so that on a vdev_reopen() we will 179*1773Seschrock * try again. 180*1773Seschrock */ 181*1773Seschrock vd->vdev_nowritecache = B_FALSE; 182*1773Seschrock 183789Sahrens return (0); 184789Sahrens } 185789Sahrens 186789Sahrens static void 187789Sahrens vdev_disk_close(vdev_t *vd) 188789Sahrens { 189789Sahrens vdev_disk_t *dvd = vd->vdev_tsd; 190789Sahrens 191789Sahrens if (dvd == NULL) 192789Sahrens return; 193789Sahrens 194789Sahrens dprintf("removing disk %s, devid %s\n", 195789Sahrens vd->vdev_path ? vd->vdev_path : "<none>", 196789Sahrens vd->vdev_devid ? vd->vdev_devid : "<none>"); 197789Sahrens 198789Sahrens if (dvd->vd_minor != NULL) 199789Sahrens ddi_devid_str_free(dvd->vd_minor); 200789Sahrens 201789Sahrens if (dvd->vd_devid != NULL) 202789Sahrens ddi_devid_free(dvd->vd_devid); 203789Sahrens 204789Sahrens if (dvd->vd_lh != NULL) 205789Sahrens (void) ldi_close(dvd->vd_lh, spa_mode, kcred); 206789Sahrens 207789Sahrens kmem_free(dvd, sizeof (vdev_disk_t)); 208789Sahrens vd->vdev_tsd = NULL; 209789Sahrens } 210789Sahrens 211789Sahrens static void 212789Sahrens vdev_disk_io_intr(buf_t *bp) 213789Sahrens { 214789Sahrens vdev_disk_buf_t *vdb = (vdev_disk_buf_t *)bp; 215789Sahrens zio_t *zio = vdb->vdb_io; 216789Sahrens 217789Sahrens if ((zio->io_error = geterror(bp)) == 0 && bp->b_resid != 0) 218789Sahrens zio->io_error = EIO; 219789Sahrens 220789Sahrens kmem_free(vdb, sizeof (vdev_disk_buf_t)); 221789Sahrens 222789Sahrens zio_next_stage_async(zio); 223789Sahrens } 224789Sahrens 225789Sahrens static void 226789Sahrens vdev_disk_ioctl_done(void *zio_arg, int error) 227789Sahrens { 228789Sahrens zio_t *zio = zio_arg; 229789Sahrens 230789Sahrens zio->io_error = error; 231789Sahrens 232789Sahrens zio_next_stage_async(zio); 233789Sahrens } 234789Sahrens 235789Sahrens static void 236789Sahrens vdev_disk_io_start(zio_t *zio) 237789Sahrens { 238789Sahrens vdev_t *vd = zio->io_vd; 239789Sahrens vdev_disk_t *dvd = vd->vdev_tsd; 240789Sahrens vdev_disk_buf_t *vdb; 241789Sahrens buf_t *bp; 242789Sahrens int flags, error; 243789Sahrens 244789Sahrens if (zio->io_type == ZIO_TYPE_IOCTL) { 245789Sahrens zio_vdev_io_bypass(zio); 246789Sahrens 247789Sahrens /* XXPOLICY */ 248789Sahrens if (vdev_is_dead(vd)) { 249789Sahrens zio->io_error = ENXIO; 250789Sahrens zio_next_stage_async(zio); 251789Sahrens return; 252789Sahrens } 253789Sahrens 254789Sahrens switch (zio->io_cmd) { 255789Sahrens 256789Sahrens case DKIOCFLUSHWRITECACHE: 257789Sahrens 258*1773Seschrock if (vd->vdev_nowritecache) { 259*1773Seschrock zio->io_error = ENOTSUP; 260*1773Seschrock break; 261*1773Seschrock } 262*1773Seschrock 263789Sahrens zio->io_dk_callback.dkc_callback = vdev_disk_ioctl_done; 264789Sahrens zio->io_dk_callback.dkc_cookie = zio; 265789Sahrens 266789Sahrens error = ldi_ioctl(dvd->vd_lh, zio->io_cmd, 267789Sahrens (uintptr_t)&zio->io_dk_callback, 268789Sahrens FKIOCTL, kcred, NULL); 269789Sahrens 270789Sahrens if (error == 0) { 271789Sahrens /* 272789Sahrens * The ioctl will be done asychronously, 273789Sahrens * and will call vdev_disk_ioctl_done() 274789Sahrens * upon completion. 275789Sahrens */ 276789Sahrens return; 277*1773Seschrock } else if (error == ENOTSUP) { 278*1773Seschrock /* 279*1773Seschrock * If we get ENOTSUP, we know that no future 280*1773Seschrock * attempts will ever succeed. In this case we 281*1773Seschrock * set a persistent bit so that we don't bother 282*1773Seschrock * with the ioctl in the future. 283*1773Seschrock */ 284*1773Seschrock vd->vdev_nowritecache = B_TRUE; 285789Sahrens } 286789Sahrens zio->io_error = error; 287*1773Seschrock 288789Sahrens break; 289789Sahrens 290789Sahrens default: 291789Sahrens zio->io_error = ENOTSUP; 292789Sahrens } 293789Sahrens 294789Sahrens zio_next_stage_async(zio); 295789Sahrens return; 296789Sahrens } 297789Sahrens 298789Sahrens if (zio->io_type == ZIO_TYPE_READ && vdev_cache_read(zio) == 0) 299789Sahrens return; 300789Sahrens 301789Sahrens if ((zio = vdev_queue_io(zio)) == NULL) 302789Sahrens return; 303789Sahrens 304789Sahrens flags = (zio->io_type == ZIO_TYPE_READ ? B_READ : B_WRITE); 305789Sahrens flags |= B_BUSY | B_NOCACHE; 306789Sahrens if (zio->io_flags & ZIO_FLAG_FAILFAST) 307789Sahrens flags |= B_FAILFAST; 308789Sahrens 309789Sahrens vdb = kmem_alloc(sizeof (vdev_disk_buf_t), KM_SLEEP); 310789Sahrens 311789Sahrens vdb->vdb_io = zio; 312789Sahrens bp = &vdb->vdb_buf; 313789Sahrens 314789Sahrens bioinit(bp); 315789Sahrens bp->b_flags = flags; 316789Sahrens bp->b_bcount = zio->io_size; 317789Sahrens bp->b_un.b_addr = zio->io_data; 318789Sahrens bp->b_lblkno = lbtodb(zio->io_offset); 319789Sahrens bp->b_bufsize = zio->io_size; 320789Sahrens bp->b_iodone = (int (*)())vdev_disk_io_intr; 321789Sahrens 322789Sahrens /* XXPOLICY */ 323789Sahrens error = vdev_is_dead(vd) ? ENXIO : vdev_error_inject(vd, zio); 324789Sahrens if (error) { 325789Sahrens zio->io_error = error; 326789Sahrens bioerror(bp, error); 327789Sahrens bp->b_resid = bp->b_bcount; 328789Sahrens bp->b_iodone(bp); 329789Sahrens return; 330789Sahrens } 331789Sahrens 332789Sahrens error = ldi_strategy(dvd->vd_lh, bp); 333789Sahrens /* ldi_strategy() will return non-zero only on programming errors */ 334789Sahrens ASSERT(error == 0); 335789Sahrens } 336789Sahrens 337789Sahrens static void 338789Sahrens vdev_disk_io_done(zio_t *zio) 339789Sahrens { 340789Sahrens vdev_queue_io_done(zio); 341789Sahrens 342789Sahrens if (zio->io_type == ZIO_TYPE_WRITE) 343789Sahrens vdev_cache_write(zio); 344789Sahrens 3451544Seschrock if (zio_injection_enabled && zio->io_error == 0) 3461544Seschrock zio->io_error = zio_handle_device_injection(zio->io_vd, EIO); 3471544Seschrock 348789Sahrens zio_next_stage(zio); 349789Sahrens } 350789Sahrens 351789Sahrens vdev_ops_t vdev_disk_ops = { 352789Sahrens vdev_disk_open, 353789Sahrens vdev_disk_close, 354789Sahrens vdev_default_asize, 355789Sahrens vdev_disk_io_start, 356789Sahrens vdev_disk_io_done, 357789Sahrens NULL, 358789Sahrens VDEV_TYPE_DISK, /* name of this vdev type */ 359789Sahrens B_TRUE /* leaf vdev */ 360789Sahrens }; 361