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