46c2a0fed81441b6af6cbaddd6d5e41fac4de736
[platform/upstream/cryptsetup.git] / lib / utils_device.c
1 /*
2  * device backend utilities
3  *
4  * Copyright (C) 2004, Jana Saout <jana@saout.de>
5  * Copyright (C) 2004-2007, Clemens Fruhwirth <clemens@endorphin.org>
6  * Copyright (C) 2009-2015, Red Hat, Inc. All rights reserved.
7  * Copyright (C) 2009-2015, Milan Broz
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 #include <string.h>
25 #include <stdlib.h>
26 #include <fcntl.h>
27 #include <errno.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <sys/ioctl.h>
31 #include <linux/fs.h>
32 #include <unistd.h>
33 #include "internal.h"
34
35 struct device {
36         char *path;
37
38         char *file_path;
39         int loop_fd;
40
41         int o_direct:1;
42         int init_done:1;
43 };
44
45 static int device_block_size_fd(int fd, size_t *min_size)
46 {
47         struct stat st;
48         int bsize = 0, r = -EINVAL;
49
50         if (fstat(fd, &st) < 0)
51                 return -EINVAL;
52
53         if (S_ISREG(st.st_mode))
54                 r = (int)crypt_getpagesize();
55         else if (ioctl(fd, BLKSSZGET, &bsize) >= 0)
56                 r = bsize;
57         else
58                 r = -EINVAL;
59
60         if (r < 0 || !min_size)
61                 return r;
62
63         if (S_ISREG(st.st_mode)) {
64                 /* file can be empty as well */
65                 if (st.st_size > bsize)
66                         *min_size = bsize;
67                 else
68                         *min_size = st.st_size;
69         } else {
70                 /* block device must have at least one block */
71                 *min_size = bsize;
72         }
73
74         return bsize;
75 }
76
77 static int device_read_test(int devfd)
78 {
79         char buffer[512];
80         int blocksize, r = -EIO;
81         size_t minsize = 0;
82
83         blocksize = device_block_size_fd(devfd, &minsize);
84
85         if (blocksize < 0)
86                 return -EINVAL;
87
88         if (minsize == 0)
89                 return 0;
90
91         if (minsize > sizeof(buffer))
92                 minsize = sizeof(buffer);
93
94         if (read_blockwise(devfd, blocksize, buffer, minsize) == (ssize_t)minsize)
95                 r = 0;
96
97         crypt_memzero(buffer, sizeof(buffer));
98         return r;
99 }
100
101 /*
102  * The direct-io is always preferred. The header is usually mapped to the same
103  * device and can be accessed when the rest of device is mapped to data device.
104  * Using dirct-io encsures that we do not mess with data in cache.
105  * (But proper alignment should prevent this in the first place.)
106  * The read test is needed to detect broken configurations (seen with remote
107  * block devices) that allow open with direct-io but then fails on read.
108  */
109 static int device_ready(struct device *device, int check_directio)
110 {
111         int devfd = -1, r = 0;
112         struct stat st;
113
114         device->o_direct = 0;
115         if (check_directio) {
116                 log_dbg("Trying to open and read device %s with direct-io.",
117                         device_path(device));
118                 devfd = open(device_path(device), O_RDONLY | O_DIRECT);
119                 if (devfd >= 0) {
120                         if (device_read_test(devfd) == 0) {
121                                 device->o_direct = 1;
122                         } else {
123                                 close(devfd);
124                                 devfd = -1;
125                         }
126                 }
127         }
128
129         if (devfd < 0) {
130                 log_dbg("Trying to open device %s without direct-io.",
131                         device_path(device));
132                 devfd = open(device_path(device), O_RDONLY);
133         }
134
135         if (devfd < 0) {
136                 log_err(NULL, _("Device %s doesn't exist or access denied.\n"),
137                         device_path(device));
138                 return -EINVAL;
139         }
140
141         if (fstat(devfd, &st) < 0)
142                 r = -EINVAL;
143         else if (!S_ISBLK(st.st_mode))
144                 r = S_ISREG(st.st_mode) ? -ENOTBLK : -EINVAL;
145
146         close(devfd);
147         return r;
148 }
149
150 int device_open(struct device *device, int flags)
151 {
152         int devfd;
153
154         flags |= O_SYNC;
155         if (device->o_direct)
156                 flags |= O_DIRECT;
157
158         devfd = open(device_path(device), flags);
159
160         if (devfd < 0)
161                 log_dbg("Cannot open device %s.", device_path(device));
162
163         return devfd;
164 }
165
166 int device_alloc(struct device **device, const char *path)
167 {
168         struct device *dev;
169         int r;
170
171         if (!path) {
172                 *device = NULL;
173                 return 0;
174         }
175
176         dev = malloc(sizeof(struct device));
177         if (!dev)
178                 return -ENOMEM;
179
180         memset(dev, 0, sizeof(struct device));
181         dev->path = strdup(path);
182         if (!dev->path) {
183                 free(dev);
184                 return -ENOMEM;
185         }
186         dev->loop_fd = -1;
187
188         r = device_ready(dev, 1);
189         if (!r) {
190                 dev->init_done = 1;
191         } else if (r == -ENOTBLK) {
192                 /* alloc loop later */
193         } else if (r < 0) {
194                 free(dev->path);
195                 free(dev);
196                 return -ENOTBLK;
197         }
198
199         *device = dev;
200         return 0;
201 }
202
203 void device_free(struct device *device)
204 {
205         if (!device)
206                 return;
207
208         if (device->loop_fd != -1) {
209                 log_dbg("Closed loop %s (%s).", device->path, device->file_path);
210                 close(device->loop_fd);
211         }
212
213         free(device->file_path);
214         free(device->path);
215         free(device);
216 }
217
218 /* Get block device path */
219 const char *device_block_path(const struct device *device)
220 {
221         if (!device || !device->init_done)
222                 return NULL;
223
224         return device->path;
225 }
226
227 /* Get path to device / file */
228 const char *device_path(const struct device *device)
229 {
230         if (!device)
231                 return NULL;
232
233         if (device->file_path)
234                 return device->file_path;
235
236         return device->path;
237 }
238
239 /* block device topology ioctls, introduced in 2.6.32 */
240 #ifndef BLKIOMIN
241 #define BLKIOMIN    _IO(0x12,120)
242 #define BLKIOOPT    _IO(0x12,121)
243 #define BLKALIGNOFF _IO(0x12,122)
244 #endif
245
246 void device_topology_alignment(struct device *device,
247                             unsigned long *required_alignment, /* bytes */
248                             unsigned long *alignment_offset,   /* bytes */
249                             unsigned long default_alignment)
250 {
251         int dev_alignment_offset = 0;
252         unsigned int min_io_size = 0, opt_io_size = 0;
253         unsigned long temp_alignment = 0;
254         int fd;
255
256         *required_alignment = default_alignment;
257         *alignment_offset = 0;
258
259         if (!device || !device->path) //FIXME
260                 return;
261
262         fd = open(device->path, O_RDONLY);
263         if (fd == -1)
264                 return;
265
266         /* minimum io size */
267         if (ioctl(fd, BLKIOMIN, &min_io_size) == -1) {
268                 log_dbg("Topology info for %s not supported, using default offset %lu bytes.",
269                         device->path, default_alignment);
270                 goto out;
271         }
272
273         /* optimal io size */
274         if (ioctl(fd, BLKIOOPT, &opt_io_size) == -1)
275                 opt_io_size = min_io_size;
276
277         /* alignment offset, bogus -1 means misaligned/unknown */
278         if (ioctl(fd, BLKALIGNOFF, &dev_alignment_offset) == -1 || dev_alignment_offset < 0)
279                 dev_alignment_offset = 0;
280         *alignment_offset = (unsigned long)dev_alignment_offset;
281
282         temp_alignment = (unsigned long)min_io_size;
283
284         if (temp_alignment < (unsigned long)opt_io_size)
285                 temp_alignment = (unsigned long)opt_io_size;
286
287         /* If calculated alignment is multiple of default, keep default */
288         if (temp_alignment && (default_alignment % temp_alignment))
289                 *required_alignment = temp_alignment;
290
291         log_dbg("Topology: IO (%u/%u), offset = %lu; Required alignment is %lu bytes.",
292                 min_io_size, opt_io_size, *alignment_offset, *required_alignment);
293 out:
294         (void)close(fd);
295 }
296
297 int device_block_size(struct device *device)
298 {
299         int fd, r = -EINVAL;
300
301         if (!device)
302                 return 0;
303
304         if (device->file_path)
305                 return (int)crypt_getpagesize();
306
307         fd = open(device->path, O_RDONLY);
308         if(fd < 0)
309                 return -EINVAL;
310
311         r = device_block_size_fd(fd, NULL);
312
313         if (r <= 0)
314                 log_dbg("Cannot get block size for device %s.", device_path(device));
315
316         close(fd);
317         return r;
318 }
319
320 int device_read_ahead(struct device *device, uint32_t *read_ahead)
321 {
322         int fd, r = 0;
323         long read_ahead_long;
324
325         if (!device)
326                 return 0;
327
328         if ((fd = open(device->path, O_RDONLY)) < 0)
329                 return 0;
330
331         r = ioctl(fd, BLKRAGET, &read_ahead_long) ? 0 : 1;
332         close(fd);
333
334         if (r)
335                 *read_ahead = (uint32_t) read_ahead_long;
336
337         return r;
338 }
339
340 /* Get data size in bytes */
341 int device_size(struct device *device, uint64_t *size)
342 {
343         struct stat st;
344         int devfd, r = -EINVAL;
345
346         devfd = open(device->path, O_RDONLY);
347         if(devfd == -1)
348                 return -EINVAL;
349
350         if (fstat(devfd, &st) < 0)
351                 goto out;
352
353         if (S_ISREG(st.st_mode)) {
354                 *size = (uint64_t)st.st_size;
355                 r = 0;
356         } else if (ioctl(devfd, BLKGETSIZE64, size) >= 0)
357                 r = 0;
358 out:
359         close(devfd);
360         return r;
361 }
362
363 static int device_info(struct device *device,
364                         enum devcheck device_check,
365                         int *readonly, uint64_t *size)
366 {
367         struct stat st;
368         int fd, r = -EINVAL, flags = 0;
369
370         *readonly = 0;
371         *size = 0;
372
373         if (stat(device->path, &st) < 0)
374                 return -EINVAL;
375
376         /* never wipe header on mounted device */
377         if (device_check == DEV_EXCL && S_ISBLK(st.st_mode))
378                 flags |= O_EXCL;
379
380         /* Try to open read-write to check whether it is a read-only device */
381         /* coverity[toctou] */
382         fd = open(device->path, O_RDWR | flags);
383         if (fd == -1 && errno == EROFS) {
384                 *readonly = 1;
385                 fd = open(device->path, O_RDONLY | flags);
386         }
387
388         if (fd == -1 && device_check == DEV_EXCL && errno == EBUSY)
389                 return -EBUSY;
390
391         if (fd == -1)
392                 return -EINVAL;
393
394         if (S_ISREG(st.st_mode)) {
395                 //FIXME: add readonly check
396                 *size = (uint64_t)st.st_size;
397                 *size >>= SECTOR_SHIFT;
398         } else {
399                 /* If the device can be opened read-write, i.e. readonly is still 0, then
400                  * check whether BKROGET says that it is read-only. E.g. read-only loop
401                  * devices may be openend read-write but are read-only according to BLKROGET
402                  */
403                 if (*readonly == 0 && (r = ioctl(fd, BLKROGET, readonly)) < 0)
404                         goto out;
405
406                 if (ioctl(fd, BLKGETSIZE64, size) >= 0) {
407                         *size >>= SECTOR_SHIFT;
408                         r = 0;
409                         goto out;
410                 }
411         }
412         r = -EINVAL;
413 out:
414         close(fd);
415         return r;
416 }
417
418 static int device_internal_prepare(struct crypt_device *cd, struct device *device)
419 {
420         char *loop_device, *file_path = NULL;
421         int r, loop_fd, readonly = 0;
422
423         if (device->init_done)
424                 return 0;
425
426         log_dbg("Allocating a free loop device.");
427         loop_device = crypt_loop_get_device();
428         if (!loop_device) {
429                 if (getuid() || geteuid())
430                         log_err(cd, _("Cannot use a loopback device, "
431                                       "running as non-root user.\n"));
432                 else
433                         log_err(cd, _("Cannot find a free loopback device.\n"));
434                 return -ENOTSUP;
435         }
436
437         /* Keep the loop open, dettached on last close. */
438         loop_fd = crypt_loop_attach(loop_device, device->path, 0, 1, &readonly);
439         if (loop_fd == -1) {
440                 log_err(cd, _("Attaching loopback device failed "
441                         "(loop device with autoclear flag is required).\n"));
442                 free(loop_device);
443                 return -EINVAL;
444         }
445
446         file_path = device->path;
447         device->path = loop_device;
448
449         r = device_ready(device, device->o_direct);
450         if (r < 0) {
451                 device->path = file_path;
452                 crypt_loop_detach(loop_device);
453                 free(loop_device);
454                 return r;
455         }
456
457         device->loop_fd = loop_fd;
458         device->file_path = file_path;
459         device->init_done = 1;
460
461         return 0;
462 }
463
464 int device_block_adjust(struct crypt_device *cd,
465                         struct device *device,
466                         enum devcheck device_check,
467                         uint64_t device_offset,
468                         uint64_t *size,
469                         uint32_t *flags)
470 {
471         int r, real_readonly;
472         uint64_t real_size;
473
474         if (!device)
475                 return -ENOTBLK;
476
477         r = device_internal_prepare(cd, device);
478         if (r)
479                 return r;
480
481         r = device_info(device, device_check, &real_readonly, &real_size);
482         if (r < 0) {
483                 if (r == -EBUSY)
484                         log_err(cd, _("Cannot use device %s which is in use "
485                                       "(already mapped or mounted).\n"),
486                                       device->path);
487                 else
488                         log_err(cd, _("Cannot get info about device %s.\n"),
489                                 device->path);
490                 return r;
491         }
492
493         if (device_offset >= real_size) {
494                 log_err(cd, _("Requested offset is beyond real size of device %s.\n"),
495                         device->path);
496                 return -EINVAL;
497         }
498
499         if (size && !*size) {
500                 *size = real_size;
501                 if (!*size) {
502                         log_err(cd, _("Device %s has zero size.\n"), device->path);
503                         return -ENOTBLK;
504                 }
505                 *size -= device_offset;
506         }
507
508         /* in case of size is set by parameter */
509         if (size && ((real_size - device_offset) < *size)) {
510                 log_dbg("Device %s: offset = %" PRIu64 " requested size = %" PRIu64
511                         ", backing device size = %" PRIu64,
512                         device->path, device_offset, *size, real_size);
513                 log_err(cd, _("Device %s is too small.\n"), device->path);
514                 return -EINVAL;
515         }
516
517         if (flags && real_readonly)
518                 *flags |= CRYPT_ACTIVATE_READONLY;
519
520         if (size)
521                 log_dbg("Calculated device size is %" PRIu64" sectors (%s), offset %" PRIu64 ".",
522                 *size, real_readonly ? "RO" : "RW", device_offset);
523         return 0;
524 }
525
526 size_t size_round_up(size_t size, unsigned int block)
527 {
528         size_t s = (size + (block - 1)) / block;
529         return s * block;
530 }