Whamcloud - gitweb
- new xml format, generated by lmc
[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 DEBUG_SUBSYSTEM S_RPC
24
25 #include <linux/obd_support.h>
26 #include <linux/lustre_net.h>
27 #include <linux/lustre_lib.h>
28
29 extern ptl_handle_eq_t request_out_eq, reply_in_eq, reply_out_eq,
30         bulk_source_eq, bulk_sink_eq;
31 static ptl_process_id_t local_id = {PTL_NID_ANY, PTL_PID_ANY};
32
33 int ptlrpc_check_bulk_sent(struct ptlrpc_bulk_desc *desc)
34 {
35         ENTRY;
36
37         if (desc->b_flags & PTL_BULK_FL_SENT)
38                 RETURN(1);
39
40         if (l_killable_pending(current)) {
41                 desc->b_flags |= PTL_RPC_FL_INTR;
42                 RETURN(1);
43         }
44
45         CDEBUG(D_NET, "no event yet\n");
46         RETURN(0);
47 }
48
49 int ptlrpc_check_bulk_received(struct ptlrpc_bulk_desc *desc)
50 {
51         ENTRY;
52
53         if (desc->b_flags & PTL_BULK_FL_RCVD)
54                 RETURN(1);
55
56         if (l_killable_pending(current)) {
57                 desc->b_flags |= PTL_RPC_FL_INTR;
58                 RETURN(1);
59         }
60
61         CDEBUG(D_NET, "no event yet\n");
62         RETURN(0);
63 }
64
65 static int ptl_send_buf(struct ptlrpc_request *request,
66                         struct ptlrpc_connection *conn, int portal)
67 {
68         int rc;
69         ptl_process_id_t remote_id;
70         ptl_handle_md_t md_h;
71         ptl_ack_req_t ack;
72
73         request->rq_req_md.user_ptr = request;
74
75         switch (request->rq_type) {
76         case PTL_RPC_TYPE_REQUEST:
77                 request->rq_req_md.start = request->rq_reqmsg;
78                 request->rq_req_md.length = request->rq_reqlen;
79                 request->rq_req_md.eventq = request_out_eq;
80                 request->rq_req_md.threshold = 1;
81                 ack = PTL_NOACK_REQ;
82                 break;
83         case PTL_RPC_TYPE_REPLY:
84                 request->rq_req_md.start = request->rq_repmsg;
85                 request->rq_req_md.length = request->rq_replen;
86                 request->rq_req_md.eventq = reply_out_eq;
87                 request->rq_req_md.threshold = 1;
88                 ack = PTL_NOACK_REQ;
89                 break;
90         default:
91                 LBUG();
92                 return -1; /* notreached */
93         }
94         request->rq_req_md.options = PTL_MD_OP_PUT;
95         request->rq_req_md.user_ptr = request;
96
97         rc = PtlMDBind(conn->c_peer.peer_ni, request->rq_req_md, &md_h);
98         //CERROR("MDBind (outgoing req/rep/bulk): %Lu\n", (__u64)md_h);
99         if (rc != 0) {
100                 CERROR("PtlMDBind failed: %d\n", rc);
101                 LBUG();
102                 return rc;
103         }
104
105         remote_id.nid = conn->c_peer.peer_nid;
106         remote_id.pid = 0;
107
108         CDEBUG(D_NET, "Sending %d bytes to portal %d, xid %Ld\n",
109                request->rq_req_md.length, portal, request->rq_xid);
110
111         rc = PtlPut(md_h, ack, remote_id, portal, 0, request->rq_xid,
112                     0, 0);
113         if (rc != PTL_OK) {
114                 CERROR("PtlPut(%Lu, %d, %Ld) failed: %d\n", remote_id.nid,
115                        portal, request->rq_xid, rc);
116                 PtlMDUnlink(md_h);
117         }
118
119         return rc;
120 }
121
122 static inline struct iovec *
123 ptlrpc_get_bulk_iov (struct ptlrpc_bulk_desc *desc)
124 {
125         struct iovec *iov;
126         
127         if (desc->b_page_count <= sizeof (desc->b_iov)/sizeof (struct iovec))
128                 return (desc->b_iov);
129         
130         OBD_ALLOC (iov, desc->b_page_count * sizeof (struct iovec));
131         if (iov == NULL)
132                 LBUG();
133         
134         return (iov);
135 }
136
137 static inline void
138 ptlrpc_put_bulk_iov (struct ptlrpc_bulk_desc *desc, struct iovec *iov)
139 {
140         if (desc->b_page_count <= sizeof (desc->b_iov)/sizeof (struct iovec))
141                 return;
142
143         OBD_FREE (iov, desc->b_page_count * sizeof (struct iovec));
144 }
145
146 int ptlrpc_send_bulk(struct ptlrpc_bulk_desc *desc)
147 {
148         int rc;
149         struct list_head *tmp, *next;
150         ptl_process_id_t remote_id;
151         __u32 xid = 0;
152         struct iovec *iov;
153         ENTRY;
154
155         iov = ptlrpc_get_bulk_iov (desc);
156         if (iov == NULL)
157                 RETURN (-ENOMEM);
158
159         desc->b_md.start = iov;
160         desc->b_md.niov = 0;
161         desc->b_md.length = 0;
162         desc->b_md.eventq = bulk_source_eq;
163         desc->b_md.threshold = 2; /* SENT and ACK */
164         desc->b_md.options = PTL_MD_OP_PUT | PTL_MD_IOV;
165         desc->b_md.user_ptr = desc;
166
167         list_for_each_safe(tmp, next, &desc->b_page_list) {
168                 struct ptlrpc_bulk_page *bulk;
169                 bulk = list_entry(tmp, struct ptlrpc_bulk_page, b_link);
170
171                 LASSERT (desc->b_md.niov < desc->b_page_count);
172
173                 if (desc->b_md.niov == 0)
174                         xid = bulk->b_xid;
175                 LASSERT (xid == bulk->b_xid);   /* should all be the same */
176                 
177                 iov[desc->b_md.niov].iov_base = bulk->b_buf;
178                 iov[desc->b_md.niov].iov_len = bulk->b_buflen;
179                 desc->b_md.niov++;
180                 desc->b_md.length += bulk->b_buflen;
181         }
182         
183         LASSERT (desc->b_md.niov == desc->b_page_count);
184         LASSERT (desc->b_md.niov != 0);
185         
186         rc = PtlMDBind(desc->b_connection->c_peer.peer_ni, desc->b_md,
187                        &desc->b_md_h);
188
189         ptlrpc_put_bulk_iov (desc, iov);        /* move down to reduce latency to send */
190
191         if (rc != PTL_OK) {
192                 CERROR("PtlMDBind failed: %d\n", rc);
193                 LBUG();
194                 RETURN(rc);
195         }
196         
197         remote_id.nid = desc->b_connection->c_peer.peer_nid;
198         remote_id.pid = 0;
199         
200         CDEBUG(D_NET, "Sending %u pages %u bytes to portal %d nid %Lx pid %d xid %d\n",
201                desc->b_md.niov, desc->b_md.length, 
202                desc->b_portal, remote_id.nid, remote_id.pid, xid);
203         
204         rc = PtlPut(desc->b_md_h, PTL_ACK_REQ, remote_id,
205                     desc->b_portal, 0, xid, 0, 0);
206         if (rc != PTL_OK) {
207                 CERROR("PtlPut(%Lu, %d, %d) failed: %d\n",
208                        remote_id.nid, desc->b_portal, xid, rc);
209                 PtlMDUnlink(desc->b_md_h);
210                 LBUG();
211                 RETURN(rc);
212         }
213
214         RETURN(0);
215 }
216
217 int ptlrpc_register_bulk(struct ptlrpc_bulk_desc *desc)
218 {
219         struct list_head *tmp, *next;
220         int rc;
221         __u32 xid = 0;
222         struct iovec *iov;
223         ENTRY;
224
225         iov = ptlrpc_get_bulk_iov (desc);
226         if (iov == NULL)
227                 return (-ENOMEM);
228
229         desc->b_md.start = iov;
230         desc->b_md.niov = 0;
231         desc->b_md.length = 0;
232         desc->b_md.threshold = 1;
233         desc->b_md.options = PTL_MD_OP_PUT | PTL_MD_IOV;
234         desc->b_md.user_ptr = desc;
235         desc->b_md.eventq = bulk_sink_eq;
236
237         list_for_each_safe(tmp, next, &desc->b_page_list) {
238                 struct ptlrpc_bulk_page *bulk;
239                 bulk = list_entry(tmp, struct ptlrpc_bulk_page, b_link);
240
241                 LASSERT (desc->b_md.niov < desc->b_page_count);
242
243                 if (desc->b_md.niov == 0)
244                         xid = bulk->b_xid;
245                 LASSERT (xid == bulk->b_xid);   /* should all be the same */
246                 
247                 iov[desc->b_md.niov].iov_base = bulk->b_buf;
248                 iov[desc->b_md.niov].iov_len = bulk->b_buflen;
249                 desc->b_md.niov++;
250                 desc->b_md.length += bulk->b_buflen;
251         }
252
253         LASSERT (desc->b_md.niov == desc->b_page_count);
254         LASSERT (desc->b_md.niov != 0);
255         
256         rc = PtlMEAttach(desc->b_connection->c_peer.peer_ni,
257                          desc->b_portal, local_id, xid, 0,
258                          PTL_UNLINK, PTL_INS_AFTER, &desc->b_me_h);
259
260         ptlrpc_put_bulk_iov (desc, iov);
261
262         if (rc != PTL_OK) {
263                 CERROR("PtlMEAttach failed: %d\n", rc);
264                 LBUG();
265                 GOTO(cleanup, rc);
266         }
267         
268         rc = PtlMDAttach(desc->b_me_h, desc->b_md, PTL_UNLINK,
269                          &desc->b_md_h);
270         if (rc != PTL_OK) {
271                 CERROR("PtlMDAttach failed: %d\n", rc);
272                 LBUG();
273                 GOTO(cleanup, rc);
274         }
275         
276         CDEBUG(D_NET, "Setup bulk sink buffers: %u pages %u bytes, xid %u, "
277                "portal %u\n", desc->b_md.niov, desc->b_md.length, 
278                xid, desc->b_portal);
279
280         RETURN(0);
281
282  cleanup:
283         ptlrpc_abort_bulk(desc);
284
285         return rc;
286 }
287
288 int ptlrpc_abort_bulk(struct ptlrpc_bulk_desc *desc)
289 {
290         /* This should be safe: these handles are initialized to be
291          * invalid in ptlrpc_prep_bulk() */
292         PtlMDUnlink(desc->b_md_h);
293         PtlMEUnlink(desc->b_me_h);
294
295         return 0;
296 }
297
298 int ptlrpc_reply(struct ptlrpc_service *svc, struct ptlrpc_request *req)
299 {
300         if (req->rq_repmsg == NULL) {
301                 CERROR("bad: someone called ptlrpc_reply when they meant "
302                        "ptlrpc_error\n");
303                 return -EINVAL;
304         }
305
306         /* FIXME: we need to increment the count of handled events */
307         req->rq_type = PTL_RPC_TYPE_REPLY;
308         //req->rq_repmsg->conn = req->rq_connection->c_remote_conn;
309         //req->rq_repmsg->token = req->rq_connection->c_remote_token;
310         req->rq_repmsg->status = HTON__u32(req->rq_status);
311         req->rq_reqmsg->type = HTON__u32(req->rq_type);
312         return ptl_send_buf(req, req->rq_connection, svc->srv_rep_portal);
313 }
314
315 int ptlrpc_error(struct ptlrpc_service *svc, struct ptlrpc_request *req)
316 {
317         int rc;
318         ENTRY;
319
320         if (req->rq_repmsg) {
321                 CERROR("req already has repmsg\n");
322                 LBUG();
323         }
324
325         rc = lustre_pack_msg(0, NULL, NULL, &req->rq_replen, &req->rq_repmsg);
326         if (rc)
327                 RETURN(rc);
328
329         req->rq_repmsg->type = HTON__u32(PTL_RPC_MSG_ERR);
330
331         rc = ptlrpc_reply(svc, req);
332         RETURN(rc);
333 }
334
335
336 int ptl_send_rpc(struct ptlrpc_request *request)
337 {
338         int rc;
339         char *repbuf;
340
341         ENTRY;
342
343         if (NTOH__u32(request->rq_reqmsg->type) != PTL_RPC_MSG_REQUEST) {
344                 CERROR("wrong packet type sent %d\n",
345                        NTOH__u32(request->rq_reqmsg->type));
346                 LBUG();
347                 RETURN(EINVAL);
348         }
349         if (request->rq_replen == 0) {
350                 CERROR("request->rq_replen is 0!\n");
351                 RETURN(EINVAL);
352         }
353
354         /* request->rq_repmsg is set only when the reply comes in, in
355          * client_packet_callback() */
356         if (request->rq_reply_md.start)
357                 OBD_FREE(request->rq_reply_md.start, request->rq_replen);
358
359         OBD_ALLOC(repbuf, request->rq_replen);
360         if (!repbuf) {
361                 LBUG();
362                 RETURN(ENOMEM);
363         }
364
365         down(&request->rq_client->cli_rpc_sem);
366
367         rc = PtlMEAttach(request->rq_connection->c_peer.peer_ni,
368                          request->rq_client->cli_reply_portal,
369                          local_id, request->rq_xid, 0, PTL_UNLINK,
370                          PTL_INS_AFTER, &request->rq_reply_me_h);
371         if (rc != PTL_OK) {
372                 CERROR("PtlMEAttach failed: %d\n", rc);
373                 LBUG();
374                 GOTO(cleanup, rc);
375         }
376
377         request->rq_type = PTL_RPC_TYPE_REQUEST;
378         request->rq_reply_md.start = repbuf;
379         request->rq_reply_md.length = request->rq_replen;
380         request->rq_reply_md.threshold = 1;
381         request->rq_reply_md.options = PTL_MD_OP_PUT;
382         request->rq_reply_md.user_ptr = request;
383         request->rq_reply_md.eventq = reply_in_eq;
384
385         rc = PtlMDAttach(request->rq_reply_me_h, request->rq_reply_md,
386                          PTL_UNLINK, &request->rq_reply_md_h);
387         if (rc != PTL_OK) {
388                 CERROR("PtlMDAttach failed: %d\n", rc);
389                 LBUG();
390                 GOTO(cleanup2, rc);
391         }
392
393         CDEBUG(D_NET, "Setup reply buffer: %u bytes, xid %Lu, portal %u\n",
394                request->rq_replen, request->rq_xid,
395                request->rq_client->cli_reply_portal);
396
397         rc = ptl_send_buf(request, request->rq_connection,
398                           request->rq_client->cli_request_portal);
399         RETURN(rc);
400
401  cleanup2:
402         PtlMEUnlink(request->rq_reply_me_h);
403  cleanup:
404         OBD_FREE(repbuf, request->rq_replen);
405         up(&request->rq_client->cli_rpc_sem);
406
407         return rc;
408 }
409
410 void ptlrpc_link_svc_me(struct ptlrpc_service *service, int i)
411 {
412         int rc;
413         ptl_md_t dummy;
414         ptl_handle_md_t md_h;
415
416         /* Attach the leading ME on which we build the ring */
417         rc = PtlMEAttach(service->srv_self.peer_ni, service->srv_req_portal,
418                          local_id, 0, ~0, PTL_RETAIN, PTL_INS_BEFORE,
419                          &(service->srv_me_h[i]));
420         if (rc != PTL_OK) {
421                 CERROR("PtlMEAttach failed: %d\n", rc);
422                 LBUG();
423         }
424         
425         if (service->srv_ref_count[i])
426                 LBUG();
427
428         dummy.start         = service->srv_buf[i];
429         dummy.length        = service->srv_buf_size;
430         dummy.max_offset    = service->srv_buf_size;
431         dummy.threshold     = PTL_MD_THRESH_INF;
432         dummy.options       = PTL_MD_OP_PUT | PTL_MD_AUTO_UNLINK;
433         dummy.user_ptr      = service;
434         dummy.eventq        = service->srv_eq_h;
435         dummy.max_offset    = service->srv_buf_size;
436         
437         rc = PtlMDAttach(service->srv_me_h[i], dummy, PTL_UNLINK, &md_h);
438         if (rc != PTL_OK) {
439                 /* cleanup */
440                 CERROR("PtlMDAttach failed: %d\n", rc);
441                 LBUG();
442         }
443 }        
444
445 /* ptl_handled_rpc() should be called by the sleeping process once
446  * it finishes processing an event.  This ensures the ref count is
447  * decremented and that the rpc ring buffer cycles properly.
448  */ 
449 int ptl_handled_rpc(struct ptlrpc_service *service, void *start) 
450 {
451         int index;
452
453         spin_lock(&service->srv_lock);
454         for (index = 0; index < service->srv_ring_length; index++)
455                 if (service->srv_buf[index] == start) 
456                         break;
457
458         if (index == service->srv_ring_length)
459                 LBUG();
460
461         CDEBUG(D_INFO, "MD index=%d Ref Count=%d\n", index,
462                service->srv_ref_count[index]);
463         service->srv_ref_count[index]--;
464
465         if (service->srv_ref_count[index] < 0)
466                 LBUG();
467
468         if (service->srv_ref_count[index] == 0 &&
469             !ptl_is_valid_handle(&(service->srv_me_h[index]))) {
470                 CDEBUG(D_NET, "relinking %d\n", index); 
471                 ptlrpc_link_svc_me(service, index); 
472         }
473         
474         spin_unlock(&service->srv_lock);
475         return 0;
476 }