xref: /titanic_44/usr/src/uts/common/fs/zfs/vdev_disk.c (revision 349b53dd4e695e3d833b5380540385145b2d3ae8)
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 void
322 vdev_disk_ioctl_done(void *zio_arg, int error)
323 {
324 	zio_t *zio = zio_arg;
325 
326 	zio->io_error = error;
327 
328 	zio_interrupt(zio);
329 }
330 
331 static int
332 vdev_disk_io_start(zio_t *zio)
333 {
334 	vdev_t *vd = zio->io_vd;
335 	vdev_disk_t *dvd = vd->vdev_tsd;
336 	vdev_disk_buf_t *vdb;
337 	struct dk_callback *dkc;
338 	buf_t *bp;
339 	int error;
340 
341 	if (zio->io_type == ZIO_TYPE_IOCTL) {
342 		/* XXPOLICY */
343 		if (!vdev_readable(vd)) {
344 			zio->io_error = ENXIO;
345 			return (ZIO_PIPELINE_CONTINUE);
346 		}
347 
348 		switch (zio->io_cmd) {
349 
350 		case DKIOCFLUSHWRITECACHE:
351 
352 			if (zfs_nocacheflush)
353 				break;
354 
355 			if (vd->vdev_nowritecache) {
356 				zio->io_error = ENOTSUP;
357 				break;
358 			}
359 
360 			zio->io_vsd = dkc = kmem_alloc(sizeof (*dkc), KM_SLEEP);
361 			zio->io_vsd_free = vdev_disk_ioctl_free;
362 
363 			dkc->dkc_callback = vdev_disk_ioctl_done;
364 			dkc->dkc_flag = FLUSH_VOLATILE;
365 			dkc->dkc_cookie = zio;
366 
367 			error = ldi_ioctl(dvd->vd_lh, zio->io_cmd,
368 			    (uintptr_t)dkc, FKIOCTL, kcred, NULL);
369 
370 			if (error == 0) {
371 				/*
372 				 * The ioctl will be done asychronously,
373 				 * and will call vdev_disk_ioctl_done()
374 				 * upon completion.
375 				 */
376 				return (ZIO_PIPELINE_STOP);
377 			}
378 
379 			if (error == ENOTSUP || error == ENOTTY) {
380 				/*
381 				 * If we get ENOTSUP or ENOTTY, we know that
382 				 * no future attempts will ever succeed.
383 				 * In this case we set a persistent bit so
384 				 * that we don't bother with the ioctl in the
385 				 * future.
386 				 */
387 				vd->vdev_nowritecache = B_TRUE;
388 			}
389 			zio->io_error = error;
390 
391 			break;
392 
393 		default:
394 			zio->io_error = ENOTSUP;
395 		}
396 
397 		return (ZIO_PIPELINE_CONTINUE);
398 	}
399 
400 	vdb = kmem_alloc(sizeof (vdev_disk_buf_t), KM_SLEEP);
401 
402 	vdb->vdb_io = zio;
403 	bp = &vdb->vdb_buf;
404 
405 	bioinit(bp);
406 	bp->b_flags = B_BUSY | B_NOCACHE |
407 	    (zio->io_type == ZIO_TYPE_READ ? B_READ : B_WRITE);
408 	if (!(zio->io_flags & (ZIO_FLAG_IO_RETRY | ZIO_FLAG_TRYHARD)))
409 		bp->b_flags |= B_FAILFAST;
410 	bp->b_bcount = zio->io_size;
411 	bp->b_un.b_addr = zio->io_data;
412 	bp->b_lblkno = lbtodb(zio->io_offset);
413 	bp->b_bufsize = zio->io_size;
414 	bp->b_iodone = (int (*)())vdev_disk_io_intr;
415 
416 	/* ldi_strategy() will return non-zero only on programming errors */
417 	VERIFY(ldi_strategy(dvd->vd_lh, bp) == 0);
418 
419 	return (ZIO_PIPELINE_STOP);
420 }
421 
422 static void
423 vdev_disk_io_done(zio_t *zio)
424 {
425 	vdev_t *vd = zio->io_vd;
426 
427 	/*
428 	 * If the device returned EIO, then attempt a DKIOCSTATE ioctl to see if
429 	 * the device has been removed.  If this is the case, then we trigger an
430 	 * asynchronous removal of the device. Otherwise, probe the device and
431 	 * make sure it's still accessible.
432 	 */
433 	if (zio->io_error == EIO) {
434 		vdev_disk_t *dvd = vd->vdev_tsd;
435 		int state = DKIO_NONE;
436 
437 		if (ldi_ioctl(dvd->vd_lh, DKIOCSTATE, (intptr_t)&state,
438 		    FKIOCTL, kcred, NULL) == 0 && state != DKIO_INSERTED) {
439 			vd->vdev_remove_wanted = B_TRUE;
440 			spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE);
441 		}
442 	}
443 }
444 
445 vdev_ops_t vdev_disk_ops = {
446 	vdev_disk_open,
447 	vdev_disk_close,
448 	vdev_default_asize,
449 	vdev_disk_io_start,
450 	vdev_disk_io_done,
451 	NULL,
452 	VDEV_TYPE_DISK,		/* name of this vdev type */
453 	B_TRUE			/* leaf vdev */
454 };
455 
456 /*
457  * Given the root disk device devid or pathname, read the label from
458  * the device, and construct a configuration nvlist.
459  */
460 int
461 vdev_disk_read_rootlabel(char *devpath, char *devid, nvlist_t **config)
462 {
463 	ldi_handle_t vd_lh;
464 	vdev_label_t *label;
465 	uint64_t s, size;
466 	int l;
467 	ddi_devid_t tmpdevid;
468 	int error = -1;
469 	char *minor_name;
470 
471 	/*
472 	 * Read the device label and build the nvlist.
473 	 */
474 	if (devid != NULL && ddi_devid_str_decode(devid, &tmpdevid,
475 	    &minor_name) == 0) {
476 		error = ldi_open_by_devid(tmpdevid, minor_name,
477 		    FREAD, kcred, &vd_lh, zfs_li);
478 		ddi_devid_free(tmpdevid);
479 		ddi_devid_str_free(minor_name);
480 	}
481 
482 	if (error && (error = ldi_open_by_name(devpath, FREAD, kcred, &vd_lh,
483 	    zfs_li)))
484 		return (error);
485 
486 	if (ldi_get_size(vd_lh, &s)) {
487 		(void) ldi_close(vd_lh, FREAD, kcred);
488 		return (EIO);
489 	}
490 
491 	size = P2ALIGN_TYPED(s, sizeof (vdev_label_t), uint64_t);
492 	label = kmem_alloc(sizeof (vdev_label_t), KM_SLEEP);
493 
494 	*config = NULL;
495 	for (l = 0; l < VDEV_LABELS; l++) {
496 		uint64_t offset, state, txg = 0;
497 
498 		/* read vdev label */
499 		offset = vdev_label_offset(size, l, 0);
500 		if (vdev_disk_physio(vd_lh, (caddr_t)label,
501 		    VDEV_SKIP_SIZE + VDEV_PHYS_SIZE, offset, B_READ) != 0)
502 			continue;
503 
504 		if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist,
505 		    sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0) {
506 			*config = NULL;
507 			continue;
508 		}
509 
510 		if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
511 		    &state) != 0 || state >= POOL_STATE_DESTROYED) {
512 			nvlist_free(*config);
513 			*config = NULL;
514 			continue;
515 		}
516 
517 		if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
518 		    &txg) != 0 || txg == 0) {
519 			nvlist_free(*config);
520 			*config = NULL;
521 			continue;
522 		}
523 
524 		break;
525 	}
526 
527 	kmem_free(label, sizeof (vdev_label_t));
528 	(void) ldi_close(vd_lh, FREAD, kcred);
529 	if (*config == NULL)
530 		error = EIDRM;
531 
532 	return (error);
533 }
534