server.c 12 KB

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