Whamcloud - gitweb
Merge b_md into HEAD
[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
25 #include <linux/obd_support.h>
26 #include <linux/obd_class.h>
27 #include <linux/lustre_net.h>
28
29 extern int request_in_callback(ptl_event_t *ev);
30
31 static int ptlrpc_check_event(struct ptlrpc_service *svc,
32                               struct ptlrpc_thread *thread, ptl_event_t *event)
33 {
34         int rc;
35         ENTRY;
36
37         spin_lock(&svc->srv_lock);
38
39         if (thread->t_flags & SVC_STOPPING)
40                 GOTO(out, rc = 1);
41
42         LASSERT ((thread->t_flags & SVC_EVENT) == 0);
43         LASSERT (ptl_is_valid_handle (&svc->srv_eq_h));
44
45         rc = PtlEQGet(svc->srv_eq_h, event);
46         switch (rc)
47         {
48         case PTL_OK:
49                 thread->t_flags |= SVC_EVENT;
50                 GOTO(out, rc = 1);
51
52         case PTL_EQ_EMPTY:
53                 GOTO(out, rc = 0);
54
55         default:
56                 CERROR("BUG: PtlEQGet returned %d\n", rc);
57                 LBUG();
58         }
59  out:
60         spin_unlock(&svc->srv_lock);
61         return rc;
62 }
63
64 struct ptlrpc_service *
65 ptlrpc_init_svc(__u32 nevents, __u32 nbufs,
66                 __u32 bufsize, __u32 max_req_size,
67                 int req_portal, int rep_portal,
68                 struct obd_uuid *uuid, svc_handler_t handler, char *name)
69 {
70         int err;
71         int rc, i;
72         struct ptlrpc_service *service;
73         ENTRY;
74
75         OBD_ALLOC(service, sizeof(*service));
76         if (!service)
77                 RETURN(NULL);
78
79         service->srv_name = name;
80         spin_lock_init(&service->srv_lock);
81         INIT_LIST_HEAD(&service->srv_threads);
82         init_waitqueue_head(&service->srv_waitq);
83
84         service->srv_max_req_size = max_req_size;
85         service->srv_buf_size = bufsize;
86         INIT_LIST_HEAD(&service->srv_rqbds);
87         service->srv_nrqbds = 0;
88         atomic_set(&service->srv_nrqbds_receiving, 0);
89
90         service->srv_rep_portal = rep_portal;
91         service->srv_req_portal = req_portal;
92         service->srv_handler = handler;
93
94         err = kportal_uuid_to_peer(uuid->uuid, &service->srv_self);
95         if (err) {
96                 CERROR("%s: cannot get peer for uuid '%s'\n", name, 
97                        uuid->uuid);
98                 OBD_FREE(service, sizeof(*service));
99                 RETURN(NULL);
100         }
101
102         rc = PtlEQAlloc(service->srv_self.peer_ni, nevents,
103                         request_in_callback, &(service->srv_eq_h));
104
105         if (rc != PTL_OK) {
106                 CERROR("%s: PtlEQAlloc failed: %d\n", name, rc);
107                 OBD_FREE(service, sizeof(*service));
108                 RETURN(NULL);
109         }
110
111         for (i = 0; i < nbufs; i++) {
112                 struct ptlrpc_request_buffer_desc *rqbd;
113
114                 OBD_ALLOC(rqbd, sizeof(*rqbd));
115                 if (rqbd == NULL)
116                         GOTO(failed, NULL);
117
118                 rqbd->rqbd_service = service;
119                 ptl_set_inv_handle(&rqbd->rqbd_me_h);
120                 atomic_set(&rqbd->rqbd_refcount, 0);
121                 OBD_ALLOC(rqbd->rqbd_buffer, service->srv_buf_size);
122                 if (rqbd->rqbd_buffer == NULL) {
123                         OBD_FREE(rqbd, sizeof(*rqbd));
124                         GOTO(failed, NULL);
125                 }
126                 list_add(&rqbd->rqbd_list, &service->srv_rqbds);
127                 service->srv_nrqbds++;
128
129                 ptlrpc_link_svc_me(rqbd);
130         }
131
132         CDEBUG(D_NET, "Starting service listening on portal %d (eq: %lu)\n",
133                service->srv_req_portal, service->srv_eq_h.handle_idx);
134
135         RETURN(service);
136 failed:
137         ptlrpc_unregister_service(service);
138         return NULL;
139 }
140
141 static int handle_incoming_request(struct obd_device *obddev,
142                                    struct ptlrpc_service *svc,
143                                    ptl_event_t *event,
144                                    struct ptlrpc_request *request)
145 {
146         struct ptlrpc_request_buffer_desc *rqbd = event->mem_desc.user_ptr;
147         int rc;
148
149         /* FIXME: If we move to an event-driven model, we should put the request
150          * on the stack of mds_handle instead. */
151
152         LASSERT (atomic_read (&rqbd->rqbd_refcount) > 0);
153         LASSERT ((event->mem_desc.options & PTL_MD_IOV) == 0);
154         LASSERT (rqbd->rqbd_service == svc);
155         LASSERT (rqbd->rqbd_buffer == event->mem_desc.start);
156         LASSERT (event->offset + event->mlength <= svc->srv_buf_size);
157
158         memset(request, 0, sizeof(*request));
159         request->rq_svc = svc;
160         request->rq_obd = obddev;
161         request->rq_xid = event->match_bits;
162         request->rq_reqmsg = event->mem_desc.start + event->offset;
163         request->rq_reqlen = event->mlength;
164
165         rc = -EINVAL;
166
167         if (request->rq_reqlen < sizeof(struct lustre_msg)) {
168                 CERROR("incomplete request (%d): ptl %d from "LPX64" xid "
169                        LPU64"\n",
170                        request->rq_reqlen, svc->srv_req_portal,
171                        event->initiator.nid, request->rq_xid);
172                 goto out;
173         }
174
175         CDEBUG(D_RPCTRACE, "Handling RPC pid:xid:nid:opc %d:"LPU64":"LPX64":%d\n",
176                NTOH__u32(request->rq_reqmsg->status),
177                request->rq_xid,
178                event->initiator.nid,
179                NTOH__u32(request->rq_reqmsg->opc));
180
181         if (NTOH__u32(request->rq_reqmsg->type) != PTL_RPC_MSG_REQUEST) {
182                 CERROR("wrong packet type received (type=%u)\n",
183                        request->rq_reqmsg->type);
184                 goto out;
185         }
186
187         if (request->rq_reqmsg->magic != PTLRPC_MSG_MAGIC) {
188                 CERROR("wrong lustre_msg magic %d: ptl %d from "LPX64" xid "
189                        LPD64"\n",
190                        request->rq_reqmsg->magic, svc->srv_req_portal,
191                        event->initiator.nid, request->rq_xid);
192                 goto out;
193         }
194
195         if (request->rq_reqmsg->version != PTLRPC_MSG_VERSION) {
196                 CERROR("wrong lustre_msg version %d: ptl %d from "LPX64" xid "
197                        LPD64"\n",
198                        request->rq_reqmsg->version, svc->srv_req_portal,
199                        event->initiator.nid, request->rq_xid);
200                 goto out;
201         }
202
203         CDEBUG(D_NET, "got req "LPD64" (md: %p + %d)\n", request->rq_xid,
204                event->mem_desc.start, event->offset);
205
206         request->rq_peer.peer_nid = event->initiator.nid;
207         /* FIXME: this NI should be the incoming NI.
208          * We don't know how to find that from here. */
209         request->rq_peer.peer_ni = svc->srv_self.peer_ni;
210
211         request->rq_export = class_conn2export((struct lustre_handle *)
212                                                request->rq_reqmsg);
213
214         if (request->rq_export) {
215                 request->rq_connection = request->rq_export->exp_connection;
216                 ptlrpc_connection_addref(request->rq_connection);
217         } else {
218                 /* create a (hopefully temporary) connection that will be used
219                  * to send the reply if this call doesn't create an export.
220                  * XXX revisit this when we revamp ptlrpc */
221                 request->rq_connection =
222                         ptlrpc_get_connection(&request->rq_peer, NULL);
223         }
224
225         rc = svc->srv_handler(request);
226         ptlrpc_put_connection(request->rq_connection);
227
228  out:
229         if (atomic_dec_and_test (&rqbd->rqbd_refcount)) /* last reference? */
230                 ptlrpc_link_svc_me (rqbd);
231
232         return rc;
233 }
234
235 /* Don't use daemonize, it removes fs struct from new thread  (bug 418) */
236 static void ptlrpc_daemonize(void)
237 {
238         exit_mm(current);
239
240         current->session = 1;
241         current->pgrp = 1;
242         current->tty = NULL;
243
244         exit_files(current);
245         reparent_to_init();
246 }
247
248 static int ptlrpc_main(void *arg)
249 {
250         struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
251         struct obd_device *obddev = data->dev;
252         struct ptlrpc_service *svc = data->svc;
253         struct ptlrpc_thread *thread = data->thread;
254         struct ptlrpc_request *request;
255         ptl_event_t *event;
256         int rc = 0;
257         unsigned long flags;
258         ENTRY;
259
260         lock_kernel();
261         ptlrpc_daemonize();
262
263 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
264         sigfillset(&current->blocked);
265         recalc_sigpending();
266 #else
267         spin_lock_irqsave(&current->sigmask_lock, flags);
268         sigfillset(&current->blocked);
269         recalc_sigpending(current);
270         spin_unlock_irqrestore(&current->sigmask_lock, flags);
271 #endif
272
273 #ifdef __arch_um__
274         sprintf(current->comm, "%s|%d", data->name,current->thread.extern_pid);
275 #else
276         strcpy(current->comm, data->name);
277 #endif
278         unlock_kernel();
279
280         OBD_ALLOC(event, sizeof(*event));
281         if (!event)
282                 GOTO(out, rc = -ENOMEM);
283         OBD_ALLOC(request, sizeof(*request));
284         if (!request)
285                 GOTO(out_event, rc = -ENOMEM);
286
287         /* Record that the thread is running */
288         thread->t_flags = SVC_RUNNING;
289         wake_up(&thread->t_ctl_waitq);
290
291         /* XXX maintain a list of all managed devices: insert here */
292
293         /* And now, loop forever on requests */
294         while (1) {
295                 wait_event(svc->srv_waitq,
296                            ptlrpc_check_event(svc, thread, event));
297
298                 if (thread->t_flags & SVC_STOPPING) {
299                         spin_lock(&svc->srv_lock);
300                         thread->t_flags &= ~SVC_STOPPING;
301                         spin_unlock(&svc->srv_lock);
302
303                         EXIT;
304                         break;
305                 }
306
307                 if (thread->t_flags & SVC_EVENT) {
308                         spin_lock(&svc->srv_lock);
309                         thread->t_flags &= ~SVC_EVENT;
310                         spin_unlock(&svc->srv_lock);
311
312                         rc = handle_incoming_request(obddev, svc, event,
313                                                      request);
314                         continue;
315                 }
316
317                 CERROR("unknown break in service");
318                 LBUG();
319                 EXIT;
320                 break;
321         }
322
323         OBD_FREE(request, sizeof(*request));
324 out_event:
325         OBD_FREE(event, sizeof(*event));
326 out:
327         thread->t_flags = SVC_STOPPED;
328         wake_up(&thread->t_ctl_waitq);
329
330         CDEBUG(D_NET, "service thread exiting, process %d: rc = %d\n",
331                current->pid, rc);
332         return rc;
333 }
334
335 static void ptlrpc_stop_thread(struct ptlrpc_service *svc,
336                                struct ptlrpc_thread *thread)
337 {
338         spin_lock(&svc->srv_lock);
339         thread->t_flags = SVC_STOPPING;
340         spin_unlock(&svc->srv_lock);
341
342         wake_up(&svc->srv_waitq);
343         wait_event(thread->t_ctl_waitq, (thread->t_flags & SVC_STOPPED));
344 }
345
346 void ptlrpc_stop_all_threads(struct ptlrpc_service *svc)
347 {
348         spin_lock(&svc->srv_lock);
349         while (!list_empty(&svc->srv_threads)) {
350                 struct ptlrpc_thread *thread;
351                 thread = list_entry(svc->srv_threads.next, struct ptlrpc_thread,
352                                     t_link);
353                 spin_unlock(&svc->srv_lock);
354                 ptlrpc_stop_thread(svc, thread);
355                 spin_lock(&svc->srv_lock);
356                 list_del(&thread->t_link);
357                 OBD_FREE(thread, sizeof(*thread));
358         }
359         spin_unlock(&svc->srv_lock);
360 }
361
362 int ptlrpc_start_thread(struct obd_device *dev, struct ptlrpc_service *svc,
363                         char *name)
364 {
365         struct ptlrpc_svc_data d;
366         struct ptlrpc_thread *thread;
367         int rc;
368         ENTRY;
369
370         OBD_ALLOC(thread, sizeof(*thread));
371         if (thread == NULL) {
372                 LBUG();
373                 RETURN(-ENOMEM);
374         }
375         init_waitqueue_head(&thread->t_ctl_waitq);
376
377         d.dev = dev;
378         d.svc = svc;
379         d.name = name;
380         d.thread = thread;
381
382         spin_lock(&svc->srv_lock);
383         list_add(&thread->t_link, &svc->srv_threads);
384         spin_unlock(&svc->srv_lock);
385
386         /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
387          * just drop the VM and FILES in ptlrpc_daemonize() right away.
388          */
389         rc = kernel_thread(ptlrpc_main, (void *) &d, CLONE_VM | CLONE_FILES);
390         if (rc < 0) {
391                 CERROR("cannot start thread\n");
392                 OBD_FREE(thread, sizeof(*thread));
393                 RETURN(rc);
394         }
395         wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_RUNNING);
396
397         RETURN(0);
398 }
399
400 int ptlrpc_unregister_service(struct ptlrpc_service *service)
401 {
402         int rc;
403
404         LASSERT (list_empty (&service->srv_threads));
405
406         /* XXX We could reply (with failure) to all buffered requests
407          * _after_ unlinking _all_ the request buffers, but _before_
408          * freeing them.
409          */
410
411         while (!list_empty (&service->srv_rqbds)) {
412                 struct ptlrpc_request_buffer_desc *rqbd =
413                         list_entry (service->srv_rqbds.next,
414                                     struct ptlrpc_request_buffer_desc,
415                                     rqbd_list);
416
417                 list_del (&rqbd->rqbd_list);
418
419                 LASSERT (atomic_read (&rqbd->rqbd_refcount) > 0);
420                 /* refcount could be anything; it's possible for the
421                  * buffers to continued to get filled after all the server
422                  * threads exited.  But we know they _have_ exited.
423                  */
424
425                 (void) PtlMEUnlink(rqbd->rqbd_me_h);
426                 /* The callback handler could have unlinked this ME already
427                  * (we're racing with her) but it's safe to ensure it _has_
428                  * been unlinked.
429                  */
430
431                 OBD_FREE (rqbd->rqbd_buffer, service->srv_buf_size);
432                 OBD_FREE (rqbd, sizeof (*rqbd));
433                 service->srv_nrqbds--;
434         }
435
436         LASSERT (service->srv_nrqbds == 0);
437
438         rc = PtlEQFree(service->srv_eq_h);
439         if (rc)
440                 CERROR("PtlEQFree failed: %d\n", rc);
441
442         OBD_FREE(service, sizeof(*service));
443         if (rc)
444                 LBUG();
445         return rc;
446 }