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");
94 sema_init(&cli->cl_sem, 1);
95 cli->cl_conn_count = 0;
96 memcpy(server_uuid.uuid, lcfg->lcfg_inlbuf2,
97 min_t(unsigned int, lcfg->lcfg_inllen2, sizeof(server_uuid)));
100 cli->cl_avail_grant = 0;
101 cli->cl_dirty_max = OSC_MAX_DIRTY_DEFAULT * 1024 * 1024;
102 INIT_LIST_HEAD(&cli->cl_cache_waiters);
103 INIT_LIST_HEAD(&cli->cl_loi_ready_list);
104 INIT_LIST_HEAD(&cli->cl_loi_write_list);
105 INIT_LIST_HEAD(&cli->cl_loi_read_list);
106 spin_lock_init(&cli->cl_loi_list_lock);
107 cli->cl_brw_in_flight = 0;
108 spin_lock_init(&cli->cl_read_rpc_hist.oh_lock);
109 spin_lock_init(&cli->cl_write_rpc_hist.oh_lock);
110 spin_lock_init(&cli->cl_read_page_hist.oh_lock);
111 spin_lock_init(&cli->cl_write_page_hist.oh_lock);
112 cli->cl_max_pages_per_rpc = PTLRPC_MAX_BRW_PAGES;
113 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT;
117 CERROR("ldlm_get_ref failed: %d\n", rc);
121 conn = ptlrpc_uuid_to_connection(&server_uuid);
123 GOTO(err_ldlm, rc = -ENOENT);
125 ptlrpc_init_client(rq_portal, rp_portal, name,
126 &obddev->obd_ldlm_client);
128 imp = class_new_import();
130 ptlrpc_put_connection(conn);
131 GOTO(err_ldlm, rc = -ENOENT);
133 imp->imp_connection = conn;
134 imp->imp_client = &obddev->obd_ldlm_client;
135 imp->imp_obd = obddev;
136 imp->imp_connect_op = connect_op;
137 imp->imp_generation = 0;
138 imp->imp_initial_recov = 1;
139 INIT_LIST_HEAD(&imp->imp_pinger_chain);
140 memcpy(imp->imp_target_uuid.uuid, lcfg->lcfg_inlbuf1,
142 class_import_put(imp);
144 cli->cl_import = imp;
145 cli->cl_max_mds_easize = sizeof(struct lov_mds_md);
146 cli->cl_max_mds_cookiesize = sizeof(struct llog_cookie);
147 cli->cl_sandev = to_kdev_t(0);
149 if (lcfg->lcfg_inllen3 != 0) {
150 if (!strcmp(lcfg->lcfg_inlbuf3, "inactive")) {
151 CDEBUG(D_HA, "marking %s %s->%s as inactive\n",
152 name, obddev->obd_name,
153 imp->imp_target_uuid.uuid);
154 imp->imp_invalid = 1;
156 if (lcfg->lcfg_inllen4 != 0)
157 mgmt_name = lcfg->lcfg_inlbuf4;
159 mgmt_name = lcfg->lcfg_inlbuf3;
163 if (mgmt_name != NULL) {
164 /* Register with management client if we need to. */
165 CDEBUG(D_HA, "%s registering with %s for events about %s\n",
166 obddev->obd_name, mgmt_name, server_uuid.uuid);
168 mgmt_obd = class_name2obd(mgmt_name);
170 CERROR("can't find mgmtcli %s to register\n",
172 GOTO(err_import, rc = -ENOSYS);
175 register_f = inter_module_get("mgmtcli_register_for_events");
177 CERROR("can't i_m_g mgmtcli_register_for_events\n");
178 GOTO(err_import, rc = -ENOSYS);
181 rc = register_f(mgmt_obd, obddev, &imp->imp_target_uuid);
182 inter_module_put("mgmtcli_register_for_events");
185 cli->cl_mgmtcli_obd = mgmt_obd;
191 class_destroy_import(imp);
199 int client_obd_cleanup(struct obd_device *obddev, int flags)
201 struct client_obd *cli = &obddev->u.cli;
205 if (cli->cl_mgmtcli_obd) {
206 mgmtcli_deregister_for_events_t dereg_f;
208 dereg_f = inter_module_get("mgmtcli_deregister_for_events");
209 dereg_f(cli->cl_mgmtcli_obd, obddev);
210 inter_module_put("mgmtcli_deregister_for_events");
212 class_destroy_import(cli->cl_import);
213 cli->cl_import = NULL;
215 ldlm_put_ref(flags & OBD_OPT_FORCE);
220 int client_connect_import(struct lustre_handle *dlm_handle,
221 struct obd_device *obd,
222 struct obd_uuid *cluuid)
224 struct client_obd *cli = &obd->u.cli;
225 struct obd_import *imp = cli->cl_import;
226 struct obd_export *exp;
231 rc = class_connect(dlm_handle, obd, cluuid);
235 cli->cl_conn_count++;
236 if (cli->cl_conn_count > 1)
238 exp = class_conn2export(dlm_handle);
240 if (obd->obd_namespace != NULL)
241 CERROR("already have namespace!\n");
242 obd->obd_namespace = ldlm_namespace_new(obd->obd_name,
243 LDLM_NAMESPACE_CLIENT);
244 if (obd->obd_namespace == NULL)
245 GOTO(out_disco, rc = -ENOMEM);
247 imp->imp_dlm_handle = *dlm_handle;
248 rc = ptlrpc_init_import(imp);
252 exp->exp_connection = ptlrpc_connection_addref(imp->imp_connection);
253 rc = ptlrpc_connect_import(imp, NULL);
255 LASSERT (imp->imp_state == LUSTRE_IMP_DISCON);
259 ptlrpc_pinger_add_import(imp);
264 ldlm_namespace_free(obd->obd_namespace, 0);
265 obd->obd_namespace = NULL;
267 cli->cl_conn_count--;
268 class_disconnect(exp, 0);
270 class_export_put(exp);
277 int client_disconnect_export(struct obd_export *exp, int failover)
279 struct obd_device *obd = class_exp2obd(exp);
280 struct client_obd *cli = &obd->u.cli;
281 struct obd_import *imp = cli->cl_import;
286 CERROR("invalid export for disconnect: exp %p cookie "LPX64"\n",
287 exp, exp ? exp->exp_handle.h_cookie : -1);
292 if (!cli->cl_conn_count) {
293 CERROR("disconnecting disconnected device (%s)\n",
295 GOTO(out_sem, rc = -EINVAL);
298 cli->cl_conn_count--;
299 if (cli->cl_conn_count)
300 GOTO(out_no_disconnect, rc = 0);
302 /* Some non-replayable imports (MDS's OSCs) are pinged, so just
303 * delete it regardless. (It's safe to delete an import that was
305 (void)ptlrpc_pinger_del_import(imp);
307 if (obd->obd_namespace != NULL) {
308 /* obd_no_recov == local only */
309 ldlm_cli_cancel_unused(obd->obd_namespace, NULL,
310 obd->obd_no_recov, NULL);
311 ldlm_namespace_free(obd->obd_namespace, obd->obd_no_recov);
312 obd->obd_namespace = NULL;
315 /* Yeah, obd_no_recov also (mainly) means "forced shutdown". */
316 if (obd->obd_no_recov)
317 ptlrpc_invalidate_import(imp, 0);
319 rc = ptlrpc_disconnect_import(imp);
323 err = class_disconnect(exp, 0);
331 /* --------------------------------------------------------------------------
332 * from old lib/target.c
333 * -------------------------------------------------------------------------- */
335 int target_handle_reconnect(struct lustre_handle *conn, struct obd_export *exp,
336 struct obd_uuid *cluuid, int initial_conn)
338 if (exp->exp_connection && !initial_conn) {
339 struct lustre_handle *hdl;
340 hdl = &exp->exp_imp_reverse->imp_remote_handle;
341 /* Might be a re-connect after a partition. */
342 if (!memcmp(&conn->cookie, &hdl->cookie, sizeof conn->cookie)) {
343 CERROR("%s reconnecting\n", cluuid->uuid);
344 conn->cookie = exp->exp_handle.h_cookie;
347 CERROR("%s reconnecting from %s, "
348 "handle mismatch (ours "LPX64", theirs "
349 LPX64")\n", cluuid->uuid,
350 exp->exp_connection->c_remote_uuid.uuid,
351 hdl->cookie, conn->cookie);
352 memset(conn, 0, sizeof *conn);
357 conn->cookie = exp->exp_handle.h_cookie;
358 CDEBUG(D_INFO, "existing export for UUID '%s' at %p\n",
360 CDEBUG(D_IOCTL,"connect: cookie "LPX64"\n", conn->cookie);
364 int target_handle_connect(struct ptlrpc_request *req, svc_handler_t handler)
366 struct obd_device *target;
367 struct obd_export *export = NULL;
368 struct obd_import *revimp;
369 struct lustre_handle conn;
370 struct obd_uuid tgtuuid;
371 struct obd_uuid cluuid;
372 struct obd_uuid remote_uuid;
375 int rc = 0, abort_recovery;
377 int initial_conn = 0;
380 OBD_RACE(OBD_FAIL_TGT_CONN_RACE);
382 LASSERT_REQSWAB (req, 0);
383 str = lustre_msg_string(req->rq_reqmsg, 0, sizeof(tgtuuid) - 1);
385 CERROR("bad target UUID for connect\n");
386 GOTO(out, rc = -EINVAL);
389 obd_str2uuid (&tgtuuid, str);
390 target = class_uuid2obd(&tgtuuid);
392 target = class_name2obd(str);
395 if (!target || target->obd_stopping || !target->obd_set_up) {
396 CERROR("UUID '%s' is not available for connect\n", str);
398 GOTO(out, rc = -ENODEV);
401 LASSERT_REQSWAB (req, 1);
402 str = lustre_msg_string(req->rq_reqmsg, 1, sizeof(cluuid) - 1);
404 CERROR("bad client UUID for connect\n");
405 GOTO(out, rc = -EINVAL);
408 obd_str2uuid (&cluuid, str);
410 /* XXX extract a nettype and format accordingly */
411 switch (sizeof(ptl_nid_t)) {
412 /* NB the casts only avoid compiler warnings */
414 snprintf(remote_uuid.uuid, sizeof remote_uuid,
415 "NET_"LPX64"_UUID", (__u64)req->rq_peer.peer_nid);
418 snprintf(remote_uuid.uuid, sizeof remote_uuid,
419 "NET_%x_UUID", (__u32)req->rq_peer.peer_nid);
425 spin_lock_bh(&target->obd_processing_task_lock);
426 abort_recovery = target->obd_abort_recovery;
427 spin_unlock_bh(&target->obd_processing_task_lock);
429 target_abort_recovery(target);
431 tmp = lustre_msg_buf(req->rq_reqmsg, 2, sizeof conn);
433 GOTO(out, rc = -EPROTO);
435 memcpy(&conn, tmp, sizeof conn);
437 rc = lustre_pack_reply(req, 0, NULL, NULL);
441 /* lctl gets a backstage, all-access pass. */
442 if (obd_uuid_equals(&cluuid, &target->obd_uuid))
443 goto dont_check_exports;
445 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_INITIAL)
448 spin_lock(&target->obd_dev_lock);
449 list_for_each(p, &target->obd_exports) {
450 export = list_entry(p, struct obd_export, exp_obd_chain);
451 if (obd_uuid_equals(&cluuid, &export->exp_client_uuid)) {
452 spin_unlock(&target->obd_dev_lock);
453 LASSERT(export->exp_obd == target);
455 rc = target_handle_reconnect(&conn, export, &cluuid,
461 /* If we found an export, we already unlocked. */
463 spin_unlock(&target->obd_dev_lock);
464 } else if (req->rq_reqmsg->conn_cnt == 1 && !initial_conn) {
465 CERROR("%s reconnected with 1 conn_cnt; cookies not random?\n",
467 GOTO(out, rc = -EALREADY);
470 /* Tell the client if we're in recovery. */
471 /* If this is the first client, start the recovery timer */
472 CWARN("%s: connection from %s %s\n", target->obd_name, cluuid.uuid,
473 target->obd_recovering ? "(recovering)" : "");
474 if (target->obd_recovering) {
475 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
476 target_start_recovery_timer(target, handler);
479 /* Tell the client if we support replayable requests */
480 if (target->obd_replayable)
481 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
483 if (export == NULL) {
484 if (target->obd_recovering) {
485 CERROR("denying connection for new client %s: "
486 "%d clients in recovery for %lds\n", cluuid.uuid,
487 target->obd_recoverable_clients,
488 (target->obd_recovery_timer.expires-jiffies)/HZ);
492 rc = obd_connect(&conn, target, &cluuid);
495 /* Tell the client if we support replayable requests */
496 if (target->obd_replayable)
497 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
499 /* If all else goes well, this is our RPC return code. */
502 if (rc && rc != EALREADY)
505 req->rq_repmsg->handle = conn;
507 /* If the client and the server are the same node, we will already
508 * have an export that really points to the client's DLM export,
509 * because we have a shared handles table.
511 * XXX this will go away when shaver stops sending the "connect" handle
512 * in the real "remote handle" field of the request --phik 24 Apr 2003
514 if (req->rq_export != NULL)
515 class_export_put(req->rq_export);
517 /* ownership of this export ref transfers to the request */
518 export = req->rq_export = class_conn2export(&conn);
519 LASSERT(export != NULL);
521 spin_lock_irqsave(&export->exp_lock, flags);
523 req->rq_repmsg->conn_cnt = export->exp_conn_cnt + 1;
524 } else if (export->exp_conn_cnt >= req->rq_reqmsg->conn_cnt) {
525 CERROR("%s: already connected at a higher conn_cnt: %d > %d\n",
526 cluuid.uuid, export->exp_conn_cnt,
527 req->rq_reqmsg->conn_cnt);
528 spin_unlock_irqrestore(&export->exp_lock, flags);
529 GOTO(out, rc = -EALREADY);
531 export->exp_conn_cnt = req->rq_reqmsg->conn_cnt;
532 spin_unlock_irqrestore(&export->exp_lock, flags);
534 /* request from liblustre? */
535 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT)
536 export->exp_libclient = 1;
538 if (export->exp_connection != NULL)
539 ptlrpc_put_connection(export->exp_connection);
540 export->exp_connection = ptlrpc_get_connection(&req->rq_peer,
543 if (rc == EALREADY) {
544 /* We indicate the reconnection in a flag, not an error code. */
545 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECONNECT);
549 if (target->obd_recovering) {
550 target->obd_connected_clients++;
553 memcpy(&conn, lustre_msg_buf(req->rq_reqmsg, 2, sizeof conn),
556 if (export->exp_imp_reverse != NULL)
557 class_destroy_import(export->exp_imp_reverse);
558 revimp = export->exp_imp_reverse = class_new_import();
559 revimp->imp_connection = ptlrpc_connection_addref(export->exp_connection);
560 revimp->imp_client = &export->exp_obd->obd_ldlm_client;
561 revimp->imp_remote_handle = conn;
562 revimp->imp_obd = target;
563 revimp->imp_dlm_fake = 1;
564 revimp->imp_state = LUSTRE_IMP_FULL;
565 class_import_put(revimp);
572 int target_handle_disconnect(struct ptlrpc_request *req)
574 struct obd_export *exp;
578 rc = lustre_pack_reply(req, 0, NULL, NULL);
582 /* keep the rq_export around so we can send the reply */
583 exp = class_export_get(req->rq_export);
584 req->rq_status = obd_disconnect(exp, 0);
588 void target_destroy_export(struct obd_export *exp)
590 /* exports created from last_rcvd data, and "fake"
591 exports created by lctl don't have an import */
592 if (exp->exp_imp_reverse != NULL)
593 class_destroy_import(exp->exp_imp_reverse);
595 /* We cancel locks at disconnect time, but this will catch any locks
596 * granted in a race with recovery-induced disconnect. */
597 if (exp->exp_obd->obd_namespace != NULL)
598 ldlm_cancel_locks_for_export(exp);
605 static void abort_delayed_replies(struct obd_device *obd)
607 struct ptlrpc_request *req;
608 struct list_head *tmp, *n;
609 list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
610 req = list_entry(tmp, struct ptlrpc_request, rq_list);
611 DEBUG_REQ(D_ERROR, req, "aborted:");
612 req->rq_status = -ENOTCONN;
613 req->rq_type = PTL_RPC_MSG_ERR;
615 class_export_put(req->rq_export);
616 list_del(&req->rq_list);
617 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
618 OBD_FREE(req, sizeof *req);
622 static void abort_recovery_queue(struct obd_device *obd)
624 struct ptlrpc_request *req;
625 struct list_head *tmp, *n;
628 list_for_each_safe(tmp, n, &obd->obd_recovery_queue) {
629 req = list_entry(tmp, struct ptlrpc_request, rq_list);
630 DEBUG_REQ(D_ERROR, req, "aborted:");
631 req->rq_status = -ENOTCONN;
632 req->rq_type = PTL_RPC_MSG_ERR;
633 rc = lustre_pack_reply(req, 0, NULL, NULL);
637 DEBUG_REQ(D_ERROR, req,
638 "packing failed for abort-reply; skipping");
640 list_del(&req->rq_list);
641 class_export_put(req->rq_export);
642 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
643 OBD_FREE(req, sizeof *req);
647 void target_abort_recovery(void *data)
649 struct obd_device *obd = data;
652 CERROR("disconnecting clients and aborting recovery\n");
653 spin_lock_bh(&obd->obd_processing_task_lock);
654 if (!obd->obd_recovering) {
655 spin_unlock_bh(&obd->obd_processing_task_lock);
660 obd->obd_recovering = obd->obd_abort_recovery = 0;
662 wake_up(&obd->obd_next_transno_waitq);
663 target_cancel_recovery_timer(obd);
664 spin_unlock_bh(&obd->obd_processing_task_lock);
666 class_disconnect_exports(obd, 0);
668 /* when recovery was aborted, cleanup orphans on mds and ost */
669 if (OBT(obd) && OBP(obd, postrecov)) {
670 rc = OBP(obd, postrecov)(obd);
672 CWARN("Cleanup %d orphans after recovery was aborted\n", rc);
674 CERROR("postrecov failed %d\n", rc);
677 abort_delayed_replies(obd);
678 abort_recovery_queue(obd);
679 ptlrpc_run_recovery_over_upcall(obd);
682 static void target_recovery_expired(unsigned long castmeharder)
684 struct obd_device *obd = (struct obd_device *)castmeharder;
685 CERROR("recovery timed out, aborting\n");
686 spin_lock_bh(&obd->obd_processing_task_lock);
687 obd->obd_abort_recovery = 1;
688 wake_up(&obd->obd_next_transno_waitq);
689 spin_unlock_bh(&obd->obd_processing_task_lock);
693 /* obd_processing_task_lock should be held */
694 void target_cancel_recovery_timer(struct obd_device *obd)
696 CDEBUG(D_HA, "%s: cancel recovery timer\n", obd->obd_name);
697 del_timer(&obd->obd_recovery_timer);
700 static void reset_recovery_timer(struct obd_device *obd)
702 spin_lock_bh(&obd->obd_processing_task_lock);
703 if (!obd->obd_recovering) {
704 spin_unlock_bh(&obd->obd_processing_task_lock);
707 CDEBUG(D_HA, "timer will expire in %u seconds\n",
708 OBD_RECOVERY_TIMEOUT / HZ);
709 mod_timer(&obd->obd_recovery_timer, jiffies + OBD_RECOVERY_TIMEOUT);
710 spin_unlock_bh(&obd->obd_processing_task_lock);
714 /* Only start it the first time called */
715 void target_start_recovery_timer(struct obd_device *obd, svc_handler_t handler)
717 spin_lock_bh(&obd->obd_processing_task_lock);
718 if (obd->obd_recovery_handler) {
719 spin_unlock_bh(&obd->obd_processing_task_lock);
722 CWARN("%s: starting recovery timer (%us)\n", obd->obd_name,
723 OBD_RECOVERY_TIMEOUT / HZ);
724 obd->obd_recovery_handler = handler;
725 obd->obd_recovery_timer.function = target_recovery_expired;
726 obd->obd_recovery_timer.data = (unsigned long)obd;
727 spin_unlock_bh(&obd->obd_processing_task_lock);
729 reset_recovery_timer(obd);
732 static int check_for_next_transno(struct obd_device *obd)
734 struct ptlrpc_request *req;
735 int wake_up = 0, connected, completed, queue_len, max;
736 __u64 next_transno, req_transno;
738 spin_lock_bh(&obd->obd_processing_task_lock);
739 req = list_entry(obd->obd_recovery_queue.next,
740 struct ptlrpc_request, rq_list);
741 max = obd->obd_max_recoverable_clients;
742 req_transno = req->rq_reqmsg->transno;
743 connected = obd->obd_connected_clients;
744 completed = max - obd->obd_recoverable_clients;
745 queue_len = obd->obd_requests_queued_for_recovery;
746 next_transno = obd->obd_next_recovery_transno;
748 CDEBUG(D_HA,"max: %d, connected: %d, completed: %d, queue_len: %d, "
749 "req_transno: "LPU64", next_transno: "LPU64"\n",
750 max, connected, completed, queue_len, req_transno, next_transno);
751 if (obd->obd_abort_recovery) {
752 CDEBUG(D_HA, "waking for aborted recovery\n");
754 } else if (!obd->obd_recovering) {
755 CDEBUG(D_HA, "waking for completed recovery (?)\n");
757 } else if (req_transno == next_transno) {
758 CDEBUG(D_HA, "waking for next ("LPD64")\n", next_transno);
760 } else if (queue_len + completed == max) {
762 "waking for skipped transno (skip: "LPD64
763 ", ql: %d, comp: %d, conn: %d, next: "LPD64")\n",
764 next_transno, queue_len, completed, max, req_transno);
765 obd->obd_next_recovery_transno = req_transno;
768 spin_unlock_bh(&obd->obd_processing_task_lock);
769 LASSERT(req->rq_reqmsg->transno >= next_transno);
773 static void process_recovery_queue(struct obd_device *obd)
775 struct ptlrpc_request *req;
776 int abort_recovery = 0;
777 struct l_wait_info lwi = { 0 };
781 spin_lock_bh(&obd->obd_processing_task_lock);
782 LASSERT(obd->obd_processing_task == current->pid);
783 req = list_entry(obd->obd_recovery_queue.next,
784 struct ptlrpc_request, rq_list);
786 if (req->rq_reqmsg->transno != obd->obd_next_recovery_transno) {
787 spin_unlock_bh(&obd->obd_processing_task_lock);
788 CDEBUG(D_HA, "Waiting for transno "LPD64" (1st is "
790 obd->obd_next_recovery_transno,
791 req->rq_reqmsg->transno);
792 l_wait_event(obd->obd_next_transno_waitq,
793 check_for_next_transno(obd), &lwi);
794 spin_lock_bh(&obd->obd_processing_task_lock);
795 abort_recovery = obd->obd_abort_recovery;
796 spin_unlock_bh(&obd->obd_processing_task_lock);
797 if (abort_recovery) {
798 target_abort_recovery(obd);
803 list_del_init(&req->rq_list);
804 obd->obd_requests_queued_for_recovery--;
805 spin_unlock_bh(&obd->obd_processing_task_lock);
807 DEBUG_REQ(D_HA, req, "processing: ");
808 (void)obd->obd_recovery_handler(req);
809 obd->obd_replayed_requests++;
810 reset_recovery_timer(obd);
811 /* bug 1580: decide how to properly sync() in recovery */
812 //mds_fsync_super(mds->mds_sb);
813 class_export_put(req->rq_export);
814 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
815 OBD_FREE(req, sizeof *req);
816 spin_lock_bh(&obd->obd_processing_task_lock);
817 obd->obd_next_recovery_transno++;
818 if (list_empty(&obd->obd_recovery_queue)) {
819 obd->obd_processing_task = 0;
820 spin_unlock_bh(&obd->obd_processing_task_lock);
823 spin_unlock_bh(&obd->obd_processing_task_lock);
828 int target_queue_recovery_request(struct ptlrpc_request *req,
829 struct obd_device *obd)
831 struct list_head *tmp;
833 __u64 transno = req->rq_reqmsg->transno;
834 struct ptlrpc_request *saved_req;
835 struct lustre_msg *reqmsg;
837 /* CAVEAT EMPTOR: The incoming request message has been swabbed
838 * (i.e. buflens etc are in my own byte order), but type-dependent
839 * buffers (eg mds_body, ost_body etc) have NOT been swabbed. */
842 INIT_LIST_HEAD(&req->rq_list);
843 DEBUG_REQ(D_HA, req, "not queueing");
847 /* XXX If I were a real man, these LBUGs would be sane cleanups. */
848 /* XXX just like the request-dup code in queue_final_reply */
849 OBD_ALLOC(saved_req, sizeof *saved_req);
852 OBD_ALLOC(reqmsg, req->rq_reqlen);
856 spin_lock_bh(&obd->obd_processing_task_lock);
858 /* If we're processing the queue, we want don't want to queue this
861 * Also, if this request has a transno less than the one we're waiting
862 * for, we should process it now. It could (and currently always will)
863 * be an open request for a descriptor that was opened some time ago.
865 * Also, a resent, replayed request that has already been
866 * handled will pass through here and be processed immediately.
868 if (obd->obd_processing_task == current->pid ||
869 transno < obd->obd_next_recovery_transno) {
870 /* Processing the queue right now, don't re-add. */
871 LASSERT(list_empty(&req->rq_list));
872 spin_unlock_bh(&obd->obd_processing_task_lock);
873 OBD_FREE(reqmsg, req->rq_reqlen);
874 OBD_FREE(saved_req, sizeof *saved_req);
878 /* A resent, replayed request that is still on the queue; just drop it.
879 The queued request will handle this. */
880 if ((lustre_msg_get_flags(req->rq_reqmsg) & (MSG_RESENT | MSG_REPLAY)) ==
881 (MSG_RESENT | MSG_REPLAY)) {
882 DEBUG_REQ(D_ERROR, req, "dropping resent queued req");
883 spin_unlock_bh(&obd->obd_processing_task_lock);
884 OBD_FREE(reqmsg, req->rq_reqlen);
885 OBD_FREE(saved_req, sizeof *saved_req);
889 memcpy(saved_req, req, sizeof *req);
890 memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
892 req->rq_reqmsg = reqmsg;
893 class_export_get(req->rq_export);
894 INIT_LIST_HEAD(&req->rq_list);
897 list_for_each(tmp, &obd->obd_recovery_queue) {
898 struct ptlrpc_request *reqiter =
899 list_entry(tmp, struct ptlrpc_request, rq_list);
901 if (reqiter->rq_reqmsg->transno > transno) {
902 list_add_tail(&req->rq_list, &reqiter->rq_list);
909 list_add_tail(&req->rq_list, &obd->obd_recovery_queue);
912 obd->obd_requests_queued_for_recovery++;
914 if (obd->obd_processing_task != 0) {
915 /* Someone else is processing this queue, we'll leave it to
918 wake_up(&obd->obd_next_transno_waitq);
919 spin_unlock_bh(&obd->obd_processing_task_lock);
923 /* Nobody is processing, and we know there's (at least) one to process
924 * now, so we'll do the honours.
926 obd->obd_processing_task = current->pid;
927 spin_unlock_bh(&obd->obd_processing_task_lock);
929 process_recovery_queue(obd);
933 struct obd_device * target_req2obd(struct ptlrpc_request *req)
935 return req->rq_export->exp_obd;
938 int target_queue_final_reply(struct ptlrpc_request *req, int rc)
940 struct obd_device *obd = target_req2obd(req);
941 struct ptlrpc_request *saved_req;
942 struct lustre_msg *reqmsg;
943 int recovery_done = 0;
946 LASSERT ((rc == 0) == (req->rq_reply_state != NULL));
949 /* Just like ptlrpc_error, but without the sending. */
950 rc = lustre_pack_reply(req, 0, NULL, NULL);
951 LASSERT(rc == 0); /* XXX handle this */
952 req->rq_type = PTL_RPC_MSG_ERR;
955 LASSERT (!req->rq_reply_state->rs_difficult);
956 LASSERT(list_empty(&req->rq_list));
957 /* XXX a bit like the request-dup code in queue_recovery_request */
958 OBD_ALLOC(saved_req, sizeof *saved_req);
961 OBD_ALLOC(reqmsg, req->rq_reqlen);
964 memcpy(saved_req, req, sizeof *saved_req);
965 memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
966 /* the copied req takes over the reply state */
967 req->rq_reply_state = NULL;
969 req->rq_reqmsg = reqmsg;
970 class_export_get(req->rq_export);
971 list_add(&req->rq_list, &obd->obd_delayed_reply_queue);
973 spin_lock_bh(&obd->obd_processing_task_lock);
974 --obd->obd_recoverable_clients;
975 recovery_done = (obd->obd_recoverable_clients == 0);
976 spin_unlock_bh(&obd->obd_processing_task_lock);
979 struct list_head *tmp, *n;
980 ldlm_reprocess_all_ns(req->rq_export->exp_obd->obd_namespace);
981 CWARN("%s: all clients recovered, sending delayed replies\n",
983 spin_lock_bh(&obd->obd_processing_task_lock);
984 obd->obd_recovering = 0;
985 target_cancel_recovery_timer(obd);
986 spin_unlock_bh(&obd->obd_processing_task_lock);
988 /* when recovery finished, cleanup orphans on mds and ost */
989 if (OBT(obd) && OBP(obd, postrecov)) {
990 rc2 = OBP(obd, postrecov)(obd);
992 CWARN("%s: all clients recovered, %d MDS "
993 "orphans deleted\n", obd->obd_name, rc2);
995 CERROR("postrecov failed %d\n", rc2);
998 list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
999 req = list_entry(tmp, struct ptlrpc_request, rq_list);
1000 DEBUG_REQ(D_ERROR, req, "delayed:");
1002 class_export_put(req->rq_export);
1003 list_del(&req->rq_list);
1004 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
1005 OBD_FREE(req, sizeof *req);
1007 ptlrpc_run_recovery_over_upcall(obd);
1009 CWARN("%s: %d recoverable clients remain\n",
1010 obd->obd_name, obd->obd_recoverable_clients);
1011 wake_up(&obd->obd_next_transno_waitq);
1018 target_send_reply_msg (struct ptlrpc_request *req, int rc, int fail_id)
1020 if (OBD_FAIL_CHECK(fail_id | OBD_FAIL_ONCE)) {
1021 obd_fail_loc |= OBD_FAIL_ONCE | OBD_FAILED;
1022 DEBUG_REQ(D_ERROR, req, "dropping reply");
1023 /* NB this does _not_ send with ACK disabled, to simulate
1024 * sending OK, but timing out for the ACK */
1025 if (req->rq_reply_state != NULL) {
1026 if (!req->rq_reply_state->rs_difficult) {
1027 lustre_free_reply_state (req->rq_reply_state);
1028 req->rq_reply_state = NULL;
1030 struct ptlrpc_service *svc =
1031 req->rq_rqbd->rqbd_srv_ni->sni_service;
1032 atomic_inc(&svc->srv_outstanding_replies);
1039 DEBUG_REQ(D_ERROR, req, "processing error (%d)", rc);
1040 if (req->rq_reply_state == NULL) {
1041 rc = lustre_pack_reply (req, 0, NULL, NULL);
1043 CERROR ("can't allocate reply\n");
1047 req->rq_type = PTL_RPC_MSG_ERR;
1049 DEBUG_REQ(D_NET, req, "sending reply");
1052 return (ptlrpc_send_reply(req, 1));
1056 target_send_reply(struct ptlrpc_request *req, int rc, int fail_id)
1059 unsigned long flags;
1060 struct ptlrpc_reply_state *rs;
1061 struct obd_device *obd;
1062 struct obd_export *exp;
1063 struct ptlrpc_srv_ni *sni;
1064 struct ptlrpc_service *svc;
1066 sni = req->rq_rqbd->rqbd_srv_ni;
1067 svc = sni->sni_service;
1069 rs = req->rq_reply_state;
1070 if (rs == NULL || !rs->rs_difficult) {
1071 /* The easy case; no notifiers and reply_out_callback()
1072 * cleans up (i.e. we can't look inside rs after a
1073 * successful send) */
1074 netrc = target_send_reply_msg (req, rc, fail_id);
1076 LASSERT (netrc == 0 || req->rq_reply_state == NULL);
1080 /* must be an export if locks saved */
1081 LASSERT (req->rq_export != NULL);
1082 /* req/reply consistent */
1083 LASSERT (rs->rs_srv_ni == sni);
1086 LASSERT (!rs->rs_scheduled);
1087 LASSERT (!rs->rs_scheduled_ever);
1088 LASSERT (!rs->rs_handled);
1089 LASSERT (!rs->rs_on_net);
1090 LASSERT (rs->rs_export == NULL);
1091 LASSERT (list_empty(&rs->rs_obd_list));
1092 LASSERT (list_empty(&rs->rs_exp_list));
1094 exp = class_export_get (req->rq_export);
1097 /* disable reply scheduling onto srv_reply_queue while I'm setting up */
1098 rs->rs_scheduled = 1;
1100 rs->rs_xid = req->rq_xid;
1101 rs->rs_transno = req->rq_transno;
1102 rs->rs_export = exp;
1104 spin_lock_irqsave (&obd->obd_uncommitted_replies_lock, flags);
1106 if (rs->rs_transno > obd->obd_last_committed) {
1107 /* not committed already */
1108 list_add_tail (&rs->rs_obd_list,
1109 &obd->obd_uncommitted_replies);
1112 spin_unlock (&obd->obd_uncommitted_replies_lock);
1113 spin_lock (&exp->exp_lock);
1115 list_add_tail (&rs->rs_exp_list, &exp->exp_outstanding_replies);
1117 spin_unlock_irqrestore (&exp->exp_lock, flags);
1119 netrc = target_send_reply_msg (req, rc, fail_id);
1121 spin_lock_irqsave (&svc->srv_lock, flags);
1123 svc->srv_n_difficult_replies++;
1125 if (netrc != 0) /* error sending: reply is off the net */
1128 if (!rs->rs_on_net || /* some notifier */
1129 list_empty(&rs->rs_exp_list) || /* completed already */
1130 list_empty(&rs->rs_obd_list)) {
1131 list_add_tail (&rs->rs_list, &svc->srv_reply_queue);
1132 wake_up (&svc->srv_waitq);
1134 list_add (&rs->rs_list, &sni->sni_active_replies);
1135 rs->rs_scheduled = 0; /* allow notifier to schedule */
1138 spin_unlock_irqrestore (&svc->srv_lock, flags);
1141 int target_handle_ping(struct ptlrpc_request *req)
1143 return lustre_pack_reply(req, 0, NULL, NULL);