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