cell.c 9.6 KB

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