1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2003 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 EXPORT_SYMTAB
25 #define DEBUG_SUBSYSTEM S_LDLM
28 # include <linux/module.h>
30 # include <liblustre.h>
32 #include <linux/obd.h>
33 #include <linux/obd_ost.h> /* for LUSTRE_OSC_NAME */
34 #include <linux/lustre_mds.h> /* for LUSTRE_MDC_NAME */
35 #include <linux/lustre_mgmt.h>
36 #include <linux/lustre_dlm.h>
37 #include <linux/lustre_net.h>
39 int client_obd_setup(struct obd_device *obddev, obd_count len, void *buf)
41 struct ptlrpc_connection *conn;
42 struct lustre_cfg* lcfg = buf;
43 struct client_obd *cli = &obddev->u.cli;
44 struct obd_import *imp;
45 struct obd_uuid server_uuid;
46 int rq_portal, rp_portal, connect_op;
47 char *name = obddev->obd_type->typ_name;
48 char *mgmt_name = NULL;
50 struct obd_device *mgmt_obd;
51 mgmtcli_register_for_events_t register_f;
54 /* In a more perfect world, we would hang a ptlrpc_client off of
55 * obd_type and just use the values from there. */
56 if (!strcmp(name, LUSTRE_OSC_NAME)) {
57 rq_portal = OST_REQUEST_PORTAL;
58 rp_portal = OSC_REPLY_PORTAL;
59 connect_op = OST_CONNECT;
60 } else if (!strcmp(name, LUSTRE_MDC_NAME)) {
61 rq_portal = MDS_REQUEST_PORTAL;
62 rp_portal = MDC_REPLY_PORTAL;
63 connect_op = MDS_CONNECT;
64 } else if (!strcmp(name, LUSTRE_MGMTCLI_NAME)) {
65 rq_portal = MGMT_REQUEST_PORTAL;
66 rp_portal = MGMT_REPLY_PORTAL;
67 connect_op = MGMT_CONNECT;
69 CERROR("unknown client OBD type \"%s\", can't setup\n",
74 if (lcfg->lcfg_inllen1 < 1) {
75 CERROR("requires a TARGET UUID\n");
79 if (lcfg->lcfg_inllen1 > 37) {
80 CERROR("client UUID must be less than 38 characters\n");
84 if (lcfg->lcfg_inllen2 < 1) {
85 CERROR("setup requires a SERVER UUID\n");
89 if (lcfg->lcfg_inllen2 > 37) {
90 CERROR("target UUID must be less than 38 characters\n");
95 sema_init(&cli->cl_sem, 1);
96 cli->cl_conn_count = 0;
97 memcpy(server_uuid.uuid, lcfg->lcfg_inlbuf2, MIN(lcfg->lcfg_inllen2,
98 sizeof(server_uuid)));
100 init_MUTEX(&cli->cl_dirty_sem);
102 cli->cl_dirty_granted = 0;
103 cli->cl_dirty_max = OSC_MAX_DIRTY_DEFAULT * 1024 * 1024;
104 cli->cl_ost_can_grant = 1;
105 INIT_LIST_HEAD(&cli->cl_cache_waiters);
106 INIT_LIST_HEAD(&cli->cl_loi_ready_list);
107 INIT_LIST_HEAD(&cli->cl_loi_write_list);
108 spin_lock_init(&cli->cl_loi_list_lock);
109 cli->cl_brw_in_flight = 0;
110 spin_lock_init(&cli->cl_read_rpc_hist.oh_lock);
111 spin_lock_init(&cli->cl_write_rpc_hist.oh_lock);
112 spin_lock_init(&cli->cl_read_page_hist.oh_lock);
113 spin_lock_init(&cli->cl_write_page_hist.oh_lock);
114 cli->cl_max_pages_per_rpc = PTL_MD_MAX_PAGES;
115 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT;
119 CERROR("ldlm_get_ref failed: %d\n", rc);
123 conn = ptlrpc_uuid_to_connection(&server_uuid);
125 GOTO(err_ldlm, rc = -ENOENT);
127 ptlrpc_init_client(rq_portal, rp_portal, name,
128 &obddev->obd_ldlm_client);
130 imp = class_new_import();
132 ptlrpc_put_connection(conn);
133 GOTO(err_ldlm, rc = -ENOENT);
135 imp->imp_connection = conn;
136 imp->imp_client = &obddev->obd_ldlm_client;
137 imp->imp_obd = obddev;
138 imp->imp_connect_op = connect_op;
139 imp->imp_generation = 0;
140 imp->imp_initial_recov = 1;
141 INIT_LIST_HEAD(&imp->imp_pinger_chain);
142 memcpy(imp->imp_target_uuid.uuid, lcfg->lcfg_inlbuf1,
144 class_import_put(imp);
146 cli->cl_import = imp;
147 cli->cl_max_mds_easize = sizeof(struct lov_mds_md);
148 cli->cl_max_mds_cookiesize = sizeof(struct llog_cookie);
149 cli->cl_sandev = to_kdev_t(0);
151 if (lcfg->lcfg_inllen3 != 0) {
152 if (!strcmp(lcfg->lcfg_inlbuf3, "inactive")) {
153 CDEBUG(D_HA, "marking %s %s->%s as inactive\n",
154 name, obddev->obd_name,
155 imp->imp_target_uuid.uuid);
156 imp->imp_invalid = 1;
158 if (lcfg->lcfg_inllen4 != 0)
159 mgmt_name = lcfg->lcfg_inlbuf4;
161 mgmt_name = lcfg->lcfg_inlbuf3;
165 if (mgmt_name != NULL) {
166 /* Register with management client if we need to. */
167 CDEBUG(D_HA, "%s registering with %s for events about %s\n",
168 obddev->obd_name, mgmt_name, server_uuid.uuid);
170 mgmt_obd = class_name2obd(mgmt_name);
172 CERROR("can't find mgmtcli %s to register\n",
174 GOTO(err_import, rc = -ENOSYS);
177 register_f = inter_module_get("mgmtcli_register_for_events");
179 CERROR("can't i_m_g mgmtcli_register_for_events\n");
180 GOTO(err_import, rc = -ENOSYS);
183 rc = register_f(mgmt_obd, obddev, &imp->imp_target_uuid);
184 inter_module_put("mgmtcli_register_for_events");
187 cli->cl_mgmtcli_obd = mgmt_obd;
193 class_destroy_import(imp);
201 int client_obd_cleanup(struct obd_device *obddev, int flags)
203 struct client_obd *cli = &obddev->u.cli;
207 if (cli->cl_mgmtcli_obd) {
208 mgmtcli_deregister_for_events_t dereg_f;
210 dereg_f = inter_module_get("mgmtcli_deregister_for_events");
211 dereg_f(cli->cl_mgmtcli_obd, obddev);
212 inter_module_put("mgmtcli_deregister_for_events");
214 class_destroy_import(cli->cl_import);
215 cli->cl_import = NULL;
217 ldlm_put_ref(flags & OBD_OPT_FORCE);
222 int client_connect_import(struct lustre_handle *dlm_handle,
223 struct obd_device *obd,
224 struct obd_uuid *cluuid)
226 struct client_obd *cli = &obd->u.cli;
227 struct obd_import *imp = cli->cl_import;
228 struct obd_export *exp;
233 rc = class_connect(dlm_handle, obd, cluuid);
237 cli->cl_conn_count++;
238 if (cli->cl_conn_count > 1)
240 exp = class_conn2export(dlm_handle);
242 if (obd->obd_namespace != NULL)
243 CERROR("already have namespace!\n");
244 obd->obd_namespace = ldlm_namespace_new(obd->obd_name,
245 LDLM_NAMESPACE_CLIENT);
246 if (obd->obd_namespace == NULL)
247 GOTO(out_disco, rc = -ENOMEM);
249 imp->imp_dlm_handle = *dlm_handle;
250 rc = ptlrpc_init_import(imp);
254 exp->exp_connection = ptlrpc_connection_addref(imp->imp_connection);
255 rc = ptlrpc_connect_import(imp, NULL);
257 LASSERT (imp->imp_state == LUSTRE_IMP_DISCON);
265 ldlm_namespace_free(obd->obd_namespace, 0);
266 obd->obd_namespace = NULL;
268 cli->cl_conn_count--;
269 class_disconnect(exp, 0);
271 class_export_put(exp);
278 int client_disconnect_export(struct obd_export *exp, int failover)
280 struct obd_device *obd = class_exp2obd(exp);
281 struct client_obd *cli = &obd->u.cli;
282 struct obd_import *imp = cli->cl_import;
287 CERROR("invalid export for disconnect: exp %p cookie "LPX64"\n",
288 exp, exp ? exp->exp_handle.h_cookie : -1);
293 if (!cli->cl_conn_count) {
294 CERROR("disconnecting disconnected device (%s)\n",
296 GOTO(out_sem, rc = -EINVAL);
299 cli->cl_conn_count--;
300 if (cli->cl_conn_count)
301 GOTO(out_no_disconnect, rc = 0);
303 /* Some non-replayable imports (MDS's OSCs) are pinged, so just
304 * delete it regardless. (It's safe to delete an import that was
306 (void)ptlrpc_pinger_del_import(imp);
308 if (obd->obd_namespace != NULL) {
309 /* obd_no_recov == local only */
310 ldlm_cli_cancel_unused(obd->obd_namespace, NULL,
311 obd->obd_no_recov, NULL);
312 ldlm_namespace_free(obd->obd_namespace, obd->obd_no_recov);
313 obd->obd_namespace = NULL;
316 /* Yeah, obd_no_recov also (mainly) means "forced shutdown". */
317 if (obd->obd_no_recov)
318 ptlrpc_set_import_active(imp, 0);
320 rc = ptlrpc_disconnect_import(imp);
324 err = class_disconnect(exp, 0);
332 /* --------------------------------------------------------------------------
333 * from old lib/target.c
334 * -------------------------------------------------------------------------- */
336 int target_handle_reconnect(struct lustre_handle *conn, struct obd_export *exp,
337 struct obd_uuid *cluuid)
339 if (exp->exp_connection) {
340 struct lustre_handle *hdl;
341 hdl = &exp->exp_imp_reverse->imp_remote_handle;
342 /* Might be a re-connect after a partition. */
343 if (!memcmp(&conn->cookie, &hdl->cookie, sizeof conn->cookie)) {
344 CERROR("%s reconnecting\n", cluuid->uuid);
345 conn->cookie = exp->exp_handle.h_cookie;
348 CERROR("%s reconnecting from %s, "
349 "handle mismatch (ours "LPX64", theirs "
350 LPX64")\n", cluuid->uuid,
351 exp->exp_connection->c_remote_uuid.uuid,
352 hdl->cookie, conn->cookie);
353 memset(conn, 0, sizeof *conn);
358 conn->cookie = exp->exp_handle.h_cookie;
359 CDEBUG(D_INFO, "existing export for UUID '%s' at %p\n",
361 CDEBUG(D_IOCTL,"connect: cookie "LPX64"\n", conn->cookie);
365 int target_handle_connect(struct ptlrpc_request *req, svc_handler_t handler)
367 struct obd_device *target;
368 struct obd_export *export = NULL;
369 struct obd_import *revimp;
370 struct lustre_handle conn;
371 struct obd_uuid tgtuuid;
372 struct obd_uuid cluuid;
373 struct obd_uuid remote_uuid;
376 int rc = 0, abort_recovery;
379 LASSERT_REQSWAB (req, 0);
380 str = lustre_msg_string(req->rq_reqmsg, 0, sizeof(tgtuuid) - 1);
382 CERROR("bad target UUID for connect\n");
383 GOTO(out, rc = -EINVAL);
386 obd_str2uuid (&tgtuuid, str);
387 target = class_uuid2obd(&tgtuuid);
389 target = class_name2obd(str);
392 if (!target || target->obd_stopping || !target->obd_set_up) {
393 CERROR("UUID '%s' is not available for connect\n", str);
394 GOTO(out, rc = -ENODEV);
397 LASSERT_REQSWAB (req, 1);
398 str = lustre_msg_string(req->rq_reqmsg, 1, sizeof(cluuid) - 1);
400 CERROR("bad client UUID for connect\n");
401 GOTO(out, rc = -EINVAL);
404 obd_str2uuid (&cluuid, str);
406 /* XXX extract a nettype and format accordingly */
407 snprintf(remote_uuid.uuid, sizeof remote_uuid,
408 "NET_"LPX64"_UUID", req->rq_peer.peer_nid);
410 spin_lock_bh(&target->obd_processing_task_lock);
411 abort_recovery = target->obd_abort_recovery;
412 spin_unlock_bh(&target->obd_processing_task_lock);
414 target_abort_recovery(target);
416 tmp = lustre_msg_buf(req->rq_reqmsg, 2, sizeof conn);
418 GOTO(out, rc = -EPROTO);
420 memcpy(&conn, tmp, sizeof conn);
422 rc = lustre_pack_reply(req, 0, NULL, NULL);
426 /* lctl gets a backstage, all-access pass. */
427 if (obd_uuid_equals(&cluuid, &target->obd_uuid))
428 goto dont_check_exports;
430 spin_lock(&target->obd_dev_lock);
431 list_for_each(p, &target->obd_exports) {
432 export = list_entry(p, struct obd_export, exp_obd_chain);
433 if (obd_uuid_equals(&cluuid, &export->exp_client_uuid)) {
434 spin_unlock(&target->obd_dev_lock);
435 LASSERT(export->exp_obd == target);
437 rc = target_handle_reconnect(&conn, export, &cluuid);
442 /* If we found an export, we already unlocked. */
444 spin_unlock(&target->obd_dev_lock);
445 } else if (req->rq_reqmsg->conn_cnt == 1) {
446 CERROR("%s reconnected with 1 conn_cnt; cookies not random?\n",
448 GOTO(out, rc = -EALREADY);
451 /* Tell the client if we're in recovery. */
452 /* If this is the first client, start the recovery timer */
453 if (target->obd_recovering) {
454 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
455 target_start_recovery_timer(target, handler);
458 /* Tell the client if we support replayable requests */
459 if (target->obd_replayable)
460 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
462 if (export == NULL) {
463 if (target->obd_recovering) {
464 CERROR("denying connection for new client %s: "
465 "%d clients in recovery for %lds\n", cluuid.uuid,
466 target->obd_recoverable_clients,
467 (target->obd_recovery_timer.expires-jiffies)/HZ);
471 rc = obd_connect(&conn, target, &cluuid);
475 /* If all else goes well, this is our RPC return code. */
478 if (rc && rc != EALREADY)
481 /* XXX track this all the time? */
482 if (target->obd_recovering) {
483 target->obd_connected_clients++;
486 req->rq_repmsg->handle = conn;
488 /* If the client and the server are the same node, we will already
489 * have an export that really points to the client's DLM export,
490 * because we have a shared handles table.
492 * XXX this will go away when shaver stops sending the "connect" handle
493 * in the real "remote handle" field of the request --phik 24 Apr 2003
495 if (req->rq_export != NULL)
496 class_export_put(req->rq_export);
498 /* ownership of this export ref transfers to the request */
499 export = req->rq_export = class_conn2export(&conn);
500 LASSERT(export != NULL);
502 if (export->exp_connection != NULL)
503 ptlrpc_put_connection(export->exp_connection);
504 export->exp_connection = ptlrpc_get_connection(&req->rq_peer,
507 LASSERT(export->exp_conn_cnt < req->rq_reqmsg->conn_cnt);
508 export->exp_conn_cnt = req->rq_reqmsg->conn_cnt;
510 if (rc == EALREADY) {
511 /* We indicate the reconnection in a flag, not an error code. */
512 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECONNECT);
516 memcpy(&conn, lustre_msg_buf(req->rq_reqmsg, 2, sizeof conn),
519 if (export->exp_imp_reverse != NULL)
520 class_destroy_import(export->exp_imp_reverse);
521 revimp = export->exp_imp_reverse = class_new_import();
522 revimp->imp_connection = ptlrpc_connection_addref(export->exp_connection);
523 revimp->imp_client = &export->exp_obd->obd_ldlm_client;
524 revimp->imp_remote_handle = conn;
525 revimp->imp_obd = target;
526 revimp->imp_dlm_fake = 1;
527 revimp->imp_state = LUSTRE_IMP_FULL;
528 class_import_put(revimp);
535 int target_handle_disconnect(struct ptlrpc_request *req)
537 struct obd_export *exp;
541 rc = lustre_pack_reply(req, 0, NULL, NULL);
545 /* keep the rq_export around so we can send the reply */
546 exp = class_export_get(req->rq_export);
547 req->rq_status = obd_disconnect(exp, 0);
551 void target_destroy_export(struct obd_export *exp)
553 /* exports created from last_rcvd data, and "fake"
554 exports created by lctl don't have an import */
555 if (exp->exp_imp_reverse != NULL)
556 class_destroy_import(exp->exp_imp_reverse);
558 /* We cancel locks at disconnect time, but this will catch any locks
559 * granted in a race with recovery-induced disconnect. */
560 ldlm_cancel_locks_for_export(exp);
567 static void abort_delayed_replies(struct obd_device *obd)
569 struct ptlrpc_request *req;
570 struct list_head *tmp, *n;
571 list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
572 req = list_entry(tmp, struct ptlrpc_request, rq_list);
573 DEBUG_REQ(D_ERROR, req, "aborted:");
574 req->rq_status = -ENOTCONN;
575 req->rq_type = PTL_RPC_MSG_ERR;
577 class_export_put(req->rq_export);
578 list_del(&req->rq_list);
579 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
580 OBD_FREE(req, sizeof *req);
584 static void abort_recovery_queue(struct obd_device *obd)
586 struct ptlrpc_request *req;
587 struct list_head *tmp, *n;
590 list_for_each_safe(tmp, n, &obd->obd_recovery_queue) {
591 req = list_entry(tmp, struct ptlrpc_request, rq_list);
592 DEBUG_REQ(D_ERROR, req, "aborted:");
593 req->rq_status = -ENOTCONN;
594 req->rq_type = PTL_RPC_MSG_ERR;
595 rc = lustre_pack_reply(req, 0, NULL, NULL);
599 DEBUG_REQ(D_ERROR, req,
600 "packing failed for abort-reply; skipping");
602 list_del(&req->rq_list);
603 class_export_put(req->rq_export);
604 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
605 OBD_FREE(req, sizeof *req);
609 void target_abort_recovery(void *data)
611 struct obd_device *obd = data;
614 CERROR("disconnecting clients and aborting recovery\n");
615 spin_lock_bh(&obd->obd_processing_task_lock);
616 if (!obd->obd_recovering) {
617 spin_unlock_bh(&obd->obd_processing_task_lock);
622 obd->obd_recovering = obd->obd_abort_recovery = 0;
624 wake_up(&obd->obd_next_transno_waitq);
625 target_cancel_recovery_timer(obd);
626 spin_unlock_bh(&obd->obd_processing_task_lock);
628 class_disconnect_exports(obd, 0);
630 /* when recovery was aborted, cleanup orphans on mds and ost */
631 if (OBT(obd) && OBP(obd, postrecov)) {
632 rc = OBP(obd, postrecov)(obd);
634 CWARN("Cleanup %d orphans after recovery was aborted\n", rc);
636 CERROR("postrecov failed %d\n", rc);
639 abort_delayed_replies(obd);
640 abort_recovery_queue(obd);
641 ptlrpc_run_recovery_over_upcall(obd);
644 static void target_recovery_expired(unsigned long castmeharder)
646 struct obd_device *obd = (struct obd_device *)castmeharder;
647 CERROR("recovery timed out, aborting\n");
648 spin_lock_bh(&obd->obd_processing_task_lock);
649 obd->obd_abort_recovery = 1;
650 wake_up(&obd->obd_next_transno_waitq);
651 spin_unlock_bh(&obd->obd_processing_task_lock);
655 /* obd_processing_task_lock should be held */
656 void target_cancel_recovery_timer(struct obd_device *obd)
658 CDEBUG(D_HA, "%s: cancel recovery timer\n", obd->obd_name);
659 del_timer(&obd->obd_recovery_timer);
662 static void reset_recovery_timer(struct obd_device *obd)
664 spin_lock_bh(&obd->obd_processing_task_lock);
665 if (!obd->obd_recovering) {
666 spin_unlock_bh(&obd->obd_processing_task_lock);
669 CDEBUG(D_HA, "timer will expire in %u seconds\n",
670 OBD_RECOVERY_TIMEOUT / HZ);
671 mod_timer(&obd->obd_recovery_timer, jiffies + OBD_RECOVERY_TIMEOUT);
672 spin_unlock_bh(&obd->obd_processing_task_lock);
676 /* Only start it the first time called */
677 void target_start_recovery_timer(struct obd_device *obd, svc_handler_t handler)
679 spin_lock_bh(&obd->obd_processing_task_lock);
680 if (obd->obd_recovery_handler) {
681 spin_unlock_bh(&obd->obd_processing_task_lock);
684 CWARN("%s: starting recovery timer (%us)\n", obd->obd_name,
685 OBD_RECOVERY_TIMEOUT / HZ);
686 obd->obd_recovery_handler = handler;
687 obd->obd_recovery_timer.function = target_recovery_expired;
688 obd->obd_recovery_timer.data = (unsigned long)obd;
689 init_timer(&obd->obd_recovery_timer);
690 spin_unlock_bh(&obd->obd_processing_task_lock);
692 reset_recovery_timer(obd);
695 static int check_for_next_transno(struct obd_device *obd)
697 struct ptlrpc_request *req;
698 int wake_up = 0, connected, completed, queue_len, max;
699 __u64 next_transno, req_transno;
701 spin_lock_bh(&obd->obd_processing_task_lock);
702 req = list_entry(obd->obd_recovery_queue.next,
703 struct ptlrpc_request, rq_list);
704 max = obd->obd_max_recoverable_clients;
705 req_transno = req->rq_reqmsg->transno;
706 connected = obd->obd_connected_clients;
707 completed = max - obd->obd_recoverable_clients;
708 queue_len = obd->obd_requests_queued_for_recovery;
709 next_transno = obd->obd_next_recovery_transno;
711 if (obd->obd_abort_recovery) {
712 CDEBUG(D_HA, "waking for aborted recovery\n");
714 } else if (!obd->obd_recovering) {
715 CDEBUG(D_HA, "waking for completed recovery (?)\n");
717 } else if (req_transno == next_transno) {
718 CDEBUG(D_HA, "waking for next ("LPD64")\n", next_transno);
720 } else if (queue_len + completed == max) {
722 "waking for skipped transno (skip: "LPD64
723 ", ql: %d, comp: %d, conn: %d, next: "LPD64")\n",
724 next_transno, queue_len, completed, max, req_transno);
725 obd->obd_next_recovery_transno = req_transno;
728 spin_unlock_bh(&obd->obd_processing_task_lock);
729 LASSERT(req->rq_reqmsg->transno >= next_transno);
733 static void process_recovery_queue(struct obd_device *obd)
735 struct ptlrpc_request *req;
736 int abort_recovery = 0;
737 struct l_wait_info lwi = { 0 };
741 spin_lock_bh(&obd->obd_processing_task_lock);
742 LASSERT(obd->obd_processing_task == current->pid);
743 req = list_entry(obd->obd_recovery_queue.next,
744 struct ptlrpc_request, rq_list);
746 if (req->rq_reqmsg->transno != obd->obd_next_recovery_transno) {
747 spin_unlock_bh(&obd->obd_processing_task_lock);
748 CDEBUG(D_HA, "Waiting for transno "LPD64" (1st is "
750 obd->obd_next_recovery_transno,
751 req->rq_reqmsg->transno);
752 l_wait_event(obd->obd_next_transno_waitq,
753 check_for_next_transno(obd), &lwi);
754 spin_lock_bh(&obd->obd_processing_task_lock);
755 abort_recovery = obd->obd_abort_recovery;
756 spin_unlock_bh(&obd->obd_processing_task_lock);
757 if (abort_recovery) {
758 target_abort_recovery(obd);
763 list_del_init(&req->rq_list);
764 obd->obd_requests_queued_for_recovery--;
765 spin_unlock_bh(&obd->obd_processing_task_lock);
767 DEBUG_REQ(D_HA, req, "processing: ");
768 (void)obd->obd_recovery_handler(req);
769 obd->obd_replayed_requests++;
770 reset_recovery_timer(obd);
771 /* bug 1580: decide how to properly sync() in recovery */
772 //mds_fsync_super(mds->mds_sb);
773 class_export_put(req->rq_export);
774 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
775 OBD_FREE(req, sizeof *req);
776 spin_lock_bh(&obd->obd_processing_task_lock);
777 obd->obd_next_recovery_transno++;
778 if (list_empty(&obd->obd_recovery_queue)) {
779 obd->obd_processing_task = 0;
780 spin_unlock_bh(&obd->obd_processing_task_lock);
783 spin_unlock_bh(&obd->obd_processing_task_lock);
788 int target_queue_recovery_request(struct ptlrpc_request *req,
789 struct obd_device *obd)
791 struct list_head *tmp;
793 __u64 transno = req->rq_reqmsg->transno;
794 struct ptlrpc_request *saved_req;
795 struct lustre_msg *reqmsg;
797 /* CAVEAT EMPTOR: The incoming request message has been swabbed
798 * (i.e. buflens etc are in my own byte order), but type-dependent
799 * buffers (eg mds_body, ost_body etc) have NOT been swabbed. */
802 INIT_LIST_HEAD(&req->rq_list);
803 DEBUG_REQ(D_HA, req, "not queueing");
807 /* XXX If I were a real man, these LBUGs would be sane cleanups. */
808 /* XXX just like the request-dup code in queue_final_reply */
809 OBD_ALLOC(saved_req, sizeof *saved_req);
812 OBD_ALLOC(reqmsg, req->rq_reqlen);
816 spin_lock_bh(&obd->obd_processing_task_lock);
818 /* If we're processing the queue, we want don't want to queue this
821 * Also, if this request has a transno less than the one we're waiting
822 * for, we should process it now. It could (and currently always will)
823 * be an open request for a descriptor that was opened some time ago.
825 if (obd->obd_processing_task == current->pid ||
826 transno < obd->obd_next_recovery_transno) {
827 /* Processing the queue right now, don't re-add. */
828 LASSERT(list_empty(&req->rq_list));
829 spin_unlock_bh(&obd->obd_processing_task_lock);
830 OBD_FREE(reqmsg, req->rq_reqlen);
831 OBD_FREE(saved_req, sizeof *saved_req);
835 memcpy(saved_req, req, sizeof *req);
836 memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
838 req->rq_reqmsg = reqmsg;
839 class_export_get(req->rq_export);
840 INIT_LIST_HEAD(&req->rq_list);
843 list_for_each(tmp, &obd->obd_recovery_queue) {
844 struct ptlrpc_request *reqiter =
845 list_entry(tmp, struct ptlrpc_request, rq_list);
847 if (reqiter->rq_reqmsg->transno > transno) {
848 list_add_tail(&req->rq_list, &reqiter->rq_list);
855 list_add_tail(&req->rq_list, &obd->obd_recovery_queue);
858 obd->obd_requests_queued_for_recovery++;
860 if (obd->obd_processing_task != 0) {
861 /* Someone else is processing this queue, we'll leave it to
864 wake_up(&obd->obd_next_transno_waitq);
865 spin_unlock_bh(&obd->obd_processing_task_lock);
869 /* Nobody is processing, and we know there's (at least) one to process
870 * now, so we'll do the honours.
872 obd->obd_processing_task = current->pid;
873 spin_unlock_bh(&obd->obd_processing_task_lock);
875 process_recovery_queue(obd);
879 struct obd_device * target_req2obd(struct ptlrpc_request *req)
881 return req->rq_export->exp_obd;
884 int target_queue_final_reply(struct ptlrpc_request *req, int rc)
886 struct obd_device *obd = target_req2obd(req);
887 struct ptlrpc_request *saved_req;
888 struct lustre_msg *reqmsg;
889 int recovery_done = 0;
893 /* Just like ptlrpc_error, but without the sending. */
894 rc = lustre_pack_reply(req, 0, NULL, NULL);
895 LASSERT(rc == 0); /* XXX handle this */
896 req->rq_type = PTL_RPC_MSG_ERR;
899 LASSERT(list_empty(&req->rq_list));
900 /* XXX a bit like the request-dup code in queue_recovery_request */
901 OBD_ALLOC(saved_req, sizeof *saved_req);
904 OBD_ALLOC(reqmsg, req->rq_reqlen);
907 memcpy(saved_req, req, sizeof *saved_req);
908 memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
910 req->rq_reqmsg = reqmsg;
911 class_export_get(req->rq_export);
912 list_add(&req->rq_list, &obd->obd_delayed_reply_queue);
914 spin_lock_bh(&obd->obd_processing_task_lock);
915 --obd->obd_recoverable_clients;
916 recovery_done = (obd->obd_recoverable_clients == 0);
917 spin_unlock_bh(&obd->obd_processing_task_lock);
920 struct list_head *tmp, *n;
921 ldlm_reprocess_all_ns(req->rq_export->exp_obd->obd_namespace);
922 CWARN("%s: all clients recovered, sending delayed replies\n",
924 spin_lock_bh(&obd->obd_processing_task_lock);
925 obd->obd_recovering = 0;
926 target_cancel_recovery_timer(obd);
927 spin_unlock_bh(&obd->obd_processing_task_lock);
929 /* when recovery finished, cleanup orphans on mds and ost */
930 if (OBT(obd) && OBP(obd, postrecov)) {
931 rc2 = OBP(obd, postrecov)(obd);
933 CWARN("%s: all clients recovered, %d MDS "
934 "orphans deleted\n", obd->obd_name, rc2);
936 CERROR("postrecov failed %d\n", rc2);
939 list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
940 req = list_entry(tmp, struct ptlrpc_request, rq_list);
941 DEBUG_REQ(D_ERROR, req, "delayed:");
943 class_export_put(req->rq_export);
944 list_del(&req->rq_list);
945 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
946 OBD_FREE(req, sizeof *req);
948 ptlrpc_run_recovery_over_upcall(obd);
950 CWARN("%s: %d recoverable clients remain\n",
951 obd->obd_name, obd->obd_recoverable_clients);
952 wake_up(&obd->obd_next_transno_waitq);
958 static void ptlrpc_abort_reply (struct ptlrpc_request *req)
960 /* On return, we must be sure that the ACK callback has either
961 * happened or will not happen. Note that the SENT callback will
962 * happen come what may since we successfully posted the PUT. */
964 struct l_wait_info lwi;
968 /* serialise with ACK callback */
969 spin_lock_irqsave (&req->rq_lock, flags);
970 if (!req->rq_want_ack) {
971 spin_unlock_irqrestore (&req->rq_lock, flags);
972 /* The ACK callback has happened already. Although the
973 * SENT callback might still be outstanding (yes really) we
974 * don't care; this is just like normal completion. */
977 spin_unlock_irqrestore (&req->rq_lock, flags);
979 /* Have a bash at unlinking the MD. This will fail until the SENT
980 * callback has happened since the MD is busy from the PUT. If the
981 * ACK still hasn't arrived after then, a successful unlink will
982 * ensure the ACK callback never happens. */
983 rc = PtlMDUnlink (req->rq_reply_md_h);
988 /* SENT callback happened; ACK callback preempted */
989 LASSERT (req->rq_want_ack);
990 spin_lock_irqsave (&req->rq_lock, flags);
991 req->rq_want_ack = 0;
992 spin_unlock_irqrestore (&req->rq_lock, flags);
997 /* Still sending or ACK callback in progress: wait until
998 * either callback has completed and try again.
999 * Actually we can't wait for the SENT callback because
1000 * there's no state the SENT callback can touch that will
1001 * allow it to communicate with us! So we just wait here
1002 * for a short time, effectively polling for the SENT
1003 * callback by calling PtlMDUnlink() again, to see if it
1004 * has finished. Note that if the ACK does arrive, its
1005 * callback wakes us in short order. --eeb */
1006 lwi = LWI_TIMEOUT (HZ/4, NULL, NULL);
1007 rc = l_wait_event(req->rq_reply_waitq, !req->rq_want_ack,
1009 CDEBUG (D_HA, "Retrying req %p: %d\n", req, rc);
1010 /* NB go back and test rq_want_ack with locking, to ensure
1011 * if ACK callback happened, it has completed stopped
1012 * referencing this req. */
1017 void target_send_reply(struct ptlrpc_request *req, int rc, int fail_id)
1021 unsigned long flags;
1022 struct ptlrpc_req_ack_lock *ack_lock;
1023 struct l_wait_info lwi = { 0 };
1024 wait_queue_t commit_wait;
1025 struct obd_device *obd =
1026 req->rq_export ? req->rq_export->exp_obd : NULL;
1027 struct obd_export *exp = NULL;
1029 if (req->rq_export) {
1030 for (i = 0; i < REQ_MAX_ACK_LOCKS; i++) {
1031 if (req->rq_ack_locks[i].mode) {
1032 exp = req->rq_export;
1039 exp->exp_outstanding_reply = req;
1040 spin_lock_irqsave (&req->rq_lock, flags);
1041 req->rq_want_ack = 1;
1042 spin_unlock_irqrestore (&req->rq_lock, flags);
1045 if (!OBD_FAIL_CHECK(fail_id | OBD_FAIL_ONCE)) {
1047 DEBUG_REQ(D_NET, req, "sending reply");
1048 netrc = ptlrpc_reply(req);
1049 } else if (rc == -ENOTCONN) {
1050 DEBUG_REQ(D_HA, req, "processing error (%d)", rc);
1051 netrc = ptlrpc_error(req);
1053 DEBUG_REQ(D_ERROR, req, "processing error (%d)", rc);
1054 netrc = ptlrpc_error(req);
1057 obd_fail_loc |= OBD_FAIL_ONCE | OBD_FAILED;
1058 DEBUG_REQ(D_ERROR, req, "dropping reply");
1059 if (req->rq_repmsg) {
1060 OBD_FREE(req->rq_repmsg, req->rq_replen);
1061 req->rq_repmsg = NULL;
1063 init_waitqueue_head(&req->rq_reply_waitq);
1067 /* a failed send simulates the callbacks */
1068 LASSERT(netrc == 0 || req->rq_want_ack == 0);
1070 LASSERT(req->rq_want_ack == 0);
1073 LASSERT(obd != NULL);
1075 init_waitqueue_entry(&commit_wait, current);
1076 add_wait_queue(&obd->obd_commit_waitq, &commit_wait);
1077 rc = l_wait_event(req->rq_reply_waitq,
1078 !req->rq_want_ack || req->rq_resent ||
1079 req->rq_transno <= obd->obd_last_committed, &lwi);
1080 remove_wait_queue(&obd->obd_commit_waitq, &commit_wait);
1082 spin_lock_irqsave (&req->rq_lock, flags);
1083 /* If we got here because the ACK callback ran, this acts as a
1084 * barrier to ensure the callback completed the wakeup. */
1085 spin_unlock_irqrestore (&req->rq_lock, flags);
1087 /* If we committed the transno already, then we might wake up before
1088 * the ack arrives. We need to stop waiting for the ack before we can
1089 * reuse this request structure. We are guaranteed by this point that
1090 * this cannot abort the sending of the actual reply.*/
1091 ptlrpc_abort_reply(req);
1093 if (req->rq_resent) {
1094 DEBUG_REQ(D_HA, req, "resent: not cancelling locks");
1099 DEBUG_REQ(D_HA, req, "cancelling locks for %s",
1100 req->rq_want_ack ? "commit" : "ack");
1102 exp->exp_outstanding_reply = NULL;
1104 for (ack_lock = req->rq_ack_locks, i = 0;
1105 i < REQ_MAX_ACK_LOCKS; i++, ack_lock++) {
1106 if (!ack_lock->mode)
1108 ldlm_lock_decref(&ack_lock->lock, ack_lock->mode);
1112 int target_handle_ping(struct ptlrpc_request *req)
1114 return lustre_pack_reply(req, 0, NULL, NULL);
1117 void *ldlm_put_lock_into_req(struct ptlrpc_request *req,
1118 struct lustre_handle *lock, int mode)
1122 for (i = 0; i < REQ_MAX_ACK_LOCKS; i++) {
1123 if (req->rq_ack_locks[i].mode)
1125 CDEBUG(D_HA, "saving lock "LPX64" in req %p ack_lock[%d]\n",
1126 lock->cookie, req, i);
1127 memcpy(&req->rq_ack_locks[i].lock, lock, sizeof(*lock));
1128 req->rq_ack_locks[i].mode = mode;
1129 return &req->rq_ack_locks[i];
1131 CERROR("no space for lock in struct ptlrpc_request\n");