xref: /illumos-gate/usr/src/uts/common/fs/zfs/vdev_file.c (revision 12a8814c)
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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 2011, 2016 by Delphix. All rights reserved.
24  */
25 
26 #include <sys/zfs_context.h>
27 #include <sys/spa.h>
28 #include <sys/spa_impl.h>
29 #include <sys/vdev_file.h>
30 #include <sys/vdev_impl.h>
31 #include <sys/zio.h>
32 #include <sys/fs/zfs.h>
33 #include <sys/fm/fs/zfs.h>
34 #include <sys/abd.h>
35 
36 /*
37  * Virtual device vector for files.
38  */
39 
40 static void
41 vdev_file_hold(vdev_t *vd)
42 {
43 	ASSERT(vd->vdev_path != NULL);
44 }
45 
46 static void
47 vdev_file_rele(vdev_t *vd)
48 {
49 	ASSERT(vd->vdev_path != NULL);
50 }
51 
52 static int
53 vdev_file_open(vdev_t *vd, uint64_t *psize, uint64_t *max_psize,
54     uint64_t *ashift)
55 {
56 	vdev_file_t *vf;
57 	vnode_t *vp;
58 	vattr_t vattr;
59 	int error;
60 
61 	/* Rotational optimizations only make sense on block devices */
62 	vd->vdev_nonrot = B_TRUE;
63 
64 	/*
65 	 * We must have a pathname, and it must be absolute.
66 	 */
67 	if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
68 		vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
69 		return (SET_ERROR(EINVAL));
70 	}
71 
72 	/*
73 	 * Reopen the device if it's not currently open.  Otherwise,
74 	 * just update the physical size of the device.
75 	 */
76 	if (vd->vdev_tsd != NULL) {
77 		ASSERT(vd->vdev_reopening);
78 		vf = vd->vdev_tsd;
79 		goto skip_open;
80 	}
81 
82 	vf = vd->vdev_tsd = kmem_zalloc(sizeof (vdev_file_t), KM_SLEEP);
83 
84 	/*
85 	 * We always open the files from the root of the global zone, even if
86 	 * we're in a local zone.  If the user has gotten to this point, the
87 	 * administrator has already decided that the pool should be available
88 	 * to local zone users, so the underlying devices should be as well.
89 	 */
90 	ASSERT(vd->vdev_path != NULL && vd->vdev_path[0] == '/');
91 	error = vn_openat(vd->vdev_path + 1, UIO_SYSSPACE,
92 	    spa_mode(vd->vdev_spa) | FOFFMAX, 0, &vp, 0, 0, rootdir, -1);
93 
94 	if (error) {
95 		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
96 		return (error);
97 	}
98 
99 	vf->vf_vnode = vp;
100 
101 #ifdef _KERNEL
102 	/*
103 	 * Make sure it's a regular file.
104 	 */
105 	if (vp->v_type != VREG) {
106 		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
107 		return (SET_ERROR(ENODEV));
108 	}
109 #endif
110 
111 skip_open:
112 	/*
113 	 * Determine the physical size of the file.
114 	 */
115 	vattr.va_mask = AT_SIZE;
116 	error = VOP_GETATTR(vf->vf_vnode, &vattr, 0, kcred, NULL);
117 	if (error) {
118 		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
119 		return (error);
120 	}
121 
122 	*max_psize = *psize = vattr.va_size;
123 	*ashift = SPA_MINBLOCKSHIFT;
124 
125 	return (0);
126 }
127 
128 static void
129 vdev_file_close(vdev_t *vd)
130 {
131 	vdev_file_t *vf = vd->vdev_tsd;
132 
133 	if (vd->vdev_reopening || vf == NULL)
134 		return;
135 
136 	if (vf->vf_vnode != NULL) {
137 		(void) VOP_PUTPAGE(vf->vf_vnode, 0, 0, B_INVAL, kcred, NULL);
138 		(void) VOP_CLOSE(vf->vf_vnode, spa_mode(vd->vdev_spa), 1, 0,
139 		    kcred, NULL);
140 		VN_RELE(vf->vf_vnode);
141 	}
142 
143 	vd->vdev_delayed_close = B_FALSE;
144 	kmem_free(vf, sizeof (vdev_file_t));
145 	vd->vdev_tsd = NULL;
146 }
147 
148 /*
149  * Implements the interrupt side for file vdev types. This routine will be
150  * called when the I/O completes allowing us to transfer the I/O to the
151  * interrupt taskqs. For consistency, the code structure mimics disk vdev
152  * types.
153  */
154 static int
155 vdev_file_io_intr(buf_t *bp)
156 {
157 	vdev_buf_t *vb = (vdev_buf_t *)bp;
158 	zio_t *zio = vb->vb_io;
159 
160 	zio->io_error = (geterror(bp) != 0 ? EIO : 0);
161 	if (zio->io_error == 0 && bp->b_resid != 0)
162 		zio->io_error = SET_ERROR(ENOSPC);
163 
164 	if (zio->io_type == ZIO_TYPE_READ) {
165 		abd_return_buf_copy(zio->io_abd, bp->b_un.b_addr, zio->io_size);
166 	} else {
167 		abd_return_buf(zio->io_abd, bp->b_un.b_addr, zio->io_size);
168 	}
169 
170 	kmem_free(vb, sizeof (vdev_buf_t));
171 	zio_delay_interrupt(zio);
172 	return (0);
173 }
174 
175 static void
176 vdev_file_io_strategy(void *arg)
177 {
178 	buf_t *bp = arg;
179 	vnode_t *vp = bp->b_private;
180 	ssize_t resid;
181 	int error;
182 
183 	error = vn_rdwr((bp->b_flags & B_READ) ? UIO_READ : UIO_WRITE,
184 	    vp, bp->b_un.b_addr, bp->b_bcount, ldbtob(bp->b_lblkno),
185 	    UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
186 
187 	if (error == 0) {
188 		bp->b_resid = resid;
189 		biodone(bp);
190 	} else {
191 		bioerror(bp, error);
192 		biodone(bp);
193 	}
194 }
195 
196 static void
197 vdev_file_io_start(zio_t *zio)
198 {
199 	vdev_t *vd = zio->io_vd;
200 	vdev_file_t *vf = vd->vdev_tsd;
201 	vdev_buf_t *vb;
202 	buf_t *bp;
203 
204 	if (zio->io_type == ZIO_TYPE_IOCTL) {
205 		/* XXPOLICY */
206 		if (!vdev_readable(vd)) {
207 			zio->io_error = SET_ERROR(ENXIO);
208 			zio_interrupt(zio);
209 			return;
210 		}
211 
212 		switch (zio->io_cmd) {
213 		case DKIOCFLUSHWRITECACHE:
214 			zio->io_error = VOP_FSYNC(vf->vf_vnode, FSYNC | FDSYNC,
215 			    kcred, NULL);
216 			break;
217 		default:
218 			zio->io_error = SET_ERROR(ENOTSUP);
219 		}
220 
221 		zio_execute(zio);
222 		return;
223 	}
224 
225 	ASSERT(zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE);
226 	zio->io_target_timestamp = zio_handle_io_delay(zio);
227 
228 	vb = kmem_alloc(sizeof (vdev_buf_t), KM_SLEEP);
229 
230 	vb->vb_io = zio;
231 	bp = &vb->vb_buf;
232 
233 	bioinit(bp);
234 	bp->b_flags = (zio->io_type == ZIO_TYPE_READ ? B_READ : B_WRITE);
235 	bp->b_bcount = zio->io_size;
236 
237 	if (zio->io_type == ZIO_TYPE_READ) {
238 		bp->b_un.b_addr =
239 		    abd_borrow_buf(zio->io_abd, zio->io_size);
240 	} else {
241 		bp->b_un.b_addr =
242 		    abd_borrow_buf_copy(zio->io_abd, zio->io_size);
243 	}
244 
245 	bp->b_lblkno = lbtodb(zio->io_offset);
246 	bp->b_bufsize = zio->io_size;
247 	bp->b_private = vf->vf_vnode;
248 	bp->b_iodone = vdev_file_io_intr;
249 
250 	VERIFY3U(taskq_dispatch(system_taskq, vdev_file_io_strategy, bp,
251 	    TQ_SLEEP), !=, TASKQID_INVALID);
252 }
253 
254 /* ARGSUSED */
255 static void
256 vdev_file_io_done(zio_t *zio)
257 {
258 }
259 
260 vdev_ops_t vdev_file_ops = {
261 	.vdev_op_open = vdev_file_open,
262 	.vdev_op_close = vdev_file_close,
263 	.vdev_op_asize = vdev_default_asize,
264 	.vdev_op_io_start = vdev_file_io_start,
265 	.vdev_op_io_done = vdev_file_io_done,
266 	.vdev_op_state_change = NULL,
267 	.vdev_op_need_resilver = NULL,
268 	.vdev_op_hold = vdev_file_hold,
269 	.vdev_op_rele = vdev_file_rele,
270 	.vdev_op_remap = NULL,
271 	.vdev_op_xlate = vdev_default_xlate,
272 	.vdev_op_type = VDEV_TYPE_FILE,		/* name of this vdev type */
273 	.vdev_op_leaf = B_TRUE			/* leaf vdev */
274 };
275 
276 /*
277  * From userland we access disks just like files.
278  */
279 #ifndef _KERNEL
280 
281 vdev_ops_t vdev_disk_ops = {
282 	.vdev_op_open = vdev_file_open,
283 	.vdev_op_close = vdev_file_close,
284 	.vdev_op_asize = vdev_default_asize,
285 	.vdev_op_io_start = vdev_file_io_start,
286 	.vdev_op_io_done = vdev_file_io_done,
287 	.vdev_op_state_change = NULL,
288 	.vdev_op_need_resilver = NULL,
289 	.vdev_op_hold = vdev_file_hold,
290 	.vdev_op_rele = vdev_file_rele,
291 	.vdev_op_remap = NULL,
292 	.vdev_op_xlate = vdev_default_xlate,
293 	.vdev_op_type = VDEV_TYPE_DISK,		/* name of this vdev type */
294 	.vdev_op_leaf = B_TRUE			/* leaf vdev */
295 };
296 
297 #endif
298