Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * net/tipc/user_reg.c: TIPC user registry code |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 3 | * |
Per Liden | 593a5f2 | 2006-01-11 19:14:19 +0100 | [diff] [blame] | 4 | * Copyright (c) 2000-2006, Ericsson AB |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 5 | * Copyright (c) 2004-2005, Wind River Systems |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 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: |
Per Liden | 9ea1fd3 | 2006-01-11 13:30:43 +0100 | [diff] [blame] | 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. |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 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 | * |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 43 | * The registry utilizes an array of "TIPC user" entries; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 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) |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 54 | * @usr_handle: user-defined value passed to callback routine |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 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 | |
Sam Ravnborg | 1fc54d8 | 2006-03-20 22:36:47 -0800 | [diff] [blame] | 68 | static struct tipc_user *users = NULL; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 69 | static u32 next_free_user = MAX_USERID + 1; |
Ingo Molnar | 34af946 | 2006-06-27 02:53:55 -0700 | [diff] [blame] | 70 | static DEFINE_SPINLOCK(reg_lock); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 71 | |
| 72 | /** |
| 73 | * reg_init - create TIPC user registry (but don't activate it) |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 74 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 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; |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 82 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 83 | spin_lock_bh(®_lock); |
| 84 | if (!users) { |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 85 | users = kzalloc(USER_LIST_SIZE, GFP_ATOMIC); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 86 | if (users) { |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 87 | for (i = 1; i <= MAX_USERID; i++) { |
| 88 | users[i].next = i - 1; |
| 89 | } |
| 90 | next_free_user = MAX_USERID; |
| 91 | } |
| 92 | } |
| 93 | spin_unlock_bh(®_lock); |
Allan Stephens | 0e35fd5 | 2008-07-14 22:44:01 -0700 | [diff] [blame] | 94 | return users ? 0 : -ENOMEM; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /** |
| 98 | * reg_callback - inform TIPC user about current operating mode |
| 99 | */ |
| 100 | |
| 101 | static void reg_callback(struct tipc_user *user_ptr) |
| 102 | { |
| 103 | tipc_mode_event cb; |
| 104 | void *arg; |
| 105 | |
| 106 | spin_lock_bh(®_lock); |
| 107 | cb = user_ptr->callback; |
| 108 | arg = user_ptr->usr_handle; |
| 109 | spin_unlock_bh(®_lock); |
| 110 | |
| 111 | if (cb) |
| 112 | cb(arg, tipc_mode, tipc_own_addr); |
| 113 | } |
| 114 | |
| 115 | /** |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 116 | * tipc_reg_start - activate TIPC user registry |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 117 | */ |
| 118 | |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 119 | int tipc_reg_start(void) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 120 | { |
| 121 | u32 u; |
| 122 | int res; |
| 123 | |
| 124 | if ((res = reg_init())) |
| 125 | return res; |
| 126 | |
| 127 | for (u = 1; u <= MAX_USERID; u++) { |
| 128 | if (users[u].callback) |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 129 | tipc_k_signal((Handler)reg_callback, |
| 130 | (unsigned long)&users[u]); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 131 | } |
Allan Stephens | 0e35fd5 | 2008-07-14 22:44:01 -0700 | [diff] [blame] | 132 | return 0; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /** |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 136 | * tipc_reg_stop - shut down & delete TIPC user registry |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 137 | */ |
| 138 | |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 139 | void tipc_reg_stop(void) |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 140 | { |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 141 | int id; |
| 142 | |
| 143 | if (!users) |
| 144 | return; |
| 145 | |
| 146 | for (id = 1; id <= MAX_USERID; id++) { |
| 147 | if (users[id].callback) |
| 148 | reg_callback(&users[id]); |
| 149 | } |
| 150 | kfree(users); |
Sam Ravnborg | 1fc54d8 | 2006-03-20 22:36:47 -0800 | [diff] [blame] | 151 | users = NULL; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /** |
| 155 | * tipc_attach - register a TIPC user |
| 156 | * |
| 157 | * NOTE: This routine may be called when TIPC is inactive. |
| 158 | */ |
| 159 | |
| 160 | int tipc_attach(u32 *userid, tipc_mode_event cb, void *usr_handle) |
| 161 | { |
| 162 | struct tipc_user *user_ptr; |
| 163 | |
| 164 | if ((tipc_mode == TIPC_NOT_RUNNING) && !cb) |
| 165 | return -ENOPROTOOPT; |
| 166 | if (!users) |
| 167 | reg_init(); |
| 168 | |
| 169 | spin_lock_bh(®_lock); |
| 170 | if (!next_free_user) { |
| 171 | spin_unlock_bh(®_lock); |
| 172 | return -EBUSY; |
| 173 | } |
| 174 | user_ptr = &users[next_free_user]; |
| 175 | *userid = next_free_user; |
| 176 | next_free_user = user_ptr->next; |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 177 | user_ptr->next = -1; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 178 | spin_unlock_bh(®_lock); |
| 179 | |
| 180 | user_ptr->callback = cb; |
| 181 | user_ptr->usr_handle = usr_handle; |
| 182 | INIT_LIST_HEAD(&user_ptr->ports); |
| 183 | atomic_inc(&tipc_user_count); |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 184 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 185 | if (cb && (tipc_mode != TIPC_NOT_RUNNING)) |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 186 | tipc_k_signal((Handler)reg_callback, (unsigned long)user_ptr); |
Allan Stephens | 0e35fd5 | 2008-07-14 22:44:01 -0700 | [diff] [blame] | 187 | return 0; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /** |
| 191 | * tipc_detach - deregister a TIPC user |
| 192 | */ |
| 193 | |
| 194 | void tipc_detach(u32 userid) |
| 195 | { |
| 196 | struct tipc_user *user_ptr; |
| 197 | struct list_head ports_temp; |
| 198 | struct user_port *up_ptr, *temp_up_ptr; |
| 199 | |
| 200 | if ((userid == 0) || (userid > MAX_USERID)) |
| 201 | return; |
| 202 | |
| 203 | spin_lock_bh(®_lock); |
| 204 | if ((!users) || (users[userid].next >= 0)) { |
| 205 | spin_unlock_bh(®_lock); |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | user_ptr = &users[userid]; |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 210 | user_ptr->callback = NULL; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 211 | INIT_LIST_HEAD(&ports_temp); |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 212 | list_splice(&user_ptr->ports, &ports_temp); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 213 | user_ptr->next = next_free_user; |
| 214 | next_free_user = userid; |
| 215 | spin_unlock_bh(®_lock); |
| 216 | |
| 217 | atomic_dec(&tipc_user_count); |
| 218 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 219 | list_for_each_entry_safe(up_ptr, temp_up_ptr, &ports_temp, uport_list) { |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 220 | tipc_deleteport(up_ptr->ref); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | /** |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 225 | * tipc_reg_add_port - register a user's driver port |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 226 | */ |
| 227 | |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 228 | int tipc_reg_add_port(struct user_port *up_ptr) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 229 | { |
| 230 | struct tipc_user *user_ptr; |
| 231 | |
| 232 | if (up_ptr->user_ref == 0) |
Allan Stephens | 0e35fd5 | 2008-07-14 22:44:01 -0700 | [diff] [blame] | 233 | return 0; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 234 | if (up_ptr->user_ref > MAX_USERID) |
| 235 | return -EINVAL; |
| 236 | if ((tipc_mode == TIPC_NOT_RUNNING) || !users ) |
| 237 | return -ENOPROTOOPT; |
| 238 | |
| 239 | spin_lock_bh(®_lock); |
| 240 | user_ptr = &users[up_ptr->user_ref]; |
| 241 | list_add(&up_ptr->uport_list, &user_ptr->ports); |
| 242 | spin_unlock_bh(®_lock); |
Allan Stephens | 0e35fd5 | 2008-07-14 22:44:01 -0700 | [diff] [blame] | 243 | return 0; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | /** |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 247 | * tipc_reg_remove_port - deregister a user's driver port |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 248 | */ |
| 249 | |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 250 | int tipc_reg_remove_port(struct user_port *up_ptr) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 251 | { |
| 252 | if (up_ptr->user_ref == 0) |
Allan Stephens | 0e35fd5 | 2008-07-14 22:44:01 -0700 | [diff] [blame] | 253 | return 0; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 254 | if (up_ptr->user_ref > MAX_USERID) |
| 255 | return -EINVAL; |
| 256 | if (!users ) |
| 257 | return -ENOPROTOOPT; |
| 258 | |
| 259 | spin_lock_bh(®_lock); |
| 260 | list_del_init(&up_ptr->uport_list); |
| 261 | spin_unlock_bh(®_lock); |
Allan Stephens | 0e35fd5 | 2008-07-14 22:44:01 -0700 | [diff] [blame] | 262 | return 0; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 263 | } |
| 264 | |