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