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