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 static int get_alignment(int fd)
43 int alignment = DEFAULT_MEM_ALIGNMENT;
45 #ifdef _PC_REC_XFER_ALIGN
46 alignment = fpathconf(fd, _PC_REC_XFER_ALIGN);
48 alignment = DEFAULT_MEM_ALIGNMENT;
53 static void *aligned_malloc(void **base, int size, int alignment)
55 #ifdef HAVE_POSIX_MEMALIGN
56 return posix_memalign(base, alignment, size) ? NULL : *base;
58 /* Credits go to Michal's padlock patches for this alignment code */
61 ptr = malloc(size + alignment);
62 if(ptr == NULL) return NULL;
65 if(alignment > 1 && ((long)ptr & (alignment - 1))) {
66 ptr += alignment - ((long)(ptr) & (alignment - 1));
72 int device_read_ahead(const char *dev, uint32_t *read_ahead)
77 if ((fd = open(dev, O_RDONLY)) < 0)
80 r = ioctl(fd, BLKRAGET, &read_ahead_long) ? 0 : 1;
84 *read_ahead = (uint32_t) read_ahead_long;
89 static int sector_size(int fd)
92 if (ioctl(fd,BLKSSZGET, &bsize) < 0)
98 int sector_size_for_device(const char *device)
100 int fd = open(device, O_RDONLY);
109 ssize_t write_blockwise(int fd, void *orig_buf, size_t count)
111 void *hangover_buf, *hangover_buf_base = NULL;
112 void *buf, *buf_base = NULL;
113 int r, hangover, solid, bsize, alignment;
116 if ((bsize = sector_size(fd)) < 0)
119 hangover = count % bsize;
120 solid = count - hangover;
121 alignment = get_alignment(fd);
123 if ((long)orig_buf & (alignment - 1)) {
124 buf = aligned_malloc(&buf_base, count, alignment);
127 memcpy(buf, orig_buf, count);
131 r = write(fd, buf, solid);
132 if (r < 0 || r != solid)
136 hangover_buf = aligned_malloc(&hangover_buf_base, bsize, alignment);
140 r = read(fd, hangover_buf, bsize);
141 if (r < 0 || r != bsize)
144 r = lseek(fd, -bsize, SEEK_CUR);
147 memcpy(hangover_buf, (char*)buf + solid, hangover);
149 r = write(fd, hangover_buf, bsize);
150 if (r < 0 || r != bsize)
155 free(hangover_buf_base);
161 ssize_t read_blockwise(int fd, void *orig_buf, size_t count) {
162 void *hangover_buf, *hangover_buf_base = NULL;
163 void *buf, *buf_base = NULL;
164 int r, hangover, solid, bsize, alignment;
167 if ((bsize = sector_size(fd)) < 0)
170 hangover = count % bsize;
171 solid = count - hangover;
172 alignment = get_alignment(fd);
174 if ((long)orig_buf & (alignment - 1)) {
175 buf = aligned_malloc(&buf_base, count, alignment);
181 r = read(fd, buf, solid);
182 if(r < 0 || r != solid)
186 hangover_buf = aligned_malloc(&hangover_buf_base, bsize, alignment);
189 r = read(fd, hangover_buf, bsize);
190 if (r < 0 || r != bsize)
193 memcpy((char *)buf + solid, hangover_buf, hangover);
197 free(hangover_buf_base);
198 if (buf != orig_buf) {
199 memcpy(orig_buf, buf, count);
206 * Combines llseek with blockwise write. write_blockwise can already deal with short writes
207 * but we also need a function to deal with short writes at the start. But this information
208 * is implicitly included in the read/write offset, which can not be set to non-aligned
209 * boundaries. Hence, we combine llseek with write.
211 ssize_t write_lseek_blockwise(int fd, char *buf, size_t count, off_t offset) {
213 void *frontPadBuf_base = NULL;
214 int r, bsize, frontHang;
215 size_t innerCount = 0;
218 if ((bsize = sector_size(fd)) < 0)
221 frontHang = offset % bsize;
223 if (lseek(fd, offset - frontHang, SEEK_SET) < 0)
227 frontPadBuf = aligned_malloc(&frontPadBuf_base,
228 bsize, get_alignment(fd));
232 r = read(fd, frontPadBuf, bsize);
233 if (r < 0 || r != bsize)
236 innerCount = bsize - frontHang;
237 if (innerCount > count)
240 memcpy(frontPadBuf + frontHang, buf, innerCount);
242 if (lseek(fd, offset - frontHang, SEEK_SET) < 0)
245 r = write(fd, frontPadBuf, bsize);
246 if (r < 0 || r != bsize)
253 ret = count ? write_blockwise(fd, buf, count) : 0;
257 free(frontPadBuf_base);
262 int device_ready(struct crypt_device *cd, const char *device, int mode)
269 if(stat(device, &st) < 0) {
270 log_err(cd, _("Device %s doesn't exist or access denied.\n"), device);
274 if (!S_ISBLK(st.st_mode))
277 log_dbg("Trying to open and read device %s.", device);
278 devfd = open(device, mode | O_DIRECT | O_SYNC);
280 log_err(cd, _("Cannot open device %s for %s%s access.\n"), device,
281 (mode & O_EXCL) ? _("exclusive ") : "",
282 (mode & O_RDWR) ? _("writable") : _("read-only"));
286 /* Try to read first sector */
287 s = read_blockwise(devfd, buf, sizeof(buf));
288 if (s < 0 || s != sizeof(buf)) {
289 log_verbose(cd, _("Cannot read device %s.\n"), device);
293 memset(buf, 0, sizeof(buf));
299 int device_size(const char *device, uint64_t *size)
303 devfd = open(device, O_RDONLY);
307 if (ioctl(devfd, BLKGETSIZE64, size) < 0)
314 static int get_device_infos(const char *device, enum devcheck device_check,
315 int *readonly, uint64_t *size)
318 unsigned long size_small;
325 if (stat(device, &st) < 0)
328 /* never wipe header on mounted device */
329 if (device_check == DEV_EXCL && S_ISBLK(st.st_mode))
332 /* Try to open read-write to check whether it is a read-only device */
333 fd = open(device, O_RDWR | flags);
334 if (fd == -1 && errno == EROFS) {
336 fd = open(device, O_RDONLY | flags);
339 if (fd == -1 && device_check == DEV_EXCL && errno == EBUSY)
345 /* If the device can be opened read-write, i.e. readonly is still 0, then
346 * check whether BKROGET says that it is read-only. E.g. read-only loop
347 * devices may be openend read-write but are read-only according to BLKROGET
349 if (*readonly == 0 && (r = ioctl(fd, BLKROGET, readonly)) < 0)
352 if (ioctl(fd, BLKGETSIZE64, size) >= 0) {
353 *size >>= SECTOR_SHIFT;
358 if (ioctl(fd, BLKGETSIZE, &size_small) >= 0) {
359 *size = (uint64_t)size_small;
370 int device_check_and_adjust(struct crypt_device *cd,
372 enum devcheck device_check,
377 int r, real_readonly;
383 r = get_device_infos(device, device_check, &real_readonly, &real_size);
386 log_err(cd, _("Cannot use device %s which is in use "
387 "(already mapped or mounted).\n"),
390 log_err(cd, _("Cannot get info about device %s.\n"),
395 if (*offset >= real_size) {
396 log_err(cd, _("Requested offset is beyond real size of device %s.\n"),
404 log_err(cd, _("Device %s has zero size.\n"), device);
410 /* in case of size is set by parameter */
411 if ((real_size - *offset) < *size) {
412 log_dbg("Device %s: offset = %" PRIu64 " requested size = %" PRIu64
413 ", backing device size = %" PRIu64,
414 device, *offset, *size, real_size);
415 log_err(cd, _("Device %s is too small.\n"), device);
419 if (device_check == DEV_SHARED) {
420 log_dbg("Checking crypt segments for device %s.", device);
421 r = crypt_sysfs_check_crypt_segment(device, *offset, *size);
423 log_err(cd, _("Cannot use device %s (crypt segments "
424 "overlaps or in use by another device).\n"),
431 *flags |= CRYPT_ACTIVATE_READONLY;
433 log_dbg("Calculated device size is %" PRIu64 " sectors (%s), offset %" PRIu64 ".",
434 *size, real_readonly ? "RO" : "RW", *offset);
439 #define DEFAULT_PROCESS_PRIORITY -18
441 static int _priority;
442 static int _memlock_count = 0;
444 // return 1 if memory is locked
445 int crypt_memlock_inc(struct crypt_device *ctx)
447 if (!_memlock_count++) {
448 log_dbg("Locking memory.");
449 if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1) {
450 log_err(ctx, _("WARNING!!! Possibly insecure memory. Are you root?\n"));
455 if (((_priority = getpriority(PRIO_PROCESS, 0)) == -1) && errno)
456 log_err(ctx, _("Cannot get process priority.\n"));
458 if (setpriority(PRIO_PROCESS, 0, DEFAULT_PROCESS_PRIORITY))
459 log_err(ctx, _("setpriority %d failed: %s\n"),
460 DEFAULT_PROCESS_PRIORITY, strerror(errno));
462 return _memlock_count ? 1 : 0;
465 int crypt_memlock_dec(struct crypt_device *ctx)
467 if (_memlock_count && (!--_memlock_count)) {
468 log_dbg("Unlocking memory.");
469 if (munlockall() == -1)
470 log_err(ctx, _("Cannot unlock memory.\n"));
471 if (setpriority(PRIO_PROCESS, 0, _priority))
472 log_err(ctx, _("setpriority %d failed: %s\n"), _priority, strerror(errno));
474 return _memlock_count ? 1 : 0;
477 /* DEVICE TOPOLOGY */
479 /* block device topology ioctls, introduced in 2.6.32 */
481 #define BLKIOMIN _IO(0x12,120)
482 #define BLKIOOPT _IO(0x12,121)
483 #define BLKALIGNOFF _IO(0x12,122)
486 void get_topology_alignment(const char *device,
487 unsigned long *required_alignment, /* bytes */
488 unsigned long *alignment_offset, /* bytes */
489 unsigned long default_alignment)
491 int dev_alignment_offset = 0;
492 unsigned int min_io_size = 0, opt_io_size = 0;
493 unsigned long temp_alignment = 0;
496 *required_alignment = default_alignment;
497 *alignment_offset = 0;
499 fd = open(device, O_RDONLY);
503 /* minimum io size */
504 if (ioctl(fd, BLKIOMIN, &min_io_size) == -1) {
505 log_dbg("Topology info for %s not supported, using default offset %lu bytes.",
506 device, default_alignment);
510 /* optimal io size */
511 if (ioctl(fd, BLKIOOPT, &opt_io_size) == -1)
512 opt_io_size = min_io_size;
514 /* alignment offset, bogus -1 means misaligned/unknown */
515 if (ioctl(fd, BLKALIGNOFF, &dev_alignment_offset) == -1 || dev_alignment_offset < 0)
516 dev_alignment_offset = 0;
517 *alignment_offset = (unsigned long)dev_alignment_offset;
519 temp_alignment = (unsigned long)min_io_size;
521 if (temp_alignment < (unsigned long)opt_io_size)
522 temp_alignment = (unsigned long)opt_io_size;
524 /* If calculated alignment is multiple of default, keep default */
525 if (temp_alignment && (default_alignment % temp_alignment))
526 *required_alignment = temp_alignment;
528 log_dbg("Topology: IO (%u/%u), offset = %lu; Required alignment is %lu bytes.",
529 min_io_size, opt_io_size, *alignment_offset, *required_alignment);