Whamcloud - gitweb
land lustre part of b_hd_sec on HEAD.
[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, 2003 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 #ifndef __KERNEL__
25 #include <liblustre.h>
26 #endif
27 #include <linux/obd_class.h>
28 #include <linux/obd_support.h>
29 #include <linux/lustre_net.h>
30 #include <linux/lustre_lib.h>
31 #include <linux/obd.h>
32 #include <linux/lustre_sec.h>
33 #include "ptlrpc_internal.h"
34
35 static int ptl_send_buf (ptl_handle_md_t *mdh, void *base, int len, 
36                          ptl_ack_req_t ack, struct ptlrpc_cb_id *cbid,
37                          struct ptlrpc_connection *conn, int portal, __u64 xid)
38 {
39         int              rc;
40         ptl_md_t         md;
41         char str[PTL_NALFMT_SIZE];
42         ENTRY;
43
44         LASSERT (portal != 0);
45         LASSERT (conn != NULL);
46         CDEBUG (D_INFO, "conn=%p ni %s id %s on %s\n",
47                 conn, conn->c_peer.peer_ni->pni_name,
48                 ptlrpc_id2str(&conn->c_peer, str),
49                 conn->c_peer.peer_ni->pni_name);
50         md.start     = base;
51         md.length    = len;
52         md.threshold = (ack == PTL_ACK_REQ) ? 2 : 1;
53         md.options   = PTLRPC_MD_OPTIONS;
54         md.user_ptr  = cbid;
55         md.eq_handle    = conn->c_peer.peer_ni->pni_eq_h;
56
57         if (ack == PTL_ACK_REQ &&
58             OBD_FAIL_CHECK(OBD_FAIL_PTLRPC_ACK | OBD_FAIL_ONCE)) {
59                 /* don't ask for the ack to simulate failing client */
60                 ack = PTL_NOACK_REQ;
61                 obd_fail_loc |= OBD_FAIL_ONCE | OBD_FAILED;
62         }
63
64         rc = PtlMDBind (conn->c_peer.peer_ni->pni_ni_h, md, 
65                         PTL_UNLINK, mdh);
66         if (rc != PTL_OK) {
67                 CERROR ("PtlMDBind failed: %d\n", rc);
68                 LASSERT (rc == PTL_NO_SPACE);
69                 RETURN (-ENOMEM);
70         }
71
72         CDEBUG(D_NET, "Sending %d bytes to portal %d, xid "LPD64"\n",
73                len, portal, xid);
74
75         rc = PtlPut (*mdh, ack, conn->c_peer.peer_id, portal, 0, xid, 0, 0);
76         if (rc != PTL_OK) {
77                 int rc2;   
78                 /* We're going to get an UNLINK event when I unlink below,
79                  * which will complete just like any other failed send, so
80                  * I fall through and return success here! */
81                 CERROR("PtlPut(%s, %d, "LPD64") failed: %d\n",
82                        ptlrpc_id2str(&conn->c_peer, str),
83                        portal, xid, rc);
84                 rc2 = PtlMDUnlink(*mdh);
85                 LASSERTF(rc2 == PTL_OK, "rc2 = %d\n", rc2);
86         }
87
88         RETURN (0);
89 }
90
91 int ptlrpc_start_bulk_transfer (struct ptlrpc_bulk_desc *desc)
92 {
93         int                 rc;
94         int                 rc2;
95         struct ptlrpc_peer *peer;
96         ptl_md_t            md;
97         __u64               xid;
98         char                str[PTL_NALFMT_SIZE];
99         ENTRY;
100
101         if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_PTLRPC_BULK_PUT_NET)) 
102                 RETURN(0);
103
104         /* NB no locking required until desc is on the network */
105         LASSERT (!desc->bd_network_rw);
106         LASSERT (desc->bd_type == BULK_PUT_SOURCE ||
107                  desc->bd_type == BULK_GET_SINK);
108         desc->bd_success = 0;
109         peer = &desc->bd_export->exp_connection->c_peer;
110
111         md.user_ptr = &desc->bd_cbid;
112         md.eq_handle = peer->peer_ni->pni_eq_h;
113         md.threshold = 2; /* SENT and ACK/REPLY */
114         md.options = PTLRPC_MD_OPTIONS;
115         ptlrpc_fill_bulk_md(&md, desc);
116
117         LASSERT (desc->bd_cbid.cbid_fn == server_bulk_callback);
118         LASSERT (desc->bd_cbid.cbid_arg == desc);
119
120         /* NB total length may be 0 for a read past EOF, so we send a 0
121          * length bulk, since the client expects a bulk event. */
122
123         rc = PtlMDBind(peer->peer_ni->pni_ni_h, md,
124                        PTL_UNLINK, &desc->bd_md_h);
125         if (rc != PTL_OK) {
126                 CERROR("PtlMDBind failed: %d\n", rc);
127                 LASSERT (rc == PTL_NO_SPACE);
128                 RETURN(-ENOMEM);
129         }
130
131         /* Client's bulk and reply matchbits are the same */
132         xid = desc->bd_req->rq_xid;
133         CDEBUG(D_NET, "Transferring %u pages %u bytes via portal %d on %s "
134                "nid %s pid %d xid "LPX64"\n", desc->bd_iov_count,
135                desc->bd_nob, desc->bd_portal, peer->peer_ni->pni_name,
136                ptlrpc_id2str(peer, str), peer->peer_id.pid, xid);
137
138         /* Network is about to get at the memory */
139         desc->bd_network_rw = 1;
140
141         if (desc->bd_type == BULK_PUT_SOURCE)
142                 rc = PtlPut (desc->bd_md_h, PTL_ACK_REQ, peer->peer_id,
143                              desc->bd_portal, 0, xid, 0, 0);
144         else
145                 rc = PtlGet (desc->bd_md_h, peer->peer_id,
146                              desc->bd_portal, 0, xid, 0);
147         
148         if (rc != PTL_OK) {
149                 /* Can't send, so we unlink the MD bound above.  The UNLINK
150                  * event this creates will signal completion with failure,
151                  * so we return SUCCESS here! */
152                 CERROR("Transfer(%s, %d, "LPX64") failed: %d\n",
153                        ptlrpc_id2str(peer, str),
154                        desc->bd_portal, xid, rc);
155                 rc2 = PtlMDUnlink(desc->bd_md_h);
156                 LASSERT (rc2 == PTL_OK);
157         }
158
159         RETURN(0);
160 }
161
162 void ptlrpc_abort_bulk (struct ptlrpc_bulk_desc *desc)
163 {
164         /* Server side bulk abort. Idempotent. Not thread-safe (i.e. only
165          * serialises with completion callback) */
166         struct l_wait_info lwi;
167         int                rc;
168
169         LASSERT (!in_interrupt ());             /* might sleep */
170
171         if (!ptlrpc_bulk_active(desc))          /* completed or */
172                 return;                         /* never started */
173         
174         /* The unlink ensures the callback happens ASAP and is the last
175          * one.  If it fails, it must be because completion just happened,
176          * but we must still l_wait_event() in this case, to give liblustre
177          * a chance to run server_bulk_callback()*/
178
179         PtlMDUnlink (desc->bd_md_h);
180
181         for (;;) {
182                 /* Network access will complete in finite time but the HUGE
183                  * timeout lets us CWARN for visibility of sluggish NALs */
184                 lwi = LWI_TIMEOUT (300 * HZ, NULL, NULL);
185                 rc = l_wait_event(desc->bd_waitq, 
186                                   !ptlrpc_bulk_active(desc), &lwi);
187                 if (rc == 0)
188                         return;
189
190                 LASSERT(rc == -ETIMEDOUT);
191                 CWARN("Unexpectedly long timeout: desc %p\n", desc);
192         }
193 }
194
195 int ptlrpc_register_bulk (struct ptlrpc_request *req)
196 {
197         struct ptlrpc_bulk_desc *desc = req->rq_bulk;
198         struct ptlrpc_peer *peer;
199         int rc;
200         int rc2;
201         ptl_handle_me_t  me_h;
202         ptl_md_t         md;
203         ENTRY;
204
205         if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_PTLRPC_BULK_GET_NET)) 
206                 RETURN(0);
207
208         /* NB no locking required until desc is on the network */
209         LASSERT (desc->bd_nob > 0);
210         LASSERT (!desc->bd_network_rw);
211         LASSERT (desc->bd_iov_count <= PTLRPC_MAX_BRW_PAGES);
212         LASSERT (desc->bd_req != NULL);
213         LASSERT (desc->bd_type == BULK_PUT_SINK ||
214                  desc->bd_type == BULK_GET_SOURCE);
215
216         desc->bd_success = 0;
217
218         peer = &desc->bd_import->imp_connection->c_peer;
219
220         md.user_ptr = &desc->bd_cbid;
221         md.eq_handle = peer->peer_ni->pni_eq_h;
222         md.threshold = 1;                       /* PUT or GET */
223         md.options = PTLRPC_MD_OPTIONS | 
224                      ((desc->bd_type == BULK_GET_SOURCE) ? 
225                       PTL_MD_OP_GET : PTL_MD_OP_PUT);
226         ptlrpc_fill_bulk_md(&md, desc);
227
228         LASSERT (desc->bd_cbid.cbid_fn == client_bulk_callback);
229         LASSERT (desc->bd_cbid.cbid_arg == desc);
230
231         /* XXX Registering the same xid on retried bulk makes my head
232          * explode trying to understand how the original request's bulk
233          * might interfere with the retried request -eeb */
234         LASSERT (!desc->bd_registered || req->rq_xid != desc->bd_last_xid);
235         desc->bd_registered = 1;
236         desc->bd_last_xid = req->rq_xid;
237         
238         rc = PtlMEAttach(peer->peer_ni->pni_ni_h,
239                          desc->bd_portal, desc->bd_import->imp_connection->c_peer.peer_id, 
240                          req->rq_xid, 0, PTL_UNLINK, PTL_INS_AFTER, &me_h);
241         if (rc != PTL_OK) {
242                 CERROR("PtlMEAttach failed: %d\n", rc);
243                 LASSERT (rc == PTL_NO_SPACE);
244                 RETURN (-ENOMEM);
245         }
246
247         /* About to let the network at it... */
248         desc->bd_network_rw = 1;
249         rc = PtlMDAttach(me_h, md, PTL_UNLINK, &desc->bd_md_h);
250         if (rc != PTL_OK) {
251                 CERROR("PtlMDAttach failed: %d\n", rc);
252                 LASSERT (rc == PTL_NO_SPACE);
253                 desc->bd_network_rw = 0;
254                 rc2 = PtlMEUnlink (me_h);
255                 LASSERT (rc2 == PTL_OK);
256                 RETURN (-ENOMEM);
257         }
258
259         CDEBUG(D_NET, "Setup bulk %s buffers: %u pages %u bytes, xid "LPX64", "
260                "portal %u on %s\n",
261                desc->bd_type == BULK_GET_SOURCE ? "get-source" : "put-sink",
262                desc->bd_iov_count, desc->bd_nob,
263                req->rq_xid, desc->bd_portal, peer->peer_ni->pni_name);
264         RETURN(0);
265 }
266
267 void ptlrpc_unregister_bulk (struct ptlrpc_request *req)
268 {
269         /* Disconnect a bulk desc from the network. Idempotent. Not
270          * thread-safe (i.e. only interlocks with completion callback). */
271         struct ptlrpc_bulk_desc *desc = req->rq_bulk;
272         wait_queue_head_t       *wq;
273         struct l_wait_info       lwi;
274         int                      rc;
275
276         LASSERT (!in_interrupt ());             /* might sleep */
277
278         if (!ptlrpc_bulk_active(desc))          /* completed or */
279                 return;                         /* never registered */
280         
281         LASSERT (desc->bd_req == req);          /* bd_req NULL until registered */
282
283         /* the unlink ensures the callback happens ASAP and is the last
284          * one.  If it fails, it must be because completion just happened,
285          * but we must still l_wait_event() in this case to give liblustre
286          * a chance to run client_bulk_callback() */
287
288         PtlMDUnlink (desc->bd_md_h);
289        
290         if (req->rq_set != NULL) 
291                 wq = &req->rq_set->set_waitq;
292         else
293                 wq = &req->rq_reply_waitq;
294
295         for (;;) {
296                 /* Network access will complete in finite time but the HUGE
297                  * timeout lets us CWARN for visibility of sluggish NALs */
298                 lwi = LWI_TIMEOUT (300 * HZ, NULL, NULL);
299                 rc = l_wait_event(*wq, !ptlrpc_bulk_active(desc), &lwi);
300                 if (rc == 0)
301                         return;
302                 
303                 LASSERT (rc == -ETIMEDOUT);
304                 CWARN("Unexpectedly long timeout: desc %p\n", desc);
305         }
306 }
307
308 int ptlrpc_send_reply (struct ptlrpc_request *req, int may_be_difficult)
309 {
310         struct ptlrpc_service     *svc = req->rq_rqbd->rqbd_srv_ni->sni_service;
311         struct ptlrpc_reply_state *rs = req->rq_reply_state;
312         struct ptlrpc_connection  *conn;
313         int                        rc;
314
315         /* We must already have a reply buffer (only ptlrpc_error() may be
316          * called without one).  We usually also have a request buffer which
317          * is either the actual (swabbed) incoming request, or a saved copy
318          * if this is a req saved in target_queue_final_reply(). but this
319          * will not be true since some security handling may skip the reqmsg
320          * setting and prepare reply under normal ptlrpc layer */
321         LASSERT (rs != NULL);
322         LASSERT (req->rq_repmsg != NULL);
323         LASSERT (may_be_difficult || !rs->rs_difficult);
324         LASSERT (req->rq_repmsg == rs->rs_msg);
325         LASSERT (rs->rs_cb_id.cbid_fn == reply_out_callback);
326         LASSERT (rs->rs_cb_id.cbid_arg == rs);
327
328         LASSERT (req->rq_repmsg != NULL);
329         if (req->rq_type != PTL_RPC_MSG_ERR)
330                 req->rq_type = PTL_RPC_MSG_REPLY;
331
332         req->rq_repmsg->type   = req->rq_type;
333         req->rq_repmsg->status = req->rq_status;
334         req->rq_repmsg->opc    = req->rq_reqmsg ? req->rq_reqmsg->opc : 0;
335
336         if (req->rq_export == NULL) 
337                 conn = ptlrpc_get_connection(&req->rq_peer, NULL);
338         else
339                 conn = ptlrpc_connection_addref(req->rq_export->exp_connection);
340
341         atomic_inc (&svc->srv_outstanding_replies);
342
343         rc = svcsec_authorize(req);
344         if (rc) {
345                 CERROR("Error wrap reply message "LPX64"\n", req->rq_xid);
346                 goto out;
347         }
348
349         rc = ptl_send_buf (&rs->rs_md_h, rs->rs_repbuf, rs->rs_repdata_len,
350                            rs->rs_difficult ? PTL_ACK_REQ : PTL_NOACK_REQ,
351                            &rs->rs_cb_id, conn,
352                            svc->srv_rep_portal, req->rq_xid);
353 out:
354         if (rc != 0) {
355                 atomic_dec (&svc->srv_outstanding_replies);
356
357                 if (!rs->rs_difficult) {
358                         /* Callers other than target_send_reply() expect me
359                          * to clean up on a comms error */
360                         lustre_free_reply_state (rs);
361                         req->rq_reply_state = NULL;
362                         req->rq_repmsg = NULL;
363                 }
364         }
365         ptlrpc_put_connection(conn);
366         return rc;
367 }
368
369 int ptlrpc_reply (struct ptlrpc_request *req)
370 {
371         return (ptlrpc_send_reply (req, 0));
372 }
373
374 int ptlrpc_error(struct ptlrpc_request *req)
375 {
376         int rc;
377         ENTRY;
378
379         if (!req->rq_repmsg) {
380                 rc = lustre_pack_reply(req, 0, NULL, NULL);
381                 if (rc)
382                         RETURN(rc);
383         }
384
385         req->rq_type = PTL_RPC_MSG_ERR;
386
387         rc = ptlrpc_send_reply (req, 0);
388         RETURN(rc);
389 }
390
391 int ptl_send_rpc(struct ptlrpc_request *request)
392 {
393         int rc;
394         int rc2;
395         struct ptlrpc_connection *connection;
396         unsigned long flags;
397         ptl_handle_me_t  reply_me_h;
398         ptl_md_t         reply_md;
399         ENTRY;
400
401         LASSERT (request->rq_type == PTL_RPC_MSG_REQUEST);
402
403         /* If this is a re-transmit, we're required to have disengaged
404          * cleanly from the previous attempt */
405         LASSERT (!request->rq_receiving_reply);
406
407         connection = request->rq_import->imp_connection;
408
409         if (request->rq_bulk != NULL) {
410                 rc = ptlrpc_register_bulk (request);
411                 if (rc != 0)
412                         RETURN(rc);
413         }
414
415         request->rq_reqmsg->handle = request->rq_import->imp_remote_handle;
416         request->rq_reqmsg->type = PTL_RPC_MSG_REQUEST;
417         request->rq_reqmsg->conn_cnt = request->rq_import->imp_conn_cnt;
418
419         /* wrap_request might need to refresh gss cred, if this is called
420          * in ptlrpcd then the whole daemon thread will be waiting on
421          * gss negotiate rpc. FIXME
422          */
423         rc = ptlrpcs_cli_wrap_request(request);
424         if (rc)
425                 GOTO(cleanup_bulk, rc);
426
427         LASSERT (request->rq_replen != 0);
428         if (request->rq_repbuf == NULL) {
429                 rc = ptlrpcs_cli_alloc_repbuf(request, request->rq_replen);
430                 if (rc)
431                         GOTO(cleanup_bulk, rc);
432         }
433
434         rc = PtlMEAttach(connection->c_peer.peer_ni->pni_ni_h,
435                          request->rq_reply_portal, /* XXX FIXME bug 249 */
436                          connection->c_peer.peer_id, request->rq_xid, 0, PTL_UNLINK,
437                          PTL_INS_AFTER, &reply_me_h);
438         if (rc != PTL_OK) {
439                 CERROR("PtlMEAttach failed: %d\n", rc);
440                 LASSERT (rc == PTL_NO_SPACE);
441                 GOTO(cleanup_repmsg, rc = -ENOMEM);
442         }
443
444         spin_lock_irqsave (&request->rq_lock, flags);
445         /* If the MD attach succeeds, there _will_ be a reply_in callback */
446         request->rq_receiving_reply = 1;
447         /* Clear any flags that may be present from previous sends. */
448         request->rq_replied = 0;
449         request->rq_err = 0;
450         request->rq_timedout = 0;
451         request->rq_net_err = 0;
452         request->rq_resend = 0;
453         request->rq_ptlrpcs_restart = 0;
454         request->rq_restart = 0;
455         spin_unlock_irqrestore (&request->rq_lock, flags);
456
457         reply_md.start     = request->rq_repbuf;
458         reply_md.length    = request->rq_repbuf_len;
459         reply_md.threshold = 1;
460         reply_md.options   = PTLRPC_MD_OPTIONS | PTL_MD_OP_PUT;
461         reply_md.user_ptr  = &request->rq_reply_cbid;
462         reply_md.eq_handle    = connection->c_peer.peer_ni->pni_eq_h;
463
464         rc = PtlMDAttach(reply_me_h, reply_md, PTL_UNLINK, 
465                          &request->rq_reply_md_h);
466         if (rc != PTL_OK) {
467                 CERROR("PtlMDAttach failed: %d\n", rc);
468                 LASSERT (rc == PTL_NO_SPACE);
469                 GOTO(cleanup_me, rc -ENOMEM);
470         }
471
472         CDEBUG(D_NET, "Setup reply buffer: %u bytes, xid "LPU64
473                ", portal %u on %s\n",
474                request->rq_replen, request->rq_xid,
475                request->rq_reply_portal,
476                connection->c_peer.peer_ni->pni_name);
477
478         ptlrpc_request_addref(request);        /* +1 ref for the SENT callback */
479
480         request->rq_sent = LTIME_S(CURRENT_TIME);
481         ptlrpc_pinger_sending_on_import(request->rq_import);
482         rc = ptl_send_buf(&request->rq_req_md_h, 
483                           request->rq_reqbuf, request->rq_reqdata_len,
484                           PTL_NOACK_REQ, &request->rq_req_cbid, 
485                           connection,
486                           request->rq_request_portal,
487                           request->rq_xid);
488         if (rc == 0) {
489                 ptlrpc_lprocfs_rpc_sent(request);
490                 RETURN(rc);
491         }
492
493         ptlrpc_req_finished (request);          /* drop callback ref */
494
495  cleanup_me:
496         /* MEUnlink is safe; the PUT didn't even get off the ground, and
497          * nobody apart from the PUT's target has the right nid+XID to
498          * access the reply buffer. */
499         rc2 = PtlMEUnlink(reply_me_h);
500         LASSERT (rc2 == PTL_OK);
501         /* UNLINKED callback called synchronously */
502         LASSERT (!request->rq_receiving_reply);
503
504  cleanup_repmsg:
505         ptlrpcs_cli_free_repbuf(request);
506
507  cleanup_bulk:
508         if (request->rq_bulk != NULL)
509                 ptlrpc_unregister_bulk(request);
510
511         return rc;
512 }
513
514 int ptlrpc_register_rqbd (struct ptlrpc_request_buffer_desc *rqbd)
515 {
516         struct ptlrpc_srv_ni    *srv_ni = rqbd->rqbd_srv_ni;
517         struct ptlrpc_service   *service = srv_ni->sni_service;
518         static ptl_process_id_t  match_id = {PTL_NID_ANY, PTL_PID_ANY};
519         int                      rc;
520         ptl_md_t                 md;
521         ptl_handle_me_t          me_h;
522
523         CDEBUG(D_NET, "PtlMEAttach: portal %d on %s\n",
524                service->srv_req_portal, srv_ni->sni_ni->pni_name);
525
526         if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_PTLRPC_RQBD))
527                 return (-ENOMEM);
528
529         rc = PtlMEAttach(srv_ni->sni_ni->pni_ni_h, service->srv_req_portal,
530                          match_id, 0, ~0, PTL_UNLINK, PTL_INS_AFTER, &me_h);
531         if (rc != PTL_OK) {
532                 CERROR("PtlMEAttach failed: %d\n", rc);
533                 return (-ENOMEM);
534         }
535
536         LASSERT(rqbd->rqbd_refcount == 0);
537         rqbd->rqbd_refcount = 1;
538
539         md.start     = rqbd->rqbd_buffer;
540         md.length    = service->srv_buf_size;
541         md.max_size  = service->srv_max_req_size;
542         md.threshold = PTL_MD_THRESH_INF;
543         md.options   = PTLRPC_MD_OPTIONS | PTL_MD_OP_PUT | PTL_MD_MAX_SIZE;
544         md.user_ptr  = &rqbd->rqbd_cbid;
545         md.eq_handle = srv_ni->sni_ni->pni_eq_h;
546         
547         rc = PtlMDAttach(me_h, md, PTL_UNLINK, &rqbd->rqbd_md_h);
548         if (rc == PTL_OK)
549                 return (0);
550
551         CERROR("PtlMDAttach failed: %d; \n", rc);
552         LASSERT (rc == PTL_NO_SPACE);
553         rc = PtlMEUnlink (me_h);
554         LASSERT (rc == PTL_OK);
555         rqbd->rqbd_refcount = 0;
556         
557         return (-ENOMEM);
558 }
559
560 static int rawrpc_timedout(void *data)
561 {
562         struct ptlrpc_request *req = (struct ptlrpc_request *) data;
563         unsigned long flags;
564
565         spin_lock_irqsave(&req->rq_lock, flags);
566         if (!req->rq_replied)
567                 req->rq_timedout = 1;
568         spin_unlock_irqrestore(&req->rq_lock, flags);
569
570         return 1;
571 }
572
573 /* to make things as simple as possible */
574 static int rawrpc_check_reply(struct ptlrpc_request *req)
575 {
576         unsigned long flags;
577         int rc;
578
579         spin_lock_irqsave (&req->rq_lock, flags);
580         rc = req->rq_replied || req->rq_net_err || req->rq_err ||
581              req->rq_resend || req->rq_restart;
582         spin_unlock_irqrestore(&req->rq_lock, flags);
583         return rc;
584 }
585
586 /*
587  * Construct a fake ptlrpc_request to do the work, in order to
588  * user the existing callback/wakeup facilities
589  */
590 int ptlrpc_do_rawrpc(struct obd_import *imp,
591                      char *reqbuf, int reqlen,
592                      char *repbuf, int *replenp,
593                      int timeout)
594 {
595         struct ptlrpc_connection *conn;
596         struct ptlrpc_request request; /* just a fake one */
597         ptl_handle_me_t reply_me_h;
598         ptl_md_t reply_md, req_md;
599         struct l_wait_info lwi;
600         unsigned long irq_flags;
601         int rc;
602         ENTRY;
603
604         LASSERT(imp);
605         class_import_get(imp);
606         if (imp->imp_state == LUSTRE_IMP_CLOSED) {
607                 CWARN("raw rpc on closed imp(=>%s)? send anyway\n",
608                        imp->imp_target_uuid.uuid);
609         }
610
611         conn = imp->imp_connection;
612
613         /* initialize request */
614         memset(&request, 0, sizeof(request));
615         request.rq_req_cbid.cbid_fn = request_out_callback;
616         request.rq_req_cbid.cbid_arg = &request;
617         request.rq_reply_cbid.cbid_fn  = reply_in_callback;
618         request.rq_reply_cbid.cbid_arg = &request;
619         request.rq_reqbuf = reqbuf;
620         request.rq_reqbuf_len = reqlen;
621         request.rq_repbuf = repbuf;
622         request.rq_repbuf_len = *replenp;
623         request.rq_set = NULL;
624         spin_lock_init(&request.rq_lock);
625         init_waitqueue_head(&request.rq_reply_waitq);
626         atomic_set(&request.rq_refcount, 1000000); /* never be droped */
627         request.rq_xid = ptlrpc_next_xid();
628
629         /* add into sending list */
630         spin_lock_irqsave(&imp->imp_lock, irq_flags);
631         list_add_tail(&request.rq_list, &imp->imp_rawrpc_list);
632         spin_unlock_irqrestore(&imp->imp_lock, irq_flags);
633
634         /* prepare reply buffer */
635         rc = PtlMEAttach(conn->c_peer.peer_ni->pni_ni_h,
636                          imp->imp_client->cli_reply_portal,
637                          conn->c_peer.peer_id, request.rq_xid, 0, PTL_UNLINK,
638                          PTL_INS_AFTER, &reply_me_h);
639         if (rc != PTL_OK) {
640                 CERROR("PtlMEAttach failed: %d\n", rc);
641                 LASSERT (rc == PTL_NO_SPACE);
642                 GOTO(cleanup_imp, rc = -ENOMEM);
643         }
644
645         spin_lock_irqsave(&request.rq_lock, irq_flags);
646         request.rq_receiving_reply = 1;
647         spin_unlock_irqrestore(&request.rq_lock, irq_flags);
648
649         reply_md.start          = repbuf;
650         reply_md.length         = *replenp;
651         reply_md.threshold      = 1;
652         reply_md.options        = PTLRPC_MD_OPTIONS | PTL_MD_OP_PUT;
653         reply_md.user_ptr       = &request.rq_reply_cbid;
654         reply_md.eq_handle      = conn->c_peer.peer_ni->pni_eq_h;
655
656         rc = PtlMDAttach(reply_me_h, reply_md, PTL_UNLINK,
657                          &request.rq_reply_md_h);
658         if (rc != PTL_OK) {
659                 CERROR("PtlMDAttach failed: %d\n", rc);
660                 LASSERT (rc == PTL_NO_SPACE);
661                 GOTO(cleanup_me, rc = -ENOMEM);
662         }
663
664         /* prepare request buffer */
665         req_md.start            = reqbuf;
666         req_md.length           = reqlen;
667         req_md.threshold        = 1;
668         req_md.options          = PTLRPC_MD_OPTIONS;
669         req_md.user_ptr         = &request.rq_req_cbid;
670         req_md.eq_handle        = conn->c_peer.peer_ni->pni_eq_h;
671
672         rc = PtlMDBind(conn->c_peer.peer_ni->pni_ni_h,
673                        req_md, PTL_UNLINK, &request.rq_req_md_h);
674         if (rc != PTL_OK) {
675                 CERROR("PtlMDBind failed %d\n", rc);
676                 LASSERT (rc == PTL_NO_SPACE);
677                 GOTO(cleanup_me, rc = -ENOMEM);
678         }
679
680         rc = PtlPut(request.rq_req_md_h, PTL_NOACK_REQ, conn->c_peer.peer_id,
681                     imp->imp_client->cli_request_portal,
682                     0, request.rq_xid, 0, 0);
683         if (rc != PTL_OK) {
684                 CERROR("PtlPut failed %d\n", rc);
685                 GOTO(cleanup_md, rc);
686         }
687
688         lwi = LWI_TIMEOUT(timeout * HZ, rawrpc_timedout, &request);
689         l_wait_event(request.rq_reply_waitq,
690                      rawrpc_check_reply(&request), &lwi);
691
692         ptlrpc_unregister_reply(&request);
693
694         if (request.rq_err || request.rq_resend || request.rq_intr ||
695             request.rq_timedout || !request.rq_replied) {
696                 CERROR("secinit rpc error: err %d, resend %d, "
697                        "intr %d, timeout %d, replied %d\n",
698                         request.rq_err, request.rq_resend, request.rq_intr,
699                         request.rq_timedout, request.rq_replied);
700                 rc = -EINVAL;
701         } else {
702                 *replenp = request.rq_nob_received;
703                 rc = 0;
704         }
705         GOTO(cleanup_imp, rc);
706
707 cleanup_md:
708         PtlMDUnlink(request.rq_req_md_h);
709 cleanup_me:
710         PtlMEUnlink(reply_me_h);
711 cleanup_imp:
712         spin_lock_irqsave(&imp->imp_lock, irq_flags);
713         list_del_init(&request.rq_list);
714         spin_unlock_irqrestore(&imp->imp_lock, irq_flags);
715
716         class_import_put(imp);
717         RETURN(rc);
718 }