summaryrefslogtreecommitdiffstats
path: root/mount/lomount.c
diff options
context:
space:
mode:
authorKarel Zak2010-12-01 14:04:35 +0100
committerKarel Zak2010-12-01 14:04:35 +0100
commitb61e939013282d8da2fbc2dc60c2cb4e58b8ab17 (patch)
tree12ccaa865bde2b30c138bcd974f530979404cb31 /mount/lomount.c
parentlscpu: use GPLv2+ (diff)
downloadkernel-qcow2-util-linux-b61e939013282d8da2fbc2dc60c2cb4e58b8ab17.tar.gz
kernel-qcow2-util-linux-b61e939013282d8da2fbc2dc60c2cb4e58b8ab17.tar.xz
kernel-qcow2-util-linux-b61e939013282d8da2fbc2dc60c2cb4e58b8ab17.zip
losetup: use /sys/dev/block/.../loop/backing_file
The basic loopdev attributes are available in sysfs since kernel 2.6.37. This patch uses the backing_file attribute from sysfs for very long filenames (the LOOP_GET_STATUS ioctl uses only 64 bytes for the filename). old version: # losetup -a /dev/loop0: [0804]:12865322 (/home/images/filesystems/this_is_really_really_long_directory_*) new version: # losetup -a /dev/loop0: [0804]:12865322 (/home/images/filesystems/this_is_really_really_long_directory_name/ext2.img) Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'mount/lomount.c')
-rw-r--r--mount/lomount.c44
1 files changed, 43 insertions, 1 deletions
diff --git a/mount/lomount.c b/mount/lomount.c
index d6556fc71..90149536a 100644
--- a/mount/lomount.c
+++ b/mount/lomount.c
@@ -80,6 +80,35 @@ struct looplist {
#define LLFLG_SUBDIR (1 << 5) /* /dev/loop/N */
#define LLFLG_DFLT (1 << 6) /* directly try to check default loops */
+/* TODO: move to lib/sysfs.c */
+static char *loopfile_from_sysfs(const char *device)
+{
+ FILE *f;
+ struct stat st;
+ char buf[PATH_MAX], *res = NULL;
+
+ if (stat(device, &st) || !S_ISBLK(st.st_mode))
+ return NULL;
+
+ snprintf(buf, sizeof(buf), _PATH_SYS_DEVBLOCK "/%d:%d/loop/backing_file",
+ major(st.st_rdev), minor(st.st_rdev));
+
+ f = fopen(buf, "r");
+ if (!f)
+ return NULL;
+
+ if (fgets(buf, sizeof(buf), f)) {
+ size_t sz = strlen(buf);
+ if (sz) {
+ buf[sz - 1] = '\0';
+ res = xstrdup(buf);
+ }
+ }
+
+ fclose(f);
+ return res;
+}
+
int
is_loop_device (const char *device) {
struct stat st;
@@ -379,13 +408,26 @@ show_loop_fd(int fd, char *device) {
if (ioctl(fd, LOOP_GET_STATUS64, &loopinfo64) == 0) {
+ char *lofile = NULL;
+
loopinfo64.lo_file_name[LO_NAME_SIZE-2] = '*';
loopinfo64.lo_file_name[LO_NAME_SIZE-1] = 0;
loopinfo64.lo_crypt_name[LO_NAME_SIZE-1] = 0;
+ /* ioctl has limited buffer for backing file name, since
+ * kernel 2.6.37 the filename is available in sysfs too
+ */
+ if (strlen((char *) loopinfo64.lo_file_name) == LO_NAME_SIZE - 1)
+ lofile = loopfile_from_sysfs(device);
+ if (!lofile)
+ lofile = (char *) loopinfo64.lo_file_name;
+
printf("%s: [%04" PRIx64 "]:%" PRIu64 " (%s)",
device, loopinfo64.lo_device, loopinfo64.lo_inode,
- loopinfo64.lo_file_name);
+ lofile);
+
+ if (lofile != (char *) loopinfo64.lo_file_name)
+ free(lofile);
if (loopinfo64.lo_offset)
printf(_(", offset %" PRIu64 ), loopinfo64.lo_offset);