From: Mike Anderson <[email-masked]>

This patch adds support for the dm_path_event dm_send_event functions which
create and send udev events.

Signed-off-by: Mike Anderson <[email-masked]>
---

 Documentation/device-mapper/dm-uevent.txt |   87 +++++++++++++
 drivers/md/dm-uevent.c                    |  187 ++++++++++++++++++++++++++++++
 drivers/md/dm-uevent.h                    |   18 ++
 drivers/md/dm.c                           |   28 ++++
 include/linux/device-mapper.h             |    2 
 5 files changed, 322 insertions(+)

Index: linux-2.6.23/Documentation/device-mapper/dm-uevent.txt
===================================================================
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ linux-2.6.23/Documentation/device-mapper/dm-uevent.txt	2007-10-12 02:20:54.000000000 +0100
@@ -0,0 +1,87 @@
+The device-mapper uevent code adds the capability to device mapper to
+create and send kobject uevents. Previously events where only
+
+
+There are two functions currently for device mapper events. The first function
+listed creates the event and the second function sends the event.
+
+void dm_path_uevent(enum dm_uevent_type event_type, struct dm_table *t,
+                   const char *path, unsigned nr_valid_paths)
+
+void dm_send_uevents(struct list_head *events, struct kobject *kobj)
+
+
+The list below provides device mapper event specific variables added to the
+uevent environment.
+
+Variable Name: DM_ACTION
+Uevent Action(s): KOBJ_CHANGE
+Type: String
+Description:
+Value: Device mapper specific action that caused the uevent action.
+	PATH_FAILED - A path has failed.
+	PATH_REINSTATED - A path has been reinstated.
+
+Variable Name: DM_SEQNUM
+Uevent Action(s): KOBJ_CHANGE
+Type: unsigned integer
+Description: A sequence number for this specific device mapper device.
+Value: Valid unsigned integer range.
+
+Variable Name: DM_PATH
+Uevent Action(s): KOBJ_CHANGE
+Type: string
+Description: Major and minor number of the path device pertaining to this
+event.
+Value: Path name in the form of "Major:Minor"
+
+Variable Name: DM_NR_VALID_PATHS
+Uevent Action(s): KOBJ_CHANGE
+Type: unsigned integer
+Description:
+Value: Valid unsigned integer range.
+
+Variable Name: DM_NAME
+Uevent Action(s): KOBJ_CHANGE
+Type: string
+Description: Name of the device mapper device.
+Value: Name
+
+Variable Name: DM_UUID
+Uevent Action(s): KOBJ_CHANGE
+Type: string
+Description: UUID of the device mapper device.
+Value: UUID
+
+An example of the uevents generated as captured by udevmonitor is shown
+below.
+
+1.) Path failure.
+# UEVENT[1189606553.769166] change@/block/dm-0
+ACTION=change
+DEVPATH=/block/dm-0
+SUBSYSTEM=block
+DM_ACTION=PATH_FAILED
+DM_SEQNUM=1
+DM_PATH=8:16
+DM_NR_VALID_PATHS=1
+DM_NAME=3600a0b8000173c960000001845bf88b4
+DM_UUID=mpath-3600a0b8000173c960000001845bf88b4
+MINOR=0
+MAJOR=0
+SEQNUM=1699
+
+2.) Path reinstate.
+UEVENT[1189635862.724786] change@/block/dm-0
+ACTION=change
+DEVPATH=/block/dm-0
+SUBSYSTEM=block
+DM_ACTION=PATH_REINSTATED
+DM_SEQNUM=10
+DM_PATH=8:16
+DM_NR_VALID_PATHS=2
+DM_NAME=3600a0b8000173c960000001845bf88b4
+DM_UUID=mpath-3600a0b8000173c960000001845bf88b4
+MINOR=0
+MAJOR=0
+SEQNUM=1713
Index: linux-2.6.23/drivers/md/dm-uevent.c
===================================================================
--- linux-2.6.23.orig/drivers/md/dm-uevent.c	2007-10-12 02:20:52.000000000 +0100
+++ linux-2.6.23/drivers/md/dm-uevent.c	2007-10-12 02:21:36.000000000 +0100
@@ -27,6 +27,16 @@
 
 #define DM_MSG_PREFIX "uevent"
 
+static struct {
+	enum dm_uevent_type	type;
+	enum kobject_action	action;
+	char			*name;
+} dm_uevent_type_names[] = {
+	{DM_UEVENT_UNKNOWN, KOBJ_CHANGE, "UNKNOWN"},
+	{DM_UEVENT_PATH_FAILED, KOBJ_CHANGE, "PATH_FAILED"},
+	{DM_UEVENT_PATH_REINSTATED, KOBJ_CHANGE, "PATH_REINSTATED"},
+};
+
 static struct kmem_cache *_dm_event_cache;
 
 struct dm_uevent {
@@ -34,6 +44,8 @@ struct dm_uevent {
 	enum kobject_action action;
 	struct kobj_uevent_env ku_env;
 	struct list_head elist;
+	char name[DM_NAME_LEN];
+	char uuid[DM_UUID_LEN];
 };
 
 static void dm_uevent_free(struct dm_uevent *event)
@@ -55,6 +67,181 @@ static struct dm_uevent *dm_uevent_alloc
 	return event;
 }
 
+/*
+ * Note: This function is used instead of calling add_uevent_var directly
+ * as the hash cell fields are local to dm-ioctl.c and the name / uuid
+ * fields need to be copied under the hash cell lock
+ */
+static int dm_add_hc_var(struct dm_uevent *event)
+{
+	struct kobj_uevent_env *env = &event->ku_env;
+	int len, prefix_len;
+
+	prefix_len = snprintf(&env->buf[env->buflen], sizeof(env->buf) -
+		       env->buflen, "DM_NAME=");
+	if (prefix_len >= (sizeof(env->buf) - env->buflen)) {
+		DMERR("%s: buffer size too small", __FUNCTION__);
+		return -ENOMEM;
+	}
+
+	len = dm_copy_hc_name(event->md, &env->buf[env->buflen + prefix_len],
+				sizeof(env->buf) - env->buflen);
+	if (len) {
+		if (len < 0) {
+			DMERR("%s: dm_copy_hc_name() for DM_NAME failed",
+			      __FUNCTION__);
+			return len;
+		}
+
+		if (len >= (sizeof(env->buf) - env->buflen)) {
+			DMERR("%s: buffer size too small", __FUNCTION__);
+			return -ENOMEM;
+		}
+		env->envp[env->envp_idx++] = &env->buf[env->buflen];
+		env->buflen += prefix_len + len + 1;
+	}
+
+	prefix_len = snprintf(&env->buf[env->buflen], sizeof(env->buf) -
+		       env->buflen, "DM_UUID=");
+	if (prefix_len >= (sizeof(env->buf) - env->buflen)) {
+		DMERR("%s: buffer size too small", __FUNCTION__);
+		return -ENOMEM;
+	}
+
+	len = dm_copy_hc_uuid(event->md, &env->buf[env->buflen + prefix_len],
+				  sizeof(env->buf) - env->buflen);
+	if (len) {
+		if (len < 0) {
+			DMERR("%s: dm_copy_hc_uuid() for DM_UUID failed",
+			      __FUNCTION__);
+			return len;
+		}
+
+		if (len >= (sizeof(env->buf) - env->buflen)) {
+			DMERR("%s: buffer size too small", __FUNCTION__);
+			return -ENOMEM;
+		}
+		env->envp[env->envp_idx++] = &env->buf[env->buflen];
+		env->buflen += prefix_len + len + 1;
+	}
+
+	return 0;
+
+}
+
+static struct dm_uevent *dm_build_path_uevent(struct mapped_device *md,
+					enum kobject_action action,
+					const char *dm_action,
+					const char *path,
+					unsigned nr_valid_paths)
+{
+	struct dm_uevent *event;
+
+	event = dm_uevent_alloc(md);
+	if (!event) {
+		DMERR("%s: dm_uevent_alloc() failed", __FUNCTION__);
+		goto out_nomem;
+	}
+
+	event->action = action;
+
+	if (add_uevent_var(&event->ku_env, "DM_ACTION=%s", dm_action)) {
+		DMERR("%s: add_uevent_var() for DM_ACTION failed",
+		      __FUNCTION__);
+		goto out_add;
+	}
+
+	if (add_uevent_var(&event->ku_env, "DM_SEQNUM=%u",
+			   dm_next_uevent_seq(md))) {
+		DMERR("%s: add_uevent_var() for DM_SEQNUM failed",
+		      __FUNCTION__);
+		goto out_add;
+	}
+
+	if (add_uevent_var(&event->ku_env, "DM_PATH=%s", path)) {
+		DMERR("%s: add_uevent_var() for DM_PATH failed",
+		      __FUNCTION__);
+		goto out_add;
+	}
+
+	if (add_uevent_var(&event->ku_env, "DM_NR_VALID_PATHS=%d",
+			   nr_valid_paths)) {
+		DMERR("%s: add_uevent_var() for DM_NR_VALID_PATHS failed",
+		      __FUNCTION__);
+		goto out_add;
+	}
+
+	return event;
+
+out_add:
+	dm_uevent_free(event);
+out_nomem:
+	return ERR_PTR(-ENOMEM);
+}
+
+/**
+ * dm_send_uevents - send uevents for given list
+ *
+ * @events:	list of events to send
+ * @kobj:	kobject generating event
+ *
+ **/
+void dm_send_uevents(struct list_head *events, struct kobject *kobj)
+{
+	int r;
+	struct dm_uevent *event, *next;
+
+	list_for_each_entry_safe(event, next, events, elist) {
+		list_del_init(&event->elist);
+
+		/*
+		 * Need to call dm_add_hc_var from here for now. Context
+		 * of previous var adds and locking used for hash_cell not
+		 * compatable.
+		 */
+		if (dm_add_hc_var(event)) {
+			DMERR("%s: dm_add_hc_var() failed", __FUNCTION__);
+			dm_uevent_free(event);
+			continue;
+		}
+
+		r = kobject_uevent_env(kobj, event->action, event->ku_env.envp);
+		if (r)
+			DMERR("%s: kobject_uevent_env failed", __FUNCTION__);
+		dm_uevent_free(event);
+	}
+}
+EXPORT_SYMBOL_GPL(dm_send_uevents);
+
+/**
+ * dm_path_uevent - called to create a new path event and queue it
+ *
+ * @event_type:	path event type enum
+ * @t:			pointer to a dm_table
+ * @path:		string containing pathname
+ * @nr_valid_paths:	number of valid paths remaining
+ *
+ **/
+void dm_path_uevent(enum dm_uevent_type event_type, struct dm_table *t,
+		   const char *path, unsigned nr_valid_paths)
+{
+	struct mapped_device *md = dm_table_get_md(t);
+	struct dm_uevent *event;
+
+	if (event_type < ARRAY_SIZE(dm_uevent_type_names)) {
+		event = dm_build_path_uevent(md,
+				   dm_uevent_type_names[event_type].action,
+				   dm_uevent_type_names[event_type].name,
+				   path,
+				   nr_valid_paths);
+		if (!IS_ERR(event))
+			dm_uevent_add(md, &event->elist);
+	} else
+		DMERR("%s: Invalid event_type %d", __FUNCTION__, event_type);
+	dm_put(md);
+}
+EXPORT_SYMBOL_GPL(dm_path_uevent);
+
 int dm_uevent_init(void)
 {
 	_dm_event_cache = KMEM_CACHE(dm_uevent, 0);
Index: linux-2.6.23/drivers/md/dm-uevent.h
===================================================================
--- linux-2.6.23.orig/drivers/md/dm-uevent.h	2007-10-12 02:20:52.000000000 +0100
+++ linux-2.6.23/drivers/md/dm-uevent.h	2007-10-12 02:20:54.000000000 +0100
@@ -21,10 +21,19 @@
 #ifndef DM_UEVENT_H
 #define DM_UEVENT_H
 
+enum dm_uevent_type {
+	DM_UEVENT_UNKNOWN,
+	DM_UEVENT_PATH_FAILED,
+	DM_UEVENT_PATH_REINSTATED,
+};
+
 #ifdef CONFIG_DM_UEVENT
 
 extern int dm_uevent_init(void);
 extern void dm_uevent_exit(void);
+extern void dm_send_uevents(struct list_head *events, struct kobject *kobj);
+extern void dm_path_uevent(enum dm_uevent_type event_type, struct dm_table *t,
+			   const char *path, unsigned nr_valid_paths);
 
 #else
 
@@ -35,6 +44,15 @@ static inline int dm_uevent_init(void)
 static inline void dm_uevent_exit(void)
 {
 }
+static inline void dm_send_uevents(struct list_head *events,
+				   struct kobject *kobj)
+{
+}
+static inline void dm_path_uevent(enum dm_uevent_type event_type,
+				  struct dm_table *t, const char *path,
+				  unsigned nr_valid_paths)
+{
+}
 
 #endif	/* CONFIG_DM_UEVENT */
 
Index: linux-2.6.23/drivers/md/dm.c
===================================================================
--- linux-2.6.23.orig/drivers/md/dm.c	2007-10-12 02:20:52.000000000 +0100
+++ linux-2.6.23/drivers/md/dm.c	2007-10-12 02:20:54.000000000 +0100
@@ -113,6 +113,9 @@ struct mapped_device {
 	 */
 	atomic_t event_nr;
 	wait_queue_head_t eventq;
+	atomic_t uevent_seq;
+	struct list_head uevent_list;
+	spinlock_t uevent_lock; /* Protect access to uevent_list */
 
 	/*
 	 * freeze/thaw support require holding onto a super block
@@ -1048,6 +1051,9 @@ static struct mapped_device *alloc_dev(i
 	atomic_set(&md->holders, 1);
 	atomic_set(&md->open_count, 0);
 	atomic_set(&md->event_nr, 0);
+	atomic_set(&md->uevent_seq, 0);
+	INIT_LIST_HEAD(&md->uevent_list);
+	spin_lock_init(&md->uevent_lock);
 
 	md->queue = blk_alloc_queue(GFP_KERNEL);
 	if (!md->queue)
@@ -1148,8 +1154,16 @@ static void free_dev(struct mapped_devic
  */
 static void event_callback(void *context)
 {
+	unsigned long flags;
+	LIST_HEAD(uevents);
 	struct mapped_device *md = (struct mapped_device *) context;
 
+	spin_lock_irqsave(&md->uevent_lock, flags);
+	list_splice_init(&md->uevent_list, &uevents);
+	spin_unlock_irqrestore(&md->uevent_lock, flags);
+
+	dm_send_uevents(&uevents, &md->disk->kobj);
+
 	atomic_inc(&md->event_nr);
 	wake_up(&md->eventq);
 }
@@ -1567,6 +1581,11 @@ out:
 /*-----------------------------------------------------------------
  * Event notification.
  *---------------------------------------------------------------*/
+uint32_t dm_next_uevent_seq(struct mapped_device *md)
+{
+	return atomic_add_return(1, &md->uevent_seq);
+}
+
 uint32_t dm_get_event_nr(struct mapped_device *md)
 {
 	return atomic_read(&md->event_nr);
@@ -1578,6 +1597,15 @@ int dm_wait_event(struct mapped_device *
 			(event_nr != atomic_read(&md->event_nr)));
 }
 
+void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(&md->uevent_lock, flags);
+	list_add(elist, &md->uevent_list);
+	spin_unlock_irqrestore(&md->uevent_lock, flags);
+}
+
 /*
  * The gendisk is only valid as long as you have a reference
  * count on 'md'.
Index: linux-2.6.23/include/linux/device-mapper.h
===================================================================
--- linux-2.6.23.orig/include/linux/device-mapper.h	2007-10-12 02:20:52.000000000 +0100
+++ linux-2.6.23/include/linux/device-mapper.h	2007-10-12 02:20:54.000000000 +0100
@@ -189,6 +189,8 @@ int dm_resume(struct mapped_device *md);
  */
 uint32_t dm_get_event_nr(struct mapped_device *md);
 int dm_wait_event(struct mapped_device *md, int event_nr);
+uint32_t dm_next_uevent_seq(struct mapped_device *md);
+void dm_uevent_add(struct mapped_device *md, struct list_head *elist);
 
 /*
  * Info functions.