1 /*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
25 * Copyright (c) 2013 Joyent, Inc. All rights reserved.
26 * Copyright 2017 Nexenta Systems, Inc.
27 */
28
29 #include <sys/zfs_context.h>
30 #include <sys/spa_impl.h>
31 #include <sys/refcount.h>
32 #include <sys/vdev_disk.h>
33 #include <sys/vdev_impl.h>
34 #include <sys/abd.h>
35 #include <sys/fs/zfs.h>
36 #include <sys/zio.h>
37 #include <sys/sunldi.h>
38 #include <sys/efi_partition.h>
39 #include <sys/fm/fs/zfs.h>
40
41 /*
42 * Virtual device vector for disks.
43 */
44
45 extern ldi_ident_t zfs_li;
46
47 static void vdev_disk_close_impl(vdev_t *, boolean_t);
48
49 typedef struct vdev_disk_ldi_cb {
50 list_node_t lcb_next;
51 ldi_callback_id_t lcb_id;
52 } vdev_disk_ldi_cb_t;
53
54 static vdev_disk_t *
55 vdev_disk_alloc(void)
56 {
57 vdev_disk_t *dvd;
58
59 dvd = kmem_zalloc(sizeof (vdev_disk_t), KM_SLEEP);
60 /*
61 * Create the LDI event callback list.
62 */
63 list_create(&dvd->vd_ldi_cbs, sizeof (vdev_disk_ldi_cb_t),
64 offsetof(vdev_disk_ldi_cb_t, lcb_next));
65 return (dvd);
66 }
67
68 static void
69 vdev_disk_free_locked(vdev_t *vd)
70 {
71 vdev_disk_ldi_cb_t *lcb;
72 vdev_disk_t *dvd = vd->vdev_tsd;
73
74 ASSERT(rw_lock_held(&vd->vdev_tsd_lock));
75
76 if (dvd == NULL)
77 return;
78
79 /*
80 * We have already closed the LDI handle. Clean up the LDI event
81 * callbacks and free vd->vdev_tsd.
82 */
83 vd->vdev_tsd = NULL;
84 while ((lcb = list_head(&dvd->vd_ldi_cbs)) != NULL) {
85 list_remove(&dvd->vd_ldi_cbs, lcb);
86 (void) ldi_ev_remove_callbacks(lcb->lcb_id);
87 kmem_free(lcb, sizeof (vdev_disk_ldi_cb_t));
88 }
89 list_destroy(&dvd->vd_ldi_cbs);
90 kmem_free(dvd, sizeof (vdev_disk_t));
91 }
92
93 static void
94 vdev_disk_free(vdev_t *vd)
95 {
96 rw_enter(&vd->vdev_tsd_lock, RW_WRITER);
97 vdev_disk_free_locked(vd);
98 rw_exit(&vd->vdev_tsd_lock);
99 }
100
101 /* ARGSUSED */
102 static int
103 vdev_disk_off_notify(ldi_handle_t lh, ldi_ev_cookie_t ecookie, void *arg,
104 void *ev_data)
105 {
106 vdev_t *vd = (vdev_t *)arg;
107
108 /*
109 * Ignore events other than offline.
110 */
111 if (strcmp(ldi_ev_get_type(ecookie), LDI_EV_OFFLINE) != 0)
112 return (LDI_EV_SUCCESS);
113
114 /*
115 * All LDI handles must be closed for the state change to succeed, so
116 * call on vdev_disk_close() to do this.
117 *
118 * We inform vdev_disk_close that it is being called from offline
119 * notify context so it will defer cleanup of LDI event callbacks and
120 * freeing of vd->vdev_tsd to the offline finalize or a reopen.
121 */
122 vdev_disk_close_impl(vd, B_TRUE);
123
124 /*
125 * Now that the device is closed, request that the spa_async_thread
126 * mark the device as REMOVED and notify FMA of the removal.
127 */
128 zfs_post_remove(vd->vdev_spa, vd);
129 vd->vdev_remove_wanted = B_TRUE;
130 spa_async_request(vd->vdev_spa, SPA_ASYNC_REMOVE);
131
132 return (LDI_EV_SUCCESS);
133 }
134
135 /* ARGSUSED */
136 static void
137 vdev_disk_off_finalize(ldi_handle_t lh, ldi_ev_cookie_t ecookie,
138 int ldi_result, void *arg, void *ev_data)
139 {
140 vdev_t *vd = (vdev_t *)arg;
141
142 /*
143 * Ignore events other than offline.
144 */
145 if (strcmp(ldi_ev_get_type(ecookie), LDI_EV_OFFLINE) != 0)
146 return;
147
148 /*
149 * We have already closed the LDI handle in notify.
150 * Clean up the LDI event callbacks and free vd->vdev_tsd.
151 */
152 vdev_disk_free(vd);
153
154 /*
155 * Request that the vdev be reopened if the offline state change was
156 * unsuccessful.
157 */
158 if (ldi_result != LDI_EV_SUCCESS) {
159 vd->vdev_probe_wanted = B_TRUE;
160 spa_async_request(vd->vdev_spa, SPA_ASYNC_PROBE);
161 }
162 }
163
164 static ldi_ev_callback_t vdev_disk_off_callb = {
165 .cb_vers = LDI_EV_CB_VERS,
166 .cb_notify = vdev_disk_off_notify,
167 .cb_finalize = vdev_disk_off_finalize
168 };
169
170 /* ARGSUSED */
171 static void
172 vdev_disk_dgrd_finalize(ldi_handle_t lh, ldi_ev_cookie_t ecookie,
173 int ldi_result, void *arg, void *ev_data)
174 {
175 vdev_t *vd = (vdev_t *)arg;
176
177 /*
178 * Ignore events other than degrade.
179 */
180 if (strcmp(ldi_ev_get_type(ecookie), LDI_EV_DEGRADE) != 0)
181 return;
182
183 /*
184 * Degrade events always succeed. Mark the vdev as degraded.
185 * This status is purely informative for the user.
186 */
187 (void) vdev_degrade(vd->vdev_spa, vd->vdev_guid, 0);
188 }
189
190 static ldi_ev_callback_t vdev_disk_dgrd_callb = {
191 .cb_vers = LDI_EV_CB_VERS,
192 .cb_notify = NULL,
193 .cb_finalize = vdev_disk_dgrd_finalize
194 };
195
196 static void
197 vdev_disk_hold(vdev_t *vd)
198 {
199 ddi_devid_t devid;
200 char *minor;
201
202 ASSERT(spa_config_held(vd->vdev_spa, SCL_STATE, RW_WRITER));
203
204 /*
205 * We must have a pathname, and it must be absolute.
206 */
207 if (vd->vdev_path == NULL || vd->vdev_path[0] != '/')
208 return;
209
210 /*
211 * Only prefetch path and devid info if the device has
212 * never been opened.
213 */
214 if (vd->vdev_tsd != NULL)
215 return;
216
217 if (vd->vdev_wholedisk == -1ULL) {
218 size_t len = strlen(vd->vdev_path) + 3;
219 char *buf = kmem_alloc(len, KM_SLEEP);
220
221 (void) snprintf(buf, len, "%ss0", vd->vdev_path);
222
223 (void) ldi_vp_from_name(buf, &vd->vdev_name_vp);
224 kmem_free(buf, len);
225 }
226
227 if (vd->vdev_name_vp == NULL)
228 (void) ldi_vp_from_name(vd->vdev_path, &vd->vdev_name_vp);
229
230 if (vd->vdev_devid != NULL &&
231 ddi_devid_str_decode(vd->vdev_devid, &devid, &minor) == 0) {
232 (void) ldi_vp_from_devid(devid, minor, &vd->vdev_devid_vp);
233 ddi_devid_str_free(minor);
234 ddi_devid_free(devid);
235 }
236 }
237
238 static void
239 vdev_disk_rele(vdev_t *vd)
240 {
241 ASSERT(spa_config_held(vd->vdev_spa, SCL_STATE, RW_WRITER));
242
243 if (vd->vdev_name_vp) {
244 VN_RELE_ASYNC(vd->vdev_name_vp,
245 dsl_pool_vnrele_taskq(vd->vdev_spa->spa_dsl_pool));
246 vd->vdev_name_vp = NULL;
247 }
248 if (vd->vdev_devid_vp) {
249 VN_RELE_ASYNC(vd->vdev_devid_vp,
250 dsl_pool_vnrele_taskq(vd->vdev_spa->spa_dsl_pool));
251 vd->vdev_devid_vp = NULL;
252 }
253 }
254
255 static int
256 vdev_disk_open(vdev_t *vd, uint64_t *psize, uint64_t *max_psize,
257 uint64_t *ashift)
258 {
259 spa_t *spa = vd->vdev_spa;
260 vdev_disk_t *dvd;
261 ldi_ev_cookie_t ecookie;
262 vdev_disk_ldi_cb_t *lcb;
263 union {
264 struct dk_minfo_ext ude;
265 struct dk_minfo ud;
266 } dks;
267 struct dk_minfo_ext *dkmext = &dks.ude;
268 struct dk_minfo *dkm = &dks.ud;
269 int error;
270 dev_t dev;
271 int otyp, vdev_ssd;
272 boolean_t validate_devid = B_FALSE;
273 ddi_devid_t devid;
274 uint64_t capacity = 0, blksz = 0, pbsize;
275
276 /*
277 * We must have a pathname, and it must be absolute.
278 */
279 if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
280 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
281 return (SET_ERROR(EINVAL));
282 }
283 rw_enter(&vd->vdev_tsd_lock, RW_WRITER);
284 dvd = vd->vdev_tsd;
285 /*
286 * Reopen the device if it's not currently open. Otherwise,
287 * just update the physical size of the device.
288 */
289 if (dvd != NULL) {
290 ASSERT(vd->vdev_reopening);
291 /*
292 * Here vd_lh is protected by vdev_tsd_lock
293 */
294 ASSERT(dvd->vd_lh != NULL);
295 /* This should not happen, but let's be safe */
296 if (dvd->vd_lh == NULL) {
297 /* What are we going to do here??? */
298 rw_exit(&vd->vdev_tsd_lock);
299 return (SET_ERROR(ENXIO));
300 }
301 goto skip_open;
302 }
303 /*
304 * Create dvd to be used as vd->vdev_tsd.
305 */
306 vd->vdev_tsd = dvd = vdev_disk_alloc();
307
308 /*
309 * When opening a disk device, we want to preserve the user's original
310 * intent. We always want to open the device by the path the user gave
311 * us, even if it is one of multiple paths to the same device. But we
312 * also want to be able to survive disks being removed/recabled.
313 * Therefore the sequence of opening devices is:
314 *
315 * 1. Try opening the device by path. For legacy pools without the
316 * 'whole_disk' property, attempt to fix the path by appending 's0'.
317 *
318 * 2. If the devid of the device matches the stored value, return
319 * success.
320 *
321 * 3. Otherwise, the device may have moved. Try opening the device
322 * by the devid instead.
323 */
324 if (vd->vdev_devid != NULL) {
325 if (ddi_devid_str_decode(vd->vdev_devid, &dvd->vd_devid,
326 &dvd->vd_minor) != 0) {
327 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
328 vdev_disk_free_locked(vd);
329 rw_exit(&vd->vdev_tsd_lock);
330 return (SET_ERROR(EINVAL));
331 }
332 }
333
334 error = EINVAL; /* presume failure */
335
336 if (vd->vdev_path != NULL) {
337
338 if (vd->vdev_wholedisk == -1ULL) {
339 size_t len = strlen(vd->vdev_path) + 3;
340 char *buf = kmem_alloc(len, KM_SLEEP);
341
342 (void) snprintf(buf, len, "%ss0", vd->vdev_path);
343
344 error = ldi_open_by_name(buf, spa_mode(spa), kcred,
345 &dvd->vd_lh, zfs_li);
346 if (error == 0) {
347 spa_strfree(vd->vdev_path);
348 vd->vdev_path = buf;
349 vd->vdev_wholedisk = 1ULL;
350 } else {
351 kmem_free(buf, len);
352 }
353 }
354
355 /*
356 * If we have not yet opened the device, try to open it by the
357 * specified path.
358 */
359 if (error != 0) {
360 error = ldi_open_by_name(vd->vdev_path, spa_mode(spa),
361 kcred, &dvd->vd_lh, zfs_li);
362 }
363
364 /*
365 * Compare the devid to the stored value.
366 */
367 if (error == 0 && vd->vdev_devid != NULL &&
368 ldi_get_devid(dvd->vd_lh, &devid) == 0) {
369 if (ddi_devid_compare(devid, dvd->vd_devid) != 0) {
370 error = SET_ERROR(EINVAL);
371 (void) ldi_close(dvd->vd_lh, spa_mode(spa),
372 kcred);
373 dvd->vd_lh = NULL;
374 }
375 ddi_devid_free(devid);
376 }
377
378 /*
379 * If we succeeded in opening the device, but 'vdev_wholedisk'
380 * is not yet set, then this must be a slice.
381 */
382 if (error == 0 && vd->vdev_wholedisk == -1ULL)
383 vd->vdev_wholedisk = 0;
384 }
385
386 /*
387 * If we were unable to open by path, or the devid check fails, open by
388 * devid instead.
389 */
390 if (error != 0 && vd->vdev_devid != NULL) {
391 error = ldi_open_by_devid(dvd->vd_devid, dvd->vd_minor,
392 spa_mode(spa), kcred, &dvd->vd_lh, zfs_li);
393 }
394
395 /*
396 * If all else fails, then try opening by physical path (if available)
397 * or the logical path (if we failed due to the devid check). While not
398 * as reliable as the devid, this will give us something, and the higher
399 * level vdev validation will prevent us from opening the wrong device.
400 */
401 if (error) {
402 if (vd->vdev_devid != NULL)
403 validate_devid = B_TRUE;
404
405 if (vd->vdev_physpath != NULL &&
406 (dev = ddi_pathname_to_dev_t(vd->vdev_physpath)) != NODEV)
407 error = ldi_open_by_dev(&dev, OTYP_BLK, spa_mode(spa),
408 kcred, &dvd->vd_lh, zfs_li);
409
410 /*
411 * Note that we don't support the legacy auto-wholedisk support
412 * as above. This hasn't been used in a very long time and we
413 * don't need to propagate its oddities to this edge condition.
414 */
415 if (error && vd->vdev_path != NULL)
416 error = ldi_open_by_name(vd->vdev_path, spa_mode(spa),
417 kcred, &dvd->vd_lh, zfs_li);
418 }
419
420 if (error) {
421 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
422 vdev_disk_free_locked(vd);
423 rw_exit(&vd->vdev_tsd_lock);
424 return (error);
425 }
426
427 /*
428 * Now that the device has been successfully opened, update the devid
429 * if necessary.
430 */
431 if (validate_devid && spa_writeable(spa) &&
432 ldi_get_devid(dvd->vd_lh, &devid) == 0) {
433 if (ddi_devid_compare(devid, dvd->vd_devid) != 0) {
434 char *vd_devid;
435
436 vd_devid = ddi_devid_str_encode(devid, dvd->vd_minor);
437 zfs_dbgmsg("vdev %s: update devid from %s, "
438 "to %s", vd->vdev_path, vd->vdev_devid, vd_devid);
439 spa_strfree(vd->vdev_devid);
440 vd->vdev_devid = spa_strdup(vd_devid);
441 ddi_devid_str_free(vd_devid);
442 }
443 ddi_devid_free(devid);
444 }
445
446 /*
447 * Once a device is opened, verify that the physical device path (if
448 * available) is up to date.
449 */
450 if (ldi_get_dev(dvd->vd_lh, &dev) == 0 &&
451 ldi_get_otyp(dvd->vd_lh, &otyp) == 0) {
452 char *physpath, *minorname;
453
454 physpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
455 minorname = NULL;
456 if (ddi_dev_pathname(dev, otyp, physpath) == 0 &&
457 ldi_get_minor_name(dvd->vd_lh, &minorname) == 0 &&
458 (vd->vdev_physpath == NULL ||
459 strcmp(vd->vdev_physpath, physpath) != 0)) {
460 if (vd->vdev_physpath)
461 spa_strfree(vd->vdev_physpath);
462 (void) strlcat(physpath, ":", MAXPATHLEN);
463 (void) strlcat(physpath, minorname, MAXPATHLEN);
464 vd->vdev_physpath = spa_strdup(physpath);
465 }
466 if (minorname)
467 kmem_free(minorname, strlen(minorname) + 1);
468 kmem_free(physpath, MAXPATHLEN);
469 }
470
471 /*
472 * Register callbacks for the LDI offline event.
473 */
474 if (ldi_ev_get_cookie(dvd->vd_lh, LDI_EV_OFFLINE, &ecookie) ==
475 LDI_EV_SUCCESS) {
476 lcb = kmem_zalloc(sizeof (vdev_disk_ldi_cb_t), KM_SLEEP);
477 list_insert_tail(&dvd->vd_ldi_cbs, lcb);
478 (void) ldi_ev_register_callbacks(dvd->vd_lh, ecookie,
479 &vdev_disk_off_callb, (void *) vd, &lcb->lcb_id);
480 }
481
482 /*
483 * Register callbacks for the LDI degrade event.
484 */
485 if (ldi_ev_get_cookie(dvd->vd_lh, LDI_EV_DEGRADE, &ecookie) ==
486 LDI_EV_SUCCESS) {
487 lcb = kmem_zalloc(sizeof (vdev_disk_ldi_cb_t), KM_SLEEP);
488 list_insert_tail(&dvd->vd_ldi_cbs, lcb);
489 (void) ldi_ev_register_callbacks(dvd->vd_lh, ecookie,
490 &vdev_disk_dgrd_callb, (void *) vd, &lcb->lcb_id);
491 }
492
493 /* Reset TRIM flag, as underlying device support may have changed */
494 vd->vdev_notrim = B_FALSE;
495
496 skip_open:
497 ASSERT(dvd != NULL);
498 /*
499 * Determine the actual size of the device.
500 */
501 if (ldi_get_size(dvd->vd_lh, psize) != 0) {
502 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
503 vdev_disk_free_locked(vd);
504 rw_exit(&vd->vdev_tsd_lock);
505 return (SET_ERROR(EINVAL));
506 }
507
508 *max_psize = *psize;
509
510 /*
511 * Determine the device's minimum transfer size.
512 * If the ioctl isn't supported, assume DEV_BSIZE.
513 */
514 if ((error = ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFOEXT,
515 (intptr_t)dkmext, FKIOCTL, kcred, NULL)) == 0) {
516 capacity = dkmext->dki_capacity - 1;
517 blksz = dkmext->dki_lbsize;
518 pbsize = dkmext->dki_pbsize;
519 } else if ((error = ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFO,
520 (intptr_t)dkm, FKIOCTL, kcred, NULL)) == 0) {
521 capacity = dkm->dki_capacity - 1;
522 blksz = dkm->dki_lbsize;
523 pbsize = blksz;
524 } else {
525 pbsize = DEV_BSIZE;
526 }
527
528 *ashift = highbit64(MAX(pbsize, SPA_MINBLOCKSIZE)) - 1;
529
530 if (vd->vdev_wholedisk == 1) {
531 int wce = 1;
532
533 if (error == 0) {
534 /*
535 * If we have the capability to expand, we'd have
536 * found out via success from DKIOCGMEDIAINFO{,EXT}.
537 * Adjust max_psize upward accordingly since we know
538 * we own the whole disk now.
539 */
540 *max_psize = capacity * blksz;
541 }
542
543 /*
544 * Since we own the whole disk, try to enable disk write
545 * caching. We ignore errors because it's OK if we can't do it.
546 */
547 (void) ldi_ioctl(dvd->vd_lh, DKIOCSETWCE, (intptr_t)&wce,
548 FKIOCTL, kcred, NULL);
549 }
550
551 if (ldi_ioctl(dvd->vd_lh, DKIOCSOLIDSTATE, (intptr_t)&vdev_ssd,
552 FKIOCTL, kcred, NULL) != 0)
553 vd->vdev_is_ssd = B_FALSE;
554 else
555 vd->vdev_is_ssd = vdev_ssd ? B_TRUE : B_FALSE;
556
557 /*
558 * We are done with vd_lh and vdev_tsd, release the vdev_tsd_lock
559 */
560 rw_exit(&vd->vdev_tsd_lock);
561
562 /*
563 * Clear the nowritecache bit, so that on a vdev_reopen() we will
564 * try again.
565 */
566 vd->vdev_nowritecache = B_FALSE;
567
568 /*
569 * vdev open has succeeded - reset fault flags if last fault was due
570 * to a failed open since the open fault looks to have been transient
571 */
572 if (vd->vdev_removed || (vd->vdev_faulted &&
573 vd->vdev_label_aux == VDEV_AUX_OPEN_FAILED)) {
574 vd->vdev_faulted = vd->vdev_removed = 0ULL;
575 vd->vdev_label_aux = VDEV_AUX_NONE;
576 }
577
578 return (0);
579 }
580
581 static void
582 vdev_disk_close_impl(vdev_t *vd, boolean_t ldi_offline)
583 {
584 vdev_disk_t *dvd;
585
586 rw_enter(&vd->vdev_tsd_lock, RW_WRITER);
587 dvd = vd->vdev_tsd;
588
589 if (vd->vdev_reopening || dvd == NULL)
590 goto out;
591
592 if (dvd->vd_minor != NULL) {
593 ddi_devid_str_free(dvd->vd_minor);
594 dvd->vd_minor = NULL;
595 }
596
597 if (dvd->vd_devid != NULL) {
598 ddi_devid_free(dvd->vd_devid);
599 dvd->vd_devid = NULL;
600 }
601
602 if (dvd->vd_lh != NULL) {
603 (void) ldi_close(dvd->vd_lh, spa_mode(vd->vdev_spa), kcred);
604 dvd->vd_lh = NULL;
605 }
606
607 vd->vdev_delayed_close = B_FALSE;
608 /*
609 * If we closed the LDI handle due to an offline notify from LDI,
610 * don't free vd->vdev_tsd or unregister the callbacks here;
611 * the offline finalize callback or a reopen will take care of it.
612 */
613 if (!ldi_offline)
614 vdev_disk_free_locked(vd);
615 out:
616 rw_exit(&vd->vdev_tsd_lock);
617 }
618
619 static void
620 vdev_disk_close(vdev_t *vd)
621 {
622 vdev_disk_close_impl(vd, B_FALSE);
623 }
624
625 int
626 vdev_disk_physio(vdev_t *vd, caddr_t data,
627 size_t size, uint64_t offset, int flags, boolean_t isdump)
628 {
629 int rc = EIO;
630 vdev_disk_t *dvd;
631
632 rw_enter(&vd->vdev_tsd_lock, RW_READER);
633 dvd = vd->vdev_tsd;
634 /*
635 * If the vdev is closed, it's likely in the REMOVED or FAULTED state.
636 * Nothing to be done here but return failure.
637 */
638 if (dvd == NULL || dvd->vd_lh == NULL)
639 goto out;
640
641 ASSERT(vd->vdev_ops == &vdev_disk_ops);
642
643 /*
644 * If in the context of an active crash dump, use the ldi_dump(9F)
645 * call instead of ldi_strategy(9F) as usual.
646 */
647 if (isdump) {
648 ASSERT3P(dvd, !=, NULL);
649 rc = ldi_dump(dvd->vd_lh, data, lbtodb(offset), lbtodb(size));
650 goto out;
651 }
652
653 rc = vdev_disk_ldi_physio(dvd->vd_lh, data, size, offset, flags);
654 out:
655 rw_exit(&vd->vdev_tsd_lock);
656 return (rc);
657 }
658
659 int
660 vdev_disk_ldi_physio(ldi_handle_t vd_lh, caddr_t data,
661 size_t size, uint64_t offset, int flags)
662 {
663 buf_t *bp;
664 int error = 0;
665
666 if (vd_lh == NULL)
667 return (SET_ERROR(EINVAL));
668
669 ASSERT(flags & B_READ || flags & B_WRITE);
670
671 bp = getrbuf(KM_SLEEP);
672 bp->b_flags = flags | B_BUSY | B_NOCACHE | B_FAILFAST;
673 bp->b_bcount = size;
674 bp->b_un.b_addr = (void *)data;
675 bp->b_lblkno = lbtodb(offset);
676 bp->b_bufsize = size;
677
678 error = ldi_strategy(vd_lh, bp);
679 ASSERT(error == 0);
680 if ((error = biowait(bp)) == 0 && bp->b_resid != 0)
681 error = SET_ERROR(EIO);
682 freerbuf(bp);
683
684 return (error);
685 }
686
687 static void
688 vdev_disk_io_intr(buf_t *bp)
689 {
690 vdev_buf_t *vb = (vdev_buf_t *)bp;
691 zio_t *zio = vb->vb_io;
692
693 /*
694 * The rest of the zio stack only deals with EIO, ECKSUM, and ENXIO.
695 * Rather than teach the rest of the stack about other error
696 * possibilities (EFAULT, etc), we normalize the error value here.
697 */
698 zio->io_error = (geterror(bp) != 0 ? EIO : 0);
699
700 if (zio->io_error == 0 && bp->b_resid != 0)
701 zio->io_error = SET_ERROR(EIO);
702
703 if (zio->io_type == ZIO_TYPE_READ) {
704 abd_return_buf_copy(zio->io_abd, bp->b_un.b_addr, zio->io_size);
705 } else {
706 abd_return_buf(zio->io_abd, bp->b_un.b_addr, zio->io_size);
707 }
708
709 kmem_free(vb, sizeof (vdev_buf_t));
710
711 zio_delay_interrupt(zio);
712 }
713
714 static void
715 vdev_disk_ioctl_free(zio_t *zio)
716 {
717 kmem_free(zio->io_vsd, sizeof (struct dk_callback));
718 }
719
720 static const zio_vsd_ops_t vdev_disk_vsd_ops = {
721 vdev_disk_ioctl_free,
722 zio_vsd_default_cksum_report
723 };
724
725 static void
726 vdev_disk_ioctl_done(void *zio_arg, int error)
727 {
728 zio_t *zio = zio_arg;
729
730 zio->io_error = error;
731
732 zio_interrupt(zio);
733 }
734
735 static void
736 vdev_disk_io_start(zio_t *zio)
737 {
738 vdev_t *vd = zio->io_vd;
739 vdev_disk_t *dvd;
740 vdev_buf_t *vb;
741 struct dk_callback *dkc;
742 buf_t *bp;
743 int error;
744
745 rw_enter(&vd->vdev_tsd_lock, RW_READER);
746 dvd = vd->vdev_tsd;
747 /*
748 * If the vdev is closed, it's likely in the REMOVED or FAULTED state.
749 * Nothing to be done here but return failure.
750 */
751 if (dvd == NULL || dvd->vd_lh == NULL) {
752 zio->io_error = ENXIO;
753 rw_exit(&vd->vdev_tsd_lock);
754 zio_interrupt(zio);
755 return;
756 }
757
758 if (zio->io_type == ZIO_TYPE_IOCTL) {
759 /* XXPOLICY */
760 if (!vdev_readable(vd)) {
761 zio->io_error = SET_ERROR(ENXIO);
762 rw_exit(&vd->vdev_tsd_lock);
763 zio_interrupt(zio);
764 return;
765 }
766
767 switch (zio->io_cmd) {
768
769 case DKIOCFLUSHWRITECACHE:
770
771 if (zfs_nocacheflush)
772 break;
773
774 if (vd->vdev_nowritecache) {
775 zio->io_error = SET_ERROR(ENOTSUP);
776 break;
777 }
778
779 zio->io_vsd = dkc = kmem_alloc(sizeof (*dkc), KM_SLEEP);
780 zio->io_vsd_ops = &vdev_disk_vsd_ops;
781
782 dkc->dkc_callback = vdev_disk_ioctl_done;
783 dkc->dkc_flag = FLUSH_VOLATILE;
784 dkc->dkc_cookie = zio;
785
786 error = ldi_ioctl(dvd->vd_lh, zio->io_cmd,
787 (uintptr_t)dkc, FKIOCTL, kcred, NULL);
788
789 if (error == 0) {
790 /*
791 * The ioctl will be done asychronously,
792 * and will call vdev_disk_ioctl_done()
793 * upon completion.
794 */
795 rw_exit(&vd->vdev_tsd_lock);
796 return;
797 }
798
799 zio->io_error = error;
800
801 break;
802
803 case DKIOCFREE:
804 /*
805 * We perform device support checks here instead of
806 * in zio_trim(), as zio_trim() might be invoked on
807 * top of a top-level vdev, whereas vdev_disk_io_start
808 * is guaranteed to be operating a leaf vdev.
809 */
810 if (vd->vdev_notrim &&
811 spa_get_force_trim(vd->vdev_spa) !=
812 SPA_FORCE_TRIM_ON) {
813 zio->io_error = SET_ERROR(ENOTSUP);
814 break;
815 }
816
817 /*
818 * zio->io_private contains a dkioc_free_list_t
819 * specifying which offsets are to be freed
820 */
821 ASSERT(zio->io_private != NULL);
822 error = ldi_ioctl(dvd->vd_lh, zio->io_cmd,
823 (uintptr_t)zio->io_private, FKIOCTL, kcred, NULL);
824
825 if (error == ENOTSUP || error == ENOTTY)
826 vd->vdev_notrim = B_TRUE;
827
828 zio->io_error = error;
829
830 break;
831
832 default:
833 zio->io_error = SET_ERROR(ENOTSUP);
834 }
835
836 rw_exit(&vd->vdev_tsd_lock);
837 zio_execute(zio);
838 return;
839 }
840
841 ASSERT(zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE);
842 zio->io_target_timestamp = zio_handle_io_delay(zio);
843
844 vb = kmem_alloc(sizeof (vdev_buf_t), KM_SLEEP);
845
846 vb->vb_io = zio;
847 bp = &vb->vb_buf;
848
849 bioinit(bp);
850 bp->b_flags = B_BUSY | B_NOCACHE |
851 (zio->io_type == ZIO_TYPE_READ ? B_READ : B_WRITE);
852 if (!(zio->io_flags & (ZIO_FLAG_IO_RETRY | ZIO_FLAG_TRYHARD)))
853 bp->b_flags |= B_FAILFAST;
854 bp->b_bcount = zio->io_size;
855
856 if (zio->io_type == ZIO_TYPE_READ) {
857 bp->b_un.b_addr =
858 abd_borrow_buf(zio->io_abd, zio->io_size);
859 } else {
860 bp->b_un.b_addr =
861 abd_borrow_buf_copy(zio->io_abd, zio->io_size);
862 }
863
864 bp->b_lblkno = lbtodb(zio->io_offset);
865 bp->b_bufsize = zio->io_size;
866 bp->b_iodone = (int (*)())vdev_disk_io_intr;
867
868 /* ldi_strategy() will return non-zero only on programming errors */
869 VERIFY(ldi_strategy(dvd->vd_lh, bp) == 0);
870 rw_exit(&vd->vdev_tsd_lock);
871 }
872
873 static void
874 vdev_disk_io_done(zio_t *zio)
875 {
876 vdev_t *vd = zio->io_vd;
877
878 /*
879 * If the device returned EIO, then attempt a DKIOCSTATE ioctl to see if
880 * the device has been removed. If this is the case, then we trigger an
881 * asynchronous removal of the device. Otherwise, probe the device and
882 * make sure it's still accessible.
883 */
884 if (zio->io_error == EIO && !vd->vdev_remove_wanted) {
885 vdev_disk_t *dvd;
886 int rc = EIO, state = DKIO_NONE;
887
888 rw_enter(&vd->vdev_tsd_lock, RW_READER);
889 dvd = vd->vdev_tsd;
890 if (dvd != NULL && dvd->vd_lh != NULL)
891 rc = ldi_ioctl(dvd->vd_lh, DKIOCSTATE,
892 (intptr_t)&state, FKIOCTL, kcred, NULL);
893 rw_exit(&vd->vdev_tsd_lock);
894 if (rc == 0 && state != DKIO_INSERTED) {
895 /*
896 * We post the resource as soon as possible, instead of
897 * when the async removal actually happens, because the
898 * DE is using this information to discard previous I/O
899 * errors.
900 */
901 zfs_post_remove(zio->io_spa, vd);
902 vd->vdev_remove_wanted = B_TRUE;
903 spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE);
904 } else if (!vd->vdev_delayed_close) {
905 vd->vdev_delayed_close = B_TRUE;
906 }
907 }
908 }
909
910 vdev_ops_t vdev_disk_ops = {
911 vdev_disk_open,
912 vdev_disk_close,
913 vdev_default_asize,
914 vdev_disk_io_start,
915 vdev_disk_io_done,
916 NULL,
917 vdev_disk_hold,
918 vdev_disk_rele,
919 NULL,
920 VDEV_TYPE_DISK, /* name of this vdev type */
921 B_TRUE /* leaf vdev */
922 };
923
924 /*
925 * Given the root disk device devid or pathname, read the label from
926 * the device, and construct a configuration nvlist.
927 */
928 int
929 vdev_disk_read_rootlabel(char *devpath, char *devid, nvlist_t **config)
930 {
931 ldi_handle_t vd_lh;
932 vdev_label_t *label;
933 uint64_t s, size;
934 int l;
935 ddi_devid_t tmpdevid;
936 int error = -1;
937 char *minor_name;
938
939 /*
940 * Read the device label and build the nvlist.
941 */
942 if (devid != NULL && ddi_devid_str_decode(devid, &tmpdevid,
943 &minor_name) == 0) {
944 error = ldi_open_by_devid(tmpdevid, minor_name,
945 FREAD, kcred, &vd_lh, zfs_li);
946 ddi_devid_free(tmpdevid);
947 ddi_devid_str_free(minor_name);
948 }
949
950 if (error && (error = ldi_open_by_name(devpath, FREAD, kcred, &vd_lh,
951 zfs_li)))
952 return (error);
953
954 if (ldi_get_size(vd_lh, &s)) {
955 (void) ldi_close(vd_lh, FREAD, kcred);
956 return (SET_ERROR(EIO));
957 }
958
959 size = P2ALIGN_TYPED(s, sizeof (vdev_label_t), uint64_t);
960 label = kmem_alloc(sizeof (vdev_label_t), KM_SLEEP);
961
962 *config = NULL;
963 for (l = 0; l < VDEV_LABELS; l++) {
964 uint64_t offset, state, txg = 0;
965
966 /* read vdev label */
967 offset = vdev_label_offset(size, l, 0);
968 if (vdev_disk_ldi_physio(vd_lh, (caddr_t)label,
969 VDEV_SKIP_SIZE + VDEV_PHYS_SIZE, offset, B_READ) != 0)
970 continue;
971
972 if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist,
973 sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0) {
974 *config = NULL;
975 continue;
976 }
977
978 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
979 &state) != 0 || state >= POOL_STATE_DESTROYED) {
980 nvlist_free(*config);
981 *config = NULL;
982 continue;
983 }
984
985 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
986 &txg) != 0 || txg == 0) {
987 nvlist_free(*config);
988 *config = NULL;
989 continue;
990 }
991
992 break;
993 }
994
995 kmem_free(label, sizeof (vdev_label_t));
996 (void) ldi_close(vd_lh, FREAD, kcred);
997 if (*config == NULL)
998 error = SET_ERROR(EIDRM);
999
1000 return (error);
1001 }