2 * utils - miscellaneous device utilities for cryptsetup
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.
29 #include <sys/types.h>
31 #include <sys/types.h>
33 #include <sys/ioctl.h>
36 #include <sys/resource.h>
38 #include "libcryptsetup.h"
41 unsigned crypt_getpagesize(void)
43 return (unsigned)sysconf(_SC_PAGESIZE);
46 static int get_alignment(int fd)
48 int alignment = DEFAULT_MEM_ALIGNMENT;
50 #ifdef _PC_REC_XFER_ALIGN
51 alignment = fpathconf(fd, _PC_REC_XFER_ALIGN);
53 alignment = DEFAULT_MEM_ALIGNMENT;
58 static void *aligned_malloc(void **base, int size, int alignment)
60 #ifdef HAVE_POSIX_MEMALIGN
61 return posix_memalign(base, alignment, size) ? NULL : *base;
63 /* Credits go to Michal's padlock patches for this alignment code */
66 ptr = malloc(size + alignment);
67 if(ptr == NULL) return NULL;
70 if(alignment > 1 && ((long)ptr & (alignment - 1))) {
71 ptr += alignment - ((long)(ptr) & (alignment - 1));
77 int device_read_ahead(const char *dev, uint32_t *read_ahead)
82 if ((fd = open(dev, O_RDONLY)) < 0)
85 r = ioctl(fd, BLKRAGET, &read_ahead_long) ? 0 : 1;
89 *read_ahead = (uint32_t) read_ahead_long;
94 static int sector_size(int fd)
97 if (ioctl(fd,BLKSSZGET, &bsize) < 0)
103 int sector_size_for_device(const char *device)
105 int fd = open(device, O_RDONLY);
114 ssize_t write_blockwise(int fd, void *orig_buf, size_t count)
116 void *hangover_buf, *hangover_buf_base = NULL;
117 void *buf, *buf_base = NULL;
118 int r, hangover, solid, bsize, alignment;
121 if ((bsize = sector_size(fd)) < 0)
124 hangover = count % bsize;
125 solid = count - hangover;
126 alignment = get_alignment(fd);
128 if ((long)orig_buf & (alignment - 1)) {
129 buf = aligned_malloc(&buf_base, count, alignment);
132 memcpy(buf, orig_buf, count);
136 r = write(fd, buf, solid);
137 if (r < 0 || r != solid)
141 hangover_buf = aligned_malloc(&hangover_buf_base, bsize, alignment);
145 r = read(fd, hangover_buf, bsize);
146 if (r < 0 || r != bsize)
149 r = lseek(fd, -bsize, SEEK_CUR);
152 memcpy(hangover_buf, (char*)buf + solid, hangover);
154 r = write(fd, hangover_buf, bsize);
155 if (r < 0 || r != bsize)
160 free(hangover_buf_base);
166 ssize_t read_blockwise(int fd, void *orig_buf, size_t count) {
167 void *hangover_buf, *hangover_buf_base = NULL;
168 void *buf, *buf_base = NULL;
169 int r, hangover, solid, bsize, alignment;
172 if ((bsize = sector_size(fd)) < 0)
175 hangover = count % bsize;
176 solid = count - hangover;
177 alignment = get_alignment(fd);
179 if ((long)orig_buf & (alignment - 1)) {
180 buf = aligned_malloc(&buf_base, count, alignment);
186 r = read(fd, buf, solid);
187 if(r < 0 || r != solid)
191 hangover_buf = aligned_malloc(&hangover_buf_base, bsize, alignment);
194 r = read(fd, hangover_buf, bsize);
195 if (r < 0 || r != bsize)
198 memcpy((char *)buf + solid, hangover_buf, hangover);
202 free(hangover_buf_base);
203 if (buf != orig_buf) {
204 memcpy(orig_buf, buf, count);
211 * Combines llseek with blockwise write. write_blockwise can already deal with short writes
212 * but we also need a function to deal with short writes at the start. But this information
213 * is implicitly included in the read/write offset, which can not be set to non-aligned
214 * boundaries. Hence, we combine llseek with write.
216 ssize_t write_lseek_blockwise(int fd, char *buf, size_t count, off_t offset) {
218 void *frontPadBuf_base = NULL;
219 int r, bsize, frontHang;
220 size_t innerCount = 0;
223 if ((bsize = sector_size(fd)) < 0)
226 frontHang = offset % bsize;
228 if (lseek(fd, offset - frontHang, SEEK_SET) < 0)
232 frontPadBuf = aligned_malloc(&frontPadBuf_base,
233 bsize, get_alignment(fd));
237 r = read(fd, frontPadBuf, bsize);
238 if (r < 0 || r != bsize)
241 innerCount = bsize - frontHang;
242 if (innerCount > count)
245 memcpy(frontPadBuf + frontHang, buf, innerCount);
247 if (lseek(fd, offset - frontHang, SEEK_SET) < 0)
250 r = write(fd, frontPadBuf, bsize);
251 if (r < 0 || r != bsize)
258 ret = count ? write_blockwise(fd, buf, count) : 0;
262 free(frontPadBuf_base);
267 int device_ready(struct crypt_device *cd, const char *device, int mode)
274 if(stat(device, &st) < 0) {
275 log_err(cd, _("Device %s doesn't exist or access denied.\n"), device);
279 if (!S_ISBLK(st.st_mode))
282 log_dbg("Trying to open and read device %s.", device);
283 devfd = open(device, mode | O_DIRECT | O_SYNC);
285 log_err(cd, _("Cannot open device %s for %s%s access.\n"), device,
286 (mode & O_EXCL) ? _("exclusive ") : "",
287 (mode & O_RDWR) ? _("writable") : _("read-only"));
291 /* Try to read first sector */
292 s = read_blockwise(devfd, buf, sizeof(buf));
293 if (s < 0 || s != sizeof(buf)) {
294 log_verbose(cd, _("Cannot read device %s.\n"), device);
298 memset(buf, 0, sizeof(buf));
304 int device_size(const char *device, uint64_t *size)
308 devfd = open(device, O_RDONLY);
312 if (ioctl(devfd, BLKGETSIZE64, size) < 0)
319 static int get_device_infos(const char *device, enum devcheck device_check,
320 int *readonly, uint64_t *size)
323 unsigned long size_small;
330 if (stat(device, &st) < 0)
333 /* never wipe header on mounted device */
334 if (device_check == DEV_EXCL && S_ISBLK(st.st_mode))
337 /* Try to open read-write to check whether it is a read-only device */
338 fd = open(device, O_RDWR | flags);
339 if (fd == -1 && errno == EROFS) {
341 fd = open(device, O_RDONLY | flags);
344 if (fd == -1 && device_check == DEV_EXCL && errno == EBUSY)
350 /* If the device can be opened read-write, i.e. readonly is still 0, then
351 * check whether BKROGET says that it is read-only. E.g. read-only loop
352 * devices may be openend read-write but are read-only according to BLKROGET
354 if (*readonly == 0 && (r = ioctl(fd, BLKROGET, readonly)) < 0)
357 if (ioctl(fd, BLKGETSIZE64, size) >= 0) {
358 *size >>= SECTOR_SHIFT;
363 if (ioctl(fd, BLKGETSIZE, &size_small) >= 0) {
364 *size = (uint64_t)size_small;
375 int device_check_and_adjust(struct crypt_device *cd,
377 enum devcheck device_check,
382 int r, real_readonly;
388 r = get_device_infos(device, device_check, &real_readonly, &real_size);
391 log_err(cd, _("Cannot use device %s which is in use "
392 "(already mapped or mounted).\n"),
395 log_err(cd, _("Cannot get info about device %s.\n"),
400 if (*offset >= real_size) {
401 log_err(cd, _("Requested offset is beyond real size of device %s.\n"),
409 log_err(cd, _("Device %s has zero size.\n"), device);
415 /* in case of size is set by parameter */
416 if ((real_size - *offset) < *size) {
417 log_dbg("Device %s: offset = %" PRIu64 " requested size = %" PRIu64
418 ", backing device size = %" PRIu64,
419 device, *offset, *size, real_size);
420 log_err(cd, _("Device %s is too small.\n"), device);
425 *flags |= CRYPT_ACTIVATE_READONLY;
427 log_dbg("Calculated device size is %" PRIu64 " sectors (%s), offset %" PRIu64 ".",
428 *size, real_readonly ? "RO" : "RW", *offset);
433 #define DEFAULT_PROCESS_PRIORITY -18
435 static int _priority;
436 static int _memlock_count = 0;
438 // return 1 if memory is locked
439 int crypt_memlock_inc(struct crypt_device *ctx)
441 if (!_memlock_count++) {
442 log_dbg("Locking memory.");
443 if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) {
444 log_err(ctx, _("WARNING!!! Possibly insecure memory. Are you root?\n"));
449 if (((_priority = getpriority(PRIO_PROCESS, 0)) == -1) && errno)
450 log_err(ctx, _("Cannot get process priority.\n"));
452 if (setpriority(PRIO_PROCESS, 0, DEFAULT_PROCESS_PRIORITY))
453 log_err(ctx, _("setpriority %d failed: %s\n"),
454 DEFAULT_PROCESS_PRIORITY, strerror(errno));
456 return _memlock_count ? 1 : 0;
459 int crypt_memlock_dec(struct crypt_device *ctx)
461 if (_memlock_count && (!--_memlock_count)) {
462 log_dbg("Unlocking memory.");
463 if (munlockall() == -1)
464 log_err(ctx, _("Cannot unlock memory.\n"));
465 if (setpriority(PRIO_PROCESS, 0, _priority))
466 log_err(ctx, _("setpriority %d failed: %s\n"), _priority, strerror(errno));
468 return _memlock_count ? 1 : 0;
471 /* DEVICE TOPOLOGY */
473 /* block device topology ioctls, introduced in 2.6.32 */
475 #define BLKIOMIN _IO(0x12,120)
476 #define BLKIOOPT _IO(0x12,121)
477 #define BLKALIGNOFF _IO(0x12,122)
480 void get_topology_alignment(const char *device,
481 unsigned long *required_alignment, /* bytes */
482 unsigned long *alignment_offset, /* bytes */
483 unsigned long default_alignment)
485 int dev_alignment_offset = 0;
486 unsigned int min_io_size = 0, opt_io_size = 0;
487 unsigned long temp_alignment = 0;
490 *required_alignment = default_alignment;
491 *alignment_offset = 0;
493 fd = open(device, O_RDONLY);
497 /* minimum io size */
498 if (ioctl(fd, BLKIOMIN, &min_io_size) == -1) {
499 log_dbg("Topology info for %s not supported, using default offset %lu bytes.",
500 device, default_alignment);
504 /* optimal io size */
505 if (ioctl(fd, BLKIOOPT, &opt_io_size) == -1)
506 opt_io_size = min_io_size;
508 /* alignment offset, bogus -1 means misaligned/unknown */
509 if (ioctl(fd, BLKALIGNOFF, &dev_alignment_offset) == -1 || dev_alignment_offset < 0)
510 dev_alignment_offset = 0;
511 *alignment_offset = (unsigned long)dev_alignment_offset;
513 temp_alignment = (unsigned long)min_io_size;
515 if (temp_alignment < (unsigned long)opt_io_size)
516 temp_alignment = (unsigned long)opt_io_size;
518 /* If calculated alignment is multiple of default, keep default */
519 if (temp_alignment && (default_alignment % temp_alignment))
520 *required_alignment = temp_alignment;
522 log_dbg("Topology: IO (%u/%u), offset = %lu; Required alignment is %lu bytes.",
523 min_io_size, opt_io_size, *alignment_offset, *required_alignment);