cell.c 9.0 KB

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