All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 5/7] Implement loopcxt_check_conflict()
@ 2016-07-14 13:29 Stanislav Brabec
  0 siblings, 0 replies; only message in thread
From: Stanislav Brabec @ 2016-07-14 13:29 UTC (permalink / raw)
  To: util-linux

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(+)

diff --git a/include/loopdev.h b/include/loopdev.h
index 57fcc1b..706388e 100644
--- a/include/loopdev.h
+++ b/include/loopdev.h
@@ -188,6 +188,9 @@ extern int loopcxt_find_by_backing_file(struct loopdev_cxt *lc,
 				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,
diff --git a/lib/loopdev.c b/lib/loopdev.c
index 2db80fc..4606003 100644
--- a/lib/loopdev.c
+++ b/lib/loopdev.c
@@ -1566,6 +1566,61 @@ int loopcxt_find_by_backing_file(struct loopdev_cxt *lc, const char *filename,
 }
 
 /*
+ * 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(CXT, ul_debugobj(lc, "found %s backed by %s",
+			loopcxt_get_device(lc), filename));
+		rc = loopcxt_get_offset(lc, &lc_offset);
+		if (rc) {
+			DBG(CXT, ul_debugobj(lc, "failed to get offset for device %s",
+				loopcxt_get_device(lc)));
+			break;
+		}
+		rc = loopcxt_get_sizelimit(lc, &lc_sizelimit);
+		if (rc) {
+			DBG(CXT, ul_debugobj(lc, "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(CXT, ul_debugobj(lc, "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)
-- 
2.9.0

-- 
Best Regards / S pozdravem,

Stanislav Brabec
software developer
---------------------------------------------------------------------
SUSE LINUX, s. r. o.                         e-mail: sbrabec@suse.com
Křižíkova 148/34 (Corso IIa)                  tel: +49 911 7405384547
186 00 Praha 8-Karlín                          fax:  +420 284 084 001
Czech Republic                                    http://www.suse.cz/
PGP: 830B 40D5 9E05 35D8 5E27 6FA3 717C 209F A04F CD76

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2016-07-14 13:29 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-07-14 13:29 [PATCH 5/7] Implement loopcxt_check_conflict() Stanislav Brabec

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.