2 * net/tipc/user_reg.c: TIPC user registry code
4 * Copyright (c) 2000-2006, Ericsson AB
5 * Copyright (c) 2004-2005, Wind River Systems
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
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.
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.
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.
41 * TIPC user registry keeps track of users of the tipc_port interface.
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.
51 * struct tipc_user - registered TIPC user info
52 * @next: index of next free registry entry (or -1 for an allocated entry)
53 * @ports: list of user ports owned by the user
58 struct list_head ports
;
62 #define USER_LIST_SIZE ((MAX_USERID + 1) * sizeof(struct tipc_user))
64 static struct tipc_user
*users
= NULL
;
65 static u32 next_free_user
= MAX_USERID
+ 1;
66 static DEFINE_SPINLOCK(reg_lock
);
69 * reg_init - create TIPC user registry (but don't activate it)
71 * If registry has been pre-initialized it is left "as is".
72 * NOTE: This routine may be called when TIPC is inactive.
75 static int reg_init(void)
79 spin_lock_bh(®_lock
);
81 users
= kzalloc(USER_LIST_SIZE
, GFP_ATOMIC
);
83 for (i
= 1; i
<= MAX_USERID
; i
++) {
84 users
[i
].next
= i
- 1;
86 next_free_user
= MAX_USERID
;
89 spin_unlock_bh(®_lock
);
90 return users
? 0 : -ENOMEM
;
94 * tipc_reg_start - activate TIPC user registry
97 int tipc_reg_start(void)
103 * tipc_reg_stop - shut down & delete TIPC user registry
106 void tipc_reg_stop(void)
116 * tipc_attach - register a TIPC user
118 * NOTE: This routine may be called when TIPC is inactive.
121 int tipc_attach(u32
*userid
)
123 struct tipc_user
*user_ptr
;
128 spin_lock_bh(®_lock
);
129 if (!next_free_user
) {
130 spin_unlock_bh(®_lock
);
133 user_ptr
= &users
[next_free_user
];
134 *userid
= next_free_user
;
135 next_free_user
= user_ptr
->next
;
137 spin_unlock_bh(®_lock
);
139 INIT_LIST_HEAD(&user_ptr
->ports
);
140 atomic_inc(&tipc_user_count
);
146 * tipc_detach - deregister a TIPC user
149 void tipc_detach(u32 userid
)
151 struct tipc_user
*user_ptr
;
152 struct list_head ports_temp
;
153 struct user_port
*up_ptr
, *temp_up_ptr
;
155 if ((userid
== 0) || (userid
> MAX_USERID
))
158 spin_lock_bh(®_lock
);
159 if ((!users
) || (users
[userid
].next
>= 0)) {
160 spin_unlock_bh(®_lock
);
164 user_ptr
= &users
[userid
];
165 INIT_LIST_HEAD(&ports_temp
);
166 list_splice(&user_ptr
->ports
, &ports_temp
);
167 user_ptr
->next
= next_free_user
;
168 next_free_user
= userid
;
169 spin_unlock_bh(®_lock
);
171 atomic_dec(&tipc_user_count
);
173 list_for_each_entry_safe(up_ptr
, temp_up_ptr
, &ports_temp
, uport_list
) {
174 tipc_deleteport(up_ptr
->ref
);
179 * tipc_reg_add_port - register a user's driver port
182 int tipc_reg_add_port(struct user_port
*up_ptr
)
184 struct tipc_user
*user_ptr
;
186 if (up_ptr
->user_ref
== 0)
188 if (up_ptr
->user_ref
> MAX_USERID
)
190 if ((tipc_mode
== TIPC_NOT_RUNNING
) || !users
)
193 spin_lock_bh(®_lock
);
194 user_ptr
= &users
[up_ptr
->user_ref
];
195 list_add(&up_ptr
->uport_list
, &user_ptr
->ports
);
196 spin_unlock_bh(®_lock
);
201 * tipc_reg_remove_port - deregister a user's driver port
204 int tipc_reg_remove_port(struct user_port
*up_ptr
)
206 if (up_ptr
->user_ref
== 0)
208 if (up_ptr
->user_ref
> MAX_USERID
)
213 spin_lock_bh(®_lock
);
214 list_del_init(&up_ptr
->uport_list
);
215 spin_unlock_bh(®_lock
);
This page took 0.062104 seconds and 5 git commands to generate.