Whamcloud - gitweb
Don't LBUG on OOM in setup.
[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", err);
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 nevents, __u32 nbufs,
68                 __u32 bufsize, __u32 max_req_size,
69                 int req_portal, int rep_portal,
70                 obd_uuid_t uuid, svc_handler_t handler, char *name)
71 {
72         int err;
73         int rc, i;
74         struct ptlrpc_service *service;
75         ENTRY;
76
77         OBD_ALLOC(service, sizeof(*service));
78         if (!service)
79                 RETURN(NULL);
80
81         service->srv_name = name;
82         spin_lock_init(&service->srv_lock);
83         INIT_LIST_HEAD(&service->srv_threads);
84         init_waitqueue_head(&service->srv_waitq);
85
86         service->srv_max_req_size = max_req_size;
87         service->srv_buf_size = bufsize;
88         INIT_LIST_HEAD(&service->srv_rqbds);
89         service->srv_nrqbds = 0;
90         atomic_set(&service->srv_nrqbds_receiving, 0);
91
92         service->srv_rep_portal = rep_portal;
93         service->srv_req_portal = req_portal;
94         service->srv_handler = handler;
95
96         err = kportal_uuid_to_peer(uuid, &service->srv_self);
97         if (err) {
98                 CERROR("%s: cannot get peer for uuid '%s'\n", name, uuid);
99                 OBD_FREE(service, sizeof(*service));
100                 RETURN(NULL);
101         }
102
103         rc = PtlEQAlloc(service->srv_self.peer_ni, nevents,
104                         request_in_callback, &(service->srv_eq_h));
105
106         if (rc != PTL_OK) {
107                 CERROR("%s: PtlEQAlloc failed: %d\n", name, rc);
108                 OBD_FREE(service, sizeof(*service));
109                 RETURN(NULL);
110         }
111
112         for (i = 0; i < nbufs; i++) {
113                 struct ptlrpc_request_buffer_desc *rqbd;
114
115                 OBD_ALLOC(rqbd, sizeof(*rqbd));
116                 if (rqbd == NULL)
117                         GOTO(failed, NULL);
118
119                 rqbd->rqbd_service = service;
120                 ptl_set_inv_handle(&rqbd->rqbd_me_h);
121                 atomic_set(&rqbd->rqbd_refcount, 0);
122                 OBD_ALLOC(rqbd->rqbd_buffer, service->srv_buf_size);
123                 if (rqbd->rqbd_buffer == NULL) {
124                         OBD_FREE(rqbd, sizeof(*rqbd));
125                         GOTO(failed, NULL);
126                 }
127                 list_add(&rqbd->rqbd_list, &service->srv_rqbds);
128                 service->srv_nrqbds++;
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
153         LASSERT (atomic_read (&rqbd->rqbd_refcount) > 0);
154         LASSERT ((event->mem_desc.options & PTL_MD_IOV) == 0);
155         LASSERT (rqbd->rqbd_service == svc);
156         LASSERT (rqbd->rqbd_buffer == event->mem_desc.start);
157         LASSERT (event->offset + event->mlength <= svc->srv_buf_size);
158
159         memset(request, 0, sizeof(*request));
160         request->rq_svc = svc;
161         request->rq_obd = obddev;
162         request->rq_xid = event->match_bits;
163         request->rq_reqmsg = event->mem_desc.start + event->offset;
164         request->rq_reqlen = event->mlength;
165
166         rc = -EINVAL;
167
168         if (request->rq_reqlen < sizeof(struct lustre_msg)) {
169                 CERROR("incomplete request (%d): ptl %d from "LPX64" xid "
170                        LPD64"\n",
171                        request->rq_reqlen, svc->srv_req_portal,
172                        event->initiator.nid, request->rq_xid);
173                 goto out;
174         }
175
176         if (NTOH__u32(request->rq_reqmsg->type) != PTL_RPC_MSG_REQUEST) {
177                 CERROR("wrong packet type received (type=%u)\n",
178                        request->rq_reqmsg->type);
179                 goto out;
180         }
181
182         if (request->rq_reqmsg->magic != PTLRPC_MSG_MAGIC) {
183                 CERROR("wrong lustre_msg magic %d: ptl %d from "LPX64" xid "
184                        LPD64"\n",
185                        request->rq_reqmsg->magic, svc->srv_req_portal,
186                        event->initiator.nid, request->rq_xid);
187                 goto out;
188         }
189
190         if (request->rq_reqmsg->version != PTLRPC_MSG_VERSION) {
191                 CERROR("wrong lustre_msg version %d: ptl %d from "LPX64" xid "
192                        LPD64"\n",
193                        request->rq_reqmsg->version, svc->srv_req_portal,
194                        event->initiator.nid, request->rq_xid);
195                 goto out;
196         }
197
198         CDEBUG(D_NET, "got req "LPD64"\n", request->rq_xid);
199
200         request->rq_peer.peer_nid = event->initiator.nid;
201         /* FIXME: this NI should be the incoming NI.
202          * We don't know how to find that from here. */
203         request->rq_peer.peer_ni = svc->srv_self.peer_ni;
204
205         request->rq_export = class_conn2export((struct lustre_handle *)
206                                                request->rq_reqmsg);
207
208         if (request->rq_export) {
209                 request->rq_connection = request->rq_export->exp_connection;
210                 ptlrpc_connection_addref(request->rq_connection);
211         } else {
212                 /* create a (hopefully temporary) connection that will be used
213                  * to send the reply if this call doesn't create an export.
214                  * XXX revisit this when we revamp ptlrpc */
215                 request->rq_connection =
216                         ptlrpc_get_connection(&request->rq_peer, NULL);
217         }
218
219         rc = svc->srv_handler(request);
220         ptlrpc_put_connection(request->rq_connection);
221
222  out:
223         if (atomic_dec_and_test (&rqbd->rqbd_refcount)) /* last reference? */
224                 ptlrpc_link_svc_me (rqbd);
225
226         return rc;
227 }
228
229 static int ptlrpc_main(void *arg)
230 {
231         struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
232         struct obd_device *obddev = data->dev;
233         struct ptlrpc_service *svc = data->svc;
234         struct ptlrpc_thread *thread = data->thread;
235         struct ptlrpc_request *request;
236         ptl_event_t *event;
237         int rc = 0;
238
239         ENTRY;
240
241         lock_kernel();
242         daemonize();
243         spin_lock_irq(&current->sigmask_lock);
244         sigfillset(&current->blocked);
245         recalc_sigpending(current);
246         spin_unlock_irq(&current->sigmask_lock);
247
248         strcpy(current->comm, data->name);
249         unlock_kernel();
250
251         OBD_ALLOC(event, sizeof(*event));
252         if (!event)
253                 GOTO(out, rc = -ENOMEM);
254         OBD_ALLOC(request, sizeof(*request));
255         if (!request)
256                 GOTO(out_event, rc = -ENOMEM);
257
258         /* Record that the thread is running */
259         thread->t_flags = SVC_RUNNING;
260         wake_up(&thread->t_ctl_waitq);
261
262         /* XXX maintain a list of all managed devices: insert here */
263
264         /* And now, loop forever on requests */
265         while (1) {
266                 wait_event(svc->srv_waitq,
267                            ptlrpc_check_event(svc, thread, event));
268
269                 if (thread->t_flags & SVC_STOPPING) {
270                         spin_lock(&svc->srv_lock);
271                         thread->t_flags &= ~SVC_STOPPING;
272                         spin_unlock(&svc->srv_lock);
273
274                         EXIT;
275                         break;
276                 }
277
278                 if (thread->t_flags & SVC_EVENT) {
279                         spin_lock(&svc->srv_lock);
280                         thread->t_flags &= ~SVC_EVENT;
281                         spin_unlock(&svc->srv_lock);
282
283                         rc = handle_incoming_request(obddev, svc, event,
284                                                      request);
285                         continue;
286                 }
287
288                 CERROR("unknown break in service");
289                 LBUG();
290                 EXIT;
291                 break;
292         }
293
294         OBD_FREE(request, sizeof(*request));
295 out_event:
296         OBD_FREE(event, sizeof(*event));
297 out:
298         thread->t_flags = SVC_STOPPED;
299         wake_up(&thread->t_ctl_waitq);
300
301         CDEBUG(D_NET, "service thread exiting, process %d: rc = %d\n",
302                current->pid, rc);
303         return rc;
304 }
305
306 static void ptlrpc_stop_thread(struct ptlrpc_service *svc,
307                                struct ptlrpc_thread *thread)
308 {
309         spin_lock(&svc->srv_lock);
310         thread->t_flags = SVC_STOPPING;
311         spin_unlock(&svc->srv_lock);
312
313         wake_up(&svc->srv_waitq);
314         wait_event(thread->t_ctl_waitq, (thread->t_flags & SVC_STOPPED));
315 }
316
317 void ptlrpc_stop_all_threads(struct ptlrpc_service *svc)
318 {
319         spin_lock(&svc->srv_lock);
320         while (!list_empty(&svc->srv_threads)) {
321                 struct ptlrpc_thread *thread;
322                 thread = list_entry(svc->srv_threads.next, struct ptlrpc_thread,
323                                     t_link);
324                 spin_unlock(&svc->srv_lock);
325                 ptlrpc_stop_thread(svc, thread);
326                 spin_lock(&svc->srv_lock);
327                 list_del(&thread->t_link);
328                 OBD_FREE(thread, sizeof(*thread));
329         }
330         spin_unlock(&svc->srv_lock);
331 }
332
333 int ptlrpc_start_thread(struct obd_device *dev, struct ptlrpc_service *svc,
334                         char *name)
335 {
336         struct ptlrpc_svc_data d;
337         struct ptlrpc_thread *thread;
338         int rc;
339         ENTRY;
340
341         OBD_ALLOC(thread, sizeof(*thread));
342         if (thread == NULL) {
343                 LBUG();
344                 RETURN(-ENOMEM);
345         }
346         init_waitqueue_head(&thread->t_ctl_waitq);
347
348         d.dev = dev;
349         d.svc = svc;
350         d.name = name;
351         d.thread = thread;
352
353         spin_lock(&svc->srv_lock);
354         list_add(&thread->t_link, &svc->srv_threads);
355         spin_unlock(&svc->srv_lock);
356
357         /* XXX should we really be cloning open file handles here? */
358         rc = kernel_thread(ptlrpc_main, (void *) &d,
359                            CLONE_VM | CLONE_FS | CLONE_FILES);
360         if (rc < 0) {
361                 CERROR("cannot start thread\n");
362                 OBD_FREE(thread, sizeof(*thread));
363                 RETURN(rc);
364         }
365         wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_RUNNING);
366
367         RETURN(0);
368 }
369
370 int ptlrpc_unregister_service(struct ptlrpc_service *service)
371 {
372         int rc;
373
374         LASSERT (list_empty (&service->srv_threads));
375
376         /* XXX We could reply (with failure) to all buffered requests
377          * _after_ unlinking _all_ the request buffers, but _before_
378          * freeing them.
379          */
380
381         while (!list_empty (&service->srv_rqbds)) {
382                 struct ptlrpc_request_buffer_desc *rqbd =
383                         list_entry (service->srv_rqbds.next,
384                                     struct ptlrpc_request_buffer_desc,
385                                     rqbd_list);
386
387                 list_del (&rqbd->rqbd_list);
388
389                 LASSERT (atomic_read (&rqbd->rqbd_refcount) > 0);
390                 /* refcount could be anything; it's possible for the
391                  * buffers to continued to get filled after all the server
392                  * threads exited.  But we know they _have_ exited.
393                  */
394
395                 (void) PtlMEUnlink(rqbd->rqbd_me_h);
396                 /* The callback handler could have unlinked this ME already
397                  * (we're racing with her) but it's safe to ensure it _has_
398                  * been unlinked.
399                  */
400
401                 OBD_FREE (rqbd->rqbd_buffer, service->srv_buf_size);
402                 OBD_FREE (rqbd, sizeof (*rqbd));
403                 service->srv_nrqbds--;
404         }
405
406         LASSERT (service->srv_nrqbds == 0);
407
408         rc = PtlEQFree(service->srv_eq_h);
409         if (rc)
410                 CERROR("PtlEQFree failed: %d\n", rc);
411
412         OBD_FREE(service, sizeof(*service));
413         if (rc)
414                 LBUG();
415         return rc;
416 }