Sun Dec 8 14:39:07 2019 UTC ()
Pull up following revision(s) (requested by jmcneill in ticket #514):

	external/cddl/osnet/dist/uts/common/fs/zfs/vdev_disk.c: revision 1.13

Initialize b_dev before passing buf to d_minphys (ldminphys needs this)


(martin)
diff -r1.11.2.1 -r1.11.2.2 src/external/cddl/osnet/dist/uts/common/fs/zfs/vdev_disk.c

cvs diff -r1.11.2.1 -r1.11.2.2 src/external/cddl/osnet/dist/uts/common/fs/zfs/vdev_disk.c (switch to unified diff)

--- src/external/cddl/osnet/dist/uts/common/fs/zfs/vdev_disk.c 2019/08/20 11:44:14 1.11.2.1
+++ src/external/cddl/osnet/dist/uts/common/fs/zfs/vdev_disk.c 2019/12/08 14:39:07 1.11.2.2
@@ -1,659 +1,660 @@ @@ -1,659 +1,660 @@
1/* 1/*
2 * CDDL HEADER START 2 * CDDL HEADER START
3 * 3 *
4 * The contents of this file are subject to the terms of the 4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License"). 5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License. 6 * You may not use this file except in compliance with the License.
7 * 7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing. 9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions 10 * See the License for the specific language governing permissions
11 * and limitations under the License. 11 * and limitations under the License.
12 * 12 *
13 * When distributing Covered Code, include this CDDL HEADER in each 13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the 15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner] 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 * 18 *
19 * CDDL HEADER END 19 * CDDL HEADER END
20 */ 20 */
21/* 21/*
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2012, 2015 by Delphix. All rights reserved. 23 * Copyright (c) 2012, 2015 by Delphix. All rights reserved.
24 * Copyright 2013 Nexenta Systems, Inc. All rights reserved. 24 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
25 * Copyright (c) 2013 Joyent, Inc. All rights reserved. 25 * Copyright (c) 2013 Joyent, Inc. All rights reserved.
26 */ 26 */
27 27
28#include <sys/zfs_context.h> 28#include <sys/zfs_context.h>
29#include <sys/spa.h> 29#include <sys/spa.h>
30#include <sys/refcount.h> 30#include <sys/refcount.h>
31#include <sys/vdev_disk.h> 31#include <sys/vdev_disk.h>
32#include <sys/vdev_impl.h> 32#include <sys/vdev_impl.h>
33#include <sys/fs/zfs.h> 33#include <sys/fs/zfs.h>
34#include <sys/zio.h> 34#include <sys/zio.h>
35#include <sys/sunldi.h> 35#include <sys/sunldi.h>
36#include <sys/fm/fs/zfs.h> 36#include <sys/fm/fs/zfs.h>
37#include <sys/disk.h> 37#include <sys/disk.h>
38#include <sys/disklabel.h> 38#include <sys/disklabel.h>
39#include <sys/dkio.h> 39#include <sys/dkio.h>
40#include <sys/workqueue.h> 40#include <sys/workqueue.h>
41 41
42#ifdef __NetBSD__ 42#ifdef __NetBSD__
43static int 43static int
44geterror(struct buf *bp) 44geterror(struct buf *bp)
45{ 45{
46 46
47 return (bp->b_error); 47 return (bp->b_error);
48} 48}
49#endif 49#endif
50 50
51/* 51/*
52 * Virtual device vector for disks. 52 * Virtual device vector for disks.
53 */ 53 */
54 54
55static void vdev_disk_io_intr(buf_t *); 55static void vdev_disk_io_intr(buf_t *);
56 56
57static void 57static void
58vdev_disk_alloc(vdev_t *vd) 58vdev_disk_alloc(vdev_t *vd)
59{ 59{
60 vdev_disk_t *dvd; 60 vdev_disk_t *dvd;
61 61
62 dvd = vd->vdev_tsd = kmem_zalloc(sizeof (vdev_disk_t), KM_SLEEP); 62 dvd = vd->vdev_tsd = kmem_zalloc(sizeof (vdev_disk_t), KM_SLEEP);
63 63
64#ifdef illumos 64#ifdef illumos
65 /* 65 /*
66 * Create the LDI event callback list. 66 * Create the LDI event callback list.
67 */ 67 */
68 list_create(&dvd->vd_ldi_cbs, sizeof (vdev_disk_ldi_cb_t), 68 list_create(&dvd->vd_ldi_cbs, sizeof (vdev_disk_ldi_cb_t),
69 offsetof(vdev_disk_ldi_cb_t, lcb_next)); 69 offsetof(vdev_disk_ldi_cb_t, lcb_next));
70#endif 70#endif
71} 71}
72 72
73 73
74static void 74static void
75vdev_disk_free(vdev_t *vd) 75vdev_disk_free(vdev_t *vd)
76{ 76{
77 vdev_disk_t *dvd = vd->vdev_tsd; 77 vdev_disk_t *dvd = vd->vdev_tsd;
78#ifdef illumos 78#ifdef illumos
79 vdev_disk_ldi_cb_t *lcb; 79 vdev_disk_ldi_cb_t *lcb;
80#endif 80#endif
81 81
82 if (dvd == NULL) 82 if (dvd == NULL)
83 return; 83 return;
84 84
85#ifdef illumos 85#ifdef illumos
86 /* 86 /*
87 * We have already closed the LDI handle. Clean up the LDI event 87 * We have already closed the LDI handle. Clean up the LDI event
88 * callbacks and free vd->vdev_tsd. 88 * callbacks and free vd->vdev_tsd.
89 */ 89 */
90 while ((lcb = list_head(&dvd->vd_ldi_cbs)) != NULL) { 90 while ((lcb = list_head(&dvd->vd_ldi_cbs)) != NULL) {
91 list_remove(&dvd->vd_ldi_cbs, lcb); 91 list_remove(&dvd->vd_ldi_cbs, lcb);
92 (void) ldi_ev_remove_callbacks(lcb->lcb_id); 92 (void) ldi_ev_remove_callbacks(lcb->lcb_id);
93 kmem_free(lcb, sizeof (vdev_disk_ldi_cb_t)); 93 kmem_free(lcb, sizeof (vdev_disk_ldi_cb_t));
94 } 94 }
95 list_destroy(&dvd->vd_ldi_cbs); 95 list_destroy(&dvd->vd_ldi_cbs);
96#endif 96#endif
97 kmem_free(dvd, sizeof (vdev_disk_t)); 97 kmem_free(dvd, sizeof (vdev_disk_t));
98 vd->vdev_tsd = NULL; 98 vd->vdev_tsd = NULL;
99} 99}
100 100
101 101
102/* 102/*
103 * It's not clear what these hold/rele functions are supposed to do. 103 * It's not clear what these hold/rele functions are supposed to do.
104 */ 104 */
105static void 105static void
106vdev_disk_hold(vdev_t *vd) 106vdev_disk_hold(vdev_t *vd)
107{ 107{
108 108
109 ASSERT(spa_config_held(vd->vdev_spa, SCL_STATE, RW_WRITER)); 109 ASSERT(spa_config_held(vd->vdev_spa, SCL_STATE, RW_WRITER));
110 110
111} 111}
112 112
113static void 113static void
114vdev_disk_rele(vdev_t *vd) 114vdev_disk_rele(vdev_t *vd)
115{ 115{
116 116
117 ASSERT(spa_config_held(vd->vdev_spa, SCL_STATE, RW_WRITER)); 117 ASSERT(spa_config_held(vd->vdev_spa, SCL_STATE, RW_WRITER));
118 118
119} 119}
120 120
121static void 121static void
122vdev_disk_flush(struct work *work, void *cookie) 122vdev_disk_flush(struct work *work, void *cookie)
123{ 123{
124 vdev_disk_t *dvd; 124 vdev_disk_t *dvd;
125 int error, cmd; 125 int error, cmd;
126 buf_t *bp; 126 buf_t *bp;
127 vnode_t *vp; 127 vnode_t *vp;
128 128
129 bp = (struct buf *)work; 129 bp = (struct buf *)work;
130 vp = bp->b_vp; 130 vp = bp->b_vp;
131 dvd = cookie; 131 dvd = cookie;
132 132
133 KASSERT(vp == dvd->vd_vp); 133 KASSERT(vp == dvd->vd_vp);
134 134
135 cmd = 1; 135 cmd = 1;
136 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 136 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
137 error = VOP_IOCTL(vp, DIOCCACHESYNC, &cmd, FREAD|FWRITE, kcred); 137 error = VOP_IOCTL(vp, DIOCCACHESYNC, &cmd, FREAD|FWRITE, kcred);
138 VOP_UNLOCK(vp, 0); 138 VOP_UNLOCK(vp, 0);
139 bp->b_error = error; 139 bp->b_error = error;
140 vdev_disk_io_intr(bp); 140 vdev_disk_io_intr(bp);
141} 141}
142 142
143static int 143static int
144vdev_disk_open(vdev_t *vd, uint64_t *psize, uint64_t *max_psize, 144vdev_disk_open(vdev_t *vd, uint64_t *psize, uint64_t *max_psize,
145 uint64_t *ashift, uint64_t *pashift) 145 uint64_t *ashift, uint64_t *pashift)
146{ 146{
147 spa_t *spa = vd->vdev_spa; 147 spa_t *spa = vd->vdev_spa;
148 vdev_disk_t *dvd; 148 vdev_disk_t *dvd;
149 vnode_t *vp; 149 vnode_t *vp;
150 struct dkwedge_info dkw; 150 struct dkwedge_info dkw;
151 struct disk *pdk; 151 struct disk *pdk;
152 int error, cmd; 152 int error, cmd;
153 struct partinfo pinfo; 153 struct partinfo pinfo;
154 154
155 /* 155 /*
156 * We must have a pathname, and it must be absolute. 156 * We must have a pathname, and it must be absolute.
157 */ 157 */
158 if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') { 158 if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
159 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL; 159 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
160 return (SET_ERROR(EINVAL)); 160 return (SET_ERROR(EINVAL));
161 } 161 }
162 162
163 /* 163 /*
164 * Reopen the device if it's not currently open. Otherwise, 164 * Reopen the device if it's not currently open. Otherwise,
165 * just update the physical size of the device. 165 * just update the physical size of the device.
166 */ 166 */
167 if (vd->vdev_tsd != NULL) { 167 if (vd->vdev_tsd != NULL) {
168 ASSERT(vd->vdev_reopening); 168 ASSERT(vd->vdev_reopening);
169 dvd = vd->vdev_tsd; 169 dvd = vd->vdev_tsd;
170 vp = dvd->vd_vp; 170 vp = dvd->vd_vp;
171 KASSERT(vp != NULL); 171 KASSERT(vp != NULL);
172 goto skip_open; 172 goto skip_open;
173 } 173 }
174 174
175 /* 175 /*
176 * Create vd->vdev_tsd. 176 * Create vd->vdev_tsd.
177 */ 177 */
178 vdev_disk_alloc(vd); 178 vdev_disk_alloc(vd);
179 dvd = vd->vdev_tsd; 179 dvd = vd->vdev_tsd;
180 180
181 /* 181 /*
182 * When opening a disk device, we want to preserve the user's original 182 * When opening a disk device, we want to preserve the user's original
183 * intent. We always want to open the device by the path the user gave 183 * intent. We always want to open the device by the path the user gave
184 * us, even if it is one of multiple paths to the save device. But we 184 * us, even if it is one of multiple paths to the save device. But we
185 * also want to be able to survive disks being removed/recabled. 185 * also want to be able to survive disks being removed/recabled.
186 * Therefore the sequence of opening devices is: 186 * Therefore the sequence of opening devices is:
187 * 187 *
188 * 1. Try opening the device by path. For legacy pools without the 188 * 1. Try opening the device by path. For legacy pools without the
189 * 'whole_disk' property, attempt to fix the path by appending 's0'. 189 * 'whole_disk' property, attempt to fix the path by appending 's0'.
190 * 190 *
191 * 2. If the devid of the device matches the stored value, return 191 * 2. If the devid of the device matches the stored value, return
192 * success. 192 * success.
193 * 193 *
194 * 3. Otherwise, the device may have moved. Try opening the device 194 * 3. Otherwise, the device may have moved. Try opening the device
195 * by the devid instead. 195 * by the devid instead.
196 */ 196 */
197 if (vd->vdev_devid != NULL) { 197 if (vd->vdev_devid != NULL) {
198 /* XXXNETBSD wedges */ 198 /* XXXNETBSD wedges */
199#ifdef illumos 199#ifdef illumos
200 if (ddi_devid_str_decode(vd->vdev_devid, &dvd->vd_devid, 200 if (ddi_devid_str_decode(vd->vdev_devid, &dvd->vd_devid,
201 &dvd->vd_minor) != 0) { 201 &dvd->vd_minor) != 0) {
202 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL; 202 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
203 return (SET_ERROR(EINVAL)); 203 return (SET_ERROR(EINVAL));
204 } 204 }
205#endif 205#endif
206 } 206 }
207 207
208 error = EINVAL; /* presume failure */ 208 error = EINVAL; /* presume failure */
209 209
210 error = vn_open(vd->vdev_path, UIO_SYSSPACE, FREAD|FWRITE, 0, 210 error = vn_open(vd->vdev_path, UIO_SYSSPACE, FREAD|FWRITE, 0,
211 &vp, CRCREAT, 0); 211 &vp, CRCREAT, 0);
212 if (error != 0) { 212 if (error != 0) {
213 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED; 213 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
214 return (SET_ERROR(error)); 214 return (SET_ERROR(error));
215 } 215 }
216 if (vp->v_type != VBLK) { 216 if (vp->v_type != VBLK) {
217 vrele(vp); 217 vrele(vp);
218 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED; 218 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
219 return (SET_ERROR(EINVAL)); 219 return (SET_ERROR(EINVAL));
220 } 220 }
221 221
222 /* XXXNETBSD Once tls-maxphys gets merged this block becomes: 222 /* XXXNETBSD Once tls-maxphys gets merged this block becomes:
223 if (VOP_IOCTL(vp, DIOCGWEDGEINFO, &dkw, FREAD, NOCRED) == 0) 223 if (VOP_IOCTL(vp, DIOCGWEDGEINFO, &dkw, FREAD, NOCRED) == 0)
224 pdk = disk_find(dkw.dkw_parent); 224 pdk = disk_find(dkw.dkw_parent);
225 else 225 else
226 pdk = disk_find_blk(vp->v_rdev); 226 pdk = disk_find_blk(vp->v_rdev);
227 dvd->vd_maxphys = (pdk ? disk_maxphys(pdk) : MACHINE_MAXPHYS); 227 dvd->vd_maxphys = (pdk ? disk_maxphys(pdk) : MACHINE_MAXPHYS);
228 */ 228 */
229 { 229 {
230 struct buf buf = { .b_bcount = MAXPHYS }; 230 struct buf buf = { .b_bcount = MAXPHYS };
231 231
232 if (VOP_IOCTL(vp, DIOCGWEDGEINFO, &dkw, FREAD, NOCRED) == 0) { 232 if (VOP_IOCTL(vp, DIOCGWEDGEINFO, &dkw, FREAD, NOCRED) == 0) {
233 pdk = disk_find(dkw.dkw_parent); 233 pdk = disk_find(dkw.dkw_parent);
234 } else { 234 } else {
235 const char *dev_name; 235 const char *dev_name;
236 236
237 dev_name = devsw_blk2name(major(vp->v_rdev)); 237 dev_name = devsw_blk2name(major(vp->v_rdev));
238 if (dev_name) { 238 if (dev_name) {
239 char disk_name[16]; 239 char disk_name[16];
240 240
241 snprintf(disk_name, sizeof(disk_name), "%s%d", 241 snprintf(disk_name, sizeof(disk_name), "%s%d",
242 dev_name, DISKUNIT(vp->v_rdev)); 242 dev_name, DISKUNIT(vp->v_rdev));
243 pdk = disk_find(disk_name); 243 pdk = disk_find(disk_name);
244 } else { 244 } else {
245 pdk = NULL; 245 pdk = NULL;
246 } 246 }
 247 buf.b_dev = vp->v_rdev;
247 } 248 }
248 if (pdk && pdk->dk_driver && pdk->dk_driver->d_minphys) 249 if (pdk && pdk->dk_driver && pdk->dk_driver->d_minphys)
249 (*pdk->dk_driver->d_minphys)(&buf); 250 (*pdk->dk_driver->d_minphys)(&buf);
250 dvd->vd_maxphys = buf.b_bcount; 251 dvd->vd_maxphys = buf.b_bcount;
251 } 252 }
252 253
253 /* 254 /*
254 * XXXNETBSD Compare the devid to the stored value. 255 * XXXNETBSD Compare the devid to the stored value.
255 */ 256 */
256 257
257 /* 258 /*
258 * Create a workqueue to process cache-flushes concurrently. 259 * Create a workqueue to process cache-flushes concurrently.
259 */ 260 */
260 error = workqueue_create(&dvd->vd_wq, "vdevsync", 261 error = workqueue_create(&dvd->vd_wq, "vdevsync",
261 vdev_disk_flush, dvd, PRI_NONE, IPL_NONE, WQ_MPSAFE); 262 vdev_disk_flush, dvd, PRI_NONE, IPL_NONE, WQ_MPSAFE);
262 if (error != 0) { 263 if (error != 0) {
263 vrele(vp); 264 vrele(vp);
264 return (SET_ERROR(error)); 265 return (SET_ERROR(error));
265 } 266 }
266 267
267 dvd->vd_vp = vp; 268 dvd->vd_vp = vp;
268 269
269skip_open: 270skip_open:
270 /* 271 /*
271 * Determine the actual size of the device. 272 * Determine the actual size of the device.
272 * Try wedge info first as it supports larger disks. 273 * Try wedge info first as it supports larger disks.
273 */ 274 */
274 error = VOP_IOCTL(vp, DIOCGWEDGEINFO, &dkw, FREAD, NOCRED); 275 error = VOP_IOCTL(vp, DIOCGWEDGEINFO, &dkw, FREAD, NOCRED);
275 if (error == 0) { 276 if (error == 0) {
276 pdk = disk_find(dkw.dkw_parent); 277 pdk = disk_find(dkw.dkw_parent);
277 if (pdk) { 278 if (pdk) {
278 pinfo.pi_secsize = (1 << pdk->dk_byteshift); 279 pinfo.pi_secsize = (1 << pdk->dk_byteshift);
279 pinfo.pi_size = dkw.dkw_size; 280 pinfo.pi_size = dkw.dkw_size;
280 pinfo.pi_offset = dkw.dkw_offset; 281 pinfo.pi_offset = dkw.dkw_offset;
281 } else  282 } else
282 error = ENODEV; 283 error = ENODEV;
283 } 284 }
284 if (error) 285 if (error)
285 error = VOP_IOCTL(vp, DIOCGPARTINFO, &pinfo, FREAD, kcred); 286 error = VOP_IOCTL(vp, DIOCGPARTINFO, &pinfo, FREAD, kcred);
286 if (error != 0) { 287 if (error != 0) {
287 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED; 288 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
288 return (SET_ERROR(error)); 289 return (SET_ERROR(error));
289 } 290 }
290 *psize = pinfo.pi_size * pinfo.pi_secsize; 291 *psize = pinfo.pi_size * pinfo.pi_secsize;
291 *max_psize = *psize; 292 *max_psize = *psize;
292 293
293 *ashift = highbit(MAX(pinfo.pi_secsize, SPA_MINBLOCKSIZE)) - 1; 294 *ashift = highbit(MAX(pinfo.pi_secsize, SPA_MINBLOCKSIZE)) - 1;
294 *pashift = *ashift; 295 *pashift = *ashift;
295 vd->vdev_wholedisk = (pinfo.pi_offset == 0); /* XXXNETBSD */ 296 vd->vdev_wholedisk = (pinfo.pi_offset == 0); /* XXXNETBSD */
296 297
297 /* 298 /*
298 * Clear the nowritecache bit, so that on a vdev_reopen() we will 299 * Clear the nowritecache bit, so that on a vdev_reopen() we will
299 * try again. 300 * try again.
300 */ 301 */
301 vd->vdev_nowritecache = B_FALSE; 302 vd->vdev_nowritecache = B_FALSE;
302 303
303 return (0); 304 return (0);
304} 305}
305 306
306static void 307static void
307vdev_disk_close(vdev_t *vd) 308vdev_disk_close(vdev_t *vd)
308{ 309{
309 vdev_disk_t *dvd = vd->vdev_tsd; 310 vdev_disk_t *dvd = vd->vdev_tsd;
310 311
311 if (vd->vdev_reopening || dvd == NULL) 312 if (vd->vdev_reopening || dvd == NULL)
312 return; 313 return;
313 314
314#ifdef illumos 315#ifdef illumos
315 if (dvd->vd_minor != NULL) { 316 if (dvd->vd_minor != NULL) {
316 ddi_devid_str_free(dvd->vd_minor); 317 ddi_devid_str_free(dvd->vd_minor);
317 dvd->vd_minor = NULL; 318 dvd->vd_minor = NULL;
318 } 319 }
319 320
320 if (dvd->vd_devid != NULL) { 321 if (dvd->vd_devid != NULL) {
321 ddi_devid_free(dvd->vd_devid); 322 ddi_devid_free(dvd->vd_devid);
322 dvd->vd_devid = NULL; 323 dvd->vd_devid = NULL;
323 } 324 }
324 325
325 if (dvd->vd_lh != NULL) { 326 if (dvd->vd_lh != NULL) {
326 (void) ldi_close(dvd->vd_lh, spa_mode(vd->vdev_spa), kcred); 327 (void) ldi_close(dvd->vd_lh, spa_mode(vd->vdev_spa), kcred);
327 dvd->vd_lh = NULL; 328 dvd->vd_lh = NULL;
328 } 329 }
329#endif 330#endif
330 331
331#ifdef __NetBSD__ 332#ifdef __NetBSD__
332 if (dvd->vd_vp != NULL) { 333 if (dvd->vd_vp != NULL) {
333 vn_close(dvd->vd_vp, FREAD|FWRITE, kcred); 334 vn_close(dvd->vd_vp, FREAD|FWRITE, kcred);
334 dvd->vd_vp = NULL; 335 dvd->vd_vp = NULL;
335 } 336 }
336 if (dvd->vd_wq != NULL) { 337 if (dvd->vd_wq != NULL) {
337 workqueue_destroy(dvd->vd_wq); 338 workqueue_destroy(dvd->vd_wq);
338 dvd->vd_wq = NULL; 339 dvd->vd_wq = NULL;
339 } 340 }
340#endif 341#endif
341 342
342 vd->vdev_delayed_close = B_FALSE; 343 vd->vdev_delayed_close = B_FALSE;
343#ifdef illumos 344#ifdef illumos
344 /* 345 /*
345 * If we closed the LDI handle due to an offline notify from LDI, 346 * If we closed the LDI handle due to an offline notify from LDI,
346 * don't free vd->vdev_tsd or unregister the callbacks here; 347 * don't free vd->vdev_tsd or unregister the callbacks here;
347 * the offline finalize callback or a reopen will take care of it. 348 * the offline finalize callback or a reopen will take care of it.
348 */ 349 */
349 if (dvd->vd_ldi_offline) 350 if (dvd->vd_ldi_offline)
350 return; 351 return;
351#endif 352#endif
352 353
353 vdev_disk_free(vd); 354 vdev_disk_free(vd);
354} 355}
355 356
356int 357int
357vdev_disk_physio(vdev_t *vd, caddr_t data, 358vdev_disk_physio(vdev_t *vd, caddr_t data,
358 size_t size, uint64_t offset, int flags, boolean_t isdump) 359 size_t size, uint64_t offset, int flags, boolean_t isdump)
359{ 360{
360#ifdef illumos 361#ifdef illumos
361 vdev_disk_t *dvd = vd->vdev_tsd; 362 vdev_disk_t *dvd = vd->vdev_tsd;
362 363
363 /* 364 /*
364 * If the vdev is closed, it's likely in the REMOVED or FAULTED state. 365 * If the vdev is closed, it's likely in the REMOVED or FAULTED state.
365 * Nothing to be done here but return failure. 366 * Nothing to be done here but return failure.
366 */ 367 */
367 if (dvd == NULL || (dvd->vd_ldi_offline && dvd->vd_lh == NULL)) 368 if (dvd == NULL || (dvd->vd_ldi_offline && dvd->vd_lh == NULL))
368 return (EIO); 369 return (EIO);
369 370
370 ASSERT(vd->vdev_ops == &vdev_disk_ops); 371 ASSERT(vd->vdev_ops == &vdev_disk_ops);
371 372
372 /* 373 /*
373 * If in the context of an active crash dump, use the ldi_dump(9F) 374 * If in the context of an active crash dump, use the ldi_dump(9F)
374 * call instead of ldi_strategy(9F) as usual. 375 * call instead of ldi_strategy(9F) as usual.
375 */ 376 */
376 if (isdump) { 377 if (isdump) {
377 ASSERT3P(dvd, !=, NULL); 378 ASSERT3P(dvd, !=, NULL);
378 return (ldi_dump(dvd->vd_lh, data, lbtodb(offset), 379 return (ldi_dump(dvd->vd_lh, data, lbtodb(offset),
379 lbtodb(size))); 380 lbtodb(size)));
380 } 381 }
381 382
382 return (vdev_disk_ldi_physio(dvd->vd_lh, data, size, offset, flags)); 383 return (vdev_disk_ldi_physio(dvd->vd_lh, data, size, offset, flags));
383#endif 384#endif
384#ifdef __NetBSD__ 385#ifdef __NetBSD__
385 return (EIO); 386 return (EIO);
386#endif 387#endif
387} 388}
388 389
389static void 390static void
390vdev_disk_io_intr(buf_t *bp) 391vdev_disk_io_intr(buf_t *bp)
391{ 392{
392 zio_t *zio = bp->b_private; 393 zio_t *zio = bp->b_private;
393 394
394 /* 395 /*
395 * The rest of the zio stack only deals with EIO, ECKSUM, and ENXIO. 396 * The rest of the zio stack only deals with EIO, ECKSUM, and ENXIO.
396 * Rather than teach the rest of the stack about other error 397 * Rather than teach the rest of the stack about other error
397 * possibilities (EFAULT, etc), we normalize the error value here. 398 * possibilities (EFAULT, etc), we normalize the error value here.
398 */ 399 */
399 zio->io_error = (geterror(bp) != 0 ? SET_ERROR(EIO) : 0); 400 zio->io_error = (geterror(bp) != 0 ? SET_ERROR(EIO) : 0);
400 401
401 if (zio->io_error == 0 && bp->b_resid != 0) 402 if (zio->io_error == 0 && bp->b_resid != 0)
402 zio->io_error = SET_ERROR(EIO); 403 zio->io_error = SET_ERROR(EIO);
403 404
404 putiobuf(bp); 405 putiobuf(bp);
405 zio_delay_interrupt(zio); 406 zio_delay_interrupt(zio);
406} 407}
407 408
408static void 409static void
409vdev_disk_ioctl_free(zio_t *zio) 410vdev_disk_ioctl_free(zio_t *zio)
410{ 411{
411 kmem_free(zio->io_vsd, sizeof (struct dk_callback)); 412 kmem_free(zio->io_vsd, sizeof (struct dk_callback));
412} 413}
413 414
414static const zio_vsd_ops_t vdev_disk_vsd_ops = { 415static const zio_vsd_ops_t vdev_disk_vsd_ops = {
415 vdev_disk_ioctl_free, 416 vdev_disk_ioctl_free,
416 zio_vsd_default_cksum_report 417 zio_vsd_default_cksum_report
417}; 418};
418 419
419static void 420static void
420vdev_disk_ioctl_done(void *zio_arg, int error) 421vdev_disk_ioctl_done(void *zio_arg, int error)
421{ 422{
422 zio_t *zio = zio_arg; 423 zio_t *zio = zio_arg;
423 424
424 zio->io_error = error; 425 zio->io_error = error;
425 426
426 zio_interrupt(zio); 427 zio_interrupt(zio);
427} 428}
428 429
429static void 430static void
430vdev_disk_io_start(zio_t *zio) 431vdev_disk_io_start(zio_t *zio)
431{ 432{
432 vdev_t *vd = zio->io_vd; 433 vdev_t *vd = zio->io_vd;
433 vdev_disk_t *dvd = vd->vdev_tsd; 434 vdev_disk_t *dvd = vd->vdev_tsd;
434 vnode_t *vp; 435 vnode_t *vp;
435 buf_t *bp, *nbp; 436 buf_t *bp, *nbp;
436 int error, size, off, resid; 437 int error, size, off, resid;
437 438
438 /* 439 /*
439 * If the vdev is closed, it's likely in the REMOVED or FAULTED state. 440 * If the vdev is closed, it's likely in the REMOVED or FAULTED state.
440 * Nothing to be done here but return failure. 441 * Nothing to be done here but return failure.
441 */ 442 */
442#ifdef illumos 443#ifdef illumos
443 if (dvd == NULL || (dvd->vd_ldi_offline && dvd->vd_lh == NULL)) { 444 if (dvd == NULL || (dvd->vd_ldi_offline && dvd->vd_lh == NULL)) {
444 zio->io_error = SET_ERROR(ENXIO); 445 zio->io_error = SET_ERROR(ENXIO);
445 zio_interrupt(zio); 446 zio_interrupt(zio);
446 return; 447 return;
447 } 448 }
448#endif 449#endif
449#ifdef __NetBSD__ 450#ifdef __NetBSD__
450 if (dvd == NULL) { 451 if (dvd == NULL) {
451 zio->io_error = SET_ERROR(ENXIO); 452 zio->io_error = SET_ERROR(ENXIO);
452 zio_interrupt(zio); 453 zio_interrupt(zio);
453 return; 454 return;
454 } 455 }
455 ASSERT3U(dvd->vd_maxphys, >, 0); 456 ASSERT3U(dvd->vd_maxphys, >, 0);
456 vp = dvd->vd_vp; 457 vp = dvd->vd_vp;
457#endif 458#endif
458 459
459 if (zio->io_type == ZIO_TYPE_IOCTL) { 460 if (zio->io_type == ZIO_TYPE_IOCTL) {
460 /* XXPOLICY */ 461 /* XXPOLICY */
461 if (!vdev_readable(vd)) { 462 if (!vdev_readable(vd)) {
462 zio->io_error = SET_ERROR(ENXIO); 463 zio->io_error = SET_ERROR(ENXIO);
463 zio_interrupt(zio); 464 zio_interrupt(zio);
464 return; 465 return;
465 } 466 }
466 467
467 switch (zio->io_cmd) { 468 switch (zio->io_cmd) {
468 case DKIOCFLUSHWRITECACHE: 469 case DKIOCFLUSHWRITECACHE:
469 470
470 if (zfs_nocacheflush) 471 if (zfs_nocacheflush)
471 break; 472 break;
472 473
473 if (vd->vdev_nowritecache) { 474 if (vd->vdev_nowritecache) {
474 zio->io_error = ENOTSUP; 475 zio->io_error = ENOTSUP;
475 break; 476 break;
476 } 477 }
477 478
478 bp = getiobuf(vp, true); 479 bp = getiobuf(vp, true);
479 bp->b_private = zio; 480 bp->b_private = zio;
480 workqueue_enqueue(dvd->vd_wq, &bp->b_work, NULL); 481 workqueue_enqueue(dvd->vd_wq, &bp->b_work, NULL);
481 return; 482 return;
482 483
483 default: 484 default:
484 zio->io_error = SET_ERROR(ENOTSUP); 485 zio->io_error = SET_ERROR(ENOTSUP);
485 break; 486 break;
486 } 487 }
487 488
488 zio_execute(zio); 489 zio_execute(zio);
489 return; 490 return;
490 } 491 }
491 492
492 bp = getiobuf(vp, true); 493 bp = getiobuf(vp, true);
493 bp->b_flags = (zio->io_type == ZIO_TYPE_READ ? B_READ : B_WRITE); 494 bp->b_flags = (zio->io_type == ZIO_TYPE_READ ? B_READ : B_WRITE);
494 bp->b_cflags = BC_BUSY | BC_NOCACHE; 495 bp->b_cflags = BC_BUSY | BC_NOCACHE;
495 bp->b_data = zio->io_data; 496 bp->b_data = zio->io_data;
496 bp->b_blkno = btodb(zio->io_offset); 497 bp->b_blkno = btodb(zio->io_offset);
497 bp->b_bcount = zio->io_size; 498 bp->b_bcount = zio->io_size;
498 bp->b_resid = zio->io_size; 499 bp->b_resid = zio->io_size;
499 bp->b_iodone = vdev_disk_io_intr; 500 bp->b_iodone = vdev_disk_io_intr;
500 bp->b_private = zio; 501 bp->b_private = zio;
501 502
502 if (!(bp->b_flags & B_READ)) { 503 if (!(bp->b_flags & B_READ)) {
503 mutex_enter(vp->v_interlock); 504 mutex_enter(vp->v_interlock);
504 vp->v_numoutput++; 505 vp->v_numoutput++;
505 mutex_exit(vp->v_interlock); 506 mutex_exit(vp->v_interlock);
506 } 507 }
507 508
508 if (bp->b_bcount <= dvd->vd_maxphys) { 509 if (bp->b_bcount <= dvd->vd_maxphys) {
509 /* We can do this I/O in one pass. */ 510 /* We can do this I/O in one pass. */
510 (void)VOP_STRATEGY(vp, bp); 511 (void)VOP_STRATEGY(vp, bp);
511 } else { 512 } else {
512 /* 513 /*
513 * The I/O is larger than we can process in one pass. 514 * The I/O is larger than we can process in one pass.
514 * Split it into smaller pieces. 515 * Split it into smaller pieces.
515 */ 516 */
516 resid = zio->io_size; 517 resid = zio->io_size;
517 off = 0; 518 off = 0;
518 while (resid != 0) { 519 while (resid != 0) {
519 size = uimin(resid, dvd->vd_maxphys); 520 size = uimin(resid, dvd->vd_maxphys);
520 nbp = getiobuf(vp, true); 521 nbp = getiobuf(vp, true);
521 nbp->b_blkno = btodb(zio->io_offset + off); 522 nbp->b_blkno = btodb(zio->io_offset + off);
522 /* Below call increments v_numoutput. */ 523 /* Below call increments v_numoutput. */
523 nestiobuf_setup(bp, nbp, off, size); 524 nestiobuf_setup(bp, nbp, off, size);
524 (void)VOP_STRATEGY(vp, nbp); 525 (void)VOP_STRATEGY(vp, nbp);
525 resid -= size; 526 resid -= size;
526 off += size; 527 off += size;
527 } 528 }
528 } 529 }
529} 530}
530 531
531static void 532static void
532vdev_disk_io_done(zio_t *zio) 533vdev_disk_io_done(zio_t *zio)
533{ 534{
534#ifdef illumos 535#ifdef illumos
535 vdev_t *vd = zio->io_vd; 536 vdev_t *vd = zio->io_vd;
536 537
537 /* 538 /*
538 * If the device returned EIO, then attempt a DKIOCSTATE ioctl to see if 539 * If the device returned EIO, then attempt a DKIOCSTATE ioctl to see if
539 * the device has been removed. If this is the case, then we trigger an 540 * the device has been removed. If this is the case, then we trigger an
540 * asynchronous removal of the device. Otherwise, probe the device and 541 * asynchronous removal of the device. Otherwise, probe the device and
541 * make sure it's still accessible. 542 * make sure it's still accessible.
542 */ 543 */
543 if (zio->io_error == EIO && !vd->vdev_remove_wanted) { 544 if (zio->io_error == EIO && !vd->vdev_remove_wanted) {
544 vdev_disk_t *dvd = vd->vdev_tsd; 545 vdev_disk_t *dvd = vd->vdev_tsd;
545 int state = DKIO_NONE; 546 int state = DKIO_NONE;
546 547
547 if (ldi_ioctl(dvd->vd_lh, DKIOCSTATE, (intptr_t)&state, 548 if (ldi_ioctl(dvd->vd_lh, DKIOCSTATE, (intptr_t)&state,
548 FKIOCTL, kcred, NULL) == 0 && state != DKIO_INSERTED) { 549 FKIOCTL, kcred, NULL) == 0 && state != DKIO_INSERTED) {
549 /* 550 /*
550 * We post the resource as soon as possible, instead of 551 * We post the resource as soon as possible, instead of
551 * when the async removal actually happens, because the 552 * when the async removal actually happens, because the
552 * DE is using this information to discard previous I/O 553 * DE is using this information to discard previous I/O
553 * errors. 554 * errors.
554 */ 555 */
555 zfs_post_remove(zio->io_spa, vd); 556 zfs_post_remove(zio->io_spa, vd);
556 vd->vdev_remove_wanted = B_TRUE; 557 vd->vdev_remove_wanted = B_TRUE;
557 spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE); 558 spa_async_request(zio->io_spa, SPA_ASYNC_REMOVE);
558 } else if (!vd->vdev_delayed_close) { 559 } else if (!vd->vdev_delayed_close) {
559 vd->vdev_delayed_close = B_TRUE; 560 vd->vdev_delayed_close = B_TRUE;
560 } 561 }
561 } 562 }
562#endif 563#endif
563} 564}
564 565
565vdev_ops_t vdev_disk_ops = { 566vdev_ops_t vdev_disk_ops = {
566 vdev_disk_open, 567 vdev_disk_open,
567 vdev_disk_close, 568 vdev_disk_close,
568 vdev_default_asize, 569 vdev_default_asize,
569 vdev_disk_io_start, 570 vdev_disk_io_start,
570 vdev_disk_io_done, 571 vdev_disk_io_done,
571 NULL, 572 NULL,
572 vdev_disk_hold, 573 vdev_disk_hold,
573 vdev_disk_rele, 574 vdev_disk_rele,
574 VDEV_TYPE_DISK, /* name of this vdev type */ 575 VDEV_TYPE_DISK, /* name of this vdev type */
575 B_TRUE /* leaf vdev */ 576 B_TRUE /* leaf vdev */
576}; 577};
577 578
578/* 579/*
579 * Given the root disk device devid or pathname, read the label from 580 * Given the root disk device devid or pathname, read the label from
580 * the device, and construct a configuration nvlist. 581 * the device, and construct a configuration nvlist.
581 */ 582 */
582int 583int
583vdev_disk_read_rootlabel(char *devpath, char *devid, nvlist_t **config) 584vdev_disk_read_rootlabel(char *devpath, char *devid, nvlist_t **config)
584{ 585{
585#ifdef __NetBSD__ 586#ifdef __NetBSD__
586 return (ENOTSUP); 587 return (ENOTSUP);
587#else 588#else
588 ldi_handle_t vd_lh; 589 ldi_handle_t vd_lh;
589 vdev_label_t *label; 590 vdev_label_t *label;
590 uint64_t s, size; 591 uint64_t s, size;
591 int l; 592 int l;
592 ddi_devid_t tmpdevid; 593 ddi_devid_t tmpdevid;
593 int error = -1; 594 int error = -1;
594 char *minor_name; 595 char *minor_name;
595 596
596 /* 597 /*
597 * Read the device label and build the nvlist. 598 * Read the device label and build the nvlist.
598 */ 599 */
599 if (devid != NULL && ddi_devid_str_decode(devid, &tmpdevid, 600 if (devid != NULL && ddi_devid_str_decode(devid, &tmpdevid,
600 &minor_name) == 0) { 601 &minor_name) == 0) {
601 error = ldi_open_by_devid(tmpdevid, minor_name, 602 error = ldi_open_by_devid(tmpdevid, minor_name,
602 FREAD, kcred, &vd_lh, zfs_li); 603 FREAD, kcred, &vd_lh, zfs_li);
603 ddi_devid_free(tmpdevid); 604 ddi_devid_free(tmpdevid);
604 ddi_devid_str_free(minor_name); 605 ddi_devid_str_free(minor_name);
605 } 606 }
606 607
607 if (error && (error = ldi_open_by_name(devpath, FREAD, kcred, &vd_lh, 608 if (error && (error = ldi_open_by_name(devpath, FREAD, kcred, &vd_lh,
608 zfs_li))) 609 zfs_li)))
609 return (error); 610 return (error);
610 611
611 if (ldi_get_size(vd_lh, &s)) { 612 if (ldi_get_size(vd_lh, &s)) {
612 (void) ldi_close(vd_lh, FREAD, kcred); 613 (void) ldi_close(vd_lh, FREAD, kcred);
613 return (SET_ERROR(EIO)); 614 return (SET_ERROR(EIO));
614 } 615 }
615 616
616 size = P2ALIGN_TYPED(s, sizeof (vdev_label_t), uint64_t); 617 size = P2ALIGN_TYPED(s, sizeof (vdev_label_t), uint64_t);
617 label = kmem_alloc(sizeof (vdev_label_t), KM_SLEEP); 618 label = kmem_alloc(sizeof (vdev_label_t), KM_SLEEP);
618 619
619 *config = NULL; 620 *config = NULL;
620 for (l = 0; l < VDEV_LABELS; l++) { 621 for (l = 0; l < VDEV_LABELS; l++) {
621 uint64_t offset, state, txg = 0; 622 uint64_t offset, state, txg = 0;
622 623
623 /* read vdev label */ 624 /* read vdev label */
624 offset = vdev_label_offset(size, l, 0); 625 offset = vdev_label_offset(size, l, 0);
625 if (vdev_disk_ldi_physio(vd_lh, (caddr_t)label, 626 if (vdev_disk_ldi_physio(vd_lh, (caddr_t)label,
626 VDEV_SKIP_SIZE + VDEV_PHYS_SIZE, offset, B_READ) != 0) 627 VDEV_SKIP_SIZE + VDEV_PHYS_SIZE, offset, B_READ) != 0)
627 continue; 628 continue;
628 629
629 if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist, 630 if (nvlist_unpack(label->vl_vdev_phys.vp_nvlist,
630 sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0) { 631 sizeof (label->vl_vdev_phys.vp_nvlist), config, 0) != 0) {
631 *config = NULL; 632 *config = NULL;
632 continue; 633 continue;
633 } 634 }
634 635
635 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE, 636 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_STATE,
636 &state) != 0 || state >= POOL_STATE_DESTROYED) { 637 &state) != 0 || state >= POOL_STATE_DESTROYED) {
637 nvlist_free(*config); 638 nvlist_free(*config);
638 *config = NULL; 639 *config = NULL;
639 continue; 640 continue;
640 } 641 }
641 642
642 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG, 643 if (nvlist_lookup_uint64(*config, ZPOOL_CONFIG_POOL_TXG,
643 &txg) != 0 || txg == 0) { 644 &txg) != 0 || txg == 0) {
644 nvlist_free(*config); 645 nvlist_free(*config);
645 *config = NULL; 646 *config = NULL;
646 continue; 647 continue;
647 } 648 }
648 649
649 break; 650 break;
650 } 651 }
651 652
652 kmem_free(label, sizeof (vdev_label_t)); 653 kmem_free(label, sizeof (vdev_label_t));
653 (void) ldi_close(vd_lh, FREAD, kcred); 654 (void) ldi_close(vd_lh, FREAD, kcred);
654 if (*config == NULL) 655 if (*config == NULL)
655 error = SET_ERROR(EIDRM); 656 error = SET_ERROR(EIDRM);
656 657
657 return (error); 658 return (error);
658#endif 659#endif
659} 660}