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;
264 CERROR("ldlm_get_ref failed: %d\n", rc);
268 ptlrpc_init_client(rq_portal, rp_portal, name,
269 &obddev->obd_ldlm_client);
271 imp = class_new_import();
273 GOTO(err_ldlm, rc = -ENOENT);
274 imp->imp_client = &obddev->obd_ldlm_client;
275 imp->imp_obd = obddev;
276 imp->imp_connect_op = connect_op;
277 imp->imp_generation = 0;
278 imp->imp_initial_recov = 1;
279 INIT_LIST_HEAD(&imp->imp_pinger_chain);
280 memcpy(imp->imp_target_uuid.uuid, lcfg->lcfg_inlbuf1,
282 class_import_put(imp);
284 rc = client_import_add_conn(imp, &server_uuid, 1);
286 CERROR("can't add initial connection\n");
287 GOTO(err_import, rc);
290 cli->cl_import = imp;
291 cli->cl_max_mds_easize = sizeof(struct lov_mds_md);
292 cli->cl_max_mds_cookiesize = sizeof(struct llog_cookie);
293 cli->cl_sandev = to_kdev_t(0);
295 if (lcfg->lcfg_inllen3 != 0) {
296 if (!strcmp(lcfg->lcfg_inlbuf3, "inactive")) {
297 CDEBUG(D_HA, "marking %s %s->%s as inactive\n",
298 name, obddev->obd_name,
299 imp->imp_target_uuid.uuid);
300 imp->imp_invalid = 1;
302 if (lcfg->lcfg_inllen4 != 0)
303 mgmt_name = lcfg->lcfg_inlbuf4;
305 mgmt_name = lcfg->lcfg_inlbuf3;
309 if (mgmt_name != NULL) {
310 /* Register with management client if we need to. */
311 CDEBUG(D_HA, "%s registering with %s for events about %s\n",
312 obddev->obd_name, mgmt_name, server_uuid.uuid);
314 mgmt_obd = class_name2obd(mgmt_name);
316 CERROR("can't find mgmtcli %s to register\n",
318 GOTO(err_import, rc = -ENOSYS);
321 register_f = inter_module_get("mgmtcli_register_for_events");
323 CERROR("can't i_m_g mgmtcli_register_for_events\n");
324 GOTO(err_import, rc = -ENOSYS);
327 rc = register_f(mgmt_obd, obddev, &imp->imp_target_uuid);
328 inter_module_put("mgmtcli_register_for_events");
331 cli->cl_mgmtcli_obd = mgmt_obd;
337 class_destroy_import(imp);
345 int client_obd_cleanup(struct obd_device *obddev, int flags)
347 struct client_obd *cli = &obddev->u.cli;
352 if (cli->cl_mgmtcli_obd) {
353 mgmtcli_deregister_for_events_t dereg_f;
355 dereg_f = inter_module_get("mgmtcli_deregister_for_events");
356 dereg_f(cli->cl_mgmtcli_obd, obddev);
357 inter_module_put("mgmtcli_deregister_for_events");
360 /* Here we try to drop the security structure after destroy import,
361 * to avoid issue of "sleep in spinlock".
363 class_import_get(cli->cl_import);
364 class_destroy_import(cli->cl_import);
365 ptlrpcs_import_drop_sec(cli->cl_import);
366 class_import_put(cli->cl_import);
367 cli->cl_import = NULL;
369 ldlm_put_ref(flags & OBD_OPT_FORCE);
374 int client_connect_import(struct lustre_handle *dlm_handle,
375 struct obd_device *obd,
376 struct obd_uuid *cluuid,
377 unsigned long connect_flags)
379 struct client_obd *cli = &obd->u.cli;
380 struct obd_import *imp = cli->cl_import;
381 struct obd_export *exp;
386 rc = class_connect(dlm_handle, obd, cluuid);
390 cli->cl_conn_count++;
391 if (cli->cl_conn_count > 1)
393 exp = class_conn2export(dlm_handle);
395 if (obd->obd_namespace != NULL)
396 CERROR("already have namespace!\n");
397 obd->obd_namespace = ldlm_namespace_new(obd->obd_name,
398 LDLM_NAMESPACE_CLIENT);
399 if (obd->obd_namespace == NULL)
400 GOTO(out_disco, rc = -ENOMEM);
402 rc = ptlrpcs_import_get_sec(imp);
406 imp->imp_dlm_handle = *dlm_handle;
407 rc = ptlrpc_init_import(imp);
411 imp->imp_connect_flags = connect_flags;
412 rc = ptlrpc_connect_import(imp, NULL);
414 LASSERT (imp->imp_state == LUSTRE_IMP_DISCON);
417 LASSERT(exp->exp_connection);
418 ptlrpc_pinger_add_import(imp);
423 ldlm_namespace_free(obd->obd_namespace, 0);
424 obd->obd_namespace = NULL;
426 cli->cl_conn_count--;
427 class_disconnect(exp, 0);
429 class_export_put(exp);
436 int client_disconnect_export(struct obd_export *exp, unsigned long flags)
438 struct obd_device *obd = class_exp2obd(exp);
439 struct client_obd *cli = &obd->u.cli;
440 struct obd_import *imp = cli->cl_import;
445 CERROR("invalid export for disconnect: exp %p cookie "LPX64"\n",
446 exp, exp ? exp->exp_handle.h_cookie : -1);
451 if (!cli->cl_conn_count) {
452 CERROR("disconnecting disconnected device (%s)\n",
454 GOTO(out_sem, rc = -EINVAL);
457 cli->cl_conn_count--;
458 if (cli->cl_conn_count)
459 GOTO(out_no_disconnect, rc = 0);
461 /* Some non-replayable imports (MDS's OSCs) are pinged, so just
462 * delete it regardless. (It's safe to delete an import that was
464 (void)ptlrpc_pinger_del_import(imp);
466 if (obd->obd_namespace != NULL) {
467 /* obd_no_recov == local only */
468 ldlm_cli_cancel_unused(obd->obd_namespace, NULL,
469 obd->obd_no_recov, NULL);
470 ldlm_namespace_free(obd->obd_namespace, obd->obd_no_recov);
471 obd->obd_namespace = NULL;
475 * Yeah, obd_no_recov also (mainly) means "forced shutdown".
477 if (obd->obd_no_recov)
478 ptlrpc_invalidate_import(imp, 0);
480 rc = ptlrpc_disconnect_import(imp);
484 err = class_disconnect(exp, 0);
492 /* --------------------------------------------------------------------------
493 * from old lib/target.c
494 * -------------------------------------------------------------------------- */
496 int target_handle_reconnect(struct lustre_handle *conn, struct obd_export *exp,
497 struct obd_uuid *cluuid, int initial_conn)
499 if (exp->exp_connection && !initial_conn) {
500 struct lustre_handle *hdl;
501 hdl = &exp->exp_imp_reverse->imp_remote_handle;
502 /* Might be a re-connect after a partition. */
503 if (!memcmp(&conn->cookie, &hdl->cookie, sizeof conn->cookie)) {
504 CERROR("%s reconnecting\n", cluuid->uuid);
505 conn->cookie = exp->exp_handle.h_cookie;
508 CERROR("%s reconnecting from %s, "
509 "handle mismatch (ours "LPX64", theirs "
510 LPX64")\n", cluuid->uuid,
511 exp->exp_connection->c_remote_uuid.uuid,
512 hdl->cookie, conn->cookie);
513 memset(conn, 0, sizeof *conn);
518 conn->cookie = exp->exp_handle.h_cookie;
519 CDEBUG(D_INFO, "existing export for UUID '%s' at %p\n",
521 CDEBUG(D_IOCTL,"connect: cookie "LPX64"\n", conn->cookie);
525 static inline int ptlrpc_peer_is_local(struct ptlrpc_peer *peer)
527 ptl_process_id_t myid;
529 PtlGetId(peer->peer_ni->pni_ni_h, &myid);
530 return (memcmp(&peer->peer_id, &myid, sizeof(myid)) == 0);
533 int target_handle_connect(struct ptlrpc_request *req)
535 unsigned long connect_flags = 0, *cfp;
536 struct obd_device *target;
537 struct obd_export *export = NULL;
538 struct obd_import *revimp;
539 struct lustre_handle conn;
540 struct obd_uuid tgtuuid;
541 struct obd_uuid cluuid;
542 struct obd_uuid remote_uuid;
547 int initial_conn = 0;
548 char peer_str[PTL_NALFMT_SIZE];
551 OBD_RACE(OBD_FAIL_TGT_CONN_RACE);
553 LASSERT_REQSWAB (req, 0);
554 str = lustre_msg_string(req->rq_reqmsg, 0, sizeof(tgtuuid) - 1);
556 CERROR("bad target UUID for connect\n");
557 GOTO(out, rc = -EINVAL);
560 obd_str2uuid (&tgtuuid, str);
561 target = class_uuid2obd(&tgtuuid);
563 target = class_name2obd(str);
565 if (!target || target->obd_stopping || !target->obd_set_up) {
566 CERROR("UUID '%s' is not available for connect from %s\n",
567 str, req->rq_peerstr);
568 GOTO(out, rc = -ENODEV);
571 LASSERT_REQSWAB (req, 1);
572 str = lustre_msg_string(req->rq_reqmsg, 1, sizeof(cluuid) - 1);
574 CERROR("bad client UUID for connect\n");
575 GOTO(out, rc = -EINVAL);
578 obd_str2uuid (&cluuid, str);
580 /* XXX extract a nettype and format accordingly */
581 switch (sizeof(ptl_nid_t)) {
582 /* NB the casts only avoid compiler warnings */
584 snprintf(remote_uuid.uuid, sizeof remote_uuid,
585 "NET_"LPX64"_UUID", (__u64)req->rq_peer.peer_id.nid);
588 snprintf(remote_uuid.uuid, sizeof remote_uuid,
589 "NET_%x_UUID", (__u32)req->rq_peer.peer_id.nid);
595 tmp = lustre_msg_buf(req->rq_reqmsg, 2, sizeof conn);
597 GOTO(out, rc = -EPROTO);
599 memcpy(&conn, tmp, sizeof conn);
601 cfp = lustre_msg_buf(req->rq_reqmsg, 3, sizeof(unsigned long));
602 LASSERT(cfp != NULL);
603 connect_flags = *cfp;
605 rc = lustre_pack_reply(req, 0, NULL, NULL);
609 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_INITIAL)
612 /* lctl gets a backstage, all-access pass. */
613 if (obd_uuid_equals(&cluuid, &target->obd_uuid))
614 goto dont_check_exports;
616 spin_lock(&target->obd_dev_lock);
617 list_for_each(p, &target->obd_exports) {
618 export = list_entry(p, struct obd_export, exp_obd_chain);
619 if (obd_uuid_equals(&cluuid, &export->exp_client_uuid)) {
620 spin_unlock(&target->obd_dev_lock);
621 LASSERT(export->exp_obd == target);
623 rc = target_handle_reconnect(&conn, export, &cluuid,
629 /* If we found an export, we already unlocked. */
631 spin_unlock(&target->obd_dev_lock);
632 } else if (req->rq_export == NULL &&
633 atomic_read(&export->exp_rpc_count) > 0) {
634 CWARN("%s: refuse connection from %s/%s to 0x%p/%d\n",
635 target->obd_name, cluuid.uuid,
636 ptlrpc_peernid2str(&req->rq_peer, peer_str),
637 export, atomic_read(&export->exp_refcount));
638 GOTO(out, rc = -EBUSY);
639 } else if (req->rq_export != NULL &&
640 atomic_read(&export->exp_rpc_count) > 1) {
641 CWARN("%s: refuse reconnection from %s@%s to 0x%p/%d\n",
642 target->obd_name, cluuid.uuid,
643 ptlrpc_peernid2str(&req->rq_peer, peer_str),
644 export, atomic_read(&export->exp_rpc_count));
645 GOTO(out, rc = -EBUSY);
646 } else if (req->rq_reqmsg->conn_cnt == 1 && !initial_conn) {
647 CERROR("%s reconnected with 1 conn_cnt; cookies not random?\n",
649 GOTO(out, rc = -EALREADY);
652 /* Tell the client if we're in recovery. */
653 /* If this is the first client, start the recovery timer */
654 CWARN("%s: connection from %s@%s/%lu %s\n", target->obd_name, cluuid.uuid,
655 ptlrpc_peernid2str(&req->rq_peer, peer_str), *cfp,
656 target->obd_recovering ? "(recovering)" : "");
658 if (target->obd_recovering) {
659 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
660 target_start_recovery_timer(target);
664 /* Tell the client if we support replayable requests */
665 if (target->obd_replayable)
666 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
669 if (export == NULL) {
670 if (target->obd_recovering) {
671 CERROR("%s denying connection for new client %s@%s: "
672 "%d clients in recovery for %lds\n", target->obd_name,
674 ptlrpc_peernid2str(&req->rq_peer, peer_str),
675 target->obd_recoverable_clients,
676 (target->obd_recovery_timer.expires-jiffies)/HZ);
680 rc = obd_connect(&conn, target, &cluuid, connect_flags);
683 /* Tell the client if we support replayable requests */
684 if (target->obd_replayable)
685 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
687 /* If all else goes well, this is our RPC return code. */
690 if (rc && rc != EALREADY)
693 req->rq_repmsg->handle = conn;
695 /* If the client and the server are the same node, we will already
696 * have an export that really points to the client's DLM export,
697 * because we have a shared handles table.
699 * XXX this will go away when shaver stops sending the "connect" handle
700 * in the real "remote handle" field of the request --phik 24 Apr 2003
702 if (req->rq_export != NULL)
703 class_export_put(req->rq_export);
705 /* ownership of this export ref transfers to the request */
706 export = req->rq_export = class_conn2export(&conn);
707 LASSERT(export != NULL);
709 spin_lock_irqsave(&export->exp_lock, flags);
711 req->rq_repmsg->conn_cnt = export->exp_conn_cnt + 1;
712 } else if (export->exp_conn_cnt >= req->rq_reqmsg->conn_cnt) {
713 CERROR("%s@%s: already connected at a higher conn_cnt: %d > %d\n",
714 cluuid.uuid, ptlrpc_peernid2str(&req->rq_peer, peer_str),
715 export->exp_conn_cnt,
716 req->rq_reqmsg->conn_cnt);
717 spin_unlock_irqrestore(&export->exp_lock, flags);
718 GOTO(out, rc = -EALREADY);
720 export->exp_conn_cnt = req->rq_reqmsg->conn_cnt;
721 spin_unlock_irqrestore(&export->exp_lock, flags);
723 /* request from liblustre? */
724 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT)
725 export->exp_libclient = 1;
727 if (!(lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_ASYNC) &&
728 ptlrpc_peer_is_local(&req->rq_peer)) {
729 CWARN("%s: exp %p set sync\n", target->obd_name, export);
730 export->exp_sync = 1;
732 CDEBUG(D_HA, "%s: exp %p set async\n",target->obd_name,export);
733 export->exp_sync = 0;
736 if (export->exp_connection != NULL)
737 ptlrpc_put_connection(export->exp_connection);
738 export->exp_connection = ptlrpc_get_connection(&req->rq_peer,
741 if (rc == EALREADY) {
742 /* We indicate the reconnection in a flag, not an error code. */
743 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECONNECT);
747 if (target->obd_recovering)
748 target->obd_connected_clients++;
750 memcpy(&conn, lustre_msg_buf(req->rq_reqmsg, 2, sizeof(conn)),
753 if (export->exp_imp_reverse != NULL) {
754 /* same logic as client_obd_cleanup */
755 class_import_get(export->exp_imp_reverse);
756 class_destroy_import(export->exp_imp_reverse);
757 ptlrpcs_import_drop_sec(export->exp_imp_reverse);
758 class_import_put(export->exp_imp_reverse);
761 /* for the rest part, we return -ENOTCONN in case of errors
762 * in order to let client initialize connection again.
764 revimp = export->exp_imp_reverse = class_new_import();
766 CERROR("fail to alloc new reverse import.\n");
767 GOTO(out, rc = -ENOTCONN);
770 revimp->imp_connection = ptlrpc_connection_addref(export->exp_connection);
771 revimp->imp_client = &export->exp_obd->obd_ldlm_client;
772 revimp->imp_remote_handle = conn;
773 revimp->imp_obd = target;
774 revimp->imp_dlm_fake = 1;
775 revimp->imp_state = LUSTRE_IMP_FULL;
777 rc = ptlrpcs_import_get_sec(revimp);
779 CERROR("reverse import can not get sec: %d\n", rc);
780 class_destroy_import(revimp);
781 export->exp_imp_reverse = NULL;
782 GOTO(out, rc = -ENOTCONN);
785 class_import_put(revimp);
787 rc = obd_connect_post(export, connect_flags);
794 int target_handle_disconnect(struct ptlrpc_request *req)
796 struct obd_export *exp;
800 rc = lustre_pack_reply(req, 0, NULL, NULL);
804 /* keep the rq_export around so we can send the reply */
805 exp = class_export_get(req->rq_export);
806 req->rq_status = obd_disconnect(exp, 0);
810 void target_destroy_export(struct obd_export *exp)
812 /* exports created from last_rcvd data, and "fake"
813 exports created by lctl don't have an import */
814 if (exp->exp_imp_reverse != NULL) {
815 ptlrpcs_import_drop_sec(exp->exp_imp_reverse);
816 class_destroy_import(exp->exp_imp_reverse);
819 /* We cancel locks at disconnect time, but this will catch any locks
820 * granted in a race with recovery-induced disconnect. */
821 if (exp->exp_obd->obd_namespace != NULL)
822 ldlm_cancel_locks_for_export(exp);
829 struct ptlrpc_request *
830 ptlrpc_clone_req( struct ptlrpc_request *orig_req)
832 struct ptlrpc_request *copy_req;
833 struct lustre_msg *copy_reqmsg;
835 OBD_ALLOC(copy_req, sizeof *copy_req);
838 OBD_ALLOC(copy_reqmsg, orig_req->rq_reqlen);
840 OBD_FREE(copy_req, sizeof *copy_req);
844 memcpy(copy_req, orig_req, sizeof *copy_req);
845 memcpy(copy_reqmsg, orig_req->rq_reqmsg, orig_req->rq_reqlen);
846 /* the copied req takes over the reply state and security data */
847 orig_req->rq_reply_state = NULL;
848 orig_req->rq_sec_svcdata = NULL;
850 copy_req->rq_reqmsg = copy_reqmsg;
851 class_export_get(copy_req->rq_export);
852 INIT_LIST_HEAD(©_req->rq_list);
857 void ptlrpc_free_clone( struct ptlrpc_request *req)
860 svcsec_cleanup_req(req);
862 class_export_put(req->rq_export);
863 list_del(&req->rq_list);
864 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
865 OBD_FREE(req, sizeof *req);
868 static void target_release_saved_req(struct ptlrpc_request *req)
871 svcsec_cleanup_req(req);
873 class_export_put(req->rq_export);
874 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
875 OBD_FREE(req, sizeof *req);
878 static void target_finish_recovery(struct obd_device *obd)
880 struct list_head *tmp, *n;
883 CWARN("%s: sending delayed replies to recovered clients\n",
886 ldlm_reprocess_all_ns(obd->obd_namespace);
888 /* when recovery finished, cleanup orphans on mds and ost */
889 if (OBT(obd) && OBP(obd, postrecov)) {
890 rc = OBP(obd, postrecov)(obd);
892 CWARN("%s: all clients recovered, %d MDS "
893 "orphans deleted\n", obd->obd_name, rc);
895 CERROR("postrecov failed %d\n", rc);
899 list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
900 struct ptlrpc_request *req;
901 req = list_entry(tmp, struct ptlrpc_request, rq_list);
902 list_del(&req->rq_list);
903 DEBUG_REQ(D_ERROR, req, "delayed:");
905 target_release_saved_req(req);
907 obd->obd_recovery_end = LTIME_S(CURRENT_TIME);
911 static void abort_recovery_queue(struct obd_device *obd)
913 struct ptlrpc_request *req;
914 struct list_head *tmp, *n;
917 list_for_each_safe(tmp, n, &obd->obd_recovery_queue) {
918 req = list_entry(tmp, struct ptlrpc_request, rq_list);
919 list_del(&req->rq_list);
920 DEBUG_REQ(D_ERROR, req, "aborted:");
921 req->rq_status = -ENOTCONN;
922 req->rq_type = PTL_RPC_MSG_ERR;
923 rc = lustre_pack_reply(req, 0, NULL, NULL);
927 DEBUG_REQ(D_ERROR, req,
928 "packing failed for abort-reply; skipping");
930 target_release_saved_req(req);
934 /* Called from a cleanup function if the device is being cleaned up
935 forcefully. The exports should all have been disconnected already,
936 the only thing left to do is
937 - clear the recovery flags
939 - free queued requests and replies, but don't send replies
940 Because the obd_stopping flag is set, no new requests should be received.
943 void target_cleanup_recovery(struct obd_device *obd)
945 struct list_head *tmp, *n;
946 struct ptlrpc_request *req;
948 spin_lock_bh(&obd->obd_processing_task_lock);
949 if (!obd->obd_recovering) {
950 spin_unlock_bh(&obd->obd_processing_task_lock);
954 obd->obd_recovering = obd->obd_abort_recovery = 0;
955 target_cancel_recovery_timer(obd);
956 spin_unlock_bh(&obd->obd_processing_task_lock);
958 list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
959 req = list_entry(tmp, struct ptlrpc_request, rq_list);
960 list_del(&req->rq_list);
961 LASSERT (req->rq_reply_state);
962 lustre_free_reply_state(req->rq_reply_state);
963 target_release_saved_req(req);
965 list_for_each_safe(tmp, n, &obd->obd_recovery_queue) {
966 req = list_entry(tmp, struct ptlrpc_request, rq_list);
967 list_del(&req->rq_list);
968 LASSERT (req->rq_reply_state == 0);
969 target_release_saved_req(req);
973 static void target_abort_recovery(void *data)
975 struct obd_device *obd = data;
977 LASSERT(!obd->obd_recovering);
979 class_disconnect_stale_exports(obd, 0);
981 CERROR("%s: recovery period over; disconnecting unfinished clients.\n",
984 abort_recovery_queue(obd);
985 target_finish_recovery(obd);
986 ptlrpc_run_recovery_over_upcall(obd);
989 static void target_recovery_expired(unsigned long castmeharder)
991 struct obd_device *obd = (struct obd_device *)castmeharder;
992 CERROR("recovery timed out, aborting\n");
993 spin_lock_bh(&obd->obd_processing_task_lock);
994 if (obd->obd_recovering)
995 obd->obd_abort_recovery = 1;
997 wake_up(&obd->obd_next_transno_waitq);
998 spin_unlock_bh(&obd->obd_processing_task_lock);
1002 /* obd_processing_task_lock should be held */
1003 void target_cancel_recovery_timer(struct obd_device *obd)
1005 CDEBUG(D_HA, "%s: cancel recovery timer\n", obd->obd_name);
1006 del_timer(&obd->obd_recovery_timer);
1010 static void reset_recovery_timer(struct obd_device *obd)
1012 spin_lock_bh(&obd->obd_processing_task_lock);
1013 if (!obd->obd_recovering) {
1014 spin_unlock_bh(&obd->obd_processing_task_lock);
1017 CDEBUG(D_HA, "timer will expire in %u seconds\n",
1018 OBD_RECOVERY_TIMEOUT / HZ);
1019 mod_timer(&obd->obd_recovery_timer, jiffies + OBD_RECOVERY_TIMEOUT);
1020 spin_unlock_bh(&obd->obd_processing_task_lock);
1024 /* Only start it the first time called */
1025 void target_start_recovery_timer(struct obd_device *obd)
1027 spin_lock_bh(&obd->obd_processing_task_lock);
1028 if (!obd->obd_recovering || timer_pending(&obd->obd_recovery_timer)) {
1029 spin_unlock_bh(&obd->obd_processing_task_lock);
1032 CWARN("%s: starting recovery timer (%us)\n", obd->obd_name,
1033 OBD_RECOVERY_TIMEOUT / HZ);
1034 obd->obd_recovery_timer.function = target_recovery_expired;
1035 obd->obd_recovery_timer.data = (unsigned long)obd;
1036 mod_timer(&obd->obd_recovery_timer, jiffies + OBD_RECOVERY_TIMEOUT);
1037 spin_unlock_bh(&obd->obd_processing_task_lock);
1041 static int check_for_next_transno(struct obd_device *obd)
1043 struct ptlrpc_request *req = NULL;
1044 int wake_up = 0, connected, completed, queue_len, max;
1045 __u64 next_transno, req_transno;
1047 spin_lock_bh(&obd->obd_processing_task_lock);
1048 if (!list_empty(&obd->obd_recovery_queue)) {
1049 req = list_entry(obd->obd_recovery_queue.next,
1050 struct ptlrpc_request, rq_list);
1051 req_transno = req->rq_reqmsg->transno;
1056 max = obd->obd_max_recoverable_clients;
1057 connected = obd->obd_connected_clients;
1058 completed = max - obd->obd_recoverable_clients;
1059 queue_len = obd->obd_requests_queued_for_recovery;
1060 next_transno = obd->obd_next_recovery_transno;
1062 CDEBUG(D_HA,"max: %d, connected: %d, completed: %d, queue_len: %d, "
1063 "req_transno: "LPU64", next_transno: "LPU64"\n",
1064 max, connected, completed, queue_len, req_transno, next_transno);
1065 if (obd->obd_abort_recovery) {
1066 CDEBUG(D_HA, "waking for aborted recovery\n");
1068 } else if (max == completed) {
1069 CDEBUG(D_HA, "waking for completed recovery\n");
1071 } else if (req_transno == next_transno) {
1072 CDEBUG(D_HA, "waking for next ("LPD64")\n", next_transno);
1074 } else if (queue_len + completed == max) {
1075 LASSERT(req->rq_reqmsg->transno >= next_transno);
1077 "waking for skipped transno (skip: "LPD64
1078 ", ql: %d, comp: %d, conn: %d, next: "LPD64")\n",
1079 next_transno, queue_len, completed, max, req_transno);
1080 obd->obd_next_recovery_transno = req_transno;
1083 spin_unlock_bh(&obd->obd_processing_task_lock);
1088 static struct ptlrpc_request *
1089 target_next_replay_req(struct obd_device *obd)
1091 struct l_wait_info lwi = { 0 };
1092 struct ptlrpc_request *req;
1094 CDEBUG(D_HA, "Waiting for transno "LPD64"\n",
1095 obd->obd_next_recovery_transno);
1096 l_wait_event(obd->obd_next_transno_waitq,
1097 check_for_next_transno(obd), &lwi);
1099 spin_lock_bh(&obd->obd_processing_task_lock);
1100 if (obd->obd_abort_recovery) {
1102 } else if (!list_empty(&obd->obd_recovery_queue)) {
1103 req = list_entry(obd->obd_recovery_queue.next,
1104 struct ptlrpc_request, rq_list);
1105 list_del_init(&req->rq_list);
1106 obd->obd_requests_queued_for_recovery--;
1110 spin_unlock_bh(&obd->obd_processing_task_lock);
1114 static int target_recovery_thread(void *arg)
1116 struct obd_device *obd = arg;
1117 struct ptlrpc_request *req;
1118 struct target_recovery_data *trd = &obd->obd_recovery_data;
1119 unsigned long flags;
1122 kportal_daemonize("tgt-recov");
1124 SIGNAL_MASK_LOCK(current, flags);
1125 sigfillset(¤t->blocked);
1127 SIGNAL_MASK_UNLOCK(current, flags);
1129 CERROR("%s: started recovery thread pid %d\n", obd->obd_name,
1131 trd->trd_processing_task = current->pid;
1133 obd->obd_recovering = 1;
1134 complete(&trd->trd_starting);
1136 while (obd->obd_recovering) {
1137 LASSERT(trd->trd_processing_task == current->pid);
1138 req = target_next_replay_req(obd);
1140 char peer_str[PTL_NALFMT_SIZE];
1141 DEBUG_REQ(D_HA, req, "processing t"LPD64" from %s: ",
1142 req->rq_reqmsg->transno,
1143 ptlrpc_peernid2str(&req->rq_peer, peer_str));
1144 (void)trd->trd_recovery_handler(req);
1145 obd->obd_replayed_requests++;
1146 reset_recovery_timer(obd);
1147 /* bug 1580: decide how to properly sync() in recovery*/
1148 //mds_fsync_super(mds->mds_sb);
1149 ptlrpc_free_clone(req);
1150 spin_lock_bh(&obd->obd_processing_task_lock);
1151 obd->obd_next_recovery_transno++;
1152 spin_unlock_bh(&obd->obd_processing_task_lock);
1154 /* recovery is over */
1155 spin_lock_bh(&obd->obd_processing_task_lock);
1156 obd->obd_recovering = 0;
1157 target_cancel_recovery_timer(obd);
1158 if (obd->obd_abort_recovery) {
1159 obd->obd_abort_recovery = 0;
1160 spin_unlock_bh(&obd->obd_processing_task_lock);
1161 target_abort_recovery(obd);
1163 LASSERT(obd->obd_recoverable_clients == 0);
1164 spin_unlock_bh(&obd->obd_processing_task_lock);
1165 target_finish_recovery(obd);
1170 trd->trd_processing_task = 0;
1171 complete(&trd->trd_finishing);
1175 int target_start_recovery_thread(struct obd_device *obd, svc_handler_t handler)
1178 struct target_recovery_data *trd = &obd->obd_recovery_data;
1180 memset(trd, 0, sizeof(*trd));
1181 init_completion(&trd->trd_starting);
1182 init_completion(&trd->trd_finishing);
1183 trd->trd_recovery_handler = handler;
1185 if (kernel_thread(target_recovery_thread, obd, 0) == 0)
1186 wait_for_completion(&trd->trd_starting);
1193 void target_stop_recovery_thread(struct obd_device *obd)
1195 spin_lock_bh(&obd->obd_processing_task_lock);
1196 if (obd->obd_recovery_data.trd_processing_task > 0) {
1197 struct target_recovery_data *trd = &obd->obd_recovery_data;
1198 CERROR("%s: aborting recovery\n", obd->obd_name);
1199 obd->obd_abort_recovery = 1;
1200 wake_up(&obd->obd_next_transno_waitq);
1201 spin_unlock_bh(&obd->obd_processing_task_lock);
1202 wait_for_completion(&trd->trd_finishing);
1204 spin_unlock_bh(&obd->obd_processing_task_lock);
1209 int target_queue_recovery_request(struct ptlrpc_request *req,
1210 struct obd_device *obd)
1212 struct list_head *tmp;
1214 __u64 transno = req->rq_reqmsg->transno;
1216 /* CAVEAT EMPTOR: The incoming request message has been swabbed
1217 * (i.e. buflens etc are in my own byte order), but type-dependent
1218 * buffers (eg mds_body, ost_body etc) have NOT been swabbed. */
1221 INIT_LIST_HEAD(&req->rq_list);
1222 DEBUG_REQ(D_HA, req, "not queueing");
1227 /* If we're processing the queue, we want don't want to queue this
1230 * Also, if this request has a transno less than the one we're waiting
1231 * for, we should process it now. It could (and currently always will)
1232 * be an open request for a descriptor that was opened some time ago.
1234 * Also, a resent, replayed request that has already been
1235 * handled will pass through here and be processed immediately.
1237 spin_lock_bh(&obd->obd_processing_task_lock);
1238 if (obd->obd_recovery_data.trd_processing_task == current->pid ||
1239 transno < obd->obd_next_recovery_transno) {
1240 /* Processing the queue right now, don't re-add. */
1241 LASSERT(list_empty(&req->rq_list));
1242 spin_unlock_bh(&obd->obd_processing_task_lock);
1245 spin_unlock_bh(&obd->obd_processing_task_lock);
1247 /* A resent, replayed request that is still on the queue; just drop it.
1248 The queued request will handle this. */
1249 if ((lustre_msg_get_flags(req->rq_reqmsg) & (MSG_RESENT | MSG_REPLAY))
1250 == (MSG_RESENT | MSG_REPLAY)) {
1251 DEBUG_REQ(D_ERROR, req, "dropping resent queued req");
1255 req = ptlrpc_clone_req(req);
1259 spin_lock_bh(&obd->obd_processing_task_lock);
1262 list_for_each(tmp, &obd->obd_recovery_queue) {
1263 struct ptlrpc_request *reqiter =
1264 list_entry(tmp, struct ptlrpc_request, rq_list);
1266 if (reqiter->rq_reqmsg->transno > transno) {
1267 list_add_tail(&req->rq_list, &reqiter->rq_list);
1274 list_add_tail(&req->rq_list, &obd->obd_recovery_queue);
1276 obd->obd_requests_queued_for_recovery++;
1277 wake_up(&obd->obd_next_transno_waitq);
1278 spin_unlock_bh(&obd->obd_processing_task_lock);
1282 struct obd_device * target_req2obd(struct ptlrpc_request *req)
1284 return req->rq_export->exp_obd;
1287 int target_queue_final_reply(struct ptlrpc_request *req, int rc)
1289 struct obd_device *obd = target_req2obd(req);
1291 LASSERT ((rc == 0) == (req->rq_reply_state != NULL));
1294 /* Just like ptlrpc_error, but without the sending. */
1295 rc = lustre_pack_reply(req, 0, NULL, NULL);
1296 LASSERT(rc == 0); /* XXX handle this */
1297 req->rq_type = PTL_RPC_MSG_ERR;
1300 LASSERT (!req->rq_reply_state->rs_difficult);
1301 LASSERT(list_empty(&req->rq_list));
1303 req = ptlrpc_clone_req(req);
1305 spin_lock_bh(&obd->obd_processing_task_lock);
1307 list_add(&req->rq_list, &obd->obd_delayed_reply_queue);
1309 /* only count the first "replay over" request from each
1311 if (req->rq_export->exp_replay_needed) {
1312 --obd->obd_recoverable_clients;
1313 req->rq_export->exp_replay_needed = 0;
1314 CWARN("%s: %d recoverable clients remain\n",
1315 obd->obd_name, obd->obd_recoverable_clients);
1317 wake_up(&obd->obd_next_transno_waitq);
1318 spin_unlock_bh(&obd->obd_processing_task_lock);
1323 target_send_reply_msg (struct ptlrpc_request *req, int rc, int fail_id)
1325 if (OBD_FAIL_CHECK(fail_id | OBD_FAIL_ONCE)) {
1326 obd_fail_loc |= OBD_FAIL_ONCE | OBD_FAILED;
1327 DEBUG_REQ(D_ERROR, req, "dropping reply");
1328 /* NB this does _not_ send with ACK disabled, to simulate
1329 * sending OK, but timing out for the ACK */
1330 if (req->rq_reply_state != NULL) {
1331 if (!req->rq_reply_state->rs_difficult) {
1332 lustre_free_reply_state (req->rq_reply_state);
1333 req->rq_reply_state = NULL;
1335 struct ptlrpc_service *svc =
1336 req->rq_rqbd->rqbd_srv_ni->sni_service;
1337 atomic_inc(&svc->srv_outstanding_replies);
1344 req->rq_status = rc;
1345 return (ptlrpc_error(req));
1347 DEBUG_REQ(D_NET, req, "sending reply");
1350 return (ptlrpc_send_reply(req, 1));
1354 target_send_reply(struct ptlrpc_request *req, int rc, int fail_id)
1357 unsigned long flags;
1358 struct ptlrpc_reply_state *rs;
1359 struct obd_device *obd;
1360 struct obd_export *exp;
1361 struct ptlrpc_srv_ni *sni;
1362 struct ptlrpc_service *svc;
1364 sni = req->rq_rqbd->rqbd_srv_ni;
1365 svc = sni->sni_service;
1367 rs = req->rq_reply_state;
1368 if (rs == NULL || !rs->rs_difficult) {
1369 /* The easy case; no notifiers and reply_out_callback()
1370 * cleans up (i.e. we can't look inside rs after a
1371 * successful send) */
1372 netrc = target_send_reply_msg (req, rc, fail_id);
1374 LASSERT (netrc == 0 || req->rq_reply_state == NULL);
1378 /* must be an export if locks saved */
1379 LASSERT (req->rq_export != NULL);
1380 /* req/reply consistent */
1381 LASSERT (rs->rs_srv_ni == sni);
1384 LASSERT (!rs->rs_scheduled);
1385 LASSERT (!rs->rs_scheduled_ever);
1386 LASSERT (!rs->rs_handled);
1387 LASSERT (!rs->rs_on_net);
1388 LASSERT (rs->rs_export == NULL);
1389 LASSERT (list_empty(&rs->rs_obd_list));
1390 LASSERT (list_empty(&rs->rs_exp_list));
1392 exp = class_export_get (req->rq_export);
1395 /* disable reply scheduling onto srv_reply_queue while I'm setting up */
1396 rs->rs_scheduled = 1;
1398 rs->rs_xid = req->rq_xid;
1399 rs->rs_transno = req->rq_transno;
1400 rs->rs_export = exp;
1402 spin_lock_irqsave (&obd->obd_uncommitted_replies_lock, flags);
1404 if (rs->rs_transno > obd->obd_last_committed) {
1405 /* not committed already */
1406 list_add_tail (&rs->rs_obd_list,
1407 &obd->obd_uncommitted_replies);
1410 spin_unlock (&obd->obd_uncommitted_replies_lock);
1411 spin_lock (&exp->exp_lock);
1413 list_add_tail (&rs->rs_exp_list, &exp->exp_outstanding_replies);
1415 spin_unlock_irqrestore (&exp->exp_lock, flags);
1417 netrc = target_send_reply_msg (req, rc, fail_id);
1419 spin_lock_irqsave (&svc->srv_lock, flags);
1421 svc->srv_n_difficult_replies++;
1423 if (netrc != 0) /* error sending: reply is off the net */
1426 if (!rs->rs_on_net || /* some notifier */
1427 list_empty(&rs->rs_exp_list) || /* completed already */
1428 list_empty(&rs->rs_obd_list)) {
1429 list_add_tail (&rs->rs_list, &svc->srv_reply_queue);
1430 wake_up (&svc->srv_waitq);
1432 list_add (&rs->rs_list, &sni->sni_active_replies);
1433 rs->rs_scheduled = 0; /* allow notifier to schedule */
1436 spin_unlock_irqrestore (&svc->srv_lock, flags);
1439 int target_handle_ping(struct ptlrpc_request *req)
1441 return lustre_pack_reply(req, 0, NULL, NULL);