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 <= 32768) { /* <= 128 MB */
250 cli->cl_max_pages_per_rpc = PTLRPC_MAX_BRW_PAGES / 3;
251 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT / 3;
253 cli->cl_max_pages_per_rpc = PTLRPC_MAX_BRW_PAGES;
254 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT;
260 CERROR("ldlm_get_ref failed: %d\n", rc);
264 ptlrpc_init_client(rq_portal, rp_portal, name,
265 &obddev->obd_ldlm_client);
267 imp = class_new_import();
269 GOTO(err_ldlm, rc = -ENOENT);
270 imp->imp_client = &obddev->obd_ldlm_client;
271 imp->imp_obd = obddev;
272 imp->imp_connect_op = connect_op;
273 imp->imp_generation = 0;
274 imp->imp_initial_recov = 1;
275 INIT_LIST_HEAD(&imp->imp_pinger_chain);
276 memcpy(imp->imp_target_uuid.uuid, lcfg->lcfg_inlbuf1,
278 class_import_put(imp);
280 rc = client_import_add_conn(imp, &server_uuid, 1);
282 CERROR("can't add initial connection\n");
283 GOTO(err_import, rc);
286 cli->cl_import = imp;
287 cli->cl_max_mds_easize = sizeof(struct lov_mds_md);
288 cli->cl_max_mds_cookiesize = sizeof(struct llog_cookie);
289 cli->cl_sandev = to_kdev_t(0);
291 if (lcfg->lcfg_inllen3 != 0) {
292 if (!strcmp(lcfg->lcfg_inlbuf3, "inactive")) {
293 CDEBUG(D_HA, "marking %s %s->%s as inactive\n",
294 name, obddev->obd_name,
295 imp->imp_target_uuid.uuid);
296 imp->imp_invalid = 1;
298 if (lcfg->lcfg_inllen4 != 0)
299 mgmt_name = lcfg->lcfg_inlbuf4;
301 mgmt_name = lcfg->lcfg_inlbuf3;
305 if (mgmt_name != NULL) {
306 /* Register with management client if we need to. */
307 CDEBUG(D_HA, "%s registering with %s for events about %s\n",
308 obddev->obd_name, mgmt_name, server_uuid.uuid);
310 mgmt_obd = class_name2obd(mgmt_name);
312 CERROR("can't find mgmtcli %s to register\n",
314 GOTO(err_import, rc = -ENOSYS);
317 register_f = inter_module_get("mgmtcli_register_for_events");
319 CERROR("can't i_m_g mgmtcli_register_for_events\n");
320 GOTO(err_import, rc = -ENOSYS);
323 rc = register_f(mgmt_obd, obddev, &imp->imp_target_uuid);
324 inter_module_put("mgmtcli_register_for_events");
327 cli->cl_mgmtcli_obd = mgmt_obd;
333 class_destroy_import(imp);
341 int client_obd_cleanup(struct obd_device *obddev, int flags)
343 struct client_obd *cli = &obddev->u.cli;
347 if (cli->cl_mgmtcli_obd) {
348 mgmtcli_deregister_for_events_t dereg_f;
350 dereg_f = inter_module_get("mgmtcli_deregister_for_events");
351 dereg_f(cli->cl_mgmtcli_obd, obddev);
352 inter_module_put("mgmtcli_deregister_for_events");
354 class_destroy_import(cli->cl_import);
355 cli->cl_import = NULL;
357 ldlm_put_ref(flags & OBD_OPT_FORCE);
362 int client_connect_import(struct lustre_handle *dlm_handle,
363 struct obd_device *obd,
364 struct obd_uuid *cluuid,
365 unsigned long connect_flags)
367 struct client_obd *cli = &obd->u.cli;
368 struct obd_import *imp = cli->cl_import;
369 struct obd_export *exp;
374 rc = class_connect(dlm_handle, obd, cluuid);
378 cli->cl_conn_count++;
379 if (cli->cl_conn_count > 1)
381 exp = class_conn2export(dlm_handle);
383 if (obd->obd_namespace != NULL)
384 CERROR("already have namespace!\n");
385 obd->obd_namespace = ldlm_namespace_new(obd->obd_name,
386 LDLM_NAMESPACE_CLIENT);
387 if (obd->obd_namespace == NULL)
388 GOTO(out_disco, rc = -ENOMEM);
390 imp->imp_dlm_handle = *dlm_handle;
391 rc = ptlrpc_init_import(imp);
395 imp->imp_connect_flags = connect_flags;
396 rc = ptlrpc_connect_import(imp, NULL);
398 LASSERT (imp->imp_state == LUSTRE_IMP_DISCON);
401 LASSERT(exp->exp_connection);
402 ptlrpc_pinger_add_import(imp);
407 ldlm_namespace_free(obd->obd_namespace, 0);
408 obd->obd_namespace = NULL;
410 cli->cl_conn_count--;
411 class_disconnect(exp, 0);
413 class_export_put(exp);
420 int client_disconnect_export(struct obd_export *exp, int flags)
422 struct obd_device *obd = class_exp2obd(exp);
423 struct client_obd *cli = &obd->u.cli;
424 struct obd_import *imp = cli->cl_import;
429 CERROR("invalid export for disconnect: exp %p cookie "LPX64"\n",
430 exp, exp ? exp->exp_handle.h_cookie : -1);
435 if (!cli->cl_conn_count) {
436 CERROR("disconnecting disconnected device (%s)\n",
438 GOTO(out_sem, rc = -EINVAL);
441 cli->cl_conn_count--;
442 if (cli->cl_conn_count)
443 GOTO(out_no_disconnect, rc = 0);
445 /* Some non-replayable imports (MDS's OSCs) are pinged, so just
446 * delete it regardless. (It's safe to delete an import that was
448 (void)ptlrpc_pinger_del_import(imp);
450 if (obd->obd_namespace != NULL) {
451 /* obd_no_recov == local only */
452 ldlm_cli_cancel_unused(obd->obd_namespace, NULL,
453 obd->obd_no_recov, NULL);
454 ldlm_namespace_free(obd->obd_namespace, obd->obd_no_recov);
455 obd->obd_namespace = NULL;
458 /* Yeah, obd_no_recov also (mainly) means "forced shutdown". */
459 if (obd->obd_no_recov)
460 ptlrpc_invalidate_import(imp, 0);
462 rc = ptlrpc_disconnect_import(imp);
466 err = class_disconnect(exp, 0);
474 /* --------------------------------------------------------------------------
475 * from old lib/target.c
476 * -------------------------------------------------------------------------- */
478 int target_handle_reconnect(struct lustre_handle *conn, struct obd_export *exp,
479 struct obd_uuid *cluuid, int initial_conn)
481 if (exp->exp_connection && !initial_conn) {
482 struct lustre_handle *hdl;
483 hdl = &exp->exp_imp_reverse->imp_remote_handle;
484 /* Might be a re-connect after a partition. */
485 if (!memcmp(&conn->cookie, &hdl->cookie, sizeof conn->cookie)) {
486 CERROR("%s reconnecting\n", cluuid->uuid);
487 conn->cookie = exp->exp_handle.h_cookie;
490 CERROR("%s reconnecting from %s, "
491 "handle mismatch (ours "LPX64", theirs "
492 LPX64")\n", cluuid->uuid,
493 exp->exp_connection->c_remote_uuid.uuid,
494 hdl->cookie, conn->cookie);
495 memset(conn, 0, sizeof *conn);
500 conn->cookie = exp->exp_handle.h_cookie;
501 CDEBUG(D_INFO, "existing export for UUID '%s' at %p\n",
503 CDEBUG(D_IOCTL,"connect: cookie "LPX64"\n", conn->cookie);
507 static char nidstr[PTL_NALFMT_SIZE];
508 int target_handle_connect(struct ptlrpc_request *req)
510 unsigned long connect_flags = 0, *cfp;
511 struct obd_device *target;
512 struct obd_export *export = NULL;
513 struct obd_import *revimp;
514 struct lustre_handle conn;
515 struct obd_uuid tgtuuid;
516 struct obd_uuid cluuid;
517 struct obd_uuid remote_uuid;
522 int initial_conn = 0;
523 char peer_str[PTL_NALFMT_SIZE];
526 OBD_RACE(OBD_FAIL_TGT_CONN_RACE);
528 LASSERT_REQSWAB (req, 0);
529 str = lustre_msg_string(req->rq_reqmsg, 0, sizeof(tgtuuid) - 1);
531 CERROR("bad target UUID for connect\n");
532 GOTO(out, rc = -EINVAL);
535 obd_str2uuid (&tgtuuid, str);
536 target = class_uuid2obd(&tgtuuid);
538 target = class_name2obd(str);
541 if (!target || target->obd_stopping || !target->obd_set_up) {
542 CERROR("UUID '%s' is not available for connect from NID %s\n",
543 str, ptlrpc_peernid2str(&req->rq_peer, nidstr));
544 GOTO(out, rc = -ENODEV);
547 LASSERT_REQSWAB (req, 1);
548 str = lustre_msg_string(req->rq_reqmsg, 1, sizeof(cluuid) - 1);
550 CERROR("bad client UUID for connect\n");
551 GOTO(out, rc = -EINVAL);
554 obd_str2uuid (&cluuid, str);
556 /* XXX extract a nettype and format accordingly */
557 switch (sizeof(ptl_nid_t)) {
558 /* NB the casts only avoid compiler warnings */
560 snprintf(remote_uuid.uuid, sizeof remote_uuid,
561 "NET_"LPX64"_UUID", (__u64)req->rq_peer.peer_id.nid);
564 snprintf(remote_uuid.uuid, sizeof remote_uuid,
565 "NET_%x_UUID", (__u32)req->rq_peer.peer_id.nid);
571 tmp = lustre_msg_buf(req->rq_reqmsg, 2, sizeof conn);
573 GOTO(out, rc = -EPROTO);
575 memcpy(&conn, tmp, sizeof conn);
577 cfp = lustre_msg_buf(req->rq_reqmsg, 3, sizeof(unsigned long));
578 LASSERT(cfp != NULL);
579 connect_flags = *cfp;
581 rc = lustre_pack_reply(req, 0, NULL, NULL);
585 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_INITIAL)
588 /* lctl gets a backstage, all-access pass. */
589 if (obd_uuid_equals(&cluuid, &target->obd_uuid))
590 goto dont_check_exports;
592 spin_lock(&target->obd_dev_lock);
593 list_for_each(p, &target->obd_exports) {
594 export = list_entry(p, struct obd_export, exp_obd_chain);
595 if (obd_uuid_equals(&cluuid, &export->exp_client_uuid)) {
596 spin_unlock(&target->obd_dev_lock);
597 LASSERT(export->exp_obd == target);
599 rc = target_handle_reconnect(&conn, export, &cluuid,
605 /* If we found an export, we already unlocked. */
607 spin_unlock(&target->obd_dev_lock);
608 } else if (req->rq_export == NULL &&
609 atomic_read(&export->exp_rpc_count) > 0) {
610 CWARN("%s: refuse connection from %s/%s to 0x%p/%d\n",
611 target->obd_name, cluuid.uuid,
612 ptlrpc_peernid2str(&req->rq_peer, peer_str),
613 export, atomic_read(&export->exp_refcount));
614 GOTO(out, rc = -EBUSY);
615 } else if (req->rq_export != NULL &&
616 atomic_read(&export->exp_rpc_count) > 1) {
617 CWARN("%s: refuse reconnection from %s@%s to 0x%p/%d\n",
618 target->obd_name, cluuid.uuid,
619 ptlrpc_peernid2str(&req->rq_peer, peer_str),
620 export, atomic_read(&export->exp_rpc_count));
621 GOTO(out, rc = -EBUSY);
623 else if (req->rq_reqmsg->conn_cnt == 1 && !initial_conn) {
624 CERROR("%s reconnected with 1 conn_cnt; cookies not random?\n",
626 GOTO(out, rc = -EALREADY);
629 /* Tell the client if we're in recovery. */
630 /* If this is the first client, start the recovery timer */
631 CWARN("%s: connection from %s@%s/%lu %s\n", target->obd_name, cluuid.uuid,
632 ptlrpc_peernid2str(&req->rq_peer, peer_str), *cfp,
633 target->obd_recovering ? "(recovering)" : "");
634 if (target->obd_recovering) {
635 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
636 target_start_recovery_timer(target);
639 /* Tell the client if we support replayable requests */
640 if (target->obd_replayable)
641 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
643 if (export == NULL) {
644 if (target->obd_recovering) {
645 CERROR("%s denying connection for new client %s@%s: "
646 "%d clients in recovery for %lds\n", target->obd_name,
648 ptlrpc_peernid2str(&req->rq_peer, peer_str),
649 target->obd_recoverable_clients,
650 (target->obd_recovery_timer.expires-jiffies)/HZ);
654 rc = obd_connect(&conn, target, &cluuid, connect_flags);
657 /* Tell the client if we support replayable requests */
658 if (target->obd_replayable)
659 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
661 /* If all else goes well, this is our RPC return code. */
664 if (rc && rc != EALREADY)
667 req->rq_repmsg->handle = conn;
669 /* If the client and the server are the same node, we will already
670 * have an export that really points to the client's DLM export,
671 * because we have a shared handles table.
673 * XXX this will go away when shaver stops sending the "connect" handle
674 * in the real "remote handle" field of the request --phik 24 Apr 2003
676 if (req->rq_export != NULL)
677 class_export_put(req->rq_export);
679 /* ownership of this export ref transfers to the request */
680 export = req->rq_export = class_conn2export(&conn);
681 LASSERT(export != NULL);
683 spin_lock_irqsave(&export->exp_lock, flags);
685 req->rq_repmsg->conn_cnt = export->exp_conn_cnt + 1;
686 } else if (export->exp_conn_cnt >= req->rq_reqmsg->conn_cnt) {
687 CERROR("%s@%s: already connected at a higher conn_cnt: %d > %d\n",
688 cluuid.uuid, ptlrpc_peernid2str(&req->rq_peer, peer_str),
689 export->exp_conn_cnt,
690 req->rq_reqmsg->conn_cnt);
691 spin_unlock_irqrestore(&export->exp_lock, flags);
692 GOTO(out, rc = -EALREADY);
694 export->exp_conn_cnt = req->rq_reqmsg->conn_cnt;
695 spin_unlock_irqrestore(&export->exp_lock, flags);
697 /* request from liblustre? */
698 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT)
699 export->exp_libclient = 1;
701 if (export->exp_connection != NULL)
702 ptlrpc_put_connection(export->exp_connection);
703 export->exp_connection = ptlrpc_get_connection(&req->rq_peer,
706 if (rc == EALREADY) {
707 /* We indicate the reconnection in a flag, not an error code. */
708 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECONNECT);
712 if (target->obd_recovering) {
713 target->obd_connected_clients++;
716 memcpy(&conn, lustre_msg_buf(req->rq_reqmsg, 2, sizeof conn),
719 if (export->exp_imp_reverse != NULL)
720 class_destroy_import(export->exp_imp_reverse);
721 revimp = export->exp_imp_reverse = class_new_import();
722 revimp->imp_connection = ptlrpc_connection_addref(export->exp_connection);
723 revimp->imp_client = &export->exp_obd->obd_ldlm_client;
724 revimp->imp_remote_handle = conn;
725 revimp->imp_obd = target;
726 revimp->imp_dlm_fake = 1;
727 revimp->imp_state = LUSTRE_IMP_FULL;
728 class_import_put(revimp);
730 rc = obd_connect_post(export, connect_flags);
737 int target_handle_disconnect(struct ptlrpc_request *req)
739 struct obd_export *exp;
743 rc = lustre_pack_reply(req, 0, NULL, NULL);
747 /* keep the rq_export around so we can send the reply */
748 exp = class_export_get(req->rq_export);
749 req->rq_status = obd_disconnect(exp, 0);
753 void target_destroy_export(struct obd_export *exp)
755 /* exports created from last_rcvd data, and "fake"
756 exports created by lctl don't have an import */
757 if (exp->exp_imp_reverse != NULL)
758 class_destroy_import(exp->exp_imp_reverse);
760 /* We cancel locks at disconnect time, but this will catch any locks
761 * granted in a race with recovery-induced disconnect. */
762 if (exp->exp_obd->obd_namespace != NULL)
763 ldlm_cancel_locks_for_export(exp);
770 struct ptlrpc_request *
771 ptlrpc_clone_req( struct ptlrpc_request *orig_req)
773 struct ptlrpc_request *copy_req;
774 struct lustre_msg *copy_reqmsg;
776 OBD_ALLOC(copy_req, sizeof *copy_req);
779 OBD_ALLOC(copy_reqmsg, orig_req->rq_reqlen);
781 OBD_FREE(copy_req, sizeof *copy_req);
785 memcpy(copy_req, orig_req, sizeof *copy_req);
786 memcpy(copy_reqmsg, orig_req->rq_reqmsg, orig_req->rq_reqlen);
787 /* the copied req takes over the reply state */
788 orig_req->rq_reply_state = NULL;
790 copy_req->rq_reqmsg = copy_reqmsg;
791 class_export_get(copy_req->rq_export);
792 INIT_LIST_HEAD(©_req->rq_list);
796 void ptlrpc_free_clone( struct ptlrpc_request *req)
798 class_export_put(req->rq_export);
799 list_del(&req->rq_list);
800 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
801 OBD_FREE(req, sizeof *req);
806 static void target_release_saved_req(struct ptlrpc_request *req)
808 class_export_put(req->rq_export);
809 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
810 OBD_FREE(req, sizeof *req);
814 static void target_finish_recovery(struct obd_device *obd)
816 struct list_head *tmp, *n;
819 CWARN("%s: sending delayed replies to recovered clients\n",
822 ldlm_reprocess_all_ns(obd->obd_namespace);
824 /* when recovery finished, cleanup orphans on mds and ost */
825 if (OBT(obd) && OBP(obd, postrecov)) {
826 rc = OBP(obd, postrecov)(obd);
828 CWARN("%s: all clients recovered, %d MDS "
829 "orphans deleted\n", obd->obd_name, rc);
831 CERROR("postrecov failed %d\n", rc);
835 list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
836 struct ptlrpc_request *req;
837 req = list_entry(tmp, struct ptlrpc_request, rq_list);
838 list_del(&req->rq_list);
839 DEBUG_REQ(D_ERROR, req, "delayed:");
841 target_release_saved_req(req);
843 obd->obd_recovery_end = LTIME_S(CURRENT_TIME);
847 static void abort_recovery_queue(struct obd_device *obd)
849 struct ptlrpc_request *req;
850 struct list_head *tmp, *n;
853 list_for_each_safe(tmp, n, &obd->obd_recovery_queue) {
854 req = list_entry(tmp, struct ptlrpc_request, rq_list);
855 list_del(&req->rq_list);
856 DEBUG_REQ(D_ERROR, req, "aborted:");
857 req->rq_status = -ENOTCONN;
858 req->rq_type = PTL_RPC_MSG_ERR;
859 rc = lustre_pack_reply(req, 0, NULL, NULL);
863 DEBUG_REQ(D_ERROR, req,
864 "packing failed for abort-reply; skipping");
866 target_release_saved_req(req);
871 /* Called from a cleanup function if the device is being cleaned up
872 forcefully. The exports should all have been disconnected already,
873 the only thing left to do is
874 - clear the recovery flags
876 - free queued requests and replies, but don't send replies
877 Because the obd_stopping flag is set, no new requests should be received.
880 void target_cleanup_recovery(struct obd_device *obd)
882 struct list_head *tmp, *n;
883 struct ptlrpc_request *req;
885 spin_lock_bh(&obd->obd_processing_task_lock);
886 if (!obd->obd_recovering) {
887 spin_unlock_bh(&obd->obd_processing_task_lock);
891 obd->obd_recovering = obd->obd_abort_recovery = 0;
892 target_cancel_recovery_timer(obd);
893 spin_unlock_bh(&obd->obd_processing_task_lock);
895 list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
896 req = list_entry(tmp, struct ptlrpc_request, rq_list);
897 list_del(&req->rq_list);
898 LASSERT (req->rq_reply_state);
899 lustre_free_reply_state(req->rq_reply_state);
900 target_release_saved_req(req);
902 list_for_each_safe(tmp, n, &obd->obd_recovery_queue) {
903 req = list_entry(tmp, struct ptlrpc_request, rq_list);
904 list_del(&req->rq_list);
905 LASSERT (req->rq_reply_state == 0);
906 target_release_saved_req(req);
911 static void target_abort_recovery(void *data)
913 struct obd_device *obd = data;
915 LASSERT(!obd->obd_recovering);
917 class_disconnect_stale_exports(obd, 0);
919 CERROR("%s: recovery period over; disconnecting unfinished clients.\n",
922 abort_recovery_queue(obd);
923 target_finish_recovery(obd);
924 ptlrpc_run_recovery_over_upcall(obd);
928 static void target_recovery_expired(unsigned long castmeharder)
930 struct obd_device *obd = (struct obd_device *)castmeharder;
931 CERROR("recovery timed out, aborting\n");
932 spin_lock_bh(&obd->obd_processing_task_lock);
933 if (obd->obd_recovering)
934 obd->obd_abort_recovery = 1;
936 wake_up(&obd->obd_next_transno_waitq);
937 spin_unlock_bh(&obd->obd_processing_task_lock);
941 /* obd_processing_task_lock should be held */
942 void target_cancel_recovery_timer(struct obd_device *obd)
944 CDEBUG(D_HA, "%s: cancel recovery timer\n", obd->obd_name);
945 del_timer(&obd->obd_recovery_timer);
949 static void reset_recovery_timer(struct obd_device *obd)
951 spin_lock_bh(&obd->obd_processing_task_lock);
952 if (!obd->obd_recovering) {
953 spin_unlock_bh(&obd->obd_processing_task_lock);
956 CDEBUG(D_HA, "timer will expire in %u seconds\n",
957 OBD_RECOVERY_TIMEOUT / HZ);
958 mod_timer(&obd->obd_recovery_timer, jiffies + OBD_RECOVERY_TIMEOUT);
959 spin_unlock_bh(&obd->obd_processing_task_lock);
963 /* Only start it the first time called */
964 void target_start_recovery_timer(struct obd_device *obd)
966 spin_lock_bh(&obd->obd_processing_task_lock);
967 if (!obd->obd_recovering || timer_pending(&obd->obd_recovery_timer)) {
968 spin_unlock_bh(&obd->obd_processing_task_lock);
971 CWARN("%s: starting recovery timer (%us)\n", obd->obd_name,
972 OBD_RECOVERY_TIMEOUT / HZ);
973 obd->obd_recovery_timer.function = target_recovery_expired;
974 obd->obd_recovery_timer.data = (unsigned long)obd;
975 mod_timer(&obd->obd_recovery_timer, jiffies + OBD_RECOVERY_TIMEOUT);
976 spin_unlock_bh(&obd->obd_processing_task_lock);
980 static int check_for_next_transno(struct obd_device *obd)
982 struct ptlrpc_request *req = NULL;
983 int wake_up = 0, connected, completed, queue_len, max;
984 __u64 next_transno, req_transno;
986 spin_lock_bh(&obd->obd_processing_task_lock);
987 if (!list_empty(&obd->obd_recovery_queue)) {
988 req = list_entry(obd->obd_recovery_queue.next,
989 struct ptlrpc_request, rq_list);
990 req_transno = req->rq_reqmsg->transno;
995 max = obd->obd_max_recoverable_clients;
996 connected = obd->obd_connected_clients;
997 completed = max - obd->obd_recoverable_clients;
998 queue_len = obd->obd_requests_queued_for_recovery;
999 next_transno = obd->obd_next_recovery_transno;
1001 CDEBUG(D_HA,"max: %d, connected: %d, completed: %d, queue_len: %d, "
1002 "req_transno: "LPU64", next_transno: "LPU64"\n",
1003 max, connected, completed, queue_len, req_transno, next_transno);
1004 if (obd->obd_abort_recovery) {
1005 CDEBUG(D_HA, "waking for aborted recovery\n");
1007 } else if (max == completed) {
1008 CDEBUG(D_HA, "waking for completed recovery\n");
1010 } else if (req_transno == next_transno) {
1011 CDEBUG(D_HA, "waking for next ("LPD64")\n", next_transno);
1013 } else if (queue_len + completed == max) {
1014 LASSERT(req->rq_reqmsg->transno >= next_transno);
1016 "waking for skipped transno (skip: "LPD64
1017 ", ql: %d, comp: %d, conn: %d, next: "LPD64")\n",
1018 next_transno, queue_len, completed, max, req_transno);
1019 obd->obd_next_recovery_transno = req_transno;
1022 spin_unlock_bh(&obd->obd_processing_task_lock);
1027 static struct ptlrpc_request *
1028 target_next_replay_req(struct obd_device *obd)
1030 struct l_wait_info lwi = { 0 };
1031 struct ptlrpc_request *req;
1033 CDEBUG(D_HA, "Waiting for transno "LPD64"\n",
1034 obd->obd_next_recovery_transno);
1035 l_wait_event(obd->obd_next_transno_waitq,
1036 check_for_next_transno(obd), &lwi);
1038 spin_lock_bh(&obd->obd_processing_task_lock);
1039 if (obd->obd_abort_recovery) {
1041 } else if (!list_empty(&obd->obd_recovery_queue)) {
1042 req = list_entry(obd->obd_recovery_queue.next,
1043 struct ptlrpc_request, rq_list);
1044 list_del_init(&req->rq_list);
1045 obd->obd_requests_queued_for_recovery--;
1049 spin_unlock_bh(&obd->obd_processing_task_lock);
1053 static int target_recovery_thread(void *arg)
1055 struct obd_device *obd = arg;
1056 struct ptlrpc_request *req;
1057 struct target_recovery_data *trd = &obd->obd_recovery_data;
1058 unsigned long flags;
1061 kportal_daemonize("tgt-recov");
1063 SIGNAL_MASK_LOCK(current, flags);
1064 sigfillset(¤t->blocked);
1066 SIGNAL_MASK_UNLOCK(current, flags);
1068 CERROR("%s: started recovery thread pid %d\n", obd->obd_name,
1070 trd->trd_processing_task = current->pid;
1072 obd->obd_recovering = 1;
1073 complete(&trd->trd_starting);
1075 while (obd->obd_recovering) {
1076 LASSERT(trd->trd_processing_task == current->pid);
1077 req = target_next_replay_req(obd);
1079 char peer_str[PTL_NALFMT_SIZE];
1080 DEBUG_REQ(D_HA, req, "processing t"LPD64" from %s: ",
1081 req->rq_reqmsg->transno,
1082 ptlrpc_peernid2str(&req->rq_peer, peer_str));
1083 (void)trd->trd_recovery_handler(req);
1084 obd->obd_replayed_requests++;
1085 reset_recovery_timer(obd);
1086 /* bug 1580: decide how to properly sync() in recovery*/
1087 //mds_fsync_super(mds->mds_sb);
1088 ptlrpc_free_clone(req);
1089 spin_lock_bh(&obd->obd_processing_task_lock);
1090 obd->obd_next_recovery_transno++;
1091 spin_unlock_bh(&obd->obd_processing_task_lock);
1093 /* recovery is over */
1094 spin_lock_bh(&obd->obd_processing_task_lock);
1095 obd->obd_recovering = 0;
1096 target_cancel_recovery_timer(obd);
1097 if (obd->obd_abort_recovery) {
1098 obd->obd_abort_recovery = 0;
1099 spin_unlock_bh(&obd->obd_processing_task_lock);
1100 target_abort_recovery(obd);
1102 LASSERT(obd->obd_recoverable_clients == 0);
1103 spin_unlock_bh(&obd->obd_processing_task_lock);
1104 target_finish_recovery(obd);
1109 trd->trd_processing_task = 0;
1110 complete(&trd->trd_finishing);
1114 int target_start_recovery_thread(struct obd_device *obd, svc_handler_t handler)
1117 struct target_recovery_data *trd = &obd->obd_recovery_data;
1119 memset(trd, 0, sizeof(*trd));
1120 init_completion(&trd->trd_starting);
1121 init_completion(&trd->trd_finishing);
1122 trd->trd_recovery_handler = handler;
1124 if (kernel_thread(target_recovery_thread, obd, 0) == 0)
1125 wait_for_completion(&trd->trd_starting);
1132 void target_stop_recovery_thread(struct obd_device *obd)
1134 spin_lock_bh(&obd->obd_processing_task_lock);
1135 if (obd->obd_recovery_data.trd_processing_task > 0) {
1136 struct target_recovery_data *trd = &obd->obd_recovery_data;
1137 CERROR("%s: aborting recovery\n", obd->obd_name);
1138 obd->obd_abort_recovery = 1;
1139 wake_up(&obd->obd_next_transno_waitq);
1140 spin_unlock_bh(&obd->obd_processing_task_lock);
1141 wait_for_completion(&trd->trd_finishing);
1143 spin_unlock_bh(&obd->obd_processing_task_lock);
1148 int target_queue_recovery_request(struct ptlrpc_request *req,
1149 struct obd_device *obd)
1151 struct list_head *tmp;
1153 __u64 transno = req->rq_reqmsg->transno;
1155 /* CAVEAT EMPTOR: The incoming request message has been swabbed
1156 * (i.e. buflens etc are in my own byte order), but type-dependent
1157 * buffers (eg mds_body, ost_body etc) have NOT been swabbed. */
1160 INIT_LIST_HEAD(&req->rq_list);
1161 DEBUG_REQ(D_HA, req, "not queueing");
1166 /* If we're processing the queue, we want don't want to queue this
1169 * Also, if this request has a transno less than the one we're waiting
1170 * for, we should process it now. It could (and currently always will)
1171 * be an open request for a descriptor that was opened some time ago.
1173 * Also, a resent, replayed request that has already been
1174 * handled will pass through here and be processed immediately.
1176 spin_lock_bh(&obd->obd_processing_task_lock);
1177 if (obd->obd_recovery_data.trd_processing_task == current->pid ||
1178 transno < obd->obd_next_recovery_transno) {
1179 /* Processing the queue right now, don't re-add. */
1180 lustre_msg_clear_flags(req->rq_reqmsg, MSG_RESENT);
1181 LASSERT(list_empty(&req->rq_list));
1182 spin_unlock_bh(&obd->obd_processing_task_lock);
1185 spin_unlock_bh(&obd->obd_processing_task_lock);
1187 /* A resent, replayed request that is still on the queue; just drop it.
1188 The queued request will handle this. */
1189 if ((lustre_msg_get_flags(req->rq_reqmsg) & (MSG_RESENT | MSG_REPLAY))
1190 == (MSG_RESENT | MSG_REPLAY)) {
1191 DEBUG_REQ(D_ERROR, req, "dropping resent queued req");
1195 req = ptlrpc_clone_req(req);
1199 spin_lock_bh(&obd->obd_processing_task_lock);
1202 list_for_each(tmp, &obd->obd_recovery_queue) {
1203 struct ptlrpc_request *reqiter =
1204 list_entry(tmp, struct ptlrpc_request, rq_list);
1206 if (reqiter->rq_reqmsg->transno > transno) {
1207 list_add_tail(&req->rq_list, &reqiter->rq_list);
1214 list_add_tail(&req->rq_list, &obd->obd_recovery_queue);
1217 obd->obd_requests_queued_for_recovery++;
1218 wake_up(&obd->obd_next_transno_waitq);
1219 spin_unlock_bh(&obd->obd_processing_task_lock);
1224 struct obd_device * target_req2obd(struct ptlrpc_request *req)
1226 return req->rq_export->exp_obd;
1229 int target_queue_final_reply(struct ptlrpc_request *req, int rc)
1231 struct obd_device *obd = target_req2obd(req);
1233 LASSERT ((rc == 0) == (req->rq_reply_state != NULL));
1236 /* Just like ptlrpc_error, but without the sending. */
1237 rc = lustre_pack_reply(req, 0, NULL, NULL);
1238 LASSERT(rc == 0); /* XXX handle this */
1239 req->rq_type = PTL_RPC_MSG_ERR;
1242 LASSERT (!req->rq_reply_state->rs_difficult);
1243 LASSERT(list_empty(&req->rq_list));
1245 req = ptlrpc_clone_req(req);
1247 spin_lock_bh(&obd->obd_processing_task_lock);
1249 list_add(&req->rq_list, &obd->obd_delayed_reply_queue);
1251 /* only count the first "replay over" request from each
1253 if (req->rq_export->exp_replay_needed) {
1254 --obd->obd_recoverable_clients;
1255 req->rq_export->exp_replay_needed = 0;
1256 CWARN("%s: %d recoverable clients remain\n",
1257 obd->obd_name, obd->obd_recoverable_clients);
1259 wake_up(&obd->obd_next_transno_waitq);
1260 spin_unlock_bh(&obd->obd_processing_task_lock);
1265 target_send_reply_msg (struct ptlrpc_request *req, int rc, int fail_id)
1267 if (OBD_FAIL_CHECK(fail_id | OBD_FAIL_ONCE)) {
1268 obd_fail_loc |= OBD_FAIL_ONCE | OBD_FAILED;
1269 DEBUG_REQ(D_ERROR, req, "dropping reply");
1270 /* NB this does _not_ send with ACK disabled, to simulate
1271 * sending OK, but timing out for the ACK */
1272 if (req->rq_reply_state != NULL) {
1273 if (!req->rq_reply_state->rs_difficult) {
1274 lustre_free_reply_state (req->rq_reply_state);
1275 req->rq_reply_state = NULL;
1277 struct ptlrpc_service *svc =
1278 req->rq_rqbd->rqbd_srv_ni->sni_service;
1279 atomic_inc(&svc->srv_outstanding_replies);
1286 req->rq_status = rc;
1287 return (ptlrpc_error(req));
1289 DEBUG_REQ(D_NET, req, "sending reply");
1292 return (ptlrpc_send_reply(req, 1));
1296 target_send_reply(struct ptlrpc_request *req, int rc, int fail_id)
1299 unsigned long flags;
1300 struct ptlrpc_reply_state *rs;
1301 struct obd_device *obd;
1302 struct obd_export *exp;
1303 struct ptlrpc_srv_ni *sni;
1304 struct ptlrpc_service *svc;
1306 sni = req->rq_rqbd->rqbd_srv_ni;
1307 svc = sni->sni_service;
1309 rs = req->rq_reply_state;
1310 if (rs == NULL || !rs->rs_difficult) {
1311 /* The easy case; no notifiers and reply_out_callback()
1312 * cleans up (i.e. we can't look inside rs after a
1313 * successful send) */
1314 netrc = target_send_reply_msg (req, rc, fail_id);
1316 LASSERT (netrc == 0 || req->rq_reply_state == NULL);
1320 /* must be an export if locks saved */
1321 LASSERT (req->rq_export != NULL);
1322 /* req/reply consistent */
1323 LASSERT (rs->rs_srv_ni == sni);
1326 LASSERT (!rs->rs_scheduled);
1327 LASSERT (!rs->rs_scheduled_ever);
1328 LASSERT (!rs->rs_handled);
1329 LASSERT (!rs->rs_on_net);
1330 LASSERT (rs->rs_export == NULL);
1331 LASSERT (list_empty(&rs->rs_obd_list));
1332 LASSERT (list_empty(&rs->rs_exp_list));
1334 exp = class_export_get (req->rq_export);
1337 /* disable reply scheduling onto srv_reply_queue while I'm setting up */
1338 rs->rs_scheduled = 1;
1340 rs->rs_xid = req->rq_xid;
1341 rs->rs_transno = req->rq_transno;
1342 rs->rs_export = exp;
1344 spin_lock_irqsave (&obd->obd_uncommitted_replies_lock, flags);
1346 if (rs->rs_transno > obd->obd_last_committed) {
1347 /* not committed already */
1348 list_add_tail (&rs->rs_obd_list,
1349 &obd->obd_uncommitted_replies);
1352 spin_unlock (&obd->obd_uncommitted_replies_lock);
1353 spin_lock (&exp->exp_lock);
1355 list_add_tail (&rs->rs_exp_list, &exp->exp_outstanding_replies);
1357 spin_unlock_irqrestore (&exp->exp_lock, flags);
1359 netrc = target_send_reply_msg (req, rc, fail_id);
1361 spin_lock_irqsave (&svc->srv_lock, flags);
1363 svc->srv_n_difficult_replies++;
1365 if (netrc != 0) /* error sending: reply is off the net */
1368 if (!rs->rs_on_net || /* some notifier */
1369 list_empty(&rs->rs_exp_list) || /* completed already */
1370 list_empty(&rs->rs_obd_list)) {
1371 list_add_tail (&rs->rs_list, &svc->srv_reply_queue);
1372 wake_up (&svc->srv_waitq);
1374 list_add (&rs->rs_list, &sni->sni_active_replies);
1375 rs->rs_scheduled = 0; /* allow notifier to schedule */
1378 spin_unlock_irqrestore (&svc->srv_lock, flags);
1381 int target_handle_ping(struct ptlrpc_request *req)
1383 return lustre_pack_reply(req, 0, NULL, NULL);