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 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 65 vdev_file_fini(void) 66 { 67 taskq_destroy(vdev_file_taskq); 68 } 69 70 static void 71 vdev_file_hold(vdev_t *vd) 72 { 73 ASSERT3P(vd->vdev_path, !=, NULL); 74 } 75 76 static void 77 vdev_file_rele(vdev_t *vd) 78 { 79 ASSERT3P(vd->vdev_path, !=, NULL); 80 } 81 82 static mode_t 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 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 /* Is not backed by a block device. */ 113 vd->vdev_is_blkdev = B_FALSE; 114 115 /* 116 * Allow TRIM on file based vdevs. This may not always be supported, 117 * since it depends on your kernel version and underlying filesystem 118 * type but it is always safe to attempt. 119 */ 120 vd->vdev_has_trim = B_TRUE; 121 122 /* 123 * Disable secure TRIM on file based vdevs. There is no way to 124 * request this behavior from the underlying filesystem. 125 */ 126 vd->vdev_has_securetrim = B_FALSE; 127 128 /* 129 * We must have a pathname, and it must be absolute. 130 */ 131 if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') { 132 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL; 133 return (SET_ERROR(EINVAL)); 134 } 135 136 /* 137 * Reopen the device if it's not currently open. Otherwise, 138 * just update the physical size of the device. 139 */ 140 if (vd->vdev_tsd != NULL) { 141 ASSERT(vd->vdev_reopening); 142 vf = vd->vdev_tsd; 143 goto skip_open; 144 } 145 146 vf = vd->vdev_tsd = kmem_zalloc(sizeof (vdev_file_t), KM_SLEEP); 147 148 /* 149 * We always open the files from the root of the global zone, even if 150 * we're in a local zone. If the user has gotten to this point, the 151 * administrator has already decided that the pool should be available 152 * to local zone users, so the underlying devices should be as well. 153 */ 154 ASSERT3P(vd->vdev_path, !=, NULL); 155 ASSERT3S(vd->vdev_path[0], ==, '/'); 156 157 error = zfs_file_open(vd->vdev_path, 158 vdev_file_open_mode(spa_mode(vd->vdev_spa)), 0, &fp); 159 if (error) { 160 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED; 161 return (error); 162 } 163 164 vf->vf_file = fp; 165 166 #ifdef _KERNEL 167 /* 168 * Make sure it's a regular file. 169 */ 170 if (zfs_file_getattr(fp, &zfa)) { 171 return (SET_ERROR(ENODEV)); 172 } 173 if (!S_ISREG(zfa.zfa_mode)) { 174 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED; 175 return (SET_ERROR(ENODEV)); 176 } 177 #endif 178 179 skip_open: 180 181 error = zfs_file_getattr(vf->vf_file, &zfa); 182 if (error) { 183 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED; 184 return (error); 185 } 186 187 *max_psize = *psize = zfa.zfa_size; 188 *logical_ashift = vdev_file_logical_ashift; 189 *physical_ashift = vdev_file_physical_ashift; 190 191 return (0); 192 } 193 194 static void 195 vdev_file_close(vdev_t *vd) 196 { 197 vdev_file_t *vf = vd->vdev_tsd; 198 199 if (vd->vdev_reopening || vf == NULL) 200 return; 201 202 if (vf->vf_file != NULL) { 203 (void) zfs_file_close(vf->vf_file); 204 } 205 206 vd->vdev_delayed_close = B_FALSE; 207 kmem_free(vf, sizeof (vdev_file_t)); 208 vd->vdev_tsd = NULL; 209 } 210 211 static void 212 vdev_file_io_strategy(void *arg) 213 { 214 zio_t *zio = (zio_t *)arg; 215 vdev_t *vd = zio->io_vd; 216 vdev_file_t *vf = vd->vdev_tsd; 217 void *buf; 218 ssize_t resid; 219 loff_t off; 220 ssize_t size; 221 int err; 222 223 off = zio->io_offset; 224 size = zio->io_size; 225 resid = 0; 226 227 ASSERT(zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE); 228 if (zio->io_type == ZIO_TYPE_READ) { 229 buf = abd_borrow_buf(zio->io_abd, zio->io_size); 230 err = zfs_file_pread(vf->vf_file, buf, size, off, &resid); 231 abd_return_buf_copy(zio->io_abd, buf, size); 232 } else { 233 buf = abd_borrow_buf_copy(zio->io_abd, zio->io_size); 234 err = zfs_file_pwrite(vf->vf_file, buf, size, off, 235 vd->vdev_ashift, &resid); 236 abd_return_buf(zio->io_abd, buf, size); 237 } 238 zio->io_error = err; 239 if (resid != 0 && zio->io_error == 0) 240 zio->io_error = SET_ERROR(ENOSPC); 241 242 zio_delay_interrupt(zio); 243 } 244 245 static void 246 vdev_file_io_fsync(void *arg) 247 { 248 zio_t *zio = (zio_t *)arg; 249 vdev_file_t *vf = zio->io_vd->vdev_tsd; 250 251 zio->io_error = zfs_file_fsync(vf->vf_file, O_SYNC | O_DSYNC); 252 253 zio_interrupt(zio); 254 } 255 256 static void 257 vdev_file_io_deallocate(void *arg) 258 { 259 zio_t *zio = (zio_t *)arg; 260 vdev_file_t *vf = zio->io_vd->vdev_tsd; 261 262 zio->io_error = zfs_file_deallocate(vf->vf_file, 263 zio->io_offset, zio->io_size); 264 265 zio_interrupt(zio); 266 } 267 268 static void 269 vdev_file_io_start(zio_t *zio) 270 { 271 vdev_t *vd = zio->io_vd; 272 273 if (zio->io_type == ZIO_TYPE_FLUSH) { 274 /* XXPOLICY */ 275 if (!vdev_readable(vd)) { 276 zio->io_error = SET_ERROR(ENXIO); 277 zio_interrupt(zio); 278 return; 279 } 280 281 if (zfs_nocacheflush) { 282 zio_interrupt(zio); 283 return; 284 } 285 286 VERIFY3U(taskq_dispatch(vdev_file_taskq, 287 vdev_file_io_fsync, zio, TQ_SLEEP), !=, TASKQID_INVALID); 288 289 return; 290 } 291 292 if (zio->io_type == ZIO_TYPE_TRIM) { 293 ASSERT3U(zio->io_size, !=, 0); 294 295 VERIFY3U(taskq_dispatch(vdev_file_taskq, 296 vdev_file_io_deallocate, zio, TQ_SLEEP), !=, 297 TASKQID_INVALID); 298 299 return; 300 } 301 302 ASSERT(zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE); 303 zio->io_target_timestamp = zio_handle_io_delay(zio); 304 305 VERIFY3U(taskq_dispatch(vdev_file_taskq, vdev_file_io_strategy, zio, 306 TQ_SLEEP), !=, TASKQID_INVALID); 307 } 308 309 static void 310 vdev_file_io_done(zio_t *zio) 311 { 312 (void) zio; 313 } 314 315 vdev_ops_t vdev_file_ops = { 316 .vdev_op_init = NULL, 317 .vdev_op_fini = NULL, 318 .vdev_op_open = vdev_file_open, 319 .vdev_op_close = vdev_file_close, 320 .vdev_op_psize_to_asize = vdev_default_asize, 321 .vdev_op_asize_to_psize = vdev_default_psize, 322 .vdev_op_min_asize = vdev_default_min_asize, 323 .vdev_op_min_alloc = NULL, 324 .vdev_op_io_start = vdev_file_io_start, 325 .vdev_op_io_done = vdev_file_io_done, 326 .vdev_op_state_change = NULL, 327 .vdev_op_need_resilver = NULL, 328 .vdev_op_hold = vdev_file_hold, 329 .vdev_op_rele = vdev_file_rele, 330 .vdev_op_remap = NULL, 331 .vdev_op_xlate = vdev_default_xlate, 332 .vdev_op_rebuild_asize = NULL, 333 .vdev_op_metaslab_init = NULL, 334 .vdev_op_config_generate = NULL, 335 .vdev_op_nparity = NULL, 336 .vdev_op_ndisks = NULL, 337 .vdev_op_type = VDEV_TYPE_FILE, /* name of this vdev type */ 338 .vdev_op_leaf = B_TRUE /* leaf vdev */ 339 }; 340 341 /* 342 * From userland we access disks just like files. 343 */ 344 #ifndef _KERNEL 345 346 vdev_ops_t vdev_disk_ops = { 347 .vdev_op_init = NULL, 348 .vdev_op_fini = NULL, 349 .vdev_op_open = vdev_file_open, 350 .vdev_op_close = vdev_file_close, 351 .vdev_op_psize_to_asize = vdev_default_asize, 352 .vdev_op_min_asize = vdev_default_min_asize, 353 .vdev_op_min_alloc = NULL, 354 .vdev_op_io_start = vdev_file_io_start, 355 .vdev_op_io_done = vdev_file_io_done, 356 .vdev_op_state_change = NULL, 357 .vdev_op_need_resilver = NULL, 358 .vdev_op_hold = vdev_file_hold, 359 .vdev_op_rele = vdev_file_rele, 360 .vdev_op_remap = NULL, 361 .vdev_op_xlate = vdev_default_xlate, 362 .vdev_op_rebuild_asize = NULL, 363 .vdev_op_metaslab_init = NULL, 364 .vdev_op_config_generate = NULL, 365 .vdev_op_nparity = NULL, 366 .vdev_op_ndisks = NULL, 367 .vdev_op_type = VDEV_TYPE_DISK, /* name of this vdev type */ 368 .vdev_op_leaf = B_TRUE /* leaf vdev */ 369 }; 370 371 #endif 372 373 ZFS_MODULE_PARAM(zfs_vdev_file, vdev_file_, logical_ashift, UINT, ZMOD_RW, 374 "Logical ashift for file-based devices"); 375 ZFS_MODULE_PARAM(zfs_vdev_file, vdev_file_, physical_ashift, UINT, ZMOD_RW, 376 "Physical ashift for file-based devices"); 377