X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=lib%2Futils_loop.c;h=9b316031bc524ff14687424468f533781a565208;hb=6497abd1df88001eb1f45f7348534911b33d05b5;hp=a1ed9600a1820f9a39037c91b859ab7a2bff091f;hpb=6361e86daf2725ae19a9a2f833d82410becb996d;p=platform%2Fupstream%2Fcryptsetup.git diff --git a/lib/utils_loop.c b/lib/utils_loop.c index a1ed960..9b31603 100644 --- a/lib/utils_loop.c +++ b/lib/utils_loop.c @@ -1,11 +1,13 @@ /* * loopback block device utilities * - * Copyright (C) 2011, Red Hat, Inc. All rights reserved. + * Copyright (C) 2009-2023 Red Hat, Inc. All rights reserved. + * Copyright (C) 2009-2023 Milan Broz * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License - * version 2 as published by the Free Software Foundation. + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -14,9 +16,10 @@ * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ +#include #include #include #include @@ -25,25 +28,51 @@ #include #include #include +#ifdef HAVE_SYS_SYSMACROS_H +# include /* for major, minor */ +#endif +#include #include #include "utils_loop.h" +#include "libcryptsetup_macros.h" + +#define LOOP_DEV_MAJOR 7 + +#ifndef LO_FLAGS_AUTOCLEAR +#define LO_FLAGS_AUTOCLEAR 4 +#endif #ifndef LOOP_CTL_GET_FREE -#define LOOP_CTL_GET_FREE 0x4C82 +#define LOOP_CTL_GET_FREE 0x4C82 +#endif + +#ifndef LOOP_SET_CAPACITY +#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 { + __u32 fd; + __u32 block_size; + struct loop_info64 info; + __u64 __reserved[8]; +}; #endif -char *crypt_loop_get_device(void) +static char *crypt_loop_get_device_old(void) { - char dev[20]; + char dev[64]; int i, loop_fd; - struct stat st; struct loop_info64 lo64 = {0}; for (i = 0; i < 256; i++) { sprintf(dev, "/dev/loop%d", i); - if (stat(dev, &st) || !S_ISBLK(st.st_mode)) - return NULL; loop_fd = open(dev, O_RDONLY); if (loop_fd < 0) @@ -60,9 +89,7 @@ char *crypt_loop_get_device(void) return NULL; } -/* loop-control not yet upstream */ -#if 0 -char *crypt_loop_get_device(void) +static char *crypt_loop_get_device(void) { char dev[64]; int i, loop_fd; @@ -70,7 +97,7 @@ char *crypt_loop_get_device(void) loop_fd = open("/dev/loop-control", O_RDONLY); if (loop_fd < 0) - return NULL; + return crypt_loop_get_device_old(); i = ioctl(loop_fd, LOOP_CTL_GET_FREE); if (i < 0) { @@ -79,51 +106,108 @@ char *crypt_loop_get_device(void) } close(loop_fd); - snprintf(dev, "/dev/loop%d", i, sizeof(dev)); + if (sprintf(dev, "/dev/loop%d", i) < 0) + return NULL; if (stat(dev, &st) || !S_ISBLK(st.st_mode)) return NULL; return strdup(dev); } -#endif -int crypt_loop_attach(const char *loop, const char *file, int offset, - int autoclear, int *readonly) +int crypt_loop_attach(char **loop, const char *file, int offset, + int autoclear, int *readonly, size_t blocksize) { - struct loop_info64 lo64 = {0}; + struct loop_config config = {0}; + char *lo_file_name; int loop_fd = -1, file_fd = -1, r = 1; + int fallback = 0; + + *loop = NULL; file_fd = open(file, (*readonly ? O_RDONLY : O_RDWR) | O_EXCL); - if (file_fd < 0 && errno == EROFS && !*readonly) { + if (file_fd < 0 && (errno == EROFS || errno == EACCES) && !*readonly) { *readonly = 1; file_fd = open(file, O_RDONLY | O_EXCL); } if (file_fd < 0) goto out; - loop_fd = open(loop, *readonly ? O_RDONLY : O_RDWR); - if (loop_fd < 0) - goto out; + config.fd = file_fd; - strncpy((char*)lo64.lo_file_name, file, LO_NAME_SIZE); - lo64.lo_offset = offset; + lo_file_name = (char*)config.info.lo_file_name; + lo_file_name[LO_NAME_SIZE-1] = '\0'; + strncpy(lo_file_name, file, LO_NAME_SIZE-1); + config.info.lo_offset = offset; if (autoclear) - lo64.lo_flags |= LO_FLAGS_AUTOCLEAR; + config.info.lo_flags |= LO_FLAGS_AUTOCLEAR; + if (blocksize > SECTOR_SIZE) + config.block_size = blocksize; - if (ioctl(loop_fd, LOOP_SET_FD, file_fd) < 0) - goto out; + while (loop_fd < 0) { + *loop = crypt_loop_get_device(); + if (!*loop) + goto out; - if (ioctl(loop_fd, LOOP_SET_STATUS64, &lo64) < 0) { - (void)ioctl(loop_fd, LOOP_CLR_FD, 0); - goto out; + loop_fd = open(*loop, *readonly ? O_RDONLY : O_RDWR); + if (loop_fd < 0) + goto out; + if (ioctl(loop_fd, LOOP_CONFIGURE, &config) < 0) { + if (errno == EINVAL || errno == ENOTTY) { + free(*loop); + *loop = NULL; + + close(loop_fd); + loop_fd = -1; + + /* kernel doesn't support LOOP_CONFIGURE */ + fallback = 1; + break; + } + if (errno != EBUSY) + goto out; + free(*loop); + *loop = NULL; + + close(loop_fd); + loop_fd = -1; + } + } + + if (fallback) { + while (loop_fd < 0) { + *loop = crypt_loop_get_device(); + if (!*loop) + goto out; + + loop_fd = open(*loop, *readonly ? O_RDONLY : O_RDWR); + if (loop_fd < 0) + goto out; + if (ioctl(loop_fd, LOOP_SET_FD, file_fd) < 0) { + if (errno != EBUSY) + goto out; + free(*loop); + *loop = NULL; + + close(loop_fd); + loop_fd = -1; + } + } + + 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; + } } /* Verify that autoclear is really set */ if (autoclear) { - memset(&lo64, 0, sizeof(lo64)); - if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) < 0 || - !(lo64.lo_flags & LO_FLAGS_AUTOCLEAR)) { + memset(&config.info, 0, sizeof(config.info)); + if (ioctl(loop_fd, LOOP_GET_STATUS64, &config.info) < 0 || + !(config.info.lo_flags & LO_FLAGS_AUTOCLEAR)) { (void)ioctl(loop_fd, LOOP_CLR_FD, 0); goto out; } @@ -135,6 +219,10 @@ out: close(loop_fd); if (file_fd >= 0) close(file_fd); + if (r && *loop) { + free(*loop); + *loop = NULL; + } return r ? -1 : loop_fd; } @@ -153,6 +241,21 @@ int crypt_loop_detach(const char *loop) return r; } +int crypt_loop_resize(const char *loop) +{ + int loop_fd = -1, r = 1; + + loop_fd = open(loop, O_RDONLY); + if (loop_fd < 0) + return 1; + + if (!ioctl(loop_fd, LOOP_SET_CAPACITY, 0)) + r = 0; + + close(loop_fd); + return r; +} + static char *_ioctl_backing_file(const char *loop) { struct loop_info64 lo64 = {0}; @@ -185,8 +288,9 @@ static char *_sysfs_backing_file(const char *loop) if (stat(loop, &st) || !S_ISBLK(st.st_mode)) return NULL; - snprintf(buf, sizeof(buf), "/sys/dev/block/%d:%d/loop/backing_file", - major(st.st_rdev), minor(st.st_rdev)); + if (snprintf(buf, sizeof(buf), "/sys/dev/block/%d:%d/loop/backing_file", + major(st.st_rdev), minor(st.st_rdev)) < 0) + return NULL; fd = open(buf, O_RDONLY); if (fd < 0) @@ -203,7 +307,12 @@ static char *_sysfs_backing_file(const char *loop) char *crypt_loop_backing_file(const char *loop) { - char *bf = _sysfs_backing_file(loop); + char *bf; + + if (!crypt_loop_device(loop)) + return NULL; + + bf = _sysfs_backing_file(loop); return bf ?: _ioctl_backing_file(loop); }