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, 2020 by Delphix. All rights reserved.
26 #include <sys/zfs_context.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>
38 * Virtual device vector for files.
41 static taskq_t
*vdev_file_taskq
;
43 unsigned long vdev_file_logical_ashift
= SPA_MINBLOCKSHIFT
;
44 unsigned long vdev_file_physical_ashift
= SPA_MINBLOCKSHIFT
;
49 vdev_file_taskq
= taskq_create("z_vdev_file", MAX(max_ncpus
, 16),
50 minclsyspri
, max_ncpus
, INT_MAX
, 0);
56 taskq_destroy(vdev_file_taskq
);
60 vdev_file_hold(vdev_t
*vd
)
62 ASSERT(vd
->vdev_path
!= NULL
);
66 vdev_file_rele(vdev_t
*vd
)
68 ASSERT(vd
->vdev_path
!= NULL
);
72 vdev_file_open_mode(spa_mode_t spa_mode
)
76 if ((spa_mode
& SPA_MODE_READ
) && (spa_mode
& SPA_MODE_WRITE
)) {
78 } else if (spa_mode
& SPA_MODE_READ
) {
80 } else if (spa_mode
& SPA_MODE_WRITE
) {
84 return (mode
| O_LARGEFILE
);
88 vdev_file_open(vdev_t
*vd
, uint64_t *psize
, uint64_t *max_psize
,
89 uint64_t *logical_ashift
, uint64_t *physical_ashift
)
97 * Rotational optimizations only make sense on block devices.
99 vd
->vdev_nonrot
= B_TRUE
;
102 * Allow TRIM on file based vdevs. This may not always be supported,
103 * since it depends on your kernel version and underlying filesystem
104 * type but it is always safe to attempt.
106 vd
->vdev_has_trim
= B_TRUE
;
109 * Disable secure TRIM on file based vdevs. There is no way to
110 * request this behavior from the underlying filesystem.
112 vd
->vdev_has_securetrim
= B_FALSE
;
115 * We must have a pathname, and it must be absolute.
117 if (vd
->vdev_path
== NULL
|| vd
->vdev_path
[0] != '/') {
118 vd
->vdev_stat
.vs_aux
= VDEV_AUX_BAD_LABEL
;
119 return (SET_ERROR(EINVAL
));
123 * Reopen the device if it's not currently open. Otherwise,
124 * just update the physical size of the device.
126 if (vd
->vdev_tsd
!= NULL
) {
127 ASSERT(vd
->vdev_reopening
);
132 vf
= vd
->vdev_tsd
= kmem_zalloc(sizeof (vdev_file_t
), KM_SLEEP
);
135 * We always open the files from the root of the global zone, even if
136 * we're in a local zone. If the user has gotten to this point, the
137 * administrator has already decided that the pool should be available
138 * to local zone users, so the underlying devices should be as well.
140 ASSERT(vd
->vdev_path
!= NULL
&& vd
->vdev_path
[0] == '/');
142 error
= zfs_file_open(vd
->vdev_path
,
143 vdev_file_open_mode(spa_mode(vd
->vdev_spa
)), 0, &fp
);
145 vd
->vdev_stat
.vs_aux
= VDEV_AUX_OPEN_FAILED
;
153 * Make sure it's a regular file.
155 if (zfs_file_getattr(fp
, &zfa
)) {
156 return (SET_ERROR(ENODEV
));
158 if (!S_ISREG(zfa
.zfa_mode
)) {
159 vd
->vdev_stat
.vs_aux
= VDEV_AUX_OPEN_FAILED
;
160 return (SET_ERROR(ENODEV
));
166 error
= zfs_file_getattr(vf
->vf_file
, &zfa
);
168 vd
->vdev_stat
.vs_aux
= VDEV_AUX_OPEN_FAILED
;
172 *max_psize
= *psize
= zfa
.zfa_size
;
173 *logical_ashift
= vdev_file_logical_ashift
;
174 *physical_ashift
= vdev_file_physical_ashift
;
180 vdev_file_close(vdev_t
*vd
)
182 vdev_file_t
*vf
= vd
->vdev_tsd
;
184 if (vd
->vdev_reopening
|| vf
== NULL
)
187 if (vf
->vf_file
!= NULL
) {
188 zfs_file_close(vf
->vf_file
);
191 vd
->vdev_delayed_close
= B_FALSE
;
192 kmem_free(vf
, sizeof (vdev_file_t
));
197 * Implements the interrupt side for file vdev types. This routine will be
198 * called when the I/O completes allowing us to transfer the I/O to the
199 * interrupt taskqs. For consistency, the code structure mimics disk vdev
203 vdev_file_io_intr(zio_t
*zio
)
205 zio_delay_interrupt(zio
);
209 vdev_file_io_strategy(void *arg
)
212 vdev_t
*vd
= zio
->io_vd
;
220 off
= zio
->io_offset
;
226 ASSERT(zio
->io_type
== ZIO_TYPE_READ
|| zio
->io_type
== ZIO_TYPE_WRITE
);
227 if (zio
->io_type
== ZIO_TYPE_READ
) {
228 buf
= abd_borrow_buf(zio
->io_abd
, zio
->io_size
);
229 err
= zfs_file_pread(vf
->vf_file
, buf
, size
, off
, &resid
);
230 abd_return_buf_copy(zio
->io_abd
, buf
, size
);
232 buf
= abd_borrow_buf_copy(zio
->io_abd
, zio
->io_size
);
233 err
= zfs_file_pwrite(vf
->vf_file
, buf
, size
, off
, &resid
);
234 abd_return_buf(zio
->io_abd
, buf
, size
);
236 if (resid
!= 0 && zio
->io_error
== 0)
237 zio
->io_error
= ENOSPC
;
239 vdev_file_io_intr(zio
);
243 vdev_file_io_start(zio_t
*zio
)
245 vdev_t
*vd
= zio
->io_vd
;
246 vdev_file_t
*vf
= vd
->vdev_tsd
;
248 if (zio
->io_type
== ZIO_TYPE_IOCTL
) {
250 if (!vdev_readable(vd
)) {
251 zio
->io_error
= SET_ERROR(ENXIO
);
256 switch (zio
->io_cmd
) {
257 case DKIOCFLUSHWRITECACHE
:
258 zio
->io_error
= zfs_file_fsync(vf
->vf_file
,
262 zio
->io_error
= SET_ERROR(ENOTSUP
);
267 } else if (zio
->io_type
== ZIO_TYPE_TRIM
) {
271 ASSERT3U(zio
->io_size
, !=, 0);
273 /* XXX FreeBSD has no fallocate routine in file ops */
274 zio
->io_error
= zfs_file_fallocate(vf
->vf_file
,
275 mode
, zio
->io_offset
, zio
->io_size
);
277 zio
->io_error
= SET_ERROR(ENOTSUP
);
281 ASSERT(zio
->io_type
== ZIO_TYPE_READ
|| zio
->io_type
== ZIO_TYPE_WRITE
);
282 zio
->io_target_timestamp
= zio_handle_io_delay(zio
);
284 VERIFY3U(taskq_dispatch(vdev_file_taskq
, vdev_file_io_strategy
, zio
,
290 vdev_file_io_done(zio_t
*zio
)
294 vdev_ops_t vdev_file_ops
= {
295 .vdev_op_init
= NULL
,
296 .vdev_op_fini
= NULL
,
297 .vdev_op_open
= vdev_file_open
,
298 .vdev_op_close
= vdev_file_close
,
299 .vdev_op_asize
= vdev_default_asize
,
300 .vdev_op_min_asize
= vdev_default_min_asize
,
301 .vdev_op_min_alloc
= NULL
,
302 .vdev_op_io_start
= vdev_file_io_start
,
303 .vdev_op_io_done
= vdev_file_io_done
,
304 .vdev_op_state_change
= NULL
,
305 .vdev_op_need_resilver
= NULL
,
306 .vdev_op_hold
= vdev_file_hold
,
307 .vdev_op_rele
= vdev_file_rele
,
308 .vdev_op_remap
= NULL
,
309 .vdev_op_xlate
= vdev_default_xlate
,
310 .vdev_op_rebuild_asize
= NULL
,
311 .vdev_op_metaslab_init
= NULL
,
312 .vdev_op_config_generate
= NULL
,
313 .vdev_op_nparity
= NULL
,
314 .vdev_op_ndisks
= NULL
,
315 .vdev_op_type
= VDEV_TYPE_FILE
, /* name of this vdev type */
316 .vdev_op_leaf
= B_TRUE
/* leaf vdev */
320 * From userland we access disks just like files.
324 vdev_ops_t vdev_disk_ops
= {
325 .vdev_op_init
= NULL
,
326 .vdev_op_fini
= NULL
,
327 .vdev_op_open
= vdev_file_open
,
328 .vdev_op_close
= vdev_file_close
,
329 .vdev_op_asize
= vdev_default_asize
,
330 .vdev_op_min_asize
= vdev_default_min_asize
,
331 .vdev_op_min_alloc
= NULL
,
332 .vdev_op_io_start
= vdev_file_io_start
,
333 .vdev_op_io_done
= vdev_file_io_done
,
334 .vdev_op_state_change
= NULL
,
335 .vdev_op_need_resilver
= NULL
,
336 .vdev_op_hold
= vdev_file_hold
,
337 .vdev_op_rele
= vdev_file_rele
,
338 .vdev_op_remap
= NULL
,
339 .vdev_op_xlate
= vdev_default_xlate
,
340 .vdev_op_rebuild_asize
= NULL
,
341 .vdev_op_metaslab_init
= NULL
,
342 .vdev_op_config_generate
= NULL
,
343 .vdev_op_nparity
= NULL
,
344 .vdev_op_ndisks
= NULL
,
345 .vdev_op_type
= VDEV_TYPE_DISK
, /* name of this vdev type */
346 .vdev_op_leaf
= B_TRUE
/* leaf vdev */
351 ZFS_MODULE_PARAM(zfs_vdev_file
, vdev_file_
, logical_ashift
, ULONG
, ZMOD_RW
,
352 "Logical ashift for file-based devices");
353 ZFS_MODULE_PARAM(zfs_vdev_file
, vdev_file_
, physical_ashift
, ULONG
, ZMOD_RW
,
354 "Physical ashift for file-based devices");