1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2002 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
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.
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.
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.
23 #define DEBUG_SUBSYSTEM S_RPC
25 #include <linux/lustre_ha.h>
27 void ptlrpc_init_client(struct recovd_obd *recovd,
28 int (*recover)(struct ptlrpc_client *recover),
30 int rep_portal, struct ptlrpc_client *cl)
32 memset(cl, 0, sizeof(*cl));
33 cl->cli_recovd = recovd;
34 cl->cli_recover = recover;
36 recovd_cli_manage(recovd, cl);
38 cl->cli_request_portal = req_portal;
39 cl->cli_reply_portal = rep_portal;
40 INIT_LIST_HEAD(&cl->cli_delayed_head);
41 INIT_LIST_HEAD(&cl->cli_sending_head);
42 INIT_LIST_HEAD(&cl->cli_dying_head);
43 spin_lock_init(&cl->cli_lock);
44 sema_init(&cl->cli_rpc_sem, 32);
47 __u8 *ptlrpc_req_to_uuid(struct ptlrpc_request *req)
49 return req->rq_connection->c_remote_uuid;
52 struct ptlrpc_connection *ptlrpc_uuid_to_connection(char *uuid)
54 struct ptlrpc_connection *c;
55 struct lustre_peer peer;
58 err = kportal_uuid_to_peer(uuid, &peer);
60 CERROR("cannot find peer %s!\n", uuid);
64 c = ptlrpc_get_connection(&peer);
71 void ptlrpc_readdress_connection(struct ptlrpc_connection *conn, char *uuid)
73 struct lustre_peer peer;
76 err = kportal_uuid_to_peer(uuid, &peer);
78 CERROR("cannot find peer %s!\n", uuid);
82 memcpy(&conn->c_peer, &peer, sizeof(peer));
86 struct ptlrpc_bulk_desc *ptlrpc_prep_bulk(struct ptlrpc_connection *conn)
88 struct ptlrpc_bulk_desc *bulk;
90 OBD_ALLOC(bulk, sizeof(*bulk));
92 bulk->b_connection = ptlrpc_connection_addref(conn);
93 init_waitqueue_head(&bulk->b_waitq);
94 INIT_LIST_HEAD(&bulk->b_page_list);
100 struct ptlrpc_bulk_page *ptlrpc_prep_bulk_page(struct ptlrpc_bulk_desc *desc)
102 struct ptlrpc_bulk_page *bulk;
104 OBD_ALLOC(bulk, sizeof(*bulk));
107 ptl_set_inv_handle(&bulk->b_md_h);
108 ptl_set_inv_handle(&bulk->b_me_h);
109 list_add_tail(&bulk->b_link, &desc->b_page_list);
110 desc->b_page_count++;
115 void ptlrpc_free_bulk(struct ptlrpc_bulk_desc *bulk)
117 struct list_head *tmp, *next;
124 list_for_each_safe(tmp, next, &bulk->b_page_list) {
125 struct ptlrpc_bulk_page *bulk;
126 bulk = list_entry(tmp, struct ptlrpc_bulk_page, b_link);
127 ptlrpc_free_bulk_page(bulk);
130 ptlrpc_put_connection(bulk->b_connection);
132 OBD_FREE(bulk, sizeof(*bulk));
136 void ptlrpc_free_bulk_page(struct ptlrpc_bulk_page *bulk)
144 list_del(&bulk->b_link);
145 bulk->b_desc->b_page_count--;
146 OBD_FREE(bulk, sizeof(*bulk));
150 struct ptlrpc_request *ptlrpc_prep_req(struct ptlrpc_client *cl,
151 struct ptlrpc_connection *conn,
152 int opcode, int count, int *lengths,
155 struct ptlrpc_request *request;
159 OBD_ALLOC(request, sizeof(*request));
161 CERROR("request allocation out of memory\n");
165 rc = lustre_pack_msg(count, lengths, bufs,
166 &request->rq_reqlen, &request->rq_reqmsg);
168 CERROR("cannot pack request %d\n", rc);
172 request->rq_type = PTL_RPC_TYPE_REQUEST;
173 request->rq_connection = ptlrpc_connection_addref(conn);
175 request->rq_reqmsg->conn = (__u64)(unsigned long)conn->c_remote_conn;
176 request->rq_reqmsg->token = conn->c_remote_token;
177 request->rq_reqmsg->opc = HTON__u32(opcode);
178 request->rq_reqmsg->type = HTON__u32(PTL_RPC_MSG_REQUEST);
179 INIT_LIST_HEAD(&request->rq_list);
181 /* this will be dec()d once in req_finished, once in free_committed */
182 atomic_set(&request->rq_refcount, 2);
184 spin_lock(&conn->c_lock);
185 request->rq_reqmsg->xid = HTON__u32(++conn->c_xid_out);
186 request->rq_xid = conn->c_xid_out;
187 spin_unlock(&conn->c_lock);
189 request->rq_client = cl;
194 void ptlrpc_req_finished(struct ptlrpc_request *request)
199 if (request->rq_repmsg != NULL) {
200 OBD_FREE(request->rq_repmsg, request->rq_replen);
201 request->rq_repmsg = NULL;
202 request->rq_reply_md.start = NULL;
205 if (atomic_dec_and_test(&request->rq_refcount))
206 ptlrpc_free_req(request);
209 void ptlrpc_free_req(struct ptlrpc_request *request)
214 if (request->rq_repmsg != NULL)
215 OBD_FREE(request->rq_repmsg, request->rq_replen);
216 if (request->rq_reqmsg != NULL)
217 OBD_FREE(request->rq_reqmsg, request->rq_reqlen);
219 if (request->rq_client) {
220 spin_lock(&request->rq_client->cli_lock);
221 list_del_init(&request->rq_list);
222 spin_unlock(&request->rq_client->cli_lock);
225 ptlrpc_put_connection(request->rq_connection);
227 OBD_FREE(request, sizeof(*request));
230 static int ptlrpc_check_reply(struct ptlrpc_request *req)
234 if (req->rq_repmsg != NULL) {
235 req->rq_transno = NTOH__u64(req->rq_repmsg->transno);
236 req->rq_flags |= PTL_RPC_FL_REPLIED;
240 if (req->rq_flags & PTL_RPC_FL_RESEND) {
241 CERROR("-- RESEND --\n");
245 if (req->rq_flags & PTL_RPC_FL_RECOVERY) {
246 CERROR("-- RESTART --\n");
251 if (CURRENT_TIME - req->rq_time >= req->rq_timeout) {
252 CERROR("-- REQ TIMEOUT --\n");
253 /* clear the timeout */
255 req->rq_connection->c_level = LUSTRE_CONN_RECOVD;
256 req->rq_flags |= PTL_RPC_FL_TIMEOUT;
257 if (req->rq_client && req->rq_client->cli_recovd)
258 recovd_cli_fail(req->rq_client);
259 if (req->rq_level < LUSTRE_CONN_FULL)
266 if (req->rq_timeout) {
267 schedule_timeout(req->rq_timeout * HZ);
270 if (sigismember(&(current->pending.signal), SIGKILL) ||
271 sigismember(&(current->pending.signal), SIGTERM) ||
272 sigismember(&(current->pending.signal), SIGINT)) {
273 req->rq_flags |= PTL_RPC_FL_INTR;
281 int ptlrpc_check_status(struct ptlrpc_request *req, int err)
286 CERROR("err is %d\n", err);
291 CERROR("req == NULL\n");
295 if (req->rq_repmsg == NULL) {
296 CERROR("req->rq_repmsg == NULL\n");
300 if (req->rq_repmsg->type == NTOH__u32(PTL_RPC_MSG_ERR)) {
301 CERROR("req->rq_repmsg->type == PTL_RPC_MSG_ERR\n");
305 if (req->rq_repmsg->status != 0) {
306 CERROR("req->rq_repmsg->status is %d\n",
307 req->rq_repmsg->status);
308 /* XXX: translate this error from net to host */
309 RETURN(req->rq_repmsg->status);
315 static void ptlrpc_cleanup_request_buf(struct ptlrpc_request *request)
317 OBD_FREE(request->rq_reqmsg, request->rq_reqlen);
318 request->rq_reqmsg = NULL;
319 request->rq_reqlen = 0;
322 /* Abort this request and cleanup any resources associated with it. */
323 static int ptlrpc_abort(struct ptlrpc_request *request)
325 /* First remove the ME for the reply; in theory, this means
326 * that we can tear down the buffer safely. */
327 PtlMEUnlink(request->rq_reply_me_h);
328 OBD_FREE(request->rq_reply_md.start, request->rq_replen);
329 request->rq_repmsg = NULL;
330 request->rq_replen = 0;
334 /* caller must lock cli */
335 void ptlrpc_free_committed(struct ptlrpc_client *cli)
337 struct list_head *tmp, *saved;
338 struct ptlrpc_request *req;
340 list_for_each_safe(tmp, saved, &cli->cli_sending_head) {
341 req = list_entry(tmp, struct ptlrpc_request, rq_list);
343 if ( (req->rq_flags & PTL_RPC_FL_REPLAY) ) {
344 CDEBUG(D_INFO, "Retaining request %Ld for replay\n",
349 /* not yet committed */
350 if (!req->rq_transno ||
351 req->rq_transno > cli->cli_last_committed)
354 CDEBUG(D_INFO, "Marking request %Ld as committed ("
355 "transno=%Lu, last_committed=%Lu\n",
356 req->rq_xid, req->rq_transno,
357 cli->cli_last_committed);
358 if (atomic_dec_and_test(&req->rq_refcount)) {
359 /* we do this to prevent free_req deadlock */
360 list_del_init(&req->rq_list);
361 req->rq_client = NULL;
362 ptlrpc_free_req(req);
364 list_del_init(&req->rq_list);
365 list_add(&req->rq_list, &cli->cli_dying_head);
373 void ptlrpc_cleanup_client(struct ptlrpc_client *cli)
375 struct list_head *tmp, *saved;
376 struct ptlrpc_request *req;
379 spin_lock(&cli->cli_lock);
380 list_for_each_safe(tmp, saved, &cli->cli_sending_head) {
381 req = list_entry(tmp, struct ptlrpc_request, rq_list);
382 CDEBUG(D_INFO, "Cleaning req %p from sending list.\n", req);
383 list_del_init(&req->rq_list);
384 req->rq_client = NULL;
385 ptlrpc_free_req(req);
387 list_for_each_safe(tmp, saved, &cli->cli_dying_head) {
388 req = list_entry(tmp, struct ptlrpc_request, rq_list);
389 CERROR("Request %p is on the dying list at cleanup!\n", req);
390 list_del_init(&req->rq_list);
391 req->rq_client = NULL;
392 ptlrpc_free_req(req);
394 spin_unlock(&cli->cli_lock);
400 void ptlrpc_continue_req(struct ptlrpc_request *req)
403 CDEBUG(D_INODE, "continue delayed request %Ld opc %d\n",
404 req->rq_xid, req->rq_reqmsg->opc);
405 wake_up_interruptible(&req->rq_wait_for_rep);
409 void ptlrpc_resend_req(struct ptlrpc_request *req)
412 CDEBUG(D_INODE, "resend request %Ld, opc %d\n",
413 req->rq_xid, req->rq_reqmsg->opc);
414 req->rq_status = -EAGAIN;
415 req->rq_level = LUSTRE_CONN_RECOVD;
416 req->rq_flags |= PTL_RPC_FL_RESEND;
417 req->rq_flags &= ~PTL_RPC_FL_TIMEOUT;
418 wake_up_interruptible(&req->rq_wait_for_rep);
422 void ptlrpc_restart_req(struct ptlrpc_request *req)
425 CDEBUG(D_INODE, "restart completed request %Ld, opc %d\n",
426 req->rq_xid, req->rq_reqmsg->opc);
427 req->rq_status = -ERESTARTSYS;
428 req->rq_flags |= PTL_RPC_FL_RECOVERY;
429 req->rq_flags &= ~PTL_RPC_FL_TIMEOUT;
430 wake_up_interruptible(&req->rq_wait_for_rep);
434 int ptlrpc_queue_wait(struct ptlrpc_request *req)
437 struct ptlrpc_client *cli = req->rq_client;
440 init_waitqueue_head(&req->rq_wait_for_rep);
441 CDEBUG(D_NET, "subsys: %s req %Ld opc %d level %d, conn level %d\n",
442 cli->cli_name, req->rq_xid, req->rq_reqmsg->opc, req->rq_level,
443 req->rq_connection->c_level);
445 /* XXX probably both an import and connection level are needed */
446 if (req->rq_level > req->rq_connection->c_level) {
447 CERROR("process %d waiting for recovery\n", current->pid);
448 spin_lock(&cli->cli_lock);
449 list_del_init(&req->rq_list);
450 list_add(&req->rq_list, cli->cli_delayed_head.prev);
451 spin_unlock(&cli->cli_lock);
452 wait_event_interruptible
453 (req->rq_wait_for_rep,
454 req->rq_level <= req->rq_connection->c_level);
455 spin_lock(&cli->cli_lock);
456 list_del_init(&req->rq_list);
457 spin_unlock(&cli->cli_lock);
458 CERROR("process %d resumed\n", current->pid);
461 req->rq_time = CURRENT_TIME;
462 req->rq_timeout = 30;
463 rc = ptl_send_rpc(req);
465 CERROR("error %d, opcode %d\n", rc, req->rq_reqmsg->opc);
468 ptlrpc_cleanup_request_buf(req);
469 up(&cli->cli_rpc_sem);
473 spin_lock(&cli->cli_lock);
474 list_del_init(&req->rq_list);
475 list_add(&req->rq_list, cli->cli_sending_head.prev);
476 spin_unlock(&cli->cli_lock);
478 CDEBUG(D_OTHER, "-- sleeping\n");
479 wait_event_interruptible(req->rq_wait_for_rep,
480 ptlrpc_check_reply(req));
481 CDEBUG(D_OTHER, "-- done\n");
483 if (req->rq_flags & PTL_RPC_FL_RESEND) {
484 req->rq_flags &= ~PTL_RPC_FL_RESEND;
488 up(&cli->cli_rpc_sem);
489 if (req->rq_flags & PTL_RPC_FL_TIMEOUT)
490 GOTO(out, rc = -ETIMEDOUT);
492 if (req->rq_flags & PTL_RPC_FL_INTR) {
493 /* Clean up the dangling reply buffers */
495 GOTO(out, rc = -EINTR);
498 if (!(req->rq_flags & PTL_RPC_FL_REPLIED))
499 GOTO(out, rc = req->rq_status);
501 rc = lustre_unpack_msg(req->rq_repmsg, req->rq_replen);
503 CERROR("unpack_rep failed: %d\n", rc);
506 CDEBUG(D_NET, "got rep %d\n", req->rq_repmsg->xid);
507 if (req->rq_repmsg->status == 0)
508 CDEBUG(D_NET, "--> buf %p len %d status %d\n", req->rq_repmsg,
509 req->rq_replen, req->rq_repmsg->status);
511 spin_lock(&cli->cli_lock);
512 cli->cli_last_rcvd = req->rq_repmsg->last_rcvd;
513 cli->cli_last_committed = req->rq_repmsg->last_committed;
514 ptlrpc_free_committed(cli);
515 spin_unlock(&cli->cli_lock);
522 int ptlrpc_replay_req(struct ptlrpc_request *req)
525 struct ptlrpc_client *cli = req->rq_client;
528 init_waitqueue_head(&req->rq_wait_for_rep);
529 CERROR("req %Ld opc %d level %d, conn level %d\n",
530 req->rq_xid, req->rq_reqmsg->opc, req->rq_level,
531 req->rq_connection->c_level);
533 req->rq_time = CURRENT_TIME;
535 rc = ptl_send_rpc(req);
537 CERROR("error %d, opcode %d\n", rc, req->rq_reqmsg->opc);
538 ptlrpc_cleanup_request_buf(req);
539 up(&cli->cli_rpc_sem);
543 CDEBUG(D_OTHER, "-- sleeping\n");
544 wait_event_interruptible(req->rq_wait_for_rep,
545 ptlrpc_check_reply(req));
546 CDEBUG(D_OTHER, "-- done\n");
548 up(&cli->cli_rpc_sem);
550 if (!(req->rq_flags & PTL_RPC_FL_REPLIED)) {
551 CERROR("Unknown reason for wakeup\n");
552 /* XXX Phil - I end up here when I kill obdctl */
554 GOTO(out, rc = -EINTR);
557 rc = lustre_unpack_msg(req->rq_repmsg, req->rq_replen);
559 CERROR("unpack_rep failed: %d\n", rc);
563 CDEBUG(D_NET, "got rep %d\n", req->rq_repmsg->xid);
564 if (req->rq_repmsg->status == 0)
565 CDEBUG(D_NET, "--> buf %p len %d status %d\n", req->rq_repmsg,
566 req->rq_replen, req->rq_repmsg->status);
568 CERROR("recovery failed: ");
569 CERROR("req %Ld opc %d level %d, conn level %d\n",
570 req->rq_xid, req->rq_reqmsg->opc, req->rq_level,
571 req->rq_connection->c_level);