cell.c 8.8 KB

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