Remove unneeded timeout when remove of temporary device succeeded.
[platform/upstream/cryptsetup.git] / lib / libdevmapper.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <stdint.h>
5 #include <stdarg.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <sys/sysmacros.h>
9 #include <sys/ioctl.h>
10 #include <unistd.h>
11 #include <dirent.h>
12 #include <errno.h>
13 #include <libdevmapper.h>
14 #include <fcntl.h>
15 #include <linux/fs.h>
16
17 #include "libcryptsetup.h"
18 #include "internal.h"
19 #include "luks.h"
20
21 #define DEVICE_DIR              "/dev"
22 #define DM_UUID_PREFIX          "CRYPT-"
23 #define DM_UUID_PREFIX_LEN      6
24 #define DM_UUID_LEN             UUID_STRING_L
25 #define DM_CRYPT_TARGET         "crypt"
26 #define RETRY_COUNT             5
27
28 static void set_dm_error(int level, const char *file, int line,
29                          const char *f, ...)
30 {
31         va_list va;
32
33         if (level > 3)
34                 return;
35
36         va_start(va, f);
37         set_error_va(f, va);
38         va_end(va);
39 }
40
41 static int _dm_simple(int task, const char *name);
42
43 static int dm_init(void)
44 {
45         dm_log_init(set_dm_error);
46         if (!_dm_simple(DM_DEVICE_LIST_VERSIONS, "test")) {
47                 set_error("Cannot communicate with device-mapper. Is the dm_mod module loaded?");
48                 return -1;
49         }
50
51         return 1;       /* unsafe memory */
52 }
53
54 static void dm_exit(void)
55 {
56         dm_log_init(NULL);
57         dm_lib_release();
58 }
59
60 static char *__lookup_dev(char *path, dev_t dev)
61 {
62         struct dirent *entry;
63         struct stat st;
64         char *ptr;
65         char *result = NULL;
66         DIR *dir;
67         int space;
68
69         path[PATH_MAX - 1] = '\0';
70         ptr = path + strlen(path);
71         *ptr++ = '/';
72         *ptr = '\0';
73         space = PATH_MAX - (ptr - path);
74
75         dir = opendir(path);
76         if (!dir)
77                 return NULL;
78
79         while((entry = readdir(dir))) {
80                 if (entry->d_name[0] == '.' &&
81                     (entry->d_name[1] == '\0' || (entry->d_name[1] == '.' &&
82                                                   entry->d_name[2] == '\0')))
83                         continue;
84
85                 strncpy(ptr, entry->d_name, space);
86                 if (lstat(path, &st) < 0)
87                         continue;
88
89                 if (S_ISDIR(st.st_mode)) {
90                         result = __lookup_dev(path, dev);
91                         if (result)
92                                 break;
93                 } else if (S_ISBLK(st.st_mode)) {
94                         if (st.st_rdev == dev) {
95                                 result = strdup(path);
96                                 break;
97                         }
98                 }
99         }
100
101         closedir(dir);
102
103         return result;
104 }
105
106 static char *lookup_dev(const char *dev)
107 {
108         uint32_t major, minor;
109         char buf[PATH_MAX + 1];
110
111         if (sscanf(dev, "%" PRIu32 ":%" PRIu32, &major, &minor) != 2)
112                 return NULL;
113
114         strncpy(buf, DEVICE_DIR, PATH_MAX);
115         buf[PATH_MAX] = '\0';
116
117         return __lookup_dev(buf, makedev(major, minor));
118 }
119
120 static int _dev_read_ahead(const char *dev, uint32_t *read_ahead)
121 {
122         int fd, r = 0;
123         long read_ahead_long;
124
125         if ((fd = open(dev, O_RDONLY)) < 0)
126                 return 0;
127
128         r = ioctl(fd, BLKRAGET, &read_ahead_long) ? 0 : 1;
129         close(fd);
130
131         if (r)
132                 *read_ahead = (uint32_t) read_ahead_long;
133
134         return r;
135 }
136
137 static char *get_params(struct crypt_options *options, const char *key)
138 {
139         char *params;
140         char *hexkey;
141         int i;
142
143         hexkey = safe_alloc(options->key_size * 2 + 1);
144         if (!hexkey) {
145                 set_error("Memory allocation problem");
146                 return NULL;
147         }
148
149         for(i = 0; i < options->key_size; i++)
150                 sprintf(&hexkey[i * 2], "%02x", (unsigned char)key[i]);
151
152         params = safe_alloc(strlen(hexkey) + strlen(options->cipher) +
153                             strlen(options->device) + 64);
154         if (!params) {
155                 set_error("Memory allocation problem");
156                 goto out;
157         }
158
159         sprintf(params, "%s %s %" PRIu64 " %s %" PRIu64,
160                 options->cipher, hexkey, options->skip,
161                 options->device, options->offset);
162
163 out:
164         safe_free(hexkey);
165
166         return params;
167 }
168
169 /* DM helpers */
170 static int _dm_simple(int task, const char *name)
171 {
172         int r = 0;
173         struct dm_task *dmt;
174
175         if (!(dmt = dm_task_create(task)))
176                 return 0;
177
178         if (!dm_task_set_name(dmt, name))
179                 goto out;
180
181         r = dm_task_run(dmt);
182
183       out:
184         dm_task_destroy(dmt);
185         return r;
186 }
187
188 static int _error_device(struct crypt_options *options)
189 {
190         struct dm_task *dmt;
191         int r = 0;
192
193         if (!(dmt = dm_task_create(DM_DEVICE_RELOAD)))
194                 return 0;
195
196         if (!dm_task_set_name(dmt, options->name))
197                 goto error;
198
199         if (!dm_task_add_target(dmt, UINT64_C(0), options->size, "error", ""))
200                 goto error;
201
202         if (!dm_task_set_ro(dmt))
203                 goto error;
204
205         if (!dm_task_no_open_count(dmt))
206                 goto error;
207
208         if (!dm_task_run(dmt))
209                 goto error;
210
211         if (!_dm_simple(DM_DEVICE_RESUME, options->name)) {
212                 _dm_simple(DM_DEVICE_CLEAR, options->name);
213                 goto error;
214         }
215
216         r = 1;
217
218 error:
219         dm_task_destroy(dmt);
220         return r;
221 }
222
223 static int _dm_remove(struct crypt_options *options, int force)
224 {
225         int r = -EINVAL;
226         int retries = force ? RETRY_COUNT : 1;
227
228         /* If force flag is set, replace device with error, read-only target.
229          * it should stop processes from reading it and also removed underlying
230          * device from mapping, so it is usable again.
231          * Force flag should be used only for temporary devices, which are
232          * intended to work inside cryptsetup only!
233          * Anyway, if some process try to read temporary cryptsetup device,
234          * it is bug - no other process should try touch it (e.g. udev).
235          */
236         if (force) {
237                  _error_device(options);
238                 retries = RETRY_COUNT;
239         }
240
241         do {
242                 r = _dm_simple(DM_DEVICE_REMOVE, options->name) ? 0 : -EINVAL;
243                 if (--retries && r)
244                         sleep(1);
245         } while (r == -EINVAL && retries);
246
247         dm_task_update_nodes();
248
249         return r;
250 }
251
252 static int dm_create_device(int reload, struct crypt_options *options,
253                             const char *key, const char *uuid)
254 {
255         struct dm_task *dmt = NULL;
256         struct dm_task *dmt_query = NULL;
257         struct dm_info dmi;
258         char *params = NULL;
259         char *error = NULL;
260         char dev_uuid[DM_UUID_PREFIX_LEN + DM_UUID_LEN + 1] = {0};
261         int r = -EINVAL;
262         uint32_t read_ahead = 0;
263
264         params = get_params(options, key);
265         if (!params)
266                 goto out_no_removal;
267  
268         if (uuid) {
269                 strncpy(dev_uuid, DM_UUID_PREFIX, DM_UUID_PREFIX_LEN);
270                 strncpy(dev_uuid + DM_UUID_PREFIX_LEN, uuid, DM_UUID_LEN);
271                 dev_uuid[DM_UUID_PREFIX_LEN + DM_UUID_LEN] = '\0';
272         }
273
274         if (!(dmt = dm_task_create(reload ? DM_DEVICE_RELOAD
275                                           : DM_DEVICE_CREATE)))
276                 goto out;
277         if (!dm_task_set_name(dmt, options->name))
278                 goto out;
279         if (options->flags & CRYPT_FLAG_READONLY && !dm_task_set_ro(dmt))
280                 goto out;
281         if (!dm_task_add_target(dmt, 0, options->size, DM_CRYPT_TARGET, params))
282                 goto out;
283
284 #ifdef DM_READ_AHEAD_MINIMUM_FLAG
285         if (_dev_read_ahead(options->device, &read_ahead) &&
286             !dm_task_set_read_ahead(dmt, read_ahead, DM_READ_AHEAD_MINIMUM_FLAG))
287                 goto out;
288 #endif
289
290         if (uuid && !dm_task_set_uuid(dmt, dev_uuid))
291                 goto out;
292
293         if (!dm_task_run(dmt))
294                 goto out;
295
296         if (reload) {
297                 dm_task_destroy(dmt);
298                 if (!(dmt = dm_task_create(DM_DEVICE_RESUME)))
299                         goto out;
300                 if (!dm_task_set_name(dmt, options->name))
301                         goto out;
302                 if (uuid && !dm_task_set_uuid(dmt, dev_uuid))
303                         goto out;
304                 if (!dm_task_run(dmt))
305                         goto out;
306         }
307
308         if (!dm_task_get_info(dmt, &dmi))
309                 goto out;
310         if (dmi.read_only)
311                 options->flags |= CRYPT_FLAG_READONLY;
312
313         r = 0;
314 out:
315         if (r < 0 && !reload) {
316                 if (get_error())
317                         error = strdup(get_error());
318
319                 _dm_remove(options, 0);
320
321                 if (error) {
322                         set_error(error);
323                         free(error);
324                 }
325         }
326
327 out_no_removal:
328         if (params)
329                 safe_free(params);
330         if (dmt)
331                 dm_task_destroy(dmt);
332         if(dmt_query)
333                 dm_task_destroy(dmt_query);
334         dm_task_update_nodes();
335         return r;
336 }
337
338 static int dm_query_device(int details, struct crypt_options *options,
339                            char **key)
340 {
341         struct dm_task *dmt;
342         struct dm_info dmi;
343         uint64_t start, length;
344         char *target_type, *params;
345         void *next = NULL;
346         int r = -EINVAL;
347
348         if (!(dmt = dm_task_create(details ? DM_DEVICE_TABLE
349                                            : DM_DEVICE_STATUS)))
350                 goto out;
351         if (!dm_task_set_name(dmt, options->name))
352                 goto out;
353         r = -ENODEV;
354         if (!dm_task_run(dmt))
355                 goto out;
356
357         r = -EINVAL;
358         if (!dm_task_get_info(dmt, &dmi))
359                 goto out;
360
361         if (!dmi.exists) {
362                 r = -ENODEV;
363                 goto out;
364         }
365
366         next = dm_get_next_target(dmt, next, &start, &length,
367                                   &target_type, &params);
368         if (!target_type || strcmp(target_type, DM_CRYPT_TARGET) != 0 ||
369             start != 0 || next)
370                 goto out;
371
372         options->hash = NULL;
373         options->cipher = NULL;
374         options->offset = 0;
375         options->skip = 0;
376         options->size = length;
377         if (details) {
378                 char *cipher, *key_, *device;
379                 uint64_t val64;
380
381                 set_error("Invalid dm table");
382
383                 cipher = strsep(&params, " ");
384                 key_ = strsep(&params, " ");
385                 if (!params)
386                         goto out;
387
388                 val64 = strtoull(params, &params, 10);
389                 if (*params != ' ')
390                         goto out;
391                 params++;
392                 options->skip = val64;
393
394                 device = strsep(&params, " ");
395                 if (!params)
396                         goto out;
397
398                 val64 = strtoull(params, &params, 10);
399                 if (*params)
400                         goto out;
401                 options->offset = val64;
402
403                 options->cipher = strdup(cipher);
404                 options->key_size = strlen(key_) / 2;
405                 if (key) {
406                         char buffer[3];
407                         char *endp;
408                         int i;
409
410                         *key = safe_alloc(options->key_size);
411                         if (!*key) {
412                                 set_error("Out of memory");
413                                 r = -ENOMEM;
414                                 goto out;
415                         }
416
417                         buffer[2] = '\0';
418                         for(i = 0; i < options->key_size; i++) {
419                                 memcpy(buffer, &key_[i * 2], 2);
420                                 (*key)[i] = strtoul(buffer, &endp, 16);
421                                 if (endp != &buffer[2]) {
422                                         safe_free(key);
423                                         *key = NULL;
424                                         goto out;
425                                 }
426                         }
427                 }
428                 memset(key_, 0, strlen(key_));
429                 options->device = lookup_dev(device);
430
431                 set_error(NULL);
432         }
433
434         r = (dmi.open_count > 0);
435
436 out:
437         if (dmt)
438                 dm_task_destroy(dmt);
439         if (r >= 0) {
440                 if (options->device)
441                         options->flags |= CRYPT_FLAG_FREE_DEVICE;
442                 if (options->cipher)
443                         options->flags |= CRYPT_FLAG_FREE_CIPHER;
444                 options->flags &= ~CRYPT_FLAG_READONLY;
445                 if (dmi.read_only)
446                         options->flags |= CRYPT_FLAG_READONLY;
447         } else {
448                 if (options->device) {
449                         free((char *)options->device);
450                         options->device = NULL;
451                         options->flags &= ~CRYPT_FLAG_FREE_DEVICE;
452                 }
453                 if (options->cipher) {
454                         free((char *)options->cipher);
455                         options->cipher = NULL;
456                         options->flags &= ~CRYPT_FLAG_FREE_CIPHER;
457                 }
458         }
459         return r;
460 }
461
462 static int dm_remove_device(int force, struct crypt_options *options)
463 {
464         if (!options || !options->name)
465                 return -EINVAL;
466
467         return _dm_remove(options, force);;
468 }
469
470
471 static const char *dm_get_dir(void)
472 {
473         return dm_dir();
474 }
475
476 struct setup_backend setup_libdevmapper_backend = {
477         .name = "dm-crypt",
478         .init = dm_init,
479         .exit = dm_exit,
480         .create = dm_create_device,
481         .status = dm_query_device,
482         .remove = dm_remove_device,
483         .dir = dm_get_dir
484 };