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);
230 EXPORT_SYMBOL(client_destroy_import);
232 /* configure an RPC client OBD device
237 * 3 - inactive-on-startup
239 int client_obd_setup(struct obd_device *obddev, struct lustre_cfg *lcfg)
241 struct client_obd *cli = &obddev->u.cli;
242 struct obd_import *imp;
243 struct obd_uuid server_uuid;
244 int rq_portal, rp_portal, connect_op;
245 char *name = obddev->obd_type->typ_name;
246 ldlm_ns_type_t ns_type = LDLM_NS_TYPE_UNKNOWN;
250 /* In a more perfect world, we would hang a ptlrpc_client off of
251 * obd_type and just use the values from there. */
252 if (!strcmp(name, LUSTRE_OSC_NAME)) {
253 rq_portal = OST_REQUEST_PORTAL;
254 rp_portal = OSC_REPLY_PORTAL;
255 connect_op = OST_CONNECT;
256 cli->cl_sp_me = LUSTRE_SP_CLI;
257 cli->cl_sp_to = LUSTRE_SP_OST;
258 ns_type = LDLM_NS_TYPE_OSC;
260 } else if (!strcmp(name, LUSTRE_MDC_NAME)) {
261 rq_portal = MDS_REQUEST_PORTAL;
262 rp_portal = MDC_REPLY_PORTAL;
263 connect_op = MDS_CONNECT;
264 cli->cl_sp_me = LUSTRE_SP_CLI;
265 cli->cl_sp_to = LUSTRE_SP_MDT;
266 ns_type = LDLM_NS_TYPE_MDC;
268 } else if (!strcmp(name, LUSTRE_MGC_NAME)) {
269 rq_portal = MGS_REQUEST_PORTAL;
270 rp_portal = MGC_REPLY_PORTAL;
271 connect_op = MGS_CONNECT;
272 cli->cl_sp_me = LUSTRE_SP_MGC;
273 cli->cl_sp_to = LUSTRE_SP_MGS;
274 cli->cl_flvr_mgc.sf_rpc = SPTLRPC_FLVR_INVALID;
275 ns_type = LDLM_NS_TYPE_MGC;
278 CERROR("unknown client OBD type \"%s\", can't setup\n",
283 if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
284 CERROR("requires a TARGET UUID\n");
288 if (LUSTRE_CFG_BUFLEN(lcfg, 1) > 37) {
289 CERROR("client UUID must be less than 38 characters\n");
293 if (LUSTRE_CFG_BUFLEN(lcfg, 2) < 1) {
294 CERROR("setup requires a SERVER UUID\n");
298 if (LUSTRE_CFG_BUFLEN(lcfg, 2) > 37) {
299 CERROR("target UUID must be less than 38 characters\n");
303 cfs_init_rwsem(&cli->cl_sem);
304 cfs_sema_init(&cli->cl_mgc_sem, 1);
305 cli->cl_conn_count = 0;
306 memcpy(server_uuid.uuid, lustre_cfg_buf(lcfg, 2),
307 min_t(unsigned int, LUSTRE_CFG_BUFLEN(lcfg, 2),
308 sizeof(server_uuid)));
311 cli->cl_avail_grant = 0;
312 /* FIXME: should limit this for the sum of all cl_dirty_max */
313 cli->cl_dirty_max = OSC_MAX_DIRTY_DEFAULT * 1024 * 1024;
314 if (cli->cl_dirty_max >> CFS_PAGE_SHIFT > cfs_num_physpages / 8)
315 cli->cl_dirty_max = cfs_num_physpages << (CFS_PAGE_SHIFT - 3);
316 CFS_INIT_LIST_HEAD(&cli->cl_cache_waiters);
317 CFS_INIT_LIST_HEAD(&cli->cl_loi_ready_list);
318 CFS_INIT_LIST_HEAD(&cli->cl_loi_hp_ready_list);
319 CFS_INIT_LIST_HEAD(&cli->cl_loi_write_list);
320 CFS_INIT_LIST_HEAD(&cli->cl_loi_read_list);
321 client_obd_list_lock_init(&cli->cl_loi_list_lock);
322 cli->cl_r_in_flight = 0;
323 cli->cl_w_in_flight = 0;
325 cfs_spin_lock_init(&cli->cl_read_rpc_hist.oh_lock);
326 cfs_spin_lock_init(&cli->cl_write_rpc_hist.oh_lock);
327 cfs_spin_lock_init(&cli->cl_read_page_hist.oh_lock);
328 cfs_spin_lock_init(&cli->cl_write_page_hist.oh_lock);
329 cfs_spin_lock_init(&cli->cl_read_offset_hist.oh_lock);
330 cfs_spin_lock_init(&cli->cl_write_offset_hist.oh_lock);
331 cfs_waitq_init(&cli->cl_destroy_waitq);
332 cfs_atomic_set(&cli->cl_destroy_in_flight, 0);
333 #ifdef ENABLE_CHECKSUM
334 /* Turn on checksumming by default. */
335 cli->cl_checksum = 1;
337 * The supported checksum types will be worked out at connect time
338 * Set cl_chksum* to CRC32 for now to avoid returning screwed info
341 cli->cl_cksum_type = cli->cl_supp_cksum_types = OBD_CKSUM_CRC32;
343 cfs_atomic_set(&cli->cl_resends, OSC_DEFAULT_RESENDS);
345 /* This value may be changed at connect time in
346 ptlrpc_connect_interpret. */
347 cli->cl_max_pages_per_rpc = min((int)PTLRPC_MAX_BRW_PAGES,
348 (int)(1024 * 1024 >> CFS_PAGE_SHIFT));
350 if (!strcmp(name, LUSTRE_MDC_NAME)) {
351 cli->cl_max_rpcs_in_flight = MDC_MAX_RIF_DEFAULT;
352 } else if (cfs_num_physpages >> (20 - CFS_PAGE_SHIFT) <= 128 /* MB */) {
353 cli->cl_max_rpcs_in_flight = 2;
354 } else if (cfs_num_physpages >> (20 - CFS_PAGE_SHIFT) <= 256 /* MB */) {
355 cli->cl_max_rpcs_in_flight = 3;
356 } else if (cfs_num_physpages >> (20 - CFS_PAGE_SHIFT) <= 512 /* MB */) {
357 cli->cl_max_rpcs_in_flight = 4;
359 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT;
364 CERROR("ldlm_get_ref failed: %d\n", rc);
368 ptlrpc_init_client(rq_portal, rp_portal, name,
369 &obddev->obd_ldlm_client);
371 imp = class_new_import(obddev);
373 GOTO(err_ldlm, rc = -ENOENT);
374 imp->imp_client = &obddev->obd_ldlm_client;
375 imp->imp_connect_op = connect_op;
376 CFS_INIT_LIST_HEAD(&imp->imp_pinger_chain);
377 memcpy(cli->cl_target_uuid.uuid, lustre_cfg_buf(lcfg, 1),
378 LUSTRE_CFG_BUFLEN(lcfg, 1));
379 class_import_put(imp);
381 rc = client_import_add_conn(imp, &server_uuid, 1);
383 CERROR("can't add initial connection\n");
384 GOTO(err_import, rc);
387 cli->cl_import = imp;
388 /* cli->cl_max_mds_{easize,cookiesize} updated by mdc_init_ea_size() */
389 cli->cl_max_mds_easize = sizeof(struct lov_mds_md_v3);
390 cli->cl_max_mds_cookiesize = sizeof(struct llog_cookie);
392 if (LUSTRE_CFG_BUFLEN(lcfg, 3) > 0) {
393 if (!strcmp(lustre_cfg_string(lcfg, 3), "inactive")) {
394 CDEBUG(D_HA, "marking %s %s->%s as inactive\n",
395 name, obddev->obd_name,
396 cli->cl_target_uuid.uuid);
397 cfs_spin_lock(&imp->imp_lock);
398 imp->imp_deactive = 1;
399 cfs_spin_unlock(&imp->imp_lock);
403 obddev->obd_namespace = ldlm_namespace_new(obddev, obddev->obd_name,
404 LDLM_NAMESPACE_CLIENT,
405 LDLM_NAMESPACE_GREEDY,
407 if (obddev->obd_namespace == NULL) {
408 CERROR("Unable to create client namespace - %s\n",
410 GOTO(err_import, rc = -ENOMEM);
413 cli->cl_qchk_stat = CL_NOT_QUOTACHECKED;
418 class_destroy_import(imp);
426 int client_obd_cleanup(struct obd_device *obddev)
430 ldlm_namespace_free_post(obddev->obd_namespace);
431 obddev->obd_namespace = NULL;
433 LASSERT(obddev->u.cli.cl_import == NULL);
439 /* ->o_connect() method for client side (OSC and MDC and MGC) */
440 int client_connect_import(const struct lu_env *env,
441 struct obd_export **exp,
442 struct obd_device *obd, struct obd_uuid *cluuid,
443 struct obd_connect_data *data, void *localdata)
445 struct client_obd *cli = &obd->u.cli;
446 struct obd_import *imp = cli->cl_import;
447 struct obd_connect_data *ocd;
448 struct lustre_handle conn = { 0 };
453 cfs_down_write(&cli->cl_sem);
454 if (cli->cl_conn_count > 0 )
455 GOTO(out_sem, rc = -EALREADY);
457 rc = class_connect(&conn, obd, cluuid);
461 cli->cl_conn_count++;
462 *exp = class_conn2export(&conn);
464 LASSERT(obd->obd_namespace);
466 imp->imp_dlm_handle = conn;
467 rc = ptlrpc_init_import(imp);
471 ocd = &imp->imp_connect_data;
474 imp->imp_connect_flags_orig = data->ocd_connect_flags;
477 rc = ptlrpc_connect_import(imp, NULL);
479 LASSERT (imp->imp_state == LUSTRE_IMP_DISCON);
482 LASSERT((*exp)->exp_connection);
485 LASSERTF((ocd->ocd_connect_flags & data->ocd_connect_flags) ==
486 ocd->ocd_connect_flags, "old "LPX64", new "LPX64"\n",
487 data->ocd_connect_flags, ocd->ocd_connect_flags);
488 data->ocd_connect_flags = ocd->ocd_connect_flags;
491 ptlrpc_pinger_add_import(imp);
497 cli->cl_conn_count--;
498 class_disconnect(*exp);
502 cfs_up_write(&cli->cl_sem);
507 int client_disconnect_export(struct obd_export *exp)
509 struct obd_device *obd = class_exp2obd(exp);
510 struct client_obd *cli;
511 struct obd_import *imp;
516 CERROR("invalid export for disconnect: exp %p cookie "LPX64"\n",
517 exp, exp ? exp->exp_handle.h_cookie : -1);
522 imp = cli->cl_import;
524 cfs_down_write(&cli->cl_sem);
525 CDEBUG(D_INFO, "disconnect %s - %d\n", obd->obd_name,
528 if (!cli->cl_conn_count) {
529 CERROR("disconnecting disconnected device (%s)\n",
531 GOTO(out_disconnect, rc = -EINVAL);
534 cli->cl_conn_count--;
535 if (cli->cl_conn_count)
536 GOTO(out_disconnect, rc = 0);
538 /* Mark import deactivated now, so we don't try to reconnect if any
539 * of the cleanup RPCs fails (e.g. ldlm cancel, etc). We don't
540 * fully deactivate the import, or that would drop all requests. */
541 cfs_spin_lock(&imp->imp_lock);
542 imp->imp_deactive = 1;
543 cfs_spin_unlock(&imp->imp_lock);
545 /* Some non-replayable imports (MDS's OSCs) are pinged, so just
546 * delete it regardless. (It's safe to delete an import that was
548 (void)ptlrpc_pinger_del_import(imp);
550 if (obd->obd_namespace != NULL) {
551 /* obd_force == local only */
552 ldlm_cli_cancel_unused(obd->obd_namespace, NULL,
553 obd->obd_force ? LCF_LOCAL : 0, NULL);
554 ldlm_namespace_free_prior(obd->obd_namespace, imp, obd->obd_force);
558 * there's no need to hold sem during disconnecting an import,
559 * and actually it may cause deadlock in gss.
561 cfs_up_write(&cli->cl_sem);
562 rc = ptlrpc_disconnect_import(imp, 0);
563 cfs_down_write(&cli->cl_sem);
565 ptlrpc_invalidate_import(imp);
570 /* use server style - class_disconnect should be always called for
572 err = class_disconnect(exp);
576 cfs_up_write(&cli->cl_sem);
581 int server_disconnect_export(struct obd_export *exp)
586 /* Disconnect early so that clients can't keep using export */
587 rc = class_disconnect(exp);
588 /* close import for avoid sending any requests */
589 if (exp->exp_imp_reverse)
590 ptlrpc_cleanup_imp(exp->exp_imp_reverse);
592 if (exp->exp_obd->obd_namespace != NULL)
593 ldlm_cancel_locks_for_export(exp);
595 /* complete all outstanding replies */
596 cfs_spin_lock(&exp->exp_lock);
597 while (!cfs_list_empty(&exp->exp_outstanding_replies)) {
598 struct ptlrpc_reply_state *rs =
599 cfs_list_entry(exp->exp_outstanding_replies.next,
600 struct ptlrpc_reply_state, rs_exp_list);
601 struct ptlrpc_service *svc = rs->rs_service;
603 cfs_spin_lock(&svc->srv_rs_lock);
604 cfs_list_del_init(&rs->rs_exp_list);
605 cfs_spin_lock(&rs->rs_lock);
606 ptlrpc_schedule_difficult_reply(rs);
607 cfs_spin_unlock(&rs->rs_lock);
608 cfs_spin_unlock(&svc->srv_rs_lock);
610 cfs_spin_unlock(&exp->exp_lock);
615 /* --------------------------------------------------------------------------
616 * from old lib/target.c
617 * -------------------------------------------------------------------------- */
619 static int target_handle_reconnect(struct lustre_handle *conn,
620 struct obd_export *exp,
621 struct obd_uuid *cluuid)
625 if (exp->exp_connection && exp->exp_imp_reverse) {
626 struct lustre_handle *hdl;
627 struct obd_device *target;
629 hdl = &exp->exp_imp_reverse->imp_remote_handle;
630 target = exp->exp_obd;
632 /* Might be a re-connect after a partition. */
633 if (!memcmp(&conn->cookie, &hdl->cookie, sizeof conn->cookie)) {
634 if (target->obd_recovering) {
635 int timeout = cfs_duration_sec(cfs_time_sub(
637 &target->obd_recovery_timer),
638 cfs_time_current()));
640 LCONSOLE_WARN("%s: Client %s (at %s) reconnect"
641 "ing, waiting for %d clients in recov"
642 "ery for %d:%.02d\n", target->obd_name,
643 obd_uuid2str(&exp->exp_client_uuid),
644 obd_export_nid2str(exp),
645 target->obd_max_recoverable_clients,
646 timeout / 60, timeout % 60);
648 LCONSOLE_WARN("%s: Client %s (at %s) "
649 "reconnecting\n", target->obd_name,
650 obd_uuid2str(&exp->exp_client_uuid),
651 obd_export_nid2str(exp));
654 conn->cookie = exp->exp_handle.h_cookie;
655 /* target_handle_connect() treats EALREADY and
656 * -EALREADY differently. EALREADY means we are
657 * doing a valid reconnect from the same client. */
660 LCONSOLE_WARN("%s: already connected client %s (at %s) "
661 "with handle "LPX64". Rejecting client "
662 "with the same UUID trying to reconnect "
663 "with handle "LPX64"\n", target->obd_name,
664 obd_uuid2str(&exp->exp_client_uuid),
665 obd_export_nid2str(exp),
666 hdl->cookie, conn->cookie);
667 memset(conn, 0, sizeof *conn);
668 /* target_handle_connect() treats EALREADY and
669 * -EALREADY differently. -EALREADY is an error
670 * (same UUID, different handle). */
675 conn->cookie = exp->exp_handle.h_cookie;
676 CDEBUG(D_HA, "connect export for UUID '%s' at %p, cookie "LPX64"\n",
677 cluuid->uuid, exp, conn->cookie);
681 void target_client_add_cb(struct obd_device *obd, __u64 transno, void *cb_data,
684 struct obd_export *exp = cb_data;
686 CDEBUG(D_RPCTRACE, "%s: committing for initial connect of %s\n",
687 obd->obd_name, exp->exp_client_uuid.uuid);
689 cfs_spin_lock(&exp->exp_lock);
690 exp->exp_need_sync = 0;
691 cfs_spin_unlock(&exp->exp_lock);
692 class_export_cb_put(exp);
694 EXPORT_SYMBOL(target_client_add_cb);
697 target_start_and_reset_recovery_timer(struct obd_device *obd,
698 struct ptlrpc_request *req,
701 int target_handle_connect(struct ptlrpc_request *req)
703 struct obd_device *target = NULL, *targref = NULL;
704 struct obd_export *export = NULL;
705 struct obd_import *revimp;
706 struct lustre_handle conn;
707 struct lustre_handle *tmp;
708 struct obd_uuid tgtuuid;
709 struct obd_uuid cluuid;
710 struct obd_uuid remote_uuid;
716 struct obd_connect_data *data, *tmpdata;
718 lnet_nid_t *client_nid = NULL;
721 OBD_RACE(OBD_FAIL_TGT_CONN_RACE);
723 str = req_capsule_client_get(&req->rq_pill, &RMF_TGTUUID);
725 DEBUG_REQ(D_ERROR, req, "bad target UUID for connect");
726 GOTO(out, rc = -EINVAL);
729 obd_str2uuid(&tgtuuid, str);
730 target = class_uuid2obd(&tgtuuid);
732 target = class_name2obd(str);
735 deuuidify(str, NULL, &target_start, &target_len);
736 LCONSOLE_ERROR_MSG(0x137, "UUID '%s' is not available for "
737 "connect (no target)\n", str);
738 GOTO(out, rc = -ENODEV);
741 cfs_spin_lock(&target->obd_dev_lock);
742 if (target->obd_stopping || !target->obd_set_up) {
743 cfs_spin_unlock(&target->obd_dev_lock);
745 deuuidify(str, NULL, &target_start, &target_len);
746 LCONSOLE_ERROR_MSG(0x137, "%.*s: Not available for connect "
747 "from %s (%s)\n", target_len, target_start,
748 libcfs_nid2str(req->rq_peer.nid),
749 (target->obd_stopping ?
750 "stopping" : "not set up"));
751 GOTO(out, rc = -ENODEV);
754 if (target->obd_no_conn) {
755 cfs_spin_unlock(&target->obd_dev_lock);
757 LCONSOLE_WARN("%s: Temporarily refusing client connection "
758 "from %s\n", target->obd_name,
759 libcfs_nid2str(req->rq_peer.nid));
760 GOTO(out, rc = -EAGAIN);
763 /* Make sure the target isn't cleaned up while we're here. Yes,
764 there's still a race between the above check and our incref here.
765 Really, class_uuid2obd should take the ref. */
766 targref = class_incref(target, __FUNCTION__, cfs_current());
768 target->obd_conn_inprogress++;
769 cfs_spin_unlock(&target->obd_dev_lock);
771 str = req_capsule_client_get(&req->rq_pill, &RMF_CLUUID);
773 DEBUG_REQ(D_ERROR, req, "bad client UUID for connect");
774 GOTO(out, rc = -EINVAL);
777 obd_str2uuid(&cluuid, str);
779 /* XXX extract a nettype and format accordingly */
780 switch (sizeof(lnet_nid_t)) {
781 /* NB the casts only avoid compiler warnings */
783 snprintf(remote_uuid.uuid, sizeof remote_uuid,
784 "NET_"LPX64"_UUID", (__u64)req->rq_peer.nid);
787 snprintf(remote_uuid.uuid, sizeof remote_uuid,
788 "NET_%x_UUID", (__u32)req->rq_peer.nid);
794 tmp = req_capsule_client_get(&req->rq_pill, &RMF_CONN);
796 GOTO(out, rc = -EPROTO);
800 size = req_capsule_get_size(&req->rq_pill, &RMF_CONNECT_DATA,
802 data = req_capsule_client_get(&req->rq_pill, &RMF_CONNECT_DATA);
804 GOTO(out, rc = -EPROTO);
806 rc = req_capsule_server_pack(&req->rq_pill);
810 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT) {
812 DEBUG_REQ(D_WARNING, req, "Refusing old (unversioned) "
813 "libclient connection attempt");
814 GOTO(out, rc = -EPROTO);
815 } else if (data->ocd_version < LUSTRE_VERSION_CODE -
816 LUSTRE_VERSION_ALLOWED_OFFSET ||
817 data->ocd_version > LUSTRE_VERSION_CODE +
818 LUSTRE_VERSION_ALLOWED_OFFSET) {
819 DEBUG_REQ(D_WARNING, req, "Refusing %s (%d.%d.%d.%d) "
820 "libclient connection attempt",
821 data->ocd_version < LUSTRE_VERSION_CODE ?
823 OBD_OCD_VERSION_MAJOR(data->ocd_version),
824 OBD_OCD_VERSION_MINOR(data->ocd_version),
825 OBD_OCD_VERSION_PATCH(data->ocd_version),
826 OBD_OCD_VERSION_FIX(data->ocd_version));
827 data = req_capsule_server_sized_get(&req->rq_pill,
829 offsetof(typeof(*data), ocd_version) +
830 sizeof(data->ocd_version));
832 data->ocd_connect_flags = OBD_CONNECT_VERSION;
833 data->ocd_version = LUSTRE_VERSION_CODE;
835 GOTO(out, rc = -EPROTO);
839 if ((lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_INITIAL) &&
840 (data->ocd_connect_flags & OBD_CONNECT_MDS))
843 /* lctl gets a backstage, all-access pass. */
844 if (obd_uuid_equals(&cluuid, &target->obd_uuid))
845 goto dont_check_exports;
847 export = cfs_hash_lookup(target->obd_uuid_hash, &cluuid);
851 /* we've found an export in the hash */
853 cfs_spin_lock(&export->exp_lock);
855 if (export->exp_connecting) { /* bug 9635, et. al. */
856 cfs_spin_unlock(&export->exp_lock);
857 LCONSOLE_WARN("%s: Export %p already connecting from %s\n",
858 export->exp_obd->obd_name, export,
859 libcfs_nid2str(req->rq_peer.nid));
860 class_export_put(export);
863 } else if (mds_conn && export->exp_connection) {
864 cfs_spin_unlock(&export->exp_lock);
865 if (req->rq_peer.nid != export->exp_connection->c_peer.nid)
866 /* mds reconnected after failover */
867 LCONSOLE_WARN("%s: Received MDS connection from "
868 "%s, removing former export from %s\n",
869 target->obd_name, libcfs_nid2str(req->rq_peer.nid),
870 libcfs_nid2str(export->exp_connection->c_peer.nid));
872 /* new mds connection from the same nid */
873 LCONSOLE_WARN("%s: Received new MDS connection from "
874 "%s, removing former export from same NID\n",
875 target->obd_name, libcfs_nid2str(req->rq_peer.nid));
876 class_fail_export(export);
877 class_export_put(export);
880 } else if (export->exp_connection != NULL &&
881 req->rq_peer.nid != export->exp_connection->c_peer.nid &&
882 (lustre_msg_get_op_flags(req->rq_reqmsg) &
883 MSG_CONNECT_INITIAL)) {
884 cfs_spin_unlock(&export->exp_lock);
885 /* in mds failover we have static uuid but nid can be
887 LCONSOLE_WARN("%s: Client %s seen on new nid %s when "
888 "existing nid %s is already connected\n",
889 target->obd_name, cluuid.uuid,
890 libcfs_nid2str(req->rq_peer.nid),
892 export->exp_connection->c_peer.nid));
894 class_export_put(export);
897 export->exp_connecting = 1;
898 cfs_spin_unlock(&export->exp_lock);
899 LASSERT(export->exp_obd == target);
901 rc = target_handle_reconnect(&conn, export, &cluuid);
904 /* If we found an export, we already unlocked. */
907 OBD_FAIL_TIMEOUT(OBD_FAIL_TGT_DELAY_CONNECT, 2 * obd_timeout);
908 } else if (req->rq_export == NULL &&
909 cfs_atomic_read(&export->exp_rpc_count) > 0) {
910 LCONSOLE_WARN("%s: Client %s (at %s) refused connection, "
911 "still busy with %d references\n",
912 target->obd_name, cluuid.uuid,
913 libcfs_nid2str(req->rq_peer.nid),
914 cfs_atomic_read(&export->exp_refcount));
915 GOTO(out, rc = -EBUSY);
916 } else if (req->rq_export != NULL &&
917 (cfs_atomic_read(&export->exp_rpc_count) > 1)) {
918 /* the current connect rpc has increased exp_rpc_count */
919 LCONSOLE_WARN("%s: Client %s (at %s) refused reconnection, "
920 "still busy with %d active RPCs\n",
921 target->obd_name, cluuid.uuid,
922 libcfs_nid2str(req->rq_peer.nid),
923 cfs_atomic_read(&export->exp_rpc_count) - 1);
924 cfs_spin_lock(&export->exp_lock);
925 if (req->rq_export->exp_conn_cnt <
926 lustre_msg_get_conn_cnt(req->rq_reqmsg))
927 /* try to abort active requests */
928 req->rq_export->exp_abort_active_req = 1;
929 cfs_spin_unlock(&export->exp_lock);
930 GOTO(out, rc = -EBUSY);
931 } else if (lustre_msg_get_conn_cnt(req->rq_reqmsg) == 1) {
932 if (!strstr(cluuid.uuid, "mdt"))
933 LCONSOLE_WARN("%s: Rejecting reconnect from the "
934 "known client %s (at %s) because it "
935 "is indicating it is a new client",
936 target->obd_name, cluuid.uuid,
937 libcfs_nid2str(req->rq_peer.nid));
938 GOTO(out, rc = -EALREADY);
940 OBD_FAIL_TIMEOUT(OBD_FAIL_TGT_DELAY_RECONNECT, 2 * obd_timeout);
947 CWARN("%s: connection from %s@%s %st"LPU64" exp %p cur %ld last %ld\n",
948 target->obd_name, cluuid.uuid, libcfs_nid2str(req->rq_peer.nid),
949 target->obd_recovering ? "recovering/" : "", data->ocd_transno,
950 export, (long)cfs_time_current_sec(),
951 export ? (long)export->exp_last_request_time : 0);
953 /* If this is the first time a client connects,
954 * reset the recovery timer */
955 if (rc == 0 && target->obd_recovering)
956 target_start_and_reset_recovery_timer(target, req, !export);
958 /* We want to handle EALREADY but *not* -EALREADY from
959 * target_handle_reconnect(), return reconnection state in a flag */
960 if (rc == EALREADY) {
961 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECONNECT);
967 /* Tell the client if we support replayable requests */
968 if (target->obd_replayable)
969 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
970 client_nid = &req->rq_peer.nid;
972 if (export == NULL) {
973 if (target->obd_recovering) {
976 t = cfs_timer_deadline(&target->obd_recovery_timer);
977 t = cfs_time_sub(t, cfs_time_current());
978 t = cfs_duration_sec(t);
979 LCONSOLE_WARN("%s: Denying connection for new client "
980 "%s (at %s), waiting for %d clients in "
981 "recovery for %d:%.02d\n",
983 libcfs_nid2str(req->rq_peer.nid),
985 cfs_atomic_read(&target-> \
986 obd_lock_replay_clients),
987 (int)t / 60, (int)t % 60);
991 rc = obd_connect(req->rq_svc_thread->t_env,
992 &export, target, &cluuid, data,
995 conn.cookie = export->exp_handle.h_cookie;
998 rc = obd_reconnect(req->rq_svc_thread->t_env,
999 export, target, &cluuid, data, client_nid);
1003 /* Return only the parts of obd_connect_data that we understand, so the
1004 * client knows that we don't understand the rest. */
1006 tmpsize = req_capsule_get_size(&req->rq_pill, &RMF_CONNECT_DATA,
1008 tmpdata = req_capsule_server_get(&req->rq_pill,
1010 /* Don't use struct assignment here, because the client reply
1011 * buffer may be smaller/larger than the local struct
1012 * obd_connect_data. */
1013 memcpy(tmpdata, data, min(tmpsize, size));
1016 /* If all else goes well, this is our RPC return code. */
1019 lustre_msg_set_handle(req->rq_repmsg, &conn);
1021 /* If the client and the server are the same node, we will already
1022 * have an export that really points to the client's DLM export,
1023 * because we have a shared handles table.
1025 * XXX this will go away when shaver stops sending the "connect" handle
1026 * in the real "remote handle" field of the request --phik 24 Apr 2003
1028 if (req->rq_export != NULL)
1029 class_export_put(req->rq_export);
1031 /* request takes one export refcount */
1032 req->rq_export = class_export_get(export);
1034 cfs_spin_lock(&export->exp_lock);
1035 if (export->exp_conn_cnt >= lustre_msg_get_conn_cnt(req->rq_reqmsg)) {
1036 cfs_spin_unlock(&export->exp_lock);
1037 CDEBUG(D_RPCTRACE, "%s: %s already connected at higher "
1038 "conn_cnt: %d > %d\n",
1039 cluuid.uuid, libcfs_nid2str(req->rq_peer.nid),
1040 export->exp_conn_cnt,
1041 lustre_msg_get_conn_cnt(req->rq_reqmsg));
1043 GOTO(out, rc = -EALREADY);
1045 LASSERT(lustre_msg_get_conn_cnt(req->rq_reqmsg) > 0);
1046 export->exp_conn_cnt = lustre_msg_get_conn_cnt(req->rq_reqmsg);
1047 export->exp_abort_active_req = 0;
1049 /* request from liblustre? Don't evict it for not pinging. */
1050 if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT) {
1051 export->exp_libclient = 1;
1052 cfs_spin_unlock(&export->exp_lock);
1054 cfs_spin_lock(&target->obd_dev_lock);
1055 cfs_list_del_init(&export->exp_obd_chain_timed);
1056 cfs_spin_unlock(&target->obd_dev_lock);
1058 cfs_spin_unlock(&export->exp_lock);
1061 if (export->exp_connection != NULL) {
1062 /* Check to see if connection came from another NID */
1063 if ((export->exp_connection->c_peer.nid != req->rq_peer.nid) &&
1064 !cfs_hlist_unhashed(&export->exp_nid_hash))
1065 cfs_hash_del(export->exp_obd->obd_nid_hash,
1066 &export->exp_connection->c_peer.nid,
1067 &export->exp_nid_hash);
1069 ptlrpc_connection_put(export->exp_connection);
1072 export->exp_connection = ptlrpc_connection_get(req->rq_peer,
1075 if (cfs_hlist_unhashed(&export->exp_nid_hash)) {
1076 cfs_hash_add(export->exp_obd->obd_nid_hash,
1077 &export->exp_connection->c_peer.nid,
1078 &export->exp_nid_hash);
1081 class_disconnect->class_export_recovery_cleanup() race
1083 cfs_spin_lock(&target->obd_recovery_task_lock);
1084 if (target->obd_recovering && !export->exp_in_recovery &&
1085 !export->exp_disconnected) {
1086 cfs_spin_lock(&export->exp_lock);
1087 /* possible race with class_disconnect_stale_exports,
1088 * export may be already in the eviction process */
1089 if (export->exp_failed) {
1090 cfs_spin_unlock(&export->exp_lock);
1091 GOTO(out, rc = -ENODEV);
1093 export->exp_in_recovery = 1;
1094 export->exp_req_replay_needed = 1;
1095 export->exp_lock_replay_needed = 1;
1096 cfs_spin_unlock(&export->exp_lock);
1097 if ((lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_TRANSNO)
1098 && (data->ocd_transno == 0))
1099 CWARN("Connect with zero transno!\n");
1101 if ((lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_TRANSNO)
1102 && data->ocd_transno < target->obd_next_recovery_transno &&
1103 data->ocd_transno > target->obd_last_committed)
1104 target->obd_next_recovery_transno = data->ocd_transno;
1105 target->obd_connected_clients++;
1106 cfs_atomic_inc(&target->obd_req_replay_clients);
1107 cfs_atomic_inc(&target->obd_lock_replay_clients);
1108 if (target->obd_connected_clients ==
1109 target->obd_max_recoverable_clients)
1110 cfs_waitq_signal(&target->obd_next_transno_waitq);
1112 cfs_spin_unlock(&target->obd_recovery_task_lock);
1114 /* Tell the client we're in recovery, when client is involved in it. */
1115 if (target->obd_recovering)
1116 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
1118 tmp = req_capsule_client_get(&req->rq_pill, &RMF_CONN);
1121 if (export->exp_imp_reverse != NULL) {
1122 /* destroyed import can be still referenced in ctxt */
1123 obd_set_info_async(export, sizeof(KEY_REVIMP_UPD),
1124 KEY_REVIMP_UPD, 0, NULL, NULL);
1126 client_destroy_import(export->exp_imp_reverse);
1129 /* for the rest part, we return -ENOTCONN in case of errors
1130 * in order to let client initialize connection again.
1132 revimp = export->exp_imp_reverse = class_new_import(target);
1134 CERROR("fail to alloc new reverse import.\n");
1135 GOTO(out, rc = -ENOTCONN);
1138 revimp->imp_connection = ptlrpc_connection_addref(export->exp_connection);
1139 revimp->imp_client = &export->exp_obd->obd_ldlm_client;
1140 revimp->imp_remote_handle = conn;
1141 revimp->imp_dlm_fake = 1;
1142 revimp->imp_state = LUSTRE_IMP_FULL;
1144 /* unknown versions will be caught in
1145 * ptlrpc_handle_server_req_in->lustre_unpack_msg() */
1146 revimp->imp_msg_magic = req->rq_reqmsg->lm_magic;
1148 if ((export->exp_connect_flags & OBD_CONNECT_AT) &&
1149 (revimp->imp_msg_magic != LUSTRE_MSG_MAGIC_V1))
1150 revimp->imp_msghdr_flags |= MSGHDR_AT_SUPPORT;
1152 revimp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
1154 if ((export->exp_connect_flags & OBD_CONNECT_FULL20) &&
1155 (revimp->imp_msg_magic != LUSTRE_MSG_MAGIC_V1))
1156 revimp->imp_msghdr_flags |= MSGHDR_CKSUM_INCOMPAT18;
1158 revimp->imp_msghdr_flags &= ~MSGHDR_CKSUM_INCOMPAT18;
1160 rc = sptlrpc_import_sec_adapt(revimp, req->rq_svc_ctx, &req->rq_flvr);
1162 CERROR("Failed to get sec for reverse import: %d\n", rc);
1163 export->exp_imp_reverse = NULL;
1164 class_destroy_import(revimp);
1167 class_import_put(revimp);
1170 cfs_spin_lock(&export->exp_lock);
1171 export->exp_connecting = 0;
1172 cfs_spin_unlock(&export->exp_lock);
1174 class_export_put(export);
1177 cfs_spin_lock(&target->obd_dev_lock);
1178 target->obd_conn_inprogress--;
1179 cfs_spin_unlock(&target->obd_dev_lock);
1181 class_decref(targref, __FUNCTION__, cfs_current());
1184 req->rq_status = rc;
1188 int target_handle_disconnect(struct ptlrpc_request *req)
1193 rc = req_capsule_server_pack(&req->rq_pill);
1197 /* keep the rq_export around so we can send the reply */
1198 req->rq_status = obd_disconnect(class_export_get(req->rq_export));
1203 void target_destroy_export(struct obd_export *exp)
1205 /* exports created from last_rcvd data, and "fake"
1206 exports created by lctl don't have an import */
1207 if (exp->exp_imp_reverse != NULL)
1208 client_destroy_import(exp->exp_imp_reverse);
1210 LASSERT_ATOMIC_ZERO(&exp->exp_locks_count);
1211 LASSERT_ATOMIC_ZERO(&exp->exp_rpc_count);
1212 LASSERT_ATOMIC_ZERO(&exp->exp_cb_count);
1213 LASSERT_ATOMIC_ZERO(&exp->exp_replay_count);
1217 * Recovery functions
1219 static void target_request_copy_get(struct ptlrpc_request *req)
1221 class_export_rpc_get(req->rq_export);
1222 LASSERT(cfs_list_empty(&req->rq_list));
1223 CFS_INIT_LIST_HEAD(&req->rq_replay_list);
1225 /* increase refcount to keep request in queue */
1226 cfs_atomic_inc(&req->rq_refcount);
1227 /** let export know it has replays to be handled */
1228 cfs_atomic_inc(&req->rq_export->exp_replay_count);
1231 static void target_request_copy_put(struct ptlrpc_request *req)
1233 LASSERT(cfs_list_empty(&req->rq_replay_list));
1234 LASSERT_ATOMIC_POS(&req->rq_export->exp_replay_count);
1236 cfs_atomic_dec(&req->rq_export->exp_replay_count);
1237 class_export_rpc_put(req->rq_export);
1238 ptlrpc_server_drop_request(req);
1241 static int target_exp_enqueue_req_replay(struct ptlrpc_request *req)
1243 __u64 transno = lustre_msg_get_transno(req->rq_reqmsg);
1244 struct obd_export *exp = req->rq_export;
1245 struct ptlrpc_request *reqiter;
1250 cfs_spin_lock(&exp->exp_lock);
1251 cfs_list_for_each_entry(reqiter, &exp->exp_req_replay_queue,
1253 if (lustre_msg_get_transno(reqiter->rq_reqmsg) == transno) {
1260 /* we expect it with RESENT and REPLAY flags */
1261 if ((lustre_msg_get_flags(req->rq_reqmsg) &
1262 (MSG_RESENT | MSG_REPLAY)) != (MSG_RESENT | MSG_REPLAY))
1263 CERROR("invalid flags %x of resent replay\n",
1264 lustre_msg_get_flags(req->rq_reqmsg));
1266 cfs_list_add_tail(&req->rq_replay_list,
1267 &exp->exp_req_replay_queue);
1270 cfs_spin_unlock(&exp->exp_lock);
1274 static void target_exp_dequeue_req_replay(struct ptlrpc_request *req)
1276 LASSERT(!cfs_list_empty(&req->rq_replay_list));
1277 LASSERT(req->rq_export);
1279 cfs_spin_lock(&req->rq_export->exp_lock);
1280 cfs_list_del_init(&req->rq_replay_list);
1281 cfs_spin_unlock(&req->rq_export->exp_lock);
1285 static void target_finish_recovery(struct obd_device *obd)
1288 LCONSOLE_INFO("%s: sending delayed replies to recovered clients\n",
1291 ldlm_reprocess_all_ns(obd->obd_namespace);
1292 cfs_spin_lock(&obd->obd_recovery_task_lock);
1293 if (!cfs_list_empty(&obd->obd_req_replay_queue) ||
1294 !cfs_list_empty(&obd->obd_lock_replay_queue) ||
1295 !cfs_list_empty(&obd->obd_final_req_queue)) {
1296 CERROR("%s: Recovery queues ( %s%s%s) are not empty\n",
1298 cfs_list_empty(&obd->obd_req_replay_queue) ? "" : "req ",
1299 cfs_list_empty(&obd->obd_lock_replay_queue) ? \
1301 cfs_list_empty(&obd->obd_final_req_queue) ? \
1303 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1306 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1308 obd->obd_recovery_end = cfs_time_current_sec();
1310 /* when recovery finished, cleanup orphans on mds and ost */
1311 if (OBT(obd) && OBP(obd, postrecov)) {
1312 int rc = OBP(obd, postrecov)(obd);
1314 LCONSOLE_WARN("%s: Post recovery failed, rc %d\n",
1320 static void abort_req_replay_queue(struct obd_device *obd)
1322 struct ptlrpc_request *req, *n;
1323 cfs_list_t abort_list;
1325 CFS_INIT_LIST_HEAD(&abort_list);
1326 cfs_spin_lock(&obd->obd_recovery_task_lock);
1327 cfs_list_splice_init(&obd->obd_req_replay_queue, &abort_list);
1328 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1329 cfs_list_for_each_entry_safe(req, n, &abort_list, rq_list) {
1330 DEBUG_REQ(D_WARNING, req, "aborted:");
1331 req->rq_status = -ENOTCONN;
1332 if (ptlrpc_error(req)) {
1333 DEBUG_REQ(D_ERROR, req,
1334 "failed abort_req_reply; skipping");
1336 target_exp_dequeue_req_replay(req);
1337 target_request_copy_put(req);
1341 static void abort_lock_replay_queue(struct obd_device *obd)
1343 struct ptlrpc_request *req, *n;
1344 cfs_list_t abort_list;
1346 CFS_INIT_LIST_HEAD(&abort_list);
1347 cfs_spin_lock(&obd->obd_recovery_task_lock);
1348 cfs_list_splice_init(&obd->obd_lock_replay_queue, &abort_list);
1349 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1350 cfs_list_for_each_entry_safe(req, n, &abort_list, rq_list){
1351 DEBUG_REQ(D_ERROR, req, "aborted:");
1352 req->rq_status = -ENOTCONN;
1353 if (ptlrpc_error(req)) {
1354 DEBUG_REQ(D_ERROR, req,
1355 "failed abort_lock_reply; skipping");
1357 target_request_copy_put(req);
1362 /* Called from a cleanup function if the device is being cleaned up
1363 forcefully. The exports should all have been disconnected already,
1364 the only thing left to do is
1365 - clear the recovery flags
1367 - free queued requests and replies, but don't send replies
1368 Because the obd_stopping flag is set, no new requests should be received.
1371 void target_cleanup_recovery(struct obd_device *obd)
1373 struct ptlrpc_request *req, *n;
1374 cfs_list_t clean_list;
1377 CFS_INIT_LIST_HEAD(&clean_list);
1378 cfs_spin_lock(&obd->obd_dev_lock);
1379 if (!obd->obd_recovering) {
1380 cfs_spin_unlock(&obd->obd_dev_lock);
1384 obd->obd_recovering = obd->obd_abort_recovery = 0;
1385 cfs_spin_unlock(&obd->obd_dev_lock);
1387 cfs_spin_lock(&obd->obd_recovery_task_lock);
1388 target_cancel_recovery_timer(obd);
1389 cfs_list_splice_init(&obd->obd_req_replay_queue, &clean_list);
1390 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1392 cfs_list_for_each_entry_safe(req, n, &clean_list, rq_list) {
1393 LASSERT(req->rq_reply_state == 0);
1394 target_exp_dequeue_req_replay(req);
1395 target_request_copy_put(req);
1398 cfs_spin_lock(&obd->obd_recovery_task_lock);
1399 cfs_list_splice_init(&obd->obd_lock_replay_queue, &clean_list);
1400 cfs_list_splice_init(&obd->obd_final_req_queue, &clean_list);
1401 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1403 cfs_list_for_each_entry_safe(req, n, &clean_list, rq_list){
1404 LASSERT(req->rq_reply_state == 0);
1405 target_request_copy_put(req);
1411 /* obd_recovery_task_lock should be held */
1412 void target_cancel_recovery_timer(struct obd_device *obd)
1414 CDEBUG(D_HA, "%s: cancel recovery timer\n", obd->obd_name);
1415 cfs_timer_disarm(&obd->obd_recovery_timer);
1418 /* extend = 1 means require at least "duration" seconds left in the timer,
1419 extend = 0 means set the total duration (start_recovery_timer) */
1420 static void reset_recovery_timer(struct obd_device *obd, int duration,
1423 cfs_time_t now = cfs_time_current_sec();
1424 cfs_duration_t left;
1426 cfs_spin_lock(&obd->obd_recovery_task_lock);
1427 if (!obd->obd_recovering || obd->obd_abort_recovery) {
1428 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1432 left = cfs_time_sub(obd->obd_recovery_end, now);
1434 if (extend && (duration > left))
1435 obd->obd_recovery_timeout += duration - left;
1436 else if (!extend && (duration > obd->obd_recovery_timeout))
1437 /* Track the client's largest expected replay time */
1438 obd->obd_recovery_timeout = duration;
1440 /* Hard limit of obd_recovery_time_hard which should not happen */
1441 if (obd->obd_recovery_timeout > obd->obd_recovery_time_hard)
1442 obd->obd_recovery_timeout = obd->obd_recovery_time_hard;
1444 obd->obd_recovery_end = obd->obd_recovery_start +
1445 obd->obd_recovery_timeout;
1446 if (cfs_time_before(now, obd->obd_recovery_end)) {
1447 left = cfs_time_sub(obd->obd_recovery_end, now);
1449 cfs_timer_arm(&obd->obd_recovery_timer, cfs_time_shift(left));
1451 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1452 CDEBUG(D_HA, "%s: recovery timer will expire in %u seconds\n",
1453 obd->obd_name, (unsigned)left);
1456 static void check_and_start_recovery_timer(struct obd_device *obd)
1458 cfs_spin_lock(&obd->obd_recovery_task_lock);
1459 if (cfs_timer_is_armed(&obd->obd_recovery_timer)) {
1460 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1463 CDEBUG(D_HA, "%s: starting recovery timer\n", obd->obd_name);
1464 obd->obd_recovery_start = cfs_time_current_sec();
1465 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1467 reset_recovery_timer(obd, obd->obd_recovery_timeout, 0);
1470 /* Reset the timer with each new client connection */
1472 * This timer is actually reconnect_timer, which is for making sure
1473 * the total recovery window is at least as big as my reconnect
1474 * attempt timing. So the initial recovery time_out will be set to
1475 * OBD_RECOVERY_FACTOR * obd_timeout. If the timeout coming
1476 * from client is bigger than this, then the recovery time_out will
1477 * be extended to make sure the client could be reconnected, in the
1478 * process, the timeout from the new client should be ignored.
1482 target_start_and_reset_recovery_timer(struct obd_device *obd,
1483 struct ptlrpc_request *req,
1486 int service_time = lustre_msg_get_service_time(req->rq_reqmsg);
1488 if (!new_client && service_time)
1489 /* Teach server about old server's estimates, as first guess
1490 * at how long new requests will take. */
1491 at_measured(&req->rq_rqbd->rqbd_service->srv_at_estimate,
1494 check_and_start_recovery_timer(obd);
1496 /* convert the service time to rpc timeout,
1497 * reuse service_time to limit stack usage */
1498 service_time = at_est2timeout(service_time);
1500 /* We expect other clients to timeout within service_time, then try
1501 * to reconnect, then try the failover server. The max delay between
1502 * connect attempts is SWITCH_MAX + SWITCH_INC + INITIAL */
1503 service_time += 2 * (CONNECTION_SWITCH_MAX + CONNECTION_SWITCH_INC +
1504 INITIAL_CONNECT_TIMEOUT);
1505 if (service_time > obd->obd_recovery_timeout && !new_client)
1506 reset_recovery_timer(obd, service_time, 0);
1511 /** Health checking routines */
1512 static inline int exp_connect_healthy(struct obd_export *exp)
1514 return (exp->exp_in_recovery);
1517 /** if export done req_replay or has replay in queue */
1518 static inline int exp_req_replay_healthy(struct obd_export *exp)
1520 return (!exp->exp_req_replay_needed ||
1521 cfs_atomic_read(&exp->exp_replay_count) > 0);
1523 /** if export done lock_replay or has replay in queue */
1524 static inline int exp_lock_replay_healthy(struct obd_export *exp)
1526 return (!exp->exp_lock_replay_needed ||
1527 cfs_atomic_read(&exp->exp_replay_count) > 0);
1530 static inline int exp_vbr_healthy(struct obd_export *exp)
1532 return (!exp->exp_vbr_failed);
1535 static inline int exp_finished(struct obd_export *exp)
1537 return (exp->exp_in_recovery && !exp->exp_lock_replay_needed);
1540 /** Checking routines for recovery */
1541 static int check_for_clients(struct obd_device *obd)
1543 if (obd->obd_abort_recovery || obd->obd_recovery_expired)
1545 LASSERT(obd->obd_connected_clients <= obd->obd_max_recoverable_clients);
1546 if (obd->obd_no_conn == 0 &&
1547 obd->obd_connected_clients + obd->obd_stale_clients ==
1548 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 = 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 */
1661 LCONSOLE_WARN("%s: recovery is timed out, "
1662 "evict stale exports\n", obd->obd_name);
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 reset_recovery_timer(obd, RECONNECT_DELAY_MAX, 1);
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)) {
1792 * Add request timeout to the recovery time so next request from
1793 * this client may come in recovery time
1795 reset_recovery_timer(class_exp2obd(req->rq_export),
1796 AT_OFF ? obd_timeout :
1797 lustre_msg_get_timeout(req->rq_reqmsg), 1);
1803 static int target_recovery_thread(void *arg)
1805 struct lu_target *lut = arg;
1806 struct obd_device *obd = lut->lut_obd;
1807 struct ptlrpc_request *req;
1808 struct target_recovery_data *trd = &obd->obd_recovery_data;
1809 unsigned long delta;
1810 unsigned long flags;
1812 struct ptlrpc_thread *thread = NULL;
1816 cfs_daemonize_ctxt("tgt_recov");
1818 SIGNAL_MASK_LOCK(current, flags);
1819 sigfillset(¤t->blocked);
1821 SIGNAL_MASK_UNLOCK(current, flags);
1823 OBD_ALLOC_PTR(thread);
1829 OBD_FREE_PTR(thread);
1833 rc = lu_context_init(&env->le_ctx, LCT_MD_THREAD);
1835 OBD_FREE_PTR(thread);
1840 thread->t_env = env;
1841 thread->t_id = -1; /* force filter_iobuf_get/put to use local buffers */
1842 env->le_ctx.lc_thread = thread;
1843 thread->t_data = NULL;
1844 thread->t_watchdog = NULL;
1846 CDEBUG(D_HA, "%s: started recovery thread pid %d\n", obd->obd_name,
1848 trd->trd_processing_task = cfs_curproc_pid();
1850 cfs_spin_lock(&obd->obd_dev_lock);
1851 obd->obd_recovering = 1;
1852 cfs_spin_unlock(&obd->obd_dev_lock);
1853 cfs_complete(&trd->trd_starting);
1855 /* first of all, we have to know the first transno to replay */
1856 if (target_recovery_overseer(obd, check_for_clients,
1857 exp_connect_healthy)) {
1858 abort_req_replay_queue(obd);
1859 abort_lock_replay_queue(obd);
1862 /* next stage: replay requests */
1864 CDEBUG(D_INFO, "1: request replay stage - %d clients from t"LPU64"\n",
1865 cfs_atomic_read(&obd->obd_req_replay_clients),
1866 obd->obd_next_recovery_transno);
1867 while ((req = target_next_replay_req(obd))) {
1868 LASSERT(trd->trd_processing_task == cfs_curproc_pid());
1869 DEBUG_REQ(D_HA, req, "processing t"LPD64" from %s",
1870 lustre_msg_get_transno(req->rq_reqmsg),
1871 libcfs_nid2str(req->rq_peer.nid));
1872 handle_recovery_req(thread, req,
1873 trd->trd_recovery_handler);
1875 * bz18031: increase next_recovery_transno before
1876 * target_request_copy_put() will drop exp_rpc reference
1878 cfs_spin_lock(&obd->obd_recovery_task_lock);
1879 obd->obd_next_recovery_transno++;
1880 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1881 target_exp_dequeue_req_replay(req);
1882 target_request_copy_put(req);
1883 obd->obd_replayed_requests++;
1887 * The second stage: replay locks
1889 CDEBUG(D_INFO, "2: lock replay stage - %d clients\n",
1890 cfs_atomic_read(&obd->obd_lock_replay_clients));
1891 while ((req = target_next_replay_lock(obd))) {
1892 LASSERT(trd->trd_processing_task == cfs_curproc_pid());
1893 DEBUG_REQ(D_HA, req, "processing lock from %s: ",
1894 libcfs_nid2str(req->rq_peer.nid));
1895 handle_recovery_req(thread, req,
1896 trd->trd_recovery_handler);
1897 target_request_copy_put(req);
1898 obd->obd_replayed_locks++;
1902 * The third stage: reply on final pings, at this moment all clients
1903 * must have request in final queue
1905 CDEBUG(D_INFO, "3: final stage - process recovery completion pings\n");
1906 /** Update server last boot epoch */
1907 lut_boot_epoch_update(lut);
1908 /* We drop recoverying flag to forward all new requests
1909 * to regular mds_handle() since now */
1910 cfs_spin_lock(&obd->obd_dev_lock);
1911 obd->obd_recovering = obd->obd_abort_recovery = 0;
1912 cfs_spin_unlock(&obd->obd_dev_lock);
1913 cfs_spin_lock(&obd->obd_recovery_task_lock);
1914 target_cancel_recovery_timer(obd);
1915 cfs_spin_unlock(&obd->obd_recovery_task_lock);
1916 while ((req = target_next_final_ping(obd))) {
1917 LASSERT(trd->trd_processing_task == cfs_curproc_pid());
1918 DEBUG_REQ(D_HA, req, "processing final ping from %s: ",
1919 libcfs_nid2str(req->rq_peer.nid));
1920 handle_recovery_req(thread, req,
1921 trd->trd_recovery_handler);
1922 target_request_copy_put(req);
1925 delta = (jiffies - delta) / CFS_HZ;
1926 CDEBUG(D_INFO,"4: recovery completed in %lus - %d/%d reqs/locks\n",
1927 delta, obd->obd_replayed_requests, obd->obd_replayed_locks);
1928 if (delta > OBD_RECOVERY_TIME_SOFT) {
1929 CWARN("too long recovery - read logs\n");
1930 libcfs_debug_dumplog();
1933 target_finish_recovery(obd);
1935 lu_context_fini(&env->le_ctx);
1936 trd->trd_processing_task = 0;
1937 cfs_complete(&trd->trd_finishing);
1939 OBD_FREE_PTR(thread);
1944 static int target_start_recovery_thread(struct lu_target *lut,
1945 svc_handler_t handler)
1947 struct obd_device *obd = lut->lut_obd;
1949 struct target_recovery_data *trd = &obd->obd_recovery_data;
1951 memset(trd, 0, sizeof(*trd));
1952 cfs_init_completion(&trd->trd_starting);
1953 cfs_init_completion(&trd->trd_finishing);
1954 trd->trd_recovery_handler = handler;
1956 if (cfs_create_thread(target_recovery_thread, lut, 0) > 0) {
1957 cfs_wait_for_completion(&trd->trd_starting);
1958 LASSERT(obd->obd_recovering != 0);
1965 void target_stop_recovery_thread(struct obd_device *obd)
1967 if (obd->obd_recovery_data.trd_processing_task > 0) {
1968 struct target_recovery_data *trd = &obd->obd_recovery_data;
1969 /** recovery can be done but postrecovery is not yet */
1970 cfs_spin_lock(&obd->obd_dev_lock);
1971 if (obd->obd_recovering) {
1972 CERROR("%s: Aborting recovery\n", obd->obd_name);
1973 obd->obd_abort_recovery = 1;
1974 cfs_waitq_signal(&obd->obd_next_transno_waitq);
1976 cfs_spin_unlock(&obd->obd_dev_lock);
1977 cfs_wait_for_completion(&trd->trd_finishing);
1981 void target_recovery_fini(struct obd_device *obd)
1983 class_disconnect_exports(obd);
1984 target_stop_recovery_thread(obd);
1985 target_cleanup_recovery(obd);
1987 EXPORT_SYMBOL(target_recovery_fini);
1989 static void target_recovery_expired(unsigned long castmeharder)
1991 struct obd_device *obd = (struct obd_device *)castmeharder;
1992 CDEBUG(D_HA, "%s: recovery timed out; %d clients are still in recovery"
1993 " after %lds (%d clients connected)\n",
1994 obd->obd_name, cfs_atomic_read(&obd->obd_lock_replay_clients),
1995 cfs_time_current_sec()- obd->obd_recovery_start,
1996 obd->obd_connected_clients);
1998 obd->obd_recovery_expired = 1;
1999 cfs_waitq_signal(&obd->obd_next_transno_waitq);
2002 void target_recovery_init(struct lu_target *lut, svc_handler_t handler)
2004 struct obd_device *obd = lut->lut_obd;
2005 if (obd->obd_max_recoverable_clients == 0) {
2006 /** Update server last boot epoch */
2007 lut_boot_epoch_update(lut);
2011 CWARN("RECOVERY: service %s, %d recoverable clients, "
2012 "last_transno "LPU64"\n", obd->obd_name,
2013 obd->obd_max_recoverable_clients, obd->obd_last_committed);
2014 LASSERT(obd->obd_stopping == 0);
2015 obd->obd_next_recovery_transno = obd->obd_last_committed + 1;
2016 obd->obd_recovery_start = 0;
2017 obd->obd_recovery_end = 0;
2019 /* both values can be get from mount data already */
2020 if (obd->obd_recovery_timeout == 0)
2021 obd->obd_recovery_timeout = OBD_RECOVERY_TIME_SOFT;
2022 if (obd->obd_recovery_time_hard == 0)
2023 obd->obd_recovery_time_hard = OBD_RECOVERY_TIME_HARD;
2024 cfs_timer_init(&obd->obd_recovery_timer, target_recovery_expired, obd);
2025 target_start_recovery_thread(lut, handler);
2027 EXPORT_SYMBOL(target_recovery_init);
2031 static int target_process_req_flags(struct obd_device *obd,
2032 struct ptlrpc_request *req)
2034 struct obd_export *exp = req->rq_export;
2035 LASSERT(exp != NULL);
2036 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REQ_REPLAY_DONE) {
2037 /* client declares he's ready to replay locks */
2038 cfs_spin_lock(&exp->exp_lock);
2039 if (exp->exp_req_replay_needed) {
2040 exp->exp_req_replay_needed = 0;
2041 cfs_spin_unlock(&exp->exp_lock);
2043 LASSERT_ATOMIC_POS(&obd->obd_req_replay_clients);
2044 cfs_atomic_dec(&obd->obd_req_replay_clients);
2046 cfs_spin_unlock(&exp->exp_lock);
2049 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_LOCK_REPLAY_DONE) {
2050 /* client declares he's ready to complete recovery
2051 * so, we put the request on th final queue */
2052 cfs_spin_lock(&exp->exp_lock);
2053 if (exp->exp_lock_replay_needed) {
2054 exp->exp_lock_replay_needed = 0;
2055 cfs_spin_unlock(&exp->exp_lock);
2057 LASSERT_ATOMIC_POS(&obd->obd_lock_replay_clients);
2058 cfs_atomic_dec(&obd->obd_lock_replay_clients);
2060 cfs_spin_unlock(&exp->exp_lock);
2066 int target_queue_recovery_request(struct ptlrpc_request *req,
2067 struct obd_device *obd)
2071 __u64 transno = lustre_msg_get_transno(req->rq_reqmsg);
2074 if (obd->obd_recovery_data.trd_processing_task == cfs_curproc_pid()) {
2075 /* Processing the queue right now, don't re-add. */
2079 target_process_req_flags(obd, req);
2081 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_LOCK_REPLAY_DONE) {
2082 /* client declares he's ready to complete recovery
2083 * so, we put the request on th final queue */
2084 target_request_copy_get(req);
2085 DEBUG_REQ(D_HA, req, "queue final req");
2086 cfs_waitq_signal(&obd->obd_next_transno_waitq);
2087 cfs_spin_lock(&obd->obd_recovery_task_lock);
2088 if (obd->obd_recovering) {
2089 cfs_list_add_tail(&req->rq_list,
2090 &obd->obd_final_req_queue);
2092 cfs_spin_unlock(&obd->obd_recovery_task_lock);
2093 target_request_copy_put(req);
2094 RETURN(obd->obd_stopping ? -ENOTCONN : 1);
2096 cfs_spin_unlock(&obd->obd_recovery_task_lock);
2099 if (lustre_msg_get_flags(req->rq_reqmsg) & MSG_REQ_REPLAY_DONE) {
2100 /* client declares he's ready to replay locks */
2101 target_request_copy_get(req);
2102 DEBUG_REQ(D_HA, req, "queue lock replay req");
2103 cfs_waitq_signal(&obd->obd_next_transno_waitq);
2104 cfs_spin_lock(&obd->obd_recovery_task_lock);
2105 LASSERT(obd->obd_recovering);
2106 /* usually due to recovery abort */
2107 if (!req->rq_export->exp_in_recovery) {
2108 cfs_spin_unlock(&obd->obd_recovery_task_lock);
2109 target_request_copy_put(req);
2112 LASSERT(req->rq_export->exp_lock_replay_needed);
2113 cfs_list_add_tail(&req->rq_list, &obd->obd_lock_replay_queue);
2114 cfs_spin_unlock(&obd->obd_recovery_task_lock);
2118 /* CAVEAT EMPTOR: The incoming request message has been swabbed
2119 * (i.e. buflens etc are in my own byte order), but type-dependent
2120 * buffers (eg mds_body, ost_body etc) have NOT been swabbed. */
2123 CFS_INIT_LIST_HEAD(&req->rq_list);
2124 DEBUG_REQ(D_HA, req, "not queueing");
2128 /* If we're processing the queue, we want don't want to queue this
2131 * Also, if this request has a transno less than the one we're waiting
2132 * for, we should process it now. It could (and currently always will)
2133 * be an open request for a descriptor that was opened some time ago.
2135 * Also, a resent, replayed request that has already been
2136 * handled will pass through here and be processed immediately.
2138 CDEBUG(D_HA, "Next recovery transno: "LPU64
2139 ", current: "LPU64", replaying\n",
2140 obd->obd_next_recovery_transno, transno);
2141 cfs_spin_lock(&obd->obd_recovery_task_lock);
2142 if (transno < obd->obd_next_recovery_transno) {
2143 /* Processing the queue right now, don't re-add. */
2144 LASSERT(cfs_list_empty(&req->rq_list));
2145 cfs_spin_unlock(&obd->obd_recovery_task_lock);
2148 cfs_spin_unlock(&obd->obd_recovery_task_lock);
2150 if (OBD_FAIL_CHECK(OBD_FAIL_TGT_REPLAY_DROP))
2153 target_request_copy_get(req);
2154 if (!req->rq_export->exp_in_recovery) {
2155 target_request_copy_put(req);
2158 LASSERT(req->rq_export->exp_req_replay_needed);
2160 if (target_exp_enqueue_req_replay(req)) {
2161 DEBUG_REQ(D_ERROR, req, "dropping resent queued req");
2162 target_request_copy_put(req);
2167 cfs_spin_lock(&obd->obd_recovery_task_lock);
2168 LASSERT(obd->obd_recovering);
2169 cfs_list_for_each(tmp, &obd->obd_req_replay_queue) {
2170 struct ptlrpc_request *reqiter =
2171 cfs_list_entry(tmp, struct ptlrpc_request, rq_list);
2173 if (lustre_msg_get_transno(reqiter->rq_reqmsg) > transno) {
2174 cfs_list_add_tail(&req->rq_list, &reqiter->rq_list);
2179 if (unlikely(lustre_msg_get_transno(reqiter->rq_reqmsg) ==
2181 DEBUG_REQ(D_ERROR, req, "dropping replay: transno "
2182 "has been claimed by another client");
2183 cfs_spin_unlock(&obd->obd_recovery_task_lock);
2184 target_exp_dequeue_req_replay(req);
2185 target_request_copy_put(req);
2191 cfs_list_add_tail(&req->rq_list, &obd->obd_req_replay_queue);
2193 obd->obd_requests_queued_for_recovery++;
2194 cfs_spin_unlock(&obd->obd_recovery_task_lock);
2195 cfs_waitq_signal(&obd->obd_next_transno_waitq);
2200 * Packs current SLV and Limit into \a req.
2202 int target_pack_pool_reply(struct ptlrpc_request *req)
2204 struct obd_device *obd;
2208 * Check that we still have all structures alive as this may
2209 * be some late rpc in shutdown time.
2211 if (unlikely(!req->rq_export || !req->rq_export->exp_obd ||
2212 !exp_connect_lru_resize(req->rq_export))) {
2213 lustre_msg_set_slv(req->rq_repmsg, 0);
2214 lustre_msg_set_limit(req->rq_repmsg, 0);
2219 * OBD is alive here as export is alive, which we checked above.
2221 obd = req->rq_export->exp_obd;
2223 cfs_read_lock(&obd->obd_pool_lock);
2224 lustre_msg_set_slv(req->rq_repmsg, obd->obd_pool_slv);
2225 lustre_msg_set_limit(req->rq_repmsg, obd->obd_pool_limit);
2226 cfs_read_unlock(&obd->obd_pool_lock);
2231 int target_send_reply_msg(struct ptlrpc_request *req, int rc, int fail_id)
2233 if (OBD_FAIL_CHECK_ORSET(fail_id & ~OBD_FAIL_ONCE, OBD_FAIL_ONCE)) {
2234 DEBUG_REQ(D_ERROR, req, "dropping reply");
2239 DEBUG_REQ(D_NET, req, "processing error (%d)", rc);
2240 req->rq_status = rc;
2241 return (ptlrpc_send_error(req, 1));
2243 DEBUG_REQ(D_NET, req, "sending reply");
2246 return (ptlrpc_send_reply(req, PTLRPC_REPLY_MAYBE_DIFFICULT));
2249 void target_send_reply(struct ptlrpc_request *req, int rc, int fail_id)
2252 struct ptlrpc_reply_state *rs;
2253 struct obd_device *obd;
2254 struct obd_export *exp;
2255 struct ptlrpc_service *svc;
2258 if (req->rq_no_reply) {
2263 svc = req->rq_rqbd->rqbd_service;
2264 rs = req->rq_reply_state;
2265 if (rs == NULL || !rs->rs_difficult) {
2267 target_send_reply_msg (req, rc, fail_id);
2272 /* must be an export if locks saved */
2273 LASSERT (req->rq_export != NULL);
2274 /* req/reply consistent */
2275 LASSERT (rs->rs_service == svc);
2278 LASSERT (!rs->rs_scheduled);
2279 LASSERT (!rs->rs_scheduled_ever);
2280 LASSERT (!rs->rs_handled);
2281 LASSERT (!rs->rs_on_net);
2282 LASSERT (rs->rs_export == NULL);
2283 LASSERT (cfs_list_empty(&rs->rs_obd_list));
2284 LASSERT (cfs_list_empty(&rs->rs_exp_list));
2286 exp = class_export_get (req->rq_export);
2289 /* disable reply scheduling while I'm setting up */
2290 rs->rs_scheduled = 1;
2292 rs->rs_xid = req->rq_xid;
2293 rs->rs_transno = req->rq_transno;
2294 rs->rs_export = exp;
2295 rs->rs_opc = lustre_msg_get_opc(rs->rs_msg);
2297 cfs_spin_lock(&exp->exp_uncommitted_replies_lock);
2298 CDEBUG(D_NET, "rs transno = "LPU64", last committed = "LPU64"\n",
2299 rs->rs_transno, exp->exp_last_committed);
2300 if (rs->rs_transno > exp->exp_last_committed) {
2301 /* not committed already */
2302 cfs_list_add_tail(&rs->rs_obd_list,
2303 &exp->exp_uncommitted_replies);
2305 cfs_spin_unlock (&exp->exp_uncommitted_replies_lock);
2307 cfs_spin_lock(&exp->exp_lock);
2308 cfs_list_add_tail(&rs->rs_exp_list, &exp->exp_outstanding_replies);
2309 cfs_spin_unlock(&exp->exp_lock);
2311 netrc = target_send_reply_msg (req, rc, fail_id);
2313 cfs_spin_lock(&svc->srv_rs_lock);
2315 cfs_atomic_inc(&svc->srv_n_difficult_replies);
2318 /* error sending: reply is off the net. Also we need +1
2319 * reply ref until ptlrpc_handle_rs() is done
2320 * with the reply state (if the send was successful, there
2321 * would have been +1 ref for the net, which
2322 * reply_out_callback leaves alone) */
2324 ptlrpc_rs_addref(rs);
2327 cfs_spin_lock(&rs->rs_lock);
2328 if (rs->rs_transno <= exp->exp_last_committed ||
2329 (!rs->rs_on_net && !rs->rs_no_ack) ||
2330 cfs_list_empty(&rs->rs_exp_list) || /* completed already */
2331 cfs_list_empty(&rs->rs_obd_list)) {
2332 CDEBUG(D_HA, "Schedule reply immediately\n");
2333 ptlrpc_dispatch_difficult_reply(rs);
2335 cfs_list_add (&rs->rs_list, &svc->srv_active_replies);
2336 rs->rs_scheduled = 0; /* allow notifier to schedule */
2338 cfs_spin_unlock(&rs->rs_lock);
2339 cfs_spin_unlock(&svc->srv_rs_lock);
2343 int target_handle_ping(struct ptlrpc_request *req)
2345 obd_ping(req->rq_export);
2346 return req_capsule_server_pack(&req->rq_pill);
2349 void target_committed_to_req(struct ptlrpc_request *req)
2351 struct obd_export *exp = req->rq_export;
2353 if (!exp->exp_obd->obd_no_transno && req->rq_repmsg != NULL)
2354 lustre_msg_set_last_committed(req->rq_repmsg,
2355 exp->exp_last_committed);
2357 DEBUG_REQ(D_IOCTL, req, "not sending last_committed update (%d/"
2358 "%d)", exp->exp_obd->obd_no_transno,
2359 req->rq_repmsg == NULL);
2361 CDEBUG(D_INFO, "last_committed "LPU64", transno "LPU64", xid "LPU64"\n",
2362 exp->exp_last_committed, req->rq_transno, req->rq_xid);
2364 EXPORT_SYMBOL(target_committed_to_req);
2366 int target_handle_qc_callback(struct ptlrpc_request *req)
2368 struct obd_quotactl *oqctl;
2369 struct client_obd *cli = &req->rq_export->exp_obd->u.cli;
2371 oqctl = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
2372 if (oqctl == NULL) {
2373 CERROR("Can't unpack obd_quotactl\n");
2377 cli->cl_qchk_stat = oqctl->qc_stat;
2382 #ifdef HAVE_QUOTA_SUPPORT
2383 int target_handle_dqacq_callback(struct ptlrpc_request *req)
2386 struct obd_device *obd = req->rq_export->exp_obd;
2387 struct obd_device *master_obd = NULL, *lov_obd = NULL;
2388 struct obd_device_target *obt;
2389 struct lustre_quota_ctxt *qctxt;
2390 struct qunit_data *qdata = NULL;
2394 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_DROP_QUOTA_REQ))
2397 rc = req_capsule_server_pack(&req->rq_pill);
2399 CERROR("packing reply failed!: rc = %d\n", rc);
2403 LASSERT(req->rq_export);
2405 qdata = quota_get_qdata(req, QUOTA_REQUEST, QUOTA_EXPORT);
2406 if (IS_ERR(qdata)) {
2407 rc = PTR_ERR(qdata);
2408 CDEBUG(D_ERROR, "Can't unpack qunit_data(rc: %d)\n", rc);
2409 req->rq_status = rc;
2413 /* we use the observer */
2414 if (obd_pin_observer(obd, &lov_obd) ||
2415 obd_pin_observer(lov_obd, &master_obd)) {
2416 CERROR("Can't find the observer, it is recovering\n");
2417 req->rq_status = -EAGAIN;
2421 obt = &master_obd->u.obt;
2422 qctxt = &obt->obt_qctxt;
2424 if (!qctxt->lqc_setup || !qctxt->lqc_valid) {
2425 /* quota_type has not been processed yet, return EAGAIN
2426 * until we know whether or not quotas are supposed to
2428 CDEBUG(D_QUOTA, "quota_type not processed yet, return "
2430 req->rq_status = -EAGAIN;
2434 cfs_down_read(&obt->obt_rwsem);
2435 if (qctxt->lqc_lqs_hash == NULL) {
2436 cfs_up_read(&obt->obt_rwsem);
2437 /* quota_type has not been processed yet, return EAGAIN
2438 * until we know whether or not quotas are supposed to
2440 CDEBUG(D_QUOTA, "quota_ctxt is not ready yet, return "
2442 req->rq_status = -EAGAIN;
2446 LASSERT(qctxt->lqc_handler);
2447 rc = qctxt->lqc_handler(master_obd, qdata,
2448 lustre_msg_get_opc(req->rq_reqmsg));
2449 cfs_up_read(&obt->obt_rwsem);
2450 if (rc && rc != -EDQUOT)
2451 CDEBUG(rc == -EBUSY ? D_QUOTA : D_ERROR,
2452 "dqacq/dqrel failed! (rc:%d)\n", rc);
2453 req->rq_status = rc;
2455 rc = quota_copy_qdata(req, qdata, QUOTA_REPLY, QUOTA_EXPORT);
2457 CERROR("Can't pack qunit_data(rc: %d)\n", rc);
2461 /* Block the quota req. b=14840 */
2462 OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_BLOCK_QUOTA_REQ, obd_timeout);
2467 obd_unpin_observer(lov_obd);
2469 obd_unpin_observer(obd);
2471 rc = ptlrpc_reply(req);
2475 #endif /* !__KERNEL__ */
2477 #endif /* HAVE_QUOTA_SUPPORT */
2479 ldlm_mode_t lck_compat_array[] = {
2480 [LCK_EX] LCK_COMPAT_EX,
2481 [LCK_PW] LCK_COMPAT_PW,
2482 [LCK_PR] LCK_COMPAT_PR,
2483 [LCK_CW] LCK_COMPAT_CW,
2484 [LCK_CR] LCK_COMPAT_CR,
2485 [LCK_NL] LCK_COMPAT_NL,
2486 [LCK_GROUP] LCK_COMPAT_GROUP,
2487 [LCK_COS] LCK_COMPAT_COS,
2491 * Rather arbitrary mapping from LDLM error codes to errno values. This should
2492 * not escape to the user level.
2494 int ldlm_error2errno(ldlm_error_t error)
2502 case ELDLM_LOCK_CHANGED:
2505 case ELDLM_LOCK_ABORTED:
2508 case ELDLM_LOCK_REPLACED:
2511 case ELDLM_NO_LOCK_DATA:
2514 case ELDLM_NAMESPACE_EXISTS:
2517 case ELDLM_BAD_NAMESPACE:
2521 if (((int)error) < 0) /* cast to signed type */
2522 result = error; /* as ldlm_error_t can be unsigned */
2524 CERROR("Invalid DLM result code: %d\n", error);
2530 EXPORT_SYMBOL(ldlm_error2errno);
2533 * Dual to ldlm_error2errno(): maps errno values back to ldlm_error_t.
2535 ldlm_error_t ldlm_errno2error(int err_no)
2544 error = ELDLM_LOCK_CHANGED;
2547 error = ELDLM_LOCK_ABORTED;
2550 error = ELDLM_LOCK_REPLACED;
2553 error = ELDLM_NO_LOCK_DATA;
2556 error = ELDLM_NAMESPACE_EXISTS;
2559 error = ELDLM_BAD_NAMESPACE;
2566 EXPORT_SYMBOL(ldlm_errno2error);
2568 #if LUSTRE_TRACKS_LOCK_EXP_REFS
2569 void ldlm_dump_export_locks(struct obd_export *exp)
2571 cfs_spin_lock(&exp->exp_locks_list_guard);
2572 if (!cfs_list_empty(&exp->exp_locks_list)) {
2573 struct ldlm_lock *lock;
2575 CERROR("dumping locks for export %p,"
2576 "ignore if the unmount doesn't hang\n", exp);
2577 cfs_list_for_each_entry(lock, &exp->exp_locks_list, l_exp_refs_link)
2578 ldlm_lock_dump(D_ERROR, lock, 0);
2580 cfs_spin_unlock(&exp->exp_locks_list_guard);
2584 static int target_bulk_timeout(void *data)
2587 /* We don't fail the connection here, because having the export
2588 * killed makes the (vital) call to commitrw very sad.
2593 static inline char *bulk2type(struct ptlrpc_bulk_desc *desc)
2595 return desc->bd_type == BULK_GET_SINK ? "GET" : "PUT";
2598 int target_bulk_io(struct obd_export *exp, struct ptlrpc_bulk_desc *desc,
2599 struct l_wait_info *lwi)
2601 struct ptlrpc_request *req = desc->bd_req;
2605 /* Check if there is eviction in progress, and if so, wait for
2607 if (unlikely(cfs_atomic_read(&exp->exp_obd->obd_evict_inprogress))) {
2608 *lwi = LWI_INTR(NULL, NULL);
2609 rc = l_wait_event(exp->exp_obd->obd_evict_inprogress_waitq,
2610 !cfs_atomic_read(&exp->exp_obd->
2611 obd_evict_inprogress),
2615 /* Check if client was evicted or tried to reconnect already */
2616 if (exp->exp_failed || exp->exp_abort_active_req) {
2619 if (desc->bd_type == BULK_PUT_SINK)
2620 rc = sptlrpc_svc_wrap_bulk(req, desc);
2622 rc = ptlrpc_start_bulk_transfer(desc);
2625 if (rc == 0 && OBD_FAIL_CHECK(OBD_FAIL_MDS_SENDPAGE)) {
2626 ptlrpc_abort_bulk(desc);
2627 } else if (rc == 0) {
2628 time_t start = cfs_time_current_sec();
2630 long timeoutl = req->rq_deadline - cfs_time_current_sec();
2631 cfs_duration_t timeout = timeoutl <= 0 ?
2632 CFS_TICK : cfs_time_seconds(timeoutl);
2633 *lwi = LWI_TIMEOUT_INTERVAL(timeout,
2634 cfs_time_seconds(1),
2635 target_bulk_timeout,
2637 rc = l_wait_event(desc->bd_waitq,
2638 !ptlrpc_server_bulk_active(desc) ||
2640 exp->exp_abort_active_req,
2642 LASSERT(rc == 0 || rc == -ETIMEDOUT);
2643 /* Wait again if we changed deadline */
2644 } while ((rc == -ETIMEDOUT) &&
2645 (req->rq_deadline > cfs_time_current_sec()));
2647 if (rc == -ETIMEDOUT) {
2648 DEBUG_REQ(D_ERROR, req,
2649 "timeout on bulk %s after %ld%+lds",
2651 req->rq_deadline - start,
2652 cfs_time_current_sec() -
2654 ptlrpc_abort_bulk(desc);
2655 } else if (exp->exp_failed) {
2656 DEBUG_REQ(D_ERROR, req, "Eviction on bulk %s",
2659 ptlrpc_abort_bulk(desc);
2660 } else if (exp->exp_abort_active_req) {
2661 DEBUG_REQ(D_ERROR, req, "Reconnect on bulk %s",
2663 /* we don't reply anyway */
2665 ptlrpc_abort_bulk(desc);
2666 } else if (!desc->bd_success ||
2667 desc->bd_nob_transferred != desc->bd_nob) {
2668 DEBUG_REQ(D_ERROR, req, "%s bulk %s %d(%d)",
2670 "truncated" : "network error on",
2672 desc->bd_nob_transferred,
2674 /* XXX should this be a different errno? */
2676 } else if (desc->bd_type == BULK_GET_SINK) {
2677 rc = sptlrpc_svc_unwrap_bulk(req, desc);
2680 DEBUG_REQ(D_ERROR, req, "bulk %s failed: rc %d",
2681 bulk2type(desc), rc);
2686 EXPORT_SYMBOL(target_bulk_io);