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 /* 22*12208SBo.Zhou@Sun.COM * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23789Sahrens */ 24789Sahrens 25789Sahrens #include <sys/zfs_context.h> 2611958SGeorge.Wilson@Sun.COM #include <sys/spa_impl.h> 276423Sgw25295 #include <sys/refcount.h> 28789Sahrens #include <sys/vdev_disk.h> 29789Sahrens #include <sys/vdev_impl.h> 30789Sahrens #include <sys/fs/zfs.h> 31789Sahrens #include <sys/zio.h> 321171Seschrock #include <sys/sunldi.h> 336976Seschrock #include <sys/fm/fs/zfs.h> 34789Sahrens 35789Sahrens /* 36789Sahrens * Virtual device vector for disks. 37789Sahrens */ 38789Sahrens 39789Sahrens extern ldi_ident_t zfs_li; 40789Sahrens 41789Sahrens typedef struct vdev_disk_buf { 42789Sahrens buf_t vdb_buf; 43789Sahrens zio_t *vdb_io; 44789Sahrens } vdev_disk_buf_t; 45789Sahrens 4611958SGeorge.Wilson@Sun.COM static void 4711958SGeorge.Wilson@Sun.COM vdev_disk_hold(vdev_t *vd) 4811958SGeorge.Wilson@Sun.COM { 4911958SGeorge.Wilson@Sun.COM ddi_devid_t devid; 5011958SGeorge.Wilson@Sun.COM char *minor; 5111958SGeorge.Wilson@Sun.COM 5211958SGeorge.Wilson@Sun.COM ASSERT(spa_config_held(vd->vdev_spa, SCL_STATE, RW_WRITER)); 5311958SGeorge.Wilson@Sun.COM 5411958SGeorge.Wilson@Sun.COM /* 5511958SGeorge.Wilson@Sun.COM * We must have a pathname, and it must be absolute. 5611958SGeorge.Wilson@Sun.COM */ 5711958SGeorge.Wilson@Sun.COM if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') 5811958SGeorge.Wilson@Sun.COM return; 5911958SGeorge.Wilson@Sun.COM 6011958SGeorge.Wilson@Sun.COM /* 6111958SGeorge.Wilson@Sun.COM * Only prefetch path and devid info if the device has 6211958SGeorge.Wilson@Sun.COM * never been opened. 6311958SGeorge.Wilson@Sun.COM */ 6411958SGeorge.Wilson@Sun.COM if (vd->vdev_tsd != NULL) 6511958SGeorge.Wilson@Sun.COM return; 6611958SGeorge.Wilson@Sun.COM 6711958SGeorge.Wilson@Sun.COM if (vd->vdev_wholedisk == -1ULL) { 6811958SGeorge.Wilson@Sun.COM size_t len = strlen(vd->vdev_path) + 3; 6911958SGeorge.Wilson@Sun.COM char *buf = kmem_alloc(len, KM_SLEEP); 7011958SGeorge.Wilson@Sun.COM 7111958SGeorge.Wilson@Sun.COM (void) snprintf(buf, len, "%ss0", vd->vdev_path); 7211958SGeorge.Wilson@Sun.COM 7311958SGeorge.Wilson@Sun.COM (void) ldi_vp_from_name(buf, &vd->vdev_name_vp); 7411958SGeorge.Wilson@Sun.COM kmem_free(buf, len); 7511958SGeorge.Wilson@Sun.COM } 7611958SGeorge.Wilson@Sun.COM 7711958SGeorge.Wilson@Sun.COM if (vd->vdev_name_vp == NULL) 7811958SGeorge.Wilson@Sun.COM (void) ldi_vp_from_name(vd->vdev_path, &vd->vdev_name_vp); 7911958SGeorge.Wilson@Sun.COM 8011958SGeorge.Wilson@Sun.COM if (vd->vdev_devid != NULL && 8111958SGeorge.Wilson@Sun.COM ddi_devid_str_decode(vd->vdev_devid, &devid, &minor) == 0) { 8211958SGeorge.Wilson@Sun.COM (void) ldi_vp_from_devid(devid, minor, &vd->vdev_devid_vp); 8311958SGeorge.Wilson@Sun.COM ddi_devid_str_free(minor); 8411958SGeorge.Wilson@Sun.COM ddi_devid_free(devid); 8511958SGeorge.Wilson@Sun.COM } 8611958SGeorge.Wilson@Sun.COM } 8711958SGeorge.Wilson@Sun.COM 8811958SGeorge.Wilson@Sun.COM static void 8911958SGeorge.Wilson@Sun.COM vdev_disk_rele(vdev_t *vd) 9011958SGeorge.Wilson@Sun.COM { 9111958SGeorge.Wilson@Sun.COM ASSERT(spa_config_held(vd->vdev_spa, SCL_STATE, RW_WRITER)); 9211958SGeorge.Wilson@Sun.COM 9311958SGeorge.Wilson@Sun.COM if (vd->vdev_name_vp) { 9411958SGeorge.Wilson@Sun.COM VN_RELE_ASYNC(vd->vdev_name_vp, 9511958SGeorge.Wilson@Sun.COM dsl_pool_vnrele_taskq(vd->vdev_spa->spa_dsl_pool)); 9611958SGeorge.Wilson@Sun.COM vd->vdev_name_vp = NULL; 9711958SGeorge.Wilson@Sun.COM } 9811958SGeorge.Wilson@Sun.COM if (vd->vdev_devid_vp) { 9911958SGeorge.Wilson@Sun.COM VN_RELE_ASYNC(vd->vdev_devid_vp, 10011958SGeorge.Wilson@Sun.COM dsl_pool_vnrele_taskq(vd->vdev_spa->spa_dsl_pool)); 10111958SGeorge.Wilson@Sun.COM vd->vdev_devid_vp = NULL; 10211958SGeorge.Wilson@Sun.COM } 10311958SGeorge.Wilson@Sun.COM } 10411958SGeorge.Wilson@Sun.COM 105789Sahrens static int 1067754SJeff.Bonwick@Sun.COM vdev_disk_open(vdev_t *vd, uint64_t *psize, uint64_t *ashift) 107789Sahrens { 1088241SJeff.Bonwick@Sun.COM spa_t *spa = vd->vdev_spa; 109789Sahrens vdev_disk_t *dvd; 110*12208SBo.Zhou@Sun.COM struct dk_minfo_ext dkmext; 1117754SJeff.Bonwick@Sun.COM int error; 1125329Sgw25295 dev_t dev; 1137754SJeff.Bonwick@Sun.COM int otyp; 114789Sahrens 115789Sahrens /* 116789Sahrens * We must have a pathname, and it must be absolute. 117789Sahrens */ 118789Sahrens if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') { 119789Sahrens vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL; 120789Sahrens return (EINVAL); 121789Sahrens } 122789Sahrens 12310850SGeorge.Wilson@Sun.COM /* 12410850SGeorge.Wilson@Sun.COM * Reopen the device if it's not currently open. Otherwise, 12510850SGeorge.Wilson@Sun.COM * just update the physical size of the device. 12610850SGeorge.Wilson@Sun.COM */ 12710850SGeorge.Wilson@Sun.COM if (vd->vdev_tsd != NULL) { 12810850SGeorge.Wilson@Sun.COM ASSERT(vd->vdev_reopening); 12910850SGeorge.Wilson@Sun.COM dvd = vd->vdev_tsd; 13010850SGeorge.Wilson@Sun.COM goto skip_open; 13110850SGeorge.Wilson@Sun.COM } 13210850SGeorge.Wilson@Sun.COM 133789Sahrens dvd = vd->vdev_tsd = kmem_zalloc(sizeof (vdev_disk_t), KM_SLEEP); 134789Sahrens 135789Sahrens /* 136789Sahrens * When opening a disk device, we want to preserve the user's original 137789Sahrens * intent. We always want to open the device by the path the user gave 138789Sahrens * us, even if it is one of multiple paths to the save device. But we 139789Sahrens * also want to be able to survive disks being removed/recabled. 140789Sahrens * Therefore the sequence of opening devices is: 141789Sahrens * 1421171Seschrock * 1. Try opening the device by path. For legacy pools without the 1431171Seschrock * 'whole_disk' property, attempt to fix the path by appending 's0'. 144789Sahrens * 145789Sahrens * 2. If the devid of the device matches the stored value, return 146789Sahrens * success. 147789Sahrens * 148789Sahrens * 3. Otherwise, the device may have moved. Try opening the device 149789Sahrens * by the devid instead. 150789Sahrens */ 151789Sahrens if (vd->vdev_devid != NULL) { 152789Sahrens if (ddi_devid_str_decode(vd->vdev_devid, &dvd->vd_devid, 153789Sahrens &dvd->vd_minor) != 0) { 154789Sahrens vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL; 155789Sahrens return (EINVAL); 156789Sahrens } 157789Sahrens } 158789Sahrens 159789Sahrens error = EINVAL; /* presume failure */ 160789Sahrens 16110850SGeorge.Wilson@Sun.COM if (vd->vdev_path != NULL) { 162789Sahrens ddi_devid_t devid; 163789Sahrens 1641171Seschrock if (vd->vdev_wholedisk == -1ULL) { 1651171Seschrock size_t len = strlen(vd->vdev_path) + 3; 1661171Seschrock char *buf = kmem_alloc(len, KM_SLEEP); 1671171Seschrock ldi_handle_t lh; 1681171Seschrock 1691171Seschrock (void) snprintf(buf, len, "%ss0", vd->vdev_path); 170789Sahrens 1718241SJeff.Bonwick@Sun.COM if (ldi_open_by_name(buf, spa_mode(spa), kcred, 1721171Seschrock &lh, zfs_li) == 0) { 1731171Seschrock spa_strfree(vd->vdev_path); 1741171Seschrock vd->vdev_path = buf; 1751171Seschrock vd->vdev_wholedisk = 1ULL; 1768241SJeff.Bonwick@Sun.COM (void) ldi_close(lh, spa_mode(spa), kcred); 1771171Seschrock } else { 1781171Seschrock kmem_free(buf, len); 1791171Seschrock } 1801171Seschrock } 181789Sahrens 1828241SJeff.Bonwick@Sun.COM error = ldi_open_by_name(vd->vdev_path, spa_mode(spa), kcred, 1831171Seschrock &dvd->vd_lh, zfs_li); 184789Sahrens 185789Sahrens /* 186789Sahrens * Compare the devid to the stored value. 187789Sahrens */ 188789Sahrens if (error == 0 && vd->vdev_devid != NULL && 189789Sahrens ldi_get_devid(dvd->vd_lh, &devid) == 0) { 190789Sahrens if (ddi_devid_compare(devid, dvd->vd_devid) != 0) { 191789Sahrens error = EINVAL; 1928241SJeff.Bonwick@Sun.COM (void) ldi_close(dvd->vd_lh, spa_mode(spa), 1938241SJeff.Bonwick@Sun.COM kcred); 194789Sahrens dvd->vd_lh = NULL; 195789Sahrens } 196789Sahrens ddi_devid_free(devid); 197789Sahrens } 1981171Seschrock 1991171Seschrock /* 2001171Seschrock * If we succeeded in opening the device, but 'vdev_wholedisk' 2011171Seschrock * is not yet set, then this must be a slice. 2021171Seschrock */ 2031171Seschrock if (error == 0 && vd->vdev_wholedisk == -1ULL) 2041171Seschrock vd->vdev_wholedisk = 0; 205789Sahrens } 206789Sahrens 207789Sahrens /* 208789Sahrens * If we were unable to open by path, or the devid check fails, open by 209789Sahrens * devid instead. 210789Sahrens */ 211789Sahrens if (error != 0 && vd->vdev_devid != NULL) 212789Sahrens error = ldi_open_by_devid(dvd->vd_devid, dvd->vd_minor, 2138241SJeff.Bonwick@Sun.COM spa_mode(spa), kcred, &dvd->vd_lh, zfs_li); 214789Sahrens 2154451Seschrock /* 2164451Seschrock * If all else fails, then try opening by physical path (if available) 2174451Seschrock * or the logical path (if we failed due to the devid check). While not 2184451Seschrock * as reliable as the devid, this will give us something, and the higher 2194451Seschrock * level vdev validation will prevent us from opening the wrong device. 2204451Seschrock */ 2214451Seschrock if (error) { 2224451Seschrock if (vd->vdev_physpath != NULL && 2238269SMark.Musante@Sun.COM (dev = ddi_pathname_to_dev_t(vd->vdev_physpath)) != NODEV) 2248241SJeff.Bonwick@Sun.COM error = ldi_open_by_dev(&dev, OTYP_BLK, spa_mode(spa), 2254451Seschrock kcred, &dvd->vd_lh, zfs_li); 2264451Seschrock 2274451Seschrock /* 2284451Seschrock * Note that we don't support the legacy auto-wholedisk support 2294451Seschrock * as above. This hasn't been used in a very long time and we 2304451Seschrock * don't need to propagate its oddities to this edge condition. 2314451Seschrock */ 23210850SGeorge.Wilson@Sun.COM if (error && vd->vdev_path != NULL) 2338241SJeff.Bonwick@Sun.COM error = ldi_open_by_name(vd->vdev_path, spa_mode(spa), 2348241SJeff.Bonwick@Sun.COM kcred, &dvd->vd_lh, zfs_li); 2354451Seschrock } 2364451Seschrock 2377754SJeff.Bonwick@Sun.COM if (error) { 238789Sahrens vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED; 2397754SJeff.Bonwick@Sun.COM return (error); 2407754SJeff.Bonwick@Sun.COM } 2415329Sgw25295 242789Sahrens /* 2434451Seschrock * Once a device is opened, verify that the physical device path (if 2444451Seschrock * available) is up to date. 2454451Seschrock */ 2464451Seschrock if (ldi_get_dev(dvd->vd_lh, &dev) == 0 && 2474451Seschrock ldi_get_otyp(dvd->vd_lh, &otyp) == 0) { 2485329Sgw25295 char *physpath, *minorname; 2495329Sgw25295 2504451Seschrock physpath = kmem_alloc(MAXPATHLEN, KM_SLEEP); 2514451Seschrock minorname = NULL; 2524451Seschrock if (ddi_dev_pathname(dev, otyp, physpath) == 0 && 2534451Seschrock ldi_get_minor_name(dvd->vd_lh, &minorname) == 0 && 2544451Seschrock (vd->vdev_physpath == NULL || 2554451Seschrock strcmp(vd->vdev_physpath, physpath) != 0)) { 2564451Seschrock if (vd->vdev_physpath) 2574451Seschrock spa_strfree(vd->vdev_physpath); 2584451Seschrock (void) strlcat(physpath, ":", MAXPATHLEN); 2594451Seschrock (void) strlcat(physpath, minorname, MAXPATHLEN); 2604451Seschrock vd->vdev_physpath = spa_strdup(physpath); 2614451Seschrock } 2624451Seschrock if (minorname) 2634451Seschrock kmem_free(minorname, strlen(minorname) + 1); 2644451Seschrock kmem_free(physpath, MAXPATHLEN); 2654451Seschrock } 2664451Seschrock 26710850SGeorge.Wilson@Sun.COM skip_open: 2684451Seschrock /* 269789Sahrens * Determine the actual size of the device. 270789Sahrens */ 271789Sahrens if (ldi_get_size(dvd->vd_lh, psize) != 0) { 272789Sahrens vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED; 273789Sahrens return (EINVAL); 274789Sahrens } 275789Sahrens 2761732Sbonwick /* 2771732Sbonwick * If we own the whole disk, try to enable disk write caching. 2781732Sbonwick * We ignore errors because it's OK if we can't do it. 2791732Sbonwick */ 2801489Swebaker if (vd->vdev_wholedisk == 1) { 2811732Sbonwick int wce = 1; 2821732Sbonwick (void) ldi_ioctl(dvd->vd_lh, DKIOCSETWCE, (intptr_t)&wce, 2831732Sbonwick FKIOCTL, kcred, NULL); 2841732Sbonwick } 2851489Swebaker 2861732Sbonwick /* 2871732Sbonwick * Determine the device's minimum transfer size. 2881732Sbonwick * If the ioctl isn't supported, assume DEV_BSIZE. 2891732Sbonwick */ 290*12208SBo.Zhou@Sun.COM if (ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFOEXT, (intptr_t)&dkmext, 2911732Sbonwick FKIOCTL, kcred, NULL) != 0) 292*12208SBo.Zhou@Sun.COM dkmext.dki_pbsize = DEV_BSIZE; 2931489Swebaker 294*12208SBo.Zhou@Sun.COM *ashift = highbit(MAX(dkmext.dki_pbsize, SPA_MINBLOCKSIZE)) - 1; 2951489Swebaker 2961773Seschrock /* 2971773Seschrock * Clear the nowritecache bit, so that on a vdev_reopen() we will 2981773Seschrock * try again. 2991773Seschrock */ 3001773Seschrock vd->vdev_nowritecache = B_FALSE; 3011773Seschrock 302789Sahrens return (0); 303789Sahrens } 304789Sahrens 305789Sahrens static void 306789Sahrens vdev_disk_close(vdev_t *vd) 307789Sahrens { 308789Sahrens vdev_disk_t *dvd = vd->vdev_tsd; 309789Sahrens 31010850SGeorge.Wilson@Sun.COM if (vd->vdev_reopening || dvd == NULL) 311789Sahrens return; 312789Sahrens 313789Sahrens if (dvd->vd_minor != NULL) 314789Sahrens ddi_devid_str_free(dvd->vd_minor); 315789Sahrens 316789Sahrens if (dvd->vd_devid != NULL) 317789Sahrens ddi_devid_free(dvd->vd_devid); 318789Sahrens 319789Sahrens if (dvd->vd_lh != NULL) 3208241SJeff.Bonwick@Sun.COM (void) ldi_close(dvd->vd_lh, spa_mode(vd->vdev_spa), kcred); 321789Sahrens 322789Sahrens kmem_free(dvd, sizeof (vdev_disk_t)); 323789Sahrens vd->vdev_tsd = NULL; 324789Sahrens } 325789Sahrens 3266423Sgw25295 int 3276423Sgw25295 vdev_disk_physio(ldi_handle_t vd_lh, caddr_t data, size_t size, 3286423Sgw25295 uint64_t offset, int flags) 3296423Sgw25295 { 3306423Sgw25295 buf_t *bp; 3316423Sgw25295 int error = 0; 3326423Sgw25295 3336423Sgw25295 if (vd_lh == NULL) 3346423Sgw25295 return (EINVAL); 3356423Sgw25295 3366423Sgw25295 ASSERT(flags & B_READ || flags & B_WRITE); 3376423Sgw25295 3386423Sgw25295 bp = getrbuf(KM_SLEEP); 3396423Sgw25295 bp->b_flags = flags | B_BUSY | B_NOCACHE | B_FAILFAST; 3406423Sgw25295 bp->b_bcount = size; 3416423Sgw25295 bp->b_un.b_addr = (void *)data; 3426423Sgw25295 bp->b_lblkno = lbtodb(offset); 3436423Sgw25295 bp->b_bufsize = size; 3446423Sgw25295 3456423Sgw25295 error = ldi_strategy(vd_lh, bp); 3466423Sgw25295 ASSERT(error == 0); 3476423Sgw25295 if ((error = biowait(bp)) == 0 && bp->b_resid != 0) 3486423Sgw25295 error = EIO; 3496423Sgw25295 freerbuf(bp); 3506423Sgw25295 3516423Sgw25295 return (error); 3526423Sgw25295 } 3536423Sgw25295 354789Sahrens static void 355789Sahrens vdev_disk_io_intr(buf_t *bp) 356789Sahrens { 357789Sahrens vdev_disk_buf_t *vdb = (vdev_disk_buf_t *)bp; 358789Sahrens zio_t *zio = vdb->vdb_io; 359789Sahrens 3606976Seschrock /* 3616976Seschrock * The rest of the zio stack only deals with EIO, ECKSUM, and ENXIO. 3626976Seschrock * Rather than teach the rest of the stack about other error 3636976Seschrock * possibilities (EFAULT, etc), we normalize the error value here. 3646976Seschrock */ 3656976Seschrock zio->io_error = (geterror(bp) != 0 ? EIO : 0); 3666976Seschrock 3676976Seschrock if (zio->io_error == 0 && bp->b_resid != 0) 368789Sahrens zio->io_error = EIO; 369789Sahrens 370789Sahrens kmem_free(vdb, sizeof (vdev_disk_buf_t)); 371789Sahrens 3725530Sbonwick zio_interrupt(zio); 373789Sahrens } 374789Sahrens 375789Sahrens static void 3767762SJeff.Bonwick@Sun.COM vdev_disk_ioctl_free(zio_t *zio) 3777762SJeff.Bonwick@Sun.COM { 3787762SJeff.Bonwick@Sun.COM kmem_free(zio->io_vsd, sizeof (struct dk_callback)); 3797762SJeff.Bonwick@Sun.COM } 3807762SJeff.Bonwick@Sun.COM 38110614SJonathan.Adams@Sun.COM static const zio_vsd_ops_t vdev_disk_vsd_ops = { 38210614SJonathan.Adams@Sun.COM vdev_disk_ioctl_free, 38310614SJonathan.Adams@Sun.COM zio_vsd_default_cksum_report 38410614SJonathan.Adams@Sun.COM }; 38510614SJonathan.Adams@Sun.COM 3867762SJeff.Bonwick@Sun.COM static void 387789Sahrens vdev_disk_ioctl_done(void *zio_arg, int error) 388789Sahrens { 389789Sahrens zio_t *zio = zio_arg; 390789Sahrens 391789Sahrens zio->io_error = error; 392789Sahrens 3935530Sbonwick zio_interrupt(zio); 394789Sahrens } 395789Sahrens 3965530Sbonwick static int 397789Sahrens vdev_disk_io_start(zio_t *zio) 398789Sahrens { 399789Sahrens vdev_t *vd = zio->io_vd; 400789Sahrens vdev_disk_t *dvd = vd->vdev_tsd; 401789Sahrens vdev_disk_buf_t *vdb; 4027754SJeff.Bonwick@Sun.COM struct dk_callback *dkc; 403789Sahrens buf_t *bp; 4047754SJeff.Bonwick@Sun.COM int error; 405789Sahrens 406789Sahrens if (zio->io_type == ZIO_TYPE_IOCTL) { 407789Sahrens /* XXPOLICY */ 4085329Sgw25295 if (!vdev_readable(vd)) { 409789Sahrens zio->io_error = ENXIO; 4105530Sbonwick return (ZIO_PIPELINE_CONTINUE); 411789Sahrens } 412789Sahrens 413789Sahrens switch (zio->io_cmd) { 414789Sahrens 415789Sahrens case DKIOCFLUSHWRITECACHE: 416789Sahrens 4172885Sahrens if (zfs_nocacheflush) 4182885Sahrens break; 4192885Sahrens 4201773Seschrock if (vd->vdev_nowritecache) { 4211773Seschrock zio->io_error = ENOTSUP; 4221773Seschrock break; 4231773Seschrock } 4241773Seschrock 4257754SJeff.Bonwick@Sun.COM zio->io_vsd = dkc = kmem_alloc(sizeof (*dkc), KM_SLEEP); 42610614SJonathan.Adams@Sun.COM zio->io_vsd_ops = &vdev_disk_vsd_ops; 4277754SJeff.Bonwick@Sun.COM 4287754SJeff.Bonwick@Sun.COM dkc->dkc_callback = vdev_disk_ioctl_done; 4297754SJeff.Bonwick@Sun.COM dkc->dkc_flag = FLUSH_VOLATILE; 4307754SJeff.Bonwick@Sun.COM dkc->dkc_cookie = zio; 431789Sahrens 432789Sahrens error = ldi_ioctl(dvd->vd_lh, zio->io_cmd, 4337754SJeff.Bonwick@Sun.COM (uintptr_t)dkc, FKIOCTL, kcred, NULL); 434789Sahrens 435789Sahrens if (error == 0) { 436789Sahrens /* 437789Sahrens * The ioctl will be done asychronously, 438789Sahrens * and will call vdev_disk_ioctl_done() 439789Sahrens * upon completion. 440789Sahrens */ 4415530Sbonwick return (ZIO_PIPELINE_STOP); 4425530Sbonwick } 4435530Sbonwick 4445530Sbonwick if (error == ENOTSUP || error == ENOTTY) { 4451773Seschrock /* 4464455Smishra * If we get ENOTSUP or ENOTTY, we know that 4474455Smishra * no future attempts will ever succeed. 4484455Smishra * In this case we set a persistent bit so 4494455Smishra * that we don't bother with the ioctl in the 4504455Smishra * future. 4511773Seschrock */ 4521773Seschrock vd->vdev_nowritecache = B_TRUE; 453789Sahrens } 454789Sahrens zio->io_error = error; 4551773Seschrock 456789Sahrens break; 457789Sahrens 458789Sahrens default: 459789Sahrens zio->io_error = ENOTSUP; 460789Sahrens } 461789Sahrens 4625530Sbonwick return (ZIO_PIPELINE_CONTINUE); 463789Sahrens } 464789Sahrens 465789Sahrens vdb = kmem_alloc(sizeof (vdev_disk_buf_t), KM_SLEEP); 466789Sahrens 467789Sahrens vdb->vdb_io = zio; 468789Sahrens bp = &vdb->vdb_buf; 469789Sahrens 470789Sahrens bioinit(bp); 4717754SJeff.Bonwick@Sun.COM bp->b_flags = B_BUSY | B_NOCACHE | 4729725SEric.Schrock@Sun.COM (zio->io_type == ZIO_TYPE_READ ? B_READ : B_WRITE); 4739725SEric.Schrock@Sun.COM if (!(zio->io_flags & (ZIO_FLAG_IO_RETRY | ZIO_FLAG_TRYHARD))) 4749725SEric.Schrock@Sun.COM bp->b_flags |= B_FAILFAST; 475789Sahrens bp->b_bcount = zio->io_size; 476789Sahrens bp->b_un.b_addr = zio->io_data; 477789Sahrens bp->b_lblkno = lbtodb(zio->io_offset); 478789Sahrens bp->b_bufsize = zio->io_size; 479789Sahrens bp->b_iodone = (int (*)())vdev_disk_io_intr; 480789Sahrens 481789Sahrens /* ldi_strategy() will return non-zero only on programming errors */ 4827754SJeff.Bonwick@Sun.COM VERIFY(ldi_strategy(dvd->vd_lh, bp) == 0); 4835530Sbonwick 4845530Sbonwick return (ZIO_PIPELINE_STOP); 485789Sahrens } 486789Sahrens 4877754SJeff.Bonwick@Sun.COM static void 488789Sahrens vdev_disk_io_done(zio_t *zio) 489789Sahrens { 4907754SJeff.Bonwick@Sun.COM vdev_t *vd = zio->io_vd; 4911544Seschrock 4924451Seschrock /* 4934451Seschrock * If the device returned EIO, then attempt a DKIOCSTATE ioctl to see if 4944451Seschrock * the device has been removed. If this is the case, then we trigger an 4955329Sgw25295 * asynchronous removal of the device. Otherwise, probe the device and 4965369Sgw25295 * make sure it's still accessible. 4974451Seschrock */ 49810575SEric.Schrock@Sun.COM if (zio->io_error == EIO && !vd->vdev_remove_wanted) { 4995329Sgw25295 vdev_disk_t *dvd = vd->vdev_tsd; 5007754SJeff.Bonwick@Sun.COM int state = DKIO_NONE; 5015329Sgw25295 5027754SJeff.Bonwick@Sun.COM if (ldi_ioctl(dvd->vd_lh, DKIOCSTATE, (intptr_t)&state, 5037754SJeff.Bonwick@Sun.COM FKIOCTL, kcred, NULL) == 0 && state != DKIO_INSERTED) { 50410575SEric.Schrock@Sun.COM /* 50510575SEric.Schrock@Sun.COM * We post the resource as soon as possible, instead of 50610575SEric.Schrock@Sun.COM * when the async removal actually happens, because the 50710575SEric.Schrock@Sun.COM * DE is using this information to discard previous I/O 50810575SEric.Schrock@Sun.COM * errors. 50910575SEric.Schrock@Sun.COM */ 51010575SEric.Schrock@Sun.COM zfs_post_remove(zio->io_spa, vd); 5114451Seschrock vd->vdev_remove_wanted = B_TRUE; 5124451Seschrock spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE); 5134451Seschrock } 5144451Seschrock } 515789Sahrens } 516789Sahrens 517789Sahrens vdev_ops_t vdev_disk_ops = { 518789Sahrens vdev_disk_open, 519789Sahrens vdev_disk_close, 520789Sahrens vdev_default_asize, 521789Sahrens vdev_disk_io_start, 522789Sahrens vdev_disk_io_done, 523789Sahrens NULL, 52411958SGeorge.Wilson@Sun.COM vdev_disk_hold, 52511958SGeorge.Wilson@Sun.COM vdev_disk_rele, 526789Sahrens VDEV_TYPE_DISK, /* name of this vdev type */ 527789Sahrens B_TRUE /* leaf vdev */ 528789Sahrens }; 5296423Sgw25295 5306423Sgw25295 /* 5317147Staylor * Given the root disk device devid or pathname, read the label from 5327147Staylor * the device, and construct a configuration nvlist. 5336423Sgw25295 */ 5347539SLin.Ling@Sun.COM int 5357539SLin.Ling@Sun.COM vdev_disk_read_rootlabel(char *devpath, char *devid, nvlist_t **config) 5366423Sgw25295 { 5376423Sgw25295 ldi_handle_t vd_lh; 5386423Sgw25295 vdev_label_t *label; 5396423Sgw25295 uint64_t s, size; 5406423Sgw25295 int l; 5417147Staylor ddi_devid_t tmpdevid; 5427687SLin.Ling@Sun.COM int error = -1; 5437147Staylor char *minor_name; 5446423Sgw25295 5456423Sgw25295 /* 5466423Sgw25295 * Read the device label and build the nvlist. 5476423Sgw25295 */ 5487687SLin.Ling@Sun.COM if (devid != NULL && ddi_devid_str_decode(devid, &tmpdevid, 5497147Staylor &minor_name) == 0) { 5507147Staylor error = ldi_open_by_devid(tmpdevid, minor_name, 5518241SJeff.Bonwick@Sun.COM FREAD, kcred, &vd_lh, zfs_li); 5527147Staylor ddi_devid_free(tmpdevid); 5537147Staylor ddi_devid_str_free(minor_name); 5547147Staylor } 5557147Staylor 5567687SLin.Ling@Sun.COM if (error && (error = ldi_open_by_name(devpath, FREAD, kcred, &vd_lh, 5577687SLin.Ling@Sun.COM zfs_li))) 5587539SLin.Ling@Sun.COM return (error); 5596423Sgw25295 5606673Seschrock if (ldi_get_size(vd_lh, &s)) { 5616673Seschrock (void) ldi_close(vd_lh, FREAD, kcred); 5627539SLin.Ling@Sun.COM return (EIO); 5636673Seschrock } 5646423Sgw25295 5656423Sgw25295 size = P2ALIGN_TYPED(s, sizeof (vdev_label_t), uint64_t); 5666423Sgw25295 label = kmem_alloc(sizeof (vdev_label_t), KM_SLEEP); 5676423Sgw25295 5689616SEric.Taylor@Sun.COM *config = NULL; 5696423Sgw25295 for (l = 0; l < VDEV_LABELS; l++) { 5706423Sgw25295 uint64_t offset, state, txg = 0; 5716423Sgw25295 5726423Sgw25295 /* read vdev label */ 5736423Sgw25295 offset = vdev_label_offset(size, l, 0); 5746423Sgw25295 if (vdev_disk_physio(vd_lh, (caddr_t)label, 5758876SLin.Ling@Sun.COM VDEV_SKIP_SIZE + VDEV_PHYS_SIZE, offset, B_READ) != 0) 5766423Sgw25295 continue; 5776423Sgw25295 5786423Sgw25295 if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist, 5797539SLin.Ling@Sun.COM sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0) { 5807539SLin.Ling@Sun.COM *config = NULL; 5816423Sgw25295 continue; 5826423Sgw25295 } 5836423Sgw25295 5847539SLin.Ling@Sun.COM if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE, 5856423Sgw25295 &state) != 0 || state >= POOL_STATE_DESTROYED) { 5867539SLin.Ling@Sun.COM nvlist_free(*config); 5877539SLin.Ling@Sun.COM *config = NULL; 5886423Sgw25295 continue; 5896423Sgw25295 } 5906423Sgw25295 5917539SLin.Ling@Sun.COM if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG, 5926423Sgw25295 &txg) != 0 || txg == 0) { 5937539SLin.Ling@Sun.COM nvlist_free(*config); 5947539SLin.Ling@Sun.COM *config = NULL; 5956423Sgw25295 continue; 5966423Sgw25295 } 5976423Sgw25295 5986423Sgw25295 break; 5996423Sgw25295 } 6006423Sgw25295 6016423Sgw25295 kmem_free(label, sizeof (vdev_label_t)); 6026673Seschrock (void) ldi_close(vd_lh, FREAD, kcred); 6039616SEric.Taylor@Sun.COM if (*config == NULL) 6049616SEric.Taylor@Sun.COM error = EIDRM; 6056673Seschrock 6067539SLin.Ling@Sun.COM return (error); 6076423Sgw25295 } 608