Skip to contentSkip to conversation

Add new "dm_device_has_holders" fn to libdm

PATCH New Peter Rajnoha 2 participants 3 messages Sep 20, 2011 – Sep 21, 2011

lore
Share & tools
lore
#0 lore raw

PATCH 4/7 Add new "dm_device_has_holders" fn to libdm

This is a helper function used both in libdevmapper and in LVM to test whether there are any devices that make use of certain device given as parameter (this will partially replace "open_count" check, see the following patch...).

Peter

---
 libdm/libdevmapper.h |    6 ++++++
 libdm/libdm-common.c |   36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+), 0 deletions(-)
libdm/libdevmapper.h +6 -0
index ae39262..9df34bb 100644
@@ -267,6 +267,12 @@ const char *dm_sysfs_dir(void);
 int dm_is_dm_major(uint32_t major);
 
 /*
+ * Determine whether a device has holders.
+ * (this requires sysfs directory to be configured via dm_set_sysfs_dir)
+ */
+int dm_device_has_holders(uint32_t major, uint32_t minor);
+
+/*
  * Release library resources
  */
 void dm_lib_release(void);
libdm/libdm-common.c +36 -0
index 7517fb1..7ea78af 100644
@@ -1051,6 +1051,42 @@ const char *dm_sysfs_dir(void)
 	return _sysfs_dir;
 }
 
+static int _is_empty_dir(const char *dir)
+{
+	struct dirent *dirent;
+	DIR *d;
+
+	if (!(d = opendir(dir))) {
+		log_sys_error("opendir", dir);
+		return 0;
+	}
+
+	while ((dirent = readdir(d)))
+		if (strcmp(dirent->d_name, ".") && strcmp(dirent->d_name, ".."))
+			break;
+
+	if (closedir(d))
+		log_sys_error("closedir", dir);
+
+	return dirent ? 0 : 1;
+}
+
+int dm_device_has_holders(uint32_t major, uint32_t minor)
+{
+	char sysfs_path[PATH_MAX];
+
+	if (!*_sysfs_dir)
+		return 0;
+
+	if (dm_snprintf(sysfs_path, PATH_MAX, "%sdev/block/%" PRIu32
+			":%" PRIu32 "/holders", _sysfs_dir, major, minor) < 0) {
+		log_error("sysfs_path dm_snprintf failed");
+		return 0;
+	}
+
+	return !_is_empty_dir(sysfs_path);
+}
+
 int dm_mknodes(const char *name)
 {
 	struct dm_task *dmt;
#1 lore raw

PATCH 4/7 Add new "dm_device_has_holders" fn to libdm

Dne 20.9.2011 14:57, Peter Rajnoha napsal(a):

Quoted 39 lines

This is a helper function used both in libdevmapper and in LVM to test whether there are any devices that make use of certain device given as parameter (this will partially replace "open_count" check, see the following patch...).

Peter

---
 libdm/libdevmapper.h |    6 ++++++
 libdm/libdm-common.c |   36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+), 0 deletions(-)
libdm/libdevmapper.h +6 -0
index ae39262..9df34bb 100644
@@ -267,6 +267,12 @@ const char *dm_sysfs_dir(void);
 int dm_is_dm_major(uint32_t major);
 
 /*
+ * Determine whether a device has holders.
+ * (this requires sysfs directory to be configured via dm_set_sysfs_dir)
+ */
+int dm_device_has_holders(uint32_t major, uint32_t minor);
+
+/*
  * Release library resources
  */
 void dm_lib_release(void);
libdm/libdm-common.c +3 -0
index 7517fb1..7ea78af 100644
@@ -1051,6 +1051,42 @@ const char *dm_sysfs_dir(void)
 	return _sysfs_dir;
 }
 
+static int _is_empty_dir(const char *dir)
+{
+	struct dirent *dirent;

libdm/libdm-common.c const char *dm_sysfs_dir(void) · line 1066

Quoted 10 lines
libdm/libdm-common.c +10 -0
+	DIR *d;
+
+	if (!(d = opendir(dir))) {
+		log_sys_error("opendir", dir);
+		return 0;
+	}
+
+	while ((dirent = readdir(d)))
+		if (strcmp(dirent->d_name, ".") && strcmp(dirent->d_name, ".."))
+			break;

Hmm since readdir() may return -1 - this looks like error check is missing here. And since it's copy from lvm-file.c - maybe generic dm_is_empty_dir with all log_error code inside might be useful.

Zdenek

#2 lore raw

PATCH 4/7 Add new "dm_device_has_holders" fn to libdm

Quoted 11 lines · Zdenek Kabelac

Dne 20.9.2011 14:57, Peter Rajnoha napsal(a):

libdm/libdm-common.c const char *dm_sysfs_dir(void) · line 1066

Quoted 8 lines
libdm/libdm-common.c +8 -0
+	if (!(d = opendir(dir))) {
+		log_sys_error("opendir", dir);
+		return 0;
+	}
+
+	while ((dirent = readdir(d)))
+		if (strcmp(dirent->d_name, ".") && strcmp(dirent->d_name, ".."))
+			break;

Hmm since readdir() may return -1 - this looks like error check is missing here

Well, this is a version of readdir that returns either NULL or a pointer to a dirent structure - man 2 readdir vs. man 3 readdir (difference in POSIX compliance).

The POSIX version returns an error only if the DIR* stream descriptor is wrong, but this one should be fine since we already check the error in opendir just before this call. I think it's OK to do it this way.

Peter