cell.c 9.0 KB

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