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