Originally reported on linux-btrfs list:
http://www.mail-archive.com/[email protected]/msg08086.html

Fix suggested on:
http://www.mail-archive.com/[email protected]/msg08386.html

loop_info.lo_name is limited to LO_NAME_SIZE (curerently 64) characters.
This can cause a problem if a file whose full path is longer than
LO_NAME_SIZE is currently mounted.

This patch changes resolve_loop_device() to:
* Check /sys/block/loopX/loop/backing_file first
* If that fails, fallback to original behaviour using loop_info.lo_name

Patch is both inline and attached (in case mail client mungles it).

Signed-off-by: Fajar A. Nugraha <[email protected]>
---
 utils.c |   18 +++++++++++++++++-
 1 files changed, 17 insertions(+), 1 deletions(-)

diff --git a/utils.c b/utils.c
index 178d1b9..a62eeee 100644
--- a/utils.c
+++ b/utils.c
@@ -649,6 +649,9 @@ int resolve_loop_device(const char* loop_dev,
char* loop_file, int max_len)
 {
        int loop_fd;
        int ret_ioctl;
+       int sysfs_fd;
+       char sysfs_path[PATH_MAX];
+       const char* sysfs_path_format = "/sys/block/loop%d/loop/backing_file";
        struct loop_info loopinfo;

        if ((loop_fd = open(loop_dev, O_RDONLY)) < 0)
@@ -658,7 +661,20 @@ int resolve_loop_device(const char* loop_dev,
char* loop_file, int max_len)
        close(loop_fd);

        if (ret_ioctl == 0)
-               strncpy(loop_file, loopinfo.lo_name, max_len);
+       {
+               snprintf(sysfs_path, PATH_MAX, sysfs_path_format, 
loopinfo.lo_number);
+               sysfs_fd = open(sysfs_path, O_RDONLY);
+               if (sysfs_fd < 0)
+               {
+                       strncpy(loop_file, loopinfo.lo_name, max_len);
+               }
+               else
+               {
+                       read(sysfs_fd, loop_file, max_len);
+                       loop_file[strlen(loop_file)-1] = '\0';
+                       close(sysfs_fd);
+               }
+       }
        else
                return -errno;

-- 
1.7.9
From e004166d8f3b30e0d498df995ac9de8b11cce59a Mon Sep 17 00:00:00 2001
From: "Fajar A. Nugraha" <[email protected]>
Date: Thu, 23 Feb 2012 13:28:33 +0700
Subject: [PATCH] Fix incorrect "error checking ... mount status" in
 mkfs.btrfs

Originally reported on linux-btrfs list:
http://www.mail-archive.com/[email protected]/msg08086.html

Fix suggested on:
http://www.mail-archive.com/[email protected]/msg08386.html

loop_info.lo_name is limited to LO_NAME_SIZE (curerently 64) characters.
This can cause a problem if a file whose full path is longer than
LO_NAME_SIZE is currently mounted.

This patch changes resolve_loop_device() to:
* Check /sys/block/loopX/loop/backing_file first
* If that fails, fallback to original behaviour using loop_info.lo_name

Signed-off-by: Fajar A. Nugraha <[email protected]>
---
 utils.c |   18 +++++++++++++++++-
 1 files changed, 17 insertions(+), 1 deletions(-)

diff --git a/utils.c b/utils.c
index 178d1b9..a62eeee 100644
--- a/utils.c
+++ b/utils.c
@@ -649,6 +649,9 @@ int resolve_loop_device(const char* loop_dev, char* loop_file, int max_len)
 {
 	int loop_fd;
 	int ret_ioctl;
+	int sysfs_fd;
+	char sysfs_path[PATH_MAX];
+	const char* sysfs_path_format = "/sys/block/loop%d/loop/backing_file";
 	struct loop_info loopinfo;
 
 	if ((loop_fd = open(loop_dev, O_RDONLY)) < 0)
@@ -658,7 +661,20 @@ int resolve_loop_device(const char* loop_dev, char* loop_file, int max_len)
 	close(loop_fd);
 
 	if (ret_ioctl == 0)
-		strncpy(loop_file, loopinfo.lo_name, max_len);
+	{
+		snprintf(sysfs_path, PATH_MAX, sysfs_path_format, loopinfo.lo_number);
+		sysfs_fd = open(sysfs_path, O_RDONLY);
+		if (sysfs_fd < 0)
+		{
+			strncpy(loop_file, loopinfo.lo_name, max_len);
+		}
+		else
+		{
+			read(sysfs_fd, loop_file, max_len);
+			loop_file[strlen(loop_file)-1] = '\0';
+			close(sysfs_fd);
+		}
+	}
 	else
 		return -errno;
 
-- 
1.7.9

Reply via email to