server.c 12 KB

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