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