cell.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 LIST_HEAD(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. down_write(&afs_cells_sem);
  107. read_lock(&afs_cells_lock);
  108. list_for_each_entry(cell, &afs_cells, link) {
  109. if (strcasecmp(cell->name, name) == 0)
  110. goto duplicate_name;
  111. }
  112. read_unlock(&afs_cells_lock);
  113. cell = afs_cell_alloc(name, vllist);
  114. if (IS_ERR(cell)) {
  115. _leave(" = %ld", PTR_ERR(cell));
  116. up_write(&afs_cells_sem);
  117. return cell;
  118. }
  119. /* add a proc directory for this cell */
  120. ret = afs_proc_cell_setup(cell);
  121. if (ret < 0)
  122. goto error;
  123. #ifdef AFS_CACHING_SUPPORT
  124. /* put it up for caching */
  125. cachefs_acquire_cookie(afs_cache_netfs.primary_index,
  126. &afs_vlocation_cache_index_def,
  127. cell,
  128. &cell->cache);
  129. #endif
  130. /* add to the cell lists */
  131. write_lock(&afs_cells_lock);
  132. list_add_tail(&cell->link, &afs_cells);
  133. write_unlock(&afs_cells_lock);
  134. down_write(&afs_proc_cells_sem);
  135. list_add_tail(&cell->proc_link, &afs_proc_cells);
  136. up_write(&afs_proc_cells_sem);
  137. up_write(&afs_cells_sem);
  138. _leave(" = %p", cell);
  139. return cell;
  140. error:
  141. up_write(&afs_cells_sem);
  142. key_put(cell->anonymous_key);
  143. kfree(cell);
  144. _leave(" = %d", ret);
  145. return ERR_PTR(ret);
  146. duplicate_name:
  147. read_unlock(&afs_cells_lock);
  148. up_write(&afs_cells_sem);
  149. return ERR_PTR(-EEXIST);
  150. }
  151. /*
  152. * set the root cell information
  153. * - can be called with a module parameter string
  154. * - can be called from a write to /proc/fs/afs/rootcell
  155. */
  156. int afs_cell_init(char *rootcell)
  157. {
  158. struct afs_cell *old_root, *new_root;
  159. char *cp;
  160. _enter("");
  161. if (!rootcell) {
  162. /* module is loaded with no parameters, or built statically.
  163. * - in the future we might initialize cell DB here.
  164. */
  165. _leave(" = 0 [no root]");
  166. return 0;
  167. }
  168. cp = strchr(rootcell, ':');
  169. if (!cp) {
  170. printk(KERN_ERR "kAFS: no VL server IP addresses specified\n");
  171. _leave(" = -EINVAL");
  172. return -EINVAL;
  173. }
  174. /* allocate a cell record for the root cell */
  175. *cp++ = 0;
  176. new_root = afs_cell_create(rootcell, cp);
  177. if (IS_ERR(new_root)) {
  178. _leave(" = %ld", PTR_ERR(new_root));
  179. return PTR_ERR(new_root);
  180. }
  181. /* install the new cell */
  182. write_lock(&afs_cells_lock);
  183. old_root = afs_cell_root;
  184. afs_cell_root = new_root;
  185. write_unlock(&afs_cells_lock);
  186. afs_put_cell(old_root);
  187. _leave(" = 0");
  188. return 0;
  189. }
  190. /*
  191. * lookup a cell record
  192. */
  193. struct afs_cell *afs_cell_lookup(const char *name, unsigned namesz)
  194. {
  195. struct afs_cell *cell;
  196. _enter("\"%*.*s\",", namesz, namesz, name ? name : "");
  197. down_read(&afs_cells_sem);
  198. read_lock(&afs_cells_lock);
  199. if (name) {
  200. /* if the cell was named, look for it in the cell record list */
  201. list_for_each_entry(cell, &afs_cells, link) {
  202. if (strncmp(cell->name, name, namesz) == 0) {
  203. afs_get_cell(cell);
  204. goto found;
  205. }
  206. }
  207. cell = ERR_PTR(-ENOENT);
  208. found:
  209. ;
  210. } else {
  211. cell = afs_cell_root;
  212. if (!cell) {
  213. /* this should not happen unless user tries to mount
  214. * when root cell is not set. Return an impossibly
  215. * bizzare errno to alert the user. Things like
  216. * ENOENT might be "more appropriate" but they happen
  217. * for other reasons.
  218. */
  219. cell = ERR_PTR(-EDESTADDRREQ);
  220. } else {
  221. afs_get_cell(cell);
  222. }
  223. }
  224. read_unlock(&afs_cells_lock);
  225. up_read(&afs_cells_sem);
  226. _leave(" = %p", cell);
  227. return cell;
  228. }
  229. #if 0
  230. /*
  231. * try and get a cell record
  232. */
  233. struct afs_cell *afs_get_cell_maybe(struct afs_cell *cell)
  234. {
  235. write_lock(&afs_cells_lock);
  236. if (cell && !list_empty(&cell->link))
  237. afs_get_cell(cell);
  238. else
  239. cell = NULL;
  240. write_unlock(&afs_cells_lock);
  241. return cell;
  242. }
  243. #endif /* 0 */
  244. /*
  245. * destroy a cell record
  246. */
  247. void afs_put_cell(struct afs_cell *cell)
  248. {
  249. if (!cell)
  250. return;
  251. _enter("%p{%d,%s}", cell, atomic_read(&cell->usage), cell->name);
  252. ASSERTCMP(atomic_read(&cell->usage), >, 0);
  253. /* to prevent a race, the decrement and the dequeue must be effectively
  254. * atomic */
  255. write_lock(&afs_cells_lock);
  256. if (likely(!atomic_dec_and_test(&cell->usage))) {
  257. write_unlock(&afs_cells_lock);
  258. _leave("");
  259. return;
  260. }
  261. ASSERT(list_empty(&cell->servers));
  262. ASSERT(list_empty(&cell->vl_list));
  263. write_unlock(&afs_cells_lock);
  264. wake_up(&afs_cells_freeable_wq);
  265. _leave(" [unused]");
  266. }
  267. /*
  268. * destroy a cell record
  269. * - must be called with the afs_cells_sem write-locked
  270. * - cell->link should have been broken by the caller
  271. */
  272. static void afs_cell_destroy(struct afs_cell *cell)
  273. {
  274. _enter("%p{%d,%s}", cell, atomic_read(&cell->usage), cell->name);
  275. ASSERTCMP(atomic_read(&cell->usage), >=, 0);
  276. ASSERT(list_empty(&cell->link));
  277. /* wait for everyone to stop using the cell */
  278. if (atomic_read(&cell->usage) > 0) {
  279. DECLARE_WAITQUEUE(myself, current);
  280. _debug("wait for cell %s", cell->name);
  281. set_current_state(TASK_UNINTERRUPTIBLE);
  282. add_wait_queue(&afs_cells_freeable_wq, &myself);
  283. while (atomic_read(&cell->usage) > 0) {
  284. schedule();
  285. set_current_state(TASK_UNINTERRUPTIBLE);
  286. }
  287. remove_wait_queue(&afs_cells_freeable_wq, &myself);
  288. set_current_state(TASK_RUNNING);
  289. }
  290. _debug("cell dead");
  291. ASSERTCMP(atomic_read(&cell->usage), ==, 0);
  292. ASSERT(list_empty(&cell->servers));
  293. ASSERT(list_empty(&cell->vl_list));
  294. afs_proc_cell_remove(cell);
  295. down_write(&afs_proc_cells_sem);
  296. list_del_init(&cell->proc_link);
  297. up_write(&afs_proc_cells_sem);
  298. #ifdef AFS_CACHING_SUPPORT
  299. cachefs_relinquish_cookie(cell->cache, 0);
  300. #endif
  301. key_put(cell->anonymous_key);
  302. kfree(cell);
  303. _leave(" [destroyed]");
  304. }
  305. /*
  306. * purge in-memory cell database on module unload or afs_init() failure
  307. * - the timeout daemon is stopped before calling this
  308. */
  309. void afs_cell_purge(void)
  310. {
  311. struct afs_cell *cell;
  312. _enter("");
  313. afs_put_cell(afs_cell_root);
  314. down_write(&afs_cells_sem);
  315. while (!list_empty(&afs_cells)) {
  316. cell = NULL;
  317. /* remove the next cell from the front of the list */
  318. write_lock(&afs_cells_lock);
  319. if (!list_empty(&afs_cells)) {
  320. cell = list_entry(afs_cells.next,
  321. struct afs_cell, link);
  322. list_del_init(&cell->link);
  323. }
  324. write_unlock(&afs_cells_lock);
  325. if (cell) {
  326. _debug("PURGING CELL %s (%d)",
  327. cell->name, atomic_read(&cell->usage));
  328. /* now the cell should be left with no references */
  329. afs_cell_destroy(cell);
  330. }
  331. }
  332. up_write(&afs_cells_sem);
  333. _leave("");
  334. }