File util-linux-loop-reuse-05.patch of Package util-linux.4136
From 211e1d4609d36081fc57dd31cad6806f51b06016 Mon Sep 17 00:00:00 2001
From: Stanislav Brabec <sbrabec@suse.cz>
Date: Thu, 14 Jul 2016 15:29:21 +0200
Subject: [PATCH 5/20] Implement loopcxt_check_conflict()
Add a function that searches for a possible conflicting (i. e. overlaying loop
device).
Signed-off-by: Stanislav Brabec <sbrabec@suse.cz>
---
include/loopdev.h | 3 +++
lib/loopdev.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 58 insertions(+)
Index: util-linux-2.25/include/loopdev.h
===================================================================
--- util-linux-2.25.orig/include/loopdev.h
+++ util-linux-2.25/include/loopdev.h
@@ -186,6 +186,9 @@ extern int loopcxt_find_by_backing_file(
const char *filename,
uint64_t offset, uint64_t sizelimit,
int flags);
+extern int loopcxt_check_conflict(struct loopdev_cxt *lc,
+ const char *filename,
+ uint64_t offset, uint64_t sizelimit);
extern int loopcxt_is_used(struct loopdev_cxt *lc,
struct stat *st,
Index: util-linux-2.25/lib/loopdev.c
===================================================================
--- util-linux-2.25.orig/lib/loopdev.c
+++ util-linux-2.25/lib/loopdev.c
@@ -1532,6 +1532,61 @@ int loopcxt_find_by_backing_file(struct
}
/*
+ * Returns: 0 = conflict, < 0 error, 1 no conflicting device
+ */
+int loopcxt_check_conflict(struct loopdev_cxt *lc, const char *filename,
+ uint64_t offset, uint64_t sizelimit)
+{
+ int rc, hasst;
+ struct stat st;
+
+ if (!filename)
+ return -EINVAL;
+
+ hasst = !stat(filename, &st);
+
+ rc = loopcxt_init_iterator(lc, LOOPITER_FL_USED);
+ if (rc)
+ return rc;
+
+ while ((rc = loopcxt_next(lc)) == 0) {
+ uint64_t lc_sizelimit, lc_offset;
+
+ rc = loopcxt_is_used(lc, hasst ? &st : NULL,
+ filename, offset, sizelimit, 0);
+ if (!rc)
+ continue;
+ if (rc != 1)
+ break;
+ DBG(lc, loopdev_debug("found %s backed by %s",
+ loopcxt_get_device(lc), filename));
+ rc = loopcxt_get_offset(lc, &lc_offset);
+ if (rc) {
+ DBG(lc, loopdev_debug("failed to get offset for device %s",
+ loopcxt_get_device(lc)));
+ break;
+ }
+ rc = loopcxt_get_sizelimit(lc, &lc_sizelimit);
+ if (rc) {
+ DBG(lc, loopdev_debug("failed to get sizelimit for device %s",
+ loopcxt_get_device(lc)));
+ break;
+ }
+
+ if (lc_sizelimit != 0 && offset >= lc_offset + lc_sizelimit)
+ continue;
+ if (sizelimit != 0 && offset + sizelimit <= lc_offset)
+ continue;
+ DBG(lc, loopdev_debug("overlapping loop device %s",
+ loopcxt_get_device(lc)));
+ rc = 0;
+ break;
+ }
+ loopcxt_deinit_iterator(lc);
+ return rc;
+}
+
+/*
* Returns allocated string with device name
*/
char *loopdev_find_by_backing_file(const char *filename, uint64_t offset, uint64_t sizelimit, int flags)