diff mbox

[02/13] Btrfs-progs: fix resolving of loop devices

Message ID 1358715858-4469-3-git-send-email-gene@czarc.net (mailing list archive)
State New, archived
Headers show

Commit Message

Gene Czarcinski Jan. 20, 2013, 9:04 p.m. UTC
From: Nirbheek Chauhan <nirbheek.chauhan@collabora.co.uk>

The LOOP_GET_STATUS ioctl truncates filenames to 64 characters. We should get
the backing file for a given loop device from /sys/. This is how losetup does it
as well.

Signed-off-by: Gene Czarcinski <gene@czarc.net>
---
 utils.c | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)

Comments

David Sterba Jan. 21, 2013, 4:14 p.m. UTC | #1
On Sun, Jan 20, 2013 at 04:04:07PM -0500, Gene Czarcinski wrote:
> From: Nirbheek Chauhan <nirbheek.chauhan@collabora.co.uk>
> 
> The LOOP_GET_STATUS ioctl truncates filenames to 64 characters. We should get
> the backing file for a given loop device from /sys/. This is how losetup does it
> as well.
> 
> Signed-off-by: Gene Czarcinski <gene@czarc.net>

I'd expect Nirbheek's signed off here as well and I've noticed

Tested-By: Hector Oron <hector.oron@collabora.co.uk>
(https://patchwork.kernel.org/patch/1629581/)

Are you guys ok with adding them?

david
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
David Sterba Jan. 22, 2013, 5:50 p.m. UTC | #2
On Sun, Jan 20, 2013 at 04:04:07PM -0500, Gene Czarcinski wrote:
> From: Nirbheek Chauhan <nirbheek.chauhan@collabora.co.uk>
> 
> The LOOP_GET_STATUS ioctl truncates filenames to 64 characters. We should get
> the backing file for a given loop device from /sys/. This is how losetup does it
> as well.

For the record, there was another patch fixing a loop device issue

https://patchwork.kernel.org/patch/1861831/

from Michael (CCed), that switched from LOOP_GET_STATUS to
LOOP_GET_STATUS64, but afaics both types of the ioctl return at most 64
characters, so parsing /sys appears to be the way to go.

david
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/utils.c b/utils.c
index 51b78d5..7953ef9 100644
--- a/utils.c
+++ b/utils.c
@@ -20,6 +20,7 @@ 
 #define __USE_XOPEN2K
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #ifndef __CHECKER__
 #include <sys/ioctl.h>
 #include <sys/mount.h>
@@ -653,21 +654,22 @@  int is_loop_device (const char* device) {
  * the associated file (e.g. /images/my_btrfs.img) */
 int resolve_loop_device(const char* loop_dev, char* loop_file, int max_len)
 {
-	int loop_fd;
-	int ret_ioctl;
-	struct loop_info loopinfo;
+	int ret;
+	FILE *f;
+	char fmt[20];
+	char p[PATH_MAX];
+	char real_loop_dev[PATH_MAX];
 
-	if ((loop_fd = open(loop_dev, O_RDONLY)) < 0)
+	if (!realpath(loop_dev, real_loop_dev))
+		return -errno;
+	snprintf(p, PATH_MAX, "/sys/block/%s/loop/backing_file", strrchr(real_loop_dev, '/'));
+	if (!(f = fopen(p, "r")))
 		return -errno;
 
-	ret_ioctl = ioctl(loop_fd, LOOP_GET_STATUS, &loopinfo);
-	close(loop_fd);
-
-	if (ret_ioctl == 0) {
-		strncpy(loop_file, loopinfo.lo_name, max_len);
-		if (max_len > 0)
-			loop_file[max_len-1] = 0;
-	} else
+	snprintf(fmt, 20, "%%%i[^\n]", max_len-1);
+	ret = fscanf(f, fmt, loop_file);
+	fclose(f);
+	if (ret == EOF)
 		return -errno;
 
 	return 0;