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