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