xref: /onnv-gate/usr/src/uts/common/fs/zfs/vdev_file.c (revision 5329:33cb98223b2d)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51544Seschrock  * Common Development and Distribution License (the "License").
61544Seschrock  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
21789Sahrens /*
22*5329Sgw25295  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
27789Sahrens 
28789Sahrens #include <sys/zfs_context.h>
29789Sahrens #include <sys/spa.h>
30789Sahrens #include <sys/vdev_file.h>
31789Sahrens #include <sys/vdev_impl.h>
32789Sahrens #include <sys/zio.h>
33789Sahrens #include <sys/fs/zfs.h>
34789Sahrens 
35789Sahrens /*
36789Sahrens  * Virtual device vector for files.
37789Sahrens  */
38789Sahrens 
39789Sahrens static int
40*5329Sgw25295 vdev_file_open_common(vdev_t *vd)
41789Sahrens {
42789Sahrens 	vdev_file_t *vf;
43789Sahrens 	vnode_t *vp;
44789Sahrens 	int error;
45789Sahrens 
46789Sahrens 	/*
47789Sahrens 	 * We must have a pathname, and it must be absolute.
48789Sahrens 	 */
49789Sahrens 	if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
50789Sahrens 		vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
51789Sahrens 		return (EINVAL);
52789Sahrens 	}
53789Sahrens 
54789Sahrens 	vf = vd->vdev_tsd = kmem_zalloc(sizeof (vdev_file_t), KM_SLEEP);
55789Sahrens 
56789Sahrens 	/*
57789Sahrens 	 * We always open the files from the root of the global zone, even if
58789Sahrens 	 * we're in a local zone.  If the user has gotten to this point, the
59789Sahrens 	 * administrator has already decided that the pool should be available
60789Sahrens 	 * to local zone users, so the underlying devices should be as well.
61789Sahrens 	 */
62789Sahrens 	ASSERT(vd->vdev_path != NULL && vd->vdev_path[0] == '/');
63*5329Sgw25295 	error = vn_openat(vd->vdev_path + 1, UIO_SYSSPACE,
64*5329Sgw25295 	    spa_mode | FOFFMAX, 0, &vp, 0, 0, rootdir);
65789Sahrens 
66789Sahrens 	if (error) {
67789Sahrens 		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
68789Sahrens 		return (error);
69789Sahrens 	}
70789Sahrens 
71789Sahrens 	vf->vf_vnode = vp;
72789Sahrens 
73789Sahrens #ifdef _KERNEL
74789Sahrens 	/*
75789Sahrens 	 * Make sure it's a regular file.
76789Sahrens 	 */
77789Sahrens 	if (vp->v_type != VREG) {
78789Sahrens 		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
79789Sahrens 		return (ENODEV);
80789Sahrens 	}
81789Sahrens #endif
82789Sahrens 
83*5329Sgw25295 	return (0);
84*5329Sgw25295 }
85*5329Sgw25295 
86*5329Sgw25295 static int
87*5329Sgw25295 vdev_file_open(vdev_t *vd, uint64_t *psize, uint64_t *ashift)
88*5329Sgw25295 {
89*5329Sgw25295 	vdev_file_t *vf;
90*5329Sgw25295 	vattr_t vattr;
91*5329Sgw25295 	int error;
92*5329Sgw25295 
93*5329Sgw25295 	if ((error = vdev_file_open_common(vd)) != 0)
94*5329Sgw25295 		return (error);
95*5329Sgw25295 
96*5329Sgw25295 	vf = vd->vdev_tsd;
97*5329Sgw25295 
98789Sahrens 	/*
99789Sahrens 	 * Determine the physical size of the file.
100789Sahrens 	 */
101789Sahrens 	vattr.va_mask = AT_SIZE;
102*5329Sgw25295 	error = VOP_GETATTR(vf->vf_vnode, &vattr, 0, kcred);
103789Sahrens 	if (error) {
104789Sahrens 		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
105789Sahrens 		return (error);
106789Sahrens 	}
107789Sahrens 
108789Sahrens 	*psize = vattr.va_size;
109789Sahrens 	*ashift = SPA_MINBLOCKSHIFT;
110789Sahrens 
111789Sahrens 	return (0);
112789Sahrens }
113789Sahrens 
114789Sahrens static void
115789Sahrens vdev_file_close(vdev_t *vd)
116789Sahrens {
117789Sahrens 	vdev_file_t *vf = vd->vdev_tsd;
118789Sahrens 
119789Sahrens 	if (vf == NULL)
120789Sahrens 		return;
121789Sahrens 
122789Sahrens 	if (vf->vf_vnode != NULL) {
123789Sahrens 		(void) VOP_PUTPAGE(vf->vf_vnode, 0, 0, B_INVAL, kcred);
124789Sahrens 		(void) VOP_CLOSE(vf->vf_vnode, spa_mode, 1, 0, kcred);
125789Sahrens 		VN_RELE(vf->vf_vnode);
126789Sahrens 	}
127789Sahrens 
128789Sahrens 	kmem_free(vf, sizeof (vdev_file_t));
129789Sahrens 	vd->vdev_tsd = NULL;
130789Sahrens }
131789Sahrens 
132*5329Sgw25295 static int
133*5329Sgw25295 vdev_file_probe_io(vdev_t *vd, caddr_t data, size_t size, uint64_t offset,
134*5329Sgw25295     enum uio_rw rw)
135*5329Sgw25295 {
136*5329Sgw25295 	vdev_file_t *vf = vd->vdev_tsd;
137*5329Sgw25295 	ssize_t resid;
138*5329Sgw25295 	int error = 0;
139*5329Sgw25295 
140*5329Sgw25295 	if (vd == NULL || vf == NULL || vf->vf_vnode == NULL)
141*5329Sgw25295 		return (EINVAL);
142*5329Sgw25295 
143*5329Sgw25295 	ASSERT(rw == UIO_READ || rw ==  UIO_WRITE);
144*5329Sgw25295 
145*5329Sgw25295 	error = vn_rdwr(rw, vf->vf_vnode, data, size, offset, UIO_SYSSPACE,
146*5329Sgw25295 	    0, RLIM64_INFINITY, kcred, &resid);
147*5329Sgw25295 	if (error || resid != 0)
148*5329Sgw25295 		return (EIO);
149*5329Sgw25295 	return (0);
150*5329Sgw25295 }
151*5329Sgw25295 
152*5329Sgw25295 static int
153*5329Sgw25295 vdev_file_probe(vdev_t *vd)
154*5329Sgw25295 {
155*5329Sgw25295 	vdev_t *nvd;
156*5329Sgw25295 	char *vl_boot;
157*5329Sgw25295 	uint64_t offset;
158*5329Sgw25295 	int l, error = 0, retries = 0;
159*5329Sgw25295 
160*5329Sgw25295 	if (vd == NULL)
161*5329Sgw25295 		return (EINVAL);
162*5329Sgw25295 
163*5329Sgw25295 	/* Hijack the current vdev */
164*5329Sgw25295 	nvd = vd;
165*5329Sgw25295 
166*5329Sgw25295 	/*
167*5329Sgw25295 	 * Pick a random label to rewrite.
168*5329Sgw25295 	 */
169*5329Sgw25295 	l = spa_get_random(VDEV_LABELS);
170*5329Sgw25295 	ASSERT(l < VDEV_LABELS);
171*5329Sgw25295 
172*5329Sgw25295 	offset = vdev_label_offset(vd->vdev_psize, l,
173*5329Sgw25295 	    offsetof(vdev_label_t, vl_boot_header));
174*5329Sgw25295 
175*5329Sgw25295 	vl_boot = kmem_alloc(VDEV_BOOT_HEADER_SIZE, KM_SLEEP);
176*5329Sgw25295 
177*5329Sgw25295 	while ((error = vdev_file_probe_io(nvd, vl_boot, VDEV_BOOT_HEADER_SIZE,
178*5329Sgw25295 	    offset, UIO_READ)) != 0 && retries == 0) {
179*5329Sgw25295 
180*5329Sgw25295 		/*
181*5329Sgw25295 		 * If we failed with the vdev that was passed in then
182*5329Sgw25295 		 * try allocating a new one and try again.
183*5329Sgw25295 		 */
184*5329Sgw25295 		nvd = kmem_zalloc(sizeof (vdev_t), KM_SLEEP);
185*5329Sgw25295 		if (vd->vdev_path)
186*5329Sgw25295 			nvd->vdev_path = spa_strdup(vd->vdev_path);
187*5329Sgw25295 		error = vdev_file_open_common(nvd);
188*5329Sgw25295 		if (error) {
189*5329Sgw25295 			vdev_set_state(vd, B_TRUE, VDEV_STATE_CANT_OPEN,
190*5329Sgw25295 			    nvd->vdev_stat.vs_aux);
191*5329Sgw25295 			break;
192*5329Sgw25295 		}
193*5329Sgw25295 		retries++;
194*5329Sgw25295 	}
195*5329Sgw25295 
196*5329Sgw25295 	if ((spa_mode & FWRITE) && !error) {
197*5329Sgw25295 		error = vdev_file_probe_io(nvd, vl_boot, VDEV_BOOT_HEADER_SIZE,
198*5329Sgw25295 		    offset, UIO_WRITE);
199*5329Sgw25295 	}
200*5329Sgw25295 
201*5329Sgw25295 	if (retries) {
202*5329Sgw25295 		vdev_file_close(nvd);
203*5329Sgw25295 		if (nvd->vdev_path)
204*5329Sgw25295 			spa_strfree(nvd->vdev_path);
205*5329Sgw25295 		kmem_free(nvd, sizeof (vdev_t));
206*5329Sgw25295 	}
207*5329Sgw25295 	kmem_free(vl_boot, VDEV_BOOT_HEADER_SIZE);
208*5329Sgw25295 
209*5329Sgw25295 	if (!error)
210*5329Sgw25295 		vd->vdev_is_failing = B_FALSE;
211*5329Sgw25295 
212*5329Sgw25295 	return (error);
213*5329Sgw25295 }
214*5329Sgw25295 
215789Sahrens static void
216789Sahrens vdev_file_io_start(zio_t *zio)
217789Sahrens {
218789Sahrens 	vdev_t *vd = zio->io_vd;
219789Sahrens 	vdev_file_t *vf = vd->vdev_tsd;
220789Sahrens 	ssize_t resid;
221789Sahrens 	int error;
222789Sahrens 
223789Sahrens 	if (zio->io_type == ZIO_TYPE_IOCTL) {
224789Sahrens 		zio_vdev_io_bypass(zio);
225789Sahrens 
226789Sahrens 		/* XXPOLICY */
227*5329Sgw25295 		if (!vdev_readable(vd)) {
228789Sahrens 			zio->io_error = ENXIO;
229789Sahrens 			zio_next_stage_async(zio);
230789Sahrens 			return;
231789Sahrens 		}
232789Sahrens 
233789Sahrens 		switch (zio->io_cmd) {
234789Sahrens 		case DKIOCFLUSHWRITECACHE:
235789Sahrens 			zio->io_error = VOP_FSYNC(vf->vf_vnode, FSYNC | FDSYNC,
236789Sahrens 			    kcred);
237789Sahrens 			dprintf("fsync(%s) = %d\n", vdev_description(vd),
238789Sahrens 			    zio->io_error);
239789Sahrens 			break;
240789Sahrens 		default:
241789Sahrens 			zio->io_error = ENOTSUP;
242789Sahrens 		}
243789Sahrens 
244789Sahrens 		zio_next_stage_async(zio);
245789Sahrens 		return;
246789Sahrens 	}
247789Sahrens 
2483059Sahrens 	/*
2493059Sahrens 	 * In the kernel, don't bother double-caching, but in userland,
2503059Sahrens 	 * we want to test the vdev_cache code.
2513059Sahrens 	 */
2523059Sahrens #ifndef _KERNEL
253789Sahrens 	if (zio->io_type == ZIO_TYPE_READ && vdev_cache_read(zio) == 0)
254789Sahrens 		return;
2553059Sahrens #endif
256789Sahrens 
257789Sahrens 	if ((zio = vdev_queue_io(zio)) == NULL)
258789Sahrens 		return;
259789Sahrens 
260789Sahrens 	/* XXPOLICY */
261*5329Sgw25295 	if (zio->io_type == ZIO_TYPE_WRITE)
262*5329Sgw25295 		error = vdev_writeable(vd) ? vdev_error_inject(vd, zio) : ENXIO;
263*5329Sgw25295 	else
264*5329Sgw25295 		error = vdev_readable(vd) ? vdev_error_inject(vd, zio) : ENXIO;
265*5329Sgw25295 	error = (vd->vdev_remove_wanted || vd->vdev_is_failing) ? ENXIO : error;
266789Sahrens 	if (error) {
267789Sahrens 		zio->io_error = error;
268789Sahrens 		zio_next_stage_async(zio);
269789Sahrens 		return;
270789Sahrens 	}
271789Sahrens 
272789Sahrens 	zio->io_error = vn_rdwr(zio->io_type == ZIO_TYPE_READ ?
273789Sahrens 	    UIO_READ : UIO_WRITE, vf->vf_vnode, zio->io_data,
274789Sahrens 	    zio->io_size, zio->io_offset, UIO_SYSSPACE,
275789Sahrens 	    0, RLIM64_INFINITY, kcred, &resid);
276789Sahrens 
277789Sahrens 	if (resid != 0 && zio->io_error == 0)
278789Sahrens 		zio->io_error = ENOSPC;
279789Sahrens 
280789Sahrens 	zio_next_stage_async(zio);
281789Sahrens }
282789Sahrens 
283789Sahrens static void
284789Sahrens vdev_file_io_done(zio_t *zio)
285789Sahrens {
286*5329Sgw25295 
287*5329Sgw25295 	if (zio_injection_enabled && zio->io_error == 0)
288*5329Sgw25295 		zio->io_error = zio_handle_device_injection(zio->io_vd, EIO);
289*5329Sgw25295 
290*5329Sgw25295 	/*
291*5329Sgw25295 	 * If this device is truely gone, then attempt to remove it
292*5329Sgw25295 	 * from the configuration.
293*5329Sgw25295 	 */
294*5329Sgw25295 	if (zio->io_error == EIO) {
295*5329Sgw25295 		vdev_t *vd = zio->io_vd;
296*5329Sgw25295 
297*5329Sgw25295 		if (vdev_probe(vd) != 0)
298*5329Sgw25295 			vd->vdev_is_failing = B_TRUE;
299*5329Sgw25295 	}
300*5329Sgw25295 
301789Sahrens 	vdev_queue_io_done(zio);
302789Sahrens 
3033059Sahrens #ifndef _KERNEL
304789Sahrens 	if (zio->io_type == ZIO_TYPE_WRITE)
305789Sahrens 		vdev_cache_write(zio);
3063059Sahrens #endif
307789Sahrens 
308789Sahrens 	zio_next_stage(zio);
309789Sahrens }
310789Sahrens 
311789Sahrens vdev_ops_t vdev_file_ops = {
312789Sahrens 	vdev_file_open,
313789Sahrens 	vdev_file_close,
314*5329Sgw25295 	vdev_file_probe,
315789Sahrens 	vdev_default_asize,
316789Sahrens 	vdev_file_io_start,
317789Sahrens 	vdev_file_io_done,
318789Sahrens 	NULL,
319789Sahrens 	VDEV_TYPE_FILE,		/* name of this vdev type */
320789Sahrens 	B_TRUE			/* leaf vdev */
321789Sahrens };
322789Sahrens 
323789Sahrens /*
324789Sahrens  * From userland we access disks just like files.
325789Sahrens  */
326789Sahrens #ifndef _KERNEL
327789Sahrens 
328789Sahrens vdev_ops_t vdev_disk_ops = {
329789Sahrens 	vdev_file_open,
330789Sahrens 	vdev_file_close,
331*5329Sgw25295 	vdev_file_probe,
332789Sahrens 	vdev_default_asize,
333789Sahrens 	vdev_file_io_start,
334789Sahrens 	vdev_file_io_done,
335789Sahrens 	NULL,
336789Sahrens 	VDEV_TYPE_DISK,		/* name of this vdev type */
337789Sahrens 	B_TRUE			/* leaf vdev */
338789Sahrens };
339789Sahrens 
340789Sahrens #endif
341