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