Add support for larger block size in loop.

This commit is contained in:
Ondrej Kozina
2021-02-02 16:26:02 +01:00
parent 25cd2b2fb7
commit ce80f7c5b1
3 changed files with 19 additions and 8 deletions

View File

@@ -773,10 +773,10 @@ static int device_internal_prepare(struct crypt_device *cd, struct device *devic
return -ENOTSUP;
}
log_dbg(cd, "Allocating a free loop device.");
log_dbg(cd, "Allocating a free loop device (block size: %zu).", SECTOR_SIZE);
/* Keep the loop open, detached on last close. */
loop_fd = crypt_loop_attach(&loop_device, device->path, 0, 1, &readonly);
loop_fd = crypt_loop_attach(&loop_device, device->path, 0, 1, &readonly, SECTOR_SIZE);
if (loop_fd == -1) {
log_err(cd, _("Attaching loopback device failed "
"(loop device with autoclear flag is required)."));
@@ -795,6 +795,8 @@ static int device_internal_prepare(struct crypt_device *cd, struct device *devic
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;

View File

@@ -35,6 +35,7 @@
#include <linux/loop.h>
#include "utils_loop.h"
#include "libcryptsetup_macros.h"
#define LOOP_DEV_MAJOR 7
@@ -50,6 +51,10 @@
#define LOOP_SET_CAPACITY 0x4C07
#endif
#ifndef LOOP_SET_BLOCK_SIZE
#define LOOP_SET_BLOCK_SIZE 0x4C09
#endif
#ifndef LOOP_CONFIGURE
#define LOOP_CONFIGURE 0x4C0A
struct loop_config {
@@ -111,7 +116,7 @@ static char *crypt_loop_get_device(void)
}
int crypt_loop_attach(char **loop, const char *file, int offset,
int autoclear, int *readonly)
int autoclear, int *readonly, size_t blocksize)
{
struct loop_config config = {0};
char *lo_file_name;
@@ -136,8 +141,10 @@ int crypt_loop_attach(char **loop, const char *file, int offset,
config.info.lo_offset = offset;
if (autoclear)
config.info.lo_flags |= LO_FLAGS_AUTOCLEAR;
if (blocksize > SECTOR_SIZE)
config.block_size = blocksize;
while (loop_fd < 0) {
while (loop_fd < 0) {
*loop = crypt_loop_get_device();
if (!*loop)
goto out;
@@ -167,9 +174,8 @@ int crypt_loop_attach(char **loop, const char *file, int offset,
}
}
if (fallback)
{
while (loop_fd < 0) {
if (fallback) {
while (loop_fd < 0) {
*loop = crypt_loop_get_device();
if (!*loop)
goto out;
@@ -188,6 +194,9 @@ int crypt_loop_attach(char **loop, const char *file, int offset,
}
}
if (blocksize > SECTOR_SIZE)
(void)ioctl(loop_fd, LOOP_SET_BLOCK_SIZE, (unsigned long)blocksize);
if (ioctl(loop_fd, LOOP_SET_STATUS64, &config.info) < 0) {
(void)ioctl(loop_fd, LOOP_CLR_FD, 0);
goto out;

View File

@@ -27,7 +27,7 @@
char *crypt_loop_backing_file(const char *loop);
int crypt_loop_device(const char *loop);
int crypt_loop_attach(char **loop, const char *file, int offset,
int autoclear, int *readonly);
int autoclear, int *readonly, size_t blocksize);
int crypt_loop_detach(const char *loop);
int crypt_loop_resize(const char *loop);