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