]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - fs/afs/rxrpc.c
[AF_RXRPC]: Make the in-kernel AFS filesystem use AF_RXRPC.
[linux-2.6-omap-h63xx.git] / fs / afs / rxrpc.c
1 /* Maintain an RxRPC server socket to do AFS communications through
2  *
3  * Copyright (C) 2007 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 <net/sock.h>
13 #include <net/af_rxrpc.h>
14 #include <rxrpc/packet.h>
15 #include "internal.h"
16 #include "afs_cm.h"
17
18 static struct socket *afs_socket; /* my RxRPC socket */
19 static struct workqueue_struct *afs_async_calls;
20
21 static void afs_wake_up_call_waiter(struct afs_call *);
22 static int afs_wait_for_call_to_complete(struct afs_call *);
23 static void afs_wake_up_async_call(struct afs_call *);
24 static int afs_dont_wait_for_call_to_complete(struct afs_call *);
25 static void afs_process_async_call(struct work_struct *);
26 static void afs_rx_interceptor(struct sock *, unsigned long, struct sk_buff *);
27 static int afs_deliver_cm_op_id(struct afs_call *, struct sk_buff *, bool);
28
29 /* synchronous call management */
30 const struct afs_wait_mode afs_sync_call = {
31         .rx_wakeup      = afs_wake_up_call_waiter,
32         .wait           = afs_wait_for_call_to_complete,
33 };
34
35 /* asynchronous call management */
36 const struct afs_wait_mode afs_async_call = {
37         .rx_wakeup      = afs_wake_up_async_call,
38         .wait           = afs_dont_wait_for_call_to_complete,
39 };
40
41 /* asynchronous incoming call management */
42 static const struct afs_wait_mode afs_async_incoming_call = {
43         .rx_wakeup      = afs_wake_up_async_call,
44 };
45
46 /* asynchronous incoming call initial processing */
47 static const struct afs_call_type afs_RXCMxxxx = {
48         .deliver        = afs_deliver_cm_op_id,
49         .abort_to_error = afs_abort_to_error,
50 };
51
52 static void afs_collect_incoming_call(struct work_struct *);
53
54 static struct sk_buff_head afs_incoming_calls;
55 static DECLARE_WORK(afs_collect_incoming_call_work, afs_collect_incoming_call);
56
57 /*
58  * open an RxRPC socket and bind it to be a server for callback notifications
59  * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT
60  */
61 int afs_open_socket(void)
62 {
63         struct sockaddr_rxrpc srx;
64         struct socket *socket;
65         int ret;
66
67         _enter("");
68
69         skb_queue_head_init(&afs_incoming_calls);
70
71         afs_async_calls = create_singlethread_workqueue("kafsd");
72         if (!afs_async_calls) {
73                 _leave(" = -ENOMEM [wq]");
74                 return -ENOMEM;
75         }
76
77         ret = sock_create_kern(AF_RXRPC, SOCK_DGRAM, PF_INET, &socket);
78         if (ret < 0) {
79                 destroy_workqueue(afs_async_calls);
80                 _leave(" = %d [socket]", ret);
81                 return ret;
82         }
83
84         socket->sk->sk_allocation = GFP_NOFS;
85
86         /* bind the callback manager's address to make this a server socket */
87         srx.srx_family                  = AF_RXRPC;
88         srx.srx_service                 = CM_SERVICE;
89         srx.transport_type              = SOCK_DGRAM;
90         srx.transport_len               = sizeof(srx.transport.sin);
91         srx.transport.sin.sin_family    = AF_INET;
92         srx.transport.sin.sin_port      = htons(AFS_CM_PORT);
93         memset(&srx.transport.sin.sin_addr, 0,
94                sizeof(srx.transport.sin.sin_addr));
95
96         ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
97         if (ret < 0) {
98                 sock_release(socket);
99                 _leave(" = %d [bind]", ret);
100                 return ret;
101         }
102
103         rxrpc_kernel_intercept_rx_messages(socket, afs_rx_interceptor);
104
105         afs_socket = socket;
106         _leave(" = 0");
107         return 0;
108 }
109
110 /*
111  * close the RxRPC socket AFS was using
112  */
113 void afs_close_socket(void)
114 {
115         _enter("");
116
117         sock_release(afs_socket);
118
119         _debug("dework");
120         destroy_workqueue(afs_async_calls);
121         _leave("");
122 }
123
124 /*
125  * allocate a call with flat request and reply buffers
126  */
127 struct afs_call *afs_alloc_flat_call(const struct afs_call_type *type,
128                                      size_t request_size, size_t reply_size)
129 {
130         struct afs_call *call;
131
132         call = kzalloc(sizeof(*call), GFP_NOFS);
133         if (!call)
134                 goto nomem_call;
135
136         if (request_size) {
137                 call->request = kmalloc(request_size, GFP_NOFS);
138                 if (!call->request)
139                         goto nomem_request;
140         }
141
142         if (reply_size) {
143                 call->buffer = kmalloc(reply_size, GFP_NOFS);
144                 if (!call->buffer)
145                         goto nomem_buffer;
146         }
147
148         call->type = type;
149         call->request_size = request_size;
150         call->reply_max = reply_size;
151
152         init_waitqueue_head(&call->waitq);
153         skb_queue_head_init(&call->rx_queue);
154         return call;
155
156 nomem_buffer:
157         kfree(call->request);
158 nomem_request:
159         kfree(call);
160 nomem_call:
161         return NULL;
162 }
163
164 /*
165  * clean up a call with flat buffer
166  */
167 void afs_flat_call_destructor(struct afs_call *call)
168 {
169         _enter("");
170
171         kfree(call->request);
172         call->request = NULL;
173         kfree(call->buffer);
174         call->buffer = NULL;
175 }
176
177 /*
178  * initiate a call
179  */
180 int afs_make_call(struct in_addr *addr, struct afs_call *call, gfp_t gfp,
181                   const struct afs_wait_mode *wait_mode)
182 {
183         struct sockaddr_rxrpc srx;
184         struct rxrpc_call *rxcall;
185         struct msghdr msg;
186         struct kvec iov[1];
187         int ret;
188
189         _enter("%x,{%d},", addr->s_addr, ntohs(call->port));
190
191         call->wait_mode = wait_mode;
192         INIT_WORK(&call->async_work, afs_process_async_call);
193
194         memset(&srx, 0, sizeof(srx));
195         srx.srx_family = AF_RXRPC;
196         srx.srx_service = call->service_id;
197         srx.transport_type = SOCK_DGRAM;
198         srx.transport_len = sizeof(srx.transport.sin);
199         srx.transport.sin.sin_family = AF_INET;
200         srx.transport.sin.sin_port = call->port;
201         memcpy(&srx.transport.sin.sin_addr, addr, 4);
202
203         /* create a call */
204         rxcall = rxrpc_kernel_begin_call(afs_socket, &srx, call->key,
205                                          (unsigned long) call, gfp);
206         if (IS_ERR(rxcall)) {
207                 ret = PTR_ERR(rxcall);
208                 goto error_kill_call;
209         }
210
211         call->rxcall = rxcall;
212
213         /* send the request */
214         iov[0].iov_base = call->request;
215         iov[0].iov_len  = call->request_size;
216
217         msg.msg_name            = NULL;
218         msg.msg_namelen         = 0;
219         msg.msg_iov             = (struct iovec *) iov;
220         msg.msg_iovlen          = 1;
221         msg.msg_control         = NULL;
222         msg.msg_controllen      = 0;
223         msg.msg_flags           = 0;
224
225         /* have to change the state *before* sending the last packet as RxRPC
226          * might give us the reply before it returns from sending the
227          * request */
228         call->state = AFS_CALL_AWAIT_REPLY;
229         ret = rxrpc_kernel_send_data(rxcall, &msg, call->request_size);
230         if (ret < 0)
231                 goto error_do_abort;
232
233         /* at this point, an async call may no longer exist as it may have
234          * already completed */
235         return wait_mode->wait(call);
236
237 error_do_abort:
238         rxrpc_kernel_abort_call(rxcall, RX_USER_ABORT);
239         rxrpc_kernel_end_call(rxcall);
240 error_kill_call:
241         call->type->destructor(call);
242         ASSERT(skb_queue_empty(&call->rx_queue));
243         kfree(call);
244         _leave(" = %d", ret);
245         return ret;
246 }
247
248 /*
249  * handles intercepted messages that were arriving in the socket's Rx queue
250  * - called with the socket receive queue lock held to ensure message ordering
251  * - called with softirqs disabled
252  */
253 static void afs_rx_interceptor(struct sock *sk, unsigned long user_call_ID,
254                                struct sk_buff *skb)
255 {
256         struct afs_call *call = (struct afs_call *) user_call_ID;
257
258         _enter("%p,,%u", call, skb->mark);
259
260         ASSERTCMP(sk, ==, afs_socket->sk);
261
262         if (!call) {
263                 /* its an incoming call for our callback service */
264                 __skb_queue_tail(&afs_incoming_calls, skb);
265                 schedule_work(&afs_collect_incoming_call_work);
266         } else {
267                 /* route the messages directly to the appropriate call */
268                 __skb_queue_tail(&call->rx_queue, skb);
269                 call->wait_mode->rx_wakeup(call);
270         }
271
272         _leave("");
273 }
274
275 /*
276  * deliver messages to a call
277  */
278 static void afs_deliver_to_call(struct afs_call *call)
279 {
280         struct sk_buff *skb;
281         bool last;
282         u32 abort_code;
283         int ret;
284
285         _enter("");
286
287         while ((call->state == AFS_CALL_AWAIT_REPLY ||
288                 call->state == AFS_CALL_AWAIT_OP_ID ||
289                 call->state == AFS_CALL_AWAIT_REQUEST ||
290                 call->state == AFS_CALL_AWAIT_ACK) &&
291                (skb = skb_dequeue(&call->rx_queue))) {
292                 switch (skb->mark) {
293                 case RXRPC_SKB_MARK_DATA:
294                         _debug("Rcv DATA");
295                         last = rxrpc_kernel_is_data_last(skb);
296                         ret = call->type->deliver(call, skb, last);
297                         switch (ret) {
298                         case 0:
299                                 if (last &&
300                                     call->state == AFS_CALL_AWAIT_REPLY)
301                                         call->state = AFS_CALL_COMPLETE;
302                                 break;
303                         case -ENOTCONN:
304                                 abort_code = RX_CALL_DEAD;
305                                 goto do_abort;
306                         case -ENOTSUPP:
307                                 abort_code = RX_INVALID_OPERATION;
308                                 goto do_abort;
309                         default:
310                                 abort_code = RXGEN_CC_UNMARSHAL;
311                                 if (call->state != AFS_CALL_AWAIT_REPLY)
312                                         abort_code = RXGEN_SS_UNMARSHAL;
313                         do_abort:
314                                 rxrpc_kernel_abort_call(call->rxcall,
315                                                         abort_code);
316                                 call->error = ret;
317                                 call->state = AFS_CALL_ERROR;
318                                 break;
319                         }
320                         rxrpc_kernel_data_delivered(skb);
321                         skb = NULL;
322                         break;
323                 case RXRPC_SKB_MARK_FINAL_ACK:
324                         _debug("Rcv ACK");
325                         call->state = AFS_CALL_COMPLETE;
326                         break;
327                 case RXRPC_SKB_MARK_BUSY:
328                         _debug("Rcv BUSY");
329                         call->error = -EBUSY;
330                         call->state = AFS_CALL_BUSY;
331                         break;
332                 case RXRPC_SKB_MARK_REMOTE_ABORT:
333                         abort_code = rxrpc_kernel_get_abort_code(skb);
334                         call->error = call->type->abort_to_error(abort_code);
335                         call->state = AFS_CALL_ABORTED;
336                         _debug("Rcv ABORT %u -> %d", abort_code, call->error);
337                         break;
338                 case RXRPC_SKB_MARK_NET_ERROR:
339                         call->error = -rxrpc_kernel_get_error_number(skb);
340                         call->state = AFS_CALL_ERROR;
341                         _debug("Rcv NET ERROR %d", call->error);
342                         break;
343                 case RXRPC_SKB_MARK_LOCAL_ERROR:
344                         call->error = -rxrpc_kernel_get_error_number(skb);
345                         call->state = AFS_CALL_ERROR;
346                         _debug("Rcv LOCAL ERROR %d", call->error);
347                         break;
348                 default:
349                         BUG();
350                         break;
351                 }
352
353                 rxrpc_kernel_free_skb(skb);
354         }
355
356         /* make sure the queue is empty if the call is done with (we might have
357          * aborted the call early because of an unmarshalling error) */
358         if (call->state >= AFS_CALL_COMPLETE) {
359                 while ((skb = skb_dequeue(&call->rx_queue)))
360                         rxrpc_kernel_free_skb(skb);
361                 if (call->incoming) {
362                         rxrpc_kernel_end_call(call->rxcall);
363                         call->type->destructor(call);
364                         ASSERT(skb_queue_empty(&call->rx_queue));
365                         kfree(call);
366                 }
367         }
368
369         _leave("");
370 }
371
372 /*
373  * wait synchronously for a call to complete
374  */
375 static int afs_wait_for_call_to_complete(struct afs_call *call)
376 {
377         struct sk_buff *skb;
378         int ret;
379
380         DECLARE_WAITQUEUE(myself, current);
381
382         _enter("");
383
384         add_wait_queue(&call->waitq, &myself);
385         for (;;) {
386                 set_current_state(TASK_INTERRUPTIBLE);
387
388                 /* deliver any messages that are in the queue */
389                 if (!skb_queue_empty(&call->rx_queue)) {
390                         __set_current_state(TASK_RUNNING);
391                         afs_deliver_to_call(call);
392                         continue;
393                 }
394
395                 ret = call->error;
396                 if (call->state >= AFS_CALL_COMPLETE)
397                         break;
398                 ret = -EINTR;
399                 if (signal_pending(current))
400                         break;
401                 schedule();
402         }
403
404         remove_wait_queue(&call->waitq, &myself);
405         __set_current_state(TASK_RUNNING);
406
407         /* kill the call */
408         if (call->state < AFS_CALL_COMPLETE) {
409                 _debug("call incomplete");
410                 rxrpc_kernel_abort_call(call->rxcall, RX_CALL_DEAD);
411                 while ((skb = skb_dequeue(&call->rx_queue)))
412                         rxrpc_kernel_free_skb(skb);
413         }
414
415         _debug("call complete");
416         rxrpc_kernel_end_call(call->rxcall);
417         call->type->destructor(call);
418         ASSERT(skb_queue_empty(&call->rx_queue));
419         kfree(call);
420         _leave(" = %d", ret);
421         return ret;
422 }
423
424 /*
425  * wake up a waiting call
426  */
427 static void afs_wake_up_call_waiter(struct afs_call *call)
428 {
429         wake_up(&call->waitq);
430 }
431
432 /*
433  * wake up an asynchronous call
434  */
435 static void afs_wake_up_async_call(struct afs_call *call)
436 {
437         _enter("");
438         queue_work(afs_async_calls, &call->async_work);
439 }
440
441 /*
442  * put a call into asynchronous mode
443  * - mustn't touch the call descriptor as the call my have completed by the
444  *   time we get here
445  */
446 static int afs_dont_wait_for_call_to_complete(struct afs_call *call)
447 {
448         _enter("");
449         return -EINPROGRESS;
450 }
451
452 /*
453  * delete an asynchronous call
454  */
455 static void afs_delete_async_call(struct work_struct *work)
456 {
457         struct afs_call *call =
458                 container_of(work, struct afs_call, async_work);
459
460         _enter("");
461
462         ASSERT(skb_queue_empty(&call->rx_queue));
463         ASSERT(!work_pending(&call->async_work));
464         kfree(call);
465
466         _leave("");
467 }
468
469 /*
470  * perform processing on an asynchronous call
471  * - on a multiple-thread workqueue this work item may try to run on several
472  *   CPUs at the same time
473  */
474 static void afs_process_async_call(struct work_struct *work)
475 {
476         struct afs_call *call =
477                 container_of(work, struct afs_call, async_work);
478
479         _enter("");
480
481         if (!skb_queue_empty(&call->rx_queue))
482                 afs_deliver_to_call(call);
483
484         if (call->state >= AFS_CALL_COMPLETE && call->wait_mode) {
485                 if (call->wait_mode->async_complete)
486                         call->wait_mode->async_complete(call->reply,
487                                                         call->error);
488                 call->reply = NULL;
489
490                 /* kill the call */
491                 rxrpc_kernel_end_call(call->rxcall);
492                 if (call->type->destructor)
493                         call->type->destructor(call);
494
495                 /* we can't just delete the call because the work item may be
496                  * queued */
497                 PREPARE_WORK(&call->async_work, afs_delete_async_call);
498                 queue_work(afs_async_calls, &call->async_work);
499         }
500
501         _leave("");
502 }
503
504 /*
505  * empty a socket buffer into a flat reply buffer
506  */
507 void afs_transfer_reply(struct afs_call *call, struct sk_buff *skb)
508 {
509         size_t len = skb->len;
510
511         if (skb_copy_bits(skb, 0, call->buffer + call->reply_size, len) < 0)
512                 BUG();
513         call->reply_size += len;
514 }
515
516 /*
517  * accept the backlog of incoming calls
518  */
519 static void afs_collect_incoming_call(struct work_struct *work)
520 {
521         struct rxrpc_call *rxcall;
522         struct afs_call *call = NULL;
523         struct sk_buff *skb;
524
525         while ((skb = skb_dequeue(&afs_incoming_calls))) {
526                 _debug("new call");
527
528                 /* don't need the notification */
529                 rxrpc_kernel_free_skb(skb);
530
531                 if (!call) {
532                         call = kzalloc(sizeof(struct afs_call), GFP_KERNEL);
533                         if (!call) {
534                                 rxrpc_kernel_reject_call(afs_socket);
535                                 return;
536                         }
537
538                         INIT_WORK(&call->async_work, afs_process_async_call);
539                         call->wait_mode = &afs_async_incoming_call;
540                         call->type = &afs_RXCMxxxx;
541                         init_waitqueue_head(&call->waitq);
542                         skb_queue_head_init(&call->rx_queue);
543                         call->state = AFS_CALL_AWAIT_OP_ID;
544                 }
545
546                 rxcall = rxrpc_kernel_accept_call(afs_socket,
547                                                   (unsigned long) call);
548                 if (!IS_ERR(rxcall)) {
549                         call->rxcall = rxcall;
550                         call = NULL;
551                 }
552         }
553
554         kfree(call);
555 }
556
557 /*
558  * grab the operation ID from an incoming cache manager call
559  */
560 static int afs_deliver_cm_op_id(struct afs_call *call, struct sk_buff *skb,
561                                 bool last)
562 {
563         size_t len = skb->len;
564         void *oibuf = (void *) &call->operation_ID;
565
566         _enter("{%u},{%zu},%d", call->offset, len, last);
567
568         ASSERTCMP(call->offset, <, 4);
569
570         /* the operation ID forms the first four bytes of the request data */
571         len = min_t(size_t, len, 4 - call->offset);
572         if (skb_copy_bits(skb, 0, oibuf + call->offset, len) < 0)
573                 BUG();
574         if (!pskb_pull(skb, len))
575                 BUG();
576         call->offset += len;
577
578         if (call->offset < 4) {
579                 if (last) {
580                         _leave(" = -EBADMSG [op ID short]");
581                         return -EBADMSG;
582                 }
583                 _leave(" = 0 [incomplete]");
584                 return 0;
585         }
586
587         call->state = AFS_CALL_AWAIT_REQUEST;
588
589         /* ask the cache manager to route the call (it'll change the call type
590          * if successful) */
591         if (!afs_cm_incoming_call(call))
592                 return -ENOTSUPP;
593
594         /* pass responsibility for the remainer of this message off to the
595          * cache manager op */
596         return call->type->deliver(call, skb, last);
597 }
598
599 /*
600  * send an empty reply
601  */
602 void afs_send_empty_reply(struct afs_call *call)
603 {
604         struct msghdr msg;
605         struct iovec iov[1];
606
607         _enter("");
608
609         iov[0].iov_base         = NULL;
610         iov[0].iov_len          = 0;
611         msg.msg_name            = NULL;
612         msg.msg_namelen         = 0;
613         msg.msg_iov             = iov;
614         msg.msg_iovlen          = 0;
615         msg.msg_control         = NULL;
616         msg.msg_controllen      = 0;
617         msg.msg_flags           = 0;
618
619         call->state = AFS_CALL_AWAIT_ACK;
620         switch (rxrpc_kernel_send_data(call->rxcall, &msg, 0)) {
621         case 0:
622                 _leave(" [replied]");
623                 return;
624
625         case -ENOMEM:
626                 _debug("oom");
627                 rxrpc_kernel_abort_call(call->rxcall, RX_USER_ABORT);
628         default:
629                 rxrpc_kernel_end_call(call->rxcall);
630                 call->rxcall = NULL;
631                 call->type->destructor(call);
632                 ASSERT(skb_queue_empty(&call->rx_queue));
633                 kfree(call);
634                 _leave(" [error]");
635                 return;
636         }
637 }
638
639 /*
640  * extract a piece of data from the received data socket buffers
641  */
642 int afs_extract_data(struct afs_call *call, struct sk_buff *skb,
643                      bool last, void *buf, size_t count)
644 {
645         size_t len = skb->len;
646
647         _enter("{%u},{%zu},%d,,%zu", call->offset, len, last, count);
648
649         ASSERTCMP(call->offset, <, count);
650
651         len = min_t(size_t, len, count - call->offset);
652         if (skb_copy_bits(skb, 0, buf + call->offset, len) < 0 ||
653             !pskb_pull(skb, len))
654                 BUG();
655         call->offset += len;
656
657         if (call->offset < count) {
658                 if (last) {
659                         _leave(" = -EBADMSG [%d < %lu]", call->offset, count);
660                         return -EBADMSG;
661                 }
662                 _leave(" = -EAGAIN");
663                 return -EAGAIN;
664         }
665         return 0;
666 }