Whamcloud - gitweb
- documentation update for MDS recovery
[fs/lustre-release.git] / lustre / ptlrpc / niobuf.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 #define DEBUG_SUBSYSTEM S_RPC
26
27 #include <linux/lustre_net.h>
28
29 extern ptl_handle_eq_t request_out_eq, 
30         reply_in_eq, 
31         reply_out_eq,
32         bulk_source_eq, 
33         bulk_sink_eq;
34 static ptl_process_id_t local_id = {PTL_ID_ANY, PTL_ID_ANY};
35
36
37 int ptlrpc_check_bulk_sent(struct ptlrpc_bulk_desc *bulk)
38 {
39         ENTRY;
40
41         if (bulk->b_flags & PTL_BULK_FL_SENT)
42                 RETURN(1);
43
44         if (sigismember(&(current->pending.signal), SIGKILL) ||
45             sigismember(&(current->pending.signal), SIGINT)) {
46                 bulk->b_flags |= PTL_RPC_FL_INTR;
47                 RETURN(1);
48         }
49
50         CDEBUG(D_NET, "no event yet\n");
51         RETURN(0);
52 }
53
54 static int ptl_send_buf(struct ptlrpc_request *request,
55                         struct ptlrpc_connection *conn, int portal)
56 {
57         int rc;
58         ptl_process_id_t remote_id;
59         ptl_handle_md_t md_h;
60         ptl_ack_req_t ack;
61
62         request->rq_req_md.user_ptr = request;
63
64         switch (request->rq_type) {
65         case PTL_RPC_TYPE_REQUEST:
66                 request->rq_req_md.start = request->rq_reqmsg;
67                 request->rq_req_md.length = request->rq_reqlen;
68                 request->rq_req_md.eventq = request_out_eq;
69                 request->rq_req_md.threshold = 1;
70                 ack = PTL_NOACK_REQ;
71                 break;
72         case PTL_RPC_TYPE_REPLY:
73                 request->rq_req_md.start = request->rq_repmsg;
74                 request->rq_req_md.length = request->rq_replen;
75                 request->rq_req_md.eventq = reply_out_eq;
76                 request->rq_req_md.threshold = 1;
77                 ack = PTL_NOACK_REQ;
78                 break;
79         default:
80                 LBUG();
81                 return -1; /* notreached */
82         }
83         request->rq_req_md.options = PTL_MD_OP_PUT;
84         request->rq_req_md.user_ptr = request;
85
86         rc = PtlMDBind(conn->c_peer.peer_ni, request->rq_req_md, &md_h);
87         //CERROR("MDBind (outgoing req/rep/bulk): %Lu\n", (__u64)md_h);
88         if (rc != 0) {
89                 CERROR("PtlMDBind failed: %d\n", rc);
90                 LBUG();
91                 return rc;
92         }
93
94         remote_id.nid = conn->c_peer.peer_nid;
95         remote_id.pid = 0;
96
97         CDEBUG(D_NET, "Sending %d bytes to portal %d, xid %d\n",
98                request->rq_req_md.length, portal, request->rq_reqmsg->xid);
99
100         rc = PtlPut(md_h, ack, remote_id, portal, 0, request->rq_reqmsg->xid,
101                     0, 0);
102         if (rc != PTL_OK) {
103                 CERROR("PtlPut(%d, %d, %d) failed: %d\n", remote_id.nid,
104                        portal, request->rq_reqmsg->xid, rc);
105                 PtlMDUnlink(md_h);
106         }
107
108         return rc;
109 }
110
111 int ptlrpc_send_bulk(struct ptlrpc_bulk_desc *bulk, int portal)
112 {
113         int rc;
114         ptl_process_id_t remote_id;
115
116         bulk->b_md.start = bulk->b_buf;
117         bulk->b_md.length = bulk->b_buflen;
118         bulk->b_md.eventq = bulk_source_eq;
119         bulk->b_md.threshold = 2; /* SENT and ACK events */
120         bulk->b_md.options = PTL_MD_OP_PUT;
121         bulk->b_md.user_ptr = bulk;
122
123         rc = PtlMDBind(bulk->b_connection->c_peer.peer_ni, bulk->b_md,
124                        &bulk->b_md_h);
125         if (rc != 0) {
126                 CERROR("PtlMDBind failed: %d\n", rc);
127                 LBUG();
128                 return rc;
129         }
130
131         remote_id.nid = bulk->b_connection->c_peer.peer_nid;
132         remote_id.pid = 0;
133
134         CDEBUG(D_NET, "Sending %d bytes to portal %d, xid %d\n",
135                bulk->b_md.length, portal, bulk->b_xid);
136
137         rc = PtlPut(bulk->b_md_h, PTL_ACK_REQ, remote_id, portal, 0,
138                     bulk->b_xid, 0, 0);
139         if (rc != PTL_OK) {
140                 CERROR("PtlPut(%d, %d, %d) failed: %d\n", remote_id.nid,
141                        portal, bulk->b_xid, rc);
142                 PtlMDUnlink(bulk->b_md_h);
143                 LBUG();
144         }
145
146         return rc;
147 }
148
149 int ptlrpc_register_bulk(struct ptlrpc_bulk_desc *bulk)
150 {
151         int rc;
152         ENTRY;
153
154         rc = PtlMEAttach(bulk->b_connection->c_peer.peer_ni, bulk->b_portal,
155                          local_id, bulk->b_xid, 0, PTL_UNLINK, PTL_INS_AFTER,
156                          &bulk->b_me_h);
157         if (rc != PTL_OK) {
158                 CERROR("PtlMEAttach failed: %d\n", rc);
159                 LBUG();
160                 GOTO(cleanup, rc);
161         }
162
163         bulk->b_md.start = bulk->b_buf;
164         bulk->b_md.length = bulk->b_buflen;
165         bulk->b_md.threshold = 1;
166         bulk->b_md.options = PTL_MD_OP_PUT;
167         bulk->b_md.user_ptr = bulk;
168         bulk->b_md.eventq = bulk_sink_eq;
169
170         rc = PtlMDAttach(bulk->b_me_h, bulk->b_md, PTL_UNLINK, &bulk->b_md_h);
171         //CERROR("MDAttach (bulk sink): %Lu\n", (__u64)bulk->b_md_h);
172         if (rc != PTL_OK) {
173                 CERROR("PtlMDAttach failed: %d\n", rc);
174                 LBUG();
175                 GOTO(cleanup, rc);
176         }
177
178         CDEBUG(D_NET, "Setup bulk sink buffer: %u bytes, xid %u, portal %u\n",
179                bulk->b_buflen, bulk->b_xid, bulk->b_portal);
180         RETURN(0);
181
182         // XXX Confirm that this is safe!
183  cleanup:
184         PtlMEUnlink(bulk->b_me_h);
185         return rc;
186 }
187
188 int ptlrpc_abort_bulk(struct ptlrpc_bulk_desc *bulk)
189 {
190         int rc;
191
192         rc = PtlMEUnlink(bulk->b_me_h);
193         if (rc != PTL_OK)
194                 CERROR("PtlMEUnlink failed: %d\n", rc);
195
196         return rc;
197 }
198
199 int ptlrpc_reply(struct ptlrpc_service *svc, struct ptlrpc_request *req)
200 {
201         /* FIXME: we need to increment the count of handled events */
202         req->rq_type = PTL_RPC_TYPE_REPLY;
203         req->rq_repmsg->conn = req->rq_connection->c_remote_conn;
204         req->rq_repmsg->token = req->rq_connection->c_remote_token;
205         req->rq_repmsg->xid = HTON__u32(req->rq_reqmsg->xid);
206         req->rq_repmsg->status = HTON__u32(req->rq_status);
207         req->rq_reqmsg->type = HTON__u32(req->rq_type);
208         return ptl_send_buf(req, req->rq_connection, svc->srv_rep_portal);
209 }
210
211 int ptlrpc_error(struct ptlrpc_service *svc, struct ptlrpc_request *req)
212 {
213         int rc;
214         ENTRY;
215
216         if (req->rq_repmsg) {
217                 CERROR("req already has repmsg\n");
218                 LBUG();
219         }
220
221         rc = lustre_pack_msg(0, NULL, NULL, &req->rq_replen, &req->rq_repmsg);
222         if (rc)
223                 RETURN(rc);
224
225         req->rq_repmsg->type = HTON__u32(PTL_RPC_MSG_ERR);
226
227         rc = ptlrpc_reply(svc, req);
228         RETURN(rc);
229 }
230
231 void ptlrpc_resend_req(struct ptlrpc_request *req)
232 {
233         ENTRY;
234         req->rq_flags |= PTL_RPC_FL_RESEND;
235         req->rq_flags &= ~PTL_RPC_FL_TIMEOUT;
236         wake_up_interruptible(&req->rq_wait_for_rep);
237         EXIT;
238         return; 
239 }
240
241 int ptl_send_rpc(struct ptlrpc_request *request)
242 {
243         ptl_process_id_t local_id;
244         int rc;
245         char *repbuf;
246
247         ENTRY;
248
249         if (NTOH__u32(request->rq_reqmsg->type) != PTL_RPC_MSG_REQUEST) {
250                 CERROR("wrong packet type sent %d\n",
251                        NTOH__u32(request->rq_reqmsg->type));
252                 LBUG();
253                 RETURN(-EINVAL);
254         }
255         if (request->rq_replen == 0) {
256                 CERROR("request->rq_replen is 0!\n");
257                 RETURN(-EINVAL);
258         }
259
260         /* request->rq_repmsg is set only when the reply comes in, in
261          * client_packet_callback() */
262         OBD_ALLOC(repbuf, request->rq_replen);
263         if (!repbuf)
264                 RETURN(-ENOMEM);
265
266         local_id.nid = PTL_ID_ANY;
267         local_id.pid = PTL_ID_ANY;
268
269         down(&request->rq_client->cli_rpc_sem);
270
271         rc = PtlMEAttach(request->rq_connection->c_peer.peer_ni,
272                          request->rq_client->cli_reply_portal,
273                          local_id, request->rq_reqmsg->xid, 0, PTL_UNLINK,
274                          PTL_INS_AFTER, &request->rq_reply_me_h);
275         if (rc != PTL_OK) {
276                 CERROR("PtlMEAttach failed: %d\n", rc);
277                 LBUG();
278                 GOTO(cleanup, rc);
279         }
280
281         request->rq_type = PTL_RPC_TYPE_REQUEST;
282         request->rq_reply_md.start = repbuf;
283         request->rq_reply_md.length = request->rq_replen;
284         request->rq_reply_md.threshold = 1;
285         request->rq_reply_md.options = PTL_MD_OP_PUT;
286         request->rq_reply_md.user_ptr = request;
287         request->rq_reply_md.eventq = reply_in_eq;
288
289         rc = PtlMDAttach(request->rq_reply_me_h, request->rq_reply_md,
290                          PTL_UNLINK, &request->rq_reply_md_h);
291         if (rc != PTL_OK) {
292                 CERROR("PtlMDAttach failed: %d\n", rc);
293                 LBUG();
294                 GOTO(cleanup2, rc);
295         }
296
297         CDEBUG(D_NET, "Setup reply buffer: %u bytes, xid %u, portal %u\n",
298                request->rq_replen, request->rq_reqmsg->xid,
299                request->rq_client->cli_reply_portal);
300
301         spin_lock(&request->rq_client->cli_lock);
302         list_add(&request->rq_list, &request->rq_client->cli_sending_head);
303         spin_unlock(&request->rq_client->cli_lock);
304
305         rc = ptl_send_buf(request, request->rq_connection,
306                           request->rq_client->cli_request_portal);
307         RETURN(rc);
308
309  cleanup2:
310         PtlMEUnlink(request->rq_reply_me_h);
311  cleanup:
312         OBD_FREE(repbuf, request->rq_replen);
313         up(&request->rq_client->cli_rpc_sem);
314
315         return rc;
316 }
317
318 void ptlrpc_link_svc_me(struct ptlrpc_service *service, int i)
319 {
320         int rc;
321         ptl_md_t dummy;
322         ptl_handle_md_t md_h;
323
324         /* Attach the leading ME on which we build the ring */
325         rc = PtlMEAttach(service->srv_self.peer_ni, service->srv_req_portal,
326                          service->srv_id, 0, ~0, PTL_RETAIN, PTL_INS_BEFORE,
327                          &(service->srv_me_h[i]));
328         if (rc != PTL_OK) {
329                 CERROR("PtlMEAttach failed: %d\n", rc);
330                 LBUG();
331         }
332         
333         if (service->srv_ref_count[i])
334                 LBUG();
335
336         dummy.start         = service->srv_buf[i];
337         dummy.length        = service->srv_buf_size;
338         dummy.max_offset    = service->srv_buf_size;
339         dummy.threshold     = PTL_MD_THRESH_INF;
340         dummy.options       = PTL_MD_OP_PUT | PTL_MD_AUTO_UNLINK;
341         dummy.user_ptr      = service;
342         dummy.eventq        = service->srv_eq_h;
343         dummy.max_offset    = service->srv_buf_size;
344         
345         rc = PtlMDAttach(service->srv_me_h[i], dummy, PTL_UNLINK, &md_h);
346         if (rc != PTL_OK) {
347                 /* cleanup */
348                 CERROR("PtlMDAttach failed: %d\n", rc);
349                 LBUG();
350         }
351 }        
352
353 /* ptl_handled_rpc() should be called by the sleeping process once
354  * it finishes processing an event.  This ensures the ref count is
355  * decremented and that the rpc ring buffer cycles properly.
356  */ 
357 int ptl_handled_rpc(struct ptlrpc_service *service, void *start) 
358 {
359         int index;
360
361         spin_lock(&service->srv_lock);
362
363         for (index = 0; index < service->srv_ring_length; index++)
364                 if (service->srv_buf[index] == start) 
365                         break;
366
367         if (index == service->srv_ring_length)
368                 LBUG();
369
370         CDEBUG(D_INFO, "MD index=%d Ref Count=%d\n", index,
371                service->srv_ref_count[index]);
372         service->srv_ref_count[index]--;
373
374         if (service->srv_ref_count[index] < 0)
375                 LBUG();
376
377         if (service->srv_ref_count[index] == 0 &&
378             !ptl_is_valid_handle(&(service->srv_me_h[index]))) {
379                 CDEBUG(D_NET, "relinking %d\n", index); 
380                 ptlrpc_link_svc_me(service, index); 
381         }
382         
383         spin_unlock(&service->srv_lock);
384         return 0;
385 }