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 2009 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)
50 spa_t *spa = vd->vdev_spa;
58 * We must have a pathname, and it must be absolute.
60 if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
61 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
65 dvd = vd->vdev_tsd = kmem_zalloc(sizeof (vdev_disk_t), KM_SLEEP);
68 * When opening a disk device, we want to preserve the user's original
69 * intent. We always want to open the device by the path the user gave
70 * us, even if it is one of multiple paths to the save device. But we
71 * also want to be able to survive disks being removed/recabled.
72 * Therefore the sequence of opening devices is:
74 * 1. Try opening the device by path. For legacy pools without the
75 * 'whole_disk' property, attempt to fix the path by appending 's0'.
77 * 2. If the devid of the device matches the stored value, return
80 * 3. Otherwise, the device may have moved. Try opening the device
81 * by the devid instead.
83 * If the vdev is part of the root pool, we avoid opening it by path.
84 * We do this because there is no /dev path available early in boot,
85 * and if we try to open the device by path at a later point, we can
86 * deadlock when devfsadm attempts to open the underlying backing store
89 if (vd->vdev_devid != NULL) {
90 if (ddi_devid_str_decode(vd->vdev_devid, &dvd->vd_devid,
91 &dvd->vd_minor) != 0) {
92 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
97 error = EINVAL; /* presume failure */
99 if (vd->vdev_path != NULL && !spa_is_root(spa)) {
102 if (vd->vdev_wholedisk == -1ULL) {
103 size_t len = strlen(vd->vdev_path) + 3;
104 char *buf = kmem_alloc(len, KM_SLEEP);
107 (void) snprintf(buf, len, "%ss0", vd->vdev_path);
109 if (ldi_open_by_name(buf, spa_mode(spa), kcred,
111 spa_strfree(vd->vdev_path);
113 vd->vdev_wholedisk = 1ULL;
114 (void) ldi_close(lh, spa_mode(spa), kcred);
120 error = ldi_open_by_name(vd->vdev_path, spa_mode(spa), kcred,
121 &dvd->vd_lh, zfs_li);
124 * Compare the devid to the stored value.
126 if (error == 0 && vd->vdev_devid != NULL &&
127 ldi_get_devid(dvd->vd_lh, &devid) == 0) {
128 if (ddi_devid_compare(devid, dvd->vd_devid) != 0) {
130 (void) ldi_close(dvd->vd_lh, spa_mode(spa),
134 ddi_devid_free(devid);
138 * If we succeeded in opening the device, but 'vdev_wholedisk'
139 * is not yet set, then this must be a slice.
141 if (error == 0 && vd->vdev_wholedisk == -1ULL)
142 vd->vdev_wholedisk = 0;
146 * If we were unable to open by path, or the devid check fails, open by
149 if (error != 0 && vd->vdev_devid != NULL)
150 error = ldi_open_by_devid(dvd->vd_devid, dvd->vd_minor,
151 spa_mode(spa), kcred, &dvd->vd_lh, zfs_li);
154 * If all else fails, then try opening by physical path (if available)
155 * or the logical path (if we failed due to the devid check). While not
156 * as reliable as the devid, this will give us something, and the higher
157 * level vdev validation will prevent us from opening the wrong device.
160 if (vd->vdev_physpath != NULL &&
161 (dev = ddi_pathname_to_dev_t(vd->vdev_physpath)) != NODEV)
162 error = ldi_open_by_dev(&dev, OTYP_BLK, spa_mode(spa),
163 kcred, &dvd->vd_lh, zfs_li);
166 * Note that we don't support the legacy auto-wholedisk support
167 * as above. This hasn't been used in a very long time and we
168 * don't need to propagate its oddities to this edge condition.
170 if (error && vd->vdev_path != NULL && !spa_is_root(spa))
171 error = ldi_open_by_name(vd->vdev_path, spa_mode(spa),
172 kcred, &dvd->vd_lh, zfs_li);
176 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
181 * Once a device is opened, verify that the physical device path (if
182 * available) is up to date.
184 if (ldi_get_dev(dvd->vd_lh, &dev) == 0 &&
185 ldi_get_otyp(dvd->vd_lh, &otyp) == 0) {
186 char *physpath, *minorname;
188 physpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
190 if (ddi_dev_pathname(dev, otyp, physpath) == 0 &&
191 ldi_get_minor_name(dvd->vd_lh, &minorname) == 0 &&
192 (vd->vdev_physpath == NULL ||
193 strcmp(vd->vdev_physpath, physpath) != 0)) {
194 if (vd->vdev_physpath)
195 spa_strfree(vd->vdev_physpath);
196 (void) strlcat(physpath, ":", MAXPATHLEN);
197 (void) strlcat(physpath, minorname, MAXPATHLEN);
198 vd->vdev_physpath = spa_strdup(physpath);
201 kmem_free(minorname, strlen(minorname) + 1);
202 kmem_free(physpath, MAXPATHLEN);
206 * Determine the actual size of the device.
208 if (ldi_get_size(dvd->vd_lh, psize) != 0) {
209 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
214 * If we own the whole disk, try to enable disk write caching.
215 * We ignore errors because it's OK if we can't do it.
217 if (vd->vdev_wholedisk == 1) {
219 (void) ldi_ioctl(dvd->vd_lh, DKIOCSETWCE, (intptr_t)&wce,
220 FKIOCTL, kcred, NULL);
224 * Determine the device's minimum transfer size.
225 * If the ioctl isn't supported, assume DEV_BSIZE.
227 if (ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFO, (intptr_t)&dkm,
228 FKIOCTL, kcred, NULL) != 0)
229 dkm.dki_lbsize = DEV_BSIZE;
231 *ashift = highbit(MAX(dkm.dki_lbsize, SPA_MINBLOCKSIZE)) - 1;
234 * Clear the nowritecache bit, so that on a vdev_reopen() we will
237 vd->vdev_nowritecache = B_FALSE;
243 vdev_disk_close(vdev_t *vd)
245 vdev_disk_t *dvd = vd->vdev_tsd;
250 if (dvd->vd_minor != NULL)
251 ddi_devid_str_free(dvd->vd_minor);
253 if (dvd->vd_devid != NULL)
254 ddi_devid_free(dvd->vd_devid);
256 if (dvd->vd_lh != NULL)
257 (void) ldi_close(dvd->vd_lh, spa_mode(vd->vdev_spa), kcred);
259 kmem_free(dvd, sizeof (vdev_disk_t));
264 vdev_disk_physio(ldi_handle_t vd_lh, caddr_t data, size_t size,
265 uint64_t offset, int flags)
273 ASSERT(flags & B_READ || flags & B_WRITE);
275 bp = getrbuf(KM_SLEEP);
276 bp->b_flags = flags | B_BUSY | B_NOCACHE | B_FAILFAST;
278 bp->b_un.b_addr = (void *)data;
279 bp->b_lblkno = lbtodb(offset);
280 bp->b_bufsize = size;
282 error = ldi_strategy(vd_lh, bp);
284 if ((error = biowait(bp)) == 0 && bp->b_resid != 0)
292 vdev_disk_io_intr(buf_t *bp)
294 vdev_disk_buf_t *vdb = (vdev_disk_buf_t *)bp;
295 zio_t *zio = vdb->vdb_io;
298 * The rest of the zio stack only deals with EIO, ECKSUM, and ENXIO.
299 * Rather than teach the rest of the stack about other error
300 * possibilities (EFAULT, etc), we normalize the error value here.
302 zio->io_error = (geterror(bp) != 0 ? EIO : 0);
304 if (zio->io_error == 0 && bp->b_resid != 0)
307 kmem_free(vdb, sizeof (vdev_disk_buf_t));
313 vdev_disk_ioctl_free(zio_t *zio)
315 kmem_free(zio->io_vsd, sizeof (struct dk_callback));
319 vdev_disk_ioctl_done(void *zio_arg, int error)
321 zio_t *zio = zio_arg;
323 zio->io_error = error;
329 vdev_disk_io_start(zio_t *zio)
331 vdev_t *vd = zio->io_vd;
332 vdev_disk_t *dvd = vd->vdev_tsd;
333 vdev_disk_buf_t *vdb;
334 struct dk_callback *dkc;
338 if (zio->io_type == ZIO_TYPE_IOCTL) {
340 if (!vdev_readable(vd)) {
341 zio->io_error = ENXIO;
342 return (ZIO_PIPELINE_CONTINUE);
345 switch (zio->io_cmd) {
347 case DKIOCFLUSHWRITECACHE:
349 if (zfs_nocacheflush)
352 if (vd->vdev_nowritecache) {
353 zio->io_error = ENOTSUP;
357 zio->io_vsd = dkc = kmem_alloc(sizeof (*dkc), KM_SLEEP);
358 zio->io_vsd_free = vdev_disk_ioctl_free;
360 dkc->dkc_callback = vdev_disk_ioctl_done;
361 dkc->dkc_flag = FLUSH_VOLATILE;
362 dkc->dkc_cookie = zio;
364 error = ldi_ioctl(dvd->vd_lh, zio->io_cmd,
365 (uintptr_t)dkc, FKIOCTL, kcred, NULL);
369 * The ioctl will be done asychronously,
370 * and will call vdev_disk_ioctl_done()
373 return (ZIO_PIPELINE_STOP);
376 if (error == ENOTSUP || error == ENOTTY) {
378 * If we get ENOTSUP or ENOTTY, we know that
379 * no future attempts will ever succeed.
380 * In this case we set a persistent bit so
381 * that we don't bother with the ioctl in the
384 vd->vdev_nowritecache = B_TRUE;
386 zio->io_error = error;
391 zio->io_error = ENOTSUP;
394 return (ZIO_PIPELINE_CONTINUE);
397 vdb = kmem_alloc(sizeof (vdev_disk_buf_t), KM_SLEEP);
403 bp->b_flags = B_BUSY | B_NOCACHE |
404 (zio->io_type == ZIO_TYPE_READ ? B_READ : B_WRITE) |
405 ((zio->io_flags & ZIO_FLAG_IO_RETRY) ? 0 : B_FAILFAST);
406 bp->b_bcount = zio->io_size;
407 bp->b_un.b_addr = zio->io_data;
408 bp->b_lblkno = lbtodb(zio->io_offset);
409 bp->b_bufsize = zio->io_size;
410 bp->b_iodone = (int (*)())vdev_disk_io_intr;
412 /* ldi_strategy() will return non-zero only on programming errors */
413 VERIFY(ldi_strategy(dvd->vd_lh, bp) == 0);
415 return (ZIO_PIPELINE_STOP);
419 vdev_disk_io_done(zio_t *zio)
421 vdev_t *vd = zio->io_vd;
424 * If the device returned EIO, then attempt a DKIOCSTATE ioctl to see if
425 * the device has been removed. If this is the case, then we trigger an
426 * asynchronous removal of the device. Otherwise, probe the device and
427 * make sure it's still accessible.
429 if (zio->io_error == EIO) {
430 vdev_disk_t *dvd = vd->vdev_tsd;
431 int state = DKIO_NONE;
433 if (ldi_ioctl(dvd->vd_lh, DKIOCSTATE, (intptr_t)&state,
434 FKIOCTL, kcred, NULL) == 0 && state != DKIO_INSERTED) {
435 vd->vdev_remove_wanted = B_TRUE;
436 spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE);
441 vdev_ops_t vdev_disk_ops = {
448 VDEV_TYPE_DISK, /* name of this vdev type */
449 B_TRUE /* leaf vdev */
453 * Given the root disk device devid or pathname, read the label from
454 * the device, and construct a configuration nvlist.
457 vdev_disk_read_rootlabel(char *devpath, char *devid, nvlist_t **config)
463 ddi_devid_t tmpdevid;
468 * Read the device label and build the nvlist.
470 if (devid != NULL && ddi_devid_str_decode(devid, &tmpdevid,
472 error = ldi_open_by_devid(tmpdevid, minor_name,
473 FREAD, kcred, &vd_lh, zfs_li);
474 ddi_devid_free(tmpdevid);
475 ddi_devid_str_free(minor_name);
478 if (error && (error = ldi_open_by_name(devpath, FREAD, kcred, &vd_lh,
482 if (ldi_get_size(vd_lh, &s)) {
483 (void) ldi_close(vd_lh, FREAD, kcred);
487 size = P2ALIGN_TYPED(s, sizeof (vdev_label_t), uint64_t);
488 label = kmem_alloc(sizeof (vdev_label_t), KM_SLEEP);
490 for (l = 0; l < VDEV_LABELS; l++) {
491 uint64_t offset, state, txg = 0;
493 /* read vdev label */
494 offset = vdev_label_offset(size, l, 0);
495 if (vdev_disk_physio(vd_lh, (caddr_t)label,
496 VDEV_SKIP_SIZE + 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);