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