cell.c 10 KB

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