ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / fs / afs / cmservice.c
1 /* cmservice.c: AFS Cache Manager Service
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
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/sched.h>
15 #include <linux/completion.h>
16 #include "server.h"
17 #include "cell.h"
18 #include "transport.h"
19 #include <rxrpc/rxrpc.h>
20 #include <rxrpc/transport.h>
21 #include <rxrpc/connection.h>
22 #include <rxrpc/call.h>
23 #include "cmservice.h"
24 #include "internal.h"
25
26 static unsigned afscm_usage;            /* AFS cache manager usage count */
27 static struct rw_semaphore afscm_sem;   /* AFS cache manager start/stop semaphore */
28
29 static int afscm_new_call(struct rxrpc_call *call);
30 static void afscm_attention(struct rxrpc_call *call);
31 static void afscm_error(struct rxrpc_call *call);
32 static void afscm_aemap(struct rxrpc_call *call);
33
34 static void _SRXAFSCM_CallBack(struct rxrpc_call *call);
35 static void _SRXAFSCM_InitCallBackState(struct rxrpc_call *call);
36 static void _SRXAFSCM_Probe(struct rxrpc_call *call);
37
38 typedef void (*_SRXAFSCM_xxxx_t)(struct rxrpc_call *call);
39
40 static const struct rxrpc_operation AFSCM_ops[] = {
41         {
42                 .id     = 204,
43                 .asize  = RXRPC_APP_MARK_EOF,
44                 .name   = "CallBack",
45                 .user   = _SRXAFSCM_CallBack,
46         },
47         {
48                 .id     = 205,
49                 .asize  = RXRPC_APP_MARK_EOF,
50                 .name   = "InitCallBackState",
51                 .user   = _SRXAFSCM_InitCallBackState,
52         },
53         {
54                 .id     = 206,
55                 .asize  = RXRPC_APP_MARK_EOF,
56                 .name   = "Probe",
57                 .user   = _SRXAFSCM_Probe,
58         },
59 #if 0
60         {
61                 .id     = 207,
62                 .asize  = RXRPC_APP_MARK_EOF,
63                 .name   = "GetLock",
64                 .user   = _SRXAFSCM_GetLock,
65         },
66         {
67                 .id     = 208,
68                 .asize  = RXRPC_APP_MARK_EOF,
69                 .name   = "GetCE",
70                 .user   = _SRXAFSCM_GetCE,
71         },
72         {
73                 .id     = 209,
74                 .asize  = RXRPC_APP_MARK_EOF,
75                 .name   = "GetXStatsVersion",
76                 .user   = _SRXAFSCM_GetXStatsVersion,
77         },
78         {
79                 .id     = 210,
80                 .asize  = RXRPC_APP_MARK_EOF,
81                 .name   = "GetXStats",
82                 .user   = _SRXAFSCM_GetXStats,
83         }
84 #endif
85 };
86
87 static struct rxrpc_service AFSCM_service = {
88         .name           = "AFS/CM",
89         .owner          = THIS_MODULE,
90         .link           = LIST_HEAD_INIT(AFSCM_service.link),
91         .new_call       = afscm_new_call,
92         .service_id     = 1,
93         .attn_func      = afscm_attention,
94         .error_func     = afscm_error,
95         .aemap_func     = afscm_aemap,
96         .ops_begin      = &AFSCM_ops[0],
97         .ops_end        = &AFSCM_ops[sizeof(AFSCM_ops) / sizeof(AFSCM_ops[0])],
98 };
99
100 static DECLARE_COMPLETION(kafscmd_alive);
101 static DECLARE_COMPLETION(kafscmd_dead);
102 static DECLARE_WAIT_QUEUE_HEAD(kafscmd_sleepq);
103 static LIST_HEAD(kafscmd_attention_list);
104 static LIST_HEAD(afscm_calls);
105 static spinlock_t afscm_calls_lock = SPIN_LOCK_UNLOCKED;
106 static spinlock_t kafscmd_attention_lock = SPIN_LOCK_UNLOCKED;
107 static int kafscmd_die;
108
109 /*****************************************************************************/
110 /*
111  * AFS Cache Manager kernel thread
112  */
113 static int kafscmd(void *arg)
114 {
115         DECLARE_WAITQUEUE(myself, current);
116
117         struct rxrpc_call *call;
118         _SRXAFSCM_xxxx_t func;
119         int die;
120
121         printk("kAFS: Started kafscmd %d\n", current->pid);
122
123         daemonize("kafscmd");
124
125         complete(&kafscmd_alive);
126
127         /* loop around looking for things to attend to */
128         do {
129                 if (list_empty(&kafscmd_attention_list)) {
130                         set_current_state(TASK_INTERRUPTIBLE);
131                         add_wait_queue(&kafscmd_sleepq, &myself);
132
133                         for (;;) {
134                                 set_current_state(TASK_INTERRUPTIBLE);
135                                 if (!list_empty(&kafscmd_attention_list) ||
136                                     signal_pending(current) ||
137                                     kafscmd_die)
138                                         break;
139
140                                 schedule();
141                         }
142
143                         remove_wait_queue(&kafscmd_sleepq, &myself);
144                         set_current_state(TASK_RUNNING);
145                 }
146
147                 die = kafscmd_die;
148
149                 /* dequeue the next call requiring attention */
150                 call = NULL;
151                 spin_lock(&kafscmd_attention_lock);
152
153                 if (!list_empty(&kafscmd_attention_list)) {
154                         call = list_entry(kafscmd_attention_list.next,
155                                           struct rxrpc_call,
156                                           app_attn_link);
157                         list_del_init(&call->app_attn_link);
158                         die = 0;
159                 }
160
161                 spin_unlock(&kafscmd_attention_lock);
162
163                 if (call) {
164                         /* act upon it */
165                         _debug("@@@ Begin Attend Call %p", call);
166
167                         func = call->app_user;
168                         if (func)
169                                 func(call);
170
171                         rxrpc_put_call(call);
172
173                         _debug("@@@ End Attend Call %p", call);
174                 }
175
176         } while(!die);
177
178         /* and that's all */
179         complete_and_exit(&kafscmd_dead, 0);
180
181 } /* end kafscmd() */
182
183 /*****************************************************************************/
184 /*
185  * handle a call coming in to the cache manager
186  * - if I want to keep the call, I must increment its usage count
187  * - the return value will be negated and passed back in an abort packet if
188  *   non-zero
189  * - serialised by virtue of there only being one krxiod
190  */
191 static int afscm_new_call(struct rxrpc_call *call)
192 {
193         _enter("%p{cid=%u u=%d}",
194                call, ntohl(call->call_id), atomic_read(&call->usage));
195
196         rxrpc_get_call(call);
197
198         /* add to my current call list */
199         spin_lock(&afscm_calls_lock);
200         list_add(&call->app_link,&afscm_calls);
201         spin_unlock(&afscm_calls_lock);
202
203         _leave(" = 0");
204         return 0;
205
206 } /* end afscm_new_call() */
207
208 /*****************************************************************************/
209 /*
210  * queue on the kafscmd queue for attention
211  */
212 static void afscm_attention(struct rxrpc_call *call)
213 {
214         _enter("%p{cid=%u u=%d}",
215                call, ntohl(call->call_id), atomic_read(&call->usage));
216
217         spin_lock(&kafscmd_attention_lock);
218
219         if (list_empty(&call->app_attn_link)) {
220                 list_add_tail(&call->app_attn_link, &kafscmd_attention_list);
221                 rxrpc_get_call(call);
222         }
223
224         spin_unlock(&kafscmd_attention_lock);
225
226         wake_up(&kafscmd_sleepq);
227
228         _leave(" {u=%d}", atomic_read(&call->usage));
229 } /* end afscm_attention() */
230
231 /*****************************************************************************/
232 /*
233  * handle my call being aborted
234  * - clean up, dequeue and put my ref to the call
235  */
236 static void afscm_error(struct rxrpc_call *call)
237 {
238         int removed;
239
240         _enter("%p{est=%s ac=%u er=%d}",
241                call,
242                rxrpc_call_error_states[call->app_err_state],
243                call->app_abort_code,
244                call->app_errno);
245
246         spin_lock(&kafscmd_attention_lock);
247
248         if (list_empty(&call->app_attn_link)) {
249                 list_add_tail(&call->app_attn_link, &kafscmd_attention_list);
250                 rxrpc_get_call(call);
251         }
252
253         spin_unlock(&kafscmd_attention_lock);
254
255         removed = 0;
256         spin_lock(&afscm_calls_lock);
257         if (!list_empty(&call->app_link)) {
258                 list_del_init(&call->app_link);
259                 removed = 1;
260         }
261         spin_unlock(&afscm_calls_lock);
262
263         if (removed)
264                 rxrpc_put_call(call);
265
266         wake_up(&kafscmd_sleepq);
267
268         _leave("");
269 } /* end afscm_error() */
270
271 /*****************************************************************************/
272 /*
273  * map afs abort codes to/from Linux error codes
274  * - called with call->lock held
275  */
276 static void afscm_aemap(struct rxrpc_call *call)
277 {
278         switch (call->app_err_state) {
279         case RXRPC_ESTATE_LOCAL_ABORT:
280                 call->app_abort_code = -call->app_errno;
281                 break;
282         case RXRPC_ESTATE_PEER_ABORT:
283                 call->app_errno = -ECONNABORTED;
284                 break;
285         default:
286                 break;
287         }
288 } /* end afscm_aemap() */
289
290 /*****************************************************************************/
291 /*
292  * start the cache manager service if not already started
293  */
294 int afscm_start(void)
295 {
296         int ret;
297
298         down_write(&afscm_sem);
299         if (!afscm_usage) {
300                 ret = kernel_thread(kafscmd, NULL, 0);
301                 if (ret < 0)
302                         goto out;
303
304                 wait_for_completion(&kafscmd_alive);
305
306                 ret = rxrpc_add_service(afs_transport, &AFSCM_service);
307                 if (ret < 0)
308                         goto kill;
309
310 #ifdef AFS_AUTOMOUNT_SUPPORT
311                 afs_kafstimod_add_timer(&afs_mntpt_expiry_timer,
312                                         afs_mntpt_expiry_timeout * HZ);
313 #endif
314         }
315
316         afscm_usage++;
317         up_write(&afscm_sem);
318
319         return 0;
320
321  kill:
322         kafscmd_die = 1;
323         wake_up(&kafscmd_sleepq);
324         wait_for_completion(&kafscmd_dead);
325
326  out:
327         up_write(&afscm_sem);
328         return ret;
329
330 } /* end afscm_start() */
331
332 /*****************************************************************************/
333 /*
334  * stop the cache manager service
335  */
336 void afscm_stop(void)
337 {
338         struct rxrpc_call *call;
339
340         down_write(&afscm_sem);
341
342         BUG_ON(afscm_usage == 0);
343         afscm_usage--;
344
345         if (afscm_usage == 0) {
346                 /* don't want more incoming calls */
347                 rxrpc_del_service(afs_transport, &AFSCM_service);
348
349                 /* abort any calls I've still got open (the afscm_error() will
350                  * dequeue them) */
351                 spin_lock(&afscm_calls_lock);
352                 while (!list_empty(&afscm_calls)) {
353                         call = list_entry(afscm_calls.next,
354                                           struct rxrpc_call,
355                                           app_link);
356
357                         list_del_init(&call->app_link);
358                         rxrpc_get_call(call);
359                         spin_unlock(&afscm_calls_lock);
360
361                         rxrpc_call_abort(call, -ESRCH); /* abort, dequeue and
362                                                          * put */
363
364                         _debug("nuking active call %08x.%d",
365                                ntohl(call->conn->conn_id),
366                                ntohl(call->call_id));
367                         rxrpc_put_call(call);
368                         rxrpc_put_call(call);
369
370                         spin_lock(&afscm_calls_lock);
371                 }
372                 spin_unlock(&afscm_calls_lock);
373
374                 /* get rid of my daemon */
375                 kafscmd_die = 1;
376                 wake_up(&kafscmd_sleepq);
377                 wait_for_completion(&kafscmd_dead);
378
379                 /* dispose of any calls waiting for attention */
380                 spin_lock(&kafscmd_attention_lock);
381                 while (!list_empty(&kafscmd_attention_list)) {
382                         call = list_entry(kafscmd_attention_list.next,
383                                           struct rxrpc_call,
384                                           app_attn_link);
385
386                         list_del_init(&call->app_attn_link);
387                         spin_unlock(&kafscmd_attention_lock);
388
389                         rxrpc_put_call(call);
390
391                         spin_lock(&kafscmd_attention_lock);
392                 }
393                 spin_unlock(&kafscmd_attention_lock);
394
395 #ifdef AFS_AUTOMOUNT_SUPPORT
396                 afs_kafstimod_del_timer(&afs_mntpt_expiry_timer);
397 #endif
398         }
399
400         up_write(&afscm_sem);
401
402 } /* end afscm_stop() */
403
404 /*****************************************************************************/
405 /*
406  * handle the fileserver breaking a set of callbacks
407  */
408 static void _SRXAFSCM_CallBack(struct rxrpc_call *call)
409 {
410         struct afs_server *server;
411         size_t count, qty, tmp;
412         int ret = 0, removed;
413
414         _enter("%p{acs=%s}", call, rxrpc_call_states[call->app_call_state]);
415
416         server = afs_server_get_from_peer(call->conn->peer);
417
418         switch (call->app_call_state) {
419                 /* we've received the last packet
420                  * - drain all the data from the call and send the reply
421                  */
422         case RXRPC_CSTATE_SRVR_GOT_ARGS:
423                 ret = -EBADMSG;
424                 qty = call->app_ready_qty;
425                 if (qty < 8 || qty > 50 * (6 * 4) + 8)
426                         break;
427
428                 {
429                         struct afs_callback *cb, *pcb;
430                         int loop;
431                         u32 *fp, *bp;
432
433                         fp = rxrpc_call_alloc_scratch(call, qty);
434
435                         /* drag the entire argument block out to the scratch
436                          * space */
437                         ret = rxrpc_call_read_data(call, fp, qty, 0);
438                         if (ret < 0)
439                                 break;
440
441                         /* and unmarshall the parameter block */
442                         ret = -EBADMSG;
443                         count = ntohl(*fp++);
444                         if (count>AFSCBMAX ||
445                             (count * (3 * 4) + 8 != qty &&
446                              count * (6 * 4) + 8 != qty))
447                                 break;
448
449                         bp = fp + count*3;
450                         tmp = ntohl(*bp++);
451                         if (tmp > 0 && tmp != count)
452                                 break;
453                         if (tmp == 0)
454                                 bp = NULL;
455
456                         pcb = cb = rxrpc_call_alloc_scratch_s(
457                                 call, struct afs_callback);
458
459                         for (loop = count - 1; loop >= 0; loop--) {
460                                 pcb->fid.vid    = ntohl(*fp++);
461                                 pcb->fid.vnode  = ntohl(*fp++);
462                                 pcb->fid.unique = ntohl(*fp++);
463                                 if (bp) {
464                                         pcb->version    = ntohl(*bp++);
465                                         pcb->expiry     = ntohl(*bp++);
466                                         pcb->type       = ntohl(*bp++);
467                                 }
468                                 else {
469                                         pcb->version    = 0;
470                                         pcb->expiry     = 0;
471                                         pcb->type       = AFSCM_CB_UNTYPED;
472                                 }
473                                 pcb++;
474                         }
475
476                         /* invoke the actual service routine */
477                         ret = SRXAFSCM_CallBack(server, count, cb);
478                         if (ret < 0)
479                                 break;
480                 }
481
482                 /* send the reply */
483                 ret = rxrpc_call_write_data(call, 0, NULL, RXRPC_LAST_PACKET,
484                                             GFP_KERNEL, 0, &count);
485                 if (ret < 0)
486                         break;
487                 break;
488
489                 /* operation complete */
490         case RXRPC_CSTATE_COMPLETE:
491                 call->app_user = NULL;
492                 removed = 0;
493                 spin_lock(&afscm_calls_lock);
494                 if (!list_empty(&call->app_link)) {
495                         list_del_init(&call->app_link);
496                         removed = 1;
497                 }
498                 spin_unlock(&afscm_calls_lock);
499
500                 if (removed)
501                         rxrpc_put_call(call);
502                 break;
503
504                 /* operation terminated on error */
505         case RXRPC_CSTATE_ERROR:
506                 call->app_user = NULL;
507                 break;
508
509         default:
510                 break;
511         }
512
513         if (ret < 0)
514                 rxrpc_call_abort(call, ret);
515
516         afs_put_server(server);
517
518         _leave(" = %d", ret);
519
520 } /* end _SRXAFSCM_CallBack() */
521
522 /*****************************************************************************/
523 /*
524  * handle the fileserver asking us to initialise our callback state
525  */
526 static void _SRXAFSCM_InitCallBackState(struct rxrpc_call *call)
527 {
528         struct afs_server *server;
529         size_t count;
530         int ret = 0, removed;
531
532         _enter("%p{acs=%s}", call, rxrpc_call_states[call->app_call_state]);
533
534         server = afs_server_get_from_peer(call->conn->peer);
535
536         switch (call->app_call_state) {
537                 /* we've received the last packet - drain all the data from the
538                  * call */
539         case RXRPC_CSTATE_SRVR_GOT_ARGS:
540                 /* shouldn't be any args */
541                 ret = -EBADMSG;
542                 break;
543
544                 /* send the reply when asked for it */
545         case RXRPC_CSTATE_SRVR_SND_REPLY:
546                 /* invoke the actual service routine */
547                 ret = SRXAFSCM_InitCallBackState(server);
548                 if (ret < 0)
549                         break;
550
551                 ret = rxrpc_call_write_data(call, 0, NULL, RXRPC_LAST_PACKET,
552                                             GFP_KERNEL, 0, &count);
553                 if (ret < 0)
554                         break;
555                 break;
556
557                 /* operation complete */
558         case RXRPC_CSTATE_COMPLETE:
559                 call->app_user = NULL;
560                 removed = 0;
561                 spin_lock(&afscm_calls_lock);
562                 if (!list_empty(&call->app_link)) {
563                         list_del_init(&call->app_link);
564                         removed = 1;
565                 }
566                 spin_unlock(&afscm_calls_lock);
567
568                 if (removed)
569                         rxrpc_put_call(call);
570                 break;
571
572                 /* operation terminated on error */
573         case RXRPC_CSTATE_ERROR:
574                 call->app_user = NULL;
575                 break;
576
577         default:
578                 break;
579         }
580
581         if (ret < 0)
582                 rxrpc_call_abort(call, ret);
583
584         afs_put_server(server);
585
586         _leave(" = %d", ret);
587
588 } /* end _SRXAFSCM_InitCallBackState() */
589
590 /*****************************************************************************/
591 /*
592  * handle a probe from a fileserver
593  */
594 static void _SRXAFSCM_Probe(struct rxrpc_call *call)
595 {
596         struct afs_server *server;
597         size_t count;
598         int ret = 0, removed;
599
600         _enter("%p{acs=%s}", call, rxrpc_call_states[call->app_call_state]);
601
602         server = afs_server_get_from_peer(call->conn->peer);
603
604         switch (call->app_call_state) {
605                 /* we've received the last packet - drain all the data from the
606                  * call */
607         case RXRPC_CSTATE_SRVR_GOT_ARGS:
608                 /* shouldn't be any args */
609                 ret = -EBADMSG;
610                 break;
611
612                 /* send the reply when asked for it */
613         case RXRPC_CSTATE_SRVR_SND_REPLY:
614                 /* invoke the actual service routine */
615                 ret = SRXAFSCM_Probe(server);
616                 if (ret < 0)
617                         break;
618
619                 ret = rxrpc_call_write_data(call, 0, NULL, RXRPC_LAST_PACKET,
620                                             GFP_KERNEL, 0, &count);
621                 if (ret < 0)
622                         break;
623                 break;
624
625                 /* operation complete */
626         case RXRPC_CSTATE_COMPLETE:
627                 call->app_user = NULL;
628                 removed = 0;
629                 spin_lock(&afscm_calls_lock);
630                 if (!list_empty(&call->app_link)) {
631                         list_del_init(&call->app_link);
632                         removed = 1;
633                 }
634                 spin_unlock(&afscm_calls_lock);
635
636                 if (removed)
637                         rxrpc_put_call(call);
638                 break;
639
640                 /* operation terminated on error */
641         case RXRPC_CSTATE_ERROR:
642                 call->app_user = NULL;
643                 break;
644
645         default:
646                 break;
647         }
648
649         if (ret < 0)
650                 rxrpc_call_abort(call, ret);
651
652         afs_put_server(server);
653
654         _leave(" = %d", ret);
655
656 } /* end _SRXAFSCM_Probe() */