xref: /onnv-gate/usr/src/uts/common/fs/zfs/vdev_file.c (revision 789:b348f31ed315)
1*789Sahrens /*
2*789Sahrens  * CDDL HEADER START
3*789Sahrens  *
4*789Sahrens  * The contents of this file are subject to the terms of the
5*789Sahrens  * Common Development and Distribution License, Version 1.0 only
6*789Sahrens  * (the "License").  You may not use this file except in compliance
7*789Sahrens  * with the License.
8*789Sahrens  *
9*789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*789Sahrens  * or http://www.opensolaris.org/os/licensing.
11*789Sahrens  * See the License for the specific language governing permissions
12*789Sahrens  * and limitations under the License.
13*789Sahrens  *
14*789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
15*789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
17*789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
18*789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
19*789Sahrens  *
20*789Sahrens  * CDDL HEADER END
21*789Sahrens  */
22*789Sahrens /*
23*789Sahrens  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*789Sahrens  * Use is subject to license terms.
25*789Sahrens  */
26*789Sahrens 
27*789Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*789Sahrens 
29*789Sahrens #include <sys/zfs_context.h>
30*789Sahrens #include <sys/spa.h>
31*789Sahrens #include <sys/vdev_file.h>
32*789Sahrens #include <sys/vdev_impl.h>
33*789Sahrens #include <sys/zio.h>
34*789Sahrens #include <sys/fs/zfs.h>
35*789Sahrens 
36*789Sahrens /*
37*789Sahrens  * Virtual device vector for files.
38*789Sahrens  */
39*789Sahrens 
40*789Sahrens static int
41*789Sahrens vdev_file_open(vdev_t *vd, uint64_t *psize, uint64_t *ashift)
42*789Sahrens {
43*789Sahrens 	vdev_file_t *vf;
44*789Sahrens 	vnode_t *vp;
45*789Sahrens 	vattr_t vattr;
46*789Sahrens 	int error;
47*789Sahrens 
48*789Sahrens 	/*
49*789Sahrens 	 * We must have a pathname, and it must be absolute.
50*789Sahrens 	 */
51*789Sahrens 	if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
52*789Sahrens 		vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
53*789Sahrens 		return (EINVAL);
54*789Sahrens 	}
55*789Sahrens 
56*789Sahrens 	vf = vd->vdev_tsd = kmem_zalloc(sizeof (vdev_file_t), KM_SLEEP);
57*789Sahrens 
58*789Sahrens #ifdef _KERNEL
59*789Sahrens 	/*
60*789Sahrens 	 * When using a file vdev in kernel context, the underlying filesystem
61*789Sahrens 	 * will already be caching the data.  Don't cache it again here.
62*789Sahrens 	 */
63*789Sahrens 	vd->vdev_cache.vc_size = 0;
64*789Sahrens #endif
65*789Sahrens 
66*789Sahrens 	/*
67*789Sahrens 	 * We always open the files from the root of the global zone, even if
68*789Sahrens 	 * we're in a local zone.  If the user has gotten to this point, the
69*789Sahrens 	 * administrator has already decided that the pool should be available
70*789Sahrens 	 * to local zone users, so the underlying devices should be as well.
71*789Sahrens 	 */
72*789Sahrens 	ASSERT(vd->vdev_path != NULL && vd->vdev_path[0] == '/');
73*789Sahrens 	error = vn_openat(vd->vdev_path + 1, UIO_SYSSPACE, spa_mode | FOFFMAX,
74*789Sahrens 	    0, &vp, 0, 0, rootdir);
75*789Sahrens 
76*789Sahrens 	if (error) {
77*789Sahrens 		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
78*789Sahrens 		return (error);
79*789Sahrens 	}
80*789Sahrens 
81*789Sahrens 	vf->vf_vnode = vp;
82*789Sahrens 
83*789Sahrens #ifdef _KERNEL
84*789Sahrens 	/*
85*789Sahrens 	 * Make sure it's a regular file.
86*789Sahrens 	 */
87*789Sahrens 	if (vp->v_type != VREG) {
88*789Sahrens 		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
89*789Sahrens 		return (ENODEV);
90*789Sahrens 	}
91*789Sahrens #endif
92*789Sahrens 
93*789Sahrens 	/*
94*789Sahrens 	 * Determine the physical size of the file.
95*789Sahrens 	 */
96*789Sahrens 	vattr.va_mask = AT_SIZE;
97*789Sahrens 	error = VOP_GETATTR(vp, &vattr, 0, kcred);
98*789Sahrens 	if (error) {
99*789Sahrens 		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
100*789Sahrens 		return (error);
101*789Sahrens 	}
102*789Sahrens 
103*789Sahrens 	*psize = vattr.va_size;
104*789Sahrens 	*ashift = SPA_MINBLOCKSHIFT;
105*789Sahrens 
106*789Sahrens 	return (0);
107*789Sahrens }
108*789Sahrens 
109*789Sahrens static void
110*789Sahrens vdev_file_close(vdev_t *vd)
111*789Sahrens {
112*789Sahrens 	vdev_file_t *vf = vd->vdev_tsd;
113*789Sahrens 
114*789Sahrens 	if (vf == NULL)
115*789Sahrens 		return;
116*789Sahrens 
117*789Sahrens 	if (vf->vf_vnode != NULL) {
118*789Sahrens 		(void) VOP_PUTPAGE(vf->vf_vnode, 0, 0, B_INVAL, kcred);
119*789Sahrens 		(void) VOP_CLOSE(vf->vf_vnode, spa_mode, 1, 0, kcred);
120*789Sahrens 		VN_RELE(vf->vf_vnode);
121*789Sahrens 	}
122*789Sahrens 
123*789Sahrens 	kmem_free(vf, sizeof (vdev_file_t));
124*789Sahrens 	vd->vdev_tsd = NULL;
125*789Sahrens }
126*789Sahrens 
127*789Sahrens static void
128*789Sahrens vdev_file_io_start(zio_t *zio)
129*789Sahrens {
130*789Sahrens 	vdev_t *vd = zio->io_vd;
131*789Sahrens 	vdev_file_t *vf = vd->vdev_tsd;
132*789Sahrens 	ssize_t resid;
133*789Sahrens 	int error;
134*789Sahrens 
135*789Sahrens 	if (zio->io_type == ZIO_TYPE_IOCTL) {
136*789Sahrens 		zio_vdev_io_bypass(zio);
137*789Sahrens 
138*789Sahrens 		/* XXPOLICY */
139*789Sahrens 		if (vdev_is_dead(vd)) {
140*789Sahrens 			zio->io_error = ENXIO;
141*789Sahrens 			zio_next_stage_async(zio);
142*789Sahrens 			return;
143*789Sahrens 		}
144*789Sahrens 
145*789Sahrens 		switch (zio->io_cmd) {
146*789Sahrens 		case DKIOCFLUSHWRITECACHE:
147*789Sahrens 			zio->io_error = VOP_FSYNC(vf->vf_vnode, FSYNC | FDSYNC,
148*789Sahrens 			    kcred);
149*789Sahrens 			dprintf("fsync(%s) = %d\n", vdev_description(vd),
150*789Sahrens 			    zio->io_error);
151*789Sahrens 			break;
152*789Sahrens 		default:
153*789Sahrens 			zio->io_error = ENOTSUP;
154*789Sahrens 		}
155*789Sahrens 
156*789Sahrens 		zio_next_stage_async(zio);
157*789Sahrens 		return;
158*789Sahrens 	}
159*789Sahrens 
160*789Sahrens 	if (zio->io_type == ZIO_TYPE_READ && vdev_cache_read(zio) == 0)
161*789Sahrens 		return;
162*789Sahrens 
163*789Sahrens 	if ((zio = vdev_queue_io(zio)) == NULL)
164*789Sahrens 		return;
165*789Sahrens 
166*789Sahrens 	/* XXPOLICY */
167*789Sahrens 	error = vdev_is_dead(vd) ? ENXIO : vdev_error_inject(vd, zio);
168*789Sahrens 	if (error) {
169*789Sahrens 		zio->io_error = error;
170*789Sahrens 		zio_next_stage_async(zio);
171*789Sahrens 		return;
172*789Sahrens 	}
173*789Sahrens 
174*789Sahrens 	zio->io_error = vn_rdwr(zio->io_type == ZIO_TYPE_READ ?
175*789Sahrens 	    UIO_READ : UIO_WRITE, vf->vf_vnode, zio->io_data,
176*789Sahrens 	    zio->io_size, zio->io_offset, UIO_SYSSPACE,
177*789Sahrens 	    0, RLIM64_INFINITY, kcred, &resid);
178*789Sahrens 
179*789Sahrens 	if (resid != 0 && zio->io_error == 0)
180*789Sahrens 		zio->io_error = ENOSPC;
181*789Sahrens 
182*789Sahrens 	zio_next_stage_async(zio);
183*789Sahrens }
184*789Sahrens 
185*789Sahrens static void
186*789Sahrens vdev_file_io_done(zio_t *zio)
187*789Sahrens {
188*789Sahrens 	vdev_queue_io_done(zio);
189*789Sahrens 
190*789Sahrens 	if (zio->io_type == ZIO_TYPE_WRITE)
191*789Sahrens 		vdev_cache_write(zio);
192*789Sahrens 
193*789Sahrens 	zio_next_stage(zio);
194*789Sahrens }
195*789Sahrens 
196*789Sahrens vdev_ops_t vdev_file_ops = {
197*789Sahrens 	vdev_file_open,
198*789Sahrens 	vdev_file_close,
199*789Sahrens 	vdev_default_asize,
200*789Sahrens 	vdev_file_io_start,
201*789Sahrens 	vdev_file_io_done,
202*789Sahrens 	NULL,
203*789Sahrens 	VDEV_TYPE_FILE,		/* name of this vdev type */
204*789Sahrens 	B_TRUE			/* leaf vdev */
205*789Sahrens };
206*789Sahrens 
207*789Sahrens /*
208*789Sahrens  * From userland we access disks just like files.
209*789Sahrens  */
210*789Sahrens #ifndef _KERNEL
211*789Sahrens 
212*789Sahrens vdev_ops_t vdev_disk_ops = {
213*789Sahrens 	vdev_file_open,
214*789Sahrens 	vdev_file_close,
215*789Sahrens 	vdev_default_asize,
216*789Sahrens 	vdev_file_io_start,
217*789Sahrens 	vdev_file_io_done,
218*789Sahrens 	NULL,
219*789Sahrens 	VDEV_TYPE_DISK,		/* name of this vdev type */
220*789Sahrens 	B_TRUE			/* leaf vdev */
221*789Sahrens };
222*789Sahrens 
223*789Sahrens #endif
224