]> git.proxmox.com Git - mirror_zfs.git/blame - module/zfs/vdev_file.c
Introduce ARC Buffer Data (ABD)
[mirror_zfs.git] / module / zfs / vdev_file.c
CommitLineData
34dc7c2f
BB
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/*
428870ff 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
26ef0cc7 23 * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
34dc7c2f
BB
24 */
25
34dc7c2f
BB
26#include <sys/zfs_context.h>
27#include <sys/spa.h>
5853fe79 28#include <sys/spa_impl.h>
34dc7c2f
BB
29#include <sys/vdev_file.h>
30#include <sys/vdev_impl.h>
31#include <sys/zio.h>
32#include <sys/fs/zfs.h>
b128c09f 33#include <sys/fm/fs/zfs.h>
a6255b7f 34#include <sys/abd.h>
34dc7c2f
BB
35
36/*
37 * Virtual device vector for files.
38 */
39
428870ff
BB
40static void
41vdev_file_hold(vdev_t *vd)
42{
43 ASSERT(vd->vdev_path != NULL);
44}
45
46static void
47vdev_file_rele(vdev_t *vd)
48{
49 ASSERT(vd->vdev_path != NULL);
50}
51
34dc7c2f 52static int
1bd201e7
CS
53vdev_file_open(vdev_t *vd, uint64_t *psize, uint64_t *max_psize,
54 uint64_t *ashift)
34dc7c2f
BB
55{
56 vdev_file_t *vf;
57 vnode_t *vp;
b128c09f 58 vattr_t vattr;
34dc7c2f
BB
59 int error;
60
fb40095f
RY
61 /* Rotational optimizations only make sense on block devices */
62 vd->vdev_nonrot = B_TRUE;
63
34dc7c2f
BB
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;
2e528b49 69 return (SET_ERROR(EINVAL));
34dc7c2f
BB
70 }
71
428870ff
BB
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
79c76d5b 82 vf = vd->vdev_tsd = kmem_zalloc(sizeof (vdev_file_t), KM_SLEEP);
34dc7c2f
BB
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,
fb5f0bc8 92 spa_mode(vd->vdev_spa) | FOFFMAX, 0, &vp, 0, 0, rootdir, -1);
34dc7c2f
BB
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;
2e528b49 107 return (SET_ERROR(ENODEV));
34dc7c2f
BB
108 }
109#endif
428870ff
BB
110
111skip_open:
34dc7c2f
BB
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
1bd201e7 122 *max_psize = *psize = vattr.va_size;
34dc7c2f
BB
123 *ashift = SPA_MINBLOCKSHIFT;
124
125 return (0);
126}
127
128static void
129vdev_file_close(vdev_t *vd)
130{
131 vdev_file_t *vf = vd->vdev_tsd;
132
428870ff 133 if (vd->vdev_reopening || vf == NULL)
34dc7c2f
BB
134 return;
135
136 if (vf->vf_vnode != NULL) {
137 (void) VOP_PUTPAGE(vf->vf_vnode, 0, 0, B_INVAL, kcred, NULL);
fb5f0bc8
BB
138 (void) VOP_CLOSE(vf->vf_vnode, spa_mode(vd->vdev_spa), 1, 0,
139 kcred, NULL);
34dc7c2f
BB
140 }
141
428870ff 142 vd->vdev_delayed_close = B_FALSE;
34dc7c2f
BB
143 kmem_free(vf, sizeof (vdev_file_t));
144 vd->vdev_tsd = NULL;
145}
146
5853fe79
GW
147static void
148vdev_file_io_strategy(void *arg)
34dc7c2f 149{
5853fe79 150 zio_t *zio = (zio_t *)arg;
34dc7c2f 151 vdev_t *vd = zio->io_vd;
5853fe79
GW
152 vdev_file_t *vf = vd->vdev_tsd;
153 ssize_t resid;
a6255b7f
DQ
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);
34dc7c2f 160
5853fe79 161 zio->io_error = vn_rdwr(zio->io_type == ZIO_TYPE_READ ?
a6255b7f
DQ
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);
5853fe79
GW
169
170 if (resid != 0 && zio->io_error == 0)
2e528b49 171 zio->io_error = SET_ERROR(ENOSPC);
3adfc400 172
26ef0cc7 173 zio_delay_interrupt(zio);
5853fe79
GW
174}
175
92119cc2
BB
176static void
177vdev_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
98b25418 187static void
5853fe79
GW
188vdev_file_io_start(zio_t *zio)
189{
5853fe79
GW
190 vdev_t *vd = zio->io_vd;
191 vdev_file_t *vf = vd->vdev_tsd;
34dc7c2f 192
3adfc400 193 if (zio->io_type == ZIO_TYPE_IOCTL) {
5853fe79
GW
194 /* XXPOLICY */
195 if (!vdev_readable(vd)) {
2e528b49 196 zio->io_error = SET_ERROR(ENXIO);
98b25418
GW
197 zio_interrupt(zio);
198 return;
5853fe79
GW
199 }
200
34dc7c2f
BB
201 switch (zio->io_cmd) {
202 case DKIOCFLUSHWRITECACHE:
f9a1ac4d
H
203
204 if (zfs_nocacheflush)
205 break;
206
92119cc2
BB
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()) {
aa9af22c 215 VERIFY3U(taskq_dispatch(system_taskq,
48d3eb40
BB
216 vdev_file_io_fsync, zio, TQ_SLEEP), !=,
217 TASKQID_INVALID);
98b25418 218 return;
92119cc2
BB
219 }
220
34dc7c2f
BB
221 zio->io_error = VOP_FSYNC(vf->vf_vnode, FSYNC | FDSYNC,
222 kcred, NULL);
34dc7c2f
BB
223 break;
224 default:
2e528b49 225 zio->io_error = SET_ERROR(ENOTSUP);
34dc7c2f
BB
226 }
227
98b25418
GW
228 zio_execute(zio);
229 return;
34dc7c2f
BB
230 }
231
26ef0cc7
TH
232 zio->io_target_timestamp = zio_handle_io_delay(zio);
233
aa9af22c 234 VERIFY3U(taskq_dispatch(system_taskq, vdev_file_io_strategy, zio,
48d3eb40 235 TQ_SLEEP), !=, TASKQID_INVALID);
34dc7c2f
BB
236}
237
b128c09f
BB
238/* ARGSUSED */
239static void
34dc7c2f
BB
240vdev_file_io_done(zio_t *zio)
241{
34dc7c2f
BB
242}
243
244vdev_ops_t vdev_file_ops = {
245 vdev_file_open,
246 vdev_file_close,
34dc7c2f
BB
247 vdev_default_asize,
248 vdev_file_io_start,
249 vdev_file_io_done,
250 NULL,
428870ff
BB
251 vdev_file_hold,
252 vdev_file_rele,
34dc7c2f
BB
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
262vdev_ops_t vdev_disk_ops = {
263 vdev_file_open,
264 vdev_file_close,
34dc7c2f
BB
265 vdev_default_asize,
266 vdev_file_io_start,
267 vdev_file_io_done,
268 NULL,
428870ff
BB
269 vdev_file_hold,
270 vdev_file_rele,
34dc7c2f
BB
271 VDEV_TYPE_DISK, /* name of this vdev type */
272 B_TRUE /* leaf vdev */
273};
274
275#endif