Whamcloud - gitweb
- do the right thing when we catch SIGTERM
[fs/lustre-release.git] / lustre / ptlrpc / client.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 int ptlrpc_enqueue(struct ptlrpc_client *peer, struct ptlrpc_request *req)
36 {
37         struct ptlrpc_request *srv_req;
38         
39         if (!peer->cli_obd) { 
40                 EXIT;
41                 return -1;
42         }
43
44         OBD_ALLOC(srv_req, sizeof(*srv_req));
45         if (!srv_req) { 
46                 EXIT;
47                 return -ENOMEM;
48         }
49
50         CDEBUG(0, "peer obd minor %d, incoming req %p, srv_req %p\n",
51                peer->cli_obd->obd_minor, req, srv_req);
52
53         memset(srv_req, 0, sizeof(*req)); 
54
55         /* move the request buffer */
56         srv_req->rq_reqbuf = req->rq_reqbuf;
57         srv_req->rq_reqlen = req->rq_reqlen;
58         srv_req->rq_obd = peer->cli_obd;
59
60         /* remember where it came from */
61         srv_req->rq_reply_handle = req;
62
63         spin_lock(&peer->cli_lock);
64         list_add(&srv_req->rq_list, &peer->cli_obd->obd_req_list); 
65         spin_unlock(&peer->cli_lock);
66         wake_up(&peer->cli_obd->obd_req_waitq);
67         return 0;
68 }
69
70 int ptlrpc_connect_client(int dev, char *uuid, int req_portal, int rep_portal, 
71                           req_pack_t req_pack, rep_unpack_t rep_unpack,
72                           struct ptlrpc_client *cl)
73 {
74         int err; 
75
76         memset(cl, 0, sizeof(*cl));
77         spin_lock_init(&cl->cli_lock);
78         cl->cli_xid = 1;
79         cl->cli_obd = NULL; 
80         cl->cli_request_portal = req_portal;
81         cl->cli_reply_portal = rep_portal;
82         cl->cli_rep_unpack = rep_unpack;
83         cl->cli_req_pack = req_pack;
84
85         /* non networked client */
86         if (dev >= 0 && dev < MAX_OBD_DEVICES) {
87                 struct obd_device *obd = &obd_dev[dev];
88                 
89                 if ((!obd->obd_flags & OBD_ATTACHED) ||
90                     (!obd->obd_flags & OBD_SET_UP)) { 
91                         CERROR("target device %d not att or setup\n", dev);
92                         return -EINVAL;
93                 }
94                 if (strcmp(obd->obd_type->typ_name, "ost") && 
95                     strcmp(obd->obd_type->typ_name, "mds")) { 
96                         return -EINVAL;
97                 }
98
99                 cl->cli_obd = &obd_dev[dev];
100                 return 0;
101         }
102
103         /* networked */
104         err = kportal_uuid_to_peer(uuid, &cl->cli_server);
105         if (err != 0) { 
106                 CERROR("cannot find peer %s!", uuid); 
107         }
108
109         return err;
110 }
111
112 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk(struct lustre_peer *peer)
113 {
114         struct ptlrpc_bulk_desc *bulk;
115
116         OBD_ALLOC(bulk, sizeof(*bulk));
117         if (bulk != NULL) {
118                 memset(bulk, 0, sizeof(*bulk));
119                 memcpy(&bulk->b_peer, peer, sizeof(*peer));
120                 init_waitqueue_head(&bulk->b_waitq);
121         }
122
123         return bulk;
124 }
125
126 struct ptlrpc_request *ptlrpc_prep_req(struct ptlrpc_client *cl, 
127                                        int opcode, int namelen, char *name,
128                                        int tgtlen, char *tgt)
129 {
130         struct ptlrpc_request *request;
131         int rc;
132         ENTRY; 
133
134         OBD_ALLOC(request, sizeof(*request));
135         if (!request) { 
136                 CERROR("request allocation out of memory\n");
137                 return NULL;
138         }
139
140         memset(request, 0, sizeof(*request));
141         //spin_lock_init(&request->rq_lock);
142
143         spin_lock(&cl->cli_lock);
144         request->rq_xid = cl->cli_xid++;
145         spin_unlock(&cl->cli_lock);
146
147         rc = cl->cli_req_pack(name, namelen, tgt, tgtlen,
148                           &request->rq_reqhdr, &request->rq_req,
149                           &request->rq_reqlen, &request->rq_reqbuf);
150         if (rc) { 
151                 CERROR("cannot pack request %d\n", rc); 
152                 return NULL;
153         }
154         request->rq_reqhdr->opc = opcode;
155         request->rq_reqhdr->xid = request->rq_xid;
156
157         EXIT;
158         return request;
159 }
160
161 void ptlrpc_free_req(struct ptlrpc_request *request)
162 {
163         if (request == NULL)
164                 return;
165
166         if (request->rq_repbuf != NULL)
167                 OBD_FREE(request->rq_repbuf, request->rq_replen);
168         OBD_FREE(request, sizeof(*request));
169 }
170
171 static int ptlrpc_check_reply(struct ptlrpc_request *req)
172 {
173         int rc = 0;
174
175         //spin_lock(&req->rq_lock);
176         if (req->rq_repbuf != NULL) {
177                 req->rq_flags = PTL_RPC_REPLY;
178                 EXIT;
179                 rc = 1;
180                 goto out;
181         }
182
183         if (sigismember(&(current->pending.signal), SIGKILL) ||
184             sigismember(&(current->pending.signal), SIGTERM) ||
185             sigismember(&(current->pending.signal), SIGSTOP) ||
186             sigismember(&(current->pending.signal), SIGINT)) { 
187                 req->rq_flags = PTL_RPC_INTR;
188                 EXIT;
189                 rc = 1; 
190                 goto out;
191         }
192
193  out:
194         //spin_unlock(&req->rq_lock);
195         return rc;
196 }
197
198 int ptlrpc_check_status(struct ptlrpc_request *req, int err)
199 {
200         ENTRY;
201
202         if (err != 0) {
203                 CERROR("err is %d\n", err);
204                 EXIT;
205                 return err;
206         }
207
208         if (req == NULL) {
209                 CERROR("req == NULL\n");
210                 EXIT;
211                 return -ENOMEM;
212         }
213
214         if (req->rq_rephdr == NULL) {
215                 CERROR("req->rq_rephdr == NULL\n");
216                 EXIT;
217                 return -ENOMEM;
218         }
219
220         if (req->rq_rephdr->status != 0) {
221                 CERROR("req->rq_rephdr->status is %d\n",
222                        req->rq_rephdr->status);
223                 EXIT;
224                 /* XXX: translate this error from net to host */
225                 return req->rq_rephdr->status;
226         }
227
228         EXIT;
229         return 0;
230 }
231
232 /* Abort this request and cleanup any resources associated with it. */
233 int ptlrpc_abort(struct ptlrpc_request *request)
234 {
235         /* First remove the ME for the reply; in theory, this means
236          * that we can tear down the buffer safely. */
237         //spin_lock(&request->rq_lock);
238         PtlMEUnlink(request->rq_reply_me_h);
239         OBD_FREE(request->rq_repbuf, request->rq_replen);
240         request->rq_repbuf = NULL;
241         request->rq_replen = 0;
242         //spin_unlock(&request->rq_lock);
243
244         return 0;
245 }
246
247
248 int ptlrpc_queue_wait(struct ptlrpc_client *cl, struct ptlrpc_request *req)
249                              
250 {
251         int rc = 0;
252         ENTRY;
253
254         init_waitqueue_head(&req->rq_wait_for_rep);
255
256         if (cl->cli_obd) {
257                 /* Local delivery */
258                 ENTRY;
259                 rc = ptlrpc_enqueue(cl, req); 
260         } else {
261                 /* Remote delivery via portals. */
262                 req->rq_req_portal = cl->cli_request_portal;
263                 req->rq_reply_portal = cl->cli_reply_portal;
264                 rc = ptl_send_rpc(req, &cl->cli_server);
265         }
266         if (rc) { 
267                 CERROR("error %d, opcode %d\n", rc, req->rq_reqhdr->opc);
268                 return -rc;
269         }
270
271         CDEBUG(D_OTHER, "-- sleeping\n");
272         wait_event_interruptible(req->rq_wait_for_rep, ptlrpc_check_reply(req));
273         CDEBUG(D_OTHER, "-- done\n");
274         //spin_lock(&req->rq_lock);
275         if (req->rq_flags == PTL_RPC_INTR) { 
276                 /* Clean up the dangling reply buffers */
277                 ptlrpc_abort(req);
278                 EXIT;
279                 rc = -EINTR;
280                 goto out;
281         }
282
283         if (req->rq_flags != PTL_RPC_REPLY) { 
284                 CERROR("Unknown reason for wakeup\n");
285                 /* XXX Phil - I end up here when I kill obdctl */
286                 ptlrpc_abort(req); 
287                 //BUG();
288                 EXIT;
289                 rc = -EINTR;
290                 goto out;
291         }
292
293         rc = cl->cli_rep_unpack(req->rq_repbuf, req->rq_replen,
294                                 &req->rq_rephdr, &req->rq_rep);
295         if (rc) {
296                 CERROR("unpack_rep failed: %d\n", rc);
297                 goto out;
298         }
299         CDEBUG(D_NET, "got rep %d\n", req->rq_rephdr->xid);
300
301         if ( req->rq_rephdr->status == 0 )
302                 CDEBUG(D_NET, "--> buf %p len %d status %d\n", req->rq_repbuf,
303                        req->rq_replen, req->rq_rephdr->status);
304
305         EXIT;
306  out:
307         //spin_unlock(&req->rq_lock);
308         return rc;
309 }