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>
38 #include <linux/lustre_sec.h>
40 /* @priority: if non-zero, move the selected to the list head
41 * @nocreate: if non-zero, only search in existed connections
43 static int import_set_conn(struct obd_import *imp, struct obd_uuid *uuid,
44 int priority, int nocreate)
46 struct ptlrpc_connection *ptlrpc_conn;
47 struct obd_import_conn *imp_conn = NULL, *item;
51 LASSERT(!(nocreate && !priority));
53 ptlrpc_conn = ptlrpc_uuid_to_connection(uuid);
55 CERROR("can't find connection %s\n", uuid->uuid);
60 OBD_ALLOC(imp_conn, sizeof(*imp_conn));
62 CERROR("fail to alloc memory\n");
63 GOTO(out_put, rc = -ENOMEM);
67 spin_lock(&imp->imp_lock);
68 list_for_each_entry(item, &imp->imp_conn_list, oic_item) {
69 if (obd_uuid_equals(uuid, &item->oic_uuid)) {
71 list_del(&item->oic_item);
72 list_add(&item->oic_item, &imp->imp_conn_list);
73 item->oic_last_attempt = 0;
75 CDEBUG(D_HA, "imp %p@%s: find existed conn %s%s\n",
76 imp, imp->imp_obd->obd_name, uuid->uuid,
77 (priority ? ", move to head." : ""));
78 spin_unlock(&imp->imp_lock);
79 GOTO(out_free, rc = 0);
84 imp_conn->oic_conn = ptlrpc_conn;
85 imp_conn->oic_uuid = *uuid;
86 imp_conn->oic_last_attempt = 0;
88 list_add(&imp_conn->oic_item, &imp->imp_conn_list);
90 list_add_tail(&imp_conn->oic_item, &imp->imp_conn_list);
91 CDEBUG(D_HA, "imp %p@%s: add connection %s at %s\n",
92 imp, imp->imp_obd->obd_name, uuid->uuid,
93 (priority ? "head" : "tail"));
97 spin_unlock(&imp->imp_lock);
101 OBD_FREE(imp_conn, sizeof(*imp_conn));
103 ptlrpc_put_connection(ptlrpc_conn);
107 int import_set_conn_priority(struct obd_import *imp, struct obd_uuid *uuid)
109 return import_set_conn(imp, uuid, 1, 1);
112 int client_import_add_conn(struct obd_import *imp, struct obd_uuid *uuid,
115 return import_set_conn(imp, uuid, priority, 0);
118 int client_import_del_conn(struct obd_import *imp, struct obd_uuid *uuid)
120 struct obd_import_conn *imp_conn;
121 struct obd_export *dlmexp;
125 spin_lock(&imp->imp_lock);
126 if (list_empty(&imp->imp_conn_list)) {
127 LASSERT(!imp->imp_conn_current);
128 LASSERT(!imp->imp_connection);
132 list_for_each_entry(imp_conn, &imp->imp_conn_list, oic_item) {
133 if (!obd_uuid_equals(uuid, &imp_conn->oic_uuid))
135 LASSERT(imp_conn->oic_conn);
137 /* is current conn? */
138 if (imp_conn == imp->imp_conn_current) {
139 LASSERT(imp_conn->oic_conn == imp->imp_connection);
141 if (imp->imp_state != LUSTRE_IMP_CLOSED &&
142 imp->imp_state != LUSTRE_IMP_DISCON) {
143 CERROR("can't remove current connection\n");
144 GOTO(out, rc = -EBUSY);
147 ptlrpc_put_connection(imp->imp_connection);
148 imp->imp_connection = NULL;
150 dlmexp = class_conn2export(&imp->imp_dlm_handle);
151 if (dlmexp && dlmexp->exp_connection) {
152 LASSERT(dlmexp->exp_connection ==
154 ptlrpc_put_connection(dlmexp->exp_connection);
155 dlmexp->exp_connection = NULL;
159 list_del(&imp_conn->oic_item);
160 ptlrpc_put_connection(imp_conn->oic_conn);
161 OBD_FREE(imp_conn, sizeof(*imp_conn));
162 CDEBUG(D_HA, "imp %p@%s: remove connection %s\n",
163 imp, imp->imp_obd->obd_name, uuid->uuid);
168 spin_unlock(&imp->imp_lock);
170 CERROR("connection %s not found\n", uuid->uuid);
174 int client_obd_setup(struct obd_device *obddev, obd_count len, void *buf)
176 struct lustre_cfg* lcfg = buf;
177 struct client_obd *cli = &obddev->u.cli;
178 struct obd_import *imp;
179 struct obd_uuid server_uuid;
180 int rq_portal, rp_portal, connect_op;
181 char *name = obddev->obd_type->typ_name;
182 char *mgmt_name = NULL;
184 struct obd_device *mgmt_obd;
185 mgmtcli_register_for_events_t register_f;
188 /* In a more perfect world, we would hang a ptlrpc_client off of
189 * obd_type and just use the values from there. */
190 if (!strcmp(name, LUSTRE_OSC_NAME)) {
191 rq_portal = OST_REQUEST_PORTAL;
192 rp_portal = OSC_REPLY_PORTAL;
193 connect_op = OST_CONNECT;
194 } else if (!strcmp(name, LUSTRE_MDC_NAME)) {
195 rq_portal = MDS_REQUEST_PORTAL;
196 rp_portal = MDC_REPLY_PORTAL;
197 connect_op = MDS_CONNECT;
198 } else if (!strcmp(name, LUSTRE_MGMTCLI_NAME)) {
199 rq_portal = MGMT_REQUEST_PORTAL;
200 rp_portal = MGMT_REPLY_PORTAL;
201 connect_op = MGMT_CONNECT;
203 CERROR("unknown client OBD type \"%s\", can't setup\n",
208 if (lcfg->lcfg_inllen1 < 1) {
209 CERROR("requires a TARGET UUID\n");
213 if (lcfg->lcfg_inllen1 > 37) {
214 CERROR("client UUID must be less than 38 characters\n");
218 if (lcfg->lcfg_inllen2 < 1) {
219 CERROR("setup requires a SERVER UUID\n");
223 if (lcfg->lcfg_inllen2 > 37) {
224 CERROR("target UUID must be less than 38 characters\n");
228 sema_init(&cli->cl_sem, 1);
229 cli->cl_conn_count = 0;
230 memcpy(server_uuid.uuid, lcfg->lcfg_inlbuf2,
231 min_t(unsigned int, lcfg->lcfg_inllen2, sizeof(server_uuid)));
234 cli->cl_avail_grant = 0;
235 /* FIXME: should limit this for the sum of all cl_dirty_max */
236 cli->cl_dirty_max = OSC_MAX_DIRTY_DEFAULT * 1024 * 1024;
237 if (cli->cl_dirty_max >> PAGE_SHIFT > num_physpages / 8)
238 cli->cl_dirty_max = num_physpages << (PAGE_SHIFT - 3);
239 INIT_LIST_HEAD(&cli->cl_cache_waiters);
240 INIT_LIST_HEAD(&cli->cl_loi_ready_list);
241 INIT_LIST_HEAD(&cli->cl_loi_write_list);
242 INIT_LIST_HEAD(&cli->cl_loi_read_list);
243 spin_lock_init(&cli->cl_loi_list_lock);
244 cli->cl_r_in_flight = 0;
245 cli->cl_w_in_flight = 0;
246 spin_lock_init(&cli->cl_read_rpc_hist.oh_lock);
247 spin_lock_init(&cli->cl_write_rpc_hist.oh_lock);
248 spin_lock_init(&cli->cl_read_page_hist.oh_lock);
249 spin_lock_init(&cli->cl_write_page_hist.oh_lock);
251 if (num_physpages >> (20 - PAGE_SHIFT) <= 128) { /* <= 128 MB */
252 cli->cl_max_pages_per_rpc = PTLRPC_MAX_BRW_PAGES / 4;
253 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT / 4;
254 } else if (num_physpages >> (20 - PAGE_SHIFT) <= 512) { /* <= 512 MB */
255 cli->cl_max_pages_per_rpc = PTLRPC_MAX_BRW_PAGES / 2;
256 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT / 2;
258 cli->cl_max_pages_per_rpc = PTLRPC_MAX_BRW_PAGES;
259 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT;
265 CERROR("ldlm_get_ref failed: %d\n", rc);
269 ptlrpc_init_client(rq_portal, rp_portal, name,
270 &obddev->obd_ldlm_client);
272 imp = class_new_import();
274 GOTO(err_ldlm, rc = -ENOENT);
275 imp->imp_client = &obddev->obd_ldlm_client;
276 imp->imp_obd = obddev;
277 imp->imp_connect_op = connect_op;
278 imp->imp_generation = 0;
279 imp->imp_initial_recov = 1;
280 INIT_LIST_HEAD(&imp->imp_pinger_chain);
281 memcpy(imp->imp_target_uuid.uuid, lcfg->lcfg_inlbuf1,
283 class_import_put(imp);
285 rc = client_import_add_conn(imp, &server_uuid, 1);
287 CERROR("can't add initial connection\n");
288 GOTO(err_import, rc);
291 cli->cl_import = imp;
292 cli->cl_max_mds_easize = sizeof(struct lov_mds_md);
293 cli->cl_max_mds_cookiesize = sizeof(struct llog_cookie);
294 cli->cl_sandev = to_kdev_t(0);
296 if (lcfg->lcfg_inllen3 != 0) {
297 if (!strcmp(lcfg->lcfg_inlbuf3, "inactive")) {
298 CDEBUG(D_HA, "marking %s %s->%s as inactive\n",
299 name, obddev->obd_name,
300 imp->imp_target_uuid.uuid);
301 imp->imp_invalid = 1;
303 if (lcfg->lcfg_inllen4 != 0)
304 mgmt_name = lcfg->lcfg_inlbuf4;
306 mgmt_name = lcfg->lcfg_inlbuf3;
310 if (mgmt_name != NULL) {
311 /* Register with management client if we need to. */
312 CDEBUG(D_HA, "%s registering with %s for events about %s\n",
313 obddev->obd_name, mgmt_name, server_uuid.uuid);
315 mgmt_obd = class_name2obd(mgmt_name);
317 CERROR("can't find mgmtcli %s to register\n",
319 GOTO(err_import, rc = -ENOSYS);
322 register_f = inter_module_get("mgmtcli_register_for_events");
324 CERROR("can't i_m_g mgmtcli_register_for_events\n");
325 GOTO(err_import, rc = -ENOSYS);
328 rc = register_f(mgmt_obd, obddev, &imp->imp_target_uuid);
329 inter_module_put("mgmtcli_register_for_events");
332 cli->cl_mgmtcli_obd = mgmt_obd;
338 class_destroy_import(imp);
346 int client_obd_cleanup(struct obd_device *obddev, int flags)
348 struct client_obd *cli = &obddev->u.cli;
353 if (cli->cl_mgmtcli_obd) {
354 mgmtcli_deregister_for_events_t dereg_f;
356 dereg_f = inter_module_get("mgmtcli_deregister_for_events");
357 dereg_f(cli->cl_mgmtcli_obd, obddev);
358 inter_module_put("mgmtcli_deregister_for_events");
361 /* Here we try to drop the security structure after destroy import,
362 * to avoid issue of "sleep in spinlock".
364 class_import_get(cli->cl_import);
365 class_destroy_import(cli->cl_import);
366 ptlrpcs_import_drop_sec(cli->cl_import);
367 class_import_put(cli->cl_import);
368 cli->cl_import = NULL;
370 ldlm_put_ref(flags & OBD_OPT_FORCE);
375 int client_connect_import(struct lustre_handle *dlm_handle,
376 struct obd_device *obd,
377 struct obd_uuid *cluuid,
378 unsigned long connect_flags)
380 struct client_obd *cli = &obd->u.cli;
381 struct obd_import *imp = cli->cl_import;
382 struct obd_export *exp;
387 rc = class_connect(dlm_handle, obd, cluuid);
391 cli->cl_conn_count++;
392 if (cli->cl_conn_count > 1)
394 exp = class_conn2export(dlm_handle);
396 if (obd->obd_namespace != NULL)
397 CERROR("already have namespace!\n");
398 obd->obd_namespace = ldlm_namespace_new(obd->obd_name,
399 LDLM_NAMESPACE_CLIENT);
400 if (obd->obd_namespace == NULL)
401 GOTO(out_disco, rc = -ENOMEM);
403 rc = ptlrpcs_import_get_sec(imp);
407 imp->imp_dlm_handle = *dlm_handle;
408 rc = ptlrpc_init_import(imp);
412 imp->imp_connect_flags = connect_flags;
413 rc = ptlrpc_connect_import(imp, NULL);
415 LASSERT (imp->imp_state == LUSTRE_IMP_DISCON);
418 LASSERT(exp->exp_connection);
419 ptlrpc_pinger_add_import(imp);
424 ldlm_namespace_free(obd->obd_namespace, 0);
425 obd->obd_namespace = NULL;
427 cli->cl_conn_count--;
428 class_disconnect(exp, 0);
430 class_export_put(exp);
437 int client_disconnect_export(struct obd_export *exp, unsigned long flags)
439 struct obd_device *obd = class_exp2obd(exp);
440 struct client_obd *cli = &obd->u.cli;
441 struct obd_import *imp = cli->cl_import;
446 CERROR("invalid export for disconnect: exp %p cookie "LPX64"\n",
447 exp, exp ? exp->exp_handle.h_cookie : -1);
452 if (!cli->cl_conn_count) {
453 CERROR("disconnecting disconnected device (%s)\n",
455 GOTO(out_sem, rc = -EINVAL);
458 cli->cl_conn_count--;
459 if (cli->cl_conn_count)
460 GOTO(out_no_disconnect, rc = 0);
462 /* Some non-replayable imports (MDS's OSCs) are pinged, so just
463 * delete it regardless. (It's safe to delete an import that was
465 (void)ptlrpc_pinger_del_import(imp);
467 if (obd->obd_namespace != NULL) {
468 /* obd_no_recov == local only */
469 ldlm_cli_cancel_unused(obd->obd_namespace, NULL,
470 obd->obd_no_recov, NULL);
471 ldlm_namespace_free(obd->obd_namespace, obd->obd_no_recov);
472 obd->obd_namespace = NULL;
476 * Yeah, obd_no_recov also (mainly) means "forced shutdown".
478 if (obd->obd_no_recov)
479 ptlrpc_invalidate_import(imp, 0);
481 rc = ptlrpc_disconnect_import(imp);
485 err = class_disconnect(exp, 0);
493 /* --------------------------------------------------------------------------
494 * from old lib/target.c
495 * -------------------------------------------------------------------------- */
497 int target_handle_reconnect(struct lustre_handle *conn, struct obd_export *exp,
498 struct obd_uuid *cluuid, int initial_conn)
500 if (exp->exp_connection && !initial_conn) {
501 struct lustre_handle *hdl;
502 hdl = &exp->exp_imp_reverse->imp_remote_handle;
503 /* Might be a re-connect after a partition. */
504 if (!memcmp(&conn->cookie, &hdl->cookie, sizeof conn->cookie)) {
505 CERROR("%s reconnecting\n", cluuid->uuid);
506 conn->cookie = exp->exp_handle.h_cookie;
509 CERROR("%s reconnecting from %s, "
510 "handle mismatch (ours "LPX64", theirs "
511 LPX64")\n", cluuid->uuid,
512 exp->exp_connection->c_remote_uuid.uuid,
513 hdl->cookie, conn->cookie);
514 memset(conn, 0, sizeof *conn);
519 conn->cookie = exp->exp_handle.h_cookie;
520 CDEBUG(D_INFO, "existing export for UUID '%s' at %p\n",
522 CDEBUG(D_IOCTL,"connect: cookie "LPX64"\n", conn->cookie);
526 static char nidstr[PTL_NALFMT_SIZE];
527 int target_handle_connect(struct ptlrpc_request *req)
529 unsigned long connect_flags = 0, *cfp;
530 struct obd_device *target;
531 struct obd_export *export = NULL;
532 struct obd_import *revimp;
533 struct lustre_handle conn;
534 struct obd_uuid tgtuuid;
535 struct obd_uuid cluuid;
536 struct obd_uuid remote_uuid;
541 int initial_conn = 0;
542 char peer_str[PTL_NALFMT_SIZE];
545 OBD_RACE(OBD_FAIL_TGT_CONN_RACE);
547 LASSERT_REQSWAB (req, 0);
548 str = lustre_msg_string(req->rq_reqmsg, 0, sizeof(tgtuuid) - 1);
550 CERROR("bad target UUID for connect\n");
551 GOTO(out, rc = -EINVAL);
554 obd_str2uuid (&tgtuuid, str);
555 target = class_uuid2obd(&tgtuuid);
557 target = class_name2obd(str);
560 if (!target || target->obd_stopping || !target->obd_set_up) {
561 CERROR("UUID '%s' is not available for connect from NID %s\n",
562 str, ptlrpc_peernid2str(&req->rq_peer, nidstr));
563 GOTO(out, rc = -ENODEV);
566 LASSERT_REQSWAB (req, 1);
567 str = lustre_msg_string(req->rq_reqmsg, 1, sizeof(cluuid) - 1);
569 CERROR("bad client UUID for connect\n");
570 GOTO(out, rc = -EINVAL);
573 obd_str2uuid (&cluuid, str);
575 /* XXX extract a nettype and format accordingly */
576 switch (sizeof(ptl_nid_t)) {
577 /* NB the casts only avoid compiler warnings */
579 snprintf(remote_uuid.uuid, sizeof remote_uuid,
580 "NET_"LPX64"_UUID", (__u64)req->rq_peer.peer_id.nid);
583 snprintf(remote_uuid.uuid, sizeof remote_uuid,
584 "NET_%x_UUID", (__u32)req->rq_peer.peer_id.nid);
590 tmp = lustre_msg_buf(req->rq_reqmsg, 2, sizeof conn);
592 GOTO(out, rc = -EPROTO);
594 memcpy(&conn, tmp, sizeof conn);
596 cfp = lustre_msg_buf(req->rq_reqmsg, 3, sizeof(unsigned long));
597 LASSERT(cfp != NULL);
598 connect_flags = *cfp;
600 rc = lustre_pack_reply(req, 0, NULL, NULL);
604 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_INITIAL)
607 /* lctl gets a backstage, all-access pass. */
608 if (obd_uuid_equals(&cluuid, &target->obd_uuid))
609 goto dont_check_exports;
611 spin_lock(&target->obd_dev_lock);
612 list_for_each(p, &target->obd_exports) {
613 export = list_entry(p, struct obd_export, exp_obd_chain);
614 if (obd_uuid_equals(&cluuid, &export->exp_client_uuid)) {
615 spin_unlock(&target->obd_dev_lock);
616 LASSERT(export->exp_obd == target);
618 rc = target_handle_reconnect(&conn, export, &cluuid,
624 /* If we found an export, we already unlocked. */
626 spin_unlock(&target->obd_dev_lock);
627 } else if (req->rq_export == NULL &&
628 atomic_read(&export->exp_rpc_count) > 0) {
629 CWARN("%s: refuse connection from %s/%s to 0x%p/%d\n",
630 target->obd_name, cluuid.uuid,
631 ptlrpc_peernid2str(&req->rq_peer, peer_str),
632 export, atomic_read(&export->exp_refcount));
633 GOTO(out, rc = -EBUSY);
634 } else if (req->rq_export != NULL &&
635 atomic_read(&export->exp_rpc_count) > 1) {
636 CWARN("%s: refuse reconnection from %s@%s to 0x%p/%d\n",
637 target->obd_name, cluuid.uuid,
638 ptlrpc_peernid2str(&req->rq_peer, peer_str),
639 export, atomic_read(&export->exp_rpc_count));
640 GOTO(out, rc = -EBUSY);
642 else if (req->rq_reqmsg->conn_cnt == 1 && !initial_conn) {
643 CERROR("%s reconnected with 1 conn_cnt; cookies not random?\n",
645 GOTO(out, rc = -EALREADY);
648 /* Tell the client if we're in recovery. */
649 /* If this is the first client, start the recovery timer */
650 CWARN("%s: connection from %s@%s/%lu %s\n", target->obd_name, cluuid.uuid,
651 ptlrpc_peernid2str(&req->rq_peer, peer_str), *cfp,
652 target->obd_recovering ? "(recovering)" : "");
653 if (target->obd_recovering) {
654 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
655 target_start_recovery_timer(target);
658 /* Tell the client if we support replayable requests */
659 if (target->obd_replayable)
660 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
662 if (export == NULL) {
663 if (target->obd_recovering) {
664 CERROR("%s denying connection for new client %s@%s: "
665 "%d clients in recovery for %lds\n", target->obd_name,
667 ptlrpc_peernid2str(&req->rq_peer, peer_str),
668 target->obd_recoverable_clients,
669 (target->obd_recovery_timer.expires-jiffies)/HZ);
673 rc = obd_connect(&conn, target, &cluuid, connect_flags);
676 /* Tell the client if we support replayable requests */
677 if (target->obd_replayable)
678 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
680 /* If all else goes well, this is our RPC return code. */
683 if (rc && rc != EALREADY)
686 req->rq_repmsg->handle = conn;
688 /* If the client and the server are the same node, we will already
689 * have an export that really points to the client's DLM export,
690 * because we have a shared handles table.
692 * XXX this will go away when shaver stops sending the "connect" handle
693 * in the real "remote handle" field of the request --phik 24 Apr 2003
695 if (req->rq_export != NULL)
696 class_export_put(req->rq_export);
698 /* ownership of this export ref transfers to the request */
699 export = req->rq_export = class_conn2export(&conn);
700 LASSERT(export != NULL);
702 spin_lock_irqsave(&export->exp_lock, flags);
704 req->rq_repmsg->conn_cnt = export->exp_conn_cnt + 1;
705 } else if (export->exp_conn_cnt >= req->rq_reqmsg->conn_cnt) {
706 CERROR("%s@%s: already connected at a higher conn_cnt: %d > %d\n",
707 cluuid.uuid, ptlrpc_peernid2str(&req->rq_peer, peer_str),
708 export->exp_conn_cnt,
709 req->rq_reqmsg->conn_cnt);
710 spin_unlock_irqrestore(&export->exp_lock, flags);
711 GOTO(out, rc = -EALREADY);
713 export->exp_conn_cnt = req->rq_reqmsg->conn_cnt;
714 spin_unlock_irqrestore(&export->exp_lock, flags);
716 /* request from liblustre? */
717 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT)
718 export->exp_libclient = 1;
720 if (export->exp_connection != NULL)
721 ptlrpc_put_connection(export->exp_connection);
722 export->exp_connection = ptlrpc_get_connection(&req->rq_peer,
725 if (rc == EALREADY) {
726 /* We indicate the reconnection in a flag, not an error code. */
727 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECONNECT);
731 if (target->obd_recovering) {
732 target->obd_connected_clients++;
735 memcpy(&conn, lustre_msg_buf(req->rq_reqmsg, 2, sizeof conn),
738 if (export->exp_imp_reverse != NULL) {
739 /* same logic as client_obd_cleanup */
740 class_import_get(export->exp_imp_reverse);
741 class_destroy_import(export->exp_imp_reverse);
742 ptlrpcs_import_drop_sec(export->exp_imp_reverse);
743 class_import_put(export->exp_imp_reverse);
746 /* for the rest part, we return -ENOTCONN in case of errors
747 * in order to let client initialize connection again.
749 revimp = export->exp_imp_reverse = class_new_import();
751 CERROR("fail to alloc new reverse import.\n");
752 GOTO(out, rc = -ENOTCONN);
755 revimp->imp_connection = ptlrpc_connection_addref(export->exp_connection);
756 revimp->imp_client = &export->exp_obd->obd_ldlm_client;
757 revimp->imp_remote_handle = conn;
758 revimp->imp_obd = target;
759 revimp->imp_dlm_fake = 1;
760 revimp->imp_state = LUSTRE_IMP_FULL;
762 rc = ptlrpcs_import_get_sec(revimp);
764 CERROR("reverse import can not get sec: %d\n", rc);
765 class_destroy_import(revimp);
766 export->exp_imp_reverse = NULL;
767 GOTO(out, rc = -ENOTCONN);
770 class_import_put(revimp);
772 rc = obd_connect_post(export, connect_flags);
779 int target_handle_disconnect(struct ptlrpc_request *req)
781 struct obd_export *exp;
785 rc = lustre_pack_reply(req, 0, NULL, NULL);
789 /* keep the rq_export around so we can send the reply */
790 exp = class_export_get(req->rq_export);
791 req->rq_status = obd_disconnect(exp, 0);
795 void target_destroy_export(struct obd_export *exp)
797 /* exports created from last_rcvd data, and "fake"
798 exports created by lctl don't have an import */
799 if (exp->exp_imp_reverse != NULL) {
800 ptlrpcs_import_drop_sec(exp->exp_imp_reverse);
801 class_destroy_import(exp->exp_imp_reverse);
804 /* We cancel locks at disconnect time, but this will catch any locks
805 * granted in a race with recovery-induced disconnect. */
806 if (exp->exp_obd->obd_namespace != NULL)
807 ldlm_cancel_locks_for_export(exp);
814 struct ptlrpc_request *
815 ptlrpc_clone_req( struct ptlrpc_request *orig_req)
817 struct ptlrpc_request *copy_req;
818 struct lustre_msg *copy_reqmsg;
820 OBD_ALLOC(copy_req, sizeof *copy_req);
823 OBD_ALLOC(copy_reqmsg, orig_req->rq_reqlen);
825 OBD_FREE(copy_req, sizeof *copy_req);
829 memcpy(copy_req, orig_req, sizeof *copy_req);
830 memcpy(copy_reqmsg, orig_req->rq_reqmsg, orig_req->rq_reqlen);
831 /* the copied req takes over the reply state and security data */
832 orig_req->rq_reply_state = NULL;
833 orig_req->rq_sec_svcdata = NULL;
835 copy_req->rq_reqmsg = copy_reqmsg;
836 class_export_get(copy_req->rq_export);
837 INIT_LIST_HEAD(©_req->rq_list);
841 void ptlrpc_free_clone( struct ptlrpc_request *req)
844 svcsec_cleanup_req(req);
846 class_export_put(req->rq_export);
847 list_del(&req->rq_list);
848 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
849 OBD_FREE(req, sizeof *req);
854 static void target_release_saved_req(struct ptlrpc_request *req)
857 svcsec_cleanup_req(req);
859 class_export_put(req->rq_export);
860 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
861 OBD_FREE(req, sizeof *req);
865 static void target_finish_recovery(struct obd_device *obd)
867 struct list_head *tmp, *n;
870 CWARN("%s: sending delayed replies to recovered clients\n",
873 ldlm_reprocess_all_ns(obd->obd_namespace);
875 /* when recovery finished, cleanup orphans on mds and ost */
876 if (OBT(obd) && OBP(obd, postrecov)) {
877 rc = OBP(obd, postrecov)(obd);
879 CWARN("%s: all clients recovered, %d MDS "
880 "orphans deleted\n", obd->obd_name, rc);
882 CERROR("postrecov failed %d\n", rc);
886 list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
887 struct ptlrpc_request *req;
888 req = list_entry(tmp, struct ptlrpc_request, rq_list);
889 list_del(&req->rq_list);
890 DEBUG_REQ(D_ERROR, req, "delayed:");
892 target_release_saved_req(req);
894 obd->obd_recovery_end = LTIME_S(CURRENT_TIME);
898 static void abort_recovery_queue(struct obd_device *obd)
900 struct ptlrpc_request *req;
901 struct list_head *tmp, *n;
904 list_for_each_safe(tmp, n, &obd->obd_recovery_queue) {
905 req = list_entry(tmp, struct ptlrpc_request, rq_list);
906 list_del(&req->rq_list);
907 DEBUG_REQ(D_ERROR, req, "aborted:");
908 req->rq_status = -ENOTCONN;
909 req->rq_type = PTL_RPC_MSG_ERR;
910 rc = lustre_pack_reply(req, 0, NULL, NULL);
914 DEBUG_REQ(D_ERROR, req,
915 "packing failed for abort-reply; skipping");
917 target_release_saved_req(req);
922 /* Called from a cleanup function if the device is being cleaned up
923 forcefully. The exports should all have been disconnected already,
924 the only thing left to do is
925 - clear the recovery flags
927 - free queued requests and replies, but don't send replies
928 Because the obd_stopping flag is set, no new requests should be received.
931 void target_cleanup_recovery(struct obd_device *obd)
933 struct list_head *tmp, *n;
934 struct ptlrpc_request *req;
936 spin_lock_bh(&obd->obd_processing_task_lock);
937 if (!obd->obd_recovering) {
938 spin_unlock_bh(&obd->obd_processing_task_lock);
942 obd->obd_recovering = obd->obd_abort_recovery = 0;
943 target_cancel_recovery_timer(obd);
944 spin_unlock_bh(&obd->obd_processing_task_lock);
946 list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
947 req = list_entry(tmp, struct ptlrpc_request, rq_list);
948 list_del(&req->rq_list);
949 LASSERT (req->rq_reply_state);
950 lustre_free_reply_state(req->rq_reply_state);
951 target_release_saved_req(req);
953 list_for_each_safe(tmp, n, &obd->obd_recovery_queue) {
954 req = list_entry(tmp, struct ptlrpc_request, rq_list);
955 list_del(&req->rq_list);
956 LASSERT (req->rq_reply_state == 0);
957 target_release_saved_req(req);
962 static void target_abort_recovery(void *data)
964 struct obd_device *obd = data;
966 LASSERT(!obd->obd_recovering);
968 class_disconnect_stale_exports(obd, 0);
970 CERROR("%s: recovery period over; disconnecting unfinished clients.\n",
973 abort_recovery_queue(obd);
974 target_finish_recovery(obd);
975 ptlrpc_run_recovery_over_upcall(obd);
979 static void target_recovery_expired(unsigned long castmeharder)
981 struct obd_device *obd = (struct obd_device *)castmeharder;
982 CERROR("recovery timed out, aborting\n");
983 spin_lock_bh(&obd->obd_processing_task_lock);
984 if (obd->obd_recovering)
985 obd->obd_abort_recovery = 1;
987 wake_up(&obd->obd_next_transno_waitq);
988 spin_unlock_bh(&obd->obd_processing_task_lock);
992 /* obd_processing_task_lock should be held */
993 void target_cancel_recovery_timer(struct obd_device *obd)
995 CDEBUG(D_HA, "%s: cancel recovery timer\n", obd->obd_name);
996 del_timer(&obd->obd_recovery_timer);
1000 static void reset_recovery_timer(struct obd_device *obd)
1002 spin_lock_bh(&obd->obd_processing_task_lock);
1003 if (!obd->obd_recovering) {
1004 spin_unlock_bh(&obd->obd_processing_task_lock);
1007 CDEBUG(D_HA, "timer will expire in %u seconds\n",
1008 OBD_RECOVERY_TIMEOUT / HZ);
1009 mod_timer(&obd->obd_recovery_timer, jiffies + OBD_RECOVERY_TIMEOUT);
1010 spin_unlock_bh(&obd->obd_processing_task_lock);
1014 /* Only start it the first time called */
1015 void target_start_recovery_timer(struct obd_device *obd)
1017 spin_lock_bh(&obd->obd_processing_task_lock);
1018 if (!obd->obd_recovering || timer_pending(&obd->obd_recovery_timer)) {
1019 spin_unlock_bh(&obd->obd_processing_task_lock);
1022 CWARN("%s: starting recovery timer (%us)\n", obd->obd_name,
1023 OBD_RECOVERY_TIMEOUT / HZ);
1024 obd->obd_recovery_timer.function = target_recovery_expired;
1025 obd->obd_recovery_timer.data = (unsigned long)obd;
1026 mod_timer(&obd->obd_recovery_timer, jiffies + OBD_RECOVERY_TIMEOUT);
1027 spin_unlock_bh(&obd->obd_processing_task_lock);
1031 static int check_for_next_transno(struct obd_device *obd)
1033 struct ptlrpc_request *req = NULL;
1034 int wake_up = 0, connected, completed, queue_len, max;
1035 __u64 next_transno, req_transno;
1037 spin_lock_bh(&obd->obd_processing_task_lock);
1038 if (!list_empty(&obd->obd_recovery_queue)) {
1039 req = list_entry(obd->obd_recovery_queue.next,
1040 struct ptlrpc_request, rq_list);
1041 req_transno = req->rq_reqmsg->transno;
1046 max = obd->obd_max_recoverable_clients;
1047 connected = obd->obd_connected_clients;
1048 completed = max - obd->obd_recoverable_clients;
1049 queue_len = obd->obd_requests_queued_for_recovery;
1050 next_transno = obd->obd_next_recovery_transno;
1052 CDEBUG(D_HA,"max: %d, connected: %d, completed: %d, queue_len: %d, "
1053 "req_transno: "LPU64", next_transno: "LPU64"\n",
1054 max, connected, completed, queue_len, req_transno, next_transno);
1055 if (obd->obd_abort_recovery) {
1056 CDEBUG(D_HA, "waking for aborted recovery\n");
1058 } else if (max == completed) {
1059 CDEBUG(D_HA, "waking for completed recovery\n");
1061 } else if (req_transno == next_transno) {
1062 CDEBUG(D_HA, "waking for next ("LPD64")\n", next_transno);
1064 } else if (queue_len + completed == max) {
1065 LASSERT(req->rq_reqmsg->transno >= next_transno);
1067 "waking for skipped transno (skip: "LPD64
1068 ", ql: %d, comp: %d, conn: %d, next: "LPD64")\n",
1069 next_transno, queue_len, completed, max, req_transno);
1070 obd->obd_next_recovery_transno = req_transno;
1073 spin_unlock_bh(&obd->obd_processing_task_lock);
1078 static struct ptlrpc_request *
1079 target_next_replay_req(struct obd_device *obd)
1081 struct l_wait_info lwi = { 0 };
1082 struct ptlrpc_request *req;
1084 CDEBUG(D_HA, "Waiting for transno "LPD64"\n",
1085 obd->obd_next_recovery_transno);
1086 l_wait_event(obd->obd_next_transno_waitq,
1087 check_for_next_transno(obd), &lwi);
1089 spin_lock_bh(&obd->obd_processing_task_lock);
1090 if (obd->obd_abort_recovery) {
1092 } else if (!list_empty(&obd->obd_recovery_queue)) {
1093 req = list_entry(obd->obd_recovery_queue.next,
1094 struct ptlrpc_request, rq_list);
1095 list_del_init(&req->rq_list);
1096 obd->obd_requests_queued_for_recovery--;
1100 spin_unlock_bh(&obd->obd_processing_task_lock);
1104 static int target_recovery_thread(void *arg)
1106 struct obd_device *obd = arg;
1107 struct ptlrpc_request *req;
1108 struct target_recovery_data *trd = &obd->obd_recovery_data;
1109 unsigned long flags;
1112 kportal_daemonize("tgt-recov");
1114 SIGNAL_MASK_LOCK(current, flags);
1115 sigfillset(¤t->blocked);
1117 SIGNAL_MASK_UNLOCK(current, flags);
1119 CERROR("%s: started recovery thread pid %d\n", obd->obd_name,
1121 trd->trd_processing_task = current->pid;
1123 obd->obd_recovering = 1;
1124 complete(&trd->trd_starting);
1126 while (obd->obd_recovering) {
1127 LASSERT(trd->trd_processing_task == current->pid);
1128 req = target_next_replay_req(obd);
1130 char peer_str[PTL_NALFMT_SIZE];
1131 DEBUG_REQ(D_HA, req, "processing t"LPD64" from %s: ",
1132 req->rq_reqmsg->transno,
1133 ptlrpc_peernid2str(&req->rq_peer, peer_str));
1134 (void)trd->trd_recovery_handler(req);
1135 obd->obd_replayed_requests++;
1136 reset_recovery_timer(obd);
1137 /* bug 1580: decide how to properly sync() in recovery*/
1138 //mds_fsync_super(mds->mds_sb);
1139 ptlrpc_free_clone(req);
1140 spin_lock_bh(&obd->obd_processing_task_lock);
1141 obd->obd_next_recovery_transno++;
1142 spin_unlock_bh(&obd->obd_processing_task_lock);
1144 /* recovery is over */
1145 spin_lock_bh(&obd->obd_processing_task_lock);
1146 obd->obd_recovering = 0;
1147 target_cancel_recovery_timer(obd);
1148 if (obd->obd_abort_recovery) {
1149 obd->obd_abort_recovery = 0;
1150 spin_unlock_bh(&obd->obd_processing_task_lock);
1151 target_abort_recovery(obd);
1153 LASSERT(obd->obd_recoverable_clients == 0);
1154 spin_unlock_bh(&obd->obd_processing_task_lock);
1155 target_finish_recovery(obd);
1160 trd->trd_processing_task = 0;
1161 complete(&trd->trd_finishing);
1165 int target_start_recovery_thread(struct obd_device *obd, svc_handler_t handler)
1168 struct target_recovery_data *trd = &obd->obd_recovery_data;
1170 memset(trd, 0, sizeof(*trd));
1171 init_completion(&trd->trd_starting);
1172 init_completion(&trd->trd_finishing);
1173 trd->trd_recovery_handler = handler;
1175 if (kernel_thread(target_recovery_thread, obd, 0) == 0)
1176 wait_for_completion(&trd->trd_starting);
1183 void target_stop_recovery_thread(struct obd_device *obd)
1185 spin_lock_bh(&obd->obd_processing_task_lock);
1186 if (obd->obd_recovery_data.trd_processing_task > 0) {
1187 struct target_recovery_data *trd = &obd->obd_recovery_data;
1188 CERROR("%s: aborting recovery\n", obd->obd_name);
1189 obd->obd_abort_recovery = 1;
1190 wake_up(&obd->obd_next_transno_waitq);
1191 spin_unlock_bh(&obd->obd_processing_task_lock);
1192 wait_for_completion(&trd->trd_finishing);
1194 spin_unlock_bh(&obd->obd_processing_task_lock);
1199 int target_queue_recovery_request(struct ptlrpc_request *req,
1200 struct obd_device *obd)
1202 struct list_head *tmp;
1204 __u64 transno = req->rq_reqmsg->transno;
1206 /* CAVEAT EMPTOR: The incoming request message has been swabbed
1207 * (i.e. buflens etc are in my own byte order), but type-dependent
1208 * buffers (eg mds_body, ost_body etc) have NOT been swabbed. */
1211 INIT_LIST_HEAD(&req->rq_list);
1212 DEBUG_REQ(D_HA, req, "not queueing");
1217 /* If we're processing the queue, we want don't want to queue this
1220 * Also, if this request has a transno less than the one we're waiting
1221 * for, we should process it now. It could (and currently always will)
1222 * be an open request for a descriptor that was opened some time ago.
1224 * Also, a resent, replayed request that has already been
1225 * handled will pass through here and be processed immediately.
1227 spin_lock_bh(&obd->obd_processing_task_lock);
1228 if (obd->obd_recovery_data.trd_processing_task == current->pid ||
1229 transno < obd->obd_next_recovery_transno) {
1230 /* Processing the queue right now, don't re-add. */
1231 LASSERT(list_empty(&req->rq_list));
1232 spin_unlock_bh(&obd->obd_processing_task_lock);
1235 spin_unlock_bh(&obd->obd_processing_task_lock);
1237 /* A resent, replayed request that is still on the queue; just drop it.
1238 The queued request will handle this. */
1239 if ((lustre_msg_get_flags(req->rq_reqmsg) & (MSG_RESENT | MSG_REPLAY))
1240 == (MSG_RESENT | MSG_REPLAY)) {
1241 DEBUG_REQ(D_ERROR, req, "dropping resent queued req");
1245 req = ptlrpc_clone_req(req);
1249 spin_lock_bh(&obd->obd_processing_task_lock);
1252 list_for_each(tmp, &obd->obd_recovery_queue) {
1253 struct ptlrpc_request *reqiter =
1254 list_entry(tmp, struct ptlrpc_request, rq_list);
1256 if (reqiter->rq_reqmsg->transno > transno) {
1257 list_add_tail(&req->rq_list, &reqiter->rq_list);
1264 list_add_tail(&req->rq_list, &obd->obd_recovery_queue);
1267 obd->obd_requests_queued_for_recovery++;
1268 wake_up(&obd->obd_next_transno_waitq);
1269 spin_unlock_bh(&obd->obd_processing_task_lock);
1274 struct obd_device * target_req2obd(struct ptlrpc_request *req)
1276 return req->rq_export->exp_obd;
1279 int target_queue_final_reply(struct ptlrpc_request *req, int rc)
1281 struct obd_device *obd = target_req2obd(req);
1283 LASSERT ((rc == 0) == (req->rq_reply_state != NULL));
1286 /* Just like ptlrpc_error, but without the sending. */
1287 rc = lustre_pack_reply(req, 0, NULL, NULL);
1288 LASSERT(rc == 0); /* XXX handle this */
1289 req->rq_type = PTL_RPC_MSG_ERR;
1292 LASSERT (!req->rq_reply_state->rs_difficult);
1293 LASSERT(list_empty(&req->rq_list));
1295 req = ptlrpc_clone_req(req);
1297 spin_lock_bh(&obd->obd_processing_task_lock);
1299 list_add(&req->rq_list, &obd->obd_delayed_reply_queue);
1301 /* only count the first "replay over" request from each
1303 if (req->rq_export->exp_replay_needed) {
1304 --obd->obd_recoverable_clients;
1305 req->rq_export->exp_replay_needed = 0;
1306 CWARN("%s: %d recoverable clients remain\n",
1307 obd->obd_name, obd->obd_recoverable_clients);
1309 wake_up(&obd->obd_next_transno_waitq);
1310 spin_unlock_bh(&obd->obd_processing_task_lock);
1315 target_send_reply_msg (struct ptlrpc_request *req, int rc, int fail_id)
1317 if (OBD_FAIL_CHECK(fail_id | OBD_FAIL_ONCE)) {
1318 obd_fail_loc |= OBD_FAIL_ONCE | OBD_FAILED;
1319 DEBUG_REQ(D_ERROR, req, "dropping reply");
1320 /* NB this does _not_ send with ACK disabled, to simulate
1321 * sending OK, but timing out for the ACK */
1322 if (req->rq_reply_state != NULL) {
1323 if (!req->rq_reply_state->rs_difficult) {
1324 lustre_free_reply_state (req->rq_reply_state);
1325 req->rq_reply_state = NULL;
1327 struct ptlrpc_service *svc =
1328 req->rq_rqbd->rqbd_srv_ni->sni_service;
1329 atomic_inc(&svc->srv_outstanding_replies);
1336 req->rq_status = rc;
1337 return (ptlrpc_error(req));
1339 DEBUG_REQ(D_NET, req, "sending reply");
1342 return (ptlrpc_send_reply(req, 1));
1346 target_send_reply(struct ptlrpc_request *req, int rc, int fail_id)
1349 unsigned long flags;
1350 struct ptlrpc_reply_state *rs;
1351 struct obd_device *obd;
1352 struct obd_export *exp;
1353 struct ptlrpc_srv_ni *sni;
1354 struct ptlrpc_service *svc;
1356 sni = req->rq_rqbd->rqbd_srv_ni;
1357 svc = sni->sni_service;
1359 rs = req->rq_reply_state;
1360 if (rs == NULL || !rs->rs_difficult) {
1361 /* The easy case; no notifiers and reply_out_callback()
1362 * cleans up (i.e. we can't look inside rs after a
1363 * successful send) */
1364 netrc = target_send_reply_msg (req, rc, fail_id);
1366 LASSERT (netrc == 0 || req->rq_reply_state == NULL);
1370 /* must be an export if locks saved */
1371 LASSERT (req->rq_export != NULL);
1372 /* req/reply consistent */
1373 LASSERT (rs->rs_srv_ni == sni);
1376 LASSERT (!rs->rs_scheduled);
1377 LASSERT (!rs->rs_scheduled_ever);
1378 LASSERT (!rs->rs_handled);
1379 LASSERT (!rs->rs_on_net);
1380 LASSERT (rs->rs_export == NULL);
1381 LASSERT (list_empty(&rs->rs_obd_list));
1382 LASSERT (list_empty(&rs->rs_exp_list));
1384 exp = class_export_get (req->rq_export);
1387 /* disable reply scheduling onto srv_reply_queue while I'm setting up */
1388 rs->rs_scheduled = 1;
1390 rs->rs_xid = req->rq_xid;
1391 rs->rs_transno = req->rq_transno;
1392 rs->rs_export = exp;
1394 spin_lock_irqsave (&obd->obd_uncommitted_replies_lock, flags);
1396 if (rs->rs_transno > obd->obd_last_committed) {
1397 /* not committed already */
1398 list_add_tail (&rs->rs_obd_list,
1399 &obd->obd_uncommitted_replies);
1402 spin_unlock (&obd->obd_uncommitted_replies_lock);
1403 spin_lock (&exp->exp_lock);
1405 list_add_tail (&rs->rs_exp_list, &exp->exp_outstanding_replies);
1407 spin_unlock_irqrestore (&exp->exp_lock, flags);
1409 netrc = target_send_reply_msg (req, rc, fail_id);
1411 spin_lock_irqsave (&svc->srv_lock, flags);
1413 svc->srv_n_difficult_replies++;
1415 if (netrc != 0) /* error sending: reply is off the net */
1418 if (!rs->rs_on_net || /* some notifier */
1419 list_empty(&rs->rs_exp_list) || /* completed already */
1420 list_empty(&rs->rs_obd_list)) {
1421 list_add_tail (&rs->rs_list, &svc->srv_reply_queue);
1422 wake_up (&svc->srv_waitq);
1424 list_add (&rs->rs_list, &sni->sni_active_replies);
1425 rs->rs_scheduled = 0; /* allow notifier to schedule */
1428 spin_unlock_irqrestore (&svc->srv_lock, flags);
1431 int target_handle_ping(struct ptlrpc_request *req)
1433 return lustre_pack_reply(req, 0, NULL, NULL);