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