linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Btrfs-progs: loopback files cannot be used if the path is too long
@ 2012-10-12 13:55 nirbheek.chauhan
  2012-10-12 13:55 ` [PATCH] Btrfs-progs: fix resolving of loop devices nirbheek.chauhan
  0 siblings, 1 reply; 3+ messages in thread
From: nirbheek.chauhan @ 2012-10-12 13:55 UTC (permalink / raw)
  To: linux-btrfs; +Cc: Nirbheek Chauhan

From: Nirbheek Chauhan <nirbheek.chauhan@collabora.co.uk>

If the path to a given loopback file is longer than 64 characters, none of the
Btrfs-progs tools can use it. This is because the size of loopinfo.lo_name
returned by the LOOP_GET_STATUS ioctl is 64.

The attached patch fixes this by fetching the backing file for a loopback device
from /sys/block; which is how `losetup` from util-linux does it as well.

Nirbheek Chauhan (1):
  Btrfs-progs: fix resolving of loop devices

 utils.c |   26 ++++++++++++++------------
 1 files changed, 14 insertions(+), 12 deletions(-)

-- 
1.7.8.6


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH] Btrfs-progs: fix resolving of loop devices
  2012-10-12 13:55 [PATCH] Btrfs-progs: loopback files cannot be used if the path is too long nirbheek.chauhan
@ 2012-10-12 13:55 ` nirbheek.chauhan
  2012-10-23  9:53   ` Hector Oron
  0 siblings, 1 reply; 3+ messages in thread
From: nirbheek.chauhan @ 2012-10-12 13:55 UTC (permalink / raw)
  To: linux-btrfs; +Cc: Nirbheek Chauhan

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.
---
 utils.c |   26 ++++++++++++++------------
 1 files changed, 14 insertions(+), 12 deletions(-)

diff --git a/utils.c b/utils.c
index 205e667..cdd6f7d 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>
@@ -651,21 +652,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;
-- 
1.7.8.6


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH] Btrfs-progs: fix resolving of loop devices
  2012-10-12 13:55 ` [PATCH] Btrfs-progs: fix resolving of loop devices nirbheek.chauhan
@ 2012-10-23  9:53   ` Hector Oron
  0 siblings, 0 replies; 3+ messages in thread
From: Hector Oron @ 2012-10-23  9:53 UTC (permalink / raw)
  To: linux-btrfs; +Cc: nirbheek.chauhan

From: Nirbheek Chauhan <nirbheek.chauhan <at> collabora.co.uk>

If the path to a given loopback file is longer than 64 characters, none of the
Btrfs-progs tools can use it. This is because the size of loopinfo.lo_name
returned by the LOOP_GET_STATUS ioctl is 64.

The attached patch fixes this by fetching the backing file for a loopback device
from /sys/block; which is how `losetup` from util-linux does it as well.

Nirbheek Chauhan (1):
  Btrfs-progs: fix resolving of loop devices

 utils.c |   26 ++++++++++++++------------
 1 files changed, 14 insertions(+), 12 deletions(-)

Tested-By: Hector Oron <hector.oron@collabora.co.uk>

diff --git a/utils.c b/utils.c
index 205e667..cdd6f7d 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>
@@ -651,21 +652,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;
-- 
1.7.8.6

^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2012-10-23  9:54 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-10-12 13:55 [PATCH] Btrfs-progs: loopback files cannot be used if the path is too long nirbheek.chauhan
2012-10-12 13:55 ` [PATCH] Btrfs-progs: fix resolving of loop devices nirbheek.chauhan
2012-10-23  9:53   ` Hector Oron

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).