Whamcloud - gitweb
Re-add memset() of request struct, since we re-use the same struct many times.
[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 = 0;
35         ENTRY;
36
37         spin_lock(&svc->srv_lock);
38         if (thread->t_flags & SVC_STOPPING)
39                 GOTO(out, rc = 1);
40
41         LASSERT ((thread->t_flags & SVC_EVENT) == 0);
42
43         if (ptl_is_valid_handle(&svc->srv_eq_h)) {
44                 int err;
45                 err = PtlEQGet(svc->srv_eq_h, event);
46
47                 if (err == PTL_OK) {
48                         thread->t_flags |= SVC_EVENT;
49                         GOTO(out, rc = 1);
50                 }
51
52                 if (err != PTL_EQ_EMPTY) {
53                         CERROR("BUG: PtlEQGet returned %d\n", rc);
54                         LBUG();
55                 }
56
57                 GOTO(out, rc = 0);
58         }
59
60         EXIT;
61  out:
62         spin_unlock(&svc->srv_lock);
63         return rc;
64 }
65
66 struct ptlrpc_service *
67 ptlrpc_init_svc(__u32 bufsize, int nbuffs, int req_portal, int rep_portal,
68                 obd_uuid_t 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                 LBUG();
78                 RETURN(NULL);
79         }
80
81         service->srv_name = name;
82         spin_lock_init(&service->srv_lock);
83         INIT_LIST_HEAD(&service->srv_reqs);
84         INIT_LIST_HEAD(&service->srv_threads);
85         init_waitqueue_head(&service->srv_waitq);
86
87         service->srv_buf_size = bufsize;
88         service->srv_nbuffs = nbuffs;
89         service->srv_rep_portal = rep_portal;
90         service->srv_req_portal = req_portal;
91         service->srv_handler = handler;
92
93         err = kportal_uuid_to_peer(uuid, &service->srv_self);
94         if (err) {
95                 CERROR("cannot get peer for uuid '%s'\n", uuid);
96                 OBD_FREE(service, sizeof(*service));
97                 RETURN(NULL);
98         }
99
100         /* NB We need exactly 1 event for each buffer we queue */
101         rc = PtlEQAlloc(service->srv_self.peer_ni, service->srv_nbuffs,
102                         request_in_callback, &(service->srv_eq_h));
103
104         if (rc != PTL_OK) {
105                 CERROR("PtlEQAlloc failed: %d\n", rc);
106                 LBUG();
107                 OBD_FREE(service, sizeof(*service));
108                 RETURN(NULL);
109         }
110
111         OBD_ALLOC(service->srv_rqbds, service->srv_nbuffs *
112                   sizeof(struct ptlrpc_request_buffer_desc));
113         if (service->srv_rqbds == NULL) {
114                 CERROR("no memory\n");
115                 LBUG();
116                 GOTO(failed, NULL);
117         }
118
119         for (i = 0; i < service->srv_nbuffs; i++) {
120                 struct ptlrpc_request_buffer_desc *rqbd =&service->srv_rqbds[i];
121
122                 rqbd->rqbd_service = service;
123                 ptl_set_inv_handle (&rqbd->rqbd_me_h);
124                 OBD_ALLOC(rqbd->rqbd_buffer, service->srv_buf_size);
125                 if (rqbd->rqbd_buffer == NULL) {
126                         CERROR("no memory\n");
127                         LBUG();
128                         GOTO(failed, NULL);
129                 }
130                 ptlrpc_link_svc_me(rqbd);
131         }
132
133         CDEBUG(D_NET, "Starting service listening on portal %d\n",
134                service->srv_req_portal);
135
136         RETURN(service);
137 failed:
138         ptlrpc_unregister_service(service);
139         return NULL;
140 }
141
142 static int handle_incoming_request(struct obd_device *obddev,
143                                    struct ptlrpc_service *svc,
144                                    ptl_event_t *event,
145                                    struct ptlrpc_request *request)
146 {
147         struct ptlrpc_request_buffer_desc *rqbd = event->mem_desc.user_ptr;
148         int rc;
149
150         /* FIXME: If we move to an event-driven model, we should put the request
151          * on the stack of mds_handle instead. */
152         LASSERT ((event->mem_desc.options & PTL_MD_IOV) == 0);
153         LASSERT (rqbd->rqbd_service == svc);
154         LASSERT (rqbd->rqbd_buffer == event->mem_desc.start);
155         LASSERT (event->offset == 0);
156
157         memset(request, 0, sizeof(*request));
158         request->rq_svc = svc;
159         request->rq_obd = obddev;
160         request->rq_xid = event->match_bits;
161         request->rq_reqmsg = event->mem_desc.start + event->offset;
162         request->rq_reqlen = event->mem_desc.length;
163
164         if (request->rq_reqlen < sizeof(struct lustre_msg)) {
165                 CERROR("incomplete request (%d): ptl %d from "LPX64" xid "LPD64"\n",
166                        request->rq_reqlen, svc->srv_req_portal,
167                        event->initiator.nid, request->rq_xid);
168                 spin_unlock(&svc->srv_lock);
169                 RETURN(-EINVAL);
170         }
171
172         if (NTOH__u32(request->rq_reqmsg->type) != PTL_RPC_MSG_REQUEST) {
173                 CERROR("wrong packet type received (type=%u)\n",
174                        request->rq_reqmsg->type);
175                 LBUG();
176                 spin_unlock(&svc->srv_lock);
177                 RETURN(-EINVAL);
178         }
179
180         if (request->rq_reqmsg->magic != PTLRPC_MSG_MAGIC) {
181                 CERROR("wrong lustre_msg magic %d: ptl %d from "LPX64" xid "LPD64"\n",
182                        request->rq_reqmsg->magic, svc->srv_req_portal,
183                        event->initiator.nid, request->rq_xid);
184                 spin_unlock(&svc->srv_lock);
185                 RETURN(-EINVAL);
186         }
187
188         if (request->rq_reqmsg->version != PTLRPC_MSG_VERSION) {
189                 CERROR("wrong lustre_msg version %d: ptl %d from "LPX64" xid "LPD64"\n",
190                        request->rq_reqmsg->version, svc->srv_req_portal,
191                        event->initiator.nid, request->rq_xid);
192                 spin_unlock(&svc->srv_lock);
193                 RETURN(-EINVAL);
194         }
195
196         CDEBUG(D_NET, "got req "LPD64"\n", request->rq_xid);
197
198         request->rq_peer.peer_nid = event->initiator.nid;
199         /* FIXME: this NI should be the incoming NI.
200          * We don't know how to find that from here. */
201         request->rq_peer.peer_ni = svc->srv_self.peer_ni;
202
203         request->rq_export = class_conn2export((struct lustre_handle *)
204                                                request->rq_reqmsg);
205
206         if (request->rq_export) {
207                 request->rq_connection = request->rq_export->exp_connection;
208                 ptlrpc_connection_addref(request->rq_connection);
209         }
210
211         spin_unlock(&svc->srv_lock);
212
213         rc = svc->srv_handler(request);
214         ptlrpc_put_connection(request->rq_connection);
215
216         ptlrpc_link_svc_me (rqbd);
217         return rc;
218 }
219
220 static int ptlrpc_main(void *arg)
221 {
222         int rc;
223         struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
224         struct obd_device *obddev = data->dev;
225         struct ptlrpc_service *svc = data->svc;
226         struct ptlrpc_thread *thread = data->thread;
227         struct ptlrpc_request *request;
228         ptl_event_t *event;
229
230         ENTRY;
231
232         lock_kernel();
233         daemonize();
234         spin_lock_irq(&current->sigmask_lock);
235         sigfillset(&current->blocked);
236         recalc_sigpending(current);
237         spin_unlock_irq(&current->sigmask_lock);
238
239         sprintf(current->comm, data->name);
240         unlock_kernel();
241
242         /* Record that the thread is running */
243         thread->t_flags = SVC_RUNNING;
244         wake_up(&thread->t_ctl_waitq);
245
246         /* XXX maintain a list of all managed devices: insert here */
247
248         OBD_ALLOC(event, sizeof(*event));
249         LASSERT(event);
250         OBD_ALLOC(request, sizeof(*request));
251         LASSERT(request);
252
253         /* And now, loop forever on requests */
254         while (1) {
255                 wait_event(svc->srv_waitq,
256                            ptlrpc_check_event(svc, thread, event));
257
258                 spin_lock(&svc->srv_lock);
259
260                 if (thread->t_flags & SVC_STOPPING) {
261                         thread->t_flags &= ~SVC_STOPPING;
262                         spin_unlock(&svc->srv_lock);
263                         EXIT;
264                         break;
265                 }
266
267                 if (thread->t_flags & SVC_EVENT) {
268                         LASSERT (event->sequence != 0);
269                         rc = handle_incoming_request(obddev, svc, event,
270                                                      request);
271                         thread->t_flags &= ~SVC_EVENT;
272                         continue;
273                 }
274
275                 CERROR("unknown break in service");
276                 spin_unlock(&svc->srv_lock);
277                 EXIT;
278                 break;
279         }
280
281         OBD_FREE(event, sizeof(*event));
282         OBD_FREE(request, sizeof(*request));
283
284         thread->t_flags = SVC_STOPPED;
285         wake_up(&thread->t_ctl_waitq);
286         CDEBUG(D_NET, "service thread exiting, process %d\n", current->pid);
287         return 0;
288 }
289
290 static void ptlrpc_stop_thread(struct ptlrpc_service *svc,
291                                struct ptlrpc_thread *thread)
292 {
293         spin_lock(&svc->srv_lock);
294         thread->t_flags = SVC_STOPPING;
295         spin_unlock(&svc->srv_lock);
296
297         wake_up(&svc->srv_waitq);
298         wait_event(thread->t_ctl_waitq, (thread->t_flags & SVC_STOPPED));
299 }
300
301 void ptlrpc_stop_all_threads(struct ptlrpc_service *svc)
302 {
303         spin_lock(&svc->srv_lock);
304         while (!list_empty(&svc->srv_threads)) {
305                 struct ptlrpc_thread *thread;
306                 thread = list_entry(svc->srv_threads.next, struct ptlrpc_thread,
307                                     t_link);
308                 spin_unlock(&svc->srv_lock);
309                 ptlrpc_stop_thread(svc, thread);
310                 spin_lock(&svc->srv_lock);
311                 list_del(&thread->t_link);
312                 OBD_FREE(thread, sizeof(*thread));
313         }
314         spin_unlock(&svc->srv_lock);
315 }
316
317 int ptlrpc_start_thread(struct obd_device *dev, struct ptlrpc_service *svc,
318                         char *name)
319 {
320         struct ptlrpc_svc_data d;
321         struct ptlrpc_thread *thread;
322         int rc;
323         ENTRY;
324
325         OBD_ALLOC(thread, sizeof(*thread));
326         if (thread == NULL) {
327                 LBUG();
328                 RETURN(-ENOMEM);
329         }
330         init_waitqueue_head(&thread->t_ctl_waitq);
331
332         d.dev = dev;
333         d.svc = svc;
334         d.name = name;
335         d.thread = thread;
336
337         spin_lock(&svc->srv_lock);
338         list_add(&thread->t_link, &svc->srv_threads);
339         spin_unlock(&svc->srv_lock);
340
341         rc = kernel_thread(ptlrpc_main, (void *) &d,
342                            CLONE_VM | CLONE_FS | CLONE_FILES);
343         if (rc < 0) {
344                 CERROR("cannot start thread\n");
345                 OBD_FREE(thread, sizeof(*thread));
346                 RETURN(-EINVAL);
347         }
348         wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_RUNNING);
349
350         RETURN(0);
351 }
352
353 int ptlrpc_unregister_service(struct ptlrpc_service *service)
354 {
355         int rc, i;
356
357         /* NB service->srv_nbuffs gets set before we attempt (and possibly
358          * fail) to allocate srv_rqbds.
359          */
360         if (service->srv_rqbds != NULL) {
361                 for (i = 0; i < service->srv_nbuffs; i++) {
362                         struct ptlrpc_request_buffer_desc *rqbd =
363                                 &service->srv_rqbds[i];
364
365                         if (rqbd->rqbd_buffer == NULL) /* no buffer allocated */
366                                 continue;             /* => never initialised */
367
368                         /* Buffer allocated => got linked */
369                         LASSERT (ptl_is_valid_handle (&rqbd->rqbd_me_h));
370
371                         rc = PtlMEUnlink(rqbd->rqbd_me_h);
372                         if (rc)
373                                 CERROR("PtlMEUnlink failed: %d\n", rc);
374
375                         OBD_FREE(rqbd->rqbd_buffer, service->srv_buf_size);
376                 }
377
378                 OBD_FREE(service->srv_rqbds, service->srv_nbuffs *
379                          sizeof (struct ptlrpc_request_buffer_desc));
380         }
381
382         rc = PtlEQFree(service->srv_eq_h);
383         if (rc)
384                 CERROR("PtlEQFree failed: %d\n", rc);
385
386         if (!list_empty(&service->srv_reqs)) {
387                 // XXX reply with errors and clean up
388                 CERROR("Request list not empty!\n");
389                 rc = -EBUSY;
390         }
391
392         OBD_FREE(service, sizeof(*service));
393         if (rc)
394                 LBUG();
395         return rc;
396 }