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