missing bits of "splice: fix racy pipe->buffers uses"
[firefly-linux-kernel-4.4.55.git] / drivers / staging / lustre / lnet / klnds / socklnd / socklnd_lib-linux.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #include "socklnd.h"
38
39 int
40 ksocknal_lib_get_conn_addrs (ksock_conn_t *conn)
41 {
42         int rc = libcfs_sock_getaddr(conn->ksnc_sock, 1,
43                                      &conn->ksnc_ipaddr,
44                                      &conn->ksnc_port);
45
46         /* Didn't need the {get,put}connsock dance to deref ksnc_sock... */
47         LASSERT (!conn->ksnc_closing);
48
49         if (rc != 0) {
50                 CERROR ("Error %d getting sock peer IP\n", rc);
51                 return rc;
52         }
53
54         rc = libcfs_sock_getaddr(conn->ksnc_sock, 0,
55                                  &conn->ksnc_myipaddr, NULL);
56         if (rc != 0) {
57                 CERROR ("Error %d getting sock local IP\n", rc);
58                 return rc;
59         }
60
61         return 0;
62 }
63
64 int
65 ksocknal_lib_zc_capable(ksock_conn_t *conn)
66 {
67         int  caps = conn->ksnc_sock->sk->sk_route_caps;
68
69         if (conn->ksnc_proto == &ksocknal_protocol_v1x)
70                 return 0;
71
72         /* ZC if the socket supports scatter/gather and doesn't need software
73          * checksums */
74         return ((caps & NETIF_F_SG) != 0 && (caps & NETIF_F_ALL_CSUM) != 0);
75 }
76
77 int
78 ksocknal_lib_send_iov (ksock_conn_t *conn, ksock_tx_t *tx)
79 {
80         struct socket *sock = conn->ksnc_sock;
81         int         nob;
82         int         rc;
83
84         if (*ksocknal_tunables.ksnd_enable_csum && /* checksum enabled */
85             conn->ksnc_proto == &ksocknal_protocol_v2x && /* V2.x connection  */
86             tx->tx_nob == tx->tx_resid           && /* frist sending    */
87             tx->tx_msg.ksm_csum == 0)                /* not checksummed  */
88                 ksocknal_lib_csum_tx(tx);
89
90         /* NB we can't trust socket ops to either consume our iovs
91          * or leave them alone. */
92
93         {
94 #if SOCKNAL_SINGLE_FRAG_TX
95                 struct iovec    scratch;
96                 struct iovec   *scratchiov = &scratch;
97                 unsigned int    niov = 1;
98 #else
99                 struct iovec   *scratchiov = conn->ksnc_scheduler->kss_scratch_iov;
100                 unsigned int    niov = tx->tx_niov;
101 #endif
102                 struct msghdr msg = {.msg_flags = MSG_DONTWAIT};
103                 int  i;
104
105                 for (nob = i = 0; i < niov; i++) {
106                         scratchiov[i] = tx->tx_iov[i];
107                         nob += scratchiov[i].iov_len;
108                 }
109
110                 if (!list_empty(&conn->ksnc_tx_queue) ||
111                     nob < tx->tx_resid)
112                         msg.msg_flags |= MSG_MORE;
113
114                 rc = kernel_sendmsg(sock, &msg, (struct kvec *)scratchiov, niov, nob);
115         }
116         return rc;
117 }
118
119 int
120 ksocknal_lib_send_kiov (ksock_conn_t *conn, ksock_tx_t *tx)
121 {
122         struct socket *sock = conn->ksnc_sock;
123         lnet_kiov_t   *kiov = tx->tx_kiov;
124         int         rc;
125         int         nob;
126
127         /* Not NOOP message */
128         LASSERT (tx->tx_lnetmsg != NULL);
129
130         /* NB we can't trust socket ops to either consume our iovs
131          * or leave them alone. */
132         if (tx->tx_msg.ksm_zc_cookies[0] != 0) {
133                 /* Zero copy is enabled */
134                 struct sock   *sk = sock->sk;
135                 struct page   *page = kiov->kiov_page;
136                 int         offset = kiov->kiov_offset;
137                 int         fragsize = kiov->kiov_len;
138                 int         msgflg = MSG_DONTWAIT;
139
140                 CDEBUG(D_NET, "page %p + offset %x for %d\n",
141                                page, offset, kiov->kiov_len);
142
143                 if (!list_empty(&conn->ksnc_tx_queue) ||
144                     fragsize < tx->tx_resid)
145                         msgflg |= MSG_MORE;
146
147                 if (sk->sk_prot->sendpage != NULL) {
148                         rc = sk->sk_prot->sendpage(sk, page,
149                                                    offset, fragsize, msgflg);
150                 } else {
151                         rc = cfs_tcp_sendpage(sk, page, offset, fragsize,
152                                               msgflg);
153                 }
154         } else {
155 #if SOCKNAL_SINGLE_FRAG_TX || !SOCKNAL_RISK_KMAP_DEADLOCK
156                 struct iovec  scratch;
157                 struct iovec *scratchiov = &scratch;
158                 unsigned int  niov = 1;
159 #else
160 #ifdef CONFIG_HIGHMEM
161 #warning "XXX risk of kmap deadlock on multiple frags..."
162 #endif
163                 struct iovec *scratchiov = conn->ksnc_scheduler->kss_scratch_iov;
164                 unsigned int  niov = tx->tx_nkiov;
165 #endif
166                 struct msghdr msg = {.msg_flags = MSG_DONTWAIT};
167                 int        i;
168
169                 for (nob = i = 0; i < niov; i++) {
170                         scratchiov[i].iov_base = kmap(kiov[i].kiov_page) +
171                                                  kiov[i].kiov_offset;
172                         nob += scratchiov[i].iov_len = kiov[i].kiov_len;
173                 }
174
175                 if (!list_empty(&conn->ksnc_tx_queue) ||
176                     nob < tx->tx_resid)
177                         msg.msg_flags |= MSG_MORE;
178
179                 rc = kernel_sendmsg(sock, &msg, (struct kvec *)scratchiov, niov, nob);
180
181                 for (i = 0; i < niov; i++)
182                         kunmap(kiov[i].kiov_page);
183         }
184         return rc;
185 }
186
187 void
188 ksocknal_lib_eager_ack (ksock_conn_t *conn)
189 {
190         int         opt = 1;
191         mm_segment_t   oldmm = get_fs();
192         struct socket *sock = conn->ksnc_sock;
193
194         /* Remind the socket to ACK eagerly.  If I don't, the socket might
195          * think I'm about to send something it could piggy-back the ACK
196          * on, introducing delay in completing zero-copy sends in my
197          * peer. */
198
199         set_fs(KERNEL_DS);
200         sock->ops->setsockopt (sock, SOL_TCP, TCP_QUICKACK,
201                                (char *)&opt, sizeof (opt));
202         set_fs(oldmm);
203 }
204
205 int
206 ksocknal_lib_recv_iov (ksock_conn_t *conn)
207 {
208 #if SOCKNAL_SINGLE_FRAG_RX
209         struct iovec  scratch;
210         struct iovec *scratchiov = &scratch;
211         unsigned int  niov = 1;
212 #else
213         struct iovec *scratchiov = conn->ksnc_scheduler->kss_scratch_iov;
214         unsigned int  niov = conn->ksnc_rx_niov;
215 #endif
216         struct iovec *iov = conn->ksnc_rx_iov;
217         struct msghdr msg = {
218                 .msg_flags      = 0
219         };
220         int       nob;
221         int       i;
222         int       rc;
223         int       fragnob;
224         int       sum;
225         __u32   saved_csum;
226
227         /* NB we can't trust socket ops to either consume our iovs
228          * or leave them alone. */
229         LASSERT (niov > 0);
230
231         for (nob = i = 0; i < niov; i++) {
232                 scratchiov[i] = iov[i];
233                 nob += scratchiov[i].iov_len;
234         }
235         LASSERT (nob <= conn->ksnc_rx_nob_wanted);
236
237         rc = kernel_recvmsg(conn->ksnc_sock, &msg,
238                 (struct kvec *)scratchiov, niov, nob, MSG_DONTWAIT);
239
240         saved_csum = 0;
241         if (conn->ksnc_proto == &ksocknal_protocol_v2x) {
242                 saved_csum = conn->ksnc_msg.ksm_csum;
243                 conn->ksnc_msg.ksm_csum = 0;
244         }
245
246         if (saved_csum != 0) {
247                 /* accumulate checksum */
248                 for (i = 0, sum = rc; sum > 0; i++, sum -= fragnob) {
249                         LASSERT (i < niov);
250
251                         fragnob = iov[i].iov_len;
252                         if (fragnob > sum)
253                                 fragnob = sum;
254
255                         conn->ksnc_rx_csum = ksocknal_csum(conn->ksnc_rx_csum,
256                                                            iov[i].iov_base, fragnob);
257                 }
258                 conn->ksnc_msg.ksm_csum = saved_csum;
259         }
260
261         return rc;
262 }
263
264 static void
265 ksocknal_lib_kiov_vunmap(void *addr)
266 {
267         if (addr == NULL)
268                 return;
269
270         vunmap(addr);
271 }
272
273 static void *
274 ksocknal_lib_kiov_vmap(lnet_kiov_t *kiov, int niov,
275                        struct iovec *iov, struct page **pages)
276 {
277         void         *addr;
278         int            nob;
279         int            i;
280
281         if (!*ksocknal_tunables.ksnd_zc_recv || pages == NULL)
282                 return NULL;
283
284         LASSERT (niov <= LNET_MAX_IOV);
285
286         if (niov < 2 ||
287             niov < *ksocknal_tunables.ksnd_zc_recv_min_nfrags)
288                 return NULL;
289
290         for (nob = i = 0; i < niov; i++) {
291                 if ((kiov[i].kiov_offset != 0 && i > 0) ||
292                     (kiov[i].kiov_offset + kiov[i].kiov_len != PAGE_CACHE_SIZE && i < niov - 1))
293                         return NULL;
294
295                 pages[i] = kiov[i].kiov_page;
296                 nob += kiov[i].kiov_len;
297         }
298
299         addr = vmap(pages, niov, VM_MAP, PAGE_KERNEL);
300         if (addr == NULL)
301                 return NULL;
302
303         iov->iov_base = addr + kiov[0].kiov_offset;
304         iov->iov_len = nob;
305
306         return addr;
307 }
308
309 int
310 ksocknal_lib_recv_kiov (ksock_conn_t *conn)
311 {
312 #if SOCKNAL_SINGLE_FRAG_RX || !SOCKNAL_RISK_KMAP_DEADLOCK
313         struct iovec   scratch;
314         struct iovec  *scratchiov = &scratch;
315         struct page  **pages      = NULL;
316         unsigned int   niov       = 1;
317 #else
318 #ifdef CONFIG_HIGHMEM
319 #warning "XXX risk of kmap deadlock on multiple frags..."
320 #endif
321         struct iovec  *scratchiov = conn->ksnc_scheduler->kss_scratch_iov;
322         struct page  **pages      = conn->ksnc_scheduler->kss_rx_scratch_pgs;
323         unsigned int   niov       = conn->ksnc_rx_nkiov;
324 #endif
325         lnet_kiov_t   *kiov = conn->ksnc_rx_kiov;
326         struct msghdr msg = {
327                 .msg_flags      = 0
328         };
329         int       nob;
330         int       i;
331         int       rc;
332         void    *base;
333         void    *addr;
334         int       sum;
335         int       fragnob;
336         int n;
337
338         /* NB we can't trust socket ops to either consume our iovs
339          * or leave them alone. */
340         if ((addr = ksocknal_lib_kiov_vmap(kiov, niov, scratchiov, pages)) != NULL) {
341                 nob = scratchiov[0].iov_len;
342                 n = 1;
343
344         } else {
345                 for (nob = i = 0; i < niov; i++) {
346                         nob += scratchiov[i].iov_len = kiov[i].kiov_len;
347                         scratchiov[i].iov_base = kmap(kiov[i].kiov_page) +
348                                                  kiov[i].kiov_offset;
349                 }
350                 n = niov;
351         }
352
353         LASSERT (nob <= conn->ksnc_rx_nob_wanted);
354
355         rc = kernel_recvmsg(conn->ksnc_sock, &msg,
356                         (struct kvec *)scratchiov, n, nob, MSG_DONTWAIT);
357
358         if (conn->ksnc_msg.ksm_csum != 0) {
359                 for (i = 0, sum = rc; sum > 0; i++, sum -= fragnob) {
360                         LASSERT (i < niov);
361
362                         /* Dang! have to kmap again because I have nowhere to stash the
363                          * mapped address.  But by doing it while the page is still
364                          * mapped, the kernel just bumps the map count and returns me
365                          * the address it stashed. */
366                         base = kmap(kiov[i].kiov_page) + kiov[i].kiov_offset;
367                         fragnob = kiov[i].kiov_len;
368                         if (fragnob > sum)
369                                 fragnob = sum;
370
371                         conn->ksnc_rx_csum = ksocknal_csum(conn->ksnc_rx_csum,
372                                                            base, fragnob);
373
374                         kunmap(kiov[i].kiov_page);
375                 }
376         }
377
378         if (addr != NULL) {
379                 ksocknal_lib_kiov_vunmap(addr);
380         } else {
381                 for (i = 0; i < niov; i++)
382                         kunmap(kiov[i].kiov_page);
383         }
384
385         return (rc);
386 }
387
388 void
389 ksocknal_lib_csum_tx(ksock_tx_t *tx)
390 {
391         int       i;
392         __u32   csum;
393         void    *base;
394
395         LASSERT(tx->tx_iov[0].iov_base == (void *)&tx->tx_msg);
396         LASSERT(tx->tx_conn != NULL);
397         LASSERT(tx->tx_conn->ksnc_proto == &ksocknal_protocol_v2x);
398
399         tx->tx_msg.ksm_csum = 0;
400
401         csum = ksocknal_csum(~0, (void *)tx->tx_iov[0].iov_base,
402                              tx->tx_iov[0].iov_len);
403
404         if (tx->tx_kiov != NULL) {
405                 for (i = 0; i < tx->tx_nkiov; i++) {
406                         base = kmap(tx->tx_kiov[i].kiov_page) +
407                                tx->tx_kiov[i].kiov_offset;
408
409                         csum = ksocknal_csum(csum, base, tx->tx_kiov[i].kiov_len);
410
411                         kunmap(tx->tx_kiov[i].kiov_page);
412                 }
413         } else {
414                 for (i = 1; i < tx->tx_niov; i++)
415                         csum = ksocknal_csum(csum, tx->tx_iov[i].iov_base,
416                                              tx->tx_iov[i].iov_len);
417         }
418
419         if (*ksocknal_tunables.ksnd_inject_csum_error) {
420                 csum++;
421                 *ksocknal_tunables.ksnd_inject_csum_error = 0;
422         }
423
424         tx->tx_msg.ksm_csum = csum;
425 }
426
427 int
428 ksocknal_lib_get_conn_tunables (ksock_conn_t *conn, int *txmem, int *rxmem, int *nagle)
429 {
430         mm_segment_t   oldmm = get_fs ();
431         struct socket *sock = conn->ksnc_sock;
432         int         len;
433         int         rc;
434
435         rc = ksocknal_connsock_addref(conn);
436         if (rc != 0) {
437                 LASSERT (conn->ksnc_closing);
438                 *txmem = *rxmem = *nagle = 0;
439                 return (-ESHUTDOWN);
440         }
441
442         rc = libcfs_sock_getbuf(sock, txmem, rxmem);
443         if (rc == 0) {
444                 len = sizeof(*nagle);
445                 set_fs(KERNEL_DS);
446                 rc = sock->ops->getsockopt(sock, SOL_TCP, TCP_NODELAY,
447                                            (char *)nagle, &len);
448                 set_fs(oldmm);
449         }
450
451         ksocknal_connsock_decref(conn);
452
453         if (rc == 0)
454                 *nagle = !*nagle;
455         else
456                 *txmem = *rxmem = *nagle = 0;
457
458         return (rc);
459 }
460
461 int
462 ksocknal_lib_setup_sock (struct socket *sock)
463 {
464         mm_segment_t    oldmm = get_fs ();
465         int          rc;
466         int          option;
467         int          keep_idle;
468         int          keep_intvl;
469         int          keep_count;
470         int          do_keepalive;
471         struct linger   linger;
472
473         sock->sk->sk_allocation = GFP_NOFS;
474
475         /* Ensure this socket aborts active sends immediately when we close
476          * it. */
477
478         linger.l_onoff = 0;
479         linger.l_linger = 0;
480
481         set_fs (KERNEL_DS);
482         rc = sock_setsockopt (sock, SOL_SOCKET, SO_LINGER,
483                               (char *)&linger, sizeof (linger));
484         set_fs (oldmm);
485         if (rc != 0) {
486                 CERROR ("Can't set SO_LINGER: %d\n", rc);
487                 return (rc);
488         }
489
490         option = -1;
491         set_fs (KERNEL_DS);
492         rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_LINGER2,
493                                     (char *)&option, sizeof (option));
494         set_fs (oldmm);
495         if (rc != 0) {
496                 CERROR ("Can't set SO_LINGER2: %d\n", rc);
497                 return (rc);
498         }
499
500         if (!*ksocknal_tunables.ksnd_nagle) {
501                 option = 1;
502
503                 set_fs (KERNEL_DS);
504                 rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_NODELAY,
505                                             (char *)&option, sizeof (option));
506                 set_fs (oldmm);
507                 if (rc != 0) {
508                         CERROR ("Can't disable nagle: %d\n", rc);
509                         return (rc);
510                 }
511         }
512
513         rc = libcfs_sock_setbuf(sock,
514                                 *ksocknal_tunables.ksnd_tx_buffer_size,
515                                 *ksocknal_tunables.ksnd_rx_buffer_size);
516         if (rc != 0) {
517                 CERROR ("Can't set buffer tx %d, rx %d buffers: %d\n",
518                         *ksocknal_tunables.ksnd_tx_buffer_size,
519                         *ksocknal_tunables.ksnd_rx_buffer_size, rc);
520                 return (rc);
521         }
522
523 /* TCP_BACKOFF_* sockopt tunables unsupported in stock kernels */
524
525         /* snapshot tunables */
526         keep_idle  = *ksocknal_tunables.ksnd_keepalive_idle;
527         keep_count = *ksocknal_tunables.ksnd_keepalive_count;
528         keep_intvl = *ksocknal_tunables.ksnd_keepalive_intvl;
529
530         do_keepalive = (keep_idle > 0 && keep_count > 0 && keep_intvl > 0);
531
532         option = (do_keepalive ? 1 : 0);
533         set_fs (KERNEL_DS);
534         rc = sock_setsockopt (sock, SOL_SOCKET, SO_KEEPALIVE,
535                               (char *)&option, sizeof (option));
536         set_fs (oldmm);
537         if (rc != 0) {
538                 CERROR ("Can't set SO_KEEPALIVE: %d\n", rc);
539                 return (rc);
540         }
541
542         if (!do_keepalive)
543                 return (0);
544
545         set_fs (KERNEL_DS);
546         rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_KEEPIDLE,
547                                     (char *)&keep_idle, sizeof (keep_idle));
548         set_fs (oldmm);
549         if (rc != 0) {
550                 CERROR ("Can't set TCP_KEEPIDLE: %d\n", rc);
551                 return (rc);
552         }
553
554         set_fs (KERNEL_DS);
555         rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_KEEPINTVL,
556                                     (char *)&keep_intvl, sizeof (keep_intvl));
557         set_fs (oldmm);
558         if (rc != 0) {
559                 CERROR ("Can't set TCP_KEEPINTVL: %d\n", rc);
560                 return (rc);
561         }
562
563         set_fs (KERNEL_DS);
564         rc = sock->ops->setsockopt (sock, SOL_TCP, TCP_KEEPCNT,
565                                     (char *)&keep_count, sizeof (keep_count));
566         set_fs (oldmm);
567         if (rc != 0) {
568                 CERROR ("Can't set TCP_KEEPCNT: %d\n", rc);
569                 return (rc);
570         }
571
572         return (0);
573 }
574
575 void
576 ksocknal_lib_push_conn (ksock_conn_t *conn)
577 {
578         struct sock    *sk;
579         struct tcp_sock *tp;
580         int          nonagle;
581         int          val = 1;
582         int          rc;
583         mm_segment_t    oldmm;
584
585         rc = ksocknal_connsock_addref(conn);
586         if (rc != 0)                        /* being shut down */
587                 return;
588
589         sk = conn->ksnc_sock->sk;
590         tp = tcp_sk(sk);
591
592         lock_sock (sk);
593         nonagle = tp->nonagle;
594         tp->nonagle = 1;
595         release_sock (sk);
596
597         oldmm = get_fs ();
598         set_fs (KERNEL_DS);
599
600         rc = sk->sk_prot->setsockopt (sk, SOL_TCP, TCP_NODELAY,
601                                       (char *)&val, sizeof (val));
602         LASSERT (rc == 0);
603
604         set_fs (oldmm);
605
606         lock_sock (sk);
607         tp->nonagle = nonagle;
608         release_sock (sk);
609
610         ksocknal_connsock_decref(conn);
611 }
612
613 extern void ksocknal_read_callback (ksock_conn_t *conn);
614 extern void ksocknal_write_callback (ksock_conn_t *conn);
615 /*
616  * socket call back in Linux
617  */
618 static void
619 ksocknal_data_ready (struct sock *sk, int n)
620 {
621         ksock_conn_t  *conn;
622
623         /* interleave correctly with closing sockets... */
624         LASSERT(!in_irq());
625         read_lock(&ksocknal_data.ksnd_global_lock);
626
627         conn = sk->sk_user_data;
628         if (conn == NULL) {          /* raced with ksocknal_terminate_conn */
629                 LASSERT (sk->sk_data_ready != &ksocknal_data_ready);
630                 sk->sk_data_ready (sk, n);
631         } else
632                 ksocknal_read_callback(conn);
633
634         read_unlock(&ksocknal_data.ksnd_global_lock);
635 }
636
637 static void
638 ksocknal_write_space (struct sock *sk)
639 {
640         ksock_conn_t  *conn;
641         int         wspace;
642         int         min_wpace;
643
644         /* interleave correctly with closing sockets... */
645         LASSERT(!in_irq());
646         read_lock(&ksocknal_data.ksnd_global_lock);
647
648         conn = sk->sk_user_data;
649         wspace = SOCKNAL_WSPACE(sk);
650         min_wpace = SOCKNAL_MIN_WSPACE(sk);
651
652         CDEBUG(D_NET, "sk %p wspace %d low water %d conn %p%s%s%s\n",
653                sk, wspace, min_wpace, conn,
654                (conn == NULL) ? "" : (conn->ksnc_tx_ready ?
655                                       " ready" : " blocked"),
656                (conn == NULL) ? "" : (conn->ksnc_tx_scheduled ?
657                                       " scheduled" : " idle"),
658                (conn == NULL) ? "" : (list_empty (&conn->ksnc_tx_queue) ?
659                                       " empty" : " queued"));
660
661         if (conn == NULL) {          /* raced with ksocknal_terminate_conn */
662                 LASSERT (sk->sk_write_space != &ksocknal_write_space);
663                 sk->sk_write_space (sk);
664
665                 read_unlock(&ksocknal_data.ksnd_global_lock);
666                 return;
667         }
668
669         if (wspace >= min_wpace) {            /* got enough space */
670                 ksocknal_write_callback(conn);
671
672                 /* Clear SOCK_NOSPACE _after_ ksocknal_write_callback so the
673                  * ENOMEM check in ksocknal_transmit is race-free (think about
674                  * it). */
675
676                 clear_bit (SOCK_NOSPACE, &sk->sk_socket->flags);
677         }
678
679         read_unlock(&ksocknal_data.ksnd_global_lock);
680 }
681
682 void
683 ksocknal_lib_save_callback(struct socket *sock, ksock_conn_t *conn)
684 {
685         conn->ksnc_saved_data_ready = sock->sk->sk_data_ready;
686         conn->ksnc_saved_write_space = sock->sk->sk_write_space;
687 }
688
689 void
690 ksocknal_lib_set_callback(struct socket *sock,  ksock_conn_t *conn)
691 {
692         sock->sk->sk_user_data = conn;
693         sock->sk->sk_data_ready = ksocknal_data_ready;
694         sock->sk->sk_write_space = ksocknal_write_space;
695         return;
696 }
697
698 void
699 ksocknal_lib_reset_callback(struct socket *sock, ksock_conn_t *conn)
700 {
701         /* Remove conn's network callbacks.
702          * NB I _have_ to restore the callback, rather than storing a noop,
703          * since the socket could survive past this module being unloaded!! */
704         sock->sk->sk_data_ready = conn->ksnc_saved_data_ready;
705         sock->sk->sk_write_space = conn->ksnc_saved_write_space;
706
707         /* A callback could be in progress already; they hold a read lock
708          * on ksnd_global_lock (to serialise with me) and NOOP if
709          * sk_user_data is NULL. */
710         sock->sk->sk_user_data = NULL;
711
712         return ;
713 }
714
715 int
716 ksocknal_lib_memory_pressure(ksock_conn_t *conn)
717 {
718         int         rc = 0;
719         ksock_sched_t *sched;
720
721         sched = conn->ksnc_scheduler;
722         spin_lock_bh(&sched->kss_lock);
723
724         if (!SOCK_TEST_NOSPACE(conn->ksnc_sock) &&
725             !conn->ksnc_tx_ready) {
726                 /* SOCK_NOSPACE is set when the socket fills
727                  * and cleared in the write_space callback
728                  * (which also sets ksnc_tx_ready).  If
729                  * SOCK_NOSPACE and ksnc_tx_ready are BOTH
730                  * zero, I didn't fill the socket and
731                  * write_space won't reschedule me, so I
732                  * return -ENOMEM to get my caller to retry
733                  * after a timeout */
734                 rc = -ENOMEM;
735         }
736
737         spin_unlock_bh(&sched->kss_lock);
738
739         return rc;
740 }