cell.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /* AFS cell and server record management
  2. *
  3. * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include "internal.h"
  14. DECLARE_RWSEM(afs_proc_cells_sem);
  15. LIST_HEAD(afs_proc_cells);
  16. static struct list_head afs_cells = LIST_HEAD_INIT(afs_cells);
  17. static DEFINE_RWLOCK(afs_cells_lock);
  18. static DECLARE_RWSEM(afs_cells_sem); /* add/remove serialisation */
  19. static DECLARE_WAIT_QUEUE_HEAD(afs_cells_freeable_wq);
  20. static struct afs_cell *afs_cell_root;
  21. /*
  22. * create a cell record
  23. * - "name" is the name of the cell
  24. * - "vllist" is a colon separated list of IP addresses in "a.b.c.d" format
  25. */
  26. struct afs_cell *afs_cell_create(const char *name, char *vllist)
  27. {
  28. struct afs_cell *cell;
  29. char *next;
  30. int ret;
  31. _enter("%s,%s", name, vllist);
  32. BUG_ON(!name); /* TODO: want to look up "this cell" in the cache */
  33. /* allocate and initialise a cell record */
  34. cell = kmalloc(sizeof(struct afs_cell) + strlen(name) + 1, GFP_KERNEL);
  35. if (!cell) {
  36. _leave(" = -ENOMEM");
  37. return ERR_PTR(-ENOMEM);
  38. }
  39. down_write(&afs_cells_sem);
  40. memset(cell, 0, sizeof(struct afs_cell));
  41. atomic_set(&cell->usage, 1);
  42. INIT_LIST_HEAD(&cell->link);
  43. rwlock_init(&cell->servers_lock);
  44. INIT_LIST_HEAD(&cell->servers);
  45. init_rwsem(&cell->vl_sem);
  46. INIT_LIST_HEAD(&cell->vl_list);
  47. spin_lock_init(&cell->vl_lock);
  48. strcpy(cell->name, name);
  49. /* fill in the VL server list from the rest of the string */
  50. ret = -EINVAL;
  51. do {
  52. unsigned a, b, c, d;
  53. next = strchr(vllist, ':');
  54. if (next)
  55. *next++ = 0;
  56. if (sscanf(vllist, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
  57. goto badaddr;
  58. if (a > 255 || b > 255 || c > 255 || d > 255)
  59. goto badaddr;
  60. cell->vl_addrs[cell->vl_naddrs++].s_addr =
  61. htonl((a << 24) | (b << 16) | (c << 8) | d);
  62. if (cell->vl_naddrs >= AFS_CELL_MAX_ADDRS)
  63. break;
  64. } while ((vllist = next));
  65. /* add a proc directory for this cell */
  66. ret = afs_proc_cell_setup(cell);
  67. if (ret < 0)
  68. goto error;
  69. #ifdef AFS_CACHING_SUPPORT
  70. /* put it up for caching */
  71. cachefs_acquire_cookie(afs_cache_netfs.primary_index,
  72. &afs_vlocation_cache_index_def,
  73. cell,
  74. &cell->cache);
  75. #endif
  76. /* add to the cell lists */
  77. write_lock(&afs_cells_lock);
  78. list_add_tail(&cell->link, &afs_cells);
  79. write_unlock(&afs_cells_lock);
  80. down_write(&afs_proc_cells_sem);
  81. list_add_tail(&cell->proc_link, &afs_proc_cells);
  82. up_write(&afs_proc_cells_sem);
  83. up_write(&afs_cells_sem);
  84. _leave(" = %p", cell);
  85. return cell;
  86. badaddr:
  87. printk(KERN_ERR "kAFS: bad VL server IP address\n");
  88. error:
  89. up_write(&afs_cells_sem);
  90. kfree(cell);
  91. _leave(" = %d", ret);
  92. return ERR_PTR(ret);
  93. }
  94. /*
  95. * set the root cell information
  96. * - can be called with a module parameter string
  97. * - can be called from a write to /proc/fs/afs/rootcell
  98. */
  99. int afs_cell_init(char *rootcell)
  100. {
  101. struct afs_cell *old_root, *new_root;
  102. char *cp;
  103. _enter("");
  104. if (!rootcell) {
  105. /* module is loaded with no parameters, or built statically.
  106. * - in the future we might initialize cell DB here.
  107. */
  108. _leave(" = 0 [no root]");
  109. return 0;
  110. }
  111. cp = strchr(rootcell, ':');
  112. if (!cp) {
  113. printk(KERN_ERR "kAFS: no VL server IP addresses specified\n");
  114. _leave(" = -EINVAL");
  115. return -EINVAL;
  116. }
  117. /* allocate a cell record for the root cell */
  118. *cp++ = 0;
  119. new_root = afs_cell_create(rootcell, cp);
  120. if (IS_ERR(new_root)) {
  121. _leave(" = %ld", PTR_ERR(new_root));
  122. return PTR_ERR(new_root);
  123. }
  124. /* install the new cell */
  125. write_lock(&afs_cells_lock);
  126. old_root = afs_cell_root;
  127. afs_cell_root = new_root;
  128. write_unlock(&afs_cells_lock);
  129. afs_put_cell(old_root);
  130. _leave(" = 0");
  131. return 0;
  132. }
  133. /*
  134. * lookup a cell record
  135. */
  136. struct afs_cell *afs_cell_lookup(const char *name, unsigned namesz)
  137. {
  138. struct afs_cell *cell;
  139. _enter("\"%*.*s\",", namesz, namesz, name ? name : "");
  140. down_read(&afs_cells_sem);
  141. read_lock(&afs_cells_lock);
  142. if (name) {
  143. /* if the cell was named, look for it in the cell record list */
  144. list_for_each_entry(cell, &afs_cells, link) {
  145. if (strncmp(cell->name, name, namesz) == 0) {
  146. afs_get_cell(cell);
  147. goto found;
  148. }
  149. }
  150. cell = ERR_PTR(-ENOENT);
  151. found:
  152. ;
  153. } else {
  154. cell = afs_cell_root;
  155. if (!cell) {
  156. /* this should not happen unless user tries to mount
  157. * when root cell is not set. Return an impossibly
  158. * bizzare errno to alert the user. Things like
  159. * ENOENT might be "more appropriate" but they happen
  160. * for other reasons.
  161. */
  162. cell = ERR_PTR(-EDESTADDRREQ);
  163. } else {
  164. afs_get_cell(cell);
  165. }
  166. }
  167. read_unlock(&afs_cells_lock);
  168. up_read(&afs_cells_sem);
  169. _leave(" = %p", cell);
  170. return cell;
  171. }
  172. /*
  173. * try and get a cell record
  174. */
  175. struct afs_cell *afs_get_cell_maybe(struct afs_cell *cell)
  176. {
  177. write_lock(&afs_cells_lock);
  178. if (cell && !list_empty(&cell->link))
  179. afs_get_cell(cell);
  180. else
  181. cell = NULL;
  182. write_unlock(&afs_cells_lock);
  183. return cell;
  184. }
  185. /*
  186. * destroy a cell record
  187. */
  188. void afs_put_cell(struct afs_cell *cell)
  189. {
  190. if (!cell)
  191. return;
  192. _enter("%p{%d,%s}", cell, atomic_read(&cell->usage), cell->name);
  193. ASSERTCMP(atomic_read(&cell->usage), >, 0);
  194. /* to prevent a race, the decrement and the dequeue must be effectively
  195. * atomic */
  196. write_lock(&afs_cells_lock);
  197. if (likely(!atomic_dec_and_test(&cell->usage))) {
  198. write_unlock(&afs_cells_lock);
  199. _leave("");
  200. return;
  201. }
  202. ASSERT(list_empty(&cell->servers));
  203. ASSERT(list_empty(&cell->vl_list));
  204. write_unlock(&afs_cells_lock);
  205. wake_up(&afs_cells_freeable_wq);
  206. _leave(" [unused]");
  207. }
  208. /*
  209. * destroy a cell record
  210. * - must be called with the afs_cells_sem write-locked
  211. * - cell->link should have been broken by the caller
  212. */
  213. static void afs_cell_destroy(struct afs_cell *cell)
  214. {
  215. _enter("%p{%d,%s}", cell, atomic_read(&cell->usage), cell->name);
  216. ASSERTCMP(atomic_read(&cell->usage), >=, 0);
  217. ASSERT(list_empty(&cell->link));
  218. /* wait for everyone to stop using the cell */
  219. if (atomic_read(&cell->usage) > 0) {
  220. DECLARE_WAITQUEUE(myself, current);
  221. _debug("wait for cell %s", cell->name);
  222. set_current_state(TASK_UNINTERRUPTIBLE);
  223. add_wait_queue(&afs_cells_freeable_wq, &myself);
  224. while (atomic_read(&cell->usage) > 0) {
  225. schedule();
  226. set_current_state(TASK_UNINTERRUPTIBLE);
  227. }
  228. remove_wait_queue(&afs_cells_freeable_wq, &myself);
  229. set_current_state(TASK_RUNNING);
  230. }
  231. _debug("cell dead");
  232. ASSERTCMP(atomic_read(&cell->usage), ==, 0);
  233. ASSERT(list_empty(&cell->servers));
  234. ASSERT(list_empty(&cell->vl_list));
  235. afs_proc_cell_remove(cell);
  236. down_write(&afs_proc_cells_sem);
  237. list_del_init(&cell->proc_link);
  238. up_write(&afs_proc_cells_sem);
  239. #ifdef AFS_CACHING_SUPPORT
  240. cachefs_relinquish_cookie(cell->cache, 0);
  241. #endif
  242. kfree(cell);
  243. _leave(" [destroyed]");
  244. }
  245. /*
  246. * purge in-memory cell database on module unload or afs_init() failure
  247. * - the timeout daemon is stopped before calling this
  248. */
  249. void afs_cell_purge(void)
  250. {
  251. struct afs_cell *cell;
  252. _enter("");
  253. afs_put_cell(afs_cell_root);
  254. down_write(&afs_cells_sem);
  255. while (!list_empty(&afs_cells)) {
  256. cell = NULL;
  257. /* remove the next cell from the front of the list */
  258. write_lock(&afs_cells_lock);
  259. if (!list_empty(&afs_cells)) {
  260. cell = list_entry(afs_cells.next,
  261. struct afs_cell, link);
  262. list_del_init(&cell->link);
  263. }
  264. write_unlock(&afs_cells_lock);
  265. if (cell) {
  266. _debug("PURGING CELL %s (%d)",
  267. cell->name, atomic_read(&cell->usage));
  268. /* now the cell should be left with no references */
  269. afs_cell_destroy(cell);
  270. }
  271. }
  272. up_write(&afs_cells_sem);
  273. _leave("");
  274. }