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);
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_open(struct device *device, int flags)
68 devfd = open(device_path(device), flags | O_DIRECT | O_SYNC);
69 if (devfd < 0 && errno == EINVAL) {
70 log_dbg("Trying to open device %s without direct-io.",
72 devfd = open(device_path(device), flags | O_SYNC);
78 int device_alloc(struct device **device, const char *path)
88 dev = malloc(sizeof(struct device));
92 memset(dev, 0, sizeof(struct device));
95 r = device_ready(path);
98 } else if (r == -ENOTBLK) {
99 /* alloc loop later */
105 dev->path = strdup(path);
115 void device_free(struct device *device)
120 if (device->loop_fd != -1) {
121 log_dbg("Closed loop %s (%s).", device->path, device->file_path);
122 close(device->loop_fd);
125 free(device->file_path);
130 /* Get block device path */
131 const char *device_block_path(const struct device *device)
133 if (!device || !device->init_done)
139 /* Get path to device / file */
140 const char *device_path(const struct device *device)
145 if (device->file_path)
146 return device->file_path;
151 /* block device topology ioctls, introduced in 2.6.32 */
153 #define BLKIOMIN _IO(0x12,120)
154 #define BLKIOOPT _IO(0x12,121)
155 #define BLKALIGNOFF _IO(0x12,122)
158 void device_topology_alignment(struct device *device,
159 unsigned long *required_alignment, /* bytes */
160 unsigned long *alignment_offset, /* bytes */
161 unsigned long default_alignment)
163 int dev_alignment_offset = 0;
164 unsigned int min_io_size = 0, opt_io_size = 0;
165 unsigned long temp_alignment = 0;
168 *required_alignment = default_alignment;
169 *alignment_offset = 0;
171 if (!device || !device->path) //FIXME
174 fd = open(device->path, O_RDONLY);
178 /* minimum io size */
179 if (ioctl(fd, BLKIOMIN, &min_io_size) == -1) {
180 log_dbg("Topology info for %s not supported, using default offset %lu bytes.",
181 device->path, default_alignment);
185 /* optimal io size */
186 if (ioctl(fd, BLKIOOPT, &opt_io_size) == -1)
187 opt_io_size = min_io_size;
189 /* alignment offset, bogus -1 means misaligned/unknown */
190 if (ioctl(fd, BLKALIGNOFF, &dev_alignment_offset) == -1 || dev_alignment_offset < 0)
191 dev_alignment_offset = 0;
192 *alignment_offset = (unsigned long)dev_alignment_offset;
194 temp_alignment = (unsigned long)min_io_size;
196 if (temp_alignment < (unsigned long)opt_io_size)
197 temp_alignment = (unsigned long)opt_io_size;
199 /* If calculated alignment is multiple of default, keep default */
200 if (temp_alignment && (default_alignment % temp_alignment))
201 *required_alignment = temp_alignment;
203 log_dbg("Topology: IO (%u/%u), offset = %lu; Required alignment is %lu bytes.",
204 min_io_size, opt_io_size, *alignment_offset, *required_alignment);
209 int device_block_size(struct device *device)
212 int fd, bsize = 0, r = -EINVAL;
217 fd = open(device->path, O_RDONLY);
221 if (fstat(fd, &st) < 0)
224 if (S_ISREG(st.st_mode)) {
225 r = (int)crypt_getpagesize();
229 if (ioctl(fd, BLKSSZGET, &bsize) >= 0)
236 int device_read_ahead(struct device *device, uint32_t *read_ahead)
239 long read_ahead_long;
244 if ((fd = open(device->path, O_RDONLY)) < 0)
247 r = ioctl(fd, BLKRAGET, &read_ahead_long) ? 0 : 1;
251 *read_ahead = (uint32_t) read_ahead_long;
256 /* Get data size in bytes */
257 int device_size(struct device *device, uint64_t *size)
260 int devfd, r = -EINVAL;
262 devfd = open(device->path, O_RDONLY);
266 if (fstat(devfd, &st) < 0)
269 if (S_ISREG(st.st_mode)) {
270 *size = (uint64_t)st.st_size;
272 } else if (ioctl(devfd, BLKGETSIZE64, size) >= 0)
279 static int device_info(struct device *device,
280 enum devcheck device_check,
281 int *readonly, uint64_t *size)
284 int fd, r = -EINVAL, flags = 0;
289 if (stat(device->path, &st) < 0)
292 /* never wipe header on mounted device */
293 if (device_check == DEV_EXCL && S_ISBLK(st.st_mode))
296 /* Try to open read-write to check whether it is a read-only device */
297 /* coverity[toctou] */
298 fd = open(device->path, O_RDWR | flags);
299 if (fd == -1 && errno == EROFS) {
301 fd = open(device->path, O_RDONLY | flags);
304 if (fd == -1 && device_check == DEV_EXCL && errno == EBUSY)
310 if (S_ISREG(st.st_mode)) {
311 //FIXME: add readonly check
312 *size = (uint64_t)st.st_size;
313 *size >>= SECTOR_SHIFT;
315 /* If the device can be opened read-write, i.e. readonly is still 0, then
316 * check whether BKROGET says that it is read-only. E.g. read-only loop
317 * devices may be openend read-write but are read-only according to BLKROGET
319 if (*readonly == 0 && (r = ioctl(fd, BLKROGET, readonly)) < 0)
322 if (ioctl(fd, BLKGETSIZE64, size) >= 0) {
323 *size >>= SECTOR_SHIFT;
334 static int device_internal_prepare(struct crypt_device *cd, struct device *device)
337 int r, loop_fd, readonly = 0;
339 if (device->init_done)
342 log_dbg("Allocating a free loop device.");
343 loop_device = crypt_loop_get_device();
345 if (getuid() || geteuid())
346 log_err(cd, _("Cannot use a loopback device, "
347 "running as non-root user.\n"));
349 log_err(cd, _("Cannot find a free loopback device.\n"));
353 /* Keep the loop open, dettached on last close. */
354 loop_fd = crypt_loop_attach(loop_device, device->path, 0, 1, &readonly);
356 log_err(cd, _("Attaching loopback device failed "
357 "(loop device with autoclear flag is required).\n"));
362 r = device_ready(loop_device);
368 device->loop_fd = loop_fd;
369 device->file_path = device->path;
370 device->path = loop_device;
371 device->init_done = 1;
376 int device_block_adjust(struct crypt_device *cd,
377 struct device *device,
378 enum devcheck device_check,
379 uint64_t device_offset,
383 int r, real_readonly;
389 r = device_internal_prepare(cd, device);
393 r = device_info(device, device_check, &real_readonly, &real_size);
396 log_err(cd, _("Cannot use device %s which is in use "
397 "(already mapped or mounted).\n"),
400 log_err(cd, _("Cannot get info about device %s.\n"),
405 if (device_offset >= real_size) {
406 log_err(cd, _("Requested offset is beyond real size of device %s.\n"),
411 if (size && !*size) {
414 log_err(cd, _("Device %s has zero size.\n"), device->path);
417 *size -= device_offset;
420 /* in case of size is set by parameter */
421 if (size && ((real_size - device_offset) < *size)) {
422 log_dbg("Device %s: offset = %" PRIu64 " requested size = %" PRIu64
423 ", backing device size = %" PRIu64,
424 device->path, device_offset, *size, real_size);
425 log_err(cd, _("Device %s is too small.\n"), device->path);
429 if (flags && real_readonly)
430 *flags |= CRYPT_ACTIVATE_READONLY;
433 log_dbg("Calculated device size is %" PRIu64" sectors (%s), offset %" PRIu64 ".",
434 *size, real_readonly ? "RO" : "RW", device_offset);
438 size_t size_round_up(size_t size, unsigned int block)
440 size_t s = (size + (block - 1)) / block;