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 /* @priority: if non-zero, move the selected to the list head
39 * @nocreate: if non-zero, only search in existed connections
41 static int import_set_conn(struct obd_import *imp, struct obd_uuid *uuid,
42 int priority, int nocreate)
44 struct ptlrpc_connection *ptlrpc_conn;
45 struct obd_import_conn *imp_conn = NULL, *item;
49 LASSERT(!(nocreate && !priority));
51 ptlrpc_conn = ptlrpc_uuid_to_connection(uuid);
53 CERROR("can't find connection %s\n", uuid->uuid);
58 OBD_ALLOC(imp_conn, sizeof(*imp_conn));
60 CERROR("fail to alloc memory\n");
61 GOTO(out_put, rc = -ENOMEM);
65 spin_lock(&imp->imp_lock);
66 list_for_each_entry(item, &imp->imp_conn_list, oic_item) {
67 if (obd_uuid_equals(uuid, &item->oic_uuid)) {
69 list_del(&item->oic_item);
70 list_add(&item->oic_item, &imp->imp_conn_list);
71 item->oic_last_attempt = 0;
73 CDEBUG(D_HA, "imp %p@%s: find existed conn %s%s\n",
74 imp, imp->imp_obd->obd_name, uuid->uuid,
75 (priority ? ", move to head." : ""));
76 spin_unlock(&imp->imp_lock);
77 GOTO(out_free, rc = 0);
82 imp_conn->oic_conn = ptlrpc_conn;
83 imp_conn->oic_uuid = *uuid;
84 imp_conn->oic_last_attempt = 0;
86 list_add(&imp_conn->oic_item, &imp->imp_conn_list);
88 list_add_tail(&imp_conn->oic_item, &imp->imp_conn_list);
89 CDEBUG(D_HA, "imp %p@%s: add connection %s at %s\n",
90 imp, imp->imp_obd->obd_name, uuid->uuid,
91 (priority ? "head" : "tail"));
95 spin_unlock(&imp->imp_lock);
99 OBD_FREE(imp_conn, sizeof(*imp_conn));
101 ptlrpc_put_connection(ptlrpc_conn);
105 int import_set_conn_priority(struct obd_import *imp, struct obd_uuid *uuid)
107 return import_set_conn(imp, uuid, 1, 1);
110 int client_import_add_conn(struct obd_import *imp, struct obd_uuid *uuid,
113 return import_set_conn(imp, uuid, priority, 0);
116 int client_import_del_conn(struct obd_import *imp, struct obd_uuid *uuid)
118 struct obd_import_conn *imp_conn;
119 struct obd_export *dlmexp;
123 spin_lock(&imp->imp_lock);
124 if (list_empty(&imp->imp_conn_list)) {
125 LASSERT(!imp->imp_conn_current);
126 LASSERT(!imp->imp_connection);
130 list_for_each_entry(imp_conn, &imp->imp_conn_list, oic_item) {
131 if (!obd_uuid_equals(uuid, &imp_conn->oic_uuid))
133 LASSERT(imp_conn->oic_conn);
135 /* is current conn? */
136 if (imp_conn == imp->imp_conn_current) {
137 LASSERT(imp_conn->oic_conn == imp->imp_connection);
139 if (imp->imp_state != LUSTRE_IMP_CLOSED &&
140 imp->imp_state != LUSTRE_IMP_DISCON) {
141 CERROR("can't remove current connection\n");
142 GOTO(out, rc = -EBUSY);
145 ptlrpc_put_connection(imp->imp_connection);
146 imp->imp_connection = NULL;
148 dlmexp = class_conn2export(&imp->imp_dlm_handle);
149 if (dlmexp && dlmexp->exp_connection) {
150 LASSERT(dlmexp->exp_connection ==
152 ptlrpc_put_connection(dlmexp->exp_connection);
153 dlmexp->exp_connection = NULL;
157 list_del(&imp_conn->oic_item);
158 ptlrpc_put_connection(imp_conn->oic_conn);
159 OBD_FREE(imp_conn, sizeof(*imp_conn));
160 CDEBUG(D_HA, "imp %p@%s: remove connection %s\n",
161 imp, imp->imp_obd->obd_name, uuid->uuid);
166 spin_unlock(&imp->imp_lock);
168 CERROR("connection %s not found\n", uuid->uuid);
172 int client_obd_setup(struct obd_device *obddev, obd_count len, void *buf)
174 struct lustre_cfg* lcfg = buf;
175 struct client_obd *cli = &obddev->u.cli;
176 struct obd_import *imp;
177 struct obd_uuid server_uuid;
178 int rq_portal, rp_portal, connect_op;
179 char *name = obddev->obd_type->typ_name;
180 char *mgmt_name = NULL;
182 struct obd_device *mgmt_obd;
183 mgmtcli_register_for_events_t register_f;
186 /* In a more perfect world, we would hang a ptlrpc_client off of
187 * obd_type and just use the values from there. */
188 if (!strcmp(name, LUSTRE_OSC_NAME)) {
189 rq_portal = OST_REQUEST_PORTAL;
190 rp_portal = OSC_REPLY_PORTAL;
191 connect_op = OST_CONNECT;
192 } else if (!strcmp(name, LUSTRE_MDC_NAME)) {
193 rq_portal = MDS_REQUEST_PORTAL;
194 rp_portal = MDC_REPLY_PORTAL;
195 connect_op = MDS_CONNECT;
196 } else if (!strcmp(name, LUSTRE_MGMTCLI_NAME)) {
197 rq_portal = MGMT_REQUEST_PORTAL;
198 rp_portal = MGMT_REPLY_PORTAL;
199 connect_op = MGMT_CONNECT;
201 CERROR("unknown client OBD type \"%s\", can't setup\n",
206 if (lcfg->lcfg_inllen1 < 1) {
207 CERROR("requires a TARGET UUID\n");
211 if (lcfg->lcfg_inllen1 > 37) {
212 CERROR("client UUID must be less than 38 characters\n");
216 if (lcfg->lcfg_inllen2 < 1) {
217 CERROR("setup requires a SERVER UUID\n");
221 if (lcfg->lcfg_inllen2 > 37) {
222 CERROR("target UUID must be less than 38 characters\n");
226 sema_init(&cli->cl_sem, 1);
227 cli->cl_conn_count = 0;
228 memcpy(server_uuid.uuid, lcfg->lcfg_inlbuf2,
229 min_t(unsigned int, lcfg->lcfg_inllen2, sizeof(server_uuid)));
232 cli->cl_avail_grant = 0;
233 /* FIXME: should limit this for the sum of all cl_dirty_max */
234 cli->cl_dirty_max = OSC_MAX_DIRTY_DEFAULT * 1024 * 1024;
235 if (cli->cl_dirty_max >> PAGE_SHIFT > num_physpages / 8)
236 cli->cl_dirty_max = num_physpages << (PAGE_SHIFT - 3);
237 INIT_LIST_HEAD(&cli->cl_cache_waiters);
238 INIT_LIST_HEAD(&cli->cl_loi_ready_list);
239 INIT_LIST_HEAD(&cli->cl_loi_write_list);
240 INIT_LIST_HEAD(&cli->cl_loi_read_list);
241 spin_lock_init(&cli->cl_loi_list_lock);
242 cli->cl_r_in_flight = 0;
243 cli->cl_w_in_flight = 0;
244 spin_lock_init(&cli->cl_read_rpc_hist.oh_lock);
245 spin_lock_init(&cli->cl_write_rpc_hist.oh_lock);
246 spin_lock_init(&cli->cl_read_page_hist.oh_lock);
247 spin_lock_init(&cli->cl_write_page_hist.oh_lock);
249 if (num_physpages >> (20 - PAGE_SHIFT) <= 128) { /* <= 128 MB */
250 cli->cl_max_pages_per_rpc = PTLRPC_MAX_BRW_PAGES / 4;
251 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT / 4;
252 } else if (num_physpages >> (20 - PAGE_SHIFT) <= 512) { /* <= 512 MB */
253 cli->cl_max_pages_per_rpc = PTLRPC_MAX_BRW_PAGES / 2;
254 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT / 2;
256 cli->cl_max_pages_per_rpc = PTLRPC_MAX_BRW_PAGES;
257 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT;
263 CERROR("ldlm_get_ref failed: %d\n", rc);
267 ptlrpc_init_client(rq_portal, rp_portal, name,
268 &obddev->obd_ldlm_client);
270 imp = class_new_import();
272 GOTO(err_ldlm, rc = -ENOENT);
273 imp->imp_client = &obddev->obd_ldlm_client;
274 imp->imp_obd = obddev;
275 imp->imp_connect_op = connect_op;
276 imp->imp_generation = 0;
277 imp->imp_initial_recov = 1;
278 INIT_LIST_HEAD(&imp->imp_pinger_chain);
279 memcpy(imp->imp_target_uuid.uuid, lcfg->lcfg_inlbuf1,
281 class_import_put(imp);
283 rc = client_import_add_conn(imp, &server_uuid, 1);
285 CERROR("can't add initial connection\n");
286 GOTO(err_import, rc);
289 cli->cl_import = imp;
290 cli->cl_max_mds_easize = sizeof(struct lov_mds_md);
291 cli->cl_max_mds_cookiesize = sizeof(struct llog_cookie);
292 cli->cl_sandev = to_kdev_t(0);
294 if (lcfg->lcfg_inllen3 != 0) {
295 if (!strcmp(lcfg->lcfg_inlbuf3, "inactive")) {
296 CDEBUG(D_HA, "marking %s %s->%s as inactive\n",
297 name, obddev->obd_name,
298 imp->imp_target_uuid.uuid);
299 imp->imp_invalid = 1;
301 if (lcfg->lcfg_inllen4 != 0)
302 mgmt_name = lcfg->lcfg_inlbuf4;
304 mgmt_name = lcfg->lcfg_inlbuf3;
308 if (mgmt_name != NULL) {
309 /* Register with management client if we need to. */
310 CDEBUG(D_HA, "%s registering with %s for events about %s\n",
311 obddev->obd_name, mgmt_name, server_uuid.uuid);
313 mgmt_obd = class_name2obd(mgmt_name);
315 CERROR("can't find mgmtcli %s to register\n",
317 GOTO(err_import, rc = -ENOSYS);
320 register_f = inter_module_get("mgmtcli_register_for_events");
322 CERROR("can't i_m_g mgmtcli_register_for_events\n");
323 GOTO(err_import, rc = -ENOSYS);
326 rc = register_f(mgmt_obd, obddev, &imp->imp_target_uuid);
327 inter_module_put("mgmtcli_register_for_events");
330 cli->cl_mgmtcli_obd = mgmt_obd;
336 class_destroy_import(imp);
344 int client_obd_cleanup(struct obd_device *obddev, int flags)
346 struct client_obd *cli = &obddev->u.cli;
350 if (cli->cl_mgmtcli_obd) {
351 mgmtcli_deregister_for_events_t dereg_f;
353 dereg_f = inter_module_get("mgmtcli_deregister_for_events");
354 dereg_f(cli->cl_mgmtcli_obd, obddev);
355 inter_module_put("mgmtcli_deregister_for_events");
357 class_destroy_import(cli->cl_import);
358 cli->cl_import = NULL;
360 ldlm_put_ref(flags & OBD_OPT_FORCE);
365 int client_connect_import(struct lustre_handle *dlm_handle,
366 struct obd_device *obd,
367 struct obd_uuid *cluuid,
368 unsigned long connect_flags)
370 struct client_obd *cli = &obd->u.cli;
371 struct obd_import *imp = cli->cl_import;
372 struct obd_export *exp;
377 rc = class_connect(dlm_handle, obd, cluuid);
381 cli->cl_conn_count++;
382 if (cli->cl_conn_count > 1)
384 exp = class_conn2export(dlm_handle);
386 if (obd->obd_namespace != NULL)
387 CERROR("already have namespace!\n");
388 obd->obd_namespace = ldlm_namespace_new(obd->obd_name,
389 LDLM_NAMESPACE_CLIENT);
390 if (obd->obd_namespace == NULL)
391 GOTO(out_disco, rc = -ENOMEM);
393 imp->imp_dlm_handle = *dlm_handle;
394 rc = ptlrpc_init_import(imp);
398 imp->imp_connect_flags = connect_flags;
399 rc = ptlrpc_connect_import(imp, NULL);
401 LASSERT (imp->imp_state == LUSTRE_IMP_DISCON);
404 LASSERT(exp->exp_connection);
405 ptlrpc_pinger_add_import(imp);
410 ldlm_namespace_free(obd->obd_namespace, 0);
411 obd->obd_namespace = NULL;
413 cli->cl_conn_count--;
414 class_disconnect(exp, 0);
416 class_export_put(exp);
423 int client_disconnect_export(struct obd_export *exp, unsigned long flags)
425 struct obd_device *obd = class_exp2obd(exp);
426 struct client_obd *cli = &obd->u.cli;
427 struct obd_import *imp = cli->cl_import;
432 CERROR("invalid export for disconnect: exp %p cookie "LPX64"\n",
433 exp, exp ? exp->exp_handle.h_cookie : -1);
438 if (!cli->cl_conn_count) {
439 CERROR("disconnecting disconnected device (%s)\n",
441 GOTO(out_sem, rc = -EINVAL);
444 cli->cl_conn_count--;
445 if (cli->cl_conn_count)
446 GOTO(out_no_disconnect, rc = 0);
448 /* Some non-replayable imports (MDS's OSCs) are pinged, so just
449 * delete it regardless. (It's safe to delete an import that was
451 (void)ptlrpc_pinger_del_import(imp);
453 if (obd->obd_namespace != NULL) {
454 /* obd_no_recov == local only */
455 ldlm_cli_cancel_unused(obd->obd_namespace, NULL,
456 obd->obd_no_recov, NULL);
457 ldlm_namespace_free(obd->obd_namespace, obd->obd_no_recov);
458 obd->obd_namespace = NULL;
462 * Yeah, obd_no_recov also (mainly) means "forced shutdown".
464 if (obd->obd_no_recov)
465 ptlrpc_invalidate_import(imp, 0);
467 rc = ptlrpc_disconnect_import(imp);
471 err = class_disconnect(exp, 0);
479 /* --------------------------------------------------------------------------
480 * from old lib/target.c
481 * -------------------------------------------------------------------------- */
483 int target_handle_reconnect(struct lustre_handle *conn, struct obd_export *exp,
484 struct obd_uuid *cluuid, int initial_conn)
486 if (exp->exp_connection && !initial_conn) {
487 struct lustre_handle *hdl;
488 hdl = &exp->exp_imp_reverse->imp_remote_handle;
489 /* Might be a re-connect after a partition. */
490 if (!memcmp(&conn->cookie, &hdl->cookie, sizeof conn->cookie)) {
491 CERROR("%s reconnecting\n", cluuid->uuid);
492 conn->cookie = exp->exp_handle.h_cookie;
495 CERROR("%s reconnecting from %s, "
496 "handle mismatch (ours "LPX64", theirs "
497 LPX64")\n", cluuid->uuid,
498 exp->exp_connection->c_remote_uuid.uuid,
499 hdl->cookie, conn->cookie);
500 memset(conn, 0, sizeof *conn);
505 conn->cookie = exp->exp_handle.h_cookie;
506 CDEBUG(D_INFO, "existing export for UUID '%s' at %p\n",
508 CDEBUG(D_IOCTL,"connect: cookie "LPX64"\n", conn->cookie);
512 static char nidstr[PTL_NALFMT_SIZE];
513 int target_handle_connect(struct ptlrpc_request *req)
515 unsigned long connect_flags = 0, *cfp;
516 struct obd_device *target;
517 struct obd_export *export = NULL;
518 struct obd_import *revimp;
519 struct lustre_handle conn;
520 struct obd_uuid tgtuuid;
521 struct obd_uuid cluuid;
522 struct obd_uuid remote_uuid;
527 int initial_conn = 0;
528 char peer_str[PTL_NALFMT_SIZE];
531 OBD_RACE(OBD_FAIL_TGT_CONN_RACE);
533 LASSERT_REQSWAB (req, 0);
534 str = lustre_msg_string(req->rq_reqmsg, 0, sizeof(tgtuuid) - 1);
536 CERROR("bad target UUID for connect\n");
537 GOTO(out, rc = -EINVAL);
540 obd_str2uuid (&tgtuuid, str);
541 target = class_uuid2obd(&tgtuuid);
543 target = class_name2obd(str);
546 if (!target || target->obd_stopping || !target->obd_set_up) {
547 CERROR("UUID '%s' is not available for connect from NID %s\n",
548 str, ptlrpc_peernid2str(&req->rq_peer, nidstr));
549 GOTO(out, rc = -ENODEV);
552 LASSERT_REQSWAB (req, 1);
553 str = lustre_msg_string(req->rq_reqmsg, 1, sizeof(cluuid) - 1);
555 CERROR("bad client UUID for connect\n");
556 GOTO(out, rc = -EINVAL);
559 obd_str2uuid (&cluuid, str);
561 /* XXX extract a nettype and format accordingly */
562 switch (sizeof(ptl_nid_t)) {
563 /* NB the casts only avoid compiler warnings */
565 snprintf(remote_uuid.uuid, sizeof remote_uuid,
566 "NET_"LPX64"_UUID", (__u64)req->rq_peer.peer_id.nid);
569 snprintf(remote_uuid.uuid, sizeof remote_uuid,
570 "NET_%x_UUID", (__u32)req->rq_peer.peer_id.nid);
576 tmp = lustre_msg_buf(req->rq_reqmsg, 2, sizeof conn);
578 GOTO(out, rc = -EPROTO);
580 memcpy(&conn, tmp, sizeof conn);
582 cfp = lustre_msg_buf(req->rq_reqmsg, 3, sizeof(unsigned long));
583 LASSERT(cfp != NULL);
584 connect_flags = *cfp;
586 rc = lustre_pack_reply(req, 0, NULL, NULL);
590 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_INITIAL)
593 /* lctl gets a backstage, all-access pass. */
594 if (obd_uuid_equals(&cluuid, &target->obd_uuid))
595 goto dont_check_exports;
597 spin_lock(&target->obd_dev_lock);
598 list_for_each(p, &target->obd_exports) {
599 export = list_entry(p, struct obd_export, exp_obd_chain);
600 if (obd_uuid_equals(&cluuid, &export->exp_client_uuid)) {
601 spin_unlock(&target->obd_dev_lock);
602 LASSERT(export->exp_obd == target);
604 rc = target_handle_reconnect(&conn, export, &cluuid,
610 /* If we found an export, we already unlocked. */
612 spin_unlock(&target->obd_dev_lock);
613 } else if (req->rq_export == NULL &&
614 atomic_read(&export->exp_rpc_count) > 0) {
615 CWARN("%s: refuse connection from %s/%s to 0x%p/%d\n",
616 target->obd_name, cluuid.uuid,
617 ptlrpc_peernid2str(&req->rq_peer, peer_str),
618 export, atomic_read(&export->exp_refcount));
619 GOTO(out, rc = -EBUSY);
620 } else if (req->rq_export != NULL &&
621 atomic_read(&export->exp_rpc_count) > 1) {
622 CWARN("%s: refuse reconnection from %s@%s to 0x%p/%d\n",
623 target->obd_name, cluuid.uuid,
624 ptlrpc_peernid2str(&req->rq_peer, peer_str),
625 export, atomic_read(&export->exp_rpc_count));
626 GOTO(out, rc = -EBUSY);
628 else if (req->rq_reqmsg->conn_cnt == 1 && !initial_conn) {
629 CERROR("%s reconnected with 1 conn_cnt; cookies not random?\n",
631 GOTO(out, rc = -EALREADY);
634 /* Tell the client if we're in recovery. */
635 /* If this is the first client, start the recovery timer */
636 CWARN("%s: connection from %s@%s/%lu %s\n", target->obd_name, cluuid.uuid,
637 ptlrpc_peernid2str(&req->rq_peer, peer_str), *cfp,
638 target->obd_recovering ? "(recovering)" : "");
639 if (target->obd_recovering) {
640 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
641 target_start_recovery_timer(target);
644 /* Tell the client if we support replayable requests */
645 if (target->obd_replayable)
646 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
648 if (export == NULL) {
649 if (target->obd_recovering) {
650 CERROR("%s denying connection for new client %s@%s: "
651 "%d clients in recovery for %lds\n", target->obd_name,
653 ptlrpc_peernid2str(&req->rq_peer, peer_str),
654 target->obd_recoverable_clients,
655 (target->obd_recovery_timer.expires-jiffies)/HZ);
659 rc = obd_connect(&conn, target, &cluuid, connect_flags);
662 /* Tell the client if we support replayable requests */
663 if (target->obd_replayable)
664 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
666 /* If all else goes well, this is our RPC return code. */
669 if (rc && rc != EALREADY)
672 req->rq_repmsg->handle = conn;
674 /* If the client and the server are the same node, we will already
675 * have an export that really points to the client's DLM export,
676 * because we have a shared handles table.
678 * XXX this will go away when shaver stops sending the "connect" handle
679 * in the real "remote handle" field of the request --phik 24 Apr 2003
681 if (req->rq_export != NULL)
682 class_export_put(req->rq_export);
684 /* ownership of this export ref transfers to the request */
685 export = req->rq_export = class_conn2export(&conn);
686 LASSERT(export != NULL);
688 spin_lock_irqsave(&export->exp_lock, flags);
690 req->rq_repmsg->conn_cnt = export->exp_conn_cnt + 1;
691 } else if (export->exp_conn_cnt >= req->rq_reqmsg->conn_cnt) {
692 CERROR("%s@%s: already connected at a higher conn_cnt: %d > %d\n",
693 cluuid.uuid, ptlrpc_peernid2str(&req->rq_peer, peer_str),
694 export->exp_conn_cnt,
695 req->rq_reqmsg->conn_cnt);
696 spin_unlock_irqrestore(&export->exp_lock, flags);
697 GOTO(out, rc = -EALREADY);
699 export->exp_conn_cnt = req->rq_reqmsg->conn_cnt;
700 spin_unlock_irqrestore(&export->exp_lock, flags);
702 /* request from liblustre? */
703 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT)
704 export->exp_libclient = 1;
706 if (export->exp_connection != NULL)
707 ptlrpc_put_connection(export->exp_connection);
708 export->exp_connection = ptlrpc_get_connection(&req->rq_peer,
711 if (rc == EALREADY) {
712 /* We indicate the reconnection in a flag, not an error code. */
713 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECONNECT);
717 if (target->obd_recovering) {
718 target->obd_connected_clients++;
721 memcpy(&conn, lustre_msg_buf(req->rq_reqmsg, 2, sizeof conn),
724 if (export->exp_imp_reverse != NULL)
725 class_destroy_import(export->exp_imp_reverse);
726 revimp = export->exp_imp_reverse = class_new_import();
727 revimp->imp_connection = ptlrpc_connection_addref(export->exp_connection);
728 revimp->imp_client = &export->exp_obd->obd_ldlm_client;
729 revimp->imp_remote_handle = conn;
730 revimp->imp_obd = target;
731 revimp->imp_dlm_fake = 1;
732 revimp->imp_state = LUSTRE_IMP_FULL;
733 class_import_put(revimp);
735 rc = obd_connect_post(export, connect_flags);
742 int target_handle_disconnect(struct ptlrpc_request *req)
744 struct obd_export *exp;
748 rc = lustre_pack_reply(req, 0, NULL, NULL);
752 /* keep the rq_export around so we can send the reply */
753 exp = class_export_get(req->rq_export);
754 req->rq_status = obd_disconnect(exp, 0);
758 void target_destroy_export(struct obd_export *exp)
760 /* exports created from last_rcvd data, and "fake"
761 exports created by lctl don't have an import */
762 if (exp->exp_imp_reverse != NULL)
763 class_destroy_import(exp->exp_imp_reverse);
765 /* We cancel locks at disconnect time, but this will catch any locks
766 * granted in a race with recovery-induced disconnect. */
767 if (exp->exp_obd->obd_namespace != NULL)
768 ldlm_cancel_locks_for_export(exp);
775 struct ptlrpc_request *
776 ptlrpc_clone_req( struct ptlrpc_request *orig_req)
778 struct ptlrpc_request *copy_req;
779 struct lustre_msg *copy_reqmsg;
781 OBD_ALLOC(copy_req, sizeof *copy_req);
784 OBD_ALLOC(copy_reqmsg, orig_req->rq_reqlen);
786 OBD_FREE(copy_req, sizeof *copy_req);
790 memcpy(copy_req, orig_req, sizeof *copy_req);
791 memcpy(copy_reqmsg, orig_req->rq_reqmsg, orig_req->rq_reqlen);
792 /* the copied req takes over the reply state */
793 orig_req->rq_reply_state = NULL;
795 copy_req->rq_reqmsg = copy_reqmsg;
796 class_export_get(copy_req->rq_export);
797 INIT_LIST_HEAD(©_req->rq_list);
801 void ptlrpc_free_clone( struct ptlrpc_request *req)
803 class_export_put(req->rq_export);
804 list_del(&req->rq_list);
805 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
806 OBD_FREE(req, sizeof *req);
811 static void target_release_saved_req(struct ptlrpc_request *req)
813 class_export_put(req->rq_export);
814 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
815 OBD_FREE(req, sizeof *req);
819 static void target_finish_recovery(struct obd_device *obd)
821 struct list_head *tmp, *n;
824 CWARN("%s: sending delayed replies to recovered clients\n",
827 ldlm_reprocess_all_ns(obd->obd_namespace);
829 /* when recovery finished, cleanup orphans on mds and ost */
830 if (OBT(obd) && OBP(obd, postrecov)) {
831 rc = OBP(obd, postrecov)(obd);
833 CWARN("%s: all clients recovered, %d MDS "
834 "orphans deleted\n", obd->obd_name, rc);
836 CERROR("postrecov failed %d\n", rc);
840 list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
841 struct ptlrpc_request *req;
842 req = list_entry(tmp, struct ptlrpc_request, rq_list);
843 list_del(&req->rq_list);
844 DEBUG_REQ(D_ERROR, req, "delayed:");
846 target_release_saved_req(req);
848 obd->obd_recovery_end = LTIME_S(CURRENT_TIME);
852 static void abort_recovery_queue(struct obd_device *obd)
854 struct ptlrpc_request *req;
855 struct list_head *tmp, *n;
858 list_for_each_safe(tmp, n, &obd->obd_recovery_queue) {
859 req = list_entry(tmp, struct ptlrpc_request, rq_list);
860 list_del(&req->rq_list);
861 DEBUG_REQ(D_ERROR, req, "aborted:");
862 req->rq_status = -ENOTCONN;
863 req->rq_type = PTL_RPC_MSG_ERR;
864 rc = lustre_pack_reply(req, 0, NULL, NULL);
868 DEBUG_REQ(D_ERROR, req,
869 "packing failed for abort-reply; skipping");
871 target_release_saved_req(req);
876 /* Called from a cleanup function if the device is being cleaned up
877 forcefully. The exports should all have been disconnected already,
878 the only thing left to do is
879 - clear the recovery flags
881 - free queued requests and replies, but don't send replies
882 Because the obd_stopping flag is set, no new requests should be received.
885 void target_cleanup_recovery(struct obd_device *obd)
887 struct list_head *tmp, *n;
888 struct ptlrpc_request *req;
890 spin_lock_bh(&obd->obd_processing_task_lock);
891 if (!obd->obd_recovering) {
892 spin_unlock_bh(&obd->obd_processing_task_lock);
896 obd->obd_recovering = obd->obd_abort_recovery = 0;
897 target_cancel_recovery_timer(obd);
898 spin_unlock_bh(&obd->obd_processing_task_lock);
900 list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
901 req = list_entry(tmp, struct ptlrpc_request, rq_list);
902 list_del(&req->rq_list);
903 LASSERT (req->rq_reply_state);
904 lustre_free_reply_state(req->rq_reply_state);
905 target_release_saved_req(req);
907 list_for_each_safe(tmp, n, &obd->obd_recovery_queue) {
908 req = list_entry(tmp, struct ptlrpc_request, rq_list);
909 list_del(&req->rq_list);
910 LASSERT (req->rq_reply_state == 0);
911 target_release_saved_req(req);
916 static void target_abort_recovery(void *data)
918 struct obd_device *obd = data;
920 LASSERT(!obd->obd_recovering);
922 class_disconnect_stale_exports(obd, 0);
924 CERROR("%s: recovery period over; disconnecting unfinished clients.\n",
927 abort_recovery_queue(obd);
928 target_finish_recovery(obd);
929 ptlrpc_run_recovery_over_upcall(obd);
933 static void target_recovery_expired(unsigned long castmeharder)
935 struct obd_device *obd = (struct obd_device *)castmeharder;
936 CERROR("recovery timed out, aborting\n");
937 spin_lock_bh(&obd->obd_processing_task_lock);
938 if (obd->obd_recovering)
939 obd->obd_abort_recovery = 1;
941 wake_up(&obd->obd_next_transno_waitq);
942 spin_unlock_bh(&obd->obd_processing_task_lock);
946 /* obd_processing_task_lock should be held */
947 void target_cancel_recovery_timer(struct obd_device *obd)
949 CDEBUG(D_HA, "%s: cancel recovery timer\n", obd->obd_name);
950 del_timer(&obd->obd_recovery_timer);
954 static void reset_recovery_timer(struct obd_device *obd)
956 spin_lock_bh(&obd->obd_processing_task_lock);
957 if (!obd->obd_recovering) {
958 spin_unlock_bh(&obd->obd_processing_task_lock);
961 CDEBUG(D_HA, "timer will expire in %u seconds\n",
962 OBD_RECOVERY_TIMEOUT / HZ);
963 mod_timer(&obd->obd_recovery_timer, jiffies + OBD_RECOVERY_TIMEOUT);
964 spin_unlock_bh(&obd->obd_processing_task_lock);
968 /* Only start it the first time called */
969 void target_start_recovery_timer(struct obd_device *obd)
971 spin_lock_bh(&obd->obd_processing_task_lock);
972 if (!obd->obd_recovering || timer_pending(&obd->obd_recovery_timer)) {
973 spin_unlock_bh(&obd->obd_processing_task_lock);
976 CWARN("%s: starting recovery timer (%us)\n", obd->obd_name,
977 OBD_RECOVERY_TIMEOUT / HZ);
978 obd->obd_recovery_timer.function = target_recovery_expired;
979 obd->obd_recovery_timer.data = (unsigned long)obd;
980 mod_timer(&obd->obd_recovery_timer, jiffies + OBD_RECOVERY_TIMEOUT);
981 spin_unlock_bh(&obd->obd_processing_task_lock);
985 static int check_for_next_transno(struct obd_device *obd)
987 struct ptlrpc_request *req = NULL;
988 int wake_up = 0, connected, completed, queue_len, max;
989 __u64 next_transno, req_transno;
991 spin_lock_bh(&obd->obd_processing_task_lock);
992 if (!list_empty(&obd->obd_recovery_queue)) {
993 req = list_entry(obd->obd_recovery_queue.next,
994 struct ptlrpc_request, rq_list);
995 req_transno = req->rq_reqmsg->transno;
1000 max = obd->obd_max_recoverable_clients;
1001 connected = obd->obd_connected_clients;
1002 completed = max - obd->obd_recoverable_clients;
1003 queue_len = obd->obd_requests_queued_for_recovery;
1004 next_transno = obd->obd_next_recovery_transno;
1006 CDEBUG(D_HA,"max: %d, connected: %d, completed: %d, queue_len: %d, "
1007 "req_transno: "LPU64", next_transno: "LPU64"\n",
1008 max, connected, completed, queue_len, req_transno, next_transno);
1009 if (obd->obd_abort_recovery) {
1010 CDEBUG(D_HA, "waking for aborted recovery\n");
1012 } else if (max == completed) {
1013 CDEBUG(D_HA, "waking for completed recovery\n");
1015 } else if (req_transno == next_transno) {
1016 CDEBUG(D_HA, "waking for next ("LPD64")\n", next_transno);
1018 } else if (queue_len + completed == max) {
1019 LASSERT(req->rq_reqmsg->transno >= next_transno);
1021 "waking for skipped transno (skip: "LPD64
1022 ", ql: %d, comp: %d, conn: %d, next: "LPD64")\n",
1023 next_transno, queue_len, completed, max, req_transno);
1024 obd->obd_next_recovery_transno = req_transno;
1027 spin_unlock_bh(&obd->obd_processing_task_lock);
1032 static struct ptlrpc_request *
1033 target_next_replay_req(struct obd_device *obd)
1035 struct l_wait_info lwi = { 0 };
1036 struct ptlrpc_request *req;
1038 CDEBUG(D_HA, "Waiting for transno "LPD64"\n",
1039 obd->obd_next_recovery_transno);
1040 l_wait_event(obd->obd_next_transno_waitq,
1041 check_for_next_transno(obd), &lwi);
1043 spin_lock_bh(&obd->obd_processing_task_lock);
1044 if (obd->obd_abort_recovery) {
1046 } else if (!list_empty(&obd->obd_recovery_queue)) {
1047 req = list_entry(obd->obd_recovery_queue.next,
1048 struct ptlrpc_request, rq_list);
1049 list_del_init(&req->rq_list);
1050 obd->obd_requests_queued_for_recovery--;
1054 spin_unlock_bh(&obd->obd_processing_task_lock);
1058 static int target_recovery_thread(void *arg)
1060 struct obd_device *obd = arg;
1061 struct ptlrpc_request *req;
1062 struct target_recovery_data *trd = &obd->obd_recovery_data;
1063 unsigned long flags;
1066 kportal_daemonize("tgt-recov");
1068 SIGNAL_MASK_LOCK(current, flags);
1069 sigfillset(¤t->blocked);
1071 SIGNAL_MASK_UNLOCK(current, flags);
1073 CERROR("%s: started recovery thread pid %d\n", obd->obd_name,
1075 trd->trd_processing_task = current->pid;
1077 obd->obd_recovering = 1;
1078 complete(&trd->trd_starting);
1080 while (obd->obd_recovering) {
1081 LASSERT(trd->trd_processing_task == current->pid);
1082 req = target_next_replay_req(obd);
1084 char peer_str[PTL_NALFMT_SIZE];
1085 DEBUG_REQ(D_HA, req, "processing t"LPD64" from %s: ",
1086 req->rq_reqmsg->transno,
1087 ptlrpc_peernid2str(&req->rq_peer, peer_str));
1088 (void)trd->trd_recovery_handler(req);
1089 obd->obd_replayed_requests++;
1090 reset_recovery_timer(obd);
1091 /* bug 1580: decide how to properly sync() in recovery*/
1092 //mds_fsync_super(mds->mds_sb);
1093 ptlrpc_free_clone(req);
1094 spin_lock_bh(&obd->obd_processing_task_lock);
1095 obd->obd_next_recovery_transno++;
1096 spin_unlock_bh(&obd->obd_processing_task_lock);
1098 /* recovery is over */
1099 spin_lock_bh(&obd->obd_processing_task_lock);
1100 obd->obd_recovering = 0;
1101 target_cancel_recovery_timer(obd);
1102 if (obd->obd_abort_recovery) {
1103 obd->obd_abort_recovery = 0;
1104 spin_unlock_bh(&obd->obd_processing_task_lock);
1105 target_abort_recovery(obd);
1107 LASSERT(obd->obd_recoverable_clients == 0);
1108 spin_unlock_bh(&obd->obd_processing_task_lock);
1109 target_finish_recovery(obd);
1114 trd->trd_processing_task = 0;
1115 complete(&trd->trd_finishing);
1119 int target_start_recovery_thread(struct obd_device *obd, svc_handler_t handler)
1122 struct target_recovery_data *trd = &obd->obd_recovery_data;
1124 memset(trd, 0, sizeof(*trd));
1125 init_completion(&trd->trd_starting);
1126 init_completion(&trd->trd_finishing);
1127 trd->trd_recovery_handler = handler;
1129 if (kernel_thread(target_recovery_thread, obd, 0) == 0)
1130 wait_for_completion(&trd->trd_starting);
1137 void target_stop_recovery_thread(struct obd_device *obd)
1139 spin_lock_bh(&obd->obd_processing_task_lock);
1140 if (obd->obd_recovery_data.trd_processing_task > 0) {
1141 struct target_recovery_data *trd = &obd->obd_recovery_data;
1142 CERROR("%s: aborting recovery\n", obd->obd_name);
1143 obd->obd_abort_recovery = 1;
1144 wake_up(&obd->obd_next_transno_waitq);
1145 spin_unlock_bh(&obd->obd_processing_task_lock);
1146 wait_for_completion(&trd->trd_finishing);
1148 spin_unlock_bh(&obd->obd_processing_task_lock);
1153 int target_queue_recovery_request(struct ptlrpc_request *req,
1154 struct obd_device *obd)
1156 struct list_head *tmp;
1158 __u64 transno = req->rq_reqmsg->transno;
1160 /* CAVEAT EMPTOR: The incoming request message has been swabbed
1161 * (i.e. buflens etc are in my own byte order), but type-dependent
1162 * buffers (eg mds_body, ost_body etc) have NOT been swabbed. */
1165 INIT_LIST_HEAD(&req->rq_list);
1166 DEBUG_REQ(D_HA, req, "not queueing");
1171 /* If we're processing the queue, we want don't want to queue this
1174 * Also, if this request has a transno less than the one we're waiting
1175 * for, we should process it now. It could (and currently always will)
1176 * be an open request for a descriptor that was opened some time ago.
1178 * Also, a resent, replayed request that has already been
1179 * handled will pass through here and be processed immediately.
1181 spin_lock_bh(&obd->obd_processing_task_lock);
1182 if (obd->obd_recovery_data.trd_processing_task == current->pid ||
1183 transno < obd->obd_next_recovery_transno) {
1184 /* Processing the queue right now, don't re-add. */
1185 LASSERT(list_empty(&req->rq_list));
1186 spin_unlock_bh(&obd->obd_processing_task_lock);
1189 spin_unlock_bh(&obd->obd_processing_task_lock);
1191 /* A resent, replayed request that is still on the queue; just drop it.
1192 The queued request will handle this. */
1193 if ((lustre_msg_get_flags(req->rq_reqmsg) & (MSG_RESENT | MSG_REPLAY))
1194 == (MSG_RESENT | MSG_REPLAY)) {
1195 DEBUG_REQ(D_ERROR, req, "dropping resent queued req");
1199 req = ptlrpc_clone_req(req);
1203 spin_lock_bh(&obd->obd_processing_task_lock);
1206 list_for_each(tmp, &obd->obd_recovery_queue) {
1207 struct ptlrpc_request *reqiter =
1208 list_entry(tmp, struct ptlrpc_request, rq_list);
1210 if (reqiter->rq_reqmsg->transno > transno) {
1211 list_add_tail(&req->rq_list, &reqiter->rq_list);
1218 list_add_tail(&req->rq_list, &obd->obd_recovery_queue);
1221 obd->obd_requests_queued_for_recovery++;
1222 wake_up(&obd->obd_next_transno_waitq);
1223 spin_unlock_bh(&obd->obd_processing_task_lock);
1228 struct obd_device * target_req2obd(struct ptlrpc_request *req)
1230 return req->rq_export->exp_obd;
1233 int target_queue_final_reply(struct ptlrpc_request *req, int rc)
1235 struct obd_device *obd = target_req2obd(req);
1237 LASSERT ((rc == 0) == (req->rq_reply_state != NULL));
1240 /* Just like ptlrpc_error, but without the sending. */
1241 rc = lustre_pack_reply(req, 0, NULL, NULL);
1242 LASSERT(rc == 0); /* XXX handle this */
1243 req->rq_type = PTL_RPC_MSG_ERR;
1246 LASSERT (!req->rq_reply_state->rs_difficult);
1247 LASSERT(list_empty(&req->rq_list));
1249 req = ptlrpc_clone_req(req);
1251 spin_lock_bh(&obd->obd_processing_task_lock);
1253 list_add(&req->rq_list, &obd->obd_delayed_reply_queue);
1255 /* only count the first "replay over" request from each
1257 if (req->rq_export->exp_replay_needed) {
1258 --obd->obd_recoverable_clients;
1259 req->rq_export->exp_replay_needed = 0;
1260 CWARN("%s: %d recoverable clients remain\n",
1261 obd->obd_name, obd->obd_recoverable_clients);
1263 wake_up(&obd->obd_next_transno_waitq);
1264 spin_unlock_bh(&obd->obd_processing_task_lock);
1269 target_send_reply_msg (struct ptlrpc_request *req, int rc, int fail_id)
1271 if (OBD_FAIL_CHECK(fail_id | OBD_FAIL_ONCE)) {
1272 obd_fail_loc |= OBD_FAIL_ONCE | OBD_FAILED;
1273 DEBUG_REQ(D_ERROR, req, "dropping reply");
1274 /* NB this does _not_ send with ACK disabled, to simulate
1275 * sending OK, but timing out for the ACK */
1276 if (req->rq_reply_state != NULL) {
1277 if (!req->rq_reply_state->rs_difficult) {
1278 lustre_free_reply_state (req->rq_reply_state);
1279 req->rq_reply_state = NULL;
1281 struct ptlrpc_service *svc =
1282 req->rq_rqbd->rqbd_srv_ni->sni_service;
1283 atomic_inc(&svc->srv_outstanding_replies);
1290 req->rq_status = rc;
1291 return (ptlrpc_error(req));
1293 DEBUG_REQ(D_NET, req, "sending reply");
1296 return (ptlrpc_send_reply(req, 1));
1300 target_send_reply(struct ptlrpc_request *req, int rc, int fail_id)
1303 unsigned long flags;
1304 struct ptlrpc_reply_state *rs;
1305 struct obd_device *obd;
1306 struct obd_export *exp;
1307 struct ptlrpc_srv_ni *sni;
1308 struct ptlrpc_service *svc;
1310 sni = req->rq_rqbd->rqbd_srv_ni;
1311 svc = sni->sni_service;
1313 rs = req->rq_reply_state;
1314 if (rs == NULL || !rs->rs_difficult) {
1315 /* The easy case; no notifiers and reply_out_callback()
1316 * cleans up (i.e. we can't look inside rs after a
1317 * successful send) */
1318 netrc = target_send_reply_msg (req, rc, fail_id);
1320 LASSERT (netrc == 0 || req->rq_reply_state == NULL);
1324 /* must be an export if locks saved */
1325 LASSERT (req->rq_export != NULL);
1326 /* req/reply consistent */
1327 LASSERT (rs->rs_srv_ni == sni);
1330 LASSERT (!rs->rs_scheduled);
1331 LASSERT (!rs->rs_scheduled_ever);
1332 LASSERT (!rs->rs_handled);
1333 LASSERT (!rs->rs_on_net);
1334 LASSERT (rs->rs_export == NULL);
1335 LASSERT (list_empty(&rs->rs_obd_list));
1336 LASSERT (list_empty(&rs->rs_exp_list));
1338 exp = class_export_get (req->rq_export);
1341 /* disable reply scheduling onto srv_reply_queue while I'm setting up */
1342 rs->rs_scheduled = 1;
1344 rs->rs_xid = req->rq_xid;
1345 rs->rs_transno = req->rq_transno;
1346 rs->rs_export = exp;
1348 spin_lock_irqsave (&obd->obd_uncommitted_replies_lock, flags);
1350 if (rs->rs_transno > obd->obd_last_committed) {
1351 /* not committed already */
1352 list_add_tail (&rs->rs_obd_list,
1353 &obd->obd_uncommitted_replies);
1356 spin_unlock (&obd->obd_uncommitted_replies_lock);
1357 spin_lock (&exp->exp_lock);
1359 list_add_tail (&rs->rs_exp_list, &exp->exp_outstanding_replies);
1361 spin_unlock_irqrestore (&exp->exp_lock, flags);
1363 netrc = target_send_reply_msg (req, rc, fail_id);
1365 spin_lock_irqsave (&svc->srv_lock, flags);
1367 svc->srv_n_difficult_replies++;
1369 if (netrc != 0) /* error sending: reply is off the net */
1372 if (!rs->rs_on_net || /* some notifier */
1373 list_empty(&rs->rs_exp_list) || /* completed already */
1374 list_empty(&rs->rs_obd_list)) {
1375 list_add_tail (&rs->rs_list, &svc->srv_reply_queue);
1376 wake_up (&svc->srv_waitq);
1378 list_add (&rs->rs_list, &sni->sni_active_replies);
1379 rs->rs_scheduled = 0; /* allow notifier to schedule */
1382 spin_unlock_irqrestore (&svc->srv_lock, flags);
1385 int target_handle_ping(struct ptlrpc_request *req)
1387 return lustre_pack_reply(req, 0, NULL, NULL);