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