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 /*
2212208SBo.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
vdev_disk_hold(vdev_t * vd)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
vdev_disk_rele(vdev_t * vd)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
vdev_disk_open(vdev_t * vd,uint64_t * psize,uint64_t * ashift)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;
11012208SBo.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 */
29012208SBo.Zhou@Sun.COM if (ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFOEXT, (intptr_t)&dkmext,
2911732Sbonwick FKIOCTL, kcred, NULL) != 0)
29212208SBo.Zhou@Sun.COM dkmext.dki_pbsize = DEV_BSIZE;
2931489Swebaker
29412208SBo.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
vdev_disk_close(vdev_t * vd)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
322*12247SGeorge.Wilson@Sun.COM vd->vdev_delayed_close = B_FALSE;
323789Sahrens kmem_free(dvd, sizeof (vdev_disk_t));
324789Sahrens vd->vdev_tsd = NULL;
325789Sahrens }
326789Sahrens
3276423Sgw25295 int
vdev_disk_physio(ldi_handle_t vd_lh,caddr_t data,size_t size,uint64_t offset,int flags)3286423Sgw25295 vdev_disk_physio(ldi_handle_t vd_lh, caddr_t data, size_t size,
3296423Sgw25295 uint64_t offset, int flags)
3306423Sgw25295 {
3316423Sgw25295 buf_t *bp;
3326423Sgw25295 int error = 0;
3336423Sgw25295
3346423Sgw25295 if (vd_lh == NULL)
3356423Sgw25295 return (EINVAL);
3366423Sgw25295
3376423Sgw25295 ASSERT(flags & B_READ || flags & B_WRITE);
3386423Sgw25295
3396423Sgw25295 bp = getrbuf(KM_SLEEP);
3406423Sgw25295 bp->b_flags = flags | B_BUSY | B_NOCACHE | B_FAILFAST;
3416423Sgw25295 bp->b_bcount = size;
3426423Sgw25295 bp->b_un.b_addr = (void *)data;
3436423Sgw25295 bp->b_lblkno = lbtodb(offset);
3446423Sgw25295 bp->b_bufsize = size;
3456423Sgw25295
3466423Sgw25295 error = ldi_strategy(vd_lh, bp);
3476423Sgw25295 ASSERT(error == 0);
3486423Sgw25295 if ((error = biowait(bp)) == 0 && bp->b_resid != 0)
3496423Sgw25295 error = EIO;
3506423Sgw25295 freerbuf(bp);
3516423Sgw25295
3526423Sgw25295 return (error);
3536423Sgw25295 }
3546423Sgw25295
355789Sahrens static void
vdev_disk_io_intr(buf_t * bp)356789Sahrens vdev_disk_io_intr(buf_t *bp)
357789Sahrens {
358789Sahrens vdev_disk_buf_t *vdb = (vdev_disk_buf_t *)bp;
359789Sahrens zio_t *zio = vdb->vdb_io;
360789Sahrens
3616976Seschrock /*
3626976Seschrock * The rest of the zio stack only deals with EIO, ECKSUM, and ENXIO.
3636976Seschrock * Rather than teach the rest of the stack about other error
3646976Seschrock * possibilities (EFAULT, etc), we normalize the error value here.
3656976Seschrock */
3666976Seschrock zio->io_error = (geterror(bp) != 0 ? EIO : 0);
3676976Seschrock
3686976Seschrock if (zio->io_error == 0 && bp->b_resid != 0)
369789Sahrens zio->io_error = EIO;
370789Sahrens
371789Sahrens kmem_free(vdb, sizeof (vdev_disk_buf_t));
372789Sahrens
3735530Sbonwick zio_interrupt(zio);
374789Sahrens }
375789Sahrens
376789Sahrens static void
vdev_disk_ioctl_free(zio_t * zio)3777762SJeff.Bonwick@Sun.COM vdev_disk_ioctl_free(zio_t *zio)
3787762SJeff.Bonwick@Sun.COM {
3797762SJeff.Bonwick@Sun.COM kmem_free(zio->io_vsd, sizeof (struct dk_callback));
3807762SJeff.Bonwick@Sun.COM }
3817762SJeff.Bonwick@Sun.COM
38210614SJonathan.Adams@Sun.COM static const zio_vsd_ops_t vdev_disk_vsd_ops = {
38310614SJonathan.Adams@Sun.COM vdev_disk_ioctl_free,
38410614SJonathan.Adams@Sun.COM zio_vsd_default_cksum_report
38510614SJonathan.Adams@Sun.COM };
38610614SJonathan.Adams@Sun.COM
3877762SJeff.Bonwick@Sun.COM static void
vdev_disk_ioctl_done(void * zio_arg,int error)388789Sahrens vdev_disk_ioctl_done(void *zio_arg, int error)
389789Sahrens {
390789Sahrens zio_t *zio = zio_arg;
391789Sahrens
392789Sahrens zio->io_error = error;
393789Sahrens
3945530Sbonwick zio_interrupt(zio);
395789Sahrens }
396789Sahrens
3975530Sbonwick static int
vdev_disk_io_start(zio_t * zio)398789Sahrens vdev_disk_io_start(zio_t *zio)
399789Sahrens {
400789Sahrens vdev_t *vd = zio->io_vd;
401789Sahrens vdev_disk_t *dvd = vd->vdev_tsd;
402789Sahrens vdev_disk_buf_t *vdb;
4037754SJeff.Bonwick@Sun.COM struct dk_callback *dkc;
404789Sahrens buf_t *bp;
4057754SJeff.Bonwick@Sun.COM int error;
406789Sahrens
407789Sahrens if (zio->io_type == ZIO_TYPE_IOCTL) {
408789Sahrens /* XXPOLICY */
4095329Sgw25295 if (!vdev_readable(vd)) {
410789Sahrens zio->io_error = ENXIO;
4115530Sbonwick return (ZIO_PIPELINE_CONTINUE);
412789Sahrens }
413789Sahrens
414789Sahrens switch (zio->io_cmd) {
415789Sahrens
416789Sahrens case DKIOCFLUSHWRITECACHE:
417789Sahrens
4182885Sahrens if (zfs_nocacheflush)
4192885Sahrens break;
4202885Sahrens
4211773Seschrock if (vd->vdev_nowritecache) {
4221773Seschrock zio->io_error = ENOTSUP;
4231773Seschrock break;
4241773Seschrock }
4251773Seschrock
4267754SJeff.Bonwick@Sun.COM zio->io_vsd = dkc = kmem_alloc(sizeof (*dkc), KM_SLEEP);
42710614SJonathan.Adams@Sun.COM zio->io_vsd_ops = &vdev_disk_vsd_ops;
4287754SJeff.Bonwick@Sun.COM
4297754SJeff.Bonwick@Sun.COM dkc->dkc_callback = vdev_disk_ioctl_done;
4307754SJeff.Bonwick@Sun.COM dkc->dkc_flag = FLUSH_VOLATILE;
4317754SJeff.Bonwick@Sun.COM dkc->dkc_cookie = zio;
432789Sahrens
433789Sahrens error = ldi_ioctl(dvd->vd_lh, zio->io_cmd,
4347754SJeff.Bonwick@Sun.COM (uintptr_t)dkc, FKIOCTL, kcred, NULL);
435789Sahrens
436789Sahrens if (error == 0) {
437789Sahrens /*
438789Sahrens * The ioctl will be done asychronously,
439789Sahrens * and will call vdev_disk_ioctl_done()
440789Sahrens * upon completion.
441789Sahrens */
4425530Sbonwick return (ZIO_PIPELINE_STOP);
4435530Sbonwick }
4445530Sbonwick
4455530Sbonwick if (error == ENOTSUP || error == ENOTTY) {
4461773Seschrock /*
4474455Smishra * If we get ENOTSUP or ENOTTY, we know that
4484455Smishra * no future attempts will ever succeed.
4494455Smishra * In this case we set a persistent bit so
4504455Smishra * that we don't bother with the ioctl in the
4514455Smishra * future.
4521773Seschrock */
4531773Seschrock vd->vdev_nowritecache = B_TRUE;
454789Sahrens }
455789Sahrens zio->io_error = error;
4561773Seschrock
457789Sahrens break;
458789Sahrens
459789Sahrens default:
460789Sahrens zio->io_error = ENOTSUP;
461789Sahrens }
462789Sahrens
4635530Sbonwick return (ZIO_PIPELINE_CONTINUE);
464789Sahrens }
465789Sahrens
466789Sahrens vdb = kmem_alloc(sizeof (vdev_disk_buf_t), KM_SLEEP);
467789Sahrens
468789Sahrens vdb->vdb_io = zio;
469789Sahrens bp = &vdb->vdb_buf;
470789Sahrens
471789Sahrens bioinit(bp);
4727754SJeff.Bonwick@Sun.COM bp->b_flags = B_BUSY | B_NOCACHE |
4739725SEric.Schrock@Sun.COM (zio->io_type == ZIO_TYPE_READ ? B_READ : B_WRITE);
4749725SEric.Schrock@Sun.COM if (!(zio->io_flags & (ZIO_FLAG_IO_RETRY | ZIO_FLAG_TRYHARD)))
4759725SEric.Schrock@Sun.COM bp->b_flags |= B_FAILFAST;
476789Sahrens bp->b_bcount = zio->io_size;
477789Sahrens bp->b_un.b_addr = zio->io_data;
478789Sahrens bp->b_lblkno = lbtodb(zio->io_offset);
479789Sahrens bp->b_bufsize = zio->io_size;
480789Sahrens bp->b_iodone = (int (*)())vdev_disk_io_intr;
481789Sahrens
482789Sahrens /* ldi_strategy() will return non-zero only on programming errors */
4837754SJeff.Bonwick@Sun.COM VERIFY(ldi_strategy(dvd->vd_lh, bp) == 0);
4845530Sbonwick
4855530Sbonwick return (ZIO_PIPELINE_STOP);
486789Sahrens }
487789Sahrens
4887754SJeff.Bonwick@Sun.COM static void
vdev_disk_io_done(zio_t * zio)489789Sahrens vdev_disk_io_done(zio_t *zio)
490789Sahrens {
4917754SJeff.Bonwick@Sun.COM vdev_t *vd = zio->io_vd;
4921544Seschrock
4934451Seschrock /*
4944451Seschrock * If the device returned EIO, then attempt a DKIOCSTATE ioctl to see if
4954451Seschrock * the device has been removed. If this is the case, then we trigger an
4965329Sgw25295 * asynchronous removal of the device. Otherwise, probe the device and
4975369Sgw25295 * make sure it's still accessible.
4984451Seschrock */
49910575SEric.Schrock@Sun.COM if (zio->io_error == EIO && !vd->vdev_remove_wanted) {
5005329Sgw25295 vdev_disk_t *dvd = vd->vdev_tsd;
5017754SJeff.Bonwick@Sun.COM int state = DKIO_NONE;
5025329Sgw25295
5037754SJeff.Bonwick@Sun.COM if (ldi_ioctl(dvd->vd_lh, DKIOCSTATE, (intptr_t)&state,
5047754SJeff.Bonwick@Sun.COM FKIOCTL, kcred, NULL) == 0 && state != DKIO_INSERTED) {
50510575SEric.Schrock@Sun.COM /*
50610575SEric.Schrock@Sun.COM * We post the resource as soon as possible, instead of
50710575SEric.Schrock@Sun.COM * when the async removal actually happens, because the
50810575SEric.Schrock@Sun.COM * DE is using this information to discard previous I/O
50910575SEric.Schrock@Sun.COM * errors.
51010575SEric.Schrock@Sun.COM */
51110575SEric.Schrock@Sun.COM zfs_post_remove(zio->io_spa, vd);
5124451Seschrock vd->vdev_remove_wanted = B_TRUE;
5134451Seschrock spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE);
514*12247SGeorge.Wilson@Sun.COM } else if (!vd->vdev_delayed_close) {
515*12247SGeorge.Wilson@Sun.COM vd->vdev_delayed_close = B_TRUE;
5164451Seschrock }
5174451Seschrock }
518789Sahrens }
519789Sahrens
520789Sahrens vdev_ops_t vdev_disk_ops = {
521789Sahrens vdev_disk_open,
522789Sahrens vdev_disk_close,
523789Sahrens vdev_default_asize,
524789Sahrens vdev_disk_io_start,
525789Sahrens vdev_disk_io_done,
526789Sahrens NULL,
52711958SGeorge.Wilson@Sun.COM vdev_disk_hold,
52811958SGeorge.Wilson@Sun.COM vdev_disk_rele,
529789Sahrens VDEV_TYPE_DISK, /* name of this vdev type */
530789Sahrens B_TRUE /* leaf vdev */
531789Sahrens };
5326423Sgw25295
5336423Sgw25295 /*
5347147Staylor * Given the root disk device devid or pathname, read the label from
5357147Staylor * the device, and construct a configuration nvlist.
5366423Sgw25295 */
5377539SLin.Ling@Sun.COM int
vdev_disk_read_rootlabel(char * devpath,char * devid,nvlist_t ** config)5387539SLin.Ling@Sun.COM vdev_disk_read_rootlabel(char *devpath, char *devid, nvlist_t **config)
5396423Sgw25295 {
5406423Sgw25295 ldi_handle_t vd_lh;
5416423Sgw25295 vdev_label_t *label;
5426423Sgw25295 uint64_t s, size;
5436423Sgw25295 int l;
5447147Staylor ddi_devid_t tmpdevid;
5457687SLin.Ling@Sun.COM int error = -1;
5467147Staylor char *minor_name;
5476423Sgw25295
5486423Sgw25295 /*
5496423Sgw25295 * Read the device label and build the nvlist.
5506423Sgw25295 */
5517687SLin.Ling@Sun.COM if (devid != NULL && ddi_devid_str_decode(devid, &tmpdevid,
5527147Staylor &minor_name) == 0) {
5537147Staylor error = ldi_open_by_devid(tmpdevid, minor_name,
5548241SJeff.Bonwick@Sun.COM FREAD, kcred, &vd_lh, zfs_li);
5557147Staylor ddi_devid_free(tmpdevid);
5567147Staylor ddi_devid_str_free(minor_name);
5577147Staylor }
5587147Staylor
5597687SLin.Ling@Sun.COM if (error && (error = ldi_open_by_name(devpath, FREAD, kcred, &vd_lh,
5607687SLin.Ling@Sun.COM zfs_li)))
5617539SLin.Ling@Sun.COM return (error);
5626423Sgw25295
5636673Seschrock if (ldi_get_size(vd_lh, &s)) {
5646673Seschrock (void) ldi_close(vd_lh, FREAD, kcred);
5657539SLin.Ling@Sun.COM return (EIO);
5666673Seschrock }
5676423Sgw25295
5686423Sgw25295 size = P2ALIGN_TYPED(s, sizeof (vdev_label_t), uint64_t);
5696423Sgw25295 label = kmem_alloc(sizeof (vdev_label_t), KM_SLEEP);
5706423Sgw25295
5719616SEric.Taylor@Sun.COM *config = NULL;
5726423Sgw25295 for (l = 0; l < VDEV_LABELS; l++) {
5736423Sgw25295 uint64_t offset, state, txg = 0;
5746423Sgw25295
5756423Sgw25295 /* read vdev label */
5766423Sgw25295 offset = vdev_label_offset(size, l, 0);
5776423Sgw25295 if (vdev_disk_physio(vd_lh, (caddr_t)label,
5788876SLin.Ling@Sun.COM VDEV_SKIP_SIZE + VDEV_PHYS_SIZE, offset, B_READ) != 0)
5796423Sgw25295 continue;
5806423Sgw25295
5816423Sgw25295 if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist,
5827539SLin.Ling@Sun.COM sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0) {
5837539SLin.Ling@Sun.COM *config = NULL;
5846423Sgw25295 continue;
5856423Sgw25295 }
5866423Sgw25295
5877539SLin.Ling@Sun.COM if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
5886423Sgw25295 &state) != 0 || state >= POOL_STATE_DESTROYED) {
5897539SLin.Ling@Sun.COM nvlist_free(*config);
5907539SLin.Ling@Sun.COM *config = NULL;
5916423Sgw25295 continue;
5926423Sgw25295 }
5936423Sgw25295
5947539SLin.Ling@Sun.COM if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
5956423Sgw25295 &txg) != 0 || txg == 0) {
5967539SLin.Ling@Sun.COM nvlist_free(*config);
5977539SLin.Ling@Sun.COM *config = NULL;
5986423Sgw25295 continue;
5996423Sgw25295 }
6006423Sgw25295
6016423Sgw25295 break;
6026423Sgw25295 }
6036423Sgw25295
6046423Sgw25295 kmem_free(label, sizeof (vdev_label_t));
6056673Seschrock (void) ldi_close(vd_lh, FREAD, kcred);
6069616SEric.Taylor@Sun.COM if (*config == NULL)
6079616SEric.Taylor@Sun.COM error = EIDRM;
6086673Seschrock
6097539SLin.Ling@Sun.COM return (error);
6106423Sgw25295 }
611