sched: Fix sched_policy < 0 comparison
[platform/adaptation/renesas_rcar/renesas_kernel.git] / net / rxrpc / ar-recvmsg.c
1 /* RxRPC recvmsg() implementation
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 <linux/net.h>
13 #include <linux/skbuff.h>
14 #include <linux/export.h>
15 #include <net/sock.h>
16 #include <net/af_rxrpc.h>
17 #include "ar-internal.h"
18
19 /*
20  * removal a call's user ID from the socket tree to make the user ID available
21  * again and so that it won't be seen again in association with that call
22  */
23 void rxrpc_remove_user_ID(struct rxrpc_sock *rx, struct rxrpc_call *call)
24 {
25         _debug("RELEASE CALL %d", call->debug_id);
26
27         if (test_bit(RXRPC_CALL_HAS_USERID, &call->flags)) {
28                 write_lock_bh(&rx->call_lock);
29                 rb_erase(&call->sock_node, &call->socket->calls);
30                 clear_bit(RXRPC_CALL_HAS_USERID, &call->flags);
31                 write_unlock_bh(&rx->call_lock);
32         }
33
34         read_lock_bh(&call->state_lock);
35         if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
36             !test_and_set_bit(RXRPC_CALL_RELEASE, &call->events))
37                 rxrpc_queue_call(call);
38         read_unlock_bh(&call->state_lock);
39 }
40
41 /*
42  * receive a message from an RxRPC socket
43  * - we need to be careful about two or more threads calling recvmsg
44  *   simultaneously
45  */
46 int rxrpc_recvmsg(struct kiocb *iocb, struct socket *sock,
47                   struct msghdr *msg, size_t len, int flags)
48 {
49         struct rxrpc_skb_priv *sp;
50         struct rxrpc_call *call = NULL, *continue_call = NULL;
51         struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
52         struct sk_buff *skb;
53         long timeo;
54         int copy, ret, ullen, offset, copied = 0;
55         u32 abort_code;
56
57         DEFINE_WAIT(wait);
58
59         _enter(",,,%zu,%d", len, flags);
60
61         if (flags & (MSG_OOB | MSG_TRUNC))
62                 return -EOPNOTSUPP;
63
64         ullen = msg->msg_flags & MSG_CMSG_COMPAT ? 4 : sizeof(unsigned long);
65
66         timeo = sock_rcvtimeo(&rx->sk, flags & MSG_DONTWAIT);
67         msg->msg_flags |= MSG_MORE;
68
69         lock_sock(&rx->sk);
70
71         for (;;) {
72                 /* return immediately if a client socket has no outstanding
73                  * calls */
74                 if (RB_EMPTY_ROOT(&rx->calls)) {
75                         if (copied)
76                                 goto out;
77                         if (rx->sk.sk_state != RXRPC_SERVER_LISTENING) {
78                                 release_sock(&rx->sk);
79                                 if (continue_call)
80                                         rxrpc_put_call(continue_call);
81                                 return -ENODATA;
82                         }
83                 }
84
85                 /* get the next message on the Rx queue */
86                 skb = skb_peek(&rx->sk.sk_receive_queue);
87                 if (!skb) {
88                         /* nothing remains on the queue */
89                         if (copied &&
90                             (msg->msg_flags & MSG_PEEK || timeo == 0))
91                                 goto out;
92
93                         /* wait for a message to turn up */
94                         release_sock(&rx->sk);
95                         prepare_to_wait_exclusive(sk_sleep(&rx->sk), &wait,
96                                                   TASK_INTERRUPTIBLE);
97                         ret = sock_error(&rx->sk);
98                         if (ret)
99                                 goto wait_error;
100
101                         if (skb_queue_empty(&rx->sk.sk_receive_queue)) {
102                                 if (signal_pending(current))
103                                         goto wait_interrupted;
104                                 timeo = schedule_timeout(timeo);
105                         }
106                         finish_wait(sk_sleep(&rx->sk), &wait);
107                         lock_sock(&rx->sk);
108                         continue;
109                 }
110
111         peek_next_packet:
112                 sp = rxrpc_skb(skb);
113                 call = sp->call;
114                 ASSERT(call != NULL);
115
116                 _debug("next pkt %s", rxrpc_pkts[sp->hdr.type]);
117
118                 /* make sure we wait for the state to be updated in this call */
119                 spin_lock_bh(&call->lock);
120                 spin_unlock_bh(&call->lock);
121
122                 if (test_bit(RXRPC_CALL_RELEASED, &call->flags)) {
123                         _debug("packet from released call");
124                         if (skb_dequeue(&rx->sk.sk_receive_queue) != skb)
125                                 BUG();
126                         rxrpc_free_skb(skb);
127                         continue;
128                 }
129
130                 /* determine whether to continue last data receive */
131                 if (continue_call) {
132                         _debug("maybe cont");
133                         if (call != continue_call ||
134                             skb->mark != RXRPC_SKB_MARK_DATA) {
135                                 release_sock(&rx->sk);
136                                 rxrpc_put_call(continue_call);
137                                 _leave(" = %d [noncont]", copied);
138                                 return copied;
139                         }
140                 }
141
142                 rxrpc_get_call(call);
143
144                 /* copy the peer address and timestamp */
145                 if (!continue_call) {
146                         if (msg->msg_name) {
147                                 size_t len =
148                                         sizeof(call->conn->trans->peer->srx);
149                                 memcpy(msg->msg_name,
150                                        &call->conn->trans->peer->srx, len);
151                                 msg->msg_namelen = len;
152                         }
153                         sock_recv_ts_and_drops(msg, &rx->sk, skb);
154                 }
155
156                 /* receive the message */
157                 if (skb->mark != RXRPC_SKB_MARK_DATA)
158                         goto receive_non_data_message;
159
160                 _debug("recvmsg DATA #%u { %d, %d }",
161                        ntohl(sp->hdr.seq), skb->len, sp->offset);
162
163                 if (!continue_call) {
164                         /* only set the control data once per recvmsg() */
165                         ret = put_cmsg(msg, SOL_RXRPC, RXRPC_USER_CALL_ID,
166                                        ullen, &call->user_call_ID);
167                         if (ret < 0)
168                                 goto copy_error;
169                         ASSERT(test_bit(RXRPC_CALL_HAS_USERID, &call->flags));
170                 }
171
172                 ASSERTCMP(ntohl(sp->hdr.seq), >=, call->rx_data_recv);
173                 ASSERTCMP(ntohl(sp->hdr.seq), <=, call->rx_data_recv + 1);
174                 call->rx_data_recv = ntohl(sp->hdr.seq);
175
176                 ASSERTCMP(ntohl(sp->hdr.seq), >, call->rx_data_eaten);
177
178                 offset = sp->offset;
179                 copy = skb->len - offset;
180                 if (copy > len - copied)
181                         copy = len - copied;
182
183                 if (skb->ip_summed == CHECKSUM_UNNECESSARY ||
184                     skb->ip_summed == CHECKSUM_PARTIAL) {
185                         ret = skb_copy_datagram_iovec(skb, offset,
186                                                       msg->msg_iov, copy);
187                 } else {
188                         ret = skb_copy_and_csum_datagram_iovec(skb, offset,
189                                                                msg->msg_iov);
190                         if (ret == -EINVAL)
191                                 goto csum_copy_error;
192                 }
193
194                 if (ret < 0)
195                         goto copy_error;
196
197                 /* handle piecemeal consumption of data packets */
198                 _debug("copied %d+%d", copy, copied);
199
200                 offset += copy;
201                 copied += copy;
202
203                 if (!(flags & MSG_PEEK))
204                         sp->offset = offset;
205
206                 if (sp->offset < skb->len) {
207                         _debug("buffer full");
208                         ASSERTCMP(copied, ==, len);
209                         break;
210                 }
211
212                 /* we transferred the whole data packet */
213                 if (sp->hdr.flags & RXRPC_LAST_PACKET) {
214                         _debug("last");
215                         if (call->conn->out_clientflag) {
216                                  /* last byte of reply received */
217                                 ret = copied;
218                                 goto terminal_message;
219                         }
220
221                         /* last bit of request received */
222                         if (!(flags & MSG_PEEK)) {
223                                 _debug("eat packet");
224                                 if (skb_dequeue(&rx->sk.sk_receive_queue) !=
225                                     skb)
226                                         BUG();
227                                 rxrpc_free_skb(skb);
228                         }
229                         msg->msg_flags &= ~MSG_MORE;
230                         break;
231                 }
232
233                 /* move on to the next data message */
234                 _debug("next");
235                 if (!continue_call)
236                         continue_call = sp->call;
237                 else
238                         rxrpc_put_call(call);
239                 call = NULL;
240
241                 if (flags & MSG_PEEK) {
242                         _debug("peek next");
243                         skb = skb->next;
244                         if (skb == (struct sk_buff *) &rx->sk.sk_receive_queue)
245                                 break;
246                         goto peek_next_packet;
247                 }
248
249                 _debug("eat packet");
250                 if (skb_dequeue(&rx->sk.sk_receive_queue) != skb)
251                         BUG();
252                 rxrpc_free_skb(skb);
253         }
254
255         /* end of non-terminal data packet reception for the moment */
256         _debug("end rcv data");
257 out:
258         release_sock(&rx->sk);
259         if (call)
260                 rxrpc_put_call(call);
261         if (continue_call)
262                 rxrpc_put_call(continue_call);
263         _leave(" = %d [data]", copied);
264         return copied;
265
266         /* handle non-DATA messages such as aborts, incoming connections and
267          * final ACKs */
268 receive_non_data_message:
269         _debug("non-data");
270
271         if (skb->mark == RXRPC_SKB_MARK_NEW_CALL) {
272                 _debug("RECV NEW CALL");
273                 ret = put_cmsg(msg, SOL_RXRPC, RXRPC_NEW_CALL, 0, &abort_code);
274                 if (ret < 0)
275                         goto copy_error;
276                 if (!(flags & MSG_PEEK)) {
277                         if (skb_dequeue(&rx->sk.sk_receive_queue) != skb)
278                                 BUG();
279                         rxrpc_free_skb(skb);
280                 }
281                 goto out;
282         }
283
284         ret = put_cmsg(msg, SOL_RXRPC, RXRPC_USER_CALL_ID,
285                        ullen, &call->user_call_ID);
286         if (ret < 0)
287                 goto copy_error;
288         ASSERT(test_bit(RXRPC_CALL_HAS_USERID, &call->flags));
289
290         switch (skb->mark) {
291         case RXRPC_SKB_MARK_DATA:
292                 BUG();
293         case RXRPC_SKB_MARK_FINAL_ACK:
294                 ret = put_cmsg(msg, SOL_RXRPC, RXRPC_ACK, 0, &abort_code);
295                 break;
296         case RXRPC_SKB_MARK_BUSY:
297                 ret = put_cmsg(msg, SOL_RXRPC, RXRPC_BUSY, 0, &abort_code);
298                 break;
299         case RXRPC_SKB_MARK_REMOTE_ABORT:
300                 abort_code = call->abort_code;
301                 ret = put_cmsg(msg, SOL_RXRPC, RXRPC_ABORT, 4, &abort_code);
302                 break;
303         case RXRPC_SKB_MARK_NET_ERROR:
304                 _debug("RECV NET ERROR %d", sp->error);
305                 abort_code = sp->error;
306                 ret = put_cmsg(msg, SOL_RXRPC, RXRPC_NET_ERROR, 4, &abort_code);
307                 break;
308         case RXRPC_SKB_MARK_LOCAL_ERROR:
309                 _debug("RECV LOCAL ERROR %d", sp->error);
310                 abort_code = sp->error;
311                 ret = put_cmsg(msg, SOL_RXRPC, RXRPC_LOCAL_ERROR, 4,
312                                &abort_code);
313                 break;
314         default:
315                 BUG();
316                 break;
317         }
318
319         if (ret < 0)
320                 goto copy_error;
321
322 terminal_message:
323         _debug("terminal");
324         msg->msg_flags &= ~MSG_MORE;
325         msg->msg_flags |= MSG_EOR;
326
327         if (!(flags & MSG_PEEK)) {
328                 _net("free terminal skb %p", skb);
329                 if (skb_dequeue(&rx->sk.sk_receive_queue) != skb)
330                         BUG();
331                 rxrpc_free_skb(skb);
332                 rxrpc_remove_user_ID(rx, call);
333         }
334
335         release_sock(&rx->sk);
336         rxrpc_put_call(call);
337         if (continue_call)
338                 rxrpc_put_call(continue_call);
339         _leave(" = %d", ret);
340         return ret;
341
342 copy_error:
343         _debug("copy error");
344         release_sock(&rx->sk);
345         rxrpc_put_call(call);
346         if (continue_call)
347                 rxrpc_put_call(continue_call);
348         _leave(" = %d", ret);
349         return ret;
350
351 csum_copy_error:
352         _debug("csum error");
353         release_sock(&rx->sk);
354         if (continue_call)
355                 rxrpc_put_call(continue_call);
356         rxrpc_kill_skb(skb);
357         if (!(flags & MSG_PEEK)) {
358                 if (skb_dequeue(&rx->sk.sk_receive_queue) != skb)
359                         BUG();
360         }
361         skb_kill_datagram(&rx->sk, skb, flags);
362         rxrpc_put_call(call);
363         return -EAGAIN;
364
365 wait_interrupted:
366         ret = sock_intr_errno(timeo);
367 wait_error:
368         finish_wait(sk_sleep(&rx->sk), &wait);
369         if (continue_call)
370                 rxrpc_put_call(continue_call);
371         if (copied)
372                 copied = ret;
373         _leave(" = %d [waitfail %d]", copied, ret);
374         return copied;
375
376 }
377
378 /**
379  * rxrpc_kernel_data_delivered - Record delivery of data message
380  * @skb: Message holding data
381  *
382  * Record the delivery of a data message.  This permits RxRPC to keep its
383  * tracking correct.  The socket buffer will be deleted.
384  */
385 void rxrpc_kernel_data_delivered(struct sk_buff *skb)
386 {
387         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
388         struct rxrpc_call *call = sp->call;
389
390         ASSERTCMP(ntohl(sp->hdr.seq), >=, call->rx_data_recv);
391         ASSERTCMP(ntohl(sp->hdr.seq), <=, call->rx_data_recv + 1);
392         call->rx_data_recv = ntohl(sp->hdr.seq);
393
394         ASSERTCMP(ntohl(sp->hdr.seq), >, call->rx_data_eaten);
395         rxrpc_free_skb(skb);
396 }
397
398 EXPORT_SYMBOL(rxrpc_kernel_data_delivered);
399
400 /**
401  * rxrpc_kernel_is_data_last - Determine if data message is last one
402  * @skb: Message holding data
403  *
404  * Determine if data message is last one for the parent call.
405  */
406 bool rxrpc_kernel_is_data_last(struct sk_buff *skb)
407 {
408         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
409
410         ASSERTCMP(skb->mark, ==, RXRPC_SKB_MARK_DATA);
411
412         return sp->hdr.flags & RXRPC_LAST_PACKET;
413 }
414
415 EXPORT_SYMBOL(rxrpc_kernel_is_data_last);
416
417 /**
418  * rxrpc_kernel_get_abort_code - Get the abort code from an RxRPC abort message
419  * @skb: Message indicating an abort
420  *
421  * Get the abort code from an RxRPC abort message.
422  */
423 u32 rxrpc_kernel_get_abort_code(struct sk_buff *skb)
424 {
425         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
426
427         ASSERTCMP(skb->mark, ==, RXRPC_SKB_MARK_REMOTE_ABORT);
428
429         return sp->call->abort_code;
430 }
431
432 EXPORT_SYMBOL(rxrpc_kernel_get_abort_code);
433
434 /**
435  * rxrpc_kernel_get_error - Get the error number from an RxRPC error message
436  * @skb: Message indicating an error
437  *
438  * Get the error number from an RxRPC error message.
439  */
440 int rxrpc_kernel_get_error_number(struct sk_buff *skb)
441 {
442         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
443
444         return sp->error;
445 }
446
447 EXPORT_SYMBOL(rxrpc_kernel_get_error_number);