2 * device backend utilities
4 * Copyright (C) 2004, Christophe Saout <christophe@saout.de>
5 * Copyright (C) 2004-2007, Clemens Fruhwirth <clemens@endorphin.org>
6 * Copyright (C) 2009-2012, Red Hat, Inc. All rights reserved.
7 * Copyright (C) 2009-2012, 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>
44 static int device_ready(const char *device)
49 log_dbg("Trying to open and read device %s.", device);
50 devfd = open(device, O_RDONLY | O_DIRECT | O_SYNC);
52 log_err(NULL, _("Device %s doesn't exist or access denied.\n"), device);
55 if (fstat(devfd, &st) < 0)
57 else if (!S_ISBLK(st.st_mode))
58 r = S_ISREG(st.st_mode) ? -ENOTBLK : -EINVAL;
64 int device_alloc(struct device **device, const char *path)
74 dev = malloc(sizeof(struct device));
78 memset(dev, 0, sizeof(struct device));
81 r = device_ready(path);
84 } else if (r == -ENOTBLK) {
85 /* alloc loop later */
91 dev->path = strdup(path);
101 void device_free(struct device *device)
106 if (device->loop_fd != -1) {
107 log_dbg("Closed loop %s (%s).", device->path, device->file_path);
108 close(device->loop_fd);
111 free(device->file_path);
116 /* Get block device path */
117 const char *device_block_path(const struct device *device)
119 if (!device || !device->init_done)
125 /* Get path to device / file */
126 const char *device_path(const struct device *device)
131 if (device->file_path)
132 return device->file_path;
137 /* block device topology ioctls, introduced in 2.6.32 */
139 #define BLKIOMIN _IO(0x12,120)
140 #define BLKIOOPT _IO(0x12,121)
141 #define BLKALIGNOFF _IO(0x12,122)
144 void device_topology_alignment(struct device *device,
145 unsigned long *required_alignment, /* bytes */
146 unsigned long *alignment_offset, /* bytes */
147 unsigned long default_alignment)
149 int dev_alignment_offset = 0;
150 unsigned int min_io_size = 0, opt_io_size = 0;
151 unsigned long temp_alignment = 0;
154 *required_alignment = default_alignment;
155 *alignment_offset = 0;
157 if (!device || !device->path) //FIXME
160 fd = open(device->path, O_RDONLY);
164 /* minimum io size */
165 if (ioctl(fd, BLKIOMIN, &min_io_size) == -1) {
166 log_dbg("Topology info for %s not supported, using default offset %lu bytes.",
167 device->path, default_alignment);
171 /* optimal io size */
172 if (ioctl(fd, BLKIOOPT, &opt_io_size) == -1)
173 opt_io_size = min_io_size;
175 /* alignment offset, bogus -1 means misaligned/unknown */
176 if (ioctl(fd, BLKALIGNOFF, &dev_alignment_offset) == -1 || dev_alignment_offset < 0)
177 dev_alignment_offset = 0;
178 *alignment_offset = (unsigned long)dev_alignment_offset;
180 temp_alignment = (unsigned long)min_io_size;
182 if (temp_alignment < (unsigned long)opt_io_size)
183 temp_alignment = (unsigned long)opt_io_size;
185 /* If calculated alignment is multiple of default, keep default */
186 if (temp_alignment && (default_alignment % temp_alignment))
187 *required_alignment = temp_alignment;
189 log_dbg("Topology: IO (%u/%u), offset = %lu; Required alignment is %lu bytes.",
190 min_io_size, opt_io_size, *alignment_offset, *required_alignment);
195 int device_block_size(struct device *device)
198 int fd, bsize = 0, r = -EINVAL;
203 fd = open(device->path, O_RDONLY);
207 if (fstat(fd, &st) < 0)
210 if (S_ISREG(st.st_mode)) {
211 r = (int)crypt_getpagesize();
215 if (ioctl(fd, BLKSSZGET, &bsize) >= 0)
222 int device_read_ahead(struct device *device, uint32_t *read_ahead)
225 long read_ahead_long;
230 if ((fd = open(device->path, O_RDONLY)) < 0)
233 r = ioctl(fd, BLKRAGET, &read_ahead_long) ? 0 : 1;
237 *read_ahead = (uint32_t) read_ahead_long;
242 /* Get data size in bytes */
243 int device_size(struct device *device, uint64_t *size)
246 int devfd, r = -EINVAL;
248 devfd = open(device->path, O_RDONLY);
252 if (fstat(devfd, &st) < 0)
255 if (S_ISREG(st.st_mode)) {
256 *size = (uint64_t)st.st_size;
258 } else if (ioctl(devfd, BLKGETSIZE64, size) >= 0)
265 static int device_info(struct device *device,
266 enum devcheck device_check,
267 int *readonly, uint64_t *size)
270 int fd, r = -EINVAL, flags = 0;
275 if (stat(device->path, &st) < 0)
278 /* never wipe header on mounted device */
279 if (device_check == DEV_EXCL && S_ISBLK(st.st_mode))
282 /* Try to open read-write to check whether it is a read-only device */
283 /* coverity[toctou] */
284 fd = open(device->path, O_RDWR | flags);
285 if (fd == -1 && errno == EROFS) {
287 fd = open(device->path, O_RDONLY | flags);
290 if (fd == -1 && device_check == DEV_EXCL && errno == EBUSY)
296 if (S_ISREG(st.st_mode)) {
297 //FIXME: add readonly check
298 *size = (uint64_t)st.st_size;
299 *size >>= SECTOR_SHIFT;
301 /* If the device can be opened read-write, i.e. readonly is still 0, then
302 * check whether BKROGET says that it is read-only. E.g. read-only loop
303 * devices may be openend read-write but are read-only according to BLKROGET
305 if (*readonly == 0 && (r = ioctl(fd, BLKROGET, readonly)) < 0)
308 if (ioctl(fd, BLKGETSIZE64, size) >= 0) {
309 *size >>= SECTOR_SHIFT;
320 static int device_internal_prepare(struct crypt_device *cd, struct device *device)
323 int r, loop_fd, readonly = 0;
325 if (device->init_done)
328 log_dbg("Allocating a free loop device.");
329 loop_device = crypt_loop_get_device();
331 if (getuid() || geteuid())
332 log_err(cd, _("Cannot use a loopback device, "
333 "running as non-root user.\n"));
335 log_err(cd, _("Cannot find a free loopback device.\n"));
339 /* Keep the loop open, dettached on last close. */
340 loop_fd = crypt_loop_attach(loop_device, device->path, 0, 1, &readonly);
342 log_err(cd, _("Attaching loopback device failed "
343 "(loop device with autoclear flag is required).\n"));
348 r = device_ready(loop_device);
354 device->loop_fd = loop_fd;
355 device->file_path = device->path;
356 device->path = loop_device;
357 device->init_done = 1;
362 int device_block_adjust(struct crypt_device *cd,
363 struct device *device,
364 enum devcheck device_check,
365 uint64_t device_offset,
369 int r, real_readonly;
375 r = device_internal_prepare(cd, device);
379 r = device_info(device, device_check, &real_readonly, &real_size);
382 log_err(cd, _("Cannot use device %s which is in use "
383 "(already mapped or mounted).\n"),
386 log_err(cd, _("Cannot get info about device %s.\n"),
391 if (device_offset >= real_size) {
392 log_err(cd, _("Requested offset is beyond real size of device %s.\n"),
397 if (size && !*size) {
400 log_err(cd, _("Device %s has zero size.\n"), device->path);
403 *size -= device_offset;
406 /* in case of size is set by parameter */
407 if (size && ((real_size - device_offset) < *size)) {
408 log_dbg("Device %s: offset = %" PRIu64 " requested size = %" PRIu64
409 ", backing device size = %" PRIu64,
410 device->path, device_offset, *size, real_size);
411 log_err(cd, _("Device %s is too small.\n"), device->path);
415 if (flags && real_readonly)
416 *flags |= CRYPT_ACTIVATE_READONLY;
419 log_dbg("Calculated device size is %" PRIu64" sectors (%s), offset %" PRIu64 ".",
420 *size, real_readonly ? "RO" : "RW", device_offset);
424 size_t size_round_up(size_t size, unsigned int block)
426 size_t s = (size + (block - 1)) / block;