1 /* 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
,
45 * lookup a server record in a cell
46 * - TODO: search the cell's server list
48 int afs_server_lookup(struct afs_cell
*cell
, const struct in_addr
*addr
,
49 struct afs_server
**_server
)
51 struct afs_server
*server
, *active
, *zombie
;
54 _enter("%p,%08x,", cell
, ntohl(addr
->s_addr
));
56 /* allocate and initialise a server record */
57 server
= kzalloc(sizeof(struct afs_server
), GFP_KERNEL
);
63 atomic_set(&server
->usage
, 1);
65 INIT_LIST_HEAD(&server
->link
);
66 init_rwsem(&server
->sem
);
67 INIT_LIST_HEAD(&server
->fs_callq
);
68 spin_lock_init(&server
->fs_lock
);
69 INIT_LIST_HEAD(&server
->cb_promises
);
70 spin_lock_init(&server
->cb_lock
);
72 for (loop
= 0; loop
< AFS_SERVER_CONN_LIST_SIZE
; loop
++)
73 server
->fs_conn_cnt
[loop
] = 4;
75 memcpy(&server
->addr
, addr
, sizeof(struct in_addr
));
76 server
->addr
.s_addr
= addr
->s_addr
;
78 afs_timer_init(&server
->timeout
, &afs_server_timer_ops
);
81 write_lock(&cell
->sv_lock
);
83 /* check the active list */
84 list_for_each_entry(active
, &cell
->sv_list
, link
) {
85 if (active
->addr
.s_addr
== addr
->s_addr
)
86 goto use_active_server
;
89 /* check the inactive list */
90 spin_lock(&cell
->sv_gylock
);
91 list_for_each_entry(zombie
, &cell
->sv_graveyard
, link
) {
92 if (zombie
->addr
.s_addr
== addr
->s_addr
)
93 goto resurrect_server
;
95 spin_unlock(&cell
->sv_gylock
);
99 list_add_tail(&server
->link
, &cell
->sv_list
);
101 write_unlock(&cell
->sv_lock
);
104 _leave(" = 0 (%p)", server
);
107 /* found a matching active server */
109 _debug("active server");
110 afs_get_server(active
);
111 write_unlock(&cell
->sv_lock
);
116 _leave(" = 0 (%p)", active
);
119 /* found a matching server in the graveyard, so resurrect it and
120 * dispose of the new record */
122 _debug("resurrecting server");
124 list_move_tail(&zombie
->link
, &cell
->sv_list
);
125 afs_get_server(zombie
);
126 afs_kafstimod_del_timer(&zombie
->timeout
);
127 spin_unlock(&cell
->sv_gylock
);
128 write_unlock(&cell
->sv_lock
);
133 _leave(" = 0 (%p)", zombie
);
138 * destroy a server record
139 * - removes from the cell list
141 void afs_put_server(struct afs_server
*server
)
143 struct afs_cell
*cell
;
148 _enter("%p", server
);
153 BUG_ON(atomic_read(&server
->usage
) <= 0);
155 /* to prevent a race, the decrement and the dequeue must be effectively
157 write_lock(&cell
->sv_lock
);
159 if (likely(!atomic_dec_and_test(&server
->usage
))) {
160 write_unlock(&cell
->sv_lock
);
165 spin_lock(&cell
->sv_gylock
);
166 list_move_tail(&server
->link
, &cell
->sv_graveyard
);
168 /* time out in 10 secs */
169 afs_kafstimod_add_timer(&server
->timeout
, 10 * HZ
);
171 spin_unlock(&cell
->sv_gylock
);
172 write_unlock(&cell
->sv_lock
);
178 * timeout server record
179 * - removes from the cell's graveyard if the usage count is zero
181 void afs_server_do_timeout(struct afs_server
*server
)
183 struct rxrpc_peer
*peer
;
184 struct afs_cell
*cell
;
187 _enter("%p", server
);
191 BUG_ON(atomic_read(&server
->usage
) < 0);
193 /* remove from graveyard if still dead */
194 spin_lock(&cell
->vl_gylock
);
195 if (atomic_read(&server
->usage
) == 0)
196 list_del_init(&server
->link
);
199 spin_unlock(&cell
->vl_gylock
);
203 return; /* resurrected */
206 /* we can now destroy it properly */
209 /* uncross-point the structs under a global lock */
210 spin_lock(&afs_server_peer_lock
);
216 spin_unlock(&afs_server_peer_lock
);
218 /* finish cleaning up the server */
219 for (loop
= AFS_SERVER_CONN_LIST_SIZE
- 1; loop
>= 0; loop
--)
220 if (server
->fs_conn
[loop
])
221 rxrpc_put_connection(server
->fs_conn
[loop
]);
223 if (server
->vlserver
)
224 rxrpc_put_connection(server
->vlserver
);
228 _leave(" [destroyed]");
232 * get a callslot on a connection to the fileserver on the specified server
234 int afs_server_request_callslot(struct afs_server
*server
,
235 struct afs_server_callslot
*callslot
)
237 struct afs_server_callslot
*pcallslot
;
238 struct rxrpc_connection
*conn
;
241 _enter("%p,",server
);
243 INIT_LIST_HEAD(&callslot
->link
);
244 callslot
->task
= current
;
245 callslot
->conn
= NULL
;
246 callslot
->nconn
= -1;
252 /* get hold of a callslot first */
253 spin_lock(&server
->fs_lock
);
255 /* resurrect the server if it's death timeout has expired */
256 if (server
->fs_state
) {
257 if (time_before(jiffies
, server
->fs_dead_jif
)) {
258 ret
= server
->fs_state
;
259 spin_unlock(&server
->fs_lock
);
260 _leave(" = %d [still dead]", ret
);
264 server
->fs_state
= 0;
267 /* try and find a connection that has spare callslots */
268 for (nconn
= 0; nconn
< AFS_SERVER_CONN_LIST_SIZE
; nconn
++) {
269 if (server
->fs_conn_cnt
[nconn
] > 0) {
270 server
->fs_conn_cnt
[nconn
]--;
271 spin_unlock(&server
->fs_lock
);
272 callslot
->nconn
= nconn
;
277 /* none were available - wait interruptibly for one to become
279 set_current_state(TASK_INTERRUPTIBLE
);
280 list_add_tail(&callslot
->link
, &server
->fs_callq
);
281 spin_unlock(&server
->fs_lock
);
283 while (!callslot
->ready
&& !signal_pending(current
)) {
285 set_current_state(TASK_INTERRUPTIBLE
);
288 set_current_state(TASK_RUNNING
);
290 /* even if we were interrupted we may still be queued */
291 if (!callslot
->ready
) {
292 spin_lock(&server
->fs_lock
);
293 list_del_init(&callslot
->link
);
294 spin_unlock(&server
->fs_lock
);
297 nconn
= callslot
->nconn
;
299 /* if interrupted, we must release any slot we also got before
300 * returning an error */
301 if (signal_pending(current
)) {
306 /* if we were woken up with an error, then pass that error back to the
309 _leave(" = %d", callslot
->errno
);
310 return callslot
->errno
;
313 /* were we given a connection directly? */
314 if (callslot
->conn
) {
316 _leave(" = 0 (nc=%d)", nconn
);
320 /* got a callslot, but no connection */
323 /* need to get hold of the RxRPC connection */
324 down_write(&server
->sem
);
326 /* quick check to see if there's an outstanding error */
327 ret
= server
->fs_state
;
329 goto error_release_upw
;
331 if (server
->fs_conn
[nconn
]) {
332 /* reuse an existing connection */
333 rxrpc_get_connection(server
->fs_conn
[nconn
]);
334 callslot
->conn
= server
->fs_conn
[nconn
];
336 /* create a new connection */
337 ret
= rxrpc_create_connection(afs_transport
,
342 &server
->fs_conn
[nconn
]);
345 goto error_release_upw
;
347 callslot
->conn
= server
->fs_conn
[0];
348 rxrpc_get_connection(callslot
->conn
);
351 up_write(&server
->sem
);
356 /* handle an error occurring */
358 up_write(&server
->sem
);
361 /* either release the callslot or pass it along to another deserving
363 spin_lock(&server
->fs_lock
);
366 /* no callslot allocated */
367 } else if (list_empty(&server
->fs_callq
)) {
369 server
->fs_conn_cnt
[nconn
]++;
370 spin_unlock(&server
->fs_lock
);
372 /* someone's waiting - dequeue them and wake them up */
373 pcallslot
= list_entry(server
->fs_callq
.next
,
374 struct afs_server_callslot
, link
);
375 list_del_init(&pcallslot
->link
);
377 pcallslot
->errno
= server
->fs_state
;
378 if (!pcallslot
->errno
) {
379 /* pass them out callslot details */
380 callslot
->conn
= xchg(&pcallslot
->conn
,
382 pcallslot
->nconn
= nconn
;
383 callslot
->nconn
= nconn
= -1;
385 pcallslot
->ready
= 1;
386 wake_up_process(pcallslot
->task
);
387 spin_unlock(&server
->fs_lock
);
390 rxrpc_put_connection(callslot
->conn
);
391 callslot
->conn
= NULL
;
393 _leave(" = %d", ret
);
398 * release a callslot back to the server
399 * - transfers the RxRPC connection to the next pending callslot if possible
401 void afs_server_release_callslot(struct afs_server
*server
,
402 struct afs_server_callslot
*callslot
)
404 struct afs_server_callslot
*pcallslot
;
406 _enter("{ad=%08x,cnt=%u},{%d}",
407 ntohl(server
->addr
.s_addr
),
408 server
->fs_conn_cnt
[callslot
->nconn
],
411 BUG_ON(callslot
->nconn
< 0);
413 spin_lock(&server
->fs_lock
);
415 if (list_empty(&server
->fs_callq
)) {
417 server
->fs_conn_cnt
[callslot
->nconn
]++;
418 spin_unlock(&server
->fs_lock
);
420 /* someone's waiting - dequeue them and wake them up */
421 pcallslot
= list_entry(server
->fs_callq
.next
,
422 struct afs_server_callslot
, link
);
423 list_del_init(&pcallslot
->link
);
425 pcallslot
->errno
= server
->fs_state
;
426 if (!pcallslot
->errno
) {
427 /* pass them out callslot details */
428 callslot
->conn
= xchg(&pcallslot
->conn
, callslot
->conn
);
429 pcallslot
->nconn
= callslot
->nconn
;
430 callslot
->nconn
= -1;
433 pcallslot
->ready
= 1;
434 wake_up_process(pcallslot
->task
);
435 spin_unlock(&server
->fs_lock
);
438 rxrpc_put_connection(callslot
->conn
);
444 * get a handle to a connection to the vlserver (volume location) on the
447 int afs_server_get_vlconn(struct afs_server
*server
,
448 struct rxrpc_connection
**_conn
)
450 struct rxrpc_connection
*conn
;
453 _enter("%p,", server
);
457 down_read(&server
->sem
);
459 if (server
->vlserver
) {
460 /* reuse an existing connection */
461 rxrpc_get_connection(server
->vlserver
);
462 conn
= server
->vlserver
;
463 up_read(&server
->sem
);
465 /* create a new connection */
466 up_read(&server
->sem
);
467 down_write(&server
->sem
);
468 if (!server
->vlserver
) {
469 ret
= rxrpc_create_connection(afs_transport
,
477 rxrpc_get_connection(server
->vlserver
);
478 conn
= server
->vlserver
;
480 up_write(&server
->sem
);
484 _leave(" = %d", ret
);