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; 51*1732Sbonwick 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 157*1732Sbonwick /* 158*1732Sbonwick * If we own the whole disk, try to enable disk write caching. 159*1732Sbonwick * We ignore errors because it's OK if we can't do it. 160*1732Sbonwick */ 1611489Swebaker if (vd->vdev_wholedisk == 1) { 162*1732Sbonwick int wce = 1; 163*1732Sbonwick (void) ldi_ioctl(dvd->vd_lh, DKIOCSETWCE, (intptr_t)&wce, 164*1732Sbonwick FKIOCTL, kcred, NULL); 165*1732Sbonwick } 1661489Swebaker 167*1732Sbonwick /* 168*1732Sbonwick * Determine the device's minimum transfer size. 169*1732Sbonwick * If the ioctl isn't supported, assume DEV_BSIZE. 170*1732Sbonwick */ 171*1732Sbonwick if (ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFO, (intptr_t)&dkm, 172*1732Sbonwick FKIOCTL, kcred, NULL) != 0) 173*1732Sbonwick dkm.dki_lbsize = DEV_BSIZE; 1741489Swebaker 175*1732Sbonwick *ashift = highbit(MAX(dkm.dki_lbsize, SPA_MINBLOCKSIZE)) - 1; 1761489Swebaker 177789Sahrens return (0); 178789Sahrens } 179789Sahrens 180789Sahrens static void 181789Sahrens vdev_disk_close(vdev_t *vd) 182789Sahrens { 183789Sahrens vdev_disk_t *dvd = vd->vdev_tsd; 184789Sahrens 185789Sahrens if (dvd == NULL) 186789Sahrens return; 187789Sahrens 188789Sahrens dprintf("removing disk %s, devid %s\n", 189789Sahrens vd->vdev_path ? vd->vdev_path : "<none>", 190789Sahrens vd->vdev_devid ? vd->vdev_devid : "<none>"); 191789Sahrens 192789Sahrens if (dvd->vd_minor != NULL) 193789Sahrens ddi_devid_str_free(dvd->vd_minor); 194789Sahrens 195789Sahrens if (dvd->vd_devid != NULL) 196789Sahrens ddi_devid_free(dvd->vd_devid); 197789Sahrens 198789Sahrens if (dvd->vd_lh != NULL) 199789Sahrens (void) ldi_close(dvd->vd_lh, spa_mode, kcred); 200789Sahrens 201789Sahrens kmem_free(dvd, sizeof (vdev_disk_t)); 202789Sahrens vd->vdev_tsd = NULL; 203789Sahrens } 204789Sahrens 205789Sahrens static void 206789Sahrens vdev_disk_io_intr(buf_t *bp) 207789Sahrens { 208789Sahrens vdev_disk_buf_t *vdb = (vdev_disk_buf_t *)bp; 209789Sahrens zio_t *zio = vdb->vdb_io; 210789Sahrens 211789Sahrens if ((zio->io_error = geterror(bp)) == 0 && bp->b_resid != 0) 212789Sahrens zio->io_error = EIO; 213789Sahrens 214789Sahrens kmem_free(vdb, sizeof (vdev_disk_buf_t)); 215789Sahrens 216789Sahrens zio_next_stage_async(zio); 217789Sahrens } 218789Sahrens 219789Sahrens static void 220789Sahrens vdev_disk_ioctl_done(void *zio_arg, int error) 221789Sahrens { 222789Sahrens zio_t *zio = zio_arg; 223789Sahrens 224789Sahrens zio->io_error = error; 225789Sahrens 226789Sahrens zio_next_stage_async(zio); 227789Sahrens } 228789Sahrens 229789Sahrens static void 230789Sahrens vdev_disk_io_start(zio_t *zio) 231789Sahrens { 232789Sahrens vdev_t *vd = zio->io_vd; 233789Sahrens vdev_disk_t *dvd = vd->vdev_tsd; 234789Sahrens vdev_disk_buf_t *vdb; 235789Sahrens buf_t *bp; 236789Sahrens int flags, error; 237789Sahrens 238789Sahrens if (zio->io_type == ZIO_TYPE_IOCTL) { 239789Sahrens zio_vdev_io_bypass(zio); 240789Sahrens 241789Sahrens /* XXPOLICY */ 242789Sahrens if (vdev_is_dead(vd)) { 243789Sahrens zio->io_error = ENXIO; 244789Sahrens zio_next_stage_async(zio); 245789Sahrens return; 246789Sahrens } 247789Sahrens 248789Sahrens switch (zio->io_cmd) { 249789Sahrens 250789Sahrens case DKIOCFLUSHWRITECACHE: 251789Sahrens 252789Sahrens zio->io_dk_callback.dkc_callback = vdev_disk_ioctl_done; 253789Sahrens zio->io_dk_callback.dkc_cookie = zio; 254789Sahrens 255789Sahrens error = ldi_ioctl(dvd->vd_lh, zio->io_cmd, 256789Sahrens (uintptr_t)&zio->io_dk_callback, 257789Sahrens FKIOCTL, kcred, NULL); 258789Sahrens 259789Sahrens if (error == 0) { 260789Sahrens /* 261789Sahrens * The ioctl will be done asychronously, 262789Sahrens * and will call vdev_disk_ioctl_done() 263789Sahrens * upon completion. 264789Sahrens */ 265789Sahrens return; 266789Sahrens } 267789Sahrens zio->io_error = error; 268789Sahrens break; 269789Sahrens 270789Sahrens default: 271789Sahrens zio->io_error = ENOTSUP; 272789Sahrens } 273789Sahrens 274789Sahrens zio_next_stage_async(zio); 275789Sahrens return; 276789Sahrens } 277789Sahrens 278789Sahrens if (zio->io_type == ZIO_TYPE_READ && vdev_cache_read(zio) == 0) 279789Sahrens return; 280789Sahrens 281789Sahrens if ((zio = vdev_queue_io(zio)) == NULL) 282789Sahrens return; 283789Sahrens 284789Sahrens flags = (zio->io_type == ZIO_TYPE_READ ? B_READ : B_WRITE); 285789Sahrens flags |= B_BUSY | B_NOCACHE; 286789Sahrens if (zio->io_flags & ZIO_FLAG_FAILFAST) 287789Sahrens flags |= B_FAILFAST; 288789Sahrens 289789Sahrens vdb = kmem_alloc(sizeof (vdev_disk_buf_t), KM_SLEEP); 290789Sahrens 291789Sahrens vdb->vdb_io = zio; 292789Sahrens bp = &vdb->vdb_buf; 293789Sahrens 294789Sahrens bioinit(bp); 295789Sahrens bp->b_flags = flags; 296789Sahrens bp->b_bcount = zio->io_size; 297789Sahrens bp->b_un.b_addr = zio->io_data; 298789Sahrens bp->b_lblkno = lbtodb(zio->io_offset); 299789Sahrens bp->b_bufsize = zio->io_size; 300789Sahrens bp->b_iodone = (int (*)())vdev_disk_io_intr; 301789Sahrens 302789Sahrens /* XXPOLICY */ 303789Sahrens error = vdev_is_dead(vd) ? ENXIO : vdev_error_inject(vd, zio); 304789Sahrens if (error) { 305789Sahrens zio->io_error = error; 306789Sahrens bioerror(bp, error); 307789Sahrens bp->b_resid = bp->b_bcount; 308789Sahrens bp->b_iodone(bp); 309789Sahrens return; 310789Sahrens } 311789Sahrens 312789Sahrens error = ldi_strategy(dvd->vd_lh, bp); 313789Sahrens /* ldi_strategy() will return non-zero only on programming errors */ 314789Sahrens ASSERT(error == 0); 315789Sahrens } 316789Sahrens 317789Sahrens static void 318789Sahrens vdev_disk_io_done(zio_t *zio) 319789Sahrens { 320789Sahrens vdev_queue_io_done(zio); 321789Sahrens 322789Sahrens if (zio->io_type == ZIO_TYPE_WRITE) 323789Sahrens vdev_cache_write(zio); 324789Sahrens 3251544Seschrock if (zio_injection_enabled && zio->io_error == 0) 3261544Seschrock zio->io_error = zio_handle_device_injection(zio->io_vd, EIO); 3271544Seschrock 328789Sahrens zio_next_stage(zio); 329789Sahrens } 330789Sahrens 331789Sahrens vdev_ops_t vdev_disk_ops = { 332789Sahrens vdev_disk_open, 333789Sahrens vdev_disk_close, 334789Sahrens vdev_default_asize, 335789Sahrens vdev_disk_io_start, 336789Sahrens vdev_disk_io_done, 337789Sahrens NULL, 338789Sahrens VDEV_TYPE_DISK, /* name of this vdev type */ 339789Sahrens B_TRUE /* leaf vdev */ 340789Sahrens }; 341