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