Blame view

drivers/md/dm-uevent.c 5.32 KB
51e5b2bd3   Mike Anderson   dm: add uevent to...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  /*
   * Device Mapper Uevent Support (dm-uevent)
   *
   * This program is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License as published by the
   * Free Software Foundation; either version 2 of the License, or (at your
   * option) any later version.
   *
   * This program is distributed in the hope that it will be useful, but
   * WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   * General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License along
   * with this program; if not, write to the Free Software Foundation, Inc.,
   * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
   *
   * Copyright IBM Corporation, 2007
   * 	Author: Mike Anderson <andmike@linux.vnet.ibm.com>
   */
  #include <linux/list.h>
  #include <linux/slab.h>
  #include <linux/kobject.h>
7a8c3d3b9   Mike Anderson   dm: uevent genera...
24
  #include <linux/dm-ioctl.h>
daaa5f7cb   Paul Gortmaker   md: Add in export...
25
  #include <linux/export.h>
51e5b2bd3   Mike Anderson   dm: add uevent to...
26
27
28
29
30
  
  #include "dm.h"
  #include "dm-uevent.h"
  
  #define DM_MSG_PREFIX "uevent"
7a8c3d3b9   Mike Anderson   dm: uevent genera...
31
32
33
34
35
36
37
38
  static const struct {
  	enum dm_uevent_type type;
  	enum kobject_action action;
  	char *name;
  } _dm_uevent_type_names[] = {
  	{DM_UEVENT_PATH_FAILED, KOBJ_CHANGE, "PATH_FAILED"},
  	{DM_UEVENT_PATH_REINSTATED, KOBJ_CHANGE, "PATH_REINSTATED"},
  };
51e5b2bd3   Mike Anderson   dm: add uevent to...
39
40
41
42
43
44
45
  static struct kmem_cache *_dm_event_cache;
  
  struct dm_uevent {
  	struct mapped_device *md;
  	enum kobject_action action;
  	struct kobj_uevent_env ku_env;
  	struct list_head elist;
7a8c3d3b9   Mike Anderson   dm: uevent genera...
46
47
  	char name[DM_NAME_LEN];
  	char uuid[DM_UUID_LEN];
51e5b2bd3   Mike Anderson   dm: add uevent to...
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  };
  
  static void dm_uevent_free(struct dm_uevent *event)
  {
  	kmem_cache_free(_dm_event_cache, event);
  }
  
  static struct dm_uevent *dm_uevent_alloc(struct mapped_device *md)
  {
  	struct dm_uevent *event;
  
  	event = kmem_cache_zalloc(_dm_event_cache, GFP_ATOMIC);
  	if (!event)
  		return NULL;
  
  	INIT_LIST_HEAD(&event->elist);
  	event->md = md;
  
  	return event;
  }
7a8c3d3b9   Mike Anderson   dm: uevent genera...
68
69
70
71
72
73
74
75
76
77
78
  static struct dm_uevent *dm_build_path_uevent(struct mapped_device *md,
  					      struct dm_target *ti,
  					      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) {
e46b272b6   Harvey Harrison   md: replace remai...
79
  		DMERR("%s: dm_uevent_alloc() failed", __func__);
7a8c3d3b9   Mike Anderson   dm: uevent genera...
80
81
82
83
84
85
86
  		goto err_nomem;
  	}
  
  	event->action = action;
  
  	if (add_uevent_var(&event->ku_env, "DM_TARGET=%s", ti->type->name)) {
  		DMERR("%s: add_uevent_var() for DM_TARGET failed",
e46b272b6   Harvey Harrison   md: replace remai...
87
  		      __func__);
7a8c3d3b9   Mike Anderson   dm: uevent genera...
88
89
90
91
92
  		goto err_add;
  	}
  
  	if (add_uevent_var(&event->ku_env, "DM_ACTION=%s", dm_action)) {
  		DMERR("%s: add_uevent_var() for DM_ACTION failed",
e46b272b6   Harvey Harrison   md: replace remai...
93
  		      __func__);
7a8c3d3b9   Mike Anderson   dm: uevent genera...
94
95
96
97
98
99
  		goto err_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",
e46b272b6   Harvey Harrison   md: replace remai...
100
  		      __func__);
7a8c3d3b9   Mike Anderson   dm: uevent genera...
101
102
103
104
  		goto err_add;
  	}
  
  	if (add_uevent_var(&event->ku_env, "DM_PATH=%s", path)) {
e46b272b6   Harvey Harrison   md: replace remai...
105
  		DMERR("%s: add_uevent_var() for DM_PATH failed", __func__);
7a8c3d3b9   Mike Anderson   dm: uevent genera...
106
107
108
109
110
111
  		goto err_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",
e46b272b6   Harvey Harrison   md: replace remai...
112
  		      __func__);
7a8c3d3b9   Mike Anderson   dm: uevent genera...
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
  		goto err_add;
  	}
  
  	return event;
  
  err_add:
  	dm_uevent_free(event);
  err_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);
  
  		/*
6076905b5   Mikulas Patocka   dm: avoid _hash_l...
140
141
  		 * When a device is being removed this copy fails and we
  		 * discard these unsent events.
7a8c3d3b9   Mike Anderson   dm: uevent genera...
142
143
144
  		 */
  		if (dm_copy_name_and_uuid(event->md, event->name,
  					  event->uuid)) {
6076905b5   Mikulas Patocka   dm: avoid _hash_l...
145
146
  			DMINFO("%s: skipping sending uevent for lost device",
  			       __func__);
7a8c3d3b9   Mike Anderson   dm: uevent genera...
147
148
149
150
151
  			goto uevent_free;
  		}
  
  		if (add_uevent_var(&event->ku_env, "DM_NAME=%s", event->name)) {
  			DMERR("%s: add_uevent_var() for DM_NAME failed",
e46b272b6   Harvey Harrison   md: replace remai...
152
  			      __func__);
7a8c3d3b9   Mike Anderson   dm: uevent genera...
153
154
155
156
157
  			goto uevent_free;
  		}
  
  		if (add_uevent_var(&event->ku_env, "DM_UUID=%s", event->uuid)) {
  			DMERR("%s: add_uevent_var() for DM_UUID failed",
e46b272b6   Harvey Harrison   md: replace remai...
158
  			      __func__);
7a8c3d3b9   Mike Anderson   dm: uevent genera...
159
160
161
162
163
  			goto uevent_free;
  		}
  
  		r = kobject_uevent_env(kobj, event->action, event->ku_env.envp);
  		if (r)
e46b272b6   Harvey Harrison   md: replace remai...
164
  			DMERR("%s: kobject_uevent_env failed", __func__);
7a8c3d3b9   Mike Anderson   dm: uevent genera...
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
  uevent_free:
  		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
   * @ti:			pointer to a dm_target
   * @path:		string containing pathname
   * @nr_valid_paths:	number of valid paths remaining
   *
   */
  void dm_path_uevent(enum dm_uevent_type event_type, struct dm_target *ti,
  		   const char *path, unsigned nr_valid_paths)
  {
  	struct mapped_device *md = dm_table_get_md(ti->table);
  	struct dm_uevent *event;
  
  	if (event_type >= ARRAY_SIZE(_dm_uevent_type_names)) {
e46b272b6   Harvey Harrison   md: replace remai...
187
  		DMERR("%s: Invalid event_type %d", __func__, event_type);
ecdb2e257   Kiyoshi Ueda   dm table: remove ...
188
  		return;
7a8c3d3b9   Mike Anderson   dm: uevent genera...
189
190
191
192
193
194
195
  	}
  
  	event = dm_build_path_uevent(md, ti,
  				     _dm_uevent_type_names[event_type].action,
  				     _dm_uevent_type_names[event_type].name,
  				     path, nr_valid_paths);
  	if (IS_ERR(event))
ecdb2e257   Kiyoshi Ueda   dm table: remove ...
196
  		return;
7a8c3d3b9   Mike Anderson   dm: uevent genera...
197
198
  
  	dm_uevent_add(md, &event->elist);
7a8c3d3b9   Mike Anderson   dm: uevent genera...
199
200
  }
  EXPORT_SYMBOL_GPL(dm_path_uevent);
51e5b2bd3   Mike Anderson   dm: add uevent to...
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
  int dm_uevent_init(void)
  {
  	_dm_event_cache = KMEM_CACHE(dm_uevent, 0);
  	if (!_dm_event_cache)
  		return -ENOMEM;
  
  	DMINFO("version 1.0.3");
  
  	return 0;
  }
  
  void dm_uevent_exit(void)
  {
  	kmem_cache_destroy(_dm_event_cache);
  }