user_reg.c 6.5 KB

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