Merge tag 'trace-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[linux/fpc-iii.git] / drivers / md / dm-uevent.c
blob8671267200d88375fb498ee9379ea74123e747e7
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Device Mapper Uevent Support (dm-uevent)
5 * Copyright IBM Corporation, 2007
6 * Author: Mike Anderson <andmike@linux.vnet.ibm.com>
7 */
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>
14 #include "dm.h"
15 #include "dm-uevent.h"
17 #define DM_MSG_PREFIX "uevent"
19 static const struct {
20 enum dm_uevent_type type;
21 enum kobject_action action;
22 char *name;
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;
30 struct dm_uevent {
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);
49 if (!event)
50 return NULL;
52 INIT_LIST_HEAD(&event->elist);
53 event->md = md;
55 return event;
58 static struct dm_uevent *dm_build_path_uevent(struct mapped_device *md,
59 struct dm_target *ti,
60 enum kobject_action action,
61 const char *dm_action,
62 const char *path,
63 unsigned nr_valid_paths)
65 struct dm_uevent *event;
67 event = dm_uevent_alloc(md);
68 if (!event) {
69 DMERR("%s: dm_uevent_alloc() failed", __func__);
70 goto err_nomem;
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",
77 __func__);
78 goto err_add;
81 if (add_uevent_var(&event->ku_env, "DM_ACTION=%s", dm_action)) {
82 DMERR("%s: add_uevent_var() for DM_ACTION failed",
83 __func__);
84 goto err_add;
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",
90 __func__);
91 goto err_add;
94 if (add_uevent_var(&event->ku_env, "DM_PATH=%s", path)) {
95 DMERR("%s: add_uevent_var() for DM_PATH failed", __func__);
96 goto err_add;
99 if (add_uevent_var(&event->ku_env, "DM_NR_VALID_PATHS=%d",
100 nr_valid_paths)) {
101 DMERR("%s: add_uevent_var() for DM_NR_VALID_PATHS failed",
102 __func__);
103 goto err_add;
106 return event;
108 err_add:
109 dm_uevent_free(event);
110 err_nomem:
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)
123 int r;
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,
134 event->uuid)) {
135 DMINFO("%s: skipping sending uevent for lost device",
136 __func__);
137 goto uevent_free;
140 if (add_uevent_var(&event->ku_env, "DM_NAME=%s", event->name)) {
141 DMERR("%s: add_uevent_var() for DM_NAME failed",
142 __func__);
143 goto uevent_free;
146 if (add_uevent_var(&event->ku_env, "DM_UUID=%s", event->uuid)) {
147 DMERR("%s: add_uevent_var() for DM_UUID failed",
148 __func__);
149 goto uevent_free;
152 r = kobject_uevent_env(kobj, event->action, event->ku_env.envp);
153 if (r)
154 DMERR("%s: kobject_uevent_env failed", __func__);
155 uevent_free:
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 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);
178 return;
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);
185 if (IS_ERR(event))
186 return;
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)
196 return -ENOMEM;
198 DMINFO("version 1.0.3");
200 return 0;
203 void dm_uevent_exit(void)
205 kmem_cache_destroy(_dm_event_cache);