utils_device.c (cryptsetup-2.3.6.tar.xz) | : | utils_device.c (cryptsetup-2.4.0.tar.xz) | ||
---|---|---|---|---|
skipping to change at line 60 | skipping to change at line 60 | |||
int dev_fd_excl; | int dev_fd_excl; | |||
struct crypt_lock_handle *lh; | struct crypt_lock_handle *lh; | |||
unsigned int o_direct:1; | unsigned int o_direct:1; | |||
unsigned int init_done:1; /* path is bdev or loop already initialized */ | unsigned int init_done:1; /* path is bdev or loop already initialized */ | |||
/* cached values */ | /* cached values */ | |||
size_t alignment; | size_t alignment; | |||
size_t block_size; | size_t block_size; | |||
size_t loop_block_size; | ||||
}; | }; | |||
static size_t device_fs_block_size_fd(int fd) | static size_t device_fs_block_size_fd(int fd) | |||
{ | { | |||
size_t page_size = crypt_getpagesize(); | size_t page_size = crypt_getpagesize(); | |||
#ifdef HAVE_SYS_STATVFS_H | #ifdef HAVE_SYS_STATVFS_H | |||
struct statvfs buf; | struct statvfs buf; | |||
/* | /* | |||
skipping to change at line 114 | skipping to change at line 115 | |||
else | else | |||
*min_size = st.st_size; | *min_size = st.st_size; | |||
} else { | } else { | |||
/* block device must have at least one block */ | /* block device must have at least one block */ | |||
*min_size = bsize; | *min_size = bsize; | |||
} | } | |||
return 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) | static size_t device_alignment_fd(int devfd) | |||
{ | { | |||
long alignment = DEFAULT_MEM_ALIGNMENT; | long alignment = DEFAULT_MEM_ALIGNMENT; | |||
#ifdef _PC_REC_XFER_ALIGN | #ifdef _PC_REC_XFER_ALIGN | |||
alignment = fpathconf(devfd, _PC_REC_XFER_ALIGN); | alignment = fpathconf(devfd, _PC_REC_XFER_ALIGN); | |||
if (alignment < 0) | if (alignment < 0) | |||
alignment = DEFAULT_MEM_ALIGNMENT; | alignment = DEFAULT_MEM_ALIGNMENT; | |||
#endif | #endif | |||
return (size_t)alignment; | return (size_t)alignment; | |||
skipping to change at line 578 | skipping to change at line 596 | |||
device->block_size = device_block_size_fd(fd, NULL); | device->block_size = device_block_size_fd(fd, NULL); | |||
close(fd); | close(fd); | |||
} | } | |||
if (!device->block_size) | if (!device->block_size) | |||
log_dbg(cd, "Cannot get block size for device %s.", device_path(d evice)); | log_dbg(cd, "Cannot get block size for device %s.", device_path(d evice)); | |||
return device->block_size; | return device->block_size; | |||
} | } | |||
size_t device_optimal_encryption_sector_size(struct crypt_device *cd, struct dev | ||||
ice *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(d | ||||
evice)); | ||||
return SECTOR_SIZE; | ||||
} | ||||
if (device->block_size >= MAX_SECTOR_SIZE) { | ||||
close(fd); | ||||
return MISALIGNED(device->block_size, MAX_SECTOR_SIZE) ? SECTOR_S | ||||
IZE : 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_SI | ||||
ZE || phys_block_size > MAX_SECTOR_SIZE || MISALIGNED(phys_block_size, device->b | ||||
lock_size)) | ||||
return device->block_size; | ||||
return phys_block_size; | ||||
} | ||||
int device_read_ahead(struct device *device, uint32_t *read_ahead) | int device_read_ahead(struct device *device, uint32_t *read_ahead) | |||
{ | { | |||
int fd, r = 0; | int fd, r = 0; | |||
long read_ahead_long; | long read_ahead_long; | |||
if (!device) | if (!device) | |||
return 0; | return 0; | |||
if ((fd = open(device->path, O_RDONLY)) < 0) | if ((fd = open(device->path, O_RDONLY)) < 0) | |||
return 0; | return 0; | |||
skipping to change at line 782 | skipping to change at line 836 | |||
if (device->init_done) | if (device->init_done) | |||
return 0; | return 0; | |||
if (getuid() || geteuid()) { | if (getuid() || geteuid()) { | |||
log_err(cd, _("Cannot use a loopback device, " | log_err(cd, _("Cannot use a loopback device, " | |||
"running as non-root user.")); | "running as non-root user.")); | |||
return -ENOTSUP; | return -ENOTSUP; | |||
} | } | |||
log_dbg(cd, "Allocating a free loop device."); | 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. */ | /* 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, device->loop_block_size); | |||
if (loop_fd == -1) { | if (loop_fd == -1) { | |||
log_err(cd, _("Attaching loopback device failed " | log_err(cd, _("Attaching loopback device failed " | |||
"(loop device with autoclear flag is required).")); | "(loop device with autoclear flag is required).")); | |||
free(loop_device); | free(loop_device); | |||
return -EINVAL; | return -EINVAL; | |||
} | } | |||
file_path = device->path; | file_path = device->path; | |||
device->path = loop_device; | device->path = loop_device; | |||
r = device_ready(cd, device); | r = device_ready(cd, device); | |||
if (r < 0) { | if (r < 0) { | |||
device->path = file_path; | device->path = file_path; | |||
crypt_loop_detach(loop_device); | crypt_loop_detach(loop_device); | |||
free(loop_device); | free(loop_device); | |||
return r; | 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->loop_fd = loop_fd; | |||
device->file_path = file_path; | device->file_path = file_path; | |||
device->init_done = 1; | device->init_done = 1; | |||
return 0; | return 0; | |||
} | } | |||
int device_block_adjust(struct crypt_device *cd, | int device_block_adjust(struct crypt_device *cd, | |||
struct device *device, | struct device *device, | |||
enum devcheck device_check, | enum devcheck device_check, | |||
skipping to change at line 1023 | skipping to change at line 1080 | |||
device->ro_dev_fd = -1; | device->ro_dev_fd = -1; | |||
} | } | |||
if (device->dev_fd != -1) { | if (device->dev_fd != -1) { | |||
log_dbg(cd, "Closing read write fd for %s.", device_path(device)) ; | log_dbg(cd, "Closing read write fd for %s.", device_path(device)) ; | |||
if (close(device->dev_fd)) | if (close(device->dev_fd)) | |||
log_dbg(cd, "Failed to close read write fd for %s.", devi ce_path(device)); | log_dbg(cd, "Failed to close read write fd for %s.", devi ce_path(device)); | |||
device->dev_fd = -1; | device->dev_fd = -1; | |||
} | } | |||
} | } | |||
void device_set_block_size(struct device *device, size_t size) | ||||
{ | ||||
if (!device) | ||||
return; | ||||
device->loop_block_size = size; | ||||
} | ||||
End of changes. 7 change blocks. | ||||
2 lines changed or deleted | 66 lines changed or added |