1 /* server.c: AFS server record management
3 * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
12 #include <linux/sched.h>
13 #include <linux/slab.h>
14 #include <rxrpc/peer.h>
15 #include <rxrpc/connection.h>
19 #include "transport.h"
21 #include "kafstimod.h"
24 DEFINE_SPINLOCK(afs_server_peer_lock
);
26 #define FS_SERVICE_ID 1 /* AFS Volume Location Service ID */
27 #define VL_SERVICE_ID 52 /* AFS Volume Location Service ID */
29 static void __afs_server_timeout(struct afs_timer
*timer
)
31 struct afs_server
*server
=
32 list_entry(timer
, struct afs_server
, timeout
);
34 _debug("SERVER TIMEOUT [%p{u=%d}]",
35 server
, atomic_read(&server
->usage
));
37 afs_server_do_timeout(server
);
40 static const struct afs_timer_ops afs_server_timer_ops
= {
41 .timed_out
= __afs_server_timeout
,
44 /*****************************************************************************/
46 * lookup a server record in a cell
47 * - TODO: search the cell's server list
49 int afs_server_lookup(struct afs_cell
*cell
, const struct in_addr
*addr
,
50 struct afs_server
**_server
)
52 struct afs_server
*server
, *active
, *zombie
;
55 _enter("%p,%08x,", cell
, ntohl(addr
->s_addr
));
57 /* allocate and initialise a server record */
58 server
= kzalloc(sizeof(struct afs_server
), GFP_KERNEL
);
64 atomic_set(&server
->usage
, 1);
66 INIT_LIST_HEAD(&server
->link
);
67 init_rwsem(&server
->sem
);
68 INIT_LIST_HEAD(&server
->fs_callq
);
69 spin_lock_init(&server
->fs_lock
);
70 INIT_LIST_HEAD(&server
->cb_promises
);
71 spin_lock_init(&server
->cb_lock
);
73 for (loop
= 0; loop
< AFS_SERVER_CONN_LIST_SIZE
; loop
++)
74 server
->fs_conn_cnt
[loop
] = 4;
76 memcpy(&server
->addr
, addr
, sizeof(struct in_addr
));
77 server
->addr
.s_addr
= addr
->s_addr
;
79 afs_timer_init(&server
->timeout
, &afs_server_timer_ops
);
82 write_lock(&cell
->sv_lock
);
84 /* check the active list */
85 list_for_each_entry(active
, &cell
->sv_list
, link
) {
86 if (active
->addr
.s_addr
== addr
->s_addr
)
87 goto use_active_server
;
90 /* check the inactive list */
91 spin_lock(&cell
->sv_gylock
);
92 list_for_each_entry(zombie
, &cell
->sv_graveyard
, link
) {
93 if (zombie
->addr
.s_addr
== addr
->s_addr
)
94 goto resurrect_server
;
96 spin_unlock(&cell
->sv_gylock
);
100 list_add_tail(&server
->link
, &cell
->sv_list
);
102 write_unlock(&cell
->sv_lock
);
105 _leave(" = 0 (%p)", server
);
108 /* found a matching active server */
110 _debug("active server");
111 afs_get_server(active
);
112 write_unlock(&cell
->sv_lock
);
117 _leave(" = 0 (%p)", active
);
120 /* found a matching server in the graveyard, so resurrect it and
121 * dispose of the new record */
123 _debug("resurrecting server");
125 list_move_tail(&zombie
->link
, &cell
->sv_list
);
126 afs_get_server(zombie
);
127 afs_kafstimod_del_timer(&zombie
->timeout
);
128 spin_unlock(&cell
->sv_gylock
);
129 write_unlock(&cell
->sv_lock
);
134 _leave(" = 0 (%p)", zombie
);
137 } /* end afs_server_lookup() */
139 /*****************************************************************************/
141 * destroy a server record
142 * - removes from the cell list
144 void afs_put_server(struct afs_server
*server
)
146 struct afs_cell
*cell
;
151 _enter("%p", server
);
156 BUG_ON(atomic_read(&server
->usage
) <= 0);
158 /* to prevent a race, the decrement and the dequeue must be effectively
160 write_lock(&cell
->sv_lock
);
162 if (likely(!atomic_dec_and_test(&server
->usage
))) {
163 write_unlock(&cell
->sv_lock
);
168 spin_lock(&cell
->sv_gylock
);
169 list_move_tail(&server
->link
, &cell
->sv_graveyard
);
171 /* time out in 10 secs */
172 afs_kafstimod_add_timer(&server
->timeout
, 10 * HZ
);
174 spin_unlock(&cell
->sv_gylock
);
175 write_unlock(&cell
->sv_lock
);
178 } /* end afs_put_server() */
180 /*****************************************************************************/
182 * timeout server record
183 * - removes from the cell's graveyard if the usage count is zero
185 void afs_server_do_timeout(struct afs_server
*server
)
187 struct rxrpc_peer
*peer
;
188 struct afs_cell
*cell
;
191 _enter("%p", server
);
195 BUG_ON(atomic_read(&server
->usage
) < 0);
197 /* remove from graveyard if still dead */
198 spin_lock(&cell
->vl_gylock
);
199 if (atomic_read(&server
->usage
) == 0)
200 list_del_init(&server
->link
);
203 spin_unlock(&cell
->vl_gylock
);
207 return; /* resurrected */
210 /* we can now destroy it properly */
213 /* uncross-point the structs under a global lock */
214 spin_lock(&afs_server_peer_lock
);
220 spin_unlock(&afs_server_peer_lock
);
222 /* finish cleaning up the server */
223 for (loop
= AFS_SERVER_CONN_LIST_SIZE
- 1; loop
>= 0; loop
--)
224 if (server
->fs_conn
[loop
])
225 rxrpc_put_connection(server
->fs_conn
[loop
]);
227 if (server
->vlserver
)
228 rxrpc_put_connection(server
->vlserver
);
232 _leave(" [destroyed]");
233 } /* end afs_server_do_timeout() */
235 /*****************************************************************************/
237 * get a callslot on a connection to the fileserver on the specified server
239 int afs_server_request_callslot(struct afs_server
*server
,
240 struct afs_server_callslot
*callslot
)
242 struct afs_server_callslot
*pcallslot
;
243 struct rxrpc_connection
*conn
;
246 _enter("%p,",server
);
248 INIT_LIST_HEAD(&callslot
->link
);
249 callslot
->task
= current
;
250 callslot
->conn
= NULL
;
251 callslot
->nconn
= -1;
257 /* get hold of a callslot first */
258 spin_lock(&server
->fs_lock
);
260 /* resurrect the server if it's death timeout has expired */
261 if (server
->fs_state
) {
262 if (time_before(jiffies
, server
->fs_dead_jif
)) {
263 ret
= server
->fs_state
;
264 spin_unlock(&server
->fs_lock
);
265 _leave(" = %d [still dead]", ret
);
269 server
->fs_state
= 0;
272 /* try and find a connection that has spare callslots */
273 for (nconn
= 0; nconn
< AFS_SERVER_CONN_LIST_SIZE
; nconn
++) {
274 if (server
->fs_conn_cnt
[nconn
] > 0) {
275 server
->fs_conn_cnt
[nconn
]--;
276 spin_unlock(&server
->fs_lock
);
277 callslot
->nconn
= nconn
;
282 /* none were available - wait interruptibly for one to become
284 set_current_state(TASK_INTERRUPTIBLE
);
285 list_add_tail(&callslot
->link
, &server
->fs_callq
);
286 spin_unlock(&server
->fs_lock
);
288 while (!callslot
->ready
&& !signal_pending(current
)) {
290 set_current_state(TASK_INTERRUPTIBLE
);
293 set_current_state(TASK_RUNNING
);
295 /* even if we were interrupted we may still be queued */
296 if (!callslot
->ready
) {
297 spin_lock(&server
->fs_lock
);
298 list_del_init(&callslot
->link
);
299 spin_unlock(&server
->fs_lock
);
302 nconn
= callslot
->nconn
;
304 /* if interrupted, we must release any slot we also got before
305 * returning an error */
306 if (signal_pending(current
)) {
311 /* if we were woken up with an error, then pass that error back to the
314 _leave(" = %d", callslot
->errno
);
315 return callslot
->errno
;
318 /* were we given a connection directly? */
319 if (callslot
->conn
) {
321 _leave(" = 0 (nc=%d)", nconn
);
325 /* got a callslot, but no connection */
328 /* need to get hold of the RxRPC connection */
329 down_write(&server
->sem
);
331 /* quick check to see if there's an outstanding error */
332 ret
= server
->fs_state
;
334 goto error_release_upw
;
336 if (server
->fs_conn
[nconn
]) {
337 /* reuse an existing connection */
338 rxrpc_get_connection(server
->fs_conn
[nconn
]);
339 callslot
->conn
= server
->fs_conn
[nconn
];
342 /* create a new connection */
343 ret
= rxrpc_create_connection(afs_transport
,
348 &server
->fs_conn
[nconn
]);
351 goto error_release_upw
;
353 callslot
->conn
= server
->fs_conn
[0];
354 rxrpc_get_connection(callslot
->conn
);
357 up_write(&server
->sem
);
362 /* handle an error occurring */
364 up_write(&server
->sem
);
367 /* either release the callslot or pass it along to another deserving
369 spin_lock(&server
->fs_lock
);
372 /* no callslot allocated */
374 else if (list_empty(&server
->fs_callq
)) {
376 server
->fs_conn_cnt
[nconn
]++;
377 spin_unlock(&server
->fs_lock
);
380 /* someone's waiting - dequeue them and wake them up */
381 pcallslot
= list_entry(server
->fs_callq
.next
,
382 struct afs_server_callslot
, link
);
383 list_del_init(&pcallslot
->link
);
385 pcallslot
->errno
= server
->fs_state
;
386 if (!pcallslot
->errno
) {
387 /* pass them out callslot details */
388 callslot
->conn
= xchg(&pcallslot
->conn
,
390 pcallslot
->nconn
= nconn
;
391 callslot
->nconn
= nconn
= -1;
393 pcallslot
->ready
= 1;
394 wake_up_process(pcallslot
->task
);
395 spin_unlock(&server
->fs_lock
);
398 rxrpc_put_connection(callslot
->conn
);
399 callslot
->conn
= NULL
;
401 _leave(" = %d", ret
);
404 } /* end afs_server_request_callslot() */
406 /*****************************************************************************/
408 * release a callslot back to the server
409 * - transfers the RxRPC connection to the next pending callslot if possible
411 void afs_server_release_callslot(struct afs_server
*server
,
412 struct afs_server_callslot
*callslot
)
414 struct afs_server_callslot
*pcallslot
;
416 _enter("{ad=%08x,cnt=%u},{%d}",
417 ntohl(server
->addr
.s_addr
),
418 server
->fs_conn_cnt
[callslot
->nconn
],
421 BUG_ON(callslot
->nconn
< 0);
423 spin_lock(&server
->fs_lock
);
425 if (list_empty(&server
->fs_callq
)) {
427 server
->fs_conn_cnt
[callslot
->nconn
]++;
428 spin_unlock(&server
->fs_lock
);
431 /* someone's waiting - dequeue them and wake them up */
432 pcallslot
= list_entry(server
->fs_callq
.next
,
433 struct afs_server_callslot
, link
);
434 list_del_init(&pcallslot
->link
);
436 pcallslot
->errno
= server
->fs_state
;
437 if (!pcallslot
->errno
) {
438 /* pass them out callslot details */
439 callslot
->conn
= xchg(&pcallslot
->conn
, callslot
->conn
);
440 pcallslot
->nconn
= callslot
->nconn
;
441 callslot
->nconn
= -1;
444 pcallslot
->ready
= 1;
445 wake_up_process(pcallslot
->task
);
446 spin_unlock(&server
->fs_lock
);
449 rxrpc_put_connection(callslot
->conn
);
452 } /* end afs_server_release_callslot() */
454 /*****************************************************************************/
456 * get a handle to a connection to the vlserver (volume location) on the
459 int afs_server_get_vlconn(struct afs_server
*server
,
460 struct rxrpc_connection
**_conn
)
462 struct rxrpc_connection
*conn
;
465 _enter("%p,", server
);
469 down_read(&server
->sem
);
471 if (server
->vlserver
) {
472 /* reuse an existing connection */
473 rxrpc_get_connection(server
->vlserver
);
474 conn
= server
->vlserver
;
475 up_read(&server
->sem
);
478 /* create a new connection */
479 up_read(&server
->sem
);
480 down_write(&server
->sem
);
481 if (!server
->vlserver
) {
482 ret
= rxrpc_create_connection(afs_transport
,
490 rxrpc_get_connection(server
->vlserver
);
491 conn
= server
->vlserver
;
493 up_write(&server
->sem
);
497 _leave(" = %d", ret
);
499 } /* end afs_server_get_vlconn() */