xref: /onnv-gate/usr/src/uts/common/fs/zfs/vdev_file.c (revision 12247:5bcd281629f8)
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*12247SGeorge.Wilson@Sun.COM  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23789Sahrens  */
24789Sahrens 
25789Sahrens #include <sys/zfs_context.h>
26789Sahrens #include <sys/spa.h>
27789Sahrens #include <sys/vdev_file.h>
28789Sahrens #include <sys/vdev_impl.h>
29789Sahrens #include <sys/zio.h>
30789Sahrens #include <sys/fs/zfs.h>
316976Seschrock #include <sys/fm/fs/zfs.h>
32789Sahrens 
33789Sahrens /*
34789Sahrens  * Virtual device vector for files.
35789Sahrens  */
36789Sahrens 
3711958SGeorge.Wilson@Sun.COM static void
vdev_file_hold(vdev_t * vd)3811958SGeorge.Wilson@Sun.COM vdev_file_hold(vdev_t *vd)
3911958SGeorge.Wilson@Sun.COM {
4011958SGeorge.Wilson@Sun.COM 	ASSERT(vd->vdev_path != NULL);
4111958SGeorge.Wilson@Sun.COM }
4211958SGeorge.Wilson@Sun.COM 
4311958SGeorge.Wilson@Sun.COM static void
vdev_file_rele(vdev_t * vd)4411958SGeorge.Wilson@Sun.COM vdev_file_rele(vdev_t *vd)
4511958SGeorge.Wilson@Sun.COM {
4611958SGeorge.Wilson@Sun.COM 	ASSERT(vd->vdev_path != NULL);
4711958SGeorge.Wilson@Sun.COM }
4811958SGeorge.Wilson@Sun.COM 
49789Sahrens static int
vdev_file_open(vdev_t * vd,uint64_t * psize,uint64_t * ashift)507754SJeff.Bonwick@Sun.COM vdev_file_open(vdev_t *vd, uint64_t *psize, uint64_t *ashift)
51789Sahrens {
52789Sahrens 	vdev_file_t *vf;
53789Sahrens 	vnode_t *vp;
547754SJeff.Bonwick@Sun.COM 	vattr_t vattr;
55789Sahrens 	int error;
56789Sahrens 
57789Sahrens 	/*
58789Sahrens 	 * We must have a pathname, and it must be absolute.
59789Sahrens 	 */
60789Sahrens 	if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
61789Sahrens 		vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
62789Sahrens 		return (EINVAL);
63789Sahrens 	}
64789Sahrens 
6510850SGeorge.Wilson@Sun.COM 	/*
6610850SGeorge.Wilson@Sun.COM 	 * Reopen the device if it's not currently open.  Otherwise,
6710850SGeorge.Wilson@Sun.COM 	 * just update the physical size of the device.
6810850SGeorge.Wilson@Sun.COM 	 */
6910850SGeorge.Wilson@Sun.COM 	if (vd->vdev_tsd != NULL) {
7010850SGeorge.Wilson@Sun.COM 		ASSERT(vd->vdev_reopening);
7110850SGeorge.Wilson@Sun.COM 		vf = vd->vdev_tsd;
7210850SGeorge.Wilson@Sun.COM 		goto skip_open;
7310850SGeorge.Wilson@Sun.COM 	}
7410850SGeorge.Wilson@Sun.COM 
75789Sahrens 	vf = vd->vdev_tsd = kmem_zalloc(sizeof (vdev_file_t), KM_SLEEP);
76789Sahrens 
77789Sahrens 	/*
78789Sahrens 	 * We always open the files from the root of the global zone, even if
79789Sahrens 	 * we're in a local zone.  If the user has gotten to this point, the
80789Sahrens 	 * administrator has already decided that the pool should be available
81789Sahrens 	 * to local zone users, so the underlying devices should be as well.
82789Sahrens 	 */
83789Sahrens 	ASSERT(vd->vdev_path != NULL && vd->vdev_path[0] == '/');
845329Sgw25295 	error = vn_openat(vd->vdev_path + 1, UIO_SYSSPACE,
858241SJeff.Bonwick@Sun.COM 	    spa_mode(vd->vdev_spa) | FOFFMAX, 0, &vp, 0, 0, rootdir, -1);
86789Sahrens 
87789Sahrens 	if (error) {
88789Sahrens 		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
89789Sahrens 		return (error);
90789Sahrens 	}
91789Sahrens 
92789Sahrens 	vf->vf_vnode = vp;
93789Sahrens 
94789Sahrens #ifdef _KERNEL
95789Sahrens 	/*
96789Sahrens 	 * Make sure it's a regular file.
97789Sahrens 	 */
98789Sahrens 	if (vp->v_type != VREG) {
99789Sahrens 		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
100789Sahrens 		return (ENODEV);
101789Sahrens 	}
102789Sahrens #endif
10310850SGeorge.Wilson@Sun.COM 
10410850SGeorge.Wilson@Sun.COM skip_open:
105789Sahrens 	/*
106789Sahrens 	 * Determine the physical size of the file.
107789Sahrens 	 */
108789Sahrens 	vattr.va_mask = AT_SIZE;
1095331Samw 	error = VOP_GETATTR(vf->vf_vnode, &vattr, 0, kcred, NULL);
110789Sahrens 	if (error) {
111789Sahrens 		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
112789Sahrens 		return (error);
113789Sahrens 	}
114789Sahrens 
115789Sahrens 	*psize = vattr.va_size;
116789Sahrens 	*ashift = SPA_MINBLOCKSHIFT;
117789Sahrens 
118789Sahrens 	return (0);
119789Sahrens }
120789Sahrens 
121789Sahrens static void
vdev_file_close(vdev_t * vd)122789Sahrens vdev_file_close(vdev_t *vd)
123789Sahrens {
124789Sahrens 	vdev_file_t *vf = vd->vdev_tsd;
125789Sahrens 
12610850SGeorge.Wilson@Sun.COM 	if (vd->vdev_reopening || vf == NULL)
127789Sahrens 		return;
128789Sahrens 
129789Sahrens 	if (vf->vf_vnode != NULL) {
1305331Samw 		(void) VOP_PUTPAGE(vf->vf_vnode, 0, 0, B_INVAL, kcred, NULL);
1318241SJeff.Bonwick@Sun.COM 		(void) VOP_CLOSE(vf->vf_vnode, spa_mode(vd->vdev_spa), 1, 0,
1328241SJeff.Bonwick@Sun.COM 		    kcred, NULL);
133789Sahrens 		VN_RELE(vf->vf_vnode);
134789Sahrens 	}
135789Sahrens 
136*12247SGeorge.Wilson@Sun.COM 	vd->vdev_delayed_close = B_FALSE;
137789Sahrens 	kmem_free(vf, sizeof (vdev_file_t));
138789Sahrens 	vd->vdev_tsd = NULL;
139789Sahrens }
140789Sahrens 
1415329Sgw25295 static int
vdev_file_io_start(zio_t * zio)142789Sahrens vdev_file_io_start(zio_t *zio)
143789Sahrens {
144789Sahrens 	vdev_t *vd = zio->io_vd;
145789Sahrens 	vdev_file_t *vf = vd->vdev_tsd;
146789Sahrens 	ssize_t resid;
147789Sahrens 
148789Sahrens 	if (zio->io_type == ZIO_TYPE_IOCTL) {
149789Sahrens 		/* XXPOLICY */
1505329Sgw25295 		if (!vdev_readable(vd)) {
151789Sahrens 			zio->io_error = ENXIO;
1525530Sbonwick 			return (ZIO_PIPELINE_CONTINUE);
153789Sahrens 		}
154789Sahrens 
155789Sahrens 		switch (zio->io_cmd) {
156789Sahrens 		case DKIOCFLUSHWRITECACHE:
157789Sahrens 			zio->io_error = VOP_FSYNC(vf->vf_vnode, FSYNC | FDSYNC,
1585331Samw 			    kcred, NULL);
159789Sahrens 			break;
160789Sahrens 		default:
161789Sahrens 			zio->io_error = ENOTSUP;
162789Sahrens 		}
163789Sahrens 
1645530Sbonwick 		return (ZIO_PIPELINE_CONTINUE);
165789Sahrens 	}
166789Sahrens 
167789Sahrens 	zio->io_error = vn_rdwr(zio->io_type == ZIO_TYPE_READ ?
168789Sahrens 	    UIO_READ : UIO_WRITE, vf->vf_vnode, zio->io_data,
169789Sahrens 	    zio->io_size, zio->io_offset, UIO_SYSSPACE,
170789Sahrens 	    0, RLIM64_INFINITY, kcred, &resid);
171789Sahrens 
172789Sahrens 	if (resid != 0 && zio->io_error == 0)
173789Sahrens 		zio->io_error = ENOSPC;
174789Sahrens 
1755530Sbonwick 	zio_interrupt(zio);
1765530Sbonwick 
1775530Sbonwick 	return (ZIO_PIPELINE_STOP);
178789Sahrens }
179789Sahrens 
1807754SJeff.Bonwick@Sun.COM /* ARGSUSED */
1817754SJeff.Bonwick@Sun.COM static void
vdev_file_io_done(zio_t * zio)182789Sahrens vdev_file_io_done(zio_t *zio)
183789Sahrens {
184789Sahrens }
185789Sahrens 
186789Sahrens vdev_ops_t vdev_file_ops = {
187789Sahrens 	vdev_file_open,
188789Sahrens 	vdev_file_close,
189789Sahrens 	vdev_default_asize,
190789Sahrens 	vdev_file_io_start,
191789Sahrens 	vdev_file_io_done,
192789Sahrens 	NULL,
19311958SGeorge.Wilson@Sun.COM 	vdev_file_hold,
19411958SGeorge.Wilson@Sun.COM 	vdev_file_rele,
195789Sahrens 	VDEV_TYPE_FILE,		/* name of this vdev type */
196789Sahrens 	B_TRUE			/* leaf vdev */
197789Sahrens };
198789Sahrens 
199789Sahrens /*
200789Sahrens  * From userland we access disks just like files.
201789Sahrens  */
202789Sahrens #ifndef _KERNEL
203789Sahrens 
204789Sahrens vdev_ops_t vdev_disk_ops = {
205789Sahrens 	vdev_file_open,
206789Sahrens 	vdev_file_close,
207789Sahrens 	vdev_default_asize,
208789Sahrens 	vdev_file_io_start,
209789Sahrens 	vdev_file_io_done,
210789Sahrens 	NULL,
21111958SGeorge.Wilson@Sun.COM 	vdev_file_hold,
21211958SGeorge.Wilson@Sun.COM 	vdev_file_rele,
213789Sahrens 	VDEV_TYPE_DISK,		/* name of this vdev type */
214789Sahrens 	B_TRUE			/* leaf vdev */
215789Sahrens };
216789Sahrens 
217789Sahrens #endif
218