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
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.
13 * Copyright 2017 Nexenta Inc. All rights reserved.
16 /* needed when building libzpool */
18 #include <sys/zfs_context.h>
21 #include <sys/sunddi.h>
23 #include <sys/dkioc_free_util.h>
24 #include <sys/sysmacros.h>
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.
38 dfl_copyin(void *arg
, dkioc_free_list_t
**out
, int ddi_flags
, int kmflags
)
40 dkioc_free_list_t
*dfl
;
42 if (ddi_flags
& FKIOCTL
) {
43 dkioc_free_list_t
*dfl_in
= arg
;
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
);
50 return (SET_ERROR(ENOMEM
));
51 bcopy(dfl_in
, dfl
, DFL_SZ(dfl_in
->dfl_num_exts
));
55 if (ddi_copyin(((uint8_t *)arg
) + offsetof(dkioc_free_list_t
,
56 dfl_num_exts
), &num_exts
, sizeof (num_exts
),
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
);
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
));
75 /* Frees a variable-length dkioc_free_list_t structure. */
77 dfl_free(dkioc_free_list_t
*dfl
)
79 kmem_free(dfl
, DFL_SZ(dfl
->dfl_num_exts
));