this repo has no description
1/*
2 * Copyright (C) 2020-2021 Anil Madhavapeddy
3 * Copyright (C) 2020-2021 Sadiq Jaffer
4 * Copyright (C) 2022 Christiano Haesbaert
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include <unistd.h>
20#include <stdio.h>
21#include <stdint.h>
22#include <stdbool.h>
23
24#include <libzfs_core.h>
25#include <libzfs.h>
26#include <caml/alloc.h>
27#include <caml/bigarray.h>
28#include <caml/callback.h>
29#include <caml/custom.h>
30#include <caml/fail.h>
31#include <caml/memory.h>
32#include <caml/mlvalues.h>
33#include <caml/signals.h>
34#include <caml/unixsupport.h>
35#include <caml/socketaddr.h>
36
37#undef ZFS_DEBUG
38#ifdef ZFS_DEBUG
39#define dprintf(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)
40#else
41#define dprintf(fmt, ...) ((void)0)
42#endif
43
44value ocaml_zfs_prop_is_string(value v_prop){
45 int res;
46 res = zfs_prop_is_string(Int_val(v_prop));
47 if (res < 0) {
48 caml_failwith("Error occurred!");
49 }
50 return Val_bool(res);
51}
52
53#define Zfs_list_val(v) (*((struct nv_list **) Data_custom_val(v)))
54#define Zfs_handle_val(v) (*((libzfs_handle_t **) Data_custom_val(v)))
55#define Zfs_pool_val(v) (*((zpool_handle_t **) Data_custom_val(v)))
56
57static void finalize_zfs_list(value v) {
58 caml_stat_free(Zfs_list_val(v));
59 Zfs_list_val(v) = NULL;
60}
61
62static struct custom_operations zfs_list_ops = {
63 "zfs.zfs_list_ops",
64 finalize_zfs_list,
65 custom_compare_default,
66 custom_hash_default,
67 custom_serialize_default,
68 custom_deserialize_default,
69 custom_compare_ext_default,
70 custom_fixed_length_default
71};
72
73static void finalize_zfs_handle(value v) {
74 caml_stat_free(Zfs_handle_val(v));
75 Zfs_handle_val(v) = NULL;
76}
77
78static struct custom_operations zfs_handle_ops = {
79 "zfs.zfs_handle",
80 finalize_zfs_handle,
81 custom_compare_default,
82 custom_hash_default,
83 custom_serialize_default,
84 custom_deserialize_default,
85 custom_compare_ext_default,
86 custom_fixed_length_default
87};
88
89static void finalize_zfs_pool(value v) {
90 caml_stat_free(Zfs_pool_val(v));
91 Zfs_pool_val(v) = NULL;
92}
93
94static struct custom_operations zfs_pool_ops = {
95 "zfs.zfs_pool",
96 finalize_zfs_pool,
97 custom_compare_default,
98 custom_hash_default,
99 custom_serialize_default,
100 custom_deserialize_default,
101 custom_compare_ext_default,
102 custom_fixed_length_default
103};
104
105// ZFS Initialisation
106
107value
108ocaml_zfs_init(value v_unit) {
109 CAMLparam0();
110 libzfs_handle_t* res;
111 CAMLlocal1(v_handle);
112
113 v_handle = caml_alloc_custom_mem(&zfs_handle_ops, sizeof(libzfs_handle_t*), 64);
114 res = libzfs_init();
115 Zfs_handle_val(v_handle) = res;
116
117 CAMLreturn(v_handle);
118}
119
120// ZFS Pools
121
122value
123ocaml_zfs_pool_open(value v_handle, value v_path) {
124 CAMLparam2(v_handle, v_path);
125 zpool_handle_t* res;
126 CAMLlocal1(v_pool);
127
128 if (!caml_string_is_c_safe(v_path))
129 caml_invalid_argument("ocaml_zfs_pool_open: path is not C-safe");
130
131 v_pool = caml_alloc_custom_mem(&zfs_pool_ops, sizeof(zpool_handle_t*), 64);
132 res = zpool_open(Zfs_handle_val(v_handle), String_val(v_path));
133 Zfs_pool_val(v_handle) = res;
134
135 CAMLreturn(v_handle);
136}
137
138value
139ocaml_zfs_pool_get_name(value v_pool) {
140 CAMLparam1(v_pool);
141 CAMLlocal1(v_path);
142 const char* result;
143
144 result = zpool_get_name(Zfs_pool_val(v_pool));
145 v_path = caml_copy_string(result);
146
147 CAMLreturn(v_path);
148}
149
150