xref: /freebsd/sys/contrib/openzfs/module/zfs/vdev_file.c (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
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, &resid);
232 		abd_return_buf(zio->io_abd, buf, size);
233 	}
234 	zio->io_error = err;
235 	if (resid != 0 && zio->io_error == 0)
236 		zio->io_error = SET_ERROR(ENOSPC);
237 
238 	zio_delay_interrupt(zio);
239 }
240 
241 static void
vdev_file_io_fsync(void * arg)242 vdev_file_io_fsync(void *arg)
243 {
244 	zio_t *zio = (zio_t *)arg;
245 	vdev_file_t *vf = zio->io_vd->vdev_tsd;
246 
247 	zio->io_error = zfs_file_fsync(vf->vf_file, O_SYNC | O_DSYNC);
248 
249 	zio_interrupt(zio);
250 }
251 
252 static void
vdev_file_io_deallocate(void * arg)253 vdev_file_io_deallocate(void *arg)
254 {
255 	zio_t *zio = (zio_t *)arg;
256 	vdev_file_t *vf = zio->io_vd->vdev_tsd;
257 
258 	zio->io_error = zfs_file_deallocate(vf->vf_file,
259 	    zio->io_offset, zio->io_size);
260 
261 	zio_interrupt(zio);
262 }
263 
264 static void
vdev_file_io_start(zio_t * zio)265 vdev_file_io_start(zio_t *zio)
266 {
267 	vdev_t *vd = zio->io_vd;
268 
269 	if (zio->io_type == ZIO_TYPE_FLUSH) {
270 		/* XXPOLICY */
271 		if (!vdev_readable(vd)) {
272 			zio->io_error = SET_ERROR(ENXIO);
273 			zio_interrupt(zio);
274 			return;
275 		}
276 
277 		if (zfs_nocacheflush) {
278 			zio_interrupt(zio);
279 			return;
280 		}
281 
282 		VERIFY3U(taskq_dispatch(vdev_file_taskq,
283 		    vdev_file_io_fsync, zio, TQ_SLEEP), !=, TASKQID_INVALID);
284 
285 		return;
286 	}
287 
288 	if (zio->io_type == ZIO_TYPE_TRIM) {
289 		ASSERT3U(zio->io_size, !=, 0);
290 
291 		VERIFY3U(taskq_dispatch(vdev_file_taskq,
292 		    vdev_file_io_deallocate, zio, TQ_SLEEP), !=,
293 		    TASKQID_INVALID);
294 
295 		return;
296 	}
297 
298 	ASSERT(zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE);
299 	zio->io_target_timestamp = zio_handle_io_delay(zio);
300 
301 	VERIFY3U(taskq_dispatch(vdev_file_taskq, vdev_file_io_strategy, zio,
302 	    TQ_SLEEP), !=, TASKQID_INVALID);
303 }
304 
305 static void
vdev_file_io_done(zio_t * zio)306 vdev_file_io_done(zio_t *zio)
307 {
308 	(void) zio;
309 }
310 
311 vdev_ops_t vdev_file_ops = {
312 	.vdev_op_init = NULL,
313 	.vdev_op_fini = NULL,
314 	.vdev_op_open = vdev_file_open,
315 	.vdev_op_close = vdev_file_close,
316 	.vdev_op_asize = vdev_default_asize,
317 	.vdev_op_min_asize = vdev_default_min_asize,
318 	.vdev_op_min_alloc = NULL,
319 	.vdev_op_io_start = vdev_file_io_start,
320 	.vdev_op_io_done = vdev_file_io_done,
321 	.vdev_op_state_change = NULL,
322 	.vdev_op_need_resilver = NULL,
323 	.vdev_op_hold = vdev_file_hold,
324 	.vdev_op_rele = vdev_file_rele,
325 	.vdev_op_remap = NULL,
326 	.vdev_op_xlate = vdev_default_xlate,
327 	.vdev_op_rebuild_asize = NULL,
328 	.vdev_op_metaslab_init = NULL,
329 	.vdev_op_config_generate = NULL,
330 	.vdev_op_nparity = NULL,
331 	.vdev_op_ndisks = NULL,
332 	.vdev_op_type = VDEV_TYPE_FILE,		/* name of this vdev type */
333 	.vdev_op_leaf = B_TRUE			/* leaf vdev */
334 };
335 
336 /*
337  * From userland we access disks just like files.
338  */
339 #ifndef _KERNEL
340 
341 vdev_ops_t vdev_disk_ops = {
342 	.vdev_op_init = NULL,
343 	.vdev_op_fini = NULL,
344 	.vdev_op_open = vdev_file_open,
345 	.vdev_op_close = vdev_file_close,
346 	.vdev_op_asize = vdev_default_asize,
347 	.vdev_op_min_asize = vdev_default_min_asize,
348 	.vdev_op_min_alloc = NULL,
349 	.vdev_op_io_start = vdev_file_io_start,
350 	.vdev_op_io_done = vdev_file_io_done,
351 	.vdev_op_state_change = NULL,
352 	.vdev_op_need_resilver = NULL,
353 	.vdev_op_hold = vdev_file_hold,
354 	.vdev_op_rele = vdev_file_rele,
355 	.vdev_op_remap = NULL,
356 	.vdev_op_xlate = vdev_default_xlate,
357 	.vdev_op_rebuild_asize = NULL,
358 	.vdev_op_metaslab_init = NULL,
359 	.vdev_op_config_generate = NULL,
360 	.vdev_op_nparity = NULL,
361 	.vdev_op_ndisks = NULL,
362 	.vdev_op_type = VDEV_TYPE_DISK,		/* name of this vdev type */
363 	.vdev_op_leaf = B_TRUE			/* leaf vdev */
364 };
365 
366 #endif
367 
368 ZFS_MODULE_PARAM(zfs_vdev_file, vdev_file_, logical_ashift, UINT, ZMOD_RW,
369 	"Logical ashift for file-based devices");
370 ZFS_MODULE_PARAM(zfs_vdev_file, vdev_file_, physical_ashift, UINT, ZMOD_RW,
371 	"Physical ashift for file-based devices");
372