blob: 2e2702e2049c3969c125308a71e5e31d98cffe06 [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
2 * net/tipc/user_reg.c: TIPC user registry code
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Per Liden593a5f22006-01-11 19:14:19 +01004 * Copyright (c) 2000-2006, Ericsson AB
Per Lidenb97bf3f2006-01-02 19:04:38 +01005 * Copyright (c) 2004-2005, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * 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 Liden9ea1fd32006-01-11 13:30:43 +010010 *
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 Lidenb97bf3f2006-01-02 19:04:38 +010023 *
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 Hideakic4307282007-02-09 23:25:21 +090043 * The registry utilizes an array of "TIPC user" entries;
Per Lidenb97bf3f2006-01-02 19:04:38 +010044 * 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)
Per Lidenb97bf3f2006-01-02 19:04:38 +010053 * @ports: list of user ports owned by the user
54 */
55
56struct tipc_user {
57 int next;
Per Lidenb97bf3f2006-01-02 19:04:38 +010058 struct list_head ports;
59};
60
61#define MAX_USERID 64
62#define USER_LIST_SIZE ((MAX_USERID + 1) * sizeof(struct tipc_user))
63
Sam Ravnborg1fc54d82006-03-20 22:36:47 -080064static struct tipc_user *users = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +010065static u32 next_free_user = MAX_USERID + 1;
Ingo Molnar34af9462006-06-27 02:53:55 -070066static DEFINE_SPINLOCK(reg_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +010067
68/**
69 * reg_init - create TIPC user registry (but don't activate it)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090070 *
Per Lidenb97bf3f2006-01-02 19:04:38 +010071 * If registry has been pre-initialized it is left "as is".
72 * NOTE: This routine may be called when TIPC is inactive.
73 */
74
75static int reg_init(void)
76{
77 u32 i;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090078
Per Lidenb97bf3f2006-01-02 19:04:38 +010079 spin_lock_bh(&reg_lock);
80 if (!users) {
Panagiotis Issaris0da974f2006-07-21 14:51:30 -070081 users = kzalloc(USER_LIST_SIZE, GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +010082 if (users) {
Per Lidenb97bf3f2006-01-02 19:04:38 +010083 for (i = 1; i <= MAX_USERID; i++) {
84 users[i].next = i - 1;
85 }
86 next_free_user = MAX_USERID;
87 }
88 }
89 spin_unlock_bh(&reg_lock);
Allan Stephens0e35fd52008-07-14 22:44:01 -070090 return users ? 0 : -ENOMEM;
Per Lidenb97bf3f2006-01-02 19:04:38 +010091}
92
93/**
Per Liden4323add2006-01-18 00:38:21 +010094 * tipc_reg_start - activate TIPC user registry
Per Lidenb97bf3f2006-01-02 19:04:38 +010095 */
96
Per Liden4323add2006-01-18 00:38:21 +010097int tipc_reg_start(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +010098{
Allan Stephensa5c2af92010-11-30 12:00:58 +000099 return reg_init();
Per Lidenb97bf3f2006-01-02 19:04:38 +0100100}
101
102/**
Per Liden4323add2006-01-18 00:38:21 +0100103 * tipc_reg_stop - shut down & delete TIPC user registry
Per Lidenb97bf3f2006-01-02 19:04:38 +0100104 */
105
Per Liden4323add2006-01-18 00:38:21 +0100106void tipc_reg_stop(void)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900107{
Per Lidenb97bf3f2006-01-02 19:04:38 +0100108 if (!users)
109 return;
110
Per Lidenb97bf3f2006-01-02 19:04:38 +0100111 kfree(users);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800112 users = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100113}
114
115/**
116 * tipc_attach - register a TIPC user
117 *
118 * NOTE: This routine may be called when TIPC is inactive.
119 */
120
Allan Stephensa5c2af92010-11-30 12:00:58 +0000121int tipc_attach(u32 *userid)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100122{
123 struct tipc_user *user_ptr;
124
Per Lidenb97bf3f2006-01-02 19:04:38 +0100125 if (!users)
126 reg_init();
127
128 spin_lock_bh(&reg_lock);
129 if (!next_free_user) {
130 spin_unlock_bh(&reg_lock);
131 return -EBUSY;
132 }
133 user_ptr = &users[next_free_user];
134 *userid = next_free_user;
135 next_free_user = user_ptr->next;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900136 user_ptr->next = -1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100137 spin_unlock_bh(&reg_lock);
138
Per Lidenb97bf3f2006-01-02 19:04:38 +0100139 INIT_LIST_HEAD(&user_ptr->ports);
140 atomic_inc(&tipc_user_count);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900141
Allan Stephens0e35fd52008-07-14 22:44:01 -0700142 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100143}
144
145/**
146 * tipc_detach - deregister a TIPC user
147 */
148
149void tipc_detach(u32 userid)
150{
151 struct tipc_user *user_ptr;
152 struct list_head ports_temp;
153 struct user_port *up_ptr, *temp_up_ptr;
154
155 if ((userid == 0) || (userid > MAX_USERID))
156 return;
157
158 spin_lock_bh(&reg_lock);
159 if ((!users) || (users[userid].next >= 0)) {
160 spin_unlock_bh(&reg_lock);
161 return;
162 }
163
164 user_ptr = &users[userid];
Per Lidenb97bf3f2006-01-02 19:04:38 +0100165 INIT_LIST_HEAD(&ports_temp);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900166 list_splice(&user_ptr->ports, &ports_temp);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100167 user_ptr->next = next_free_user;
168 next_free_user = userid;
169 spin_unlock_bh(&reg_lock);
170
171 atomic_dec(&tipc_user_count);
172
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900173 list_for_each_entry_safe(up_ptr, temp_up_ptr, &ports_temp, uport_list) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100174 tipc_deleteport(up_ptr->ref);
175 }
176}
177
178/**
Per Liden4323add2006-01-18 00:38:21 +0100179 * tipc_reg_add_port - register a user's driver port
Per Lidenb97bf3f2006-01-02 19:04:38 +0100180 */
181
Per Liden4323add2006-01-18 00:38:21 +0100182int tipc_reg_add_port(struct user_port *up_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100183{
184 struct tipc_user *user_ptr;
185
186 if (up_ptr->user_ref == 0)
Allan Stephens0e35fd52008-07-14 22:44:01 -0700187 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100188 if (up_ptr->user_ref > MAX_USERID)
189 return -EINVAL;
190 if ((tipc_mode == TIPC_NOT_RUNNING) || !users )
191 return -ENOPROTOOPT;
192
193 spin_lock_bh(&reg_lock);
194 user_ptr = &users[up_ptr->user_ref];
195 list_add(&up_ptr->uport_list, &user_ptr->ports);
196 spin_unlock_bh(&reg_lock);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700197 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100198}
199
200/**
Per Liden4323add2006-01-18 00:38:21 +0100201 * tipc_reg_remove_port - deregister a user's driver port
Per Lidenb97bf3f2006-01-02 19:04:38 +0100202 */
203
Per Liden4323add2006-01-18 00:38:21 +0100204int tipc_reg_remove_port(struct user_port *up_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100205{
206 if (up_ptr->user_ref == 0)
Allan Stephens0e35fd52008-07-14 22:44:01 -0700207 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100208 if (up_ptr->user_ref > MAX_USERID)
209 return -EINVAL;
210 if (!users )
211 return -ENOPROTOOPT;
212
213 spin_lock_bh(&reg_lock);
214 list_del_init(&up_ptr->uport_list);
215 spin_unlock_bh(&reg_lock);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700216 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100217}
218