These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / drivers / staging / lustre / lustre / ptlrpc / events.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) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 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 #define DEBUG_SUBSYSTEM S_RPC
38
39 #include "../../include/linux/libcfs/libcfs.h"
40 # ifdef __mips64__
41 #  include <linux/kernel.h>
42 # endif
43
44 #include "../include/obd_class.h"
45 #include "../include/lustre_net.h"
46 #include "../include/lustre_sec.h"
47 #include "ptlrpc_internal.h"
48
49 lnet_handle_eq_t   ptlrpc_eq_h;
50
51 /*
52  *  Client's outgoing request callback
53  */
54 void request_out_callback(lnet_event_t *ev)
55 {
56         struct ptlrpc_cb_id *cbid = ev->md.user_ptr;
57         struct ptlrpc_request *req = cbid->cbid_arg;
58
59         LASSERT(ev->type == LNET_EVENT_SEND ||
60                 ev->type == LNET_EVENT_UNLINK);
61         LASSERT(ev->unlinked);
62
63         DEBUG_REQ(D_NET, req, "type %d, status %d", ev->type, ev->status);
64
65         sptlrpc_request_out_callback(req);
66         spin_lock(&req->rq_lock);
67         req->rq_real_sent = ktime_get_real_seconds();
68         if (ev->unlinked)
69                 req->rq_req_unlink = 0;
70
71         if (ev->type == LNET_EVENT_UNLINK || ev->status != 0) {
72
73                 /* Failed send: make it seem like the reply timed out, just
74                  * like failing sends in client.c does currently...  */
75
76                 req->rq_net_err = 1;
77                 ptlrpc_client_wake_req(req);
78         }
79         spin_unlock(&req->rq_lock);
80
81         ptlrpc_req_finished(req);
82 }
83
84 /*
85  * Client's incoming reply callback
86  */
87 void reply_in_callback(lnet_event_t *ev)
88 {
89         struct ptlrpc_cb_id *cbid = ev->md.user_ptr;
90         struct ptlrpc_request *req = cbid->cbid_arg;
91
92         DEBUG_REQ(D_NET, req, "type %d, status %d", ev->type, ev->status);
93
94         LASSERT(ev->type == LNET_EVENT_PUT || ev->type == LNET_EVENT_UNLINK);
95         LASSERT(ev->md.start == req->rq_repbuf);
96         LASSERT(ev->offset + ev->mlength <= req->rq_repbuf_len);
97         /* We've set LNET_MD_MANAGE_REMOTE for all outgoing requests
98            for adaptive timeouts' early reply. */
99         LASSERT((ev->md.options & LNET_MD_MANAGE_REMOTE) != 0);
100
101         spin_lock(&req->rq_lock);
102
103         req->rq_receiving_reply = 0;
104         req->rq_early = 0;
105         if (ev->unlinked)
106                 req->rq_reply_unlink = 0;
107
108         if (ev->status)
109                 goto out_wake;
110
111         if (ev->type == LNET_EVENT_UNLINK) {
112                 LASSERT(ev->unlinked);
113                 DEBUG_REQ(D_NET, req, "unlink");
114                 goto out_wake;
115         }
116
117         if (ev->mlength < ev->rlength) {
118                 CDEBUG(D_RPCTRACE, "truncate req %p rpc %d - %d+%d\n", req,
119                        req->rq_replen, ev->rlength, ev->offset);
120                 req->rq_reply_truncate = 1;
121                 req->rq_replied = 1;
122                 req->rq_status = -EOVERFLOW;
123                 req->rq_nob_received = ev->rlength + ev->offset;
124                 goto out_wake;
125         }
126
127         if ((ev->offset == 0) &&
128             ((lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT))) {
129                 /* Early reply */
130                 DEBUG_REQ(D_ADAPTTO, req,
131                           "Early reply received: mlen=%u offset=%d replen=%d replied=%d unlinked=%d",
132                           ev->mlength, ev->offset,
133                           req->rq_replen, req->rq_replied, ev->unlinked);
134
135                 req->rq_early_count++; /* number received, client side */
136
137                 if (req->rq_replied)   /* already got the real reply */
138                         goto out_wake;
139
140                 req->rq_early = 1;
141                 req->rq_reply_off = ev->offset;
142                 req->rq_nob_received = ev->mlength;
143                 /* And we're still receiving */
144                 req->rq_receiving_reply = 1;
145         } else {
146                 /* Real reply */
147                 req->rq_rep_swab_mask = 0;
148                 req->rq_replied = 1;
149                 /* Got reply, no resend required */
150                 req->rq_resend = 0;
151                 req->rq_reply_off = ev->offset;
152                 req->rq_nob_received = ev->mlength;
153                 /* LNetMDUnlink can't be called under the LNET_LOCK,
154                    so we must unlink in ptlrpc_unregister_reply */
155                 DEBUG_REQ(D_INFO, req,
156                           "reply in flags=%x mlen=%u offset=%d replen=%d",
157                           lustre_msg_get_flags(req->rq_reqmsg),
158                           ev->mlength, ev->offset, req->rq_replen);
159         }
160
161         req->rq_import->imp_last_reply_time = ktime_get_real_seconds();
162
163 out_wake:
164         /* NB don't unlock till after wakeup; req can disappear under us
165          * since we don't have our own ref */
166         ptlrpc_client_wake_req(req);
167         spin_unlock(&req->rq_lock);
168 }
169
170 /*
171  * Client's bulk has been written/read
172  */
173 void client_bulk_callback(lnet_event_t *ev)
174 {
175         struct ptlrpc_cb_id *cbid = ev->md.user_ptr;
176         struct ptlrpc_bulk_desc *desc = cbid->cbid_arg;
177         struct ptlrpc_request *req;
178
179         LASSERT((desc->bd_type == BULK_PUT_SINK &&
180                  ev->type == LNET_EVENT_PUT) ||
181                 (desc->bd_type == BULK_GET_SOURCE &&
182                  ev->type == LNET_EVENT_GET) ||
183                 ev->type == LNET_EVENT_UNLINK);
184         LASSERT(ev->unlinked);
185
186         if (CFS_FAIL_CHECK_ORSET(OBD_FAIL_PTLRPC_CLIENT_BULK_CB, CFS_FAIL_ONCE))
187                 ev->status = -EIO;
188
189         if (CFS_FAIL_CHECK_ORSET(OBD_FAIL_PTLRPC_CLIENT_BULK_CB2,
190                                  CFS_FAIL_ONCE))
191                 ev->status = -EIO;
192
193         CDEBUG((ev->status == 0) ? D_NET : D_ERROR,
194                "event type %d, status %d, desc %p\n",
195                ev->type, ev->status, desc);
196
197         spin_lock(&desc->bd_lock);
198         req = desc->bd_req;
199         LASSERT(desc->bd_md_count > 0);
200         desc->bd_md_count--;
201
202         if (ev->type != LNET_EVENT_UNLINK && ev->status == 0) {
203                 desc->bd_nob_transferred += ev->mlength;
204                 desc->bd_sender = ev->sender;
205         } else {
206                 /* start reconnect and resend if network error hit */
207                 spin_lock(&req->rq_lock);
208                 req->rq_net_err = 1;
209                 spin_unlock(&req->rq_lock);
210         }
211
212         if (ev->status != 0)
213                 desc->bd_failure = 1;
214
215         /* NB don't unlock till after wakeup; desc can disappear under us
216          * otherwise */
217         if (desc->bd_md_count == 0)
218                 ptlrpc_client_wake_req(desc->bd_req);
219
220         spin_unlock(&desc->bd_lock);
221 }
222
223 /*
224  * We will have percpt request history list for ptlrpc service in upcoming
225  * patches because we don't want to be serialized by current per-service
226  * history operations. So we require history ID can (somehow) show arriving
227  * order w/o grabbing global lock, and user can sort them in userspace.
228  *
229  * This is how we generate history ID for ptlrpc_request:
230  * ----------------------------------------------------
231  * |  32 bits  |  16 bits  | (16 - X)bits  |  X bits  |
232  * ----------------------------------------------------
233  * |  seconds  | usec / 16 |   sequence    | CPT id   |
234  * ----------------------------------------------------
235  *
236  * it might not be precise but should be good enough.
237  */
238
239 #define REQS_CPT_BITS(svcpt)    ((svcpt)->scp_service->srv_cpt_bits)
240
241 #define REQS_SEC_SHIFT          32
242 #define REQS_USEC_SHIFT         16
243 #define REQS_SEQ_SHIFT(svcpt)   REQS_CPT_BITS(svcpt)
244
245 static void ptlrpc_req_add_history(struct ptlrpc_service_part *svcpt,
246                                    struct ptlrpc_request *req)
247 {
248         __u64 sec = req->rq_arrival_time.tv_sec;
249         __u32 usec = req->rq_arrival_time.tv_nsec / NSEC_PER_USEC / 16; /* usec / 16 */
250         __u64 new_seq;
251
252         /* set sequence ID for request and add it to history list,
253          * it must be called with hold svcpt::scp_lock */
254
255         new_seq = (sec << REQS_SEC_SHIFT) |
256                   (usec << REQS_USEC_SHIFT) |
257                   (svcpt->scp_cpt < 0 ? 0 : svcpt->scp_cpt);
258
259         if (new_seq > svcpt->scp_hist_seq) {
260                 /* This handles the initial case of scp_hist_seq == 0 or
261                  * we just jumped into a new time window */
262                 svcpt->scp_hist_seq = new_seq;
263         } else {
264                 LASSERT(REQS_SEQ_SHIFT(svcpt) < REQS_USEC_SHIFT);
265                 /* NB: increase sequence number in current usec bucket,
266                  * however, it's possible that we used up all bits for
267                  * sequence and jumped into the next usec bucket (future time),
268                  * then we hope there will be less RPCs per bucket at some
269                  * point, and sequence will catch up again */
270                 svcpt->scp_hist_seq += (1U << REQS_SEQ_SHIFT(svcpt));
271                 new_seq = svcpt->scp_hist_seq;
272         }
273
274         req->rq_history_seq = new_seq;
275
276         list_add_tail(&req->rq_history_list, &svcpt->scp_hist_reqs);
277 }
278
279 /*
280  * Server's incoming request callback
281  */
282 void request_in_callback(lnet_event_t *ev)
283 {
284         struct ptlrpc_cb_id *cbid = ev->md.user_ptr;
285         struct ptlrpc_request_buffer_desc *rqbd = cbid->cbid_arg;
286         struct ptlrpc_service_part *svcpt = rqbd->rqbd_svcpt;
287         struct ptlrpc_service *service = svcpt->scp_service;
288         struct ptlrpc_request *req;
289
290         LASSERT(ev->type == LNET_EVENT_PUT ||
291                 ev->type == LNET_EVENT_UNLINK);
292         LASSERT((char *)ev->md.start >= rqbd->rqbd_buffer);
293         LASSERT((char *)ev->md.start + ev->offset + ev->mlength <=
294                 rqbd->rqbd_buffer + service->srv_buf_size);
295
296         CDEBUG((ev->status == 0) ? D_NET : D_ERROR,
297                "event type %d, status %d, service %s\n",
298                ev->type, ev->status, service->srv_name);
299
300         if (ev->unlinked) {
301                 /* If this is the last request message to fit in the
302                  * request buffer we can use the request object embedded in
303                  * rqbd.  Note that if we failed to allocate a request,
304                  * we'd have to re-post the rqbd, which we can't do in this
305                  * context. */
306                 req = &rqbd->rqbd_req;
307                 memset(req, 0, sizeof(*req));
308         } else {
309                 LASSERT(ev->type == LNET_EVENT_PUT);
310                 if (ev->status != 0) {
311                         /* We moaned above already... */
312                         return;
313                 }
314                 req = ptlrpc_request_cache_alloc(GFP_ATOMIC);
315                 if (req == NULL) {
316                         CERROR("Can't allocate incoming request descriptor: Dropping %s RPC from %s\n",
317                                service->srv_name,
318                                libcfs_id2str(ev->initiator));
319                         return;
320                 }
321         }
322
323         /* NB we ABSOLUTELY RELY on req being zeroed, so pointers are NULL,
324          * flags are reset and scalars are zero.  We only set the message
325          * size to non-zero if this was a successful receive. */
326         req->rq_xid = ev->match_bits;
327         req->rq_reqbuf = ev->md.start + ev->offset;
328         if (ev->type == LNET_EVENT_PUT && ev->status == 0)
329                 req->rq_reqdata_len = ev->mlength;
330         ktime_get_real_ts64(&req->rq_arrival_time);
331         req->rq_peer = ev->initiator;
332         req->rq_self = ev->target.nid;
333         req->rq_rqbd = rqbd;
334         req->rq_phase = RQ_PHASE_NEW;
335         spin_lock_init(&req->rq_lock);
336         INIT_LIST_HEAD(&req->rq_timed_list);
337         INIT_LIST_HEAD(&req->rq_exp_list);
338         atomic_set(&req->rq_refcount, 1);
339         if (ev->type == LNET_EVENT_PUT)
340                 CDEBUG(D_INFO, "incoming req@%p x%llu msgsize %u\n",
341                        req, req->rq_xid, ev->mlength);
342
343         CDEBUG(D_RPCTRACE, "peer: %s\n", libcfs_id2str(req->rq_peer));
344
345         spin_lock(&svcpt->scp_lock);
346
347         ptlrpc_req_add_history(svcpt, req);
348
349         if (ev->unlinked) {
350                 svcpt->scp_nrqbds_posted--;
351                 CDEBUG(D_INFO, "Buffer complete: %d buffers still posted\n",
352                        svcpt->scp_nrqbds_posted);
353
354                 /* Normally, don't complain about 0 buffers posted; LNET won't
355                  * drop incoming reqs since we set the portal lazy */
356                 if (test_req_buffer_pressure &&
357                     ev->type != LNET_EVENT_UNLINK &&
358                     svcpt->scp_nrqbds_posted == 0)
359                         CWARN("All %s request buffers busy\n",
360                               service->srv_name);
361
362                 /* req takes over the network's ref on rqbd */
363         } else {
364                 /* req takes a ref on rqbd */
365                 rqbd->rqbd_refcount++;
366         }
367
368         list_add_tail(&req->rq_list, &svcpt->scp_req_incoming);
369         svcpt->scp_nreqs_incoming++;
370
371         /* NB everything can disappear under us once the request
372          * has been queued and we unlock, so do the wake now... */
373         wake_up(&svcpt->scp_waitq);
374
375         spin_unlock(&svcpt->scp_lock);
376 }
377
378 /*
379  *  Server's outgoing reply callback
380  */
381 void reply_out_callback(lnet_event_t *ev)
382 {
383         struct ptlrpc_cb_id *cbid = ev->md.user_ptr;
384         struct ptlrpc_reply_state *rs = cbid->cbid_arg;
385         struct ptlrpc_service_part *svcpt = rs->rs_svcpt;
386
387         LASSERT(ev->type == LNET_EVENT_SEND ||
388                 ev->type == LNET_EVENT_ACK ||
389                 ev->type == LNET_EVENT_UNLINK);
390
391         if (!rs->rs_difficult) {
392                 /* 'Easy' replies have no further processing so I drop the
393                  * net's ref on 'rs' */
394                 LASSERT(ev->unlinked);
395                 ptlrpc_rs_decref(rs);
396                 return;
397         }
398
399         LASSERT(rs->rs_on_net);
400
401         if (ev->unlinked) {
402                 /* Last network callback. The net's ref on 'rs' stays put
403                  * until ptlrpc_handle_rs() is done with it */
404                 spin_lock(&svcpt->scp_rep_lock);
405                 spin_lock(&rs->rs_lock);
406
407                 rs->rs_on_net = 0;
408                 if (!rs->rs_no_ack ||
409                     rs->rs_transno <=
410                     rs->rs_export->exp_obd->obd_last_committed)
411                         ptlrpc_schedule_difficult_reply(rs);
412
413                 spin_unlock(&rs->rs_lock);
414                 spin_unlock(&svcpt->scp_rep_lock);
415         }
416 }
417
418 static void ptlrpc_master_callback(lnet_event_t *ev)
419 {
420         struct ptlrpc_cb_id *cbid = ev->md.user_ptr;
421         void (*callback)(lnet_event_t *ev) = cbid->cbid_fn;
422
423         /* Honestly, it's best to find out early. */
424         LASSERT(cbid->cbid_arg != LP_POISON);
425         LASSERT(callback == request_out_callback ||
426                 callback == reply_in_callback ||
427                 callback == client_bulk_callback ||
428                 callback == request_in_callback ||
429                 callback == reply_out_callback);
430
431         callback(ev);
432 }
433
434 int ptlrpc_uuid_to_peer(struct obd_uuid *uuid,
435                         lnet_process_id_t *peer, lnet_nid_t *self)
436 {
437         int best_dist = 0;
438         __u32 best_order = 0;
439         int count = 0;
440         int rc = -ENOENT;
441         int portals_compatibility;
442         int dist;
443         __u32 order;
444         lnet_nid_t dst_nid;
445         lnet_nid_t src_nid;
446
447         portals_compatibility = LNetCtl(IOC_LIBCFS_PORTALS_COMPATIBILITY, NULL);
448
449         peer->pid = LUSTRE_SRV_LNET_PID;
450
451         /* Choose the matching UUID that's closest */
452         while (lustre_uuid_to_peer(uuid->uuid, &dst_nid, count++) == 0) {
453                 dist = LNetDist(dst_nid, &src_nid, &order);
454                 if (dist < 0)
455                         continue;
456
457                 if (dist == 0) {                /* local! use loopback LND */
458                         peer->nid = *self = LNET_MKNID(LNET_MKNET(LOLND, 0), 0);
459                         rc = 0;
460                         break;
461                 }
462
463                 if (rc < 0 ||
464                     dist < best_dist ||
465                     (dist == best_dist && order < best_order)) {
466                         best_dist = dist;
467                         best_order = order;
468
469                         if (portals_compatibility > 1) {
470                                 /* Strong portals compatibility: Zero the nid's
471                                  * NET, so if I'm reading new config logs, or
472                                  * getting configured by (new) lconf I can
473                                  * still talk to old servers. */
474                                 dst_nid = LNET_MKNID(0, LNET_NIDADDR(dst_nid));
475                                 src_nid = LNET_MKNID(0, LNET_NIDADDR(src_nid));
476                         }
477                         peer->nid = dst_nid;
478                         *self = src_nid;
479                         rc = 0;
480                 }
481         }
482
483         CDEBUG(D_NET, "%s->%s\n", uuid->uuid, libcfs_id2str(*peer));
484         return rc;
485 }
486
487 static void ptlrpc_ni_fini(void)
488 {
489         wait_queue_head_t waitq;
490         struct l_wait_info lwi;
491         int rc;
492         int retries;
493
494         /* Wait for the event queue to become idle since there may still be
495          * messages in flight with pending events (i.e. the fire-and-forget
496          * messages == client requests and "non-difficult" server
497          * replies */
498
499         for (retries = 0;; retries++) {
500                 rc = LNetEQFree(ptlrpc_eq_h);
501                 switch (rc) {
502                 default:
503                         LBUG();
504
505                 case 0:
506                         LNetNIFini();
507                         return;
508
509                 case -EBUSY:
510                         if (retries != 0)
511                                 CWARN("Event queue still busy\n");
512
513                         /* Wait for a bit */
514                         init_waitqueue_head(&waitq);
515                         lwi = LWI_TIMEOUT(cfs_time_seconds(2), NULL, NULL);
516                         l_wait_event(waitq, 0, &lwi);
517                         break;
518                 }
519         }
520         /* notreached */
521 }
522
523 static lnet_pid_t ptl_get_pid(void)
524 {
525         lnet_pid_t pid;
526
527         pid = LUSTRE_SRV_LNET_PID;
528         return pid;
529 }
530
531 static int ptlrpc_ni_init(void)
532 {
533         int rc;
534         lnet_pid_t pid;
535
536         pid = ptl_get_pid();
537         CDEBUG(D_NET, "My pid is: %x\n", pid);
538
539         /* We're not passing any limits yet... */
540         rc = LNetNIInit(pid);
541         if (rc < 0) {
542                 CDEBUG(D_NET, "Can't init network interface: %d\n", rc);
543                 return -ENOENT;
544         }
545
546         /* CAVEAT EMPTOR: how we process portals events is _radically_
547          * different depending on... */
548         /* kernel LNet calls our master callback when there are new event,
549          * because we are guaranteed to get every event via callback,
550          * so we just set EQ size to 0 to avoid overhead of serializing
551          * enqueue/dequeue operations in LNet. */
552         rc = LNetEQAlloc(0, ptlrpc_master_callback, &ptlrpc_eq_h);
553         if (rc == 0)
554                 return 0;
555
556         CERROR("Failed to allocate event queue: %d\n", rc);
557         LNetNIFini();
558
559         return -ENOMEM;
560 }
561
562 int ptlrpc_init_portals(void)
563 {
564         int rc = ptlrpc_ni_init();
565
566         if (rc != 0) {
567                 CERROR("network initialisation failed\n");
568                 return -EIO;
569         }
570         rc = ptlrpcd_addref();
571         if (rc == 0)
572                 return 0;
573
574         CERROR("rpcd initialisation failed\n");
575         ptlrpc_ni_fini();
576         return rc;
577 }
578
579 void ptlrpc_exit_portals(void)
580 {
581         ptlrpcd_decref();
582         ptlrpc_ni_fini();
583 }