user_reg.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. #include "core.h"
  37. #include "user_reg.h"
  38. /*
  39. * TIPC user registry keeps track of users of the tipc_port interface.
  40. *
  41. * The registry utilizes an array of "TIPC user" entries;
  42. * a user's ID is the index of their associated array entry.
  43. * Array entry 0 is not used, so userid 0 is not valid;
  44. * TIPC sometimes uses this value to denote an anonymous user.
  45. * The list of free entries is initially chained from last entry to entry 1.
  46. */
  47. /**
  48. * struct tipc_user - registered TIPC user info
  49. * @next: index of next free registry entry (or -1 for an allocated entry)
  50. * @ports: list of user ports owned by the user
  51. */
  52. struct tipc_user {
  53. int next;
  54. struct list_head ports;
  55. };
  56. #define MAX_USERID 64
  57. #define USER_LIST_SIZE ((MAX_USERID + 1) * sizeof(struct tipc_user))
  58. static struct tipc_user *users = NULL;
  59. static u32 next_free_user = MAX_USERID + 1;
  60. static DEFINE_SPINLOCK(reg_lock);
  61. /**
  62. * reg_init - create TIPC user registry (but don't activate it)
  63. *
  64. * If registry has been pre-initialized it is left "as is".
  65. * NOTE: This routine may be called when TIPC is inactive.
  66. */
  67. static int reg_init(void)
  68. {
  69. u32 i;
  70. spin_lock_bh(&reg_lock);
  71. if (!users) {
  72. users = kzalloc(USER_LIST_SIZE, GFP_ATOMIC);
  73. if (users) {
  74. for (i = 1; i <= MAX_USERID; i++) {
  75. users[i].next = i - 1;
  76. }
  77. next_free_user = MAX_USERID;
  78. }
  79. }
  80. spin_unlock_bh(&reg_lock);
  81. return users ? 0 : -ENOMEM;
  82. }
  83. /**
  84. * tipc_reg_start - activate TIPC user registry
  85. */
  86. int tipc_reg_start(void)
  87. {
  88. return reg_init();
  89. }
  90. /**
  91. * tipc_reg_stop - shut down & delete TIPC user registry
  92. */
  93. void tipc_reg_stop(void)
  94. {
  95. if (!users)
  96. return;
  97. kfree(users);
  98. users = NULL;
  99. }
  100. /**
  101. * tipc_attach - register a TIPC user
  102. *
  103. * NOTE: This routine may be called when TIPC is inactive.
  104. */
  105. int tipc_attach(u32 *userid)
  106. {
  107. struct tipc_user *user_ptr;
  108. if (!users)
  109. reg_init();
  110. spin_lock_bh(&reg_lock);
  111. if (!next_free_user) {
  112. spin_unlock_bh(&reg_lock);
  113. return -EBUSY;
  114. }
  115. user_ptr = &users[next_free_user];
  116. *userid = next_free_user;
  117. next_free_user = user_ptr->next;
  118. user_ptr->next = -1;
  119. spin_unlock_bh(&reg_lock);
  120. INIT_LIST_HEAD(&user_ptr->ports);
  121. atomic_inc(&tipc_user_count);
  122. return 0;
  123. }
  124. /**
  125. * tipc_detach - deregister a TIPC user
  126. */
  127. void tipc_detach(u32 userid)
  128. {
  129. struct tipc_user *user_ptr;
  130. struct list_head ports_temp;
  131. struct user_port *up_ptr, *temp_up_ptr;
  132. if ((userid == 0) || (userid > MAX_USERID))
  133. return;
  134. spin_lock_bh(&reg_lock);
  135. if ((!users) || (users[userid].next >= 0)) {
  136. spin_unlock_bh(&reg_lock);
  137. return;
  138. }
  139. user_ptr = &users[userid];
  140. INIT_LIST_HEAD(&ports_temp);
  141. list_splice(&user_ptr->ports, &ports_temp);
  142. user_ptr->next = next_free_user;
  143. next_free_user = userid;
  144. spin_unlock_bh(&reg_lock);
  145. atomic_dec(&tipc_user_count);
  146. list_for_each_entry_safe(up_ptr, temp_up_ptr, &ports_temp, uport_list) {
  147. tipc_deleteport(up_ptr->ref);
  148. }
  149. }
  150. /**
  151. * tipc_reg_add_port - register a user's driver port
  152. */
  153. int tipc_reg_add_port(struct user_port *up_ptr)
  154. {
  155. struct tipc_user *user_ptr;
  156. if (up_ptr->user_ref == 0)
  157. return 0;
  158. if (up_ptr->user_ref > MAX_USERID)
  159. return -EINVAL;
  160. if ((tipc_mode == TIPC_NOT_RUNNING) || !users )
  161. return -ENOPROTOOPT;
  162. spin_lock_bh(&reg_lock);
  163. user_ptr = &users[up_ptr->user_ref];
  164. list_add(&up_ptr->uport_list, &user_ptr->ports);
  165. spin_unlock_bh(&reg_lock);
  166. return 0;
  167. }
  168. /**
  169. * tipc_reg_remove_port - deregister a user's driver port
  170. */
  171. int tipc_reg_remove_port(struct user_port *up_ptr)
  172. {
  173. if (up_ptr->user_ref == 0)
  174. return 0;
  175. if (up_ptr->user_ref > MAX_USERID)
  176. return -EINVAL;
  177. if (!users )
  178. return -ENOPROTOOPT;
  179. spin_lock_bh(&reg_lock);
  180. list_del_init(&up_ptr->uport_list);
  181. spin_unlock_bh(&reg_lock);
  182. return 0;
  183. }