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.
32 * Copyright (c) 2011, 2012, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
40 # define EXPORT_SYMTAB
42 #define DEBUG_SUBSYSTEM S_LDLM
45 # include <libcfs/libcfs.h>
47 # include <liblustre.h>
50 #include <lustre_mds.h>
51 #include <lustre_dlm.h>
52 #include <lustre_net.h>
53 #include <lustre_sec.h>
54 #include "ldlm_internal.h"
56 /* @priority: if non-zero, move the selected to the list head
57 * @create: if zero, only search in existed connections
59 static int import_set_conn(struct obd_import *imp, struct obd_uuid *uuid,
60 int priority, int create)
62 struct ptlrpc_connection *ptlrpc_conn;
63 struct obd_import_conn *imp_conn = NULL, *item;
67 if (!create && !priority) {
68 CDEBUG(D_HA, "Nothing to do\n");
72 ptlrpc_conn = ptlrpc_uuid_to_connection(uuid);
74 CDEBUG(D_HA, "can't find connection %s\n", uuid->uuid);
79 OBD_ALLOC(imp_conn, sizeof(*imp_conn));
81 GOTO(out_put, rc = -ENOMEM);
85 cfs_spin_lock(&imp->imp_lock);
86 cfs_list_for_each_entry(item, &imp->imp_conn_list, oic_item) {
87 if (obd_uuid_equals(uuid, &item->oic_uuid)) {
89 cfs_list_del(&item->oic_item);
90 cfs_list_add(&item->oic_item,
92 item->oic_last_attempt = 0;
94 CDEBUG(D_HA, "imp %p@%s: found existing conn %s%s\n",
95 imp, imp->imp_obd->obd_name, uuid->uuid,
96 (priority ? ", moved to head" : ""));
97 cfs_spin_unlock(&imp->imp_lock);
98 GOTO(out_free, rc = 0);
103 imp_conn->oic_conn = ptlrpc_conn;
104 imp_conn->oic_uuid = *uuid;
105 imp_conn->oic_last_attempt = 0;
107 cfs_list_add(&imp_conn->oic_item, &imp->imp_conn_list);
109 cfs_list_add_tail(&imp_conn->oic_item,
110 &imp->imp_conn_list);
111 CDEBUG(D_HA, "imp %p@%s: add connection %s at %s\n",
112 imp, imp->imp_obd->obd_name, uuid->uuid,
113 (priority ? "head" : "tail"));
115 cfs_spin_unlock(&imp->imp_lock);
116 GOTO(out_free, rc = -ENOENT);
119 cfs_spin_unlock(&imp->imp_lock);
123 OBD_FREE(imp_conn, sizeof(*imp_conn));
125 ptlrpc_connection_put(ptlrpc_conn);
129 int import_set_conn_priority(struct obd_import *imp, struct obd_uuid *uuid)
131 return import_set_conn(imp, uuid, 1, 0);
134 int client_import_add_conn(struct obd_import *imp, struct obd_uuid *uuid,
137 return import_set_conn(imp, uuid, priority, 1);
140 int client_import_del_conn(struct obd_import *imp, struct obd_uuid *uuid)
142 struct obd_import_conn *imp_conn;
143 struct obd_export *dlmexp;
147 cfs_spin_lock(&imp->imp_lock);
148 if (cfs_list_empty(&imp->imp_conn_list)) {
149 LASSERT(!imp->imp_connection);
153 cfs_list_for_each_entry(imp_conn, &imp->imp_conn_list, oic_item) {
154 if (!obd_uuid_equals(uuid, &imp_conn->oic_uuid))
156 LASSERT(imp_conn->oic_conn);
158 /* is current conn? */
159 if (imp_conn == imp->imp_conn_current) {
160 LASSERT(imp_conn->oic_conn == imp->imp_connection);
162 if (imp->imp_state != LUSTRE_IMP_CLOSED &&
163 imp->imp_state != LUSTRE_IMP_DISCON) {
164 CERROR("can't remove current connection\n");
165 GOTO(out, rc = -EBUSY);
168 ptlrpc_connection_put(imp->imp_connection);
169 imp->imp_connection = NULL;
171 dlmexp = class_conn2export(&imp->imp_dlm_handle);
172 if (dlmexp && dlmexp->exp_connection) {
173 LASSERT(dlmexp->exp_connection ==
175 ptlrpc_connection_put(dlmexp->exp_connection);
176 dlmexp->exp_connection = NULL;
180 cfs_list_del(&imp_conn->oic_item);
181 ptlrpc_connection_put(imp_conn->oic_conn);
182 OBD_FREE(imp_conn, sizeof(*imp_conn));
183 CDEBUG(D_HA, "imp %p@%s: remove connection %s\n",
184 imp, imp->imp_obd->obd_name, uuid->uuid);
189 cfs_spin_unlock(&imp->imp_lock);
191 CERROR("connection %s not found\n", uuid->uuid);
196 * Find conn uuid by peer nid. @peer is a server nid. This function is used
197 * to find a conn uuid of @imp which can reach @peer.
199 int client_import_find_conn(struct obd_import *imp, lnet_nid_t peer,
200 struct obd_uuid *uuid)
202 struct obd_import_conn *conn;
206 cfs_spin_lock(&imp->imp_lock);
207 cfs_list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
208 /* check if conn uuid does have this peer nid */
209 if (class_check_uuid(&conn->oic_uuid, peer)) {
210 *uuid = conn->oic_uuid;
215 cfs_spin_unlock(&imp->imp_lock);
218 EXPORT_SYMBOL(client_import_find_conn);
220 void client_destroy_import(struct obd_import *imp)
222 /* drop security policy instance after all rpc finished/aborted
223 * to let all busy contexts be released. */
224 class_import_get(imp);
225 class_destroy_import(imp);
226 sptlrpc_import_sec_put(imp);
227 class_import_put(imp);
230 /* configure an RPC client OBD device
235 * 3 - inactive-on-startup
237 int client_obd_setup(struct obd_device *obddev, struct lustre_cfg *lcfg)
239 struct client_obd *cli = &obddev->u.cli;
240 struct obd_import *imp;
241 struct obd_uuid server_uuid;
242 int rq_portal, rp_portal, connect_op;
243 char *name = obddev->obd_type->typ_name;
244 ldlm_ns_type_t ns_type = LDLM_NS_TYPE_UNKNOWN;
248 /* In a more perfect world, we would hang a ptlrpc_client off of
249 * obd_type and just use the values from there. */
250 if (!strcmp(name, LUSTRE_OSC_NAME)) {
251 rq_portal = OST_REQUEST_PORTAL;
252 rp_portal = OSC_REPLY_PORTAL;
253 connect_op = OST_CONNECT;
254 cli->cl_sp_me = LUSTRE_SP_CLI;
255 cli->cl_sp_to = LUSTRE_SP_OST;
256 ns_type = LDLM_NS_TYPE_OSC;
258 } else if (!strcmp(name, LUSTRE_MDC_NAME)) {
259 rq_portal = MDS_REQUEST_PORTAL;
260 rp_portal = MDC_REPLY_PORTAL;
261 connect_op = MDS_CONNECT;
262 cli->cl_sp_me = LUSTRE_SP_CLI;
263 cli->cl_sp_to = LUSTRE_SP_MDT;
264 ns_type = LDLM_NS_TYPE_MDC;
266 } else if (!strcmp(name, LUSTRE_MGC_NAME)) {
267 rq_portal = MGS_REQUEST_PORTAL;
268 rp_portal = MGC_REPLY_PORTAL;
269 connect_op = MGS_CONNECT;
270 cli->cl_sp_me = LUSTRE_SP_MGC;
271 cli->cl_sp_to = LUSTRE_SP_MGS;
272 cli->cl_flvr_mgc.sf_rpc = SPTLRPC_FLVR_INVALID;
273 ns_type = LDLM_NS_TYPE_MGC;
276 CERROR("unknown client OBD type \"%s\", can't setup\n",
281 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
282 CERROR("requires a TARGET UUID\n");
286 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > 37) {
287 CERROR("client UUID must be less than 38 characters\n");
291 if (LUSTRE_CFG_BUFLEN(lcfg, 2) < 1) {
292 CERROR("setup requires a SERVER UUID\n");
296 if (LUSTRE_CFG_BUFLEN(lcfg, 2) > 37) {
297 CERROR("target UUID must be less than 38 characters\n");
301 cfs_init_rwsem(&cli->cl_sem);
302 cfs_sema_init(&cli->cl_mgc_sem, 1);
303 cli->cl_conn_count = 0;
304 memcpy(server_uuid.uuid, lustre_cfg_buf(lcfg, 2),
305 min_t(unsigned int, LUSTRE_CFG_BUFLEN(lcfg, 2),
306 sizeof(server_uuid)));
309 cli->cl_avail_grant = 0;
310 /* FIXME: should limit this for the sum of all cl_dirty_max */
311 cli->cl_dirty_max = OSC_MAX_DIRTY_DEFAULT * 1024 * 1024;
312 if (cli->cl_dirty_max >> CFS_PAGE_SHIFT > cfs_num_physpages / 8)
313 cli->cl_dirty_max = cfs_num_physpages << (CFS_PAGE_SHIFT - 3);
314 CFS_INIT_LIST_HEAD(&cli->cl_cache_waiters);
315 CFS_INIT_LIST_HEAD(&cli->cl_loi_ready_list);
316 CFS_INIT_LIST_HEAD(&cli->cl_loi_hp_ready_list);
317 CFS_INIT_LIST_HEAD(&cli->cl_loi_write_list);
318 CFS_INIT_LIST_HEAD(&cli->cl_loi_read_list);
319 client_obd_list_lock_init(&cli->cl_loi_list_lock);
320 cli->cl_r_in_flight = 0;
321 cli->cl_w_in_flight = 0;
323 cfs_spin_lock_init(&cli->cl_read_rpc_hist.oh_lock);
324 cfs_spin_lock_init(&cli->cl_write_rpc_hist.oh_lock);
325 cfs_spin_lock_init(&cli->cl_read_page_hist.oh_lock);
326 cfs_spin_lock_init(&cli->cl_write_page_hist.oh_lock);
327 cfs_spin_lock_init(&cli->cl_read_offset_hist.oh_lock);
328 cfs_spin_lock_init(&cli->cl_write_offset_hist.oh_lock);
329 cfs_waitq_init(&cli->cl_destroy_waitq);
330 cfs_atomic_set(&cli->cl_destroy_in_flight, 0);
331 #ifdef ENABLE_CHECKSUM
332 /* Turn on checksumming by default. */
333 cli->cl_checksum = 1;
335 * The supported checksum types will be worked out at connect time
336 * Set cl_chksum* to CRC32 for now to avoid returning screwed info
339 cli->cl_cksum_type = cli->cl_supp_cksum_types = OBD_CKSUM_CRC32;
341 cfs_atomic_set(&cli->cl_resends, OSC_DEFAULT_RESENDS);
343 /* This value may be changed at connect time in
344 ptlrpc_connect_interpret. */
345 cli->cl_max_pages_per_rpc = min((int)PTLRPC_MAX_BRW_PAGES,
346 (int)(1024 * 1024 >> CFS_PAGE_SHIFT));
348 if (!strcmp(name, LUSTRE_MDC_NAME)) {
349 cli->cl_max_rpcs_in_flight = MDC_MAX_RIF_DEFAULT;
350 } else if (cfs_num_physpages >> (20 - CFS_PAGE_SHIFT) <= 128 /* MB */) {
351 cli->cl_max_rpcs_in_flight = 2;
352 } else if (cfs_num_physpages >> (20 - CFS_PAGE_SHIFT) <= 256 /* MB */) {
353 cli->cl_max_rpcs_in_flight = 3;
354 } else if (cfs_num_physpages >> (20 - CFS_PAGE_SHIFT) <= 512 /* MB */) {
355 cli->cl_max_rpcs_in_flight = 4;
357 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT;
362 CERROR("ldlm_get_ref failed: %d\n", rc);
366 ptlrpc_init_client(rq_portal, rp_portal, name,
367 &obddev->obd_ldlm_client);
369 imp = class_new_import(obddev);
371 GOTO(err_ldlm, rc = -ENOENT);
372 imp->imp_client = &obddev->obd_ldlm_client;
373 imp->imp_connect_op = connect_op;
374 CFS_INIT_LIST_HEAD(&imp->imp_pinger_chain);
375 memcpy(cli->cl_target_uuid.uuid, lustre_cfg_buf(lcfg, 1),
376 LUSTRE_CFG_BUFLEN(lcfg, 1));
377 class_import_put(imp);
379 rc = client_import_add_conn(imp, &server_uuid, 1);
381 CERROR("can't add initial connection\n");
382 GOTO(err_import, rc);
385 cli->cl_import = imp;
386 /* cli->cl_max_mds_{easize,cookiesize} updated by mdc_init_ea_size() */
387 cli->cl_max_mds_easize = sizeof(struct lov_mds_md_v3);
388 cli->cl_max_mds_cookiesize = sizeof(struct llog_cookie);
390 if (LUSTRE_CFG_BUFLEN(lcfg, 3) > 0) {
391 if (!strcmp(lustre_cfg_string(lcfg, 3), "inactive")) {
392 CDEBUG(D_HA, "marking %s %s->%s as inactive\n",
393 name, obddev->obd_name,
394 cli->cl_target_uuid.uuid);
395 cfs_spin_lock(&imp->imp_lock);
396 imp->imp_deactive = 1;
397 cfs_spin_unlock(&imp->imp_lock);
401 obddev->obd_namespace = ldlm_namespace_new(obddev, obddev->obd_name,
402 LDLM_NAMESPACE_CLIENT,
403 LDLM_NAMESPACE_GREEDY,
405 if (obddev->obd_namespace == NULL) {
406 CERROR("Unable to create client namespace - %s\n",
408 GOTO(err_import, rc = -ENOMEM);
411 cli->cl_qchk_stat = CL_NOT_QUOTACHECKED;
416 class_destroy_import(imp);
424 int client_obd_cleanup(struct obd_device *obddev)
428 ldlm_namespace_free_post(obddev->obd_namespace);
429 obddev->obd_namespace = NULL;
435 /* ->o_connect() method for client side (OSC and MDC and MGC) */
436 int client_connect_import(const struct lu_env *env,
437 struct obd_export **exp,
438 struct obd_device *obd, struct obd_uuid *cluuid,
439 struct obd_connect_data *data, void *localdata)
441 struct client_obd *cli = &obd->u.cli;
442 struct obd_import *imp = cli->cl_import;
443 struct obd_connect_data *ocd;
444 struct lustre_handle conn = { 0 };
449 cfs_down_write(&cli->cl_sem);
450 if (cli->cl_conn_count > 0 )
451 GOTO(out_sem, rc = -EALREADY);
453 rc = class_connect(&conn, obd, cluuid);
457 cli->cl_conn_count++;
458 *exp = class_conn2export(&conn);
460 LASSERT(obd->obd_namespace);
462 imp->imp_dlm_handle = conn;
463 rc = ptlrpc_init_import(imp);
467 ocd = &imp->imp_connect_data;
470 imp->imp_connect_flags_orig = data->ocd_connect_flags;
473 rc = ptlrpc_connect_import(imp);
475 LASSERT (imp->imp_state == LUSTRE_IMP_DISCON);
478 LASSERT((*exp)->exp_connection);
481 LASSERTF((ocd->ocd_connect_flags & data->ocd_connect_flags) ==
482 ocd->ocd_connect_flags, "old "LPX64", new "LPX64"\n",
483 data->ocd_connect_flags, ocd->ocd_connect_flags);
484 data->ocd_connect_flags = ocd->ocd_connect_flags;
487 ptlrpc_pinger_add_import(imp);
493 cli->cl_conn_count--;
494 class_disconnect(*exp);
498 cfs_up_write(&cli->cl_sem);
503 int client_disconnect_export(struct obd_export *exp)
505 struct obd_device *obd = class_exp2obd(exp);
506 struct client_obd *cli;
507 struct obd_import *imp;
512 CERROR("invalid export for disconnect: exp %p cookie "LPX64"\n",
513 exp, exp ? exp->exp_handle.h_cookie : -1);
518 imp = cli->cl_import;
520 cfs_down_write(&cli->cl_sem);
521 CDEBUG(D_INFO, "disconnect %s - %d\n", obd->obd_name,
524 if (!cli->cl_conn_count) {
525 CERROR("disconnecting disconnected device (%s)\n",
527 GOTO(out_disconnect, rc = -EINVAL);
530 cli->cl_conn_count--;
531 if (cli->cl_conn_count)
532 GOTO(out_disconnect, rc = 0);
534 /* Mark import deactivated now, so we don't try to reconnect if any
535 * of the cleanup RPCs fails (e.g. ldlm cancel, etc). We don't
536 * fully deactivate the import, or that would drop all requests. */
537 cfs_spin_lock(&imp->imp_lock);
538 imp->imp_deactive = 1;
539 cfs_spin_unlock(&imp->imp_lock);
541 /* Some non-replayable imports (MDS's OSCs) are pinged, so just
542 * delete it regardless. (It's safe to delete an import that was
544 (void)ptlrpc_pinger_del_import(imp);
546 if (obd->obd_namespace != NULL) {
547 /* obd_force == local only */
548 ldlm_cli_cancel_unused(obd->obd_namespace, NULL,
549 obd->obd_force ? LCF_LOCAL : 0, NULL);
550 ldlm_namespace_free_prior(obd->obd_namespace, imp, obd->obd_force);
554 * there's no need to hold sem during disconnecting an import,
555 * and actually it may cause deadlock in gss.
557 cfs_up_write(&cli->cl_sem);
558 rc = ptlrpc_disconnect_import(imp, 0);
559 cfs_down_write(&cli->cl_sem);
561 ptlrpc_invalidate_import(imp);
563 if (imp->imp_rq_pool) {
564 ptlrpc_free_rq_pool(imp->imp_rq_pool);
565 imp->imp_rq_pool = NULL;
567 client_destroy_import(imp);
568 cli->cl_import = NULL;
573 /* use server style - class_disconnect should be always called for
575 err = class_disconnect(exp);
579 cfs_up_write(&cli->cl_sem);
584 int server_disconnect_export(struct obd_export *exp)
589 /* Disconnect early so that clients can't keep using export */
590 rc = class_disconnect(exp);
591 /* close import for avoid sending any requests */
592 if (exp->exp_imp_reverse)
593 ptlrpc_cleanup_imp(exp->exp_imp_reverse);
595 if (exp->exp_obd->obd_namespace != NULL)
596 ldlm_cancel_locks_for_export(exp);
598 /* complete all outstanding replies */
599 cfs_spin_lock(&exp->exp_lock);
600 while (!cfs_list_empty(&exp->exp_outstanding_replies)) {
601 struct ptlrpc_reply_state *rs =
602 cfs_list_entry(exp->exp_outstanding_replies.next,
603 struct ptlrpc_reply_state, rs_exp_list);
604 struct ptlrpc_service *svc = rs->rs_service;
606 cfs_spin_lock(&svc->srv_rs_lock);
607 cfs_list_del_init(&rs->rs_exp_list);
608 cfs_spin_lock(&rs->rs_lock);
609 ptlrpc_schedule_difficult_reply(rs);
610 cfs_spin_unlock(&rs->rs_lock);
611 cfs_spin_unlock(&svc->srv_rs_lock);
613 cfs_spin_unlock(&exp->exp_lock);
618 /* --------------------------------------------------------------------------
619 * from old lib/target.c
620 * -------------------------------------------------------------------------- */
622 static int target_handle_reconnect(struct lustre_handle *conn,
623 struct obd_export *exp,
624 struct obd_uuid *cluuid)
627 if (exp->exp_connection && exp->exp_imp_reverse) {
628 struct lustre_handle *hdl;
629 hdl = &exp->exp_imp_reverse->imp_remote_handle;
630 /* Might be a re-connect after a partition. */
631 if (!memcmp(&conn->cookie, &hdl->cookie, sizeof conn->cookie)) {
632 CWARN("%s: %s reconnecting\n", exp->exp_obd->obd_name,
634 conn->cookie = exp->exp_handle.h_cookie;
635 /* target_handle_connect() treats EALREADY and
636 * -EALREADY differently. EALREADY means we are
637 * doing a valid reconnect from the same client. */
640 CERROR("%s reconnecting from %s, "
641 "handle mismatch (ours "LPX64", theirs "
642 LPX64")\n", cluuid->uuid,
643 exp->exp_connection->c_remote_uuid.uuid,
644 hdl->cookie, conn->cookie);
645 memset(conn, 0, sizeof *conn);
646 /* target_handle_connect() treats EALREADY and
647 * -EALREADY differently. -EALREADY is an error
648 * (same UUID, different handle). */
653 conn->cookie = exp->exp_handle.h_cookie;
654 CDEBUG(D_HA, "connect export for UUID '%s' at %p, cookie "LPX64"\n",
655 cluuid->uuid, exp, conn->cookie);
659 void target_client_add_cb(struct obd_device *obd, __u64 transno, void *cb_data,
662 struct obd_export *exp = cb_data;
664 CDEBUG(D_RPCTRACE, "%s: committing for initial connect of %s\n",
665 obd->obd_name, exp->exp_client_uuid.uuid);
667 cfs_spin_lock(&exp->exp_lock);
668 exp->exp_need_sync = 0;
669 cfs_spin_unlock(&exp->exp_lock);
670 class_export_cb_put(exp);
672 EXPORT_SYMBOL(target_client_add_cb);
676 check_and_start_recovery_timer(struct obd_device *obd,
677 struct ptlrpc_request *req, int new_client);
680 check_and_start_recovery_timer(struct obd_device *obd,
681 struct ptlrpc_request *req, int new_client)
686 int target_handle_connect(struct ptlrpc_request *req)
688 struct obd_device *target, *targref = NULL;
689 struct obd_export *export = NULL;
690 struct obd_import *revimp;
691 struct lustre_handle conn;
692 struct lustre_handle *tmp;
693 struct obd_uuid tgtuuid;
694 struct obd_uuid cluuid;
695 struct obd_uuid remote_uuid;
699 struct obd_connect_data *data, *tmpdata;
701 lnet_nid_t *client_nid = NULL;
704 OBD_RACE(OBD_FAIL_TGT_CONN_RACE);
706 str = req_capsule_client_get(&req->rq_pill, &RMF_TGTUUID);
708 DEBUG_REQ(D_ERROR, req, "bad target UUID for connect");
709 GOTO(out, rc = -EINVAL);
712 obd_str2uuid(&tgtuuid, str);
713 target = class_uuid2obd(&tgtuuid);
715 target = class_name2obd(str);
717 if (!target || target->obd_stopping || !target->obd_set_up) {
718 LCONSOLE_ERROR_MSG(0x137, "UUID '%s' is not available "
719 "for connect (%s)\n", str,
720 !target ? "no target" :
721 (target->obd_stopping ? "stopping" :
723 GOTO(out, rc = -ENODEV);
726 if (target->obd_no_conn) {
727 LCONSOLE_WARN("%s: temporarily refusing client connection "
728 "from %s\n", target->obd_name,
729 libcfs_nid2str(req->rq_peer.nid));
730 GOTO(out, rc = -EAGAIN);
733 /* Make sure the target isn't cleaned up while we're here. Yes,
734 there's still a race between the above check and our incref here.
735 Really, class_uuid2obd should take the ref. */
736 targref = class_incref(target, __FUNCTION__, cfs_current());
739 str = req_capsule_client_get(&req->rq_pill, &RMF_CLUUID);
741 DEBUG_REQ(D_ERROR, req, "bad client UUID for connect");
742 GOTO(out, rc = -EINVAL);
745 obd_str2uuid(&cluuid, str);
747 /* XXX extract a nettype and format accordingly */
748 switch (sizeof(lnet_nid_t)) {
749 /* NB the casts only avoid compiler warnings */
751 snprintf(remote_uuid.uuid, sizeof remote_uuid,
752 "NET_"LPX64"_UUID", (__u64)req->rq_peer.nid);
755 snprintf(remote_uuid.uuid, sizeof remote_uuid,
756 "NET_%x_UUID", (__u32)req->rq_peer.nid);
762 tmp = req_capsule_client_get(&req->rq_pill, &RMF_CONN);
764 GOTO(out, rc = -EPROTO);
768 size = req_capsule_get_size(&req->rq_pill, &RMF_CONNECT_DATA,
770 data = req_capsule_client_get(&req->rq_pill, &RMF_CONNECT_DATA);
772 GOTO(out, rc = -EPROTO);
774 rc = req_capsule_server_pack(&req->rq_pill);
778 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT) {
780 DEBUG_REQ(D_WARNING, req, "Refusing old (unversioned) "
781 "libclient connection attempt");
782 GOTO(out, rc = -EPROTO);
783 } else if (data->ocd_version < LUSTRE_VERSION_CODE -
784 LUSTRE_VERSION_ALLOWED_OFFSET ||
785 data->ocd_version > LUSTRE_VERSION_CODE +
786 LUSTRE_VERSION_ALLOWED_OFFSET) {
787 DEBUG_REQ(D_WARNING, req, "Refusing %s (%d.%d.%d.%d) "
788 "libclient connection attempt",
789 data->ocd_version < LUSTRE_VERSION_CODE ?
791 OBD_OCD_VERSION_MAJOR(data->ocd_version),
792 OBD_OCD_VERSION_MINOR(data->ocd_version),
793 OBD_OCD_VERSION_PATCH(data->ocd_version),
794 OBD_OCD_VERSION_FIX(data->ocd_version));
795 data = req_capsule_server_sized_get(&req->rq_pill,
797 offsetof(typeof(*data), ocd_version) +
798 sizeof(data->ocd_version));
800 data->ocd_connect_flags = OBD_CONNECT_VERSION;
801 data->ocd_version = LUSTRE_VERSION_CODE;
803 GOTO(out, rc = -EPROTO);
807 if ((lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_INITIAL) &&
808 (data->ocd_connect_flags & OBD_CONNECT_MDS))
811 /* lctl gets a backstage, all-access pass. */
812 if (obd_uuid_equals(&cluuid, &target->obd_uuid))
813 goto dont_check_exports;
815 export = cfs_hash_lookup(target->obd_uuid_hash, &cluuid);
819 /* we've found an export in the hash */
820 if (export->exp_connecting) { /* bug 9635, et. al. */
821 CWARN("%s: exp %p already connecting\n",
822 export->exp_obd->obd_name, export);
823 class_export_put(export);
826 } else if (mds_conn && export->exp_connection) {
827 if (req->rq_peer.nid != export->exp_connection->c_peer.nid)
828 /* mds reconnected after failover */
829 CWARN("%s: received MDS connection from NID %s,"
830 " removing former export from NID %s\n",
831 target->obd_name, libcfs_nid2str(req->rq_peer.nid),
832 libcfs_nid2str(export->exp_connection->c_peer.nid));
834 /* new mds connection from the same nid */
835 CWARN("%s: received new MDS connection from NID %s,"
836 " removing former export from same NID\n",
837 target->obd_name, libcfs_nid2str(req->rq_peer.nid));
838 class_fail_export(export);
839 class_export_put(export);
842 } else if (export->exp_connection != NULL &&
843 req->rq_peer.nid != export->exp_connection->c_peer.nid &&
844 (lustre_msg_get_op_flags(req->rq_reqmsg) &
845 MSG_CONNECT_INITIAL)) {
846 /* in mds failover we have static uuid but nid can be
848 CWARN("%s: cookie %s seen on new NID %s when "
849 "existing NID %s is already connected\n",
850 target->obd_name, cluuid.uuid,
851 libcfs_nid2str(req->rq_peer.nid),
852 libcfs_nid2str(export->exp_connection->c_peer.nid));
854 class_export_put(export);
857 cfs_spin_lock(&export->exp_lock);
858 export->exp_connecting = 1;
859 cfs_spin_unlock(&export->exp_lock);
860 LASSERT(export->exp_obd == target);
862 rc = target_handle_reconnect(&conn, export, &cluuid);
865 /* If we found an export, we already unlocked. */
868 OBD_FAIL_TIMEOUT(OBD_FAIL_TGT_DELAY_CONNECT, 2 * obd_timeout);
869 } else if (req->rq_export == NULL &&
870 cfs_atomic_read(&export->exp_rpc_count) > 0) {
871 CWARN("%s: refuse connection from %s/%s to 0x%p/%d\n",
872 target->obd_name, cluuid.uuid,
873 libcfs_nid2str(req->rq_peer.nid),
874 export, cfs_atomic_read(&export->exp_refcount));
875 GOTO(out, rc = -EBUSY);
876 } else if (req->rq_export != NULL &&
877 (cfs_atomic_read(&export->exp_rpc_count) > 1)) {
878 /* the current connect rpc has increased exp_rpc_count */
879 CWARN("%s: refuse reconnection from %s@%s to 0x%p/%d\n",
880 target->obd_name, cluuid.uuid,
881 libcfs_nid2str(req->rq_peer.nid),
882 export, cfs_atomic_read(&export->exp_rpc_count) - 1);
883 cfs_spin_lock(&export->exp_lock);
884 if (req->rq_export->exp_conn_cnt <
885 lustre_msg_get_conn_cnt(req->rq_reqmsg))
886 /* try to abort active requests */
887 req->rq_export->exp_abort_active_req = 1;
888 cfs_spin_unlock(&export->exp_lock);
889 GOTO(out, rc = -EBUSY);
890 } else if (lustre_msg_get_conn_cnt(req->rq_reqmsg) == 1) {
891 CERROR("%s: NID %s (%s) reconnected with 1 conn_cnt; "
892 "cookies not random?\n", target->obd_name,
893 libcfs_nid2str(req->rq_peer.nid), cluuid.uuid);
894 GOTO(out, rc = -EALREADY);
896 OBD_FAIL_TIMEOUT(OBD_FAIL_TGT_DELAY_RECONNECT, 2 * obd_timeout);
903 CDEBUG(D_HA, "%s: connection from %s@%s %st"LPU64" exp %p cur %ld last %ld\n",
904 target->obd_name, cluuid.uuid, libcfs_nid2str(req->rq_peer.nid),
905 target->obd_recovering ? "recovering/" : "", data->ocd_transno,
906 export, (long)cfs_time_current_sec(),
907 export ? (long)export->exp_last_request_time : 0);
909 /* If this is the first time a client connects, reset the recovery
911 if (rc == 0 && target->obd_recovering)
912 check_and_start_recovery_timer(target, req, export == NULL);
914 /* We want to handle EALREADY but *not* -EALREADY from
915 * target_handle_reconnect(), return reconnection state in a flag */
916 if (rc == EALREADY) {
917 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECONNECT);
923 /* Tell the client if we support replayable requests */
924 if (target->obd_replayable)
925 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
926 client_nid = &req->rq_peer.nid;
928 if (export == NULL) {
929 if (target->obd_recovering) {
932 t = cfs_timer_deadline(&target->obd_recovery_timer);
933 t = cfs_time_sub(t, cfs_time_current());
934 CERROR("%s: denying connection for new client %s (%s): "
935 "%d clients in recovery for "CFS_TIME_T"s\n",
937 libcfs_nid2str(req->rq_peer.nid), cluuid.uuid,
938 cfs_atomic_read(&target-> \
939 obd_lock_replay_clients),
940 cfs_duration_sec(t));
944 rc = obd_connect(req->rq_svc_thread->t_env,
945 &export, target, &cluuid, data,
948 conn.cookie = export->exp_handle.h_cookie;
949 /* LU-1092 reconnect put export refcount in the
950 * end, connect needs take one here too. */
951 class_export_get(export);
955 rc = obd_reconnect(req->rq_svc_thread->t_env,
956 export, target, &cluuid, data, client_nid);
958 /* prevous done via class_conn2export */
959 class_export_get(export);
964 LASSERT(target->u.obt.obt_magic == OBT_MAGIC);
965 data->ocd_instance = target->u.obt.obt_instance;
967 /* Return only the parts of obd_connect_data that we understand, so the
968 * client knows that we don't understand the rest. */
970 tmpsize = req_capsule_get_size(&req->rq_pill, &RMF_CONNECT_DATA,
972 tmpdata = req_capsule_server_get(&req->rq_pill,
974 /* Don't use struct assignment here, because the client reply
975 * buffer may be smaller/larger than the local struct
976 * obd_connect_data. */
977 memcpy(tmpdata, data, min(tmpsize, size));
980 /* If all else goes well, this is our RPC return code. */
983 lustre_msg_set_handle(req->rq_repmsg, &conn);
985 /* If the client and the server are the same node, we will already
986 * have an export that really points to the client's DLM export,
987 * because we have a shared handles table.
989 * XXX this will go away when shaver stops sending the "connect" handle
990 * in the real "remote handle" field of the request --phik 24 Apr 2003
992 if (req->rq_export != NULL)
993 class_export_put(req->rq_export);
995 req->rq_export = export;
997 cfs_spin_lock(&export->exp_lock);
998 if (export->exp_conn_cnt >= lustre_msg_get_conn_cnt(req->rq_reqmsg)) {
999 cfs_spin_unlock(&export->exp_lock);
1000 CERROR("%s: %s already connected at higher conn_cnt: %d > %d\n",
1001 cluuid.uuid, libcfs_nid2str(req->rq_peer.nid),
1002 export->exp_conn_cnt,
1003 lustre_msg_get_conn_cnt(req->rq_reqmsg));
1005 GOTO(out, rc = -EALREADY);
1007 LASSERT(lustre_msg_get_conn_cnt(req->rq_reqmsg) > 0);
1008 export->exp_conn_cnt = lustre_msg_get_conn_cnt(req->rq_reqmsg);
1009 export->exp_abort_active_req = 0;
1011 /* request from liblustre? Don't evict it for not pinging. */
1012 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT) {
1013 export->exp_libclient = 1;
1014 cfs_spin_unlock(&export->exp_lock);
1016 cfs_spin_lock(&target->obd_dev_lock);
1017 cfs_list_del_init(&export->exp_obd_chain_timed);
1018 cfs_spin_unlock(&target->obd_dev_lock);
1020 cfs_spin_unlock(&export->exp_lock);
1023 if (export->exp_connection != NULL) {
1024 /* Check to see if connection came from another NID */
1025 if ((export->exp_connection->c_peer.nid != req->rq_peer.nid) &&
1026 !cfs_hlist_unhashed(&export->exp_nid_hash))
1027 cfs_hash_del(export->exp_obd->obd_nid_hash,
1028 &export->exp_connection->c_peer.nid,
1029 &export->exp_nid_hash);
1031 ptlrpc_connection_put(export->exp_connection);
1034 export->exp_connection = ptlrpc_connection_get(req->rq_peer,
1037 if (cfs_hlist_unhashed(&export->exp_nid_hash)) {
1038 cfs_hash_add(export->exp_obd->obd_nid_hash,
1039 &export->exp_connection->c_peer.nid,
1040 &export->exp_nid_hash);
1043 class_disconnect->class_export_recovery_cleanup() race
1045 if (target->obd_recovering && !export->exp_in_recovery) {
1047 __u64 transno = data->ocd_transno;
1049 cfs_spin_lock(&export->exp_lock);
1050 export->exp_in_recovery = 1;
1051 export->exp_req_replay_needed = 1;
1052 export->exp_lock_replay_needed = 1;
1053 cfs_spin_unlock(&export->exp_lock);
1055 has_transno = !!(lustre_msg_get_op_flags(req->rq_reqmsg) &
1056 MSG_CONNECT_TRANSNO);
1057 if (has_transno && transno == 0)
1058 CWARN("Connect with zero transno!\n");
1060 if (has_transno && transno > 0 &&
1061 transno < target->obd_next_recovery_transno &&
1062 transno > target->obd_last_committed) {
1063 /* another way is to use cmpxchg() so it will be
1065 cfs_spin_lock(&target->obd_recovery_task_lock);
1066 if (transno < target->obd_next_recovery_transno)
1067 target->obd_next_recovery_transno = transno;
1068 cfs_spin_unlock(&target->obd_recovery_task_lock);
1071 cfs_atomic_inc(&target->obd_req_replay_clients);
1072 cfs_atomic_inc(&target->obd_lock_replay_clients);
1073 if (cfs_atomic_inc_return(&target->obd_connected_clients) ==
1074 target->obd_max_recoverable_clients)
1075 cfs_waitq_signal(&target->obd_next_transno_waitq);
1078 /* Tell the client we're in recovery, when client is involved in it. */
1079 if (target->obd_recovering)
1080 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
1082 tmp = req_capsule_client_get(&req->rq_pill, &RMF_CONN);
1085 if (export->exp_imp_reverse != NULL) {
1086 /* destroyed import can be still referenced in ctxt */
1087 obd_set_info_async(export, sizeof(KEY_REVIMP_UPD),
1088 KEY_REVIMP_UPD, 0, NULL, NULL);
1090 client_destroy_import(export->exp_imp_reverse);
1093 /* for the rest part, we return -ENOTCONN in case of errors
1094 * in order to let client initialize connection again.
1096 revimp = export->exp_imp_reverse = class_new_import(target);
1098 CERROR("fail to alloc new reverse import.\n");
1099 GOTO(out, rc = -ENOTCONN);
1102 revimp->imp_connection = ptlrpc_connection_addref(export->exp_connection);
1103 revimp->imp_client = &export->exp_obd->obd_ldlm_client;
1104 revimp->imp_remote_handle = conn;
1105 revimp->imp_dlm_fake = 1;
1106 revimp->imp_state = LUSTRE_IMP_FULL;
1108 /* unknown versions will be caught in
1109 * ptlrpc_handle_server_req_in->lustre_unpack_msg() */
1110 revimp->imp_msg_magic = req->rq_reqmsg->lm_magic;
1112 if ((export->exp_connect_flags & OBD_CONNECT_AT) &&
1113 (revimp->imp_msg_magic != LUSTRE_MSG_MAGIC_V1))
1114 revimp->imp_msghdr_flags |= MSGHDR_AT_SUPPORT;
1116 revimp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
1118 if ((export->exp_connect_flags & OBD_CONNECT_FULL20) &&
1119 (revimp->imp_msg_magic != LUSTRE_MSG_MAGIC_V1))
1120 revimp->imp_msghdr_flags |= MSGHDR_CKSUM_INCOMPAT18;
1122 revimp->imp_msghdr_flags &= ~MSGHDR_CKSUM_INCOMPAT18;
1124 rc = sptlrpc_import_sec_adapt(revimp, req->rq_svc_ctx, &req->rq_flvr);
1126 CERROR("Failed to get sec for reverse import: %d\n", rc);
1127 export->exp_imp_reverse = NULL;
1128 class_destroy_import(revimp);
1131 class_import_put(revimp);
1134 cfs_spin_lock(&export->exp_lock);
1135 export->exp_connecting = 0;
1136 cfs_spin_unlock(&export->exp_lock);
1138 class_export_put(export);
1141 class_decref(targref, __FUNCTION__, cfs_current());
1143 req->rq_status = rc;
1147 int target_handle_disconnect(struct ptlrpc_request *req)
1152 rc = req_capsule_server_pack(&req->rq_pill);
1156 /* keep the rq_export around so we can send the reply */
1157 req->rq_status = obd_disconnect(class_export_get(req->rq_export));
1162 void target_destroy_export(struct obd_export *exp)
1164 /* exports created from last_rcvd data, and "fake"
1165 exports created by lctl don't have an import */
1166 if (exp->exp_imp_reverse != NULL)
1167 client_destroy_import(exp->exp_imp_reverse);
1169 LASSERT_ATOMIC_ZERO(&exp->exp_locks_count);
1170 LASSERT_ATOMIC_ZERO(&exp->exp_rpc_count);
1171 LASSERT_ATOMIC_ZERO(&exp->exp_cb_count);
1172 LASSERT_ATOMIC_ZERO(&exp->exp_replay_count);
1176 * Recovery functions
1178 static void target_request_copy_get(struct ptlrpc_request *req)
1180 class_export_rpc_get(req->rq_export);
1181 LASSERT(cfs_list_empty(&req->rq_list));
1182 CFS_INIT_LIST_HEAD(&req->rq_replay_list);
1184 /* increase refcount to keep request in queue */
1185 cfs_atomic_inc(&req->rq_refcount);
1186 /** let export know it has replays to be handled */
1187 cfs_atomic_inc(&req->rq_export->exp_replay_count);
1190 static void target_request_copy_put(struct ptlrpc_request *req)
1192 LASSERT(cfs_list_empty(&req->rq_replay_list));
1193 LASSERT_ATOMIC_POS(&req->rq_export->exp_replay_count);
1195 cfs_atomic_dec(&req->rq_export->exp_replay_count);
1196 class_export_rpc_put(req->rq_export);
1197 ptlrpc_server_drop_request(req);
1200 static int target_exp_enqueue_req_replay(struct ptlrpc_request *req)
1202 __u64 transno = lustre_msg_get_transno(req->rq_reqmsg);
1203 struct obd_export *exp = req->rq_export;
1204 struct ptlrpc_request *reqiter;
1209 cfs_spin_lock(&exp->exp_lock);
1210 cfs_list_for_each_entry(reqiter, &exp->exp_req_replay_queue,
1212 if (lustre_msg_get_transno(reqiter->rq_reqmsg) == transno) {
1219 /* we expect it with RESENT and REPLAY flags */
1220 if ((lustre_msg_get_flags(req->rq_reqmsg) &
1221 (MSG_RESENT | MSG_REPLAY)) != (MSG_RESENT | MSG_REPLAY))
1222 CERROR("invalid flags %x of resent replay\n",
1223 lustre_msg_get_flags(req->rq_reqmsg));
1225 cfs_list_add_tail(&req->rq_replay_list,
1226 &exp->exp_req_replay_queue);
1229 cfs_spin_unlock(&exp->exp_lock);
1233 static void target_exp_dequeue_req_replay(struct ptlrpc_request *req)
1235 LASSERT(!cfs_list_empty(&req->rq_replay_list));
1236 LASSERT(req->rq_export);
1238 cfs_spin_lock(&req->rq_export->exp_lock);
1239 cfs_list_del_init(&req->rq_replay_list);
1240 cfs_spin_unlock(&req->rq_export->exp_lock);
1244 static void target_finish_recovery(struct obd_device *obd)
1246 time_t elapsed_time = max_t(time_t, 1, cfs_time_current_sec() -
1247 obd->obd_recovery_start);
1250 LCONSOLE_INFO("%s: Recovery over after %d:%.02d, of %d clients "
1251 "%d recovered and %d %s evicted.\n", obd->obd_name,
1252 (int)elapsed_time / 60, (int)elapsed_time % 60,
1253 obd->obd_max_recoverable_clients,
1254 cfs_atomic_read(&obd->obd_connected_clients),
1255 obd->obd_stale_clients,
1256 obd->obd_stale_clients == 1 ? "was" : "were");
1258 ldlm_reprocess_all_ns(obd->obd_namespace);
1259 cfs_spin_lock(&obd->obd_recovery_task_lock);
1260 if (!cfs_list_empty(&obd->obd_req_replay_queue) ||
1261 !cfs_list_empty(&obd->obd_lock_replay_queue) ||
1262 !cfs_list_empty(&obd->obd_final_req_queue)) {
1263 CERROR("%s: Recovery queues ( %s%s%s) are not empty\n",
1265 cfs_list_empty(&obd->obd_req_replay_queue) ? "" : "req ",
1266 cfs_list_empty(&obd->obd_lock_replay_queue) ? \
1268 cfs_list_empty(&obd->obd_final_req_queue) ? \
1270 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1273 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1275 obd->obd_recovery_end = cfs_time_current_sec();
1277 /* when recovery finished, cleanup orphans on mds and ost */
1278 if (OBT(obd) && OBP(obd, postrecov)) {
1279 int rc = OBP(obd, postrecov)(obd);
1281 LCONSOLE_WARN("%s: Post recovery failed, rc %d\n",
1287 static void abort_req_replay_queue(struct obd_device *obd)
1289 struct ptlrpc_request *req, *n;
1290 cfs_list_t abort_list;
1292 CFS_INIT_LIST_HEAD(&abort_list);
1293 cfs_spin_lock(&obd->obd_recovery_task_lock);
1294 cfs_list_splice_init(&obd->obd_req_replay_queue, &abort_list);
1295 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1296 cfs_list_for_each_entry_safe(req, n, &abort_list, rq_list) {
1297 DEBUG_REQ(D_WARNING, req, "aborted:");
1298 req->rq_status = -ENOTCONN;
1299 if (ptlrpc_error(req)) {
1300 DEBUG_REQ(D_ERROR, req,
1301 "failed abort_req_reply; skipping");
1303 target_exp_dequeue_req_replay(req);
1304 target_request_copy_put(req);
1308 static void abort_lock_replay_queue(struct obd_device *obd)
1310 struct ptlrpc_request *req, *n;
1311 cfs_list_t abort_list;
1313 CFS_INIT_LIST_HEAD(&abort_list);
1314 cfs_spin_lock(&obd->obd_recovery_task_lock);
1315 cfs_list_splice_init(&obd->obd_lock_replay_queue, &abort_list);
1316 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1317 cfs_list_for_each_entry_safe(req, n, &abort_list, rq_list){
1318 DEBUG_REQ(D_ERROR, req, "aborted:");
1319 req->rq_status = -ENOTCONN;
1320 if (ptlrpc_error(req)) {
1321 DEBUG_REQ(D_ERROR, req,
1322 "failed abort_lock_reply; skipping");
1324 target_request_copy_put(req);
1328 /* Called from a cleanup function if the device is being cleaned up
1329 forcefully. The exports should all have been disconnected already,
1330 the only thing left to do is
1331 - clear the recovery flags
1333 - free queued requests and replies, but don't send replies
1334 Because the obd_stopping flag is set, no new requests should be received.
1337 void target_cleanup_recovery(struct obd_device *obd)
1339 struct ptlrpc_request *req, *n;
1340 cfs_list_t clean_list;
1343 CFS_INIT_LIST_HEAD(&clean_list);
1344 cfs_spin_lock(&obd->obd_dev_lock);
1345 if (!obd->obd_recovering) {
1346 cfs_spin_unlock(&obd->obd_dev_lock);
1350 obd->obd_recovering = obd->obd_abort_recovery = 0;
1351 cfs_spin_unlock(&obd->obd_dev_lock);
1353 cfs_spin_lock(&obd->obd_recovery_task_lock);
1354 target_cancel_recovery_timer(obd);
1355 cfs_list_splice_init(&obd->obd_req_replay_queue, &clean_list);
1356 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1358 cfs_list_for_each_entry_safe(req, n, &clean_list, rq_list) {
1359 LASSERT(req->rq_reply_state == 0);
1360 target_exp_dequeue_req_replay(req);
1361 target_request_copy_put(req);
1364 cfs_spin_lock(&obd->obd_recovery_task_lock);
1365 cfs_list_splice_init(&obd->obd_lock_replay_queue, &clean_list);
1366 cfs_list_splice_init(&obd->obd_final_req_queue, &clean_list);
1367 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1369 cfs_list_for_each_entry_safe(req, n, &clean_list, rq_list){
1370 LASSERT(req->rq_reply_state == 0);
1371 target_request_copy_put(req);
1377 /* obd_recovery_task_lock should be held */
1378 void target_cancel_recovery_timer(struct obd_device *obd)
1380 CDEBUG(D_HA, "%s: cancel recovery timer\n", obd->obd_name);
1381 cfs_timer_disarm(&obd->obd_recovery_timer);
1384 static void target_start_recovery_timer(struct obd_device *obd)
1386 if (obd->obd_recovery_start != 0)
1389 cfs_spin_lock(&obd->obd_dev_lock);
1390 if (!obd->obd_recovering || obd->obd_abort_recovery) {
1391 cfs_spin_unlock(&obd->obd_dev_lock);
1395 LASSERT(obd->obd_recovery_timeout != 0);
1397 if (obd->obd_recovery_start != 0) {
1398 cfs_spin_unlock(&obd->obd_dev_lock);
1402 cfs_timer_arm(&obd->obd_recovery_timer,
1403 cfs_time_shift(obd->obd_recovery_timeout));
1404 obd->obd_recovery_start = cfs_time_current_sec();
1405 cfs_spin_unlock(&obd->obd_dev_lock);
1407 LCONSOLE_WARN("%s: Will be in recovery for at least %d:%.02d, "
1408 "or until %d client%s reconnect%s\n",
1410 obd->obd_recovery_timeout / 60,
1411 obd->obd_recovery_timeout % 60,
1412 obd->obd_max_recoverable_clients,
1413 (obd->obd_max_recoverable_clients == 1) ? "" : "s",
1414 (obd->obd_max_recoverable_clients == 1) ? "s": "");
1418 * extend recovery window.
1420 * if @extend is true, extend recovery window to have @drt remaining at least;
1421 * otherwise, make sure the recovery timeout value is not less than @drt.
1423 static void extend_recovery_timer(struct obd_device *obd, int drt, bool extend)
1427 cfs_duration_t left;
1430 cfs_spin_lock(&obd->obd_dev_lock);
1431 if (!obd->obd_recovering || obd->obd_abort_recovery) {
1432 cfs_spin_unlock(&obd->obd_dev_lock);
1436 LASSERT(obd->obd_recovery_start != 0);
1438 now = cfs_time_current_sec();
1439 to = obd->obd_recovery_timeout;
1440 end = obd->obd_recovery_start + to;
1441 left = cfs_time_sub(end, now);
1443 if (extend && (drt > left)) {
1445 } else if (!extend && (drt > to)) {
1447 /* reduce drt by already passed time */
1448 drt -= obd->obd_recovery_timeout - left;
1451 if (to > obd->obd_recovery_time_hard)
1452 to = obd->obd_recovery_time_hard;
1453 if (obd->obd_recovery_timeout < to) {
1454 obd->obd_recovery_timeout = to;
1455 cfs_timer_arm(&obd->obd_recovery_timer,
1456 cfs_time_shift(drt));
1458 cfs_spin_unlock(&obd->obd_dev_lock);
1460 CDEBUG(D_HA, "%s: recovery timer will expire in %u seconds\n",
1461 obd->obd_name, (unsigned)drt);
1464 /* Reset the timer with each new client connection */
1466 * This timer is actually reconnect_timer, which is for making sure
1467 * the total recovery window is at least as big as my reconnect
1468 * attempt timing. So the initial recovery time_out will be set to
1469 * OBD_RECOVERY_FACTOR * obd_timeout. If the timeout coming
1470 * from client is bigger than this, then the recovery time_out will
1471 * be extended to make sure the client could be reconnected, in the
1472 * process, the timeout from the new client should be ignored.
1476 check_and_start_recovery_timer(struct obd_device *obd,
1477 struct ptlrpc_request *req,
1480 int service_time = lustre_msg_get_service_time(req->rq_reqmsg);
1481 struct obd_device_target *obt = &obd->u.obt;
1482 struct lustre_sb_info *lsi;
1484 if (!new_client && service_time)
1485 /* Teach server about old server's estimates, as first guess
1486 * at how long new requests will take. */
1487 at_measured(&req->rq_rqbd->rqbd_service->srv_at_estimate,
1490 target_start_recovery_timer(obd);
1492 /* convert the service time to rpc timeout,
1493 * reuse service_time to limit stack usage */
1494 service_time = at_est2timeout(service_time);
1496 /* We expect other clients to timeout within service_time, then try
1497 * to reconnect, then try the failover server. The max delay between
1498 * connect attempts is SWITCH_MAX + SWITCH_INC + INITIAL */
1499 service_time += 2 * INITIAL_CONNECT_TIMEOUT;
1501 LASSERT(obt->obt_magic == OBT_MAGIC);
1502 lsi = s2lsi(obt->obt_sb);
1503 if (!(lsi->lsi_flags | LSI_IR_CAPABLE))
1504 service_time += 2 * (CONNECTION_SWITCH_MAX +
1505 CONNECTION_SWITCH_INC);
1506 if (service_time > obd->obd_recovery_timeout && !new_client)
1507 extend_recovery_timer(obd, service_time, false);
1510 /** Health checking routines */
1511 static inline int exp_connect_healthy(struct obd_export *exp)
1513 return (exp->exp_in_recovery);
1516 /** if export done req_replay or has replay in queue */
1517 static inline int exp_req_replay_healthy(struct obd_export *exp)
1519 return (!exp->exp_req_replay_needed ||
1520 cfs_atomic_read(&exp->exp_replay_count) > 0);
1522 /** if export done lock_replay or has replay in queue */
1523 static inline int exp_lock_replay_healthy(struct obd_export *exp)
1525 return (!exp->exp_lock_replay_needed ||
1526 cfs_atomic_read(&exp->exp_replay_count) > 0);
1529 static inline int exp_vbr_healthy(struct obd_export *exp)
1531 return (!exp->exp_vbr_failed);
1534 static inline int exp_finished(struct obd_export *exp)
1536 return (exp->exp_in_recovery && !exp->exp_lock_replay_needed);
1539 /** Checking routines for recovery */
1540 static int check_for_clients(struct obd_device *obd)
1542 unsigned int clnts = cfs_atomic_read(&obd->obd_connected_clients);
1544 if (obd->obd_abort_recovery || obd->obd_recovery_expired)
1546 LASSERT(clnts <= obd->obd_max_recoverable_clients);
1547 if (obd->obd_no_conn == 0 &&
1548 clnts + obd->obd_stale_clients == obd->obd_max_recoverable_clients)
1553 static int check_for_next_transno(struct obd_device *obd)
1555 struct ptlrpc_request *req = NULL;
1556 int wake_up = 0, connected, completed, queue_len;
1557 __u64 next_transno, req_transno;
1560 cfs_spin_lock(&obd->obd_recovery_task_lock);
1561 if (!cfs_list_empty(&obd->obd_req_replay_queue)) {
1562 req = cfs_list_entry(obd->obd_req_replay_queue.next,
1563 struct ptlrpc_request, rq_list);
1564 req_transno = lustre_msg_get_transno(req->rq_reqmsg);
1569 connected = cfs_atomic_read(&obd->obd_connected_clients);
1570 completed = connected - cfs_atomic_read(&obd->obd_req_replay_clients);
1571 queue_len = obd->obd_requests_queued_for_recovery;
1572 next_transno = obd->obd_next_recovery_transno;
1574 CDEBUG(D_HA, "max: %d, connected: %d, completed: %d, queue_len: %d, "
1575 "req_transno: "LPU64", next_transno: "LPU64"\n",
1576 obd->obd_max_recoverable_clients, connected, completed,
1577 queue_len, req_transno, next_transno);
1579 if (obd->obd_abort_recovery) {
1580 CDEBUG(D_HA, "waking for aborted recovery\n");
1582 } else if (obd->obd_recovery_expired) {
1583 CDEBUG(D_HA, "waking for expired recovery\n");
1585 } else if (cfs_atomic_read(&obd->obd_req_replay_clients) == 0) {
1586 CDEBUG(D_HA, "waking for completed recovery\n");
1588 } else if (req_transno == next_transno) {
1589 CDEBUG(D_HA, "waking for next ("LPD64")\n", next_transno);
1591 } else if (queue_len == cfs_atomic_read(&obd->obd_req_replay_clients)) {
1593 /** handle gaps occured due to lost reply or VBR */
1594 LASSERTF(req_transno >= next_transno,
1595 "req_transno: "LPU64", next_transno: "LPU64"\n",
1596 req_transno, next_transno);
1597 if (req_transno > obd->obd_last_committed &&
1598 !obd->obd_version_recov)
1601 "%s: waking for gap in transno, VBR is %s (skip: "
1602 LPD64", ql: %d, comp: %d, conn: %d, next: "LPD64
1603 ", last_committed: "LPD64")\n",
1604 obd->obd_name, obd->obd_version_recov ? "ON" : "OFF",
1605 next_transno, queue_len, completed, connected,
1606 req_transno, obd->obd_last_committed);
1607 obd->obd_next_recovery_transno = req_transno;
1609 } else if (OBD_FAIL_CHECK(OBD_FAIL_MDS_RECOVERY_ACCEPTS_GAPS)) {
1610 CDEBUG(D_HA, "accepting transno gaps is explicitly allowed"
1611 " by fail_lock, waking up ("LPD64")\n", next_transno);
1612 obd->obd_next_recovery_transno = req_transno;
1615 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1619 static int check_for_next_lock(struct obd_device *obd)
1623 cfs_spin_lock(&obd->obd_recovery_task_lock);
1624 if (!cfs_list_empty(&obd->obd_lock_replay_queue)) {
1625 CDEBUG(D_HA, "waking for next lock\n");
1627 } else if (cfs_atomic_read(&obd->obd_lock_replay_clients) == 0) {
1628 CDEBUG(D_HA, "waking for completed lock replay\n");
1630 } else if (obd->obd_abort_recovery) {
1631 CDEBUG(D_HA, "waking for aborted recovery\n");
1633 } else if (obd->obd_recovery_expired) {
1634 CDEBUG(D_HA, "waking for expired recovery\n");
1637 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1643 * wait for recovery events,
1644 * check its status with help of check_routine
1645 * evict dead clients via health_check
1647 static int target_recovery_overseer(struct obd_device *obd,
1648 int (*check_routine)(struct obd_device *),
1649 int (*health_check)(struct obd_export *))
1652 cfs_wait_event(obd->obd_next_transno_waitq, check_routine(obd));
1653 if (obd->obd_abort_recovery) {
1654 CWARN("recovery is aborted, evict exports in recovery\n");
1655 /** evict exports which didn't finish recovery yet */
1656 class_disconnect_stale_exports(obd, exp_finished);
1658 } else if (obd->obd_recovery_expired) {
1659 obd->obd_recovery_expired = 0;
1660 /** If some clients died being recovered, evict them */
1662 "recovery is timed out, evict stale exports\n");
1663 /** evict cexports with no replay in queue, they are stalled */
1664 class_disconnect_stale_exports(obd, health_check);
1665 /** continue with VBR */
1666 cfs_spin_lock(&obd->obd_dev_lock);
1667 obd->obd_version_recov = 1;
1668 cfs_spin_unlock(&obd->obd_dev_lock);
1670 * reset timer, recovery will proceed with versions now,
1671 * timeout is set just to handle reconnection delays
1673 extend_recovery_timer(obd, RECONNECT_DELAY_MAX, true);
1674 /** Wait for recovery events again, after evicting bad clients */
1680 static struct ptlrpc_request *target_next_replay_req(struct obd_device *obd)
1682 struct ptlrpc_request *req = NULL;
1685 CDEBUG(D_HA, "Waiting for transno "LPD64"\n",
1686 obd->obd_next_recovery_transno);
1688 if (target_recovery_overseer(obd, check_for_next_transno,
1689 exp_req_replay_healthy)) {
1690 abort_req_replay_queue(obd);
1691 abort_lock_replay_queue(obd);
1694 cfs_spin_lock(&obd->obd_recovery_task_lock);
1695 if (!cfs_list_empty(&obd->obd_req_replay_queue)) {
1696 req = cfs_list_entry(obd->obd_req_replay_queue.next,
1697 struct ptlrpc_request, rq_list);
1698 cfs_list_del_init(&req->rq_list);
1699 obd->obd_requests_queued_for_recovery--;
1700 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1702 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1703 LASSERT(cfs_list_empty(&obd->obd_req_replay_queue));
1704 LASSERT(cfs_atomic_read(&obd->obd_req_replay_clients) == 0);
1705 /** evict exports failed VBR */
1706 class_disconnect_stale_exports(obd, exp_vbr_healthy);
1711 static struct ptlrpc_request *target_next_replay_lock(struct obd_device *obd)
1713 struct ptlrpc_request *req = NULL;
1715 CDEBUG(D_HA, "Waiting for lock\n");
1716 if (target_recovery_overseer(obd, check_for_next_lock,
1717 exp_lock_replay_healthy))
1718 abort_lock_replay_queue(obd);
1720 cfs_spin_lock(&obd->obd_recovery_task_lock);
1721 if (!cfs_list_empty(&obd->obd_lock_replay_queue)) {
1722 req = cfs_list_entry(obd->obd_lock_replay_queue.next,
1723 struct ptlrpc_request, rq_list);
1724 cfs_list_del_init(&req->rq_list);
1725 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1727 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1728 LASSERT(cfs_list_empty(&obd->obd_lock_replay_queue));
1729 LASSERT(cfs_atomic_read(&obd->obd_lock_replay_clients) == 0);
1730 /** evict exports failed VBR */
1731 class_disconnect_stale_exports(obd, exp_vbr_healthy);
1736 static struct ptlrpc_request *target_next_final_ping(struct obd_device *obd)
1738 struct ptlrpc_request *req = NULL;
1740 cfs_spin_lock(&obd->obd_recovery_task_lock);
1741 if (!cfs_list_empty(&obd->obd_final_req_queue)) {
1742 req = cfs_list_entry(obd->obd_final_req_queue.next,
1743 struct ptlrpc_request, rq_list);
1744 cfs_list_del_init(&req->rq_list);
1745 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1746 if (req->rq_export->exp_in_recovery) {
1747 cfs_spin_lock(&req->rq_export->exp_lock);
1748 req->rq_export->exp_in_recovery = 0;
1749 cfs_spin_unlock(&req->rq_export->exp_lock);
1752 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1757 static int handle_recovery_req(struct ptlrpc_thread *thread,
1758 struct ptlrpc_request *req,
1759 svc_handler_t handler)
1765 * export can be evicted during recovery, no need to handle replays for
1766 * it after that, discard such request silently
1768 if (req->rq_export->exp_disconnected)
1769 GOTO(reqcopy_put, rc = 0);
1771 rc = lu_context_init(&req->rq_recov_session, LCT_SESSION);
1773 CERROR("Failure to initialize session: %d\n", rc);
1774 GOTO(reqcopy_put, rc);
1777 req->rq_recov_session.lc_thread = thread;
1778 lu_context_enter(&req->rq_recov_session);
1779 req->rq_svc_thread = thread;
1780 req->rq_svc_thread->t_env->le_ses = &req->rq_recov_session;
1782 /* thread context */
1783 lu_context_enter(&thread->t_env->le_ctx);
1785 lu_context_exit(&thread->t_env->le_ctx);
1787 lu_context_exit(&req->rq_recov_session);
1788 lu_context_fini(&req->rq_recov_session);
1789 /* don't reset timer for final stage */
1790 if (!exp_finished(req->rq_export)) {
1791 int to = obd_timeout;
1794 * Add request timeout to the recovery time so next request from
1795 * this client may come in recovery time
1798 struct ptlrpc_service *svc = req->rq_rqbd->rqbd_service;
1799 /* If the server sent early reply for this request,
1800 * the client will recalculate the timeout according to
1801 * current server estimate service time, so we will
1802 * use the maxium timeout here for waiting the client
1803 * sending the next req */
1804 to = max((int)at_est2timeout(
1805 at_get(&svc->srv_at_estimate)),
1806 (int)lustre_msg_get_timeout(req->rq_reqmsg));
1807 /* Add net_latency (see ptlrpc_replay_req) */
1808 to += lustre_msg_get_service_time(req->rq_reqmsg);
1810 extend_recovery_timer(class_exp2obd(req->rq_export), to, true);
1816 static int target_recovery_thread(void *arg)
1818 struct lu_target *lut = arg;
1819 struct obd_device *obd = lut->lut_obd;
1820 struct ptlrpc_request *req;
1821 struct target_recovery_data *trd = &obd->obd_recovery_data;
1822 unsigned long delta;
1823 unsigned long flags;
1825 struct ptlrpc_thread *thread = NULL;
1829 cfs_daemonize_ctxt("tgt_recov");
1831 SIGNAL_MASK_LOCK(current, flags);
1832 sigfillset(¤t->blocked);
1834 SIGNAL_MASK_UNLOCK(current, flags);
1836 OBD_ALLOC_PTR(thread);
1842 OBD_FREE_PTR(thread);
1846 rc = lu_context_init(&env->le_ctx, LCT_MD_THREAD);
1848 OBD_FREE_PTR(thread);
1853 thread->t_env = env;
1854 thread->t_id = -1; /* force filter_iobuf_get/put to use local buffers */
1855 env->le_ctx.lc_thread = thread;
1856 thread->t_data = NULL;
1857 thread->t_watchdog = NULL;
1859 CDEBUG(D_HA, "%s: started recovery thread pid %d\n", obd->obd_name,
1861 trd->trd_processing_task = cfs_curproc_pid();
1863 cfs_spin_lock(&obd->obd_dev_lock);
1864 obd->obd_recovering = 1;
1865 cfs_spin_unlock(&obd->obd_dev_lock);
1866 cfs_complete(&trd->trd_starting);
1868 /* first of all, we have to know the first transno to replay */
1869 if (target_recovery_overseer(obd, check_for_clients,
1870 exp_connect_healthy)) {
1871 abort_req_replay_queue(obd);
1872 abort_lock_replay_queue(obd);
1875 /* next stage: replay requests */
1877 CDEBUG(D_INFO, "1: request replay stage - %d clients from t"LPU64"\n",
1878 cfs_atomic_read(&obd->obd_req_replay_clients),
1879 obd->obd_next_recovery_transno);
1880 while ((req = target_next_replay_req(obd))) {
1881 LASSERT(trd->trd_processing_task == cfs_curproc_pid());
1882 DEBUG_REQ(D_HA, req, "processing t"LPD64" from %s",
1883 lustre_msg_get_transno(req->rq_reqmsg),
1884 libcfs_nid2str(req->rq_peer.nid));
1885 handle_recovery_req(thread, req,
1886 trd->trd_recovery_handler);
1888 * bz18031: increase next_recovery_transno before
1889 * target_request_copy_put() will drop exp_rpc reference
1891 cfs_spin_lock(&obd->obd_recovery_task_lock);
1892 obd->obd_next_recovery_transno++;
1893 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1894 target_exp_dequeue_req_replay(req);
1895 target_request_copy_put(req);
1896 obd->obd_replayed_requests++;
1900 * The second stage: replay locks
1902 CDEBUG(D_INFO, "2: lock replay stage - %d clients\n",
1903 cfs_atomic_read(&obd->obd_lock_replay_clients));
1904 while ((req = target_next_replay_lock(obd))) {
1905 LASSERT(trd->trd_processing_task == cfs_curproc_pid());
1906 DEBUG_REQ(D_HA, req, "processing lock from %s: ",
1907 libcfs_nid2str(req->rq_peer.nid));
1908 handle_recovery_req(thread, req,
1909 trd->trd_recovery_handler);
1910 target_request_copy_put(req);
1911 obd->obd_replayed_locks++;
1915 * The third stage: reply on final pings, at this moment all clients
1916 * must have request in final queue
1918 CDEBUG(D_INFO, "3: final stage - process recovery completion pings\n");
1919 /** Update server last boot epoch */
1920 lut_boot_epoch_update(lut);
1921 /* We drop recoverying flag to forward all new requests
1922 * to regular mds_handle() since now */
1923 cfs_spin_lock(&obd->obd_dev_lock);
1924 obd->obd_recovering = obd->obd_abort_recovery = 0;
1925 cfs_spin_unlock(&obd->obd_dev_lock);
1926 cfs_spin_lock(&obd->obd_recovery_task_lock);
1927 target_cancel_recovery_timer(obd);
1928 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1929 while ((req = target_next_final_ping(obd))) {
1930 LASSERT(trd->trd_processing_task == cfs_curproc_pid());
1931 DEBUG_REQ(D_HA, req, "processing final ping from %s: ",
1932 libcfs_nid2str(req->rq_peer.nid));
1933 handle_recovery_req(thread, req,
1934 trd->trd_recovery_handler);
1935 target_request_copy_put(req);
1938 delta = (jiffies - delta) / CFS_HZ;
1939 CDEBUG(D_INFO,"4: recovery completed in %lus - %d/%d reqs/locks\n",
1940 delta, obd->obd_replayed_requests, obd->obd_replayed_locks);
1941 if (delta > OBD_RECOVERY_TIME_SOFT) {
1942 CWARN("too long recovery - read logs\n");
1943 libcfs_debug_dumplog();
1946 target_finish_recovery(obd);
1948 lu_context_fini(&env->le_ctx);
1949 trd->trd_processing_task = 0;
1950 cfs_complete(&trd->trd_finishing);
1952 OBD_FREE_PTR(thread);
1957 static int target_start_recovery_thread(struct lu_target *lut,
1958 svc_handler_t handler)
1960 struct obd_device *obd = lut->lut_obd;
1962 struct target_recovery_data *trd = &obd->obd_recovery_data;
1964 memset(trd, 0, sizeof(*trd));
1965 cfs_init_completion(&trd->trd_starting);
1966 cfs_init_completion(&trd->trd_finishing);
1967 trd->trd_recovery_handler = handler;
1969 if (cfs_create_thread(target_recovery_thread, lut, 0) > 0) {
1970 cfs_wait_for_completion(&trd->trd_starting);
1971 LASSERT(obd->obd_recovering != 0);
1978 void target_stop_recovery_thread(struct obd_device *obd)
1980 if (obd->obd_recovery_data.trd_processing_task > 0) {
1981 struct target_recovery_data *trd = &obd->obd_recovery_data;
1982 /** recovery can be done but postrecovery is not yet */
1983 cfs_spin_lock(&obd->obd_dev_lock);
1984 if (obd->obd_recovering) {
1985 CERROR("%s: Aborting recovery\n", obd->obd_name);
1986 obd->obd_abort_recovery = 1;
1987 cfs_waitq_signal(&obd->obd_next_transno_waitq);
1989 cfs_spin_unlock(&obd->obd_dev_lock);
1990 cfs_wait_for_completion(&trd->trd_finishing);
1994 void target_recovery_fini(struct obd_device *obd)
1996 class_disconnect_exports(obd);
1997 target_stop_recovery_thread(obd);
1998 target_cleanup_recovery(obd);
2000 EXPORT_SYMBOL(target_recovery_fini);
2002 static void target_recovery_expired(unsigned long castmeharder)
2004 struct obd_device *obd = (struct obd_device *)castmeharder;
2005 CDEBUG(D_HA, "%s: recovery timed out; %d clients are still in recovery"
2006 " after %lds (%d clients connected)\n",
2007 obd->obd_name, cfs_atomic_read(&obd->obd_lock_replay_clients),
2008 cfs_time_current_sec()- obd->obd_recovery_start,
2009 cfs_atomic_read(&obd->obd_connected_clients));
2011 obd->obd_recovery_expired = 1;
2012 cfs_waitq_signal(&obd->obd_next_transno_waitq);
2015 void target_recovery_init(struct lu_target *lut, svc_handler_t handler)
2017 struct obd_device *obd = lut->lut_obd;
2018 if (obd->obd_max_recoverable_clients == 0) {
2019 /** Update server last boot epoch */
2020 lut_boot_epoch_update(lut);
2024 CWARN("RECOVERY: service %s, %d recoverable clients, "
2025 "last_transno "LPU64"\n", obd->obd_name,
2026 obd->obd_max_recoverable_clients, obd->obd_last_committed);
2027 LASSERT(obd->obd_stopping == 0);
2028 obd->obd_next_recovery_transno = obd->obd_last_committed + 1;
2029 obd->obd_recovery_start = 0;
2030 obd->obd_recovery_end = 0;
2032 cfs_timer_init(&obd->obd_recovery_timer, target_recovery_expired, obd);
2033 target_start_recovery_thread(lut, handler);
2035 EXPORT_SYMBOL(target_recovery_init);
2039 static int target_process_req_flags(struct obd_device *obd,
2040 struct ptlrpc_request *req)
2042 struct obd_export *exp = req->rq_export;
2043 LASSERT(exp != NULL);
2044 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REQ_REPLAY_DONE) {
2045 /* client declares he's ready to replay locks */
2046 cfs_spin_lock(&exp->exp_lock);
2047 if (exp->exp_req_replay_needed) {
2048 exp->exp_req_replay_needed = 0;
2049 cfs_spin_unlock(&exp->exp_lock);
2051 LASSERT_ATOMIC_POS(&obd->obd_req_replay_clients);
2052 cfs_atomic_dec(&obd->obd_req_replay_clients);
2054 cfs_spin_unlock(&exp->exp_lock);
2057 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_LOCK_REPLAY_DONE) {
2058 /* client declares he's ready to complete recovery
2059 * so, we put the request on th final queue */
2060 cfs_spin_lock(&exp->exp_lock);
2061 if (exp->exp_lock_replay_needed) {
2062 exp->exp_lock_replay_needed = 0;
2063 cfs_spin_unlock(&exp->exp_lock);
2065 LASSERT_ATOMIC_POS(&obd->obd_lock_replay_clients);
2066 cfs_atomic_dec(&obd->obd_lock_replay_clients);
2068 cfs_spin_unlock(&exp->exp_lock);
2074 int target_queue_recovery_request(struct ptlrpc_request *req,
2075 struct obd_device *obd)
2079 __u64 transno = lustre_msg_get_transno(req->rq_reqmsg);
2082 if (obd->obd_recovery_data.trd_processing_task == cfs_curproc_pid()) {
2083 /* Processing the queue right now, don't re-add. */
2087 target_process_req_flags(obd, req);
2089 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_LOCK_REPLAY_DONE) {
2090 /* client declares he's ready to complete recovery
2091 * so, we put the request on th final queue */
2092 target_request_copy_get(req);
2093 DEBUG_REQ(D_HA, req, "queue final req");
2094 cfs_waitq_signal(&obd->obd_next_transno_waitq);
2095 cfs_spin_lock(&obd->obd_recovery_task_lock);
2096 if (obd->obd_recovering) {
2097 cfs_list_add_tail(&req->rq_list,
2098 &obd->obd_final_req_queue);
2100 cfs_spin_unlock(&obd->obd_recovery_task_lock);
2101 target_request_copy_put(req);
2102 RETURN(obd->obd_stopping ? -ENOTCONN : 1);
2104 cfs_spin_unlock(&obd->obd_recovery_task_lock);
2107 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REQ_REPLAY_DONE) {
2108 /* client declares he's ready to replay locks */
2109 target_request_copy_get(req);
2110 DEBUG_REQ(D_HA, req, "queue lock replay req");
2111 cfs_waitq_signal(&obd->obd_next_transno_waitq);
2112 cfs_spin_lock(&obd->obd_recovery_task_lock);
2113 LASSERT(obd->obd_recovering);
2114 /* usually due to recovery abort */
2115 if (!req->rq_export->exp_in_recovery) {
2116 cfs_spin_unlock(&obd->obd_recovery_task_lock);
2117 target_request_copy_put(req);
2120 LASSERT(req->rq_export->exp_lock_replay_needed);
2121 cfs_list_add_tail(&req->rq_list, &obd->obd_lock_replay_queue);
2122 cfs_spin_unlock(&obd->obd_recovery_task_lock);
2126 /* CAVEAT EMPTOR: The incoming request message has been swabbed
2127 * (i.e. buflens etc are in my own byte order), but type-dependent
2128 * buffers (eg mdt_body, ost_body etc) have NOT been swabbed. */
2131 CFS_INIT_LIST_HEAD(&req->rq_list);
2132 DEBUG_REQ(D_HA, req, "not queueing");
2136 /* If we're processing the queue, we want don't want to queue this
2139 * Also, if this request has a transno less than the one we're waiting
2140 * for, we should process it now. It could (and currently always will)
2141 * be an open request for a descriptor that was opened some time ago.
2143 * Also, a resent, replayed request that has already been
2144 * handled will pass through here and be processed immediately.
2146 CWARN("Next recovery transno: "LPU64", current: "LPU64", replaying\n",
2147 obd->obd_next_recovery_transno, transno);
2148 cfs_spin_lock(&obd->obd_recovery_task_lock);
2149 if (transno < obd->obd_next_recovery_transno) {
2150 /* Processing the queue right now, don't re-add. */
2151 LASSERT(cfs_list_empty(&req->rq_list));
2152 cfs_spin_unlock(&obd->obd_recovery_task_lock);
2155 cfs_spin_unlock(&obd->obd_recovery_task_lock);
2157 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_REPLAY_DROP))
2160 target_request_copy_get(req);
2161 if (!req->rq_export->exp_in_recovery) {
2162 target_request_copy_put(req);
2165 LASSERT(req->rq_export->exp_req_replay_needed);
2167 if (target_exp_enqueue_req_replay(req)) {
2168 DEBUG_REQ(D_ERROR, req, "dropping resent queued req");
2169 target_request_copy_put(req);
2174 cfs_spin_lock(&obd->obd_recovery_task_lock);
2175 LASSERT(obd->obd_recovering);
2176 cfs_list_for_each(tmp, &obd->obd_req_replay_queue) {
2177 struct ptlrpc_request *reqiter =
2178 cfs_list_entry(tmp, struct ptlrpc_request, rq_list);
2180 if (lustre_msg_get_transno(reqiter->rq_reqmsg) > transno) {
2181 cfs_list_add_tail(&req->rq_list, &reqiter->rq_list);
2186 if (unlikely(lustre_msg_get_transno(reqiter->rq_reqmsg) ==
2188 DEBUG_REQ(D_ERROR, req, "dropping replay: transno "
2189 "has been claimed by another client");
2190 cfs_spin_unlock(&obd->obd_recovery_task_lock);
2191 target_exp_dequeue_req_replay(req);
2192 target_request_copy_put(req);
2198 cfs_list_add_tail(&req->rq_list, &obd->obd_req_replay_queue);
2200 obd->obd_requests_queued_for_recovery++;
2201 cfs_spin_unlock(&obd->obd_recovery_task_lock);
2202 cfs_waitq_signal(&obd->obd_next_transno_waitq);
2207 * Packs current SLV and Limit into \a req.
2209 int target_pack_pool_reply(struct ptlrpc_request *req)
2211 struct obd_device *obd;
2215 * Check that we still have all structures alive as this may
2216 * be some late rpc in shutdown time.
2218 if (unlikely(!req->rq_export || !req->rq_export->exp_obd ||
2219 !exp_connect_lru_resize(req->rq_export))) {
2220 lustre_msg_set_slv(req->rq_repmsg, 0);
2221 lustre_msg_set_limit(req->rq_repmsg, 0);
2226 * OBD is alive here as export is alive, which we checked above.
2228 obd = req->rq_export->exp_obd;
2230 cfs_read_lock(&obd->obd_pool_lock);
2231 lustre_msg_set_slv(req->rq_repmsg, obd->obd_pool_slv);
2232 lustre_msg_set_limit(req->rq_repmsg, obd->obd_pool_limit);
2233 cfs_read_unlock(&obd->obd_pool_lock);
2238 int target_send_reply_msg(struct ptlrpc_request *req, int rc, int fail_id)
2240 if (OBD_FAIL_CHECK_ORSET(fail_id & ~OBD_FAIL_ONCE, OBD_FAIL_ONCE)) {
2241 DEBUG_REQ(D_ERROR, req, "dropping reply");
2246 DEBUG_REQ(D_ERROR, req, "processing error (%d)", rc);
2247 req->rq_status = rc;
2248 return (ptlrpc_send_error(req, 1));
2250 DEBUG_REQ(D_NET, req, "sending reply");
2253 return (ptlrpc_send_reply(req, PTLRPC_REPLY_MAYBE_DIFFICULT));
2256 void target_send_reply(struct ptlrpc_request *req, int rc, int fail_id)
2259 struct ptlrpc_reply_state *rs;
2260 struct obd_export *exp;
2261 struct ptlrpc_service *svc;
2264 if (req->rq_no_reply) {
2269 svc = req->rq_rqbd->rqbd_service;
2270 rs = req->rq_reply_state;
2271 if (rs == NULL || !rs->rs_difficult) {
2273 target_send_reply_msg (req, rc, fail_id);
2278 /* must be an export if locks saved */
2279 LASSERT (req->rq_export != NULL);
2280 /* req/reply consistent */
2281 LASSERT (rs->rs_service == svc);
2284 LASSERT (!rs->rs_scheduled);
2285 LASSERT (!rs->rs_scheduled_ever);
2286 LASSERT (!rs->rs_handled);
2287 LASSERT (!rs->rs_on_net);
2288 LASSERT (rs->rs_export == NULL);
2289 LASSERT (cfs_list_empty(&rs->rs_obd_list));
2290 LASSERT (cfs_list_empty(&rs->rs_exp_list));
2292 exp = class_export_get (req->rq_export);
2294 /* disable reply scheduling while I'm setting up */
2295 rs->rs_scheduled = 1;
2297 rs->rs_xid = req->rq_xid;
2298 rs->rs_transno = req->rq_transno;
2299 rs->rs_export = exp;
2300 rs->rs_opc = lustre_msg_get_opc(rs->rs_msg);
2302 cfs_spin_lock(&exp->exp_uncommitted_replies_lock);
2303 CDEBUG(D_NET, "rs transno = "LPU64", last committed = "LPU64"\n",
2304 rs->rs_transno, exp->exp_last_committed);
2305 if (rs->rs_transno > exp->exp_last_committed) {
2306 /* not committed already */
2307 cfs_list_add_tail(&rs->rs_obd_list,
2308 &exp->exp_uncommitted_replies);
2310 cfs_spin_unlock (&exp->exp_uncommitted_replies_lock);
2312 cfs_spin_lock(&exp->exp_lock);
2313 cfs_list_add_tail(&rs->rs_exp_list, &exp->exp_outstanding_replies);
2314 cfs_spin_unlock(&exp->exp_lock);
2316 netrc = target_send_reply_msg (req, rc, fail_id);
2318 cfs_spin_lock(&svc->srv_rs_lock);
2320 cfs_atomic_inc(&svc->srv_n_difficult_replies);
2323 /* error sending: reply is off the net. Also we need +1
2324 * reply ref until ptlrpc_handle_rs() is done
2325 * with the reply state (if the send was successful, there
2326 * would have been +1 ref for the net, which
2327 * reply_out_callback leaves alone) */
2329 ptlrpc_rs_addref(rs);
2332 cfs_spin_lock(&rs->rs_lock);
2333 if (rs->rs_transno <= exp->exp_last_committed ||
2334 (!rs->rs_on_net && !rs->rs_no_ack) ||
2335 cfs_list_empty(&rs->rs_exp_list) || /* completed already */
2336 cfs_list_empty(&rs->rs_obd_list)) {
2337 CDEBUG(D_HA, "Schedule reply immediately\n");
2338 ptlrpc_dispatch_difficult_reply(rs);
2340 cfs_list_add (&rs->rs_list, &svc->srv_active_replies);
2341 rs->rs_scheduled = 0; /* allow notifier to schedule */
2343 cfs_spin_unlock(&rs->rs_lock);
2344 cfs_spin_unlock(&svc->srv_rs_lock);
2348 int target_handle_ping(struct ptlrpc_request *req)
2350 obd_ping(req->rq_export);
2351 return req_capsule_server_pack(&req->rq_pill);
2354 void target_committed_to_req(struct ptlrpc_request *req)
2356 struct obd_export *exp = req->rq_export;
2358 if (!exp->exp_obd->obd_no_transno && req->rq_repmsg != NULL)
2359 lustre_msg_set_last_committed(req->rq_repmsg,
2360 exp->exp_last_committed);
2362 DEBUG_REQ(D_IOCTL, req, "not sending last_committed update (%d/"
2363 "%d)", exp->exp_obd->obd_no_transno,
2364 req->rq_repmsg == NULL);
2366 CDEBUG(D_INFO, "last_committed "LPU64", transno "LPU64", xid "LPU64"\n",
2367 exp->exp_last_committed, req->rq_transno, req->rq_xid);
2369 EXPORT_SYMBOL(target_committed_to_req);
2371 int target_handle_qc_callback(struct ptlrpc_request *req)
2373 struct obd_quotactl *oqctl;
2374 struct client_obd *cli = &req->rq_export->exp_obd->u.cli;
2376 oqctl = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
2377 if (oqctl == NULL) {
2378 CERROR("Can't unpack obd_quotactl\n");
2382 cli->cl_qchk_stat = oqctl->qc_stat;
2387 #ifdef HAVE_QUOTA_SUPPORT
2388 int target_handle_dqacq_callback(struct ptlrpc_request *req)
2391 struct obd_device *obd = req->rq_export->exp_obd;
2392 struct obd_device *master_obd = NULL, *lov_obd = NULL;
2393 struct obd_device_target *obt;
2394 struct lustre_quota_ctxt *qctxt;
2395 struct qunit_data *qdata = NULL;
2399 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DROP_QUOTA_REQ))
2402 rc = req_capsule_server_pack(&req->rq_pill);
2404 CERROR("packing reply failed!: rc = %d\n", rc);
2408 LASSERT(req->rq_export);
2410 qdata = quota_get_qdata(req, QUOTA_REQUEST, QUOTA_EXPORT);
2411 if (IS_ERR(qdata)) {
2412 rc = PTR_ERR(qdata);
2413 CDEBUG(D_ERROR, "Can't unpack qunit_data(rc: %d)\n", rc);
2414 req->rq_status = rc;
2418 /* we use the observer */
2419 if (obd_pin_observer(obd, &lov_obd) ||
2420 obd_pin_observer(lov_obd, &master_obd)) {
2421 CERROR("Can't find the observer, it is recovering\n");
2422 req->rq_status = -EAGAIN;
2426 obt = &master_obd->u.obt;
2427 qctxt = &obt->obt_qctxt;
2429 if (!qctxt->lqc_setup || !qctxt->lqc_valid) {
2430 /* quota_type has not been processed yet, return EAGAIN
2431 * until we know whether or not quotas are supposed to
2433 CDEBUG(D_QUOTA, "quota_type not processed yet, return "
2435 req->rq_status = -EAGAIN;
2439 cfs_down_read(&obt->obt_rwsem);
2440 if (qctxt->lqc_lqs_hash == NULL) {
2441 cfs_up_read(&obt->obt_rwsem);
2442 /* quota_type has not been processed yet, return EAGAIN
2443 * until we know whether or not quotas are supposed to
2445 CDEBUG(D_QUOTA, "quota_ctxt is not ready yet, return "
2447 req->rq_status = -EAGAIN;
2451 LASSERT(qctxt->lqc_handler);
2452 rc = qctxt->lqc_handler(master_obd, qdata,
2453 lustre_msg_get_opc(req->rq_reqmsg));
2454 cfs_up_read(&obt->obt_rwsem);
2455 if (rc && rc != -EDQUOT)
2456 CDEBUG(rc == -EBUSY ? D_QUOTA : D_ERROR,
2457 "dqacq/dqrel failed! (rc:%d)\n", rc);
2458 req->rq_status = rc;
2460 rc = quota_copy_qdata(req, qdata, QUOTA_REPLY, QUOTA_EXPORT);
2462 CERROR("Can't pack qunit_data(rc: %d)\n", rc);
2466 /* Block the quota req. b=14840 */
2467 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_BLOCK_QUOTA_REQ, obd_timeout);
2472 obd_unpin_observer(lov_obd);
2474 obd_unpin_observer(obd);
2476 rc = ptlrpc_reply(req);
2480 #endif /* !__KERNEL__ */
2482 #endif /* HAVE_QUOTA_SUPPORT */
2484 ldlm_mode_t lck_compat_array[] = {
2485 [LCK_EX] LCK_COMPAT_EX,
2486 [LCK_PW] LCK_COMPAT_PW,
2487 [LCK_PR] LCK_COMPAT_PR,
2488 [LCK_CW] LCK_COMPAT_CW,
2489 [LCK_CR] LCK_COMPAT_CR,
2490 [LCK_NL] LCK_COMPAT_NL,
2491 [LCK_GROUP] LCK_COMPAT_GROUP,
2492 [LCK_COS] LCK_COMPAT_COS,
2496 * Rather arbitrary mapping from LDLM error codes to errno values. This should
2497 * not escape to the user level.
2499 int ldlm_error2errno(ldlm_error_t error)
2507 case ELDLM_LOCK_CHANGED:
2510 case ELDLM_LOCK_ABORTED:
2513 case ELDLM_LOCK_REPLACED:
2516 case ELDLM_NO_LOCK_DATA:
2519 case ELDLM_NAMESPACE_EXISTS:
2522 case ELDLM_BAD_NAMESPACE:
2526 if (((int)error) < 0) /* cast to signed type */
2527 result = error; /* as ldlm_error_t can be unsigned */
2529 CERROR("Invalid DLM result code: %d\n", error);
2535 EXPORT_SYMBOL(ldlm_error2errno);
2538 * Dual to ldlm_error2errno(): maps errno values back to ldlm_error_t.
2540 ldlm_error_t ldlm_errno2error(int err_no)
2549 error = ELDLM_LOCK_CHANGED;
2552 error = ELDLM_LOCK_ABORTED;
2555 error = ELDLM_LOCK_REPLACED;
2558 error = ELDLM_NO_LOCK_DATA;
2561 error = ELDLM_NAMESPACE_EXISTS;
2564 error = ELDLM_BAD_NAMESPACE;
2571 EXPORT_SYMBOL(ldlm_errno2error);
2573 #if LUSTRE_TRACKS_LOCK_EXP_REFS
2574 void ldlm_dump_export_locks(struct obd_export *exp)
2576 cfs_spin_lock(&exp->exp_locks_list_guard);
2577 if (!cfs_list_empty(&exp->exp_locks_list)) {
2578 struct ldlm_lock *lock;
2580 CERROR("dumping locks for export %p,"
2581 "ignore if the unmount doesn't hang\n", exp);
2582 cfs_list_for_each_entry(lock, &exp->exp_locks_list, l_exp_refs_link)
2583 ldlm_lock_dump(D_ERROR, lock, 0);
2585 cfs_spin_unlock(&exp->exp_locks_list_guard);
2589 static int target_bulk_timeout(void *data)
2592 /* We don't fail the connection here, because having the export
2593 * killed makes the (vital) call to commitrw very sad.
2598 static inline char *bulk2type(struct ptlrpc_bulk_desc *desc)
2600 return desc->bd_type == BULK_GET_SINK ? "GET" : "PUT";
2603 int target_bulk_io(struct obd_export *exp, struct ptlrpc_bulk_desc *desc,
2604 struct l_wait_info *lwi)
2606 struct ptlrpc_request *req = desc->bd_req;
2610 /* Check if there is eviction in progress, and if so, wait for
2612 if (unlikely(cfs_atomic_read(&exp->exp_obd->obd_evict_inprogress))) {
2613 *lwi = LWI_INTR(NULL, NULL);
2614 rc = l_wait_event(exp->exp_obd->obd_evict_inprogress_waitq,
2615 !cfs_atomic_read(&exp->exp_obd->
2616 obd_evict_inprogress),
2620 /* Check if client was evicted or tried to reconnect already */
2621 if (exp->exp_failed || exp->exp_abort_active_req) {
2624 if (desc->bd_type == BULK_PUT_SINK)
2625 rc = sptlrpc_svc_wrap_bulk(req, desc);
2627 rc = ptlrpc_start_bulk_transfer(desc);
2630 if (rc == 0 && OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE)) {
2631 ptlrpc_abort_bulk(desc);
2632 } else if (rc == 0) {
2633 time_t start = cfs_time_current_sec();
2635 long timeoutl = req->rq_deadline - cfs_time_current_sec();
2636 cfs_duration_t timeout = timeoutl <= 0 ?
2637 CFS_TICK : cfs_time_seconds(timeoutl);
2638 *lwi = LWI_TIMEOUT_INTERVAL(timeout,
2639 cfs_time_seconds(1),
2640 target_bulk_timeout,
2642 rc = l_wait_event(desc->bd_waitq,
2643 !ptlrpc_server_bulk_active(desc) ||
2645 exp->exp_abort_active_req,
2647 LASSERT(rc == 0 || rc == -ETIMEDOUT);
2648 /* Wait again if we changed deadline */
2649 } while ((rc == -ETIMEDOUT) &&
2650 (req->rq_deadline > cfs_time_current_sec()));
2652 if (rc == -ETIMEDOUT) {
2653 DEBUG_REQ(D_ERROR, req,
2654 "timeout on bulk %s after %ld%+lds",
2656 req->rq_deadline - start,
2657 cfs_time_current_sec() -
2659 ptlrpc_abort_bulk(desc);
2660 } else if (exp->exp_failed) {
2661 DEBUG_REQ(D_ERROR, req, "Eviction on bulk %s",
2664 ptlrpc_abort_bulk(desc);
2665 } else if (exp->exp_abort_active_req) {
2666 DEBUG_REQ(D_ERROR, req, "Reconnect on bulk %s",
2668 /* we don't reply anyway */
2670 ptlrpc_abort_bulk(desc);
2671 } else if (!desc->bd_success ||
2672 desc->bd_nob_transferred != desc->bd_nob) {
2673 DEBUG_REQ(D_ERROR, req, "%s bulk %s %d(%d)",
2675 "truncated" : "network error on",
2677 desc->bd_nob_transferred,
2679 /* XXX should this be a different errno? */
2681 } else if (desc->bd_type == BULK_GET_SINK) {
2682 rc = sptlrpc_svc_unwrap_bulk(req, desc);
2685 DEBUG_REQ(D_ERROR, req, "bulk %s failed: rc %d",
2686 bulk2type(desc), rc);
2691 EXPORT_SYMBOL(target_bulk_io);