cell.c 13 KB

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