Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /* Maintain an RxRPC server socket to do AFS communications through
0003  *
0004  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
0005  * Written by David Howells (dhowells@redhat.com)
0006  */
0007 
0008 #include <linux/slab.h>
0009 #include <linux/sched/signal.h>
0010 
0011 #include <net/sock.h>
0012 #include <net/af_rxrpc.h>
0013 #include "internal.h"
0014 #include "afs_cm.h"
0015 #include "protocol_yfs.h"
0016 
0017 struct workqueue_struct *afs_async_calls;
0018 
0019 static void afs_wake_up_call_waiter(struct sock *, struct rxrpc_call *, unsigned long);
0020 static void afs_wake_up_async_call(struct sock *, struct rxrpc_call *, unsigned long);
0021 static void afs_process_async_call(struct work_struct *);
0022 static void afs_rx_new_call(struct sock *, struct rxrpc_call *, unsigned long);
0023 static void afs_rx_discard_new_call(struct rxrpc_call *, unsigned long);
0024 static int afs_deliver_cm_op_id(struct afs_call *);
0025 
0026 /* asynchronous incoming call initial processing */
0027 static const struct afs_call_type afs_RXCMxxxx = {
0028     .name       = "CB.xxxx",
0029     .deliver    = afs_deliver_cm_op_id,
0030 };
0031 
0032 /*
0033  * open an RxRPC socket and bind it to be a server for callback notifications
0034  * - the socket is left in blocking mode and non-blocking ops use MSG_DONTWAIT
0035  */
0036 int afs_open_socket(struct afs_net *net)
0037 {
0038     struct sockaddr_rxrpc srx;
0039     struct socket *socket;
0040     int ret;
0041 
0042     _enter("");
0043 
0044     ret = sock_create_kern(net->net, AF_RXRPC, SOCK_DGRAM, PF_INET6, &socket);
0045     if (ret < 0)
0046         goto error_1;
0047 
0048     socket->sk->sk_allocation = GFP_NOFS;
0049 
0050     /* bind the callback manager's address to make this a server socket */
0051     memset(&srx, 0, sizeof(srx));
0052     srx.srx_family          = AF_RXRPC;
0053     srx.srx_service         = CM_SERVICE;
0054     srx.transport_type      = SOCK_DGRAM;
0055     srx.transport_len       = sizeof(srx.transport.sin6);
0056     srx.transport.sin6.sin6_family  = AF_INET6;
0057     srx.transport.sin6.sin6_port    = htons(AFS_CM_PORT);
0058 
0059     ret = rxrpc_sock_set_min_security_level(socket->sk,
0060                         RXRPC_SECURITY_ENCRYPT);
0061     if (ret < 0)
0062         goto error_2;
0063 
0064     ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
0065     if (ret == -EADDRINUSE) {
0066         srx.transport.sin6.sin6_port = 0;
0067         ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
0068     }
0069     if (ret < 0)
0070         goto error_2;
0071 
0072     srx.srx_service = YFS_CM_SERVICE;
0073     ret = kernel_bind(socket, (struct sockaddr *) &srx, sizeof(srx));
0074     if (ret < 0)
0075         goto error_2;
0076 
0077     /* Ideally, we'd turn on service upgrade here, but we can't because
0078      * OpenAFS is buggy and leaks the userStatus field from packet to
0079      * packet and between FS packets and CB packets - so if we try to do an
0080      * upgrade on an FS packet, OpenAFS will leak that into the CB packet
0081      * it sends back to us.
0082      */
0083 
0084     rxrpc_kernel_new_call_notification(socket, afs_rx_new_call,
0085                        afs_rx_discard_new_call);
0086 
0087     ret = kernel_listen(socket, INT_MAX);
0088     if (ret < 0)
0089         goto error_2;
0090 
0091     net->socket = socket;
0092     afs_charge_preallocation(&net->charge_preallocation_work);
0093     _leave(" = 0");
0094     return 0;
0095 
0096 error_2:
0097     sock_release(socket);
0098 error_1:
0099     _leave(" = %d", ret);
0100     return ret;
0101 }
0102 
0103 /*
0104  * close the RxRPC socket AFS was using
0105  */
0106 void afs_close_socket(struct afs_net *net)
0107 {
0108     _enter("");
0109 
0110     kernel_listen(net->socket, 0);
0111     flush_workqueue(afs_async_calls);
0112 
0113     if (net->spare_incoming_call) {
0114         afs_put_call(net->spare_incoming_call);
0115         net->spare_incoming_call = NULL;
0116     }
0117 
0118     _debug("outstanding %u", atomic_read(&net->nr_outstanding_calls));
0119     wait_var_event(&net->nr_outstanding_calls,
0120                !atomic_read(&net->nr_outstanding_calls));
0121     _debug("no outstanding calls");
0122 
0123     kernel_sock_shutdown(net->socket, SHUT_RDWR);
0124     flush_workqueue(afs_async_calls);
0125     sock_release(net->socket);
0126 
0127     _debug("dework");
0128     _leave("");
0129 }
0130 
0131 /*
0132  * Allocate a call.
0133  */
0134 static struct afs_call *afs_alloc_call(struct afs_net *net,
0135                        const struct afs_call_type *type,
0136                        gfp_t gfp)
0137 {
0138     struct afs_call *call;
0139     int o;
0140 
0141     call = kzalloc(sizeof(*call), gfp);
0142     if (!call)
0143         return NULL;
0144 
0145     call->type = type;
0146     call->net = net;
0147     call->debug_id = atomic_inc_return(&rxrpc_debug_id);
0148     refcount_set(&call->ref, 1);
0149     INIT_WORK(&call->async_work, afs_process_async_call);
0150     init_waitqueue_head(&call->waitq);
0151     spin_lock_init(&call->state_lock);
0152     call->iter = &call->def_iter;
0153 
0154     o = atomic_inc_return(&net->nr_outstanding_calls);
0155     trace_afs_call(call->debug_id, afs_call_trace_alloc, 1, o,
0156                __builtin_return_address(0));
0157     return call;
0158 }
0159 
0160 /*
0161  * Dispose of a reference on a call.
0162  */
0163 void afs_put_call(struct afs_call *call)
0164 {
0165     struct afs_net *net = call->net;
0166     unsigned int debug_id = call->debug_id;
0167     bool zero;
0168     int r, o;
0169 
0170     zero = __refcount_dec_and_test(&call->ref, &r);
0171     o = atomic_read(&net->nr_outstanding_calls);
0172     trace_afs_call(debug_id, afs_call_trace_put, r - 1, o,
0173                __builtin_return_address(0));
0174 
0175     if (zero) {
0176         ASSERT(!work_pending(&call->async_work));
0177         ASSERT(call->type->name != NULL);
0178 
0179         if (call->rxcall) {
0180             rxrpc_kernel_end_call(net->socket, call->rxcall);
0181             call->rxcall = NULL;
0182         }
0183         if (call->type->destructor)
0184             call->type->destructor(call);
0185 
0186         afs_unuse_server_notime(call->net, call->server, afs_server_trace_put_call);
0187         afs_put_addrlist(call->alist);
0188         kfree(call->request);
0189 
0190         trace_afs_call(call->debug_id, afs_call_trace_free, 0, o,
0191                    __builtin_return_address(0));
0192         kfree(call);
0193 
0194         o = atomic_dec_return(&net->nr_outstanding_calls);
0195         if (o == 0)
0196             wake_up_var(&net->nr_outstanding_calls);
0197     }
0198 }
0199 
0200 static struct afs_call *afs_get_call(struct afs_call *call,
0201                      enum afs_call_trace why)
0202 {
0203     int r;
0204 
0205     __refcount_inc(&call->ref, &r);
0206 
0207     trace_afs_call(call->debug_id, why, r + 1,
0208                atomic_read(&call->net->nr_outstanding_calls),
0209                __builtin_return_address(0));
0210     return call;
0211 }
0212 
0213 /*
0214  * Queue the call for actual work.
0215  */
0216 static void afs_queue_call_work(struct afs_call *call)
0217 {
0218     if (call->type->work) {
0219         INIT_WORK(&call->work, call->type->work);
0220 
0221         afs_get_call(call, afs_call_trace_work);
0222         if (!queue_work(afs_wq, &call->work))
0223             afs_put_call(call);
0224     }
0225 }
0226 
0227 /*
0228  * allocate a call with flat request and reply buffers
0229  */
0230 struct afs_call *afs_alloc_flat_call(struct afs_net *net,
0231                      const struct afs_call_type *type,
0232                      size_t request_size, size_t reply_max)
0233 {
0234     struct afs_call *call;
0235 
0236     call = afs_alloc_call(net, type, GFP_NOFS);
0237     if (!call)
0238         goto nomem_call;
0239 
0240     if (request_size) {
0241         call->request_size = request_size;
0242         call->request = kmalloc(request_size, GFP_NOFS);
0243         if (!call->request)
0244             goto nomem_free;
0245     }
0246 
0247     if (reply_max) {
0248         call->reply_max = reply_max;
0249         call->buffer = kmalloc(reply_max, GFP_NOFS);
0250         if (!call->buffer)
0251             goto nomem_free;
0252     }
0253 
0254     afs_extract_to_buf(call, call->reply_max);
0255     call->operation_ID = type->op;
0256     init_waitqueue_head(&call->waitq);
0257     return call;
0258 
0259 nomem_free:
0260     afs_put_call(call);
0261 nomem_call:
0262     return NULL;
0263 }
0264 
0265 /*
0266  * clean up a call with flat buffer
0267  */
0268 void afs_flat_call_destructor(struct afs_call *call)
0269 {
0270     _enter("");
0271 
0272     kfree(call->request);
0273     call->request = NULL;
0274     kfree(call->buffer);
0275     call->buffer = NULL;
0276 }
0277 
0278 /*
0279  * Advance the AFS call state when the RxRPC call ends the transmit phase.
0280  */
0281 static void afs_notify_end_request_tx(struct sock *sock,
0282                       struct rxrpc_call *rxcall,
0283                       unsigned long call_user_ID)
0284 {
0285     struct afs_call *call = (struct afs_call *)call_user_ID;
0286 
0287     afs_set_call_state(call, AFS_CALL_CL_REQUESTING, AFS_CALL_CL_AWAIT_REPLY);
0288 }
0289 
0290 /*
0291  * Initiate a call and synchronously queue up the parameters for dispatch.  Any
0292  * error is stored into the call struct, which the caller must check for.
0293  */
0294 void afs_make_call(struct afs_addr_cursor *ac, struct afs_call *call, gfp_t gfp)
0295 {
0296     struct sockaddr_rxrpc *srx = &ac->alist->addrs[ac->index];
0297     struct rxrpc_call *rxcall;
0298     struct msghdr msg;
0299     struct kvec iov[1];
0300     size_t len;
0301     s64 tx_total_len;
0302     int ret;
0303 
0304     _enter(",{%pISp},", &srx->transport);
0305 
0306     ASSERT(call->type != NULL);
0307     ASSERT(call->type->name != NULL);
0308 
0309     _debug("____MAKE %p{%s,%x} [%d]____",
0310            call, call->type->name, key_serial(call->key),
0311            atomic_read(&call->net->nr_outstanding_calls));
0312 
0313     call->addr_ix = ac->index;
0314     call->alist = afs_get_addrlist(ac->alist);
0315 
0316     /* Work out the length we're going to transmit.  This is awkward for
0317      * calls such as FS.StoreData where there's an extra injection of data
0318      * after the initial fixed part.
0319      */
0320     tx_total_len = call->request_size;
0321     if (call->write_iter)
0322         tx_total_len += iov_iter_count(call->write_iter);
0323 
0324     /* If the call is going to be asynchronous, we need an extra ref for
0325      * the call to hold itself so the caller need not hang on to its ref.
0326      */
0327     if (call->async) {
0328         afs_get_call(call, afs_call_trace_get);
0329         call->drop_ref = true;
0330     }
0331 
0332     /* create a call */
0333     rxcall = rxrpc_kernel_begin_call(call->net->socket, srx, call->key,
0334                      (unsigned long)call,
0335                      tx_total_len, gfp,
0336                      (call->async ?
0337                       afs_wake_up_async_call :
0338                       afs_wake_up_call_waiter),
0339                      call->upgrade,
0340                      (call->intr ? RXRPC_PREINTERRUPTIBLE :
0341                       RXRPC_UNINTERRUPTIBLE),
0342                      call->debug_id);
0343     if (IS_ERR(rxcall)) {
0344         ret = PTR_ERR(rxcall);
0345         call->error = ret;
0346         goto error_kill_call;
0347     }
0348 
0349     call->rxcall = rxcall;
0350 
0351     if (call->max_lifespan)
0352         rxrpc_kernel_set_max_life(call->net->socket, rxcall,
0353                       call->max_lifespan);
0354     call->issue_time = ktime_get_real();
0355 
0356     /* send the request */
0357     iov[0].iov_base = call->request;
0358     iov[0].iov_len  = call->request_size;
0359 
0360     msg.msg_name        = NULL;
0361     msg.msg_namelen     = 0;
0362     iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, call->request_size);
0363     msg.msg_control     = NULL;
0364     msg.msg_controllen  = 0;
0365     msg.msg_flags       = MSG_WAITALL | (call->write_iter ? MSG_MORE : 0);
0366 
0367     ret = rxrpc_kernel_send_data(call->net->socket, rxcall,
0368                      &msg, call->request_size,
0369                      afs_notify_end_request_tx);
0370     if (ret < 0)
0371         goto error_do_abort;
0372 
0373     if (call->write_iter) {
0374         msg.msg_iter = *call->write_iter;
0375         msg.msg_flags &= ~MSG_MORE;
0376         trace_afs_send_data(call, &msg);
0377 
0378         ret = rxrpc_kernel_send_data(call->net->socket,
0379                          call->rxcall, &msg,
0380                          iov_iter_count(&msg.msg_iter),
0381                          afs_notify_end_request_tx);
0382         *call->write_iter = msg.msg_iter;
0383 
0384         trace_afs_sent_data(call, &msg, ret);
0385         if (ret < 0)
0386             goto error_do_abort;
0387     }
0388 
0389     /* Note that at this point, we may have received the reply or an abort
0390      * - and an asynchronous call may already have completed.
0391      *
0392      * afs_wait_for_call_to_complete(call, ac)
0393      * must be called to synchronously clean up.
0394      */
0395     return;
0396 
0397 error_do_abort:
0398     if (ret != -ECONNABORTED) {
0399         rxrpc_kernel_abort_call(call->net->socket, rxcall,
0400                     RX_USER_ABORT, ret, "KSD");
0401     } else {
0402         len = 0;
0403         iov_iter_kvec(&msg.msg_iter, READ, NULL, 0, 0);
0404         rxrpc_kernel_recv_data(call->net->socket, rxcall,
0405                        &msg.msg_iter, &len, false,
0406                        &call->abort_code, &call->service_id);
0407         ac->abort_code = call->abort_code;
0408         ac->responded = true;
0409     }
0410     call->error = ret;
0411     trace_afs_call_done(call);
0412 error_kill_call:
0413     if (call->type->done)
0414         call->type->done(call);
0415 
0416     /* We need to dispose of the extra ref we grabbed for an async call.
0417      * The call, however, might be queued on afs_async_calls and we need to
0418      * make sure we don't get any more notifications that might requeue it.
0419      */
0420     if (call->rxcall) {
0421         rxrpc_kernel_end_call(call->net->socket, call->rxcall);
0422         call->rxcall = NULL;
0423     }
0424     if (call->async) {
0425         if (cancel_work_sync(&call->async_work))
0426             afs_put_call(call);
0427         afs_put_call(call);
0428     }
0429 
0430     ac->error = ret;
0431     call->state = AFS_CALL_COMPLETE;
0432     _leave(" = %d", ret);
0433 }
0434 
0435 /*
0436  * Log remote abort codes that indicate that we have a protocol disagreement
0437  * with the server.
0438  */
0439 static void afs_log_error(struct afs_call *call, s32 remote_abort)
0440 {
0441     static int max = 0;
0442     const char *msg;
0443     int m;
0444 
0445     switch (remote_abort) {
0446     case RX_EOF:         msg = "unexpected EOF";    break;
0447     case RXGEN_CC_MARSHAL:   msg = "client marshalling";    break;
0448     case RXGEN_CC_UNMARSHAL: msg = "client unmarshalling";  break;
0449     case RXGEN_SS_MARSHAL:   msg = "server marshalling";    break;
0450     case RXGEN_SS_UNMARSHAL: msg = "server unmarshalling";  break;
0451     case RXGEN_DECODE:   msg = "opcode decode";     break;
0452     case RXGEN_SS_XDRFREE:   msg = "server XDR cleanup";    break;
0453     case RXGEN_CC_XDRFREE:   msg = "client XDR cleanup";    break;
0454     case -32:        msg = "insufficient data"; break;
0455     default:
0456         return;
0457     }
0458 
0459     m = max;
0460     if (m < 3) {
0461         max = m + 1;
0462         pr_notice("kAFS: Peer reported %s failure on %s [%pISp]\n",
0463               msg, call->type->name,
0464               &call->alist->addrs[call->addr_ix].transport);
0465     }
0466 }
0467 
0468 /*
0469  * deliver messages to a call
0470  */
0471 static void afs_deliver_to_call(struct afs_call *call)
0472 {
0473     enum afs_call_state state;
0474     size_t len;
0475     u32 abort_code, remote_abort = 0;
0476     int ret;
0477 
0478     _enter("%s", call->type->name);
0479 
0480     while (state = READ_ONCE(call->state),
0481            state == AFS_CALL_CL_AWAIT_REPLY ||
0482            state == AFS_CALL_SV_AWAIT_OP_ID ||
0483            state == AFS_CALL_SV_AWAIT_REQUEST ||
0484            state == AFS_CALL_SV_AWAIT_ACK
0485            ) {
0486         if (state == AFS_CALL_SV_AWAIT_ACK) {
0487             len = 0;
0488             iov_iter_kvec(&call->def_iter, READ, NULL, 0, 0);
0489             ret = rxrpc_kernel_recv_data(call->net->socket,
0490                              call->rxcall, &call->def_iter,
0491                              &len, false, &remote_abort,
0492                              &call->service_id);
0493             trace_afs_receive_data(call, &call->def_iter, false, ret);
0494 
0495             if (ret == -EINPROGRESS || ret == -EAGAIN)
0496                 return;
0497             if (ret < 0 || ret == 1) {
0498                 if (ret == 1)
0499                     ret = 0;
0500                 goto call_complete;
0501             }
0502             return;
0503         }
0504 
0505         ret = call->type->deliver(call);
0506         state = READ_ONCE(call->state);
0507         if (ret == 0 && call->unmarshalling_error)
0508             ret = -EBADMSG;
0509         switch (ret) {
0510         case 0:
0511             afs_queue_call_work(call);
0512             if (state == AFS_CALL_CL_PROC_REPLY) {
0513                 if (call->op)
0514                     set_bit(AFS_SERVER_FL_MAY_HAVE_CB,
0515                         &call->op->server->flags);
0516                 goto call_complete;
0517             }
0518             ASSERTCMP(state, >, AFS_CALL_CL_PROC_REPLY);
0519             goto done;
0520         case -EINPROGRESS:
0521         case -EAGAIN:
0522             goto out;
0523         case -ECONNABORTED:
0524             ASSERTCMP(state, ==, AFS_CALL_COMPLETE);
0525             afs_log_error(call, call->abort_code);
0526             goto done;
0527         case -ENOTSUPP:
0528             abort_code = RXGEN_OPCODE;
0529             rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
0530                         abort_code, ret, "KIV");
0531             goto local_abort;
0532         case -EIO:
0533             pr_err("kAFS: Call %u in bad state %u\n",
0534                    call->debug_id, state);
0535             fallthrough;
0536         case -ENODATA:
0537         case -EBADMSG:
0538         case -EMSGSIZE:
0539         case -ENOMEM:
0540         case -EFAULT:
0541             abort_code = RXGEN_CC_UNMARSHAL;
0542             if (state != AFS_CALL_CL_AWAIT_REPLY)
0543                 abort_code = RXGEN_SS_UNMARSHAL;
0544             rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
0545                         abort_code, ret, "KUM");
0546             goto local_abort;
0547         default:
0548             abort_code = RX_CALL_DEAD;
0549             rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
0550                         abort_code, ret, "KER");
0551             goto local_abort;
0552         }
0553     }
0554 
0555 done:
0556     if (call->type->done)
0557         call->type->done(call);
0558 out:
0559     _leave("");
0560     return;
0561 
0562 local_abort:
0563     abort_code = 0;
0564 call_complete:
0565     afs_set_call_complete(call, ret, remote_abort);
0566     state = AFS_CALL_COMPLETE;
0567     goto done;
0568 }
0569 
0570 /*
0571  * Wait synchronously for a call to complete and clean up the call struct.
0572  */
0573 long afs_wait_for_call_to_complete(struct afs_call *call,
0574                    struct afs_addr_cursor *ac)
0575 {
0576     long ret;
0577     bool rxrpc_complete = false;
0578 
0579     DECLARE_WAITQUEUE(myself, current);
0580 
0581     _enter("");
0582 
0583     ret = call->error;
0584     if (ret < 0)
0585         goto out;
0586 
0587     add_wait_queue(&call->waitq, &myself);
0588     for (;;) {
0589         set_current_state(TASK_UNINTERRUPTIBLE);
0590 
0591         /* deliver any messages that are in the queue */
0592         if (!afs_check_call_state(call, AFS_CALL_COMPLETE) &&
0593             call->need_attention) {
0594             call->need_attention = false;
0595             __set_current_state(TASK_RUNNING);
0596             afs_deliver_to_call(call);
0597             continue;
0598         }
0599 
0600         if (afs_check_call_state(call, AFS_CALL_COMPLETE))
0601             break;
0602 
0603         if (!rxrpc_kernel_check_life(call->net->socket, call->rxcall)) {
0604             /* rxrpc terminated the call. */
0605             rxrpc_complete = true;
0606             break;
0607         }
0608 
0609         schedule();
0610     }
0611 
0612     remove_wait_queue(&call->waitq, &myself);
0613     __set_current_state(TASK_RUNNING);
0614 
0615     if (!afs_check_call_state(call, AFS_CALL_COMPLETE)) {
0616         if (rxrpc_complete) {
0617             afs_set_call_complete(call, call->error, call->abort_code);
0618         } else {
0619             /* Kill off the call if it's still live. */
0620             _debug("call interrupted");
0621             if (rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
0622                             RX_USER_ABORT, -EINTR, "KWI"))
0623                 afs_set_call_complete(call, -EINTR, 0);
0624         }
0625     }
0626 
0627     spin_lock_bh(&call->state_lock);
0628     ac->abort_code = call->abort_code;
0629     ac->error = call->error;
0630     spin_unlock_bh(&call->state_lock);
0631 
0632     ret = ac->error;
0633     switch (ret) {
0634     case 0:
0635         ret = call->ret0;
0636         call->ret0 = 0;
0637 
0638         fallthrough;
0639     case -ECONNABORTED:
0640         ac->responded = true;
0641         break;
0642     }
0643 
0644 out:
0645     _debug("call complete");
0646     afs_put_call(call);
0647     _leave(" = %p", (void *)ret);
0648     return ret;
0649 }
0650 
0651 /*
0652  * wake up a waiting call
0653  */
0654 static void afs_wake_up_call_waiter(struct sock *sk, struct rxrpc_call *rxcall,
0655                     unsigned long call_user_ID)
0656 {
0657     struct afs_call *call = (struct afs_call *)call_user_ID;
0658 
0659     call->need_attention = true;
0660     wake_up(&call->waitq);
0661 }
0662 
0663 /*
0664  * wake up an asynchronous call
0665  */
0666 static void afs_wake_up_async_call(struct sock *sk, struct rxrpc_call *rxcall,
0667                    unsigned long call_user_ID)
0668 {
0669     struct afs_call *call = (struct afs_call *)call_user_ID;
0670     int r;
0671 
0672     trace_afs_notify_call(rxcall, call);
0673     call->need_attention = true;
0674 
0675     if (__refcount_inc_not_zero(&call->ref, &r)) {
0676         trace_afs_call(call->debug_id, afs_call_trace_wake, r + 1,
0677                    atomic_read(&call->net->nr_outstanding_calls),
0678                    __builtin_return_address(0));
0679 
0680         if (!queue_work(afs_async_calls, &call->async_work))
0681             afs_put_call(call);
0682     }
0683 }
0684 
0685 /*
0686  * Perform I/O processing on an asynchronous call.  The work item carries a ref
0687  * to the call struct that we either need to release or to pass on.
0688  */
0689 static void afs_process_async_call(struct work_struct *work)
0690 {
0691     struct afs_call *call = container_of(work, struct afs_call, async_work);
0692 
0693     _enter("");
0694 
0695     if (call->state < AFS_CALL_COMPLETE && call->need_attention) {
0696         call->need_attention = false;
0697         afs_deliver_to_call(call);
0698     }
0699 
0700     afs_put_call(call);
0701     _leave("");
0702 }
0703 
0704 static void afs_rx_attach(struct rxrpc_call *rxcall, unsigned long user_call_ID)
0705 {
0706     struct afs_call *call = (struct afs_call *)user_call_ID;
0707 
0708     call->rxcall = rxcall;
0709 }
0710 
0711 /*
0712  * Charge the incoming call preallocation.
0713  */
0714 void afs_charge_preallocation(struct work_struct *work)
0715 {
0716     struct afs_net *net =
0717         container_of(work, struct afs_net, charge_preallocation_work);
0718     struct afs_call *call = net->spare_incoming_call;
0719 
0720     for (;;) {
0721         if (!call) {
0722             call = afs_alloc_call(net, &afs_RXCMxxxx, GFP_KERNEL);
0723             if (!call)
0724                 break;
0725 
0726             call->drop_ref = true;
0727             call->async = true;
0728             call->state = AFS_CALL_SV_AWAIT_OP_ID;
0729             init_waitqueue_head(&call->waitq);
0730             afs_extract_to_tmp(call);
0731         }
0732 
0733         if (rxrpc_kernel_charge_accept(net->socket,
0734                            afs_wake_up_async_call,
0735                            afs_rx_attach,
0736                            (unsigned long)call,
0737                            GFP_KERNEL,
0738                            call->debug_id) < 0)
0739             break;
0740         call = NULL;
0741     }
0742     net->spare_incoming_call = call;
0743 }
0744 
0745 /*
0746  * Discard a preallocated call when a socket is shut down.
0747  */
0748 static void afs_rx_discard_new_call(struct rxrpc_call *rxcall,
0749                     unsigned long user_call_ID)
0750 {
0751     struct afs_call *call = (struct afs_call *)user_call_ID;
0752 
0753     call->rxcall = NULL;
0754     afs_put_call(call);
0755 }
0756 
0757 /*
0758  * Notification of an incoming call.
0759  */
0760 static void afs_rx_new_call(struct sock *sk, struct rxrpc_call *rxcall,
0761                 unsigned long user_call_ID)
0762 {
0763     struct afs_net *net = afs_sock2net(sk);
0764 
0765     queue_work(afs_wq, &net->charge_preallocation_work);
0766 }
0767 
0768 /*
0769  * Grab the operation ID from an incoming cache manager call.  The socket
0770  * buffer is discarded on error or if we don't yet have sufficient data.
0771  */
0772 static int afs_deliver_cm_op_id(struct afs_call *call)
0773 {
0774     int ret;
0775 
0776     _enter("{%zu}", iov_iter_count(call->iter));
0777 
0778     /* the operation ID forms the first four bytes of the request data */
0779     ret = afs_extract_data(call, true);
0780     if (ret < 0)
0781         return ret;
0782 
0783     call->operation_ID = ntohl(call->tmp);
0784     afs_set_call_state(call, AFS_CALL_SV_AWAIT_OP_ID, AFS_CALL_SV_AWAIT_REQUEST);
0785 
0786     /* ask the cache manager to route the call (it'll change the call type
0787      * if successful) */
0788     if (!afs_cm_incoming_call(call))
0789         return -ENOTSUPP;
0790 
0791     trace_afs_cb_call(call);
0792 
0793     /* pass responsibility for the remainer of this message off to the
0794      * cache manager op */
0795     return call->type->deliver(call);
0796 }
0797 
0798 /*
0799  * Advance the AFS call state when an RxRPC service call ends the transmit
0800  * phase.
0801  */
0802 static void afs_notify_end_reply_tx(struct sock *sock,
0803                     struct rxrpc_call *rxcall,
0804                     unsigned long call_user_ID)
0805 {
0806     struct afs_call *call = (struct afs_call *)call_user_ID;
0807 
0808     afs_set_call_state(call, AFS_CALL_SV_REPLYING, AFS_CALL_SV_AWAIT_ACK);
0809 }
0810 
0811 /*
0812  * send an empty reply
0813  */
0814 void afs_send_empty_reply(struct afs_call *call)
0815 {
0816     struct afs_net *net = call->net;
0817     struct msghdr msg;
0818 
0819     _enter("");
0820 
0821     rxrpc_kernel_set_tx_length(net->socket, call->rxcall, 0);
0822 
0823     msg.msg_name        = NULL;
0824     msg.msg_namelen     = 0;
0825     iov_iter_kvec(&msg.msg_iter, WRITE, NULL, 0, 0);
0826     msg.msg_control     = NULL;
0827     msg.msg_controllen  = 0;
0828     msg.msg_flags       = 0;
0829 
0830     switch (rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, 0,
0831                        afs_notify_end_reply_tx)) {
0832     case 0:
0833         _leave(" [replied]");
0834         return;
0835 
0836     case -ENOMEM:
0837         _debug("oom");
0838         rxrpc_kernel_abort_call(net->socket, call->rxcall,
0839                     RXGEN_SS_MARSHAL, -ENOMEM, "KOO");
0840         fallthrough;
0841     default:
0842         _leave(" [error]");
0843         return;
0844     }
0845 }
0846 
0847 /*
0848  * send a simple reply
0849  */
0850 void afs_send_simple_reply(struct afs_call *call, const void *buf, size_t len)
0851 {
0852     struct afs_net *net = call->net;
0853     struct msghdr msg;
0854     struct kvec iov[1];
0855     int n;
0856 
0857     _enter("");
0858 
0859     rxrpc_kernel_set_tx_length(net->socket, call->rxcall, len);
0860 
0861     iov[0].iov_base     = (void *) buf;
0862     iov[0].iov_len      = len;
0863     msg.msg_name        = NULL;
0864     msg.msg_namelen     = 0;
0865     iov_iter_kvec(&msg.msg_iter, WRITE, iov, 1, len);
0866     msg.msg_control     = NULL;
0867     msg.msg_controllen  = 0;
0868     msg.msg_flags       = 0;
0869 
0870     n = rxrpc_kernel_send_data(net->socket, call->rxcall, &msg, len,
0871                    afs_notify_end_reply_tx);
0872     if (n >= 0) {
0873         /* Success */
0874         _leave(" [replied]");
0875         return;
0876     }
0877 
0878     if (n == -ENOMEM) {
0879         _debug("oom");
0880         rxrpc_kernel_abort_call(net->socket, call->rxcall,
0881                     RXGEN_SS_MARSHAL, -ENOMEM, "KOO");
0882     }
0883     _leave(" [error]");
0884 }
0885 
0886 /*
0887  * Extract a piece of data from the received data socket buffers.
0888  */
0889 int afs_extract_data(struct afs_call *call, bool want_more)
0890 {
0891     struct afs_net *net = call->net;
0892     struct iov_iter *iter = call->iter;
0893     enum afs_call_state state;
0894     u32 remote_abort = 0;
0895     int ret;
0896 
0897     _enter("{%s,%zu,%zu},%d",
0898            call->type->name, call->iov_len, iov_iter_count(iter), want_more);
0899 
0900     ret = rxrpc_kernel_recv_data(net->socket, call->rxcall, iter,
0901                      &call->iov_len, want_more, &remote_abort,
0902                      &call->service_id);
0903     if (ret == 0 || ret == -EAGAIN)
0904         return ret;
0905 
0906     state = READ_ONCE(call->state);
0907     if (ret == 1) {
0908         switch (state) {
0909         case AFS_CALL_CL_AWAIT_REPLY:
0910             afs_set_call_state(call, state, AFS_CALL_CL_PROC_REPLY);
0911             break;
0912         case AFS_CALL_SV_AWAIT_REQUEST:
0913             afs_set_call_state(call, state, AFS_CALL_SV_REPLYING);
0914             break;
0915         case AFS_CALL_COMPLETE:
0916             kdebug("prem complete %d", call->error);
0917             return afs_io_error(call, afs_io_error_extract);
0918         default:
0919             break;
0920         }
0921         return 0;
0922     }
0923 
0924     afs_set_call_complete(call, ret, remote_abort);
0925     return ret;
0926 }
0927 
0928 /*
0929  * Log protocol error production.
0930  */
0931 noinline int afs_protocol_error(struct afs_call *call,
0932                 enum afs_eproto_cause cause)
0933 {
0934     trace_afs_protocol_error(call, cause);
0935     if (call)
0936         call->unmarshalling_error = true;
0937     return -EBADMSG;
0938 }