Whamcloud - gitweb
Landing b_recovery
[fs/lustre-release.git] / lustre / ptlrpc / service.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2002 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #define DEBUG_SUBSYSTEM S_RPC
24 #ifndef __KERNEL__
25 #include <liblustre.h>
26 #include <linux/kp30.h>
27 #endif
28 #include <linux/obd_support.h>
29 #include <linux/obd_class.h>
30 #include <linux/lustre_net.h>
31 #include <portals/types.h>
32 #include "ptlrpc_internal.h"
33
34 extern int request_in_callback(ptl_event_t *ev);
35
36 static int ptlrpc_check_event(struct ptlrpc_service *svc,
37                               struct ptlrpc_thread *thread, ptl_event_t *event)
38 {
39         struct ptlrpc_srv_ni *srv_ni;
40         int i, idx, rc;
41         ENTRY;
42
43         spin_lock(&svc->srv_lock);
44
45         if (thread->t_flags & SVC_STOPPING)
46                 GOTO(out, rc = 1);
47
48         LASSERT ((thread->t_flags & SVC_EVENT) == 0);
49         LASSERT (ptlrpc_ninterfaces > 0);
50
51         for (i = 0; i < ptlrpc_ninterfaces; i++) {
52                 idx = (svc->srv_interface_rover + i) % ptlrpc_ninterfaces;
53                 srv_ni = &svc->srv_interfaces[idx];
54
55                 LASSERT (!PtlHandleEqual (srv_ni->sni_eq_h, PTL_HANDLE_NONE));
56
57                 rc = PtlEQGet(srv_ni->sni_eq_h, event);
58                 switch (rc) {
59                 case PTL_OK:
60                         /* next time start with the next interface */
61                         svc->srv_interface_rover = (idx+1) % ptlrpc_ninterfaces;
62                         thread->t_flags |= SVC_EVENT;
63                         GOTO(out, rc = 1);
64
65                 case PTL_EQ_EMPTY:
66                         continue;
67
68                 case PTL_EQ_DROPPED:
69                         CWARN("Event queue overflow (bug 2125): timeouts will "
70                               "follow.\n");
71                         continue;
72
73                 default:
74                         CERROR("BUG: PtlEQGet returned %d\n", rc);
75                         LBUG();
76                 }
77         }
78         rc = 0;
79         EXIT;
80  out:
81         spin_unlock(&svc->srv_lock);
82         return rc;
83 }
84
85 struct ptlrpc_service * ptlrpc_init_svc(__u32 nevents, __u32 nbufs,
86                                         __u32 bufsize, __u32 max_req_size,
87                                         int req_portal, int rep_portal,
88                                         svc_handler_t handler, char *name,
89                                         struct proc_dir_entry *proc_entry)
90 {
91         int i, j, ssize, rc;
92         struct ptlrpc_service *service;
93         struct ptlrpc_srv_ni  *srv_ni;
94         ENTRY;
95
96         LASSERT (ptlrpc_ninterfaces > 0);
97
98         ssize = offsetof (struct ptlrpc_service,
99                           srv_interfaces[ptlrpc_ninterfaces]);
100         OBD_ALLOC(service, ssize);
101         if (service == NULL)
102                 RETURN(NULL);
103
104         service->srv_name = name;
105         spin_lock_init(&service->srv_lock);
106         INIT_LIST_HEAD(&service->srv_threads);
107         init_waitqueue_head(&service->srv_waitq);
108
109         service->srv_max_req_size = max_req_size;
110         service->srv_buf_size = bufsize;
111
112         service->srv_rep_portal = rep_portal;
113         service->srv_req_portal = req_portal;
114         service->srv_handler = handler;
115         service->srv_interface_rover = 0;
116
117         /* First initialise enough for early teardown */
118         for (i = 0; i < ptlrpc_ninterfaces; i++) {
119                 srv_ni = &service->srv_interfaces[i];
120
121                 srv_ni->sni_service = service;
122                 srv_ni->sni_ni = &ptlrpc_interfaces[i];
123                 srv_ni->sni_eq_h = PTL_HANDLE_NONE;
124                 INIT_LIST_HEAD(&srv_ni->sni_rqbds);
125                 srv_ni->sni_nrqbds = 0;
126                 atomic_set(&srv_ni->sni_nrqbds_receiving, 0);
127         }
128
129         /* Now allocate the event queue and request buffers, assuming all
130          * interfaces require the same level of buffering. */
131         for (i = 0; i < ptlrpc_ninterfaces; i++) {
132                 srv_ni = &service->srv_interfaces[i];
133                 CDEBUG (D_NET, "%s: initialising interface %s\n", name,
134                         srv_ni->sni_ni->pni_name);
135
136                 rc = PtlEQAlloc(srv_ni->sni_ni->pni_ni_h, nevents,
137                                 request_in_callback, &(srv_ni->sni_eq_h));
138                 if (rc != PTL_OK) {
139                         CERROR("%s.%d: PtlEQAlloc on %s failed: %d\n",
140                                name, i, srv_ni->sni_ni->pni_name, rc);
141                         GOTO (failed, NULL);
142                 }
143
144                 for (j = 0; j < nbufs; j++) {
145                         struct ptlrpc_request_buffer_desc *rqbd;
146
147                         OBD_ALLOC_WAIT(rqbd, sizeof(*rqbd));
148                         if (rqbd == NULL) {
149                                 CERROR ("%s.%d: Can't allocate request "
150                                         "descriptor %d on %s\n",
151                                         name, i, srv_ni->sni_nrqbds,
152                                         srv_ni->sni_ni->pni_name);
153                                 GOTO(failed, NULL);
154                         }
155
156                         rqbd->rqbd_srv_ni = srv_ni;
157                         rqbd->rqbd_me_h = PTL_HANDLE_NONE;
158                         atomic_set(&rqbd->rqbd_refcount, 0);
159
160                         OBD_ALLOC_WAIT(rqbd->rqbd_buffer, service->srv_buf_size);
161                         if (rqbd->rqbd_buffer == NULL) {
162                                 CERROR ("%s.%d: Can't allocate request "
163                                         "buffer %d on %s\n",
164                                         name, i, srv_ni->sni_nrqbds,
165                                         srv_ni->sni_ni->pni_name);
166                                 OBD_FREE(rqbd, sizeof(*rqbd));
167                                 GOTO(failed, NULL);
168                         }
169                         list_add(&rqbd->rqbd_list, &srv_ni->sni_rqbds);
170                         srv_ni->sni_nrqbds++;
171
172                         ptlrpc_link_svc_me(rqbd);
173                 }
174         }
175
176         if (proc_entry != NULL)
177                 ptlrpc_lprocfs_register_service(proc_entry, service);
178
179         CDEBUG(D_NET, "%s: Started on %d interfaces, listening on portal %d\n",
180                service->srv_name, ptlrpc_ninterfaces, service->srv_req_portal);
181
182         RETURN(service);
183 failed:
184         ptlrpc_unregister_service(service);
185         return NULL;
186 }
187
188 static int handle_incoming_request(struct obd_device *obddev,
189                                    struct ptlrpc_service *svc,
190                                    ptl_event_t *event,
191                                    struct ptlrpc_request *request)
192 {
193         struct ptlrpc_request_buffer_desc *rqbd = event->mem_desc.user_ptr;
194         int rc;
195
196         /* FIXME: If we move to an event-driven model, we should put the request
197          * on the stack of mds_handle instead. */
198
199         LASSERT (atomic_read (&rqbd->rqbd_refcount) > 0);
200         LASSERT ((event->mem_desc.options & (PTL_MD_IOV | PTL_MD_KIOV)) == 0);
201         LASSERT (rqbd->rqbd_srv_ni->sni_service == svc);
202         LASSERT (rqbd->rqbd_buffer == event->mem_desc.start);
203         LASSERT (event->offset + event->mlength <= svc->srv_buf_size);
204
205         memset(request, 0, sizeof(*request));
206         spin_lock_init (&request->rq_lock);
207         INIT_LIST_HEAD(&request->rq_list);
208         request->rq_svc = svc;
209         request->rq_xid = event->match_bits;
210         request->rq_reqmsg = event->mem_desc.start + event->offset;
211         request->rq_reqlen = event->mlength;
212
213 #if SWAB_PARANOIA
214         /* Clear request swab mask; this is a new request */
215         request->rq_req_swab_mask = 0;
216 #endif
217         rc = lustre_unpack_msg (request->rq_reqmsg, request->rq_reqlen);
218         if (rc != 0) {
219                 CERROR ("error unpacking request: ptl %d from "LPX64
220                         " xid "LPU64"\n", svc->srv_req_portal,
221                        event->initiator.nid, request->rq_xid);
222                 goto out;
223         }
224         rc = -EINVAL;
225         if (request->rq_reqmsg->type != PTL_RPC_MSG_REQUEST) {
226                 CERROR("wrong packet type received (type=%u)\n",
227                        request->rq_reqmsg->type);
228                 goto out;
229         }
230
231         CDEBUG(D_NET, "got req "LPD64" (md: %p + %d)\n", request->rq_xid,
232                event->mem_desc.start, event->offset);
233
234         request->rq_peer.peer_nid = event->initiator.nid;
235         request->rq_peer.peer_ni = rqbd->rqbd_srv_ni->sni_ni;
236
237         request->rq_export = class_conn2export(&request->rq_reqmsg->handle);
238
239         if (request->rq_export) {
240                 if (request->rq_reqmsg->conn_cnt < 
241                     request->rq_export->exp_conn_cnt) {
242                         DEBUG_REQ(D_ERROR, request,
243                                   "DROPPING req from old connection %d < %d",
244                                   request->rq_reqmsg->conn_cnt,
245                                   request->rq_export->exp_conn_cnt);
246                         goto put_conn;
247                 }
248
249                 request->rq_export->exp_last_request_time =
250                         LTIME_S(CURRENT_TIME);
251         } 
252
253         CDEBUG(D_RPCTRACE, "Handling RPC pname:cluuid+ref:pid:xid:ni:nid:opc "
254                "%s:%s+%d:%d:"LPU64":%s:"LPX64":%d\n", current->comm,
255                (request->rq_export ?
256                 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
257                (request->rq_export ?
258                 atomic_read(&request->rq_export->exp_refcount) : -99),
259                request->rq_reqmsg->status, request->rq_xid,
260                rqbd->rqbd_srv_ni->sni_ni->pni_name, event->initiator.nid,
261                request->rq_reqmsg->opc);
262
263         rc = svc->srv_handler(request);
264         CDEBUG(D_RPCTRACE, "Handled RPC pname:cluuid+ref:pid:xid:ni:nid:opc "
265                "%s:%s+%d:%d:"LPU64":%s:"LPX64":%d\n", current->comm,
266                (request->rq_export ?
267                 (char *)request->rq_export->exp_client_uuid.uuid : "0"),
268                (request->rq_export ?
269                 atomic_read(&request->rq_export->exp_refcount) : -99),
270                request->rq_reqmsg->status, request->rq_xid,
271                rqbd->rqbd_srv_ni->sni_ni->pni_name, event->initiator.nid,
272                request->rq_reqmsg->opc);
273
274 put_conn:
275         if (request->rq_export != NULL)
276                 class_export_put(request->rq_export);
277
278  out:
279         if (atomic_dec_and_test (&rqbd->rqbd_refcount)) /* last reference? */
280                 ptlrpc_link_svc_me (rqbd);
281
282         return rc;
283 }
284
285 /* Don't use daemonize, it removes fs struct from new thread (bug 418) */
286 void ptlrpc_daemonize(void)
287 {
288         exit_mm(current);
289         lustre_daemonize_helper();
290         exit_files(current);
291         reparent_to_init();
292 }
293
294 static long timeval_sub(struct timeval *large, struct timeval *small)
295 {
296         return (large->tv_sec - small->tv_sec) * 1000000 +
297                 (large->tv_usec - small->tv_usec);
298 }
299
300 static int ptlrpc_main(void *arg)
301 {
302         struct ptlrpc_svc_data *data = arg;
303         struct obd_device *obddev = data->dev;
304         struct ptlrpc_service *svc = data->svc;
305         struct ptlrpc_thread *thread = data->thread;
306         struct ptlrpc_request *request;
307         ptl_event_t *event;
308         unsigned long flags;
309         struct timeval start_time, finish_time;
310         long total;
311         int rc = 0;
312         ENTRY;
313
314         lock_kernel();
315         ptlrpc_daemonize();
316
317         SIGNAL_MASK_LOCK(current, flags);
318         sigfillset(&current->blocked);
319         RECALC_SIGPENDING;
320         SIGNAL_MASK_UNLOCK(current, flags);
321
322         THREAD_NAME(current->comm, "%s", data->name);
323         unlock_kernel();
324
325         OBD_ALLOC(event, sizeof(*event));
326         if (event == NULL)
327                 GOTO(out, rc = -ENOMEM);
328         OBD_ALLOC(request, sizeof(*request));
329         if (request == NULL)
330                 GOTO(out_event, rc = -ENOMEM);
331
332         /* Record that the thread is running */
333         thread->t_flags = SVC_RUNNING;
334         wake_up(&thread->t_ctl_waitq);
335
336         /* XXX maintain a list of all managed devices: insert here */
337
338         do_gettimeofday(&finish_time);
339         /* And now, loop forever on requests */
340         while (1) {
341                 struct l_wait_info lwi = { 0 };
342                 l_wait_event_exclusive(svc->srv_waitq,
343                                        ptlrpc_check_event(svc, thread, event),
344                                        &lwi);
345
346                 spin_lock(&svc->srv_lock);
347                 if (thread->t_flags & SVC_STOPPING) {
348                         thread->t_flags &= ~SVC_STOPPING;
349                         spin_unlock(&svc->srv_lock);
350
351                         EXIT;
352                         break;
353                 }
354
355                 if (!(thread->t_flags & SVC_EVENT)) {
356                         CERROR("unknown flag in service");
357                         spin_unlock(&svc->srv_lock);
358                         LBUG();
359                         EXIT;
360                         break;
361                 }
362
363                 thread->t_flags &= ~SVC_EVENT;
364                 spin_unlock(&svc->srv_lock);
365
366                 do_gettimeofday(&start_time);
367                 total = timeval_sub(&start_time, &event->arrival_time);
368                 if (svc->srv_stats != NULL) {
369                         lprocfs_counter_add(svc->srv_stats, PTLRPC_REQWAIT_CNTR,
370                                             total);
371                         lprocfs_counter_add(svc->srv_stats,
372                                             PTLRPC_SVCIDLETIME_CNTR,
373                                             timeval_sub(&start_time,
374                                                         &finish_time));
375 #if 0 /* Wait for b_eq branch */
376                         lprocfs_counter_add(svc->srv_stats,
377                                             PTLRPC_SVCEQDEPTH_CNTR, 0);
378 #endif
379                 }
380
381                 if (total / 1000000 > (long)obd_timeout) {
382                         CERROR("Dropping request from NID "LPX64" because it's "
383                                "%ld seconds old.\n", event->initiator.nid,
384                                total / 1000000); /* bug 1502 */
385                 } else {
386                         CDEBUG(D_HA, "request from NID "LPX64" noticed after "
387                                "%ldus\n", event->initiator.nid, total);
388                         rc = handle_incoming_request(obddev, svc, event,
389                                                      request);
390                 }
391                 do_gettimeofday(&finish_time);
392                 total = timeval_sub(&finish_time, &start_time);
393
394                 CDEBUG((total / 1000000 > (long)obd_timeout) ? D_ERROR : D_HA,
395                        "request "LPU64" from NID "LPX64" processed in %ldus "
396                        "(%ldus total)\n", request->rq_xid, event->initiator.nid,
397                        total, timeval_sub(&finish_time, &event->arrival_time));
398
399                 if (svc->srv_stats != NULL) {
400                         int opc = opcode_offset(request->rq_reqmsg->opc);
401                         if (opc > 0) {
402                                 LASSERT(opc < LUSTRE_MAX_OPCODES);
403                                 lprocfs_counter_add(svc->srv_stats,
404                                                     opc + PTLRPC_LAST_CNTR,
405                                                     total);
406                         }
407                 }
408         }
409
410         /* NB should wait for all SENT callbacks to complete before exiting
411          * here.  Unfortunately at this time there is no way to track this
412          * state. */
413         OBD_FREE(request, sizeof(*request));
414 out_event:
415         OBD_FREE(event, sizeof(*event));
416 out:
417         thread->t_flags = SVC_STOPPED;
418         wake_up(&thread->t_ctl_waitq);
419
420         CDEBUG(D_NET, "service thread exiting, process %d: rc = %d\n",
421                current->pid, rc);
422         return rc;
423 }
424
425 static void ptlrpc_stop_thread(struct ptlrpc_service *svc,
426                                struct ptlrpc_thread *thread)
427 {
428         struct l_wait_info lwi = { 0 };
429
430         spin_lock(&svc->srv_lock);
431         thread->t_flags = SVC_STOPPING;
432         spin_unlock(&svc->srv_lock);
433
434         wake_up_all(&svc->srv_waitq);
435         l_wait_event(thread->t_ctl_waitq, (thread->t_flags & SVC_STOPPED),
436                      &lwi);
437 }
438
439 void ptlrpc_stop_all_threads(struct ptlrpc_service *svc)
440 {
441         spin_lock(&svc->srv_lock);
442         while (!list_empty(&svc->srv_threads)) {
443                 struct ptlrpc_thread *thread;
444                 thread = list_entry(svc->srv_threads.next, struct ptlrpc_thread,
445                                     t_link);
446                 spin_unlock(&svc->srv_lock);
447                 ptlrpc_stop_thread(svc, thread);
448                 spin_lock(&svc->srv_lock);
449                 list_del(&thread->t_link);
450                 OBD_FREE(thread, sizeof(*thread));
451         }
452         spin_unlock(&svc->srv_lock);
453 }
454
455 int ptlrpc_start_n_threads(struct obd_device *dev, struct ptlrpc_service *svc,
456                            int num_threads, char *base_name)
457 {
458         int i, rc = 0;
459         ENTRY;
460
461         for (i = 0; i < num_threads; i++) {
462                 char name[32];
463                 sprintf(name, "%s_%02d", base_name, i);
464                 rc = ptlrpc_start_thread(dev, svc, name);
465                 if (rc) {
466                         CERROR("cannot start %s thread #%d: rc %d\n", base_name,
467                                i, rc);
468                         ptlrpc_stop_all_threads(svc);
469                 }
470         }
471         RETURN(rc);
472 }
473
474 int ptlrpc_start_thread(struct obd_device *dev, struct ptlrpc_service *svc,
475                         char *name)
476 {
477         struct l_wait_info lwi = { 0 };
478         struct ptlrpc_svc_data d;
479         struct ptlrpc_thread *thread;
480         int rc;
481         ENTRY;
482
483         OBD_ALLOC(thread, sizeof(*thread));
484         if (thread == NULL)
485                 RETURN(-ENOMEM);
486         init_waitqueue_head(&thread->t_ctl_waitq);
487
488         d.dev = dev;
489         d.svc = svc;
490         d.name = name;
491         d.thread = thread;
492
493         spin_lock(&svc->srv_lock);
494         list_add(&thread->t_link, &svc->srv_threads);
495         spin_unlock(&svc->srv_lock);
496
497         /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
498          * just drop the VM and FILES in ptlrpc_daemonize() right away.
499          */
500         rc = kernel_thread(ptlrpc_main, &d, CLONE_VM | CLONE_FILES);
501         if (rc < 0) {
502                 CERROR("cannot start thread: %d\n", rc);
503                 OBD_FREE(thread, sizeof(*thread));
504                 RETURN(rc);
505         }
506         l_wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_RUNNING, &lwi);
507
508         RETURN(0);
509 }
510
511 int ptlrpc_unregister_service(struct ptlrpc_service *service)
512 {
513         int i, rc;
514         struct ptlrpc_srv_ni *srv_ni;
515
516         LASSERT (list_empty (&service->srv_threads));
517
518         /* XXX We could reply (with failure) to all buffered requests
519          * _after_ unlinking _all_ the request buffers, but _before_
520          * freeing them.
521          */
522
523         for (i = 0; i < ptlrpc_ninterfaces; i++) {
524                 srv_ni = &service->srv_interfaces[i];
525                 CDEBUG (D_NET, "%s: tearing down interface %s\n",
526                         service->srv_name, srv_ni->sni_ni->pni_name);
527
528                 while (!list_empty (&srv_ni->sni_rqbds)) {
529                         struct ptlrpc_request_buffer_desc *rqbd =
530                                 list_entry (srv_ni->sni_rqbds.next,
531                                             struct ptlrpc_request_buffer_desc,
532                                             rqbd_list);
533
534                         list_del (&rqbd->rqbd_list);
535
536                         LASSERT (atomic_read (&rqbd->rqbd_refcount) > 0);
537                         /* refcount could be anything; it's possible for
538                          * the buffers to continued to get filled after all
539                          * the server threads exited.  But we know they
540                          * _have_ exited.
541                          */
542
543                         (void) PtlMEUnlink(rqbd->rqbd_me_h);
544                         /* The callback handler could have unlinked this ME
545                          * already (we're racing with her) but it's safe to
546                          * ensure it _has_ been unlinked.
547                          */
548
549                         OBD_FREE (rqbd->rqbd_buffer, service->srv_buf_size);
550                         OBD_FREE (rqbd, sizeof (*rqbd));
551                         srv_ni->sni_nrqbds--;
552                 }
553
554                 LASSERT (srv_ni->sni_nrqbds == 0);
555
556                 if (!PtlHandleEqual (srv_ni->sni_eq_h, PTL_HANDLE_NONE)) {
557                         rc = PtlEQFree(srv_ni->sni_eq_h);
558                         if (rc)
559                                 CERROR("%s.%d: PtlEQFree failed on %s: %d\n",
560                                        service->srv_name, i,
561                                        srv_ni->sni_ni->pni_name, rc);
562                 }
563         }
564
565         ptlrpc_lprocfs_unregister_service(service);
566
567         OBD_FREE(service,
568                  offsetof (struct ptlrpc_service,
569                            srv_interfaces[ptlrpc_ninterfaces]));
570         return 0;
571 }