server.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. /* server.c: AFS 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/sched.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 "internal.h"
  22. DEFINE_SPINLOCK(afs_server_peer_lock);
  23. #define FS_SERVICE_ID 1 /* AFS Volume Location Service ID */
  24. #define VL_SERVICE_ID 52 /* AFS Volume Location Service ID */
  25. static void __afs_server_timeout(struct afs_timer *timer)
  26. {
  27. struct afs_server *server =
  28. list_entry(timer, struct afs_server, timeout);
  29. _debug("SERVER TIMEOUT [%p{u=%d}]",
  30. server, atomic_read(&server->usage));
  31. afs_server_do_timeout(server);
  32. }
  33. static const struct afs_timer_ops afs_server_timer_ops = {
  34. .timed_out = __afs_server_timeout,
  35. };
  36. /*****************************************************************************/
  37. /*
  38. * lookup a server record in a cell
  39. * - TODO: search the cell's server list
  40. */
  41. int afs_server_lookup(struct afs_cell *cell, const struct in_addr *addr,
  42. struct afs_server **_server)
  43. {
  44. struct afs_server *server, *active, *zombie;
  45. int loop;
  46. _enter("%p,%08x,", cell, ntohl(addr->s_addr));
  47. /* allocate and initialise a server record */
  48. server = kzalloc(sizeof(struct afs_server), GFP_KERNEL);
  49. if (!server) {
  50. _leave(" = -ENOMEM");
  51. return -ENOMEM;
  52. }
  53. atomic_set(&server->usage, 1);
  54. INIT_LIST_HEAD(&server->link);
  55. init_rwsem(&server->sem);
  56. INIT_LIST_HEAD(&server->fs_callq);
  57. spin_lock_init(&server->fs_lock);
  58. INIT_LIST_HEAD(&server->cb_promises);
  59. spin_lock_init(&server->cb_lock);
  60. for (loop = 0; loop < AFS_SERVER_CONN_LIST_SIZE; loop++)
  61. server->fs_conn_cnt[loop] = 4;
  62. memcpy(&server->addr, addr, sizeof(struct in_addr));
  63. server->addr.s_addr = addr->s_addr;
  64. afs_timer_init(&server->timeout, &afs_server_timer_ops);
  65. /* add to the cell */
  66. write_lock(&cell->sv_lock);
  67. /* check the active list */
  68. list_for_each_entry(active, &cell->sv_list, link) {
  69. if (active->addr.s_addr == addr->s_addr)
  70. goto use_active_server;
  71. }
  72. /* check the inactive list */
  73. spin_lock(&cell->sv_gylock);
  74. list_for_each_entry(zombie, &cell->sv_graveyard, link) {
  75. if (zombie->addr.s_addr == addr->s_addr)
  76. goto resurrect_server;
  77. }
  78. spin_unlock(&cell->sv_gylock);
  79. afs_get_cell(cell);
  80. server->cell = cell;
  81. list_add_tail(&server->link, &cell->sv_list);
  82. write_unlock(&cell->sv_lock);
  83. *_server = server;
  84. _leave(" = 0 (%p)", server);
  85. return 0;
  86. /* found a matching active server */
  87. use_active_server:
  88. _debug("active server");
  89. afs_get_server(active);
  90. write_unlock(&cell->sv_lock);
  91. kfree(server);
  92. *_server = active;
  93. _leave(" = 0 (%p)", active);
  94. return 0;
  95. /* found a matching server in the graveyard, so resurrect it and
  96. * dispose of the new record */
  97. resurrect_server:
  98. _debug("resurrecting server");
  99. list_move_tail(&zombie->link, &cell->sv_list);
  100. afs_get_server(zombie);
  101. afs_kafstimod_del_timer(&zombie->timeout);
  102. spin_unlock(&cell->sv_gylock);
  103. write_unlock(&cell->sv_lock);
  104. kfree(server);
  105. *_server = zombie;
  106. _leave(" = 0 (%p)", zombie);
  107. return 0;
  108. } /* end afs_server_lookup() */
  109. /*****************************************************************************/
  110. /*
  111. * destroy a server record
  112. * - removes from the cell list
  113. */
  114. void afs_put_server(struct afs_server *server)
  115. {
  116. struct afs_cell *cell;
  117. if (!server)
  118. return;
  119. _enter("%p", server);
  120. cell = server->cell;
  121. /* sanity check */
  122. BUG_ON(atomic_read(&server->usage) <= 0);
  123. /* to prevent a race, the decrement and the dequeue must be effectively
  124. * atomic */
  125. write_lock(&cell->sv_lock);
  126. if (likely(!atomic_dec_and_test(&server->usage))) {
  127. write_unlock(&cell->sv_lock);
  128. _leave("");
  129. return;
  130. }
  131. spin_lock(&cell->sv_gylock);
  132. list_move_tail(&server->link, &cell->sv_graveyard);
  133. /* time out in 10 secs */
  134. afs_kafstimod_add_timer(&server->timeout, 10 * HZ);
  135. spin_unlock(&cell->sv_gylock);
  136. write_unlock(&cell->sv_lock);
  137. _leave(" [killed]");
  138. } /* end afs_put_server() */
  139. /*****************************************************************************/
  140. /*
  141. * timeout server record
  142. * - removes from the cell's graveyard if the usage count is zero
  143. */
  144. void afs_server_do_timeout(struct afs_server *server)
  145. {
  146. struct rxrpc_peer *peer;
  147. struct afs_cell *cell;
  148. int loop;
  149. _enter("%p", server);
  150. cell = server->cell;
  151. BUG_ON(atomic_read(&server->usage) < 0);
  152. /* remove from graveyard if still dead */
  153. spin_lock(&cell->vl_gylock);
  154. if (atomic_read(&server->usage) == 0)
  155. list_del_init(&server->link);
  156. else
  157. server = NULL;
  158. spin_unlock(&cell->vl_gylock);
  159. if (!server) {
  160. _leave("");
  161. return; /* resurrected */
  162. }
  163. /* we can now destroy it properly */
  164. afs_put_cell(cell);
  165. /* uncross-point the structs under a global lock */
  166. spin_lock(&afs_server_peer_lock);
  167. peer = server->peer;
  168. if (peer) {
  169. server->peer = NULL;
  170. peer->user = NULL;
  171. }
  172. spin_unlock(&afs_server_peer_lock);
  173. /* finish cleaning up the server */
  174. for (loop = AFS_SERVER_CONN_LIST_SIZE - 1; loop >= 0; loop--)
  175. if (server->fs_conn[loop])
  176. rxrpc_put_connection(server->fs_conn[loop]);
  177. if (server->vlserver)
  178. rxrpc_put_connection(server->vlserver);
  179. kfree(server);
  180. _leave(" [destroyed]");
  181. } /* end afs_server_do_timeout() */
  182. /*****************************************************************************/
  183. /*
  184. * get a callslot on a connection to the fileserver on the specified server
  185. */
  186. int afs_server_request_callslot(struct afs_server *server,
  187. struct afs_server_callslot *callslot)
  188. {
  189. struct afs_server_callslot *pcallslot;
  190. struct rxrpc_connection *conn;
  191. int nconn, ret;
  192. _enter("%p,",server);
  193. INIT_LIST_HEAD(&callslot->link);
  194. callslot->task = current;
  195. callslot->conn = NULL;
  196. callslot->nconn = -1;
  197. callslot->ready = 0;
  198. ret = 0;
  199. conn = NULL;
  200. /* get hold of a callslot first */
  201. spin_lock(&server->fs_lock);
  202. /* resurrect the server if it's death timeout has expired */
  203. if (server->fs_state) {
  204. if (time_before(jiffies, server->fs_dead_jif)) {
  205. ret = server->fs_state;
  206. spin_unlock(&server->fs_lock);
  207. _leave(" = %d [still dead]", ret);
  208. return ret;
  209. }
  210. server->fs_state = 0;
  211. }
  212. /* try and find a connection that has spare callslots */
  213. for (nconn = 0; nconn < AFS_SERVER_CONN_LIST_SIZE; nconn++) {
  214. if (server->fs_conn_cnt[nconn] > 0) {
  215. server->fs_conn_cnt[nconn]--;
  216. spin_unlock(&server->fs_lock);
  217. callslot->nconn = nconn;
  218. goto obtained_slot;
  219. }
  220. }
  221. /* none were available - wait interruptibly for one to become
  222. * available */
  223. set_current_state(TASK_INTERRUPTIBLE);
  224. list_add_tail(&callslot->link, &server->fs_callq);
  225. spin_unlock(&server->fs_lock);
  226. while (!callslot->ready && !signal_pending(current)) {
  227. schedule();
  228. set_current_state(TASK_INTERRUPTIBLE);
  229. }
  230. set_current_state(TASK_RUNNING);
  231. /* even if we were interrupted we may still be queued */
  232. if (!callslot->ready) {
  233. spin_lock(&server->fs_lock);
  234. list_del_init(&callslot->link);
  235. spin_unlock(&server->fs_lock);
  236. }
  237. nconn = callslot->nconn;
  238. /* if interrupted, we must release any slot we also got before
  239. * returning an error */
  240. if (signal_pending(current)) {
  241. ret = -EINTR;
  242. goto error_release;
  243. }
  244. /* if we were woken up with an error, then pass that error back to the
  245. * called */
  246. if (nconn < 0) {
  247. _leave(" = %d", callslot->errno);
  248. return callslot->errno;
  249. }
  250. /* were we given a connection directly? */
  251. if (callslot->conn) {
  252. /* yes - use it */
  253. _leave(" = 0 (nc=%d)", nconn);
  254. return 0;
  255. }
  256. /* got a callslot, but no connection */
  257. obtained_slot:
  258. /* need to get hold of the RxRPC connection */
  259. down_write(&server->sem);
  260. /* quick check to see if there's an outstanding error */
  261. ret = server->fs_state;
  262. if (ret)
  263. goto error_release_upw;
  264. if (server->fs_conn[nconn]) {
  265. /* reuse an existing connection */
  266. rxrpc_get_connection(server->fs_conn[nconn]);
  267. callslot->conn = server->fs_conn[nconn];
  268. }
  269. else {
  270. /* create a new connection */
  271. ret = rxrpc_create_connection(afs_transport,
  272. htons(7000),
  273. server->addr.s_addr,
  274. FS_SERVICE_ID,
  275. NULL,
  276. &server->fs_conn[nconn]);
  277. if (ret < 0)
  278. goto error_release_upw;
  279. callslot->conn = server->fs_conn[0];
  280. rxrpc_get_connection(callslot->conn);
  281. }
  282. up_write(&server->sem);
  283. _leave(" = 0");
  284. return 0;
  285. /* handle an error occurring */
  286. error_release_upw:
  287. up_write(&server->sem);
  288. error_release:
  289. /* either release the callslot or pass it along to another deserving
  290. * task */
  291. spin_lock(&server->fs_lock);
  292. if (nconn < 0) {
  293. /* no callslot allocated */
  294. }
  295. else if (list_empty(&server->fs_callq)) {
  296. /* no one waiting */
  297. server->fs_conn_cnt[nconn]++;
  298. spin_unlock(&server->fs_lock);
  299. }
  300. else {
  301. /* someone's waiting - dequeue them and wake them up */
  302. pcallslot = list_entry(server->fs_callq.next,
  303. struct afs_server_callslot, link);
  304. list_del_init(&pcallslot->link);
  305. pcallslot->errno = server->fs_state;
  306. if (!pcallslot->errno) {
  307. /* pass them out callslot details */
  308. callslot->conn = xchg(&pcallslot->conn,
  309. callslot->conn);
  310. pcallslot->nconn = nconn;
  311. callslot->nconn = nconn = -1;
  312. }
  313. pcallslot->ready = 1;
  314. wake_up_process(pcallslot->task);
  315. spin_unlock(&server->fs_lock);
  316. }
  317. rxrpc_put_connection(callslot->conn);
  318. callslot->conn = NULL;
  319. _leave(" = %d", ret);
  320. return ret;
  321. } /* end afs_server_request_callslot() */
  322. /*****************************************************************************/
  323. /*
  324. * release a callslot back to the server
  325. * - transfers the RxRPC connection to the next pending callslot if possible
  326. */
  327. void afs_server_release_callslot(struct afs_server *server,
  328. struct afs_server_callslot *callslot)
  329. {
  330. struct afs_server_callslot *pcallslot;
  331. _enter("{ad=%08x,cnt=%u},{%d}",
  332. ntohl(server->addr.s_addr),
  333. server->fs_conn_cnt[callslot->nconn],
  334. callslot->nconn);
  335. BUG_ON(callslot->nconn < 0);
  336. spin_lock(&server->fs_lock);
  337. if (list_empty(&server->fs_callq)) {
  338. /* no one waiting */
  339. server->fs_conn_cnt[callslot->nconn]++;
  340. spin_unlock(&server->fs_lock);
  341. }
  342. else {
  343. /* someone's waiting - dequeue them and wake them up */
  344. pcallslot = list_entry(server->fs_callq.next,
  345. struct afs_server_callslot, link);
  346. list_del_init(&pcallslot->link);
  347. pcallslot->errno = server->fs_state;
  348. if (!pcallslot->errno) {
  349. /* pass them out callslot details */
  350. callslot->conn = xchg(&pcallslot->conn, callslot->conn);
  351. pcallslot->nconn = callslot->nconn;
  352. callslot->nconn = -1;
  353. }
  354. pcallslot->ready = 1;
  355. wake_up_process(pcallslot->task);
  356. spin_unlock(&server->fs_lock);
  357. }
  358. rxrpc_put_connection(callslot->conn);
  359. _leave("");
  360. } /* end afs_server_release_callslot() */
  361. /*****************************************************************************/
  362. /*
  363. * get a handle to a connection to the vlserver (volume location) on the
  364. * specified server
  365. */
  366. int afs_server_get_vlconn(struct afs_server *server,
  367. struct rxrpc_connection **_conn)
  368. {
  369. struct rxrpc_connection *conn;
  370. int ret;
  371. _enter("%p,", server);
  372. ret = 0;
  373. conn = NULL;
  374. down_read(&server->sem);
  375. if (server->vlserver) {
  376. /* reuse an existing connection */
  377. rxrpc_get_connection(server->vlserver);
  378. conn = server->vlserver;
  379. up_read(&server->sem);
  380. }
  381. else {
  382. /* create a new connection */
  383. up_read(&server->sem);
  384. down_write(&server->sem);
  385. if (!server->vlserver) {
  386. ret = rxrpc_create_connection(afs_transport,
  387. htons(7003),
  388. server->addr.s_addr,
  389. VL_SERVICE_ID,
  390. NULL,
  391. &server->vlserver);
  392. }
  393. if (ret == 0) {
  394. rxrpc_get_connection(server->vlserver);
  395. conn = server->vlserver;
  396. }
  397. up_write(&server->sem);
  398. }
  399. *_conn = conn;
  400. _leave(" = %d", ret);
  401. return ret;
  402. } /* end afs_server_get_vlconn() */