]>
git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/vdev_file.c
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.
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.
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]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
26 #include <sys/zfs_context.h>
28 #include <sys/spa_impl.h>
29 #include <sys/vdev_file.h>
30 #include <sys/vdev_impl.h>
32 #include <sys/fs/zfs.h>
33 #include <sys/fm/fs/zfs.h>
37 * Virtual device vector for files.
41 vdev_file_hold(vdev_t
*vd
)
43 ASSERT(vd
->vdev_path
!= NULL
);
47 vdev_file_rele(vdev_t
*vd
)
49 ASSERT(vd
->vdev_path
!= NULL
);
53 vdev_file_open(vdev_t
*vd
, uint64_t *psize
, uint64_t *max_psize
,
61 /* Rotational optimizations only make sense on block devices */
62 vd
->vdev_nonrot
= B_TRUE
;
65 * We must have a pathname, and it must be absolute.
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
));
73 * Reopen the device if it's not currently open. Otherwise,
74 * just update the physical size of the device.
76 if (vd
->vdev_tsd
!= NULL
) {
77 ASSERT(vd
->vdev_reopening
);
82 vf
= vd
->vdev_tsd
= kmem_zalloc(sizeof (vdev_file_t
), KM_SLEEP
);
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.
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);
95 vd
->vdev_stat
.vs_aux
= VDEV_AUX_OPEN_FAILED
;
103 * Make sure it's a regular file.
105 if (vp
->v_type
!= VREG
) {
106 vd
->vdev_stat
.vs_aux
= VDEV_AUX_OPEN_FAILED
;
107 return (SET_ERROR(ENODEV
));
113 * Determine the physical size of the file.
115 vattr
.va_mask
= AT_SIZE
;
116 error
= VOP_GETATTR(vf
->vf_vnode
, &vattr
, 0, kcred
, NULL
);
118 vd
->vdev_stat
.vs_aux
= VDEV_AUX_OPEN_FAILED
;
122 *max_psize
= *psize
= vattr
.va_size
;
123 *ashift
= SPA_MINBLOCKSHIFT
;
129 vdev_file_close(vdev_t
*vd
)
131 vdev_file_t
*vf
= vd
->vdev_tsd
;
133 if (vd
->vdev_reopening
|| vf
== NULL
)
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,
142 vd
->vdev_delayed_close
= B_FALSE
;
143 kmem_free(vf
, sizeof (vdev_file_t
));
148 vdev_file_io_strategy(void *arg
)
150 zio_t
*zio
= (zio_t
*)arg
;
151 vdev_t
*vd
= zio
->io_vd
;
152 vdev_file_t
*vf
= vd
->vdev_tsd
;
156 if (zio
->io_type
== ZIO_TYPE_READ
)
157 buf
= abd_borrow_buf(zio
->io_abd
, zio
->io_size
);
159 buf
= abd_borrow_buf_copy(zio
->io_abd
, zio
->io_size
);
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
);
165 if (zio
->io_type
== ZIO_TYPE_READ
)
166 abd_return_buf_copy(zio
->io_abd
, buf
, zio
->io_size
);
168 abd_return_buf(zio
->io_abd
, buf
, zio
->io_size
);
170 if (resid
!= 0 && zio
->io_error
== 0)
171 zio
->io_error
= SET_ERROR(ENOSPC
);
173 zio_delay_interrupt(zio
);
177 vdev_file_io_fsync(void *arg
)
179 zio_t
*zio
= (zio_t
*)arg
;
180 vdev_file_t
*vf
= zio
->io_vd
->vdev_tsd
;
182 zio
->io_error
= VOP_FSYNC(vf
->vf_vnode
, FSYNC
| FDSYNC
, kcred
, NULL
);
188 vdev_file_io_start(zio_t
*zio
)
190 vdev_t
*vd
= zio
->io_vd
;
191 vdev_file_t
*vf
= vd
->vdev_tsd
;
193 if (zio
->io_type
== ZIO_TYPE_IOCTL
) {
195 if (!vdev_readable(vd
)) {
196 zio
->io_error
= SET_ERROR(ENXIO
);
201 switch (zio
->io_cmd
) {
202 case DKIOCFLUSHWRITECACHE
:
204 if (zfs_nocacheflush
)
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.
214 if (spl_fstrans_check()) {
215 VERIFY3U(taskq_dispatch(system_taskq
,
216 vdev_file_io_fsync
, zio
, TQ_SLEEP
), !=,
221 zio
->io_error
= VOP_FSYNC(vf
->vf_vnode
, FSYNC
| FDSYNC
,
225 zio
->io_error
= SET_ERROR(ENOTSUP
);
232 zio
->io_target_timestamp
= zio_handle_io_delay(zio
);
234 VERIFY3U(taskq_dispatch(system_taskq
, vdev_file_io_strategy
, zio
,
235 TQ_SLEEP
), !=, TASKQID_INVALID
);
240 vdev_file_io_done(zio_t
*zio
)
244 vdev_ops_t vdev_file_ops
= {
253 VDEV_TYPE_FILE
, /* name of this vdev type */
254 B_TRUE
/* leaf vdev */
258 * From userland we access disks just like files.
262 vdev_ops_t vdev_disk_ops
= {
271 VDEV_TYPE_DISK
, /* name of this vdev type */
272 B_TRUE
/* leaf vdev */