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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012 by Delphix. All rights reserved.
26 #include <sys/zfs_context.h>
27 #include <sys/spa_impl.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/efi_partition.h>
35 #include <sys/fm/fs/zfs.h>
38 * Virtual device vector for disks.
41 extern ldi_ident_t zfs_li
;
44 vdev_disk_hold(vdev_t
*vd
)
49 ASSERT(spa_config_held(vd
->vdev_spa
, SCL_STATE
, RW_WRITER
));
52 * We must have a pathname, and it must be absolute.
54 if (vd
->vdev_path
== NULL
|| vd
->vdev_path
[0] != '/')
58 * Only prefetch path and devid info if the device has
61 if (vd
->vdev_tsd
!= NULL
)
64 if (vd
->vdev_wholedisk
== -1ULL) {
65 size_t len
= strlen(vd
->vdev_path
) + 3;
66 char *buf
= kmem_alloc(len
, KM_SLEEP
);
68 (void) snprintf(buf
, len
, "%ss0", vd
->vdev_path
);
70 (void) ldi_vp_from_name(buf
, &vd
->vdev_name_vp
);
74 if (vd
->vdev_name_vp
== NULL
)
75 (void) ldi_vp_from_name(vd
->vdev_path
, &vd
->vdev_name_vp
);
77 if (vd
->vdev_devid
!= NULL
&&
78 ddi_devid_str_decode(vd
->vdev_devid
, &devid
, &minor
) == 0) {
79 (void) ldi_vp_from_devid(devid
, minor
, &vd
->vdev_devid_vp
);
80 ddi_devid_str_free(minor
);
81 ddi_devid_free(devid
);
86 vdev_disk_rele(vdev_t
*vd
)
88 ASSERT(spa_config_held(vd
->vdev_spa
, SCL_STATE
, RW_WRITER
));
90 if (vd
->vdev_name_vp
) {
91 VN_RELE_ASYNC(vd
->vdev_name_vp
,
92 dsl_pool_vnrele_taskq(vd
->vdev_spa
->spa_dsl_pool
));
93 vd
->vdev_name_vp
= NULL
;
95 if (vd
->vdev_devid_vp
) {
96 VN_RELE_ASYNC(vd
->vdev_devid_vp
,
97 dsl_pool_vnrele_taskq(vd
->vdev_spa
->spa_dsl_pool
));
98 vd
->vdev_devid_vp
= NULL
;
103 vdev_disk_get_space(vdev_t
*vd
, uint64_t capacity
, uint_t blksz
)
105 ASSERT(vd
->vdev_wholedisk
);
107 vdev_disk_t
*dvd
= vd
->vdev_tsd
;
110 uint64_t avail_space
= 0;
111 int efisize
= EFI_LABEL_SIZE
* 2;
113 dk_ioc
.dki_data
= kmem_alloc(efisize
, KM_SLEEP
);
115 dk_ioc
.dki_length
= efisize
;
116 dk_ioc
.dki_data_64
= (uint64_t)(uintptr_t)dk_ioc
.dki_data
;
117 efi
= dk_ioc
.dki_data
;
119 if (ldi_ioctl(dvd
->vd_lh
, DKIOCGETEFI
, (intptr_t)&dk_ioc
,
120 FKIOCTL
, kcred
, NULL
) == 0) {
121 uint64_t efi_altern_lba
= LE_64(efi
->efi_gpt_AlternateLBA
);
123 zfs_dbgmsg("vdev %s, capacity %llu, altern lba %llu",
124 vd
->vdev_path
, capacity
, efi_altern_lba
);
125 if (capacity
> efi_altern_lba
)
126 avail_space
= (capacity
- efi_altern_lba
) * blksz
;
128 kmem_free(dk_ioc
.dki_data
, efisize
);
129 return (avail_space
);
133 vdev_disk_open(vdev_t
*vd
, uint64_t *psize
, uint64_t *max_psize
,
136 spa_t
*spa
= vd
->vdev_spa
;
138 struct dk_minfo_ext dkmext
;
144 * We must have a pathname, and it must be absolute.
146 if (vd
->vdev_path
== NULL
|| vd
->vdev_path
[0] != '/') {
147 vd
->vdev_stat
.vs_aux
= VDEV_AUX_BAD_LABEL
;
152 * Reopen the device if it's not currently open. Otherwise,
153 * just update the physical size of the device.
155 if (vd
->vdev_tsd
!= NULL
) {
156 ASSERT(vd
->vdev_reopening
);
161 dvd
= vd
->vdev_tsd
= kmem_zalloc(sizeof (vdev_disk_t
), KM_SLEEP
);
164 * When opening a disk device, we want to preserve the user's original
165 * intent. We always want to open the device by the path the user gave
166 * us, even if it is one of multiple paths to the same device. But we
167 * also want to be able to survive disks being removed/recabled.
168 * Therefore the sequence of opening devices is:
170 * 1. Try opening the device by path. For legacy pools without the
171 * 'whole_disk' property, attempt to fix the path by appending 's0'.
173 * 2. If the devid of the device matches the stored value, return
176 * 3. Otherwise, the device may have moved. Try opening the device
177 * by the devid instead.
179 if (vd
->vdev_devid
!= NULL
) {
180 if (ddi_devid_str_decode(vd
->vdev_devid
, &dvd
->vd_devid
,
181 &dvd
->vd_minor
) != 0) {
182 vd
->vdev_stat
.vs_aux
= VDEV_AUX_BAD_LABEL
;
187 error
= EINVAL
; /* presume failure */
189 if (vd
->vdev_path
!= NULL
) {
192 if (vd
->vdev_wholedisk
== -1ULL) {
193 size_t len
= strlen(vd
->vdev_path
) + 3;
194 char *buf
= kmem_alloc(len
, KM_SLEEP
);
197 (void) snprintf(buf
, len
, "%ss0", vd
->vdev_path
);
199 if (ldi_open_by_name(buf
, spa_mode(spa
), kcred
,
201 spa_strfree(vd
->vdev_path
);
203 vd
->vdev_wholedisk
= 1ULL;
204 (void) ldi_close(lh
, spa_mode(spa
), kcred
);
210 error
= ldi_open_by_name(vd
->vdev_path
, spa_mode(spa
), kcred
,
211 &dvd
->vd_lh
, zfs_li
);
214 * Compare the devid to the stored value.
216 if (error
== 0 && vd
->vdev_devid
!= NULL
&&
217 ldi_get_devid(dvd
->vd_lh
, &devid
) == 0) {
218 if (ddi_devid_compare(devid
, dvd
->vd_devid
) != 0) {
220 (void) ldi_close(dvd
->vd_lh
, spa_mode(spa
),
224 ddi_devid_free(devid
);
228 * If we succeeded in opening the device, but 'vdev_wholedisk'
229 * is not yet set, then this must be a slice.
231 if (error
== 0 && vd
->vdev_wholedisk
== -1ULL)
232 vd
->vdev_wholedisk
= 0;
236 * If we were unable to open by path, or the devid check fails, open by
239 if (error
!= 0 && vd
->vdev_devid
!= NULL
)
240 error
= ldi_open_by_devid(dvd
->vd_devid
, dvd
->vd_minor
,
241 spa_mode(spa
), kcred
, &dvd
->vd_lh
, zfs_li
);
244 * If all else fails, then try opening by physical path (if available)
245 * or the logical path (if we failed due to the devid check). While not
246 * as reliable as the devid, this will give us something, and the higher
247 * level vdev validation will prevent us from opening the wrong device.
250 if (vd
->vdev_physpath
!= NULL
&&
251 (dev
= ddi_pathname_to_dev_t(vd
->vdev_physpath
)) != NODEV
)
252 error
= ldi_open_by_dev(&dev
, OTYP_BLK
, spa_mode(spa
),
253 kcred
, &dvd
->vd_lh
, zfs_li
);
256 * Note that we don't support the legacy auto-wholedisk support
257 * as above. This hasn't been used in a very long time and we
258 * don't need to propagate its oddities to this edge condition.
260 if (error
&& vd
->vdev_path
!= NULL
)
261 error
= ldi_open_by_name(vd
->vdev_path
, spa_mode(spa
),
262 kcred
, &dvd
->vd_lh
, zfs_li
);
266 vd
->vdev_stat
.vs_aux
= VDEV_AUX_OPEN_FAILED
;
271 * Once a device is opened, verify that the physical device path (if
272 * available) is up to date.
274 if (ldi_get_dev(dvd
->vd_lh
, &dev
) == 0 &&
275 ldi_get_otyp(dvd
->vd_lh
, &otyp
) == 0) {
276 char *physpath
, *minorname
;
278 physpath
= kmem_alloc(MAXPATHLEN
, KM_SLEEP
);
280 if (ddi_dev_pathname(dev
, otyp
, physpath
) == 0 &&
281 ldi_get_minor_name(dvd
->vd_lh
, &minorname
) == 0 &&
282 (vd
->vdev_physpath
== NULL
||
283 strcmp(vd
->vdev_physpath
, physpath
) != 0)) {
284 if (vd
->vdev_physpath
)
285 spa_strfree(vd
->vdev_physpath
);
286 (void) strlcat(physpath
, ":", MAXPATHLEN
);
287 (void) strlcat(physpath
, minorname
, MAXPATHLEN
);
288 vd
->vdev_physpath
= spa_strdup(physpath
);
291 kmem_free(minorname
, strlen(minorname
) + 1);
292 kmem_free(physpath
, MAXPATHLEN
);
297 * Determine the actual size of the device.
299 if (ldi_get_size(dvd
->vd_lh
, psize
) != 0) {
300 vd
->vdev_stat
.vs_aux
= VDEV_AUX_OPEN_FAILED
;
305 * Determine the device's minimum transfer size.
306 * If the ioctl isn't supported, assume DEV_BSIZE.
308 if (ldi_ioctl(dvd
->vd_lh
, DKIOCGMEDIAINFOEXT
, (intptr_t)&dkmext
,
309 FKIOCTL
, kcred
, NULL
) != 0)
310 dkmext
.dki_pbsize
= DEV_BSIZE
;
312 *ashift
= highbit(MAX(dkmext
.dki_pbsize
, SPA_MINBLOCKSIZE
)) - 1;
314 if (vd
->vdev_wholedisk
== 1) {
315 uint64_t capacity
= dkmext
.dki_capacity
- 1;
316 uint64_t blksz
= dkmext
.dki_lbsize
;
320 * If we own the whole disk, try to enable disk write caching.
321 * We ignore errors because it's OK if we can't do it.
323 (void) ldi_ioctl(dvd
->vd_lh
, DKIOCSETWCE
, (intptr_t)&wce
,
324 FKIOCTL
, kcred
, NULL
);
326 *max_psize
= *psize
+ vdev_disk_get_space(vd
, capacity
, blksz
);
327 zfs_dbgmsg("capacity change: vdev %s, psize %llu, "
328 "max_psize %llu", vd
->vdev_path
, *psize
, *max_psize
);
334 * Clear the nowritecache bit, so that on a vdev_reopen() we will
337 vd
->vdev_nowritecache
= B_FALSE
;
343 vdev_disk_close(vdev_t
*vd
)
345 vdev_disk_t
*dvd
= vd
->vdev_tsd
;
347 if (vd
->vdev_reopening
|| dvd
== NULL
)
350 if (dvd
->vd_minor
!= NULL
)
351 ddi_devid_str_free(dvd
->vd_minor
);
353 if (dvd
->vd_devid
!= NULL
)
354 ddi_devid_free(dvd
->vd_devid
);
356 if (dvd
->vd_lh
!= NULL
)
357 (void) ldi_close(dvd
->vd_lh
, spa_mode(vd
->vdev_spa
), kcred
);
359 vd
->vdev_delayed_close
= B_FALSE
;
360 kmem_free(dvd
, sizeof (vdev_disk_t
));
365 vdev_disk_physio(ldi_handle_t vd_lh
, caddr_t data
, size_t size
,
366 uint64_t offset
, int flags
)
374 ASSERT(flags
& B_READ
|| flags
& B_WRITE
);
376 bp
= getrbuf(KM_SLEEP
);
377 bp
->b_flags
= flags
| B_BUSY
| B_NOCACHE
| B_FAILFAST
;
379 bp
->b_un
.b_addr
= (void *)data
;
380 bp
->b_lblkno
= lbtodb(offset
);
381 bp
->b_bufsize
= size
;
383 error
= ldi_strategy(vd_lh
, bp
);
385 if ((error
= biowait(bp
)) == 0 && bp
->b_resid
!= 0)
393 vdev_disk_io_intr(buf_t
*bp
)
395 vdev_buf_t
*vb
= (vdev_buf_t
*)bp
;
396 zio_t
*zio
= vb
->vb_io
;
399 * The rest of the zio stack only deals with EIO, ECKSUM, and ENXIO.
400 * Rather than teach the rest of the stack about other error
401 * possibilities (EFAULT, etc), we normalize the error value here.
403 zio
->io_error
= (geterror(bp
) != 0 ? EIO
: 0);
405 if (zio
->io_error
== 0 && bp
->b_resid
!= 0)
408 kmem_free(vb
, sizeof (vdev_buf_t
));
414 vdev_disk_ioctl_free(zio_t
*zio
)
416 kmem_free(zio
->io_vsd
, sizeof (struct dk_callback
));
419 static const zio_vsd_ops_t vdev_disk_vsd_ops
= {
420 vdev_disk_ioctl_free
,
421 zio_vsd_default_cksum_report
425 vdev_disk_ioctl_done(void *zio_arg
, int error
)
427 zio_t
*zio
= zio_arg
;
429 zio
->io_error
= error
;
435 vdev_disk_io_start(zio_t
*zio
)
437 vdev_t
*vd
= zio
->io_vd
;
438 vdev_disk_t
*dvd
= vd
->vdev_tsd
;
440 struct dk_callback
*dkc
;
444 if (zio
->io_type
== ZIO_TYPE_IOCTL
) {
446 if (!vdev_readable(vd
)) {
447 zio
->io_error
= ENXIO
;
448 return (ZIO_PIPELINE_CONTINUE
);
451 switch (zio
->io_cmd
) {
453 case DKIOCFLUSHWRITECACHE
:
455 if (zfs_nocacheflush
)
458 if (vd
->vdev_nowritecache
) {
459 zio
->io_error
= ENOTSUP
;
463 zio
->io_vsd
= dkc
= kmem_alloc(sizeof (*dkc
), KM_SLEEP
);
464 zio
->io_vsd_ops
= &vdev_disk_vsd_ops
;
466 dkc
->dkc_callback
= vdev_disk_ioctl_done
;
467 dkc
->dkc_flag
= FLUSH_VOLATILE
;
468 dkc
->dkc_cookie
= zio
;
470 error
= ldi_ioctl(dvd
->vd_lh
, zio
->io_cmd
,
471 (uintptr_t)dkc
, FKIOCTL
, kcred
, NULL
);
475 * The ioctl will be done asychronously,
476 * and will call vdev_disk_ioctl_done()
479 return (ZIO_PIPELINE_STOP
);
482 if (error
== ENOTSUP
|| error
== ENOTTY
) {
484 * If we get ENOTSUP or ENOTTY, we know that
485 * no future attempts will ever succeed.
486 * In this case we set a persistent bit so
487 * that we don't bother with the ioctl in the
490 vd
->vdev_nowritecache
= B_TRUE
;
492 zio
->io_error
= error
;
497 zio
->io_error
= ENOTSUP
;
500 return (ZIO_PIPELINE_CONTINUE
);
503 vb
= kmem_alloc(sizeof (vdev_buf_t
), KM_SLEEP
);
509 bp
->b_flags
= B_BUSY
| B_NOCACHE
|
510 (zio
->io_type
== ZIO_TYPE_READ
? B_READ
: B_WRITE
);
511 if (!(zio
->io_flags
& (ZIO_FLAG_IO_RETRY
| ZIO_FLAG_TRYHARD
)))
512 bp
->b_flags
|= B_FAILFAST
;
513 bp
->b_bcount
= zio
->io_size
;
514 bp
->b_un
.b_addr
= zio
->io_data
;
515 bp
->b_lblkno
= lbtodb(zio
->io_offset
);
516 bp
->b_bufsize
= zio
->io_size
;
517 bp
->b_iodone
= (int (*)())vdev_disk_io_intr
;
519 /* ldi_strategy() will return non-zero only on programming errors */
520 VERIFY(ldi_strategy(dvd
->vd_lh
, bp
) == 0);
522 return (ZIO_PIPELINE_STOP
);
526 vdev_disk_io_done(zio_t
*zio
)
528 vdev_t
*vd
= zio
->io_vd
;
531 * If the device returned EIO, then attempt a DKIOCSTATE ioctl to see if
532 * the device has been removed. If this is the case, then we trigger an
533 * asynchronous removal of the device. Otherwise, probe the device and
534 * make sure it's still accessible.
536 if (zio
->io_error
== EIO
&& !vd
->vdev_remove_wanted
) {
537 vdev_disk_t
*dvd
= vd
->vdev_tsd
;
538 int state
= DKIO_NONE
;
540 if (ldi_ioctl(dvd
->vd_lh
, DKIOCSTATE
, (intptr_t)&state
,
541 FKIOCTL
, kcred
, NULL
) == 0 && state
!= DKIO_INSERTED
) {
543 * We post the resource as soon as possible, instead of
544 * when the async removal actually happens, because the
545 * DE is using this information to discard previous I/O
548 zfs_post_remove(zio
->io_spa
, vd
);
549 vd
->vdev_remove_wanted
= B_TRUE
;
550 spa_async_request(zio
->io_spa
, SPA_ASYNC_REMOVE
);
551 } else if (!vd
->vdev_delayed_close
) {
552 vd
->vdev_delayed_close
= B_TRUE
;
557 vdev_ops_t vdev_disk_ops
= {
566 VDEV_TYPE_DISK
, /* name of this vdev type */
567 B_TRUE
/* leaf vdev */
571 * Given the root disk device devid or pathname, read the label from
572 * the device, and construct a configuration nvlist.
575 vdev_disk_read_rootlabel(char *devpath
, char *devid
, nvlist_t
**config
)
581 ddi_devid_t tmpdevid
;
586 * Read the device label and build the nvlist.
588 if (devid
!= NULL
&& ddi_devid_str_decode(devid
, &tmpdevid
,
590 error
= ldi_open_by_devid(tmpdevid
, minor_name
,
591 FREAD
, kcred
, &vd_lh
, zfs_li
);
592 ddi_devid_free(tmpdevid
);
593 ddi_devid_str_free(minor_name
);
596 if (error
&& (error
= ldi_open_by_name(devpath
, FREAD
, kcred
, &vd_lh
,
600 if (ldi_get_size(vd_lh
, &s
)) {
601 (void) ldi_close(vd_lh
, FREAD
, kcred
);
605 size
= P2ALIGN_TYPED(s
, sizeof (vdev_label_t
), uint64_t);
606 label
= kmem_alloc(sizeof (vdev_label_t
), KM_SLEEP
);
609 for (l
= 0; l
< VDEV_LABELS
; l
++) {
610 uint64_t offset
, state
, txg
= 0;
612 /* read vdev label */
613 offset
= vdev_label_offset(size
, l
, 0);
614 if (vdev_disk_physio(vd_lh
, (caddr_t
)label
,
615 VDEV_SKIP_SIZE
+ VDEV_PHYS_SIZE
, offset
, B_READ
) != 0)
618 if (nvlist_unpack(label
->vl_vdev_phys
.vp_nvlist
,
619 sizeof (label
->vl_vdev_phys
.vp_nvlist
), config
, 0) != 0) {
624 if (nvlist_lookup_uint64(*config
, ZPOOL_CONFIG_POOL_STATE
,
625 &state
) != 0 || state
>= POOL_STATE_DESTROYED
) {
626 nvlist_free(*config
);
631 if (nvlist_lookup_uint64(*config
, ZPOOL_CONFIG_POOL_TXG
,
632 &txg
) != 0 || txg
== 0) {
633 nvlist_free(*config
);
641 kmem_free(label
, sizeof (vdev_label_t
));
642 (void) ldi_close(vd_lh
, FREAD
, kcred
);