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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2010, 2014, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 * This file deals with various client/target related logic including recovery.
40 * TODO: This code more logically belongs in the ptlrpc module than in ldlm and
44 #define DEBUG_SUBSYSTEM S_LDLM
46 #include <linux/kthread.h>
47 #include <libcfs/libcfs.h>
49 #include <obd_class.h>
50 #include <lustre_dlm.h>
51 #include <lustre_net.h>
52 #include <lustre_sec.h>
53 #include "ldlm_internal.h"
55 /* @priority: If non-zero, move the selected connection to the list head.
56 * @create: If zero, only search in existing connections.
58 static int import_set_conn(struct obd_import *imp, struct obd_uuid *uuid,
59 int priority, int create)
61 struct ptlrpc_connection *ptlrpc_conn;
62 struct obd_import_conn *imp_conn = NULL, *item;
66 if (!create && !priority) {
67 CDEBUG(D_HA, "Nothing to do\n");
71 ptlrpc_conn = ptlrpc_uuid_to_connection(uuid);
73 CDEBUG(D_HA, "can't find connection %s\n", uuid->uuid);
78 OBD_ALLOC(imp_conn, sizeof(*imp_conn));
80 GOTO(out_put, rc = -ENOMEM);
84 spin_lock(&imp->imp_lock);
85 list_for_each_entry(item, &imp->imp_conn_list, oic_item) {
86 if (obd_uuid_equals(uuid, &item->oic_uuid)) {
88 list_del(&item->oic_item);
89 list_add(&item->oic_item,
91 item->oic_last_attempt = 0;
93 CDEBUG(D_HA, "imp %p@%s: found existing conn %s%s\n",
94 imp, imp->imp_obd->obd_name, uuid->uuid,
95 (priority ? ", moved to head" : ""));
96 spin_unlock(&imp->imp_lock);
97 GOTO(out_free, rc = 0);
100 /* No existing import connection found for \a uuid. */
102 imp_conn->oic_conn = ptlrpc_conn;
103 imp_conn->oic_uuid = *uuid;
104 imp_conn->oic_last_attempt = 0;
106 list_add(&imp_conn->oic_item, &imp->imp_conn_list);
108 list_add_tail(&imp_conn->oic_item,
109 &imp->imp_conn_list);
110 CDEBUG(D_HA, "imp %p@%s: add connection %s at %s\n",
111 imp, imp->imp_obd->obd_name, uuid->uuid,
112 (priority ? "head" : "tail"));
114 spin_unlock(&imp->imp_lock);
115 GOTO(out_free, rc = -ENOENT);
118 spin_unlock(&imp->imp_lock);
122 OBD_FREE(imp_conn, sizeof(*imp_conn));
124 ptlrpc_connection_put(ptlrpc_conn);
128 int import_set_conn_priority(struct obd_import *imp, struct obd_uuid *uuid)
130 return import_set_conn(imp, uuid, 1, 0);
133 int client_import_add_conn(struct obd_import *imp, struct obd_uuid *uuid,
136 return import_set_conn(imp, uuid, priority, 1);
138 EXPORT_SYMBOL(client_import_add_conn);
140 int client_import_del_conn(struct obd_import *imp, struct obd_uuid *uuid)
142 struct obd_import_conn *imp_conn;
143 struct obd_export *dlmexp;
147 spin_lock(&imp->imp_lock);
148 if (list_empty(&imp->imp_conn_list)) {
149 LASSERT(!imp->imp_connection);
153 list_for_each_entry(imp_conn, &imp->imp_conn_list, oic_item) {
154 if (!obd_uuid_equals(uuid, &imp_conn->oic_uuid))
156 LASSERT(imp_conn->oic_conn);
158 if (imp_conn == imp->imp_conn_current) {
159 LASSERT(imp_conn->oic_conn == imp->imp_connection);
161 if (imp->imp_state != LUSTRE_IMP_CLOSED &&
162 imp->imp_state != LUSTRE_IMP_DISCON) {
163 CERROR("can't remove current connection\n");
164 GOTO(out, rc = -EBUSY);
167 ptlrpc_connection_put(imp->imp_connection);
168 imp->imp_connection = NULL;
170 dlmexp = class_conn2export(&imp->imp_dlm_handle);
171 if (dlmexp && dlmexp->exp_connection) {
172 LASSERT(dlmexp->exp_connection ==
174 ptlrpc_connection_put(dlmexp->exp_connection);
175 dlmexp->exp_connection = NULL;
179 list_del(&imp_conn->oic_item);
180 ptlrpc_connection_put(imp_conn->oic_conn);
181 OBD_FREE(imp_conn, sizeof(*imp_conn));
182 CDEBUG(D_HA, "imp %p@%s: remove connection %s\n",
183 imp, imp->imp_obd->obd_name, uuid->uuid);
188 spin_unlock(&imp->imp_lock);
190 CERROR("connection %s not found\n", uuid->uuid);
193 EXPORT_SYMBOL(client_import_del_conn);
196 * Find conn UUID by peer NID. \a peer is a server NID. This function is used
197 * to find a conn uuid of \a imp which can reach \a peer.
199 int client_import_find_conn(struct obd_import *imp, lnet_nid_t peer,
200 struct obd_uuid *uuid)
202 struct obd_import_conn *conn;
206 spin_lock(&imp->imp_lock);
207 list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
208 /* Check if conn UUID does have this peer NID. */
209 if (class_check_uuid(&conn->oic_uuid, peer)) {
210 *uuid = conn->oic_uuid;
215 spin_unlock(&imp->imp_lock);
218 EXPORT_SYMBOL(client_import_find_conn);
220 void client_destroy_import(struct obd_import *imp)
222 /* Drop security policy instance after all RPCs have finished/aborted
223 * to let all busy contexts be released. */
224 class_import_get(imp);
225 class_destroy_import(imp);
226 sptlrpc_import_sec_put(imp);
227 class_import_put(imp);
229 EXPORT_SYMBOL(client_destroy_import);
232 * Check whether or not the OSC is on MDT.
235 * setup 0:{fsname}-OSTxxxx-osc[-MDTxxxx] 1:lustre-OST0000_UUID 2:NID
237 * setup 0:{fsname}-OSTxxxx-osc 1:lustre-OST0000_UUID 2:NID
240 static int osc_on_mdt(char *obdname)
244 ptr = strrchr(obdname, '-');
248 if (strncmp(ptr + 1, "MDT", 3) == 0)
254 /* Configure an RPC client OBD device.
259 * 3 - inactive-on-startup
261 int client_obd_setup(struct obd_device *obddev, struct lustre_cfg *lcfg)
263 struct client_obd *cli = &obddev->u.cli;
264 struct obd_import *imp;
265 struct obd_uuid server_uuid;
266 int rq_portal, rp_portal, connect_op;
267 char *name = obddev->obd_type->typ_name;
268 enum ldlm_ns_type ns_type = LDLM_NS_TYPE_UNKNOWN;
269 char *cli_name = lustre_cfg_buf(lcfg, 0);
273 /* In a more perfect world, we would hang a ptlrpc_client off of
274 * obd_type and just use the values from there. */
275 if (!strcmp(name, LUSTRE_OSC_NAME)) {
276 rq_portal = OST_REQUEST_PORTAL;
277 rp_portal = OSC_REPLY_PORTAL;
278 connect_op = OST_CONNECT;
279 cli->cl_sp_me = LUSTRE_SP_CLI;
280 cli->cl_sp_to = LUSTRE_SP_OST;
281 ns_type = LDLM_NS_TYPE_OSC;
282 } else if (!strcmp(name, LUSTRE_MDC_NAME) ||
283 !strcmp(name, LUSTRE_LWP_NAME)) {
284 rq_portal = MDS_REQUEST_PORTAL;
285 rp_portal = MDC_REPLY_PORTAL;
286 connect_op = MDS_CONNECT;
287 if (is_lwp_on_ost(cli_name))
288 cli->cl_sp_me = LUSTRE_SP_OST;
289 else if (is_lwp_on_mdt(cli_name))
290 cli->cl_sp_me = LUSTRE_SP_MDT;
292 cli->cl_sp_me = LUSTRE_SP_CLI;
293 cli->cl_sp_to = LUSTRE_SP_MDT;
294 ns_type = LDLM_NS_TYPE_MDC;
295 } else if (!strcmp(name, LUSTRE_OSP_NAME)) {
296 if (strstr(lustre_cfg_buf(lcfg, 1), "OST") == NULL) {
297 /* OSP_on_MDT for other MDTs */
298 connect_op = MDS_CONNECT;
299 cli->cl_sp_to = LUSTRE_SP_MDT;
300 ns_type = LDLM_NS_TYPE_MDC;
301 rq_portal = OUT_PORTAL;
303 /* OSP on MDT for OST */
304 connect_op = OST_CONNECT;
305 cli->cl_sp_to = LUSTRE_SP_OST;
306 ns_type = LDLM_NS_TYPE_OSC;
307 rq_portal = OST_REQUEST_PORTAL;
309 rp_portal = OSC_REPLY_PORTAL;
310 cli->cl_sp_me = LUSTRE_SP_MDT;
311 } else if (!strcmp(name, LUSTRE_MGC_NAME)) {
312 rq_portal = MGS_REQUEST_PORTAL;
313 rp_portal = MGC_REPLY_PORTAL;
314 connect_op = MGS_CONNECT;
315 cli->cl_sp_me = LUSTRE_SP_MGC;
316 cli->cl_sp_to = LUSTRE_SP_MGS;
317 cli->cl_flvr_mgc.sf_rpc = SPTLRPC_FLVR_INVALID;
318 ns_type = LDLM_NS_TYPE_MGC;
320 CERROR("unknown client OBD type \"%s\", can't setup\n",
325 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
326 CERROR("requires a TARGET UUID\n");
330 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > 37) {
331 CERROR("client UUID must be less than 38 characters\n");
335 if (LUSTRE_CFG_BUFLEN(lcfg, 2) < 1) {
336 CERROR("setup requires a SERVER UUID\n");
340 if (LUSTRE_CFG_BUFLEN(lcfg, 2) > 37) {
341 CERROR("target UUID must be less than 38 characters\n");
345 init_rwsem(&cli->cl_sem);
346 mutex_init(&cli->cl_mgc_mutex);
347 cli->cl_conn_count = 0;
348 memcpy(server_uuid.uuid, lustre_cfg_buf(lcfg, 2),
349 min_t(unsigned int, LUSTRE_CFG_BUFLEN(lcfg, 2),
350 sizeof(server_uuid)));
352 cli->cl_dirty_pages = 0;
353 cli->cl_avail_grant = 0;
354 /* FIXME: Should limit this for the sum of all cl_dirty_max_pages. */
355 /* cl_dirty_max_pages may be changed at connect time in
356 * ptlrpc_connect_interpret(). */
357 client_adjust_max_dirty(cli);
358 INIT_LIST_HEAD(&cli->cl_cache_waiters);
359 INIT_LIST_HEAD(&cli->cl_loi_ready_list);
360 INIT_LIST_HEAD(&cli->cl_loi_hp_ready_list);
361 INIT_LIST_HEAD(&cli->cl_loi_write_list);
362 INIT_LIST_HEAD(&cli->cl_loi_read_list);
363 spin_lock_init(&cli->cl_loi_list_lock);
364 atomic_set(&cli->cl_pending_w_pages, 0);
365 atomic_set(&cli->cl_pending_r_pages, 0);
366 cli->cl_r_in_flight = 0;
367 cli->cl_w_in_flight = 0;
369 spin_lock_init(&cli->cl_read_rpc_hist.oh_lock);
370 spin_lock_init(&cli->cl_write_rpc_hist.oh_lock);
371 spin_lock_init(&cli->cl_read_page_hist.oh_lock);
372 spin_lock_init(&cli->cl_write_page_hist.oh_lock);
373 spin_lock_init(&cli->cl_read_offset_hist.oh_lock);
374 spin_lock_init(&cli->cl_write_offset_hist.oh_lock);
377 INIT_LIST_HEAD(&cli->cl_lru_osc);
378 atomic_set(&cli->cl_lru_shrinkers, 0);
379 atomic_long_set(&cli->cl_lru_busy, 0);
380 atomic_long_set(&cli->cl_lru_in_list, 0);
381 INIT_LIST_HEAD(&cli->cl_lru_list);
382 spin_lock_init(&cli->cl_lru_list_lock);
383 atomic_long_set(&cli->cl_unstable_count, 0);
385 init_waitqueue_head(&cli->cl_destroy_waitq);
386 atomic_set(&cli->cl_destroy_in_flight, 0);
387 #ifdef ENABLE_CHECKSUM
388 /* Turn on checksumming by default. */
389 cli->cl_checksum = 1;
391 * The supported checksum types will be worked out at connect time
392 * Set cl_chksum* to CRC32 for now to avoid returning screwed info
395 cli->cl_cksum_type = cli->cl_supp_cksum_types = OBD_CKSUM_CRC32;
397 atomic_set(&cli->cl_resends, OSC_DEFAULT_RESENDS);
399 /* This value may be reduced at connect time in
400 * ptlrpc_connect_interpret() . We initialize it to only
401 * 1MB until we know what the performance looks like.
402 * In the future this should likely be increased. LU-1431 */
403 cli->cl_max_pages_per_rpc = min_t(int, PTLRPC_MAX_BRW_PAGES,
404 LNET_MTU >> PAGE_CACHE_SHIFT);
406 /* set cl_chunkbits default value to PAGE_CACHE_SHIFT,
407 * it will be updated at OSC connection time. */
408 cli->cl_chunkbits = PAGE_CACHE_SHIFT;
410 if (!strcmp(name, LUSTRE_MDC_NAME)) {
411 cli->cl_max_rpcs_in_flight = OBD_MAX_RIF_DEFAULT;
412 } else if (totalram_pages >> (20 - PAGE_CACHE_SHIFT) <= 128 /* MB */) {
413 cli->cl_max_rpcs_in_flight = 2;
414 } else if (totalram_pages >> (20 - PAGE_CACHE_SHIFT) <= 256 /* MB */) {
415 cli->cl_max_rpcs_in_flight = 3;
416 } else if (totalram_pages >> (20 - PAGE_CACHE_SHIFT) <= 512 /* MB */) {
417 cli->cl_max_rpcs_in_flight = 4;
419 if (osc_on_mdt(obddev->obd_name))
420 cli->cl_max_rpcs_in_flight = OBD_MAX_RIF_MAX;
422 cli->cl_max_rpcs_in_flight = OBD_MAX_RIF_DEFAULT;
425 spin_lock_init(&cli->cl_mod_rpcs_lock);
426 spin_lock_init(&cli->cl_mod_rpcs_hist.oh_lock);
427 cli->cl_max_mod_rpcs_in_flight = 0;
428 cli->cl_mod_rpcs_in_flight = 0;
429 cli->cl_close_rpcs_in_flight = 0;
430 init_waitqueue_head(&cli->cl_mod_rpcs_waitq);
431 cli->cl_mod_tag_bitmap = NULL;
433 if (connect_op == MDS_CONNECT) {
434 cli->cl_max_mod_rpcs_in_flight = cli->cl_max_rpcs_in_flight - 1;
435 OBD_ALLOC(cli->cl_mod_tag_bitmap,
436 BITS_TO_LONGS(OBD_MAX_RIF_MAX) * sizeof(long));
437 if (cli->cl_mod_tag_bitmap == NULL)
438 GOTO(err, rc = -ENOMEM);
443 CERROR("ldlm_get_ref failed: %d\n", rc);
447 ptlrpc_init_client(rq_portal, rp_portal, name,
448 &obddev->obd_ldlm_client);
450 imp = class_new_import(obddev);
452 GOTO(err_ldlm, rc = -ENOENT);
453 imp->imp_client = &obddev->obd_ldlm_client;
454 imp->imp_connect_op = connect_op;
455 memcpy(cli->cl_target_uuid.uuid, lustre_cfg_buf(lcfg, 1),
456 LUSTRE_CFG_BUFLEN(lcfg, 1));
457 class_import_put(imp);
459 rc = client_import_add_conn(imp, &server_uuid, 1);
461 CERROR("can't add initial connection\n");
462 GOTO(err_import, rc);
465 cli->cl_import = imp;
466 /* cli->cl_max_mds_easize updated by mdc_init_ea_size() */
467 cli->cl_max_mds_easize = sizeof(struct lov_mds_md_v3);
469 if (LUSTRE_CFG_BUFLEN(lcfg, 3) > 0) {
470 if (!strcmp(lustre_cfg_string(lcfg, 3), "inactive")) {
471 CDEBUG(D_HA, "marking %s %s->%s as inactive\n",
472 name, obddev->obd_name,
473 cli->cl_target_uuid.uuid);
474 spin_lock(&imp->imp_lock);
475 imp->imp_deactive = 1;
476 spin_unlock(&imp->imp_lock);
480 obddev->obd_namespace = ldlm_namespace_new(obddev, obddev->obd_name,
481 LDLM_NAMESPACE_CLIENT,
482 LDLM_NAMESPACE_GREEDY,
484 if (obddev->obd_namespace == NULL) {
485 CERROR("Unable to create client namespace - %s\n",
487 GOTO(err_import, rc = -ENOMEM);
493 class_destroy_import(imp);
497 if (cli->cl_mod_tag_bitmap != NULL)
498 OBD_FREE(cli->cl_mod_tag_bitmap,
499 BITS_TO_LONGS(OBD_MAX_RIF_MAX) * sizeof(long));
500 cli->cl_mod_tag_bitmap = NULL;
504 EXPORT_SYMBOL(client_obd_setup);
506 int client_obd_cleanup(struct obd_device *obddev)
508 struct client_obd *cli = &obddev->u.cli;
511 ldlm_namespace_free_post(obddev->obd_namespace);
512 obddev->obd_namespace = NULL;
514 obd_cleanup_client_import(obddev);
515 LASSERT(obddev->u.cli.cl_import == NULL);
519 if (cli->cl_mod_tag_bitmap != NULL)
520 OBD_FREE(cli->cl_mod_tag_bitmap,
521 BITS_TO_LONGS(OBD_MAX_RIF_MAX) * sizeof(long));
522 cli->cl_mod_tag_bitmap = NULL;
526 EXPORT_SYMBOL(client_obd_cleanup);
528 /* ->o_connect() method for client side (OSC and MDC and MGC) */
529 int client_connect_import(const struct lu_env *env,
530 struct obd_export **exp,
531 struct obd_device *obd, struct obd_uuid *cluuid,
532 struct obd_connect_data *data, void *localdata)
534 struct client_obd *cli = &obd->u.cli;
535 struct obd_import *imp = cli->cl_import;
536 struct obd_connect_data *ocd;
537 struct lustre_handle conn = { 0 };
543 down_write(&cli->cl_sem);
544 if (cli->cl_conn_count > 0)
545 GOTO(out_sem, rc = -EALREADY);
547 rc = class_connect(&conn, obd, cluuid);
551 cli->cl_conn_count++;
552 *exp = class_conn2export(&conn);
554 LASSERT(obd->obd_namespace);
556 imp->imp_dlm_handle = conn;
557 rc = ptlrpc_init_import(imp);
561 ocd = &imp->imp_connect_data;
564 is_mdc = strncmp(imp->imp_obd->obd_type->typ_name,
565 LUSTRE_MDC_NAME, 3) == 0;
567 data->ocd_connect_flags |= OBD_CONNECT_MULTIMODRPCS;
568 imp->imp_connect_flags_orig = data->ocd_connect_flags;
571 rc = ptlrpc_connect_import(imp);
573 LASSERT (imp->imp_state == LUSTRE_IMP_DISCON);
576 LASSERT(*exp != NULL && (*exp)->exp_connection);
579 LASSERTF((ocd->ocd_connect_flags & data->ocd_connect_flags) ==
580 ocd->ocd_connect_flags, "old "LPX64", new "LPX64"\n",
581 data->ocd_connect_flags, ocd->ocd_connect_flags);
582 data->ocd_connect_flags = ocd->ocd_connect_flags;
583 /* clear the flag as it was not set and is not known
586 data->ocd_connect_flags &= ~OBD_CONNECT_MULTIMODRPCS;
589 ptlrpc_pinger_add_import(imp);
595 cli->cl_conn_count--;
596 class_disconnect(*exp);
600 up_write(&cli->cl_sem);
604 EXPORT_SYMBOL(client_connect_import);
606 int client_disconnect_export(struct obd_export *exp)
608 struct obd_device *obd = class_exp2obd(exp);
609 struct client_obd *cli;
610 struct obd_import *imp;
615 CERROR("invalid export for disconnect: exp %p cookie "LPX64"\n",
616 exp, exp ? exp->exp_handle.h_cookie : -1);
621 imp = cli->cl_import;
623 down_write(&cli->cl_sem);
624 CDEBUG(D_INFO, "disconnect %s - %zu\n", obd->obd_name,
627 if (cli->cl_conn_count == 0) {
628 CERROR("disconnecting disconnected device (%s)\n",
630 GOTO(out_disconnect, rc = -EINVAL);
633 cli->cl_conn_count--;
634 if (cli->cl_conn_count != 0)
635 GOTO(out_disconnect, rc = 0);
637 /* Mark import deactivated now, so we don't try to reconnect if any
638 * of the cleanup RPCs fails (e.g. LDLM cancel, etc). We don't
639 * fully deactivate the import, or that would drop all requests. */
640 spin_lock(&imp->imp_lock);
641 imp->imp_deactive = 1;
642 spin_unlock(&imp->imp_lock);
644 /* Some non-replayable imports (MDS's OSCs) are pinged, so just
645 * delete it regardless. (It's safe to delete an import that was
647 (void)ptlrpc_pinger_del_import(imp);
649 if (obd->obd_namespace != NULL) {
650 /* obd_force == local only */
651 ldlm_cli_cancel_unused(obd->obd_namespace, NULL,
652 obd->obd_force ? LCF_LOCAL : 0, NULL);
653 ldlm_namespace_free_prior(obd->obd_namespace, imp, obd->obd_force);
656 /* There's no need to hold sem while disconnecting an import,
657 * and it may actually cause deadlock in GSS. */
658 up_write(&cli->cl_sem);
659 rc = ptlrpc_disconnect_import(imp, 0);
660 down_write(&cli->cl_sem);
662 ptlrpc_invalidate_import(imp);
667 /* Use server style - class_disconnect should be always called for
669 err = class_disconnect(exp);
673 up_write(&cli->cl_sem);
677 EXPORT_SYMBOL(client_disconnect_export);
679 #ifdef HAVE_SERVER_SUPPORT
680 int server_disconnect_export(struct obd_export *exp)
685 /* Disconnect early so that clients can't keep using export. */
686 rc = class_disconnect(exp);
687 /* Close import to avoid sending any requests. */
688 if (exp->exp_imp_reverse)
689 ptlrpc_cleanup_imp(exp->exp_imp_reverse);
691 ldlm_bl_thread_wakeup();
693 /* complete all outstanding replies */
694 spin_lock(&exp->exp_lock);
695 while (!list_empty(&exp->exp_outstanding_replies)) {
696 struct ptlrpc_reply_state *rs =
697 list_entry(exp->exp_outstanding_replies.next,
698 struct ptlrpc_reply_state, rs_exp_list);
699 struct ptlrpc_service_part *svcpt = rs->rs_svcpt;
701 spin_lock(&svcpt->scp_rep_lock);
703 list_del_init(&rs->rs_exp_list);
704 spin_lock(&rs->rs_lock);
705 ptlrpc_schedule_difficult_reply(rs);
706 spin_unlock(&rs->rs_lock);
708 spin_unlock(&svcpt->scp_rep_lock);
710 spin_unlock(&exp->exp_lock);
714 EXPORT_SYMBOL(server_disconnect_export);
716 /* --------------------------------------------------------------------------
717 * from old lib/target.c
718 * -------------------------------------------------------------------------- */
720 static int target_handle_reconnect(struct lustre_handle *conn,
721 struct obd_export *exp,
722 struct obd_uuid *cluuid)
724 struct lustre_handle *hdl;
727 hdl = &exp->exp_imp_reverse->imp_remote_handle;
728 if (exp->exp_connection && lustre_handle_is_used(hdl)) {
729 struct obd_device *target;
731 target = exp->exp_obd;
733 /* Might be a re-connect after a partition. */
734 if (!memcmp(&conn->cookie, &hdl->cookie, sizeof conn->cookie)) {
735 if (target->obd_recovering) {
736 int timeout = cfs_duration_sec(cfs_time_sub(
738 &target->obd_recovery_timer),
739 cfs_time_current()));
741 LCONSOLE_WARN("%s: Client %s (at %s) reconnect"
742 "ing, waiting for %d clients in recov"
743 "ery for %d:%.02d\n", target->obd_name,
744 obd_uuid2str(&exp->exp_client_uuid),
745 obd_export_nid2str(exp),
746 target->obd_max_recoverable_clients,
747 timeout / 60, timeout % 60);
749 LCONSOLE_WARN("%s: Client %s (at %s) "
750 "reconnecting\n", target->obd_name,
751 obd_uuid2str(&exp->exp_client_uuid),
752 obd_export_nid2str(exp));
755 conn->cookie = exp->exp_handle.h_cookie;
756 /* target_handle_connect() treats EALREADY and
757 * -EALREADY differently. EALREADY means we are
758 * doing a valid reconnect from the same client. */
761 LCONSOLE_WARN("%s: already connected client %s (at %s) "
762 "with handle "LPX64". Rejecting client "
763 "with the same UUID trying to reconnect "
764 "with handle "LPX64"\n", target->obd_name,
765 obd_uuid2str(&exp->exp_client_uuid),
766 obd_export_nid2str(exp),
767 hdl->cookie, conn->cookie);
768 memset(conn, 0, sizeof *conn);
769 /* target_handle_connect() treats EALREADY and
770 * -EALREADY differently. -EALREADY is an error
771 * (same UUID, different handle). */
776 conn->cookie = exp->exp_handle.h_cookie;
777 CDEBUG(D_HA, "connect export for UUID '%s' at %p, cookie "LPX64"\n",
778 cluuid->uuid, exp, conn->cookie);
782 void target_client_add_cb(struct obd_device *obd, __u64 transno, void *cb_data,
785 struct obd_export *exp = cb_data;
787 CDEBUG(D_RPCTRACE, "%s: committing for initial connect of %s\n",
788 obd->obd_name, exp->exp_client_uuid.uuid);
790 spin_lock(&exp->exp_lock);
791 exp->exp_need_sync = 0;
792 spin_unlock(&exp->exp_lock);
793 class_export_cb_put(exp);
797 check_and_start_recovery_timer(struct obd_device *obd,
798 struct ptlrpc_request *req, int new_client);
801 * update flags for import during reconnect process
803 static int rev_import_flags_update(struct obd_import *revimp,
804 struct ptlrpc_request *req)
807 struct obd_connect_data *data;
809 data = req_capsule_client_get(&req->rq_pill, &RMF_CONNECT_DATA);
811 if (data->ocd_connect_flags & OBD_CONNECT_AT)
812 revimp->imp_msghdr_flags |= MSGHDR_AT_SUPPORT;
814 revimp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
816 revimp->imp_msghdr_flags |= MSGHDR_CKSUM_INCOMPAT18;
818 rc = sptlrpc_import_sec_adapt(revimp, req->rq_svc_ctx, &req->rq_flvr);
820 CERROR("%s: cannot get reverse import %s security: rc = %d\n",
821 revimp->imp_client->cli_name,
822 libcfs_id2str(req->rq_peer), rc);
830 * Allocate a new reverse import for an export.
832 * \retval -errno in case error hit
833 * \retval 0 if reverse import correctly init
835 int rev_import_init(struct obd_export *export)
837 struct obd_device *obd = export->exp_obd;
838 struct obd_import *revimp;
840 LASSERT(export->exp_imp_reverse == NULL);
842 revimp = class_new_import(obd);
846 revimp->imp_remote_handle.cookie = 0ULL;
847 revimp->imp_client = &obd->obd_ldlm_client;
848 revimp->imp_dlm_fake = 1;
850 /* it is safe to connect import in new state as no sends possible */
851 spin_lock(&export->exp_lock);
852 export->exp_imp_reverse = revimp;
853 spin_unlock(&export->exp_lock);
854 class_import_put(revimp);
858 EXPORT_SYMBOL(rev_import_init);
861 * Handle reconnect for an export.
863 * \param exp export to handle reconnect process
864 * \param req client reconnect request
866 * \retval -rc in case securitfy flavor can't be changed
867 * \retval 0 in case none problems
869 static int rev_import_reconnect(struct obd_export *exp,
870 struct ptlrpc_request *req)
872 struct obd_import *revimp = exp->exp_imp_reverse;
873 struct lustre_handle *lh;
876 /* avoid sending a request until import flags are changed */
877 ptlrpc_import_enter_resend(revimp);
879 if (revimp->imp_connection != NULL)
880 ptlrpc_connection_put(revimp->imp_connection);
883 * client from recovery don't have a handle so we need to take from
884 * request. it may produce situation when wrong client connected
885 * to recovery as we trust a client uuid
887 lh = req_capsule_client_get(&req->rq_pill, &RMF_CONN);
888 revimp->imp_remote_handle = *lh;
890 /* unknown versions will be caught in
891 * ptlrpc_handle_server_req_in->lustre_unpack_msg() */
892 revimp->imp_msg_magic = req->rq_reqmsg->lm_magic;
894 revimp->imp_connection = ptlrpc_connection_addref(exp->exp_connection);
896 rc = rev_import_flags_update(revimp, req);
898 /* it is safe to still be in RECOVERY phase as we are not able
899 * to setup correct security flavor so requests are not able to
900 * be delivered correctly */
904 /* resend all rpc's via new connection */
905 return ptlrpc_import_recovery_state_machine(revimp);
908 int target_handle_connect(struct ptlrpc_request *req)
910 struct obd_device *target = NULL;
911 struct obd_export *export = NULL;
912 /* connect handle - filled from target_handle_reconnect in
914 struct lustre_handle conn;
915 struct lustre_handle *tmp;
916 struct obd_uuid tgtuuid;
917 struct obd_uuid cluuid;
922 bool mds_conn = false, lw_client = false;
923 bool mds_mds_conn = false;
924 bool new_mds_mds_conn = false;
925 struct obd_connect_data *data, *tmpdata;
927 lnet_nid_t *client_nid = NULL;
930 OBD_RACE(OBD_FAIL_TGT_CONN_RACE);
932 str = req_capsule_client_get(&req->rq_pill, &RMF_TGTUUID);
934 DEBUG_REQ(D_ERROR, req, "bad target UUID for connect");
935 GOTO(out, rc = -EINVAL);
938 obd_str2uuid(&tgtuuid, str);
939 target = class_uuid2obd(&tgtuuid);
941 target = class_name2obd(str);
944 deuuidify(str, NULL, &target_start, &target_len);
945 LCONSOLE_ERROR_MSG(0x137, "%s: not available for connect "
946 "from %s (no target). If you are running "
947 "an HA pair check that the target is "
948 "mounted on the other server.\n", str,
949 libcfs_nid2str(req->rq_peer.nid));
950 GOTO(out, rc = -ENODEV);
953 spin_lock(&target->obd_dev_lock);
954 /* Make sure the target isn't cleaned up while we're here. Yes,
955 * there's still a race between the above check and our incref here.
956 * Really, class_uuid2obd should take the ref. */
957 class_incref(target, __func__, current);
959 if (target->obd_stopping || !target->obd_set_up) {
960 spin_unlock(&target->obd_dev_lock);
962 deuuidify(str, NULL, &target_start, &target_len);
963 LCONSOLE_INFO("%.*s: Not available for connect from %s (%s)\n",
964 target_len, target_start,
965 libcfs_nid2str(req->rq_peer.nid),
966 (target->obd_stopping ?
967 "stopping" : "not set up"));
968 GOTO(out, rc = -ENODEV);
971 if (target->obd_no_conn) {
972 spin_unlock(&target->obd_dev_lock);
974 CDEBUG(D_INFO, "%s: Temporarily refusing client connection "
975 "from %s\n", target->obd_name,
976 libcfs_nid2str(req->rq_peer.nid));
977 GOTO(out, rc = -EAGAIN);
980 target->obd_conn_inprogress++;
981 spin_unlock(&target->obd_dev_lock);
983 str = req_capsule_client_get(&req->rq_pill, &RMF_CLUUID);
985 DEBUG_REQ(D_ERROR, req, "bad client UUID for connect");
986 GOTO(out, rc = -EINVAL);
989 obd_str2uuid(&cluuid, str);
991 tmp = req_capsule_client_get(&req->rq_pill, &RMF_CONN);
993 GOTO(out, rc = -EPROTO);
997 size = req_capsule_get_size(&req->rq_pill, &RMF_CONNECT_DATA,
999 data = req_capsule_client_get(&req->rq_pill, &RMF_CONNECT_DATA);
1001 GOTO(out, rc = -EPROTO);
1003 rc = req_capsule_server_pack(&req->rq_pill);
1007 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
1008 /* Don't allow clients to connect that are using old 1.8 format
1009 * protocol conventions (LUSTRE_MSG_MAGIC_v1, !MSGHDR_CKSUM_INCOMPAT18,
1010 * ldlm_flock_policy_wire format, MDT_ATTR_xTIME_SET, etc). The
1011 * FULL20 flag should be set on all connections since 2.0, but no
1012 * longer affects behaviour.
1014 * Later this check will be disabled and the flag can be retired
1015 * completely once interop with 3.0 is no longer needed.
1017 if (!(data->ocd_connect_flags & OBD_CONNECT_FULL20))
1018 GOTO(out, rc = -EPROTO);
1021 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT) {
1022 if (data->ocd_version < LUSTRE_VERSION_CODE -
1023 LUSTRE_VERSION_ALLOWED_OFFSET ||
1024 data->ocd_version > LUSTRE_VERSION_CODE +
1025 LUSTRE_VERSION_ALLOWED_OFFSET) {
1026 DEBUG_REQ(D_WARNING, req, "Refusing %s (%d.%d.%d.%d) "
1027 "libclient connection attempt",
1028 data->ocd_version < LUSTRE_VERSION_CODE ?
1030 OBD_OCD_VERSION_MAJOR(data->ocd_version),
1031 OBD_OCD_VERSION_MINOR(data->ocd_version),
1032 OBD_OCD_VERSION_PATCH(data->ocd_version),
1033 OBD_OCD_VERSION_FIX(data->ocd_version));
1034 data = req_capsule_server_sized_get(&req->rq_pill,
1036 offsetof(typeof(*data), ocd_version) +
1037 sizeof(data->ocd_version));
1039 data->ocd_connect_flags = OBD_CONNECT_VERSION;
1040 data->ocd_version = LUSTRE_VERSION_CODE;
1042 GOTO(out, rc = -EPROTO);
1046 /* Note: lw_client is needed in MDS-MDS failover during update log
1047 * processing, so we needs to allow lw_client to be connected at
1048 * anytime, instead of only the initial connection */
1049 lw_client = (data->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT) != 0;
1051 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_INITIAL) {
1052 mds_conn = (data->ocd_connect_flags & OBD_CONNECT_MDS) != 0;
1053 mds_mds_conn = (data->ocd_connect_flags &
1054 OBD_CONNECT_MDS_MDS) != 0;
1056 /* OBD_CONNECT_MNE_SWAB is defined as OBD_CONNECT_MDS_MDS
1057 * for Imperative Recovery connection from MGC to MGS.
1059 * Via check OBD_CONNECT_FID, we can distinguish whether
1060 * the OBD_CONNECT_MDS_MDS/OBD_CONNECT_MNE_SWAB is from
1063 (data->ocd_connect_flags & OBD_CONNECT_MDS_MDS) &&
1064 (data->ocd_connect_flags & OBD_CONNECT_FID) &&
1065 (data->ocd_connect_flags & OBD_CONNECT_VERSION)) {
1066 __u32 major = OBD_OCD_VERSION_MAJOR(data->ocd_version);
1067 __u32 minor = OBD_OCD_VERSION_MINOR(data->ocd_version);
1068 __u32 patch = OBD_OCD_VERSION_PATCH(data->ocd_version);
1070 /* We do not support the MDT-MDT interoperations with
1071 * different version MDT because of protocol changes. */
1072 if (unlikely(major != LUSTRE_MAJOR ||
1073 minor != LUSTRE_MINOR ||
1074 abs(patch - LUSTRE_PATCH) > 3)) {
1075 LCONSOLE_WARN("%s (%u.%u.%u.%u) refused the "
1076 "connection from different version MDT "
1077 "(%d.%d.%d.%d) %s %s\n",
1078 target->obd_name, LUSTRE_MAJOR,
1079 LUSTRE_MINOR, LUSTRE_PATCH, LUSTRE_FIX,
1080 major, minor, patch,
1081 OBD_OCD_VERSION_FIX(data->ocd_version),
1082 libcfs_nid2str(req->rq_peer.nid), str);
1084 GOTO(out, rc = -EPROTO);
1089 /* lctl gets a backstage, all-access pass. */
1090 if (obd_uuid_equals(&cluuid, &target->obd_uuid))
1091 goto dont_check_exports;
1093 export = cfs_hash_lookup(target->obd_uuid_hash, &cluuid);
1097 /* We've found an export in the hash. */
1099 spin_lock(&export->exp_lock);
1101 if (export->exp_connecting) { /* bug 9635, et. al. */
1102 spin_unlock(&export->exp_lock);
1103 LCONSOLE_WARN("%s: Export %p already connecting from %s\n",
1104 export->exp_obd->obd_name, export,
1105 libcfs_nid2str(req->rq_peer.nid));
1106 class_export_put(export);
1109 } else if ((mds_conn || lw_client) && export->exp_connection != NULL) {
1110 spin_unlock(&export->exp_lock);
1111 if (req->rq_peer.nid != export->exp_connection->c_peer.nid)
1112 /* MDS or LWP reconnected after failover. */
1113 LCONSOLE_WARN("%s: Received %s connection from "
1114 "%s, removing former export from %s\n",
1115 target->obd_name, mds_conn ? "MDS" : "LWP",
1116 libcfs_nid2str(req->rq_peer.nid),
1117 libcfs_nid2str(export->exp_connection->c_peer.nid));
1119 /* New MDS connection from the same NID. */
1120 LCONSOLE_WARN("%s: Received new %s connection from "
1121 "%s, removing former export from same NID\n",
1122 target->obd_name, mds_conn ? "MDS" : "LWP",
1123 libcfs_nid2str(req->rq_peer.nid));
1124 class_fail_export(export);
1125 class_export_put(export);
1128 } else if (export->exp_connection != NULL &&
1129 req->rq_peer.nid != export->exp_connection->c_peer.nid &&
1130 (lustre_msg_get_op_flags(req->rq_reqmsg) &
1131 MSG_CONNECT_INITIAL)) {
1132 spin_unlock(&export->exp_lock);
1133 /* In MDS failover we have static UUID but NID can change. */
1134 LCONSOLE_WARN("%s: Client %s seen on new nid %s when "
1135 "existing nid %s is already connected\n",
1136 target->obd_name, cluuid.uuid,
1137 libcfs_nid2str(req->rq_peer.nid),
1139 export->exp_connection->c_peer.nid));
1141 class_export_put(export);
1144 export->exp_connecting = 1;
1145 spin_unlock(&export->exp_lock);
1146 LASSERT(export->exp_obd == target);
1148 rc = target_handle_reconnect(&conn, export, &cluuid);
1151 /* If we found an export, we already unlocked. */
1154 OBD_FAIL_TIMEOUT(OBD_FAIL_TGT_DELAY_CONNECT, 2 * obd_timeout);
1155 } else if (req->rq_export == NULL &&
1156 atomic_read(&export->exp_rpc_count) > 0) {
1157 LCONSOLE_WARN("%s: Client %s (at %s) refused connection, "
1158 "still busy with %d references\n",
1159 target->obd_name, cluuid.uuid,
1160 libcfs_nid2str(req->rq_peer.nid),
1161 atomic_read(&export->exp_refcount));
1162 GOTO(out, rc = -EBUSY);
1163 } else if (lustre_msg_get_conn_cnt(req->rq_reqmsg) == 1) {
1164 if (!strstr(cluuid.uuid, "mdt"))
1165 LCONSOLE_WARN("%s: Rejecting reconnect from the "
1166 "known client %s (at %s) because it "
1167 "is indicating it is a new client",
1168 target->obd_name, cluuid.uuid,
1169 libcfs_nid2str(req->rq_peer.nid));
1170 GOTO(out, rc = -EALREADY);
1172 OBD_FAIL_TIMEOUT(OBD_FAIL_TGT_DELAY_RECONNECT, 2 * obd_timeout);
1179 CDEBUG(D_HA, "%s: connection from %s@%s %st"LPU64" exp %p cur %ld last %ld\n",
1180 target->obd_name, cluuid.uuid, libcfs_nid2str(req->rq_peer.nid),
1181 target->obd_recovering ? "recovering/" : "", data->ocd_transno,
1182 export, (long)cfs_time_current_sec(),
1183 export ? (long)export->exp_last_request_time : 0);
1185 /* If this is the first time a client connects, reset the recovery
1186 * timer. Discard lightweight connections which might be local. */
1187 if (!lw_client && rc == 0 && target->obd_recovering)
1188 check_and_start_recovery_timer(target, req, export == NULL);
1190 /* We want to handle EALREADY but *not* -EALREADY from
1191 * target_handle_reconnect(), return reconnection state in a flag. */
1192 if (rc == EALREADY) {
1193 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECONNECT);
1199 /* Tell the client if we support replayable requests. */
1200 if (target->obd_replayable)
1201 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
1202 client_nid = &req->rq_peer.nid;
1204 if (export == NULL) {
1205 /* allow lightweight connections during recovery */
1206 /* allow "new" MDT to be connected during recovery, since we
1207 * need retrieve recovery update records from it */
1208 if (target->obd_recovering && !lw_client && !mds_mds_conn) {
1210 int c; /* connected */
1211 int i; /* in progress */
1213 int s; /* stale/evicted */
1215 c = atomic_read(&target->obd_connected_clients);
1216 i = atomic_read(&target->obd_lock_replay_clients);
1217 k = target->obd_max_recoverable_clients;
1218 s = target->obd_stale_clients;
1219 t = cfs_timer_deadline(&target->obd_recovery_timer);
1220 t = cfs_time_sub(t, cfs_time_current());
1221 t = cfs_duration_sec(t);
1222 LCONSOLE_WARN("%s: Denying connection for new client %s"
1223 "(at %s), waiting for %d known clients "
1224 "(%d recovered, %d in progress, and %d "
1225 "evicted) to recover in %d:%.02d\n",
1226 target->obd_name, cluuid.uuid,
1227 libcfs_nid2str(req->rq_peer.nid), k,
1228 c - i, i, s, (int)t / 60,
1233 rc = obd_connect(req->rq_svc_thread->t_env,
1234 &export, target, &cluuid, data,
1236 if (mds_conn && OBD_FAIL_CHECK(OBD_FAIL_TGT_RCVG_FLAG))
1237 lustre_msg_add_op_flags(req->rq_repmsg,
1238 MSG_CONNECT_RECOVERING);
1240 conn.cookie = export->exp_handle.h_cookie;
1241 rc = rev_import_init(export);
1245 new_mds_mds_conn = true;
1248 rc = obd_reconnect(req->rq_svc_thread->t_env,
1249 export, target, &cluuid, data, client_nid);
1254 LASSERT(target->u.obt.obt_magic == OBT_MAGIC);
1255 data->ocd_instance = target->u.obt.obt_instance;
1257 /* Return only the parts of obd_connect_data that we understand, so the
1258 * client knows that we don't understand the rest. */
1260 tmpsize = req_capsule_get_size(&req->rq_pill, &RMF_CONNECT_DATA,
1262 tmpdata = req_capsule_server_get(&req->rq_pill,
1264 /* Don't use struct assignment here, because the client reply
1265 * buffer may be smaller/larger than the local struct
1266 * obd_connect_data. */
1267 memcpy(tmpdata, data, min(tmpsize, size));
1270 /* If the client and the server are the same node, we will already
1271 * have an export that really points to the client's DLM export,
1272 * because we have a shared handles table.
1274 * XXX this will go away when shaver stops sending the "connect" handle
1275 * in the real "remote handle" field of the request --phik 24 Apr 2003
1277 ptlrpc_request_change_export(req, export);
1279 spin_lock(&export->exp_lock);
1280 if (export->exp_conn_cnt >= lustre_msg_get_conn_cnt(req->rq_reqmsg)) {
1281 spin_unlock(&export->exp_lock);
1282 CDEBUG(D_RPCTRACE, "%s: %s already connected at greater "
1283 "or equal conn_cnt: %d >= %d\n",
1284 cluuid.uuid, libcfs_nid2str(req->rq_peer.nid),
1285 export->exp_conn_cnt,
1286 lustre_msg_get_conn_cnt(req->rq_reqmsg));
1288 GOTO(out, rc = -EALREADY);
1290 LASSERT(lustre_msg_get_conn_cnt(req->rq_reqmsg) > 0);
1291 export->exp_conn_cnt = lustre_msg_get_conn_cnt(req->rq_reqmsg);
1293 /* Don't evict liblustre clients for not pinging. */
1294 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT) {
1295 export->exp_libclient = 1;
1296 spin_unlock(&export->exp_lock);
1298 spin_lock(&target->obd_dev_lock);
1299 list_del_init(&export->exp_obd_chain_timed);
1300 spin_unlock(&target->obd_dev_lock);
1302 spin_unlock(&export->exp_lock);
1305 if (export->exp_connection != NULL) {
1306 /* Check to see if connection came from another NID. */
1307 if ((export->exp_connection->c_peer.nid != req->rq_peer.nid) &&
1308 !hlist_unhashed(&export->exp_nid_hash))
1309 cfs_hash_del(export->exp_obd->obd_nid_hash,
1310 &export->exp_connection->c_peer.nid,
1311 &export->exp_nid_hash);
1313 ptlrpc_connection_put(export->exp_connection);
1316 export->exp_connection = ptlrpc_connection_get(req->rq_peer,
1319 if (hlist_unhashed(&export->exp_nid_hash))
1320 cfs_hash_add(export->exp_obd->obd_nid_hash,
1321 &export->exp_connection->c_peer.nid,
1322 &export->exp_nid_hash);
1324 lustre_msg_set_handle(req->rq_repmsg, &conn);
1326 rc = rev_import_reconnect(export, req);
1330 if (target->obd_recovering && !export->exp_in_recovery && !lw_client) {
1332 __u64 transno = data->ocd_transno;
1334 spin_lock(&export->exp_lock);
1335 /* possible race with class_disconnect_stale_exports,
1336 * export may be already in the eviction process */
1337 if (export->exp_failed) {
1338 spin_unlock(&export->exp_lock);
1339 GOTO(out, rc = -ENODEV);
1341 export->exp_in_recovery = 1;
1342 export->exp_req_replay_needed = 1;
1343 export->exp_lock_replay_needed = 1;
1344 spin_unlock(&export->exp_lock);
1346 has_transno = !!(lustre_msg_get_op_flags(req->rq_reqmsg) &
1347 MSG_CONNECT_TRANSNO);
1348 if (has_transno && transno == 0)
1349 CWARN("Connect with zero transno!\n");
1351 if (has_transno && transno > 0 &&
1352 transno < target->obd_next_recovery_transno &&
1353 transno > target->obd_last_committed) {
1354 /* Another way is to use cmpxchg() to be lock-free. */
1355 spin_lock(&target->obd_recovery_task_lock);
1356 if (transno < target->obd_next_recovery_transno)
1357 target->obd_next_recovery_transno = transno;
1358 spin_unlock(&target->obd_recovery_task_lock);
1361 atomic_inc(&target->obd_req_replay_clients);
1362 atomic_inc(&target->obd_lock_replay_clients);
1363 /* Note: MDS-MDS connection is allowed to be connected during
1364 * recovery, no matter if the exports needs to be recoveried.
1365 * Because we need retrieve updates logs from all other MDTs.
1366 * So if the MDS-MDS export is new, obd_max_recoverable_clients
1367 * also needs to be increased to match other recovery checking
1369 if (new_mds_mds_conn)
1370 target->obd_max_recoverable_clients++;
1371 if (atomic_inc_return(&target->obd_connected_clients) ==
1372 target->obd_max_recoverable_clients)
1373 wake_up(&target->obd_next_transno_waitq);
1376 /* Tell the client we're in recovery, when client is involved in it. */
1377 if (target->obd_recovering && !lw_client)
1378 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
1382 spin_lock(&export->exp_lock);
1383 export->exp_connecting = 0;
1384 spin_unlock(&export->exp_lock);
1386 class_export_put(export);
1388 if (target != NULL) {
1389 spin_lock(&target->obd_dev_lock);
1390 target->obd_conn_inprogress--;
1391 spin_unlock(&target->obd_dev_lock);
1393 class_decref(target, __func__, current);
1395 req->rq_status = rc;
1399 int target_handle_disconnect(struct ptlrpc_request *req)
1404 rc = req_capsule_server_pack(&req->rq_pill);
1408 /* Keep the rq_export around so we can send the reply. */
1409 req->rq_status = obd_disconnect(class_export_get(req->rq_export));
1414 void target_destroy_export(struct obd_export *exp)
1416 struct obd_import *imp = NULL;
1417 /* exports created from last_rcvd data, and "fake"
1418 exports created by lctl don't have an import */
1419 spin_lock(&exp->exp_lock);
1420 if (exp->exp_imp_reverse != NULL) {
1421 imp = exp->exp_imp_reverse;
1422 exp->exp_imp_reverse = NULL;
1424 spin_unlock(&exp->exp_lock);
1426 client_destroy_import(imp);
1428 LASSERT_ATOMIC_ZERO(&exp->exp_locks_count);
1429 LASSERT_ATOMIC_ZERO(&exp->exp_rpc_count);
1430 LASSERT_ATOMIC_ZERO(&exp->exp_cb_count);
1431 LASSERT_ATOMIC_ZERO(&exp->exp_replay_count);
1433 EXPORT_SYMBOL(target_destroy_export);
1436 * Recovery functions
1438 static void target_request_copy_get(struct ptlrpc_request *req)
1440 class_export_rpc_inc(req->rq_export);
1441 LASSERT(list_empty(&req->rq_list));
1442 INIT_LIST_HEAD(&req->rq_replay_list);
1444 /* Increase refcount to keep request in queue. */
1445 atomic_inc(&req->rq_refcount);
1446 /* Let export know it has replays to be handled. */
1447 atomic_inc(&req->rq_export->exp_replay_count);
1450 static void target_request_copy_put(struct ptlrpc_request *req)
1452 LASSERT(list_empty(&req->rq_replay_list));
1453 LASSERT_ATOMIC_POS(&req->rq_export->exp_replay_count);
1455 atomic_dec(&req->rq_export->exp_replay_count);
1456 class_export_rpc_dec(req->rq_export);
1457 ptlrpc_server_drop_request(req);
1460 static int target_exp_enqueue_req_replay(struct ptlrpc_request *req)
1462 __u64 transno = lustre_msg_get_transno(req->rq_reqmsg);
1463 struct obd_export *exp = req->rq_export;
1464 struct ptlrpc_request *reqiter;
1465 struct ptlrpc_request *dup_req = NULL;
1470 spin_lock(&exp->exp_lock);
1471 list_for_each_entry(reqiter, &exp->exp_req_replay_queue,
1473 if (lustre_msg_get_transno(reqiter->rq_reqmsg) == transno) {
1481 /* We expect it with RESENT and REPLAY flags. */
1482 if ((lustre_msg_get_flags(req->rq_reqmsg) &
1483 (MSG_RESENT | MSG_REPLAY)) != (MSG_RESENT | MSG_REPLAY))
1484 CERROR("invalid flags %x of resent replay\n",
1485 lustre_msg_get_flags(req->rq_reqmsg));
1487 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REPLAY) {
1490 new_conn = lustre_msg_get_conn_cnt(req->rq_reqmsg);
1492 lustre_msg_get_conn_cnt(dup_req->rq_reqmsg))
1493 lustre_msg_set_conn_cnt(dup_req->rq_reqmsg,
1497 list_add_tail(&req->rq_replay_list,
1498 &exp->exp_req_replay_queue);
1501 spin_unlock(&exp->exp_lock);
1505 static void target_exp_dequeue_req_replay(struct ptlrpc_request *req)
1507 LASSERT(!list_empty(&req->rq_replay_list));
1508 LASSERT(req->rq_export);
1510 spin_lock(&req->rq_export->exp_lock);
1511 list_del_init(&req->rq_replay_list);
1512 spin_unlock(&req->rq_export->exp_lock);
1515 static void target_finish_recovery(struct lu_target *lut)
1517 struct obd_device *obd = lut->lut_obd;
1520 /* Only log a recovery message when recovery has occurred. */
1521 if (obd->obd_recovery_start) {
1522 time_t elapsed_time = max_t(time_t, 1, cfs_time_current_sec() -
1523 obd->obd_recovery_start);
1524 LCONSOLE_INFO("%s: Recovery over after %d:%.02d, of %d clients "
1525 "%d recovered and %d %s evicted.\n", obd->obd_name,
1526 (int)elapsed_time / 60, (int)elapsed_time % 60,
1527 obd->obd_max_recoverable_clients,
1528 atomic_read(&obd->obd_connected_clients),
1529 obd->obd_stale_clients,
1530 obd->obd_stale_clients == 1 ? "was" : "were");
1533 ldlm_reprocess_all_ns(obd->obd_namespace);
1534 spin_lock(&obd->obd_recovery_task_lock);
1535 if (!list_empty(&obd->obd_req_replay_queue) ||
1536 !list_empty(&obd->obd_lock_replay_queue) ||
1537 !list_empty(&obd->obd_final_req_queue)) {
1538 CERROR("%s: Recovery queues ( %s%s%s) are not empty\n",
1540 list_empty(&obd->obd_req_replay_queue) ? "" : "req ",
1541 list_empty(&obd->obd_lock_replay_queue) ? \
1543 list_empty(&obd->obd_final_req_queue) ? \
1545 spin_unlock(&obd->obd_recovery_task_lock);
1548 spin_unlock(&obd->obd_recovery_task_lock);
1550 if (lut->lut_tdtd != NULL &&
1551 (!list_empty(&lut->lut_tdtd->tdtd_replay_list) ||
1552 !list_empty(&lut->lut_tdtd->tdtd_replay_finish_list))) {
1553 dtrq_list_dump(lut->lut_tdtd, D_ERROR);
1554 dtrq_list_destroy(lut->lut_tdtd);
1557 obd->obd_recovery_end = cfs_time_current_sec();
1559 /* When recovery finished, cleanup orphans on MDS and OST. */
1560 if (OBT(obd) && OBP(obd, postrecov)) {
1561 int rc = OBP(obd, postrecov)(obd);
1563 LCONSOLE_WARN("%s: Post recovery failed, rc %d\n",
1569 static void abort_req_replay_queue(struct obd_device *obd)
1571 struct ptlrpc_request *req, *n;
1572 struct list_head abort_list;
1574 INIT_LIST_HEAD(&abort_list);
1575 spin_lock(&obd->obd_recovery_task_lock);
1576 list_splice_init(&obd->obd_req_replay_queue, &abort_list);
1577 spin_unlock(&obd->obd_recovery_task_lock);
1578 list_for_each_entry_safe(req, n, &abort_list, rq_list) {
1579 DEBUG_REQ(D_WARNING, req, "aborted:");
1580 req->rq_status = -ENOTCONN;
1581 if (ptlrpc_error(req)) {
1582 DEBUG_REQ(D_ERROR, req,
1583 "failed abort_req_reply; skipping");
1585 target_exp_dequeue_req_replay(req);
1586 target_request_copy_put(req);
1590 static void abort_lock_replay_queue(struct obd_device *obd)
1592 struct ptlrpc_request *req, *n;
1593 struct list_head abort_list;
1595 INIT_LIST_HEAD(&abort_list);
1596 spin_lock(&obd->obd_recovery_task_lock);
1597 list_splice_init(&obd->obd_lock_replay_queue, &abort_list);
1598 spin_unlock(&obd->obd_recovery_task_lock);
1599 list_for_each_entry_safe(req, n, &abort_list, rq_list) {
1600 DEBUG_REQ(D_ERROR, req, "aborted:");
1601 req->rq_status = -ENOTCONN;
1602 if (ptlrpc_error(req)) {
1603 DEBUG_REQ(D_ERROR, req,
1604 "failed abort_lock_reply; skipping");
1606 target_request_copy_put(req);
1610 /* Called from a cleanup function if the device is being cleaned up
1611 forcefully. The exports should all have been disconnected already,
1612 the only thing left to do is
1613 - clear the recovery flags
1615 - free queued requests and replies, but don't send replies
1616 Because the obd_stopping flag is set, no new requests should be received.
1619 void target_cleanup_recovery(struct obd_device *obd)
1621 struct ptlrpc_request *req, *n;
1622 struct list_head clean_list;
1624 INIT_LIST_HEAD(&clean_list);
1625 spin_lock(&obd->obd_dev_lock);
1626 if (!obd->obd_recovering) {
1627 spin_unlock(&obd->obd_dev_lock);
1631 obd->obd_recovering = obd->obd_abort_recovery = 0;
1632 obd->obd_force_abort_recovery = 0;
1633 spin_unlock(&obd->obd_dev_lock);
1635 spin_lock(&obd->obd_recovery_task_lock);
1636 target_cancel_recovery_timer(obd);
1637 list_splice_init(&obd->obd_req_replay_queue, &clean_list);
1638 spin_unlock(&obd->obd_recovery_task_lock);
1640 list_for_each_entry_safe(req, n, &clean_list, rq_list) {
1641 LASSERT(req->rq_reply_state == NULL);
1642 target_exp_dequeue_req_replay(req);
1643 target_request_copy_put(req);
1646 spin_lock(&obd->obd_recovery_task_lock);
1647 list_splice_init(&obd->obd_lock_replay_queue, &clean_list);
1648 list_splice_init(&obd->obd_final_req_queue, &clean_list);
1649 spin_unlock(&obd->obd_recovery_task_lock);
1651 list_for_each_entry_safe(req, n, &clean_list, rq_list) {
1652 LASSERT(req->rq_reply_state == NULL);
1653 target_request_copy_put(req);
1658 EXPORT_SYMBOL(target_cleanup_recovery);
1660 /* obd_recovery_task_lock should be held */
1661 void target_cancel_recovery_timer(struct obd_device *obd)
1663 CDEBUG(D_HA, "%s: cancel recovery timer\n", obd->obd_name);
1664 cfs_timer_disarm(&obd->obd_recovery_timer);
1667 static void target_start_recovery_timer(struct obd_device *obd)
1669 if (obd->obd_recovery_start != 0)
1672 spin_lock(&obd->obd_dev_lock);
1673 if (!obd->obd_recovering || obd->obd_abort_recovery ||
1674 obd->obd_force_abort_recovery) {
1675 spin_unlock(&obd->obd_dev_lock);
1679 LASSERT(obd->obd_recovery_timeout != 0);
1681 if (obd->obd_recovery_start != 0) {
1682 spin_unlock(&obd->obd_dev_lock);
1686 cfs_timer_arm(&obd->obd_recovery_timer,
1687 cfs_time_shift(obd->obd_recovery_timeout));
1688 obd->obd_recovery_start = cfs_time_current_sec();
1689 spin_unlock(&obd->obd_dev_lock);
1691 LCONSOLE_WARN("%s: Will be in recovery for at least %d:%.02d, "
1692 "or until %d client%s reconnect%s\n",
1694 obd->obd_recovery_timeout / 60,
1695 obd->obd_recovery_timeout % 60,
1696 obd->obd_max_recoverable_clients,
1697 (obd->obd_max_recoverable_clients == 1) ? "" : "s",
1698 (obd->obd_max_recoverable_clients == 1) ? "s": "");
1702 * extend recovery window.
1704 * if @extend is true, extend recovery window to have @drt remaining at least;
1705 * otherwise, make sure the recovery timeout value is not less than @drt.
1707 static void extend_recovery_timer(struct obd_device *obd, int drt, bool extend)
1711 cfs_duration_t left;
1714 spin_lock(&obd->obd_dev_lock);
1715 if (!obd->obd_recovering || obd->obd_abort_recovery ||
1716 obd->obd_force_abort_recovery) {
1717 spin_unlock(&obd->obd_dev_lock);
1720 LASSERT(obd->obd_recovery_start != 0);
1722 now = cfs_time_current_sec();
1723 to = obd->obd_recovery_timeout;
1724 end = obd->obd_recovery_start + to;
1725 left = cfs_time_sub(end, now);
1727 if (extend && (drt > left)) {
1729 } else if (!extend && (drt > to)) {
1733 if (to > obd->obd_recovery_time_hard)
1734 to = obd->obd_recovery_time_hard;
1735 if (obd->obd_recovery_timeout < to) {
1736 obd->obd_recovery_timeout = to;
1737 end = obd->obd_recovery_start + to;
1738 cfs_timer_arm(&obd->obd_recovery_timer,
1739 cfs_time_shift(end - now));
1741 spin_unlock(&obd->obd_dev_lock);
1743 CDEBUG(D_HA, "%s: recovery timer will expire in %u seconds\n",
1744 obd->obd_name, (unsigned)cfs_time_sub(end, now));
1747 /* Reset the timer with each new client connection */
1749 * This timer is actually reconnect_timer, which is for making sure
1750 * the total recovery window is at least as big as my reconnect
1751 * attempt timing. So the initial recovery time_out will be set to
1752 * OBD_RECOVERY_FACTOR * obd_timeout. If the timeout coming
1753 * from client is bigger than this, then the recovery time_out will
1754 * be extended to make sure the client could be reconnected, in the
1755 * process, the timeout from the new client should be ignored.
1759 check_and_start_recovery_timer(struct obd_device *obd,
1760 struct ptlrpc_request *req,
1763 int service_time = lustre_msg_get_service_time(req->rq_reqmsg);
1764 struct obd_device_target *obt = &obd->u.obt;
1766 if (!new_client && service_time)
1767 /* Teach server about old server's estimates, as first guess
1768 * at how long new requests will take. */
1769 at_measured(&req->rq_rqbd->rqbd_svcpt->scp_at_estimate,
1772 target_start_recovery_timer(obd);
1774 /* Convert the service time to RPC timeout,
1775 * and reuse service_time to limit stack usage. */
1776 service_time = at_est2timeout(service_time);
1778 /* We expect other clients to timeout within service_time, then try
1779 * to reconnect, then try the failover server. The max delay between
1780 * connect attempts is SWITCH_MAX + SWITCH_INC + INITIAL. */
1781 service_time += 2 * INITIAL_CONNECT_TIMEOUT;
1783 LASSERT(obt->obt_magic == OBT_MAGIC);
1784 service_time += 2 * (CONNECTION_SWITCH_MAX + CONNECTION_SWITCH_INC);
1785 if (service_time > obd->obd_recovery_timeout && !new_client)
1786 extend_recovery_timer(obd, service_time, false);
1789 /** Health checking routines */
1790 static inline int exp_connect_healthy(struct obd_export *exp)
1792 return (exp->exp_in_recovery);
1795 /** if export done req_replay or has replay in queue */
1796 static inline int exp_req_replay_healthy(struct obd_export *exp)
1798 return (!exp->exp_req_replay_needed ||
1799 atomic_read(&exp->exp_replay_count) > 0);
1801 /** if export done lock_replay or has replay in queue */
1802 static inline int exp_lock_replay_healthy(struct obd_export *exp)
1804 return (!exp->exp_lock_replay_needed ||
1805 atomic_read(&exp->exp_replay_count) > 0);
1808 static inline int exp_vbr_healthy(struct obd_export *exp)
1810 return (!exp->exp_vbr_failed);
1813 static inline int exp_finished(struct obd_export *exp)
1815 return (exp->exp_in_recovery && !exp->exp_lock_replay_needed);
1818 static int check_for_next_transno(struct lu_target *lut)
1820 struct ptlrpc_request *req = NULL;
1821 struct obd_device *obd = lut->lut_obd;
1822 struct target_distribute_txn_data *tdtd = lut->lut_tdtd;
1823 int wake_up = 0, connected, completed, queue_len;
1824 __u64 req_transno = 0;
1825 __u64 update_transno = 0;
1826 __u64 next_transno = 0;
1829 spin_lock(&obd->obd_recovery_task_lock);
1830 if (!list_empty(&obd->obd_req_replay_queue)) {
1831 req = list_entry(obd->obd_req_replay_queue.next,
1832 struct ptlrpc_request, rq_list);
1833 req_transno = lustre_msg_get_transno(req->rq_reqmsg);
1837 update_transno = distribute_txn_get_next_transno(tdtd);
1839 connected = atomic_read(&obd->obd_connected_clients);
1840 completed = connected - atomic_read(&obd->obd_req_replay_clients);
1841 queue_len = obd->obd_requests_queued_for_recovery;
1842 next_transno = obd->obd_next_recovery_transno;
1844 CDEBUG(D_HA, "max: %d, connected: %d, completed: %d, queue_len: %d, "
1845 "req_transno: "LPU64", next_transno: "LPU64"\n",
1846 obd->obd_max_recoverable_clients, connected, completed,
1847 queue_len, req_transno, next_transno);
1849 if (obd->obd_abort_recovery || obd->obd_force_abort_recovery) {
1850 CDEBUG(D_HA, "waking for aborted recovery\n");
1852 } else if (obd->obd_recovery_expired) {
1853 CDEBUG(D_HA, "waking for expired recovery\n");
1855 } else if (tdtd != NULL && req != NULL &&
1856 is_req_replayed_by_update(req)) {
1857 LASSERTF(req_transno < next_transno, "req_transno "LPU64
1858 "next_transno"LPU64"\n", req_transno, next_transno);
1859 CDEBUG(D_HA, "waking for duplicate req ("LPU64")\n",
1862 } else if (req_transno == next_transno ||
1863 (update_transno != 0 && update_transno <= next_transno)) {
1864 CDEBUG(D_HA, "waking for next ("LPD64")\n", next_transno);
1866 } else if (queue_len > 0 &&
1867 queue_len == atomic_read(&obd->obd_req_replay_clients)) {
1869 /** handle gaps occured due to lost reply or VBR */
1870 LASSERTF(req_transno >= next_transno,
1871 "req_transno: "LPU64", next_transno: "LPU64"\n",
1872 req_transno, next_transno);
1873 if (req_transno > obd->obd_last_committed &&
1874 !obd->obd_version_recov)
1877 "%s: waking for gap in transno, VBR is %s (skip: "
1878 LPD64", ql: %d, comp: %d, conn: %d, next: "LPD64
1879 ", next_update "LPD64" last_committed: "LPD64")\n",
1880 obd->obd_name, obd->obd_version_recov ? "ON" : "OFF",
1881 next_transno, queue_len, completed, connected,
1882 req_transno, update_transno, obd->obd_last_committed);
1883 obd->obd_next_recovery_transno = req_transno;
1885 } else if (atomic_read(&obd->obd_req_replay_clients) == 0) {
1886 CDEBUG(D_HA, "waking for completed recovery\n");
1888 } else if (OBD_FAIL_CHECK(OBD_FAIL_MDS_RECOVERY_ACCEPTS_GAPS)) {
1889 CDEBUG(D_HA, "accepting transno gaps is explicitly allowed"
1890 " by fail_lock, waking up ("LPD64")\n", next_transno);
1891 obd->obd_next_recovery_transno = req_transno;
1894 spin_unlock(&obd->obd_recovery_task_lock);
1898 static int check_for_next_lock(struct lu_target *lut)
1900 struct obd_device *obd = lut->lut_obd;
1903 spin_lock(&obd->obd_recovery_task_lock);
1904 if (!list_empty(&obd->obd_lock_replay_queue)) {
1905 CDEBUG(D_HA, "waking for next lock\n");
1907 } else if (atomic_read(&obd->obd_lock_replay_clients) == 0) {
1908 CDEBUG(D_HA, "waking for completed lock replay\n");
1910 } else if (obd->obd_abort_recovery || obd->obd_force_abort_recovery) {
1911 CDEBUG(D_HA, "waking for aborted recovery\n");
1913 } else if (obd->obd_recovery_expired) {
1914 CDEBUG(D_HA, "waking for expired recovery\n");
1917 spin_unlock(&obd->obd_recovery_task_lock);
1923 * wait for recovery events,
1924 * check its status with help of check_routine
1925 * evict dead clients via health_check
1927 static int target_recovery_overseer(struct lu_target *lut,
1928 int (*check_routine)(struct lu_target *),
1929 int (*health_check)(struct obd_export *))
1931 struct obd_device *obd = lut->lut_obd;
1933 if ((obd->obd_recovery_start != 0) && (cfs_time_current_sec() >=
1934 (obd->obd_recovery_start + obd->obd_recovery_time_hard))) {
1935 CWARN("recovery is aborted by hard timeout\n");
1936 obd->obd_abort_recovery = 1;
1939 while (wait_event_timeout(obd->obd_next_transno_waitq,
1941 msecs_to_jiffies(60 * MSEC_PER_SEC)) == 0)
1942 /* wait indefinitely for event, but don't trigger watchdog */;
1944 if (obd->obd_abort_recovery || obd->obd_force_abort_recovery) {
1945 CWARN("recovery is aborted, evict exports in recovery\n");
1946 /** evict exports which didn't finish recovery yet */
1947 class_disconnect_stale_exports(obd, exp_finished);
1949 } else if (obd->obd_recovery_expired) {
1950 obd->obd_recovery_expired = 0;
1951 /** If some clients died being recovered, evict them */
1952 LCONSOLE_WARN("%s: recovery is timed out, "
1953 "evict stale exports\n", obd->obd_name);
1954 /** evict cexports with no replay in queue, they are stalled */
1955 class_disconnect_stale_exports(obd, health_check);
1956 /** continue with VBR */
1957 spin_lock(&obd->obd_dev_lock);
1958 obd->obd_version_recov = 1;
1959 spin_unlock(&obd->obd_dev_lock);
1961 * reset timer, recovery will proceed with versions now,
1962 * timeout is set just to handle reconnection delays
1964 extend_recovery_timer(obd, RECONNECT_DELAY_MAX, true);
1965 /** Wait for recovery events again, after evicting bad clients */
1971 static struct ptlrpc_request *target_next_replay_lock(struct lu_target *lut)
1973 struct obd_device *obd = lut->lut_obd;
1974 struct ptlrpc_request *req = NULL;
1976 CDEBUG(D_HA, "Waiting for lock\n");
1977 if (target_recovery_overseer(lut, check_for_next_lock,
1978 exp_lock_replay_healthy))
1979 abort_lock_replay_queue(obd);
1981 spin_lock(&obd->obd_recovery_task_lock);
1982 if (!list_empty(&obd->obd_lock_replay_queue)) {
1983 req = list_entry(obd->obd_lock_replay_queue.next,
1984 struct ptlrpc_request, rq_list);
1985 list_del_init(&req->rq_list);
1986 spin_unlock(&obd->obd_recovery_task_lock);
1988 spin_unlock(&obd->obd_recovery_task_lock);
1989 LASSERT(list_empty(&obd->obd_lock_replay_queue));
1990 LASSERT(atomic_read(&obd->obd_lock_replay_clients) == 0);
1991 /** evict exports failed VBR */
1992 class_disconnect_stale_exports(obd, exp_vbr_healthy);
1997 static struct ptlrpc_request *target_next_final_ping(struct obd_device *obd)
1999 struct ptlrpc_request *req = NULL;
2001 spin_lock(&obd->obd_recovery_task_lock);
2002 if (!list_empty(&obd->obd_final_req_queue)) {
2003 req = list_entry(obd->obd_final_req_queue.next,
2004 struct ptlrpc_request, rq_list);
2005 list_del_init(&req->rq_list);
2006 spin_unlock(&obd->obd_recovery_task_lock);
2007 if (req->rq_export->exp_in_recovery) {
2008 spin_lock(&req->rq_export->exp_lock);
2009 req->rq_export->exp_in_recovery = 0;
2010 spin_unlock(&req->rq_export->exp_lock);
2013 spin_unlock(&obd->obd_recovery_task_lock);
2018 static void handle_recovery_req(struct ptlrpc_thread *thread,
2019 struct ptlrpc_request *req,
2020 svc_handler_t handler)
2025 * export can be evicted during recovery, no need to handle replays for
2026 * it after that, discard such request silently
2028 if (req->rq_export->exp_disconnected)
2031 req->rq_session.lc_thread = thread;
2032 req->rq_svc_thread = thread;
2033 req->rq_svc_thread->t_env->le_ses = &req->rq_session;
2035 /* thread context */
2036 lu_context_enter(&thread->t_env->le_ctx);
2038 lu_context_exit(&thread->t_env->le_ctx);
2040 /* don't reset timer for final stage */
2041 if (!exp_finished(req->rq_export)) {
2042 int to = obd_timeout;
2045 * Add request timeout to the recovery time so next request from
2046 * this client may come in recovery time
2049 struct ptlrpc_service_part *svcpt;
2051 svcpt = req->rq_rqbd->rqbd_svcpt;
2052 /* If the server sent early reply for this request,
2053 * the client will recalculate the timeout according to
2054 * current server estimate service time, so we will
2055 * use the maxium timeout here for waiting the client
2056 * sending the next req */
2057 to = max((int)at_est2timeout(
2058 at_get(&svcpt->scp_at_estimate)),
2059 (int)lustre_msg_get_timeout(req->rq_reqmsg));
2060 /* Add 2 net_latency, one for balance rq_deadline
2061 * (see ptl_send_rpc), one for resend the req to server,
2062 * Note: client will pack net_latency in replay req
2063 * (see ptlrpc_replay_req) */
2064 to += 2 * lustre_msg_get_service_time(req->rq_reqmsg);
2066 extend_recovery_timer(class_exp2obd(req->rq_export), to, true);
2071 /** Checking routines for recovery */
2072 static int check_for_recovery_ready(struct lu_target *lut)
2074 struct obd_device *obd = lut->lut_obd;
2075 unsigned int clnts = atomic_read(&obd->obd_connected_clients);
2077 CDEBUG(D_HA, "connected %d stale %d max_recoverable_clients %d"
2078 " abort %d expired %d\n", clnts, obd->obd_stale_clients,
2079 obd->obd_max_recoverable_clients, obd->obd_abort_recovery,
2080 obd->obd_recovery_expired);
2082 if (obd->obd_force_abort_recovery)
2085 if (!obd->obd_abort_recovery && !obd->obd_recovery_expired) {
2086 LASSERT(clnts <= obd->obd_max_recoverable_clients);
2087 if (clnts + obd->obd_stale_clients <
2088 obd->obd_max_recoverable_clients)
2092 if (lut->lut_tdtd != NULL) {
2093 if (!lut->lut_tdtd->tdtd_replay_ready) {
2094 /* Let's extend recovery timer, in case the recovery
2095 * timer expired, and some clients got evicted */
2096 extend_recovery_timer(obd, obd->obd_recovery_timeout,
2100 dtrq_list_dump(lut->lut_tdtd, D_HA);
2108 REQUEST_RECOVERY = 1,
2109 UPDATE_RECOVERY = 2,
2112 static __u64 get_next_replay_req_transno(struct obd_device *obd)
2116 if (!list_empty(&obd->obd_req_replay_queue)) {
2117 struct ptlrpc_request *req;
2119 req = list_entry(obd->obd_req_replay_queue.next,
2120 struct ptlrpc_request, rq_list);
2121 transno = lustre_msg_get_transno(req->rq_reqmsg);
2126 __u64 get_next_transno(struct lu_target *lut, int *type)
2128 struct obd_device *obd = lut->lut_obd;
2129 struct target_distribute_txn_data *tdtd = lut->lut_tdtd;
2131 __u64 update_transno;
2134 transno = get_next_replay_req_transno(obd);
2136 *type = REQUEST_RECOVERY;
2141 update_transno = distribute_txn_get_next_transno(tdtd);
2142 if (transno == 0 || (transno >= update_transno &&
2143 update_transno != 0)) {
2144 transno = update_transno;
2146 *type = UPDATE_RECOVERY;
2153 * drop duplicate replay request
2155 * Because the operation has been replayed by update recovery, the request
2156 * with the same transno will be dropped and also notify the client to send
2157 * next replay request.
2159 * \param[in] env execution environment
2160 * \param[in] obd failover obd device
2161 * \param[in] req request to be dropped
2163 static void drop_duplicate_replay_req(struct lu_env *env,
2164 struct obd_device *obd,
2165 struct ptlrpc_request *req)
2167 DEBUG_REQ(D_HA, req, "remove t"LPD64" from %s because of duplicate"
2168 " update records are found.\n",
2169 lustre_msg_get_transno(req->rq_reqmsg),
2170 libcfs_nid2str(req->rq_peer.nid));
2172 /* Right now, only for MDS reint operation update replay and
2173 * normal request replay can have the same transno */
2174 if (lustre_msg_get_opc(req->rq_reqmsg) == MDS_REINT) {
2175 req_capsule_set(&req->rq_pill, &RQF_MDS_REINT);
2176 req->rq_status = req_capsule_server_pack(&req->rq_pill);
2177 if (likely(req->rq_export))
2178 target_committed_to_req(req);
2179 lustre_msg_set_transno(req->rq_repmsg, req->rq_transno);
2180 target_send_reply(req, req->rq_status, 0);
2182 DEBUG_REQ(D_ERROR, req, "wrong opc" "from %s\n",
2183 libcfs_nid2str(req->rq_peer.nid));
2185 target_exp_dequeue_req_replay(req);
2186 target_request_copy_put(req);
2187 obd->obd_replayed_requests++;
2190 static void replay_request_or_update(struct lu_env *env,
2191 struct lu_target *lut,
2192 struct target_recovery_data *trd,
2193 struct ptlrpc_thread *thread)
2195 struct obd_device *obd = lut->lut_obd;
2196 struct ptlrpc_request *req = NULL;
2201 CDEBUG(D_HA, "Waiting for transno "LPD64"\n",
2202 obd->obd_next_recovery_transno);
2204 /* Replay all of request and update by transno */
2206 struct target_distribute_txn_data *tdtd = lut->lut_tdtd;
2208 CFS_FAIL_TIMEOUT(OBD_FAIL_TGT_REPLAY_DELAY2, cfs_fail_val);
2210 /** It is needed to extend recovery window above
2211 * recovery_time_soft. Extending is possible only in the
2212 * end of recovery window (see more details in
2213 * handle_recovery_req()).
2215 CFS_FAIL_TIMEOUT_MS(OBD_FAIL_TGT_REPLAY_DELAY, 300);
2217 if (target_recovery_overseer(lut, check_for_next_transno,
2218 exp_req_replay_healthy)) {
2219 abort_req_replay_queue(obd);
2220 abort_lock_replay_queue(obd);
2223 spin_lock(&obd->obd_recovery_task_lock);
2224 transno = get_next_transno(lut, &type);
2225 if (type == REQUEST_RECOVERY && transno != 0) {
2226 /* Drop replay request from client side, if the
2227 * replay has been executed by update with the
2229 req = list_entry(obd->obd_req_replay_queue.next,
2230 struct ptlrpc_request, rq_list);
2232 list_del_init(&req->rq_list);
2233 obd->obd_requests_queued_for_recovery--;
2234 spin_unlock(&obd->obd_recovery_task_lock);
2236 /* Let's check if the request has been redone by
2238 if (is_req_replayed_by_update(req)) {
2239 struct distribute_txn_replay_req *dtrq;
2241 dtrq = distribute_txn_lookup_finish_list(tdtd,
2243 LASSERT(dtrq != NULL);
2244 spin_lock(&tdtd->tdtd_replay_list_lock);
2245 list_del_init(&dtrq->dtrq_list);
2246 spin_unlock(&tdtd->tdtd_replay_list_lock);
2249 drop_duplicate_replay_req(env, obd, req);
2254 LASSERT(trd->trd_processing_task == current_pid());
2255 DEBUG_REQ(D_HA, req, "processing t"LPD64" from %s",
2256 lustre_msg_get_transno(req->rq_reqmsg),
2257 libcfs_nid2str(req->rq_peer.nid));
2259 handle_recovery_req(thread, req,
2260 trd->trd_recovery_handler);
2262 * bz18031: increase next_recovery_transno before
2263 * target_request_copy_put() will drop exp_rpc reference
2265 spin_lock(&obd->obd_recovery_task_lock);
2266 obd->obd_next_recovery_transno++;
2267 spin_unlock(&obd->obd_recovery_task_lock);
2268 target_exp_dequeue_req_replay(req);
2269 target_request_copy_put(req);
2270 obd->obd_replayed_requests++;
2271 } else if (type == UPDATE_RECOVERY && transno != 0) {
2272 struct distribute_txn_replay_req *dtrq;
2274 spin_unlock(&obd->obd_recovery_task_lock);
2276 LASSERT(tdtd != NULL);
2277 dtrq = distribute_txn_get_next_req(tdtd);
2278 lu_context_enter(&thread->t_env->le_ctx);
2279 tdtd->tdtd_replay_handler(env, tdtd, dtrq);
2280 lu_context_exit(&thread->t_env->le_ctx);
2281 extend_recovery_timer(obd, obd_timeout, true);
2283 /* Add it to the replay finish list */
2284 spin_lock(&tdtd->tdtd_replay_list_lock);
2285 if (dtrq->dtrq_xid != 0) {
2286 CDEBUG(D_HA, "Move x"LPU64" t"LPU64
2287 " to finish list\n", dtrq->dtrq_xid,
2288 dtrq->dtrq_master_transno);
2289 list_add(&dtrq->dtrq_list,
2290 &tdtd->tdtd_replay_finish_list);
2294 spin_unlock(&tdtd->tdtd_replay_list_lock);
2296 spin_lock(&obd->obd_recovery_task_lock);
2297 if (transno == obd->obd_next_recovery_transno)
2298 obd->obd_next_recovery_transno++;
2299 else if (transno > obd->obd_next_recovery_transno)
2300 obd->obd_next_recovery_transno = transno + 1;
2301 spin_unlock(&obd->obd_recovery_task_lock);
2304 spin_unlock(&obd->obd_recovery_task_lock);
2305 LASSERT(list_empty(&obd->obd_req_replay_queue));
2306 LASSERT(atomic_read(&obd->obd_req_replay_clients) == 0);
2307 /** evict exports failed VBR */
2308 class_disconnect_stale_exports(obd, exp_vbr_healthy);
2314 static int target_recovery_thread(void *arg)
2316 struct lu_target *lut = arg;
2317 struct obd_device *obd = lut->lut_obd;
2318 struct ptlrpc_request *req;
2319 struct target_recovery_data *trd = &obd->obd_recovery_data;
2320 unsigned long delta;
2322 struct ptlrpc_thread *thread = NULL;
2326 unshare_fs_struct();
2327 OBD_ALLOC_PTR(thread);
2333 OBD_FREE_PTR(thread);
2337 rc = lu_context_init(&env->le_ctx, LCT_MD_THREAD | LCT_DT_THREAD);
2339 OBD_FREE_PTR(thread);
2344 thread->t_env = env;
2345 thread->t_id = -1; /* force filter_iobuf_get/put to use local buffers */
2346 env->le_ctx.lc_thread = thread;
2347 tgt_io_thread_init(thread); /* init thread_big_cache for IO requests */
2348 thread->t_watchdog = NULL;
2350 CDEBUG(D_HA, "%s: started recovery thread pid %d\n", obd->obd_name,
2352 trd->trd_processing_task = current_pid();
2354 spin_lock(&obd->obd_dev_lock);
2355 obd->obd_recovering = 1;
2356 spin_unlock(&obd->obd_dev_lock);
2357 complete(&trd->trd_starting);
2359 /* first of all, we have to know the first transno to replay */
2360 if (target_recovery_overseer(lut, check_for_recovery_ready,
2361 exp_connect_healthy)) {
2362 abort_req_replay_queue(obd);
2363 abort_lock_replay_queue(obd);
2364 if (lut->lut_tdtd != NULL)
2365 dtrq_list_destroy(lut->lut_tdtd);
2368 /* next stage: replay requests or update */
2370 CDEBUG(D_INFO, "1: request replay stage - %d clients from t"LPU64"\n",
2371 atomic_read(&obd->obd_req_replay_clients),
2372 obd->obd_next_recovery_transno);
2373 replay_request_or_update(env, lut, trd, thread);
2376 * The second stage: replay locks
2378 CDEBUG(D_INFO, "2: lock replay stage - %d clients\n",
2379 atomic_read(&obd->obd_lock_replay_clients));
2380 while ((req = target_next_replay_lock(lut))) {
2381 LASSERT(trd->trd_processing_task == current_pid());
2382 DEBUG_REQ(D_HA, req, "processing lock from %s: ",
2383 libcfs_nid2str(req->rq_peer.nid));
2384 handle_recovery_req(thread, req,
2385 trd->trd_recovery_handler);
2386 target_request_copy_put(req);
2387 obd->obd_replayed_locks++;
2391 * The third stage: reply on final pings, at this moment all clients
2392 * must have request in final queue
2394 CFS_FAIL_TIMEOUT(OBD_FAIL_TGT_REPLAY_RECONNECT, cfs_fail_val);
2395 CDEBUG(D_INFO, "3: final stage - process recovery completion pings\n");
2396 /** Update server last boot epoch */
2397 tgt_boot_epoch_update(lut);
2398 /* We drop recoverying flag to forward all new requests
2399 * to regular mds_handle() since now */
2400 spin_lock(&obd->obd_dev_lock);
2401 obd->obd_recovering = obd->obd_abort_recovery = 0;
2402 spin_unlock(&obd->obd_dev_lock);
2403 spin_lock(&obd->obd_recovery_task_lock);
2404 target_cancel_recovery_timer(obd);
2405 spin_unlock(&obd->obd_recovery_task_lock);
2406 while ((req = target_next_final_ping(obd))) {
2407 LASSERT(trd->trd_processing_task == current_pid());
2408 DEBUG_REQ(D_HA, req, "processing final ping from %s: ",
2409 libcfs_nid2str(req->rq_peer.nid));
2410 handle_recovery_req(thread, req,
2411 trd->trd_recovery_handler);
2412 /* Because the waiting client can not send ping to server,
2413 * so we need refresh the last_request_time, to avoid the
2414 * export is being evicted */
2415 ptlrpc_update_export_timer(req->rq_export, 0);
2416 target_request_copy_put(req);
2419 delta = jiffies_to_msecs(jiffies - delta) / MSEC_PER_SEC;
2420 CDEBUG(D_INFO,"4: recovery completed in %lus - %d/%d reqs/locks\n",
2421 delta, obd->obd_replayed_requests, obd->obd_replayed_locks);
2422 if (delta > OBD_RECOVERY_TIME_SOFT) {
2423 CWARN("too long recovery - read logs\n");
2424 libcfs_debug_dumplog();
2427 target_finish_recovery(lut);
2429 lu_context_fini(&env->le_ctx);
2430 trd->trd_processing_task = 0;
2431 complete(&trd->trd_finishing);
2433 tgt_io_thread_done(thread);
2434 OBD_FREE_PTR(thread);
2439 static int target_start_recovery_thread(struct lu_target *lut,
2440 svc_handler_t handler)
2442 struct obd_device *obd = lut->lut_obd;
2444 struct target_recovery_data *trd = &obd->obd_recovery_data;
2446 memset(trd, 0, sizeof(*trd));
2447 init_completion(&trd->trd_starting);
2448 init_completion(&trd->trd_finishing);
2449 trd->trd_recovery_handler = handler;
2451 if (!IS_ERR(kthread_run(target_recovery_thread,
2452 lut, "tgt_recov"))) {
2453 wait_for_completion(&trd->trd_starting);
2454 LASSERT(obd->obd_recovering != 0);
2462 void target_stop_recovery_thread(struct obd_device *obd)
2464 if (obd->obd_recovery_data.trd_processing_task > 0) {
2465 struct target_recovery_data *trd = &obd->obd_recovery_data;
2466 /** recovery can be done but postrecovery is not yet */
2467 spin_lock(&obd->obd_dev_lock);
2468 if (obd->obd_recovering) {
2469 CERROR("%s: Aborting recovery\n", obd->obd_name);
2470 obd->obd_abort_recovery = 1;
2471 wake_up(&obd->obd_next_transno_waitq);
2473 spin_unlock(&obd->obd_dev_lock);
2474 wait_for_completion(&trd->trd_finishing);
2477 EXPORT_SYMBOL(target_stop_recovery_thread);
2479 void target_recovery_fini(struct obd_device *obd)
2481 class_disconnect_exports(obd);
2482 target_stop_recovery_thread(obd);
2483 target_cleanup_recovery(obd);
2485 EXPORT_SYMBOL(target_recovery_fini);
2487 static void target_recovery_expired(unsigned long castmeharder)
2489 struct obd_device *obd = (struct obd_device *)castmeharder;
2490 CDEBUG(D_HA, "%s: recovery timed out; %d clients are still in recovery"
2491 " after %lds (%d clients connected)\n",
2492 obd->obd_name, atomic_read(&obd->obd_lock_replay_clients),
2493 cfs_time_current_sec()- obd->obd_recovery_start,
2494 atomic_read(&obd->obd_connected_clients));
2496 obd->obd_recovery_expired = 1;
2497 wake_up(&obd->obd_next_transno_waitq);
2500 void target_recovery_init(struct lu_target *lut, svc_handler_t handler)
2502 struct obd_device *obd = lut->lut_obd;
2504 if (obd->obd_max_recoverable_clients == 0) {
2505 /** Update server last boot epoch */
2506 tgt_boot_epoch_update(lut);
2510 CDEBUG(D_HA, "RECOVERY: service %s, %d recoverable clients, "
2511 "last_transno "LPU64"\n", obd->obd_name,
2512 obd->obd_max_recoverable_clients, obd->obd_last_committed);
2513 LASSERT(obd->obd_stopping == 0);
2514 obd->obd_next_recovery_transno = obd->obd_last_committed + 1;
2515 obd->obd_recovery_start = 0;
2516 obd->obd_recovery_end = 0;
2518 cfs_timer_init(&obd->obd_recovery_timer, target_recovery_expired, obd);
2519 target_start_recovery_thread(lut, handler);
2521 EXPORT_SYMBOL(target_recovery_init);
2523 static int target_process_req_flags(struct obd_device *obd,
2524 struct ptlrpc_request *req)
2526 struct obd_export *exp = req->rq_export;
2527 LASSERT(exp != NULL);
2528 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REQ_REPLAY_DONE) {
2529 /* client declares he's ready to replay locks */
2530 spin_lock(&exp->exp_lock);
2531 if (exp->exp_req_replay_needed) {
2532 exp->exp_req_replay_needed = 0;
2533 spin_unlock(&exp->exp_lock);
2535 LASSERT_ATOMIC_POS(&obd->obd_req_replay_clients);
2536 atomic_dec(&obd->obd_req_replay_clients);
2538 spin_unlock(&exp->exp_lock);
2541 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_LOCK_REPLAY_DONE) {
2542 /* client declares he's ready to complete recovery
2543 * so, we put the request on th final queue */
2544 spin_lock(&exp->exp_lock);
2545 if (exp->exp_lock_replay_needed) {
2546 exp->exp_lock_replay_needed = 0;
2547 spin_unlock(&exp->exp_lock);
2549 LASSERT_ATOMIC_POS(&obd->obd_lock_replay_clients);
2550 atomic_dec(&obd->obd_lock_replay_clients);
2552 spin_unlock(&exp->exp_lock);
2558 int target_queue_recovery_request(struct ptlrpc_request *req,
2559 struct obd_device *obd)
2561 __u64 transno = lustre_msg_get_transno(req->rq_reqmsg);
2562 struct ptlrpc_request *reqiter;
2566 if (obd->obd_recovery_data.trd_processing_task == current_pid()) {
2567 /* Processing the queue right now, don't re-add. */
2571 target_process_req_flags(obd, req);
2573 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_LOCK_REPLAY_DONE) {
2574 /* client declares he's ready to complete recovery
2575 * so, we put the request on th final queue */
2576 target_request_copy_get(req);
2577 DEBUG_REQ(D_HA, req, "queue final req");
2578 wake_up(&obd->obd_next_transno_waitq);
2579 spin_lock(&obd->obd_recovery_task_lock);
2580 if (obd->obd_recovering) {
2581 list_add_tail(&req->rq_list,
2582 &obd->obd_final_req_queue);
2584 spin_unlock(&obd->obd_recovery_task_lock);
2585 target_request_copy_put(req);
2586 RETURN(obd->obd_stopping ? -ENOTCONN : 1);
2588 spin_unlock(&obd->obd_recovery_task_lock);
2591 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REQ_REPLAY_DONE) {
2592 /* client declares he's ready to replay locks */
2593 target_request_copy_get(req);
2594 DEBUG_REQ(D_HA, req, "queue lock replay req");
2595 wake_up(&obd->obd_next_transno_waitq);
2596 spin_lock(&obd->obd_recovery_task_lock);
2597 LASSERT(obd->obd_recovering);
2598 /* usually due to recovery abort */
2599 if (!req->rq_export->exp_in_recovery) {
2600 spin_unlock(&obd->obd_recovery_task_lock);
2601 target_request_copy_put(req);
2604 LASSERT(req->rq_export->exp_lock_replay_needed);
2605 list_add_tail(&req->rq_list, &obd->obd_lock_replay_queue);
2606 spin_unlock(&obd->obd_recovery_task_lock);
2610 /* CAVEAT EMPTOR: The incoming request message has been swabbed
2611 * (i.e. buflens etc are in my own byte order), but type-dependent
2612 * buffers (eg mdt_body, ost_body etc) have NOT been swabbed. */
2615 INIT_LIST_HEAD(&req->rq_list);
2616 DEBUG_REQ(D_HA, req, "not queueing");
2620 /* If we're processing the queue, we want don't want to queue this
2623 * Also, if this request has a transno less than the one we're waiting
2624 * for, we should process it now. It could (and currently always will)
2625 * be an open request for a descriptor that was opened some time ago.
2627 * Also, a resent, replayed request that has already been
2628 * handled will pass through here and be processed immediately.
2630 CDEBUG(D_HA, "Next recovery transno: "LPU64
2631 ", current: "LPU64", replaying\n",
2632 obd->obd_next_recovery_transno, transno);
2634 /* If the request has been replayed by update replay, then sends this
2635 * request to the recovery thread (replay_request_or_update()), where
2636 * it will be handled */
2637 spin_lock(&obd->obd_recovery_task_lock);
2638 if (transno < obd->obd_next_recovery_transno &&
2639 !is_req_replayed_by_update(req)) {
2640 /* Processing the queue right now, don't re-add. */
2641 LASSERT(list_empty(&req->rq_list));
2642 spin_unlock(&obd->obd_recovery_task_lock);
2645 spin_unlock(&obd->obd_recovery_task_lock);
2647 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_REPLAY_DROP))
2650 target_request_copy_get(req);
2651 if (!req->rq_export->exp_in_recovery) {
2652 target_request_copy_put(req);
2655 LASSERT(req->rq_export->exp_req_replay_needed);
2657 if (target_exp_enqueue_req_replay(req)) {
2658 DEBUG_REQ(D_ERROR, req, "dropping resent queued req");
2659 target_request_copy_put(req);
2664 spin_lock(&obd->obd_recovery_task_lock);
2665 LASSERT(obd->obd_recovering);
2666 list_for_each_entry(reqiter, &obd->obd_req_replay_queue, rq_list) {
2667 if (lustre_msg_get_transno(reqiter->rq_reqmsg) > transno) {
2668 list_add_tail(&req->rq_list, &reqiter->rq_list);
2673 if (unlikely(lustre_msg_get_transno(reqiter->rq_reqmsg) ==
2675 DEBUG_REQ(D_ERROR, req, "dropping replay: transno "
2676 "has been claimed by another client");
2677 spin_unlock(&obd->obd_recovery_task_lock);
2678 target_exp_dequeue_req_replay(req);
2679 target_request_copy_put(req);
2685 list_add_tail(&req->rq_list, &obd->obd_req_replay_queue);
2687 obd->obd_requests_queued_for_recovery++;
2688 spin_unlock(&obd->obd_recovery_task_lock);
2689 wake_up(&obd->obd_next_transno_waitq);
2693 int target_handle_ping(struct ptlrpc_request *req)
2695 obd_ping(req->rq_svc_thread->t_env, req->rq_export);
2696 return req_capsule_server_pack(&req->rq_pill);
2699 void target_committed_to_req(struct ptlrpc_request *req)
2701 struct obd_export *exp = req->rq_export;
2703 if (!exp->exp_obd->obd_no_transno && req->rq_repmsg != NULL)
2704 lustre_msg_set_last_committed(req->rq_repmsg,
2705 exp->exp_last_committed);
2707 DEBUG_REQ(D_IOCTL, req, "not sending last_committed update (%d/"
2708 "%d)", exp->exp_obd->obd_no_transno,
2709 req->rq_repmsg == NULL);
2711 CDEBUG(D_INFO, "last_committed "LPU64", transno "LPU64", xid "LPU64"\n",
2712 exp->exp_last_committed, req->rq_transno, req->rq_xid);
2715 #endif /* HAVE_SERVER_SUPPORT */
2718 * Packs current SLV and Limit into \a req.
2720 int target_pack_pool_reply(struct ptlrpc_request *req)
2722 struct obd_device *obd;
2725 /* Check that we still have all structures alive as this may
2726 * be some late RPC at shutdown time. */
2727 if (unlikely(!req->rq_export || !req->rq_export->exp_obd ||
2728 !exp_connect_lru_resize(req->rq_export))) {
2729 lustre_msg_set_slv(req->rq_repmsg, 0);
2730 lustre_msg_set_limit(req->rq_repmsg, 0);
2734 /* OBD is alive here as export is alive, which we checked above. */
2735 obd = req->rq_export->exp_obd;
2737 read_lock(&obd->obd_pool_lock);
2738 lustre_msg_set_slv(req->rq_repmsg, obd->obd_pool_slv);
2739 lustre_msg_set_limit(req->rq_repmsg, obd->obd_pool_limit);
2740 read_unlock(&obd->obd_pool_lock);
2745 static int target_send_reply_msg(struct ptlrpc_request *req,
2746 int rc, int fail_id)
2748 if (OBD_FAIL_CHECK_ORSET(fail_id & ~OBD_FAIL_ONCE, OBD_FAIL_ONCE)) {
2749 DEBUG_REQ(D_ERROR, req, "dropping reply");
2752 if (unlikely(lustre_msg_get_opc(req->rq_reqmsg) == MDS_REINT &&
2753 OBD_FAIL_CHECK(OBD_FAIL_MDS_REINT_MULTI_NET_REP)))
2757 DEBUG_REQ(D_NET, req, "processing error (%d)", rc);
2758 req->rq_status = rc;
2759 return ptlrpc_send_error(req, 1);
2761 DEBUG_REQ(D_NET, req, "sending reply");
2764 return ptlrpc_send_reply(req, PTLRPC_REPLY_MAYBE_DIFFICULT);
2767 void target_send_reply(struct ptlrpc_request *req, int rc, int fail_id)
2769 struct ptlrpc_service_part *svcpt;
2771 struct ptlrpc_reply_state *rs;
2772 struct obd_export *exp;
2775 if (req->rq_no_reply) {
2780 svcpt = req->rq_rqbd->rqbd_svcpt;
2781 rs = req->rq_reply_state;
2782 if (rs == NULL || !rs->rs_difficult) {
2784 target_send_reply_msg (req, rc, fail_id);
2789 /* must be an export if locks saved */
2790 LASSERT(req->rq_export != NULL);
2791 /* req/reply consistent */
2792 LASSERT(rs->rs_svcpt == svcpt);
2795 LASSERT(!rs->rs_scheduled);
2796 LASSERT(!rs->rs_scheduled_ever);
2797 LASSERT(!rs->rs_handled);
2798 LASSERT(!rs->rs_on_net);
2799 LASSERT(rs->rs_export == NULL);
2800 LASSERT(list_empty(&rs->rs_obd_list));
2801 LASSERT(list_empty(&rs->rs_exp_list));
2803 exp = class_export_get(req->rq_export);
2805 /* disable reply scheduling while I'm setting up */
2806 rs->rs_scheduled = 1;
2808 rs->rs_xid = req->rq_xid;
2809 rs->rs_transno = req->rq_transno;
2810 rs->rs_export = exp;
2811 rs->rs_opc = lustre_msg_get_opc(req->rq_reqmsg);
2813 spin_lock(&exp->exp_uncommitted_replies_lock);
2814 CDEBUG(D_NET, "rs transno = "LPU64", last committed = "LPU64"\n",
2815 rs->rs_transno, exp->exp_last_committed);
2816 if (rs->rs_transno > exp->exp_last_committed) {
2817 /* not committed already */
2818 list_add_tail(&rs->rs_obd_list,
2819 &exp->exp_uncommitted_replies);
2821 spin_unlock(&exp->exp_uncommitted_replies_lock);
2823 spin_lock(&exp->exp_lock);
2824 list_add_tail(&rs->rs_exp_list, &exp->exp_outstanding_replies);
2825 spin_unlock(&exp->exp_lock);
2827 netrc = target_send_reply_msg(req, rc, fail_id);
2829 spin_lock(&svcpt->scp_rep_lock);
2831 atomic_inc(&svcpt->scp_nreps_difficult);
2834 /* error sending: reply is off the net. Also we need +1
2835 * reply ref until ptlrpc_handle_rs() is done
2836 * with the reply state (if the send was successful, there
2837 * would have been +1 ref for the net, which
2838 * reply_out_callback leaves alone) */
2840 ptlrpc_rs_addref(rs);
2843 spin_lock(&rs->rs_lock);
2844 if (rs->rs_transno <= exp->exp_last_committed ||
2845 (!rs->rs_on_net && !rs->rs_no_ack) ||
2846 list_empty(&rs->rs_exp_list) || /* completed already */
2847 list_empty(&rs->rs_obd_list)) {
2848 CDEBUG(D_HA, "Schedule reply immediately\n");
2849 ptlrpc_dispatch_difficult_reply(rs);
2851 list_add(&rs->rs_list, &svcpt->scp_rep_active);
2852 rs->rs_scheduled = 0; /* allow notifier to schedule */
2854 spin_unlock(&rs->rs_lock);
2855 spin_unlock(&svcpt->scp_rep_lock);
2859 enum ldlm_mode lck_compat_array[] = {
2860 [LCK_EX] = LCK_COMPAT_EX,
2861 [LCK_PW] = LCK_COMPAT_PW,
2862 [LCK_PR] = LCK_COMPAT_PR,
2863 [LCK_CW] = LCK_COMPAT_CW,
2864 [LCK_CR] = LCK_COMPAT_CR,
2865 [LCK_NL] = LCK_COMPAT_NL,
2866 [LCK_GROUP] = LCK_COMPAT_GROUP,
2867 [LCK_COS] = LCK_COMPAT_COS,
2871 * Rather arbitrary mapping from LDLM error codes to errno values. This should
2872 * not escape to the user level.
2874 int ldlm_error2errno(enum ldlm_error error)
2880 case ELDLM_LOCK_MATCHED:
2883 case ELDLM_LOCK_CHANGED:
2886 case ELDLM_LOCK_ABORTED:
2889 case ELDLM_LOCK_REPLACED:
2892 case ELDLM_NO_LOCK_DATA:
2895 case ELDLM_NAMESPACE_EXISTS:
2898 case ELDLM_BAD_NAMESPACE:
2902 if (((int)error) < 0) { /* cast to signed type */
2903 result = error; /* as ldlm_error can be unsigned */
2905 CERROR("Invalid DLM result code: %d\n", error);
2911 EXPORT_SYMBOL(ldlm_error2errno);
2914 * Dual to ldlm_error2errno(): maps errno values back to enum ldlm_error.
2916 enum ldlm_error ldlm_errno2error(int err_no)
2925 error = ELDLM_LOCK_CHANGED;
2928 error = ELDLM_LOCK_ABORTED;
2931 error = ELDLM_LOCK_REPLACED;
2934 error = ELDLM_NO_LOCK_DATA;
2937 error = ELDLM_NAMESPACE_EXISTS;
2940 error = ELDLM_BAD_NAMESPACE;
2948 #if LUSTRE_TRACKS_LOCK_EXP_REFS
2949 void ldlm_dump_export_locks(struct obd_export *exp)
2951 spin_lock(&exp->exp_locks_list_guard);
2952 if (!list_empty(&exp->exp_locks_list)) {
2953 struct ldlm_lock *lock;
2955 CERROR("dumping locks for export %p,"
2956 "ignore if the unmount doesn't hang\n", exp);
2957 list_for_each_entry(lock, &exp->exp_locks_list,
2959 LDLM_ERROR(lock, "lock:");
2961 spin_unlock(&exp->exp_locks_list_guard);
2965 #ifdef HAVE_SERVER_SUPPORT
2966 static int target_bulk_timeout(void *data)
2969 /* We don't fail the connection here, because having the export
2970 * killed makes the (vital) call to commitrw very sad.
2975 static inline const char *bulk2type(struct ptlrpc_request *req)
2977 if (req->rq_bulk_read)
2979 if (req->rq_bulk_write)
2984 int target_bulk_io(struct obd_export *exp, struct ptlrpc_bulk_desc *desc,
2985 struct l_wait_info *lwi)
2987 struct ptlrpc_request *req = desc->bd_req;
2988 time_t start = cfs_time_current_sec();
2994 /* If there is eviction in progress, wait for it to finish. */
2995 if (unlikely(atomic_read(&exp->exp_obd->obd_evict_inprogress))) {
2996 *lwi = LWI_INTR(NULL, NULL);
2997 rc = l_wait_event(exp->exp_obd->obd_evict_inprogress_waitq,
2998 !atomic_read(&exp->exp_obd->
2999 obd_evict_inprogress),
3003 /* Check if client was evicted or reconnected already. */
3004 if (exp->exp_failed ||
3005 exp->exp_conn_cnt > lustre_msg_get_conn_cnt(req->rq_reqmsg)) {
3008 if (req->rq_bulk_read)
3009 rc = sptlrpc_svc_wrap_bulk(req, desc);
3011 if ((exp->exp_connect_data.ocd_connect_flags &
3012 OBD_CONNECT_BULK_MBITS) != 0)
3013 req->rq_mbits = lustre_msg_get_mbits(req->rq_reqmsg);
3014 else /* old version, bulk matchbits is rq_xid */
3015 req->rq_mbits = req->rq_xid;
3018 rc = ptlrpc_start_bulk_transfer(desc);
3022 DEBUG_REQ(D_ERROR, req, "bulk %s failed: rc %d",
3023 bulk2type(req), rc);
3027 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE)) {
3028 ptlrpc_abort_bulk(desc);
3032 /* limit actual bulk transfer to bulk_timeout seconds */
3033 deadline = start + bulk_timeout;
3034 if (deadline > req->rq_deadline)
3035 deadline = req->rq_deadline;
3038 long timeoutl = deadline - cfs_time_current_sec();
3039 cfs_duration_t timeout = timeoutl <= 0 ?
3040 CFS_TICK : cfs_time_seconds(timeoutl);
3043 *lwi = LWI_TIMEOUT_INTERVAL(timeout, cfs_time_seconds(1),
3044 target_bulk_timeout, desc);
3045 rc = l_wait_event(desc->bd_waitq,
3046 !ptlrpc_server_bulk_active(desc) ||
3049 lustre_msg_get_conn_cnt(req->rq_reqmsg),
3051 LASSERT(rc == 0 || rc == -ETIMEDOUT);
3052 /* Wait again if we changed rq_deadline. */
3053 rq_deadline = ACCESS_ONCE(req->rq_deadline);
3054 deadline = start + bulk_timeout;
3055 if (deadline > rq_deadline)
3056 deadline = rq_deadline;
3057 } while ((rc == -ETIMEDOUT) &&
3058 (deadline > cfs_time_current_sec()));
3060 if (rc == -ETIMEDOUT) {
3061 DEBUG_REQ(D_ERROR, req, "timeout on bulk %s after %ld%+lds",
3062 bulk2type(req), deadline - start,
3063 cfs_time_current_sec() - deadline);
3064 ptlrpc_abort_bulk(desc);
3065 } else if (exp->exp_failed) {
3066 DEBUG_REQ(D_ERROR, req, "Eviction on bulk %s",
3069 ptlrpc_abort_bulk(desc);
3070 } else if (exp->exp_conn_cnt >
3071 lustre_msg_get_conn_cnt(req->rq_reqmsg)) {
3072 DEBUG_REQ(D_ERROR, req, "Reconnect on bulk %s",
3074 /* We don't reply anyway. */
3076 ptlrpc_abort_bulk(desc);
3077 } else if (desc->bd_failure) {
3078 DEBUG_REQ(D_ERROR, req, "network error on bulk %s",
3080 /* XXX should this be a different errno? */
3083 if (req->rq_bulk_write)
3084 rc = sptlrpc_svc_unwrap_bulk(req, desc);
3085 if (rc == 0 && desc->bd_nob_transferred != desc->bd_nob) {
3086 DEBUG_REQ(D_ERROR, req, "truncated bulk %s %d(%d)",
3087 bulk2type(req), desc->bd_nob_transferred,
3089 /* XXX should this be a different errno? */
3096 EXPORT_SYMBOL(target_bulk_io);
3098 #endif /* HAVE_SERVER_SUPPORT */