cell.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /* cell.c: 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/sched.h>
  13. #include <linux/slab.h>
  14. #include <rxrpc/peer.h>
  15. #include <rxrpc/connection.h>
  16. #include "volume.h"
  17. #include "cell.h"
  18. #include "server.h"
  19. #include "transport.h"
  20. #include "vlclient.h"
  21. #include "kafstimod.h"
  22. #include "super.h"
  23. #include "internal.h"
  24. DECLARE_RWSEM(afs_proc_cells_sem);
  25. LIST_HEAD(afs_proc_cells);
  26. static struct list_head afs_cells = LIST_HEAD_INIT(afs_cells);
  27. static DEFINE_RWLOCK(afs_cells_lock);
  28. static DECLARE_RWSEM(afs_cells_sem); /* add/remove serialisation */
  29. static struct afs_cell *afs_cell_root;
  30. #ifdef AFS_CACHING_SUPPORT
  31. static cachefs_match_val_t afs_cell_cache_match(void *target,
  32. const void *entry);
  33. static void afs_cell_cache_update(void *source, void *entry);
  34. struct cachefs_index_def afs_cache_cell_index_def = {
  35. .name = "cell_ix",
  36. .data_size = sizeof(struct afs_cache_cell),
  37. .keys[0] = { CACHEFS_INDEX_KEYS_ASCIIZ, 64 },
  38. .match = afs_cell_cache_match,
  39. .update = afs_cell_cache_update,
  40. };
  41. #endif
  42. /*****************************************************************************/
  43. /*
  44. * create a cell record
  45. * - "name" is the name of the cell
  46. * - "vllist" is a colon separated list of IP addresses in "a.b.c.d" format
  47. */
  48. int afs_cell_create(const char *name, char *vllist, struct afs_cell **_cell)
  49. {
  50. struct afs_cell *cell;
  51. char *next;
  52. int ret;
  53. _enter("%s", name);
  54. BUG_ON(!name); /* TODO: want to look up "this cell" in the cache */
  55. /* allocate and initialise a cell record */
  56. cell = kmalloc(sizeof(struct afs_cell) + strlen(name) + 1, GFP_KERNEL);
  57. if (!cell) {
  58. _leave(" = -ENOMEM");
  59. return -ENOMEM;
  60. }
  61. down_write(&afs_cells_sem);
  62. memset(cell, 0, sizeof(struct afs_cell));
  63. atomic_set(&cell->usage, 0);
  64. INIT_LIST_HEAD(&cell->link);
  65. rwlock_init(&cell->sv_lock);
  66. INIT_LIST_HEAD(&cell->sv_list);
  67. INIT_LIST_HEAD(&cell->sv_graveyard);
  68. spin_lock_init(&cell->sv_gylock);
  69. init_rwsem(&cell->vl_sem);
  70. INIT_LIST_HEAD(&cell->vl_list);
  71. INIT_LIST_HEAD(&cell->vl_graveyard);
  72. spin_lock_init(&cell->vl_gylock);
  73. strcpy(cell->name,name);
  74. /* fill in the VL server list from the rest of the string */
  75. ret = -EINVAL;
  76. do {
  77. unsigned a, b, c, d;
  78. next = strchr(vllist, ':');
  79. if (next)
  80. *next++ = 0;
  81. if (sscanf(vllist, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
  82. goto badaddr;
  83. if (a > 255 || b > 255 || c > 255 || d > 255)
  84. goto badaddr;
  85. cell->vl_addrs[cell->vl_naddrs++].s_addr =
  86. htonl((a << 24) | (b << 16) | (c << 8) | d);
  87. if (cell->vl_naddrs >= AFS_CELL_MAX_ADDRS)
  88. break;
  89. } while(vllist = next, vllist);
  90. /* add a proc dir for this cell */
  91. ret = afs_proc_cell_setup(cell);
  92. if (ret < 0)
  93. goto error;
  94. #ifdef AFS_CACHING_SUPPORT
  95. /* put it up for caching */
  96. cachefs_acquire_cookie(afs_cache_netfs.primary_index,
  97. &afs_vlocation_cache_index_def,
  98. cell,
  99. &cell->cache);
  100. #endif
  101. /* add to the cell lists */
  102. write_lock(&afs_cells_lock);
  103. list_add_tail(&cell->link, &afs_cells);
  104. write_unlock(&afs_cells_lock);
  105. down_write(&afs_proc_cells_sem);
  106. list_add_tail(&cell->proc_link, &afs_proc_cells);
  107. up_write(&afs_proc_cells_sem);
  108. *_cell = cell;
  109. up_write(&afs_cells_sem);
  110. _leave(" = 0 (%p)", cell);
  111. return 0;
  112. badaddr:
  113. printk(KERN_ERR "kAFS: bad VL server IP address: '%s'\n", vllist);
  114. error:
  115. up_write(&afs_cells_sem);
  116. kfree(cell);
  117. _leave(" = %d", ret);
  118. return ret;
  119. } /* end afs_cell_create() */
  120. /*****************************************************************************/
  121. /*
  122. * initialise the cell database from module parameters
  123. */
  124. int afs_cell_init(char *rootcell)
  125. {
  126. struct afs_cell *old_root, *new_root;
  127. char *cp;
  128. int ret;
  129. _enter("");
  130. if (!rootcell) {
  131. /* module is loaded with no parameters, or built statically.
  132. * - in the future we might initialize cell DB here.
  133. */
  134. _leave(" = 0 (but no root)");
  135. return 0;
  136. }
  137. cp = strchr(rootcell, ':');
  138. if (!cp) {
  139. printk(KERN_ERR "kAFS: no VL server IP addresses specified\n");
  140. _leave(" = %d (no colon)", -EINVAL);
  141. return -EINVAL;
  142. }
  143. /* allocate a cell record for the root cell */
  144. *cp++ = 0;
  145. ret = afs_cell_create(rootcell, cp, &new_root);
  146. if (ret < 0) {
  147. _leave(" = %d", ret);
  148. return ret;
  149. }
  150. /* as afs_put_cell() takes locks by itself, we have to do
  151. * a little gymnastics to be race-free.
  152. */
  153. afs_get_cell(new_root);
  154. write_lock(&afs_cells_lock);
  155. while (afs_cell_root) {
  156. old_root = afs_cell_root;
  157. afs_cell_root = NULL;
  158. write_unlock(&afs_cells_lock);
  159. afs_put_cell(old_root);
  160. write_lock(&afs_cells_lock);
  161. }
  162. afs_cell_root = new_root;
  163. write_unlock(&afs_cells_lock);
  164. _leave(" = %d", ret);
  165. return ret;
  166. } /* end afs_cell_init() */
  167. /*****************************************************************************/
  168. /*
  169. * lookup a cell record
  170. */
  171. int afs_cell_lookup(const char *name, unsigned namesz, struct afs_cell **_cell)
  172. {
  173. struct afs_cell *cell;
  174. int ret;
  175. _enter("\"%*.*s\",", namesz, namesz, name ? name : "");
  176. *_cell = NULL;
  177. if (name) {
  178. /* if the cell was named, look for it in the cell record list */
  179. ret = -ENOENT;
  180. cell = NULL;
  181. read_lock(&afs_cells_lock);
  182. list_for_each_entry(cell, &afs_cells, link) {
  183. if (strncmp(cell->name, name, namesz) == 0) {
  184. afs_get_cell(cell);
  185. goto found;
  186. }
  187. }
  188. cell = NULL;
  189. found:
  190. read_unlock(&afs_cells_lock);
  191. if (cell)
  192. ret = 0;
  193. }
  194. else {
  195. read_lock(&afs_cells_lock);
  196. cell = afs_cell_root;
  197. if (!cell) {
  198. /* this should not happen unless user tries to mount
  199. * when root cell is not set. Return an impossibly
  200. * bizzare errno to alert the user. Things like
  201. * ENOENT might be "more appropriate" but they happen
  202. * for other reasons.
  203. */
  204. ret = -EDESTADDRREQ;
  205. }
  206. else {
  207. afs_get_cell(cell);
  208. ret = 0;
  209. }
  210. read_unlock(&afs_cells_lock);
  211. }
  212. *_cell = cell;
  213. _leave(" = %d (%p)", ret, cell);
  214. return ret;
  215. } /* end afs_cell_lookup() */
  216. /*****************************************************************************/
  217. /*
  218. * try and get a cell record
  219. */
  220. struct afs_cell *afs_get_cell_maybe(struct afs_cell **_cell)
  221. {
  222. struct afs_cell *cell;
  223. write_lock(&afs_cells_lock);
  224. cell = *_cell;
  225. if (cell && !list_empty(&cell->link))
  226. afs_get_cell(cell);
  227. else
  228. cell = NULL;
  229. write_unlock(&afs_cells_lock);
  230. return cell;
  231. } /* end afs_get_cell_maybe() */
  232. /*****************************************************************************/
  233. /*
  234. * destroy a cell record
  235. */
  236. void afs_put_cell(struct afs_cell *cell)
  237. {
  238. if (!cell)
  239. return;
  240. _enter("%p{%d,%s}", cell, atomic_read(&cell->usage), cell->name);
  241. /* sanity check */
  242. BUG_ON(atomic_read(&cell->usage) <= 0);
  243. /* to prevent a race, the decrement and the dequeue must be effectively
  244. * atomic */
  245. write_lock(&afs_cells_lock);
  246. if (likely(!atomic_dec_and_test(&cell->usage))) {
  247. write_unlock(&afs_cells_lock);
  248. _leave("");
  249. return;
  250. }
  251. write_unlock(&afs_cells_lock);
  252. BUG_ON(!list_empty(&cell->sv_list));
  253. BUG_ON(!list_empty(&cell->sv_graveyard));
  254. BUG_ON(!list_empty(&cell->vl_list));
  255. BUG_ON(!list_empty(&cell->vl_graveyard));
  256. _leave(" [unused]");
  257. } /* end afs_put_cell() */
  258. /*****************************************************************************/
  259. /*
  260. * destroy a cell record
  261. */
  262. static void afs_cell_destroy(struct afs_cell *cell)
  263. {
  264. _enter("%p{%d,%s}", cell, atomic_read(&cell->usage), cell->name);
  265. /* to prevent a race, the decrement and the dequeue must be effectively
  266. * atomic */
  267. write_lock(&afs_cells_lock);
  268. /* sanity check */
  269. BUG_ON(atomic_read(&cell->usage) != 0);
  270. list_del_init(&cell->link);
  271. write_unlock(&afs_cells_lock);
  272. down_write(&afs_cells_sem);
  273. afs_proc_cell_remove(cell);
  274. down_write(&afs_proc_cells_sem);
  275. list_del_init(&cell->proc_link);
  276. up_write(&afs_proc_cells_sem);
  277. #ifdef AFS_CACHING_SUPPORT
  278. cachefs_relinquish_cookie(cell->cache, 0);
  279. #endif
  280. up_write(&afs_cells_sem);
  281. BUG_ON(!list_empty(&cell->sv_list));
  282. BUG_ON(!list_empty(&cell->sv_graveyard));
  283. BUG_ON(!list_empty(&cell->vl_list));
  284. BUG_ON(!list_empty(&cell->vl_graveyard));
  285. /* finish cleaning up the cell */
  286. kfree(cell);
  287. _leave(" [destroyed]");
  288. } /* end afs_cell_destroy() */
  289. /*****************************************************************************/
  290. /*
  291. * lookup the server record corresponding to an Rx RPC peer
  292. */
  293. int afs_server_find_by_peer(const struct rxrpc_peer *peer,
  294. struct afs_server **_server)
  295. {
  296. struct afs_server *server;
  297. struct afs_cell *cell;
  298. _enter("%p{a=%08x},", peer, ntohl(peer->addr.s_addr));
  299. /* search the cell list */
  300. read_lock(&afs_cells_lock);
  301. list_for_each_entry(cell, &afs_cells, link) {
  302. _debug("? cell %s",cell->name);
  303. write_lock(&cell->sv_lock);
  304. /* check the active list */
  305. list_for_each_entry(server, &cell->sv_list, link) {
  306. _debug("?? server %08x", ntohl(server->addr.s_addr));
  307. if (memcmp(&server->addr, &peer->addr,
  308. sizeof(struct in_addr)) == 0)
  309. goto found_server;
  310. }
  311. /* check the inactive list */
  312. spin_lock(&cell->sv_gylock);
  313. list_for_each_entry(server, &cell->sv_graveyard, link) {
  314. _debug("?? dead server %08x",
  315. ntohl(server->addr.s_addr));
  316. if (memcmp(&server->addr, &peer->addr,
  317. sizeof(struct in_addr)) == 0)
  318. goto found_dead_server;
  319. }
  320. spin_unlock(&cell->sv_gylock);
  321. write_unlock(&cell->sv_lock);
  322. }
  323. read_unlock(&afs_cells_lock);
  324. _leave(" = -ENOENT");
  325. return -ENOENT;
  326. /* we found it in the graveyard - resurrect it */
  327. found_dead_server:
  328. list_del(&server->link);
  329. list_add_tail(&server->link, &cell->sv_list);
  330. afs_get_server(server);
  331. afs_kafstimod_del_timer(&server->timeout);
  332. spin_unlock(&cell->sv_gylock);
  333. goto success;
  334. /* we found it - increment its ref count and return it */
  335. found_server:
  336. afs_get_server(server);
  337. success:
  338. write_unlock(&cell->sv_lock);
  339. read_unlock(&afs_cells_lock);
  340. *_server = server;
  341. _leave(" = 0 (s=%p c=%p)", server, cell);
  342. return 0;
  343. } /* end afs_server_find_by_peer() */
  344. /*****************************************************************************/
  345. /*
  346. * purge in-memory cell database on module unload or afs_init() failure
  347. * - the timeout daemon is stopped before calling this
  348. */
  349. void afs_cell_purge(void)
  350. {
  351. struct afs_vlocation *vlocation;
  352. struct afs_cell *cell;
  353. _enter("");
  354. afs_put_cell(afs_cell_root);
  355. while (!list_empty(&afs_cells)) {
  356. cell = NULL;
  357. /* remove the next cell from the front of the list */
  358. write_lock(&afs_cells_lock);
  359. if (!list_empty(&afs_cells)) {
  360. cell = list_entry(afs_cells.next,
  361. struct afs_cell, link);
  362. list_del_init(&cell->link);
  363. }
  364. write_unlock(&afs_cells_lock);
  365. if (cell) {
  366. _debug("PURGING CELL %s (%d)",
  367. cell->name, atomic_read(&cell->usage));
  368. BUG_ON(!list_empty(&cell->sv_list));
  369. BUG_ON(!list_empty(&cell->vl_list));
  370. /* purge the cell's VL graveyard list */
  371. _debug(" - clearing VL graveyard");
  372. spin_lock(&cell->vl_gylock);
  373. while (!list_empty(&cell->vl_graveyard)) {
  374. vlocation = list_entry(cell->vl_graveyard.next,
  375. struct afs_vlocation,
  376. link);
  377. list_del_init(&vlocation->link);
  378. afs_kafstimod_del_timer(&vlocation->timeout);
  379. spin_unlock(&cell->vl_gylock);
  380. afs_vlocation_do_timeout(vlocation);
  381. /* TODO: race if move to use krxtimod instead
  382. * of kafstimod */
  383. spin_lock(&cell->vl_gylock);
  384. }
  385. spin_unlock(&cell->vl_gylock);
  386. /* purge the cell's server graveyard list */
  387. _debug(" - clearing server graveyard");
  388. spin_lock(&cell->sv_gylock);
  389. while (!list_empty(&cell->sv_graveyard)) {
  390. struct afs_server *server;
  391. server = list_entry(cell->sv_graveyard.next,
  392. struct afs_server, link);
  393. list_del_init(&server->link);
  394. afs_kafstimod_del_timer(&server->timeout);
  395. spin_unlock(&cell->sv_gylock);
  396. afs_server_do_timeout(server);
  397. spin_lock(&cell->sv_gylock);
  398. }
  399. spin_unlock(&cell->sv_gylock);
  400. /* now the cell should be left with no references */
  401. afs_cell_destroy(cell);
  402. }
  403. }
  404. _leave("");
  405. } /* end afs_cell_purge() */
  406. /*****************************************************************************/
  407. /*
  408. * match a cell record obtained from the cache
  409. */
  410. #ifdef AFS_CACHING_SUPPORT
  411. static cachefs_match_val_t afs_cell_cache_match(void *target,
  412. const void *entry)
  413. {
  414. const struct afs_cache_cell *ccell = entry;
  415. struct afs_cell *cell = target;
  416. _enter("{%s},{%s}", ccell->name, cell->name);
  417. if (strncmp(ccell->name, cell->name, sizeof(ccell->name)) == 0) {
  418. _leave(" = SUCCESS");
  419. return CACHEFS_MATCH_SUCCESS;
  420. }
  421. _leave(" = FAILED");
  422. return CACHEFS_MATCH_FAILED;
  423. } /* end afs_cell_cache_match() */
  424. #endif
  425. /*****************************************************************************/
  426. /*
  427. * update a cell record in the cache
  428. */
  429. #ifdef AFS_CACHING_SUPPORT
  430. static void afs_cell_cache_update(void *source, void *entry)
  431. {
  432. struct afs_cache_cell *ccell = entry;
  433. struct afs_cell *cell = source;
  434. _enter("%p,%p", source, entry);
  435. strncpy(ccell->name, cell->name, sizeof(ccell->name));
  436. memcpy(ccell->vl_servers,
  437. cell->vl_addrs,
  438. min(sizeof(ccell->vl_servers), sizeof(cell->vl_addrs)));
  439. } /* end afs_cell_cache_update() */
  440. #endif