1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
33 * Copyright (c) 2011 Whamcloud, Inc.
36 * This file is part of Lustre, http://www.lustre.org/
37 * Lustre is a trademark of Sun Microsystems, Inc.
41 # define EXPORT_SYMTAB
43 #define DEBUG_SUBSYSTEM S_LDLM
46 # include <libcfs/libcfs.h>
48 # include <liblustre.h>
51 #include <lustre_mds.h>
52 #include <lustre_dlm.h>
53 #include <lustre_net.h>
54 #include <lustre_sec.h>
55 #include "ldlm_internal.h"
57 /* @priority: if non-zero, move the selected to the list head
58 * @create: if zero, only search in existed connections
60 static int import_set_conn(struct obd_import *imp, struct obd_uuid *uuid,
61 int priority, int create)
63 struct ptlrpc_connection *ptlrpc_conn;
64 struct obd_import_conn *imp_conn = NULL, *item;
68 if (!create && !priority) {
69 CDEBUG(D_HA, "Nothing to do\n");
73 ptlrpc_conn = ptlrpc_uuid_to_connection(uuid);
75 CDEBUG(D_HA, "can't find connection %s\n", uuid->uuid);
80 OBD_ALLOC(imp_conn, sizeof(*imp_conn));
82 GOTO(out_put, rc = -ENOMEM);
86 cfs_spin_lock(&imp->imp_lock);
87 cfs_list_for_each_entry(item, &imp->imp_conn_list, oic_item) {
88 if (obd_uuid_equals(uuid, &item->oic_uuid)) {
90 cfs_list_del(&item->oic_item);
91 cfs_list_add(&item->oic_item,
93 item->oic_last_attempt = 0;
95 CDEBUG(D_HA, "imp %p@%s: found existing conn %s%s\n",
96 imp, imp->imp_obd->obd_name, uuid->uuid,
97 (priority ? ", moved to head" : ""));
98 cfs_spin_unlock(&imp->imp_lock);
99 GOTO(out_free, rc = 0);
104 imp_conn->oic_conn = ptlrpc_conn;
105 imp_conn->oic_uuid = *uuid;
106 imp_conn->oic_last_attempt = 0;
108 cfs_list_add(&imp_conn->oic_item, &imp->imp_conn_list);
110 cfs_list_add_tail(&imp_conn->oic_item,
111 &imp->imp_conn_list);
112 CDEBUG(D_HA, "imp %p@%s: add connection %s at %s\n",
113 imp, imp->imp_obd->obd_name, uuid->uuid,
114 (priority ? "head" : "tail"));
116 cfs_spin_unlock(&imp->imp_lock);
117 GOTO(out_free, rc = -ENOENT);
120 cfs_spin_unlock(&imp->imp_lock);
124 OBD_FREE(imp_conn, sizeof(*imp_conn));
126 ptlrpc_connection_put(ptlrpc_conn);
130 int import_set_conn_priority(struct obd_import *imp, struct obd_uuid *uuid)
132 return import_set_conn(imp, uuid, 1, 0);
135 int client_import_add_conn(struct obd_import *imp, struct obd_uuid *uuid,
138 return import_set_conn(imp, uuid, priority, 1);
141 int client_import_del_conn(struct obd_import *imp, struct obd_uuid *uuid)
143 struct obd_import_conn *imp_conn;
144 struct obd_export *dlmexp;
148 cfs_spin_lock(&imp->imp_lock);
149 if (cfs_list_empty(&imp->imp_conn_list)) {
150 LASSERT(!imp->imp_connection);
154 cfs_list_for_each_entry(imp_conn, &imp->imp_conn_list, oic_item) {
155 if (!obd_uuid_equals(uuid, &imp_conn->oic_uuid))
157 LASSERT(imp_conn->oic_conn);
159 /* is current conn? */
160 if (imp_conn == imp->imp_conn_current) {
161 LASSERT(imp_conn->oic_conn == imp->imp_connection);
163 if (imp->imp_state != LUSTRE_IMP_CLOSED &&
164 imp->imp_state != LUSTRE_IMP_DISCON) {
165 CERROR("can't remove current connection\n");
166 GOTO(out, rc = -EBUSY);
169 ptlrpc_connection_put(imp->imp_connection);
170 imp->imp_connection = NULL;
172 dlmexp = class_conn2export(&imp->imp_dlm_handle);
173 if (dlmexp && dlmexp->exp_connection) {
174 LASSERT(dlmexp->exp_connection ==
176 ptlrpc_connection_put(dlmexp->exp_connection);
177 dlmexp->exp_connection = NULL;
181 cfs_list_del(&imp_conn->oic_item);
182 ptlrpc_connection_put(imp_conn->oic_conn);
183 OBD_FREE(imp_conn, sizeof(*imp_conn));
184 CDEBUG(D_HA, "imp %p@%s: remove connection %s\n",
185 imp, imp->imp_obd->obd_name, uuid->uuid);
190 cfs_spin_unlock(&imp->imp_lock);
192 CERROR("connection %s not found\n", uuid->uuid);
197 * Find conn uuid by peer nid. @peer is a server nid. This function is used
198 * to find a conn uuid of @imp which can reach @peer.
200 int client_import_find_conn(struct obd_import *imp, lnet_nid_t peer,
201 struct obd_uuid *uuid)
203 struct obd_import_conn *conn;
207 cfs_spin_lock(&imp->imp_lock);
208 cfs_list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
209 /* check if conn uuid does have this peer nid */
210 if (class_check_uuid(&conn->oic_uuid, peer)) {
211 *uuid = conn->oic_uuid;
216 cfs_spin_unlock(&imp->imp_lock);
219 EXPORT_SYMBOL(client_import_find_conn);
221 void client_destroy_import(struct obd_import *imp)
223 /* drop security policy instance after all rpc finished/aborted
224 * to let all busy contexts be released. */
225 class_import_get(imp);
226 class_destroy_import(imp);
227 sptlrpc_import_sec_put(imp);
228 class_import_put(imp);
231 /* configure an RPC client OBD device
236 * 3 - inactive-on-startup
238 int client_obd_setup(struct obd_device *obddev, struct lustre_cfg *lcfg)
240 struct client_obd *cli = &obddev->u.cli;
241 struct obd_import *imp;
242 struct obd_uuid server_uuid;
243 int rq_portal, rp_portal, connect_op;
244 char *name = obddev->obd_type->typ_name;
245 ldlm_ns_type_t ns_type = LDLM_NS_TYPE_UNKNOWN;
249 /* In a more perfect world, we would hang a ptlrpc_client off of
250 * obd_type and just use the values from there. */
251 if (!strcmp(name, LUSTRE_OSC_NAME)) {
252 rq_portal = OST_REQUEST_PORTAL;
253 rp_portal = OSC_REPLY_PORTAL;
254 connect_op = OST_CONNECT;
255 cli->cl_sp_me = LUSTRE_SP_CLI;
256 cli->cl_sp_to = LUSTRE_SP_OST;
257 ns_type = LDLM_NS_TYPE_OSC;
259 } else if (!strcmp(name, LUSTRE_MDC_NAME)) {
260 rq_portal = MDS_REQUEST_PORTAL;
261 rp_portal = MDC_REPLY_PORTAL;
262 connect_op = MDS_CONNECT;
263 cli->cl_sp_me = LUSTRE_SP_CLI;
264 cli->cl_sp_to = LUSTRE_SP_MDT;
265 ns_type = LDLM_NS_TYPE_MDC;
267 } else if (!strcmp(name, LUSTRE_MGC_NAME)) {
268 rq_portal = MGS_REQUEST_PORTAL;
269 rp_portal = MGC_REPLY_PORTAL;
270 connect_op = MGS_CONNECT;
271 cli->cl_sp_me = LUSTRE_SP_MGC;
272 cli->cl_sp_to = LUSTRE_SP_MGS;
273 cli->cl_flvr_mgc.sf_rpc = SPTLRPC_FLVR_INVALID;
274 ns_type = LDLM_NS_TYPE_MGC;
277 CERROR("unknown client OBD type \"%s\", can't setup\n",
282 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
283 CERROR("requires a TARGET UUID\n");
287 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > 37) {
288 CERROR("client UUID must be less than 38 characters\n");
292 if (LUSTRE_CFG_BUFLEN(lcfg, 2) < 1) {
293 CERROR("setup requires a SERVER UUID\n");
297 if (LUSTRE_CFG_BUFLEN(lcfg, 2) > 37) {
298 CERROR("target UUID must be less than 38 characters\n");
302 cfs_init_rwsem(&cli->cl_sem);
303 cfs_sema_init(&cli->cl_mgc_sem, 1);
304 cli->cl_conn_count = 0;
305 memcpy(server_uuid.uuid, lustre_cfg_buf(lcfg, 2),
306 min_t(unsigned int, LUSTRE_CFG_BUFLEN(lcfg, 2),
307 sizeof(server_uuid)));
310 cli->cl_avail_grant = 0;
311 /* FIXME: should limit this for the sum of all cl_dirty_max */
312 cli->cl_dirty_max = OSC_MAX_DIRTY_DEFAULT * 1024 * 1024;
313 if (cli->cl_dirty_max >> CFS_PAGE_SHIFT > cfs_num_physpages / 8)
314 cli->cl_dirty_max = cfs_num_physpages << (CFS_PAGE_SHIFT - 3);
315 CFS_INIT_LIST_HEAD(&cli->cl_cache_waiters);
316 CFS_INIT_LIST_HEAD(&cli->cl_loi_ready_list);
317 CFS_INIT_LIST_HEAD(&cli->cl_loi_hp_ready_list);
318 CFS_INIT_LIST_HEAD(&cli->cl_loi_write_list);
319 CFS_INIT_LIST_HEAD(&cli->cl_loi_read_list);
320 client_obd_list_lock_init(&cli->cl_loi_list_lock);
321 cli->cl_r_in_flight = 0;
322 cli->cl_w_in_flight = 0;
324 cfs_spin_lock_init(&cli->cl_read_rpc_hist.oh_lock);
325 cfs_spin_lock_init(&cli->cl_write_rpc_hist.oh_lock);
326 cfs_spin_lock_init(&cli->cl_read_page_hist.oh_lock);
327 cfs_spin_lock_init(&cli->cl_write_page_hist.oh_lock);
328 cfs_spin_lock_init(&cli->cl_read_offset_hist.oh_lock);
329 cfs_spin_lock_init(&cli->cl_write_offset_hist.oh_lock);
330 cfs_waitq_init(&cli->cl_destroy_waitq);
331 cfs_atomic_set(&cli->cl_destroy_in_flight, 0);
332 #ifdef ENABLE_CHECKSUM
333 /* Turn on checksumming by default. */
334 cli->cl_checksum = 1;
336 * The supported checksum types will be worked out at connect time
337 * Set cl_chksum* to CRC32 for now to avoid returning screwed info
340 cli->cl_cksum_type = cli->cl_supp_cksum_types = OBD_CKSUM_CRC32;
342 cfs_atomic_set(&cli->cl_resends, OSC_DEFAULT_RESENDS);
344 /* This value may be changed at connect time in
345 ptlrpc_connect_interpret. */
346 cli->cl_max_pages_per_rpc = min((int)PTLRPC_MAX_BRW_PAGES,
347 (int)(1024 * 1024 >> CFS_PAGE_SHIFT));
349 if (!strcmp(name, LUSTRE_MDC_NAME)) {
350 cli->cl_max_rpcs_in_flight = MDC_MAX_RIF_DEFAULT;
351 } else if (cfs_num_physpages >> (20 - CFS_PAGE_SHIFT) <= 128 /* MB */) {
352 cli->cl_max_rpcs_in_flight = 2;
353 } else if (cfs_num_physpages >> (20 - CFS_PAGE_SHIFT) <= 256 /* MB */) {
354 cli->cl_max_rpcs_in_flight = 3;
355 } else if (cfs_num_physpages >> (20 - CFS_PAGE_SHIFT) <= 512 /* MB */) {
356 cli->cl_max_rpcs_in_flight = 4;
358 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT;
363 CERROR("ldlm_get_ref failed: %d\n", rc);
367 ptlrpc_init_client(rq_portal, rp_portal, name,
368 &obddev->obd_ldlm_client);
370 imp = class_new_import(obddev);
372 GOTO(err_ldlm, rc = -ENOENT);
373 imp->imp_client = &obddev->obd_ldlm_client;
374 imp->imp_connect_op = connect_op;
375 CFS_INIT_LIST_HEAD(&imp->imp_pinger_chain);
376 memcpy(cli->cl_target_uuid.uuid, lustre_cfg_buf(lcfg, 1),
377 LUSTRE_CFG_BUFLEN(lcfg, 1));
378 class_import_put(imp);
380 rc = client_import_add_conn(imp, &server_uuid, 1);
382 CERROR("can't add initial connection\n");
383 GOTO(err_import, rc);
386 cli->cl_import = imp;
387 /* cli->cl_max_mds_{easize,cookiesize} updated by mdc_init_ea_size() */
388 cli->cl_max_mds_easize = sizeof(struct lov_mds_md_v3);
389 cli->cl_max_mds_cookiesize = sizeof(struct llog_cookie);
391 if (LUSTRE_CFG_BUFLEN(lcfg, 3) > 0) {
392 if (!strcmp(lustre_cfg_string(lcfg, 3), "inactive")) {
393 CDEBUG(D_HA, "marking %s %s->%s as inactive\n",
394 name, obddev->obd_name,
395 cli->cl_target_uuid.uuid);
396 cfs_spin_lock(&imp->imp_lock);
397 imp->imp_deactive = 1;
398 cfs_spin_unlock(&imp->imp_lock);
402 obddev->obd_namespace = ldlm_namespace_new(obddev, obddev->obd_name,
403 LDLM_NAMESPACE_CLIENT,
404 LDLM_NAMESPACE_GREEDY,
406 if (obddev->obd_namespace == NULL) {
407 CERROR("Unable to create client namespace - %s\n",
409 GOTO(err_import, rc = -ENOMEM);
412 cli->cl_qchk_stat = CL_NOT_QUOTACHECKED;
417 class_destroy_import(imp);
425 int client_obd_cleanup(struct obd_device *obddev)
429 ldlm_namespace_free_post(obddev->obd_namespace);
430 obddev->obd_namespace = NULL;
436 /* ->o_connect() method for client side (OSC and MDC and MGC) */
437 int client_connect_import(const struct lu_env *env,
438 struct obd_export **exp,
439 struct obd_device *obd, struct obd_uuid *cluuid,
440 struct obd_connect_data *data, void *localdata)
442 struct client_obd *cli = &obd->u.cli;
443 struct obd_import *imp = cli->cl_import;
444 struct obd_connect_data *ocd;
445 struct lustre_handle conn = { 0 };
450 cfs_down_write(&cli->cl_sem);
451 if (cli->cl_conn_count > 0 )
452 GOTO(out_sem, rc = -EALREADY);
454 rc = class_connect(&conn, obd, cluuid);
458 cli->cl_conn_count++;
459 *exp = class_conn2export(&conn);
461 LASSERT(obd->obd_namespace);
463 imp->imp_dlm_handle = conn;
464 rc = ptlrpc_init_import(imp);
468 ocd = &imp->imp_connect_data;
471 imp->imp_connect_flags_orig = data->ocd_connect_flags;
474 rc = ptlrpc_connect_import(imp, NULL);
476 LASSERT (imp->imp_state == LUSTRE_IMP_DISCON);
479 LASSERT((*exp)->exp_connection);
482 LASSERTF((ocd->ocd_connect_flags & data->ocd_connect_flags) ==
483 ocd->ocd_connect_flags, "old "LPX64", new "LPX64"\n",
484 data->ocd_connect_flags, ocd->ocd_connect_flags);
485 data->ocd_connect_flags = ocd->ocd_connect_flags;
488 ptlrpc_pinger_add_import(imp);
494 cli->cl_conn_count--;
495 class_disconnect(*exp);
499 cfs_up_write(&cli->cl_sem);
504 int client_disconnect_export(struct obd_export *exp)
506 struct obd_device *obd = class_exp2obd(exp);
507 struct client_obd *cli;
508 struct obd_import *imp;
513 CERROR("invalid export for disconnect: exp %p cookie "LPX64"\n",
514 exp, exp ? exp->exp_handle.h_cookie : -1);
519 imp = cli->cl_import;
521 cfs_down_write(&cli->cl_sem);
522 CDEBUG(D_INFO, "disconnect %s - %d\n", obd->obd_name,
525 if (!cli->cl_conn_count) {
526 CERROR("disconnecting disconnected device (%s)\n",
528 GOTO(out_disconnect, rc = -EINVAL);
531 cli->cl_conn_count--;
532 if (cli->cl_conn_count)
533 GOTO(out_disconnect, rc = 0);
535 /* Mark import deactivated now, so we don't try to reconnect if any
536 * of the cleanup RPCs fails (e.g. ldlm cancel, etc). We don't
537 * fully deactivate the import, or that would drop all requests. */
538 cfs_spin_lock(&imp->imp_lock);
539 imp->imp_deactive = 1;
540 cfs_spin_unlock(&imp->imp_lock);
542 /* Some non-replayable imports (MDS's OSCs) are pinged, so just
543 * delete it regardless. (It's safe to delete an import that was
545 (void)ptlrpc_pinger_del_import(imp);
547 if (obd->obd_namespace != NULL) {
548 /* obd_force == local only */
549 ldlm_cli_cancel_unused(obd->obd_namespace, NULL,
550 obd->obd_force ? LCF_LOCAL : 0, NULL);
551 ldlm_namespace_free_prior(obd->obd_namespace, imp, obd->obd_force);
555 * there's no need to hold sem during disconnecting an import,
556 * and actually it may cause deadlock in gss.
558 cfs_up_write(&cli->cl_sem);
559 rc = ptlrpc_disconnect_import(imp, 0);
560 cfs_down_write(&cli->cl_sem);
562 ptlrpc_invalidate_import(imp);
564 if (imp->imp_rq_pool) {
565 ptlrpc_free_rq_pool(imp->imp_rq_pool);
566 imp->imp_rq_pool = NULL;
568 client_destroy_import(imp);
569 cli->cl_import = NULL;
574 /* use server style - class_disconnect should be always called for
576 err = class_disconnect(exp);
580 cfs_up_write(&cli->cl_sem);
585 int server_disconnect_export(struct obd_export *exp)
590 /* Disconnect early so that clients can't keep using export */
591 rc = class_disconnect(exp);
592 /* close import for avoid sending any requests */
593 if (exp->exp_imp_reverse)
594 ptlrpc_cleanup_imp(exp->exp_imp_reverse);
596 if (exp->exp_obd->obd_namespace != NULL)
597 ldlm_cancel_locks_for_export(exp);
599 /* complete all outstanding replies */
600 cfs_spin_lock(&exp->exp_lock);
601 while (!cfs_list_empty(&exp->exp_outstanding_replies)) {
602 struct ptlrpc_reply_state *rs =
603 cfs_list_entry(exp->exp_outstanding_replies.next,
604 struct ptlrpc_reply_state, rs_exp_list);
605 struct ptlrpc_service *svc = rs->rs_service;
607 cfs_spin_lock(&svc->srv_rs_lock);
608 cfs_list_del_init(&rs->rs_exp_list);
609 cfs_spin_lock(&rs->rs_lock);
610 ptlrpc_schedule_difficult_reply(rs);
611 cfs_spin_unlock(&rs->rs_lock);
612 cfs_spin_unlock(&svc->srv_rs_lock);
614 cfs_spin_unlock(&exp->exp_lock);
619 /* --------------------------------------------------------------------------
620 * from old lib/target.c
621 * -------------------------------------------------------------------------- */
623 static int target_handle_reconnect(struct lustre_handle *conn,
624 struct obd_export *exp,
625 struct obd_uuid *cluuid)
628 if (exp->exp_connection && exp->exp_imp_reverse) {
629 struct lustre_handle *hdl;
630 hdl = &exp->exp_imp_reverse->imp_remote_handle;
631 /* Might be a re-connect after a partition. */
632 if (!memcmp(&conn->cookie, &hdl->cookie, sizeof conn->cookie)) {
633 CWARN("%s: %s reconnecting\n", exp->exp_obd->obd_name,
635 conn->cookie = exp->exp_handle.h_cookie;
636 /* target_handle_connect() treats EALREADY and
637 * -EALREADY differently. EALREADY means we are
638 * doing a valid reconnect from the same client. */
641 CERROR("%s reconnecting from %s, "
642 "handle mismatch (ours "LPX64", theirs "
643 LPX64")\n", cluuid->uuid,
644 exp->exp_connection->c_remote_uuid.uuid,
645 hdl->cookie, conn->cookie);
646 memset(conn, 0, sizeof *conn);
647 /* target_handle_connect() treats EALREADY and
648 * -EALREADY differently. -EALREADY is an error
649 * (same UUID, different handle). */
654 conn->cookie = exp->exp_handle.h_cookie;
655 CDEBUG(D_HA, "connect export for UUID '%s' at %p, cookie "LPX64"\n",
656 cluuid->uuid, exp, conn->cookie);
660 void target_client_add_cb(struct obd_device *obd, __u64 transno, void *cb_data,
663 struct obd_export *exp = cb_data;
665 CDEBUG(D_RPCTRACE, "%s: committing for initial connect of %s\n",
666 obd->obd_name, exp->exp_client_uuid.uuid);
668 cfs_spin_lock(&exp->exp_lock);
669 exp->exp_need_sync = 0;
670 cfs_spin_unlock(&exp->exp_lock);
671 class_export_cb_put(exp);
673 EXPORT_SYMBOL(target_client_add_cb);
676 target_start_and_reset_recovery_timer(struct obd_device *obd,
677 struct ptlrpc_request *req,
680 int target_handle_connect(struct ptlrpc_request *req)
682 struct obd_device *target, *targref = NULL;
683 struct obd_export *export = NULL;
684 struct obd_import *revimp;
685 struct lustre_handle conn;
686 struct lustre_handle *tmp;
687 struct obd_uuid tgtuuid;
688 struct obd_uuid cluuid;
689 struct obd_uuid remote_uuid;
693 struct obd_connect_data *data, *tmpdata;
695 lnet_nid_t *client_nid = NULL;
698 OBD_RACE(OBD_FAIL_TGT_CONN_RACE);
700 str = req_capsule_client_get(&req->rq_pill, &RMF_TGTUUID);
702 DEBUG_REQ(D_ERROR, req, "bad target UUID for connect");
703 GOTO(out, rc = -EINVAL);
706 obd_str2uuid(&tgtuuid, str);
707 target = class_uuid2obd(&tgtuuid);
709 target = class_name2obd(str);
711 if (!target || target->obd_stopping || !target->obd_set_up) {
712 LCONSOLE_ERROR_MSG(0x137, "UUID '%s' is not available "
713 " for connect (%s)\n", str,
714 !target ? "no target" :
715 (target->obd_stopping ? "stopping" :
717 GOTO(out, rc = -ENODEV);
720 if (target->obd_no_conn) {
721 LCONSOLE_WARN("%s: temporarily refusing client connection "
722 "from %s\n", target->obd_name,
723 libcfs_nid2str(req->rq_peer.nid));
724 GOTO(out, rc = -EAGAIN);
727 /* Make sure the target isn't cleaned up while we're here. Yes,
728 there's still a race between the above check and our incref here.
729 Really, class_uuid2obd should take the ref. */
730 targref = class_incref(target, __FUNCTION__, cfs_current());
733 str = req_capsule_client_get(&req->rq_pill, &RMF_CLUUID);
735 DEBUG_REQ(D_ERROR, req, "bad client UUID for connect");
736 GOTO(out, rc = -EINVAL);
739 obd_str2uuid(&cluuid, str);
741 /* XXX extract a nettype and format accordingly */
742 switch (sizeof(lnet_nid_t)) {
743 /* NB the casts only avoid compiler warnings */
745 snprintf(remote_uuid.uuid, sizeof remote_uuid,
746 "NET_"LPX64"_UUID", (__u64)req->rq_peer.nid);
749 snprintf(remote_uuid.uuid, sizeof remote_uuid,
750 "NET_%x_UUID", (__u32)req->rq_peer.nid);
756 tmp = req_capsule_client_get(&req->rq_pill, &RMF_CONN);
758 GOTO(out, rc = -EPROTO);
762 size = req_capsule_get_size(&req->rq_pill, &RMF_CONNECT_DATA,
764 data = req_capsule_client_get(&req->rq_pill, &RMF_CONNECT_DATA);
766 GOTO(out, rc = -EPROTO);
768 rc = req_capsule_server_pack(&req->rq_pill);
772 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT) {
774 DEBUG_REQ(D_WARNING, req, "Refusing old (unversioned) "
775 "libclient connection attempt");
776 GOTO(out, rc = -EPROTO);
777 } else if (data->ocd_version < LUSTRE_VERSION_CODE -
778 LUSTRE_VERSION_ALLOWED_OFFSET ||
779 data->ocd_version > LUSTRE_VERSION_CODE +
780 LUSTRE_VERSION_ALLOWED_OFFSET) {
781 DEBUG_REQ(D_WARNING, req, "Refusing %s (%d.%d.%d.%d) "
782 "libclient connection attempt",
783 data->ocd_version < LUSTRE_VERSION_CODE ?
785 OBD_OCD_VERSION_MAJOR(data->ocd_version),
786 OBD_OCD_VERSION_MINOR(data->ocd_version),
787 OBD_OCD_VERSION_PATCH(data->ocd_version),
788 OBD_OCD_VERSION_FIX(data->ocd_version));
789 data = req_capsule_server_sized_get(&req->rq_pill,
791 offsetof(typeof(*data), ocd_version) +
792 sizeof(data->ocd_version));
794 data->ocd_connect_flags = OBD_CONNECT_VERSION;
795 data->ocd_version = LUSTRE_VERSION_CODE;
797 GOTO(out, rc = -EPROTO);
801 if ((lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_INITIAL) &&
802 (data->ocd_connect_flags & OBD_CONNECT_MDS))
805 /* lctl gets a backstage, all-access pass. */
806 if (obd_uuid_equals(&cluuid, &target->obd_uuid))
807 goto dont_check_exports;
809 export = cfs_hash_lookup(target->obd_uuid_hash, &cluuid);
813 /* we've found an export in the hash */
814 if (export->exp_connecting) { /* bug 9635, et. al. */
815 CWARN("%s: exp %p already connecting\n",
816 export->exp_obd->obd_name, export);
817 class_export_put(export);
820 } else if (mds_conn && export->exp_connection) {
821 if (req->rq_peer.nid != export->exp_connection->c_peer.nid)
822 /* mds reconnected after failover */
823 CWARN("%s: received MDS connection from NID %s,"
824 " removing former export from NID %s\n",
825 target->obd_name, libcfs_nid2str(req->rq_peer.nid),
826 libcfs_nid2str(export->exp_connection->c_peer.nid));
828 /* new mds connection from the same nid */
829 CWARN("%s: received new MDS connection from NID %s,"
830 " removing former export from same NID\n",
831 target->obd_name, libcfs_nid2str(req->rq_peer.nid));
832 class_fail_export(export);
833 class_export_put(export);
836 } else if (export->exp_connection != NULL &&
837 req->rq_peer.nid != export->exp_connection->c_peer.nid &&
838 (lustre_msg_get_op_flags(req->rq_reqmsg) &
839 MSG_CONNECT_INITIAL)) {
840 /* in mds failover we have static uuid but nid can be
842 CWARN("%s: cookie %s seen on new NID %s when "
843 "existing NID %s is already connected\n",
844 target->obd_name, cluuid.uuid,
845 libcfs_nid2str(req->rq_peer.nid),
846 libcfs_nid2str(export->exp_connection->c_peer.nid));
848 class_export_put(export);
851 cfs_spin_lock(&export->exp_lock);
852 export->exp_connecting = 1;
853 cfs_spin_unlock(&export->exp_lock);
854 class_export_put(export);
855 LASSERT(export->exp_obd == target);
857 rc = target_handle_reconnect(&conn, export, &cluuid);
860 /* If we found an export, we already unlocked. */
863 OBD_FAIL_TIMEOUT(OBD_FAIL_TGT_DELAY_CONNECT, 2 * obd_timeout);
864 } else if (req->rq_export == NULL &&
865 cfs_atomic_read(&export->exp_rpc_count) > 0) {
866 CWARN("%s: refuse connection from %s/%s to 0x%p/%d\n",
867 target->obd_name, cluuid.uuid,
868 libcfs_nid2str(req->rq_peer.nid),
869 export, cfs_atomic_read(&export->exp_refcount));
870 GOTO(out, rc = -EBUSY);
871 } else if (req->rq_export != NULL &&
872 (cfs_atomic_read(&export->exp_rpc_count) > 1)) {
873 /* the current connect rpc has increased exp_rpc_count */
874 CWARN("%s: refuse reconnection from %s@%s to 0x%p/%d\n",
875 target->obd_name, cluuid.uuid,
876 libcfs_nid2str(req->rq_peer.nid),
877 export, cfs_atomic_read(&export->exp_rpc_count) - 1);
878 cfs_spin_lock(&export->exp_lock);
879 if (req->rq_export->exp_conn_cnt <
880 lustre_msg_get_conn_cnt(req->rq_reqmsg))
881 /* try to abort active requests */
882 req->rq_export->exp_abort_active_req = 1;
883 cfs_spin_unlock(&export->exp_lock);
884 GOTO(out, rc = -EBUSY);
885 } else if (lustre_msg_get_conn_cnt(req->rq_reqmsg) == 1) {
886 CERROR("%s: NID %s (%s) reconnected with 1 conn_cnt; "
887 "cookies not random?\n", target->obd_name,
888 libcfs_nid2str(req->rq_peer.nid), cluuid.uuid);
889 GOTO(out, rc = -EALREADY);
891 OBD_FAIL_TIMEOUT(OBD_FAIL_TGT_DELAY_RECONNECT, 2 * obd_timeout);
898 CWARN("%s: connection from %s@%s %st"LPU64" exp %p cur %ld last %ld\n",
899 target->obd_name, cluuid.uuid, libcfs_nid2str(req->rq_peer.nid),
900 target->obd_recovering ? "recovering/" : "", data->ocd_transno,
901 export, (long)cfs_time_current_sec(),
902 export ? (long)export->exp_last_request_time : 0);
904 /* If this is the first time a client connects,
905 * reset the recovery timer */
906 if (rc == 0 && target->obd_recovering)
907 target_start_and_reset_recovery_timer(target, req, !export);
909 /* We want to handle EALREADY but *not* -EALREADY from
910 * target_handle_reconnect(), return reconnection state in a flag */
911 if (rc == EALREADY) {
912 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECONNECT);
918 /* Tell the client if we support replayable requests */
919 if (target->obd_replayable)
920 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
921 client_nid = &req->rq_peer.nid;
923 if (export == NULL) {
924 if (target->obd_recovering) {
927 t = cfs_timer_deadline(&target->obd_recovery_timer);
928 t = cfs_time_sub(t, cfs_time_current());
929 CERROR("%s: denying connection for new client %s (%s): "
930 "%d clients in recovery for "CFS_TIME_T"s\n",
932 libcfs_nid2str(req->rq_peer.nid), cluuid.uuid,
933 cfs_atomic_read(&target-> \
934 obd_lock_replay_clients),
935 cfs_duration_sec(t));
939 rc = obd_connect(req->rq_svc_thread->t_env,
940 &export, target, &cluuid, data,
943 conn.cookie = export->exp_handle.h_cookie;
946 rc = obd_reconnect(req->rq_svc_thread->t_env,
947 export, target, &cluuid, data, client_nid);
949 /* prevous done via class_conn2export */
950 class_export_get(export);
954 /* Return only the parts of obd_connect_data that we understand, so the
955 * client knows that we don't understand the rest. */
957 tmpsize = req_capsule_get_size(&req->rq_pill, &RMF_CONNECT_DATA,
959 tmpdata = req_capsule_server_get(&req->rq_pill,
961 /* Don't use struct assignment here, because the client reply
962 * buffer may be smaller/larger than the local struct
963 * obd_connect_data. */
964 memcpy(tmpdata, data, min(tmpsize, size));
967 /* If all else goes well, this is our RPC return code. */
970 lustre_msg_set_handle(req->rq_repmsg, &conn);
972 /* If the client and the server are the same node, we will already
973 * have an export that really points to the client's DLM export,
974 * because we have a shared handles table.
976 * XXX this will go away when shaver stops sending the "connect" handle
977 * in the real "remote handle" field of the request --phik 24 Apr 2003
979 if (req->rq_export != NULL)
980 class_export_put(req->rq_export);
982 req->rq_export = export;
984 cfs_spin_lock(&export->exp_lock);
985 if (export->exp_conn_cnt >= lustre_msg_get_conn_cnt(req->rq_reqmsg)) {
986 cfs_spin_unlock(&export->exp_lock);
987 CERROR("%s: %s already connected at higher conn_cnt: %d > %d\n",
988 cluuid.uuid, libcfs_nid2str(req->rq_peer.nid),
989 export->exp_conn_cnt,
990 lustre_msg_get_conn_cnt(req->rq_reqmsg));
992 GOTO(out, rc = -EALREADY);
994 LASSERT(lustre_msg_get_conn_cnt(req->rq_reqmsg) > 0);
995 export->exp_conn_cnt = lustre_msg_get_conn_cnt(req->rq_reqmsg);
996 export->exp_abort_active_req = 0;
998 /* request from liblustre? Don't evict it for not pinging. */
999 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT) {
1000 export->exp_libclient = 1;
1001 cfs_spin_unlock(&export->exp_lock);
1003 cfs_spin_lock(&target->obd_dev_lock);
1004 cfs_list_del_init(&export->exp_obd_chain_timed);
1005 cfs_spin_unlock(&target->obd_dev_lock);
1007 cfs_spin_unlock(&export->exp_lock);
1010 if (export->exp_connection != NULL) {
1011 /* Check to see if connection came from another NID */
1012 if ((export->exp_connection->c_peer.nid != req->rq_peer.nid) &&
1013 !cfs_hlist_unhashed(&export->exp_nid_hash))
1014 cfs_hash_del(export->exp_obd->obd_nid_hash,
1015 &export->exp_connection->c_peer.nid,
1016 &export->exp_nid_hash);
1018 ptlrpc_connection_put(export->exp_connection);
1021 export->exp_connection = ptlrpc_connection_get(req->rq_peer,
1024 if (cfs_hlist_unhashed(&export->exp_nid_hash)) {
1025 cfs_hash_add(export->exp_obd->obd_nid_hash,
1026 &export->exp_connection->c_peer.nid,
1027 &export->exp_nid_hash);
1030 cfs_spin_lock(&target->obd_recovery_task_lock);
1031 if (target->obd_recovering && !export->exp_in_recovery) {
1032 cfs_spin_lock(&export->exp_lock);
1033 export->exp_in_recovery = 1;
1034 export->exp_req_replay_needed = 1;
1035 export->exp_lock_replay_needed = 1;
1036 cfs_spin_unlock(&export->exp_lock);
1037 if ((lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_TRANSNO)
1038 && (data->ocd_transno == 0))
1039 CWARN("Connect with zero transno!\n");
1041 if ((lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_TRANSNO)
1042 && data->ocd_transno < target->obd_next_recovery_transno &&
1043 data->ocd_transno > target->obd_last_committed)
1044 target->obd_next_recovery_transno = data->ocd_transno;
1045 target->obd_connected_clients++;
1046 cfs_atomic_inc(&target->obd_req_replay_clients);
1047 cfs_atomic_inc(&target->obd_lock_replay_clients);
1048 if (target->obd_connected_clients ==
1049 target->obd_max_recoverable_clients)
1050 cfs_waitq_signal(&target->obd_next_transno_waitq);
1052 cfs_spin_unlock(&target->obd_recovery_task_lock);
1054 /* Tell the client we're in recovery, when client is involved in it. */
1055 if (target->obd_recovering)
1056 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
1058 tmp = req_capsule_client_get(&req->rq_pill, &RMF_CONN);
1061 if (export->exp_imp_reverse != NULL) {
1062 /* destroyed import can be still referenced in ctxt */
1063 obd_set_info_async(export, sizeof(KEY_REVIMP_UPD),
1064 KEY_REVIMP_UPD, 0, NULL, NULL);
1066 client_destroy_import(export->exp_imp_reverse);
1069 /* for the rest part, we return -ENOTCONN in case of errors
1070 * in order to let client initialize connection again.
1072 revimp = export->exp_imp_reverse = class_new_import(target);
1074 CERROR("fail to alloc new reverse import.\n");
1075 GOTO(out, rc = -ENOTCONN);
1078 revimp->imp_connection = ptlrpc_connection_addref(export->exp_connection);
1079 revimp->imp_client = &export->exp_obd->obd_ldlm_client;
1080 revimp->imp_remote_handle = conn;
1081 revimp->imp_dlm_fake = 1;
1082 revimp->imp_state = LUSTRE_IMP_FULL;
1084 /* unknown versions will be caught in
1085 * ptlrpc_handle_server_req_in->lustre_unpack_msg() */
1086 revimp->imp_msg_magic = req->rq_reqmsg->lm_magic;
1088 if ((export->exp_connect_flags & OBD_CONNECT_AT) &&
1089 (revimp->imp_msg_magic != LUSTRE_MSG_MAGIC_V1))
1090 revimp->imp_msghdr_flags |= MSGHDR_AT_SUPPORT;
1092 revimp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
1094 if ((export->exp_connect_flags & OBD_CONNECT_FULL20) &&
1095 (revimp->imp_msg_magic != LUSTRE_MSG_MAGIC_V1))
1096 revimp->imp_msghdr_flags |= MSGHDR_CKSUM_INCOMPAT18;
1098 revimp->imp_msghdr_flags &= ~MSGHDR_CKSUM_INCOMPAT18;
1100 rc = sptlrpc_import_sec_adapt(revimp, req->rq_svc_ctx, &req->rq_flvr);
1102 CERROR("Failed to get sec for reverse import: %d\n", rc);
1103 export->exp_imp_reverse = NULL;
1104 class_destroy_import(revimp);
1107 class_import_put(revimp);
1110 cfs_spin_lock(&export->exp_lock);
1111 export->exp_connecting = 0;
1112 cfs_spin_unlock(&export->exp_lock);
1115 class_decref(targref, __FUNCTION__, cfs_current());
1117 req->rq_status = rc;
1121 int target_handle_disconnect(struct ptlrpc_request *req)
1126 rc = req_capsule_server_pack(&req->rq_pill);
1130 /* keep the rq_export around so we can send the reply */
1131 req->rq_status = obd_disconnect(class_export_get(req->rq_export));
1136 void target_destroy_export(struct obd_export *exp)
1138 /* exports created from last_rcvd data, and "fake"
1139 exports created by lctl don't have an import */
1140 if (exp->exp_imp_reverse != NULL)
1141 client_destroy_import(exp->exp_imp_reverse);
1143 LASSERT_ATOMIC_ZERO(&exp->exp_locks_count);
1144 LASSERT_ATOMIC_ZERO(&exp->exp_rpc_count);
1145 LASSERT_ATOMIC_ZERO(&exp->exp_cb_count);
1146 LASSERT_ATOMIC_ZERO(&exp->exp_replay_count);
1150 * Recovery functions
1152 static void target_request_copy_get(struct ptlrpc_request *req)
1154 class_export_rpc_get(req->rq_export);
1155 LASSERT(cfs_list_empty(&req->rq_list));
1156 CFS_INIT_LIST_HEAD(&req->rq_replay_list);
1158 /* increase refcount to keep request in queue */
1159 cfs_atomic_inc(&req->rq_refcount);
1160 /** let export know it has replays to be handled */
1161 cfs_atomic_inc(&req->rq_export->exp_replay_count);
1164 static void target_request_copy_put(struct ptlrpc_request *req)
1166 LASSERT(cfs_list_empty(&req->rq_replay_list));
1167 LASSERT_ATOMIC_POS(&req->rq_export->exp_replay_count);
1169 cfs_atomic_dec(&req->rq_export->exp_replay_count);
1170 class_export_rpc_put(req->rq_export);
1171 ptlrpc_server_drop_request(req);
1174 static int target_exp_enqueue_req_replay(struct ptlrpc_request *req)
1176 __u64 transno = lustre_msg_get_transno(req->rq_reqmsg);
1177 struct obd_export *exp = req->rq_export;
1178 struct ptlrpc_request *reqiter;
1183 cfs_spin_lock(&exp->exp_lock);
1184 cfs_list_for_each_entry(reqiter, &exp->exp_req_replay_queue,
1186 if (lustre_msg_get_transno(reqiter->rq_reqmsg) == transno) {
1193 /* we expect it with RESENT and REPLAY flags */
1194 if ((lustre_msg_get_flags(req->rq_reqmsg) &
1195 (MSG_RESENT | MSG_REPLAY)) != (MSG_RESENT | MSG_REPLAY))
1196 CERROR("invalid flags %x of resent replay\n",
1197 lustre_msg_get_flags(req->rq_reqmsg));
1199 cfs_list_add_tail(&req->rq_replay_list,
1200 &exp->exp_req_replay_queue);
1203 cfs_spin_unlock(&exp->exp_lock);
1207 static void target_exp_dequeue_req_replay(struct ptlrpc_request *req)
1209 LASSERT(!cfs_list_empty(&req->rq_replay_list));
1210 LASSERT(req->rq_export);
1212 cfs_spin_lock(&req->rq_export->exp_lock);
1213 cfs_list_del_init(&req->rq_replay_list);
1214 cfs_spin_unlock(&req->rq_export->exp_lock);
1218 static void target_finish_recovery(struct obd_device *obd)
1221 LCONSOLE_INFO("%s: sending delayed replies to recovered clients\n",
1224 ldlm_reprocess_all_ns(obd->obd_namespace);
1225 cfs_spin_lock(&obd->obd_recovery_task_lock);
1226 if (!cfs_list_empty(&obd->obd_req_replay_queue) ||
1227 !cfs_list_empty(&obd->obd_lock_replay_queue) ||
1228 !cfs_list_empty(&obd->obd_final_req_queue)) {
1229 CERROR("%s: Recovery queues ( %s%s%s) are not empty\n",
1231 cfs_list_empty(&obd->obd_req_replay_queue) ? "" : "req ",
1232 cfs_list_empty(&obd->obd_lock_replay_queue) ? \
1234 cfs_list_empty(&obd->obd_final_req_queue) ? \
1236 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1239 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1241 obd->obd_recovery_end = cfs_time_current_sec();
1243 /* when recovery finished, cleanup orphans on mds and ost */
1244 if (OBT(obd) && OBP(obd, postrecov)) {
1245 int rc = OBP(obd, postrecov)(obd);
1247 LCONSOLE_WARN("%s: Post recovery failed, rc %d\n",
1253 static void abort_req_replay_queue(struct obd_device *obd)
1255 struct ptlrpc_request *req, *n;
1256 cfs_list_t abort_list;
1258 CFS_INIT_LIST_HEAD(&abort_list);
1259 cfs_spin_lock(&obd->obd_recovery_task_lock);
1260 cfs_list_splice_init(&obd->obd_req_replay_queue, &abort_list);
1261 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1262 cfs_list_for_each_entry_safe(req, n, &abort_list, rq_list) {
1263 DEBUG_REQ(D_WARNING, req, "aborted:");
1264 req->rq_status = -ENOTCONN;
1265 if (ptlrpc_error(req)) {
1266 DEBUG_REQ(D_ERROR, req,
1267 "failed abort_req_reply; skipping");
1269 target_exp_dequeue_req_replay(req);
1270 target_request_copy_put(req);
1274 static void abort_lock_replay_queue(struct obd_device *obd)
1276 struct ptlrpc_request *req, *n;
1277 cfs_list_t abort_list;
1279 CFS_INIT_LIST_HEAD(&abort_list);
1280 cfs_spin_lock(&obd->obd_recovery_task_lock);
1281 cfs_list_splice_init(&obd->obd_lock_replay_queue, &abort_list);
1282 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1283 cfs_list_for_each_entry_safe(req, n, &abort_list, rq_list){
1284 DEBUG_REQ(D_ERROR, req, "aborted:");
1285 req->rq_status = -ENOTCONN;
1286 if (ptlrpc_error(req)) {
1287 DEBUG_REQ(D_ERROR, req,
1288 "failed abort_lock_reply; skipping");
1290 target_request_copy_put(req);
1295 /* Called from a cleanup function if the device is being cleaned up
1296 forcefully. The exports should all have been disconnected already,
1297 the only thing left to do is
1298 - clear the recovery flags
1300 - free queued requests and replies, but don't send replies
1301 Because the obd_stopping flag is set, no new requests should be received.
1304 void target_cleanup_recovery(struct obd_device *obd)
1306 struct ptlrpc_request *req, *n;
1307 cfs_list_t clean_list;
1310 CFS_INIT_LIST_HEAD(&clean_list);
1311 cfs_spin_lock(&obd->obd_dev_lock);
1312 if (!obd->obd_recovering) {
1313 cfs_spin_unlock(&obd->obd_dev_lock);
1317 obd->obd_recovering = obd->obd_abort_recovery = 0;
1318 cfs_spin_unlock(&obd->obd_dev_lock);
1320 cfs_spin_lock(&obd->obd_recovery_task_lock);
1321 target_cancel_recovery_timer(obd);
1322 cfs_list_splice_init(&obd->obd_req_replay_queue, &clean_list);
1323 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1325 cfs_list_for_each_entry_safe(req, n, &clean_list, rq_list) {
1326 LASSERT(req->rq_reply_state == 0);
1327 target_exp_dequeue_req_replay(req);
1328 target_request_copy_put(req);
1331 cfs_spin_lock(&obd->obd_recovery_task_lock);
1332 cfs_list_splice_init(&obd->obd_lock_replay_queue, &clean_list);
1333 cfs_list_splice_init(&obd->obd_final_req_queue, &clean_list);
1334 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1336 cfs_list_for_each_entry_safe(req, n, &clean_list, rq_list){
1337 LASSERT(req->rq_reply_state == 0);
1338 target_request_copy_put(req);
1344 /* obd_recovery_task_lock should be held */
1345 void target_cancel_recovery_timer(struct obd_device *obd)
1347 CDEBUG(D_HA, "%s: cancel recovery timer\n", obd->obd_name);
1348 cfs_timer_disarm(&obd->obd_recovery_timer);
1351 /* extend = 1 means require at least "duration" seconds left in the timer,
1352 extend = 0 means set the total duration (start_recovery_timer) */
1353 static void reset_recovery_timer(struct obd_device *obd, int duration,
1356 cfs_time_t now = cfs_time_current_sec();
1357 cfs_duration_t left;
1359 cfs_spin_lock(&obd->obd_recovery_task_lock);
1360 if (!obd->obd_recovering || obd->obd_abort_recovery) {
1361 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1365 left = cfs_time_sub(obd->obd_recovery_end, now);
1367 if (extend && (duration > left))
1368 obd->obd_recovery_timeout += duration - left;
1369 else if (!extend && (duration > obd->obd_recovery_timeout))
1370 /* Track the client's largest expected replay time */
1371 obd->obd_recovery_timeout = duration;
1373 /* Hard limit of obd_recovery_time_hard which should not happen */
1374 if (obd->obd_recovery_timeout > obd->obd_recovery_time_hard)
1375 obd->obd_recovery_timeout = obd->obd_recovery_time_hard;
1377 obd->obd_recovery_end = obd->obd_recovery_start +
1378 obd->obd_recovery_timeout;
1379 if (!cfs_timer_is_armed(&obd->obd_recovery_timer) ||
1380 cfs_time_before(now, obd->obd_recovery_end)) {
1381 left = cfs_time_sub(obd->obd_recovery_end, now);
1382 cfs_timer_arm(&obd->obd_recovery_timer, cfs_time_shift(left));
1384 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1385 CDEBUG(D_HA, "%s: recovery timer will expire in %u seconds\n",
1386 obd->obd_name, (unsigned)left);
1389 static void check_and_start_recovery_timer(struct obd_device *obd)
1391 cfs_spin_lock(&obd->obd_recovery_task_lock);
1392 if (cfs_timer_is_armed(&obd->obd_recovery_timer)) {
1393 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1396 CDEBUG(D_HA, "%s: starting recovery timer\n", obd->obd_name);
1397 obd->obd_recovery_start = cfs_time_current_sec();
1398 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1400 reset_recovery_timer(obd, obd->obd_recovery_timeout, 0);
1403 /* Reset the timer with each new client connection */
1405 * This timer is actually reconnect_timer, which is for making sure
1406 * the total recovery window is at least as big as my reconnect
1407 * attempt timing. So the initial recovery time_out will be set to
1408 * OBD_RECOVERY_FACTOR * obd_timeout. If the timeout coming
1409 * from client is bigger than this, then the recovery time_out will
1410 * be extended to make sure the client could be reconnected, in the
1411 * process, the timeout from the new client should be ignored.
1415 target_start_and_reset_recovery_timer(struct obd_device *obd,
1416 struct ptlrpc_request *req,
1419 int service_time = lustre_msg_get_service_time(req->rq_reqmsg);
1421 if (!new_client && service_time)
1422 /* Teach server about old server's estimates, as first guess
1423 * at how long new requests will take. */
1424 at_measured(&req->rq_rqbd->rqbd_service->srv_at_estimate,
1427 check_and_start_recovery_timer(obd);
1429 /* convert the service time to rpc timeout,
1430 * reuse service_time to limit stack usage */
1431 service_time = at_est2timeout(service_time);
1433 /* We expect other clients to timeout within service_time, then try
1434 * to reconnect, then try the failover server. The max delay between
1435 * connect attempts is SWITCH_MAX + SWITCH_INC + INITIAL */
1436 service_time += 2 * (CONNECTION_SWITCH_MAX + CONNECTION_SWITCH_INC +
1437 INITIAL_CONNECT_TIMEOUT);
1438 if (service_time > obd->obd_recovery_timeout && !new_client)
1439 reset_recovery_timer(obd, service_time, 0);
1444 /** Health checking routines */
1445 static inline int exp_connect_healthy(struct obd_export *exp)
1447 return (exp->exp_in_recovery);
1450 /** if export done req_replay or has replay in queue */
1451 static inline int exp_req_replay_healthy(struct obd_export *exp)
1453 return (!exp->exp_req_replay_needed ||
1454 cfs_atomic_read(&exp->exp_replay_count) > 0);
1456 /** if export done lock_replay or has replay in queue */
1457 static inline int exp_lock_replay_healthy(struct obd_export *exp)
1459 return (!exp->exp_lock_replay_needed ||
1460 cfs_atomic_read(&exp->exp_replay_count) > 0);
1463 static inline int exp_vbr_healthy(struct obd_export *exp)
1465 return (!exp->exp_vbr_failed);
1468 static inline int exp_finished(struct obd_export *exp)
1470 return (exp->exp_in_recovery && !exp->exp_lock_replay_needed);
1473 /** Checking routines for recovery */
1474 static int check_for_clients(struct obd_device *obd)
1476 if (obd->obd_abort_recovery || obd->obd_recovery_expired)
1478 LASSERT(obd->obd_connected_clients <= obd->obd_max_recoverable_clients);
1479 if (obd->obd_no_conn == 0 &&
1480 obd->obd_connected_clients + obd->obd_stale_clients ==
1481 obd->obd_max_recoverable_clients)
1486 static int check_for_next_transno(struct obd_device *obd)
1488 struct ptlrpc_request *req = NULL;
1489 int wake_up = 0, connected, completed, queue_len;
1490 __u64 next_transno, req_transno;
1493 cfs_spin_lock(&obd->obd_recovery_task_lock);
1494 if (!cfs_list_empty(&obd->obd_req_replay_queue)) {
1495 req = cfs_list_entry(obd->obd_req_replay_queue.next,
1496 struct ptlrpc_request, rq_list);
1497 req_transno = lustre_msg_get_transno(req->rq_reqmsg);
1502 connected = obd->obd_connected_clients;
1503 completed = connected - cfs_atomic_read(&obd->obd_req_replay_clients);
1504 queue_len = obd->obd_requests_queued_for_recovery;
1505 next_transno = obd->obd_next_recovery_transno;
1507 CDEBUG(D_HA, "max: %d, connected: %d, completed: %d, queue_len: %d, "
1508 "req_transno: "LPU64", next_transno: "LPU64"\n",
1509 obd->obd_max_recoverable_clients, connected, completed,
1510 queue_len, req_transno, next_transno);
1512 if (obd->obd_abort_recovery) {
1513 CDEBUG(D_HA, "waking for aborted recovery\n");
1515 } else if (obd->obd_recovery_expired) {
1516 CDEBUG(D_HA, "waking for expired recovery\n");
1518 } else if (cfs_atomic_read(&obd->obd_req_replay_clients) == 0) {
1519 CDEBUG(D_HA, "waking for completed recovery\n");
1521 } else if (req_transno == next_transno) {
1522 CDEBUG(D_HA, "waking for next ("LPD64")\n", next_transno);
1524 } else if (queue_len == cfs_atomic_read(&obd->obd_req_replay_clients)) {
1526 /** handle gaps occured due to lost reply or VBR */
1527 LASSERTF(req_transno >= next_transno,
1528 "req_transno: "LPU64", next_transno: "LPU64"\n",
1529 req_transno, next_transno);
1530 if (req_transno > obd->obd_last_committed &&
1531 !obd->obd_version_recov)
1534 "%s: waking for gap in transno, VBR is %s (skip: "
1535 LPD64", ql: %d, comp: %d, conn: %d, next: "LPD64
1536 ", last_committed: "LPD64")\n",
1537 obd->obd_name, obd->obd_version_recov ? "ON" : "OFF",
1538 next_transno, queue_len, completed, connected,
1539 req_transno, obd->obd_last_committed);
1540 obd->obd_next_recovery_transno = req_transno;
1542 } else if (OBD_FAIL_CHECK(OBD_FAIL_MDS_RECOVERY_ACCEPTS_GAPS)) {
1543 CDEBUG(D_HA, "accepting transno gaps is explicitly allowed"
1544 " by fail_lock, waking up ("LPD64")\n", next_transno);
1545 obd->obd_next_recovery_transno = req_transno;
1548 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1552 static int check_for_next_lock(struct obd_device *obd)
1556 cfs_spin_lock(&obd->obd_recovery_task_lock);
1557 if (!cfs_list_empty(&obd->obd_lock_replay_queue)) {
1558 CDEBUG(D_HA, "waking for next lock\n");
1560 } else if (cfs_atomic_read(&obd->obd_lock_replay_clients) == 0) {
1561 CDEBUG(D_HA, "waking for completed lock replay\n");
1563 } else if (obd->obd_abort_recovery) {
1564 CDEBUG(D_HA, "waking for aborted recovery\n");
1566 } else if (obd->obd_recovery_expired) {
1567 CDEBUG(D_HA, "waking for expired recovery\n");
1570 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1576 * wait for recovery events,
1577 * check its status with help of check_routine
1578 * evict dead clients via health_check
1580 static int target_recovery_overseer(struct obd_device *obd,
1581 int (*check_routine)(struct obd_device *),
1582 int (*health_check)(struct obd_export *))
1585 cfs_wait_event(obd->obd_next_transno_waitq, check_routine(obd));
1586 if (obd->obd_abort_recovery) {
1587 CWARN("recovery is aborted, evict exports in recovery\n");
1588 /** evict exports which didn't finish recovery yet */
1589 class_disconnect_stale_exports(obd, exp_finished);
1591 } else if (obd->obd_recovery_expired) {
1592 obd->obd_recovery_expired = 0;
1593 /** If some clients died being recovered, evict them */
1595 "recovery is timed out, evict stale exports\n");
1596 /** evict cexports with no replay in queue, they are stalled */
1597 class_disconnect_stale_exports(obd, health_check);
1598 /** continue with VBR */
1599 cfs_spin_lock(&obd->obd_dev_lock);
1600 obd->obd_version_recov = 1;
1601 cfs_spin_unlock(&obd->obd_dev_lock);
1603 * reset timer, recovery will proceed with versions now,
1604 * timeout is set just to handle reconnection delays
1606 reset_recovery_timer(obd, RECONNECT_DELAY_MAX, 1);
1607 /** Wait for recovery events again, after evicting bad clients */
1613 static struct ptlrpc_request *target_next_replay_req(struct obd_device *obd)
1615 struct ptlrpc_request *req = NULL;
1618 CDEBUG(D_HA, "Waiting for transno "LPD64"\n",
1619 obd->obd_next_recovery_transno);
1621 if (target_recovery_overseer(obd, check_for_next_transno,
1622 exp_req_replay_healthy)) {
1623 abort_req_replay_queue(obd);
1624 abort_lock_replay_queue(obd);
1627 cfs_spin_lock(&obd->obd_recovery_task_lock);
1628 if (!cfs_list_empty(&obd->obd_req_replay_queue)) {
1629 req = cfs_list_entry(obd->obd_req_replay_queue.next,
1630 struct ptlrpc_request, rq_list);
1631 cfs_list_del_init(&req->rq_list);
1632 obd->obd_requests_queued_for_recovery--;
1633 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1635 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1636 LASSERT(cfs_list_empty(&obd->obd_req_replay_queue));
1637 LASSERT(cfs_atomic_read(&obd->obd_req_replay_clients) == 0);
1638 /** evict exports failed VBR */
1639 class_disconnect_stale_exports(obd, exp_vbr_healthy);
1644 static struct ptlrpc_request *target_next_replay_lock(struct obd_device *obd)
1646 struct ptlrpc_request *req = NULL;
1648 CDEBUG(D_HA, "Waiting for lock\n");
1649 if (target_recovery_overseer(obd, check_for_next_lock,
1650 exp_lock_replay_healthy))
1651 abort_lock_replay_queue(obd);
1653 cfs_spin_lock(&obd->obd_recovery_task_lock);
1654 if (!cfs_list_empty(&obd->obd_lock_replay_queue)) {
1655 req = cfs_list_entry(obd->obd_lock_replay_queue.next,
1656 struct ptlrpc_request, rq_list);
1657 cfs_list_del_init(&req->rq_list);
1658 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1660 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1661 LASSERT(cfs_list_empty(&obd->obd_lock_replay_queue));
1662 LASSERT(cfs_atomic_read(&obd->obd_lock_replay_clients) == 0);
1663 /** evict exports failed VBR */
1664 class_disconnect_stale_exports(obd, exp_vbr_healthy);
1669 static struct ptlrpc_request *target_next_final_ping(struct obd_device *obd)
1671 struct ptlrpc_request *req = NULL;
1673 cfs_spin_lock(&obd->obd_recovery_task_lock);
1674 if (!cfs_list_empty(&obd->obd_final_req_queue)) {
1675 req = cfs_list_entry(obd->obd_final_req_queue.next,
1676 struct ptlrpc_request, rq_list);
1677 cfs_list_del_init(&req->rq_list);
1678 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1679 if (req->rq_export->exp_in_recovery) {
1680 cfs_spin_lock(&req->rq_export->exp_lock);
1681 req->rq_export->exp_in_recovery = 0;
1682 cfs_spin_unlock(&req->rq_export->exp_lock);
1685 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1690 static int handle_recovery_req(struct ptlrpc_thread *thread,
1691 struct ptlrpc_request *req,
1692 svc_handler_t handler)
1698 * export can be evicted during recovery, no need to handle replays for
1699 * it after that, discard such request silently
1701 if (req->rq_export->exp_disconnected)
1702 GOTO(reqcopy_put, rc = 0);
1704 rc = lu_context_init(&req->rq_recov_session, LCT_SESSION);
1706 CERROR("Failure to initialize session: %d\n", rc);
1707 GOTO(reqcopy_put, rc);
1710 req->rq_recov_session.lc_thread = thread;
1711 lu_context_enter(&req->rq_recov_session);
1712 req->rq_svc_thread = thread;
1713 req->rq_svc_thread->t_env->le_ses = &req->rq_recov_session;
1715 /* thread context */
1716 lu_context_enter(&thread->t_env->le_ctx);
1718 lu_context_exit(&thread->t_env->le_ctx);
1720 lu_context_exit(&req->rq_recov_session);
1721 lu_context_fini(&req->rq_recov_session);
1722 /* don't reset timer for final stage */
1723 if (!exp_finished(req->rq_export)) {
1725 * Add request timeout to the recovery time so next request from
1726 * this client may come in recovery time
1728 reset_recovery_timer(class_exp2obd(req->rq_export),
1729 AT_OFF ? obd_timeout :
1730 lustre_msg_get_timeout(req->rq_reqmsg), 1);
1736 static int target_recovery_thread(void *arg)
1738 struct lu_target *lut = arg;
1739 struct obd_device *obd = lut->lut_obd;
1740 struct ptlrpc_request *req;
1741 struct target_recovery_data *trd = &obd->obd_recovery_data;
1742 unsigned long delta;
1743 unsigned long flags;
1745 struct ptlrpc_thread fake_svc_thread, *thread = &fake_svc_thread;
1749 cfs_daemonize_ctxt("tgt_recov");
1751 SIGNAL_MASK_LOCK(current, flags);
1752 sigfillset(¤t->blocked);
1754 SIGNAL_MASK_UNLOCK(current, flags);
1756 rc = lu_context_init(&env.le_ctx, LCT_MD_THREAD);
1760 thread->t_env = &env;
1761 thread->t_id = -1; /* force filter_iobuf_get/put to use local buffers */
1762 env.le_ctx.lc_thread = thread;
1763 thread->t_data = NULL;
1765 CERROR("%s: started recovery thread pid %d\n", obd->obd_name,
1767 trd->trd_processing_task = cfs_curproc_pid();
1769 cfs_spin_lock(&obd->obd_dev_lock);
1770 obd->obd_recovering = 1;
1771 cfs_spin_unlock(&obd->obd_dev_lock);
1772 cfs_complete(&trd->trd_starting);
1774 /* first of all, we have to know the first transno to replay */
1775 if (target_recovery_overseer(obd, check_for_clients,
1776 exp_connect_healthy)) {
1777 abort_req_replay_queue(obd);
1778 abort_lock_replay_queue(obd);
1781 /* next stage: replay requests */
1783 CDEBUG(D_INFO, "1: request replay stage - %d clients from t"LPU64"\n",
1784 cfs_atomic_read(&obd->obd_req_replay_clients),
1785 obd->obd_next_recovery_transno);
1786 while ((req = target_next_replay_req(obd))) {
1787 LASSERT(trd->trd_processing_task == cfs_curproc_pid());
1788 DEBUG_REQ(D_HA, req, "processing t"LPD64" from %s",
1789 lustre_msg_get_transno(req->rq_reqmsg),
1790 libcfs_nid2str(req->rq_peer.nid));
1791 handle_recovery_req(thread, req,
1792 trd->trd_recovery_handler);
1794 * bz18031: increase next_recovery_transno before
1795 * target_request_copy_put() will drop exp_rpc reference
1797 cfs_spin_lock(&obd->obd_recovery_task_lock);
1798 obd->obd_next_recovery_transno++;
1799 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1800 target_exp_dequeue_req_replay(req);
1801 target_request_copy_put(req);
1802 obd->obd_replayed_requests++;
1806 * The second stage: replay locks
1808 CDEBUG(D_INFO, "2: lock replay stage - %d clients\n",
1809 cfs_atomic_read(&obd->obd_lock_replay_clients));
1810 while ((req = target_next_replay_lock(obd))) {
1811 LASSERT(trd->trd_processing_task == cfs_curproc_pid());
1812 DEBUG_REQ(D_HA, req, "processing lock from %s: ",
1813 libcfs_nid2str(req->rq_peer.nid));
1814 handle_recovery_req(thread, req,
1815 trd->trd_recovery_handler);
1816 target_request_copy_put(req);
1817 obd->obd_replayed_locks++;
1821 * The third stage: reply on final pings, at this moment all clients
1822 * must have request in final queue
1824 CDEBUG(D_INFO, "3: final stage - process recovery completion pings\n");
1825 /** Update server last boot epoch */
1826 lut_boot_epoch_update(lut);
1827 /* We drop recoverying flag to forward all new requests
1828 * to regular mds_handle() since now */
1829 cfs_spin_lock(&obd->obd_dev_lock);
1830 obd->obd_recovering = obd->obd_abort_recovery = 0;
1831 cfs_spin_unlock(&obd->obd_dev_lock);
1832 cfs_spin_lock(&obd->obd_recovery_task_lock);
1833 target_cancel_recovery_timer(obd);
1834 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1835 while ((req = target_next_final_ping(obd))) {
1836 LASSERT(trd->trd_processing_task == cfs_curproc_pid());
1837 DEBUG_REQ(D_HA, req, "processing final ping from %s: ",
1838 libcfs_nid2str(req->rq_peer.nid));
1839 handle_recovery_req(thread, req,
1840 trd->trd_recovery_handler);
1841 target_request_copy_put(req);
1844 delta = (jiffies - delta) / CFS_HZ;
1845 CDEBUG(D_INFO,"4: recovery completed in %lus - %d/%d reqs/locks\n",
1846 delta, obd->obd_replayed_requests, obd->obd_replayed_locks);
1847 if (delta > OBD_RECOVERY_TIME_SOFT) {
1848 CWARN("too long recovery - read logs\n");
1849 libcfs_debug_dumplog();
1852 target_finish_recovery(obd);
1854 lu_context_fini(&env.le_ctx);
1855 trd->trd_processing_task = 0;
1856 cfs_complete(&trd->trd_finishing);
1860 static int target_start_recovery_thread(struct lu_target *lut,
1861 svc_handler_t handler)
1863 struct obd_device *obd = lut->lut_obd;
1865 struct target_recovery_data *trd = &obd->obd_recovery_data;
1867 memset(trd, 0, sizeof(*trd));
1868 cfs_init_completion(&trd->trd_starting);
1869 cfs_init_completion(&trd->trd_finishing);
1870 trd->trd_recovery_handler = handler;
1872 if (cfs_create_thread(target_recovery_thread, lut, 0) > 0) {
1873 cfs_wait_for_completion(&trd->trd_starting);
1874 LASSERT(obd->obd_recovering != 0);
1881 void target_stop_recovery_thread(struct obd_device *obd)
1883 if (obd->obd_recovery_data.trd_processing_task > 0) {
1884 struct target_recovery_data *trd = &obd->obd_recovery_data;
1885 /** recovery can be done but postrecovery is not yet */
1886 cfs_spin_lock(&obd->obd_dev_lock);
1887 if (obd->obd_recovering) {
1888 CERROR("%s: Aborting recovery\n", obd->obd_name);
1889 obd->obd_abort_recovery = 1;
1890 cfs_waitq_signal(&obd->obd_next_transno_waitq);
1892 cfs_spin_unlock(&obd->obd_dev_lock);
1893 cfs_wait_for_completion(&trd->trd_finishing);
1897 void target_recovery_fini(struct obd_device *obd)
1899 class_disconnect_exports(obd);
1900 target_stop_recovery_thread(obd);
1901 target_cleanup_recovery(obd);
1903 EXPORT_SYMBOL(target_recovery_fini);
1905 static void target_recovery_expired(unsigned long castmeharder)
1907 struct obd_device *obd = (struct obd_device *)castmeharder;
1908 CDEBUG(D_HA, "%s: recovery timed out; %d clients are still in recovery"
1909 " after %lds (%d clients connected)\n",
1910 obd->obd_name, cfs_atomic_read(&obd->obd_lock_replay_clients),
1911 cfs_time_current_sec()- obd->obd_recovery_start,
1912 obd->obd_connected_clients);
1914 obd->obd_recovery_expired = 1;
1915 cfs_waitq_signal(&obd->obd_next_transno_waitq);
1918 void target_recovery_init(struct lu_target *lut, svc_handler_t handler)
1920 struct obd_device *obd = lut->lut_obd;
1921 if (obd->obd_max_recoverable_clients == 0) {
1922 /** Update server last boot epoch */
1923 lut_boot_epoch_update(lut);
1927 CWARN("RECOVERY: service %s, %d recoverable clients, "
1928 "last_transno "LPU64"\n", obd->obd_name,
1929 obd->obd_max_recoverable_clients, obd->obd_last_committed);
1930 LASSERT(obd->obd_stopping == 0);
1931 obd->obd_next_recovery_transno = obd->obd_last_committed + 1;
1932 obd->obd_recovery_start = 0;
1933 obd->obd_recovery_end = 0;
1935 /* both values can be get from mount data already */
1936 if (obd->obd_recovery_timeout == 0)
1937 obd->obd_recovery_timeout = OBD_RECOVERY_TIME_SOFT;
1938 if (obd->obd_recovery_time_hard == 0)
1939 obd->obd_recovery_time_hard = OBD_RECOVERY_TIME_HARD;
1940 cfs_timer_init(&obd->obd_recovery_timer, target_recovery_expired, obd);
1941 target_start_recovery_thread(lut, handler);
1943 EXPORT_SYMBOL(target_recovery_init);
1947 static int target_process_req_flags(struct obd_device *obd,
1948 struct ptlrpc_request *req)
1950 struct obd_export *exp = req->rq_export;
1951 LASSERT(exp != NULL);
1952 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REQ_REPLAY_DONE) {
1953 /* client declares he's ready to replay locks */
1954 cfs_spin_lock(&exp->exp_lock);
1955 if (exp->exp_req_replay_needed) {
1956 exp->exp_req_replay_needed = 0;
1957 cfs_spin_unlock(&exp->exp_lock);
1959 LASSERT_ATOMIC_POS(&obd->obd_req_replay_clients);
1960 cfs_atomic_dec(&obd->obd_req_replay_clients);
1962 cfs_spin_unlock(&exp->exp_lock);
1965 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_LOCK_REPLAY_DONE) {
1966 /* client declares he's ready to complete recovery
1967 * so, we put the request on th final queue */
1968 cfs_spin_lock(&exp->exp_lock);
1969 if (exp->exp_lock_replay_needed) {
1970 exp->exp_lock_replay_needed = 0;
1971 cfs_spin_unlock(&exp->exp_lock);
1973 LASSERT_ATOMIC_POS(&obd->obd_lock_replay_clients);
1974 cfs_atomic_dec(&obd->obd_lock_replay_clients);
1976 cfs_spin_unlock(&exp->exp_lock);
1982 int target_queue_recovery_request(struct ptlrpc_request *req,
1983 struct obd_device *obd)
1987 __u64 transno = lustre_msg_get_transno(req->rq_reqmsg);
1990 if (obd->obd_recovery_data.trd_processing_task == cfs_curproc_pid()) {
1991 /* Processing the queue right now, don't re-add. */
1995 target_process_req_flags(obd, req);
1997 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_LOCK_REPLAY_DONE) {
1998 /* client declares he's ready to complete recovery
1999 * so, we put the request on th final queue */
2000 target_request_copy_get(req);
2001 DEBUG_REQ(D_HA, req, "queue final req");
2002 cfs_waitq_signal(&obd->obd_next_transno_waitq);
2003 cfs_spin_lock(&obd->obd_recovery_task_lock);
2004 if (obd->obd_recovering) {
2005 cfs_list_add_tail(&req->rq_list,
2006 &obd->obd_final_req_queue);
2008 cfs_spin_unlock(&obd->obd_recovery_task_lock);
2009 target_request_copy_put(req);
2010 RETURN(obd->obd_stopping ? -ENOTCONN : 1);
2012 cfs_spin_unlock(&obd->obd_recovery_task_lock);
2015 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REQ_REPLAY_DONE) {
2016 /* client declares he's ready to replay locks */
2017 target_request_copy_get(req);
2018 DEBUG_REQ(D_HA, req, "queue lock replay req");
2019 cfs_waitq_signal(&obd->obd_next_transno_waitq);
2020 cfs_spin_lock(&obd->obd_recovery_task_lock);
2021 LASSERT(obd->obd_recovering);
2022 /* usually due to recovery abort */
2023 if (!req->rq_export->exp_in_recovery) {
2024 cfs_spin_unlock(&obd->obd_recovery_task_lock);
2025 target_request_copy_put(req);
2028 LASSERT(req->rq_export->exp_lock_replay_needed);
2029 cfs_list_add_tail(&req->rq_list, &obd->obd_lock_replay_queue);
2030 cfs_spin_unlock(&obd->obd_recovery_task_lock);
2034 /* CAVEAT EMPTOR: The incoming request message has been swabbed
2035 * (i.e. buflens etc are in my own byte order), but type-dependent
2036 * buffers (eg mds_body, ost_body etc) have NOT been swabbed. */
2039 CFS_INIT_LIST_HEAD(&req->rq_list);
2040 DEBUG_REQ(D_HA, req, "not queueing");
2044 /* If we're processing the queue, we want don't want to queue this
2047 * Also, if this request has a transno less than the one we're waiting
2048 * for, we should process it now. It could (and currently always will)
2049 * be an open request for a descriptor that was opened some time ago.
2051 * Also, a resent, replayed request that has already been
2052 * handled will pass through here and be processed immediately.
2054 CWARN("Next recovery transno: "LPU64", current: "LPU64", replaying\n",
2055 obd->obd_next_recovery_transno, transno);
2056 cfs_spin_lock(&obd->obd_recovery_task_lock);
2057 if (transno < obd->obd_next_recovery_transno) {
2058 /* Processing the queue right now, don't re-add. */
2059 LASSERT(cfs_list_empty(&req->rq_list));
2060 cfs_spin_unlock(&obd->obd_recovery_task_lock);
2063 cfs_spin_unlock(&obd->obd_recovery_task_lock);
2065 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_REPLAY_DROP))
2068 target_request_copy_get(req);
2069 if (!req->rq_export->exp_in_recovery) {
2070 target_request_copy_put(req);
2073 LASSERT(req->rq_export->exp_req_replay_needed);
2075 if (target_exp_enqueue_req_replay(req)) {
2076 DEBUG_REQ(D_ERROR, req, "dropping resent queued req");
2077 target_request_copy_put(req);
2082 cfs_spin_lock(&obd->obd_recovery_task_lock);
2083 LASSERT(obd->obd_recovering);
2084 cfs_list_for_each(tmp, &obd->obd_req_replay_queue) {
2085 struct ptlrpc_request *reqiter =
2086 cfs_list_entry(tmp, struct ptlrpc_request, rq_list);
2088 if (lustre_msg_get_transno(reqiter->rq_reqmsg) > transno) {
2089 cfs_list_add_tail(&req->rq_list, &reqiter->rq_list);
2094 if (unlikely(lustre_msg_get_transno(reqiter->rq_reqmsg) ==
2096 DEBUG_REQ(D_ERROR, req, "dropping replay: transno "
2097 "has been claimed by another client");
2098 cfs_spin_unlock(&obd->obd_recovery_task_lock);
2099 target_exp_dequeue_req_replay(req);
2100 target_request_copy_put(req);
2106 cfs_list_add_tail(&req->rq_list, &obd->obd_req_replay_queue);
2108 obd->obd_requests_queued_for_recovery++;
2109 cfs_spin_unlock(&obd->obd_recovery_task_lock);
2110 cfs_waitq_signal(&obd->obd_next_transno_waitq);
2115 * Packs current SLV and Limit into \a req.
2117 int target_pack_pool_reply(struct ptlrpc_request *req)
2119 struct obd_device *obd;
2123 * Check that we still have all structures alive as this may
2124 * be some late rpc in shutdown time.
2126 if (unlikely(!req->rq_export || !req->rq_export->exp_obd ||
2127 !exp_connect_lru_resize(req->rq_export))) {
2128 lustre_msg_set_slv(req->rq_repmsg, 0);
2129 lustre_msg_set_limit(req->rq_repmsg, 0);
2134 * OBD is alive here as export is alive, which we checked above.
2136 obd = req->rq_export->exp_obd;
2138 cfs_read_lock(&obd->obd_pool_lock);
2139 lustre_msg_set_slv(req->rq_repmsg, obd->obd_pool_slv);
2140 lustre_msg_set_limit(req->rq_repmsg, obd->obd_pool_limit);
2141 cfs_read_unlock(&obd->obd_pool_lock);
2146 int target_send_reply_msg(struct ptlrpc_request *req, int rc, int fail_id)
2148 if (OBD_FAIL_CHECK_ORSET(fail_id & ~OBD_FAIL_ONCE, OBD_FAIL_ONCE)) {
2149 DEBUG_REQ(D_ERROR, req, "dropping reply");
2154 DEBUG_REQ(D_ERROR, req, "processing error (%d)", rc);
2155 req->rq_status = rc;
2156 return (ptlrpc_send_error(req, 1));
2158 DEBUG_REQ(D_NET, req, "sending reply");
2161 return (ptlrpc_send_reply(req, PTLRPC_REPLY_MAYBE_DIFFICULT));
2164 void target_send_reply(struct ptlrpc_request *req, int rc, int fail_id)
2167 struct ptlrpc_reply_state *rs;
2168 struct obd_device *obd;
2169 struct obd_export *exp;
2170 struct ptlrpc_service *svc;
2173 if (req->rq_no_reply) {
2178 svc = req->rq_rqbd->rqbd_service;
2179 rs = req->rq_reply_state;
2180 if (rs == NULL || !rs->rs_difficult) {
2182 target_send_reply_msg (req, rc, fail_id);
2187 /* must be an export if locks saved */
2188 LASSERT (req->rq_export != NULL);
2189 /* req/reply consistent */
2190 LASSERT (rs->rs_service == svc);
2193 LASSERT (!rs->rs_scheduled);
2194 LASSERT (!rs->rs_scheduled_ever);
2195 LASSERT (!rs->rs_handled);
2196 LASSERT (!rs->rs_on_net);
2197 LASSERT (rs->rs_export == NULL);
2198 LASSERT (cfs_list_empty(&rs->rs_obd_list));
2199 LASSERT (cfs_list_empty(&rs->rs_exp_list));
2201 exp = class_export_get (req->rq_export);
2204 /* disable reply scheduling while I'm setting up */
2205 rs->rs_scheduled = 1;
2207 rs->rs_xid = req->rq_xid;
2208 rs->rs_transno = req->rq_transno;
2209 rs->rs_export = exp;
2210 rs->rs_opc = lustre_msg_get_opc(rs->rs_msg);
2212 cfs_spin_lock(&exp->exp_uncommitted_replies_lock);
2213 CDEBUG(D_NET, "rs transno = "LPU64", last committed = "LPU64"\n",
2214 rs->rs_transno, exp->exp_last_committed);
2215 if (rs->rs_transno > exp->exp_last_committed) {
2216 /* not committed already */
2217 cfs_list_add_tail(&rs->rs_obd_list,
2218 &exp->exp_uncommitted_replies);
2220 cfs_spin_unlock (&exp->exp_uncommitted_replies_lock);
2222 cfs_spin_lock(&exp->exp_lock);
2223 cfs_list_add_tail(&rs->rs_exp_list, &exp->exp_outstanding_replies);
2224 cfs_spin_unlock(&exp->exp_lock);
2226 netrc = target_send_reply_msg (req, rc, fail_id);
2228 cfs_spin_lock(&svc->srv_rs_lock);
2230 cfs_atomic_inc(&svc->srv_n_difficult_replies);
2233 /* error sending: reply is off the net. Also we need +1
2234 * reply ref until ptlrpc_handle_rs() is done
2235 * with the reply state (if the send was successful, there
2236 * would have been +1 ref for the net, which
2237 * reply_out_callback leaves alone) */
2239 ptlrpc_rs_addref(rs);
2242 cfs_spin_lock(&rs->rs_lock);
2243 if (rs->rs_transno <= exp->exp_last_committed ||
2244 (!rs->rs_on_net && !rs->rs_no_ack) ||
2245 cfs_list_empty(&rs->rs_exp_list) || /* completed already */
2246 cfs_list_empty(&rs->rs_obd_list)) {
2247 CDEBUG(D_HA, "Schedule reply immediately\n");
2248 ptlrpc_dispatch_difficult_reply(rs);
2250 cfs_list_add (&rs->rs_list, &svc->srv_active_replies);
2251 rs->rs_scheduled = 0; /* allow notifier to schedule */
2253 cfs_spin_unlock(&rs->rs_lock);
2254 cfs_spin_unlock(&svc->srv_rs_lock);
2258 int target_handle_ping(struct ptlrpc_request *req)
2260 obd_ping(req->rq_export);
2261 return req_capsule_server_pack(&req->rq_pill);
2264 void target_committed_to_req(struct ptlrpc_request *req)
2266 struct obd_export *exp = req->rq_export;
2268 if (!exp->exp_obd->obd_no_transno && req->rq_repmsg != NULL)
2269 lustre_msg_set_last_committed(req->rq_repmsg,
2270 exp->exp_last_committed);
2272 DEBUG_REQ(D_IOCTL, req, "not sending last_committed update (%d/"
2273 "%d)", exp->exp_obd->obd_no_transno,
2274 req->rq_repmsg == NULL);
2276 CDEBUG(D_INFO, "last_committed "LPU64", transno "LPU64", xid "LPU64"\n",
2277 exp->exp_last_committed, req->rq_transno, req->rq_xid);
2279 EXPORT_SYMBOL(target_committed_to_req);
2281 int target_handle_qc_callback(struct ptlrpc_request *req)
2283 struct obd_quotactl *oqctl;
2284 struct client_obd *cli = &req->rq_export->exp_obd->u.cli;
2286 oqctl = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
2287 if (oqctl == NULL) {
2288 CERROR("Can't unpack obd_quotactl\n");
2292 cli->cl_qchk_stat = oqctl->qc_stat;
2297 #ifdef HAVE_QUOTA_SUPPORT
2298 int target_handle_dqacq_callback(struct ptlrpc_request *req)
2301 struct obd_device *obd = req->rq_export->exp_obd;
2302 struct obd_device *master_obd = NULL, *lov_obd = NULL;
2303 struct obd_device_target *obt;
2304 struct lustre_quota_ctxt *qctxt;
2305 struct qunit_data *qdata = NULL;
2309 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DROP_QUOTA_REQ))
2312 rc = req_capsule_server_pack(&req->rq_pill);
2314 CERROR("packing reply failed!: rc = %d\n", rc);
2318 LASSERT(req->rq_export);
2320 qdata = quota_get_qdata(req, QUOTA_REQUEST, QUOTA_EXPORT);
2321 if (IS_ERR(qdata)) {
2322 rc = PTR_ERR(qdata);
2323 CDEBUG(D_ERROR, "Can't unpack qunit_data(rc: %d)\n", rc);
2324 req->rq_status = rc;
2328 /* we use the observer */
2329 if (obd_pin_observer(obd, &lov_obd) ||
2330 obd_pin_observer(lov_obd, &master_obd)) {
2331 CERROR("Can't find the observer, it is recovering\n");
2332 req->rq_status = -EAGAIN;
2336 obt = &master_obd->u.obt;
2337 qctxt = &obt->obt_qctxt;
2339 if (!qctxt->lqc_setup || !qctxt->lqc_valid) {
2340 /* quota_type has not been processed yet, return EAGAIN
2341 * until we know whether or not quotas are supposed to
2343 CDEBUG(D_QUOTA, "quota_type not processed yet, return "
2345 req->rq_status = -EAGAIN;
2349 cfs_down_read(&obt->obt_rwsem);
2350 if (qctxt->lqc_lqs_hash == NULL) {
2351 cfs_up_read(&obt->obt_rwsem);
2352 /* quota_type has not been processed yet, return EAGAIN
2353 * until we know whether or not quotas are supposed to
2355 CDEBUG(D_QUOTA, "quota_ctxt is not ready yet, return "
2357 req->rq_status = -EAGAIN;
2361 LASSERT(qctxt->lqc_handler);
2362 rc = qctxt->lqc_handler(master_obd, qdata,
2363 lustre_msg_get_opc(req->rq_reqmsg));
2364 cfs_up_read(&obt->obt_rwsem);
2365 if (rc && rc != -EDQUOT)
2366 CDEBUG(rc == -EBUSY ? D_QUOTA : D_ERROR,
2367 "dqacq/dqrel failed! (rc:%d)\n", rc);
2368 req->rq_status = rc;
2370 rc = quota_copy_qdata(req, qdata, QUOTA_REPLY, QUOTA_EXPORT);
2372 CERROR("Can't pack qunit_data(rc: %d)\n", rc);
2376 /* Block the quota req. b=14840 */
2377 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_BLOCK_QUOTA_REQ, obd_timeout);
2382 obd_unpin_observer(lov_obd);
2384 obd_unpin_observer(obd);
2386 rc = ptlrpc_reply(req);
2390 #endif /* !__KERNEL__ */
2392 #endif /* HAVE_QUOTA_SUPPORT */
2394 ldlm_mode_t lck_compat_array[] = {
2395 [LCK_EX] LCK_COMPAT_EX,
2396 [LCK_PW] LCK_COMPAT_PW,
2397 [LCK_PR] LCK_COMPAT_PR,
2398 [LCK_CW] LCK_COMPAT_CW,
2399 [LCK_CR] LCK_COMPAT_CR,
2400 [LCK_NL] LCK_COMPAT_NL,
2401 [LCK_GROUP] LCK_COMPAT_GROUP,
2402 [LCK_COS] LCK_COMPAT_COS,
2406 * Rather arbitrary mapping from LDLM error codes to errno values. This should
2407 * not escape to the user level.
2409 int ldlm_error2errno(ldlm_error_t error)
2417 case ELDLM_LOCK_CHANGED:
2420 case ELDLM_LOCK_ABORTED:
2423 case ELDLM_LOCK_REPLACED:
2426 case ELDLM_NO_LOCK_DATA:
2429 case ELDLM_NAMESPACE_EXISTS:
2432 case ELDLM_BAD_NAMESPACE:
2436 if (((int)error) < 0) /* cast to signed type */
2437 result = error; /* as ldlm_error_t can be unsigned */
2439 CERROR("Invalid DLM result code: %d\n", error);
2445 EXPORT_SYMBOL(ldlm_error2errno);
2448 * Dual to ldlm_error2errno(): maps errno values back to ldlm_error_t.
2450 ldlm_error_t ldlm_errno2error(int err_no)
2459 error = ELDLM_LOCK_CHANGED;
2462 error = ELDLM_LOCK_ABORTED;
2465 error = ELDLM_LOCK_REPLACED;
2468 error = ELDLM_NO_LOCK_DATA;
2471 error = ELDLM_NAMESPACE_EXISTS;
2474 error = ELDLM_BAD_NAMESPACE;
2481 EXPORT_SYMBOL(ldlm_errno2error);
2483 #if LUSTRE_TRACKS_LOCK_EXP_REFS
2484 void ldlm_dump_export_locks(struct obd_export *exp)
2486 cfs_spin_lock(&exp->exp_locks_list_guard);
2487 if (!cfs_list_empty(&exp->exp_locks_list)) {
2488 struct ldlm_lock *lock;
2490 CERROR("dumping locks for export %p,"
2491 "ignore if the unmount doesn't hang\n", exp);
2492 cfs_list_for_each_entry(lock, &exp->exp_locks_list, l_exp_refs_link)
2493 ldlm_lock_dump(D_ERROR, lock, 0);
2495 cfs_spin_unlock(&exp->exp_locks_list_guard);
2499 static int target_bulk_timeout(void *data)
2502 /* We don't fail the connection here, because having the export
2503 * killed makes the (vital) call to commitrw very sad.
2508 static inline char *bulk2type(struct ptlrpc_bulk_desc *desc)
2510 return desc->bd_type == BULK_GET_SINK ? "GET" : "PUT";
2513 int target_bulk_io(struct obd_export *exp, struct ptlrpc_bulk_desc *desc,
2514 struct l_wait_info *lwi)
2516 struct ptlrpc_request *req = desc->bd_req;
2520 /* Check if there is eviction in progress, and if so, wait for
2522 if (unlikely(cfs_atomic_read(&exp->exp_obd->obd_evict_inprogress))) {
2523 *lwi = LWI_INTR(NULL, NULL);
2524 rc = l_wait_event(exp->exp_obd->obd_evict_inprogress_waitq,
2525 !cfs_atomic_read(&exp->exp_obd->
2526 obd_evict_inprogress),
2530 /* Check if client was evicted or tried to reconnect already */
2531 if (exp->exp_failed || exp->exp_abort_active_req) {
2534 if (desc->bd_type == BULK_PUT_SINK)
2535 rc = sptlrpc_svc_wrap_bulk(req, desc);
2537 rc = ptlrpc_start_bulk_transfer(desc);
2540 if (rc == 0 && OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE)) {
2541 ptlrpc_abort_bulk(desc);
2542 } else if (rc == 0) {
2543 time_t start = cfs_time_current_sec();
2545 long timeoutl = req->rq_deadline - cfs_time_current_sec();
2546 cfs_duration_t timeout = timeoutl <= 0 ?
2547 CFS_TICK : cfs_time_seconds(timeoutl);
2548 *lwi = LWI_TIMEOUT_INTERVAL(timeout,
2549 cfs_time_seconds(1),
2550 target_bulk_timeout,
2552 rc = l_wait_event(desc->bd_waitq,
2553 !ptlrpc_server_bulk_active(desc) ||
2555 exp->exp_abort_active_req,
2557 LASSERT(rc == 0 || rc == -ETIMEDOUT);
2558 /* Wait again if we changed deadline */
2559 } while ((rc == -ETIMEDOUT) &&
2560 (req->rq_deadline > cfs_time_current_sec()));
2562 if (rc == -ETIMEDOUT) {
2563 DEBUG_REQ(D_ERROR, req,
2564 "timeout on bulk %s after %ld%+lds",
2566 req->rq_deadline - start,
2567 cfs_time_current_sec() -
2569 ptlrpc_abort_bulk(desc);
2570 } else if (exp->exp_failed) {
2571 DEBUG_REQ(D_ERROR, req, "Eviction on bulk %s",
2574 ptlrpc_abort_bulk(desc);
2575 } else if (exp->exp_abort_active_req) {
2576 DEBUG_REQ(D_ERROR, req, "Reconnect on bulk %s",
2578 /* we don't reply anyway */
2580 ptlrpc_abort_bulk(desc);
2581 } else if (!desc->bd_success ||
2582 desc->bd_nob_transferred != desc->bd_nob) {
2583 DEBUG_REQ(D_ERROR, req, "%s bulk %s %d(%d)",
2585 "truncated" : "network error on",
2587 desc->bd_nob_transferred,
2589 /* XXX should this be a different errno? */
2591 } else if (desc->bd_type == BULK_GET_SINK) {
2592 rc = sptlrpc_svc_unwrap_bulk(req, desc);
2595 DEBUG_REQ(D_ERROR, req, "bulk %s failed: rc %d",
2596 bulk2type(desc), rc);
2601 EXPORT_SYMBOL(target_bulk_io);