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.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <sys/types.h>
28 #include <sys/ioctl.h>
42 static int device_ready(const char *device)
47 if(stat(device, &st) < 0) {
48 log_err(NULL, _("Device %s doesn't exist or access denied.\n"), device);
52 if (!S_ISBLK(st.st_mode))
53 return S_ISREG(st.st_mode) ? -ENOTBLK : -EINVAL;
55 log_dbg("Trying to open and read device %s.", device);
56 devfd = open(device, O_RDONLY | O_DIRECT | O_SYNC);
58 log_err(NULL, _("Cannot open device %s.\n"), device);
66 int device_alloc(struct device **device, const char *path)
76 dev = malloc(sizeof(struct device));
80 memset(dev, 0, sizeof(struct device));
83 r = device_ready(path);
86 } else if (r == -ENOTBLK) {
87 /* alloc loop later */
93 dev->path = strdup(path);
103 void device_free(struct device *device)
108 if (device->loop_fd != -1) {
109 log_dbg("Closed loop %s (%s).", device->path, device->file_path);
110 close(device->loop_fd);
113 free(device->file_path);
118 /* Get block device path */
119 const char *device_block_path(const struct device *device)
121 if (!device || !device->init_done)
127 /* Get path to device / file */
128 const char *device_path(const struct device *device)
133 if (device->file_path)
134 return device->file_path;
139 /* block device topology ioctls, introduced in 2.6.32 */
141 #define BLKIOMIN _IO(0x12,120)
142 #define BLKIOOPT _IO(0x12,121)
143 #define BLKALIGNOFF _IO(0x12,122)
146 void device_topology_alignment(struct device *device,
147 unsigned long *required_alignment, /* bytes */
148 unsigned long *alignment_offset, /* bytes */
149 unsigned long default_alignment)
151 int dev_alignment_offset = 0;
152 unsigned int min_io_size = 0, opt_io_size = 0;
153 unsigned long temp_alignment = 0;
156 *required_alignment = default_alignment;
157 *alignment_offset = 0;
159 if (!device || !device->path) //FIXME
162 fd = open(device->path, O_RDONLY);
166 /* minimum io size */
167 if (ioctl(fd, BLKIOMIN, &min_io_size) == -1) {
168 log_dbg("Topology info for %s not supported, using default offset %lu bytes.",
169 device->path, default_alignment);
173 /* optimal io size */
174 if (ioctl(fd, BLKIOOPT, &opt_io_size) == -1)
175 opt_io_size = min_io_size;
177 /* alignment offset, bogus -1 means misaligned/unknown */
178 if (ioctl(fd, BLKALIGNOFF, &dev_alignment_offset) == -1 || dev_alignment_offset < 0)
179 dev_alignment_offset = 0;
180 *alignment_offset = (unsigned long)dev_alignment_offset;
182 temp_alignment = (unsigned long)min_io_size;
184 if (temp_alignment < (unsigned long)opt_io_size)
185 temp_alignment = (unsigned long)opt_io_size;
187 /* If calculated alignment is multiple of default, keep default */
188 if (temp_alignment && (default_alignment % temp_alignment))
189 *required_alignment = temp_alignment;
191 log_dbg("Topology: IO (%u/%u), offset = %lu; Required alignment is %lu bytes.",
192 min_io_size, opt_io_size, *alignment_offset, *required_alignment);
197 int device_block_size(struct device *device)
200 int fd, bsize = 0, r = -EINVAL;
205 fd = open(device->path, O_RDONLY);
209 if (fstat(fd, &st) < 0)
212 if (S_ISREG(st.st_mode)) {
213 r = (int)crypt_getpagesize();
217 if (ioctl(fd, BLKSSZGET, &bsize) >= 0)
224 int device_read_ahead(struct device *device, uint32_t *read_ahead)
227 long read_ahead_long;
232 if ((fd = open(device->path, O_RDONLY)) < 0)
235 r = ioctl(fd, BLKRAGET, &read_ahead_long) ? 0 : 1;
239 *read_ahead = (uint32_t) read_ahead_long;
244 /* Get data size in bytes */
245 int device_size(struct device *device, uint64_t *size)
248 int devfd, r = -EINVAL;
250 devfd = open(device->path, O_RDONLY);
254 if (fstat(devfd, &st) < 0)
257 if (S_ISREG(st.st_mode)) {
258 *size = (uint64_t)st.st_size;
260 } else if (ioctl(devfd, BLKGETSIZE64, size) >= 0)
267 static int device_info(struct device *device,
268 enum devcheck device_check,
269 int *readonly, uint64_t *size)
272 int fd, r = -EINVAL, flags = 0;
277 if (stat(device->path, &st) < 0)
280 /* never wipe header on mounted device */
281 if (device_check == DEV_EXCL && S_ISBLK(st.st_mode))
284 /* Try to open read-write to check whether it is a read-only device */
285 fd = open(device->path, O_RDWR | flags);
286 if (fd == -1 && errno == EROFS) {
288 fd = open(device->path, O_RDONLY | flags);
291 if (fd == -1 && device_check == DEV_EXCL && errno == EBUSY)
297 if (S_ISREG(st.st_mode)) {
298 //FIXME: add readonly check
299 *size = (uint64_t)st.st_size;
300 *size >>= SECTOR_SHIFT;
302 /* If the device can be opened read-write, i.e. readonly is still 0, then
303 * check whether BKROGET says that it is read-only. E.g. read-only loop
304 * devices may be openend read-write but are read-only according to BLKROGET
306 if (*readonly == 0 && (r = ioctl(fd, BLKROGET, readonly)) < 0)
309 if (ioctl(fd, BLKGETSIZE64, size) >= 0) {
310 *size >>= SECTOR_SHIFT;
321 static int device_internal_prepare(struct crypt_device *cd, struct device *device)
324 int r, loop_fd, readonly = 0;
326 if (device->init_done)
329 log_dbg("Allocating free loop device.");
330 loop_device = crypt_loop_get_device();
332 log_err(cd, _("Cannot find a free loopback device.\n"));
336 /* Keep the loop open, dettached on last close. */
337 loop_fd = crypt_loop_attach(loop_device, device->path, 0, 1, &readonly);
339 log_err(cd, _("Attaching loopback device failed "
340 "(loop device with autoclear flag is required).\n"));
345 r = device_ready(loop_device);
351 device->loop_fd = loop_fd;
352 device->file_path = device->path;
353 device->path = loop_device;
354 device->init_done = 1;
359 int device_block_adjust(struct crypt_device *cd,
360 struct device *device,
361 enum devcheck device_check,
362 uint64_t device_offset,
366 int r, real_readonly;
372 r = device_internal_prepare(cd, device);
376 r = device_info(device, device_check, &real_readonly, &real_size);
379 log_err(cd, _("Cannot use device %s which is in use "
380 "(already mapped or mounted).\n"),
383 log_err(cd, _("Cannot get info about device %s.\n"),
388 if (device_offset >= real_size) {
389 log_err(cd, _("Requested offset is beyond real size of device %s.\n"),
394 if (size && !*size) {
397 log_err(cd, _("Device %s has zero size.\n"), device->path);
400 *size -= device_offset;
403 /* in case of size is set by parameter */
404 if (size && ((real_size - device_offset) < *size)) {
405 log_dbg("Device %s: offset = %" PRIu64 " requested size = %" PRIu64
406 ", backing device size = %" PRIu64,
407 device->path, device_offset, *size, real_size);
408 log_err(cd, _("Device %s is too small.\n"), device->path);
412 if (flags && real_readonly)
413 *flags |= CRYPT_ACTIVATE_READONLY;
416 log_dbg("Calculated device size is %" PRIu64" sectors (%s), offset %" PRIu64 ".",
417 *size, real_readonly ? "RO" : "RW", device_offset);