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