xref: /titanic_44/usr/src/uts/common/fs/zfs/vdev_disk.c (revision b81bb599d2182818b41e3bbe6d4d1771b9d484d4)
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 2006 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 /*
41  * Tunable parameter for debugging or performance analysis.  Setting
42  * zfs_nocacheflush will cause corruption on power loss if a volatile
43  * out-of-order write cache is enabled.
44  */
45 boolean_t zfs_nocacheflush = B_FALSE;
46 
47 extern ldi_ident_t zfs_li;
48 
49 typedef struct vdev_disk_buf {
50 	buf_t	vdb_buf;
51 	zio_t	*vdb_io;
52 } vdev_disk_buf_t;
53 
54 static int
55 vdev_disk_open(vdev_t *vd, uint64_t *psize, uint64_t *ashift)
56 {
57 	vdev_disk_t *dvd;
58 	struct dk_minfo dkm;
59 	int error;
60 
61 	/*
62 	 * We must have a pathname, and it must be absolute.
63 	 */
64 	if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
65 		vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
66 		return (EINVAL);
67 	}
68 
69 	dvd = vd->vdev_tsd = kmem_zalloc(sizeof (vdev_disk_t), KM_SLEEP);
70 
71 	/*
72 	 * When opening a disk device, we want to preserve the user's original
73 	 * intent.  We always want to open the device by the path the user gave
74 	 * us, even if it is one of multiple paths to the save device.  But we
75 	 * also want to be able to survive disks being removed/recabled.
76 	 * Therefore the sequence of opening devices is:
77 	 *
78 	 * 1. Try opening the device by path.  For legacy pools without the
79 	 *    'whole_disk' property, attempt to fix the path by appending 's0'.
80 	 *
81 	 * 2. If the devid of the device matches the stored value, return
82 	 *    success.
83 	 *
84 	 * 3. Otherwise, the device may have moved.  Try opening the device
85 	 *    by the devid instead.
86 	 *
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) {
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 	if (error) {
152 		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
153 		return (error);
154 	}
155 
156 	/*
157 	 * Determine the actual size of the device.
158 	 */
159 	if (ldi_get_size(dvd->vd_lh, psize) != 0) {
160 		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
161 		return (EINVAL);
162 	}
163 
164 	/*
165 	 * If we own the whole disk, try to enable disk write caching.
166 	 * We ignore errors because it's OK if we can't do it.
167 	 */
168 	if (vd->vdev_wholedisk == 1) {
169 		int wce = 1;
170 		(void) ldi_ioctl(dvd->vd_lh, DKIOCSETWCE, (intptr_t)&wce,
171 		    FKIOCTL, kcred, NULL);
172 	}
173 
174 	/*
175 	 * Determine the device's minimum transfer size.
176 	 * If the ioctl isn't supported, assume DEV_BSIZE.
177 	 */
178 	if (ldi_ioctl(dvd->vd_lh, DKIOCGMEDIAINFO, (intptr_t)&dkm,
179 	    FKIOCTL, kcred, NULL) != 0)
180 		dkm.dki_lbsize = DEV_BSIZE;
181 
182 	*ashift = highbit(MAX(dkm.dki_lbsize, SPA_MINBLOCKSIZE)) - 1;
183 
184 	/*
185 	 * Clear the nowritecache bit, so that on a vdev_reopen() we will
186 	 * try again.
187 	 */
188 	vd->vdev_nowritecache = B_FALSE;
189 
190 	return (0);
191 }
192 
193 static void
194 vdev_disk_close(vdev_t *vd)
195 {
196 	vdev_disk_t *dvd = vd->vdev_tsd;
197 
198 	if (dvd == NULL)
199 		return;
200 
201 	dprintf("removing disk %s, devid %s\n",
202 	    vd->vdev_path ? vd->vdev_path : "<none>",
203 	    vd->vdev_devid ? vd->vdev_devid : "<none>");
204 
205 	if (dvd->vd_minor != NULL)
206 		ddi_devid_str_free(dvd->vd_minor);
207 
208 	if (dvd->vd_devid != NULL)
209 		ddi_devid_free(dvd->vd_devid);
210 
211 	if (dvd->vd_lh != NULL)
212 		(void) ldi_close(dvd->vd_lh, spa_mode, kcred);
213 
214 	kmem_free(dvd, sizeof (vdev_disk_t));
215 	vd->vdev_tsd = NULL;
216 }
217 
218 static void
219 vdev_disk_io_intr(buf_t *bp)
220 {
221 	vdev_disk_buf_t *vdb = (vdev_disk_buf_t *)bp;
222 	zio_t *zio = vdb->vdb_io;
223 
224 	if ((zio->io_error = geterror(bp)) == 0 && bp->b_resid != 0)
225 		zio->io_error = EIO;
226 
227 	kmem_free(vdb, sizeof (vdev_disk_buf_t));
228 
229 	zio_next_stage_async(zio);
230 }
231 
232 static void
233 vdev_disk_ioctl_done(void *zio_arg, int error)
234 {
235 	zio_t *zio = zio_arg;
236 
237 	zio->io_error = error;
238 
239 	zio_next_stage_async(zio);
240 }
241 
242 static void
243 vdev_disk_io_start(zio_t *zio)
244 {
245 	vdev_t *vd = zio->io_vd;
246 	vdev_disk_t *dvd = vd->vdev_tsd;
247 	vdev_disk_buf_t *vdb;
248 	buf_t *bp;
249 	int flags, error;
250 
251 	if (zio->io_type == ZIO_TYPE_IOCTL) {
252 		zio_vdev_io_bypass(zio);
253 
254 		/* XXPOLICY */
255 		if (vdev_is_dead(vd)) {
256 			zio->io_error = ENXIO;
257 			zio_next_stage_async(zio);
258 			return;
259 		}
260 
261 		switch (zio->io_cmd) {
262 
263 		case DKIOCFLUSHWRITECACHE:
264 
265 			if (zfs_nocacheflush)
266 				break;
267 
268 			if (vd->vdev_nowritecache) {
269 				zio->io_error = ENOTSUP;
270 				break;
271 			}
272 
273 			zio->io_dk_callback.dkc_callback = vdev_disk_ioctl_done;
274 			zio->io_dk_callback.dkc_cookie = zio;
275 
276 			error = ldi_ioctl(dvd->vd_lh, zio->io_cmd,
277 			    (uintptr_t)&zio->io_dk_callback,
278 			    FKIOCTL, kcred, NULL);
279 
280 			if (error == 0) {
281 				/*
282 				 * The ioctl will be done asychronously,
283 				 * and will call vdev_disk_ioctl_done()
284 				 * upon completion.
285 				 */
286 				return;
287 			} else if (error == ENOTSUP) {
288 				/*
289 				 * If we get ENOTSUP, we know that no future
290 				 * attempts will ever succeed.  In this case we
291 				 * set a persistent bit so that we don't bother
292 				 * with the ioctl in the future.
293 				 */
294 				vd->vdev_nowritecache = B_TRUE;
295 			}
296 			zio->io_error = error;
297 
298 			break;
299 
300 		default:
301 			zio->io_error = ENOTSUP;
302 		}
303 
304 		zio_next_stage_async(zio);
305 		return;
306 	}
307 
308 	if (zio->io_type == ZIO_TYPE_READ && vdev_cache_read(zio) == 0)
309 		return;
310 
311 	if ((zio = vdev_queue_io(zio)) == NULL)
312 		return;
313 
314 	flags = (zio->io_type == ZIO_TYPE_READ ? B_READ : B_WRITE);
315 	flags |= B_BUSY | B_NOCACHE;
316 	if (zio->io_flags & ZIO_FLAG_FAILFAST)
317 		flags |= B_FAILFAST;
318 
319 	vdb = kmem_alloc(sizeof (vdev_disk_buf_t), KM_SLEEP);
320 
321 	vdb->vdb_io = zio;
322 	bp = &vdb->vdb_buf;
323 
324 	bioinit(bp);
325 	bp->b_flags = flags;
326 	bp->b_bcount = zio->io_size;
327 	bp->b_un.b_addr = zio->io_data;
328 	bp->b_lblkno = lbtodb(zio->io_offset);
329 	bp->b_bufsize = zio->io_size;
330 	bp->b_iodone = (int (*)())vdev_disk_io_intr;
331 
332 	/* XXPOLICY */
333 	error = vdev_is_dead(vd) ? ENXIO : vdev_error_inject(vd, zio);
334 	if (error) {
335 		zio->io_error = error;
336 		bioerror(bp, error);
337 		bp->b_resid = bp->b_bcount;
338 		bp->b_iodone(bp);
339 		return;
340 	}
341 
342 	error = ldi_strategy(dvd->vd_lh, bp);
343 	/* ldi_strategy() will return non-zero only on programming errors */
344 	ASSERT(error == 0);
345 }
346 
347 static void
348 vdev_disk_io_done(zio_t *zio)
349 {
350 	vdev_queue_io_done(zio);
351 
352 	if (zio->io_type == ZIO_TYPE_WRITE)
353 		vdev_cache_write(zio);
354 
355 	if (zio_injection_enabled && zio->io_error == 0)
356 		zio->io_error = zio_handle_device_injection(zio->io_vd, EIO);
357 
358 	zio_next_stage(zio);
359 }
360 
361 vdev_ops_t vdev_disk_ops = {
362 	vdev_disk_open,
363 	vdev_disk_close,
364 	vdev_default_asize,
365 	vdev_disk_io_start,
366 	vdev_disk_io_done,
367 	NULL,
368 	VDEV_TYPE_DISK,		/* name of this vdev type */
369 	B_TRUE			/* leaf vdev */
370 };
371