1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* device backend utilities
*
* Copyright (C) 2004 Jana Saout <jana@saout.de>
* Copyright (C) 2004-2007 Clemens Fruhwirth <clemens@endorphin.org>
* Copyright (C) 2009-2025 Red Hat, Inc. All rights reserved.
* Copyright (C) 2009-2025 Milan Broz
*/
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <linux/fs.h>
#include <unistd.h>
#if HAVE_SYS_SYSMACROS_H
# include <sys/sysmacros.h> /* for major, minor */
#endif
#if HAVE_SYS_STATVFS_H
# include <sys/statvfs.h>
#endif
#include "internal.h"
#include "utils_device_locking.h"
struct device {
char *path;
char *file_path;
int loop_fd;
int ro_dev_fd;
int dev_fd;
int dev_fd_excl;
struct crypt_lock_handle *lh;
unsigned int o_direct:1;
unsigned int init_done:1; /* path is bdev or loop already initialized */
/* cached values */
size_t alignment;
size_t block_size;
size_t loop_block_size;
};
static size_t device_fs_block_size_fd(int fd)
{
size_t max_size = MAX_SECTOR_SIZE;
#if HAVE_SYS_STATVFS_H
struct statvfs buf;
/*
* NOTE: some filesystems (NFS) returns bogus blocksize (1MB).
* Page-size io should always work and avoids increasing IO beyond aligned LUKS header.
*/
if (!fstatvfs(fd, &buf) && buf.f_bsize && buf.f_bsize <= max_size)
return (size_t)buf.f_bsize;
#endif
return max_size;
}
static size_t device_block_size_fd(int fd, size_t *min_size)
{
struct stat st;
size_t bsize;
int arg;
if (fstat(fd, &st) < 0)
return 0;
if (S_ISREG(st.st_mode))
bsize = device_fs_block_size_fd(fd);
else {
if (ioctl(fd, BLKSSZGET, &arg) < 0)
bsize = crypt_getpagesize();
else
bsize = (size_t)arg;
}
if (!min_size)
return bsize;
if (S_ISREG(st.st_mode)) {
/* file can be empty as well */
if (st.st_size > (ssize_t)bsize)
*min_size = bsize;
else
*min_size = st.st_size;
} else {
/* block device must have at least one block */
*min_size = bsize;
}
return bsize;
}
static size_t device_block_phys_size_fd(int fd)
{
struct stat st;
int arg;
size_t bsize = SECTOR_SIZE;
if (fstat(fd, &st) < 0)
return bsize;
if (S_ISREG(st.st_mode))
bsize = MAX_SECTOR_SIZE;
else if (ioctl(fd, BLKPBSZGET, &arg) >= 0)
bsize = (size_t)arg;
return bsize;
}
static size_t device_alignment_fd(int devfd)
{
long alignment = DEFAULT_MEM_ALIGNMENT;
#ifdef _PC_REC_XFER_ALIGN
alignment = fpathconf(devfd, _PC_REC_XFER_ALIGN);
if (alignment < 0)
alignment = DEFAULT_MEM_ALIGNMENT;
#endif
return (size_t)alignment;
}
static int device_read_test(struct crypt_device *cd, int devfd)
{
char buffer[512];
int r;
size_t minsize = 0, blocksize, alignment;
struct stat st;
/* skip check for block devices, direct-io must work there */
if (fstat(devfd, &st) < 0)
return -EINVAL;
if (S_ISBLK(st.st_mode))
return 0;
blocksize = device_block_size_fd(devfd, &minsize);
alignment = device_alignment_fd(devfd);
if (!blocksize || !alignment)
return -EINVAL;
if (minsize == 0)
return 0;
if (minsize > sizeof(buffer))
minsize = sizeof(buffer);
if (read_blockwise(devfd, blocksize, alignment, buffer, minsize) == (ssize_t)minsize) {
log_dbg(cd, "Direct-io read works.");
r = 0;
} else {
log_dbg(cd, "Direct-io read failed.");
r = -EIO;
}
crypt_safe_memzero(buffer, sizeof(buffer));
return r;
}
/*
* The direct-io is always preferred. The header is usually mapped to the same
* device and can be accessed when the rest of device is mapped to data device.
* Using direct-io ensures that we do not mess with data in cache.
* (But proper alignment should prevent this in the first place.)
* The read test is needed to detect broken configurations (seen with remote
* block devices) that allow open with direct-io but then fails on read.
*/
static int device_ready(struct crypt_device *cd, struct device *device)
{
int devfd = -1, r = 0;
struct stat st;
size_t tmp_size;
if (!device)
return -EINVAL;
if (device->o_direct) {
log_dbg(cd, "Trying to open device %s with direct-io.",
device_path(device));
device->o_direct = 0;
devfd = open(device_path(device), O_RDONLY | O_DIRECT);
if (devfd >= 0) {
if (device_read_test(cd, devfd) == 0) {
device->o_direct = 1;
} else {
close(devfd);
devfd = -1;
}
}
}
if (devfd < 0) {
log_dbg(cd, "Trying to open device %s without direct-io.",
device_path(device));
devfd = open(device_path(device), O_RDONLY);
}
if (devfd < 0) {
log_err(cd, _("Device %s does not exist or access denied."),
device_path(device));
return -EINVAL;
}
if (fstat(devfd, &st) < 0)
r = -EINVAL;
else if (!S_ISBLK(st.st_mode))
r = S_ISREG(st.st_mode) ? -ENOTBLK : -EINVAL;
if (r == -EINVAL) {
log_err(cd, _("Device %s is not compatible."),
device_path(device));
close(devfd);
return r;
}
/* Allow only increase (loop device) */
tmp_size = device_alignment_fd(devfd);
if (tmp_size > device->alignment)
device->alignment = tmp_size;
tmp_size = device_block_size_fd(devfd, NULL);
if (tmp_size > device->block_size)
device->block_size = tmp_size;
close(devfd);
return r;
}
static int _open_locked(struct crypt_device *cd, struct device *device, int flags)
{
int fd;
if (!device)
return -EINVAL;
log_dbg(cd, "Opening locked device %s", device_path(device));
if ((flags & O_ACCMODE) != O_RDONLY && device_locked_readonly(device->lh)) {
log_dbg(cd, "Cannot open locked device %s in write mode. Read lock held.", device_path(device));
return -EAGAIN;
}
fd = open(device_path(device), flags);
if (fd < 0)
return -errno;
if (device_locked_verify(cd, fd, device->lh)) {
/* fd doesn't correspond to a locked resource */
close(fd);
log_dbg(cd, "Failed to verify lock resource for device %s.", device_path(device));
return -EINVAL;
}
return fd;
}
/*
* Common wrapper for device sync.
*/
void device_sync(struct crypt_device *cd, struct device *device)
{
if (!device || device->dev_fd < 0)
return;
if (fsync(device->dev_fd) == -1)
log_dbg(cd, "Cannot sync device %s.", device_path(device));
}
/*
* in non-locked mode returns always fd or -1
*
* in locked mode:
* opened fd or one of:
* -EAGAIN : requested write mode while device being locked in via shared lock
* -EINVAL : invalid lock fd state
* -1 : all other errors
*/
static int device_open_internal(struct crypt_device *cd, struct device *device, int flags)
{
int access, devfd;
if (device->o_direct)
flags |= O_DIRECT;
access = flags & O_ACCMODE;
if (access == O_WRONLY)
access = O_RDWR;
if (access == O_RDONLY && device->ro_dev_fd >= 0) {
log_dbg(cd, "Reusing open r%c fd on device %s", 'o', device_path(device));
return device->ro_dev_fd;
} else if (access == O_RDWR && device->dev_fd >= 0) {
log_dbg(cd, "Reusing open r%c fd on device %s", 'w', device_path(device));
return device->dev_fd;
}
if (device_locked(device->lh))
devfd = _open_locked(cd, device, flags);
else
devfd = open(device_path(device), flags);
if (devfd < 0) {
log_dbg(cd, "Cannot open device %s%s.",
device_path(device),
access != O_RDONLY ? " for write" : "");
return devfd;
}
if (access == O_RDONLY)
device->ro_dev_fd = devfd;
else
device->dev_fd = devfd;
return devfd;
}
int device_open(struct crypt_device *cd, struct device *device, int flags)
{
if (!device)
return -EINVAL;
assert(!device_locked(device->lh));
return device_open_internal(cd, device, flags);
}
int device_open_excl(struct crypt_device *cd, struct device *device, int flags)
{
const char *path;
struct stat st;
if (!device)
return -EINVAL;
assert(!device_locked(device->lh));
if (device->dev_fd_excl < 0) {
path = device_path(device);
if (stat(path, &st))
return -EINVAL;
if (!S_ISBLK(st.st_mode))
log_dbg(cd, "%s is not a block device. Can't open in exclusive mode.",
path);
else {
/* open(2) with O_EXCL (w/o O_CREAT) on regular file is undefined behaviour according to man page */
/* coverity[toctou] */
device->dev_fd_excl = open(path, O_RDONLY | O_EXCL); /* lgtm[cpp/toctou-race-condition] */
if (device->dev_fd_excl < 0)
return errno == EBUSY ? -EBUSY : device->dev_fd_excl;
if (fstat(device->dev_fd_excl, &st) || !S_ISBLK(st.st_mode)) {
log_dbg(cd, "%s is not a block device. Can't open in exclusive mode.",
path);
close(device->dev_fd_excl);
device->dev_fd_excl = -1;
} else
log_dbg(cd, "Device %s is blocked for exclusive open.", path);
}
}
return device_open_internal(cd, device, flags);
}
void device_release_excl(struct crypt_device *cd, struct device *device)
{
if (device && device->dev_fd_excl >= 0) {
if (close(device->dev_fd_excl))
log_dbg(cd, "Failed to release exclusive handle on device %s.",
device_path(device));
else
log_dbg(cd, "Closed exclusive fd for %s.", device_path(device));
device->dev_fd_excl = -1;
}
}
int device_open_locked(struct crypt_device *cd, struct device *device, int flags)
{
if (!device)
return -EINVAL;
assert(!crypt_metadata_locking_enabled() || device_locked(device->lh));
return device_open_internal(cd, device, flags);
}
/* Avoid any read from device, expects direct-io to work. */
int device_alloc_no_check(struct device **device, const char *path)
{
struct device *dev;
if (!path) {
*device = NULL;
return 0;
}
dev = malloc(sizeof(struct device));
if (!dev)
return -ENOMEM;
memset(dev, 0, sizeof(struct device));
dev->path = strdup(path);
if (!dev->path) {
free(dev);
return -ENOMEM;
}
dev->loop_fd = -1;
dev->ro_dev_fd = -1;
dev->dev_fd = -1;
dev->dev_fd_excl = -1;
dev->o_direct = 1;
*device = dev;
return 0;
}
int device_alloc(struct crypt_device *cd, struct device **device, const char *path)
{
struct device *dev;
int r;
r = device_alloc_no_check(&dev, path);
if (r < 0)
return r;
if (dev) {
r = device_ready(cd, dev);
if (!r) {
dev->init_done = 1;
} else if (r == -ENOTBLK) {
/* alloc loop later */
} else if (r < 0) {
free(dev->path);
free(dev);
return -ENOTBLK;
}
}
*device = dev;
return 0;
}
void device_free(struct crypt_device *cd, struct device *device)
{
if (!device)
return;
device_close(cd, device);
if (device->dev_fd_excl != -1) {
log_dbg(cd, "Closed exclusive fd for %s.", device_path(device));
close(device->dev_fd_excl);
}
if (device->loop_fd != -1) {
log_dbg(cd, "Closed loop %s (%s).", device->path, device->file_path);
close(device->loop_fd);
}
assert(!device_locked(device->lh));
free(device->file_path);
free(device->path);
free(device);
}
/* Get block device path */
const char *device_block_path(const struct device *device)
{
if (!device)
return NULL;
return device->path;
}
/* Get path to device / file */
const char *device_path(const struct device *device)
{
if (!device)
return NULL;
if (device->file_path)
return device->file_path;
return device->path;
}
/* block device topology ioctls, introduced in 2.6.32 */
#ifndef BLKIOMIN
#define BLKIOMIN _IO(0x12,120)
#define BLKIOOPT _IO(0x12,121)
#define BLKALIGNOFF _IO(0x12,122)
#endif
void device_topology_alignment(struct crypt_device *cd,
struct device *device,
unsigned long *required_alignment, /* bytes */
unsigned long *alignment_offset, /* bytes */
unsigned long default_alignment)
{
int dev_alignment_offset = 0;
unsigned int min_io_size = 0, opt_io_size = 0;
unsigned long temp_alignment = 0;
int fd;
*required_alignment = default_alignment;
*alignment_offset = 0;
if (!device || !device->path) //FIXME
return;
fd = open(device->path, O_RDONLY);
if (fd == -1)
return;
/* minimum io size */
if (ioctl(fd, BLKIOMIN, &min_io_size) == -1) {
log_dbg(cd, "Topology info for %s not supported, using default alignment %lu bytes.",
device->path, default_alignment);
goto out;
}
/* optimal io size */
if (ioctl(fd, BLKIOOPT, &opt_io_size) == -1)
opt_io_size = min_io_size;
/* alignment offset, bogus -1 means misaligned/unknown */
if (ioctl(fd, BLKALIGNOFF, &dev_alignment_offset) == -1 || dev_alignment_offset < 0)
dev_alignment_offset = 0;
*alignment_offset = (unsigned long)dev_alignment_offset;
temp_alignment = (unsigned long)min_io_size;
/*
* Ignore bogus opt-io that could break alignment.
* Also real opt_io_size should be aligned to minimal page size (4k).
* Some bogus USB enclosures reports wrong data here.
*/
if ((temp_alignment < (unsigned long)opt_io_size) &&
!((unsigned long)opt_io_size % temp_alignment) && !MISALIGNED_4K(opt_io_size))
temp_alignment = (unsigned long)opt_io_size;
else if (opt_io_size && (opt_io_size != min_io_size))
log_err(cd, _("Ignoring bogus optimal-io size for data device (%u bytes)."), opt_io_size);
/* If calculated alignment is multiple of default, keep default */
if (temp_alignment && (default_alignment % temp_alignment))
*required_alignment = temp_alignment;
log_dbg(cd, "Topology: IO (%u/%u), offset = %lu; Required alignment is %lu bytes.",
min_io_size, opt_io_size, *alignment_offset, *required_alignment);
out:
(void)close(fd);
}
size_t device_block_size(struct crypt_device *cd, struct device *device)
{
int fd;
if (!device)
return 0;
if (device->block_size)
return device->block_size;
fd = open(device->file_path ?: device->path, O_RDONLY);
if (fd >= 0) {
device->block_size = device_block_size_fd(fd, NULL);
close(fd);
}
if (!device->block_size)
log_dbg(cd, "Cannot get block size for device %s.", device_path(device));
return device->block_size;
}
size_t device_optimal_encryption_sector_size(struct crypt_device *cd, struct device *device)
{
int fd;
size_t phys_block_size;
if (!device)
return SECTOR_SIZE;
fd = open(device->file_path ?: device->path, O_RDONLY);
if (fd < 0) {
log_dbg(cd, "Cannot get optimal encryption sector size for device %s.", device_path(device));
return SECTOR_SIZE;
}
/* cache device block size */
device->block_size = device_block_size_fd(fd, NULL);
if (!device->block_size) {
close(fd);
log_dbg(cd, "Cannot get block size for device %s.", device_path(device));
return SECTOR_SIZE;
}
if (device->block_size >= MAX_SECTOR_SIZE) {
close(fd);
return MISALIGNED(device->block_size, MAX_SECTOR_SIZE) ? SECTOR_SIZE : MAX_SECTOR_SIZE;
}
phys_block_size = device_block_phys_size_fd(fd);
close(fd);
if (device->block_size >= phys_block_size ||
phys_block_size <= SECTOR_SIZE ||
phys_block_size > MAX_SECTOR_SIZE ||
MISALIGNED(phys_block_size, device->block_size))
return device->block_size;
return phys_block_size;
}
int device_read_ahead(struct device *device, uint32_t *read_ahead)
{
int fd, r = 0;
long read_ahead_long;
if (!device)
return 0;
if ((fd = open(device->path, O_RDONLY)) < 0)
return 0;
r = ioctl(fd, BLKRAGET, &read_ahead_long) ? 0 : 1;
close(fd);
if (r)
*read_ahead = (uint32_t) read_ahead_long;
return r;
}
/* Get data size in bytes */
int device_size(struct device *device, uint64_t *size)
{
struct stat st;
int devfd, r = -EINVAL;
if (!device)
return -EINVAL;
devfd = open(device->path, O_RDONLY);
if (devfd == -1)
return -EINVAL;
if (fstat(devfd, &st) < 0)
goto out;
if (S_ISREG(st.st_mode)) {
*size = (uint64_t)st.st_size;
r = 0;
} else if (ioctl(devfd, BLKGETSIZE64, size) >= 0)
r = 0;
out:
close(devfd);
return r;
}
/* For a file, allocate the required space */
int device_fallocate(struct device *device, uint64_t size)
{
struct stat st;
int devfd, r = -EINVAL;
if (!device)
return -EINVAL;
devfd = open(device_path(device), O_RDWR);
if (devfd == -1)
return -EINVAL;
if (!fstat(devfd, &st) && S_ISREG(st.st_mode) &&
((uint64_t)st.st_size >= size || !posix_fallocate(devfd, 0, size))) {
r = 0;
if (device->file_path && crypt_loop_resize(device->path))
r = -EINVAL;
}
close(devfd);
return r;
}
int device_check_size(struct crypt_device *cd,
struct device *device,
uint64_t req_offset, int falloc)
{
uint64_t dev_size;
if (device_size(device, &dev_size)) {
log_dbg(cd, "Cannot get device size for device %s.", device_path(device));
return -EIO;
}
log_dbg(cd, "Device size %" PRIu64 ", offset %" PRIu64 ".", dev_size, req_offset);
if (req_offset > dev_size) {
/* If it is header file, increase its size */
if (falloc && !device_fallocate(device, req_offset))
return 0;
log_err(cd, _("Device %s is too small. Need at least %" PRIu64 " bytes."),
device_path(device), req_offset);
return -EINVAL;
}
return 0;
}
static int device_info(struct crypt_device *cd,
struct device *device,
enum devcheck device_check,
int *readonly, uint64_t *size)
{
struct stat st;
int fd = -1, r, flags = 0, real_readonly;
uint64_t real_size;
if (!device)
return -ENOTBLK;
real_readonly = 0;
real_size = 0;
if (stat(device->path, &st) < 0) {
r = -EINVAL;
goto out;
}
/* never wipe header on mounted device */
if (device_check == DEV_EXCL && S_ISBLK(st.st_mode))
flags |= O_EXCL;
/* Try to open read-write to check whether it is a read-only device */
/* coverity[toctou] */
fd = open(device->path, O_RDWR | flags);
if (fd == -1 && errno == EROFS) {
real_readonly = 1;
fd = open(device->path, O_RDONLY | flags);
}
if (fd == -1 && device_check == DEV_EXCL && errno == EBUSY) {
r = -EBUSY;
goto out;
}
if (fd == -1) {
r = errno ? -errno : -EINVAL;
goto out;
}
r = 0;
if (S_ISREG(st.st_mode)) {
//FIXME: add readonly check
real_size = (uint64_t)st.st_size;
real_size >>= SECTOR_SHIFT;
} else {
/* If the device can be opened read-write, i.e. readonly is still 0, then
* check whether BKROGET says that it is read-only. E.g. read-only loop
* devices may be opened read-write but are read-only according to BLKROGET
*/
if (real_readonly == 0 && (r = ioctl(fd, BLKROGET, &real_readonly)) < 0)
goto out;
r = ioctl(fd, BLKGETSIZE64, &real_size);
if (r >= 0) {
real_size >>= SECTOR_SHIFT;
goto out;
}
}
out:
if (fd != -1)
close(fd);
switch (r) {
case 0:
if (readonly)
*readonly = real_readonly;
if (size)
*size = real_size;
break;
case -EBUSY:
log_err(cd, _("Cannot use device %s which is in use "
"(already mapped or mounted)."), device_path(device));
break;
case -EACCES:
log_err(cd, _("Cannot use device %s, permission denied."), device_path(device));
break;
default:
log_err(cd, _("Cannot get info about device %s."), device_path(device));
r = -EINVAL;
}
return r;
}
int device_check_access(struct crypt_device *cd,
struct device *device,
enum devcheck device_check)
{
return device_info(cd, device, device_check, NULL, NULL);
}
static int device_internal_prepare(struct crypt_device *cd, struct device *device)
{
char *loop_device = NULL, *file_path = NULL;
int r, loop_fd, readonly = 0;
if (device->init_done)
return 0;
if (getuid() || geteuid()) {
log_err(cd, _("Cannot use a loopback device, "
"running as non-root user."));
return -ENOTSUP;
}
log_dbg(cd, "Allocating a free loop device (block size: %zu).",
device->loop_block_size ?: SECTOR_SIZE);
/* Keep the loop open, detached on last close. */
loop_fd = crypt_loop_attach(&loop_device, device->path, 0, 1, &readonly, device->loop_block_size);
if (loop_fd == -1) {
log_err(cd, _("Attaching loopback device failed "
"(loop device with autoclear flag is required)."));
free(loop_device);
return -EINVAL;
}
file_path = device->path;
device->path = loop_device;
r = device_ready(cd, device);
if (r < 0) {
device->path = file_path;
crypt_loop_detach(loop_device);
free(loop_device);
return r;
}
log_dbg(cd, "Attached loop device block size is %zu bytes.", device_block_size_fd(loop_fd, NULL));
device->loop_fd = loop_fd;
device->file_path = file_path;
device->init_done = 1;
return 0;
}
int device_block_adjust(struct crypt_device *cd,
struct device *device,
enum devcheck device_check,
uint64_t device_offset,
uint64_t *size,
uint32_t *flags)
{
int r, real_readonly;
uint64_t real_size;
if (!device)
return -ENOTBLK;
r = device_internal_prepare(cd, device);
if (r)
return r;
r = device_info(cd, device, device_check, &real_readonly, &real_size);
if (r)
return r;
if (device_offset >= real_size) {
log_err(cd, _("Requested offset is beyond real size of device %s."),
device_path(device));
return -EINVAL;
}
if (size && !*size) {
*size = real_size;
if (!*size) {
log_err(cd, _("Device %s has zero size."), device_path(device));
return -ENOTBLK;
}
*size -= device_offset;
}
/* in case of size is set by parameter */
if (size && ((real_size - device_offset) < *size)) {
log_dbg(cd, "Device %s: offset = %" PRIu64 " requested size = %" PRIu64
", backing device size = %" PRIu64,
device->path, device_offset, *size, real_size);
log_err(cd, _("Device %s is too small."), device_path(device));
return -EINVAL;
}
if (flags && real_readonly)
*flags |= CRYPT_ACTIVATE_READONLY;
if (size)
log_dbg(cd, "Calculated device size is %" PRIu64" sectors (%s), offset %" PRIu64 ".",
*size, real_readonly ? "RO" : "RW", device_offset);
return 0;
}
size_t size_round_up(size_t size, size_t block)
{
size_t s = (size + (block - 1)) / block;
return s * block;
}
void device_disable_direct_io(struct device *device)
{
if (device)
device->o_direct = 0;
}
int device_direct_io(const struct device *device)
{
return device ? device->o_direct : 0;
}
static int device_compare_path(const char *path1, const char *path2)
{
struct stat st_path1, st_path2;
if (stat(path1, &st_path1 ) < 0 || stat(path2, &st_path2 ) < 0)
return -EINVAL;
if (S_ISBLK(st_path1.st_mode) && S_ISBLK(st_path2.st_mode))
return (st_path1.st_rdev == st_path2.st_rdev) ? 1 : 0;
if (S_ISREG(st_path1.st_mode) && S_ISREG(st_path2.st_mode))
return (st_path1.st_ino == st_path2.st_ino &&
st_path1.st_dev == st_path2.st_dev) ? 1 : 0;
return 0;
}
int device_is_identical(struct device *device1, struct device *device2)
{
if (!device1 || !device2)
return 0;
if (device1 == device2)
return 1;
if (!strcmp(device_path(device1), device_path(device2)))
return 1;
return device_compare_path(device_path(device1), device_path(device2));
}
int device_is_rotational(struct device *device)
{
struct stat st;
if (!device)
return -EINVAL;
if (stat(device_path(device), &st) < 0)
return -EINVAL;
if (!S_ISBLK(st.st_mode))
return 0;
return crypt_dev_is_rotational(major(st.st_rdev), minor(st.st_rdev));
}
int device_is_dax(struct device *device)
{
struct stat st;
if (!device)
return -EINVAL;
if (stat(device_path(device), &st) < 0)
return -EINVAL;
if (!S_ISBLK(st.st_mode))
return 0;
return crypt_dev_is_dax(major(st.st_rdev), minor(st.st_rdev));
}
int device_is_zoned(struct device *device)
{
struct stat st;
if (!device)
return -EINVAL;
if (stat(device_path(device), &st) < 0)
return -EINVAL;
if (!S_ISBLK(st.st_mode))
return 0;
return crypt_dev_is_zoned(major(st.st_rdev), minor(st.st_rdev));
}
int device_is_nop_dif(struct device *device, uint32_t *tag_size)
{
struct stat st;
if (!device)
return -EINVAL;
if (stat(device_path(device), &st) < 0)
return -EINVAL;
if (!S_ISBLK(st.st_mode))
return 0;
return crypt_dev_is_nop_dif(major(st.st_rdev), minor(st.st_rdev), tag_size);
}
size_t device_alignment(struct device *device)
{
int devfd;
if (!device)
return -EINVAL;
if (!device->alignment) {
devfd = open(device_path(device), O_RDONLY);
if (devfd != -1) {
device->alignment = device_alignment_fd(devfd);
close(devfd);
}
}
return device->alignment;
}
void device_set_lock_handle(struct device *device, struct crypt_lock_handle *h)
{
if (device)
device->lh = h;
}
struct crypt_lock_handle *device_get_lock_handle(struct device *device)
{
return device ? device->lh : NULL;
}
int device_read_lock(struct crypt_device *cd, struct device *device)
{
if (!device || !crypt_metadata_locking_enabled())
return 0;
if (device_read_lock_internal(cd, device))
return -EBUSY;
return 0;
}
int device_write_lock(struct crypt_device *cd, struct device *device)
{
if (!device || !crypt_metadata_locking_enabled())
return 0;
assert(!device_locked(device->lh) || !device_locked_readonly(device->lh));
return device_write_lock_internal(cd, device);
}
void device_read_unlock(struct crypt_device *cd, struct device *device)
{
if (!device || !crypt_metadata_locking_enabled())
return;
assert(device_locked(device->lh));
device_unlock_internal(cd, device);
}
void device_write_unlock(struct crypt_device *cd, struct device *device)
{
if (!device || !crypt_metadata_locking_enabled())
return;
assert(device_locked(device->lh) && !device_locked_readonly(device->lh));
device_unlock_internal(cd, device);
}
bool device_is_locked(struct device *device)
{
return device ? device_locked(device->lh) : 0;
}
void device_close(struct crypt_device *cd, struct device *device)
{
if (!device)
return;
if (device->ro_dev_fd != -1) {
log_dbg(cd, "Closing read only fd for %s.", device_path(device));
if (close(device->ro_dev_fd))
log_dbg(cd, "Failed to close read only fd for %s.", device_path(device));
device->ro_dev_fd = -1;
}
if (device->dev_fd != -1) {
log_dbg(cd, "Closing read write fd for %s.", device_path(device));
if (close(device->dev_fd))
log_dbg(cd, "Failed to close read write fd for %s.", device_path(device));
device->dev_fd = -1;
}
}
void device_set_block_size(struct device *device, size_t size)
{
if (!device)
return;
device->loop_block_size = size;
}
|