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>
39 #include <linux/lustre_gs.h>
41 /* @priority: if non-zero, move the selected to the list head
42 * @nocreate: if non-zero, only search in existed connections
44 static int import_set_conn(struct obd_import *imp, struct obd_uuid *uuid,
45 int priority, int nocreate)
47 struct ptlrpc_connection *ptlrpc_conn;
48 struct obd_import_conn *imp_conn = NULL, *item;
52 LASSERT(!(nocreate && !priority));
54 ptlrpc_conn = ptlrpc_uuid_to_connection(uuid);
56 CERROR("can't find connection %s\n", uuid->uuid);
61 OBD_ALLOC(imp_conn, sizeof(*imp_conn));
63 CERROR("fail to alloc memory\n");
64 GOTO(out_put, rc = -ENOMEM);
68 spin_lock(&imp->imp_lock);
69 list_for_each_entry(item, &imp->imp_conn_list, oic_item) {
70 if (obd_uuid_equals(uuid, &item->oic_uuid)) {
72 list_del(&item->oic_item);
73 list_add(&item->oic_item, &imp->imp_conn_list);
74 item->oic_last_attempt = 0;
76 CDEBUG(D_HA, "imp %p@%s: find existed conn %s%s\n",
77 imp, imp->imp_obd->obd_name, uuid->uuid,
78 (priority ? ", move to head." : ""));
79 spin_unlock(&imp->imp_lock);
80 GOTO(out_free, rc = 0);
85 imp_conn->oic_conn = ptlrpc_conn;
86 imp_conn->oic_uuid = *uuid;
87 imp_conn->oic_last_attempt = 0;
89 list_add(&imp_conn->oic_item, &imp->imp_conn_list);
91 list_add_tail(&imp_conn->oic_item, &imp->imp_conn_list);
92 CDEBUG(D_HA, "imp %p@%s: add connection %s at %s\n",
93 imp, imp->imp_obd->obd_name, uuid->uuid,
94 (priority ? "head" : "tail"));
98 spin_unlock(&imp->imp_lock);
102 OBD_FREE(imp_conn, sizeof(*imp_conn));
104 ptlrpc_put_connection(ptlrpc_conn);
108 int import_set_conn_priority(struct obd_import *imp, struct obd_uuid *uuid)
110 return import_set_conn(imp, uuid, 1, 1);
113 int client_import_add_conn(struct obd_import *imp, struct obd_uuid *uuid,
116 return import_set_conn(imp, uuid, priority, 0);
119 int client_import_del_conn(struct obd_import *imp, struct obd_uuid *uuid)
121 struct obd_import_conn *imp_conn;
122 struct obd_export *dlmexp;
126 spin_lock(&imp->imp_lock);
127 if (list_empty(&imp->imp_conn_list)) {
128 LASSERT(!imp->imp_conn_current);
129 LASSERT(!imp->imp_connection);
133 list_for_each_entry(imp_conn, &imp->imp_conn_list, oic_item) {
134 if (!obd_uuid_equals(uuid, &imp_conn->oic_uuid))
136 LASSERT(imp_conn->oic_conn);
138 /* is current conn? */
139 if (imp_conn == imp->imp_conn_current) {
140 LASSERT(imp_conn->oic_conn == imp->imp_connection);
142 if (imp->imp_state != LUSTRE_IMP_CLOSED &&
143 imp->imp_state != LUSTRE_IMP_DISCON) {
144 CERROR("can't remove current connection\n");
145 GOTO(out, rc = -EBUSY);
148 ptlrpc_put_connection(imp->imp_connection);
149 imp->imp_connection = NULL;
151 dlmexp = class_conn2export(&imp->imp_dlm_handle);
152 if (dlmexp && dlmexp->exp_connection) {
153 LASSERT(dlmexp->exp_connection ==
155 ptlrpc_put_connection(dlmexp->exp_connection);
156 dlmexp->exp_connection = NULL;
160 list_del(&imp_conn->oic_item);
161 ptlrpc_put_connection(imp_conn->oic_conn);
162 OBD_FREE(imp_conn, sizeof(*imp_conn));
163 CDEBUG(D_HA, "imp %p@%s: remove connection %s\n",
164 imp, imp->imp_obd->obd_name, uuid->uuid);
169 spin_unlock(&imp->imp_lock);
171 CERROR("connection %s not found\n", uuid->uuid);
175 int client_obd_setup(struct obd_device *obddev, obd_count len, void *buf)
177 struct lustre_cfg* lcfg = buf;
178 struct client_obd *cli = &obddev->u.cli;
179 struct obd_import *imp;
180 struct obd_uuid server_uuid;
181 int rq_portal, rp_portal, connect_op;
182 char *name = obddev->obd_type->typ_name;
183 char *mgmt_name = NULL;
187 /* In a more perfect world, we would hang a ptlrpc_client off of
188 * obd_type and just use the values from there. */
189 if (!strcmp(name, OBD_OSC_DEVICENAME)) {
190 rq_portal = OST_REQUEST_PORTAL;
191 rp_portal = OSC_REPLY_PORTAL;
192 connect_op = OST_CONNECT;
193 } else if (!strcmp(name, OBD_MDC_DEVICENAME)) {
194 rq_portal = MDS_REQUEST_PORTAL;
195 rp_portal = MDC_REPLY_PORTAL;
196 connect_op = MDS_CONNECT;
197 } else if (!strcmp(name, OBD_MGMTCLI_DEVICENAME)) {
198 rq_portal = MGMT_REQUEST_PORTAL;
199 rp_portal = MGMT_REPLY_PORTAL;
200 connect_op = MGMT_CONNECT;
201 } else if (!strcmp(name, LUSTRE_GKC_NAME)) {
202 rq_portal = GKS_REQUEST_PORTAL;
203 rp_portal = GKC_REPLY_PORTAL;
204 connect_op = GKS_CONNECT;
207 CERROR("unknown client OBD type \"%s\", can't setup\n",
213 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
214 CERROR("requires a TARGET UUID\n");
218 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > 37) {
219 CERROR("client UUID must be less than 38 characters\n");
223 if (LUSTRE_CFG_BUFLEN(lcfg, 2) < 1) {
224 CERROR("setup requires a SERVER UUID\n");
228 if (LUSTRE_CFG_BUFLEN(lcfg, 2) > 37) {
229 CERROR("target UUID must be less than 38 characters\n");
233 sema_init(&cli->cl_sem, 1);
234 cli->cl_conn_count = 0;
235 memcpy(server_uuid.uuid, lustre_cfg_buf(lcfg, 2),
236 min_t(unsigned int, LUSTRE_CFG_BUFLEN(lcfg, 2),
237 sizeof(server_uuid)));
240 cli->cl_avail_grant = 0;
241 /* FIXME: should limit this for the sum of all cl_dirty_max */
242 cli->cl_dirty_max = OSC_MAX_DIRTY_DEFAULT * 1024 * 1024;
243 if (cli->cl_dirty_max >> PAGE_SHIFT > num_physpages / 8)
244 cli->cl_dirty_max = num_physpages << (PAGE_SHIFT - 3);
245 INIT_LIST_HEAD(&cli->cl_cache_waiters);
246 INIT_LIST_HEAD(&cli->cl_loi_ready_list);
247 INIT_LIST_HEAD(&cli->cl_loi_write_list);
248 INIT_LIST_HEAD(&cli->cl_loi_read_list);
249 spin_lock_init(&cli->cl_loi_list_lock);
250 cli->cl_r_in_flight = 0;
251 cli->cl_w_in_flight = 0;
252 spin_lock_init(&cli->cl_read_rpc_hist.oh_lock);
253 spin_lock_init(&cli->cl_write_rpc_hist.oh_lock);
254 spin_lock_init(&cli->cl_read_page_hist.oh_lock);
255 spin_lock_init(&cli->cl_write_page_hist.oh_lock);
257 memset(&cli->cl_last_write_time, 0,
258 sizeof(cli->cl_last_write_time));
259 cli->cl_write_gap_sum = 0;
260 cli->cl_write_gaps = 0;
261 cli->cl_write_num = 0;
262 cli->cl_read_num = 0;
264 if (num_physpages >> (20 - PAGE_SHIFT) <= 128) { /* <= 128 MB */
265 cli->cl_max_pages_per_rpc = PTLRPC_MAX_BRW_PAGES / 4;
266 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT / 4;
268 } else if (num_physpages >> (20 - PAGE_SHIFT) <= 512) { /* <= 512 MB */
269 cli->cl_max_pages_per_rpc = PTLRPC_MAX_BRW_PAGES / 2;
270 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT / 2;
273 cli->cl_max_pages_per_rpc = PTLRPC_MAX_BRW_PAGES;
274 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT;
279 CERROR("ldlm_get_ref failed: %d\n", rc);
283 ptlrpc_init_client(rq_portal, rp_portal, name,
284 &obddev->obd_ldlm_client);
286 imp = class_new_import();
288 GOTO(err_ldlm, rc = -ENOENT);
289 imp->imp_client = &obddev->obd_ldlm_client;
290 imp->imp_obd = obddev;
291 imp->imp_connect_op = connect_op;
292 imp->imp_generation = 0;
293 imp->imp_initial_recov = 1;
294 INIT_LIST_HEAD(&imp->imp_pinger_chain);
295 memcpy(imp->imp_target_uuid.uuid, lustre_cfg_buf(lcfg, 1),
296 LUSTRE_CFG_BUFLEN(lcfg, 1));
297 class_import_put(imp);
299 rc = client_import_add_conn(imp, &server_uuid, 1);
301 CERROR("can't add initial connection\n");
302 GOTO(err_import, rc);
305 cli->cl_import = imp;
306 cli->cl_max_mds_easize = sizeof(struct lov_mds_md);
307 cli->cl_max_mds_cookiesize = sizeof(struct llog_cookie);
308 cli->cl_sandev = to_kdev_t(0);
310 if (LUSTRE_CFG_BUFLEN(lcfg, 3) > 0) {
311 if (!strcmp(lustre_cfg_string(lcfg, 3), "inactive")) {
312 CDEBUG(D_HA, "marking %s %s->%s as inactive\n",
313 name, obddev->obd_name,
314 imp->imp_target_uuid.uuid);
315 imp->imp_invalid = 1;
317 if (LUSTRE_CFG_BUFLEN(lcfg, 4) > 0)
318 mgmt_name = lustre_cfg_string(lcfg, 4);
320 mgmt_name = lustre_cfg_string(lcfg, 3);
324 if (mgmt_name != NULL) {
325 /* Register with management client if we need to. */
326 CDEBUG(D_HA, "%s registering with %s for events about %s\n",
327 obddev->obd_name, mgmt_name, server_uuid.uuid);
329 mgmt_obd = class_name2obd(mgmt_name);
331 CERROR("can't find mgmtcli %s to register\n",
333 GOTO(err_import, rc = -ENOSYS);
336 register_f = (mgmtcli_register_for_events_t)symbol_get("mgmtcli_register_for_events");
338 CERROR("can't i_m_g mgmtcli_register_for_events\n");
339 GOTO(err_import, rc = -ENOSYS);
342 rc = register_f(mgmt_obd, obddev, &imp->imp_target_uuid);
343 symbol_put("mgmtcli_register_for_events");
346 cli->cl_mgmtcli_obd = mgmt_obd;
352 class_destroy_import(imp);
360 int client_obd_cleanup(struct obd_device *obddev, int flags)
362 struct client_obd *cli = &obddev->u.cli;
367 if (cli->cl_mgmtcli_obd) {
368 mgmtcli_deregister_for_events_t dereg_f;
370 dereg_f = (mgmtcli_deregister_for_events_t)symbol_get("mgmtcli_deregister_for_events");
371 dereg_f(cli->cl_mgmtcli_obd, obddev);
372 symbol_put("mgmtcli_deregister_for_events");
375 /* Here we try to drop the security structure after destroy import,
376 * to avoid issue of "sleep in spinlock".
378 class_import_get(cli->cl_import);
379 class_destroy_import(cli->cl_import);
380 ptlrpcs_import_drop_sec(cli->cl_import);
381 class_import_put(cli->cl_import);
382 cli->cl_import = NULL;
384 if (cli->cl_write_gap_sum) {
385 CWARN("%s: (write num: %lu, read num: %lu): %lu write gaps: %lu "
386 "av. (usec), %lu total (usec)\n", obddev->obd_name,
387 cli->cl_write_num, cli->cl_read_num, cli->cl_write_gaps,
388 cli->cl_write_gap_sum / cli->cl_write_gaps,
389 cli->cl_write_gap_sum);
392 ldlm_put_ref(flags & OBD_OPT_FORCE);
396 int client_connect_import(struct lustre_handle *dlm_handle,
397 struct obd_device *obd,
398 struct obd_uuid *cluuid,
399 struct obd_connect_data *conn_data,
400 unsigned long connect_flags)
402 struct client_obd *cli = &obd->u.cli;
403 struct obd_import *imp = cli->cl_import;
404 struct obd_export *exp;
409 rc = class_connect(dlm_handle, obd, cluuid);
413 cli->cl_conn_count++;
414 if (cli->cl_conn_count > 1)
416 exp = class_conn2export(dlm_handle);
418 if (obd->obd_namespace != NULL)
419 CERROR("already have namespace!\n");
420 obd->obd_namespace = ldlm_namespace_new(obd->obd_name,
421 LDLM_NAMESPACE_CLIENT);
422 if (obd->obd_namespace == NULL)
423 GOTO(out_disco, rc = -ENOMEM);
425 rc = ptlrpcs_import_get_sec(imp);
429 imp->imp_dlm_handle = *dlm_handle;
430 rc = ptlrpc_init_import(imp);
434 imp->imp_connect_flags = connect_flags;
436 memcpy(&imp->imp_connect_data, conn_data, sizeof(*conn_data));
438 rc = ptlrpc_connect_import(imp, NULL);
440 LASSERT (imp->imp_state == LUSTRE_IMP_DISCON);
443 LASSERT(exp->exp_connection);
444 ptlrpc_pinger_add_import(imp);
449 ldlm_namespace_free(obd->obd_namespace, 0);
450 obd->obd_namespace = NULL;
452 cli->cl_conn_count--;
453 class_disconnect(exp, 0);
455 class_export_put(exp);
462 int client_disconnect_export(struct obd_export *exp, unsigned long flags)
464 struct obd_device *obd = class_exp2obd(exp);
465 struct client_obd *cli = &obd->u.cli;
466 struct obd_import *imp = cli->cl_import;
471 CERROR("invalid export for disconnect: exp %p cookie "LPX64"\n",
472 exp, exp ? exp->exp_handle.h_cookie : -1);
477 if (!cli->cl_conn_count) {
478 CERROR("disconnecting disconnected device (%s)\n",
480 GOTO(out_sem, rc = -EINVAL);
483 cli->cl_conn_count--;
484 if (cli->cl_conn_count)
485 GOTO(out_no_disconnect, rc = 0);
487 /* Some non-replayable imports (MDS's OSCs) are pinged, so just
488 * delete it regardless. (It's safe to delete an import that was
490 (void)ptlrpc_pinger_del_import(imp);
492 if (obd->obd_namespace != NULL) {
493 /* obd_no_recov == local only */
494 ldlm_cli_cancel_unused(obd->obd_namespace, NULL,
495 obd->obd_no_recov, NULL);
496 ldlm_namespace_free(obd->obd_namespace, obd->obd_no_recov);
497 obd->obd_namespace = NULL;
501 * Yeah, obd_no_recov also (mainly) means "forced shutdown".
503 if (obd->obd_no_recov)
504 ptlrpc_invalidate_import(imp, 0);
506 rc = ptlrpc_disconnect_import(imp);
510 err = class_disconnect(exp, 0);
518 /* --------------------------------------------------------------------------
519 * from old lib/target.c
520 * -------------------------------------------------------------------------- */
522 int target_handle_reconnect(struct lustre_handle *conn, struct obd_export *exp,
523 struct obd_uuid *cluuid, int initial_conn)
525 if (exp->exp_connection && !initial_conn) {
526 struct lustre_handle *hdl;
527 hdl = &exp->exp_imp_reverse->imp_remote_handle;
528 /* Might be a re-connect after a partition. */
529 if (!memcmp(&conn->cookie, &hdl->cookie, sizeof conn->cookie)) {
530 CERROR("%s reconnecting\n", cluuid->uuid);
531 conn->cookie = exp->exp_handle.h_cookie;
534 CERROR("%s reconnecting from %s, "
535 "handle mismatch (ours "LPX64", theirs "
536 LPX64")\n", cluuid->uuid,
537 exp->exp_connection->c_remote_uuid.uuid,
538 hdl->cookie, conn->cookie);
539 memset(conn, 0, sizeof *conn);
544 conn->cookie = exp->exp_handle.h_cookie;
545 CDEBUG(D_INFO, "existing export for UUID '%s' at %p\n",
547 CDEBUG(D_IOCTL,"connect: cookie "LPX64"\n", conn->cookie);
551 static inline int ptlrpc_peer_is_local(struct ptlrpc_peer *peer)
553 ptl_process_id_t myid;
555 PtlGetId(peer->peer_ni->pni_ni_h, &myid);
556 return (memcmp(&peer->peer_id, &myid, sizeof(myid)) == 0);
559 /* To check whether the p_flavor is in deny list or not
562 * EPERM found, refuse
565 static int check_deny_list(struct list_head *head, __u32 flavor)
567 deny_sec_t *p_deny_sec = NULL;
568 deny_sec_t *n_deny_sec = NULL;
570 list_for_each_entry_safe(p_deny_sec, n_deny_sec, head, list) {
571 if (p_deny_sec->flavor == flavor)
577 int target_check_deny_sec(struct obd_device *target, struct ptlrpc_request *req)
582 flavor = req->rq_req_secflvr;
584 if (!strcmp(target->obd_type->typ_name, OBD_MDS_DEVICENAME)) {
585 spin_lock(&target->u.mds.mds_denylist_lock);
586 rc = check_deny_list(&target->u.mds.mds_denylist, flavor);
587 spin_unlock(&target->u.mds.mds_denylist_lock);
588 } else if (!strcmp(target->obd_type->typ_name, OBD_FILTER_DEVICENAME)) {
589 spin_lock(&target->u.filter.fo_denylist_lock);
590 rc = check_deny_list(&target->u.filter.fo_denylist, flavor);
591 spin_unlock(&target->u.filter.fo_denylist_lock);
597 int target_handle_connect(struct ptlrpc_request *req)
599 unsigned long connect_flags = 0, *cfp;
600 struct obd_device *target;
601 struct obd_export *export = NULL;
602 struct obd_import *revimp;
603 struct lustre_handle conn;
604 struct obd_uuid tgtuuid;
605 struct obd_uuid cluuid;
606 struct obd_uuid remote_uuid;
608 struct obd_connect_data *conn_data;
609 int conn_data_size = sizeof(*conn_data);
613 int initial_conn = 0;
614 char peer_str[PTL_NALFMT_SIZE];
615 const int offset = 1;
618 OBD_RACE(OBD_FAIL_TGT_CONN_RACE);
620 LASSERT_REQSWAB (req, offset + 0);
621 str = lustre_msg_string(req->rq_reqmsg, offset + 0,
622 sizeof(tgtuuid) - 1);
624 CERROR("bad target UUID for connect\n");
625 GOTO(out, rc = -EINVAL);
628 obd_str2uuid (&tgtuuid, str);
629 target = class_uuid2obd(&tgtuuid);
631 target = class_name2obd(str);
633 if (!target || target->obd_stopping || !target->obd_set_up) {
634 CERROR("UUID '%s' is not available for connect from %s\n",
635 str, req->rq_peerstr);
636 GOTO(out, rc = -ENODEV);
639 /* check the secure deny list of mds/ost */
640 rc = target_check_deny_sec(target, req);
644 LASSERT_REQSWAB (req, offset + 1);
645 str = lustre_msg_string(req->rq_reqmsg, offset + 1, sizeof(cluuid) - 1);
647 CERROR("bad client UUID for connect\n");
648 GOTO(out, rc = -EINVAL);
651 obd_str2uuid (&cluuid, str);
653 /* XXX extract a nettype and format accordingly */
654 switch (sizeof(ptl_nid_t)) {
655 /* NB the casts only avoid compiler warnings */
657 snprintf((char *)remote_uuid.uuid, sizeof(remote_uuid),
658 "NET_"LPX64"_UUID", (__u64)req->rq_peer.peer_id.nid);
661 snprintf((char *)remote_uuid.uuid, sizeof(remote_uuid),
662 "NET_%x_UUID", (__u32)req->rq_peer.peer_id.nid);
668 tmp = lustre_msg_buf(req->rq_reqmsg, offset + 2, sizeof(conn));
670 GOTO(out, rc = -EPROTO);
672 memcpy(&conn, tmp, sizeof conn);
674 cfp = lustre_msg_buf(req->rq_reqmsg, offset + 3, sizeof(unsigned long));
675 LASSERT(cfp != NULL);
676 connect_flags = *cfp;
678 conn_data = lustre_swab_reqbuf(req, offset + 4, sizeof(*conn_data),
679 lustre_swab_connect);
681 GOTO(out, rc = -EPROTO);
683 rc = lustre_pack_reply(req, 1, &conn_data_size, NULL);
687 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_INITIAL)
690 /* lctl gets a backstage, all-access pass. */
691 if (obd_uuid_equals(&cluuid, &target->obd_uuid))
692 goto dont_check_exports;
694 spin_lock(&target->obd_dev_lock);
695 list_for_each(p, &target->obd_exports) {
696 export = list_entry(p, struct obd_export, exp_obd_chain);
697 if (obd_uuid_equals(&cluuid, &export->exp_client_uuid)) {
698 spin_unlock(&target->obd_dev_lock);
699 LASSERT(export->exp_obd == target);
701 rc = target_handle_reconnect(&conn, export, &cluuid,
707 /* If we found an export, we already unlocked. */
709 spin_unlock(&target->obd_dev_lock);
710 } else if (req->rq_export == NULL &&
711 atomic_read(&export->exp_rpc_count) > 0) {
712 CWARN("%s: refuse connection from %s/%s to 0x%p/%d\n",
713 target->obd_name, cluuid.uuid,
714 ptlrpc_peernid2str(&req->rq_peer, peer_str),
715 export, atomic_read(&export->exp_refcount));
716 GOTO(out, rc = -EBUSY);
717 } else if (req->rq_export != NULL &&
718 atomic_read(&export->exp_rpc_count) > 1) {
719 CWARN("%s: refuse reconnection from %s@%s to 0x%p/%d\n",
720 target->obd_name, cluuid.uuid,
721 ptlrpc_peernid2str(&req->rq_peer, peer_str),
722 export, atomic_read(&export->exp_rpc_count));
723 GOTO(out, rc = -EBUSY);
724 } else if (req->rq_reqmsg->conn_cnt == 1 && !initial_conn) {
725 CERROR("%s reconnected with 1 conn_cnt; cookies not random?\n",
727 GOTO(out, rc = -EALREADY);
730 /* Tell the client if we're in recovery. */
731 /* If this is the first client, start the recovery timer */
732 CWARN("%s: connection from %s@%s/%lu %st"LPU64"\n", target->obd_name,
733 cluuid.uuid, ptlrpc_peernid2str(&req->rq_peer, peer_str), *cfp,
734 target->obd_recovering ? "recovering/" : "", conn_data->transno);
736 if (target->obd_recovering) {
737 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
738 target_start_recovery_timer(target);
742 /* Tell the client if we support replayable requests */
743 if (target->obd_replayable)
744 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
747 if (export == NULL) {
748 if (target->obd_recovering) {
749 CERROR("%s denying connection for new client %s@%s: "
750 "%d clients in recovery for %lds\n", target->obd_name,
752 ptlrpc_peernid2str(&req->rq_peer, peer_str),
753 target->obd_recoverable_clients,
754 (target->obd_recovery_timer.expires-jiffies)/HZ);
758 rc = obd_connect(&conn, target, &cluuid, conn_data,
763 /* Return only the parts of obd_connect_data that we understand, so the
764 * client knows that we don't understand the rest. */
765 conn_data->ocd_connect_flags &= OBD_CONNECT_SUPPORTED;
766 memcpy(lustre_msg_buf(req->rq_repmsg, 0, sizeof(*conn_data)), conn_data,
769 /* Tell the client if we support replayable requests */
770 if (target->obd_replayable)
771 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
773 /* If all else goes well, this is our RPC return code. */
776 if (rc && rc != EALREADY)
779 req->rq_repmsg->handle = conn;
781 /* If the client and the server are the same node, we will already
782 * have an export that really points to the client's DLM export,
783 * because we have a shared handles table.
785 * XXX this will go away when shaver stops sending the "connect" handle
786 * in the real "remote handle" field of the request --phik 24 Apr 2003
788 if (req->rq_export != NULL)
789 class_export_put(req->rq_export);
791 /* ownership of this export ref transfers to the request */
792 export = req->rq_export = class_conn2export(&conn);
793 LASSERT(export != NULL);
795 spin_lock_irqsave(&export->exp_lock, flags);
797 req->rq_repmsg->conn_cnt = export->exp_conn_cnt + 1;
798 } else if (export->exp_conn_cnt >= req->rq_reqmsg->conn_cnt) {
799 CERROR("%s@%s: already connected at a higher conn_cnt: %d > %d\n",
800 cluuid.uuid, ptlrpc_peernid2str(&req->rq_peer, peer_str),
801 export->exp_conn_cnt,
802 req->rq_reqmsg->conn_cnt);
803 spin_unlock_irqrestore(&export->exp_lock, flags);
804 GOTO(out, rc = -EALREADY);
806 export->exp_conn_cnt = req->rq_reqmsg->conn_cnt;
807 spin_unlock_irqrestore(&export->exp_lock, flags);
809 /* request from liblustre? */
810 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT)
811 export->exp_libclient = 1;
813 if (!(lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_ASYNC) &&
814 ptlrpc_peer_is_local(&req->rq_peer)) {
815 CWARN("%s: exp %p set sync\n", target->obd_name, export);
816 export->exp_sync = 1;
818 CDEBUG(D_HA, "%s: exp %p set async\n",target->obd_name,export);
819 export->exp_sync = 0;
822 if (export->exp_connection != NULL)
823 ptlrpc_put_connection(export->exp_connection);
824 export->exp_connection = ptlrpc_get_connection(&req->rq_peer,
827 if (rc == EALREADY) {
828 /* We indicate the reconnection in a flag, not an error code. */
829 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECONNECT);
833 spin_lock_bh(&target->obd_processing_task_lock);
834 if (target->obd_recovering && export->exp_connected == 0) {
835 __u64 t = conn_data->transno;
836 export->exp_connected = 1;
837 if ((lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_TRANSNO)
838 && t < target->obd_next_recovery_transno)
839 target->obd_next_recovery_transno = t;
840 target->obd_connected_clients++;
841 if (target->obd_connected_clients == target->obd_max_recoverable_clients)
842 wake_up(&target->obd_next_transno_waitq);
844 spin_unlock_bh(&target->obd_processing_task_lock);
846 memcpy(&conn, lustre_msg_buf(req->rq_reqmsg, offset + 2, sizeof(conn)),
849 if (export->exp_imp_reverse != NULL) {
850 /* same logic as client_obd_cleanup */
851 class_import_get(export->exp_imp_reverse);
852 class_destroy_import(export->exp_imp_reverse);
853 ptlrpcs_import_drop_sec(export->exp_imp_reverse);
854 class_import_put(export->exp_imp_reverse);
857 /* for the rest part, we return -ENOTCONN in case of errors
858 * in order to let client initialize connection again.
860 revimp = export->exp_imp_reverse = class_new_import();
862 CERROR("fail to alloc new reverse import.\n");
863 GOTO(out, rc = -ENOTCONN);
866 revimp->imp_connection = ptlrpc_connection_addref(export->exp_connection);
867 revimp->imp_client = &export->exp_obd->obd_ldlm_client;
868 revimp->imp_remote_handle = conn;
869 revimp->imp_obd = target;
870 revimp->imp_dlm_fake = 1;
871 revimp->imp_state = LUSTRE_IMP_FULL;
873 rc = ptlrpcs_import_get_sec(revimp);
875 CERROR("reverse import can not get sec: %d\n", rc);
876 class_destroy_import(revimp);
877 export->exp_imp_reverse = NULL;
878 GOTO(out, rc = -ENOTCONN);
881 class_import_put(revimp);
883 rc = obd_connect_post(export, initial_conn, connect_flags);
890 int target_handle_disconnect(struct ptlrpc_request *req)
892 struct obd_export *exp;
896 rc = lustre_pack_reply(req, 0, NULL, NULL);
900 /* keep the rq_export around so we can send the reply */
901 exp = class_export_get(req->rq_export);
902 req->rq_status = obd_disconnect(exp, 0);
906 void target_destroy_export(struct obd_export *exp)
908 /* exports created from last_rcvd data, and "fake"
909 exports created by lctl don't have an import */
910 if (exp->exp_imp_reverse != NULL) {
911 ptlrpcs_import_drop_sec(exp->exp_imp_reverse);
912 class_destroy_import(exp->exp_imp_reverse);
915 /* We cancel locks at disconnect time, but this will catch any locks
916 * granted in a race with recovery-induced disconnect. */
917 if (exp->exp_obd->obd_namespace != NULL)
918 ldlm_cancel_locks_for_export(exp);
925 struct ptlrpc_request *
926 ptlrpc_clone_req( struct ptlrpc_request *orig_req)
928 struct ptlrpc_request *copy_req;
929 struct lustre_msg *copy_reqmsg;
931 OBD_ALLOC(copy_req, sizeof *copy_req);
934 OBD_ALLOC(copy_reqmsg, orig_req->rq_reqlen);
936 OBD_FREE(copy_req, sizeof *copy_req);
940 memcpy(copy_req, orig_req, sizeof *copy_req);
941 memcpy(copy_reqmsg, orig_req->rq_reqmsg, orig_req->rq_reqlen);
942 /* the copied req takes over the reply state and security data */
943 orig_req->rq_reply_state = NULL;
944 orig_req->rq_svcsec_data = NULL;
946 copy_req->rq_reqmsg = copy_reqmsg;
947 class_export_get(copy_req->rq_export);
948 INIT_LIST_HEAD(©_req->rq_list);
953 void ptlrpc_free_clone( struct ptlrpc_request *req)
956 svcsec_cleanup_req(req);
958 class_export_put(req->rq_export);
959 list_del(&req->rq_list);
960 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
961 OBD_FREE(req, sizeof *req);
964 static void target_release_saved_req(struct ptlrpc_request *req)
967 svcsec_cleanup_req(req);
969 class_export_put(req->rq_export);
970 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
971 OBD_FREE(req, sizeof *req);
974 static void target_finish_recovery(struct obd_device *obd)
978 ldlm_reprocess_all_ns(obd->obd_namespace);
980 /* when recovery finished, cleanup orphans on mds and ost */
981 if (OBT(obd) && OBP(obd, postrecov)) {
982 rc = OBP(obd, postrecov)(obd);
984 CWARN("%s: all clients recovered, %d MDS "
985 "orphans deleted\n", obd->obd_name, rc);
987 CERROR("postrecov failed %d\n", rc);
990 obd->obd_recovery_end = LTIME_S(CURRENT_TIME);
994 static void abort_req_replay_queue(struct obd_device *obd)
996 struct ptlrpc_request *req;
997 struct list_head *tmp, *n;
1000 list_for_each_safe(tmp, n, &obd->obd_req_replay_queue) {
1001 req = list_entry(tmp, struct ptlrpc_request, rq_list);
1002 list_del(&req->rq_list);
1003 DEBUG_REQ(D_ERROR, req, "aborted:");
1004 req->rq_status = -ENOTCONN;
1005 req->rq_type = PTL_RPC_MSG_ERR;
1006 rc = lustre_pack_reply(req, 0, NULL, NULL);
1010 DEBUG_REQ(D_ERROR, req,
1011 "packing failed for abort-reply; skipping");
1013 target_release_saved_req(req);
1017 static void abort_lock_replay_queue(struct obd_device *obd)
1019 struct ptlrpc_request *req;
1020 struct list_head *tmp, *n;
1023 list_for_each_safe(tmp, n, &obd->obd_lock_replay_queue) {
1024 req = list_entry(tmp, struct ptlrpc_request, rq_list);
1025 list_del(&req->rq_list);
1026 DEBUG_REQ(D_ERROR, req, "aborted:");
1027 req->rq_status = -ENOTCONN;
1028 req->rq_type = PTL_RPC_MSG_ERR;
1029 rc = lustre_pack_reply(req, 0, NULL, NULL);
1033 DEBUG_REQ(D_ERROR, req,
1034 "packing failed for abort-reply; skipping");
1036 target_release_saved_req(req);
1040 /* Called from a cleanup function if the device is being cleaned up
1041 forcefully. The exports should all have been disconnected already,
1042 the only thing left to do is
1043 - clear the recovery flags
1045 - free queued requests and replies, but don't send replies
1046 Because the obd_stopping flag is set, no new requests should be received.
1049 void target_cleanup_recovery(struct obd_device *obd)
1051 struct list_head *tmp, *n;
1052 struct ptlrpc_request *req;
1054 spin_lock_bh(&obd->obd_processing_task_lock);
1055 if (!obd->obd_recovering) {
1056 spin_unlock_bh(&obd->obd_processing_task_lock);
1060 obd->obd_recovering = obd->obd_abort_recovery = 0;
1061 target_cancel_recovery_timer(obd);
1062 spin_unlock_bh(&obd->obd_processing_task_lock);
1064 list_for_each_safe(tmp, n, &obd->obd_req_replay_queue) {
1065 req = list_entry(tmp, struct ptlrpc_request, rq_list);
1066 list_del(&req->rq_list);
1067 LASSERT (req->rq_reply_state == 0);
1068 target_release_saved_req(req);
1070 list_for_each_safe(tmp, n, &obd->obd_lock_replay_queue) {
1071 req = list_entry(tmp, struct ptlrpc_request, rq_list);
1072 list_del(&req->rq_list);
1073 LASSERT (req->rq_reply_state == 0);
1074 target_release_saved_req(req);
1076 list_for_each_safe(tmp, n, &obd->obd_final_req_queue) {
1077 req = list_entry(tmp, struct ptlrpc_request, rq_list);
1078 list_del(&req->rq_list);
1079 LASSERT (req->rq_reply_state == 0);
1080 target_release_saved_req(req);
1085 static void target_abort_recovery(void *data)
1087 struct obd_device *obd = data;
1089 LASSERT(!obd->obd_recovering);
1091 class_disconnect_stale_exports(obd, 0);
1093 CERROR("%s: recovery period over; disconnecting unfinished clients.\n",
1096 abort_recovery_queue(obd);
1097 target_finish_recovery(obd);
1098 ptlrpc_run_recovery_over_upcall(obd);
1102 static void target_recovery_expired(unsigned long castmeharder)
1104 struct obd_device *obd = (struct obd_device *)castmeharder;
1105 spin_lock_bh(&obd->obd_processing_task_lock);
1106 if (obd->obd_recovering)
1107 obd->obd_abort_recovery = 1;
1109 wake_up(&obd->obd_next_transno_waitq);
1110 spin_unlock_bh(&obd->obd_processing_task_lock);
1114 /* obd_processing_task_lock should be held */
1115 void target_cancel_recovery_timer(struct obd_device *obd)
1117 CDEBUG(D_HA, "%s: cancel recovery timer\n", obd->obd_name);
1118 del_timer(&obd->obd_recovery_timer);
1122 static void reset_recovery_timer(struct obd_device *obd)
1124 spin_lock_bh(&obd->obd_processing_task_lock);
1125 if (!obd->obd_recovering) {
1126 spin_unlock_bh(&obd->obd_processing_task_lock);
1129 CDEBUG(D_HA, "timer will expire in %u seconds\n",
1130 OBD_RECOVERY_TIMEOUT / HZ);
1131 mod_timer(&obd->obd_recovery_timer, jiffies + OBD_RECOVERY_TIMEOUT);
1132 spin_unlock_bh(&obd->obd_processing_task_lock);
1136 /* Only start it the first time called */
1137 void target_start_recovery_timer(struct obd_device *obd)
1139 spin_lock_bh(&obd->obd_processing_task_lock);
1140 if (!obd->obd_recovering || timer_pending(&obd->obd_recovery_timer)) {
1141 spin_unlock_bh(&obd->obd_processing_task_lock);
1144 CWARN("%s: starting recovery timer (%us)\n", obd->obd_name,
1145 OBD_RECOVERY_TIMEOUT / HZ);
1146 obd->obd_recovery_timer.function = target_recovery_expired;
1147 obd->obd_recovery_timer.data = (unsigned long)obd;
1148 mod_timer(&obd->obd_recovery_timer, jiffies + OBD_RECOVERY_TIMEOUT);
1149 spin_unlock_bh(&obd->obd_processing_task_lock);
1153 static int check_for_next_transno(struct obd_device *obd)
1155 struct ptlrpc_request *req = NULL;
1156 int wake_up = 0, connected, completed, queue_len, max;
1157 __u64 next_transno, req_transno;
1159 spin_lock_bh(&obd->obd_processing_task_lock);
1160 if (!list_empty(&obd->obd_req_replay_queue)) {
1161 req = list_entry(obd->obd_req_replay_queue.next,
1162 struct ptlrpc_request, rq_list);
1163 req_transno = req->rq_reqmsg->transno;
1168 max = obd->obd_max_recoverable_clients;
1169 connected = obd->obd_connected_clients;
1170 completed = max - obd->obd_recoverable_clients;
1171 queue_len = obd->obd_requests_queued_for_recovery;
1172 next_transno = obd->obd_next_recovery_transno;
1174 CDEBUG(D_HA,"max: %d, connected: %d, completed: %d, queue_len: %d, "
1175 "req_transno: "LPU64", next_transno: "LPU64"\n",
1176 max, connected, completed, queue_len, req_transno, next_transno);
1177 if (obd->obd_abort_recovery) {
1178 CDEBUG(D_HA, "waking for aborted recovery\n");
1180 } else if (atomic_read(&obd->obd_req_replay_clients) == 0) {
1181 CDEBUG(D_HA, "waking for completed recovery\n");
1183 } else if (req_transno == next_transno) {
1184 CDEBUG(D_HA, "waking for next ("LPD64")\n", next_transno);
1186 } else if (queue_len + completed == max) {
1187 LASSERT(req->rq_reqmsg->transno >= next_transno);
1188 CDEBUG(req_transno > obd->obd_last_committed ? D_ERROR : D_HA,
1189 "waking for skipped transno (skip: "LPD64
1190 ", ql: %d, comp: %d, conn: %d, next: "LPD64")\n",
1191 next_transno, queue_len, completed, max, req_transno);
1192 obd->obd_next_recovery_transno = req_transno;
1194 } else if (queue_len == atomic_read(&obd->obd_req_replay_clients)) {
1195 /* some clients haven't connected in time, but we can try
1196 * to replay requests that demand on already committed ones
1197 * also, we can replay first non-committed transation */
1198 LASSERT(req_transno != 0);
1199 if (req_transno == obd->obd_last_committed + 1) {
1200 obd->obd_next_recovery_transno = req_transno;
1201 } else if (req_transno > obd->obd_last_committed) {
1202 /* can't continue recovery: have no needed transno */
1203 obd->obd_abort_recovery = 1;
1204 CDEBUG(D_ERROR, "abort due to missed clients. max: %d, "
1205 "connected: %d, completed: %d, queue_len: %d, "
1206 "req_transno: "LPU64", next_transno: "LPU64"\n",
1207 max, connected, completed, queue_len,
1208 req_transno, next_transno);
1212 spin_unlock_bh(&obd->obd_processing_task_lock);
1217 static struct ptlrpc_request *
1218 target_next_replay_req(struct obd_device *obd)
1220 struct l_wait_info lwi = { 0 };
1221 struct ptlrpc_request *req;
1223 CDEBUG(D_HA, "Waiting for transno "LPD64"\n",
1224 obd->obd_next_recovery_transno);
1225 l_wait_event(obd->obd_next_transno_waitq,
1226 check_for_next_transno(obd), &lwi);
1228 spin_lock_bh(&obd->obd_processing_task_lock);
1229 if (obd->obd_abort_recovery) {
1231 } else if (!list_empty(&obd->obd_req_replay_queue)) {
1232 req = list_entry(obd->obd_req_replay_queue.next,
1233 struct ptlrpc_request, rq_list);
1234 list_del_init(&req->rq_list);
1235 obd->obd_requests_queued_for_recovery--;
1239 spin_unlock_bh(&obd->obd_processing_task_lock);
1243 static int check_for_next_lock(struct obd_device *obd)
1245 struct ptlrpc_request *req = NULL;
1248 spin_lock_bh(&obd->obd_processing_task_lock);
1249 if (!list_empty(&obd->obd_lock_replay_queue)) {
1250 req = list_entry(obd->obd_lock_replay_queue.next,
1251 struct ptlrpc_request, rq_list);
1252 CDEBUG(D_HA, "waking for next lock\n");
1254 } else if (atomic_read(&obd->obd_lock_replay_clients) == 0) {
1255 CDEBUG(D_HA, "waking for completed lock replay\n");
1257 } else if (obd->obd_abort_recovery) {
1258 CDEBUG(D_HA, "waking for aborted recovery\n");
1261 spin_unlock_bh(&obd->obd_processing_task_lock);
1266 static struct ptlrpc_request *
1267 target_next_replay_lock(struct obd_device *obd)
1269 struct l_wait_info lwi = { 0 };
1270 struct ptlrpc_request *req;
1272 CDEBUG(D_HA, "Waiting for lock\n");
1273 l_wait_event(obd->obd_next_transno_waitq,
1274 check_for_next_lock(obd), &lwi);
1276 spin_lock_bh(&obd->obd_processing_task_lock);
1277 if (obd->obd_abort_recovery) {
1279 } else if (!list_empty(&obd->obd_lock_replay_queue)) {
1280 req = list_entry(obd->obd_lock_replay_queue.next,
1281 struct ptlrpc_request, rq_list);
1282 list_del_init(&req->rq_list);
1286 spin_unlock_bh(&obd->obd_processing_task_lock);
1290 static struct ptlrpc_request *
1291 target_next_final_ping(struct obd_device *obd)
1293 struct ptlrpc_request *req;
1295 spin_lock_bh(&obd->obd_processing_task_lock);
1296 if (!list_empty(&obd->obd_final_req_queue)) {
1297 req = list_entry(obd->obd_final_req_queue.next,
1298 struct ptlrpc_request, rq_list);
1299 list_del_init(&req->rq_list);
1303 spin_unlock_bh(&obd->obd_processing_task_lock);
1307 static int req_replay_done(struct obd_export *exp)
1309 if (exp->exp_req_replay_needed)
1314 static int lock_replay_done(struct obd_export *exp)
1316 if (exp->exp_lock_replay_needed)
1321 static int connect_done(struct obd_export *exp)
1323 if (exp->exp_connected)
1328 static int check_for_clients(struct obd_device *obd)
1330 if (obd->obd_abort_recovery)
1332 LASSERT(obd->obd_connected_clients <= obd->obd_max_recoverable_clients);
1333 if (obd->obd_connected_clients == obd->obd_max_recoverable_clients)
1338 static int target_recovery_thread(void *arg)
1340 struct obd_device *obd = arg;
1341 struct ptlrpc_request *req;
1342 struct target_recovery_data *trd = &obd->obd_recovery_data;
1343 char peer_str[PTL_NALFMT_SIZE];
1344 struct l_wait_info lwi = { 0 };
1345 unsigned long delta;
1346 unsigned long flags;
1349 kportal_daemonize("tgt-recov");
1351 SIGNAL_MASK_LOCK(current, flags);
1352 sigfillset(¤t->blocked);
1354 SIGNAL_MASK_UNLOCK(current, flags);
1356 CERROR("%s: started recovery thread pid %d\n", obd->obd_name,
1358 trd->trd_processing_task = current->pid;
1360 obd->obd_recovering = 1;
1361 complete(&trd->trd_starting);
1363 /* first of all, we have to know the first transno to replay */
1364 obd->obd_abort_recovery = 0;
1365 l_wait_event(obd->obd_next_transno_waitq,
1366 check_for_clients(obd), &lwi);
1368 spin_lock_bh(&obd->obd_processing_task_lock);
1369 target_cancel_recovery_timer(obd);
1370 spin_unlock_bh(&obd->obd_processing_task_lock);
1372 /* If some clients haven't connected in time, evict them */
1373 if (obd->obd_abort_recovery) {
1375 CDEBUG(D_ERROR, "few clients haven't connect in time (%d/%d),"
1376 "evict them ...\n", obd->obd_connected_clients,
1377 obd->obd_max_recoverable_clients);
1378 obd->obd_abort_recovery = 0;
1379 stale = class_disconnect_stale_exports(obd, connect_done, 0);
1380 atomic_sub(stale, &obd->obd_req_replay_clients);
1381 atomic_sub(stale, &obd->obd_lock_replay_clients);
1384 /* next stage: replay requests */
1386 CDEBUG(D_ERROR, "1: request replay stage - %d clients from t"LPU64"\n",
1387 atomic_read(&obd->obd_req_replay_clients),
1388 obd->obd_next_recovery_transno);
1389 while ((req = target_next_replay_req(obd))) {
1390 LASSERT(trd->trd_processing_task == current->pid);
1391 DEBUG_REQ(D_HA, req, "processing t"LPD64" from %s",
1392 req->rq_reqmsg->transno,
1393 ptlrpc_peernid2str(&req->rq_peer, peer_str));
1394 (void)trd->trd_recovery_handler(req);
1395 obd->obd_replayed_requests++;
1396 reset_recovery_timer(obd);
1397 /* bug 1580: decide how to properly sync() in recovery*/
1398 //mds_fsync_super(mds->mds_sb);
1399 ptlrpc_free_clone(req);
1400 spin_lock_bh(&obd->obd_processing_task_lock);
1401 obd->obd_next_recovery_transno++;
1402 spin_unlock_bh(&obd->obd_processing_task_lock);
1405 spin_lock_bh(&obd->obd_processing_task_lock);
1406 target_cancel_recovery_timer(obd);
1407 spin_unlock_bh(&obd->obd_processing_task_lock);
1409 /* If some clients haven't replayed requests in time, evict them */
1410 if (obd->obd_abort_recovery) {
1412 CDEBUG(D_ERROR, "req replay timed out, aborting ...\n");
1413 obd->obd_abort_recovery = 0;
1414 stale = class_disconnect_stale_exports(obd, req_replay_done, 0);
1415 atomic_sub(stale, &obd->obd_lock_replay_clients);
1416 abort_req_replay_queue(obd);
1417 /* XXX for debuggin tests 11 and 17 */
1421 /* The second stage: replay locks */
1422 CDEBUG(D_ERROR, "2: lock replay stage - %d clients\n",
1423 atomic_read(&obd->obd_lock_replay_clients));
1424 while ((req = target_next_replay_lock(obd))) {
1425 LASSERT(trd->trd_processing_task == current->pid);
1426 DEBUG_REQ(D_HA, req, "processing lock from %s: ",
1427 ptlrpc_peernid2str(&req->rq_peer, peer_str));
1428 (void)trd->trd_recovery_handler(req);
1429 reset_recovery_timer(obd);
1430 ptlrpc_free_clone(req);
1431 obd->obd_replayed_locks++;
1434 spin_lock_bh(&obd->obd_processing_task_lock);
1435 target_cancel_recovery_timer(obd);
1436 spin_unlock_bh(&obd->obd_processing_task_lock);
1438 /* If some clients haven't replayed requests in time, evict them */
1439 if (obd->obd_abort_recovery) {
1441 CERROR("lock replay timed out, aborting ...\n");
1442 obd->obd_abort_recovery = 0;
1443 stale = class_disconnect_stale_exports(obd, lock_replay_done, 0);
1444 abort_lock_replay_queue(obd);
1447 /* We drop recoverying flag to forward all new requests
1448 * to regular mds_handle() since now */
1449 spin_lock_bh(&obd->obd_processing_task_lock);
1450 obd->obd_recovering = 0;
1451 spin_unlock_bh(&obd->obd_processing_task_lock);
1453 /* The third stage: reply on final pings */
1454 CDEBUG(D_ERROR, "3: final stage - process recovery completion pings\n");
1455 while ((req = target_next_final_ping(obd))) {
1456 LASSERT(trd->trd_processing_task == current->pid);
1457 DEBUG_REQ(D_HA, req, "processing final ping from %s: ",
1458 ptlrpc_peernid2str(&req->rq_peer, peer_str));
1459 (void)trd->trd_recovery_handler(req);
1460 ptlrpc_free_clone(req);
1463 delta = (jiffies - delta) / HZ;
1464 CDEBUG(D_ERROR,"4: recovery completed in %lus - %d/%d reqs/locks\n",
1465 delta, obd->obd_replayed_requests, obd->obd_replayed_locks);
1466 if (delta > obd_timeout * 2) {
1467 CWARN("too long recovery - read logs\n");
1468 portals_debug_dumplog();
1470 target_finish_recovery(obd);
1472 trd->trd_processing_task = 0;
1473 complete(&trd->trd_finishing);
1477 int target_start_recovery_thread(struct obd_device *obd, svc_handler_t handler)
1480 struct target_recovery_data *trd = &obd->obd_recovery_data;
1482 memset(trd, 0, sizeof(*trd));
1483 init_completion(&trd->trd_starting);
1484 init_completion(&trd->trd_finishing);
1485 trd->trd_recovery_handler = handler;
1487 if (kernel_thread(target_recovery_thread, obd, 0) > 0) {
1488 wait_for_completion(&trd->trd_starting);
1489 LASSERT(obd->obd_recovering != 0);
1496 void target_stop_recovery_thread(struct obd_device *obd)
1498 spin_lock_bh(&obd->obd_processing_task_lock);
1499 if (obd->obd_recovery_data.trd_processing_task > 0) {
1500 struct target_recovery_data *trd = &obd->obd_recovery_data;
1501 CERROR("%s: aborting recovery\n", obd->obd_name);
1502 obd->obd_abort_recovery = 1;
1503 wake_up(&obd->obd_next_transno_waitq);
1504 spin_unlock_bh(&obd->obd_processing_task_lock);
1505 wait_for_completion(&trd->trd_finishing);
1507 spin_unlock_bh(&obd->obd_processing_task_lock);
1512 int target_process_req_flags(struct obd_device *obd, struct ptlrpc_request *req)
1514 struct obd_export *exp = req->rq_export;
1515 LASSERT(exp != NULL);
1516 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REQ_REPLAY_DONE) {
1517 /* client declares he's ready to replay locks */
1518 spin_lock_bh(&obd->obd_processing_task_lock);
1519 if (exp->exp_req_replay_needed) {
1520 LASSERT(atomic_read(&obd->obd_req_replay_clients) > 0);
1521 exp->exp_req_replay_needed = 0;
1522 atomic_dec(&obd->obd_req_replay_clients);
1523 obd->obd_recoverable_clients--;
1524 if (atomic_read(&obd->obd_req_replay_clients) == 0)
1525 CDEBUG(D_HA, "all clients have replayed reqs\n");
1526 wake_up(&obd->obd_next_transno_waitq);
1528 spin_unlock_bh(&obd->obd_processing_task_lock);
1530 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_LOCK_REPLAY_DONE) {
1531 /* client declares he's ready to complete recovery
1532 * so, we put the request on th final queue */
1533 spin_lock_bh(&obd->obd_processing_task_lock);
1534 if (exp->exp_lock_replay_needed) {
1535 LASSERT(atomic_read(&obd->obd_lock_replay_clients) > 0);
1536 exp->exp_lock_replay_needed = 0;
1537 atomic_dec(&obd->obd_lock_replay_clients);
1538 if (atomic_read(&obd->obd_lock_replay_clients) == 0)
1539 CDEBUG(D_HA, "all clients have replayed locks\n");
1540 wake_up(&obd->obd_next_transno_waitq);
1542 spin_unlock_bh(&obd->obd_processing_task_lock);
1548 int target_queue_recovery_request(struct ptlrpc_request *req,
1549 struct obd_device *obd)
1551 struct list_head *tmp;
1553 __u64 transno = req->rq_reqmsg->transno;
1555 if (obd->obd_recovery_data.trd_processing_task == current->pid) {
1556 /* Processing the queue right now, don't re-add. */
1560 target_process_req_flags(obd, req);
1562 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_LOCK_REPLAY_DONE) {
1563 /* client declares he's ready to complete recovery
1564 * so, we put the request on th final queue */
1565 req = ptlrpc_clone_req(req);
1568 DEBUG_REQ(D_HA, req, "queue final req");
1569 spin_lock_bh(&obd->obd_processing_task_lock);
1570 list_add_tail(&req->rq_list, &obd->obd_final_req_queue);
1571 spin_unlock_bh(&obd->obd_processing_task_lock);
1574 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REQ_REPLAY_DONE) {
1575 /* client declares he's ready to replay locks */
1576 req = ptlrpc_clone_req(req);
1579 DEBUG_REQ(D_HA, req, "queue lock replay req");
1580 spin_lock_bh(&obd->obd_processing_task_lock);
1581 list_add_tail(&req->rq_list, &obd->obd_lock_replay_queue);
1582 spin_unlock_bh(&obd->obd_processing_task_lock);
1583 wake_up(&obd->obd_next_transno_waitq);
1588 /* CAVEAT EMPTOR: The incoming request message has been swabbed
1589 * (i.e. buflens etc are in my own byte order), but type-dependent
1590 * buffers (eg mds_body, ost_body etc) have NOT been swabbed. */
1593 INIT_LIST_HEAD(&req->rq_list);
1594 DEBUG_REQ(D_HA, req, "not queueing");
1599 /* If we're processing the queue, we want don't want to queue this
1602 * Also, if this request has a transno less than the one we're waiting
1603 * for, we should process it now. It could (and currently always will)
1604 * be an open request for a descriptor that was opened some time ago.
1606 * Also, a resent, replayed request that has already been
1607 * handled will pass through here and be processed immediately.
1609 spin_lock_bh(&obd->obd_processing_task_lock);
1610 if (transno < obd->obd_next_recovery_transno && check_for_clients(obd)) {
1611 /* Processing the queue right now, don't re-add. */
1612 LASSERT(list_empty(&req->rq_list));
1613 spin_unlock_bh(&obd->obd_processing_task_lock);
1616 spin_unlock_bh(&obd->obd_processing_task_lock);
1618 /* A resent, replayed request that is still on the queue; just drop it.
1619 The queued request will handle this. */
1620 if ((lustre_msg_get_flags(req->rq_reqmsg) & (MSG_RESENT | MSG_REPLAY))
1621 == (MSG_RESENT | MSG_REPLAY)) {
1622 DEBUG_REQ(D_ERROR, req, "dropping resent queued req");
1626 req = ptlrpc_clone_req(req);
1630 spin_lock_bh(&obd->obd_processing_task_lock);
1633 list_for_each(tmp, &obd->obd_req_replay_queue) {
1634 struct ptlrpc_request *reqiter =
1635 list_entry(tmp, struct ptlrpc_request, rq_list);
1637 if (reqiter->rq_reqmsg->transno > transno) {
1638 list_add_tail(&req->rq_list, &reqiter->rq_list);
1645 list_add_tail(&req->rq_list, &obd->obd_req_replay_queue);
1647 obd->obd_requests_queued_for_recovery++;
1648 wake_up(&obd->obd_next_transno_waitq);
1649 spin_unlock_bh(&obd->obd_processing_task_lock);
1653 struct obd_device * target_req2obd(struct ptlrpc_request *req)
1655 return req->rq_export->exp_obd;
1659 target_send_reply_msg (struct ptlrpc_request *req, int rc, int fail_id)
1661 if (OBD_FAIL_CHECK(fail_id | OBD_FAIL_ONCE)) {
1662 obd_fail_loc |= OBD_FAIL_ONCE | OBD_FAILED;
1663 DEBUG_REQ(D_ERROR, req, "dropping reply");
1664 /* NB this does _not_ send with ACK disabled, to simulate
1665 * sending OK, but timing out for the ACK */
1666 if (req->rq_reply_state != NULL) {
1667 if (!req->rq_reply_state->rs_difficult) {
1668 lustre_free_reply_state (req->rq_reply_state);
1669 req->rq_reply_state = NULL;
1671 struct ptlrpc_service *svc =
1672 req->rq_rqbd->rqbd_srv_ni->sni_service;
1673 atomic_inc(&svc->srv_outstanding_replies);
1680 req->rq_status = rc;
1681 return (ptlrpc_error(req));
1683 DEBUG_REQ(D_NET, req, "sending reply");
1686 return (ptlrpc_send_reply(req, 1));
1690 target_send_reply(struct ptlrpc_request *req, int rc, int fail_id)
1693 unsigned long flags;
1694 struct ptlrpc_reply_state *rs;
1695 struct obd_device *obd;
1696 struct obd_export *exp;
1697 struct ptlrpc_srv_ni *sni;
1698 struct ptlrpc_service *svc;
1700 sni = req->rq_rqbd->rqbd_srv_ni;
1701 svc = sni->sni_service;
1703 rs = req->rq_reply_state;
1704 if (rs == NULL || !rs->rs_difficult) {
1705 /* The easy case; no notifiers and reply_out_callback()
1706 * cleans up (i.e. we can't look inside rs after a
1707 * successful send) */
1708 netrc = target_send_reply_msg (req, rc, fail_id);
1710 LASSERT (netrc == 0 || req->rq_reply_state == NULL);
1714 /* must be an export if locks saved */
1715 LASSERT (req->rq_export != NULL);
1716 /* req/reply consistent */
1717 LASSERT (rs->rs_srv_ni == sni);
1720 LASSERT (!rs->rs_scheduled);
1721 LASSERT (!rs->rs_scheduled_ever);
1722 LASSERT (!rs->rs_handled);
1723 LASSERT (!rs->rs_on_net);
1724 LASSERT (rs->rs_export == NULL);
1725 LASSERT (list_empty(&rs->rs_obd_list));
1726 LASSERT (list_empty(&rs->rs_exp_list));
1728 exp = class_export_get (req->rq_export);
1731 /* disable reply scheduling onto srv_reply_queue while I'm setting up */
1732 rs->rs_scheduled = 1;
1734 rs->rs_xid = req->rq_xid;
1735 rs->rs_transno = req->rq_transno;
1736 rs->rs_export = exp;
1738 spin_lock_irqsave (&obd->obd_uncommitted_replies_lock, flags);
1740 if (rs->rs_transno > obd->obd_last_committed) {
1741 /* not committed already */
1742 list_add_tail (&rs->rs_obd_list,
1743 &obd->obd_uncommitted_replies);
1746 spin_unlock (&obd->obd_uncommitted_replies_lock);
1747 spin_lock (&exp->exp_lock);
1749 list_add_tail (&rs->rs_exp_list, &exp->exp_outstanding_replies);
1751 spin_unlock_irqrestore (&exp->exp_lock, flags);
1753 netrc = target_send_reply_msg (req, rc, fail_id);
1755 spin_lock_irqsave (&svc->srv_lock, flags);
1757 svc->srv_n_difficult_replies++;
1759 if (netrc != 0) /* error sending: reply is off the net */
1762 if (!rs->rs_on_net || /* some notifier */
1763 list_empty(&rs->rs_exp_list) || /* completed already */
1764 list_empty(&rs->rs_obd_list)) {
1765 list_add_tail (&rs->rs_list, &svc->srv_reply_queue);
1766 wake_up (&svc->srv_waitq);
1768 list_add (&rs->rs_list, &sni->sni_active_replies);
1769 rs->rs_scheduled = 0; /* allow notifier to schedule */
1772 spin_unlock_irqrestore (&svc->srv_lock, flags);
1775 int target_handle_ping(struct ptlrpc_request *req)
1777 return lustre_pack_reply(req, 0, NULL, NULL);