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, 2013, 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
47 # include <libcfs/libcfs.h>
49 # include <liblustre.h>
52 #include <obd_class.h>
53 #include <lustre_dlm.h>
54 #include <lustre_net.h>
55 #include <lustre_sec.h>
56 #include "ldlm_internal.h"
58 /* @priority: If non-zero, move the selected connection to the list head.
59 * @create: If zero, only search in existing connections.
61 static int import_set_conn(struct obd_import *imp, struct obd_uuid *uuid,
62 int priority, int create)
64 struct ptlrpc_connection *ptlrpc_conn;
65 struct obd_import_conn *imp_conn = NULL, *item;
69 if (!create && !priority) {
70 CDEBUG(D_HA, "Nothing to do\n");
74 ptlrpc_conn = ptlrpc_uuid_to_connection(uuid);
76 CDEBUG(D_HA, "can't find connection %s\n", uuid->uuid);
81 OBD_ALLOC(imp_conn, sizeof(*imp_conn));
83 GOTO(out_put, rc = -ENOMEM);
87 spin_lock(&imp->imp_lock);
88 cfs_list_for_each_entry(item, &imp->imp_conn_list, oic_item) {
89 if (obd_uuid_equals(uuid, &item->oic_uuid)) {
91 cfs_list_del(&item->oic_item);
92 cfs_list_add(&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 cfs_list_add(&imp_conn->oic_item, &imp->imp_conn_list);
111 cfs_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_del_conn(struct obd_import *imp, struct obd_uuid *uuid)
145 struct obd_import_conn *imp_conn;
146 struct obd_export *dlmexp;
150 spin_lock(&imp->imp_lock);
151 if (cfs_list_empty(&imp->imp_conn_list)) {
152 LASSERT(!imp->imp_connection);
156 cfs_list_for_each_entry(imp_conn, &imp->imp_conn_list, oic_item) {
157 if (!obd_uuid_equals(uuid, &imp_conn->oic_uuid))
159 LASSERT(imp_conn->oic_conn);
161 if (imp_conn == imp->imp_conn_current) {
162 LASSERT(imp_conn->oic_conn == imp->imp_connection);
164 if (imp->imp_state != LUSTRE_IMP_CLOSED &&
165 imp->imp_state != LUSTRE_IMP_DISCON) {
166 CERROR("can't remove current connection\n");
167 GOTO(out, rc = -EBUSY);
170 ptlrpc_connection_put(imp->imp_connection);
171 imp->imp_connection = NULL;
173 dlmexp = class_conn2export(&imp->imp_dlm_handle);
174 if (dlmexp && dlmexp->exp_connection) {
175 LASSERT(dlmexp->exp_connection ==
177 ptlrpc_connection_put(dlmexp->exp_connection);
178 dlmexp->exp_connection = NULL;
182 cfs_list_del(&imp_conn->oic_item);
183 ptlrpc_connection_put(imp_conn->oic_conn);
184 OBD_FREE(imp_conn, sizeof(*imp_conn));
185 CDEBUG(D_HA, "imp %p@%s: remove connection %s\n",
186 imp, imp->imp_obd->obd_name, uuid->uuid);
191 spin_unlock(&imp->imp_lock);
193 CERROR("connection %s not found\n", uuid->uuid);
196 EXPORT_SYMBOL(client_import_del_conn);
199 * Find conn UUID by peer NID. \a peer is a server NID. This function is used
200 * to find a conn uuid of \a imp which can reach \a peer.
202 int client_import_find_conn(struct obd_import *imp, lnet_nid_t peer,
203 struct obd_uuid *uuid)
205 struct obd_import_conn *conn;
209 spin_lock(&imp->imp_lock);
210 cfs_list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
211 /* Check if conn UUID does have this peer NID. */
212 if (class_check_uuid(&conn->oic_uuid, peer)) {
213 *uuid = conn->oic_uuid;
218 spin_unlock(&imp->imp_lock);
221 EXPORT_SYMBOL(client_import_find_conn);
223 void client_destroy_import(struct obd_import *imp)
225 /* Drop security policy instance after all RPCs have finished/aborted
226 * to let all busy contexts be released. */
227 class_import_get(imp);
228 class_destroy_import(imp);
229 sptlrpc_import_sec_put(imp);
230 class_import_put(imp);
232 EXPORT_SYMBOL(client_destroy_import);
235 * Check whether or not the OSC is on MDT.
238 * setup 0:{fsname}-OSTxxxx-osc[-MDTxxxx] 1:lustre-OST0000_UUID 2:NID
240 * setup 0:{fsname}-OSTxxxx-osc 1:lustre-OST0000_UUID 2:NID
243 static int osc_on_mdt(char *obdname)
247 ptr = strrchr(obdname, '-');
251 if (strncmp(ptr + 1, "MDT", 3) == 0)
257 /* Configure an RPC client OBD device.
262 * 3 - inactive-on-startup
264 int client_obd_setup(struct obd_device *obddev, struct lustre_cfg *lcfg)
266 struct client_obd *cli = &obddev->u.cli;
267 struct obd_import *imp;
268 struct obd_uuid server_uuid;
269 int rq_portal, rp_portal, connect_op;
270 char *name = obddev->obd_type->typ_name;
271 ldlm_ns_type_t ns_type = LDLM_NS_TYPE_UNKNOWN;
275 /* In a more perfect world, we would hang a ptlrpc_client off of
276 * obd_type and just use the values from there. */
277 if (!strcmp(name, LUSTRE_OSC_NAME)) {
278 rq_portal = OST_REQUEST_PORTAL;
279 rp_portal = OSC_REPLY_PORTAL;
280 connect_op = OST_CONNECT;
281 cli->cl_sp_me = LUSTRE_SP_CLI;
282 cli->cl_sp_to = LUSTRE_SP_OST;
283 ns_type = LDLM_NS_TYPE_OSC;
284 } else if (!strcmp(name, LUSTRE_MDC_NAME) ||
285 !strcmp(name, LUSTRE_LWP_NAME)) {
286 rq_portal = MDS_REQUEST_PORTAL;
287 rp_portal = MDC_REPLY_PORTAL;
288 connect_op = MDS_CONNECT;
289 cli->cl_sp_me = LUSTRE_SP_CLI;
290 cli->cl_sp_to = LUSTRE_SP_MDT;
291 ns_type = LDLM_NS_TYPE_MDC;
292 } else if (!strcmp(name, LUSTRE_OSP_NAME)) {
293 if (strstr(lustre_cfg_buf(lcfg, 1), "OST") == NULL) {
294 /* OSP_on_MDT for other MDTs */
295 connect_op = MDS_CONNECT;
296 cli->cl_sp_to = LUSTRE_SP_MDT;
297 ns_type = LDLM_NS_TYPE_MDC;
298 rq_portal = OUT_PORTAL;
300 /* OSP on MDT for OST */
301 connect_op = OST_CONNECT;
302 cli->cl_sp_to = LUSTRE_SP_OST;
303 ns_type = LDLM_NS_TYPE_OSC;
304 rq_portal = OST_REQUEST_PORTAL;
306 rp_portal = OSC_REPLY_PORTAL;
307 cli->cl_sp_me = LUSTRE_SP_CLI;
308 } else if (!strcmp(name, LUSTRE_MGC_NAME)) {
309 rq_portal = MGS_REQUEST_PORTAL;
310 rp_portal = MGC_REPLY_PORTAL;
311 connect_op = MGS_CONNECT;
312 cli->cl_sp_me = LUSTRE_SP_MGC;
313 cli->cl_sp_to = LUSTRE_SP_MGS;
314 cli->cl_flvr_mgc.sf_rpc = SPTLRPC_FLVR_INVALID;
315 ns_type = LDLM_NS_TYPE_MGC;
317 CERROR("unknown client OBD type \"%s\", can't setup\n",
322 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
323 CERROR("requires a TARGET UUID\n");
327 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > 37) {
328 CERROR("client UUID must be less than 38 characters\n");
332 if (LUSTRE_CFG_BUFLEN(lcfg, 2) < 1) {
333 CERROR("setup requires a SERVER UUID\n");
337 if (LUSTRE_CFG_BUFLEN(lcfg, 2) > 37) {
338 CERROR("target UUID must be less than 38 characters\n");
342 init_rwsem(&cli->cl_sem);
343 mutex_init(&cli->cl_mgc_mutex);
344 cli->cl_conn_count = 0;
345 memcpy(server_uuid.uuid, lustre_cfg_buf(lcfg, 2),
346 min_t(unsigned int, LUSTRE_CFG_BUFLEN(lcfg, 2),
347 sizeof(server_uuid)));
350 cli->cl_avail_grant = 0;
351 /* FIXME: Should limit this for the sum of all cl_dirty_max. */
352 cli->cl_dirty_max = OSC_MAX_DIRTY_DEFAULT * 1024 * 1024;
353 if (cli->cl_dirty_max >> PAGE_CACHE_SHIFT > totalram_pages / 8)
354 cli->cl_dirty_max = totalram_pages << (PAGE_CACHE_SHIFT - 3);
355 CFS_INIT_LIST_HEAD(&cli->cl_cache_waiters);
356 CFS_INIT_LIST_HEAD(&cli->cl_loi_ready_list);
357 CFS_INIT_LIST_HEAD(&cli->cl_loi_hp_ready_list);
358 CFS_INIT_LIST_HEAD(&cli->cl_loi_write_list);
359 CFS_INIT_LIST_HEAD(&cli->cl_loi_read_list);
360 client_obd_list_lock_init(&cli->cl_loi_list_lock);
361 atomic_set(&cli->cl_pending_w_pages, 0);
362 atomic_set(&cli->cl_pending_r_pages, 0);
363 cli->cl_r_in_flight = 0;
364 cli->cl_w_in_flight = 0;
366 spin_lock_init(&cli->cl_read_rpc_hist.oh_lock);
367 spin_lock_init(&cli->cl_write_rpc_hist.oh_lock);
368 spin_lock_init(&cli->cl_read_page_hist.oh_lock);
369 spin_lock_init(&cli->cl_write_page_hist.oh_lock);
370 spin_lock_init(&cli->cl_read_offset_hist.oh_lock);
371 spin_lock_init(&cli->cl_write_offset_hist.oh_lock);
374 CFS_INIT_LIST_HEAD(&cli->cl_lru_osc);
375 atomic_set(&cli->cl_lru_shrinkers, 0);
376 atomic_set(&cli->cl_lru_busy, 0);
377 atomic_set(&cli->cl_lru_in_list, 0);
378 CFS_INIT_LIST_HEAD(&cli->cl_lru_list);
379 client_obd_list_lock_init(&cli->cl_lru_list_lock);
380 atomic_set(&cli->cl_unstable_count, 0);
382 init_waitqueue_head(&cli->cl_destroy_waitq);
383 atomic_set(&cli->cl_destroy_in_flight, 0);
384 #ifdef ENABLE_CHECKSUM
385 /* Turn on checksumming by default. */
386 cli->cl_checksum = 1;
388 * The supported checksum types will be worked out at connect time
389 * Set cl_chksum* to CRC32 for now to avoid returning screwed info
392 cli->cl_cksum_type = cli->cl_supp_cksum_types = OBD_CKSUM_CRC32;
394 atomic_set(&cli->cl_resends, OSC_DEFAULT_RESENDS);
396 /* This value may be reduced at connect time in
397 * ptlrpc_connect_interpret() . We initialize it to only
398 * 1MB until we know what the performance looks like.
399 * In the future this should likely be increased. LU-1431 */
400 cli->cl_max_pages_per_rpc = min_t(int, PTLRPC_MAX_BRW_PAGES,
401 LNET_MTU >> PAGE_CACHE_SHIFT);
403 /* set cl_chunkbits default value to PAGE_CACHE_SHIFT,
404 * it will be updated at OSC connection time. */
405 cli->cl_chunkbits = PAGE_CACHE_SHIFT;
407 if (!strcmp(name, LUSTRE_MDC_NAME)) {
408 cli->cl_max_rpcs_in_flight = OBD_MAX_RIF_DEFAULT;
409 } else if (totalram_pages >> (20 - PAGE_CACHE_SHIFT) <= 128 /* MB */) {
410 cli->cl_max_rpcs_in_flight = 2;
411 } else if (totalram_pages >> (20 - PAGE_CACHE_SHIFT) <= 256 /* MB */) {
412 cli->cl_max_rpcs_in_flight = 3;
413 } else if (totalram_pages >> (20 - PAGE_CACHE_SHIFT) <= 512 /* MB */) {
414 cli->cl_max_rpcs_in_flight = 4;
416 if (osc_on_mdt(obddev->obd_name))
417 cli->cl_max_rpcs_in_flight = OBD_MAX_RIF_MAX;
419 cli->cl_max_rpcs_in_flight = OBD_MAX_RIF_DEFAULT;
423 CERROR("ldlm_get_ref failed: %d\n", rc);
427 ptlrpc_init_client(rq_portal, rp_portal, name,
428 &obddev->obd_ldlm_client);
430 imp = class_new_import(obddev);
432 GOTO(err_ldlm, rc = -ENOENT);
433 imp->imp_client = &obddev->obd_ldlm_client;
434 imp->imp_connect_op = connect_op;
435 memcpy(cli->cl_target_uuid.uuid, lustre_cfg_buf(lcfg, 1),
436 LUSTRE_CFG_BUFLEN(lcfg, 1));
437 class_import_put(imp);
439 rc = client_import_add_conn(imp, &server_uuid, 1);
441 CERROR("can't add initial connection\n");
442 GOTO(err_import, rc);
445 cli->cl_import = imp;
446 /* cli->cl_max_mds_{easize,cookiesize} updated by mdc_init_ea_size() */
447 cli->cl_max_mds_easize = sizeof(struct lov_mds_md_v3);
448 cli->cl_max_mds_cookiesize = sizeof(struct llog_cookie);
450 if (LUSTRE_CFG_BUFLEN(lcfg, 3) > 0) {
451 if (!strcmp(lustre_cfg_string(lcfg, 3), "inactive")) {
452 CDEBUG(D_HA, "marking %s %s->%s as inactive\n",
453 name, obddev->obd_name,
454 cli->cl_target_uuid.uuid);
455 spin_lock(&imp->imp_lock);
456 imp->imp_deactive = 1;
457 spin_unlock(&imp->imp_lock);
461 obddev->obd_namespace = ldlm_namespace_new(obddev, obddev->obd_name,
462 LDLM_NAMESPACE_CLIENT,
463 LDLM_NAMESPACE_GREEDY,
465 if (obddev->obd_namespace == NULL) {
466 CERROR("Unable to create client namespace - %s\n",
468 GOTO(err_import, rc = -ENOMEM);
471 cli->cl_qchk_stat = CL_NOT_QUOTACHECKED;
476 class_destroy_import(imp);
483 EXPORT_SYMBOL(client_obd_setup);
485 int client_obd_cleanup(struct obd_device *obddev)
489 ldlm_namespace_free_post(obddev->obd_namespace);
490 obddev->obd_namespace = NULL;
492 obd_cleanup_client_import(obddev);
493 LASSERT(obddev->u.cli.cl_import == NULL);
498 EXPORT_SYMBOL(client_obd_cleanup);
500 /* ->o_connect() method for client side (OSC and MDC and MGC) */
501 int client_connect_import(const struct lu_env *env,
502 struct obd_export **exp,
503 struct obd_device *obd, struct obd_uuid *cluuid,
504 struct obd_connect_data *data, void *localdata)
506 struct client_obd *cli = &obd->u.cli;
507 struct obd_import *imp = cli->cl_import;
508 struct obd_connect_data *ocd;
509 struct lustre_handle conn = { 0 };
514 down_write(&cli->cl_sem);
515 if (cli->cl_conn_count > 0 )
516 GOTO(out_sem, rc = -EALREADY);
518 rc = class_connect(&conn, obd, cluuid);
522 cli->cl_conn_count++;
523 *exp = class_conn2export(&conn);
525 LASSERT(obd->obd_namespace);
527 imp->imp_dlm_handle = conn;
528 rc = ptlrpc_init_import(imp);
532 ocd = &imp->imp_connect_data;
535 imp->imp_connect_flags_orig = data->ocd_connect_flags;
538 rc = ptlrpc_connect_import(imp);
540 LASSERT (imp->imp_state == LUSTRE_IMP_DISCON);
543 LASSERT(*exp != NULL && (*exp)->exp_connection);
546 LASSERTF((ocd->ocd_connect_flags & data->ocd_connect_flags) ==
547 ocd->ocd_connect_flags, "old "LPX64", new "LPX64"\n",
548 data->ocd_connect_flags, ocd->ocd_connect_flags);
549 data->ocd_connect_flags = ocd->ocd_connect_flags;
552 ptlrpc_pinger_add_import(imp);
558 cli->cl_conn_count--;
559 class_disconnect(*exp);
563 up_write(&cli->cl_sem);
567 EXPORT_SYMBOL(client_connect_import);
569 int client_disconnect_export(struct obd_export *exp)
571 struct obd_device *obd = class_exp2obd(exp);
572 struct client_obd *cli;
573 struct obd_import *imp;
578 CERROR("invalid export for disconnect: exp %p cookie "LPX64"\n",
579 exp, exp ? exp->exp_handle.h_cookie : -1);
584 imp = cli->cl_import;
586 down_write(&cli->cl_sem);
587 CDEBUG(D_INFO, "disconnect %s - %d\n", obd->obd_name,
590 if (!cli->cl_conn_count) {
591 CERROR("disconnecting disconnected device (%s)\n",
593 GOTO(out_disconnect, rc = -EINVAL);
596 cli->cl_conn_count--;
597 if (cli->cl_conn_count)
598 GOTO(out_disconnect, rc = 0);
600 /* Mark import deactivated now, so we don't try to reconnect if any
601 * of the cleanup RPCs fails (e.g. LDLM cancel, etc). We don't
602 * fully deactivate the import, or that would drop all requests. */
603 spin_lock(&imp->imp_lock);
604 imp->imp_deactive = 1;
605 spin_unlock(&imp->imp_lock);
607 /* Some non-replayable imports (MDS's OSCs) are pinged, so just
608 * delete it regardless. (It's safe to delete an import that was
610 (void)ptlrpc_pinger_del_import(imp);
612 if (obd->obd_namespace != NULL) {
613 /* obd_force == local only */
614 ldlm_cli_cancel_unused(obd->obd_namespace, NULL,
615 obd->obd_force ? LCF_LOCAL : 0, NULL);
616 ldlm_namespace_free_prior(obd->obd_namespace, imp, obd->obd_force);
619 /* There's no need to hold sem while disconnecting an import,
620 * and it may actually cause deadlock in GSS. */
621 up_write(&cli->cl_sem);
622 rc = ptlrpc_disconnect_import(imp, 0);
623 down_write(&cli->cl_sem);
625 ptlrpc_invalidate_import(imp);
630 /* Use server style - class_disconnect should be always called for
632 err = class_disconnect(exp);
636 up_write(&cli->cl_sem);
640 EXPORT_SYMBOL(client_disconnect_export);
642 #ifdef HAVE_SERVER_SUPPORT
643 int server_disconnect_export(struct obd_export *exp)
648 /* Disconnect early so that clients can't keep using export. */
649 rc = class_disconnect(exp);
650 /* Close import to avoid sending any requests. */
651 if (exp->exp_imp_reverse)
652 ptlrpc_cleanup_imp(exp->exp_imp_reverse);
654 if (exp->exp_obd->obd_namespace != NULL)
655 ldlm_cancel_locks_for_export(exp);
657 /* complete all outstanding replies */
658 spin_lock(&exp->exp_lock);
659 while (!cfs_list_empty(&exp->exp_outstanding_replies)) {
660 struct ptlrpc_reply_state *rs =
661 cfs_list_entry(exp->exp_outstanding_replies.next,
662 struct ptlrpc_reply_state, rs_exp_list);
663 struct ptlrpc_service_part *svcpt = rs->rs_svcpt;
665 spin_lock(&svcpt->scp_rep_lock);
667 cfs_list_del_init(&rs->rs_exp_list);
668 spin_lock(&rs->rs_lock);
669 ptlrpc_schedule_difficult_reply(rs);
670 spin_unlock(&rs->rs_lock);
672 spin_unlock(&svcpt->scp_rep_lock);
674 spin_unlock(&exp->exp_lock);
678 EXPORT_SYMBOL(server_disconnect_export);
680 /* --------------------------------------------------------------------------
681 * from old lib/target.c
682 * -------------------------------------------------------------------------- */
684 static int target_handle_reconnect(struct lustre_handle *conn,
685 struct obd_export *exp,
686 struct obd_uuid *cluuid)
690 if (exp->exp_connection && exp->exp_imp_reverse) {
691 struct lustre_handle *hdl;
692 struct obd_device *target;
694 hdl = &exp->exp_imp_reverse->imp_remote_handle;
695 target = exp->exp_obd;
697 /* Might be a re-connect after a partition. */
698 if (!memcmp(&conn->cookie, &hdl->cookie, sizeof conn->cookie)) {
699 if (target->obd_recovering) {
700 int timeout = cfs_duration_sec(cfs_time_sub(
702 &target->obd_recovery_timer),
703 cfs_time_current()));
705 LCONSOLE_WARN("%s: Client %s (at %s) reconnect"
706 "ing, waiting for %d clients in recov"
707 "ery for %d:%.02d\n", target->obd_name,
708 obd_uuid2str(&exp->exp_client_uuid),
709 obd_export_nid2str(exp),
710 target->obd_max_recoverable_clients,
711 timeout / 60, timeout % 60);
713 LCONSOLE_WARN("%s: Client %s (at %s) "
714 "reconnecting\n", target->obd_name,
715 obd_uuid2str(&exp->exp_client_uuid),
716 obd_export_nid2str(exp));
719 conn->cookie = exp->exp_handle.h_cookie;
720 /* target_handle_connect() treats EALREADY and
721 * -EALREADY differently. EALREADY means we are
722 * doing a valid reconnect from the same client. */
725 LCONSOLE_WARN("%s: already connected client %s (at %s) "
726 "with handle "LPX64". Rejecting client "
727 "with the same UUID trying to reconnect "
728 "with handle "LPX64"\n", target->obd_name,
729 obd_uuid2str(&exp->exp_client_uuid),
730 obd_export_nid2str(exp),
731 hdl->cookie, conn->cookie);
732 memset(conn, 0, sizeof *conn);
733 /* target_handle_connect() treats EALREADY and
734 * -EALREADY differently. -EALREADY is an error
735 * (same UUID, different handle). */
740 conn->cookie = exp->exp_handle.h_cookie;
741 CDEBUG(D_HA, "connect export for UUID '%s' at %p, cookie "LPX64"\n",
742 cluuid->uuid, exp, conn->cookie);
746 void target_client_add_cb(struct obd_device *obd, __u64 transno, void *cb_data,
749 struct obd_export *exp = cb_data;
751 CDEBUG(D_RPCTRACE, "%s: committing for initial connect of %s\n",
752 obd->obd_name, exp->exp_client_uuid.uuid);
754 spin_lock(&exp->exp_lock);
755 exp->exp_need_sync = 0;
756 spin_unlock(&exp->exp_lock);
757 class_export_cb_put(exp);
759 EXPORT_SYMBOL(target_client_add_cb);
763 check_and_start_recovery_timer(struct obd_device *obd,
764 struct ptlrpc_request *req, int new_client);
767 check_and_start_recovery_timer(struct obd_device *obd,
768 struct ptlrpc_request *req, int new_client)
773 int target_handle_connect(struct ptlrpc_request *req)
775 struct obd_device *target = NULL, *targref = NULL;
776 struct obd_export *export = NULL;
777 struct obd_import *revimp;
778 struct obd_import *tmp_imp = NULL;
779 struct lustre_handle conn;
780 struct lustre_handle *tmp;
781 struct obd_uuid tgtuuid;
782 struct obd_uuid cluuid;
783 struct obd_uuid remote_uuid;
788 bool mds_conn = false, lw_client = false;
789 struct obd_connect_data *data, *tmpdata;
791 lnet_nid_t *client_nid = NULL;
794 OBD_RACE(OBD_FAIL_TGT_CONN_RACE);
796 str = req_capsule_client_get(&req->rq_pill, &RMF_TGTUUID);
798 DEBUG_REQ(D_ERROR, req, "bad target UUID for connect");
799 GOTO(out, rc = -EINVAL);
802 obd_str2uuid(&tgtuuid, str);
803 target = class_uuid2obd(&tgtuuid);
805 target = class_name2obd(str);
808 deuuidify(str, NULL, &target_start, &target_len);
809 LCONSOLE_ERROR_MSG(0x137, "%s: not available for connect "
810 "from %s (no target). If you are running "
811 "an HA pair check that the target is "
812 "mounted on the other server.\n", str,
813 libcfs_nid2str(req->rq_peer.nid));
814 GOTO(out, rc = -ENODEV);
817 spin_lock(&target->obd_dev_lock);
818 if (target->obd_stopping || !target->obd_set_up) {
819 spin_unlock(&target->obd_dev_lock);
821 deuuidify(str, NULL, &target_start, &target_len);
822 LCONSOLE_INFO("%.*s: Not available for connect from %s (%s)\n",
823 target_len, target_start,
824 libcfs_nid2str(req->rq_peer.nid),
825 (target->obd_stopping ?
826 "stopping" : "not set up"));
827 GOTO(out, rc = -ENODEV);
830 if (target->obd_no_conn) {
831 spin_unlock(&target->obd_dev_lock);
833 CDEBUG(D_INFO, "%s: Temporarily refusing client connection "
834 "from %s\n", target->obd_name,
835 libcfs_nid2str(req->rq_peer.nid));
836 GOTO(out, rc = -EAGAIN);
839 /* Make sure the target isn't cleaned up while we're here. Yes,
840 * there's still a race between the above check and our incref here.
841 * Really, class_uuid2obd should take the ref. */
842 targref = class_incref(target, __FUNCTION__, current);
844 target->obd_conn_inprogress++;
845 spin_unlock(&target->obd_dev_lock);
847 str = req_capsule_client_get(&req->rq_pill, &RMF_CLUUID);
849 DEBUG_REQ(D_ERROR, req, "bad client UUID for connect");
850 GOTO(out, rc = -EINVAL);
853 obd_str2uuid(&cluuid, str);
855 /* XXX Extract a nettype and format accordingly. */
856 switch (sizeof(lnet_nid_t)) {
857 /* NB the casts only avoid compiler warnings. */
859 snprintf(remote_uuid.uuid, sizeof remote_uuid,
860 "NET_"LPX64"_UUID", (__u64)req->rq_peer.nid);
863 snprintf(remote_uuid.uuid, sizeof remote_uuid,
864 "NET_%x_UUID", (__u32)req->rq_peer.nid);
870 tmp = req_capsule_client_get(&req->rq_pill, &RMF_CONN);
872 GOTO(out, rc = -EPROTO);
876 size = req_capsule_get_size(&req->rq_pill, &RMF_CONNECT_DATA,
878 data = req_capsule_client_get(&req->rq_pill, &RMF_CONNECT_DATA);
880 GOTO(out, rc = -EPROTO);
882 rc = req_capsule_server_pack(&req->rq_pill);
886 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT) {
887 if (data->ocd_version < LUSTRE_VERSION_CODE -
888 LUSTRE_VERSION_ALLOWED_OFFSET ||
889 data->ocd_version > LUSTRE_VERSION_CODE +
890 LUSTRE_VERSION_ALLOWED_OFFSET) {
891 DEBUG_REQ(D_WARNING, req, "Refusing %s (%d.%d.%d.%d) "
892 "libclient connection attempt",
893 data->ocd_version < LUSTRE_VERSION_CODE ?
895 OBD_OCD_VERSION_MAJOR(data->ocd_version),
896 OBD_OCD_VERSION_MINOR(data->ocd_version),
897 OBD_OCD_VERSION_PATCH(data->ocd_version),
898 OBD_OCD_VERSION_FIX(data->ocd_version));
899 data = req_capsule_server_sized_get(&req->rq_pill,
901 offsetof(typeof(*data), ocd_version) +
902 sizeof(data->ocd_version));
904 data->ocd_connect_flags = OBD_CONNECT_VERSION;
905 data->ocd_version = LUSTRE_VERSION_CODE;
907 GOTO(out, rc = -EPROTO);
911 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_INITIAL) {
912 mds_conn = (data->ocd_connect_flags & OBD_CONNECT_MDS) != 0;
913 lw_client = (data->ocd_connect_flags &
914 OBD_CONNECT_LIGHTWEIGHT) != 0;
917 /* lctl gets a backstage, all-access pass. */
918 if (obd_uuid_equals(&cluuid, &target->obd_uuid))
919 goto dont_check_exports;
921 export = cfs_hash_lookup(target->obd_uuid_hash, &cluuid);
925 /* We've found an export in the hash. */
927 spin_lock(&export->exp_lock);
929 if (export->exp_connecting) { /* bug 9635, et. al. */
930 spin_unlock(&export->exp_lock);
931 LCONSOLE_WARN("%s: Export %p already connecting from %s\n",
932 export->exp_obd->obd_name, export,
933 libcfs_nid2str(req->rq_peer.nid));
934 class_export_put(export);
937 } else if ((mds_conn || lw_client) && export->exp_connection != NULL) {
938 spin_unlock(&export->exp_lock);
939 if (req->rq_peer.nid != export->exp_connection->c_peer.nid)
940 /* MDS or LWP reconnected after failover. */
941 LCONSOLE_WARN("%s: Received %s connection from "
942 "%s, removing former export from %s\n",
943 target->obd_name, mds_conn ? "MDS" : "LWP",
944 libcfs_nid2str(req->rq_peer.nid),
945 libcfs_nid2str(export->exp_connection->c_peer.nid));
947 /* New MDS connection from the same NID. */
948 LCONSOLE_WARN("%s: Received new %s connection from "
949 "%s, removing former export from same NID\n",
950 target->obd_name, mds_conn ? "MDS" : "LWP",
951 libcfs_nid2str(req->rq_peer.nid));
952 class_fail_export(export);
953 class_export_put(export);
956 } else if (export->exp_connection != NULL &&
957 req->rq_peer.nid != export->exp_connection->c_peer.nid &&
958 (lustre_msg_get_op_flags(req->rq_reqmsg) &
959 MSG_CONNECT_INITIAL)) {
960 spin_unlock(&export->exp_lock);
961 /* In MDS failover we have static UUID but NID can change. */
962 LCONSOLE_WARN("%s: Client %s seen on new nid %s when "
963 "existing nid %s is already connected\n",
964 target->obd_name, cluuid.uuid,
965 libcfs_nid2str(req->rq_peer.nid),
967 export->exp_connection->c_peer.nid));
969 class_export_put(export);
972 export->exp_connecting = 1;
973 spin_unlock(&export->exp_lock);
974 LASSERT(export->exp_obd == target);
976 rc = target_handle_reconnect(&conn, export, &cluuid);
979 /* If we found an export, we already unlocked. */
982 OBD_FAIL_TIMEOUT(OBD_FAIL_TGT_DELAY_CONNECT, 2 * obd_timeout);
983 } else if (req->rq_export == NULL &&
984 atomic_read(&export->exp_rpc_count) > 0) {
985 LCONSOLE_WARN("%s: Client %s (at %s) refused connection, "
986 "still busy with %d references\n",
987 target->obd_name, cluuid.uuid,
988 libcfs_nid2str(req->rq_peer.nid),
989 atomic_read(&export->exp_refcount));
990 GOTO(out, rc = -EBUSY);
991 } else if (lustre_msg_get_conn_cnt(req->rq_reqmsg) == 1) {
992 if (!strstr(cluuid.uuid, "mdt"))
993 LCONSOLE_WARN("%s: Rejecting reconnect from the "
994 "known client %s (at %s) because it "
995 "is indicating it is a new client",
996 target->obd_name, cluuid.uuid,
997 libcfs_nid2str(req->rq_peer.nid));
998 GOTO(out, rc = -EALREADY);
1000 OBD_FAIL_TIMEOUT(OBD_FAIL_TGT_DELAY_RECONNECT, 2 * obd_timeout);
1007 CDEBUG(D_HA, "%s: connection from %s@%s %st"LPU64" exp %p cur %ld last %ld\n",
1008 target->obd_name, cluuid.uuid, libcfs_nid2str(req->rq_peer.nid),
1009 target->obd_recovering ? "recovering/" : "", data->ocd_transno,
1010 export, (long)cfs_time_current_sec(),
1011 export ? (long)export->exp_last_request_time : 0);
1013 /* If this is the first time a client connects, reset the recovery
1014 * timer. Discard lightweight connections which might be local. */
1015 if (!lw_client && rc == 0 && target->obd_recovering)
1016 check_and_start_recovery_timer(target, req, export == NULL);
1018 /* We want to handle EALREADY but *not* -EALREADY from
1019 * target_handle_reconnect(), return reconnection state in a flag. */
1020 if (rc == EALREADY) {
1021 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECONNECT);
1027 /* Tell the client if we support replayable requests. */
1028 if (target->obd_replayable)
1029 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
1030 client_nid = &req->rq_peer.nid;
1032 if (export == NULL) {
1033 /* allow lightweight connections during recovery */
1034 if (target->obd_recovering && !lw_client) {
1036 int c; /* connected */
1037 int i; /* in progress */
1039 int s; /* stale/evicted */
1041 c = atomic_read(&target->obd_connected_clients);
1042 i = atomic_read(&target->obd_lock_replay_clients);
1043 k = target->obd_max_recoverable_clients;
1044 s = target->obd_stale_clients;
1045 t = cfs_timer_deadline(&target->obd_recovery_timer);
1046 t = cfs_time_sub(t, cfs_time_current());
1047 t = cfs_duration_sec(t);
1048 LCONSOLE_WARN("%s: Denying connection for new client "
1049 "%s (at %s), waiting for all %d known "
1050 "clients (%d recovered, %d in progress, "
1051 "and %d evicted) to recover in %d:%.02d\n",
1052 target->obd_name, cluuid.uuid,
1053 libcfs_nid2str(req->rq_peer.nid), k,
1054 c - i, i, s, (int)t / 60,
1059 rc = obd_connect(req->rq_svc_thread->t_env,
1060 &export, target, &cluuid, data,
1062 if (mds_conn && OBD_FAIL_CHECK(OBD_FAIL_TGT_RCVG_FLAG))
1063 lustre_msg_add_op_flags(req->rq_repmsg,
1064 MSG_CONNECT_RECOVERING);
1066 conn.cookie = export->exp_handle.h_cookie;
1069 rc = obd_reconnect(req->rq_svc_thread->t_env,
1070 export, target, &cluuid, data, client_nid);
1075 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 6, 50, 0)
1076 /* 2.2.0 clients always swab nidtbl entries due to a bug, so server
1077 * will do the swabbing for if the client is using the same endianness.
1079 * This fixup is version-limited, because we don't want to carry the
1080 * OBD_CONNECT_MNE_SWAB flag around forever, just so long as we need
1081 * interop with unpatched 2.2 clients. For newer clients, servers
1082 * will never do MNE swabbing, let the client handle that. LU-1644 */
1083 export->exp_need_mne_swab = !ptlrpc_req_need_swab(req) &&
1084 !(data->ocd_connect_flags & OBD_CONNECT_MNE_SWAB);
1086 #warning "LU-1644: Remove old OBD_CONNECT_MNE_SWAB fixup and exp_need_mne_swab"
1089 LASSERT(target->u.obt.obt_magic == OBT_MAGIC);
1090 data->ocd_instance = target->u.obt.obt_instance;
1092 /* Return only the parts of obd_connect_data that we understand, so the
1093 * client knows that we don't understand the rest. */
1095 tmpsize = req_capsule_get_size(&req->rq_pill, &RMF_CONNECT_DATA,
1097 tmpdata = req_capsule_server_get(&req->rq_pill,
1099 /* Don't use struct assignment here, because the client reply
1100 * buffer may be smaller/larger than the local struct
1101 * obd_connect_data. */
1102 memcpy(tmpdata, data, min(tmpsize, size));
1105 /* If all else goes well, this is our RPC return code. */
1108 lustre_msg_set_handle(req->rq_repmsg, &conn);
1110 /* If the client and the server are the same node, we will already
1111 * have an export that really points to the client's DLM export,
1112 * because we have a shared handles table.
1114 * XXX this will go away when shaver stops sending the "connect" handle
1115 * in the real "remote handle" field of the request --phik 24 Apr 2003
1117 ptlrpc_request_change_export(req, export);
1119 spin_lock(&export->exp_lock);
1120 if (export->exp_conn_cnt >= lustre_msg_get_conn_cnt(req->rq_reqmsg)) {
1121 spin_unlock(&export->exp_lock);
1122 CDEBUG(D_RPCTRACE, "%s: %s already connected at greater "
1123 "or equal conn_cnt: %d >= %d\n",
1124 cluuid.uuid, libcfs_nid2str(req->rq_peer.nid),
1125 export->exp_conn_cnt,
1126 lustre_msg_get_conn_cnt(req->rq_reqmsg));
1128 GOTO(out, rc = -EALREADY);
1130 LASSERT(lustre_msg_get_conn_cnt(req->rq_reqmsg) > 0);
1131 export->exp_conn_cnt = lustre_msg_get_conn_cnt(req->rq_reqmsg);
1133 /* Don't evict liblustre clients for not pinging. */
1134 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT) {
1135 export->exp_libclient = 1;
1136 spin_unlock(&export->exp_lock);
1138 spin_lock(&target->obd_dev_lock);
1139 cfs_list_del_init(&export->exp_obd_chain_timed);
1140 spin_unlock(&target->obd_dev_lock);
1142 spin_unlock(&export->exp_lock);
1145 if (export->exp_connection != NULL) {
1146 /* Check to see if connection came from another NID. */
1147 if ((export->exp_connection->c_peer.nid != req->rq_peer.nid) &&
1148 !cfs_hlist_unhashed(&export->exp_nid_hash))
1149 cfs_hash_del(export->exp_obd->obd_nid_hash,
1150 &export->exp_connection->c_peer.nid,
1151 &export->exp_nid_hash);
1153 ptlrpc_connection_put(export->exp_connection);
1156 export->exp_connection = ptlrpc_connection_get(req->rq_peer,
1159 if (cfs_hlist_unhashed(&export->exp_nid_hash)) {
1160 cfs_hash_add(export->exp_obd->obd_nid_hash,
1161 &export->exp_connection->c_peer.nid,
1162 &export->exp_nid_hash);
1165 if (target->obd_recovering && !export->exp_in_recovery && !lw_client) {
1167 __u64 transno = data->ocd_transno;
1169 spin_lock(&export->exp_lock);
1170 /* possible race with class_disconnect_stale_exports,
1171 * export may be already in the eviction process */
1172 if (export->exp_failed) {
1173 spin_unlock(&export->exp_lock);
1174 GOTO(out, rc = -ENODEV);
1176 export->exp_in_recovery = 1;
1177 export->exp_req_replay_needed = 1;
1178 export->exp_lock_replay_needed = 1;
1179 spin_unlock(&export->exp_lock);
1181 has_transno = !!(lustre_msg_get_op_flags(req->rq_reqmsg) &
1182 MSG_CONNECT_TRANSNO);
1183 if (has_transno && transno == 0)
1184 CWARN("Connect with zero transno!\n");
1186 if (has_transno && transno > 0 &&
1187 transno < target->obd_next_recovery_transno &&
1188 transno > target->obd_last_committed) {
1189 /* Another way is to use cmpxchg() to be lock-free. */
1190 spin_lock(&target->obd_recovery_task_lock);
1191 if (transno < target->obd_next_recovery_transno)
1192 target->obd_next_recovery_transno = transno;
1193 spin_unlock(&target->obd_recovery_task_lock);
1196 atomic_inc(&target->obd_req_replay_clients);
1197 atomic_inc(&target->obd_lock_replay_clients);
1198 if (atomic_inc_return(&target->obd_connected_clients) ==
1199 target->obd_max_recoverable_clients)
1200 wake_up(&target->obd_next_transno_waitq);
1203 /* Tell the client we're in recovery, when client is involved in it. */
1204 if (target->obd_recovering && !lw_client)
1205 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
1207 tmp = req_capsule_client_get(&req->rq_pill, &RMF_CONN);
1210 /* Return -ENOTCONN in case of errors to let client reconnect. */
1211 revimp = class_new_import(target);
1212 if (revimp == NULL) {
1213 CERROR("fail to alloc new reverse import.\n");
1214 GOTO(out, rc = -ENOTCONN);
1217 spin_lock(&export->exp_lock);
1218 if (export->exp_imp_reverse != NULL)
1219 /* destroyed import can be still referenced in ctxt */
1220 tmp_imp = export->exp_imp_reverse;
1221 export->exp_imp_reverse = revimp;
1222 spin_unlock(&export->exp_lock);
1224 revimp->imp_connection = ptlrpc_connection_addref(export->exp_connection);
1225 revimp->imp_client = &export->exp_obd->obd_ldlm_client;
1226 revimp->imp_remote_handle = conn;
1227 revimp->imp_dlm_fake = 1;
1228 revimp->imp_state = LUSTRE_IMP_FULL;
1230 /* Unknown versions will be caught in
1231 * ptlrpc_handle_server_req_in->lustre_unpack_msg(). */
1232 revimp->imp_msg_magic = req->rq_reqmsg->lm_magic;
1234 if ((data->ocd_connect_flags & OBD_CONNECT_AT) &&
1235 (revimp->imp_msg_magic != LUSTRE_MSG_MAGIC_V1))
1236 revimp->imp_msghdr_flags |= MSGHDR_AT_SUPPORT;
1238 revimp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
1240 if ((data->ocd_connect_flags & OBD_CONNECT_FULL20) &&
1241 (revimp->imp_msg_magic != LUSTRE_MSG_MAGIC_V1))
1242 revimp->imp_msghdr_flags |= MSGHDR_CKSUM_INCOMPAT18;
1244 revimp->imp_msghdr_flags &= ~MSGHDR_CKSUM_INCOMPAT18;
1246 rc = sptlrpc_import_sec_adapt(revimp, req->rq_svc_ctx, &req->rq_flvr);
1248 CERROR("Failed to get sec for reverse import: %d\n", rc);
1249 spin_lock(&export->exp_lock);
1250 export->exp_imp_reverse = NULL;
1251 spin_unlock(&export->exp_lock);
1252 class_destroy_import(revimp);
1255 class_import_put(revimp);
1258 if (tmp_imp != NULL)
1259 client_destroy_import(tmp_imp);
1261 spin_lock(&export->exp_lock);
1262 export->exp_connecting = 0;
1263 spin_unlock(&export->exp_lock);
1265 class_export_put(export);
1268 spin_lock(&target->obd_dev_lock);
1269 target->obd_conn_inprogress--;
1270 spin_unlock(&target->obd_dev_lock);
1272 class_decref(targref, __func__, current);
1275 req->rq_status = rc;
1278 EXPORT_SYMBOL(target_handle_connect);
1280 int target_handle_disconnect(struct ptlrpc_request *req)
1285 rc = req_capsule_server_pack(&req->rq_pill);
1289 /* Keep the rq_export around so we can send the reply. */
1290 req->rq_status = obd_disconnect(class_export_get(req->rq_export));
1294 EXPORT_SYMBOL(target_handle_disconnect);
1296 void target_destroy_export(struct obd_export *exp)
1298 struct obd_import *imp = NULL;
1299 /* exports created from last_rcvd data, and "fake"
1300 exports created by lctl don't have an import */
1301 spin_lock(&exp->exp_lock);
1302 if (exp->exp_imp_reverse != NULL) {
1303 imp = exp->exp_imp_reverse;
1304 exp->exp_imp_reverse = NULL;
1306 spin_unlock(&exp->exp_lock);
1308 client_destroy_import(imp);
1310 LASSERT_ATOMIC_ZERO(&exp->exp_locks_count);
1311 LASSERT_ATOMIC_ZERO(&exp->exp_rpc_count);
1312 LASSERT_ATOMIC_ZERO(&exp->exp_cb_count);
1313 LASSERT_ATOMIC_ZERO(&exp->exp_replay_count);
1315 EXPORT_SYMBOL(target_destroy_export);
1318 * Recovery functions
1320 static void target_request_copy_get(struct ptlrpc_request *req)
1322 class_export_rpc_inc(req->rq_export);
1323 LASSERT(cfs_list_empty(&req->rq_list));
1324 CFS_INIT_LIST_HEAD(&req->rq_replay_list);
1326 /* Increase refcount to keep request in queue. */
1327 atomic_inc(&req->rq_refcount);
1328 /* Let export know it has replays to be handled. */
1329 atomic_inc(&req->rq_export->exp_replay_count);
1332 static void target_request_copy_put(struct ptlrpc_request *req)
1334 LASSERT(cfs_list_empty(&req->rq_replay_list));
1335 LASSERT_ATOMIC_POS(&req->rq_export->exp_replay_count);
1337 atomic_dec(&req->rq_export->exp_replay_count);
1338 class_export_rpc_dec(req->rq_export);
1339 ptlrpc_server_drop_request(req);
1342 static int target_exp_enqueue_req_replay(struct ptlrpc_request *req)
1344 __u64 transno = lustre_msg_get_transno(req->rq_reqmsg);
1345 struct obd_export *exp = req->rq_export;
1346 struct ptlrpc_request *reqiter;
1351 spin_lock(&exp->exp_lock);
1352 cfs_list_for_each_entry(reqiter, &exp->exp_req_replay_queue,
1354 if (lustre_msg_get_transno(reqiter->rq_reqmsg) == transno) {
1361 /* We expect it with RESENT and REPLAY flags. */
1362 if ((lustre_msg_get_flags(req->rq_reqmsg) &
1363 (MSG_RESENT | MSG_REPLAY)) != (MSG_RESENT | MSG_REPLAY))
1364 CERROR("invalid flags %x of resent replay\n",
1365 lustre_msg_get_flags(req->rq_reqmsg));
1367 cfs_list_add_tail(&req->rq_replay_list,
1368 &exp->exp_req_replay_queue);
1371 spin_unlock(&exp->exp_lock);
1375 static void target_exp_dequeue_req_replay(struct ptlrpc_request *req)
1377 LASSERT(!cfs_list_empty(&req->rq_replay_list));
1378 LASSERT(req->rq_export);
1380 spin_lock(&req->rq_export->exp_lock);
1381 cfs_list_del_init(&req->rq_replay_list);
1382 spin_unlock(&req->rq_export->exp_lock);
1386 static void target_finish_recovery(struct obd_device *obd)
1390 /* Only log a recovery message when recovery has occurred. */
1391 if (obd->obd_recovery_start) {
1392 time_t elapsed_time = max_t(time_t, 1, cfs_time_current_sec() -
1393 obd->obd_recovery_start);
1394 LCONSOLE_INFO("%s: Recovery over after %d:%.02d, of %d clients "
1395 "%d recovered and %d %s evicted.\n", obd->obd_name,
1396 (int)elapsed_time / 60, (int)elapsed_time % 60,
1397 obd->obd_max_recoverable_clients,
1398 atomic_read(&obd->obd_connected_clients),
1399 obd->obd_stale_clients,
1400 obd->obd_stale_clients == 1 ? "was" : "were");
1403 ldlm_reprocess_all_ns(obd->obd_namespace);
1404 spin_lock(&obd->obd_recovery_task_lock);
1405 if (!cfs_list_empty(&obd->obd_req_replay_queue) ||
1406 !cfs_list_empty(&obd->obd_lock_replay_queue) ||
1407 !cfs_list_empty(&obd->obd_final_req_queue)) {
1408 CERROR("%s: Recovery queues ( %s%s%s) are not empty\n",
1410 cfs_list_empty(&obd->obd_req_replay_queue) ? "" : "req ",
1411 cfs_list_empty(&obd->obd_lock_replay_queue) ? \
1413 cfs_list_empty(&obd->obd_final_req_queue) ? \
1415 spin_unlock(&obd->obd_recovery_task_lock);
1418 spin_unlock(&obd->obd_recovery_task_lock);
1420 obd->obd_recovery_end = cfs_time_current_sec();
1422 /* When recovery finished, cleanup orphans on MDS and OST. */
1423 if (OBT(obd) && OBP(obd, postrecov)) {
1424 int rc = OBP(obd, postrecov)(obd);
1426 LCONSOLE_WARN("%s: Post recovery failed, rc %d\n",
1432 static void abort_req_replay_queue(struct obd_device *obd)
1434 struct ptlrpc_request *req, *n;
1435 cfs_list_t abort_list;
1437 CFS_INIT_LIST_HEAD(&abort_list);
1438 spin_lock(&obd->obd_recovery_task_lock);
1439 cfs_list_splice_init(&obd->obd_req_replay_queue, &abort_list);
1440 spin_unlock(&obd->obd_recovery_task_lock);
1441 cfs_list_for_each_entry_safe(req, n, &abort_list, rq_list) {
1442 DEBUG_REQ(D_WARNING, req, "aborted:");
1443 req->rq_status = -ENOTCONN;
1444 if (ptlrpc_error(req)) {
1445 DEBUG_REQ(D_ERROR, req,
1446 "failed abort_req_reply; skipping");
1448 target_exp_dequeue_req_replay(req);
1449 target_request_copy_put(req);
1453 static void abort_lock_replay_queue(struct obd_device *obd)
1455 struct ptlrpc_request *req, *n;
1456 cfs_list_t abort_list;
1458 CFS_INIT_LIST_HEAD(&abort_list);
1459 spin_lock(&obd->obd_recovery_task_lock);
1460 cfs_list_splice_init(&obd->obd_lock_replay_queue, &abort_list);
1461 spin_unlock(&obd->obd_recovery_task_lock);
1462 cfs_list_for_each_entry_safe(req, n, &abort_list, rq_list){
1463 DEBUG_REQ(D_ERROR, req, "aborted:");
1464 req->rq_status = -ENOTCONN;
1465 if (ptlrpc_error(req)) {
1466 DEBUG_REQ(D_ERROR, req,
1467 "failed abort_lock_reply; skipping");
1469 target_request_copy_put(req);
1473 /* Called from a cleanup function if the device is being cleaned up
1474 forcefully. The exports should all have been disconnected already,
1475 the only thing left to do is
1476 - clear the recovery flags
1478 - free queued requests and replies, but don't send replies
1479 Because the obd_stopping flag is set, no new requests should be received.
1482 void target_cleanup_recovery(struct obd_device *obd)
1484 struct ptlrpc_request *req, *n;
1485 cfs_list_t clean_list;
1488 CFS_INIT_LIST_HEAD(&clean_list);
1489 spin_lock(&obd->obd_dev_lock);
1490 if (!obd->obd_recovering) {
1491 spin_unlock(&obd->obd_dev_lock);
1495 obd->obd_recovering = obd->obd_abort_recovery = 0;
1496 spin_unlock(&obd->obd_dev_lock);
1498 spin_lock(&obd->obd_recovery_task_lock);
1499 target_cancel_recovery_timer(obd);
1500 cfs_list_splice_init(&obd->obd_req_replay_queue, &clean_list);
1501 spin_unlock(&obd->obd_recovery_task_lock);
1503 cfs_list_for_each_entry_safe(req, n, &clean_list, rq_list) {
1504 LASSERT(req->rq_reply_state == 0);
1505 target_exp_dequeue_req_replay(req);
1506 target_request_copy_put(req);
1509 spin_lock(&obd->obd_recovery_task_lock);
1510 cfs_list_splice_init(&obd->obd_lock_replay_queue, &clean_list);
1511 cfs_list_splice_init(&obd->obd_final_req_queue, &clean_list);
1512 spin_unlock(&obd->obd_recovery_task_lock);
1514 cfs_list_for_each_entry_safe(req, n, &clean_list, rq_list){
1515 LASSERT(req->rq_reply_state == 0);
1516 target_request_copy_put(req);
1521 EXPORT_SYMBOL(target_cleanup_recovery);
1523 /* obd_recovery_task_lock should be held */
1524 void target_cancel_recovery_timer(struct obd_device *obd)
1526 CDEBUG(D_HA, "%s: cancel recovery timer\n", obd->obd_name);
1527 cfs_timer_disarm(&obd->obd_recovery_timer);
1529 EXPORT_SYMBOL(target_cancel_recovery_timer);
1531 static void target_start_recovery_timer(struct obd_device *obd)
1533 if (obd->obd_recovery_start != 0)
1536 spin_lock(&obd->obd_dev_lock);
1537 if (!obd->obd_recovering || obd->obd_abort_recovery) {
1538 spin_unlock(&obd->obd_dev_lock);
1542 LASSERT(obd->obd_recovery_timeout != 0);
1544 if (obd->obd_recovery_start != 0) {
1545 spin_unlock(&obd->obd_dev_lock);
1549 cfs_timer_arm(&obd->obd_recovery_timer,
1550 cfs_time_shift(obd->obd_recovery_timeout));
1551 obd->obd_recovery_start = cfs_time_current_sec();
1552 spin_unlock(&obd->obd_dev_lock);
1554 LCONSOLE_WARN("%s: Will be in recovery for at least %d:%.02d, "
1555 "or until %d client%s reconnect%s\n",
1557 obd->obd_recovery_timeout / 60,
1558 obd->obd_recovery_timeout % 60,
1559 obd->obd_max_recoverable_clients,
1560 (obd->obd_max_recoverable_clients == 1) ? "" : "s",
1561 (obd->obd_max_recoverable_clients == 1) ? "s": "");
1565 * extend recovery window.
1567 * if @extend is true, extend recovery window to have @drt remaining at least;
1568 * otherwise, make sure the recovery timeout value is not less than @drt.
1570 static void extend_recovery_timer(struct obd_device *obd, int drt, bool extend)
1574 cfs_duration_t left;
1577 spin_lock(&obd->obd_dev_lock);
1578 if (!obd->obd_recovering || obd->obd_abort_recovery) {
1579 spin_unlock(&obd->obd_dev_lock);
1582 LASSERT(obd->obd_recovery_start != 0);
1584 now = cfs_time_current_sec();
1585 to = obd->obd_recovery_timeout;
1586 end = obd->obd_recovery_start + to;
1587 left = cfs_time_sub(end, now);
1589 if (extend && (drt > left)) {
1591 } else if (!extend && (drt > to)) {
1593 /* reduce drt by already passed time */
1594 drt -= obd->obd_recovery_timeout - left;
1597 if (to > obd->obd_recovery_time_hard)
1598 to = obd->obd_recovery_time_hard;
1599 if (obd->obd_recovery_timeout < to ||
1600 obd->obd_recovery_timeout == obd->obd_recovery_time_hard) {
1601 obd->obd_recovery_timeout = to;
1602 cfs_timer_arm(&obd->obd_recovery_timer,
1603 cfs_time_shift(drt));
1605 spin_unlock(&obd->obd_dev_lock);
1607 CDEBUG(D_HA, "%s: recovery timer will expire in %u seconds\n",
1608 obd->obd_name, (unsigned)drt);
1611 /* Reset the timer with each new client connection */
1613 * This timer is actually reconnect_timer, which is for making sure
1614 * the total recovery window is at least as big as my reconnect
1615 * attempt timing. So the initial recovery time_out will be set to
1616 * OBD_RECOVERY_FACTOR * obd_timeout. If the timeout coming
1617 * from client is bigger than this, then the recovery time_out will
1618 * be extended to make sure the client could be reconnected, in the
1619 * process, the timeout from the new client should be ignored.
1623 check_and_start_recovery_timer(struct obd_device *obd,
1624 struct ptlrpc_request *req,
1627 int service_time = lustre_msg_get_service_time(req->rq_reqmsg);
1628 struct obd_device_target *obt = &obd->u.obt;
1630 if (!new_client && service_time)
1631 /* Teach server about old server's estimates, as first guess
1632 * at how long new requests will take. */
1633 at_measured(&req->rq_rqbd->rqbd_svcpt->scp_at_estimate,
1636 target_start_recovery_timer(obd);
1638 /* Convert the service time to RPC timeout,
1639 * and reuse service_time to limit stack usage. */
1640 service_time = at_est2timeout(service_time);
1642 /* We expect other clients to timeout within service_time, then try
1643 * to reconnect, then try the failover server. The max delay between
1644 * connect attempts is SWITCH_MAX + SWITCH_INC + INITIAL. */
1645 service_time += 2 * INITIAL_CONNECT_TIMEOUT;
1647 LASSERT(obt->obt_magic == OBT_MAGIC);
1648 service_time += 2 * (CONNECTION_SWITCH_MAX + CONNECTION_SWITCH_INC);
1649 if (service_time > obd->obd_recovery_timeout && !new_client)
1650 extend_recovery_timer(obd, service_time, false);
1653 /** Health checking routines */
1654 static inline int exp_connect_healthy(struct obd_export *exp)
1656 return (exp->exp_in_recovery);
1659 /** if export done req_replay or has replay in queue */
1660 static inline int exp_req_replay_healthy(struct obd_export *exp)
1662 return (!exp->exp_req_replay_needed ||
1663 atomic_read(&exp->exp_replay_count) > 0);
1665 /** if export done lock_replay or has replay in queue */
1666 static inline int exp_lock_replay_healthy(struct obd_export *exp)
1668 return (!exp->exp_lock_replay_needed ||
1669 atomic_read(&exp->exp_replay_count) > 0);
1672 static inline int exp_vbr_healthy(struct obd_export *exp)
1674 return (!exp->exp_vbr_failed);
1677 static inline int exp_finished(struct obd_export *exp)
1679 return (exp->exp_in_recovery && !exp->exp_lock_replay_needed);
1682 /** Checking routines for recovery */
1683 static int check_for_clients(struct obd_device *obd)
1685 unsigned int clnts = atomic_read(&obd->obd_connected_clients);
1687 if (obd->obd_abort_recovery || obd->obd_recovery_expired)
1689 LASSERT(clnts <= obd->obd_max_recoverable_clients);
1690 return (clnts + obd->obd_stale_clients ==
1691 obd->obd_max_recoverable_clients);
1694 static int check_for_next_transno(struct obd_device *obd)
1696 struct ptlrpc_request *req = NULL;
1697 int wake_up = 0, connected, completed, queue_len;
1698 __u64 next_transno, req_transno;
1701 spin_lock(&obd->obd_recovery_task_lock);
1702 if (!cfs_list_empty(&obd->obd_req_replay_queue)) {
1703 req = cfs_list_entry(obd->obd_req_replay_queue.next,
1704 struct ptlrpc_request, rq_list);
1705 req_transno = lustre_msg_get_transno(req->rq_reqmsg);
1710 connected = atomic_read(&obd->obd_connected_clients);
1711 completed = connected - atomic_read(&obd->obd_req_replay_clients);
1712 queue_len = obd->obd_requests_queued_for_recovery;
1713 next_transno = obd->obd_next_recovery_transno;
1715 CDEBUG(D_HA, "max: %d, connected: %d, completed: %d, queue_len: %d, "
1716 "req_transno: "LPU64", next_transno: "LPU64"\n",
1717 obd->obd_max_recoverable_clients, connected, completed,
1718 queue_len, req_transno, next_transno);
1720 if (obd->obd_abort_recovery) {
1721 CDEBUG(D_HA, "waking for aborted recovery\n");
1723 } else if (obd->obd_recovery_expired) {
1724 CDEBUG(D_HA, "waking for expired recovery\n");
1726 } else if (req_transno == next_transno) {
1727 CDEBUG(D_HA, "waking for next ("LPD64")\n", next_transno);
1729 } else if (queue_len > 0 &&
1730 queue_len == atomic_read(&obd->obd_req_replay_clients)) {
1732 /** handle gaps occured due to lost reply or VBR */
1733 LASSERTF(req_transno >= next_transno,
1734 "req_transno: "LPU64", next_transno: "LPU64"\n",
1735 req_transno, next_transno);
1736 if (req_transno > obd->obd_last_committed &&
1737 !obd->obd_version_recov)
1740 "%s: waking for gap in transno, VBR is %s (skip: "
1741 LPD64", ql: %d, comp: %d, conn: %d, next: "LPD64
1742 ", last_committed: "LPD64")\n",
1743 obd->obd_name, obd->obd_version_recov ? "ON" : "OFF",
1744 next_transno, queue_len, completed, connected,
1745 req_transno, obd->obd_last_committed);
1746 obd->obd_next_recovery_transno = req_transno;
1748 } else if (atomic_read(&obd->obd_req_replay_clients) == 0) {
1749 CDEBUG(D_HA, "waking for completed recovery\n");
1751 } else if (OBD_FAIL_CHECK(OBD_FAIL_MDS_RECOVERY_ACCEPTS_GAPS)) {
1752 CDEBUG(D_HA, "accepting transno gaps is explicitly allowed"
1753 " by fail_lock, waking up ("LPD64")\n", next_transno);
1754 obd->obd_next_recovery_transno = req_transno;
1757 spin_unlock(&obd->obd_recovery_task_lock);
1761 static int check_for_next_lock(struct obd_device *obd)
1765 spin_lock(&obd->obd_recovery_task_lock);
1766 if (!cfs_list_empty(&obd->obd_lock_replay_queue)) {
1767 CDEBUG(D_HA, "waking for next lock\n");
1769 } else if (atomic_read(&obd->obd_lock_replay_clients) == 0) {
1770 CDEBUG(D_HA, "waking for completed lock replay\n");
1772 } else if (obd->obd_abort_recovery) {
1773 CDEBUG(D_HA, "waking for aborted recovery\n");
1775 } else if (obd->obd_recovery_expired) {
1776 CDEBUG(D_HA, "waking for expired recovery\n");
1779 spin_unlock(&obd->obd_recovery_task_lock);
1785 * wait for recovery events,
1786 * check its status with help of check_routine
1787 * evict dead clients via health_check
1789 static int target_recovery_overseer(struct obd_device *obd,
1790 int (*check_routine)(struct obd_device *),
1791 int (*health_check)(struct obd_export *))
1794 wait_event(obd->obd_next_transno_waitq, check_routine(obd));
1795 if (obd->obd_abort_recovery) {
1796 CWARN("recovery is aborted, evict exports in recovery\n");
1797 /** evict exports which didn't finish recovery yet */
1798 class_disconnect_stale_exports(obd, exp_finished);
1800 } else if (obd->obd_recovery_expired) {
1801 obd->obd_recovery_expired = 0;
1802 /** If some clients died being recovered, evict them */
1803 LCONSOLE_WARN("%s: recovery is timed out, "
1804 "evict stale exports\n", obd->obd_name);
1805 /** evict cexports with no replay in queue, they are stalled */
1806 class_disconnect_stale_exports(obd, health_check);
1807 /** continue with VBR */
1808 spin_lock(&obd->obd_dev_lock);
1809 obd->obd_version_recov = 1;
1810 spin_unlock(&obd->obd_dev_lock);
1812 * reset timer, recovery will proceed with versions now,
1813 * timeout is set just to handle reconnection delays
1815 extend_recovery_timer(obd, RECONNECT_DELAY_MAX, true);
1816 /** Wait for recovery events again, after evicting bad clients */
1822 static struct ptlrpc_request *target_next_replay_req(struct obd_device *obd)
1824 struct ptlrpc_request *req = NULL;
1827 CDEBUG(D_HA, "Waiting for transno "LPD64"\n",
1828 obd->obd_next_recovery_transno);
1830 if (target_recovery_overseer(obd, check_for_next_transno,
1831 exp_req_replay_healthy)) {
1832 abort_req_replay_queue(obd);
1833 abort_lock_replay_queue(obd);
1836 spin_lock(&obd->obd_recovery_task_lock);
1837 if (!cfs_list_empty(&obd->obd_req_replay_queue)) {
1838 req = cfs_list_entry(obd->obd_req_replay_queue.next,
1839 struct ptlrpc_request, rq_list);
1840 cfs_list_del_init(&req->rq_list);
1841 obd->obd_requests_queued_for_recovery--;
1842 spin_unlock(&obd->obd_recovery_task_lock);
1844 spin_unlock(&obd->obd_recovery_task_lock);
1845 LASSERT(cfs_list_empty(&obd->obd_req_replay_queue));
1846 LASSERT(atomic_read(&obd->obd_req_replay_clients) == 0);
1847 /** evict exports failed VBR */
1848 class_disconnect_stale_exports(obd, exp_vbr_healthy);
1853 static struct ptlrpc_request *target_next_replay_lock(struct obd_device *obd)
1855 struct ptlrpc_request *req = NULL;
1857 CDEBUG(D_HA, "Waiting for lock\n");
1858 if (target_recovery_overseer(obd, check_for_next_lock,
1859 exp_lock_replay_healthy))
1860 abort_lock_replay_queue(obd);
1862 spin_lock(&obd->obd_recovery_task_lock);
1863 if (!cfs_list_empty(&obd->obd_lock_replay_queue)) {
1864 req = cfs_list_entry(obd->obd_lock_replay_queue.next,
1865 struct ptlrpc_request, rq_list);
1866 cfs_list_del_init(&req->rq_list);
1867 spin_unlock(&obd->obd_recovery_task_lock);
1869 spin_unlock(&obd->obd_recovery_task_lock);
1870 LASSERT(cfs_list_empty(&obd->obd_lock_replay_queue));
1871 LASSERT(atomic_read(&obd->obd_lock_replay_clients) == 0);
1872 /** evict exports failed VBR */
1873 class_disconnect_stale_exports(obd, exp_vbr_healthy);
1878 static struct ptlrpc_request *target_next_final_ping(struct obd_device *obd)
1880 struct ptlrpc_request *req = NULL;
1882 spin_lock(&obd->obd_recovery_task_lock);
1883 if (!cfs_list_empty(&obd->obd_final_req_queue)) {
1884 req = cfs_list_entry(obd->obd_final_req_queue.next,
1885 struct ptlrpc_request, rq_list);
1886 cfs_list_del_init(&req->rq_list);
1887 spin_unlock(&obd->obd_recovery_task_lock);
1888 if (req->rq_export->exp_in_recovery) {
1889 spin_lock(&req->rq_export->exp_lock);
1890 req->rq_export->exp_in_recovery = 0;
1891 spin_unlock(&req->rq_export->exp_lock);
1894 spin_unlock(&obd->obd_recovery_task_lock);
1899 static void handle_recovery_req(struct ptlrpc_thread *thread,
1900 struct ptlrpc_request *req,
1901 svc_handler_t handler)
1906 * export can be evicted during recovery, no need to handle replays for
1907 * it after that, discard such request silently
1909 if (req->rq_export->exp_disconnected)
1912 req->rq_session.lc_thread = thread;
1913 req->rq_svc_thread = thread;
1914 req->rq_svc_thread->t_env->le_ses = &req->rq_session;
1916 /* thread context */
1917 lu_context_enter(&thread->t_env->le_ctx);
1919 lu_context_exit(&thread->t_env->le_ctx);
1921 /* don't reset timer for final stage */
1922 if (!exp_finished(req->rq_export)) {
1923 int to = obd_timeout;
1926 * Add request timeout to the recovery time so next request from
1927 * this client may come in recovery time
1930 struct ptlrpc_service_part *svcpt;
1932 svcpt = req->rq_rqbd->rqbd_svcpt;
1933 /* If the server sent early reply for this request,
1934 * the client will recalculate the timeout according to
1935 * current server estimate service time, so we will
1936 * use the maxium timeout here for waiting the client
1937 * sending the next req */
1938 to = max((int)at_est2timeout(
1939 at_get(&svcpt->scp_at_estimate)),
1940 (int)lustre_msg_get_timeout(req->rq_reqmsg));
1941 /* Add 2 net_latency, one for balance rq_deadline
1942 * (see ptl_send_rpc), one for resend the req to server,
1943 * Note: client will pack net_latency in replay req
1944 * (see ptlrpc_replay_req) */
1945 to += 2 * lustre_msg_get_service_time(req->rq_reqmsg);
1947 extend_recovery_timer(class_exp2obd(req->rq_export), to, true);
1952 static int target_recovery_thread(void *arg)
1954 struct lu_target *lut = arg;
1955 struct obd_device *obd = lut->lut_obd;
1956 struct ptlrpc_request *req;
1957 struct target_recovery_data *trd = &obd->obd_recovery_data;
1958 unsigned long delta;
1960 struct ptlrpc_thread *thread = NULL;
1964 unshare_fs_struct();
1965 OBD_ALLOC_PTR(thread);
1971 OBD_FREE_PTR(thread);
1975 rc = lu_context_init(&env->le_ctx, LCT_MD_THREAD | LCT_DT_THREAD);
1977 OBD_FREE_PTR(thread);
1982 thread->t_env = env;
1983 thread->t_id = -1; /* force filter_iobuf_get/put to use local buffers */
1984 env->le_ctx.lc_thread = thread;
1985 tgt_io_thread_init(thread); /* init thread_big_cache for IO requests */
1986 thread->t_watchdog = NULL;
1988 CDEBUG(D_HA, "%s: started recovery thread pid %d\n", obd->obd_name,
1990 trd->trd_processing_task = current_pid();
1992 spin_lock(&obd->obd_dev_lock);
1993 obd->obd_recovering = 1;
1994 spin_unlock(&obd->obd_dev_lock);
1995 complete(&trd->trd_starting);
1997 /* first of all, we have to know the first transno to replay */
1998 if (target_recovery_overseer(obd, check_for_clients,
1999 exp_connect_healthy)) {
2000 abort_req_replay_queue(obd);
2001 abort_lock_replay_queue(obd);
2004 /* next stage: replay requests */
2006 CDEBUG(D_INFO, "1: request replay stage - %d clients from t"LPU64"\n",
2007 atomic_read(&obd->obd_req_replay_clients),
2008 obd->obd_next_recovery_transno);
2009 while ((req = target_next_replay_req(obd))) {
2010 LASSERT(trd->trd_processing_task == current_pid());
2011 DEBUG_REQ(D_HA, req, "processing t"LPD64" from %s",
2012 lustre_msg_get_transno(req->rq_reqmsg),
2013 libcfs_nid2str(req->rq_peer.nid));
2014 handle_recovery_req(thread, req,
2015 trd->trd_recovery_handler);
2017 * bz18031: increase next_recovery_transno before
2018 * target_request_copy_put() will drop exp_rpc reference
2020 spin_lock(&obd->obd_recovery_task_lock);
2021 obd->obd_next_recovery_transno++;
2022 spin_unlock(&obd->obd_recovery_task_lock);
2023 target_exp_dequeue_req_replay(req);
2024 target_request_copy_put(req);
2025 obd->obd_replayed_requests++;
2029 * The second stage: replay locks
2031 CDEBUG(D_INFO, "2: lock replay stage - %d clients\n",
2032 atomic_read(&obd->obd_lock_replay_clients));
2033 while ((req = target_next_replay_lock(obd))) {
2034 LASSERT(trd->trd_processing_task == current_pid());
2035 DEBUG_REQ(D_HA, req, "processing lock from %s: ",
2036 libcfs_nid2str(req->rq_peer.nid));
2037 handle_recovery_req(thread, req,
2038 trd->trd_recovery_handler);
2039 target_request_copy_put(req);
2040 obd->obd_replayed_locks++;
2044 * The third stage: reply on final pings, at this moment all clients
2045 * must have request in final queue
2047 CDEBUG(D_INFO, "3: final stage - process recovery completion pings\n");
2048 /** Update server last boot epoch */
2049 tgt_boot_epoch_update(lut);
2050 /* We drop recoverying flag to forward all new requests
2051 * to regular mds_handle() since now */
2052 spin_lock(&obd->obd_dev_lock);
2053 obd->obd_recovering = obd->obd_abort_recovery = 0;
2054 spin_unlock(&obd->obd_dev_lock);
2055 spin_lock(&obd->obd_recovery_task_lock);
2056 target_cancel_recovery_timer(obd);
2057 spin_unlock(&obd->obd_recovery_task_lock);
2058 while ((req = target_next_final_ping(obd))) {
2059 LASSERT(trd->trd_processing_task == current_pid());
2060 DEBUG_REQ(D_HA, req, "processing final ping from %s: ",
2061 libcfs_nid2str(req->rq_peer.nid));
2062 handle_recovery_req(thread, req,
2063 trd->trd_recovery_handler);
2064 target_request_copy_put(req);
2067 delta = (jiffies - delta) / HZ;
2068 CDEBUG(D_INFO,"4: recovery completed in %lus - %d/%d reqs/locks\n",
2069 delta, obd->obd_replayed_requests, obd->obd_replayed_locks);
2070 if (delta > OBD_RECOVERY_TIME_SOFT) {
2071 CWARN("too long recovery - read logs\n");
2072 libcfs_debug_dumplog();
2075 target_finish_recovery(obd);
2077 lu_context_fini(&env->le_ctx);
2078 trd->trd_processing_task = 0;
2079 complete(&trd->trd_finishing);
2081 tgt_io_thread_done(thread);
2082 OBD_FREE_PTR(thread);
2087 static int target_start_recovery_thread(struct lu_target *lut,
2088 svc_handler_t handler)
2090 struct obd_device *obd = lut->lut_obd;
2092 struct target_recovery_data *trd = &obd->obd_recovery_data;
2094 memset(trd, 0, sizeof(*trd));
2095 init_completion(&trd->trd_starting);
2096 init_completion(&trd->trd_finishing);
2097 trd->trd_recovery_handler = handler;
2099 if (!IS_ERR(kthread_run(target_recovery_thread,
2100 lut, "tgt_recov"))) {
2101 wait_for_completion(&trd->trd_starting);
2102 LASSERT(obd->obd_recovering != 0);
2110 void target_stop_recovery_thread(struct obd_device *obd)
2112 if (obd->obd_recovery_data.trd_processing_task > 0) {
2113 struct target_recovery_data *trd = &obd->obd_recovery_data;
2114 /** recovery can be done but postrecovery is not yet */
2115 spin_lock(&obd->obd_dev_lock);
2116 if (obd->obd_recovering) {
2117 CERROR("%s: Aborting recovery\n", obd->obd_name);
2118 obd->obd_abort_recovery = 1;
2119 wake_up(&obd->obd_next_transno_waitq);
2121 spin_unlock(&obd->obd_dev_lock);
2122 wait_for_completion(&trd->trd_finishing);
2125 EXPORT_SYMBOL(target_stop_recovery_thread);
2127 void target_recovery_fini(struct obd_device *obd)
2129 class_disconnect_exports(obd);
2130 target_stop_recovery_thread(obd);
2131 target_cleanup_recovery(obd);
2133 EXPORT_SYMBOL(target_recovery_fini);
2135 static void target_recovery_expired(unsigned long castmeharder)
2137 struct obd_device *obd = (struct obd_device *)castmeharder;
2138 CDEBUG(D_HA, "%s: recovery timed out; %d clients are still in recovery"
2139 " after %lds (%d clients connected)\n",
2140 obd->obd_name, atomic_read(&obd->obd_lock_replay_clients),
2141 cfs_time_current_sec()- obd->obd_recovery_start,
2142 atomic_read(&obd->obd_connected_clients));
2144 obd->obd_recovery_expired = 1;
2145 wake_up(&obd->obd_next_transno_waitq);
2148 void target_recovery_init(struct lu_target *lut, svc_handler_t handler)
2150 struct obd_device *obd = lut->lut_obd;
2151 if (obd->obd_max_recoverable_clients == 0) {
2152 /** Update server last boot epoch */
2153 tgt_boot_epoch_update(lut);
2157 CDEBUG(D_HA, "RECOVERY: service %s, %d recoverable clients, "
2158 "last_transno "LPU64"\n", obd->obd_name,
2159 obd->obd_max_recoverable_clients, obd->obd_last_committed);
2160 LASSERT(obd->obd_stopping == 0);
2161 obd->obd_next_recovery_transno = obd->obd_last_committed + 1;
2162 obd->obd_recovery_start = 0;
2163 obd->obd_recovery_end = 0;
2165 cfs_timer_init(&obd->obd_recovery_timer, target_recovery_expired, obd);
2166 target_start_recovery_thread(lut, handler);
2168 EXPORT_SYMBOL(target_recovery_init);
2170 #endif /* __KERNEL__ */
2172 static int target_process_req_flags(struct obd_device *obd,
2173 struct ptlrpc_request *req)
2175 struct obd_export *exp = req->rq_export;
2176 LASSERT(exp != NULL);
2177 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REQ_REPLAY_DONE) {
2178 /* client declares he's ready to replay locks */
2179 spin_lock(&exp->exp_lock);
2180 if (exp->exp_req_replay_needed) {
2181 exp->exp_req_replay_needed = 0;
2182 spin_unlock(&exp->exp_lock);
2184 LASSERT_ATOMIC_POS(&obd->obd_req_replay_clients);
2185 atomic_dec(&obd->obd_req_replay_clients);
2187 spin_unlock(&exp->exp_lock);
2190 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_LOCK_REPLAY_DONE) {
2191 /* client declares he's ready to complete recovery
2192 * so, we put the request on th final queue */
2193 spin_lock(&exp->exp_lock);
2194 if (exp->exp_lock_replay_needed) {
2195 exp->exp_lock_replay_needed = 0;
2196 spin_unlock(&exp->exp_lock);
2198 LASSERT_ATOMIC_POS(&obd->obd_lock_replay_clients);
2199 atomic_dec(&obd->obd_lock_replay_clients);
2201 spin_unlock(&exp->exp_lock);
2207 int target_queue_recovery_request(struct ptlrpc_request *req,
2208 struct obd_device *obd)
2212 __u64 transno = lustre_msg_get_transno(req->rq_reqmsg);
2215 if (obd->obd_recovery_data.trd_processing_task == current_pid()) {
2216 /* Processing the queue right now, don't re-add. */
2220 target_process_req_flags(obd, req);
2222 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_LOCK_REPLAY_DONE) {
2223 /* client declares he's ready to complete recovery
2224 * so, we put the request on th final queue */
2225 target_request_copy_get(req);
2226 DEBUG_REQ(D_HA, req, "queue final req");
2227 wake_up(&obd->obd_next_transno_waitq);
2228 spin_lock(&obd->obd_recovery_task_lock);
2229 if (obd->obd_recovering) {
2230 cfs_list_add_tail(&req->rq_list,
2231 &obd->obd_final_req_queue);
2233 spin_unlock(&obd->obd_recovery_task_lock);
2234 target_request_copy_put(req);
2235 RETURN(obd->obd_stopping ? -ENOTCONN : 1);
2237 spin_unlock(&obd->obd_recovery_task_lock);
2240 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REQ_REPLAY_DONE) {
2241 /* client declares he's ready to replay locks */
2242 target_request_copy_get(req);
2243 DEBUG_REQ(D_HA, req, "queue lock replay req");
2244 wake_up(&obd->obd_next_transno_waitq);
2245 spin_lock(&obd->obd_recovery_task_lock);
2246 LASSERT(obd->obd_recovering);
2247 /* usually due to recovery abort */
2248 if (!req->rq_export->exp_in_recovery) {
2249 spin_unlock(&obd->obd_recovery_task_lock);
2250 target_request_copy_put(req);
2253 LASSERT(req->rq_export->exp_lock_replay_needed);
2254 cfs_list_add_tail(&req->rq_list, &obd->obd_lock_replay_queue);
2255 spin_unlock(&obd->obd_recovery_task_lock);
2259 /* CAVEAT EMPTOR: The incoming request message has been swabbed
2260 * (i.e. buflens etc are in my own byte order), but type-dependent
2261 * buffers (eg mdt_body, ost_body etc) have NOT been swabbed. */
2264 CFS_INIT_LIST_HEAD(&req->rq_list);
2265 DEBUG_REQ(D_HA, req, "not queueing");
2269 /* If we're processing the queue, we want don't want to queue this
2272 * Also, if this request has a transno less than the one we're waiting
2273 * for, we should process it now. It could (and currently always will)
2274 * be an open request for a descriptor that was opened some time ago.
2276 * Also, a resent, replayed request that has already been
2277 * handled will pass through here and be processed immediately.
2279 CDEBUG(D_HA, "Next recovery transno: "LPU64
2280 ", current: "LPU64", replaying\n",
2281 obd->obd_next_recovery_transno, transno);
2282 spin_lock(&obd->obd_recovery_task_lock);
2283 if (transno < obd->obd_next_recovery_transno) {
2284 /* Processing the queue right now, don't re-add. */
2285 LASSERT(cfs_list_empty(&req->rq_list));
2286 spin_unlock(&obd->obd_recovery_task_lock);
2289 spin_unlock(&obd->obd_recovery_task_lock);
2291 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_REPLAY_DROP))
2294 target_request_copy_get(req);
2295 if (!req->rq_export->exp_in_recovery) {
2296 target_request_copy_put(req);
2299 LASSERT(req->rq_export->exp_req_replay_needed);
2301 if (target_exp_enqueue_req_replay(req)) {
2302 DEBUG_REQ(D_ERROR, req, "dropping resent queued req");
2303 target_request_copy_put(req);
2308 spin_lock(&obd->obd_recovery_task_lock);
2309 LASSERT(obd->obd_recovering);
2310 cfs_list_for_each(tmp, &obd->obd_req_replay_queue) {
2311 struct ptlrpc_request *reqiter =
2312 cfs_list_entry(tmp, struct ptlrpc_request, rq_list);
2314 if (lustre_msg_get_transno(reqiter->rq_reqmsg) > transno) {
2315 cfs_list_add_tail(&req->rq_list, &reqiter->rq_list);
2320 if (unlikely(lustre_msg_get_transno(reqiter->rq_reqmsg) ==
2322 DEBUG_REQ(D_ERROR, req, "dropping replay: transno "
2323 "has been claimed by another client");
2324 spin_unlock(&obd->obd_recovery_task_lock);
2325 target_exp_dequeue_req_replay(req);
2326 target_request_copy_put(req);
2332 cfs_list_add_tail(&req->rq_list, &obd->obd_req_replay_queue);
2334 obd->obd_requests_queued_for_recovery++;
2335 spin_unlock(&obd->obd_recovery_task_lock);
2336 wake_up(&obd->obd_next_transno_waitq);
2339 EXPORT_SYMBOL(target_queue_recovery_request);
2341 int target_handle_ping(struct ptlrpc_request *req)
2343 obd_ping(req->rq_svc_thread->t_env, req->rq_export);
2344 return req_capsule_server_pack(&req->rq_pill);
2346 EXPORT_SYMBOL(target_handle_ping);
2348 void target_committed_to_req(struct ptlrpc_request *req)
2350 struct obd_export *exp = req->rq_export;
2352 if (!exp->exp_obd->obd_no_transno && req->rq_repmsg != NULL)
2353 lustre_msg_set_last_committed(req->rq_repmsg,
2354 exp->exp_last_committed);
2356 DEBUG_REQ(D_IOCTL, req, "not sending last_committed update (%d/"
2357 "%d)", exp->exp_obd->obd_no_transno,
2358 req->rq_repmsg == NULL);
2360 CDEBUG(D_INFO, "last_committed "LPU64", transno "LPU64", xid "LPU64"\n",
2361 exp->exp_last_committed, req->rq_transno, req->rq_xid);
2363 EXPORT_SYMBOL(target_committed_to_req);
2365 #endif /* HAVE_SERVER_SUPPORT */
2368 * Packs current SLV and Limit into \a req.
2370 int target_pack_pool_reply(struct ptlrpc_request *req)
2372 struct obd_device *obd;
2375 /* Check that we still have all structures alive as this may
2376 * be some late RPC at shutdown time. */
2377 if (unlikely(!req->rq_export || !req->rq_export->exp_obd ||
2378 !exp_connect_lru_resize(req->rq_export))) {
2379 lustre_msg_set_slv(req->rq_repmsg, 0);
2380 lustre_msg_set_limit(req->rq_repmsg, 0);
2384 /* OBD is alive here as export is alive, which we checked above. */
2385 obd = req->rq_export->exp_obd;
2387 read_lock(&obd->obd_pool_lock);
2388 lustre_msg_set_slv(req->rq_repmsg, obd->obd_pool_slv);
2389 lustre_msg_set_limit(req->rq_repmsg, obd->obd_pool_limit);
2390 read_unlock(&obd->obd_pool_lock);
2394 EXPORT_SYMBOL(target_pack_pool_reply);
2396 int target_send_reply_msg(struct ptlrpc_request *req, int rc, int fail_id)
2398 if (OBD_FAIL_CHECK_ORSET(fail_id & ~OBD_FAIL_ONCE, OBD_FAIL_ONCE)) {
2399 DEBUG_REQ(D_ERROR, req, "dropping reply");
2404 DEBUG_REQ(D_NET, req, "processing error (%d)", rc);
2405 req->rq_status = rc;
2406 return (ptlrpc_send_error(req, 1));
2408 DEBUG_REQ(D_NET, req, "sending reply");
2411 return (ptlrpc_send_reply(req, PTLRPC_REPLY_MAYBE_DIFFICULT));
2414 void target_send_reply(struct ptlrpc_request *req, int rc, int fail_id)
2416 struct ptlrpc_service_part *svcpt;
2418 struct ptlrpc_reply_state *rs;
2419 struct obd_export *exp;
2422 if (req->rq_no_reply) {
2427 svcpt = req->rq_rqbd->rqbd_svcpt;
2428 rs = req->rq_reply_state;
2429 if (rs == NULL || !rs->rs_difficult) {
2431 target_send_reply_msg (req, rc, fail_id);
2436 /* must be an export if locks saved */
2437 LASSERT (req->rq_export != NULL);
2438 /* req/reply consistent */
2439 LASSERT(rs->rs_svcpt == svcpt);
2442 LASSERT (!rs->rs_scheduled);
2443 LASSERT (!rs->rs_scheduled_ever);
2444 LASSERT (!rs->rs_handled);
2445 LASSERT (!rs->rs_on_net);
2446 LASSERT (rs->rs_export == NULL);
2447 LASSERT (cfs_list_empty(&rs->rs_obd_list));
2448 LASSERT (cfs_list_empty(&rs->rs_exp_list));
2450 exp = class_export_get (req->rq_export);
2452 /* disable reply scheduling while I'm setting up */
2453 rs->rs_scheduled = 1;
2455 rs->rs_xid = req->rq_xid;
2456 rs->rs_transno = req->rq_transno;
2457 rs->rs_export = exp;
2458 rs->rs_opc = lustre_msg_get_opc(req->rq_reqmsg);
2460 spin_lock(&exp->exp_uncommitted_replies_lock);
2461 CDEBUG(D_NET, "rs transno = "LPU64", last committed = "LPU64"\n",
2462 rs->rs_transno, exp->exp_last_committed);
2463 if (rs->rs_transno > exp->exp_last_committed) {
2464 /* not committed already */
2465 cfs_list_add_tail(&rs->rs_obd_list,
2466 &exp->exp_uncommitted_replies);
2468 spin_unlock(&exp->exp_uncommitted_replies_lock);
2470 spin_lock(&exp->exp_lock);
2471 cfs_list_add_tail(&rs->rs_exp_list, &exp->exp_outstanding_replies);
2472 spin_unlock(&exp->exp_lock);
2474 netrc = target_send_reply_msg(req, rc, fail_id);
2476 spin_lock(&svcpt->scp_rep_lock);
2478 atomic_inc(&svcpt->scp_nreps_difficult);
2481 /* error sending: reply is off the net. Also we need +1
2482 * reply ref until ptlrpc_handle_rs() is done
2483 * with the reply state (if the send was successful, there
2484 * would have been +1 ref for the net, which
2485 * reply_out_callback leaves alone) */
2487 ptlrpc_rs_addref(rs);
2490 spin_lock(&rs->rs_lock);
2491 if (rs->rs_transno <= exp->exp_last_committed ||
2492 (!rs->rs_on_net && !rs->rs_no_ack) ||
2493 cfs_list_empty(&rs->rs_exp_list) || /* completed already */
2494 cfs_list_empty(&rs->rs_obd_list)) {
2495 CDEBUG(D_HA, "Schedule reply immediately\n");
2496 ptlrpc_dispatch_difficult_reply(rs);
2498 cfs_list_add(&rs->rs_list, &svcpt->scp_rep_active);
2499 rs->rs_scheduled = 0; /* allow notifier to schedule */
2501 spin_unlock(&rs->rs_lock);
2502 spin_unlock(&svcpt->scp_rep_lock);
2505 EXPORT_SYMBOL(target_send_reply);
2507 ldlm_mode_t lck_compat_array[] = {
2508 [LCK_EX] = LCK_COMPAT_EX,
2509 [LCK_PW] = LCK_COMPAT_PW,
2510 [LCK_PR] = LCK_COMPAT_PR,
2511 [LCK_CW] = LCK_COMPAT_CW,
2512 [LCK_CR] = LCK_COMPAT_CR,
2513 [LCK_NL] = LCK_COMPAT_NL,
2514 [LCK_GROUP] = LCK_COMPAT_GROUP,
2515 [LCK_COS] = LCK_COMPAT_COS,
2519 * Rather arbitrary mapping from LDLM error codes to errno values. This should
2520 * not escape to the user level.
2522 int ldlm_error2errno(ldlm_error_t error)
2530 case ELDLM_LOCK_CHANGED:
2533 case ELDLM_LOCK_ABORTED:
2536 case ELDLM_LOCK_REPLACED:
2539 case ELDLM_NO_LOCK_DATA:
2542 case ELDLM_NAMESPACE_EXISTS:
2545 case ELDLM_BAD_NAMESPACE:
2549 if (((int)error) < 0) /* cast to signed type */
2550 result = error; /* as ldlm_error_t can be unsigned */
2552 CERROR("Invalid DLM result code: %d\n", error);
2558 EXPORT_SYMBOL(ldlm_error2errno);
2561 * Dual to ldlm_error2errno(): maps errno values back to ldlm_error_t.
2563 ldlm_error_t ldlm_errno2error(int err_no)
2572 error = ELDLM_LOCK_CHANGED;
2575 error = ELDLM_LOCK_ABORTED;
2578 error = ELDLM_LOCK_REPLACED;
2581 error = ELDLM_NO_LOCK_DATA;
2584 error = ELDLM_NAMESPACE_EXISTS;
2587 error = ELDLM_BAD_NAMESPACE;
2594 EXPORT_SYMBOL(ldlm_errno2error);
2596 #if LUSTRE_TRACKS_LOCK_EXP_REFS
2597 void ldlm_dump_export_locks(struct obd_export *exp)
2599 spin_lock(&exp->exp_locks_list_guard);
2600 if (!cfs_list_empty(&exp->exp_locks_list)) {
2601 struct ldlm_lock *lock;
2603 CERROR("dumping locks for export %p,"
2604 "ignore if the unmount doesn't hang\n", exp);
2605 cfs_list_for_each_entry(lock, &exp->exp_locks_list,
2607 LDLM_ERROR(lock, "lock:");
2609 spin_unlock(&exp->exp_locks_list_guard);
2613 #ifdef HAVE_SERVER_SUPPORT
2614 static int target_bulk_timeout(void *data)
2617 /* We don't fail the connection here, because having the export
2618 * killed makes the (vital) call to commitrw very sad.
2623 static inline char *bulk2type(struct ptlrpc_bulk_desc *desc)
2625 return desc->bd_type == BULK_GET_SINK ? "GET" : "PUT";
2628 int target_bulk_io(struct obd_export *exp, struct ptlrpc_bulk_desc *desc,
2629 struct l_wait_info *lwi)
2631 struct ptlrpc_request *req = desc->bd_req;
2632 time_t start = cfs_time_current_sec();
2637 /* If there is eviction in progress, wait for it to finish. */
2638 if (unlikely(atomic_read(&exp->exp_obd->obd_evict_inprogress))) {
2639 *lwi = LWI_INTR(NULL, NULL);
2640 rc = l_wait_event(exp->exp_obd->obd_evict_inprogress_waitq,
2641 !atomic_read(&exp->exp_obd->
2642 obd_evict_inprogress),
2646 /* Check if client was evicted or reconnected already. */
2647 if (exp->exp_failed ||
2648 exp->exp_conn_cnt > lustre_msg_get_conn_cnt(req->rq_reqmsg)) {
2651 if (desc->bd_type == BULK_PUT_SINK)
2652 rc = sptlrpc_svc_wrap_bulk(req, desc);
2654 rc = ptlrpc_start_bulk_transfer(desc);
2658 DEBUG_REQ(D_ERROR, req, "bulk %s failed: rc %d",
2659 bulk2type(desc), rc);
2663 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE)) {
2664 ptlrpc_abort_bulk(desc);
2669 long timeoutl = req->rq_deadline - cfs_time_current_sec();
2670 cfs_duration_t timeout = timeoutl <= 0 ?
2671 CFS_TICK : cfs_time_seconds(timeoutl);
2673 *lwi = LWI_TIMEOUT_INTERVAL(timeout, cfs_time_seconds(1),
2674 target_bulk_timeout, desc);
2675 rc = l_wait_event(desc->bd_waitq,
2676 !ptlrpc_server_bulk_active(desc) ||
2679 lustre_msg_get_conn_cnt(req->rq_reqmsg),
2681 LASSERT(rc == 0 || rc == -ETIMEDOUT);
2682 /* Wait again if we changed deadline. */
2683 } while ((rc == -ETIMEDOUT) &&
2684 (req->rq_deadline > cfs_time_current_sec()));
2686 if (rc == -ETIMEDOUT) {
2687 DEBUG_REQ(D_ERROR, req, "timeout on bulk %s after %ld%+lds",
2688 bulk2type(desc), req->rq_deadline - start,
2689 cfs_time_current_sec() - req->rq_deadline);
2690 ptlrpc_abort_bulk(desc);
2691 } else if (exp->exp_failed) {
2692 DEBUG_REQ(D_ERROR, req, "Eviction on bulk %s",
2695 ptlrpc_abort_bulk(desc);
2696 } else if (exp->exp_conn_cnt >
2697 lustre_msg_get_conn_cnt(req->rq_reqmsg)) {
2698 DEBUG_REQ(D_ERROR, req, "Reconnect on bulk %s",
2700 /* We don't reply anyway. */
2702 ptlrpc_abort_bulk(desc);
2703 } else if (desc->bd_failure ||
2704 desc->bd_nob_transferred != desc->bd_nob) {
2705 DEBUG_REQ(D_ERROR, req, "%s bulk %s %d(%d)",
2706 desc->bd_failure ? "network error on" : "truncated",
2707 bulk2type(desc), desc->bd_nob_transferred,
2709 /* XXX Should this be a different errno? */
2711 } else if (desc->bd_type == BULK_GET_SINK) {
2712 rc = sptlrpc_svc_unwrap_bulk(req, desc);
2717 EXPORT_SYMBOL(target_bulk_io);
2719 #endif /* HAVE_SERVER_SUPPORT */