Print this page
expandable RAID-Z
| 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 2007 Sun Microsystems, Inc. All rights reserved.
23 23 * Use is subject to license terms.
24 24 */
25 25
26 26 #pragma ident "%Z%%M% %I% %E% SMI"
27 27
28 28 #include <sys/zfs_context.h>
29 29 #include <sys/spa.h>
30 30 #include <sys/vdev_disk.h>
31 31 #include <sys/vdev_impl.h>
32 32 #include <sys/fs/zfs.h>
33 33 #include <sys/zio.h>
34 34 #include <sys/sunldi.h>
35 35
36 36 /*
37 37 * Virtual device vector for disks.
38 38 */
39 39
40 40 extern ldi_ident_t zfs_li;
41 41
42 42 typedef struct vdev_disk_buf {
43 43 buf_t vdb_buf;
44 44 zio_t *vdb_io;
45 45 } vdev_disk_buf_t;
46 46
47 47 static int
48 48 vdev_disk_open_common(vdev_t *vd)
49 49 {
50 50 vdev_disk_t *dvd;
51 51 dev_t dev;
52 52 int error;
53 53
54 54 /*
55 55 * We must have a pathname, and it must be absolute.
56 56 */
57 57 if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
58 58 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
59 59 return (EINVAL);
60 60 }
61 61
62 62 dvd = vd->vdev_tsd = kmem_zalloc(sizeof (vdev_disk_t), KM_SLEEP);
63 63
64 64 /*
65 65 * When opening a disk device, we want to preserve the user's original
66 66 * intent. We always want to open the device by the path the user gave
67 67 * us, even if it is one of multiple paths to the save device. But we
68 68 * also want to be able to survive disks being removed/recabled.
69 69 * Therefore the sequence of opening devices is:
70 70 *
71 71 * 1. Try opening the device by path. For legacy pools without the
72 72 * 'whole_disk' property, attempt to fix the path by appending 's0'.
73 73 *
74 74 * 2. If the devid of the device matches the stored value, return
75 75 * success.
76 76 *
77 77 * 3. Otherwise, the device may have moved. Try opening the device
78 78 * by the devid instead.
79 79 *
80 80 */
81 81 if (vd->vdev_devid != NULL) {
82 82 if (ddi_devid_str_decode(vd->vdev_devid, &dvd->vd_devid,
83 83 &dvd->vd_minor) != 0) {
84 84 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
85 85 return (EINVAL);
86 86 }
87 87 }
88 88
89 89 error = EINVAL; /* presume failure */
90 90
91 91 if (vd->vdev_path != NULL) {
92 92 ddi_devid_t devid;
93 93
94 94 if (vd->vdev_wholedisk == -1ULL) {
95 95 size_t len = strlen(vd->vdev_path) + 3;
96 96 char *buf = kmem_alloc(len, KM_SLEEP);
97 97 ldi_handle_t lh;
98 98
99 99 (void) snprintf(buf, len, "%ss0", vd->vdev_path);
100 100
101 101 if (ldi_open_by_name(buf, spa_mode, kcred,
102 102 &lh, zfs_li) == 0) {
103 103 spa_strfree(vd->vdev_path);
104 104 vd->vdev_path = buf;
105 105 vd->vdev_wholedisk = 1ULL;
106 106 (void) ldi_close(lh, spa_mode, kcred);
107 107 } else {
108 108 kmem_free(buf, len);
109 109 }
110 110 }
111 111
112 112 error = ldi_open_by_name(vd->vdev_path, spa_mode, kcred,
113 113 &dvd->vd_lh, zfs_li);
114 114
115 115 /*
116 116 * Compare the devid to the stored value.
117 117 */
118 118 if (error == 0 && vd->vdev_devid != NULL &&
119 119 ldi_get_devid(dvd->vd_lh, &devid) == 0) {
120 120 if (ddi_devid_compare(devid, dvd->vd_devid) != 0) {
121 121 error = EINVAL;
122 122 (void) ldi_close(dvd->vd_lh, spa_mode, kcred);
123 123 dvd->vd_lh = NULL;
124 124 }
125 125 ddi_devid_free(devid);
126 126 }
127 127
128 128 /*
129 129 * If we succeeded in opening the device, but 'vdev_wholedisk'
130 130 * is not yet set, then this must be a slice.
131 131 */
132 132 if (error == 0 && vd->vdev_wholedisk == -1ULL)
133 133 vd->vdev_wholedisk = 0;
134 134 }
135 135
136 136 /*
137 137 * If we were unable to open by path, or the devid check fails, open by
138 138 * devid instead.
139 139 */
140 140 if (error != 0 && vd->vdev_devid != NULL)
141 141 error = ldi_open_by_devid(dvd->vd_devid, dvd->vd_minor,
142 142 spa_mode, kcred, &dvd->vd_lh, zfs_li);
143 143
144 144 /*
145 145 * If all else fails, then try opening by physical path (if available)
146 146 * or the logical path (if we failed due to the devid check). While not
147 147 * as reliable as the devid, this will give us something, and the higher
148 148 * level vdev validation will prevent us from opening the wrong device.
149 149 */
150 150 if (error) {
151 151 if (vd->vdev_physpath != NULL &&
152 152 (dev = ddi_pathname_to_dev_t(vd->vdev_physpath)) != ENODEV)
153 153 error = ldi_open_by_dev(&dev, OTYP_BLK, spa_mode,
154 154 kcred, &dvd->vd_lh, zfs_li);
155 155
156 156 /*
157 157 * Note that we don't support the legacy auto-wholedisk support
158 158 * as above. This hasn't been used in a very long time and we
159 159 * don't need to propagate its oddities to this edge condition.
160 160 */
161 161 if (error && vd->vdev_path != NULL)
162 162 error = ldi_open_by_name(vd->vdev_path, spa_mode, kcred,
163 163 &dvd->vd_lh, zfs_li);
164 164 }
165 165
166 166 if (error)
167 167 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
168 168
169 169 return (error);
170 170 }
171 171
172 172 static int
173 173 vdev_disk_open(vdev_t *vd, uint64_t *psize, uint64_t *ashift)
174 174 {
175 175 vdev_disk_t *dvd;
176 176 struct dk_minfo dkm;
177 177 int error;
178 178 dev_t dev;
179 179 int otyp;
180 180
181 181 error = vdev_disk_open_common(vd);
182 182 if (error)
183 183 return (error);
184 184
185 185 dvd = vd->vdev_tsd;
186 186 /*
187 187 * Once a device is opened, verify that the physical device path (if
188 188 * available) is up to date.
189 189 */
190 190 if (ldi_get_dev(dvd->vd_lh, &dev) == 0 &&
191 191 ldi_get_otyp(dvd->vd_lh, &otyp) == 0) {
192 192 char *physpath, *minorname;
193 193
194 194 physpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
195 195 minorname = NULL;
196 196 if (ddi_dev_pathname(dev, otyp, physpath) == 0 &&
197 197 ldi_get_minor_name(dvd->vd_lh, &minorname) == 0 &&
198 198 (vd->vdev_physpath == NULL ||
199 199 strcmp(vd->vdev_physpath, physpath) != 0)) {
200 200 if (vd->vdev_physpath)
201 201 spa_strfree(vd->vdev_physpath);
202 202 (void) strlcat(physpath, ":", MAXPATHLEN);
203 203 (void) strlcat(physpath, minorname, MAXPATHLEN);
204 204 vd->vdev_physpath = spa_strdup(physpath);
205 205 }
206 206 if (minorname)
207 207 kmem_free(minorname, strlen(minorname) + 1);
208 208 kmem_free(physpath, MAXPATHLEN);
209 209 }
210 210
211 211 /*
212 212 * Determine the actual size of the device.
213 213 */
214 214 if (ldi_get_size(dvd->vd_lh, psize) != 0) {
215 215 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
216 216 return (EINVAL);
217 217 }
218 218
219 219 /*
220 220 * If we own the whole disk, try to enable disk write caching.
221 221 * We ignore errors because it's OK if we can't do it.
222 222 */
223 223 if (vd->vdev_wholedisk == 1) {
224 224 int wce = 1;
225 225 (void) ldi_ioctl(dvd->vd_lh, DKIOCSETWCE, (intptr_t)&wce,
226 226 FKIOCTL, kcred, NULL);
227 227 }
228 228
229 229 /*
230 230 * Determine the device's minimum transfer size.
231 231 * If the ioctl isn't supported, assume DEV_BSIZE.
232 232 */
233 233 if (ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFO, (intptr_t)&dkm,
234 234 FKIOCTL, kcred, NULL) != 0)
235 235 dkm.dki_lbsize = DEV_BSIZE;
236 236
237 237 *ashift = highbit(MAX(dkm.dki_lbsize, SPA_MINBLOCKSIZE)) - 1;
238 238
239 239 /*
240 240 * Clear the nowritecache bit, so that on a vdev_reopen() we will
241 241 * try again.
242 242 */
243 243 vd->vdev_nowritecache = B_FALSE;
244 244
245 245 return (0);
246 246 }
247 247
248 248 static void
249 249 vdev_disk_close(vdev_t *vd)
250 250 {
251 251 vdev_disk_t *dvd = vd->vdev_tsd;
252 252
253 253 if (dvd == NULL)
254 254 return;
255 255
256 256 if (dvd->vd_minor != NULL)
257 257 ddi_devid_str_free(dvd->vd_minor);
258 258
259 259 if (dvd->vd_devid != NULL)
260 260 ddi_devid_free(dvd->vd_devid);
261 261
262 262 if (dvd->vd_lh != NULL)
263 263 (void) ldi_close(dvd->vd_lh, spa_mode, kcred);
264 264
265 265 kmem_free(dvd, sizeof (vdev_disk_t));
266 266 vd->vdev_tsd = NULL;
267 267 }
268 268
269 269 static int
270 270 vdev_disk_probe_io(vdev_t *vd, caddr_t data, size_t size, uint64_t offset,
271 271 int flags)
272 272 {
273 273 buf_t buf;
274 274 int error = 0;
275 275 vdev_disk_t *dvd = vd->vdev_tsd;
276 276
277 277 if (vd == NULL || dvd == NULL || dvd->vd_lh == NULL)
278 278 return (EINVAL);
279 279
280 280 ASSERT(flags & B_READ || flags & B_WRITE);
281 281
282 282 bioinit(&buf);
283 283 buf.b_flags = flags | B_BUSY | B_NOCACHE | B_FAILFAST;
284 284 buf.b_bcount = size;
285 285 buf.b_un.b_addr = (void *)data;
286 286 buf.b_lblkno = lbtodb(offset);
287 287 buf.b_bufsize = size;
288 288
289 289 error = ldi_strategy(dvd->vd_lh, &buf);
290 290 ASSERT(error == 0);
291 291 error = biowait(&buf);
292 292
293 293 if (zio_injection_enabled && error == 0)
294 294 error = zio_handle_device_injection(vd, EIO);
295 295
296 296 return (error);
297 297 }
298 298
299 299 /*
300 300 * Determine if the underlying device is accessible by reading and writing
301 301 * to a known location. We must be able to do this during syncing context
302 302 * and thus we cannot set the vdev state directly.
303 303 */
304 304 static int
305 305 vdev_disk_probe(vdev_t *vd)
306 306 {
307 307 uint64_t offset;
308 308 vdev_t *nvd;
309 309 int l, error = 0, retries = 0;
310 310 char *vl_pad;
311 311
312 312 if (vd == NULL)
313 313 return (EINVAL);
314 314
315 315 /* Hijack the current vdev */
316 316 nvd = vd;
317 317
318 318 /*
319 319 * Pick a random label to rewrite.
320 320 */
321 321 l = spa_get_random(VDEV_LABELS);
322 322 ASSERT(l < VDEV_LABELS);
323 323
324 324 offset = vdev_label_offset(vd->vdev_psize, l,
325 325 offsetof(vdev_label_t, vl_pad));
326 326
327 327 vl_pad = kmem_alloc(VDEV_SKIP_SIZE, KM_SLEEP);
328 328
329 329 /*
330 330 * Try to read and write to a special location on the
331 331 * label. We use the existing vdev initially and only
332 332 * try to create and reopen it if we encounter a failure.
333 333 */
334 334 while ((error = vdev_disk_probe_io(nvd, vl_pad, VDEV_SKIP_SIZE,
335 335 offset, B_READ)) != 0 && retries == 0) {
336 336
337 337 nvd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
338 338 if (vd->vdev_path)
339 339 nvd->vdev_path = spa_strdup(vd->vdev_path);
340 340 if (vd->vdev_physpath)
341 341 nvd->vdev_physpath = spa_strdup(vd->vdev_physpath);
342 342 if (vd->vdev_devid)
343 343 nvd->vdev_devid = spa_strdup(vd->vdev_devid);
344 344 nvd->vdev_wholedisk = vd->vdev_wholedisk;
345 345 nvd->vdev_guid = vd->vdev_guid;
346 346 retries++;
347 347
348 348 error = vdev_disk_open_common(nvd);
349 349 if (error)
350 350 break;
351 351 }
352 352
353 353 if (!error) {
354 354 error = vdev_disk_probe_io(nvd, vl_pad, VDEV_SKIP_SIZE,
355 355 offset, B_WRITE);
356 356 }
357 357
358 358 /* Clean up if we allocated a new vdev */
359 359 if (retries) {
360 360 vdev_disk_close(nvd);
361 361 if (nvd->vdev_path)
362 362 spa_strfree(nvd->vdev_path);
363 363 if (nvd->vdev_physpath)
364 364 spa_strfree(nvd->vdev_physpath);
365 365 if (nvd->vdev_devid)
366 366 spa_strfree(nvd->vdev_devid);
367 367 kmem_free(nvd, sizeof (vdev_t));
368 368 }
369 369 kmem_free(vl_pad, VDEV_SKIP_SIZE);
370 370
371 371 /* Reset the failing flag */
372 372 if (!error)
373 373 vd->vdev_is_failing = B_FALSE;
374 374
375 375 return (error);
376 376 }
377 377
378 378 static void
379 379 vdev_disk_io_intr(buf_t *bp)
380 380 {
381 381 vdev_disk_buf_t *vdb = (vdev_disk_buf_t *)bp;
382 382 zio_t *zio = vdb->vdb_io;
383 383
384 384 if ((zio->io_error = geterror(bp)) == 0 && bp->b_resid != 0)
385 385 zio->io_error = EIO;
386 386
387 387 kmem_free(vdb, sizeof (vdev_disk_buf_t));
388 388
389 389 zio_interrupt(zio);
390 390 }
391 391
392 392 static void
393 393 vdev_disk_ioctl_done(void *zio_arg, int error)
394 394 {
395 395 zio_t *zio = zio_arg;
396 396
397 397 zio->io_error = error;
398 398
399 399 zio_interrupt(zio);
400 400 }
401 401
402 402 static int
403 403 vdev_disk_io_start(zio_t *zio)
404 404 {
405 405 vdev_t *vd = zio->io_vd;
406 406 vdev_disk_t *dvd = vd->vdev_tsd;
407 407 vdev_disk_buf_t *vdb;
408 408 buf_t *bp;
409 409 int flags, error;
410 410
411 411 if (zio->io_type == ZIO_TYPE_IOCTL) {
412 412 zio_vdev_io_bypass(zio);
413 413
414 414 /* XXPOLICY */
415 415 if (!vdev_readable(vd)) {
416 416 zio->io_error = ENXIO;
417 417 return (ZIO_PIPELINE_CONTINUE);
418 418 }
419 419
420 420 switch (zio->io_cmd) {
421 421
422 422 case DKIOCFLUSHWRITECACHE:
423 423
424 424 if (zfs_nocacheflush)
425 425 break;
426 426
427 427 if (vd->vdev_nowritecache) {
428 428 zio->io_error = ENOTSUP;
429 429 break;
430 430 }
431 431
432 432 zio->io_dk_callback.dkc_callback = vdev_disk_ioctl_done;
433 433 zio->io_dk_callback.dkc_flag = FLUSH_VOLATILE;
434 434 zio->io_dk_callback.dkc_cookie = zio;
435 435
436 436 error = ldi_ioctl(dvd->vd_lh, zio->io_cmd,
437 437 (uintptr_t)&zio->io_dk_callback,
438 438 FKIOCTL, kcred, NULL);
439 439
440 440 if (error == 0) {
441 441 /*
442 442 * The ioctl will be done asychronously,
443 443 * and will call vdev_disk_ioctl_done()
444 444 * upon completion.
445 445 */
446 446 return (ZIO_PIPELINE_STOP);
447 447 }
448 448
449 449 if (error == ENOTSUP || error == ENOTTY) {
450 450 /*
451 451 * If we get ENOTSUP or ENOTTY, we know that
452 452 * no future attempts will ever succeed.
453 453 * In this case we set a persistent bit so
454 454 * that we don't bother with the ioctl in the
455 455 * future.
456 456 */
457 457 vd->vdev_nowritecache = B_TRUE;
458 458 }
459 459 zio->io_error = error;
460 460
461 461 break;
462 462
463 463 default:
464 464 zio->io_error = ENOTSUP;
465 465 }
466 466
467 467 return (ZIO_PIPELINE_CONTINUE);
468 468 }
469 469
470 470 if (zio->io_type == ZIO_TYPE_READ && vdev_cache_read(zio) == 0)
471 471 return (ZIO_PIPELINE_STOP);
472 472
473 473 if ((zio = vdev_queue_io(zio)) == NULL)
474 474 return (ZIO_PIPELINE_STOP);
475 475
476 476 if (zio->io_type == ZIO_TYPE_WRITE)
477 477 error = vdev_writeable(vd) ? vdev_error_inject(vd, zio) : ENXIO;
478 478 else
479 479 error = vdev_readable(vd) ? vdev_error_inject(vd, zio) : ENXIO;
480 480 error = (vd->vdev_remove_wanted || vd->vdev_is_failing) ? ENXIO : error;
481 481
482 482 if (error) {
483 483 zio->io_error = error;
484 484 zio_interrupt(zio);
485 485 return (ZIO_PIPELINE_STOP);
486 486 }
487 487
488 488 flags = (zio->io_type == ZIO_TYPE_READ ? B_READ : B_WRITE);
489 489 flags |= B_BUSY | B_NOCACHE;
490 490 if (zio->io_flags & ZIO_FLAG_FAILFAST)
491 491 flags |= B_FAILFAST;
492 492
493 493 vdb = kmem_alloc(sizeof (vdev_disk_buf_t), KM_SLEEP);
494 494
495 495 vdb->vdb_io = zio;
496 496 bp = &vdb->vdb_buf;
497 497
498 498 bioinit(bp);
499 499 bp->b_flags = flags;
500 500 bp->b_bcount = zio->io_size;
501 501 bp->b_un.b_addr = zio->io_data;
502 502 bp->b_lblkno = lbtodb(zio->io_offset);
503 503 bp->b_bufsize = zio->io_size;
504 504 bp->b_iodone = (int (*)())vdev_disk_io_intr;
505 505
506 506 error = ldi_strategy(dvd->vd_lh, bp);
507 507 /* ldi_strategy() will return non-zero only on programming errors */
508 508 ASSERT(error == 0);
509 509
510 510 return (ZIO_PIPELINE_STOP);
511 511 }
512 512
513 513 static int
514 514 vdev_disk_io_done(zio_t *zio)
515 515 {
516 516 vdev_queue_io_done(zio);
517 517
518 518 if (zio->io_type == ZIO_TYPE_WRITE)
519 519 vdev_cache_write(zio);
520 520
521 521 if (zio_injection_enabled && zio->io_error == 0)
522 522 zio->io_error = zio_handle_device_injection(zio->io_vd, EIO);
523 523
524 524 /*
525 525 * If the device returned EIO, then attempt a DKIOCSTATE ioctl to see if
526 526 * the device has been removed. If this is the case, then we trigger an
527 527 * asynchronous removal of the device. Otherwise, probe the device and
528 528 * make sure it's still accessible.
529 529 */
530 530 if (zio->io_error == EIO) {
531 531 vdev_t *vd = zio->io_vd;
532 532 vdev_disk_t *dvd = vd->vdev_tsd;
533 533 int state;
534 534
535 535 state = DKIO_NONE;
536 536 if (dvd && ldi_ioctl(dvd->vd_lh, DKIOCSTATE, (intptr_t)&state,
537 537 FKIOCTL, kcred, NULL) == 0 &&
538 538 state != DKIO_INSERTED) {
539 539 vd->vdev_remove_wanted = B_TRUE;
540 540 spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE);
541 541 } else if (vdev_probe(vd) != 0) {
542 542 ASSERT(vd->vdev_ops->vdev_op_leaf);
543 543 vd->vdev_is_failing = B_TRUE;
544 544 }
545 545 }
546 546
547 547 return (ZIO_PIPELINE_CONTINUE);
|
↓ open down ↓ |
547 lines elided |
↑ open up ↑ |
548 548 }
549 549
550 550 vdev_ops_t vdev_disk_ops = {
551 551 vdev_disk_open,
552 552 vdev_disk_close,
553 553 vdev_disk_probe,
554 554 vdev_default_asize,
555 555 vdev_disk_io_start,
556 556 vdev_disk_io_done,
557 557 NULL,
558 + NULL,
558 559 VDEV_TYPE_DISK, /* name of this vdev type */
559 560 B_TRUE /* leaf vdev */
560 561 };
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX