4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2010, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
33 * This file deals with various client/target related logic including recovery.
35 * TODO: This code more logically belongs in the ptlrpc module than in ldlm and
39 #define DEBUG_SUBSYSTEM S_LDLM
41 #include <cl_object.h>
42 #include <linux/jiffies.h>
43 #include <linux/kernel.h>
44 #include <linux/kthread.h>
45 #include <libcfs/libcfs.h>
47 #include <obd_class.h>
48 #include <lustre_dlm.h>
49 #include <lustre_net.h>
50 #include <lustre_sec.h>
51 #include "ldlm_internal.h"
54 * @priority: If non-zero, move the selected connection to the list head.
55 * @create: If zero, only search in existing connections.
57 static int import_set_conn(struct obd_import *imp, struct obd_uuid *uuid,
58 int priority, int create)
60 struct ptlrpc_connection *ptlrpc_conn;
61 struct obd_import_conn *imp_conn = NULL, *item;
62 lnet_nid_t nid4refnet = LNET_NID_ANY;
67 if (!create && !priority) {
68 CDEBUG(D_HA, "Nothing to do\n");
72 if (imp->imp_connection &&
73 imp->imp_connection->c_remote_uuid.uuid[0] == 0)
74 /* nid4refnet is used to restrict network connections */
75 nid4refnet = imp->imp_connection->c_self;
76 ptlrpc_conn = ptlrpc_uuid_to_connection(uuid, nid4refnet);
78 CDEBUG(D_HA, "can't find connection %s\n", uuid->uuid);
83 OBD_ALLOC(imp_conn, sizeof(*imp_conn));
85 GOTO(out_put, rc = -ENOMEM);
88 spin_lock(&imp->imp_lock);
89 list_for_each_entry(item, &imp->imp_conn_list, oic_item) {
90 if (obd_uuid_equals(uuid, &item->oic_uuid)) {
92 list_move(&item->oic_item,
94 item->oic_last_attempt = 0;
96 CDEBUG(D_HA, "imp %p@%s: found existing conn %s%s\n",
97 imp, imp->imp_obd->obd_name, uuid->uuid,
98 (priority ? ", moved to head" : ""));
99 spin_unlock(&imp->imp_lock);
100 GOTO(out_free, rc = 0);
103 /* No existing import connection found for \a uuid. */
105 imp_conn->oic_conn = ptlrpc_conn;
106 imp_conn->oic_uuid = *uuid;
107 imp_conn->oic_last_attempt = 0;
109 list_add(&imp_conn->oic_item, &imp->imp_conn_list);
111 list_add_tail(&imp_conn->oic_item,
112 &imp->imp_conn_list);
113 CDEBUG(D_HA, "imp %p@%s: add connection %s at %s\n",
114 imp, imp->imp_obd->obd_name, uuid->uuid,
115 (priority ? "head" : "tail"));
117 spin_unlock(&imp->imp_lock);
118 GOTO(out_free, rc = -ENOENT);
121 spin_unlock(&imp->imp_lock);
125 OBD_FREE(imp_conn, sizeof(*imp_conn));
127 ptlrpc_connection_put(ptlrpc_conn);
131 int import_set_conn_priority(struct obd_import *imp, struct obd_uuid *uuid)
133 return import_set_conn(imp, uuid, 1, 0);
136 int client_import_add_conn(struct obd_import *imp, struct obd_uuid *uuid,
139 return import_set_conn(imp, uuid, priority, 1);
141 EXPORT_SYMBOL(client_import_add_conn);
143 int client_import_dyn_add_conn(struct obd_import *imp, struct obd_uuid *uuid,
144 lnet_nid_t prim_nid, int priority)
146 struct ptlrpc_connection *ptlrpc_conn;
149 ptlrpc_conn = ptlrpc_uuid_to_connection(uuid, prim_nid);
151 const char *str_uuid = obd_uuid2str(uuid);
153 rc = class_add_uuid(str_uuid, prim_nid);
155 CERROR("%s: failed to add UUID '%s': rc = %d\n",
156 imp->imp_obd->obd_name, str_uuid, rc);
160 return import_set_conn(imp, uuid, priority, 1);
162 EXPORT_SYMBOL(client_import_dyn_add_conn);
164 int client_import_add_nids_to_conn(struct obd_import *imp, lnet_nid_t *nids,
165 int nid_count, struct obd_uuid *uuid)
167 struct obd_import_conn *conn;
171 if (nid_count <= 0 || !nids)
174 spin_lock(&imp->imp_lock);
175 list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
176 if (class_check_uuid(&conn->oic_uuid, nids[0])) {
177 *uuid = conn->oic_uuid;
178 rc = class_add_nids_to_uuid(&conn->oic_uuid, nids,
183 spin_unlock(&imp->imp_lock);
186 EXPORT_SYMBOL(client_import_add_nids_to_conn);
188 int client_import_del_conn(struct obd_import *imp, struct obd_uuid *uuid)
190 struct obd_import_conn *imp_conn;
191 struct obd_export *dlmexp;
196 spin_lock(&imp->imp_lock);
197 if (list_empty(&imp->imp_conn_list)) {
198 LASSERT(!imp->imp_connection);
202 list_for_each_entry(imp_conn, &imp->imp_conn_list, oic_item) {
203 if (!obd_uuid_equals(uuid, &imp_conn->oic_uuid))
205 LASSERT(imp_conn->oic_conn);
207 if (imp_conn == imp->imp_conn_current) {
208 LASSERT(imp_conn->oic_conn == imp->imp_connection);
210 if (imp->imp_state != LUSTRE_IMP_CLOSED &&
211 imp->imp_state != LUSTRE_IMP_DISCON) {
212 CERROR("can't remove current connection\n");
213 GOTO(out, rc = -EBUSY);
216 ptlrpc_connection_put(imp->imp_connection);
217 imp->imp_connection = NULL;
219 dlmexp = class_conn2export(&imp->imp_dlm_handle);
220 if (dlmexp && dlmexp->exp_connection) {
221 LASSERT(dlmexp->exp_connection ==
223 ptlrpc_connection_put(dlmexp->exp_connection);
224 dlmexp->exp_connection = NULL;
228 class_export_put(dlmexp);
231 list_del(&imp_conn->oic_item);
232 ptlrpc_connection_put(imp_conn->oic_conn);
233 OBD_FREE(imp_conn, sizeof(*imp_conn));
234 CDEBUG(D_HA, "imp %p@%s: remove connection %s\n",
235 imp, imp->imp_obd->obd_name, uuid->uuid);
240 spin_unlock(&imp->imp_lock);
242 CERROR("connection %s not found\n", uuid->uuid);
245 EXPORT_SYMBOL(client_import_del_conn);
248 * Find conn UUID by peer NID. \a peer is a server NID. This function is used
249 * to find a conn uuid of \a imp which can reach \a peer.
251 int client_import_find_conn(struct obd_import *imp, lnet_nid_t peer,
252 struct obd_uuid *uuid)
254 struct obd_import_conn *conn;
259 spin_lock(&imp->imp_lock);
260 list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
261 /* Check if conn UUID does have this peer NID. */
262 if (class_check_uuid(&conn->oic_uuid, peer)) {
263 *uuid = conn->oic_uuid;
268 spin_unlock(&imp->imp_lock);
271 EXPORT_SYMBOL(client_import_find_conn);
273 void client_destroy_import(struct obd_import *imp)
276 * Drop security policy instance after all RPCs have finished/aborted
277 * to let all busy contexts be released.
279 class_import_get(imp);
280 class_destroy_import(imp);
281 sptlrpc_import_sec_put(imp);
282 class_import_put(imp);
284 EXPORT_SYMBOL(client_destroy_import);
287 * Check whether or not the OSC is on MDT.
290 * setup 0:{fsname}-OSTxxxx-osc[-MDTxxxx] 1:lustre-OST0000_UUID 2:NID
292 * setup 0:{fsname}-OSTxxxx-osc 1:lustre-OST0000_UUID 2:NID
295 static int osc_on_mdt(char *obdname)
299 ptr = strrchr(obdname, '-');
303 if (strncmp(ptr + 1, "MDT", 3) == 0)
310 * Configure an RPC client OBD device.
315 * 3 - inactive-on-startup
316 * 4 - restrictive net
318 int client_obd_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
320 struct client_obd *cli = &obd->u.cli;
321 struct obd_import *imp;
322 struct obd_uuid server_uuid;
323 int rq_portal, rp_portal, connect_op;
324 const char *name = obd->obd_type->typ_name;
325 enum ldlm_ns_type ns_type = LDLM_NS_TYPE_UNKNOWN;
326 char *cli_name = lustre_cfg_buf(lcfg, 0);
327 struct ptlrpc_connection fake_conn = { .c_self = 0,
328 .c_remote_uuid.uuid[0] = 0 };
334 * In a more perfect world, we would hang a ptlrpc_client off of
335 * obd_type and just use the values from there.
337 if (!strcmp(name, LUSTRE_OSC_NAME)) {
338 rq_portal = OST_REQUEST_PORTAL;
339 rp_portal = OSC_REPLY_PORTAL;
340 connect_op = OST_CONNECT;
341 cli->cl_sp_me = LUSTRE_SP_CLI;
342 cli->cl_sp_to = LUSTRE_SP_OST;
343 ns_type = LDLM_NS_TYPE_OSC;
344 } else if (!strcmp(name, LUSTRE_MDC_NAME) ||
345 !strcmp(name, LUSTRE_LWP_NAME)) {
346 rq_portal = MDS_REQUEST_PORTAL;
347 rp_portal = MDC_REPLY_PORTAL;
348 connect_op = MDS_CONNECT;
349 if (is_lwp_on_ost(cli_name))
350 cli->cl_sp_me = LUSTRE_SP_OST;
351 else if (is_lwp_on_mdt(cli_name))
352 cli->cl_sp_me = LUSTRE_SP_MDT;
354 cli->cl_sp_me = LUSTRE_SP_CLI;
355 cli->cl_sp_to = LUSTRE_SP_MDT;
356 ns_type = LDLM_NS_TYPE_MDC;
357 } else if (!strcmp(name, LUSTRE_OSP_NAME)) {
358 if (strstr(lustre_cfg_buf(lcfg, 1), "OST") == NULL) {
359 /* OSP_on_MDT for other MDTs */
360 connect_op = MDS_CONNECT;
361 cli->cl_sp_to = LUSTRE_SP_MDT;
362 ns_type = LDLM_NS_TYPE_MDC;
363 rq_portal = OUT_PORTAL;
365 /* OSP on MDT for OST */
366 connect_op = OST_CONNECT;
367 cli->cl_sp_to = LUSTRE_SP_OST;
368 ns_type = LDLM_NS_TYPE_OSC;
369 rq_portal = OST_REQUEST_PORTAL;
371 rp_portal = OSC_REPLY_PORTAL;
372 cli->cl_sp_me = LUSTRE_SP_MDT;
373 } else if (!strcmp(name, LUSTRE_MGC_NAME)) {
374 rq_portal = MGS_REQUEST_PORTAL;
375 rp_portal = MGC_REPLY_PORTAL;
376 connect_op = MGS_CONNECT;
377 cli->cl_sp_me = LUSTRE_SP_MGC;
378 cli->cl_sp_to = LUSTRE_SP_MGS;
379 cli->cl_flvr_mgc.sf_rpc = SPTLRPC_FLVR_INVALID;
380 ns_type = LDLM_NS_TYPE_MGC;
382 CERROR("unknown client OBD type \"%s\", can't setup\n",
387 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
388 CERROR("requires a TARGET UUID\n");
392 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > 37) {
393 CERROR("client UUID must be less than 38 characters\n");
397 if (LUSTRE_CFG_BUFLEN(lcfg, 2) < 1) {
398 CERROR("setup requires a SERVER UUID\n");
402 if (LUSTRE_CFG_BUFLEN(lcfg, 2) > 37) {
403 CERROR("target UUID must be less than 38 characters\n");
407 init_rwsem(&cli->cl_sem);
408 mutex_init(&cli->cl_mgc_mutex);
410 init_rwsem(&cli->cl_seq_rwsem);
411 cli->cl_conn_count = 0;
412 memcpy(server_uuid.uuid, lustre_cfg_buf(lcfg, 2),
413 min_t(unsigned int, LUSTRE_CFG_BUFLEN(lcfg, 2),
414 sizeof(server_uuid)));
416 cli->cl_dirty_pages = 0;
417 cli->cl_dirty_max_pages = 0;
418 cli->cl_avail_grant = 0;
419 /* FIXME: Should limit this for the sum of all cl_dirty_max_pages. */
421 * cl_dirty_max_pages may be changed at connect time in
422 * ptlrpc_connect_interpret().
424 client_adjust_max_dirty(cli);
425 init_waitqueue_head(&cli->cl_cache_waiters);
426 INIT_LIST_HEAD(&cli->cl_loi_ready_list);
427 INIT_LIST_HEAD(&cli->cl_loi_hp_ready_list);
428 INIT_LIST_HEAD(&cli->cl_loi_write_list);
429 INIT_LIST_HEAD(&cli->cl_loi_read_list);
430 spin_lock_init(&cli->cl_loi_list_lock);
431 atomic_set(&cli->cl_pending_w_pages, 0);
432 atomic_set(&cli->cl_pending_r_pages, 0);
433 cli->cl_r_in_flight = 0;
434 cli->cl_w_in_flight = 0;
436 spin_lock_init(&cli->cl_read_rpc_hist.oh_lock);
437 spin_lock_init(&cli->cl_write_rpc_hist.oh_lock);
438 spin_lock_init(&cli->cl_read_page_hist.oh_lock);
439 spin_lock_init(&cli->cl_write_page_hist.oh_lock);
440 spin_lock_init(&cli->cl_read_offset_hist.oh_lock);
441 spin_lock_init(&cli->cl_write_offset_hist.oh_lock);
444 INIT_LIST_HEAD(&cli->cl_lru_osc);
445 atomic_set(&cli->cl_lru_shrinkers, 0);
446 atomic_long_set(&cli->cl_lru_busy, 0);
447 atomic_long_set(&cli->cl_lru_in_list, 0);
448 INIT_LIST_HEAD(&cli->cl_lru_list);
449 spin_lock_init(&cli->cl_lru_list_lock);
450 atomic_long_set(&cli->cl_unstable_count, 0);
451 INIT_LIST_HEAD(&cli->cl_shrink_list);
452 INIT_LIST_HEAD(&cli->cl_grant_chain);
454 INIT_LIST_HEAD(&cli->cl_flight_waiters);
455 cli->cl_rpcs_in_flight = 0;
457 init_waitqueue_head(&cli->cl_destroy_waitq);
458 atomic_set(&cli->cl_destroy_in_flight, 0);
461 cli->cl_supp_cksum_types = OBD_CKSUM_CRC32;
462 cli->cl_preferred_cksum_type = 0;
463 #ifdef ENABLE_CHECKSUM
464 /* Turn on checksumming by default. */
465 cli->cl_checksum = 1;
467 * The supported checksum types will be worked out at connect time
468 * Set cl_chksum* to CRC32 for now to avoid returning screwed info
471 cli->cl_cksum_type = cli->cl_supp_cksum_types;
473 atomic_set(&cli->cl_resends, OSC_DEFAULT_RESENDS);
476 * Set it to possible maximum size. It may be reduced by ocd_brw_size
477 * from OFD after connecting.
479 cli->cl_max_pages_per_rpc = PTLRPC_MAX_BRW_PAGES;
481 cli->cl_max_short_io_bytes = OBD_DEF_SHORT_IO_BYTES;
484 * set cl_chunkbits default value to PAGE_SHIFT,
485 * it will be updated at OSC connection time.
487 cli->cl_chunkbits = PAGE_SHIFT;
489 if (!strcmp(name, LUSTRE_MDC_NAME)) {
490 cli->cl_max_rpcs_in_flight = OBD_MAX_RIF_DEFAULT;
491 } else if (cfs_totalram_pages() >> (20 - PAGE_SHIFT) <= 128 /* MB */) {
492 cli->cl_max_rpcs_in_flight = 2;
493 } else if (cfs_totalram_pages() >> (20 - PAGE_SHIFT) <= 256 /* MB */) {
494 cli->cl_max_rpcs_in_flight = 3;
495 } else if (cfs_totalram_pages() >> (20 - PAGE_SHIFT) <= 512 /* MB */) {
496 cli->cl_max_rpcs_in_flight = 4;
498 if (osc_on_mdt(obd->obd_name))
499 cli->cl_max_rpcs_in_flight = OBD_MAX_RIF_MAX;
501 cli->cl_max_rpcs_in_flight = OBD_MAX_RIF_DEFAULT;
504 spin_lock_init(&cli->cl_mod_rpcs_lock);
505 spin_lock_init(&cli->cl_mod_rpcs_hist.oh_lock);
506 cli->cl_max_mod_rpcs_in_flight = 0;
507 cli->cl_mod_rpcs_in_flight = 0;
508 cli->cl_close_rpcs_in_flight = 0;
509 init_waitqueue_head(&cli->cl_mod_rpcs_waitq);
510 cli->cl_mod_tag_bitmap = NULL;
512 INIT_LIST_HEAD(&cli->cl_chg_dev_linkage);
514 if (connect_op == MDS_CONNECT) {
515 cli->cl_max_mod_rpcs_in_flight = cli->cl_max_rpcs_in_flight - 1;
516 OBD_ALLOC(cli->cl_mod_tag_bitmap,
517 BITS_TO_LONGS(OBD_MAX_RIF_MAX) * sizeof(long));
518 if (cli->cl_mod_tag_bitmap == NULL)
519 GOTO(err, rc = -ENOMEM);
524 CERROR("ldlm_get_ref failed: %d\n", rc);
528 ptlrpc_init_client(rq_portal, rp_portal, name,
529 &obd->obd_ldlm_client);
531 imp = class_new_import(obd);
533 GOTO(err_ldlm, rc = -ENOENT);
534 imp->imp_client = &obd->obd_ldlm_client;
535 imp->imp_connect_op = connect_op;
536 memcpy(cli->cl_target_uuid.uuid, lustre_cfg_buf(lcfg, 1),
537 LUSTRE_CFG_BUFLEN(lcfg, 1));
538 class_import_put(imp);
540 if (lustre_cfg_buf(lcfg, 4)) {
541 __u32 refnet = libcfs_str2net(lustre_cfg_string(lcfg, 4));
543 if (refnet == LNET_NET_ANY) {
545 CERROR("%s: bad mount option 'network=%s': rc = %d\n",
546 obd->obd_name, lustre_cfg_string(lcfg, 4),
548 GOTO(err_import, rc);
550 fake_conn.c_self = LNET_MKNID(refnet, 0);
551 imp->imp_connection = &fake_conn;
554 rc = client_import_add_conn(imp, &server_uuid, 1);
556 CERROR("can't add initial connection\n");
557 GOTO(err_import, rc);
559 imp->imp_connection = NULL;
561 cli->cl_import = imp;
562 /* cli->cl_max_mds_easize updated by mdc_init_ea_size() */
563 cli->cl_max_mds_easize = sizeof(struct lov_mds_md_v3);
565 if (LUSTRE_CFG_BUFLEN(lcfg, 3) > 0) {
566 if (!strcmp(lustre_cfg_string(lcfg, 3), "inactive")) {
567 CDEBUG(D_HA, "marking %s %s->%s as inactive\n",
569 cli->cl_target_uuid.uuid);
570 spin_lock(&imp->imp_lock);
571 imp->imp_deactive = 1;
572 spin_unlock(&imp->imp_lock);
576 obd->obd_namespace = ldlm_namespace_new(obd, obd->obd_name,
577 LDLM_NAMESPACE_CLIENT,
578 LDLM_NAMESPACE_GREEDY,
580 if (IS_ERR(obd->obd_namespace)) {
581 rc = PTR_ERR(obd->obd_namespace);
582 CERROR("%s: unable to create client namespace: rc = %d\n",
584 obd->obd_namespace = NULL;
585 GOTO(err_import, rc);
591 class_destroy_import(imp);
595 if (cli->cl_mod_tag_bitmap != NULL)
596 OBD_FREE(cli->cl_mod_tag_bitmap,
597 BITS_TO_LONGS(OBD_MAX_RIF_MAX) * sizeof(long));
598 cli->cl_mod_tag_bitmap = NULL;
602 EXPORT_SYMBOL(client_obd_setup);
604 int client_obd_cleanup(struct obd_device *obd)
606 struct client_obd *cli = &obd->u.cli;
610 ldlm_namespace_free_post(obd->obd_namespace);
611 obd->obd_namespace = NULL;
613 obd_cleanup_client_import(obd);
614 LASSERT(obd->u.cli.cl_import == NULL);
618 if (cli->cl_mod_tag_bitmap != NULL)
619 OBD_FREE(cli->cl_mod_tag_bitmap,
620 BITS_TO_LONGS(OBD_MAX_RIF_MAX) * sizeof(long));
621 cli->cl_mod_tag_bitmap = NULL;
625 EXPORT_SYMBOL(client_obd_cleanup);
627 /* ->o_connect() method for client side (OSC and MDC and MGC) */
628 int client_connect_import(const struct lu_env *env,
629 struct obd_export **exp,
630 struct obd_device *obd, struct obd_uuid *cluuid,
631 struct obd_connect_data *data, void *localdata)
633 struct client_obd *cli = &obd->u.cli;
634 struct obd_import *imp = cli->cl_import;
635 struct obd_connect_data *ocd;
636 struct lustre_handle conn = { 0 };
642 down_write(&cli->cl_sem);
643 if (cli->cl_conn_count > 0)
644 GOTO(out_sem, rc = -EALREADY);
646 rc = class_connect(&conn, obd, cluuid);
650 cli->cl_conn_count++;
651 *exp = class_conn2export(&conn);
653 LASSERT(obd->obd_namespace);
655 imp->imp_dlm_handle = conn;
656 rc = ptlrpc_init_import(imp);
660 ocd = &imp->imp_connect_data;
663 imp->imp_connect_flags_orig = data->ocd_connect_flags;
664 imp->imp_connect_flags2_orig = data->ocd_connect_flags2;
667 rc = ptlrpc_connect_import(imp);
669 LASSERT(imp->imp_state == LUSTRE_IMP_DISCON);
672 LASSERT(*exp != NULL && (*exp)->exp_connection);
675 LASSERTF((ocd->ocd_connect_flags & data->ocd_connect_flags) ==
676 ocd->ocd_connect_flags, "old %#llx, new %#llx\n",
677 data->ocd_connect_flags, ocd->ocd_connect_flags);
678 data->ocd_connect_flags = ocd->ocd_connect_flags;
679 data->ocd_connect_flags2 = ocd->ocd_connect_flags2;
682 ptlrpc_pinger_add_import(imp);
688 cli->cl_conn_count--;
689 class_disconnect(*exp);
693 up_write(&cli->cl_sem);
695 if (!rc && localdata) {
696 LASSERT(cli->cl_cache == NULL); /* only once */
697 cli->cl_cache = (struct cl_client_cache *)localdata;
698 cl_cache_incref(cli->cl_cache);
699 cli->cl_lru_left = &cli->cl_cache->ccc_lru_left;
701 /* add this osc into entity list */
702 LASSERT(list_empty(&cli->cl_lru_osc));
703 spin_lock(&cli->cl_cache->ccc_lru_lock);
704 list_add(&cli->cl_lru_osc, &cli->cl_cache->ccc_lru);
705 spin_unlock(&cli->cl_cache->ccc_lru_lock);
710 EXPORT_SYMBOL(client_connect_import);
712 int client_disconnect_export(struct obd_export *exp)
714 struct obd_device *obd = class_exp2obd(exp);
715 struct client_obd *cli;
716 struct obd_import *imp;
722 CERROR("invalid export for disconnect: exp %p cookie %#llx\n",
723 exp, exp ? exp->exp_handle.h_cookie : -1);
728 imp = cli->cl_import;
730 down_write(&cli->cl_sem);
731 CDEBUG(D_INFO, "disconnect %s - %zu\n", obd->obd_name,
734 if (cli->cl_conn_count == 0) {
735 CERROR("disconnecting disconnected device (%s)\n",
737 GOTO(out_disconnect, rc = -EINVAL);
740 cli->cl_conn_count--;
741 if (cli->cl_conn_count != 0)
742 GOTO(out_disconnect, rc = 0);
745 * Mark import deactivated now, so we don't try to reconnect if any
746 * of the cleanup RPCs fails (e.g. LDLM cancel, etc). We don't
747 * fully deactivate the import, or that would drop all requests.
749 spin_lock(&imp->imp_lock);
750 imp->imp_deactive = 1;
751 spin_unlock(&imp->imp_lock);
754 * Some non-replayable imports (MDS's OSCs) are pinged, so just
755 * delete it regardless. (It's safe to delete an import that was
758 (void)ptlrpc_pinger_del_import(imp);
760 if (obd->obd_namespace != NULL) {
761 /* obd_force == local only */
762 ldlm_cli_cancel_unused(obd->obd_namespace, NULL,
763 obd->obd_force ? LCF_LOCAL : 0, NULL);
764 ldlm_namespace_free_prior(obd->obd_namespace, imp,
769 * There's no need to hold sem while disconnecting an import,
770 * and it may actually cause deadlock in GSS.
772 up_write(&cli->cl_sem);
773 rc = ptlrpc_disconnect_import(imp, 0);
774 down_write(&cli->cl_sem);
776 ptlrpc_invalidate_import(imp);
782 * Use server style - class_disconnect should be always called for
785 err = class_disconnect(exp);
789 up_write(&cli->cl_sem);
793 EXPORT_SYMBOL(client_disconnect_export);
795 #ifdef HAVE_SERVER_SUPPORT
796 int server_disconnect_export(struct obd_export *exp)
802 /* Disconnect early so that clients can't keep using export. */
803 rc = class_disconnect(exp);
804 /* Close import to avoid sending any requests. */
805 if (exp->exp_imp_reverse)
806 ptlrpc_cleanup_imp(exp->exp_imp_reverse);
808 ldlm_bl_thread_wakeup();
810 /* complete all outstanding replies */
811 spin_lock(&exp->exp_lock);
812 while (!list_empty(&exp->exp_outstanding_replies)) {
813 struct ptlrpc_reply_state *rs =
814 list_entry(exp->exp_outstanding_replies.next,
815 struct ptlrpc_reply_state, rs_exp_list);
816 struct ptlrpc_service_part *svcpt = rs->rs_svcpt;
818 spin_lock(&svcpt->scp_rep_lock);
820 list_del_init(&rs->rs_exp_list);
822 spin_lock(&rs->rs_lock);
823 /* clear rs_convert_lock to make sure rs is handled and put */
824 rs->rs_convert_lock = 0;
825 ptlrpc_schedule_difficult_reply(rs);
826 spin_unlock(&rs->rs_lock);
828 spin_unlock(&svcpt->scp_rep_lock);
830 spin_unlock(&exp->exp_lock);
834 EXPORT_SYMBOL(server_disconnect_export);
836 static inline int target_check_recovery_timer(struct obd_device *target)
841 if (!target->obd_recovering || target->obd_recovery_start == 0)
844 remaining = hrtimer_get_remaining(&target->obd_recovery_timer);
845 timeout = ktime_divns(remaining, NSEC_PER_SEC);
849 /* the recovery timer should expire, but it isn't triggered,
850 * it's better to abort the recovery of this target to speed up
851 * the recovery of the whole cluster. */
852 spin_lock(&target->obd_dev_lock);
853 if (target->obd_recovering) {
854 CERROR("%s: Aborting recovery\n", target->obd_name);
855 target->obd_abort_recovery = 1;
856 wake_up(&target->obd_next_transno_waitq);
858 spin_unlock(&target->obd_dev_lock);
863 * --------------------------------------------------------------------------
864 * from old lib/target.c
865 * --------------------------------------------------------------------------
867 static int target_handle_reconnect(struct lustre_handle *conn,
868 struct obd_export *exp,
869 struct obd_uuid *cluuid)
871 struct obd_device *target;
872 struct lustre_handle *hdl;
878 hdl = &exp->exp_imp_reverse->imp_remote_handle;
879 if (!exp->exp_connection || !lustre_handle_is_used(hdl)) {
880 conn->cookie = exp->exp_handle.h_cookie;
882 "connect export for UUID '%s' at %p, cookie %#llx\n",
883 cluuid->uuid, exp, conn->cookie);
887 target = exp->exp_obd;
889 /* Might be a re-connect after a partition. */
890 if (memcmp(&conn->cookie, &hdl->cookie, sizeof(conn->cookie))) {
891 LCONSOLE_WARN("%s: already connected client %s (at %s) with handle %#llx. Rejecting client with the same UUID trying to reconnect with handle %#llx\n",
893 obd_uuid2str(&exp->exp_client_uuid),
894 obd_export_nid2str(exp),
895 hdl->cookie, conn->cookie);
896 memset(conn, 0, sizeof(*conn));
898 * target_handle_connect() treats EALREADY and
899 * -EALREADY differently. -EALREADY is an error
900 * (same UUID, different handle).
905 if (!target->obd_recovering) {
906 LCONSOLE_WARN("%s: Client %s (at %s) reconnecting\n",
907 target->obd_name, obd_uuid2str(&exp->exp_client_uuid),
908 obd_export_nid2str(exp));
909 GOTO(out_already, rc);
912 remaining = hrtimer_get_remaining(&target->obd_recovery_timer);
913 timeout = ktime_divns(remaining, NSEC_PER_SEC);
915 LCONSOLE_WARN("%s: Client %s (at %s) reconnected, waiting for %d clients in recovery for %lld:%.02lld\n",
917 obd_uuid2str(&exp->exp_client_uuid),
918 obd_export_nid2str(exp),
919 atomic_read(&target->obd_max_recoverable_clients),
920 timeout / 60, timeout % 60);
922 struct target_distribute_txn_data *tdtd;
927 target_check_recovery_timer(target);
929 tdtd = class_exp2tgt(exp)->lut_tdtd;
930 if (tdtd && tdtd->tdtd_show_update_logs_retrievers)
931 buf = tdtd->tdtd_show_update_logs_retrievers(
932 tdtd->tdtd_show_retrievers_cbdata,
936 LCONSOLE_WARN("%s: Client %s (at %s) reconnecting, waiting for %d MDTs (%s) in recovery for %lld:%.02lld. Please wait until all MDTs recovered or you may force MDT evicition via 'lctl --device %s abort_recovery.\n",
938 obd_uuid2str(&exp->exp_client_uuid),
939 obd_export_nid2str(exp), count,
940 buf ? buf : "unknown (not enough RAM)",
941 (abs(timeout) + target->obd_recovery_timeout) / 60,
942 (abs(timeout) + target->obd_recovery_timeout) % 60,
945 LCONSOLE_WARN("%s: Recovery already passed deadline %lld:%.02lld. If you do not want to wait more, you may force taget eviction via 'lctl --device %s abort_recovery.\n",
946 target->obd_name, abs(timeout) / 60,
947 abs(timeout) % 60, target->obd_name);
954 conn->cookie = exp->exp_handle.h_cookie;
956 * target_handle_connect() treats EALREADY and
957 * -EALREADY differently. EALREADY means we are
958 * doing a valid reconnect from the same client.
964 check_and_start_recovery_timer(struct obd_device *obd,
965 struct ptlrpc_request *req, int new_client);
968 * update flags for import during reconnect process
970 static int rev_import_flags_update(struct obd_import *revimp,
971 struct ptlrpc_request *req)
974 struct obd_connect_data *data;
976 data = req_capsule_client_get(&req->rq_pill, &RMF_CONNECT_DATA);
978 if (data->ocd_connect_flags & OBD_CONNECT_AT)
979 revimp->imp_msghdr_flags |= MSGHDR_AT_SUPPORT;
981 revimp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
983 revimp->imp_msghdr_flags |= MSGHDR_CKSUM_INCOMPAT18;
985 revimp->imp_connect_data = *data;
986 rc = sptlrpc_import_sec_adapt(revimp, req->rq_svc_ctx, &req->rq_flvr);
988 CERROR("%s: cannot get reverse import %s security: rc = %d\n",
989 revimp->imp_client->cli_name,
990 libcfs_id2str(req->rq_peer), rc);
998 * Allocate a new reverse import for an export.
1000 * \retval -errno in case error hit
1001 * \retval 0 if reverse import correctly init
1003 int rev_import_init(struct obd_export *export)
1005 struct obd_device *obd = export->exp_obd;
1006 struct obd_import *revimp;
1008 LASSERT(export->exp_imp_reverse == NULL);
1010 revimp = class_new_import(obd);
1014 revimp->imp_remote_handle.cookie = 0ULL;
1015 revimp->imp_client = &obd->obd_ldlm_client;
1016 revimp->imp_dlm_fake = 1;
1018 /* it is safe to connect import in new state as no sends possible */
1019 spin_lock(&export->exp_lock);
1020 export->exp_imp_reverse = revimp;
1021 spin_unlock(&export->exp_lock);
1022 class_import_put(revimp);
1026 EXPORT_SYMBOL(rev_import_init);
1029 * Handle reconnect for an export.
1031 * \param exp export to handle reconnect process
1032 * \param req client reconnect request
1034 * \retval -rc in case securitfy flavor can't be changed
1035 * \retval 0 in case none problems
1037 static int rev_import_reconnect(struct obd_export *exp,
1038 struct ptlrpc_request *req)
1040 struct obd_import *revimp = exp->exp_imp_reverse;
1041 struct lustre_handle *lh;
1044 /* avoid sending a request until import flags are changed */
1045 ptlrpc_import_enter_resend(revimp);
1047 ptlrpc_connection_put(revimp->imp_connection);
1050 * client from recovery don't have a handle so we need to take from
1051 * request. it may produce situation when wrong client connected
1052 * to recovery as we trust a client uuid
1054 lh = req_capsule_client_get(&req->rq_pill, &RMF_CONN);
1055 revimp->imp_remote_handle = *lh;
1058 * unknown versions will be caught in
1059 * ptlrpc_handle_server_req_in->lustre_unpack_msg()
1061 revimp->imp_msg_magic = req->rq_reqmsg->lm_magic;
1063 revimp->imp_connection = ptlrpc_connection_addref(exp->exp_connection);
1065 rc = rev_import_flags_update(revimp, req);
1068 * it is safe to still be in RECOVERY phase as we are not able
1069 * to setup correct security flavor so requests are not able to
1070 * be delivered correctly
1075 /* resend all rpc's via new connection */
1076 return ptlrpc_import_recovery_state_machine(revimp);
1079 int target_handle_connect(struct ptlrpc_request *req)
1081 struct obd_device *target = NULL;
1082 struct obd_export *export = NULL;
1084 * connect handle - filled from target_handle_reconnect in
1087 struct lustre_handle conn;
1088 struct lustre_handle *tmp;
1089 struct obd_uuid cluuid;
1094 bool mds_conn = false, lw_client = false, initial_conn = false;
1095 bool mds_mds_conn = false;
1096 bool new_mds_mds_conn = false;
1097 struct obd_connect_data *data, *tmpdata;
1099 lnet_nid_t *client_nid = NULL;
1100 struct ptlrpc_connection *pcon = NULL;
1104 OBD_RACE(OBD_FAIL_TGT_CONN_RACE);
1106 str = req_capsule_client_get(&req->rq_pill, &RMF_TGTUUID);
1108 DEBUG_REQ(D_ERROR, req, "bad target UUID for connect");
1109 GOTO(out, rc = -EINVAL);
1112 target = class_dev_by_str(str);
1114 deuuidify(str, NULL, &target_start, &target_len);
1115 LCONSOLE_ERROR_MSG(0x137,
1116 "%s: not available for connect from %s (no target). If you are running an HA pair check that the target is mounted on the other server.\n",
1117 str, libcfs_nid2str(req->rq_peer.nid));
1118 GOTO(out, rc = -ENODEV);
1121 spin_lock(&target->obd_dev_lock);
1123 target->obd_conn_inprogress++;
1125 if (target->obd_stopping || !target->obd_set_up) {
1126 spin_unlock(&target->obd_dev_lock);
1128 deuuidify(str, NULL, &target_start, &target_len);
1129 LCONSOLE_INFO("%.*s: Not available for connect from %s (%s)\n",
1130 target_len, target_start,
1131 libcfs_nid2str(req->rq_peer.nid),
1132 (target->obd_stopping ?
1133 "stopping" : "not set up"));
1134 GOTO(out, rc = -ENODEV);
1137 if (target->obd_no_conn) {
1138 spin_unlock(&target->obd_dev_lock);
1141 "%s: Temporarily refusing client connection from %s\n",
1142 target->obd_name, libcfs_nid2str(req->rq_peer.nid));
1143 GOTO(out, rc = -EAGAIN);
1146 spin_unlock(&target->obd_dev_lock);
1148 str = req_capsule_client_get(&req->rq_pill, &RMF_CLUUID);
1150 DEBUG_REQ(D_ERROR, req, "bad client UUID for connect");
1151 GOTO(out, rc = -EINVAL);
1154 obd_str2uuid(&cluuid, str);
1156 tmp = req_capsule_client_get(&req->rq_pill, &RMF_CONN);
1158 GOTO(out, rc = -EPROTO);
1162 size = req_capsule_get_size(&req->rq_pill, &RMF_CONNECT_DATA,
1164 if (size < 0 || size > 8 * sizeof(struct obd_connect_data))
1165 GOTO(out, rc = -EPROTO);
1166 data = req_capsule_client_get(&req->rq_pill, &RMF_CONNECT_DATA);
1168 GOTO(out, rc = -EPROTO);
1170 rc = req_capsule_server_pack(&req->rq_pill);
1174 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
1176 * Don't allow clients to connect that are using old 1.8 format
1177 * protocol conventions (LUSTRE_MSG_MAGIC_v1, !MSGHDR_CKSUM_INCOMPAT18,
1178 * ldlm_flock_policy_wire format, MDT_ATTR_xTIME_SET, etc). The
1179 * FULL20 flag should be set on all connections since 2.0, but no
1180 * longer affects behaviour.
1182 * Later this check will be disabled and the flag can be retired
1183 * completely once interop with 3.0 is no longer needed.
1185 if (!(data->ocd_connect_flags & OBD_CONNECT_FULL20))
1186 GOTO(out, rc = -EPROTO);
1189 * Don't allow liblustre clients to connect.
1190 * - testing was disabled in v2_2_50_0-61-g6a75d65
1191 * - building was disabled in v2_5_58_0-28-g7277179
1192 * - client code was deleted in v2_6_50_0-101-gcdfbc72,
1193 * - clients were refused connect for version difference > 0.0.1.32
1195 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT) {
1196 DEBUG_REQ(D_WARNING, req, "Refusing libclient connection");
1197 GOTO(out, rc = -EPROTO);
1202 * Note: lw_client is needed in MDS-MDS failover during update log
1203 * processing, so we needs to allow lw_client to be connected at
1204 * anytime, instead of only the initial connection
1206 lw_client = (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) != 0;
1208 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_INITIAL) {
1209 initial_conn = true;
1210 mds_conn = (data->ocd_connect_flags & OBD_CONNECT_MDS) != 0;
1211 mds_mds_conn = (data->ocd_connect_flags &
1212 OBD_CONNECT_MDS_MDS) != 0;
1215 * OBD_CONNECT_MNE_SWAB is removed at 2.14
1216 * Checking OBD_CONNECT_FID can be removed in the future.
1218 * Via check OBD_CONNECT_FID, we can distinguish whether
1219 * the OBD_CONNECT_MDS_MDS/OBD_CONNECT_MNE_SWAB is from
1220 * MGC or MDT, since MGC does not use OBD_CONNECT_FID.
1223 (data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
1224 (data->ocd_connect_flags & OBD_CONNECT_FID) &&
1225 (data->ocd_connect_flags & OBD_CONNECT_VERSION)) {
1226 __u32 major = OBD_OCD_VERSION_MAJOR(data->ocd_version);
1227 __u32 minor = OBD_OCD_VERSION_MINOR(data->ocd_version);
1228 __u32 patch = OBD_OCD_VERSION_PATCH(data->ocd_version);
1231 * We do not support the MDT-MDT interoperations with
1232 * different version MDT because of protocol changes.
1234 if (unlikely(major != LUSTRE_MAJOR ||
1235 minor != LUSTRE_MINOR ||
1236 abs(patch - LUSTRE_PATCH) > 3)) {
1237 LCONSOLE_WARN("%s (%u.%u.%u.%u) refused the connection from different version MDT (%d.%d.%d.%d) %s %s\n",
1238 target->obd_name, LUSTRE_MAJOR,
1239 LUSTRE_MINOR, LUSTRE_PATCH,
1240 LUSTRE_FIX, major, minor, patch,
1241 OBD_OCD_VERSION_FIX(data->ocd_version),
1242 libcfs_nid2str(req->rq_peer.nid),
1244 GOTO(out, rc = -EPROTO);
1249 /* lctl gets a backstage, all-access pass. */
1250 if (obd_uuid_equals(&cluuid, &target->obd_uuid))
1251 goto dont_check_exports;
1253 export = obd_uuid_lookup(target, &cluuid);
1257 /* We've found an export in the hash. */
1259 spin_lock(&export->exp_lock);
1261 if (export->exp_connecting) { /* b=9635, et. al. */
1262 spin_unlock(&export->exp_lock);
1263 LCONSOLE_WARN("%s: Export %p already connecting from %s\n",
1264 export->exp_obd->obd_name, export,
1265 libcfs_nid2str(req->rq_peer.nid));
1266 class_export_put(export);
1269 } else if ((mds_conn || (lw_client && initial_conn) ||
1270 data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
1271 export->exp_connection != NULL) {
1272 spin_unlock(&export->exp_lock);
1273 if (req->rq_peer.nid != export->exp_connection->c_peer.nid) {
1274 /* MDS or LWP reconnected after failover. */
1275 LCONSOLE_WARN("%s: Received %s connection from %s, removing former export from %s\n",
1277 mds_conn ? "MDS" : "LWP",
1278 libcfs_nid2str(req->rq_peer.nid),
1279 libcfs_nid2str(export->exp_connection->c_peer.nid));
1281 /* New MDS connection from the same NID. */
1282 LCONSOLE_WARN("%s: Received new %s connection from %s, removing former export from same NID\n",
1284 mds_conn ? "MDS" : "LWP",
1285 libcfs_nid2str(req->rq_peer.nid));
1288 if (req->rq_peer.nid == export->exp_connection->c_peer.nid &&
1289 data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) {
1291 * Because exports between MDTs will always be
1292 * kept, let's do not fail such export if they
1293 * come from the same NID, otherwise it might
1294 * cause eviction between MDTs, which might
1295 * cause namespace inconsistency
1297 spin_lock(&export->exp_lock);
1298 export->exp_connecting = 1;
1299 export->exp_conn_cnt = 0;
1300 spin_unlock(&export->exp_lock);
1301 conn.cookie = export->exp_handle.h_cookie;
1304 class_fail_export(export);
1305 class_export_put(export);
1309 } else if (export->exp_connection != NULL && initial_conn &&
1310 req->rq_peer.nid != export->exp_connection->c_peer.nid) {
1311 spin_unlock(&export->exp_lock);
1312 /* In MDS failover we have static UUID but NID can change. */
1313 LCONSOLE_WARN("%s: Client %s seen on new nid %s when existing nid %s is already connected\n",
1314 target->obd_name, cluuid.uuid,
1315 libcfs_nid2str(req->rq_peer.nid),
1317 export->exp_connection->c_peer.nid));
1319 class_export_put(export);
1321 } else if (OBD_FAIL_PRECHECK(OBD_FAIL_TGT_RECOVERY_CONNECT) &&
1323 spin_unlock(&export->exp_lock);
1326 export->exp_connecting = 1;
1327 spin_unlock(&export->exp_lock);
1328 LASSERT(export->exp_obd == target);
1330 rc = target_handle_reconnect(&conn, export, &cluuid);
1333 /* If we found an export, we already unlocked. */
1336 OBD_FAIL_TIMEOUT(OBD_FAIL_TGT_DELAY_CONNECT, 2 * obd_timeout);
1337 } else if (req->rq_export == NULL &&
1338 atomic_read(&export->exp_rpc_count) > 0) {
1339 LCONSOLE_WARN("%s: Client %s (at %s) refused connection, still busy with %d references\n",
1340 target->obd_name, cluuid.uuid,
1341 libcfs_nid2str(req->rq_peer.nid),
1342 refcount_read(&export->exp_handle.h_ref));
1343 GOTO(out, rc = -EBUSY);
1344 } else if (lustre_msg_get_conn_cnt(req->rq_reqmsg) == 1 &&
1346 if (!strstr(cluuid.uuid, "mdt"))
1347 LCONSOLE_WARN("%s: Rejecting reconnect from the known client %s (at %s) because it is indicating it is a new client\n",
1348 target->obd_name, cluuid.uuid,
1349 libcfs_nid2str(req->rq_peer.nid));
1350 GOTO(out, rc = -EALREADY);
1352 OBD_FAIL_TIMEOUT(OBD_FAIL_TGT_DELAY_RECONNECT, 2 * obd_timeout);
1358 CDEBUG(D_HA, "%s: connection from %s@%s %st%llu exp %p cur %lld last %lld\n",
1359 target->obd_name, cluuid.uuid, libcfs_nid2str(req->rq_peer.nid),
1360 target->obd_recovering ? "recovering/" : "", data->ocd_transno,
1361 export, ktime_get_seconds(),
1362 export ? export->exp_last_request_time : 0);
1365 * If this is the first time a client connects, reset the recovery
1366 * timer. Discard lightweight connections which might be local.
1368 if (!lw_client && rc == 0 && target->obd_recovering)
1369 check_and_start_recovery_timer(target, req, export == NULL);
1372 * We want to handle EALREADY but *not* -EALREADY from
1373 * target_handle_reconnect(), return reconnection state in a flag.
1375 if (rc == EALREADY) {
1376 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECONNECT);
1382 /* Tell the client if we support replayable requests. */
1383 if (target->obd_replayable)
1384 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
1385 client_nid = &req->rq_peer.nid;
1387 if (export == NULL) {
1388 /* allow lightweight connections during recovery */
1390 * allow "new" MDT to be connected during recovery, since we
1391 * need retrieve recovery update records from it
1393 if (target->obd_recovering && !lw_client && !mds_mds_conn) {
1394 struct hrtimer *timer = &target->obd_recovery_timer;
1403 connected = atomic_read(&target->obd_connected_clients);
1404 in_progress = atomic_read(&target->obd_lock_replay_clients);
1406 atomic_read(&target->obd_max_recoverable_clients);
1407 stale = target->obd_stale_clients;
1408 remaining = hrtimer_get_remaining(timer);
1409 left = ktime_divns(remaining, NSEC_PER_SEC);
1411 if (ktime_to_ns(remaining) > 0) {
1412 msg = "to recover in";
1415 msg = "already passed deadline";
1418 target_check_recovery_timer(target);
1421 LCONSOLE_WARN("%s: Denying connection for new client %s (at %s), waiting for %d known clients (%d recovered, %d in progress, and %d evicted) %s %lld:%.02lld\n",
1422 target->obd_name, cluuid.uuid,
1423 libcfs_nid2str(req->rq_peer.nid), known,
1424 connected - in_progress, in_progress,
1425 stale, msg, timeout / 60, timeout % 60);
1429 rc = obd_connect(req->rq_svc_thread->t_env,
1430 &export, target, &cluuid, data,
1432 if (mds_conn && OBD_FAIL_CHECK(OBD_FAIL_TGT_RCVG_FLAG))
1433 lustre_msg_add_op_flags(req->rq_repmsg,
1434 MSG_CONNECT_RECOVERING);
1436 conn.cookie = export->exp_handle.h_cookie;
1437 rc = rev_import_init(export);
1441 new_mds_mds_conn = true;
1444 rc = obd_reconnect(req->rq_svc_thread->t_env,
1445 export, target, &cluuid, data, client_nid);
1450 LASSERT(target->u.obt.obt_magic == OBT_MAGIC);
1451 data->ocd_instance = target->u.obt.obt_instance;
1454 * Return only the parts of obd_connect_data that we understand, so the
1455 * client knows that we don't understand the rest.
1458 tmpsize = req_capsule_get_size(&req->rq_pill, &RMF_CONNECT_DATA,
1460 tmpdata = req_capsule_server_get(&req->rq_pill,
1463 * Don't use struct assignment here, because the client reply
1464 * buffer may be smaller/larger than the local struct
1467 memcpy(tmpdata, data, min(tmpsize, size));
1471 * If the client and the server are the same node, we will already
1472 * have an export that really points to the client's DLM export,
1473 * because we have a shared handles table.
1475 * XXX this will go away when shaver stops sending the "connect" handle
1476 * in the real "remote handle" field of the request --phik 24 Apr 2003
1478 ptlrpc_request_change_export(req, export);
1480 pcon = ptlrpc_connection_get(req->rq_peer, req->rq_self, &cluuid);
1482 GOTO(out, rc = -ENOTCONN);
1484 spin_lock(&export->exp_lock);
1486 if (export->exp_disconnected) {
1487 spin_unlock(&export->exp_lock);
1488 GOTO(out, rc = -ENODEV);
1490 if (export->exp_conn_cnt >= lustre_msg_get_conn_cnt(req->rq_reqmsg)) {
1491 spin_unlock(&export->exp_lock);
1493 "%s: %s already connected at greater or equal conn_cnt: %d >= %d\n",
1494 cluuid.uuid, libcfs_nid2str(req->rq_peer.nid),
1495 export->exp_conn_cnt,
1496 lustre_msg_get_conn_cnt(req->rq_reqmsg));
1498 GOTO(out, rc = -EALREADY);
1500 LASSERT(lustre_msg_get_conn_cnt(req->rq_reqmsg) > 0);
1501 export->exp_conn_cnt = lustre_msg_get_conn_cnt(req->rq_reqmsg);
1503 /* Check to see if connection came from another NID. */
1504 if (export->exp_connection != NULL &&
1505 export->exp_connection->c_peer.nid != req->rq_peer.nid) {
1506 obd_nid_del(export->exp_obd, export);
1507 ptlrpc_connection_put(export->exp_connection);
1508 export->exp_connection = NULL;
1511 if (export->exp_connection == NULL) {
1512 export->exp_connection = pcon;
1515 obd_nid_add(export->exp_obd, export);
1517 spin_unlock(&export->exp_lock);
1519 lustre_msg_set_handle(req->rq_repmsg, &conn);
1521 rc = rev_import_reconnect(export, req);
1525 if (target->obd_recovering && !export->exp_in_recovery && !lw_client) {
1527 __u64 transno = data->ocd_transno;
1529 spin_lock(&export->exp_lock);
1531 * possible race with class_disconnect_stale_exports,
1532 * export may be already in the eviction process
1534 if (export->exp_failed) {
1535 spin_unlock(&export->exp_lock);
1536 GOTO(out, rc = -ENODEV);
1538 export->exp_in_recovery = 1;
1539 export->exp_req_replay_needed = 1;
1540 export->exp_lock_replay_needed = 1;
1541 spin_unlock(&export->exp_lock);
1543 has_transno = !!(lustre_msg_get_op_flags(req->rq_reqmsg) &
1544 MSG_CONNECT_TRANSNO);
1545 if (has_transno && transno == 0)
1546 CWARN("Connect with zero transno!\n");
1548 if (has_transno && transno > 0 &&
1549 transno < target->obd_next_recovery_transno &&
1550 transno > target->obd_last_committed) {
1551 /* Another way is to use cmpxchg() to be lock-free. */
1552 spin_lock(&target->obd_recovery_task_lock);
1553 if (transno < target->obd_next_recovery_transno)
1554 target->obd_next_recovery_transno = transno;
1555 spin_unlock(&target->obd_recovery_task_lock);
1558 atomic_inc(&target->obd_req_replay_clients);
1559 atomic_inc(&target->obd_lock_replay_clients);
1561 * Note: MDS-MDS connection is allowed to be connected during
1562 * recovery, no matter if the exports needs to be recoveried.
1563 * Because we need retrieve updates logs from all other MDTs.
1564 * So if the MDS-MDS export is new, obd_max_recoverable_clients
1565 * also needs to be increased to match other recovery checking
1568 if (new_mds_mds_conn)
1569 atomic_inc(&target->obd_max_recoverable_clients);
1571 if (atomic_inc_return(&target->obd_connected_clients) ==
1572 atomic_read(&target->obd_max_recoverable_clients))
1573 wake_up(&target->obd_next_transno_waitq);
1576 /* Tell the client we're in recovery, when client is involved in it. */
1577 if (target->obd_recovering && !lw_client)
1578 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
1582 spin_lock(&export->exp_lock);
1583 export->exp_connecting = 0;
1584 spin_unlock(&export->exp_lock);
1586 class_export_put(export);
1588 if (target != NULL) {
1589 spin_lock(&target->obd_dev_lock);
1590 target->obd_conn_inprogress--;
1591 spin_unlock(&target->obd_dev_lock);
1592 class_decref(target, "find", current);
1595 ptlrpc_connection_put(pcon);
1596 req->rq_status = rc;
1600 int target_handle_disconnect(struct ptlrpc_request *req)
1606 rc = req_capsule_server_pack(&req->rq_pill);
1610 /* In case of target disconnect, updating sec ctx immediately is
1611 * required in order to record latest sequence number used.
1612 * Sequence is normally updated on export destroy, but this event
1613 * can occur too late, ie after a new target connect request has
1615 * Maintaining correct sequence when client connection becomes idle
1616 * ensures that GSS does not erroneously consider requests as replays.
1618 rc = sptlrpc_export_update_ctx(req->rq_export);
1622 /* Keep the rq_export around so we can send the reply. */
1623 req->rq_status = obd_disconnect(class_export_get(req->rq_export));
1628 void target_destroy_export(struct obd_export *exp)
1630 struct obd_import *imp = NULL;
1632 * exports created from last_rcvd data, and "fake"
1633 * exports created by lctl don't have an import
1635 spin_lock(&exp->exp_lock);
1636 if (exp->exp_imp_reverse != NULL) {
1637 imp = exp->exp_imp_reverse;
1638 exp->exp_imp_reverse = NULL;
1640 spin_unlock(&exp->exp_lock);
1642 client_destroy_import(imp);
1644 LASSERT_ATOMIC_ZERO(&exp->exp_locks_count);
1645 LASSERT_ATOMIC_ZERO(&exp->exp_rpc_count);
1646 LASSERT_ATOMIC_ZERO(&exp->exp_cb_count);
1647 LASSERT_ATOMIC_ZERO(&exp->exp_replay_count);
1649 EXPORT_SYMBOL(target_destroy_export);
1652 * Recovery functions
1654 static void target_request_copy_get(struct ptlrpc_request *req)
1656 class_export_rpc_inc(req->rq_export);
1657 LASSERT(list_empty(&req->rq_list));
1658 INIT_LIST_HEAD(&req->rq_replay_list);
1660 /* Increase refcount to keep request in queue. */
1661 atomic_inc(&req->rq_refcount);
1662 /* Let export know it has replays to be handled. */
1663 atomic_inc(&req->rq_export->exp_replay_count);
1666 static void target_request_copy_put(struct ptlrpc_request *req)
1668 LASSERT(list_empty(&req->rq_replay_list));
1669 LASSERT_ATOMIC_POS(&req->rq_export->exp_replay_count);
1671 atomic_dec(&req->rq_export->exp_replay_count);
1672 class_export_rpc_dec(req->rq_export);
1673 ptlrpc_server_drop_request(req);
1676 static int target_exp_enqueue_req_replay(struct ptlrpc_request *req)
1678 __u64 transno = lustre_msg_get_transno(req->rq_reqmsg);
1679 struct obd_export *exp = req->rq_export;
1680 struct ptlrpc_request *reqiter;
1681 struct ptlrpc_request *dup_req = NULL;
1686 spin_lock(&exp->exp_lock);
1687 list_for_each_entry(reqiter, &exp->exp_req_replay_queue,
1689 if (lustre_msg_get_transno(reqiter->rq_reqmsg) == transno) {
1697 /* We expect it with RESENT and REPLAY flags. */
1698 if ((lustre_msg_get_flags(req->rq_reqmsg) &
1699 (MSG_RESENT | MSG_REPLAY)) != (MSG_RESENT | MSG_REPLAY))
1700 CERROR("invalid flags %x of resent replay\n",
1701 lustre_msg_get_flags(req->rq_reqmsg));
1703 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1706 new_conn = lustre_msg_get_conn_cnt(req->rq_reqmsg);
1708 lustre_msg_get_conn_cnt(dup_req->rq_reqmsg))
1709 lustre_msg_set_conn_cnt(dup_req->rq_reqmsg,
1713 list_add_tail(&req->rq_replay_list,
1714 &exp->exp_req_replay_queue);
1717 spin_unlock(&exp->exp_lock);
1721 static void target_exp_dequeue_req_replay(struct ptlrpc_request *req)
1723 LASSERT(!list_empty(&req->rq_replay_list));
1724 LASSERT(req->rq_export);
1726 spin_lock(&req->rq_export->exp_lock);
1727 list_del_init(&req->rq_replay_list);
1728 spin_unlock(&req->rq_export->exp_lock);
1731 static void target_finish_recovery(struct lu_target *lut)
1733 struct obd_device *obd = lut->lut_obd;
1737 /* Only log a recovery message when recovery has occurred. */
1738 if (obd->obd_recovery_start) {
1739 time64_t now = ktime_get_seconds();
1740 time64_t elapsed_time;
1742 elapsed_time = max_t(time64_t, now - obd->obd_recovery_start,
1744 LCONSOLE_INFO("%s: Recovery over after %lld:%.02lld, of %d clients %d recovered and %d %s evicted.\n",
1745 obd->obd_name, elapsed_time / 60,
1747 atomic_read(&obd->obd_max_recoverable_clients),
1748 atomic_read(&obd->obd_connected_clients),
1749 obd->obd_stale_clients,
1750 obd->obd_stale_clients == 1 ? "was" : "were");
1753 ldlm_reprocess_recovery_done(obd->obd_namespace);
1754 spin_lock(&obd->obd_recovery_task_lock);
1755 if (!list_empty(&obd->obd_req_replay_queue) ||
1756 !list_empty(&obd->obd_lock_replay_queue) ||
1757 !list_empty(&obd->obd_final_req_queue)) {
1758 CERROR("%s: Recovery queues ( %s%s%s) are not empty\n",
1760 list_empty(&obd->obd_req_replay_queue) ? "" : "req ",
1761 list_empty(&obd->obd_lock_replay_queue) ? \
1763 list_empty(&obd->obd_final_req_queue) ? \
1765 spin_unlock(&obd->obd_recovery_task_lock);
1768 spin_unlock(&obd->obd_recovery_task_lock);
1770 obd->obd_recovery_end = ktime_get_seconds();
1772 /* When recovery finished, cleanup orphans on MDS and OST. */
1773 if (obd->obd_type && OBP(obd, postrecov)) {
1774 int rc = OBP(obd, postrecov)(obd);
1777 LCONSOLE_WARN("%s: Post recovery failed, rc %d\n",
1783 static void abort_req_replay_queue(struct obd_device *obd)
1785 struct ptlrpc_request *req, *n;
1786 LIST_HEAD(abort_list);
1788 spin_lock(&obd->obd_recovery_task_lock);
1789 list_splice_init(&obd->obd_req_replay_queue, &abort_list);
1790 spin_unlock(&obd->obd_recovery_task_lock);
1791 list_for_each_entry_safe(req, n, &abort_list, rq_list) {
1792 DEBUG_REQ(D_WARNING, req, "aborted:");
1793 req->rq_status = -ENOTCONN;
1794 if (ptlrpc_error(req)) {
1795 DEBUG_REQ(D_ERROR, req,
1796 "failed abort_req_reply; skipping");
1798 target_exp_dequeue_req_replay(req);
1799 target_request_copy_put(req);
1803 static void abort_lock_replay_queue(struct obd_device *obd)
1805 struct ptlrpc_request *req, *n;
1806 LIST_HEAD(abort_list);
1808 spin_lock(&obd->obd_recovery_task_lock);
1809 list_splice_init(&obd->obd_lock_replay_queue, &abort_list);
1810 spin_unlock(&obd->obd_recovery_task_lock);
1811 list_for_each_entry_safe(req, n, &abort_list, rq_list) {
1812 DEBUG_REQ(D_ERROR, req, "aborted:");
1813 req->rq_status = -ENOTCONN;
1814 if (ptlrpc_error(req)) {
1815 DEBUG_REQ(D_ERROR, req,
1816 "failed abort_lock_reply; skipping");
1818 target_request_copy_put(req);
1823 * Called from a cleanup function if the device is being cleaned up
1824 * forcefully. The exports should all have been disconnected already,
1825 * the only thing left to do is
1826 * - clear the recovery flags
1827 * - cancel the timer
1828 * - free queued requests and replies, but don't send replies
1829 * Because the obd_stopping flag is set, no new requests should be received.
1831 void target_cleanup_recovery(struct obd_device *obd)
1833 struct ptlrpc_request *req, *n;
1834 LIST_HEAD(clean_list);
1836 spin_lock(&obd->obd_dev_lock);
1837 if (!obd->obd_recovering) {
1838 spin_unlock(&obd->obd_dev_lock);
1842 obd->obd_recovering = obd->obd_abort_recovery = 0;
1843 spin_unlock(&obd->obd_dev_lock);
1845 spin_lock(&obd->obd_recovery_task_lock);
1846 target_cancel_recovery_timer(obd);
1847 list_splice_init(&obd->obd_req_replay_queue, &clean_list);
1848 spin_unlock(&obd->obd_recovery_task_lock);
1850 list_for_each_entry_safe(req, n, &clean_list, rq_list) {
1851 LASSERT(req->rq_reply_state == NULL);
1852 target_exp_dequeue_req_replay(req);
1853 target_request_copy_put(req);
1856 spin_lock(&obd->obd_recovery_task_lock);
1857 list_splice_init(&obd->obd_lock_replay_queue, &clean_list);
1858 list_splice_init(&obd->obd_final_req_queue, &clean_list);
1859 spin_unlock(&obd->obd_recovery_task_lock);
1861 list_for_each_entry_safe(req, n, &clean_list, rq_list) {
1862 LASSERT(req->rq_reply_state == NULL);
1863 target_request_copy_put(req);
1868 EXPORT_SYMBOL(target_cleanup_recovery);
1870 /* obd_recovery_task_lock should be held */
1871 void target_cancel_recovery_timer(struct obd_device *obd)
1873 CDEBUG(D_HA, "%s: cancel recovery timer\n", obd->obd_name);
1874 hrtimer_cancel(&obd->obd_recovery_timer);
1877 static void target_start_recovery_timer(struct obd_device *obd)
1881 if (obd->obd_recovery_start != 0)
1884 spin_lock(&obd->obd_dev_lock);
1885 if (!obd->obd_recovering || obd->obd_abort_recovery) {
1886 spin_unlock(&obd->obd_dev_lock);
1890 LASSERT(obd->obd_recovery_timeout != 0);
1892 if (obd->obd_recovery_start != 0) {
1893 spin_unlock(&obd->obd_dev_lock);
1897 obd->obd_recovery_start = ktime_get_seconds();
1898 delay = ktime_set(obd->obd_recovery_start +
1899 obd->obd_recovery_timeout, 0);
1900 hrtimer_start(&obd->obd_recovery_timer, delay, HRTIMER_MODE_ABS);
1901 spin_unlock(&obd->obd_dev_lock);
1903 LCONSOLE_WARN("%s: Will be in recovery for at least %u:%02u, or until %d client%s reconnect%s\n",
1905 obd->obd_recovery_timeout / 60,
1906 obd->obd_recovery_timeout % 60,
1907 atomic_read(&obd->obd_max_recoverable_clients),
1908 (atomic_read(&obd->obd_max_recoverable_clients) == 1) ?
1910 (atomic_read(&obd->obd_max_recoverable_clients) == 1) ?
1915 * extend recovery window.
1917 * if @extend is true, extend recovery window to have @dr_timeout remaining
1918 * at least; otherwise, make sure the recovery timeout value is not less
1921 static void extend_recovery_timer(struct obd_device *obd, timeout_t dr_timeout,
1928 spin_lock(&obd->obd_dev_lock);
1929 if (!obd->obd_recovering || obd->obd_abort_recovery ||
1930 obd->obd_stopping) {
1931 spin_unlock(&obd->obd_dev_lock);
1934 LASSERT(obd->obd_recovery_start != 0);
1936 left_ns = hrtimer_get_remaining(&obd->obd_recovery_timer);
1937 left = ktime_divns(left_ns, NSEC_PER_SEC);
1940 timeout = obd->obd_recovery_timeout;
1941 /* dr_timeout will happen after the hrtimer has expired.
1942 * Add the excess time to the soft recovery timeout without
1943 * exceeding the hard recovery timeout.
1945 if (dr_timeout > left) {
1946 timeout += dr_timeout - left;
1947 timeout = min_t(timeout_t, obd->obd_recovery_time_hard,
1951 timeout = clamp_t(timeout_t, dr_timeout,
1952 obd->obd_recovery_timeout,
1953 obd->obd_recovery_time_hard);
1956 if (timeout == obd->obd_recovery_time_hard)
1957 CWARN("%s: extended recovery timer reached hard limit: %d, extend: %d\n",
1958 obd->obd_name, timeout, extend);
1960 if (obd->obd_recovery_timeout < timeout) {
1963 obd->obd_recovery_timeout = timeout;
1964 end = ktime_set(obd->obd_recovery_start + timeout, 0);
1965 now = ktime_set(ktime_get_seconds(), 0);
1966 left_ns = ktime_sub(end, now);
1967 hrtimer_start(&obd->obd_recovery_timer, end, HRTIMER_MODE_ABS);
1968 left = ktime_divns(left_ns, NSEC_PER_SEC);
1970 spin_unlock(&obd->obd_dev_lock);
1972 CDEBUG(D_HA, "%s: recovery timer will expire in %d seconds\n",
1973 obd->obd_name, left);
1976 /* Reset the timer with each new client connection */
1978 * This timer is actually reconnect_timer, which is for making sure
1979 * the total recovery window is at least as big as my reconnect
1980 * attempt timing. So the initial recovery time_out will be set to
1981 * OBD_RECOVERY_FACTOR * obd_timeout. If the timeout coming
1982 * from client is bigger than this, then the recovery time_out will
1983 * be extended to make sure the client could be reconnected, in the
1984 * process, the timeout from the new client should be ignored.
1987 check_and_start_recovery_timer(struct obd_device *obd,
1988 struct ptlrpc_request *req,
1991 timeout_t service_timeout = lustre_msg_get_service_timeout(req->rq_reqmsg);
1992 struct obd_device_target *obt = &obd->u.obt;
1994 if (!new_client && service_timeout)
1996 * Teach server about old server's estimates, as first guess
1997 * at how long new requests will take.
1999 at_measured(&req->rq_rqbd->rqbd_svcpt->scp_at_estimate,
2002 target_start_recovery_timer(obd);
2005 * Convert the service time to RPC timeout,
2006 * and reuse service_timeout to limit stack usage.
2008 service_timeout = at_est2timeout(service_timeout);
2010 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_SLUGGISH_NET) &&
2011 service_timeout < at_extra)
2012 service_timeout = at_extra;
2015 * We expect other clients to timeout within service_timeout, then try
2016 * to reconnect, then try the failover server. The max delay between
2017 * connect attempts is SWITCH_MAX + SWITCH_INC + INITIAL.
2019 service_timeout += 2 * INITIAL_CONNECT_TIMEOUT;
2021 LASSERT(obt->obt_magic == OBT_MAGIC);
2022 service_timeout += 2 * (CONNECTION_SWITCH_MAX + CONNECTION_SWITCH_INC);
2023 if (service_timeout > obd->obd_recovery_timeout && !new_client)
2024 extend_recovery_timer(obd, service_timeout, false);
2027 /** Health checking routines */
2028 static inline int exp_connect_healthy(struct obd_export *exp)
2030 return exp->exp_in_recovery;
2033 /** if export done req_replay or has replay in queue */
2034 static inline int exp_req_replay_healthy(struct obd_export *exp)
2036 return (!exp->exp_req_replay_needed ||
2037 atomic_read(&exp->exp_replay_count) > 0);
2041 static inline int exp_req_replay_healthy_or_from_mdt(struct obd_export *exp)
2043 return (exp_connect_flags(exp) & OBD_CONNECT_MDS_MDS) ||
2044 exp_req_replay_healthy(exp);
2047 /** if export done lock_replay or has replay in queue */
2048 static inline int exp_lock_replay_healthy(struct obd_export *exp)
2050 return (!exp->exp_lock_replay_needed ||
2051 atomic_read(&exp->exp_replay_count) > 0);
2054 static inline int exp_vbr_healthy(struct obd_export *exp)
2056 return !exp->exp_vbr_failed;
2059 static inline int exp_finished(struct obd_export *exp)
2061 return exp->exp_in_recovery && !exp->exp_lock_replay_needed;
2064 static inline int exp_finished_or_from_mdt(struct obd_export *exp)
2066 return (exp_connect_flags(exp) & OBD_CONNECT_MDS_MDS) ||
2070 static int check_for_next_transno(struct lu_target *lut)
2072 struct ptlrpc_request *req = NULL;
2073 struct obd_device *obd = lut->lut_obd;
2074 struct target_distribute_txn_data *tdtd = lut->lut_tdtd;
2075 int wake_up = 0, connected, completed, queue_len;
2076 __u64 req_transno = 0;
2077 __u64 update_transno = 0;
2078 __u64 next_transno = 0;
2082 spin_lock(&obd->obd_recovery_task_lock);
2083 if (!list_empty(&obd->obd_req_replay_queue)) {
2084 req = list_entry(obd->obd_req_replay_queue.next,
2085 struct ptlrpc_request, rq_list);
2086 req_transno = lustre_msg_get_transno(req->rq_reqmsg);
2089 if (!obd->obd_abort_recov_mdt && tdtd)
2090 update_transno = distribute_txn_get_next_transno(tdtd);
2092 connected = atomic_read(&obd->obd_connected_clients);
2093 completed = connected - atomic_read(&obd->obd_req_replay_clients);
2094 queue_len = obd->obd_requests_queued_for_recovery;
2095 next_transno = obd->obd_next_recovery_transno;
2098 "max: %d, connected: %d, completed: %d, queue_len: %d, req_transno: %llu, next_transno: %llu\n",
2099 atomic_read(&obd->obd_max_recoverable_clients),
2100 connected, completed,
2101 queue_len, req_transno, next_transno);
2103 if (obd->obd_abort_recovery) {
2104 CDEBUG(D_HA, "waking for aborted recovery\n");
2106 } else if (obd->obd_recovery_expired) {
2107 CDEBUG(D_HA, "waking for expired recovery\n");
2109 } else if (!obd->obd_abort_recov_mdt && tdtd && req &&
2110 is_req_replayed_by_update(req)) {
2111 LASSERTF(req_transno < next_transno,
2112 "req_transno %llu next_transno%llu\n", req_transno,
2114 CDEBUG(D_HA, "waking for duplicate req (%llu)\n",
2117 } else if (req_transno == next_transno ||
2118 (update_transno != 0 && update_transno <= next_transno)) {
2119 CDEBUG(D_HA, "waking for next (%lld)\n", next_transno);
2121 } else if (queue_len > 0 &&
2122 queue_len == atomic_read(&obd->obd_req_replay_clients)) {
2123 /** handle gaps occured due to lost reply or VBR */
2124 LASSERTF(req_transno >= next_transno,
2125 "req_transno: %llu, next_transno: %llu\n",
2126 req_transno, next_transno);
2128 "%s: waking for gap in transno, VBR is %s (skip: %lld, ql: %d, comp: %d, conn: %d, next: %lld, next_update %lld last_committed: %lld)\n",
2129 obd->obd_name, obd->obd_version_recov ? "ON" : "OFF",
2130 next_transno, queue_len, completed, connected,
2131 req_transno, update_transno, obd->obd_last_committed);
2132 obd->obd_next_recovery_transno = req_transno;
2134 } else if (atomic_read(&obd->obd_req_replay_clients) == 0) {
2135 CDEBUG(D_HA, "waking for completed recovery\n");
2137 } else if (OBD_FAIL_CHECK(OBD_FAIL_MDS_RECOVERY_ACCEPTS_GAPS)) {
2139 "accepting transno gaps is explicitly allowed by fail_lock, waking up (%lld)\n",
2141 obd->obd_next_recovery_transno = req_transno;
2144 spin_unlock(&obd->obd_recovery_task_lock);
2148 static int check_for_next_lock(struct lu_target *lut)
2150 struct obd_device *obd = lut->lut_obd;
2153 spin_lock(&obd->obd_recovery_task_lock);
2154 if (!list_empty(&obd->obd_lock_replay_queue)) {
2155 CDEBUG(D_HA, "waking for next lock\n");
2157 } else if (atomic_read(&obd->obd_lock_replay_clients) == 0) {
2158 CDEBUG(D_HA, "waking for completed lock replay\n");
2160 } else if (obd->obd_abort_recovery) {
2161 CDEBUG(D_HA, "waking for aborted recovery\n");
2163 } else if (obd->obd_recovery_expired) {
2164 CDEBUG(D_HA, "waking for expired recovery\n");
2167 spin_unlock(&obd->obd_recovery_task_lock);
2172 static int check_update_llog(struct lu_target *lut)
2174 struct obd_device *obd = lut->lut_obd;
2175 struct target_distribute_txn_data *tdtd = lut->lut_tdtd;
2177 if (obd->obd_abort_recovery) {
2178 CDEBUG(D_HA, "waking for aborted recovery\n");
2182 if (atomic_read(&tdtd->tdtd_recovery_threads_count) == 0) {
2183 CDEBUG(D_HA, "waking for completion of reading update log\n");
2191 * wait for recovery events,
2192 * check its status with help of check_routine
2193 * evict dead clients via health_check
2195 static int target_recovery_overseer(struct lu_target *lut,
2196 int (*check_routine)(struct lu_target *),
2197 int (*health_check)(struct obd_export *))
2199 struct obd_device *obd = lut->lut_obd;
2200 struct target_distribute_txn_data *tdtd;
2204 if (obd->obd_recovering && obd->obd_recovery_start == 0) {
2205 now = ktime_get_seconds();
2206 if (now - last > 600) {
2207 LCONSOLE_INFO("%s: in recovery but waiting for the first client to connect\n",
2212 if (obd->obd_recovery_start != 0 && ktime_get_seconds() >=
2213 (obd->obd_recovery_start + obd->obd_recovery_time_hard)) {
2214 __u64 next_update_transno = 0;
2217 * Only abort the recovery if there are no update recovery
2220 spin_lock(&obd->obd_recovery_task_lock);
2221 if (!obd->obd_abort_recov_mdt && lut->lut_tdtd) {
2222 next_update_transno =
2223 distribute_txn_get_next_transno(lut->lut_tdtd);
2225 tdtd = lut->lut_tdtd;
2227 * If next_update_transno == 0, it probably because
2228 * updatelog retrieve threads did not get any records
2229 * yet, let's wait those threads stopped
2231 if (next_update_transno == 0) {
2232 spin_unlock(&obd->obd_recovery_task_lock);
2234 while (wait_event_timeout(
2235 tdtd->tdtd_recovery_threads_waitq,
2236 check_update_llog(lut),
2237 cfs_time_seconds(60)) == 0);
2239 spin_lock(&obd->obd_recovery_task_lock);
2240 next_update_transno =
2241 distribute_txn_get_next_transno(tdtd);
2245 if (next_update_transno != 0 && !obd->obd_abort_recovery) {
2246 obd->obd_next_recovery_transno = next_update_transno;
2247 spin_unlock(&obd->obd_recovery_task_lock);
2249 * Disconnect unfinished exports from clients, and
2250 * keep connection from MDT to make sure the update
2251 * recovery will still keep trying until some one
2252 * manually abort the recovery
2254 class_disconnect_stale_exports(obd,
2255 exp_finished_or_from_mdt);
2256 /* Abort all of replay & replay lock req from clients */
2257 abort_req_replay_queue(obd);
2258 abort_lock_replay_queue(obd);
2260 "%s: there are still update replay (%#llx)in the queue.\n",
2261 obd->obd_name, next_update_transno);
2263 obd->obd_abort_recovery = 1;
2264 spin_unlock(&obd->obd_recovery_task_lock);
2265 CWARN("%s recovery is aborted by hard timeout\n",
2270 while (wait_event_timeout(obd->obd_next_transno_waitq,
2272 cfs_time_seconds(60)) == 0)
2273 ; /* wait indefinitely for event, but don't trigger watchdog */
2275 if (obd->obd_abort_recovery) {
2276 CWARN("recovery is aborted, evict exports in recovery\n");
2277 if (lut->lut_tdtd != NULL) {
2278 tdtd = lut->lut_tdtd;
2280 * Let's wait all of the update log recovery thread
2284 tdtd->tdtd_recovery_threads_waitq,
2285 atomic_read(&tdtd->tdtd_recovery_threads_count)
2287 /* Then abort the update recovery list */
2288 dtrq_list_destroy(lut->lut_tdtd);
2291 /** evict exports which didn't finish recovery yet */
2292 class_disconnect_stale_exports(obd, exp_finished);
2294 } else if (obd->obd_recovery_expired) {
2295 obd->obd_recovery_expired = 0;
2297 /** If some clients died being recovered, evict them */
2298 LCONSOLE_WARN("%s: recovery is timed out, evict stale exports\n",
2300 /** evict cexports with no replay in queue, they are stalled */
2301 class_disconnect_stale_exports(obd, health_check);
2303 /** continue with VBR */
2304 spin_lock(&obd->obd_dev_lock);
2305 obd->obd_version_recov = 1;
2306 spin_unlock(&obd->obd_dev_lock);
2308 * reset timer, recovery will proceed with versions now,
2309 * timeout is set just to handle reconnection delays
2311 extend_recovery_timer(obd, RECONNECT_DELAY_MAX, true);
2313 * Wait for recovery events again, after evicting bad clients
2320 static struct ptlrpc_request *target_next_replay_lock(struct lu_target *lut)
2322 struct obd_device *obd = lut->lut_obd;
2323 struct ptlrpc_request *req = NULL;
2325 CDEBUG(D_HA, "Waiting for lock\n");
2326 if (target_recovery_overseer(lut, check_for_next_lock,
2327 exp_lock_replay_healthy))
2328 abort_lock_replay_queue(obd);
2330 spin_lock(&obd->obd_recovery_task_lock);
2331 if (!list_empty(&obd->obd_lock_replay_queue)) {
2332 req = list_entry(obd->obd_lock_replay_queue.next,
2333 struct ptlrpc_request, rq_list);
2334 list_del_init(&req->rq_list);
2335 spin_unlock(&obd->obd_recovery_task_lock);
2337 spin_unlock(&obd->obd_recovery_task_lock);
2338 LASSERT(list_empty(&obd->obd_lock_replay_queue));
2339 LASSERT(atomic_read(&obd->obd_lock_replay_clients) == 0);
2340 /** evict exports failed VBR */
2341 class_disconnect_stale_exports(obd, exp_vbr_healthy);
2346 static struct ptlrpc_request *target_next_final_ping(struct obd_device *obd)
2348 struct ptlrpc_request *req = NULL;
2350 spin_lock(&obd->obd_recovery_task_lock);
2351 if (!list_empty(&obd->obd_final_req_queue)) {
2352 req = list_entry(obd->obd_final_req_queue.next,
2353 struct ptlrpc_request, rq_list);
2354 list_del_init(&req->rq_list);
2355 spin_unlock(&obd->obd_recovery_task_lock);
2356 if (req->rq_export->exp_in_recovery) {
2357 spin_lock(&req->rq_export->exp_lock);
2358 req->rq_export->exp_in_recovery = 0;
2359 spin_unlock(&req->rq_export->exp_lock);
2362 spin_unlock(&obd->obd_recovery_task_lock);
2367 static void handle_recovery_req(struct ptlrpc_thread *thread,
2368 struct ptlrpc_request *req,
2369 svc_handler_t handler)
2374 * export can be evicted during recovery, no need to handle replays for
2375 * it after that, discard such request silently
2377 if (req->rq_export->exp_disconnected)
2380 req->rq_session.lc_thread = thread;
2381 req->rq_svc_thread = thread;
2382 req->rq_svc_thread->t_env->le_ses = &req->rq_session;
2384 /* thread context */
2385 lu_context_enter(&thread->t_env->le_ctx);
2387 lu_context_exit(&thread->t_env->le_ctx);
2389 req->rq_svc_thread->t_env->le_ses = NULL;
2391 /* don't reset timer for final stage */
2392 if (!exp_finished(req->rq_export)) {
2393 timeout_t timeout = obd_timeout;
2396 * Add request @timeout to the recovery time so next request from
2397 * this client may come in recovery time
2400 struct ptlrpc_service_part *svcpt;
2401 timeout_t est_timeout;
2403 svcpt = req->rq_rqbd->rqbd_svcpt;
2405 * If the server sent early reply for this request,
2406 * the client will recalculate the timeout according to
2407 * current server estimate service time, so we will
2408 * use the maxium timeout here for waiting the client
2409 * sending the next req
2411 est_timeout = at_get(&svcpt->scp_at_estimate);
2412 timeout = max_t(timeout_t, at_est2timeout(est_timeout),
2413 lustre_msg_get_timeout(req->rq_reqmsg));
2415 * Add 2 net_latency, one for balance rq_deadline
2416 * (see ptl_send_rpc), one for resend the req to server,
2417 * Note: client will pack net_latency in replay req
2418 * (see ptlrpc_replay_req)
2420 timeout += 2 * lustre_msg_get_service_timeout(req->rq_reqmsg);
2422 extend_recovery_timer(class_exp2obd(req->rq_export), timeout,
2428 /** Checking routines for recovery */
2429 static int check_for_recovery_ready(struct lu_target *lut)
2431 struct obd_device *obd = lut->lut_obd;
2432 unsigned int clnts = atomic_read(&obd->obd_connected_clients);
2435 "connected %d stale %d max_recoverable_clients %d abort %d expired %d\n",
2436 clnts, obd->obd_stale_clients,
2437 atomic_read(&obd->obd_max_recoverable_clients),
2438 obd->obd_abort_recovery, obd->obd_recovery_expired);
2440 if (!obd->obd_abort_recovery && !obd->obd_recovery_expired) {
2442 atomic_read(&obd->obd_max_recoverable_clients));
2443 if (clnts + obd->obd_stale_clients <
2444 atomic_read(&obd->obd_max_recoverable_clients))
2448 if (!obd->obd_abort_recov_mdt && lut->lut_tdtd != NULL) {
2449 if (!lut->lut_tdtd->tdtd_replay_ready &&
2450 !obd->obd_abort_recovery && !obd->obd_stopping) {
2452 * Let's extend recovery timer, in case the recovery
2453 * timer expired, and some clients got evicted
2455 extend_recovery_timer(obd, obd->obd_recovery_timeout,
2458 "%s update recovery is not ready, extend recovery %d\n",
2459 obd->obd_name, obd->obd_recovery_timeout);
2468 REQUEST_RECOVERY = 1,
2469 UPDATE_RECOVERY = 2,
2472 static __u64 get_next_replay_req_transno(struct obd_device *obd)
2476 if (!list_empty(&obd->obd_req_replay_queue)) {
2477 struct ptlrpc_request *req;
2479 req = list_entry(obd->obd_req_replay_queue.next,
2480 struct ptlrpc_request, rq_list);
2481 transno = lustre_msg_get_transno(req->rq_reqmsg);
2487 static __u64 get_next_transno(struct lu_target *lut, int *type)
2489 struct obd_device *obd = lut->lut_obd;
2490 struct target_distribute_txn_data *tdtd = lut->lut_tdtd;
2492 __u64 update_transno;
2496 transno = get_next_replay_req_transno(obd);
2498 *type = REQUEST_RECOVERY;
2500 if (!tdtd || obd->obd_abort_recov_mdt)
2503 update_transno = distribute_txn_get_next_transno(tdtd);
2504 if (transno == 0 || (transno >= update_transno &&
2505 update_transno != 0)) {
2506 transno = update_transno;
2508 *type = UPDATE_RECOVERY;
2515 * drop duplicate replay request
2517 * Because the operation has been replayed by update recovery, the request
2518 * with the same transno will be dropped and also notify the client to send
2519 * next replay request.
2521 * \param[in] env execution environment
2522 * \param[in] obd failover obd device
2523 * \param[in] req request to be dropped
2525 static void drop_duplicate_replay_req(struct lu_env *env,
2526 struct obd_device *obd,
2527 struct ptlrpc_request *req)
2529 DEBUG_REQ(D_HA, req,
2530 "remove t%lld from %s because duplicate update records found",
2531 lustre_msg_get_transno(req->rq_reqmsg),
2532 libcfs_nid2str(req->rq_peer.nid));
2535 * Right now, only for MDS reint operation update replay and
2536 * normal request replay can have the same transno
2538 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_REINT) {
2539 req_capsule_set(&req->rq_pill, &RQF_MDS_REINT);
2540 req->rq_status = req_capsule_server_pack(&req->rq_pill);
2541 if (likely(req->rq_export))
2542 target_committed_to_req(req);
2543 lustre_msg_set_transno(req->rq_repmsg, req->rq_transno);
2544 target_send_reply(req, req->rq_status, 0);
2546 DEBUG_REQ(D_ERROR, req, "wrong opc from %s",
2547 libcfs_nid2str(req->rq_peer.nid));
2549 target_exp_dequeue_req_replay(req);
2550 target_request_copy_put(req);
2551 obd->obd_replayed_requests++;
2554 #define WATCHDOG_TIMEOUT (obd_timeout * 10)
2556 static void replay_request_or_update(struct lu_env *env,
2557 struct lu_target *lut,
2558 struct target_recovery_data *trd,
2559 struct ptlrpc_thread *thread)
2561 struct obd_device *obd = lut->lut_obd;
2562 struct ptlrpc_request *req = NULL;
2568 CDEBUG(D_HA, "Waiting for transno %lld\n",
2569 obd->obd_next_recovery_transno);
2571 /* Replay all of request and update by transno */
2573 struct target_distribute_txn_data *tdtd = lut->lut_tdtd;
2575 CFS_FAIL_TIMEOUT(OBD_FAIL_TGT_REPLAY_DELAY2, cfs_fail_val);
2578 * It is needed to extend recovery window above
2579 * recovery_time_soft. Extending is possible only in the
2580 * end of recovery window (see more details in
2581 * handle_recovery_req()).
2583 CFS_FAIL_TIMEOUT_MS(OBD_FAIL_TGT_REPLAY_DELAY, 300);
2585 if (target_recovery_overseer(lut, check_for_next_transno,
2586 exp_req_replay_healthy_or_from_mdt)) {
2587 abort_req_replay_queue(obd);
2588 abort_lock_replay_queue(obd);
2592 spin_lock(&obd->obd_recovery_task_lock);
2593 transno = get_next_transno(lut, &type);
2594 if (type == REQUEST_RECOVERY && transno != 0) {
2596 * Drop replay request from client side, if the
2597 * replay has been executed by update with the
2600 req = list_entry(obd->obd_req_replay_queue.next,
2601 struct ptlrpc_request, rq_list);
2603 list_del_init(&req->rq_list);
2604 obd->obd_requests_queued_for_recovery--;
2605 spin_unlock(&obd->obd_recovery_task_lock);
2608 * Let's check if the request has been redone by
2611 if (is_req_replayed_by_update(req)) {
2612 struct distribute_txn_replay_req *dtrq;
2614 dtrq = distribute_txn_lookup_finish_list(tdtd,
2616 LASSERT(dtrq != NULL);
2617 spin_lock(&tdtd->tdtd_replay_list_lock);
2618 list_del_init(&dtrq->dtrq_list);
2619 spin_unlock(&tdtd->tdtd_replay_list_lock);
2622 drop_duplicate_replay_req(env, obd, req);
2627 LASSERT(trd->trd_processing_task == current->pid);
2628 DEBUG_REQ(D_HA, req, "processing x%llu t%lld from %s",
2630 lustre_msg_get_transno(req->rq_reqmsg),
2631 libcfs_nid2str(req->rq_peer.nid));
2633 ptlrpc_watchdog_init(&thread->t_watchdog,
2635 handle_recovery_req(thread, req,
2636 trd->trd_recovery_handler);
2637 ptlrpc_watchdog_disable(&thread->t_watchdog);
2640 * bz18031: increase next_recovery_transno before
2641 * target_request_copy_put() will drop exp_rpc reference
2643 spin_lock(&obd->obd_recovery_task_lock);
2644 obd->obd_next_recovery_transno++;
2645 spin_unlock(&obd->obd_recovery_task_lock);
2646 target_exp_dequeue_req_replay(req);
2647 target_request_copy_put(req);
2648 obd->obd_replayed_requests++;
2649 } else if (type == UPDATE_RECOVERY && transno != 0) {
2650 struct distribute_txn_replay_req *dtrq;
2653 spin_unlock(&obd->obd_recovery_task_lock);
2655 LASSERT(tdtd != NULL);
2656 dtrq = distribute_txn_get_next_req(tdtd);
2657 lu_context_enter(&thread->t_env->le_ctx);
2658 ptlrpc_watchdog_init(&thread->t_watchdog,
2660 rc = tdtd->tdtd_replay_handler(env, tdtd, dtrq);
2661 ptlrpc_watchdog_disable(&thread->t_watchdog);
2662 lu_context_exit(&thread->t_env->le_ctx);
2663 extend_recovery_timer(obd, obd_timeout, true);
2665 if (rc == 0 && dtrq->dtrq_xid != 0) {
2667 "Move x%llu t%llu to finish list\n",
2669 dtrq->dtrq_master_transno);
2671 /* Add it to the replay finish list */
2672 spin_lock(&tdtd->tdtd_replay_list_lock);
2673 list_add(&dtrq->dtrq_list,
2674 &tdtd->tdtd_replay_finish_list);
2675 spin_unlock(&tdtd->tdtd_replay_list_lock);
2677 spin_lock(&obd->obd_recovery_task_lock);
2678 if (transno == obd->obd_next_recovery_transno)
2679 obd->obd_next_recovery_transno++;
2681 obd->obd_next_recovery_transno)
2682 obd->obd_next_recovery_transno =
2684 spin_unlock(&obd->obd_recovery_task_lock);
2689 spin_unlock(&obd->obd_recovery_task_lock);
2691 LASSERT(list_empty(&obd->obd_req_replay_queue));
2692 LASSERT(atomic_read(&obd->obd_req_replay_clients) == 0);
2693 /** evict exports failed VBR */
2694 class_disconnect_stale_exports(obd, exp_vbr_healthy);
2700 static int target_recovery_thread(void *arg)
2702 struct lu_target *lut = arg;
2703 struct obd_device *obd = lut->lut_obd;
2704 struct ptlrpc_request *req;
2705 struct target_recovery_data *trd = &obd->obd_recovery_data;
2706 unsigned long delta;
2708 struct ptlrpc_thread *thread = NULL;
2712 OBD_ALLOC_PTR(thread);
2718 GOTO(out_thread, rc = -ENOMEM);
2719 rc = lu_env_add(env);
2723 rc = lu_context_init(&env->le_ctx, LCT_MD_THREAD | LCT_DT_THREAD);
2725 GOTO(out_env_remove, rc);
2727 thread->t_env = env;
2728 thread->t_id = -1; /* force filter_iobuf_get/put to use local buffers */
2729 thread->t_task = current;
2730 env->le_ctx.lc_thread = thread;
2731 tgt_io_thread_init(thread); /* init thread_big_cache for IO requests */
2733 CDEBUG(D_HA, "%s: started recovery thread pid %d\n", obd->obd_name,
2735 trd->trd_processing_task = current->pid;
2737 spin_lock(&obd->obd_dev_lock);
2738 obd->obd_recovering = 1;
2739 spin_unlock(&obd->obd_dev_lock);
2740 complete(&trd->trd_starting);
2742 /* first of all, we have to know the first transno to replay */
2743 if (target_recovery_overseer(lut, check_for_recovery_ready,
2744 exp_connect_healthy)) {
2745 abort_req_replay_queue(obd);
2746 abort_lock_replay_queue(obd);
2747 if (lut->lut_tdtd != NULL)
2748 dtrq_list_destroy(lut->lut_tdtd);
2751 /* next stage: replay requests or update */
2753 CDEBUG(D_INFO, "1: request replay stage - %d clients from t%llu\n",
2754 atomic_read(&obd->obd_req_replay_clients),
2755 obd->obd_next_recovery_transno);
2756 replay_request_or_update(env, lut, trd, thread);
2759 * The second stage: replay locks
2761 CDEBUG(D_INFO, "2: lock replay stage - %d clients\n",
2762 atomic_read(&obd->obd_lock_replay_clients));
2763 while ((req = target_next_replay_lock(lut))) {
2764 LASSERT(trd->trd_processing_task == current->pid);
2765 DEBUG_REQ(D_HA, req, "processing lock from %s:",
2766 libcfs_nid2str(req->rq_peer.nid));
2767 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_LOCK_REPLAY)) {
2768 req->rq_status = -ENODEV;
2769 target_request_copy_put(req);
2772 handle_recovery_req(thread, req,
2773 trd->trd_recovery_handler);
2774 target_request_copy_put(req);
2775 obd->obd_replayed_locks++;
2779 * The third stage: reply on final pings, at this moment all clients
2780 * must have request in final queue
2782 CFS_FAIL_TIMEOUT(OBD_FAIL_TGT_REPLAY_RECONNECT, cfs_fail_val);
2783 CDEBUG(D_INFO, "3: final stage - process recovery completion pings\n");
2784 /** Update server last boot epoch */
2785 tgt_boot_epoch_update(lut);
2787 * We drop recoverying flag to forward all new requests
2788 * to regular mds_handle() since now
2790 spin_lock(&obd->obd_dev_lock);
2791 obd->obd_recovering = obd->obd_abort_recovery = 0;
2792 spin_unlock(&obd->obd_dev_lock);
2793 spin_lock(&obd->obd_recovery_task_lock);
2794 target_cancel_recovery_timer(obd);
2795 spin_unlock(&obd->obd_recovery_task_lock);
2796 while ((req = target_next_final_ping(obd))) {
2797 LASSERT(trd->trd_processing_task == current->pid);
2798 DEBUG_REQ(D_HA, req, "processing final ping from %s:",
2799 libcfs_nid2str(req->rq_peer.nid));
2800 handle_recovery_req(thread, req,
2801 trd->trd_recovery_handler);
2803 * Because the waiting client can not send ping to server,
2804 * so we need refresh the last_request_time, to avoid the
2805 * export is being evicted
2807 ptlrpc_update_export_timer(req->rq_export, 0);
2808 target_request_copy_put(req);
2811 delta = jiffies_to_msecs(jiffies - delta) / MSEC_PER_SEC;
2812 CDEBUG(D_INFO, "4: recovery completed in %lus - %d/%d reqs/locks\n",
2813 delta, obd->obd_replayed_requests, obd->obd_replayed_locks);
2814 if (delta > OBD_RECOVERY_TIME_SOFT) {
2815 CWARN("too long recovery - read logs\n");
2816 libcfs_debug_dumplog();
2819 target_finish_recovery(lut);
2820 lu_context_fini(&env->le_ctx);
2821 trd->trd_processing_task = 0;
2822 complete_all(&trd->trd_finishing);
2823 tgt_io_thread_done(thread);
2829 OBD_FREE_PTR(thread);
2833 static int target_start_recovery_thread(struct lu_target *lut,
2834 svc_handler_t handler)
2836 struct obd_device *obd = lut->lut_obd;
2838 struct target_recovery_data *trd = &obd->obd_recovery_data;
2841 memset(trd, 0, sizeof(*trd));
2842 init_completion(&trd->trd_starting);
2843 init_completion(&trd->trd_finishing);
2844 trd->trd_recovery_handler = handler;
2846 rc = server_name2index(obd->obd_name, &index, NULL);
2850 if (!IS_ERR(kthread_run(target_recovery_thread,
2851 lut, "tgt_recover_%d", index))) {
2852 wait_for_completion(&trd->trd_starting);
2853 LASSERT(obd->obd_recovering != 0);
2861 void target_stop_recovery_thread(struct obd_device *obd)
2863 if (obd->obd_recovery_data.trd_processing_task > 0) {
2864 struct target_recovery_data *trd = &obd->obd_recovery_data;
2865 /** recovery can be done but postrecovery is not yet */
2866 spin_lock(&obd->obd_dev_lock);
2867 if (obd->obd_recovering) {
2868 CERROR("%s: Aborting recovery\n", obd->obd_name);
2869 obd->obd_abort_recovery = 1;
2870 wake_up(&obd->obd_next_transno_waitq);
2872 spin_unlock(&obd->obd_dev_lock);
2873 wait_for_completion(&trd->trd_finishing);
2876 EXPORT_SYMBOL(target_stop_recovery_thread);
2878 void target_recovery_fini(struct obd_device *obd)
2880 class_disconnect_exports(obd);
2881 target_stop_recovery_thread(obd);
2882 target_cleanup_recovery(obd);
2884 EXPORT_SYMBOL(target_recovery_fini);
2886 static enum hrtimer_restart target_recovery_expired(struct hrtimer *timer)
2888 struct obd_device *obd = container_of(timer, struct obd_device,
2889 obd_recovery_timer);
2892 "%s: recovery timed out; %d clients are still in recovery after %llu seconds (%d clients connected)\n",
2893 obd->obd_name, atomic_read(&obd->obd_lock_replay_clients),
2894 ktime_get_seconds() - obd->obd_recovery_start,
2895 atomic_read(&obd->obd_connected_clients));
2897 obd->obd_recovery_expired = 1;
2898 wake_up(&obd->obd_next_transno_waitq);
2899 return HRTIMER_NORESTART;
2902 void target_recovery_init(struct lu_target *lut, svc_handler_t handler)
2904 struct obd_device *obd = lut->lut_obd;
2906 if (lut->lut_bottom->dd_rdonly)
2909 if (atomic_read(&obd->obd_max_recoverable_clients) == 0) {
2910 /** Update server last boot epoch */
2911 tgt_boot_epoch_update(lut);
2915 CDEBUG(D_HA, "RECOVERY: service %s, %d recoverable clients, "
2916 "last_transno %llu\n", obd->obd_name,
2917 atomic_read(&obd->obd_max_recoverable_clients),
2918 obd->obd_last_committed);
2919 LASSERT(obd->obd_stopping == 0);
2920 obd->obd_next_recovery_transno = obd->obd_last_committed + 1;
2921 obd->obd_recovery_start = 0;
2922 obd->obd_recovery_end = 0;
2924 hrtimer_init(&obd->obd_recovery_timer, CLOCK_MONOTONIC,
2926 obd->obd_recovery_timer.function = &target_recovery_expired;
2927 target_start_recovery_thread(lut, handler);
2929 EXPORT_SYMBOL(target_recovery_init);
2931 static int target_process_req_flags(struct obd_device *obd,
2932 struct ptlrpc_request *req)
2934 struct obd_export *exp = req->rq_export;
2936 LASSERT(exp != NULL);
2937 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REQ_REPLAY_DONE) {
2938 /* client declares he's ready to replay locks */
2939 spin_lock(&exp->exp_lock);
2940 if (exp->exp_req_replay_needed) {
2941 exp->exp_req_replay_needed = 0;
2942 spin_unlock(&exp->exp_lock);
2944 LASSERT_ATOMIC_POS(&obd->obd_req_replay_clients);
2945 atomic_dec(&obd->obd_req_replay_clients);
2947 spin_unlock(&exp->exp_lock);
2950 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_LOCK_REPLAY_DONE) {
2952 * client declares he's ready to complete recovery
2953 * so, we put the request on th final queue
2955 spin_lock(&exp->exp_lock);
2956 if (exp->exp_lock_replay_needed) {
2957 exp->exp_lock_replay_needed = 0;
2958 spin_unlock(&exp->exp_lock);
2960 LASSERT_ATOMIC_POS(&obd->obd_lock_replay_clients);
2961 atomic_dec(&obd->obd_lock_replay_clients);
2963 spin_unlock(&exp->exp_lock);
2969 int target_queue_recovery_request(struct ptlrpc_request *req,
2970 struct obd_device *obd)
2972 __u64 transno = lustre_msg_get_transno(req->rq_reqmsg);
2973 struct ptlrpc_request *reqiter;
2978 if (obd->obd_recovery_data.trd_processing_task == current->pid) {
2979 /* Processing the queue right now, don't re-add. */
2983 target_process_req_flags(obd, req);
2985 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_LOCK_REPLAY_DONE) {
2986 if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_TGT_RECOVERY_REQ_RACE))) {
2987 if (cfs_fail_val == 1) {
2990 wake_up(&cfs_race_waitq);
2992 schedule_timeout_interruptible(
2993 cfs_time_seconds(1));
2998 * client declares he's ready to complete recovery
2999 * so, we put the request on th final queue
3001 target_request_copy_get(req);
3002 DEBUG_REQ(D_HA, req, "queue final req");
3003 wake_up(&obd->obd_next_transno_waitq);
3004 spin_lock(&obd->obd_recovery_task_lock);
3005 if (obd->obd_recovering) {
3006 struct ptlrpc_request *tmp;
3007 struct ptlrpc_request *duplicate = NULL;
3009 if (likely(!req->rq_export->exp_replay_done)) {
3010 req->rq_export->exp_replay_done = 1;
3011 list_add_tail(&req->rq_list,
3012 &obd->obd_final_req_queue);
3013 spin_unlock(&obd->obd_recovery_task_lock);
3018 * XXX O(n), but only happens if final ping is
3019 * timed out, probably reorganize the list as
3022 list_for_each_entry_safe(reqiter, tmp,
3023 &obd->obd_final_req_queue,
3025 if (reqiter->rq_export == req->rq_export) {
3026 list_del_init(&reqiter->rq_list);
3027 duplicate = reqiter;
3032 list_add_tail(&req->rq_list,
3033 &obd->obd_final_req_queue);
3034 req->rq_export->exp_replay_done = 1;
3035 spin_unlock(&obd->obd_recovery_task_lock);
3037 if (duplicate != NULL) {
3038 DEBUG_REQ(D_HA, duplicate,
3039 "put prev final req");
3040 target_request_copy_put(duplicate);
3044 spin_unlock(&obd->obd_recovery_task_lock);
3045 target_request_copy_put(req);
3046 RETURN(obd->obd_stopping ? -ENOTCONN : 1);
3049 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REQ_REPLAY_DONE) {
3050 /* client declares he's ready to replay locks */
3051 target_request_copy_get(req);
3052 DEBUG_REQ(D_HA, req, "queue lock replay req");
3053 wake_up(&obd->obd_next_transno_waitq);
3054 spin_lock(&obd->obd_recovery_task_lock);
3055 LASSERT(obd->obd_recovering);
3056 /* usually due to recovery abort */
3057 if (!req->rq_export->exp_in_recovery) {
3058 spin_unlock(&obd->obd_recovery_task_lock);
3059 target_request_copy_put(req);
3062 LASSERT(req->rq_export->exp_lock_replay_needed);
3063 list_add_tail(&req->rq_list, &obd->obd_lock_replay_queue);
3064 spin_unlock(&obd->obd_recovery_task_lock);
3069 * CAVEAT EMPTOR: The incoming request message has been swabbed
3070 * (i.e. buflens etc are in my own byte order), but type-dependent
3071 * buffers (eg mdt_body, ost_body etc) have NOT been swabbed.
3075 INIT_LIST_HEAD(&req->rq_list);
3076 DEBUG_REQ(D_HA, req, "not queueing");
3081 * If we're processing the queue, we want don't want to queue this
3084 * Also, if this request has a transno less than the one we're waiting
3085 * for, we should process it now. It could (and currently always will)
3086 * be an open request for a descriptor that was opened some time ago.
3088 * Also, a resent, replayed request that has already been
3089 * handled will pass through here and be processed immediately.
3092 "Next recovery transno: %llu, current: %llu, replaying\n",
3093 obd->obd_next_recovery_transno, transno);
3096 * If the request has been replayed by update replay, then sends this
3097 * request to the recovery thread (replay_request_or_update()), where
3098 * it will be handled
3100 spin_lock(&obd->obd_recovery_task_lock);
3101 if (transno < obd->obd_next_recovery_transno &&
3102 !is_req_replayed_by_update(req)) {
3103 /* Processing the queue right now, don't re-add. */
3104 LASSERT(list_empty(&req->rq_list));
3105 spin_unlock(&obd->obd_recovery_task_lock);
3108 spin_unlock(&obd->obd_recovery_task_lock);
3110 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_REPLAY_DROP))
3113 target_request_copy_get(req);
3114 if (!req->rq_export->exp_in_recovery) {
3115 target_request_copy_put(req);
3118 LASSERT(req->rq_export->exp_req_replay_needed);
3120 if (target_exp_enqueue_req_replay(req)) {
3121 DEBUG_REQ(D_ERROR, req, "dropping resent queued req");
3122 target_request_copy_put(req);
3127 spin_lock(&obd->obd_recovery_task_lock);
3128 LASSERT(obd->obd_recovering);
3129 list_for_each_entry(reqiter, &obd->obd_req_replay_queue, rq_list) {
3130 if (lustre_msg_get_transno(reqiter->rq_reqmsg) > transno) {
3131 list_add_tail(&req->rq_list, &reqiter->rq_list);
3136 if (unlikely(lustre_msg_get_transno(reqiter->rq_reqmsg) ==
3138 DEBUG_REQ(D_ERROR, req,
3139 "dropping replay: transno has been claimed by another client");
3140 spin_unlock(&obd->obd_recovery_task_lock);
3141 target_exp_dequeue_req_replay(req);
3142 target_request_copy_put(req);
3148 list_add_tail(&req->rq_list, &obd->obd_req_replay_queue);
3150 obd->obd_requests_queued_for_recovery++;
3151 spin_unlock(&obd->obd_recovery_task_lock);
3152 wake_up(&obd->obd_next_transno_waitq);
3156 void target_committed_to_req(struct ptlrpc_request *req)
3158 struct obd_export *exp = req->rq_export;
3160 if (!exp->exp_obd->obd_no_transno && req->rq_repmsg != NULL)
3161 lustre_msg_set_last_committed(req->rq_repmsg,
3162 exp->exp_last_committed);
3164 DEBUG_REQ(D_IOCTL, req,
3165 "not sending last_committed update (%d/%d)",
3166 exp->exp_obd->obd_no_transno,
3167 req->rq_repmsg == NULL);
3169 CDEBUG(D_INFO, "last_committed %llu, transno %llu, xid %llu\n",
3170 exp->exp_last_committed, req->rq_transno, req->rq_xid);
3173 #endif /* HAVE_SERVER_SUPPORT */
3176 * Packs current SLV and Limit into \a req.
3178 int target_pack_pool_reply(struct ptlrpc_request *req)
3180 struct obd_device *obd;
3185 * Check that we still have all structures alive as this may
3186 * be some late RPC at shutdown time.
3188 if (unlikely(!req->rq_export || !req->rq_export->exp_obd ||
3189 !exp_connect_lru_resize(req->rq_export))) {
3190 lustre_msg_set_slv(req->rq_repmsg, 0);
3191 lustre_msg_set_limit(req->rq_repmsg, 0);
3195 /* OBD is alive here as export is alive, which we checked above. */
3196 obd = req->rq_export->exp_obd;
3198 read_lock(&obd->obd_pool_lock);
3199 lustre_msg_set_slv(req->rq_repmsg, obd->obd_pool_slv);
3200 lustre_msg_set_limit(req->rq_repmsg, obd->obd_pool_limit);
3201 read_unlock(&obd->obd_pool_lock);
3206 static int target_send_reply_msg(struct ptlrpc_request *req,
3207 int rc, int fail_id)
3209 if (OBD_FAIL_CHECK_ORSET(fail_id & ~OBD_FAIL_ONCE, OBD_FAIL_ONCE)) {
3210 DEBUG_REQ(D_ERROR, req, "dropping reply");
3214 * We can have a null rq_reqmsg in the event of bad signature or
3215 * no context when unwrapping
3217 if (req->rq_reqmsg &&
3218 unlikely(lustre_msg_get_opc(req->rq_reqmsg) == MDS_REINT &&
3219 OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_MULTI_NET_REP)))
3223 DEBUG_REQ(D_NET, req, "processing error (%d)", rc);
3224 req->rq_status = rc;
3225 return ptlrpc_send_error(req, 1);
3227 DEBUG_REQ(D_NET, req, "sending reply");
3229 return ptlrpc_send_reply(req, PTLRPC_REPLY_MAYBE_DIFFICULT);
3232 void target_send_reply(struct ptlrpc_request *req, int rc, int fail_id)
3234 struct ptlrpc_service_part *svcpt;
3236 struct ptlrpc_reply_state *rs;
3237 struct obd_export *exp;
3241 if (req->rq_no_reply) {
3246 svcpt = req->rq_rqbd->rqbd_svcpt;
3247 rs = req->rq_reply_state;
3248 if (rs == NULL || !rs->rs_difficult) {
3250 target_send_reply_msg(req, rc, fail_id);
3255 /* must be an export if locks saved */
3256 LASSERT(req->rq_export != NULL);
3257 /* req/reply consistent */
3258 LASSERT(rs->rs_svcpt == svcpt);
3261 LASSERT(!rs->rs_scheduled);
3262 LASSERT(!rs->rs_scheduled_ever);
3263 LASSERT(!rs->rs_handled);
3264 LASSERT(!rs->rs_on_net);
3265 LASSERT(rs->rs_export == NULL);
3266 LASSERT(list_empty(&rs->rs_obd_list));
3267 LASSERT(list_empty(&rs->rs_exp_list));
3269 exp = class_export_get(req->rq_export);
3271 /* disable reply scheduling while I'm setting up */
3272 rs->rs_scheduled = 1;
3274 rs->rs_xid = req->rq_xid;
3275 rs->rs_transno = req->rq_transno;
3276 rs->rs_export = exp;
3277 rs->rs_opc = lustre_msg_get_opc(req->rq_reqmsg);
3279 spin_lock(&exp->exp_uncommitted_replies_lock);
3280 CDEBUG(D_NET, "rs transno = %llu, last committed = %llu\n",
3281 rs->rs_transno, exp->exp_last_committed);
3282 if (rs->rs_transno > exp->exp_last_committed) {
3283 /* not committed already */
3284 list_add_tail(&rs->rs_obd_list,
3285 &exp->exp_uncommitted_replies);
3287 spin_unlock(&exp->exp_uncommitted_replies_lock);
3289 spin_lock(&exp->exp_lock);
3290 list_add_tail(&rs->rs_exp_list, &exp->exp_outstanding_replies);
3291 spin_unlock(&exp->exp_lock);
3293 netrc = target_send_reply_msg(req, rc, fail_id);
3295 spin_lock(&svcpt->scp_rep_lock);
3297 atomic_inc(&svcpt->scp_nreps_difficult);
3301 * error sending: reply is off the net. Also we need +1
3302 * reply ref until ptlrpc_handle_rs() is done
3303 * with the reply state (if the send was successful, there
3304 * would have been +1 ref for the net, which
3305 * reply_out_callback leaves alone)
3308 ptlrpc_rs_addref(rs);
3311 spin_lock(&rs->rs_lock);
3312 if (rs->rs_transno <= exp->exp_last_committed ||
3313 (!rs->rs_on_net && !rs->rs_no_ack) ||
3314 list_empty(&rs->rs_exp_list) || /* completed already */
3315 list_empty(&rs->rs_obd_list)) {
3316 CDEBUG(D_HA, "Schedule reply immediately\n");
3317 ptlrpc_dispatch_difficult_reply(rs);
3319 list_add(&rs->rs_list, &svcpt->scp_rep_active);
3320 rs->rs_scheduled = 0; /* allow notifier to schedule */
3322 spin_unlock(&rs->rs_lock);
3323 spin_unlock(&svcpt->scp_rep_lock);
3327 enum ldlm_mode lck_compat_array[] = {
3328 [LCK_EX] = LCK_COMPAT_EX,
3329 [LCK_PW] = LCK_COMPAT_PW,
3330 [LCK_PR] = LCK_COMPAT_PR,
3331 [LCK_CW] = LCK_COMPAT_CW,
3332 [LCK_CR] = LCK_COMPAT_CR,
3333 [LCK_NL] = LCK_COMPAT_NL,
3334 [LCK_GROUP] = LCK_COMPAT_GROUP,
3335 [LCK_COS] = LCK_COMPAT_COS,
3339 * Rather arbitrary mapping from LDLM error codes to errno values. This should
3340 * not escape to the user level.
3342 int ldlm_error2errno(enum ldlm_error error)
3348 case ELDLM_LOCK_MATCHED:
3351 case ELDLM_LOCK_CHANGED:
3354 case ELDLM_LOCK_ABORTED:
3357 case ELDLM_LOCK_REPLACED:
3360 case ELDLM_NO_LOCK_DATA:
3363 case ELDLM_NAMESPACE_EXISTS:
3366 case ELDLM_BAD_NAMESPACE:
3370 if (((int)error) < 0) { /* cast to signed type */
3371 result = error; /* as ldlm_error can be unsigned */
3373 CERROR("Invalid DLM result code: %d\n", error);
3379 EXPORT_SYMBOL(ldlm_error2errno);
3382 * Dual to ldlm_error2errno(): maps errno values back to enum ldlm_error.
3384 enum ldlm_error ldlm_errno2error(int err_no)
3393 error = ELDLM_LOCK_CHANGED;
3396 error = ELDLM_LOCK_ABORTED;
3399 error = ELDLM_LOCK_REPLACED;
3402 error = ELDLM_NO_LOCK_DATA;
3405 error = ELDLM_NAMESPACE_EXISTS;
3408 error = ELDLM_BAD_NAMESPACE;
3416 #if LUSTRE_TRACKS_LOCK_EXP_REFS
3417 void ldlm_dump_export_locks(struct obd_export *exp)
3419 spin_lock(&exp->exp_locks_list_guard);
3420 if (!list_empty(&exp->exp_locks_list)) {
3421 struct ldlm_lock *lock;
3423 CERROR("dumping locks for export %p, ignore if the unmount doesn't hang\n",
3425 list_for_each_entry(lock, &exp->exp_locks_list,
3427 LDLM_ERROR(lock, "lock:");
3429 spin_unlock(&exp->exp_locks_list_guard);
3433 #ifdef HAVE_SERVER_SUPPORT
3434 static inline const char *bulk2type(struct ptlrpc_request *req)
3436 if (req->rq_bulk_read)
3438 if (req->rq_bulk_write)
3443 int target_bulk_io(struct obd_export *exp, struct ptlrpc_bulk_desc *desc)
3445 struct ptlrpc_request *req = desc->bd_req;
3446 time64_t start = ktime_get_seconds();
3452 /* If there is eviction in progress, wait for it to finish. */
3454 exp->exp_obd->obd_evict_inprogress_waitq,
3455 !atomic_read(&exp->exp_obd->obd_evict_inprogress));
3457 /* Check if client was evicted or reconnected already. */
3458 if (exp->exp_failed ||
3459 exp->exp_conn_cnt > lustre_msg_get_conn_cnt(req->rq_reqmsg)) {
3462 if (req->rq_bulk_read)
3463 rc = sptlrpc_svc_wrap_bulk(req, desc);
3465 if (OCD_HAS_FLAG(&exp->exp_connect_data, BULK_MBITS))
3466 req->rq_mbits = lustre_msg_get_mbits(req->rq_reqmsg);
3467 else /* old version, bulk matchbits is rq_xid */
3468 req->rq_mbits = req->rq_xid;
3471 rc = ptlrpc_start_bulk_transfer(desc);
3475 DEBUG_REQ(D_ERROR, req, "bulk %s failed: rc = %d",
3476 bulk2type(req), rc);
3480 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE)) {
3481 ptlrpc_abort_bulk(desc);
3485 /* limit actual bulk transfer to bulk_timeout seconds */
3486 deadline = start + bulk_timeout;
3487 if (deadline > req->rq_deadline)
3488 deadline = req->rq_deadline;
3491 time64_t timeoutl = deadline - ktime_get_seconds();
3492 time64_t rq_deadline;
3494 while (timeoutl >= 0 &&
3495 wait_event_idle_timeout(
3497 !ptlrpc_server_bulk_active(desc) ||
3500 lustre_msg_get_conn_cnt(req->rq_reqmsg),
3501 timeoutl ? cfs_time_seconds(1) : 1) == 0)
3503 rc = timeoutl < 0 ? -ETIMEDOUT : 0;
3505 /* Wait again if we changed rq_deadline. */
3506 rq_deadline = READ_ONCE(req->rq_deadline);
3507 deadline = start + bulk_timeout;
3508 if (deadline > rq_deadline)
3509 deadline = rq_deadline;
3510 } while (rc == -ETIMEDOUT &&
3511 deadline > ktime_get_seconds());
3513 if (rc == -ETIMEDOUT) {
3514 DEBUG_REQ(D_ERROR, req, "timeout on bulk %s after %lld%+llds",
3515 bulk2type(req), deadline - start,
3516 ktime_get_real_seconds() - deadline);
3517 ptlrpc_abort_bulk(desc);
3518 } else if (exp->exp_failed) {
3519 DEBUG_REQ(D_ERROR, req, "Eviction on bulk %s",
3522 ptlrpc_abort_bulk(desc);
3523 } else if (exp->exp_conn_cnt >
3524 lustre_msg_get_conn_cnt(req->rq_reqmsg)) {
3525 DEBUG_REQ(D_ERROR, req, "Reconnect on bulk %s",
3527 /* We don't reply anyway. */
3529 ptlrpc_abort_bulk(desc);
3530 } else if (desc->bd_failure) {
3531 DEBUG_REQ(D_ERROR, req, "network error on bulk %s",
3533 /* XXX should this be a different errno? */
3536 if (req->rq_bulk_write)
3537 rc = sptlrpc_svc_unwrap_bulk(req, desc);
3538 if (rc == 0 && desc->bd_nob_transferred != desc->bd_nob) {
3539 DEBUG_REQ(D_ERROR, req, "truncated bulk %s %d(%d)",
3540 bulk2type(req), desc->bd_nob_transferred,
3542 /* XXX should this be a different errno? */
3549 EXPORT_SYMBOL(target_bulk_io);
3551 #endif /* HAVE_SERVER_SUPPORT */