xref: /titanic_50/usr/src/uts/common/fs/zfs/vdev_disk.c (revision e07d9cb85217949d497b02d7211de8a197d2f2eb)
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 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/zfs_context.h>
29 #include <sys/spa.h>
30 #include <sys/vdev_disk.h>
31 #include <sys/vdev_impl.h>
32 #include <sys/fs/zfs.h>
33 #include <sys/zio.h>
34 #include <sys/sunldi.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 	char *physpath, *minorname;
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 	 */
84 	if (vd->vdev_devid != NULL) {
85 		if (ddi_devid_str_decode(vd->vdev_devid, &dvd->vd_devid,
86 		    &dvd->vd_minor) != 0) {
87 			vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
88 			return (EINVAL);
89 		}
90 	}
91 
92 	error = EINVAL;		/* presume failure */
93 
94 	if (vd->vdev_path != NULL) {
95 		ddi_devid_t devid;
96 
97 		if (vd->vdev_wholedisk == -1ULL) {
98 			size_t len = strlen(vd->vdev_path) + 3;
99 			char *buf = kmem_alloc(len, KM_SLEEP);
100 			ldi_handle_t lh;
101 
102 			(void) snprintf(buf, len, "%ss0", vd->vdev_path);
103 
104 			if (ldi_open_by_name(buf, spa_mode, kcred,
105 			    &lh, zfs_li) == 0) {
106 				spa_strfree(vd->vdev_path);
107 				vd->vdev_path = buf;
108 				vd->vdev_wholedisk = 1ULL;
109 				(void) ldi_close(lh, spa_mode, kcred);
110 			} else {
111 				kmem_free(buf, len);
112 			}
113 		}
114 
115 		error = ldi_open_by_name(vd->vdev_path, spa_mode, kcred,
116 		    &dvd->vd_lh, zfs_li);
117 
118 		/*
119 		 * Compare the devid to the stored value.
120 		 */
121 		if (error == 0 && vd->vdev_devid != NULL &&
122 		    ldi_get_devid(dvd->vd_lh, &devid) == 0) {
123 			if (ddi_devid_compare(devid, dvd->vd_devid) != 0) {
124 				error = EINVAL;
125 				(void) ldi_close(dvd->vd_lh, spa_mode, kcred);
126 				dvd->vd_lh = NULL;
127 			}
128 			ddi_devid_free(devid);
129 		}
130 
131 		/*
132 		 * If we succeeded in opening the device, but 'vdev_wholedisk'
133 		 * is not yet set, then this must be a slice.
134 		 */
135 		if (error == 0 && vd->vdev_wholedisk == -1ULL)
136 			vd->vdev_wholedisk = 0;
137 	}
138 
139 	/*
140 	 * If we were unable to open by path, or the devid check fails, open by
141 	 * devid instead.
142 	 */
143 	if (error != 0 && vd->vdev_devid != NULL)
144 		error = ldi_open_by_devid(dvd->vd_devid, dvd->vd_minor,
145 		    spa_mode, kcred, &dvd->vd_lh, zfs_li);
146 
147 	/*
148 	 * If all else fails, then try opening by physical path (if available)
149 	 * or the logical path (if we failed due to the devid check).  While not
150 	 * as reliable as the devid, this will give us something, and the higher
151 	 * level vdev validation will prevent us from opening the wrong device.
152 	 */
153 	if (error) {
154 		if (vd->vdev_physpath != NULL &&
155 		    (dev = ddi_pathname_to_dev_t(vd->vdev_physpath)) != ENODEV)
156 			error = ldi_open_by_dev(&dev, OTYP_BLK, spa_mode,
157 			    kcred, &dvd->vd_lh, zfs_li);
158 
159 		/*
160 		 * Note that we don't support the legacy auto-wholedisk support
161 		 * as above.  This hasn't been used in a very long time and we
162 		 * don't need to propagate its oddities to this edge condition.
163 		 */
164 		if (error && vd->vdev_path != NULL)
165 			error = ldi_open_by_name(vd->vdev_path, spa_mode, kcred,
166 			    &dvd->vd_lh, zfs_li);
167 	}
168 
169 	if (error) {
170 		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
171 		return (error);
172 	}
173 
174 	/*
175 	 * Once a device is opened, verify that the physical device path (if
176 	 * available) is up to date.
177 	 */
178 	if (ldi_get_dev(dvd->vd_lh, &dev) == 0 &&
179 	    ldi_get_otyp(dvd->vd_lh, &otyp) == 0) {
180 		physpath = kmem_alloc(MAXPATHLEN, KM_SLEEP);
181 		minorname = NULL;
182 		if (ddi_dev_pathname(dev, otyp, physpath) == 0 &&
183 		    ldi_get_minor_name(dvd->vd_lh, &minorname) == 0 &&
184 		    (vd->vdev_physpath == NULL ||
185 		    strcmp(vd->vdev_physpath, physpath) != 0)) {
186 			if (vd->vdev_physpath)
187 				spa_strfree(vd->vdev_physpath);
188 			(void) strlcat(physpath, ":", MAXPATHLEN);
189 			(void) strlcat(physpath, minorname, MAXPATHLEN);
190 			vd->vdev_physpath = spa_strdup(physpath);
191 		}
192 		if (minorname)
193 			kmem_free(minorname, strlen(minorname) + 1);
194 		kmem_free(physpath, MAXPATHLEN);
195 	}
196 
197 	/*
198 	 * Determine the actual size of the device.
199 	 */
200 	if (ldi_get_size(dvd->vd_lh, psize) != 0) {
201 		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
202 		return (EINVAL);
203 	}
204 
205 	/*
206 	 * If we own the whole disk, try to enable disk write caching.
207 	 * We ignore errors because it's OK if we can't do it.
208 	 */
209 	if (vd->vdev_wholedisk == 1) {
210 		int wce = 1;
211 		(void) ldi_ioctl(dvd->vd_lh, DKIOCSETWCE, (intptr_t)&wce,
212 		    FKIOCTL, kcred, NULL);
213 	}
214 
215 	/*
216 	 * Determine the device's minimum transfer size.
217 	 * If the ioctl isn't supported, assume DEV_BSIZE.
218 	 */
219 	if (ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFO, (intptr_t)&dkm,
220 	    FKIOCTL, kcred, NULL) != 0)
221 		dkm.dki_lbsize = DEV_BSIZE;
222 
223 	*ashift = highbit(MAX(dkm.dki_lbsize, SPA_MINBLOCKSIZE)) - 1;
224 
225 	/*
226 	 * Clear the nowritecache bit, so that on a vdev_reopen() we will
227 	 * try again.
228 	 */
229 	vd->vdev_nowritecache = B_FALSE;
230 
231 	return (0);
232 }
233 
234 static void
235 vdev_disk_close(vdev_t *vd)
236 {
237 	vdev_disk_t *dvd = vd->vdev_tsd;
238 
239 	if (dvd == NULL)
240 		return;
241 
242 	if (dvd->vd_minor != NULL)
243 		ddi_devid_str_free(dvd->vd_minor);
244 
245 	if (dvd->vd_devid != NULL)
246 		ddi_devid_free(dvd->vd_devid);
247 
248 	if (dvd->vd_lh != NULL)
249 		(void) ldi_close(dvd->vd_lh, spa_mode, kcred);
250 
251 	kmem_free(dvd, sizeof (vdev_disk_t));
252 	vd->vdev_tsd = NULL;
253 }
254 
255 static void
256 vdev_disk_io_intr(buf_t *bp)
257 {
258 	vdev_disk_buf_t *vdb = (vdev_disk_buf_t *)bp;
259 	zio_t *zio = vdb->vdb_io;
260 
261 	if ((zio->io_error = geterror(bp)) == 0 && bp->b_resid != 0)
262 		zio->io_error = EIO;
263 
264 	kmem_free(vdb, sizeof (vdev_disk_buf_t));
265 
266 	zio_next_stage_async(zio);
267 }
268 
269 static void
270 vdev_disk_ioctl_done(void *zio_arg, int error)
271 {
272 	zio_t *zio = zio_arg;
273 
274 	zio->io_error = error;
275 
276 	zio_next_stage_async(zio);
277 }
278 
279 static void
280 vdev_disk_io_start(zio_t *zio)
281 {
282 	vdev_t *vd = zio->io_vd;
283 	vdev_disk_t *dvd = vd->vdev_tsd;
284 	vdev_disk_buf_t *vdb;
285 	buf_t *bp;
286 	int flags, error;
287 
288 	if (zio->io_type == ZIO_TYPE_IOCTL) {
289 		zio_vdev_io_bypass(zio);
290 
291 		/* XXPOLICY */
292 		if (vdev_is_dead(vd)) {
293 			zio->io_error = ENXIO;
294 			zio_next_stage_async(zio);
295 			return;
296 		}
297 
298 		switch (zio->io_cmd) {
299 
300 		case DKIOCFLUSHWRITECACHE:
301 
302 			if (zfs_nocacheflush)
303 				break;
304 
305 			if (vd->vdev_nowritecache) {
306 				zio->io_error = ENOTSUP;
307 				break;
308 			}
309 
310 			zio->io_dk_callback.dkc_callback = vdev_disk_ioctl_done;
311 			zio->io_dk_callback.dkc_cookie = zio;
312 
313 			error = ldi_ioctl(dvd->vd_lh, zio->io_cmd,
314 			    (uintptr_t)&zio->io_dk_callback,
315 			    FKIOCTL, kcred, NULL);
316 
317 			if (error == 0) {
318 				/*
319 				 * The ioctl will be done asychronously,
320 				 * and will call vdev_disk_ioctl_done()
321 				 * upon completion.
322 				 */
323 				return;
324 			} else if (error == ENOTSUP || error == ENOTTY) {
325 				/*
326 				 * If we get ENOTSUP or ENOTTY, we know that
327 				 * no future attempts will ever succeed.
328 				 * In this case we set a persistent bit so
329 				 * that we don't bother with the ioctl in the
330 				 * future.
331 				 */
332 				vd->vdev_nowritecache = B_TRUE;
333 			}
334 			zio->io_error = error;
335 
336 			break;
337 
338 		default:
339 			zio->io_error = ENOTSUP;
340 		}
341 
342 		zio_next_stage_async(zio);
343 		return;
344 	}
345 
346 	if (zio->io_type == ZIO_TYPE_READ && vdev_cache_read(zio) == 0)
347 		return;
348 
349 	if ((zio = vdev_queue_io(zio)) == NULL)
350 		return;
351 
352 	flags = (zio->io_type == ZIO_TYPE_READ ? B_READ : B_WRITE);
353 	flags |= B_BUSY | B_NOCACHE;
354 	if (zio->io_flags & ZIO_FLAG_FAILFAST)
355 		flags |= B_FAILFAST;
356 
357 	vdb = kmem_alloc(sizeof (vdev_disk_buf_t), KM_SLEEP);
358 
359 	vdb->vdb_io = zio;
360 	bp = &vdb->vdb_buf;
361 
362 	bioinit(bp);
363 	bp->b_flags = flags;
364 	bp->b_bcount = zio->io_size;
365 	bp->b_un.b_addr = zio->io_data;
366 	bp->b_lblkno = lbtodb(zio->io_offset);
367 	bp->b_bufsize = zio->io_size;
368 	bp->b_iodone = (int (*)())vdev_disk_io_intr;
369 
370 	/* XXPOLICY */
371 	error = vdev_is_dead(vd) ? ENXIO : vdev_error_inject(vd, zio);
372 	if (error) {
373 		zio->io_error = error;
374 		bioerror(bp, error);
375 		bp->b_resid = bp->b_bcount;
376 		bp->b_iodone(bp);
377 		return;
378 	}
379 
380 	error = ldi_strategy(dvd->vd_lh, bp);
381 	/* ldi_strategy() will return non-zero only on programming errors */
382 	ASSERT(error == 0);
383 }
384 
385 static void
386 vdev_disk_io_done(zio_t *zio)
387 {
388 	vdev_t *vd = zio->io_vd;
389 	vdev_disk_t *dvd = vd->vdev_tsd;
390 	int state;
391 
392 	vdev_queue_io_done(zio);
393 
394 	if (zio->io_type == ZIO_TYPE_WRITE)
395 		vdev_cache_write(zio);
396 
397 	if (zio_injection_enabled && zio->io_error == 0)
398 		zio->io_error = zio_handle_device_injection(zio->io_vd, EIO);
399 
400 	/*
401 	 * If the device returned EIO, then attempt a DKIOCSTATE ioctl to see if
402 	 * the device has been removed.  If this is the case, then we trigger an
403 	 * asynchronous removal of the device.
404 	 */
405 	if (zio->io_error == EIO) {
406 		state = DKIO_NONE;
407 		if (ldi_ioctl(dvd->vd_lh, DKIOCSTATE, (intptr_t)&state,
408 		    FKIOCTL, kcred, NULL) == 0 &&
409 		    state != DKIO_INSERTED) {
410 			vd->vdev_remove_wanted = B_TRUE;
411 			spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE);
412 		}
413 	}
414 
415 	zio_next_stage(zio);
416 }
417 
418 vdev_ops_t vdev_disk_ops = {
419 	vdev_disk_open,
420 	vdev_disk_close,
421 	vdev_default_asize,
422 	vdev_disk_io_start,
423 	vdev_disk_io_done,
424 	NULL,
425 	VDEV_TYPE_DISK,		/* name of this vdev type */
426 	B_TRUE			/* leaf vdev */
427 };
428