1 /*
2 * CDDL HEADER START
3 *
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.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
24 */
25
26 #include <sys/zfs_context.h>
27 #include <sys/spa.h>
28 #include <sys/spa_impl.h>
29 #include <sys/vdev_file.h>
30 #include <sys/vdev_impl.h>
31 #include <sys/vdev_trim.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/vnode.h>
37 #include <sys/zfs_file.h>
38 #ifdef _KERNEL
39 #include <linux/falloc.h>
40 #include <sys/fcntl.h>
41 #else
42 #include <fcntl.h>
43 #endif
44 /*
45 * Virtual device vector for files.
46 */
47
48 static taskq_t *vdev_file_taskq;
49
50 /*
51 * By default, the logical/physical ashift for file vdevs is set to
52 * SPA_MINBLOCKSHIFT (9). This allows all file vdevs to use 512B (1 << 9)
53 * blocksizes. Users may opt to change one or both of these for testing
54 * or performance reasons. Care should be taken as these values will
55 * impact the vdev_ashift setting which can only be set at vdev creation
56 * time.
57 */
58 static uint_t vdev_file_logical_ashift = SPA_MINBLOCKSHIFT;
59 static uint_t vdev_file_physical_ashift = SPA_MINBLOCKSHIFT;
60
61 static void
vdev_file_hold(vdev_t * vd)62 vdev_file_hold(vdev_t *vd)
63 {
64 ASSERT(vd->vdev_path != NULL);
65 }
66
67 static void
vdev_file_rele(vdev_t * vd)68 vdev_file_rele(vdev_t *vd)
69 {
70 ASSERT(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)90 vdev_file_open(vdev_t *vd, uint64_t *psize, uint64_t *max_psize,
91 uint64_t *logical_ashift, uint64_t *physical_ashift)
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 /*
104 * Allow TRIM on file based vdevs. This may not always be supported,
105 * since it depends on your kernel version and underlying filesystem
106 * type but it is always safe to attempt.
107 */
108 vd->vdev_has_trim = B_TRUE;
109
110 /*
111 * Disable secure TRIM on file based vdevs. There is no way to
112 * request this behavior from the underlying filesystem.
113 */
114 vd->vdev_has_securetrim = B_FALSE;
115
116 /*
117 * We must have a pathname, and it must be absolute.
118 */
119 if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
120 vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
121 return (SET_ERROR(EINVAL));
122 }
123
124 /*
125 * Reopen the device if it's not currently open. Otherwise,
126 * just update the physical size of the device.
127 */
128 if (vd->vdev_tsd != NULL) {
129 ASSERT(vd->vdev_reopening);
130 vf = vd->vdev_tsd;
131 goto skip_open;
132 }
133
134 vf = vd->vdev_tsd = kmem_zalloc(sizeof (vdev_file_t), KM_SLEEP);
135
136 /*
137 * We always open the files from the root of the global zone, even if
138 * we're in a local zone. If the user has gotten to this point, the
139 * administrator has already decided that the pool should be available
140 * to local zone users, so the underlying devices should be as well.
141 */
142 ASSERT(vd->vdev_path != NULL && vd->vdev_path[0] == '/');
143
144 error = zfs_file_open(vd->vdev_path,
145 vdev_file_open_mode(spa_mode(vd->vdev_spa)), 0, &fp);
146 if (error) {
147 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
148 return (error);
149 }
150
151 vf->vf_file = fp;
152
153 #ifdef _KERNEL
154 /*
155 * Make sure it's a regular file.
156 */
157 if (zfs_file_getattr(fp, &zfa)) {
158 return (SET_ERROR(ENODEV));
159 }
160 if (!S_ISREG(zfa.zfa_mode)) {
161 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
162 return (SET_ERROR(ENODEV));
163 }
164 #endif
165
166 skip_open:
167
168 error = zfs_file_getattr(vf->vf_file, &zfa);
169 if (error) {
170 vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
171 return (error);
172 }
173
174 *max_psize = *psize = zfa.zfa_size;
175 *logical_ashift = vdev_file_logical_ashift;
176 *physical_ashift = vdev_file_physical_ashift;
177
178 return (0);
179 }
180
181 static void
vdev_file_close(vdev_t * vd)182 vdev_file_close(vdev_t *vd)
183 {
184 vdev_file_t *vf = vd->vdev_tsd;
185
186 if (vd->vdev_reopening || vf == NULL)
187 return;
188
189 if (vf->vf_file != NULL) {
190 (void) zfs_file_close(vf->vf_file);
191 }
192
193 vd->vdev_delayed_close = B_FALSE;
194 kmem_free(vf, sizeof (vdev_file_t));
195 vd->vdev_tsd = NULL;
196 }
197
198 static void
vdev_file_io_strategy(void * arg)199 vdev_file_io_strategy(void *arg)
200 {
201 zio_t *zio = (zio_t *)arg;
202 vdev_t *vd = zio->io_vd;
203 vdev_file_t *vf = vd->vdev_tsd;
204 ssize_t resid;
205 void *buf;
206 loff_t off;
207 ssize_t size;
208 int err;
209
210 off = zio->io_offset;
211 size = zio->io_size;
212 resid = 0;
213
214 if (zio->io_type == ZIO_TYPE_READ) {
215 buf = abd_borrow_buf(zio->io_abd, zio->io_size);
216 err = zfs_file_pread(vf->vf_file, buf, size, off, &resid);
217 abd_return_buf_copy(zio->io_abd, buf, size);
218 } else {
219 buf = abd_borrow_buf_copy(zio->io_abd, zio->io_size);
220 err = zfs_file_pwrite(vf->vf_file, buf, size, off, &resid);
221 abd_return_buf(zio->io_abd, buf, size);
222 }
223 zio->io_error = err;
224 if (resid != 0 && zio->io_error == 0)
225 zio->io_error = SET_ERROR(ENOSPC);
226
227 zio_delay_interrupt(zio);
228 }
229
230 static void
vdev_file_io_fsync(void * arg)231 vdev_file_io_fsync(void *arg)
232 {
233 zio_t *zio = (zio_t *)arg;
234 vdev_file_t *vf = zio->io_vd->vdev_tsd;
235
236 zio->io_error = zfs_file_fsync(vf->vf_file, O_SYNC | O_DSYNC);
237
238 zio_interrupt(zio);
239 }
240
241 static void
vdev_file_io_start(zio_t * zio)242 vdev_file_io_start(zio_t *zio)
243 {
244 vdev_t *vd = zio->io_vd;
245 vdev_file_t *vf = vd->vdev_tsd;
246
247 if (zio->io_type == ZIO_TYPE_FLUSH) {
248 /* XXPOLICY */
249 if (!vdev_readable(vd)) {
250 zio->io_error = SET_ERROR(ENXIO);
251 zio_interrupt(zio);
252 return;
253 }
254
255 if (zfs_nocacheflush) {
256 zio_execute(zio);
257 return;
258 }
259
260 /*
261 * We cannot safely call vfs_fsync() when PF_FSTRANS
262 * is set in the current context. Filesystems like
263 * XFS include sanity checks to verify it is not
264 * already set, see xfs_vm_writepage(). Therefore
265 * the sync must be dispatched to a different context.
266 */
267 if (__spl_pf_fstrans_check()) {
268 VERIFY3U(taskq_dispatch(vdev_file_taskq,
269 vdev_file_io_fsync, zio, TQ_SLEEP), !=,
270 TASKQID_INVALID);
271 return;
272 }
273
274 zio->io_error = zfs_file_fsync(vf->vf_file, O_SYNC | O_DSYNC);
275
276 zio_execute(zio);
277 return;
278 } else if (zio->io_type == ZIO_TYPE_TRIM) {
279 ASSERT3U(zio->io_size, !=, 0);
280 zio->io_error = zfs_file_deallocate(vf->vf_file,
281 zio->io_offset, zio->io_size);
282 zio_execute(zio);
283 return;
284 }
285
286 zio->io_target_timestamp = zio_handle_io_delay(zio);
287
288 VERIFY3U(taskq_dispatch(vdev_file_taskq, vdev_file_io_strategy, zio,
289 TQ_SLEEP), !=, TASKQID_INVALID);
290 }
291
292 static void
vdev_file_io_done(zio_t * zio)293 vdev_file_io_done(zio_t *zio)
294 {
295 (void) zio;
296 }
297
298 vdev_ops_t vdev_file_ops = {
299 .vdev_op_init = NULL,
300 .vdev_op_fini = NULL,
301 .vdev_op_open = vdev_file_open,
302 .vdev_op_close = vdev_file_close,
303 .vdev_op_asize = vdev_default_asize,
304 .vdev_op_min_asize = vdev_default_min_asize,
305 .vdev_op_min_alloc = NULL,
306 .vdev_op_io_start = vdev_file_io_start,
307 .vdev_op_io_done = vdev_file_io_done,
308 .vdev_op_state_change = NULL,
309 .vdev_op_need_resilver = NULL,
310 .vdev_op_hold = vdev_file_hold,
311 .vdev_op_rele = vdev_file_rele,
312 .vdev_op_remap = NULL,
313 .vdev_op_xlate = vdev_default_xlate,
314 .vdev_op_rebuild_asize = NULL,
315 .vdev_op_metaslab_init = NULL,
316 .vdev_op_config_generate = NULL,
317 .vdev_op_nparity = NULL,
318 .vdev_op_ndisks = NULL,
319 .vdev_op_type = VDEV_TYPE_FILE, /* name of this vdev type */
320 .vdev_op_leaf = B_TRUE /* leaf vdev */
321 };
322
323 void
vdev_file_init(void)324 vdev_file_init(void)
325 {
326 vdev_file_taskq = taskq_create("z_vdev_file", MAX(boot_ncpus, 16),
327 minclsyspri, boot_ncpus, INT_MAX, TASKQ_DYNAMIC);
328
329 VERIFY(vdev_file_taskq);
330 }
331
332 void
vdev_file_fini(void)333 vdev_file_fini(void)
334 {
335 taskq_destroy(vdev_file_taskq);
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_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