2 * device backend utilities
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
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.
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.
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.
28 #include <sys/types.h>
30 #include <sys/ioctl.h>
45 static int device_block_size_fd(int fd, size_t *min_size)
48 int bsize = 0, r = -EINVAL;
50 if (fstat(fd, &st) < 0)
53 if (S_ISREG(st.st_mode))
54 r = (int)crypt_getpagesize();
55 else if (ioctl(fd, BLKSSZGET, &bsize) >= 0)
60 if (r < 0 || !min_size)
63 if (S_ISREG(st.st_mode)) {
64 /* file can be empty as well */
65 if (st.st_size > bsize)
68 *min_size = st.st_size;
70 /* block device must have at least one block */
77 static int device_read_test(int devfd)
80 int blocksize, r = -EIO;
83 blocksize = device_block_size_fd(devfd, &minsize);
91 if (minsize > sizeof(buffer))
92 minsize = sizeof(buffer);
94 if (read_blockwise(devfd, blocksize, buffer, minsize) == (ssize_t)minsize)
97 crypt_memzero(buffer, sizeof(buffer));
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.
109 static int device_ready(struct device *device, int check_directio)
111 int devfd = -1, r = 0;
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);
120 if (device_read_test(devfd) == 0) {
121 device->o_direct = 1;
130 log_dbg("Trying to open device %s without direct-io.",
131 device_path(device));
132 devfd = open(device_path(device), O_RDONLY);
136 log_err(NULL, _("Device %s doesn't exist or access denied.\n"),
137 device_path(device));
141 if (fstat(devfd, &st) < 0)
143 else if (!S_ISBLK(st.st_mode))
144 r = S_ISREG(st.st_mode) ? -ENOTBLK : -EINVAL;
150 int device_open(struct device *device, int flags)
155 if (device->o_direct)
158 devfd = open(device_path(device), flags);
161 log_dbg("Cannot open device %s.", device_path(device));
166 int device_alloc(struct device **device, const char *path)
176 dev = malloc(sizeof(struct device));
180 memset(dev, 0, sizeof(struct device));
181 dev->path = strdup(path);
188 r = device_ready(dev, 1);
191 } else if (r == -ENOTBLK) {
192 /* alloc loop later */
203 void device_free(struct device *device)
208 if (device->loop_fd != -1) {
209 log_dbg("Closed loop %s (%s).", device->path, device->file_path);
210 close(device->loop_fd);
213 free(device->file_path);
218 /* Get block device path */
219 const char *device_block_path(const struct device *device)
221 if (!device || !device->init_done)
227 /* Get path to device / file */
228 const char *device_path(const struct device *device)
233 if (device->file_path)
234 return device->file_path;
239 /* block device topology ioctls, introduced in 2.6.32 */
241 #define BLKIOMIN _IO(0x12,120)
242 #define BLKIOOPT _IO(0x12,121)
243 #define BLKALIGNOFF _IO(0x12,122)
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)
251 int dev_alignment_offset = 0;
252 unsigned int min_io_size = 0, opt_io_size = 0;
253 unsigned long temp_alignment = 0;
256 *required_alignment = default_alignment;
257 *alignment_offset = 0;
259 if (!device || !device->path) //FIXME
262 fd = open(device->path, O_RDONLY);
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);
273 /* optimal io size */
274 if (ioctl(fd, BLKIOOPT, &opt_io_size) == -1)
275 opt_io_size = min_io_size;
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;
282 temp_alignment = (unsigned long)min_io_size;
284 if (temp_alignment < (unsigned long)opt_io_size)
285 temp_alignment = (unsigned long)opt_io_size;
287 /* If calculated alignment is multiple of default, keep default */
288 if (temp_alignment && (default_alignment % temp_alignment))
289 *required_alignment = temp_alignment;
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);
297 int device_block_size(struct device *device)
304 if (device->file_path)
305 return (int)crypt_getpagesize();
307 fd = open(device->path, O_RDONLY);
311 r = device_block_size_fd(fd, NULL);
314 log_dbg("Cannot get block size for device %s.", device_path(device));
320 int device_read_ahead(struct device *device, uint32_t *read_ahead)
323 long read_ahead_long;
328 if ((fd = open(device->path, O_RDONLY)) < 0)
331 r = ioctl(fd, BLKRAGET, &read_ahead_long) ? 0 : 1;
335 *read_ahead = (uint32_t) read_ahead_long;
340 /* Get data size in bytes */
341 int device_size(struct device *device, uint64_t *size)
344 int devfd, r = -EINVAL;
346 devfd = open(device->path, O_RDONLY);
350 if (fstat(devfd, &st) < 0)
353 if (S_ISREG(st.st_mode)) {
354 *size = (uint64_t)st.st_size;
356 } else if (ioctl(devfd, BLKGETSIZE64, size) >= 0)
363 static int device_info(struct device *device,
364 enum devcheck device_check,
365 int *readonly, uint64_t *size)
368 int fd, r = -EINVAL, flags = 0;
373 if (stat(device->path, &st) < 0)
376 /* never wipe header on mounted device */
377 if (device_check == DEV_EXCL && S_ISBLK(st.st_mode))
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) {
385 fd = open(device->path, O_RDONLY | flags);
388 if (fd == -1 && device_check == DEV_EXCL && errno == EBUSY)
394 if (S_ISREG(st.st_mode)) {
395 //FIXME: add readonly check
396 *size = (uint64_t)st.st_size;
397 *size >>= SECTOR_SHIFT;
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
403 if (*readonly == 0 && (r = ioctl(fd, BLKROGET, readonly)) < 0)
406 if (ioctl(fd, BLKGETSIZE64, size) >= 0) {
407 *size >>= SECTOR_SHIFT;
418 static int device_internal_prepare(struct crypt_device *cd, struct device *device)
420 char *loop_device, *file_path = NULL;
421 int r, loop_fd, readonly = 0;
423 if (device->init_done)
426 log_dbg("Allocating a free loop device.");
427 loop_device = crypt_loop_get_device();
429 if (getuid() || geteuid())
430 log_err(cd, _("Cannot use a loopback device, "
431 "running as non-root user.\n"));
433 log_err(cd, _("Cannot find a free loopback device.\n"));
437 /* Keep the loop open, dettached on last close. */
438 loop_fd = crypt_loop_attach(loop_device, device->path, 0, 1, &readonly);
440 log_err(cd, _("Attaching loopback device failed "
441 "(loop device with autoclear flag is required).\n"));
446 file_path = device->path;
447 device->path = loop_device;
449 r = device_ready(device, device->o_direct);
451 device->path = file_path;
452 crypt_loop_detach(loop_device);
457 device->loop_fd = loop_fd;
458 device->file_path = file_path;
459 device->init_done = 1;
464 int device_block_adjust(struct crypt_device *cd,
465 struct device *device,
466 enum devcheck device_check,
467 uint64_t device_offset,
471 int r, real_readonly;
477 r = device_internal_prepare(cd, device);
481 r = device_info(device, device_check, &real_readonly, &real_size);
484 log_err(cd, _("Cannot use device %s which is in use "
485 "(already mapped or mounted).\n"),
488 log_err(cd, _("Cannot get info about device %s.\n"),
493 if (device_offset >= real_size) {
494 log_err(cd, _("Requested offset is beyond real size of device %s.\n"),
499 if (size && !*size) {
502 log_err(cd, _("Device %s has zero size.\n"), device->path);
505 *size -= device_offset;
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);
517 if (flags && real_readonly)
518 *flags |= CRYPT_ACTIVATE_READONLY;
521 log_dbg("Calculated device size is %" PRIu64" sectors (%s), offset %" PRIu64 ".",
522 *size, real_readonly ? "RO" : "RW", device_offset);
526 size_t size_round_up(size_t size, unsigned int block)
528 size_t s = (size + (block - 1)) / block;