4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <sys/zfs_context.h>
28 #include <sys/refcount.h>
29 #include <sys/vdev_disk.h>
30 #include <sys/vdev_impl.h>
31 #include <sys/fs/zfs.h>
33 #include <sys/sunldi.h>
34 #include <sys/fm/fs/zfs.h>
37 * Virtual device vector for disks.
40 extern ldi_ident_t zfs_li;
42 typedef struct vdev_disk_buf {
48 vdev_disk_open(vdev_t *vd, uint64_t *psize, uint64_t *ashift)
57 * We must have a pathname, and it must be absolute.
59 if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
60 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
64 dvd = vd->vdev_tsd = kmem_zalloc(sizeof (vdev_disk_t), KM_SLEEP);
67 * When opening a disk device, we want to preserve the user's original
68 * intent. We always want to open the device by the path the user gave
69 * us, even if it is one of multiple paths to the save device. But we
70 * also want to be able to survive disks being removed/recabled.
71 * Therefore the sequence of opening devices is:
73 * 1. Try opening the device by path. For legacy pools without the
74 * 'whole_disk' property, attempt to fix the path by appending 's0'.
76 * 2. If the devid of the device matches the stored value, return
79 * 3. Otherwise, the device may have moved. Try opening the device
80 * by the devid instead.
82 * If the vdev is part of the root pool, we avoid opening it by path.
83 * We do this because there is no /dev path available early in boot,
84 * and if we try to open the device by path at a later point, we can
85 * deadlock when devfsadm attempts to open the underlying backing store
88 if (vd->vdev_devid != NULL) {
89 if (ddi_devid_str_decode(vd->vdev_devid, &dvd->vd_devid,
90 &dvd->vd_minor) != 0) {
91 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
96 error = EINVAL; /* presume failure */
98 if (vd->vdev_path != NULL && !spa_is_root(vd->vdev_spa)) {
101 if (vd->vdev_wholedisk == -1ULL) {
102 size_t len = strlen(vd->vdev_path) + 3;
103 char *buf = kmem_alloc(len, KM_SLEEP);
106 (void) snprintf(buf, len, "%ss0", vd->vdev_path);
108 if (ldi_open_by_name(buf, spa_mode, kcred,
110 spa_strfree(vd->vdev_path);
112 vd->vdev_wholedisk = 1ULL;
113 (void) ldi_close(lh, spa_mode, kcred);
119 error = ldi_open_by_name(vd->vdev_path, spa_mode, kcred,
120 &dvd->vd_lh, zfs_li);
123 * Compare the devid to the stored value.
125 if (error == 0 && vd->vdev_devid != NULL &&
126 ldi_get_devid(dvd->vd_lh, &devid) == 0) {
127 if (ddi_devid_compare(devid, dvd->vd_devid) != 0) {
129 (void) ldi_close(dvd->vd_lh, spa_mode, kcred);
132 ddi_devid_free(devid);
136 * If we succeeded in opening the device, but 'vdev_wholedisk'
137 * is not yet set, then this must be a slice.
139 if (error == 0 && vd->vdev_wholedisk == -1ULL)
140 vd->vdev_wholedisk = 0;
144 * If we were unable to open by path, or the devid check fails, open by
147 if (error != 0 && vd->vdev_devid != NULL)
148 error = ldi_open_by_devid(dvd->vd_devid, dvd->vd_minor,
149 spa_mode, kcred, &dvd->vd_lh, zfs_li);
152 * If all else fails, then try opening by physical path (if available)
153 * or the logical path (if we failed due to the devid check). While not
154 * as reliable as the devid, this will give us something, and the higher
155 * level vdev validation will prevent us from opening the wrong device.
158 if (vd->vdev_physpath != NULL &&
159 (dev = ddi_pathname_to_dev_t(vd->vdev_physpath)) != ENODEV)
160 error = ldi_open_by_dev(&dev, OTYP_BLK, spa_mode,
161 kcred, &dvd->vd_lh, zfs_li);
164 * Note that we don't support the legacy auto-wholedisk support
165 * as above. This hasn't been used in a very long time and we
166 * don't need to propagate its oddities to this edge condition.
168 if (error && vd->vdev_path != NULL &&
169 !spa_is_root(vd->vdev_spa))
170 error = ldi_open_by_name(vd->vdev_path, spa_mode, kcred,
171 &dvd->vd_lh, zfs_li);
175 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
180 * Once a device is opened, verify that the physical device path (if
181 * available) is up to date.
183 if (ldi_get_dev(dvd->vd_lh, &dev) == 0 &&
184 ldi_get_otyp(dvd->vd_lh, &otyp) == 0) {
185 char *physpath, *minorname;
187 physpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
189 if (ddi_dev_pathname(dev, otyp, physpath) == 0 &&
190 ldi_get_minor_name(dvd->vd_lh, &minorname) == 0 &&
191 (vd->vdev_physpath == NULL ||
192 strcmp(vd->vdev_physpath, physpath) != 0)) {
193 if (vd->vdev_physpath)
194 spa_strfree(vd->vdev_physpath);
195 (void) strlcat(physpath, ":", MAXPATHLEN);
196 (void) strlcat(physpath, minorname, MAXPATHLEN);
197 vd->vdev_physpath = spa_strdup(physpath);
200 kmem_free(minorname, strlen(minorname) + 1);
201 kmem_free(physpath, MAXPATHLEN);
205 * Determine the actual size of the device.
207 if (ldi_get_size(dvd->vd_lh, psize) != 0) {
208 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
213 * If we own the whole disk, try to enable disk write caching.
214 * We ignore errors because it's OK if we can't do it.
216 if (vd->vdev_wholedisk == 1) {
218 (void) ldi_ioctl(dvd->vd_lh, DKIOCSETWCE, (intptr_t)&wce,
219 FKIOCTL, kcred, NULL);
223 * Determine the device's minimum transfer size.
224 * If the ioctl isn't supported, assume DEV_BSIZE.
226 if (ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFO, (intptr_t)&dkm,
227 FKIOCTL, kcred, NULL) != 0)
228 dkm.dki_lbsize = DEV_BSIZE;
230 *ashift = highbit(MAX(dkm.dki_lbsize, SPA_MINBLOCKSIZE)) - 1;
233 * Clear the nowritecache bit, so that on a vdev_reopen() we will
236 vd->vdev_nowritecache = B_FALSE;
242 vdev_disk_close(vdev_t *vd)
244 vdev_disk_t *dvd = vd->vdev_tsd;
249 if (dvd->vd_minor != NULL)
250 ddi_devid_str_free(dvd->vd_minor);
252 if (dvd->vd_devid != NULL)
253 ddi_devid_free(dvd->vd_devid);
255 if (dvd->vd_lh != NULL)
256 (void) ldi_close(dvd->vd_lh, spa_mode, kcred);
258 kmem_free(dvd, sizeof (vdev_disk_t));
263 vdev_disk_physio(ldi_handle_t vd_lh, caddr_t data, size_t size,
264 uint64_t offset, int flags)
272 ASSERT(flags & B_READ || flags & B_WRITE);
274 bp = getrbuf(KM_SLEEP);
275 bp->b_flags = flags | B_BUSY | B_NOCACHE | B_FAILFAST;
277 bp->b_un.b_addr = (void *)data;
278 bp->b_lblkno = lbtodb(offset);
279 bp->b_bufsize = size;
281 error = ldi_strategy(vd_lh, bp);
283 if ((error = biowait(bp)) == 0 && bp->b_resid != 0)
291 vdev_disk_io_intr(buf_t *bp)
293 vdev_disk_buf_t *vdb = (vdev_disk_buf_t *)bp;
294 zio_t *zio = vdb->vdb_io;
297 * The rest of the zio stack only deals with EIO, ECKSUM, and ENXIO.
298 * Rather than teach the rest of the stack about other error
299 * possibilities (EFAULT, etc), we normalize the error value here.
301 zio->io_error = (geterror(bp) != 0 ? EIO : 0);
303 if (zio->io_error == 0 && bp->b_resid != 0)
306 kmem_free(vdb, sizeof (vdev_disk_buf_t));
312 vdev_disk_ioctl_free(zio_t *zio)
314 kmem_free(zio->io_vsd, sizeof (struct dk_callback));
318 vdev_disk_ioctl_done(void *zio_arg, int error)
320 zio_t *zio = zio_arg;
322 zio->io_error = error;
328 vdev_disk_io_start(zio_t *zio)
330 vdev_t *vd = zio->io_vd;
331 vdev_disk_t *dvd = vd->vdev_tsd;
332 vdev_disk_buf_t *vdb;
333 struct dk_callback *dkc;
337 if (zio->io_type == ZIO_TYPE_IOCTL) {
339 if (!vdev_readable(vd)) {
340 zio->io_error = ENXIO;
341 return (ZIO_PIPELINE_CONTINUE);
344 switch (zio->io_cmd) {
346 case DKIOCFLUSHWRITECACHE:
348 if (zfs_nocacheflush)
351 if (vd->vdev_nowritecache) {
352 zio->io_error = ENOTSUP;
356 zio->io_vsd = dkc = kmem_alloc(sizeof (*dkc), KM_SLEEP);
357 zio->io_vsd_free = vdev_disk_ioctl_free;
359 dkc->dkc_callback = vdev_disk_ioctl_done;
360 dkc->dkc_flag = FLUSH_VOLATILE;
361 dkc->dkc_cookie = zio;
363 error = ldi_ioctl(dvd->vd_lh, zio->io_cmd,
364 (uintptr_t)dkc, FKIOCTL, kcred, NULL);
368 * The ioctl will be done asychronously,
369 * and will call vdev_disk_ioctl_done()
372 return (ZIO_PIPELINE_STOP);
375 if (error == ENOTSUP || error == ENOTTY) {
377 * If we get ENOTSUP or ENOTTY, we know that
378 * no future attempts will ever succeed.
379 * In this case we set a persistent bit so
380 * that we don't bother with the ioctl in the
383 vd->vdev_nowritecache = B_TRUE;
385 zio->io_error = error;
390 zio->io_error = ENOTSUP;
393 return (ZIO_PIPELINE_CONTINUE);
396 vdb = kmem_alloc(sizeof (vdev_disk_buf_t), KM_SLEEP);
402 bp->b_flags = B_BUSY | B_NOCACHE |
403 (zio->io_type == ZIO_TYPE_READ ? B_READ : B_WRITE) |
404 ((zio->io_flags & ZIO_FLAG_IO_RETRY) ? 0 : B_FAILFAST);
405 bp->b_bcount = zio->io_size;
406 bp->b_un.b_addr = zio->io_data;
407 bp->b_lblkno = lbtodb(zio->io_offset);
408 bp->b_bufsize = zio->io_size;
409 bp->b_iodone = (int (*)())vdev_disk_io_intr;
411 /* ldi_strategy() will return non-zero only on programming errors */
412 VERIFY(ldi_strategy(dvd->vd_lh, bp) == 0);
414 return (ZIO_PIPELINE_STOP);
418 vdev_disk_io_done(zio_t *zio)
420 vdev_t *vd = zio->io_vd;
423 * If the device returned EIO, then attempt a DKIOCSTATE ioctl to see if
424 * the device has been removed. If this is the case, then we trigger an
425 * asynchronous removal of the device. Otherwise, probe the device and
426 * make sure it's still accessible.
428 if (zio->io_error == EIO) {
429 vdev_disk_t *dvd = vd->vdev_tsd;
430 int state = DKIO_NONE;
432 if (ldi_ioctl(dvd->vd_lh, DKIOCSTATE, (intptr_t)&state,
433 FKIOCTL, kcred, NULL) == 0 && state != DKIO_INSERTED) {
434 vd->vdev_remove_wanted = B_TRUE;
435 spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE);
440 vdev_ops_t vdev_disk_ops = {
447 VDEV_TYPE_DISK, /* name of this vdev type */
448 B_TRUE /* leaf vdev */
452 * Given the root disk device devid or pathname, read the label from
453 * the device, and construct a configuration nvlist.
456 vdev_disk_read_rootlabel(char *devpath, char *devid, nvlist_t **config)
462 ddi_devid_t tmpdevid;
467 * Read the device label and build the nvlist.
469 if (devid != NULL && ddi_devid_str_decode(devid, &tmpdevid,
471 error = ldi_open_by_devid(tmpdevid, minor_name,
472 spa_mode, kcred, &vd_lh, zfs_li);
473 ddi_devid_free(tmpdevid);
474 ddi_devid_str_free(minor_name);
477 if (error && (error = ldi_open_by_name(devpath, FREAD, kcred, &vd_lh,
481 if (ldi_get_size(vd_lh, &s)) {
482 (void) ldi_close(vd_lh, FREAD, kcred);
486 size = P2ALIGN_TYPED(s, sizeof (vdev_label_t), uint64_t);
487 label = kmem_alloc(sizeof (vdev_label_t), KM_SLEEP);
489 for (l = 0; l < VDEV_LABELS; l++) {
490 uint64_t offset, state, txg = 0;
492 /* read vdev label */
493 offset = vdev_label_offset(size, l, 0);
494 if (vdev_disk_physio(vd_lh, (caddr_t)label,
495 VDEV_SKIP_SIZE + VDEV_BOOT_HEADER_SIZE +
496 VDEV_PHYS_SIZE, offset, B_READ) != 0)
499 if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist,
500 sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0) {
505 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
506 &state) != 0 || state >= POOL_STATE_DESTROYED) {
507 nvlist_free(*config);
512 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
513 &txg) != 0 || txg == 0) {
514 nvlist_free(*config);
522 kmem_free(label, sizeof (vdev_label_t));
523 (void) ldi_close(vd_lh, FREAD, kcred);