xref: /illumos-gate/usr/src/uts/common/fs/zfs/vdev_file.c (revision ac04831d)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
2298d1cbfeSGeorge Wilson  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23094e47e9SGeorge Wilson  * Copyright (c) 2011, 2016 by Delphix. All rights reserved.
24084fd14fSBrian Behlendorf  * Copyright 2019 Joyent, Inc.
25fa9e4066Sahrens  */
26fa9e4066Sahrens 
27fa9e4066Sahrens #include <sys/zfs_context.h>
28fa9e4066Sahrens #include <sys/spa.h>
2931d7e8faSGeorge Wilson #include <sys/spa_impl.h>
30fa9e4066Sahrens #include <sys/vdev_file.h>
31fa9e4066Sahrens #include <sys/vdev_impl.h>
32084fd14fSBrian Behlendorf #include <sys/vdev_trim.h>
33fa9e4066Sahrens #include <sys/zio.h>
34fa9e4066Sahrens #include <sys/fs/zfs.h>
3551ece835Seschrock #include <sys/fm/fs/zfs.h>
36770499e1SDan Kimmel #include <sys/abd.h>
37084fd14fSBrian Behlendorf #include <sys/fcntl.h>
38084fd14fSBrian Behlendorf #include <sys/vnode.h>
39fa9e4066Sahrens 
40fa9e4066Sahrens /*
41fa9e4066Sahrens  * Virtual device vector for files.
42fa9e4066Sahrens  */
43fa9e4066Sahrens 
44dcba9f3fSGeorge Wilson static void
vdev_file_hold(vdev_t * vd)45dcba9f3fSGeorge Wilson vdev_file_hold(vdev_t *vd)
46dcba9f3fSGeorge Wilson {
47dcba9f3fSGeorge Wilson 	ASSERT(vd->vdev_path != NULL);
48dcba9f3fSGeorge Wilson }
49dcba9f3fSGeorge Wilson 
50dcba9f3fSGeorge Wilson static void
vdev_file_rele(vdev_t * vd)51dcba9f3fSGeorge Wilson vdev_file_rele(vdev_t *vd)
52dcba9f3fSGeorge Wilson {
53dcba9f3fSGeorge Wilson 	ASSERT(vd->vdev_path != NULL);
54dcba9f3fSGeorge Wilson }
55dcba9f3fSGeorge Wilson 
56fa9e4066Sahrens static int
vdev_file_open(vdev_t * vd,uint64_t * psize,uint64_t * max_psize,uint64_t * ashift)574263d13fSGeorge Wilson vdev_file_open(vdev_t *vd, uint64_t *psize, uint64_t *max_psize,
584263d13fSGeorge Wilson     uint64_t *ashift)
59fa9e4066Sahrens {
60fa9e4066Sahrens 	vdev_file_t *vf;
61fa9e4066Sahrens 	vnode_t *vp;
62e14bb325SJeff Bonwick 	vattr_t vattr;
63fa9e4066Sahrens 	int error;
64fa9e4066Sahrens 
65084fd14fSBrian Behlendorf 	/*
66084fd14fSBrian Behlendorf 	 * Rotational optimizations only make sense on block devices.
67084fd14fSBrian Behlendorf 	 */
6812a8814cSTom Caputi 	vd->vdev_nonrot = B_TRUE;
6912a8814cSTom Caputi 
70084fd14fSBrian Behlendorf 	/*
71084fd14fSBrian Behlendorf 	 * Allow TRIM on file based vdevs.  This may not always be supported,
72084fd14fSBrian Behlendorf 	 * since it depends on your kernel version and underlying filesystem
73084fd14fSBrian Behlendorf 	 * type but it is always safe to attempt.
74084fd14fSBrian Behlendorf 	 */
75084fd14fSBrian Behlendorf 	vd->vdev_has_trim = B_TRUE;
76084fd14fSBrian Behlendorf 
77084fd14fSBrian Behlendorf 	/*
78084fd14fSBrian Behlendorf 	 * Disable secure TRIM on file based vdevs.  There is no way to
79084fd14fSBrian Behlendorf 	 * request this behavior from the underlying filesystem.
80084fd14fSBrian Behlendorf 	 */
81084fd14fSBrian Behlendorf 	vd->vdev_has_securetrim = B_FALSE;
82084fd14fSBrian Behlendorf 
83fa9e4066Sahrens 	/*
84fa9e4066Sahrens 	 * We must have a pathname, and it must be absolute.
85fa9e4066Sahrens 	 */
86fa9e4066Sahrens 	if (vd->vdev_path == NULL || vd->vdev_path[0] != '/') {
87fa9e4066Sahrens 		vd->vdev_stat.vs_aux = VDEV_AUX_BAD_LABEL;
88be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
89fa9e4066Sahrens 	}
90fa9e4066Sahrens 
91095bcd66SGeorge Wilson 	/*
92095bcd66SGeorge Wilson 	 * Reopen the device if it's not currently open.  Otherwise,
93095bcd66SGeorge Wilson 	 * just update the physical size of the device.
94095bcd66SGeorge Wilson 	 */
95095bcd66SGeorge Wilson 	if (vd->vdev_tsd != NULL) {
96095bcd66SGeorge Wilson 		ASSERT(vd->vdev_reopening);
97095bcd66SGeorge Wilson 		vf = vd->vdev_tsd;
98095bcd66SGeorge Wilson 		goto skip_open;
99095bcd66SGeorge Wilson 	}
100095bcd66SGeorge Wilson 
101fa9e4066Sahrens 	vf = vd->vdev_tsd = kmem_zalloc(sizeof (vdev_file_t), KM_SLEEP);
102fa9e4066Sahrens 
103fa9e4066Sahrens 	/*
104fa9e4066Sahrens 	 * We always open the files from the root of the global zone, even if
105fa9e4066Sahrens 	 * we're in a local zone.  If the user has gotten to this point, the
106fa9e4066Sahrens 	 * administrator has already decided that the pool should be available
107fa9e4066Sahrens 	 * to local zone users, so the underlying devices should be as well.
108fa9e4066Sahrens 	 */
109fa9e4066Sahrens 	ASSERT(vd->vdev_path != NULL && vd->vdev_path[0] == '/');
1100a4e9518Sgw 	error = vn_openat(vd->vdev_path + 1, UIO_SYSSPACE,
1118ad4d6ddSJeff Bonwick 	    spa_mode(vd->vdev_spa) | FOFFMAX, 0, &vp, 0, 0, rootdir, -1);
112fa9e4066Sahrens 
113fa9e4066Sahrens 	if (error) {
114fa9e4066Sahrens 		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
115fa9e4066Sahrens 		return (error);
116fa9e4066Sahrens 	}
117fa9e4066Sahrens 
118fa9e4066Sahrens 	vf->vf_vnode = vp;
119fa9e4066Sahrens 
120fa9e4066Sahrens #ifdef _KERNEL
121fa9e4066Sahrens 	/*
122fa9e4066Sahrens 	 * Make sure it's a regular file.
123fa9e4066Sahrens 	 */
124fa9e4066Sahrens 	if (vp->v_type != VREG) {
125fa9e4066Sahrens 		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
126be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENODEV));
127fa9e4066Sahrens 	}
128fa9e4066Sahrens #endif
129095bcd66SGeorge Wilson 
130095bcd66SGeorge Wilson skip_open:
131fa9e4066Sahrens 	/*
132fa9e4066Sahrens 	 * Determine the physical size of the file.
133fa9e4066Sahrens 	 */
134fa9e4066Sahrens 	vattr.va_mask = AT_SIZE;
135da6c28aaSamw 	error = VOP_GETATTR(vf->vf_vnode, &vattr, 0, kcred, NULL);
136fa9e4066Sahrens 	if (error) {
137fa9e4066Sahrens 		vd->vdev_stat.vs_aux = VDEV_AUX_OPEN_FAILED;
138fa9e4066Sahrens 		return (error);
139fa9e4066Sahrens 	}
140fa9e4066Sahrens 
1414263d13fSGeorge Wilson 	*max_psize = *psize = vattr.va_size;
142fa9e4066Sahrens 	*ashift = SPA_MINBLOCKSHIFT;
143fa9e4066Sahrens 
144fa9e4066Sahrens 	return (0);
145fa9e4066Sahrens }
146fa9e4066Sahrens 
147fa9e4066Sahrens static void
vdev_file_close(vdev_t * vd)148fa9e4066Sahrens vdev_file_close(vdev_t *vd)
149fa9e4066Sahrens {
150fa9e4066Sahrens 	vdev_file_t *vf = vd->vdev_tsd;
151fa9e4066Sahrens 
152095bcd66SGeorge Wilson 	if (vd->vdev_reopening || vf == NULL)
153fa9e4066Sahrens 		return;
154fa9e4066Sahrens 
155fa9e4066Sahrens 	if (vf->vf_vnode != NULL) {
156da6c28aaSamw 		(void) VOP_PUTPAGE(vf->vf_vnode, 0, 0, B_INVAL, kcred, NULL);
1578ad4d6ddSJeff Bonwick 		(void) VOP_CLOSE(vf->vf_vnode, spa_mode(vd->vdev_spa), 1, 0,
1588ad4d6ddSJeff Bonwick 		    kcred, NULL);
159fa9e4066Sahrens 		VN_RELE(vf->vf_vnode);
160fa9e4066Sahrens 	}
161fa9e4066Sahrens 
16298d1cbfeSGeorge Wilson 	vd->vdev_delayed_close = B_FALSE;
163fa9e4066Sahrens 	kmem_free(vf, sizeof (vdev_file_t));
164fa9e4066Sahrens 	vd->vdev_tsd = NULL;
165fa9e4066Sahrens }
166fa9e4066Sahrens 
16731d7e8faSGeorge Wilson /*
16831d7e8faSGeorge Wilson  * Implements the interrupt side for file vdev types. This routine will be
16931d7e8faSGeorge Wilson  * called when the I/O completes allowing us to transfer the I/O to the
17031d7e8faSGeorge Wilson  * interrupt taskqs. For consistency, the code structure mimics disk vdev
17131d7e8faSGeorge Wilson  * types.
17231d7e8faSGeorge Wilson  */
173c62757b2SToomas Soome static int
vdev_file_io_intr(buf_t * bp)17431d7e8faSGeorge Wilson vdev_file_io_intr(buf_t *bp)
17531d7e8faSGeorge Wilson {
17631d7e8faSGeorge Wilson 	vdev_buf_t *vb = (vdev_buf_t *)bp;
17731d7e8faSGeorge Wilson 	zio_t *zio = vb->vb_io;
17831d7e8faSGeorge Wilson 
17931d7e8faSGeorge Wilson 	zio->io_error = (geterror(bp) != 0 ? EIO : 0);
18031d7e8faSGeorge Wilson 	if (zio->io_error == 0 && bp->b_resid != 0)
181be6fd75aSMatthew Ahrens 		zio->io_error = SET_ERROR(ENOSPC);
18231d7e8faSGeorge Wilson 
183770499e1SDan Kimmel 	if (zio->io_type == ZIO_TYPE_READ) {
184770499e1SDan Kimmel 		abd_return_buf_copy(zio->io_abd, bp->b_un.b_addr, zio->io_size);
185770499e1SDan Kimmel 	} else {
186770499e1SDan Kimmel 		abd_return_buf(zio->io_abd, bp->b_un.b_addr, zio->io_size);
187770499e1SDan Kimmel 	}
188770499e1SDan Kimmel 
18931d7e8faSGeorge Wilson 	kmem_free(vb, sizeof (vdev_buf_t));
19097e81309SPrakash Surya 	zio_delay_interrupt(zio);
191c62757b2SToomas Soome 	return (0);
19231d7e8faSGeorge Wilson }
19331d7e8faSGeorge Wilson 
19431d7e8faSGeorge Wilson static void
vdev_file_io_strategy(void * arg)19531d7e8faSGeorge Wilson vdev_file_io_strategy(void *arg)
19631d7e8faSGeorge Wilson {
19731d7e8faSGeorge Wilson 	buf_t *bp = arg;
19831d7e8faSGeorge Wilson 	vnode_t *vp = bp->b_private;
19931d7e8faSGeorge Wilson 	ssize_t resid;
20031d7e8faSGeorge Wilson 	int error;
20131d7e8faSGeorge Wilson 
20231d7e8faSGeorge Wilson 	error = vn_rdwr((bp->b_flags & B_READ) ? UIO_READ : UIO_WRITE,
20331d7e8faSGeorge Wilson 	    vp, bp->b_un.b_addr, bp->b_bcount, ldbtob(bp->b_lblkno),
20431d7e8faSGeorge Wilson 	    UIO_SYSSPACE, 0, RLIM64_INFINITY, kcred, &resid);
20531d7e8faSGeorge Wilson 
20631d7e8faSGeorge Wilson 	if (error == 0) {
20731d7e8faSGeorge Wilson 		bp->b_resid = resid;
20831d7e8faSGeorge Wilson 		biodone(bp);
20931d7e8faSGeorge Wilson 	} else {
21031d7e8faSGeorge Wilson 		bioerror(bp, error);
21131d7e8faSGeorge Wilson 		biodone(bp);
21231d7e8faSGeorge Wilson 	}
21331d7e8faSGeorge Wilson }
21431d7e8faSGeorge Wilson 
215738f37bcSGeorge Wilson static void
vdev_file_io_start(zio_t * zio)216fa9e4066Sahrens vdev_file_io_start(zio_t *zio)
217fa9e4066Sahrens {
218fa9e4066Sahrens 	vdev_t *vd = zio->io_vd;
219fa9e4066Sahrens 	vdev_file_t *vf = vd->vdev_tsd;
22031d7e8faSGeorge Wilson 	vdev_buf_t *vb;
22131d7e8faSGeorge Wilson 	buf_t *bp;
222fa9e4066Sahrens 
223fa9e4066Sahrens 	if (zio->io_type == ZIO_TYPE_IOCTL) {
224fa9e4066Sahrens 		/* XXPOLICY */
2250a4e9518Sgw 		if (!vdev_readable(vd)) {
226be6fd75aSMatthew Ahrens 			zio->io_error = SET_ERROR(ENXIO);
227738f37bcSGeorge Wilson 			zio_interrupt(zio);
228738f37bcSGeorge Wilson 			return;
229fa9e4066Sahrens 		}
230fa9e4066Sahrens 
231fa9e4066Sahrens 		switch (zio->io_cmd) {
232fa9e4066Sahrens 		case DKIOCFLUSHWRITECACHE:
233fa9e4066Sahrens 			zio->io_error = VOP_FSYNC(vf->vf_vnode, FSYNC | FDSYNC,
234da6c28aaSamw 			    kcred, NULL);
235fa9e4066Sahrens 			break;
236fa9e4066Sahrens 		default:
237be6fd75aSMatthew Ahrens 			zio->io_error = SET_ERROR(ENOTSUP);
238fa9e4066Sahrens 		}
239fa9e4066Sahrens 
240084fd14fSBrian Behlendorf 		zio_execute(zio);
241084fd14fSBrian Behlendorf 		return;
242084fd14fSBrian Behlendorf 	} else if (zio->io_type == ZIO_TYPE_TRIM) {
243084fd14fSBrian Behlendorf 		struct flock64 flck;
244084fd14fSBrian Behlendorf 
245084fd14fSBrian Behlendorf 		ASSERT3U(zio->io_size, !=, 0);
246084fd14fSBrian Behlendorf 		bzero(&flck, sizeof (flck));
247084fd14fSBrian Behlendorf 		flck.l_type = F_FREESP;
248084fd14fSBrian Behlendorf 		flck.l_start = zio->io_offset;
249084fd14fSBrian Behlendorf 		flck.l_len = zio->io_size;
250084fd14fSBrian Behlendorf 		flck.l_whence = 0;
251084fd14fSBrian Behlendorf 
252084fd14fSBrian Behlendorf 		zio->io_error = VOP_SPACE(vf->vf_vnode, F_FREESP, &flck,
253084fd14fSBrian Behlendorf 		    0, 0, kcred, NULL);
254084fd14fSBrian Behlendorf 
255738f37bcSGeorge Wilson 		zio_execute(zio);
256738f37bcSGeorge Wilson 		return;
257fa9e4066Sahrens 	}
258fa9e4066Sahrens 
259f693d300SSteven Hartland 	ASSERT(zio->io_type == ZIO_TYPE_READ || zio->io_type == ZIO_TYPE_WRITE);
26097e81309SPrakash Surya 	zio->io_target_timestamp = zio_handle_io_delay(zio);
261f693d300SSteven Hartland 
26231d7e8faSGeorge Wilson 	vb = kmem_alloc(sizeof (vdev_buf_t), KM_SLEEP);
263fa9e4066Sahrens 
26431d7e8faSGeorge Wilson 	vb->vb_io = zio;
26531d7e8faSGeorge Wilson 	bp = &vb->vb_buf;
266fa9e4066Sahrens 
26731d7e8faSGeorge Wilson 	bioinit(bp);
26831d7e8faSGeorge Wilson 	bp->b_flags = (zio->io_type == ZIO_TYPE_READ ? B_READ : B_WRITE);
26931d7e8faSGeorge Wilson 	bp->b_bcount = zio->io_size;
270770499e1SDan Kimmel 
271770499e1SDan Kimmel 	if (zio->io_type == ZIO_TYPE_READ) {
272770499e1SDan Kimmel 		bp->b_un.b_addr =
273770499e1SDan Kimmel 		    abd_borrow_buf(zio->io_abd, zio->io_size);
274770499e1SDan Kimmel 	} else {
275770499e1SDan Kimmel 		bp->b_un.b_addr =
276770499e1SDan Kimmel 		    abd_borrow_buf_copy(zio->io_abd, zio->io_size);
277770499e1SDan Kimmel 	}
278770499e1SDan Kimmel 
27931d7e8faSGeorge Wilson 	bp->b_lblkno = lbtodb(zio->io_offset);
28031d7e8faSGeorge Wilson 	bp->b_bufsize = zio->io_size;
28131d7e8faSGeorge Wilson 	bp->b_private = vf->vf_vnode;
282c62757b2SToomas Soome 	bp->b_iodone = vdev_file_io_intr;
28331d7e8faSGeorge Wilson 
2842c1e2b44SGeorge Wilson 	VERIFY3U(taskq_dispatch(system_taskq, vdev_file_io_strategy, bp,
285fc8ae2ecSToomas Soome 	    TQ_SLEEP), !=, TASKQID_INVALID);
286fa9e4066Sahrens }
287fa9e4066Sahrens 
288e14bb325SJeff Bonwick /* ARGSUSED */
289e14bb325SJeff Bonwick static void
vdev_file_io_done(zio_t * zio)290fa9e4066Sahrens vdev_file_io_done(zio_t *zio)
291fa9e4066Sahrens {
292fa9e4066Sahrens }
293fa9e4066Sahrens 
294fa9e4066Sahrens vdev_ops_t vdev_file_ops = {
295a3874b8bSToomas Soome 	.vdev_op_open = vdev_file_open,
296a3874b8bSToomas Soome 	.vdev_op_close = vdev_file_close,
297a3874b8bSToomas Soome 	.vdev_op_asize = vdev_default_asize,
298a3874b8bSToomas Soome 	.vdev_op_io_start = vdev_file_io_start,
299a3874b8bSToomas Soome 	.vdev_op_io_done = vdev_file_io_done,
300a3874b8bSToomas Soome 	.vdev_op_state_change = NULL,
301a3874b8bSToomas Soome 	.vdev_op_need_resilver = NULL,
302a3874b8bSToomas Soome 	.vdev_op_hold = vdev_file_hold,
303a3874b8bSToomas Soome 	.vdev_op_rele = vdev_file_rele,
304a3874b8bSToomas Soome 	.vdev_op_remap = NULL,
305a3874b8bSToomas Soome 	.vdev_op_xlate = vdev_default_xlate,
306*ac04831dSMike Gerdts 	.vdev_op_dumpio = NULL,
307a3874b8bSToomas Soome 	.vdev_op_type = VDEV_TYPE_FILE,		/* name of this vdev type */
308a3874b8bSToomas Soome 	.vdev_op_leaf = B_TRUE			/* leaf vdev */
309fa9e4066Sahrens };
310fa9e4066Sahrens 
311fa9e4066Sahrens /*
312fa9e4066Sahrens  * From userland we access disks just like files.
313fa9e4066Sahrens  */
314fa9e4066Sahrens #ifndef _KERNEL
315fa9e4066Sahrens 
316fa9e4066Sahrens vdev_ops_t vdev_disk_ops = {
317a3874b8bSToomas Soome 	.vdev_op_open = vdev_file_open,
318a3874b8bSToomas Soome 	.vdev_op_close = vdev_file_close,
319a3874b8bSToomas Soome 	.vdev_op_asize = vdev_default_asize,
320a3874b8bSToomas Soome 	.vdev_op_io_start = vdev_file_io_start,
321a3874b8bSToomas Soome 	.vdev_op_io_done = vdev_file_io_done,
322a3874b8bSToomas Soome 	.vdev_op_state_change = NULL,
323a3874b8bSToomas Soome 	.vdev_op_need_resilver = NULL,
324a3874b8bSToomas Soome 	.vdev_op_hold = vdev_file_hold,
325a3874b8bSToomas Soome 	.vdev_op_rele = vdev_file_rele,
326a3874b8bSToomas Soome 	.vdev_op_remap = NULL,
327a3874b8bSToomas Soome 	.vdev_op_xlate = vdev_default_xlate,
328*ac04831dSMike Gerdts 	.vdev_op_dumpio = NULL,
329a3874b8bSToomas Soome 	.vdev_op_type = VDEV_TYPE_DISK,		/* name of this vdev type */
330a3874b8bSToomas Soome 	.vdev_op_leaf = B_TRUE			/* leaf vdev */
331fa9e4066Sahrens };
332fa9e4066Sahrens 
333fa9e4066Sahrens #endif
334