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