xref: /illumos-gate/usr/src/uts/common/os/dkioc_free_util.c (revision 66582b606a8194f7f3ba5b3a3a6dca5b0d346361)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2017 Nexenta Inc.  All rights reserved.
14  */
15 
16 /* needed when building libzpool */
17 #ifndef	_KERNEL
18 #include <sys/zfs_context.h>
19 #endif
20 
21 #include <sys/sunddi.h>
22 #include <sys/dkio.h>
23 #include <sys/dkioc_free_util.h>
24 #include <sys/sysmacros.h>
25 #include <sys/file.h>
26 #include <sys/sdt.h>
27 
28 /*
29  * Copy-in convenience function for variable-length dkioc_free_list_t
30  * structures. The pointer to be copied from is in `arg' (may be a pointer
31  * to userspace). A new buffer is allocated and a pointer to it is placed
32  * in `out'. `ddi_flags' indicates whether the pointer is from user-
33  * or kernelspace (FKIOCTL) and `kmflags' are the flags passed to
34  * kmem_zalloc when allocating the new structure.
35  * Returns 0 on success, or an errno on failure.
36  */
37 int
38 dfl_copyin(void *arg, dkioc_free_list_t **out, int ddi_flags, int kmflags)
39 {
40 	dkioc_free_list_t *dfl;
41 
42 	if (ddi_flags & FKIOCTL) {
43 		dkioc_free_list_t *dfl_in = arg;
44 
45 		if (dfl_in->dfl_num_exts == 0 ||
46 		    dfl_in->dfl_num_exts > DFL_COPYIN_MAX_EXTS)
47 			return (SET_ERROR(EINVAL));
48 		dfl = kmem_alloc(DFL_SZ(dfl_in->dfl_num_exts), kmflags);
49 		if (dfl == NULL)
50 			return (SET_ERROR(ENOMEM));
51 		bcopy(dfl_in, dfl, DFL_SZ(dfl_in->dfl_num_exts));
52 	} else {
53 		uint64_t num_exts;
54 
55 		if (ddi_copyin(((uint8_t *)arg) + offsetof(dkioc_free_list_t,
56 		    dfl_num_exts), &num_exts, sizeof (num_exts),
57 		    ddi_flags) != 0)
58 			return (SET_ERROR(EFAULT));
59 		if (num_exts == 0 || num_exts > DFL_COPYIN_MAX_EXTS)
60 			return (SET_ERROR(EINVAL));
61 		dfl = kmem_alloc(DFL_SZ(num_exts), kmflags);
62 		if (dfl == NULL)
63 			return (SET_ERROR(ENOMEM));
64 		if (ddi_copyin(arg, dfl, DFL_SZ(num_exts), ddi_flags) != 0 ||
65 		    dfl->dfl_num_exts != num_exts) {
66 			kmem_free(dfl, DFL_SZ(num_exts));
67 			return (SET_ERROR(EFAULT));
68 		}
69 	}
70 
71 	*out = dfl;
72 	return (0);
73 }
74 
75 /* Frees a variable-length dkioc_free_list_t structure. */
76 void
77 dfl_free(dkioc_free_list_t *dfl)
78 {
79 	kmem_free(dfl, DFL_SZ(dfl->dfl_num_exts));
80 }
81