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