diff options
Diffstat (limited to 'net/tipc/user_reg.c')
-rw-r--r-- | net/tipc/user_reg.c | 265 |
1 files changed, 265 insertions, 0 deletions
diff --git a/net/tipc/user_reg.c b/net/tipc/user_reg.c new file mode 100644 index 000000000000..35ec7dc8211d --- /dev/null +++ b/net/tipc/user_reg.c | |||
@@ -0,0 +1,265 @@ | |||
1 | /* | ||
2 | * net/tipc/user_reg.c: TIPC user registry code | ||
3 | * | ||
4 | * Copyright (c) 2000-2006, Ericsson AB | ||
5 | * Copyright (c) 2004-2005, Wind River Systems | ||
6 | * All rights reserved. | ||
7 | * | ||
8 | * Redistribution and use in source and binary forms, with or without | ||
9 | * modification, are permitted provided that the following conditions are met: | ||
10 | * | ||
11 | * 1. Redistributions of source code must retain the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer. | ||
13 | * 2. Redistributions in binary form must reproduce the above copyright | ||
14 | * notice, this list of conditions and the following disclaimer in the | ||
15 | * documentation and/or other materials provided with the distribution. | ||
16 | * 3. Neither the names of the copyright holders nor the names of its | ||
17 | * contributors may be used to endorse or promote products derived from | ||
18 | * this software without specific prior written permission. | ||
19 | * | ||
20 | * Alternatively, this software may be distributed under the terms of the | ||
21 | * GNU General Public License ("GPL") version 2 as published by the Free | ||
22 | * Software Foundation. | ||
23 | * | ||
24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
28 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
29 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
30 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
31 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
32 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
34 | * POSSIBILITY OF SUCH DAMAGE. | ||
35 | */ | ||
36 | |||
37 | #include "core.h" | ||
38 | #include "user_reg.h" | ||
39 | |||
40 | /* | ||
41 | * TIPC user registry keeps track of users of the tipc_port interface. | ||
42 | * | ||
43 | * The registry utilizes an array of "TIPC user" entries; | ||
44 | * a user's ID is the index of their associated array entry. | ||
45 | * Array entry 0 is not used, so userid 0 is not valid; | ||
46 | * TIPC sometimes uses this value to denote an anonymous user. | ||
47 | * The list of free entries is initially chained from last entry to entry 1. | ||
48 | */ | ||
49 | |||
50 | /** | ||
51 | * struct tipc_user - registered TIPC user info | ||
52 | * @next: index of next free registry entry (or -1 for an allocated entry) | ||
53 | * @callback: ptr to routine to call when TIPC mode changes (NULL if none) | ||
54 | * @usr_handle: user-defined value passed to callback routine | ||
55 | * @ports: list of user ports owned by the user | ||
56 | */ | ||
57 | |||
58 | struct tipc_user { | ||
59 | int next; | ||
60 | tipc_mode_event callback; | ||
61 | void *usr_handle; | ||
62 | struct list_head ports; | ||
63 | }; | ||
64 | |||
65 | #define MAX_USERID 64 | ||
66 | #define USER_LIST_SIZE ((MAX_USERID + 1) * sizeof(struct tipc_user)) | ||
67 | |||
68 | static struct tipc_user *users = 0; | ||
69 | static u32 next_free_user = MAX_USERID + 1; | ||
70 | static spinlock_t reg_lock = SPIN_LOCK_UNLOCKED; | ||
71 | |||
72 | /** | ||
73 | * reg_init - create TIPC user registry (but don't activate it) | ||
74 | * | ||
75 | * If registry has been pre-initialized it is left "as is". | ||
76 | * NOTE: This routine may be called when TIPC is inactive. | ||
77 | */ | ||
78 | |||
79 | static int reg_init(void) | ||
80 | { | ||
81 | u32 i; | ||
82 | |||
83 | spin_lock_bh(®_lock); | ||
84 | if (!users) { | ||
85 | users = (struct tipc_user *)kmalloc(USER_LIST_SIZE, GFP_ATOMIC); | ||
86 | if (users) { | ||
87 | memset(users, 0, USER_LIST_SIZE); | ||
88 | for (i = 1; i <= MAX_USERID; i++) { | ||
89 | users[i].next = i - 1; | ||
90 | } | ||
91 | next_free_user = MAX_USERID; | ||
92 | } | ||
93 | } | ||
94 | spin_unlock_bh(®_lock); | ||
95 | return users ? TIPC_OK : -ENOMEM; | ||
96 | } | ||
97 | |||
98 | /** | ||
99 | * reg_callback - inform TIPC user about current operating mode | ||
100 | */ | ||
101 | |||
102 | static void reg_callback(struct tipc_user *user_ptr) | ||
103 | { | ||
104 | tipc_mode_event cb; | ||
105 | void *arg; | ||
106 | |||
107 | spin_lock_bh(®_lock); | ||
108 | cb = user_ptr->callback; | ||
109 | arg = user_ptr->usr_handle; | ||
110 | spin_unlock_bh(®_lock); | ||
111 | |||
112 | if (cb) | ||
113 | cb(arg, tipc_mode, tipc_own_addr); | ||
114 | } | ||
115 | |||
116 | /** | ||
117 | * reg_start - activate TIPC user registry | ||
118 | */ | ||
119 | |||
120 | int reg_start(void) | ||
121 | { | ||
122 | u32 u; | ||
123 | int res; | ||
124 | |||
125 | if ((res = reg_init())) | ||
126 | return res; | ||
127 | |||
128 | for (u = 1; u <= MAX_USERID; u++) { | ||
129 | if (users[u].callback) | ||
130 | k_signal((Handler)reg_callback, | ||
131 | (unsigned long)&users[u]); | ||
132 | } | ||
133 | return TIPC_OK; | ||
134 | } | ||
135 | |||
136 | /** | ||
137 | * reg_stop - shut down & delete TIPC user registry | ||
138 | */ | ||
139 | |||
140 | void reg_stop(void) | ||
141 | { | ||
142 | int id; | ||
143 | |||
144 | if (!users) | ||
145 | return; | ||
146 | |||
147 | for (id = 1; id <= MAX_USERID; id++) { | ||
148 | if (users[id].callback) | ||
149 | reg_callback(&users[id]); | ||
150 | } | ||
151 | kfree(users); | ||
152 | users = 0; | ||
153 | } | ||
154 | |||
155 | /** | ||
156 | * tipc_attach - register a TIPC user | ||
157 | * | ||
158 | * NOTE: This routine may be called when TIPC is inactive. | ||
159 | */ | ||
160 | |||
161 | int tipc_attach(u32 *userid, tipc_mode_event cb, void *usr_handle) | ||
162 | { | ||
163 | struct tipc_user *user_ptr; | ||
164 | |||
165 | if ((tipc_mode == TIPC_NOT_RUNNING) && !cb) | ||
166 | return -ENOPROTOOPT; | ||
167 | if (!users) | ||
168 | reg_init(); | ||
169 | |||
170 | spin_lock_bh(®_lock); | ||
171 | if (!next_free_user) { | ||
172 | spin_unlock_bh(®_lock); | ||
173 | return -EBUSY; | ||
174 | } | ||
175 | user_ptr = &users[next_free_user]; | ||
176 | *userid = next_free_user; | ||
177 | next_free_user = user_ptr->next; | ||
178 | user_ptr->next = -1; | ||
179 | spin_unlock_bh(®_lock); | ||
180 | |||
181 | user_ptr->callback = cb; | ||
182 | user_ptr->usr_handle = usr_handle; | ||
183 | INIT_LIST_HEAD(&user_ptr->ports); | ||
184 | atomic_inc(&tipc_user_count); | ||
185 | |||
186 | if (cb && (tipc_mode != TIPC_NOT_RUNNING)) | ||
187 | k_signal((Handler)reg_callback, (unsigned long)user_ptr); | ||
188 | return TIPC_OK; | ||
189 | } | ||
190 | |||
191 | /** | ||
192 | * tipc_detach - deregister a TIPC user | ||
193 | */ | ||
194 | |||
195 | void tipc_detach(u32 userid) | ||
196 | { | ||
197 | struct tipc_user *user_ptr; | ||
198 | struct list_head ports_temp; | ||
199 | struct user_port *up_ptr, *temp_up_ptr; | ||
200 | |||
201 | if ((userid == 0) || (userid > MAX_USERID)) | ||
202 | return; | ||
203 | |||
204 | spin_lock_bh(®_lock); | ||
205 | if ((!users) || (users[userid].next >= 0)) { | ||
206 | spin_unlock_bh(®_lock); | ||
207 | return; | ||
208 | } | ||
209 | |||
210 | user_ptr = &users[userid]; | ||
211 | user_ptr->callback = NULL; | ||
212 | INIT_LIST_HEAD(&ports_temp); | ||
213 | list_splice(&user_ptr->ports, &ports_temp); | ||
214 | user_ptr->next = next_free_user; | ||
215 | next_free_user = userid; | ||
216 | spin_unlock_bh(®_lock); | ||
217 | |||
218 | atomic_dec(&tipc_user_count); | ||
219 | |||
220 | list_for_each_entry_safe(up_ptr, temp_up_ptr, &ports_temp, uport_list) { | ||
221 | tipc_deleteport(up_ptr->ref); | ||
222 | } | ||
223 | } | ||
224 | |||
225 | /** | ||
226 | * reg_add_port - register a user's driver port | ||
227 | */ | ||
228 | |||
229 | int reg_add_port(struct user_port *up_ptr) | ||
230 | { | ||
231 | struct tipc_user *user_ptr; | ||
232 | |||
233 | if (up_ptr->user_ref == 0) | ||
234 | return TIPC_OK; | ||
235 | if (up_ptr->user_ref > MAX_USERID) | ||
236 | return -EINVAL; | ||
237 | if ((tipc_mode == TIPC_NOT_RUNNING) || !users ) | ||
238 | return -ENOPROTOOPT; | ||
239 | |||
240 | spin_lock_bh(®_lock); | ||
241 | user_ptr = &users[up_ptr->user_ref]; | ||
242 | list_add(&up_ptr->uport_list, &user_ptr->ports); | ||
243 | spin_unlock_bh(®_lock); | ||
244 | return TIPC_OK; | ||
245 | } | ||
246 | |||
247 | /** | ||
248 | * reg_remove_port - deregister a user's driver port | ||
249 | */ | ||
250 | |||
251 | int reg_remove_port(struct user_port *up_ptr) | ||
252 | { | ||
253 | if (up_ptr->user_ref == 0) | ||
254 | return TIPC_OK; | ||
255 | if (up_ptr->user_ref > MAX_USERID) | ||
256 | return -EINVAL; | ||
257 | if (!users ) | ||
258 | return -ENOPROTOOPT; | ||
259 | |||
260 | spin_lock_bh(®_lock); | ||
261 | list_del_init(&up_ptr->uport_list); | ||
262 | spin_unlock_bh(®_lock); | ||
263 | return TIPC_OK; | ||
264 | } | ||
265 | |||