summaryrefslogtreecommitdiffstats
path: root/mount/lomount.c
diff options
context:
space:
mode:
authorKarel Zak2007-09-11 14:35:34 +0200
committerKarel Zak2007-10-26 01:02:44 +0200
commit2368077223fa5800cf88659c9c57a7f6517f3fad (patch)
treef50e7a4fc58685921cd737f59b46f44183574083 /mount/lomount.c
parentlosetup: canonicalize loopfile name (diff)
downloadkernel-qcow2-util-linux-2368077223fa5800cf88659c9c57a7f6517f3fad.tar.gz
kernel-qcow2-util-linux-2368077223fa5800cf88659c9c57a7f6517f3fad.tar.xz
kernel-qcow2-util-linux-2368077223fa5800cf88659c9c57a7f6517f3fad.zip
mount: prevent loop mounting the same file twice
The mount syscall prevents mounting the same device twice to the same mountpoint. When loop mounting a file, for each file a new loop device gets allocated, which prevents the detection of loop mounting the same file to the same mountpoint twice. The patch adds a check to prevent double mounts, if the same loopfile is going to be mounted with the same offset to the same mountpoint. Co-Author: Matthias Koenig <mkoenig@suse.de> Signed-off-by: Matthias Koenig <mkoenig@suse.de> Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'mount/lomount.c')
-rw-r--r--mount/lomount.c108
1 files changed, 107 insertions, 1 deletions
diff --git a/mount/lomount.c b/mount/lomount.c
index b3c16fb6b..5bd895407 100644
--- a/mount/lomount.c
+++ b/mount/lomount.c
@@ -139,7 +139,7 @@ show_used_loop_devices (void) {
for (j = 0; j < SIZE(loop_formats); j++) {
for(i = 0; i < 256; i++) {
- sprintf(dev, loop_formats[j], i);
+ snprintf(dev, sizeof(dev), loop_formats[j], i);
if (stat (dev, &statbuf) == 0 && S_ISBLK(statbuf.st_mode)) {
fd = open (dev, O_RDONLY);
if (fd >= 0) {
@@ -165,6 +165,102 @@ show_used_loop_devices (void) {
#endif
+/* check if the loopfile is already associated with the same given
+ * parameters.
+ *
+ * returns: -1 error
+ * 0 unused
+ * 1 loop device already used
+ */
+static int
+is_associated(int dev, struct stat *file, unsigned long long offset)
+{
+ struct loop_info64 linfo64;
+ struct loop_info64 linfo;
+ int ret = 0;
+
+ if (ioctl(dev, LOOP_GET_STATUS64, &linfo64) == 0) {
+ if (file->st_dev == linfo64.lo_device &&
+ file->st_ino == linfo64.lo_inode &&
+ offset == linfo64.lo_offset)
+ ret = 1;
+ return ret;
+ }
+ if (ioctl(dev, LOOP_GET_STATUS, &linfo) == 0) {
+ if (file->st_dev == linfo.lo_device &&
+ file->st_ino == linfo.lo_inode &&
+ offset == linfo.lo_offset)
+ ret = 1;
+ return ret;
+ }
+
+ return errno == ENXIO ? 0 : -1;
+}
+
+/* check if the loop file is already used with the same given
+ * parameters. We check for device no, inode and offset.
+ * returns: associated devname or NULL
+ */
+char *
+loopfile_used (const char *filename, unsigned long long offset) {
+ char dev[20];
+ char *loop_formats[] = { "/dev/loop%d", "/dev/loop/%d" };
+ int i, j, fd;
+ struct stat devstat, filestat;
+ struct loop_info loopinfo;
+
+ if (stat(filename, &filestat) == -1) {
+ perror(filename);
+ return NULL;
+ }
+
+ for (j = 0; j < SIZE(loop_formats); j++) {
+ for(i = 0; i < 256; i++) {
+ snprintf(dev, sizeof(dev), loop_formats[j], i);
+ if (stat (dev, &devstat) == 0 && S_ISBLK(devstat.st_mode)) {
+ fd = open (dev, O_RDONLY);
+ if (fd >= 0) {
+ int res = 0;
+
+ if(ioctl (fd, LOOP_GET_STATUS, &loopinfo) == 0)
+ res = is_associated(fd, &filestat, offset);
+ close (fd);
+ if (res == 1)
+ return xstrdup(dev);
+ }
+ continue; /* continue trying as long as devices exist */
+ }
+ break;
+ }
+ }
+ return NULL;
+}
+
+int
+loopfile_used_with(char *devname, const char *filename, unsigned long long offset)
+{
+ struct stat statbuf;
+ int fd, ret;
+
+ if (!is_loop_device(devname))
+ return 0;
+
+ if (stat(filename, &statbuf) == -1) {
+ perror(filename);
+ return -1;
+ }
+
+ fd = open(devname, O_RDONLY);
+ if (fd == -1) {
+ perror(devname);
+ return -1;
+ }
+ ret = is_associated(fd, &statbuf, offset);
+
+ close(fd);
+ return ret;
+}
+
int
is_loop_device (const char *device) {
struct stat statbuf;
@@ -279,6 +375,16 @@ set_loop(const char *device, const char *file, unsigned long long offset,
char *pass;
char *filename;
+ if (verbose) {
+ char *xdev = loopfile_used(file, offset);
+
+ if (xdev) {
+ printf(_("warning: %s is already associated with %s\n"),
+ file, xdev);
+ free(xdev);
+ }
+ }
+
mode = (*loopro ? O_RDONLY : O_RDWR);
if ((ffd = open(file, mode)) < 0) {
if (!*loopro && errno == EROFS)