1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Device Mapper Uevent Support (dm-uevent)
5 * Copyright IBM Corporation, 2007
6 * Author: Mike Anderson <andmike@linux.vnet.ibm.com>
8 #include <linux/list.h>
9 #include <linux/slab.h>
10 #include <linux/kobject.h>
11 #include <linux/dm-ioctl.h>
12 #include <linux/export.h>
15 #include "dm-uevent.h"
17 #define DM_MSG_PREFIX "uevent"
20 enum dm_uevent_type type;
21 enum kobject_action action;
23 } _dm_uevent_type_names[] = {
24 {DM_UEVENT_PATH_FAILED, KOBJ_CHANGE, "PATH_FAILED"},
25 {DM_UEVENT_PATH_REINSTATED, KOBJ_CHANGE, "PATH_REINSTATED"},
28 static struct kmem_cache *_dm_event_cache;
31 struct mapped_device *md;
32 enum kobject_action action;
33 struct kobj_uevent_env ku_env;
34 struct list_head elist;
35 char name[DM_NAME_LEN];
36 char uuid[DM_UUID_LEN];
39 static void dm_uevent_free(struct dm_uevent *event)
41 kmem_cache_free(_dm_event_cache, event);
44 static struct dm_uevent *dm_uevent_alloc(struct mapped_device *md)
46 struct dm_uevent *event;
48 event = kmem_cache_zalloc(_dm_event_cache, GFP_ATOMIC);
52 INIT_LIST_HEAD(&event->elist);
58 static struct dm_uevent *dm_build_path_uevent(struct mapped_device *md,
60 enum kobject_action action,
61 const char *dm_action,
63 unsigned int nr_valid_paths)
65 struct dm_uevent *event;
67 event = dm_uevent_alloc(md);
69 DMERR("%s: dm_uevent_alloc() failed", __func__);
73 event->action = action;
75 if (add_uevent_var(&event->ku_env, "DM_TARGET=%s", ti->type->name)) {
76 DMERR("%s: add_uevent_var() for DM_TARGET failed",
81 if (add_uevent_var(&event->ku_env, "DM_ACTION=%s", dm_action)) {
82 DMERR("%s: add_uevent_var() for DM_ACTION failed",
87 if (add_uevent_var(&event->ku_env, "DM_SEQNUM=%u",
88 dm_next_uevent_seq(md))) {
89 DMERR("%s: add_uevent_var() for DM_SEQNUM failed",
94 if (add_uevent_var(&event->ku_env, "DM_PATH=%s", path)) {
95 DMERR("%s: add_uevent_var() for DM_PATH failed", __func__);
99 if (add_uevent_var(&event->ku_env, "DM_NR_VALID_PATHS=%d",
101 DMERR("%s: add_uevent_var() for DM_NR_VALID_PATHS failed",
109 dm_uevent_free(event);
111 return ERR_PTR(-ENOMEM);
115 * dm_send_uevents - send uevents for given list
117 * @events: list of events to send
118 * @kobj: kobject generating event
121 void dm_send_uevents(struct list_head *events, struct kobject *kobj)
124 struct dm_uevent *event, *next;
126 list_for_each_entry_safe(event, next, events, elist) {
127 list_del_init(&event->elist);
130 * When a device is being removed this copy fails and we
131 * discard these unsent events.
133 if (dm_copy_name_and_uuid(event->md, event->name,
135 DMINFO("%s: skipping sending uevent for lost device",
140 if (add_uevent_var(&event->ku_env, "DM_NAME=%s", event->name)) {
141 DMERR("%s: add_uevent_var() for DM_NAME failed",
146 if (add_uevent_var(&event->ku_env, "DM_UUID=%s", event->uuid)) {
147 DMERR("%s: add_uevent_var() for DM_UUID failed",
152 r = kobject_uevent_env(kobj, event->action, event->ku_env.envp);
154 DMERR("%s: kobject_uevent_env failed", __func__);
156 dm_uevent_free(event);
159 EXPORT_SYMBOL_GPL(dm_send_uevents);
162 * dm_path_uevent - called to create a new path event and queue it
164 * @event_type: path event type enum
165 * @ti: pointer to a dm_target
166 * @path: string containing pathname
167 * @nr_valid_paths: number of valid paths remaining
170 void dm_path_uevent(enum dm_uevent_type event_type, struct dm_target *ti,
171 const char *path, unsigned int nr_valid_paths)
173 struct mapped_device *md = dm_table_get_md(ti->table);
174 struct dm_uevent *event;
176 if (event_type >= ARRAY_SIZE(_dm_uevent_type_names)) {
177 DMERR("%s: Invalid event_type %d", __func__, event_type);
181 event = dm_build_path_uevent(md, ti,
182 _dm_uevent_type_names[event_type].action,
183 _dm_uevent_type_names[event_type].name,
184 path, nr_valid_paths);
188 dm_uevent_add(md, &event->elist);
190 EXPORT_SYMBOL_GPL(dm_path_uevent);
192 int dm_uevent_init(void)
194 _dm_event_cache = KMEM_CACHE(dm_uevent, 0);
195 if (!_dm_event_cache)
198 DMINFO("version 1.0.3");
203 void dm_uevent_exit(void)
205 kmem_cache_destroy(_dm_event_cache);