From: Karol Lewandowski Date: Fri, 24 Jul 2020 12:47:02 +0000 (+0200) Subject: Merge branch 'upstream' into tizen X-Git-Tag: accepted/tizen/unified/20211109.043025~4 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=6cd353b6260d3be1b8093534d74ce74f82f0040a;hp=-c;p=platform%2Fupstream%2Fcryptsetup.git Merge branch 'upstream' into tizen Change-Id: I93d730a6b0596450d95f8f32c947ed20573d3d0a --- 6cd353b6260d3be1b8093534d74ce74f82f0040a diff --combined lib/utils_loop.c index 640d95b,9696a40..681c123 --- a/lib/utils_loop.c +++ b/lib/utils_loop.c @@@ -1,8 -1,8 +1,8 @@@ /* * loopback block device utilities * - * Copyright (C) 2011-2012, Red Hat, Inc. All rights reserved. - * Copyright (C) 2009-2012, Milan Broz + * Copyright (C) 2009-2020 Red Hat, Inc. All rights reserved. + * Copyright (C) 2009-2020 Milan Broz * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License @@@ -19,6 -19,7 +19,7 @@@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ + #include #include #include #include @@@ -28,10 -29,10 +29,13 @@@ #include #include #include + #ifdef HAVE_SYS_SYSMACROS_H + # include /* for major, minor */ + #endif #include +#ifdef HAVE_SYS_SYSMACROS_H +#include /* for major, minor */ +#endif #include "utils_loop.h" @@@ -45,6 -46,10 +49,10 @@@ #define LOOP_CTL_GET_FREE 0x4C82 #endif + #ifndef LOOP_SET_CAPACITY + #define LOOP_SET_CAPACITY 0x4C07 + #endif + static char *crypt_loop_get_device_old(void) { char dev[20]; @@@ -69,7 -74,7 +77,7 @@@ return NULL; } - char *crypt_loop_get_device(void) + static char *crypt_loop_get_device(void) { char dev[64]; int i, loop_fd; @@@ -95,12 -100,15 +103,15 @@@ return strdup(dev); } - int crypt_loop_attach(const char *loop, const char *file, int offset, + int crypt_loop_attach(char **loop, const char *file, int offset, int autoclear, int *readonly) { struct loop_info64 lo64 = {0}; + char *lo_file_name; int loop_fd = -1, file_fd = -1, r = 1; + *loop = NULL; + file_fd = open(file, (*readonly ? O_RDONLY : O_RDWR) | O_EXCL); if (file_fd < 0 && (errno == EROFS || errno == EACCES) && !*readonly) { *readonly = 1; @@@ -109,18 -117,33 +120,33 @@@ if (file_fd < 0) goto out; - loop_fd = open(loop, *readonly ? O_RDONLY : O_RDWR); - if (loop_fd < 0) - goto out; + 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; - strncpy((char*)lo64.lo_file_name, file, LO_NAME_SIZE); + close(loop_fd); + loop_fd = -1; + } + } + + lo_file_name = (char*)lo64.lo_file_name; + lo_file_name[LO_NAME_SIZE-1] = '\0'; + strncpy(lo_file_name, file, LO_NAME_SIZE-1); lo64.lo_offset = offset; if (autoclear) lo64.lo_flags |= LO_FLAGS_AUTOCLEAR; - if (ioctl(loop_fd, LOOP_SET_FD, file_fd) < 0) - goto out; - if (ioctl(loop_fd, LOOP_SET_STATUS64, &lo64) < 0) { (void)ioctl(loop_fd, LOOP_CLR_FD, 0); goto out; @@@ -142,6 -165,10 +168,10 @@@ out close(loop_fd); if (file_fd >= 0) close(file_fd); + if (r && *loop) { + free(*loop); + *loop = NULL; + } return r ? -1 : loop_fd; } @@@ -160,6 -187,21 +190,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}; @@@ -210,7 -252,12 +255,12 @@@ static char *_sysfs_backing_file(const 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); }