* [PATCH] Fix partitioned loop devices resolve.
@ 2015-11-09 9:11 Florian Margaine
0 siblings, 0 replies; only message in thread
From: Florian Margaine @ 2015-11-09 9:11 UTC (permalink / raw)
To: linux-btrfs
[-- Attachment #1: Type: text/plain, Size: 1476 bytes --]
When using partitions on a loop device, the device's name can be
e.g. /dev/loop0p1 or similar, and no relevant entry exists in the /sys
filesystem, so the current resolve_loop_device function fails.
Instead of using string functions to extract the device name and reading
this file, this patch uses the loop device API through ioctl to get the
correct backing file.
Signed-off-by: Florian Margaine <florian@platform.sh>
---
utils.c | 18 ++++++------------
1 file changed, 6 insertions(+), 12 deletions(-)
diff --git a/utils.c b/utils.c
index d546bea..73a05e1 100644
--- a/utils.c
+++ b/utils.c
@@ -1176,22 +1176,16 @@ static int is_loop_device (const char* device) {
static int resolve_loop_device(const char* loop_dev, char* loop_file,
int max_len)
{
- int ret;
- FILE *f;
- char fmt[20];
- char p[PATH_MAX];
- char real_loop_dev[PATH_MAX];
+ int fd;
+ struct loop_info64 lo64;
- if (!realpath(loop_dev, real_loop_dev))
+ if (!(fd = open(loop_dev, O_RDONLY)))
return -errno;
- snprintf(p, PATH_MAX, "/sys/block/%s/loop/backing_file",
strrchr(real_loop_dev, '/'));
- if (!(f = fopen(p, "r")))
+ if (ioctl(fd, LOOP_GET_STATUS64, &lo64) != 0)
return -errno;
- snprintf(fmt, 20, "%%%i[^\n]", max_len-1);
- ret = fscanf(f, fmt, loop_file);
- fclose(f);
- if (ret == EOF)
+ memcpy(loop_file, lo64.lo_file_name, strlen(lo64.lo_file_name) + 1);
+ if (close(fd) != 0)
return -errno;
return 0;
--
2.6.1
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 473 bytes --]
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2015-11-09 9:11 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-11-09 9:11 [PATCH] Fix partitioned loop devices resolve Florian Margaine
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.