xref: /illumos-gate/usr/src/uts/common/fs/zfs/vdev_disk.c (revision 635216b673cf196ac523ff2a7ab715717e553292)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/zfs_context.h>
27 #include <sys/spa.h>
28 #include <sys/refcount.h>
29 #include <sys/vdev_disk.h>
30 #include <sys/vdev_impl.h>
31 #include <sys/fs/zfs.h>
32 #include <sys/zio.h>
33 #include <sys/sunldi.h>
34 #include <sys/fm/fs/zfs.h>
35 
36 /*
37  * Virtual device vector for disks.
38  */
39 
40 extern ldi_ident_t zfs_li;
41 
42 typedef struct vdev_disk_buf {
43 	buf_t	vdb_buf;
44 	zio_t	*vdb_io;
45 } vdev_disk_buf_t;
46 
47 static int
48 vdev_disk_open(vdev_t *vd, uint64_t *psize, uint64_t *ashift)
49 {
50 	spa_t *spa = vd->vdev_spa;
51 	vdev_disk_t *dvd;
52 	struct dk_minfo dkm;
53 	int error;
54 	dev_t dev;
55 	int otyp;
56 
57 	/*
58 	 * We must have a pathname, and it must be absolute.
59 	 */
60 	if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
61 		vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
62 		return (EINVAL);
63 	}
64 
65 	dvd = vd->vdev_tsd = kmem_zalloc(sizeof (vdev_disk_t), KM_SLEEP);
66 
67 	/*
68 	 * When opening a disk device, we want to preserve the user's original
69 	 * intent.  We always want to open the device by the path the user gave
70 	 * us, even if it is one of multiple paths to the save device.  But we
71 	 * also want to be able to survive disks being removed/recabled.
72 	 * Therefore the sequence of opening devices is:
73 	 *
74 	 * 1. Try opening the device by path.  For legacy pools without the
75 	 *    'whole_disk' property, attempt to fix the path by appending 's0'.
76 	 *
77 	 * 2. If the devid of the device matches the stored value, return
78 	 *    success.
79 	 *
80 	 * 3. Otherwise, the device may have moved.  Try opening the device
81 	 *    by the devid instead.
82 	 *
83 	 * If the vdev is part of the root pool, we avoid opening it by path
84 	 * unless we're adding (i.e. attaching) it to the vdev namespace.
85 	 * We do this because there is no /dev path available early in boot,
86 	 * and if we try to open the device by path at a later point, we can
87 	 * deadlock when devfsadm attempts to open the underlying backing store
88 	 * file.
89 	 */
90 	if (vd->vdev_devid != NULL) {
91 		if (ddi_devid_str_decode(vd->vdev_devid, &dvd->vd_devid,
92 		    &dvd->vd_minor) != 0) {
93 			vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
94 			return (EINVAL);
95 		}
96 	}
97 
98 	error = EINVAL;		/* presume failure */
99 
100 	if (vd->vdev_path != NULL && (!spa_is_root(spa) ||
101 	    spa_lookup_by_guid(spa, vd->vdev_guid, B_FALSE) == NULL)) {
102 		ddi_devid_t devid;
103 
104 		if (vd->vdev_wholedisk == -1ULL) {
105 			size_t len = strlen(vd->vdev_path) + 3;
106 			char *buf = kmem_alloc(len, KM_SLEEP);
107 			ldi_handle_t lh;
108 
109 			(void) snprintf(buf, len, "%ss0", vd->vdev_path);
110 
111 			if (ldi_open_by_name(buf, spa_mode(spa), kcred,
112 			    &lh, zfs_li) == 0) {
113 				spa_strfree(vd->vdev_path);
114 				vd->vdev_path = buf;
115 				vd->vdev_wholedisk = 1ULL;
116 				(void) ldi_close(lh, spa_mode(spa), kcred);
117 			} else {
118 				kmem_free(buf, len);
119 			}
120 		}
121 
122 		error = ldi_open_by_name(vd->vdev_path, spa_mode(spa), kcred,
123 		    &dvd->vd_lh, zfs_li);
124 
125 		/*
126 		 * Compare the devid to the stored value.
127 		 */
128 		if (error == 0 && vd->vdev_devid != NULL &&
129 		    ldi_get_devid(dvd->vd_lh, &devid) == 0) {
130 			if (ddi_devid_compare(devid, dvd->vd_devid) != 0) {
131 				error = EINVAL;
132 				(void) ldi_close(dvd->vd_lh, spa_mode(spa),
133 				    kcred);
134 				dvd->vd_lh = NULL;
135 			}
136 			ddi_devid_free(devid);
137 		}
138 
139 		/*
140 		 * If we succeeded in opening the device, but 'vdev_wholedisk'
141 		 * is not yet set, then this must be a slice.
142 		 */
143 		if (error == 0 && vd->vdev_wholedisk == -1ULL)
144 			vd->vdev_wholedisk = 0;
145 	}
146 
147 	/*
148 	 * If we were unable to open by path, or the devid check fails, open by
149 	 * devid instead.
150 	 */
151 	if (error != 0 && vd->vdev_devid != NULL)
152 		error = ldi_open_by_devid(dvd->vd_devid, dvd->vd_minor,
153 		    spa_mode(spa), kcred, &dvd->vd_lh, zfs_li);
154 
155 	/*
156 	 * If all else fails, then try opening by physical path (if available)
157 	 * or the logical path (if we failed due to the devid check).  While not
158 	 * as reliable as the devid, this will give us something, and the higher
159 	 * level vdev validation will prevent us from opening the wrong device.
160 	 */
161 	if (error) {
162 		if (vd->vdev_physpath != NULL &&
163 		    (dev = ddi_pathname_to_dev_t(vd->vdev_physpath)) != NODEV)
164 			error = ldi_open_by_dev(&dev, OTYP_BLK, spa_mode(spa),
165 			    kcred, &dvd->vd_lh, zfs_li);
166 
167 		/*
168 		 * Note that we don't support the legacy auto-wholedisk support
169 		 * as above.  This hasn't been used in a very long time and we
170 		 * don't need to propagate its oddities to this edge condition.
171 		 */
172 		if (error && vd->vdev_path != NULL && (!spa_is_root(spa) ||
173 		    spa_lookup_by_guid(spa, vd->vdev_guid, B_FALSE) == NULL))
174 			error = ldi_open_by_name(vd->vdev_path, spa_mode(spa),
175 			    kcred, &dvd->vd_lh, zfs_li);
176 	}
177 
178 	if (error) {
179 		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
180 		return (error);
181 	}
182 
183 	/*
184 	 * Once a device is opened, verify that the physical device path (if
185 	 * available) is up to date.
186 	 */
187 	if (ldi_get_dev(dvd->vd_lh, &dev) == 0 &&
188 	    ldi_get_otyp(dvd->vd_lh, &otyp) == 0) {
189 		char *physpath, *minorname;
190 
191 		physpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
192 		minorname = NULL;
193 		if (ddi_dev_pathname(dev, otyp, physpath) == 0 &&
194 		    ldi_get_minor_name(dvd->vd_lh, &minorname) == 0 &&
195 		    (vd->vdev_physpath == NULL ||
196 		    strcmp(vd->vdev_physpath, physpath) != 0)) {
197 			if (vd->vdev_physpath)
198 				spa_strfree(vd->vdev_physpath);
199 			(void) strlcat(physpath, ":", MAXPATHLEN);
200 			(void) strlcat(physpath, minorname, MAXPATHLEN);
201 			vd->vdev_physpath = spa_strdup(physpath);
202 		}
203 		if (minorname)
204 			kmem_free(minorname, strlen(minorname) + 1);
205 		kmem_free(physpath, MAXPATHLEN);
206 	}
207 
208 	/*
209 	 * Determine the actual size of the device.
210 	 */
211 	if (ldi_get_size(dvd->vd_lh, psize) != 0) {
212 		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
213 		return (EINVAL);
214 	}
215 
216 	/*
217 	 * If we own the whole disk, try to enable disk write caching.
218 	 * We ignore errors because it's OK if we can't do it.
219 	 */
220 	if (vd->vdev_wholedisk == 1) {
221 		int wce = 1;
222 		(void) ldi_ioctl(dvd->vd_lh, DKIOCSETWCE, (intptr_t)&wce,
223 		    FKIOCTL, kcred, NULL);
224 	}
225 
226 	/*
227 	 * Determine the device's minimum transfer size.
228 	 * If the ioctl isn't supported, assume DEV_BSIZE.
229 	 */
230 	if (ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFO, (intptr_t)&dkm,
231 	    FKIOCTL, kcred, NULL) != 0)
232 		dkm.dki_lbsize = DEV_BSIZE;
233 
234 	*ashift = highbit(MAX(dkm.dki_lbsize, SPA_MINBLOCKSIZE)) - 1;
235 
236 	/*
237 	 * Clear the nowritecache bit, so that on a vdev_reopen() we will
238 	 * try again.
239 	 */
240 	vd->vdev_nowritecache = B_FALSE;
241 
242 	return (0);
243 }
244 
245 static void
246 vdev_disk_close(vdev_t *vd)
247 {
248 	vdev_disk_t *dvd = vd->vdev_tsd;
249 
250 	if (dvd == NULL)
251 		return;
252 
253 	if (dvd->vd_minor != NULL)
254 		ddi_devid_str_free(dvd->vd_minor);
255 
256 	if (dvd->vd_devid != NULL)
257 		ddi_devid_free(dvd->vd_devid);
258 
259 	if (dvd->vd_lh != NULL)
260 		(void) ldi_close(dvd->vd_lh, spa_mode(vd->vdev_spa), kcred);
261 
262 	kmem_free(dvd, sizeof (vdev_disk_t));
263 	vd->vdev_tsd = NULL;
264 }
265 
266 int
267 vdev_disk_physio(ldi_handle_t vd_lh, caddr_t data, size_t size,
268     uint64_t offset, int flags)
269 {
270 	buf_t *bp;
271 	int error = 0;
272 
273 	if (vd_lh == NULL)
274 		return (EINVAL);
275 
276 	ASSERT(flags & B_READ || flags & B_WRITE);
277 
278 	bp = getrbuf(KM_SLEEP);
279 	bp->b_flags = flags | B_BUSY | B_NOCACHE | B_FAILFAST;
280 	bp->b_bcount = size;
281 	bp->b_un.b_addr = (void *)data;
282 	bp->b_lblkno = lbtodb(offset);
283 	bp->b_bufsize = size;
284 
285 	error = ldi_strategy(vd_lh, bp);
286 	ASSERT(error == 0);
287 	if ((error = biowait(bp)) == 0 && bp->b_resid != 0)
288 		error = EIO;
289 	freerbuf(bp);
290 
291 	return (error);
292 }
293 
294 static void
295 vdev_disk_io_intr(buf_t *bp)
296 {
297 	vdev_disk_buf_t *vdb = (vdev_disk_buf_t *)bp;
298 	zio_t *zio = vdb->vdb_io;
299 
300 	/*
301 	 * The rest of the zio stack only deals with EIO, ECKSUM, and ENXIO.
302 	 * Rather than teach the rest of the stack about other error
303 	 * possibilities (EFAULT, etc), we normalize the error value here.
304 	 */
305 	zio->io_error = (geterror(bp) != 0 ? EIO : 0);
306 
307 	if (zio->io_error == 0 && bp->b_resid != 0)
308 		zio->io_error = EIO;
309 
310 	kmem_free(vdb, sizeof (vdev_disk_buf_t));
311 
312 	zio_interrupt(zio);
313 }
314 
315 static void
316 vdev_disk_ioctl_free(zio_t *zio)
317 {
318 	kmem_free(zio->io_vsd, sizeof (struct dk_callback));
319 }
320 
321 static const zio_vsd_ops_t vdev_disk_vsd_ops = {
322 	vdev_disk_ioctl_free,
323 	zio_vsd_default_cksum_report
324 };
325 
326 static void
327 vdev_disk_ioctl_done(void *zio_arg, int error)
328 {
329 	zio_t *zio = zio_arg;
330 
331 	zio->io_error = error;
332 
333 	zio_interrupt(zio);
334 }
335 
336 static int
337 vdev_disk_io_start(zio_t *zio)
338 {
339 	vdev_t *vd = zio->io_vd;
340 	vdev_disk_t *dvd = vd->vdev_tsd;
341 	vdev_disk_buf_t *vdb;
342 	struct dk_callback *dkc;
343 	buf_t *bp;
344 	int error;
345 
346 	if (zio->io_type == ZIO_TYPE_IOCTL) {
347 		/* XXPOLICY */
348 		if (!vdev_readable(vd)) {
349 			zio->io_error = ENXIO;
350 			return (ZIO_PIPELINE_CONTINUE);
351 		}
352 
353 		switch (zio->io_cmd) {
354 
355 		case DKIOCFLUSHWRITECACHE:
356 
357 			if (zfs_nocacheflush)
358 				break;
359 
360 			if (vd->vdev_nowritecache) {
361 				zio->io_error = ENOTSUP;
362 				break;
363 			}
364 
365 			zio->io_vsd = dkc = kmem_alloc(sizeof (*dkc), KM_SLEEP);
366 			zio->io_vsd_ops = &vdev_disk_vsd_ops;
367 
368 			dkc->dkc_callback = vdev_disk_ioctl_done;
369 			dkc->dkc_flag = FLUSH_VOLATILE;
370 			dkc->dkc_cookie = zio;
371 
372 			error = ldi_ioctl(dvd->vd_lh, zio->io_cmd,
373 			    (uintptr_t)dkc, FKIOCTL, kcred, NULL);
374 
375 			if (error == 0) {
376 				/*
377 				 * The ioctl will be done asychronously,
378 				 * and will call vdev_disk_ioctl_done()
379 				 * upon completion.
380 				 */
381 				return (ZIO_PIPELINE_STOP);
382 			}
383 
384 			if (error == ENOTSUP || error == ENOTTY) {
385 				/*
386 				 * If we get ENOTSUP or ENOTTY, we know that
387 				 * no future attempts will ever succeed.
388 				 * In this case we set a persistent bit so
389 				 * that we don't bother with the ioctl in the
390 				 * future.
391 				 */
392 				vd->vdev_nowritecache = B_TRUE;
393 			}
394 			zio->io_error = error;
395 
396 			break;
397 
398 		default:
399 			zio->io_error = ENOTSUP;
400 		}
401 
402 		return (ZIO_PIPELINE_CONTINUE);
403 	}
404 
405 	vdb = kmem_alloc(sizeof (vdev_disk_buf_t), KM_SLEEP);
406 
407 	vdb->vdb_io = zio;
408 	bp = &vdb->vdb_buf;
409 
410 	bioinit(bp);
411 	bp->b_flags = B_BUSY | B_NOCACHE |
412 	    (zio->io_type == ZIO_TYPE_READ ? B_READ : B_WRITE);
413 	if (!(zio->io_flags & (ZIO_FLAG_IO_RETRY | ZIO_FLAG_TRYHARD)))
414 		bp->b_flags |= B_FAILFAST;
415 	bp->b_bcount = zio->io_size;
416 	bp->b_un.b_addr = zio->io_data;
417 	bp->b_lblkno = lbtodb(zio->io_offset);
418 	bp->b_bufsize = zio->io_size;
419 	bp->b_iodone = (int (*)())vdev_disk_io_intr;
420 
421 	/* ldi_strategy() will return non-zero only on programming errors */
422 	VERIFY(ldi_strategy(dvd->vd_lh, bp) == 0);
423 
424 	return (ZIO_PIPELINE_STOP);
425 }
426 
427 static void
428 vdev_disk_io_done(zio_t *zio)
429 {
430 	vdev_t *vd = zio->io_vd;
431 
432 	/*
433 	 * If the device returned EIO, then attempt a DKIOCSTATE ioctl to see if
434 	 * the device has been removed.  If this is the case, then we trigger an
435 	 * asynchronous removal of the device. Otherwise, probe the device and
436 	 * make sure it's still accessible.
437 	 */
438 	if (zio->io_error == EIO && !vd->vdev_remove_wanted) {
439 		vdev_disk_t *dvd = vd->vdev_tsd;
440 		int state = DKIO_NONE;
441 
442 		if (ldi_ioctl(dvd->vd_lh, DKIOCSTATE, (intptr_t)&state,
443 		    FKIOCTL, kcred, NULL) == 0 && state != DKIO_INSERTED) {
444 			/*
445 			 * We post the resource as soon as possible, instead of
446 			 * when the async removal actually happens, because the
447 			 * DE is using this information to discard previous I/O
448 			 * errors.
449 			 */
450 			zfs_post_remove(zio->io_spa, vd);
451 			vd->vdev_remove_wanted = B_TRUE;
452 			spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE);
453 		}
454 	}
455 }
456 
457 vdev_ops_t vdev_disk_ops = {
458 	vdev_disk_open,
459 	vdev_disk_close,
460 	vdev_default_asize,
461 	vdev_disk_io_start,
462 	vdev_disk_io_done,
463 	NULL,
464 	VDEV_TYPE_DISK,		/* name of this vdev type */
465 	B_TRUE			/* leaf vdev */
466 };
467 
468 /*
469  * Given the root disk device devid or pathname, read the label from
470  * the device, and construct a configuration nvlist.
471  */
472 int
473 vdev_disk_read_rootlabel(char *devpath, char *devid, nvlist_t **config)
474 {
475 	ldi_handle_t vd_lh;
476 	vdev_label_t *label;
477 	uint64_t s, size;
478 	int l;
479 	ddi_devid_t tmpdevid;
480 	int error = -1;
481 	char *minor_name;
482 
483 	/*
484 	 * Read the device label and build the nvlist.
485 	 */
486 	if (devid != NULL && ddi_devid_str_decode(devid, &tmpdevid,
487 	    &minor_name) == 0) {
488 		error = ldi_open_by_devid(tmpdevid, minor_name,
489 		    FREAD, kcred, &vd_lh, zfs_li);
490 		ddi_devid_free(tmpdevid);
491 		ddi_devid_str_free(minor_name);
492 	}
493 
494 	if (error && (error = ldi_open_by_name(devpath, FREAD, kcred, &vd_lh,
495 	    zfs_li)))
496 		return (error);
497 
498 	if (ldi_get_size(vd_lh, &s)) {
499 		(void) ldi_close(vd_lh, FREAD, kcred);
500 		return (EIO);
501 	}
502 
503 	size = P2ALIGN_TYPED(s, sizeof (vdev_label_t), uint64_t);
504 	label = kmem_alloc(sizeof (vdev_label_t), KM_SLEEP);
505 
506 	*config = NULL;
507 	for (l = 0; l < VDEV_LABELS; l++) {
508 		uint64_t offset, state, txg = 0;
509 
510 		/* read vdev label */
511 		offset = vdev_label_offset(size, l, 0);
512 		if (vdev_disk_physio(vd_lh, (caddr_t)label,
513 		    VDEV_SKIP_SIZE + VDEV_PHYS_SIZE, offset, B_READ) != 0)
514 			continue;
515 
516 		if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist,
517 		    sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0) {
518 			*config = NULL;
519 			continue;
520 		}
521 
522 		if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
523 		    &state) != 0 || state >= POOL_STATE_DESTROYED) {
524 			nvlist_free(*config);
525 			*config = NULL;
526 			continue;
527 		}
528 
529 		if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
530 		    &txg) != 0 || txg == 0) {
531 			nvlist_free(*config);
532 			*config = NULL;
533 			continue;
534 		}
535 
536 		break;
537 	}
538 
539 	kmem_free(label, sizeof (vdev_label_t));
540 	(void) ldi_close(vd_lh, FREAD, kcred);
541 	if (*config == NULL)
542 		error = EIDRM;
543 
544 	return (error);
545 }
546