/
era_dm.c
376 lines (297 loc) · 7.21 KB
/
era_dm.c
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
* This file is released under the GPL.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include "era.h"
#include "era_dm.h"
#include <libdevmapper.h>
void era_dm_init(void)
{
dm_lib_init();
}
void era_dm_exit(void)
{
dm_lib_release();
dm_lib_exit();
}
static int _dm_create(int task, int wait,
const char *name, const char *uuid,
uint64_t start, uint64_t length,
const char *target, const char *table,
struct era_dm_info *info)
{
struct dm_task *dmt;
uint32_t cookie = 0;
int rc;
if (!(dmt = dm_task_create(task)))
return -1;
if (!dm_task_set_name(dmt, name))
goto out;
if (uuid && !dm_task_set_uuid(dmt, uuid))
goto out;
if (target && !dm_task_add_target(dmt, start, length, target, table))
goto out;
if (wait && !dm_task_set_cookie(dmt, &cookie, 0))
goto out;
rc = dm_task_run(dmt);
if (wait)
(void) dm_udev_wait(cookie);
if (rc && info)
{
struct dm_info dmi;
if (!dm_task_get_info(dmt, &dmi))
goto out;
info->target_count = dmi.target_count;
info->open_count = dmi.open_count;
info->suspended = dmi.suspended;
info->exists = dmi.exists;
info->major = dmi.major;
info->minor = dmi.minor;
}
dm_task_destroy(dmt);
return rc ? 0 : -1;
out:
dm_task_destroy(dmt);
return -1;
}
static int _dm_simple(int task, int wait, const char *name)
{
struct dm_task *dmt;
uint32_t cookie = 0;
int rc;
if (!(dmt = dm_task_create(task)))
return -1;
if (!dm_task_set_name(dmt, name))
goto out;
if (wait && !dm_task_set_cookie(dmt, &cookie, 0))
goto out;
rc = dm_task_run(dmt);
if (wait)
(void) dm_udev_wait(cookie);
dm_task_destroy(dmt);
return rc ? 0 : -1;
out:
dm_task_destroy(dmt);
return -1;
}
int era_dm_create_empty(const char *name, const char *uuid,
struct era_dm_info *info)
{
return _dm_create(DM_DEVICE_CREATE, 0, name, uuid,
0, 0, NULL, NULL, info);
}
int era_dm_create(const char *name, const char *uuid,
uint64_t start, uint64_t length,
const char *target, const char *table,
struct era_dm_info *info)
{
return _dm_create(DM_DEVICE_CREATE, 1, name, uuid,
start, length, target, table, info);
}
int era_dm_load(const char *name,
uint64_t start, uint64_t length,
const char *target, const char *table,
struct era_dm_info *info)
{
return _dm_create(DM_DEVICE_RELOAD, 0, name, NULL,
start, length, target, table, info);
}
int era_dm_suspend(const char *name)
{
return _dm_simple(DM_DEVICE_SUSPEND, 0, name);
}
int era_dm_resume(const char *name)
{
return _dm_simple(DM_DEVICE_RESUME, 1, name);
}
int era_dm_remove(const char *name)
{
return _dm_simple(DM_DEVICE_REMOVE, 1, name);
}
int era_dm_clear(const char *name)
{
return _dm_simple(DM_DEVICE_CLEAR, 0, name);
}
int era_dm_info(const char *name,
const char *uuid,
struct era_dm_info *info,
size_t name_size, char *name_ptr,
size_t uuid_size, char *uuid_ptr)
{
struct dm_task *dmt;
struct dm_info dmi;
int rc = -1;
if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
return -1;
if (name && !dm_task_set_name(dmt, name))
goto out;
if (uuid && !dm_task_set_uuid(dmt, uuid))
goto out;
if (!dm_task_run(dmt))
goto out;
if (!dm_task_get_info(dmt, &dmi))
goto out;
if (info)
{
info->target_count = dmi.target_count;
info->open_count = dmi.open_count;
info->suspended = dmi.suspended;
info->exists = dmi.exists;
info->major = dmi.major;
info->minor = dmi.minor;
}
if (dmi.exists && name_size > 0 && name_ptr)
{
const char *dm_name;
size_t dm_name_len;
dm_name = dm_task_get_name(dmt);
if (!dm_name)
goto out;
dm_name_len = strlen(dm_name);
if (dm_name_len >= name_size)
{
rc = dm_name_len + 1;
goto out;
}
strcpy(name_ptr, dm_name);
}
if (dmi.exists && uuid_size > 0 && uuid_ptr)
{
const char *dm_uuid;
size_t dm_uuid_len;
dm_uuid = dm_task_get_uuid(dmt);
if (!dm_uuid)
goto out;
dm_uuid_len = strlen(dm_uuid);
if (dm_uuid_len >= uuid_size)
{
dm_task_destroy(dmt);
return dm_uuid_len + 1;
}
strcpy(uuid_ptr, dm_uuid);
}
rc = 0;
out:
dm_task_destroy(dmt);
return rc;
}
static int _first_status(int task,
const char *name,
const char *uuid,
uint64_t *start, uint64_t *length,
size_t target_size, char *target_ptr,
size_t params_size, char *params_ptr)
{
uint64_t a, b;
struct dm_task *dmt;
struct dm_info dmi;
char *tgt, *prm;
int rc = -1;
if (!(dmt = dm_task_create(task)))
return -1;
if (name && !dm_task_set_name(dmt, name))
goto out;
if (uuid && !dm_task_set_uuid(dmt, uuid))
goto out;
if (!dm_task_run(dmt))
goto out;
if (!dm_task_get_info(dmt, &dmi))
goto out;
if (!dmi.exists)
{
error(0, "target %s does not exists",
name ? name : (uuid ? uuid : "<NULL>"));
goto out;
}
(void)dm_get_next_target(dmt, NULL, &a, &b, &tgt, &prm);
if (start)
*start = a;
if (length)
*length = b;
if (target_size > 0 && target_ptr)
{
if (strlen(tgt) >= target_size)
{
error(0, "too long target name");
goto out;
}
strcpy(target_ptr, tgt);
}
if (params_size > 0 && params_ptr)
{
if (strlen(prm) >= params_size)
{
error(0, "too long target params");
goto out;
}
strcpy(params_ptr, prm);
}
rc = 0;
out:
dm_task_destroy(dmt);
return rc;
}
int era_dm_first_table(const char *name,
const char *uuid,
uint64_t *start, uint64_t *length,
size_t target_size, char *target_ptr,
size_t params_size, char *params_ptr)
{
return _first_status(DM_DEVICE_TABLE, name, uuid, start, length,
target_size, target_ptr, params_size, params_ptr);
}
int era_dm_first_status(const char *name,
const char *uuid,
uint64_t *start, uint64_t *length,
size_t target_size, char *target_ptr,
size_t params_size, char *params_ptr)
{
return _first_status(DM_DEVICE_STATUS, name, uuid, start, length,
target_size, target_ptr, params_size, params_ptr);
}
int era_dm_message0(const char *name, const char *message)
{
struct dm_task *dmt;
int rc = -1;
if (!(dmt = dm_task_create(DM_DEVICE_TARGET_MSG)))
return -1;
if (!dm_task_set_name(dmt, name))
goto out;
if (!dm_task_set_sector(dmt, 0))
goto out;
if (!dm_task_set_message(dmt, message))
goto out;
if (!dm_task_run(dmt))
goto out;
rc = 0;
out:
dm_task_destroy(dmt);
return rc;
}
int era_dm_list(int (*cb)(void *arg, const char *name), void *cbarg)
{
struct dm_task *dmt;
struct dm_names *names;
unsigned next = 0;
if (!(dmt = dm_task_create(DM_DEVICE_LIST)))
return -1;
if (!dm_task_run(dmt))
goto out;
if (!(names = dm_task_get_names(dmt)))
goto out;
if (names->dev)
{
do {
names = (struct dm_names *)((char *)names + next);
if (cb(cbarg, names->name))
goto out;
next = names->next;
} while(next);
}
dm_task_destroy(dmt);
return 0;
out:
dm_task_destroy(dmt);
return -1;
}