cell.c 9.1 KB

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