Whamcloud - gitweb
- name switch: "light" --> "lite"
[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 EXPORT_SYMTAB
24
25 #include <linux/config.h>
26 #include <linux/module.h>
27 #include <linux/kernel.h>
28
29 #define DEBUG_SUBSYSTEM S_RPC
30
31 #include <linux/obd_support.h>
32 #include <linux/obd_class.h>
33 #include <linux/lustre_net.h>
34
35 extern int request_in_callback(ptl_event_t *ev, void *data);
36 extern int ptl_handled_rpc(struct ptlrpc_service *service, void *start);
37
38 static int ptlrpc_check_event(struct ptlrpc_service *svc)
39 {
40         int rc = 0;
41
42         spin_lock(&svc->srv_lock);
43         if (sigismember(&(current->pending.signal), SIGKILL) ||
44             sigismember(&(current->pending.signal), SIGTERM) ||
45             sigismember(&(current->pending.signal), SIGINT)) {
46                 svc->srv_flags |= SVC_KILLED;
47                 GOTO(out, rc = 1);
48         }
49
50         if (svc->srv_flags & SVC_STOPPING)
51                 GOTO(out, rc = 1);
52
53         if (svc->srv_flags & SVC_EVENT)
54                 LBUG();
55
56         if (ptl_is_valid_handle(&svc->srv_eq_h)) {
57                 int err;
58                 err = PtlEQGet(svc->srv_eq_h, &svc->srv_ev);
59
60                 if (err == PTL_OK) {
61                         svc->srv_flags |= SVC_EVENT;
62                         GOTO(out, rc = 1);
63                 }
64
65                 if (err != PTL_EQ_EMPTY) {
66                         CDEBUG(D_NET, "BUG: PtlEQGet returned %d\n", rc);
67                         LBUG();
68                 }
69
70                 GOTO(out, rc = 0);
71         }
72
73         if (!list_empty(&svc->srv_reqs)) {
74                 svc->srv_flags |= SVC_LIST;
75                 GOTO(out, rc = 1);
76         }
77
78         EXIT;
79  out:
80         spin_unlock(&svc->srv_lock);
81         return rc;
82 }
83
84 struct ptlrpc_service *
85 ptlrpc_init_svc(__u32 bufsize, int req_portal, int rep_portal, char *uuid,
86                 svc_handler_t handler)
87 {
88         int err;
89         int rc, i;
90         struct ptlrpc_service *service;
91
92         OBD_ALLOC(service, sizeof(*service));
93         if (!service) {
94                 LBUG();
95                 RETURN(NULL);
96         }
97
98         spin_lock_init(&service->srv_lock);
99         INIT_LIST_HEAD(&service->srv_reqs);
100         init_waitqueue_head(&service->srv_ctl_waitq);
101         init_waitqueue_head(&service->srv_waitq);
102
103         service->srv_thread = NULL;
104         service->srv_flags = 0;
105
106         service->srv_buf_size = bufsize;
107         service->srv_rep_portal = rep_portal;
108         service->srv_req_portal = req_portal;
109         service->srv_handler = handler;
110
111         err = kportal_uuid_to_peer(uuid, &service->srv_self);
112         if (err) {
113                 CERROR("cannot get peer for uuid %s", uuid);
114                 GOTO(err_free, NULL);
115         }
116
117         service->srv_ring_length = RPC_RING_LENGTH;
118         service->srv_id.nid = PTL_ID_ANY;
119         service->srv_id.pid = PTL_ID_ANY;
120
121         rc = PtlEQAlloc(service->srv_self.peer_ni, 128,
122                         request_in_callback,
123                         service, &(service->srv_eq_h));
124
125         if (rc != PTL_OK) {
126                 CERROR("PtlEQAlloc failed: %d\n", rc);
127                 LBUG();
128                 GOTO(err_free, NULL);
129         }
130
131         for (i = 0; i < service->srv_ring_length; i++) {
132                 OBD_ALLOC(service->srv_buf[i], service->srv_buf_size);
133                 if (service->srv_buf[i] == NULL) {
134                         CERROR("no memory\n");
135                         LBUG();
136                         GOTO(err_ring, NULL);
137                 }
138                 service->srv_ref_count[i] = 0;
139                 ptlrpc_link_svc_me(service, i);
140         }
141
142         CDEBUG(D_NET, "Starting service listening on portal %d\n",
143                service->srv_req_portal);
144
145         return service;
146 err_ring:
147         service->srv_ring_length = i;
148         rpc_unregister_service(service); // XXX verify this is right
149         PtlEQFree(service->srv_eq_h);
150 err_free:
151         OBD_FREE(service, sizeof(*service));
152         return NULL;
153 }
154
155 static int ptlrpc_main(void *arg)
156 {
157         int rc;
158         struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
159         struct obd_device *obddev = data->dev;
160         struct ptlrpc_service *svc = data->svc;
161
162         ENTRY;
163
164         lock_kernel();
165         daemonize();
166         spin_lock_irq(&current->sigmask_lock);
167         sigfillset(&current->blocked);
168         recalc_sigpending(current);
169         spin_unlock_irq(&current->sigmask_lock);
170
171         sprintf(current->comm, data->name);
172
173         /* Record that the  thread is running */
174         svc->srv_thread = current;
175         svc->srv_flags = SVC_RUNNING;
176         wake_up(&svc->srv_ctl_waitq);
177
178         /* XXX maintain a list of all managed devices: insert here */
179
180         /* And now, loop forever on requests */
181         while (1) {
182                 wait_event(svc->srv_waitq, ptlrpc_check_event(svc));
183
184                 spin_lock(&svc->srv_lock);
185                 if (svc->srv_flags & SVC_SIGNAL) {
186                         spin_unlock(&svc->srv_lock);
187                         EXIT;
188                         break;
189                 }
190
191                 if (svc->srv_flags & SVC_STOPPING) {
192                         spin_unlock(&svc->srv_lock);
193                         EXIT;
194                         break;
195                 }
196
197                 if (svc->srv_flags & SVC_EVENT) {
198                         struct ptlrpc_request request;
199                         void *start;
200                         svc->srv_flags = SVC_RUNNING;
201
202                         /* FIXME: If we move to an event-driven model,
203                          * we should put the request on the stack of
204                          * mds_handle instead. */
205                         start = svc->srv_ev.mem_desc.start;
206                         memset(&request, 0, sizeof(request));
207                         request.rq_obd = obddev;
208                         request.rq_reqbuf = (svc->srv_ev.mem_desc.start +
209                                              svc->srv_ev.offset);
210                         request.rq_reqlen = svc->srv_ev.mem_desc.length;
211                         request.rq_xid = svc->srv_ev.match_bits;
212                         CDEBUG(D_NET, "got req %d\n", request.rq_xid);
213
214                         request.rq_peer.peer_nid = svc->srv_ev.initiator.nid;
215                         /* FIXME: this NI should be the incoming NI.
216                          * We don't know how to find that from here. */
217                         request.rq_peer.peer_ni = svc->srv_self.peer_ni;
218                         svc->srv_flags &= ~SVC_EVENT;
219
220                         spin_unlock(&svc->srv_lock);
221                         rc = svc->srv_handler(obddev, svc, &request);
222                         ptl_handled_rpc(svc, start);
223                         continue;
224                 }
225
226                 if (svc->srv_flags & SVC_LIST) {
227                         struct ptlrpc_request *request;
228                         svc->srv_flags = SVC_RUNNING;
229
230                         request = list_entry(svc->srv_reqs.next,
231                                              struct ptlrpc_request,
232                                              rq_list);
233                         list_del(&request->rq_list);
234                         spin_unlock(&svc->srv_lock);
235                         rc = svc->srv_handler(obddev, svc, request);
236                         continue;
237                 }
238                 CERROR("unknown break in service");
239                 spin_unlock(&svc->srv_lock);
240                 EXIT;
241                 break;
242         }
243
244         svc->srv_thread = NULL;
245         svc->srv_flags = SVC_STOPPED;
246         wake_up(&svc->srv_ctl_waitq);
247         CDEBUG(D_NET, "svc exiting process %d\n", current->pid);
248         return 0;
249 }
250
251 void ptlrpc_stop_thread(struct ptlrpc_service *svc)
252 {
253         svc->srv_flags = SVC_STOPPING;
254
255         wake_up(&svc->srv_waitq);
256         wait_event_interruptible(svc->srv_ctl_waitq,
257                                  (svc->srv_flags & SVC_STOPPED));
258 }
259
260 int ptlrpc_start_thread(struct obd_device *dev, struct ptlrpc_service *svc,
261                                 char *name)
262 {
263         struct ptlrpc_svc_data d;
264         int rc;
265         ENTRY;
266
267         d.dev = dev;
268         d.svc = svc;
269         d.name = name;
270
271         init_waitqueue_head(&svc->srv_waitq);
272
273         init_waitqueue_head(&svc->srv_ctl_waitq);
274         rc = kernel_thread(ptlrpc_main, (void *) &d,
275                            CLONE_VM | CLONE_FS | CLONE_FILES);
276         if (rc < 0) {
277                 CERROR("cannot start thread\n");
278                 RETURN(-EINVAL);
279         }
280         wait_event(svc->srv_ctl_waitq, svc->srv_flags & SVC_RUNNING);
281
282         RETURN(0);
283 }
284
285 int rpc_unregister_service(struct ptlrpc_service *service)
286 {
287         int rc, i;
288
289         for (i = 0; i < service->srv_ring_length; i++) {
290                 if (ptl_is_valid_handle(&(service->srv_me_h[i]))) {
291                         rc = PtlMEUnlink(service->srv_me_h[i]);
292                         if (rc)
293                                 CERROR("PtlMEUnlink failed: %d\n", rc);
294                         ptl_set_inv_handle(&(service->srv_me_h[i]));
295                 }
296
297                 if (service->srv_buf[i] != NULL)
298                         OBD_FREE(service->srv_buf[i], service->srv_buf_size);
299                 service->srv_buf[i] = NULL;
300         }
301
302         rc = PtlEQFree(service->srv_eq_h);
303         if (rc)
304                 CERROR("PtlEQFree failed: %d\n", rc);
305
306         return 0;
307 }