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 * version 2 as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include <sys/types.h>
29 #include <sys/ioctl.h>
43 static int device_ready(const char *device)
48 log_dbg("Trying to open and read device %s.", device);
49 devfd = open(device, O_RDONLY | O_DIRECT | O_SYNC);
51 log_err(NULL, _("Device %s doesn't exist or access denied.\n"), device);
54 if (fstat(devfd, &st) < 0)
56 else if (!S_ISBLK(st.st_mode))
57 r = S_ISREG(st.st_mode) ? -ENOTBLK : -EINVAL;
63 int device_alloc(struct device **device, const char *path)
73 dev = malloc(sizeof(struct device));
77 memset(dev, 0, sizeof(struct device));
80 r = device_ready(path);
83 } else if (r == -ENOTBLK) {
84 /* alloc loop later */
90 dev->path = strdup(path);
100 void device_free(struct device *device)
105 if (device->loop_fd != -1) {
106 log_dbg("Closed loop %s (%s).", device->path, device->file_path);
107 close(device->loop_fd);
110 free(device->file_path);
115 /* Get block device path */
116 const char *device_block_path(const struct device *device)
118 if (!device || !device->init_done)
124 /* Get path to device / file */
125 const char *device_path(const struct device *device)
130 if (device->file_path)
131 return device->file_path;
136 /* block device topology ioctls, introduced in 2.6.32 */
138 #define BLKIOMIN _IO(0x12,120)
139 #define BLKIOOPT _IO(0x12,121)
140 #define BLKALIGNOFF _IO(0x12,122)
143 void device_topology_alignment(struct device *device,
144 unsigned long *required_alignment, /* bytes */
145 unsigned long *alignment_offset, /* bytes */
146 unsigned long default_alignment)
148 int dev_alignment_offset = 0;
149 unsigned int min_io_size = 0, opt_io_size = 0;
150 unsigned long temp_alignment = 0;
153 *required_alignment = default_alignment;
154 *alignment_offset = 0;
156 if (!device || !device->path) //FIXME
159 fd = open(device->path, O_RDONLY);
163 /* minimum io size */
164 if (ioctl(fd, BLKIOMIN, &min_io_size) == -1) {
165 log_dbg("Topology info for %s not supported, using default offset %lu bytes.",
166 device->path, default_alignment);
170 /* optimal io size */
171 if (ioctl(fd, BLKIOOPT, &opt_io_size) == -1)
172 opt_io_size = min_io_size;
174 /* alignment offset, bogus -1 means misaligned/unknown */
175 if (ioctl(fd, BLKALIGNOFF, &dev_alignment_offset) == -1 || dev_alignment_offset < 0)
176 dev_alignment_offset = 0;
177 *alignment_offset = (unsigned long)dev_alignment_offset;
179 temp_alignment = (unsigned long)min_io_size;
181 if (temp_alignment < (unsigned long)opt_io_size)
182 temp_alignment = (unsigned long)opt_io_size;
184 /* If calculated alignment is multiple of default, keep default */
185 if (temp_alignment && (default_alignment % temp_alignment))
186 *required_alignment = temp_alignment;
188 log_dbg("Topology: IO (%u/%u), offset = %lu; Required alignment is %lu bytes.",
189 min_io_size, opt_io_size, *alignment_offset, *required_alignment);
194 int device_block_size(struct device *device)
197 int fd, bsize = 0, r = -EINVAL;
202 fd = open(device->path, O_RDONLY);
206 if (fstat(fd, &st) < 0)
209 if (S_ISREG(st.st_mode)) {
210 r = (int)crypt_getpagesize();
214 if (ioctl(fd, BLKSSZGET, &bsize) >= 0)
221 int device_read_ahead(struct device *device, uint32_t *read_ahead)
224 long read_ahead_long;
229 if ((fd = open(device->path, O_RDONLY)) < 0)
232 r = ioctl(fd, BLKRAGET, &read_ahead_long) ? 0 : 1;
236 *read_ahead = (uint32_t) read_ahead_long;
241 /* Get data size in bytes */
242 int device_size(struct device *device, uint64_t *size)
245 int devfd, r = -EINVAL;
247 devfd = open(device->path, O_RDONLY);
251 if (fstat(devfd, &st) < 0)
254 if (S_ISREG(st.st_mode)) {
255 *size = (uint64_t)st.st_size;
257 } else if (ioctl(devfd, BLKGETSIZE64, size) >= 0)
264 static int device_info(struct device *device,
265 enum devcheck device_check,
266 int *readonly, uint64_t *size)
269 int fd, r = -EINVAL, flags = 0;
274 if (stat(device->path, &st) < 0)
277 /* never wipe header on mounted device */
278 if (device_check == DEV_EXCL && S_ISBLK(st.st_mode))
281 /* Try to open read-write to check whether it is a read-only device */
282 /* coverity[toctou] */
283 fd = open(device->path, O_RDWR | flags);
284 if (fd == -1 && errno == EROFS) {
286 fd = open(device->path, O_RDONLY | flags);
289 if (fd == -1 && device_check == DEV_EXCL && errno == EBUSY)
295 if (S_ISREG(st.st_mode)) {
296 //FIXME: add readonly check
297 *size = (uint64_t)st.st_size;
298 *size >>= SECTOR_SHIFT;
300 /* If the device can be opened read-write, i.e. readonly is still 0, then
301 * check whether BKROGET says that it is read-only. E.g. read-only loop
302 * devices may be openend read-write but are read-only according to BLKROGET
304 if (*readonly == 0 && (r = ioctl(fd, BLKROGET, readonly)) < 0)
307 if (ioctl(fd, BLKGETSIZE64, size) >= 0) {
308 *size >>= SECTOR_SHIFT;
319 static int device_internal_prepare(struct crypt_device *cd, struct device *device)
322 int r, loop_fd, readonly = 0;
324 if (device->init_done)
327 log_dbg("Allocating a free loop device.");
328 loop_device = crypt_loop_get_device();
330 if (getuid() || geteuid())
331 log_err(cd, _("Cannot use a loopback device, "
332 "running as non-root user.\n"));
334 log_err(cd, _("Cannot find a free loopback device.\n"));
338 /* Keep the loop open, dettached on last close. */
339 loop_fd = crypt_loop_attach(loop_device, device->path, 0, 1, &readonly);
341 log_err(cd, _("Attaching loopback device failed "
342 "(loop device with autoclear flag is required).\n"));
347 r = device_ready(loop_device);
353 device->loop_fd = loop_fd;
354 device->file_path = device->path;
355 device->path = loop_device;
356 device->init_done = 1;
361 int device_block_adjust(struct crypt_device *cd,
362 struct device *device,
363 enum devcheck device_check,
364 uint64_t device_offset,
368 int r, real_readonly;
374 r = device_internal_prepare(cd, device);
378 r = device_info(device, device_check, &real_readonly, &real_size);
381 log_err(cd, _("Cannot use device %s which is in use "
382 "(already mapped or mounted).\n"),
385 log_err(cd, _("Cannot get info about device %s.\n"),
390 if (device_offset >= real_size) {
391 log_err(cd, _("Requested offset is beyond real size of device %s.\n"),
396 if (size && !*size) {
399 log_err(cd, _("Device %s has zero size.\n"), device->path);
402 *size -= device_offset;
405 /* in case of size is set by parameter */
406 if (size && ((real_size - device_offset) < *size)) {
407 log_dbg("Device %s: offset = %" PRIu64 " requested size = %" PRIu64
408 ", backing device size = %" PRIu64,
409 device->path, device_offset, *size, real_size);
410 log_err(cd, _("Device %s is too small.\n"), device->path);
414 if (flags && real_readonly)
415 *flags |= CRYPT_ACTIVATE_READONLY;
418 log_dbg("Calculated device size is %" PRIu64" sectors (%s), offset %" PRIu64 ".",
419 *size, real_readonly ? "RO" : "RW", device_offset);
423 size_t size_round_up(size_t size, unsigned int block)
425 size_t s = (size + (block - 1)) / block;