aboutsummaryrefslogtreecommitdiffstats
path: root/liblfjail/zfs.hh
blob: 34ddb06fde2e6fc876bc79e610d2433bafdb560f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
 * This source code is released into the public domain.
 */

#ifndef	LFJAIL_ZFS_HH
#define	LFJAIL_ZFS_HH

#include "generator.hh"
#include "generic_error.hh"
#include "spawn.hh"

namespace lfjail::zfs {

// Hardcode the program locations so we don't rely on $PATH.
constexpr std::string path_zpool = "/sbin/zpool";
constexpr std::string path_zfs = "/sbin/zfs";

/*
 * Exception thrown when a ZFS operation fails.
 */
struct zfs_error : generic_error {
	template<typename... Args>
	zfs_error(std::format_string<Args...> fmt, Args &&...args)
		: generic_error(fmt, std::forward<Args>(args)...)
	{}
};

/*
 * Data type for an enumeration value.
 */

template<typename Tag>
struct enumeration_value final {
	const std::string_view value;
};

template<typename Tag>
struct enumeration final : Tag {
	enumeration() = default;

	enumeration(enumeration_value<Tag> value_)
		: value(value_.value)
	{}

	enumeration(enumeration const &) = default;
	enumeration(enumeration &&) noexcept = default;

	auto operator=(enumeration const &) -> enumeration& = default;
	auto operator=(enumeration &&) noexcept -> enumeration& = default;

	auto operator=(enumeration_value<Tag> newvalue) -> enumeration & {
		value = newvalue.value;
		return *this;
	}

	std::string_view value;
};

template<typename Tag>
auto operator==(enumeration<Tag> const &lhs,
		enumeration_value<Tag> const &rhs)
	-> bool
{
	return lhs.value == rhs.value;
}

struct failmode {
	enumeration_value<failmode> continue_{"continue"};
	enumeration_value<failmode> panic{"panic"};
	enumeration_value<failmode> wait{"wait"};
};

using failmode_t = enumeration<failmode>;

struct dataset_type {
	enumeration_value<dataset_type> filesystem{"filesystem"};
	enumeration_value<dataset_type> volume{"volume"};
	enumeration_value<dataset_type> snapshot{"snapshot"};
	enumeration_value<dataset_type> bookmark{"bookmark"};
};

using dataset_type_t = enumeration<dataset_type>;

struct snapdir {
	enumeration_value<snapdir> disabled{"disabled"};
	enumeration_value<snapdir> hidden{"hidden"};
	enumeration_value<snapdir> visible{"visible"};
};

using snapdir_t = enumeration<snapdir>;

struct aclmode {
	enumeration_value<aclmode> discard{"discard"};
	enumeration_value<aclmode> groupmask{"groupmask"};
	enumeration_value<aclmode> passthrough{"passthrough"};
	enumeration_value<aclmode> restricted{"restricted"};
};

using aclmode_t = enumeration<aclmode>;

struct aclinherit {
	enumeration_value<aclinherit> discard{"discard"};
	enumeration_value<aclinherit> noallow{"noallow"};
	enumeration_value<aclinherit> restricted{"restricted"};
	enumeration_value<aclinherit> passthrough{"passthrough"};
	enumeration_value<aclinherit> passthrough_x{"passthrough-x"};
};

using aclinherit_t = enumeration<aclinherit>;

struct canmount {
	enumeration_value<canmount> on{"on"};
	enumeration_value<canmount> off{"off"};
	enumeration_value<canmount> noauto{"noauto"};
};

using canmount_t = enumeration<canmount>;

struct xattr {
	enumeration_value<xattr> on{"on"};
	enumeration_value<xattr> off{"off"};
	enumeration_value<xattr> dir{"dir"};
	enumeration_value<xattr> sa{"dir"};
};

using xattr_t = enumeration<xattr>;

struct normalization {
	enumeration_value<normalization> none{"none"};
	enumeration_value<normalization> formC{"formC"};
	enumeration_value<normalization> formD{"formD"};
	enumeration_value<normalization> formKC{"formKC"};
	enumeration_value<normalization> formKD{"formKD"};
};

using normalization_t = enumeration<normalization>;

struct casesensitivity {
	enumeration_value<casesensitivity> sensitive{"sensitive"};
	enumeration_value<casesensitivity> insensitive{"insensitive"};
	enumeration_value<casesensitivity> mixed{"mixed"};
};

using casesensitivity_t = enumeration<casesensitivity>;

struct primarycache {
	enumeration_value<primarycache> all{"all"};
	enumeration_value<primarycache> none{"none"};
	enumeration_value<primarycache> metadata{"metadata"};
};

using primarycache_t = enumeration<primarycache>;

struct secondarycache {
	enumeration_value<secondarycache> all{"all"};
	enumeration_value<secondarycache> none{"none"};
	enumeration_value<secondarycache> metadata{"metadata"};
};

using secondarycache_t = enumeration<secondarycache>;

struct logbias {
	enumeration_value<logbias> latency{"latency"};
	enumeration_value<logbias> throughput{"throughput"};
};

using logbias_t = enumeration<logbias>;

struct sync {
	enumeration_value<sync> standard{"standard"};
	enumeration_value<sync> disabled{"disabled"};
	enumeration_value<sync> always{"always"};
};

using sync_t = enumeration<sync>;

/*
 * Datasets.
 */

struct dataset {
	// Dataset identity.
	std::string name;
	dataset_type_t type;
	std::uint64_t guid = 0;
	std::uint64_t objsetid = 0;

	// Bookkeeping data, read-only.
	std::uint64_t creation = 0u;
	std::uint64_t used = 0u;
	std::uint64_t available = 0u;
	std::uint64_t referenced = 0u;
	std::uint64_t usedbysnapshots = 0u;
	std::uint64_t usedbydataset = 0u;
	std::uint64_t usedbychildren = 0u;
	std::uint64_t usedbyrefreservation = 0u;
	double compressratio = 0;
	double refcompressratio = 0;
	bool mounted = false;
	std::uint64_t createtxg = 0;

	// Mount options
	std::string mountpoint; // XXX - should have a way to handle none/legacy
	canmount_t canmount;

	// Miscellaneous options
	std::uint64_t quota = 0u;
	std::uint64_t reservation = 0u;
	std::uint64_t recordsize = 0u;
	std::string sharenfs;
	std::string sharesmb;
	std::string checksum;
	std::string compression;
	bool atime = false;
	bool devices = false;
	bool exec = false;
	bool setuid = false;
	bool readonly = false;
	std::string jailed;
	snapdir_t snapdir;
	aclmode_t aclmode;
	aclinherit_t aclinherit;
	xattr_t xattr;
	unsigned copies = 0;
	unsigned version = 0;
	bool utf8only = false;
	normalization_t normalization;
	casesensitivity_t casesensitivity;
	bool vscan = false;
	bool nbmand = false;
	std::uint64_t refquota = 0;
	std::uint64_t refreservation = 0;
	primarycache_t primarycache;
	secondarycache_t secondarycache;
	logbias_t logbias;
	std::string dedup;
	std::string mlslabel;
	sync_t sync;
	std::string dnodesize;
};

/* Return true if a dataset by this name exists. */
auto dataset_exists(std::string_view name) -> bool;

/* Fetch details for a specific dataset by name. */
auto get_dataset(std::string_view name) -> dataset;

/* Fetch names for all datasets on the system. */
auto get_dataset_names() -> std::generator<std::string>;

/* Fetch details for all datasets on the system. */
auto get_datasets() -> std::generator<dataset>;

/* Destroy a dataset */
auto dataset_destroy(std::string_view name) -> void;

/*
 * Create a new ZFS dataset.
 */
auto create_dataset(std::string const &dsname, auto &&...args) -> dataset {
	using namespace std::literals;

	try {
		auto ret = spawn(exec::execl(path_zfs,
				"zfs", "create", ("-o"s + args)...,
				"--", dsname)).wait();
		if (!ret)
			throw zfs_error("failed to create dataset");

		return get_dataset(dsname);
	} catch (std::exception const &exc) {
		throw zfs_error("failed to create dataset: {}", exc.what());
	}
}

/*
 * Pools.
 */

struct pool {
	// Pool identity
	std::string name;
	std::uint64_t guid = 0u;
	std::uint64_t load_guid = 0u;
	std::optional<std::string> comment;

	// Bookkeeping information, read-only
	std::uint64_t size = 0u;
	std::uint64_t free = 0u;
	std::uint64_t allocated = 0u;
	std::uint64_t expandsize = 0u;
	std::uint64_t freeing = 0u;
	std::uint64_t leaked = 0u;
	unsigned capacity = 0;
	unsigned fragmentation = 0;
	std::uint64_t bcloneused = 0;
	std::uint64_t bclonesaved = 0;
	double bcloneratio = 0;
	std::uint64_t dedup_table_size = 0;
	double dedupratio = 0;
	std::uint64_t last_scrubbed_txg = 0;
	std::string health;
	std::optional<std::string> checkpoint;

	// Paths
	std::optional<std::string> bootfs;
	std::optional<std::string> altroot;
	std::optional<std::string> cachefile;

	// Miscellaneous options
	bool delegation = false;
	bool autoreplace = false;
	bool listsnapshots = false;
	bool autoexpand = false;
	bool readonly = false;
	bool multihost = false;
	bool autotrim = false;
	failmode_t failmode;
	std::string compatibility;

	unsigned ashift = 0;

	// TODO - feature flags

	// Get the pool's root dataset.
	auto dataset() -> struct dataset;
};

/* Fetch details for a specific pool by name. */
auto get_pool(std::string_view name) -> pool;

/* Fetch details for all pools on the system. */
auto get_pools() -> std::generator<pool>;

} // namespace lfjail::zfs

#endif	// LFJAIL_ZFS_HH