1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * This file and its contents are supplied under the terms of the
4 * Common Development and Distribution License ("CDDL"), version 1.0.
5 * You may only use this file in accordance with the terms of version
6 * 1.0 of the CDDL.
7 *
8 * A full copy of the text of the CDDL should have accompanied this
9 * source. A copy of the CDDL is also available via the Internet at
10 * https://opensource.org/license/CDDL-1.0.
11 */
12 /*
13 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
14 * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
15 * Copyright (c) 2025, Klara, Inc.
16 * Copyright (c) 2026, TrueNAS.
17 */
18
19 #include <sys/zfs_context.h>
20 #include <sys/spa.h>
21 #include <sys/vdev_file.h>
22 #include <sys/vdev_impl.h>
23 #include <sys/zio.h>
24 #include <sys/fs/zfs.h>
25 #include <sys/fm/fs/zfs.h>
26 #include <sys/abd.h>
27 #include <sys/stat.h>
28
29 /*
30 * Virtual device vector for files.
31 */
32
33 static taskq_t *vdev_file_taskq;
34
35 /*
36 * By default, the logical/physical ashift for file vdevs is set to
37 * SPA_MINBLOCKSHIFT (9). This allows all file vdevs to use 512B (1 << 9)
38 * blocksizes. Users may opt to change one or both of these for testing
39 * or performance reasons. Care should be taken as these values will
40 * impact the vdev_ashift setting which can only be set at vdev creation
41 * time.
42 */
43 static uint_t vdev_file_logical_ashift = SPA_MINBLOCKSHIFT;
44 static uint_t vdev_file_physical_ashift = SPA_MINBLOCKSHIFT;
45
46 void
vdev_file_init(void)47 vdev_file_init(void)
48 {
49 vdev_file_taskq = taskq_create("z_vdev_file", MAX(boot_ncpus, 16),
50 minclsyspri, boot_ncpus, INT_MAX, TASKQ_DYNAMIC);
51
52 VERIFY(vdev_file_taskq);
53 }
54
55 void
vdev_file_fini(void)56 vdev_file_fini(void)
57 {
58 taskq_destroy(vdev_file_taskq);
59 }
60
61 static void
vdev_file_hold(vdev_t * vd)62 vdev_file_hold(vdev_t *vd)
63 {
64 ASSERT3P(vd->vdev_path, !=, NULL);
65 }
66
67 static void
vdev_file_rele(vdev_t * vd)68 vdev_file_rele(vdev_t *vd)
69 {
70 ASSERT3P(vd->vdev_path, !=, NULL);
71 }
72
73 static mode_t
vdev_file_open_mode(spa_mode_t spa_mode)74 vdev_file_open_mode(spa_mode_t spa_mode)
75 {
76 mode_t mode = 0;
77
78 if ((spa_mode & SPA_MODE_READ) && (spa_mode & SPA_MODE_WRITE)) {
79 mode = O_RDWR;
80 } else if (spa_mode & SPA_MODE_READ) {
81 mode = O_RDONLY;
82 } else if (spa_mode & SPA_MODE_WRITE) {
83 mode = O_WRONLY;
84 }
85
86 return (mode | O_LARGEFILE);
87 }
88
89 static int
vdev_file_open(vdev_t * vd,uint64_t * psize,uint64_t * max_psize,uint64_t * logical_ashift,uint64_t * physical_ashift,cred_t * cr)90 vdev_file_open(vdev_t *vd, uint64_t *psize, uint64_t *max_psize,
91 uint64_t *logical_ashift, uint64_t *physical_ashift, cred_t *cr)
92 {
93 vdev_file_t *vf;
94 zfs_file_t *fp;
95 zfs_file_attr_t zfa;
96 int error;
97
98 /*
99 * Rotational optimizations only make sense on block devices.
100 */
101 vd->vdev_nonrot = B_TRUE;
102
103 /* Is not backed by a block device. */
104 vd->vdev_is_blkdev = B_FALSE;
105
106 /*
107 * Allow TRIM on file based vdevs. This may not always be supported,
108 * since it depends on your kernel version and underlying filesystem
109 * type but it is always safe to attempt.
110 */
111 vd->vdev_has_trim = B_TRUE;
112
113 /*
114 * Disable secure TRIM on file based vdevs. There is no way to
115 * request this behavior from the underlying filesystem.
116 */
117 vd->vdev_has_securetrim = B_FALSE;
118
119 /*
120 * We must have a pathname, and it must be absolute.
121 */
122 if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
123 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
124 return (SET_ERROR(EINVAL));
125 }
126
127 /*
128 * Reopen the device if it's not currently open. Otherwise,
129 * just update the physical size of the device.
130 */
131 if (vd->vdev_tsd != NULL) {
132 ASSERT(vd->vdev_reopening);
133 vf = vd->vdev_tsd;
134 goto skip_open;
135 }
136
137 vf = vd->vdev_tsd = kmem_zalloc(sizeof (vdev_file_t), KM_SLEEP);
138
139 /*
140 * We always open the files from the root of the global zone, even if
141 * we're in a local zone. If the user has gotten to this point, the
142 * administrator has already decided that the pool should be available
143 * to local zone users, so the underlying devices should be as well.
144 */
145 ASSERT3P(vd->vdev_path, !=, NULL);
146 ASSERT3S(vd->vdev_path[0], ==, '/');
147
148 error = zfs_file_open(vd->vdev_path,
149 vdev_file_open_mode(spa_mode(vd->vdev_spa)), 0, cr, &fp);
150
151 if (error) {
152 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
153 return (error);
154 }
155
156 vf->vf_file = fp;
157
158 #ifdef _KERNEL
159 /*
160 * Make sure it's a regular file.
161 */
162 if (zfs_file_getattr(fp, &zfa)) {
163 return (SET_ERROR(ENODEV));
164 }
165 if (!S_ISREG(zfa.zfa_mode)) {
166 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
167 return (SET_ERROR(ENODEV));
168 }
169 #endif
170
171 skip_open:
172
173 error = zfs_file_getattr(vf->vf_file, &zfa);
174 if (error) {
175 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
176 return (error);
177 }
178
179 *max_psize = *psize = zfa.zfa_size;
180 *logical_ashift = vdev_file_logical_ashift;
181 *physical_ashift = vdev_file_physical_ashift;
182
183 return (0);
184 }
185
186 static void
vdev_file_close(vdev_t * vd)187 vdev_file_close(vdev_t *vd)
188 {
189 vdev_file_t *vf = vd->vdev_tsd;
190
191 if (vd->vdev_reopening || vf == NULL)
192 return;
193
194 if (vf->vf_file != NULL) {
195 (void) zfs_file_close(vf->vf_file);
196 }
197
198 vd->vdev_delayed_close = B_FALSE;
199 kmem_free(vf, sizeof (vdev_file_t));
200 vd->vdev_tsd = NULL;
201 }
202
203 static void
vdev_file_io_strategy(void * arg)204 vdev_file_io_strategy(void *arg)
205 {
206 zio_t *zio = (zio_t *)arg;
207 vdev_t *vd = zio->io_vd;
208 vdev_file_t *vf = vd->vdev_tsd;
209 void *buf;
210 ssize_t resid;
211 loff_t off;
212 ssize_t size;
213 int err;
214
215 off = zio->io_offset;
216 size = zio->io_size;
217 resid = 0;
218
219 ASSERT(zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE);
220 if (zio->io_type == ZIO_TYPE_READ) {
221 buf = abd_borrow_buf(zio->io_abd, zio->io_size);
222 err = zfs_file_pread(vf->vf_file, buf, size, off, &resid);
223 abd_return_buf_copy(zio->io_abd, buf, size);
224 } else {
225 buf = abd_borrow_buf_copy(zio->io_abd, zio->io_size);
226 err = zfs_file_pwrite(vf->vf_file, buf, size, off,
227 vd->vdev_ashift, &resid);
228 abd_return_buf(zio->io_abd, buf, size);
229 }
230 zio->io_error = err;
231 if (resid != 0 && zio->io_error == 0)
232 zio->io_error = SET_ERROR(ENOSPC);
233
234 zio_delay_interrupt(zio);
235 }
236
237 static void
vdev_file_io_fsync(void * arg)238 vdev_file_io_fsync(void *arg)
239 {
240 zio_t *zio = (zio_t *)arg;
241 vdev_file_t *vf = zio->io_vd->vdev_tsd;
242
243 zio->io_error = zfs_file_fsync(vf->vf_file, O_SYNC | O_DSYNC);
244
245 zio_interrupt(zio);
246 }
247
248 static void
vdev_file_io_deallocate(void * arg)249 vdev_file_io_deallocate(void *arg)
250 {
251 zio_t *zio = (zio_t *)arg;
252 vdev_file_t *vf = zio->io_vd->vdev_tsd;
253
254 zio->io_error = zfs_file_deallocate(vf->vf_file,
255 zio->io_offset, zio->io_size);
256
257 zio_interrupt(zio);
258 }
259
260 static void
vdev_file_io_start(zio_t * zio)261 vdev_file_io_start(zio_t *zio)
262 {
263 vdev_t *vd = zio->io_vd;
264
265 if (zio->io_type == ZIO_TYPE_FLUSH) {
266 /* XXPOLICY */
267 if (!vdev_readable(vd)) {
268 zio->io_error = SET_ERROR(ENXIO);
269 zio_interrupt(zio);
270 return;
271 }
272
273 if (zfs_nocacheflush) {
274 zio_interrupt(zio);
275 return;
276 }
277
278 VERIFY3U(taskq_dispatch(vdev_file_taskq,
279 vdev_file_io_fsync, zio, TQ_SLEEP), !=, TASKQID_INVALID);
280
281 return;
282 }
283
284 if (zio->io_type == ZIO_TYPE_TRIM) {
285 ASSERT3U(zio->io_size, !=, 0);
286
287 VERIFY3U(taskq_dispatch(vdev_file_taskq,
288 vdev_file_io_deallocate, zio, TQ_SLEEP), !=,
289 TASKQID_INVALID);
290
291 return;
292 }
293
294 ASSERT(zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE);
295 zio->io_target_timestamp = zio_handle_io_delay(zio);
296
297 VERIFY3U(taskq_dispatch(vdev_file_taskq, vdev_file_io_strategy, zio,
298 TQ_SLEEP), !=, TASKQID_INVALID);
299 }
300
301 static void
vdev_file_io_done(zio_t * zio)302 vdev_file_io_done(zio_t *zio)
303 {
304 (void) zio;
305 }
306
307 vdev_ops_t vdev_file_ops = {
308 .vdev_op_init = NULL,
309 .vdev_op_fini = NULL,
310 .vdev_op_open = vdev_file_open,
311 .vdev_op_close = vdev_file_close,
312 .vdev_op_psize_to_asize = vdev_default_asize,
313 .vdev_op_asize_to_psize = vdev_default_psize,
314 .vdev_op_min_asize = vdev_default_min_asize,
315 .vdev_op_min_alloc = NULL,
316 .vdev_op_io_start = vdev_file_io_start,
317 .vdev_op_io_done = vdev_file_io_done,
318 .vdev_op_state_change = NULL,
319 .vdev_op_need_resilver = NULL,
320 .vdev_op_hold = vdev_file_hold,
321 .vdev_op_rele = vdev_file_rele,
322 .vdev_op_remap = NULL,
323 .vdev_op_xlate = vdev_default_xlate,
324 .vdev_op_rebuild_asize = NULL,
325 .vdev_op_metaslab_init = NULL,
326 .vdev_op_config_generate = NULL,
327 .vdev_op_nparity = NULL,
328 .vdev_op_ndisks = NULL,
329 .vdev_op_type = VDEV_TYPE_FILE, /* name of this vdev type */
330 .vdev_op_leaf = B_TRUE /* leaf vdev */
331 };
332
333 /*
334 * From userland we access disks just like files.
335 */
336 #ifndef _KERNEL
337
338 vdev_ops_t vdev_disk_ops = {
339 .vdev_op_init = NULL,
340 .vdev_op_fini = NULL,
341 .vdev_op_open = vdev_file_open,
342 .vdev_op_close = vdev_file_close,
343 .vdev_op_psize_to_asize = vdev_default_asize,
344 .vdev_op_min_asize = vdev_default_min_asize,
345 .vdev_op_min_alloc = NULL,
346 .vdev_op_io_start = vdev_file_io_start,
347 .vdev_op_io_done = vdev_file_io_done,
348 .vdev_op_state_change = NULL,
349 .vdev_op_need_resilver = NULL,
350 .vdev_op_hold = vdev_file_hold,
351 .vdev_op_rele = vdev_file_rele,
352 .vdev_op_remap = NULL,
353 .vdev_op_xlate = vdev_default_xlate,
354 .vdev_op_rebuild_asize = NULL,
355 .vdev_op_metaslab_init = NULL,
356 .vdev_op_config_generate = NULL,
357 .vdev_op_nparity = NULL,
358 .vdev_op_ndisks = NULL,
359 .vdev_op_type = VDEV_TYPE_DISK, /* name of this vdev type */
360 .vdev_op_leaf = B_TRUE /* leaf vdev */
361 };
362
363 #endif
364
365 ZFS_MODULE_PARAM(zfs_vdev_file, vdev_file_, logical_ashift, UINT, ZMOD_RW,
366 "Logical ashift for file-based devices");
367 ZFS_MODULE_PARAM(zfs_vdev_file, vdev_file_, physical_ashift, UINT, ZMOD_RW,
368 "Physical ashift for file-based devices");
369