]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/vdev_file.c
Introduce ARC Buffer Data (ABD)
[mirror_zfs.git] / module / zfs / vdev_file.c
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, 2015 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 }
141
142 vd->vdev_delayed_close = B_FALSE;
143 kmem_free(vf, sizeof (vdev_file_t));
144 vd->vdev_tsd = NULL;
145 }
146
147 static void
148 vdev_file_io_strategy(void *arg)
149 {
150 zio_t *zio = (zio_t *)arg;
151 vdev_t *vd = zio->io_vd;
152 vdev_file_t *vf = vd->vdev_tsd;
153 ssize_t resid;
154 void *buf;
155
156 if (zio->io_type == ZIO_TYPE_READ)
157 buf = abd_borrow_buf(zio->io_abd, zio->io_size);
158 else
159 buf = abd_borrow_buf_copy(zio->io_abd, zio->io_size);
160
161 zio->io_error = vn_rdwr(zio->io_type == ZIO_TYPE_READ ?
162 UIO_READ : UIO_WRITE, vf->vf_vnode, buf, zio->io_size,
163 zio->io_offset, UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
164
165 if (zio->io_type == ZIO_TYPE_READ)
166 abd_return_buf_copy(zio->io_abd, buf, zio->io_size);
167 else
168 abd_return_buf(zio->io_abd, buf, zio->io_size);
169
170 if (resid != 0 && zio->io_error == 0)
171 zio->io_error = SET_ERROR(ENOSPC);
172
173 zio_delay_interrupt(zio);
174 }
175
176 static void
177 vdev_file_io_fsync(void *arg)
178 {
179 zio_t *zio = (zio_t *)arg;
180 vdev_file_t *vf = zio->io_vd->vdev_tsd;
181
182 zio->io_error = VOP_FSYNC(vf->vf_vnode, FSYNC | FDSYNC, kcred, NULL);
183
184 zio_interrupt(zio);
185 }
186
187 static void
188 vdev_file_io_start(zio_t *zio)
189 {
190 vdev_t *vd = zio->io_vd;
191 vdev_file_t *vf = vd->vdev_tsd;
192
193 if (zio->io_type == ZIO_TYPE_IOCTL) {
194 /* XXPOLICY */
195 if (!vdev_readable(vd)) {
196 zio->io_error = SET_ERROR(ENXIO);
197 zio_interrupt(zio);
198 return;
199 }
200
201 switch (zio->io_cmd) {
202 case DKIOCFLUSHWRITECACHE:
203
204 if (zfs_nocacheflush)
205 break;
206
207 /*
208 * We cannot safely call vfs_fsync() when PF_FSTRANS
209 * is set in the current context. Filesystems like
210 * XFS include sanity checks to verify it is not
211 * already set, see xfs_vm_writepage(). Therefore
212 * the sync must be dispatched to a different context.
213 */
214 if (spl_fstrans_check()) {
215 VERIFY3U(taskq_dispatch(system_taskq,
216 vdev_file_io_fsync, zio, TQ_SLEEP), !=,
217 TASKQID_INVALID);
218 return;
219 }
220
221 zio->io_error = VOP_FSYNC(vf->vf_vnode, FSYNC | FDSYNC,
222 kcred, NULL);
223 break;
224 default:
225 zio->io_error = SET_ERROR(ENOTSUP);
226 }
227
228 zio_execute(zio);
229 return;
230 }
231
232 zio->io_target_timestamp = zio_handle_io_delay(zio);
233
234 VERIFY3U(taskq_dispatch(system_taskq, vdev_file_io_strategy, zio,
235 TQ_SLEEP), !=, TASKQID_INVALID);
236 }
237
238 /* ARGSUSED */
239 static void
240 vdev_file_io_done(zio_t *zio)
241 {
242 }
243
244 vdev_ops_t vdev_file_ops = {
245 vdev_file_open,
246 vdev_file_close,
247 vdev_default_asize,
248 vdev_file_io_start,
249 vdev_file_io_done,
250 NULL,
251 vdev_file_hold,
252 vdev_file_rele,
253 VDEV_TYPE_FILE, /* name of this vdev type */
254 B_TRUE /* leaf vdev */
255 };
256
257 /*
258 * From userland we access disks just like files.
259 */
260 #ifndef _KERNEL
261
262 vdev_ops_t vdev_disk_ops = {
263 vdev_file_open,
264 vdev_file_close,
265 vdev_default_asize,
266 vdev_file_io_start,
267 vdev_file_io_done,
268 NULL,
269 vdev_file_hold,
270 vdev_file_rele,
271 VDEV_TYPE_DISK, /* name of this vdev type */
272 B_TRUE /* leaf vdev */
273 };
274
275 #endif